nmi.c 6.9 KB

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