2 * linux/kernel/printk.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
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).
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
20 #include <linux/kernel.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>
53 #include <asm/uaccess.h>
55 #define CREATE_TRACE_POINTS
56 #include <trace/events/printk.h>
59 #define LOG_TOO_MUCH_WARNING
60 #ifdef LOG_TOO_MUCH_WARNING
61 static int log_in_resume
;
64 #ifdef CONFIG_EARLY_PRINTK_DIRECT
65 extern void printascii(char *);
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
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 */
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 */
83 EXPORT_SYMBOL_GPL(console_printk
);
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.
90 EXPORT_SYMBOL(oops_in_progress
);
93 * console_sem protects the console_drivers list, and also
94 * provides serialisation for access to the entire console
97 static DEFINE_SEMAPHORE(console_sem
);
98 struct console
*console_drivers
;
99 EXPORT_SYMBOL_GPL(console_drivers
);
101 #ifdef CONFIG_LOCKDEP
102 static struct lockdep_map console_lock_dep_map
= {
103 .name
= "console_lock"
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
115 static int console_locked
, console_suspended
;
118 * If exclusive_console is non-NULL then only this console is to be printed to.
120 static struct console
*exclusive_console
;
123 * Array of consoles built from command line options (console=)
125 struct console_cmdline
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 */
135 #define MAX_CMDLINECONSOLES 8
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
);
143 /* Flag: console code may call schedule() */
144 static int console_may_schedule
;
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.
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
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.
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.
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
169 * Optionally, a message can carry a dictionary of properties (key/value pairs),
170 * to provide userspace with a machine-readable message context.
172 * Examples for well-defined, commonly used property names are:
173 * DEVICE=b12:8 device identifier
177 * +sound:card0 subsystem:devname
178 * SUBSYSTEM=pci driver-core subsystem name
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.
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"
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"
196 * 0032 00 00 00 padding to next message header
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.
202 * /dev/kmsg exports the structured data in the following line format:
203 * "level,sequnum,timestamp;<message text>\n"
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.
209 * Users of the export format should ignore possible additional values
210 * separated by ',', and find the message after the ';' character.
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 */
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 */
231 * The logbuf_lock protects kmsg buffer, indices, counters. It is also
232 * used in interesting ways to provide interlocking in console_unlock();
234 static DEFINE_RAW_SPINLOCK(logbuf_lock
);
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
;
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
;
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
;
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
;
257 /* the next printk record to read after the last 'clear' command */
258 static u64 clear_seq
;
259 static u32 clear_idx
;
261 #define PREFIX_MAX 32
262 #define LOG_LINE_MAX 1024 - PREFIX_MAX
265 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
268 #define LOG_ALIGN __alignof__(struct log)
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
;
275 #ifdef CONFIG_MT_PRINTK_UART_CONSOLE
277 extern int mt_need_uart_console
;
278 inline void mt_disable_uart()
280 if (mt_need_uart_console
== 0) {
281 printk("<< printk console disable >>\n");
282 printk_disable_uart
= 1;
284 printk("<< printk console can't be disabled >>\n");
287 inline void mt_enable_uart()
289 if (mt_need_uart_console
== 1) {
290 if (printk_disable_uart
== 0)
292 printk_disable_uart
= 0;
293 printk("<< printk console enable >>\n");
295 printk("<< printk console can't be enabled >>\n");
300 /* cpu currently holding logbuf_lock */
301 static volatile unsigned int logbuf_cpu
= UINT_MAX
;
303 /* human readable text of the record */
304 static char *log_text(const struct log
*msg
)
306 return (char *)msg
+ sizeof(struct log
);
309 /* optional key/value pair dictionary attached to the record */
310 static char *log_dict(const struct log
*msg
)
312 return (char *)msg
+ sizeof(struct log
) + msg
->text_len
;
315 /* get record by index; idx must point to valid msg */
316 static struct log
*log_from_idx(u32 idx
)
318 struct log
*msg
= (struct log
*)(log_buf
+ idx
);
321 * A length == 0 record is the end of buffer marker. Wrap around and
322 * read the message at the start of the buffer.
325 return (struct log
*)log_buf
;
329 /* get next record; idx must point to valid msg */
330 static u32
log_next(u32 idx
)
332 struct log
*msg
= (struct log
*)(log_buf
+ idx
);
334 /* length == 0 indicates the end of the buffer; wrap */
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.
341 msg
= (struct log
*)log_buf
;
344 return idx
+ msg
->len
;
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
)
355 int this_cpu
= smp_processor_id();
356 char state
= __raw_get_cpu_var(printk_state
);
358 __raw_get_cpu_var(printk_state
) = ' ';
364 if (console_suspended
== 0) {
365 tlen
= snprintf(tbuf
, sizeof(tbuf
), "%c(%x)[%d:%s]",
366 state
, this_cpu
, current
->pid
, current
->comm
);
368 tlen
= snprintf(tbuf
, sizeof(tbuf
), "%c%x)", state
, this_cpu
);
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);
376 while (log_first_seq
< log_next_seq
) {
379 if (log_next_idx
> log_first_idx
)
380 free
= max(log_buf_len
- log_next_idx
, log_first_idx
);
382 free
= log_first_idx
- log_next_idx
;
384 if (free
> size
+ sizeof(struct log
))
387 /* drop old messages until we have enough contiuous space */
388 log_first_idx
= log_next(log_first_idx
);
392 if (log_next_idx
+ size
+ sizeof(struct log
) >= log_buf_len
) {
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.
398 memset(log_buf
+ log_next_idx
, 0, sizeof(struct log
));
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
);
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;
415 msg
->ts_nsec
= ts_nsec
;
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
;
422 log_next_idx
+= msg
->len
;
426 #ifdef CONFIG_SECURITY_DMESG_RESTRICT
427 int dmesg_restrict
= 1;
432 static int syslog_action_restricted(int type
)
437 * Unless restricted, we allow "read all" and "get buffer size"
440 return type
!= SYSLOG_ACTION_READ_ALL
&&
441 type
!= SYSLOG_ACTION_SIZE_BUFFER
;
444 static int check_syslog_permissions(int type
, bool from_file
)
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.
450 if (from_file
&& type
!= SYSLOG_ACTION_OPEN
)
453 if (syslog_action_restricted(type
)) {
454 if (capable(CAP_SYSLOG
))
457 * For historical reasons, accept CAP_SYS_ADMIN too, with
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 "
464 current
->comm
, task_pid_nr(current
));
469 return security_syslog(type
);
473 /* /dev/kmsg - userspace message inject/listen interface */
474 struct devkmsg_user
{
482 static ssize_t
devkmsg_writev(struct kiocb
*iocb
, const struct iovec
*iv
,
483 unsigned long count
, loff_t pos
)
487 int level
= default_message_loglevel
;
488 int facility
= 1; /* LOG_USER */
489 size_t len
= iov_length(iv
, count
);
492 if (len
> LOG_LINE_MAX
)
494 buf
= kmalloc(len
+1, GFP_KERNEL
);
499 for (i
= 0; i
< count
; i
++) {
500 if (copy_from_user(line
, iv
[i
].iov_base
, iv
[i
].iov_len
)) {
504 line
+= iv
[i
].iov_len
;
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.
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.
517 if (line
[0] == '<') {
520 i
= simple_strtoul(line
+1, &endp
, 10);
521 if (endp
&& endp
[0] == '>') {
532 printk_emit(facility
, level
, NULL
, 0, "%s", line
);
538 static ssize_t
devkmsg_read(struct file
*file
, char __user
*buf
,
539 size_t count
, loff_t
*ppos
)
541 struct devkmsg_user
*user
= file
->private_data
;
552 ret
= mutex_lock_interruptible(&user
->lock
);
555 raw_spin_lock_irq(&logbuf_lock
);
556 while (user
->seq
== log_next_seq
) {
557 if (file
->f_flags
& O_NONBLOCK
) {
559 raw_spin_unlock_irq(&logbuf_lock
);
563 raw_spin_unlock_irq(&logbuf_lock
);
564 ret
= wait_event_interruptible(log_wait
,
565 user
->seq
!= log_next_seq
);
568 raw_spin_lock_irq(&logbuf_lock
);
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
;
576 raw_spin_unlock_irq(&logbuf_lock
);
580 msg
= log_from_idx(user
->idx
);
581 ts_usec
= msg
->ts_nsec
;
582 do_div(ts_usec
, 1000);
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.
592 if (msg
->flags
& LOG_CONT
&& !(user
->prev
& LOG_CONT
))
594 else if ((msg
->flags
& LOG_CONT
) ||
595 ((user
->prev
& LOG_CONT
) && !(msg
->flags
& LOG_PREFIX
)))
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
;
603 /* escape non-printable characters */
604 for (i
= 0; i
< msg
->text_len
; i
++) {
605 unsigned char c
= log_text(msg
)[i
];
607 if (c
< ' ' || c
>= 127 || c
== '\\')
608 len
+= sprintf(user
->buf
+ len
, "\\x%02x", c
);
610 user
->buf
[len
++] = c
;
612 user
->buf
[len
++] = '\n';
617 for (i
= 0; i
< msg
->dict_len
; i
++) {
618 unsigned char c
= log_dict(msg
)[i
];
621 user
->buf
[len
++] = ' ';
626 user
->buf
[len
++] = '\n';
631 if (c
< ' ' || c
>= 127 || c
== '\\') {
632 len
+= sprintf(user
->buf
+ len
, "\\x%02x", c
);
636 user
->buf
[len
++] = c
;
638 user
->buf
[len
++] = '\n';
641 user
->idx
= log_next(user
->idx
);
643 raw_spin_unlock_irq(&logbuf_lock
);
650 if (copy_to_user(buf
, user
->buf
, len
)) {
656 mutex_unlock(&user
->lock
);
660 static loff_t
devkmsg_llseek(struct file
*file
, loff_t offset
, int whence
)
662 struct devkmsg_user
*user
= file
->private_data
;
670 raw_spin_lock_irq(&logbuf_lock
);
673 /* the first record */
674 user
->idx
= log_first_idx
;
675 user
->seq
= log_first_seq
;
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.
683 user
->idx
= clear_idx
;
684 user
->seq
= clear_seq
;
687 /* after the last record */
688 user
->idx
= log_next_idx
;
689 user
->seq
= log_next_seq
;
694 raw_spin_unlock_irq(&logbuf_lock
);
698 static unsigned int devkmsg_poll(struct file
*file
, poll_table
*wait
)
700 struct devkmsg_user
*user
= file
->private_data
;
704 return POLLERR
|POLLNVAL
;
706 poll_wait(file
, &log_wait
, wait
);
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
;
714 ret
= POLLIN
|POLLRDNORM
;
716 raw_spin_unlock_irq(&logbuf_lock
);
721 static int devkmsg_open(struct inode
*inode
, struct file
*file
)
723 struct devkmsg_user
*user
;
726 /* write-only does not need any file context */
727 if ((file
->f_flags
& O_ACCMODE
) == O_WRONLY
)
730 err
= check_syslog_permissions(SYSLOG_ACTION_READ_ALL
,
735 user
= kmalloc(sizeof(struct devkmsg_user
), GFP_KERNEL
);
739 mutex_init(&user
->lock
);
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
);
746 file
->private_data
= user
;
750 static int devkmsg_release(struct inode
*inode
, struct file
*file
)
752 struct devkmsg_user
*user
= file
->private_data
;
757 mutex_destroy(&user
->lock
);
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
,
773 * This appends the listed symbols to /proc/vmcoreinfo
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.
780 void log_buf_kexec_setup(void)
782 VMCOREINFO_SYMBOL(log_buf
);
783 VMCOREINFO_SYMBOL(log_buf_len
);
784 VMCOREINFO_SYMBOL(log_first_idx
);
785 VMCOREINFO_SYMBOL(log_next_idx
);
787 * Export struct log size and field offsets. User space tools can
788 * parse it and detect any changes to structure down the line.
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
);
798 /* requested log_buf_len from kernel cmdline */
799 static unsigned long __initdata new_log_buf_len
;
801 /* save requested log_buf_len since it's too early to process it */
802 static int __init
log_buf_len_setup(char *str
)
804 unsigned size
= memparse(str
, &str
);
807 size
= roundup_pow_of_two(size
);
808 if (size
> log_buf_len
)
809 new_log_buf_len
= size
;
813 early_param("log_buf_len", log_buf_len_setup
);
815 void __init
setup_log_buf(int early
)
821 if (!new_log_buf_len
)
827 mem
= memblock_alloc(new_log_buf_len
, PAGE_SIZE
);
830 new_log_buf
= __va(mem
);
832 new_log_buf
= alloc_bootmem_nopanic(new_log_buf_len
);
835 if (unlikely(!new_log_buf
)) {
836 pr_err("log_buf_len: %ld bytes not available\n",
841 raw_spin_lock_irqsave(&logbuf_lock
, flags
);
842 log_buf_len
= new_log_buf_len
;
843 log_buf
= new_log_buf
;
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
);
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
);
854 static bool __read_mostly ignore_loglevel
;
856 static int __init
ignore_loglevel_setup(char *str
)
859 printk(KERN_INFO
"debug: ignoring loglevel setting.\n");
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.");
869 #ifdef CONFIG_BOOT_PRINTK_DELAY
871 static int boot_delay
; /* msecs delay after each printk during bootup */
872 static unsigned long long loops_per_msec
; /* based on boot_delay */
874 static int __init
boot_delay_setup(char *str
)
878 lpj
= preset_lpj
? preset_lpj
: 1000000; /* some guess */
879 loops_per_msec
= (unsigned long long)lpj
/ 1000 * HZ
;
881 get_option(&str
, &boot_delay
);
882 if (boot_delay
> 10 * 1000)
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
);
890 __setup("boot_delay=", boot_delay_setup
);
892 static void boot_delay_msec(int level
)
894 unsigned long long k
;
895 unsigned long timeout
;
897 if ((boot_delay
== 0 || system_state
!= SYSTEM_BOOTING
)
898 || (level
>= console_loglevel
&& !ignore_loglevel
)) {
902 k
= (unsigned long long)loops_per_msec
* boot_delay
;
904 timeout
= jiffies
+ msecs_to_jiffies(boot_delay
);
909 * use (volatile) jiffies to prevent
910 * compiler reduction; loop termination via jiffies
911 * is secondary and may or may not happen.
913 if (time_after(jiffies
, timeout
))
915 touch_nmi_watchdog();
919 static inline void boot_delay_msec(int level
)
924 #if defined(CONFIG_PRINTK_TIME)
925 static bool printk_time
= 1;
927 static bool printk_time
;
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
);
932 static size_t print_time(u64 ts
, char *buf
)
934 unsigned long rem_nsec
;
939 rem_nsec
= do_div(ts
, 1000000000);
942 return snprintf(NULL
, 0, "[%5lu.000000]", (unsigned long)ts
);
944 return sprintf(buf
, "[%5lu.%06lu]",
945 (unsigned long)ts
, rem_nsec
/ 1000);
948 static size_t print_prefix(const struct log
*msg
, bool syslog
, char *buf
)
951 unsigned int prefix
= (msg
->facility
<< 3) | msg
->level
;
955 len
+= sprintf(buf
, "<%u>", prefix
);
960 else if (prefix
> 99)
967 len
+= print_time(msg
->ts_nsec
, buf
? buf
+ len
: NULL
);
971 static size_t msg_print_text(const struct log
*msg
, enum log_flags prev
,
972 bool syslog
, char *buf
, size_t size
)
974 const char *text
= log_text(msg
);
975 size_t text_size
= msg
->text_len
;
980 if ((prev
& LOG_CONT
) && !(msg
->flags
& LOG_PREFIX
))
983 if (msg
->flags
& LOG_CONT
) {
984 if ((prev
& LOG_CONT
) && !(prev
& LOG_NEWLINE
))
987 if (!(msg
->flags
& LOG_NEWLINE
))
992 const char *next
= memchr(text
, '\n', text_size
);
996 text_len
= next
- text
;
998 text_size
-= next
- text
;
1000 text_len
= text_size
;
1004 if (print_prefix(msg
, syslog
, NULL
) +
1005 text_len
+ 1 >= size
- len
)
1009 len
+= print_prefix(msg
, syslog
, buf
+ len
);
1010 memcpy(buf
+ len
, text
, text_len
);
1012 if (next
|| newline
)
1015 /* SYSLOG_ACTION_* buffer size only calculation */
1017 len
+= print_prefix(msg
, syslog
, NULL
);
1019 if (next
|| newline
)
1030 static int syslog_print(char __user
*buf
, int size
)
1036 text
= kmalloc(LOG_LINE_MAX
+ PREFIX_MAX
, GFP_KERNEL
);
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
;
1052 if (syslog_seq
== log_next_seq
) {
1053 raw_spin_unlock_irq(&logbuf_lock
);
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
);
1065 syslog_prev
= msg
->flags
;
1066 n
-= syslog_partial
;
1069 /* partial read(), remember position */
1071 syslog_partial
+= n
;
1074 raw_spin_unlock_irq(&logbuf_lock
);
1079 if (copy_to_user(buf
, text
+ skip
, n
)) {
1094 static int syslog_print_all(char __user
*buf
, int size
, bool clear
)
1099 text
= kmalloc(LOG_LINE_MAX
+ PREFIX_MAX
, GFP_KERNEL
);
1103 raw_spin_lock_irq(&logbuf_lock
);
1108 enum log_flags prev
;
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
;
1117 * Find first record that fits, including all following records,
1118 * into the user-provided buffer for this dump.
1123 while (seq
< log_next_seq
) {
1124 struct log
*msg
= log_from_idx(idx
);
1126 len
+= msg_print_text(msg
, prev
, true, NULL
, 0);
1128 idx
= log_next(idx
);
1132 /* move first record forward until length fits into the buffer */
1136 while (len
> size
&& seq
< log_next_seq
) {
1137 struct log
*msg
= log_from_idx(idx
);
1139 len
-= msg_print_text(msg
, prev
, true, NULL
, 0);
1141 idx
= log_next(idx
);
1145 /* last message fitting into this dump */
1146 next_seq
= log_next_seq
;
1150 while (len
>= 0 && seq
< next_seq
) {
1151 struct log
*msg
= log_from_idx(idx
);
1154 textlen
= msg_print_text(msg
, prev
, true, text
,
1155 LOG_LINE_MAX
+ PREFIX_MAX
);
1160 idx
= log_next(idx
);
1164 raw_spin_unlock_irq(&logbuf_lock
);
1165 if (copy_to_user(buf
+ len
, text
, textlen
))
1169 raw_spin_lock_irq(&logbuf_lock
);
1171 if (seq
< log_first_seq
) {
1172 /* messages are gone, move to next one */
1173 seq
= log_first_seq
;
1174 idx
= log_first_idx
;
1181 clear_seq
= log_next_seq
;
1182 clear_idx
= log_next_idx
;
1184 raw_spin_unlock_irq(&logbuf_lock
);
1190 int do_syslog(int type
, char __user
*buf
, int len
, bool from_file
)
1193 static int saved_console_loglevel
= -1;
1196 error
= check_syslog_permissions(type
, from_file
);
1200 error
= security_syslog(type
);
1205 case SYSLOG_ACTION_CLOSE
: /* Close log */
1207 case SYSLOG_ACTION_OPEN
: /* Open log */
1209 case SYSLOG_ACTION_READ
: /* Read from log */
1211 if (!buf
|| len
< 0)
1216 if (!access_ok(VERIFY_WRITE
, buf
, len
)) {
1220 error
= wait_event_interruptible(log_wait
,
1221 syslog_seq
!= log_next_seq
);
1224 error
= syslog_print(buf
, len
);
1226 /* Read/clear last kernel messages */
1227 case SYSLOG_ACTION_READ_CLEAR
:
1230 /* Read last kernel messages */
1231 case SYSLOG_ACTION_READ_ALL
:
1233 if (!buf
|| len
< 0)
1238 if (!access_ok(VERIFY_WRITE
, buf
, len
)) {
1242 error
= syslog_print_all(buf
, len
, clear
);
1244 /* Clear ring buffer */
1245 case SYSLOG_ACTION_CLEAR
:
1246 syslog_print_all(NULL
, 0, true);
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
;
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;
1261 /* Set level of messages printed to console */
1262 case SYSLOG_ACTION_CONSOLE_LEVEL
:
1264 if (len
< 1 || len
> 8)
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;
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
;
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.
1289 error
= log_next_idx
- syslog_idx
;
1291 u64 seq
= syslog_seq
;
1292 u32 idx
= syslog_idx
;
1293 enum log_flags prev
= syslog_prev
;
1296 while (seq
< log_next_seq
) {
1297 struct log
*msg
= log_from_idx(idx
);
1299 error
+= msg_print_text(msg
, prev
, true, NULL
, 0);
1300 idx
= log_next(idx
);
1304 error
-= syslog_partial
;
1306 raw_spin_unlock_irq(&logbuf_lock
);
1308 /* Size of the log buffer */
1309 case SYSLOG_ACTION_SIZE_BUFFER
:
1310 error
= log_buf_len
;
1320 SYSCALL_DEFINE3(syslog
, int, type
, char __user
*, buf
, int, len
)
1322 return do_syslog(type
, buf
, len
, SYSLOG_FROM_READER
);
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.
1330 static void call_console_drivers(int level
, const char *text
, size_t len
)
1332 struct console
*con
;
1334 trace_console(text
, len
);
1336 if (level
>= console_loglevel
&& !ignore_loglevel
)
1338 if (!console_drivers
)
1341 for_each_console(con
) {
1342 if (printk_disable_uart
&& (con
->flags
& CON_CONSDEV
))
1344 if (exclusive_console
&& con
!= exclusive_console
)
1346 if (!(con
->flags
& CON_ENABLED
))
1350 if (!cpu_online(smp_processor_id()) &&
1351 !(con
->flags
& CON_ANYTIME
))
1353 con
->write(con
, text
, len
);
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
1362 static void zap_locks(void)
1364 static unsigned long oops_timestamp
;
1366 if (time_after_eq(jiffies
, oops_timestamp
) &&
1367 !time_after(jiffies
, oops_timestamp
+ 30 * HZ
))
1370 oops_timestamp
= jiffies
;
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);
1379 /* Check if we have any console registered that can be called early in boot. */
1380 static int have_callable_console(void)
1382 struct console
*con
;
1384 for_each_console(con
)
1385 if (con
->flags
& CON_ANYTIME
)
1392 * Can we actually use the console at this time on this cpu?
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.
1399 static inline int can_use_console(unsigned int cpu
)
1401 return cpu_online(cpu
) || have_callable_console();
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.
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.
1414 static int console_trylock_for_printk(unsigned int cpu
)
1415 __releases(&logbuf_lock
)
1417 int retval
= 0, wake
= 0;
1419 if (console_trylock()) {
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.
1428 if (!can_use_console(cpu
)) {
1434 logbuf_cpu
= UINT_MAX
;
1435 raw_spin_unlock(&logbuf_lock
);
1441 int printk_delay_msec __read_mostly
;
1443 static inline void printk_delay(void)
1445 if (unlikely(printk_delay_msec
)) {
1446 int m
= printk_delay_msec
;
1450 touch_nmi_watchdog();
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.
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 */
1473 static void cont_flush(enum log_flags flags
)
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.
1486 log_store(cont
.facility
, cont
.level
, flags
| LOG_NOCONS
,
1487 cont
.ts_nsec
, NULL
, 0, cont
.buf
, cont
.len
);
1489 cont
.flushed
= true;
1492 * If no fragment of this line ever reached the console,
1493 * just submit it to the store and free the buffer.
1495 log_store(cont
.facility
, cont
.level
, flags
, 0,
1496 NULL
, 0, cont
.buf
, cont
.len
);
1501 static bool cont_add(int facility
, int level
, const char *text
, size_t len
)
1503 if (cont
.len
&& cont
.flushed
)
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
);
1513 cont
.facility
= facility
;
1515 cont
.owner
= current
;
1516 cont
.ts_nsec
= local_clock();
1519 cont
.flushed
= false;
1522 memcpy(cont
.buf
+ cont
.len
, text
, len
);
1525 if (cont
.len
> (sizeof(cont
.buf
) * 80) / 100)
1526 cont_flush(LOG_CONT
);
1531 static size_t cont_print_text(char *text
, size_t size
)
1536 if (cont
.cons
== 0 && (console_prev
& LOG_NEWLINE
)) {
1537 textlen
+= print_time(cont
.ts_nsec
, text
);
1541 len
= cont
.len
- cont
.cons
;
1545 memcpy(text
+ textlen
, cont
.buf
+ cont
.cons
, len
);
1547 cont
.cons
= cont
.len
;
1551 if (cont
.flags
& LOG_NEWLINE
)
1552 text
[textlen
++] = '\n';
1553 /* got everything, release buffer */
1559 asmlinkage
int vprintk_emit(int facility
, int level
,
1560 const char *dict
, size_t dictlen
,
1561 const char *fmt
, va_list args
)
1563 static int recursion_bug
;
1564 static char textbuf
[LOG_LINE_MAX
];
1565 char *text
= textbuf
;
1567 enum log_flags lflags
= 0;
1568 unsigned long flags
;
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
);
1579 /* This stops the holder of console_sem just where we want him */
1580 local_irq_save(flags
);
1581 this_cpu
= smp_processor_id();
1584 * Ouch, printk recursed into itself!
1586 if (unlikely(logbuf_cpu
== this_cpu
)) {
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:
1594 if (!oops_in_progress
&& !lockdep_recursing(current
)) {
1596 goto out_restore_irqs
;
1602 raw_spin_lock(&logbuf_lock
);
1603 logbuf_cpu
= this_cpu
;
1605 if (recursion_bug
) {
1606 static const char recursion_msg
[] =
1607 "BUG: recent printk recursion!";
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
);
1617 * The printf needs to come first; we need the syslog
1618 * prefix which might be passed-in as a parameter.
1620 text_len
= vscnprintf(text
, sizeof(textbuf
), fmt
, args
);
1622 /* mark and strip a trailing newline */
1623 if (text_len
&& text
[text_len
-1] == '\n') {
1625 lflags
|= LOG_NEWLINE
;
1628 /* strip kernel syslog prefix and extract log level or control flags */
1629 if (facility
== 0) {
1630 int kern_level
= printk_get_level(text
);
1633 const char *end_of_header
= printk_skip_level(text
);
1634 switch (kern_level
) {
1637 level
= kern_level
- '0';
1638 case 'd': /* KERN_DEFAULT */
1639 lflags
|= LOG_PREFIX
;
1640 case 'c': /* KERN_CONT */
1643 text_len
-= end_of_header
- text
;
1644 text
= (char *)end_of_header
;
1648 #ifdef CONFIG_EARLY_PRINTK_DIRECT
1653 level
= default_message_loglevel
;
1656 lflags
|= LOG_PREFIX
|LOG_NEWLINE
;
1658 #ifdef CONFIG_PRINTK_PROCESS_INFO
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
) = '.';
1666 __raw_get_cpu_var(printk_state
) = ' ';
1669 if (!(lflags
& LOG_NEWLINE
)) {
1671 * Flush the conflicting buffer. An earlier newline was missing,
1672 * or another task also prints continuation lines.
1674 if (cont
.len
&& (lflags
& LOG_PREFIX
|| cont
.owner
!= current
))
1675 cont_flush(LOG_NEWLINE
);
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
);
1682 bool stored
= false;
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.
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
);
1697 log_store(facility
, level
, lflags
, 0,
1698 dict
, dictlen
, text
, text_len
);
1700 printed_len
+= text_len
;
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()
1707 * The console_trylock_for_printk() function will release 'logbuf_lock'
1708 * regardless of whether it actually gets the console semaphore or not.
1710 if (console_trylock_for_printk(this_cpu
))
1715 local_irq_restore(flags
);
1719 EXPORT_SYMBOL(vprintk_emit
);
1721 asmlinkage
int vprintk(const char *fmt
, va_list args
)
1723 return vprintk_emit(0, -1, NULL
, 0, fmt
, args
);
1725 EXPORT_SYMBOL(vprintk
);
1727 asmlinkage
int printk_emit(int facility
, int level
,
1728 const char *dict
, size_t dictlen
,
1729 const char *fmt
, ...)
1734 va_start(args
, fmt
);
1735 r
= vprintk_emit(facility
, level
, dict
, dictlen
, fmt
, args
);
1740 EXPORT_SYMBOL(printk_emit
);
1743 * printk - print a kernel message
1744 * @fmt: format string
1746 * This is printk(). It can be called from any context. We want it to work.
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.
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.
1761 * See the vsnprintf() documentation for format string extensions over C99.
1763 asmlinkage
int printk(const char *fmt
, ...)
1768 #ifdef CONFIG_KGDB_KDB
1769 if (unlikely(kdb_trap_printk
)) {
1770 va_start(args
, fmt
);
1771 r
= vkdb_printf(fmt
, args
);
1776 va_start(args
, fmt
);
1777 r
= vprintk_emit(0, -1, NULL
, 0, fmt
, args
);
1782 EXPORT_SYMBOL(printk
);
1784 #else /* CONFIG_PRINTK */
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
{
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; }
1811 #endif /* CONFIG_PRINTK */
1813 #ifdef CONFIG_EARLY_PRINTK
1814 struct console
*early_console
;
1816 void early_vprintk(const char *fmt
, va_list ap
)
1818 if (early_console
) {
1820 int n
= vscnprintf(buf
, sizeof(buf
), fmt
, ap
);
1822 early_console
->write(early_console
, buf
, n
);
1826 asmlinkage
void early_printk(const char *fmt
, ...)
1831 early_vprintk(fmt
, ap
);
1836 static int __add_preferred_console(char *name
, int idx
, char *options
,
1839 struct console_cmdline
*c
;
1843 * See if this tty is not yet registered, and
1844 * if we have a slot free.
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
) {
1850 selected_console
= i
;
1853 if (i
== MAX_CMDLINECONSOLES
)
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
;
1867 * Set up a list of consoles. Called from init/main.c
1869 static int __init
console_setup(char *str
)
1871 char buf
[sizeof(console_cmdline
[0].name
) + 4]; /* 4 for index */
1872 char *s
, *options
, *brl_options
= NULL
;
1875 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1876 if (!memcmp(str
, "brl,", 4)) {
1879 } else if (!memcmp(str
, "brl=", 4)) {
1880 brl_options
= str
+ 4;
1881 str
= strchr(brl_options
, ',');
1883 printk(KERN_ERR
"need port name after brl=\n");
1891 * Decode str into name, index, options.
1893 if (str
[0] >= '0' && str
[0] <= '9') {
1894 strcpy(buf
, "ttyS");
1895 strncpy(buf
+ 4, str
, sizeof(buf
) - 5);
1897 strncpy(buf
, str
, sizeof(buf
) - 1);
1899 buf
[sizeof(buf
) - 1] = 0;
1900 if ((options
= strchr(str
, ',')) != NULL
)
1903 if (!strcmp(str
, "ttya"))
1904 strcpy(buf
, "ttyS0");
1905 if (!strcmp(str
, "ttyb"))
1906 strcpy(buf
, "ttyS1");
1908 for (s
= buf
; *s
; s
++)
1909 if ((*s
>= '0' && *s
<= '9') || *s
== ',')
1911 idx
= simple_strtoul(s
, NULL
, 10);
1914 __add_preferred_console(buf
, idx
, options
, brl_options
);
1915 console_set_on_cmdline
= 1;
1918 __setup("console=", console_setup
);
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
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.
1933 int add_preferred_console(char *name
, int idx
, char *options
)
1935 return __add_preferred_console(name
, idx
, options
, NULL
);
1938 int update_console_cmdline(char *name
, int idx
, char *name_new
, int idx_new
, char *options
)
1940 struct console_cmdline
*c
;
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
;
1957 bool console_suspend_enabled
= 1;
1958 EXPORT_SYMBOL(console_suspend_enabled
);
1960 static int __init
console_suspend_disable(char *str
)
1962 console_suspend_enabled
= 0;
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");
1972 * suspend_console - suspend the console subsystem
1974 * This disables printk() while we go into suspend states
1976 void suspend_console(void)
1978 if (!console_suspend_enabled
)
1980 printk("Suspending console(s) (use no_console_suspend to debug)\n");
1982 console_suspended
= 1;
1984 mutex_release(&console_lock_dep_map
, 1, _RET_IP_
);
1986 EXPORT_SYMBOL_GPL(suspend_console
);
1988 void resume_console(void)
1990 if (!console_suspend_enabled
)
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;
1999 // log_in_resume = 0;
2000 // __raw_get_cpu_var(MT_trace_in_resume_console) = 0;
2005 EXPORT_SYMBOL_GPL(resume_console
);
2008 * console_cpu_notify - print deferred console messages after CPU hotplug
2009 * @self: notifier struct
2010 * @action: CPU hotplug event
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.
2018 static int __cpuinit
console_cpu_notify(struct notifier_block
*self
,
2019 unsigned long action
, void *hcpu
)
2024 case CPU_DOWN_FAILED
:
2025 case CPU_UP_CANCELED
:
2033 * console_lock - lock the console system for exclusive use.
2035 * Acquires a lock which guarantees that the caller has
2036 * exclusive access to the console system and the console_drivers list.
2038 * Can sleep, returns nothing.
2040 void console_lock(void)
2045 if (console_suspended
)
2048 console_may_schedule
= 1;
2049 mutex_acquire(&console_lock_dep_map
, 0, 0, _RET_IP_
);
2051 EXPORT_SYMBOL(console_lock
);
2054 * console_trylock - try to lock the console system for exclusive use.
2056 * Tried to acquire a lock which guarantees that the caller has
2057 * exclusive access to the console system and the console_drivers list.
2059 * returns 1 on success, and 0 on failure to acquire the lock.
2061 int console_trylock(void)
2063 if (down_trylock(&console_sem
))
2065 if (console_suspended
) {
2070 console_may_schedule
= 0;
2071 mutex_acquire(&console_lock_dep_map
, 0, 1, _RET_IP_
);
2074 EXPORT_SYMBOL(console_trylock
);
2076 int is_console_locked(void)
2078 return console_locked
;
2081 static void console_cont_flush(char *text
, size_t size
)
2083 unsigned long flags
;
2086 raw_spin_lock_irqsave(&logbuf_lock
, flags
);
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.
2096 if (console_seq
< log_next_seq
&& !cont
.cons
)
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
);
2107 raw_spin_unlock_irqrestore(&logbuf_lock
, flags
);
2111 * console_unlock - unlock the console system
2113 * Releases the console_lock which the caller holds on the console system
2114 * and the console driver list.
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.
2120 * If there is output waiting, we wake /dev/kmsg and syslog() users.
2122 * console_unlock(); may be called from any context.
2124 #ifdef LOG_TOO_MUCH_WARNING
2125 static int console_log_max
= 400000;
2126 static int already_skip_log
;
2128 void console_unlock(void)
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;
2139 int org_loglevel
= console_loglevel
;
2143 if (console_suspended
) {
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.
2158 do_cond_resched
= console_may_schedule
;
2159 console_may_schedule
= 0;
2161 /* flush buffered message fragment immediately to console */
2162 console_cont_flush(text
, sizeof(text
));
2169 raw_spin_lock_irqsave(&logbuf_lock
, flags
);
2170 #ifdef LOG_TOO_MUCH_WARNING /*For Resume log too much*/
2171 if (log_in_resume
) {
2176 if (seen_seq
!= log_next_seq
) {
2178 seen_seq
= log_next_seq
;
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
;
2188 if (console_seq
== log_next_seq
)
2191 msg
= log_from_idx(console_idx
);
2192 if (msg
->flags
& LOG_NOCONS
) {
2194 * Skip record we have buffered and already printed
2195 * directly to the console when we received it.
2197 console_idx
= log_next(console_idx
);
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.
2204 msg
->flags
&= ~LOG_NOCONS
;
2205 console_prev
= msg
->flags
;
2210 len
= msg_print_text(msg
, console_prev
, false,
2211 text
, sizeof(text
));
2212 console_idx
= log_next(console_idx
);
2214 console_prev
= msg
->flags
;
2215 raw_spin_unlock(&logbuf_lock
);
2217 stop_critical_timings(); /* don't trace print latency */
2218 #ifdef LOG_TOO_MUCH_WARNING
2220 For uart console, 10us/per chars
2221 400,000 chars = need to wait 4.0 sec
2224 if (log_in_resume
) {
2225 org_loglevel
= console_loglevel
;
2226 console_loglevel
= 4;
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;
2237 start_critical_timings();
2238 /* For Resume log too much*/
2239 if (log_in_resume
) {
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");
2250 start_critical_timings();
2251 call_console_drivers(level
, text
, len
);
2253 local_irq_restore(flags
);
2255 if (do_cond_resched
)
2259 mutex_release(&console_lock_dep_map
, 1, _RET_IP_
);
2261 /* Release the exclusive_console once it is used */
2262 if (unlikely(exclusive_console
))
2263 exclusive_console
= NULL
;
2265 raw_spin_unlock(&logbuf_lock
);
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.
2275 raw_spin_lock(&logbuf_lock
);
2276 retry
= console_seq
!= log_next_seq
;
2277 raw_spin_unlock_irqrestore(&logbuf_lock
, flags
);
2279 if (retry
&& console_trylock())
2285 EXPORT_SYMBOL(console_unlock
);
2288 * console_conditional_schedule - yield the CPU if required
2290 * If the console code is currently allowed to sleep, and
2291 * if this CPU should yield the CPU to another task, do
2294 * Must be called within console_lock();.
2296 void __sched
console_conditional_schedule(void)
2298 if (console_may_schedule
)
2301 EXPORT_SYMBOL(console_conditional_schedule
);
2303 void console_unblank(void)
2308 * console_unblank can no longer be called in interrupt context unless
2309 * oops_in_progress is set to 1..
2311 if (oops_in_progress
) {
2312 if (down_trylock(&console_sem
) != 0)
2318 console_may_schedule
= 0;
2320 if ((c
->flags
& CON_ENABLED
) && c
->unblank
)
2326 * console_flush_on_panic - flush console content on panic
2328 * Immediately output all pending messages no matter what.
2330 void console_flush_on_panic(void)
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.
2340 console_may_schedule
= 0;
2345 * Return the console tty driver structure and its associated index
2347 struct tty_driver
*console_device(int *index
)
2350 struct tty_driver
*driver
= NULL
;
2353 for_each_console(c
) {
2356 driver
= c
->device(c
, index
);
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.
2369 void console_stop(struct console
*console
)
2372 console
->flags
&= ~CON_ENABLED
;
2375 EXPORT_SYMBOL(console_stop
);
2377 void console_start(struct console
*console
)
2380 console
->flags
|= CON_ENABLED
;
2383 EXPORT_SYMBOL(console_start
);
2385 static int __read_mostly keep_bootcon
;
2387 static int __init
keep_bootcon_setup(char *str
)
2390 printk(KERN_INFO
"debug: skip boot console de-registration.\n");
2395 early_param("keep_bootcon", keep_bootcon_setup
);
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.
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.
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
2416 void register_console(struct console
*newcon
)
2419 unsigned long flags
;
2420 struct console
*bcon
= NULL
;
2423 * before we register a new CON_BOOT console, make sure we don't
2424 * already have a valid console
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
);
2437 if (console_drivers
&& console_drivers
->flags
& CON_BOOT
)
2438 bcon
= console_drivers
;
2440 if (preferred_console
< 0 || bcon
|| !console_drivers
)
2441 preferred_console
= selected_console
;
2443 if (newcon
->early_setup
)
2444 newcon
->early_setup();
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.
2451 if (preferred_console
< 0) {
2452 if (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;
2465 * See if this console matches one we selected on
2468 for (i
= 0; i
< MAX_CMDLINECONSOLES
&& console_cmdline
[i
].name
[0];
2470 BUILD_BUG_ON(sizeof(console_cmdline
[i
].name
) !=
2471 sizeof(newcon
->name
));
2472 if (strcmp(console_cmdline
[i
].name
, newcon
->name
) != 0)
2474 if (newcon
->index
>= 0 &&
2475 newcon
->index
!= console_cmdline
[i
].index
)
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
);
2489 if (newcon
->setup
&&
2490 newcon
->setup(newcon
, console_cmdline
[i
].options
) != 0)
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
;
2501 if (!(newcon
->flags
& CON_ENABLED
))
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
2510 if (bcon
&& ((newcon
->flags
& (CON_CONSDEV
| CON_BOOT
)) == CON_CONSDEV
))
2511 newcon
->flags
&= ~CON_PRINTBUFFER
;
2514 * Put this console in the list - keep the
2515 * preferred driver at the head of the list.
2518 if ((newcon
->flags
& CON_CONSDEV
) || console_drivers
== NULL
) {
2519 newcon
->next
= console_drivers
;
2520 console_drivers
= newcon
;
2522 newcon
->next
->flags
&= ~CON_CONSDEV
;
2524 newcon
->next
= console_drivers
->next
;
2525 console_drivers
->next
= newcon
;
2527 if (newcon
->flags
& CON_PRINTBUFFER
) {
2529 * console_unlock(); will print out the buffered messages
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
);
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.
2542 exclusive_console
= newcon
;
2545 console_sysfs_notify();
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)
2555 ((newcon
->flags
& (CON_CONSDEV
| CON_BOOT
)) == CON_CONSDEV
) &&
2557 /* we need to iterate through twice, to make sure we print
2558 * everything out, before we unregister the console(s)
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
);
2566 printk(KERN_INFO
"%sconsole [%s%d] enabled\n",
2567 (newcon
->flags
& CON_BOOT
) ? "boot" : "" ,
2568 newcon
->name
, newcon
->index
);
2571 EXPORT_SYMBOL(register_console
);
2573 int unregister_console(struct console
*console
)
2575 struct console
*a
, *b
;
2578 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
2579 if (console
->flags
& CON_BRL
)
2580 return braille_unregister_console(console
);
2584 if (console_drivers
== console
) {
2585 console_drivers
=console
->next
;
2587 } else if (console_drivers
) {
2588 for (a
=console_drivers
->next
, b
=console_drivers
;
2589 a
; b
=a
, a
=b
->next
) {
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.
2602 if (console_drivers
!= NULL
&& console
->flags
& CON_CONSDEV
)
2603 console_drivers
->flags
|= CON_CONSDEV
;
2606 console_sysfs_notify();
2609 EXPORT_SYMBOL(unregister_console
);
2611 static int __init
printk_late_init(void)
2613 struct console
*con
;
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
);
2622 hotcpu_notifier(console_cpu_notify
, 0);
2625 late_initcall(printk_late_init
);
2627 #if defined CONFIG_PRINTK
2629 * Delayed printk version, for scheduler-internal messages:
2631 #define PRINTK_BUF_SIZE 512
2633 #define PRINTK_PENDING_WAKEUP 0x01
2634 #define PRINTK_PENDING_SCHED 0x02
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
);
2640 static void wake_up_klogd_work_func(struct irq_work
*irq_work
)
2642 int pending
= __this_cpu_xchg(printk_pending
, 0);
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;
2652 if (pending
& PRINTK_PENDING_WAKEUP
)
2653 wake_up_interruptible(&log_wait
);
2656 static DEFINE_PER_CPU(struct irq_work
, wake_up_klogd_work
) = {
2657 .func
= wake_up_klogd_work_func
,
2658 .flags
= IRQ_WORK_LAZY
,
2661 void wake_up_klogd(void)
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
));
2671 int printk_deferred(const char *fmt
, ...)
2673 unsigned long flags
;
2678 local_irq_save(flags
);
2679 buf
= __get_cpu_var(printk_sched_buf
);
2680 buf_length
= __get_cpu_var(printk_sched_length
);
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
;
2687 printk("delayed log buf overflow, size:%d\n", buf_length
);
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
);
2700 * printk rate limiting, lifted from the networking subsystem.
2702 * This enforces a rate limit: not more than 10 kernel messages
2703 * every 5s to make a denial-of-service attack impossible.
2705 DEFINE_RATELIMIT_STATE(printk_ratelimit_state
, 5 * HZ
, 10);
2707 int __printk_ratelimit(const char *func
)
2709 return ___ratelimit(&printk_ratelimit_state
, func
);
2711 EXPORT_SYMBOL(__printk_ratelimit
);
2714 * printk_timed_ratelimit - caller-controlled printk ratelimiting
2715 * @caller_jiffies: pointer to caller's state
2716 * @interval_msecs: minimum interval between prints
2718 * printk_timed_ratelimit() returns true if more than @interval_msecs
2719 * milliseconds have elapsed since the last time printk_timed_ratelimit()
2722 bool printk_timed_ratelimit(unsigned long *caller_jiffies
,
2723 unsigned int interval_msecs
)
2725 if (*caller_jiffies
== 0
2726 || !time_in_range(jiffies
, *caller_jiffies
,
2728 + msecs_to_jiffies(interval_msecs
))) {
2729 *caller_jiffies
= jiffies
;
2734 EXPORT_SYMBOL(printk_timed_ratelimit
);
2736 static DEFINE_SPINLOCK(dump_list_lock
);
2737 static LIST_HEAD(dump_list
);
2740 * kmsg_dump_register - register a kernel log dumper.
2741 * @dumper: pointer to the kmsg_dumper structure
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.
2747 int kmsg_dump_register(struct kmsg_dumper
*dumper
)
2749 unsigned long flags
;
2752 /* The dump callback needs to be set */
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
);
2763 spin_unlock_irqrestore(&dump_list_lock
, flags
);
2767 EXPORT_SYMBOL_GPL(kmsg_dump_register
);
2770 * kmsg_dump_unregister - unregister a kmsg dumper.
2771 * @dumper: pointer to the kmsg_dumper structure
2773 * Removes a dump device from the system. Returns zero on success and
2774 * %-EINVAL otherwise.
2776 int kmsg_dump_unregister(struct kmsg_dumper
*dumper
)
2778 unsigned long flags
;
2781 spin_lock_irqsave(&dump_list_lock
, flags
);
2782 if (dumper
->registered
) {
2783 dumper
->registered
= 0;
2784 list_del_rcu(&dumper
->list
);
2787 spin_unlock_irqrestore(&dump_list_lock
, flags
);
2792 EXPORT_SYMBOL_GPL(kmsg_dump_unregister
);
2794 static bool always_kmsg_dump
;
2795 module_param_named(always_kmsg_dump
, always_kmsg_dump
, bool, S_IRUGO
| S_IWUSR
);
2798 * kmsg_dump - dump kernel log to kernel message dumpers.
2799 * @reason: the reason (oops, panic etc) for dumping
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().
2805 void kmsg_dump(enum kmsg_dump_reason reason
)
2807 struct kmsg_dumper
*dumper
;
2808 unsigned long flags
;
2810 if ((reason
> KMSG_DUMP_OOPS
) && !always_kmsg_dump
)
2814 list_for_each_entry_rcu(dumper
, &dump_list
, list
) {
2815 if (dumper
->max_reason
&& reason
> dumper
->max_reason
)
2818 /* initialize iterator with data about the stored records */
2819 dumper
->active
= true;
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
);
2828 /* invoke dumper which will iterate over records */
2829 dumper
->dump(dumper
, reason
);
2831 /* reset iterator */
2832 dumper
->active
= false;
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
2845 * Start at the beginning of the kmsg buffer, with the oldest kmsg
2846 * record, and copy one record into the provided buffer.
2848 * Consecutive calls will return the next available record moving
2849 * towards the end of the buffer with the youngest messages.
2851 * A return value of FALSE indicates that there are no more records to
2854 * The function is similar to kmsg_dump_get_line(), but grabs no locks.
2856 bool kmsg_dump_get_line_nolock(struct kmsg_dumper
*dumper
, bool syslog
,
2857 char *line
, size_t size
, size_t *len
)
2863 if (!dumper
->active
)
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
;
2873 if (dumper
->cur_seq
>= log_next_seq
)
2876 msg
= log_from_idx(dumper
->cur_idx
);
2877 l
= msg_print_text(msg
, 0, syslog
, line
, size
);
2879 dumper
->cur_idx
= log_next(dumper
->cur_idx
);
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
2896 * Start at the beginning of the kmsg buffer, with the oldest kmsg
2897 * record, and copy one record into the provided buffer.
2899 * Consecutive calls will return the next available record moving
2900 * towards the end of the buffer with the youngest messages.
2902 * A return value of FALSE indicates that there are no more records to
2905 bool kmsg_dump_get_line(struct kmsg_dumper
*dumper
, bool syslog
,
2906 char *line
, size_t size
, size_t *len
)
2908 unsigned long flags
;
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
);
2917 EXPORT_SYMBOL_GPL(kmsg_dump_get_line
);
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
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.
2932 * Consecutive calls will fill the buffer with the next block of
2933 * available older records, not including the earlier retrieved ones.
2935 * A return value of FALSE indicates that there are no more records to
2938 bool kmsg_dump_get_buffer(struct kmsg_dumper
*dumper
, bool syslog
,
2939 char *buf
, size_t size
, size_t *len
)
2941 unsigned long flags
;
2946 enum log_flags prev
;
2950 if (!dumper
->active
)
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
;
2961 if (dumper
->cur_seq
>= dumper
->next_seq
) {
2962 raw_spin_unlock_irqrestore(&logbuf_lock
, flags
);
2966 /* calculate length of entire buffer */
2967 seq
= dumper
->cur_seq
;
2968 idx
= dumper
->cur_idx
;
2970 while (seq
< dumper
->next_seq
) {
2971 struct log
*msg
= log_from_idx(idx
);
2973 l
+= msg_print_text(msg
, prev
, true, NULL
, 0);
2974 idx
= log_next(idx
);
2979 /* move first record forward until length fits into the buffer */
2980 seq
= dumper
->cur_seq
;
2981 idx
= dumper
->cur_idx
;
2983 while (l
> size
&& seq
< dumper
->next_seq
) {
2984 struct log
*msg
= log_from_idx(idx
);
2986 l
-= msg_print_text(msg
, prev
, true, NULL
, 0);
2987 idx
= log_next(idx
);
2992 /* last message in next interation */
2998 while (seq
< dumper
->next_seq
) {
2999 struct log
*msg
= log_from_idx(idx
);
3001 l
+= msg_print_text(msg
, prev
, syslog
, buf
+ l
, size
- l
);
3002 idx
= log_next(idx
);
3007 dumper
->next_seq
= next_seq
;
3008 dumper
->next_idx
= next_idx
;
3010 raw_spin_unlock_irqrestore(&logbuf_lock
, flags
);
3016 EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer
);
3019 * kmsg_dump_rewind_nolock - reset the interator (unlocked version)
3020 * @dumper: registered kmsg dumper
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.
3026 * The function is similar to kmsg_dump_rewind(), but grabs no locks.
3028 void kmsg_dump_rewind_nolock(struct kmsg_dumper
*dumper
)
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
;
3037 * kmsg_dump_rewind - reset the interator
3038 * @dumper: registered kmsg dumper
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.
3044 void kmsg_dump_rewind(struct kmsg_dumper
*dumper
)
3046 unsigned long flags
;
3048 raw_spin_lock_irqsave(&logbuf_lock
, flags
);
3049 kmsg_dump_rewind_nolock(dumper
);
3050 raw_spin_unlock_irqrestore(&logbuf_lock
, flags
);
3052 EXPORT_SYMBOL_GPL(kmsg_dump_rewind
);
3054 static char dump_stack_arch_desc_str
[128];
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
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.
3066 void __init
dump_stack_set_arch_desc(const char *fmt
, ...)
3070 va_start(args
, fmt
);
3071 vsnprintf(dump_stack_arch_desc_str
, sizeof(dump_stack_arch_desc_str
),
3077 * dump_stack_print_info - print generic debug info for dump_stack()
3078 * @log_lvl: log level
3080 * Arch-specific dump_stack() implementations can use this function to
3081 * print out the same debug information as the generic dump_stack().
3083 void dump_stack_print_info(const char *log_lvl
)
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
);
3091 if (dump_stack_arch_desc_str
[0] != '\0')
3092 printk("%sHardware name: %s\n",
3093 log_lvl
, dump_stack_arch_desc_str
);
3095 print_worker_info(log_lvl
, current
);
3099 * show_regs_print_info - print generic debug info for show_regs()
3100 * @log_lvl: log level
3102 * show_regs() implementations can use this function to print out generic
3103 * debug information.
3105 void show_regs_print_info(const char *log_lvl
)
3107 dump_stack_print_info(log_lvl
);
3109 printk("%stask: %p ti: %p task.ti: %p\n",
3110 log_lvl
, current
, current_thread_info(),
3111 task_thread_info(current
));
3114 void get_kernel_log_buffer(unsigned long *addr
, unsigned long *size
, unsigned long *start
)
3116 *addr
= (unsigned long)log_buf
;
3117 *size
= log_buf_len
;
3118 *start
= (unsigned long)&log_first_idx
;