[PATCH v4] PM / devfreq: governor_userspace: Guard against NULL governor_data during switch

From: Finley Xiao

Date: Thu Dec 03 2026 - 22:03:25 EST


The userspace governor is the only one that uses devfreq->governor_data,
allocated in its DEVFREQ_GOV_START handler (userspace_init()) and freed
in its DEVFREQ_GOV_STOP handler (userspace_exit()).

When switching governors via sysfs (governor_store in devfreq.c), the
sequence is:

STOP(old) -> df->governor = userspace -> START(userspace) -> userspace_init

Between the df->governor assignment and userspace_init() completing,
governor_data is still NULL (the previous governor never touched it,
and the core does not clear or pre-allocate it). During that window,
asynchronous paths that call update_devfreq() outside of the driver's
control -- the OPP notifier (devfreq_notifier_call()) and the pm_qos
notifier (qos_notifier_call()) -- take df->lock and call
update_devfreq(), which ends up in devfreq_userspace_func(), that
dereferences governor_data without a NULL check, causing:

Unable to handle kernel access to user memory outside uaccess routines
pc : devfreq_userspace_func+0xc/0x28
lr : devfreq_update_target+0x58/0x158

Add a NULL check in devfreq_userspace_func() and fall back to
previous_freq, which is the same behaviour as the existing "no user
frequency specified yet" branch. This closes the race for all callers
since every path ultimately goes through get_target_freq.

Additionally, take devfreq->lock around every place where governor_data
is written or freed in userspace_init() and userspace_exit(), including
the error path that drops the data when sysfs_create_group() fails.
Without it, a concurrent OPP/pm_qos notifier running under devfreq->lock
could race with the setup/teardown of governor_data and read
governor_data after it is freed but before it is set to NULL,
constituting a use-after-free. Note that all DEVFREQ_GOV_START callers
invoke the handler without holding devfreq->lock, so taking the lock in
userspace_init() is safe.

Fixes: 5fdded844892 ("PM/devfreq: governor: Add a private governor_data for governor")
Cc: stable@xxxxxxxxxxxxxxx # 5.10+
Signed-off-by: Finley Xiao <finley.xiao@xxxxxxxxxxxxxx>

Link: https://lore.kernel.org/linux-pm/20260723080806.1451-1-finley.xiao@xxxxxxxxxxxxxx/
---
Changes in v4:
- Rebased on top of devfreq-next, which contains commit 444612ce70e4
("PM / devfreq: userspace: Fix memory leak in userspace_init()") that
rewrote userspace_init().
- Also protect the sysfs_create_group() error path in userspace_init()
with devfreq->lock.

Changes in v3:
- Also take devfreq->lock around the governor_data assignment in
userspace_init()

Changes in v2:
- Add Fixes: tag and Cc: stable@xxxxxxxxxxxxxxx

drivers/devfreq/governor_userspace.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/devfreq/governor_userspace.c b/drivers/devfreq/governor_userspace.c
index 8211aadb81f0..4ca3b28a4b26 100644
--- a/drivers/devfreq/governor_userspace.c
+++ b/drivers/devfreq/governor_userspace.c
@@ -24,7 +24,7 @@ static int devfreq_userspace_func(struct devfreq *df, unsigned long *freq)
{
struct userspace_data *data = df->governor_data;

- if (data->valid)
+ if (data && data->valid)
*freq = data->user_frequency;
else
*freq = df->previous_freq; /* No user freq specified yet */
@@ -93,12 +93,16 @@ static int userspace_init(struct devfreq *devfreq)
return -ENOMEM;

data->valid = false;
+ mutex_lock(&devfreq->lock);
devfreq->governor_data = data;
+ mutex_unlock(&devfreq->lock);

err = sysfs_create_group(&devfreq->dev.kobj, &dev_attr_group);
if (err) {
+ mutex_lock(&devfreq->lock);
kfree(data);
devfreq->governor_data = NULL;
+ mutex_unlock(&devfreq->lock);
}

return err;
@@ -113,8 +117,10 @@ static void userspace_exit(struct devfreq *devfreq)
if (devfreq->dev.kobj.sd)
sysfs_remove_group(&devfreq->dev.kobj, &dev_attr_group);

+ mutex_lock(&devfreq->lock);
kfree(devfreq->governor_data);
devfreq->governor_data = NULL;
+ mutex_unlock(&devfreq->lock);
}

static int devfreq_userspace_handler(struct devfreq *devfreq,
--
2.43.0