Skip to contentSkip to conversation

sched: access local runqueue directly in single_task_running

PATCH In review Dominik Dingel 3 participants 4 messages Sep 18, 2015

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

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
index 78b4bad10..5bfad0b 100644
@@ -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
maintainer
#1 lore raw
Quoted 26 lines · Dominik Dingel

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 +0 -0
index 78b4bad10..5bfad0b 100644

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);
 
#2 lore raw
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 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 +0 -0
index 78b4bad10..5bfad0b 100644

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)
*/

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);
 
maintainer Acked-by
#3 lore raw
In reply to Paolo Bonzini
Quoted 23 lines · Paolo Bonzini

kernel/sched/core.c unsigned long nr_running(void) · line 2619

Quoted 8 lines

+++ b/kernel/sched/core.c

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.

In which case: