Skip to contentSkip to conversation

lib/kstrtox.c: Add "false"/"true" support to kstrtobool()

PATCH Superseded Jagdish Gediya 5 participants 9 messages Apr 26, 2022 – May 16, 2022

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 lib/kstrtox.c: Add "false"/"true" support to kstrtobool()

At many places in kernel, It is necessary to convert sysfs input to corrosponding bool value e.g. "false" or "0" need to be converted to bool false, "true" or "1" need to be converted to bool true, places where such conversion is needed currently check the input string manually, kstrtobool() can be utilized at such places but currently it doesn't have support to accept "false"/"true".

Add support to accept "false"/"true" as valid string in kstrtobool().

---
Chnage in v2:
- kstrtobool to kstrtobool() in commit message.
- Split single patch into 2
- Remove strcmp usage and instead compare 1st character only.

 lib/kstrtox.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
lib/kstrtox.c +5 -1
index 886510d248e5..465e31e4d70d 100644
@@ -340,7 +340,7 @@ EXPORT_SYMBOL(kstrtos8);
  * @s: input string
  * @res: result
  *
- * This routine returns 0 iff the first character is one of 'Yy1Nn0', or
+ * This routine returns 0 if the first character is one of 'YyTt1NnFf0', or
  * [oO][NnFf] for "on" and "off". Otherwise it will return -EINVAL.  Value
  * pointed to by res is updated upon finding a match.
  */
@@ -353,11 +353,15 @@ int kstrtobool(const char *s, bool *res)
 	switch (s[0]) {
 	case 'y':
 	case 'Y':
+	case 't':
+	case 'T':
 	case '1':
 		*res = true;
 		return 0;
 	case 'n':
 	case 'N':
+	case 'f':
+	case 'F':
 	case '0':
 		*res = false;
 		return 0;
Signature · 1 line
2.35.1
#1 lore raw

PATCH v2 2/2 mm: Covert sysfs input to bool using kstrtobool()

Sysfs input conversion to corrosponding bool value e.g. "false" or "0" to false, "true" or "1" to true are currently handled through strncmp at multiple places. Use kstrtobool() to convert sysfs input to bool value.

---
Chnage in v2:
- kstrtobool to kstrtobool() in commit message.
- Split single patch into 2
- Remove strcmp usage and instead compare 1st character only.

 mm/migrate.c    | 6 +-----
 mm/swap_state.c | 6 +-----
 2 files changed, 2 insertions(+), 10 deletions(-)
mm/migrate.c +1 -5
index 6c31ee1e1c9b..1de39bbfd6f9 100644
@@ -2523,11 +2523,7 @@ static ssize_t numa_demotion_enabled_store(struct kobject *kobj,
 					   struct kobj_attribute *attr,
 					   const char *buf, size_t count)
 {
-	if (!strncmp(buf, "true", 4) || !strncmp(buf, "1", 1))
-		numa_demotion_enabled = true;
-	else if (!strncmp(buf, "false", 5) || !strncmp(buf, "0", 1))
-		numa_demotion_enabled = false;
-	else
+	if (kstrtobool(buf, &numa_demotion_enabled))
 		return -EINVAL;
 
 	return count;
mm/swap_state.c +1 -5
index 013856004825..dba10045a825 100644
@@ -865,11 +865,7 @@ static ssize_t vma_ra_enabled_store(struct kobject *kobj,
 				      struct kobj_attribute *attr,
 				      const char *buf, size_t count)
 {
-	if (!strncmp(buf, "true", 4) || !strncmp(buf, "1", 1))
-		enable_vma_readahead = true;
-	else if (!strncmp(buf, "false", 5) || !strncmp(buf, "0", 1))
-		enable_vma_readahead = false;
-	else
+	if (kstrtobool(buf, &enable_vma_readahead))
 		return -EINVAL;
 
 	return count;
Signature · 1 line
2.35.1
#3 lore raw

Re: [PATCH v2 2/2] mm: Covert sysfs input to bool using kstrtobool()

In reply to Jagdish Gediya
Quoted 4 lines · Jagdish Gediya

Sysfs input conversion to corrosponding bool value e.g. "false" or "0" to false, "true" or "1" to true are currently handled through strncmp at multiple places. Use kstrtobool() to convert sysfs input to bool value.

...

mm/migrate.c static ssize_t numa_demotion_enabled_store(struct kobject *kobj, · line 2527

Quoted 2 lines
mm/migrate.c +1 -0
+	if (kstrtobool(buf, &numa_demotion_enabled))
 		return -EINVAL;

Hmm... The commit message doesn't explain what's wrong with the error codes returned by kstrtobool(). Can't it be

	ret = kstrtobool();
	if (ret)
		return ret;

?

...

mm/swap_state.c static ssize_t vma_ra_enabled_store(struct kobject *kobj, · line 869

Quoted 2 lines
mm/swap_state.c +1 -0
+	if (kstrtobool(buf, &enable_vma_readahead))
 		return -EINVAL;

Ditto.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#4 lore raw

Re: [PATCH v2 2/2] mm: Covert sysfs input to bool using kstrtobool()

Quoted 14 lines · On Wed, 27 Apr 2022 18:17:29 +0300 Andy Shevchenko…
Quoted 4 lines · Jagdish Gediya

Sysfs input conversion to corrosponding bool value e.g. "false" or "0" to false, "true" or "1" to true are currently handled through strncmp at multiple places. Use kstrtobool() to convert sysfs input to bool value.

...

Review

Quoted 2 lines
(quoted diff) +1 -0
+	if (kstrtobool(buf, &numa_demotion_enabled))
 		return -EINVAL;

Hmm... The commit message doesn't explain what's wrong with the error codes returned by kstrtobool(). Can't it be

Quoted 5 lines
	ret = kstrtobool();
	if (ret)
		return ret;

?

Jagdish fell asleep.

Yes, I agree. It has no practical difference at present because kstrtobool() can only return 0 or -EINVAL. I did this:

mm/migrate.c renamed from mm/migrate.c~mm-convert-sysfs-input-to-bool-using-kstrtobool-fix +4 -2
@@ -2523,8 +2523,10 @@ static ssize_t numa_demotion_enabled_sto
 					   struct kobj_attribute *attr,
 					   const char *buf, size_t count)
 {
-	if (kstrtobool(buf, &numa_demotion_enabled))
-		return -EINVAL;
+	ssize_t ret;
+
+	ret = kstrtobool(buf, &numa_demotion_enabled);
+		return ret;
 
 	return count;
 }
mm/swap_state.c renamed from mm/swap_state.c~mm-convert-sysfs-input-to-bool-using-kstrtobool-fix +5 -2
@@ -874,8 +874,11 @@ static ssize_t vma_ra_enabled_store(stru
 				      struct kobj_attribute *attr,
 				      const char *buf, size_t count)
 {
-	if (kstrtobool(buf, &enable_vma_readahead))
-		return -EINVAL;
+	ssize_t ret;
+
+	ret = kstrtobool(buf, &enable_vma_readahead);
+	if (ret)
+		return ret;
 
 	return count;
 }

_

#5 lore raw

Re: [PATCH v2 2/2] mm: Covert sysfs input to bool using kstrtobool()

Quoted 20 lines · Andrew Morton
Quoted 18 lines · On Wed, 27 Apr 2022 18:17:29 +0300 Andy Shevchenko…
Quoted 4 lines · Jagdish Gediya

Sysfs input conversion to corrosponding bool value e.g. "false" or "0" to false, "true" or "1" to true are currently handled through strncmp at multiple places. Use kstrtobool() to convert sysfs input to bool value.

...

Review

Quoted 2 lines
(quoted diff) +1 -0
+	if (kstrtobool(buf, &numa_demotion_enabled))
 		return -EINVAL;

Hmm... The commit message doesn't explain what's wrong with the error codes returned by kstrtobool(). Can't it be

	ret = kstrtobool();
	if (ret)
		return ret;
Quoted 6 lines
Quoted 1 line

?

Jagdish fell asleep.

Yes, I agree. It has no practical difference at present because kstrtobool() can only return 0 or -EINVAL. I did this:

	ret = f();
	if (ret)
		return ret;

is slightly better because it doesn't require to load EINVAL into register:

	call f
	test	eax, eax
	js	1f
	...

1:
ret

#6 lore raw

Re: [PATCH v2 2/2] mm: Covert sysfs input to bool using kstrtobool()

In reply to Andrew Morton
Quoted 16 lines · Andrew Morton
Quoted 14 lines · On Wed, 27 Apr 2022 18:17:29 +0300 Andy Shevchenko…
Quoted 4 lines · Jagdish Gediya

Sysfs input conversion to corrosponding bool value e.g. "false" or "0" to false, "true" or "1" to true are currently handled through strncmp at multiple places. Use kstrtobool() to convert sysfs input to bool value.

...

Review

Quoted 2 lines
(quoted diff) +1 -0
+	if (kstrtobool(buf, &numa_demotion_enabled))
 		return -EINVAL;

Hmm... The commit message doesn't explain what's wrong with the error codes returned by kstrtobool(). Can't it be

Quoted 7 lines
Quoted 5 lines
	ret = kstrtobool();
	if (ret)
		return ret;

?

Jagdish fell asleep.

Sorry, I was away from work for few days.

Quoted 36 lines

Yes, I agree. It has no practical difference at present because kstrtobool() can only return 0 or -EINVAL. I did this:

mm/migrate.c renamed from mm/migrate.c~mm-convert-sysfs-input-to-bool-using-kstrtobool-fix +4 -2
@@ -2523,8 +2523,10 @@ static ssize_t numa_demotion_enabled_sto
 					   struct kobj_attribute *attr,
 					   const char *buf, size_t count)
 {
-	if (kstrtobool(buf, &numa_demotion_enabled))
-		return -EINVAL;
+	ssize_t ret;
+
+	ret = kstrtobool(buf, &numa_demotion_enabled);
+		return ret;
 
 	return count;
 }
mm/swap_state.c renamed from mm/swap_state.c~mm-convert-sysfs-input-to-bool-using-kstrtobool-fix +5 -2
@@ -874,8 +874,11 @@ static ssize_t vma_ra_enabled_store(stru
 				      struct kobj_attribute *attr,
 				      const char *buf, size_t count)
 {
-	if (kstrtobool(buf, &enable_vma_readahead))
-		return -EINVAL;
+	ssize_t ret;
+
+	ret = kstrtobool(buf, &enable_vma_readahead);
+	if (ret)
+		return ret;
 
 	return count;
 }

_

#7 lore raw

Re: [PATCH v2 2/2] mm: Covert sysfs input to bool using kstrtobool()

In reply to Andy Shevchenko
Quoted 14 lines · Andy Shevchenko
Quoted 4 lines · Jagdish Gediya

Sysfs input conversion to corrosponding bool value e.g. "false" or "0" to false, "true" or "1" to true are currently handled through strncmp at multiple places. Use kstrtobool() to convert sysfs input to bool value.

...

mm/migrate.c static ssize_t numa_demotion_enabled_store(struct kobject *kobj, · line 2527

Quoted 2 lines
mm/migrate.c +1 -0
+	if (kstrtobool(buf, &numa_demotion_enabled))
 		return -EINVAL;

Hmm... The commit message doesn't explain what's wrong with the error codes returned by kstrtobool(). Can't it be

Quoted 5 lines
	ret = kstrtobool();
	if (ret)
		return ret;

?

Sorry for the late reply, I was away from work for few days. Yes, It can be like what you mentioned.

Quoted 13 lines

...

mm/swap_state.c static ssize_t vma_ra_enabled_store(struct kobject *kobj, · line 869

Quoted 2 lines
mm/swap_state.c +1 -0
+	if (kstrtobool(buf, &enable_vma_readahead))
 		return -EINVAL;

Ditto.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Reviewed-by on v2 1/2
#8 lore raw
In reply to Jagdish Gediya
Quoted 10 lines · Jagdish Gediya

At many places in kernel, It is necessary to convert sysfs input to corrosponding bool value e.g. "false" or "0" need to be converted to bool false, "true" or "1" need to be converted to bool true, places where such conversion is needed currently check the input string manually, kstrtobool() can be utilized at such places but currently it doesn't have support to accept "false"/"true".

Add support to accept "false"/"true" as valid string in kstrtobool().

... except ...

Quoted 2 lines

- * This routine returns 0 iff the first character is one of 'Yy1Nn0', or
+ * This routine returns 0 if the first character is one of 'YyTt1NnFf0', or

https://en.wikipedia.org/wiki/If_and_only_if