]> git.karo-electronics.de Git - mv-sheeva.git/blob - kernel/printk.c
Merge branch 'stable/for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mv-sheeva.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
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/kexec.h>
36 #include <linux/kdb.h>
37 #include <linux/ratelimit.h>
38 #include <linux/kmsg_dump.h>
39 #include <linux/syslog.h>
40 #include <linux/cpu.h>
41 #include <linux/notifier.h>
42
43 #include <asm/uaccess.h>
44
45 /*
46  * for_each_console() allows you to iterate on each console
47  */
48 #define for_each_console(con) \
49         for (con = console_drivers; con != NULL; con = con->next)
50
51 /*
52  * Architectures can override it:
53  */
54 void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
55 {
56 }
57
58 #define __LOG_BUF_LEN   (1 << CONFIG_LOG_BUF_SHIFT)
59
60 /* printk's without a loglevel use this.. */
61 #define DEFAULT_MESSAGE_LOGLEVEL 4 /* KERN_WARNING */
62
63 /* We show everything that is MORE important than this.. */
64 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
65 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
66
67 DECLARE_WAIT_QUEUE_HEAD(log_wait);
68
69 int console_printk[4] = {
70         DEFAULT_CONSOLE_LOGLEVEL,       /* console_loglevel */
71         DEFAULT_MESSAGE_LOGLEVEL,       /* default_message_loglevel */
72         MINIMUM_CONSOLE_LOGLEVEL,       /* minimum_console_loglevel */
73         DEFAULT_CONSOLE_LOGLEVEL,       /* default_console_loglevel */
74 };
75
76 /*
77  * Low level drivers may need that to know if they can schedule in
78  * their unblank() callback or not. So let's export it.
79  */
80 int oops_in_progress;
81 EXPORT_SYMBOL(oops_in_progress);
82
83 /*
84  * console_sem protects the console_drivers list, and also
85  * provides serialisation for access to the entire console
86  * driver system.
87  */
88 static DEFINE_SEMAPHORE(console_sem);
89 struct console *console_drivers;
90 EXPORT_SYMBOL_GPL(console_drivers);
91
92 /*
93  * This is used for debugging the mess that is the VT code by
94  * keeping track if we have the console semaphore held. It's
95  * definitely not the perfect debug tool (we don't know if _WE_
96  * hold it are racing, but it helps tracking those weird code
97  * path in the console code where we end up in places I want
98  * locked without the console sempahore held
99  */
100 static int console_locked, console_suspended;
101
102 /*
103  * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars
104  * It is also used in interesting ways to provide interlocking in
105  * release_console_sem().
106  */
107 static DEFINE_SPINLOCK(logbuf_lock);
108
109 #define LOG_BUF_MASK (log_buf_len-1)
110 #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK])
111
112 /*
113  * The indices into log_buf are not constrained to log_buf_len - they
114  * must be masked before subscripting
115  */
116 static unsigned log_start;      /* Index into log_buf: next char to be read by syslog() */
117 static unsigned con_start;      /* Index into log_buf: next char to be sent to consoles */
118 static unsigned log_end;        /* Index into log_buf: most-recently-written-char + 1 */
119
120 /*
121  *      Array of consoles built from command line options (console=)
122  */
123 struct console_cmdline
124 {
125         char    name[8];                        /* Name of the driver       */
126         int     index;                          /* Minor dev. to use        */
127         char    *options;                       /* Options for the driver   */
128 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
129         char    *brl_options;                   /* Options for braille driver */
130 #endif
131 };
132
133 #define MAX_CMDLINECONSOLES 8
134
135 static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
136 static int selected_console = -1;
137 static int preferred_console = -1;
138 int console_set_on_cmdline;
139 EXPORT_SYMBOL(console_set_on_cmdline);
140
141 /* Flag: console code may call schedule() */
142 static int console_may_schedule;
143
144 #ifdef CONFIG_PRINTK
145
146 static char __log_buf[__LOG_BUF_LEN];
147 static char *log_buf = __log_buf;
148 static int log_buf_len = __LOG_BUF_LEN;
149 static unsigned logged_chars; /* Number of chars produced since last read+clear operation */
150 static int saved_console_loglevel = -1;
151
152 #ifdef CONFIG_KEXEC
153 /*
154  * This appends the listed symbols to /proc/vmcoreinfo
155  *
156  * /proc/vmcoreinfo is used by various utiilties, like crash and makedumpfile to
157  * obtain access to symbols that are otherwise very difficult to locate.  These
158  * symbols are specifically used so that utilities can access and extract the
159  * dmesg log from a vmcore file after a crash.
160  */
161 void log_buf_kexec_setup(void)
162 {
163         VMCOREINFO_SYMBOL(log_buf);
164         VMCOREINFO_SYMBOL(log_end);
165         VMCOREINFO_SYMBOL(log_buf_len);
166         VMCOREINFO_SYMBOL(logged_chars);
167 }
168 #endif
169
170 static int __init log_buf_len_setup(char *str)
171 {
172         unsigned size = memparse(str, &str);
173         unsigned long flags;
174
175         if (size)
176                 size = roundup_pow_of_two(size);
177         if (size > log_buf_len) {
178                 unsigned start, dest_idx, offset;
179                 char *new_log_buf;
180
181                 new_log_buf = alloc_bootmem(size);
182                 if (!new_log_buf) {
183                         printk(KERN_WARNING "log_buf_len: allocation failed\n");
184                         goto out;
185                 }
186
187                 spin_lock_irqsave(&logbuf_lock, flags);
188                 log_buf_len = size;
189                 log_buf = new_log_buf;
190
191                 offset = start = min(con_start, log_start);
192                 dest_idx = 0;
193                 while (start != log_end) {
194                         log_buf[dest_idx] = __log_buf[start & (__LOG_BUF_LEN - 1)];
195                         start++;
196                         dest_idx++;
197                 }
198                 log_start -= offset;
199                 con_start -= offset;
200                 log_end -= offset;
201                 spin_unlock_irqrestore(&logbuf_lock, flags);
202
203                 printk(KERN_NOTICE "log_buf_len: %d\n", log_buf_len);
204         }
205 out:
206         return 1;
207 }
208
209 __setup("log_buf_len=", log_buf_len_setup);
210
211 #ifdef CONFIG_BOOT_PRINTK_DELAY
212
213 static unsigned int boot_delay; /* msecs delay after each printk during bootup */
214 static unsigned long long loops_per_msec;       /* based on boot_delay */
215
216 static int __init boot_delay_setup(char *str)
217 {
218         unsigned long lpj;
219
220         lpj = preset_lpj ? preset_lpj : 1000000;        /* some guess */
221         loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
222
223         get_option(&str, &boot_delay);
224         if (boot_delay > 10 * 1000)
225                 boot_delay = 0;
226
227         pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
228                 "HZ: %d, loops_per_msec: %llu\n",
229                 boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
230         return 1;
231 }
232 __setup("boot_delay=", boot_delay_setup);
233
234 static void boot_delay_msec(void)
235 {
236         unsigned long long k;
237         unsigned long timeout;
238
239         if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
240                 return;
241
242         k = (unsigned long long)loops_per_msec * boot_delay;
243
244         timeout = jiffies + msecs_to_jiffies(boot_delay);
245         while (k) {
246                 k--;
247                 cpu_relax();
248                 /*
249                  * use (volatile) jiffies to prevent
250                  * compiler reduction; loop termination via jiffies
251                  * is secondary and may or may not happen.
252                  */
253                 if (time_after(jiffies, timeout))
254                         break;
255                 touch_nmi_watchdog();
256         }
257 }
258 #else
259 static inline void boot_delay_msec(void)
260 {
261 }
262 #endif
263
264 int do_syslog(int type, char __user *buf, int len, bool from_file)
265 {
266         unsigned i, j, limit, count;
267         int do_clear = 0;
268         char c;
269         int error = 0;
270
271         error = security_syslog(type, from_file);
272         if (error)
273                 return error;
274
275         switch (type) {
276         case SYSLOG_ACTION_CLOSE:       /* Close log */
277                 break;
278         case SYSLOG_ACTION_OPEN:        /* Open log */
279                 break;
280         case SYSLOG_ACTION_READ:        /* Read from log */
281                 error = -EINVAL;
282                 if (!buf || len < 0)
283                         goto out;
284                 error = 0;
285                 if (!len)
286                         goto out;
287                 if (!access_ok(VERIFY_WRITE, buf, len)) {
288                         error = -EFAULT;
289                         goto out;
290                 }
291                 error = wait_event_interruptible(log_wait,
292                                                         (log_start - log_end));
293                 if (error)
294                         goto out;
295                 i = 0;
296                 spin_lock_irq(&logbuf_lock);
297                 while (!error && (log_start != log_end) && i < len) {
298                         c = LOG_BUF(log_start);
299                         log_start++;
300                         spin_unlock_irq(&logbuf_lock);
301                         error = __put_user(c,buf);
302                         buf++;
303                         i++;
304                         cond_resched();
305                         spin_lock_irq(&logbuf_lock);
306                 }
307                 spin_unlock_irq(&logbuf_lock);
308                 if (!error)
309                         error = i;
310                 break;
311         /* Read/clear last kernel messages */
312         case SYSLOG_ACTION_READ_CLEAR:
313                 do_clear = 1;
314                 /* FALL THRU */
315         /* Read last kernel messages */
316         case SYSLOG_ACTION_READ_ALL:
317                 error = -EINVAL;
318                 if (!buf || len < 0)
319                         goto out;
320                 error = 0;
321                 if (!len)
322                         goto out;
323                 if (!access_ok(VERIFY_WRITE, buf, len)) {
324                         error = -EFAULT;
325                         goto out;
326                 }
327                 count = len;
328                 if (count > log_buf_len)
329                         count = log_buf_len;
330                 spin_lock_irq(&logbuf_lock);
331                 if (count > logged_chars)
332                         count = logged_chars;
333                 if (do_clear)
334                         logged_chars = 0;
335                 limit = log_end;
336                 /*
337                  * __put_user() could sleep, and while we sleep
338                  * printk() could overwrite the messages
339                  * we try to copy to user space. Therefore
340                  * the messages are copied in reverse. <manfreds>
341                  */
342                 for (i = 0; i < count && !error; i++) {
343                         j = limit-1-i;
344                         if (j + log_buf_len < log_end)
345                                 break;
346                         c = LOG_BUF(j);
347                         spin_unlock_irq(&logbuf_lock);
348                         error = __put_user(c,&buf[count-1-i]);
349                         cond_resched();
350                         spin_lock_irq(&logbuf_lock);
351                 }
352                 spin_unlock_irq(&logbuf_lock);
353                 if (error)
354                         break;
355                 error = i;
356                 if (i != count) {
357                         int offset = count-error;
358                         /* buffer overflow during copy, correct user buffer. */
359                         for (i = 0; i < error; i++) {
360                                 if (__get_user(c,&buf[i+offset]) ||
361                                     __put_user(c,&buf[i])) {
362                                         error = -EFAULT;
363                                         break;
364                                 }
365                                 cond_resched();
366                         }
367                 }
368                 break;
369         /* Clear ring buffer */
370         case SYSLOG_ACTION_CLEAR:
371                 logged_chars = 0;
372                 break;
373         /* Disable logging to console */
374         case SYSLOG_ACTION_CONSOLE_OFF:
375                 if (saved_console_loglevel == -1)
376                         saved_console_loglevel = console_loglevel;
377                 console_loglevel = minimum_console_loglevel;
378                 break;
379         /* Enable logging to console */
380         case SYSLOG_ACTION_CONSOLE_ON:
381                 if (saved_console_loglevel != -1) {
382                         console_loglevel = saved_console_loglevel;
383                         saved_console_loglevel = -1;
384                 }
385                 break;
386         /* Set level of messages printed to console */
387         case SYSLOG_ACTION_CONSOLE_LEVEL:
388                 error = -EINVAL;
389                 if (len < 1 || len > 8)
390                         goto out;
391                 if (len < minimum_console_loglevel)
392                         len = minimum_console_loglevel;
393                 console_loglevel = len;
394                 /* Implicitly re-enable logging to console */
395                 saved_console_loglevel = -1;
396                 error = 0;
397                 break;
398         /* Number of chars in the log buffer */
399         case SYSLOG_ACTION_SIZE_UNREAD:
400                 error = log_end - log_start;
401                 break;
402         /* Size of the log buffer */
403         case SYSLOG_ACTION_SIZE_BUFFER:
404                 error = log_buf_len;
405                 break;
406         default:
407                 error = -EINVAL;
408                 break;
409         }
410 out:
411         return error;
412 }
413
414 SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
415 {
416         return do_syslog(type, buf, len, SYSLOG_FROM_CALL);
417 }
418
419 #ifdef  CONFIG_KGDB_KDB
420 /* kdb dmesg command needs access to the syslog buffer.  do_syslog()
421  * uses locks so it cannot be used during debugging.  Just tell kdb
422  * where the start and end of the physical and logical logs are.  This
423  * is equivalent to do_syslog(3).
424  */
425 void kdb_syslog_data(char *syslog_data[4])
426 {
427         syslog_data[0] = log_buf;
428         syslog_data[1] = log_buf + log_buf_len;
429         syslog_data[2] = log_buf + log_end -
430                 (logged_chars < log_buf_len ? logged_chars : log_buf_len);
431         syslog_data[3] = log_buf + log_end;
432 }
433 #endif  /* CONFIG_KGDB_KDB */
434
435 /*
436  * Call the console drivers on a range of log_buf
437  */
438 static void __call_console_drivers(unsigned start, unsigned end)
439 {
440         struct console *con;
441
442         for_each_console(con) {
443                 if ((con->flags & CON_ENABLED) && con->write &&
444                                 (cpu_online(smp_processor_id()) ||
445                                 (con->flags & CON_ANYTIME)))
446                         con->write(con, &LOG_BUF(start), end - start);
447         }
448 }
449
450 static int __read_mostly ignore_loglevel;
451
452 static int __init ignore_loglevel_setup(char *str)
453 {
454         ignore_loglevel = 1;
455         printk(KERN_INFO "debug: ignoring loglevel setting.\n");
456
457         return 0;
458 }
459
460 early_param("ignore_loglevel", ignore_loglevel_setup);
461
462 /*
463  * Write out chars from start to end - 1 inclusive
464  */
465 static void _call_console_drivers(unsigned start,
466                                 unsigned end, int msg_log_level)
467 {
468         if ((msg_log_level < console_loglevel || ignore_loglevel) &&
469                         console_drivers && start != end) {
470                 if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) {
471                         /* wrapped write */
472                         __call_console_drivers(start & LOG_BUF_MASK,
473                                                 log_buf_len);
474                         __call_console_drivers(0, end & LOG_BUF_MASK);
475                 } else {
476                         __call_console_drivers(start, end);
477                 }
478         }
479 }
480
481 /*
482  * Call the console drivers, asking them to write out
483  * log_buf[start] to log_buf[end - 1].
484  * The console_sem must be held.
485  */
486 static void call_console_drivers(unsigned start, unsigned end)
487 {
488         unsigned cur_index, start_print;
489         static int msg_level = -1;
490
491         BUG_ON(((int)(start - end)) > 0);
492
493         cur_index = start;
494         start_print = start;
495         while (cur_index != end) {
496                 if (msg_level < 0 && ((end - cur_index) > 2) &&
497                                 LOG_BUF(cur_index + 0) == '<' &&
498                                 LOG_BUF(cur_index + 1) >= '0' &&
499                                 LOG_BUF(cur_index + 1) <= '7' &&
500                                 LOG_BUF(cur_index + 2) == '>') {
501                         msg_level = LOG_BUF(cur_index + 1) - '0';
502                         cur_index += 3;
503                         start_print = cur_index;
504                 }
505                 while (cur_index != end) {
506                         char c = LOG_BUF(cur_index);
507
508                         cur_index++;
509                         if (c == '\n') {
510                                 if (msg_level < 0) {
511                                         /*
512                                          * printk() has already given us loglevel tags in
513                                          * the buffer.  This code is here in case the
514                                          * log buffer has wrapped right round and scribbled
515                                          * on those tags
516                                          */
517                                         msg_level = default_message_loglevel;
518                                 }
519                                 _call_console_drivers(start_print, cur_index, msg_level);
520                                 msg_level = -1;
521                                 start_print = cur_index;
522                                 break;
523                         }
524                 }
525         }
526         _call_console_drivers(start_print, end, msg_level);
527 }
528
529 static void emit_log_char(char c)
530 {
531         LOG_BUF(log_end) = c;
532         log_end++;
533         if (log_end - log_start > log_buf_len)
534                 log_start = log_end - log_buf_len;
535         if (log_end - con_start > log_buf_len)
536                 con_start = log_end - log_buf_len;
537         if (logged_chars < log_buf_len)
538                 logged_chars++;
539 }
540
541 /*
542  * Zap console related locks when oopsing. Only zap at most once
543  * every 10 seconds, to leave time for slow consoles to print a
544  * full oops.
545  */
546 static void zap_locks(void)
547 {
548         static unsigned long oops_timestamp;
549
550         if (time_after_eq(jiffies, oops_timestamp) &&
551                         !time_after(jiffies, oops_timestamp + 30 * HZ))
552                 return;
553
554         oops_timestamp = jiffies;
555
556         /* If a crash is occurring, make sure we can't deadlock */
557         spin_lock_init(&logbuf_lock);
558         /* And make sure that we print immediately */
559         sema_init(&console_sem, 1);
560 }
561
562 #if defined(CONFIG_PRINTK_TIME)
563 static int printk_time = 1;
564 #else
565 static int printk_time = 0;
566 #endif
567 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
568
569 /* Check if we have any console registered that can be called early in boot. */
570 static int have_callable_console(void)
571 {
572         struct console *con;
573
574         for_each_console(con)
575                 if (con->flags & CON_ANYTIME)
576                         return 1;
577
578         return 0;
579 }
580
581 /**
582  * printk - print a kernel message
583  * @fmt: format string
584  *
585  * This is printk().  It can be called from any context.  We want it to work.
586  *
587  * We try to grab the console_sem.  If we succeed, it's easy - we log the output and
588  * call the console drivers.  If we fail to get the semaphore we place the output
589  * into the log buffer and return.  The current holder of the console_sem will
590  * notice the new output in release_console_sem() and will send it to the
591  * consoles before releasing the semaphore.
592  *
593  * One effect of this deferred printing is that code which calls printk() and
594  * then changes console_loglevel may break. This is because console_loglevel
595  * is inspected when the actual printing occurs.
596  *
597  * See also:
598  * printf(3)
599  *
600  * See the vsnprintf() documentation for format string extensions over C99.
601  */
602
603 asmlinkage int printk(const char *fmt, ...)
604 {
605         va_list args;
606         int r;
607
608 #ifdef CONFIG_KGDB_KDB
609         if (unlikely(kdb_trap_printk)) {
610                 va_start(args, fmt);
611                 r = vkdb_printf(fmt, args);
612                 va_end(args);
613                 return r;
614         }
615 #endif
616         va_start(args, fmt);
617         r = vprintk(fmt, args);
618         va_end(args);
619
620         return r;
621 }
622
623 /* cpu currently holding logbuf_lock */
624 static volatile unsigned int printk_cpu = UINT_MAX;
625
626 /*
627  * Can we actually use the console at this time on this cpu?
628  *
629  * Console drivers may assume that per-cpu resources have
630  * been allocated. So unless they're explicitly marked as
631  * being able to cope (CON_ANYTIME) don't call them until
632  * this CPU is officially up.
633  */
634 static inline int can_use_console(unsigned int cpu)
635 {
636         return cpu_online(cpu) || have_callable_console();
637 }
638
639 /*
640  * Try to get console ownership to actually show the kernel
641  * messages from a 'printk'. Return true (and with the
642  * console_semaphore held, and 'console_locked' set) if it
643  * is successful, false otherwise.
644  *
645  * This gets called with the 'logbuf_lock' spinlock held and
646  * interrupts disabled. It should return with 'lockbuf_lock'
647  * released but interrupts still disabled.
648  */
649 static int acquire_console_semaphore_for_printk(unsigned int cpu)
650 {
651         int retval = 0;
652
653         if (!try_acquire_console_sem()) {
654                 retval = 1;
655
656                 /*
657                  * If we can't use the console, we need to release
658                  * the console semaphore by hand to avoid flushing
659                  * the buffer. We need to hold the console semaphore
660                  * in order to do this test safely.
661                  */
662                 if (!can_use_console(cpu)) {
663                         console_locked = 0;
664                         up(&console_sem);
665                         retval = 0;
666                 }
667         }
668         printk_cpu = UINT_MAX;
669         spin_unlock(&logbuf_lock);
670         return retval;
671 }
672 static const char recursion_bug_msg [] =
673                 KERN_CRIT "BUG: recent printk recursion!\n";
674 static int recursion_bug;
675 static int new_text_line = 1;
676 static char printk_buf[1024];
677
678 int printk_delay_msec __read_mostly;
679
680 static inline void printk_delay(void)
681 {
682         if (unlikely(printk_delay_msec)) {
683                 int m = printk_delay_msec;
684
685                 while (m--) {
686                         mdelay(1);
687                         touch_nmi_watchdog();
688                 }
689         }
690 }
691
692 asmlinkage int vprintk(const char *fmt, va_list args)
693 {
694         int printed_len = 0;
695         int current_log_level = default_message_loglevel;
696         unsigned long flags;
697         int this_cpu;
698         char *p;
699
700         boot_delay_msec();
701         printk_delay();
702
703         preempt_disable();
704         /* This stops the holder of console_sem just where we want him */
705         raw_local_irq_save(flags);
706         this_cpu = smp_processor_id();
707
708         /*
709          * Ouch, printk recursed into itself!
710          */
711         if (unlikely(printk_cpu == this_cpu)) {
712                 /*
713                  * If a crash is occurring during printk() on this CPU,
714                  * then try to get the crash message out but make sure
715                  * we can't deadlock. Otherwise just return to avoid the
716                  * recursion and return - but flag the recursion so that
717                  * it can be printed at the next appropriate moment:
718                  */
719                 if (!oops_in_progress) {
720                         recursion_bug = 1;
721                         goto out_restore_irqs;
722                 }
723                 zap_locks();
724         }
725
726         lockdep_off();
727         spin_lock(&logbuf_lock);
728         printk_cpu = this_cpu;
729
730         if (recursion_bug) {
731                 recursion_bug = 0;
732                 strcpy(printk_buf, recursion_bug_msg);
733                 printed_len = strlen(recursion_bug_msg);
734         }
735         /* Emit the output into the temporary buffer */
736         printed_len += vscnprintf(printk_buf + printed_len,
737                                   sizeof(printk_buf) - printed_len, fmt, args);
738
739
740         p = printk_buf;
741
742         /* Do we have a loglevel in the string? */
743         if (p[0] == '<') {
744                 unsigned char c = p[1];
745                 if (c && p[2] == '>') {
746                         switch (c) {
747                         case '0' ... '7': /* loglevel */
748                                 current_log_level = c - '0';
749                         /* Fallthrough - make sure we're on a new line */
750                         case 'd': /* KERN_DEFAULT */
751                                 if (!new_text_line) {
752                                         emit_log_char('\n');
753                                         new_text_line = 1;
754                                 }
755                         /* Fallthrough - skip the loglevel */
756                         case 'c': /* KERN_CONT */
757                                 p += 3;
758                                 break;
759                         }
760                 }
761         }
762
763         /*
764          * Copy the output into log_buf.  If the caller didn't provide
765          * appropriate log level tags, we insert them here
766          */
767         for ( ; *p; p++) {
768                 if (new_text_line) {
769                         /* Always output the token */
770                         emit_log_char('<');
771                         emit_log_char(current_log_level + '0');
772                         emit_log_char('>');
773                         printed_len += 3;
774                         new_text_line = 0;
775
776                         if (printk_time) {
777                                 /* Follow the token with the time */
778                                 char tbuf[50], *tp;
779                                 unsigned tlen;
780                                 unsigned long long t;
781                                 unsigned long nanosec_rem;
782
783                                 t = cpu_clock(printk_cpu);
784                                 nanosec_rem = do_div(t, 1000000000);
785                                 tlen = sprintf(tbuf, "[%5lu.%06lu] ",
786                                                 (unsigned long) t,
787                                                 nanosec_rem / 1000);
788
789                                 for (tp = tbuf; tp < tbuf + tlen; tp++)
790                                         emit_log_char(*tp);
791                                 printed_len += tlen;
792                         }
793
794                         if (!*p)
795                                 break;
796                 }
797
798                 emit_log_char(*p);
799                 if (*p == '\n')
800                         new_text_line = 1;
801         }
802
803         /*
804          * Try to acquire and then immediately release the
805          * console semaphore. The release will do all the
806          * actual magic (print out buffers, wake up klogd,
807          * etc). 
808          *
809          * The acquire_console_semaphore_for_printk() function
810          * will release 'logbuf_lock' regardless of whether it
811          * actually gets the semaphore or not.
812          */
813         if (acquire_console_semaphore_for_printk(this_cpu))
814                 release_console_sem();
815
816         lockdep_on();
817 out_restore_irqs:
818         raw_local_irq_restore(flags);
819
820         preempt_enable();
821         return printed_len;
822 }
823 EXPORT_SYMBOL(printk);
824 EXPORT_SYMBOL(vprintk);
825
826 #else
827
828 static void call_console_drivers(unsigned start, unsigned end)
829 {
830 }
831
832 #endif
833
834 static int __add_preferred_console(char *name, int idx, char *options,
835                                    char *brl_options)
836 {
837         struct console_cmdline *c;
838         int i;
839
840         /*
841          *      See if this tty is not yet registered, and
842          *      if we have a slot free.
843          */
844         for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
845                 if (strcmp(console_cmdline[i].name, name) == 0 &&
846                           console_cmdline[i].index == idx) {
847                                 if (!brl_options)
848                                         selected_console = i;
849                                 return 0;
850                 }
851         if (i == MAX_CMDLINECONSOLES)
852                 return -E2BIG;
853         if (!brl_options)
854                 selected_console = i;
855         c = &console_cmdline[i];
856         strlcpy(c->name, name, sizeof(c->name));
857         c->options = options;
858 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
859         c->brl_options = brl_options;
860 #endif
861         c->index = idx;
862         return 0;
863 }
864 /*
865  * Set up a list of consoles.  Called from init/main.c
866  */
867 static int __init console_setup(char *str)
868 {
869         char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
870         char *s, *options, *brl_options = NULL;
871         int idx;
872
873 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
874         if (!memcmp(str, "brl,", 4)) {
875                 brl_options = "";
876                 str += 4;
877         } else if (!memcmp(str, "brl=", 4)) {
878                 brl_options = str + 4;
879                 str = strchr(brl_options, ',');
880                 if (!str) {
881                         printk(KERN_ERR "need port name after brl=\n");
882                         return 1;
883                 }
884                 *(str++) = 0;
885         }
886 #endif
887
888         /*
889          * Decode str into name, index, options.
890          */
891         if (str[0] >= '0' && str[0] <= '9') {
892                 strcpy(buf, "ttyS");
893                 strncpy(buf + 4, str, sizeof(buf) - 5);
894         } else {
895                 strncpy(buf, str, sizeof(buf) - 1);
896         }
897         buf[sizeof(buf) - 1] = 0;
898         if ((options = strchr(str, ',')) != NULL)
899                 *(options++) = 0;
900 #ifdef __sparc__
901         if (!strcmp(str, "ttya"))
902                 strcpy(buf, "ttyS0");
903         if (!strcmp(str, "ttyb"))
904                 strcpy(buf, "ttyS1");
905 #endif
906         for (s = buf; *s; s++)
907                 if ((*s >= '0' && *s <= '9') || *s == ',')
908                         break;
909         idx = simple_strtoul(s, NULL, 10);
910         *s = 0;
911
912         __add_preferred_console(buf, idx, options, brl_options);
913         console_set_on_cmdline = 1;
914         return 1;
915 }
916 __setup("console=", console_setup);
917
918 /**
919  * add_preferred_console - add a device to the list of preferred consoles.
920  * @name: device name
921  * @idx: device index
922  * @options: options for this console
923  *
924  * The last preferred console added will be used for kernel messages
925  * and stdin/out/err for init.  Normally this is used by console_setup
926  * above to handle user-supplied console arguments; however it can also
927  * be used by arch-specific code either to override the user or more
928  * commonly to provide a default console (ie from PROM variables) when
929  * the user has not supplied one.
930  */
931 int add_preferred_console(char *name, int idx, char *options)
932 {
933         return __add_preferred_console(name, idx, options, NULL);
934 }
935
936 int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
937 {
938         struct console_cmdline *c;
939         int i;
940
941         for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
942                 if (strcmp(console_cmdline[i].name, name) == 0 &&
943                           console_cmdline[i].index == idx) {
944                                 c = &console_cmdline[i];
945                                 strlcpy(c->name, name_new, sizeof(c->name));
946                                 c->name[sizeof(c->name) - 1] = 0;
947                                 c->options = options;
948                                 c->index = idx_new;
949                                 return i;
950                 }
951         /* not found */
952         return -1;
953 }
954
955 int console_suspend_enabled = 1;
956 EXPORT_SYMBOL(console_suspend_enabled);
957
958 static int __init console_suspend_disable(char *str)
959 {
960         console_suspend_enabled = 0;
961         return 1;
962 }
963 __setup("no_console_suspend", console_suspend_disable);
964
965 /**
966  * suspend_console - suspend the console subsystem
967  *
968  * This disables printk() while we go into suspend states
969  */
970 void suspend_console(void)
971 {
972         if (!console_suspend_enabled)
973                 return;
974         printk("Suspending console(s) (use no_console_suspend to debug)\n");
975         acquire_console_sem();
976         console_suspended = 1;
977         up(&console_sem);
978 }
979
980 void resume_console(void)
981 {
982         if (!console_suspend_enabled)
983                 return;
984         down(&console_sem);
985         console_suspended = 0;
986         release_console_sem();
987 }
988
989 /**
990  * console_cpu_notify - print deferred console messages after CPU hotplug
991  * @self: notifier struct
992  * @action: CPU hotplug event
993  * @hcpu: unused
994  *
995  * If printk() is called from a CPU that is not online yet, the messages
996  * will be spooled but will not show up on the console.  This function is
997  * called when a new CPU comes online (or fails to come up), and ensures
998  * that any such output gets printed.
999  */
1000 static int __cpuinit console_cpu_notify(struct notifier_block *self,
1001         unsigned long action, void *hcpu)
1002 {
1003         switch (action) {
1004         case CPU_ONLINE:
1005         case CPU_DEAD:
1006         case CPU_DYING:
1007         case CPU_DOWN_FAILED:
1008         case CPU_UP_CANCELED:
1009                 acquire_console_sem();
1010                 release_console_sem();
1011         }
1012         return NOTIFY_OK;
1013 }
1014
1015 /**
1016  * acquire_console_sem - lock the console system for exclusive use.
1017  *
1018  * Acquires a semaphore which guarantees that the caller has
1019  * exclusive access to the console system and the console_drivers list.
1020  *
1021  * Can sleep, returns nothing.
1022  */
1023 void acquire_console_sem(void)
1024 {
1025         BUG_ON(in_interrupt());
1026         down(&console_sem);
1027         if (console_suspended)
1028                 return;
1029         console_locked = 1;
1030         console_may_schedule = 1;
1031 }
1032 EXPORT_SYMBOL(acquire_console_sem);
1033
1034 int try_acquire_console_sem(void)
1035 {
1036         if (down_trylock(&console_sem))
1037                 return -1;
1038         if (console_suspended) {
1039                 up(&console_sem);
1040                 return -1;
1041         }
1042         console_locked = 1;
1043         console_may_schedule = 0;
1044         return 0;
1045 }
1046 EXPORT_SYMBOL(try_acquire_console_sem);
1047
1048 int is_console_locked(void)
1049 {
1050         return console_locked;
1051 }
1052
1053 static DEFINE_PER_CPU(int, printk_pending);
1054
1055 void printk_tick(void)
1056 {
1057         if (__get_cpu_var(printk_pending)) {
1058                 __get_cpu_var(printk_pending) = 0;
1059                 wake_up_interruptible(&log_wait);
1060         }
1061 }
1062
1063 int printk_needs_cpu(int cpu)
1064 {
1065         return per_cpu(printk_pending, cpu);
1066 }
1067
1068 void wake_up_klogd(void)
1069 {
1070         if (waitqueue_active(&log_wait))
1071                 __raw_get_cpu_var(printk_pending) = 1;
1072 }
1073
1074 /**
1075  * release_console_sem - unlock the console system
1076  *
1077  * Releases the semaphore which the caller holds on the console system
1078  * and the console driver list.
1079  *
1080  * While the semaphore was held, console output may have been buffered
1081  * by printk().  If this is the case, release_console_sem() emits
1082  * the output prior to releasing the semaphore.
1083  *
1084  * If there is output waiting for klogd, we wake it up.
1085  *
1086  * release_console_sem() may be called from any context.
1087  */
1088 void release_console_sem(void)
1089 {
1090         unsigned long flags;
1091         unsigned _con_start, _log_end;
1092         unsigned wake_klogd = 0;
1093
1094         if (console_suspended) {
1095                 up(&console_sem);
1096                 return;
1097         }
1098
1099         console_may_schedule = 0;
1100
1101         for ( ; ; ) {
1102                 spin_lock_irqsave(&logbuf_lock, flags);
1103                 wake_klogd |= log_start - log_end;
1104                 if (con_start == log_end)
1105                         break;                  /* Nothing to print */
1106                 _con_start = con_start;
1107                 _log_end = log_end;
1108                 con_start = log_end;            /* Flush */
1109                 spin_unlock(&logbuf_lock);
1110                 stop_critical_timings();        /* don't trace print latency */
1111                 call_console_drivers(_con_start, _log_end);
1112                 start_critical_timings();
1113                 local_irq_restore(flags);
1114         }
1115         console_locked = 0;
1116         up(&console_sem);
1117         spin_unlock_irqrestore(&logbuf_lock, flags);
1118         if (wake_klogd)
1119                 wake_up_klogd();
1120 }
1121 EXPORT_SYMBOL(release_console_sem);
1122
1123 /**
1124  * console_conditional_schedule - yield the CPU if required
1125  *
1126  * If the console code is currently allowed to sleep, and
1127  * if this CPU should yield the CPU to another task, do
1128  * so here.
1129  *
1130  * Must be called within acquire_console_sem().
1131  */
1132 void __sched console_conditional_schedule(void)
1133 {
1134         if (console_may_schedule)
1135                 cond_resched();
1136 }
1137 EXPORT_SYMBOL(console_conditional_schedule);
1138
1139 void console_unblank(void)
1140 {
1141         struct console *c;
1142
1143         /*
1144          * console_unblank can no longer be called in interrupt context unless
1145          * oops_in_progress is set to 1..
1146          */
1147         if (oops_in_progress) {
1148                 if (down_trylock(&console_sem) != 0)
1149                         return;
1150         } else
1151                 acquire_console_sem();
1152
1153         console_locked = 1;
1154         console_may_schedule = 0;
1155         for_each_console(c)
1156                 if ((c->flags & CON_ENABLED) && c->unblank)
1157                         c->unblank();
1158         release_console_sem();
1159 }
1160
1161 /*
1162  * Return the console tty driver structure and its associated index
1163  */
1164 struct tty_driver *console_device(int *index)
1165 {
1166         struct console *c;
1167         struct tty_driver *driver = NULL;
1168
1169         acquire_console_sem();
1170         for_each_console(c) {
1171                 if (!c->device)
1172                         continue;
1173                 driver = c->device(c, index);
1174                 if (driver)
1175                         break;
1176         }
1177         release_console_sem();
1178         return driver;
1179 }
1180
1181 /*
1182  * Prevent further output on the passed console device so that (for example)
1183  * serial drivers can disable console output before suspending a port, and can
1184  * re-enable output afterwards.
1185  */
1186 void console_stop(struct console *console)
1187 {
1188         acquire_console_sem();
1189         console->flags &= ~CON_ENABLED;
1190         release_console_sem();
1191 }
1192 EXPORT_SYMBOL(console_stop);
1193
1194 void console_start(struct console *console)
1195 {
1196         acquire_console_sem();
1197         console->flags |= CON_ENABLED;
1198         release_console_sem();
1199 }
1200 EXPORT_SYMBOL(console_start);
1201
1202 /*
1203  * The console driver calls this routine during kernel initialization
1204  * to register the console printing procedure with printk() and to
1205  * print any messages that were printed by the kernel before the
1206  * console driver was initialized.
1207  *
1208  * This can happen pretty early during the boot process (because of
1209  * early_printk) - sometimes before setup_arch() completes - be careful
1210  * of what kernel features are used - they may not be initialised yet.
1211  *
1212  * There are two types of consoles - bootconsoles (early_printk) and
1213  * "real" consoles (everything which is not a bootconsole) which are
1214  * handled differently.
1215  *  - Any number of bootconsoles can be registered at any time.
1216  *  - As soon as a "real" console is registered, all bootconsoles
1217  *    will be unregistered automatically.
1218  *  - Once a "real" console is registered, any attempt to register a
1219  *    bootconsoles will be rejected
1220  */
1221 void register_console(struct console *newcon)
1222 {
1223         int i;
1224         unsigned long flags;
1225         struct console *bcon = NULL;
1226
1227         /*
1228          * before we register a new CON_BOOT console, make sure we don't
1229          * already have a valid console
1230          */
1231         if (console_drivers && newcon->flags & CON_BOOT) {
1232                 /* find the last or real console */
1233                 for_each_console(bcon) {
1234                         if (!(bcon->flags & CON_BOOT)) {
1235                                 printk(KERN_INFO "Too late to register bootconsole %s%d\n",
1236                                         newcon->name, newcon->index);
1237                                 return;
1238                         }
1239                 }
1240         }
1241
1242         if (console_drivers && console_drivers->flags & CON_BOOT)
1243                 bcon = console_drivers;
1244
1245         if (preferred_console < 0 || bcon || !console_drivers)
1246                 preferred_console = selected_console;
1247
1248         if (newcon->early_setup)
1249                 newcon->early_setup();
1250
1251         /*
1252          *      See if we want to use this console driver. If we
1253          *      didn't select a console we take the first one
1254          *      that registers here.
1255          */
1256         if (preferred_console < 0) {
1257                 if (newcon->index < 0)
1258                         newcon->index = 0;
1259                 if (newcon->setup == NULL ||
1260                     newcon->setup(newcon, NULL) == 0) {
1261                         newcon->flags |= CON_ENABLED;
1262                         if (newcon->device) {
1263                                 newcon->flags |= CON_CONSDEV;
1264                                 preferred_console = 0;
1265                         }
1266                 }
1267         }
1268
1269         /*
1270          *      See if this console matches one we selected on
1271          *      the command line.
1272          */
1273         for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1274                         i++) {
1275                 if (strcmp(console_cmdline[i].name, newcon->name) != 0)
1276                         continue;
1277                 if (newcon->index >= 0 &&
1278                     newcon->index != console_cmdline[i].index)
1279                         continue;
1280                 if (newcon->index < 0)
1281                         newcon->index = console_cmdline[i].index;
1282 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1283                 if (console_cmdline[i].brl_options) {
1284                         newcon->flags |= CON_BRL;
1285                         braille_register_console(newcon,
1286                                         console_cmdline[i].index,
1287                                         console_cmdline[i].options,
1288                                         console_cmdline[i].brl_options);
1289                         return;
1290                 }
1291 #endif
1292                 if (newcon->setup &&
1293                     newcon->setup(newcon, console_cmdline[i].options) != 0)
1294                         break;
1295                 newcon->flags |= CON_ENABLED;
1296                 newcon->index = console_cmdline[i].index;
1297                 if (i == selected_console) {
1298                         newcon->flags |= CON_CONSDEV;
1299                         preferred_console = selected_console;
1300                 }
1301                 break;
1302         }
1303
1304         if (!(newcon->flags & CON_ENABLED))
1305                 return;
1306
1307         /*
1308          * If we have a bootconsole, and are switching to a real console,
1309          * don't print everything out again, since when the boot console, and
1310          * the real console are the same physical device, it's annoying to
1311          * see the beginning boot messages twice
1312          */
1313         if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
1314                 newcon->flags &= ~CON_PRINTBUFFER;
1315
1316         /*
1317          *      Put this console in the list - keep the
1318          *      preferred driver at the head of the list.
1319          */
1320         acquire_console_sem();
1321         if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
1322                 newcon->next = console_drivers;
1323                 console_drivers = newcon;
1324                 if (newcon->next)
1325                         newcon->next->flags &= ~CON_CONSDEV;
1326         } else {
1327                 newcon->next = console_drivers->next;
1328                 console_drivers->next = newcon;
1329         }
1330         if (newcon->flags & CON_PRINTBUFFER) {
1331                 /*
1332                  * release_console_sem() will print out the buffered messages
1333                  * for us.
1334                  */
1335                 spin_lock_irqsave(&logbuf_lock, flags);
1336                 con_start = log_start;
1337                 spin_unlock_irqrestore(&logbuf_lock, flags);
1338         }
1339         release_console_sem();
1340
1341         /*
1342          * By unregistering the bootconsoles after we enable the real console
1343          * we get the "console xxx enabled" message on all the consoles -
1344          * boot consoles, real consoles, etc - this is to ensure that end
1345          * users know there might be something in the kernel's log buffer that
1346          * went to the bootconsole (that they do not see on the real console)
1347          */
1348         if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV)) {
1349                 /* we need to iterate through twice, to make sure we print
1350                  * everything out, before we unregister the console(s)
1351                  */
1352                 printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
1353                         newcon->name, newcon->index);
1354                 for_each_console(bcon)
1355                         if (bcon->flags & CON_BOOT)
1356                                 unregister_console(bcon);
1357         } else {
1358                 printk(KERN_INFO "%sconsole [%s%d] enabled\n",
1359                         (newcon->flags & CON_BOOT) ? "boot" : "" ,
1360                         newcon->name, newcon->index);
1361         }
1362 }
1363 EXPORT_SYMBOL(register_console);
1364
1365 int unregister_console(struct console *console)
1366 {
1367         struct console *a, *b;
1368         int res = 1;
1369
1370 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1371         if (console->flags & CON_BRL)
1372                 return braille_unregister_console(console);
1373 #endif
1374
1375         acquire_console_sem();
1376         if (console_drivers == console) {
1377                 console_drivers=console->next;
1378                 res = 0;
1379         } else if (console_drivers) {
1380                 for (a=console_drivers->next, b=console_drivers ;
1381                      a; b=a, a=b->next) {
1382                         if (a == console) {
1383                                 b->next = a->next;
1384                                 res = 0;
1385                                 break;
1386                         }
1387                 }
1388         }
1389
1390         /*
1391          * If this isn't the last console and it has CON_CONSDEV set, we
1392          * need to set it on the next preferred console.
1393          */
1394         if (console_drivers != NULL && console->flags & CON_CONSDEV)
1395                 console_drivers->flags |= CON_CONSDEV;
1396
1397         release_console_sem();
1398         return res;
1399 }
1400 EXPORT_SYMBOL(unregister_console);
1401
1402 static int __init printk_late_init(void)
1403 {
1404         struct console *con;
1405
1406         for_each_console(con) {
1407                 if (con->flags & CON_BOOT) {
1408                         printk(KERN_INFO "turn off boot console %s%d\n",
1409                                 con->name, con->index);
1410                         unregister_console(con);
1411                 }
1412         }
1413         hotcpu_notifier(console_cpu_notify, 0);
1414         return 0;
1415 }
1416 late_initcall(printk_late_init);
1417
1418 #if defined CONFIG_PRINTK
1419
1420 /*
1421  * printk rate limiting, lifted from the networking subsystem.
1422  *
1423  * This enforces a rate limit: not more than 10 kernel messages
1424  * every 5s to make a denial-of-service attack impossible.
1425  */
1426 DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
1427
1428 int __printk_ratelimit(const char *func)
1429 {
1430         return ___ratelimit(&printk_ratelimit_state, func);
1431 }
1432 EXPORT_SYMBOL(__printk_ratelimit);
1433
1434 /**
1435  * printk_timed_ratelimit - caller-controlled printk ratelimiting
1436  * @caller_jiffies: pointer to caller's state
1437  * @interval_msecs: minimum interval between prints
1438  *
1439  * printk_timed_ratelimit() returns true if more than @interval_msecs
1440  * milliseconds have elapsed since the last time printk_timed_ratelimit()
1441  * returned true.
1442  */
1443 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1444                         unsigned int interval_msecs)
1445 {
1446         if (*caller_jiffies == 0
1447                         || !time_in_range(jiffies, *caller_jiffies,
1448                                         *caller_jiffies
1449                                         + msecs_to_jiffies(interval_msecs))) {
1450                 *caller_jiffies = jiffies;
1451                 return true;
1452         }
1453         return false;
1454 }
1455 EXPORT_SYMBOL(printk_timed_ratelimit);
1456
1457 static DEFINE_SPINLOCK(dump_list_lock);
1458 static LIST_HEAD(dump_list);
1459
1460 /**
1461  * kmsg_dump_register - register a kernel log dumper.
1462  * @dumper: pointer to the kmsg_dumper structure
1463  *
1464  * Adds a kernel log dumper to the system. The dump callback in the
1465  * structure will be called when the kernel oopses or panics and must be
1466  * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
1467  */
1468 int kmsg_dump_register(struct kmsg_dumper *dumper)
1469 {
1470         unsigned long flags;
1471         int err = -EBUSY;
1472
1473         /* The dump callback needs to be set */
1474         if (!dumper->dump)
1475                 return -EINVAL;
1476
1477         spin_lock_irqsave(&dump_list_lock, flags);
1478         /* Don't allow registering multiple times */
1479         if (!dumper->registered) {
1480                 dumper->registered = 1;
1481                 list_add_tail(&dumper->list, &dump_list);
1482                 err = 0;
1483         }
1484         spin_unlock_irqrestore(&dump_list_lock, flags);
1485
1486         return err;
1487 }
1488 EXPORT_SYMBOL_GPL(kmsg_dump_register);
1489
1490 /**
1491  * kmsg_dump_unregister - unregister a kmsg dumper.
1492  * @dumper: pointer to the kmsg_dumper structure
1493  *
1494  * Removes a dump device from the system. Returns zero on success and
1495  * %-EINVAL otherwise.
1496  */
1497 int kmsg_dump_unregister(struct kmsg_dumper *dumper)
1498 {
1499         unsigned long flags;
1500         int err = -EINVAL;
1501
1502         spin_lock_irqsave(&dump_list_lock, flags);
1503         if (dumper->registered) {
1504                 dumper->registered = 0;
1505                 list_del(&dumper->list);
1506                 err = 0;
1507         }
1508         spin_unlock_irqrestore(&dump_list_lock, flags);
1509
1510         return err;
1511 }
1512 EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
1513
1514 static const char const *kmsg_reasons[] = {
1515         [KMSG_DUMP_OOPS]        = "oops",
1516         [KMSG_DUMP_PANIC]       = "panic",
1517         [KMSG_DUMP_KEXEC]       = "kexec",
1518 };
1519
1520 static const char *kmsg_to_str(enum kmsg_dump_reason reason)
1521 {
1522         if (reason >= ARRAY_SIZE(kmsg_reasons) || reason < 0)
1523                 return "unknown";
1524
1525         return kmsg_reasons[reason];
1526 }
1527
1528 /**
1529  * kmsg_dump - dump kernel log to kernel message dumpers.
1530  * @reason: the reason (oops, panic etc) for dumping
1531  *
1532  * Iterate through each of the dump devices and call the oops/panic
1533  * callbacks with the log buffer.
1534  */
1535 void kmsg_dump(enum kmsg_dump_reason reason)
1536 {
1537         unsigned long end;
1538         unsigned chars;
1539         struct kmsg_dumper *dumper;
1540         const char *s1, *s2;
1541         unsigned long l1, l2;
1542         unsigned long flags;
1543
1544         /* Theoretically, the log could move on after we do this, but
1545            there's not a lot we can do about that. The new messages
1546            will overwrite the start of what we dump. */
1547         spin_lock_irqsave(&logbuf_lock, flags);
1548         end = log_end & LOG_BUF_MASK;
1549         chars = logged_chars;
1550         spin_unlock_irqrestore(&logbuf_lock, flags);
1551
1552         if (chars > end) {
1553                 s1 = log_buf + log_buf_len - chars + end;
1554                 l1 = chars - end;
1555
1556                 s2 = log_buf;
1557                 l2 = end;
1558         } else {
1559                 s1 = "";
1560                 l1 = 0;
1561
1562                 s2 = log_buf + end - chars;
1563                 l2 = chars;
1564         }
1565
1566         if (!spin_trylock_irqsave(&dump_list_lock, flags)) {
1567                 printk(KERN_ERR "dump_kmsg: dump list lock is held during %s, skipping dump\n",
1568                                 kmsg_to_str(reason));
1569                 return;
1570         }
1571         list_for_each_entry(dumper, &dump_list, list)
1572                 dumper->dump(dumper, reason, s1, l1, s2, l2);
1573         spin_unlock_irqrestore(&dump_list_lock, flags);
1574 }
1575 #endif