From: Jie Zhan <zhanjie9@hisilicon.com>
To: Finley Xiao <finley.xiao@rock-chips.com>, <heiko@sntech.de>,
<myungjoo.ham@samsung.com>, <kyungmin.park@samsung.com>,
<cw00.choi@samsung.com>, <linux-pm@vger.kernel.org>,
<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v1] PM / devfreq: governor_userspace: Guard against NULL governor_data during switch
Date: Tue, 28 Jul 2026 11:06:25 +0800 [thread overview]
Message-ID: <4e1b7360-4ab9-4c50-b4b9-61a44707068a@hisilicon.com> (raw)
In-Reply-To: <20260723080806.1451-1-finley.xiao@rock-chips.com>
Hi Finley,
On 7/23/2026 4:08 PM, Finley Xiao wrote:
> 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.c:686) and the pm_qos notifier
> (devfreq.c:707) -- run devfreq_update_target() under df->lock and reach
> devfreq_userspace_func(), which 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 the kfree()/NULL assignment in
> userspace_exit(). Without it, a concurrent OPP/pm_qos notifier running
> under devfreq->lock could read governor_data after it is freed but
> before it is set to NULL, constituting a use-after-free.
>
> Signed-off-by: Finley Xiao <finley.xiao@rock-chips.com>
> ---
> drivers/devfreq/governor_userspace.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/devfreq/governor_userspace.c b/drivers/devfreq/governor_userspace.c
> index 3906ebedbae8..f7a485c43fd7 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)
This one is fine.
> *freq = data->user_frequency;
> else
> *freq = df->previous_freq; /* No user freq specified yet */
> @@ -110,8 +110,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);
However, for this one, I'm not sure it's the proper place to add the lock.
Why should we allow the whole governor switching to be concurrent with
frequency switching? I guess there might be something wrong elsewhere.
The devfreq->lock should be held around governor switching.
Can you have a try? but be careful of deadlock because the governor
switching currently holds devfreq_list_lock.
Thanks!
Jie
> }
>
> static int devfreq_userspace_handler(struct devfreq *devfreq,
next prev parent reply other threads:[~2026-07-28 3:06 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-23 8:08 Finley Xiao
2026-07-28 3:06 ` Jie Zhan [this message]
2026-09-29 10:11 ` Finley Xiao
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=4e1b7360-4ab9-4c50-b4b9-61a44707068a@hisilicon.com \
--to=zhanjie9@hisilicon.com \
--cc=cw00.choi@samsung.com \
--cc=finley.xiao@rock-chips.com \
--cc=heiko@sntech.de \
--cc=kyungmin.park@samsung.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=myungjoo.ham@samsung.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®