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