Per CPU idle injection
Subsystems (2)
v3 cover Per CPU idle injection
This series introduces per CPU idle injection. In preparation for this enhance the existing powercap/idle_inject and modify intel_powerclamp to use this. Then add per core idle injection driver.
v3
- Change callback from per CPU to per device and use in intel_powerclamp
- Remove unused var in per cpu idle injection module
v2
- Update based on feedback from Rafael on patch 2/4
- Kconfig dependency issue
Srinivas Pandruvada (4):
powercap: idle_inject: Export symbols
powercap: idle_inject: Add update callback thermal/drivers/intel_powerclamp: Use powercap idle-inject framework thermal/drivers/intel_cpu_idle_cooling: Introduce Intel cpu idle
cooling driver
drivers/powercap/idle_inject.c | 57 ++- drivers/thermal/intel/Kconfig | 12 + drivers/thermal/intel/Makefile | 1 + .../thermal/intel/intel_cpu_idle_cooling.c | 259 ++++++++++++++ drivers/thermal/intel/intel_powerclamp.c | 328 +++++++----------- include/linux/idle_inject.h | 3 + 6 files changed, 443 insertions(+), 217 deletions(-) create mode 100644 drivers/thermal/intel/intel_cpu_idle_cooling.c
Signature · 1 line
2.31.1
PATCH v3 1/4 powercap: idle_inject: Export symbols
Export symbols for external interfaces, so that they can be used in other loadable modules.
Export is done under name space IDLE_INJECT.
---
v2/v3:
No change
drivers/powercap/idle_inject.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/powercap/idle_inject.c +7 -0
@@ -160,6 +160,7 @@ void idle_inject_set_duration(struct idle_inject_device *ii_dev,WRITE_ONCE (ii_dev->idle_duration_us, idle_duration_us); } } +EXPORT_SYMBOL_NS_GPL (idle_inject_set_duration,IDLE_INJECT );/** * idle_inject_get_duration - idle and run duration retrieval helper
@@ -174,6 +175,7 @@ void idle_inject_get_duration(struct idle_inject_device *ii_dev, *run_duration_us =READ_ONCE (ii_dev->run_duration_us); *idle_duration_us =READ_ONCE (ii_dev->idle_duration_us); } +EXPORT_SYMBOL_NS_GPL (idle_inject_get_duration,IDLE_INJECT );/** * idle_inject_set_latency - set the maximum latency allowed
@@ -185,6 +187,7 @@ void idle_inject_set_latency(struct idle_inject_device *ii_dev, {WRITE_ONCE (ii_dev->latency_us, latency_us); } +EXPORT_SYMBOL_NS_GPL (idle_inject_set_latency,IDLE_INJECT );/** * idle_inject_start - start idle injections
@@ -216,6 +219,7 @@ int idle_inject_start(struct idle_inject_device *ii_dev)return 0 ; } +EXPORT_SYMBOL_NS_GPL (idle_inject_start,IDLE_INJECT );/** * idle_inject_stop - stops idle injections
@@ -262,6 +266,7 @@ void idle_inject_stop(struct idle_inject_device *ii_dev)cpu_hotplug_enable (); } +EXPORT_SYMBOL_NS_GPL (idle_inject_stop,IDLE_INJECT );/** * idle_inject_setup - prepare the current task for idle injection
@@ -337,6 +342,7 @@ struct idle_inject_device *idle_inject_register(struct cpumask *cpumask)return NULL ; } +EXPORT_SYMBOL_NS_GPL (idle_inject_register,IDLE_INJECT );/** * idle_inject_unregister - unregister idle injection control device
@@ -357,6 +363,7 @@ void idle_inject_unregister(struct idle_inject_device *ii_dev)kfree (ii_dev); } +EXPORT_SYMBOL_NS_GPL (idle_inject_unregister,IDLE_INJECT );static struct smp_hotplug_thread idle_inject_threads = { .store = &idle_inject_thread.tsk,
Signature · 1 line
2.31.1
Re: [PATCH v3 1/4] powercap: idle_inject: Export symbols
Quoted 7 lines · Srinivas Pandruvada
Export symbols for external interfaces, so that they can be used in other loadable modules.
Export is done under name space
IDLE_INJECT.Signed-off-by Srinivas Pandruvada <[email protected]>---
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
PATCH v3 2/4 powercap: idle_inject: Add update callback
In reply to Srinivas PandruvadaThe powercap/idle_inject core uses play_idle_precise() to inject idle time. But play_idle_precise() can't ensure that the CPU is fully idle for the specified duration because of wakeups due to interrupts. To compensate for the reduced idle time due to these wakes, the caller can adjust requested idle time for the next cycle.
The goal of idle injection is to keep system at some idle percent on average, so this is fine to overshoot or undershoot instantaneous idle times.
The idle inject core provides an interface idle_inject_set_duration() to set idle and runtime duration.
Some architectures provide interface to get actual idle time observed by the hardware. So, the effective idle percent can be adjusted using the hardware feedback. For example, Intel CPUs provides package idle counters, which is currently used by Intel powerclamp driver to readjust runtime duration.
When the caller's desired idle time over a period is less or greater than the actual CPU idle time observed by the hardware, caller can readjust idle and runtime duration for the next cycle.
The only way this can be done currently is by monitoring hardware idle time from a different software thread and readjust idle and runtime duration using idle_inject_set_duration().
This can be avoided by adding a callback which callers can register and readjust from this callback function.
Add a capability to register an optional update() callback, which can be called from the idle inject core before waking up CPUs for idle injection. This callback can be registered via a new interface:idle_inject_register_full().
During this process of constantly adjusting idle and runtime duration there can be some cases where actual idle time is more than the desired. In this case idle inject can be skipped for a cycle. If update() callback returns false, then the idle inject core skips waking up CPUs for the idle injection.
---
v3:
- Replace prepare/complete callback with update callback
v2
- Replace begin/end with prepare/complete
- Add new interface idle_inject_register_full with callbacks
- Update kernel doc
- Update commit description
drivers/powercap/idle_inject.c | 50 ++++++++++++++++++++++++++++++---- include/linux/idle_inject.h | 3 ++ 2 files changed, 47 insertions(+), 6 deletions(-)
drivers/powercap/idle_inject.c +44 -6
@@ -63,13 +63,27 @@ struct idle_inject_thread {* @idle_duration_us: duration of CPU idle time to inject * @run_duration_us: duration of CPU run time to allow * @latency_us: max allowed latency +* @update: Optional callback deciding whether or not to skip idle +* injection in the given cycle. * @cpumask: mask of CPUs affected by idle injection +* +* This structure is used to define per instance idle inject device data. Each +* instance has an idle duration, a run duration and mask of CPUs to inject +* idle. +* Actual idle is injected by calling kernel scheduler interface +* play_idle_precise(). There is one optional callbacks which the caller can +* register by calling idle_inject_register_full(): +* update() - This callback is called just before waking up CPUs to inject +* idle. If this callback returns false, CPUs are not woken up to inject idle +* for this cycle. Also gives opportunity to the caller to readjust idle +* and run duration by calling idle_inject_set_duration() for the next cycle. */ struct idle_inject_device {struct hrtimer timer;unsigned int idle_duration_us;unsigned int run_duration_us;unsigned int latency_us; +bool (*update)(void );unsigned long cpumask[]; };
@@ -111,11 +125,12 @@ static enum hrtimer_restart idle_inject_timer_fn(struct hrtimer *timer)struct idle_inject_device *ii_dev = container_of(timer, struct idle_inject_device, timer); +if (!ii_dev->update || (ii_dev->update && ii_dev->update ())) +idle_inject_wakeup (ii_dev); + duration_us =READ_ONCE (ii_dev->run_duration_us); duration_us +=READ_ONCE (ii_dev->idle_duration_us); -idle_inject_wakeup (ii_dev); -hrtimer_forward_now (timer,ns_to_ktime (duration_us *NSEC_PER_USEC ));return HRTIMER_RESTART ;
@@ -295,17 +310,22 @@ static int idle_inject_should_run(unsigned int cpu) }/** -* idle_inject_register - initialize idle injection on a set of CPUs +* idle_inject_register_full - initialize idle injection on a set of CPUs * @cpumask: CPUs to be affected by idle injection +* @update: This callback is called just before waking up CPUs to inject +* idle * * This function creates an idle injection control device structure for the -* given set of CPUs and initializes the timer associated with it. It does not -* start any injection cycles. +* given set of CPUs and initializes the timer associated with it. This +* function also allows to register update()callback. +* It does not start any injection cycles. * * Return: NULL if memory allocation fails, idle injection control device * pointer on success. */ -struct idle_inject_device *idle_inject_register (struct cpumask *cpumask) + +struct idle_inject_device *idle_inject_register_full (struct cpumask *cpumask, +bool (*update)(void )) {struct idle_inject_device *ii_dev;int cpu, cpu_rb;
@@ -318,6 +338,7 @@ struct idle_inject_device *idle_inject_register(struct cpumask *cpumask)hrtimer_init (&ii_dev->timer,CLOCK_MONOTONIC ,HRTIMER_MODE_REL ); ii_dev->timer.function = idle_inject_timer_fn; ii_dev->latency_us =UINT_MAX ; + ii_dev->update = update;for_each_cpu (cpu,to_cpumask (ii_dev->cpumask)) {
@@ -342,6 +363,23 @@ struct idle_inject_device *idle_inject_register(struct cpumask *cpumask)return NULL ; } +EXPORT_SYMBOL_NS_GPL (idle_inject_register_full,IDLE_INJECT ); + +/** +* idle_inject_register - initialize idle injection on a set of CPUs +* @cpumask: CPUs to be affected by idle injection +* +* This function creates an idle injection control device structure for the +* given set of CPUs and initializes the timer associated with it. It does not +* start any injection cycles. +* +* Return: NULL if memory allocation fails, idle injection control device +* pointer on success. +*/ +struct idle_inject_device *idle_inject_register (struct cpumask *cpumask) +{ +return idle_inject_register_full (cpumask,NULL ); +}EXPORT_SYMBOL_NS_GPL (idle_inject_register,IDLE_INJECT );/**
include/linux/idle_inject.h +3 -0
@@ -13,6 +13,9 @@ struct idle_inject_device;struct idle_inject_device *idle_inject_register (struct cpumask *cpumask); +struct idle_inject_device *idle_inject_register_full (struct cpumask *cpumask, +bool (*update)(void )); +void idle_inject_unregister (struct idle_inject_device *ii_dev);int idle_inject_start (struct idle_inject_device *ii_dev);
Signature · 1 line
2.31.1
Re: [PATCH v3 2/4] powercap: idle_inject: Add update callback
On 17/01/2023 03:07, Srinivas Pandruvada wrote:
[ ... ]
drivers/powercap/idle_inject.c struct idle_inject_device *idle_inject_register(struct cpumask *cpumask) · line 383
Quoted 5 lines
drivers/powercap/idle_inject.c +4
-0+struct idle_inject_device *idle_inject_register (struct cpumask *cpumask) +{ +return idle_inject_register_full (cpumask,NULL ); +}EXPORT_SYMBOL_NS_GPL (idle_inject_register,IDLE_INJECT );
Why not just add the parameter to idle_inject_register() ?
There is only one user ATM
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
Re: [PATCH v3 2/4] powercap: idle_inject: Add update callback
Quoted 13 lines · Daniel Lezcano
On 17/01/2023 03:07, Srinivas Pandruvada wrote:
[ ... ]
Quoted 6 lines
+struct
idle_inject_device*idle_inject_register(struct cpumask *cpumask)drivers/powercap/idle_inject.c +3
-0+{ + return idle_inject_register_full(cpumask, NULL); +}
EXPORT_SYMBOL_NS_GPL(idle_inject_register, IDLE_INJECT);Why not just add the parameter to
idle_inject_register()?
Quoted 1 line
There is only one user ATM
That was done in v1. But Rafael suggested to avoid changes to existing kernel source.
Thanks,
Srinivas
Quoted 2 lines
Rafael J. Wysocki
maintainerRe: [PATCH v3 2/4] powercap: idle_inject: Add update callback
Quoted 15 lines · srinivas pandruvada
Quoted 13 lines · Daniel Lezcano
On 17/01/2023 03:07, Srinivas Pandruvada wrote:
[ ... ]
drivers/powercap/idle_inject.c struct idle_inject_device *idle_inject_register(struct cpumask *cpumask) · line 383
Quoted 6 lines
+struct
idle_inject_device*idle_inject_register(struct cpumask *cpumask)drivers/powercap/idle_inject.c +3
-0+{ +return idle_inject_register_full (cpumask,NULL ); +}EXPORT_SYMBOL_NS_GPL (idle_inject_register,IDLE_INJECT );Why not just add the parameter to
idle_inject_register()?
Quoted 3 lines
Quoted 1 line
There is only one user ATM
That was done in v1. But Rafael suggested to avoid changes to existing kernel source.
However, it can be done if Daniel prefers that. It is 1 callback now only, so no big deal I suppose.
Re: [PATCH v3 2/4] powercap: idle_inject: Add update callback
On Tue, 2023-01-17 at 16:09 +0100, Rafael J. Wysocki wrote:
Hi Daniel,
Quoted 17 lines
Quoted 15 lines · srinivas pandruvada
Quoted 13 lines · Daniel Lezcano
On 17/01/2023 03:07, Srinivas Pandruvada wrote:
[ ... ]
Quoted 6 lines
+struct
idle_inject_device*idle_inject_register(struct cpumask *cpumask)drivers/powercap/idle_inject.c +3
-0+{ + return idle_inject_register_full(cpumask, NULL); +}
EXPORT_SYMBOL_NS_GPL(idle_inject_register, IDLE_INJECT);Why not just add the parameter to
idle_inject_register()?
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
There is only one user ATM
That was done in v1. But Rafael suggested to avoid changes to existing
kernel source.However, it can be done if Daniel prefers that. It is 1 callback now only, so no big deal I suppose.
Do you have preference? I can submit an update.
Thanks,
Srinivas
PATCH v3 3/4 thermal/drivers/intel_powerclamp: Use powercap idle-inject framework
In reply to Srinivas PandruvadaThere are two idle injection implementation in the Linux kernel. One via intel_powerclamp and the other using powercap/idle_inject. Both implementation end up in calling play_idle* function from a FIFO priority thread. Both can't be used at the same time.
Currently per core idle injection (cpuidle_cooling) is using powercap/idle_inject, which is not used in platforms where intel_powerclamp is used for system wide idle injection. So there is no conflict. But there are some use cases where per core idle injection is beneficial on the same system where system wide idle injection is also used via intel_powerclamp. To avoid conflict only one of the idle injection type must be in use at a time. This require a common framework which both per core and system wide idle injection can use.
Here powercap/idle_inject can be used for both per-core and for system wide idle injection. This framework has a well defined interface which allow registry for per-core or for all CPUs (system wide). If particular CPU is already participating in idle injection, the call to registry fails. Here the registry can be done when user space changes the current cooling device state.
Also one framework for idle injection is better as there is one loop calling play_idle*, instead of multiple for better maintenance.
So, reuse powercap/idle_inject calls in intel_powerclamp. This simplifies the code as all per CPU kthreads which calls play_idle* can be removed.
The changes include:
- Remove unneeded include files
- Remove per CPU kthread workers: balancing_work and idle_injection_work.
- Reuse the compensation related code by moving from previous worker thread to idle_injection callback.
- Adjust the idle_duration and runtime by using powercap/idle_inject interface.
- Remove all variables, which are not required once powercap/idle_inject is used.
- Add mutex to avoid race during removal of idle injection during module unload and user action to change idle inject percent. Also for protection during dynamic adjustment of run and idle time from update() callback.
- Remove online/offline callbacks to designate control CPU
---
v3:
- Use Update callback which is per device
- Remove use of control_cpu and online/offline callback to set this
v2:
- Use idle_inject_register_full instead of idle_inject_register
- Also fix dependency issue with POWERCAP config
drivers/thermal/intel/Kconfig | 2 + drivers/thermal/intel/intel_powerclamp.c | 328 ++++++++--------------- 2 files changed, 119 insertions(+), 211 deletions(-)
drivers/thermal/intel/Kconfig +2 -0
@@ -3,6 +3,8 @@ config INTEL_POWERCLAMP tristate "Intel PowerClamp idle injection driver" depends on X86 depends on CPU_SUP_INTEL + select POWERCAP + select IDLE_INJECT help Enable this to enable Intel PowerClamp idle injection driver. This enforce idle time which results in more package C-state residency. The
drivers/thermal/intel/intel_powerclamp.c +117 -211
496 lines not on this page · raw message
Signature · 1 line
2.31.1
Re: [PATCH v3 3/4] thermal/drivers/intel_powerclamp: Use powercap idle-inject framework
On 17/01/2023 03:07, Srinivas Pandruvada wrote:
[ ... ]
drivers/thermal/intel/Kconfig config INTEL_POWERCLAMP · line 7
Quoted 10 lines
drivers/thermal/intel/Kconfig +2
-0@@ -3,6 +3,8 @@ config INTEL_POWERCLAMP tristate "Intel PowerClamp idle injection driver" depends on X86 depends on CPU_SUP_INTEL + select POWERCAP + select IDLE_INJECT
Just a question, not a comment. As IDLE_INJECT depends on POWERCAP, is it necessary to select POWERCAP also here ?
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
Rafael J. Wysocki
maintainerRe: [PATCH v3 3/4] thermal/drivers/intel_powerclamp: Use powercap idle-inject framework
Quoted 5 lines · Daniel Lezcano
On 17/01/2023 03:07, Srinivas Pandruvada wrote:
[ ... ]
Quoted 13 lines
drivers/thermal/intel/Kconfig config INTEL_POWERCLAMP · line 7
Quoted 10 lines
drivers/thermal/intel/Kconfig +2
-0@@ -3,6 +3,8 @@ config INTEL_POWERCLAMP tristate "Intel PowerClamp idle injection driver" depends on X86 depends on CPU_SUP_INTEL + select POWERCAP + select IDLE_INJECTJust a question, not a comment. As
IDLE_INJECTdepends on POWERCAP, is it necessary to select POWERCAP also here ?
Yes, it is.
If IDLE_INJECT alone is selected, POWERCAP may not be and there will be a broken dependency.
Re: [PATCH v3 3/4] thermal/drivers/intel_powerclamp: Use powercap idle-inject framework
Quoted 21 lines · Rafael J. Wysocki
Quoted 5 lines · Daniel Lezcano
On 17/01/2023 03:07, Srinivas Pandruvada wrote:
[ ... ]
Quoted 13 lines
drivers/thermal/intel/Kconfig config INTEL_POWERCLAMP · line 7
Quoted 10 lines
drivers/thermal/intel/Kconfig +2
-0@@ -3,6 +3,8 @@ config INTEL_POWERCLAMP tristate "Intel PowerClamp idle injection driver" depends on X86 depends on CPU_SUP_INTEL + select POWERCAP + select IDLE_INJECTJust a question, not a comment. As
IDLE_INJECTdepends on POWERCAP, is it necessary to select POWERCAP also here ?
Quoted 4 lines
Yes, it is.
If
IDLE_INJECTalone is selected, POWERCAP may not be and there will be a broken dependency.
Ok, thanks
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
PATCH v3 4/4 thermal/drivers/intel_cpu_idle_cooling: Introduce Intel cpu idle cooling driver
In reply to Srinivas PandruvadaThe cpu idle cooling is used to cool down a CPU by injecting idle cycles at runtime. The objective is similar to intel_powerclamp driver, which is used for system wide cooling by injecting idle on each CPU.
This driver is modeled after drivers/thermal/cpuidle_cooling.c by reusing powercap/idle_inject framework.
On each CPU online a thermal cooling device is registered. The minimum state of the cooling device is 0 and maximum is 100. When user space changes the current state to non zero, then register with idle inject framework and start idle inject.
The default idle duration is 24 milli seconds, matching intel_powerclamp, which doesn't change based on the current state of cooling device. The runtime is changed based on the current state.
---
v3:
Suggestion from Daniel:
- Remove unused cpuidle_cpu_mask
- Why not register idle_inject here before calling thermal_cooling_device_register() so you get ride of the lock?
We can't as cooling device is registered during CPU online. But if you also register idle,
then every CPU is registered with idle inject core. So intel_powerclamp can't
use those CPUs to register again.
Here the logic is only register when user actually changing the state of cooling device from 0 to 1. In this way either per CPU or all cpu (intel_powerclamp) can be used one at a time.
Reuse cpuidle_cooling_device.c. The RFC tried to do that, But you don't save lots of reuse. The of_device separation and separate idle_injection register will need a separate function.
To be clean implementation, added a new module. This module adds very few lines of new code.
v2:
- Removed callback arguments for idle_inject_register
drivers/thermal/intel/Kconfig | 10 + drivers/thermal/intel/Makefile | 1 + .../thermal/intel/intel_cpu_idle_cooling.c | 259 ++++++++++++++++++ 3 files changed, 270 insertions(+) create mode 100644 drivers/thermal/intel/intel_cpu_idle_cooling.c
drivers/thermal/intel/Kconfig +10 -0
@@ -115,3 +115,13 @@ config INTEL_HFI_THERMAL These capabilities may change as a result of changes in the operating conditions of the system such power and thermal limits. If selected, the kernel relays updates in CPUs' capabilities to userspace. + +config INTEL_CPU_IDLE_COOLING + tristate "Intel CPU idle cooling device" + depends on IDLE_INJECT + help + This implements the CPU cooling mechanism through + idle injection. This will throttle the CPU by injecting + idle cycle. + Unlike Intel Power clamp driver, this driver provides + idle injection for each CPU.
drivers/thermal/intel/Makefile +1 -0
@@ -14,3 +14,4 @@ obj-$(CONFIG_INTEL_TCC_COOLING) += intel_tcc_cooling.o obj-$(CONFIG_X86_THERMAL_VECTOR) += therm_throt.o obj-$(CONFIG_INTEL_MENLOW) += intel_menlow.o obj-$(CONFIG_INTEL_HFI_THERMAL) += intel_hfi.o +obj-$(CONFIG_INTEL_CPU_IDLE_COOLING) += intel_cpu_idle_cooling.o
drivers/thermal/intel/intel_cpu_idle_cooling.c new file +259 -0
259 lines not on this page · raw message
Signature · 1 line
2.31.1