Re: [PATCH v3] init/main.c: Wrap long kernel cmdline when printing to logs

From: Doug Anderson
Date: Fri Oct 24 2025 - 12:15:42 EST


Hi,

On Fri, Oct 24, 2025 at 12:51 AM Geert Uytterhoeven
<geert@xxxxxxxxxxxxxx> wrote:
>
> > +config CMDLINE_LOG_WRAP_IDEAL_LEN
> > + int "Length to try to wrap the cmdline when logged at boot"
> > + default 1021
> > + range -1 1021
>
> Apparently COMMAND_LINE_SIZE is still smaller than 1021 on several
> architectures (even in asm-generic: 512). Unfortunately only s390
> still controls it through a config option, so you cannot have a
> "depends on COMMAND_LINE_SIZE > 1021" here...
>
> > + help
> > + At boot time, the kernel command line is logged to the console.
> > + The log message will start with the prefix "Kernel command line: ".
> > + The log message will attempt to be wrapped (split into multiple log
> > + messages) at spaces based on CMDLINE_LOG_WRAP_IDEAL_LEN characters.
> > + If wrapping happens, each log message will start with the prefix and
> > + all but the last message will end with " \". Messages may exceed the
> > + ideal length if a place to wrap isn't found before the specified
> > + number of characters.
> > +
> > + A value of -1 disables wrapping, though be warned that the maximum
>
> Or zero, right?
> So perhaps just use range 0 1021.

Sure, we can use 0 as the sentinel value. I was thinking -1 would be a
more obvious "wrapping is totally disabled" value but I don't feel
strongly about it. I'll change to 0 in the next patch.


> > + length of a log message (1021 characters) may cause the cmdline to
> > + be truncated.
> > +
> > config INITRAMFS_PRESERVE_MTIME
> > bool "Preserve cpio archive mtimes in initramfs"
> > depends on BLK_DEV_INITRD
>
> > --- a/init/main.c
> > +++ b/init/main.c
>
> > +static void print_kernel_cmdline(const char *cmdline)
> > +{
> > + size_t len;
> > +
> > + /* Config option of -1 disables wrapping */
> > + if (CONFIG_CMDLINE_LOG_WRAP_IDEAL_LEN < 0) {
>
> As does zero, right?

As the code is written right now, 0 does not disable wrapping. The
code treats everything less than 23 characters (the length of "Kernel
command line: " plus " \") to mean "wrap everything at the first
space". Yes, it's a little weird to do it this way, but it was either
that or add yet-another KConfig setting to disable wrapping and then
set the minimum to something higher. In v1/v2 I had the minimum set to
40 specifically to avoid the confusing case. There was previous
discussion about this in v2 [1].

...but yes, we can choose to make 0 be the special sentinel to disable
wrapping. I'll assume that's what you want and I'll change it in the
next version.


> You can add a check for "COMMAND_LINE_SIZE <= 1021" here, so the
> compiler will eliminate the splitting code when it is not needed.

As the Kconfig is described and as the code is written, someone could
still choose wrapping even if COMMAND_LINE_SIZE <= 1021. Someone
could, for instance, choose to wrap these lines at 100 or 200
characters to make the log message look cleaner in their terminal/text
editor. ...but you're right that I can write this to be more optimal
for folks with shorter command lines who haven't touched the config.
I'll change the "if" test to this:

if (CONFIG_CMDLINE_LOG_WRAP_IDEAL_LEN == 0 ||
IDEAL_CMDLINE_LEN >= COMMAND_LINE_SIZE - 1) {
pr_noitce(...);
return;
}

That changes the sentinel to 0 (as I think you requested) and should
allow the compiler to optimize things out.

Making sure I got my math correct... Let me know if you see something wrong.

KERNEL_CMDLINE_PREFIX_LEN is 21

Assuming the CONFIG value isn't tiny, IDEAL_CMDLINE_LEN is the CONFIG
value minus 21.

So let's assume COMMAND_LINE_SIZE is 256. That means we can have at
most a cmdline length 255 to handle the '\0' termination.

So if the CONFIG value is (255 + 21) = 276 or more then we should hit
the "if" test and early-out because we can't wrap. If the CONFIG value
is 275 or less then we should proceed to wrapping.

We want ((276 - 21) >= (256 - 1)) to be true. It is.

We want ((275 - 21) >= (256 - 1)) to be false. It is.


I'll wait a few days and send a v4. I'll fold in Andrew's "__init"
patch as well.


[1] http://lore.kernel.org/r/CAD=FV=WFbH6kBMcoHNwQzsay6ecQQ2sZ3qc-=XTboFXK+RSspA@xxxxxxxxxxxxxx


-Doug