Skip to contentSkip to conversation

x86: pass in size to early cmdline parsing

PATCH New Dave Hansen 2 participants 7 messages Dec 17, 2015 – Dec 18, 2015 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 x86: pass in size to early cmdline parsing

From: Dave Hansen <[email protected]>

We will use this in a few patches to implement tests for early parsing.

---

 b/arch/x86/lib/cmdline.c |   11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
arch/x86/lib/cmdline.c renamed from arch/x86/lib/cmdline.c~pass-in-cmdline-size +9 -2
diff -puN arch/x86/lib/cmdline.c~pass-in-cmdline-size arch/x86/lib/cmdline.c
@@ -23,7 +23,8 @@ static inline int myisspace(u8 c)
  * Returns the position of that @option (starts counting with 1)
  * or 0 on not found.
  */
-int cmdline_find_option_bool(const char *cmdline, const char *option)
+static int __cmdline_find_option_bool(const char *cmdline,
+		int max_cmdline_size, const char *option)
 {
 	char c;
 	int len, pos = 0, wstart = 0;
@@ -37,7 +38,7 @@ int cmdline_find_option_bool(const char
 	if (!cmdline)
 		return -1;      /* No command line */
 
-	len = min_t(int, strlen(cmdline), COMMAND_LINE_SIZE);
+	len = min_t(int, strlen(cmdline), max_cmdline_size);
 	if (!len)
 		return 0;
 
@@ -82,3 +83,9 @@ int cmdline_find_option_bool(const char
 
 	return 0;	/* Buffer overrun */
 }
+
+int cmdline_find_option_bool(const char *cmdline, const char *option)
+{
+	return __cmdline_find_option_bool(cmdline, COMMAND_LINE_SIZE,
+			option);
+}

_

#1 lore raw

PATCH 2/5 x86: test early command-line code

From: Dave Hansen <[email protected]>

Here are some simple tests for the early command-line code. It had way more bugs than it should have, so let's make sure they never pop up again.

Note, there are a few failures in here now. We will fix those up in the next few patches.

This is complete overkill for this code, but I had to do it to convince myself that I wasn't making it any worse.

---

 b/arch/x86/Kconfig.debug  |    7 ++
 b/arch/x86/kernel/check.c |    4 +
 b/arch/x86/lib/cmdline.c  |  122 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 133 insertions(+)
arch/x86/Kconfig.debug renamed from arch/x86/Kconfig.debug~x86-early-command-line-test +7 -0
diff -puN arch/x86/Kconfig.debug~x86-early-command-line-test arch/x86/Kconfig.debug
@@ -400,4 +400,11 @@ config PUNIT_ATOM_DEBUG
 	  The current power state can be read from
 	  /sys/kernel/debug/punit_atom/dev_power_state
 
+config X86_TEST_EARLY_CMDLINE
+	bool "Early command line runtime tests"
+	---help---
+	  This creates some test command-lines and tries to parse
+	  a bunch of options from them.  The overhead is small both
+	  at boot and increased text/data sizes.
+
 endmenu
arch/x86/kernel/check.c renamed from arch/x86/kernel/check.c~x86-early-command-line-test +4 -0
diff -puN arch/x86/kernel/check.c~x86-early-command-line-test arch/x86/kernel/check.c
@@ -165,3 +165,7 @@ static int start_periodic_check_for_corr
 }
 device_initcall(start_periodic_check_for_corruption);
 
+#ifdef CONFIG_X86_TEST_EARLY_CMDLINE
+int test_early_cmdline(void)
+late_initcall(test_early_cmdline);
+#endif /* CONFIG_X86_TEST_EARLY_CMDLINE */
arch/x86/lib/cmdline.c renamed from arch/x86/lib/cmdline.c~x86-early-command-line-test +122 -0

131 lines not on this page · raw message

_

#2 lore raw

Re: [PATCH 2/5] x86: test early command-line code

reproduce:

Hi Dave,

[auto build test ERROR on v4.4-rc5]
[also build test ERROR on next-20151217]
[cannot apply to tip/x86/core]

url:    https://github.com/0day-ci/linux/commits/Dave-Hansen/x86-pass-in-size-to-early-cmdline-parsing/20151218-055822
config: x86_64-allmodconfig (attached as .config)
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

All error/warnings (new ones prefixed by >>):

include/linux/compiler.h:228:8: sparse: attribute 'no_sanitize_address': unknown attribute arch/x86/kernel/check.c:170:1: sparse: expected ';' at end of parameter declaration arch/x86/kernel/check.c:170:1: sparse: expected function body arch/x86/kernel/check.c:170:1: sparse: Expected ; at the end of type declaration arch/x86/kernel/check.c:170:1: sparse: got =
In file included from arch/x86/kernel/check.c:1:0:
arch/x86/kernel/check.c: In function 'test_early_cmdline':

>> include/linux/init.h:183:20: error: storage class specified for parameter '__initcall_test_early_cmdline7'
     static initcall_t __initcall_##fn##id __used \
                       ^
   include/linux/init.h:216:28: note: in expansion of macro '__define_initcall'
    #define late_initcall(fn)  __define_initcall(fn, 7)
                               ^
>> arch/x86/kernel/check.c:170:1: note: in expansion of macro 'late_initcall'
    late_initcall(test_early_cmdline);
    ^
>> arch/x86/kernel/check.c:170:1: error: parameter '__initcall_test_early_cmdline7' is initialized
>> arch/x86/kernel/check.c:170:1: warning: '__used__' attribute ignored [-Wattributes]
   In file included from arch/x86/kernel/check.c:1:0:
>> include/linux/init.h:183:20: error: section attribute not allowed for '__initcall_test_early_cmdline7'
     static initcall_t __initcall_##fn##id __used \
                       ^
   include/linux/init.h:216:28: note: in expansion of macro '__define_initcall'
    #define late_initcall(fn)  __define_initcall(fn, 7)
                               ^
>> arch/x86/kernel/check.c:170:1: note: in expansion of macro 'late_initcall'
    late_initcall(test_early_cmdline);
    ^
>> arch/x86/kernel/check.c:170:34: error: expected declaration specifiers before ';' token
    late_initcall(test_early_cmdline);
                                     ^
>> arch/x86/kernel/check.c:169:5: error: old-style parameter declarations in prototyped function definition
    int test_early_cmdline(void)
        ^
>> arch/x86/kernel/check.c:170:34: error: expected '{' at end of input
    late_initcall(test_early_cmdline);
                                     ^
>> arch/x86/kernel/check.c:170:34: warning: control reaches end of non-void function [-Wreturn-type]
    late_initcall(test_early_cmdline);
                                     ^

sparse warnings: (new ones prefixed by >>)

include/linux/compiler.h:228:8: sparse: attribute 'no_sanitize_address': unknown attribute

>> arch/x86/kernel/check.c:170:1: sparse: expected ';' at end of parameter declaration
>> arch/x86/kernel/check.c:170:1: sparse: expected function body
   arch/x86/kernel/check.c:170:1: sparse: Expected ; at the end of type declaration
   arch/x86/kernel/check.c:170:1: sparse: got =
   In file included from arch/x86/kernel/check.c:1:0:
   arch/x86/kernel/check.c: In function 'test_early_cmdline':
   include/linux/init.h:183:20: error: storage class specified for parameter '__initcall_test_early_cmdline7'
     static initcall_t __initcall_##fn##id __used \
                       ^
   include/linux/init.h:216:28: note: in expansion of macro '__define_initcall'
    #define late_initcall(fn)  __define_initcall(fn, 7)
                               ^
   arch/x86/kernel/check.c:170:1: note: in expansion of macro 'late_initcall'
    late_initcall(test_early_cmdline);
    ^
   arch/x86/kernel/check.c:170:1: error: parameter '__initcall_test_early_cmdline7' is initialized
   arch/x86/kernel/check.c:170:1: warning: '__used__' attribute ignored [-Wattributes]
   In file included from arch/x86/kernel/check.c:1:0:
   include/linux/init.h:183:20: error: section attribute not allowed for '__initcall_test_early_cmdline7'
     static initcall_t __initcall_##fn##id __used \
                       ^
   include/linux/init.h:216:28: note: in expansion of macro '__define_initcall'
    #define late_initcall(fn)  __define_initcall(fn, 7)
                               ^
   arch/x86/kernel/check.c:170:1: note: in expansion of macro 'late_initcall'
    late_initcall(test_early_cmdline);
    ^
   arch/x86/kernel/check.c:170:34: error: expected declaration specifiers before ';' token
    late_initcall(test_early_cmdline);
                                     ^
   arch/x86/kernel/check.c:169:5: error: old-style parameter declarations in prototyped function definition
    int test_early_cmdline(void)
        ^
   arch/x86/kernel/check.c:170:34: error: expected '{' at end of input
    late_initcall(test_early_cmdline);
                                     ^
   arch/x86/kernel/check.c:170:34: warning: control reaches end of non-void function [-Wreturn-type]
    late_initcall(test_early_cmdline);
                                     ^

vim +/__initcall_test_early_cmdline7 +170 arch/x86/kernel/check.c

163 schedule_delayed_work(&bios_check_work, 0);
164 return 0;
165 }
166 device_initcall(start_periodic_check_for_corruption);
167
168 #ifdef CONFIG_X86_TEST_EARLY_CMDLINE

 > 169	int test_early_cmdline(void)
 > 170	late_initcall(test_early_cmdline);

171 #endif /* CONFIG_X86_TEST_EARLY_CMDLINE */

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#3 lore raw

Re: [PATCH 2/5] x86: test early command-line code

In reply to Dave Hansen
reproduce:

Hi Dave,

[auto build test ERROR on v4.4-rc5]
[also build test ERROR on next-20151217]
[cannot apply to tip/x86/core]

url:    https://github.com/0day-ci/linux/commits/Dave-Hansen/x86-pass-in-size-to-early-cmdline-parsing/20151218-060427
config: i386-allyesconfig (attached as .config)
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

All errors (new ones prefixed by >>):

arch/x86/kernel/check.c: In function 'test_early_cmdline':

>> arch/x86/kernel/check.c:170:19: error: storage class specified for parameter '__initcall_test_early_cmdline7'
    late_initcall(test_early_cmdline);
                      ^
   arch/x86/kernel/check.c:170:1: error: parameter '__initcall_test_early_cmdline7' is initialized
    late_initcall(test_early_cmdline);
    ^
   arch/x86/kernel/check.c:170:1: warning: '__used__' attribute ignored [-Wattributes]
>> arch/x86/kernel/check.c:170:19: error: section attribute not allowed for '__initcall_test_early_cmdline7'
    late_initcall(test_early_cmdline);
                      ^
   arch/x86/kernel/check.c:170:152: error: expected declaration specifiers before ';' token
   arch/x86/kernel/check.c:169:5: error: old-style parameter declarations in prototyped function definition
    int test_early_cmdline(void)
        ^

arch/x86/kernel/check.c:170:152: error: expected '{' at end of input arch/x86/kernel/check.c:170:152: warning: control reaches end of non-void function [-Wreturn-type]

vim +/__initcall_test_early_cmdline7 +170 arch/x86/kernel/check.c

164 return 0;
165 }
166 device_initcall(start_periodic_check_for_corruption);
167
168 #ifdef CONFIG_X86_TEST_EARLY_CMDLINE
169 int test_early_cmdline(void)

 > 170	late_initcall(test_early_cmdline);

171 #endif /* CONFIG_X86_TEST_EARLY_CMDLINE */

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#4 lore raw

PATCH 3/5 x86: fix early command-line parsing when matching at end

In reply to Dave Hansen

From: Dave Hansen <[email protected]>

The x86 early command line parsing in cmdline_find_option_bool() is buggy. If it matches a specified 'option' all the way to the end of the command-line, it will consider it a match.

For instance,

    	cmdline = "foo";
    	cmdline_find_option_bool(cmdline, "fool");

will return 1. This is particularly annoying since we have actual FPU options like "noxsave" and "noxsaves" So, command-line "foo bar noxsave" will match *BOTH* a "noxsave" and "noxsaves". (This turns out not to be an actual problem because "noxsave" implies "noxsaves", but it's still confusing.)

To fix this, we simplify the code and stop tracking 'len'. 'len' was trying to indicate either the NULL terminator *OR* the end of a non-NULL-terminated commandline at 'max_cmdline_size'. But,
each of the three states is *already* checking 'cmdline' for a NULL terminator.

We _only_ need to check if we have overrun 'max_cmdline_size', and that we can do without keeping 'len' around.

Also add some commends to clarify what is going on.

---

 b/arch/x86/lib/cmdline.c |   34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)
arch/x86/lib/cmdline.c renamed from arch/x86/lib/cmdline.c~x86-broken-end-of-string-command-line-parsing +24 -10
diff -puN arch/x86/lib/cmdline.c~x86-broken-end-of-string-command-line-parsing arch/x86/lib/cmdline.c
@@ -22,13 +22,15 @@ static inline int myisspace(u8 c)
  * @option: option string to look for
  *
  * Returns the position of that @option (starts counting with 1)
- * or 0 on not found.
+ * or 0 on not found.  @option will only be found if it is found
+ * as an entire word in @cmdline.  For instance, if @option="car"
+ * then a cmdline which contains "cart" will not match.
  */
 static int __cmdline_find_option_bool(const char *cmdline,
 		int max_cmdline_size, const char *option)
 {
 	char c;
-	int len, pos = 0, wstart = 0;
+	int pos = 0, wstart = 0;
 	const char *opptr = NULL;
 	enum {
 		st_wordstart = 0,	/* Start of word/after whitespace */
@@ -39,11 +41,14 @@ static int __cmdline_find_option_bool(co
 	if (!cmdline)
 		return -1;      /* No command line */
 
-	len = min_t(int, strlen(cmdline), max_cmdline_size);
-	if (!len)
+	if (!strlen(cmdline))
 		return 0;
 
-	while (len--) {
+	/*
+	 * This 'pos' check ensures we do not overrun
+	 * a non-NULL-terminated 'cmdline'
+	 */
+	while (pos < max_cmdline_size) {
 		c = *(char *)cmdline++;
 		pos++;
 
@@ -60,17 +65,26 @@ static int __cmdline_find_option_bool(co
 			/* fall through */
 
 		case st_wordcmp:
-			if (!*opptr)
+			if (!*opptr) {
+				/*
+				 * We matched all the way to the end of the
+				 * option we were looking for.  If the
+				 * command-line has a space _or_ ends, then
+				 * we matched!
+				 */
 				if (!c || myisspace(c))
 					return wstart;
 				else
 					state = st_wordskip;
-			else if (!c)
+			} else if (!c) {
+				/*
+				 * Hit the NULL terminator on the end of
+				 * cmdline.
+				 */
 				return 0;
-			else if (c != *opptr++)
+			} else if (c != *opptr++) {
 				state = st_wordskip;
-			else if (!len)		/* last word and is matching */
-				return wstart;
+			}
 			break;
 
 		case st_wordskip:

_

#5 lore raw

PATCH 4/5 x86: fix early command-line parsing, when partial word match

In reply to Dave Hansen

From: Dave Hansen <[email protected]>

cmdline_find_option_bool() keeps track of position in two strings:
1. the command-line
2. the option we are searchign for in the command-line

We plow through each character in the command-line one at a time, always moving forward. We move forward in the option ('opptr') when we match characters in 'cmdline'. We reset the 'opptr' only when we go in to the 'st_wordstart' state.

But, if we fail to match an option because we see a space ( state=st_wordcmp, *opptr='\0',c=' '), we set state='st_wordskip' and 'break', moving to the next character. But, that move to the next character is the one *after* the ' '. This means that we will miss a 'st_wordstart' state.

For instance, if we have

	cmdline = "foo fool";

and are searching for "fool", we have:

	"fool"

opptr = ----^

"foo fool"
c = --------^

We see that 'l' != ' ', set state=st_wordskip, break, and then move 'c', so:

"foo fool"
c = ---------^

and are still in state=st_wordskip. We will stay in wordskip until we have skipped "fool", thus missing the option we were looking for. This *only* happens when you have a partially- matching word followed by a matching one.

To fix this, we always fall *into* the 'st_wordskip' state when we set it.

---

 b/arch/x86/lib/cmdline.c |   18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
arch/x86/lib/cmdline.c renamed from arch/x86/lib/cmdline.c~x86-mid-option-match-command-line-parsing +13 -5
diff -puN arch/x86/lib/cmdline.c~x86-mid-option-match-command-line-parsing arch/x86/lib/cmdline.c
@@ -74,18 +74,26 @@ static int __cmdline_find_option_bool(co
 				 */
 				if (!c || myisspace(c))
 					return wstart;
-				else
-					state = st_wordskip;
+				/*
+				 * We hit the end of the option, but _not_
+				 * the end of a word on the cmdline.  Not
+				 * a match.
+				 */
 			} else if (!c) {
 				/*
 				 * Hit the NULL terminator on the end of
 				 * cmdline.
 				 */
 				return 0;
-			} else if (c != *opptr++) {
-				state = st_wordskip;
+			} else if (c == *opptr++) {
+				/*
+				 * We are currently matching, so continue
+				 * to the next character on the cmdline.
+				 */
+				break;
 			}
-			break;
+			state = st_wordskip;
+			/* fall through */
 
 		case st_wordskip:
 			if (!c)

_

#6 lore raw

PATCH 5/5 x86: simplify early command line parsing

In reply to Dave Hansen

From: Dave Hansen <[email protected]>

__cmdline_find_option_bool() tries to account for both NULL-terminated and non-NULL-terminated strings. It keeps 'pos' to look for the end of the buffer and also looks for '!c' in a bunch of places to look for NULL termination.

But, it also calls strlen(). You can't call strlen on a non-NULL-terminated string.

If !strlen(cmdline), then cmdline[0]=='\0'. In that case, we will go in to the while() loop, set c='\0', hit st_wordstart, notice !c, and will immediately return 0.

So, remove the strlen(). It is unnecessary and unsafe.

---

 b/arch/x86/lib/cmdline.c |    3 ---
 1 file changed, 3 deletions(-)
arch/x86/lib/cmdline.c renamed from arch/x86/lib/cmdline.c~x86-early-command-line-non-term +0 -3
diff -puN arch/x86/lib/cmdline.c~x86-early-command-line-non-term arch/x86/lib/cmdline.c
@@ -41,9 +41,6 @@ static int __cmdline_find_option_bool(co
 	if (!cmdline)
 		return -1;      /* No command line */
 
-	if (!strlen(cmdline))
-		return 0;
-
 	/*
 	 * This 'pos' check ensures we do not overrun
 	 * a non-NULL-terminated 'cmdline'

_