[PATCH 1/2] sched/debug: Convert copy_from_user() + kstrtouint() to kstrtouint_from_user()

Fushuai Wang posted 2 patches 3 weeks, 1 day ago
There is a newer version of this series
[PATCH 1/2] sched/debug: Convert copy_from_user() + kstrtouint() to kstrtouint_from_user()
Posted by Fushuai Wang 3 weeks, 1 day ago
From: Fushuai Wang <wangfushuai@baidu.com>

Using kstrtouint_from_user() instead of copy_from_user() + kstrtouint()
makes the code simpler and less error-prone.

Signed-off-by: Fushuai Wang <wangfushuai@baidu.com>
---
 kernel/sched/debug.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)

diff --git a/kernel/sched/debug.c b/kernel/sched/debug.c
index 41caa22e0680..1091f9046260 100644
--- a/kernel/sched/debug.c
+++ b/kernel/sched/debug.c
@@ -172,18 +172,12 @@ static const struct file_operations sched_feat_fops = {
 static ssize_t sched_scaling_write(struct file *filp, const char __user *ubuf,
 				   size_t cnt, loff_t *ppos)
 {
-	char buf[16];
 	unsigned int scaling;
+	int ret;
 
-	if (cnt > 15)
-		cnt = 15;
-
-	if (copy_from_user(&buf, ubuf, cnt))
-		return -EFAULT;
-	buf[cnt] = '\0';
-
-	if (kstrtouint(buf, 10, &scaling))
-		return -EINVAL;
+	ret = kstrtouint_from_user(ubuf, cnt, 10, &scaling);
+	if (ret < 0)
+		return ret;
 
 	if (scaling >= SCHED_TUNABLESCALING_END)
 		return -EINVAL;
-- 
2.36.1
Re: [PATCH 1/2] sched/debug: Convert copy_from_user() + kstrtouint() to kstrtouint_from_user()
Posted by Yury Norov 3 weeks, 1 day ago
On Fri, Jan 16, 2026 at 09:17:50PM +0800, Fushuai Wang wrote:
> From: Fushuai Wang <wangfushuai@baidu.com>
> 
> Using kstrtouint_from_user() instead of copy_from_user() + kstrtouint()
> makes the code simpler and less error-prone.
> 
> Signed-off-by: Fushuai Wang <wangfushuai@baidu.com>
> ---
>  kernel/sched/debug.c | 14 ++++----------
>  1 file changed, 4 insertions(+), 10 deletions(-)
> 
> diff --git a/kernel/sched/debug.c b/kernel/sched/debug.c
> index 41caa22e0680..1091f9046260 100644
> --- a/kernel/sched/debug.c
> +++ b/kernel/sched/debug.c
> @@ -172,18 +172,12 @@ static const struct file_operations sched_feat_fops = {
>  static ssize_t sched_scaling_write(struct file *filp, const char __user *ubuf,
>  				   size_t cnt, loff_t *ppos)
>  {
> -	char buf[16];
>  	unsigned int scaling;
> +	int ret;
>  
> -	if (cnt > 15)
> -		cnt = 15;
> -
> -	if (copy_from_user(&buf, ubuf, cnt))
> -		return -EFAULT;
> -	buf[cnt] = '\0';
> -
> -	if (kstrtouint(buf, 10, &scaling))
> -		return -EINVAL;
> +	ret = kstrtouint_from_user(ubuf, cnt, 10, &scaling);
> +	if (ret < 0)
> +		return ret;

This thing returns 0 on success, so I think you could better do:

        if (ret)
                return ret;

With that, for the series:

Suggested-by: Yury Norov <ynorov@nvidia.com>
Reviewed-by: Yury Norov <ynorov@nvidia.com>

Thanks,
Yury

>  
>  	if (scaling >= SCHED_TUNABLESCALING_END)
>  		return -EINVAL;
> -- 
> 2.36.1
>