Re: [PATCH v2 0/2] printk_ringbuffer: don't needlessly wrap data blocks around

John Ogness
2025-09-15T15:25:09Z

On 2025-09-15, Petr Mladek <pmladek@suse.com> wrote:
> It might be possible to catch this in either in data_alloc().
> or in get_next_lpos(). They could ignore/yell about when
> the really occupied space would be bigger than DATA_SIZE(data_ring).
>
> Something like:
>
> diff --git a/kernel/printk/printk_ringbuffer.c b/kernel/printk/printk_ringbuffer.c
> index 17b741b2eccd..d7ba4c0d8c3b 100644
> --- a/kernel/printk/printk_ringbuffer.c
> +++ b/kernel/printk/printk_ringbuffer.c
> @@ -1056,8 +1056,16 @@ static char *data_alloc(struct printk_ringbuffer *rb, unsigned int size,
> do {
> next_lpos = get_next_lpos(data_ring, begin_lpos, size);
>
> - if (!data_push_tail(rb, next_lpos - DATA_SIZE(data_ring))) {
> - /* Failed to allocate, specify a data-less block. */
> + /*
> + * Double check that the really used space won't be bigger than
> + * the ring buffer. Wrapped messages need to reserve more space,
> + * see get_next_lpos.
> + *
> + * Specify a data-less block when the check or the allocation
> + * fails.
> + */
> + if (WARN_ON_ONCE(next_lpos - begin_lpos > DATA_SIZE(data_ring)) ||
> + !data_push_tail(rb, next_lpos - DATA_SIZE(data_ring))) {
> blk_lpos->begin = FAILED_LPOS;
> blk_lpos->next = FAILED_LPOS;
> return NULL;
>
>
> Similar check would need to be done also in data_realloc().

I like this. It is an important sanity check and safe error handling in
case (for whatever reason) the data ring gets corrupted.

John