All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC] kexec: Add bootconfig support to get_command_line
@ 2025-08-11 15:37 Usama Arif
  2025-08-11 16:21 ` Paul E. McKenney
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Usama Arif @ 2025-08-11 15:37 UTC (permalink / raw)
  To: kexec, horms; +Cc: paulmck, leitao, kernel-team, 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.

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

Signed-off-by: Usama Arif <usamaarif642@gmail.com>
---
 kexec/kexec.c | 50 ++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 44 insertions(+), 6 deletions(-)

diff --git a/kexec/kexec.c b/kexec/kexec.c
index 6bf12d7..b31cb1f 100644
--- a/kexec/kexec.c
+++ b/kexec/kexec.c
@@ -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)
-- 
2.47.3



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [RFC] kexec: Add bootconfig support to get_command_line
  2025-08-11 15:37 [RFC] kexec: Add bootconfig support to get_command_line Usama Arif
@ 2025-08-11 16:21 ` Paul E. McKenney
  2025-08-20 13:01 ` Breno Leitao
  2025-08-21  3:28 ` Baoquan He
  2 siblings, 0 replies; 6+ messages in thread
From: Paul E. McKenney @ 2025-08-11 16:21 UTC (permalink / raw)
  To: Usama Arif; +Cc: kexec, horms, leitao, kernel-team

On Mon, Aug 11, 2025 at 04:37:36PM +0100, Usama Arif wrote:
> 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
> 
> Signed-off-by: Usama Arif <usamaarif642@gmail.com>

Reviewed-by: Paul E. McKenney <paulmck@kernel.org>

> ---
>  kexec/kexec.c | 50 ++++++++++++++++++++++++++++++++++++++++++++------
>  1 file changed, 44 insertions(+), 6 deletions(-)
> 
> diff --git a/kexec/kexec.c b/kexec/kexec.c
> index 6bf12d7..b31cb1f 100644
> --- a/kexec/kexec.c
> +++ b/kexec/kexec.c
> @@ -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)
> -- 
> 2.47.3
> 


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC] kexec: Add bootconfig support to get_command_line
  2025-08-11 15:37 [RFC] kexec: Add bootconfig support to get_command_line Usama Arif
  2025-08-11 16:21 ` Paul E. McKenney
@ 2025-08-20 13:01 ` Breno Leitao
  2025-08-21  3:28 ` Baoquan He
  2 siblings, 0 replies; 6+ messages in thread
From: Breno Leitao @ 2025-08-20 13:01 UTC (permalink / raw)
  To: Usama Arif; +Cc: kexec, horms, paulmck, kernel-team

On Mon, Aug 11, 2025 at 04:37:36PM +0100, Usama Arif wrote:
> 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
> 
> Signed-off-by: Usama Arif <usamaarif642@gmail.com>

Reviewed-by: Breno Leitao <leitao@debian.org>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC] kexec: Add bootconfig support to get_command_line
  2025-08-11 15:37 [RFC] kexec: Add bootconfig support to get_command_line Usama Arif
  2025-08-11 16:21 ` Paul E. McKenney
  2025-08-20 13:01 ` Breno Leitao
@ 2025-08-21  3:28 ` Baoquan He
  2025-08-21 11:32   ` Usama Arif
  2 siblings, 1 reply; 6+ messages in thread
From: Baoquan He @ 2025-08-21  3:28 UTC (permalink / raw)
  To: Usama Arif; +Cc: kexec, horms, paulmck, leitao, kernel-team

Hi,

On 08/11/25 at 04:37pm, Usama Arif wrote:
> 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.

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

> 
> 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
> 
> Signed-off-by: Usama Arif <usamaarif642@gmail.com>
> ---
>  kexec/kexec.c | 50 ++++++++++++++++++++++++++++++++++++++++++++------
>  1 file changed, 44 insertions(+), 6 deletions(-)
> 
> diff --git a/kexec/kexec.c b/kexec/kexec.c
> index 6bf12d7..b31cb1f 100644
> --- a/kexec/kexec.c
> +++ b/kexec/kexec.c
> @@ -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)
> -- 
> 2.47.3
> 
> 



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC] kexec: Add bootconfig support to get_command_line
  2025-08-21  3:28 ` Baoquan He
@ 2025-08-21 11:32   ` Usama Arif
  2025-08-22  3:53     ` Baoquan He
  0 siblings, 1 reply; 6+ messages in thread
From: Usama Arif @ 2025-08-21 11:32 UTC (permalink / raw)
  To: Baoquan He; +Cc: kexec, horms, paulmck, leitao, kernel-team



On 21/08/2025 04:28, Baoquan He wrote:
> Hi,
> 
> On 08/11/25 at 04:37pm, Usama Arif wrote:
>> 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.
> 
> 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

> 
> 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..

> 
> Sorry, I am not familiar with bootconfig.
> 
> Thanks
> Baoquan
> 




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RFC] kexec: Add bootconfig support to get_command_line
  2025-08-21 11:32   ` Usama Arif
@ 2025-08-22  3:53     ` Baoquan He
  0 siblings, 0 replies; 6+ messages in thread
From: Baoquan He @ 2025-08-22  3:53 UTC (permalink / raw)
  To: Usama Arif; +Cc: kexec, horms, paulmck, leitao, kernel-team

On 08/21/25 at 12:32pm, Usama Arif wrote:
> 
> 
> On 21/08/2025 04:28, Baoquan He wrote:
> > Hi,
> > 
> > On 08/11/25 at 04:37pm, Usama Arif wrote:
> >> 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.
> > 
> > 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
> 
> > 
> > 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,

Acked-by: Baoquan He <bhe@redhat.com>



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-08-22 11:23 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-11 15:37 [RFC] kexec: Add bootconfig support to get_command_line Usama Arif
2025-08-11 16:21 ` Paul E. McKenney
2025-08-20 13:01 ` Breno Leitao
2025-08-21  3:28 ` Baoquan He
2025-08-21 11:32   ` Usama Arif
2025-08-22  3:53     ` Baoquan He

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.