On 20/07/20 12:26, pet...@infradead.org wrote: > --- > kernel/sched/core.c | 34 ++++++++++++++++++++++++++++------ > 1 file changed, 28 insertions(+), 6 deletions(-) > > diff --git a/kernel/sched/core.c b/kernel/sched/core.c > index e15543cb84812..b5973d7fa521c 100644 > --- a/kernel/sched/core.c > +++ b/kernel/sched/core.c > @@ -4100,9 +4100,9 @@ pick_next_task(struct rq *rq, struct task_struct *prev, > struct rq_flags *rf) > */ > static void __sched notrace __schedule(bool preempt) > { > + unsigned long prev_state, tmp_state; > struct task_struct *prev, *next; > unsigned long *switch_count; > - unsigned long prev_state; > struct rq_flags rf; > struct rq *rq; > int cpu; > @@ -4140,16 +4140,38 @@ static void __sched notrace __schedule(bool preempt) > rq_lock(rq, &rf); > smp_mb__after_spinlock(); > > + /* > + * We must re-load prev->state in case ttwu_remote() changed it > + * before we acquired rq->lock. > + */ > + tmp_state = prev->state; > + if (unlikely(prev_state != tmp_state)) { > + /* > + * ptrace_{,un}freeze_traced() think it is cool to change > + * ->state around behind our backs between TASK_TRACED and > + * __TASK_TRACED. > + * > + * This is safe because this, as well as any __TASK_TRACED > + * wakeups are under siglock. > + * > + * For any other case, a changed prev_state must be to > + * TASK_RUNNING, such that when it blocks, the load has > + * happened before the smp_mb(). > + * > + * Also see the comment with deactivate_task(). > + */ > + SCHED_WARN_ON(tmp_state && (prev_state & __TASK_TRACED && > + !(tmp_state & __TASK_TRACED))); > +
IIUC if the state changed and isn't TASK_RUNNING it *has* to have __TASK_TRACED, so can't that be SCHED_WARN_ON(tmp_state && !(tmp_state & __TASK_TRACED)); Other than that, LGTM. > + prev_state = tmp_state; > + } > + > /* Promote REQ to ACT */ > rq->clock_update_flags <<= 1; > update_rq_clock(rq); > > switch_count = &prev->nivcsw; > - /* > - * We must re-load prev->state in case ttwu_remote() changed it > - * before we acquired rq->lock. > - */ > - if (!preempt && prev_state && prev_state == prev->state) { > + if (!preempt && prev_state) { > if (signal_pending_state(prev_state, prev)) { > prev->state = TASK_RUNNING; > } else {