lkml.org 
[lkml]   [2022]   [Dec]   [21]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
From
Subject[PATCH printk v3 5/6] printk: introduce console_get_next_message() and console_message
Date
Code for performing the console output is intermixed with code
that is formatting the output for that console. Introduce a new
helper function console_get_next_message() to handle the reading
and formatting of the console text. The helper does not require
any locking so that in the future it can be used for other console
printing contexts as well.

This also introduces a new struct console_message to wrap the
struct console_buffers adding meta-data about its contents. This
allows users of console_get_next_message() to receive all relevant
information about the message that was read and formatted.

The reason a wrapper struct is introduced instead of adding the
meta-data to struct console_buffers is because the upcoming atomic
consoles will need per-cpu and per-context console_buffers. It
would not make sense to make the meta-data also per-cpu and
per-context as that data can be easily stored on the stack of the
console printing context.

Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
kernel/printk/internal.h | 16 ++++++
kernel/printk/printk.c | 118 +++++++++++++++++++++++++++------------
2 files changed, 97 insertions(+), 37 deletions(-)

diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h
index f5ebbd392fee..4f2eb8c470bc 100644
--- a/kernel/printk/internal.h
+++ b/kernel/printk/internal.h
@@ -91,3 +91,19 @@ struct console_buffers {
char outbuf[CONSOLE_EXT_LOG_MAX];
char scratchbuf[LOG_LINE_MAX];
};
+
+/**
+ * console_message - Container for a prepared console message.
+ * @cbufs: Console buffers used to prepare the message.
+ * @outbuf_len: The length of prepared text in @cbufs->outbuf to output. This
+ * does not count the terminator. A value of 0 means there is
+ * nothing to output and this record should be skipped.
+ * @outbuf_seq: The sequence number of the record used for @cbufs->outbuf.
+ * @dropped: The number of dropped records from reading @outbuf_seq.
+ */
+struct console_message {
+ struct console_buffers *cbufs;
+ unsigned int outbuf_len;
+ u64 outbuf_seq;
+ unsigned long dropped;
+};
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 2e5e2eda1fa1..7cac636600f8 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -2725,34 +2725,34 @@ static void __console_unlock(void)
}

/*
- * Print one record for the given console. The record printed is whatever
- * record is the next available record for the given console.
+ * Read and format the specified record (or a later record if the specified
+ * record is not available).
*
- * @handover will be set to true if a printk waiter has taken over the
- * console_lock, in which case the caller is no longer holding both the
- * console_lock and the SRCU read lock. Otherwise it is set to false.
+ * @cmsg will contain the formatted result. @cmsg->cbufs must point to a
+ * struct console_buffers.
*
- * @cookie is the cookie from the SRCU read lock.
+ * @seq is the record to read and format. If it is not available, the next
+ * valid record is read.
*
- * Returns false if the given console has no next record to print, otherwise
- * true.
+ * @is_extended specifies the message should be formatted for extended
+ * console output.
*
- * Requires the console_lock and the SRCU read lock.
+ * Returns false if no record is available. Otherwise true and all fields
+ * of @cmsg are valid. (See the documentation of struct console_message
+ * for information about the @cmsg fields.)
*/
-static bool console_emit_next_record(struct console *con, bool *handover, int cookie)
-{
- bool is_extended = console_srcu_read_flags(con) & CON_EXTENDED;
- static char dropped_text[DROPPED_TEXT_MAX];
- static struct console_buffers cbufs;
- const size_t scratchbuf_sz = sizeof(cbufs.scratchbuf);
- const size_t outbuf_sz = sizeof(cbufs.outbuf);
- char *scratchbuf = &cbufs.scratchbuf[0];
- char *outbuf = &cbufs.outbuf[0];
+static bool console_get_next_message(struct console_message *cmsg, u64 seq,
+ bool is_extended)
+{
+ struct console_buffers *cbufs = cmsg->cbufs;
+ const size_t scratchbuf_sz = sizeof(cbufs->scratchbuf);
+ const size_t outbuf_sz = sizeof(cbufs->outbuf);
+ char *scratchbuf = &cbufs->scratchbuf[0];
+ char *outbuf = &cbufs->outbuf[0];
static int panic_console_dropped;
struct printk_info info;
struct printk_record r;
- unsigned long flags;
- size_t len;
+ size_t len = 0;

/*
* Formatting extended messages requires a separate buffer, so use the
@@ -2766,33 +2766,77 @@ static bool console_emit_next_record(struct console *con, bool *handover, int co
else
prb_rec_init_rd(&r, &info, outbuf, outbuf_sz);

- *handover = false;
-
- if (!prb_read_valid(prb, con->seq, &r))
+ if (!prb_read_valid(prb, seq, &r))
return false;

- if (con->seq != r.info->seq) {
- con->dropped += r.info->seq - con->seq;
- con->seq = r.info->seq;
- if (panic_in_progress() && panic_console_dropped++ > 10) {
- suppress_panic_printk = 1;
- pr_warn_once("Too many dropped messages. Suppress messages on non-panic CPUs to prevent livelock.\n");
- }
+ cmsg->outbuf_seq = r.info->seq;
+ cmsg->dropped = r.info->seq - seq;
+
+ /*
+ * Check for dropped messages in panic here so that printk
+ * suppression can occur as early as possible if necessary.
+ */
+ if (cmsg->dropped &&
+ panic_in_progress() &&
+ panic_console_dropped++ > 10) {
+ suppress_panic_printk = 1;
+ pr_warn_once("Too many dropped messages. Suppress messages on non-panic CPUs to prevent livelock.\n");
}

/* Skip record that has level above the console loglevel. */
- if (suppress_message_printing(r.info->level)) {
- con->seq++;
- goto skip;
- }
+ if (suppress_message_printing(r.info->level))
+ goto out;

if (is_extended) {
len = info_print_ext_header(outbuf, outbuf_sz, r.info);
len += msg_print_ext_body(outbuf + len, outbuf_sz - len,
- &r.text_buf[0], r.info->text_len, &r.info->dev_info);
+ r.text_buf, r.info->text_len, &r.info->dev_info);
} else {
len = record_print_text(&r, console_msg_format & MSG_FORMAT_SYSLOG, printk_time);
}
+out:
+ cmsg->outbuf_len = len;
+ return true;
+}
+
+/*
+ * Print one record for the given console. The record printed is whatever
+ * record is the next available record for the given console.
+ *
+ * @handover will be set to true if a printk waiter has taken over the
+ * console_lock, in which case the caller is no longer holding both the
+ * console_lock and the SRCU read lock. Otherwise it is set to false.
+ *
+ * @cookie is the cookie from the SRCU read lock.
+ *
+ * Returns false if the given console has no next record to print, otherwise
+ * true.
+ *
+ * Requires the console_lock and the SRCU read lock.
+ */
+static bool console_emit_next_record(struct console *con, bool *handover, int cookie)
+{
+ bool is_extended = console_srcu_read_flags(con) & CON_EXTENDED;
+ static char dropped_text[DROPPED_TEXT_MAX];
+ static struct console_buffers cbufs;
+ static struct console_message cmsg = {
+ .cbufs = &cbufs,
+ };
+ char *outbuf = &cbufs.outbuf[0];
+ unsigned long flags;
+
+ *handover = false;
+
+ if (!console_get_next_message(&cmsg, con->seq, is_extended))
+ return false;
+
+ con->dropped += cmsg.dropped;
+
+ /* Skip messages of formatted length 0. */
+ if (cmsg.outbuf_len == 0) {
+ con->seq = cmsg.outbuf_seq + 1;
+ goto skip;
+ }

/*
* While actively printing out messages, if another printk()
@@ -2808,11 +2852,11 @@ static bool console_emit_next_record(struct console *con, bool *handover, int co
console_lock_spinning_enable();

stop_critical_timings(); /* don't trace print latency */
- call_console_driver(con, outbuf, len,
+ call_console_driver(con, outbuf, cmsg.outbuf_len,
is_extended ? NULL : dropped_text);
start_critical_timings();

- con->seq++;
+ con->seq = cmsg.outbuf_seq + 1;

*handover = console_lock_spinning_disable_and_check(cookie);
printk_safe_exit_irqrestore(flags);
--
2.30.2
\
 
 \ /
  Last update: 2023-03-26 23:17    [W:0.179 / U:0.488 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site