Merge tag 'v3.10.102' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / printk.c
1 /*
2 * linux/kernel/printk.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * Modified to make sys_syslog() more flexible: added commands to
7 * return the last 4k of kernel messages, regardless of whether
8 * they've been read or not. Added option to suppress kernel printk's
9 * to the console. Added hook for sending the console messages
10 * elsewhere, in preparation for a serial line console (someday).
11 * Ted Ts'o, 2/11/93.
12 * Modified for sysctl support, 1/8/97, Chris Horn.
13 * Fixed SMP synchronization, 08/08/99, Manfred Spraul
14 * manfred@colorfullife.com
15 * Rewrote bits to get rid of console_lock
16 * 01Mar01 Andrew Morton
17 */
18
19
20 #include <linux/kernel.h>
21 #include <linux/mm.h>
22 #include <linux/tty.h>
23 #include <linux/tty_driver.h>
24 #include <linux/console.h>
25 #include <linux/init.h>
26 #include <linux/jiffies.h>
27 #include <linux/nmi.h>
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/interrupt.h> /* For in_interrupt() */
31 #include <linux/delay.h>
32 #include <linux/smp.h>
33 #include <linux/security.h>
34 #include <linux/bootmem.h>
35 #include <linux/memblock.h>
36 #include <linux/aio.h>
37 #include <linux/syscalls.h>
38 #include <linux/suspend.h>
39 #include <linux/kexec.h>
40 #include <linux/kdb.h>
41 #include <linux/ratelimit.h>
42 #include <linux/kmsg_dump.h>
43 #include <linux/syslog.h>
44 #include <linux/cpu.h>
45 #include <linux/notifier.h>
46 #include <linux/rculist.h>
47 #include <linux/poll.h>
48 #include <linux/irq_work.h>
49 #include <linux/utsname.h>
50 #include <linux/mt_sched_mon.h>
51 #include <linux/aee.h>
52
53 #include <asm/uaccess.h>
54
55 #define CREATE_TRACE_POINTS
56 #include <trace/events/printk.h>
57
58 /* Some options {*/
59 #define LOG_TOO_MUCH_WARNING
60 #ifdef LOG_TOO_MUCH_WARNING
61 static int log_in_resume;
62 #endif
63 /* Some options }*/
64 #ifdef CONFIG_EARLY_PRINTK_DIRECT
65 extern void printascii(char *);
66 #endif
67
68 bool printk_disable_uart = 0;
69 static DEFINE_PER_CPU(char, printk_state);
70 /* printk's without a loglevel use this.. */
71 #define DEFAULT_MESSAGE_LOGLEVEL CONFIG_DEFAULT_MESSAGE_LOGLEVEL
72
73 /* We show everything that is MORE important than this.. */
74 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
75 #define DEFAULT_CONSOLE_LOGLEVEL 6 /* anything MORE serious than KERN_INFO */
76
77 int 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 };
83 EXPORT_SYMBOL_GPL(console_printk);
84
85 /*
86 * Low level drivers may need that to know if they can schedule in
87 * their unblank() callback or not. So let's export it.
88 */
89 int oops_in_progress;
90 EXPORT_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 */
97 static DEFINE_SEMAPHORE(console_sem);
98 struct console *console_drivers;
99 EXPORT_SYMBOL_GPL(console_drivers);
100
101 #ifdef CONFIG_LOCKDEP
102 static struct lockdep_map console_lock_dep_map = {
103 .name = "console_lock"
104 };
105 #endif
106
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 */
115 static int console_locked, console_suspended;
116
117 /*
118 * If exclusive_console is non-NULL then only this console is to be printed to.
119 */
120 static struct console *exclusive_console;
121
122 /*
123 * Array of consoles built from command line options (console=)
124 */
125 struct console_cmdline
126 {
127 char name[16]; /* Name of the driver */
128 int index; /* Minor dev. to use */
129 char *options; /* Options for the driver */
130 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
131 char *brl_options; /* Options for braille driver */
132 #endif
133 };
134
135 #define MAX_CMDLINECONSOLES 8
136
137 static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
138 static int selected_console = -1;
139 static int preferred_console = -1;
140 int console_set_on_cmdline;
141 EXPORT_SYMBOL(console_set_on_cmdline);
142
143 /* Flag: console code may call schedule() */
144 static int console_may_schedule;
145
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 *
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.
211 */
212
213 enum log_flags {
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 */
218 };
219
220 struct 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 */
225 u8 facility; /* syslog facility */
226 u8 flags:5; /* internal record flags */
227 u8 level:3; /* syslog level */
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 */
234 static DEFINE_RAW_SPINLOCK(logbuf_lock);
235
236 #ifdef CONFIG_PRINTK
237 DECLARE_WAIT_QUEUE_HEAD(log_wait);
238 /* the next printk record to read by syslog(READ) or /proc/kmsg */
239 static u64 syslog_seq;
240 static u32 syslog_idx;
241 static enum log_flags syslog_prev;
242 static size_t syslog_partial;
243
244 /* index and sequence number of the first record stored in the buffer */
245 /*static*/ u64 log_first_seq;
246 /*static*/ u32 log_first_idx;
247
248 /* index and sequence number of the next record to store in the buffer */
249 /*static*/ u64 log_next_seq;
250 /*static*/ u32 log_next_idx;
251
252 /* the next printk record to write to the console */
253 static u64 console_seq;
254 static u32 console_idx;
255 static enum log_flags console_prev;
256
257 /* the next printk record to read after the last 'clear' command */
258 static u64 clear_seq;
259 static u32 clear_idx;
260
261 #define PREFIX_MAX 32
262 #define LOG_LINE_MAX 1024 - PREFIX_MAX
263
264 /* record buffer */
265 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
266 #define LOG_ALIGN 4
267 #else
268 #define LOG_ALIGN __alignof__(struct log)
269 #endif
270 #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
271 static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN);
272 static char *log_buf = __log_buf;
273 static u32 log_buf_len = __LOG_BUF_LEN;
274
275 #ifdef CONFIG_MT_PRINTK_UART_CONSOLE
276
277 extern int mt_need_uart_console;
278 inline 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 }
287 inline 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
300 /* cpu currently holding logbuf_lock */
301 static volatile unsigned int logbuf_cpu = UINT_MAX;
302
303 /* human readable text of the record */
304 static 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 */
310 static 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 */
316 static 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 */
330 static 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
347 /* insert record into the buffer, discard old ones, update heads */
348 static void log_store(int facility, int level,
349 enum log_flags flags, u64 ts_nsec,
350 const char *dict, u16 dict_len,
351 const char *text, u16 text_len)
352 {
353 struct log *msg;
354 u32 size, pad_len;
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 }*/
371 /* number of '\0' padding bytes to next message */
372 size = sizeof(struct log) + text_len +tlen + dict_len;
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);
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;
408 msg->text_len = text_len;
409 memcpy(log_dict(msg), dict, dict_len);
410 msg->dict_len = dict_len;
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();
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 }
425
426 #ifdef CONFIG_SECURITY_DMESG_RESTRICT
427 int dmesg_restrict = 1;
428 #else
429 int dmesg_restrict;
430 #endif
431
432 static 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
444 static 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
473 /* /dev/kmsg - userspace message inject/listen interface */
474 struct devkmsg_user {
475 u64 seq;
476 u32 idx;
477 enum log_flags prev;
478 struct mutex lock;
479 char buf[8192];
480 };
481
482 static 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++) {
500 if (copy_from_user(line, iv[i].iov_base, iv[i].iov_len)) {
501 ret = -EFAULT;
502 goto out;
503 }
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);
533 out:
534 kfree(buf);
535 return ret;
536 }
537
538 static 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;
543 u64 ts_usec;
544 size_t i;
545 char cont = '-';
546 size_t len;
547 ssize_t ret;
548
549 if (!user)
550 return -EBADF;
551
552 ret = mutex_lock_interruptible(&user->lock);
553 if (ret)
554 return ret;
555 raw_spin_lock_irq(&logbuf_lock);
556 while (user->seq == log_next_seq) {
557 if (file->f_flags & O_NONBLOCK) {
558 ret = -EAGAIN;
559 raw_spin_unlock_irq(&logbuf_lock);
560 goto out;
561 }
562
563 raw_spin_unlock_irq(&logbuf_lock);
564 ret = wait_event_interruptible(log_wait,
565 user->seq != log_next_seq);
566 if (ret)
567 goto out;
568 raw_spin_lock_irq(&logbuf_lock);
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;
576 raw_spin_unlock_irq(&logbuf_lock);
577 goto out;
578 }
579
580 msg = log_from_idx(user->idx);
581 ts_usec = msg->ts_nsec;
582 do_div(ts_usec, 1000);
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;
602
603 /* escape non-printable characters */
604 for (i = 0; i < msg->text_len; i++) {
605 unsigned char c = log_text(msg)[i];
606
607 if (c < ' ' || c >= 127 || c == '\\')
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++) {
618 unsigned char c = log_dict(msg)[i];
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
631 if (c < ' ' || c >= 127 || c == '\\') {
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++;
643 raw_spin_unlock_irq(&logbuf_lock);
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;
655 out:
656 mutex_unlock(&user->lock);
657 return ret;
658 }
659
660 static 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
670 raw_spin_lock_irq(&logbuf_lock);
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 }
694 raw_spin_unlock_irq(&logbuf_lock);
695 return ret;
696 }
697
698 static 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
708 raw_spin_lock_irq(&logbuf_lock);
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;
713 else
714 ret = POLLIN|POLLRDNORM;
715 }
716 raw_spin_unlock_irq(&logbuf_lock);
717
718 return ret;
719 }
720
721 static 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
730 err = check_syslog_permissions(SYSLOG_ACTION_READ_ALL,
731 SYSLOG_FROM_READER);
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
741 raw_spin_lock_irq(&logbuf_lock);
742 user->idx = log_first_idx;
743 user->seq = log_first_seq;
744 raw_spin_unlock_irq(&logbuf_lock);
745
746 file->private_data = user;
747 return 0;
748 }
749
750 static 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
762 const 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
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 */
780 void log_buf_kexec_setup(void)
781 {
782 VMCOREINFO_SYMBOL(log_buf);
783 VMCOREINFO_SYMBOL(log_buf_len);
784 VMCOREINFO_SYMBOL(log_first_idx);
785 VMCOREINFO_SYMBOL(log_next_idx);
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);
795 }
796 #endif
797
798 /* requested log_buf_len from kernel cmdline */
799 static unsigned long __initdata new_log_buf_len;
800
801 /* save requested log_buf_len since it's too early to process it */
802 static int __init log_buf_len_setup(char *str)
803 {
804 unsigned size = memparse(str, &str);
805
806 if (size)
807 size = roundup_pow_of_two(size);
808 if (size > log_buf_len)
809 new_log_buf_len = size;
810
811 return 0;
812 }
813 early_param("log_buf_len", log_buf_len_setup);
814
815 void __init setup_log_buf(int early)
816 {
817 unsigned long flags;
818 char *new_log_buf;
819 int free;
820
821 if (!new_log_buf_len)
822 return;
823
824 if (early) {
825 unsigned long mem;
826
827 mem = memblock_alloc(new_log_buf_len, PAGE_SIZE);
828 if (!mem)
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
841 raw_spin_lock_irqsave(&logbuf_lock, flags);
842 log_buf_len = new_log_buf_len;
843 log_buf = new_log_buf;
844 new_log_buf_len = 0;
845 free = __LOG_BUF_LEN - log_next_idx;
846 memcpy(log_buf, __log_buf, __LOG_BUF_LEN);
847 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
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 }
853
854 static bool __read_mostly ignore_loglevel;
855
856 static 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
864 early_param("ignore_loglevel", ignore_loglevel_setup);
865 module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR);
866 MODULE_PARM_DESC(ignore_loglevel, "ignore loglevel setting, to"
867 "print all kernel messages to the console.");
868
869 #ifdef CONFIG_BOOT_PRINTK_DELAY
870
871 static int boot_delay; /* msecs delay after each printk during bootup */
872 static unsigned long long loops_per_msec; /* based on boot_delay */
873
874 static int __init boot_delay_setup(char *str)
875 {
876 unsigned long lpj;
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
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);
888 return 1;
889 }
890 __setup("boot_delay=", boot_delay_setup);
891
892 static void boot_delay_msec(int level)
893 {
894 unsigned long long k;
895 unsigned long timeout;
896
897 if ((boot_delay == 0 || system_state != SYSTEM_BOOTING)
898 || (level >= console_loglevel && !ignore_loglevel)) {
899 return;
900 }
901
902 k = (unsigned long long)loops_per_msec * boot_delay;
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
919 static inline void boot_delay_msec(int level)
920 {
921 }
922 #endif
923
924 #if defined(CONFIG_PRINTK_TIME)
925 static bool printk_time = 1;
926 #else
927 static bool printk_time;
928 #endif
929 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
930 module_param_named(disable_uart, printk_disable_uart, bool, S_IRUGO | S_IWUSR);
931
932 static size_t print_time(u64 ts, char *buf)
933 {
934 unsigned long rem_nsec;
935
936 if (!printk_time)
937 return 0;
938
939 rem_nsec = do_div(ts, 1000000000);
940
941 if (!buf)
942 return snprintf(NULL, 0, "[%5lu.000000]", (unsigned long)ts);
943
944 return sprintf(buf, "[%5lu.%06lu]",
945 (unsigned long)ts, rem_nsec / 1000);
946 }
947
948 static size_t print_prefix(const struct log *msg, bool syslog, char *buf)
949 {
950 size_t len = 0;
951 unsigned int prefix = (msg->facility << 3) | msg->level;
952
953 if (syslog) {
954 if (buf) {
955 len += sprintf(buf, "<%u>", prefix);
956 } else {
957 len += 3;
958 if (prefix > 999)
959 len += 3;
960 else if (prefix > 99)
961 len += 2;
962 else if (prefix > 9)
963 len++;
964 }
965 }
966
967 len += print_time(msg->ts_nsec, buf ? buf + len : NULL);
968 return len;
969 }
970
971 static size_t msg_print_text(const struct log *msg, enum log_flags prev,
972 bool syslog, char *buf, size_t size)
973 {
974 const char *text = log_text(msg);
975 size_t text_size = msg->text_len;
976 bool prefix = true;
977 bool newline = true;
978 size_t len = 0;
979
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
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 }
1002
1003 if (buf) {
1004 if (print_prefix(msg, syslog, NULL) +
1005 text_len + 1 >= size - len)
1006 break;
1007
1008 if (prefix)
1009 len += print_prefix(msg, syslog, buf + len);
1010 memcpy(buf + len, text, text_len);
1011 len += text_len;
1012 if (next || newline)
1013 buf[len++] = '\n';
1014 } else {
1015 /* SYSLOG_ACTION_* buffer size only calculation */
1016 if (prefix)
1017 len += print_prefix(msg, syslog, NULL);
1018 len += text_len;
1019 if (next || newline)
1020 len++;
1021 }
1022
1023 prefix = true;
1024 text = next;
1025 } while (text);
1026
1027 return len;
1028 }
1029
1030 static int syslog_print(char __user *buf, int size)
1031 {
1032 char *text;
1033 struct log *msg;
1034 int len = 0;
1035
1036 text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
1037 if (!text)
1038 return -ENOMEM;
1039
1040 while (size > 0) {
1041 size_t n;
1042 size_t skip;
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;
1049 syslog_prev = 0;
1050 syslog_partial = 0;
1051 }
1052 if (syslog_seq == log_next_seq) {
1053 raw_spin_unlock_irq(&logbuf_lock);
1054 break;
1055 }
1056
1057 skip = syslog_partial;
1058 msg = log_from_idx(syslog_idx);
1059 n = msg_print_text(msg, syslog_prev, true, text,
1060 LOG_LINE_MAX + PREFIX_MAX);
1061 if (n - syslog_partial <= size) {
1062 /* message fits into buffer, move forward */
1063 syslog_idx = log_next(syslog_idx);
1064 syslog_seq++;
1065 syslog_prev = msg->flags;
1066 n -= syslog_partial;
1067 syslog_partial = 0;
1068 } else if (!len){
1069 /* partial read(), remember position */
1070 n = size;
1071 syslog_partial += n;
1072 } else
1073 n = 0;
1074 raw_spin_unlock_irq(&logbuf_lock);
1075
1076 if (!n)
1077 break;
1078
1079 if (copy_to_user(buf, text + skip, n)) {
1080 if (!len)
1081 len = -EFAULT;
1082 break;
1083 }
1084
1085 len += n;
1086 size -= n;
1087 buf += n;
1088 }
1089
1090 kfree(text);
1091 return len;
1092 }
1093
1094 static int syslog_print_all(char __user *buf, int size, bool clear)
1095 {
1096 char *text;
1097 int len = 0;
1098
1099 text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
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;
1108 enum log_flags prev;
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.
1119 */
1120 seq = clear_seq;
1121 idx = clear_idx;
1122 prev = 0;
1123 while (seq < log_next_seq) {
1124 struct log *msg = log_from_idx(idx);
1125
1126 len += msg_print_text(msg, prev, true, NULL, 0);
1127 prev = msg->flags;
1128 idx = log_next(idx);
1129 seq++;
1130 }
1131
1132 /* move first record forward until length fits into the buffer */
1133 seq = clear_seq;
1134 idx = clear_idx;
1135 prev = 0;
1136 while (len > size && seq < log_next_seq) {
1137 struct log *msg = log_from_idx(idx);
1138
1139 len -= msg_print_text(msg, prev, true, NULL, 0);
1140 prev = msg->flags;
1141 idx = log_next(idx);
1142 seq++;
1143 }
1144
1145 /* last message fitting into this dump */
1146 next_seq = log_next_seq;
1147
1148 len = 0;
1149 prev = 0;
1150 while (len >= 0 && seq < next_seq) {
1151 struct log *msg = log_from_idx(idx);
1152 int textlen;
1153
1154 textlen = msg_print_text(msg, prev, true, text,
1155 LOG_LINE_MAX + PREFIX_MAX);
1156 if (textlen < 0) {
1157 len = textlen;
1158 break;
1159 }
1160 idx = log_next(idx);
1161 seq++;
1162 prev = msg->flags;
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;
1175 prev = 0;
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
1190 int do_syslog(int type, char __user *buf, int len, bool from_file)
1191 {
1192 bool clear = false;
1193 static int saved_console_loglevel = -1;
1194 int error;
1195
1196 error = check_syslog_permissions(type, from_file);
1197 if (error)
1198 goto out;
1199
1200 error = security_syslog(type);
1201 if (error)
1202 return error;
1203
1204 switch (type) {
1205 case SYSLOG_ACTION_CLOSE: /* Close log */
1206 break;
1207 case SYSLOG_ACTION_OPEN: /* Open log */
1208 break;
1209 case SYSLOG_ACTION_READ: /* Read from log */
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 }
1220 error = wait_event_interruptible(log_wait,
1221 syslog_seq != log_next_seq);
1222 if (error)
1223 goto out;
1224 error = syslog_print(buf, len);
1225 break;
1226 /* Read/clear last kernel messages */
1227 case SYSLOG_ACTION_READ_CLEAR:
1228 clear = true;
1229 /* FALL THRU */
1230 /* Read last kernel messages */
1231 case SYSLOG_ACTION_READ_ALL:
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 }
1242 error = syslog_print_all(buf, len, clear);
1243 break;
1244 /* Clear ring buffer */
1245 case SYSLOG_ACTION_CLEAR:
1246 syslog_print_all(NULL, 0, true);
1247 break;
1248 /* Disable logging to console */
1249 case SYSLOG_ACTION_CONSOLE_OFF:
1250 if (saved_console_loglevel == -1)
1251 saved_console_loglevel = console_loglevel;
1252 console_loglevel = minimum_console_loglevel;
1253 break;
1254 /* Enable logging to console */
1255 case SYSLOG_ACTION_CONSOLE_ON:
1256 if (saved_console_loglevel != -1) {
1257 console_loglevel = saved_console_loglevel;
1258 saved_console_loglevel = -1;
1259 }
1260 break;
1261 /* Set level of messages printed to console */
1262 case SYSLOG_ACTION_CONSOLE_LEVEL:
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;
1269 /* Implicitly re-enable logging to console */
1270 saved_console_loglevel = -1;
1271 error = 0;
1272 break;
1273 /* Number of chars in the log buffer */
1274 case SYSLOG_ACTION_SIZE_UNREAD:
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;
1280 syslog_prev = 0;
1281 syslog_partial = 0;
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 {
1291 u64 seq = syslog_seq;
1292 u32 idx = syslog_idx;
1293 enum log_flags prev = syslog_prev;
1294
1295 error = 0;
1296 while (seq < log_next_seq) {
1297 struct log *msg = log_from_idx(idx);
1298
1299 error += msg_print_text(msg, prev, true, NULL, 0);
1300 idx = log_next(idx);
1301 seq++;
1302 prev = msg->flags;
1303 }
1304 error -= syslog_partial;
1305 }
1306 raw_spin_unlock_irq(&logbuf_lock);
1307 break;
1308 /* Size of the log buffer */
1309 case SYSLOG_ACTION_SIZE_BUFFER:
1310 error = log_buf_len;
1311 break;
1312 default:
1313 error = -EINVAL;
1314 break;
1315 }
1316 out:
1317 return error;
1318 }
1319
1320 SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
1321 {
1322 return do_syslog(type, buf, len, SYSLOG_FROM_READER);
1323 }
1324
1325 /*
1326 * Call the console drivers, asking them to write out
1327 * log_buf[start] to log_buf[end - 1].
1328 * The console_lock must be held.
1329 */
1330 static void call_console_drivers(int level, const char *text, size_t len)
1331 {
1332 struct console *con;
1333
1334 trace_console(text, len);
1335
1336 if (level >= console_loglevel && !ignore_loglevel)
1337 return;
1338 if (!console_drivers)
1339 return;
1340
1341 for_each_console(con) {
1342 if (printk_disable_uart && (con->flags & CON_CONSDEV))
1343 continue;
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 }
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 */
1362 static void zap_locks(void)
1363 {
1364 static unsigned long oops_timestamp;
1365
1366 if (time_after_eq(jiffies, oops_timestamp) &&
1367 !time_after(jiffies, oops_timestamp + 30 * HZ))
1368 return;
1369
1370 oops_timestamp = jiffies;
1371
1372 debug_locks_off();
1373 /* If a crash is occurring, make sure we can't deadlock */
1374 raw_spin_lock_init(&logbuf_lock);
1375 /* And make sure that we print immediately */
1376 sema_init(&console_sem, 1);
1377 }
1378
1379 /* Check if we have any console registered that can be called early in boot. */
1380 static int have_callable_console(void)
1381 {
1382 struct console *con;
1383
1384 for_each_console(con)
1385 if (con->flags & CON_ANYTIME)
1386 return 1;
1387
1388 return 0;
1389 }
1390
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 */
1399 static 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
1407 * console_lock held, and 'console_locked' set) if it
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 */
1414 static int console_trylock_for_printk(unsigned int cpu)
1415 __releases(&logbuf_lock)
1416 {
1417 int retval = 0, wake = 0;
1418
1419 if (console_trylock()) {
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;
1430 wake = 1;
1431 retval = 0;
1432 }
1433 }
1434 logbuf_cpu = UINT_MAX;
1435 raw_spin_unlock(&logbuf_lock);
1436 if (wake)
1437 up(&console_sem);
1438 return retval;
1439 }
1440
1441 int printk_delay_msec __read_mostly;
1442
1443 static 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
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 */
1461 static 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 */
1469 enum log_flags flags; /* prefix, newline flags */
1470 bool flushed:1; /* buffer sealed and committed */
1471 } cont;
1472
1473 static void cont_flush(enum log_flags flags)
1474 {
1475 if (cont.flushed)
1476 return;
1477 if (cont.len == 0)
1478 return;
1479
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 }
1499 }
1500
1501 static 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)) {
1507 /* the line gets too long, split it up in separate records */
1508 cont_flush(LOG_CONT);
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();
1517 cont.flags = 0;
1518 cont.cons = 0;
1519 cont.flushed = false;
1520 }
1521
1522 memcpy(cont.buf + cont.len, text, len);
1523 cont.len += len;
1524
1525 if (cont.len > (sizeof(cont.buf) * 80) / 100)
1526 cont_flush(LOG_CONT);
1527
1528 return true;
1529 }
1530
1531 static size_t cont_print_text(char *text, size_t size)
1532 {
1533 size_t textlen = 0;
1534 size_t len;
1535
1536 if (cont.cons == 0 && (console_prev & LOG_NEWLINE)) {
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) {
1551 if (cont.flags & LOG_NEWLINE)
1552 text[textlen++] = '\n';
1553 /* got everything, release buffer */
1554 cont.len = 0;
1555 }
1556 return textlen;
1557 }
1558
1559 asmlinkage int vprintk_emit(int facility, int level,
1560 const char *dict, size_t dictlen,
1561 const char *fmt, va_list args)
1562 {
1563 static int recursion_bug;
1564 static char textbuf[LOG_LINE_MAX];
1565 char *text = textbuf;
1566 size_t text_len;
1567 enum log_flags lflags = 0;
1568 unsigned long flags;
1569 int this_cpu;
1570 int printed_len = 0;
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));
1576 boot_delay_msec(level);
1577 printk_delay();
1578
1579 /* This stops the holder of console_sem just where we want him */
1580 local_irq_save(flags);
1581 this_cpu = smp_processor_id();
1582
1583 /*
1584 * Ouch, printk recursed into itself!
1585 */
1586 if (unlikely(logbuf_cpu == this_cpu)) {
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 */
1594 if (!oops_in_progress && !lockdep_recursing(current)) {
1595 recursion_bug = 1;
1596 goto out_restore_irqs;
1597 }
1598 zap_locks();
1599 }
1600
1601 lockdep_off();
1602 raw_spin_lock(&logbuf_lock);
1603 logbuf_cpu = this_cpu;
1604
1605 if (recursion_bug) {
1606 static const char recursion_msg[] =
1607 "BUG: recent printk recursion!";
1608
1609 recursion_bug = 0;
1610 printed_len += strlen(recursion_msg);
1611 /* emit KERN_CRIT message */
1612 log_store(0, 2, LOG_PREFIX|LOG_NEWLINE, 0,
1613 NULL, 0, recursion_msg, printed_len);
1614 }
1615
1616 /*
1617 * The printf needs to come first; we need the syslog
1618 * prefix which might be passed-in as a parameter.
1619 */
1620 text_len = vscnprintf(text, sizeof(textbuf), fmt, args);
1621
1622 /* mark and strip a trailing newline */
1623 if (text_len && text[text_len-1] == '\n') {
1624 text_len--;
1625 lflags |= LOG_NEWLINE;
1626 }
1627
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;
1645 }
1646 }
1647
1648 #ifdef CONFIG_EARLY_PRINTK_DIRECT
1649 printascii(text);
1650 #endif
1651
1652 if (level == -1)
1653 level = default_message_loglevel;
1654
1655 if (dict)
1656 lflags |= LOG_PREFIX|LOG_NEWLINE;
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
1669 if (!(lflags & LOG_NEWLINE)) {
1670 /*
1671 * Flush the conflicting buffer. An earlier newline was missing,
1672 * or another task also prints continuation lines.
1673 */
1674 if (cont.len && (lflags & LOG_PREFIX || cont.owner != current))
1675 cont_flush(LOG_NEWLINE);
1676
1677 /* buffer line if possible, otherwise store it right away */
1678 if (!cont_add(facility, level, text, text_len))
1679 log_store(facility, level, lflags | LOG_CONT, 0,
1680 dict, dictlen, text, text_len);
1681 } else {
1682 bool stored = false;
1683
1684 /*
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.
1689 */
1690 if (cont.len && cont.owner == current) {
1691 if (!(lflags & LOG_PREFIX))
1692 stored = cont_add(facility, level, text, text_len);
1693 cont_flush(LOG_NEWLINE);
1694 }
1695
1696 if (!stored)
1697 log_store(facility, level, lflags, 0,
1698 dict, dictlen, text, text_len);
1699 }
1700 printed_len += text_len;
1701
1702 /*
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.
1706 *
1707 * The console_trylock_for_printk() function will release 'logbuf_lock'
1708 * regardless of whether it actually gets the console semaphore or not.
1709 */
1710 if (console_trylock_for_printk(this_cpu))
1711 console_unlock();
1712
1713 lockdep_on();
1714 out_restore_irqs:
1715 local_irq_restore(flags);
1716
1717 return printed_len;
1718 }
1719 EXPORT_SYMBOL(vprintk_emit);
1720
1721 asmlinkage int vprintk(const char *fmt, va_list args)
1722 {
1723 return vprintk_emit(0, -1, NULL, 0, fmt, args);
1724 }
1725 EXPORT_SYMBOL(vprintk);
1726
1727 asmlinkage 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 }
1740 EXPORT_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 */
1763 asmlinkage 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 }
1782 EXPORT_SYMBOL(printk);
1783
1784 #else /* CONFIG_PRINTK */
1785
1786 #define LOG_LINE_MAX 0
1787 #define PREFIX_MAX 0
1788 #define LOG_LINE_MAX 0
1789 static u64 syslog_seq;
1790 static u32 syslog_idx;
1791 static u64 console_seq;
1792 static u32 console_idx;
1793 static enum log_flags syslog_prev;
1794 static u64 log_first_seq;
1795 static u32 log_first_idx;
1796 static u64 log_next_seq;
1797 static enum log_flags console_prev;
1798 static struct cont {
1799 size_t len;
1800 size_t cons;
1801 u8 level;
1802 bool flushed:1;
1803 } cont;
1804 static struct log *log_from_idx(u32 idx) { return NULL; }
1805 static u32 log_next(u32 idx) { return 0; }
1806 static void call_console_drivers(int level, const char *text, size_t len) {}
1807 static size_t msg_print_text(const struct log *msg, enum log_flags prev,
1808 bool syslog, char *buf, size_t size) { return 0; }
1809 static size_t cont_print_text(char *text, size_t size) { return 0; }
1810
1811 #endif /* CONFIG_PRINTK */
1812
1813 #ifdef CONFIG_EARLY_PRINTK
1814 struct console *early_console;
1815
1816 void 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
1826 asmlinkage 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
1836 static 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 }
1866 /*
1867 * Set up a list of consoles. Called from init/main.c
1868 */
1869 static int __init console_setup(char *str)
1870 {
1871 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
1872 char *s, *options, *brl_options = NULL;
1873 int idx;
1874
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
1890 /*
1891 * Decode str into name, index, options.
1892 */
1893 if (str[0] >= '0' && str[0] <= '9') {
1894 strcpy(buf, "ttyS");
1895 strncpy(buf + 4, str, sizeof(buf) - 5);
1896 } else {
1897 strncpy(buf, str, sizeof(buf) - 1);
1898 }
1899 buf[sizeof(buf) - 1] = 0;
1900 if ((options = strchr(str, ',')) != NULL)
1901 *(options++) = 0;
1902 #ifdef __sparc__
1903 if (!strcmp(str, "ttya"))
1904 strcpy(buf, "ttyS0");
1905 if (!strcmp(str, "ttyb"))
1906 strcpy(buf, "ttyS1");
1907 #endif
1908 for (s = buf; *s; s++)
1909 if ((*s >= '0' && *s <= '9') || *s == ',')
1910 break;
1911 idx = simple_strtoul(s, NULL, 10);
1912 *s = 0;
1913
1914 __add_preferred_console(buf, idx, options, brl_options);
1915 console_set_on_cmdline = 1;
1916 return 1;
1917 }
1918 __setup("console=", console_setup);
1919
1920 /**
1921 * add_preferred_console - add a device to the list of preferred consoles.
1922 * @name: device name
1923 * @idx: device index
1924 * @options: options for this console
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 */
1933 int add_preferred_console(char *name, int idx, char *options)
1934 {
1935 return __add_preferred_console(name, idx, options, NULL);
1936 }
1937
1938 int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
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];
1947 strlcpy(c->name, name_new, sizeof(c->name));
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
1957 bool console_suspend_enabled = 1;
1958 EXPORT_SYMBOL(console_suspend_enabled);
1959
1960 static 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);
1966 module_param_named(console_suspend, console_suspend_enabled,
1967 bool, S_IRUGO | S_IWUSR);
1968 MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
1969 " and hibernate operations");
1970
1971 /**
1972 * suspend_console - suspend the console subsystem
1973 *
1974 * This disables printk() while we go into suspend states
1975 */
1976 void suspend_console(void)
1977 {
1978 if (!console_suspend_enabled)
1979 return;
1980 printk("Suspending console(s) (use no_console_suspend to debug)\n");
1981 console_lock();
1982 console_suspended = 1;
1983 up(&console_sem);
1984 mutex_release(&console_lock_dep_map, 1, _RET_IP_);
1985 }
1986 EXPORT_SYMBOL_GPL(suspend_console);
1987
1988 void resume_console(void)
1989 {
1990 if (!console_suspend_enabled)
1991 return;
1992 down(&console_sem);
1993 mutex_acquire(&console_lock_dep_map, 0, 0, _RET_IP_);
1994 console_suspended = 0;
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
2004 }
2005 EXPORT_SYMBOL_GPL(resume_console);
2006
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 */
2018 static 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:
2024 case CPU_DOWN_FAILED:
2025 case CPU_UP_CANCELED:
2026 console_lock();
2027 console_unlock();
2028 }
2029 return NOTIFY_OK;
2030 }
2031
2032 /**
2033 * console_lock - lock the console system for exclusive use.
2034 *
2035 * Acquires a lock which guarantees that the caller has
2036 * exclusive access to the console system and the console_drivers list.
2037 *
2038 * Can sleep, returns nothing.
2039 */
2040 void console_lock(void)
2041 {
2042 might_sleep();
2043
2044 down(&console_sem);
2045 if (console_suspended)
2046 return;
2047 console_locked = 1;
2048 console_may_schedule = 1;
2049 mutex_acquire(&console_lock_dep_map, 0, 0, _RET_IP_);
2050 }
2051 EXPORT_SYMBOL(console_lock);
2052
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 */
2061 int console_trylock(void)
2062 {
2063 if (down_trylock(&console_sem))
2064 return 0;
2065 if (console_suspended) {
2066 up(&console_sem);
2067 return 0;
2068 }
2069 console_locked = 1;
2070 console_may_schedule = 0;
2071 mutex_acquire(&console_lock_dep_map, 0, 1, _RET_IP_);
2072 return 1;
2073 }
2074 EXPORT_SYMBOL(console_trylock);
2075
2076 int is_console_locked(void)
2077 {
2078 return console_locked;
2079 }
2080
2081 static 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;
2106 out:
2107 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2108 }
2109
2110 /**
2111 * console_unlock - unlock the console system
2112 *
2113 * Releases the console_lock which the caller holds on the console system
2114 * and the console driver list.
2115 *
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.
2119 *
2120 * If there is output waiting, we wake /dev/kmsg and syslog() users.
2121 *
2122 * console_unlock(); may be called from any context.
2123 */
2124 #ifdef LOG_TOO_MUCH_WARNING
2125 static int console_log_max = 400000;
2126 static int already_skip_log;
2127 #endif
2128 void console_unlock(void)
2129 {
2130 static char text[LOG_LINE_MAX + PREFIX_MAX];
2131 static u64 seen_seq;
2132 unsigned long flags;
2133 bool wake_klogd = false;
2134 bool retry;
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
2141
2142 if (console_suspended) {
2143 up(&console_sem);
2144 return;
2145 }
2146
2147 console_may_schedule = 0;
2148
2149 /* flush buffered message fragment immediately to console */
2150 console_cont_flush(text, sizeof(text));
2151 again:
2152 for (;;) {
2153 struct log *msg;
2154 size_t len;
2155 int level;
2156
2157 raw_spin_lock_irqsave(&logbuf_lock, flags);
2158 #ifdef LOG_TOO_MUCH_WARNING /*For Resume log too much*/
2159 if (log_in_resume) {
2160 t1 = sched_clock();
2161 }
2162 #endif
2163
2164 if (seen_seq != log_next_seq) {
2165 wake_klogd = true;
2166 seen_seq = log_next_seq;
2167 }
2168
2169 if (console_seq < log_first_seq) {
2170 /* messages are gone, move to first one */
2171 console_seq = log_first_seq;
2172 console_idx = log_first_idx;
2173 console_prev = 0;
2174 }
2175 skip:
2176 if (console_seq == log_next_seq)
2177 break;
2178
2179 msg = log_from_idx(console_idx);
2180 if (msg->flags & LOG_NOCONS) {
2181 /*
2182 * Skip record we have buffered and already printed
2183 * directly to the console when we received it.
2184 */
2185 console_idx = log_next(console_idx);
2186 console_seq++;
2187 /*
2188 * We will get here again when we register a new
2189 * CON_PRINTBUFFER console. Clear the flag so we
2190 * will properly dump everything later.
2191 */
2192 msg->flags &= ~LOG_NOCONS;
2193 console_prev = msg->flags;
2194 goto skip;
2195 }
2196
2197 level = msg->level;
2198 len = msg_print_text(msg, console_prev, false,
2199 text, sizeof(text));
2200 console_idx = log_next(console_idx);
2201 console_seq++;
2202 console_prev = msg->flags;
2203 raw_spin_unlock(&logbuf_lock);
2204
2205 stop_critical_timings(); /* don't trace print latency */
2206 #ifdef LOG_TOO_MUCH_WARNING
2207 /*
2208 For uart console, 10us/per chars
2209 400,000 chars = need to wait 4.0 sec
2210 normal case: 4sec
2211 */
2212 if (log_in_resume) {
2213 org_loglevel = console_loglevel;
2214 console_loglevel = 4;
2215 }
2216 total_log_size += len;
2217 if (total_log_size < console_log_max)
2218 call_console_drivers(level, text, len);
2219 else if (!already_skip_log) {
2220 sprintf(aee_str, "PRINTK too much:%lu", total_log_size);
2221 aee_kernel_warning(aee_str, "Need to shrink kernel log");
2222 already_skip_log = 1;
2223 }
2224 /**/
2225 start_critical_timings();
2226 /* For Resume log too much*/
2227 if (log_in_resume) {
2228 t2 = sched_clock();
2229 console_loglevel = org_loglevel;
2230 if (t2 - t1 > 100000000) {
2231 sprintf( aee_str,"[RESUME CONSOLE too long:%lluns>100ms] s:%lluns, e:%lluns\n", t2 - t1, t1, t2);
2232 aee_kernel_warning(aee_str, "Need to shrink kernel log");
2233 }
2234 }
2235
2236 /**/
2237 #else
2238 start_critical_timings();
2239 call_console_drivers(level, text, len);
2240 #endif
2241 local_irq_restore(flags);
2242 }
2243 console_locked = 0;
2244 mutex_release(&console_lock_dep_map, 1, _RET_IP_);
2245
2246 /* Release the exclusive_console once it is used */
2247 if (unlikely(exclusive_console))
2248 exclusive_console = NULL;
2249
2250 raw_spin_unlock(&logbuf_lock);
2251
2252 up(&console_sem);
2253
2254 /*
2255 * Someone could have filled up the buffer again, so re-check if there's
2256 * something to flush. In case we cannot trylock the console_sem again,
2257 * there's a new owner and the console_unlock() from them will do the
2258 * flush, no worries.
2259 */
2260 raw_spin_lock(&logbuf_lock);
2261 retry = console_seq != log_next_seq;
2262 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2263
2264 if (retry && console_trylock())
2265 goto again;
2266
2267 if (wake_klogd)
2268 wake_up_klogd();
2269 }
2270 EXPORT_SYMBOL(console_unlock);
2271
2272 /**
2273 * console_conditional_schedule - yield the CPU if required
2274 *
2275 * If the console code is currently allowed to sleep, and
2276 * if this CPU should yield the CPU to another task, do
2277 * so here.
2278 *
2279 * Must be called within console_lock();.
2280 */
2281 void __sched console_conditional_schedule(void)
2282 {
2283 if (console_may_schedule)
2284 cond_resched();
2285 }
2286 EXPORT_SYMBOL(console_conditional_schedule);
2287
2288 void console_unblank(void)
2289 {
2290 struct console *c;
2291
2292 /*
2293 * console_unblank can no longer be called in interrupt context unless
2294 * oops_in_progress is set to 1..
2295 */
2296 if (oops_in_progress) {
2297 if (down_trylock(&console_sem) != 0)
2298 return;
2299 } else
2300 console_lock();
2301
2302 console_locked = 1;
2303 console_may_schedule = 0;
2304 for_each_console(c)
2305 if ((c->flags & CON_ENABLED) && c->unblank)
2306 c->unblank();
2307 console_unlock();
2308 }
2309
2310 /*
2311 * Return the console tty driver structure and its associated index
2312 */
2313 struct tty_driver *console_device(int *index)
2314 {
2315 struct console *c;
2316 struct tty_driver *driver = NULL;
2317
2318 console_lock();
2319 for_each_console(c) {
2320 if (!c->device)
2321 continue;
2322 driver = c->device(c, index);
2323 if (driver)
2324 break;
2325 }
2326 console_unlock();
2327 return driver;
2328 }
2329
2330 /*
2331 * Prevent further output on the passed console device so that (for example)
2332 * serial drivers can disable console output before suspending a port, and can
2333 * re-enable output afterwards.
2334 */
2335 void console_stop(struct console *console)
2336 {
2337 console_lock();
2338 console->flags &= ~CON_ENABLED;
2339 console_unlock();
2340 }
2341 EXPORT_SYMBOL(console_stop);
2342
2343 void console_start(struct console *console)
2344 {
2345 console_lock();
2346 console->flags |= CON_ENABLED;
2347 console_unlock();
2348 }
2349 EXPORT_SYMBOL(console_start);
2350
2351 static int __read_mostly keep_bootcon;
2352
2353 static int __init keep_bootcon_setup(char *str)
2354 {
2355 keep_bootcon = 1;
2356 printk(KERN_INFO "debug: skip boot console de-registration.\n");
2357
2358 return 0;
2359 }
2360
2361 early_param("keep_bootcon", keep_bootcon_setup);
2362
2363 /*
2364 * The console driver calls this routine during kernel initialization
2365 * to register the console printing procedure with printk() and to
2366 * print any messages that were printed by the kernel before the
2367 * console driver was initialized.
2368 *
2369 * This can happen pretty early during the boot process (because of
2370 * early_printk) - sometimes before setup_arch() completes - be careful
2371 * of what kernel features are used - they may not be initialised yet.
2372 *
2373 * There are two types of consoles - bootconsoles (early_printk) and
2374 * "real" consoles (everything which is not a bootconsole) which are
2375 * handled differently.
2376 * - Any number of bootconsoles can be registered at any time.
2377 * - As soon as a "real" console is registered, all bootconsoles
2378 * will be unregistered automatically.
2379 * - Once a "real" console is registered, any attempt to register a
2380 * bootconsoles will be rejected
2381 */
2382 void register_console(struct console *newcon)
2383 {
2384 int i;
2385 unsigned long flags;
2386 struct console *bcon = NULL;
2387
2388 /*
2389 * before we register a new CON_BOOT console, make sure we don't
2390 * already have a valid console
2391 */
2392 if (console_drivers && newcon->flags & CON_BOOT) {
2393 /* find the last or real console */
2394 for_each_console(bcon) {
2395 if (!(bcon->flags & CON_BOOT)) {
2396 printk(KERN_INFO "Too late to register bootconsole %s%d\n",
2397 newcon->name, newcon->index);
2398 return;
2399 }
2400 }
2401 }
2402
2403 if (console_drivers && console_drivers->flags & CON_BOOT)
2404 bcon = console_drivers;
2405
2406 if (preferred_console < 0 || bcon || !console_drivers)
2407 preferred_console = selected_console;
2408
2409 if (newcon->early_setup)
2410 newcon->early_setup();
2411
2412 /*
2413 * See if we want to use this console driver. If we
2414 * didn't select a console we take the first one
2415 * that registers here.
2416 */
2417 if (preferred_console < 0) {
2418 if (newcon->index < 0)
2419 newcon->index = 0;
2420 if (newcon->setup == NULL ||
2421 newcon->setup(newcon, NULL) == 0) {
2422 newcon->flags |= CON_ENABLED;
2423 if (newcon->device) {
2424 newcon->flags |= CON_CONSDEV;
2425 preferred_console = 0;
2426 }
2427 }
2428 }
2429
2430 /*
2431 * See if this console matches one we selected on
2432 * the command line.
2433 */
2434 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
2435 i++) {
2436 BUILD_BUG_ON(sizeof(console_cmdline[i].name) !=
2437 sizeof(newcon->name));
2438 if (strcmp(console_cmdline[i].name, newcon->name) != 0)
2439 continue;
2440 if (newcon->index >= 0 &&
2441 newcon->index != console_cmdline[i].index)
2442 continue;
2443 if (newcon->index < 0)
2444 newcon->index = console_cmdline[i].index;
2445 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
2446 if (console_cmdline[i].brl_options) {
2447 newcon->flags |= CON_BRL;
2448 braille_register_console(newcon,
2449 console_cmdline[i].index,
2450 console_cmdline[i].options,
2451 console_cmdline[i].brl_options);
2452 return;
2453 }
2454 #endif
2455 if (newcon->setup &&
2456 newcon->setup(newcon, console_cmdline[i].options) != 0)
2457 break;
2458 newcon->flags |= CON_ENABLED;
2459 newcon->index = console_cmdline[i].index;
2460 if (i == selected_console) {
2461 newcon->flags |= CON_CONSDEV;
2462 preferred_console = selected_console;
2463 }
2464 break;
2465 }
2466
2467 if (!(newcon->flags & CON_ENABLED))
2468 return;
2469
2470 /*
2471 * If we have a bootconsole, and are switching to a real console,
2472 * don't print everything out again, since when the boot console, and
2473 * the real console are the same physical device, it's annoying to
2474 * see the beginning boot messages twice
2475 */
2476 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
2477 newcon->flags &= ~CON_PRINTBUFFER;
2478
2479 /*
2480 * Put this console in the list - keep the
2481 * preferred driver at the head of the list.
2482 */
2483 console_lock();
2484 if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
2485 newcon->next = console_drivers;
2486 console_drivers = newcon;
2487 if (newcon->next)
2488 newcon->next->flags &= ~CON_CONSDEV;
2489 } else {
2490 newcon->next = console_drivers->next;
2491 console_drivers->next = newcon;
2492 }
2493 if (newcon->flags & CON_PRINTBUFFER) {
2494 /*
2495 * console_unlock(); will print out the buffered messages
2496 * for us.
2497 */
2498 raw_spin_lock_irqsave(&logbuf_lock, flags);
2499 console_seq = syslog_seq;
2500 console_idx = syslog_idx;
2501 console_prev = syslog_prev;
2502 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2503 /*
2504 * We're about to replay the log buffer. Only do this to the
2505 * just-registered console to avoid excessive message spam to
2506 * the already-registered consoles.
2507 */
2508 exclusive_console = newcon;
2509 }
2510 console_unlock();
2511 console_sysfs_notify();
2512
2513 /*
2514 * By unregistering the bootconsoles after we enable the real console
2515 * we get the "console xxx enabled" message on all the consoles -
2516 * boot consoles, real consoles, etc - this is to ensure that end
2517 * users know there might be something in the kernel's log buffer that
2518 * went to the bootconsole (that they do not see on the real console)
2519 */
2520 if (bcon &&
2521 ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
2522 !keep_bootcon) {
2523 /* we need to iterate through twice, to make sure we print
2524 * everything out, before we unregister the console(s)
2525 */
2526 printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
2527 newcon->name, newcon->index);
2528 for_each_console(bcon)
2529 if (bcon->flags & CON_BOOT)
2530 unregister_console(bcon);
2531 } else {
2532 printk(KERN_INFO "%sconsole [%s%d] enabled\n",
2533 (newcon->flags & CON_BOOT) ? "boot" : "" ,
2534 newcon->name, newcon->index);
2535 }
2536 }
2537 EXPORT_SYMBOL(register_console);
2538
2539 int unregister_console(struct console *console)
2540 {
2541 struct console *a, *b;
2542 int res = 1;
2543
2544 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
2545 if (console->flags & CON_BRL)
2546 return braille_unregister_console(console);
2547 #endif
2548
2549 console_lock();
2550 if (console_drivers == console) {
2551 console_drivers=console->next;
2552 res = 0;
2553 } else if (console_drivers) {
2554 for (a=console_drivers->next, b=console_drivers ;
2555 a; b=a, a=b->next) {
2556 if (a == console) {
2557 b->next = a->next;
2558 res = 0;
2559 break;
2560 }
2561 }
2562 }
2563
2564 /*
2565 * If this isn't the last console and it has CON_CONSDEV set, we
2566 * need to set it on the next preferred console.
2567 */
2568 if (console_drivers != NULL && console->flags & CON_CONSDEV)
2569 console_drivers->flags |= CON_CONSDEV;
2570
2571 console_unlock();
2572 console_sysfs_notify();
2573 return res;
2574 }
2575 EXPORT_SYMBOL(unregister_console);
2576
2577 static int __init printk_late_init(void)
2578 {
2579 struct console *con;
2580
2581 for_each_console(con) {
2582 if (!keep_bootcon && con->flags & CON_BOOT) {
2583 printk(KERN_INFO "turn off boot console %s%d\n",
2584 con->name, con->index);
2585 unregister_console(con);
2586 }
2587 }
2588 hotcpu_notifier(console_cpu_notify, 0);
2589 return 0;
2590 }
2591 late_initcall(printk_late_init);
2592
2593 #if defined CONFIG_PRINTK
2594 /*
2595 * Delayed printk version, for scheduler-internal messages:
2596 */
2597 #define PRINTK_BUF_SIZE 512
2598
2599 #define PRINTK_PENDING_WAKEUP 0x01
2600 #define PRINTK_PENDING_SCHED 0x02
2601
2602 static DEFINE_PER_CPU(int, printk_pending);
2603 static DEFINE_PER_CPU(char [PRINTK_BUF_SIZE], printk_sched_buf);
2604 static DEFINE_PER_CPU(int, printk_sched_length);
2605
2606 static void wake_up_klogd_work_func(struct irq_work *irq_work)
2607 {
2608 int pending = __this_cpu_xchg(printk_pending, 0);
2609
2610 if (pending & PRINTK_PENDING_SCHED) {
2611 char *buf = __get_cpu_var(printk_sched_buf);
2612 printk(KERN_WARNING "[printk_delayed:start]\n");
2613 printk(KERN_WARNING "%s", buf);
2614 printk(KERN_WARNING "[printk_delayed:done]\n");
2615 __get_cpu_var(printk_sched_length) = 0;
2616 }
2617
2618 if (pending & PRINTK_PENDING_WAKEUP)
2619 wake_up_interruptible(&log_wait);
2620 }
2621
2622 static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) = {
2623 .func = wake_up_klogd_work_func,
2624 .flags = IRQ_WORK_LAZY,
2625 };
2626
2627 void wake_up_klogd(void)
2628 {
2629 preempt_disable();
2630 if (waitqueue_active(&log_wait)) {
2631 this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP);
2632 irq_work_queue(&__get_cpu_var(wake_up_klogd_work));
2633 }
2634 preempt_enable();
2635 }
2636
2637 int printk_deferred(const char *fmt, ...)
2638 {
2639 unsigned long flags;
2640 va_list args;
2641 char *buf;
2642 int r;
2643 int buf_length;
2644 local_irq_save(flags);
2645 buf = __get_cpu_var(printk_sched_buf);
2646 buf_length = __get_cpu_var(printk_sched_length);
2647
2648 va_start(args, fmt);
2649 if(PRINTK_BUF_SIZE >= buf_length){
2650 r = vsnprintf((buf_length + buf), PRINTK_BUF_SIZE-buf_length, fmt, args);
2651 __get_cpu_var(printk_sched_length) += r;
2652 }else{
2653 printk("delayed log buf overflow, size:%d\n", buf_length);
2654 r = 0;
2655 }
2656 va_end(args);
2657
2658 __this_cpu_or(printk_pending, PRINTK_PENDING_SCHED);
2659 irq_work_queue(&__get_cpu_var(wake_up_klogd_work));
2660 local_irq_restore(flags);
2661
2662 return r;
2663 }
2664
2665 /*
2666 * printk rate limiting, lifted from the networking subsystem.
2667 *
2668 * This enforces a rate limit: not more than 10 kernel messages
2669 * every 5s to make a denial-of-service attack impossible.
2670 */
2671 DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
2672
2673 int __printk_ratelimit(const char *func)
2674 {
2675 return ___ratelimit(&printk_ratelimit_state, func);
2676 }
2677 EXPORT_SYMBOL(__printk_ratelimit);
2678
2679 /**
2680 * printk_timed_ratelimit - caller-controlled printk ratelimiting
2681 * @caller_jiffies: pointer to caller's state
2682 * @interval_msecs: minimum interval between prints
2683 *
2684 * printk_timed_ratelimit() returns true if more than @interval_msecs
2685 * milliseconds have elapsed since the last time printk_timed_ratelimit()
2686 * returned true.
2687 */
2688 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
2689 unsigned int interval_msecs)
2690 {
2691 if (*caller_jiffies == 0
2692 || !time_in_range(jiffies, *caller_jiffies,
2693 *caller_jiffies
2694 + msecs_to_jiffies(interval_msecs))) {
2695 *caller_jiffies = jiffies;
2696 return true;
2697 }
2698 return false;
2699 }
2700 EXPORT_SYMBOL(printk_timed_ratelimit);
2701
2702 static DEFINE_SPINLOCK(dump_list_lock);
2703 static LIST_HEAD(dump_list);
2704
2705 /**
2706 * kmsg_dump_register - register a kernel log dumper.
2707 * @dumper: pointer to the kmsg_dumper structure
2708 *
2709 * Adds a kernel log dumper to the system. The dump callback in the
2710 * structure will be called when the kernel oopses or panics and must be
2711 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
2712 */
2713 int kmsg_dump_register(struct kmsg_dumper *dumper)
2714 {
2715 unsigned long flags;
2716 int err = -EBUSY;
2717
2718 /* The dump callback needs to be set */
2719 if (!dumper->dump)
2720 return -EINVAL;
2721
2722 spin_lock_irqsave(&dump_list_lock, flags);
2723 /* Don't allow registering multiple times */
2724 if (!dumper->registered) {
2725 dumper->registered = 1;
2726 list_add_tail_rcu(&dumper->list, &dump_list);
2727 err = 0;
2728 }
2729 spin_unlock_irqrestore(&dump_list_lock, flags);
2730
2731 return err;
2732 }
2733 EXPORT_SYMBOL_GPL(kmsg_dump_register);
2734
2735 /**
2736 * kmsg_dump_unregister - unregister a kmsg dumper.
2737 * @dumper: pointer to the kmsg_dumper structure
2738 *
2739 * Removes a dump device from the system. Returns zero on success and
2740 * %-EINVAL otherwise.
2741 */
2742 int kmsg_dump_unregister(struct kmsg_dumper *dumper)
2743 {
2744 unsigned long flags;
2745 int err = -EINVAL;
2746
2747 spin_lock_irqsave(&dump_list_lock, flags);
2748 if (dumper->registered) {
2749 dumper->registered = 0;
2750 list_del_rcu(&dumper->list);
2751 err = 0;
2752 }
2753 spin_unlock_irqrestore(&dump_list_lock, flags);
2754 synchronize_rcu();
2755
2756 return err;
2757 }
2758 EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
2759
2760 static bool always_kmsg_dump;
2761 module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR);
2762
2763 /**
2764 * kmsg_dump - dump kernel log to kernel message dumpers.
2765 * @reason: the reason (oops, panic etc) for dumping
2766 *
2767 * Call each of the registered dumper's dump() callback, which can
2768 * retrieve the kmsg records with kmsg_dump_get_line() or
2769 * kmsg_dump_get_buffer().
2770 */
2771 void kmsg_dump(enum kmsg_dump_reason reason)
2772 {
2773 struct kmsg_dumper *dumper;
2774 unsigned long flags;
2775
2776 if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
2777 return;
2778
2779 rcu_read_lock();
2780 list_for_each_entry_rcu(dumper, &dump_list, list) {
2781 if (dumper->max_reason && reason > dumper->max_reason)
2782 continue;
2783
2784 /* initialize iterator with data about the stored records */
2785 dumper->active = true;
2786
2787 raw_spin_lock_irqsave(&logbuf_lock, flags);
2788 dumper->cur_seq = clear_seq;
2789 dumper->cur_idx = clear_idx;
2790 dumper->next_seq = log_next_seq;
2791 dumper->next_idx = log_next_idx;
2792 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2793
2794 /* invoke dumper which will iterate over records */
2795 dumper->dump(dumper, reason);
2796
2797 /* reset iterator */
2798 dumper->active = false;
2799 }
2800 rcu_read_unlock();
2801 }
2802
2803 /**
2804 * kmsg_dump_get_line_nolock - retrieve one kmsg log line (unlocked version)
2805 * @dumper: registered kmsg dumper
2806 * @syslog: include the "<4>" prefixes
2807 * @line: buffer to copy the line to
2808 * @size: maximum size of the buffer
2809 * @len: length of line placed into buffer
2810 *
2811 * Start at the beginning of the kmsg buffer, with the oldest kmsg
2812 * record, and copy one record into the provided buffer.
2813 *
2814 * Consecutive calls will return the next available record moving
2815 * towards the end of the buffer with the youngest messages.
2816 *
2817 * A return value of FALSE indicates that there are no more records to
2818 * read.
2819 *
2820 * The function is similar to kmsg_dump_get_line(), but grabs no locks.
2821 */
2822 bool kmsg_dump_get_line_nolock(struct kmsg_dumper *dumper, bool syslog,
2823 char *line, size_t size, size_t *len)
2824 {
2825 struct log *msg;
2826 size_t l = 0;
2827 bool ret = false;
2828
2829 if (!dumper->active)
2830 goto out;
2831
2832 if (dumper->cur_seq < log_first_seq) {
2833 /* messages are gone, move to first available one */
2834 dumper->cur_seq = log_first_seq;
2835 dumper->cur_idx = log_first_idx;
2836 }
2837
2838 /* last entry */
2839 if (dumper->cur_seq >= log_next_seq)
2840 goto out;
2841
2842 msg = log_from_idx(dumper->cur_idx);
2843 l = msg_print_text(msg, 0, syslog, line, size);
2844
2845 dumper->cur_idx = log_next(dumper->cur_idx);
2846 dumper->cur_seq++;
2847 ret = true;
2848 out:
2849 if (len)
2850 *len = l;
2851 return ret;
2852 }
2853
2854 /**
2855 * kmsg_dump_get_line - retrieve one kmsg log line
2856 * @dumper: registered kmsg dumper
2857 * @syslog: include the "<4>" prefixes
2858 * @line: buffer to copy the line to
2859 * @size: maximum size of the buffer
2860 * @len: length of line placed into buffer
2861 *
2862 * Start at the beginning of the kmsg buffer, with the oldest kmsg
2863 * record, and copy one record into the provided buffer.
2864 *
2865 * Consecutive calls will return the next available record moving
2866 * towards the end of the buffer with the youngest messages.
2867 *
2868 * A return value of FALSE indicates that there are no more records to
2869 * read.
2870 */
2871 bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog,
2872 char *line, size_t size, size_t *len)
2873 {
2874 unsigned long flags;
2875 bool ret;
2876
2877 raw_spin_lock_irqsave(&logbuf_lock, flags);
2878 ret = kmsg_dump_get_line_nolock(dumper, syslog, line, size, len);
2879 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2880
2881 return ret;
2882 }
2883 EXPORT_SYMBOL_GPL(kmsg_dump_get_line);
2884
2885 /**
2886 * kmsg_dump_get_buffer - copy kmsg log lines
2887 * @dumper: registered kmsg dumper
2888 * @syslog: include the "<4>" prefixes
2889 * @buf: buffer to copy the line to
2890 * @size: maximum size of the buffer
2891 * @len: length of line placed into buffer
2892 *
2893 * Start at the end of the kmsg buffer and fill the provided buffer
2894 * with as many of the the *youngest* kmsg records that fit into it.
2895 * If the buffer is large enough, all available kmsg records will be
2896 * copied with a single call.
2897 *
2898 * Consecutive calls will fill the buffer with the next block of
2899 * available older records, not including the earlier retrieved ones.
2900 *
2901 * A return value of FALSE indicates that there are no more records to
2902 * read.
2903 */
2904 bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
2905 char *buf, size_t size, size_t *len)
2906 {
2907 unsigned long flags;
2908 u64 seq;
2909 u32 idx;
2910 u64 next_seq;
2911 u32 next_idx;
2912 enum log_flags prev;
2913 size_t l = 0;
2914 bool ret = false;
2915
2916 if (!dumper->active)
2917 goto out;
2918
2919 raw_spin_lock_irqsave(&logbuf_lock, flags);
2920 if (dumper->cur_seq < log_first_seq) {
2921 /* messages are gone, move to first available one */
2922 dumper->cur_seq = log_first_seq;
2923 dumper->cur_idx = log_first_idx;
2924 }
2925
2926 /* last entry */
2927 if (dumper->cur_seq >= dumper->next_seq) {
2928 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2929 goto out;
2930 }
2931
2932 /* calculate length of entire buffer */
2933 seq = dumper->cur_seq;
2934 idx = dumper->cur_idx;
2935 prev = 0;
2936 while (seq < dumper->next_seq) {
2937 struct log *msg = log_from_idx(idx);
2938
2939 l += msg_print_text(msg, prev, true, NULL, 0);
2940 idx = log_next(idx);
2941 seq++;
2942 prev = msg->flags;
2943 }
2944
2945 /* move first record forward until length fits into the buffer */
2946 seq = dumper->cur_seq;
2947 idx = dumper->cur_idx;
2948 prev = 0;
2949 while (l > size && seq < dumper->next_seq) {
2950 struct log *msg = log_from_idx(idx);
2951
2952 l -= msg_print_text(msg, prev, true, NULL, 0);
2953 idx = log_next(idx);
2954 seq++;
2955 prev = msg->flags;
2956 }
2957
2958 /* last message in next interation */
2959 next_seq = seq;
2960 next_idx = idx;
2961
2962 l = 0;
2963 prev = 0;
2964 while (seq < dumper->next_seq) {
2965 struct log *msg = log_from_idx(idx);
2966
2967 l += msg_print_text(msg, prev, syslog, buf + l, size - l);
2968 idx = log_next(idx);
2969 seq++;
2970 prev = msg->flags;
2971 }
2972
2973 dumper->next_seq = next_seq;
2974 dumper->next_idx = next_idx;
2975 ret = true;
2976 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2977 out:
2978 if (len)
2979 *len = l;
2980 return ret;
2981 }
2982 EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer);
2983
2984 /**
2985 * kmsg_dump_rewind_nolock - reset the interator (unlocked version)
2986 * @dumper: registered kmsg dumper
2987 *
2988 * Reset the dumper's iterator so that kmsg_dump_get_line() and
2989 * kmsg_dump_get_buffer() can be called again and used multiple
2990 * times within the same dumper.dump() callback.
2991 *
2992 * The function is similar to kmsg_dump_rewind(), but grabs no locks.
2993 */
2994 void kmsg_dump_rewind_nolock(struct kmsg_dumper *dumper)
2995 {
2996 dumper->cur_seq = clear_seq;
2997 dumper->cur_idx = clear_idx;
2998 dumper->next_seq = log_next_seq;
2999 dumper->next_idx = log_next_idx;
3000 }
3001
3002 /**
3003 * kmsg_dump_rewind - reset the interator
3004 * @dumper: registered kmsg dumper
3005 *
3006 * Reset the dumper's iterator so that kmsg_dump_get_line() and
3007 * kmsg_dump_get_buffer() can be called again and used multiple
3008 * times within the same dumper.dump() callback.
3009 */
3010 void kmsg_dump_rewind(struct kmsg_dumper *dumper)
3011 {
3012 unsigned long flags;
3013
3014 raw_spin_lock_irqsave(&logbuf_lock, flags);
3015 kmsg_dump_rewind_nolock(dumper);
3016 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
3017 }
3018 EXPORT_SYMBOL_GPL(kmsg_dump_rewind);
3019
3020 static char dump_stack_arch_desc_str[128];
3021
3022 /**
3023 * dump_stack_set_arch_desc - set arch-specific str to show with task dumps
3024 * @fmt: printf-style format string
3025 * @...: arguments for the format string
3026 *
3027 * The configured string will be printed right after utsname during task
3028 * dumps. Usually used to add arch-specific system identifiers. If an
3029 * arch wants to make use of such an ID string, it should initialize this
3030 * as soon as possible during boot.
3031 */
3032 void __init dump_stack_set_arch_desc(const char *fmt, ...)
3033 {
3034 va_list args;
3035
3036 va_start(args, fmt);
3037 vsnprintf(dump_stack_arch_desc_str, sizeof(dump_stack_arch_desc_str),
3038 fmt, args);
3039 va_end(args);
3040 }
3041
3042 /**
3043 * dump_stack_print_info - print generic debug info for dump_stack()
3044 * @log_lvl: log level
3045 *
3046 * Arch-specific dump_stack() implementations can use this function to
3047 * print out the same debug information as the generic dump_stack().
3048 */
3049 void dump_stack_print_info(const char *log_lvl)
3050 {
3051 printk("%sCPU: %d PID: %d Comm: %.20s %s %s %.*s\n",
3052 log_lvl, raw_smp_processor_id(), current->pid, current->comm,
3053 print_tainted(), init_utsname()->release,
3054 (int)strcspn(init_utsname()->version, " "),
3055 init_utsname()->version);
3056
3057 if (dump_stack_arch_desc_str[0] != '\0')
3058 printk("%sHardware name: %s\n",
3059 log_lvl, dump_stack_arch_desc_str);
3060
3061 print_worker_info(log_lvl, current);
3062 }
3063
3064 /**
3065 * show_regs_print_info - print generic debug info for show_regs()
3066 * @log_lvl: log level
3067 *
3068 * show_regs() implementations can use this function to print out generic
3069 * debug information.
3070 */
3071 void show_regs_print_info(const char *log_lvl)
3072 {
3073 dump_stack_print_info(log_lvl);
3074
3075 printk("%stask: %p ti: %p task.ti: %p\n",
3076 log_lvl, current, current_thread_info(),
3077 task_thread_info(current));
3078 }
3079
3080 void get_kernel_log_buffer(unsigned long *addr, unsigned long *size, unsigned long *start)
3081 {
3082 *addr = (unsigned long)log_buf;
3083 *size = log_buf_len;
3084 *start = (unsigned long)&log_first_idx;
3085 }
3086 #endif