Re: [PATCH] init: Remove strlcat() and sprintf() from do_one_initcall()

From: David Laight

Date: Sun Nov 15 2026 - 09:53:44 EST


On Sat, 14 Nov 2026 12:05:31 +0100
Thorsten Blum <blum@xxxxxxxxxx> wrote:

> In preparation for removing strlcat() from the kernel [1], format the
> warning directly in WARN(). This also removes the temporary 64-byte
> buffer and the sprintf() call while preserving the warning text.
>
> Link: https://github.com/KSPP/linux/issues/370 [1]
> Signed-off-by: Thorsten Blum <blum@xxxxxxxxxx>

This one actually makes sense!
I might have used 'const char *' variables and removed the ' ' before
the '\n'. But:

Reviewed-by: David Laight <david.laight.linux@xxxxxxxxx>

> ---
> init/main.c | 21 +++++++++++----------
> 1 file changed, 11 insertions(+), 10 deletions(-)
>
> diff --git a/init/main.c b/init/main.c
> index 93a2b12976f0..430b403e0880 100644
> --- a/init/main.c
> +++ b/init/main.c
> @@ -1397,7 +1397,8 @@ static inline void do_trace_initcall_level(const char *level)
> int __init_or_module do_one_initcall(initcall_t fn)
> {
> int count = preempt_count();
> - char msgbuf[64];
> + bool preempt_imbalanced;
> + bool irqs_left_disabled;
> int ret;
>
> if (initcall_blacklisted(fn))
> @@ -1407,17 +1408,17 @@ int __init_or_module do_one_initcall(initcall_t fn)
> ret = fn();
> do_trace_initcall_finish(fn, ret);
>
> - msgbuf[0] = 0;
> -
> - if (preempt_count() != count) {
> - sprintf(msgbuf, "preemption imbalance ");
> + preempt_imbalanced = preempt_count() != count;
> + if (preempt_imbalanced)
> preempt_count_set(count);
> - }
> - if (irqs_disabled()) {
> - strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf));
> +
> + irqs_left_disabled = irqs_disabled();
> + if (irqs_left_disabled)
> local_irq_enable();
> - }
> - WARN(msgbuf[0], "initcall %pS returned with %s\n", fn, msgbuf);
> +
> + WARN(preempt_imbalanced || irqs_left_disabled, "initcall %pS returned with %s%s\n", fn,
> + preempt_imbalanced ? "preemption imbalance " : "",
> + irqs_left_disabled ? "disabled interrupts " : "");
>
> add_latent_entropy();
> return ret;
>