[PATCH 2/2] printk: Support message continuation from /dev/kmsg

From: James Byrne
Date: Tue Nov 19 2019 - 12:09:00 EST

Since commit 4bcc595ccd80 ("printk: reinstate KERN_CONT for printing
continuation lines") the behaviour of messages written into /dev/kmsg
from user space has changed. Previously if you wrote a message that
did not end with a newline, followed by one ending with a newline, the
second message was treated as a continuation of the first. This is no
longer the case since for a message to be treated as a continuation, an
explicit KERN_CONT is required at the start, and this cannot be used in
messages written via /dev/kmsg.

This commit allows bit 11 of the facility/level number to be used to set
the continuation flag, so you can write two messages that you want to be
joined into /dev/kmsg like this:

<13>This is a continu
<2061>ation message.\n

Signed-off-by: James Byrne <james.byrne@xxxxxxxxxxxxxxxxx>

Documentation/ABI/testing/dev-kmsg | 6 +++++-
kernel/printk/printk.c | 6 ++++++
2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/Documentation/ABI/testing/dev-kmsg b/Documentation/ABI/testing/dev-kmsg
index 6326deeaf5e3..793cf22595fe 100644
--- a/Documentation/ABI/testing/dev-kmsg
+++ b/Documentation/ABI/testing/dev-kmsg
@@ -12,7 +12,8 @@ Description: The /dev/kmsg character device node provides userspace access
The logged line can be prefixed with a <N> syslog prefix, which
carries the syslog priority and facility. The single decimal
prefix number is composed of the 3 lowest bits being the syslog
- priority and the next 8 bits the syslog facility number.
+ priority, the next 8 bits the syslog facility number and the
+ next bit a continuation flag.

If no prefix is given, the priority number is the default kernel
log priority and the facility number is set to LOG_USER (1). It
@@ -20,6 +21,9 @@ Description: The /dev/kmsg character device node provides userspace access
facility number LOG_KERN (0), to make sure that the origin of
the messages can always be reliably determined.

+ Setting bit 11 of the prefix number, the continuation flag, is
+ equivalent to prefixing a kernel printk message with KERN_CONT.
Accessing the buffer:
Every read() from the opened device node receives one record
of the kernel's printk buffer.
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index a3db7f5e56d9..d04353076e92 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -451,6 +451,7 @@ static u32 clear_idx;

#define LOG_LEVEL(v) ((v) & 0x07)
#define LOG_FACILITY(v) ((v) >> 3 & 0xff)
+#define LOG_CONT_USER(v) ((v) & 0x800)

/* record buffer */
#define LOG_ALIGN __alignof__(struct printk_log)
@@ -869,6 +870,8 @@ static ssize_t devkmsg_write(struct kiocb *iocb, struct iov_iter *from)
level = LOG_LEVEL(u);
if (LOG_FACILITY(u) != 0)
facility = LOG_FACILITY(u);
+ if (LOG_CONT_USER(u) != 0)
+ facility |= 0x100;
len -= endp - line;
line = endp;
@@ -1954,6 +1957,9 @@ int vprintk_store(int facility, int level,
text_len -= 2;
text += 2;
+ } else if (facility & 0x100) {
+ lflags |= LOG_CONT;
+ facility &= 0xff;

if (level == LOGLEVEL_DEFAULT)