drivers: power: report battery voltage in AOSP compatible format
[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{
391f1c61 127 char name[16]; /* Name of the driver */
1da177e4
LT
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
d46354fc 1334 trace_console_rcuidle(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 2133 bool wake_klogd = false;
0ba4f4bb 2134 bool do_cond_resched, 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
1da177e4 2142
557240b4 2143 if (console_suspended) {
403f3075 2144 up(&console_sem);
557240b4
LT
2145 return;
2146 }
78944e54 2147
0ba4f4bb
TH
2148 /*
2149 * Console drivers are called under logbuf_lock, so
2150 * @console_may_schedule should be cleared before; however, we may
2151 * end up dumping a lot of lines, for example, if called from
2152 * console registration path, and should invoke cond_resched()
2153 * between lines if allowable. Not doing so can cause a very long
2154 * scheduling stall on a slow console leading to RCU stall and
2155 * softlockup warnings which exacerbate the issue with more
2156 * messages practically incapacitating the system.
2157 */
2158 do_cond_resched = console_may_schedule;
78944e54
AD
2159 console_may_schedule = 0;
2160
084681d1 2161 /* flush buffered message fragment immediately to console */
eab07260 2162 console_cont_flush(text, sizeof(text));
4f2a8d3c 2163again:
7ff9554b
KS
2164 for (;;) {
2165 struct log *msg;
3ce9a7c0 2166 size_t len;
7ff9554b
KS
2167 int level;
2168
07354eb1 2169 raw_spin_lock_irqsave(&logbuf_lock, flags);
6fa3eb70
S
2170#ifdef LOG_TOO_MUCH_WARNING /*For Resume log too much*/
2171 if (log_in_resume) {
2172 t1 = sched_clock();
2173 }
2174#endif
2175
7ff9554b
KS
2176 if (seen_seq != log_next_seq) {
2177 wake_klogd = true;
2178 seen_seq = log_next_seq;
2179 }
2180
2181 if (console_seq < log_first_seq) {
2182 /* messages are gone, move to first one */
2183 console_seq = log_first_seq;
2184 console_idx = log_first_idx;
5becfb1d 2185 console_prev = 0;
7ff9554b 2186 }
084681d1 2187skip:
7ff9554b
KS
2188 if (console_seq == log_next_seq)
2189 break;
2190
2191 msg = log_from_idx(console_idx);
084681d1
KS
2192 if (msg->flags & LOG_NOCONS) {
2193 /*
2194 * Skip record we have buffered and already printed
2195 * directly to the console when we received it.
2196 */
2197 console_idx = log_next(console_idx);
2198 console_seq++;
68b6507d
KS
2199 /*
2200 * We will get here again when we register a new
2201 * CON_PRINTBUFFER console. Clear the flag so we
2202 * will properly dump everything later.
2203 */
2204 msg->flags &= ~LOG_NOCONS;
eab07260 2205 console_prev = msg->flags;
084681d1
KS
2206 goto skip;
2207 }
649e6ee3 2208
084681d1 2209 level = msg->level;
5becfb1d
KS
2210 len = msg_print_text(msg, console_prev, false,
2211 text, sizeof(text));
7ff9554b
KS
2212 console_idx = log_next(console_idx);
2213 console_seq++;
5becfb1d 2214 console_prev = msg->flags;
07354eb1 2215 raw_spin_unlock(&logbuf_lock);
7ff9554b 2216
81d68a96 2217 stop_critical_timings(); /* don't trace print latency */
6fa3eb70
S
2218#ifdef LOG_TOO_MUCH_WARNING
2219 /*
2220 For uart console, 10us/per chars
2221 400,000 chars = need to wait 4.0 sec
2222 normal case: 4sec
2223 */
2224 if (log_in_resume) {
2225 org_loglevel = console_loglevel;
2226 console_loglevel = 4;
2227 }
2228 total_log_size += len;
2229 if (total_log_size < console_log_max)
2230 call_console_drivers(level, text, len);
2231 else if (!already_skip_log) {
2232 sprintf(aee_str, "PRINTK too much:%lu", total_log_size);
2233 aee_kernel_warning(aee_str, "Need to shrink kernel log");
2234 already_skip_log = 1;
2235 }
2236 /**/
2237 start_critical_timings();
2238 /* For Resume log too much*/
2239 if (log_in_resume) {
2240 t2 = sched_clock();
2241 console_loglevel = org_loglevel;
2242 if (t2 - t1 > 100000000) {
2243 sprintf( aee_str,"[RESUME CONSOLE too long:%lluns>100ms] s:%lluns, e:%lluns\n", t2 - t1, t1, t2);
2244 aee_kernel_warning(aee_str, "Need to shrink kernel log");
2245 }
2246 }
2247
2248 /**/
2249#else
2250 start_critical_timings();
2251 call_console_drivers(level, text, len);
2252#endif
1da177e4 2253 local_irq_restore(flags);
0ba4f4bb
TH
2254
2255 if (do_cond_resched)
2256 cond_resched();
1da177e4
LT
2257 }
2258 console_locked = 0;
daee7797 2259 mutex_release(&console_lock_dep_map, 1, _RET_IP_);
fe3d8ad3
FT
2260
2261 /* Release the exclusive_console once it is used */
2262 if (unlikely(exclusive_console))
2263 exclusive_console = NULL;
2264
07354eb1 2265 raw_spin_unlock(&logbuf_lock);
4f2a8d3c 2266
0b5e1c52 2267 up(&console_sem);
4f2a8d3c
PZ
2268
2269 /*
2270 * Someone could have filled up the buffer again, so re-check if there's
2271 * something to flush. In case we cannot trylock the console_sem again,
2272 * there's a new owner and the console_unlock() from them will do the
2273 * flush, no worries.
2274 */
07354eb1 2275 raw_spin_lock(&logbuf_lock);
7ff9554b 2276 retry = console_seq != log_next_seq;
09dc3cf9
PZ
2277 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2278
4f2a8d3c
PZ
2279 if (retry && console_trylock())
2280 goto again;
2281
e3e8a75d
KK
2282 if (wake_klogd)
2283 wake_up_klogd();
1da177e4 2284}
ac751efa 2285EXPORT_SYMBOL(console_unlock);
1da177e4 2286
ddad86c2
MW
2287/**
2288 * console_conditional_schedule - yield the CPU if required
1da177e4
LT
2289 *
2290 * If the console code is currently allowed to sleep, and
2291 * if this CPU should yield the CPU to another task, do
2292 * so here.
2293 *
ac751efa 2294 * Must be called within console_lock();.
1da177e4
LT
2295 */
2296void __sched console_conditional_schedule(void)
2297{
2298 if (console_may_schedule)
2299 cond_resched();
2300}
2301EXPORT_SYMBOL(console_conditional_schedule);
2302
1da177e4
LT
2303void console_unblank(void)
2304{
2305 struct console *c;
2306
2307 /*
2308 * console_unblank can no longer be called in interrupt context unless
2309 * oops_in_progress is set to 1..
2310 */
2311 if (oops_in_progress) {
2312 if (down_trylock(&console_sem) != 0)
2313 return;
2314 } else
ac751efa 2315 console_lock();
1da177e4
LT
2316
2317 console_locked = 1;
2318 console_may_schedule = 0;
4d091611 2319 for_each_console(c)
1da177e4
LT
2320 if ((c->flags & CON_ENABLED) && c->unblank)
2321 c->unblank();
ac751efa 2322 console_unlock();
1da177e4 2323}
1da177e4 2324
0ba4f4bb
TH
2325/**
2326 * console_flush_on_panic - flush console content on panic
2327 *
2328 * Immediately output all pending messages no matter what.
2329 */
2330void console_flush_on_panic(void)
2331{
2332 /*
2333 * If someone else is holding the console lock, trylock will fail
2334 * and may_schedule may be set. Ignore and proceed to unlock so
2335 * that messages are flushed out. As this can be called from any
2336 * context and we don't want to get preempted while flushing,
2337 * ensure may_schedule is cleared.
2338 */
2339 console_trylock();
2340 console_may_schedule = 0;
2341 console_unlock();
2342}
2343
1da177e4
LT
2344/*
2345 * Return the console tty driver structure and its associated index
2346 */
2347struct tty_driver *console_device(int *index)
2348{
2349 struct console *c;
2350 struct tty_driver *driver = NULL;
2351
ac751efa 2352 console_lock();
4d091611 2353 for_each_console(c) {
1da177e4
LT
2354 if (!c->device)
2355 continue;
2356 driver = c->device(c, index);
2357 if (driver)
2358 break;
2359 }
ac751efa 2360 console_unlock();
1da177e4
LT
2361 return driver;
2362}
2363
2364/*
2365 * Prevent further output on the passed console device so that (for example)
2366 * serial drivers can disable console output before suspending a port, and can
2367 * re-enable output afterwards.
2368 */
2369void console_stop(struct console *console)
2370{
ac751efa 2371 console_lock();
1da177e4 2372 console->flags &= ~CON_ENABLED;
ac751efa 2373 console_unlock();
1da177e4
LT
2374}
2375EXPORT_SYMBOL(console_stop);
2376
2377void console_start(struct console *console)
2378{
ac751efa 2379 console_lock();
1da177e4 2380 console->flags |= CON_ENABLED;
ac751efa 2381 console_unlock();
1da177e4
LT
2382}
2383EXPORT_SYMBOL(console_start);
2384
7bf69395
FDN
2385static int __read_mostly keep_bootcon;
2386
2387static int __init keep_bootcon_setup(char *str)
2388{
2389 keep_bootcon = 1;
2390 printk(KERN_INFO "debug: skip boot console de-registration.\n");
2391
2392 return 0;
2393}
2394
2395early_param("keep_bootcon", keep_bootcon_setup);
2396
1da177e4
LT
2397/*
2398 * The console driver calls this routine during kernel initialization
2399 * to register the console printing procedure with printk() and to
2400 * print any messages that were printed by the kernel before the
2401 * console driver was initialized.
4d091611
RG
2402 *
2403 * This can happen pretty early during the boot process (because of
2404 * early_printk) - sometimes before setup_arch() completes - be careful
2405 * of what kernel features are used - they may not be initialised yet.
2406 *
2407 * There are two types of consoles - bootconsoles (early_printk) and
2408 * "real" consoles (everything which is not a bootconsole) which are
2409 * handled differently.
2410 * - Any number of bootconsoles can be registered at any time.
2411 * - As soon as a "real" console is registered, all bootconsoles
2412 * will be unregistered automatically.
2413 * - Once a "real" console is registered, any attempt to register a
2414 * bootconsoles will be rejected
1da177e4 2415 */
4d091611 2416void register_console(struct console *newcon)
1da177e4 2417{
40dc5651 2418 int i;
1da177e4 2419 unsigned long flags;
4d091611 2420 struct console *bcon = NULL;
1da177e4 2421
4d091611
RG
2422 /*
2423 * before we register a new CON_BOOT console, make sure we don't
2424 * already have a valid console
2425 */
2426 if (console_drivers && newcon->flags & CON_BOOT) {
2427 /* find the last or real console */
2428 for_each_console(bcon) {
2429 if (!(bcon->flags & CON_BOOT)) {
2430 printk(KERN_INFO "Too late to register bootconsole %s%d\n",
2431 newcon->name, newcon->index);
2432 return;
2433 }
2434 }
69331af7
GH
2435 }
2436
4d091611
RG
2437 if (console_drivers && console_drivers->flags & CON_BOOT)
2438 bcon = console_drivers;
2439
2440 if (preferred_console < 0 || bcon || !console_drivers)
1da177e4
LT
2441 preferred_console = selected_console;
2442
4d091611
RG
2443 if (newcon->early_setup)
2444 newcon->early_setup();
18a8bd94 2445
1da177e4
LT
2446 /*
2447 * See if we want to use this console driver. If we
2448 * didn't select a console we take the first one
2449 * that registers here.
2450 */
2451 if (preferred_console < 0) {
4d091611
RG
2452 if (newcon->index < 0)
2453 newcon->index = 0;
2454 if (newcon->setup == NULL ||
2455 newcon->setup(newcon, NULL) == 0) {
2456 newcon->flags |= CON_ENABLED;
2457 if (newcon->device) {
2458 newcon->flags |= CON_CONSDEV;
cd3a1b85
JK
2459 preferred_console = 0;
2460 }
1da177e4
LT
2461 }
2462 }
2463
2464 /*
2465 * See if this console matches one we selected on
2466 * the command line.
2467 */
40dc5651
JJ
2468 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
2469 i++) {
391f1c61
PH
2470 BUILD_BUG_ON(sizeof(console_cmdline[i].name) !=
2471 sizeof(newcon->name));
4d091611 2472 if (strcmp(console_cmdline[i].name, newcon->name) != 0)
1da177e4 2473 continue;
4d091611
RG
2474 if (newcon->index >= 0 &&
2475 newcon->index != console_cmdline[i].index)
1da177e4 2476 continue;
4d091611
RG
2477 if (newcon->index < 0)
2478 newcon->index = console_cmdline[i].index;
f7511d5f
ST
2479#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
2480 if (console_cmdline[i].brl_options) {
4d091611
RG
2481 newcon->flags |= CON_BRL;
2482 braille_register_console(newcon,
f7511d5f
ST
2483 console_cmdline[i].index,
2484 console_cmdline[i].options,
2485 console_cmdline[i].brl_options);
2486 return;
2487 }
2488#endif
4d091611
RG
2489 if (newcon->setup &&
2490 newcon->setup(newcon, console_cmdline[i].options) != 0)
1da177e4 2491 break;
4d091611
RG
2492 newcon->flags |= CON_ENABLED;
2493 newcon->index = console_cmdline[i].index;
ab4af03a 2494 if (i == selected_console) {
4d091611 2495 newcon->flags |= CON_CONSDEV;
ab4af03a
GE
2496 preferred_console = selected_console;
2497 }
1da177e4
LT
2498 break;
2499 }
2500
4d091611 2501 if (!(newcon->flags & CON_ENABLED))
1da177e4
LT
2502 return;
2503
8259cf43
RG
2504 /*
2505 * If we have a bootconsole, and are switching to a real console,
2506 * don't print everything out again, since when the boot console, and
2507 * the real console are the same physical device, it's annoying to
2508 * see the beginning boot messages twice
2509 */
2510 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
4d091611 2511 newcon->flags &= ~CON_PRINTBUFFER;
1da177e4
LT
2512
2513 /*
2514 * Put this console in the list - keep the
2515 * preferred driver at the head of the list.
2516 */
ac751efa 2517 console_lock();
4d091611
RG
2518 if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
2519 newcon->next = console_drivers;
2520 console_drivers = newcon;
2521 if (newcon->next)
2522 newcon->next->flags &= ~CON_CONSDEV;
1da177e4 2523 } else {
4d091611
RG
2524 newcon->next = console_drivers->next;
2525 console_drivers->next = newcon;
1da177e4 2526 }
4d091611 2527 if (newcon->flags & CON_PRINTBUFFER) {
1da177e4 2528 /*
ac751efa 2529 * console_unlock(); will print out the buffered messages
1da177e4
LT
2530 * for us.
2531 */
07354eb1 2532 raw_spin_lock_irqsave(&logbuf_lock, flags);
7ff9554b
KS
2533 console_seq = syslog_seq;
2534 console_idx = syslog_idx;
5becfb1d 2535 console_prev = syslog_prev;
07354eb1 2536 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
fe3d8ad3
FT
2537 /*
2538 * We're about to replay the log buffer. Only do this to the
2539 * just-registered console to avoid excessive message spam to
2540 * the already-registered consoles.
2541 */
2542 exclusive_console = newcon;
1da177e4 2543 }
ac751efa 2544 console_unlock();
fbc92a34 2545 console_sysfs_notify();
8259cf43
RG
2546
2547 /*
2548 * By unregistering the bootconsoles after we enable the real console
2549 * we get the "console xxx enabled" message on all the consoles -
2550 * boot consoles, real consoles, etc - this is to ensure that end
2551 * users know there might be something in the kernel's log buffer that
2552 * went to the bootconsole (that they do not see on the real console)
2553 */
7bf69395
FDN
2554 if (bcon &&
2555 ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
2556 !keep_bootcon) {
8259cf43
RG
2557 /* we need to iterate through twice, to make sure we print
2558 * everything out, before we unregister the console(s)
2559 */
2560 printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
2561 newcon->name, newcon->index);
2562 for_each_console(bcon)
2563 if (bcon->flags & CON_BOOT)
2564 unregister_console(bcon);
2565 } else {
2566 printk(KERN_INFO "%sconsole [%s%d] enabled\n",
2567 (newcon->flags & CON_BOOT) ? "boot" : "" ,
2568 newcon->name, newcon->index);
2569 }
1da177e4
LT
2570}
2571EXPORT_SYMBOL(register_console);
2572
40dc5651 2573int unregister_console(struct console *console)
1da177e4 2574{
40dc5651 2575 struct console *a, *b;
1da177e4
LT
2576 int res = 1;
2577
f7511d5f
ST
2578#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
2579 if (console->flags & CON_BRL)
2580 return braille_unregister_console(console);
2581#endif
2582
ac751efa 2583 console_lock();
1da177e4
LT
2584 if (console_drivers == console) {
2585 console_drivers=console->next;
2586 res = 0;
e9b15b54 2587 } else if (console_drivers) {
1da177e4
LT
2588 for (a=console_drivers->next, b=console_drivers ;
2589 a; b=a, a=b->next) {
2590 if (a == console) {
2591 b->next = a->next;
2592 res = 0;
2593 break;
40dc5651 2594 }
1da177e4
LT
2595 }
2596 }
40dc5651 2597
69331af7 2598 /*
ab4af03a
GE
2599 * If this isn't the last console and it has CON_CONSDEV set, we
2600 * need to set it on the next preferred console.
1da177e4 2601 */
69331af7 2602 if (console_drivers != NULL && console->flags & CON_CONSDEV)
ab4af03a 2603 console_drivers->flags |= CON_CONSDEV;
1da177e4 2604
ac751efa 2605 console_unlock();
fbc92a34 2606 console_sysfs_notify();
1da177e4
LT
2607 return res;
2608}
2609EXPORT_SYMBOL(unregister_console);
d59745ce 2610
034260d6 2611static int __init printk_late_init(void)
0c5564bd 2612{
4d091611
RG
2613 struct console *con;
2614
2615 for_each_console(con) {
4c30c6f5 2616 if (!keep_bootcon && con->flags & CON_BOOT) {
cb00e99c 2617 printk(KERN_INFO "turn off boot console %s%d\n",
4d091611 2618 con->name, con->index);
42c2c8c8 2619 unregister_console(con);
cb00e99c 2620 }
0c5564bd 2621 }
034260d6 2622 hotcpu_notifier(console_cpu_notify, 0);
0c5564bd
RG
2623 return 0;
2624}
034260d6 2625late_initcall(printk_late_init);
0c5564bd 2626
7ef3d2fd 2627#if defined CONFIG_PRINTK
dc72c32e
FW
2628/*
2629 * Delayed printk version, for scheduler-internal messages:
2630 */
2631#define PRINTK_BUF_SIZE 512
2632
2633#define PRINTK_PENDING_WAKEUP 0x01
2634#define PRINTK_PENDING_SCHED 0x02
2635
2636static DEFINE_PER_CPU(int, printk_pending);
2637static DEFINE_PER_CPU(char [PRINTK_BUF_SIZE], printk_sched_buf);
6fa3eb70 2638static DEFINE_PER_CPU(int, printk_sched_length);
dc72c32e
FW
2639
2640static void wake_up_klogd_work_func(struct irq_work *irq_work)
2641{
2642 int pending = __this_cpu_xchg(printk_pending, 0);
2643
2644 if (pending & PRINTK_PENDING_SCHED) {
2645 char *buf = __get_cpu_var(printk_sched_buf);
6fa3eb70
S
2646 printk(KERN_WARNING "[printk_delayed:start]\n");
2647 printk(KERN_WARNING "%s", buf);
2648 printk(KERN_WARNING "[printk_delayed:done]\n");
2649 __get_cpu_var(printk_sched_length) = 0;
dc72c32e
FW
2650 }
2651
2652 if (pending & PRINTK_PENDING_WAKEUP)
2653 wake_up_interruptible(&log_wait);
2654}
2655
2656static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) = {
2657 .func = wake_up_klogd_work_func,
2658 .flags = IRQ_WORK_LAZY,
2659};
2660
2661void wake_up_klogd(void)
2662{
2663 preempt_disable();
2664 if (waitqueue_active(&log_wait)) {
2665 this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP);
2666 irq_work_queue(&__get_cpu_var(wake_up_klogd_work));
2667 }
2668 preempt_enable();
2669}
717115e1 2670
3984bb13 2671int printk_deferred(const char *fmt, ...)
600e1458
PZ
2672{
2673 unsigned long flags;
2674 va_list args;
2675 char *buf;
2676 int r;
6fa3eb70 2677 int buf_length;
600e1458
PZ
2678 local_irq_save(flags);
2679 buf = __get_cpu_var(printk_sched_buf);
6fa3eb70 2680 buf_length = __get_cpu_var(printk_sched_length);
600e1458
PZ
2681
2682 va_start(args, fmt);
6fa3eb70
S
2683 if(PRINTK_BUF_SIZE >= buf_length){
2684 r = vsnprintf((buf_length + buf), PRINTK_BUF_SIZE-buf_length, fmt, args);
2685 __get_cpu_var(printk_sched_length) += r;
2686 }else{
2687 printk("delayed log buf overflow, size:%d\n", buf_length);
2688 r = 0;
2689 }
600e1458
PZ
2690 va_end(args);
2691
2692 __this_cpu_or(printk_pending, PRINTK_PENDING_SCHED);
74876a98 2693 irq_work_queue(&__get_cpu_var(wake_up_klogd_work));
600e1458
PZ
2694 local_irq_restore(flags);
2695
2696 return r;
2697}
2698
1da177e4
LT
2699/*
2700 * printk rate limiting, lifted from the networking subsystem.
2701 *
641de9d8
UKK
2702 * This enforces a rate limit: not more than 10 kernel messages
2703 * every 5s to make a denial-of-service attack impossible.
1da177e4 2704 */
641de9d8
UKK
2705DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
2706
5c828713 2707int __printk_ratelimit(const char *func)
1da177e4 2708{
5c828713 2709 return ___ratelimit(&printk_ratelimit_state, func);
1da177e4 2710}
5c828713 2711EXPORT_SYMBOL(__printk_ratelimit);
f46c4833
AM
2712
2713/**
2714 * printk_timed_ratelimit - caller-controlled printk ratelimiting
2715 * @caller_jiffies: pointer to caller's state
2716 * @interval_msecs: minimum interval between prints
2717 *
2718 * printk_timed_ratelimit() returns true if more than @interval_msecs
2719 * milliseconds have elapsed since the last time printk_timed_ratelimit()
2720 * returned true.
2721 */
2722bool printk_timed_ratelimit(unsigned long *caller_jiffies,
2723 unsigned int interval_msecs)
2724{
f2d28a2e
GK
2725 if (*caller_jiffies == 0
2726 || !time_in_range(jiffies, *caller_jiffies,
2727 *caller_jiffies
2728 + msecs_to_jiffies(interval_msecs))) {
2729 *caller_jiffies = jiffies;
f46c4833
AM
2730 return true;
2731 }
2732 return false;
2733}
2734EXPORT_SYMBOL(printk_timed_ratelimit);
456b565c
SK
2735
2736static DEFINE_SPINLOCK(dump_list_lock);
2737static LIST_HEAD(dump_list);
2738
2739/**
2740 * kmsg_dump_register - register a kernel log dumper.
6485536b 2741 * @dumper: pointer to the kmsg_dumper structure
456b565c
SK
2742 *
2743 * Adds a kernel log dumper to the system. The dump callback in the
2744 * structure will be called when the kernel oopses or panics and must be
2745 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
2746 */
2747int kmsg_dump_register(struct kmsg_dumper *dumper)
2748{
2749 unsigned long flags;
2750 int err = -EBUSY;
2751
2752 /* The dump callback needs to be set */
2753 if (!dumper->dump)
2754 return -EINVAL;
2755
2756 spin_lock_irqsave(&dump_list_lock, flags);
2757 /* Don't allow registering multiple times */
2758 if (!dumper->registered) {
2759 dumper->registered = 1;
fb842b00 2760 list_add_tail_rcu(&dumper->list, &dump_list);
456b565c
SK
2761 err = 0;
2762 }
2763 spin_unlock_irqrestore(&dump_list_lock, flags);
2764
2765 return err;
2766}
2767EXPORT_SYMBOL_GPL(kmsg_dump_register);
2768
2769/**
2770 * kmsg_dump_unregister - unregister a kmsg dumper.
6485536b 2771 * @dumper: pointer to the kmsg_dumper structure
456b565c
SK
2772 *
2773 * Removes a dump device from the system. Returns zero on success and
2774 * %-EINVAL otherwise.
2775 */
2776int kmsg_dump_unregister(struct kmsg_dumper *dumper)
2777{
2778 unsigned long flags;
2779 int err = -EINVAL;
2780
2781 spin_lock_irqsave(&dump_list_lock, flags);
2782 if (dumper->registered) {
2783 dumper->registered = 0;
fb842b00 2784 list_del_rcu(&dumper->list);
456b565c
SK
2785 err = 0;
2786 }
2787 spin_unlock_irqrestore(&dump_list_lock, flags);
fb842b00 2788 synchronize_rcu();
456b565c
SK
2789
2790 return err;
2791}
2792EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
2793
7ff9554b
KS
2794static bool always_kmsg_dump;
2795module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR);
2796
456b565c
SK
2797/**
2798 * kmsg_dump - dump kernel log to kernel message dumpers.
2799 * @reason: the reason (oops, panic etc) for dumping
2800 *
e2ae715d
KS
2801 * Call each of the registered dumper's dump() callback, which can
2802 * retrieve the kmsg records with kmsg_dump_get_line() or
2803 * kmsg_dump_get_buffer().
456b565c
SK
2804 */
2805void kmsg_dump(enum kmsg_dump_reason reason)
2806{
456b565c 2807 struct kmsg_dumper *dumper;
456b565c
SK
2808 unsigned long flags;
2809
c22ab332
MG
2810 if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
2811 return;
2812
e2ae715d
KS
2813 rcu_read_lock();
2814 list_for_each_entry_rcu(dumper, &dump_list, list) {
2815 if (dumper->max_reason && reason > dumper->max_reason)
2816 continue;
2817
2818 /* initialize iterator with data about the stored records */
2819 dumper->active = true;
2820
2821 raw_spin_lock_irqsave(&logbuf_lock, flags);
2822 dumper->cur_seq = clear_seq;
2823 dumper->cur_idx = clear_idx;
2824 dumper->next_seq = log_next_seq;
2825 dumper->next_idx = log_next_idx;
2826 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2827
2828 /* invoke dumper which will iterate over records */
2829 dumper->dump(dumper, reason);
2830
2831 /* reset iterator */
2832 dumper->active = false;
2833 }
2834 rcu_read_unlock();
2835}
2836
2837/**
533827c9 2838 * kmsg_dump_get_line_nolock - retrieve one kmsg log line (unlocked version)
e2ae715d
KS
2839 * @dumper: registered kmsg dumper
2840 * @syslog: include the "<4>" prefixes
2841 * @line: buffer to copy the line to
2842 * @size: maximum size of the buffer
2843 * @len: length of line placed into buffer
2844 *
2845 * Start at the beginning of the kmsg buffer, with the oldest kmsg
2846 * record, and copy one record into the provided buffer.
2847 *
2848 * Consecutive calls will return the next available record moving
2849 * towards the end of the buffer with the youngest messages.
2850 *
2851 * A return value of FALSE indicates that there are no more records to
2852 * read.
533827c9
AV
2853 *
2854 * The function is similar to kmsg_dump_get_line(), but grabs no locks.
e2ae715d 2855 */
533827c9
AV
2856bool kmsg_dump_get_line_nolock(struct kmsg_dumper *dumper, bool syslog,
2857 char *line, size_t size, size_t *len)
e2ae715d 2858{
e2ae715d
KS
2859 struct log *msg;
2860 size_t l = 0;
2861 bool ret = false;
2862
2863 if (!dumper->active)
2864 goto out;
7ff9554b 2865
e2ae715d
KS
2866 if (dumper->cur_seq < log_first_seq) {
2867 /* messages are gone, move to first available one */
2868 dumper->cur_seq = log_first_seq;
2869 dumper->cur_idx = log_first_idx;
2870 }
456b565c 2871
e2ae715d 2872 /* last entry */
533827c9 2873 if (dumper->cur_seq >= log_next_seq)
e2ae715d 2874 goto out;
456b565c 2875
e2ae715d 2876 msg = log_from_idx(dumper->cur_idx);
5becfb1d 2877 l = msg_print_text(msg, 0, syslog, line, size);
e2ae715d
KS
2878
2879 dumper->cur_idx = log_next(dumper->cur_idx);
2880 dumper->cur_seq++;
2881 ret = true;
e2ae715d
KS
2882out:
2883 if (len)
2884 *len = l;
2885 return ret;
2886}
533827c9
AV
2887
2888/**
2889 * kmsg_dump_get_line - retrieve one kmsg log line
2890 * @dumper: registered kmsg dumper
2891 * @syslog: include the "<4>" prefixes
2892 * @line: buffer to copy the line to
2893 * @size: maximum size of the buffer
2894 * @len: length of line placed into buffer
2895 *
2896 * Start at the beginning of the kmsg buffer, with the oldest kmsg
2897 * record, and copy one record into the provided buffer.
2898 *
2899 * Consecutive calls will return the next available record moving
2900 * towards the end of the buffer with the youngest messages.
2901 *
2902 * A return value of FALSE indicates that there are no more records to
2903 * read.
2904 */
2905bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog,
2906 char *line, size_t size, size_t *len)
2907{
2908 unsigned long flags;
2909 bool ret;
2910
2911 raw_spin_lock_irqsave(&logbuf_lock, flags);
2912 ret = kmsg_dump_get_line_nolock(dumper, syslog, line, size, len);
2913 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2914
2915 return ret;
2916}
e2ae715d
KS
2917EXPORT_SYMBOL_GPL(kmsg_dump_get_line);
2918
2919/**
2920 * kmsg_dump_get_buffer - copy kmsg log lines
2921 * @dumper: registered kmsg dumper
2922 * @syslog: include the "<4>" prefixes
4f0f4af5 2923 * @buf: buffer to copy the line to
e2ae715d
KS
2924 * @size: maximum size of the buffer
2925 * @len: length of line placed into buffer
2926 *
2927 * Start at the end of the kmsg buffer and fill the provided buffer
2928 * with as many of the the *youngest* kmsg records that fit into it.
2929 * If the buffer is large enough, all available kmsg records will be
2930 * copied with a single call.
2931 *
2932 * Consecutive calls will fill the buffer with the next block of
2933 * available older records, not including the earlier retrieved ones.
2934 *
2935 * A return value of FALSE indicates that there are no more records to
2936 * read.
2937 */
2938bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
2939 char *buf, size_t size, size_t *len)
2940{
2941 unsigned long flags;
2942 u64 seq;
2943 u32 idx;
2944 u64 next_seq;
2945 u32 next_idx;
5becfb1d 2946 enum log_flags prev;
e2ae715d
KS
2947 size_t l = 0;
2948 bool ret = false;
2949
2950 if (!dumper->active)
2951 goto out;
2952
2953 raw_spin_lock_irqsave(&logbuf_lock, flags);
2954 if (dumper->cur_seq < log_first_seq) {
2955 /* messages are gone, move to first available one */
2956 dumper->cur_seq = log_first_seq;
2957 dumper->cur_idx = log_first_idx;
2958 }
2959
2960 /* last entry */
2961 if (dumper->cur_seq >= dumper->next_seq) {
2962 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2963 goto out;
2964 }
2965
2966 /* calculate length of entire buffer */
2967 seq = dumper->cur_seq;
2968 idx = dumper->cur_idx;
5becfb1d 2969 prev = 0;
e2ae715d
KS
2970 while (seq < dumper->next_seq) {
2971 struct log *msg = log_from_idx(idx);
2972
5becfb1d 2973 l += msg_print_text(msg, prev, true, NULL, 0);
e2ae715d
KS
2974 idx = log_next(idx);
2975 seq++;
5becfb1d 2976 prev = msg->flags;
e2ae715d
KS
2977 }
2978
2979 /* move first record forward until length fits into the buffer */
2980 seq = dumper->cur_seq;
2981 idx = dumper->cur_idx;
5becfb1d 2982 prev = 0;
e2ae715d
KS
2983 while (l > size && seq < dumper->next_seq) {
2984 struct log *msg = log_from_idx(idx);
456b565c 2985
5becfb1d 2986 l -= msg_print_text(msg, prev, true, NULL, 0);
e2ae715d
KS
2987 idx = log_next(idx);
2988 seq++;
5becfb1d 2989 prev = msg->flags;
456b565c 2990 }
e2ae715d
KS
2991
2992 /* last message in next interation */
2993 next_seq = seq;
2994 next_idx = idx;
2995
2996 l = 0;
5becfb1d 2997 prev = 0;
e2ae715d
KS
2998 while (seq < dumper->next_seq) {
2999 struct log *msg = log_from_idx(idx);
3000
5becfb1d 3001 l += msg_print_text(msg, prev, syslog, buf + l, size - l);
e2ae715d
KS
3002 idx = log_next(idx);
3003 seq++;
5becfb1d 3004 prev = msg->flags;
e2ae715d
KS
3005 }
3006
3007 dumper->next_seq = next_seq;
3008 dumper->next_idx = next_idx;
3009 ret = true;
7ff9554b 3010 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
e2ae715d
KS
3011out:
3012 if (len)
3013 *len = l;
3014 return ret;
3015}
3016EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer);
456b565c 3017
533827c9
AV
3018/**
3019 * kmsg_dump_rewind_nolock - reset the interator (unlocked version)
3020 * @dumper: registered kmsg dumper
3021 *
3022 * Reset the dumper's iterator so that kmsg_dump_get_line() and
3023 * kmsg_dump_get_buffer() can be called again and used multiple
3024 * times within the same dumper.dump() callback.
3025 *
3026 * The function is similar to kmsg_dump_rewind(), but grabs no locks.
3027 */
3028void kmsg_dump_rewind_nolock(struct kmsg_dumper *dumper)
3029{
3030 dumper->cur_seq = clear_seq;
3031 dumper->cur_idx = clear_idx;
3032 dumper->next_seq = log_next_seq;
3033 dumper->next_idx = log_next_idx;
3034}
3035
e2ae715d
KS
3036/**
3037 * kmsg_dump_rewind - reset the interator
3038 * @dumper: registered kmsg dumper
3039 *
3040 * Reset the dumper's iterator so that kmsg_dump_get_line() and
3041 * kmsg_dump_get_buffer() can be called again and used multiple
3042 * times within the same dumper.dump() callback.
3043 */
3044void kmsg_dump_rewind(struct kmsg_dumper *dumper)
3045{
3046 unsigned long flags;
3047
3048 raw_spin_lock_irqsave(&logbuf_lock, flags);
533827c9 3049 kmsg_dump_rewind_nolock(dumper);
e2ae715d 3050 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
456b565c 3051}
e2ae715d 3052EXPORT_SYMBOL_GPL(kmsg_dump_rewind);
196779b9 3053
98e5e1bf
TH
3054static char dump_stack_arch_desc_str[128];
3055
3056/**
3057 * dump_stack_set_arch_desc - set arch-specific str to show with task dumps
3058 * @fmt: printf-style format string
3059 * @...: arguments for the format string
3060 *
3061 * The configured string will be printed right after utsname during task
3062 * dumps. Usually used to add arch-specific system identifiers. If an
3063 * arch wants to make use of such an ID string, it should initialize this
3064 * as soon as possible during boot.
3065 */
3066void __init dump_stack_set_arch_desc(const char *fmt, ...)
3067{
3068 va_list args;
3069
3070 va_start(args, fmt);
3071 vsnprintf(dump_stack_arch_desc_str, sizeof(dump_stack_arch_desc_str),
3072 fmt, args);
3073 va_end(args);
3074}
3075
196779b9
TH
3076/**
3077 * dump_stack_print_info - print generic debug info for dump_stack()
3078 * @log_lvl: log level
3079 *
3080 * Arch-specific dump_stack() implementations can use this function to
3081 * print out the same debug information as the generic dump_stack().
3082 */
3083void dump_stack_print_info(const char *log_lvl)
3084{
3085 printk("%sCPU: %d PID: %d Comm: %.20s %s %s %.*s\n",
3086 log_lvl, raw_smp_processor_id(), current->pid, current->comm,
3087 print_tainted(), init_utsname()->release,
3088 (int)strcspn(init_utsname()->version, " "),
3089 init_utsname()->version);
98e5e1bf
TH
3090
3091 if (dump_stack_arch_desc_str[0] != '\0')
3092 printk("%sHardware name: %s\n",
3093 log_lvl, dump_stack_arch_desc_str);
3d1cb205
TH
3094
3095 print_worker_info(log_lvl, current);
196779b9
TH
3096}
3097
a43cb95d
TH
3098/**
3099 * show_regs_print_info - print generic debug info for show_regs()
3100 * @log_lvl: log level
3101 *
3102 * show_regs() implementations can use this function to print out generic
3103 * debug information.
3104 */
3105void show_regs_print_info(const char *log_lvl)
3106{
3107 dump_stack_print_info(log_lvl);
3108
3109 printk("%stask: %p ti: %p task.ti: %p\n",
3110 log_lvl, current, current_thread_info(),
3111 task_thread_info(current));
3112}
3113
6fa3eb70
S
3114void get_kernel_log_buffer(unsigned long *addr, unsigned long *size, unsigned long *start)
3115{
3116 *addr = (unsigned long)log_buf;
3117 *size = log_buf_len;
3118 *start = (unsigned long)&log_first_idx;
3119}
7ef3d2fd 3120#endif