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