Skip to contentSkip to conversation

sched, kvm: fix race condition involving sched_in_preempt_notifers

PATCH In review Tejun Heo 3 participants 9 messages Nov 13, 2009 – Nov 30, 2009 Scheduler

lore
Share & tools
lore

Tejun Heo

author
#0 lore raw

PATCH 1/1 sched, kvm: fix race condition involving sched_in_preempt_notifers

In finish_task_switch(), fire_sched_in_preempt_notifiers() is called after finish_lock_switch(). However, depending on architecture, preemption can be enabled after finish_lock_switch() which breaks the semantics of preempt notifiers. Move it before finish_arch_switch(). This also makes in notifiers symmetric to out notifiers in terms of locking - now both are called under rq lock.

NOT_SIGNED_OFF_YET
Cc: Ingo Molnar <[email protected]>
Cc: Avi Kivity <[email protected]>
---
Avi, I think kvm should be fine with this but I haven't tested it. Does this look okay to you? If so, can you please route this through kvm tree with my signoff?

Thanks.

 kernel/sched.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched.c +1 -1
Index: work/kernel/sched.c
===================================================================
@@ -2751,9 +2751,9 @@ static void finish_task_switch(struct rq
 	prev_state = prev->state;
 	finish_arch_switch(prev);
 	perf_event_task_sched_in(current, cpu_of(rq));
+	fire_sched_in_preempt_notifiers(current);
 	finish_lock_switch(rq, prev);
 
-	fire_sched_in_preempt_notifiers(current);
 	if (mm)
 		mmdrop(mm);
 	if (unlikely(prev_state == TASK_DEAD)) {
#1 lore raw
Quoted 7 lines · Tejun Heo

In finish_task_switch(), fire_sched_in_preempt_notifiers() is called after finish_lock_switch(). However, depending on architecture, preemption can be enabled after finish_lock_switch() which breaks the semantics of preempt notifiers. Move it before finish_arch_switch(). This also makes in notifiers symmetric to out notifiers in terms of locking - now both are called under rq lock.

Quoted 7 lines

NOT_SIGNED_OFF_YET
Cc: Ingo Molnar <[email protected]>
Cc: Avi Kivity <[email protected]>
---
Avi, I think kvm should be fine with this but I haven't tested it. Does this look okay to you? If so, can you please route this through kvm tree with my signoff?

I'd like to have Avi's Ack for it, but we want to do sched.c changes via the scheduler tree.

Ingo

Acked-by
#2 lore raw
Quoted 11 lines · Ingo Molnar
Quoted 8 lines · Tejun Heo

In finish_task_switch(), fire_sched_in_preempt_notifiers() is called after finish_lock_switch(). However, depending on architecture, preemption can be enabled after finish_lock_switch() which breaks the semantics of preempt notifiers. Move it before finish_arch_switch(). This also makes in notifiers symmetric to out notifiers in terms of locking - now both are called under rq lock.

Quoted 1 line

I'd like to have Avi's Ack for it,

Quoted 3 lines

but we want to do sched.c changes via
the scheduler tree.

Definitely.

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
author
#3 lore raw
Quoted 10 lines · Avi Kivity
Quoted 9 lines · Ingo Molnar
Quoted 8 lines · Tejun Heo

In finish_task_switch(), fire_sched_in_preempt_notifiers() is called after finish_lock_switch(). However, depending on architecture, preemption can be enabled after finish_lock_switch() which breaks the semantics of preempt notifiers. Move it before finish_arch_switch(). This also makes in notifiers symmetric to out notifiers in terms of locking - now both are called under rq lock.

Quoted 6 lines
Quoted 1 line

I'd like to have Avi's Ack for it,

Quoted 2 lines

but we want to do sched.c changes via
the scheduler tree.

So, this one was a bust. Given that the only platform which might have been affected by the original bug is ia64 and it's very unlikely to happen. I think reverting this from sched/urgent would be the right thing to do at this point if the branch is headed for another push to Linus. Avi, what do you think?

Thanks.

Signature · 1 line
tejun
#4 lore raw
Quoted 11 lines · Tejun Heo
Quoted 5 lines
Quoted 5 lines

but we want to do sched.c changes via
the scheduler tree.

So, this one was a bust. Given that the only platform which might have been affected by the original bug is ia64 and it's very unlikely to happen. I think reverting this from sched/urgent would be the right thing to do at this point if the branch is headed for another push to Linus. Avi, what do you think?

I agree.

Signature · 1 line
error compiling committee.c: too many arguments to function
#5 lore raw
Quoted 5 lines · Avi Kivity
Quoted 4 lines · Tejun Heo
Quoted 4 lines
Quoted 4 lines

but we want to do sched.c changes via
the scheduler tree.

Quoted 7 lines
Quoted 5 lines

So, this one was a bust. Given that the only platform which might have been affected by the original bug is ia64 and it's very unlikely to happen. I think reverting this from sched/urgent would be the right thing to do at this point if the branch is headed for another push to Linus. Avi, what do you think?

I agree.

that sched/urgent commit is not queued up for 2.6.32 (Linus indicated there wont be an -rc9), it spills over to the 2.6.33 merge window.

Ingo

#6 lore raw
Quoted 4 lines · Ingo Molnar

that sched/urgent commit is not queued up for 2.6.32 (Linus indicated there wont be an -rc9), it spills over to the 2.6.33 merge window.

It shouldn't go into .33 either, until we figure out how to fix it.

Signature · 1 line
error compiling committee.c: too many arguments to function
author
#7 lore raw

PATCH 1/1 sched, kvm: fix race condition involving sched_in_preempt_notifers

498657a478c60be092208422fefa9c7b248729c2 incorrectly assumed that preempt wasn't disabled around context_switch() and thus was fixing imaginary problem. It also broke kvm because it depended on ->sched_in() to be called with irq enabled so that it can do smp calls from there.

Revert the incorrect commit and add comment describing different contexts under with the two callbacks are invoked.

Avi: spotted transposed in/out in the added comment.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Avi Kivity <[email protected]>

---
Avi, thanks for spotting it.

 include/linux/preempt.h |    5 +++++
 kernel/sched.c          |    2 +-
 2 files changed, 6 insertions(+), 1 deletion(-)
include/linux/preempt.h +5 -0
index 72b1a10..2e681d9 100644
@@ -105,6 +105,11 @@ struct preempt_notifier;
  * @sched_out: we've just been preempted
  *    notifier: struct preempt_notifier for the task being preempted
  *    next: the task that's kicking us out
+ *
+ * Please note that sched_in and out are called under different
+ * contexts.  sched_out is called with rq lock held and irq disabled
+ * while sched_in is called without rq lock and irq enabled.  This
+ * difference is intentional and depended upon by its users.
  */
 struct preempt_ops {
 	void (*sched_in)(struct preempt_notifier *notifier, int cpu);
kernel/sched.c +1 -1
index 3c91f11..e36c868 100644
@@ -2758,9 +2758,9 @@ static void finish_task_switch(struct rq *rq, struct task_struct *prev)
 	prev_state = prev->state;
 	finish_arch_switch(prev);
 	perf_event_task_sched_in(current, cpu_of(rq));
-	fire_sched_in_preempt_notifiers(current);
 	finish_lock_switch(rq, prev);
 
+	fire_sched_in_preempt_notifiers(current);
 	if (mm)
 		mmdrop(mm);
 	if (unlikely(prev_state == TASK_DEAD)) {
#8 lore raw

[tip:sched/urgent] sched, kvm: Fix race condition involving sched_in_preempt_notifers

In reply to Tejun Heo
Commit-ID:  498657a478c60be092208422fefa9c7b248729c2
Gitweb:     http://git.kernel.org/tip/498657a478c60be092208422fefa9c7b248729c2
Author:     Tejun Heo <[email protected]>
AuthorDate: Fri, 13 Nov 2009 18:33:53 +0900
Committer:  Ingo Molnar <[email protected]>
CommitDate: Sun, 15 Nov 2009 09:59:54 +0100

sched, kvm: Fix race condition involving sched_in_preempt_notifers

In finish_task_switch(), fire_sched_in_preempt_notifiers() is called after finish_lock_switch().

However, depending on architecture, preemption can be enabled after finish_lock_switch() which breaks the semantics of preempt notifiers.

So move it before finish_arch_switch(). This also makes the in- notifiers symmetric to out- notifiers in terms of locking - now both are called under rq lock.

LKML-Reference: <[email protected]>

---
 kernel/sched.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
kernel/sched.c +1 -1
index 701eca4..cea2bea 100644
@@ -2758,9 +2758,9 @@ static void finish_task_switch(struct rq *rq, struct task_struct *prev)
 	prev_state = prev->state;
 	finish_arch_switch(prev);
 	perf_event_task_sched_in(current, cpu_of(rq));
+	fire_sched_in_preempt_notifiers(current);
 	finish_lock_switch(rq, prev);
 
-	fire_sched_in_preempt_notifiers(current);
 	if (mm)
 		mmdrop(mm);
 	if (unlikely(prev_state == TASK_DEAD)) {