nmi.c 7.1 KB

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