Skip to contentSkip to conversation

printk: ringbuffer: fix line counting

PATCH In review John Ogness 3 participants 5 messages Jan 13, 2021 – Jan 15, 2021 Printk

lore
Share & tools
lore

John Ogness

author maintainer
#0 lore raw

PATCH 1/1 printk: ringbuffer: fix line counting

Counting text lines in a record simply involves counting the number of newline characters (+1). However, it is searching the full data block for newline characters, even though the text data can be (and often is) a subset of that area. Since the extra area in the data block was never initialized, the result is that extra newlines may be seen and counted.

Restrict newline searching to the text data length.

Fixes b6cf8b3f3312 ("printk: add lockless ringbuffer")
---
 kernel/printk/printk_ringbuffer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/printk/printk_ringbuffer.c +1 -1
index 6704f06e0417..8a7b7362c0dd 100644
@@ -1718,7 +1718,7 @@ static bool copy_data(struct prb_data_ring *data_ring,
 
 	/* Caller interested in the line count? */
 	if (line_count)
-		*line_count = count_lines(data, data_size);
+		*line_count = count_lines(data, len);
 
 	/* Caller interested in the data content? */
 	if (!buf || !buf_size)
Signature · 1 line
2.20.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · John Ogness

Counting text lines in a record simply involves counting the number of newline characters (+1). However, it is searching the full data block for newline characters, even though the text data can be (and often is) a subset of that area. Since the extra area in the data block was never initialized, the result is that extra newlines may be seen and counted.

Great catch!

Quoted 4 lines

Restrict newline searching to the text data length.

Fixes b6cf8b3f3312 ("printk: add lockless ringbuffer")

There is a note below.

Quoted 9 lines
---
 kernel/printk/printk_ringbuffer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/printk/printk_ringbuffer.c +0 -0
index 6704f06e0417..8a7b7362c0dd 100644

kernel/printk/printk_ringbuffer.c static bool copy_data(struct prb_data_ring *data_ring, · line 1724

Quoted 8 lines
kernel/printk/printk_ringbuffer.c +1 -1
@@ -1718,7 +1718,7 @@ static bool copy_data(struct prb_data_ring *data_ring,
 
 	/* Caller interested in the line count? */
 	if (line_count)
-		*line_count = count_lines(data, data_size);
+		*line_count = count_lines(data, len);
 
 	/* Caller interested in the data content? */
 	if (!buf || !buf_size)

Another question is what line count should be returned when the data are copied into the buffer. In this case, the text might get shrunken even more.

Well, this case is not supported by the API at the moment. @line_count is defined only in prb_read_valid_info() where the buffer is always NULL.

But we might add a WARN_ONCE() or a comment there to prevent similar mistakes in the future.

Best Regards,
Petr

author maintainer
#2 lore raw
Quoted 9 lines · Petr Mladek
Quoted 9 lines
kernel/printk/printk_ringbuffer.c +1 -1
@@ -1718,7 +1718,7 @@ static bool copy_data(struct prb_data_ring *data_ring,
 
 	/* Caller interested in the line count? */
 	if (line_count)
-		*line_count = count_lines(data, data_size);
+		*line_count = count_lines(data, len);
 
Quoted 6 lines

kernel/printk/printk_ringbuffer.c static bool copy_data(struct prb_data_ring *data_ring, · line 1724

Quoted 2 lines
kernel/printk/printk_ringbuffer.c +0 -0
 	/* Caller interested in the data content? */
 	if (!buf || !buf_size)

Another question is what line count should be returned when the data are copied into the buffer. In this case, the text might get shrunken even more.

Good point. The code could look like this:

        if (!buf || !buf_size) {
                data_size = len;
        } else {
                data_size = min_t(u16, buf_size, len);
                memcpy(&buf[0], data, data_size);
        }

        if (line_count)
                *line_count = count_lines(data, data_size);
                
        return true;

John Ogness

Acked-by on v1 1/1
#3 lore raw
In reply to John Ogness
Quoted 8 lines · John Ogness

Counting text lines in a record simply involves counting the number of newline characters (+1). However, it is searching the full data block for newline characters, even though the text data can be (and often is) a subset of that area. Since the extra area in the data block was never initialized, the result is that extra newlines may be seen and counted.

Quoted 4 lines

Restrict newline searching to the text data length.

Fixes b6cf8b3f3312 ("printk: add lockless ringbuffer")
	-ss
maintainer
#4 lore raw
In reply to John Ogness
Quoted 11 lines · John Ogness

Counting text lines in a record simply involves counting the number of newline characters (+1). However, it is searching the full data block for newline characters, even though the text data can be (and often is) a subset of that area. Since the extra area in the data block was never initialized, the result is that extra newlines may be seen and counted.

Restrict newline searching to the text data length.

Fixes b6cf8b3f3312 ("printk: add lockless ringbuffer")

The patch is committed in printk/linux.git, branch printk-rework.

I plan to send it for-5.11 the following week after it spends few days in linux-next.

Best Regards,
Petr