mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/2] Convert copy_from_user() + kstrtouint() to kstrtouint_from_user()
@ 2026-01-17 14:56 Fushuai Wang
  2026-01-17 14:56 ` [PATCH v2 1/2] sched/debug: " Fushuai Wang
  2026-01-17 14:56 ` [PATCH v2 2/2] x86/tlb: " Fushuai Wang
  0 siblings, 2 replies; 5+ messages in thread
From: Fushuai Wang @ 2026-01-17 14:56 UTC (permalink / raw)
  To: mingo, peterz, juri.lelli, vincent.guittot, dietmar.eggemann,
	rostedt, bsegall, mgorman, vschneid, dave.hansen, luto, tglx, bp,
	hpa
  Cc: linux-kernel, x86, wangfushuai, aliceryhl, ynorov

From: Fushuai Wang <wangfushuai@baidu.com>

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/20260112073039.1185-1-fushuai.wang@linux.dev/T/

v1:
https://lore.kernel.org/all/20260116131751.45768-1-fushuai.wang@linux.dev/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(-)

-- 
2.36.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 1/2] sched/debug: Convert copy_from_user() + kstrtouint() to kstrtouint_from_user()
  2026-01-17 14:56 [PATCH v2 0/2] Convert copy_from_user() + kstrtouint() to kstrtouint_from_user() Fushuai Wang
@ 2026-01-17 14:56 ` Fushuai Wang
  2026-01-22 10:16   ` [tip: sched/core] " tip-bot2 for Fushuai Wang
  2026-01-17 14:56 ` [PATCH v2 2/2] x86/tlb: " Fushuai Wang
  1 sibling, 1 reply; 5+ messages in thread
From: Fushuai Wang @ 2026-01-17 14:56 UTC (permalink / raw)
  To: mingo, peterz, juri.lelli, vincent.guittot, dietmar.eggemann,
	rostedt, bsegall, mgorman, vschneid, dave.hansen, luto, tglx, bp,
	hpa
  Cc: linux-kernel, x86, wangfushuai, aliceryhl, ynorov

From: Fushuai Wang <wangfushuai@baidu.com>

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

Suggested-by: Yury Norov <ynorov@nvidia.com>
Signed-off-by: Fushuai Wang <wangfushuai@baidu.com>
Reviewed-by: Yury Norov <ynorov@nvidia.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..b27229a1922b 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)
+		return ret;
 
 	if (scaling >= SCHED_TUNABLESCALING_END)
 		return -EINVAL;
-- 
2.36.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 2/2] x86/tlb: Convert copy_from_user() + kstrtouint() to kstrtouint_from_user()
  2026-01-17 14:56 [PATCH v2 0/2] Convert copy_from_user() + kstrtouint() to kstrtouint_from_user() Fushuai Wang
  2026-01-17 14:56 ` [PATCH v2 1/2] sched/debug: " Fushuai Wang
@ 2026-01-17 14:56 ` Fushuai Wang
  2026-05-25  0:18   ` [tip: x86/cleanups] " tip-bot2 for Fushuai Wang
  1 sibling, 1 reply; 5+ messages in thread
From: Fushuai Wang @ 2026-01-17 14:56 UTC (permalink / raw)
  To: mingo, peterz, juri.lelli, vincent.guittot, dietmar.eggemann,
	rostedt, bsegall, mgorman, vschneid, dave.hansen, luto, tglx, bp,
	hpa
  Cc: linux-kernel, x86, wangfushuai, aliceryhl, ynorov

From: Fushuai Wang <wangfushuai@baidu.com>

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

Suggested-by: Yury Norov <ynorov@nvidia.com>
Signed-off-by: Fushuai Wang <wangfushuai@baidu.com>
Reviewed-by: Yury Norov <ynorov@nvidia.com>
---
 arch/x86/mm/tlb.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c
index f5b93e01e347..5e6a07306214 100644
--- a/arch/x86/mm/tlb.c
+++ b/arch/x86/mm/tlb.c
@@ -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;
-- 
2.36.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [tip: sched/core] sched/debug: Convert copy_from_user() + kstrtouint() to kstrtouint_from_user()
  2026-01-17 14:56 ` [PATCH v2 1/2] sched/debug: " Fushuai Wang
@ 2026-01-22 10:16   ` tip-bot2 for Fushuai Wang
  0 siblings, 0 replies; 5+ messages in thread
From: tip-bot2 for Fushuai Wang @ 2026-01-22 10:16 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Yury Norov, Fushuai Wang, Peter Zijlstra (Intel), x86, linux-kernel

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 <wangfushuai@baidu.com>
AuthorDate:    Sat, 17 Jan 2026 22:56:14 +08:00
Committer:     Peter Zijlstra <peterz@infradead.org>
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.

Suggested-by: Yury Norov <ynorov@nvidia.com>
Signed-off-by: Fushuai Wang <wangfushuai@baidu.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Yury Norov <ynorov@nvidia.com>
Link: https://patch.msgid.link/20260117145615.53455-2-fushuai.wang@linux.dev
---
 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 5f9b771..929fdf0 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)
+		return ret;
 
 	if (scaling >= SCHED_TUNABLESCALING_END)
 		return -EINVAL;

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [tip: x86/cleanups] x86/tlb: Convert copy_from_user() + kstrtouint() to kstrtouint_from_user()
  2026-01-17 14:56 ` [PATCH v2 2/2] x86/tlb: " Fushuai Wang
@ 2026-05-25  0:18   ` tip-bot2 for Fushuai Wang
  0 siblings, 0 replies; 5+ messages in thread
From: tip-bot2 for Fushuai Wang @ 2026-05-25  0:18 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Yury Norov, Fushuai Wang, Borislav Petkov (AMD), x86, linux-kernel

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 <wangfushuai@baidu.com>
AuthorDate:    Sat, 17 Jan 2026 22:56:15 +08:00
Committer:     Borislav Petkov (AMD) <bp@alien8.de>
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. ]

Suggested-by: Yury Norov <ynorov@nvidia.com>
Signed-off-by: Fushuai Wang <wangfushuai@baidu.com>
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Reviewed-by: Yury Norov <ynorov@nvidia.com>
Link: https://patch.msgid.link/20260117145615.53455-3-fushuai.wang@linux.dev
---
 arch/x86/mm/tlb.c | 19 +++++++------------
 1 file changed, 7 insertions(+), 12 deletions(-)

diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c
index af43d17..ce2e2c4 100644
--- a/arch/x86/mm/tlb.c
+++ b/arch/x86/mm/tlb.c
@@ -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;

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-05-25  0:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-01-17 14:56 [PATCH v2 0/2] Convert copy_from_user() + kstrtouint() to kstrtouint_from_user() Fushuai Wang
2026-01-17 14:56 ` [PATCH v2 1/2] sched/debug: " Fushuai Wang
2026-01-22 10:16   ` [tip: sched/core] " tip-bot2 for Fushuai Wang
2026-01-17 14:56 ` [PATCH v2 2/2] x86/tlb: " Fushuai Wang
2026-05-25  0:18   ` [tip: x86/cleanups] " tip-bot2 for Fushuai Wang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®