Skip to contentSkip to conversation

Use idle_inject framework for intel_powerclamp

PATCH In review Srinivas Pandruvada 4 participants 11 messages Feb 1, 2023 – Feb 5, 2023

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

v5 cover Use idle_inject framework for intel_powerclamp

Dropped the per core idle injection patch from this series. Hence the subject of this series is changed. Now it is just for using
idle inject framework for intel_powerclamp.
Add additionl patch for module paramters to set cpumask and max idle percent.

The old cover letter:
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.

v5:
- additionl patch for module paramters to set cpumask and max idle percent
- One fix for update callback
- Rebased on top of patch
"thermal: intel_powerclamp: Fix cur_state for multi package system"

v4:
- Dropped the per core idle inject patch

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_powerclamp: Add additional module params

 .../driver-api/thermal/intel_powerclamp.rst   |  22 +
 drivers/powercap/idle_inject.c                |  57 +-
 drivers/thermal/intel/Kconfig                 |   2 +
 drivers/thermal/intel/intel_powerclamp.c      | 512 ++++++++++--------
 include/linux/idle_inject.h                   |   3 +
 5 files changed, 367 insertions(+), 229 deletions(-)
Signature · 1 line
2.39.1
#1 lore raw

PATCH v5 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/v4/v5:
No change

 drivers/powercap/idle_inject.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/powercap/idle_inject.c +7 -0
index c03b5402c03b..ec02b370ec16 100644
@@ -162,6 +162,7 @@ void idle_inject_set_duration(struct idle_inject_device *ii_dev,
 	if (!run_duration_us)
 		pr_debug("CPU is forced to 100 percent idle\n");
 }
+EXPORT_SYMBOL_NS_GPL(idle_inject_set_duration, IDLE_INJECT);
 
 /**
  * idle_inject_get_duration - idle and run duration retrieval helper
@@ -176,6 +177,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
@@ -187,6 +189,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
@@ -218,6 +221,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
@@ -264,6 +268,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
@@ -339,6 +344,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
@@ -359,6 +365,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.39.1
Acked-by on v5 1/4
#2 lore raw

Re: [PATCH v5 1/4] powercap: idle_inject: Export symbols

Quoted 6 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.

(missing my ack from v3)

Quoted 67 lines

---
v2/v3/v4/v5:
No change

  drivers/powercap/idle_inject.c | 7 +++++++
  1 file changed, 7 insertions(+)
drivers/powercap/idle_inject.c +7 -0
index c03b5402c03b..ec02b370ec16 100644
@@ -162,6 +162,7 @@ void idle_inject_set_duration(struct idle_inject_device *ii_dev,
  	if (!run_duration_us)
  		pr_debug("CPU is forced to 100 percent idle\n");
  }
+EXPORT_SYMBOL_NS_GPL(idle_inject_set_duration, IDLE_INJECT);
 
  /**
   * idle_inject_get_duration - idle and run duration retrieval helper
@@ -176,6 +177,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
@@ -187,6 +189,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
@@ -218,6 +221,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
@@ -264,6 +268,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
@@ -339,6 +344,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
@@ -359,6 +365,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 · 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
#3 lore raw

PATCH v5 2/4 powercap: idle_inject: Add update callback

In reply to Srinivas Pandruvada

The 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.

---
v4/v5:
- No change

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
index ec02b370ec16..3ac81086d71f 100644
@@ -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;
@@ -297,17 +312,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;
@@ -320,6 +340,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)) {
 
@@ -344,6 +365,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
index fb88e23a99d3..a85d5dd40f72 100644
@@ -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.39.1
#4 lore raw

PATCH v5 3/4 thermal/drivers/intel_powerclamp: Use powercap idle-inject framework

In reply to Srinivas Pandruvada

There 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.

It is better to use one idle injection framework for better maintainability. In this way, there is only one caller for play_idle.

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).

This reduces code complexity in the intel powerclamp driver as all the per CPU kthreads, delayed work and calls to 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
- Use cpu_present_mask global variable for CPU mask
- Remove hot plug locks

---
v5:
- Can no longer sleep in update() callbak which is a hr timer callback So skip if mutex will result in sleep.

v4:
- Remove local cpumask for present CPUs as there is one global one also no need for hot plug locks as it can't change
- Add some comments on functions

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 | 379 ++++++++++-------------
 2 files changed, 158 insertions(+), 223 deletions(-)
drivers/thermal/intel/Kconfig +2 -0
index e50fd260484a..b346a646fffb 100644
@@ -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 +156 -223

560 lines not on this page · raw message

Signature · 1 line
2.39.1
#5 lore raw

PATCH v5 4/4 thermal/drivers/intel_powerclamp: Add additional module params

In reply to Srinivas Pandruvada

In some use cases, it is desirable to only inject idle on certain set of CPUs. For example on Alder Lake systems, it is possible that we force idle only on P-Cores for thermal reasons. Also the idle percent can be more than 50% if we only choose partial set of CPUs in the system.

Introduce module parameters for setting cpumask and max_idle. They can be only changed when the cooling device is inactive. This module already have other module parameters. There is no change done for those parameters.

cpumask (Read/Write): A bit mask of CPUs to inject idle. The format of this bitmask is same as used in other subsystems like in /proc/irq/*/smp_affinity. The mask is comma separated 32 bit groups. Each CPU is one bit. For example for 256 CPU system the full mask is:
ffffffff,ffffffff,ffffffff,ffffffff,ffffffff,ffffffff,ffffffff,ffffffff The leftmost mask is for CPU 0-32.

max_idle (Read/Write): Maximum injected idle time to the total CPU time ratio in percent range from 1 to 100. Even if the cooling device max_state is always 100 (100%), this parameter allows to add a max idle percent limit. The default is 50, to match the current implementation of powerclamp driver. Also doesn't allow value more than 75, if the cpumask includes every CPU present in the system.

---
v5
New patch

 .../driver-api/thermal/intel_powerclamp.rst   |  22 +++
 drivers/thermal/intel/intel_powerclamp.c      | 169 ++++++++++++++++--
 2 files changed, 173 insertions(+), 18 deletions(-)
Documentation/driver-api/thermal/intel_powerclamp.rst +22 -0
index 3f6dfb0b3ea6..d805e28b7a45 100644
@@ -26,6 +26,8 @@ By:
 	    - Generic Thermal Layer (sysfs)
 	    - Kernel APIs (TBD)
 
+	(*) Module Parameters
+
 INTRODUCTION
 ============
 
@@ -318,3 +320,23 @@ device, a PID based userspace thermal controller can manage to
 control CPU temperature effectively, when no other thermal influence
 is added. For example, a UltraBook user can compile the kernel under
 certain temperature (below most active trip points).
+
+Module Parameters
+=================
+
+``cpumask`` (RW)
+	A bit mask of CPUs to inject idle. The format of the bitmask is same as
+	used in other subsystems like in /proc/irq/*/smp_affinity. The mask is
+	comma separated 32 bit groups. Each CPU is one bit. For example for a 256
+	CPU system the full mask is:
+	ffffffff,ffffffff,ffffffff,ffffffff,ffffffff,ffffffff,ffffffff,ffffffff
+
+	The leftmost mask is for CPU 0-32.
+
+``max_idle`` (RW)
+	Maximum injected idle time to the total CPU time ratio in percent range
+	from 1 to 100. Even if the cooling device max_state is always 100 (100%),
+	this parameter allows to add a max idle percent	limit. The default is 50,
+	to match the current implementation of powerclamp driver. Also doesn't
+	allow value more than 75, if the cpumask includes every CPU present in
+	the system.
drivers/thermal/intel/intel_powerclamp.c +151 -18

217 lines not on this page · raw message

Signature · 1 line
2.39.1
#6 lore raw

Re: [PATCH v5 4/4] thermal/drivers/intel_powerclamp: Add additional module params

First, I would say "Add two module parameters" in the subject.

Quoted 7 lines · Srinivas Pandruvada

In some use cases, it is desirable to only inject idle on certain set of CPUs. For example on Alder Lake systems, it is possible that we force idle only on P-Cores for thermal reasons. Also the idle percent can be more than 50% if we only choose partial set of CPUs in the system.

Introduce module parameters for setting cpumask and max_idle.

"Introduce 2 new module parameters for this purpose."

Quoted 3 lines

They can be only changed when the cooling device is inactive. This module already have other module parameters. There is no change done for those parameters.

s/have/has/

"... other parameters that are not affected by this change."

Quoted 50 lines

cpumask (Read/Write): A bit mask of CPUs to inject idle. The format of this bitmask is same as used in other subsystems like in /proc/irq/*/smp_affinity. The mask is comma separated 32 bit groups. Each CPU is one bit. For example for 256 CPU system the full mask is:
ffffffff,ffffffff,ffffffff,ffffffff,ffffffff,ffffffff,ffffffff,ffffffff The leftmost mask is for CPU 0-32.

max_idle (Read/Write): Maximum injected idle time to the total CPU time ratio in percent range from 1 to 100. Even if the cooling device max_state is always 100 (100%), this parameter allows to add a max idle percent limit. The default is 50, to match the current implementation of powerclamp driver. Also doesn't allow value more than 75, if the cpumask includes every CPU present in the system.

---
v5
New patch

 .../driver-api/thermal/intel_powerclamp.rst   |  22 +++
 drivers/thermal/intel/intel_powerclamp.c      | 169 ++++++++++++++++--
 2 files changed, 173 insertions(+), 18 deletions(-)
Documentation/driver-api/thermal/intel_powerclamp.rst +12 -0
index 3f6dfb0b3ea6..d805e28b7a45 100644
@@ -26,6 +26,8 @@ By:
            - Generic Thermal Layer (sysfs)
            - Kernel APIs (TBD)
 
+       (*) Module Parameters
+
 INTRODUCTION
 ============
 
@@ -318,3 +320,23 @@ device, a PID based userspace thermal controller can manage to
 control CPU temperature effectively, when no other thermal influence
 is added. For example, a UltraBook user can compile the kernel under
 certain temperature (below most active trip points).
+
+Module Parameters
+=================
+
+``cpumask`` (RW)
+       A bit mask of CPUs to inject idle. The format of the bitmask is same as
+       used in other subsystems like in /proc/irq/*/smp_affinity. The mask is
+       comma separated 32 bit groups. Each CPU is one bit. For example for a 256
+       CPU system the full mask is:
+       ffffffff,ffffffff,ffffffff,ffffffff,ffffffff,ffffffff,ffffffff,ffffffff

Documentation/driver-api/thermal/intel_powerclamp.rst device, a PID based userspace thermal controller can manage to · line 342

Quoted 10 lines
Documentation/driver-api/thermal/intel_powerclamp.rst +10 -0
+
+       The leftmost mask is for CPU 0-32.
+
+``max_idle`` (RW)
+       Maximum injected idle time to the total CPU time ratio in percent range
+       from 1 to 100. Even if the cooling device max_state is always 100 (100%),
+       this parameter allows to add a max idle percent limit. The default is 50,
+       to match the current implementation of powerclamp driver. Also doesn't
+       allow value more than 75, if the cpumask includes every CPU present in
+       the system.

I'm not sure if this is driver-api. It's admin-guide rather IMO.

Quoted 18 lines
drivers/thermal/intel/intel_powerclamp.c +2 -1
index 850195ebe5e0..68830b726da2 100644
@@ -37,7 +37,7 @@
 #include <asm/mwait.h>
 #include <asm/cpu_device_id.h>
 
-#define MAX_TARGET_RATIO (50U)
+#define MAX_TARGET_RATIO (100U)
 /* For each undisturbed clamping period (no extra wake ups during idle time),
  * we increment the confidence counter for the given target ratio.
  * CONFIDENCE_OK defines the level where runtime calibration results are
@@ -109,6 +109,135 @@ static const struct kernel_param_ops duration_ops = {
 module_param_cb(duration, &duration_ops, &duration, 0644);
 MODULE_PARM_DESC(duration, "forced idle time for each attempt in msec.");
 
+#define DEFAULT_MAX_IDLE       50

drivers/thermal/intel/intel_powerclamp.c static const struct kernel_param_ops duration_ops = { · line 122

Quoted 10 lines
drivers/thermal/intel/intel_powerclamp.c +10 -0
+#define MAX_ALL_CPU_IDLE       75
+
+static u8 max_idle = DEFAULT_MAX_IDLE;
+
+static cpumask_var_t idle_injection_cpu_mask;
+
+static int allocate_idle_injection_mask(void)
+{
+       /* This mask is allocated only one time and freed during module exit */
+       if (!idle_injection_cpu_mask) {

I would do

if (idle_injection_cpu_mask)

         return 0;

here and rearrange the rest of the function accordingly.

Quoted 18 lines
drivers/thermal/intel/intel_powerclamp.c +18 -0
+               if (!zalloc_cpumask_var(&idle_injection_cpu_mask, GFP_KERNEL))
+                       return -ENOMEM;
+
+               cpumask_copy(idle_injection_cpu_mask, cpu_present_mask);
+       }
+
+       return 0;
+}
+
+static int cpumask_set(const char *arg, const struct kernel_param *kp)
+{
+       int ret;
+
+       mutex_lock(&powerclamp_lock);
+
+       /* Can't set mask when cooling device is in use */
+       if (powerclamp_data.clamping) {
+               ret = -EAGAIN;

drivers/thermal/intel/intel_powerclamp.c static const struct kernel_param_ops duration_ops = { · line 150

Quoted 10 lines
drivers/thermal/intel/intel_powerclamp.c +10 -0
+               goto skip_cpumask_set;
+       }
+
+       /*
+        * When module parameters are passed from kernel command line
+        * during insmod, the module parameter callback is called
+        * before powerclamp_init(), so we can't assume that some
+        * cpumask can be allocated before here.
+        */
+       ret = allocate_idle_injection_mask();

Could it be allocated by powerclamp_init(), though? It is not useful before that function runs anyway.

drivers/thermal/intel/intel_powerclamp.c static const struct kernel_param_ops duration_ops = { · line 154

Quoted 4 lines
drivers/thermal/intel/intel_powerclamp.c +4 -0
+       if (ret)
+               goto skip_cpumask_set;
+
+       ret = bitmap_parse(arg, strlen(arg), cpumask_bits(idle_injection_cpu_mask),

So this would replace the existing idle_injection_cpu_mask even if it is going to fail later.

drivers/thermal/intel/intel_powerclamp.c static const struct kernel_param_ops duration_ops = { · line 164

Quoted 10 lines
drivers/thermal/intel/intel_powerclamp.c +10 -0
+                          nr_cpumask_bits);
+       if (ret)
+               goto skip_cpumask_set;
+
+       if (cpumask_empty(idle_injection_cpu_mask)) {
+               ret = -EINVAL;
+               goto skip_cpumask_set;
+       }
+
+       if (cpumask_equal(cpu_present_mask, idle_injection_cpu_mask) &&

Should this check be against cpu_online_mask instead? Arguably offline CPUs don't matter here.

Quoted 9 lines
drivers/thermal/intel/intel_powerclamp.c +9 -0
+                         max_idle > MAX_ALL_CPU_IDLE) {
+               ret = -EINVAL;
+               goto skip_cpumask_set;
+       }
+
+       mutex_unlock(&powerclamp_lock);
+
+       return 0;
+

drivers/thermal/intel/intel_powerclamp.c static const struct kernel_param_ops duration_ops = { · line 183

Quoted 10 lines
drivers/thermal/intel/intel_powerclamp.c +10 -0
+skip_cpumask_set:
+       mutex_unlock(&powerclamp_lock);
+
+       return ret;
+}
+
+static int cpumask_get(char *buf, const struct kernel_param *kp)
+{
+       if (!idle_injection_cpu_mask)
+               return -EINVAL;

I would return -ENODEV here.

Quoted 6 lines
drivers/thermal/intel/intel_powerclamp.c +6 -0
+
+       return bitmap_print_to_pagebuf(false, buf, cpumask_bits(idle_injection_cpu_mask),
+                                      nr_cpumask_bits);
+}
+
+static const struct kernel_param_ops cpumask_ops = {

drivers/thermal/intel/intel_powerclamp.c static const struct kernel_param_ops duration_ops = { · line 199

Quoted 10 lines
drivers/thermal/intel/intel_powerclamp.c +10 -0
+       .set = cpumask_set,
+       .get = cpumask_get,
+};
+
+module_param_cb(cpumask, &cpumask_ops, NULL, 0644);
+MODULE_PARM_DESC(cpumask, "Mask of CPUs to use for idle injection.");
+
+static int max_idle_set(const char *arg, const struct kernel_param *kp)
+{
+       u8 _max_idle;

new_max_idle would be slightly better I think.

Quoted 11 lines
drivers/thermal/intel/intel_powerclamp.c +11 -0
+       int ret = 0;
+
+       mutex_lock(&powerclamp_lock);
+
+       /* Can't set mask when cooling device is in use */
+       if (powerclamp_data.clamping) {
+               ret = -EAGAIN;
+               goto skip_limit_set;
+       }
+
+       ret = kstrtou8(arg, 10, &_max_idle);

drivers/thermal/intel/intel_powerclamp.c static const struct kernel_param_ops duration_ops = { · line 220

Quoted 10 lines
drivers/thermal/intel/intel_powerclamp.c +10 -0
+       if (ret)
+               goto skip_limit_set;
+
+       if (_max_idle > MAX_TARGET_RATIO) {
+               ret = -EINVAL;
+               goto skip_limit_set;
+       }
+
+       if (idle_injection_cpu_mask && cpumask_equal(cpu_present_mask, idle_injection_cpu_mask) &&
+           _max_idle > MAX_ALL_CPU_IDLE) {

The same check is done here and in cpumask_set(). Could it be done in a separate function called from here and from there?

Quoted 62 lines
drivers/thermal/intel/intel_powerclamp.c +29 -15
+               ret = -EINVAL;
+               goto skip_limit_set;
+       }
+
+       max_idle = _max_idle;
+
+skip_limit_set:
+       mutex_unlock(&powerclamp_lock);
+
+       return ret;
+}
+
+static const struct kernel_param_ops max_idle_ops = {
+       .set = max_idle_set,
+       .get = param_get_int,
+};
+
+module_param_cb(max_idle, &max_idle_ops, &max_idle, 0644);
+MODULE_PARM_DESC(max_idle, "maximum injected idle time to the total CPU time ratio in percent range:1-100");
+
 struct powerclamp_calibration_data {
        unsigned long confidence;  /* used for calibration, basically a counter
                                    * gets incremented each time a clamping
@@ -342,6 +471,10 @@ static unsigned int get_run_time(void)
        unsigned int compensated_ratio;
        unsigned int runtime;
 
+       /* No compensation for non systemwide idle injection */
+       if (max_idle > MAX_ALL_CPU_IDLE)
+               return (duration * 100 / powerclamp_data.target_ratio - duration);
+
        /*
         * make sure user selected ratio does not take effect until
         * the next round. adjust target_ratio if user has changed
@@ -460,21 +593,11 @@ static void trigger_idle_injection(void)
  */
 static int powerclamp_idle_injection_register(void)
 {
-       /*
-        * The idle inject core will only inject for online CPUs,
-        * So we can register for all present CPUs. In this way
-        * if some CPU goes online/offline while idle inject
-        * is registered, nothing additional calls are required.
-        * The same runtime and idle time is applicable for
-        * newly onlined CPUs if any.
-        *
-        * Here cpu_present_mask can be used as is.
-        * cast to (struct cpumask *) is required as the
-        * cpu_present_mask is const struct cpumask *, otherwise
-        * there will be compiler warnings.
-        */
-       ii_dev = idle_inject_register_full((struct cpumask *)cpu_present_mask,
-                                          idle_inject_update);
+       if (cpumask_equal(cpu_present_mask, idle_injection_cpu_mask))
+               ii_dev = idle_inject_register_full(idle_injection_cpu_mask, idle_inject_update);
+       else
+               ii_dev = idle_inject_register(idle_injection_cpu_mask);
+
        if (!ii_dev) {
                pr_err("powerclamp: idle_inject_register failed\n");
                return -EAGAIN;

drivers/thermal/intel/intel_powerclamp.c static int start_power_clamp(void) · line 636

Quoted 5 lines
drivers/thermal/intel/intel_powerclamp.c +1 -1
@@ -510,7 +633,7 @@ static int start_power_clamp(void)
        ret = powerclamp_idle_injection_register();
        if (!ret) {
                trigger_idle_injection();
-               if (poll_pkg_cstate_enable)
+               if (poll_pkg_cstate_enable && max_idle < MAX_ALL_CPU_IDLE)

Why is the additional check needed here?

Quoted 37 lines
drivers/thermal/intel/intel_powerclamp.c +11 -2
                        schedule_delayed_work(&poll_pkg_cstate_work, 0);
        }
 
@@ -565,7 +688,7 @@ static int powerclamp_set_cur_state(struct thermal_cooling_device *cdev,
        mutex_lock(&powerclamp_lock);
 
        new_target_ratio = clamp(new_target_ratio, 0UL,
-                               (unsigned long) (MAX_TARGET_RATIO - 1));
+                               (unsigned long) (max_idle - 1));
        if (!powerclamp_data.target_ratio && new_target_ratio > 0) {
                pr_info("Start idle injection to reduce power\n");
                powerclamp_data.target_ratio = new_target_ratio;
@@ -656,6 +779,13 @@ static int __init powerclamp_init(void)
 
        /* probe cpu features and ids here */
        retval = powerclamp_probe();
+       if (retval)
+               return retval;
+
+       mutex_lock(&powerclamp_lock);
+       retval = allocate_idle_injection_mask();
+       mutex_unlock(&powerclamp_lock);
+
        if (retval)
                return retval;
 
@@ -689,6 +819,9 @@ static void __exit powerclamp_exit(void)
 
        cancel_delayed_work_sync(&poll_pkg_cstate_work);
        debugfs_remove_recursive(debug_dir);
+
+       if (idle_injection_cpu_mask)
+               free_cpumask_var(idle_injection_cpu_mask);
 }
 module_exit(powerclamp_exit);
 
--
#7 lore raw

Re: [PATCH v5 4/4] thermal/drivers/intel_powerclamp: Add additional module params

Quoted 15 lines · Rafael J. Wysocki

First, I would say "Add two module parameters" in the subject.

Quoted 10 lines · Srinivas Pandruvada

In some use cases, it is desirable to only inject idle on certain set
of CPUs. For example on Alder Lake systems, it is possible that we force
idle only on P-Cores for thermal reasons. Also the idle percent can be
more than 50% if we only choose partial set of CPUs in the system.

Introduce module parameters for setting cpumask and max_idle.

Quoted 8 lines

"Introduce 2 new module parameters for this purpose."

Quoted 4 lines

They can be only changed when the cooling device is inactive. This module
already have other module parameters. There is no change done for those parameters.

s/have/has/

OK

Quoted 82 lines

"... other parameters that are not affected by this change."

Quoted 79 lines

cpumask (Read/Write): A bit mask of CPUs to inject idle. The format of
this bitmask is same as used in other subsystems like in /proc/irq/*/smp_affinity. The mask is comma separated 32 bit groups.
Each CPU is one bit. For example for 256 CPU system the full mask is:
ffffffff,ffffffff,ffffffff,ffffffff,ffffffff,ffffffff,ffffffff,ffff ffff
The leftmost mask is for CPU 0-32.

max_idle (Read/Write): Maximum injected idle time to the total CPU time
ratio in percent range from 1 to 100. Even if the cooling device max_state
is always 100 (100%), this parameter allows to add a max idle percent
limit. The default is 50, to match the current implementation of powerclamp
driver. Also doesn't allow value more than 75, if the cpumask includes
every CPU present in the system.

Signed-off-by: Srinivas Pandruvada
<[email protected]>
---
v5
New patch

 .../driver-api/thermal/intel_powerclamp.rst   |  22 +++ drivers/thermal/intel/intel_powerclamp.c      | 169
++++++++++++++++--

 2 files changed, 173 insertions(+), 18 deletions(-)
Documentation/driver-api/thermal/intel_powerclamp.rst +2 -0
index 3f6dfb0b3ea6..d805e28b7a45 100644
@@ -26,6 +26,8 @@ By:
             - Generic Thermal Layer (sysfs)
             - Kernel APIs (TBD)
 
+       (*) Module Parameters
+

 INTRODUCTION
 ============

Documentation/driver-api/thermal/intel_powerclamp.rst +0 -0
@@ -318,3 +320,23 @@ device, a PID based userspace thermal

controller can manage to
 control CPU temperature effectively, when no other thermal influence
 is added. For example, a UltraBook user can compile the kernel under
 certain temperature (below most active trip points).

Documentation/driver-api/thermal/intel_powerclamp.rst +6 -0
+
+Module Parameters
+=================
+
+``cpumask`` (RW)
+       A bit mask of CPUs to inject idle. The format of the

bitmask is same as
+       used in other subsystems like in /proc/irq/*/smp_affinity. The mask is
+       comma separated 32 bit groups. Each CPU is one bit. For example for a 256
+       CPU system the full mask is:
+ ffffffff,ffffffff,ffffffff,ffffffff,ffffffff,ffffffff,ffffffff,ffff ffff

Documentation/driver-api/thermal/intel_powerclamp.rst +5 -0
+
+       The leftmost mask is for CPU 0-32.
+
+``max_idle`` (RW)
+       Maximum injected idle time to the total CPU time ratio in

percent range
+       from 1 to 100. Even if the cooling device max_state is always 100 (100%),
+       this parameter allows to add a max idle percent limit. The

Quoted 9 lines
Quoted 6 lines

default is 50,
+       to match the current implementation of powerclamp driver. Also doesn't
+       allow value more than 75, if the cpumask includes every CPU present in
+       the system.

I'm not sure if this is driver-api.  It's admin-guide rather IMO.

This whole file can be moved to admin_guide as there is no API.

Quoted 37 lines
Quoted 32 lines
drivers/thermal/intel/intel_powerclamp.c +1 -1
index 850195ebe5e0..68830b726da2 100644
@@ -37,7 +37,7 @@
  #include <asm/mwait.h>
  #include <asm/cpu_device_id.h>
 
-#define MAX_TARGET_RATIO (50U)
+#define MAX_TARGET_RATIO (100U)
  /* For each undisturbed clamping period (no extra wake ups during
 idle time),
   * we increment the confidence counter for the given target ratio.

  * CONFIDENCE_OK defines the level where runtime calibration results are

drivers/thermal/intel/intel_powerclamp.c +0 -0
@@ -109,6 +109,135 @@ static const struct kernel_param_ops
 duration_ops = {
  module_param_cb(duration, &duration_ops, &duration, 0644);

 MODULE_PARM_DESC(duration, "forced idle time for each attempt in msec.");

drivers/thermal/intel/intel_powerclamp.c +10 -0
+#define DEFAULT_MAX_IDLE       50
+#define MAX_ALL_CPU_IDLE       75
+
+static u8 max_idle = DEFAULT_MAX_IDLE;
+
+static cpumask_var_t idle_injection_cpu_mask;
+
+static int allocate_idle_injection_mask(void)
+{
+       /* This mask is allocated only one time and freed during
Quoted 2 lines

module exit */
+       if (!idle_injection_cpu_mask) {

I would do

Quoted 2 lines

if (idle_injection_cpu_mask)
         return 0;

OK

Quoted 30 lines

here and rearrange the rest of the function accordingly.

Quoted 27 lines

+               if (!zalloc_cpumask_var(&idle_injection_cpu_mask, GFP_KERNEL))
+                       return -ENOMEM;
+
+               cpumask_copy(idle_injection_cpu_mask, cpu_present_mask);
+       }

drivers/thermal/intel/intel_powerclamp.c +5 -0
+
+       return 0;
+}
+
+static int cpumask_set(const char *arg, const struct kernel_param

*kp)

drivers/thermal/intel/intel_powerclamp.c +13 -0
+{
+       int ret;
+
+       mutex_lock(&powerclamp_lock);
+
+       /* Can't set mask when cooling device is in use */
+       if (powerclamp_data.clamping) {
+               ret = -EAGAIN;
+               goto skip_cpumask_set;
+       }
+
+       /*
+        * When module parameters are passed from kernel command

line

Quoted 9 lines
Quoted 5 lines

+        * during insmod, the module parameter callback is called +        * before powerclamp_init(), so we can't assume that some +        * cpumask can be allocated before here.
+        */
+       ret = allocate_idle_injection_mask();

Could it be allocated by powerclamp_init(), though?  It is not useful before that function runs anyway.

It is required when passed as module param. For example:

#insmod drivers/thermal/intel/intel_powerclamp.ko cpumask=f

If you return error, module will not be loaded at all.

insmod: ERROR: could not insert
module drivers/thermal/intel/intel_powerclamp.ko: Invalid parameters

You have to return success then module parameters will be silently ignored.

Quoted 8 lines
Quoted 5 lines

+       if (ret)
+               goto skip_cpumask_set;
+
+       ret = bitmap_parse(arg, strlen(arg),
cpumask_bits(idle_injection_cpu_mask),

So this would replace the existing idle_injection_cpu_mask even if it is going to fail later.

Yes, when user sets 0s in mask. Otherwise I have to keep a separate cpumask and then copy. The size of cpumask type is 1024 bytes.

I will fix this.

Quoted 15 lines
Quoted 11 lines

+                          nr_cpumask_bits);
+       if (ret)
+               goto skip_cpumask_set;

drivers/thermal/intel/intel_powerclamp.c +7 -0
+
+       if (cpumask_empty(idle_injection_cpu_mask)) {
+               ret = -EINVAL;
+               goto skip_cpumask_set;
+       }
+
+       if (cpumask_equal(cpu_present_mask,
Quoted 1 line

idle_injection_cpu_mask) &&

Should this check be against cpu_online_mask instead?  Arguably offline CPUs don't matter here.

It is present mask which takes care of case when you started idle injection on CPUS when some CPUs were offline and then online while the session is going on. The idle-inject core takes care of online during session. Othewise online CPUs don't get idle injection and will run at 100%.

Quoted 14 lines
Quoted 14 lines

+                         max_idle > MAX_ALL_CPU_IDLE) { +               ret = -EINVAL;
+               goto skip_cpumask_set;
+       }

drivers/thermal/intel/intel_powerclamp.c +9 -0
+
+       mutex_unlock(&powerclamp_lock);
+
+       return 0;
+
+skip_cpumask_set:
+       mutex_unlock(&powerclamp_lock);
+
+       return ret;
Quoted 8 lines

drivers/thermal/intel/intel_powerclamp.c static const struct kernel_param_ops duration_ops = { · line 183

Quoted 6 lines
drivers/thermal/intel/intel_powerclamp.c +6 -0
+}
+
+static int cpumask_get(char *buf, const struct kernel_param *kp)
+{
+       if (!idle_injection_cpu_mask)
+               return -EINVAL;

I would return -ENODEV here.

OK

Quoted 14 lines
Quoted 14 lines

+
+       return bitmap_print_to_pagebuf(false, buf, cpumask_bits(idle_injection_cpu_mask),
+                                      nr_cpumask_bits);

drivers/thermal/intel/intel_powerclamp.c +9 -0
+}
+
+static const struct kernel_param_ops cpumask_ops = {
+       .set = cpumask_set,
+       .get = cpumask_get,
+};
+
+module_param_cb(cpumask, &cpumask_ops, NULL, 0644);
+MODULE_PARM_DESC(cpumask, "Mask of CPUs to use for idle
Quoted 8 lines
Quoted 6 lines

injection.");

drivers/thermal/intel/intel_powerclamp.c +2 -0
+
+static int max_idle_set(const char *arg, const struct kernel_param

*kp)
+{
+       u8 _max_idle;

new_max_idle would be slightly better I think.

OK

Quoted 19 lines
Quoted 19 lines

+       int ret = 0;

drivers/thermal/intel/intel_powerclamp.c +17 -0
+
+       mutex_lock(&powerclamp_lock);
+
+       /* Can't set mask when cooling device is in use */
+       if (powerclamp_data.clamping) {
+               ret = -EAGAIN;
+               goto skip_limit_set;
+       }
+
+       ret = kstrtou8(arg, 10, &_max_idle);
+       if (ret)
+               goto skip_limit_set;
+
+       if (_max_idle > MAX_TARGET_RATIO) {
+               ret = -EINVAL;
+               goto skip_limit_set;
+       }
Quoted 8 lines
Quoted 4 lines

+
+       if (idle_injection_cpu_mask && cpumask_equal(cpu_present_mask, idle_injection_cpu_mask) && +           _max_idle > MAX_ALL_CPU_IDLE) {

The same check is done here and in cpumask_set().  Could it be done in
a separate function called from here and from there?

OK.

Quoted 73 lines
Quoted 73 lines

+               ret = -EINVAL;
+               goto skip_limit_set;
+       }

drivers/thermal/intel/intel_powerclamp.c +16 -0
+
+       max_idle = _max_idle;
+
+skip_limit_set:
+       mutex_unlock(&powerclamp_lock);
+
+       return ret;
+}
+
+static const struct kernel_param_ops max_idle_ops = {
+       .set = max_idle_set,
+       .get = param_get_int,
+};
+
+module_param_cb(max_idle, &max_idle_ops, &max_idle, 0644);
+MODULE_PARM_DESC(max_idle, "maximum injected idle time to the

total CPU time ratio in percent range:1-100");
+
 struct powerclamp_calibration_data {
        unsigned long confidence;  /* used for calibration, basically a counter
                                    * gets incremented each time a clamping

drivers/thermal/intel/intel_powerclamp.c +4 -0
@@ -342,6 +471,10 @@ static unsigned int get_run_time(void)
         unsigned int compensated_ratio;
         unsigned int runtime;
 
+       /* No compensation for non systemwide idle injection */
+       if (max_idle > MAX_ALL_CPU_IDLE)
+               return (duration * 100 /
 powerclamp_data.target_ratio - duration);
+
         /*
          * make sure user selected ratio does not take effect until

         * the next round. adjust target_ratio if user has changed

drivers/thermal/intel/intel_powerclamp.c +2 -15
@@ -460,21 +593,11 @@ static void trigger_idle_injection(void)
   */
  static int powerclamp_idle_injection_register(void)
  {
-       /*
-        * The idle inject core will only inject for online CPUs,
-        * So we can register for all present CPUs. In this way
-        * if some CPU goes online/offline while idle inject
-        * is registered, nothing additional calls are required.
-        * The same runtime and idle time is applicable for
-        * newly onlined CPUs if any.
-        *
-        * Here cpu_present_mask can be used as is.
-        * cast to (struct cpumask *) is required as the
-        * cpu_present_mask is const struct cpumask *, otherwise
-        * there will be compiler warnings.
-        */
-       ii_dev = idle_inject_register_full((struct cpumask
 *)cpu_present_mask,
-                                          idle_inject_update);
+       if (cpumask_equal(cpu_present_mask,
 idle_injection_cpu_mask))
+               ii_dev =
 idle_inject_register_full(idle_injection_cpu_mask,

idle_inject_update);
+       else
+               ii_dev = idle_inject_register(idle_injection_cpu_mask); +
        if (!ii_dev) {
                pr_err("powerclamp: idle_inject_register failed\n");
                return -EAGAIN;

drivers/thermal/intel/intel_powerclamp.c +0 -0
@@ -510,7 +633,7 @@ static int start_power_clamp(void)
Quoted 8 lines

drivers/thermal/intel/intel_powerclamp.c static int start_power_clamp(void)

Quoted 6 lines
drivers/thermal/intel/intel_powerclamp.c +1 -1
         ret = powerclamp_idle_injection_register();
         if (!ret) {
                 trigger_idle_injection();
-               if (poll_pkg_cstate_enable)
+               if (poll_pkg_cstate_enable && max_idle <
 MAX_ALL_CPU_IDLE)

Why is the additional check needed here?

poll_pkg_cstate_enable is true for single package system, the additional check prevents starting package c-state polling thread. But poll_pkg_cstate_enable can be set to false to avoid this check.

Thanks,
Srinivas

Quoted 42 lines
Quoted 42 lines


schedule_delayed_work(&poll_pkg_cstate_work, 0);
        }

drivers/thermal/intel/intel_powerclamp.c +1 -1
@@ -565,7 +688,7 @@ static int powerclamp_set_cur_state(struct
 thermal_cooling_device *cdev,
         mutex_lock(&powerclamp_lock);
 
         new_target_ratio = clamp(new_target_ratio, 0UL,
-                               (unsigned long) (MAX_TARGET_RATIO -
 1));
+                               (unsigned long) (max_idle - 1));
         if (!powerclamp_data.target_ratio && new_target_ratio > 0)

{
                pr_info("Start idle injection to reduce power\n"); powerclamp_data.target_ratio = new_target_ratio;

drivers/thermal/intel/intel_powerclamp.c +10 -1
@@ -656,6 +779,13 @@ static int __init powerclamp_init(void)
 
         /* probe cpu features and ids here */
         retval = powerclamp_probe();
+       if (retval)
+               return retval;
+
+       mutex_lock(&powerclamp_lock);
+       retval = allocate_idle_injection_mask();
+       mutex_unlock(&powerclamp_lock);
+
         if (retval)
                 return retval;
 
@@ -689,6 +819,9 @@ static void __exit powerclamp_exit(void)
 
         cancel_delayed_work_sync(&poll_pkg_cstate_work);
         debugfs_remove_recursive(debug_dir);
+
+       if (idle_injection_cpu_mask)
+               free_cpumask_var(idle_injection_cpu_mask);
  }
  module_exit(powerclamp_exit);
 
--
reviewer
#8 lore raw

Re: [PATCH v5 4/4] thermal/drivers/intel_powerclamp: Add additional module params

In reply to Srinivas Pandruvada

Hi, Srinivas,

Quoted 109 lines · Srinivas Pandruvada

In some use cases, it is desirable to only inject idle on certain set of CPUs. For example on Alder Lake systems, it is possible that we force
idle only on P-Cores for thermal reasons. Also the idle percent can be
more than 50% if we only choose partial set of CPUs in the system.

Introduce module parameters for setting cpumask and max_idle. They can be only changed when the cooling device is inactive. This module already have other module parameters. There is no change done for those parameters.

cpumask (Read/Write): A bit mask of CPUs to inject idle. The format of
this bitmask is same as used in other subsystems like in /proc/irq/*/smp_affinity. The mask is comma separated 32 bit groups. Each CPU is one bit. For example for 256 CPU system the full mask is:
ffffffff,ffffffff,ffffffff,ffffffff,ffffffff,ffffffff,ffffffff,ffffff ff
The leftmost mask is for CPU 0-32.

max_idle (Read/Write): Maximum injected idle time to the total CPU time
ratio in percent range from 1 to 100. Even if the cooling device max_state
is always 100 (100%), this parameter allows to add a max idle percent limit. The default is 50, to match the current implementation of powerclamp
driver. Also doesn't allow value more than 75, if the cpumask includes
every CPU present in the system.

Signed-off-by: Srinivas Pandruvada <
[email protected]>
---
v5
New patch

 .../driver-api/thermal/intel_powerclamp.rst   |  22 +++
 drivers/thermal/intel/intel_powerclamp.c      | 169
++++++++++++++++--
 2 files changed, 173 insertions(+), 18 deletions(-)
Documentation/driver-api/thermal/intel_powerclamp.rst +2 -0
index 3f6dfb0b3ea6..d805e28b7a45 100644
@@ -26,6 +26,8 @@ By:
 	    - Generic Thermal Layer (sysfs)
 	    - Kernel APIs (TBD)
 
+	(*) Module Parameters
+
 INTRODUCTION
 ============
 
@@ -318,3 +320,23 @@ device, a PID based userspace thermal controller

can manage to
control CPU temperature effectively, when no other thermal influence is added. For example, a UltraBook user can compile the kernel under certain temperature (below most active trip points).

Documentation/driver-api/thermal/intel_powerclamp.rst +6 -0
+
+Module Parameters
+=================
+
+``cpumask`` (RW)
+	A bit mask of CPUs to inject idle. The format of the bitmask is

same as

Documentation/driver-api/thermal/intel_powerclamp.rst +1 -0
+	used in other subsystems like in /proc/irq/*/smp_affinity. The

mask is

Documentation/driver-api/thermal/intel_powerclamp.rst +1 -0
+	comma separated 32 bit groups. Each CPU is one bit. For example

for a 256

Documentation/driver-api/thermal/intel_powerclamp.rst +2 -0
+	CPU system the full mask is:
+	ffffffff,ffffffff,ffffffff,ffffffff,ffffffff,ffffffff,ffffffff,

ffffffff

Documentation/driver-api/thermal/intel_powerclamp.rst +5 -0
+
+	The leftmost mask is for CPU 0-32.
+
+``max_idle`` (RW)
+	Maximum injected idle time to the total CPU time ratio in

percent range

Documentation/driver-api/thermal/intel_powerclamp.rst +1 -0
+	from 1 to 100. Even if the cooling device max_state is always

100 (100%),

Documentation/driver-api/thermal/intel_powerclamp.rst +1 -0
+	this parameter allows to add a max idle percent	limit. The

default is 50,

Documentation/driver-api/thermal/intel_powerclamp.rst +1 -0
+	to match the current implementation of powerclamp driver. Also

doesn't

Documentation/driver-api/thermal/intel_powerclamp.rst +1 -0
+	allow value more than 75, if the cpumask includes every CPU

present in

Documentation/driver-api/thermal/intel_powerclamp.rst +1 -0
+	the system.
drivers/thermal/intel/intel_powerclamp.c +1 -1
index 850195ebe5e0..68830b726da2 100644
@@ -37,7 +37,7 @@
 #include <asm/mwait.h>
 #include <asm/cpu_device_id.h>
 
-#define MAX_TARGET_RATIO (50U)
+#define MAX_TARGET_RATIO (100U)
 /* For each undisturbed clamping period (no extra wake ups during
 idle time),
  * we increment the confidence counter for the given target ratio.

* CONFIDENCE_OK defines the level where runtime calibration results
are

drivers/thermal/intel/intel_powerclamp.c +0 -0
@@ -109,6 +109,135 @@ static const struct kernel_param_ops
 duration_ops = {

drivers/thermal/intel/intel_powerclamp.c static const struct kernel_param_ops · line 119

Quoted 10 lines
drivers/thermal/intel/intel_powerclamp.c +6 -0
 module_param_cb(duration, &duration_ops, &duration, 0644);
 MODULE_PARM_DESC(duration, "forced idle time for each attempt in
 msec.");
 
+#define DEFAULT_MAX_IDLE	50
+#define MAX_ALL_CPU_IDLE	75
+
+static u8 max_idle = DEFAULT_MAX_IDLE;
+
+static cpumask_var_t idle_injection_cpu_mask;

#ifdef CONFIG_CPUMASK_OFFSTACK
typedef struct cpumask *cpumask_var_t;
#else
typedef struct cpumask cpumask_var_t[1];
fi

I happened to build with CONFIG_CPUMASK_OFFSTACK cleared, and got following errors

$ make
  CALL    scripts/checksyscalls.sh
  DESCEND objtool
  AR      drivers/thermal/intel/built-in.a
  CC [M]  drivers/thermal/intel/intel_powerclamp.o
drivers/thermal/intel/intel_powerclamp.c: In function ‘allocate_idle_injection_mask’:
drivers/thermal/intel/intel_powerclamp.c:122:6: error: the address of ‘idle_injection_cpu_mask’ will always evaluate as ‘true’ [-Werror=address]
  122 |  if (!idle_injection_cpu_mask) {
      |      ^
drivers/thermal/intel/intel_powerclamp.c: In function ‘cpumask_get’:
drivers/thermal/intel/intel_powerclamp.c:183:6: error: the address of ‘idle_injection_cpu_mask’ will always evaluate as ‘true’ [-Werror=address]
  183 |  if (!idle_injection_cpu_mask)
      |      ^
drivers/thermal/intel/intel_powerclamp.c: In function ‘max_idle_set’:
drivers/thermal/intel/intel_powerclamp.c:220:6: error: the address of ‘idle_injection_cpu_mask’ will always evaluate as ‘true’ [-Werror=address]
  220 |  if (idle_injection_cpu_mask && cpumask_equal(cpu_present_mask, idle_injection_cpu_mask) &&
      |      ^~~~~~~~~~~~~~~~~~~~~~~
drivers/thermal/intel/intel_powerclamp.c: In function ‘powerclamp_exit’:
drivers/thermal/intel/intel_powerclamp.c:825:6: error: the address of ‘idle_injection_cpu_mask’ will always evaluate as ‘true’ [-Werror=address]
  825 |  if (idle_injection_cpu_mask)
      |      ^~~~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors
make[4]: *** [scripts/Makefile.build:252: drivers/thermal/intel/intel_powerclamp.o] Error 1
make[3]: *** [scripts/Makefile.build:504: drivers/thermal/intel] Error 2
make[2]: *** [scripts/Makefile.build:504: drivers/thermal] Error 2
make[1]: *** [scripts/Makefile.build:504: drivers] Error 2
make: *** [Makefile:2021: .] Error 2
Quoted 30 lines
drivers/thermal/intel/intel_powerclamp.c +4 -0
+
+static int allocate_idle_injection_mask(void)
+{
+	/* This mask is allocated only one time and freed during module

exit */

drivers/thermal/intel/intel_powerclamp.c +2 -0
+	if (!idle_injection_cpu_mask) {
+		if (!zalloc_cpumask_var(&idle_injection_cpu_mask,

GFP_KERNEL))

drivers/thermal/intel/intel_powerclamp.c +3 -0
+			return -ENOMEM;
+
+		cpumask_copy(idle_injection_cpu_mask,

cpu_present_mask);

drivers/thermal/intel/intel_powerclamp.c +6 -0
+	}
+
+	return 0;
+}
+
+static int cpumask_set(const char *arg, const struct kernel_param

*kp)

drivers/thermal/intel/intel_powerclamp.c +11 -0
+{
+	int ret;
+
+	mutex_lock(&powerclamp_lock);
+
+	/* Can't set mask when cooling device is in use */
+	if (powerclamp_data.clamping) {
+		ret = -EAGAIN;
+		goto skip_cpumask_set;
+	}
+

drivers/thermal/intel/intel_powerclamp.c static const struct kernel_param_ops duration_ops = { · line 153

Quoted 10 lines
drivers/thermal/intel/intel_powerclamp.c +10 -0
+	/*
+	 * When module parameters are passed from kernel command line
+	 * during insmod, the module parameter callback is called
+	 * before powerclamp_init(), so we can't assume that some
+	 * cpumask can be allocated before here.
+	 */
+	ret = allocate_idle_injection_mask();
+	if (ret)
+		goto skip_cpumask_set;
+

Just a suggestion, can we have a quick path for restoring to all cpu mode?

Say, echo > /sys/module/intel_powerclamp/parameters/cpumask

Quoted 99 lines
drivers/thermal/intel/intel_powerclamp.c +1 -0
+	ret = bitmap_parse(arg, strlen(arg),

cpumask_bits(idle_injection_cpu_mask),

drivers/thermal/intel/intel_powerclamp.c +31 -0
+			   nr_cpumask_bits);
+	if (ret)
+		goto skip_cpumask_set;
+
+	if (cpumask_empty(idle_injection_cpu_mask)) {
+		ret = -EINVAL;
+		goto skip_cpumask_set;
+	}
+
+	if (cpumask_equal(cpu_present_mask, idle_injection_cpu_mask) &&
+			  max_idle > MAX_ALL_CPU_IDLE) {
+		ret = -EINVAL;
+		goto skip_cpumask_set;
+	}
+
+	mutex_unlock(&powerclamp_lock);
+
+	return 0;
+
+skip_cpumask_set:
+	mutex_unlock(&powerclamp_lock);
+
+	return ret;
+}
+
+static int cpumask_get(char *buf, const struct kernel_param *kp)
+{
+	if (!idle_injection_cpu_mask)
+		return -EINVAL;
+
+	return bitmap_print_to_pagebuf(false, buf,

cpumask_bits(idle_injection_cpu_mask),

drivers/thermal/intel/intel_powerclamp.c +10 -0
+				       nr_cpumask_bits);
+}
+
+static const struct kernel_param_ops cpumask_ops = {
+	.set = cpumask_set,
+	.get = cpumask_get,
+};
+
+module_param_cb(cpumask, &cpumask_ops, NULL, 0644);
+MODULE_PARM_DESC(cpumask, "Mask of CPUs to use for idle

injection.");

drivers/thermal/intel/intel_powerclamp.c +2 -0
+
+static int max_idle_set(const char *arg, const struct kernel_param

*kp)

drivers/thermal/intel/intel_powerclamp.c +22 -0
+{
+	u8 _max_idle;
+	int ret = 0;
+
+	mutex_lock(&powerclamp_lock);
+
+	/* Can't set mask when cooling device is in use */
+	if (powerclamp_data.clamping) {
+		ret = -EAGAIN;
+		goto skip_limit_set;
+	}
+
+	ret = kstrtou8(arg, 10, &_max_idle);
+	if (ret)
+		goto skip_limit_set;
+
+	if (_max_idle > MAX_TARGET_RATIO) {
+		ret = -EINVAL;
+		goto skip_limit_set;
+	}
+
+	if (idle_injection_cpu_mask && cpumask_equal(cpu_present_mask,

idle_injection_cpu_mask) &&

drivers/thermal/intel/intel_powerclamp.c +20 -0
+	    _max_idle > MAX_ALL_CPU_IDLE) {
+		ret = -EINVAL;
+		goto skip_limit_set;
+	}
+
+	max_idle = _max_idle;
+
+skip_limit_set:
+	mutex_unlock(&powerclamp_lock);
+
+	return ret;
+}
+
+static const struct kernel_param_ops max_idle_ops = {
+	.set = max_idle_set,
+	.get = param_get_int,
+};
+
+module_param_cb(max_idle, &max_idle_ops, &max_idle, 0644);
+MODULE_PARM_DESC(max_idle, "maximum injected idle time to the total

CPU time ratio in percent range:1-100");

drivers/thermal/intel/intel_powerclamp.c +1 -0
+
 struct powerclamp_calibration_data {
 	unsigned long confidence;  /* used for calibration, basically a

counter
* gets incremented each time a
clamping

drivers/thermal/intel/intel_powerclamp.c static unsigned int get_run_time(void) · line 477

Quoted 7 lines
drivers/thermal/intel/intel_powerclamp.c +3 -0
@@ -342,6 +471,10 @@ static unsigned int get_run_time(void)
 	unsigned int compensated_ratio;
 	unsigned int runtime;
 
+	/* No compensation for non systemwide idle injection */
+	if (max_idle > MAX_ALL_CPU_IDLE)
+		return (duration * 100 / powerclamp_data.target_ratio -
 duration);

The comment and the code is not aligned.
we can still set max_idle to a value lower than MAX_ALL_CPU_IDLE for non systemwide idle injection, right?

thanks,
rui

Quoted 80 lines
drivers/thermal/intel/intel_powerclamp.c +1 -0
+
 	/*
 	 * make sure user selected ratio does not take effect until
drivers/thermal/intel/intel_powerclamp.c +5 -15
 	 * the next round. adjust target_ratio if user has changed
@@ -460,21 +593,11 @@ static void trigger_idle_injection(void)
  */
 static int powerclamp_idle_injection_register(void)
 {
-	/*
-	 * The idle inject core will only inject for online CPUs,
-	 * So we can register for all present CPUs. In this way
-	 * if some CPU goes online/offline while idle inject
-	 * is registered, nothing additional calls are required.
-	 * The same runtime and idle time is applicable for
-	 * newly onlined CPUs if any.
-	 *
-	 * Here cpu_present_mask can be used as is.
-	 * cast to (struct cpumask *) is required as the
-	 * cpu_present_mask is const struct cpumask *, otherwise
-	 * there will be compiler warnings.
-	 */
-	ii_dev = idle_inject_register_full((struct cpumask
 *)cpu_present_mask,
-					   idle_inject_update);
+	if (cpumask_equal(cpu_present_mask, idle_injection_cpu_mask))
+		ii_dev =
 idle_inject_register_full(idle_injection_cpu_mask,
 idle_inject_update);
+	else
+		ii_dev = idle_inject_register(idle_injection_cpu_mask);
+
drivers/thermal/intel/intel_powerclamp.c +1 -1
 	if (!ii_dev) {
 		pr_err("powerclamp: idle_inject_register failed\n");
 		return -EAGAIN;
@@ -510,7 +633,7 @@ static int start_power_clamp(void)
 	ret = powerclamp_idle_injection_register();
 	if (!ret) {
 		trigger_idle_injection();
-		if (poll_pkg_cstate_enable)
+		if (poll_pkg_cstate_enable && max_idle <
 MAX_ALL_CPU_IDLE)
 			schedule_delayed_work(&poll_pkg_cstate_work,
 0);
drivers/thermal/intel/intel_powerclamp.c +1 -1
 	}
@@ -565,7 +688,7 @@ static int powerclamp_set_cur_state(struct
 thermal_cooling_device *cdev,
 	mutex_lock(&powerclamp_lock);
 
 	new_target_ratio = clamp(new_target_ratio, 0UL,
-				(unsigned long) (MAX_TARGET_RATIO -
 1));
+				(unsigned long) (max_idle - 1));
 	if (!powerclamp_data.target_ratio && new_target_ratio > 0) {
drivers/thermal/intel/intel_powerclamp.c +10 -0
 		pr_info("Start idle injection to reduce power\n");
 		powerclamp_data.target_ratio = new_target_ratio;
@@ -656,6 +779,13 @@ static int __init powerclamp_init(void)
 
 	/* probe cpu features and ids here */
 	retval = powerclamp_probe();
+	if (retval)
+		return retval;
+
+	mutex_lock(&powerclamp_lock);
+	retval = allocate_idle_injection_mask();
+	mutex_unlock(&powerclamp_lock);
+
 	if (retval)
 		return retval;
 
@@ -689,6 +819,9 @@ static void __exit powerclamp_exit(void)
 
 	cancel_delayed_work_sync(&poll_pkg_cstate_work);
 	debugfs_remove_recursive(debug_dir);
+
+	if (idle_injection_cpu_mask)
+		free_cpumask_var(idle_injection_cpu_mask);
 }
 module_exit(powerclamp_exit);
 
#9 lore raw

Re: [PATCH v5 4/4] thermal/drivers/intel_powerclamp: Add additional module params

Hi Rui,

Quoted 1 line

[...]

Quoted 10 lines

#ifdef CONFIG_CPUMASK_OFFSTACK
typedef struct cpumask *cpumask_var_t;
#else
typedef struct cpumask cpumask_var_t[1];
fi

I happened to build with CONFIG_CPUMASK_OFFSTACK cleared, and got following errors

Try v2 for the module parameters

Thanks,
Srinivas

Quoted 293 lines
$ make
  CALL    scripts/checksyscalls.sh
  DESCEND objtool
  AR      drivers/thermal/intel/built-in.a
  CC [M]  drivers/thermal/intel/intel_powerclamp.o
drivers/thermal/intel/intel_powerclamp.c: In function
‘allocate_idle_injection_mask’:
drivers/thermal/intel/intel_powerclamp.c:122:6: error: the address of
‘idle_injection_cpu_mask’ will always evaluate as ‘true’ [-
Werror=address]
  122 |  if (!idle_injection_cpu_mask) {
      |      ^
drivers/thermal/intel/intel_powerclamp.c: In function ‘cpumask_get’:
drivers/thermal/intel/intel_powerclamp.c:183:6: error: the address of
‘idle_injection_cpu_mask’ will always evaluate as ‘true’ [-
Werror=address]
  183 |  if (!idle_injection_cpu_mask)
      |      ^
drivers/thermal/intel/intel_powerclamp.c: In function ‘max_idle_set’:
drivers/thermal/intel/intel_powerclamp.c:220:6: error: the address of
‘idle_injection_cpu_mask’ will always evaluate as ‘true’ [-
Werror=address]
  220 |  if (idle_injection_cpu_mask &&
cpumask_equal(cpu_present_mask, idle_injection_cpu_mask) &&
      |      ^~~~~~~~~~~~~~~~~~~~~~~
drivers/thermal/intel/intel_powerclamp.c: In function
‘powerclamp_exit’:
drivers/thermal/intel/intel_powerclamp.c:825:6: error: the address of
‘idle_injection_cpu_mask’ will always evaluate as ‘true’ [-
Werror=address]
  825 |  if (idle_injection_cpu_mask)
      |      ^~~~~~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors
make[4]: *** [scripts/Makefile.build:252:
drivers/thermal/intel/intel_powerclamp.o] Error 1
make[3]: *** [scripts/Makefile.build:504: drivers/thermal/intel]
Error 2
make[2]: *** [scripts/Makefile.build:504: drivers/thermal] Error 2
make[1]: *** [scripts/Makefile.build:504: drivers] Error 2
make: *** [Makefile:2021: .] Error 2
Quoted 34 lines
drivers/thermal/intel/intel_powerclamp.c +4 -0
+
+static int allocate_idle_injection_mask(void)
+{
+       /* This mask is allocated only one time and freed during

module
exit */
+       if (!idle_injection_cpu_mask) {
+               if (!zalloc_cpumask_var(&idle_injection_cpu_mask, GFP_KERNEL))
+                       return -ENOMEM;
+
+               cpumask_copy(idle_injection_cpu_mask, cpu_present_mask);
+       }

drivers/thermal/intel/intel_powerclamp.c +5 -0
+
+       return 0;
+}
+
+static int cpumask_set(const char *arg, const struct kernel_param

*kp)

drivers/thermal/intel/intel_powerclamp.c +13 -0
+{
+       int ret;
+
+       mutex_lock(&powerclamp_lock);
+
+       /* Can't set mask when cooling device is in use */
+       if (powerclamp_data.clamping) {
+               ret = -EAGAIN;
+               goto skip_cpumask_set;
+       }
+
+       /*
+        * When module parameters are passed from kernel command

line

Quoted 8 lines

+        * during insmod, the module parameter callback is called +        * before powerclamp_init(), so we can't assume that some +        * cpumask can be allocated before here.
+        */
+       ret = allocate_idle_injection_mask();
+       if (ret)
+               goto skip_cpumask_set;
+

Just a suggestion, can we have a quick path for restoring to all cpu mode?

Say, echo > /sys/module/intel_powerclamp/parameters/cpumask

Quoted 105 lines

+       ret = bitmap_parse(arg, strlen(arg),
cpumask_bits(idle_injection_cpu_mask),
+                          nr_cpumask_bits);
+       if (ret)
+               goto skip_cpumask_set;

drivers/thermal/intel/intel_powerclamp.c +7 -0
+
+       if (cpumask_empty(idle_injection_cpu_mask)) {
+               ret = -EINVAL;
+               goto skip_cpumask_set;
+       }
+
+       if (cpumask_equal(cpu_present_mask,

idle_injection_cpu_mask) &&
+                         max_idle > MAX_ALL_CPU_IDLE) { +               ret = -EINVAL;
+               goto skip_cpumask_set;
+       }

drivers/thermal/intel/intel_powerclamp.c +17 -0
+
+       mutex_unlock(&powerclamp_lock);
+
+       return 0;
+
+skip_cpumask_set:
+       mutex_unlock(&powerclamp_lock);
+
+       return ret;
+}
+
+static int cpumask_get(char *buf, const struct kernel_param *kp)
+{
+       if (!idle_injection_cpu_mask)
+               return -EINVAL;
+
+       return bitmap_print_to_pagebuf(false, buf,

cpumask_bits(idle_injection_cpu_mask),
+                                      nr_cpumask_bits);

drivers/thermal/intel/intel_powerclamp.c +9 -0
+}
+
+static const struct kernel_param_ops cpumask_ops = {
+       .set = cpumask_set,
+       .get = cpumask_get,
+};
+
+module_param_cb(cpumask, &cpumask_ops, NULL, 0644);
+MODULE_PARM_DESC(cpumask, "Mask of CPUs to use for idle

injection.");

(quoted diff) +2 -0
+
+static int max_idle_set(const char *arg, const struct kernel_param

*kp)

drivers/thermal/intel/intel_powerclamp.c +22 -0
+{
+       u8 _max_idle;
+       int ret = 0;
+
+       mutex_lock(&powerclamp_lock);
+
+       /* Can't set mask when cooling device is in use */
+       if (powerclamp_data.clamping) {
+               ret = -EAGAIN;
+               goto skip_limit_set;
+       }
+
+       ret = kstrtou8(arg, 10, &_max_idle);
+       if (ret)
+               goto skip_limit_set;
+
+       if (_max_idle > MAX_TARGET_RATIO) {
+               ret = -EINVAL;
+               goto skip_limit_set;
+       }
+
+       if (idle_injection_cpu_mask &&

cpumask_equal(cpu_present_mask,
idle_injection_cpu_mask) &&
+           _max_idle > MAX_ALL_CPU_IDLE) {
+               ret = -EINVAL;
+               goto skip_limit_set;
+       }

drivers/thermal/intel/intel_powerclamp.c +16 -0
+
+       max_idle = _max_idle;
+
+skip_limit_set:
+       mutex_unlock(&powerclamp_lock);
+
+       return ret;
+}
+
+static const struct kernel_param_ops max_idle_ops = {
+       .set = max_idle_set,
+       .get = param_get_int,
+};
+
+module_param_cb(max_idle, &max_idle_ops, &max_idle, 0644);
+MODULE_PARM_DESC(max_idle, "maximum injected idle time to the

total
CPU time ratio in percent range:1-100");
+
 struct powerclamp_calibration_data {
        unsigned long confidence;  /* used for calibration, basically a
counter
                                    * gets incremented each time a clamping

drivers/thermal/intel/intel_powerclamp.c +0 -0
@@ -342,6 +471,10 @@ static unsigned int get_run_time(void)
         unsigned int compensated_ratio;
         unsigned int runtime;
Quoted 6 lines


+       /* No compensation for non systemwide idle injection */ +       if (max_idle > MAX_ALL_CPU_IDLE)
+               return (duration * 100 / powerclamp_data.target_ratio -
duration);

The comment and the code is not aligned.
we can still set max_idle to a value lower than MAX_ALL_CPU_IDLE for non systemwide idle injection, right?

thanks,
rui

Quoted 85 lines

+
        /*
         * make sure user selected ratio does not take effect until
         * the next round. adjust target_ratio if user has changed

drivers/thermal/intel/intel_powerclamp.c +2 -15
@@ -460,21 +593,11 @@ static void trigger_idle_injection(void)
   */
  static int powerclamp_idle_injection_register(void)
  {
-       /*
-        * The idle inject core will only inject for online CPUs,
-        * So we can register for all present CPUs. In this way
-        * if some CPU goes online/offline while idle inject
-        * is registered, nothing additional calls are required.
-        * The same runtime and idle time is applicable for
-        * newly onlined CPUs if any.
-        *
-        * Here cpu_present_mask can be used as is.
-        * cast to (struct cpumask *) is required as the
-        * cpu_present_mask is const struct cpumask *, otherwise
-        * there will be compiler warnings.
-        */
-       ii_dev = idle_inject_register_full((struct cpumask
 *)cpu_present_mask,
-                                          idle_inject_update);
+       if (cpumask_equal(cpu_present_mask,
 idle_injection_cpu_mask))
+               ii_dev =
 idle_inject_register_full(idle_injection_cpu_mask,

idle_inject_update);
+       else
+               ii_dev = idle_inject_register(idle_injection_cpu_mask); +
        if (!ii_dev) {
                pr_err("powerclamp: idle_inject_register failed\n");
                return -EAGAIN;

drivers/thermal/intel/intel_powerclamp.c +1 -1
@@ -510,7 +633,7 @@ static int start_power_clamp(void)
         ret = powerclamp_idle_injection_register();
         if (!ret) {
                 trigger_idle_injection();
-               if (poll_pkg_cstate_enable)
+               if (poll_pkg_cstate_enable && max_idle <
 MAX_ALL_CPU_IDLE)

                        schedule_delayed_work(&poll_pkg_cstate_work ,
0);
        }

(quoted diff) +0 -0
@@ -565,7 +688,7 @@ static int powerclamp_set_cur_state(struct
 thermal_cooling_device *cdev,
         mutex_lock(&powerclamp_lock);


        new_target_ratio = clamp(new_target_ratio, 0UL, -                               (unsigned long) (MAX_TARGET_RATIO - 1));
+                               (unsigned long) (max_idle - 1)); if (!powerclamp_data.target_ratio && new_target_ratio > 0) {
                pr_info("Start idle injection to reduce power\n"); powerclamp_data.target_ratio = new_target_ratio;

(quoted diff) +0 -0
@@ -656,6 +779,13 @@ static int __init powerclamp_init(void)


        /* probe cpu features and ids here */
        retval = powerclamp_probe();
+       if (retval)
+               return retval;

drivers/thermal/intel/intel_powerclamp.c +5 -0
+
+       mutex_lock(&powerclamp_lock);
+       retval = allocate_idle_injection_mask();
+       mutex_unlock(&powerclamp_lock);
+

        if (retval)
                return retval;

(quoted diff) +0 -0
@@ -689,6 +819,9 @@ static void __exit powerclamp_exit(void)


        cancel_delayed_work_sync(&poll_pkg_cstate_work); debugfs_remove_recursive(debug_dir);
+
+       if (idle_injection_cpu_mask)
+               free_cpumask_var(idle_injection_cpu_mask); }
 module_exit(powerclamp_exit);

#10 lore raw
In reply to Srinivas Pandruvada
Quoted 11 lines · Srinivas Pandruvada

Dropped the per core idle injection patch from this series. Hence the subject of this series is changed. Now it is just for using
idle inject framework for intel_powerclamp.
Add additionl patch for module paramters to set cpumask and max idle percent.

The old cover letter:
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.

Quoted 5 lines

v5:
- additionl patch for module paramters to set cpumask and max idle percent
- One fix for update callback
- Rebased on top of patch
"thermal: intel_powerclamp: Fix cur_state for multi package system"

Patches [1-3/4] have been applied as 6.3 material and I'll comment patch [4/4] separately.

Quoted 29 lines

v4:
- Dropped the per core idle inject patch

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_powerclamp: Add additional module params

 .../driver-api/thermal/intel_powerclamp.rst   |  22 +
 drivers/powercap/idle_inject.c                |  57 +-
 drivers/thermal/intel/Kconfig                 |   2 +
 drivers/thermal/intel/intel_powerclamp.c      | 512 ++++++++++--------
 include/linux/idle_inject.h                   |   3 +
 5 files changed, 367 insertions(+), 229 deletions(-)

--
2.39.1