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

From: Petr Mladek
Date: Mon Jan 02 2023 - 11:19:52 EST


On Wed 2022-12-21 21:33:04, 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 output in a single 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>

> --- a/kernel/printk/printk.c
> +++ b/kernel/printk/printk.c
> @@ -2724,6 +2699,50 @@ static void __console_unlock(void)
> up_console_sem();
> }
>
> +/*
> + * Prepend the message in @cmsg->cbufs->outbuf with a "dropped message". This
> + * is achieved by shifting the existing message over and inserting the dropped
> + * message.
> + *
> + * @cmsg is the console message to prepend.
> + *
> + * @dropped is the dropped count to report in the dropped message.
> + *
> + * If the message text in @cmsg->cbufs->outbuf does not have enough space for
> + * the dropped message, the message text will be sufficiently truncated.
> + *
> + * If @cmsg->cbufs->outbuf is modified, @cmsg->outbuf_len is updated.
> + */
> +static void console_prepend_dropped(struct console_message *cmsg, unsigned long dropped)
> +{
> + struct console_buffers *cbufs = cmsg->cbufs;
> + const size_t scratchbuf_sz = sizeof(cbufs->scratchbuf);
> + const size_t outbuf_sz = sizeof(cbufs->outbuf);
> + char *scratchbuf = &cbufs->scratchbuf[0];
> + char *outbuf = &cbufs->outbuf[0];
> + size_t len;
> +
> + len = snprintf(scratchbuf, scratchbuf_sz,
> + "** %lu printk messages dropped **\n", dropped);
> +
> + /*
> + * Make sure outbuf is sufficiently large before prepending. Space
> + * for a terminator is also counted in case truncation occurs.
> + */
> + if (WARN_ON_ONCE(len + 1 >= outbuf_sz))
> + return;

Strictly speaking, this should be:

if (WARN_ON_ONCE(len >= outbuf_sz))
return;

The trailing '\0' will fit into the buffer when len < outbuf_sz.

That said, the consoles would be almost unusable when we are anywhere close this
limit. Most messages would be truncated.

> +
> + if (cmsg->outbuf_len + len >= outbuf_sz) {
> + /* Truncate the message, but keep it terminated. */
> + cmsg->outbuf_len = outbuf_sz - (len + 1);
> + outbuf[cmsg->outbuf_len] = 0;
> + }
> +
> + memmove(outbuf + len, outbuf, cmsg->outbuf_len + 1);
> + memcxpy(outbuf, scratchbuf, len);
> + cmsg->outbuf_len += len;
> +}
> +
> /*
> * Read and format the specified record (or a later record if the specified
> * record is not available).

Otherwise, the change looks fine:

Reviewed-by: Petr Mladek <pmladek@xxxxxxxx>

Best Regards,
Petr