sched: fix BUG in preempt_notifier
PATCH 1/1 sched: fix BUG in preempt_notifier
--- kernel/sched/core.c | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-)
kernel/sched/core.c +4 -21
@@ -2318,15 +2318,12 @@ void wake_up_new_task(struct task_struct *p)#ifdef CONFIG_PREEMPT_NOTIFIERS -static struct static_key preempt_notifier_key =STATIC_KEY_INIT_FALSE ; -/** * preempt_notifier_register - tell me when current is being preempted & rescheduled * @notifier: notifier struct to register */ void preempt_notifier_register (struct preempt_notifier *notifier) { -static_key_slow_inc (&preempt_notifier_key);hlist_add_head (¬ifier->link, ¤t->preempt_notifiers); }EXPORT_SYMBOL_GPL (preempt_notifier_register);
@@ -2340,11 +2337,10 @@ EXPORT_SYMBOL_GPL(preempt_notifier_register);void preempt_notifier_unregister (struct preempt_notifier *notifier) {hlist_del (¬ifier->link); -static_key_slow_dec (&preempt_notifier_key); }EXPORT_SYMBOL_GPL (preempt_notifier_unregister); -static void __fire_sched_in_preempt_notifiers (struct task_struct *curr) +static void fire_sched_in_preempt_notifiers (struct task_struct *curr) {struct preempt_notifier *notifier;
@@ -2352,14 +2348,9 @@ static void __fire_sched_in_preempt_notifiers(struct task_struct *curr) notifier->ops->sched_in (notifier,raw_smp_processor_id ()); } -static __always_inline void fire_sched_in_preempt_notifiers(struct task_struct *curr) -{ -if (static_key_false (&preempt_notifier_key)) -__fire_sched_in_preempt_notifiers (curr); -}static void -__fire_sched_out_preempt_notifiers (struct task_struct *curr, +fire_sched_out_preempt_notifiers (struct task_struct *curr,struct task_struct *next) {struct preempt_notifier *notifier;
@@ -2368,21 +2359,13 @@ __fire_sched_out_preempt_notifiers(struct task_struct *curr, notifier->ops->sched_out(notifier, next); } -static __always_inline void -fire_sched_out_preempt_notifiers (struct task_struct *curr, -struct task_struct *next) -{ -if (static_key_false (&preempt_notifier_key)) -__fire_sched_out_preempt_notifiers (curr, next); -} -#else /* !CONFIG_PREEMPT_NOTIFIERS */ -static inline void fire_sched_in_preempt_notifiers (struct task_struct *curr) +static void fire_sched_in_preempt_notifiers (struct task_struct *curr) { } -static inline void +static void fire_sched_out_preempt_notifiers (struct task_struct *curr,struct task_struct *next) {
Signature · 1 line
2.1.0
more information about the bug is below:
https://bugzilla.kernel.org/show_bug.cgi?id=100701
the preempt_notifier_unregister function is run in_atomic context, but static_key_slow_dec function may cause sleep.
Quoted 81 lines · Lidong Chen
Signed-off-by Lidong Chen <[email protected]>---
kernel/sched/core.c| 25 ++++---------------------1 file changed, 4 insertions(+), 21 deletions(-)kernel/sched/core.c +1
-10@@ -2318,15 +2318,12 @@ void wake_up_new_task(struct task_struct *p)#ifdef CONFIG_PREEMPT_NOTIFIERS -static struct static_key preempt_notifier_key =STATIC_KEY_INIT_FALSE ; -/** * preempt_notifier_register - tell me when current is being preempted & rescheduled * @notifier: notifier struct to register */ void preempt_notifier_register (struct preempt_notifier *notifier) { -static_key_slow_inc (&preempt_notifier_key);hlist_add_head (¬ifier->link, ¤t->preempt_notifiers); }EXPORT_SYMBOL_GPL (preempt_notifier_register);@@ -2340,11 +2337,10 @@ EXPORT_SYMBOL_GPL(preempt_notifier_register);void preempt_notifier_unregister (struct preempt_notifier *notifier) {hlist_del (¬ifier->link); -static_key_slow_dec (&preempt_notifier_key); }EXPORT_SYMBOL_GPL (preempt_notifier_unregister); -static void __fire_sched_in_preempt_notifiers (struct task_struct *curr) +static void fire_sched_in_preempt_notifiers (struct task_struct *curr) {struct preempt_notifier *notifier;@@ -2352,14 +2348,9 @@ static void __fire_sched_in_preempt_notifiers(struct task_struct *curr) notifier->ops->sched_in (notifier,raw_smp_processor_id ()); } -static __always_inline void fire_sched_in_preempt_notifiers(struct task_struct *curr) -{ -if (static_key_false (&preempt_notifier_key)) -__fire_sched_in_preempt_notifiers (curr); -}static void
kernel/sched/core.c +1
-1-__fire_sched_out_preempt_notifiers(struct task_struct *curr, +fire_sched_out_preempt_notifiers(struct task_struct *curr,struct
task_struct*next){ struct preempt_notifier *notifier;kernel/sched/core.c +2
-10@@ -2368,21 +2359,13 @@ __fire_sched_out_preempt_notifiers(struct task_struct *curr, notifier->ops->sched_out(notifier, next); } -static __always_inline void -fire_sched_out_preempt_notifiers (struct task_struct *curr, -struct task_struct *next) -{ -if (static_key_false (&preempt_notifier_key)) -__fire_sched_out_preempt_notifiers (curr, next); -} -#else /* !CONFIG_PREEMPT_NOTIFIERS */ -static inline void fire_sched_in_preempt_notifiers (struct task_struct *curr) +static void fire_sched_in_preempt_notifiers (struct task_struct *curr) { } -static inline void +static void fire_sched_out_preempt_notifiers (struct task_struct *curr,struct task_struct *next) {Signature · 1 line
2.1.0
���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m����������?�I�
Quoted 4 lines · cfme_admin
more information about the bug is below:
https://bugzilla.kernel.org/show_bug.cgi?id=100701the
preempt_notifier_unregisterfunction is runin_atomiccontext, butstatic_key_slow_decfunction may cause sleep.
This bug should already be fixed by this pending commit:
6efde1d3716b ("sched/preempt, kvm: Fix KVM preempt_notifier usage")
Attached below.
Thanks,
Ingo
========================>
Quoted 1 line
From
6efde1d3716b7d055d3310bccba2df244cf430d7Mon Sep 17 00:00:00 2001
From: Peter Zijlstra <[email protected]>
Date: Thu, 25 Jun 2015 14:55:14 +0200
Subject: [PATCH] sched/preempt, kvm: Fix KVM preempt_notifier usage
The preempt-notifier API needs to sleep with the addition of the static_key, we do however need to hold off preemption while modifying the preempt notifier list, otherwise a preemption could observe an inconsistent list state.
There is no reason to have preemption disabled in the caller, registering a preempt notifier is a purely task local affair.
Therefore move the preempt_disable into the functions and change the callers to a preemptible context.
--- kernel/sched/core.c | 11 +++++++++++ virt/kvm/kvm_main.c | 5 +++-- 2 files changed, 14 insertions(+), 2 deletions(-)
kernel/sched/core.c +11 -0
@@ -2138,7 +2138,12 @@ static struct static_key preempt_notifier_key = STATIC_KEY_INIT_FALSE;void preempt_notifier_register (struct preempt_notifier *notifier) {static_key_slow_inc (&preempt_notifier_key); +/* +* Avoid preemption while changing the preempt notifier list. +*/ +preempt_disable ();hlist_add_head (¬ifier->link, ¤t->preempt_notifiers); +preempt_enable (); }EXPORT_SYMBOL_GPL (preempt_notifier_register);
@@ -2150,7 +2155,13 @@ EXPORT_SYMBOL_GPL(preempt_notifier_register);*/ void preempt_notifier_unregister (struct preempt_notifier *notifier) { +/* +* Avoid preemption while changing the preempt notifier list. +*/ +preempt_disable ();hlist_del (¬ifier->link); +preempt_enable (); +static_key_slow_dec (&preempt_notifier_key); }EXPORT_SYMBOL_GPL (preempt_notifier_unregister);
virt/kvm/kvm_main.c +3 -2
@@ -128,8 +128,9 @@ int vcpu_load(struct kvm_vcpu *vcpu)if (mutex_lock_killable (&vcpu->mutex))return -EINTR ; - cpu =get_cpu ();preempt_notifier_register (&vcpu->preempt_notifier); + + cpu =get_cpu ();kvm_arch_vcpu_load (vcpu, cpu);put_cpu ();return 0 ;
@@ -139,8 +140,8 @@ void vcpu_put(struct kvm_vcpu *vcpu) {preempt_disable ();kvm_arch_vcpu_put (vcpu); -preempt_notifier_unregister (&vcpu->preempt_notifier);preempt_enable (); +preempt_notifier_unregister (&vcpu->preempt_notifier);mutex_unlock (&vcpu->mutex); }
Quoted 1 line · Lidong Chen
Signed-off-by Lidong Chen <[email protected]>
Subject: sched,kvm: Fix KVM preempt_notifier usage
The preempt-notifier API needs to sleep with the addition of the static_key, we do however need to hold off preemption while modifying the preempt notifier list, otherwise a preemption could observe an inconsistent list state.
There is no reason to have preemption disabled in the caller, registering a preempt notifier is a purely task local affair.
Therefore move the preempt_disable into the functions and change the callers to a preemptible context.
If this commit in tip tree fix your bug?
Regards,
Wanpeng Li
Quoted 77 lines
--- kernel/sched/core.c | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-)kernel/sched/core.c +4
-21@@ -2318,15 +2318,12 @@ void wake_up_new_task(struct task_struct *p)#ifdef CONFIG_PREEMPT_NOTIFIERS -static struct static_key preempt_notifier_key =STATIC_KEY_INIT_FALSE ; -/** * preempt_notifier_register - tell me when current is being preempted & rescheduled * @notifier: notifier struct to register */ void preempt_notifier_register (struct preempt_notifier *notifier) { -static_key_slow_inc (&preempt_notifier_key);hlist_add_head (¬ifier->link, ¤t->preempt_notifiers); }EXPORT_SYMBOL_GPL (preempt_notifier_register);@@ -2340,11 +2337,10 @@ EXPORT_SYMBOL_GPL(preempt_notifier_register);void preempt_notifier_unregister (struct preempt_notifier *notifier) {hlist_del (¬ifier->link); -static_key_slow_dec (&preempt_notifier_key); }EXPORT_SYMBOL_GPL (preempt_notifier_unregister); -static void __fire_sched_in_preempt_notifiers (struct task_struct *curr) +static void fire_sched_in_preempt_notifiers (struct task_struct *curr) {struct preempt_notifier *notifier;@@ -2352,14 +2348,9 @@ static void __fire_sched_in_preempt_notifiers(struct task_struct *curr) notifier->ops->sched_in (notifier,raw_smp_processor_id ()); } -static __always_inline void fire_sched_in_preempt_notifiers(struct task_struct *curr) -{ -if (static_key_false (&preempt_notifier_key)) -__fire_sched_in_preempt_notifiers (curr); -}static void -__fire_sched_out_preempt_notifiers (struct task_struct *curr, +fire_sched_out_preempt_notifiers (struct task_struct *curr,struct task_struct *next) {struct preempt_notifier *notifier;@@ -2368,21 +2359,13 @@ __fire_sched_out_preempt_notifiers(struct task_struct *curr, notifier->ops->sched_out(notifier, next); } -static __always_inline void -fire_sched_out_preempt_notifiers (struct task_struct *curr, -struct task_struct *next) -{ -if (static_key_false (&preempt_notifier_key)) -__fire_sched_out_preempt_notifiers (curr, next); -} -#else /* !CONFIG_PREEMPT_NOTIFIERS */ -static inline void fire_sched_in_preempt_notifiers (struct task_struct *curr) +static void fire_sched_in_preempt_notifiers (struct task_struct *curr) { } -static inline void +static void fire_sched_out_preempt_notifiers (struct task_struct *curr,struct task_struct *next) {
This is a better way to solve this bug.
Thanks.
Quoted 101 lines · Wanpeng Li
Quoted 1 line · Lidong Chen
Signed-off-by Lidong Chen <[email protected]>Subject: sched,kvm: Fix KVM
preempt_notifierusageThe preempt-notifier API needs to sleep with the addition of the
static_key, we do however need to hold off preemption while modifying the preempt notifier list, otherwise a preemption could observe an inconsistent list state.There is no reason to have preemption disabled in the caller, registering a preempt notifier is a purely task local affair.
Therefore move the
preempt_disableinto the functions and change the callers to a preemptible context.Cc Gleb Natapov<[email protected]>Fixes 1cde2930e154 ("sched/preempt: Add static_key() to preempt_notifiers")Reported-and-Tested-by Pontus Fuchs<[email protected]>Signed-off-by Peter Zijlstra (Intel)<[email protected]>If this commit in tip tree fix your bug?
Regards,
Wanpeng LiQuoted 72 lines
--- kernel/sched/core.c | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-)kernel/sched/core.c +3
-20@@ -2318,15 +2318,12 @@ void wake_up_new_task(struct task_struct *p)#ifdef CONFIG_PREEMPT_NOTIFIERS -static struct static_key preempt_notifier_key =STATIC_KEY_INIT_FALSE ; -/** * preempt_notifier_register - tell me when current is being preempted & rescheduled * @notifier: notifier struct to register */ void preempt_notifier_register (struct preempt_notifier *notifier) { -static_key_slow_inc (&preempt_notifier_key);hlist_add_head (¬ifier->link, ¤t->preempt_notifiers); }EXPORT_SYMBOL_GPL (preempt_notifier_register);@@ -2340,11 +2337,10 @@ EXPORT_SYMBOL_GPL(preempt_notifier_register);void preempt_notifier_unregister (struct preempt_notifier *notifier) {hlist_del (¬ifier->link); -static_key_slow_dec (&preempt_notifier_key); }EXPORT_SYMBOL_GPL (preempt_notifier_unregister); -static void __fire_sched_in_preempt_notifiers (struct task_struct *curr) +static void fire_sched_in_preempt_notifiers (struct task_struct *curr) {struct preempt_notifier *notifier;@@ -2352,14 +2348,9 @@ static void __fire_sched_in_preempt_notifiers(struct task_struct *curr) notifier->ops->sched_in (notifier,raw_smp_processor_id ()); } -static __always_inline void fire_sched_in_preempt_notifiers(struct task_struct *curr) -{ -if (static_key_false (&preempt_notifier_key)) -__fire_sched_in_preempt_notifiers (curr); -}static void -__fire_sched_out_preempt_notifiers (struct task_struct *curr, +fire_sched_out_preempt_notifiers (struct task_struct *curr,struct task_struct *next) {struct preempt_notifier *notifier;@@ -2368,21 +2359,13 @@ __fire_sched_out_preempt_notifiers(struct task_struct *curr, notifier->ops->sched_out(notifier, next); } -static __always_inline void -fire_sched_out_preempt_notifiers (struct task_struct *curr, -struct task_struct *next) -{ -if (static_key_false (&preempt_notifier_key)) -__fire_sched_out_preempt_notifiers (curr, next); -} -#else /* !CONFIG_PREEMPT_NOTIFIERS */ -static inline void fire_sched_in_preempt_notifiers (struct task_struct *curr) +static void fire_sched_in_preempt_notifiers (struct task_struct *curr) { }
Quoted 6 lines
kernel/sched/core.c __fire_sched_out_preempt_notifiers(struct task_struct *curr, · line 2371
Quoted 5 lines
kernel/sched/core.c +1
-1-static inline void +static void fire_sched_out_preempt_notifiers (struct task_struct *curr,struct task_struct *next) {
���{.n�+�������+%�����ݶ��w��{.n�+����{��G�����{ay�ʇڙ�,j��f���h��������z_�(�階�ݢj"���m������G������?����&���~��iO��z��v�^�m����������?�I�