xref: /linux/kernel/printk/internal.h (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
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 
66 __printf(4, 0)
67 int vprintk_store(int facility, int level,
68 		  const struct dev_printk_info *dev_info,
69 		  const char *fmt, va_list args);
70 
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);
73 
74 void __printk_safe_enter(void);
75 void __printk_safe_exit(void);
76 
77 bool printk_percpu_data_ready(void);
78 
79 #define printk_safe_enter_irqsave(flags)	\
80 	do {					\
81 		local_irq_save(flags);		\
82 		__printk_safe_enter();		\
83 	} while (0)
84 
85 #define printk_safe_exit_irqrestore(flags)	\
86 	do {					\
87 		__printk_safe_exit();		\
88 		local_irq_restore(flags);	\
89 	} while (0)
90 
91 void defer_console_output(void);
92 bool is_printk_legacy_deferred(void);
93 
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);
98 
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);
110 
111 /*
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
115  * records.
116  */
console_is_usable(struct console * con,short flags,bool use_atomic)117 static inline bool console_is_usable(struct console *con, short flags, bool use_atomic)
118 {
119 	if (!(flags & CON_ENABLED))
120 		return false;
121 
122 	if ((flags & CON_SUSPENDED))
123 		return false;
124 
125 	if (flags & CON_NBCON) {
126 		/* The write_atomic() callback is optional. */
127 		if (use_atomic && !con->write_atomic)
128 			return false;
129 
130 		/*
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
134 		 * available.
135 		 */
136 	} else {
137 		if (!con->write)
138 			return false;
139 	}
140 
141 	/*
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.
145 	 */
146 	if (!cpu_online(raw_smp_processor_id()) && !(flags & CON_ANYTIME))
147 		return false;
148 
149 	return true;
150 }
151 
152 /**
153  * nbcon_kthread_wake - Wake up a console printing thread
154  * @con:	Console to operate on
155  */
nbcon_kthread_wake(struct console * con)156 static inline void nbcon_kthread_wake(struct console *con)
157 {
158 	/*
159 	 * Guarantee any new records can be seen by tasks preparing to wait
160 	 * before this context checks if the rcuwait is empty.
161 	 *
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.
166 	 *
167 	 * This pairs with nbcon_kthread_func:A.
168 	 */
169 	rcuwait_wake_up(&con->rcuwait); /* LMM(nbcon_kthread_wake:A) */
170 }
171 
172 #else
173 
174 #define PRINTK_PREFIX_MAX	0
175 #define PRINTK_MESSAGE_MAX	0
176 #define PRINTKRB_RECORD_MAX	0
177 
178 #define printk_kthreads_running (false)
179 
180 /*
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.
184  */
185 #define printk_safe_enter_irqsave(flags) local_irq_save(flags)
186 #define printk_safe_exit_irqrestore(flags) local_irq_restore(flags)
187 
printk_percpu_data_ready(void)188 static inline bool printk_percpu_data_ready(void) { return false; }
defer_console_output(void)189 static inline void defer_console_output(void) { }
is_printk_legacy_deferred(void)190 static inline bool is_printk_legacy_deferred(void) { return false; }
nbcon_seq_read(struct console * con)191 static inline u64 nbcon_seq_read(struct console *con) { return 0; }
nbcon_seq_force(struct console * con,u64 seq)192 static inline void nbcon_seq_force(struct console *con, u64 seq) { }
nbcon_alloc(struct console * con)193 static inline bool nbcon_alloc(struct console *con) { return false; }
nbcon_free(struct console * con)194 static inline void nbcon_free(struct console *con) { }
nbcon_get_default_prio(void)195 static inline enum nbcon_prio nbcon_get_default_prio(void) { return NBCON_PRIO_NONE; }
nbcon_atomic_flush_pending(void)196 static inline void nbcon_atomic_flush_pending(void) { }
nbcon_legacy_emit_next_record(struct console * con,bool * handover,int cookie,bool use_atomic)197 static inline bool nbcon_legacy_emit_next_record(struct console *con, bool *handover,
198 						 int cookie, bool use_atomic) { return false; }
nbcon_kthread_wake(struct console * con)199 static inline void nbcon_kthread_wake(struct console *con) { }
nbcon_kthreads_wake(void)200 static inline void nbcon_kthreads_wake(void) { }
201 
console_is_usable(struct console * con,short flags,bool use_atomic)202 static inline bool console_is_usable(struct console *con, short flags,
203 				     bool use_atomic) { return false; }
204 
205 #endif /* CONFIG_PRINTK */
206 
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;
211 
212 /**
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
218  *
219  * Note that the legacy loop also flushes the nbcon consoles.
220  */
221 struct console_flush_type {
222 	bool	nbcon_atomic;
223 	bool	nbcon_offload;
224 	bool	legacy_direct;
225 	bool	legacy_offload;
226 };
227 
228 /*
229  * Identify which console flushing methods should be used in the context of
230  * the caller.
231  */
printk_get_console_flush_type(struct console_flush_type * ft)232 static inline void printk_get_console_flush_type(struct console_flush_type *ft)
233 {
234 	memset(ft, 0, sizeof(*ft));
235 
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;
241 			else
242 				ft->nbcon_atomic = true;
243 		}
244 
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;
249 			else
250 				ft->legacy_offload = true;
251 		}
252 		break;
253 
254 	case NBCON_PRIO_EMERGENCY:
255 		if (have_nbcon_console && !have_boot_console)
256 			ft->nbcon_atomic = true;
257 
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;
262 			else
263 				ft->legacy_offload = true;
264 		}
265 		break;
266 
267 	case NBCON_PRIO_PANIC:
268 		/*
269 		 * In panic, the nbcon consoles will directly print. But
270 		 * only allowed if there are no boot consoles.
271 		 */
272 		if (have_nbcon_console && !have_boot_console)
273 			ft->nbcon_atomic = true;
274 
275 		if (have_legacy_console || have_boot_console) {
276 			/*
277 			 * This is the same decision as NBCON_PRIO_NORMAL
278 			 * except that offloading never occurs in panic.
279 			 *
280 			 * Note that console_flush_on_panic() will flush
281 			 * legacy consoles anyway, even if unsafe.
282 			 */
283 			if (!is_printk_legacy_deferred())
284 				ft->legacy_direct = true;
285 
286 			/*
287 			 * In panic, if nbcon atomic printing occurs,
288 			 * the legacy consoles must remain silent until
289 			 * explicitly allowed.
290 			 */
291 			if (ft->nbcon_atomic && !legacy_allow_panic_sync)
292 				ft->legacy_direct = false;
293 		}
294 		break;
295 
296 	default:
297 		WARN_ON_ONCE(1);
298 		break;
299 	}
300 }
301 
302 extern struct printk_buffers printk_shared_pbufs;
303 
304 /**
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.
308  */
309 struct printk_buffers {
310 	char	outbuf[PRINTK_MESSAGE_MAX];
311 	char	scratchbuf[PRINTKRB_RECORD_MAX];
312 };
313 
314 /**
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.
322  */
323 struct printk_message {
324 	struct printk_buffers	*pbufs;
325 	unsigned int		outbuf_len;
326 	u64			seq;
327 	unsigned long		dropped;
328 };
329 
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);
333 
334 #ifdef CONFIG_PRINTK
335 void console_prepend_dropped(struct printk_message *pmsg, unsigned long dropped);
336 void console_prepend_replay(struct printk_message *pmsg);
337 #endif
338