* [PATCH 2/5] x86: test early command-line code
2015-12-17 22:00 [PATCH 1/5] x86: pass in size to early cmdline parsing Dave Hansen
@ 2015-12-17 22:00 ` Dave Hansen
2015-12-17 22:44 ` kbuild test robot
2015-12-18 2:03 ` kbuild test robot
2015-12-17 22:00 ` [PATCH 3/5] x86: fix early command-line parsing when matching at end Dave Hansen
` (2 subsequent siblings)
3 siblings, 2 replies; 7+ messages in thread
From: Dave Hansen @ 2015-12-17 22:00 UTC (permalink / raw)
To: linux-kernel; +Cc: Dave Hansen, dave.hansen, bp, hpa, fenghua.yu, yu-cheng.yu
From: Dave Hansen <dave.hansen@linux.intel.com>
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.
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: linux-kernel@vger.kernel.org
Cc: fenghua.yu@intel.com
Cc: yu-cheng.yu@intel.com
---
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(+)
diff -puN arch/x86/Kconfig.debug~x86-early-command-line-test arch/x86/Kconfig.debug
--- a/arch/x86/Kconfig.debug~x86-early-command-line-test 2015-12-17 13:55:30.619652300 -0800
+++ b/arch/x86/Kconfig.debug 2015-12-17 13:55:30.625652571 -0800
@@ -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
diff -puN arch/x86/kernel/check.c~x86-early-command-line-test arch/x86/kernel/check.c
--- a/arch/x86/kernel/check.c~x86-early-command-line-test 2015-12-17 13:55:30.621652391 -0800
+++ b/arch/x86/kernel/check.c 2015-12-17 13:55:30.626652617 -0800
@@ -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 */
diff -puN arch/x86/lib/cmdline.c~x86-early-command-line-test arch/x86/lib/cmdline.c
--- a/arch/x86/lib/cmdline.c~x86-early-command-line-test 2015-12-17 13:55:30.622652436 -0800
+++ b/arch/x86/lib/cmdline.c 2015-12-17 13:55:30.626652617 -0800
@@ -4,6 +4,7 @@
*
* Misc librarized functions for cmdline poking.
*/
+#include <linux/bug.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/ctype.h>
@@ -89,3 +90,124 @@ int cmdline_find_option_bool(const char
return __cmdline_find_option_bool(cmdline, COMMAND_LINE_SIZE,
option);
}
+
+#ifdef CONFIG_X86_TEST_EARLY_CMDLINE
+
+static int __cmdtest(char *cmdline, int str_size, char *option,
+ int expected_result, int do_shrink)
+{
+ int ret;
+ int null_terminate;
+ /* Results are 1-based, so bias back down by 1 */
+ int option_end = expected_result + strlen(option) - 1;
+ int shrink_max = 0;
+
+ if (cmdline && do_shrink)
+ shrink_max = strlen(cmdline);
+ /*
+ * The option was not found. If it was not found in the
+ * *full* command-line, it should never be found in any
+ * *part* of the command-line.
+ */
+ for (null_terminate = 0; null_terminate <= 1; null_terminate++) {
+ int shrink_by;
+ for (shrink_by = 0; shrink_by < shrink_max; shrink_by++) {
+ int str_size_tst = str_size - shrink_by;
+ char tmp = cmdline[str_size_tst];
+
+ /*
+ * Do not run tests that would truncate
+ * over the expected option
+ */
+ if (str_size_tst <= option_end)
+ continue;
+
+ if (null_terminate)
+ cmdline[str_size_tst] = '\0';
+ ret = __cmdline_find_option_bool(cmdline, str_size_tst,
+ option);
+ if (null_terminate)
+ cmdline[str_size_tst] = tmp;
+
+ if (ret == expected_result)
+ continue;
+ pr_err("failed cmdline test ('%s', %d, '%s') == %d "
+ "nulld: %d got: %d\n",
+ cmdline, str_size_tst, option,
+ expected_result, null_terminate,
+ ret);
+ return 1;
+ }
+ }
+ return 0;
+}
+
+#define cmdtest(cmdline, option, result) \
+ WARN_ON(__cmdtest(cmdline, sizeof(cmdline), option, result, 1))
+
+#define cmdtest_noshrink(cmdline, option, result) \
+ WARN_ON(__cmdtest(cmdline, sizeof(cmdline), option, result, 0))
+
+char cmdline1[] = "CALL me Ishmael ";
+char cmdline2[] = "Whenever I find myself growing grim about the mouth ";
+char cmdline3[] = "grow growing ";
+int test_early_cmdline(void)
+{
+ /* NULL command-line: */
+ WARN_ON(__cmdline_find_option_bool(NULL, 22, "Ishmael") != -1);
+ /* zero-length command-line: */
+ cmdtest("", "Ishmael", 0);
+
+ /* Find words at each of 3 positions: start, middle, end */
+ cmdtest(cmdline1, "CALL", 1);
+ cmdtest(cmdline1, "me", 6);
+ cmdtest(cmdline1, "Ishmael", 9);
+
+ /*
+ * Fail to find strings that all occur in the cmdline,
+ * but not as full words
+ */
+ /*
+ * If "option" is _present_ in "cmdline" as the start of a
+ * word, like cmdline="foo bar" and we pass in option="b",
+ * when we shrink cmdline to "foo b", it will match. So,
+ * skip shrink tests for those.
+ */
+ cmdtest_noshrink(cmdline1, "m", 0);
+ cmdtest(cmdline1, "e", 0);
+ cmdtest(cmdline1, "C", 0);
+ cmdtest(cmdline1, "l", 0);
+ cmdtest_noshrink(cmdline1, "Ishmae", 0);
+ cmdtest(cmdline1, "mael", 0);
+ /*
+ * Look for strings that do not occur, but match until
+ * close to the end of cmdline
+ */
+ cmdtest_noshrink(cmdline1, "Ishmae", 0);
+ cmdtest(cmdline1, "Ishmaels", 0);
+ cmdtest(cmdline1, "maels", 0);
+
+ /*
+ * Look for full words that do not occur in a different
+ * cmdline
+ */
+ cmdtest(cmdline2, "CALL", 0);
+ cmdtest(cmdline2, "me", 0);
+ cmdtest(cmdline2, "Ishmael", 0);
+ /*
+ * Look for full words which do occur in cmdline2
+ */
+ cmdtest(cmdline2, "Whenever", 1);
+ cmdtest(cmdline2, "growing", 24);
+ cmdtest(cmdline2, "grim", 32);
+ cmdtest(cmdline2, "mouth", 47);
+
+ /*
+ * Catch the bug where if we match a partial word and
+ * then have a space, we do not match the _next_ word.
+ */
+ cmdtest(cmdline3, "grow", 1);
+ cmdtest(cmdline3, "growing", 6);
+ return 0;
+}
+#endif /* CONFIG_X86_TEST_EARLY_CMDLINE */
_
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 3/5] x86: fix early command-line parsing when matching at end
2015-12-17 22:00 [PATCH 1/5] x86: pass in size to early cmdline parsing Dave Hansen
2015-12-17 22:00 ` [PATCH 2/5] x86: test early command-line code Dave Hansen
@ 2015-12-17 22:00 ` Dave Hansen
2015-12-17 22:00 ` [PATCH 4/5] x86: fix early command-line parsing, when partial word match Dave Hansen
2015-12-17 22:01 ` [PATCH 5/5] x86: simplify early command line parsing Dave Hansen
3 siblings, 0 replies; 7+ messages in thread
From: Dave Hansen @ 2015-12-17 22:00 UTC (permalink / raw)
To: linux-kernel; +Cc: Dave Hansen, dave.hansen, bp, hpa, fenghua.yu, yu-cheng.yu
From: Dave Hansen <dave.hansen@linux.intel.com>
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.
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: linux-kernel@vger.kernel.org
Cc: fenghua.yu@intel.com
Cc: yu-cheng.yu@intel.com
---
b/arch/x86/lib/cmdline.c | 34 ++++++++++++++++++++++++----------
1 file changed, 24 insertions(+), 10 deletions(-)
diff -puN arch/x86/lib/cmdline.c~x86-broken-end-of-string-command-line-parsing arch/x86/lib/cmdline.c
--- a/arch/x86/lib/cmdline.c~x86-broken-end-of-string-command-line-parsing 2015-12-17 13:54:53.398967308 -0800
+++ b/arch/x86/lib/cmdline.c 2015-12-17 13:54:53.401967444 -0800
@@ -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:
_
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 4/5] x86: fix early command-line parsing, when partial word match
2015-12-17 22:00 [PATCH 1/5] x86: pass in size to early cmdline parsing Dave Hansen
2015-12-17 22:00 ` [PATCH 2/5] x86: test early command-line code Dave Hansen
2015-12-17 22:00 ` [PATCH 3/5] x86: fix early command-line parsing when matching at end Dave Hansen
@ 2015-12-17 22:00 ` Dave Hansen
2015-12-17 22:01 ` [PATCH 5/5] x86: simplify early command line parsing Dave Hansen
3 siblings, 0 replies; 7+ messages in thread
From: Dave Hansen @ 2015-12-17 22:00 UTC (permalink / raw)
To: linux-kernel; +Cc: Dave Hansen, dave.hansen, bp, hpa, fenghua.yu, yu-cheng.yu
From: Dave Hansen <dave.hansen@linux.intel.com>
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.
Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: linux-kernel@vger.kernel.org
Cc: fenghua.yu@intel.com
Cc: yu-cheng.yu@intel.com
---
b/arch/x86/lib/cmdline.c | 18 +++++++++++++-----
1 file changed, 13 insertions(+), 5 deletions(-)
diff -puN arch/x86/lib/cmdline.c~x86-mid-option-match-command-line-parsing arch/x86/lib/cmdline.c
--- a/arch/x86/lib/cmdline.c~x86-mid-option-match-command-line-parsing 2015-12-17 13:54:53.804985689 -0800
+++ b/arch/x86/lib/cmdline.c 2015-12-17 13:54:53.808985871 -0800
@@ -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)
_
^ permalink raw reply [flat|nested] 7+ messages in thread