nmi.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. if (len >= sizeof(s->buffer)) {
  63. atomic_inc(&nmi_message_lost);
  64. return 0;
  65. }
  66. /*
  67. * Make sure that all old data have been read before the buffer was
  68. * reseted. This is not needed when we just append data.
  69. */
  70. if (!len)
  71. smp_rmb();
  72. add = vsnprintf(s->buffer + len, sizeof(s->buffer) - len, fmt, args);
  73. /*
  74. * Do it once again if the buffer has been flushed in the meantime.
  75. * Note that atomic_cmpxchg() is an implicit memory barrier that
  76. * makes sure that the data were written before updating s->len.
  77. */
  78. if (atomic_cmpxchg(&s->len, len, len + add) != len)
  79. goto again;
  80. /* Get flushed in a more safe context. */
  81. if (add && printk_nmi_irq_ready) {
  82. /* Make sure that IRQ work is really initialized. */
  83. smp_rmb();
  84. irq_work_queue(&s->work);
  85. }
  86. return add;
  87. }
  88. static void printk_nmi_flush_line(const char *text, int len)
  89. {
  90. /*
  91. * The buffers are flushed in NMI only on panic. The messages must
  92. * go only into the ring buffer at this stage. Consoles will get
  93. * explicitly called later when a crashdump is not generated.
  94. */
  95. if (in_nmi())
  96. printk_deferred("%.*s", len, text);
  97. else
  98. printk("%.*s", len, text);
  99. }
  100. /*
  101. * printk one line from the temporary buffer from @start index until
  102. * and including the @end index.
  103. */
  104. static void printk_nmi_flush_seq_line(struct nmi_seq_buf *s,
  105. int start, int end)
  106. {
  107. const char *buf = s->buffer + start;
  108. printk_nmi_flush_line(buf, (end - start) + 1);
  109. }
  110. /*
  111. * Flush data from the associated per_CPU buffer. The function
  112. * can be called either via IRQ work or independently.
  113. */
  114. static void __printk_nmi_flush(struct irq_work *work)
  115. {
  116. static raw_spinlock_t read_lock =
  117. __RAW_SPIN_LOCK_INITIALIZER(read_lock);
  118. struct nmi_seq_buf *s = container_of(work, struct nmi_seq_buf, work);
  119. unsigned long flags;
  120. size_t len, size;
  121. int i, last_i;
  122. /*
  123. * The lock has two functions. First, one reader has to flush all
  124. * available message to make the lockless synchronization with
  125. * writers easier. Second, we do not want to mix messages from
  126. * different CPUs. This is especially important when printing
  127. * a backtrace.
  128. */
  129. raw_spin_lock_irqsave(&read_lock, flags);
  130. i = 0;
  131. more:
  132. len = atomic_read(&s->len);
  133. /*
  134. * This is just a paranoid check that nobody has manipulated
  135. * the buffer an unexpected way. If we printed something then
  136. * @len must only increase.
  137. */
  138. if (i && i >= len) {
  139. const char *msg = "printk_nmi_flush: internal error\n";
  140. printk_nmi_flush_line(msg, strlen(msg));
  141. }
  142. if (!len)
  143. goto out; /* Someone else has already flushed the buffer. */
  144. /* Make sure that data has been written up to the @len */
  145. smp_rmb();
  146. size = min(len, sizeof(s->buffer));
  147. last_i = i;
  148. /* Print line by line. */
  149. for (; i < size; i++) {
  150. if (s->buffer[i] == '\n') {
  151. printk_nmi_flush_seq_line(s, last_i, i);
  152. last_i = i + 1;
  153. }
  154. }
  155. /* Check if there was a partial line. */
  156. if (last_i < size) {
  157. printk_nmi_flush_seq_line(s, last_i, size - 1);
  158. printk_nmi_flush_line("\n", strlen("\n"));
  159. }
  160. /*
  161. * Check that nothing has got added in the meantime and truncate
  162. * the buffer. Note that atomic_cmpxchg() is an implicit memory
  163. * barrier that makes sure that the data were copied before
  164. * updating s->len.
  165. */
  166. if (atomic_cmpxchg(&s->len, len, 0) != len)
  167. goto more;
  168. out:
  169. raw_spin_unlock_irqrestore(&read_lock, flags);
  170. }
  171. /**
  172. * printk_nmi_flush - flush all per-cpu nmi buffers.
  173. *
  174. * The buffers are flushed automatically via IRQ work. This function
  175. * is useful only when someone wants to be sure that all buffers have
  176. * been flushed at some point.
  177. */
  178. void printk_nmi_flush(void)
  179. {
  180. int cpu;
  181. for_each_possible_cpu(cpu)
  182. __printk_nmi_flush(&per_cpu(nmi_print_seq, cpu).work);
  183. }
  184. /**
  185. * printk_nmi_flush_on_panic - flush all per-cpu nmi buffers when the system
  186. * goes down.
  187. *
  188. * Similar to printk_nmi_flush() but it can be called even in NMI context when
  189. * the system goes down. It does the best effort to get NMI messages into
  190. * the main ring buffer.
  191. *
  192. * Note that it could try harder when there is only one CPU online.
  193. */
  194. void printk_nmi_flush_on_panic(void)
  195. {
  196. /*
  197. * Make sure that we could access the main ring buffer.
  198. * Do not risk a double release when more CPUs are up.
  199. */
  200. if (in_nmi() && raw_spin_is_locked(&logbuf_lock)) {
  201. if (num_online_cpus() > 1)
  202. return;
  203. debug_locks_off();
  204. raw_spin_lock_init(&logbuf_lock);
  205. }
  206. printk_nmi_flush();
  207. }
  208. void __init printk_nmi_init(void)
  209. {
  210. int cpu;
  211. for_each_possible_cpu(cpu) {
  212. struct nmi_seq_buf *s = &per_cpu(nmi_print_seq, cpu);
  213. init_irq_work(&s->work, __printk_nmi_flush);
  214. }
  215. /* Make sure that IRQ works are initialized before enabling. */
  216. smp_wmb();
  217. printk_nmi_irq_ready = 1;
  218. /* Flush pending messages that did not have scheduled IRQ works. */
  219. printk_nmi_flush();
  220. }
  221. void printk_nmi_enter(void)
  222. {
  223. this_cpu_write(printk_func, vprintk_nmi);
  224. }
  225. void printk_nmi_exit(void)
  226. {
  227. this_cpu_write(printk_func, vprintk_default);
  228. }