blob: 74357329550fc4e68446234b757d57d2930a8851 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
Jesper Juhl40dc5652005-10-30 15:02:46 -080013 * Fixed SMP synchronization, 08/08/99, Manfred Spraul
Christian Kujau624dffc2006-01-15 02:43:54 +010014 * manfred@colorfullife.com
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Rewrote bits to get rid of console_lock
Francois Camie1f8e872008-10-15 22:01:59 -070016 * 01Mar01 Andrew Morton
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
19#include <linux/kernel.h>
20#include <linux/mm.h>
21#include <linux/tty.h>
22#include <linux/tty_driver.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/console.h>
24#include <linux/init.h>
Randy Dunlapbfe8df32007-10-16 01:23:46 -070025#include <linux/jiffies.h>
26#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
Jan Engelhardt3b9c0412006-06-25 05:48:15 -070028#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/interrupt.h> /* For in_interrupt() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/delay.h>
31#include <linux/smp.h>
32#include <linux/security.h>
33#include <linux/bootmem.h>
Mike Travis162a7e72011-05-24 17:13:20 -070034#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/syscalls.h>
Neil Horman04d491a2009-04-02 16:58:57 -070036#include <linux/kexec.h>
Jason Wesseld37d39a2010-05-20 21:04:27 -050037#include <linux/kdb.h>
Ingo Molnar3fff4c42009-09-22 16:18:09 +020038#include <linux/ratelimit.h>
Simon Kagstrom456b5652009-10-16 14:09:18 +020039#include <linux/kmsg_dump.h>
Kees Cook00234592010-02-03 15:36:43 -080040#include <linux/syslog.h>
Kevin Cernekee034260d2010-06-03 22:11:25 -070041#include <linux/cpu.h>
42#include <linux/notifier.h>
Huang Yingfb842b02011-01-12 16:59:43 -080043#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <asm/uaccess.h>
46
Johannes Berg95100352011-11-24 20:03:08 +010047#define CREATE_TRACE_POINTS
48#include <trace/events/printk.h>
49
Ingo Molnar076f9772008-01-30 13:33:06 +010050/*
51 * Architectures can override it:
52 */
Jiri Slabye17ba732008-05-12 15:44:40 +020053void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
Ingo Molnar076f9772008-01-30 13:33:06 +010054{
55}
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/* printk's without a loglevel use this.. */
Mandeep Singh Baines5af5bcb2011-03-22 16:34:23 -070058#define DEFAULT_MESSAGE_LOGLEVEL CONFIG_DEFAULT_MESSAGE_LOGLEVEL
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/*
Patrick Pletscher0bbfb7c2007-02-17 20:10:16 +010074 * Low level drivers may need that to know if they can schedule in
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * 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 */
Thomas Gleixner5b8c4f22010-09-07 14:33:43 +000085static DEFINE_SEMAPHORE(console_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086struct console *console_drivers;
Ingo Molnara29d1cf2008-06-02 13:19:08 +020087EXPORT_SYMBOL_GPL(console_drivers);
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/*
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 */
Linus Torvalds557240b2006-06-19 18:16:01 -070097static int console_locked, console_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99/*
Feng Tangfe3d8ad2011-03-22 16:34:21 -0700100 * If exclusive_console is non-NULL then only this console is to be printed to.
101 */
102static struct console *exclusive_console;
103
104/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * 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 */
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700112#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
113 char *brl_options; /* Options for braille driver */
114#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115};
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;
Markus Armbruster9e124fe2008-05-26 23:31:07 +0100122int console_set_on_cmdline;
123EXPORT_SYMBOL(console_set_on_cmdline);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125/* Flag: console code may call schedule() */
126static int console_may_schedule;
127
Matt Mackalld59745c2005-05-01 08:59:02 -0700128#ifdef CONFIG_PRINTK
Kay Sievers7ff95542012-05-03 02:29:13 +0200129/*
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 */
Matt Mackalld59745c2005-05-01 08:59:02 -0700153
Kay Sievers7ff95542012-05-03 02:29:13 +0200154struct 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);
167
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)
Matt Mackalld59745c2005-05-01 08:59:02 -0700175static char __log_buf[__LOG_BUF_LEN];
176static char *log_buf = __log_buf;
Kay Sievers7ff95542012-05-03 02:29:13 +0200177static 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}
Matt Mackalld59745c2005-05-01 08:59:02 -0700299
Neil Horman04d491a2009-04-02 16:58:57 -0700300#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);
Neil Horman04d491a2009-04-02 16:58:57 -0700312 VMCOREINFO_SYMBOL(log_buf_len);
Kay Sievers7ff95542012-05-03 02:29:13 +0200313 VMCOREINFO_SYMBOL(log_first_idx);
314 VMCOREINFO_SYMBOL(log_next_idx);
Neil Horman04d491a2009-04-02 16:58:57 -0700315}
316#endif
317
Mike Travis162a7e72011-05-24 17:13:20 -0700318/* 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322static int __init log_buf_len_setup(char *str)
323{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800324 unsigned size = memparse(str, &str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 if (size)
327 size = roundup_pow_of_two(size);
Mike Travis162a7e72011-05-24 17:13:20 -0700328 if (size > log_buf_len)
329 new_log_buf_len = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Mike Travis162a7e72011-05-24 17:13:20 -0700331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
Mike Travis162a7e72011-05-24 17:13:20 -0700333early_param("log_buf_len", log_buf_len_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Mike Travis162a7e72011-05-24 17:13:20 -0700335void __init setup_log_buf(int early)
336{
337 unsigned long flags;
Mike Travis162a7e72011-05-24 17:13:20 -0700338 char *new_log_buf;
339 int free;
340
341 if (!new_log_buf_len)
342 return;
343
344 if (early) {
345 unsigned long mem;
346
347 mem = memblock_alloc(new_log_buf_len, PAGE_SIZE);
Tejun Heo1f5026a2011-07-12 09:58:09 +0200348 if (!mem)
Mike Travis162a7e72011-05-24 17:13:20 -0700349 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
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200361 raw_spin_lock_irqsave(&logbuf_lock, flags);
Mike Travis162a7e72011-05-24 17:13:20 -0700362 log_buf_len = new_log_buf_len;
363 log_buf = new_log_buf;
364 new_log_buf_len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +0200365 free = __LOG_BUF_LEN - log_next_idx;
366 memcpy(log_buf, __log_buf, __LOG_BUF_LEN);
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200367 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
Mike Travis162a7e72011-05-24 17:13:20 -0700368
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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700374#ifdef CONFIG_BOOT_PRINTK_DELAY
375
Namhyung Kim674dff62010-10-26 14:22:48 -0700376static int boot_delay; /* msecs delay after each printk during bootup */
Dave Young3a3b6ed2009-09-22 16:43:31 -0700377static unsigned long long loops_per_msec; /* based on boot_delay */
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700378
379static int __init boot_delay_setup(char *str)
380{
381 unsigned long lpj;
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700382
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
Dave Young3a3b6ed2009-09-22 16:43:31 -0700390 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);
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700393 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
Dave Young3a3b6ed2009-09-22 16:43:31 -0700405 k = (unsigned long long)loops_per_msec * boot_delay;
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700406
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
Dan Rosenbergeaf06b22010-11-11 14:05:18 -0800427#ifdef CONFIG_SECURITY_DMESG_RESTRICT
428int dmesg_restrict = 1;
429#else
430int dmesg_restrict;
431#endif
432
Linus Torvaldsee24aeb2011-02-10 17:53:55 -0800433static 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)) {
Jonathan Niederf2c0d022011-08-08 06:22:43 +0200455 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));
Linus Torvaldsee24aeb2011-02-10 17:53:55 -0800459 return 0;
460 }
461 return -EPERM;
462 }
463 return 0;
464}
465
Kay Sievers7ff95542012-05-03 02:29:13 +0200466#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
Kees Cook00234592010-02-03 15:36:43 -0800621int do_syslog(int type, char __user *buf, int len, bool from_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Kay Sievers7ff95542012-05-03 02:29:13 +0200623 bool clear = false;
624 static int saved_console_loglevel = -1;
Linus Torvaldsee24aeb2011-02-10 17:53:55 -0800625 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Linus Torvaldsee24aeb2011-02-10 17:53:55 -0800627 error = check_syslog_permissions(type, from_file);
628 if (error)
629 goto out;
Eric Paris12b30522010-11-15 18:36:29 -0500630
631 error = security_syslog(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (error)
633 return error;
634
635 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -0800636 case SYSLOG_ACTION_CLOSE: /* Close log */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800638 case SYSLOG_ACTION_OPEN: /* Open log */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800640 case SYSLOG_ACTION_READ: /* Read from log */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 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 }
Jesper Juhl40dc5652005-10-30 15:02:46 -0800651 error = wait_event_interruptible(log_wait,
Kay Sievers7ff95542012-05-03 02:29:13 +0200652 syslog_seq != log_next_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (error)
654 goto out;
Kay Sievers7ff95542012-05-03 02:29:13 +0200655 error = syslog_print(buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800657 /* Read/clear last kernel messages */
658 case SYSLOG_ACTION_READ_CLEAR:
Kay Sievers7ff95542012-05-03 02:29:13 +0200659 clear = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 /* FALL THRU */
Kees Cookd78ca3c2010-02-03 15:37:13 -0800661 /* Read last kernel messages */
662 case SYSLOG_ACTION_READ_ALL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 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 }
Kay Sievers7ff95542012-05-03 02:29:13 +0200673 error = syslog_print_all(buf, len, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800675 /* Clear ring buffer */
676 case SYSLOG_ACTION_CLEAR:
Kay Sievers7ff95542012-05-03 02:29:13 +0200677 syslog_print_all(NULL, 0, true);
Kees Cookd78ca3c2010-02-03 15:37:13 -0800678 /* Disable logging to console */
679 case SYSLOG_ACTION_CONSOLE_OFF:
Frans Pop1aaad492009-07-06 13:31:48 +0200680 if (saved_console_loglevel == -1)
681 saved_console_loglevel = console_loglevel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 console_loglevel = minimum_console_loglevel;
683 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800684 /* Enable logging to console */
685 case SYSLOG_ACTION_CONSOLE_ON:
Frans Pop1aaad492009-07-06 13:31:48 +0200686 if (saved_console_loglevel != -1) {
687 console_loglevel = saved_console_loglevel;
688 saved_console_loglevel = -1;
689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800691 /* Set level of messages printed to console */
692 case SYSLOG_ACTION_CONSOLE_LEVEL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 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;
Frans Pop1aaad492009-07-06 13:31:48 +0200699 /* Implicitly re-enable logging to console */
700 saved_console_loglevel = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 error = 0;
702 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800703 /* Number of chars in the log buffer */
704 case SYSLOG_ACTION_SIZE_UNREAD:
Kay Sievers7ff95542012-05-03 02:29:13 +0200705 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -0800733 /* Size of the log buffer */
734 case SYSLOG_ACTION_SIZE_BUFFER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 error = log_buf_len;
736 break;
737 default:
738 error = -EINVAL;
739 break;
740 }
741out:
742 return error;
743}
744
Heiko Carstens1e7bfb22009-01-14 14:14:29 +0100745SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Kees Cook00234592010-02-03 15:36:43 -0800747 return do_syslog(type, buf, len, SYSLOG_FROM_CALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
Jason Wessel67fc4e02010-05-20 21:04:21 -0500750#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;
Kay Sievers7ff95542012-05-03 02:29:13 +0200760 syslog_data[2] = log_buf + log_first_idx;
761 syslog_data[3] = log_buf + log_next_idx;
Jason Wessel67fc4e02010-05-20 21:04:21 -0500762}
763#endif /* CONFIG_KGDB_KDB */
764
Rusty Russell2329abf2012-01-13 09:32:18 +1030765static bool __read_mostly ignore_loglevel;
Ingo Molnar792908222006-12-06 20:40:51 -0800766
Adrian Bunk99eea6a2006-12-22 01:07:00 -0800767static int __init ignore_loglevel_setup(char *str)
Ingo Molnar792908222006-12-06 20:40:51 -0800768{
769 ignore_loglevel = 1;
770 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
771
Ingo Molnarc4772d92008-01-31 22:45:23 +0100772 return 0;
Ingo Molnar792908222006-12-06 20:40:51 -0800773}
774
Ingo Molnarc4772d92008-01-31 22:45:23 +0100775early_param("ignore_loglevel", ignore_loglevel_setup);
Rusty Russell29d4d6d2012-01-13 09:32:17 +1030776module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR);
Yanmin Zhang0eca6b72011-10-31 17:11:25 -0700777MODULE_PARM_DESC(ignore_loglevel, "ignore loglevel setting, to"
778 "print all kernel messages to the console.");
Ingo Molnar792908222006-12-06 20:40:51 -0800779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 * Call the console drivers, asking them to write out
782 * log_buf[start] to log_buf[end - 1].
Torben Hohnac751ef2011-01-25 15:07:35 -0800783 * The console_lock must be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 */
Kay Sievers7ff95542012-05-03 02:29:13 +0200785static void call_console_drivers(int level, const char *text, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Kay Sievers7ff95542012-05-03 02:29:13 +0200787 struct console *con;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Kay Sievers7ff95542012-05-03 02:29:13 +0200789 trace_console(text, 0, len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Kay Sievers7ff95542012-05-03 02:29:13 +0200791 if (level >= console_loglevel && !ignore_loglevel)
792 return;
793 if (!console_drivers)
794 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Kay Sievers7ff95542012-05-03 02:29:13 +0200796 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
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) &&
Jesper Juhl40dc5652005-10-30 15:02:46 -0800820 !time_after(jiffies, oops_timestamp + 30 * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return;
822
823 oops_timestamp = jiffies;
824
Peter Zijlstra94d24fc2011-06-07 11:17:30 +0200825 debug_locks_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /* If a crash is occurring, make sure we can't deadlock */
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200827 raw_spin_lock_init(&logbuf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 /* And make sure that we print immediately */
Thomas Gleixner5b8c4f22010-09-07 14:33:43 +0000829 sema_init(&console_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700832/* 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
Robin Getz4d091612009-07-01 21:08:37 -0400837 for_each_console(con)
Michael Ellerman76a8ad22006-06-25 05:47:40 -0700838 if (con->flags & CON_ANYTIME)
839 return 1;
840
841 return 0;
842}
843
Linus Torvalds266c2e02008-03-24 19:25:08 -0700844/*
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
Torben Hohnac751ef2011-01-25 15:07:35 -0800860 * console_lock held, and 'console_locked' set) if it
Linus Torvalds266c2e02008-03-24 19:25:08 -0700861 * 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 */
Torben Hohnac751ef2011-01-25 15:07:35 -0800867static int console_trylock_for_printk(unsigned int cpu)
Namhyung Kim8155c022010-10-26 14:22:47 -0700868 __releases(&logbuf_lock)
Linus Torvalds266c2e02008-03-24 19:25:08 -0700869{
Peter Zijlstra0b5e1c52011-06-07 11:15:33 +0200870 int retval = 0, wake = 0;
Linus Torvalds266c2e02008-03-24 19:25:08 -0700871
Torben Hohnac751ef2011-01-25 15:07:35 -0800872 if (console_trylock()) {
Linus Torvalds093a07e2008-04-15 13:09:54 -0700873 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;
Peter Zijlstra0b5e1c52011-06-07 11:15:33 +0200883 wake = 1;
Linus Torvalds093a07e2008-04-15 13:09:54 -0700884 retval = 0;
885 }
886 }
Kay Sievers7ff95542012-05-03 02:29:13 +0200887 logbuf_cpu = UINT_MAX;
Peter Zijlstra0b5e1c52011-06-07 11:15:33 +0200888 if (wake)
889 up(&console_sem);
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200890 raw_spin_unlock(&logbuf_lock);
Linus Torvalds266c2e02008-03-24 19:25:08 -0700891 return retval;
892}
Ingo Molnar32a76002008-01-25 21:07:58 +0100893
Dave Youngaf913222009-09-22 16:43:33 -0700894int 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
Kay Sievers7ff95542012-05-03 02:29:13 +0200908asmlinkage int vprintk_emit(int facility, int level,
909 const char *dict, size_t dictlen,
910 const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Kay Sievers7ff95542012-05-03 02:29:13 +0200912 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;
Nick Andrewac60ad72008-05-12 21:21:04 +0200919 unsigned long flags;
Ingo Molnar32a76002008-01-25 21:07:58 +0100920 int this_cpu;
Kay Sievers7ff95542012-05-03 02:29:13 +0200921 bool newline = false;
922 bool cont = false;
923 int printed_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700925 boot_delay_msec();
Dave Youngaf913222009-09-22 16:43:33 -0700926 printk_delay();
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 /* This stops the holder of console_sem just where we want him */
Peter Zijlstra1a9a8ae2011-06-07 11:17:30 +0200929 local_irq_save(flags);
Ingo Molnar32a76002008-01-25 21:07:58 +0100930 this_cpu = smp_processor_id();
931
932 /*
933 * Ouch, printk recursed into itself!
934 */
Kay Sievers7ff95542012-05-03 02:29:13 +0200935 if (unlikely(logbuf_cpu == this_cpu)) {
Ingo Molnar32a76002008-01-25 21:07:58 +0100936 /*
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 */
Peter Zijlstra94d24fc2011-06-07 11:17:30 +0200943 if (!oops_in_progress && !lockdep_recursing(current)) {
Tejun Heo3b8945e2008-05-12 21:21:04 +0200944 recursion_bug = 1;
Ingo Molnar32a76002008-01-25 21:07:58 +0100945 goto out_restore_irqs;
946 }
947 zap_locks();
948 }
949
Ingo Molnara0f1ccf2006-07-03 00:24:58 -0700950 lockdep_off();
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200951 raw_spin_lock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +0200952 logbuf_cpu = this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Tejun Heo3b8945e2008-05-12 21:21:04 +0200954 if (recursion_bug) {
Kay Sievers7ff95542012-05-03 02:29:13 +0200955 static const char recursion_msg[] =
956 "BUG: recent printk recursion!";
957
Tejun Heo3b8945e2008-05-12 21:21:04 +0200958 recursion_bug = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +0200959 printed_len += strlen(recursion_msg);
960 /* emit KERN_CRIT message */
961 log_store(0, 2, NULL, 0, recursion_msg, printed_len);
Linus Torvalds5fd29d62009-06-16 10:57:02 -0700962 }
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 /*
Kay Sievers7ff95542012-05-03 02:29:13 +0200965 * The printf needs to come first; we need the syslog
966 * prefix which might be passed-in as a parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 */
Kay Sievers7ff95542012-05-03 02:29:13 +0200968 textlen = vscnprintf(text, sizeof(textbuf), fmt, args);
Nick Andrewac60ad72008-05-12 21:21:04 +0200969
Kay Sievers7ff95542012-05-03 02:29:13 +0200970 /* mark and strip a trailing newline */
971 if (textlen && text[textlen-1] == '\n') {
972 textlen--;
973 newline = true;
974 }
Kay Sievers9d90c8d2011-03-13 03:19:51 +0100975
Kay Sievers7ff95542012-05-03 02:29:13 +0200976 /* 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;
990 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
Kay Sievers7ff95542012-05-03 02:29:13 +0200992 }
Nick Andrewac60ad72008-05-12 21:21:04 +0200993
Kay Sievers7ff95542012-05-03 02:29:13 +0200994 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 }
1000
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 }
1008
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 }
1016
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
1030
Linus Torvalds266c2e02008-03-24 19:25:08 -07001031 /*
Kay Sievers7ff95542012-05-03 02:29:13 +02001032 * 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.
Linus Torvalds266c2e02008-03-24 19:25:08 -07001035 *
Kay Sievers7ff95542012-05-03 02:29:13 +02001036 * The console_trylock_for_printk() function will release 'logbuf_lock'
1037 * regardless of whether it actually gets the console semaphore or not.
Linus Torvalds266c2e02008-03-24 19:25:08 -07001038 */
Torben Hohnac751ef2011-01-25 15:07:35 -08001039 if (console_trylock_for_printk(this_cpu))
1040 console_unlock();
Michael Ellerman76a8ad22006-06-25 05:47:40 -07001041
Linus Torvalds266c2e02008-03-24 19:25:08 -07001042 lockdep_on();
Ingo Molnar32a76002008-01-25 21:07:58 +01001043out_restore_irqs:
Peter Zijlstra1a9a8ae2011-06-07 11:17:30 +02001044 local_irq_restore(flags);
Michael Ellerman76a8ad22006-06-25 05:47:40 -07001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return printed_len;
1047}
Kay Sievers7ff95542012-05-03 02:29:13 +02001048EXPORT_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}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054EXPORT_SYMBOL(vprintk);
1055
Kay Sievers7ff95542012-05-03 02:29:13 +02001056asmlinkage 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);
Matt Mackalld59745c2005-05-01 08:59:02 -07001112#else
1113
Kay Sievers7ff95542012-05-03 02:29:13 +02001114static void call_console_drivers(int level, const char *text, size_t len)
Jesper Juhl40dc5652005-10-30 15:02:46 -08001115{
1116}
Matt Mackalld59745c2005-05-01 08:59:02 -07001117
1118#endif
1119
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001120static 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}
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001150/*
1151 * Set up a list of consoles. Called from init/main.c
1152 */
1153static int __init console_setup(char *str)
1154{
Yinghai Lueaa944a2007-07-15 23:37:27 -07001155 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001156 char *s, *options, *brl_options = NULL;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001157 int idx;
1158
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001159#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
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001174 /*
1175 * Decode str into name, index, options.
1176 */
1177 if (str[0] >= '0' && str[0] <= '9') {
Yinghai Lueaa944a2007-07-15 23:37:27 -07001178 strcpy(buf, "ttyS");
1179 strncpy(buf + 4, str, sizeof(buf) - 5);
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001180 } else {
Yinghai Lueaa944a2007-07-15 23:37:27 -07001181 strncpy(buf, str, sizeof(buf) - 1);
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001182 }
Yinghai Lueaa944a2007-07-15 23:37:27 -07001183 buf[sizeof(buf) - 1] = 0;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001184 if ((options = strchr(str, ',')) != NULL)
1185 *(options++) = 0;
1186#ifdef __sparc__
1187 if (!strcmp(str, "ttya"))
Yinghai Lueaa944a2007-07-15 23:37:27 -07001188 strcpy(buf, "ttyS0");
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001189 if (!strcmp(str, "ttyb"))
Yinghai Lueaa944a2007-07-15 23:37:27 -07001190 strcpy(buf, "ttyS1");
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001191#endif
Yinghai Lueaa944a2007-07-15 23:37:27 -07001192 for (s = buf; *s; s++)
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001193 if ((*s >= '0' && *s <= '9') || *s == ',')
1194 break;
1195 idx = simple_strtoul(s, NULL, 10);
1196 *s = 0;
1197
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001198 __add_preferred_console(buf, idx, options, brl_options);
Markus Armbruster9e124fe2008-05-26 23:31:07 +01001199 console_set_on_cmdline = 1;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001200 return 1;
1201}
1202__setup("console=", console_setup);
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204/**
Matt Mackall3c0547b2005-05-16 21:53:47 -07001205 * add_preferred_console - add a device to the list of preferred consoles.
Martin Waitzddad86c2005-11-13 16:08:14 -08001206 * @name: device name
1207 * @idx: device index
1208 * @options: options for this console
Matt Mackall3c0547b2005-05-16 21:53:47 -07001209 *
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 */
David S. Millerfb445ee2007-12-29 01:19:49 -08001217int add_preferred_console(char *name, int idx, char *options)
Matt Mackall3c0547b2005-05-16 21:53:47 -07001218{
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001219 return __add_preferred_console(name, idx, options, NULL);
Matt Mackall3c0547b2005-05-16 21:53:47 -07001220}
1221
Daniel Ritzb6b1d872007-08-03 16:07:43 +02001222int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
Yinghai Lu18a8bd92007-07-15 23:37:59 -07001223{
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];
Markus Armbrusterf7352952008-04-30 00:54:52 -07001231 strlcpy(c->name, name_new, sizeof(c->name));
Yinghai Lu18a8bd92007-07-15 23:37:59 -07001232 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
Rusty Russell2329abf2012-01-13 09:32:18 +10301241bool console_suspend_enabled = 1;
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001242EXPORT_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);
Yanmin Zhang134620f2011-10-31 17:11:27 -07001250module_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");
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001254
Matt Mackall3c0547b2005-05-16 21:53:47 -07001255/**
Linus Torvalds557240b2006-06-19 18:16:01 -07001256 * suspend_console - suspend the console subsystem
1257 *
1258 * This disables printk() while we go into suspend states
1259 */
1260void suspend_console(void)
1261{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001262 if (!console_suspend_enabled)
1263 return;
Pavel Machek0d630812008-07-23 21:28:32 -07001264 printk("Suspending console(s) (use no_console_suspend to debug)\n");
Torben Hohnac751ef2011-01-25 15:07:35 -08001265 console_lock();
Linus Torvalds557240b2006-06-19 18:16:01 -07001266 console_suspended = 1;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001267 up(&console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -07001268}
1269
1270void resume_console(void)
1271{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001272 if (!console_suspend_enabled)
1273 return;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001274 down(&console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -07001275 console_suspended = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08001276 console_unlock();
Linus Torvalds557240b2006-06-19 18:16:01 -07001277}
1278
1279/**
Kevin Cernekee034260d2010-06-03 22:11:25 -07001280 * 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:
Torben Hohnac751ef2011-01-25 15:07:35 -08001299 console_lock();
1300 console_unlock();
Kevin Cernekee034260d2010-06-03 22:11:25 -07001301 }
1302 return NOTIFY_OK;
1303}
1304
1305/**
Torben Hohnac751ef2011-01-25 15:07:35 -08001306 * console_lock - lock the console system for exclusive use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 *
Torben Hohnac751ef2011-01-25 15:07:35 -08001308 * Acquires a lock which guarantees that the caller has
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 * exclusive access to the console system and the console_drivers list.
1310 *
1311 * Can sleep, returns nothing.
1312 */
Torben Hohnac751ef2011-01-25 15:07:35 -08001313void console_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Eric Sesterhenn8abd8e22006-04-01 01:21:17 +02001315 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 down(&console_sem);
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001317 if (console_suspended)
1318 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 console_locked = 1;
1320 console_may_schedule = 1;
1321}
Torben Hohnac751ef2011-01-25 15:07:35 -08001322EXPORT_SYMBOL(console_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Torben Hohnac751ef2011-01-25 15:07:35 -08001324/**
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 if (down_trylock(&console_sem))
Torben Hohnac751ef2011-01-25 15:07:35 -08001335 return 0;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001336 if (console_suspended) {
1337 up(&console_sem);
Torben Hohnac751ef2011-01-25 15:07:35 -08001338 return 0;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 console_locked = 1;
1341 console_may_schedule = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08001342 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
Torben Hohnac751ef2011-01-25 15:07:35 -08001344EXPORT_SYMBOL(console_trylock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346int is_console_locked(void)
1347{
1348 return console_locked;
1349}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +01001351/*
Kay Sievers7ff95542012-05-03 02:29:13 +02001352 * Delayed printk version, for scheduler-internal messages:
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +01001353 */
1354#define PRINTK_BUF_SIZE 512
1355
1356#define PRINTK_PENDING_WAKEUP 0x01
1357#define PRINTK_PENDING_SCHED 0x02
1358
Peter Zijlstrab845b512008-08-08 21:47:09 +02001359static DEFINE_PER_CPU(int, printk_pending);
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +01001360static DEFINE_PER_CPU(char [PRINTK_BUF_SIZE], printk_sched_buf);
Peter Zijlstrab845b512008-08-08 21:47:09 +02001361
1362void printk_tick(void)
1363{
Eric Dumazet40dc11f2010-11-26 17:22:16 +01001364 if (__this_cpu_read(printk_pending)) {
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +01001365 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);
Peter Zijlstrab845b512008-08-08 21:47:09 +02001372 }
1373}
1374
1375int printk_needs_cpu(int cpu)
1376{
Eric Dumazet40dc11f2010-11-26 17:22:16 +01001377 if (cpu_is_offline(cpu))
Heiko Carstens61ab25442010-11-26 13:00:59 +01001378 printk_tick();
Eric Dumazet40dc11f2010-11-26 17:22:16 +01001379 return __this_cpu_read(printk_pending);
Peter Zijlstrab845b512008-08-08 21:47:09 +02001380}
1381
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08001382void wake_up_klogd(void)
1383{
Peter Zijlstrab845b512008-08-08 21:47:09 +02001384 if (waitqueue_active(&log_wait))
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +01001385 this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP);
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08001386}
1387
Kay Sievers7ff95542012-05-03 02:29:13 +02001388/* the next printk record to write to the console */
1389static u64 console_seq;
1390static u32 console_idx;
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392/**
Torben Hohnac751ef2011-01-25 15:07:35 -08001393 * console_unlock - unlock the console system
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 *
Torben Hohnac751ef2011-01-25 15:07:35 -08001395 * Releases the console_lock which the caller holds on the console system
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 * and the console driver list.
1397 *
Torben Hohnac751ef2011-01-25 15:07:35 -08001398 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 *
Kay Sievers7ff95542012-05-03 02:29:13 +02001402 * If there is output waiting, we wake it /dev/kmsg and syslog() users.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 *
Torben Hohnac751ef2011-01-25 15:07:35 -08001404 * console_unlock(); may be called from any context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 */
Torben Hohnac751ef2011-01-25 15:07:35 -08001406void console_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
Kay Sievers7ff95542012-05-03 02:29:13 +02001408 static u64 seen_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 unsigned long flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02001410 bool wake_klogd = false;
1411 bool retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Linus Torvalds557240b2006-06-19 18:16:01 -07001413 if (console_suspended) {
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001414 up(&console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -07001415 return;
1416 }
Antonino A. Daplas78944e52006-08-05 12:14:16 -07001417
1418 console_may_schedule = 0;
1419
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02001420again:
Kay Sievers7ff95542012-05-03 02:29:13 +02001421 for (;;) {
1422 struct log *msg;
1423 static char text[LOG_LINE_MAX];
1424 size_t len;
1425 int level;
1426
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001427 raw_spin_lock_irqsave(&logbuf_lock, flags);
Kay Sievers7ff95542012-05-03 02:29:13 +02001428 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++;
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001452 raw_spin_unlock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02001453
Steven Rostedt81d68a92008-05-12 21:20:42 +02001454 stop_critical_timings(); /* don't trace print latency */
Kay Sievers7ff95542012-05-03 02:29:13 +02001455 call_console_drivers(level, text, len);
Steven Rostedt81d68a92008-05-12 21:20:42 +02001456 start_critical_timings();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 local_irq_restore(flags);
1458 }
1459 console_locked = 0;
Feng Tangfe3d8ad2011-03-22 16:34:21 -07001460
1461 /* Release the exclusive_console once it is used */
1462 if (unlikely(exclusive_console))
1463 exclusive_console = NULL;
1464
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001465 raw_spin_unlock(&logbuf_lock);
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02001466
Peter Zijlstra0b5e1c52011-06-07 11:15:33 +02001467 up(&console_sem);
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02001468
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 */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001475 raw_spin_lock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02001476 retry = console_seq != log_next_seq;
Peter Zijlstra09dc3cf2011-12-08 14:34:13 -08001477 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
1478
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02001479 if (retry && console_trylock())
1480 goto again;
1481
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08001482 if (wake_klogd)
1483 wake_up_klogd();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
Torben Hohnac751ef2011-01-25 15:07:35 -08001485EXPORT_SYMBOL(console_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Martin Waitzddad86c2005-11-13 16:08:14 -08001487/**
1488 * console_conditional_schedule - yield the CPU if required
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 *
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 *
Torben Hohnac751ef2011-01-25 15:07:35 -08001494 * Must be called within console_lock();.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 */
1496void __sched console_conditional_schedule(void)
1497{
1498 if (console_may_schedule)
1499 cond_resched();
1500}
1501EXPORT_SYMBOL(console_conditional_schedule);
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503void 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
Torben Hohnac751ef2011-01-25 15:07:35 -08001515 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
1517 console_locked = 1;
1518 console_may_schedule = 0;
Robin Getz4d091612009-07-01 21:08:37 -04001519 for_each_console(c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if ((c->flags & CON_ENABLED) && c->unblank)
1521 c->unblank();
Torben Hohnac751ef2011-01-25 15:07:35 -08001522 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
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
Torben Hohnac751ef2011-01-25 15:07:35 -08001533 console_lock();
Robin Getz4d091612009-07-01 21:08:37 -04001534 for_each_console(c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (!c->device)
1536 continue;
1537 driver = c->device(c, index);
1538 if (driver)
1539 break;
1540 }
Torben Hohnac751ef2011-01-25 15:07:35 -08001541 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 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{
Torben Hohnac751ef2011-01-25 15:07:35 -08001552 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 console->flags &= ~CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08001554 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
1556EXPORT_SYMBOL(console_stop);
1557
1558void console_start(struct console *console)
1559{
Torben Hohnac751ef2011-01-25 15:07:35 -08001560 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 console->flags |= CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08001562 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564EXPORT_SYMBOL(console_start);
1565
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07001566static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578/*
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.
Robin Getz4d091612009-07-01 21:08:37 -04001583 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 */
Robin Getz4d091612009-07-01 21:08:37 -04001597void register_console(struct console *newcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
Jesper Juhl40dc5652005-10-30 15:02:46 -08001599 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 unsigned long flags;
Robin Getz4d091612009-07-01 21:08:37 -04001601 struct console *bcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Robin Getz4d091612009-07-01 21:08:37 -04001603 /*
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 }
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001616 }
1617
Robin Getz4d091612009-07-01 21:08:37 -04001618 if (console_drivers && console_drivers->flags & CON_BOOT)
1619 bcon = console_drivers;
1620
1621 if (preferred_console < 0 || bcon || !console_drivers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 preferred_console = selected_console;
1623
Robin Getz4d091612009-07-01 21:08:37 -04001624 if (newcon->early_setup)
1625 newcon->early_setup();
Yinghai Lu18a8bd92007-07-15 23:37:59 -07001626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 /*
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) {
Robin Getz4d091612009-07-01 21:08:37 -04001633 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;
Jan Kiszkacd3a1b82008-05-12 21:21:04 +02001640 preferred_console = 0;
1641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
1643 }
1644
1645 /*
1646 * See if this console matches one we selected on
1647 * the command line.
1648 */
Jesper Juhl40dc5652005-10-30 15:02:46 -08001649 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
1650 i++) {
Robin Getz4d091612009-07-01 21:08:37 -04001651 if (strcmp(console_cmdline[i].name, newcon->name) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 continue;
Robin Getz4d091612009-07-01 21:08:37 -04001653 if (newcon->index >= 0 &&
1654 newcon->index != console_cmdline[i].index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 continue;
Robin Getz4d091612009-07-01 21:08:37 -04001656 if (newcon->index < 0)
1657 newcon->index = console_cmdline[i].index;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001658#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1659 if (console_cmdline[i].brl_options) {
Robin Getz4d091612009-07-01 21:08:37 -04001660 newcon->flags |= CON_BRL;
1661 braille_register_console(newcon,
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001662 console_cmdline[i].index,
1663 console_cmdline[i].options,
1664 console_cmdline[i].brl_options);
1665 return;
1666 }
1667#endif
Robin Getz4d091612009-07-01 21:08:37 -04001668 if (newcon->setup &&
1669 newcon->setup(newcon, console_cmdline[i].options) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 break;
Robin Getz4d091612009-07-01 21:08:37 -04001671 newcon->flags |= CON_ENABLED;
1672 newcon->index = console_cmdline[i].index;
Greg Edwardsab4af032005-06-23 00:09:05 -07001673 if (i == selected_console) {
Robin Getz4d091612009-07-01 21:08:37 -04001674 newcon->flags |= CON_CONSDEV;
Greg Edwardsab4af032005-06-23 00:09:05 -07001675 preferred_console = selected_console;
1676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 break;
1678 }
1679
Robin Getz4d091612009-07-01 21:08:37 -04001680 if (!(newcon->flags & CON_ENABLED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 return;
1682
Robin Getz8259cf42009-07-09 13:08:37 -04001683 /*
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))
Robin Getz4d091612009-07-01 21:08:37 -04001690 newcon->flags &= ~CON_PRINTBUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 /*
1693 * Put this console in the list - keep the
1694 * preferred driver at the head of the list.
1695 */
Torben Hohnac751ef2011-01-25 15:07:35 -08001696 console_lock();
Robin Getz4d091612009-07-01 21:08:37 -04001697 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 } else {
Robin Getz4d091612009-07-01 21:08:37 -04001703 newcon->next = console_drivers->next;
1704 console_drivers->next = newcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
Robin Getz4d091612009-07-01 21:08:37 -04001706 if (newcon->flags & CON_PRINTBUFFER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 /*
Torben Hohnac751ef2011-01-25 15:07:35 -08001708 * console_unlock(); will print out the buffered messages
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 * for us.
1710 */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001711 raw_spin_lock_irqsave(&logbuf_lock, flags);
Kay Sievers7ff95542012-05-03 02:29:13 +02001712 console_seq = syslog_seq;
1713 console_idx = syslog_idx;
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001714 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
Feng Tangfe3d8ad2011-03-22 16:34:21 -07001715 /*
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
Torben Hohnac751ef2011-01-25 15:07:35 -08001722 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01001723 console_sysfs_notify();
Robin Getz8259cf42009-07-09 13:08:37 -04001724
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 */
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07001732 if (bcon &&
1733 ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
1734 !keep_bootcon) {
Robin Getz8259cf42009-07-09 13:08:37 -04001735 /* 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748}
1749EXPORT_SYMBOL(register_console);
1750
Jesper Juhl40dc5652005-10-30 15:02:46 -08001751int unregister_console(struct console *console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
Jesper Juhl40dc5652005-10-30 15:02:46 -08001753 struct console *a, *b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 int res = 1;
1755
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001756#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1757 if (console->flags & CON_BRL)
1758 return braille_unregister_console(console);
1759#endif
1760
Torben Hohnac751ef2011-01-25 15:07:35 -08001761 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 if (console_drivers == console) {
1763 console_drivers=console->next;
1764 res = 0;
Benjamin Herrenschmidte9b15b52005-11-23 13:37:44 -08001765 } else if (console_drivers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 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;
Jesper Juhl40dc5652005-10-30 15:02:46 -08001772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774 }
Jesper Juhl40dc5652005-10-30 15:02:46 -08001775
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001776 /*
Greg Edwardsab4af032005-06-23 00:09:05 -07001777 * If this isn't the last console and it has CON_CONSDEV set, we
1778 * need to set it on the next preferred console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 */
Gerd Hoffmann69331af2007-05-08 00:26:49 -07001780 if (console_drivers != NULL && console->flags & CON_CONSDEV)
Greg Edwardsab4af032005-06-23 00:09:05 -07001781 console_drivers->flags |= CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Torben Hohnac751ef2011-01-25 15:07:35 -08001783 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01001784 console_sysfs_notify();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 return res;
1786}
1787EXPORT_SYMBOL(unregister_console);
Matt Mackalld59745c2005-05-01 08:59:02 -07001788
Kevin Cernekee034260d2010-06-03 22:11:25 -07001789static int __init printk_late_init(void)
Robin Getz0c5564b2007-08-20 15:22:47 -04001790{
Robin Getz4d091612009-07-01 21:08:37 -04001791 struct console *con;
1792
1793 for_each_console(con) {
Nishanth Aravamudan4c30c6f2011-08-25 15:59:11 -07001794 if (!keep_bootcon && con->flags & CON_BOOT) {
Robin Getzcb00e992007-08-21 23:14:58 -04001795 printk(KERN_INFO "turn off boot console %s%d\n",
Robin Getz4d091612009-07-01 21:08:37 -04001796 con->name, con->index);
Sonic Zhang42c2c8c2009-08-06 15:58:11 -07001797 unregister_console(con);
Robin Getzcb00e992007-08-21 23:14:58 -04001798 }
Robin Getz0c5564b2007-08-20 15:22:47 -04001799 }
Kevin Cernekee034260d2010-06-03 22:11:25 -07001800 hotcpu_notifier(console_cpu_notify, 0);
Robin Getz0c5564b2007-08-20 15:22:47 -04001801 return 0;
1802}
Kevin Cernekee034260d2010-06-03 22:11:25 -07001803late_initcall(printk_late_init);
Robin Getz0c5564b2007-08-20 15:22:47 -04001804
Joe Perches7ef3d2f2008-02-08 04:21:25 -08001805#if defined CONFIG_PRINTK
Dave Young717115e2008-07-25 01:45:58 -07001806
Peter Zijlstra600e1452012-03-15 12:35:37 +01001807int 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827/*
1828 * printk rate limiting, lifted from the networking subsystem.
1829 *
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07001830 * This enforces a rate limit: not more than 10 kernel messages
1831 * every 5s to make a denial-of-service attack impossible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 */
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07001833DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
1834
Christian Borntraeger5c828712009-10-23 14:58:11 +02001835int __printk_ratelimit(const char *func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Christian Borntraeger5c828712009-10-23 14:58:11 +02001837 return ___ratelimit(&printk_ratelimit_state, func);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
Christian Borntraeger5c828712009-10-23 14:58:11 +02001839EXPORT_SYMBOL(__printk_ratelimit);
Andrew Mortonf46c4832006-11-02 22:07:16 -08001840
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{
Guillaume Knispelf2d28a22009-03-17 16:18:42 +01001853 if (*caller_jiffies == 0
1854 || !time_in_range(jiffies, *caller_jiffies,
1855 *caller_jiffies
1856 + msecs_to_jiffies(interval_msecs))) {
1857 *caller_jiffies = jiffies;
Andrew Mortonf46c4832006-11-02 22:07:16 -08001858 return true;
1859 }
1860 return false;
1861}
1862EXPORT_SYMBOL(printk_timed_ratelimit);
Simon Kagstrom456b5652009-10-16 14:09:18 +02001863
1864static DEFINE_SPINLOCK(dump_list_lock);
1865static LIST_HEAD(dump_list);
1866
1867/**
1868 * kmsg_dump_register - register a kernel log dumper.
Randy Dunlap64855362009-12-17 15:27:27 -08001869 * @dumper: pointer to the kmsg_dumper structure
Simon Kagstrom456b5652009-10-16 14:09:18 +02001870 *
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;
Huang Yingfb842b02011-01-12 16:59:43 -08001888 list_add_tail_rcu(&dumper->list, &dump_list);
Simon Kagstrom456b5652009-10-16 14:09:18 +02001889 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.
Randy Dunlap64855362009-12-17 15:27:27 -08001899 * @dumper: pointer to the kmsg_dumper structure
Simon Kagstrom456b5652009-10-16 14:09:18 +02001900 *
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;
Huang Yingfb842b02011-01-12 16:59:43 -08001912 list_del_rcu(&dumper->list);
Simon Kagstrom456b5652009-10-16 14:09:18 +02001913 err = 0;
1914 }
1915 spin_unlock_irqrestore(&dump_list_lock, flags);
Huang Yingfb842b02011-01-12 16:59:43 -08001916 synchronize_rcu();
Simon Kagstrom456b5652009-10-16 14:09:18 +02001917
1918 return err;
1919}
1920EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
1921
Kay Sievers7ff95542012-05-03 02:29:13 +02001922static bool always_kmsg_dump;
1923module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR);
1924
Simon Kagstrom456b5652009-10-16 14:09:18 +02001925/**
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{
Kay Sievers7ff95542012-05-03 02:29:13 +02001934 u64 idx;
Simon Kagstrom456b5652009-10-16 14:09:18 +02001935 struct kmsg_dumper *dumper;
1936 const char *s1, *s2;
1937 unsigned long l1, l2;
1938 unsigned long flags;
1939
Matthew Garrettc22ab332012-03-05 14:59:10 -08001940 if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
1941 return;
1942
Simon Kagstrom456b5652009-10-16 14:09:18 +02001943 /* 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. */
Kay Sievers7ff95542012-05-03 02:29:13 +02001946
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001947 raw_spin_lock_irqsave(&logbuf_lock, flags);
Kay Sievers7ff95542012-05-03 02:29:13 +02001948 if (syslog_seq < log_first_seq)
1949 idx = syslog_idx;
1950 else
1951 idx = log_first_idx;
Simon Kagstrom456b5652009-10-16 14:09:18 +02001952
Kay Sievers7ff95542012-05-03 02:29:13 +02001953 if (idx > log_next_idx) {
1954 s1 = log_buf;
1955 l1 = log_next_idx;
Simon Kagstrom456b5652009-10-16 14:09:18 +02001956
Kay Sievers7ff95542012-05-03 02:29:13 +02001957 s2 = log_buf + idx;
1958 l2 = log_buf_len - idx;
Simon Kagstrom456b5652009-10-16 14:09:18 +02001959 } else {
1960 s1 = "";
1961 l1 = 0;
1962
Kay Sievers7ff95542012-05-03 02:29:13 +02001963 s2 = log_buf + idx;
1964 l2 = log_next_idx - idx;
Simon Kagstrom456b5652009-10-16 14:09:18 +02001965 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001966 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
Simon Kagstrom456b5652009-10-16 14:09:18 +02001967
Huang Yingfb842b02011-01-12 16:59:43 -08001968 rcu_read_lock();
1969 list_for_each_entry_rcu(dumper, &dump_list, list)
Simon Kagstrom456b5652009-10-16 14:09:18 +02001970 dumper->dump(dumper, reason, s1, l1, s2, l2);
Huang Yingfb842b02011-01-12 16:59:43 -08001971 rcu_read_unlock();
Simon Kagstrom456b5652009-10-16 14:09:18 +02001972}
Joe Perches7ef3d2f2008-02-08 04:21:25 -08001973#endif