printk: convert byte-buffer to variable-length record buffer
[deliverable/linux.git] / kernel / printk.c
CommitLineData
1da177e4
LT
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.
40dc5651 13 * Fixed SMP synchronization, 08/08/99, Manfred Spraul
624dffcb 14 * manfred@colorfullife.com
1da177e4 15 * Rewrote bits to get rid of console_lock
e1f8e874 16 * 01Mar01 Andrew Morton
1da177e4
LT
17 */
18
19#include <linux/kernel.h>
20#include <linux/mm.h>
21#include <linux/tty.h>
22#include <linux/tty_driver.h>
1da177e4
LT
23#include <linux/console.h>
24#include <linux/init.h>
bfe8df3d
RD
25#include <linux/jiffies.h>
26#include <linux/nmi.h>
1da177e4 27#include <linux/module.h>
3b9c0410 28#include <linux/moduleparam.h>
1da177e4 29#include <linux/interrupt.h> /* For in_interrupt() */
1da177e4
LT
30#include <linux/delay.h>
31#include <linux/smp.h>
32#include <linux/security.h>
33#include <linux/bootmem.h>
162a7e75 34#include <linux/memblock.h>
1da177e4 35#include <linux/syscalls.h>
04d491ab 36#include <linux/kexec.h>
d37d39ae 37#include <linux/kdb.h>
3fff4c42 38#include <linux/ratelimit.h>
456b565c 39#include <linux/kmsg_dump.h>
00234592 40#include <linux/syslog.h>
034260d6
KC
41#include <linux/cpu.h>
42#include <linux/notifier.h>
fb842b00 43#include <linux/rculist.h>
1da177e4
LT
44
45#include <asm/uaccess.h>
46
95100358
JB
47#define CREATE_TRACE_POINTS
48#include <trace/events/printk.h>
49
076f9776
IM
50/*
51 * Architectures can override it:
52 */
e17ba73b 53void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
076f9776
IM
54{
55}
56
1da177e4 57/* printk's without a loglevel use this.. */
5af5bcb8 58#define DEFAULT_MESSAGE_LOGLEVEL CONFIG_DEFAULT_MESSAGE_LOGLEVEL
1da177e4
LT
59
60/* We show everything that is MORE important than this.. */
61#define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
62#define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
63
64DECLARE_WAIT_QUEUE_HEAD(log_wait);
65
66int console_printk[4] = {
67 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
68 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
69 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
70 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
71};
72
1da177e4 73/*
0bbfb7c2 74 * Low level drivers may need that to know if they can schedule in
1da177e4
LT
75 * their unblank() callback or not. So let's export it.
76 */
77int oops_in_progress;
78EXPORT_SYMBOL(oops_in_progress);
79
80/*
81 * console_sem protects the console_drivers list, and also
82 * provides serialisation for access to the entire console
83 * driver system.
84 */
5b8c4f23 85static DEFINE_SEMAPHORE(console_sem);
1da177e4 86struct console *console_drivers;
a29d1cfe
IM
87EXPORT_SYMBOL_GPL(console_drivers);
88
1da177e4
LT
89/*
90 * This is used for debugging the mess that is the VT code by
91 * keeping track if we have the console semaphore held. It's
92 * definitely not the perfect debug tool (we don't know if _WE_
93 * hold it are racing, but it helps tracking those weird code
94 * path in the console code where we end up in places I want
95 * locked without the console sempahore held
96 */
557240b4 97static int console_locked, console_suspended;
1da177e4 98
fe3d8ad3
FT
99/*
100 * If exclusive_console is non-NULL then only this console is to be printed to.
101 */
102static struct console *exclusive_console;
103
1da177e4
LT
104/*
105 * Array of consoles built from command line options (console=)
106 */
107struct console_cmdline
108{
109 char name[8]; /* Name of the driver */
110 int index; /* Minor dev. to use */
111 char *options; /* Options for the driver */
f7511d5f
ST
112#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
113 char *brl_options; /* Options for braille driver */
114#endif
1da177e4
LT
115};
116
117#define MAX_CMDLINECONSOLES 8
118
119static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
120static int selected_console = -1;
121static int preferred_console = -1;
9e124fe1
MA
122int console_set_on_cmdline;
123EXPORT_SYMBOL(console_set_on_cmdline);
1da177e4
LT
124
125/* Flag: console code may call schedule() */
126static int console_may_schedule;
127
d59745ce 128#ifdef CONFIG_PRINTK
7ff9554b
KS
129/*
130 * The printk log buffer consists of a chain of concatenated variable
131 * length records. Every record starts with a record header, containing
132 * the overall length of the record.
133 *
134 * The heads to the first and last entry in the buffer, as well as the
135 * sequence numbers of these both entries are maintained when messages
136 * are stored..
137 *
138 * If the heads indicate available messages, the length in the header
139 * tells the start next message. A length == 0 for the next message
140 * indicates a wrap-around to the beginning of the buffer.
141 *
142 * Every record carries the monotonic timestamp in microseconds, as well as
143 * the standard userspace syslog level and syslog facility. The usual
144 * kernel messages use LOG_KERN; userspace-injected messages always carry
145 * a matching syslog facility, by default LOG_USER. The origin of every
146 * message can be reliably determined that way.
147 *
148 * The human readable log message directly follows the message header. The
149 * length of the message text is stored in the header, the stored message
150 * is not terminated.
151 *
152 */
153
154struct log {
155 u64 ts_nsec; /* timestamp in nanoseconds */
156 u16 len; /* length of entire record */
157 u16 text_len; /* length of text buffer */
158 u16 dict_len; /* length of dictionary buffer */
159 u16 level; /* syslog level + facility */
160};
161
162/*
163 * The logbuf_lock protects kmsg buffer, indices, counters. It is also
164 * used in interesting ways to provide interlocking in console_unlock();
165 */
166static DEFINE_RAW_SPINLOCK(logbuf_lock);
d59745ce 167
7ff9554b
KS
168/* cpu currently holding logbuf_lock */
169static volatile unsigned int logbuf_cpu = UINT_MAX;
170
171#define LOG_LINE_MAX 1024
172
173/* record buffer */
174#define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
d59745ce
MM
175static char __log_buf[__LOG_BUF_LEN];
176static char *log_buf = __log_buf;
7ff9554b
KS
177static u32 log_buf_len = __LOG_BUF_LEN;
178
179/* index and sequence number of the first record stored in the buffer */
180static u64 log_first_seq;
181static u32 log_first_idx;
182
183/* index and sequence number of the next record to store in the buffer */
184static u64 log_next_seq;
185static u32 log_next_idx;
186
187/* the next printk record to read after the last 'clear' command */
188static u64 clear_seq;
189static u32 clear_idx;
190
191/* the next printk record to read by syslog(READ) or /proc/kmsg */
192static u64 syslog_seq;
193static u32 syslog_idx;
194
195/* human readable text of the record */
196static char *log_text(const struct log *msg)
197{
198 return (char *)msg + sizeof(struct log);
199}
200
201/* optional key/value pair dictionary attached to the record */
202static char *log_dict(const struct log *msg)
203{
204 return (char *)msg + sizeof(struct log) + msg->text_len;
205}
206
207/* get record by index; idx must point to valid msg */
208static struct log *log_from_idx(u32 idx)
209{
210 struct log *msg = (struct log *)(log_buf + idx);
211
212 /*
213 * A length == 0 record is the end of buffer marker. Wrap around and
214 * read the message at the start of the buffer.
215 */
216 if (!msg->len)
217 return (struct log *)log_buf;
218 return msg;
219}
220
221/* get next record; idx must point to valid msg */
222static u32 log_next(u32 idx)
223{
224 struct log *msg = (struct log *)(log_buf + idx);
225
226 /* length == 0 indicates the end of the buffer; wrap */
227 /*
228 * A length == 0 record is the end of buffer marker. Wrap around and
229 * read the message at the start of the buffer as *this* one, and
230 * return the one after that.
231 */
232 if (!msg->len) {
233 msg = (struct log *)log_buf;
234 return msg->len;
235 }
236 return idx + msg->len;
237}
238
239#if !defined(CONFIG_64BIT) || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
240#define LOG_ALIGN 4
241#else
242#define LOG_ALIGN 8
243#endif
244
245/* insert record into the buffer, discard old ones, update heads */
246static void log_store(int facility, int level,
247 const char *dict, u16 dict_len,
248 const char *text, u16 text_len)
249{
250 struct log *msg;
251 u32 size, pad_len;
252
253 /* number of '\0' padding bytes to next message */
254 size = sizeof(struct log) + text_len + dict_len;
255 pad_len = (-size) & (LOG_ALIGN - 1);
256 size += pad_len;
257
258 while (log_first_seq < log_next_seq) {
259 u32 free;
260
261 if (log_next_idx > log_first_idx)
262 free = max(log_buf_len - log_next_idx, log_first_idx);
263 else
264 free = log_first_idx - log_next_idx;
265
266 if (free > size + sizeof(struct log))
267 break;
268
269 /* drop old messages until we have enough contiuous space */
270 log_first_idx = log_next(log_first_idx);
271 log_first_seq++;
272 }
273
274 if (log_next_idx + size + sizeof(struct log) >= log_buf_len) {
275 /*
276 * This message + an additional empty header does not fit
277 * at the end of the buffer. Add an empty header with len == 0
278 * to signify a wrap around.
279 */
280 memset(log_buf + log_next_idx, 0, sizeof(struct log));
281 log_next_idx = 0;
282 }
283
284 /* fill message */
285 msg = (struct log *)(log_buf + log_next_idx);
286 memcpy(log_text(msg), text, text_len);
287 msg->text_len = text_len;
288 memcpy(log_dict(msg), dict, dict_len);
289 msg->dict_len = dict_len;
290 msg->level = (facility << 3) | (level & 7);
291 msg->ts_nsec = local_clock();
292 memset(log_dict(msg) + dict_len, 0, pad_len);
293 msg->len = sizeof(struct log) + text_len + dict_len + pad_len;
294
295 /* insert message */
296 log_next_idx += msg->len;
297 log_next_seq++;
298}
d59745ce 299
04d491ab
NH
300#ifdef CONFIG_KEXEC
301/*
302 * This appends the listed symbols to /proc/vmcoreinfo
303 *
304 * /proc/vmcoreinfo is used by various utiilties, like crash and makedumpfile to
305 * obtain access to symbols that are otherwise very difficult to locate. These
306 * symbols are specifically used so that utilities can access and extract the
307 * dmesg log from a vmcore file after a crash.
308 */
309void log_buf_kexec_setup(void)
310{
311 VMCOREINFO_SYMBOL(log_buf);
04d491ab 312 VMCOREINFO_SYMBOL(log_buf_len);
7ff9554b
KS
313 VMCOREINFO_SYMBOL(log_first_idx);
314 VMCOREINFO_SYMBOL(log_next_idx);
04d491ab
NH
315}
316#endif
317
162a7e75
MT
318/* requested log_buf_len from kernel cmdline */
319static unsigned long __initdata new_log_buf_len;
320
321/* save requested log_buf_len since it's too early to process it */
1da177e4
LT
322static int __init log_buf_len_setup(char *str)
323{
eed4a2ab 324 unsigned size = memparse(str, &str);
1da177e4
LT
325
326 if (size)
327 size = roundup_pow_of_two(size);
162a7e75
MT
328 if (size > log_buf_len)
329 new_log_buf_len = size;
330
331 return 0;
1da177e4 332}
162a7e75
MT
333early_param("log_buf_len", log_buf_len_setup);
334
335void __init setup_log_buf(int early)
336{
337 unsigned long flags;
162a7e75
MT
338 char *new_log_buf;
339 int free;
340
341 if (!new_log_buf_len)
342 return;
1da177e4 343
162a7e75
MT
344 if (early) {
345 unsigned long mem;
346
347 mem = memblock_alloc(new_log_buf_len, PAGE_SIZE);
1f5026a7 348 if (!mem)
162a7e75
MT
349 return;
350 new_log_buf = __va(mem);
351 } else {
352 new_log_buf = alloc_bootmem_nopanic(new_log_buf_len);
353 }
354
355 if (unlikely(!new_log_buf)) {
356 pr_err("log_buf_len: %ld bytes not available\n",
357 new_log_buf_len);
358 return;
359 }
360
07354eb1 361 raw_spin_lock_irqsave(&logbuf_lock, flags);
162a7e75
MT
362 log_buf_len = new_log_buf_len;
363 log_buf = new_log_buf;
364 new_log_buf_len = 0;
7ff9554b
KS
365 free = __LOG_BUF_LEN - log_next_idx;
366 memcpy(log_buf, __log_buf, __LOG_BUF_LEN);
07354eb1 367 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
162a7e75
MT
368
369 pr_info("log_buf_len: %d\n", log_buf_len);
370 pr_info("early log buf free: %d(%d%%)\n",
371 free, (free * 100) / __LOG_BUF_LEN);
372}
1da177e4 373
bfe8df3d
RD
374#ifdef CONFIG_BOOT_PRINTK_DELAY
375
674dff65 376static int boot_delay; /* msecs delay after each printk during bootup */
3a3b6ed2 377static unsigned long long loops_per_msec; /* based on boot_delay */
bfe8df3d
RD
378
379static int __init boot_delay_setup(char *str)
380{
381 unsigned long lpj;
bfe8df3d
RD
382
383 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
384 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
385
386 get_option(&str, &boot_delay);
387 if (boot_delay > 10 * 1000)
388 boot_delay = 0;
389
3a3b6ed2
DY
390 pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
391 "HZ: %d, loops_per_msec: %llu\n",
392 boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
bfe8df3d
RD
393 return 1;
394}
395__setup("boot_delay=", boot_delay_setup);
396
397static void boot_delay_msec(void)
398{
399 unsigned long long k;
400 unsigned long timeout;
401
402 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
403 return;
404
3a3b6ed2 405 k = (unsigned long long)loops_per_msec * boot_delay;
bfe8df3d
RD
406
407 timeout = jiffies + msecs_to_jiffies(boot_delay);
408 while (k) {
409 k--;
410 cpu_relax();
411 /*
412 * use (volatile) jiffies to prevent
413 * compiler reduction; loop termination via jiffies
414 * is secondary and may or may not happen.
415 */
416 if (time_after(jiffies, timeout))
417 break;
418 touch_nmi_watchdog();
419 }
420}
421#else
422static inline void boot_delay_msec(void)
423{
424}
425#endif
426
eaf06b24
DR
427#ifdef CONFIG_SECURITY_DMESG_RESTRICT
428int dmesg_restrict = 1;
429#else
430int dmesg_restrict;
431#endif
432
ee24aebf
LT
433static int syslog_action_restricted(int type)
434{
435 if (dmesg_restrict)
436 return 1;
437 /* Unless restricted, we allow "read all" and "get buffer size" for everybody */
438 return type != SYSLOG_ACTION_READ_ALL && type != SYSLOG_ACTION_SIZE_BUFFER;
439}
440
441static int check_syslog_permissions(int type, bool from_file)
442{
443 /*
444 * If this is from /proc/kmsg and we've already opened it, then we've
445 * already done the capabilities checks at open time.
446 */
447 if (from_file && type != SYSLOG_ACTION_OPEN)
448 return 0;
449
450 if (syslog_action_restricted(type)) {
451 if (capable(CAP_SYSLOG))
452 return 0;
453 /* For historical reasons, accept CAP_SYS_ADMIN too, with a warning */
454 if (capable(CAP_SYS_ADMIN)) {
f2c0d026
JN
455 printk_once(KERN_WARNING "%s (%d): "
456 "Attempt to access syslog with CAP_SYS_ADMIN "
457 "but no CAP_SYSLOG (deprecated).\n",
458 current->comm, task_pid_nr(current));
ee24aebf
LT
459 return 0;
460 }
461 return -EPERM;
462 }
463 return 0;
464}
465
7ff9554b
KS
466#if defined(CONFIG_PRINTK_TIME)
467static bool printk_time = 1;
468#else
469static bool printk_time;
470#endif
471module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
472
473static int syslog_print_line(u32 idx, char *text, size_t size)
474{
475 struct log *msg;
476 size_t len;
477
478 msg = log_from_idx(idx);
479 if (!text) {
480 /* calculate length only */
481 len = 3;
482
483 if (msg->level > 9)
484 len++;
485 if (msg->level > 99)
486 len++;
487
488 if (printk_time)
489 len += 15;
490
491 len += msg->text_len;
492 len++;
493 return len;
494 }
495
496 len = sprintf(text, "<%u>", msg->level);
497
498 if (printk_time) {
499 unsigned long long t = msg->ts_nsec;
500 unsigned long rem_ns = do_div(t, 1000000000);
501
502 len += sprintf(text + len, "[%5lu.%06lu] ",
503 (unsigned long) t, rem_ns / 1000);
504 }
505
506 if (len + msg->text_len > size)
507 return -EINVAL;
508 memcpy(text + len, log_text(msg), msg->text_len);
509 len += msg->text_len;
510 text[len++] = '\n';
511 return len;
512}
513
514static int syslog_print(char __user *buf, int size)
515{
516 char *text;
517 int len;
518
519 text = kmalloc(LOG_LINE_MAX, GFP_KERNEL);
520 if (!text)
521 return -ENOMEM;
522
523 raw_spin_lock_irq(&logbuf_lock);
524 if (syslog_seq < log_first_seq) {
525 /* messages are gone, move to first one */
526 syslog_seq = log_first_seq;
527 syslog_idx = log_first_idx;
528 }
529 len = syslog_print_line(syslog_idx, text, LOG_LINE_MAX);
530 syslog_idx = log_next(syslog_idx);
531 syslog_seq++;
532 raw_spin_unlock_irq(&logbuf_lock);
533
534 if (len > 0 && copy_to_user(buf, text, len))
535 len = -EFAULT;
536
537 kfree(text);
538 return len;
539}
540
541static int syslog_print_all(char __user *buf, int size, bool clear)
542{
543 char *text;
544 int len = 0;
545
546 text = kmalloc(LOG_LINE_MAX, GFP_KERNEL);
547 if (!text)
548 return -ENOMEM;
549
550 raw_spin_lock_irq(&logbuf_lock);
551 if (buf) {
552 u64 next_seq;
553 u64 seq;
554 u32 idx;
555
556 if (clear_seq < log_first_seq) {
557 /* messages are gone, move to first available one */
558 clear_seq = log_first_seq;
559 clear_idx = log_first_idx;
560 }
561
562 /*
563 * Find first record that fits, including all following records,
564 * into the user-provided buffer for this dump.
565 */
566 seq = clear_seq;
567 idx = clear_idx;
568 while (seq < log_next_seq) {
569 len += syslog_print_line(idx, NULL, 0);
570 idx = log_next(idx);
571 seq++;
572 }
573 seq = clear_seq;
574 idx = clear_idx;
575 while (len > size && seq < log_next_seq) {
576 len -= syslog_print_line(idx, NULL, 0);
577 idx = log_next(idx);
578 seq++;
579 }
580
581 /* last message in this dump */
582 next_seq = log_next_seq;
583
584 len = 0;
585 while (len >= 0 && seq < next_seq) {
586 int textlen;
587
588 textlen = syslog_print_line(idx, text, LOG_LINE_MAX);
589 if (textlen < 0) {
590 len = textlen;
591 break;
592 }
593 idx = log_next(idx);
594 seq++;
595
596 raw_spin_unlock_irq(&logbuf_lock);
597 if (copy_to_user(buf + len, text, textlen))
598 len = -EFAULT;
599 else
600 len += textlen;
601 raw_spin_lock_irq(&logbuf_lock);
602
603 if (seq < log_first_seq) {
604 /* messages are gone, move to next one */
605 seq = log_first_seq;
606 idx = log_first_idx;
607 }
608 }
609 }
610
611 if (clear) {
612 clear_seq = log_next_seq;
613 clear_idx = log_next_idx;
614 }
615 raw_spin_unlock_irq(&logbuf_lock);
616
617 kfree(text);
618 return len;
619}
620
00234592 621int do_syslog(int type, char __user *buf, int len, bool from_file)
1da177e4 622{
7ff9554b
KS
623 bool clear = false;
624 static int saved_console_loglevel = -1;
ee24aebf 625 int error;
1da177e4 626
ee24aebf
LT
627 error = check_syslog_permissions(type, from_file);
628 if (error)
629 goto out;
12b3052c
EP
630
631 error = security_syslog(type);
1da177e4
LT
632 if (error)
633 return error;
634
635 switch (type) {
d78ca3cd 636 case SYSLOG_ACTION_CLOSE: /* Close log */
1da177e4 637 break;
d78ca3cd 638 case SYSLOG_ACTION_OPEN: /* Open log */
1da177e4 639 break;
d78ca3cd 640 case SYSLOG_ACTION_READ: /* Read from log */
1da177e4
LT
641 error = -EINVAL;
642 if (!buf || len < 0)
643 goto out;
644 error = 0;
645 if (!len)
646 goto out;
647 if (!access_ok(VERIFY_WRITE, buf, len)) {
648 error = -EFAULT;
649 goto out;
650 }
40dc5651 651 error = wait_event_interruptible(log_wait,
7ff9554b 652 syslog_seq != log_next_seq);
1da177e4
LT
653 if (error)
654 goto out;
7ff9554b 655 error = syslog_print(buf, len);
1da177e4 656 break;
d78ca3cd
KC
657 /* Read/clear last kernel messages */
658 case SYSLOG_ACTION_READ_CLEAR:
7ff9554b 659 clear = true;
1da177e4 660 /* FALL THRU */
d78ca3cd
KC
661 /* Read last kernel messages */
662 case SYSLOG_ACTION_READ_ALL:
1da177e4
LT
663 error = -EINVAL;
664 if (!buf || len < 0)
665 goto out;
666 error = 0;
667 if (!len)
668 goto out;
669 if (!access_ok(VERIFY_WRITE, buf, len)) {
670 error = -EFAULT;
671 goto out;
672 }
7ff9554b 673 error = syslog_print_all(buf, len, clear);
1da177e4 674 break;
d78ca3cd
KC
675 /* Clear ring buffer */
676 case SYSLOG_ACTION_CLEAR:
7ff9554b 677 syslog_print_all(NULL, 0, true);
d78ca3cd
KC
678 /* Disable logging to console */
679 case SYSLOG_ACTION_CONSOLE_OFF:
1aaad49e
FP
680 if (saved_console_loglevel == -1)
681 saved_console_loglevel = console_loglevel;
1da177e4
LT
682 console_loglevel = minimum_console_loglevel;
683 break;
d78ca3cd
KC
684 /* Enable logging to console */
685 case SYSLOG_ACTION_CONSOLE_ON:
1aaad49e
FP
686 if (saved_console_loglevel != -1) {
687 console_loglevel = saved_console_loglevel;
688 saved_console_loglevel = -1;
689 }
1da177e4 690 break;
d78ca3cd
KC
691 /* Set level of messages printed to console */
692 case SYSLOG_ACTION_CONSOLE_LEVEL:
1da177e4
LT
693 error = -EINVAL;
694 if (len < 1 || len > 8)
695 goto out;
696 if (len < minimum_console_loglevel)
697 len = minimum_console_loglevel;
698 console_loglevel = len;
1aaad49e
FP
699 /* Implicitly re-enable logging to console */
700 saved_console_loglevel = -1;
1da177e4
LT
701 error = 0;
702 break;
d78ca3cd
KC
703 /* Number of chars in the log buffer */
704 case SYSLOG_ACTION_SIZE_UNREAD:
7ff9554b
KS
705 raw_spin_lock_irq(&logbuf_lock);
706 if (syslog_seq < log_first_seq) {
707 /* messages are gone, move to first one */
708 syslog_seq = log_first_seq;
709 syslog_idx = log_first_idx;
710 }
711 if (from_file) {
712 /*
713 * Short-cut for poll(/"proc/kmsg") which simply checks
714 * for pending data, not the size; return the count of
715 * records, not the length.
716 */
717 error = log_next_idx - syslog_idx;
718 } else {
719 u64 seq;
720 u32 idx;
721
722 error = 0;
723 seq = syslog_seq;
724 idx = syslog_idx;
725 while (seq < log_next_seq) {
726 error += syslog_print_line(idx, NULL, 0);
727 idx = log_next(idx);
728 seq++;
729 }
730 }
731 raw_spin_unlock_irq(&logbuf_lock);
1da177e4 732 break;
d78ca3cd
KC
733 /* Size of the log buffer */
734 case SYSLOG_ACTION_SIZE_BUFFER:
1da177e4
LT
735 error = log_buf_len;
736 break;
737 default:
738 error = -EINVAL;
739 break;
740 }
741out:
742 return error;
743}
744
1e7bfb21 745SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
1da177e4 746{
00234592 747 return do_syslog(type, buf, len, SYSLOG_FROM_CALL);
1da177e4
LT
748}
749
67fc4e0c
JW
750#ifdef CONFIG_KGDB_KDB
751/* kdb dmesg command needs access to the syslog buffer. do_syslog()
752 * uses locks so it cannot be used during debugging. Just tell kdb
753 * where the start and end of the physical and logical logs are. This
754 * is equivalent to do_syslog(3).
755 */
756void kdb_syslog_data(char *syslog_data[4])
757{
758 syslog_data[0] = log_buf;
759 syslog_data[1] = log_buf + log_buf_len;
7ff9554b
KS
760 syslog_data[2] = log_buf + log_first_idx;
761 syslog_data[3] = log_buf + log_next_idx;
67fc4e0c
JW
762}
763#endif /* CONFIG_KGDB_KDB */
764
2329abfa 765static bool __read_mostly ignore_loglevel;
79290822 766
99eea6a1 767static int __init ignore_loglevel_setup(char *str)
79290822
IM
768{
769 ignore_loglevel = 1;
770 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
771
c4772d99 772 return 0;
79290822
IM
773}
774
c4772d99 775early_param("ignore_loglevel", ignore_loglevel_setup);
29d4d6df 776module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR);
0eca6b7c
YZ
777MODULE_PARM_DESC(ignore_loglevel, "ignore loglevel setting, to"
778 "print all kernel messages to the console.");
79290822 779
1da177e4
LT
780/*
781 * Call the console drivers, asking them to write out
782 * log_buf[start] to log_buf[end - 1].
ac751efa 783 * The console_lock must be held.
1da177e4 784 */
7ff9554b 785static void call_console_drivers(int level, const char *text, size_t len)
1da177e4 786{
7ff9554b 787 struct console *con;
1da177e4 788
7ff9554b
KS
789 trace_console(text, 0, len, len);
790
791 if (level >= console_loglevel && !ignore_loglevel)
792 return;
793 if (!console_drivers)
794 return;
795
796 for_each_console(con) {
797 if (exclusive_console && con != exclusive_console)
798 continue;
799 if (!(con->flags & CON_ENABLED))
800 continue;
801 if (!con->write)
802 continue;
803 if (!cpu_online(smp_processor_id()) &&
804 !(con->flags & CON_ANYTIME))
805 continue;
806 con->write(con, text, len);
807 }
1da177e4
LT
808}
809
810/*
811 * Zap console related locks when oopsing. Only zap at most once
812 * every 10 seconds, to leave time for slow consoles to print a
813 * full oops.
814 */
815static void zap_locks(void)
816{
817 static unsigned long oops_timestamp;
818
819 if (time_after_eq(jiffies, oops_timestamp) &&
40dc5651 820 !time_after(jiffies, oops_timestamp + 30 * HZ))
1da177e4
LT
821 return;
822
823 oops_timestamp = jiffies;
824
94d24fc4 825 debug_locks_off();
1da177e4 826 /* If a crash is occurring, make sure we can't deadlock */
07354eb1 827 raw_spin_lock_init(&logbuf_lock);
1da177e4 828 /* And make sure that we print immediately */
5b8c4f23 829 sema_init(&console_sem, 1);
1da177e4
LT
830}
831
76a8ad29
ME
832/* Check if we have any console registered that can be called early in boot. */
833static int have_callable_console(void)
834{
835 struct console *con;
836
4d091611 837 for_each_console(con)
76a8ad29
ME
838 if (con->flags & CON_ANYTIME)
839 return 1;
840
841 return 0;
842}
843
266c2e0a
LT
844/*
845 * Can we actually use the console at this time on this cpu?
846 *
847 * Console drivers may assume that per-cpu resources have
848 * been allocated. So unless they're explicitly marked as
849 * being able to cope (CON_ANYTIME) don't call them until
850 * this CPU is officially up.
851 */
852static inline int can_use_console(unsigned int cpu)
853{
854 return cpu_online(cpu) || have_callable_console();
855}
856
857/*
858 * Try to get console ownership to actually show the kernel
859 * messages from a 'printk'. Return true (and with the
ac751efa 860 * console_lock held, and 'console_locked' set) if it
266c2e0a
LT
861 * is successful, false otherwise.
862 *
863 * This gets called with the 'logbuf_lock' spinlock held and
864 * interrupts disabled. It should return with 'lockbuf_lock'
865 * released but interrupts still disabled.
866 */
ac751efa 867static int console_trylock_for_printk(unsigned int cpu)
8155c02a 868 __releases(&logbuf_lock)
266c2e0a 869{
0b5e1c52 870 int retval = 0, wake = 0;
266c2e0a 871
ac751efa 872 if (console_trylock()) {
093a07e2
LT
873 retval = 1;
874
875 /*
876 * If we can't use the console, we need to release
877 * the console semaphore by hand to avoid flushing
878 * the buffer. We need to hold the console semaphore
879 * in order to do this test safely.
880 */
881 if (!can_use_console(cpu)) {
882 console_locked = 0;
0b5e1c52 883 wake = 1;
093a07e2
LT
884 retval = 0;
885 }
886 }
7ff9554b 887 logbuf_cpu = UINT_MAX;
0b5e1c52
PZ
888 if (wake)
889 up(&console_sem);
07354eb1 890 raw_spin_unlock(&logbuf_lock);
266c2e0a
LT
891 return retval;
892}
32a76006 893
af91322e
DY
894int printk_delay_msec __read_mostly;
895
896static inline void printk_delay(void)
897{
898 if (unlikely(printk_delay_msec)) {
899 int m = printk_delay_msec;
900
901 while (m--) {
902 mdelay(1);
903 touch_nmi_watchdog();
904 }
905 }
906}
907
7ff9554b
KS
908asmlinkage int vprintk_emit(int facility, int level,
909 const char *dict, size_t dictlen,
910 const char *fmt, va_list args)
1da177e4 911{
7ff9554b
KS
912 static int recursion_bug;
913 static char buf[LOG_LINE_MAX];
914 static size_t buflen;
915 static int buflevel;
916 static char textbuf[LOG_LINE_MAX];
917 char *text = textbuf;
918 size_t textlen;
ac60ad74 919 unsigned long flags;
32a76006 920 int this_cpu;
7ff9554b
KS
921 bool newline = false;
922 bool cont = false;
923 int printed_len = 0;
1da177e4 924
bfe8df3d 925 boot_delay_msec();
af91322e 926 printk_delay();
bfe8df3d 927
1da177e4 928 /* This stops the holder of console_sem just where we want him */
1a9a8aef 929 local_irq_save(flags);
32a76006
IM
930 this_cpu = smp_processor_id();
931
932 /*
933 * Ouch, printk recursed into itself!
934 */
7ff9554b 935 if (unlikely(logbuf_cpu == this_cpu)) {
32a76006
IM
936 /*
937 * If a crash is occurring during printk() on this CPU,
938 * then try to get the crash message out but make sure
939 * we can't deadlock. Otherwise just return to avoid the
940 * recursion and return - but flag the recursion so that
941 * it can be printed at the next appropriate moment:
942 */
94d24fc4 943 if (!oops_in_progress && !lockdep_recursing(current)) {
3b8945e8 944 recursion_bug = 1;
32a76006
IM
945 goto out_restore_irqs;
946 }
947 zap_locks();
948 }
949
a0f1ccfd 950 lockdep_off();
07354eb1 951 raw_spin_lock(&logbuf_lock);
7ff9554b 952 logbuf_cpu = this_cpu;
1da177e4 953
3b8945e8 954 if (recursion_bug) {
7ff9554b
KS
955 static const char recursion_msg[] =
956 "BUG: recent printk recursion!";
957
3b8945e8 958 recursion_bug = 0;
7ff9554b
KS
959 printed_len += strlen(recursion_msg);
960 /* emit KERN_CRIT message */
961 log_store(0, 2, NULL, 0, recursion_msg, printed_len);
32a76006 962 }
1da177e4 963
7ff9554b
KS
964 /*
965 * The printf needs to come first; we need the syslog
966 * prefix which might be passed-in as a parameter.
967 */
968 textlen = vscnprintf(text, sizeof(textbuf), fmt, args);
5fd29d6c 969
7ff9554b
KS
970 /* mark and strip a trailing newline */
971 if (textlen && text[textlen-1] == '\n') {
972 textlen--;
973 newline = true;
974 }
9d90c8d9 975
7ff9554b
KS
976 /* strip syslog prefix and extract log level or flags */
977 if (text[0] == '<' && text[1] && text[2] == '>') {
978 switch (text[1]) {
979 case '0' ... '7':
980 if (level == -1)
981 level = text[1] - '0';
982 text += 3;
983 textlen -= 3;
984 break;
985 case 'c': /* KERN_CONT */
986 cont = true;
987 case 'd': /* KERN_DEFAULT */
988 text += 3;
989 textlen -= 3;
9d90c8d9 990 break;
5fd29d6c
LT
991 }
992 }
993
7ff9554b
KS
994 if (buflen && (!cont || dict)) {
995 /* no continuation; flush existing buffer */
996 log_store(facility, buflevel, NULL, 0, buf, buflen);
997 printed_len += buflen;
998 buflen = 0;
999 }
9d90c8d9 1000
7ff9554b
KS
1001 if (buflen == 0) {
1002 /* remember level for first message in the buffer */
1003 if (level == -1)
1004 buflevel = default_message_loglevel;
1005 else
1006 buflevel = level;
1007 }
ac60ad74 1008
7ff9554b
KS
1009 if (buflen || !newline) {
1010 /* append to existing buffer, or buffer until next message */
1011 if (buflen + textlen > sizeof(buf))
1012 textlen = sizeof(buf) - buflen;
1013 memcpy(buf + buflen, text, textlen);
1014 buflen += textlen;
1015 }
ac60ad74 1016
7ff9554b
KS
1017 if (newline) {
1018 /* end of line; flush buffer */
1019 if (buflen) {
1020 log_store(facility, buflevel,
1021 dict, dictlen, buf, buflen);
1022 printed_len += buflen;
1023 buflen = 0;
1024 } else {
1025 log_store(facility, buflevel,
1026 dict, dictlen, text, textlen);
1027 printed_len += textlen;
1028 }
1da177e4
LT
1029 }
1030
266c2e0a 1031 /*
7ff9554b
KS
1032 * Try to acquire and then immediately release the console semaphore.
1033 * The release will print out buffers and wake up /dev/kmsg and syslog()
1034 * users.
266c2e0a 1035 *
7ff9554b
KS
1036 * The console_trylock_for_printk() function will release 'logbuf_lock'
1037 * regardless of whether it actually gets the console semaphore or not.
266c2e0a 1038 */
ac751efa
TH
1039 if (console_trylock_for_printk(this_cpu))
1040 console_unlock();
76a8ad29 1041
266c2e0a 1042 lockdep_on();
32a76006 1043out_restore_irqs:
1a9a8aef 1044 local_irq_restore(flags);
76a8ad29 1045
1da177e4
LT
1046 return printed_len;
1047}
7ff9554b
KS
1048EXPORT_SYMBOL(vprintk_emit);
1049
1050asmlinkage int vprintk(const char *fmt, va_list args)
1051{
1052 return vprintk_emit(0, -1, NULL, 0, fmt, args);
1053}
1da177e4
LT
1054EXPORT_SYMBOL(vprintk);
1055
7ff9554b
KS
1056asmlinkage int printk_emit(int facility, int level,
1057 const char *dict, size_t dictlen,
1058 const char *fmt, ...)
1059{
1060 va_list args;
1061 int r;
1062
1063 va_start(args, fmt);
1064 r = vprintk_emit(facility, level, dict, dictlen, fmt, args);
1065 va_end(args);
1066
1067 return r;
1068}
1069EXPORT_SYMBOL(printk_emit);
1070
1071/**
1072 * printk - print a kernel message
1073 * @fmt: format string
1074 *
1075 * This is printk(). It can be called from any context. We want it to work.
1076 *
1077 * We try to grab the console_lock. If we succeed, it's easy - we log the
1078 * output and call the console drivers. If we fail to get the semaphore, we
1079 * place the output into the log buffer and return. The current holder of
1080 * the console_sem will notice the new output in console_unlock(); and will
1081 * send it to the consoles before releasing the lock.
1082 *
1083 * One effect of this deferred printing is that code which calls printk() and
1084 * then changes console_loglevel may break. This is because console_loglevel
1085 * is inspected when the actual printing occurs.
1086 *
1087 * See also:
1088 * printf(3)
1089 *
1090 * See the vsnprintf() documentation for format string extensions over C99.
1091 */
1092asmlinkage int printk(const char *fmt, ...)
1093{
1094 va_list args;
1095 int r;
1096
1097#ifdef CONFIG_KGDB_KDB
1098 if (unlikely(kdb_trap_printk)) {
1099 va_start(args, fmt);
1100 r = vkdb_printf(fmt, args);
1101 va_end(args);
1102 return r;
1103 }
1104#endif
1105 va_start(args, fmt);
1106 r = vprintk_emit(0, -1, NULL, 0, fmt, args);
1107 va_end(args);
1108
1109 return r;
1110}
1111EXPORT_SYMBOL(printk);
d59745ce
MM
1112#else
1113
7ff9554b 1114static void call_console_drivers(int level, const char *text, size_t len)
40dc5651
JJ
1115{
1116}
d59745ce
MM
1117
1118#endif
1119
f7511d5f
ST
1120static int __add_preferred_console(char *name, int idx, char *options,
1121 char *brl_options)
1122{
1123 struct console_cmdline *c;
1124 int i;
1125
1126 /*
1127 * See if this tty is not yet registered, and
1128 * if we have a slot free.
1129 */
1130 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
1131 if (strcmp(console_cmdline[i].name, name) == 0 &&
1132 console_cmdline[i].index == idx) {
1133 if (!brl_options)
1134 selected_console = i;
1135 return 0;
1136 }
1137 if (i == MAX_CMDLINECONSOLES)
1138 return -E2BIG;
1139 if (!brl_options)
1140 selected_console = i;
1141 c = &console_cmdline[i];
1142 strlcpy(c->name, name, sizeof(c->name));
1143 c->options = options;
1144#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1145 c->brl_options = brl_options;
1146#endif
1147 c->index = idx;
1148 return 0;
1149}
2ea1c539
JB
1150/*
1151 * Set up a list of consoles. Called from init/main.c
1152 */
1153static int __init console_setup(char *str)
1154{
eaa944af 1155 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
f7511d5f 1156 char *s, *options, *brl_options = NULL;
2ea1c539
JB
1157 int idx;
1158
f7511d5f
ST
1159#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1160 if (!memcmp(str, "brl,", 4)) {
1161 brl_options = "";
1162 str += 4;
1163 } else if (!memcmp(str, "brl=", 4)) {
1164 brl_options = str + 4;
1165 str = strchr(brl_options, ',');
1166 if (!str) {
1167 printk(KERN_ERR "need port name after brl=\n");
1168 return 1;
1169 }
1170 *(str++) = 0;
1171 }
1172#endif
1173
2ea1c539
JB
1174 /*
1175 * Decode str into name, index, options.
1176 */
1177 if (str[0] >= '0' && str[0] <= '9') {
eaa944af
YL
1178 strcpy(buf, "ttyS");
1179 strncpy(buf + 4, str, sizeof(buf) - 5);
2ea1c539 1180 } else {
eaa944af 1181 strncpy(buf, str, sizeof(buf) - 1);
2ea1c539 1182 }
eaa944af 1183 buf[sizeof(buf) - 1] = 0;
2ea1c539
JB
1184 if ((options = strchr(str, ',')) != NULL)
1185 *(options++) = 0;
1186#ifdef __sparc__
1187 if (!strcmp(str, "ttya"))
eaa944af 1188 strcpy(buf, "ttyS0");
2ea1c539 1189 if (!strcmp(str, "ttyb"))
eaa944af 1190 strcpy(buf, "ttyS1");
2ea1c539 1191#endif
eaa944af 1192 for (s = buf; *s; s++)
2ea1c539
JB
1193 if ((*s >= '0' && *s <= '9') || *s == ',')
1194 break;
1195 idx = simple_strtoul(s, NULL, 10);
1196 *s = 0;
1197
f7511d5f 1198 __add_preferred_console(buf, idx, options, brl_options);
9e124fe1 1199 console_set_on_cmdline = 1;
2ea1c539
JB
1200 return 1;
1201}
1202__setup("console=", console_setup);
1203
3c0547ba
MM
1204/**
1205 * add_preferred_console - add a device to the list of preferred consoles.
ddad86c2
MW
1206 * @name: device name
1207 * @idx: device index
1208 * @options: options for this console
3c0547ba
MM
1209 *
1210 * The last preferred console added will be used for kernel messages
1211 * and stdin/out/err for init. Normally this is used by console_setup
1212 * above to handle user-supplied console arguments; however it can also
1213 * be used by arch-specific code either to override the user or more
1214 * commonly to provide a default console (ie from PROM variables) when
1215 * the user has not supplied one.
1216 */
fb445ee5 1217int add_preferred_console(char *name, int idx, char *options)
3c0547ba 1218{
f7511d5f 1219 return __add_preferred_console(name, idx, options, NULL);
3c0547ba
MM
1220}
1221
b6b1d877 1222int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
18a8bd94
YL
1223{
1224 struct console_cmdline *c;
1225 int i;
1226
1227 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
1228 if (strcmp(console_cmdline[i].name, name) == 0 &&
1229 console_cmdline[i].index == idx) {
1230 c = &console_cmdline[i];
f735295b 1231 strlcpy(c->name, name_new, sizeof(c->name));
18a8bd94
YL
1232 c->name[sizeof(c->name) - 1] = 0;
1233 c->options = options;
1234 c->index = idx_new;
1235 return i;
1236 }
1237 /* not found */
1238 return -1;
1239}
1240
2329abfa 1241bool console_suspend_enabled = 1;
8f4ce8c3
AS
1242EXPORT_SYMBOL(console_suspend_enabled);
1243
1244static int __init console_suspend_disable(char *str)
1245{
1246 console_suspend_enabled = 0;
1247 return 1;
1248}
1249__setup("no_console_suspend", console_suspend_disable);
134620f7
YZ
1250module_param_named(console_suspend, console_suspend_enabled,
1251 bool, S_IRUGO | S_IWUSR);
1252MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
1253 " and hibernate operations");
8f4ce8c3 1254
557240b4
LT
1255/**
1256 * suspend_console - suspend the console subsystem
1257 *
1258 * This disables printk() while we go into suspend states
1259 */
1260void suspend_console(void)
1261{
8f4ce8c3
AS
1262 if (!console_suspend_enabled)
1263 return;
0d63081d 1264 printk("Suspending console(s) (use no_console_suspend to debug)\n");
ac751efa 1265 console_lock();
557240b4 1266 console_suspended = 1;
403f3075 1267 up(&console_sem);
557240b4
LT
1268}
1269
1270void resume_console(void)
1271{
8f4ce8c3
AS
1272 if (!console_suspend_enabled)
1273 return;
403f3075 1274 down(&console_sem);
557240b4 1275 console_suspended = 0;
ac751efa 1276 console_unlock();
557240b4
LT
1277}
1278
034260d6
KC
1279/**
1280 * console_cpu_notify - print deferred console messages after CPU hotplug
1281 * @self: notifier struct
1282 * @action: CPU hotplug event
1283 * @hcpu: unused
1284 *
1285 * If printk() is called from a CPU that is not online yet, the messages
1286 * will be spooled but will not show up on the console. This function is
1287 * called when a new CPU comes online (or fails to come up), and ensures
1288 * that any such output gets printed.
1289 */
1290static int __cpuinit console_cpu_notify(struct notifier_block *self,
1291 unsigned long action, void *hcpu)
1292{
1293 switch (action) {
1294 case CPU_ONLINE:
1295 case CPU_DEAD:
1296 case CPU_DYING:
1297 case CPU_DOWN_FAILED:
1298 case CPU_UP_CANCELED:
ac751efa
TH
1299 console_lock();
1300 console_unlock();
034260d6
KC
1301 }
1302 return NOTIFY_OK;
1303}
1304
1da177e4 1305/**
ac751efa 1306 * console_lock - lock the console system for exclusive use.
1da177e4 1307 *
ac751efa 1308 * Acquires a lock which guarantees that the caller has
1da177e4
LT
1309 * exclusive access to the console system and the console_drivers list.
1310 *
1311 * Can sleep, returns nothing.
1312 */
ac751efa 1313void console_lock(void)
1da177e4 1314{
8abd8e29 1315 BUG_ON(in_interrupt());
1da177e4 1316 down(&console_sem);
403f3075
AH
1317 if (console_suspended)
1318 return;
1da177e4
LT
1319 console_locked = 1;
1320 console_may_schedule = 1;
1321}
ac751efa 1322EXPORT_SYMBOL(console_lock);
1da177e4 1323
ac751efa
TH
1324/**
1325 * console_trylock - try to lock the console system for exclusive use.
1326 *
1327 * Tried to acquire a lock which guarantees that the caller has
1328 * exclusive access to the console system and the console_drivers list.
1329 *
1330 * returns 1 on success, and 0 on failure to acquire the lock.
1331 */
1332int console_trylock(void)
1da177e4
LT
1333{
1334 if (down_trylock(&console_sem))
ac751efa 1335 return 0;
403f3075
AH
1336 if (console_suspended) {
1337 up(&console_sem);
ac751efa 1338 return 0;
403f3075 1339 }
1da177e4
LT
1340 console_locked = 1;
1341 console_may_schedule = 0;
ac751efa 1342 return 1;
1da177e4 1343}
ac751efa 1344EXPORT_SYMBOL(console_trylock);
1da177e4
LT
1345
1346int is_console_locked(void)
1347{
1348 return console_locked;
1349}
1da177e4 1350
3ccf3e83 1351/*
7ff9554b 1352 * Delayed printk version, for scheduler-internal messages:
3ccf3e83
PZ
1353 */
1354#define PRINTK_BUF_SIZE 512
1355
1356#define PRINTK_PENDING_WAKEUP 0x01
1357#define PRINTK_PENDING_SCHED 0x02
1358
b845b517 1359static DEFINE_PER_CPU(int, printk_pending);
3ccf3e83 1360static DEFINE_PER_CPU(char [PRINTK_BUF_SIZE], printk_sched_buf);
b845b517
PZ
1361
1362void printk_tick(void)
e3e8a75d 1363{
40dc11ff 1364 if (__this_cpu_read(printk_pending)) {
3ccf3e83
PZ
1365 int pending = __this_cpu_xchg(printk_pending, 0);
1366 if (pending & PRINTK_PENDING_SCHED) {
1367 char *buf = __get_cpu_var(printk_sched_buf);
1368 printk(KERN_WARNING "[sched_delayed] %s", buf);
1369 }
1370 if (pending & PRINTK_PENDING_WAKEUP)
1371 wake_up_interruptible(&log_wait);
b845b517
PZ
1372 }
1373}
1374
1375int printk_needs_cpu(int cpu)
1376{
40dc11ff 1377 if (cpu_is_offline(cpu))
61ab2544 1378 printk_tick();
40dc11ff 1379 return __this_cpu_read(printk_pending);
b845b517
PZ
1380}
1381
1382void wake_up_klogd(void)
1383{
1384 if (waitqueue_active(&log_wait))
3ccf3e83 1385 this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP);
e3e8a75d
KK
1386}
1387
7ff9554b
KS
1388/* the next printk record to write to the console */
1389static u64 console_seq;
1390static u32 console_idx;
1391
1da177e4 1392/**
ac751efa 1393 * console_unlock - unlock the console system
1da177e4 1394 *
ac751efa 1395 * Releases the console_lock which the caller holds on the console system
1da177e4
LT
1396 * and the console driver list.
1397 *
ac751efa
TH
1398 * While the console_lock was held, console output may have been buffered
1399 * by printk(). If this is the case, console_unlock(); emits
1400 * the output prior to releasing the lock.
1da177e4 1401 *
7ff9554b 1402 * If there is output waiting, we wake it /dev/kmsg and syslog() users.
1da177e4 1403 *
ac751efa 1404 * console_unlock(); may be called from any context.
1da177e4 1405 */
ac751efa 1406void console_unlock(void)
1da177e4 1407{
7ff9554b 1408 static u64 seen_seq;
1da177e4 1409 unsigned long flags;
7ff9554b
KS
1410 bool wake_klogd = false;
1411 bool retry;
1da177e4 1412
557240b4 1413 if (console_suspended) {
403f3075 1414 up(&console_sem);
557240b4
LT
1415 return;
1416 }
78944e54
AD
1417
1418 console_may_schedule = 0;
1419
4f2a8d3c 1420again:
7ff9554b
KS
1421 for (;;) {
1422 struct log *msg;
1423 static char text[LOG_LINE_MAX];
1424 size_t len;
1425 int level;
1426
07354eb1 1427 raw_spin_lock_irqsave(&logbuf_lock, flags);
7ff9554b
KS
1428 if (seen_seq != log_next_seq) {
1429 wake_klogd = true;
1430 seen_seq = log_next_seq;
1431 }
1432
1433 if (console_seq < log_first_seq) {
1434 /* messages are gone, move to first one */
1435 console_seq = log_first_seq;
1436 console_idx = log_first_idx;
1437 }
1438
1439 if (console_seq == log_next_seq)
1440 break;
1441
1442 msg = log_from_idx(console_idx);
1443 level = msg->level & 7;
1444 len = msg->text_len;
1445 if (len+1 >= sizeof(text))
1446 len = sizeof(text)-1;
1447 memcpy(text, log_text(msg), len);
1448 text[len++] = '\n';
1449
1450 console_idx = log_next(console_idx);
1451 console_seq++;
07354eb1 1452 raw_spin_unlock(&logbuf_lock);
7ff9554b 1453
81d68a96 1454 stop_critical_timings(); /* don't trace print latency */
7ff9554b 1455 call_console_drivers(level, text, len);
81d68a96 1456 start_critical_timings();
1da177e4
LT
1457 local_irq_restore(flags);
1458 }
1459 console_locked = 0;
fe3d8ad3
FT
1460
1461 /* Release the exclusive_console once it is used */
1462 if (unlikely(exclusive_console))
1463 exclusive_console = NULL;
1464
07354eb1 1465 raw_spin_unlock(&logbuf_lock);
4f2a8d3c 1466
0b5e1c52 1467 up(&console_sem);
4f2a8d3c
PZ
1468
1469 /*
1470 * Someone could have filled up the buffer again, so re-check if there's
1471 * something to flush. In case we cannot trylock the console_sem again,
1472 * there's a new owner and the console_unlock() from them will do the
1473 * flush, no worries.
1474 */
07354eb1 1475 raw_spin_lock(&logbuf_lock);
7ff9554b 1476 retry = console_seq != log_next_seq;
09dc3cf9
PZ
1477 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
1478
4f2a8d3c
PZ
1479 if (retry && console_trylock())
1480 goto again;
1481
e3e8a75d
KK
1482 if (wake_klogd)
1483 wake_up_klogd();
1da177e4 1484}
ac751efa 1485EXPORT_SYMBOL(console_unlock);
1da177e4 1486
ddad86c2
MW
1487/**
1488 * console_conditional_schedule - yield the CPU if required
1da177e4
LT
1489 *
1490 * If the console code is currently allowed to sleep, and
1491 * if this CPU should yield the CPU to another task, do
1492 * so here.
1493 *
ac751efa 1494 * Must be called within console_lock();.
1da177e4
LT
1495 */
1496void __sched console_conditional_schedule(void)
1497{
1498 if (console_may_schedule)
1499 cond_resched();
1500}
1501EXPORT_SYMBOL(console_conditional_schedule);
1502
1da177e4
LT
1503void console_unblank(void)
1504{
1505 struct console *c;
1506
1507 /*
1508 * console_unblank can no longer be called in interrupt context unless
1509 * oops_in_progress is set to 1..
1510 */
1511 if (oops_in_progress) {
1512 if (down_trylock(&console_sem) != 0)
1513 return;
1514 } else
ac751efa 1515 console_lock();
1da177e4
LT
1516
1517 console_locked = 1;
1518 console_may_schedule = 0;
4d091611 1519 for_each_console(c)
1da177e4
LT
1520 if ((c->flags & CON_ENABLED) && c->unblank)
1521 c->unblank();
ac751efa 1522 console_unlock();
1da177e4 1523}
1da177e4
LT
1524
1525/*
1526 * Return the console tty driver structure and its associated index
1527 */
1528struct tty_driver *console_device(int *index)
1529{
1530 struct console *c;
1531 struct tty_driver *driver = NULL;
1532
ac751efa 1533 console_lock();
4d091611 1534 for_each_console(c) {
1da177e4
LT
1535 if (!c->device)
1536 continue;
1537 driver = c->device(c, index);
1538 if (driver)
1539 break;
1540 }
ac751efa 1541 console_unlock();
1da177e4
LT
1542 return driver;
1543}
1544
1545/*
1546 * Prevent further output on the passed console device so that (for example)
1547 * serial drivers can disable console output before suspending a port, and can
1548 * re-enable output afterwards.
1549 */
1550void console_stop(struct console *console)
1551{
ac751efa 1552 console_lock();
1da177e4 1553 console->flags &= ~CON_ENABLED;
ac751efa 1554 console_unlock();
1da177e4
LT
1555}
1556EXPORT_SYMBOL(console_stop);
1557
1558void console_start(struct console *console)
1559{
ac751efa 1560 console_lock();
1da177e4 1561 console->flags |= CON_ENABLED;
ac751efa 1562 console_unlock();
1da177e4
LT
1563}
1564EXPORT_SYMBOL(console_start);
1565
7bf69395
FDN
1566static int __read_mostly keep_bootcon;
1567
1568static int __init keep_bootcon_setup(char *str)
1569{
1570 keep_bootcon = 1;
1571 printk(KERN_INFO "debug: skip boot console de-registration.\n");
1572
1573 return 0;
1574}
1575
1576early_param("keep_bootcon", keep_bootcon_setup);
1577
1da177e4
LT
1578/*
1579 * The console driver calls this routine during kernel initialization
1580 * to register the console printing procedure with printk() and to
1581 * print any messages that were printed by the kernel before the
1582 * console driver was initialized.
4d091611
RG
1583 *
1584 * This can happen pretty early during the boot process (because of
1585 * early_printk) - sometimes before setup_arch() completes - be careful
1586 * of what kernel features are used - they may not be initialised yet.
1587 *
1588 * There are two types of consoles - bootconsoles (early_printk) and
1589 * "real" consoles (everything which is not a bootconsole) which are
1590 * handled differently.
1591 * - Any number of bootconsoles can be registered at any time.
1592 * - As soon as a "real" console is registered, all bootconsoles
1593 * will be unregistered automatically.
1594 * - Once a "real" console is registered, any attempt to register a
1595 * bootconsoles will be rejected
1da177e4 1596 */
4d091611 1597void register_console(struct console *newcon)
1da177e4 1598{
40dc5651 1599 int i;
1da177e4 1600 unsigned long flags;
4d091611 1601 struct console *bcon = NULL;
1da177e4 1602
4d091611
RG
1603 /*
1604 * before we register a new CON_BOOT console, make sure we don't
1605 * already have a valid console
1606 */
1607 if (console_drivers && newcon->flags & CON_BOOT) {
1608 /* find the last or real console */
1609 for_each_console(bcon) {
1610 if (!(bcon->flags & CON_BOOT)) {
1611 printk(KERN_INFO "Too late to register bootconsole %s%d\n",
1612 newcon->name, newcon->index);
1613 return;
1614 }
1615 }
69331af7
GH
1616 }
1617
4d091611
RG
1618 if (console_drivers && console_drivers->flags & CON_BOOT)
1619 bcon = console_drivers;
1620
1621 if (preferred_console < 0 || bcon || !console_drivers)
1da177e4
LT
1622 preferred_console = selected_console;
1623
4d091611
RG
1624 if (newcon->early_setup)
1625 newcon->early_setup();
18a8bd94 1626
1da177e4
LT
1627 /*
1628 * See if we want to use this console driver. If we
1629 * didn't select a console we take the first one
1630 * that registers here.
1631 */
1632 if (preferred_console < 0) {
4d091611
RG
1633 if (newcon->index < 0)
1634 newcon->index = 0;
1635 if (newcon->setup == NULL ||
1636 newcon->setup(newcon, NULL) == 0) {
1637 newcon->flags |= CON_ENABLED;
1638 if (newcon->device) {
1639 newcon->flags |= CON_CONSDEV;
cd3a1b85
JK
1640 preferred_console = 0;
1641 }
1da177e4
LT
1642 }
1643 }
1644
1645 /*
1646 * See if this console matches one we selected on
1647 * the command line.
1648 */
40dc5651
JJ
1649 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1650 i++) {
4d091611 1651 if (strcmp(console_cmdline[i].name, newcon->name) != 0)
1da177e4 1652 continue;
4d091611
RG
1653 if (newcon->index >= 0 &&
1654 newcon->index != console_cmdline[i].index)
1da177e4 1655 continue;
4d091611
RG
1656 if (newcon->index < 0)
1657 newcon->index = console_cmdline[i].index;
f7511d5f
ST
1658#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1659 if (console_cmdline[i].brl_options) {
4d091611
RG
1660 newcon->flags |= CON_BRL;
1661 braille_register_console(newcon,
f7511d5f
ST
1662 console_cmdline[i].index,
1663 console_cmdline[i].options,
1664 console_cmdline[i].brl_options);
1665 return;
1666 }
1667#endif
4d091611
RG
1668 if (newcon->setup &&
1669 newcon->setup(newcon, console_cmdline[i].options) != 0)
1da177e4 1670 break;
4d091611
RG
1671 newcon->flags |= CON_ENABLED;
1672 newcon->index = console_cmdline[i].index;
ab4af03a 1673 if (i == selected_console) {
4d091611 1674 newcon->flags |= CON_CONSDEV;
ab4af03a
GE
1675 preferred_console = selected_console;
1676 }
1da177e4
LT
1677 break;
1678 }
1679
4d091611 1680 if (!(newcon->flags & CON_ENABLED))
1da177e4
LT
1681 return;
1682
8259cf43
RG
1683 /*
1684 * If we have a bootconsole, and are switching to a real console,
1685 * don't print everything out again, since when the boot console, and
1686 * the real console are the same physical device, it's annoying to
1687 * see the beginning boot messages twice
1688 */
1689 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
4d091611 1690 newcon->flags &= ~CON_PRINTBUFFER;
1da177e4
LT
1691
1692 /*
1693 * Put this console in the list - keep the
1694 * preferred driver at the head of the list.
1695 */
ac751efa 1696 console_lock();
4d091611
RG
1697 if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
1698 newcon->next = console_drivers;
1699 console_drivers = newcon;
1700 if (newcon->next)
1701 newcon->next->flags &= ~CON_CONSDEV;
1da177e4 1702 } else {
4d091611
RG
1703 newcon->next = console_drivers->next;
1704 console_drivers->next = newcon;
1da177e4 1705 }
4d091611 1706 if (newcon->flags & CON_PRINTBUFFER) {
1da177e4 1707 /*
ac751efa 1708 * console_unlock(); will print out the buffered messages
1da177e4
LT
1709 * for us.
1710 */
07354eb1 1711 raw_spin_lock_irqsave(&logbuf_lock, flags);
7ff9554b
KS
1712 console_seq = syslog_seq;
1713 console_idx = syslog_idx;
07354eb1 1714 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
fe3d8ad3
FT
1715 /*
1716 * We're about to replay the log buffer. Only do this to the
1717 * just-registered console to avoid excessive message spam to
1718 * the already-registered consoles.
1719 */
1720 exclusive_console = newcon;
1da177e4 1721 }
ac751efa 1722 console_unlock();
fbc92a34 1723 console_sysfs_notify();
8259cf43
RG
1724
1725 /*
1726 * By unregistering the bootconsoles after we enable the real console
1727 * we get the "console xxx enabled" message on all the consoles -
1728 * boot consoles, real consoles, etc - this is to ensure that end
1729 * users know there might be something in the kernel's log buffer that
1730 * went to the bootconsole (that they do not see on the real console)
1731 */
7bf69395
FDN
1732 if (bcon &&
1733 ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
1734 !keep_bootcon) {
8259cf43
RG
1735 /* we need to iterate through twice, to make sure we print
1736 * everything out, before we unregister the console(s)
1737 */
1738 printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
1739 newcon->name, newcon->index);
1740 for_each_console(bcon)
1741 if (bcon->flags & CON_BOOT)
1742 unregister_console(bcon);
1743 } else {
1744 printk(KERN_INFO "%sconsole [%s%d] enabled\n",
1745 (newcon->flags & CON_BOOT) ? "boot" : "" ,
1746 newcon->name, newcon->index);
1747 }
1da177e4
LT
1748}
1749EXPORT_SYMBOL(register_console);
1750
40dc5651 1751int unregister_console(struct console *console)
1da177e4 1752{
40dc5651 1753 struct console *a, *b;
1da177e4
LT
1754 int res = 1;
1755
f7511d5f
ST
1756#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1757 if (console->flags & CON_BRL)
1758 return braille_unregister_console(console);
1759#endif
1760
ac751efa 1761 console_lock();
1da177e4
LT
1762 if (console_drivers == console) {
1763 console_drivers=console->next;
1764 res = 0;
e9b15b54 1765 } else if (console_drivers) {
1da177e4
LT
1766 for (a=console_drivers->next, b=console_drivers ;
1767 a; b=a, a=b->next) {
1768 if (a == console) {
1769 b->next = a->next;
1770 res = 0;
1771 break;
40dc5651 1772 }
1da177e4
LT
1773 }
1774 }
40dc5651 1775
69331af7 1776 /*
ab4af03a
GE
1777 * If this isn't the last console and it has CON_CONSDEV set, we
1778 * need to set it on the next preferred console.
1da177e4 1779 */
69331af7 1780 if (console_drivers != NULL && console->flags & CON_CONSDEV)
ab4af03a 1781 console_drivers->flags |= CON_CONSDEV;
1da177e4 1782
ac751efa 1783 console_unlock();
fbc92a34 1784 console_sysfs_notify();
1da177e4
LT
1785 return res;
1786}
1787EXPORT_SYMBOL(unregister_console);
d59745ce 1788
034260d6 1789static int __init printk_late_init(void)
0c5564bd 1790{
4d091611
RG
1791 struct console *con;
1792
1793 for_each_console(con) {
4c30c6f5 1794 if (!keep_bootcon && con->flags & CON_BOOT) {
cb00e99c 1795 printk(KERN_INFO "turn off boot console %s%d\n",
4d091611 1796 con->name, con->index);
42c2c8c8 1797 unregister_console(con);
cb00e99c 1798 }
0c5564bd 1799 }
034260d6 1800 hotcpu_notifier(console_cpu_notify, 0);
0c5564bd
RG
1801 return 0;
1802}
034260d6 1803late_initcall(printk_late_init);
0c5564bd 1804
7ef3d2fd 1805#if defined CONFIG_PRINTK
717115e1 1806
600e1458
PZ
1807int printk_sched(const char *fmt, ...)
1808{
1809 unsigned long flags;
1810 va_list args;
1811 char *buf;
1812 int r;
1813
1814 local_irq_save(flags);
1815 buf = __get_cpu_var(printk_sched_buf);
1816
1817 va_start(args, fmt);
1818 r = vsnprintf(buf, PRINTK_BUF_SIZE, fmt, args);
1819 va_end(args);
1820
1821 __this_cpu_or(printk_pending, PRINTK_PENDING_SCHED);
1822 local_irq_restore(flags);
1823
1824 return r;
1825}
1826
1da177e4
LT
1827/*
1828 * printk rate limiting, lifted from the networking subsystem.
1829 *
641de9d8
UKK
1830 * This enforces a rate limit: not more than 10 kernel messages
1831 * every 5s to make a denial-of-service attack impossible.
1da177e4 1832 */
641de9d8
UKK
1833DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
1834
5c828713 1835int __printk_ratelimit(const char *func)
1da177e4 1836{
5c828713 1837 return ___ratelimit(&printk_ratelimit_state, func);
1da177e4 1838}
5c828713 1839EXPORT_SYMBOL(__printk_ratelimit);
f46c4833
AM
1840
1841/**
1842 * printk_timed_ratelimit - caller-controlled printk ratelimiting
1843 * @caller_jiffies: pointer to caller's state
1844 * @interval_msecs: minimum interval between prints
1845 *
1846 * printk_timed_ratelimit() returns true if more than @interval_msecs
1847 * milliseconds have elapsed since the last time printk_timed_ratelimit()
1848 * returned true.
1849 */
1850bool printk_timed_ratelimit(unsigned long *caller_jiffies,
1851 unsigned int interval_msecs)
1852{
f2d28a2e
GK
1853 if (*caller_jiffies == 0
1854 || !time_in_range(jiffies, *caller_jiffies,
1855 *caller_jiffies
1856 + msecs_to_jiffies(interval_msecs))) {
1857 *caller_jiffies = jiffies;
f46c4833
AM
1858 return true;
1859 }
1860 return false;
1861}
1862EXPORT_SYMBOL(printk_timed_ratelimit);
456b565c
SK
1863
1864static DEFINE_SPINLOCK(dump_list_lock);
1865static LIST_HEAD(dump_list);
1866
1867/**
1868 * kmsg_dump_register - register a kernel log dumper.
6485536b 1869 * @dumper: pointer to the kmsg_dumper structure
456b565c
SK
1870 *
1871 * Adds a kernel log dumper to the system. The dump callback in the
1872 * structure will be called when the kernel oopses or panics and must be
1873 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
1874 */
1875int kmsg_dump_register(struct kmsg_dumper *dumper)
1876{
1877 unsigned long flags;
1878 int err = -EBUSY;
1879
1880 /* The dump callback needs to be set */
1881 if (!dumper->dump)
1882 return -EINVAL;
1883
1884 spin_lock_irqsave(&dump_list_lock, flags);
1885 /* Don't allow registering multiple times */
1886 if (!dumper->registered) {
1887 dumper->registered = 1;
fb842b00 1888 list_add_tail_rcu(&dumper->list, &dump_list);
456b565c
SK
1889 err = 0;
1890 }
1891 spin_unlock_irqrestore(&dump_list_lock, flags);
1892
1893 return err;
1894}
1895EXPORT_SYMBOL_GPL(kmsg_dump_register);
1896
1897/**
1898 * kmsg_dump_unregister - unregister a kmsg dumper.
6485536b 1899 * @dumper: pointer to the kmsg_dumper structure
456b565c
SK
1900 *
1901 * Removes a dump device from the system. Returns zero on success and
1902 * %-EINVAL otherwise.
1903 */
1904int kmsg_dump_unregister(struct kmsg_dumper *dumper)
1905{
1906 unsigned long flags;
1907 int err = -EINVAL;
1908
1909 spin_lock_irqsave(&dump_list_lock, flags);
1910 if (dumper->registered) {
1911 dumper->registered = 0;
fb842b00 1912 list_del_rcu(&dumper->list);
456b565c
SK
1913 err = 0;
1914 }
1915 spin_unlock_irqrestore(&dump_list_lock, flags);
fb842b00 1916 synchronize_rcu();
456b565c
SK
1917
1918 return err;
1919}
1920EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
1921
7ff9554b
KS
1922static bool always_kmsg_dump;
1923module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR);
1924
456b565c
SK
1925/**
1926 * kmsg_dump - dump kernel log to kernel message dumpers.
1927 * @reason: the reason (oops, panic etc) for dumping
1928 *
1929 * Iterate through each of the dump devices and call the oops/panic
1930 * callbacks with the log buffer.
1931 */
1932void kmsg_dump(enum kmsg_dump_reason reason)
1933{
7ff9554b 1934 u64 idx;
456b565c
SK
1935 struct kmsg_dumper *dumper;
1936 const char *s1, *s2;
1937 unsigned long l1, l2;
1938 unsigned long flags;
1939
c22ab332
MG
1940 if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
1941 return;
1942
456b565c
SK
1943 /* Theoretically, the log could move on after we do this, but
1944 there's not a lot we can do about that. The new messages
1945 will overwrite the start of what we dump. */
7ff9554b 1946
07354eb1 1947 raw_spin_lock_irqsave(&logbuf_lock, flags);
7ff9554b
KS
1948 if (syslog_seq < log_first_seq)
1949 idx = syslog_idx;
1950 else
1951 idx = log_first_idx;
456b565c 1952
7ff9554b
KS
1953 if (idx > log_next_idx) {
1954 s1 = log_buf;
1955 l1 = log_next_idx;
456b565c 1956
7ff9554b
KS
1957 s2 = log_buf + idx;
1958 l2 = log_buf_len - idx;
456b565c
SK
1959 } else {
1960 s1 = "";
1961 l1 = 0;
1962
7ff9554b
KS
1963 s2 = log_buf + idx;
1964 l2 = log_next_idx - idx;
456b565c 1965 }
7ff9554b 1966 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
456b565c 1967
fb842b00
HY
1968 rcu_read_lock();
1969 list_for_each_entry_rcu(dumper, &dump_list, list)
456b565c 1970 dumper->dump(dumper, reason, s1, l1, s2, l2);
fb842b00 1971 rcu_read_unlock();
456b565c 1972}
7ef3d2fd 1973#endif
This page took 0.728652 seconds and 5 git commands to generate.