sched, kvm: fix race condition involving sched_in_preempt_notifers
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
@@ -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 )) {
Quoted 7 lines · Tejun Heo
In
finish_task_switch(),fire_sched_in_preempt_notifiers()is called afterfinish_lock_switch(). However, depending on architecture, preemption can be enabled afterfinish_lock_switch()which breaks the semantics of preempt notifiers. Move it beforefinish_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
Avi Kivity
Acked-byQuoted 11 lines · Ingo Molnar
Quoted 8 lines · Tejun Heo
In
finish_task_switch(),fire_sched_in_preempt_notifiers()is called afterfinish_lock_switch(). However, depending on architecture, preemption can be enabled afterfinish_lock_switch()which breaks the semantics of preempt notifiers. Move it beforefinish_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.cchanges via
the scheduler tree.
Definitely.
Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
[tip:sched/urgent] sched, kvm: Fix race condition involving sched_in_preempt_notifers
In reply to Tejun HeoCommit-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
@@ -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 )) {
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 afterfinish_lock_switch(). However, depending on architecture, preemption can be enabled afterfinish_lock_switch()which breaks the semantics of preempt notifiers. Move it beforefinish_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,
Acked-by Avi Kivity <[email protected]>Quoted 2 lines
but we want to do
sched.cchanges 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
Quoted 11 lines · Tejun Heo
Quoted 5 lines
Quoted 5 lines
but we want to do
sched.cchanges 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
Quoted 5 lines · Avi Kivity
Quoted 4 lines · Tejun Heo
Quoted 4 lines
Quoted 4 lines
but we want to do
sched.cchanges 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
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
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.
---
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
@@ -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
@@ -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 )) {