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 LOG_NEWLINE
= 2, /* text ended with a newline */
57 LOG_CONT
= 8, /* text is a fragment of a continuation line */
60 struct printk_ringbuffer
;
61 struct dev_printk_info
;
63 extern struct printk_ringbuffer
*prb
;
64 extern bool printk_kthreads_running
;
67 int vprintk_store(int facility
, int level
,
68 const struct dev_printk_info
*dev_info
,
69 const char *fmt
, va_list args
);
71 __printf(1, 0) int vprintk_default(const char *fmt
, va_list args
);
72 __printf(1, 0) int vprintk_deferred(const char *fmt
, va_list args
);
74 void __printk_safe_enter(void);
75 void __printk_safe_exit(void);
77 bool printk_percpu_data_ready(void);
79 #define printk_safe_enter_irqsave(flags) \
81 local_irq_save(flags); \
82 __printk_safe_enter(); \
85 #define printk_safe_exit_irqrestore(flags) \
87 __printk_safe_exit(); \
88 local_irq_restore(flags); \
91 void defer_console_output(void);
92 bool is_printk_legacy_deferred(void);
94 u16
printk_parse_prefix(const char *text
, int *level
,
95 enum printk_info_flags
*flags
);
96 void console_lock_spinning_enable(void);
97 int console_lock_spinning_disable_and_check(int cookie
);
99 u64
nbcon_seq_read(struct console
*con
);
100 void nbcon_seq_force(struct console
*con
, u64 seq
);
101 bool nbcon_alloc(struct console
*con
);
102 void nbcon_free(struct console
*con
);
103 enum nbcon_prio
nbcon_get_default_prio(void);
104 void nbcon_atomic_flush_pending(void);
105 bool nbcon_legacy_emit_next_record(struct console
*con
, bool *handover
,
106 int cookie
, bool use_atomic
);
107 bool nbcon_kthread_create(struct console
*con
);
108 void nbcon_kthread_stop(struct console
*con
);
109 void nbcon_kthreads_wake(void);
112 * Check if the given console is currently capable and allowed to print
113 * records. Note that this function does not consider the current context,
114 * which can also play a role in deciding if @con can be used to print
117 static inline bool console_is_usable(struct console
*con
, short flags
, bool use_atomic
)
119 if (!(flags
& CON_ENABLED
))
122 if ((flags
& CON_SUSPENDED
))
125 if (flags
& CON_NBCON
) {
126 /* The write_atomic() callback is optional. */
127 if (use_atomic
&& !con
->write_atomic
)
131 * For the !use_atomic case, @printk_kthreads_running is not
132 * checked because the write_thread() callback is also used
133 * via the legacy loop when the printer threads are not
142 * Console drivers may assume that per-cpu resources have been
143 * allocated. So unless they're explicitly marked as being able to
144 * cope (CON_ANYTIME) don't call them until this CPU is officially up.
146 if (!cpu_online(raw_smp_processor_id()) && !(flags
& CON_ANYTIME
))
153 * nbcon_kthread_wake - Wake up a console printing thread
154 * @con: Console to operate on
156 static inline void nbcon_kthread_wake(struct console
*con
)
159 * Guarantee any new records can be seen by tasks preparing to wait
160 * before this context checks if the rcuwait is empty.
162 * The full memory barrier in rcuwait_wake_up() pairs with the full
163 * memory barrier within set_current_state() of
164 * ___rcuwait_wait_event(), which is called after prepare_to_rcuwait()
165 * adds the waiter but before it has checked the wait condition.
167 * This pairs with nbcon_kthread_func:A.
169 rcuwait_wake_up(&con
->rcuwait
); /* LMM(nbcon_kthread_wake:A) */
174 #define PRINTK_PREFIX_MAX 0
175 #define PRINTK_MESSAGE_MAX 0
176 #define PRINTKRB_RECORD_MAX 0
178 #define printk_kthreads_running (false)
181 * In !PRINTK builds we still export console_sem
182 * semaphore and some of console functions (console_unlock()/etc.), so
183 * printk-safe must preserve the existing local IRQ guarantees.
185 #define printk_safe_enter_irqsave(flags) local_irq_save(flags)
186 #define printk_safe_exit_irqrestore(flags) local_irq_restore(flags)
188 static inline bool printk_percpu_data_ready(void) { return false; }
189 static inline void defer_console_output(void) { }
190 static inline bool is_printk_legacy_deferred(void) { return false; }
191 static inline u64
nbcon_seq_read(struct console
*con
) { return 0; }
192 static inline void nbcon_seq_force(struct console
*con
, u64 seq
) { }
193 static inline bool nbcon_alloc(struct console
*con
) { return false; }
194 static inline void nbcon_free(struct console
*con
) { }
195 static inline enum nbcon_prio
nbcon_get_default_prio(void) { return NBCON_PRIO_NONE
; }
196 static inline void nbcon_atomic_flush_pending(void) { }
197 static inline bool nbcon_legacy_emit_next_record(struct console
*con
, bool *handover
,
198 int cookie
, bool use_atomic
) { return false; }
199 static inline void nbcon_kthread_wake(struct console
*con
) { }
200 static inline void nbcon_kthreads_wake(void) { }
202 static inline bool console_is_usable(struct console
*con
, short flags
,
203 bool use_atomic
) { return false; }
205 #endif /* CONFIG_PRINTK */
207 extern bool have_boot_console
;
208 extern bool have_nbcon_console
;
209 extern bool have_legacy_console
;
210 extern bool legacy_allow_panic_sync
;
213 * struct console_flush_type - Define available console flush methods
214 * @nbcon_atomic: Flush directly using nbcon_atomic() callback
215 * @nbcon_offload: Offload flush to printer thread
216 * @legacy_direct: Call the legacy loop in this context
217 * @legacy_offload: Offload the legacy loop into IRQ or legacy thread
219 * Note that the legacy loop also flushes the nbcon consoles.
221 struct console_flush_type
{
229 * Identify which console flushing methods should be used in the context of
232 static inline void printk_get_console_flush_type(struct console_flush_type
*ft
)
234 memset(ft
, 0, sizeof(*ft
));
236 switch (nbcon_get_default_prio()) {
237 case NBCON_PRIO_NORMAL
:
238 if (have_nbcon_console
&& !have_boot_console
) {
239 if (printk_kthreads_running
)
240 ft
->nbcon_offload
= true;
242 ft
->nbcon_atomic
= true;
245 /* Legacy consoles are flushed directly when possible. */
246 if (have_legacy_console
|| have_boot_console
) {
247 if (!is_printk_legacy_deferred())
248 ft
->legacy_direct
= true;
250 ft
->legacy_offload
= true;
254 case NBCON_PRIO_EMERGENCY
:
255 if (have_nbcon_console
&& !have_boot_console
)
256 ft
->nbcon_atomic
= true;
258 /* Legacy consoles are flushed directly when possible. */
259 if (have_legacy_console
|| have_boot_console
) {
260 if (!is_printk_legacy_deferred())
261 ft
->legacy_direct
= true;
263 ft
->legacy_offload
= true;
267 case NBCON_PRIO_PANIC
:
269 * In panic, the nbcon consoles will directly print. But
270 * only allowed if there are no boot consoles.
272 if (have_nbcon_console
&& !have_boot_console
)
273 ft
->nbcon_atomic
= true;
275 if (have_legacy_console
|| have_boot_console
) {
277 * This is the same decision as NBCON_PRIO_NORMAL
278 * except that offloading never occurs in panic.
280 * Note that console_flush_on_panic() will flush
281 * legacy consoles anyway, even if unsafe.
283 if (!is_printk_legacy_deferred())
284 ft
->legacy_direct
= true;
287 * In panic, if nbcon atomic printing occurs,
288 * the legacy consoles must remain silent until
289 * explicitly allowed.
291 if (ft
->nbcon_atomic
&& !legacy_allow_panic_sync
)
292 ft
->legacy_direct
= false;
302 extern struct printk_buffers printk_shared_pbufs
;
305 * struct printk_buffers - Buffers to read/format/output printk messages.
306 * @outbuf: After formatting, contains text to output.
307 * @scratchbuf: Used as temporary ringbuffer reading and string-print space.
309 struct printk_buffers
{
310 char outbuf
[PRINTK_MESSAGE_MAX
];
311 char scratchbuf
[PRINTKRB_RECORD_MAX
];
315 * struct printk_message - Container for a prepared printk message.
316 * @pbufs: printk buffers used to prepare the message.
317 * @outbuf_len: The length of prepared text in @pbufs->outbuf to output. This
318 * does not count the terminator. A value of 0 means there is
319 * nothing to output and this record should be skipped.
320 * @seq: The sequence number of the record used for @pbufs->outbuf.
321 * @dropped: The number of dropped records from reading @seq.
323 struct printk_message
{
324 struct printk_buffers
*pbufs
;
325 unsigned int outbuf_len
;
327 unsigned long dropped
;
330 bool other_cpu_in_panic(void);
331 bool printk_get_next_message(struct printk_message
*pmsg
, u64 seq
,
332 bool is_extended
, bool may_supress
);
335 void console_prepend_dropped(struct printk_message
*pmsg
, unsigned long dropped
);
336 void console_prepend_replay(struct printk_message
*pmsg
);