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