PM / devfreq: check the get_cur_freq() return value
Subsystems (2)
v3 cover PM / devfreq: check the get_cur_freq() return value
From: Bean Huo <[email protected]>
devfreq_set_target() does not check the return value of the optional ->get_cur_freq() callback, so a failing callback can leave an uninitialized frequency in freqs.old for the transition notifiers. Patch 1 fixes that by falling back to previous_freq, like the other two callers already do. Patch 2 writes down what a driver is expected to return from the callback.
v2 had four patches. Martin applied patches 3 and 4 (the ufshcd part) to the SCSI tree, so this v3 only has the two devfreq patches.
Changes since v2:
- Patch 1: do not call ->get_cur_freq() inside the if condition. Read the frequency into a local variable and only use it when the callback returns 0, as Chanwoo suggested. Behavior is the same as in v2. I kept the Reviewed-by tags from Avri and Jie, since only the form of the code changed; please tell me if that is not OK.
- Patch 1 and 2: add Reviewed-by from Jie Zhan.
- Patch 2: add Acked-by from Chanwoo Choi. No change in the patch.
v2: https://lore.kernel.org/all/[email protected]/
Bean Huo (2):
PM / devfreq: Fall back to previous_freq when get_cur_freq() fails PM / devfreq: Add more details to the get_cur_freq() comment
drivers/devfreq/devfreq.c | 12 +++++++----- include/linux/devfreq.h | 7 +++++-- 2 files changed, 12 insertions(+), 7 deletions(-)
base-commit: 4ff20abe28d65a0c52f0b280008fcfb93c3ee2b2
Signature · 1 line
2.34.1
PATCH v3 1/2 PM / devfreq: Fall back to previous_freq when get_cur_freq() fails
From: Bean Huo <[email protected]>
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.
---
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(-)
drivers/devfreq/devfreq.c +7 -5
@@ -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;
Signature · 1 line
2.34.1
PATCH v3 2/2 PM / devfreq: Add more details to the get_cur_freq() comment
In reply to Bean HuoFrom: Bean Huo <[email protected]>
The comment for ->get_cur_freq() only says that the device should provide the frequency at which it is operating. It does not tell the driver author which unit to use, what the return value means, or that the frequency should be one of @freq_table. These have to be found by reading the devfreq core.
Add these details to the comment.
---
Changes since v2:
- Add Reviewed-by from Jie Zhan and Acked-by from Chanwoo Choi.
include/linux/devfreq.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
include/linux/devfreq.h +5 -2
@@ -90,8 +90,11 @@ struct devfreq_dev_status {* use this directly. Instead, governors are recommended * to use devfreq_update_stats() along with * devfreq.last_status. -* @get_cur_freq: The device should provide the current frequency -* at which it is operating. +* @get_cur_freq: The device should provide the frequency, in Hz, at +* which it is currently operating, and return 0, or a +* negative errno on failure. The frequency should be one +* of @freq_table, as it is used for the transition +* statistics. * @exit: An optional callback that is called when devfreq * is removing the devfreq object due to error or * from devfreq_remove_device() call. If the user
Signature · 1 line
2.34.1
Chanwoo Choi
AppliedHi,
Quoted 28 lines · Bean Huo
From: Bean Huo <[email protected]>
devfreq_set_target()does not check the return value of the optional ->get_cur_freq() callback, so a failing callback can leave an uninitialized frequency in freqs.old for the transition notifiers. Patch 1 fixes that by falling back toprevious_freq, like the other two callers already do. Patch 2 writes down what a driver is expected to return from the callback.v2 had four patches. Martin applied patches 3 and 4 (the ufshcd part) to the SCSI tree, so this v3 only has the two devfreq patches.
Changes since v2:
- Patch 1: do not call ->get_cur_freq() inside the if condition. Read the frequency into a local variable and only use it when the callback returns 0, as Chanwoo suggested. Behavior is the same as in v2. I kept the Reviewed-by tags from Avri and Jie, since only the form of the code changed; please tell me if that is not OK.
- Patch 1 and 2: add Reviewed-by from Jie Zhan.
- Patch 2: add Acked-by from Chanwoo Choi. No change in the patch.v2: https://lore.kernel.org/all/[email protected]/
Bean Huo (2):
PM / devfreq: Fall back toprevious_freqwhenget_cur_freq()fails PM / devfreq: Add more details to theget_cur_freq()comment
Quoted 10 lines
drivers/devfreq/devfreq.c | 12 +++++++----- include/linux/devfreq.h | 7 +++++-- 2 files changed, 12 insertions(+), 7 deletions(-)base-commit:
4ff20abe28d65a0c52f0b280008fcfb93c3ee2b2--
2.34.1
Applied them. Thanks.
Signature · 3 lines
Best Regards, Chanwoo Choi Samsung Electronics