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/types.h>
7
8 #if defined(CONFIG_PRINTK) && defined(CONFIG_SYSCTL)
9 struct ctl_table;
10 void __init printk_sysctl_init(void);
11 int devkmsg_sysctl_set_loglvl(const struct ctl_table *table, int write,
12 void *buffer, size_t *lenp, loff_t *ppos);
13 #else
14 #define printk_sysctl_init() do { } while (0)
15 #endif
16
17 #define con_printk(lvl, con, fmt, ...) \
18 printk(lvl pr_fmt("%s%sconsole [%s%d] " fmt), \
19 (con->flags & CON_NBCON) ? "" : "legacy ", \
20 (con->flags & CON_BOOT) ? "boot" : "", \
21 con->name, con->index, ##__VA_ARGS__)
22
23 /*
24 * Identify if legacy printing is forced in a dedicated kthread. If
25 * true, all printing via console lock occurs within a dedicated
26 * legacy printer thread. The only exception is on panic, after the
27 * nbcon consoles have had their chance to print the panic messages
28 * first.
29 */
30 #ifdef CONFIG_PREEMPT_RT
31 # define force_legacy_kthread() (true)
32 #else
33 # define force_legacy_kthread() (false)
34 #endif
35
36 #ifdef CONFIG_PRINTK
37
38 #ifdef CONFIG_PRINTK_CALLER
39 #define PRINTK_PREFIX_MAX 48
40 #else
41 #define PRINTK_PREFIX_MAX 32
42 #endif
43
44 /*
45 * the maximum size of a formatted record (i.e. with prefix added
46 * per line and dropped messages or in extended message format)
47 */
48 #define PRINTK_MESSAGE_MAX 2048
49
50 /* the maximum size allowed to be reserved for a record */
51 #define PRINTKRB_RECORD_MAX 1024
52
53 /* Flags for a single printk record. */
54 enum printk_info_flags {
55 /* always show on console, ignore console_loglevel */
56 LOG_FORCE_CON = 1,
57 LOG_NEWLINE = 2, /* text ended with a newline */
58 LOG_CONT = 8, /* text is a fragment of a continuation line */
59 };
60
61 struct printk_ringbuffer;
62 struct dev_printk_info;
63
64 extern struct printk_ringbuffer *prb;
65 extern bool printk_kthreads_running;
66 extern bool printk_kthreads_ready;
67 extern bool debug_non_panic_cpus;
68
69 __printf(4, 0)
70 int vprintk_store(int facility, int level,
71 const struct dev_printk_info *dev_info,
72 const char *fmt, va_list args);
73
74 __printf(1, 0) int vprintk_default(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 * nbcon_kthread_wake - Wake up a console printing thread
116 * @con: Console to operate on
117 */
nbcon_kthread_wake(struct console * con)118 static inline void nbcon_kthread_wake(struct console *con)
119 {
120 /*
121 * Guarantee any new records can be seen by tasks preparing to wait
122 * before this context checks if the rcuwait is empty.
123 *
124 * The full memory barrier in rcuwait_wake_up() pairs with the full
125 * memory barrier within set_current_state() of
126 * ___rcuwait_wait_event(), which is called after prepare_to_rcuwait()
127 * adds the waiter but before it has checked the wait condition.
128 *
129 * This pairs with nbcon_kthread_func:A.
130 */
131 rcuwait_wake_up(&con->rcuwait); /* LMM(nbcon_kthread_wake:A) */
132 }
133
134 #else
135
136 #define PRINTK_PREFIX_MAX 0
137 #define PRINTK_MESSAGE_MAX 0
138 #define PRINTKRB_RECORD_MAX 0
139
140 #define printk_kthreads_running (false)
141 #define printk_kthreads_ready (false)
142
143 /*
144 * In !PRINTK builds we still export console_sem
145 * semaphore and some of console functions (console_unlock()/etc.), so
146 * printk-safe must preserve the existing local IRQ guarantees.
147 */
148 #define printk_safe_enter_irqsave(flags) local_irq_save(flags)
149 #define printk_safe_exit_irqrestore(flags) local_irq_restore(flags)
150
printk_percpu_data_ready(void)151 static inline bool printk_percpu_data_ready(void) { return false; }
defer_console_output(void)152 static inline void defer_console_output(void) { }
is_printk_legacy_deferred(void)153 static inline bool is_printk_legacy_deferred(void) { return false; }
nbcon_seq_read(struct console * con)154 static inline u64 nbcon_seq_read(struct console *con) { return 0; }
nbcon_seq_force(struct console * con,u64 seq)155 static inline void nbcon_seq_force(struct console *con, u64 seq) { }
nbcon_alloc(struct console * con)156 static inline bool nbcon_alloc(struct console *con) { return false; }
nbcon_free(struct console * con)157 static inline void nbcon_free(struct console *con) { }
nbcon_get_default_prio(void)158 static inline enum nbcon_prio nbcon_get_default_prio(void) { return NBCON_PRIO_NONE; }
nbcon_atomic_flush_pending(void)159 static inline void nbcon_atomic_flush_pending(void) { }
nbcon_legacy_emit_next_record(struct console * con,bool * handover,int cookie,bool use_atomic)160 static inline bool nbcon_legacy_emit_next_record(struct console *con, bool *handover,
161 int cookie, bool use_atomic) { return false; }
nbcon_kthread_wake(struct console * con)162 static inline void nbcon_kthread_wake(struct console *con) { }
nbcon_kthreads_wake(void)163 static inline void nbcon_kthreads_wake(void) { }
164
165 #endif /* CONFIG_PRINTK */
166
167 extern bool have_boot_console;
168 extern bool have_nbcon_console;
169 extern bool have_legacy_console;
170 extern bool legacy_allow_panic_sync;
171
172 /**
173 * struct console_flush_type - Define available console flush methods
174 * @nbcon_atomic: Flush directly using nbcon_atomic() callback
175 * @nbcon_offload: Offload flush to printer thread
176 * @legacy_direct: Call the legacy loop in this context
177 * @legacy_offload: Offload the legacy loop into IRQ or legacy thread
178 *
179 * Note that the legacy loop also flushes the nbcon consoles.
180 */
181 struct console_flush_type {
182 bool nbcon_atomic;
183 bool nbcon_offload;
184 bool legacy_direct;
185 bool legacy_offload;
186 };
187
188 extern bool console_irqwork_blocked;
189
190 /*
191 * Identify which console flushing methods should be used in the context of
192 * the caller.
193 */
printk_get_console_flush_type(struct console_flush_type * ft)194 static inline void printk_get_console_flush_type(struct console_flush_type *ft)
195 {
196 memset(ft, 0, sizeof(*ft));
197
198 switch (nbcon_get_default_prio()) {
199 case NBCON_PRIO_NORMAL:
200 if (have_nbcon_console && !have_boot_console) {
201 if (printk_kthreads_running && !console_irqwork_blocked)
202 ft->nbcon_offload = true;
203 else
204 ft->nbcon_atomic = true;
205 }
206
207 /* Legacy consoles are flushed directly when possible. */
208 if (have_legacy_console || have_boot_console) {
209 if (!is_printk_legacy_deferred())
210 ft->legacy_direct = true;
211 else if (!console_irqwork_blocked)
212 ft->legacy_offload = true;
213 }
214 break;
215
216 case NBCON_PRIO_EMERGENCY:
217 if (have_nbcon_console && !have_boot_console)
218 ft->nbcon_atomic = true;
219
220 /* Legacy consoles are flushed directly when possible. */
221 if (have_legacy_console || have_boot_console) {
222 if (!is_printk_legacy_deferred())
223 ft->legacy_direct = true;
224 else if (!console_irqwork_blocked)
225 ft->legacy_offload = true;
226 }
227 break;
228
229 case NBCON_PRIO_PANIC:
230 /*
231 * In panic, the nbcon consoles will directly print. But
232 * only allowed if there are no boot consoles.
233 */
234 if (have_nbcon_console && !have_boot_console)
235 ft->nbcon_atomic = true;
236
237 if (have_legacy_console || have_boot_console) {
238 /*
239 * This is the same decision as NBCON_PRIO_NORMAL
240 * except that offloading never occurs in panic.
241 *
242 * Note that console_flush_on_panic() will flush
243 * legacy consoles anyway, even if unsafe.
244 */
245 if (!is_printk_legacy_deferred())
246 ft->legacy_direct = true;
247
248 /*
249 * In panic, if nbcon atomic printing occurs,
250 * the legacy consoles must remain silent until
251 * explicitly allowed.
252 */
253 if (ft->nbcon_atomic && !legacy_allow_panic_sync)
254 ft->legacy_direct = false;
255 }
256 break;
257
258 default:
259 WARN_ON_ONCE(1);
260 break;
261 }
262 }
263
264 extern struct printk_buffers printk_shared_pbufs;
265
266 /**
267 * struct printk_buffers - Buffers to read/format/output printk messages.
268 * @outbuf: After formatting, contains text to output.
269 * @scratchbuf: Used as temporary ringbuffer reading and string-print space.
270 */
271 struct printk_buffers {
272 char outbuf[PRINTK_MESSAGE_MAX];
273 char scratchbuf[PRINTKRB_RECORD_MAX];
274 };
275
276 /**
277 * struct printk_message - Container for a prepared printk message.
278 * @pbufs: printk buffers used to prepare the message.
279 * @outbuf_len: The length of prepared text in @pbufs->outbuf to output. This
280 * does not count the terminator. A value of 0 means there is
281 * nothing to output and this record should be skipped.
282 * @seq: The sequence number of the record used for @pbufs->outbuf.
283 * @dropped: The number of dropped records from reading @seq.
284 */
285 struct printk_message {
286 struct printk_buffers *pbufs;
287 unsigned int outbuf_len;
288 u64 seq;
289 unsigned long dropped;
290 };
291
292 bool printk_get_next_message(struct printk_message *pmsg, u64 seq,
293 bool is_extended, bool may_supress);
294
295 #ifdef CONFIG_PRINTK
296 void console_prepend_dropped(struct printk_message *pmsg, unsigned long dropped);
297 void console_prepend_replay(struct printk_message *pmsg);
298 #endif
299
300 #ifdef CONFIG_SMP
301 bool is_printk_cpu_sync_owner(void);
302 #else
is_printk_cpu_sync_owner(void)303 static inline bool is_printk_cpu_sync_owner(void) { return false; }
304 #endif
305