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/syscalls.h>
36 #include <linux/kexec.h>
37 #include <linux/kdb.h>
38 #include <linux/ratelimit.h>
39 #include <linux/kmsg_dump.h>
40 #include <linux/syslog.h>
41 #include <linux/cpu.h>
42 #include <linux/notifier.h>
43 #include <linux/rculist.h>
45 #include <asm/uaccess.h>
47 #define CREATE_TRACE_POINTS
48 #include <trace/events/printk.h>
51 * Architectures can override it:
53 void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
57 /* printk's without a loglevel use this.. */
58 #define DEFAULT_MESSAGE_LOGLEVEL CONFIG_DEFAULT_MESSAGE_LOGLEVEL
60 /* We show everything that is MORE important than this.. */
61 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
62 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
64 DECLARE_WAIT_QUEUE_HEAD(log_wait);
66 int console_printk[4] = {
67 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
68 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
69 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
70 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
74 * Low level drivers may need that to know if they can schedule in
75 * their unblank() callback or not. So let's export it.
78 EXPORT_SYMBOL(oops_in_progress);
81 * console_sem protects the console_drivers list, and also
82 * provides serialisation for access to the entire console
85 static DEFINE_SEMAPHORE(console_sem);
86 struct console *console_drivers;
87 EXPORT_SYMBOL_GPL(console_drivers);
90 * This is used for debugging the mess that is the VT code by
91 * keeping track if we have the console semaphore held. It's
92 * definitely not the perfect debug tool (we don't know if _WE_
93 * hold it are racing, but it helps tracking those weird code
94 * path in the console code where we end up in places I want
95 * locked without the console sempahore held
97 static int console_locked, console_suspended;
100 * If exclusive_console is non-NULL then only this console is to be printed to.
102 static struct console *exclusive_console;
105 * Array of consoles built from command line options (console=)
107 struct console_cmdline
109 char name[8]; /* Name of the driver */
110 int index; /* Minor dev. to use */
111 char *options; /* Options for the driver */
112 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
113 char *brl_options; /* Options for braille driver */
117 #define MAX_CMDLINECONSOLES 8
119 static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
120 static int selected_console = -1;
121 static int preferred_console = -1;
122 int console_set_on_cmdline;
123 EXPORT_SYMBOL(console_set_on_cmdline);
125 /* Flag: console code may call schedule() */
126 static int console_may_schedule;
130 * The printk log buffer consists of a chain of concatenated variable
131 * length records. Every record starts with a record header, containing
132 * the overall length of the record.
134 * The heads to the first and last entry in the buffer, as well as the
135 * sequence numbers of these both entries are maintained when messages
138 * If the heads indicate available messages, the length in the header
139 * tells the start next message. A length == 0 for the next message
140 * indicates a wrap-around to the beginning of the buffer.
142 * Every record carries the monotonic timestamp in microseconds, as well as
143 * the standard userspace syslog level and syslog facility. The usual
144 * kernel messages use LOG_KERN; userspace-injected messages always carry
145 * a matching syslog facility, by default LOG_USER. The origin of every
146 * message can be reliably determined that way.
148 * The human readable log message directly follows the message header. The
149 * length of the message text is stored in the header, the stored message
155 u64 ts_nsec; /* timestamp in nanoseconds */
156 u16 len; /* length of entire record */
157 u16 text_len; /* length of text buffer */
158 u16 dict_len; /* length of dictionary buffer */
159 u16 level; /* syslog level + facility */
163 * The logbuf_lock protects kmsg buffer, indices, counters. It is also
164 * used in interesting ways to provide interlocking in console_unlock();
166 static DEFINE_RAW_SPINLOCK(logbuf_lock);
168 /* cpu currently holding logbuf_lock */
169 static volatile unsigned int logbuf_cpu = UINT_MAX;
171 #define LOG_LINE_MAX 1024
174 #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
175 static char __log_buf[__LOG_BUF_LEN];
176 static char *log_buf = __log_buf;
177 static u32 log_buf_len = __LOG_BUF_LEN;
179 /* index and sequence number of the first record stored in the buffer */
180 static u64 log_first_seq;
181 static u32 log_first_idx;
183 /* index and sequence number of the next record to store in the buffer */
184 static u64 log_next_seq;
185 static u32 log_next_idx;
187 /* the next printk record to read after the last 'clear' command */
188 static u64 clear_seq;
189 static u32 clear_idx;
191 /* the next printk record to read by syslog(READ) or /proc/kmsg */
192 static u64 syslog_seq;
193 static u32 syslog_idx;
195 /* human readable text of the record */
196 static char *log_text(const struct log *msg)
198 return (char *)msg + sizeof(struct log);
201 /* optional key/value pair dictionary attached to the record */
202 static char *log_dict(const struct log *msg)
204 return (char *)msg + sizeof(struct log) + msg->text_len;
207 /* get record by index; idx must point to valid msg */
208 static struct log *log_from_idx(u32 idx)
210 struct log *msg = (struct log *)(log_buf + idx);
213 * A length == 0 record is the end of buffer marker. Wrap around and
214 * read the message at the start of the buffer.
217 return (struct log *)log_buf;
221 /* get next record; idx must point to valid msg */
222 static u32 log_next(u32 idx)
224 struct log *msg = (struct log *)(log_buf + idx);
226 /* length == 0 indicates the end of the buffer; wrap */
228 * A length == 0 record is the end of buffer marker. Wrap around and
229 * read the message at the start of the buffer as *this* one, and
230 * return the one after that.
233 msg = (struct log *)log_buf;
236 return idx + msg->len;
239 #if !defined(CONFIG_64BIT) || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
245 /* insert record into the buffer, discard old ones, update heads */
246 static void log_store(int facility, int level,
247 const char *dict, u16 dict_len,
248 const char *text, u16 text_len)
253 /* number of '\0' padding bytes to next message */
254 size = sizeof(struct log) + text_len + dict_len;
255 pad_len = (-size) & (LOG_ALIGN - 1);
258 while (log_first_seq < log_next_seq) {
261 if (log_next_idx > log_first_idx)
262 free = max(log_buf_len - log_next_idx, log_first_idx);
264 free = log_first_idx - log_next_idx;
266 if (free > size + sizeof(struct log))
269 /* drop old messages until we have enough contiuous space */
270 log_first_idx = log_next(log_first_idx);
274 if (log_next_idx + size + sizeof(struct log) >= log_buf_len) {
276 * This message + an additional empty header does not fit
277 * at the end of the buffer. Add an empty header with len == 0
278 * to signify a wrap around.
280 memset(log_buf + log_next_idx, 0, sizeof(struct log));
285 msg = (struct log *)(log_buf + log_next_idx);
286 memcpy(log_text(msg), text, text_len);
287 msg->text_len = text_len;
288 memcpy(log_dict(msg), dict, dict_len);
289 msg->dict_len = dict_len;
290 msg->level = (facility << 3) | (level & 7);
291 msg->ts_nsec = local_clock();
292 memset(log_dict(msg) + dict_len, 0, pad_len);
293 msg->len = sizeof(struct log) + text_len + dict_len + pad_len;
296 log_next_idx += msg->len;
302 * This appends the listed symbols to /proc/vmcoreinfo
304 * /proc/vmcoreinfo is used by various utiilties, like crash and makedumpfile to
305 * obtain access to symbols that are otherwise very difficult to locate. These
306 * symbols are specifically used so that utilities can access and extract the
307 * dmesg log from a vmcore file after a crash.
309 void log_buf_kexec_setup(void)
311 VMCOREINFO_SYMBOL(log_buf);
312 VMCOREINFO_SYMBOL(log_buf_len);
313 VMCOREINFO_SYMBOL(log_first_idx);
314 VMCOREINFO_SYMBOL(log_next_idx);
318 /* requested log_buf_len from kernel cmdline */
319 static unsigned long __initdata new_log_buf_len;
321 /* save requested log_buf_len since it's too early to process it */
322 static int __init log_buf_len_setup(char *str)
324 unsigned size = memparse(str, &str);
327 size = roundup_pow_of_two(size);
328 if (size > log_buf_len)
329 new_log_buf_len = size;
333 early_param("log_buf_len", log_buf_len_setup);
335 void __init setup_log_buf(int early)
341 if (!new_log_buf_len)
347 mem = memblock_alloc(new_log_buf_len, PAGE_SIZE);
350 new_log_buf = __va(mem);
352 new_log_buf = alloc_bootmem_nopanic(new_log_buf_len);
355 if (unlikely(!new_log_buf)) {
356 pr_err("log_buf_len: %ld bytes not available\n",
361 raw_spin_lock_irqsave(&logbuf_lock, flags);
362 log_buf_len = new_log_buf_len;
363 log_buf = new_log_buf;
365 free = __LOG_BUF_LEN - log_next_idx;
366 memcpy(log_buf, __log_buf, __LOG_BUF_LEN);
367 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
369 pr_info("log_buf_len: %d\n", log_buf_len);
370 pr_info("early log buf free: %d(%d%%)\n",
371 free, (free * 100) / __LOG_BUF_LEN);
374 #ifdef CONFIG_BOOT_PRINTK_DELAY
376 static int boot_delay; /* msecs delay after each printk during bootup */
377 static unsigned long long loops_per_msec; /* based on boot_delay */
379 static int __init boot_delay_setup(char *str)
383 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
384 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
386 get_option(&str, &boot_delay);
387 if (boot_delay > 10 * 1000)
390 pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
391 "HZ: %d, loops_per_msec: %llu\n",
392 boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
395 __setup("boot_delay=", boot_delay_setup);
397 static void boot_delay_msec(void)
399 unsigned long long k;
400 unsigned long timeout;
402 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
405 k = (unsigned long long)loops_per_msec * boot_delay;
407 timeout = jiffies + msecs_to_jiffies(boot_delay);
412 * use (volatile) jiffies to prevent
413 * compiler reduction; loop termination via jiffies
414 * is secondary and may or may not happen.
416 if (time_after(jiffies, timeout))
418 touch_nmi_watchdog();
422 static inline void boot_delay_msec(void)
427 #ifdef CONFIG_SECURITY_DMESG_RESTRICT
428 int dmesg_restrict = 1;
433 static int syslog_action_restricted(int type)
437 /* Unless restricted, we allow "read all" and "get buffer size" for everybody */
438 return type != SYSLOG_ACTION_READ_ALL && type != SYSLOG_ACTION_SIZE_BUFFER;
441 static int check_syslog_permissions(int type, bool from_file)
444 * If this is from /proc/kmsg and we've already opened it, then we've
445 * already done the capabilities checks at open time.
447 if (from_file && type != SYSLOG_ACTION_OPEN)
450 if (syslog_action_restricted(type)) {
451 if (capable(CAP_SYSLOG))
453 /* For historical reasons, accept CAP_SYS_ADMIN too, with a warning */
454 if (capable(CAP_SYS_ADMIN)) {
455 printk_once(KERN_WARNING "%s (%d): "
456 "Attempt to access syslog with CAP_SYS_ADMIN "
457 "but no CAP_SYSLOG (deprecated).\n",
458 current->comm, task_pid_nr(current));
466 #if defined(CONFIG_PRINTK_TIME)
467 static bool printk_time = 1;
469 static bool printk_time;
471 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
473 static int syslog_print_line(u32 idx, char *text, size_t size)
478 msg = log_from_idx(idx);
480 /* calculate length only */
491 len += msg->text_len;
496 len = sprintf(text, "<%u>", msg->level);
499 unsigned long long t = msg->ts_nsec;
500 unsigned long rem_ns = do_div(t, 1000000000);
502 len += sprintf(text + len, "[%5lu.%06lu] ",
503 (unsigned long) t, rem_ns / 1000);
506 if (len + msg->text_len > size)
508 memcpy(text + len, log_text(msg), msg->text_len);
509 len += msg->text_len;
514 static int syslog_print(char __user *buf, int size)
519 text = kmalloc(LOG_LINE_MAX, GFP_KERNEL);
523 raw_spin_lock_irq(&logbuf_lock);
524 if (syslog_seq < log_first_seq) {
525 /* messages are gone, move to first one */
526 syslog_seq = log_first_seq;
527 syslog_idx = log_first_idx;
529 len = syslog_print_line(syslog_idx, text, LOG_LINE_MAX);
530 syslog_idx = log_next(syslog_idx);
532 raw_spin_unlock_irq(&logbuf_lock);
534 if (len > 0 && copy_to_user(buf, text, len))
541 static int syslog_print_all(char __user *buf, int size, bool clear)
546 text = kmalloc(LOG_LINE_MAX, GFP_KERNEL);
550 raw_spin_lock_irq(&logbuf_lock);
556 if (clear_seq < log_first_seq) {
557 /* messages are gone, move to first available one */
558 clear_seq = log_first_seq;
559 clear_idx = log_first_idx;
563 * Find first record that fits, including all following records,
564 * into the user-provided buffer for this dump.
568 while (seq < log_next_seq) {
569 len += syslog_print_line(idx, NULL, 0);
575 while (len > size && seq < log_next_seq) {
576 len -= syslog_print_line(idx, NULL, 0);
581 /* last message in this dump */
582 next_seq = log_next_seq;
585 while (len >= 0 && seq < next_seq) {
588 textlen = syslog_print_line(idx, text, LOG_LINE_MAX);
596 raw_spin_unlock_irq(&logbuf_lock);
597 if (copy_to_user(buf + len, text, textlen))
601 raw_spin_lock_irq(&logbuf_lock);
603 if (seq < log_first_seq) {
604 /* messages are gone, move to next one */
612 clear_seq = log_next_seq;
613 clear_idx = log_next_idx;
615 raw_spin_unlock_irq(&logbuf_lock);
621 int do_syslog(int type, char __user *buf, int len, bool from_file)
624 static int saved_console_loglevel = -1;
627 error = check_syslog_permissions(type, from_file);
631 error = security_syslog(type);
636 case SYSLOG_ACTION_CLOSE: /* Close log */
638 case SYSLOG_ACTION_OPEN: /* Open log */
640 case SYSLOG_ACTION_READ: /* Read from log */
647 if (!access_ok(VERIFY_WRITE, buf, len)) {
651 error = wait_event_interruptible(log_wait,
652 syslog_seq != log_next_seq);
655 error = syslog_print(buf, len);
657 /* Read/clear last kernel messages */
658 case SYSLOG_ACTION_READ_CLEAR:
661 /* Read last kernel messages */
662 case SYSLOG_ACTION_READ_ALL:
669 if (!access_ok(VERIFY_WRITE, buf, len)) {
673 error = syslog_print_all(buf, len, clear);
675 /* Clear ring buffer */
676 case SYSLOG_ACTION_CLEAR:
677 syslog_print_all(NULL, 0, true);
678 /* Disable logging to console */
679 case SYSLOG_ACTION_CONSOLE_OFF:
680 if (saved_console_loglevel == -1)
681 saved_console_loglevel = console_loglevel;
682 console_loglevel = minimum_console_loglevel;
684 /* Enable logging to console */
685 case SYSLOG_ACTION_CONSOLE_ON:
686 if (saved_console_loglevel != -1) {
687 console_loglevel = saved_console_loglevel;
688 saved_console_loglevel = -1;
691 /* Set level of messages printed to console */
692 case SYSLOG_ACTION_CONSOLE_LEVEL:
694 if (len < 1 || len > 8)
696 if (len < minimum_console_loglevel)
697 len = minimum_console_loglevel;
698 console_loglevel = len;
699 /* Implicitly re-enable logging to console */
700 saved_console_loglevel = -1;
703 /* Number of chars in the log buffer */
704 case SYSLOG_ACTION_SIZE_UNREAD:
705 raw_spin_lock_irq(&logbuf_lock);
706 if (syslog_seq < log_first_seq) {
707 /* messages are gone, move to first one */
708 syslog_seq = log_first_seq;
709 syslog_idx = log_first_idx;
713 * Short-cut for poll(/"proc/kmsg") which simply checks
714 * for pending data, not the size; return the count of
715 * records, not the length.
717 error = log_next_idx - syslog_idx;
725 while (seq < log_next_seq) {
726 error += syslog_print_line(idx, NULL, 0);
731 raw_spin_unlock_irq(&logbuf_lock);
733 /* Size of the log buffer */
734 case SYSLOG_ACTION_SIZE_BUFFER:
745 SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
747 return do_syslog(type, buf, len, SYSLOG_FROM_CALL);
750 #ifdef CONFIG_KGDB_KDB
751 /* kdb dmesg command needs access to the syslog buffer. do_syslog()
752 * uses locks so it cannot be used during debugging. Just tell kdb
753 * where the start and end of the physical and logical logs are. This
754 * is equivalent to do_syslog(3).
756 void kdb_syslog_data(char *syslog_data[4])
758 syslog_data[0] = log_buf;
759 syslog_data[1] = log_buf + log_buf_len;
760 syslog_data[2] = log_buf + log_first_idx;
761 syslog_data[3] = log_buf + log_next_idx;
763 #endif /* CONFIG_KGDB_KDB */
765 static bool __read_mostly ignore_loglevel;
767 static int __init ignore_loglevel_setup(char *str)
770 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
775 early_param("ignore_loglevel", ignore_loglevel_setup);
776 module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR);
777 MODULE_PARM_DESC(ignore_loglevel, "ignore loglevel setting, to"
778 "print all kernel messages to the console.");
781 * Call the console drivers, asking them to write out
782 * log_buf[start] to log_buf[end - 1].
783 * The console_lock must be held.
785 static void call_console_drivers(int level, const char *text, size_t len)
789 trace_console(text, 0, len, len);
791 if (level >= console_loglevel && !ignore_loglevel)
793 if (!console_drivers)
796 for_each_console(con) {
797 if (exclusive_console && con != exclusive_console)
799 if (!(con->flags & CON_ENABLED))
803 if (!cpu_online(smp_processor_id()) &&
804 !(con->flags & CON_ANYTIME))
806 con->write(con, text, len);
811 * Zap console related locks when oopsing. Only zap at most once
812 * every 10 seconds, to leave time for slow consoles to print a
815 static void zap_locks(void)
817 static unsigned long oops_timestamp;
819 if (time_after_eq(jiffies, oops_timestamp) &&
820 !time_after(jiffies, oops_timestamp + 30 * HZ))
823 oops_timestamp = jiffies;
826 /* If a crash is occurring, make sure we can't deadlock */
827 raw_spin_lock_init(&logbuf_lock);
828 /* And make sure that we print immediately */
829 sema_init(&console_sem, 1);
832 /* Check if we have any console registered that can be called early in boot. */
833 static int have_callable_console(void)
837 for_each_console(con)
838 if (con->flags & CON_ANYTIME)
845 * Can we actually use the console at this time on this cpu?
847 * Console drivers may assume that per-cpu resources have
848 * been allocated. So unless they're explicitly marked as
849 * being able to cope (CON_ANYTIME) don't call them until
850 * this CPU is officially up.
852 static inline int can_use_console(unsigned int cpu)
854 return cpu_online(cpu) || have_callable_console();
858 * Try to get console ownership to actually show the kernel
859 * messages from a 'printk'. Return true (and with the
860 * console_lock held, and 'console_locked' set) if it
861 * is successful, false otherwise.
863 * This gets called with the 'logbuf_lock' spinlock held and
864 * interrupts disabled. It should return with 'lockbuf_lock'
865 * released but interrupts still disabled.
867 static int console_trylock_for_printk(unsigned int cpu)
868 __releases(&logbuf_lock)
870 int retval = 0, wake = 0;
872 if (console_trylock()) {
876 * If we can't use the console, we need to release
877 * the console semaphore by hand to avoid flushing
878 * the buffer. We need to hold the console semaphore
879 * in order to do this test safely.
881 if (!can_use_console(cpu)) {
887 logbuf_cpu = UINT_MAX;
890 raw_spin_unlock(&logbuf_lock);
894 int printk_delay_msec __read_mostly;
896 static inline void printk_delay(void)
898 if (unlikely(printk_delay_msec)) {
899 int m = printk_delay_msec;
903 touch_nmi_watchdog();
908 asmlinkage int vprintk_emit(int facility, int level,
909 const char *dict, size_t dictlen,
910 const char *fmt, va_list args)
912 static int recursion_bug;
913 static char buf[LOG_LINE_MAX];
914 static size_t buflen;
916 static char textbuf[LOG_LINE_MAX];
917 char *text = textbuf;
921 bool newline = false;
928 /* This stops the holder of console_sem just where we want him */
929 local_irq_save(flags);
930 this_cpu = smp_processor_id();
933 * Ouch, printk recursed into itself!
935 if (unlikely(logbuf_cpu == this_cpu)) {
937 * If a crash is occurring during printk() on this CPU,
938 * then try to get the crash message out but make sure
939 * we can't deadlock. Otherwise just return to avoid the
940 * recursion and return - but flag the recursion so that
941 * it can be printed at the next appropriate moment:
943 if (!oops_in_progress && !lockdep_recursing(current)) {
945 goto out_restore_irqs;
951 raw_spin_lock(&logbuf_lock);
952 logbuf_cpu = this_cpu;
955 static const char recursion_msg[] =
956 "BUG: recent printk recursion!";
959 printed_len += strlen(recursion_msg);
960 /* emit KERN_CRIT message */
961 log_store(0, 2, NULL, 0, recursion_msg, printed_len);
965 * The printf needs to come first; we need the syslog
966 * prefix which might be passed-in as a parameter.
968 textlen = vscnprintf(text, sizeof(textbuf), fmt, args);
970 /* mark and strip a trailing newline */
971 if (textlen && text[textlen-1] == '\n') {
976 /* strip syslog prefix and extract log level or flags */
977 if (text[0] == '<' && text[1] && text[2] == '>') {
981 level = text[1] - '0';
985 case 'c': /* KERN_CONT */
987 case 'd': /* KERN_DEFAULT */
994 if (buflen && (!cont || dict)) {
995 /* no continuation; flush existing buffer */
996 log_store(facility, buflevel, NULL, 0, buf, buflen);
997 printed_len += buflen;
1002 /* remember level for first message in the buffer */
1004 buflevel = default_message_loglevel;
1009 if (buflen || !newline) {
1010 /* append to existing buffer, or buffer until next message */
1011 if (buflen + textlen > sizeof(buf))
1012 textlen = sizeof(buf) - buflen;
1013 memcpy(buf + buflen, text, textlen);
1018 /* end of line; flush buffer */
1020 log_store(facility, buflevel,
1021 dict, dictlen, buf, buflen);
1022 printed_len += buflen;
1025 log_store(facility, buflevel,
1026 dict, dictlen, text, textlen);
1027 printed_len += textlen;
1032 * Try to acquire and then immediately release the console semaphore.
1033 * The release will print out buffers and wake up /dev/kmsg and syslog()
1036 * The console_trylock_for_printk() function will release 'logbuf_lock'
1037 * regardless of whether it actually gets the console semaphore or not.
1039 if (console_trylock_for_printk(this_cpu))
1044 local_irq_restore(flags);
1048 EXPORT_SYMBOL(vprintk_emit);
1050 asmlinkage int vprintk(const char *fmt, va_list args)
1052 return vprintk_emit(0, -1, NULL, 0, fmt, args);
1054 EXPORT_SYMBOL(vprintk);
1056 asmlinkage int printk_emit(int facility, int level,
1057 const char *dict, size_t dictlen,
1058 const char *fmt, ...)
1063 va_start(args, fmt);
1064 r = vprintk_emit(facility, level, dict, dictlen, fmt, args);
1069 EXPORT_SYMBOL(printk_emit);
1072 * printk - print a kernel message
1073 * @fmt: format string
1075 * This is printk(). It can be called from any context. We want it to work.
1077 * We try to grab the console_lock. If we succeed, it's easy - we log the
1078 * output and call the console drivers. If we fail to get the semaphore, we
1079 * place the output into the log buffer and return. The current holder of
1080 * the console_sem will notice the new output in console_unlock(); and will
1081 * send it to the consoles before releasing the lock.
1083 * One effect of this deferred printing is that code which calls printk() and
1084 * then changes console_loglevel may break. This is because console_loglevel
1085 * is inspected when the actual printing occurs.
1090 * See the vsnprintf() documentation for format string extensions over C99.
1092 asmlinkage int printk(const char *fmt, ...)
1097 #ifdef CONFIG_KGDB_KDB
1098 if (unlikely(kdb_trap_printk)) {
1099 va_start(args, fmt);
1100 r = vkdb_printf(fmt, args);
1105 va_start(args, fmt);
1106 r = vprintk_emit(0, -1, NULL, 0, fmt, args);
1111 EXPORT_SYMBOL(printk);
1114 static void call_console_drivers(int level, const char *text, size_t len)
1120 static int __add_preferred_console(char *name, int idx, char *options,
1123 struct console_cmdline *c;
1127 * See if this tty is not yet registered, and
1128 * if we have a slot free.
1130 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
1131 if (strcmp(console_cmdline[i].name, name) == 0 &&
1132 console_cmdline[i].index == idx) {
1134 selected_console = i;
1137 if (i == MAX_CMDLINECONSOLES)
1140 selected_console = i;
1141 c = &console_cmdline[i];
1142 strlcpy(c->name, name, sizeof(c->name));
1143 c->options = options;
1144 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1145 c->brl_options = brl_options;
1151 * Set up a list of consoles. Called from init/main.c
1153 static int __init console_setup(char *str)
1155 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
1156 char *s, *options, *brl_options = NULL;
1159 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1160 if (!memcmp(str, "brl,", 4)) {
1163 } else if (!memcmp(str, "brl=", 4)) {
1164 brl_options = str + 4;
1165 str = strchr(brl_options, ',');
1167 printk(KERN_ERR "need port name after brl=\n");
1175 * Decode str into name, index, options.
1177 if (str[0] >= '0' && str[0] <= '9') {
1178 strcpy(buf, "ttyS");
1179 strncpy(buf + 4, str, sizeof(buf) - 5);
1181 strncpy(buf, str, sizeof(buf) - 1);
1183 buf[sizeof(buf) - 1] = 0;
1184 if ((options = strchr(str, ',')) != NULL)
1187 if (!strcmp(str, "ttya"))
1188 strcpy(buf, "ttyS0");
1189 if (!strcmp(str, "ttyb"))
1190 strcpy(buf, "ttyS1");
1192 for (s = buf; *s; s++)
1193 if ((*s >= '0' && *s <= '9') || *s == ',')
1195 idx = simple_strtoul(s, NULL, 10);
1198 __add_preferred_console(buf, idx, options, brl_options);
1199 console_set_on_cmdline = 1;
1202 __setup("console=", console_setup);
1205 * add_preferred_console - add a device to the list of preferred consoles.
1206 * @name: device name
1207 * @idx: device index
1208 * @options: options for this console
1210 * The last preferred console added will be used for kernel messages
1211 * and stdin/out/err for init. Normally this is used by console_setup
1212 * above to handle user-supplied console arguments; however it can also
1213 * be used by arch-specific code either to override the user or more
1214 * commonly to provide a default console (ie from PROM variables) when
1215 * the user has not supplied one.
1217 int add_preferred_console(char *name, int idx, char *options)
1219 return __add_preferred_console(name, idx, options, NULL);
1222 int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
1224 struct console_cmdline *c;
1227 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
1228 if (strcmp(console_cmdline[i].name, name) == 0 &&
1229 console_cmdline[i].index == idx) {
1230 c = &console_cmdline[i];
1231 strlcpy(c->name, name_new, sizeof(c->name));
1232 c->name[sizeof(c->name) - 1] = 0;
1233 c->options = options;
1241 bool console_suspend_enabled = 1;
1242 EXPORT_SYMBOL(console_suspend_enabled);
1244 static int __init console_suspend_disable(char *str)
1246 console_suspend_enabled = 0;
1249 __setup("no_console_suspend", console_suspend_disable);
1250 module_param_named(console_suspend, console_suspend_enabled,
1251 bool, S_IRUGO | S_IWUSR);
1252 MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
1253 " and hibernate operations");
1256 * suspend_console - suspend the console subsystem
1258 * This disables printk() while we go into suspend states
1260 void suspend_console(void)
1262 if (!console_suspend_enabled)
1264 printk("Suspending console(s) (use no_console_suspend to debug)\n");
1266 console_suspended = 1;
1270 void resume_console(void)
1272 if (!console_suspend_enabled)
1275 console_suspended = 0;
1280 * console_cpu_notify - print deferred console messages after CPU hotplug
1281 * @self: notifier struct
1282 * @action: CPU hotplug event
1285 * If printk() is called from a CPU that is not online yet, the messages
1286 * will be spooled but will not show up on the console. This function is
1287 * called when a new CPU comes online (or fails to come up), and ensures
1288 * that any such output gets printed.
1290 static int __cpuinit console_cpu_notify(struct notifier_block *self,
1291 unsigned long action, void *hcpu)
1297 case CPU_DOWN_FAILED:
1298 case CPU_UP_CANCELED:
1306 * console_lock - lock the console system for exclusive use.
1308 * Acquires a lock which guarantees that the caller has
1309 * exclusive access to the console system and the console_drivers list.
1311 * Can sleep, returns nothing.
1313 void console_lock(void)
1315 BUG_ON(in_interrupt());
1317 if (console_suspended)
1320 console_may_schedule = 1;
1322 EXPORT_SYMBOL(console_lock);
1325 * console_trylock - try to lock the console system for exclusive use.
1327 * Tried to acquire a lock which guarantees that the caller has
1328 * exclusive access to the console system and the console_drivers list.
1330 * returns 1 on success, and 0 on failure to acquire the lock.
1332 int console_trylock(void)
1334 if (down_trylock(&console_sem))
1336 if (console_suspended) {
1341 console_may_schedule = 0;
1344 EXPORT_SYMBOL(console_trylock);
1346 int is_console_locked(void)
1348 return console_locked;
1352 * Delayed printk version, for scheduler-internal messages:
1354 #define PRINTK_BUF_SIZE 512
1356 #define PRINTK_PENDING_WAKEUP 0x01
1357 #define PRINTK_PENDING_SCHED 0x02
1359 static DEFINE_PER_CPU(int, printk_pending);
1360 static DEFINE_PER_CPU(char [PRINTK_BUF_SIZE], printk_sched_buf);
1362 void printk_tick(void)
1364 if (__this_cpu_read(printk_pending)) {
1365 int pending = __this_cpu_xchg(printk_pending, 0);
1366 if (pending & PRINTK_PENDING_SCHED) {
1367 char *buf = __get_cpu_var(printk_sched_buf);
1368 printk(KERN_WARNING "[sched_delayed] %s", buf);
1370 if (pending & PRINTK_PENDING_WAKEUP)
1371 wake_up_interruptible(&log_wait);
1375 int printk_needs_cpu(int cpu)
1377 if (cpu_is_offline(cpu))
1379 return __this_cpu_read(printk_pending);
1382 void wake_up_klogd(void)
1384 if (waitqueue_active(&log_wait))
1385 this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP);
1388 /* the next printk record to write to the console */
1389 static u64 console_seq;
1390 static u32 console_idx;
1393 * console_unlock - unlock the console system
1395 * Releases the console_lock which the caller holds on the console system
1396 * and the console driver list.
1398 * While the console_lock was held, console output may have been buffered
1399 * by printk(). If this is the case, console_unlock(); emits
1400 * the output prior to releasing the lock.
1402 * If there is output waiting, we wake it /dev/kmsg and syslog() users.
1404 * console_unlock(); may be called from any context.
1406 void console_unlock(void)
1408 static u64 seen_seq;
1409 unsigned long flags;
1410 bool wake_klogd = false;
1413 if (console_suspended) {
1418 console_may_schedule = 0;
1423 static char text[LOG_LINE_MAX];
1427 raw_spin_lock_irqsave(&logbuf_lock, flags);
1428 if (seen_seq != log_next_seq) {
1430 seen_seq = log_next_seq;
1433 if (console_seq < log_first_seq) {
1434 /* messages are gone, move to first one */
1435 console_seq = log_first_seq;
1436 console_idx = log_first_idx;
1439 if (console_seq == log_next_seq)
1442 msg = log_from_idx(console_idx);
1443 level = msg->level & 7;
1444 len = msg->text_len;
1445 if (len+1 >= sizeof(text))
1446 len = sizeof(text)-1;
1447 memcpy(text, log_text(msg), len);
1450 console_idx = log_next(console_idx);
1452 raw_spin_unlock(&logbuf_lock);
1454 stop_critical_timings(); /* don't trace print latency */
1455 call_console_drivers(level, text, len);
1456 start_critical_timings();
1457 local_irq_restore(flags);
1461 /* Release the exclusive_console once it is used */
1462 if (unlikely(exclusive_console))
1463 exclusive_console = NULL;
1465 raw_spin_unlock(&logbuf_lock);
1470 * Someone could have filled up the buffer again, so re-check if there's
1471 * something to flush. In case we cannot trylock the console_sem again,
1472 * there's a new owner and the console_unlock() from them will do the
1473 * flush, no worries.
1475 raw_spin_lock(&logbuf_lock);
1476 retry = console_seq != log_next_seq;
1477 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
1479 if (retry && console_trylock())
1485 EXPORT_SYMBOL(console_unlock);
1488 * console_conditional_schedule - yield the CPU if required
1490 * If the console code is currently allowed to sleep, and
1491 * if this CPU should yield the CPU to another task, do
1494 * Must be called within console_lock();.
1496 void __sched console_conditional_schedule(void)
1498 if (console_may_schedule)
1501 EXPORT_SYMBOL(console_conditional_schedule);
1503 void console_unblank(void)
1508 * console_unblank can no longer be called in interrupt context unless
1509 * oops_in_progress is set to 1..
1511 if (oops_in_progress) {
1512 if (down_trylock(&console_sem) != 0)
1518 console_may_schedule = 0;
1520 if ((c->flags & CON_ENABLED) && c->unblank)
1526 * Return the console tty driver structure and its associated index
1528 struct tty_driver *console_device(int *index)
1531 struct tty_driver *driver = NULL;
1534 for_each_console(c) {
1537 driver = c->device(c, index);
1546 * Prevent further output on the passed console device so that (for example)
1547 * serial drivers can disable console output before suspending a port, and can
1548 * re-enable output afterwards.
1550 void console_stop(struct console *console)
1553 console->flags &= ~CON_ENABLED;
1556 EXPORT_SYMBOL(console_stop);
1558 void console_start(struct console *console)
1561 console->flags |= CON_ENABLED;
1564 EXPORT_SYMBOL(console_start);
1566 static int __read_mostly keep_bootcon;
1568 static int __init keep_bootcon_setup(char *str)
1571 printk(KERN_INFO "debug: skip boot console de-registration.\n");
1576 early_param("keep_bootcon", keep_bootcon_setup);
1579 * The console driver calls this routine during kernel initialization
1580 * to register the console printing procedure with printk() and to
1581 * print any messages that were printed by the kernel before the
1582 * console driver was initialized.
1584 * This can happen pretty early during the boot process (because of
1585 * early_printk) - sometimes before setup_arch() completes - be careful
1586 * of what kernel features are used - they may not be initialised yet.
1588 * There are two types of consoles - bootconsoles (early_printk) and
1589 * "real" consoles (everything which is not a bootconsole) which are
1590 * handled differently.
1591 * - Any number of bootconsoles can be registered at any time.
1592 * - As soon as a "real" console is registered, all bootconsoles
1593 * will be unregistered automatically.
1594 * - Once a "real" console is registered, any attempt to register a
1595 * bootconsoles will be rejected
1597 void register_console(struct console *newcon)
1600 unsigned long flags;
1601 struct console *bcon = NULL;
1604 * before we register a new CON_BOOT console, make sure we don't
1605 * already have a valid console
1607 if (console_drivers && newcon->flags & CON_BOOT) {
1608 /* find the last or real console */
1609 for_each_console(bcon) {
1610 if (!(bcon->flags & CON_BOOT)) {
1611 printk(KERN_INFO "Too late to register bootconsole %s%d\n",
1612 newcon->name, newcon->index);
1618 if (console_drivers && console_drivers->flags & CON_BOOT)
1619 bcon = console_drivers;
1621 if (preferred_console < 0 || bcon || !console_drivers)
1622 preferred_console = selected_console;
1624 if (newcon->early_setup)
1625 newcon->early_setup();
1628 * See if we want to use this console driver. If we
1629 * didn't select a console we take the first one
1630 * that registers here.
1632 if (preferred_console < 0) {
1633 if (newcon->index < 0)
1635 if (newcon->setup == NULL ||
1636 newcon->setup(newcon, NULL) == 0) {
1637 newcon->flags |= CON_ENABLED;
1638 if (newcon->device) {
1639 newcon->flags |= CON_CONSDEV;
1640 preferred_console = 0;
1646 * See if this console matches one we selected on
1649 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1651 if (strcmp(console_cmdline[i].name, newcon->name) != 0)
1653 if (newcon->index >= 0 &&
1654 newcon->index != console_cmdline[i].index)
1656 if (newcon->index < 0)
1657 newcon->index = console_cmdline[i].index;
1658 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1659 if (console_cmdline[i].brl_options) {
1660 newcon->flags |= CON_BRL;
1661 braille_register_console(newcon,
1662 console_cmdline[i].index,
1663 console_cmdline[i].options,
1664 console_cmdline[i].brl_options);
1668 if (newcon->setup &&
1669 newcon->setup(newcon, console_cmdline[i].options) != 0)
1671 newcon->flags |= CON_ENABLED;
1672 newcon->index = console_cmdline[i].index;
1673 if (i == selected_console) {
1674 newcon->flags |= CON_CONSDEV;
1675 preferred_console = selected_console;
1680 if (!(newcon->flags & CON_ENABLED))
1684 * If we have a bootconsole, and are switching to a real console,
1685 * don't print everything out again, since when the boot console, and
1686 * the real console are the same physical device, it's annoying to
1687 * see the beginning boot messages twice
1689 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
1690 newcon->flags &= ~CON_PRINTBUFFER;
1693 * Put this console in the list - keep the
1694 * preferred driver at the head of the list.
1697 if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
1698 newcon->next = console_drivers;
1699 console_drivers = newcon;
1701 newcon->next->flags &= ~CON_CONSDEV;
1703 newcon->next = console_drivers->next;
1704 console_drivers->next = newcon;
1706 if (newcon->flags & CON_PRINTBUFFER) {
1708 * console_unlock(); will print out the buffered messages
1711 raw_spin_lock_irqsave(&logbuf_lock, flags);
1712 console_seq = syslog_seq;
1713 console_idx = syslog_idx;
1714 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
1716 * We're about to replay the log buffer. Only do this to the
1717 * just-registered console to avoid excessive message spam to
1718 * the already-registered consoles.
1720 exclusive_console = newcon;
1723 console_sysfs_notify();
1726 * By unregistering the bootconsoles after we enable the real console
1727 * we get the "console xxx enabled" message on all the consoles -
1728 * boot consoles, real consoles, etc - this is to ensure that end
1729 * users know there might be something in the kernel's log buffer that
1730 * went to the bootconsole (that they do not see on the real console)
1733 ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
1735 /* we need to iterate through twice, to make sure we print
1736 * everything out, before we unregister the console(s)
1738 printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
1739 newcon->name, newcon->index);
1740 for_each_console(bcon)
1741 if (bcon->flags & CON_BOOT)
1742 unregister_console(bcon);
1744 printk(KERN_INFO "%sconsole [%s%d] enabled\n",
1745 (newcon->flags & CON_BOOT) ? "boot" : "" ,
1746 newcon->name, newcon->index);
1749 EXPORT_SYMBOL(register_console);
1751 int unregister_console(struct console *console)
1753 struct console *a, *b;
1756 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1757 if (console->flags & CON_BRL)
1758 return braille_unregister_console(console);
1762 if (console_drivers == console) {
1763 console_drivers=console->next;
1765 } else if (console_drivers) {
1766 for (a=console_drivers->next, b=console_drivers ;
1767 a; b=a, a=b->next) {
1777 * If this isn't the last console and it has CON_CONSDEV set, we
1778 * need to set it on the next preferred console.
1780 if (console_drivers != NULL && console->flags & CON_CONSDEV)
1781 console_drivers->flags |= CON_CONSDEV;
1784 console_sysfs_notify();
1787 EXPORT_SYMBOL(unregister_console);
1789 static int __init printk_late_init(void)
1791 struct console *con;
1793 for_each_console(con) {
1794 if (!keep_bootcon && con->flags & CON_BOOT) {
1795 printk(KERN_INFO "turn off boot console %s%d\n",
1796 con->name, con->index);
1797 unregister_console(con);
1800 hotcpu_notifier(console_cpu_notify, 0);
1803 late_initcall(printk_late_init);
1805 #if defined CONFIG_PRINTK
1807 int printk_sched(const char *fmt, ...)
1809 unsigned long flags;
1814 local_irq_save(flags);
1815 buf = __get_cpu_var(printk_sched_buf);
1817 va_start(args, fmt);
1818 r = vsnprintf(buf, PRINTK_BUF_SIZE, fmt, args);
1821 __this_cpu_or(printk_pending, PRINTK_PENDING_SCHED);
1822 local_irq_restore(flags);
1828 * printk rate limiting, lifted from the networking subsystem.
1830 * This enforces a rate limit: not more than 10 kernel messages
1831 * every 5s to make a denial-of-service attack impossible.
1833 DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
1835 int __printk_ratelimit(const char *func)
1837 return ___ratelimit(&printk_ratelimit_state, func);
1839 EXPORT_SYMBOL(__printk_ratelimit);
1842 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1843 * @caller_jiffies: pointer to caller's state
1844 * @interval_msecs: minimum interval between prints
1846 * printk_timed_ratelimit() returns true if more than @interval_msecs
1847 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1850 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1851 unsigned int interval_msecs)
1853 if (*caller_jiffies == 0
1854 || !time_in_range(jiffies, *caller_jiffies,
1856 + msecs_to_jiffies(interval_msecs))) {
1857 *caller_jiffies = jiffies;
1862 EXPORT_SYMBOL(printk_timed_ratelimit);
1864 static DEFINE_SPINLOCK(dump_list_lock);
1865 static LIST_HEAD(dump_list);
1868 * kmsg_dump_register - register a kernel log dumper.
1869 * @dumper: pointer to the kmsg_dumper structure
1871 * Adds a kernel log dumper to the system. The dump callback in the
1872 * structure will be called when the kernel oopses or panics and must be
1873 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
1875 int kmsg_dump_register(struct kmsg_dumper *dumper)
1877 unsigned long flags;
1880 /* The dump callback needs to be set */
1884 spin_lock_irqsave(&dump_list_lock, flags);
1885 /* Don't allow registering multiple times */
1886 if (!dumper->registered) {
1887 dumper->registered = 1;
1888 list_add_tail_rcu(&dumper->list, &dump_list);
1891 spin_unlock_irqrestore(&dump_list_lock, flags);
1895 EXPORT_SYMBOL_GPL(kmsg_dump_register);
1898 * kmsg_dump_unregister - unregister a kmsg dumper.
1899 * @dumper: pointer to the kmsg_dumper structure
1901 * Removes a dump device from the system. Returns zero on success and
1902 * %-EINVAL otherwise.
1904 int kmsg_dump_unregister(struct kmsg_dumper *dumper)
1906 unsigned long flags;
1909 spin_lock_irqsave(&dump_list_lock, flags);
1910 if (dumper->registered) {
1911 dumper->registered = 0;
1912 list_del_rcu(&dumper->list);
1915 spin_unlock_irqrestore(&dump_list_lock, flags);
1920 EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
1922 static bool always_kmsg_dump;
1923 module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR);
1926 * kmsg_dump - dump kernel log to kernel message dumpers.
1927 * @reason: the reason (oops, panic etc) for dumping
1929 * Iterate through each of the dump devices and call the oops/panic
1930 * callbacks with the log buffer.
1932 void kmsg_dump(enum kmsg_dump_reason reason)
1935 struct kmsg_dumper *dumper;
1936 const char *s1, *s2;
1937 unsigned long l1, l2;
1938 unsigned long flags;
1940 if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
1943 /* Theoretically, the log could move on after we do this, but
1944 there's not a lot we can do about that. The new messages
1945 will overwrite the start of what we dump. */
1947 raw_spin_lock_irqsave(&logbuf_lock, flags);
1948 if (syslog_seq < log_first_seq)
1951 idx = log_first_idx;
1953 if (idx > log_next_idx) {
1958 l2 = log_buf_len - idx;
1964 l2 = log_next_idx - idx;
1966 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
1969 list_for_each_entry_rcu(dumper, &dump_list, list)
1970 dumper->dump(dumper, reason, s1, l1, s2, l2);