* [PATCH] thermal: intel: powerclamp: Use guard(mutex) to simplify the code
@ 2026-11-10 23:16 Thorsten Blum
2026-11-11 12:31 ` Thorsten Blum
0 siblings, 1 reply; 2+ messages in thread
From: Thorsten Blum @ 2026-11-10 23:16 UTC (permalink / raw)
To: Rafael J. Wysocki, Daniel Lezcano, Zhang Rui, Lukasz Luba,
Thorsten Blum, Yury Norov
Cc: Rafael J. Wysocki, linux-pm, linux-kernel
Use guard(mutex) to replace explicit mutex_{lock,unlock}() calls.
Simplify the return paths and drop redundant ret initializations in
duration_set(), window_size_set(), and powerclamp_set_cur_state().
Signed-off-by: Thorsten Blum <blum@kernel.org>
---
drivers/thermal/intel/intel_powerclamp.c | 71 ++++++++++--------------
1 file changed, 28 insertions(+), 43 deletions(-)
diff --git a/drivers/thermal/intel/intel_powerclamp.c b/drivers/thermal/intel/intel_powerclamp.c
index 1ffcb0a699e5..8461ba868248 100644
--- a/drivers/thermal/intel/intel_powerclamp.c
+++ b/drivers/thermal/intel/intel_powerclamp.c
@@ -80,25 +80,22 @@ static unsigned int window_size;
static int duration_set(const char *arg, const struct kernel_param *kp)
{
- int ret = 0;
+ int ret;
unsigned long new_duration;
ret = kstrtoul(arg, 10, &new_duration);
if (ret)
- goto exit;
+ return ret;
if (new_duration > 25 || new_duration < 6) {
pr_err("Out of recommended range %lu, between 6-25ms\n",
new_duration);
- ret = -EINVAL;
- goto exit;
+ return -EINVAL;
}
- mutex_lock(&powerclamp_lock);
+ guard(mutex)(&powerclamp_lock);
duration = new_duration * 1000;
- mutex_unlock(&powerclamp_lock);
-exit:
- return ret;
+ return 0;
}
static int duration_get(char *buf, const struct kernel_param *kp)
@@ -149,17 +146,14 @@ static int cpumask_set(const char *arg, const struct kernel_param *kp)
cpumask_var_t new_mask;
int ret;
- mutex_lock(&powerclamp_lock);
+ guard(mutex)(&powerclamp_lock);
/* Can't set mask when cooling device is in use */
- if (powerclamp_data.clamping) {
- ret = -EAGAIN;
- goto skip_cpumask_set;
- }
+ if (powerclamp_data.clamping)
+ return -EAGAIN;
- ret = alloc_cpumask_var(&new_mask, GFP_KERNEL);
- if (!ret)
- goto skip_cpumask_set;
+ if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
+ return 0;
ret = bitmap_parse(arg, strlen(arg), cpumask_bits(new_mask),
nr_cpumask_bits);
@@ -182,8 +176,6 @@ static int cpumask_set(const char *arg, const struct kernel_param *kp)
free_cpumask_set:
free_cpumask_var(new_mask);
-skip_cpumask_set:
- mutex_unlock(&powerclamp_lock);
return ret;
}
@@ -263,25 +255,22 @@ static struct powerclamp_calibration_data cal_data[MAX_TARGET_RATIO];
static int window_size_set(const char *arg, const struct kernel_param *kp)
{
- int ret = 0;
+ int ret;
unsigned long new_window_size;
ret = kstrtoul(arg, 10, &new_window_size);
if (ret)
- goto exit_win;
+ return ret;
if (new_window_size > 10 || new_window_size < 2) {
pr_err("Out of recommended window size %lu, between 2-10\n",
new_window_size);
- ret = -EINVAL;
- goto exit_win;
+ return -EINVAL;
}
window_size = new_window_size;
smp_mb();
-exit_win:
-
- return ret;
+ return 0;
}
static const struct kernel_param_ops window_size_ops = {
@@ -506,10 +495,9 @@ static void poll_pkg_cstate(struct work_struct *dummy)
msr_last = msr_now;
tsc_last = tsc_now;
- mutex_lock(&powerclamp_lock);
+ guard(mutex)(&powerclamp_lock);
if (powerclamp_data.clamping)
schedule_delayed_work(&poll_pkg_cstate_work, HZ);
- mutex_unlock(&powerclamp_lock);
}
static struct idle_inject_device *ii_dev;
@@ -633,9 +621,8 @@ static int powerclamp_get_max_state(struct thermal_cooling_device *cdev,
static int powerclamp_get_cur_state(struct thermal_cooling_device *cdev,
unsigned long *state)
{
- mutex_lock(&powerclamp_lock);
+ guard(mutex)(&powerclamp_lock);
*state = powerclamp_data.target_ratio;
- mutex_unlock(&powerclamp_lock);
return 0;
}
@@ -643,15 +630,15 @@ static int powerclamp_get_cur_state(struct thermal_cooling_device *cdev,
static int powerclamp_set_cur_state(struct thermal_cooling_device *cdev,
unsigned long new_target_ratio)
{
- int ret = 0;
+ int ret;
- mutex_lock(&powerclamp_lock);
+ guard(mutex)(&powerclamp_lock);
new_target_ratio = clamp(new_target_ratio, 0UL,
(unsigned long) (max_idle - 1));
if (powerclamp_data.target_ratio == new_target_ratio)
- goto exit_set;
+ return 0;
if (!powerclamp_data.target_ratio && new_target_ratio > 0) {
pr_info("Start idle injection to reduce power\n");
@@ -659,23 +646,21 @@ static int powerclamp_set_cur_state(struct thermal_cooling_device *cdev,
ret = start_power_clamp();
if (ret)
powerclamp_data.target_ratio = 0;
- goto exit_set;
- } else if (powerclamp_data.target_ratio > 0 && new_target_ratio == 0) {
+ return ret;
+ }
+
+ if (powerclamp_data.target_ratio > 0 && new_target_ratio == 0) {
pr_info("Stop forced idle injection\n");
end_power_clamp();
powerclamp_data.target_ratio = 0;
- } else /* adjust currently running */ {
- unsigned int runtime;
-
- powerclamp_data.target_ratio = new_target_ratio;
- runtime = get_run_time();
- idle_inject_set_duration(ii_dev, runtime, duration);
+ return 0;
}
-exit_set:
- mutex_unlock(&powerclamp_lock);
+ /* adjust currently running */
+ powerclamp_data.target_ratio = new_target_ratio;
+ idle_inject_set_duration(ii_dev, get_run_time(), duration);
- return ret;
+ return 0;
}
/* bind to generic thermal layer as cooling device*/
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH] thermal: intel: powerclamp: Use guard(mutex) to simplify the code
2026-11-10 23:16 [PATCH] thermal: intel: powerclamp: Use guard(mutex) to simplify the code Thorsten Blum
@ 2026-11-11 12:31 ` Thorsten Blum
0 siblings, 0 replies; 2+ messages in thread
From: Thorsten Blum @ 2026-11-11 12:31 UTC (permalink / raw)
To: Rafael J. Wysocki, Daniel Lezcano, Zhang Rui, Lukasz Luba, Yury Norov
Cc: Rafael J. Wysocki, linux-pm, linux-kernel
On Wed, Nov 11, 2026 at 12:16:08AM +0100, Thorsten Blum wrote:
> Use guard(mutex) to replace explicit mutex_{lock,unlock}() calls.
>
> Simplify the return paths and drop redundant ret initializations in
> duration_set(), window_size_set(), and powerclamp_set_cur_state().
>
> Signed-off-by: Thorsten Blum <blum@kernel.org>
> ---
> drivers/thermal/intel/intel_powerclamp.c | 71 ++++++++++--------------
> 1 file changed, 28 insertions(+), 43 deletions(-)
I'll send a v2 after taking Sashiko's feedback into account:
https://sashiko.dev/#/patchset/20261110231611.7159-2-blum%40kernel.org
And this patch should only be applied after the following fix:
https://lore.kernel.org/r/20261111121225.70218-3-blum@kernel.org/
Thanks,
Thorsten
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-11 12:31 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-10 23:16 [PATCH] thermal: intel: powerclamp: Use guard(mutex) to simplify the code Thorsten Blum
2026-11-11 12:31 ` Thorsten Blum
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®