Commit cf775444 authored by Sergey Senozhatsky's avatar Sergey Senozhatsky Committed by Linus Torvalds
Browse files

printk: introduce suppress_message_printing()

Messages' levels and console log level are inspected when the actual
printing occurs, which may provoke console_unlock() and
console_cont_flush() to waste CPU cycles on every message that has
loglevel above the current console_loglevel.

Schematically, console_unlock() does the following:

        for (;;) {
                raw_spin_lock_irqsave(&logbuf_lock, flags);
                msg = log_from_idx(console_idx);

                if (msg->flags & LOG_NOCONS) {
                        goto skip;

                level = msg->level;
                len += msg_print_text();                        >> sprintfs

                if (nr_ext_console_drivers) {
                        ext_len = msg_print_ext_header();       >> scnprintf
                        ext_len += msg_print_ext_body();        >> scnprintfs

                call_console_drivers(level, ext_text, ext_len, text, len)
                        if (level >= console_loglevel &&        >> drop the message



The thing here is this deferred `level >= console_loglevel' check.  We
are wasting CPU cycles on sprintfs/memcpy/etc.  preparing the messages
that we will eventually drop.

This can be huge when we register a new CON_PRINTBUFFER console, for
instance.  For every such a console register_console() resets the

        console_seq, console_idx, console_prev

and sets a `exclusive console' pointer to replay the log buffer to that
just-registered console.  And there can be a lot of messages to replay,
in the worst case most of which can be dropped after console_loglevel

We know messages' levels long before we call msg_print_text() and
friends, so we can just move console_loglevel check out of
call_console_drivers() and format a new message only if we are sure that
it won't be dropped.

The patch factors out loglevel check into suppress_message_printing()
function and tests message->level and console_loglevel before formatting
functions in console_unlock() and console_cont_flush() are getting
executed.  This improves things not only for exclusive CON_PRINTBUFFER
consoles, but for every console_unlock() that attempts to print a
message of level above the console_loglevel.


Signed-off-by: default avatarSergey Senozhatsky <>
Reviewed-by: default avatarPetr Mladek <>
Cc: Tejun Heo <>
Cc: Jan Kara <>
Cc: Calvin Owens <>
Signed-off-by: default avatarAndrew Morton <>
Signed-off-by: default avatarLinus Torvalds <>
parent 874f9c7d
......@@ -985,6 +985,11 @@ module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR);
"ignore loglevel setting (prints all kernel messages to the console)");
static bool suppress_message_printing(int level)
return (level >= console_loglevel && !ignore_loglevel);
static int boot_delay; /* msecs delay after each printk during bootup */
......@@ -1014,7 +1019,7 @@ static void boot_delay_msec(int level)
unsigned long timeout;
if ((boot_delay == 0 || system_state != SYSTEM_BOOTING)
|| (level >= console_loglevel && !ignore_loglevel)) {
|| suppress_message_printing(level)) {
......@@ -1438,8 +1443,6 @@ static void call_console_drivers(int level,
trace_console(text, len);
if (level >= console_loglevel && !ignore_loglevel)
if (!console_drivers)
......@@ -1908,6 +1911,7 @@ static void call_console_drivers(int level,
static size_t msg_print_text(const struct printk_log *msg, enum log_flags prev,
bool syslog, char *buf, size_t size) { return 0; }
static size_t cont_print_text(char *text, size_t size) { return 0; }
static bool suppress_message_printing(int level) { return false; }
/* Still needs to be defined for users */
DEFINE_PER_CPU(printk_func_t, printk_func);
......@@ -2187,6 +2191,13 @@ static void console_cont_flush(char *text, size_t size)
if (!cont.len)
goto out;
if (suppress_message_printing(cont.level)) {
cont.cons = cont.len;
if (cont.flushed)
cont.len = 0;
goto out;
* We still queue earlier records, likely because the console was
* busy. The earlier ones need to be printed before this one, we
......@@ -2290,10 +2301,13 @@ void console_unlock(void)
msg = log_from_idx(console_idx);
if (msg->flags & LOG_NOCONS) {
level = msg->level;
if ((msg->flags & LOG_NOCONS) ||
suppress_message_printing(level)) {
* Skip record we have buffered and already printed
* directly to the console when we received it.
* directly to the console when we received it, and
* record that has level above the console loglevel.
console_idx = log_next(console_idx);
......@@ -2307,7 +2321,6 @@ void console_unlock(void)
goto skip;
level = msg->level;
len += msg_print_text(msg, console_prev, false,
text + len, sizeof(text) - len);
if (nr_ext_console_drivers) {
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment