* FAILED: patch "[PATCH] cpufreq: Skip inactive policies in cpufreq_driver_target()" failed to apply to 6.1-stable tree
@ 2026-12-02 12:59 gregkh
2026-12-03 19:40 ` [PATCH 6.1.y 1/4] cpufreq: Consolidate some code in cpufreq_online() Sasha Levin
0 siblings, 1 reply; 5+ messages in thread
From: gregkh @ 2026-12-02 12:59 UTC (permalink / raw)
To: zhongqiu.han, rafael.j.wysocki, sashiko-bot, stable; +Cc: stable
The patch below does not apply to the 6.1-stable tree.
If someone wants it applied there, or to any other stable or longterm
tree, then please email the backport, including the original git commit
id to <stable@vger.kernel.org>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.1.y
git checkout FETCH_HEAD
git cherry-pick -x c5bdb87573726d3a10c58bb3ae09daf017be2297
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<stable@vger.kernel.org>' --in-reply-to '2026120248-concerned-system-eb91@gregkh' --subject-prefix 'PATCH 6.1.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From c5bdb87573726d3a10c58bb3ae09daf017be2297 Mon Sep 17 00:00:00 2001
From: Zhongqiu Han <zhongqiu.han@oss.qualcomm.com>
Date: Tue, 10 Nov 2026 21:58:14 +0800
Subject: [PATCH] cpufreq: Skip inactive policies in cpufreq_driver_target()
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>
diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index 24319b214d7b..4332cb114070 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -2452,6 +2452,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);
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 6.1.y 1/4] cpufreq: Consolidate some code in cpufreq_online()
2026-12-02 12:59 FAILED: patch "[PATCH] cpufreq: Skip inactive policies in cpufreq_driver_target()" failed to apply to 6.1-stable tree gregkh
@ 2026-12-03 19:40 ` Sasha Levin
2026-12-03 19:40 ` [PATCH 6.1.y 2/4] cpufreq: Split cpufreq_online() Sasha Levin
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Sasha Levin @ 2026-12-03 19:40 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 387b51709db546033cf9a940860b6614a5cda5b6 ]
Notice that the policy->cpu update in cpufreq_policy_alloc() can be
moved to cpufreq_online() and then it can be carried out under the
policy rwsem, along with the clearing of policy->governor (unnecessary
in the "new policy" code branch, but also not harmful). If this is
done, the bottom parts of the "if (policy)" branches become identical
and they can be collapsed and moved below the conditional.
Modify the code accordingly which makes it somewhat easier to follow.
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/13741234.uLZWGnKmhe@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 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index 931ab927316bd..de9dec99781b0 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -1274,7 +1274,6 @@ static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
init_waitqueue_head(&policy->transition_wait);
INIT_WORK(&policy->update, handle_update);
- policy->cpu = cpu;
return policy;
err_min_qos_notifier:
@@ -1362,17 +1361,18 @@ static int cpufreq_online(unsigned int cpu)
/* This is the only online CPU for the policy. Start over. */
new_policy = false;
- down_write(&policy->rwsem);
- policy->cpu = cpu;
- policy->governor = NULL;
} else {
new_policy = true;
policy = cpufreq_policy_alloc(cpu);
if (!policy)
return -ENOMEM;
- down_write(&policy->rwsem);
}
+ down_write(&policy->rwsem);
+
+ policy->cpu = cpu;
+ policy->governor = NULL;
+
if (!new_policy && cpufreq_driver->online) {
/* Recover policy->cpus using related_cpus */
cpumask_copy(policy->cpus, policy->related_cpus);
--
2.53.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 6.1.y 2/4] cpufreq: Split cpufreq_online()
2026-12-03 19:40 ` [PATCH 6.1.y 1/4] cpufreq: Consolidate some code in cpufreq_online() Sasha Levin
@ 2026-12-03 19:40 ` Sasha Levin
2026-12-03 19:40 ` [PATCH 6.1.y 3/4] cpufreq: Add and use cpufreq policy locking guards Sasha Levin
2026-12-03 19:40 ` [PATCH 6.1.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:40 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 de9dec99781b0..b49d7bd3eeb83 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -1342,32 +1342,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;
@@ -1394,7 +1375,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;
}
/*
@@ -1541,21 +1522,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)
@@ -1569,12 +1539,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.1.y 3/4] cpufreq: Add and use cpufreq policy locking guards
2026-12-03 19:40 ` [PATCH 6.1.y 1/4] cpufreq: Consolidate some code in cpufreq_online() Sasha Levin
2026-12-03 19:40 ` [PATCH 6.1.y 2/4] cpufreq: Split cpufreq_online() Sasha Levin
@ 2026-12-03 19:40 ` Sasha Levin
2026-12-03 19:40 ` [PATCH 6.1.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:40 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 | 109 +++++++++++++++++---------------------
include/linux/cpufreq.h | 6 +++
2 files changed, 54 insertions(+), 61 deletions(-)
diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index b49d7bd3eeb83..8fda458d38bce 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -975,17 +975,16 @@ static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
{
struct cpufreq_policy *policy = to_policy(kobj);
struct freq_attr *fattr = to_attr(attr);
- ssize_t ret = -EBUSY;
if (!fattr->show)
return -EIO;
- down_read(&policy->rwsem);
+ guard(cpufreq_policy_read)(policy);
+
if (likely(!policy_is_inactive(policy)))
- ret = fattr->show(policy, buf);
- up_read(&policy->rwsem);
+ return fattr->show(policy, buf);
- return ret;
+ return -EBUSY;
}
static ssize_t store(struct kobject *kobj, struct attribute *attr,
@@ -993,17 +992,16 @@ static ssize_t store(struct kobject *kobj, struct attribute *attr,
{
struct cpufreq_policy *policy = to_policy(kobj);
struct freq_attr *fattr = to_attr(attr);
- ssize_t ret = -EBUSY;
if (!fattr->store)
return -EIO;
- down_write(&policy->rwsem);
+ guard(cpufreq_policy_write)(policy);
+
if (likely(!policy_is_inactive(policy)))
- ret = fattr->store(policy, buf, count);
- up_write(&policy->rwsem);
+ return fattr->store(policy, buf, count);
- return ret;
+ return -EBUSY;
}
static void cpufreq_sysfs_release(struct kobject *kobj)
@@ -1134,7 +1132,8 @@ static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cp
if (cpumask_test_cpu(cpu, policy->cpus))
return 0;
- down_write(&policy->rwsem);
+ guard(cpufreq_policy_write)(policy);
+
if (has_target())
cpufreq_stop_governor(policy);
@@ -1145,7 +1144,7 @@ static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cp
if (ret)
pr_err("%s: Failed to start governor\n", __func__);
}
- up_write(&policy->rwsem);
+
return ret;
}
@@ -1165,9 +1164,10 @@ static void handle_update(struct work_struct *work)
container_of(work, struct cpufreq_policy, update);
pr_debug("handle_update for cpu %u called\n", policy->cpu);
- down_write(&policy->rwsem);
+
+ guard(cpufreq_policy_write)(policy);
+
refresh_frequency_limits(policy);
- up_write(&policy->rwsem);
}
static int cpufreq_notifier_min(struct notifier_block *nb, unsigned long freq,
@@ -1193,11 +1193,11 @@ static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
struct kobject *kobj;
struct completion *cmp;
- down_write(&policy->rwsem);
- cpufreq_stats_free_table(policy);
- kobj = &policy->kobj;
- cmp = &policy->kobj_unregister;
- up_write(&policy->rwsem);
+ scoped_guard(cpufreq_policy_write, policy) {
+ cpufreq_stats_free_table(policy);
+ kobj = &policy->kobj;
+ cmp = &policy->kobj_unregister;
+ }
kobject_put(kobj);
/*
@@ -1349,7 +1349,7 @@ static int cpufreq_policy_online(struct cpufreq_policy *policy,
unsigned int j;
int ret;
- down_write(&policy->rwsem);
+ guard(cpufreq_policy_write)(policy);
policy->cpu = cpu;
policy->governor = NULL;
@@ -1522,10 +1522,7 @@ static int cpufreq_policy_online(struct cpufreq_policy *policy,
goto out_destroy_policy;
}
-out_unlock:
- up_write(&policy->rwsem);
-
- return ret;
+ return 0;
out_destroy_policy:
for_each_cpu(j, policy->real_cpus)
@@ -1542,7 +1539,7 @@ static int cpufreq_policy_online(struct cpufreq_policy *policy,
out_clear_policy:
cpumask_clear(policy->cpus);
- goto out_unlock;
+ return ret;
}
static int cpufreq_online(unsigned int cpu)
@@ -1677,11 +1674,10 @@ static int cpufreq_offline(unsigned int cpu)
return 0;
}
- down_write(&policy->rwsem);
+ guard(cpufreq_policy_write)(policy);
__cpufreq_offline(cpu, policy);
- up_write(&policy->rwsem);
return 0;
}
@@ -1698,33 +1694,29 @@ static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
if (!policy)
return;
- down_write(&policy->rwsem);
+ scoped_guard(cpufreq_policy_write, policy) {
+ if (cpu_online(cpu))
+ __cpufreq_offline(cpu, policy);
- if (cpu_online(cpu))
- __cpufreq_offline(cpu, policy);
+ remove_cpu_dev_symlink(policy, cpu, dev);
- remove_cpu_dev_symlink(policy, cpu, dev);
+ if (!cpumask_empty(policy->real_cpus))
+ return;
- if (!cpumask_empty(policy->real_cpus)) {
- up_write(&policy->rwsem);
- return;
- }
+ /*
+ * Unregister cpufreq cooling once all the CPUs of the policy
+ * are removed.
+ */
+ if (cpufreq_thermal_control_enabled(cpufreq_driver)) {
+ cpufreq_cooling_unregister(policy->cdev);
+ policy->cdev = NULL;
+ }
- /*
- * Unregister cpufreq cooling once all the CPUs of the policy are
- * removed.
- */
- if (cpufreq_thermal_control_enabled(cpufreq_driver)) {
- cpufreq_cooling_unregister(policy->cdev);
- policy->cdev = NULL;
+ /* We did light-weight exit earlier, do full tear down now */
+ if (cpufreq_driver->offline && cpufreq_driver->exit)
+ cpufreq_driver->exit(policy);
}
- /* We did light-weight exit earlier, do full tear down now */
- if (cpufreq_driver->offline && cpufreq_driver->exit)
- cpufreq_driver->exit(policy);
-
- up_write(&policy->rwsem);
-
cpufreq_policy_free(policy);
}
@@ -1877,15 +1869,16 @@ unsigned int cpufreq_get(unsigned int cpu)
struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
unsigned int ret_freq = 0;
- if (policy) {
- down_read(&policy->rwsem);
+ if (!policy)
+ return 0;
+
+ scoped_guard(cpufreq_policy_read, policy) {
if (cpufreq_driver->get)
ret_freq = __cpufreq_get(policy);
- up_read(&policy->rwsem);
-
- cpufreq_cpu_put(policy);
}
+ cpufreq_cpu_put(policy);
+
return ret_freq;
}
EXPORT_SYMBOL(cpufreq_get);
@@ -2400,15 +2393,9 @@ int cpufreq_driver_target(struct cpufreq_policy *policy,
unsigned int target_freq,
unsigned int relation)
{
- int ret;
+ guard(cpufreq_policy_write)(policy);
- down_write(&policy->rwsem);
-
- ret = __cpufreq_driver_target(policy, target_freq, relation);
-
- 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 a6535f51f3681..7cf9b7db0abff 100644
--- a/include/linux/cpufreq.h
+++ b/include/linux/cpufreq.h
@@ -170,6 +170,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.1.y 4/4] cpufreq: Skip inactive policies in cpufreq_driver_target()
2026-12-03 19:40 ` [PATCH 6.1.y 1/4] cpufreq: Consolidate some code in cpufreq_online() Sasha Levin
2026-12-03 19:40 ` [PATCH 6.1.y 2/4] cpufreq: Split cpufreq_online() Sasha Levin
2026-12-03 19:40 ` [PATCH 6.1.y 3/4] cpufreq: Add and use cpufreq policy locking guards Sasha Levin
@ 2026-12-03 19:40 ` Sasha Levin
2 siblings, 0 replies; 5+ messages in thread
From: Sasha Levin @ 2026-12-03 19:40 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 8fda458d38bce..67de4897067d5 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -2395,6 +2395,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
end of thread, other threads:[~2026-12-03 19:40 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 12:59 FAILED: patch "[PATCH] cpufreq: Skip inactive policies in cpufreq_driver_target()" failed to apply to 6.1-stable tree gregkh
2026-12-03 19:40 ` [PATCH 6.1.y 1/4] cpufreq: Consolidate some code in cpufreq_online() Sasha Levin
2026-12-03 19:40 ` [PATCH 6.1.y 2/4] cpufreq: Split cpufreq_online() Sasha Levin
2026-12-03 19:40 ` [PATCH 6.1.y 3/4] cpufreq: Add and use cpufreq policy locking guards Sasha Levin
2026-12-03 19:40 ` [PATCH 6.1.y 4/4] cpufreq: Skip inactive policies in cpufreq_driver_target() Sasha Levin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox