printk: ringbuffer: Improve prb_next_seq() performance
authorPetr Mladek <pmladek@suse.com>
Fri, 21 Jan 2022 13:06:28 +0000 (18:36 +0530)
committerPetr Mladek <pmladek@suse.com>
Wed, 26 Jan 2022 15:00:32 +0000 (16:00 +0100)
commitf244b4dc53e520d4570b2610436aba0593ce6f55
tree4258a27d59e985d40193ade0de8418bdb8b7f415
parent11e4b63abbe23872b45f325a7c6c8b7f9ff42cad
printk: ringbuffer: Improve prb_next_seq() performance

prb_next_seq() always iterates from the first known sequence number.
In the worst case, it might loop 8k times for 256kB buffer,
15k times for 512kB buffer, and 64k times for 2MB buffer.

It was reported that polling and reading using syslog interface
might occupy 50% of CPU.

Speedup the search by storing @id of the last finalized descriptor.

The loop is still needed because the @id is stored and read in the best
effort way. An atomic variable is used to keep the @id consistent.
But the stores and reads are not serialized against each other.
The descriptor could get reused in the meantime. The related sequence
number will be used only when it is still valid.

An invalid value should be read _only_ when there is a flood of messages
and the ringbuffer is rapidly reused. The performance is the least
problem in this case.

Reported-by: Chunlei Wang <chunlei.wang@mediatek.com>
Signed-off-by: Mukesh Ojha <quic_mojha@quicinc.com>
Reviewed-by: John Ogness <john.ogness@linutronix.de>
Signed-off-by: Petr Mladek <pmladek@suse.com>
Link: https://lore.kernel.org/r/1642770388-17327-1-git-send-email-quic_mojha@quicinc.com
Link: https://lore.kernel.org/lkml/YXlddJxLh77DKfIO@alley/T/#m43062e8b2a17f8dbc8c6ccdb8851fb0dbaabbb14
kernel/printk/printk_ringbuffer.c
kernel/printk/printk_ringbuffer.h