]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/kernel/printk.c | |
3 | * | |
4 | * Copyright (C) 1991, 1992 Linus Torvalds | |
5 | * | |
6 | * Modified to make sys_syslog() more flexible: added commands to | |
7 | * return the last 4k of kernel messages, regardless of whether | |
8 | * they've been read or not. Added option to suppress kernel printk's | |
9 | * to the console. Added hook for sending the console messages | |
10 | * elsewhere, in preparation for a serial line console (someday). | |
11 | * Ted Ts'o, 2/11/93. | |
12 | * Modified for sysctl support, 1/8/97, Chris Horn. | |
40dc5651 | 13 | * Fixed SMP synchronization, 08/08/99, Manfred Spraul |
624dffcb | 14 | * manfred@colorfullife.com |
1da177e4 | 15 | * Rewrote bits to get rid of console_lock |
e1f8e874 | 16 | * 01Mar01 Andrew Morton |
1da177e4 LT |
17 | */ |
18 | ||
dd5adbfb HZ |
19 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
20 | ||
1da177e4 LT |
21 | #include <linux/kernel.h> |
22 | #include <linux/mm.h> | |
23 | #include <linux/tty.h> | |
24 | #include <linux/tty_driver.h> | |
1da177e4 LT |
25 | #include <linux/console.h> |
26 | #include <linux/init.h> | |
bfe8df3d RD |
27 | #include <linux/jiffies.h> |
28 | #include <linux/nmi.h> | |
1da177e4 | 29 | #include <linux/module.h> |
3b9c0410 | 30 | #include <linux/moduleparam.h> |
1da177e4 LT |
31 | #include <linux/delay.h> |
32 | #include <linux/smp.h> | |
33 | #include <linux/security.h> | |
34 | #include <linux/bootmem.h> | |
162a7e75 | 35 | #include <linux/memblock.h> |
1da177e4 | 36 | #include <linux/syscalls.h> |
692f66f2 | 37 | #include <linux/crash_core.h> |
d37d39ae | 38 | #include <linux/kdb.h> |
3fff4c42 | 39 | #include <linux/ratelimit.h> |
456b565c | 40 | #include <linux/kmsg_dump.h> |
00234592 | 41 | #include <linux/syslog.h> |
034260d6 | 42 | #include <linux/cpu.h> |
fb842b00 | 43 | #include <linux/rculist.h> |
e11fea92 | 44 | #include <linux/poll.h> |
74876a98 | 45 | #include <linux/irq_work.h> |
249771b8 | 46 | #include <linux/ctype.h> |
e2e40f2c | 47 | #include <linux/uio.h> |
e6017571 | 48 | #include <linux/sched/clock.h> |
b17b0153 | 49 | #include <linux/sched/debug.h> |
68db0cf1 | 50 | #include <linux/sched/task_stack.h> |
1da177e4 | 51 | |
7c0f6ba6 | 52 | #include <linux/uaccess.h> |
40a7d9f5 | 53 | #include <asm/sections.h> |
1da177e4 | 54 | |
58eacfff | 55 | #include <trace/events/initcall.h> |
95100358 JB |
56 | #define CREATE_TRACE_POINTS |
57 | #include <trace/events/printk.h> | |
58 | ||
d197c43d | 59 | #include "console_cmdline.h" |
bbeddf52 | 60 | #include "braille.h" |
42a0bb3f | 61 | #include "internal.h" |
d197c43d | 62 | |
1da177e4 | 63 | int console_printk[4] = { |
a8fe19eb | 64 | CONSOLE_LOGLEVEL_DEFAULT, /* console_loglevel */ |
42a9dc0b | 65 | MESSAGE_LOGLEVEL_DEFAULT, /* default_message_loglevel */ |
a8fe19eb BP |
66 | CONSOLE_LOGLEVEL_MIN, /* minimum_console_loglevel */ |
67 | CONSOLE_LOGLEVEL_DEFAULT, /* default_console_loglevel */ | |
1da177e4 LT |
68 | }; |
69 | ||
56e6c104 TZ |
70 | atomic_t ignore_console_lock_warning __read_mostly = ATOMIC_INIT(0); |
71 | EXPORT_SYMBOL(ignore_console_lock_warning); | |
72 | ||
1da177e4 | 73 | /* |
0bbfb7c2 | 74 | * Low level drivers may need that to know if they can schedule in |
1da177e4 LT |
75 | * their unblank() callback or not. So let's export it. |
76 | */ | |
77 | int oops_in_progress; | |
78 | EXPORT_SYMBOL(oops_in_progress); | |
79 | ||
80 | /* | |
81 | * console_sem protects the console_drivers list, and also | |
82 | * provides serialisation for access to the entire console | |
83 | * driver system. | |
84 | */ | |
5b8c4f23 | 85 | static DEFINE_SEMAPHORE(console_sem); |
1da177e4 | 86 | struct console *console_drivers; |
a29d1cfe IM |
87 | EXPORT_SYMBOL_GPL(console_drivers); |
88 | ||
daee7797 DV |
89 | #ifdef CONFIG_LOCKDEP |
90 | static struct lockdep_map console_lock_dep_map = { | |
91 | .name = "console_lock" | |
92 | }; | |
93 | #endif | |
94 | ||
750afe7b BP |
95 | enum devkmsg_log_bits { |
96 | __DEVKMSG_LOG_BIT_ON = 0, | |
97 | __DEVKMSG_LOG_BIT_OFF, | |
98 | __DEVKMSG_LOG_BIT_LOCK, | |
99 | }; | |
100 | ||
101 | enum devkmsg_log_masks { | |
102 | DEVKMSG_LOG_MASK_ON = BIT(__DEVKMSG_LOG_BIT_ON), | |
103 | DEVKMSG_LOG_MASK_OFF = BIT(__DEVKMSG_LOG_BIT_OFF), | |
104 | DEVKMSG_LOG_MASK_LOCK = BIT(__DEVKMSG_LOG_BIT_LOCK), | |
105 | }; | |
106 | ||
107 | /* Keep both the 'on' and 'off' bits clear, i.e. ratelimit by default: */ | |
108 | #define DEVKMSG_LOG_MASK_DEFAULT 0 | |
109 | ||
110 | static unsigned int __read_mostly devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT; | |
111 | ||
112 | static int __control_devkmsg(char *str) | |
113 | { | |
114 | if (!str) | |
115 | return -EINVAL; | |
116 | ||
117 | if (!strncmp(str, "on", 2)) { | |
118 | devkmsg_log = DEVKMSG_LOG_MASK_ON; | |
119 | return 2; | |
120 | } else if (!strncmp(str, "off", 3)) { | |
121 | devkmsg_log = DEVKMSG_LOG_MASK_OFF; | |
122 | return 3; | |
123 | } else if (!strncmp(str, "ratelimit", 9)) { | |
124 | devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT; | |
125 | return 9; | |
126 | } | |
127 | return -EINVAL; | |
128 | } | |
129 | ||
130 | static int __init control_devkmsg(char *str) | |
131 | { | |
132 | if (__control_devkmsg(str) < 0) | |
133 | return 1; | |
134 | ||
135 | /* | |
136 | * Set sysctl string accordingly: | |
137 | */ | |
6fd78a1a SS |
138 | if (devkmsg_log == DEVKMSG_LOG_MASK_ON) |
139 | strcpy(devkmsg_log_str, "on"); | |
140 | else if (devkmsg_log == DEVKMSG_LOG_MASK_OFF) | |
141 | strcpy(devkmsg_log_str, "off"); | |
750afe7b BP |
142 | /* else "ratelimit" which is set by default. */ |
143 | ||
144 | /* | |
145 | * Sysctl cannot change it anymore. The kernel command line setting of | |
146 | * this parameter is to force the setting to be permanent throughout the | |
147 | * runtime of the system. This is a precation measure against userspace | |
148 | * trying to be a smarta** and attempting to change it up on us. | |
149 | */ | |
150 | devkmsg_log |= DEVKMSG_LOG_MASK_LOCK; | |
151 | ||
152 | return 0; | |
153 | } | |
154 | __setup("printk.devkmsg=", control_devkmsg); | |
155 | ||
156 | char devkmsg_log_str[DEVKMSG_STR_MAX_SIZE] = "ratelimit"; | |
157 | ||
158 | int devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write, | |
159 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
160 | { | |
161 | char old_str[DEVKMSG_STR_MAX_SIZE]; | |
162 | unsigned int old; | |
163 | int err; | |
164 | ||
165 | if (write) { | |
166 | if (devkmsg_log & DEVKMSG_LOG_MASK_LOCK) | |
167 | return -EINVAL; | |
168 | ||
169 | old = devkmsg_log; | |
170 | strncpy(old_str, devkmsg_log_str, DEVKMSG_STR_MAX_SIZE); | |
171 | } | |
172 | ||
173 | err = proc_dostring(table, write, buffer, lenp, ppos); | |
174 | if (err) | |
175 | return err; | |
176 | ||
177 | if (write) { | |
178 | err = __control_devkmsg(devkmsg_log_str); | |
179 | ||
180 | /* | |
181 | * Do not accept an unknown string OR a known string with | |
182 | * trailing crap... | |
183 | */ | |
184 | if (err < 0 || (err + 1 != *lenp)) { | |
185 | ||
186 | /* ... and restore old setting. */ | |
187 | devkmsg_log = old; | |
188 | strncpy(devkmsg_log_str, old_str, DEVKMSG_STR_MAX_SIZE); | |
189 | ||
190 | return -EINVAL; | |
191 | } | |
192 | } | |
193 | ||
194 | return 0; | |
195 | } | |
196 | ||
9627808d | 197 | /* Number of registered extended console drivers. */ |
6fe29354 TH |
198 | static int nr_ext_console_drivers; |
199 | ||
bd8d7cf5 JK |
200 | /* |
201 | * Helper macros to handle lockdep when locking/unlocking console_sem. We use | |
202 | * macros instead of functions so that _RET_IP_ contains useful information. | |
203 | */ | |
204 | #define down_console_sem() do { \ | |
205 | down(&console_sem);\ | |
206 | mutex_acquire(&console_lock_dep_map, 0, 0, _RET_IP_);\ | |
207 | } while (0) | |
208 | ||
209 | static int __down_trylock_console_sem(unsigned long ip) | |
210 | { | |
f975237b SS |
211 | int lock_failed; |
212 | unsigned long flags; | |
213 | ||
214 | /* | |
215 | * Here and in __up_console_sem() we need to be in safe mode, | |
216 | * because spindump/WARN/etc from under console ->lock will | |
217 | * deadlock in printk()->down_trylock_console_sem() otherwise. | |
218 | */ | |
219 | printk_safe_enter_irqsave(flags); | |
220 | lock_failed = down_trylock(&console_sem); | |
221 | printk_safe_exit_irqrestore(flags); | |
222 | ||
223 | if (lock_failed) | |
bd8d7cf5 JK |
224 | return 1; |
225 | mutex_acquire(&console_lock_dep_map, 0, 1, ip); | |
226 | return 0; | |
227 | } | |
228 | #define down_trylock_console_sem() __down_trylock_console_sem(_RET_IP_) | |
229 | ||
f975237b SS |
230 | static void __up_console_sem(unsigned long ip) |
231 | { | |
232 | unsigned long flags; | |
233 | ||
234 | mutex_release(&console_lock_dep_map, 1, ip); | |
235 | ||
236 | printk_safe_enter_irqsave(flags); | |
237 | up(&console_sem); | |
238 | printk_safe_exit_irqrestore(flags); | |
239 | } | |
240 | #define up_console_sem() __up_console_sem(_RET_IP_) | |
bd8d7cf5 | 241 | |
1da177e4 LT |
242 | /* |
243 | * This is used for debugging the mess that is the VT code by | |
244 | * keeping track if we have the console semaphore held. It's | |
245 | * definitely not the perfect debug tool (we don't know if _WE_ | |
0b90fec3 AE |
246 | * hold it and are racing, but it helps tracking those weird code |
247 | * paths in the console code where we end up in places I want | |
248 | * locked without the console sempahore held). | |
1da177e4 | 249 | */ |
557240b4 | 250 | static int console_locked, console_suspended; |
1da177e4 | 251 | |
fe3d8ad3 FT |
252 | /* |
253 | * If exclusive_console is non-NULL then only this console is to be printed to. | |
254 | */ | |
255 | static struct console *exclusive_console; | |
256 | ||
1da177e4 LT |
257 | /* |
258 | * Array of consoles built from command line options (console=) | |
259 | */ | |
1da177e4 LT |
260 | |
261 | #define MAX_CMDLINECONSOLES 8 | |
262 | ||
263 | static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES]; | |
d197c43d | 264 | |
1da177e4 | 265 | static int preferred_console = -1; |
9e124fe1 MA |
266 | int console_set_on_cmdline; |
267 | EXPORT_SYMBOL(console_set_on_cmdline); | |
1da177e4 LT |
268 | |
269 | /* Flag: console code may call schedule() */ | |
270 | static int console_may_schedule; | |
271 | ||
cca10d58 SS |
272 | enum con_msg_format_flags { |
273 | MSG_FORMAT_DEFAULT = 0, | |
274 | MSG_FORMAT_SYSLOG = (1 << 0), | |
275 | }; | |
276 | ||
277 | static int console_msg_format = MSG_FORMAT_DEFAULT; | |
278 | ||
7ff9554b KS |
279 | /* |
280 | * The printk log buffer consists of a chain of concatenated variable | |
281 | * length records. Every record starts with a record header, containing | |
282 | * the overall length of the record. | |
283 | * | |
284 | * The heads to the first and last entry in the buffer, as well as the | |
0b90fec3 AE |
285 | * sequence numbers of these entries are maintained when messages are |
286 | * stored. | |
7ff9554b KS |
287 | * |
288 | * If the heads indicate available messages, the length in the header | |
289 | * tells the start next message. A length == 0 for the next message | |
290 | * indicates a wrap-around to the beginning of the buffer. | |
291 | * | |
292 | * Every record carries the monotonic timestamp in microseconds, as well as | |
293 | * the standard userspace syslog level and syslog facility. The usual | |
294 | * kernel messages use LOG_KERN; userspace-injected messages always carry | |
295 | * a matching syslog facility, by default LOG_USER. The origin of every | |
296 | * message can be reliably determined that way. | |
297 | * | |
298 | * The human readable log message directly follows the message header. The | |
299 | * length of the message text is stored in the header, the stored message | |
300 | * is not terminated. | |
301 | * | |
e11fea92 KS |
302 | * Optionally, a message can carry a dictionary of properties (key/value pairs), |
303 | * to provide userspace with a machine-readable message context. | |
304 | * | |
305 | * Examples for well-defined, commonly used property names are: | |
306 | * DEVICE=b12:8 device identifier | |
307 | * b12:8 block dev_t | |
308 | * c127:3 char dev_t | |
309 | * n8 netdev ifindex | |
310 | * +sound:card0 subsystem:devname | |
311 | * SUBSYSTEM=pci driver-core subsystem name | |
312 | * | |
313 | * Valid characters in property names are [a-zA-Z0-9.-_]. The plain text value | |
314 | * follows directly after a '=' character. Every property is terminated by | |
315 | * a '\0' character. The last property is not terminated. | |
316 | * | |
317 | * Example of a message structure: | |
318 | * 0000 ff 8f 00 00 00 00 00 00 monotonic time in nsec | |
319 | * 0008 34 00 record is 52 bytes long | |
320 | * 000a 0b 00 text is 11 bytes long | |
321 | * 000c 1f 00 dictionary is 23 bytes long | |
322 | * 000e 03 00 LOG_KERN (facility) LOG_ERR (level) | |
323 | * 0010 69 74 27 73 20 61 20 6c "it's a l" | |
324 | * 69 6e 65 "ine" | |
325 | * 001b 44 45 56 49 43 "DEVIC" | |
326 | * 45 3d 62 38 3a 32 00 44 "E=b8:2\0D" | |
327 | * 52 49 56 45 52 3d 62 75 "RIVER=bu" | |
328 | * 67 "g" | |
329 | * 0032 00 00 00 padding to next message header | |
330 | * | |
62e32ac3 | 331 | * The 'struct printk_log' buffer header must never be directly exported to |
e11fea92 KS |
332 | * userspace, it is a kernel-private implementation detail that might |
333 | * need to be changed in the future, when the requirements change. | |
334 | * | |
335 | * /dev/kmsg exports the structured data in the following line format: | |
b389645f AO |
336 | * "<level>,<sequnum>,<timestamp>,<contflag>[,additional_values, ... ];<message text>\n" |
337 | * | |
338 | * Users of the export format should ignore possible additional values | |
339 | * separated by ',', and find the message after the ';' character. | |
e11fea92 KS |
340 | * |
341 | * The optional key/value pairs are attached as continuation lines starting | |
342 | * with a space character and terminated by a newline. All possible | |
343 | * non-prinatable characters are escaped in the "\xff" notation. | |
7ff9554b KS |
344 | */ |
345 | ||
084681d1 | 346 | enum log_flags { |
5becfb1d KS |
347 | LOG_NEWLINE = 2, /* text ended with a newline */ |
348 | LOG_PREFIX = 4, /* text started with a prefix */ | |
349 | LOG_CONT = 8, /* text is a fragment of a continuation line */ | |
084681d1 KS |
350 | }; |
351 | ||
62e32ac3 | 352 | struct printk_log { |
7ff9554b KS |
353 | u64 ts_nsec; /* timestamp in nanoseconds */ |
354 | u16 len; /* length of entire record */ | |
355 | u16 text_len; /* length of text buffer */ | |
356 | u16 dict_len; /* length of dictionary buffer */ | |
084681d1 KS |
357 | u8 facility; /* syslog facility */ |
358 | u8 flags:5; /* internal record flags */ | |
359 | u8 level:3; /* syslog level */ | |
5c9cf8af AR |
360 | } |
361 | #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS | |
362 | __packed __aligned(4) | |
363 | #endif | |
364 | ; | |
7ff9554b KS |
365 | |
366 | /* | |
458df9fd SR |
367 | * The logbuf_lock protects kmsg buffer, indices, counters. This can be taken |
368 | * within the scheduler's rq lock. It must be released before calling | |
369 | * console_unlock() or anything else that might wake up a process. | |
7ff9554b | 370 | */ |
cf9b1106 | 371 | DEFINE_RAW_SPINLOCK(logbuf_lock); |
d59745ce | 372 | |
de6fcbdb SS |
373 | /* |
374 | * Helper macros to lock/unlock logbuf_lock and switch between | |
375 | * printk-safe/unsafe modes. | |
376 | */ | |
377 | #define logbuf_lock_irq() \ | |
378 | do { \ | |
379 | printk_safe_enter_irq(); \ | |
380 | raw_spin_lock(&logbuf_lock); \ | |
381 | } while (0) | |
382 | ||
383 | #define logbuf_unlock_irq() \ | |
384 | do { \ | |
385 | raw_spin_unlock(&logbuf_lock); \ | |
386 | printk_safe_exit_irq(); \ | |
387 | } while (0) | |
388 | ||
389 | #define logbuf_lock_irqsave(flags) \ | |
390 | do { \ | |
391 | printk_safe_enter_irqsave(flags); \ | |
392 | raw_spin_lock(&logbuf_lock); \ | |
393 | } while (0) | |
394 | ||
395 | #define logbuf_unlock_irqrestore(flags) \ | |
396 | do { \ | |
397 | raw_spin_unlock(&logbuf_lock); \ | |
398 | printk_safe_exit_irqrestore(flags); \ | |
399 | } while (0) | |
400 | ||
96efedf1 | 401 | #ifdef CONFIG_PRINTK |
dc72c32e | 402 | DECLARE_WAIT_QUEUE_HEAD(log_wait); |
7f3a781d KS |
403 | /* the next printk record to read by syslog(READ) or /proc/kmsg */ |
404 | static u64 syslog_seq; | |
405 | static u32 syslog_idx; | |
eb02dac9 | 406 | static size_t syslog_partial; |
e80c1a9d | 407 | static bool syslog_time; |
7ff9554b KS |
408 | |
409 | /* index and sequence number of the first record stored in the buffer */ | |
410 | static u64 log_first_seq; | |
411 | static u32 log_first_idx; | |
412 | ||
413 | /* index and sequence number of the next record to store in the buffer */ | |
414 | static u64 log_next_seq; | |
415 | static u32 log_next_idx; | |
416 | ||
eab07260 KS |
417 | /* the next printk record to write to the console */ |
418 | static u64 console_seq; | |
419 | static u32 console_idx; | |
f92b070f | 420 | static u64 exclusive_console_stop_seq; |
eab07260 | 421 | |
7ff9554b KS |
422 | /* the next printk record to read after the last 'clear' command */ |
423 | static u64 clear_seq; | |
424 | static u32 clear_idx; | |
425 | ||
70498253 | 426 | #define PREFIX_MAX 32 |
249771b8 | 427 | #define LOG_LINE_MAX (1024 - PREFIX_MAX) |
7f3a781d | 428 | |
3824657c MK |
429 | #define LOG_LEVEL(v) ((v) & 0x07) |
430 | #define LOG_FACILITY(v) ((v) >> 3 & 0xff) | |
431 | ||
7f3a781d | 432 | /* record buffer */ |
62e32ac3 | 433 | #define LOG_ALIGN __alignof__(struct printk_log) |
7f3a781d | 434 | #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT) |
e6fe3e5b | 435 | #define LOG_BUF_LEN_MAX (u32)(1 << 31) |
f8450fca | 436 | static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN); |
7f3a781d KS |
437 | static char *log_buf = __log_buf; |
438 | static u32 log_buf_len = __LOG_BUF_LEN; | |
439 | ||
14c4000a VH |
440 | /* Return log buffer address */ |
441 | char *log_buf_addr_get(void) | |
442 | { | |
443 | return log_buf; | |
444 | } | |
445 | ||
446 | /* Return log buffer size */ | |
447 | u32 log_buf_len_get(void) | |
448 | { | |
449 | return log_buf_len; | |
450 | } | |
451 | ||
7ff9554b | 452 | /* human readable text of the record */ |
62e32ac3 | 453 | static char *log_text(const struct printk_log *msg) |
7ff9554b | 454 | { |
62e32ac3 | 455 | return (char *)msg + sizeof(struct printk_log); |
7ff9554b KS |
456 | } |
457 | ||
458 | /* optional key/value pair dictionary attached to the record */ | |
62e32ac3 | 459 | static char *log_dict(const struct printk_log *msg) |
7ff9554b | 460 | { |
62e32ac3 | 461 | return (char *)msg + sizeof(struct printk_log) + msg->text_len; |
7ff9554b KS |
462 | } |
463 | ||
464 | /* get record by index; idx must point to valid msg */ | |
62e32ac3 | 465 | static struct printk_log *log_from_idx(u32 idx) |
7ff9554b | 466 | { |
62e32ac3 | 467 | struct printk_log *msg = (struct printk_log *)(log_buf + idx); |
7ff9554b KS |
468 | |
469 | /* | |
470 | * A length == 0 record is the end of buffer marker. Wrap around and | |
471 | * read the message at the start of the buffer. | |
472 | */ | |
473 | if (!msg->len) | |
62e32ac3 | 474 | return (struct printk_log *)log_buf; |
7ff9554b KS |
475 | return msg; |
476 | } | |
477 | ||
478 | /* get next record; idx must point to valid msg */ | |
479 | static u32 log_next(u32 idx) | |
480 | { | |
62e32ac3 | 481 | struct printk_log *msg = (struct printk_log *)(log_buf + idx); |
7ff9554b KS |
482 | |
483 | /* length == 0 indicates the end of the buffer; wrap */ | |
484 | /* | |
485 | * A length == 0 record is the end of buffer marker. Wrap around and | |
486 | * read the message at the start of the buffer as *this* one, and | |
487 | * return the one after that. | |
488 | */ | |
489 | if (!msg->len) { | |
62e32ac3 | 490 | msg = (struct printk_log *)log_buf; |
7ff9554b KS |
491 | return msg->len; |
492 | } | |
493 | return idx + msg->len; | |
494 | } | |
495 | ||
f40e4b9f PM |
496 | /* |
497 | * Check whether there is enough free space for the given message. | |
498 | * | |
499 | * The same values of first_idx and next_idx mean that the buffer | |
500 | * is either empty or full. | |
501 | * | |
502 | * If the buffer is empty, we must respect the position of the indexes. | |
503 | * They cannot be reset to the beginning of the buffer. | |
504 | */ | |
505 | static int logbuf_has_space(u32 msg_size, bool empty) | |
0a581694 PM |
506 | { |
507 | u32 free; | |
508 | ||
f40e4b9f | 509 | if (log_next_idx > log_first_idx || empty) |
0a581694 PM |
510 | free = max(log_buf_len - log_next_idx, log_first_idx); |
511 | else | |
512 | free = log_first_idx - log_next_idx; | |
513 | ||
514 | /* | |
515 | * We need space also for an empty header that signalizes wrapping | |
516 | * of the buffer. | |
517 | */ | |
518 | return free >= msg_size + sizeof(struct printk_log); | |
519 | } | |
520 | ||
f40e4b9f | 521 | static int log_make_free_space(u32 msg_size) |
0a581694 | 522 | { |
f468908b ID |
523 | while (log_first_seq < log_next_seq && |
524 | !logbuf_has_space(msg_size, false)) { | |
0b90fec3 | 525 | /* drop old messages until we have enough contiguous space */ |
0a581694 PM |
526 | log_first_idx = log_next(log_first_idx); |
527 | log_first_seq++; | |
528 | } | |
f40e4b9f | 529 | |
f468908b ID |
530 | if (clear_seq < log_first_seq) { |
531 | clear_seq = log_first_seq; | |
532 | clear_idx = log_first_idx; | |
533 | } | |
534 | ||
f40e4b9f | 535 | /* sequence numbers are equal, so the log buffer is empty */ |
f468908b | 536 | if (logbuf_has_space(msg_size, log_first_seq == log_next_seq)) |
f40e4b9f PM |
537 | return 0; |
538 | ||
539 | return -ENOMEM; | |
0a581694 PM |
540 | } |
541 | ||
85c87043 PM |
542 | /* compute the message size including the padding bytes */ |
543 | static u32 msg_used_size(u16 text_len, u16 dict_len, u32 *pad_len) | |
544 | { | |
545 | u32 size; | |
546 | ||
547 | size = sizeof(struct printk_log) + text_len + dict_len; | |
548 | *pad_len = (-size) & (LOG_ALIGN - 1); | |
549 | size += *pad_len; | |
550 | ||
551 | return size; | |
552 | } | |
553 | ||
55bd53a4 PM |
554 | /* |
555 | * Define how much of the log buffer we could take at maximum. The value | |
556 | * must be greater than two. Note that only half of the buffer is available | |
557 | * when the index points to the middle. | |
558 | */ | |
559 | #define MAX_LOG_TAKE_PART 4 | |
560 | static const char trunc_msg[] = "<truncated>"; | |
561 | ||
562 | static u32 truncate_msg(u16 *text_len, u16 *trunc_msg_len, | |
563 | u16 *dict_len, u32 *pad_len) | |
564 | { | |
565 | /* | |
566 | * The message should not take the whole buffer. Otherwise, it might | |
567 | * get removed too soon. | |
568 | */ | |
569 | u32 max_text_len = log_buf_len / MAX_LOG_TAKE_PART; | |
570 | if (*text_len > max_text_len) | |
571 | *text_len = max_text_len; | |
572 | /* enable the warning message */ | |
573 | *trunc_msg_len = strlen(trunc_msg); | |
574 | /* disable the "dict" completely */ | |
575 | *dict_len = 0; | |
576 | /* compute the size again, count also the warning message */ | |
577 | return msg_used_size(*text_len + *trunc_msg_len, 0, pad_len); | |
578 | } | |
579 | ||
7ff9554b | 580 | /* insert record into the buffer, discard old ones, update heads */ |
034633cc PM |
581 | static int log_store(int facility, int level, |
582 | enum log_flags flags, u64 ts_nsec, | |
583 | const char *dict, u16 dict_len, | |
584 | const char *text, u16 text_len) | |
7ff9554b | 585 | { |
62e32ac3 | 586 | struct printk_log *msg; |
7ff9554b | 587 | u32 size, pad_len; |
55bd53a4 | 588 | u16 trunc_msg_len = 0; |
7ff9554b KS |
589 | |
590 | /* number of '\0' padding bytes to next message */ | |
85c87043 | 591 | size = msg_used_size(text_len, dict_len, &pad_len); |
7ff9554b | 592 | |
55bd53a4 PM |
593 | if (log_make_free_space(size)) { |
594 | /* truncate the message if it is too long for empty buffer */ | |
595 | size = truncate_msg(&text_len, &trunc_msg_len, | |
596 | &dict_len, &pad_len); | |
597 | /* survive when the log buffer is too small for trunc_msg */ | |
598 | if (log_make_free_space(size)) | |
034633cc | 599 | return 0; |
55bd53a4 | 600 | } |
7ff9554b | 601 | |
39b25109 | 602 | if (log_next_idx + size + sizeof(struct printk_log) > log_buf_len) { |
7ff9554b KS |
603 | /* |
604 | * This message + an additional empty header does not fit | |
605 | * at the end of the buffer. Add an empty header with len == 0 | |
606 | * to signify a wrap around. | |
607 | */ | |
62e32ac3 | 608 | memset(log_buf + log_next_idx, 0, sizeof(struct printk_log)); |
7ff9554b KS |
609 | log_next_idx = 0; |
610 | } | |
611 | ||
612 | /* fill message */ | |
62e32ac3 | 613 | msg = (struct printk_log *)(log_buf + log_next_idx); |
7ff9554b KS |
614 | memcpy(log_text(msg), text, text_len); |
615 | msg->text_len = text_len; | |
55bd53a4 PM |
616 | if (trunc_msg_len) { |
617 | memcpy(log_text(msg) + text_len, trunc_msg, trunc_msg_len); | |
618 | msg->text_len += trunc_msg_len; | |
619 | } | |
7ff9554b KS |
620 | memcpy(log_dict(msg), dict, dict_len); |
621 | msg->dict_len = dict_len; | |
084681d1 KS |
622 | msg->facility = facility; |
623 | msg->level = level & 7; | |
624 | msg->flags = flags & 0x1f; | |
625 | if (ts_nsec > 0) | |
626 | msg->ts_nsec = ts_nsec; | |
627 | else | |
628 | msg->ts_nsec = local_clock(); | |
7ff9554b | 629 | memset(log_dict(msg) + dict_len, 0, pad_len); |
fce6e033 | 630 | msg->len = size; |
7ff9554b KS |
631 | |
632 | /* insert message */ | |
633 | log_next_idx += msg->len; | |
634 | log_next_seq++; | |
034633cc PM |
635 | |
636 | return msg->text_len; | |
7ff9554b | 637 | } |
d59745ce | 638 | |
e99aa461 | 639 | int dmesg_restrict = IS_ENABLED(CONFIG_SECURITY_DMESG_RESTRICT); |
637241a9 KC |
640 | |
641 | static int syslog_action_restricted(int type) | |
642 | { | |
643 | if (dmesg_restrict) | |
644 | return 1; | |
645 | /* | |
646 | * Unless restricted, we allow "read all" and "get buffer size" | |
647 | * for everybody. | |
648 | */ | |
649 | return type != SYSLOG_ACTION_READ_ALL && | |
650 | type != SYSLOG_ACTION_SIZE_BUFFER; | |
651 | } | |
652 | ||
c71b02e4 | 653 | static int check_syslog_permissions(int type, int source) |
637241a9 KC |
654 | { |
655 | /* | |
656 | * If this is from /proc/kmsg and we've already opened it, then we've | |
657 | * already done the capabilities checks at open time. | |
658 | */ | |
3ea4331c | 659 | if (source == SYSLOG_FROM_PROC && type != SYSLOG_ACTION_OPEN) |
d194e5d6 | 660 | goto ok; |
637241a9 KC |
661 | |
662 | if (syslog_action_restricted(type)) { | |
663 | if (capable(CAP_SYSLOG)) | |
d194e5d6 | 664 | goto ok; |
637241a9 KC |
665 | /* |
666 | * For historical reasons, accept CAP_SYS_ADMIN too, with | |
667 | * a warning. | |
668 | */ | |
669 | if (capable(CAP_SYS_ADMIN)) { | |
670 | pr_warn_once("%s (%d): Attempt to access syslog with " | |
671 | "CAP_SYS_ADMIN but no CAP_SYSLOG " | |
672 | "(deprecated).\n", | |
673 | current->comm, task_pid_nr(current)); | |
d194e5d6 | 674 | goto ok; |
637241a9 KC |
675 | } |
676 | return -EPERM; | |
677 | } | |
d194e5d6 | 678 | ok: |
637241a9 KC |
679 | return security_syslog(type); |
680 | } | |
681 | ||
d43ff430 TH |
682 | static void append_char(char **pp, char *e, char c) |
683 | { | |
684 | if (*pp < e) | |
685 | *(*pp)++ = c; | |
686 | } | |
637241a9 | 687 | |
0a295e67 | 688 | static ssize_t msg_print_ext_header(char *buf, size_t size, |
5aa068ea | 689 | struct printk_log *msg, u64 seq) |
0a295e67 TH |
690 | { |
691 | u64 ts_usec = msg->ts_nsec; | |
0a295e67 TH |
692 | |
693 | do_div(ts_usec, 1000); | |
694 | ||
0a295e67 | 695 | return scnprintf(buf, size, "%u,%llu,%llu,%c;", |
5aa068ea LT |
696 | (msg->facility << 3) | msg->level, seq, ts_usec, |
697 | msg->flags & LOG_CONT ? 'c' : '-'); | |
0a295e67 TH |
698 | } |
699 | ||
700 | static ssize_t msg_print_ext_body(char *buf, size_t size, | |
701 | char *dict, size_t dict_len, | |
702 | char *text, size_t text_len) | |
703 | { | |
704 | char *p = buf, *e = buf + size; | |
705 | size_t i; | |
706 | ||
707 | /* escape non-printable characters */ | |
708 | for (i = 0; i < text_len; i++) { | |
709 | unsigned char c = text[i]; | |
710 | ||
711 | if (c < ' ' || c >= 127 || c == '\\') | |
712 | p += scnprintf(p, e - p, "\\x%02x", c); | |
713 | else | |
714 | append_char(&p, e, c); | |
715 | } | |
716 | append_char(&p, e, '\n'); | |
717 | ||
718 | if (dict_len) { | |
719 | bool line = true; | |
720 | ||
721 | for (i = 0; i < dict_len; i++) { | |
722 | unsigned char c = dict[i]; | |
723 | ||
724 | if (line) { | |
725 | append_char(&p, e, ' '); | |
726 | line = false; | |
727 | } | |
728 | ||
729 | if (c == '\0') { | |
730 | append_char(&p, e, '\n'); | |
731 | line = true; | |
732 | continue; | |
733 | } | |
734 | ||
735 | if (c < ' ' || c >= 127 || c == '\\') { | |
736 | p += scnprintf(p, e - p, "\\x%02x", c); | |
737 | continue; | |
738 | } | |
739 | ||
740 | append_char(&p, e, c); | |
741 | } | |
742 | append_char(&p, e, '\n'); | |
743 | } | |
744 | ||
745 | return p - buf; | |
746 | } | |
747 | ||
e11fea92 KS |
748 | /* /dev/kmsg - userspace message inject/listen interface */ |
749 | struct devkmsg_user { | |
750 | u64 seq; | |
751 | u32 idx; | |
750afe7b | 752 | struct ratelimit_state rs; |
e11fea92 | 753 | struct mutex lock; |
d43ff430 | 754 | char buf[CONSOLE_EXT_LOG_MAX]; |
e11fea92 KS |
755 | }; |
756 | ||
9adcfaff TH |
757 | static __printf(3, 4) __cold |
758 | int devkmsg_emit(int facility, int level, const char *fmt, ...) | |
759 | { | |
760 | va_list args; | |
761 | int r; | |
762 | ||
763 | va_start(args, fmt); | |
764 | r = vprintk_emit(facility, level, NULL, 0, fmt, args); | |
765 | va_end(args); | |
766 | ||
767 | return r; | |
768 | } | |
769 | ||
849f3127 | 770 | static ssize_t devkmsg_write(struct kiocb *iocb, struct iov_iter *from) |
e11fea92 KS |
771 | { |
772 | char *buf, *line; | |
e11fea92 KS |
773 | int level = default_message_loglevel; |
774 | int facility = 1; /* LOG_USER */ | |
750afe7b BP |
775 | struct file *file = iocb->ki_filp; |
776 | struct devkmsg_user *user = file->private_data; | |
66ee59af | 777 | size_t len = iov_iter_count(from); |
e11fea92 KS |
778 | ssize_t ret = len; |
779 | ||
750afe7b | 780 | if (!user || len > LOG_LINE_MAX) |
e11fea92 | 781 | return -EINVAL; |
750afe7b BP |
782 | |
783 | /* Ignore when user logging is disabled. */ | |
784 | if (devkmsg_log & DEVKMSG_LOG_MASK_OFF) | |
785 | return len; | |
786 | ||
787 | /* Ratelimit when not explicitly enabled. */ | |
788 | if (!(devkmsg_log & DEVKMSG_LOG_MASK_ON)) { | |
789 | if (!___ratelimit(&user->rs, current->comm)) | |
790 | return ret; | |
791 | } | |
792 | ||
e11fea92 KS |
793 | buf = kmalloc(len+1, GFP_KERNEL); |
794 | if (buf == NULL) | |
795 | return -ENOMEM; | |
796 | ||
849f3127 | 797 | buf[len] = '\0'; |
cbbd26b8 | 798 | if (!copy_from_iter_full(buf, len, from)) { |
849f3127 AV |
799 | kfree(buf); |
800 | return -EFAULT; | |
e11fea92 KS |
801 | } |
802 | ||
803 | /* | |
804 | * Extract and skip the syslog prefix <[0-9]*>. Coming from userspace | |
805 | * the decimal value represents 32bit, the lower 3 bit are the log | |
806 | * level, the rest are the log facility. | |
807 | * | |
808 | * If no prefix or no userspace facility is specified, we | |
809 | * enforce LOG_USER, to be able to reliably distinguish | |
810 | * kernel-generated messages from userspace-injected ones. | |
811 | */ | |
812 | line = buf; | |
813 | if (line[0] == '<') { | |
814 | char *endp = NULL; | |
3824657c | 815 | unsigned int u; |
e11fea92 | 816 | |
3824657c | 817 | u = simple_strtoul(line + 1, &endp, 10); |
e11fea92 | 818 | if (endp && endp[0] == '>') { |
3824657c MK |
819 | level = LOG_LEVEL(u); |
820 | if (LOG_FACILITY(u) != 0) | |
821 | facility = LOG_FACILITY(u); | |
e11fea92 KS |
822 | endp++; |
823 | len -= endp - line; | |
824 | line = endp; | |
825 | } | |
826 | } | |
e11fea92 | 827 | |
9adcfaff | 828 | devkmsg_emit(facility, level, "%s", line); |
e11fea92 KS |
829 | kfree(buf); |
830 | return ret; | |
831 | } | |
832 | ||
833 | static ssize_t devkmsg_read(struct file *file, char __user *buf, | |
834 | size_t count, loff_t *ppos) | |
835 | { | |
836 | struct devkmsg_user *user = file->private_data; | |
62e32ac3 | 837 | struct printk_log *msg; |
e11fea92 KS |
838 | size_t len; |
839 | ssize_t ret; | |
840 | ||
841 | if (!user) | |
842 | return -EBADF; | |
843 | ||
4a77a5a0 YL |
844 | ret = mutex_lock_interruptible(&user->lock); |
845 | if (ret) | |
846 | return ret; | |
de6fcbdb SS |
847 | |
848 | logbuf_lock_irq(); | |
e11fea92 KS |
849 | while (user->seq == log_next_seq) { |
850 | if (file->f_flags & O_NONBLOCK) { | |
851 | ret = -EAGAIN; | |
de6fcbdb | 852 | logbuf_unlock_irq(); |
e11fea92 KS |
853 | goto out; |
854 | } | |
855 | ||
de6fcbdb | 856 | logbuf_unlock_irq(); |
e11fea92 KS |
857 | ret = wait_event_interruptible(log_wait, |
858 | user->seq != log_next_seq); | |
859 | if (ret) | |
860 | goto out; | |
de6fcbdb | 861 | logbuf_lock_irq(); |
e11fea92 KS |
862 | } |
863 | ||
864 | if (user->seq < log_first_seq) { | |
865 | /* our last seen message is gone, return error and reset */ | |
866 | user->idx = log_first_idx; | |
867 | user->seq = log_first_seq; | |
868 | ret = -EPIPE; | |
de6fcbdb | 869 | logbuf_unlock_irq(); |
e11fea92 KS |
870 | goto out; |
871 | } | |
872 | ||
873 | msg = log_from_idx(user->idx); | |
0a295e67 | 874 | len = msg_print_ext_header(user->buf, sizeof(user->buf), |
5aa068ea | 875 | msg, user->seq); |
0a295e67 TH |
876 | len += msg_print_ext_body(user->buf + len, sizeof(user->buf) - len, |
877 | log_dict(msg), msg->dict_len, | |
878 | log_text(msg), msg->text_len); | |
d39f3d77 | 879 | |
e11fea92 KS |
880 | user->idx = log_next(user->idx); |
881 | user->seq++; | |
de6fcbdb | 882 | logbuf_unlock_irq(); |
e11fea92 KS |
883 | |
884 | if (len > count) { | |
885 | ret = -EINVAL; | |
886 | goto out; | |
887 | } | |
888 | ||
889 | if (copy_to_user(buf, user->buf, len)) { | |
890 | ret = -EFAULT; | |
891 | goto out; | |
892 | } | |
893 | ret = len; | |
894 | out: | |
895 | mutex_unlock(&user->lock); | |
896 | return ret; | |
897 | } | |
898 | ||
899 | static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence) | |
900 | { | |
901 | struct devkmsg_user *user = file->private_data; | |
902 | loff_t ret = 0; | |
903 | ||
904 | if (!user) | |
905 | return -EBADF; | |
906 | if (offset) | |
907 | return -ESPIPE; | |
908 | ||
de6fcbdb | 909 | logbuf_lock_irq(); |
e11fea92 KS |
910 | switch (whence) { |
911 | case SEEK_SET: | |
912 | /* the first record */ | |
913 | user->idx = log_first_idx; | |
914 | user->seq = log_first_seq; | |
915 | break; | |
916 | case SEEK_DATA: | |
917 | /* | |
918 | * The first record after the last SYSLOG_ACTION_CLEAR, | |
919 | * like issued by 'dmesg -c'. Reading /dev/kmsg itself | |
920 | * changes no global state, and does not clear anything. | |
921 | */ | |
922 | user->idx = clear_idx; | |
923 | user->seq = clear_seq; | |
924 | break; | |
925 | case SEEK_END: | |
926 | /* after the last record */ | |
927 | user->idx = log_next_idx; | |
928 | user->seq = log_next_seq; | |
929 | break; | |
930 | default: | |
931 | ret = -EINVAL; | |
932 | } | |
de6fcbdb | 933 | logbuf_unlock_irq(); |
e11fea92 KS |
934 | return ret; |
935 | } | |
936 | ||
9dd95748 | 937 | static __poll_t devkmsg_poll(struct file *file, poll_table *wait) |
e11fea92 KS |
938 | { |
939 | struct devkmsg_user *user = file->private_data; | |
9dd95748 | 940 | __poll_t ret = 0; |
e11fea92 KS |
941 | |
942 | if (!user) | |
a9a08845 | 943 | return EPOLLERR|EPOLLNVAL; |
e11fea92 KS |
944 | |
945 | poll_wait(file, &log_wait, wait); | |
946 | ||
de6fcbdb | 947 | logbuf_lock_irq(); |
e11fea92 KS |
948 | if (user->seq < log_next_seq) { |
949 | /* return error when data has vanished underneath us */ | |
950 | if (user->seq < log_first_seq) | |
a9a08845 | 951 | ret = EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI; |
0a285317 | 952 | else |
a9a08845 | 953 | ret = EPOLLIN|EPOLLRDNORM; |
e11fea92 | 954 | } |
de6fcbdb | 955 | logbuf_unlock_irq(); |
e11fea92 KS |
956 | |
957 | return ret; | |
958 | } | |
959 | ||
960 | static int devkmsg_open(struct inode *inode, struct file *file) | |
961 | { | |
962 | struct devkmsg_user *user; | |
963 | int err; | |
964 | ||
750afe7b BP |
965 | if (devkmsg_log & DEVKMSG_LOG_MASK_OFF) |
966 | return -EPERM; | |
e11fea92 | 967 | |
750afe7b BP |
968 | /* write-only does not need any file context */ |
969 | if ((file->f_flags & O_ACCMODE) != O_WRONLY) { | |
970 | err = check_syslog_permissions(SYSLOG_ACTION_READ_ALL, | |
971 | SYSLOG_FROM_READER); | |
972 | if (err) | |
973 | return err; | |
974 | } | |
e11fea92 KS |
975 | |
976 | user = kmalloc(sizeof(struct devkmsg_user), GFP_KERNEL); | |
977 | if (!user) | |
978 | return -ENOMEM; | |
979 | ||
750afe7b BP |
980 | ratelimit_default_init(&user->rs); |
981 | ratelimit_set_flags(&user->rs, RATELIMIT_MSG_ON_RELEASE); | |
982 | ||
e11fea92 KS |
983 | mutex_init(&user->lock); |
984 | ||
de6fcbdb | 985 | logbuf_lock_irq(); |
e11fea92 KS |
986 | user->idx = log_first_idx; |
987 | user->seq = log_first_seq; | |
de6fcbdb | 988 | logbuf_unlock_irq(); |
e11fea92 KS |
989 | |
990 | file->private_data = user; | |
991 | return 0; | |
992 | } | |
993 | ||
994 | static int devkmsg_release(struct inode *inode, struct file *file) | |
995 | { | |
996 | struct devkmsg_user *user = file->private_data; | |
997 | ||
998 | if (!user) | |
999 | return 0; | |
1000 | ||
750afe7b BP |
1001 | ratelimit_state_exit(&user->rs); |
1002 | ||
e11fea92 KS |
1003 | mutex_destroy(&user->lock); |
1004 | kfree(user); | |
1005 | return 0; | |
1006 | } | |
1007 | ||
1008 | const struct file_operations kmsg_fops = { | |
1009 | .open = devkmsg_open, | |
1010 | .read = devkmsg_read, | |
849f3127 | 1011 | .write_iter = devkmsg_write, |
e11fea92 KS |
1012 | .llseek = devkmsg_llseek, |
1013 | .poll = devkmsg_poll, | |
1014 | .release = devkmsg_release, | |
1015 | }; | |
1016 | ||
692f66f2 | 1017 | #ifdef CONFIG_CRASH_CORE |
04d491ab | 1018 | /* |
4c1ace64 | 1019 | * This appends the listed symbols to /proc/vmcore |
04d491ab | 1020 | * |
4c1ace64 | 1021 | * /proc/vmcore is used by various utilities, like crash and makedumpfile to |
04d491ab NH |
1022 | * obtain access to symbols that are otherwise very difficult to locate. These |
1023 | * symbols are specifically used so that utilities can access and extract the | |
1024 | * dmesg log from a vmcore file after a crash. | |
1025 | */ | |
692f66f2 | 1026 | void log_buf_vmcoreinfo_setup(void) |
04d491ab NH |
1027 | { |
1028 | VMCOREINFO_SYMBOL(log_buf); | |
04d491ab | 1029 | VMCOREINFO_SYMBOL(log_buf_len); |
7ff9554b | 1030 | VMCOREINFO_SYMBOL(log_first_idx); |
f468908b | 1031 | VMCOREINFO_SYMBOL(clear_idx); |
7ff9554b | 1032 | VMCOREINFO_SYMBOL(log_next_idx); |
6791457a | 1033 | /* |
62e32ac3 | 1034 | * Export struct printk_log size and field offsets. User space tools can |
6791457a VG |
1035 | * parse it and detect any changes to structure down the line. |
1036 | */ | |
62e32ac3 JP |
1037 | VMCOREINFO_STRUCT_SIZE(printk_log); |
1038 | VMCOREINFO_OFFSET(printk_log, ts_nsec); | |
1039 | VMCOREINFO_OFFSET(printk_log, len); | |
1040 | VMCOREINFO_OFFSET(printk_log, text_len); | |
1041 | VMCOREINFO_OFFSET(printk_log, dict_len); | |
04d491ab NH |
1042 | } |
1043 | #endif | |
1044 | ||
162a7e75 MT |
1045 | /* requested log_buf_len from kernel cmdline */ |
1046 | static unsigned long __initdata new_log_buf_len; | |
1047 | ||
c0a318a3 | 1048 | /* we practice scaling the ring buffer by powers of 2 */ |
e6fe3e5b | 1049 | static void __init log_buf_len_update(u64 size) |
1da177e4 | 1050 | { |
e6fe3e5b HZ |
1051 | if (size > (u64)LOG_BUF_LEN_MAX) { |
1052 | size = (u64)LOG_BUF_LEN_MAX; | |
1053 | pr_err("log_buf over 2G is not supported.\n"); | |
1054 | } | |
1055 | ||
1da177e4 LT |
1056 | if (size) |
1057 | size = roundup_pow_of_two(size); | |
162a7e75 | 1058 | if (size > log_buf_len) |
e6fe3e5b | 1059 | new_log_buf_len = (unsigned long)size; |
c0a318a3 LR |
1060 | } |
1061 | ||
1062 | /* save requested log_buf_len since it's too early to process it */ | |
1063 | static int __init log_buf_len_setup(char *str) | |
1064 | { | |
e6fe3e5b | 1065 | u64 size; |
277fcdb2 HZ |
1066 | |
1067 | if (!str) | |
1068 | return -EINVAL; | |
1069 | ||
1070 | size = memparse(str, &str); | |
c0a318a3 LR |
1071 | |
1072 | log_buf_len_update(size); | |
162a7e75 MT |
1073 | |
1074 | return 0; | |
1da177e4 | 1075 | } |
162a7e75 MT |
1076 | early_param("log_buf_len", log_buf_len_setup); |
1077 | ||
2240a31d GU |
1078 | #ifdef CONFIG_SMP |
1079 | #define __LOG_CPU_MAX_BUF_LEN (1 << CONFIG_LOG_CPU_MAX_BUF_SHIFT) | |
1080 | ||
23b2899f LR |
1081 | static void __init log_buf_add_cpu(void) |
1082 | { | |
1083 | unsigned int cpu_extra; | |
1084 | ||
1085 | /* | |
1086 | * archs should set up cpu_possible_bits properly with | |
1087 | * set_cpu_possible() after setup_arch() but just in | |
1088 | * case lets ensure this is valid. | |
1089 | */ | |
1090 | if (num_possible_cpus() == 1) | |
1091 | return; | |
1092 | ||
1093 | cpu_extra = (num_possible_cpus() - 1) * __LOG_CPU_MAX_BUF_LEN; | |
1094 | ||
1095 | /* by default this will only continue through for large > 64 CPUs */ | |
1096 | if (cpu_extra <= __LOG_BUF_LEN / 2) | |
1097 | return; | |
1098 | ||
1099 | pr_info("log_buf_len individual max cpu contribution: %d bytes\n", | |
1100 | __LOG_CPU_MAX_BUF_LEN); | |
1101 | pr_info("log_buf_len total cpu_extra contributions: %d bytes\n", | |
1102 | cpu_extra); | |
1103 | pr_info("log_buf_len min size: %d bytes\n", __LOG_BUF_LEN); | |
1104 | ||
1105 | log_buf_len_update(cpu_extra + __LOG_BUF_LEN); | |
1106 | } | |
2240a31d GU |
1107 | #else /* !CONFIG_SMP */ |
1108 | static inline void log_buf_add_cpu(void) {} | |
1109 | #endif /* CONFIG_SMP */ | |
23b2899f | 1110 | |
162a7e75 MT |
1111 | void __init setup_log_buf(int early) |
1112 | { | |
1113 | unsigned long flags; | |
162a7e75 | 1114 | char *new_log_buf; |
d2130e82 | 1115 | unsigned int free; |
162a7e75 | 1116 | |
23b2899f LR |
1117 | if (log_buf != __log_buf) |
1118 | return; | |
1119 | ||
1120 | if (!early && !new_log_buf_len) | |
1121 | log_buf_add_cpu(); | |
1122 | ||
162a7e75 MT |
1123 | if (!new_log_buf_len) |
1124 | return; | |
1da177e4 | 1125 | |
162a7e75 | 1126 | if (early) { |
9da791df | 1127 | new_log_buf = |
70300177 | 1128 | memblock_virt_alloc(new_log_buf_len, LOG_ALIGN); |
162a7e75 | 1129 | } else { |
70300177 LR |
1130 | new_log_buf = memblock_virt_alloc_nopanic(new_log_buf_len, |
1131 | LOG_ALIGN); | |
162a7e75 MT |
1132 | } |
1133 | ||
1134 | if (unlikely(!new_log_buf)) { | |
e6fe3e5b | 1135 | pr_err("log_buf_len: %lu bytes not available\n", |
162a7e75 MT |
1136 | new_log_buf_len); |
1137 | return; | |
1138 | } | |
1139 | ||
de6fcbdb | 1140 | logbuf_lock_irqsave(flags); |
162a7e75 MT |
1141 | log_buf_len = new_log_buf_len; |
1142 | log_buf = new_log_buf; | |
1143 | new_log_buf_len = 0; | |
7ff9554b KS |
1144 | free = __LOG_BUF_LEN - log_next_idx; |
1145 | memcpy(log_buf, __log_buf, __LOG_BUF_LEN); | |
de6fcbdb | 1146 | logbuf_unlock_irqrestore(flags); |
162a7e75 | 1147 | |
e6fe3e5b HZ |
1148 | pr_info("log_buf_len: %u bytes\n", log_buf_len); |
1149 | pr_info("early log buf free: %u(%u%%)\n", | |
162a7e75 MT |
1150 | free, (free * 100) / __LOG_BUF_LEN); |
1151 | } | |
1da177e4 | 1152 | |
2fa72c8f AC |
1153 | static bool __read_mostly ignore_loglevel; |
1154 | ||
1155 | static int __init ignore_loglevel_setup(char *str) | |
1156 | { | |
d25d9fec | 1157 | ignore_loglevel = true; |
27083bac | 1158 | pr_info("debug: ignoring loglevel setting.\n"); |
2fa72c8f AC |
1159 | |
1160 | return 0; | |
1161 | } | |
1162 | ||
1163 | early_param("ignore_loglevel", ignore_loglevel_setup); | |
1164 | module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR); | |
205bd3d2 JP |
1165 | MODULE_PARM_DESC(ignore_loglevel, |
1166 | "ignore loglevel setting (prints all kernel messages to the console)"); | |
2fa72c8f | 1167 | |
cf775444 SS |
1168 | static bool suppress_message_printing(int level) |
1169 | { | |
1170 | return (level >= console_loglevel && !ignore_loglevel); | |
1171 | } | |
1172 | ||
bfe8df3d RD |
1173 | #ifdef CONFIG_BOOT_PRINTK_DELAY |
1174 | ||
674dff65 | 1175 | static int boot_delay; /* msecs delay after each printk during bootup */ |
3a3b6ed2 | 1176 | static unsigned long long loops_per_msec; /* based on boot_delay */ |
bfe8df3d RD |
1177 | |
1178 | static int __init boot_delay_setup(char *str) | |
1179 | { | |
1180 | unsigned long lpj; | |
bfe8df3d RD |
1181 | |
1182 | lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */ | |
1183 | loops_per_msec = (unsigned long long)lpj / 1000 * HZ; | |
1184 | ||
1185 | get_option(&str, &boot_delay); | |
1186 | if (boot_delay > 10 * 1000) | |
1187 | boot_delay = 0; | |
1188 | ||
3a3b6ed2 DY |
1189 | pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, " |
1190 | "HZ: %d, loops_per_msec: %llu\n", | |
1191 | boot_delay, preset_lpj, lpj, HZ, loops_per_msec); | |
29e9d225 | 1192 | return 0; |
bfe8df3d | 1193 | } |
29e9d225 | 1194 | early_param("boot_delay", boot_delay_setup); |
bfe8df3d | 1195 | |
2fa72c8f | 1196 | static void boot_delay_msec(int level) |
bfe8df3d RD |
1197 | { |
1198 | unsigned long long k; | |
1199 | unsigned long timeout; | |
1200 | ||
ff48cd26 | 1201 | if ((boot_delay == 0 || system_state >= SYSTEM_RUNNING) |
cf775444 | 1202 | || suppress_message_printing(level)) { |
bfe8df3d | 1203 | return; |
2fa72c8f | 1204 | } |
bfe8df3d | 1205 | |
3a3b6ed2 | 1206 | k = (unsigned long long)loops_per_msec * boot_delay; |
bfe8df3d RD |
1207 | |
1208 | timeout = jiffies + msecs_to_jiffies(boot_delay); | |
1209 | while (k) { | |
1210 | k--; | |
1211 | cpu_relax(); | |
1212 | /* | |
1213 | * use (volatile) jiffies to prevent | |
1214 | * compiler reduction; loop termination via jiffies | |
1215 | * is secondary and may or may not happen. | |
1216 | */ | |
1217 | if (time_after(jiffies, timeout)) | |
1218 | break; | |
1219 | touch_nmi_watchdog(); | |
1220 | } | |
1221 | } | |
1222 | #else | |
2fa72c8f | 1223 | static inline void boot_delay_msec(int level) |
bfe8df3d RD |
1224 | { |
1225 | } | |
1226 | #endif | |
1227 | ||
e99aa461 | 1228 | static bool printk_time = IS_ENABLED(CONFIG_PRINTK_TIME); |
7ff9554b KS |
1229 | module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR); |
1230 | ||
084681d1 KS |
1231 | static size_t print_time(u64 ts, char *buf) |
1232 | { | |
e80c1a9d | 1233 | unsigned long rem_nsec = do_div(ts, 1000000000); |
35dac27c | 1234 | |
084681d1 | 1235 | if (!buf) |
35dac27c | 1236 | return snprintf(NULL, 0, "[%5lu.000000] ", (unsigned long)ts); |
084681d1 | 1237 | |
084681d1 KS |
1238 | return sprintf(buf, "[%5lu.%06lu] ", |
1239 | (unsigned long)ts, rem_nsec / 1000); | |
1240 | } | |
1241 | ||
e80c1a9d TH |
1242 | static size_t print_prefix(const struct printk_log *msg, bool syslog, |
1243 | bool time, char *buf) | |
649e6ee3 | 1244 | { |
3ce9a7c0 | 1245 | size_t len = 0; |
43a73a50 | 1246 | unsigned int prefix = (msg->facility << 3) | msg->level; |
649e6ee3 | 1247 | |
3ce9a7c0 KS |
1248 | if (syslog) { |
1249 | if (buf) { | |
43a73a50 | 1250 | len += sprintf(buf, "<%u>", prefix); |
3ce9a7c0 KS |
1251 | } else { |
1252 | len += 3; | |
43a73a50 KS |
1253 | if (prefix > 999) |
1254 | len += 3; | |
1255 | else if (prefix > 99) | |
1256 | len += 2; | |
1257 | else if (prefix > 9) | |
3ce9a7c0 KS |
1258 | len++; |
1259 | } | |
1260 | } | |
649e6ee3 | 1261 | |
e80c1a9d TH |
1262 | if (time) |
1263 | len += print_time(msg->ts_nsec, buf ? buf + len : NULL); | |
3ce9a7c0 | 1264 | return len; |
649e6ee3 KS |
1265 | } |
1266 | ||
e80c1a9d TH |
1267 | static size_t msg_print_text(const struct printk_log *msg, bool syslog, |
1268 | bool time, char *buf, size_t size) | |
7ff9554b | 1269 | { |
3ce9a7c0 KS |
1270 | const char *text = log_text(msg); |
1271 | size_t text_size = msg->text_len; | |
1272 | size_t len = 0; | |
1273 | ||
1274 | do { | |
1275 | const char *next = memchr(text, '\n', text_size); | |
1276 | size_t text_len; | |
1277 | ||
1278 | if (next) { | |
1279 | text_len = next - text; | |
1280 | next++; | |
1281 | text_size -= next - text; | |
1282 | } else { | |
1283 | text_len = text_size; | |
1284 | } | |
7ff9554b | 1285 | |
3ce9a7c0 | 1286 | if (buf) { |
e80c1a9d | 1287 | if (print_prefix(msg, syslog, time, NULL) + |
70498253 | 1288 | text_len + 1 >= size - len) |
3ce9a7c0 | 1289 | break; |
7ff9554b | 1290 | |
e80c1a9d | 1291 | len += print_prefix(msg, syslog, time, buf + len); |
3ce9a7c0 KS |
1292 | memcpy(buf + len, text, text_len); |
1293 | len += text_len; | |
5aa068ea | 1294 | buf[len++] = '\n'; |
3ce9a7c0 KS |
1295 | } else { |
1296 | /* SYSLOG_ACTION_* buffer size only calculation */ | |
e80c1a9d | 1297 | len += print_prefix(msg, syslog, time, NULL); |
5becfb1d | 1298 | len += text_len; |
5aa068ea | 1299 | len++; |
3ce9a7c0 | 1300 | } |
7ff9554b | 1301 | |
3ce9a7c0 KS |
1302 | text = next; |
1303 | } while (text); | |
7ff9554b | 1304 | |
7ff9554b KS |
1305 | return len; |
1306 | } | |
1307 | ||
1308 | static int syslog_print(char __user *buf, int size) | |
1309 | { | |
1310 | char *text; | |
62e32ac3 | 1311 | struct printk_log *msg; |
116e90b2 | 1312 | int len = 0; |
7ff9554b | 1313 | |
70498253 | 1314 | text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL); |
7ff9554b KS |
1315 | if (!text) |
1316 | return -ENOMEM; | |
1317 | ||
116e90b2 JB |
1318 | while (size > 0) { |
1319 | size_t n; | |
eb02dac9 | 1320 | size_t skip; |
116e90b2 | 1321 | |
de6fcbdb | 1322 | logbuf_lock_irq(); |
116e90b2 JB |
1323 | if (syslog_seq < log_first_seq) { |
1324 | /* messages are gone, move to first one */ | |
1325 | syslog_seq = log_first_seq; | |
1326 | syslog_idx = log_first_idx; | |
eb02dac9 | 1327 | syslog_partial = 0; |
116e90b2 JB |
1328 | } |
1329 | if (syslog_seq == log_next_seq) { | |
de6fcbdb | 1330 | logbuf_unlock_irq(); |
116e90b2 JB |
1331 | break; |
1332 | } | |
eb02dac9 | 1333 | |
e80c1a9d TH |
1334 | /* |
1335 | * To keep reading/counting partial line consistent, | |
1336 | * use printk_time value as of the beginning of a line. | |
1337 | */ | |
1338 | if (!syslog_partial) | |
1339 | syslog_time = printk_time; | |
1340 | ||
eb02dac9 | 1341 | skip = syslog_partial; |
116e90b2 | 1342 | msg = log_from_idx(syslog_idx); |
e80c1a9d TH |
1343 | n = msg_print_text(msg, true, syslog_time, text, |
1344 | LOG_LINE_MAX + PREFIX_MAX); | |
eb02dac9 KS |
1345 | if (n - syslog_partial <= size) { |
1346 | /* message fits into buffer, move forward */ | |
116e90b2 JB |
1347 | syslog_idx = log_next(syslog_idx); |
1348 | syslog_seq++; | |
eb02dac9 KS |
1349 | n -= syslog_partial; |
1350 | syslog_partial = 0; | |
1351 | } else if (!len){ | |
1352 | /* partial read(), remember position */ | |
1353 | n = size; | |
1354 | syslog_partial += n; | |
116e90b2 JB |
1355 | } else |
1356 | n = 0; | |
de6fcbdb | 1357 | logbuf_unlock_irq(); |
116e90b2 JB |
1358 | |
1359 | if (!n) | |
1360 | break; | |
1361 | ||
eb02dac9 | 1362 | if (copy_to_user(buf, text + skip, n)) { |
116e90b2 JB |
1363 | if (!len) |
1364 | len = -EFAULT; | |
1365 | break; | |
1366 | } | |
eb02dac9 KS |
1367 | |
1368 | len += n; | |
1369 | size -= n; | |
1370 | buf += n; | |
7ff9554b | 1371 | } |
7ff9554b KS |
1372 | |
1373 | kfree(text); | |
1374 | return len; | |
1375 | } | |
1376 | ||
1377 | static int syslog_print_all(char __user *buf, int size, bool clear) | |
1378 | { | |
1379 | char *text; | |
1380 | int len = 0; | |
63842c21 NG |
1381 | u64 next_seq; |
1382 | u64 seq; | |
1383 | u32 idx; | |
e80c1a9d | 1384 | bool time; |
63842c21 | 1385 | |
70498253 | 1386 | text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL); |
7ff9554b KS |
1387 | if (!text) |
1388 | return -ENOMEM; | |
1389 | ||
e80c1a9d | 1390 | time = printk_time; |
de6fcbdb | 1391 | logbuf_lock_irq(); |
63842c21 NG |
1392 | /* |
1393 | * Find first record that fits, including all following records, | |
1394 | * into the user-provided buffer for this dump. | |
1395 | */ | |
1396 | seq = clear_seq; | |
1397 | idx = clear_idx; | |
1398 | while (seq < log_next_seq) { | |
1399 | struct printk_log *msg = log_from_idx(idx); | |
1400 | ||
e80c1a9d | 1401 | len += msg_print_text(msg, true, time, NULL, 0); |
63842c21 NG |
1402 | idx = log_next(idx); |
1403 | seq++; | |
1404 | } | |
e2ae715d | 1405 | |
63842c21 NG |
1406 | /* move first record forward until length fits into the buffer */ |
1407 | seq = clear_seq; | |
1408 | idx = clear_idx; | |
1409 | while (len > size && seq < log_next_seq) { | |
1410 | struct printk_log *msg = log_from_idx(idx); | |
3ce9a7c0 | 1411 | |
e80c1a9d | 1412 | len -= msg_print_text(msg, true, time, NULL, 0); |
63842c21 NG |
1413 | idx = log_next(idx); |
1414 | seq++; | |
1415 | } | |
7ff9554b | 1416 | |
63842c21 NG |
1417 | /* last message fitting into this dump */ |
1418 | next_seq = log_next_seq; | |
7ff9554b | 1419 | |
63842c21 NG |
1420 | len = 0; |
1421 | while (len >= 0 && seq < next_seq) { | |
1422 | struct printk_log *msg = log_from_idx(idx); | |
e80c1a9d TH |
1423 | int textlen = msg_print_text(msg, true, time, text, |
1424 | LOG_LINE_MAX + PREFIX_MAX); | |
7ff9554b | 1425 | |
63842c21 NG |
1426 | idx = log_next(idx); |
1427 | seq++; | |
7ff9554b | 1428 | |
63842c21 NG |
1429 | logbuf_unlock_irq(); |
1430 | if (copy_to_user(buf + len, text, textlen)) | |
1431 | len = -EFAULT; | |
1432 | else | |
1433 | len += textlen; | |
1434 | logbuf_lock_irq(); | |
7ff9554b | 1435 | |
63842c21 NG |
1436 | if (seq < log_first_seq) { |
1437 | /* messages are gone, move to next one */ | |
1438 | seq = log_first_seq; | |
1439 | idx = log_first_idx; | |
7ff9554b KS |
1440 | } |
1441 | } | |
1442 | ||
1443 | if (clear) { | |
1444 | clear_seq = log_next_seq; | |
1445 | clear_idx = log_next_idx; | |
1446 | } | |
de6fcbdb | 1447 | logbuf_unlock_irq(); |
7ff9554b KS |
1448 | |
1449 | kfree(text); | |
1450 | return len; | |
1451 | } | |
1452 | ||
8599dc7d PM |
1453 | static void syslog_clear(void) |
1454 | { | |
1455 | logbuf_lock_irq(); | |
1456 | clear_seq = log_next_seq; | |
1457 | clear_idx = log_next_idx; | |
1458 | logbuf_unlock_irq(); | |
1459 | } | |
1460 | ||
3ea4331c | 1461 | int do_syslog(int type, char __user *buf, int len, int source) |
1da177e4 | 1462 | { |
7ff9554b | 1463 | bool clear = false; |
a39d4a85 | 1464 | static int saved_console_loglevel = LOGLEVEL_DEFAULT; |
ee24aebf | 1465 | int error; |
1da177e4 | 1466 | |
3ea4331c | 1467 | error = check_syslog_permissions(type, source); |
ee24aebf | 1468 | if (error) |
077a1cc0 | 1469 | return error; |
12b3052c | 1470 | |
1da177e4 | 1471 | switch (type) { |
d78ca3cd | 1472 | case SYSLOG_ACTION_CLOSE: /* Close log */ |
1da177e4 | 1473 | break; |
d78ca3cd | 1474 | case SYSLOG_ACTION_OPEN: /* Open log */ |
1da177e4 | 1475 | break; |
d78ca3cd | 1476 | case SYSLOG_ACTION_READ: /* Read from log */ |
1da177e4 | 1477 | if (!buf || len < 0) |
077a1cc0 | 1478 | return -EINVAL; |
1da177e4 | 1479 | if (!len) |
077a1cc0 NA |
1480 | return 0; |
1481 | if (!access_ok(VERIFY_WRITE, buf, len)) | |
1482 | return -EFAULT; | |
40dc5651 | 1483 | error = wait_event_interruptible(log_wait, |
7ff9554b | 1484 | syslog_seq != log_next_seq); |
cb424ffe | 1485 | if (error) |
077a1cc0 | 1486 | return error; |
7ff9554b | 1487 | error = syslog_print(buf, len); |
1da177e4 | 1488 | break; |
d78ca3cd KC |
1489 | /* Read/clear last kernel messages */ |
1490 | case SYSLOG_ACTION_READ_CLEAR: | |
7ff9554b | 1491 | clear = true; |
1da177e4 | 1492 | /* FALL THRU */ |
d78ca3cd KC |
1493 | /* Read last kernel messages */ |
1494 | case SYSLOG_ACTION_READ_ALL: | |
1da177e4 | 1495 | if (!buf || len < 0) |
077a1cc0 | 1496 | return -EINVAL; |
1da177e4 | 1497 | if (!len) |
077a1cc0 NA |
1498 | return 0; |
1499 | if (!access_ok(VERIFY_WRITE, buf, len)) | |
1500 | return -EFAULT; | |
7ff9554b | 1501 | error = syslog_print_all(buf, len, clear); |
1da177e4 | 1502 | break; |
d78ca3cd KC |
1503 | /* Clear ring buffer */ |
1504 | case SYSLOG_ACTION_CLEAR: | |
8599dc7d | 1505 | syslog_clear(); |
4661e356 | 1506 | break; |
d78ca3cd KC |
1507 | /* Disable logging to console */ |
1508 | case SYSLOG_ACTION_CONSOLE_OFF: | |
a39d4a85 | 1509 | if (saved_console_loglevel == LOGLEVEL_DEFAULT) |
1aaad49e | 1510 | saved_console_loglevel = console_loglevel; |
1da177e4 LT |
1511 | console_loglevel = minimum_console_loglevel; |
1512 | break; | |
d78ca3cd KC |
1513 | /* Enable logging to console */ |
1514 | case SYSLOG_ACTION_CONSOLE_ON: | |
a39d4a85 | 1515 | if (saved_console_loglevel != LOGLEVEL_DEFAULT) { |
1aaad49e | 1516 | console_loglevel = saved_console_loglevel; |
a39d4a85 | 1517 | saved_console_loglevel = LOGLEVEL_DEFAULT; |
1aaad49e | 1518 | } |
1da177e4 | 1519 | break; |
d78ca3cd KC |
1520 | /* Set level of messages printed to console */ |
1521 | case SYSLOG_ACTION_CONSOLE_LEVEL: | |
1da177e4 | 1522 | if (len < 1 || len > 8) |
077a1cc0 | 1523 | return -EINVAL; |
1da177e4 LT |
1524 | if (len < minimum_console_loglevel) |
1525 | len = minimum_console_loglevel; | |
1526 | console_loglevel = len; | |
1aaad49e | 1527 | /* Implicitly re-enable logging to console */ |
a39d4a85 | 1528 | saved_console_loglevel = LOGLEVEL_DEFAULT; |
1da177e4 | 1529 | break; |
d78ca3cd KC |
1530 | /* Number of chars in the log buffer */ |
1531 | case SYSLOG_ACTION_SIZE_UNREAD: | |
de6fcbdb | 1532 | logbuf_lock_irq(); |
7ff9554b KS |
1533 | if (syslog_seq < log_first_seq) { |
1534 | /* messages are gone, move to first one */ | |
1535 | syslog_seq = log_first_seq; | |
1536 | syslog_idx = log_first_idx; | |
eb02dac9 | 1537 | syslog_partial = 0; |
7ff9554b | 1538 | } |
3ea4331c | 1539 | if (source == SYSLOG_FROM_PROC) { |
7ff9554b KS |
1540 | /* |
1541 | * Short-cut for poll(/"proc/kmsg") which simply checks | |
1542 | * for pending data, not the size; return the count of | |
1543 | * records, not the length. | |
1544 | */ | |
e97e1267 | 1545 | error = log_next_seq - syslog_seq; |
7ff9554b | 1546 | } else { |
5becfb1d KS |
1547 | u64 seq = syslog_seq; |
1548 | u32 idx = syslog_idx; | |
e80c1a9d | 1549 | bool time = syslog_partial ? syslog_time : printk_time; |
7ff9554b | 1550 | |
7ff9554b | 1551 | while (seq < log_next_seq) { |
62e32ac3 | 1552 | struct printk_log *msg = log_from_idx(idx); |
3ce9a7c0 | 1553 | |
e80c1a9d TH |
1554 | error += msg_print_text(msg, true, time, NULL, |
1555 | 0); | |
1556 | time = printk_time; | |
7ff9554b KS |
1557 | idx = log_next(idx); |
1558 | seq++; | |
1559 | } | |
eb02dac9 | 1560 | error -= syslog_partial; |
7ff9554b | 1561 | } |
de6fcbdb | 1562 | logbuf_unlock_irq(); |
1da177e4 | 1563 | break; |
d78ca3cd KC |
1564 | /* Size of the log buffer */ |
1565 | case SYSLOG_ACTION_SIZE_BUFFER: | |
1da177e4 LT |
1566 | error = log_buf_len; |
1567 | break; | |
1568 | default: | |
1569 | error = -EINVAL; | |
1570 | break; | |
1571 | } | |
077a1cc0 | 1572 | |
1da177e4 LT |
1573 | return error; |
1574 | } | |
1575 | ||
1e7bfb21 | 1576 | SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len) |
1da177e4 | 1577 | { |
637241a9 | 1578 | return do_syslog(type, buf, len, SYSLOG_FROM_READER); |
1da177e4 LT |
1579 | } |
1580 | ||
c162d5b4 PM |
1581 | /* |
1582 | * Special console_lock variants that help to reduce the risk of soft-lockups. | |
1583 | * They allow to pass console_lock to another printk() call using a busy wait. | |
1584 | */ | |
1585 | ||
1586 | #ifdef CONFIG_LOCKDEP | |
1587 | static struct lockdep_map console_owner_dep_map = { | |
1588 | .name = "console_owner" | |
1589 | }; | |
1590 | #endif | |
1591 | ||
1592 | static DEFINE_RAW_SPINLOCK(console_owner_lock); | |
1593 | static struct task_struct *console_owner; | |
1594 | static bool console_waiter; | |
1595 | ||
1596 | /** | |
1597 | * console_lock_spinning_enable - mark beginning of code where another | |
1598 | * thread might safely busy wait | |
1599 | * | |
1600 | * This basically converts console_lock into a spinlock. This marks | |
1601 | * the section where the console_lock owner can not sleep, because | |
1602 | * there may be a waiter spinning (like a spinlock). Also it must be | |
1603 | * ready to hand over the lock at the end of the section. | |
1604 | */ | |
1605 | static void console_lock_spinning_enable(void) | |
1606 | { | |
1607 | raw_spin_lock(&console_owner_lock); | |
1608 | console_owner = current; | |
1609 | raw_spin_unlock(&console_owner_lock); | |
1610 | ||
1611 | /* The waiter may spin on us after setting console_owner */ | |
1612 | spin_acquire(&console_owner_dep_map, 0, 0, _THIS_IP_); | |
1613 | } | |
1614 | ||
1615 | /** | |
1616 | * console_lock_spinning_disable_and_check - mark end of code where another | |
1617 | * thread was able to busy wait and check if there is a waiter | |
1618 | * | |
1619 | * This is called at the end of the section where spinning is allowed. | |
1620 | * It has two functions. First, it is a signal that it is no longer | |
1621 | * safe to start busy waiting for the lock. Second, it checks if | |
1622 | * there is a busy waiter and passes the lock rights to her. | |
1623 | * | |
1624 | * Important: Callers lose the lock if there was a busy waiter. | |
1625 | * They must not touch items synchronized by console_lock | |
1626 | * in this case. | |
1627 | * | |
1628 | * Return: 1 if the lock rights were passed, 0 otherwise. | |
1629 | */ | |
1630 | static int console_lock_spinning_disable_and_check(void) | |
1631 | { | |
1632 | int waiter; | |
1633 | ||
1634 | raw_spin_lock(&console_owner_lock); | |
1635 | waiter = READ_ONCE(console_waiter); | |
1636 | console_owner = NULL; | |
1637 | raw_spin_unlock(&console_owner_lock); | |
1638 | ||
1639 | if (!waiter) { | |
1640 | spin_release(&console_owner_dep_map, 1, _THIS_IP_); | |
1641 | return 0; | |
1642 | } | |
1643 | ||
1644 | /* The waiter is now free to continue */ | |
1645 | WRITE_ONCE(console_waiter, false); | |
1646 | ||
1647 | spin_release(&console_owner_dep_map, 1, _THIS_IP_); | |
1648 | ||
1649 | /* | |
1650 | * Hand off console_lock to waiter. The waiter will perform | |
1651 | * the up(). After this, the waiter is the console_lock owner. | |
1652 | */ | |
1653 | mutex_release(&console_lock_dep_map, 1, _THIS_IP_); | |
1654 | return 1; | |
1655 | } | |
1656 | ||
1657 | /** | |
1658 | * console_trylock_spinning - try to get console_lock by busy waiting | |
1659 | * | |
1660 | * This allows to busy wait for the console_lock when the current | |
1661 | * owner is running in specially marked sections. It means that | |
1662 | * the current owner is running and cannot reschedule until it | |
1663 | * is ready to lose the lock. | |
1664 | * | |
1665 | * Return: 1 if we got the lock, 0 othrewise | |
1666 | */ | |
1667 | static int console_trylock_spinning(void) | |
1668 | { | |
1669 | struct task_struct *owner = NULL; | |
1670 | bool waiter; | |
1671 | bool spin = false; | |
1672 | unsigned long flags; | |
1673 | ||
1674 | if (console_trylock()) | |
1675 | return 1; | |
1676 | ||
1677 | printk_safe_enter_irqsave(flags); | |
1678 | ||
1679 | raw_spin_lock(&console_owner_lock); | |
1680 | owner = READ_ONCE(console_owner); | |
1681 | waiter = READ_ONCE(console_waiter); | |
1682 | if (!waiter && owner && owner != current) { | |
1683 | WRITE_ONCE(console_waiter, true); | |
1684 | spin = true; | |
1685 | } | |
1686 | raw_spin_unlock(&console_owner_lock); | |
1687 | ||
1688 | /* | |
1689 | * If there is an active printk() writing to the | |
1690 | * consoles, instead of having it write our data too, | |
1691 | * see if we can offload that load from the active | |
1692 | * printer, and do some printing ourselves. | |
1693 | * Go into a spin only if there isn't already a waiter | |
1694 | * spinning, and there is an active printer, and | |
1695 | * that active printer isn't us (recursive printk?). | |
1696 | */ | |
1697 | if (!spin) { | |
1698 | printk_safe_exit_irqrestore(flags); | |
1699 | return 0; | |
1700 | } | |
1701 | ||
1702 | /* We spin waiting for the owner to release us */ | |
1703 | spin_acquire(&console_owner_dep_map, 0, 0, _THIS_IP_); | |
1704 | /* Owner will clear console_waiter on hand off */ | |
1705 | while (READ_ONCE(console_waiter)) | |
1706 | cpu_relax(); | |
1707 | spin_release(&console_owner_dep_map, 1, _THIS_IP_); | |
1708 | ||
1709 | printk_safe_exit_irqrestore(flags); | |
1710 | /* | |
1711 | * The owner passed the console lock to us. | |
1712 | * Since we did not spin on console lock, annotate | |
1713 | * this as a trylock. Otherwise lockdep will | |
1714 | * complain. | |
1715 | */ | |
1716 | mutex_acquire(&console_lock_dep_map, 0, 1, _THIS_IP_); | |
1717 | ||
1718 | return 1; | |
1719 | } | |
1720 | ||
1da177e4 LT |
1721 | /* |
1722 | * Call the console drivers, asking them to write out | |
1723 | * log_buf[start] to log_buf[end - 1]. | |
ac751efa | 1724 | * The console_lock must be held. |
1da177e4 | 1725 | */ |
d9c23523 | 1726 | static void call_console_drivers(const char *ext_text, size_t ext_len, |
6fe29354 | 1727 | const char *text, size_t len) |
1da177e4 | 1728 | { |
7ff9554b | 1729 | struct console *con; |
1da177e4 | 1730 | |
fc98c3c8 | 1731 | trace_console_rcuidle(text, len); |
7ff9554b | 1732 | |
7ff9554b KS |
1733 | if (!console_drivers) |
1734 | return; | |
1735 | ||
1736 | for_each_console(con) { | |
1737 | if (exclusive_console && con != exclusive_console) | |
1738 | continue; | |
1739 | if (!(con->flags & CON_ENABLED)) | |
1740 | continue; | |
1741 | if (!con->write) | |
1742 | continue; | |
1743 | if (!cpu_online(smp_processor_id()) && | |
1744 | !(con->flags & CON_ANYTIME)) | |
1745 | continue; | |
6fe29354 TH |
1746 | if (con->flags & CON_EXTENDED) |
1747 | con->write(con, ext_text, ext_len); | |
1748 | else | |
1749 | con->write(con, text, len); | |
7ff9554b | 1750 | } |
1da177e4 LT |
1751 | } |
1752 | ||
af91322e DY |
1753 | int printk_delay_msec __read_mostly; |
1754 | ||
1755 | static inline void printk_delay(void) | |
1756 | { | |
1757 | if (unlikely(printk_delay_msec)) { | |
1758 | int m = printk_delay_msec; | |
1759 | ||
1760 | while (m--) { | |
1761 | mdelay(1); | |
1762 | touch_nmi_watchdog(); | |
1763 | } | |
1764 | } | |
1765 | } | |
1766 | ||
084681d1 KS |
1767 | /* |
1768 | * Continuation lines are buffered, and not committed to the record buffer | |
1769 | * until the line is complete, or a race forces it. The line fragments | |
1770 | * though, are printed immediately to the consoles to ensure everything has | |
1771 | * reached the console in case of a kernel crash. | |
1772 | */ | |
1773 | static struct cont { | |
1774 | char buf[LOG_LINE_MAX]; | |
1775 | size_t len; /* length == 0 means unused buffer */ | |
084681d1 KS |
1776 | struct task_struct *owner; /* task of first print*/ |
1777 | u64 ts_nsec; /* time of first print */ | |
1778 | u8 level; /* log level of first message */ | |
0b90fec3 | 1779 | u8 facility; /* log facility of first message */ |
eab07260 | 1780 | enum log_flags flags; /* prefix, newline flags */ |
084681d1 KS |
1781 | } cont; |
1782 | ||
5e467652 | 1783 | static void cont_flush(void) |
084681d1 | 1784 | { |
084681d1 KS |
1785 | if (cont.len == 0) |
1786 | return; | |
5c2992ee LT |
1787 | |
1788 | log_store(cont.facility, cont.level, cont.flags, cont.ts_nsec, | |
1789 | NULL, 0, cont.buf, cont.len); | |
1790 | cont.len = 0; | |
084681d1 KS |
1791 | } |
1792 | ||
5e467652 | 1793 | static bool cont_add(int facility, int level, enum log_flags flags, const char *text, size_t len) |
084681d1 | 1794 | { |
9627808d SS |
1795 | /* If the line gets too long, split it up in separate records. */ |
1796 | if (cont.len + len > sizeof(cont.buf)) { | |
5e467652 | 1797 | cont_flush(); |
084681d1 KS |
1798 | return false; |
1799 | } | |
1800 | ||
1801 | if (!cont.len) { | |
1802 | cont.facility = facility; | |
1803 | cont.level = level; | |
1804 | cont.owner = current; | |
1805 | cont.ts_nsec = local_clock(); | |
5e467652 | 1806 | cont.flags = flags; |
084681d1 KS |
1807 | } |
1808 | ||
1809 | memcpy(cont.buf + cont.len, text, len); | |
1810 | cont.len += len; | |
eab07260 | 1811 | |
5e467652 LT |
1812 | // The original flags come from the first line, |
1813 | // but later continuations can add a newline. | |
1814 | if (flags & LOG_NEWLINE) { | |
1815 | cont.flags |= LOG_NEWLINE; | |
1816 | cont_flush(); | |
1817 | } | |
1818 | ||
084681d1 KS |
1819 | return true; |
1820 | } | |
1821 | ||
c362c7ff LT |
1822 | static size_t log_output(int facility, int level, enum log_flags lflags, const char *dict, size_t dictlen, char *text, size_t text_len) |
1823 | { | |
c362c7ff | 1824 | /* |
5e467652 LT |
1825 | * If an earlier line was buffered, and we're a continuation |
1826 | * write from the same process, try to add it to the buffer. | |
c362c7ff LT |
1827 | */ |
1828 | if (cont.len) { | |
5e467652 LT |
1829 | if (cont.owner == current && (lflags & LOG_CONT)) { |
1830 | if (cont_add(facility, level, lflags, text, text_len)) | |
1831 | return text_len; | |
1832 | } | |
1833 | /* Otherwise, make sure it's flushed */ | |
1834 | cont_flush(); | |
1835 | } | |
c362c7ff | 1836 | |
8835ca59 LT |
1837 | /* Skip empty continuation lines that couldn't be added - they just flush */ |
1838 | if (!text_len && (lflags & LOG_CONT)) | |
1839 | return 0; | |
1840 | ||
5e467652 LT |
1841 | /* If it doesn't end in a newline, try to buffer the current line */ |
1842 | if (!(lflags & LOG_NEWLINE)) { | |
1843 | if (cont_add(facility, level, lflags, text, text_len)) | |
c362c7ff LT |
1844 | return text_len; |
1845 | } | |
1846 | ||
5e467652 | 1847 | /* Store it in the record log */ |
c362c7ff LT |
1848 | return log_store(facility, level, lflags, 0, dict, dictlen, text, text_len); |
1849 | } | |
1850 | ||
ba552399 PM |
1851 | /* Must be called under logbuf_lock. */ |
1852 | int vprintk_store(int facility, int level, | |
1853 | const char *dict, size_t dictlen, | |
1854 | const char *fmt, va_list args) | |
1da177e4 | 1855 | { |
7ff9554b KS |
1856 | static char textbuf[LOG_LINE_MAX]; |
1857 | char *text = textbuf; | |
aec47caa | 1858 | size_t text_len; |
5becfb1d | 1859 | enum log_flags lflags = 0; |
bfe8df3d | 1860 | |
7ff9554b KS |
1861 | /* |
1862 | * The printf needs to come first; we need the syslog | |
1863 | * prefix which might be passed-in as a parameter. | |
1864 | */ | |
98e35f58 | 1865 | text_len = vscnprintf(text, sizeof(textbuf), fmt, args); |
5fd29d6c | 1866 | |
7ff9554b | 1867 | /* mark and strip a trailing newline */ |
c313af14 KS |
1868 | if (text_len && text[text_len-1] == '\n') { |
1869 | text_len--; | |
5becfb1d | 1870 | lflags |= LOG_NEWLINE; |
7ff9554b | 1871 | } |
9d90c8d9 | 1872 | |
088a52aa JP |
1873 | /* strip kernel syslog prefix and extract log level or control flags */ |
1874 | if (facility == 0) { | |
4bcc595c | 1875 | int kern_level; |
088a52aa | 1876 | |
4bcc595c | 1877 | while ((kern_level = printk_get_level(text)) != 0) { |
088a52aa JP |
1878 | switch (kern_level) { |
1879 | case '0' ... '7': | |
a39d4a85 | 1880 | if (level == LOGLEVEL_DEFAULT) |
088a52aa | 1881 | level = kern_level - '0'; |
a39d4a85 | 1882 | /* fallthrough */ |
088a52aa JP |
1883 | case 'd': /* KERN_DEFAULT */ |
1884 | lflags |= LOG_PREFIX; | |
4bcc595c LT |
1885 | break; |
1886 | case 'c': /* KERN_CONT */ | |
1887 | lflags |= LOG_CONT; | |
088a52aa | 1888 | } |
4bcc595c LT |
1889 | |
1890 | text_len -= 2; | |
1891 | text += 2; | |
5fd29d6c LT |
1892 | } |
1893 | } | |
1894 | ||
a39d4a85 | 1895 | if (level == LOGLEVEL_DEFAULT) |
c313af14 | 1896 | level = default_message_loglevel; |
9d90c8d9 | 1897 | |
5becfb1d KS |
1898 | if (dict) |
1899 | lflags |= LOG_PREFIX|LOG_NEWLINE; | |
ac60ad74 | 1900 | |
ba552399 PM |
1901 | return log_output(facility, level, lflags, |
1902 | dict, dictlen, text, text_len); | |
1903 | } | |
1da177e4 | 1904 | |
ba552399 PM |
1905 | asmlinkage int vprintk_emit(int facility, int level, |
1906 | const char *dict, size_t dictlen, | |
1907 | const char *fmt, va_list args) | |
1908 | { | |
1909 | int printed_len; | |
3ac37a93 | 1910 | bool in_sched = false, pending_output; |
ba552399 | 1911 | unsigned long flags; |
3ac37a93 | 1912 | u64 curr_log_seq; |
ba552399 PM |
1913 | |
1914 | if (level == LOGLEVEL_SCHED) { | |
1915 | level = LOGLEVEL_DEFAULT; | |
1916 | in_sched = true; | |
1917 | } | |
1918 | ||
1919 | boot_delay_msec(level); | |
1920 | printk_delay(); | |
1921 | ||
1922 | /* This stops the holder of console_sem just where we want him */ | |
1923 | logbuf_lock_irqsave(flags); | |
3ac37a93 | 1924 | curr_log_seq = log_next_seq; |
ba552399 | 1925 | printed_len = vprintk_store(facility, level, dict, dictlen, fmt, args); |
3ac37a93 | 1926 | pending_output = (curr_log_seq != log_next_seq); |
de6fcbdb | 1927 | logbuf_unlock_irqrestore(flags); |
939f04be | 1928 | |
458df9fd | 1929 | /* If called from the scheduler, we can not call up(). */ |
3ac37a93 | 1930 | if (!in_sched && pending_output) { |
fd5f7cde SS |
1931 | /* |
1932 | * Disable preemption to avoid being preempted while holding | |
1933 | * console_sem which would prevent anyone from printing to | |
1934 | * console | |
1935 | */ | |
1936 | preempt_disable(); | |
d18bbc21 AM |
1937 | /* |
1938 | * Try to acquire and then immediately release the console | |
1939 | * semaphore. The release will print out buffers and wake up | |
1940 | * /dev/kmsg and syslog() users. | |
1941 | */ | |
c162d5b4 | 1942 | if (console_trylock_spinning()) |
d18bbc21 | 1943 | console_unlock(); |
fd5f7cde | 1944 | preempt_enable(); |
d18bbc21 | 1945 | } |
76a8ad29 | 1946 | |
3ac37a93 SS |
1947 | if (pending_output) |
1948 | wake_up_klogd(); | |
1da177e4 LT |
1949 | return printed_len; |
1950 | } | |
7ff9554b KS |
1951 | EXPORT_SYMBOL(vprintk_emit); |
1952 | ||
1953 | asmlinkage int vprintk(const char *fmt, va_list args) | |
1954 | { | |
bd66a892 | 1955 | return vprintk_func(fmt, args); |
7ff9554b | 1956 | } |
1da177e4 LT |
1957 | EXPORT_SYMBOL(vprintk); |
1958 | ||
a0cba217 | 1959 | int vprintk_default(const char *fmt, va_list args) |
afdc34a3 SRRH |
1960 | { |
1961 | int r; | |
1962 | ||
1963 | #ifdef CONFIG_KGDB_KDB | |
34aaff40 PM |
1964 | /* Allow to pass printk() to kdb but avoid a recursion. */ |
1965 | if (unlikely(kdb_trap_printk && kdb_printf_cpu < 0)) { | |
f7d4ca8b | 1966 | r = vkdb_printf(KDB_MSGSRC_PRINTK, fmt, args); |
afdc34a3 SRRH |
1967 | return r; |
1968 | } | |
1969 | #endif | |
a0cba217 | 1970 | r = vprintk_emit(0, LOGLEVEL_DEFAULT, NULL, 0, fmt, args); |
afdc34a3 SRRH |
1971 | |
1972 | return r; | |
1973 | } | |
1974 | EXPORT_SYMBOL_GPL(vprintk_default); | |
1975 | ||
7ff9554b KS |
1976 | /** |
1977 | * printk - print a kernel message | |
1978 | * @fmt: format string | |
1979 | * | |
1980 | * This is printk(). It can be called from any context. We want it to work. | |
1981 | * | |
1982 | * We try to grab the console_lock. If we succeed, it's easy - we log the | |
1983 | * output and call the console drivers. If we fail to get the semaphore, we | |
1984 | * place the output into the log buffer and return. The current holder of | |
1985 | * the console_sem will notice the new output in console_unlock(); and will | |
1986 | * send it to the consoles before releasing the lock. | |
1987 | * | |
1988 | * One effect of this deferred printing is that code which calls printk() and | |
1989 | * then changes console_loglevel may break. This is because console_loglevel | |
1990 | * is inspected when the actual printing occurs. | |
1991 | * | |
1992 | * See also: | |
1993 | * printf(3) | |
1994 | * | |
1995 | * See the vsnprintf() documentation for format string extensions over C99. | |
1996 | */ | |
722a9f92 | 1997 | asmlinkage __visible int printk(const char *fmt, ...) |
7ff9554b KS |
1998 | { |
1999 | va_list args; | |
2000 | int r; | |
2001 | ||
7ff9554b | 2002 | va_start(args, fmt); |
a0cba217 | 2003 | r = vprintk_func(fmt, args); |
7ff9554b KS |
2004 | va_end(args); |
2005 | ||
2006 | return r; | |
2007 | } | |
2008 | EXPORT_SYMBOL(printk); | |
7f3a781d | 2009 | |
96efedf1 | 2010 | #else /* CONFIG_PRINTK */ |
d59745ce | 2011 | |
70498253 KS |
2012 | #define LOG_LINE_MAX 0 |
2013 | #define PREFIX_MAX 0 | |
e80c1a9d | 2014 | #define printk_time false |
249771b8 | 2015 | |
96efedf1 KS |
2016 | static u64 syslog_seq; |
2017 | static u32 syslog_idx; | |
eab07260 KS |
2018 | static u64 console_seq; |
2019 | static u32 console_idx; | |
f92b070f | 2020 | static u64 exclusive_console_stop_seq; |
96efedf1 KS |
2021 | static u64 log_first_seq; |
2022 | static u32 log_first_idx; | |
2023 | static u64 log_next_seq; | |
6fe29354 TH |
2024 | static char *log_text(const struct printk_log *msg) { return NULL; } |
2025 | static char *log_dict(const struct printk_log *msg) { return NULL; } | |
62e32ac3 | 2026 | static struct printk_log *log_from_idx(u32 idx) { return NULL; } |
7f3a781d | 2027 | static u32 log_next(u32 idx) { return 0; } |
6fe29354 | 2028 | static ssize_t msg_print_ext_header(char *buf, size_t size, |
5aa068ea LT |
2029 | struct printk_log *msg, |
2030 | u64 seq) { return 0; } | |
6fe29354 TH |
2031 | static ssize_t msg_print_ext_body(char *buf, size_t size, |
2032 | char *dict, size_t dict_len, | |
2033 | char *text, size_t text_len) { return 0; } | |
c162d5b4 PM |
2034 | static void console_lock_spinning_enable(void) { } |
2035 | static int console_lock_spinning_disable_and_check(void) { return 0; } | |
d9c23523 | 2036 | static void call_console_drivers(const char *ext_text, size_t ext_len, |
6fe29354 | 2037 | const char *text, size_t len) {} |
e80c1a9d TH |
2038 | static size_t msg_print_text(const struct printk_log *msg, bool syslog, |
2039 | bool time, char *buf, size_t size) { return 0; } | |
a6ae928c | 2040 | static bool suppress_message_printing(int level) { return false; } |
d59745ce | 2041 | |
7f3a781d | 2042 | #endif /* CONFIG_PRINTK */ |
d59745ce | 2043 | |
d0380e6c TG |
2044 | #ifdef CONFIG_EARLY_PRINTK |
2045 | struct console *early_console; | |
2046 | ||
722a9f92 | 2047 | asmlinkage __visible void early_printk(const char *fmt, ...) |
d0380e6c TG |
2048 | { |
2049 | va_list ap; | |
1dc6244b JP |
2050 | char buf[512]; |
2051 | int n; | |
2052 | ||
2053 | if (!early_console) | |
2054 | return; | |
d0380e6c TG |
2055 | |
2056 | va_start(ap, fmt); | |
1dc6244b | 2057 | n = vscnprintf(buf, sizeof(buf), fmt, ap); |
d0380e6c | 2058 | va_end(ap); |
1dc6244b JP |
2059 | |
2060 | early_console->write(early_console, buf, n); | |
d0380e6c TG |
2061 | } |
2062 | #endif | |
2063 | ||
f7511d5f ST |
2064 | static int __add_preferred_console(char *name, int idx, char *options, |
2065 | char *brl_options) | |
2066 | { | |
2067 | struct console_cmdline *c; | |
2068 | int i; | |
2069 | ||
2070 | /* | |
2071 | * See if this tty is not yet registered, and | |
2072 | * if we have a slot free. | |
2073 | */ | |
dac8bbba PM |
2074 | for (i = 0, c = console_cmdline; |
2075 | i < MAX_CMDLINECONSOLES && c->name[0]; | |
2076 | i++, c++) { | |
23475408 | 2077 | if (strcmp(c->name, name) == 0 && c->index == idx) { |
dac8bbba PM |
2078 | if (!brl_options) |
2079 | preferred_console = i; | |
23475408 | 2080 | return 0; |
f7511d5f | 2081 | } |
23475408 | 2082 | } |
f7511d5f ST |
2083 | if (i == MAX_CMDLINECONSOLES) |
2084 | return -E2BIG; | |
2085 | if (!brl_options) | |
ad86ee2b | 2086 | preferred_console = i; |
f7511d5f ST |
2087 | strlcpy(c->name, name, sizeof(c->name)); |
2088 | c->options = options; | |
bbeddf52 JP |
2089 | braille_set_options(c, brl_options); |
2090 | ||
f7511d5f ST |
2091 | c->index = idx; |
2092 | return 0; | |
2093 | } | |
cca10d58 SS |
2094 | |
2095 | static int __init console_msg_format_setup(char *str) | |
2096 | { | |
2097 | if (!strcmp(str, "syslog")) | |
2098 | console_msg_format = MSG_FORMAT_SYSLOG; | |
2099 | if (!strcmp(str, "default")) | |
2100 | console_msg_format = MSG_FORMAT_DEFAULT; | |
2101 | return 1; | |
2102 | } | |
2103 | __setup("console_msg_format=", console_msg_format_setup); | |
2104 | ||
2ea1c539 | 2105 | /* |
0b90fec3 AE |
2106 | * Set up a console. Called via do_early_param() in init/main.c |
2107 | * for each "console=" parameter in the boot command line. | |
2ea1c539 JB |
2108 | */ |
2109 | static int __init console_setup(char *str) | |
2110 | { | |
0b90fec3 | 2111 | char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for "ttyS" */ |
f7511d5f | 2112 | char *s, *options, *brl_options = NULL; |
2ea1c539 JB |
2113 | int idx; |
2114 | ||
bbeddf52 JP |
2115 | if (_braille_console_setup(&str, &brl_options)) |
2116 | return 1; | |
f7511d5f | 2117 | |
2ea1c539 JB |
2118 | /* |
2119 | * Decode str into name, index, options. | |
2120 | */ | |
2121 | if (str[0] >= '0' && str[0] <= '9') { | |
eaa944af YL |
2122 | strcpy(buf, "ttyS"); |
2123 | strncpy(buf + 4, str, sizeof(buf) - 5); | |
2ea1c539 | 2124 | } else { |
eaa944af | 2125 | strncpy(buf, str, sizeof(buf) - 1); |
2ea1c539 | 2126 | } |
eaa944af | 2127 | buf[sizeof(buf) - 1] = 0; |
249771b8 AE |
2128 | options = strchr(str, ','); |
2129 | if (options) | |
2ea1c539 JB |
2130 | *(options++) = 0; |
2131 | #ifdef __sparc__ | |
2132 | if (!strcmp(str, "ttya")) | |
eaa944af | 2133 | strcpy(buf, "ttyS0"); |
2ea1c539 | 2134 | if (!strcmp(str, "ttyb")) |
eaa944af | 2135 | strcpy(buf, "ttyS1"); |
2ea1c539 | 2136 | #endif |
eaa944af | 2137 | for (s = buf; *s; s++) |
249771b8 | 2138 | if (isdigit(*s) || *s == ',') |
2ea1c539 JB |
2139 | break; |
2140 | idx = simple_strtoul(s, NULL, 10); | |
2141 | *s = 0; | |
2142 | ||
f7511d5f | 2143 | __add_preferred_console(buf, idx, options, brl_options); |
9e124fe1 | 2144 | console_set_on_cmdline = 1; |
2ea1c539 JB |
2145 | return 1; |
2146 | } | |
2147 | __setup("console=", console_setup); | |
2148 | ||
3c0547ba MM |
2149 | /** |
2150 | * add_preferred_console - add a device to the list of preferred consoles. | |
ddad86c2 MW |
2151 | * @name: device name |
2152 | * @idx: device index | |
2153 | * @options: options for this console | |
3c0547ba MM |
2154 | * |
2155 | * The last preferred console added will be used for kernel messages | |
2156 | * and stdin/out/err for init. Normally this is used by console_setup | |
2157 | * above to handle user-supplied console arguments; however it can also | |
2158 | * be used by arch-specific code either to override the user or more | |
2159 | * commonly to provide a default console (ie from PROM variables) when | |
2160 | * the user has not supplied one. | |
2161 | */ | |
fb445ee5 | 2162 | int add_preferred_console(char *name, int idx, char *options) |
3c0547ba | 2163 | { |
f7511d5f | 2164 | return __add_preferred_console(name, idx, options, NULL); |
3c0547ba MM |
2165 | } |
2166 | ||
d25d9fec | 2167 | bool console_suspend_enabled = true; |
8f4ce8c3 AS |
2168 | EXPORT_SYMBOL(console_suspend_enabled); |
2169 | ||
2170 | static int __init console_suspend_disable(char *str) | |
2171 | { | |
d25d9fec | 2172 | console_suspend_enabled = false; |
8f4ce8c3 AS |
2173 | return 1; |
2174 | } | |
2175 | __setup("no_console_suspend", console_suspend_disable); | |
134620f7 YZ |
2176 | module_param_named(console_suspend, console_suspend_enabled, |
2177 | bool, S_IRUGO | S_IWUSR); | |
2178 | MODULE_PARM_DESC(console_suspend, "suspend console during suspend" | |
2179 | " and hibernate operations"); | |
8f4ce8c3 | 2180 | |
557240b4 LT |
2181 | /** |
2182 | * suspend_console - suspend the console subsystem | |
2183 | * | |
2184 | * This disables printk() while we go into suspend states | |
2185 | */ | |
2186 | void suspend_console(void) | |
2187 | { | |
8f4ce8c3 AS |
2188 | if (!console_suspend_enabled) |
2189 | return; | |
47319f71 | 2190 | pr_info("Suspending console(s) (use no_console_suspend to debug)\n"); |
ac751efa | 2191 | console_lock(); |
557240b4 | 2192 | console_suspended = 1; |
bd8d7cf5 | 2193 | up_console_sem(); |
557240b4 LT |
2194 | } |
2195 | ||
2196 | void resume_console(void) | |
2197 | { | |
8f4ce8c3 AS |
2198 | if (!console_suspend_enabled) |
2199 | return; | |
bd8d7cf5 | 2200 | down_console_sem(); |
557240b4 | 2201 | console_suspended = 0; |
ac751efa | 2202 | console_unlock(); |
557240b4 LT |
2203 | } |
2204 | ||
034260d6 KC |
2205 | /** |
2206 | * console_cpu_notify - print deferred console messages after CPU hotplug | |
90b14889 | 2207 | * @cpu: unused |
034260d6 KC |
2208 | * |
2209 | * If printk() is called from a CPU that is not online yet, the messages | |
64ca752d SS |
2210 | * will be printed on the console only if there are CON_ANYTIME consoles. |
2211 | * This function is called when a new CPU comes online (or fails to come | |
2212 | * up) or goes offline. | |
034260d6 | 2213 | */ |
90b14889 SAS |
2214 | static int console_cpu_notify(unsigned int cpu) |
2215 | { | |
f97960fb | 2216 | if (!cpuhp_tasks_frozen) { |
64ca752d SS |
2217 | /* If trylock fails, someone else is doing the printing */ |
2218 | if (console_trylock()) | |
2219 | console_unlock(); | |
034260d6 | 2220 | } |
90b14889 | 2221 | return 0; |
034260d6 KC |
2222 | } |
2223 | ||
1da177e4 | 2224 | /** |
ac751efa | 2225 | * console_lock - lock the console system for exclusive use. |
1da177e4 | 2226 | * |
ac751efa | 2227 | * Acquires a lock which guarantees that the caller has |
1da177e4 LT |
2228 | * exclusive access to the console system and the console_drivers list. |
2229 | * | |
2230 | * Can sleep, returns nothing. | |
2231 | */ | |
ac751efa | 2232 | void console_lock(void) |
1da177e4 | 2233 | { |
6b898c07 DV |
2234 | might_sleep(); |
2235 | ||
bd8d7cf5 | 2236 | down_console_sem(); |
403f3075 AH |
2237 | if (console_suspended) |
2238 | return; | |
1da177e4 LT |
2239 | console_locked = 1; |
2240 | console_may_schedule = 1; | |
2241 | } | |
ac751efa | 2242 | EXPORT_SYMBOL(console_lock); |
1da177e4 | 2243 | |
ac751efa TH |
2244 | /** |
2245 | * console_trylock - try to lock the console system for exclusive use. | |
2246 | * | |
0b90fec3 AE |
2247 | * Try to acquire a lock which guarantees that the caller has exclusive |
2248 | * access to the console system and the console_drivers list. | |
ac751efa TH |
2249 | * |
2250 | * returns 1 on success, and 0 on failure to acquire the lock. | |
2251 | */ | |
2252 | int console_trylock(void) | |
1da177e4 | 2253 | { |
bd8d7cf5 | 2254 | if (down_trylock_console_sem()) |
ac751efa | 2255 | return 0; |
403f3075 | 2256 | if (console_suspended) { |
bd8d7cf5 | 2257 | up_console_sem(); |
ac751efa | 2258 | return 0; |
403f3075 | 2259 | } |
1da177e4 | 2260 | console_locked = 1; |
fd5f7cde | 2261 | console_may_schedule = 0; |
ac751efa | 2262 | return 1; |
1da177e4 | 2263 | } |
ac751efa | 2264 | EXPORT_SYMBOL(console_trylock); |
1da177e4 LT |
2265 | |
2266 | int is_console_locked(void) | |
2267 | { | |
2268 | return console_locked; | |
2269 | } | |
d48de54a | 2270 | EXPORT_SYMBOL(is_console_locked); |
1da177e4 | 2271 | |
a8199371 SS |
2272 | /* |
2273 | * Check if we have any console that is capable of printing while cpu is | |
2274 | * booting or shutting down. Requires console_sem. | |
2275 | */ | |
2276 | static int have_callable_console(void) | |
2277 | { | |
2278 | struct console *con; | |
2279 | ||
2280 | for_each_console(con) | |
adaf6590 SS |
2281 | if ((con->flags & CON_ENABLED) && |
2282 | (con->flags & CON_ANYTIME)) | |
a8199371 SS |
2283 | return 1; |
2284 | ||
2285 | return 0; | |
2286 | } | |
2287 | ||
2288 | /* | |
2289 | * Can we actually use the console at this time on this cpu? | |
2290 | * | |
2291 | * Console drivers may assume that per-cpu resources have been allocated. So | |
2292 | * unless they're explicitly marked as being able to cope (CON_ANYTIME) don't | |
2293 | * call them until this CPU is officially up. | |
2294 | */ | |
2295 | static inline int can_use_console(void) | |
2296 | { | |
2297 | return cpu_online(raw_smp_processor_id()) || have_callable_console(); | |
2298 | } | |
2299 | ||
1da177e4 | 2300 | /** |
ac751efa | 2301 | * console_unlock - unlock the console system |
1da177e4 | 2302 | * |
ac751efa | 2303 | * Releases the console_lock which the caller holds on the console system |
1da177e4 LT |
2304 | * and the console driver list. |
2305 | * | |
ac751efa TH |
2306 | * While the console_lock was held, console output may have been buffered |
2307 | * by printk(). If this is the case, console_unlock(); emits | |
2308 | * the output prior to releasing the lock. | |
1da177e4 | 2309 | * |
7f3a781d | 2310 | * If there is output waiting, we wake /dev/kmsg and syslog() users. |
1da177e4 | 2311 | * |
ac751efa | 2312 | * console_unlock(); may be called from any context. |
1da177e4 | 2313 | */ |
ac751efa | 2314 | void console_unlock(void) |
1da177e4 | 2315 | { |
6fe29354 | 2316 | static char ext_text[CONSOLE_EXT_LOG_MAX]; |
70498253 | 2317 | static char text[LOG_LINE_MAX + PREFIX_MAX]; |
1da177e4 | 2318 | unsigned long flags; |
8d91f8b1 | 2319 | bool do_cond_resched, retry; |
1da177e4 | 2320 | |
557240b4 | 2321 | if (console_suspended) { |
bd8d7cf5 | 2322 | up_console_sem(); |
557240b4 LT |
2323 | return; |
2324 | } | |
78944e54 | 2325 | |
8d91f8b1 | 2326 | /* |
257ab443 | 2327 | * Console drivers are called with interrupts disabled, so |
8d91f8b1 TH |
2328 | * @console_may_schedule should be cleared before; however, we may |
2329 | * end up dumping a lot of lines, for example, if called from | |
2330 | * console registration path, and should invoke cond_resched() | |
2331 | * between lines if allowable. Not doing so can cause a very long | |
2332 | * scheduling stall on a slow console leading to RCU stall and | |
2333 | * softlockup warnings which exacerbate the issue with more | |
2334 | * messages practically incapacitating the system. | |
257ab443 PM |
2335 | * |
2336 | * console_trylock() is not able to detect the preemptive | |
2337 | * context reliably. Therefore the value must be stored before | |
2338 | * and cleared after the the "again" goto label. | |
8d91f8b1 TH |
2339 | */ |
2340 | do_cond_resched = console_may_schedule; | |
257ab443 | 2341 | again: |
78944e54 AD |
2342 | console_may_schedule = 0; |
2343 | ||
a8199371 SS |
2344 | /* |
2345 | * We released the console_sem lock, so we need to recheck if | |
2346 | * cpu is online and (if not) is there at least one CON_ANYTIME | |
2347 | * console. | |
2348 | */ | |
2349 | if (!can_use_console()) { | |
2350 | console_locked = 0; | |
2351 | up_console_sem(); | |
2352 | return; | |
2353 | } | |
2354 | ||
7ff9554b | 2355 | for (;;) { |
62e32ac3 | 2356 | struct printk_log *msg; |
6fe29354 | 2357 | size_t ext_len = 0; |
3ce9a7c0 | 2358 | size_t len; |
7ff9554b | 2359 | |
f975237b SS |
2360 | printk_safe_enter_irqsave(flags); |
2361 | raw_spin_lock(&logbuf_lock); | |
7ff9554b | 2362 | if (console_seq < log_first_seq) { |
51a72ab7 HZ |
2363 | len = sprintf(text, |
2364 | "** %llu printk messages dropped **\n", | |
2365 | log_first_seq - console_seq); | |
84b5ec8a | 2366 | |
7ff9554b KS |
2367 | /* messages are gone, move to first one */ |
2368 | console_seq = log_first_seq; | |
2369 | console_idx = log_first_idx; | |
84b5ec8a WD |
2370 | } else { |
2371 | len = 0; | |
7ff9554b | 2372 | } |
084681d1 | 2373 | skip: |
7ff9554b KS |
2374 | if (console_seq == log_next_seq) |
2375 | break; | |
2376 | ||
2377 | msg = log_from_idx(console_idx); | |
a6ae928c | 2378 | if (suppress_message_printing(msg->level)) { |
084681d1 | 2379 | /* |
a6ae928c PM |
2380 | * Skip record we have buffered and already printed |
2381 | * directly to the console when we received it, and | |
2382 | * record that has level above the console loglevel. | |
084681d1 KS |
2383 | */ |
2384 | console_idx = log_next(console_idx); | |
2385 | console_seq++; | |
2386 | goto skip; | |
2387 | } | |
649e6ee3 | 2388 | |
f92b070f PM |
2389 | /* Output to all consoles once old messages replayed. */ |
2390 | if (unlikely(exclusive_console && | |
2391 | console_seq >= exclusive_console_stop_seq)) { | |
2392 | exclusive_console = NULL; | |
2393 | } | |
2394 | ||
cca10d58 SS |
2395 | len += msg_print_text(msg, |
2396 | console_msg_format & MSG_FORMAT_SYSLOG, | |
e80c1a9d | 2397 | printk_time, text + len, sizeof(text) - len); |
6fe29354 TH |
2398 | if (nr_ext_console_drivers) { |
2399 | ext_len = msg_print_ext_header(ext_text, | |
2400 | sizeof(ext_text), | |
5aa068ea | 2401 | msg, console_seq); |
6fe29354 TH |
2402 | ext_len += msg_print_ext_body(ext_text + ext_len, |
2403 | sizeof(ext_text) - ext_len, | |
2404 | log_dict(msg), msg->dict_len, | |
2405 | log_text(msg), msg->text_len); | |
2406 | } | |
7ff9554b KS |
2407 | console_idx = log_next(console_idx); |
2408 | console_seq++; | |
07354eb1 | 2409 | raw_spin_unlock(&logbuf_lock); |
7ff9554b | 2410 | |
dbdda842 SRV |
2411 | /* |
2412 | * While actively printing out messages, if another printk() | |
2413 | * were to occur on another CPU, it may wait for this one to | |
2414 | * finish. This task can not be preempted if there is a | |
2415 | * waiter waiting to take over. | |
2416 | */ | |
c162d5b4 | 2417 | console_lock_spinning_enable(); |
dbdda842 | 2418 | |
81d68a96 | 2419 | stop_critical_timings(); /* don't trace print latency */ |
d9c23523 | 2420 | call_console_drivers(ext_text, ext_len, text, len); |
81d68a96 | 2421 | start_critical_timings(); |
dbdda842 | 2422 | |
c162d5b4 PM |
2423 | if (console_lock_spinning_disable_and_check()) { |
2424 | printk_safe_exit_irqrestore(flags); | |
43a17111 | 2425 | return; |
c162d5b4 | 2426 | } |
dbdda842 | 2427 | |
f975237b | 2428 | printk_safe_exit_irqrestore(flags); |
8d91f8b1 TH |
2429 | |
2430 | if (do_cond_resched) | |
2431 | cond_resched(); | |
1da177e4 | 2432 | } |
dbdda842 | 2433 | |
1da177e4 | 2434 | console_locked = 0; |
fe3d8ad3 | 2435 | |
07354eb1 | 2436 | raw_spin_unlock(&logbuf_lock); |
4f2a8d3c | 2437 | |
bd8d7cf5 | 2438 | up_console_sem(); |
4f2a8d3c PZ |
2439 | |
2440 | /* | |
2441 | * Someone could have filled up the buffer again, so re-check if there's | |
2442 | * something to flush. In case we cannot trylock the console_sem again, | |
2443 | * there's a new owner and the console_unlock() from them will do the | |
2444 | * flush, no worries. | |
2445 | */ | |
07354eb1 | 2446 | raw_spin_lock(&logbuf_lock); |
7ff9554b | 2447 | retry = console_seq != log_next_seq; |
f975237b SS |
2448 | raw_spin_unlock(&logbuf_lock); |
2449 | printk_safe_exit_irqrestore(flags); | |
09dc3cf9 | 2450 | |
4f2a8d3c PZ |
2451 | if (retry && console_trylock()) |
2452 | goto again; | |
1da177e4 | 2453 | } |
ac751efa | 2454 | EXPORT_SYMBOL(console_unlock); |
1da177e4 | 2455 | |
ddad86c2 MW |
2456 | /** |
2457 | * console_conditional_schedule - yield the CPU if required | |
1da177e4 LT |
2458 | * |
2459 | * If the console code is currently allowed to sleep, and | |
2460 | * if this CPU should yield the CPU to another task, do | |
2461 | * so here. | |
2462 | * | |
ac751efa | 2463 | * Must be called within console_lock();. |
1da177e4 LT |
2464 | */ |
2465 | void __sched console_conditional_schedule(void) | |
2466 | { | |
2467 | if (console_may_schedule) | |
2468 | cond_resched(); | |
2469 | } | |
2470 | EXPORT_SYMBOL(console_conditional_schedule); | |
2471 | ||
1da177e4 LT |
2472 | void console_unblank(void) |
2473 | { | |
2474 | struct console *c; | |
2475 | ||
2476 | /* | |
2477 | * console_unblank can no longer be called in interrupt context unless | |
2478 | * oops_in_progress is set to 1.. | |
2479 | */ | |
2480 | if (oops_in_progress) { | |
bd8d7cf5 | 2481 | if (down_trylock_console_sem() != 0) |
1da177e4 LT |
2482 | return; |
2483 | } else | |
ac751efa | 2484 | console_lock(); |
1da177e4 LT |
2485 | |
2486 | console_locked = 1; | |
2487 | console_may_schedule = 0; | |
4d091611 | 2488 | for_each_console(c) |
1da177e4 LT |
2489 | if ((c->flags & CON_ENABLED) && c->unblank) |
2490 | c->unblank(); | |
ac751efa | 2491 | console_unlock(); |
1da177e4 | 2492 | } |
1da177e4 | 2493 | |
8d91f8b1 TH |
2494 | /** |
2495 | * console_flush_on_panic - flush console content on panic | |
2496 | * | |
2497 | * Immediately output all pending messages no matter what. | |
2498 | */ | |
2499 | void console_flush_on_panic(void) | |
2500 | { | |
2501 | /* | |
2502 | * If someone else is holding the console lock, trylock will fail | |
2503 | * and may_schedule may be set. Ignore and proceed to unlock so | |
2504 | * that messages are flushed out. As this can be called from any | |
2505 | * context and we don't want to get preempted while flushing, | |
2506 | * ensure may_schedule is cleared. | |
2507 | */ | |
2508 | console_trylock(); | |
2509 | console_may_schedule = 0; | |
2510 | console_unlock(); | |
2511 | } | |
2512 | ||
1da177e4 LT |
2513 | /* |
2514 | * Return the console tty driver structure and its associated index | |
2515 | */ | |
2516 | struct tty_driver *console_device(int *index) | |
2517 | { | |
2518 | struct console *c; | |
2519 | struct tty_driver *driver = NULL; | |
2520 | ||
ac751efa | 2521 | console_lock(); |
4d091611 | 2522 | for_each_console(c) { |
1da177e4 LT |
2523 | if (!c->device) |
2524 | continue; | |
2525 | driver = c->device(c, index); | |
2526 | if (driver) | |
2527 | break; | |
2528 | } | |
ac751efa | 2529 | console_unlock(); |
1da177e4 LT |
2530 | return driver; |
2531 | } | |
2532 | ||
2533 | /* | |
2534 | * Prevent further output on the passed console device so that (for example) | |
2535 | * serial drivers can disable console output before suspending a port, and can | |
2536 | * re-enable output afterwards. | |
2537 | */ | |
2538 | void console_stop(struct console *console) | |
2539 | { | |
ac751efa | 2540 | console_lock(); |
1da177e4 | 2541 | console->flags &= ~CON_ENABLED; |
ac751efa | 2542 | console_unlock(); |
1da177e4 LT |
2543 | } |
2544 | EXPORT_SYMBOL(console_stop); | |
2545 | ||
2546 | void console_start(struct console *console) | |
2547 | { | |
ac751efa | 2548 | console_lock(); |
1da177e4 | 2549 | console->flags |= CON_ENABLED; |
ac751efa | 2550 | console_unlock(); |
1da177e4 LT |
2551 | } |
2552 | EXPORT_SYMBOL(console_start); | |
2553 | ||
7bf69395 FDN |
2554 | static int __read_mostly keep_bootcon; |
2555 | ||
2556 | static int __init keep_bootcon_setup(char *str) | |
2557 | { | |
2558 | keep_bootcon = 1; | |
27083bac | 2559 | pr_info("debug: skip boot console de-registration.\n"); |
7bf69395 FDN |
2560 | |
2561 | return 0; | |
2562 | } | |
2563 | ||
2564 | early_param("keep_bootcon", keep_bootcon_setup); | |
2565 | ||
1da177e4 LT |
2566 | /* |
2567 | * The console driver calls this routine during kernel initialization | |
2568 | * to register the console printing procedure with printk() and to | |
2569 | * print any messages that were printed by the kernel before the | |
2570 | * console driver was initialized. | |
4d091611 RG |
2571 | * |
2572 | * This can happen pretty early during the boot process (because of | |
2573 | * early_printk) - sometimes before setup_arch() completes - be careful | |
2574 | * of what kernel features are used - they may not be initialised yet. | |
2575 | * | |
2576 | * There are two types of consoles - bootconsoles (early_printk) and | |
2577 | * "real" consoles (everything which is not a bootconsole) which are | |
2578 | * handled differently. | |
2579 | * - Any number of bootconsoles can be registered at any time. | |
2580 | * - As soon as a "real" console is registered, all bootconsoles | |
2581 | * will be unregistered automatically. | |
2582 | * - Once a "real" console is registered, any attempt to register a | |
2583 | * bootconsoles will be rejected | |
1da177e4 | 2584 | */ |
4d091611 | 2585 | void register_console(struct console *newcon) |
1da177e4 | 2586 | { |
40dc5651 | 2587 | int i; |
1da177e4 | 2588 | unsigned long flags; |
4d091611 | 2589 | struct console *bcon = NULL; |
23475408 | 2590 | struct console_cmdline *c; |
b077bafa | 2591 | static bool has_preferred; |
1da177e4 | 2592 | |
16cf48a6 AB |
2593 | if (console_drivers) |
2594 | for_each_console(bcon) | |
2595 | if (WARN(bcon == newcon, | |
2596 | "console '%s%d' already registered\n", | |
2597 | bcon->name, bcon->index)) | |
2598 | return; | |
2599 | ||
4d091611 RG |
2600 | /* |
2601 | * before we register a new CON_BOOT console, make sure we don't | |
2602 | * already have a valid console | |
2603 | */ | |
2604 | if (console_drivers && newcon->flags & CON_BOOT) { | |
2605 | /* find the last or real console */ | |
2606 | for_each_console(bcon) { | |
2607 | if (!(bcon->flags & CON_BOOT)) { | |
27083bac | 2608 | pr_info("Too late to register bootconsole %s%d\n", |
4d091611 RG |
2609 | newcon->name, newcon->index); |
2610 | return; | |
2611 | } | |
2612 | } | |
69331af7 GH |
2613 | } |
2614 | ||
4d091611 RG |
2615 | if (console_drivers && console_drivers->flags & CON_BOOT) |
2616 | bcon = console_drivers; | |
2617 | ||
b077bafa | 2618 | if (!has_preferred || bcon || !console_drivers) |
ad86ee2b | 2619 | has_preferred = preferred_console >= 0; |
1da177e4 LT |
2620 | |
2621 | /* | |
2622 | * See if we want to use this console driver. If we | |
2623 | * didn't select a console we take the first one | |
2624 | * that registers here. | |
2625 | */ | |
b077bafa | 2626 | if (!has_preferred) { |
4d091611 RG |
2627 | if (newcon->index < 0) |
2628 | newcon->index = 0; | |
2629 | if (newcon->setup == NULL || | |
2630 | newcon->setup(newcon, NULL) == 0) { | |
2631 | newcon->flags |= CON_ENABLED; | |
2632 | if (newcon->device) { | |
2633 | newcon->flags |= CON_CONSDEV; | |
b077bafa | 2634 | has_preferred = true; |
cd3a1b85 | 2635 | } |
1da177e4 LT |
2636 | } |
2637 | } | |
2638 | ||
2639 | /* | |
dac8bbba PM |
2640 | * See if this console matches one we selected on |
2641 | * the command line. | |
1da177e4 | 2642 | */ |
dac8bbba PM |
2643 | for (i = 0, c = console_cmdline; |
2644 | i < MAX_CMDLINECONSOLES && c->name[0]; | |
2645 | i++, c++) { | |
c7cef0a8 PH |
2646 | if (!newcon->match || |
2647 | newcon->match(newcon, c->name, c->index, c->options) != 0) { | |
2648 | /* default matching */ | |
2649 | BUILD_BUG_ON(sizeof(c->name) != sizeof(newcon->name)); | |
2650 | if (strcmp(c->name, newcon->name) != 0) | |
2651 | continue; | |
2652 | if (newcon->index >= 0 && | |
2653 | newcon->index != c->index) | |
2654 | continue; | |
2655 | if (newcon->index < 0) | |
2656 | newcon->index = c->index; | |
bbeddf52 | 2657 | |
c7cef0a8 PH |
2658 | if (_braille_register_console(newcon, c)) |
2659 | return; | |
2660 | ||
2661 | if (newcon->setup && | |
2662 | newcon->setup(newcon, c->options) != 0) | |
2663 | break; | |
2664 | } | |
bbeddf52 | 2665 | |
4d091611 | 2666 | newcon->flags |= CON_ENABLED; |
ad86ee2b | 2667 | if (i == preferred_console) { |
4d091611 | 2668 | newcon->flags |= CON_CONSDEV; |
b077bafa | 2669 | has_preferred = true; |
ab4af03a | 2670 | } |
1da177e4 LT |
2671 | break; |
2672 | } | |
2673 | ||
4d091611 | 2674 | if (!(newcon->flags & CON_ENABLED)) |
1da177e4 LT |
2675 | return; |
2676 | ||
8259cf43 RG |
2677 | /* |
2678 | * If we have a bootconsole, and are switching to a real console, | |
2679 | * don't print everything out again, since when the boot console, and | |
2680 | * the real console are the same physical device, it's annoying to | |
2681 | * see the beginning boot messages twice | |
2682 | */ | |
2683 | if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV)) | |
4d091611 | 2684 | newcon->flags &= ~CON_PRINTBUFFER; |
1da177e4 LT |
2685 | |
2686 | /* | |
2687 | * Put this console in the list - keep the | |
2688 | * preferred driver at the head of the list. | |
2689 | */ | |
ac751efa | 2690 | console_lock(); |
4d091611 RG |
2691 | if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) { |
2692 | newcon->next = console_drivers; | |
2693 | console_drivers = newcon; | |
2694 | if (newcon->next) | |
2695 | newcon->next->flags &= ~CON_CONSDEV; | |
1da177e4 | 2696 | } else { |
4d091611 RG |
2697 | newcon->next = console_drivers->next; |
2698 | console_drivers->next = newcon; | |
1da177e4 | 2699 | } |
6fe29354 TH |
2700 | |
2701 | if (newcon->flags & CON_EXTENDED) | |
9627808d | 2702 | nr_ext_console_drivers++; |
6fe29354 | 2703 | |
4d091611 | 2704 | if (newcon->flags & CON_PRINTBUFFER) { |
1da177e4 | 2705 | /* |
ac751efa | 2706 | * console_unlock(); will print out the buffered messages |
1da177e4 LT |
2707 | * for us. |
2708 | */ | |
de6fcbdb | 2709 | logbuf_lock_irqsave(flags); |
7ff9554b KS |
2710 | console_seq = syslog_seq; |
2711 | console_idx = syslog_idx; | |
fe3d8ad3 FT |
2712 | /* |
2713 | * We're about to replay the log buffer. Only do this to the | |
2714 | * just-registered console to avoid excessive message spam to | |
2715 | * the already-registered consoles. | |
884e370e SS |
2716 | * |
2717 | * Set exclusive_console with disabled interrupts to reduce | |
2718 | * race window with eventual console_flush_on_panic() that | |
2719 | * ignores console_lock. | |
fe3d8ad3 FT |
2720 | */ |
2721 | exclusive_console = newcon; | |
f92b070f | 2722 | exclusive_console_stop_seq = console_seq; |
884e370e | 2723 | logbuf_unlock_irqrestore(flags); |
1da177e4 | 2724 | } |
ac751efa | 2725 | console_unlock(); |
fbc92a34 | 2726 | console_sysfs_notify(); |
8259cf43 RG |
2727 | |
2728 | /* | |
2729 | * By unregistering the bootconsoles after we enable the real console | |
2730 | * we get the "console xxx enabled" message on all the consoles - | |
2731 | * boot consoles, real consoles, etc - this is to ensure that end | |
2732 | * users know there might be something in the kernel's log buffer that | |
2733 | * went to the bootconsole (that they do not see on the real console) | |
2734 | */ | |
27083bac | 2735 | pr_info("%sconsole [%s%d] enabled\n", |
6b802394 KC |
2736 | (newcon->flags & CON_BOOT) ? "boot" : "" , |
2737 | newcon->name, newcon->index); | |
7bf69395 FDN |
2738 | if (bcon && |
2739 | ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) && | |
2740 | !keep_bootcon) { | |
6b802394 KC |
2741 | /* We need to iterate through all boot consoles, to make |
2742 | * sure we print everything out, before we unregister them. | |
8259cf43 | 2743 | */ |
8259cf43 RG |
2744 | for_each_console(bcon) |
2745 | if (bcon->flags & CON_BOOT) | |
2746 | unregister_console(bcon); | |
8259cf43 | 2747 | } |
1da177e4 LT |
2748 | } |
2749 | EXPORT_SYMBOL(register_console); | |
2750 | ||
40dc5651 | 2751 | int unregister_console(struct console *console) |
1da177e4 | 2752 | { |
40dc5651 | 2753 | struct console *a, *b; |
bbeddf52 | 2754 | int res; |
1da177e4 | 2755 | |
27083bac | 2756 | pr_info("%sconsole [%s%d] disabled\n", |
6b802394 KC |
2757 | (console->flags & CON_BOOT) ? "boot" : "" , |
2758 | console->name, console->index); | |
2759 | ||
bbeddf52 JP |
2760 | res = _braille_unregister_console(console); |
2761 | if (res) | |
2762 | return res; | |
f7511d5f | 2763 | |
bbeddf52 | 2764 | res = 1; |
ac751efa | 2765 | console_lock(); |
1da177e4 LT |
2766 | if (console_drivers == console) { |
2767 | console_drivers=console->next; | |
2768 | res = 0; | |
e9b15b54 | 2769 | } else if (console_drivers) { |
1da177e4 LT |
2770 | for (a=console_drivers->next, b=console_drivers ; |
2771 | a; b=a, a=b->next) { | |
2772 | if (a == console) { | |
2773 | b->next = a->next; | |
2774 | res = 0; | |
2775 | break; | |
40dc5651 | 2776 | } |
1da177e4 LT |
2777 | } |
2778 | } | |
40dc5651 | 2779 | |
6fe29354 TH |
2780 | if (!res && (console->flags & CON_EXTENDED)) |
2781 | nr_ext_console_drivers--; | |
2782 | ||
69331af7 | 2783 | /* |
ab4af03a GE |
2784 | * If this isn't the last console and it has CON_CONSDEV set, we |
2785 | * need to set it on the next preferred console. | |
1da177e4 | 2786 | */ |
69331af7 | 2787 | if (console_drivers != NULL && console->flags & CON_CONSDEV) |
ab4af03a | 2788 | console_drivers->flags |= CON_CONSDEV; |
1da177e4 | 2789 | |
7fa21dd8 | 2790 | console->flags &= ~CON_ENABLED; |
ac751efa | 2791 | console_unlock(); |
fbc92a34 | 2792 | console_sysfs_notify(); |
1da177e4 LT |
2793 | return res; |
2794 | } | |
2795 | EXPORT_SYMBOL(unregister_console); | |
d59745ce | 2796 | |
0c688614 NP |
2797 | /* |
2798 | * Initialize the console device. This is called *early*, so | |
2799 | * we can't necessarily depend on lots of kernel help here. | |
2800 | * Just do some early initializations, and do the complex setup | |
2801 | * later. | |
2802 | */ | |
2803 | void __init console_init(void) | |
2804 | { | |
58eacfff | 2805 | int ret; |
1b1eeca7 AB |
2806 | initcall_t call; |
2807 | initcall_entry_t *ce; | |
0c688614 NP |
2808 | |
2809 | /* Setup the default TTY line discipline. */ | |
2810 | n_tty_init(); | |
2811 | ||
2812 | /* | |
2813 | * set up the console device so that later boot sequences can | |
2814 | * inform about problems etc.. | |
2815 | */ | |
1b1eeca7 | 2816 | ce = __con_initcall_start; |
58eacfff | 2817 | trace_initcall_level("console"); |
1b1eeca7 AB |
2818 | while (ce < __con_initcall_end) { |
2819 | call = initcall_from_entry(ce); | |
2820 | trace_initcall_start(call); | |
2821 | ret = call(); | |
2822 | trace_initcall_finish(call, ret); | |
2823 | ce++; | |
0c688614 NP |
2824 | } |
2825 | } | |
2826 | ||
81cc26f2 TR |
2827 | /* |
2828 | * Some boot consoles access data that is in the init section and which will | |
2829 | * be discarded after the initcalls have been run. To make sure that no code | |
2830 | * will access this data, unregister the boot consoles in a late initcall. | |
2831 | * | |
2832 | * If for some reason, such as deferred probe or the driver being a loadable | |
2833 | * module, the real console hasn't registered yet at this point, there will | |
2834 | * be a brief interval in which no messages are logged to the console, which | |
2835 | * makes it difficult to diagnose problems that occur during this time. | |
2836 | * | |
2837 | * To mitigate this problem somewhat, only unregister consoles whose memory | |
2b1be689 MR |
2838 | * intersects with the init section. Note that all other boot consoles will |
2839 | * get unregistred when the real preferred console is registered. | |
81cc26f2 | 2840 | */ |
034260d6 | 2841 | static int __init printk_late_init(void) |
0c5564bd | 2842 | { |
4d091611 | 2843 | struct console *con; |
90b14889 | 2844 | int ret; |
4d091611 RG |
2845 | |
2846 | for_each_console(con) { | |
5a814231 PM |
2847 | if (!(con->flags & CON_BOOT)) |
2848 | continue; | |
2849 | ||
2850 | /* Check addresses that might be used for enabled consoles. */ | |
2851 | if (init_section_intersects(con, sizeof(*con)) || | |
2852 | init_section_contains(con->write, 0) || | |
2853 | init_section_contains(con->read, 0) || | |
2854 | init_section_contains(con->device, 0) || | |
2855 | init_section_contains(con->unblank, 0) || | |
2856 | init_section_contains(con->data, 0)) { | |
81cc26f2 | 2857 | /* |
2b1be689 MR |
2858 | * Please, consider moving the reported consoles out |
2859 | * of the init section. | |
81cc26f2 | 2860 | */ |
2b1be689 MR |
2861 | pr_warn("bootconsole [%s%d] uses init memory and must be disabled even before the real one is ready\n", |
2862 | con->name, con->index); | |
2863 | unregister_console(con); | |
cb00e99c | 2864 | } |
0c5564bd | 2865 | } |
90b14889 SAS |
2866 | ret = cpuhp_setup_state_nocalls(CPUHP_PRINTK_DEAD, "printk:dead", NULL, |
2867 | console_cpu_notify); | |
2868 | WARN_ON(ret < 0); | |
2869 | ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "printk:online", | |
2870 | console_cpu_notify, NULL); | |
2871 | WARN_ON(ret < 0); | |
0c5564bd RG |
2872 | return 0; |
2873 | } | |
034260d6 | 2874 | late_initcall(printk_late_init); |
0c5564bd | 2875 | |
7ef3d2fd | 2876 | #if defined CONFIG_PRINTK |
dc72c32e FW |
2877 | /* |
2878 | * Delayed printk version, for scheduler-internal messages: | |
2879 | */ | |
dc72c32e | 2880 | #define PRINTK_PENDING_WAKEUP 0x01 |
458df9fd | 2881 | #define PRINTK_PENDING_OUTPUT 0x02 |
dc72c32e FW |
2882 | |
2883 | static DEFINE_PER_CPU(int, printk_pending); | |
dc72c32e FW |
2884 | |
2885 | static void wake_up_klogd_work_func(struct irq_work *irq_work) | |
2886 | { | |
2887 | int pending = __this_cpu_xchg(printk_pending, 0); | |
2888 | ||
458df9fd SR |
2889 | if (pending & PRINTK_PENDING_OUTPUT) { |
2890 | /* If trylock fails, someone else is doing the printing */ | |
2891 | if (console_trylock()) | |
2892 | console_unlock(); | |
dc72c32e FW |
2893 | } |
2894 | ||
2895 | if (pending & PRINTK_PENDING_WAKEUP) | |
2896 | wake_up_interruptible(&log_wait); | |
2897 | } | |
2898 | ||
2899 | static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) = { | |
2900 | .func = wake_up_klogd_work_func, | |
2901 | .flags = IRQ_WORK_LAZY, | |
2902 | }; | |
2903 | ||
2904 | void wake_up_klogd(void) | |
2905 | { | |
2906 | preempt_disable(); | |
2907 | if (waitqueue_active(&log_wait)) { | |
2908 | this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP); | |
bb964a92 | 2909 | irq_work_queue(this_cpu_ptr(&wake_up_klogd_work)); |
dc72c32e FW |
2910 | } |
2911 | preempt_enable(); | |
2912 | } | |
717115e1 | 2913 | |
a338f84d | 2914 | void defer_console_output(void) |
600e1458 | 2915 | { |
719f6a70 | 2916 | preempt_disable(); |
458df9fd | 2917 | __this_cpu_or(printk_pending, PRINTK_PENDING_OUTPUT); |
bb964a92 | 2918 | irq_work_queue(this_cpu_ptr(&wake_up_klogd_work)); |
81954606 | 2919 | preempt_enable(); |
a338f84d PM |
2920 | } |
2921 | ||
2922 | int vprintk_deferred(const char *fmt, va_list args) | |
2923 | { | |
2924 | int r; | |
2925 | ||
2926 | r = vprintk_emit(0, LOGLEVEL_SCHED, NULL, 0, fmt, args); | |
2927 | defer_console_output(); | |
600e1458 PZ |
2928 | |
2929 | return r; | |
2930 | } | |
2931 | ||
719f6a70 PM |
2932 | int printk_deferred(const char *fmt, ...) |
2933 | { | |
2934 | va_list args; | |
2935 | int r; | |
2936 | ||
2937 | va_start(args, fmt); | |
2938 | r = vprintk_deferred(fmt, args); | |
2939 | va_end(args); | |
2940 | ||
2941 | return r; | |
2942 | } | |
2943 | ||
1da177e4 LT |
2944 | /* |
2945 | * printk rate limiting, lifted from the networking subsystem. | |
2946 | * | |
641de9d8 UKK |
2947 | * This enforces a rate limit: not more than 10 kernel messages |
2948 | * every 5s to make a denial-of-service attack impossible. | |
1da177e4 | 2949 | */ |
641de9d8 UKK |
2950 | DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10); |
2951 | ||
5c828713 | 2952 | int __printk_ratelimit(const char *func) |
1da177e4 | 2953 | { |
5c828713 | 2954 | return ___ratelimit(&printk_ratelimit_state, func); |
1da177e4 | 2955 | } |
5c828713 | 2956 | EXPORT_SYMBOL(__printk_ratelimit); |
f46c4833 AM |
2957 | |
2958 | /** | |
2959 | * printk_timed_ratelimit - caller-controlled printk ratelimiting | |
2960 | * @caller_jiffies: pointer to caller's state | |
2961 | * @interval_msecs: minimum interval between prints | |
2962 | * | |
2963 | * printk_timed_ratelimit() returns true if more than @interval_msecs | |
2964 | * milliseconds have elapsed since the last time printk_timed_ratelimit() | |
2965 | * returned true. | |
2966 | */ | |
2967 | bool printk_timed_ratelimit(unsigned long *caller_jiffies, | |
2968 | unsigned int interval_msecs) | |
2969 | { | |
249771b8 AE |
2970 | unsigned long elapsed = jiffies - *caller_jiffies; |
2971 | ||
2972 | if (*caller_jiffies && elapsed <= msecs_to_jiffies(interval_msecs)) | |
2973 | return false; | |
2974 | ||
2975 | *caller_jiffies = jiffies; | |
2976 | return true; | |
f46c4833 AM |
2977 | } |
2978 | EXPORT_SYMBOL(printk_timed_ratelimit); | |
456b565c SK |
2979 | |
2980 | static DEFINE_SPINLOCK(dump_list_lock); | |
2981 | static LIST_HEAD(dump_list); | |
2982 | ||
2983 | /** | |
2984 | * kmsg_dump_register - register a kernel log dumper. | |
6485536b | 2985 | * @dumper: pointer to the kmsg_dumper structure |
456b565c SK |
2986 | * |
2987 | * Adds a kernel log dumper to the system. The dump callback in the | |
2988 | * structure will be called when the kernel oopses or panics and must be | |
2989 | * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise. | |
2990 | */ | |
2991 | int kmsg_dump_register(struct kmsg_dumper *dumper) | |
2992 | { | |
2993 | unsigned long flags; | |
2994 | int err = -EBUSY; | |
2995 | ||
2996 | /* The dump callback needs to be set */ | |
2997 | if (!dumper->dump) | |
2998 | return -EINVAL; | |
2999 | ||
3000 | spin_lock_irqsave(&dump_list_lock, flags); | |
3001 | /* Don't allow registering multiple times */ | |
3002 | if (!dumper->registered) { | |
3003 | dumper->registered = 1; | |
fb842b00 | 3004 | list_add_tail_rcu(&dumper->list, &dump_list); |
456b565c SK |
3005 | err = 0; |
3006 | } | |
3007 | spin_unlock_irqrestore(&dump_list_lock, flags); | |
3008 | ||
3009 | return err; | |
3010 | } | |
3011 | EXPORT_SYMBOL_GPL(kmsg_dump_register); | |
3012 | ||
3013 | /** | |
3014 | * kmsg_dump_unregister - unregister a kmsg dumper. | |
6485536b | 3015 | * @dumper: pointer to the kmsg_dumper structure |
456b565c SK |
3016 | * |
3017 | * Removes a dump device from the system. Returns zero on success and | |
3018 | * %-EINVAL otherwise. | |
3019 | */ | |
3020 | int kmsg_dump_unregister(struct kmsg_dumper *dumper) | |
3021 | { | |
3022 | unsigned long flags; | |
3023 | int err = -EINVAL; | |
3024 | ||
3025 | spin_lock_irqsave(&dump_list_lock, flags); | |
3026 | if (dumper->registered) { | |
3027 | dumper->registered = 0; | |
fb842b00 | 3028 | list_del_rcu(&dumper->list); |
456b565c SK |
3029 | err = 0; |
3030 | } | |
3031 | spin_unlock_irqrestore(&dump_list_lock, flags); | |
fb842b00 | 3032 | synchronize_rcu(); |
456b565c SK |
3033 | |
3034 | return err; | |
3035 | } | |
3036 | EXPORT_SYMBOL_GPL(kmsg_dump_unregister); | |
3037 | ||
7ff9554b KS |
3038 | static bool always_kmsg_dump; |
3039 | module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR); | |
3040 | ||
456b565c SK |
3041 | /** |
3042 | * kmsg_dump - dump kernel log to kernel message dumpers. | |
3043 | * @reason: the reason (oops, panic etc) for dumping | |
3044 | * | |
e2ae715d KS |
3045 | * Call each of the registered dumper's dump() callback, which can |
3046 | * retrieve the kmsg records with kmsg_dump_get_line() or | |
3047 | * kmsg_dump_get_buffer(). | |
456b565c SK |
3048 | */ |
3049 | void kmsg_dump(enum kmsg_dump_reason reason) | |
3050 | { | |
456b565c | 3051 | struct kmsg_dumper *dumper; |
456b565c SK |
3052 | unsigned long flags; |
3053 | ||
c22ab332 MG |
3054 | if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump) |
3055 | return; | |
3056 | ||
e2ae715d KS |
3057 | rcu_read_lock(); |
3058 | list_for_each_entry_rcu(dumper, &dump_list, list) { | |
3059 | if (dumper->max_reason && reason > dumper->max_reason) | |
3060 | continue; | |
3061 | ||
3062 | /* initialize iterator with data about the stored records */ | |
3063 | dumper->active = true; | |
3064 | ||
de6fcbdb | 3065 | logbuf_lock_irqsave(flags); |
e2ae715d KS |
3066 | dumper->cur_seq = clear_seq; |
3067 | dumper->cur_idx = clear_idx; | |
3068 | dumper->next_seq = log_next_seq; | |
3069 | dumper->next_idx = log_next_idx; | |
de6fcbdb | 3070 | logbuf_unlock_irqrestore(flags); |
e2ae715d KS |
3071 | |
3072 | /* invoke dumper which will iterate over records */ | |
3073 | dumper->dump(dumper, reason); | |
3074 | ||
3075 | /* reset iterator */ | |
3076 | dumper->active = false; | |
3077 | } | |
3078 | rcu_read_unlock(); | |
3079 | } | |
3080 | ||
3081 | /** | |
533827c9 | 3082 | * kmsg_dump_get_line_nolock - retrieve one kmsg log line (unlocked version) |
e2ae715d KS |
3083 | * @dumper: registered kmsg dumper |
3084 | * @syslog: include the "<4>" prefixes | |
3085 | * @line: buffer to copy the line to | |
3086 | * @size: maximum size of the buffer | |
3087 | * @len: length of line placed into buffer | |
3088 | * | |
3089 | * Start at the beginning of the kmsg buffer, with the oldest kmsg | |
3090 | * record, and copy one record into the provided buffer. | |
3091 | * | |
3092 | * Consecutive calls will return the next available record moving | |
3093 | * towards the end of the buffer with the youngest messages. | |
3094 | * | |
3095 | * A return value of FALSE indicates that there are no more records to | |
3096 | * read. | |
533827c9 AV |
3097 | * |
3098 | * The function is similar to kmsg_dump_get_line(), but grabs no locks. | |
e2ae715d | 3099 | */ |
533827c9 AV |
3100 | bool kmsg_dump_get_line_nolock(struct kmsg_dumper *dumper, bool syslog, |
3101 | char *line, size_t size, size_t *len) | |
e2ae715d | 3102 | { |
62e32ac3 | 3103 | struct printk_log *msg; |
e2ae715d KS |
3104 | size_t l = 0; |
3105 | bool ret = false; | |
3106 | ||
3107 | if (!dumper->active) | |
3108 | goto out; | |
7ff9554b | 3109 | |
e2ae715d KS |
3110 | if (dumper->cur_seq < log_first_seq) { |
3111 | /* messages are gone, move to first available one */ | |
3112 | dumper->cur_seq = log_first_seq; | |
3113 | dumper->cur_idx = log_first_idx; | |
3114 | } | |
456b565c | 3115 | |
e2ae715d | 3116 | /* last entry */ |
533827c9 | 3117 | if (dumper->cur_seq >= log_next_seq) |
e2ae715d | 3118 | goto out; |
456b565c | 3119 | |
e2ae715d | 3120 | msg = log_from_idx(dumper->cur_idx); |
e80c1a9d | 3121 | l = msg_print_text(msg, syslog, printk_time, line, size); |
e2ae715d KS |
3122 | |
3123 | dumper->cur_idx = log_next(dumper->cur_idx); | |
3124 | dumper->cur_seq++; | |
3125 | ret = true; | |
e2ae715d KS |
3126 | out: |
3127 | if (len) | |
3128 | *len = l; | |
3129 | return ret; | |
3130 | } | |
533827c9 AV |
3131 | |
3132 | /** | |
3133 | * kmsg_dump_get_line - retrieve one kmsg log line | |
3134 | * @dumper: registered kmsg dumper | |
3135 | * @syslog: include the "<4>" prefixes | |
3136 | * @line: buffer to copy the line to | |
3137 | * @size: maximum size of the buffer | |
3138 | * @len: length of line placed into buffer | |
3139 | * | |
3140 | * Start at the beginning of the kmsg buffer, with the oldest kmsg | |
3141 | * record, and copy one record into the provided buffer. | |
3142 | * | |
3143 | * Consecutive calls will return the next available record moving | |
3144 | * towards the end of the buffer with the youngest messages. | |
3145 | * | |
3146 | * A return value of FALSE indicates that there are no more records to | |
3147 | * read. | |
3148 | */ | |
3149 | bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog, | |
3150 | char *line, size_t size, size_t *len) | |
3151 | { | |
3152 | unsigned long flags; | |
3153 | bool ret; | |
3154 | ||
de6fcbdb | 3155 | logbuf_lock_irqsave(flags); |
533827c9 | 3156 | ret = kmsg_dump_get_line_nolock(dumper, syslog, line, size, len); |
de6fcbdb | 3157 | logbuf_unlock_irqrestore(flags); |
533827c9 AV |
3158 | |
3159 | return ret; | |
3160 | } | |
e2ae715d KS |
3161 | EXPORT_SYMBOL_GPL(kmsg_dump_get_line); |
3162 | ||
3163 | /** | |
3164 | * kmsg_dump_get_buffer - copy kmsg log lines | |
3165 | * @dumper: registered kmsg dumper | |
3166 | * @syslog: include the "<4>" prefixes | |
4f0f4af5 | 3167 | * @buf: buffer to copy the line to |
e2ae715d KS |
3168 | * @size: maximum size of the buffer |
3169 | * @len: length of line placed into buffer | |
3170 | * | |
3171 | * Start at the end of the kmsg buffer and fill the provided buffer | |
3172 | * with as many of the the *youngest* kmsg records that fit into it. | |
3173 | * If the buffer is large enough, all available kmsg records will be | |
3174 | * copied with a single call. | |
3175 | * | |
3176 | * Consecutive calls will fill the buffer with the next block of | |
3177 | * available older records, not including the earlier retrieved ones. | |
3178 | * | |
3179 | * A return value of FALSE indicates that there are no more records to | |
3180 | * read. | |
3181 | */ | |
3182 | bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog, | |
3183 | char *buf, size_t size, size_t *len) | |
3184 | { | |
3185 | unsigned long flags; | |
3186 | u64 seq; | |
3187 | u32 idx; | |
3188 | u64 next_seq; | |
3189 | u32 next_idx; | |
3190 | size_t l = 0; | |
3191 | bool ret = false; | |
e80c1a9d | 3192 | bool time = printk_time; |
e2ae715d KS |
3193 | |
3194 | if (!dumper->active) | |
3195 | goto out; | |
3196 | ||
de6fcbdb | 3197 | logbuf_lock_irqsave(flags); |
e2ae715d KS |
3198 | if (dumper->cur_seq < log_first_seq) { |
3199 | /* messages are gone, move to first available one */ | |
3200 | dumper->cur_seq = log_first_seq; | |
3201 | dumper->cur_idx = log_first_idx; | |
3202 | } | |
3203 | ||
3204 | /* last entry */ | |
3205 | if (dumper->cur_seq >= dumper->next_seq) { | |
de6fcbdb | 3206 | logbuf_unlock_irqrestore(flags); |
e2ae715d KS |
3207 | goto out; |
3208 | } | |
3209 | ||
3210 | /* calculate length of entire buffer */ | |
3211 | seq = dumper->cur_seq; | |
3212 | idx = dumper->cur_idx; | |
3213 | while (seq < dumper->next_seq) { | |
62e32ac3 | 3214 | struct printk_log *msg = log_from_idx(idx); |
e2ae715d | 3215 | |
e80c1a9d | 3216 | l += msg_print_text(msg, true, time, NULL, 0); |
e2ae715d KS |
3217 | idx = log_next(idx); |
3218 | seq++; | |
3219 | } | |
3220 | ||
3221 | /* move first record forward until length fits into the buffer */ | |
3222 | seq = dumper->cur_seq; | |
3223 | idx = dumper->cur_idx; | |
3224 | while (l > size && seq < dumper->next_seq) { | |
62e32ac3 | 3225 | struct printk_log *msg = log_from_idx(idx); |
456b565c | 3226 | |
e80c1a9d | 3227 | l -= msg_print_text(msg, true, time, NULL, 0); |
e2ae715d KS |
3228 | idx = log_next(idx); |
3229 | seq++; | |
456b565c | 3230 | } |
e2ae715d KS |
3231 | |
3232 | /* last message in next interation */ | |
3233 | next_seq = seq; | |
3234 | next_idx = idx; | |
3235 | ||
3236 | l = 0; | |
3237 | while (seq < dumper->next_seq) { | |
62e32ac3 | 3238 | struct printk_log *msg = log_from_idx(idx); |
e2ae715d | 3239 | |
e80c1a9d | 3240 | l += msg_print_text(msg, syslog, time, buf + l, size - l); |
e2ae715d KS |
3241 | idx = log_next(idx); |
3242 | seq++; | |
3243 | } | |
3244 | ||
3245 | dumper->next_seq = next_seq; | |
3246 | dumper->next_idx = next_idx; | |
3247 | ret = true; | |
de6fcbdb | 3248 | logbuf_unlock_irqrestore(flags); |
e2ae715d KS |
3249 | out: |
3250 | if (len) | |
3251 | *len = l; | |
3252 | return ret; | |
3253 | } | |
3254 | EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer); | |
456b565c | 3255 | |
533827c9 AV |
3256 | /** |
3257 | * kmsg_dump_rewind_nolock - reset the interator (unlocked version) | |
3258 | * @dumper: registered kmsg dumper | |
3259 | * | |
3260 | * Reset the dumper's iterator so that kmsg_dump_get_line() and | |
3261 | * kmsg_dump_get_buffer() can be called again and used multiple | |
3262 | * times within the same dumper.dump() callback. | |
3263 | * | |
3264 | * The function is similar to kmsg_dump_rewind(), but grabs no locks. | |
3265 | */ | |
3266 | void kmsg_dump_rewind_nolock(struct kmsg_dumper *dumper) | |
3267 | { | |
3268 | dumper->cur_seq = clear_seq; | |
3269 | dumper->cur_idx = clear_idx; | |
3270 | dumper->next_seq = log_next_seq; | |
3271 | dumper->next_idx = log_next_idx; | |
3272 | } | |
3273 | ||
e2ae715d KS |
3274 | /** |
3275 | * kmsg_dump_rewind - reset the interator | |
3276 | * @dumper: registered kmsg dumper | |
3277 | * | |
3278 | * Reset the dumper's iterator so that kmsg_dump_get_line() and | |
3279 | * kmsg_dump_get_buffer() can be called again and used multiple | |
3280 | * times within the same dumper.dump() callback. | |
3281 | */ | |
3282 | void kmsg_dump_rewind(struct kmsg_dumper *dumper) | |
3283 | { | |
3284 | unsigned long flags; | |
3285 | ||
de6fcbdb | 3286 | logbuf_lock_irqsave(flags); |
533827c9 | 3287 | kmsg_dump_rewind_nolock(dumper); |
de6fcbdb | 3288 | logbuf_unlock_irqrestore(flags); |
456b565c | 3289 | } |
e2ae715d | 3290 | EXPORT_SYMBOL_GPL(kmsg_dump_rewind); |
196779b9 | 3291 | |
7ef3d2fd | 3292 | #endif |