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