[PATCH] printk: always use deferred printk when flush printk_safe lines

From: Sergey Senozhatsky
Date: Tue Dec 27 2016 - 09:16:07 EST


Always use printk_deferred() in printk_safe_flush_line().
Flushing can be done from NMI or printk_safe contexts (when
we are in panic), so we can't call console drivers, yet still
want to store the messages in the logbuf buffer. Therefore we
use a deferred printk version.

Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@xxxxxxxxx>
Suggested-by: Petr Mladek <pmladek@xxxxxxxx>
---
kernel/printk/printk_safe.c | 30 ++++++++++++------------------
1 file changed, 12 insertions(+), 18 deletions(-)

diff --git a/kernel/printk/printk_safe.c b/kernel/printk/printk_safe.c
index efc89a4e9df5..801f0f1c7547 100644
--- a/kernel/printk/printk_safe.c
+++ b/kernel/printk/printk_safe.c
@@ -110,19 +110,6 @@ static int printk_safe_log_store(struct printk_safe_seq_buf *s,
return add;
}

-static void printk_safe_flush_line(const char *text, int len)
-{
- /*
- * The buffers are flushed in NMI only on panic. The messages must
- * go only into the ring buffer at this stage. Consoles will get
- * explicitly called later when a crashdump is not generated.
- */
- if (in_nmi())
- printk_deferred("%.*s", len, text);
- else
- printk("%.*s", len, text);
-}
-
/* printk part of the temporary buffer line by line */
static int printk_safe_flush_buffer(const char *start, size_t len)
{
@@ -136,7 +123,14 @@ static int printk_safe_flush_buffer(const char *start, size_t len)
/* Print line by line. */
while (c < end) {
if (*c == '\n') {
- printk_safe_flush_line(start, c - start + 1);
+ /*
+ * Avoid any console drivers calls from here, because
+ * we may be in NMI or printk_safe context (when in
+ * panic). The messages must go only into the ring
+ * buffer at this stage. Consoles will get explicitly
+ * called later when a crashdump is not generated.
+ */
+ printk_deferred("%.*s", c - start + 1, start);
start = ++c;
header = true;
continue;
@@ -149,7 +143,7 @@ static int printk_safe_flush_buffer(const char *start, size_t len)
continue;
}

- printk_safe_flush_line(start, c - start);
+ printk_deferred("%.*s", c - start, start);
start = c++;
header = true;
continue;
@@ -163,8 +157,8 @@ static int printk_safe_flush_buffer(const char *start, size_t len)
if (start < end && !header) {
static const char newline[] = KERN_CONT "\n";

- printk_safe_flush_line(start, end - start);
- printk_safe_flush_line(newline, strlen(newline));
+ printk_deferred("%.*s", end - start, start);
+ printk_deferred("%.*s", strlen(newline), newline);
}

return len;
@@ -206,7 +200,7 @@ static void __printk_safe_flush(struct irq_work *work)
if ((i && i >= len) || len > sizeof(s->buffer)) {
const char *msg = "printk_safe_flush: internal error\n";

- printk_safe_flush_line(msg, strlen(msg));
+ printk_deferred("%.*s", strlen(msg), msg);
len = 0;
}

--
2.11.1