xref: /linux/kernel/printk/internal.h (revision 3fd6c59042dbba50391e30862beac979491145fe)
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 	/* 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 
62 struct printk_ringbuffer;
63 struct dev_printk_info;
64 
65 extern struct printk_ringbuffer *prb;
66 extern bool printk_kthreads_running;
67 
68 __printf(4, 0)
69 int 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 
76 void __printk_safe_enter(void);
77 void __printk_safe_exit(void);
78 
79 bool 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 
93 void defer_console_output(void);
94 bool is_printk_legacy_deferred(void);
95 bool is_printk_force_console(void);
96 
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);
101 
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);
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  */
console_is_usable(struct console * con,short flags,bool use_atomic)120 static 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  */
nbcon_kthread_wake(struct console * con)159 static 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 
printk_percpu_data_ready(void)191 static inline bool printk_percpu_data_ready(void) { return false; }
defer_console_output(void)192 static inline void defer_console_output(void) { }
is_printk_legacy_deferred(void)193 static inline bool is_printk_legacy_deferred(void) { return false; }
nbcon_seq_read(struct console * con)194 static inline u64 nbcon_seq_read(struct console *con) { return 0; }
nbcon_seq_force(struct console * con,u64 seq)195 static inline void nbcon_seq_force(struct console *con, u64 seq) { }
nbcon_alloc(struct console * con)196 static inline bool nbcon_alloc(struct console *con) { return false; }
nbcon_free(struct console * con)197 static inline void nbcon_free(struct console *con) { }
nbcon_get_default_prio(void)198 static inline enum nbcon_prio nbcon_get_default_prio(void) { return NBCON_PRIO_NONE; }
nbcon_atomic_flush_pending(void)199 static inline void nbcon_atomic_flush_pending(void) { }
nbcon_legacy_emit_next_record(struct console * con,bool * handover,int cookie,bool use_atomic)200 static inline bool nbcon_legacy_emit_next_record(struct console *con, bool *handover,
201 						 int cookie, bool use_atomic) { return false; }
nbcon_kthread_wake(struct console * con)202 static inline void nbcon_kthread_wake(struct console *con) { }
nbcon_kthreads_wake(void)203 static inline void nbcon_kthreads_wake(void) { }
204 
console_is_usable(struct console * con,short flags,bool use_atomic)205 static inline bool console_is_usable(struct console *con, short flags,
206 				     bool use_atomic) { return false; }
207 
208 #endif /* CONFIG_PRINTK */
209 
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;
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  */
224 struct 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  */
printk_get_console_flush_type(struct console_flush_type * ft)235 static 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 
305 extern 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  */
312 struct 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  */
326 struct printk_message {
327 	struct printk_buffers	*pbufs;
328 	unsigned int		outbuf_len;
329 	u64			seq;
330 	unsigned long		dropped;
331 };
332 
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);
336 
337 #ifdef CONFIG_PRINTK
338 void console_prepend_dropped(struct printk_message *pmsg, unsigned long dropped);
339 void console_prepend_replay(struct printk_message *pmsg);
340 #endif
341