[PATCH v3 1/2] PM / devfreq: Fall back to previous_freq when get_cur_freq() fails

From: Bean Huo

Date: Thu Nov 26 2026 - 17:32:56 EST


From: Bean Huo <beanhuo@xxxxxxxxxx>

devfreq_set_target() calls the optional ->get_cur_freq() callback to get
the frequency that is passed as freqs.old to the DEVFREQ_PRECHANGE and
DEVFREQ_POSTCHANGE notifiers, but it does not check the return value. If
the callback fails without setting @freq, cur_freq is never assigned, and
an uninitialized stack value is passed to the notifiers.

hisi_uncore_get_cur_freq() can hit this. It returns -ENODEV without
setting @freq when its PCC channel is missing. On the mailbox error path
it sets @freq to 0 instead, so that the core does not read a random
value.

The other two callers, cur_freq_show() and devfreq_monitor_resume(),
already check the return value and use devfreq->previous_freq when the
callback fails. Do the same in devfreq_set_target(). Read the frequency
into a local variable first, so that a value the callback writes before
it fails is not used either.

This does not seem to cause a visible problem today. The passive governor
is the only DEVFREQ_TRANSITION_NOTIFIER user in the tree, and it only
reads freqs.new. So this patch is not marked for stable.

Fixes: 0fe3a66410a3 ("PM / devfreq: Add new DEVFREQ_TRANSITION_NOTIFIER notifier")
Signed-off-by: Bean Huo <beanhuo@xxxxxxxxxx>
Reviewed-by: Avri Altman <avri.altman@xxxxxxxxxxx>
Reviewed-by: Jie Zhan <zhanjie9@xxxxxxxxxxxxx>
---
Changes since v2:
- Do not call ->get_cur_freq() inside the if condition, as Chanwoo
suggested. No change in behavior.
- Add Reviewed-by from Jie Zhan.

drivers/devfreq/devfreq.c | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
index 82dd9a43dc62..7546ed720dcb 100644
--- a/drivers/devfreq/devfreq.c
+++ b/drivers/devfreq/devfreq.c
@@ -345,13 +345,15 @@ static int devfreq_set_target(struct devfreq *devfreq, unsigned long new_freq,
u32 flags)
{
struct devfreq_freqs freqs;
- unsigned long cur_freq;
+ unsigned long cur_freq = devfreq->previous_freq;
+ unsigned long freq;
int err = 0;

- if (devfreq->profile->get_cur_freq)
- devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
- else
- cur_freq = devfreq->previous_freq;
+ if (devfreq->profile->get_cur_freq) {
+ err = devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq);
+ if (!err)
+ cur_freq = freq;
+ }

freqs.old = cur_freq;
freqs.new = new_freq;
--
2.34.1