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