Skip to content

Commit 2de70d7

Browse files
jognessgregkh
authored andcommitted
printk: avoid prb_first_valid_seq() where possible
[ Upstream commit 13791c8 ] If message sizes average larger than expected (more than 32 characters), the data_ring will wrap before the desc_ring. Once the data_ring wraps, it will start invalidating descriptors. These invalid descriptors hang around until they are eventually recycled when the desc_ring wraps. Readers do not care about invalid descriptors, but they still need to iterate past them. If the average message size is much larger than 32 characters, then there will be many invalid descriptors preceding the valid descriptors. The function prb_first_valid_seq() always begins at the oldest descriptor and searches for the first valid descriptor. This can be rather expensive for the above scenario. And, in fact, because of its heavy usage in /dev/kmsg, there have been reports of long delays and even RCU stalls. For code that does not need to search from the oldest record, replace prb_first_valid_seq() usage with prb_read_valid_*() functions, which provide a start sequence number to search from. Fixes: 896fbe2 ("printk: use the lockless ringbuffer") Reported-by: kernel test robot <oliver.sang@intel.com> Reported-by: J. Avila <elavila@google.com> Signed-off-by: John Ogness <john.ogness@linutronix.de> Reviewed-by: Petr Mladek <pmladek@suse.com> Signed-off-by: Petr Mladek <pmladek@suse.com> Link: https://lore.kernel.org/r/20210211173152.1629-1-john.ogness@linutronix.de Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 86f5b09 commit 2de70d7

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

‎kernel/printk/printk.c‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -782,9 +782,9 @@ static ssize_t devkmsg_read(struct file *file, char __user *buf,
782782
logbuf_lock_irq();
783783
}
784784

785-
if (user->seq < prb_first_valid_seq(prb)) {
785+
if (r->info->seq != user->seq) {
786786
/* our last seen message is gone, return error and reset */
787-
user->seq = prb_first_valid_seq(prb);
787+
user->seq = r->info->seq;
788788
ret = -EPIPE;
789789
logbuf_unlock_irq();
790790
goto out;
@@ -859,6 +859,7 @@ static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence)
859859
static __poll_t devkmsg_poll(struct file *file, poll_table *wait)
860860
{
861861
struct devkmsg_user *user = file->private_data;
862+
struct printk_info info;
862863
__poll_t ret = 0;
863864

864865
if (!user)
@@ -867,9 +868,9 @@ static __poll_t devkmsg_poll(struct file *file, poll_table *wait)
867868
poll_wait(file, &log_wait, wait);
868869

869870
logbuf_lock_irq();
870-
if (prb_read_valid(prb, user->seq, NULL)) {
871+
if (prb_read_valid_info(prb, user->seq, &info, NULL)) {
871872
/* return error when data has vanished underneath us */
872-
if (user->seq < prb_first_valid_seq(prb))
873+
if (info.seq != user->seq)
873874
ret = EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI;
874875
else
875876
ret = EPOLLIN|EPOLLRDNORM;
@@ -1606,6 +1607,7 @@ static void syslog_clear(void)
16061607

16071608
int do_syslog(int type, char __user *buf, int len, int source)
16081609
{
1610+
struct printk_info info;
16091611
bool clear = false;
16101612
static int saved_console_loglevel = LOGLEVEL_DEFAULT;
16111613
int error;
@@ -1676,9 +1678,14 @@ int do_syslog(int type, char __user *buf, int len, int source)
16761678
/* Number of chars in the log buffer */
16771679
case SYSLOG_ACTION_SIZE_UNREAD:
16781680
logbuf_lock_irq();
1679-
if (syslog_seq < prb_first_valid_seq(prb)) {
1681+
if (!prb_read_valid_info(prb, syslog_seq, &info, NULL)) {
1682+
/* No unread messages. */
1683+
logbuf_unlock_irq();
1684+
return 0;
1685+
}
1686+
if (info.seq != syslog_seq) {
16801687
/* messages are gone, move to first one */
1681-
syslog_seq = prb_first_valid_seq(prb);
1688+
syslog_seq = info.seq;
16821689
syslog_partial = 0;
16831690
}
16841691
if (source == SYSLOG_FROM_PROC) {
@@ -1690,7 +1697,6 @@ int do_syslog(int type, char __user *buf, int len, int source)
16901697
error = prb_next_seq(prb) - syslog_seq;
16911698
} else {
16921699
bool time = syslog_partial ? syslog_time : printk_time;
1693-
struct printk_info info;
16941700
unsigned int line_count;
16951701
u64 seq;
16961702

@@ -3378,9 +3384,11 @@ bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
33783384
goto out;
33793385

33803386
logbuf_lock_irqsave(flags);
3381-
if (dumper->cur_seq < prb_first_valid_seq(prb)) {
3382-
/* messages are gone, move to first available one */
3383-
dumper->cur_seq = prb_first_valid_seq(prb);
3387+
if (prb_read_valid_info(prb, dumper->cur_seq, &info, NULL)) {
3388+
if (info.seq != dumper->cur_seq) {
3389+
/* messages are gone, move to first available one */
3390+
dumper->cur_seq = info.seq;
3391+
}
33843392
}
33853393

33863394
/* last entry */

0 commit comments

Comments
 (0)