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/ratelimit.h>
37 #include <linux/kmsg_dump.h>
38 #include <linux/syslog.h>
40 #include <asm/uaccess.h>
43 * for_each_console() allows you to iterate on each console
45 #define for_each_console(con) \
46 for (con = console_drivers; con != NULL; con = con->next)
49 * Architectures can override it:
51 void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
55 /* printk's without a loglevel use this.. */
56 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
58 /* We show everything that is MORE important than this.. */
59 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
60 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
62 DECLARE_WAIT_QUEUE_HEAD(log_wait);
64 int console_printk[4] = {
65 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
66 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
67 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
68 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
72 * Low level drivers may need that to know if they can schedule in
73 * their unblank() callback or not. So let's export it.
76 EXPORT_SYMBOL(oops_in_progress);
79 * console_sem protects the console_drivers list, and also
80 * provides serialisation for access to the entire console
83 static DECLARE_MUTEX(console_sem);
84 struct console *console_drivers;
85 EXPORT_SYMBOL_GPL(console_drivers);
88 * This is used for debugging the mess that is the VT code by
89 * keeping track if we have the console semaphore held. It's
90 * definitely not the perfect debug tool (we don't know if _WE_
91 * hold it are racing, but it helps tracking those weird code
92 * path in the console code where we end up in places I want
93 * locked without the console sempahore held
95 static int console_locked, console_suspended;
97 #define LOG_BUF_MASK(ns) ((ns)->buf_len-1)
98 #define LOG_BUF(ns, idx) ((ns)->buf[(idx) & LOG_BUF_MASK(ns)])
101 * To access container syslog ring buffer
103 #define sys_log_lock (syslog_ns->logbuf_lock)
104 #define sys_log_start (syslog_ns->log_start)
105 #define sys_log_end (syslog_ns->log_end)
106 #define sys_log_con_start (syslog_ns->con_start)
107 #define sys_log_buf_len (syslog_ns->buf_len)
108 #define sys_log_logged_chars (syslog_ns->logged_chars)
109 #define sys_log_buf (syslog_ns->buf)
112 * Array of consoles built from command line options (console=)
114 struct console_cmdline
116 char name[8]; /* Name of the driver */
117 int index; /* Minor dev. to use */
118 char *options; /* Options for the driver */
119 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
120 char *brl_options; /* Options for braille driver */
124 #define MAX_CMDLINECONSOLES 8
126 static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
127 static int selected_console = -1;
128 static int preferred_console = -1;
129 int console_set_on_cmdline;
130 EXPORT_SYMBOL(console_set_on_cmdline);
132 /* Flag: console code may call schedule() */
133 static int console_may_schedule;
137 static int saved_console_loglevel = -1;
141 * This appends the listed symbols to /proc/vmcoreinfo
143 * /proc/vmcoreinfo is used by various utiilties, like crash and makedumpfile to
144 * obtain access to symbols that are otherwise very difficult to locate. These
145 * symbols are specifically used so that utilities can access and extract the
146 * dmesg log from a vmcore file after a crash.
148 void log_buf_kexec_setup(void)
150 struct syslog_ns *syslog_ns = current_syslog_ns();
152 VMCOREINFO_SYMBOL(sys_log_buf);
153 VMCOREINFO_SYMBOL(sys_log_end);
154 VMCOREINFO_SYMBOL(sys_log_buf_len);
155 VMCOREINFO_SYMBOL(sys_log_logged_chars);
159 static int __init log_buf_len_setup(char *str)
161 unsigned size = memparse(str, &str);
164 size = roundup_pow_of_two(size);
165 resize_syslog_ns(&init_syslog_ns, size);
170 __setup("log_buf_len=", log_buf_len_setup);
172 #ifdef CONFIG_BOOT_PRINTK_DELAY
174 static unsigned int boot_delay; /* msecs delay after each printk during bootup */
175 static unsigned long long loops_per_msec; /* based on boot_delay */
177 static int __init boot_delay_setup(char *str)
181 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
182 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
184 get_option(&str, &boot_delay);
185 if (boot_delay > 10 * 1000)
188 pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
189 "HZ: %d, loops_per_msec: %llu\n",
190 boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
193 __setup("boot_delay=", boot_delay_setup);
195 static void boot_delay_msec(void)
197 unsigned long long k;
198 unsigned long timeout;
200 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
203 k = (unsigned long long)loops_per_msec * boot_delay;
205 timeout = jiffies + msecs_to_jiffies(boot_delay);
210 * use (volatile) jiffies to prevent
211 * compiler reduction; loop termination via jiffies
212 * is secondary and may or may not happen.
214 if (time_after(jiffies, timeout))
216 touch_nmi_watchdog();
220 static inline void boot_delay_msec(void)
225 int do_syslog(int type, char __user *buf, int len, bool from_file)
227 unsigned i, j, limit, count;
231 struct syslog_ns *syslog_ns = current_syslog_ns();
233 error = security_syslog(type, from_file);
238 case SYSLOG_ACTION_CLOSE: /* Close log */
240 case SYSLOG_ACTION_OPEN: /* Open log */
242 case SYSLOG_ACTION_READ: /* Read from log */
249 if (!access_ok(VERIFY_WRITE, buf, len)) {
253 error = wait_event_interruptible(log_wait,
254 (sys_log_start - sys_log_end));
258 spin_lock_irq(&sys_log_lock);
259 while (!error && (sys_log_start != sys_log_end) && i < len) {
260 c = LOG_BUF(syslog_ns, sys_log_start);
262 spin_unlock_irq(&sys_log_lock);
263 error = __put_user(c,buf);
267 spin_lock_irq(&sys_log_lock);
269 spin_unlock_irq(&sys_log_lock);
273 /* Read/clear last kernel messages */
274 case SYSLOG_ACTION_READ_CLEAR:
277 /* Read last kernel messages */
278 case SYSLOG_ACTION_READ_ALL:
285 if (!access_ok(VERIFY_WRITE, buf, len)) {
290 if (count > sys_log_buf_len)
291 count = sys_log_buf_len;
292 spin_lock_irq(&sys_log_lock);
293 if (count > sys_log_logged_chars)
294 count = sys_log_logged_chars;
296 sys_log_logged_chars = 0;
299 * __put_user() could sleep, and while we sleep
300 * printk() could overwrite the messages
301 * we try to copy to user space. Therefore
302 * the messages are copied in reverse. <manfreds>
304 for (i = 0; i < count && !error; i++) {
306 if (j + sys_log_buf_len < sys_log_end)
308 c = LOG_BUF(syslog_ns, j);
309 spin_unlock_irq(&sys_log_lock);
310 error = __put_user(c,&buf[count-1-i]);
312 spin_lock_irq(&sys_log_lock);
314 spin_unlock_irq(&sys_log_lock);
319 int offset = count-error;
320 /* buffer overflow during copy, correct user buffer. */
321 for (i = 0; i < error; i++) {
322 if (__get_user(c,&buf[i+offset]) ||
323 __put_user(c,&buf[i])) {
331 /* Clear ring buffer */
332 case SYSLOG_ACTION_CLEAR:
333 sys_log_logged_chars = 0;
335 /* Disable logging to console */
336 case SYSLOG_ACTION_CONSOLE_OFF:
337 if (saved_console_loglevel == -1)
338 saved_console_loglevel = console_loglevel;
339 console_loglevel = minimum_console_loglevel;
341 /* Enable logging to console */
342 case SYSLOG_ACTION_CONSOLE_ON:
343 if (saved_console_loglevel != -1) {
344 console_loglevel = saved_console_loglevel;
345 saved_console_loglevel = -1;
348 /* Set level of messages printed to console */
349 case SYSLOG_ACTION_CONSOLE_LEVEL:
351 if (len < 1 || len > 8)
353 if (len < minimum_console_loglevel)
354 len = minimum_console_loglevel;
355 console_loglevel = len;
356 /* Implicitly re-enable logging to console */
357 saved_console_loglevel = -1;
360 /* Number of chars in the log buffer */
361 case SYSLOG_ACTION_SIZE_UNREAD:
362 error = sys_log_end - sys_log_start;
364 /* Size of the log buffer */
365 case SYSLOG_ACTION_SIZE_BUFFER:
366 error = sys_log_buf_len;
376 SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
378 return do_syslog(type, buf, len, SYSLOG_FROM_CALL);
382 * Call the console drivers on a range of log_buf
384 static void __call_console_drivers(struct syslog_ns *syslog_ns,
385 unsigned start, unsigned end)
389 for_each_console(con) {
390 if ((con->flags & CON_ENABLED) && con->write &&
391 (cpu_online(smp_processor_id()) ||
392 (con->flags & CON_ANYTIME)))
393 con->write(con, &LOG_BUF(syslog_ns, start),
398 static int __read_mostly ignore_loglevel;
400 static int __init ignore_loglevel_setup(char *str)
403 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
408 early_param("ignore_loglevel", ignore_loglevel_setup);
411 * Write out chars from start to end - 1 inclusive
413 static void _call_console_drivers(struct syslog_ns *syslog_ns, unsigned start,
414 unsigned end, int msg_log_level)
416 if ((msg_log_level < console_loglevel || ignore_loglevel) &&
417 console_drivers && start != end) {
418 if ((start & LOG_BUF_MASK(syslog_ns)) >
419 (end & LOG_BUF_MASK(syslog_ns))) {
421 __call_console_drivers(syslog_ns,
422 start & LOG_BUF_MASK(syslog_ns),
424 __call_console_drivers(syslog_ns, 0,
425 end & LOG_BUF_MASK(syslog_ns));
427 __call_console_drivers(syslog_ns, start, end);
433 * Call the console drivers, asking them to write out
434 * log_buf[start] to log_buf[end - 1].
435 * The console_sem must be held.
437 static void call_console_drivers(struct syslog_ns *syslog_ns,
438 unsigned start, unsigned end)
440 unsigned cur_index, start_print;
441 static int msg_level = -1;
443 BUG_ON(((int)(start - end)) > 0);
447 while (cur_index != end) {
448 if (msg_level < 0 && ((end - cur_index) > 2) &&
449 LOG_BUF(syslog_ns, cur_index + 0) == '<' &&
450 LOG_BUF(syslog_ns, cur_index + 1) >= '0' &&
451 LOG_BUF(syslog_ns, cur_index + 1) <= '7' &&
452 LOG_BUF(syslog_ns, cur_index + 2) == '>') {
453 msg_level = LOG_BUF(syslog_ns, cur_index + 1) - '0';
455 start_print = cur_index;
457 while (cur_index != end) {
458 char c = LOG_BUF(syslog_ns, cur_index);
464 * printk() has already given us loglevel tags in
465 * the buffer. This code is here in case the
466 * log buffer has wrapped right round and scribbled
469 msg_level = default_message_loglevel;
471 _call_console_drivers(syslog_ns,
472 start_print, cur_index, msg_level);
474 start_print = cur_index;
479 _call_console_drivers(syslog_ns, start_print, end, msg_level);
482 static void emit_log_char(struct syslog_ns *syslog_ns, char c)
484 LOG_BUF(syslog_ns, sys_log_end) = c;
486 if (sys_log_end - sys_log_start > sys_log_buf_len)
487 sys_log_start = sys_log_end - sys_log_buf_len;
488 if (sys_log_end - sys_log_con_start > sys_log_buf_len)
489 sys_log_con_start = sys_log_end - sys_log_buf_len;
490 if (sys_log_logged_chars < sys_log_buf_len)
491 sys_log_logged_chars++;
495 * Zap console related locks when oopsing. Only zap at most once
496 * every 10 seconds, to leave time for slow consoles to print a
499 static void zap_locks(struct syslog_ns *syslog_ns)
501 static unsigned long oops_timestamp;
503 if (time_after_eq(jiffies, oops_timestamp) &&
504 !time_after(jiffies, oops_timestamp + 30 * HZ))
507 oops_timestamp = jiffies;
509 /* If a crash is occurring, make sure we can't deadlock */
510 spin_lock_init(&sys_log_lock);
511 /* And make sure that we print immediately */
512 init_MUTEX(&console_sem);
515 #if defined(CONFIG_PRINTK_TIME)
516 static int printk_time = 1;
518 static int printk_time = 0;
520 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
522 /* Check if we have any console registered that can be called early in boot. */
523 static int have_callable_console(void)
527 for_each_console(con)
528 if (con->flags & CON_ANYTIME)
535 * printk - print a kernel message
536 * @fmt: format string
538 * This is printk(). It can be called from any context. We want it to work.
540 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
541 * call the console drivers. If we fail to get the semaphore we place the output
542 * into the log buffer and return. The current holder of the console_sem will
543 * notice the new output in release_console_sem() and will send it to the
544 * consoles before releasing the semaphore.
546 * One effect of this deferred printing is that code which calls printk() and
547 * then changes console_loglevel may break. This is because console_loglevel
548 * is inspected when the actual printing occurs.
553 * See the vsnprintf() documentation for format string extensions over C99.
556 asmlinkage int printk(const char *fmt, ...)
562 r = vprintk(fmt, args);
568 /* cpu currently holding logbuf_lock */
569 static volatile unsigned int printk_cpu = UINT_MAX;
572 * Can we actually use the console at this time on this cpu?
574 * Console drivers may assume that per-cpu resources have
575 * been allocated. So unless they're explicitly marked as
576 * being able to cope (CON_ANYTIME) don't call them until
577 * this CPU is officially up.
579 static inline int can_use_console(unsigned int cpu)
581 return cpu_online(cpu) || have_callable_console();
585 * Try to get console ownership to actually show the kernel
586 * messages from a 'printk'. Return true (and with the
587 * console_semaphore held, and 'console_locked' set) if it
588 * is successful, false otherwise.
590 * This gets called with the 'logbuf_lock' spinlock held and
591 * interrupts disabled. It should return with 'lockbuf_lock'
592 * released but interrupts still disabled.
594 static int acquire_console_semaphore_for_printk(
595 struct syslog_ns *syslog_ns, unsigned int cpu)
599 if (!try_acquire_console_sem()) {
603 * If we can't use the console, we need to release
604 * the console semaphore by hand to avoid flushing
605 * the buffer. We need to hold the console semaphore
606 * in order to do this test safely.
608 if (!can_use_console(cpu)) {
614 printk_cpu = UINT_MAX;
615 spin_unlock(&sys_log_lock);
618 static const char recursion_bug_msg [] =
619 KERN_CRIT "BUG: recent printk recursion!\n";
620 static int recursion_bug;
621 static int new_text_line = 1;
622 static char printk_buf[1024];
624 int printk_delay_msec __read_mostly;
626 static inline void printk_delay(void)
628 if (unlikely(printk_delay_msec)) {
629 int m = printk_delay_msec;
633 touch_nmi_watchdog();
638 asmlinkage int vprintk(const char *fmt, va_list args)
641 int current_log_level = default_message_loglevel;
642 struct syslog_ns *syslog_ns = current_syslog_ns();
651 /* This stops the holder of console_sem just where we want him */
652 raw_local_irq_save(flags);
653 this_cpu = smp_processor_id();
656 * Ouch, printk recursed into itself!
658 if (unlikely(printk_cpu == this_cpu)) {
660 * If a crash is occurring during printk() on this CPU,
661 * then try to get the crash message out but make sure
662 * we can't deadlock. Otherwise just return to avoid the
663 * recursion and return - but flag the recursion so that
664 * it can be printed at the next appropriate moment:
666 if (!oops_in_progress) {
668 goto out_restore_irqs;
670 zap_locks(syslog_ns);
674 spin_lock(&sys_log_lock);
675 printk_cpu = this_cpu;
679 strcpy(printk_buf, recursion_bug_msg);
680 printed_len = strlen(recursion_bug_msg);
682 /* Emit the output into the temporary buffer */
683 printed_len += vscnprintf(printk_buf + printed_len,
684 sizeof(printk_buf) - printed_len, fmt, args);
689 /* Do we have a loglevel in the string? */
691 unsigned char c = p[1];
692 if (c && p[2] == '>') {
694 case '0' ... '7': /* loglevel */
695 current_log_level = c - '0';
696 /* Fallthrough - make sure we're on a new line */
697 case 'd': /* KERN_DEFAULT */
698 if (!new_text_line) {
699 emit_log_char(syslog_ns, '\n');
702 /* Fallthrough - skip the loglevel */
703 case 'c': /* KERN_CONT */
711 * Copy the output into log_buf. If the caller didn't provide
712 * appropriate log level tags, we insert them here
716 /* Always output the token */
717 emit_log_char(syslog_ns, '<');
718 emit_log_char(syslog_ns, current_log_level + '0');
719 emit_log_char(syslog_ns, '>');
724 /* Follow the token with the time */
727 unsigned long long t;
728 unsigned long nanosec_rem;
730 t = cpu_clock(printk_cpu);
731 nanosec_rem = do_div(t, 1000000000);
732 tlen = sprintf(tbuf, "ns_id='%d' %5lu.%06lu] ",
737 for (tp = tbuf; tp < tbuf + tlen; tp++)
738 emit_log_char(syslog_ns, *tp);
746 emit_log_char(syslog_ns, *p);
752 * Try to acquire and then immediately release the
753 * console semaphore. The release will do all the
754 * actual magic (print out buffers, wake up klogd,
757 * The acquire_console_semaphore_for_printk() function
758 * will release 'logbuf_lock' regardless of whether it
759 * actually gets the semaphore or not.
761 if (acquire_console_semaphore_for_printk(syslog_ns, this_cpu))
762 release_console_sem();
766 raw_local_irq_restore(flags);
771 EXPORT_SYMBOL(printk);
772 EXPORT_SYMBOL(vprintk);
776 static int __add_preferred_console(char *name, int idx, char *options,
779 struct console_cmdline *c;
783 * See if this tty is not yet registered, and
784 * if we have a slot free.
786 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
787 if (strcmp(console_cmdline[i].name, name) == 0 &&
788 console_cmdline[i].index == idx) {
790 selected_console = i;
793 if (i == MAX_CMDLINECONSOLES)
796 selected_console = i;
797 c = &console_cmdline[i];
798 strlcpy(c->name, name, sizeof(c->name));
799 c->options = options;
800 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
801 c->brl_options = brl_options;
807 * Set up a list of consoles. Called from init/main.c
809 static int __init console_setup(char *str)
811 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
812 char *s, *options, *brl_options = NULL;
815 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
816 if (!memcmp(str, "brl,", 4)) {
819 } else if (!memcmp(str, "brl=", 4)) {
820 brl_options = str + 4;
821 str = strchr(brl_options, ',');
823 printk(KERN_ERR "need port name after brl=\n");
831 * Decode str into name, index, options.
833 if (str[0] >= '0' && str[0] <= '9') {
835 strncpy(buf + 4, str, sizeof(buf) - 5);
837 strncpy(buf, str, sizeof(buf) - 1);
839 buf[sizeof(buf) - 1] = 0;
840 if ((options = strchr(str, ',')) != NULL)
843 if (!strcmp(str, "ttya"))
844 strcpy(buf, "ttyS0");
845 if (!strcmp(str, "ttyb"))
846 strcpy(buf, "ttyS1");
848 for (s = buf; *s; s++)
849 if ((*s >= '0' && *s <= '9') || *s == ',')
851 idx = simple_strtoul(s, NULL, 10);
854 __add_preferred_console(buf, idx, options, brl_options);
855 console_set_on_cmdline = 1;
858 __setup("console=", console_setup);
861 * add_preferred_console - add a device to the list of preferred consoles.
864 * @options: options for this console
866 * The last preferred console added will be used for kernel messages
867 * and stdin/out/err for init. Normally this is used by console_setup
868 * above to handle user-supplied console arguments; however it can also
869 * be used by arch-specific code either to override the user or more
870 * commonly to provide a default console (ie from PROM variables) when
871 * the user has not supplied one.
873 int add_preferred_console(char *name, int idx, char *options)
875 return __add_preferred_console(name, idx, options, NULL);
878 int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
880 struct console_cmdline *c;
883 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
884 if (strcmp(console_cmdline[i].name, name) == 0 &&
885 console_cmdline[i].index == idx) {
886 c = &console_cmdline[i];
887 strlcpy(c->name, name_new, sizeof(c->name));
888 c->name[sizeof(c->name) - 1] = 0;
889 c->options = options;
897 int console_suspend_enabled = 1;
898 EXPORT_SYMBOL(console_suspend_enabled);
900 static int __init console_suspend_disable(char *str)
902 console_suspend_enabled = 0;
905 __setup("no_console_suspend", console_suspend_disable);
908 * suspend_console - suspend the console subsystem
910 * This disables printk() while we go into suspend states
912 void suspend_console(void)
914 if (!console_suspend_enabled)
916 printk("Suspending console(s) (use no_console_suspend to debug)\n");
917 acquire_console_sem();
918 console_suspended = 1;
922 void resume_console(void)
924 if (!console_suspend_enabled)
927 console_suspended = 0;
928 release_console_sem();
932 * acquire_console_sem - lock the console system for exclusive use.
934 * Acquires a semaphore which guarantees that the caller has
935 * exclusive access to the console system and the console_drivers list.
937 * Can sleep, returns nothing.
939 void acquire_console_sem(void)
941 BUG_ON(in_interrupt());
943 if (console_suspended)
946 console_may_schedule = 1;
948 EXPORT_SYMBOL(acquire_console_sem);
950 int try_acquire_console_sem(void)
952 if (down_trylock(&console_sem))
954 if (console_suspended) {
959 console_may_schedule = 0;
962 EXPORT_SYMBOL(try_acquire_console_sem);
964 int is_console_locked(void)
966 return console_locked;
969 static DEFINE_PER_CPU(int, printk_pending);
971 void printk_tick(void)
973 if (__get_cpu_var(printk_pending)) {
974 __get_cpu_var(printk_pending) = 0;
975 wake_up_interruptible(&log_wait);
979 int printk_needs_cpu(int cpu)
981 return per_cpu(printk_pending, cpu);
984 void wake_up_klogd(void)
986 if (waitqueue_active(&log_wait))
987 __raw_get_cpu_var(printk_pending) = 1;
991 * release_console_sem - unlock the console system
993 * Releases the semaphore which the caller holds on the console system
994 * and the console driver list.
996 * While the semaphore was held, console output may have been buffered
997 * by printk(). If this is the case, release_console_sem() emits
998 * the output prior to releasing the semaphore.
1000 * If there is output waiting for klogd, we wake it up.
1002 * release_console_sem() may be called from any context.
1004 void release_console_sem(void)
1006 if (console_suspended) {
1011 console_may_schedule = 0;
1012 #ifdef CONFIG_PRINTK
1014 unsigned long flags;
1015 unsigned _con_start, _log_end;
1016 unsigned wake_klogd = 0;
1017 struct syslog_ns *syslog_ns = current_syslog_ns();
1020 spin_lock_irqsave(&sys_log_lock, flags);
1021 wake_klogd |= sys_log_start - sys_log_end;
1022 if (sys_log_con_start == sys_log_end)
1023 break; /* Nothing to print */
1024 _con_start = sys_log_con_start;
1025 _log_end = sys_log_end;
1026 sys_log_con_start = sys_log_end; /* Flush */
1027 spin_unlock(&sys_log_lock);
1028 stop_critical_timings();/* don't trace print latency */
1029 call_console_drivers(syslog_ns, _con_start, _log_end);
1030 start_critical_timings();
1031 local_irq_restore(flags);
1033 spin_unlock_irqrestore(&sys_log_lock, flags);
1041 EXPORT_SYMBOL(release_console_sem);
1044 * console_conditional_schedule - yield the CPU if required
1046 * If the console code is currently allowed to sleep, and
1047 * if this CPU should yield the CPU to another task, do
1050 * Must be called within acquire_console_sem().
1052 void __sched console_conditional_schedule(void)
1054 if (console_may_schedule)
1057 EXPORT_SYMBOL(console_conditional_schedule);
1059 void console_unblank(void)
1064 * console_unblank can no longer be called in interrupt context unless
1065 * oops_in_progress is set to 1..
1067 if (oops_in_progress) {
1068 if (down_trylock(&console_sem) != 0)
1071 acquire_console_sem();
1074 console_may_schedule = 0;
1076 if ((c->flags & CON_ENABLED) && c->unblank)
1078 release_console_sem();
1082 * Return the console tty driver structure and its associated index
1084 struct tty_driver *console_device(int *index)
1087 struct tty_driver *driver = NULL;
1089 acquire_console_sem();
1090 for_each_console(c) {
1093 driver = c->device(c, index);
1097 release_console_sem();
1102 * Prevent further output on the passed console device so that (for example)
1103 * serial drivers can disable console output before suspending a port, and can
1104 * re-enable output afterwards.
1106 void console_stop(struct console *console)
1108 acquire_console_sem();
1109 console->flags &= ~CON_ENABLED;
1110 release_console_sem();
1112 EXPORT_SYMBOL(console_stop);
1114 void console_start(struct console *console)
1116 acquire_console_sem();
1117 console->flags |= CON_ENABLED;
1118 release_console_sem();
1120 EXPORT_SYMBOL(console_start);
1123 * The console driver calls this routine during kernel initialization
1124 * to register the console printing procedure with printk() and to
1125 * print any messages that were printed by the kernel before the
1126 * console driver was initialized.
1128 * This can happen pretty early during the boot process (because of
1129 * early_printk) - sometimes before setup_arch() completes - be careful
1130 * of what kernel features are used - they may not be initialised yet.
1132 * There are two types of consoles - bootconsoles (early_printk) and
1133 * "real" consoles (everything which is not a bootconsole) which are
1134 * handled differently.
1135 * - Any number of bootconsoles can be registered at any time.
1136 * - As soon as a "real" console is registered, all bootconsoles
1137 * will be unregistered automatically.
1138 * - Once a "real" console is registered, any attempt to register a
1139 * bootconsoles will be rejected
1141 void register_console(struct console *newcon)
1144 struct console *bcon = NULL;
1147 * before we register a new CON_BOOT console, make sure we don't
1148 * already have a valid console
1150 if (console_drivers && newcon->flags & CON_BOOT) {
1151 /* find the last or real console */
1152 for_each_console(bcon) {
1153 if (!(bcon->flags & CON_BOOT)) {
1154 printk(KERN_INFO "Too late to register bootconsole %s%d\n",
1155 newcon->name, newcon->index);
1161 if (console_drivers && console_drivers->flags & CON_BOOT)
1162 bcon = console_drivers;
1164 if (preferred_console < 0 || bcon || !console_drivers)
1165 preferred_console = selected_console;
1167 if (newcon->early_setup)
1168 newcon->early_setup();
1171 * See if we want to use this console driver. If we
1172 * didn't select a console we take the first one
1173 * that registers here.
1175 if (preferred_console < 0) {
1176 if (newcon->index < 0)
1178 if (newcon->setup == NULL ||
1179 newcon->setup(newcon, NULL) == 0) {
1180 newcon->flags |= CON_ENABLED;
1181 if (newcon->device) {
1182 newcon->flags |= CON_CONSDEV;
1183 preferred_console = 0;
1189 * See if this console matches one we selected on
1192 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1194 if (strcmp(console_cmdline[i].name, newcon->name) != 0)
1196 if (newcon->index >= 0 &&
1197 newcon->index != console_cmdline[i].index)
1199 if (newcon->index < 0)
1200 newcon->index = console_cmdline[i].index;
1201 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1202 if (console_cmdline[i].brl_options) {
1203 newcon->flags |= CON_BRL;
1204 braille_register_console(newcon,
1205 console_cmdline[i].index,
1206 console_cmdline[i].options,
1207 console_cmdline[i].brl_options);
1211 if (newcon->setup &&
1212 newcon->setup(newcon, console_cmdline[i].options) != 0)
1214 newcon->flags |= CON_ENABLED;
1215 newcon->index = console_cmdline[i].index;
1216 if (i == selected_console) {
1217 newcon->flags |= CON_CONSDEV;
1218 preferred_console = selected_console;
1223 if (!(newcon->flags & CON_ENABLED))
1227 * If we have a bootconsole, and are switching to a real console,
1228 * don't print everything out again, since when the boot console, and
1229 * the real console are the same physical device, it's annoying to
1230 * see the beginning boot messages twice
1232 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
1233 newcon->flags &= ~CON_PRINTBUFFER;
1236 * Put this console in the list - keep the
1237 * preferred driver at the head of the list.
1239 acquire_console_sem();
1240 if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
1241 newcon->next = console_drivers;
1242 console_drivers = newcon;
1244 newcon->next->flags &= ~CON_CONSDEV;
1246 newcon->next = console_drivers->next;
1247 console_drivers->next = newcon;
1249 #ifdef CONFIG_PRINTK
1250 if (newcon->flags & CON_PRINTBUFFER) {
1251 unsigned long flags;
1253 * release_console_sem() will print out the buffered messages
1257 struct syslog_ns *syslog_ns = current_syslog_ns();
1259 spin_lock_irqsave(&sys_log_lock, flags);
1260 sys_log_con_start = sys_log_start;
1261 spin_unlock_irqrestore(&sys_log_lock, flags);
1264 release_console_sem();
1267 * By unregistering the bootconsoles after we enable the real console
1268 * we get the "console xxx enabled" message on all the consoles -
1269 * boot consoles, real consoles, etc - this is to ensure that end
1270 * users know there might be something in the kernel's log buffer that
1271 * went to the bootconsole (that they do not see on the real console)
1273 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV)) {
1274 /* we need to iterate through twice, to make sure we print
1275 * everything out, before we unregister the console(s)
1277 printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
1278 newcon->name, newcon->index);
1279 for_each_console(bcon)
1280 if (bcon->flags & CON_BOOT)
1281 unregister_console(bcon);
1283 printk(KERN_INFO "%sconsole [%s%d] enabled\n",
1284 (newcon->flags & CON_BOOT) ? "boot" : "" ,
1285 newcon->name, newcon->index);
1288 EXPORT_SYMBOL(register_console);
1290 int unregister_console(struct console *console)
1292 struct console *a, *b;
1295 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1296 if (console->flags & CON_BRL)
1297 return braille_unregister_console(console);
1300 acquire_console_sem();
1301 if (console_drivers == console) {
1302 console_drivers=console->next;
1304 } else if (console_drivers) {
1305 for (a=console_drivers->next, b=console_drivers ;
1306 a; b=a, a=b->next) {
1316 * If this isn't the last console and it has CON_CONSDEV set, we
1317 * need to set it on the next preferred console.
1319 if (console_drivers != NULL && console->flags & CON_CONSDEV)
1320 console_drivers->flags |= CON_CONSDEV;
1322 release_console_sem();
1325 EXPORT_SYMBOL(unregister_console);
1327 static int __init disable_boot_consoles(void)
1329 struct console *con;
1331 for_each_console(con) {
1332 if (con->flags & CON_BOOT) {
1333 printk(KERN_INFO "turn off boot console %s%d\n",
1334 con->name, con->index);
1335 unregister_console(con);
1340 late_initcall(disable_boot_consoles);
1342 #if defined CONFIG_PRINTK
1345 * printk rate limiting, lifted from the networking subsystem.
1347 * This enforces a rate limit: not more than 10 kernel messages
1348 * every 5s to make a denial-of-service attack impossible.
1350 DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
1352 int __printk_ratelimit(const char *func)
1354 return ___ratelimit(&printk_ratelimit_state, func);
1356 EXPORT_SYMBOL(__printk_ratelimit);
1359 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1360 * @caller_jiffies: pointer to caller's state
1361 * @interval_msecs: minimum interval between prints
1363 * printk_timed_ratelimit() returns true if more than @interval_msecs
1364 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1367 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1368 unsigned int interval_msecs)
1370 if (*caller_jiffies == 0
1371 || !time_in_range(jiffies, *caller_jiffies,
1373 + msecs_to_jiffies(interval_msecs))) {
1374 *caller_jiffies = jiffies;
1379 EXPORT_SYMBOL(printk_timed_ratelimit);
1381 static DEFINE_SPINLOCK(dump_list_lock);
1382 static LIST_HEAD(dump_list);
1385 * kmsg_dump_register - register a kernel log dumper.
1386 * @dumper: pointer to the kmsg_dumper structure
1388 * Adds a kernel log dumper to the system. The dump callback in the
1389 * structure will be called when the kernel oopses or panics and must be
1390 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
1392 int kmsg_dump_register(struct kmsg_dumper *dumper)
1394 unsigned long flags;
1397 /* The dump callback needs to be set */
1401 spin_lock_irqsave(&dump_list_lock, flags);
1402 /* Don't allow registering multiple times */
1403 if (!dumper->registered) {
1404 dumper->registered = 1;
1405 list_add_tail(&dumper->list, &dump_list);
1408 spin_unlock_irqrestore(&dump_list_lock, flags);
1412 EXPORT_SYMBOL_GPL(kmsg_dump_register);
1415 * kmsg_dump_unregister - unregister a kmsg dumper.
1416 * @dumper: pointer to the kmsg_dumper structure
1418 * Removes a dump device from the system. Returns zero on success and
1419 * %-EINVAL otherwise.
1421 int kmsg_dump_unregister(struct kmsg_dumper *dumper)
1423 unsigned long flags;
1426 spin_lock_irqsave(&dump_list_lock, flags);
1427 if (dumper->registered) {
1428 dumper->registered = 0;
1429 list_del(&dumper->list);
1432 spin_unlock_irqrestore(&dump_list_lock, flags);
1436 EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
1438 static const char const *kmsg_reasons[] = {
1439 [KMSG_DUMP_OOPS] = "oops",
1440 [KMSG_DUMP_PANIC] = "panic",
1441 [KMSG_DUMP_KEXEC] = "kexec",
1444 static const char *kmsg_to_str(enum kmsg_dump_reason reason)
1446 if (reason >= ARRAY_SIZE(kmsg_reasons) || reason < 0)
1449 return kmsg_reasons[reason];
1453 * kmsg_dump - dump kernel log to kernel message dumpers.
1454 * @reason: the reason (oops, panic etc) for dumping
1456 * Iterate through each of the dump devices and call the oops/panic
1457 * callbacks with the log buffer.
1459 void kmsg_dump(enum kmsg_dump_reason reason)
1463 struct kmsg_dumper *dumper;
1464 const char *s1, *s2;
1465 unsigned long l1, l2;
1466 unsigned long flags;
1467 struct syslog_ns *syslog_ns = current_syslog_ns();
1469 /* Theoretically, the log could move on after we do this, but
1470 there's not a lot we can do about that. The new messages
1471 will overwrite the start of what we dump. */
1472 spin_lock_irqsave(&sys_log_lock, flags);
1473 end = sys_log_end & LOG_BUF_MASK(syslog_ns);
1474 chars = sys_log_logged_chars;
1475 spin_unlock_irqrestore(&sys_log_lock, flags);
1477 if (sys_log_logged_chars > end) {
1478 s1 = sys_log_buf + sys_log_buf_len - sys_log_logged_chars + end;
1479 l1 = sys_log_logged_chars - end;
1487 s2 = sys_log_buf + end - sys_log_logged_chars;
1488 l2 = sys_log_logged_chars;
1491 if (!spin_trylock_irqsave(&dump_list_lock, flags)) {
1492 printk(KERN_ERR "dump_kmsg: dump list lock is held during %s, skipping dump\n",
1493 kmsg_to_str(reason));
1496 list_for_each_entry(dumper, &dump_list, list)
1497 dumper->dump(dumper, reason, s1, l1, s2, l2);
1498 spin_unlock_irqrestore(&dump_list_lock, flags);