Skip to contentSkip to conversation

Use existing helper to convert "on/off" to boolean

PATCH Applied Minfei Huang 3 participants 6 messages Apr 29, 2016 – Apr 30, 2016

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

PATCH 1/1 Use existing helper to convert "on/off" to boolean

It's more convenient to use existing function helper to convert string "on/off" to boolean.

---
 lib/kstrtox.c    | 2 +-
 mm/page_alloc.c  | 9 +--------
 mm/page_poison.c | 8 +-------
 3 files changed, 3 insertions(+), 16 deletions(-)
lib/kstrtox.c +1 -1
index d8a5cf6..3c66fc4 100644
@@ -326,7 +326,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 'Yy1Nn0', or
  * [oO][NnFf] for "on" and "off". Otherwise it will return -EINVAL.  Value
  * pointed to by res is updated upon finding a match.
  */
mm/page_alloc.c +1 -8
index 59de90d..d31426d 100644
@@ -513,14 +513,7 @@ static int __init early_debug_pagealloc(char *buf)
 {
 	if (!buf)
 		return -EINVAL;
-
-	if (strcmp(buf, "on") == 0)
-		_debug_pagealloc_enabled = true;
-
-	if (strcmp(buf, "off") == 0)
-		_debug_pagealloc_enabled = false;
-
-	return 0;
+	return kstrtobool(buf, &_debug_pagealloc_enabled);
 }
 early_param("debug_pagealloc", early_debug_pagealloc);
 
mm/page_poison.c +1 -7
index 479e7ea..1eae5fa 100644
@@ -13,13 +13,7 @@ static int early_page_poison_param(char *buf)
 {
 	if (!buf)
 		return -EINVAL;
-
-	if (strcmp(buf, "on") == 0)
-		want_page_poisoning = true;
-	else if (strcmp(buf, "off") == 0)
-		want_page_poisoning = false;
-
-	return 0;
+	return strtobool(buf, &want_page_poisoning);
 }
 early_param("page_poison", early_page_poison_param);
 
Signature · 7 lines
2.6.3

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#1 lore raw
Quoted 2 lines · Minfei Huang

It's more convenient to use existing function helper to convert string "on/off" to boolean.

But kstrtobool in linux-next only does "This routine returns 0 iff the first character is one of 'Yy1Nn0'" so it doesn't know about on/off. Or am I missing anything?

Quoted 63 lines
---
 lib/kstrtox.c    | 2 +-
 mm/page_alloc.c  | 9 +--------
 mm/page_poison.c | 8 +-------
 3 files changed, 3 insertions(+), 16 deletions(-)
lib/kstrtox.c +1 -1
index d8a5cf6..3c66fc4 100644
@@ -326,7 +326,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 'Yy1Nn0', or
  * [oO][NnFf] for "on" and "off". Otherwise it will return -EINVAL.  Value
  * pointed to by res is updated upon finding a match.
  */
mm/page_alloc.c +1 -8
index 59de90d..d31426d 100644
@@ -513,14 +513,7 @@ static int __init early_debug_pagealloc(char *buf)
 {
 	if (!buf)
 		return -EINVAL;
-
-	if (strcmp(buf, "on") == 0)
-		_debug_pagealloc_enabled = true;
-
-	if (strcmp(buf, "off") == 0)
-		_debug_pagealloc_enabled = false;
-
-	return 0;
+	return kstrtobool(buf, &_debug_pagealloc_enabled);
 }
 early_param("debug_pagealloc", early_debug_pagealloc);
 
mm/page_poison.c +1 -7
index 479e7ea..1eae5fa 100644
@@ -13,13 +13,7 @@ static int early_page_poison_param(char *buf)
 {
 	if (!buf)
 		return -EINVAL;
-
-	if (strcmp(buf, "on") == 0)
-		want_page_poisoning = true;
-	else if (strcmp(buf, "off") == 0)
-		want_page_poisoning = false;
-
-	return 0;
+	return strtobool(buf, &want_page_poisoning);
 }
 early_param("page_poison", early_page_poison_param);
 
Signature · 1 line
2.6.3
Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#2 lore raw
Quoted 7 lines · Michal Hocko
Quoted 2 lines · Minfei Huang

It's more convenient to use existing function helper to convert string "on/off" to boolean.

But kstrtobool in linux-next only does "This routine returns 0 iff the first character is one of 'Yy1Nn0'" so it doesn't know about on/off. Or am I missing anything?

Hi, Michal.

Thanks for your reply.

Following is the kstrtobool comment from linus tree, which has explained that this function can parse "on"/"off" string. Also Kees Cook has posted such patch to fix this issue as well. So I think it's safe to fix it.

"
This routine returns 0 if the first character is one of 'Yy1Nn0', or [oO][NnFf] for "on" and "off". Otherwise it will return -EINVAL. Value pointed to by res is updated upon finding a match.
"

commit 4cc7ecb7f2a60e8deb783b8fbf7c1ae467acb920
Author: Kees Cook <[email protected]>
Date: Thu Mar 17 14:23:00 2016 -0700

param: convert some "on"/"off" users to strtobool

This changes several users of manual "on"/"off" parsing to use strtobool.

Some side-effects:
- these uses will now parse y/n/1/0 meaningfully too
- the early_param uses will now bubble up parse errors

Thanks
Minfei

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Acked-by
#3 lore raw
Quoted 9 lines · Minfei Huang
Quoted 7 lines · Michal Hocko
Quoted 2 lines · Minfei Huang

It's more convenient to use existing function helper to convert string "on/off" to boolean.

But kstrtobool in linux-next only does "This routine returns 0 iff the first character is one of 'Yy1Nn0'" so it doesn't know about on/off. Or am I missing anything?

Quoted 8 lines

Hi, Michal.

Thanks for your reply.

Following is the kstrtobool comment from linus tree, which has explained that this function can parse "on"/"off" string. Also Kees Cook has posted such patch to fix this issue as well. So I think it's safe to fix it.

OK, I was looking at wrong tree and missed a81a5a17d44b ("lib: add "on"/"off" support to kstrtobool")

Sorry about the confusion. Feel free to add

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#4 lore raw
In reply to Minfei Huang

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

Quoted 13 lines · On Fri, 29 Apr 2016 13:47:04 +0800 Minfei Huang <[email protected]> wrote:

It's more convenient to use existing function helper to convert string "on/off" to boolean.

...

lib/kstrtox.c +1 -1
@@ -326,7 +326,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 'Yy1Nn0', or

That isn't actually a typo. "iff" is shorthand for "if and only if".
ie: kstrtobool() will not return 0 in any other case.

Use of "iff" is a bit pretentious but I guess it does convey some conceivably useful info.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#5 lore raw
Quoted 19 lines · Andrew Morton

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

Quoted 13 lines · On Fri, 29 Apr 2016 13:47:04 +0800 Minfei Huang <[email protected]> wrote:

It's more convenient to use existing function helper to convert string "on/off" to boolean.

...

lib/kstrtox.c +1 -1
@@ -326,7 +326,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 'Yy1Nn0', or

That isn't actually a typo. "iff" is shorthand for "if and only if".
ie: kstrtobool() will not return 0 in any other case.

Quoted 3 lines

Use of "iff" is a bit pretentious but I guess it does convey some conceivably useful info.

Got it. Thanks for your explanation.

Thanks
Minfei

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>