powercap/drivers/idle_injection: Add an idle injection framework
PATCH v8 1/1 powercap/drivers/idle_injection: Add an idle injection framework
Initially, the cpu_cooling device for ARM was changed by adding a new policy inserting idle cycles. The intel_powerclamp driver does a similar action.
Instead of implementing idle injections privately in the cpu_cooling device, move the idle injection code in a dedicated framework and give the opportunity to other frameworks to make use of it.
The framework relies on the smpboot kthreads which handles via its main loop the common code for hotplugging and [un]parking.
This code was previously tested with the cpu cooling device and went through several iterations. It results now in split code and API exported in the header file. It was tested with the cpu cooling device with success.
---
V8:
- Rollback only what was modified
- Add the park() callback to reset the should_run flag
V7:
- Replace count approach by htimer_forward and restart (Peter Zijlstra)
- Wait for task inactive when stopping the idle injections
- Changed the comments and description
V6:
- Move count to wake up function (Viresh Kumar)
- Split atomic/non-atomic context to wake up tasks
- Add the park callback to handle unplug/inject race
- Replace atomic by READ_ONCE and WRITE_ONCE (Peter Zijlstra)
V5:
- Move init_completion in the init function (Viresh Kumar)
- Increment task count in the wakeup function (Viresh Kumar)
- Remove rollback at init time (Viresh Kumar)
V4:
- Wait for completion when stopping (Viresh Kumar)
- Check init already done and rollback (Viresh Kumar)
V3:
- Fixed typos (Viresh Kumar)
- Removed extra blank line (Viresh Kumar)
- Added full stop (Viresh Kumar)
- Fixed Return kerneldoc format (Viresh Kumar)
- Fixed multiple kthreads initialization (Viresh Kumar)
- Fixed rollbacking the actions in the unregister function (Viresh Kumar)
- Make idle injection kthreads name hardcoded
- Kthreads are created in the initcall process
V2: Fixed checkpatch warnings
--- drivers/powercap/Kconfig | 10 + drivers/powercap/Makefile | 1 + drivers/powercap/idle_injection.c | 375 ++++++++++++++++++++++++++++++++++++++ include/linux/idle_injection.h | 29 +++ 4 files changed, 415 insertions(+) create mode 100644 drivers/powercap/idle_injection.c create mode 100644 include/linux/idle_injection.h
drivers/powercap/Kconfig +10 -0
@@ -29,4 +29,14 @@ config INTEL_RAPL controller, CPU core (Power Plance 0), graphics uncore (Power Plane 1), etc. +config IDLE_INJECTION + bool "Idle injection framework" + depends on CPU_IDLE + default n + help + This enables support for the idle injection framework. It + provides a way to force idle periods on a set of specified + CPUs for power capping. Idle period can be injected + synchronously on a set of specified CPUs or alternatively + on a per CPU basis. endif
drivers/powercap/Makefile +1 -0
@@ -1,2 +1,3 @@ obj-$(CONFIG_POWERCAP) += powercap_sys.o obj-$(CONFIG_INTEL_RAPL) += intel_rapl.o +obj-$(CONFIG_IDLE_INJECTION) += idle_injection.o
drivers/powercap/idle_injection.c new file +375 -0
375 lines not on this page · raw message
include/linux/idle_injection.h new file +29 -0
@@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2018 Linaro Ltd +* +* Author: Daniel Lezcano <[email protected]> +* +*/ +#ifndef __IDLE_INJECTION_H__ +#define __IDLE_INJECTION_H__ + +/* private idle injection device structure */ +struct idle_injection_device ; + +struct idle_injection_device *idle_injection_register (struct cpumask *cpumask); + +void idle_injection_unregister (struct idle_injection_device *ii_dev); + +int idle_injection_start (struct idle_injection_device *ii_dev); + +void idle_injection_stop (struct idle_injection_device *ii_dev); + +void idle_injection_set_duration (struct idle_injection_device *ii_dev, +unsigned int run_duration_ms, +unsigned int idle_duration_ms); + +void idle_injection_get_duration (struct idle_injection_device *ii_dev, +unsigned int *run_duration_ms, +unsigned int *idle_duration_ms); +#endif /* __IDLE_INJECTION_H__ */
Signature · 1 line
2.7.4
Quoted 24 lines · Daniel Lezcano
+++
b/drivers/powercap/idle_injection.cdrivers/powercap/idle_injection.c new file +22
-0@@ -0,0 +1,375 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2018 Linaro Limited +* +* Author: Daniel Lezcano <[email protected]> +* +* The idle injection framework proposes a way to force a cpu to enter +* an idle state during a specified amount of time for a specified +* period. +* +* It relies on the smpboot kthreads which handles, via its main loop, +* the common code for hotplugging and [un]parking. +* +* At init time, all the kthreads are created. +* +* A cpumask is specified as parameter for the idle injection +* registering function. The kthreads will be synchronized regarding +* this cpumask. +* +* The idle + run duration is specified via the helpers and then the +* idle injection can be started at this point. +*
drivers/powercap/idle_injection.c line 32
Quoted 10 lines
drivers/powercap/idle_injection.c new file +10
-0+* A kthread will call play_idle() with the specified idle duration +* from above. +* +* A timer is set after waking up all the tasks, to the next idle +* injection cycle. +* +* The task handling the timer interrupt will wakeup all the kthreads +* belonging to the cpumask. +* +* Stopping the idle injection is synchonuous, when the function
synchronous
Quoted 9 lines
drivers/powercap/idle_injection.c new file +8
-0+* returns, there is the guarantee there is no more idle injection +* kthread in activity. +* +* It is up to the user of this framework to provide a lock at an +* upper level to prevent stupid things to happen, like starting while +* we are unregistering. +*/ +static void idle_injection_wakeup (struct idle_injection_device *ii_dev)
drivers/powercap/idle_injection.c line 96
Quoted 10 lines
drivers/powercap/idle_injection.c new file +10
-0+{ +struct idle_injection_thread *iit; +unsigned int cpu; + +for_each_cpu_and (cpu,to_cpumask (ii_dev->cpumask), cpu_online_mask) { + iit =per_cpu_ptr (&idle_injection_thread, cpu); + iit->should_run =1 ; +wake_up_process (iit->tsk); + } +}
Thread A Thread B
CPU3 hotplug out
-> idle_injection_park()
iit(of-CPU3)->should_run = 0;idle_injection_wakeup()
for_each_cpu_and(online)..
CPU3-selected
clear CPU3 from cpu-online mask.
iit(of-CPU3)->should_run = 1; wake_up_process()
With the above sequence of events, is it possible that the iit->should_run variable is set to 1 while the CPU is offlined ? And so the crash we discussed in the previous version may still exist ? Sorry I am not able to take my mind away from thinking about these stupid races :(
Quoted 42 lines
drivers/powercap/idle_injection.c new file +42
-0+ +/** +* idle_injection_wakeup_fn - idle injection timer callback +* @timer: a hrtimer structure +* +* This function is called when the idle injection timer expires which +* will wake up the idle injection tasks and these ones, in turn, play +* idle a specified amount of time. +* +* Return: HRTIMER_RESTART. +*/ +static enum hrtimer_restart idle_injection_wakeup_fn (struct hrtimer *timer) +{ +unsigned int run_duration_ms; +unsigned int idle_duration_ms; +struct idle_injection_device *ii_dev = + container_of(timer, struct idle_injection_device, timer); + + run_duration_ms =READ_ONCE (ii_dev->run_duration_ms); + idle_duration_ms =READ_ONCE (ii_dev->idle_duration_ms); + +idle_injection_wakeup (ii_dev); + +hrtimer_forward_now (timer, +ms_to_ktime (idle_duration_ms + run_duration_ms)); + +return HRTIMER_RESTART ; +} + +/** +* idle_injection_fn - idle injection routine +* @cpu: the CPU number the task belongs to +* +* The idle injection routine will stay idle the specified amount of +* time +*/ +static void idle_injection_fn (unsigned int cpu) +{ +struct idle_injection_device *ii_dev; +struct idle_injection_thread *iit; + + ii_dev =per_cpu (idle_injection_device, cpu);
drivers/powercap/idle_injection.c line 148
Quoted 10 lines
drivers/powercap/idle_injection.c new file +10
-0+ iit =per_cpu_ptr (&idle_injection_thread, cpu); + +/* +* Boolean used by the smpboot main loop and used as a +* flip-flop in this function +*/ + iit->should_run =0 ; + +play_idle (READ_ONCE (ii_dev->idle_duration_ms)); +}
Maybe we shouldn't change things now (too much effort already went into it already), I just wanted to share an idea that popped up in my mind. Maybe we could have used the msleep() or similar API with run_duration_ms from the kthread instead of the whole hrtimer stuff. Maybe that would have been simpler to manage? Maybe not :)
The patch looks fine otherwise. I don't have any other (negative) feedback :)
Signature · 1 line
viresh
Quoted 70 lines · Viresh Kumar
Quoted 24 lines · Daniel Lezcano
+++
b/drivers/powercap/idle_injection.cdrivers/powercap/idle_injection.c new file +22
-0@@ -0,0 +1,375 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2018 Linaro Limited +* +* Author: Daniel Lezcano <[email protected]> +* +* The idle injection framework proposes a way to force a cpu to enter +* an idle state during a specified amount of time for a specified +* period. +* +* It relies on the smpboot kthreads which handles, via its main loop, +* the common code for hotplugging and [un]parking. +* +* At init time, all the kthreads are created. +* +* A cpumask is specified as parameter for the idle injection +* registering function. The kthreads will be synchronized regarding +* this cpumask. +* +* The idle + run duration is specified via the helpers and then the +* idle injection can be started at this point. +* drivers/powercap/idle_injection.c line 32
Quoted 10 lines
drivers/powercap/idle_injection.c new file +10
-0+* A kthread will call play_idle() with the specified idle duration +* from above. +* +* A timer is set after waking up all the tasks, to the next idle +* injection cycle. +* +* The task handling the timer interrupt will wakeup all the kthreads +* belonging to the cpumask. +* +* Stopping the idle injection is synchonuous, when the function synchronous
Quoted 9 lines
drivers/powercap/idle_injection.c new file +8
-0+* returns, there is the guarantee there is no more idle injection +* kthread in activity. +* +* It is up to the user of this framework to provide a lock at an +* upper level to prevent stupid things to happen, like starting while +* we are unregistering. +*/ +static void idle_injection_wakeup (struct idle_injection_device *ii_dev)drivers/powercap/idle_injection.c line 96
Quoted 10 lines
drivers/powercap/idle_injection.c new file +10
-0+{ +struct idle_injection_thread *iit; +unsigned int cpu; + +for_each_cpu_and (cpu,to_cpumask (ii_dev->cpumask), cpu_online_mask) { + iit =per_cpu_ptr (&idle_injection_thread, cpu); + iit->should_run =1 ; +wake_up_process (iit->tsk); + } +}Thread A Thread B
CPU3 hotplug out -> idle_injection_park() iit(of-CPU3)->should_run = 0;
idle_injection_wakeup()
for_each_cpu_and(online)..
CPU3-selected
clear CPU3 from cpu-online mask.
Quoted 7 lines
iit(of-CPU3)->should_run = 1; wake_up_process()With the above sequence of events, is it possible that the
iit->should_runvariable is set to 1 while the CPU is offlined ? And so the crash we discussed in the previous version may still exist ? Sorry I am not able to take my mind away from thinking about these stupid races :(
If I refer to previous Peter's comment about a similar race, I think it is possible.
I guess setting the should_run flag to zero in the unpark() must fix the issue also.
Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs Follow Linaro: <http://www.facebook.com/pages/Linaro> Facebook | <http://twitter.com/#!/linaroorg> Twitter | <http://www.linaro.org/linaro-blog/> Blog
Quoted 79 lines · Daniel Lezcano
Quoted 70 lines · Viresh Kumar
Quoted 24 lines · Daniel Lezcano
+++
b/drivers/powercap/idle_injection.cdrivers/powercap/idle_injection.c new file +22
-0@@ -0,0 +1,375 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright 2018 Linaro Limited +* +* Author: Daniel Lezcano <[email protected]> +* +* The idle injection framework proposes a way to force a cpu to enter +* an idle state during a specified amount of time for a specified +* period. +* +* It relies on the smpboot kthreads which handles, via its main loop, +* the common code for hotplugging and [un]parking. +* +* At init time, all the kthreads are created. +* +* A cpumask is specified as parameter for the idle injection +* registering function. The kthreads will be synchronized regarding +* this cpumask. +* +* The idle + run duration is specified via the helpers and then the +* idle injection can be started at this point. +* drivers/powercap/idle_injection.c line 32
Quoted 10 lines
drivers/powercap/idle_injection.c new file +10
-0+* A kthread will call play_idle() with the specified idle duration +* from above. +* +* A timer is set after waking up all the tasks, to the next idle +* injection cycle. +* +* The task handling the timer interrupt will wakeup all the kthreads +* belonging to the cpumask. +* +* Stopping the idle injection is synchonuous, when the function synchronous
Quoted 9 lines
drivers/powercap/idle_injection.c new file +8
-0+* returns, there is the guarantee there is no more idle injection +* kthread in activity. +* +* It is up to the user of this framework to provide a lock at an +* upper level to prevent stupid things to happen, like starting while +* we are unregistering. +*/ +static void idle_injection_wakeup (struct idle_injection_device *ii_dev)drivers/powercap/idle_injection.c line 96
Quoted 10 lines
drivers/powercap/idle_injection.c new file +10
-0+{ +struct idle_injection_thread *iit; +unsigned int cpu; + +for_each_cpu_and (cpu,to_cpumask (ii_dev->cpumask), cpu_online_mask) { + iit =per_cpu_ptr (&idle_injection_thread, cpu); + iit->should_run =1 ; +wake_up_process (iit->tsk); + } +}Thread A Thread B
CPU3 hotplug out -> idle_injection_park() iit(of-CPU3)->should_run = 0;
idle_injection_wakeup()
for_each_cpu_and(online)..
CPU3-selected
clear CPU3 from cpu-online mask.Quoted 7 lines
iit(of-CPU3)->should_run = 1; wake_up_process()With the above sequence of events, is it possible that the
iit->should_runvariable is set to 1 while the CPU is offlined ? And so the crash we discussed in the previous version may still exist ? Sorry I am not able to take my mind away from thinking about these stupid races :(
Quoted 5 lines
If I refer to previous Peter's comment about a similar race, I think it is possible.
I guess setting the
should_runflag to zero in theunpark()must fix the issue also.
Right. But since you are already taking the hotplug lock in stop-idle-injection, you can iterate over all CPUs of a mask instead of the online ones. That would be one callback less to run at every unpark (though there wouldn't be so many of them I believe).
Signature · 1 line
viresh