Race condition between CPU hotplug off flow and __sched_setscheduler()
Subsystems (2)
Hi all
There is a race condition between CPU hotplug off flow and __sched_setscheduler(), which will cause hang-up in CPU hotplug off flow.
Syndrome:
During hotplug off flow in CPU_A, it blocks on CPUHP_AP_SCHED_WAIT_EMPTY state when enters rcuwait_wait_event(). In that moment, CPU_A stays in idle and cannot wake up stopper thread(cpuhp/A) to continue CPU_A hotplug off flow.
Root cause:Balance_push() callback has been stolen by CPU_B in executing __sched_setscheduler() func., which should be executed in idle task of CPU_A to wake up stopper thread (cpuhp/A) through calling rcuwait_wake_up(&rq->hotplug_wait).
Racing flow as below:CPU_A is going to hotplug off and set rq->balance_callback = &balance_push_callback, then CPU_A should use balance_push() to push the task out and release rq_lock.
But if CPU_B do __sched_setscheduler() before CPU_A switch to swapper/A, CPU_B use splice_balance_callbacks() to steal rq-
Quoted 1 line
balance_callbackand set theCPU_Arq->balance_callback= NULL.
Due to rq->balance_callback is NULL,
so swapper/A could not do balance_push() at CPU_A, Due to rq(rq_A) != this_rq(rq_B),
so swapper/A could not do rcuwait_wake_up() at CPU_B.
Racing flow:
-----------------------------------------------------------------------CPU_A (Hotplug down)
-----------------------------------------------------------------------
State: CPUHP_AP_ACTIVEsched_cpu_deactivate()
-> balance_push_set(cpu, true)
-> rq_A->balance_callback = &balance_push_callback
=> CPU_A set rq_A balance_callback here.
State: CPUHP_AP_SCHED_WAIT_EMPTYsched_cpu_wait_empty()
-> balance_hotplug_wait()
-> rcuwait_wait_event(&rq->hotplug_wait)
=> CPU_A do while loop to push task out from CPU_A,
until swapper/A wake up cpuhp/A.
-> schedule()
-> rq_lock(rq, &rf)
-> context_switch()
-> finish_lock_switch()
-> __balance_callbacks(rq_A)
-> do_balance_callbacks(rq,
splice_balance_callbacks(rq))
-> balance_push(rq_A)
-> raw_spin_rq_unlock_irq(rq_A)
=> CPU_A release rq_A lock.CPU_A release rq_A lock, CPU_B can get rq_A lock.
-----------------------------------------------------------------------CPU_B (do __sched_setscheduler(), set rq_A->balance_callback = NULL)
-----------------------------------------------------------------------__sched_setscheduler(p) => task_rq(p) is rq_A
-> task_rq_lock(rq_A)
-> splice_balance_callbacks(rq_A)
-> if (head)
rq_A->balance_callback = NULL
=> CPU_B steal rq_A->balance_callback.
-> task_rq_unlock(rq_A)CPU_B release rq_A lock, CPU_A can get rq_A lock and switch to swapper/A.
-----------------------------------------------------------------------CPU_A (Hotplug down)
-----------------------------------------------------------------------
switch to swapper/A:schedule()
-> rq_lock(rq, &rf)
-> context_switch()
-> finish_lock_switch()
-> __balance_callbacks(rq_A)
-> do_balance_callbacks(rq, NULL)
=> Because rq_A->balance_callback = NULL,
swapper/A could not do rcuwait_wake_up().
-> raw_spin_rq_unlock_irq(rq_A) -----------------------------------------------------------------------CPU_B (do __sched_setscheduler(), set rq_A->balance_callback = NULL)
-----------------------------------------------------------------------balance_callbacks(rq_A, head)
-> balance_push(rq_A)
-> rq->balance_callback = &balance_push_callback;
-> if (rq != this_rq())
return;
=> Because rq = rq_A, this_rq = rq_B,
swapper/A could not do rcuwait_wake_up().
-----------------------------------------------------------------------CPU_A (Hotplug down)
-----------------------------------------------------------------------rcuwait_wait_event(&rq->hotplug_wait)
=> swapper/A could not do rcuwait_wake_up(),
it cannot wake up stopper thread(cpuhp/A),
so system could not exit the while loop at rcuwait_wait_event.
Do you have any suggestion or solution for this issue? Thank you.
Best regards,
Jing-Ting Wu
Peter Zijlstra
maintainerQuoted 6 lines · Jing-Ting Wu
Hi all
There is a race condition between CPU hotplug off flow and
__sched_setscheduler(), which will cause hang-up in CPU hotplug off flow.
Oooh, you're using core scheduling and the A/B are SMT siblings?
Peter Zijlstra
maintainerQuoted 9 lines · Peter Zijlstra
Quoted 6 lines · Jing-Ting Wu
Hi all
There is a race condition between CPU hotplug off flow and
__sched_setscheduler(), which will cause hang-up in CPU hotplug off flow.Oooh, you're using core scheduling and the A/B are SMT siblings?
Hmm, no.. lemme try again.
Peter Zijlstra
maintainerQuoted 6 lines · Jing-Ting Wu
Hi all
There is a race condition between CPU hotplug off flow and
__sched_setscheduler(), which will cause hang-up in CPU hotplug off flow.
How easy can you reproduce; does the below hack make it better?
kernel/sched/core.c +14 -4
@@ -4763,20 +4763,30 @@ struct callback_head balance_push_callback = { .func = (void (*)(struct callback_head *))balance_push, }; -static inline struct callback_head *splice_balance_callbacks (struct rq *rq) +static inline struct callback_head * +__splice_balance_callbacks (struct rq *rq,bool foo) {struct callback_head *head = rq->balance_callback;lockdep_assert_rq_held (rq); -if (head) - rq->balance_callback =NULL ; +if (head) { +if (foo && head == &balance_push_callback) + head =NULL ; +else + rq->balance_callback =NULL ; + }return head; } +static inline struct callback_head *splice_balance_callbacks (struct rq *rq) +{ +return __splice_balance_callbacks (rq, true); +} +static void __balance_callbacks (struct rq *rq) { -do_balance_callbacks (rq,splice_balance_callbacks (rq)); +do_balance_callbacks (rq,__splice_balance_callbacks (rq, false)); }static inline void balance_callbacks (struct rq *rq,struct callback_head *head)
Quoted 9 lines · Peter Zijlstra
Quoted 6 lines · Jing-Ting Wu
Hi all
There is a race condition between CPU hotplug off flow and
__sched_setscheduler(), which will cause hang-up in CPU hotplug off flow.How easy can you reproduce; does the below hack make it better?
The issue can be reproduced in about 48 hours when hotplug up/down frequently.
Thanks for your suggestion.
I think the hack patch could stay the rq->balance_callback when rq-
Quoted 1 line
callback =
&balance_push_callback.
We can add hack patch to the stability test.
Quoted 43 lines
kernel/sched/core.c +14
-4@@ -4763,20 +4763,30 @@ struct callback_head balance_push_callback = { .func = (void (*)(struct callback_head *))balance_push, }; -static inline struct callback_head *splice_balance_callbacks (struct rq *rq) +static inline struct callback_head * +__splice_balance_callbacks (struct rq *rq,bool foo) {struct callback_head *head = rq->balance_callback;lockdep_assert_rq_held (rq); -if (head) - rq->balance_callback =NULL ; +if (head) { +if (foo && head == &balance_push_callback) + head =NULL ; +else + rq->balance_callback =NULL ; + }return head; } +static inline struct callback_head *splice_balance_callbacks (struct rq *rq) +{ +return __splice_balance_callbacks (rq, true); +} +static void __balance_callbacks (struct rq *rq) { -do_balance_callbacks (rq,splice_balance_callbacks (rq)); + do_balance_callbacks(rq, __splice_balance_callbacks(rq,false));
}
Quoted 3 lines
static inline void balance_callbacks(struct rq *rq, struct
callback_head*head)
_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek
Hi Peter
Quoted 11 lines
Quoted 1 line
How easy can you reproduce; does the below hack make it better?
The issue can be reproduced in about 48 hours when hotplug up/down frequently.
Thanks for your suggestion.
I think the hack patch could stay therq->balance_callbackwhen rq-Quoted 1 line
callback =
&balance_push_callback.We can add hack patch to the stability test.
Use the patch of previous mail, we pass the stability test at previous 48 hours and still testing.
I think the patch could make the issue better. Do you suggest to upstream this patch to mainline?
Thank you.
Quoted 46 lines
Quoted 46 lines
kernel/sched/core.c +0
-0@@ -4763,20 +4763,30 @@ struct callback_head balance_push_callback=
{kernel/sched/core.c +0
-1.func = (void (*)(struct callback_head *))balance_push, }; -static inline struct callback_head*splice_balance_callbacks(struct
rq *rq)kernel/sched/core.c +9
-2+static inline struct callback_head * +__splice_balance_callbacks (struct rq *rq,bool foo) {struct callback_head *head = rq->balance_callback;lockdep_assert_rq_held (rq); -if (head) - rq->balance_callback =NULL ; +if (head) { +if (foo && head == &balance_push_callback) + head =NULL ; +else + rq->balance_callback =NULL ; + }return head; } +static inline struct callback_head*splice_balance_callbacks(struct
rq *rq)kernel/sched/core.c +5
-1+{ +return __splice_balance_callbacks (rq, true); +} +static void __balance_callbacks (struct rq *rq) { -do_balance_callbacks (rq,splice_balance_callbacks (rq)); + do_balance_callbacks(rq, __splice_balance_callbacks(rq,false));
}
Quoted 4 lines
Quoted 3 lines
static inline void balance_callbacks(struct rq *rq, struct
callback_head*head)
Best Regards,
Jing-Ting Wu
_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek
Hi Peter
Quoted 1 line
Quoted 1 line
Quoted 1 line
How easy can you reproduce; does the below hack make it better?
The patch is helpful to the syndrome, passed stability test over 10 days so far. (as-is: < 48 hours failed)
Could you help to upstream this patch to linux kernel mainline? Thank you.
Quoted 47 lines
Quoted 47 lines
Quoted 47 lines
kernel/sched/core.c +0
-0@@ -4763,20 +4763,30 @@ struct callback_head
balance_push_callback
=
{kernel/sched/core.c +0
-1.func = (void (*)(struct callback_head *))balance_push, }; -static inline struct callback_head*splice_balance_callbacks(struct
rq *rq)kernel/sched/core.c +9
-2+static inline struct callback_head * +__splice_balance_callbacks (struct rq *rq,bool foo) {struct callback_head *head = rq->balance_callback;lockdep_assert_rq_held (rq); -if (head) - rq->balance_callback =NULL ; +if (head) { +if (foo && head == &balance_push_callback) + head =NULL ; +else + rq->balance_callback =NULL ; + }return head; } +static inline struct callback_head*splice_balance_callbacks(struct
rq *rq)kernel/sched/core.c +5
-1+{ +return __splice_balance_callbacks (rq, true); +} +static void __balance_callbacks (struct rq *rq) { -do_balance_callbacks (rq,splice_balance_callbacks (rq)); + do_balance_callbacks(rq, __splice_balance_callbacks(rq,false));
}
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
static inline void balance_callbacks(struct rq *rq, struct
callback_head*head)
Best Regards,
Jing-Ting Wu
Quoted 2 lines
_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek
Peter Zijlstra
maintainerQuoted 7 lines · Jing-Ting Wu
Hi Peter
Quoted 1 line
Quoted 1 line
Quoted 1 line
How easy can you reproduce; does the below hack make it better?
The patch is helpful to the syndrome, passed stability test over 10 days so far. (as-is: < 48 hours failed)
Excellent, let me go write a Changelog for it, or something.
_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek
Peter Zijlstra
maintainerPATCH 1/1 sched: Fix balance_push() vs __sched_setscheduler()
Quoted 6 lines · Peter Zijlstra
Quoted 2 lines · Jing-Ting Wu
The patch is helpful to the syndrome, passed stability test over 10 days so far. (as-is: < 48 hours failed)
Excellent, let me go write a Changelog for it, or something.
How's this then?
---
Subject: sched: Fix balance_push() vs __sched_setscheduler()
From: Peter Zijlstra <[email protected]>
Date: Tue Jun 7 22:41:55 CEST 2022
The purpose of balance_push() is to act as a filter on task selection in the case of CPU hotplug, specifically when taking the CPU out.
It does this by (ab)using the balance callback infrastructure, with the express purpose of keeping all the unlikely/odd cases in a single place.
In order to serve it's purpose, the balance_push_callback needs to be (exclusively) on the callback list at all times (noting that the callback always places itself back on the list the moment it runs, also noting that when the CPU goes down, regular balancing concerns are moot, so ignoring them is fine).
And here-in lies the problem, __sched_setscheduler()'s use of splice_balance_callbacks() takes the callbacks off the list across a lock-break, making it possible for, an interleaving, __schedule() to see an empty list and not get filtered.
--- kernel/sched/core.c | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-)
kernel/sched/core.c +33 -3
@@ -4798,25 +4798,55 @@ static void do_balance_callbacks(structstatic void balance_push (struct rq *rq); +/* +* balance_push_callback is a right abuse of the callback interface and plays +* by significantly different rules. +* +* Where the normal balance_callback's purpose is to be ran in the same context +* that queued it (only later, when it's safe to drop rq->lock again), +* balance_push_callback is specifically targeted at __schedule(). +* +* This abuse is tolerated because it places all the unlikely/odd cases behind +* a single test, namely: rq->balance_callback == NULL. +*/ struct callback_head balance_push_callback = { .next =NULL , .func = (void (*)(struct callback_head *))balance_push, }; -static inline struct callback_head *splice_balance_callbacks (struct rq *rq) +static inline struct callback_head * +__splice_balance_callbacks (struct rq *rq,bool split) {struct callback_head *head = rq->balance_callback; +if (likely (!head)) +return NULL ; +lockdep_assert_rq_held (rq); -if (head) +/* +* Must not take balance_push_callback off the list when +* splace_balance_callbac() and balance_callbacks() are not +* in the same rq->lock section. +* +* In that case it would be possible for __schedule() to interleave +* and observe the list empty. +*/ +if (split && head == &balance_push_callback) + head =NULL ; +else rq->balance_callback =NULL ;return head; } +static inline struct callback_head *splice_balance_callbacks (struct rq *rq) +{ +return __splice_balance_callbacks (rq, true); +} +static void __balance_callbacks (struct rq *rq) { -do_balance_callbacks (rq,splice_balance_callbacks (rq)); +do_balance_callbacks (rq,__splice_balance_callbacks (rq, false)); }static inline void balance_callbacks (struct rq *rq,struct callback_head *head)
_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek
Re: [PATCH] sched: Fix balance_push() vs __sched_setscheduler()
Hi Peter
Quoted 9 lines · Peter Zijlstra
Quoted 6 lines · Peter Zijlstra
Quoted 3 lines · Jing-Ting Wu
The patch is helpful to the syndrome, passed stability test over 10
days so far. (as-is: < 48 hours failed)Excellent, let me go write a Changelog for it, or something.
How's this then?
I think the description is fine.
Thanks for your help.
[...]
Quoted 6 lines
-static inline struct
callback_head*splice_balance_callbacks(struct rq *rq)kernel/sched/core.c +2
-0+static inline struct callback_head * +__splice_balance_callbacks (struct rq *rq,bool split) {
kernel/sched/core.c static void do_balance_callbacks(struct · line 4828
Quoted 10 lines
kernel/sched/core.c +6
-1struct callback_head *head = rq->balance_callback; +if (likely (!head)) +return NULL ; +lockdep_assert_rq_held (rq); -if (head) +/* +* Must not take balance_push_callback off the list when +* splace_balance_callbac() and balance_callbacks() are not
Should we change splace_balance_callbac() to splice_balance_callbacks() at here?
Quoted 5 lines
kernel/sched/core.c +3
-0+* in the same rq->lock section. +* +* In that case it would be possible for __schedule() to interleave
kernel/sched/core.c static void do_balance_callbacks(struct · line 4840
Quoted 9 lines
kernel/sched/core.c +5
-0+* and observe the list empty. +*/ +if (split && head == &balance_push_callback) + head =NULL ; +else rq->balance_callback =NULL ;return head; }
[...]
Best Regards,
Jing-Ting Wu
Peter Zijlstra
maintainerRe: [PATCH] sched: Fix balance_push() vs __sched_setscheduler()
Quoted 7 lines · Jing-Ting Wu
kernel/sched/core.c static void do_balance_callbacks(struct · line 4828
Quoted 3 lines
kernel/sched/core.c +3
-0+/* +* Must not take balance_push_callback off the list when +* splace_balance_callbac() and balance_callbacks() are not Should we change
splace_balance_callbac()tosplice_balance_callbacks()at here?
Pff, typing is so hard.. :-)
I'll also go find me a Fixes tag I suppose.
_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek
[tip: sched/urgent] sched: Fix balance_push() vs __sched_setscheduler()
In reply to Peter ZijlstraThe following commit has been merged into the sched/urgent branch of tip:
The following commit has been merged into the sched/urgent branch of tip:
Commit-ID: 04193d590b390ec7a0592630f46d559ec6564ba1 Gitweb: https://git.kernel.org/tip/04193d590b390ec7a0592630f46d559ec6564ba1 Author: Peter Zijlstra <[email protected]> AuthorDate: Tue, 07 Jun 2022 22:41:55 +02:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Mon, 13 Jun 2022 10:15:07 +02:00
sched: Fix balance_push() vs __sched_setscheduler()
The purpose of balance_push() is to act as a filter on task selection in the case of CPU hotplug, specifically when taking the CPU out.
It does this by (ab)using the balance callback infrastructure, with the express purpose of keeping all the unlikely/odd cases in a single place.
In order to serve its purpose, the balance_push_callback needs to be (exclusively) on the callback list at all times (noting that the callback always places itself back on the list the moment it runs, also noting that when the CPU goes down, regular balancing concerns are moot, so ignoring them is fine).
And here-in lies the problem, __sched_setscheduler()'s use of splice_balance_callbacks() takes the callbacks off the list across a lock-break, making it possible for, an interleaving, __schedule() to see an empty list and not get filtered.
--- kernel/sched/core.c | 36 +++++++++++++++++++++++++++++++++--- kernel/sched/sched.h | 5 +++++ 2 files changed, 38 insertions(+), 3 deletions(-)
kernel/sched/core.c +33 -3
@@ -4798,25 +4798,55 @@ static void do_balance_callbacks(struct rq *rq, struct callback_head *head)static void balance_push (struct rq *rq); +/* +* balance_push_callback is a right abuse of the callback interface and plays +* by significantly different rules. +* +* Where the normal balance_callback's purpose is to be ran in the same context +* that queued it (only later, when it's safe to drop rq->lock again), +* balance_push_callback is specifically targeted at __schedule(). +* +* This abuse is tolerated because it places all the unlikely/odd cases behind +* a single test, namely: rq->balance_callback == NULL. +*/ struct callback_head balance_push_callback = { .next =NULL , .func = (void (*)(struct callback_head *))balance_push, }; -static inline struct callback_head *splice_balance_callbacks (struct rq *rq) +static inline struct callback_head * +__splice_balance_callbacks (struct rq *rq,bool split) {struct callback_head *head = rq->balance_callback; +if (likely (!head)) +return NULL ; +lockdep_assert_rq_held (rq); -if (head) +/* +* Must not take balance_push_callback off the list when +* splice_balance_callbacks() and balance_callbacks() are not +* in the same rq->lock section. +* +* In that case it would be possible for __schedule() to interleave +* and observe the list empty. +*/ +if (split && head == &balance_push_callback) + head =NULL ; +else rq->balance_callback =NULL ;return head; } +static inline struct callback_head *splice_balance_callbacks (struct rq *rq) +{ +return __splice_balance_callbacks (rq, true); +} +static void __balance_callbacks (struct rq *rq) { -do_balance_callbacks (rq,splice_balance_callbacks (rq)); +do_balance_callbacks (rq,__splice_balance_callbacks (rq, false)); }static inline void balance_callbacks (struct rq *rq,struct callback_head *head)
kernel/sched/sched.h +5 -0
@@ -1693,6 +1693,11 @@ queue_balance_callback(struct rq *rq, {lockdep_assert_rq_held (rq); +/* +* Don't (re)queue an already queued item; nor queue anything when +* balance_push() is active, see the comment with +* balance_push_callback. +*/ if (unlikely (head->next || rq->balance_callback == &balance_push_callback))return ;