* [PATCH 6.6.y 2/4] cpufreq: Split cpufreq_online()
2026-12-03 19:30 ` [PATCH 6.6.y 1/4] cpufreq: Consolidate some code in cpufreq_online() Sasha Levin
@ 2026-12-03 19:30 ` Sasha Levin
2026-12-03 19:30 ` [PATCH 6.6.y 3/4] cpufreq: Add and use cpufreq policy locking guards Sasha Levin
2026-12-03 19:30 ` [PATCH 6.6.y 4/4] cpufreq: Skip inactive policies in cpufreq_driver_target() Sasha Levin
2 siblings, 0 replies; 5+ messages in thread
From: Sasha Levin @ 2026-12-03 19:30 UTC (permalink / raw)
To: stable
Cc: Rafael J. Wysocki, Viresh Kumar, Mario Limonciello, Sudeep Holla,
Sasha Levin
From: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com>
[ Upstream commit 68974e3a15b92639396f39d1cd09d5288ba687bc ]
In preparation for the introduction of cpufreq policy locking guards,
move the part of cpufreq_online() that is carried out under the policy
rwsem into a separate function called cpufreq_policy_online().
No intentional functional impact.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Mario Limonciello <mario.limonciello@amd.com>
Acked-by: Sudeep Holla <sudeep.holla@arm.com>
Tested-by: Sudeep Holla <sudeep.holla@arm.com>
Link: https://patch.msgid.link/3354747.aeNJFYEL58@rjwysocki.net
Stable-dep-of: c5bdb8757372 ("cpufreq: Skip inactive policies in cpufreq_driver_target()")
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
drivers/cpufreq/cpufreq.c | 91 ++++++++++++++++++++++-----------------
1 file changed, 52 insertions(+), 39 deletions(-)
diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index 6450bca6e3b74..fd30060927f32 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -1391,32 +1391,13 @@ static void cpufreq_policy_free(struct cpufreq_policy *policy)
kfree(policy);
}
-static int cpufreq_online(unsigned int cpu)
+static int cpufreq_policy_online(struct cpufreq_policy *policy,
+ unsigned int cpu, bool new_policy)
{
- struct cpufreq_policy *policy;
- bool new_policy;
unsigned long flags;
unsigned int j;
int ret;
- pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
-
- /* Check if this CPU already has a policy to manage it */
- policy = per_cpu(cpufreq_cpu_data, cpu);
- if (policy) {
- WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
- if (!policy_is_inactive(policy))
- return cpufreq_add_policy_cpu(policy, cpu);
-
- /* This is the only online CPU for the policy. Start over. */
- new_policy = false;
- } else {
- new_policy = true;
- policy = cpufreq_policy_alloc(cpu);
- if (!policy)
- return -ENOMEM;
- }
-
down_write(&policy->rwsem);
policy->cpu = cpu;
@@ -1443,7 +1424,7 @@ static int cpufreq_online(unsigned int cpu)
if (ret) {
pr_debug("%s: %d: initialization failed\n", __func__,
__LINE__);
- goto out_free_policy;
+ goto out_clear_policy;
}
/* Let the per-policy boost flag mirror the cpufreq_driver boost during init */
@@ -1594,21 +1575,10 @@ static int cpufreq_online(unsigned int cpu)
goto out_destroy_policy;
}
+out_unlock:
up_write(&policy->rwsem);
- kobject_uevent(&policy->kobj, KOBJ_ADD);
-
- /* Callback for handling stuff after policy is ready */
- if (cpufreq_driver->ready)
- cpufreq_driver->ready(policy);
-
- /* Register cpufreq cooling only for a new policy */
- if (new_policy && cpufreq_thermal_control_enabled(cpufreq_driver))
- policy->cdev = of_cpufreq_cooling_register(policy);
-
- pr_debug("initialization complete\n");
-
- return 0;
+ return ret;
out_destroy_policy:
for_each_cpu(j, policy->real_cpus)
@@ -1622,12 +1592,55 @@ static int cpufreq_online(unsigned int cpu)
if (cpufreq_driver->exit)
cpufreq_driver->exit(policy);
-out_free_policy:
+out_clear_policy:
cpumask_clear(policy->cpus);
- up_write(&policy->rwsem);
- cpufreq_policy_free(policy);
- return ret;
+ goto out_unlock;
+}
+
+static int cpufreq_online(unsigned int cpu)
+{
+ struct cpufreq_policy *policy;
+ bool new_policy;
+ int ret;
+
+ pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
+
+ /* Check if this CPU already has a policy to manage it */
+ policy = per_cpu(cpufreq_cpu_data, cpu);
+ if (policy) {
+ WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
+ if (!policy_is_inactive(policy))
+ return cpufreq_add_policy_cpu(policy, cpu);
+
+ /* This is the only online CPU for the policy. Start over. */
+ new_policy = false;
+ } else {
+ new_policy = true;
+ policy = cpufreq_policy_alloc(cpu);
+ if (!policy)
+ return -ENOMEM;
+ }
+
+ ret = cpufreq_policy_online(policy, cpu, new_policy);
+ if (ret) {
+ cpufreq_policy_free(policy);
+ return ret;
+ }
+
+ kobject_uevent(&policy->kobj, KOBJ_ADD);
+
+ /* Callback for handling stuff after policy is ready */
+ if (cpufreq_driver->ready)
+ cpufreq_driver->ready(policy);
+
+ /* Register cpufreq cooling only for a new policy */
+ if (new_policy && cpufreq_thermal_control_enabled(cpufreq_driver))
+ policy->cdev = of_cpufreq_cooling_register(policy);
+
+ pr_debug("initialization complete\n");
+
+ return 0;
}
/**
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH 6.6.y 3/4] cpufreq: Add and use cpufreq policy locking guards
2026-12-03 19:30 ` [PATCH 6.6.y 1/4] cpufreq: Consolidate some code in cpufreq_online() Sasha Levin
2026-12-03 19:30 ` [PATCH 6.6.y 2/4] cpufreq: Split cpufreq_online() Sasha Levin
@ 2026-12-03 19:30 ` Sasha Levin
2026-12-03 19:30 ` [PATCH 6.6.y 4/4] cpufreq: Skip inactive policies in cpufreq_driver_target() Sasha Levin
2 siblings, 0 replies; 5+ messages in thread
From: Sasha Levin @ 2026-12-03 19:30 UTC (permalink / raw)
To: stable
Cc: Rafael J. Wysocki, Viresh Kumar, Mario Limonciello, Sudeep Holla,
Sasha Levin
From: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com>
[ Upstream commit 6fec833b9d70c54ceacbf7d07665215fbd0cddef ]
Introduce "read" and "write" locking guards for cpufreq policies and use
them where applicable in the cpufreq core.
No intentional functional impact.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Mario Limonciello <mario.limonciello@amd.com>
Acked-by: Sudeep Holla <sudeep.holla@arm.com>
Tested-by: Sudeep Holla <sudeep.holla@arm.com>
Link: https://patch.msgid.link/8518682.T7Z3S40VBb@rjwysocki.net
Stable-dep-of: c5bdb8757372 ("cpufreq: Skip inactive policies in cpufreq_driver_target()")
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
drivers/cpufreq/cpufreq.c | 10 ++--------
include/linux/cpufreq.h | 6 ++++++
2 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index fd30060927f32..18666cdf64d29 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -2452,15 +2452,9 @@ int cpufreq_driver_target(struct cpufreq_policy *policy,
unsigned int target_freq,
unsigned int relation)
{
- int ret;
-
- down_write(&policy->rwsem);
-
- ret = __cpufreq_driver_target(policy, target_freq, relation);
+ guard(cpufreq_policy_write)(policy);
- up_write(&policy->rwsem);
-
- return ret;
+ return __cpufreq_driver_target(policy, target_freq, relation);
}
EXPORT_SYMBOL_GPL(cpufreq_driver_target);
diff --git a/include/linux/cpufreq.h b/include/linux/cpufreq.h
index 03ea59ad0b2dc..eed53fec5f0ee 100644
--- a/include/linux/cpufreq.h
+++ b/include/linux/cpufreq.h
@@ -179,6 +179,12 @@ struct cpufreq_policy {
struct notifier_block nb_max;
};
+DEFINE_GUARD(cpufreq_policy_write, struct cpufreq_policy *,
+ down_write(&_T->rwsem), up_write(&_T->rwsem))
+
+DEFINE_GUARD(cpufreq_policy_read, struct cpufreq_policy *,
+ down_read(&_T->rwsem), up_read(&_T->rwsem))
+
/*
* Used for passing new cpufreq policy data to the cpufreq driver's ->verify()
* callback for sanitization. That callback is only expected to modify the min
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH 6.6.y 4/4] cpufreq: Skip inactive policies in cpufreq_driver_target()
2026-12-03 19:30 ` [PATCH 6.6.y 1/4] cpufreq: Consolidate some code in cpufreq_online() Sasha Levin
2026-12-03 19:30 ` [PATCH 6.6.y 2/4] cpufreq: Split cpufreq_online() Sasha Levin
2026-12-03 19:30 ` [PATCH 6.6.y 3/4] cpufreq: Add and use cpufreq policy locking guards Sasha Levin
@ 2026-12-03 19:30 ` Sasha Levin
2 siblings, 0 replies; 5+ messages in thread
From: Sasha Levin @ 2026-12-03 19:30 UTC (permalink / raw)
To: stable; +Cc: Zhongqiu Han, Sashiko, Rafael J. Wysocki, Sasha Levin
From: Zhongqiu Han <zhongqiu.han@oss.qualcomm.com>
[ Upstream commit c5bdb87573726d3a10c58bb3ae09daf017be2297 ]
A policy obtained with cpufreq_cpu_get() may become inactive before
cpufreq_driver_target() acquires its rwsem, which may lead to a NULL
pointer dereference:
CPU X CPU Y (last CPU in policy)
cpufreq_cpu_get()
cpufreq_offline()
__cpufreq_offline()
->exit() /* frees freq_table */
policy->freq_table = NULL
cpufreq_driver_target()
__cpufreq_driver_target()
__target_index()
policy->freq_table[index] /* NULL dereference */
Before commit 2372784542cb ("cpufreq: Drop 'freq_table' argument of
__target_index()"), __cpufreq_driver_target() returned an error if
policy->freq_table was NULL. Since the offline path clears it under the
policy rwsem, which cpufreq_driver_target() also holds, that check was
sufficient to prevent the crash.
The race has been reachable in-tree since commit c210063b40ac
("cpufreq: mediatek: Add opp notification support"), which added the
first caller of cpufreq_driver_target() that can run concurrently with
CPU offline.
Address this by making cpufreq_driver_target() return -EBUSY if the
policy is inactive after acquiring its rwsem, in analogy with the checks
in cpufreq_get(), refresh_frequency_limits() and the sysfs show() and
store() callbacks.
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/patchset/20261104153042.3987338-1-zhongqiu.han%40oss.qualcomm.com
Fixes: 2372784542cb ("cpufreq: Drop 'freq_table' argument of __target_index()")
Fixes: c210063b40ac ("cpufreq: mediatek: Add opp notification support")
Cc: 5.19+ <stable@vger.kernel.org> # v5.19+
Signed-off-by: Zhongqiu Han <zhongqiu.han@oss.qualcomm.com>
Link: https://patch.msgid.link/20261110135814.621355-1-zhongqiu.han@oss.qualcomm.com
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
drivers/cpufreq/cpufreq.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index 18666cdf64d29..7a8c4628b4a9c 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -2454,6 +2454,14 @@ int cpufreq_driver_target(struct cpufreq_policy *policy,
{
guard(cpufreq_policy_write)(policy);
+ /*
+ * The last CPU in the policy may have gone offline after the caller
+ * obtained it, in which case the policy is inactive and must not be
+ * used for changing the frequency.
+ */
+ if (unlikely(policy_is_inactive(policy)))
+ return -EBUSY;
+
return __cpufreq_driver_target(policy, target_freq, relation);
}
EXPORT_SYMBOL_GPL(cpufreq_driver_target);
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread