[PATCH] Fix prb_next_seq() performance issue
chunlei.wang
Chunlei.wang at mediatek.com
Sun Oct 24 22:51:07 PDT 2021
Subject: [PATCH] Fix prb_next_seq() performance issue.
prb_next_seq() function have a performance issue, in bad case it will
check all the seqs. If log buffer is 256K, the number of while loop
is 8K, 512K is 16K, 2M is 64K, it will cause performance issue.
we have a process use do_syslog poll&read method to read kernel log,
sometime the cpu loading is 50%. So we add old_seq to reduce the
loop time. It only check new log times.
Signed-off-by: Chunlei Wang <chunlei.wang at mediatek.com>
---
kernel/printk/printk.c | 12 ++++++------
kernel/printk/printk_ringbuffer.c | 4 ++--
kernel/printk/printk_ringbuffer.h | 2 +-
3 files changed, 9 insertions(+), 9 deletions(-)
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index a8d0a58deebc..9c6d4d617a95 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -801,7 +801,7 @@ static loff_t devkmsg_llseek(struct file *file,
loff_t offset, int whence)
break;
case SEEK_END:
/* after the last record */
- atomic64_set(&user->seq, prb_next_seq(prb));
+ atomic64_set(&user->seq, prb_next_seq(prb,
latched_seq_read_nolock(&clear_seq)));
break;
default:
ret = -EINVAL;
@@ -1155,9 +1155,9 @@ void __init setup_log_buf(int early)
free -= text_size;
}
- if (seq != prb_next_seq(&printk_rb_static)) {
+ if (seq != prb_next_seq(&printk_rb_static, seq)) {
pr_err("dropped %llu messages\n",
- prb_next_seq(&printk_rb_static) - seq);
+ prb_next_seq(&printk_rb_static, seq) - seq);
}
pr_info("log_buf_len: %u bytes\n", log_buf_len);
@@ -1625,7 +1625,7 @@ static int syslog_print_all(char __user *buf, int
size, bool clear)
static void syslog_clear(void)
{
mutex_lock(&syslog_lock);
- latched_seq_write(&clear_seq, prb_next_seq(prb));
+ latched_seq_write(&clear_seq, prb_next_seq(prb, 0));
mutex_unlock(&syslog_lock);
}
@@ -1714,7 +1714,7 @@ int do_syslog(int type, char __user *buf, int
len, int source)
* for pending data, not the size; return the
count of
* records, not the length.
*/
- error = prb_next_seq(prb) - syslog_seq;
+ error = prb_next_seq(prb, syslog_seq) -
syslog_seq;
} else {
bool time = syslog_partial ? syslog_time :
printk_time;
unsigned int line_count;
@@ -3573,7 +3573,7 @@ EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer);
void kmsg_dump_rewind(struct kmsg_dump_iter *iter)
{
iter->cur_seq = latched_seq_read_nolock(&clear_seq);
- iter->next_seq = prb_next_seq(prb);
+ iter->next_seq = prb_next_seq(prb, iter->cur_seq);
}
EXPORT_SYMBOL_GPL(kmsg_dump_rewind);
diff --git a/kernel/printk/printk_ringbuffer.c
b/kernel/printk/printk_ringbuffer.c
index 8a7b7362c0dd..a910b79d9341 100644
--- a/kernel/printk/printk_ringbuffer.c
+++ b/kernel/printk/printk_ringbuffer.c
@@ -2003,9 +2003,9 @@ u64 prb_first_valid_seq(struct printk_ringbuffer
*rb)
* Return: The sequence number of the next newest (not yet available)
record
* for readers.
*/
-u64 prb_next_seq(struct printk_ringbuffer *rb)
+u64 prb_next_seq(struct printk_ringbuffer *rb, u64 old_seq)
{
- u64 seq = 0;
+ u64 seq = old_seq;
/* Search forward from the oldest descriptor. */
while (_prb_read_valid(rb, &seq, NULL, NULL))
diff --git a/kernel/printk/printk_ringbuffer.h
b/kernel/printk/printk_ringbuffer.h
index 73cc80e01cef..0729b21a9d34 100644
--- a/kernel/printk/printk_ringbuffer.h
+++ b/kernel/printk/printk_ringbuffer.h
@@ -377,6 +377,6 @@ bool prb_read_valid_info(struct printk_ringbuffer
*rb, u64 seq,
struct printk_info *info, unsigned int
*line_count);
u64 prb_first_valid_seq(struct printk_ringbuffer *rb);
-u64 prb_next_seq(struct printk_ringbuffer *rb);
+u64 prb_next_seq(struct printk_ringbuffer *rb, u64 seq);
#endif /* _KERNEL_PRINTK_RINGBUFFER_H */
--
2.18.0
More information about the Linux-mediatek
mailing list