^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * printk_safe.c - Safe printk for printk-deadlock-prone contexts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/preempt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/debug_locks.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/kdb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/smp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/cpumask.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/irq_work.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/printk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/kprobes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * printk() could not take logbuf_lock in NMI context. Instead,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * it uses an alternative implementation that temporary stores
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * the strings into a per-CPU buffer. The content of the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * is later flushed into the main ring buffer via IRQ work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * The alternative implementation is chosen transparently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * by examining current printk() context mask stored in @printk_context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * per-CPU variable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * The implementation allows to flush the strings also from another CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * There are situations when we want to make sure that all buffers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * were handled or when IRQs are blocked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define SAFE_LOG_BUF_LEN ((1 << CONFIG_PRINTK_SAFE_LOG_BUF_SHIFT) - \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) sizeof(atomic_t) - \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) sizeof(atomic_t) - \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) sizeof(struct irq_work))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct printk_safe_seq_buf {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) atomic_t len; /* length of written data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) atomic_t message_lost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct irq_work work; /* IRQ work that flushes the buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) unsigned char buffer[SAFE_LOG_BUF_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static DEFINE_PER_CPU(struct printk_safe_seq_buf, safe_print_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static DEFINE_PER_CPU(int, printk_context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static DEFINE_RAW_SPINLOCK(safe_read_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #ifdef CONFIG_PRINTK_NMI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static DEFINE_PER_CPU(struct printk_safe_seq_buf, nmi_print_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /* Get flushed in a more safe context. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static void queue_flush_work(struct printk_safe_seq_buf *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (printk_percpu_data_ready())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) irq_work_queue(&s->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * Add a message to per-CPU context-dependent buffer. NMI and printk-safe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * have dedicated buffers, because otherwise printk-safe preempted by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * NMI-printk would have overwritten the NMI messages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * The messages are flushed from irq work (or from panic()), possibly,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * from other CPU, concurrently with printk_safe_log_store(). Should this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * happen, printk_safe_log_store() will notice the buffer->len mismatch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * and repeat the write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static __printf(2, 0) int printk_safe_log_store(struct printk_safe_seq_buf *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) const char *fmt, va_list args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) int add;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) va_list ap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) len = atomic_read(&s->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /* The trailing '\0' is not counted into len. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (len >= sizeof(s->buffer) - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) atomic_inc(&s->message_lost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) queue_flush_work(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * Make sure that all old data have been read before the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * was reset. This is not needed when we just append data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) va_copy(ap, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) add = vscnprintf(s->buffer + len, sizeof(s->buffer) - len, fmt, ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) va_end(ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (!add)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * Do it once again if the buffer has been flushed in the meantime.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * Note that atomic_cmpxchg() is an implicit memory barrier that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * makes sure that the data were written before updating s->len.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (atomic_cmpxchg(&s->len, len, len + add) != len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) queue_flush_work(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return add;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static inline void printk_safe_flush_line(const char *text, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * Avoid any console drivers calls from here, because we may be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * in NMI or printk_safe context (when in panic). The messages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * must go only into the ring buffer at this stage. Consoles will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * get explicitly called later when a crashdump is not generated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) printk_deferred("%.*s", len, text);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /* printk part of the temporary buffer line by line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static int printk_safe_flush_buffer(const char *start, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) const char *c, *end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) bool header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) c = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) end = start + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) header = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) /* Print line by line. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) while (c < end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (*c == '\n') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) printk_safe_flush_line(start, c - start + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) start = ++c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) header = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) /* Handle continuous lines or missing new line. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if ((c + 1 < end) && printk_get_level(c)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (header) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) c = printk_skip_level(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) printk_safe_flush_line(start, c - start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) start = c++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) header = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) header = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) c++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) /* Check if there was a partial line. Ignore pure header. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (start < end && !header) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static const char newline[] = KERN_CONT "\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) printk_safe_flush_line(start, end - start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) printk_safe_flush_line(newline, strlen(newline));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static void report_message_lost(struct printk_safe_seq_buf *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) int lost = atomic_xchg(&s->message_lost, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (lost)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) printk_deferred("Lost %d message(s)!\n", lost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * Flush data from the associated per-CPU buffer. The function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * can be called either via IRQ work or independently.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static void __printk_safe_flush(struct irq_work *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct printk_safe_seq_buf *s =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) container_of(work, struct printk_safe_seq_buf, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * The lock has two functions. First, one reader has to flush all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * available message to make the lockless synchronization with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * writers easier. Second, we do not want to mix messages from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * different CPUs. This is especially important when printing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * a backtrace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) raw_spin_lock_irqsave(&safe_read_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) more:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) len = atomic_read(&s->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * This is just a paranoid check that nobody has manipulated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * the buffer an unexpected way. If we printed something then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * @len must only increase. Also it should never overflow the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * buffer size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if ((i && i >= len) || len > sizeof(s->buffer)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) const char *msg = "printk_safe_flush: internal error\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) printk_safe_flush_line(msg, strlen(msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) goto out; /* Someone else has already flushed the buffer. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) /* Make sure that data has been written up to the @len */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) i += printk_safe_flush_buffer(s->buffer + i, len - i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * Check that nothing has got added in the meantime and truncate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * the buffer. Note that atomic_cmpxchg() is an implicit memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * barrier that makes sure that the data were copied before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * updating s->len.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (atomic_cmpxchg(&s->len, len, 0) != len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) goto more;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) report_message_lost(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) raw_spin_unlock_irqrestore(&safe_read_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * printk_safe_flush - flush all per-cpu nmi buffers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * The buffers are flushed automatically via IRQ work. This function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * is useful only when someone wants to be sure that all buffers have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * been flushed at some point.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) void printk_safe_flush(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) #ifdef CONFIG_PRINTK_NMI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) __printk_safe_flush(&per_cpu(nmi_print_seq, cpu).work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) __printk_safe_flush(&per_cpu(safe_print_seq, cpu).work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * printk_safe_flush_on_panic - flush all per-cpu nmi buffers when the system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * goes down.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * Similar to printk_safe_flush() but it can be called even in NMI context when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * the system goes down. It does the best effort to get NMI messages into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * the main ring buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) * Note that it could try harder when there is only one CPU online.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) void printk_safe_flush_on_panic(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * Make sure that we could access the main ring buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * Do not risk a double release when more CPUs are up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) if (raw_spin_is_locked(&logbuf_lock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (num_online_cpus() > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) debug_locks_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) raw_spin_lock_init(&logbuf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (raw_spin_is_locked(&safe_read_lock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (num_online_cpus() > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) debug_locks_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) raw_spin_lock_init(&safe_read_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) printk_safe_flush();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) #ifdef CONFIG_PRINTK_NMI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * Safe printk() for NMI context. It uses a per-CPU buffer to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * store the message. NMIs are not nested, so there is always only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * one writer running. But the buffer might get flushed from another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * CPU, so we need to be careful.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static __printf(1, 0) int vprintk_nmi(const char *fmt, va_list args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) struct printk_safe_seq_buf *s = this_cpu_ptr(&nmi_print_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) return printk_safe_log_store(s, fmt, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) void noinstr printk_nmi_enter(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) this_cpu_add(printk_context, PRINTK_NMI_CONTEXT_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) void noinstr printk_nmi_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) this_cpu_sub(printk_context, PRINTK_NMI_CONTEXT_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * Marks a code that might produce many messages in NMI context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * and the risk of losing them is more critical than eventual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * reordering.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * It has effect only when called in NMI context. Then printk()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * will try to store the messages into the main logbuf directly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * and use the per-CPU buffers only as a fallback when the lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * is not available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) void printk_nmi_direct_enter(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (this_cpu_read(printk_context) & PRINTK_NMI_CONTEXT_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) this_cpu_or(printk_context, PRINTK_NMI_DIRECT_CONTEXT_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) void printk_nmi_direct_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) this_cpu_and(printk_context, ~PRINTK_NMI_DIRECT_CONTEXT_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) static __printf(1, 0) int vprintk_nmi(const char *fmt, va_list args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) #endif /* CONFIG_PRINTK_NMI */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) * Lock-less printk(), to avoid deadlocks should the printk() recurse
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) * into itself. It uses a per-CPU buffer to store the message, just like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * NMI.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) static __printf(1, 0) int vprintk_safe(const char *fmt, va_list args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) struct printk_safe_seq_buf *s = this_cpu_ptr(&safe_print_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) return printk_safe_log_store(s, fmt, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) /* Can be preempted by NMI. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) void __printk_safe_enter(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) this_cpu_inc(printk_context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) /* Can be preempted by NMI. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) void __printk_safe_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) this_cpu_dec(printk_context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) __printf(1, 0) int vprintk_func(const char *fmt, va_list args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) #ifdef CONFIG_KGDB_KDB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) /* Allow to pass printk() to kdb but avoid a recursion. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) if (unlikely(kdb_trap_printk && kdb_printf_cpu < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) return vkdb_printf(KDB_MSGSRC_PRINTK, fmt, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) * Try to use the main logbuf even in NMI. But avoid calling console
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) * drivers that might have their own locks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) if ((this_cpu_read(printk_context) & PRINTK_NMI_DIRECT_CONTEXT_MASK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) raw_spin_trylock(&logbuf_lock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) len = vprintk_store(0, LOGLEVEL_DEFAULT, NULL, fmt, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) raw_spin_unlock(&logbuf_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) defer_console_output();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) /* Use extra buffer in NMI when logbuf_lock is taken or in safe mode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if (this_cpu_read(printk_context) & PRINTK_NMI_CONTEXT_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return vprintk_nmi(fmt, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) /* Use extra buffer to prevent a recursion deadlock in safe mode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (this_cpu_read(printk_context) & PRINTK_SAFE_CONTEXT_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) return vprintk_safe(fmt, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) /* No obstacles. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) return vprintk_default(fmt, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) void __init printk_safe_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) struct printk_safe_seq_buf *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) s = &per_cpu(safe_print_seq, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) init_irq_work(&s->work, __printk_safe_flush);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) #ifdef CONFIG_PRINTK_NMI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) s = &per_cpu(nmi_print_seq, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) init_irq_work(&s->work, __printk_safe_flush);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) /* Flush pending messages that did not have scheduled IRQ works. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) printk_safe_flush();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }