From nobody Sat Feb 7 17:40:59 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E3CBD332ED7; Fri, 7 Nov 2025 18:18:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762539510; cv=none; b=RZ+wXpu7IVumRs9W9K+eMgYchFShr5wLYvjXTp/ecDJGePHCKCYKVyHmY4UpcXLu/bOaoNyfTQN94apn3cEDBIe4AMiUhAe8DxkYyOlQNVnaErvbJBdhtDMXQ7gdBDsqnkJRaQt4nO8btvO8mIOWc3eFMmNm3hxKU3j13KOe3vI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762539510; c=relaxed/simple; bh=xQVzgD7GVp4/BOHYm5HSOOyNHFn2GczjN+HJaZNJ9rQ=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=dRACWw0MQU3q4uR8A+FJzvswaekEcZ+iCMPFI7tG7+ydQGidNwRL7e8XO9Xmh/OO4oZ8CK3FhaBLKz0fYS+QyFdOUuKpFUO4GNn0CXJvlDwkguxXreJFPfTJ2XkoniriuDb4Z3QHlJWpekf3Fo36s1p7ci4MSeiZ8a6r0Ilz88E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=YmhrtoES; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="YmhrtoES" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8B850C16AAE; Fri, 7 Nov 2025 18:18:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1762539509; bh=xQVzgD7GVp4/BOHYm5HSOOyNHFn2GczjN+HJaZNJ9rQ=; h=From:To:Cc:Subject:Date:From; b=YmhrtoESWEncfjf37tPDWCHbtqdtyJtTEOJEqtkkEAauFWUX5ULRZkT9eLRQ0+4dp 3M/tvhxVIj1iehJavPfUh7hgLbUloLRzK1pqAHoKX3GaIPSn4dsFK34mt6RErMJ286 7ImXSLjsGBmoDdVOPXiPAgJXn4Arrg3SQN6C492dB8IM7VYlphzbC+1oICTkjL5kX4 GxzuimQEEoatcFbkHPMhJ75gJjRjKqE5LkSOF1JlEnsU+LGx7DWUO3mCu2dnTijI+1 ZbjqtE+L+ODG8SJHtiHUGBRT2nGyeloXigNuNo//szQCVZ5RMkz1356I7ETmM3R4SW 5OsiGa1L3uxzg== From: "Rafael J. Wysocki" To: Linux PM Cc: LKML , Srinivas Pandruvada Subject: [PATCH v1] cpufreq: intel_pstate: Use mutex guard for driver locking Date: Fri, 07 Nov 2025 19:18:26 +0100 Message-ID: <2807232.mvXUDI8C0e@rafael.j.wysocki> Organization: Linux Kernel Development Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Rafael J. Wysocki Use guard(mutex)(&intel_pstate_driver_lock), or the scoped variant of it, wherever intel_pstate_driver_lock needs to be held. This allows some local variables and goto statements to be dropped as they are not necessary any more. Signed-off-by: Rafael J. Wysocki Reviewed-by: Muhammad Usama Anjum --- drivers/cpufreq/intel_pstate.c | 99 +++++++++++++-----------------------= ----- 1 file changed, 33 insertions(+), 66 deletions(-) --- a/drivers/cpufreq/intel_pstate.c +++ b/drivers/cpufreq/intel_pstate.c @@ -1467,7 +1467,8 @@ static void set_power_ctl_ee_state(bool { u64 power_ctl; =20 - mutex_lock(&intel_pstate_driver_lock); + guard(mutex)(&intel_pstate_driver_lock); + rdmsrq(MSR_IA32_POWER_CTL, power_ctl); if (input) { power_ctl &=3D ~BIT(MSR_IA32_POWER_CTL_BIT_EE); @@ -1477,7 +1478,6 @@ static void set_power_ctl_ee_state(bool power_ctl_ee_state =3D POWER_CTL_EE_DISABLE; } wrmsrq(MSR_IA32_POWER_CTL, power_ctl); - mutex_unlock(&intel_pstate_driver_lock); } =20 static void intel_pstate_hwp_enable(struct cpudata *cpudata); @@ -1599,13 +1599,9 @@ static int intel_pstate_update_status(co static ssize_t show_status(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { - ssize_t ret; - - mutex_lock(&intel_pstate_driver_lock); - ret =3D intel_pstate_show_status(buf); - mutex_unlock(&intel_pstate_driver_lock); + guard(mutex)(&intel_pstate_driver_lock); =20 - return ret; + return intel_pstate_show_status(buf); } =20 static ssize_t store_status(struct kobject *a, struct kobj_attribute *b, @@ -1614,11 +1610,13 @@ static ssize_t store_status(struct kobje char *p =3D memchr(buf, '\n', count); int ret; =20 - mutex_lock(&intel_pstate_driver_lock); + guard(mutex)(&intel_pstate_driver_lock); + ret =3D intel_pstate_update_status(buf, p ? p - buf : count); - mutex_unlock(&intel_pstate_driver_lock); + if (ret < 0) + return ret; =20 - return ret < 0 ? ret : count; + return count; } =20 static ssize_t show_turbo_pct(struct kobject *kobj, @@ -1628,12 +1626,10 @@ static ssize_t show_turbo_pct(struct kob int total, no_turbo, turbo_pct; uint32_t turbo_fp; =20 - mutex_lock(&intel_pstate_driver_lock); + guard(mutex)(&intel_pstate_driver_lock); =20 - if (!intel_pstate_driver) { - mutex_unlock(&intel_pstate_driver_lock); + if (!intel_pstate_driver) return -EAGAIN; - } =20 cpu =3D all_cpu_data[0]; =20 @@ -1642,8 +1638,6 @@ static ssize_t show_turbo_pct(struct kob turbo_fp =3D div_fp(no_turbo, total); turbo_pct =3D 100 - fp_toint(mul_fp(turbo_fp, int_tofp(100))); =20 - mutex_unlock(&intel_pstate_driver_lock); - return sprintf(buf, "%u\n", turbo_pct); } =20 @@ -1653,38 +1647,26 @@ static ssize_t show_num_pstates(struct k struct cpudata *cpu; int total; =20 - mutex_lock(&intel_pstate_driver_lock); + guard(mutex)(&intel_pstate_driver_lock); =20 - if (!intel_pstate_driver) { - mutex_unlock(&intel_pstate_driver_lock); + if (!intel_pstate_driver) return -EAGAIN; - } =20 cpu =3D all_cpu_data[0]; total =3D cpu->pstate.turbo_pstate - cpu->pstate.min_pstate + 1; =20 - mutex_unlock(&intel_pstate_driver_lock); - return sprintf(buf, "%u\n", total); } =20 static ssize_t show_no_turbo(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { - ssize_t ret; - - mutex_lock(&intel_pstate_driver_lock); + guard(mutex)(&intel_pstate_driver_lock); =20 - if (!intel_pstate_driver) { - mutex_unlock(&intel_pstate_driver_lock); + if (!intel_pstate_driver) return -EAGAIN; - } - - ret =3D sprintf(buf, "%u\n", global.no_turbo); - - mutex_unlock(&intel_pstate_driver_lock); =20 - return ret; + return sprintf(buf, "%u\n", global.no_turbo); } =20 static ssize_t store_no_turbo(struct kobject *a, struct kobj_attribute *b, @@ -1696,29 +1678,25 @@ static ssize_t store_no_turbo(struct kob if (sscanf(buf, "%u", &input) !=3D 1) return -EINVAL; =20 - mutex_lock(&intel_pstate_driver_lock); + guard(mutex)(&intel_pstate_driver_lock); =20 - if (!intel_pstate_driver) { - count =3D -EAGAIN; - goto unlock_driver; - } + if (!intel_pstate_driver) + return -EAGAIN; =20 no_turbo =3D !!clamp_t(int, input, 0, 1); =20 WRITE_ONCE(global.turbo_disabled, turbo_is_disabled()); if (global.turbo_disabled && !no_turbo) { pr_notice("Turbo disabled by BIOS or unavailable on processor\n"); - count =3D -EPERM; if (global.no_turbo) - goto unlock_driver; - else - no_turbo =3D 1; - } + return -EPERM; =20 - if (no_turbo =3D=3D global.no_turbo) { - goto unlock_driver; + no_turbo =3D 1; } =20 + if (no_turbo =3D=3D global.no_turbo) + return count; + WRITE_ONCE(global.no_turbo, no_turbo); =20 mutex_lock(&intel_pstate_limits_lock); @@ -1737,9 +1715,6 @@ static ssize_t store_no_turbo(struct kob intel_pstate_update_limits_for_all(); arch_set_max_freq_ratio(no_turbo); =20 -unlock_driver: - mutex_unlock(&intel_pstate_driver_lock); - return count; } =20 @@ -1789,12 +1764,10 @@ static ssize_t store_max_perf_pct(struct if (ret !=3D 1) return -EINVAL; =20 - mutex_lock(&intel_pstate_driver_lock); + guard(mutex)(&intel_pstate_driver_lock); =20 - if (!intel_pstate_driver) { - mutex_unlock(&intel_pstate_driver_lock); + if (!intel_pstate_driver) return -EAGAIN; - } =20 mutex_lock(&intel_pstate_limits_lock); =20 @@ -1807,8 +1780,6 @@ static ssize_t store_max_perf_pct(struct else update_qos_requests(FREQ_QOS_MAX); =20 - mutex_unlock(&intel_pstate_driver_lock); - return count; } =20 @@ -1822,12 +1793,10 @@ static ssize_t store_min_perf_pct(struct if (ret !=3D 1) return -EINVAL; =20 - mutex_lock(&intel_pstate_driver_lock); + guard(mutex)(&intel_pstate_driver_lock); =20 - if (!intel_pstate_driver) { - mutex_unlock(&intel_pstate_driver_lock); + if (!intel_pstate_driver) return -EAGAIN; - } =20 mutex_lock(&intel_pstate_limits_lock); =20 @@ -1841,8 +1810,6 @@ static ssize_t store_min_perf_pct(struct else update_qos_requests(FREQ_QOS_MIN); =20 - mutex_unlock(&intel_pstate_driver_lock); - return count; } =20 @@ -1863,10 +1830,10 @@ static ssize_t store_hwp_dynamic_boost(s if (ret) return ret; =20 - mutex_lock(&intel_pstate_driver_lock); + guard(mutex)(&intel_pstate_driver_lock); + hwp_boost =3D !!input; intel_pstate_update_policies(); - mutex_unlock(&intel_pstate_driver_lock); =20 return count; } @@ -3977,9 +3944,9 @@ hwp_cpu_matched: =20 } =20 - mutex_lock(&intel_pstate_driver_lock); - rc =3D intel_pstate_register_driver(default_driver); - mutex_unlock(&intel_pstate_driver_lock); + scoped_guard(mutex, &intel_pstate_driver_lock) { + rc =3D intel_pstate_register_driver(default_driver); + } if (rc) { intel_pstate_sysfs_remove(); return rc;