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/syscalls.h>
35 #include <linux/kexec.h>
36 #include <linux/kdb.h>
37 #include <linux/ratelimit.h>
38 #include <linux/kmsg_dump.h>
39 #include <linux/syslog.h>
40 #include <linux/cpu.h>
41 #include <linux/notifier.h>
42 #include <linux/rculist.h>
44 #include <asm/uaccess.h>
47 * Architectures can override it:
49 void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
53 #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
55 #ifdef CONFIG_DEBUG_LL
56 extern void printascii(char *);
59 /* printk's without a loglevel use this.. */
60 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
62 /* We show everything that is MORE important than this.. */
63 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
64 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
66 DECLARE_WAIT_QUEUE_HEAD(log_wait);
68 int console_printk[4] = {
69 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
70 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
71 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
72 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
76 * Low level drivers may need that to know if they can schedule in
77 * their unblank() callback or not. So let's export it.
80 EXPORT_SYMBOL(oops_in_progress);
83 * console_sem protects the console_drivers list, and also
84 * provides serialisation for access to the entire console
87 static DEFINE_SEMAPHORE(console_sem);
88 struct console *console_drivers;
89 EXPORT_SYMBOL_GPL(console_drivers);
92 * This is used for debugging the mess that is the VT code by
93 * keeping track if we have the console semaphore held. It's
94 * definitely not the perfect debug tool (we don't know if _WE_
95 * hold it are racing, but it helps tracking those weird code
96 * path in the console code where we end up in places I want
97 * locked without the console sempahore held
99 static int console_locked, console_suspended;
102 * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
103 * It is also used in interesting ways to provide interlocking in
106 static DEFINE_SPINLOCK(logbuf_lock);
108 #define LOG_BUF_MASK (log_buf_len-1)
109 #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
112 * The indices into log_buf are not constrained to log_buf_len - they
113 * must be masked before subscripting
115 static unsigned log_start; /* Index into log_buf: next char to be read by syslog() */
116 static unsigned con_start; /* Index into log_buf: next char to be sent to consoles */
117 static unsigned log_end; /* Index into log_buf: most-recently-written-char + 1 */
120 * Array of consoles built from command line options (console=)
122 struct console_cmdline
124 char name[8]; /* Name of the driver */
125 int index; /* Minor dev. to use */
126 char *options; /* Options for the driver */
127 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
128 char *brl_options; /* Options for braille driver */
132 #define MAX_CMDLINECONSOLES 8
134 static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
135 static int selected_console = -1;
136 static int preferred_console = -1;
137 int console_set_on_cmdline;
138 EXPORT_SYMBOL(console_set_on_cmdline);
140 /* Flag: console code may call schedule() */
141 static int console_may_schedule;
145 static char __log_buf[__LOG_BUF_LEN];
146 static char *log_buf = __log_buf;
147 static int log_buf_len = __LOG_BUF_LEN;
148 static unsigned logged_chars; /* Number of chars produced since last read+clear operation */
149 static int saved_console_loglevel = -1;
153 * This appends the listed symbols to /proc/vmcoreinfo
155 * /proc/vmcoreinfo is used by various utiilties, like crash and makedumpfile to
156 * obtain access to symbols that are otherwise very difficult to locate. These
157 * symbols are specifically used so that utilities can access and extract the
158 * dmesg log from a vmcore file after a crash.
160 void log_buf_kexec_setup(void)
162 VMCOREINFO_SYMBOL(log_buf);
163 VMCOREINFO_SYMBOL(log_end);
164 VMCOREINFO_SYMBOL(log_buf_len);
165 VMCOREINFO_SYMBOL(logged_chars);
169 static int __init log_buf_len_setup(char *str)
171 unsigned size = memparse(str, &str);
175 size = roundup_pow_of_two(size);
176 if (size > log_buf_len) {
177 unsigned start, dest_idx, offset;
180 new_log_buf = alloc_bootmem(size);
182 printk(KERN_WARNING "log_buf_len: allocation failed\n");
186 spin_lock_irqsave(&logbuf_lock, flags);
188 log_buf = new_log_buf;
190 offset = start = min(con_start, log_start);
192 while (start != log_end) {
193 log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
200 spin_unlock_irqrestore(&logbuf_lock, flags);
202 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
208 __setup("log_buf_len=", log_buf_len_setup);
210 #ifdef CONFIG_BOOT_PRINTK_DELAY
212 static int boot_delay; /* msecs delay after each printk during bootup */
213 static unsigned long long loops_per_msec; /* based on boot_delay */
215 static int __init boot_delay_setup(char *str)
219 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
220 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
222 get_option(&str, &boot_delay);
223 if (boot_delay > 10 * 1000)
226 pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
227 "HZ: %d, loops_per_msec: %llu\n",
228 boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
231 __setup("boot_delay=", boot_delay_setup);
233 static void boot_delay_msec(void)
235 unsigned long long k;
236 unsigned long timeout;
238 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
241 k = (unsigned long long)loops_per_msec * boot_delay;
243 timeout = jiffies + msecs_to_jiffies(boot_delay);
248 * use (volatile) jiffies to prevent
249 * compiler reduction; loop termination via jiffies
250 * is secondary and may or may not happen.
252 if (time_after(jiffies, timeout))
254 touch_nmi_watchdog();
258 static inline void boot_delay_msec(void)
264 * Return the number of unread characters in the log buffer.
266 static int log_buf_get_len(void)
272 * Clears the ring-buffer
274 void log_buf_clear(void)
280 * Copy a range of characters from the log buffer.
282 int log_buf_copy(char *dest, int idx, int len)
285 bool took_lock = false;
287 if (!oops_in_progress) {
288 spin_lock_irq(&logbuf_lock);
292 max = log_buf_get_len();
293 if (idx < 0 || idx >= max) {
299 idx += (log_end - max);
301 dest[len] = LOG_BUF(idx + len);
305 spin_unlock_irq(&logbuf_lock);
310 #ifdef CONFIG_SECURITY_DMESG_RESTRICT
311 int dmesg_restrict = 1;
316 static int syslog_action_restricted(int type)
320 /* Unless restricted, we allow "read all" and "get buffer size" for everybody */
321 return type != SYSLOG_ACTION_READ_ALL && type != SYSLOG_ACTION_SIZE_BUFFER;
324 static int check_syslog_permissions(int type, bool from_file)
327 * If this is from /proc/kmsg and we've already opened it, then we've
328 * already done the capabilities checks at open time.
330 if (from_file && type != SYSLOG_ACTION_OPEN)
333 if (syslog_action_restricted(type)) {
334 if (capable(CAP_SYSLOG))
336 /* For historical reasons, accept CAP_SYS_ADMIN too, with a warning */
337 if (capable(CAP_SYS_ADMIN)) {
338 WARN_ONCE(1, "Attempt to access syslog with CAP_SYS_ADMIN "
339 "but no CAP_SYSLOG (deprecated).\n");
347 int do_syslog(int type, char __user *buf, int len, bool from_file)
349 unsigned i, j, limit, count;
354 error = check_syslog_permissions(type, from_file);
358 error = security_syslog(type);
363 case SYSLOG_ACTION_CLOSE: /* Close log */
365 case SYSLOG_ACTION_OPEN: /* Open log */
367 case SYSLOG_ACTION_READ: /* Read from log */
374 if (!access_ok(VERIFY_WRITE, buf, len)) {
378 error = wait_event_interruptible(log_wait,
379 (log_start - log_end));
383 spin_lock_irq(&logbuf_lock);
384 while (!error && (log_start != log_end) && i < len) {
385 c = LOG_BUF(log_start);
387 spin_unlock_irq(&logbuf_lock);
388 error = __put_user(c,buf);
392 spin_lock_irq(&logbuf_lock);
394 spin_unlock_irq(&logbuf_lock);
398 /* Read/clear last kernel messages */
399 case SYSLOG_ACTION_READ_CLEAR:
402 /* Read last kernel messages */
403 case SYSLOG_ACTION_READ_ALL:
410 if (!access_ok(VERIFY_WRITE, buf, len)) {
415 if (count > log_buf_len)
417 spin_lock_irq(&logbuf_lock);
418 if (count > logged_chars)
419 count = logged_chars;
424 * __put_user() could sleep, and while we sleep
425 * printk() could overwrite the messages
426 * we try to copy to user space. Therefore
427 * the messages are copied in reverse. <manfreds>
429 for (i = 0; i < count && !error; i++) {
431 if (j + log_buf_len < log_end)
434 spin_unlock_irq(&logbuf_lock);
435 error = __put_user(c,&buf[count-1-i]);
437 spin_lock_irq(&logbuf_lock);
439 spin_unlock_irq(&logbuf_lock);
444 int offset = count-error;
445 /* buffer overflow during copy, correct user buffer. */
446 for (i = 0; i < error; i++) {
447 if (__get_user(c,&buf[i+offset]) ||
448 __put_user(c,&buf[i])) {
456 /* Clear ring buffer */
457 case SYSLOG_ACTION_CLEAR:
460 /* Disable logging to console */
461 case SYSLOG_ACTION_CONSOLE_OFF:
462 if (saved_console_loglevel == -1)
463 saved_console_loglevel = console_loglevel;
464 console_loglevel = minimum_console_loglevel;
466 /* Enable logging to console */
467 case SYSLOG_ACTION_CONSOLE_ON:
468 if (saved_console_loglevel != -1) {
469 console_loglevel = saved_console_loglevel;
470 saved_console_loglevel = -1;
473 /* Set level of messages printed to console */
474 case SYSLOG_ACTION_CONSOLE_LEVEL:
476 if (len < 1 || len > 8)
478 if (len < minimum_console_loglevel)
479 len = minimum_console_loglevel;
480 console_loglevel = len;
481 /* Implicitly re-enable logging to console */
482 saved_console_loglevel = -1;
485 /* Number of chars in the log buffer */
486 case SYSLOG_ACTION_SIZE_UNREAD:
487 error = log_end - log_start;
489 /* Size of the log buffer */
490 case SYSLOG_ACTION_SIZE_BUFFER:
501 SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
503 return do_syslog(type, buf, len, SYSLOG_FROM_CALL);
506 #ifdef CONFIG_KGDB_KDB
507 /* kdb dmesg command needs access to the syslog buffer. do_syslog()
508 * uses locks so it cannot be used during debugging. Just tell kdb
509 * where the start and end of the physical and logical logs are. This
510 * is equivalent to do_syslog(3).
512 void kdb_syslog_data(char *syslog_data[4])
514 syslog_data[0] = log_buf;
515 syslog_data[1] = log_buf + log_buf_len;
516 syslog_data[2] = log_buf + log_end -
517 (logged_chars < log_buf_len ? logged_chars : log_buf_len);
518 syslog_data[3] = log_buf + log_end;
520 #endif /* CONFIG_KGDB_KDB */
523 * Call the console drivers on a range of log_buf
525 static void __call_console_drivers(unsigned start, unsigned end)
529 for_each_console(con) {
530 if ((con->flags & CON_ENABLED) && con->write &&
531 (cpu_online(smp_processor_id()) ||
532 (con->flags & CON_ANYTIME)))
533 con->write(con, &LOG_BUF(start), end - start);
537 static int __read_mostly ignore_loglevel;
539 static int __init ignore_loglevel_setup(char *str)
542 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
547 early_param("ignore_loglevel", ignore_loglevel_setup);
550 * Write out chars from start to end - 1 inclusive
552 static void _call_console_drivers(unsigned start,
553 unsigned end, int msg_log_level)
555 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
556 console_drivers && start != end) {
557 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
559 __call_console_drivers(start & LOG_BUF_MASK,
561 __call_console_drivers(0, end & LOG_BUF_MASK);
563 __call_console_drivers(start, end);
569 * Call the console drivers, asking them to write out
570 * log_buf[start] to log_buf[end - 1].
571 * The console_lock must be held.
573 static void call_console_drivers(unsigned start, unsigned end)
575 unsigned cur_index, start_print;
576 static int msg_level = -1;
578 BUG_ON(((int)(start - end)) > 0);
582 while (cur_index != end) {
583 if (msg_level < 0 && ((end - cur_index) > 2) &&
584 LOG_BUF(cur_index + 0) == '<' &&
585 LOG_BUF(cur_index + 1) >= '0' &&
586 LOG_BUF(cur_index + 1) <= '7' &&
587 LOG_BUF(cur_index + 2) == '>') {
588 msg_level = LOG_BUF(cur_index + 1) - '0';
590 start_print = cur_index;
592 while (cur_index != end) {
593 char c = LOG_BUF(cur_index);
599 * printk() has already given us loglevel tags in
600 * the buffer. This code is here in case the
601 * log buffer has wrapped right round and scribbled
604 msg_level = default_message_loglevel;
606 _call_console_drivers(start_print, cur_index, msg_level);
608 start_print = cur_index;
613 _call_console_drivers(start_print, end, msg_level);
616 static void emit_log_char(char c)
618 LOG_BUF(log_end) = c;
620 if (log_end - log_start > log_buf_len)
621 log_start = log_end - log_buf_len;
622 if (log_end - con_start > log_buf_len)
623 con_start = log_end - log_buf_len;
624 if (logged_chars < log_buf_len)
629 * Zap console related locks when oopsing. Only zap at most once
630 * every 10 seconds, to leave time for slow consoles to print a
633 static void zap_locks(void)
635 static unsigned long oops_timestamp;
637 if (time_after_eq(jiffies, oops_timestamp) &&
638 !time_after(jiffies, oops_timestamp + 30 * HZ))
641 oops_timestamp = jiffies;
643 /* If a crash is occurring, make sure we can't deadlock */
644 spin_lock_init(&logbuf_lock);
645 /* And make sure that we print immediately */
646 sema_init(&console_sem, 1);
649 #if defined(CONFIG_PRINTK_TIME)
650 static int printk_time = 1;
652 static int printk_time = 0;
654 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
656 /* Check if we have any console registered that can be called early in boot. */
657 static int have_callable_console(void)
661 for_each_console(con)
662 if (con->flags & CON_ANYTIME)
669 * printk - print a kernel message
670 * @fmt: format string
672 * This is printk(). It can be called from any context. We want it to work.
674 * We try to grab the console_lock. If we succeed, it's easy - we log the output and
675 * call the console drivers. If we fail to get the semaphore we place the output
676 * into the log buffer and return. The current holder of the console_sem will
677 * notice the new output in console_unlock(); and will send it to the
678 * consoles before releasing the lock.
680 * One effect of this deferred printing is that code which calls printk() and
681 * then changes console_loglevel may break. This is because console_loglevel
682 * is inspected when the actual printing occurs.
687 * See the vsnprintf() documentation for format string extensions over C99.
690 asmlinkage int printk(const char *fmt, ...)
695 #ifdef CONFIG_KGDB_KDB
696 if (unlikely(kdb_trap_printk)) {
698 r = vkdb_printf(fmt, args);
704 r = vprintk(fmt, args);
710 /* cpu currently holding logbuf_lock */
711 static volatile unsigned int printk_cpu = UINT_MAX;
714 * Can we actually use the console at this time on this cpu?
716 * Console drivers may assume that per-cpu resources have
717 * been allocated. So unless they're explicitly marked as
718 * being able to cope (CON_ANYTIME) don't call them until
719 * this CPU is officially up.
721 static inline int can_use_console(unsigned int cpu)
723 return cpu_online(cpu) || have_callable_console();
727 * Try to get console ownership to actually show the kernel
728 * messages from a 'printk'. Return true (and with the
729 * console_lock held, and 'console_locked' set) if it
730 * is successful, false otherwise.
732 * This gets called with the 'logbuf_lock' spinlock held and
733 * interrupts disabled. It should return with 'lockbuf_lock'
734 * released but interrupts still disabled.
736 static int console_trylock_for_printk(unsigned int cpu)
737 __releases(&logbuf_lock)
741 if (console_trylock()) {
745 * If we can't use the console, we need to release
746 * the console semaphore by hand to avoid flushing
747 * the buffer. We need to hold the console semaphore
748 * in order to do this test safely.
750 if (!can_use_console(cpu)) {
756 printk_cpu = UINT_MAX;
757 spin_unlock(&logbuf_lock);
760 static const char recursion_bug_msg [] =
761 KERN_CRIT "BUG: recent printk recursion!\n";
762 static int recursion_bug;
763 static int new_text_line = 1;
764 static char printk_buf[1024];
766 int printk_delay_msec __read_mostly;
768 static inline void printk_delay(void)
770 if (unlikely(printk_delay_msec)) {
771 int m = printk_delay_msec;
775 touch_nmi_watchdog();
780 asmlinkage int vprintk(const char *fmt, va_list args)
783 int current_log_level = default_message_loglevel;
792 /* This stops the holder of console_sem just where we want him */
793 raw_local_irq_save(flags);
794 this_cpu = smp_processor_id();
797 * Ouch, printk recursed into itself!
799 if (unlikely(printk_cpu == this_cpu)) {
801 * If a crash is occurring during printk() on this CPU,
802 * then try to get the crash message out but make sure
803 * we can't deadlock. Otherwise just return to avoid the
804 * recursion and return - but flag the recursion so that
805 * it can be printed at the next appropriate moment:
807 if (!oops_in_progress) {
809 goto out_restore_irqs;
815 spin_lock(&logbuf_lock);
816 printk_cpu = this_cpu;
820 strcpy(printk_buf, recursion_bug_msg);
821 printed_len = strlen(recursion_bug_msg);
823 /* Emit the output into the temporary buffer */
824 printed_len += vscnprintf(printk_buf + printed_len,
825 sizeof(printk_buf) - printed_len, fmt, args);
827 #ifdef CONFIG_DEBUG_LL
828 printascii(printk_buf);
833 /* Do we have a loglevel in the string? */
835 unsigned char c = p[1];
836 if (c && p[2] == '>') {
838 case '0' ... '7': /* loglevel */
839 current_log_level = c - '0';
840 /* Fallthrough - make sure we're on a new line */
841 case 'd': /* KERN_DEFAULT */
842 if (!new_text_line) {
846 /* Fallthrough - skip the loglevel */
847 case 'c': /* KERN_CONT */
855 * Copy the output into log_buf. If the caller didn't provide
856 * appropriate log level tags, we insert them here
860 /* Always output the token */
862 emit_log_char(current_log_level + '0');
868 /* Follow the token with the time */
871 unsigned long long t;
872 unsigned long nanosec_rem;
874 t = cpu_clock(printk_cpu);
875 nanosec_rem = do_div(t, 1000000000);
876 tlen = sprintf(tbuf, "[%5lu.%06lu] ",
880 for (tp = tbuf; tp < tbuf + tlen; tp++)
895 * Try to acquire and then immediately release the
896 * console semaphore. The release will do all the
897 * actual magic (print out buffers, wake up klogd,
900 * The console_trylock_for_printk() function
901 * will release 'logbuf_lock' regardless of whether it
902 * actually gets the semaphore or not.
904 if (console_trylock_for_printk(this_cpu))
909 raw_local_irq_restore(flags);
914 EXPORT_SYMBOL(printk);
915 EXPORT_SYMBOL(vprintk);
919 static void call_console_drivers(unsigned start, unsigned end)
925 static int __add_preferred_console(char *name, int idx, char *options,
928 struct console_cmdline *c;
932 * See if this tty is not yet registered, and
933 * if we have a slot free.
935 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
936 if (strcmp(console_cmdline[i].name, name) == 0 &&
937 console_cmdline[i].index == idx) {
939 selected_console = i;
942 if (i == MAX_CMDLINECONSOLES)
945 selected_console = i;
946 c = &console_cmdline[i];
947 strlcpy(c->name, name, sizeof(c->name));
948 c->options = options;
949 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
950 c->brl_options = brl_options;
956 * Set up a list of consoles. Called from init/main.c
958 static int __init console_setup(char *str)
960 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
961 char *s, *options, *brl_options = NULL;
964 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
965 if (!memcmp(str, "brl,", 4)) {
968 } else if (!memcmp(str, "brl=", 4)) {
969 brl_options = str + 4;
970 str = strchr(brl_options, ',');
972 printk(KERN_ERR "need port name after brl=\n");
980 * Decode str into name, index, options.
982 if (str[0] >= '0' && str[0] <= '9') {
984 strncpy(buf + 4, str, sizeof(buf) - 5);
986 strncpy(buf, str, sizeof(buf) - 1);
988 buf[sizeof(buf) - 1] = 0;
989 if ((options = strchr(str, ',')) != NULL)
992 if (!strcmp(str, "ttya"))
993 strcpy(buf, "ttyS0");
994 if (!strcmp(str, "ttyb"))
995 strcpy(buf, "ttyS1");
997 for (s = buf; *s; s++)
998 if ((*s >= '0' && *s <= '9') || *s == ',')
1000 idx = simple_strtoul(s, NULL, 10);
1003 __add_preferred_console(buf, idx, options, brl_options);
1004 console_set_on_cmdline = 1;
1007 __setup("console=", console_setup);
1010 * add_preferred_console - add a device to the list of preferred consoles.
1011 * @name: device name
1012 * @idx: device index
1013 * @options: options for this console
1015 * The last preferred console added will be used for kernel messages
1016 * and stdin/out/err for init. Normally this is used by console_setup
1017 * above to handle user-supplied console arguments; however it can also
1018 * be used by arch-specific code either to override the user or more
1019 * commonly to provide a default console (ie from PROM variables) when
1020 * the user has not supplied one.
1022 int add_preferred_console(char *name, int idx, char *options)
1024 return __add_preferred_console(name, idx, options, NULL);
1027 int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
1029 struct console_cmdline *c;
1032 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
1033 if (strcmp(console_cmdline[i].name, name) == 0 &&
1034 console_cmdline[i].index == idx) {
1035 c = &console_cmdline[i];
1036 strlcpy(c->name, name_new, sizeof(c->name));
1037 c->name[sizeof(c->name) - 1] = 0;
1038 c->options = options;
1046 int console_suspend_enabled = 1;
1047 EXPORT_SYMBOL(console_suspend_enabled);
1049 static int __init console_suspend_disable(char *str)
1051 console_suspend_enabled = 0;
1054 __setup("no_console_suspend", console_suspend_disable);
1057 * suspend_console - suspend the console subsystem
1059 * This disables printk() while we go into suspend states
1061 void suspend_console(void)
1063 if (!console_suspend_enabled)
1065 printk("Suspending console(s) (use no_console_suspend to debug)\n");
1067 console_suspended = 1;
1071 void resume_console(void)
1073 if (!console_suspend_enabled)
1076 console_suspended = 0;
1081 * console_cpu_notify - print deferred console messages after CPU hotplug
1082 * @self: notifier struct
1083 * @action: CPU hotplug event
1086 * If printk() is called from a CPU that is not online yet, the messages
1087 * will be spooled but will not show up on the console. This function is
1088 * called when a new CPU comes online (or fails to come up), and ensures
1089 * that any such output gets printed.
1091 static int __cpuinit console_cpu_notify(struct notifier_block *self,
1092 unsigned long action, void *hcpu)
1098 case CPU_DOWN_FAILED:
1099 case CPU_UP_CANCELED:
1107 * console_lock - lock the console system for exclusive use.
1109 * Acquires a lock which guarantees that the caller has
1110 * exclusive access to the console system and the console_drivers list.
1112 * Can sleep, returns nothing.
1114 void console_lock(void)
1116 BUG_ON(in_interrupt());
1118 if (console_suspended)
1121 console_may_schedule = 1;
1123 EXPORT_SYMBOL(console_lock);
1126 * console_trylock - try to lock the console system for exclusive use.
1128 * Tried to acquire a lock which guarantees that the caller has
1129 * exclusive access to the console system and the console_drivers list.
1131 * returns 1 on success, and 0 on failure to acquire the lock.
1133 int console_trylock(void)
1135 if (down_trylock(&console_sem))
1137 if (console_suspended) {
1142 console_may_schedule = 0;
1145 EXPORT_SYMBOL(console_trylock);
1147 int is_console_locked(void)
1149 return console_locked;
1152 static DEFINE_PER_CPU(int, printk_pending);
1154 void printk_tick(void)
1156 if (__this_cpu_read(printk_pending)) {
1157 __this_cpu_write(printk_pending, 0);
1158 wake_up_interruptible(&log_wait);
1162 int printk_needs_cpu(int cpu)
1164 if (cpu_is_offline(cpu))
1166 return __this_cpu_read(printk_pending);
1169 void wake_up_klogd(void)
1171 if (waitqueue_active(&log_wait))
1172 this_cpu_write(printk_pending, 1);
1176 * console_unlock - unlock the console system
1178 * Releases the console_lock which the caller holds on the console system
1179 * and the console driver list.
1181 * While the console_lock was held, console output may have been buffered
1182 * by printk(). If this is the case, console_unlock(); emits
1183 * the output prior to releasing the lock.
1185 * If there is output waiting for klogd, we wake it up.
1187 * console_unlock(); may be called from any context.
1189 void console_unlock(void)
1191 unsigned long flags;
1192 unsigned _con_start, _log_end;
1193 unsigned wake_klogd = 0;
1195 if (console_suspended) {
1200 console_may_schedule = 0;
1203 spin_lock_irqsave(&logbuf_lock, flags);
1204 wake_klogd |= log_start - log_end;
1205 if (con_start == log_end)
1206 break; /* Nothing to print */
1207 _con_start = con_start;
1209 con_start = log_end; /* Flush */
1210 spin_unlock(&logbuf_lock);
1211 stop_critical_timings(); /* don't trace print latency */
1212 call_console_drivers(_con_start, _log_end);
1213 start_critical_timings();
1214 local_irq_restore(flags);
1218 spin_unlock_irqrestore(&logbuf_lock, flags);
1222 EXPORT_SYMBOL(console_unlock);
1225 * console_conditional_schedule - yield the CPU if required
1227 * If the console code is currently allowed to sleep, and
1228 * if this CPU should yield the CPU to another task, do
1231 * Must be called within console_lock();.
1233 void __sched console_conditional_schedule(void)
1235 if (console_may_schedule)
1238 EXPORT_SYMBOL(console_conditional_schedule);
1240 void console_unblank(void)
1245 * console_unblank can no longer be called in interrupt context unless
1246 * oops_in_progress is set to 1..
1248 if (oops_in_progress) {
1249 if (down_trylock(&console_sem) != 0)
1255 console_may_schedule = 0;
1257 if ((c->flags & CON_ENABLED) && c->unblank)
1263 * Return the console tty driver structure and its associated index
1265 struct tty_driver *console_device(int *index)
1268 struct tty_driver *driver = NULL;
1271 for_each_console(c) {
1274 driver = c->device(c, index);
1283 * Prevent further output on the passed console device so that (for example)
1284 * serial drivers can disable console output before suspending a port, and can
1285 * re-enable output afterwards.
1287 void console_stop(struct console *console)
1290 console->flags &= ~CON_ENABLED;
1293 EXPORT_SYMBOL(console_stop);
1295 void console_start(struct console *console)
1298 console->flags |= CON_ENABLED;
1301 EXPORT_SYMBOL(console_start);
1304 * The console driver calls this routine during kernel initialization
1305 * to register the console printing procedure with printk() and to
1306 * print any messages that were printed by the kernel before the
1307 * console driver was initialized.
1309 * This can happen pretty early during the boot process (because of
1310 * early_printk) - sometimes before setup_arch() completes - be careful
1311 * of what kernel features are used - they may not be initialised yet.
1313 * There are two types of consoles - bootconsoles (early_printk) and
1314 * "real" consoles (everything which is not a bootconsole) which are
1315 * handled differently.
1316 * - Any number of bootconsoles can be registered at any time.
1317 * - As soon as a "real" console is registered, all bootconsoles
1318 * will be unregistered automatically.
1319 * - Once a "real" console is registered, any attempt to register a
1320 * bootconsoles will be rejected
1322 void register_console(struct console *newcon)
1325 unsigned long flags;
1326 struct console *bcon = NULL;
1329 * before we register a new CON_BOOT console, make sure we don't
1330 * already have a valid console
1332 if (console_drivers && newcon->flags & CON_BOOT) {
1333 /* find the last or real console */
1334 for_each_console(bcon) {
1335 if (!(bcon->flags & CON_BOOT)) {
1336 printk(KERN_INFO "Too late to register bootconsole %s%d\n",
1337 newcon->name, newcon->index);
1343 if (console_drivers && console_drivers->flags & CON_BOOT)
1344 bcon = console_drivers;
1346 if (preferred_console < 0 || bcon || !console_drivers)
1347 preferred_console = selected_console;
1349 if (newcon->early_setup)
1350 newcon->early_setup();
1353 * See if we want to use this console driver. If we
1354 * didn't select a console we take the first one
1355 * that registers here.
1357 if (preferred_console < 0) {
1358 if (newcon->index < 0)
1360 if (newcon->setup == NULL ||
1361 newcon->setup(newcon, NULL) == 0) {
1362 newcon->flags |= CON_ENABLED;
1363 if (newcon->device) {
1364 newcon->flags |= CON_CONSDEV;
1365 preferred_console = 0;
1371 * See if this console matches one we selected on
1374 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1376 if (strcmp(console_cmdline[i].name, newcon->name) != 0)
1378 if (newcon->index >= 0 &&
1379 newcon->index != console_cmdline[i].index)
1381 if (newcon->index < 0)
1382 newcon->index = console_cmdline[i].index;
1383 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1384 if (console_cmdline[i].brl_options) {
1385 newcon->flags |= CON_BRL;
1386 braille_register_console(newcon,
1387 console_cmdline[i].index,
1388 console_cmdline[i].options,
1389 console_cmdline[i].brl_options);
1393 if (newcon->setup &&
1394 newcon->setup(newcon, console_cmdline[i].options) != 0)
1396 newcon->flags |= CON_ENABLED;
1397 newcon->index = console_cmdline[i].index;
1398 if (i == selected_console) {
1399 newcon->flags |= CON_CONSDEV;
1400 preferred_console = selected_console;
1405 if (!(newcon->flags & CON_ENABLED))
1409 * If we have a bootconsole, and are switching to a real console,
1410 * don't print everything out again, since when the boot console, and
1411 * the real console are the same physical device, it's annoying to
1412 * see the beginning boot messages twice
1414 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
1415 newcon->flags &= ~CON_PRINTBUFFER;
1418 * Put this console in the list - keep the
1419 * preferred driver at the head of the list.
1422 if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
1423 newcon->next = console_drivers;
1424 console_drivers = newcon;
1426 newcon->next->flags &= ~CON_CONSDEV;
1428 newcon->next = console_drivers->next;
1429 console_drivers->next = newcon;
1431 if (newcon->flags & CON_PRINTBUFFER) {
1433 * console_unlock(); will print out the buffered messages
1436 spin_lock_irqsave(&logbuf_lock, flags);
1437 con_start = log_start;
1438 spin_unlock_irqrestore(&logbuf_lock, flags);
1441 console_sysfs_notify();
1444 * By unregistering the bootconsoles after we enable the real console
1445 * we get the "console xxx enabled" message on all the consoles -
1446 * boot consoles, real consoles, etc - this is to ensure that end
1447 * users know there might be something in the kernel's log buffer that
1448 * went to the bootconsole (that they do not see on the real console)
1450 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV)) {
1451 /* we need to iterate through twice, to make sure we print
1452 * everything out, before we unregister the console(s)
1454 printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
1455 newcon->name, newcon->index);
1456 for_each_console(bcon)
1457 if (bcon->flags & CON_BOOT)
1458 unregister_console(bcon);
1460 printk(KERN_INFO "%sconsole [%s%d] enabled\n",
1461 (newcon->flags & CON_BOOT) ? "boot" : "" ,
1462 newcon->name, newcon->index);
1465 EXPORT_SYMBOL(register_console);
1467 int unregister_console(struct console *console)
1469 struct console *a, *b;
1472 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1473 if (console->flags & CON_BRL)
1474 return braille_unregister_console(console);
1478 if (console_drivers == console) {
1479 console_drivers=console->next;
1481 } else if (console_drivers) {
1482 for (a=console_drivers->next, b=console_drivers ;
1483 a; b=a, a=b->next) {
1493 * If this isn't the last console and it has CON_CONSDEV set, we
1494 * need to set it on the next preferred console.
1496 if (console_drivers != NULL && console->flags & CON_CONSDEV)
1497 console_drivers->flags |= CON_CONSDEV;
1500 console_sysfs_notify();
1503 EXPORT_SYMBOL(unregister_console);
1505 static int __init printk_late_init(void)
1507 struct console *con;
1509 for_each_console(con) {
1510 if (con->flags & CON_BOOT) {
1511 printk(KERN_INFO "turn off boot console %s%d\n",
1512 con->name, con->index);
1513 unregister_console(con);
1516 hotcpu_notifier(console_cpu_notify, 0);
1519 late_initcall(printk_late_init);
1521 #if defined CONFIG_PRINTK
1524 * printk rate limiting, lifted from the networking subsystem.
1526 * This enforces a rate limit: not more than 10 kernel messages
1527 * every 5s to make a denial-of-service attack impossible.
1529 DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
1531 int __printk_ratelimit(const char *func)
1533 return ___ratelimit(&printk_ratelimit_state, func);
1535 EXPORT_SYMBOL(__printk_ratelimit);
1538 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1539 * @caller_jiffies: pointer to caller's state
1540 * @interval_msecs: minimum interval between prints
1542 * printk_timed_ratelimit() returns true if more than @interval_msecs
1543 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1546 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1547 unsigned int interval_msecs)
1549 if (*caller_jiffies == 0
1550 || !time_in_range(jiffies, *caller_jiffies,
1552 + msecs_to_jiffies(interval_msecs))) {
1553 *caller_jiffies = jiffies;
1558 EXPORT_SYMBOL(printk_timed_ratelimit);
1560 static DEFINE_SPINLOCK(dump_list_lock);
1561 static LIST_HEAD(dump_list);
1564 * kmsg_dump_register - register a kernel log dumper.
1565 * @dumper: pointer to the kmsg_dumper structure
1567 * Adds a kernel log dumper to the system. The dump callback in the
1568 * structure will be called when the kernel oopses or panics and must be
1569 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
1571 int kmsg_dump_register(struct kmsg_dumper *dumper)
1573 unsigned long flags;
1576 /* The dump callback needs to be set */
1580 spin_lock_irqsave(&dump_list_lock, flags);
1581 /* Don't allow registering multiple times */
1582 if (!dumper->registered) {
1583 dumper->registered = 1;
1584 list_add_tail_rcu(&dumper->list, &dump_list);
1587 spin_unlock_irqrestore(&dump_list_lock, flags);
1591 EXPORT_SYMBOL_GPL(kmsg_dump_register);
1594 * kmsg_dump_unregister - unregister a kmsg dumper.
1595 * @dumper: pointer to the kmsg_dumper structure
1597 * Removes a dump device from the system. Returns zero on success and
1598 * %-EINVAL otherwise.
1600 int kmsg_dump_unregister(struct kmsg_dumper *dumper)
1602 unsigned long flags;
1605 spin_lock_irqsave(&dump_list_lock, flags);
1606 if (dumper->registered) {
1607 dumper->registered = 0;
1608 list_del_rcu(&dumper->list);
1611 spin_unlock_irqrestore(&dump_list_lock, flags);
1616 EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
1619 * kmsg_dump - dump kernel log to kernel message dumpers.
1620 * @reason: the reason (oops, panic etc) for dumping
1622 * Iterate through each of the dump devices and call the oops/panic
1623 * callbacks with the log buffer.
1625 void kmsg_dump(enum kmsg_dump_reason reason)
1629 struct kmsg_dumper *dumper;
1630 const char *s1, *s2;
1631 unsigned long l1, l2;
1632 unsigned long flags;
1634 /* Theoretically, the log could move on after we do this, but
1635 there's not a lot we can do about that. The new messages
1636 will overwrite the start of what we dump. */
1637 spin_lock_irqsave(&logbuf_lock, flags);
1638 end = log_end & LOG_BUF_MASK;
1639 chars = logged_chars;
1640 spin_unlock_irqrestore(&logbuf_lock, flags);
1643 s1 = log_buf + log_buf_len - chars + end;
1652 s2 = log_buf + end - chars;
1657 list_for_each_entry_rcu(dumper, &dump_list, list)
1658 dumper->dump(dumper, reason, s1, l1, s2, l2);