Skip to contentSkip to conversation

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

PATCH Applied Jagdish Gediya 10 participants 18 messages Apr 26, 2022 – Aug 26, 2022

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

PATCH v3 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().

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

Changes in v3:
- Covert -> Convert in patch 2 subject
- Collected Reviewed-by: Matthew Wilcox (Oracle) <[email protected]>

 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 v3 2/2 mm: Convert 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.

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

Changes in v3:
- Covert -> Convert in patch 2 subject
- Collected Reviewed-by: Matthew Wilcox (Oracle) <[email protected]>

 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

From: Matthew Wilcox

Quoted 1 line

Sent: 26 April 2022 20:15

()

Quoted 10 lines · Jagdish Gediya
Quoted 2 lines

HEY! You still have the buggy IFF -> IF change. FIX IT. My R-b was very clearly conditional on you fixing it.

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

my 2c

Iff doesn't really go with an 'or' clause.
(With a maths degree I know what it means!)

so it probably reads better as 'if'.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#4 lore raw
In reply to Matthew Wilcox
Quoted 6 lines · Matthew Wilcox
Quoted 2 lines · Jagdish Gediya

HEY! You still have the buggy IFF -> IF change. FIX IT. My R-b was very clearly conditional on you fixing it.

My bad, I forgot to correct it. Andrew has corrected it.

Quoted 2 lines
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

#5 lore raw
In reply to Jagdish Gediya
Quoted 2 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

corresponding

Quoted 6 lines

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().

Andrew, this patch still needs a bit of work.

Quoted 21 lines

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

Changes in v3:
- Covert -> Convert in patch 2 subject
- Collected Reviewed-by: Matthew Wilcox (Oracle) <[email protected]>

 lib/kstrtox.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
lib/kstrtox.c +0 -0
index 886510d248e5..465e31e4d70d 100644

lib/kstrtox.c EXPORT_SYMBOL(kstrtos8); · line 343

Quoted 5 lines
lib/kstrtox.c +1 -1
@@ -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

s/if/iff/ please. It's _not_ a typo!

Quoted 22 lines
lib/kstrtox.c +4 -0
  * [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
Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#6 lore raw
Quoted 6 lines · On Wed, 27 Apr 2022 18:13:43 +0300 Andy Shevchenko…
Quoted 2 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

corresponding

Quoted 8 lines
Quoted 6 lines

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().

Andrew, this patch still needs a bit of work.

I fixed the two issues you identified in this email. Is there more to do?

Reviewed-by
#7 lore raw
Quoted 14 lines · Andrew Morton
Quoted 11 lines · On Wed, 27 Apr 2022 18:13:43 +0300 Andy Shevchenko…
Quoted 2 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

corresponding

Quoted 5 lines

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".

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

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

Andrew, this patch still needs a bit of work.

I fixed the two issues you identified in this email. Is there more to do?

I think the rest is fine, esp. taking into account Matthew's review. Feel free to add

(It might be better to check the second character as well to be sure we avoid typos like 'talse' and 'frue', anyone to comment?)

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#8 lore raw
Quoted 1 line · Andy Shevchenko

On Wed, Apr 27, 2022 at 8:06 PM Andrew Morton <[email protected]> wrote:

...

Quoted 2 lines

(It might be better to check the second character as well to be sure we avoid typos like 'talse' and 'frue', anyone to comment?)

Note that T and F on the keyboard are quite close to each other, so there is non-zero probability of the above mentioned typo(s).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#9 lore raw
In reply to Jagdish Gediya
Quoted 11 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().

I've just spotted that this broke arm64's "rodata=full" command line option, since "full" gets parsed as 'f' = FALSE, when previously that would have been rejected. So anyone passing "rodata=full" on the command line will have rodata disabled, which is not what they wanted.

The current state of things is a bit messy (we prase the option twice because arch code needs it early), and we can probably fix that with some refactoring, but I do wonder if we actually want to open up the sysfs parsing to accept anything *beginning* with [tTfF] rather than the full "true" and "false" strings as previously, or whether it's worth reverting this for now in case anything else is affected.

Mark.

Quoted 47 lines

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

Changes in v3:
- Covert -> Convert in patch 2 subject
- Collected Reviewed-by: Matthew Wilcox (Oracle) <[email protected]>

 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
#10 lore raw
Quoted 8 lines · Mark Rutland
Quoted 7 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".

Quoted 6 lines
Quoted 4 lines

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

I've just spotted that this broke arm64's "rodata=full" command line option,

That isn't a documented option.

        rodata=         [KNL]
                on      Mark read-only kernel memory as read-only (default).
                off     Leave read-only kernel memory writable for debugging.

Hopefully this is an object lesson in why you need to update the documentation when you extend a feature.

Quoted 10 lines

since "full" gets parsed as 'f' = FALSE, when previously that would have been rejected. So anyone passing "rodata=full" on the command line will have rodata disabled, which is not what they wanted.

The current state of things is a bit messy (we prase the option twice because arch code needs it early), and we can probably fix that with some refactoring, but I do wonder if we actually want to open up the sysfs parsing to accept anything *beginning* with [tTfF] rather than the full "true" and "false" strings as previously, or whether it's worth reverting this for now in case anything else is affected.

Well, that's going to break people who've started using the new option. As a quick fix, how about only allowing either "f\0" or "fa"?

#11 lore raw
Quoted 20 lines · Matthew Wilcox
Quoted 19 lines · Mark Rutland
Quoted 11 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().

I've just spotted that this broke arm64's "rodata=full" command line option,

since "full" gets parsed as 'f' = FALSE, when previously that would have been rejected. So anyone passing "rodata=full" on the command line will have rodata disabled, which is not what they wanted.

Quoted 8 lines
Quoted 6 lines

The current state of things is a bit messy (we prase the option twice because arch code needs it early), and we can probably fix that with some refactoring, but I do wonder if we actually want to open up the sysfs parsing to accept anything *beginning* with [tTfF] rather than the full "true" and "false" strings as previously, or whether it's worth reverting this for now in case anything else is affected.

Well, that's going to break people who've started using the new option.

Ah; I had mistakenly thought this was new in v5.19, and so a revert was fine. I see that it made it in for v5.18.

Quoted 1 line

As a quick fix, how about only allowing either "f\0" or "fa"?

TBH I reckon it's best to go for reworking the "rodata=" parsing, and backporting that to stable, since people could be relying on any "f*" string by now...

I'll have a go at that rework...

Mark.

#12 lore raw
In reply to Matthew Wilcox
Quoted 36 lines · Matthew Wilcox
Quoted 8 lines · Mark Rutland
Quoted 7 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".

Quoted 6 lines
Quoted 4 lines

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

I've just spotted that this broke arm64's "rodata=full" command line option,

That isn't a documented option.

        rodata=         [KNL]
                on      Mark read-only kernel memory as read-only (default).
                off     Leave read-only kernel memory writable for debugging.

Hopefully this is an object lesson in why you need to update the documentation when you extend a feature.

Quoted 10 lines

since "full" gets parsed as 'f' = FALSE, when previously that would have been rejected. So anyone passing "rodata=full" on the command line will have rodata disabled, which is not what they wanted.

The current state of things is a bit messy (we prase the option twice because arch code needs it early), and we can probably fix that with some refactoring, but I do wonder if we actually want to open up the sysfs parsing to accept anything *beginning* with [tTfF] rather than the full "true" and "false" strings as previously, or whether it's worth reverting this for now in case anything else is affected.

Quoted 2 lines

Well, that's going to break people who've started using the new option. As a quick fix, how about only allowing either "f\0" or "fa"?

I think we need to be more strict in kstrtobool(), i.e. 'f\0' ('t\0') and 'fal' ('tru') perhaps?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#13 lore raw
Quoted 37 lines · Andy Shevchenko
Quoted 36 lines · Matthew Wilcox
Quoted 8 lines · Mark Rutland
Quoted 7 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".

Quoted 6 lines
Quoted 4 lines

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

I've just spotted that this broke arm64's "rodata=full" command line option,

That isn't a documented option.

        rodata=         [KNL]
                on      Mark read-only kernel memory as read-only (default).
                off     Leave read-only kernel memory writable for debugging.

Hopefully this is an object lesson in why you need to update the documentation when you extend a feature.

Quoted 10 lines

since "full" gets parsed as 'f' = FALSE, when previously that would have been rejected. So anyone passing "rodata=full" on the command line will have rodata disabled, which is not what they wanted.

The current state of things is a bit messy (we prase the option twice because arch code needs it early), and we can probably fix that with some refactoring, but I do wonder if we actually want to open up the sysfs parsing to accept anything *beginning* with [tTfF] rather than the full "true" and "false" strings as previously, or whether it's worth reverting this for now in case anything else is affected.

Quoted 5 lines
Quoted 2 lines

Well, that's going to break people who've started using the new option. As a quick fix, how about only allowing either "f\0" or "fa"?

I think we need to be more strict in kstrtobool(), i.e. 'f\0' ('t\0') and 'fal' ('tru') perhaps?

Actually kstrtobool() has been designed as a generic parser that should have lowest priority. It means that the code that uses it should take care of any other custom cases _before_ calling for kstrtobool().

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#14 lore raw
Quoted 11 lines · Andy Shevchenko
Quoted 10 lines · Andy Shevchenko
Quoted 9 lines · Matthew Wilcox
Quoted 8 lines · Mark Rutland
Quoted 7 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".

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

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

I've just spotted that this broke arm64's "rodata=full" command line option,

The problem still seems to be in mainline.

Quoted 23 lines
Quoted 23 lines
Quoted 22 lines

That isn't a documented option.

        rodata=         [KNL]
                on      Mark read-only kernel memory as read-only (default).
                off     Leave read-only kernel memory writable for debugging.

Hopefully this is an object lesson in why you need to update the documentation when you extend a feature.

Quoted 10 lines

since "full" gets parsed as 'f' = FALSE, when previously that would have been rejected. So anyone passing "rodata=full" on the command line will have rodata disabled, which is not what they wanted.

The current state of things is a bit messy (we prase the option twice because arch code needs it early), and we can probably fix that with some refactoring, but I do wonder if we actually want to open up the sysfs parsing to accept anything *beginning* with [tTfF] rather than the full "true" and "false" strings as previously, or whether it's worth reverting this for now in case anything else is affected.

Well, that's going to break people who've started using the new option. As a quick fix, how about only allowing either "f\0" or "fa"?

Quoted 6 lines
Quoted 2 lines

I think we need to be more strict in kstrtobool(), i.e. 'f\0' ('t\0') and 'fal' ('tru') perhaps?

Actually kstrtobool() has been designed as a generic parser that should have lowest priority. It means that the code that uses it should take care of any other custom cases _before_ calling for kstrtobool().

Makes sense.

Jagdish, could you please send a patch fixing the "rodata" early_param in arch/arm64/mm/mmu.c?

Please, also add:

Cc [email protected] # v5.19+

Best Regards,
Petr

#15 lore raw
Quoted 5 lines · Petr Mladek
Quoted 3 lines · Andy Shevchenko

Actually kstrtobool() has been designed as a generic parser that should have lowest priority. It means that the code that uses it should take care of any other custom cases _before_ calling for kstrtobool().

Quoted 8 lines

Makes sense.

Jagdish, could you please send a patch fixing the "rodata" early_param in arch/arm64/mm/mmu.c?

Please, also add:

Cc [email protected] # v5.19+

I have a fix already queued here:

https://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux.git/commit/?h=for-next/fixes&id=2e8cff0a0eee87b27f0cf87ad8310eb41b5886ab

Sadly, it's missing the stable tag, but we can always send it manually if the robot doesn't grab it.

Will

#16 lore raw
In reply to Andy Shevchenko
Quoted 37 lines · Andy Shevchenko
Quoted 36 lines · Matthew Wilcox
Quoted 8 lines · Mark Rutland
Quoted 7 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".

Quoted 6 lines
Quoted 4 lines

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

I've just spotted that this broke arm64's "rodata=full" command line option,

That isn't a documented option.

        rodata=         [KNL]
                on      Mark read-only kernel memory as read-only (default).
                off     Leave read-only kernel memory writable for debugging.

Hopefully this is an object lesson in why you need to update the documentation when you extend a feature.

Quoted 10 lines

since "full" gets parsed as 'f' = FALSE, when previously that would have been rejected. So anyone passing "rodata=full" on the command line will have rodata disabled, which is not what they wanted.

The current state of things is a bit messy (we prase the option twice because arch code needs it early), and we can probably fix that with some refactoring, but I do wonder if we actually want to open up the sysfs parsing to accept anything *beginning* with [tTfF] rather than the full "true" and "false" strings as previously, or whether it's worth reverting this for now in case anything else is affected.

Quoted 5 lines
Quoted 2 lines

Well, that's going to break people who've started using the new option. As a quick fix, how about only allowing either "f\0" or "fa"?

I think we need to be more strict in kstrtobool(), i.e. 'f\0' ('t\0') and 'fal' ('tru') perhaps?

lol what?

the only way to be strict is to accept "0" and "1" with optional newline and delete all the rubbish entirely.

#17 lore raw
Quoted 3 lines · Alexey Dobriyan
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Matthew Wilcox

On Mon, Jul 25, 2022 at 03:55:27PM +0100, Mark Rutland wrote:

...

Quoted 10 lines
Quoted 5 lines
Quoted 2 lines

Well, that's going to break people who've started using the new option. As a quick fix, how about only allowing either "f\0" or "fa"?

I think we need to be more strict in kstrtobool(), i.e. 'f\0' ('t\0') and 'fal' ('tru') perhaps?

lol what?

the only way to be strict is to accept "0" and "1" with optional newline and delete all the rubbish entirely.

You have an anti-user mindset. Be more constructive, please.

Signature · 2 lines
With Best Regards,
Andy Shevchenko