internal.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * internal.h - printk internal definitions
  4. */
  5. #include <linux/console.h>
  6. #include <linux/percpu.h>
  7. #include <linux/types.h>
  8. #if defined(CONFIG_PRINTK) && defined(CONFIG_SYSCTL)
  9. struct ctl_table;
  10. void __init printk_sysctl_init(void);
  11. int devkmsg_sysctl_set_loglvl(const struct ctl_table *table, int write,
  12. void *buffer, size_t *lenp, loff_t *ppos);
  13. #else
  14. #define printk_sysctl_init() do { } while (0)
  15. #endif
  16. #define con_printk(lvl, con, fmt, ...) \
  17. printk(lvl pr_fmt("%s%sconsole [%s%d] " fmt), \
  18. (con->flags & CON_NBCON) ? "" : "legacy ", \
  19. (con->flags & CON_BOOT) ? "boot" : "", \
  20. con->name, con->index, ##__VA_ARGS__)
  21. /*
  22. * Identify if legacy printing is forced in a dedicated kthread. If
  23. * true, all printing via console lock occurs within a dedicated
  24. * legacy printer thread. The only exception is on panic, after the
  25. * nbcon consoles have had their chance to print the panic messages
  26. * first.
  27. */
  28. #ifdef CONFIG_PREEMPT_RT
  29. # define force_legacy_kthread() (true)
  30. #else
  31. # define force_legacy_kthread() (false)
  32. #endif
  33. #ifdef CONFIG_PRINTK
  34. #ifdef CONFIG_PRINTK_CALLER
  35. #define PRINTK_PREFIX_MAX 48
  36. #else
  37. #define PRINTK_PREFIX_MAX 32
  38. #endif
  39. /*
  40. * the maximum size of a formatted record (i.e. with prefix added
  41. * per line and dropped messages or in extended message format)
  42. */
  43. #define PRINTK_MESSAGE_MAX 2048
  44. /* the maximum size allowed to be reserved for a record */
  45. #define PRINTKRB_RECORD_MAX 1024
  46. /* Flags for a single printk record. */
  47. enum printk_info_flags {
  48. LOG_NEWLINE = 2, /* text ended with a newline */
  49. LOG_CONT = 8, /* text is a fragment of a continuation line */
  50. };
  51. struct printk_ringbuffer;
  52. struct dev_printk_info;
  53. extern struct printk_ringbuffer *prb;
  54. extern bool printk_kthreads_running;
  55. __printf(4, 0)
  56. int vprintk_store(int facility, int level,
  57. const struct dev_printk_info *dev_info,
  58. const char *fmt, va_list args);
  59. __printf(1, 0) int vprintk_default(const char *fmt, va_list args);
  60. __printf(1, 0) int vprintk_deferred(const char *fmt, va_list args);
  61. void __printk_safe_enter(void);
  62. void __printk_safe_exit(void);
  63. bool printk_percpu_data_ready(void);
  64. #define printk_safe_enter_irqsave(flags) \
  65. do { \
  66. local_irq_save(flags); \
  67. __printk_safe_enter(); \
  68. } while (0)
  69. #define printk_safe_exit_irqrestore(flags) \
  70. do { \
  71. __printk_safe_exit(); \
  72. local_irq_restore(flags); \
  73. } while (0)
  74. void defer_console_output(void);
  75. bool is_printk_legacy_deferred(void);
  76. u16 printk_parse_prefix(const char *text, int *level,
  77. enum printk_info_flags *flags);
  78. void console_lock_spinning_enable(void);
  79. int console_lock_spinning_disable_and_check(int cookie);
  80. u64 nbcon_seq_read(struct console *con);
  81. void nbcon_seq_force(struct console *con, u64 seq);
  82. bool nbcon_alloc(struct console *con);
  83. void nbcon_free(struct console *con);
  84. enum nbcon_prio nbcon_get_default_prio(void);
  85. void nbcon_atomic_flush_pending(void);
  86. bool nbcon_legacy_emit_next_record(struct console *con, bool *handover,
  87. int cookie, bool use_atomic);
  88. bool nbcon_kthread_create(struct console *con);
  89. void nbcon_kthread_stop(struct console *con);
  90. void nbcon_kthreads_wake(void);
  91. /*
  92. * Check if the given console is currently capable and allowed to print
  93. * records. Note that this function does not consider the current context,
  94. * which can also play a role in deciding if @con can be used to print
  95. * records.
  96. */
  97. static inline bool console_is_usable(struct console *con, short flags, bool use_atomic)
  98. {
  99. if (!(flags & CON_ENABLED))
  100. return false;
  101. if ((flags & CON_SUSPENDED))
  102. return false;
  103. if (flags & CON_NBCON) {
  104. /* The write_atomic() callback is optional. */
  105. if (use_atomic && !con->write_atomic)
  106. return false;
  107. /*
  108. * For the !use_atomic case, @printk_kthreads_running is not
  109. * checked because the write_thread() callback is also used
  110. * via the legacy loop when the printer threads are not
  111. * available.
  112. */
  113. } else {
  114. if (!con->write)
  115. return false;
  116. }
  117. /*
  118. * Console drivers may assume that per-cpu resources have been
  119. * allocated. So unless they're explicitly marked as being able to
  120. * cope (CON_ANYTIME) don't call them until this CPU is officially up.
  121. */
  122. if (!cpu_online(raw_smp_processor_id()) && !(flags & CON_ANYTIME))
  123. return false;
  124. return true;
  125. }
  126. /**
  127. * nbcon_kthread_wake - Wake up a console printing thread
  128. * @con: Console to operate on
  129. */
  130. static inline void nbcon_kthread_wake(struct console *con)
  131. {
  132. /*
  133. * Guarantee any new records can be seen by tasks preparing to wait
  134. * before this context checks if the rcuwait is empty.
  135. *
  136. * The full memory barrier in rcuwait_wake_up() pairs with the full
  137. * memory barrier within set_current_state() of
  138. * ___rcuwait_wait_event(), which is called after prepare_to_rcuwait()
  139. * adds the waiter but before it has checked the wait condition.
  140. *
  141. * This pairs with nbcon_kthread_func:A.
  142. */
  143. rcuwait_wake_up(&con->rcuwait); /* LMM(nbcon_kthread_wake:A) */
  144. }
  145. #else
  146. #define PRINTK_PREFIX_MAX 0
  147. #define PRINTK_MESSAGE_MAX 0
  148. #define PRINTKRB_RECORD_MAX 0
  149. #define printk_kthreads_running (false)
  150. /*
  151. * In !PRINTK builds we still export console_sem
  152. * semaphore and some of console functions (console_unlock()/etc.), so
  153. * printk-safe must preserve the existing local IRQ guarantees.
  154. */
  155. #define printk_safe_enter_irqsave(flags) local_irq_save(flags)
  156. #define printk_safe_exit_irqrestore(flags) local_irq_restore(flags)
  157. static inline bool printk_percpu_data_ready(void) { return false; }
  158. static inline void defer_console_output(void) { }
  159. static inline bool is_printk_legacy_deferred(void) { return false; }
  160. static inline u64 nbcon_seq_read(struct console *con) { return 0; }
  161. static inline void nbcon_seq_force(struct console *con, u64 seq) { }
  162. static inline bool nbcon_alloc(struct console *con) { return false; }
  163. static inline void nbcon_free(struct console *con) { }
  164. static inline enum nbcon_prio nbcon_get_default_prio(void) { return NBCON_PRIO_NONE; }
  165. static inline void nbcon_atomic_flush_pending(void) { }
  166. static inline bool nbcon_legacy_emit_next_record(struct console *con, bool *handover,
  167. int cookie, bool use_atomic) { return false; }
  168. static inline void nbcon_kthread_wake(struct console *con) { }
  169. static inline void nbcon_kthreads_wake(void) { }
  170. static inline bool console_is_usable(struct console *con, short flags,
  171. bool use_atomic) { return false; }
  172. #endif /* CONFIG_PRINTK */
  173. extern bool have_boot_console;
  174. extern bool have_nbcon_console;
  175. extern bool have_legacy_console;
  176. extern bool legacy_allow_panic_sync;
  177. /**
  178. * struct console_flush_type - Define available console flush methods
  179. * @nbcon_atomic: Flush directly using nbcon_atomic() callback
  180. * @nbcon_offload: Offload flush to printer thread
  181. * @legacy_direct: Call the legacy loop in this context
  182. * @legacy_offload: Offload the legacy loop into IRQ or legacy thread
  183. *
  184. * Note that the legacy loop also flushes the nbcon consoles.
  185. */
  186. struct console_flush_type {
  187. bool nbcon_atomic;
  188. bool nbcon_offload;
  189. bool legacy_direct;
  190. bool legacy_offload;
  191. };
  192. /*
  193. * Identify which console flushing methods should be used in the context of
  194. * the caller.
  195. */
  196. static inline void printk_get_console_flush_type(struct console_flush_type *ft)
  197. {
  198. memset(ft, 0, sizeof(*ft));
  199. switch (nbcon_get_default_prio()) {
  200. case NBCON_PRIO_NORMAL:
  201. if (have_nbcon_console && !have_boot_console) {
  202. if (printk_kthreads_running)
  203. ft->nbcon_offload = true;
  204. else
  205. ft->nbcon_atomic = true;
  206. }
  207. /* Legacy consoles are flushed directly when possible. */
  208. if (have_legacy_console || have_boot_console) {
  209. if (!is_printk_legacy_deferred())
  210. ft->legacy_direct = true;
  211. else
  212. ft->legacy_offload = true;
  213. }
  214. break;
  215. case NBCON_PRIO_EMERGENCY:
  216. if (have_nbcon_console && !have_boot_console)
  217. ft->nbcon_atomic = true;
  218. /* Legacy consoles are flushed directly when possible. */
  219. if (have_legacy_console || have_boot_console) {
  220. if (!is_printk_legacy_deferred())
  221. ft->legacy_direct = true;
  222. else
  223. ft->legacy_offload = true;
  224. }
  225. break;
  226. case NBCON_PRIO_PANIC:
  227. /*
  228. * In panic, the nbcon consoles will directly print. But
  229. * only allowed if there are no boot consoles.
  230. */
  231. if (have_nbcon_console && !have_boot_console)
  232. ft->nbcon_atomic = true;
  233. if (have_legacy_console || have_boot_console) {
  234. /*
  235. * This is the same decision as NBCON_PRIO_NORMAL
  236. * except that offloading never occurs in panic.
  237. *
  238. * Note that console_flush_on_panic() will flush
  239. * legacy consoles anyway, even if unsafe.
  240. */
  241. if (!is_printk_legacy_deferred())
  242. ft->legacy_direct = true;
  243. /*
  244. * In panic, if nbcon atomic printing occurs,
  245. * the legacy consoles must remain silent until
  246. * explicitly allowed.
  247. */
  248. if (ft->nbcon_atomic && !legacy_allow_panic_sync)
  249. ft->legacy_direct = false;
  250. }
  251. break;
  252. default:
  253. WARN_ON_ONCE(1);
  254. break;
  255. }
  256. }
  257. extern struct printk_buffers printk_shared_pbufs;
  258. /**
  259. * struct printk_buffers - Buffers to read/format/output printk messages.
  260. * @outbuf: After formatting, contains text to output.
  261. * @scratchbuf: Used as temporary ringbuffer reading and string-print space.
  262. */
  263. struct printk_buffers {
  264. char outbuf[PRINTK_MESSAGE_MAX];
  265. char scratchbuf[PRINTKRB_RECORD_MAX];
  266. };
  267. /**
  268. * struct printk_message - Container for a prepared printk message.
  269. * @pbufs: printk buffers used to prepare the message.
  270. * @outbuf_len: The length of prepared text in @pbufs->outbuf to output. This
  271. * does not count the terminator. A value of 0 means there is
  272. * nothing to output and this record should be skipped.
  273. * @seq: The sequence number of the record used for @pbufs->outbuf.
  274. * @dropped: The number of dropped records from reading @seq.
  275. */
  276. struct printk_message {
  277. struct printk_buffers *pbufs;
  278. unsigned int outbuf_len;
  279. u64 seq;
  280. unsigned long dropped;
  281. };
  282. bool other_cpu_in_panic(void);
  283. bool printk_get_next_message(struct printk_message *pmsg, u64 seq,
  284. bool is_extended, bool may_supress);
  285. #ifdef CONFIG_PRINTK
  286. void console_prepend_dropped(struct printk_message *pmsg, unsigned long dropped);
  287. void console_prepend_replay(struct printk_message *pmsg);
  288. #endif
  289. #ifdef CONFIG_SMP
  290. bool is_printk_cpu_sync_owner(void);
  291. #else
  292. static inline bool is_printk_cpu_sync_owner(void) { return false; }
  293. #endif