blob: b4045e7827431a83327589cb5593d2a678fdaa3c [file] [log] [blame]
Thomas Gleixner1ccea772019-05-19 15:51:43 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Petr Mladek42a0bb32016-05-20 17:00:33 -07002/*
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +09003 * printk_safe.c - Safe printk for printk-deadlock-prone contexts
Petr Mladek42a0bb32016-05-20 17:00:33 -07004 */
5
6#include <linux/preempt.h>
7#include <linux/spinlock.h>
Petr Mladekcf9b1102016-05-20 17:00:42 -07008#include <linux/debug_locks.h>
Petr Mladek42a0bb32016-05-20 17:00:33 -07009#include <linux/smp.h>
10#include <linux/cpumask.h>
11#include <linux/irq_work.h>
12#include <linux/printk.h>
13
14#include "internal.h"
15
16/*
17 * printk() could not take logbuf_lock in NMI context. Instead,
18 * it uses an alternative implementation that temporary stores
19 * the strings into a per-CPU buffer. The content of the buffer
20 * is later flushed into the main ring buffer via IRQ work.
21 *
22 * The alternative implementation is chosen transparently
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +090023 * by examinig current printk() context mask stored in @printk_context
24 * per-CPU variable.
Petr Mladek42a0bb32016-05-20 17:00:33 -070025 *
26 * The implementation allows to flush the strings also from another CPU.
27 * There are situations when we want to make sure that all buffers
28 * were handled or when IRQs are blocked.
29 */
Steven Rostedt (VMware)af41acf2017-10-11 12:46:47 -040030static int printk_safe_irq_ready __read_mostly;
Petr Mladek42a0bb32016-05-20 17:00:33 -070031
Sergey Senozhatskyf92bac32016-12-27 23:16:05 +090032#define SAFE_LOG_BUF_LEN ((1 << CONFIG_PRINTK_SAFE_LOG_BUF_SHIFT) - \
Sergey Senozhatskyddb9baa2016-12-27 23:16:08 +090033 sizeof(atomic_t) - \
34 sizeof(atomic_t) - \
35 sizeof(struct irq_work))
Petr Mladek42a0bb32016-05-20 17:00:33 -070036
Sergey Senozhatskyf92bac32016-12-27 23:16:05 +090037struct printk_safe_seq_buf {
Petr Mladek42a0bb32016-05-20 17:00:33 -070038 atomic_t len; /* length of written data */
Sergey Senozhatskyddb9baa2016-12-27 23:16:08 +090039 atomic_t message_lost;
Petr Mladek42a0bb32016-05-20 17:00:33 -070040 struct irq_work work; /* IRQ work that flushes the buffer */
Sergey Senozhatskyf92bac32016-12-27 23:16:05 +090041 unsigned char buffer[SAFE_LOG_BUF_LEN];
Petr Mladek42a0bb32016-05-20 17:00:33 -070042};
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +090043
44static DEFINE_PER_CPU(struct printk_safe_seq_buf, safe_print_seq);
45static DEFINE_PER_CPU(int, printk_context);
46
47#ifdef CONFIG_PRINTK_NMI
Sergey Senozhatskyf92bac32016-12-27 23:16:05 +090048static DEFINE_PER_CPU(struct printk_safe_seq_buf, nmi_print_seq);
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +090049#endif
Petr Mladek42a0bb32016-05-20 17:00:33 -070050
Sergey Senozhatskyddb9baa2016-12-27 23:16:08 +090051/* Get flushed in a more safe context. */
52static void queue_flush_work(struct printk_safe_seq_buf *s)
53{
Steven Rostedt (VMware)af41acf2017-10-11 12:46:47 -040054 if (printk_safe_irq_ready)
Sergey Senozhatskyddb9baa2016-12-27 23:16:08 +090055 irq_work_queue(&s->work);
Sergey Senozhatskyddb9baa2016-12-27 23:16:08 +090056}
57
Petr Mladek42a0bb32016-05-20 17:00:33 -070058/*
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +090059 * Add a message to per-CPU context-dependent buffer. NMI and printk-safe
60 * have dedicated buffers, because otherwise printk-safe preempted by
61 * NMI-printk would have overwritten the NMI messages.
62 *
Baoquan Hebc829362017-10-22 22:30:55 +080063 * The messages are flushed from irq work (or from panic()), possibly,
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +090064 * from other CPU, concurrently with printk_safe_log_store(). Should this
65 * happen, printk_safe_log_store() will notice the buffer->len mismatch
66 * and repeat the write.
Petr Mladek42a0bb32016-05-20 17:00:33 -070067 */
Nicolas Ioossf4e981c2017-05-24 07:49:50 +020068static __printf(2, 0) int printk_safe_log_store(struct printk_safe_seq_buf *s,
69 const char *fmt, va_list args)
Petr Mladek42a0bb32016-05-20 17:00:33 -070070{
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +090071 int add;
Petr Mladek42a0bb32016-05-20 17:00:33 -070072 size_t len;
Tetsuo Handa988a35f2018-05-11 19:54:19 +090073 va_list ap;
Petr Mladek42a0bb32016-05-20 17:00:33 -070074
75again:
76 len = atomic_read(&s->len);
77
Petr Mladek4a998e32016-12-12 16:45:40 -080078 /* The trailing '\0' is not counted into len. */
79 if (len >= sizeof(s->buffer) - 1) {
Sergey Senozhatskyddb9baa2016-12-27 23:16:08 +090080 atomic_inc(&s->message_lost);
81 queue_flush_work(s);
Petr Mladek42a0bb32016-05-20 17:00:33 -070082 return 0;
Petr Mladekb522dea2016-05-20 17:00:36 -070083 }
Petr Mladek42a0bb32016-05-20 17:00:33 -070084
85 /*
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +090086 * Make sure that all old data have been read before the buffer
87 * was reset. This is not needed when we just append data.
Petr Mladek42a0bb32016-05-20 17:00:33 -070088 */
89 if (!len)
90 smp_rmb();
91
Tetsuo Handa988a35f2018-05-11 19:54:19 +090092 va_copy(ap, args);
93 add = vscnprintf(s->buffer + len, sizeof(s->buffer) - len, fmt, ap);
94 va_end(ap);
Sergey Senozhatskyddb9baa2016-12-27 23:16:08 +090095 if (!add)
96 return 0;
Petr Mladek42a0bb32016-05-20 17:00:33 -070097
98 /*
99 * Do it once again if the buffer has been flushed in the meantime.
100 * Note that atomic_cmpxchg() is an implicit memory barrier that
101 * makes sure that the data were written before updating s->len.
102 */
103 if (atomic_cmpxchg(&s->len, len, len + add) != len)
104 goto again;
105
Sergey Senozhatskyddb9baa2016-12-27 23:16:08 +0900106 queue_flush_work(s);
Petr Mladek42a0bb32016-05-20 17:00:33 -0700107 return add;
108}
109
Sergey Senozhatsky7acac342017-02-07 01:42:53 +0900110static inline void printk_safe_flush_line(const char *text, int len)
Petr Mladek42a0bb32016-05-20 17:00:33 -0700111{
Petr Mladekcf9b1102016-05-20 17:00:42 -0700112 /*
Sergey Senozhatsky7acac342017-02-07 01:42:53 +0900113 * Avoid any console drivers calls from here, because we may be
114 * in NMI or printk_safe context (when in panic). The messages
115 * must go only into the ring buffer at this stage. Consoles will
116 * get explicitly called later when a crashdump is not generated.
Petr Mladekcf9b1102016-05-20 17:00:42 -0700117 */
Sergey Senozhatsky7acac342017-02-07 01:42:53 +0900118 printk_deferred("%.*s", len, text);
Petr Mladek42a0bb32016-05-20 17:00:33 -0700119}
120
Petr Mladek22c2c7b22016-12-12 16:45:44 -0800121/* printk part of the temporary buffer line by line */
Sergey Senozhatskyf92bac32016-12-27 23:16:05 +0900122static int printk_safe_flush_buffer(const char *start, size_t len)
Sergey Senozhatsky19feeff2016-09-01 16:15:04 -0700123{
Petr Mladek22c2c7b22016-12-12 16:45:44 -0800124 const char *c, *end;
125 bool header;
Sergey Senozhatsky19feeff2016-09-01 16:15:04 -0700126
Petr Mladek22c2c7b22016-12-12 16:45:44 -0800127 c = start;
128 end = start + len;
129 header = true;
130
131 /* Print line by line. */
132 while (c < end) {
133 if (*c == '\n') {
Sergey Senozhatskyf92bac32016-12-27 23:16:05 +0900134 printk_safe_flush_line(start, c - start + 1);
Petr Mladek22c2c7b22016-12-12 16:45:44 -0800135 start = ++c;
136 header = true;
137 continue;
138 }
139
140 /* Handle continuous lines or missing new line. */
141 if ((c + 1 < end) && printk_get_level(c)) {
142 if (header) {
143 c = printk_skip_level(c);
144 continue;
145 }
146
Sergey Senozhatskyf92bac32016-12-27 23:16:05 +0900147 printk_safe_flush_line(start, c - start);
Petr Mladek22c2c7b22016-12-12 16:45:44 -0800148 start = c++;
149 header = true;
150 continue;
151 }
152
153 header = false;
154 c++;
155 }
156
157 /* Check if there was a partial line. Ignore pure header. */
158 if (start < end && !header) {
159 static const char newline[] = KERN_CONT "\n";
160
Sergey Senozhatskyf92bac32016-12-27 23:16:05 +0900161 printk_safe_flush_line(start, end - start);
162 printk_safe_flush_line(newline, strlen(newline));
Petr Mladek22c2c7b22016-12-12 16:45:44 -0800163 }
164
165 return len;
Sergey Senozhatsky19feeff2016-09-01 16:15:04 -0700166}
167
Sergey Senozhatskyddb9baa2016-12-27 23:16:08 +0900168static void report_message_lost(struct printk_safe_seq_buf *s)
169{
170 int lost = atomic_xchg(&s->message_lost, 0);
171
172 if (lost)
173 printk_deferred("Lost %d message(s)!\n", lost);
174}
175
Sergey Senozhatsky19feeff2016-09-01 16:15:04 -0700176/*
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +0900177 * Flush data from the associated per-CPU buffer. The function
Petr Mladek42a0bb32016-05-20 17:00:33 -0700178 * can be called either via IRQ work or independently.
179 */
Sergey Senozhatskyf92bac32016-12-27 23:16:05 +0900180static void __printk_safe_flush(struct irq_work *work)
Petr Mladek42a0bb32016-05-20 17:00:33 -0700181{
182 static raw_spinlock_t read_lock =
183 __RAW_SPIN_LOCK_INITIALIZER(read_lock);
Sergey Senozhatskyf92bac32016-12-27 23:16:05 +0900184 struct printk_safe_seq_buf *s =
185 container_of(work, struct printk_safe_seq_buf, work);
Petr Mladek42a0bb32016-05-20 17:00:33 -0700186 unsigned long flags;
Petr Mladek22c2c7b22016-12-12 16:45:44 -0800187 size_t len;
188 int i;
Petr Mladek42a0bb32016-05-20 17:00:33 -0700189
190 /*
191 * The lock has two functions. First, one reader has to flush all
192 * available message to make the lockless synchronization with
193 * writers easier. Second, we do not want to mix messages from
194 * different CPUs. This is especially important when printing
195 * a backtrace.
196 */
197 raw_spin_lock_irqsave(&read_lock, flags);
198
199 i = 0;
200more:
201 len = atomic_read(&s->len);
202
203 /*
204 * This is just a paranoid check that nobody has manipulated
205 * the buffer an unexpected way. If we printed something then
Petr Mladek22c2c7b22016-12-12 16:45:44 -0800206 * @len must only increase. Also it should never overflow the
207 * buffer size.
Petr Mladek42a0bb32016-05-20 17:00:33 -0700208 */
Petr Mladek22c2c7b22016-12-12 16:45:44 -0800209 if ((i && i >= len) || len > sizeof(s->buffer)) {
Sergey Senozhatskyf92bac32016-12-27 23:16:05 +0900210 const char *msg = "printk_safe_flush: internal error\n";
Sergey Senozhatsky19feeff2016-09-01 16:15:04 -0700211
Sergey Senozhatskyf92bac32016-12-27 23:16:05 +0900212 printk_safe_flush_line(msg, strlen(msg));
Petr Mladek22c2c7b22016-12-12 16:45:44 -0800213 len = 0;
Sergey Senozhatsky19feeff2016-09-01 16:15:04 -0700214 }
Petr Mladek42a0bb32016-05-20 17:00:33 -0700215
216 if (!len)
217 goto out; /* Someone else has already flushed the buffer. */
218
219 /* Make sure that data has been written up to the @len */
220 smp_rmb();
Sergey Senozhatskyf92bac32016-12-27 23:16:05 +0900221 i += printk_safe_flush_buffer(s->buffer + i, len - i);
Petr Mladek42a0bb32016-05-20 17:00:33 -0700222
223 /*
224 * Check that nothing has got added in the meantime and truncate
225 * the buffer. Note that atomic_cmpxchg() is an implicit memory
226 * barrier that makes sure that the data were copied before
227 * updating s->len.
228 */
229 if (atomic_cmpxchg(&s->len, len, 0) != len)
230 goto more;
231
232out:
Sergey Senozhatskyddb9baa2016-12-27 23:16:08 +0900233 report_message_lost(s);
Petr Mladek42a0bb32016-05-20 17:00:33 -0700234 raw_spin_unlock_irqrestore(&read_lock, flags);
235}
236
237/**
Sergey Senozhatskyf92bac32016-12-27 23:16:05 +0900238 * printk_safe_flush - flush all per-cpu nmi buffers.
Petr Mladek42a0bb32016-05-20 17:00:33 -0700239 *
240 * The buffers are flushed automatically via IRQ work. This function
241 * is useful only when someone wants to be sure that all buffers have
242 * been flushed at some point.
243 */
Sergey Senozhatskyf92bac32016-12-27 23:16:05 +0900244void printk_safe_flush(void)
Petr Mladek42a0bb32016-05-20 17:00:33 -0700245{
246 int cpu;
247
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +0900248 for_each_possible_cpu(cpu) {
249#ifdef CONFIG_PRINTK_NMI
Sergey Senozhatskyf92bac32016-12-27 23:16:05 +0900250 __printk_safe_flush(&per_cpu(nmi_print_seq, cpu).work);
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +0900251#endif
252 __printk_safe_flush(&per_cpu(safe_print_seq, cpu).work);
253 }
Petr Mladek42a0bb32016-05-20 17:00:33 -0700254}
255
Petr Mladekcf9b1102016-05-20 17:00:42 -0700256/**
Sergey Senozhatskyf92bac32016-12-27 23:16:05 +0900257 * printk_safe_flush_on_panic - flush all per-cpu nmi buffers when the system
Petr Mladekcf9b1102016-05-20 17:00:42 -0700258 * goes down.
259 *
Sergey Senozhatskyf92bac32016-12-27 23:16:05 +0900260 * Similar to printk_safe_flush() but it can be called even in NMI context when
Petr Mladekcf9b1102016-05-20 17:00:42 -0700261 * the system goes down. It does the best effort to get NMI messages into
262 * the main ring buffer.
263 *
264 * Note that it could try harder when there is only one CPU online.
265 */
Sergey Senozhatskyf92bac32016-12-27 23:16:05 +0900266void printk_safe_flush_on_panic(void)
Petr Mladekcf9b1102016-05-20 17:00:42 -0700267{
268 /*
269 * Make sure that we could access the main ring buffer.
270 * Do not risk a double release when more CPUs are up.
271 */
Sergey Senozhatsky554755b2018-05-30 16:03:50 +0900272 if (raw_spin_is_locked(&logbuf_lock)) {
Petr Mladekcf9b1102016-05-20 17:00:42 -0700273 if (num_online_cpus() > 1)
274 return;
275
276 debug_locks_off();
277 raw_spin_lock_init(&logbuf_lock);
278 }
279
Sergey Senozhatskyf92bac32016-12-27 23:16:05 +0900280 printk_safe_flush();
Petr Mladekcf9b1102016-05-20 17:00:42 -0700281}
282
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +0900283#ifdef CONFIG_PRINTK_NMI
284/*
285 * Safe printk() for NMI context. It uses a per-CPU buffer to
286 * store the message. NMIs are not nested, so there is always only
287 * one writer running. But the buffer might get flushed from another
288 * CPU, so we need to be careful.
289 */
Nicolas Ioossf4e981c2017-05-24 07:49:50 +0200290static __printf(1, 0) int vprintk_nmi(const char *fmt, va_list args)
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +0900291{
292 struct printk_safe_seq_buf *s = this_cpu_ptr(&nmi_print_seq);
293
294 return printk_safe_log_store(s, fmt, args);
295}
296
Steven Rostedt (VMware)d1c392c2018-09-05 16:29:49 -0400297void notrace printk_nmi_enter(void)
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +0900298{
Petr Mladek03fc7f92018-06-27 16:20:28 +0200299 this_cpu_or(printk_context, PRINTK_NMI_CONTEXT_MASK);
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +0900300}
301
Steven Rostedt (VMware)d1c392c2018-09-05 16:29:49 -0400302void notrace printk_nmi_exit(void)
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +0900303{
Petr Mladek03fc7f92018-06-27 16:20:28 +0200304 this_cpu_and(printk_context, ~PRINTK_NMI_CONTEXT_MASK);
305}
306
307/*
308 * Marks a code that might produce many messages in NMI context
309 * and the risk of losing them is more critical than eventual
310 * reordering.
311 *
312 * It has effect only when called in NMI context. Then printk()
313 * will try to store the messages into the main logbuf directly
314 * and use the per-CPU buffers only as a fallback when the lock
315 * is not available.
316 */
317void printk_nmi_direct_enter(void)
318{
319 if (this_cpu_read(printk_context) & PRINTK_NMI_CONTEXT_MASK)
320 this_cpu_or(printk_context, PRINTK_NMI_DIRECT_CONTEXT_MASK);
321}
322
323void printk_nmi_direct_exit(void)
324{
325 this_cpu_and(printk_context, ~PRINTK_NMI_DIRECT_CONTEXT_MASK);
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +0900326}
327
328#else
329
Nicolas Ioossf4e981c2017-05-24 07:49:50 +0200330static __printf(1, 0) int vprintk_nmi(const char *fmt, va_list args)
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +0900331{
332 return 0;
333}
334
335#endif /* CONFIG_PRINTK_NMI */
336
337/*
338 * Lock-less printk(), to avoid deadlocks should the printk() recurse
339 * into itself. It uses a per-CPU buffer to store the message, just like
340 * NMI.
341 */
Nicolas Ioossf4e981c2017-05-24 07:49:50 +0200342static __printf(1, 0) int vprintk_safe(const char *fmt, va_list args)
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +0900343{
344 struct printk_safe_seq_buf *s = this_cpu_ptr(&safe_print_seq);
345
346 return printk_safe_log_store(s, fmt, args);
347}
348
349/* Can be preempted by NMI. */
350void __printk_safe_enter(void)
351{
352 this_cpu_inc(printk_context);
353}
354
355/* Can be preempted by NMI. */
356void __printk_safe_exit(void)
357{
358 this_cpu_dec(printk_context);
359}
360
361__printf(1, 0) int vprintk_func(const char *fmt, va_list args)
362{
Petr Mladek03fc7f92018-06-27 16:20:28 +0200363 /*
364 * Try to use the main logbuf even in NMI. But avoid calling console
365 * drivers that might have their own locks.
366 */
367 if ((this_cpu_read(printk_context) & PRINTK_NMI_DIRECT_CONTEXT_MASK) &&
368 raw_spin_trylock(&logbuf_lock)) {
369 int len;
370
371 len = vprintk_store(0, LOGLEVEL_DEFAULT, NULL, 0, fmt, args);
372 raw_spin_unlock(&logbuf_lock);
373 defer_console_output();
374 return len;
375 }
376
Petr Mladek719f6a72017-04-20 10:52:31 +0200377 /* Use extra buffer in NMI when logbuf_lock is taken or in safe mode. */
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +0900378 if (this_cpu_read(printk_context) & PRINTK_NMI_CONTEXT_MASK)
379 return vprintk_nmi(fmt, args);
380
Petr Mladek719f6a72017-04-20 10:52:31 +0200381 /* Use extra buffer to prevent a recursion deadlock in safe mode. */
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +0900382 if (this_cpu_read(printk_context) & PRINTK_SAFE_CONTEXT_MASK)
383 return vprintk_safe(fmt, args);
384
Petr Mladek719f6a72017-04-20 10:52:31 +0200385 /* No obstacles. */
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +0900386 return vprintk_default(fmt, args);
387}
388
Sergey Senozhatskyf92bac32016-12-27 23:16:05 +0900389void __init printk_safe_init(void)
Petr Mladek42a0bb32016-05-20 17:00:33 -0700390{
391 int cpu;
392
393 for_each_possible_cpu(cpu) {
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +0900394 struct printk_safe_seq_buf *s;
Petr Mladek42a0bb32016-05-20 17:00:33 -0700395
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +0900396 s = &per_cpu(safe_print_seq, cpu);
Sergey Senozhatskyf92bac32016-12-27 23:16:05 +0900397 init_irq_work(&s->work, __printk_safe_flush);
Sergey Senozhatsky099f1c82016-12-27 23:16:06 +0900398
399#ifdef CONFIG_PRINTK_NMI
400 s = &per_cpu(nmi_print_seq, cpu);
401 init_irq_work(&s->work, __printk_safe_flush);
402#endif
Petr Mladek42a0bb32016-05-20 17:00:33 -0700403 }
404
Steven Rostedt (VMware)af41acf2017-10-11 12:46:47 -0400405 /*
406 * In the highly unlikely event that a NMI were to trigger at
407 * this moment. Make sure IRQ work is set up before this
408 * variable is set.
409 */
410 barrier();
Sergey Senozhatskyf92bac32016-12-27 23:16:05 +0900411 printk_safe_irq_ready = 1;
Petr Mladek42a0bb32016-05-20 17:00:33 -0700412
413 /* Flush pending messages that did not have scheduled IRQ works. */
Sergey Senozhatskyf92bac32016-12-27 23:16:05 +0900414 printk_safe_flush();
Petr Mladek42a0bb32016-05-20 17:00:33 -0700415}