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
19 #include <linux/kernel.h>
21 #include <linux/tty.h>
22 #include <linux/tty_driver.h>
23 #include <linux/console.h>
24 #include <linux/init.h>
25 #include <linux/jiffies.h>
26 #include <linux/nmi.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/interrupt.h> /* For in_interrupt() */
30 #include <linux/delay.h>
31 #include <linux/smp.h>
32 #include <linux/security.h>
33 #include <linux/bootmem.h>
34 #include <linux/memblock.h>
35 #include <linux/aio.h>
36 #include <linux/syscalls.h>
37 #include <linux/kexec.h>
38 #include <linux/kdb.h>
39 #include <linux/ratelimit.h>
40 #include <linux/kmsg_dump.h>
41 #include <linux/syslog.h>
42 #include <linux/cpu.h>
43 #include <linux/notifier.h>
44 #include <linux/rculist.h>
45 #include <linux/poll.h>
46 #include <linux/irq_work.h>
47 #include <linux/utsname.h>
48 #include <linux/ctype.h>
50 #include <asm/uaccess.h>
52 #define CREATE_TRACE_POINTS
53 #include <trace/events/printk.h>
55 #include "console_cmdline.h"
58 int console_printk
[4] = {
59 CONSOLE_LOGLEVEL_DEFAULT
, /* console_loglevel */
60 MESSAGE_LOGLEVEL_DEFAULT
, /* default_message_loglevel */
61 CONSOLE_LOGLEVEL_MIN
, /* minimum_console_loglevel */
62 CONSOLE_LOGLEVEL_DEFAULT
, /* default_console_loglevel */
65 /* Deferred messaged from sched code are marked by this special level */
66 #define SCHED_MESSAGE_LOGLEVEL -2
69 * Low level drivers may need that to know if they can schedule in
70 * their unblank() callback or not. So let's export it.
73 EXPORT_SYMBOL(oops_in_progress
);
76 * console_sem protects the console_drivers list, and also
77 * provides serialisation for access to the entire console
80 static DEFINE_SEMAPHORE(console_sem
);
81 struct console
*console_drivers
;
82 EXPORT_SYMBOL_GPL(console_drivers
);
85 static struct lockdep_map console_lock_dep_map
= {
86 .name
= "console_lock"
91 * Helper macros to handle lockdep when locking/unlocking console_sem. We use
92 * macros instead of functions so that _RET_IP_ contains useful information.
94 #define down_console_sem() do { \
96 mutex_acquire(&console_lock_dep_map, 0, 0, _RET_IP_);\
99 static int __down_trylock_console_sem(unsigned long ip
)
101 if (down_trylock(&console_sem
))
103 mutex_acquire(&console_lock_dep_map
, 0, 1, ip
);
106 #define down_trylock_console_sem() __down_trylock_console_sem(_RET_IP_)
108 #define up_console_sem() do { \
109 mutex_release(&console_lock_dep_map, 1, _RET_IP_);\
114 * This is used for debugging the mess that is the VT code by
115 * keeping track if we have the console semaphore held. It's
116 * definitely not the perfect debug tool (we don't know if _WE_
117 * hold it and are racing, but it helps tracking those weird code
118 * paths in the console code where we end up in places I want
119 * locked without the console sempahore held).
121 static int console_locked
, console_suspended
;
124 * If exclusive_console is non-NULL then only this console is to be printed to.
126 static struct console
*exclusive_console
;
129 * Array of consoles built from command line options (console=)
132 #define MAX_CMDLINECONSOLES 8
134 static struct console_cmdline console_cmdline
[MAX_CMDLINECONSOLES
];
136 static int selected_console
= -1;
137 static int preferred_console
= -1;
138 int console_set_on_cmdline
;
139 EXPORT_SYMBOL(console_set_on_cmdline
);
141 /* Flag: console code may call schedule() */
142 static int console_may_schedule
;
145 * The printk log buffer consists of a chain of concatenated variable
146 * length records. Every record starts with a record header, containing
147 * the overall length of the record.
149 * The heads to the first and last entry in the buffer, as well as the
150 * sequence numbers of these entries are maintained when messages are
153 * If the heads indicate available messages, the length in the header
154 * tells the start next message. A length == 0 for the next message
155 * indicates a wrap-around to the beginning of the buffer.
157 * Every record carries the monotonic timestamp in microseconds, as well as
158 * the standard userspace syslog level and syslog facility. The usual
159 * kernel messages use LOG_KERN; userspace-injected messages always carry
160 * a matching syslog facility, by default LOG_USER. The origin of every
161 * message can be reliably determined that way.
163 * The human readable log message directly follows the message header. The
164 * length of the message text is stored in the header, the stored message
167 * Optionally, a message can carry a dictionary of properties (key/value pairs),
168 * to provide userspace with a machine-readable message context.
170 * Examples for well-defined, commonly used property names are:
171 * DEVICE=b12:8 device identifier
175 * +sound:card0 subsystem:devname
176 * SUBSYSTEM=pci driver-core subsystem name
178 * Valid characters in property names are [a-zA-Z0-9.-_]. The plain text value
179 * follows directly after a '=' character. Every property is terminated by
180 * a '\0' character. The last property is not terminated.
182 * Example of a message structure:
183 * 0000 ff 8f 00 00 00 00 00 00 monotonic time in nsec
184 * 0008 34 00 record is 52 bytes long
185 * 000a 0b 00 text is 11 bytes long
186 * 000c 1f 00 dictionary is 23 bytes long
187 * 000e 03 00 LOG_KERN (facility) LOG_ERR (level)
188 * 0010 69 74 27 73 20 61 20 6c "it's a l"
190 * 001b 44 45 56 49 43 "DEVIC"
191 * 45 3d 62 38 3a 32 00 44 "E=b8:2\0D"
192 * 52 49 56 45 52 3d 62 75 "RIVER=bu"
194 * 0032 00 00 00 padding to next message header
196 * The 'struct printk_log' buffer header must never be directly exported to
197 * userspace, it is a kernel-private implementation detail that might
198 * need to be changed in the future, when the requirements change.
200 * /dev/kmsg exports the structured data in the following line format:
201 * "level,sequnum,timestamp;<message text>\n"
203 * The optional key/value pairs are attached as continuation lines starting
204 * with a space character and terminated by a newline. All possible
205 * non-prinatable characters are escaped in the "\xff" notation.
207 * Users of the export format should ignore possible additional values
208 * separated by ',', and find the message after the ';' character.
212 LOG_NOCONS
= 1, /* already flushed, do not print to console */
213 LOG_NEWLINE
= 2, /* text ended with a newline */
214 LOG_PREFIX
= 4, /* text started with a prefix */
215 LOG_CONT
= 8, /* text is a fragment of a continuation line */
219 u64 ts_nsec
; /* timestamp in nanoseconds */
220 u16 len
; /* length of entire record */
221 u16 text_len
; /* length of text buffer */
222 u16 dict_len
; /* length of dictionary buffer */
223 u8 facility
; /* syslog facility */
224 u8 flags
:5; /* internal record flags */
225 u8 level
:3; /* syslog level */
229 * The logbuf_lock protects kmsg buffer, indices, counters. This can be taken
230 * within the scheduler's rq lock. It must be released before calling
231 * console_unlock() or anything else that might wake up a process.
233 static DEFINE_RAW_SPINLOCK(logbuf_lock
);
236 DECLARE_WAIT_QUEUE_HEAD(log_wait
);
237 /* the next printk record to read by syslog(READ) or /proc/kmsg */
238 static u64 syslog_seq
;
239 static u32 syslog_idx
;
240 static enum log_flags syslog_prev
;
241 static size_t syslog_partial
;
243 /* index and sequence number of the first record stored in the buffer */
244 static u64 log_first_seq
;
245 static u32 log_first_idx
;
247 /* index and sequence number of the next record to store in the buffer */
248 static u64 log_next_seq
;
249 static u32 log_next_idx
;
251 /* the next printk record to write to the console */
252 static u64 console_seq
;
253 static u32 console_idx
;
254 static enum log_flags console_prev
;
256 /* the next printk record to read after the last 'clear' command */
257 static u64 clear_seq
;
258 static u32 clear_idx
;
260 #define PREFIX_MAX 32
261 #define LOG_LINE_MAX (1024 - PREFIX_MAX)
264 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
267 #define LOG_ALIGN __alignof__(struct printk_log)
269 #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
270 #define __LOG_CPU_MAX_BUF_LEN (1 << CONFIG_LOG_CPU_MAX_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 /* Return log buffer address */
276 char *log_buf_addr_get(void)
281 /* Return log buffer size */
282 u32
log_buf_len_get(void)
287 /* human readable text of the record */
288 static char *log_text(const struct printk_log
*msg
)
290 return (char *)msg
+ sizeof(struct printk_log
);
293 /* optional key/value pair dictionary attached to the record */
294 static char *log_dict(const struct printk_log
*msg
)
296 return (char *)msg
+ sizeof(struct printk_log
) + msg
->text_len
;
299 /* get record by index; idx must point to valid msg */
300 static struct printk_log
*log_from_idx(u32 idx
)
302 struct printk_log
*msg
= (struct printk_log
*)(log_buf
+ idx
);
305 * A length == 0 record is the end of buffer marker. Wrap around and
306 * read the message at the start of the buffer.
309 return (struct printk_log
*)log_buf
;
313 /* get next record; idx must point to valid msg */
314 static u32
log_next(u32 idx
)
316 struct printk_log
*msg
= (struct printk_log
*)(log_buf
+ idx
);
318 /* length == 0 indicates the end of the buffer; wrap */
320 * A length == 0 record is the end of buffer marker. Wrap around and
321 * read the message at the start of the buffer as *this* one, and
322 * return the one after that.
325 msg
= (struct printk_log
*)log_buf
;
328 return idx
+ msg
->len
;
332 * Check whether there is enough free space for the given message.
334 * The same values of first_idx and next_idx mean that the buffer
335 * is either empty or full.
337 * If the buffer is empty, we must respect the position of the indexes.
338 * They cannot be reset to the beginning of the buffer.
340 static int logbuf_has_space(u32 msg_size
, bool empty
)
344 if (log_next_idx
> log_first_idx
|| empty
)
345 free
= max(log_buf_len
- log_next_idx
, log_first_idx
);
347 free
= log_first_idx
- log_next_idx
;
350 * We need space also for an empty header that signalizes wrapping
353 return free
>= msg_size
+ sizeof(struct printk_log
);
356 static int log_make_free_space(u32 msg_size
)
358 while (log_first_seq
< log_next_seq
) {
359 if (logbuf_has_space(msg_size
, false))
361 /* drop old messages until we have enough contiguous space */
362 log_first_idx
= log_next(log_first_idx
);
366 /* sequence numbers are equal, so the log buffer is empty */
367 if (logbuf_has_space(msg_size
, true))
373 /* compute the message size including the padding bytes */
374 static u32
msg_used_size(u16 text_len
, u16 dict_len
, u32
*pad_len
)
378 size
= sizeof(struct printk_log
) + text_len
+ dict_len
;
379 *pad_len
= (-size
) & (LOG_ALIGN
- 1);
386 * Define how much of the log buffer we could take at maximum. The value
387 * must be greater than two. Note that only half of the buffer is available
388 * when the index points to the middle.
390 #define MAX_LOG_TAKE_PART 4
391 static const char trunc_msg
[] = "<truncated>";
393 static u32
truncate_msg(u16
*text_len
, u16
*trunc_msg_len
,
394 u16
*dict_len
, u32
*pad_len
)
397 * The message should not take the whole buffer. Otherwise, it might
398 * get removed too soon.
400 u32 max_text_len
= log_buf_len
/ MAX_LOG_TAKE_PART
;
401 if (*text_len
> max_text_len
)
402 *text_len
= max_text_len
;
403 /* enable the warning message */
404 *trunc_msg_len
= strlen(trunc_msg
);
405 /* disable the "dict" completely */
407 /* compute the size again, count also the warning message */
408 return msg_used_size(*text_len
+ *trunc_msg_len
, 0, pad_len
);
411 /* insert record into the buffer, discard old ones, update heads */
412 static int log_store(int facility
, int level
,
413 enum log_flags flags
, u64 ts_nsec
,
414 const char *dict
, u16 dict_len
,
415 const char *text
, u16 text_len
)
417 struct printk_log
*msg
;
419 u16 trunc_msg_len
= 0;
421 /* number of '\0' padding bytes to next message */
422 size
= msg_used_size(text_len
, dict_len
, &pad_len
);
424 if (log_make_free_space(size
)) {
425 /* truncate the message if it is too long for empty buffer */
426 size
= truncate_msg(&text_len
, &trunc_msg_len
,
427 &dict_len
, &pad_len
);
428 /* survive when the log buffer is too small for trunc_msg */
429 if (log_make_free_space(size
))
433 if (log_next_idx
+ size
+ sizeof(struct printk_log
) > log_buf_len
) {
435 * This message + an additional empty header does not fit
436 * at the end of the buffer. Add an empty header with len == 0
437 * to signify a wrap around.
439 memset(log_buf
+ log_next_idx
, 0, sizeof(struct printk_log
));
444 msg
= (struct printk_log
*)(log_buf
+ log_next_idx
);
445 memcpy(log_text(msg
), text
, text_len
);
446 msg
->text_len
= text_len
;
448 memcpy(log_text(msg
) + text_len
, trunc_msg
, trunc_msg_len
);
449 msg
->text_len
+= trunc_msg_len
;
451 memcpy(log_dict(msg
), dict
, dict_len
);
452 msg
->dict_len
= dict_len
;
453 msg
->facility
= facility
;
454 msg
->level
= level
& 7;
455 msg
->flags
= flags
& 0x1f;
457 msg
->ts_nsec
= ts_nsec
;
459 msg
->ts_nsec
= local_clock();
460 memset(log_dict(msg
) + dict_len
, 0, pad_len
);
464 log_next_idx
+= msg
->len
;
467 return msg
->text_len
;
470 int dmesg_restrict
= IS_ENABLED(CONFIG_SECURITY_DMESG_RESTRICT
);
472 static int syslog_action_restricted(int type
)
477 * Unless restricted, we allow "read all" and "get buffer size"
480 return type
!= SYSLOG_ACTION_READ_ALL
&&
481 type
!= SYSLOG_ACTION_SIZE_BUFFER
;
484 static int check_syslog_permissions(int type
, bool from_file
)
487 * If this is from /proc/kmsg and we've already opened it, then we've
488 * already done the capabilities checks at open time.
490 if (from_file
&& type
!= SYSLOG_ACTION_OPEN
)
493 if (syslog_action_restricted(type
)) {
494 if (capable(CAP_SYSLOG
))
497 * For historical reasons, accept CAP_SYS_ADMIN too, with
500 if (capable(CAP_SYS_ADMIN
)) {
501 pr_warn_once("%s (%d): Attempt to access syslog with "
502 "CAP_SYS_ADMIN but no CAP_SYSLOG "
504 current
->comm
, task_pid_nr(current
));
509 return security_syslog(type
);
513 /* /dev/kmsg - userspace message inject/listen interface */
514 struct devkmsg_user
{
522 static ssize_t
devkmsg_write(struct kiocb
*iocb
, struct iov_iter
*from
)
526 int level
= default_message_loglevel
;
527 int facility
= 1; /* LOG_USER */
528 size_t len
= iocb
->ki_nbytes
;
531 if (len
> LOG_LINE_MAX
)
533 buf
= kmalloc(len
+1, GFP_KERNEL
);
538 if (copy_from_iter(buf
, len
, from
) != len
) {
544 * Extract and skip the syslog prefix <[0-9]*>. Coming from userspace
545 * the decimal value represents 32bit, the lower 3 bit are the log
546 * level, the rest are the log facility.
548 * If no prefix or no userspace facility is specified, we
549 * enforce LOG_USER, to be able to reliably distinguish
550 * kernel-generated messages from userspace-injected ones.
553 if (line
[0] == '<') {
556 i
= simple_strtoul(line
+1, &endp
, 10);
557 if (endp
&& endp
[0] == '>') {
567 printk_emit(facility
, level
, NULL
, 0, "%s", line
);
572 static ssize_t
devkmsg_read(struct file
*file
, char __user
*buf
,
573 size_t count
, loff_t
*ppos
)
575 struct devkmsg_user
*user
= file
->private_data
;
576 struct printk_log
*msg
;
586 ret
= mutex_lock_interruptible(&user
->lock
);
589 raw_spin_lock_irq(&logbuf_lock
);
590 while (user
->seq
== log_next_seq
) {
591 if (file
->f_flags
& O_NONBLOCK
) {
593 raw_spin_unlock_irq(&logbuf_lock
);
597 raw_spin_unlock_irq(&logbuf_lock
);
598 ret
= wait_event_interruptible(log_wait
,
599 user
->seq
!= log_next_seq
);
602 raw_spin_lock_irq(&logbuf_lock
);
605 if (user
->seq
< log_first_seq
) {
606 /* our last seen message is gone, return error and reset */
607 user
->idx
= log_first_idx
;
608 user
->seq
= log_first_seq
;
610 raw_spin_unlock_irq(&logbuf_lock
);
614 msg
= log_from_idx(user
->idx
);
615 ts_usec
= msg
->ts_nsec
;
616 do_div(ts_usec
, 1000);
619 * If we couldn't merge continuation line fragments during the print,
620 * export the stored flags to allow an optional external merge of the
621 * records. Merging the records isn't always neccessarily correct, like
622 * when we hit a race during printing. In most cases though, it produces
623 * better readable output. 'c' in the record flags mark the first
624 * fragment of a line, '+' the following.
626 if (msg
->flags
& LOG_CONT
&& !(user
->prev
& LOG_CONT
))
628 else if ((msg
->flags
& LOG_CONT
) ||
629 ((user
->prev
& LOG_CONT
) && !(msg
->flags
& LOG_PREFIX
)))
632 len
= sprintf(user
->buf
, "%u,%llu,%llu,%c;",
633 (msg
->facility
<< 3) | msg
->level
,
634 user
->seq
, ts_usec
, cont
);
635 user
->prev
= msg
->flags
;
637 /* escape non-printable characters */
638 for (i
= 0; i
< msg
->text_len
; i
++) {
639 unsigned char c
= log_text(msg
)[i
];
641 if (c
< ' ' || c
>= 127 || c
== '\\')
642 len
+= sprintf(user
->buf
+ len
, "\\x%02x", c
);
644 user
->buf
[len
++] = c
;
646 user
->buf
[len
++] = '\n';
651 for (i
= 0; i
< msg
->dict_len
; i
++) {
652 unsigned char c
= log_dict(msg
)[i
];
655 user
->buf
[len
++] = ' ';
660 user
->buf
[len
++] = '\n';
665 if (c
< ' ' || c
>= 127 || c
== '\\') {
666 len
+= sprintf(user
->buf
+ len
, "\\x%02x", c
);
670 user
->buf
[len
++] = c
;
672 user
->buf
[len
++] = '\n';
675 user
->idx
= log_next(user
->idx
);
677 raw_spin_unlock_irq(&logbuf_lock
);
684 if (copy_to_user(buf
, user
->buf
, len
)) {
690 mutex_unlock(&user
->lock
);
694 static loff_t
devkmsg_llseek(struct file
*file
, loff_t offset
, int whence
)
696 struct devkmsg_user
*user
= file
->private_data
;
704 raw_spin_lock_irq(&logbuf_lock
);
707 /* the first record */
708 user
->idx
= log_first_idx
;
709 user
->seq
= log_first_seq
;
713 * The first record after the last SYSLOG_ACTION_CLEAR,
714 * like issued by 'dmesg -c'. Reading /dev/kmsg itself
715 * changes no global state, and does not clear anything.
717 user
->idx
= clear_idx
;
718 user
->seq
= clear_seq
;
721 /* after the last record */
722 user
->idx
= log_next_idx
;
723 user
->seq
= log_next_seq
;
728 raw_spin_unlock_irq(&logbuf_lock
);
732 static unsigned int devkmsg_poll(struct file
*file
, poll_table
*wait
)
734 struct devkmsg_user
*user
= file
->private_data
;
738 return POLLERR
|POLLNVAL
;
740 poll_wait(file
, &log_wait
, wait
);
742 raw_spin_lock_irq(&logbuf_lock
);
743 if (user
->seq
< log_next_seq
) {
744 /* return error when data has vanished underneath us */
745 if (user
->seq
< log_first_seq
)
746 ret
= POLLIN
|POLLRDNORM
|POLLERR
|POLLPRI
;
748 ret
= POLLIN
|POLLRDNORM
;
750 raw_spin_unlock_irq(&logbuf_lock
);
755 static int devkmsg_open(struct inode
*inode
, struct file
*file
)
757 struct devkmsg_user
*user
;
760 /* write-only does not need any file context */
761 if ((file
->f_flags
& O_ACCMODE
) == O_WRONLY
)
764 err
= check_syslog_permissions(SYSLOG_ACTION_READ_ALL
,
769 user
= kmalloc(sizeof(struct devkmsg_user
), GFP_KERNEL
);
773 mutex_init(&user
->lock
);
775 raw_spin_lock_irq(&logbuf_lock
);
776 user
->idx
= log_first_idx
;
777 user
->seq
= log_first_seq
;
778 raw_spin_unlock_irq(&logbuf_lock
);
780 file
->private_data
= user
;
784 static int devkmsg_release(struct inode
*inode
, struct file
*file
)
786 struct devkmsg_user
*user
= file
->private_data
;
791 mutex_destroy(&user
->lock
);
796 const struct file_operations kmsg_fops
= {
797 .open
= devkmsg_open
,
798 .read
= devkmsg_read
,
799 .write_iter
= devkmsg_write
,
800 .llseek
= devkmsg_llseek
,
801 .poll
= devkmsg_poll
,
802 .release
= devkmsg_release
,
807 * This appends the listed symbols to /proc/vmcore
809 * /proc/vmcore is used by various utilities, like crash and makedumpfile to
810 * obtain access to symbols that are otherwise very difficult to locate. These
811 * symbols are specifically used so that utilities can access and extract the
812 * dmesg log from a vmcore file after a crash.
814 void log_buf_kexec_setup(void)
816 VMCOREINFO_SYMBOL(log_buf
);
817 VMCOREINFO_SYMBOL(log_buf_len
);
818 VMCOREINFO_SYMBOL(log_first_idx
);
819 VMCOREINFO_SYMBOL(log_next_idx
);
821 * Export struct printk_log size and field offsets. User space tools can
822 * parse it and detect any changes to structure down the line.
824 VMCOREINFO_STRUCT_SIZE(printk_log
);
825 VMCOREINFO_OFFSET(printk_log
, ts_nsec
);
826 VMCOREINFO_OFFSET(printk_log
, len
);
827 VMCOREINFO_OFFSET(printk_log
, text_len
);
828 VMCOREINFO_OFFSET(printk_log
, dict_len
);
832 /* requested log_buf_len from kernel cmdline */
833 static unsigned long __initdata new_log_buf_len
;
835 /* we practice scaling the ring buffer by powers of 2 */
836 static void __init
log_buf_len_update(unsigned size
)
839 size
= roundup_pow_of_two(size
);
840 if (size
> log_buf_len
)
841 new_log_buf_len
= size
;
844 /* save requested log_buf_len since it's too early to process it */
845 static int __init
log_buf_len_setup(char *str
)
847 unsigned size
= memparse(str
, &str
);
849 log_buf_len_update(size
);
853 early_param("log_buf_len", log_buf_len_setup
);
855 static void __init
log_buf_add_cpu(void)
857 unsigned int cpu_extra
;
860 * archs should set up cpu_possible_bits properly with
861 * set_cpu_possible() after setup_arch() but just in
862 * case lets ensure this is valid.
864 if (num_possible_cpus() == 1)
867 cpu_extra
= (num_possible_cpus() - 1) * __LOG_CPU_MAX_BUF_LEN
;
869 /* by default this will only continue through for large > 64 CPUs */
870 if (cpu_extra
<= __LOG_BUF_LEN
/ 2)
873 pr_info("log_buf_len individual max cpu contribution: %d bytes\n",
874 __LOG_CPU_MAX_BUF_LEN
);
875 pr_info("log_buf_len total cpu_extra contributions: %d bytes\n",
877 pr_info("log_buf_len min size: %d bytes\n", __LOG_BUF_LEN
);
879 log_buf_len_update(cpu_extra
+ __LOG_BUF_LEN
);
882 void __init
setup_log_buf(int early
)
888 if (log_buf
!= __log_buf
)
891 if (!early
&& !new_log_buf_len
)
894 if (!new_log_buf_len
)
899 memblock_virt_alloc(new_log_buf_len
, LOG_ALIGN
);
901 new_log_buf
= memblock_virt_alloc_nopanic(new_log_buf_len
,
905 if (unlikely(!new_log_buf
)) {
906 pr_err("log_buf_len: %ld bytes not available\n",
911 raw_spin_lock_irqsave(&logbuf_lock
, flags
);
912 log_buf_len
= new_log_buf_len
;
913 log_buf
= new_log_buf
;
915 free
= __LOG_BUF_LEN
- log_next_idx
;
916 memcpy(log_buf
, __log_buf
, __LOG_BUF_LEN
);
917 raw_spin_unlock_irqrestore(&logbuf_lock
, flags
);
919 pr_info("log_buf_len: %d bytes\n", log_buf_len
);
920 pr_info("early log buf free: %d(%d%%)\n",
921 free
, (free
* 100) / __LOG_BUF_LEN
);
924 static bool __read_mostly ignore_loglevel
;
926 static int __init
ignore_loglevel_setup(char *str
)
928 ignore_loglevel
= true;
929 pr_info("debug: ignoring loglevel setting.\n");
934 early_param("ignore_loglevel", ignore_loglevel_setup
);
935 module_param(ignore_loglevel
, bool, S_IRUGO
| S_IWUSR
);
936 MODULE_PARM_DESC(ignore_loglevel
, "ignore loglevel setting, to"
937 "print all kernel messages to the console.");
939 #ifdef CONFIG_BOOT_PRINTK_DELAY
941 static int boot_delay
; /* msecs delay after each printk during bootup */
942 static unsigned long long loops_per_msec
; /* based on boot_delay */
944 static int __init
boot_delay_setup(char *str
)
948 lpj
= preset_lpj
? preset_lpj
: 1000000; /* some guess */
949 loops_per_msec
= (unsigned long long)lpj
/ 1000 * HZ
;
951 get_option(&str
, &boot_delay
);
952 if (boot_delay
> 10 * 1000)
955 pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
956 "HZ: %d, loops_per_msec: %llu\n",
957 boot_delay
, preset_lpj
, lpj
, HZ
, loops_per_msec
);
960 early_param("boot_delay", boot_delay_setup
);
962 static void boot_delay_msec(int level
)
964 unsigned long long k
;
965 unsigned long timeout
;
967 if ((boot_delay
== 0 || system_state
!= SYSTEM_BOOTING
)
968 || (level
>= console_loglevel
&& !ignore_loglevel
)) {
972 k
= (unsigned long long)loops_per_msec
* boot_delay
;
974 timeout
= jiffies
+ msecs_to_jiffies(boot_delay
);
979 * use (volatile) jiffies to prevent
980 * compiler reduction; loop termination via jiffies
981 * is secondary and may or may not happen.
983 if (time_after(jiffies
, timeout
))
985 touch_nmi_watchdog();
989 static inline void boot_delay_msec(int level
)
994 static bool printk_time
= IS_ENABLED(CONFIG_PRINTK_TIME
);
995 module_param_named(time
, printk_time
, bool, S_IRUGO
| S_IWUSR
);
997 static size_t print_time(u64 ts
, char *buf
)
999 unsigned long rem_nsec
;
1004 rem_nsec
= do_div(ts
, 1000000000);
1007 return snprintf(NULL
, 0, "[%5lu.000000] ", (unsigned long)ts
);
1009 return sprintf(buf
, "[%5lu.%06lu] ",
1010 (unsigned long)ts
, rem_nsec
/ 1000);
1013 static size_t print_prefix(const struct printk_log
*msg
, bool syslog
, char *buf
)
1016 unsigned int prefix
= (msg
->facility
<< 3) | msg
->level
;
1020 len
+= sprintf(buf
, "<%u>", prefix
);
1025 else if (prefix
> 99)
1027 else if (prefix
> 9)
1032 len
+= print_time(msg
->ts_nsec
, buf
? buf
+ len
: NULL
);
1036 static size_t msg_print_text(const struct printk_log
*msg
, enum log_flags prev
,
1037 bool syslog
, char *buf
, size_t size
)
1039 const char *text
= log_text(msg
);
1040 size_t text_size
= msg
->text_len
;
1042 bool newline
= true;
1045 if ((prev
& LOG_CONT
) && !(msg
->flags
& LOG_PREFIX
))
1048 if (msg
->flags
& LOG_CONT
) {
1049 if ((prev
& LOG_CONT
) && !(prev
& LOG_NEWLINE
))
1052 if (!(msg
->flags
& LOG_NEWLINE
))
1057 const char *next
= memchr(text
, '\n', text_size
);
1061 text_len
= next
- text
;
1063 text_size
-= next
- text
;
1065 text_len
= text_size
;
1069 if (print_prefix(msg
, syslog
, NULL
) +
1070 text_len
+ 1 >= size
- len
)
1074 len
+= print_prefix(msg
, syslog
, buf
+ len
);
1075 memcpy(buf
+ len
, text
, text_len
);
1077 if (next
|| newline
)
1080 /* SYSLOG_ACTION_* buffer size only calculation */
1082 len
+= print_prefix(msg
, syslog
, NULL
);
1084 if (next
|| newline
)
1095 static int syslog_print(char __user
*buf
, int size
)
1098 struct printk_log
*msg
;
1101 text
= kmalloc(LOG_LINE_MAX
+ PREFIX_MAX
, GFP_KERNEL
);
1109 raw_spin_lock_irq(&logbuf_lock
);
1110 if (syslog_seq
< log_first_seq
) {
1111 /* messages are gone, move to first one */
1112 syslog_seq
= log_first_seq
;
1113 syslog_idx
= log_first_idx
;
1117 if (syslog_seq
== log_next_seq
) {
1118 raw_spin_unlock_irq(&logbuf_lock
);
1122 skip
= syslog_partial
;
1123 msg
= log_from_idx(syslog_idx
);
1124 n
= msg_print_text(msg
, syslog_prev
, true, text
,
1125 LOG_LINE_MAX
+ PREFIX_MAX
);
1126 if (n
- syslog_partial
<= size
) {
1127 /* message fits into buffer, move forward */
1128 syslog_idx
= log_next(syslog_idx
);
1130 syslog_prev
= msg
->flags
;
1131 n
-= syslog_partial
;
1134 /* partial read(), remember position */
1136 syslog_partial
+= n
;
1139 raw_spin_unlock_irq(&logbuf_lock
);
1144 if (copy_to_user(buf
, text
+ skip
, n
)) {
1159 static int syslog_print_all(char __user
*buf
, int size
, bool clear
)
1164 text
= kmalloc(LOG_LINE_MAX
+ PREFIX_MAX
, GFP_KERNEL
);
1168 raw_spin_lock_irq(&logbuf_lock
);
1173 enum log_flags prev
;
1175 if (clear_seq
< log_first_seq
) {
1176 /* messages are gone, move to first available one */
1177 clear_seq
= log_first_seq
;
1178 clear_idx
= log_first_idx
;
1182 * Find first record that fits, including all following records,
1183 * into the user-provided buffer for this dump.
1188 while (seq
< log_next_seq
) {
1189 struct printk_log
*msg
= log_from_idx(idx
);
1191 len
+= msg_print_text(msg
, prev
, true, NULL
, 0);
1193 idx
= log_next(idx
);
1197 /* move first record forward until length fits into the buffer */
1201 while (len
> size
&& seq
< log_next_seq
) {
1202 struct printk_log
*msg
= log_from_idx(idx
);
1204 len
-= msg_print_text(msg
, prev
, true, NULL
, 0);
1206 idx
= log_next(idx
);
1210 /* last message fitting into this dump */
1211 next_seq
= log_next_seq
;
1214 while (len
>= 0 && seq
< next_seq
) {
1215 struct printk_log
*msg
= log_from_idx(idx
);
1218 textlen
= msg_print_text(msg
, prev
, true, text
,
1219 LOG_LINE_MAX
+ PREFIX_MAX
);
1224 idx
= log_next(idx
);
1228 raw_spin_unlock_irq(&logbuf_lock
);
1229 if (copy_to_user(buf
+ len
, text
, textlen
))
1233 raw_spin_lock_irq(&logbuf_lock
);
1235 if (seq
< log_first_seq
) {
1236 /* messages are gone, move to next one */
1237 seq
= log_first_seq
;
1238 idx
= log_first_idx
;
1245 clear_seq
= log_next_seq
;
1246 clear_idx
= log_next_idx
;
1248 raw_spin_unlock_irq(&logbuf_lock
);
1254 int do_syslog(int type
, char __user
*buf
, int len
, bool from_file
)
1257 static int saved_console_loglevel
= -1;
1260 error
= check_syslog_permissions(type
, from_file
);
1264 error
= security_syslog(type
);
1269 case SYSLOG_ACTION_CLOSE
: /* Close log */
1271 case SYSLOG_ACTION_OPEN
: /* Open log */
1273 case SYSLOG_ACTION_READ
: /* Read from log */
1275 if (!buf
|| len
< 0)
1280 if (!access_ok(VERIFY_WRITE
, buf
, len
)) {
1284 error
= wait_event_interruptible(log_wait
,
1285 syslog_seq
!= log_next_seq
);
1288 error
= syslog_print(buf
, len
);
1290 /* Read/clear last kernel messages */
1291 case SYSLOG_ACTION_READ_CLEAR
:
1294 /* Read last kernel messages */
1295 case SYSLOG_ACTION_READ_ALL
:
1297 if (!buf
|| len
< 0)
1302 if (!access_ok(VERIFY_WRITE
, buf
, len
)) {
1306 error
= syslog_print_all(buf
, len
, clear
);
1308 /* Clear ring buffer */
1309 case SYSLOG_ACTION_CLEAR
:
1310 syslog_print_all(NULL
, 0, true);
1312 /* Disable logging to console */
1313 case SYSLOG_ACTION_CONSOLE_OFF
:
1314 if (saved_console_loglevel
== -1)
1315 saved_console_loglevel
= console_loglevel
;
1316 console_loglevel
= minimum_console_loglevel
;
1318 /* Enable logging to console */
1319 case SYSLOG_ACTION_CONSOLE_ON
:
1320 if (saved_console_loglevel
!= -1) {
1321 console_loglevel
= saved_console_loglevel
;
1322 saved_console_loglevel
= -1;
1325 /* Set level of messages printed to console */
1326 case SYSLOG_ACTION_CONSOLE_LEVEL
:
1328 if (len
< 1 || len
> 8)
1330 if (len
< minimum_console_loglevel
)
1331 len
= minimum_console_loglevel
;
1332 console_loglevel
= len
;
1333 /* Implicitly re-enable logging to console */
1334 saved_console_loglevel
= -1;
1337 /* Number of chars in the log buffer */
1338 case SYSLOG_ACTION_SIZE_UNREAD
:
1339 raw_spin_lock_irq(&logbuf_lock
);
1340 if (syslog_seq
< log_first_seq
) {
1341 /* messages are gone, move to first one */
1342 syslog_seq
= log_first_seq
;
1343 syslog_idx
= log_first_idx
;
1349 * Short-cut for poll(/"proc/kmsg") which simply checks
1350 * for pending data, not the size; return the count of
1351 * records, not the length.
1353 error
= log_next_seq
- syslog_seq
;
1355 u64 seq
= syslog_seq
;
1356 u32 idx
= syslog_idx
;
1357 enum log_flags prev
= syslog_prev
;
1360 while (seq
< log_next_seq
) {
1361 struct printk_log
*msg
= log_from_idx(idx
);
1363 error
+= msg_print_text(msg
, prev
, true, NULL
, 0);
1364 idx
= log_next(idx
);
1368 error
-= syslog_partial
;
1370 raw_spin_unlock_irq(&logbuf_lock
);
1372 /* Size of the log buffer */
1373 case SYSLOG_ACTION_SIZE_BUFFER
:
1374 error
= log_buf_len
;
1384 SYSCALL_DEFINE3(syslog
, int, type
, char __user
*, buf
, int, len
)
1386 return do_syslog(type
, buf
, len
, SYSLOG_FROM_READER
);
1390 * Call the console drivers, asking them to write out
1391 * log_buf[start] to log_buf[end - 1].
1392 * The console_lock must be held.
1394 static void call_console_drivers(int level
, const char *text
, size_t len
)
1396 struct console
*con
;
1398 trace_console(text
, len
);
1400 if (level
>= console_loglevel
&& !ignore_loglevel
)
1402 if (!console_drivers
)
1405 for_each_console(con
) {
1406 if (exclusive_console
&& con
!= exclusive_console
)
1408 if (!(con
->flags
& CON_ENABLED
))
1412 if (!cpu_online(smp_processor_id()) &&
1413 !(con
->flags
& CON_ANYTIME
))
1415 con
->write(con
, text
, len
);
1420 * Zap console related locks when oopsing. Only zap at most once
1421 * every 10 seconds, to leave time for slow consoles to print a
1424 static void zap_locks(void)
1426 static unsigned long oops_timestamp
;
1428 if (time_after_eq(jiffies
, oops_timestamp
) &&
1429 !time_after(jiffies
, oops_timestamp
+ 30 * HZ
))
1432 oops_timestamp
= jiffies
;
1435 /* If a crash is occurring, make sure we can't deadlock */
1436 raw_spin_lock_init(&logbuf_lock
);
1437 /* And make sure that we print immediately */
1438 sema_init(&console_sem
, 1);
1442 * Check if we have any console that is capable of printing while cpu is
1443 * booting or shutting down. Requires console_sem.
1445 static int have_callable_console(void)
1447 struct console
*con
;
1449 for_each_console(con
)
1450 if (con
->flags
& CON_ANYTIME
)
1457 * Can we actually use the console at this time on this cpu?
1459 * Console drivers may assume that per-cpu resources have been allocated. So
1460 * unless they're explicitly marked as being able to cope (CON_ANYTIME) don't
1461 * call them until this CPU is officially up.
1463 static inline int can_use_console(unsigned int cpu
)
1465 return cpu_online(cpu
) || have_callable_console();
1469 * Try to get console ownership to actually show the kernel
1470 * messages from a 'printk'. Return true (and with the
1471 * console_lock held, and 'console_locked' set) if it
1472 * is successful, false otherwise.
1474 static int console_trylock_for_printk(void)
1476 unsigned int cpu
= smp_processor_id();
1478 if (!console_trylock())
1481 * If we can't use the console, we need to release the console
1482 * semaphore by hand to avoid flushing the buffer. We need to hold the
1483 * console semaphore in order to do this test safely.
1485 if (!can_use_console(cpu
)) {
1493 int printk_delay_msec __read_mostly
;
1495 static inline void printk_delay(void)
1497 if (unlikely(printk_delay_msec
)) {
1498 int m
= printk_delay_msec
;
1502 touch_nmi_watchdog();
1508 * Continuation lines are buffered, and not committed to the record buffer
1509 * until the line is complete, or a race forces it. The line fragments
1510 * though, are printed immediately to the consoles to ensure everything has
1511 * reached the console in case of a kernel crash.
1513 static struct cont
{
1514 char buf
[LOG_LINE_MAX
];
1515 size_t len
; /* length == 0 means unused buffer */
1516 size_t cons
; /* bytes written to console */
1517 struct task_struct
*owner
; /* task of first print*/
1518 u64 ts_nsec
; /* time of first print */
1519 u8 level
; /* log level of first message */
1520 u8 facility
; /* log facility of first message */
1521 enum log_flags flags
; /* prefix, newline flags */
1522 bool flushed
:1; /* buffer sealed and committed */
1525 static void cont_flush(enum log_flags flags
)
1534 * If a fragment of this line was directly flushed to the
1535 * console; wait for the console to pick up the rest of the
1536 * line. LOG_NOCONS suppresses a duplicated output.
1538 log_store(cont
.facility
, cont
.level
, flags
| LOG_NOCONS
,
1539 cont
.ts_nsec
, NULL
, 0, cont
.buf
, cont
.len
);
1541 cont
.flushed
= true;
1544 * If no fragment of this line ever reached the console,
1545 * just submit it to the store and free the buffer.
1547 log_store(cont
.facility
, cont
.level
, flags
, 0,
1548 NULL
, 0, cont
.buf
, cont
.len
);
1553 static bool cont_add(int facility
, int level
, const char *text
, size_t len
)
1555 if (cont
.len
&& cont
.flushed
)
1558 if (cont
.len
+ len
> sizeof(cont
.buf
)) {
1559 /* the line gets too long, split it up in separate records */
1560 cont_flush(LOG_CONT
);
1565 cont
.facility
= facility
;
1567 cont
.owner
= current
;
1568 cont
.ts_nsec
= local_clock();
1571 cont
.flushed
= false;
1574 memcpy(cont
.buf
+ cont
.len
, text
, len
);
1577 if (cont
.len
> (sizeof(cont
.buf
) * 80) / 100)
1578 cont_flush(LOG_CONT
);
1583 static size_t cont_print_text(char *text
, size_t size
)
1588 if (cont
.cons
== 0 && (console_prev
& LOG_NEWLINE
)) {
1589 textlen
+= print_time(cont
.ts_nsec
, text
);
1593 len
= cont
.len
- cont
.cons
;
1597 memcpy(text
+ textlen
, cont
.buf
+ cont
.cons
, len
);
1599 cont
.cons
= cont
.len
;
1603 if (cont
.flags
& LOG_NEWLINE
)
1604 text
[textlen
++] = '\n';
1605 /* got everything, release buffer */
1611 asmlinkage
int vprintk_emit(int facility
, int level
,
1612 const char *dict
, size_t dictlen
,
1613 const char *fmt
, va_list args
)
1615 static int recursion_bug
;
1616 static char textbuf
[LOG_LINE_MAX
];
1617 char *text
= textbuf
;
1618 size_t text_len
= 0;
1619 enum log_flags lflags
= 0;
1620 unsigned long flags
;
1622 int printed_len
= 0;
1623 bool in_sched
= false;
1624 /* cpu currently holding logbuf_lock in this function */
1625 static volatile unsigned int logbuf_cpu
= UINT_MAX
;
1627 if (level
== SCHED_MESSAGE_LOGLEVEL
) {
1632 boot_delay_msec(level
);
1635 /* This stops the holder of console_sem just where we want him */
1636 local_irq_save(flags
);
1637 this_cpu
= smp_processor_id();
1640 * Ouch, printk recursed into itself!
1642 if (unlikely(logbuf_cpu
== this_cpu
)) {
1644 * If a crash is occurring during printk() on this CPU,
1645 * then try to get the crash message out but make sure
1646 * we can't deadlock. Otherwise just return to avoid the
1647 * recursion and return - but flag the recursion so that
1648 * it can be printed at the next appropriate moment:
1650 if (!oops_in_progress
&& !lockdep_recursing(current
)) {
1652 local_irq_restore(flags
);
1659 raw_spin_lock(&logbuf_lock
);
1660 logbuf_cpu
= this_cpu
;
1662 if (unlikely(recursion_bug
)) {
1663 static const char recursion_msg
[] =
1664 "BUG: recent printk recursion!";
1667 /* emit KERN_CRIT message */
1668 printed_len
+= log_store(0, 2, LOG_PREFIX
|LOG_NEWLINE
, 0,
1669 NULL
, 0, recursion_msg
,
1670 strlen(recursion_msg
));
1674 * The printf needs to come first; we need the syslog
1675 * prefix which might be passed-in as a parameter.
1678 text_len
= scnprintf(text
, sizeof(textbuf
),
1679 KERN_WARNING
"[sched_delayed] ");
1681 text_len
+= vscnprintf(text
+ text_len
,
1682 sizeof(textbuf
) - text_len
, fmt
, args
);
1684 /* mark and strip a trailing newline */
1685 if (text_len
&& text
[text_len
-1] == '\n') {
1687 lflags
|= LOG_NEWLINE
;
1690 /* strip kernel syslog prefix and extract log level or control flags */
1691 if (facility
== 0) {
1692 int kern_level
= printk_get_level(text
);
1695 const char *end_of_header
= printk_skip_level(text
);
1696 switch (kern_level
) {
1699 level
= kern_level
- '0';
1700 case 'd': /* KERN_DEFAULT */
1701 lflags
|= LOG_PREFIX
;
1704 * No need to check length here because vscnprintf
1705 * put '\0' at the end of the string. Only valid and
1706 * newly printed level is detected.
1708 text_len
-= end_of_header
- text
;
1709 text
= (char *)end_of_header
;
1714 level
= default_message_loglevel
;
1717 lflags
|= LOG_PREFIX
|LOG_NEWLINE
;
1719 if (!(lflags
& LOG_NEWLINE
)) {
1721 * Flush the conflicting buffer. An earlier newline was missing,
1722 * or another task also prints continuation lines.
1724 if (cont
.len
&& (lflags
& LOG_PREFIX
|| cont
.owner
!= current
))
1725 cont_flush(LOG_NEWLINE
);
1727 /* buffer line if possible, otherwise store it right away */
1728 if (cont_add(facility
, level
, text
, text_len
))
1729 printed_len
+= text_len
;
1731 printed_len
+= log_store(facility
, level
,
1732 lflags
| LOG_CONT
, 0,
1733 dict
, dictlen
, text
, text_len
);
1735 bool stored
= false;
1738 * If an earlier newline was missing and it was the same task,
1739 * either merge it with the current buffer and flush, or if
1740 * there was a race with interrupts (prefix == true) then just
1741 * flush it out and store this line separately.
1742 * If the preceding printk was from a different task and missed
1743 * a newline, flush and append the newline.
1746 if (cont
.owner
== current
&& !(lflags
& LOG_PREFIX
))
1747 stored
= cont_add(facility
, level
, text
,
1749 cont_flush(LOG_NEWLINE
);
1753 printed_len
+= text_len
;
1755 printed_len
+= log_store(facility
, level
, lflags
, 0,
1756 dict
, dictlen
, text
, text_len
);
1759 logbuf_cpu
= UINT_MAX
;
1760 raw_spin_unlock(&logbuf_lock
);
1762 local_irq_restore(flags
);
1764 /* If called from the scheduler, we can not call up(). */
1768 * Disable preemption to avoid being preempted while holding
1769 * console_sem which would prevent anyone from printing to
1775 * Try to acquire and then immediately release the console
1776 * semaphore. The release will print out buffers and wake up
1777 * /dev/kmsg and syslog() users.
1779 if (console_trylock_for_printk())
1787 EXPORT_SYMBOL(vprintk_emit
);
1789 asmlinkage
int vprintk(const char *fmt
, va_list args
)
1791 return vprintk_emit(0, -1, NULL
, 0, fmt
, args
);
1793 EXPORT_SYMBOL(vprintk
);
1795 asmlinkage
int printk_emit(int facility
, int level
,
1796 const char *dict
, size_t dictlen
,
1797 const char *fmt
, ...)
1802 va_start(args
, fmt
);
1803 r
= vprintk_emit(facility
, level
, dict
, dictlen
, fmt
, args
);
1808 EXPORT_SYMBOL(printk_emit
);
1811 * printk - print a kernel message
1812 * @fmt: format string
1814 * This is printk(). It can be called from any context. We want it to work.
1816 * We try to grab the console_lock. If we succeed, it's easy - we log the
1817 * output and call the console drivers. If we fail to get the semaphore, we
1818 * place the output into the log buffer and return. The current holder of
1819 * the console_sem will notice the new output in console_unlock(); and will
1820 * send it to the consoles before releasing the lock.
1822 * One effect of this deferred printing is that code which calls printk() and
1823 * then changes console_loglevel may break. This is because console_loglevel
1824 * is inspected when the actual printing occurs.
1829 * See the vsnprintf() documentation for format string extensions over C99.
1831 asmlinkage __visible
int printk(const char *fmt
, ...)
1836 #ifdef CONFIG_KGDB_KDB
1837 if (unlikely(kdb_trap_printk
)) {
1838 va_start(args
, fmt
);
1839 r
= vkdb_printf(fmt
, args
);
1844 va_start(args
, fmt
);
1845 r
= vprintk_emit(0, -1, NULL
, 0, fmt
, args
);
1850 EXPORT_SYMBOL(printk
);
1852 #else /* CONFIG_PRINTK */
1854 #define LOG_LINE_MAX 0
1855 #define PREFIX_MAX 0
1857 static u64 syslog_seq
;
1858 static u32 syslog_idx
;
1859 static u64 console_seq
;
1860 static u32 console_idx
;
1861 static enum log_flags syslog_prev
;
1862 static u64 log_first_seq
;
1863 static u32 log_first_idx
;
1864 static u64 log_next_seq
;
1865 static enum log_flags console_prev
;
1866 static struct cont
{
1872 static struct printk_log
*log_from_idx(u32 idx
) { return NULL
; }
1873 static u32
log_next(u32 idx
) { return 0; }
1874 static void call_console_drivers(int level
, const char *text
, size_t len
) {}
1875 static size_t msg_print_text(const struct printk_log
*msg
, enum log_flags prev
,
1876 bool syslog
, char *buf
, size_t size
) { return 0; }
1877 static size_t cont_print_text(char *text
, size_t size
) { return 0; }
1879 #endif /* CONFIG_PRINTK */
1881 #ifdef CONFIG_EARLY_PRINTK
1882 struct console
*early_console
;
1884 void early_vprintk(const char *fmt
, va_list ap
)
1886 if (early_console
) {
1888 int n
= vscnprintf(buf
, sizeof(buf
), fmt
, ap
);
1890 early_console
->write(early_console
, buf
, n
);
1894 asmlinkage __visible
void early_printk(const char *fmt
, ...)
1899 early_vprintk(fmt
, ap
);
1904 static int __add_preferred_console(char *name
, int idx
, char *options
,
1907 struct console_cmdline
*c
;
1911 * See if this tty is not yet registered, and
1912 * if we have a slot free.
1914 for (i
= 0, c
= console_cmdline
;
1915 i
< MAX_CMDLINECONSOLES
&& c
->name
[0];
1917 if (strcmp(c
->name
, name
) == 0 && c
->index
== idx
) {
1919 selected_console
= i
;
1923 if (i
== MAX_CMDLINECONSOLES
)
1926 selected_console
= i
;
1927 strlcpy(c
->name
, name
, sizeof(c
->name
));
1928 c
->options
= options
;
1929 braille_set_options(c
, brl_options
);
1935 * Set up a console. Called via do_early_param() in init/main.c
1936 * for each "console=" parameter in the boot command line.
1938 static int __init
console_setup(char *str
)
1940 char buf
[sizeof(console_cmdline
[0].name
) + 4]; /* 4 for "ttyS" */
1941 char *s
, *options
, *brl_options
= NULL
;
1944 if (_braille_console_setup(&str
, &brl_options
))
1948 * Decode str into name, index, options.
1950 if (str
[0] >= '0' && str
[0] <= '9') {
1951 strcpy(buf
, "ttyS");
1952 strncpy(buf
+ 4, str
, sizeof(buf
) - 5);
1954 strncpy(buf
, str
, sizeof(buf
) - 1);
1956 buf
[sizeof(buf
) - 1] = 0;
1957 options
= strchr(str
, ',');
1961 if (!strcmp(str
, "ttya"))
1962 strcpy(buf
, "ttyS0");
1963 if (!strcmp(str
, "ttyb"))
1964 strcpy(buf
, "ttyS1");
1966 for (s
= buf
; *s
; s
++)
1967 if (isdigit(*s
) || *s
== ',')
1969 idx
= simple_strtoul(s
, NULL
, 10);
1972 __add_preferred_console(buf
, idx
, options
, brl_options
);
1973 console_set_on_cmdline
= 1;
1976 __setup("console=", console_setup
);
1979 * add_preferred_console - add a device to the list of preferred consoles.
1980 * @name: device name
1981 * @idx: device index
1982 * @options: options for this console
1984 * The last preferred console added will be used for kernel messages
1985 * and stdin/out/err for init. Normally this is used by console_setup
1986 * above to handle user-supplied console arguments; however it can also
1987 * be used by arch-specific code either to override the user or more
1988 * commonly to provide a default console (ie from PROM variables) when
1989 * the user has not supplied one.
1991 int add_preferred_console(char *name
, int idx
, char *options
)
1993 return __add_preferred_console(name
, idx
, options
, NULL
);
1996 int update_console_cmdline(char *name
, int idx
, char *name_new
, int idx_new
, char *options
)
1998 struct console_cmdline
*c
;
2001 for (i
= 0, c
= console_cmdline
;
2002 i
< MAX_CMDLINECONSOLES
&& c
->name
[0];
2004 if (strcmp(c
->name
, name
) == 0 && c
->index
== idx
) {
2005 strlcpy(c
->name
, name_new
, sizeof(c
->name
));
2006 c
->options
= options
;
2014 bool console_suspend_enabled
= true;
2015 EXPORT_SYMBOL(console_suspend_enabled
);
2017 static int __init
console_suspend_disable(char *str
)
2019 console_suspend_enabled
= false;
2022 __setup("no_console_suspend", console_suspend_disable
);
2023 module_param_named(console_suspend
, console_suspend_enabled
,
2024 bool, S_IRUGO
| S_IWUSR
);
2025 MODULE_PARM_DESC(console_suspend
, "suspend console during suspend"
2026 " and hibernate operations");
2029 * suspend_console - suspend the console subsystem
2031 * This disables printk() while we go into suspend states
2033 void suspend_console(void)
2035 if (!console_suspend_enabled
)
2037 printk("Suspending console(s) (use no_console_suspend to debug)\n");
2039 console_suspended
= 1;
2043 void resume_console(void)
2045 if (!console_suspend_enabled
)
2048 console_suspended
= 0;
2053 * console_cpu_notify - print deferred console messages after CPU hotplug
2054 * @self: notifier struct
2055 * @action: CPU hotplug event
2058 * If printk() is called from a CPU that is not online yet, the messages
2059 * will be spooled but will not show up on the console. This function is
2060 * called when a new CPU comes online (or fails to come up), and ensures
2061 * that any such output gets printed.
2063 static int console_cpu_notify(struct notifier_block
*self
,
2064 unsigned long action
, void *hcpu
)
2069 case CPU_DOWN_FAILED
:
2070 case CPU_UP_CANCELED
:
2078 * console_lock - lock the console system for exclusive use.
2080 * Acquires a lock which guarantees that the caller has
2081 * exclusive access to the console system and the console_drivers list.
2083 * Can sleep, returns nothing.
2085 void console_lock(void)
2090 if (console_suspended
)
2093 console_may_schedule
= 1;
2095 EXPORT_SYMBOL(console_lock
);
2098 * console_trylock - try to lock the console system for exclusive use.
2100 * Try to acquire a lock which guarantees that the caller has exclusive
2101 * access to the console system and the console_drivers list.
2103 * returns 1 on success, and 0 on failure to acquire the lock.
2105 int console_trylock(void)
2107 if (down_trylock_console_sem())
2109 if (console_suspended
) {
2114 console_may_schedule
= 0;
2117 EXPORT_SYMBOL(console_trylock
);
2119 int is_console_locked(void)
2121 return console_locked
;
2124 static void console_cont_flush(char *text
, size_t size
)
2126 unsigned long flags
;
2129 raw_spin_lock_irqsave(&logbuf_lock
, flags
);
2135 * We still queue earlier records, likely because the console was
2136 * busy. The earlier ones need to be printed before this one, we
2137 * did not flush any fragment so far, so just let it queue up.
2139 if (console_seq
< log_next_seq
&& !cont
.cons
)
2142 len
= cont_print_text(text
, size
);
2143 raw_spin_unlock(&logbuf_lock
);
2144 stop_critical_timings();
2145 call_console_drivers(cont
.level
, text
, len
);
2146 start_critical_timings();
2147 local_irq_restore(flags
);
2150 raw_spin_unlock_irqrestore(&logbuf_lock
, flags
);
2154 * console_unlock - unlock the console system
2156 * Releases the console_lock which the caller holds on the console system
2157 * and the console driver list.
2159 * While the console_lock was held, console output may have been buffered
2160 * by printk(). If this is the case, console_unlock(); emits
2161 * the output prior to releasing the lock.
2163 * If there is output waiting, we wake /dev/kmsg and syslog() users.
2165 * console_unlock(); may be called from any context.
2167 void console_unlock(void)
2169 static char text
[LOG_LINE_MAX
+ PREFIX_MAX
];
2170 static u64 seen_seq
;
2171 unsigned long flags
;
2172 bool wake_klogd
= false;
2175 if (console_suspended
) {
2180 console_may_schedule
= 0;
2182 /* flush buffered message fragment immediately to console */
2183 console_cont_flush(text
, sizeof(text
));
2186 struct printk_log
*msg
;
2190 raw_spin_lock_irqsave(&logbuf_lock
, flags
);
2191 if (seen_seq
!= log_next_seq
) {
2193 seen_seq
= log_next_seq
;
2196 if (console_seq
< log_first_seq
) {
2197 len
= sprintf(text
, "** %u printk messages dropped ** ",
2198 (unsigned)(log_first_seq
- console_seq
));
2200 /* messages are gone, move to first one */
2201 console_seq
= log_first_seq
;
2202 console_idx
= log_first_idx
;
2208 if (console_seq
== log_next_seq
)
2211 msg
= log_from_idx(console_idx
);
2212 if (msg
->flags
& LOG_NOCONS
) {
2214 * Skip record we have buffered and already printed
2215 * directly to the console when we received it.
2217 console_idx
= log_next(console_idx
);
2220 * We will get here again when we register a new
2221 * CON_PRINTBUFFER console. Clear the flag so we
2222 * will properly dump everything later.
2224 msg
->flags
&= ~LOG_NOCONS
;
2225 console_prev
= msg
->flags
;
2230 len
+= msg_print_text(msg
, console_prev
, false,
2231 text
+ len
, sizeof(text
) - len
);
2232 console_idx
= log_next(console_idx
);
2234 console_prev
= msg
->flags
;
2235 raw_spin_unlock(&logbuf_lock
);
2237 stop_critical_timings(); /* don't trace print latency */
2238 call_console_drivers(level
, text
, len
);
2239 start_critical_timings();
2240 local_irq_restore(flags
);
2244 /* Release the exclusive_console once it is used */
2245 if (unlikely(exclusive_console
))
2246 exclusive_console
= NULL
;
2248 raw_spin_unlock(&logbuf_lock
);
2253 * Someone could have filled up the buffer again, so re-check if there's
2254 * something to flush. In case we cannot trylock the console_sem again,
2255 * there's a new owner and the console_unlock() from them will do the
2256 * flush, no worries.
2258 raw_spin_lock(&logbuf_lock
);
2259 retry
= console_seq
!= log_next_seq
;
2260 raw_spin_unlock_irqrestore(&logbuf_lock
, flags
);
2262 if (retry
&& console_trylock())
2268 EXPORT_SYMBOL(console_unlock
);
2271 * console_conditional_schedule - yield the CPU if required
2273 * If the console code is currently allowed to sleep, and
2274 * if this CPU should yield the CPU to another task, do
2277 * Must be called within console_lock();.
2279 void __sched
console_conditional_schedule(void)
2281 if (console_may_schedule
)
2284 EXPORT_SYMBOL(console_conditional_schedule
);
2286 void console_unblank(void)
2291 * console_unblank can no longer be called in interrupt context unless
2292 * oops_in_progress is set to 1..
2294 if (oops_in_progress
) {
2295 if (down_trylock_console_sem() != 0)
2301 console_may_schedule
= 0;
2303 if ((c
->flags
& CON_ENABLED
) && c
->unblank
)
2309 * Return the console tty driver structure and its associated index
2311 struct tty_driver
*console_device(int *index
)
2314 struct tty_driver
*driver
= NULL
;
2317 for_each_console(c
) {
2320 driver
= c
->device(c
, index
);
2329 * Prevent further output on the passed console device so that (for example)
2330 * serial drivers can disable console output before suspending a port, and can
2331 * re-enable output afterwards.
2333 void console_stop(struct console
*console
)
2336 console
->flags
&= ~CON_ENABLED
;
2339 EXPORT_SYMBOL(console_stop
);
2341 void console_start(struct console
*console
)
2344 console
->flags
|= CON_ENABLED
;
2347 EXPORT_SYMBOL(console_start
);
2349 static int __read_mostly keep_bootcon
;
2351 static int __init
keep_bootcon_setup(char *str
)
2354 pr_info("debug: skip boot console de-registration.\n");
2359 early_param("keep_bootcon", keep_bootcon_setup
);
2362 * The console driver calls this routine during kernel initialization
2363 * to register the console printing procedure with printk() and to
2364 * print any messages that were printed by the kernel before the
2365 * console driver was initialized.
2367 * This can happen pretty early during the boot process (because of
2368 * early_printk) - sometimes before setup_arch() completes - be careful
2369 * of what kernel features are used - they may not be initialised yet.
2371 * There are two types of consoles - bootconsoles (early_printk) and
2372 * "real" consoles (everything which is not a bootconsole) which are
2373 * handled differently.
2374 * - Any number of bootconsoles can be registered at any time.
2375 * - As soon as a "real" console is registered, all bootconsoles
2376 * will be unregistered automatically.
2377 * - Once a "real" console is registered, any attempt to register a
2378 * bootconsoles will be rejected
2380 void register_console(struct console
*newcon
)
2383 unsigned long flags
;
2384 struct console
*bcon
= NULL
;
2385 struct console_cmdline
*c
;
2387 if (console_drivers
)
2388 for_each_console(bcon
)
2389 if (WARN(bcon
== newcon
,
2390 "console '%s%d' already registered\n",
2391 bcon
->name
, bcon
->index
))
2395 * before we register a new CON_BOOT console, make sure we don't
2396 * already have a valid console
2398 if (console_drivers
&& newcon
->flags
& CON_BOOT
) {
2399 /* find the last or real console */
2400 for_each_console(bcon
) {
2401 if (!(bcon
->flags
& CON_BOOT
)) {
2402 pr_info("Too late to register bootconsole %s%d\n",
2403 newcon
->name
, newcon
->index
);
2409 if (console_drivers
&& console_drivers
->flags
& CON_BOOT
)
2410 bcon
= console_drivers
;
2412 if (preferred_console
< 0 || bcon
|| !console_drivers
)
2413 preferred_console
= selected_console
;
2415 if (newcon
->early_setup
)
2416 newcon
->early_setup();
2419 * See if we want to use this console driver. If we
2420 * didn't select a console we take the first one
2421 * that registers here.
2423 if (preferred_console
< 0) {
2424 if (newcon
->index
< 0)
2426 if (newcon
->setup
== NULL
||
2427 newcon
->setup(newcon
, NULL
) == 0) {
2428 newcon
->flags
|= CON_ENABLED
;
2429 if (newcon
->device
) {
2430 newcon
->flags
|= CON_CONSDEV
;
2431 preferred_console
= 0;
2437 * See if this console matches one we selected on
2440 for (i
= 0, c
= console_cmdline
;
2441 i
< MAX_CMDLINECONSOLES
&& c
->name
[0];
2443 if (strcmp(c
->name
, newcon
->name
) != 0)
2445 if (newcon
->index
>= 0 &&
2446 newcon
->index
!= c
->index
)
2448 if (newcon
->index
< 0)
2449 newcon
->index
= c
->index
;
2451 if (_braille_register_console(newcon
, c
))
2454 if (newcon
->setup
&&
2455 newcon
->setup(newcon
, console_cmdline
[i
].options
) != 0)
2457 newcon
->flags
|= CON_ENABLED
;
2458 newcon
->index
= c
->index
;
2459 if (i
== selected_console
) {
2460 newcon
->flags
|= CON_CONSDEV
;
2461 preferred_console
= selected_console
;
2466 if (!(newcon
->flags
& CON_ENABLED
))
2470 * If we have a bootconsole, and are switching to a real console,
2471 * don't print everything out again, since when the boot console, and
2472 * the real console are the same physical device, it's annoying to
2473 * see the beginning boot messages twice
2475 if (bcon
&& ((newcon
->flags
& (CON_CONSDEV
| CON_BOOT
)) == CON_CONSDEV
))
2476 newcon
->flags
&= ~CON_PRINTBUFFER
;
2479 * Put this console in the list - keep the
2480 * preferred driver at the head of the list.
2483 if ((newcon
->flags
& CON_CONSDEV
) || console_drivers
== NULL
) {
2484 newcon
->next
= console_drivers
;
2485 console_drivers
= newcon
;
2487 newcon
->next
->flags
&= ~CON_CONSDEV
;
2489 newcon
->next
= console_drivers
->next
;
2490 console_drivers
->next
= newcon
;
2492 if (newcon
->flags
& CON_PRINTBUFFER
) {
2494 * console_unlock(); will print out the buffered messages
2497 raw_spin_lock_irqsave(&logbuf_lock
, flags
);
2498 console_seq
= syslog_seq
;
2499 console_idx
= syslog_idx
;
2500 console_prev
= syslog_prev
;
2501 raw_spin_unlock_irqrestore(&logbuf_lock
, flags
);
2503 * We're about to replay the log buffer. Only do this to the
2504 * just-registered console to avoid excessive message spam to
2505 * the already-registered consoles.
2507 exclusive_console
= newcon
;
2510 console_sysfs_notify();
2513 * By unregistering the bootconsoles after we enable the real console
2514 * we get the "console xxx enabled" message on all the consoles -
2515 * boot consoles, real consoles, etc - this is to ensure that end
2516 * users know there might be something in the kernel's log buffer that
2517 * went to the bootconsole (that they do not see on the real console)
2519 pr_info("%sconsole [%s%d] enabled\n",
2520 (newcon
->flags
& CON_BOOT
) ? "boot" : "" ,
2521 newcon
->name
, newcon
->index
);
2523 ((newcon
->flags
& (CON_CONSDEV
| CON_BOOT
)) == CON_CONSDEV
) &&
2525 /* We need to iterate through all boot consoles, to make
2526 * sure we print everything out, before we unregister them.
2528 for_each_console(bcon
)
2529 if (bcon
->flags
& CON_BOOT
)
2530 unregister_console(bcon
);
2533 EXPORT_SYMBOL(register_console
);
2535 int unregister_console(struct console
*console
)
2537 struct console
*a
, *b
;
2540 pr_info("%sconsole [%s%d] disabled\n",
2541 (console
->flags
& CON_BOOT
) ? "boot" : "" ,
2542 console
->name
, console
->index
);
2544 res
= _braille_unregister_console(console
);
2550 if (console_drivers
== console
) {
2551 console_drivers
=console
->next
;
2553 } else if (console_drivers
) {
2554 for (a
=console_drivers
->next
, b
=console_drivers
;
2555 a
; b
=a
, a
=b
->next
) {
2565 * If this isn't the last console and it has CON_CONSDEV set, we
2566 * need to set it on the next preferred console.
2568 if (console_drivers
!= NULL
&& console
->flags
& CON_CONSDEV
)
2569 console_drivers
->flags
|= CON_CONSDEV
;
2571 console
->flags
&= ~CON_ENABLED
;
2573 console_sysfs_notify();
2576 EXPORT_SYMBOL(unregister_console
);
2578 static int __init
printk_late_init(void)
2580 struct console
*con
;
2582 for_each_console(con
) {
2583 if (!keep_bootcon
&& con
->flags
& CON_BOOT
) {
2584 unregister_console(con
);
2587 hotcpu_notifier(console_cpu_notify
, 0);
2590 late_initcall(printk_late_init
);
2592 #if defined CONFIG_PRINTK
2594 * Delayed printk version, for scheduler-internal messages:
2596 #define PRINTK_PENDING_WAKEUP 0x01
2597 #define PRINTK_PENDING_OUTPUT 0x02
2599 static DEFINE_PER_CPU(int, printk_pending
);
2601 static void wake_up_klogd_work_func(struct irq_work
*irq_work
)
2603 int pending
= __this_cpu_xchg(printk_pending
, 0);
2605 if (pending
& PRINTK_PENDING_OUTPUT
) {
2606 /* If trylock fails, someone else is doing the printing */
2607 if (console_trylock())
2611 if (pending
& PRINTK_PENDING_WAKEUP
)
2612 wake_up_interruptible(&log_wait
);
2615 static DEFINE_PER_CPU(struct irq_work
, wake_up_klogd_work
) = {
2616 .func
= wake_up_klogd_work_func
,
2617 .flags
= IRQ_WORK_LAZY
,
2620 void wake_up_klogd(void)
2623 if (waitqueue_active(&log_wait
)) {
2624 this_cpu_or(printk_pending
, PRINTK_PENDING_WAKEUP
);
2625 irq_work_queue(&__get_cpu_var(wake_up_klogd_work
));
2630 int printk_deferred(const char *fmt
, ...)
2636 va_start(args
, fmt
);
2637 r
= vprintk_emit(0, SCHED_MESSAGE_LOGLEVEL
, NULL
, 0, fmt
, args
);
2640 __this_cpu_or(printk_pending
, PRINTK_PENDING_OUTPUT
);
2641 irq_work_queue(&__get_cpu_var(wake_up_klogd_work
));
2648 * printk rate limiting, lifted from the networking subsystem.
2650 * This enforces a rate limit: not more than 10 kernel messages
2651 * every 5s to make a denial-of-service attack impossible.
2653 DEFINE_RATELIMIT_STATE(printk_ratelimit_state
, 5 * HZ
, 10);
2655 int __printk_ratelimit(const char *func
)
2657 return ___ratelimit(&printk_ratelimit_state
, func
);
2659 EXPORT_SYMBOL(__printk_ratelimit
);
2662 * printk_timed_ratelimit - caller-controlled printk ratelimiting
2663 * @caller_jiffies: pointer to caller's state
2664 * @interval_msecs: minimum interval between prints
2666 * printk_timed_ratelimit() returns true if more than @interval_msecs
2667 * milliseconds have elapsed since the last time printk_timed_ratelimit()
2670 bool printk_timed_ratelimit(unsigned long *caller_jiffies
,
2671 unsigned int interval_msecs
)
2673 unsigned long elapsed
= jiffies
- *caller_jiffies
;
2675 if (*caller_jiffies
&& elapsed
<= msecs_to_jiffies(interval_msecs
))
2678 *caller_jiffies
= jiffies
;
2681 EXPORT_SYMBOL(printk_timed_ratelimit
);
2683 static DEFINE_SPINLOCK(dump_list_lock
);
2684 static LIST_HEAD(dump_list
);
2687 * kmsg_dump_register - register a kernel log dumper.
2688 * @dumper: pointer to the kmsg_dumper structure
2690 * Adds a kernel log dumper to the system. The dump callback in the
2691 * structure will be called when the kernel oopses or panics and must be
2692 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
2694 int kmsg_dump_register(struct kmsg_dumper
*dumper
)
2696 unsigned long flags
;
2699 /* The dump callback needs to be set */
2703 spin_lock_irqsave(&dump_list_lock
, flags
);
2704 /* Don't allow registering multiple times */
2705 if (!dumper
->registered
) {
2706 dumper
->registered
= 1;
2707 list_add_tail_rcu(&dumper
->list
, &dump_list
);
2710 spin_unlock_irqrestore(&dump_list_lock
, flags
);
2714 EXPORT_SYMBOL_GPL(kmsg_dump_register
);
2717 * kmsg_dump_unregister - unregister a kmsg dumper.
2718 * @dumper: pointer to the kmsg_dumper structure
2720 * Removes a dump device from the system. Returns zero on success and
2721 * %-EINVAL otherwise.
2723 int kmsg_dump_unregister(struct kmsg_dumper
*dumper
)
2725 unsigned long flags
;
2728 spin_lock_irqsave(&dump_list_lock
, flags
);
2729 if (dumper
->registered
) {
2730 dumper
->registered
= 0;
2731 list_del_rcu(&dumper
->list
);
2734 spin_unlock_irqrestore(&dump_list_lock
, flags
);
2739 EXPORT_SYMBOL_GPL(kmsg_dump_unregister
);
2741 static bool always_kmsg_dump
;
2742 module_param_named(always_kmsg_dump
, always_kmsg_dump
, bool, S_IRUGO
| S_IWUSR
);
2745 * kmsg_dump - dump kernel log to kernel message dumpers.
2746 * @reason: the reason (oops, panic etc) for dumping
2748 * Call each of the registered dumper's dump() callback, which can
2749 * retrieve the kmsg records with kmsg_dump_get_line() or
2750 * kmsg_dump_get_buffer().
2752 void kmsg_dump(enum kmsg_dump_reason reason
)
2754 struct kmsg_dumper
*dumper
;
2755 unsigned long flags
;
2757 if ((reason
> KMSG_DUMP_OOPS
) && !always_kmsg_dump
)
2761 list_for_each_entry_rcu(dumper
, &dump_list
, list
) {
2762 if (dumper
->max_reason
&& reason
> dumper
->max_reason
)
2765 /* initialize iterator with data about the stored records */
2766 dumper
->active
= true;
2768 raw_spin_lock_irqsave(&logbuf_lock
, flags
);
2769 dumper
->cur_seq
= clear_seq
;
2770 dumper
->cur_idx
= clear_idx
;
2771 dumper
->next_seq
= log_next_seq
;
2772 dumper
->next_idx
= log_next_idx
;
2773 raw_spin_unlock_irqrestore(&logbuf_lock
, flags
);
2775 /* invoke dumper which will iterate over records */
2776 dumper
->dump(dumper
, reason
);
2778 /* reset iterator */
2779 dumper
->active
= false;
2785 * kmsg_dump_get_line_nolock - retrieve one kmsg log line (unlocked version)
2786 * @dumper: registered kmsg dumper
2787 * @syslog: include the "<4>" prefixes
2788 * @line: buffer to copy the line to
2789 * @size: maximum size of the buffer
2790 * @len: length of line placed into buffer
2792 * Start at the beginning of the kmsg buffer, with the oldest kmsg
2793 * record, and copy one record into the provided buffer.
2795 * Consecutive calls will return the next available record moving
2796 * towards the end of the buffer with the youngest messages.
2798 * A return value of FALSE indicates that there are no more records to
2801 * The function is similar to kmsg_dump_get_line(), but grabs no locks.
2803 bool kmsg_dump_get_line_nolock(struct kmsg_dumper
*dumper
, bool syslog
,
2804 char *line
, size_t size
, size_t *len
)
2806 struct printk_log
*msg
;
2810 if (!dumper
->active
)
2813 if (dumper
->cur_seq
< log_first_seq
) {
2814 /* messages are gone, move to first available one */
2815 dumper
->cur_seq
= log_first_seq
;
2816 dumper
->cur_idx
= log_first_idx
;
2820 if (dumper
->cur_seq
>= log_next_seq
)
2823 msg
= log_from_idx(dumper
->cur_idx
);
2824 l
= msg_print_text(msg
, 0, syslog
, line
, size
);
2826 dumper
->cur_idx
= log_next(dumper
->cur_idx
);
2836 * kmsg_dump_get_line - retrieve one kmsg log line
2837 * @dumper: registered kmsg dumper
2838 * @syslog: include the "<4>" prefixes
2839 * @line: buffer to copy the line to
2840 * @size: maximum size of the buffer
2841 * @len: length of line placed into buffer
2843 * Start at the beginning of the kmsg buffer, with the oldest kmsg
2844 * record, and copy one record into the provided buffer.
2846 * Consecutive calls will return the next available record moving
2847 * towards the end of the buffer with the youngest messages.
2849 * A return value of FALSE indicates that there are no more records to
2852 bool kmsg_dump_get_line(struct kmsg_dumper
*dumper
, bool syslog
,
2853 char *line
, size_t size
, size_t *len
)
2855 unsigned long flags
;
2858 raw_spin_lock_irqsave(&logbuf_lock
, flags
);
2859 ret
= kmsg_dump_get_line_nolock(dumper
, syslog
, line
, size
, len
);
2860 raw_spin_unlock_irqrestore(&logbuf_lock
, flags
);
2864 EXPORT_SYMBOL_GPL(kmsg_dump_get_line
);
2867 * kmsg_dump_get_buffer - copy kmsg log lines
2868 * @dumper: registered kmsg dumper
2869 * @syslog: include the "<4>" prefixes
2870 * @buf: buffer to copy the line to
2871 * @size: maximum size of the buffer
2872 * @len: length of line placed into buffer
2874 * Start at the end of the kmsg buffer and fill the provided buffer
2875 * with as many of the the *youngest* kmsg records that fit into it.
2876 * If the buffer is large enough, all available kmsg records will be
2877 * copied with a single call.
2879 * Consecutive calls will fill the buffer with the next block of
2880 * available older records, not including the earlier retrieved ones.
2882 * A return value of FALSE indicates that there are no more records to
2885 bool kmsg_dump_get_buffer(struct kmsg_dumper
*dumper
, bool syslog
,
2886 char *buf
, size_t size
, size_t *len
)
2888 unsigned long flags
;
2893 enum log_flags prev
;
2897 if (!dumper
->active
)
2900 raw_spin_lock_irqsave(&logbuf_lock
, flags
);
2901 if (dumper
->cur_seq
< log_first_seq
) {
2902 /* messages are gone, move to first available one */
2903 dumper
->cur_seq
= log_first_seq
;
2904 dumper
->cur_idx
= log_first_idx
;
2908 if (dumper
->cur_seq
>= dumper
->next_seq
) {
2909 raw_spin_unlock_irqrestore(&logbuf_lock
, flags
);
2913 /* calculate length of entire buffer */
2914 seq
= dumper
->cur_seq
;
2915 idx
= dumper
->cur_idx
;
2917 while (seq
< dumper
->next_seq
) {
2918 struct printk_log
*msg
= log_from_idx(idx
);
2920 l
+= msg_print_text(msg
, prev
, true, NULL
, 0);
2921 idx
= log_next(idx
);
2926 /* move first record forward until length fits into the buffer */
2927 seq
= dumper
->cur_seq
;
2928 idx
= dumper
->cur_idx
;
2930 while (l
> size
&& seq
< dumper
->next_seq
) {
2931 struct printk_log
*msg
= log_from_idx(idx
);
2933 l
-= msg_print_text(msg
, prev
, true, NULL
, 0);
2934 idx
= log_next(idx
);
2939 /* last message in next interation */
2944 while (seq
< dumper
->next_seq
) {
2945 struct printk_log
*msg
= log_from_idx(idx
);
2947 l
+= msg_print_text(msg
, prev
, syslog
, buf
+ l
, size
- l
);
2948 idx
= log_next(idx
);
2953 dumper
->next_seq
= next_seq
;
2954 dumper
->next_idx
= next_idx
;
2956 raw_spin_unlock_irqrestore(&logbuf_lock
, flags
);
2962 EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer
);
2965 * kmsg_dump_rewind_nolock - reset the interator (unlocked version)
2966 * @dumper: registered kmsg dumper
2968 * Reset the dumper's iterator so that kmsg_dump_get_line() and
2969 * kmsg_dump_get_buffer() can be called again and used multiple
2970 * times within the same dumper.dump() callback.
2972 * The function is similar to kmsg_dump_rewind(), but grabs no locks.
2974 void kmsg_dump_rewind_nolock(struct kmsg_dumper
*dumper
)
2976 dumper
->cur_seq
= clear_seq
;
2977 dumper
->cur_idx
= clear_idx
;
2978 dumper
->next_seq
= log_next_seq
;
2979 dumper
->next_idx
= log_next_idx
;
2983 * kmsg_dump_rewind - reset the interator
2984 * @dumper: registered kmsg dumper
2986 * Reset the dumper's iterator so that kmsg_dump_get_line() and
2987 * kmsg_dump_get_buffer() can be called again and used multiple
2988 * times within the same dumper.dump() callback.
2990 void kmsg_dump_rewind(struct kmsg_dumper
*dumper
)
2992 unsigned long flags
;
2994 raw_spin_lock_irqsave(&logbuf_lock
, flags
);
2995 kmsg_dump_rewind_nolock(dumper
);
2996 raw_spin_unlock_irqrestore(&logbuf_lock
, flags
);
2998 EXPORT_SYMBOL_GPL(kmsg_dump_rewind
);
3000 static char dump_stack_arch_desc_str
[128];
3003 * dump_stack_set_arch_desc - set arch-specific str to show with task dumps
3004 * @fmt: printf-style format string
3005 * @...: arguments for the format string
3007 * The configured string will be printed right after utsname during task
3008 * dumps. Usually used to add arch-specific system identifiers. If an
3009 * arch wants to make use of such an ID string, it should initialize this
3010 * as soon as possible during boot.
3012 void __init
dump_stack_set_arch_desc(const char *fmt
, ...)
3016 va_start(args
, fmt
);
3017 vsnprintf(dump_stack_arch_desc_str
, sizeof(dump_stack_arch_desc_str
),
3023 * dump_stack_print_info - print generic debug info for dump_stack()
3024 * @log_lvl: log level
3026 * Arch-specific dump_stack() implementations can use this function to
3027 * print out the same debug information as the generic dump_stack().
3029 void dump_stack_print_info(const char *log_lvl
)
3031 printk("%sCPU: %d PID: %d Comm: %.20s %s %s %.*s\n",
3032 log_lvl
, raw_smp_processor_id(), current
->pid
, current
->comm
,
3033 print_tainted(), init_utsname()->release
,
3034 (int)strcspn(init_utsname()->version
, " "),
3035 init_utsname()->version
);
3037 if (dump_stack_arch_desc_str
[0] != '\0')
3038 printk("%sHardware name: %s\n",
3039 log_lvl
, dump_stack_arch_desc_str
);
3041 print_worker_info(log_lvl
, current
);
3045 * show_regs_print_info - print generic debug info for show_regs()
3046 * @log_lvl: log level
3048 * show_regs() implementations can use this function to print out generic
3049 * debug information.
3051 void show_regs_print_info(const char *log_lvl
)
3053 dump_stack_print_info(log_lvl
);
3055 printk("%stask: %p ti: %p task.ti: %p\n",
3056 log_lvl
, current
, current_thread_info(),
3057 task_thread_info(current
));