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