Skip to content

Commit daaab5b

Browse files
jognesspmladek
authored andcommitted
printk: introduce struct printk_buffers
Introduce a new struct printk_buffers to contain all the buffers needed to read and format a printk message for output. Putting the buffers inside a struct reduces the number of buffer pointers that need to be tracked. Also, it allows usage of the sizeof() macro for the buffer sizes, rather than expecting certain sized buffers being passed in. Note that since the output buffer for normal consoles is now CONSOLE_EXT_LOG_MAX instead of CONSOLE_LOG_MAX, multi-line messages that may have been previously truncated will now be printed in full. This should be considered a feature and not a bug since the CONSOLE_LOG_MAX restriction was about limiting static buffer usage rather than limiting printed text. Signed-off-by: John Ogness <[email protected]> Reviewed-by: Petr Mladek <[email protected]> Signed-off-by: Petr Mladek <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 02b2396 commit daaab5b

File tree

2 files changed

+36
-31
lines changed

2 files changed

+36
-31
lines changed

kernel/printk/internal.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,13 @@ u16 printk_parse_prefix(const char *text, int *level,
8282

8383
static inline bool printk_percpu_data_ready(void) { return false; }
8484
#endif /* CONFIG_PRINTK */
85+
86+
/**
87+
* struct printk_buffers - Buffers to read/format/output printk messages.
88+
* @outbuf: After formatting, contains text to output.
89+
* @scratchbuf: Used as temporary ringbuffer reading and string-print space.
90+
*/
91+
struct printk_buffers {
92+
char outbuf[CONSOLE_EXT_LOG_MAX];
93+
char scratchbuf[LOG_LINE_MAX];
94+
};

kernel/printk/printk.c

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2728,14 +2728,6 @@ static void __console_unlock(void)
27282728
* Print one record for the given console. The record printed is whatever
27292729
* record is the next available record for the given console.
27302730
*
2731-
* @text is a buffer of size CONSOLE_LOG_MAX.
2732-
*
2733-
* If extended messages should be printed, @ext_text is a buffer of size
2734-
* CONSOLE_EXT_LOG_MAX. Otherwise @ext_text must be NULL.
2735-
*
2736-
* If dropped messages should be printed, @dropped_text is a buffer of size
2737-
* DROPPED_TEXT_MAX. Otherwise @dropped_text must be NULL.
2738-
*
27392731
* @handover will be set to true if a printk waiter has taken over the
27402732
* console_lock, in which case the caller is no longer holding both the
27412733
* console_lock and the SRCU read lock. Otherwise it is set to false.
@@ -2747,17 +2739,33 @@ static void __console_unlock(void)
27472739
*
27482740
* Requires the console_lock and the SRCU read lock.
27492741
*/
2750-
static bool console_emit_next_record(struct console *con, char *text, char *ext_text,
2751-
char *dropped_text, bool *handover, int cookie)
2742+
static bool console_emit_next_record(struct console *con, bool *handover, int cookie)
27522743
{
2744+
static char dropped_text[DROPPED_TEXT_MAX];
2745+
static struct printk_buffers pbufs;
27532746
static int panic_console_dropped;
2747+
2748+
bool is_extended = console_srcu_read_flags(con) & CON_EXTENDED;
2749+
const size_t scratchbuf_sz = sizeof(pbufs.scratchbuf);
2750+
const size_t outbuf_sz = sizeof(pbufs.outbuf);
2751+
char *scratchbuf = &pbufs.scratchbuf[0];
2752+
char *outbuf = &pbufs.outbuf[0];
27542753
struct printk_info info;
27552754
struct printk_record r;
27562755
unsigned long flags;
2757-
char *write_text;
27582756
size_t len;
27592757

2760-
prb_rec_init_rd(&r, &info, text, CONSOLE_LOG_MAX);
2758+
/*
2759+
* Formatting extended messages requires a separate buffer, so use the
2760+
* scratch buffer to read in the ringbuffer text.
2761+
*
2762+
* Formatting normal messages is done in-place, so read the ringbuffer
2763+
* text directly into the output buffer.
2764+
*/
2765+
if (is_extended)
2766+
prb_rec_init_rd(&r, &info, scratchbuf, scratchbuf_sz);
2767+
else
2768+
prb_rec_init_rd(&r, &info, outbuf, outbuf_sz);
27612769

27622770
*handover = false;
27632771

@@ -2779,13 +2787,11 @@ static bool console_emit_next_record(struct console *con, char *text, char *ext_
27792787
goto skip;
27802788
}
27812789

2782-
if (ext_text) {
2783-
write_text = ext_text;
2784-
len = info_print_ext_header(ext_text, CONSOLE_EXT_LOG_MAX, r.info);
2785-
len += msg_print_ext_body(ext_text + len, CONSOLE_EXT_LOG_MAX - len,
2790+
if (is_extended) {
2791+
len = info_print_ext_header(outbuf, outbuf_sz, r.info);
2792+
len += msg_print_ext_body(outbuf + len, outbuf_sz - len,
27862793
&r.text_buf[0], r.info->text_len, &r.info->dev_info);
27872794
} else {
2788-
write_text = text;
27892795
len = record_print_text(&r, console_msg_format & MSG_FORMAT_SYSLOG, printk_time);
27902796
}
27912797

@@ -2803,7 +2809,8 @@ static bool console_emit_next_record(struct console *con, char *text, char *ext_
28032809
console_lock_spinning_enable();
28042810

28052811
stop_critical_timings(); /* don't trace print latency */
2806-
call_console_driver(con, write_text, len, dropped_text);
2812+
call_console_driver(con, outbuf, len,
2813+
is_extended ? NULL : dropped_text);
28072814
start_critical_timings();
28082815

28092816
con->seq++;
@@ -2839,9 +2846,6 @@ static bool console_emit_next_record(struct console *con, char *text, char *ext_
28392846
*/
28402847
static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handover)
28412848
{
2842-
static char dropped_text[DROPPED_TEXT_MAX];
2843-
static char ext_text[CONSOLE_EXT_LOG_MAX];
2844-
static char text[CONSOLE_LOG_MAX];
28452849
bool any_usable = false;
28462850
struct console *con;
28472851
bool any_progress;
@@ -2861,16 +2865,7 @@ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handove
28612865
continue;
28622866
any_usable = true;
28632867

2864-
if (console_srcu_read_flags(con) & CON_EXTENDED) {
2865-
/* Extended consoles do not print "dropped messages". */
2866-
progress = console_emit_next_record(con, &text[0],
2867-
&ext_text[0], NULL,
2868-
handover, cookie);
2869-
} else {
2870-
progress = console_emit_next_record(con, &text[0],
2871-
NULL, &dropped_text[0],
2872-
handover, cookie);
2873-
}
2868+
progress = console_emit_next_record(con, handover, cookie);
28742869

28752870
/*
28762871
* If a handover has occurred, the SRCU read lock

0 commit comments

Comments
 (0)