Skip to contentSkip to conversation

kexec: Add bootconfig support to get_command_line

RFC In review Usama Arif 4 participants 6 messages Aug 11, 2025 – Aug 22, 2025

Subsystems (2)
lore
Share & tools
lore

Usama Arif

author
#0 lore raw

RFC 1/1 kexec: Add bootconfig support to get_command_line

If the kernel is enabled with CONFIG_BOOT_CONFIG_FORCE, it will always append the bootconfig at the start of the kernel commandline. If reuse-cmdline option is used in kexec, the bootconfig will be repeatedly appended at the start of the commandline on every kexec. As there is a limit on the size of kernel commandline that can be used, --reuse-cmdline option will break kexec depending on the size of bootconfig on repeated kexec.

Bootconfig is embedded in the kernel, so it should not be considered when reusing kernel command line.
Modify get_command_line() to first attempt reading kernel parameters from /proc/bootconfig before falling back to /proc/cmdline. This allows kexec to use the original boot parameters without bootconfig which is embedded in the kernel.

The function will fallback to /proc/cmdline behavior when:

- /proc/bootconfig doesn't exist
- /proc/bootconfig is empty
- /proc/bootconfig doesn't contain the expected marker format
- The extracted parameter line is empty

---
 kexec/kexec.c | 50 ++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 44 insertions(+), 6 deletions(-)
kexec/kexec.c +44 -6
index 6bf12d7..b31cb1f 100644
@@ -1235,15 +1235,53 @@ static char *slurp_proc_file(const char *filename, size_t *len)
  */
 char *get_command_line(void)
 {
-	char *p, *line;
+	char *p, *line = NULL;
 	size_t size;
+	char *bootconfig_line;
+	size_t bootconfig_size;
+
+	/* First try to get command line parameters from /proc/bootconfig */
+	bootconfig_line = slurp_proc_file("/proc/bootconfig", &bootconfig_size);
+	if (bootconfig_line && bootconfig_size > 0) {
+		/* Look for "# Parameters from bootloader:" */
+		char *params_marker = strstr(bootconfig_line, "# Parameters from bootloader:");
+		if (params_marker) {
+			/* Find the next line after the marker */
+			char *params_start = strchr(params_marker, '\n');
+			if (params_start) {
+				params_start++; /* Move past the newline */
+
+				/* Check if this line starts with "# " */
+				if (strncmp(params_start, "# ", 2) == 0) {
+					/* Skip the "# " prefix */
+					params_start += 2;
+
+					/* Find the end of the line */
+					char *params_end = strchr(params_start, '\n');
+					if (params_end) {
+						*params_end = '\0';
+					}
+
+					/* Check if the extracted line is not empty */
+					if (strlen(params_start) > 0) {
+						/* Allocate and copy the parameters */
+						line = xstrdup(params_start);
+					}
+				}
+			}
+		}
+		free(bootconfig_line);
+	}
 
-	line = slurp_proc_file("/proc/cmdline", &size);
-	if (!line || !size)
-		die("Failed to read /proc/cmdline\n");
+	/* Fall back to reading /proc/cmdline if we didn't get a line from bootconfig */
+	if (!line) {
+		line = slurp_proc_file("/proc/cmdline", &size);
+		if (!line || !size)
+			die("Failed to read /proc/cmdline\n");
 
-	/* strip newline */
-	line[size-1] = '\0';
+		/* strip newline */
+		line[size-1] = '\0';
+	}
 
 	p = strpbrk(line, "\r\n");
 	if (p)
Signature · 1 line
2.47.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · Usama Arif

If the kernel is enabled with CONFIG_BOOT_CONFIG_FORCE, it will always append the bootconfig at the start of the kernel commandline. If reuse-cmdline option is used in kexec, the bootconfig will be repeatedly appended at the start of the commandline on every kexec. As there is a limit on the size of kernel commandline that can be used, --reuse-cmdline option will break kexec depending on the size of bootconfig on repeated kexec.

Bootconfig is embedded in the kernel, so it should not be considered when reusing kernel command line.
Modify get_command_line() to first attempt reading kernel parameters from /proc/bootconfig before falling back to /proc/cmdline. This allows kexec to use the original boot parameters without bootconfig which is embedded in the kernel.

Quoted 8 lines

The function will fallback to /proc/cmdline behavior when:

- /proc/bootconfig doesn't exist
- /proc/bootconfig is empty
- /proc/bootconfig doesn't contain the expected marker format
- The extracted parameter line is empty

Quoted 71 lines
---
 kexec/kexec.c | 50 ++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 44 insertions(+), 6 deletions(-)
kexec/kexec.c +44 -6
index 6bf12d7..b31cb1f 100644
@@ -1235,15 +1235,53 @@ static char *slurp_proc_file(const char *filename, size_t *len)
  */
 char *get_command_line(void)
 {
-	char *p, *line;
+	char *p, *line = NULL;
 	size_t size;
+	char *bootconfig_line;
+	size_t bootconfig_size;
+
+	/* First try to get command line parameters from /proc/bootconfig */
+	bootconfig_line = slurp_proc_file("/proc/bootconfig", &bootconfig_size);
+	if (bootconfig_line && bootconfig_size > 0) {
+		/* Look for "# Parameters from bootloader:" */
+		char *params_marker = strstr(bootconfig_line, "# Parameters from bootloader:");
+		if (params_marker) {
+			/* Find the next line after the marker */
+			char *params_start = strchr(params_marker, '\n');
+			if (params_start) {
+				params_start++; /* Move past the newline */
+
+				/* Check if this line starts with "# " */
+				if (strncmp(params_start, "# ", 2) == 0) {
+					/* Skip the "# " prefix */
+					params_start += 2;
+
+					/* Find the end of the line */
+					char *params_end = strchr(params_start, '\n');
+					if (params_end) {
+						*params_end = '\0';
+					}
+
+					/* Check if the extracted line is not empty */
+					if (strlen(params_start) > 0) {
+						/* Allocate and copy the parameters */
+						line = xstrdup(params_start);
+					}
+				}
+			}
+		}
+		free(bootconfig_line);
+	}
 
-	line = slurp_proc_file("/proc/cmdline", &size);
-	if (!line || !size)
-		die("Failed to read /proc/cmdline\n");
+	/* Fall back to reading /proc/cmdline if we didn't get a line from bootconfig */
+	if (!line) {
+		line = slurp_proc_file("/proc/cmdline", &size);
+		if (!line || !size)
+			die("Failed to read /proc/cmdline\n");
 
-	/* strip newline */
-	line[size-1] = '\0';
+		/* strip newline */
+		line[size-1] = '\0';
+	}
 
 	p = strpbrk(line, "\r\n");
 	if (p)
Signature · 1 line
2.47.3
Reviewed-by on v1 1/1
#2 lore raw
In reply to Usama Arif
Quoted 15 lines · Usama Arif

If the kernel is enabled with CONFIG_BOOT_CONFIG_FORCE, it will always append the bootconfig at the start of the kernel commandline. If reuse-cmdline option is used in kexec, the bootconfig will be repeatedly appended at the start of the commandline on every kexec. As there is a limit on the size of kernel commandline that can be used, --reuse-cmdline option will break kexec depending on the size of bootconfig on repeated kexec.

Bootconfig is embedded in the kernel, so it should not be considered when reusing kernel command line.
Modify get_command_line() to first attempt reading kernel parameters from /proc/bootconfig before falling back to /proc/cmdline. This allows kexec to use the original boot parameters without bootconfig which is embedded in the kernel.

Quoted 8 lines

The function will fallback to /proc/cmdline behavior when:

- /proc/bootconfig doesn't exist
- /proc/bootconfig is empty
- /proc/bootconfig doesn't contain the expected marker format
- The extracted parameter line is empty

#3 lore raw
In reply to Usama Arif

Hi,

Quoted 8 lines · Usama Arif

If the kernel is enabled with CONFIG_BOOT_CONFIG_FORCE, it will always append the bootconfig at the start of the kernel commandline. If reuse-cmdline option is used in kexec, the bootconfig will be repeatedly appended at the start of the commandline on every kexec. As there is a limit on the size of kernel commandline that can be used, --reuse-cmdline option will break kexec depending on the size of bootconfig on repeated kexec.

Quoted 6 lines

Bootconfig is embedded in the kernel, so it should not be considered when reusing kernel command line.
Modify get_command_line() to first attempt reading kernel parameters from /proc/bootconfig before falling back to /proc/cmdline. This allows kexec to use the original boot parameters without bootconfig which is embedded in the kernel.

Thanks for the great fix. While I have some questions about the change.

1, if we can read kernel parameters from /proc/bootconfig, does it mean
we will skip other kernel parameters, e.g I set some parameters in grub, meanwhile set bootconfg? Is this allowed?

2. If /proc/bootconfig includes the whole kernel parameters of the runnign kernel, setup_boot_config() will parse and append bootconfig to kernel cmdline again, right? But it doesn't matter.

Sorry, I am not familiar with bootconfig.

Thanks
Baoquan

Quoted 81 lines

The function will fallback to /proc/cmdline behavior when:

- /proc/bootconfig doesn't exist
- /proc/bootconfig is empty
- /proc/bootconfig doesn't contain the expected marker format
- The extracted parameter line is empty

---
 kexec/kexec.c | 50 ++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 44 insertions(+), 6 deletions(-)
kexec/kexec.c +44 -6
index 6bf12d7..b31cb1f 100644
@@ -1235,15 +1235,53 @@ static char *slurp_proc_file(const char *filename, size_t *len)
  */
 char *get_command_line(void)
 {
-	char *p, *line;
+	char *p, *line = NULL;
 	size_t size;
+	char *bootconfig_line;
+	size_t bootconfig_size;
+
+	/* First try to get command line parameters from /proc/bootconfig */
+	bootconfig_line = slurp_proc_file("/proc/bootconfig", &bootconfig_size);
+	if (bootconfig_line && bootconfig_size > 0) {
+		/* Look for "# Parameters from bootloader:" */
+		char *params_marker = strstr(bootconfig_line, "# Parameters from bootloader:");
+		if (params_marker) {
+			/* Find the next line after the marker */
+			char *params_start = strchr(params_marker, '\n');
+			if (params_start) {
+				params_start++; /* Move past the newline */
+
+				/* Check if this line starts with "# " */
+				if (strncmp(params_start, "# ", 2) == 0) {
+					/* Skip the "# " prefix */
+					params_start += 2;
+
+					/* Find the end of the line */
+					char *params_end = strchr(params_start, '\n');
+					if (params_end) {
+						*params_end = '\0';
+					}
+
+					/* Check if the extracted line is not empty */
+					if (strlen(params_start) > 0) {
+						/* Allocate and copy the parameters */
+						line = xstrdup(params_start);
+					}
+				}
+			}
+		}
+		free(bootconfig_line);
+	}
 
-	line = slurp_proc_file("/proc/cmdline", &size);
-	if (!line || !size)
-		die("Failed to read /proc/cmdline\n");
+	/* Fall back to reading /proc/cmdline if we didn't get a line from bootconfig */
+	if (!line) {
+		line = slurp_proc_file("/proc/cmdline", &size);
+		if (!line || !size)
+			die("Failed to read /proc/cmdline\n");
 
-	/* strip newline */
-	line[size-1] = '\0';
+		/* strip newline */
+		line[size-1] = '\0';
+	}
 
 	p = strpbrk(line, "\r\n");
 	if (p)
Signature · 1 line
2.47.3
author
#4 lore raw
Quoted 18 lines · Baoquan He

Hi,

Quoted 8 lines · Usama Arif

If the kernel is enabled with CONFIG_BOOT_CONFIG_FORCE, it will always append the bootconfig at the start of the kernel commandline. If reuse-cmdline option is used in kexec, the bootconfig will be repeatedly appended at the start of the commandline on every kexec. As there is a limit on the size of kernel commandline that can be used, --reuse-cmdline option will break kexec depending on the size of bootconfig on repeated kexec.

Quoted 6 lines

Bootconfig is embedded in the kernel, so it should not be considered when reusing kernel command line.
Modify get_command_line() to first attempt reading kernel parameters from /proc/bootconfig before falling back to /proc/cmdline. This allows kexec to use the original boot parameters without bootconfig which is embedded in the kernel.

Quoted 5 lines

Thanks for the great fix. While I have some questions about the change.

1, if we can read kernel parameters from /proc/bootconfig, does it mean
we will skip other kernel parameters, e.g I set some parameters in grub, meanwhile set bootconfg? Is this allowed?

Hi Baoquan,

Thanks for the questions.

/proc/bootconfig treats the parameters from bootconfig separate from the rest. i.e. parameters from grub will be coming after "# Parameters from bootloader:" in /proc/bootconfig. It shouldn't skip grub parameters. For e.g. on my machine

cat /proc/bootconfig
kernel.csdlock_debug = "1"
kernel.mitigations = "off"
# Parameters from bootloader:
# crashkernel=.....

kernel.csdlock_debug = "1"
kernel.mitigations = "off"
comes from bootconfig, while the crashkernel and the rest come from bootloader [1]

[1] https://elixir.bootlin.com/linux/v6.16.1/source/Documentation/filesystems/proc.rst#L739

Quoted 4 lines

2. If /proc/bootconfig includes the whole kernel parameters of the runnign kernel, setup_boot_config() will parse and append bootconfig to kernel cmdline again, right? But it doesn't matter.

/proc/bootconfig contains the whole parameters, but the bootconfig is seperated by "# Parameters from bootloader:". If we repeatedly kexec using /proc/cmdline, we will continuously append bootconfig,
and wont be able to kexec anymore once the commandline gets too big..

Quoted 6 lines

Sorry, I am not familiar with bootconfig.

Thanks
Baoquan

Acked-by
#5 lore raw
Quoted 49 lines · Usama Arif
Quoted 18 lines · Baoquan He

Hi,

Quoted 8 lines · Usama Arif

If the kernel is enabled with CONFIG_BOOT_CONFIG_FORCE, it will always append the bootconfig at the start of the kernel commandline. If reuse-cmdline option is used in kexec, the bootconfig will be repeatedly appended at the start of the commandline on every kexec. As there is a limit on the size of kernel commandline that can be used, --reuse-cmdline option will break kexec depending on the size of bootconfig on repeated kexec.

Quoted 6 lines

Bootconfig is embedded in the kernel, so it should not be considered when reusing kernel command line.
Modify get_command_line() to first attempt reading kernel parameters from /proc/bootconfig before falling back to /proc/cmdline. This allows kexec to use the original boot parameters without bootconfig which is embedded in the kernel.

Quoted 5 lines

Thanks for the great fix. While I have some questions about the change.

1, if we can read kernel parameters from /proc/bootconfig, does it mean
we will skip other kernel parameters, e.g I set some parameters in grub, meanwhile set bootconfg? Is this allowed?

Hi Baoquan,

Thanks for the questions.

/proc/bootconfig treats the parameters from bootconfig separate from the rest. i.e. parameters from grub will be coming after "# Parameters from bootloader:" in /proc/bootconfig. It shouldn't skip grub parameters. For e.g. on my machine

cat /proc/bootconfig
kernel.csdlock_debug = "1"
kernel.mitigations = "off"
# Parameters from bootloader:
# crashkernel=.....

kernel.csdlock_debug = "1"
kernel.mitigations = "off"
comes from bootconfig, while the crashkernel and the rest come from bootloader [1]

[1] https://elixir.bootlin.com/linux/v6.16.1/source/Documentation/filesystems/proc.rst#L739

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

2. If /proc/bootconfig includes the whole kernel parameters of the runnign kernel, setup_boot_config() will parse and append bootconfig to kernel cmdline again, right? But it doesn't matter.

/proc/bootconfig contains the whole parameters, but the bootconfig is seperated by "# Parameters from bootloader:". If we repeatedly kexec using /proc/cmdline, we will continuously append bootconfig,
and wont be able to kexec anymore once the commandline gets too big..

I see, then it should be OK. I thought /proc/bootconfig only contains the content of bootconfig, and was worried about the incomplete kernel cmdline passing to 2nd kernel. Thanks for the explanation.

For the patch,