Skip to content

Commit 5d808c7

Browse files
dtccccPeter Zijlstra
authored andcommitted
sched: Fix race between yield_to() and try_to_wake_up()
We met a SCHED_WARN in set_next_buddy(): __warn_printk set_next_buddy yield_to_task_fair yield_to kvm_vcpu_yield_to [kvm] ... After a short dig, we found the rq_lock held by yield_to() may not be exactly the rq that the target task belongs to. There is a race window against try_to_wake_up(). CPU0 target_task blocking on CPU1 lock rq0 & rq1 double check task_rq == p_rq, ok woken to CPU2 (lock task_pi & rq2) task_rq = rq2 yield_to_task_fair (w/o lock rq2) In this race window, yield_to() is operating the task w/o the correct lock. Fix this by taking task pi_lock first. Fixes: d95f412 ("sched: Add yield_to(task, preempt) functionality") Signed-off-by: Tianchen Ding <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 7c8cd56 commit 5d808c7

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

kernel/sched/syscalls.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1433,7 +1433,7 @@ int __sched yield_to(struct task_struct *p, bool preempt)
14331433
struct rq *rq, *p_rq;
14341434
int yielded = 0;
14351435

1436-
scoped_guard (irqsave) {
1436+
scoped_guard (raw_spinlock_irqsave, &p->pi_lock) {
14371437
rq = this_rq();
14381438

14391439
again:

0 commit comments

Comments
 (0)