1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * internal.h - printk internal definitions
5 #include <linux/console.h>
6 #include <linux/percpu.h>
7 #include <linux/types.h>
9 #if defined(CONFIG_PRINTK) && defined(CONFIG_SYSCTL)
11 void __init
printk_sysctl_init(void);
12 int devkmsg_sysctl_set_loglvl(const struct ctl_table
*table
, int write
,
13 void *buffer
, size_t *lenp
, loff_t
*ppos
);
15 #define printk_sysctl_init() do { } while (0)
18 #define con_printk(lvl, con, fmt, ...) \
19 printk(lvl pr_fmt("%s%sconsole [%s%d] " fmt), \
20 (con->flags & CON_NBCON) ? "" : "legacy ", \
21 (con->flags & CON_BOOT) ? "boot" : "", \
22 con->name, con->index, ##__VA_ARGS__)
25 * Identify if legacy printing is forced in a dedicated kthread. If
26 * true, all printing via console lock occurs within a dedicated
27 * legacy printer thread. The only exception is on panic, after the
28 * nbcon consoles have had their chance to print the panic messages
31 #ifdef CONFIG_PREEMPT_RT
32 # define force_legacy_kthread() (true)
34 # define force_legacy_kthread() (false)
39 #ifdef CONFIG_PRINTK_CALLER
40 #define PRINTK_PREFIX_MAX 48
42 #define PRINTK_PREFIX_MAX 32
46 * the maximum size of a formatted record (i.e. with prefix added
47 * per line and dropped messages or in extended message format)
49 #define PRINTK_MESSAGE_MAX 2048
51 /* the maximum size allowed to be reserved for a record */
52 #define PRINTKRB_RECORD_MAX 1024
54 /* Flags for a single printk record. */
55 enum printk_info_flags
{
56 /* always show on console, ignore console_loglevel */
58 LOG_NEWLINE
= 2, /* text ended with a newline */
59 LOG_CONT
= 8, /* text is a fragment of a continuation line */
62 struct printk_ringbuffer
;
63 struct dev_printk_info
;
65 extern struct printk_ringbuffer
*prb
;
66 extern bool printk_kthreads_running
;
69 int vprintk_store(int facility
, int level
,
70 const struct dev_printk_info
*dev_info
,
71 const char *fmt
, va_list args
);
73 __printf(1, 0) int vprintk_default(const char *fmt
, va_list args
);
74 __printf(1, 0) int vprintk_deferred(const char *fmt
, va_list args
);
76 void __printk_safe_enter(void);
77 void __printk_safe_exit(void);
79 bool printk_percpu_data_ready(void);
81 #define printk_safe_enter_irqsave(flags) \
83 local_irq_save(flags); \
84 __printk_safe_enter(); \
87 #define printk_safe_exit_irqrestore(flags) \
89 __printk_safe_exit(); \
90 local_irq_restore(flags); \
93 void defer_console_output(void);
94 bool is_printk_legacy_deferred(void);
95 bool is_printk_force_console(void);
97 u16
printk_parse_prefix(const char *text
, int *level
,
98 enum printk_info_flags
*flags
);
99 void console_lock_spinning_enable(void);
100 int console_lock_spinning_disable_and_check(int cookie
);
102 u64
nbcon_seq_read(struct console
*con
);
103 void nbcon_seq_force(struct console
*con
, u64 seq
);
104 bool nbcon_alloc(struct console
*con
);
105 void nbcon_free(struct console
*con
);
106 enum nbcon_prio
nbcon_get_default_prio(void);
107 void nbcon_atomic_flush_pending(void);
108 bool nbcon_legacy_emit_next_record(struct console
*con
, bool *handover
,
109 int cookie
, bool use_atomic
);
110 bool nbcon_kthread_create(struct console
*con
);
111 void nbcon_kthread_stop(struct console
*con
);
112 void nbcon_kthreads_wake(void);
115 * Check if the given console is currently capable and allowed to print
116 * records. Note that this function does not consider the current context,
117 * which can also play a role in deciding if @con can be used to print
120 static inline bool console_is_usable(struct console
*con
, short flags
, bool use_atomic
)
122 if (!(flags
& CON_ENABLED
))
125 if ((flags
& CON_SUSPENDED
))
128 if (flags
& CON_NBCON
) {
129 /* The write_atomic() callback is optional. */
130 if (use_atomic
&& !con
->write_atomic
)
134 * For the !use_atomic case, @printk_kthreads_running is not
135 * checked because the write_thread() callback is also used
136 * via the legacy loop when the printer threads are not
145 * Console drivers may assume that per-cpu resources have been
146 * allocated. So unless they're explicitly marked as being able to
147 * cope (CON_ANYTIME) don't call them until this CPU is officially up.
149 if (!cpu_online(raw_smp_processor_id()) && !(flags
& CON_ANYTIME
))
156 * nbcon_kthread_wake - Wake up a console printing thread
157 * @con: Console to operate on
159 static inline void nbcon_kthread_wake(struct console
*con
)
162 * Guarantee any new records can be seen by tasks preparing to wait
163 * before this context checks if the rcuwait is empty.
165 * The full memory barrier in rcuwait_wake_up() pairs with the full
166 * memory barrier within set_current_state() of
167 * ___rcuwait_wait_event(), which is called after prepare_to_rcuwait()
168 * adds the waiter but before it has checked the wait condition.
170 * This pairs with nbcon_kthread_func:A.
172 rcuwait_wake_up(&con
->rcuwait
); /* LMM(nbcon_kthread_wake:A) */
177 #define PRINTK_PREFIX_MAX 0
178 #define PRINTK_MESSAGE_MAX 0
179 #define PRINTKRB_RECORD_MAX 0
181 #define printk_kthreads_running (false)
184 * In !PRINTK builds we still export console_sem
185 * semaphore and some of console functions (console_unlock()/etc.), so
186 * printk-safe must preserve the existing local IRQ guarantees.
188 #define printk_safe_enter_irqsave(flags) local_irq_save(flags)
189 #define printk_safe_exit_irqrestore(flags) local_irq_restore(flags)
191 static inline bool printk_percpu_data_ready(void) { return false; }
192 static inline void defer_console_output(void) { }
193 static inline bool is_printk_legacy_deferred(void) { return false; }
194 static inline u64
nbcon_seq_read(struct console
*con
) { return 0; }
195 static inline void nbcon_seq_force(struct console
*con
, u64 seq
) { }
196 static inline bool nbcon_alloc(struct console
*con
) { return false; }
197 static inline void nbcon_free(struct console
*con
) { }
198 static inline enum nbcon_prio
nbcon_get_default_prio(void) { return NBCON_PRIO_NONE
; }
199 static inline void nbcon_atomic_flush_pending(void) { }
200 static inline bool nbcon_legacy_emit_next_record(struct console
*con
, bool *handover
,
201 int cookie
, bool use_atomic
) { return false; }
202 static inline void nbcon_kthread_wake(struct console
*con
) { }
203 static inline void nbcon_kthreads_wake(void) { }
205 static inline bool console_is_usable(struct console
*con
, short flags
,
206 bool use_atomic
) { return false; }
208 #endif /* CONFIG_PRINTK */
210 extern bool have_boot_console
;
211 extern bool have_nbcon_console
;
212 extern bool have_legacy_console
;
213 extern bool legacy_allow_panic_sync
;
216 * struct console_flush_type - Define available console flush methods
217 * @nbcon_atomic: Flush directly using nbcon_atomic() callback
218 * @nbcon_offload: Offload flush to printer thread
219 * @legacy_direct: Call the legacy loop in this context
220 * @legacy_offload: Offload the legacy loop into IRQ or legacy thread
222 * Note that the legacy loop also flushes the nbcon consoles.
224 struct console_flush_type
{
232 * Identify which console flushing methods should be used in the context of
235 static inline void printk_get_console_flush_type(struct console_flush_type
*ft
)
237 memset(ft
, 0, sizeof(*ft
));
239 switch (nbcon_get_default_prio()) {
240 case NBCON_PRIO_NORMAL
:
241 if (have_nbcon_console
&& !have_boot_console
) {
242 if (printk_kthreads_running
)
243 ft
->nbcon_offload
= true;
245 ft
->nbcon_atomic
= true;
248 /* Legacy consoles are flushed directly when possible. */
249 if (have_legacy_console
|| have_boot_console
) {
250 if (!is_printk_legacy_deferred())
251 ft
->legacy_direct
= true;
253 ft
->legacy_offload
= true;
257 case NBCON_PRIO_EMERGENCY
:
258 if (have_nbcon_console
&& !have_boot_console
)
259 ft
->nbcon_atomic
= true;
261 /* Legacy consoles are flushed directly when possible. */
262 if (have_legacy_console
|| have_boot_console
) {
263 if (!is_printk_legacy_deferred())
264 ft
->legacy_direct
= true;
266 ft
->legacy_offload
= true;
270 case NBCON_PRIO_PANIC
:
272 * In panic, the nbcon consoles will directly print. But
273 * only allowed if there are no boot consoles.
275 if (have_nbcon_console
&& !have_boot_console
)
276 ft
->nbcon_atomic
= true;
278 if (have_legacy_console
|| have_boot_console
) {
280 * This is the same decision as NBCON_PRIO_NORMAL
281 * except that offloading never occurs in panic.
283 * Note that console_flush_on_panic() will flush
284 * legacy consoles anyway, even if unsafe.
286 if (!is_printk_legacy_deferred())
287 ft
->legacy_direct
= true;
290 * In panic, if nbcon atomic printing occurs,
291 * the legacy consoles must remain silent until
292 * explicitly allowed.
294 if (ft
->nbcon_atomic
&& !legacy_allow_panic_sync
)
295 ft
->legacy_direct
= false;
305 extern struct printk_buffers printk_shared_pbufs
;
308 * struct printk_buffers - Buffers to read/format/output printk messages.
309 * @outbuf: After formatting, contains text to output.
310 * @scratchbuf: Used as temporary ringbuffer reading and string-print space.
312 struct printk_buffers
{
313 char outbuf
[PRINTK_MESSAGE_MAX
];
314 char scratchbuf
[PRINTKRB_RECORD_MAX
];
318 * struct printk_message - Container for a prepared printk message.
319 * @pbufs: printk buffers used to prepare the message.
320 * @outbuf_len: The length of prepared text in @pbufs->outbuf to output. This
321 * does not count the terminator. A value of 0 means there is
322 * nothing to output and this record should be skipped.
323 * @seq: The sequence number of the record used for @pbufs->outbuf.
324 * @dropped: The number of dropped records from reading @seq.
326 struct printk_message
{
327 struct printk_buffers
*pbufs
;
328 unsigned int outbuf_len
;
330 unsigned long dropped
;
333 bool other_cpu_in_panic(void);
334 bool printk_get_next_message(struct printk_message
*pmsg
, u64 seq
,
335 bool is_extended
, bool may_supress
);
338 void console_prepend_dropped(struct printk_message
*pmsg
, unsigned long dropped
);
339 void console_prepend_replay(struct printk_message
*pmsg
);