[PATCH] cpufreq: Update sscanf() to kstrtouint()

Bowen Yu posted 1 patch 7 months ago
drivers/cpufreq/cpufreq.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
[PATCH] cpufreq: Update sscanf() to kstrtouint()
Posted by Bowen Yu 7 months ago
In store_scaling_setspeed(), sscanf is still used to read to sysfs.
Newer kstrtox provide more features including overflow protection,
better errorhandling and allows for other systems of numeration. It
is therefore better to update sscanf() to kstrtouint().

Signed-off-by: Bowen Yu <yubowen8@huawei.com>
---
 drivers/cpufreq/cpufreq.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index be727da0be4d..0c842edd1a76 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -920,9 +920,9 @@ static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
 	if (!policy->governor || !policy->governor->store_setspeed)
 		return -EINVAL;
 
-	ret = sscanf(buf, "%u", &freq);
-	if (ret != 1)
-		return -EINVAL;
+	ret = kstrtouint(buf, 0, &freq);
+	if (ret)
+		return ret;
 
 	policy->governor->store_setspeed(policy, freq);
 
-- 
2.33.0
Re: [PATCH] cpufreq: Update sscanf() to kstrtouint()
Posted by David Laight 7 months ago
On Mon, 19 May 2025 15:09:38 +0800
Bowen Yu <yubowen8@huawei.com> wrote:

> In store_scaling_setspeed(), sscanf is still used to read to sysfs.
> Newer kstrtox provide more features including overflow protection,
> better errorhandling and allows for other systems of numeration. It
> is therefore better to update sscanf() to kstrtouint().

This is a UAPI change.
Since the value is a frequency there could easily be scripts
that append Hz to the value.
You're making them fail.

	David

> 
> Signed-off-by: Bowen Yu <yubowen8@huawei.com>
> ---
>  drivers/cpufreq/cpufreq.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
> index be727da0be4d..0c842edd1a76 100644
> --- a/drivers/cpufreq/cpufreq.c
> +++ b/drivers/cpufreq/cpufreq.c
> @@ -920,9 +920,9 @@ static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
>  	if (!policy->governor || !policy->governor->store_setspeed)
>  		return -EINVAL;
>  
> -	ret = sscanf(buf, "%u", &freq);
> -	if (ret != 1)
> -		return -EINVAL;
> +	ret = kstrtouint(buf, 0, &freq);
> +	if (ret)
> +		return ret;
>  
>  	policy->governor->store_setspeed(policy, freq);
>
Re: [PATCH] cpufreq: Update sscanf() to kstrtouint()
Posted by Rafael J. Wysocki 7 months ago
On Mon, May 19, 2025 at 11:46 PM David Laight
<david.laight.linux@gmail.com> wrote:
>
> On Mon, 19 May 2025 15:09:38 +0800
> Bowen Yu <yubowen8@huawei.com> wrote:
>
> > In store_scaling_setspeed(), sscanf is still used to read to sysfs.
> > Newer kstrtox provide more features including overflow protection,
> > better errorhandling and allows for other systems of numeration. It
> > is therefore better to update sscanf() to kstrtouint().
>
> This is a UAPI change.
> Since the value is a frequency there could easily be scripts
> that append Hz to the value.

Which would be incorrect because the value is in kHz.

They are not expected to do so in any case, though, so hopefully there
are none, in which case we don't want any to appear in the future.

> You're making them fail.

So if there are any, we'll get to know.
回复: [PATCH] cpufreq: Update sscanf() to kstrtouint()
Posted by yubowen (H) 7 months ago
Hi David,

Thanks for reviewing.

I think since reading the frequency value gives only value and not unit, the input should stay coherent and only take numbers as valid input instead of accepting all user input with numbers at the beginning. In addition, previous patches have changed other places that used sscanf() in this file and it is better to stay consistent.

Would you say that this makes sense?

Best regards,

Bowen Yu

-----邮件原件-----
发件人: David Laight <david.laight.linux@gmail.com> 
发送时间: 2025年5月20日 5:47
收件人: yubowen (H) <yubowen8@huawei.com>
抄送: rafael@kernel.org; viresh.kumar@linaro.org; linux-pm@vger.kernel.org; linux-kernel@vger.kernel.org; Linuxarm <linuxarm@huawei.com>; Zhanjie <zhanjie9@hisilicon.com>; Jonathan Cameron <jonathan.cameron@huawei.com>; lihuisong (C) <lihuisong@huawei.com>; zhenglifeng (A) <zhenglifeng1@huawei.com>; cenxinghai <cenxinghai@h-partners.com>
主题: Re: [PATCH] cpufreq: Update sscanf() to kstrtouint()

On Mon, 19 May 2025 15:09:38 +0800
Bowen Yu <yubowen8@huawei.com> wrote:

> In store_scaling_setspeed(), sscanf is still used to read to sysfs.
> Newer kstrtox provide more features including overflow protection, 
> better errorhandling and allows for other systems of numeration. It is 
> therefore better to update sscanf() to kstrtouint().

This is a UAPI change.
Since the value is a frequency there could easily be scripts that append Hz to the value.
You're making them fail.

	David

> 
> Signed-off-by: Bowen Yu <yubowen8@huawei.com>
> ---
>  drivers/cpufreq/cpufreq.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c 
> index be727da0be4d..0c842edd1a76 100644
> --- a/drivers/cpufreq/cpufreq.c
> +++ b/drivers/cpufreq/cpufreq.c
> @@ -920,9 +920,9 @@ static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
>  	if (!policy->governor || !policy->governor->store_setspeed)
>  		return -EINVAL;
>  
> -	ret = sscanf(buf, "%u", &freq);
> -	if (ret != 1)
> -		return -EINVAL;
> +	ret = kstrtouint(buf, 0, &freq);
> +	if (ret)
> +		return ret;
>  
>  	policy->governor->store_setspeed(policy, freq);
>  

Re: [PATCH] cpufreq: Update sscanf() to kstrtouint()
Posted by Viresh Kumar 7 months ago
On 19-05-25, 15:09, Bowen Yu wrote:
> In store_scaling_setspeed(), sscanf is still used to read to sysfs.
> Newer kstrtox provide more features including overflow protection,
> better errorhandling and allows for other systems of numeration. It
> is therefore better to update sscanf() to kstrtouint().
> 
> Signed-off-by: Bowen Yu <yubowen8@huawei.com>
> ---
>  drivers/cpufreq/cpufreq.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
> index be727da0be4d..0c842edd1a76 100644
> --- a/drivers/cpufreq/cpufreq.c
> +++ b/drivers/cpufreq/cpufreq.c
> @@ -920,9 +920,9 @@ static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
>  	if (!policy->governor || !policy->governor->store_setspeed)
>  		return -EINVAL;
>  
> -	ret = sscanf(buf, "%u", &freq);
> -	if (ret != 1)
> -		return -EINVAL;
> +	ret = kstrtouint(buf, 0, &freq);
> +	if (ret)
> +		return ret;
>  
>  	policy->governor->store_setspeed(policy, freq);

Acked-by: Viresh Kumar <viresh.kumar@linaro.org>

-- 
viresh
Re: [PATCH] cpufreq: Update sscanf() to kstrtouint()
Posted by Rafael J. Wysocki 7 months ago
On Mon, May 19, 2025 at 12:04 PM Viresh Kumar <viresh.kumar@linaro.org> wrote:
>
> On 19-05-25, 15:09, Bowen Yu wrote:
> > In store_scaling_setspeed(), sscanf is still used to read to sysfs.
> > Newer kstrtox provide more features including overflow protection,
> > better errorhandling and allows for other systems of numeration. It
> > is therefore better to update sscanf() to kstrtouint().
> >
> > Signed-off-by: Bowen Yu <yubowen8@huawei.com>
> > ---
> >  drivers/cpufreq/cpufreq.c | 6 +++---
> >  1 file changed, 3 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
> > index be727da0be4d..0c842edd1a76 100644
> > --- a/drivers/cpufreq/cpufreq.c
> > +++ b/drivers/cpufreq/cpufreq.c
> > @@ -920,9 +920,9 @@ static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
> >       if (!policy->governor || !policy->governor->store_setspeed)
> >               return -EINVAL;
> >
> > -     ret = sscanf(buf, "%u", &freq);
> > -     if (ret != 1)
> > -             return -EINVAL;
> > +     ret = kstrtouint(buf, 0, &freq);
> > +     if (ret)
> > +             return ret;
> >
> >       policy->governor->store_setspeed(policy, freq);
>
> Acked-by: Viresh Kumar <viresh.kumar@linaro.org>

Applied as 6.16 material, thanks!