printk: make printk more robust by not allowing recursion
[linux-2.6.git] / kernel / printk.c
1 /*
2  *  linux/kernel/printk.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *
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).
11  * Ted Ts'o, 2/11/93.
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 <andrewm@uow.edu.au>
17  */
18
19 #include <linux/kernel.h>
20 #include <linux/mm.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
37 #include <asm/uaccess.h>
38
39 #define __LOG_BUF_LEN   (1 << CONFIG_LOG_BUF_SHIFT)
40
41 /* printk's without a loglevel use this.. */
42 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
43
44 /* We show everything that is MORE important than this.. */
45 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
46 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
47
48 DECLARE_WAIT_QUEUE_HEAD(log_wait);
49
50 int console_printk[4] = {
51         DEFAULT_CONSOLE_LOGLEVEL,       /* console_loglevel */
52         DEFAULT_MESSAGE_LOGLEVEL,       /* default_message_loglevel */
53         MINIMUM_CONSOLE_LOGLEVEL,       /* minimum_console_loglevel */
54         DEFAULT_CONSOLE_LOGLEVEL,       /* default_console_loglevel */
55 };
56
57 /*
58  * Low level drivers may need that to know if they can schedule in
59  * their unblank() callback or not. So let's export it.
60  */
61 int oops_in_progress;
62 EXPORT_SYMBOL(oops_in_progress);
63
64 /*
65  * console_sem protects the console_drivers list, and also
66  * provides serialisation for access to the entire console
67  * driver system.
68  */
69 static DECLARE_MUTEX(console_sem);
70 static DECLARE_MUTEX(secondary_console_sem);
71 struct console *console_drivers;
72 /*
73  * This is used for debugging the mess that is the VT code by
74  * keeping track if we have the console semaphore held. It's
75  * definitely not the perfect debug tool (we don't know if _WE_
76  * hold it are racing, but it helps tracking those weird code
77  * path in the console code where we end up in places I want
78  * locked without the console sempahore held
79  */
80 static int console_locked, console_suspended;
81
82 /*
83  * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
84  * It is also used in interesting ways to provide interlocking in
85  * release_console_sem().
86  */
87 static DEFINE_SPINLOCK(logbuf_lock);
88
89 #define LOG_BUF_MASK    (log_buf_len-1)
90 #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
91
92 /*
93  * The indices into log_buf are not constrained to log_buf_len - they
94  * must be masked before subscripting
95  */
96 static unsigned long log_start; /* Index into log_buf: next char to be read by syslog() */
97 static unsigned long con_start; /* Index into log_buf: next char to be sent to consoles */
98 static unsigned long log_end;   /* Index into log_buf: most-recently-written-char + 1 */
99
100 /*
101  *      Array of consoles built from command line options (console=)
102  */
103 struct console_cmdline
104 {
105         char    name[8];                        /* Name of the driver       */
106         int     index;                          /* Minor dev. to use        */
107         char    *options;                       /* Options for the driver   */
108 };
109
110 #define MAX_CMDLINECONSOLES 8
111
112 static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
113 static int selected_console = -1;
114 static int preferred_console = -1;
115
116 /* Flag: console code may call schedule() */
117 static int console_may_schedule;
118
119 #ifdef CONFIG_PRINTK
120
121 static char __log_buf[__LOG_BUF_LEN];
122 static char *log_buf = __log_buf;
123 static int log_buf_len = __LOG_BUF_LEN;
124 static unsigned long logged_chars; /* Number of chars produced since last read+clear operation */
125
126 static int __init log_buf_len_setup(char *str)
127 {
128         unsigned long size = memparse(str, &str);
129         unsigned long flags;
130
131         if (size)
132                 size = roundup_pow_of_two(size);
133         if (size > log_buf_len) {
134                 unsigned long start, dest_idx, offset;
135                 char *new_log_buf;
136
137                 new_log_buf = alloc_bootmem(size);
138                 if (!new_log_buf) {
139                         printk(KERN_WARNING "log_buf_len: allocation failed\n");
140                         goto out;
141                 }
142
143                 spin_lock_irqsave(&logbuf_lock, flags);
144                 log_buf_len = size;
145                 log_buf = new_log_buf;
146
147                 offset = start = min(con_start, log_start);
148                 dest_idx = 0;
149                 while (start != log_end) {
150                         log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
151                         start++;
152                         dest_idx++;
153                 }
154                 log_start -= offset;
155                 con_start -= offset;
156                 log_end -= offset;
157                 spin_unlock_irqrestore(&logbuf_lock, flags);
158
159                 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
160         }
161 out:
162         return 1;
163 }
164
165 __setup("log_buf_len=", log_buf_len_setup);
166
167 #ifdef CONFIG_BOOT_PRINTK_DELAY
168
169 static unsigned int boot_delay; /* msecs delay after each printk during bootup */
170 static unsigned long long printk_delay_msec; /* per msec, based on boot_delay */
171
172 static int __init boot_delay_setup(char *str)
173 {
174         unsigned long lpj;
175         unsigned long long loops_per_msec;
176
177         lpj = preset_lpj ? preset_lpj : 1000000;        /* some guess */
178         loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
179
180         get_option(&str, &boot_delay);
181         if (boot_delay > 10 * 1000)
182                 boot_delay = 0;
183
184         printk_delay_msec = loops_per_msec;
185         printk(KERN_DEBUG "boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
186                 "HZ: %d, printk_delay_msec: %llu\n",
187                 boot_delay, preset_lpj, lpj, HZ, printk_delay_msec);
188         return 1;
189 }
190 __setup("boot_delay=", boot_delay_setup);
191
192 static void boot_delay_msec(void)
193 {
194         unsigned long long k;
195         unsigned long timeout;
196
197         if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
198                 return;
199
200         k = (unsigned long long)printk_delay_msec * boot_delay;
201
202         timeout = jiffies + msecs_to_jiffies(boot_delay);
203         while (k) {
204                 k--;
205                 cpu_relax();
206                 /*
207                  * use (volatile) jiffies to prevent
208                  * compiler reduction; loop termination via jiffies
209                  * is secondary and may or may not happen.
210                  */
211                 if (time_after(jiffies, timeout))
212                         break;
213                 touch_nmi_watchdog();
214         }
215 }
216 #else
217 static inline void boot_delay_msec(void)
218 {
219 }
220 #endif
221
222 /*
223  * Return the number of unread characters in the log buffer.
224  */
225 int log_buf_get_len(void)
226 {
227         return logged_chars;
228 }
229
230 /*
231  * Copy a range of characters from the log buffer.
232  */
233 int log_buf_copy(char *dest, int idx, int len)
234 {
235         int ret, max;
236         bool took_lock = false;
237
238         if (!oops_in_progress) {
239                 spin_lock_irq(&logbuf_lock);
240                 took_lock = true;
241         }
242
243         max = log_buf_get_len();
244         if (idx < 0 || idx >= max) {
245                 ret = -1;
246         } else {
247                 if (len > max)
248                         len = max;
249                 ret = len;
250                 idx += (log_end - max);
251                 while (len-- > 0)
252                         dest[len] = LOG_BUF(idx + len);
253         }
254
255         if (took_lock)
256                 spin_unlock_irq(&logbuf_lock);
257
258         return ret;
259 }
260
261 /*
262  * Extract a single character from the log buffer.
263  */
264 int log_buf_read(int idx)
265 {
266         char ret;
267
268         if (log_buf_copy(&ret, idx, 1) == 1)
269                 return ret;
270         else
271                 return -1;
272 }
273
274 /*
275  * Commands to do_syslog:
276  *
277  *      0 -- Close the log.  Currently a NOP.
278  *      1 -- Open the log. Currently a NOP.
279  *      2 -- Read from the log.
280  *      3 -- Read all messages remaining in the ring buffer.
281  *      4 -- Read and clear all messages remaining in the ring buffer
282  *      5 -- Clear ring buffer.
283  *      6 -- Disable printk's to console
284  *      7 -- Enable printk's to console
285  *      8 -- Set level of messages printed to console
286  *      9 -- Return number of unread characters in the log buffer
287  *     10 -- Return size of the log buffer
288  */
289 int do_syslog(int type, char __user *buf, int len)
290 {
291         unsigned long i, j, limit, count;
292         int do_clear = 0;
293         char c;
294         int error = 0;
295
296         error = security_syslog(type);
297         if (error)
298                 return error;
299
300         switch (type) {
301         case 0:         /* Close log */
302                 break;
303         case 1:         /* Open log */
304                 break;
305         case 2:         /* Read from log */
306                 error = -EINVAL;
307                 if (!buf || len < 0)
308                         goto out;
309                 error = 0;
310                 if (!len)
311                         goto out;
312                 if (!access_ok(VERIFY_WRITE, buf, len)) {
313                         error = -EFAULT;
314                         goto out;
315                 }
316                 error = wait_event_interruptible(log_wait,
317                                                         (log_start - log_end));
318                 if (error)
319                         goto out;
320                 i = 0;
321                 spin_lock_irq(&logbuf_lock);
322                 while (!error && (log_start != log_end) && i < len) {
323                         c = LOG_BUF(log_start);
324                         log_start++;
325                         spin_unlock_irq(&logbuf_lock);
326                         error = __put_user(c,buf);
327                         buf++;
328                         i++;
329                         cond_resched();
330                         spin_lock_irq(&logbuf_lock);
331                 }
332                 spin_unlock_irq(&logbuf_lock);
333                 if (!error)
334                         error = i;
335                 break;
336         case 4:         /* Read/clear last kernel messages */
337                 do_clear = 1;
338                 /* FALL THRU */
339         case 3:         /* Read last kernel messages */
340                 error = -EINVAL;
341                 if (!buf || len < 0)
342                         goto out;
343                 error = 0;
344                 if (!len)
345                         goto out;
346                 if (!access_ok(VERIFY_WRITE, buf, len)) {
347                         error = -EFAULT;
348                         goto out;
349                 }
350                 count = len;
351                 if (count > log_buf_len)
352                         count = log_buf_len;
353                 spin_lock_irq(&logbuf_lock);
354                 if (count > logged_chars)
355                         count = logged_chars;
356                 if (do_clear)
357                         logged_chars = 0;
358                 limit = log_end;
359                 /*
360                  * __put_user() could sleep, and while we sleep
361                  * printk() could overwrite the messages
362                  * we try to copy to user space. Therefore
363                  * the messages are copied in reverse. <manfreds>
364                  */
365                 for (i = 0; i < count && !error; i++) {
366                         j = limit-1-i;
367                         if (j + log_buf_len < log_end)
368                                 break;
369                         c = LOG_BUF(j);
370                         spin_unlock_irq(&logbuf_lock);
371                         error = __put_user(c,&buf[count-1-i]);
372                         cond_resched();
373                         spin_lock_irq(&logbuf_lock);
374                 }
375                 spin_unlock_irq(&logbuf_lock);
376                 if (error)
377                         break;
378                 error = i;
379                 if (i != count) {
380                         int offset = count-error;
381                         /* buffer overflow during copy, correct user buffer. */
382                         for (i = 0; i < error; i++) {
383                                 if (__get_user(c,&buf[i+offset]) ||
384                                     __put_user(c,&buf[i])) {
385                                         error = -EFAULT;
386                                         break;
387                                 }
388                                 cond_resched();
389                         }
390                 }
391                 break;
392         case 5:         /* Clear ring buffer */
393                 logged_chars = 0;
394                 break;
395         case 6:         /* Disable logging to console */
396                 console_loglevel = minimum_console_loglevel;
397                 break;
398         case 7:         /* Enable logging to console */
399                 console_loglevel = default_console_loglevel;
400                 break;
401         case 8:         /* Set level of messages printed to console */
402                 error = -EINVAL;
403                 if (len < 1 || len > 8)
404                         goto out;
405                 if (len < minimum_console_loglevel)
406                         len = minimum_console_loglevel;
407                 console_loglevel = len;
408                 error = 0;
409                 break;
410         case 9:         /* Number of chars in the log buffer */
411                 error = log_end - log_start;
412                 break;
413         case 10:        /* Size of the log buffer */
414                 error = log_buf_len;
415                 break;
416         default:
417                 error = -EINVAL;
418                 break;
419         }
420 out:
421         return error;
422 }
423
424 asmlinkage long sys_syslog(int type, char __user *buf, int len)
425 {
426         return do_syslog(type, buf, len);
427 }
428
429 /*
430  * Call the console drivers on a range of log_buf
431  */
432 static void __call_console_drivers(unsigned long start, unsigned long end)
433 {
434         struct console *con;
435
436         for (con = console_drivers; con; con = con->next) {
437                 if ((con->flags & CON_ENABLED) && con->write &&
438                                 (cpu_online(smp_processor_id()) ||
439                                 (con->flags & CON_ANYTIME)))
440                         con->write(con, &LOG_BUF(start), end - start);
441         }
442 }
443
444 static int __read_mostly ignore_loglevel;
445
446 static int __init ignore_loglevel_setup(char *str)
447 {
448         ignore_loglevel = 1;
449         printk(KERN_INFO "debug: ignoring loglevel setting.\n");
450
451         return 1;
452 }
453
454 __setup("ignore_loglevel", ignore_loglevel_setup);
455
456 /*
457  * Write out chars from start to end - 1 inclusive
458  */
459 static void _call_console_drivers(unsigned long start,
460                                 unsigned long end, int msg_log_level)
461 {
462         if ((msg_log_level < console_loglevel || ignore_loglevel) &&
463                         console_drivers && start != end) {
464                 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
465                         /* wrapped write */
466                         __call_console_drivers(start & LOG_BUF_MASK,
467                                                 log_buf_len);
468                         __call_console_drivers(0, end & LOG_BUF_MASK);
469                 } else {
470                         __call_console_drivers(start, end);
471                 }
472         }
473 }
474
475 /*
476  * Call the console drivers, asking them to write out
477  * log_buf[start] to log_buf[end - 1].
478  * The console_sem must be held.
479  */
480 static void call_console_drivers(unsigned long start, unsigned long end)
481 {
482         unsigned long cur_index, start_print;
483         static int msg_level = -1;
484
485         BUG_ON(((long)(start - end)) > 0);
486
487         cur_index = start;
488         start_print = start;
489         while (cur_index != end) {
490                 if (msg_level < 0 && ((end - cur_index) > 2) &&
491                                 LOG_BUF(cur_index + 0) == '<' &&
492                                 LOG_BUF(cur_index + 1) >= '0' &&
493                                 LOG_BUF(cur_index + 1) <= '7' &&
494                                 LOG_BUF(cur_index + 2) == '>') {
495                         msg_level = LOG_BUF(cur_index + 1) - '0';
496                         cur_index += 3;
497                         start_print = cur_index;
498                 }
499                 while (cur_index != end) {
500                         char c = LOG_BUF(cur_index);
501
502                         cur_index++;
503                         if (c == '\n') {
504                                 if (msg_level < 0) {
505                                         /*
506                                          * printk() has already given us loglevel tags in
507                                          * the buffer.  This code is here in case the
508                                          * log buffer has wrapped right round and scribbled
509                                          * on those tags
510                                          */
511                                         msg_level = default_message_loglevel;
512                                 }
513                                 _call_console_drivers(start_print, cur_index, msg_level);
514                                 msg_level = -1;
515                                 start_print = cur_index;
516                                 break;
517                         }
518                 }
519         }
520         _call_console_drivers(start_print, end, msg_level);
521 }
522
523 static void emit_log_char(char c)
524 {
525         LOG_BUF(log_end) = c;
526         log_end++;
527         if (log_end - log_start > log_buf_len)
528                 log_start = log_end - log_buf_len;
529         if (log_end - con_start > log_buf_len)
530                 con_start = log_end - log_buf_len;
531         if (logged_chars < log_buf_len)
532                 logged_chars++;
533 }
534
535 /*
536  * Zap console related locks when oopsing. Only zap at most once
537  * every 10 seconds, to leave time for slow consoles to print a
538  * full oops.
539  */
540 static void zap_locks(void)
541 {
542         static unsigned long oops_timestamp;
543
544         if (time_after_eq(jiffies, oops_timestamp) &&
545                         !time_after(jiffies, oops_timestamp + 30 * HZ))
546                 return;
547
548         oops_timestamp = jiffies;
549
550         /* If a crash is occurring, make sure we can't deadlock */
551         spin_lock_init(&logbuf_lock);
552         /* And make sure that we print immediately */
553         init_MUTEX(&console_sem);
554 }
555
556 #if defined(CONFIG_PRINTK_TIME)
557 static int printk_time = 1;
558 #else
559 static int printk_time = 0;
560 #endif
561 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
562
563 static int __init printk_time_setup(char *str)
564 {
565         if (*str)
566                 return 0;
567         printk_time = 1;
568         printk(KERN_NOTICE "The 'time' option is deprecated and "
569                 "is scheduled for removal in early 2008\n");
570         printk(KERN_NOTICE "Use 'printk.time=<value>' instead\n");
571         return 1;
572 }
573
574 __setup("time", printk_time_setup);
575
576 __attribute__((weak)) unsigned long long printk_clock(void)
577 {
578         return sched_clock();
579 }
580
581 /* Check if we have any console registered that can be called early in boot. */
582 static int have_callable_console(void)
583 {
584         struct console *con;
585
586         for (con = console_drivers; con; con = con->next)
587                 if (con->flags & CON_ANYTIME)
588                         return 1;
589
590         return 0;
591 }
592
593 /**
594  * printk - print a kernel message
595  * @fmt: format string
596  *
597  * This is printk().  It can be called from any context.  We want it to work.
598  * Be aware of the fact that if oops_in_progress is not set, we might try to
599  * wake klogd up which could deadlock on runqueue lock if printk() is called
600  * from scheduler code.
601  *
602  * We try to grab the console_sem.  If we succeed, it's easy - we log the output and
603  * call the console drivers.  If we fail to get the semaphore we place the output
604  * into the log buffer and return.  The current holder of the console_sem will
605  * notice the new output in release_console_sem() and will send it to the
606  * consoles before releasing the semaphore.
607  *
608  * One effect of this deferred printing is that code which calls printk() and
609  * then changes console_loglevel may break. This is because console_loglevel
610  * is inspected when the actual printing occurs.
611  *
612  * See also:
613  * printf(3)
614  */
615
616 asmlinkage int printk(const char *fmt, ...)
617 {
618         va_list args;
619         int r;
620
621         va_start(args, fmt);
622         r = vprintk(fmt, args);
623         va_end(args);
624
625         return r;
626 }
627
628 /* cpu currently holding logbuf_lock */
629 static volatile unsigned int printk_cpu = UINT_MAX;
630
631 const char printk_recursion_bug_msg [] =
632                         KERN_CRIT "BUG: recent printk recursion!\n";
633 static int printk_recursion_bug;
634
635 asmlinkage int vprintk(const char *fmt, va_list args)
636 {
637         static int log_level_unknown = 1;
638         static char printk_buf[1024];
639
640         unsigned long flags;
641         int printed_len = 0;
642         int this_cpu;
643         char *p;
644
645         boot_delay_msec();
646
647         preempt_disable();
648         /* This stops the holder of console_sem just where we want him */
649         raw_local_irq_save(flags);
650         this_cpu = smp_processor_id();
651
652         /*
653          * Ouch, printk recursed into itself!
654          */
655         if (unlikely(printk_cpu == this_cpu)) {
656                 /*
657                  * If a crash is occurring during printk() on this CPU,
658                  * then try to get the crash message out but make sure
659                  * we can't deadlock. Otherwise just return to avoid the
660                  * recursion and return - but flag the recursion so that
661                  * it can be printed at the next appropriate moment:
662                  */
663                 if (!oops_in_progress) {
664                         printk_recursion_bug = 1;
665                         goto out_restore_irqs;
666                 }
667                 zap_locks();
668         }
669
670         lockdep_off();
671         spin_lock(&logbuf_lock);
672         printk_cpu = this_cpu;
673
674         if (printk_recursion_bug) {
675                 printk_recursion_bug = 0;
676                 strcpy(printk_buf, printk_recursion_bug_msg);
677                 printed_len = sizeof(printk_recursion_bug_msg);
678         }
679         /* Emit the output into the temporary buffer */
680         printed_len += vscnprintf(printk_buf + printed_len,
681                                   sizeof(printk_buf), fmt, args);
682
683         /*
684          * Copy the output into log_buf.  If the caller didn't provide
685          * appropriate log level tags, we insert them here
686          */
687         for (p = printk_buf; *p; p++) {
688                 if (log_level_unknown) {
689                         /* log_level_unknown signals the start of a new line */
690                         if (printk_time) {
691                                 int loglev_char;
692                                 char tbuf[50], *tp;
693                                 unsigned tlen;
694                                 unsigned long long t;
695                                 unsigned long nanosec_rem;
696
697                                 /*
698                                  * force the log level token to be
699                                  * before the time output.
700                                  */
701                                 if (p[0] == '<' && p[1] >='0' &&
702                                    p[1] <= '7' && p[2] == '>') {
703                                         loglev_char = p[1];
704                                         p += 3;
705                                         printed_len -= 3;
706                                 } else {
707                                         loglev_char = default_message_loglevel
708                                                 + '0';
709                                 }
710                                 t = printk_clock();
711                                 nanosec_rem = do_div(t, 1000000000);
712                                 tlen = sprintf(tbuf,
713                                                 "<%c>[%5lu.%06lu] ",
714                                                 loglev_char,
715                                                 (unsigned long)t,
716                                                 nanosec_rem/1000);
717
718                                 for (tp = tbuf; tp < tbuf + tlen; tp++)
719                                         emit_log_char(*tp);
720                                 printed_len += tlen;
721                         } else {
722                                 if (p[0] != '<' || p[1] < '0' ||
723                                    p[1] > '7' || p[2] != '>') {
724                                         emit_log_char('<');
725                                         emit_log_char(default_message_loglevel
726                                                 + '0');
727                                         emit_log_char('>');
728                                         printed_len += 3;
729                                 }
730                         }
731                         log_level_unknown = 0;
732                         if (!*p)
733                                 break;
734                 }
735                 emit_log_char(*p);
736                 if (*p == '\n')
737                         log_level_unknown = 1;
738         }
739
740         if (!down_trylock(&console_sem)) {
741                 /*
742                  * We own the drivers.  We can drop the spinlock and
743                  * let release_console_sem() print the text, maybe ...
744                  */
745                 console_locked = 1;
746                 printk_cpu = UINT_MAX;
747                 spin_unlock(&logbuf_lock);
748
749                 /*
750                  * Console drivers may assume that per-cpu resources have
751                  * been allocated. So unless they're explicitly marked as
752                  * being able to cope (CON_ANYTIME) don't call them until
753                  * this CPU is officially up.
754                  */
755                 if (cpu_online(smp_processor_id()) || have_callable_console()) {
756                         console_may_schedule = 0;
757                         release_console_sem();
758                 } else {
759                         /* Release by hand to avoid flushing the buffer. */
760                         console_locked = 0;
761                         up(&console_sem);
762                 }
763                 lockdep_on();
764                 raw_local_irq_restore(flags);
765         } else {
766                 /*
767                  * Someone else owns the drivers.  We drop the spinlock, which
768                  * allows the semaphore holder to proceed and to call the
769                  * console drivers with the output which we just produced.
770                  */
771                 printk_cpu = UINT_MAX;
772                 spin_unlock(&logbuf_lock);
773                 lockdep_on();
774 out_restore_irqs:
775                 raw_local_irq_restore(flags);
776         }
777
778         preempt_enable();
779         return printed_len;
780 }
781 EXPORT_SYMBOL(printk);
782 EXPORT_SYMBOL(vprintk);
783
784 #else
785
786 asmlinkage long sys_syslog(int type, char __user *buf, int len)
787 {
788         return -ENOSYS;
789 }
790
791 static void call_console_drivers(unsigned long start, unsigned long end)
792 {
793 }
794
795 #endif
796
797 /*
798  * Set up a list of consoles.  Called from init/main.c
799  */
800 static int __init console_setup(char *str)
801 {
802         char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
803         char *s, *options;
804         int idx;
805
806         /*
807          * Decode str into name, index, options.
808          */
809         if (str[0] >= '0' && str[0] <= '9') {
810                 strcpy(buf, "ttyS");
811                 strncpy(buf + 4, str, sizeof(buf) - 5);
812         } else {
813                 strncpy(buf, str, sizeof(buf) - 1);
814         }
815         buf[sizeof(buf) - 1] = 0;
816         if ((options = strchr(str, ',')) != NULL)
817                 *(options++) = 0;
818 #ifdef __sparc__
819         if (!strcmp(str, "ttya"))
820                 strcpy(buf, "ttyS0");
821         if (!strcmp(str, "ttyb"))
822                 strcpy(buf, "ttyS1");
823 #endif
824         for (s = buf; *s; s++)
825                 if ((*s >= '0' && *s <= '9') || *s == ',')
826                         break;
827         idx = simple_strtoul(s, NULL, 10);
828         *s = 0;
829
830         add_preferred_console(buf, idx, options);
831         return 1;
832 }
833 __setup("console=", console_setup);
834
835 /**
836  * add_preferred_console - add a device to the list of preferred consoles.
837  * @name: device name
838  * @idx: device index
839  * @options: options for this console
840  *
841  * The last preferred console added will be used for kernel messages
842  * and stdin/out/err for init.  Normally this is used by console_setup
843  * above to handle user-supplied console arguments; however it can also
844  * be used by arch-specific code either to override the user or more
845  * commonly to provide a default console (ie from PROM variables) when
846  * the user has not supplied one.
847  */
848 int add_preferred_console(char *name, int idx, char *options)
849 {
850         struct console_cmdline *c;
851         int i;
852
853         /*
854          *      See if this tty is not yet registered, and
855          *      if we have a slot free.
856          */
857         for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
858                 if (strcmp(console_cmdline[i].name, name) == 0 &&
859                           console_cmdline[i].index == idx) {
860                                 selected_console = i;
861                                 return 0;
862                 }
863         if (i == MAX_CMDLINECONSOLES)
864                 return -E2BIG;
865         selected_console = i;
866         c = &console_cmdline[i];
867         memcpy(c->name, name, sizeof(c->name));
868         c->name[sizeof(c->name) - 1] = 0;
869         c->options = options;
870         c->index = idx;
871         return 0;
872 }
873
874 int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
875 {
876         struct console_cmdline *c;
877         int i;
878
879         for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
880                 if (strcmp(console_cmdline[i].name, name) == 0 &&
881                           console_cmdline[i].index == idx) {
882                                 c = &console_cmdline[i];
883                                 memcpy(c->name, name_new, sizeof(c->name));
884                                 c->name[sizeof(c->name) - 1] = 0;
885                                 c->options = options;
886                                 c->index = idx_new;
887                                 return i;
888                 }
889         /* not found */
890         return -1;
891 }
892
893 int console_suspend_enabled = 1;
894 EXPORT_SYMBOL(console_suspend_enabled);
895
896 static int __init console_suspend_disable(char *str)
897 {
898         console_suspend_enabled = 0;
899         return 1;
900 }
901 __setup("no_console_suspend", console_suspend_disable);
902
903 /**
904  * suspend_console - suspend the console subsystem
905  *
906  * This disables printk() while we go into suspend states
907  */
908 void suspend_console(void)
909 {
910         if (!console_suspend_enabled)
911                 return;
912         printk("Suspending console(s)\n");
913         acquire_console_sem();
914         console_suspended = 1;
915 }
916
917 void resume_console(void)
918 {
919         if (!console_suspend_enabled)
920                 return;
921         console_suspended = 0;
922         release_console_sem();
923 }
924
925 /**
926  * acquire_console_sem - lock the console system for exclusive use.
927  *
928  * Acquires a semaphore which guarantees that the caller has
929  * exclusive access to the console system and the console_drivers list.
930  *
931  * Can sleep, returns nothing.
932  */
933 void acquire_console_sem(void)
934 {
935         BUG_ON(in_interrupt());
936         if (console_suspended) {
937                 down(&secondary_console_sem);
938                 return;
939         }
940         down(&console_sem);
941         console_locked = 1;
942         console_may_schedule = 1;
943 }
944 EXPORT_SYMBOL(acquire_console_sem);
945
946 int try_acquire_console_sem(void)
947 {
948         if (down_trylock(&console_sem))
949                 return -1;
950         console_locked = 1;
951         console_may_schedule = 0;
952         return 0;
953 }
954 EXPORT_SYMBOL(try_acquire_console_sem);
955
956 int is_console_locked(void)
957 {
958         return console_locked;
959 }
960
961 void wake_up_klogd(void)
962 {
963         if (!oops_in_progress && waitqueue_active(&log_wait))
964                 wake_up_interruptible(&log_wait);
965 }
966
967 /**
968  * release_console_sem - unlock the console system
969  *
970  * Releases the semaphore which the caller holds on the console system
971  * and the console driver list.
972  *
973  * While the semaphore was held, console output may have been buffered
974  * by printk().  If this is the case, release_console_sem() emits
975  * the output prior to releasing the semaphore.
976  *
977  * If there is output waiting for klogd, we wake it up.
978  *
979  * release_console_sem() may be called from any context.
980  */
981 void release_console_sem(void)
982 {
983         unsigned long flags;
984         unsigned long _con_start, _log_end;
985         unsigned long wake_klogd = 0;
986
987         if (console_suspended) {
988                 up(&secondary_console_sem);
989                 return;
990         }
991
992         console_may_schedule = 0;
993
994         for ( ; ; ) {
995                 spin_lock_irqsave(&logbuf_lock, flags);
996                 wake_klogd |= log_start - log_end;
997                 if (con_start == log_end)
998                         break;                  /* Nothing to print */
999                 _con_start = con_start;
1000                 _log_end = log_end;
1001                 con_start = log_end;            /* Flush */
1002                 spin_unlock(&logbuf_lock);
1003                 call_console_drivers(_con_start, _log_end);
1004                 local_irq_restore(flags);
1005         }
1006         console_locked = 0;
1007         up(&console_sem);
1008         spin_unlock_irqrestore(&logbuf_lock, flags);
1009         if (wake_klogd)
1010                 wake_up_klogd();
1011 }
1012 EXPORT_SYMBOL(release_console_sem);
1013
1014 /**
1015  * console_conditional_schedule - yield the CPU if required
1016  *
1017  * If the console code is currently allowed to sleep, and
1018  * if this CPU should yield the CPU to another task, do
1019  * so here.
1020  *
1021  * Must be called within acquire_console_sem().
1022  */
1023 void __sched console_conditional_schedule(void)
1024 {
1025         if (console_may_schedule)
1026                 cond_resched();
1027 }
1028 EXPORT_SYMBOL(console_conditional_schedule);
1029
1030 void console_print(const char *s)
1031 {
1032         printk(KERN_EMERG "%s", s);
1033 }
1034 EXPORT_SYMBOL(console_print);
1035
1036 void console_unblank(void)
1037 {
1038         struct console *c;
1039
1040         /*
1041          * console_unblank can no longer be called in interrupt context unless
1042          * oops_in_progress is set to 1..
1043          */
1044         if (oops_in_progress) {
1045                 if (down_trylock(&console_sem) != 0)
1046                         return;
1047         } else
1048                 acquire_console_sem();
1049
1050         console_locked = 1;
1051         console_may_schedule = 0;
1052         for (c = console_drivers; c != NULL; c = c->next)
1053                 if ((c->flags & CON_ENABLED) && c->unblank)
1054                         c->unblank();
1055         release_console_sem();
1056 }
1057
1058 /*
1059  * Return the console tty driver structure and its associated index
1060  */
1061 struct tty_driver *console_device(int *index)
1062 {
1063         struct console *c;
1064         struct tty_driver *driver = NULL;
1065
1066         acquire_console_sem();
1067         for (c = console_drivers; c != NULL; c = c->next) {
1068                 if (!c->device)
1069                         continue;
1070                 driver = c->device(c, index);
1071                 if (driver)
1072                         break;
1073         }
1074         release_console_sem();
1075         return driver;
1076 }
1077
1078 /*
1079  * Prevent further output on the passed console device so that (for example)
1080  * serial drivers can disable console output before suspending a port, and can
1081  * re-enable output afterwards.
1082  */
1083 void console_stop(struct console *console)
1084 {
1085         acquire_console_sem();
1086         console->flags &= ~CON_ENABLED;
1087         release_console_sem();
1088 }
1089 EXPORT_SYMBOL(console_stop);
1090
1091 void console_start(struct console *console)
1092 {
1093         acquire_console_sem();
1094         console->flags |= CON_ENABLED;
1095         release_console_sem();
1096 }
1097 EXPORT_SYMBOL(console_start);
1098
1099 /*
1100  * The console driver calls this routine during kernel initialization
1101  * to register the console printing procedure with printk() and to
1102  * print any messages that were printed by the kernel before the
1103  * console driver was initialized.
1104  */
1105 void register_console(struct console *console)
1106 {
1107         int i;
1108         unsigned long flags;
1109         struct console *bootconsole = NULL;
1110
1111         if (console_drivers) {
1112                 if (console->flags & CON_BOOT)
1113                         return;
1114                 if (console_drivers->flags & CON_BOOT)
1115                         bootconsole = console_drivers;
1116         }
1117
1118         if (preferred_console < 0 || bootconsole || !console_drivers)
1119                 preferred_console = selected_console;
1120
1121         if (console->early_setup)
1122                 console->early_setup();
1123
1124         /*
1125          *      See if we want to use this console driver. If we
1126          *      didn't select a console we take the first one
1127          *      that registers here.
1128          */
1129         if (preferred_console < 0) {
1130                 if (console->index < 0)
1131                         console->index = 0;
1132                 if (console->setup == NULL ||
1133                     console->setup(console, NULL) == 0) {
1134                         console->flags |= CON_ENABLED | CON_CONSDEV;
1135                         preferred_console = 0;
1136                 }
1137         }
1138
1139         /*
1140          *      See if this console matches one we selected on
1141          *      the command line.
1142          */
1143         for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1144                         i++) {
1145                 if (strcmp(console_cmdline[i].name, console->name) != 0)
1146                         continue;
1147                 if (console->index >= 0 &&
1148                     console->index != console_cmdline[i].index)
1149                         continue;
1150                 if (console->index < 0)
1151                         console->index = console_cmdline[i].index;
1152                 if (console->setup &&
1153                     console->setup(console, console_cmdline[i].options) != 0)
1154                         break;
1155                 console->flags |= CON_ENABLED;
1156                 console->index = console_cmdline[i].index;
1157                 if (i == selected_console) {
1158                         console->flags |= CON_CONSDEV;
1159                         preferred_console = selected_console;
1160                 }
1161                 break;
1162         }
1163
1164         if (!(console->flags & CON_ENABLED))
1165                 return;
1166
1167         if (bootconsole && (console->flags & CON_CONSDEV)) {
1168                 printk(KERN_INFO "console handover: boot [%s%d] -> real [%s%d]\n",
1169                        bootconsole->name, bootconsole->index,
1170                        console->name, console->index);
1171                 unregister_console(bootconsole);
1172                 console->flags &= ~CON_PRINTBUFFER;
1173         } else {
1174                 printk(KERN_INFO "console [%s%d] enabled\n",
1175                        console->name, console->index);
1176         }
1177
1178         /*
1179          *      Put this console in the list - keep the
1180          *      preferred driver at the head of the list.
1181          */
1182         acquire_console_sem();
1183         if ((console->flags & CON_CONSDEV) || console_drivers == NULL) {
1184                 console->next = console_drivers;
1185                 console_drivers = console;
1186                 if (console->next)
1187                         console->next->flags &= ~CON_CONSDEV;
1188         } else {
1189                 console->next = console_drivers->next;
1190                 console_drivers->next = console;
1191         }
1192         if (console->flags & CON_PRINTBUFFER) {
1193                 /*
1194                  * release_console_sem() will print out the buffered messages
1195                  * for us.
1196                  */
1197                 spin_lock_irqsave(&logbuf_lock, flags);
1198                 con_start = log_start;
1199                 spin_unlock_irqrestore(&logbuf_lock, flags);
1200         }
1201         release_console_sem();
1202 }
1203 EXPORT_SYMBOL(register_console);
1204
1205 int unregister_console(struct console *console)
1206 {
1207         struct console *a, *b;
1208         int res = 1;
1209
1210         acquire_console_sem();
1211         if (console_drivers == console) {
1212                 console_drivers=console->next;
1213                 res = 0;
1214         } else if (console_drivers) {
1215                 for (a=console_drivers->next, b=console_drivers ;
1216                      a; b=a, a=b->next) {
1217                         if (a == console) {
1218                                 b->next = a->next;
1219                                 res = 0;
1220                                 break;
1221                         }
1222                 }
1223         }
1224
1225         /*
1226          * If this isn't the last console and it has CON_CONSDEV set, we
1227          * need to set it on the next preferred console.
1228          */
1229         if (console_drivers != NULL && console->flags & CON_CONSDEV)
1230                 console_drivers->flags |= CON_CONSDEV;
1231
1232         release_console_sem();
1233         return res;
1234 }
1235 EXPORT_SYMBOL(unregister_console);
1236
1237 static int __init disable_boot_consoles(void)
1238 {
1239         if (console_drivers != NULL) {
1240                 if (console_drivers->flags & CON_BOOT) {
1241                         printk(KERN_INFO "turn off boot console %s%d\n",
1242                                 console_drivers->name, console_drivers->index);
1243                         return unregister_console(console_drivers);
1244                 }
1245         }
1246         return 0;
1247 }
1248 late_initcall(disable_boot_consoles);
1249
1250 /**
1251  * tty_write_message - write a message to a certain tty, not just the console.
1252  * @tty: the destination tty_struct
1253  * @msg: the message to write
1254  *
1255  * This is used for messages that need to be redirected to a specific tty.
1256  * We don't put it into the syslog queue right now maybe in the future if
1257  * really needed.
1258  */
1259 void tty_write_message(struct tty_struct *tty, char *msg)
1260 {
1261         if (tty && tty->driver->write)
1262                 tty->driver->write(tty, msg, strlen(msg));
1263         return;
1264 }
1265
1266 /*
1267  * printk rate limiting, lifted from the networking subsystem.
1268  *
1269  * This enforces a rate limit: not more than one kernel message
1270  * every printk_ratelimit_jiffies to make a denial-of-service
1271  * attack impossible.
1272  */
1273 int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst)
1274 {
1275         static DEFINE_SPINLOCK(ratelimit_lock);
1276         static unsigned long toks = 10 * 5 * HZ;
1277         static unsigned long last_msg;
1278         static int missed;
1279         unsigned long flags;
1280         unsigned long now = jiffies;
1281
1282         spin_lock_irqsave(&ratelimit_lock, flags);
1283         toks += now - last_msg;
1284         last_msg = now;
1285         if (toks > (ratelimit_burst * ratelimit_jiffies))
1286                 toks = ratelimit_burst * ratelimit_jiffies;
1287         if (toks >= ratelimit_jiffies) {
1288                 int lost = missed;
1289
1290                 missed = 0;
1291                 toks -= ratelimit_jiffies;
1292                 spin_unlock_irqrestore(&ratelimit_lock, flags);
1293                 if (lost)
1294                         printk(KERN_WARNING "printk: %d messages suppressed.\n", lost);
1295                 return 1;
1296         }
1297         missed++;
1298         spin_unlock_irqrestore(&ratelimit_lock, flags);
1299         return 0;
1300 }
1301 EXPORT_SYMBOL(__printk_ratelimit);
1302
1303 /* minimum time in jiffies between messages */
1304 int printk_ratelimit_jiffies = 5 * HZ;
1305
1306 /* number of messages we send before ratelimiting */
1307 int printk_ratelimit_burst = 10;
1308
1309 int printk_ratelimit(void)
1310 {
1311         return __printk_ratelimit(printk_ratelimit_jiffies,
1312                                 printk_ratelimit_burst);
1313 }
1314 EXPORT_SYMBOL(printk_ratelimit);
1315
1316 /**
1317  * printk_timed_ratelimit - caller-controlled printk ratelimiting
1318  * @caller_jiffies: pointer to caller's state
1319  * @interval_msecs: minimum interval between prints
1320  *
1321  * printk_timed_ratelimit() returns true if more than @interval_msecs
1322  * milliseconds have elapsed since the last time printk_timed_ratelimit()
1323  * returned true.
1324  */
1325 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1326                         unsigned int interval_msecs)
1327 {
1328         if (*caller_jiffies == 0 || time_after(jiffies, *caller_jiffies)) {
1329                 *caller_jiffies = jiffies + msecs_to_jiffies(interval_msecs);
1330                 return true;
1331         }
1332         return false;
1333 }
1334 EXPORT_SYMBOL(printk_timed_ratelimit);