Skip to contentSkip to conversation

Convert copy_from_user() + kstrtouint() to kstrtouint_from_user()

PATCH Applied Fushuai Wang 2 participants 5 messages Jan 17 – May 25

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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
index 41caa22e0680..b27229a1922b 100644
@@ -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
merge bot Applied
#2 lore raw

[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
index 5f9b771..929fdf0 100644
@@ -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;
#3 lore raw

PATCH v2 2/2 x86/tlb: Convert copy_from_user() + kstrtouint() to kstrtouint_from_user()

In reply to Fushuai Wang

From: 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
index f5b93e01e347..5e6a07306214 100644
@@ -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
merge bot Applied
#4 lore raw

[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
index af43d17..ce2e2c4 100644
@@ -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;