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/jiffies.h>
36 #include <linux/suspend.h>
38 #include <asm/uaccess.h>
41 * Architectures can override it:
43 void asmlinkage
__attribute__((weak
)) early_printk(const char *fmt
, ...)
47 #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
49 /* printk's without a loglevel use this.. */
50 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
52 /* We show everything that is MORE important than this.. */
53 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
54 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
56 DECLARE_WAIT_QUEUE_HEAD(log_wait
);
58 int console_printk
[4] = {
59 DEFAULT_CONSOLE_LOGLEVEL
, /* console_loglevel */
60 DEFAULT_MESSAGE_LOGLEVEL
, /* default_message_loglevel */
61 MINIMUM_CONSOLE_LOGLEVEL
, /* minimum_console_loglevel */
62 DEFAULT_CONSOLE_LOGLEVEL
, /* default_console_loglevel */
66 * Low level drivers may need that to know if they can schedule in
67 * their unblank() callback or not. So let's export it.
70 EXPORT_SYMBOL(oops_in_progress
);
73 * console_sem protects the console_drivers list, and also
74 * provides serialisation for access to the entire console
77 static DECLARE_MUTEX(console_sem
);
78 static DECLARE_MUTEX(secondary_console_sem
);
79 struct console
*console_drivers
;
80 EXPORT_SYMBOL_GPL(console_drivers
);
83 * This is used for debugging the mess that is the VT code by
84 * keeping track if we have the console semaphore held. It's
85 * definitely not the perfect debug tool (we don't know if _WE_
86 * hold it are racing, but it helps tracking those weird code
87 * path in the console code where we end up in places I want
88 * locked without the console sempahore held
90 static int console_locked
, console_suspended
;
93 * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
94 * It is also used in interesting ways to provide interlocking in
95 * release_console_sem().
97 static DEFINE_SPINLOCK(logbuf_lock
);
99 #define LOG_BUF_MASK (log_buf_len-1)
100 #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
103 * The indices into log_buf are not constrained to log_buf_len - they
104 * must be masked before subscripting
106 static unsigned log_start
; /* Index into log_buf: next char to be read by syslog() */
107 static unsigned con_start
; /* Index into log_buf: next char to be sent to consoles */
108 static unsigned log_end
; /* Index into log_buf: most-recently-written-char + 1 */
111 * Array of consoles built from command line options (console=)
113 struct console_cmdline
115 char name
[8]; /* Name of the driver */
116 int index
; /* Minor dev. to use */
117 char *options
; /* Options for the driver */
118 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
119 char *brl_options
; /* Options for braille driver */
123 #define MAX_CMDLINECONSOLES 8
125 static struct console_cmdline console_cmdline
[MAX_CMDLINECONSOLES
];
126 static int selected_console
= -1;
127 static int preferred_console
= -1;
128 int console_set_on_cmdline
;
129 EXPORT_SYMBOL(console_set_on_cmdline
);
131 /* Flag: console code may call schedule() */
132 static int console_may_schedule
;
136 static char __log_buf
[__LOG_BUF_LEN
];
137 static char *log_buf
= __log_buf
;
138 static int log_buf_len
= __LOG_BUF_LEN
;
139 static unsigned logged_chars
; /* Number of chars produced since last read+clear operation */
141 static int __init
log_buf_len_setup(char *str
)
143 unsigned size
= memparse(str
, &str
);
147 size
= roundup_pow_of_two(size
);
148 if (size
> log_buf_len
) {
149 unsigned start
, dest_idx
, offset
;
152 new_log_buf
= alloc_bootmem(size
);
154 printk(KERN_WARNING
"log_buf_len: allocation failed\n");
158 spin_lock_irqsave(&logbuf_lock
, flags
);
160 log_buf
= new_log_buf
;
162 offset
= start
= min(con_start
, log_start
);
164 while (start
!= log_end
) {
165 log_buf
[dest_idx
] = __log_buf
[start
& (__LOG_BUF_LEN
- 1)];
172 spin_unlock_irqrestore(&logbuf_lock
, flags
);
174 printk(KERN_NOTICE
"log_buf_len: %d\n", log_buf_len
);
180 __setup("log_buf_len=", log_buf_len_setup
);
182 #ifdef CONFIG_BOOT_PRINTK_DELAY
184 static unsigned int boot_delay
; /* msecs delay after each printk during bootup */
185 static unsigned long long printk_delay_msec
; /* per msec, based on boot_delay */
187 static int __init
boot_delay_setup(char *str
)
190 unsigned long long loops_per_msec
;
192 lpj
= preset_lpj
? preset_lpj
: 1000000; /* some guess */
193 loops_per_msec
= (unsigned long long)lpj
/ 1000 * HZ
;
195 get_option(&str
, &boot_delay
);
196 if (boot_delay
> 10 * 1000)
199 printk_delay_msec
= loops_per_msec
;
200 printk(KERN_DEBUG
"boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
201 "HZ: %d, printk_delay_msec: %llu\n",
202 boot_delay
, preset_lpj
, lpj
, HZ
, printk_delay_msec
);
205 __setup("boot_delay=", boot_delay_setup
);
207 static void boot_delay_msec(void)
209 unsigned long long k
;
210 unsigned long timeout
;
212 if (boot_delay
== 0 || system_state
!= SYSTEM_BOOTING
)
215 k
= (unsigned long long)printk_delay_msec
* boot_delay
;
217 timeout
= jiffies
+ msecs_to_jiffies(boot_delay
);
222 * use (volatile) jiffies to prevent
223 * compiler reduction; loop termination via jiffies
224 * is secondary and may or may not happen.
226 if (time_after(jiffies
, timeout
))
228 touch_nmi_watchdog();
232 static inline void boot_delay_msec(void)
238 * Commands to do_syslog:
240 * 0 -- Close the log. Currently a NOP.
241 * 1 -- Open the log. Currently a NOP.
242 * 2 -- Read from the log.
243 * 3 -- Read all messages remaining in the ring buffer.
244 * 4 -- Read and clear all messages remaining in the ring buffer
245 * 5 -- Clear ring buffer.
246 * 6 -- Disable printk's to console
247 * 7 -- Enable printk's to console
248 * 8 -- Set level of messages printed to console
249 * 9 -- Return number of unread characters in the log buffer
250 * 10 -- Return size of the log buffer
252 int do_syslog(int type
, char __user
*buf
, int len
)
254 unsigned i
, j
, limit
, count
;
259 error
= security_syslog(type
);
264 case 0: /* Close log */
266 case 1: /* Open log */
268 case 2: /* Read from log */
275 if (!access_ok(VERIFY_WRITE
, buf
, len
)) {
279 error
= wait_event_interruptible(log_wait
,
280 (log_start
- log_end
));
284 spin_lock_irq(&logbuf_lock
);
285 while (!error
&& (log_start
!= log_end
) && i
< len
) {
286 c
= LOG_BUF(log_start
);
288 spin_unlock_irq(&logbuf_lock
);
289 error
= __put_user(c
,buf
);
293 spin_lock_irq(&logbuf_lock
);
295 spin_unlock_irq(&logbuf_lock
);
299 case 4: /* Read/clear last kernel messages */
302 case 3: /* Read last kernel messages */
309 if (!access_ok(VERIFY_WRITE
, buf
, len
)) {
314 if (count
> log_buf_len
)
316 spin_lock_irq(&logbuf_lock
);
317 if (count
> logged_chars
)
318 count
= logged_chars
;
323 * __put_user() could sleep, and while we sleep
324 * printk() could overwrite the messages
325 * we try to copy to user space. Therefore
326 * the messages are copied in reverse. <manfreds>
328 for (i
= 0; i
< count
&& !error
; i
++) {
330 if (j
+ log_buf_len
< log_end
)
333 spin_unlock_irq(&logbuf_lock
);
334 error
= __put_user(c
,&buf
[count
-1-i
]);
336 spin_lock_irq(&logbuf_lock
);
338 spin_unlock_irq(&logbuf_lock
);
343 int offset
= count
-error
;
344 /* buffer overflow during copy, correct user buffer. */
345 for (i
= 0; i
< error
; i
++) {
346 if (__get_user(c
,&buf
[i
+offset
]) ||
347 __put_user(c
,&buf
[i
])) {
355 case 5: /* Clear ring buffer */
358 case 6: /* Disable logging to console */
359 console_loglevel
= minimum_console_loglevel
;
361 case 7: /* Enable logging to console */
362 console_loglevel
= default_console_loglevel
;
364 case 8: /* Set level of messages printed to console */
366 if (len
< 1 || len
> 8)
368 if (len
< minimum_console_loglevel
)
369 len
= minimum_console_loglevel
;
370 console_loglevel
= len
;
373 case 9: /* Number of chars in the log buffer */
374 error
= log_end
- log_start
;
376 case 10: /* Size of the log buffer */
387 asmlinkage
long sys_syslog(int type
, char __user
*buf
, int len
)
389 return do_syslog(type
, buf
, len
);
393 * Call the console drivers on a range of log_buf
395 static void __call_console_drivers(unsigned start
, unsigned end
,
396 unsigned int loglevel
)
400 for (con
= console_drivers
; con
; con
= con
->next
) {
401 if ((con
->flags
& CON_ENABLED
) && con
->write
&&
402 (cpu_online(smp_processor_id()) ||
403 (con
->flags
& CON_ANYTIME
)))
404 con
->write(con
, &LOG_BUF(start
), end
- start
, loglevel
);
408 static int __read_mostly ignore_loglevel
;
410 static int __init
ignore_loglevel_setup(char *str
)
413 printk(KERN_INFO
"debug: ignoring loglevel setting.\n");
418 early_param("ignore_loglevel", ignore_loglevel_setup
);
421 * Write out chars from start to end - 1 inclusive
423 static void _call_console_drivers(unsigned start
,
424 unsigned end
, int msg_log_level
)
426 if ((msg_log_level
< console_loglevel
|| ignore_loglevel
) &&
427 console_drivers
&& start
!= end
) {
428 if ((start
& LOG_BUF_MASK
) > (end
& LOG_BUF_MASK
)) {
430 __call_console_drivers(start
& LOG_BUF_MASK
,
431 log_buf_len
, msg_log_level
);
432 __call_console_drivers(0, end
& LOG_BUF_MASK
,
435 __call_console_drivers(start
, end
, msg_log_level
);
441 * Call the console drivers, asking them to write out
442 * log_buf[start] to log_buf[end - 1].
443 * The console_sem must be held.
445 static void call_console_drivers(unsigned start
, unsigned end
)
447 unsigned cur_index
, start_print
;
448 static int msg_level
= -1;
450 BUG_ON(((int)(start
- end
)) > 0);
454 while (cur_index
!= end
) {
455 if (msg_level
< 0 && ((end
- cur_index
) > 2) &&
456 LOG_BUF(cur_index
+ 0) == '<' &&
457 LOG_BUF(cur_index
+ 1) >= '0' &&
458 LOG_BUF(cur_index
+ 1) <= '7' &&
459 LOG_BUF(cur_index
+ 2) == '>') {
460 msg_level
= LOG_BUF(cur_index
+ 1) - '0';
462 start_print
= cur_index
;
464 while (cur_index
!= end
) {
465 char c
= LOG_BUF(cur_index
);
471 * printk() has already given us loglevel tags in
472 * the buffer. This code is here in case the
473 * log buffer has wrapped right round and scribbled
476 msg_level
= default_message_loglevel
;
478 _call_console_drivers(start_print
, cur_index
, msg_level
);
480 start_print
= cur_index
;
485 _call_console_drivers(start_print
, end
, msg_level
);
488 static void emit_log_char(char c
)
490 LOG_BUF(log_end
) = c
;
492 if (log_end
- log_start
> log_buf_len
)
493 log_start
= log_end
- log_buf_len
;
494 if (log_end
- con_start
> log_buf_len
)
495 con_start
= log_end
- log_buf_len
;
496 if (logged_chars
< log_buf_len
)
501 * Zap console related locks when oopsing. Only zap at most once
502 * every 10 seconds, to leave time for slow consoles to print a
505 static void zap_locks(void)
507 static unsigned long oops_timestamp
;
509 if (time_after_eq(jiffies
, oops_timestamp
) &&
510 !time_after(jiffies
, oops_timestamp
+ 30 * HZ
))
513 oops_timestamp
= jiffies
;
515 /* If a crash is occurring, make sure we can't deadlock */
516 spin_lock_init(&logbuf_lock
);
517 /* And make sure that we print immediately */
518 init_MUTEX(&console_sem
);
521 #if defined(CONFIG_PRINTK_TIME)
522 static int printk_time
= 1;
524 static int printk_time
= 0;
526 module_param_named(time
, printk_time
, bool, S_IRUGO
| S_IWUSR
);
528 /* Check if we have any console registered that can be called early in boot. */
529 static int have_callable_console(void)
533 for (con
= console_drivers
; con
; con
= con
->next
)
534 if (con
->flags
& CON_ANYTIME
)
541 * printk - print a kernel message
542 * @fmt: format string
544 * This is printk(). It can be called from any context. We want it to work.
546 * We try to grab the console_sem. If we succeed, it's easy - we log the output and
547 * call the console drivers. If we fail to get the semaphore we place the output
548 * into the log buffer and return. The current holder of the console_sem will
549 * notice the new output in release_console_sem() and will send it to the
550 * consoles before releasing the semaphore.
552 * One effect of this deferred printing is that code which calls printk() and
553 * then changes console_loglevel may break. This is because console_loglevel
554 * is inspected when the actual printing occurs.
559 * See the vsnprintf() documentation for format string extensions over C99.
562 asmlinkage
int printk(const char *fmt
, ...)
568 r
= vprintk(fmt
, args
);
574 /* cpu currently holding logbuf_lock */
575 static volatile unsigned int printk_cpu
= UINT_MAX
;
578 * Can we actually use the console at this time on this cpu?
580 * Console drivers may assume that per-cpu resources have
581 * been allocated. So unless they're explicitly marked as
582 * being able to cope (CON_ANYTIME) don't call them until
583 * this CPU is officially up.
585 static inline int can_use_console(unsigned int cpu
)
587 return cpu_online(cpu
) || have_callable_console();
591 * Try to get console ownership to actually show the kernel
592 * messages from a 'printk'. Return true (and with the
593 * console_semaphore held, and 'console_locked' set) if it
594 * is successful, false otherwise.
596 * This gets called with the 'logbuf_lock' spinlock held and
597 * interrupts disabled. It should return with 'lockbuf_lock'
598 * released but interrupts still disabled.
600 static int acquire_console_semaphore_for_printk(unsigned int cpu
)
604 if (!try_acquire_console_sem()) {
608 * If we can't use the console, we need to release
609 * the console semaphore by hand to avoid flushing
610 * the buffer. We need to hold the console semaphore
611 * in order to do this test safely.
613 if (!can_use_console(cpu
)) {
619 printk_cpu
= UINT_MAX
;
620 spin_unlock(&logbuf_lock
);
623 static const char recursion_bug_msg
[] =
624 KERN_CRIT
"BUG: recent printk recursion!\n";
625 static int recursion_bug
;
626 static int new_text_line
= 1;
627 static char printk_buf
[1024];
629 asmlinkage
int vprintk(const char *fmt
, va_list args
)
632 int current_log_level
= default_message_loglevel
;
640 /* This stops the holder of console_sem just where we want him */
641 raw_local_irq_save(flags
);
642 this_cpu
= smp_processor_id();
645 * Ouch, printk recursed into itself!
647 if (unlikely(printk_cpu
== this_cpu
)) {
649 * If a crash is occurring during printk() on this CPU,
650 * then try to get the crash message out but make sure
651 * we can't deadlock. Otherwise just return to avoid the
652 * recursion and return - but flag the recursion so that
653 * it can be printed at the next appropriate moment:
655 if (!oops_in_progress
) {
657 goto out_restore_irqs
;
663 spin_lock(&logbuf_lock
);
664 printk_cpu
= this_cpu
;
668 strcpy(printk_buf
, recursion_bug_msg
);
669 printed_len
= sizeof(recursion_bug_msg
);
671 /* Emit the output into the temporary buffer */
672 printed_len
+= vscnprintf(printk_buf
+ printed_len
,
673 sizeof(printk_buf
) - printed_len
, fmt
, args
);
677 * Copy the output into log_buf. If the caller didn't provide
678 * appropriate log level tags, we insert them here
680 for (p
= printk_buf
; *p
; p
++) {
682 /* If a token, set current_log_level and skip over */
683 if (p
[0] == '<' && p
[1] >= '0' && p
[1] <= '7' &&
685 current_log_level
= p
[1] - '0';
690 /* Always output the token */
692 emit_log_char(current_log_level
+ '0');
698 /* Follow the token with the time */
701 unsigned long long t
;
702 unsigned long nanosec_rem
;
704 t
= cpu_clock(printk_cpu
);
705 nanosec_rem
= do_div(t
, 1000000000);
706 tlen
= sprintf(tbuf
, "[%5lu.%06lu] ",
710 for (tp
= tbuf
; tp
< tbuf
+ tlen
; tp
++)
725 * Try to acquire and then immediately release the
726 * console semaphore. The release will do all the
727 * actual magic (print out buffers, wake up klogd,
730 * The acquire_console_semaphore_for_printk() function
731 * will release 'logbuf_lock' regardless of whether it
732 * actually gets the semaphore or not.
734 if (acquire_console_semaphore_for_printk(this_cpu
))
735 release_console_sem();
739 raw_local_irq_restore(flags
);
744 EXPORT_SYMBOL(printk
);
745 EXPORT_SYMBOL(vprintk
);
749 asmlinkage
long sys_syslog(int type
, char __user
*buf
, int len
)
754 static void call_console_drivers(unsigned start
, unsigned end
)
760 static int __add_preferred_console(char *name
, int idx
, char *options
,
763 struct console_cmdline
*c
;
767 * See if this tty is not yet registered, and
768 * if we have a slot free.
770 for (i
= 0; i
< MAX_CMDLINECONSOLES
&& console_cmdline
[i
].name
[0]; i
++)
771 if (strcmp(console_cmdline
[i
].name
, name
) == 0 &&
772 console_cmdline
[i
].index
== idx
) {
774 selected_console
= i
;
777 if (i
== MAX_CMDLINECONSOLES
)
780 selected_console
= i
;
781 c
= &console_cmdline
[i
];
782 strlcpy(c
->name
, name
, sizeof(c
->name
));
783 c
->options
= options
;
784 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
785 c
->brl_options
= brl_options
;
791 * Set up a list of consoles. Called from init/main.c
793 static int __init
console_setup(char *str
)
795 char buf
[sizeof(console_cmdline
[0].name
) + 4]; /* 4 for index */
796 char *s
, *options
, *brl_options
= NULL
;
799 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
800 if (!memcmp(str
, "brl,", 4)) {
803 } else if (!memcmp(str
, "brl=", 4)) {
804 brl_options
= str
+ 4;
805 str
= strchr(brl_options
, ',');
807 printk(KERN_ERR
"need port name after brl=\n");
815 * Decode str into name, index, options.
817 if (str
[0] >= '0' && str
[0] <= '9') {
819 strncpy(buf
+ 4, str
, sizeof(buf
) - 5);
821 strncpy(buf
, str
, sizeof(buf
) - 1);
823 buf
[sizeof(buf
) - 1] = 0;
824 if ((options
= strchr(str
, ',')) != NULL
)
827 if (!strcmp(str
, "ttya"))
828 strcpy(buf
, "ttyS0");
829 if (!strcmp(str
, "ttyb"))
830 strcpy(buf
, "ttyS1");
832 for (s
= buf
; *s
; s
++)
833 if ((*s
>= '0' && *s
<= '9') || *s
== ',')
835 idx
= simple_strtoul(s
, NULL
, 10);
838 __add_preferred_console(buf
, idx
, options
, brl_options
);
839 console_set_on_cmdline
= 1;
842 __setup("console=", console_setup
);
845 * add_preferred_console - add a device to the list of preferred consoles.
848 * @options: options for this console
850 * The last preferred console added will be used for kernel messages
851 * and stdin/out/err for init. Normally this is used by console_setup
852 * above to handle user-supplied console arguments; however it can also
853 * be used by arch-specific code either to override the user or more
854 * commonly to provide a default console (ie from PROM variables) when
855 * the user has not supplied one.
857 int add_preferred_console(char *name
, int idx
, char *options
)
859 return __add_preferred_console(name
, idx
, options
, NULL
);
862 int update_console_cmdline(char *name
, int idx
, char *name_new
, int idx_new
, char *options
)
864 struct console_cmdline
*c
;
867 for (i
= 0; i
< MAX_CMDLINECONSOLES
&& console_cmdline
[i
].name
[0]; i
++)
868 if (strcmp(console_cmdline
[i
].name
, name
) == 0 &&
869 console_cmdline
[i
].index
== idx
) {
870 c
= &console_cmdline
[i
];
871 strlcpy(c
->name
, name_new
, sizeof(c
->name
));
872 c
->name
[sizeof(c
->name
) - 1] = 0;
873 c
->options
= options
;
881 int console_suspend_enabled
= 1;
882 EXPORT_SYMBOL(console_suspend_enabled
);
884 static int __init
console_suspend_disable(char *str
)
886 console_suspend_enabled
= 0;
889 __setup("no_console_suspend", console_suspend_disable
);
892 * suspend_console - suspend the console subsystem
894 * This disables printk() while we go into suspend states
896 void suspend_console(void)
898 if (!console_suspend_enabled
)
900 printk("Suspending console(s) (use no_console_suspend to debug)\n");
901 acquire_console_sem();
902 console_suspended
= 1;
904 EXPORT_IF_TOI_MODULAR(suspend_console
);
906 void resume_console(void)
908 if (!console_suspend_enabled
)
910 console_suspended
= 0;
911 release_console_sem();
913 EXPORT_IF_TOI_MODULAR(resume_console
);
916 * acquire_console_sem - lock the console system for exclusive use.
918 * Acquires a semaphore which guarantees that the caller has
919 * exclusive access to the console system and the console_drivers list.
921 * Can sleep, returns nothing.
923 void acquire_console_sem(void)
925 BUG_ON(in_interrupt());
926 if (console_suspended
) {
927 down(&secondary_console_sem
);
932 console_may_schedule
= 1;
934 EXPORT_SYMBOL(acquire_console_sem
);
936 int try_acquire_console_sem(void)
938 if (down_trylock(&console_sem
))
941 console_may_schedule
= 0;
944 EXPORT_SYMBOL(try_acquire_console_sem
);
946 int is_console_locked(void)
948 return console_locked
;
951 static DEFINE_PER_CPU(int, printk_pending
);
953 void printk_tick(void)
955 if (__get_cpu_var(printk_pending
)) {
956 __get_cpu_var(printk_pending
) = 0;
957 wake_up_interruptible(&log_wait
);
961 int printk_needs_cpu(int cpu
)
963 return per_cpu(printk_pending
, cpu
);
966 void wake_up_klogd(void)
968 if (waitqueue_active(&log_wait
))
969 __raw_get_cpu_var(printk_pending
) = 1;
973 * release_console_sem - unlock the console system
975 * Releases the semaphore which the caller holds on the console system
976 * and the console driver list.
978 * While the semaphore was held, console output may have been buffered
979 * by printk(). If this is the case, release_console_sem() emits
980 * the output prior to releasing the semaphore.
982 * If there is output waiting for klogd, we wake it up.
984 * release_console_sem() may be called from any context.
986 void release_console_sem(void)
989 unsigned _con_start
, _log_end
;
990 unsigned wake_klogd
= 0;
992 if (console_suspended
) {
993 up(&secondary_console_sem
);
997 console_may_schedule
= 0;
1000 spin_lock_irqsave(&logbuf_lock
, flags
);
1001 wake_klogd
|= log_start
- log_end
;
1002 if (con_start
== log_end
)
1003 break; /* Nothing to print */
1004 _con_start
= con_start
;
1006 con_start
= log_end
; /* Flush */
1007 spin_unlock(&logbuf_lock
);
1008 stop_critical_timings(); /* don't trace print latency */
1009 call_console_drivers(_con_start
, _log_end
);
1010 start_critical_timings();
1011 local_irq_restore(flags
);
1015 spin_unlock_irqrestore(&logbuf_lock
, flags
);
1019 EXPORT_SYMBOL(release_console_sem
);
1022 * console_conditional_schedule - yield the CPU if required
1024 * If the console code is currently allowed to sleep, and
1025 * if this CPU should yield the CPU to another task, do
1028 * Must be called within acquire_console_sem().
1030 void __sched
console_conditional_schedule(void)
1032 if (console_may_schedule
)
1035 EXPORT_SYMBOL(console_conditional_schedule
);
1037 void console_print(const char *s
)
1039 printk(KERN_EMERG
"%s", s
);
1041 EXPORT_SYMBOL(console_print
);
1043 void console_unblank(void)
1048 * console_unblank can no longer be called in interrupt context unless
1049 * oops_in_progress is set to 1..
1051 if (oops_in_progress
) {
1052 if (down_trylock(&console_sem
) != 0)
1055 acquire_console_sem();
1058 console_may_schedule
= 0;
1059 for (c
= console_drivers
; c
!= NULL
; c
= c
->next
)
1060 if ((c
->flags
& CON_ENABLED
) && c
->unblank
)
1062 release_console_sem();
1066 * Return the console tty driver structure and its associated index
1068 struct tty_driver
*console_device(int *index
)
1071 struct tty_driver
*driver
= NULL
;
1073 acquire_console_sem();
1074 for (c
= console_drivers
; c
!= NULL
; c
= c
->next
) {
1077 driver
= c
->device(c
, index
);
1081 release_console_sem();
1086 * Prevent further output on the passed console device so that (for example)
1087 * serial drivers can disable console output before suspending a port, and can
1088 * re-enable output afterwards.
1090 void console_stop(struct console
*console
)
1092 acquire_console_sem();
1093 console
->flags
&= ~CON_ENABLED
;
1094 release_console_sem();
1096 EXPORT_SYMBOL(console_stop
);
1098 void console_start(struct console
*console
)
1100 acquire_console_sem();
1101 console
->flags
|= CON_ENABLED
;
1102 release_console_sem();
1104 EXPORT_SYMBOL(console_start
);
1107 * The console driver calls this routine during kernel initialization
1108 * to register the console printing procedure with printk() and to
1109 * print any messages that were printed by the kernel before the
1110 * console driver was initialized.
1112 void register_console(struct console
*console
)
1115 unsigned long flags
;
1116 struct console
*bootconsole
= NULL
;
1118 if (console_drivers
) {
1119 if (console
->flags
& CON_BOOT
)
1121 if (console_drivers
->flags
& CON_BOOT
)
1122 bootconsole
= console_drivers
;
1125 if (preferred_console
< 0 || bootconsole
|| !console_drivers
)
1126 preferred_console
= selected_console
;
1128 if (console
->early_setup
)
1129 console
->early_setup();
1132 * See if we want to use this console driver. If we
1133 * didn't select a console we take the first one
1134 * that registers here.
1136 if (preferred_console
< 0) {
1137 if (console
->index
< 0)
1139 if (console
->setup
== NULL
||
1140 console
->setup(console
, NULL
) == 0) {
1141 console
->flags
|= CON_ENABLED
;
1142 if (console
->device
) {
1143 console
->flags
|= CON_CONSDEV
;
1144 preferred_console
= 0;
1150 * See if this console matches one we selected on
1153 for (i
= 0; i
< MAX_CMDLINECONSOLES
&& console_cmdline
[i
].name
[0];
1155 if (strcmp(console_cmdline
[i
].name
, console
->name
) != 0)
1157 if (console
->index
>= 0 &&
1158 console
->index
!= console_cmdline
[i
].index
)
1160 if (console
->index
< 0)
1161 console
->index
= console_cmdline
[i
].index
;
1162 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1163 if (console_cmdline
[i
].brl_options
) {
1164 console
->flags
|= CON_BRL
;
1165 braille_register_console(console
,
1166 console_cmdline
[i
].index
,
1167 console_cmdline
[i
].options
,
1168 console_cmdline
[i
].brl_options
);
1172 if (console
->setup
&&
1173 console
->setup(console
, console_cmdline
[i
].options
) != 0)
1175 console
->flags
|= CON_ENABLED
;
1176 console
->index
= console_cmdline
[i
].index
;
1177 if (i
== selected_console
) {
1178 console
->flags
|= CON_CONSDEV
;
1179 preferred_console
= selected_console
;
1184 if (!(console
->flags
& CON_ENABLED
))
1187 if (bootconsole
&& (console
->flags
& CON_CONSDEV
)) {
1188 printk(KERN_INFO
"console handover: boot [%s%d] -> real [%s%d]\n",
1189 bootconsole
->name
, bootconsole
->index
,
1190 console
->name
, console
->index
);
1191 unregister_console(bootconsole
);
1192 console
->flags
&= ~CON_PRINTBUFFER
;
1194 printk(KERN_INFO
"console [%s%d] enabled\n",
1195 console
->name
, console
->index
);
1199 * Put this console in the list - keep the
1200 * preferred driver at the head of the list.
1202 acquire_console_sem();
1203 if ((console
->flags
& CON_CONSDEV
) || console_drivers
== NULL
) {
1204 console
->next
= console_drivers
;
1205 console_drivers
= console
;
1207 console
->next
->flags
&= ~CON_CONSDEV
;
1209 console
->next
= console_drivers
->next
;
1210 console_drivers
->next
= console
;
1212 if (console
->flags
& CON_PRINTBUFFER
) {
1214 * release_console_sem() will print out the buffered messages
1217 spin_lock_irqsave(&logbuf_lock
, flags
);
1218 con_start
= log_start
;
1219 spin_unlock_irqrestore(&logbuf_lock
, flags
);
1221 release_console_sem();
1223 EXPORT_SYMBOL(register_console
);
1225 int unregister_console(struct console
*console
)
1227 struct console
*a
, *b
;
1230 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1231 if (console
->flags
& CON_BRL
)
1232 return braille_unregister_console(console
);
1235 acquire_console_sem();
1236 if (console_drivers
== console
) {
1237 console_drivers
=console
->next
;
1239 } else if (console_drivers
) {
1240 for (a
=console_drivers
->next
, b
=console_drivers
;
1241 a
; b
=a
, a
=b
->next
) {
1251 * If this isn't the last console and it has CON_CONSDEV set, we
1252 * need to set it on the next preferred console.
1254 if (console_drivers
!= NULL
&& console
->flags
& CON_CONSDEV
)
1255 console_drivers
->flags
|= CON_CONSDEV
;
1257 release_console_sem();
1260 EXPORT_SYMBOL(unregister_console
);
1262 static int __init
disable_boot_consoles(void)
1264 if (console_drivers
!= NULL
) {
1265 if (console_drivers
->flags
& CON_BOOT
) {
1266 printk(KERN_INFO
"turn off boot console %s%d\n",
1267 console_drivers
->name
, console_drivers
->index
);
1268 return unregister_console(console_drivers
);
1273 late_initcall(disable_boot_consoles
);
1275 #if defined CONFIG_PRINTK
1278 * printk rate limiting, lifted from the networking subsystem.
1280 * This enforces a rate limit: not more than 10 kernel messages
1281 * every 5s to make a denial-of-service attack impossible.
1283 DEFINE_RATELIMIT_STATE(printk_ratelimit_state
, 5 * HZ
, 10);
1285 int printk_ratelimit(void)
1287 return __ratelimit(&printk_ratelimit_state
);
1289 EXPORT_SYMBOL(printk_ratelimit
);
1292 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1293 * @caller_jiffies: pointer to caller's state
1294 * @interval_msecs: minimum interval between prints
1296 * printk_timed_ratelimit() returns true if more than @interval_msecs
1297 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1300 bool printk_timed_ratelimit(unsigned long *caller_jiffies
,
1301 unsigned int interval_msecs
)
1303 if (*caller_jiffies
== 0 || time_after(jiffies
, *caller_jiffies
)) {
1304 *caller_jiffies
= jiffies
+ msecs_to_jiffies(interval_msecs
);
1309 EXPORT_SYMBOL(printk_timed_ratelimit
);