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