powercap/drivers/idle_injection: Add an idle injection framework
PATCH v7 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.
---
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 | 355 ++++++++++++++++++++++++++++++++++++++ include/linux/idle_injection.h | 29 ++++ 4 files changed, 395 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 +355 -0
355 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 22 lines · Daniel Lezcano
drivers/powercap/idle_injection.c new file +22
-0+/** +* idle_injection_stop - stops the idle injections +* @ii_dev: a pointer to an idle injection_device structure +* +* The function stops the idle injection and waits for the threads to +* complete. If we are in the process of injecting an idle cycle, then +* this will wait the end of the cycle. +* +* When the function returns there is no more idle injection +* activity. The kthreads are scheduled out and the periodic timer is +* off. +*/ +void idle_injection_stop (struct idle_injection_device *ii_dev) +{ +struct idle_injection_thread *iit; +unsigned int cpu; + +pr_debug ("Stopping injecting idle cycles on CPUs '%*pbl'\n" , +cpumask_pr_args (to_cpumask (ii_dev->cpumask))); + +hrtimer_cancel (&ii_dev->timer); +
drivers/powercap/idle_injection.c line 239
Quoted 10 lines
drivers/powercap/idle_injection.c new file +10
-0+/* +* We want the guarantee we have a quescient point where +* parked threads stay in there state while we are stopping +* the idle injection. After exiting the loop, if any CPU is +* plugged in, the 'should_run' boolean being false, the +* smpboot main loop schedules the task out. +*/ +cpu_hotplug_disable (); + +for_each_cpu_and (cpu,to_cpumask (ii_dev->cpumask), cpu_online_mask) {
Maybe you should do below for all CPUs in the mask. Is the below usecase possible ?
- CPU0-4 are part of the mask and are all online.
- hrtimer fires and sets should_run for all of them to 1.
- Right at this time CPU3 goes offline, so the thread gets parked with should_run == 1. Is there a reason why this can't happen ?
- Now we unregister the stuff and CPU3 again comes online.
- Because it had should_run as true, we again run the thread and Crash.
makes sense ?
drivers/powercap/idle_injection.c line 243
Quoted 4 lines
drivers/powercap/idle_injection.c new file +4
-0+ iit =per_cpu_ptr (&idle_injection_thread, cpu); + iit->should_run =0 ; + +wait_task_inactive (iit->tsk,0 );
I am not very sure of what guarantees this will provide.
@Peter: Do you see any more race scenarios here ?
Quoted 23 lines
drivers/powercap/idle_injection.c new file +22
-0+ } + +cpu_hotplug_enable (); +} +struct idle_injection_device *idle_injection_register (struct cpumask *cpumask) +{ +struct idle_injection_device *ii_dev; +int cpu; + + ii_dev =kzalloc (sizeof (*ii_dev) +cpumask_size (),GFP_KERNEL ); +if (!ii_dev) +return NULL ; + +cpumask_copy (to_cpumask (ii_dev->cpumask), cpumask); +hrtimer_init (&ii_dev->timer,CLOCK_MONOTONIC ,HRTIMER_MODE_REL ); + ii_dev->timer.function = idle_injection_wakeup_fn; + +for_each_cpu (cpu,to_cpumask (ii_dev->cpumask)) { + +if (per_cpu (idle_injection_device, cpu)) { +pr_err ("cpu%d is already registered\n" , cpu); + goto out_rollback_per_cpu;
drivers/powercap/idle_injection.c line 316
Quoted 10 lines
drivers/powercap/idle_injection.c new file +10
-0+ } + +per_cpu (idle_injection_device, cpu) = ii_dev; + } + +return ii_dev; + +out_rollback_per_cpu: +for_each_cpu (cpu,to_cpumask (ii_dev->cpumask)) +per_cpu (idle_injection_device, cpu) =NULL ;
So if two parts of the kernel call this routine with the same cpumask, then the second call will also overwrite the masks with NULL and return error. That will screw up things a bit here.
Signature · 1 line
viresh
Quoted 37 lines · Viresh Kumar
Quoted 22 lines · Daniel Lezcano
drivers/powercap/idle_injection.c new file +22
-0+/** +* idle_injection_stop - stops the idle injections +* @ii_dev: a pointer to an idle injection_device structure +* +* The function stops the idle injection and waits for the threads to +* complete. If we are in the process of injecting an idle cycle, then +* this will wait the end of the cycle. +* +* When the function returns there is no more idle injection +* activity. The kthreads are scheduled out and the periodic timer is +* off. +*/ +void idle_injection_stop (struct idle_injection_device *ii_dev) +{ +struct idle_injection_thread *iit; +unsigned int cpu; + +pr_debug ("Stopping injecting idle cycles on CPUs '%*pbl'\n" , +cpumask_pr_args (to_cpumask (ii_dev->cpumask))); + +hrtimer_cancel (&ii_dev->timer); +drivers/powercap/idle_injection.c line 239
Quoted 10 lines
drivers/powercap/idle_injection.c new file +10
-0+/* +* We want the guarantee we have a quescient point where +* parked threads stay in there state while we are stopping +* the idle injection. After exiting the loop, if any CPU is +* plugged in, the 'should_run' boolean being false, the +* smpboot main loop schedules the task out. +*/ +cpu_hotplug_disable (); + +for_each_cpu_and (cpu,to_cpumask (ii_dev->cpumask), cpu_online_mask) {Maybe you should do below for all CPUs in the mask. Is the below usecase possible ?
Quoted 2 lines
- CPU0-4 are part of the mask and are all online.
- hrtimer fires and setsshould_runfor all of them to 1.
^^
hrtimer_cancel gives you the guarantee, the timer is no longer active and there is no execution in the timer handler. So the timer can no longer fire after hrtimer_cancel() is called (which is a blocking call).
Quoted 8 lines
- Right at this time CPU3 goes offline, so the thread gets parked with
should_run== 1. Is there a reason why this can't happen ?
- Now we unregister the stuff and CPU3 again comes online.
- Because it hadshould_runas true, we again run the thread and Crash.makes sense ?
Quoted 6 lines
drivers/powercap/idle_injection.c line 243
Quoted 4 lines
drivers/powercap/idle_injection.c new file +4
-0+ iit =per_cpu_ptr (&idle_injection_thread, cpu); + iit->should_run =0 ; + +wait_task_inactive (iit->tsk,0 );I am not very sure of what guarantees this will provide.
We get the guarantee any idle injection cycle is ended.
Quoted 31 lines
@Peter: Do you see any more race scenarios here ?
Quoted 29 lines
drivers/powercap/idle_injection.c new file +28
-0+ } + +cpu_hotplug_enable (); +} +struct idle_injection_device *idle_injection_register (struct cpumask *cpumask) +{ +struct idle_injection_device *ii_dev; +int cpu; + + ii_dev =kzalloc (sizeof (*ii_dev) +cpumask_size (),GFP_KERNEL ); +if (!ii_dev) +return NULL ; + +cpumask_copy (to_cpumask (ii_dev->cpumask), cpumask); +hrtimer_init (&ii_dev->timer,CLOCK_MONOTONIC ,HRTIMER_MODE_REL ); + ii_dev->timer.function = idle_injection_wakeup_fn; + +for_each_cpu (cpu,to_cpumask (ii_dev->cpumask)) { + +if (per_cpu (idle_injection_device, cpu)) { +pr_err ("cpu%d is already registered\n" , cpu); + goto out_rollback_per_cpu; + } + +per_cpu (idle_injection_device, cpu) = ii_dev; + } + +return ii_dev;
Quoted 8 lines
drivers/powercap/idle_injection.c line 316
Quoted 4 lines
drivers/powercap/idle_injection.c new file +4
-0+ +out_rollback_per_cpu: +for_each_cpu (cpu,to_cpumask (ii_dev->cpumask)) +per_cpu (idle_injection_device, cpu) =NULL ;So if two parts of the kernel call this routine with the same cpumask, then the second call will also overwrite the masks with NULL and return error. That will screw up things a bit here.
Apparently there is a misunderstanding :)
https://lkml.org/lkml/2018/5/29/209 (at the end)
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 38 lines · Daniel Lezcano
Quoted 37 lines · Viresh Kumar
Quoted 22 lines · Daniel Lezcano
drivers/powercap/idle_injection.c new file +22
-0+/** +* idle_injection_stop - stops the idle injections +* @ii_dev: a pointer to an idle injection_device structure +* +* The function stops the idle injection and waits for the threads to +* complete. If we are in the process of injecting an idle cycle, then +* this will wait the end of the cycle. +* +* When the function returns there is no more idle injection +* activity. The kthreads are scheduled out and the periodic timer is +* off. +*/ +void idle_injection_stop (struct idle_injection_device *ii_dev) +{ +struct idle_injection_thread *iit; +unsigned int cpu; + +pr_debug ("Stopping injecting idle cycles on CPUs '%*pbl'\n" , +cpumask_pr_args (to_cpumask (ii_dev->cpumask))); + +hrtimer_cancel (&ii_dev->timer); +drivers/powercap/idle_injection.c line 239
Quoted 10 lines
drivers/powercap/idle_injection.c new file +10
-0+/* +* We want the guarantee we have a quescient point where +* parked threads stay in there state while we are stopping +* the idle injection. After exiting the loop, if any CPU is +* plugged in, the 'should_run' boolean being false, the +* smpboot main loop schedules the task out. +*/ +cpu_hotplug_disable (); + +for_each_cpu_and (cpu,to_cpumask (ii_dev->cpumask), cpu_online_mask) {Maybe you should do below for all CPUs in the mask. Is the below usecase possible ?
Quoted 7 lines
Quoted 2 lines
- CPU0-4 are part of the mask and are all online.
- hrtimer fires and setsshould_runfor all of them to 1.^^
hrtimer_cancelgives you the guarantee, the timer is no longer active and there is no execution in the timer handler. So the timer can no longer fire afterhrtimer_cancel()is called (which is a blocking call).
Right but that isn't called yet in my sequence.
Quoted 3 lines
Quoted 3 lines
- Right at this time CPU3 goes offline, so the thread gets parked with
should_run== 1. Is there a reason why this can't happen ?
- Now we unregister the stuff and CPU3 again comes online.
It gets called here from unregister/stop.
Quoted 12 lines
Quoted 11 lines
- Because it had
should_runas true, we again run the thread and Crash.makes sense ?
drivers/powercap/idle_injection.c line 316
Quoted 3 lines
drivers/powercap/idle_injection.c new file +3
-0+out_rollback_per_cpu: +for_each_cpu (cpu,to_cpumask (ii_dev->cpumask)) +per_cpu (idle_injection_device, cpu) =NULL ;So if two parts of the kernel call this routine with the same cpumask, then the second call will also overwrite the masks with NULL and return error. That will screw up things a bit here.
Quoted 3 lines
Apparently there is a misunderstanding :)
https://lkml.org/lkml/2018/5/29/209 (at the end)
Right, your earlier version was doing the right thing :)
Signature · 1 line
viresh
Quoted 42 lines · Viresh Kumar
Quoted 41 lines · Daniel Lezcano
Quoted 39 lines · Viresh Kumar
Quoted 22 lines · Daniel Lezcano
drivers/powercap/idle_injection.c new file +22
-0+/** +* idle_injection_stop - stops the idle injections +* @ii_dev: a pointer to an idle injection_device structure +* +* The function stops the idle injection and waits for the threads to +* complete. If we are in the process of injecting an idle cycle, then +* this will wait the end of the cycle. +* +* When the function returns there is no more idle injection +* activity. The kthreads are scheduled out and the periodic timer is +* off. +*/ +void idle_injection_stop (struct idle_injection_device *ii_dev) +{ +struct idle_injection_thread *iit; +unsigned int cpu; + +pr_debug ("Stopping injecting idle cycles on CPUs '%*pbl'\n" , +cpumask_pr_args (to_cpumask (ii_dev->cpumask))); + +hrtimer_cancel (&ii_dev->timer); +drivers/powercap/idle_injection.c line 239
Quoted 10 lines
drivers/powercap/idle_injection.c new file +10
-0+/* +* We want the guarantee we have a quescient point where +* parked threads stay in there state while we are stopping +* the idle injection. After exiting the loop, if any CPU is +* plugged in, the 'should_run' boolean being false, the +* smpboot main loop schedules the task out. +*/ +cpu_hotplug_disable (); + +for_each_cpu_and (cpu,to_cpumask (ii_dev->cpumask), cpu_online_mask) {Maybe you should do below for all CPUs in the mask. Is the below usecase possible ?
- CPU0-4 are part of the mask and are all online.
- hrtimer fires and setsshould_runfor all of them to 1.
Quoted 9 lines
Quoted 4 lines
^^
hrtimer_cancelgives you the guarantee, the timer is no longer active and there is no execution in the timer handler. So the timer can no longer fire afterhrtimer_cancel()is called (which is a blocking call).Right but that isn't called yet in my sequence.
Quoted 2 lines
Quoted 2 lines
- Right at this time CPU3 goes offline, so the thread gets parked with
should_run== 1. Is there a reason why this can't happen ?
for this specific case, we can use the park() callback to set should_run to false, no ?
Quoted 22 lines
Quoted 1 line
Quoted 1 line
- Now we unregister the stuff and CPU3 again comes online.
It gets called here from unregister/stop.
Quoted 12 lines
Quoted 11 lines
- Because it had
should_runas true, we again run the thread and Crash.makes sense ?
drivers/powercap/idle_injection.c line 316
Quoted 3 lines
drivers/powercap/idle_injection.c new file +3
-0+out_rollback_per_cpu: +for_each_cpu (cpu,to_cpumask (ii_dev->cpumask)) +per_cpu (idle_injection_device, cpu) =NULL ;So if two parts of the kernel call this routine with the same cpumask, then the second call will also overwrite the masks with NULL and return error. That will screw up things a bit here.
Quoted 3 lines
Apparently there is a misunderstanding :)
https://lkml.org/lkml/2018/5/29/209 (at the end)
Right, your earlier version was doing the right thing :)
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 2 lines · Daniel Lezcano
for this specific case, we can use the
park()callback to setshould_runto false, no ?
Yep, that can be one option. Or just iterate through all the CPUs in the mask.
Signature · 1 line
viresh