Loading...
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
9#if defined(CONFIG_PRINTK) && defined(CONFIG_SYSCTL)
10struct ctl_table;
11void __init printk_sysctl_init(void);
12int devkmsg_sysctl_set_loglvl(const struct ctl_table *table, int write,
13 void *buffer, size_t *lenp, loff_t *ppos);
14#else
15#define printk_sysctl_init() do { } while (0)
16#endif
17
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__)
23
24/*
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
29 * first.
30 */
31#ifdef CONFIG_PREEMPT_RT
32# define force_legacy_kthread() (true)
33#else
34# define force_legacy_kthread() (false)
35#endif
36
37#ifdef CONFIG_PRINTK
38
39#ifdef CONFIG_PRINTK_CALLER
40#define PRINTK_PREFIX_MAX 48
41#else
42#define PRINTK_PREFIX_MAX 32
43#endif
44
45/*
46 * the maximum size of a formatted record (i.e. with prefix added
47 * per line and dropped messages or in extended message format)
48 */
49#define PRINTK_MESSAGE_MAX 2048
50
51/* the maximum size allowed to be reserved for a record */
52#define PRINTKRB_RECORD_MAX 1024
53
54/* Flags for a single printk record. */
55enum printk_info_flags {
56 /* always show on console, ignore console_loglevel */
57 LOG_FORCE_CON = 1,
58 LOG_NEWLINE = 2, /* text ended with a newline */
59 LOG_CONT = 8, /* text is a fragment of a continuation line */
60};
61
62struct printk_ringbuffer;
63struct dev_printk_info;
64
65extern struct printk_ringbuffer *prb;
66extern bool printk_kthreads_running;
67
68__printf(4, 0)
69int vprintk_store(int facility, int level,
70 const struct dev_printk_info *dev_info,
71 const char *fmt, va_list args);
72
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);
75
76void __printk_safe_enter(void);
77void __printk_safe_exit(void);
78
79bool printk_percpu_data_ready(void);
80
81#define printk_safe_enter_irqsave(flags) \
82 do { \
83 local_irq_save(flags); \
84 __printk_safe_enter(); \
85 } while (0)
86
87#define printk_safe_exit_irqrestore(flags) \
88 do { \
89 __printk_safe_exit(); \
90 local_irq_restore(flags); \
91 } while (0)
92
93void defer_console_output(void);
94bool is_printk_legacy_deferred(void);
95bool is_printk_force_console(void);
96
97u16 printk_parse_prefix(const char *text, int *level,
98 enum printk_info_flags *flags);
99void console_lock_spinning_enable(void);
100int console_lock_spinning_disable_and_check(int cookie);
101
102u64 nbcon_seq_read(struct console *con);
103void nbcon_seq_force(struct console *con, u64 seq);
104bool nbcon_alloc(struct console *con);
105void nbcon_free(struct console *con);
106enum nbcon_prio nbcon_get_default_prio(void);
107void nbcon_atomic_flush_pending(void);
108bool nbcon_legacy_emit_next_record(struct console *con, bool *handover,
109 int cookie, bool use_atomic);
110bool nbcon_kthread_create(struct console *con);
111void nbcon_kthread_stop(struct console *con);
112void nbcon_kthreads_wake(void);
113
114/*
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
118 * records.
119 */
120static inline bool console_is_usable(struct console *con, short flags, bool use_atomic)
121{
122 if (!(flags & CON_ENABLED))
123 return false;
124
125 if ((flags & CON_SUSPENDED))
126 return false;
127
128 if (flags & CON_NBCON) {
129 /* The write_atomic() callback is optional. */
130 if (use_atomic && !con->write_atomic)
131 return false;
132
133 /*
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
137 * available.
138 */
139 } else {
140 if (!con->write)
141 return false;
142 }
143
144 /*
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.
148 */
149 if (!cpu_online(raw_smp_processor_id()) && !(flags & CON_ANYTIME))
150 return false;
151
152 return true;
153}
154
155/**
156 * nbcon_kthread_wake - Wake up a console printing thread
157 * @con: Console to operate on
158 */
159static inline void nbcon_kthread_wake(struct console *con)
160{
161 /*
162 * Guarantee any new records can be seen by tasks preparing to wait
163 * before this context checks if the rcuwait is empty.
164 *
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.
169 *
170 * This pairs with nbcon_kthread_func:A.
171 */
172 rcuwait_wake_up(&con->rcuwait); /* LMM(nbcon_kthread_wake:A) */
173}
174
175#else
176
177#define PRINTK_PREFIX_MAX 0
178#define PRINTK_MESSAGE_MAX 0
179#define PRINTKRB_RECORD_MAX 0
180
181#define printk_kthreads_running (false)
182
183/*
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.
187 */
188#define printk_safe_enter_irqsave(flags) local_irq_save(flags)
189#define printk_safe_exit_irqrestore(flags) local_irq_restore(flags)
190
191static inline bool printk_percpu_data_ready(void) { return false; }
192static inline void defer_console_output(void) { }
193static inline bool is_printk_legacy_deferred(void) { return false; }
194static inline u64 nbcon_seq_read(struct console *con) { return 0; }
195static inline void nbcon_seq_force(struct console *con, u64 seq) { }
196static inline bool nbcon_alloc(struct console *con) { return false; }
197static inline void nbcon_free(struct console *con) { }
198static inline enum nbcon_prio nbcon_get_default_prio(void) { return NBCON_PRIO_NONE; }
199static inline void nbcon_atomic_flush_pending(void) { }
200static inline bool nbcon_legacy_emit_next_record(struct console *con, bool *handover,
201 int cookie, bool use_atomic) { return false; }
202static inline void nbcon_kthread_wake(struct console *con) { }
203static inline void nbcon_kthreads_wake(void) { }
204
205static inline bool console_is_usable(struct console *con, short flags,
206 bool use_atomic) { return false; }
207
208#endif /* CONFIG_PRINTK */
209
210extern bool have_boot_console;
211extern bool have_nbcon_console;
212extern bool have_legacy_console;
213extern bool legacy_allow_panic_sync;
214
215/**
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
221 *
222 * Note that the legacy loop also flushes the nbcon consoles.
223 */
224struct console_flush_type {
225 bool nbcon_atomic;
226 bool nbcon_offload;
227 bool legacy_direct;
228 bool legacy_offload;
229};
230
231/*
232 * Identify which console flushing methods should be used in the context of
233 * the caller.
234 */
235static inline void printk_get_console_flush_type(struct console_flush_type *ft)
236{
237 memset(ft, 0, sizeof(*ft));
238
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;
244 else
245 ft->nbcon_atomic = true;
246 }
247
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;
252 else
253 ft->legacy_offload = true;
254 }
255 break;
256
257 case NBCON_PRIO_EMERGENCY:
258 if (have_nbcon_console && !have_boot_console)
259 ft->nbcon_atomic = true;
260
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;
265 else
266 ft->legacy_offload = true;
267 }
268 break;
269
270 case NBCON_PRIO_PANIC:
271 /*
272 * In panic, the nbcon consoles will directly print. But
273 * only allowed if there are no boot consoles.
274 */
275 if (have_nbcon_console && !have_boot_console)
276 ft->nbcon_atomic = true;
277
278 if (have_legacy_console || have_boot_console) {
279 /*
280 * This is the same decision as NBCON_PRIO_NORMAL
281 * except that offloading never occurs in panic.
282 *
283 * Note that console_flush_on_panic() will flush
284 * legacy consoles anyway, even if unsafe.
285 */
286 if (!is_printk_legacy_deferred())
287 ft->legacy_direct = true;
288
289 /*
290 * In panic, if nbcon atomic printing occurs,
291 * the legacy consoles must remain silent until
292 * explicitly allowed.
293 */
294 if (ft->nbcon_atomic && !legacy_allow_panic_sync)
295 ft->legacy_direct = false;
296 }
297 break;
298
299 default:
300 WARN_ON_ONCE(1);
301 break;
302 }
303}
304
305extern struct printk_buffers printk_shared_pbufs;
306
307/**
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.
311 */
312struct printk_buffers {
313 char outbuf[PRINTK_MESSAGE_MAX];
314 char scratchbuf[PRINTKRB_RECORD_MAX];
315};
316
317/**
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.
325 */
326struct printk_message {
327 struct printk_buffers *pbufs;
328 unsigned int outbuf_len;
329 u64 seq;
330 unsigned long dropped;
331};
332
333bool other_cpu_in_panic(void);
334bool printk_get_next_message(struct printk_message *pmsg, u64 seq,
335 bool is_extended, bool may_supress);
336
337#ifdef CONFIG_PRINTK
338void console_prepend_dropped(struct printk_message *pmsg, unsigned long dropped);
339void console_prepend_replay(struct printk_message *pmsg);
340#endif
341
342#ifdef CONFIG_SMP
343bool is_printk_cpu_sync_owner(void);
344#else
345static inline bool is_printk_cpu_sync_owner(void) { return false; }
346#endif
1/*
2 * internal.h - printk internal definitions
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/percpu.h>
18
19typedef __printf(1, 0) int (*printk_func_t)(const char *fmt, va_list args);
20
21int __printf(1, 0) vprintk_default(const char *fmt, va_list args);
22
23#ifdef CONFIG_PRINTK_NMI
24
25extern raw_spinlock_t logbuf_lock;
26
27/*
28 * printk() could not take logbuf_lock in NMI context. Instead,
29 * it temporary stores the strings into a per-CPU buffer.
30 * The alternative implementation is chosen transparently
31 * via per-CPU variable.
32 */
33DECLARE_PER_CPU(printk_func_t, printk_func);
34static inline __printf(1, 0) int vprintk_func(const char *fmt, va_list args)
35{
36 return this_cpu_read(printk_func)(fmt, args);
37}
38
39extern atomic_t nmi_message_lost;
40static inline int get_nmi_message_lost(void)
41{
42 return atomic_xchg(&nmi_message_lost, 0);
43}
44
45#else /* CONFIG_PRINTK_NMI */
46
47static inline __printf(1, 0) int vprintk_func(const char *fmt, va_list args)
48{
49 return vprintk_default(fmt, args);
50}
51
52static inline int get_nmi_message_lost(void)
53{
54 return 0;
55}
56
57#endif /* CONFIG_PRINTK_NMI */