Convert copy_from_user() + kstrtouint() to kstrtouint_from_user()
v2 cover Convert copy_from_user() + kstrtouint() to kstrtouint_from_user()
From: Fushuai Wang <[email protected]>
Hi,
There are several places use copy_from_user() to copy a string from userspase, followed by kstrtoXXX() to convert it to a desired type. This approach requires manually handling NUL-termination and proper buffer sizing, which is error-prone and unnecessarily complex. The kernel provides kstrtoXXX_from_user() helpers to copy a string from userspace and perform the conversion in one step. So use a kstrtoXXX_from_user() instead of copy_from_user() + kstrtoXXX().
Recent discussion:
https://lore.kernel.org/all/[email protected]/T/
v1:
https://lore.kernel.org/all/[email protected]/T/#t
Fushuai Wang (2):
sched/debug: Convert copy_from_user() + kstrtouint() to
kstrtouint_from_user()
x86/tlb: Convert copy_from_user() + kstrtouint() to
kstrtouint_from_user()
arch/x86/mm/tlb.c | 13 ++++--------- kernel/sched/debug.c | 14 ++++---------- 2 files changed, 8 insertions(+), 19 deletions(-)
Signature · 1 line
2.36.1
PATCH v2 1/2 sched/debug: Convert copy_from_user() + kstrtouint() to kstrtouint_from_user()
From: Fushuai Wang <[email protected]>
Using kstrtouint_from_user() instead of copy_from_user() + kstrtouint() makes the code simpler and less error-prone.
--- kernel/sched/debug.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-)
kernel/sched/debug.c +4 -10
@@ -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) +return ret;if (scaling >=SCHED_TUNABLESCALING_END )return -EINVAL ;
Signature · 1 line
2.36.1
[tip: sched/core] sched/debug: Convert copy_from_user() + kstrtouint() to kstrtouint_from_user()
The following commit has been merged into the sched/core branch of tip:
The following commit has been merged into the sched/core branch of tip:
Commit-ID: 4fe82cf3024a4bdd2571d584efc25598533d5c96 Gitweb: https://git.kernel.org/tip/4fe82cf3024a4bdd2571d584efc25598533d5c96 Author: Fushuai Wang <[email protected]> AuthorDate: Sat, 17 Jan 2026 22:56:14 +08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Thu, 22 Jan 2026 11:11:16 +01:00
sched/debug: Convert copy_from_user() + kstrtouint() to kstrtouint_from_user()
Using kstrtouint_from_user() instead of copy_from_user() + kstrtouint() makes the code simpler and less error-prone.
--- kernel/sched/debug.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-)
kernel/sched/debug.c +4 -10
@@ -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) +return ret;if (scaling >=SCHED_TUNABLESCALING_END )return -EINVAL ;
PATCH v2 2/2 x86/tlb: Convert copy_from_user() + kstrtouint() to kstrtouint_from_user()
In reply to Fushuai WangFrom: Fushuai Wang <[email protected]>
Using kstrtouint_from_user() instead of copy_from_user() + kstrtouint() makes the code simpler and less error-prone.
--- arch/x86/mm/tlb.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)
arch/x86/mm/tlb.c +4 -9
@@ -1802,17 +1802,12 @@ static ssize_t tlbflush_read_file(struct file *file, char __user *user_buf,static ssize_t tlbflush_write_file (struct file *file,const char __user *user_buf,size_t count,loff_t *ppos) { -char buf[32 ]; -ssize_t len;int ceiling; +int err; - len =min (count,sizeof (buf) -1 ); -if (copy_from_user (buf, user_buf, len)) -return -EFAULT ; - - buf[len] ='\0' ; -if (kstrtoint (buf,0 , &ceiling)) -return -EINVAL ; + err =kstrtoint_from_user (user_buf, count,0 , &ceiling); +if (err) +return err;if (ceiling <0 )return -EINVAL ;
Signature · 1 line
2.36.1
[tip: x86/cleanups] x86/tlb: Convert copy_from_user() + kstrtouint() to kstrtouint_from_user()
The following commit has been merged into the x86/cleanups branch of tip:
The following commit has been merged into the x86/cleanups branch of tip:
Commit-ID: 648fb97ee908b602f507e8b1f1ae98dd6342f05d Gitweb: https://git.kernel.org/tip/648fb97ee908b602f507e8b1f1ae98dd6342f05d Author: Fushuai Wang <[email protected]> AuthorDate: Sat, 17 Jan 2026 22:56:15 +08:00 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Sun, 24 May 2026 16:08:38 -07:00
x86/tlb: Convert copy_from_user() + kstrtouint() to kstrtouint_from_user()
Using kstrtouint_from_user() instead of copy_from_user() + kstrtouint() makes the code simpler and less error-prone.
No functional changes.
[ bp: Align function args on opening brace, while at it. ]
--- arch/x86/mm/tlb.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-)
arch/x86/mm/tlb.c +7 -12
@@ -1769,7 +1769,7 @@ bool nmi_uaccess_okay(void) }static ssize_t tlbflush_read_file (struct file *file,char __user *user_buf, -size_t count,loff_t *ppos) +size_t count,loff_t *ppos) {char buf[32 ];unsigned int len;
@@ -1778,20 +1778,15 @@ static ssize_t tlbflush_read_file(struct file *file, char __user *user_buf,return simple_read_from_buffer (user_buf, count, ppos, buf, len); } -static ssize_t tlbflush_write_file (struct file *file, -const char __user *user_buf,size_t count,loff_t *ppos) +static ssize_t tlbflush_write_file (struct file *file,const char __user *user_buf, +size_t count,loff_t *ppos) { -char buf[32 ]; -ssize_t len;int ceiling; +int err; - len =min (count,sizeof (buf) -1 ); -if (copy_from_user (buf, user_buf, len)) -return -EFAULT ; - - buf[len] ='\0' ; -if (kstrtoint (buf,0 , &ceiling)) -return -EINVAL ; + err =kstrtoint_from_user (user_buf, count,0 , &ceiling); +if (err) +return err;if (ceiling <0 )return -EINVAL ;