nmi.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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(int level, 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. if (level != LOGLEVEL_DEFAULT) {
  73. add = snprintf(s->buffer + len, sizeof(s->buffer) - len,
  74. KERN_SOH "%c", '0' + level);
  75. add += vsnprintf(s->buffer + len + add,
  76. sizeof(s->buffer) - len - add,
  77. fmt, args);
  78. } else {
  79. add = vsnprintf(s->buffer + len, sizeof(s->buffer) - len,
  80. fmt, args);
  81. }
  82. /*
  83. * Do it once again if the buffer has been flushed in the meantime.
  84. * Note that atomic_cmpxchg() is an implicit memory barrier that
  85. * makes sure that the data were written before updating s->len.
  86. */
  87. if (atomic_cmpxchg(&s->len, len, len + add) != len)
  88. goto again;
  89. /* Get flushed in a more safe context. */
  90. if (add && printk_nmi_irq_ready) {
  91. /* Make sure that IRQ work is really initialized. */
  92. smp_rmb();
  93. irq_work_queue(&s->work);
  94. }
  95. return add;
  96. }
  97. /*
  98. * printk one line from the temporary buffer from @start index until
  99. * and including the @end index.
  100. */
  101. static void print_nmi_seq_line(struct nmi_seq_buf *s, int start, int end)
  102. {
  103. const char *buf = s->buffer + start;
  104. /*
  105. * The buffers are flushed in NMI only on panic. The messages must
  106. * go only into the ring buffer at this stage. Consoles will get
  107. * explicitly called later when a crashdump is not generated.
  108. */
  109. if (in_nmi())
  110. printk_deferred("%.*s", (end - start) + 1, buf);
  111. else
  112. printk("%.*s", (end - start) + 1, buf);
  113. }
  114. /*
  115. * Flush data from the associated per_CPU buffer. The function
  116. * can be called either via IRQ work or independently.
  117. */
  118. static void __printk_nmi_flush(struct irq_work *work)
  119. {
  120. static raw_spinlock_t read_lock =
  121. __RAW_SPIN_LOCK_INITIALIZER(read_lock);
  122. struct nmi_seq_buf *s = container_of(work, struct nmi_seq_buf, work);
  123. unsigned long flags;
  124. size_t len, size;
  125. int i, last_i;
  126. /*
  127. * The lock has two functions. First, one reader has to flush all
  128. * available message to make the lockless synchronization with
  129. * writers easier. Second, we do not want to mix messages from
  130. * different CPUs. This is especially important when printing
  131. * a backtrace.
  132. */
  133. raw_spin_lock_irqsave(&read_lock, flags);
  134. i = 0;
  135. more:
  136. len = atomic_read(&s->len);
  137. /*
  138. * This is just a paranoid check that nobody has manipulated
  139. * the buffer an unexpected way. If we printed something then
  140. * @len must only increase.
  141. */
  142. if (i && i >= len)
  143. pr_err("printk_nmi_flush: internal error: i=%d >= len=%zu\n",
  144. i, len);
  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. print_nmi_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. print_nmi_seq_line(s, last_i, size - 1);
  161. pr_cont("\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. }