Subject: [tip: x86/boot] x86/boot: Move kernel cmdline setup earlier in the boot process (again)

The following commit has been merged into the x86/boot branch of tip:

Commit-ID: 4faa0e5d6d79fc4c6e1943e8b62a65744d8439a0
Gitweb: https://git.kernel.org/tip/4faa0e5d6d79fc4c6e1943e8b62a65744d8439a0
Author: Julian Stecklina <[email protected]>
AuthorDate: Thu, 28 Mar 2024 16:42:12 +01:00
Committer: Ingo Molnar <[email protected]>
CommitterDate: Fri, 29 Mar 2024 08:19:12 +01:00

x86/boot: Move kernel cmdline setup earlier in the boot process (again)

When split_lock_detect=off (or similar) is specified in
CONFIG_CMDLINE, its effect is lost. The flow is currently this:

setup_arch():
-> early_cpu_init()
-> early_identify_cpu()
-> sld_setup()
-> sld_state_setup()
-> Looks for split_lock_detect in boot_command_line

-> e820__memory_setup()

-> Assemble final command line:
boot_command_line = builtin_cmdline + boot_cmdline

-> parse_early_param()

There were earlier attempts at fixing this in:

8d48bf8206f7 ("x86/boot: Pull up cmdline preparation and early param parsing")

later reverted in:

fbe618399854 ("Revert "x86/boot: Pull up cmdline preparation and early param parsing"")

.. because parse_early_param() can't be called before
e820__memory_setup().

In this patch, we just move the command line concatenation to the
beginning of early_cpu_init(). This should fix sld_state_setup(), while
not running in the same issues as the earlier attempt.

The order is now:

setup_arch():
-> Assemble final command line:
boot_command_line = builtin_cmdline + boot_cmdline

-> early_cpu_init()
-> early_identify_cpu()
-> sld_setup()
-> sld_state_setup()
-> Looks for split_lock_detect in boot_command_line

-> e820__memory_setup()

-> parse_early_param()

Signed-off-by: Julian Stecklina <[email protected]>
Signed-off-by: Ingo Molnar <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: [email protected]
---
arch/x86/kernel/setup.c | 32 ++++++++++++++++----------------
1 file changed, 16 insertions(+), 16 deletions(-)

diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
index 3e1e96e..4c35f1b 100644
--- a/arch/x86/kernel/setup.c
+++ b/arch/x86/kernel/setup.c
@@ -753,6 +753,22 @@ void __init setup_arch(char **cmdline_p)
boot_cpu_data.x86_phys_bits = MAX_PHYSMEM_BITS;
#endif

+#ifdef CONFIG_CMDLINE_BOOL
+#ifdef CONFIG_CMDLINE_OVERRIDE
+ strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
+#else
+ if (builtin_cmdline[0]) {
+ /* append boot loader cmdline to builtin */
+ strlcat(builtin_cmdline, " ", COMMAND_LINE_SIZE);
+ strlcat(builtin_cmdline, boot_command_line, COMMAND_LINE_SIZE);
+ strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
+ }
+#endif
+#endif
+
+ strscpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
+ *cmdline_p = command_line;
+
/*
* If we have OLPC OFW, we might end up relocating the fixmap due to
* reserve_top(), so do this before touching the ioremap area.
@@ -832,22 +848,6 @@ void __init setup_arch(char **cmdline_p)
bss_resource.start = __pa_symbol(__bss_start);
bss_resource.end = __pa_symbol(__bss_stop)-1;

-#ifdef CONFIG_CMDLINE_BOOL
-#ifdef CONFIG_CMDLINE_OVERRIDE
- strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
-#else
- if (builtin_cmdline[0]) {
- /* append boot loader cmdline to builtin */
- strlcat(builtin_cmdline, " ", COMMAND_LINE_SIZE);
- strlcat(builtin_cmdline, boot_command_line, COMMAND_LINE_SIZE);
- strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
- }
-#endif
-#endif
-
- strscpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
- *cmdline_p = command_line;
-
/*
* x86_configure_nx() is called before parse_early_param() to detect
* whether hardware doesn't support NX (so that the early EHCI debug


2024-04-08 18:18:02

by Borislav Petkov

[permalink] [raw]
Subject: Re: [tip: x86/boot] x86/boot: Move kernel cmdline setup earlier in the boot process (again)

On Fri, Mar 29, 2024 at 07:51:13AM -0000, tip-bot2 for Julian Stecklina wrote:
> The following commit has been merged into the x86/boot branch of tip:
>
> Commit-ID: 4faa0e5d6d79fc4c6e1943e8b62a65744d8439a0
> Gitweb: https://git.kernel.org/tip/4faa0e5d6d79fc4c6e1943e8b62a65744d8439a0
> Author: Julian Stecklina <[email protected]>
> AuthorDate: Thu, 28 Mar 2024 16:42:12 +01:00
> Committer: Ingo Molnar <[email protected]>
> CommitterDate: Fri, 29 Mar 2024 08:19:12 +01:00
>
> x86/boot: Move kernel cmdline setup earlier in the boot process (again)

..

> The order is now:
>
> setup_arch():
> -> Assemble final command line:
> boot_command_line = builtin_cmdline + boot_cmdline
>
> -> early_cpu_init()
> -> early_identify_cpu()
> -> sld_setup()
> -> sld_state_setup()
> -> Looks for split_lock_detect in boot_command_line
>
> -> e820__memory_setup()
>
> -> parse_early_param()

So that thing. Should we do something like the silly thing below so that
it catches potential issues with parsing builtin cmdline stuff too
early?

diff --git a/arch/x86/include/asm/setup.h b/arch/x86/include/asm/setup.h
index e61e68d71cba..2e1d19e103e6 100644
--- a/arch/x86/include/asm/setup.h
+++ b/arch/x86/include/asm/setup.h
@@ -7,6 +7,7 @@
#define COMMAND_LINE_SIZE 2048

#include <linux/linkage.h>
+
#include <asm/page_types.h>
#include <asm/ibt.h>

@@ -28,6 +29,8 @@
#define NEW_CL_POINTER 0x228 /* Relative to real mode data */

#ifndef __ASSEMBLY__
+#include <linux/cache.h>
+
#include <asm/bootparam.h>
#include <asm/x86_init.h>

@@ -133,6 +136,12 @@ asmlinkage void __init __noreturn x86_64_start_reservations(char *real_mode_data
#endif /* __i386__ */
#endif /* _SETUP */

+#ifdef CONFIG_CMDLINE_BOOL
+extern bool builtin_cmdline_added __ro_after_init;
+#else
+#define builtin_cmdline_added 0
+#endif
+
#else /* __ASSEMBLY */

.macro __RESERVE_BRK name, size
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
index 55a1fc332e20..a35ca100f57c 100644
--- a/arch/x86/kernel/setup.c
+++ b/arch/x86/kernel/setup.c
@@ -165,6 +165,7 @@ unsigned long saved_video_mode;
static char __initdata command_line[COMMAND_LINE_SIZE];
#ifdef CONFIG_CMDLINE_BOOL
static char __initdata builtin_cmdline[COMMAND_LINE_SIZE] = CONFIG_CMDLINE;
+bool builtin_cmdline_added __ro_after_init;
#endif

#if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE)
@@ -765,6 +766,7 @@ void __init setup_arch(char **cmdline_p)
strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
}
#endif
+ builtin_cmdline_added = true;
#endif

strscpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
diff --git a/arch/x86/lib/cmdline.c b/arch/x86/lib/cmdline.c
index 80570eb3c89b..6307cd62acd7 100644
--- a/arch/x86/lib/cmdline.c
+++ b/arch/x86/lib/cmdline.c
@@ -6,9 +6,12 @@
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/ctype.h>
+
#include <asm/setup.h>
#include <asm/cmdline.h>

+#include <asm/bug.h>
+
static inline int myisspace(u8 c)
{
return c <= ' '; /* Close enough approximation */
@@ -205,12 +208,16 @@ __cmdline_find_option(const char *cmdline, int max_cmdline_size,

int cmdline_find_option_bool(const char *cmdline, const char *option)
{
+ WARN_ON_ONCE(!builtin_cmdline_added);
+
return __cmdline_find_option_bool(cmdline, COMMAND_LINE_SIZE, option);
}

int cmdline_find_option(const char *cmdline, const char *option, char *buffer,
int bufsize)
{
+ WARN_ON_ONCE(!builtin_cmdline_added);
+
return __cmdline_find_option(cmdline, COMMAND_LINE_SIZE, option,
buffer, bufsize);
}

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette

2024-04-08 18:28:54

by Ingo Molnar

[permalink] [raw]
Subject: Re: [tip: x86/boot] x86/boot: Move kernel cmdline setup earlier in the boot process (again)


* Borislav Petkov <[email protected]> wrote:

> On Fri, Mar 29, 2024 at 07:51:13AM -0000, tip-bot2 for Julian Stecklina wrote:
> > The following commit has been merged into the x86/boot branch of tip:
> >
> > Commit-ID: 4faa0e5d6d79fc4c6e1943e8b62a65744d8439a0
> > Gitweb: https://git.kernel.org/tip/4faa0e5d6d79fc4c6e1943e8b62a65744d8439a0
> > Author: Julian Stecklina <[email protected]>
> > AuthorDate: Thu, 28 Mar 2024 16:42:12 +01:00
> > Committer: Ingo Molnar <[email protected]>
> > CommitterDate: Fri, 29 Mar 2024 08:19:12 +01:00
> >
> > x86/boot: Move kernel cmdline setup earlier in the boot process (again)
>
> ...
>
> > The order is now:
> >
> > setup_arch():
> > -> Assemble final command line:
> > boot_command_line = builtin_cmdline + boot_cmdline
> >
> > -> early_cpu_init()
> > -> early_identify_cpu()
> > -> sld_setup()
> > -> sld_state_setup()
> > -> Looks for split_lock_detect in boot_command_line
> >
> > -> e820__memory_setup()
> >
> > -> parse_early_param()
>
> So that thing. Should we do something like the silly thing below so that
> it catches potential issues with parsing builtin cmdline stuff too early?

Yep, that's a good idea.

Acked-by: Ingo Molnar <[email protected]>

Ingo

2024-04-09 15:39:30

by Borislav Petkov

[permalink] [raw]
Subject: [PATCH] x86/setup: Warn when option parsing is done too early

On Mon, Apr 08, 2024 at 08:27:49PM +0200, Ingo Molnar wrote:
> > So that thing. Should we do something like the silly thing below so that
> > it catches potential issues with parsing builtin cmdline stuff too early?
>
> Yep, that's a good idea.
>
> Acked-by: Ingo Molnar <[email protected]>

---
From: "Borislav Petkov (AMD)" <[email protected]>
Date: Mon, 8 Apr 2024 19:46:03 +0200

Commit

4faa0e5d6d79 ("x86/boot: Move kernel cmdline setup earlier in the boot process (again)")

fixed and issue where cmdline parsing would happen before the final
boot_command_line string has been built from the builtin and boot
cmdlines and thus cmdline arguments would get lost.

Add a check to catch any future wrong use ordering so that such issues
can be caught in time.

Signed-off-by: Borislav Petkov (AMD) <[email protected]>
---
arch/x86/include/asm/setup.h | 8 ++++++++
arch/x86/kernel/setup.c | 2 ++
arch/x86/lib/cmdline.c | 6 ++++++
3 files changed, 16 insertions(+)

diff --git a/arch/x86/include/asm/setup.h b/arch/x86/include/asm/setup.h
index e61e68d71cba..0667b2a88614 100644
--- a/arch/x86/include/asm/setup.h
+++ b/arch/x86/include/asm/setup.h
@@ -28,6 +28,8 @@
#define NEW_CL_POINTER 0x228 /* Relative to real mode data */

#ifndef __ASSEMBLY__
+#include <linux/cache.h>
+
#include <asm/bootparam.h>
#include <asm/x86_init.h>

@@ -133,6 +135,12 @@ asmlinkage void __init __noreturn x86_64_start_reservations(char *real_mode_data
#endif /* __i386__ */
#endif /* _SETUP */

+#ifdef CONFIG_CMDLINE_BOOL
+extern bool builtin_cmdline_added __ro_after_init;
+#else
+#define builtin_cmdline_added 0
+#endif
+
#else /* __ASSEMBLY */

.macro __RESERVE_BRK name, size
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
index e125e059e2c4..7260bf57fe46 100644
--- a/arch/x86/kernel/setup.c
+++ b/arch/x86/kernel/setup.c
@@ -164,6 +164,7 @@ unsigned long saved_video_mode;
static char __initdata command_line[COMMAND_LINE_SIZE];
#ifdef CONFIG_CMDLINE_BOOL
static char __initdata builtin_cmdline[COMMAND_LINE_SIZE] = CONFIG_CMDLINE;
+bool builtin_cmdline_added __ro_after_init;
#endif

#if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE)
@@ -843,6 +844,7 @@ void __init setup_arch(char **cmdline_p)
strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
}
#endif
+ builtin_cmdline_added = true;
#endif

strscpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
diff --git a/arch/x86/lib/cmdline.c b/arch/x86/lib/cmdline.c
index 80570eb3c89b..e0a6dfc663b4 100644
--- a/arch/x86/lib/cmdline.c
+++ b/arch/x86/lib/cmdline.c
@@ -6,8 +6,10 @@
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/ctype.h>
+
#include <asm/setup.h>
#include <asm/cmdline.h>
+#include <asm/bug.h>

static inline int myisspace(u8 c)
{
@@ -205,12 +207,16 @@ __cmdline_find_option(const char *cmdline, int max_cmdline_size,

int cmdline_find_option_bool(const char *cmdline, const char *option)
{
+ WARN_ON_ONCE(!builtin_cmdline_added);
+
return __cmdline_find_option_bool(cmdline, COMMAND_LINE_SIZE, option);
}

int cmdline_find_option(const char *cmdline, const char *option, char *buffer,
int bufsize)
{
+ WARN_ON_ONCE(!builtin_cmdline_added);
+
return __cmdline_find_option(cmdline, COMMAND_LINE_SIZE, option,
buffer, bufsize);
}
--
2.43.0

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette

Subject: [tip: x86/boot] x86/setup: Warn when option parsing is done too early

The following commit has been merged into the x86/boot branch of tip:

Commit-ID: 0c40b1c7a897bd9733e72aca2396fd3a62f1db17
Gitweb: https://git.kernel.org/tip/0c40b1c7a897bd9733e72aca2396fd3a62f1db17
Author: Borislav Petkov (AMD) <[email protected]>
AuthorDate: Mon, 08 Apr 2024 19:46:03 +02:00
Committer: Borislav Petkov (AMD) <[email protected]>
CommitterDate: Mon, 27 May 2024 18:54:45 +02:00

x86/setup: Warn when option parsing is done too early

Commit

4faa0e5d6d79 ("x86/boot: Move kernel cmdline setup earlier in the boot process (again)")

fixed and issue where cmdline parsing would happen before the final
boot_command_line string has been built from the builtin and boot
cmdlines and thus cmdline arguments would get lost.

Add a check to catch any future wrong use ordering so that such issues
can be caught in time.

Signed-off-by: Borislav Petkov (AMD) <[email protected]>
Acked-by: Ingo Molnar <[email protected]>
Link: https://lore.kernel.org/r/20240409152541.GCZhVd9XIPXyTNd9vc@fat_crate.local
---
arch/x86/include/asm/setup.h | 8 ++++++++
arch/x86/kernel/setup.c | 2 ++
arch/x86/lib/cmdline.c | 8 ++++++++
3 files changed, 18 insertions(+)

diff --git a/arch/x86/include/asm/setup.h b/arch/x86/include/asm/setup.h
index e61e68d..0667b2a 100644
--- a/arch/x86/include/asm/setup.h
+++ b/arch/x86/include/asm/setup.h
@@ -28,6 +28,8 @@
#define NEW_CL_POINTER 0x228 /* Relative to real mode data */

#ifndef __ASSEMBLY__
+#include <linux/cache.h>
+
#include <asm/bootparam.h>
#include <asm/x86_init.h>

@@ -133,6 +135,12 @@ asmlinkage void __init __noreturn x86_64_start_reservations(char *real_mode_data
#endif /* __i386__ */
#endif /* _SETUP */

+#ifdef CONFIG_CMDLINE_BOOL
+extern bool builtin_cmdline_added __ro_after_init;
+#else
+#define builtin_cmdline_added 0
+#endif
+
#else /* __ASSEMBLY */

.macro __RESERVE_BRK name, size
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
index 05c5aa9..728927e 100644
--- a/arch/x86/kernel/setup.c
+++ b/arch/x86/kernel/setup.c
@@ -165,6 +165,7 @@ unsigned long saved_video_mode;
static char __initdata command_line[COMMAND_LINE_SIZE];
#ifdef CONFIG_CMDLINE_BOOL
static char __initdata builtin_cmdline[COMMAND_LINE_SIZE] = CONFIG_CMDLINE;
+bool builtin_cmdline_added __ro_after_init;
#endif

#if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE)
@@ -765,6 +766,7 @@ void __init setup_arch(char **cmdline_p)
strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
}
#endif
+ builtin_cmdline_added = true;
#endif

strscpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
diff --git a/arch/x86/lib/cmdline.c b/arch/x86/lib/cmdline.c
index 80570eb..384da1f 100644
--- a/arch/x86/lib/cmdline.c
+++ b/arch/x86/lib/cmdline.c
@@ -6,8 +6,10 @@
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/ctype.h>
+
#include <asm/setup.h>
#include <asm/cmdline.h>
+#include <asm/bug.h>

static inline int myisspace(u8 c)
{
@@ -205,12 +207,18 @@ __cmdline_find_option(const char *cmdline, int max_cmdline_size,

int cmdline_find_option_bool(const char *cmdline, const char *option)
{
+ if (IS_ENABLED(CONFIG_CMDLINE_BOOL))
+ WARN_ON_ONCE(!builtin_cmdline_added);
+
return __cmdline_find_option_bool(cmdline, COMMAND_LINE_SIZE, option);
}

int cmdline_find_option(const char *cmdline, const char *option, char *buffer,
int bufsize)
{
+ if (IS_ENABLED(CONFIG_CMDLINE_BOOL))
+ WARN_ON_ONCE(!builtin_cmdline_added);
+
return __cmdline_find_option(cmdline, COMMAND_LINE_SIZE, option,
buffer, bufsize);
}