Skip to contentSkip to conversation

watchdog: use kstrtouint to parse softlockup_panic=

PATCH Superseded 林濬哲 2 participants 8 messages Sep 21 – Sep 22

Subsystems (3)
lore
Share & tools
lore

林濬哲

author
#0 lore raw

PATCH 1/2 watchdog: use kstrtouint to parse softlockup_panic=

simple_strtoul() is deprecated. Replace it with kstrtouint(), which also reports parse errors back to the boot process instead of silently accepting garbage.

Assisted-by AI coding assistant (disclosed per kernel AI guidelines)
---
 kernel/watchdog.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
kernel/watchdog.c +5 -2
index e5134ad7b663..420f08140713 100644
@@ -17,6 +17,7 @@
 #include <linux/irq.h>
 #include <linux/irqdesc.h>
 #include <linux/kernel_stat.h>
+#include <linux/kstrtox.h>
 #include <linux/kvm_para.h>
 #include <linux/math64.h>
 #include <linux/mm.h>
@@ -422,8 +423,10 @@ static unsigned long soft_lockup_nmi_warn;
 
 static int __init softlockup_panic_setup(char *str)
 {
-	softlockup_panic = simple_strtoul(str, NULL, 0);
-	return 1;
+	int ret;
+
+	ret = kstrtouint(str, 0, &softlockup_panic);
+	return ret ? ret : 1;
 }
 __setup("softlockup_panic=", softlockup_panic_setup);
 
Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 23 lines · 林濬哲

simple_strtoul() is deprecated. Replace it with kstrtouint(), which also reports parse errors back to the boot process instead of silently accepting garbage.

Assisted-by AI coding assistant (disclosed per kernel AI guidelines)
---
 kernel/watchdog.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
kernel/watchdog.c +1 -0
index e5134ad7b663..420f08140713 100644
@@ -17,6 +17,7 @@
 #include <linux/irq.h>
 #include <linux/irqdesc.h>
 #include <linux/kernel_stat.h>
+#include <linux/kstrtox.h>
 #include <linux/kvm_para.h>
 #include <linux/math64.h>
 #include <linux/mm.h>

kernel/watchdog.c static unsigned long soft_lockup_nmi_warn; · line 429

Quoted 9 lines
kernel/watchdog.c +4 -2
@@ -422,8 +423,10 @@ static unsigned long soft_lockup_nmi_warn;
 
 static int __init softlockup_panic_setup(char *str)
 {
-	softlockup_panic = simple_strtoul(str, NULL, 0);
-	return 1;
+	int ret;
+
+	ret = kstrtouint(str, 0, &softlockup_panic);
+	return ret ? ret : 1;

Please read the documentation (or ask your AI to do it).

/*
* NOTE: __setup functions return values:
* @fn returns 1 (or non-zero) if the option argument is "handled"
* and returns 0 if the option argument is "not handled". */

There is no means to "reports parse errors back to the boot process" from __setup functions.

Guenter

author
#2 lore raw

PATCH v2 1/2 watchdog: use kstrtouint to parse softlockup_panic=

simple_strtoul() is deprecated. Replace it with kstrtouint(). A bad value now leaves the variable unchanged and produces a warning, instead of being silently clamped.

__setup() handlers return whether they handled the option; there is no way to propagate parse errors, so the handler simply returns 1.

v2: warn on a bad value and return 1 instead of returning the parse
error, which __setup() handlers cannot do (Guenter Roeck).

Assisted-by AI coding assistant (disclosed per kernel AI guidelines)
---
 kernel/watchdog.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/watchdog.c +6 -1
index e5134ad7b663..7f8c904554a9 100644
@@ -17,6 +17,7 @@
 #include <linux/irq.h>
 #include <linux/irqdesc.h>
 #include <linux/kernel_stat.h>
+#include <linux/kstrtox.h>
 #include <linux/kvm_para.h>
 #include <linux/math64.h>
 #include <linux/mm.h>
@@ -422,7 +423,11 @@ static unsigned long soft_lockup_nmi_warn;
 
 static int __init softlockup_panic_setup(char *str)
 {
-	softlockup_panic = simple_strtoul(str, NULL, 0);
+	int ret;
+
+	ret = kstrtouint(str, 0, &softlockup_panic);
+	if (ret)
+		pr_warn("softlockup_panic: bad option string '%s'\n", str);
 	return 1;
 }
 __setup("softlockup_panic=", softlockup_panic_setup);
Signature · 1 line
2.54.0.windows.1
maintainer
#3 lore raw
Quoted 30 lines · 林濬哲

simple_strtoul() is deprecated. Replace it with kstrtouint(). A bad value now leaves the variable unchanged and produces a warning, instead of being silently clamped.

__setup() handlers return whether they handled the option; there is no way to propagate parse errors, so the handler simply returns 1.

v2: warn on a bad value and return 1 instead of returning the parse
error, which __setup() handlers cannot do (Guenter Roeck).

Assisted-by AI coding assistant (disclosed per kernel AI guidelines)
---
 kernel/watchdog.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/watchdog.c +1 -0
index e5134ad7b663..7f8c904554a9 100644
@@ -17,6 +17,7 @@
 #include <linux/irq.h>
 #include <linux/irqdesc.h>
 #include <linux/kernel_stat.h>
+#include <linux/kstrtox.h>
 #include <linux/kvm_para.h>
 #include <linux/math64.h>
 #include <linux/mm.h>

kernel/watchdog.c static unsigned long soft_lockup_nmi_warn; · line 430

Quoted 9 lines
kernel/watchdog.c +5 -1
@@ -422,7 +423,11 @@ static unsigned long soft_lockup_nmi_warn;
 
 static int __init softlockup_panic_setup(char *str)
 {
-	softlockup_panic = simple_strtoul(str, NULL, 0);
+	int ret;
+
+	ret = kstrtouint(str, 0, &softlockup_panic);
+	if (ret)
+		pr_warn("softlockup_panic: bad option string '%s'\n", str);

Hardly any other __setup function warns here, and I do not see the point adding it here.

Guenter

Quoted 7 lines
kernel/watchdog.c +0 -0
 	return 1;
 }
 __setup("softlockup_panic=", softlockup_panic_setup);
Signature · 1 line
2.54.0.windows.1
author
#4 lore raw

PATCH v3 1/2 PM: hibernate: use kstrtouint to parse resume= device numbers

simple_strtoul() is deprecated. Replace the base-16 parse with kstrtouint(); the explicit trailing-character check the old code needed is provided by kstrtouint() itself. As a side effect, values wider than 32 bits are now rejected instead of being silently truncated before the dev_t conversion.

Assisted-by AI coding assistant (disclosed per kernel AI guidelines)
---
 kernel/power/hibernate.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
kernel/power/hibernate.c +7 -3
index d2479c69d71a..74f2d20518cc 100644
@@ -14,6 +14,7 @@
 #include <crypto/acompress.h>
 #include <linux/blkdev.h>
 #include <linux/export.h>
+#include <linux/kstrtox.h>
 #include <linux/suspend.h>
 #include <linux/reboot.h>
 #include <linux/string.h>
@@ -1278,7 +1279,7 @@ static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
 	error = lookup_bdev(name, &dev);
 	if (error) {
 		unsigned maj, min, offset;
-		char *p, dummy;
+		char dummy;
 
 		error = 0;
 		if (sscanf(name, "%u:%u%c", &maj, &min, &dummy) == 2 ||
@@ -1288,9 +1289,12 @@ static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
 			if (maj != MAJOR(dev) || min != MINOR(dev))
 				error = -EINVAL;
 		} else {
-			dev = new_decode_dev(simple_strtoul(name, &p, 16));
-			if (*p)
+			unsigned int val;
+
+			if (kstrtouint(name, 16, &val))
 				error = -EINVAL;
+			else
+				dev = new_decode_dev(val);
 		}
 	}
 	kfree(name);
Signature · 1 line
2.54.0.windows.1
author
#5 lore raw

PATCH v3 2/2 watchdog: use kstrtouint to parse softlockup_panic=

In reply to Guenter Roeck

simple_strtoul() is deprecated. Use kstrtouint() instead; on a parse error the variable is now left unchanged instead of being silently clamped.

__setup() handlers return whether they handled the option and cannot propagate parse errors, so the handler simply returns 1.

v3: do not warn on parse errors (Guenter Roeck).

Assisted-by AI coding assistant (per kernel AI guidelines)
---
 kernel/watchdog.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
kernel/watchdog.c +4 -3
index 420f08140713..69cd5ebb4929 100644
@@ -423,10 +423,11 @@ static unsigned long soft_lockup_nmi_warn;
 
 static int __init softlockup_panic_setup(char *str)
 {
-	int ret;
+	unsigned int val;
 
-	ret = kstrtouint(str, 0, &softlockup_panic);
-	return ret ? ret : 1;
+	if (!kstrtouint(str, 0, &val))
+		softlockup_panic = val;
+	return 1;
 }
 __setup("softlockup_panic=", softlockup_panic_setup);
 
Signature · 1 line
2.54.0.windows.1
maintainer
#6 lore raw
Quoted 22 lines · 林濬哲

simple_strtoul() is deprecated. Use kstrtouint() instead; on a parse error the variable is now left unchanged instead of being silently clamped.

__setup() handlers return whether they handled the option and cannot propagate parse errors, so the handler simply returns 1.

v3: do not warn on parse errors (Guenter Roeck).

Assisted-by AI coding assistant (per kernel AI guidelines)
---
 kernel/watchdog.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
kernel/watchdog.c +0 -0
index 420f08140713..69cd5ebb4929 100644
@@ -423,10 +423,11 @@ static unsigned long soft_lockup_nmi_warn;
 

kernel/watchdog.c static unsigned long soft_lockup_nmi_warn; · line 430

Quoted 10 lines
kernel/watchdog.c +4 -3
 static int __init softlockup_panic_setup(char *str)
 {
-	int ret;
+	unsigned int val;
 
-	ret = kstrtouint(str, 0, &softlockup_panic);
-	return ret ? ret : 1;
+	if (!kstrtouint(str, 0, &val))
+		softlockup_panic = val;
+	return 1;

This is not v3, it is a patch applied on top of a previous version, and it unnecessarily introduces a temporary variable.

Guenter

author
#7 lore raw

PATCH v2 2/2 PM: hibernate: use kstrtouint to parse resume= device numbers

In reply to Guenter Roeck

simple_strtoul() is deprecated. Replace the base-16 parse with kstrtouint(); the explicit trailing-character check the old code needed is provided by kstrtouint() itself. As a side effect, values wider than 32 bits are now rejected instead of being silently truncated before the dev_t conversion.

Assisted-by AI coding assistant (disclosed per kernel AI guidelines)
---
 kernel/power/hibernate.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
kernel/power/hibernate.c +7 -3
index d2479c69d71a..74f2d20518cc 100644
@@ -14,6 +14,7 @@
 #include <crypto/acompress.h>
 #include <linux/blkdev.h>
 #include <linux/export.h>
+#include <linux/kstrtox.h>
 #include <linux/suspend.h>
 #include <linux/reboot.h>
 #include <linux/string.h>
@@ -1278,7 +1279,7 @@ static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
 	error = lookup_bdev(name, &dev);
 	if (error) {
 		unsigned maj, min, offset;
-		char *p, dummy;
+		char dummy;
 
 		error = 0;
 		if (sscanf(name, "%u:%u%c", &maj, &min, &dummy) == 2 ||
@@ -1288,9 +1289,12 @@ static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
 			if (maj != MAJOR(dev) || min != MINOR(dev))
 				error = -EINVAL;
 		} else {
-			dev = new_decode_dev(simple_strtoul(name, &p, 16));
-			if (*p)
+			unsigned int val;
+
+			if (kstrtouint(name, 16, &val))
 				error = -EINVAL;
+			else
+				dev = new_decode_dev(val);
 		}
 	}
 	kfree(name);
Signature · 1 line
2.54.0.windows.1