Currently the driver verification code uses two separate conditions to
ensure that exactly one of setpolicy or target functions is provided:
if (!has_setpolicy && !has_target)
return -EINVAL;
if (has_setpolicy && has_target)
return -EINVAL;
This can be simplified into a single condition:
if (has_setpolicy == has_target)
return -EINVAL;
which makes the intent clearer and avoids duplicated logic.
No functional changes intended.
Signed-off-by: Zihuan Zhang <zhangzihuan@kylinos.cn>
---
drivers/cpufreq/cpufreq.c | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index a067b5447fe8..92633ff2c4f3 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -2908,6 +2908,8 @@ static int cpuhp_cpufreq_offline(unsigned int cpu)
int cpufreq_register_driver(struct cpufreq_driver *driver_data)
{
unsigned long flags;
+ bool has_setpolicy = driver_data->setpolicy;
+ bool has_target = driver_data->target_index || driver_data->target;
int ret;
if (cpufreq_disabled())
@@ -2921,10 +2923,7 @@ int cpufreq_register_driver(struct cpufreq_driver *driver_data)
return -EPROBE_DEFER;
if (!driver_data || !driver_data->verify || !driver_data->init ||
- !(driver_data->setpolicy || driver_data->target_index ||
- driver_data->target) ||
- (driver_data->setpolicy && (driver_data->target_index ||
- driver_data->target)) ||
+ (has_setpolicy == has_target) ||
(!driver_data->get_intermediate != !driver_data->target_intermediate) ||
(!driver_data->online != !driver_data->offline) ||
(driver_data->adjust_perf && !driver_data->fast_switch))
--
2.25.1
On 21-08-25, 17:00, Zihuan Zhang wrote:
> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
> index a067b5447fe8..92633ff2c4f3 100644
> --- a/drivers/cpufreq/cpufreq.c
> +++ b/drivers/cpufreq/cpufreq.c
> @@ -2908,6 +2908,8 @@ static int cpuhp_cpufreq_offline(unsigned int cpu)
> int cpufreq_register_driver(struct cpufreq_driver *driver_data)
> {
> unsigned long flags;
driver_data can be NULL here. It is checked at a later point.
> + bool has_setpolicy = driver_data->setpolicy;
This is a pointer and ..
> + bool has_target = driver_data->target_index || driver_data->target;
.. this is bool.
Their comparison will always fail. Did you actually try this with both
setpolicy and target() set for a cpufreq driver to check if it really
fails ?
What you need is:
bool has_setpolicy = !!driver_data->setpolicy;
> int ret;
>
> if (cpufreq_disabled())
> @@ -2921,10 +2923,7 @@ int cpufreq_register_driver(struct cpufreq_driver *driver_data)
> return -EPROBE_DEFER;
>
> if (!driver_data || !driver_data->verify || !driver_data->init ||
> - !(driver_data->setpolicy || driver_data->target_index ||
> - driver_data->target) ||
> - (driver_data->setpolicy && (driver_data->target_index ||
> - driver_data->target)) ||
> + (has_setpolicy == has_target) ||
I would rather do:
(!!driver_data->setpolicy == (driver_data->target_index || driver_data->target))
> (!driver_data->get_intermediate != !driver_data->target_intermediate) ||
> (!driver_data->online != !driver_data->offline) ||
> (driver_data->adjust_perf && !driver_data->fast_switch))
> --
> 2.25.1
--
viresh
在 2025/8/21 17:17, Viresh Kumar 写道:
> On 21-08-25, 17:00, Zihuan Zhang wrote:
>> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
>> index a067b5447fe8..92633ff2c4f3 100644
>> --- a/drivers/cpufreq/cpufreq.c
>> +++ b/drivers/cpufreq/cpufreq.c
>> @@ -2908,6 +2908,8 @@ static int cpuhp_cpufreq_offline(unsigned int cpu)
>> int cpufreq_register_driver(struct cpufreq_driver *driver_data)
>> {
>> unsigned long flags;
> driver_data can be NULL here. It is checked at a later point.
>
Thanks for your feedback. I did think about the case where driver_data
is NULL, but I didn’t think it through properly at the time.
You are right — this clearly can cause issues.
>> + bool has_setpolicy = driver_data->setpolicy;
> This is a pointer and ..
>
>> + bool has_target = driver_data->target_index || driver_data->target;
> .. this is bool.
>
> Their comparison will always fail. Did you actually try this with both
> setpolicy and target() set for a cpufreq driver to check if it really
> fails ?
>
> What you need is:
>
> bool has_setpolicy = !!driver_data->setpolicy;
Sorry about that. I only tested the case where driver registration succeeds.
Do you have any suggestions on how to better test or handle the cases
where driver registration could fail?
>> int ret;
>>
>> if (cpufreq_disabled())
>> @@ -2921,10 +2923,7 @@ int cpufreq_register_driver(struct cpufreq_driver *driver_data)
>> return -EPROBE_DEFER;
>>
>> if (!driver_data || !driver_data->verify || !driver_data->init ||
>> - !(driver_data->setpolicy || driver_data->target_index ||
>> - driver_data->target) ||
>> - (driver_data->setpolicy && (driver_data->target_index ||
>> - driver_data->target)) ||
>> + (has_setpolicy == has_target) ||
> I would rather do:
>
> (!!driver_data->setpolicy == (driver_data->target_index || driver_data->target))
The current version of the code is much better and safer.
Thanks for pointing this out.
I will carefully test all cases, including potential failure paths, and
send a next version accordingly.
>> (!driver_data->get_intermediate != !driver_data->target_intermediate) ||
>> (!driver_data->online != !driver_data->offline) ||
>> (driver_data->adjust_perf && !driver_data->fast_switch))
>> --
>> 2.25.1
On 21-08-25, 17:45, Zihuan Zhang wrote: > Do you have any suggestions on how to better test or handle the cases where > driver registration could fail? That's what I suggested earlier. > > I would rather do: > > > > (!!driver_data->setpolicy == (driver_data->target_index || driver_data->target)) > > The current version of the code is much better and safer. You can actually do this.. This first converts `driver_data->setpolicy` to bool and then compares both of them, and both can't be true or false. -- viresh
在 2025/8/21 17:56, Viresh Kumar 写道: > On 21-08-25, 17:45, Zihuan Zhang wrote: >>> I would rather do: >>> >>> (!!driver_data->setpolicy == (driver_data->target_index || driver_data->target)) >> The current version of the code is much better and safer. > You can actually do this.. This first converts > `driver_data->setpolicy` to bool and then compares both of them, and > both can't be true or false. Thanks. I will.
© 2016 - 2026 Red Hat, Inc.