sched: access local runqueue directly in single_task_running
Subsystems (3)
PATCH 1/1 sched: access local runqueue directly in single_task_running
Commit 2ee507c47293 ("sched: Add function single_task_running to let a task check if it is the only task running on a cpu") referenced the current runqueue with the smp_processor_id. When CONFIG_DEBUG_PREEMPT is enabled, that is only allowed if preemption is disabled or the currrent task is bound to the local cpu (e.g. kernel worker).
With commit f78195129963 ("kvm: add halt_poll_ns module parameter") KVM calls single_task_running. If CONFIG_DEBUG_PREEMPT is enabled that generates a lot of kernel messages.
To avoid adding preemption in that cases, as it would limit the usefulness, we change single_task_running to access directly the cpu local runqueue.
--- kernel/sched/core.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/sched/core.c +4 -4
@@ -2614,13 +2614,13 @@ unsigned long nr_running(void)/* * Check if only the current task is running on the cpu. +* +* Caution result is subject to time-of-check-to-time-of-use race, +* every caller is responsible to set up additional fences if necessary. */ bool single_task_running (void ) { -if (cpu_rq (smp_processor_id ())->nr_running ==1 ) -return true; -else -return false; +return raw_rq ()->nr_running ==1 ; }EXPORT_SYMBOL (single_task_running);
Signature · 1 line
2.3.8
Paolo Bonzini
maintainerQuoted 26 lines · Dominik Dingel
Commit
2ee507c47293("sched: Add functionsingle_task_runningto let a task check if it is the only task running on a cpu") referenced the current runqueue with thesmp_processor_id. WhenCONFIG_DEBUG_PREEMPTis enabled, that is only allowed if preemption is disabled or the currrent task is bound to the local cpu (e.g. kernel worker).With commit
f78195129963("kvm: addhalt_poll_nsmodule parameter") KVM callssingle_task_running. IfCONFIG_DEBUG_PREEMPTis enabled that generates a lot of kernel messages.To avoid adding preemption in that cases, as it would limit the usefulness, we change
single_task_runningto access directly the cpu local runqueue.Cc Tim Chen <[email protected]>Suggested-by Peter Zijlstra <[email protected]>Cc <[email protected]> # 4.2.xSigned-off-by Dominik Dingel <[email protected]>--- kernel/sched/core.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)kernel/sched/core.c +0
-0
kernel/sched/core.c unsigned long nr_running(void) · line 2619
Quoted 6 lines
kernel/sched/core.c +3
-0@@ -2614,13 +2614,13 @@ unsigned long nr_running(void)/* * Check if only the current task is running on the cpu. +* +* Caution result is subject to time-of-check-to-time-of-use race, +* every caller is responsible to set up additional fences if necessary.
Let's expand it a bit more:
* Caution: this function does not check that the caller has disabled
* preemption, thus the result might have a time-of-check-to-time-of-use
* race. The caller is responsible to use this correctly, for example:
*
* - use it from a non-preemptable section
*
* - use it from a thread that is bound to a single CPU
*
* - use it in a loop where each iteration takes very little time
* (e.g. a polling loop)
*/
I'll include it in my pull request.
Paolo
Quoted 12 lines
kernel/sched/core.c +1
-4*/ bool single_task_running (void ) { -if (cpu_rq (smp_processor_id ())->nr_running ==1 ) -return true; -else -return false; +return raw_rq ()->nr_running ==1 ; }EXPORT_SYMBOL (single_task_running);
Quoted 50 lines · On Fri, 18 Sep 2015 13:26:53 +0200 Paolo Bonzini <[email protected]> wrote:
Quoted 26 lines · Dominik Dingel
Commit
2ee507c47293("sched: Add functionsingle_task_runningto let a task check if it is the only task running on a cpu") referenced the current runqueue with thesmp_processor_id. WhenCONFIG_DEBUG_PREEMPTis enabled, that is only allowed if preemption is disabled or the currrent task is bound to the local cpu (e.g. kernel worker).With commit
f78195129963("kvm: addhalt_poll_nsmodule parameter") KVM callssingle_task_running. IfCONFIG_DEBUG_PREEMPTis enabled that generates a lot of kernel messages.To avoid adding preemption in that cases, as it would limit the usefulness, we change
single_task_runningto access directly the cpu local runqueue.Cc Tim Chen <[email protected]>Suggested-by Peter Zijlstra <[email protected]>Cc <[email protected]> # 4.2.xSigned-off-by Dominik Dingel <[email protected]>--- kernel/sched/core.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)kernel/sched/core.c +0
-0kernel/sched/core.c unsigned long nr_running(void) · line 2619
Quoted 6 lines
kernel/sched/core.c +3
-0@@ -2614,13 +2614,13 @@ unsigned long nr_running(void)/* * Check if only the current task is running on the cpu. +* +* Caution result is subject to time-of-check-to-time-of-use race, +* every caller is responsible to set up additional fences if necessary. Let's expand it a bit more:
* Caution: this function does not check that the caller has disabled
* preemption, thus the result might have a time-of-check-to-time-of-use
* race. The caller is responsible to use this correctly, for example:
*
* - use it from a non-preemptable section
*
* - use it from a thread that is bound to a single CPU
*
* - use it in a loop where each iteration takes very little time
* (e.g. a polling loop)
*/
Quoted 1 line
I'll include it in my pull request.
Sounds really good.
Thank you!
Quoted 15 lines
Paolo
Quoted 12 lines
kernel/sched/core.c +1
-4*/ bool single_task_running (void ) { -if (cpu_rq (smp_processor_id ())->nr_running ==1 ) -return true; -else -return false; +return raw_rq ()->nr_running ==1 ; }EXPORT_SYMBOL (single_task_running);
Quoted 23 lines · Paolo Bonzini
kernel/sched/core.c unsigned long nr_running(void) · line 2619
Quoted 8 lines
+++
b/kernel/sched/core.ckernel/sched/core.c +3
-0@@ -2614,13 +2614,13 @@ unsigned long nr_running(void)/* * Check if only the current task is running on the cpu. +* +* Caution result is subject to time-of-check-to-time-of-use race, +* every caller is responsible to set up additional fences if necessary. Let's expand it a bit more:
* Caution: this function does not check that the caller has disabled
* preemption, thus the result might have a time-of-check-to-time-of-use
* race. The caller is responsible to use this correctly, for example:
*
* - use it from a non-preemptable section
*
* - use it from a thread that is bound to a single CPU
*
* - use it in a loop where each iteration takes very little time
* (e.g. a polling loop)
*/
Quoted 1 line
I'll include it in my pull request.
In which case: