From: Thorsten Blum <blum@kernel.org>
To: "Rafael J. Wysocki" <rafael@kernel.org>,
Daniel Lezcano <daniel.lezcano@kernel.org>,
Zhang Rui <rui.zhang@intel.com>,
Lukasz Luba <lukasz.luba@arm.com>,
Thorsten Blum <blum@kernel.org>, Yury Norov <ynorov@nvidia.com>
Cc: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com>,
linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] thermal: intel: powerclamp: Use guard(mutex) to simplify the code
Date: Wed, 11 Nov 2026 00:16:08 +0100 [thread overview]
Message-ID: <20261110231611.7159-2-blum@kernel.org> (raw)
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*/
reply other threads:[~2026-11-10 23:16 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261110231611.7159-2-blum@kernel.org \
--to=blum@kernel.org \
--cc=daniel.lezcano@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=lukasz.luba@arm.com \
--cc=rafael.j.wysocki@intel.com \
--cc=rafael@kernel.org \
--cc=rui.zhang@intel.com \
--cc=ynorov@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®