|
17 | 17 |
|
18 | 18 | #include <linux/preempt.h> |
19 | 19 | #include <linux/spinlock.h> |
| 20 | +#include <linux/debug_locks.h> |
20 | 21 | #include <linux/smp.h> |
21 | 22 | #include <linux/cpumask.h> |
22 | 23 | #include <linux/irq_work.h> |
@@ -106,7 +107,16 @@ static void print_nmi_seq_line(struct nmi_seq_buf *s, int start, int end) |
106 | 107 | { |
107 | 108 | const char *buf = s->buffer + start; |
108 | 109 |
|
109 | | - printk("%.*s", (end - start) + 1, buf); |
| 110 | + /* |
| 111 | + * The buffers are flushed in NMI only on panic. The messages must |
| 112 | + * go only into the ring buffer at this stage. Consoles will get |
| 113 | + * explicitly called later when a crashdump is not generated. |
| 114 | + */ |
| 115 | + if (in_nmi()) |
| 116 | + printk_deferred("%.*s", (end - start) + 1, buf); |
| 117 | + else |
| 118 | + printk("%.*s", (end - start) + 1, buf); |
| 119 | + |
110 | 120 | } |
111 | 121 |
|
112 | 122 | /* |
@@ -194,6 +204,33 @@ void printk_nmi_flush(void) |
194 | 204 | __printk_nmi_flush(&per_cpu(nmi_print_seq, cpu).work); |
195 | 205 | } |
196 | 206 |
|
| 207 | +/** |
| 208 | + * printk_nmi_flush_on_panic - flush all per-cpu nmi buffers when the system |
| 209 | + * goes down. |
| 210 | + * |
| 211 | + * Similar to printk_nmi_flush() but it can be called even in NMI context when |
| 212 | + * the system goes down. It does the best effort to get NMI messages into |
| 213 | + * the main ring buffer. |
| 214 | + * |
| 215 | + * Note that it could try harder when there is only one CPU online. |
| 216 | + */ |
| 217 | +void printk_nmi_flush_on_panic(void) |
| 218 | +{ |
| 219 | + /* |
| 220 | + * Make sure that we could access the main ring buffer. |
| 221 | + * Do not risk a double release when more CPUs are up. |
| 222 | + */ |
| 223 | + if (in_nmi() && raw_spin_is_locked(&logbuf_lock)) { |
| 224 | + if (num_online_cpus() > 1) |
| 225 | + return; |
| 226 | + |
| 227 | + debug_locks_off(); |
| 228 | + raw_spin_lock_init(&logbuf_lock); |
| 229 | + } |
| 230 | + |
| 231 | + printk_nmi_flush(); |
| 232 | +} |
| 233 | + |
197 | 234 | void __init printk_nmi_init(void) |
198 | 235 | { |
199 | 236 | int cpu; |
|
0 commit comments