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>
37 #include <asm/uaccess.h>
40 * for_each_console() allows you to iterate on each console
42 #define for_each_console(con) \
43 for (con = console_drivers; con != NULL; con = con->next)
46 * Architectures can override it:
48 void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
52 #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
54 #ifdef CONFIG_DEBUG_LL
55 extern void printascii(char *);
58 /* printk's without a loglevel use this.. */
59 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
61 /* We show everything that is MORE important than this.. */
62 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
63 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
65 DECLARE_WAIT_QUEUE_HEAD(log_wait);
67 int console_printk[4] = {
68 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
69 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
70 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
71 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
74 static int saved_console_loglevel = -1;
77 * Low level drivers may need that to know if they can schedule in
78 * their unblank() callback or not. So let's export it.
81 EXPORT_SYMBOL(oops_in_progress);
84 * console_sem protects the console_drivers list, and also
85 * provides serialisation for access to the entire console
88 static DECLARE_MUTEX(console_sem);
89 struct console *console_drivers;
90 EXPORT_SYMBOL_GPL(console_drivers);
93 * This is used for debugging the mess that is the VT code by
94 * keeping track if we have the console semaphore held. It's
95 * definitely not the perfect debug tool (we don't know if _WE_
96 * hold it are racing, but it helps tracking those weird code
97 * path in the console code where we end up in places I want
98 * locked without the console sempahore held
100 static int console_locked, console_suspended;
103 * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
104 * It is also used in interesting ways to provide interlocking in
105 * release_console_sem().
107 static DEFINE_SPINLOCK(logbuf_lock);
109 #define LOG_BUF_MASK (log_buf_len-1)
110 #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
113 * The indices into log_buf are not constrained to log_buf_len - they
114 * must be masked before subscripting
116 static unsigned log_start; /* Index into log_buf: next char to be read by syslog() */
117 static unsigned con_start; /* Index into log_buf: next char to be sent to consoles */
118 static unsigned log_end; /* Index into log_buf: most-recently-written-char + 1 */
121 * Array of consoles built from command line options (console=)
123 struct console_cmdline
125 char name[8]; /* Name of the driver */
126 int index; /* Minor dev. to use */
127 char *options; /* Options for the driver */
128 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
129 char *brl_options; /* Options for braille driver */
133 #define MAX_CMDLINECONSOLES 8
135 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;
146 static char __log_buf[__LOG_BUF_LEN];
147 static char *log_buf = __log_buf;
148 static int log_buf_len = __LOG_BUF_LEN;
149 static unsigned logged_chars; /* Number of chars produced since last read+clear operation */
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 unsigned 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);
311 * Commands to do_syslog:
313 * 0 -- Close the log. Currently a NOP.
314 * 1 -- Open the log. Currently a NOP.
315 * 2 -- Read from the log.
316 * 3 -- Read all messages remaining in the ring buffer.
317 * 4 -- Read and clear all messages remaining in the ring buffer
318 * 5 -- Clear ring buffer.
319 * 6 -- Disable printk's to console
320 * 7 -- Enable printk's to console
321 * 8 -- Set level of messages printed to console
322 * 9 -- Return number of unread characters in the log buffer
323 * 10 -- Return size of the log buffer
325 int do_syslog(int type, char __user *buf, int len)
327 unsigned i, j, limit, count;
332 error = security_syslog(type);
337 case 0: /* Close log */
339 case 1: /* Open log */
341 case 2: /* Read from log */
348 if (!access_ok(VERIFY_WRITE, buf, len)) {
352 error = wait_event_interruptible(log_wait,
353 (log_start - log_end));
357 spin_lock_irq(&logbuf_lock);
358 while (!error && (log_start != log_end) && i < len) {
359 c = LOG_BUF(log_start);
361 spin_unlock_irq(&logbuf_lock);
362 error = __put_user(c,buf);
366 spin_lock_irq(&logbuf_lock);
368 spin_unlock_irq(&logbuf_lock);
372 case 4: /* Read/clear last kernel messages */
375 case 3: /* Read last kernel messages */
382 if (!access_ok(VERIFY_WRITE, buf, len)) {
387 if (count > log_buf_len)
389 spin_lock_irq(&logbuf_lock);
390 if (count > logged_chars)
391 count = logged_chars;
396 * __put_user() could sleep, and while we sleep
397 * printk() could overwrite the messages
398 * we try to copy to user space. Therefore
399 * the messages are copied in reverse. <manfreds>
401 for (i = 0; i < count && !error; i++) {
403 if (j + log_buf_len < log_end)
406 spin_unlock_irq(&logbuf_lock);
407 error = __put_user(c,&buf[count-1-i]);
409 spin_lock_irq(&logbuf_lock);
411 spin_unlock_irq(&logbuf_lock);
416 int offset = count-error;
417 /* buffer overflow during copy, correct user buffer. */
418 for (i = 0; i < error; i++) {
419 if (__get_user(c,&buf[i+offset]) ||
420 __put_user(c,&buf[i])) {
428 case 5: /* Clear ring buffer */
431 case 6: /* Disable logging to console */
432 if (saved_console_loglevel == -1)
433 saved_console_loglevel = console_loglevel;
434 console_loglevel = minimum_console_loglevel;
436 case 7: /* Enable logging to console */
437 if (saved_console_loglevel != -1) {
438 console_loglevel = saved_console_loglevel;
439 saved_console_loglevel = -1;
442 case 8: /* Set level of messages printed to console */
444 if (len < 1 || len > 8)
446 if (len < minimum_console_loglevel)
447 len = minimum_console_loglevel;
448 console_loglevel = len;
449 /* Implicitly re-enable logging to console */
450 saved_console_loglevel = -1;
453 case 9: /* Number of chars in the log buffer */
454 error = log_end - log_start;
456 case 10: /* Size of the log buffer */
467 SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
469 return do_syslog(type, buf, len);
473 * Call the console drivers on a range of log_buf
475 static void __call_console_drivers(unsigned start, unsigned end)
479 for_each_console(con) {
480 if ((con->flags & CON_ENABLED) && con->write &&
481 (cpu_online(smp_processor_id()) ||
482 (con->flags & CON_ANYTIME)))
483 con->write(con, &LOG_BUF(start), end - start);
487 static int __read_mostly ignore_loglevel;
489 static int __init ignore_loglevel_setup(char *str)
492 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
497 early_param("ignore_loglevel", ignore_loglevel_setup);
500 * Write out chars from start to end - 1 inclusive
502 static void _call_console_drivers(unsigned start,
503 unsigned end, int msg_log_level)
505 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
506 console_drivers && start != end) {
507 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
509 __call_console_drivers(start & LOG_BUF_MASK,
511 __call_console_drivers(0, end & LOG_BUF_MASK);
513 __call_console_drivers(start, end);
519 * Call the console drivers, asking them to write out
520 * log_buf[start] to log_buf[end - 1].
521 * The console_sem must be held.
523 static void call_console_drivers(unsigned start, unsigned end)
525 unsigned cur_index, start_print;
526 static int msg_level = -1;
528 BUG_ON(((int)(start - end)) > 0);
532 while (cur_index != end) {
533 if (msg_level < 0 && ((end - cur_index) > 2) &&
534 LOG_BUF(cur_index + 0) == '<' &&
535 LOG_BUF(cur_index + 1) >= '0' &&
536 LOG_BUF(cur_index + 1) <= '7' &&
537 LOG_BUF(cur_index + 2) == '>') {
538 msg_level = LOG_BUF(cur_index + 1) - '0';
540 start_print = cur_index;
542 while (cur_index != end) {
543 char c = LOG_BUF(cur_index);
549 * printk() has already given us loglevel tags in
550 * the buffer. This code is here in case the
551 * log buffer has wrapped right round and scribbled
554 msg_level = default_message_loglevel;
556 _call_console_drivers(start_print, cur_index, msg_level);
558 start_print = cur_index;
563 _call_console_drivers(start_print, end, msg_level);
566 static void emit_log_char(char c)
568 LOG_BUF(log_end) = c;
570 if (log_end - log_start > log_buf_len)
571 log_start = log_end - log_buf_len;
572 if (log_end - con_start > log_buf_len)
573 con_start = log_end - log_buf_len;
574 if (logged_chars < log_buf_len)
579 * Zap console related locks when oopsing. Only zap at most once
580 * every 10 seconds, to leave time for slow consoles to print a
583 static void zap_locks(void)
585 static unsigned long oops_timestamp;
587 if (time_after_eq(jiffies, oops_timestamp) &&
588 !time_after(jiffies, oops_timestamp + 30 * HZ))
591 oops_timestamp = jiffies;
593 /* If a crash is occurring, make sure we can't deadlock */
594 spin_lock_init(&logbuf_lock);
595 /* And make sure that we print immediately */
596 init_MUTEX(&console_sem);
599 #if defined(CONFIG_PRINTK_TIME)
600 static int printk_time = 1;
602 static int printk_time = 0;
604 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
606 /* Check if we have any console registered that can be called early in boot. */
607 static int have_callable_console(void)
611 for_each_console(con)
612 if (con->flags & CON_ANYTIME)
619 * printk - print a kernel message
620 * @fmt: format string
622 * This is printk(). It can be called from any context. We want it to work.
624 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
625 * call the console drivers. If we fail to get the semaphore we place the output
626 * into the log buffer and return. The current holder of the console_sem will
627 * notice the new output in release_console_sem() and will send it to the
628 * consoles before releasing the semaphore.
630 * One effect of this deferred printing is that code which calls printk() and
631 * then changes console_loglevel may break. This is because console_loglevel
632 * is inspected when the actual printing occurs.
637 * See the vsnprintf() documentation for format string extensions over C99.
640 asmlinkage int printk(const char *fmt, ...)
646 r = vprintk(fmt, args);
652 /* cpu currently holding logbuf_lock */
653 static volatile unsigned int printk_cpu = UINT_MAX;
656 * Can we actually use the console at this time on this cpu?
658 * Console drivers may assume that per-cpu resources have
659 * been allocated. So unless they're explicitly marked as
660 * being able to cope (CON_ANYTIME) don't call them until
661 * this CPU is officially up.
663 static inline int can_use_console(unsigned int cpu)
665 return cpu_online(cpu) || have_callable_console();
669 * Try to get console ownership to actually show the kernel
670 * messages from a 'printk'. Return true (and with the
671 * console_semaphore held, and 'console_locked' set) if it
672 * is successful, false otherwise.
674 * This gets called with the 'logbuf_lock' spinlock held and
675 * interrupts disabled. It should return with 'lockbuf_lock'
676 * released but interrupts still disabled.
678 static int acquire_console_semaphore_for_printk(unsigned int cpu)
682 if (!try_acquire_console_sem()) {
686 * If we can't use the console, we need to release
687 * the console semaphore by hand to avoid flushing
688 * the buffer. We need to hold the console semaphore
689 * in order to do this test safely.
691 if (!can_use_console(cpu)) {
697 printk_cpu = UINT_MAX;
698 spin_unlock(&logbuf_lock);
701 static const char recursion_bug_msg [] =
702 KERN_CRIT "BUG: recent printk recursion!\n";
703 static int recursion_bug;
704 static int new_text_line = 1;
705 static char printk_buf[1024];
707 int printk_delay_msec __read_mostly;
709 static inline void printk_delay(void)
711 if (unlikely(printk_delay_msec)) {
712 int m = printk_delay_msec;
716 touch_nmi_watchdog();
721 asmlinkage int vprintk(const char *fmt, va_list args)
724 int current_log_level = default_message_loglevel;
733 /* This stops the holder of console_sem just where we want him */
734 raw_local_irq_save(flags);
735 this_cpu = smp_processor_id();
738 * Ouch, printk recursed into itself!
740 if (unlikely(printk_cpu == this_cpu)) {
742 * If a crash is occurring during printk() on this CPU,
743 * then try to get the crash message out but make sure
744 * we can't deadlock. Otherwise just return to avoid the
745 * recursion and return - but flag the recursion so that
746 * it can be printed at the next appropriate moment:
748 if (!oops_in_progress) {
750 goto out_restore_irqs;
756 spin_lock(&logbuf_lock);
757 printk_cpu = this_cpu;
761 strcpy(printk_buf, recursion_bug_msg);
762 printed_len = strlen(recursion_bug_msg);
764 /* Emit the output into the temporary buffer */
765 printed_len += vscnprintf(printk_buf + printed_len,
766 sizeof(printk_buf) - printed_len, fmt, args);
768 #ifdef CONFIG_DEBUG_LL
769 printascii(printk_buf);
774 /* Do we have a loglevel in the string? */
776 unsigned char c = p[1];
777 if (c && p[2] == '>') {
779 case '0' ... '7': /* loglevel */
780 current_log_level = c - '0';
781 /* Fallthrough - make sure we're on a new line */
782 case 'd': /* KERN_DEFAULT */
783 if (!new_text_line) {
787 /* Fallthrough - skip the loglevel */
788 case 'c': /* KERN_CONT */
796 * Copy the output into log_buf. If the caller didn't provide
797 * appropriate log level tags, we insert them here
801 /* Always output the token */
803 emit_log_char(current_log_level + '0');
809 /* Follow the token with the time */
812 unsigned long long t;
813 unsigned long nanosec_rem;
815 t = cpu_clock(printk_cpu);
816 nanosec_rem = do_div(t, 1000000000);
817 tlen = sprintf(tbuf, "[%5lu.%06lu] ",
821 for (tp = tbuf; tp < tbuf + tlen; tp++)
836 * Try to acquire and then immediately release the
837 * console semaphore. The release will do all the
838 * actual magic (print out buffers, wake up klogd,
841 * The acquire_console_semaphore_for_printk() function
842 * will release 'logbuf_lock' regardless of whether it
843 * actually gets the semaphore or not.
845 if (acquire_console_semaphore_for_printk(this_cpu))
846 release_console_sem();
850 raw_local_irq_restore(flags);
855 EXPORT_SYMBOL(printk);
856 EXPORT_SYMBOL(vprintk);
860 static void call_console_drivers(unsigned start, unsigned end)
866 static int __add_preferred_console(char *name, int idx, char *options,
869 struct console_cmdline *c;
873 * See if this tty is not yet registered, and
874 * if we have a slot free.
876 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
877 if (strcmp(console_cmdline[i].name, name) == 0 &&
878 console_cmdline[i].index == idx) {
880 selected_console = i;
883 if (i == MAX_CMDLINECONSOLES)
886 selected_console = i;
887 c = &console_cmdline[i];
888 strlcpy(c->name, name, sizeof(c->name));
889 c->options = options;
890 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
891 c->brl_options = brl_options;
897 * Set up a list of consoles. Called from init/main.c
899 static int __init console_setup(char *str)
901 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
902 char *s, *options, *brl_options = NULL;
905 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
906 if (!memcmp(str, "brl,", 4)) {
909 } else if (!memcmp(str, "brl=", 4)) {
910 brl_options = str + 4;
911 str = strchr(brl_options, ',');
913 printk(KERN_ERR "need port name after brl=\n");
921 * Decode str into name, index, options.
923 if (str[0] >= '0' && str[0] <= '9') {
925 strncpy(buf + 4, str, sizeof(buf) - 5);
927 strncpy(buf, str, sizeof(buf) - 1);
929 buf[sizeof(buf) - 1] = 0;
930 if ((options = strchr(str, ',')) != NULL)
933 if (!strcmp(str, "ttya"))
934 strcpy(buf, "ttyS0");
935 if (!strcmp(str, "ttyb"))
936 strcpy(buf, "ttyS1");
938 for (s = buf; *s; s++)
939 if ((*s >= '0' && *s <= '9') || *s == ',')
941 idx = simple_strtoul(s, NULL, 10);
944 __add_preferred_console(buf, idx, options, brl_options);
945 console_set_on_cmdline = 1;
948 __setup("console=", console_setup);
951 * add_preferred_console - add a device to the list of preferred consoles.
954 * @options: options for this console
956 * The last preferred console added will be used for kernel messages
957 * and stdin/out/err for init. Normally this is used by console_setup
958 * above to handle user-supplied console arguments; however it can also
959 * be used by arch-specific code either to override the user or more
960 * commonly to provide a default console (ie from PROM variables) when
961 * the user has not supplied one.
963 int add_preferred_console(char *name, int idx, char *options)
965 return __add_preferred_console(name, idx, options, NULL);
968 int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
970 struct console_cmdline *c;
973 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
974 if (strcmp(console_cmdline[i].name, name) == 0 &&
975 console_cmdline[i].index == idx) {
976 c = &console_cmdline[i];
977 strlcpy(c->name, name_new, sizeof(c->name));
978 c->name[sizeof(c->name) - 1] = 0;
979 c->options = options;
987 int console_suspend_enabled = 1;
988 EXPORT_SYMBOL(console_suspend_enabled);
990 static int __init console_suspend_disable(char *str)
992 console_suspend_enabled = 0;
995 __setup("no_console_suspend", console_suspend_disable);
998 * suspend_console - suspend the console subsystem
1000 * This disables printk() while we go into suspend states
1002 void suspend_console(void)
1004 if (!console_suspend_enabled)
1006 printk("Suspending console(s) (use no_console_suspend to debug)\n");
1007 acquire_console_sem();
1008 console_suspended = 1;
1012 void resume_console(void)
1014 if (!console_suspend_enabled)
1017 console_suspended = 0;
1018 release_console_sem();
1022 * acquire_console_sem - lock the console system for exclusive use.
1024 * Acquires a semaphore which guarantees that the caller has
1025 * exclusive access to the console system and the console_drivers list.
1027 * Can sleep, returns nothing.
1029 void acquire_console_sem(void)
1031 BUG_ON(in_interrupt());
1033 if (console_suspended)
1036 console_may_schedule = 1;
1038 EXPORT_SYMBOL(acquire_console_sem);
1040 int try_acquire_console_sem(void)
1042 if (down_trylock(&console_sem))
1044 if (console_suspended) {
1049 console_may_schedule = 0;
1052 EXPORT_SYMBOL(try_acquire_console_sem);
1054 int is_console_locked(void)
1056 return console_locked;
1059 static DEFINE_PER_CPU(int, printk_pending);
1061 void printk_tick(void)
1063 if (__get_cpu_var(printk_pending)) {
1064 __get_cpu_var(printk_pending) = 0;
1065 wake_up_interruptible(&log_wait);
1069 int printk_needs_cpu(int cpu)
1071 if (unlikely(cpu_is_offline(cpu)))
1073 return per_cpu(printk_pending, cpu);
1076 void wake_up_klogd(void)
1078 if (waitqueue_active(&log_wait))
1079 __raw_get_cpu_var(printk_pending) = 1;
1083 * release_console_sem - unlock the console system
1085 * Releases the semaphore which the caller holds on the console system
1086 * and the console driver list.
1088 * While the semaphore was held, console output may have been buffered
1089 * by printk(). If this is the case, release_console_sem() emits
1090 * the output prior to releasing the semaphore.
1092 * If there is output waiting for klogd, we wake it up.
1094 * release_console_sem() may be called from any context.
1096 void release_console_sem(void)
1098 unsigned long flags;
1099 unsigned _con_start, _log_end;
1100 unsigned wake_klogd = 0;
1102 if (console_suspended) {
1107 console_may_schedule = 0;
1110 spin_lock_irqsave(&logbuf_lock, flags);
1111 wake_klogd |= log_start - log_end;
1112 if (con_start == log_end)
1113 break; /* Nothing to print */
1114 _con_start = con_start;
1116 con_start = log_end; /* Flush */
1117 spin_unlock(&logbuf_lock);
1118 stop_critical_timings(); /* don't trace print latency */
1119 call_console_drivers(_con_start, _log_end);
1120 start_critical_timings();
1121 local_irq_restore(flags);
1125 spin_unlock_irqrestore(&logbuf_lock, flags);
1129 EXPORT_SYMBOL(release_console_sem);
1132 * console_conditional_schedule - yield the CPU if required
1134 * If the console code is currently allowed to sleep, and
1135 * if this CPU should yield the CPU to another task, do
1138 * Must be called within acquire_console_sem().
1140 void __sched console_conditional_schedule(void)
1142 if (console_may_schedule)
1145 EXPORT_SYMBOL(console_conditional_schedule);
1147 void console_unblank(void)
1152 * console_unblank can no longer be called in interrupt context unless
1153 * oops_in_progress is set to 1..
1155 if (oops_in_progress) {
1156 if (down_trylock(&console_sem) != 0)
1159 acquire_console_sem();
1162 console_may_schedule = 0;
1164 if ((c->flags & CON_ENABLED) && c->unblank)
1166 release_console_sem();
1170 * Return the console tty driver structure and its associated index
1172 struct tty_driver *console_device(int *index)
1175 struct tty_driver *driver = NULL;
1177 acquire_console_sem();
1178 for_each_console(c) {
1181 driver = c->device(c, index);
1185 release_console_sem();
1190 * Prevent further output on the passed console device so that (for example)
1191 * serial drivers can disable console output before suspending a port, and can
1192 * re-enable output afterwards.
1194 void console_stop(struct console *console)
1196 acquire_console_sem();
1197 console->flags &= ~CON_ENABLED;
1198 release_console_sem();
1200 EXPORT_SYMBOL(console_stop);
1202 void console_start(struct console *console)
1204 acquire_console_sem();
1205 console->flags |= CON_ENABLED;
1206 release_console_sem();
1208 EXPORT_SYMBOL(console_start);
1211 * The console driver calls this routine during kernel initialization
1212 * to register the console printing procedure with printk() and to
1213 * print any messages that were printed by the kernel before the
1214 * console driver was initialized.
1216 * This can happen pretty early during the boot process (because of
1217 * early_printk) - sometimes before setup_arch() completes - be careful
1218 * of what kernel features are used - they may not be initialised yet.
1220 * There are two types of consoles - bootconsoles (early_printk) and
1221 * "real" consoles (everything which is not a bootconsole) which are
1222 * handled differently.
1223 * - Any number of bootconsoles can be registered at any time.
1224 * - As soon as a "real" console is registered, all bootconsoles
1225 * will be unregistered automatically.
1226 * - Once a "real" console is registered, any attempt to register a
1227 * bootconsoles will be rejected
1229 void register_console(struct console *newcon)
1232 unsigned long flags;
1233 struct console *bcon = NULL;
1236 * before we register a new CON_BOOT console, make sure we don't
1237 * already have a valid console
1239 if (console_drivers && newcon->flags & CON_BOOT) {
1240 /* find the last or real console */
1241 for_each_console(bcon) {
1242 if (!(bcon->flags & CON_BOOT)) {
1243 printk(KERN_INFO "Too late to register bootconsole %s%d\n",
1244 newcon->name, newcon->index);
1250 if (console_drivers && console_drivers->flags & CON_BOOT)
1251 bcon = console_drivers;
1253 if (preferred_console < 0 || bcon || !console_drivers)
1254 preferred_console = selected_console;
1256 if (newcon->early_setup)
1257 newcon->early_setup();
1260 * See if we want to use this console driver. If we
1261 * didn't select a console we take the first one
1262 * that registers here.
1264 if (preferred_console < 0) {
1265 if (newcon->index < 0)
1267 if (newcon->setup == NULL ||
1268 newcon->setup(newcon, NULL) == 0) {
1269 newcon->flags |= CON_ENABLED;
1270 if (newcon->device) {
1271 newcon->flags |= CON_CONSDEV;
1272 preferred_console = 0;
1278 * See if this console matches one we selected on
1281 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1283 if (strcmp(console_cmdline[i].name, newcon->name) != 0)
1285 if (newcon->index >= 0 &&
1286 newcon->index != console_cmdline[i].index)
1288 if (newcon->index < 0)
1289 newcon->index = console_cmdline[i].index;
1290 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1291 if (console_cmdline[i].brl_options) {
1292 newcon->flags |= CON_BRL;
1293 braille_register_console(newcon,
1294 console_cmdline[i].index,
1295 console_cmdline[i].options,
1296 console_cmdline[i].brl_options);
1300 if (newcon->setup &&
1301 newcon->setup(newcon, console_cmdline[i].options) != 0)
1303 newcon->flags |= CON_ENABLED;
1304 newcon->index = console_cmdline[i].index;
1305 if (i == selected_console) {
1306 newcon->flags |= CON_CONSDEV;
1307 preferred_console = selected_console;
1312 if (!(newcon->flags & CON_ENABLED))
1316 * If we have a bootconsole, and are switching to a real console,
1317 * don't print everything out again, since when the boot console, and
1318 * the real console are the same physical device, it's annoying to
1319 * see the beginning boot messages twice
1321 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
1322 newcon->flags &= ~CON_PRINTBUFFER;
1325 * Put this console in the list - keep the
1326 * preferred driver at the head of the list.
1328 acquire_console_sem();
1329 if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
1330 newcon->next = console_drivers;
1331 console_drivers = newcon;
1333 newcon->next->flags &= ~CON_CONSDEV;
1335 newcon->next = console_drivers->next;
1336 console_drivers->next = newcon;
1338 if (newcon->flags & CON_PRINTBUFFER) {
1340 * release_console_sem() will print out the buffered messages
1343 spin_lock_irqsave(&logbuf_lock, flags);
1344 con_start = log_start;
1345 spin_unlock_irqrestore(&logbuf_lock, flags);
1347 release_console_sem();
1350 * By unregistering the bootconsoles after we enable the real console
1351 * we get the "console xxx enabled" message on all the consoles -
1352 * boot consoles, real consoles, etc - this is to ensure that end
1353 * users know there might be something in the kernel's log buffer that
1354 * went to the bootconsole (that they do not see on the real console)
1356 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV)) {
1357 /* we need to iterate through twice, to make sure we print
1358 * everything out, before we unregister the console(s)
1360 printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
1361 newcon->name, newcon->index);
1362 for_each_console(bcon)
1363 if (bcon->flags & CON_BOOT)
1364 unregister_console(bcon);
1366 printk(KERN_INFO "%sconsole [%s%d] enabled\n",
1367 (newcon->flags & CON_BOOT) ? "boot" : "" ,
1368 newcon->name, newcon->index);
1371 EXPORT_SYMBOL(register_console);
1373 int unregister_console(struct console *console)
1375 struct console *a, *b;
1378 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1379 if (console->flags & CON_BRL)
1380 return braille_unregister_console(console);
1383 acquire_console_sem();
1384 if (console_drivers == console) {
1385 console_drivers=console->next;
1387 } else if (console_drivers) {
1388 for (a=console_drivers->next, b=console_drivers ;
1389 a; b=a, a=b->next) {
1399 * If this isn't the last console and it has CON_CONSDEV set, we
1400 * need to set it on the next preferred console.
1402 if (console_drivers != NULL && console->flags & CON_CONSDEV)
1403 console_drivers->flags |= CON_CONSDEV;
1405 release_console_sem();
1408 EXPORT_SYMBOL(unregister_console);
1410 static int __init disable_boot_consoles(void)
1412 struct console *con;
1414 for_each_console(con) {
1415 if (con->flags & CON_BOOT) {
1416 printk(KERN_INFO "turn off boot console %s%d\n",
1417 con->name, con->index);
1418 unregister_console(con);
1423 late_initcall(disable_boot_consoles);
1425 #if defined CONFIG_PRINTK
1428 * printk rate limiting, lifted from the networking subsystem.
1430 * This enforces a rate limit: not more than 10 kernel messages
1431 * every 5s to make a denial-of-service attack impossible.
1433 DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
1435 int printk_ratelimit(void)
1437 return __ratelimit(&printk_ratelimit_state);
1439 EXPORT_SYMBOL(printk_ratelimit);
1442 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1443 * @caller_jiffies: pointer to caller's state
1444 * @interval_msecs: minimum interval between prints
1446 * printk_timed_ratelimit() returns true if more than @interval_msecs
1447 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1450 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1451 unsigned int interval_msecs)
1453 if (*caller_jiffies == 0
1454 || !time_in_range(jiffies, *caller_jiffies,
1456 + msecs_to_jiffies(interval_msecs))) {
1457 *caller_jiffies = jiffies;
1462 EXPORT_SYMBOL(printk_timed_ratelimit);