Re: [PATCH printk v4 6/8] printk: introduce console_prepend_dropped() for dropped messages

From: Petr Mladek
Date: Thu Jan 05 2023 - 11:17:19 EST


On Thu 2023-01-05 11:43:33, John Ogness wrote:
> Currently "dropped messages" are separately printed immediately
> before printing the printk message. Since normal consoles are
> now using an output buffer that is much larger than previously,
> the "dropped message" could be prepended to the printk message
> and then output everything in a single write() call.
>
> Introduce a helper function console_prepend_dropped() to prepend
> an existing message with a "dropped message". This simplifies
> the code by allowing all message formatting to be handled
> together and then only requires a single write() call to output
> the full message. And since this helper does not require any
> locking, it can be used in the future for other console printing
> contexts as well.
>
> Signed-off-by: John Ogness <john.ogness@xxxxxxxxxxxxx>

Reviewed-by: Petr Mladek <pmladek@xxxxxxxx>

A comment below.

> --- a/kernel/printk/printk.c
> +++ b/kernel/printk/printk.c
> @@ -2724,6 +2699,52 @@ static void __console_unlock(void)
> up_console_sem();
> }
>
> +/*
> + * Prepend the message in @pmsg->pbufs->outbuf with a "dropped message". This
> + * is achieved by shifting the existing message over and inserting the dropped
> + * message.
> + *
> + * @pmsg is the printk message to prepend.
> + *
> + * @dropped is the dropped count to report in the dropped message.
> + *
> + * If the message text in @pmsg->pbufs->outbuf does not have enough space for
> + * the dropped message, the message text will be sufficiently truncated.
> + *
> + * If @pmsg->pbufs->outbuf is modified, @pmsg->outbuf_len is updated.
> + */
> +static void console_prepend_dropped(struct printk_message *pmsg, unsigned long dropped)
> +{
> + struct printk_buffers *pbufs = pmsg->pbufs;
> + const size_t scratchbuf_sz = sizeof(pbufs->scratchbuf);
> + const size_t outbuf_sz = sizeof(pbufs->outbuf);
> + char *scratchbuf = &pbufs->scratchbuf[0];
> + char *outbuf = &pbufs->outbuf[0];
> + size_t len;
> +
> + len = snprintf(scratchbuf, scratchbuf_sz,
> + "** %lu printk messages dropped **\n", dropped);
> +
> + /*
> + * Make sure outbuf is sufficiently large before prepending.
> + * Keep at least the prefix when the message must be truncated.
> + * It is a rather theoretical problem when someone tries to
> + * use a minimalist buffer.
> + */
> + if (WARN_ON_ONCE(len + PREFIX_MAX >= outbuf_sz))
> + return;

I guess that this will always trigger the compiler warning
when CONFIG_PRINTK is disabled. See the report for v3 at
https://lore.kernel.org/r/202301052114.vvN3wQoH-lkp@xxxxxxxxx

Hmm, we might want to fix this warning so that it does not break
build with -Werror.

IMHO, the proper solution would be to define this function only when
CONFIG_PRINTK is defined. But it might require bigger changes
and define many more console functions only when CONFIG_PRINTK
is defined. This is out-of-scope of this patchset.

I wonder if the following would work as an "intermediate" workaround:

if (!IS_ENABLED(CONFIG_PRINTK) ||
WARN_ON_ONCE(len + PREFIX_MAX >= outbuf_sz))
return;


Best Regards,
Petr