nmi.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * nmi.c - Safe printk in NMI context
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/preempt.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/debug_locks.h>
  20. #include <linux/smp.h>
  21. #include <linux/cpumask.h>
  22. #include <linux/irq_work.h>
  23. #include <linux/printk.h>
  24. #include "internal.h"
  25. /*
  26. * printk() could not take logbuf_lock in NMI context. Instead,
  27. * it uses an alternative implementation that temporary stores
  28. * the strings into a per-CPU buffer. The content of the buffer
  29. * is later flushed into the main ring buffer via IRQ work.
  30. *
  31. * The alternative implementation is chosen transparently
  32. * via @printk_func per-CPU variable.
  33. *
  34. * The implementation allows to flush the strings also from another CPU.
  35. * There are situations when we want to make sure that all buffers
  36. * were handled or when IRQs are blocked.
  37. */
  38. DEFINE_PER_CPU(printk_func_t, printk_func) = vprintk_default;
  39. static int printk_nmi_irq_ready;
  40. atomic_t nmi_message_lost;
  41. #define NMI_LOG_BUF_LEN ((1 << CONFIG_NMI_LOG_BUF_SHIFT) - \
  42. sizeof(atomic_t) - sizeof(struct irq_work))
  43. struct nmi_seq_buf {
  44. atomic_t len; /* length of written data */
  45. struct irq_work work; /* IRQ work that flushes the buffer */
  46. unsigned char buffer[NMI_LOG_BUF_LEN];
  47. };
  48. static DEFINE_PER_CPU(struct nmi_seq_buf, nmi_print_seq);
  49. /*
  50. * Safe printk() for NMI context. It uses a per-CPU buffer to
  51. * store the message. NMIs are not nested, so there is always only
  52. * one writer running. But the buffer might get flushed from another
  53. * CPU, so we need to be careful.
  54. */
  55. static int vprintk_nmi(const char *fmt, va_list args)
  56. {
  57. struct nmi_seq_buf *s = this_cpu_ptr(&nmi_print_seq);
  58. int add = 0;
  59. size_t len;
  60. again:
  61. len = atomic_read(&s->len);
  62. /* The trailing '\0' is not counted into len. */
  63. if (len >= sizeof(s->buffer) - 1) {
  64. atomic_inc(&nmi_message_lost);
  65. return 0;
  66. }
  67. /*
  68. * Make sure that all old data have been read before the buffer was
  69. * reseted. This is not needed when we just append data.
  70. */
  71. if (!len)
  72. smp_rmb();
  73. add = vscnprintf(s->buffer + len, sizeof(s->buffer) - len, fmt, args);
  74. /*
  75. * Do it once again if the buffer has been flushed in the meantime.
  76. * Note that atomic_cmpxchg() is an implicit memory barrier that
  77. * makes sure that the data were written before updating s->len.
  78. */
  79. if (atomic_cmpxchg(&s->len, len, len + add) != len)
  80. goto again;
  81. /* Get flushed in a more safe context. */
  82. if (add && printk_nmi_irq_ready) {
  83. /* Make sure that IRQ work is really initialized. */
  84. smp_rmb();
  85. irq_work_queue(&s->work);
  86. }
  87. return add;
  88. }
  89. static void printk_nmi_flush_line(const char *text, int len)
  90. {
  91. /*
  92. * The buffers are flushed in NMI only on panic. The messages must
  93. * go only into the ring buffer at this stage. Consoles will get
  94. * explicitly called later when a crashdump is not generated.
  95. */
  96. if (in_nmi())
  97. printk_deferred("%.*s", len, text);
  98. else
  99. printk("%.*s", len, text);
  100. }
  101. /* printk part of the temporary buffer line by line */
  102. static int printk_nmi_flush_buffer(const char *start, size_t len)
  103. {
  104. const char *c, *end;
  105. bool header;
  106. c = start;
  107. end = start + len;
  108. header = true;
  109. /* Print line by line. */
  110. while (c < end) {
  111. if (*c == '\n') {
  112. printk_nmi_flush_line(start, c - start + 1);
  113. start = ++c;
  114. header = true;
  115. continue;
  116. }
  117. /* Handle continuous lines or missing new line. */
  118. if ((c + 1 < end) && printk_get_level(c)) {
  119. if (header) {
  120. c = printk_skip_level(c);
  121. continue;
  122. }
  123. printk_nmi_flush_line(start, c - start);
  124. start = c++;
  125. header = true;
  126. continue;
  127. }
  128. header = false;
  129. c++;
  130. }
  131. /* Check if there was a partial line. Ignore pure header. */
  132. if (start < end && !header) {
  133. static const char newline[] = KERN_CONT "\n";
  134. printk_nmi_flush_line(start, end - start);
  135. printk_nmi_flush_line(newline, strlen(newline));
  136. }
  137. return len;
  138. }
  139. /*
  140. * Flush data from the associated per_CPU buffer. The function
  141. * can be called either via IRQ work or independently.
  142. */
  143. static void __printk_nmi_flush(struct irq_work *work)
  144. {
  145. static raw_spinlock_t read_lock =
  146. __RAW_SPIN_LOCK_INITIALIZER(read_lock);
  147. struct nmi_seq_buf *s = container_of(work, struct nmi_seq_buf, work);
  148. unsigned long flags;
  149. size_t len;
  150. int i;
  151. /*
  152. * The lock has two functions. First, one reader has to flush all
  153. * available message to make the lockless synchronization with
  154. * writers easier. Second, we do not want to mix messages from
  155. * different CPUs. This is especially important when printing
  156. * a backtrace.
  157. */
  158. raw_spin_lock_irqsave(&read_lock, flags);
  159. i = 0;
  160. more:
  161. len = atomic_read(&s->len);
  162. /*
  163. * This is just a paranoid check that nobody has manipulated
  164. * the buffer an unexpected way. If we printed something then
  165. * @len must only increase. Also it should never overflow the
  166. * buffer size.
  167. */
  168. if ((i && i >= len) || len > sizeof(s->buffer)) {
  169. const char *msg = "printk_nmi_flush: internal error\n";
  170. printk_nmi_flush_line(msg, strlen(msg));
  171. len = 0;
  172. }
  173. if (!len)
  174. goto out; /* Someone else has already flushed the buffer. */
  175. /* Make sure that data has been written up to the @len */
  176. smp_rmb();
  177. i += printk_nmi_flush_buffer(s->buffer + i, len - i);
  178. /*
  179. * Check that nothing has got added in the meantime and truncate
  180. * the buffer. Note that atomic_cmpxchg() is an implicit memory
  181. * barrier that makes sure that the data were copied before
  182. * updating s->len.
  183. */
  184. if (atomic_cmpxchg(&s->len, len, 0) != len)
  185. goto more;
  186. out:
  187. raw_spin_unlock_irqrestore(&read_lock, flags);
  188. }
  189. /**
  190. * printk_nmi_flush - flush all per-cpu nmi buffers.
  191. *
  192. * The buffers are flushed automatically via IRQ work. This function
  193. * is useful only when someone wants to be sure that all buffers have
  194. * been flushed at some point.
  195. */
  196. void printk_nmi_flush(void)
  197. {
  198. int cpu;
  199. for_each_possible_cpu(cpu)
  200. __printk_nmi_flush(&per_cpu(nmi_print_seq, cpu).work);
  201. }
  202. /**
  203. * printk_nmi_flush_on_panic - flush all per-cpu nmi buffers when the system
  204. * goes down.
  205. *
  206. * Similar to printk_nmi_flush() but it can be called even in NMI context when
  207. * the system goes down. It does the best effort to get NMI messages into
  208. * the main ring buffer.
  209. *
  210. * Note that it could try harder when there is only one CPU online.
  211. */
  212. void printk_nmi_flush_on_panic(void)
  213. {
  214. /*
  215. * Make sure that we could access the main ring buffer.
  216. * Do not risk a double release when more CPUs are up.
  217. */
  218. if (in_nmi() && raw_spin_is_locked(&logbuf_lock)) {
  219. if (num_online_cpus() > 1)
  220. return;
  221. debug_locks_off();
  222. raw_spin_lock_init(&logbuf_lock);
  223. }
  224. printk_nmi_flush();
  225. }
  226. void __init printk_nmi_init(void)
  227. {
  228. int cpu;
  229. for_each_possible_cpu(cpu) {
  230. struct nmi_seq_buf *s = &per_cpu(nmi_print_seq, cpu);
  231. init_irq_work(&s->work, __printk_nmi_flush);
  232. }
  233. /* Make sure that IRQ works are initialized before enabling. */
  234. smp_wmb();
  235. printk_nmi_irq_ready = 1;
  236. /* Flush pending messages that did not have scheduled IRQ works. */
  237. printk_nmi_flush();
  238. }
  239. void printk_nmi_enter(void)
  240. {
  241. this_cpu_write(printk_func, vprintk_nmi);
  242. }
  243. void printk_nmi_exit(void)
  244. {
  245. this_cpu_write(printk_func, vprintk_default);
  246. }