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)
10 struct ctl_table;
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);
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. */
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 */
58 };
59
60 struct printk_ringbuffer;
61 struct dev_printk_info;
62
63 extern struct printk_ringbuffer *prb;
64 extern bool printk_kthreads_running;
65 extern bool debug_non_panic_cpus;
66
67 __printf(4, 0)
68 int vprintk_store(int facility, int level,
69 const struct dev_printk_info *dev_info,
70 const char *fmt, va_list args);
71
72 __printf(1, 0) int vprintk_default(const char *fmt, va_list args);
73 __printf(1, 0) int vprintk_deferred(const char *fmt, va_list args);
74
75 void __printk_safe_enter(void);
76 void __printk_safe_exit(void);
77
78 bool printk_percpu_data_ready(void);
79
80 #define printk_safe_enter_irqsave(flags) \
81 do { \
82 local_irq_save(flags); \
83 __printk_safe_enter(); \
84 } while (0)
85
86 #define printk_safe_exit_irqrestore(flags) \
87 do { \
88 __printk_safe_exit(); \
89 local_irq_restore(flags); \
90 } while (0)
91
92 void defer_console_output(void);
93 bool is_printk_legacy_deferred(void);
94
95 u16 printk_parse_prefix(const char *text, int *level,
96 enum printk_info_flags *flags);
97 void console_lock_spinning_enable(void);
98 int console_lock_spinning_disable_and_check(int cookie);
99
100 u64 nbcon_seq_read(struct console *con);
101 void nbcon_seq_force(struct console *con, u64 seq);
102 bool nbcon_alloc(struct console *con);
103 void nbcon_free(struct console *con);
104 enum nbcon_prio nbcon_get_default_prio(void);
105 void nbcon_atomic_flush_pending(void);
106 bool nbcon_legacy_emit_next_record(struct console *con, bool *handover,
107 int cookie, bool use_atomic);
108 bool nbcon_kthread_create(struct console *con);
109 void nbcon_kthread_stop(struct console *con);
110 void nbcon_kthreads_wake(void);
111
112 /*
113 * Check if the given console is currently capable and allowed to print
114 * records. Note that this function does not consider the current context,
115 * which can also play a role in deciding if @con can be used to print
116 * records.
117 */
console_is_usable(struct console * con,short flags,bool use_atomic)118 static inline bool console_is_usable(struct console *con, short flags, bool use_atomic)
119 {
120 if (!(flags & CON_ENABLED))
121 return false;
122
123 if ((flags & CON_SUSPENDED))
124 return false;
125
126 if (flags & CON_NBCON) {
127 /* The write_atomic() callback is optional. */
128 if (use_atomic && !con->write_atomic)
129 return false;
130
131 /*
132 * For the !use_atomic case, @printk_kthreads_running is not
133 * checked because the write_thread() callback is also used
134 * via the legacy loop when the printer threads are not
135 * available.
136 */
137 } else {
138 if (!con->write)
139 return false;
140 }
141
142 /*
143 * Console drivers may assume that per-cpu resources have been
144 * allocated. So unless they're explicitly marked as being able to
145 * cope (CON_ANYTIME) don't call them until this CPU is officially up.
146 */
147 if (!cpu_online(raw_smp_processor_id()) && !(flags & CON_ANYTIME))
148 return false;
149
150 return true;
151 }
152
153 /**
154 * nbcon_kthread_wake - Wake up a console printing thread
155 * @con: Console to operate on
156 */
nbcon_kthread_wake(struct console * con)157 static inline void nbcon_kthread_wake(struct console *con)
158 {
159 /*
160 * Guarantee any new records can be seen by tasks preparing to wait
161 * before this context checks if the rcuwait is empty.
162 *
163 * The full memory barrier in rcuwait_wake_up() pairs with the full
164 * memory barrier within set_current_state() of
165 * ___rcuwait_wait_event(), which is called after prepare_to_rcuwait()
166 * adds the waiter but before it has checked the wait condition.
167 *
168 * This pairs with nbcon_kthread_func:A.
169 */
170 rcuwait_wake_up(&con->rcuwait); /* LMM(nbcon_kthread_wake:A) */
171 }
172
173 #else
174
175 #define PRINTK_PREFIX_MAX 0
176 #define PRINTK_MESSAGE_MAX 0
177 #define PRINTKRB_RECORD_MAX 0
178
179 #define printk_kthreads_running (false)
180
181 /*
182 * In !PRINTK builds we still export console_sem
183 * semaphore and some of console functions (console_unlock()/etc.), so
184 * printk-safe must preserve the existing local IRQ guarantees.
185 */
186 #define printk_safe_enter_irqsave(flags) local_irq_save(flags)
187 #define printk_safe_exit_irqrestore(flags) local_irq_restore(flags)
188
printk_percpu_data_ready(void)189 static inline bool printk_percpu_data_ready(void) { return false; }
defer_console_output(void)190 static inline void defer_console_output(void) { }
is_printk_legacy_deferred(void)191 static inline bool is_printk_legacy_deferred(void) { return false; }
nbcon_seq_read(struct console * con)192 static inline u64 nbcon_seq_read(struct console *con) { return 0; }
nbcon_seq_force(struct console * con,u64 seq)193 static inline void nbcon_seq_force(struct console *con, u64 seq) { }
nbcon_alloc(struct console * con)194 static inline bool nbcon_alloc(struct console *con) { return false; }
nbcon_free(struct console * con)195 static inline void nbcon_free(struct console *con) { }
nbcon_get_default_prio(void)196 static inline enum nbcon_prio nbcon_get_default_prio(void) { return NBCON_PRIO_NONE; }
nbcon_atomic_flush_pending(void)197 static inline void nbcon_atomic_flush_pending(void) { }
nbcon_legacy_emit_next_record(struct console * con,bool * handover,int cookie,bool use_atomic)198 static inline bool nbcon_legacy_emit_next_record(struct console *con, bool *handover,
199 int cookie, bool use_atomic) { return false; }
nbcon_kthread_wake(struct console * con)200 static inline void nbcon_kthread_wake(struct console *con) { }
nbcon_kthreads_wake(void)201 static inline void nbcon_kthreads_wake(void) { }
202
console_is_usable(struct console * con,short flags,bool use_atomic)203 static inline bool console_is_usable(struct console *con, short flags,
204 bool use_atomic) { return false; }
205
206 #endif /* CONFIG_PRINTK */
207
208 extern bool have_boot_console;
209 extern bool have_nbcon_console;
210 extern bool have_legacy_console;
211 extern bool legacy_allow_panic_sync;
212
213 /**
214 * struct console_flush_type - Define available console flush methods
215 * @nbcon_atomic: Flush directly using nbcon_atomic() callback
216 * @nbcon_offload: Offload flush to printer thread
217 * @legacy_direct: Call the legacy loop in this context
218 * @legacy_offload: Offload the legacy loop into IRQ or legacy thread
219 *
220 * Note that the legacy loop also flushes the nbcon consoles.
221 */
222 struct console_flush_type {
223 bool nbcon_atomic;
224 bool nbcon_offload;
225 bool legacy_direct;
226 bool legacy_offload;
227 };
228
229 /*
230 * Identify which console flushing methods should be used in the context of
231 * the caller.
232 */
printk_get_console_flush_type(struct console_flush_type * ft)233 static inline void printk_get_console_flush_type(struct console_flush_type *ft)
234 {
235 memset(ft, 0, sizeof(*ft));
236
237 switch (nbcon_get_default_prio()) {
238 case NBCON_PRIO_NORMAL:
239 if (have_nbcon_console && !have_boot_console) {
240 if (printk_kthreads_running)
241 ft->nbcon_offload = true;
242 else
243 ft->nbcon_atomic = true;
244 }
245
246 /* Legacy consoles are flushed directly when possible. */
247 if (have_legacy_console || have_boot_console) {
248 if (!is_printk_legacy_deferred())
249 ft->legacy_direct = true;
250 else
251 ft->legacy_offload = true;
252 }
253 break;
254
255 case NBCON_PRIO_EMERGENCY:
256 if (have_nbcon_console && !have_boot_console)
257 ft->nbcon_atomic = true;
258
259 /* Legacy consoles are flushed directly when possible. */
260 if (have_legacy_console || have_boot_console) {
261 if (!is_printk_legacy_deferred())
262 ft->legacy_direct = true;
263 else
264 ft->legacy_offload = true;
265 }
266 break;
267
268 case NBCON_PRIO_PANIC:
269 /*
270 * In panic, the nbcon consoles will directly print. But
271 * only allowed if there are no boot consoles.
272 */
273 if (have_nbcon_console && !have_boot_console)
274 ft->nbcon_atomic = true;
275
276 if (have_legacy_console || have_boot_console) {
277 /*
278 * This is the same decision as NBCON_PRIO_NORMAL
279 * except that offloading never occurs in panic.
280 *
281 * Note that console_flush_on_panic() will flush
282 * legacy consoles anyway, even if unsafe.
283 */
284 if (!is_printk_legacy_deferred())
285 ft->legacy_direct = true;
286
287 /*
288 * In panic, if nbcon atomic printing occurs,
289 * the legacy consoles must remain silent until
290 * explicitly allowed.
291 */
292 if (ft->nbcon_atomic && !legacy_allow_panic_sync)
293 ft->legacy_direct = false;
294 }
295 break;
296
297 default:
298 WARN_ON_ONCE(1);
299 break;
300 }
301 }
302
303 extern struct printk_buffers printk_shared_pbufs;
304
305 /**
306 * struct printk_buffers - Buffers to read/format/output printk messages.
307 * @outbuf: After formatting, contains text to output.
308 * @scratchbuf: Used as temporary ringbuffer reading and string-print space.
309 */
310 struct printk_buffers {
311 char outbuf[PRINTK_MESSAGE_MAX];
312 char scratchbuf[PRINTKRB_RECORD_MAX];
313 };
314
315 /**
316 * struct printk_message - Container for a prepared printk message.
317 * @pbufs: printk buffers used to prepare the message.
318 * @outbuf_len: The length of prepared text in @pbufs->outbuf to output. This
319 * does not count the terminator. A value of 0 means there is
320 * nothing to output and this record should be skipped.
321 * @seq: The sequence number of the record used for @pbufs->outbuf.
322 * @dropped: The number of dropped records from reading @seq.
323 */
324 struct printk_message {
325 struct printk_buffers *pbufs;
326 unsigned int outbuf_len;
327 u64 seq;
328 unsigned long dropped;
329 };
330
331 bool other_cpu_in_panic(void);
332 bool printk_get_next_message(struct printk_message *pmsg, u64 seq,
333 bool is_extended, bool may_supress);
334
335 #ifdef CONFIG_PRINTK
336 void console_prepend_dropped(struct printk_message *pmsg, unsigned long dropped);
337 void console_prepend_replay(struct printk_message *pmsg);
338 #endif
339
340 #ifdef CONFIG_SMP
341 bool is_printk_cpu_sync_owner(void);
342 #else
is_printk_cpu_sync_owner(void)343 static inline bool is_printk_cpu_sync_owner(void) { return false; }
344 #endif
345