blob: 0b31715f335a7a8ba1a846fe3b93fe71d97cd8d7 [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>
Kay Sieverse11fea92012-05-03 02:29:41 +020044#include <linux/poll.h>
Frederic Weisbecker74876a92012-10-12 18:00:23 +020045#include <linux/irq_work.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <asm/uaccess.h>
48
Johannes Berg95100352011-11-24 20:03:08 +010049#define CREATE_TRACE_POINTS
50#include <trace/events/printk.h>
51
Ingo Molnar076f9772008-01-30 13:33:06 +010052/*
53 * Architectures can override it:
54 */
Jiri Slabye17ba732008-05-12 15:44:40 +020055void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
Ingo Molnar076f9772008-01-30 13:33:06 +010056{
57}
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/* printk's without a loglevel use this.. */
Mandeep Singh Baines5af5bcb2011-03-22 16:34:23 -070060#define DEFAULT_MESSAGE_LOGLEVEL CONFIG_DEFAULT_MESSAGE_LOGLEVEL
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62/* We show everything that is MORE important than this.. */
63#define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
64#define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
65
66DECLARE_WAIT_QUEUE_HEAD(log_wait);
67
68int console_printk[4] = {
69 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
70 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
71 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
72 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
73};
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/*
Patrick Pletscher0bbfb7c2007-02-17 20:10:16 +010076 * Low level drivers may need that to know if they can schedule in
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * their unblank() callback or not. So let's export it.
78 */
79int oops_in_progress;
80EXPORT_SYMBOL(oops_in_progress);
81
82/*
83 * console_sem protects the console_drivers list, and also
84 * provides serialisation for access to the entire console
85 * driver system.
86 */
Thomas Gleixner5b8c4f22010-09-07 14:33:43 +000087static DEFINE_SEMAPHORE(console_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088struct console *console_drivers;
Ingo Molnara29d1cf2008-06-02 13:19:08 +020089EXPORT_SYMBOL_GPL(console_drivers);
90
Daniel Vetterdaee7792012-09-22 19:52:11 +020091#ifdef CONFIG_LOCKDEP
92static struct lockdep_map console_lock_dep_map = {
93 .name = "console_lock"
94};
95#endif
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/*
98 * This is used for debugging the mess that is the VT code by
99 * keeping track if we have the console semaphore held. It's
100 * definitely not the perfect debug tool (we don't know if _WE_
101 * hold it are racing, but it helps tracking those weird code
102 * path in the console code where we end up in places I want
103 * locked without the console sempahore held
104 */
Linus Torvalds557240b2006-06-19 18:16:01 -0700105static int console_locked, console_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/*
Feng Tangfe3d8ad2011-03-22 16:34:21 -0700108 * If exclusive_console is non-NULL then only this console is to be printed to.
109 */
110static struct console *exclusive_console;
111
112/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * Array of consoles built from command line options (console=)
114 */
115struct console_cmdline
116{
117 char name[8]; /* Name of the driver */
118 int index; /* Minor dev. to use */
119 char *options; /* Options for the driver */
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700120#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
121 char *brl_options; /* Options for braille driver */
122#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123};
124
125#define MAX_CMDLINECONSOLES 8
126
127static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
128static int selected_console = -1;
129static int preferred_console = -1;
Markus Armbruster9e124fe2008-05-26 23:31:07 +0100130int console_set_on_cmdline;
131EXPORT_SYMBOL(console_set_on_cmdline);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133/* Flag: console code may call schedule() */
134static int console_may_schedule;
135
Kay Sievers7ff95542012-05-03 02:29:13 +0200136/*
137 * The printk log buffer consists of a chain of concatenated variable
138 * length records. Every record starts with a record header, containing
139 * the overall length of the record.
140 *
141 * The heads to the first and last entry in the buffer, as well as the
142 * sequence numbers of these both entries are maintained when messages
143 * are stored..
144 *
145 * If the heads indicate available messages, the length in the header
146 * tells the start next message. A length == 0 for the next message
147 * indicates a wrap-around to the beginning of the buffer.
148 *
149 * Every record carries the monotonic timestamp in microseconds, as well as
150 * the standard userspace syslog level and syslog facility. The usual
151 * kernel messages use LOG_KERN; userspace-injected messages always carry
152 * a matching syslog facility, by default LOG_USER. The origin of every
153 * message can be reliably determined that way.
154 *
155 * The human readable log message directly follows the message header. The
156 * length of the message text is stored in the header, the stored message
157 * is not terminated.
158 *
Kay Sieverse11fea92012-05-03 02:29:41 +0200159 * Optionally, a message can carry a dictionary of properties (key/value pairs),
160 * to provide userspace with a machine-readable message context.
161 *
162 * Examples for well-defined, commonly used property names are:
163 * DEVICE=b12:8 device identifier
164 * b12:8 block dev_t
165 * c127:3 char dev_t
166 * n8 netdev ifindex
167 * +sound:card0 subsystem:devname
168 * SUBSYSTEM=pci driver-core subsystem name
169 *
170 * Valid characters in property names are [a-zA-Z0-9.-_]. The plain text value
171 * follows directly after a '=' character. Every property is terminated by
172 * a '\0' character. The last property is not terminated.
173 *
174 * Example of a message structure:
175 * 0000 ff 8f 00 00 00 00 00 00 monotonic time in nsec
176 * 0008 34 00 record is 52 bytes long
177 * 000a 0b 00 text is 11 bytes long
178 * 000c 1f 00 dictionary is 23 bytes long
179 * 000e 03 00 LOG_KERN (facility) LOG_ERR (level)
180 * 0010 69 74 27 73 20 61 20 6c "it's a l"
181 * 69 6e 65 "ine"
182 * 001b 44 45 56 49 43 "DEVIC"
183 * 45 3d 62 38 3a 32 00 44 "E=b8:2\0D"
184 * 52 49 56 45 52 3d 62 75 "RIVER=bu"
185 * 67 "g"
186 * 0032 00 00 00 padding to next message header
187 *
188 * The 'struct log' buffer header must never be directly exported to
189 * userspace, it is a kernel-private implementation detail that might
190 * need to be changed in the future, when the requirements change.
191 *
192 * /dev/kmsg exports the structured data in the following line format:
193 * "level,sequnum,timestamp;<message text>\n"
194 *
195 * The optional key/value pairs are attached as continuation lines starting
196 * with a space character and terminated by a newline. All possible
197 * non-prinatable characters are escaped in the "\xff" notation.
198 *
199 * Users of the export format should ignore possible additional values
200 * separated by ',', and find the message after the ';' character.
Kay Sievers7ff95542012-05-03 02:29:13 +0200201 */
Matt Mackalld59745c2005-05-01 08:59:02 -0700202
Kay Sievers084681d2012-06-28 09:38:53 +0200203enum log_flags {
Kay Sievers5becfb12012-07-09 12:15:42 -0700204 LOG_NOCONS = 1, /* already flushed, do not print to console */
205 LOG_NEWLINE = 2, /* text ended with a newline */
206 LOG_PREFIX = 4, /* text started with a prefix */
207 LOG_CONT = 8, /* text is a fragment of a continuation line */
Kay Sievers084681d2012-06-28 09:38:53 +0200208};
209
Kay Sievers7ff95542012-05-03 02:29:13 +0200210struct log {
211 u64 ts_nsec; /* timestamp in nanoseconds */
212 u16 len; /* length of entire record */
213 u16 text_len; /* length of text buffer */
214 u16 dict_len; /* length of dictionary buffer */
Kay Sievers084681d2012-06-28 09:38:53 +0200215 u8 facility; /* syslog facility */
216 u8 flags:5; /* internal record flags */
217 u8 level:3; /* syslog level */
Kay Sievers7ff95542012-05-03 02:29:13 +0200218};
219
220/*
221 * The logbuf_lock protects kmsg buffer, indices, counters. It is also
222 * used in interesting ways to provide interlocking in console_unlock();
223 */
224static DEFINE_RAW_SPINLOCK(logbuf_lock);
225
Kay Sievers96efedf2012-07-16 18:35:29 -0700226#ifdef CONFIG_PRINTK
Kay Sievers7f3a7812012-05-09 01:37:51 +0200227/* the next printk record to read by syslog(READ) or /proc/kmsg */
228static u64 syslog_seq;
229static u32 syslog_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -0700230static enum log_flags syslog_prev;
Kay Sieverseb02dac2012-07-09 10:05:10 -0700231static size_t syslog_partial;
Kay Sievers7f3a7812012-05-09 01:37:51 +0200232
233/* index and sequence number of the first record stored in the buffer */
234static u64 log_first_seq;
235static u32 log_first_idx;
236
237/* index and sequence number of the next record to store in the buffer */
238static u64 log_next_seq;
Kay Sievers7f3a7812012-05-09 01:37:51 +0200239static u32 log_next_idx;
240
Kay Sieverseab07262012-07-16 18:35:30 -0700241/* the next printk record to write to the console */
242static u64 console_seq;
243static u32 console_idx;
244static enum log_flags console_prev;
245
Kay Sievers7f3a7812012-05-09 01:37:51 +0200246/* the next printk record to read after the last 'clear' command */
247static u64 clear_seq;
248static u32 clear_idx;
Kay Sievers7ff95542012-05-03 02:29:13 +0200249
Kay Sievers70498252012-07-16 18:35:29 -0700250#define PREFIX_MAX 32
251#define LOG_LINE_MAX 1024 - PREFIX_MAX
Kay Sievers7ff95542012-05-03 02:29:13 +0200252
253/* record buffer */
Andrew Lunn6ebb0172012-06-05 08:52:34 +0200254#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
Stephen Warrenf8450fc2012-05-10 16:14:33 -0600255#define LOG_ALIGN 4
256#else
Andrew Lunn6ebb0172012-06-05 08:52:34 +0200257#define LOG_ALIGN __alignof__(struct log)
Stephen Warrenf8450fc2012-05-10 16:14:33 -0600258#endif
Kay Sievers7ff95542012-05-03 02:29:13 +0200259#define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
Stephen Warrenf8450fc2012-05-10 16:14:33 -0600260static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN);
Matt Mackalld59745c2005-05-01 08:59:02 -0700261static char *log_buf = __log_buf;
Kay Sievers7ff95542012-05-03 02:29:13 +0200262static u32 log_buf_len = __LOG_BUF_LEN;
263
Kay Sievers7f3a7812012-05-09 01:37:51 +0200264/* cpu currently holding logbuf_lock */
265static volatile unsigned int logbuf_cpu = UINT_MAX;
Kay Sievers7ff95542012-05-03 02:29:13 +0200266
267/* human readable text of the record */
268static char *log_text(const struct log *msg)
269{
270 return (char *)msg + sizeof(struct log);
271}
272
273/* optional key/value pair dictionary attached to the record */
274static char *log_dict(const struct log *msg)
275{
276 return (char *)msg + sizeof(struct log) + msg->text_len;
277}
278
279/* get record by index; idx must point to valid msg */
280static struct log *log_from_idx(u32 idx)
281{
282 struct log *msg = (struct log *)(log_buf + idx);
283
284 /*
285 * A length == 0 record is the end of buffer marker. Wrap around and
286 * read the message at the start of the buffer.
287 */
288 if (!msg->len)
289 return (struct log *)log_buf;
290 return msg;
291}
292
293/* get next record; idx must point to valid msg */
294static u32 log_next(u32 idx)
295{
296 struct log *msg = (struct log *)(log_buf + idx);
297
298 /* length == 0 indicates the end of the buffer; wrap */
299 /*
300 * A length == 0 record is the end of buffer marker. Wrap around and
301 * read the message at the start of the buffer as *this* one, and
302 * return the one after that.
303 */
304 if (!msg->len) {
305 msg = (struct log *)log_buf;
306 return msg->len;
307 }
308 return idx + msg->len;
309}
310
Kay Sievers7ff95542012-05-03 02:29:13 +0200311/* insert record into the buffer, discard old ones, update heads */
312static void log_store(int facility, int level,
Kay Sievers084681d2012-06-28 09:38:53 +0200313 enum log_flags flags, u64 ts_nsec,
Kay Sievers7ff95542012-05-03 02:29:13 +0200314 const char *dict, u16 dict_len,
315 const char *text, u16 text_len)
316{
317 struct log *msg;
318 u32 size, pad_len;
319
320 /* number of '\0' padding bytes to next message */
321 size = sizeof(struct log) + text_len + dict_len;
322 pad_len = (-size) & (LOG_ALIGN - 1);
323 size += pad_len;
324
325 while (log_first_seq < log_next_seq) {
326 u32 free;
327
328 if (log_next_idx > log_first_idx)
329 free = max(log_buf_len - log_next_idx, log_first_idx);
330 else
331 free = log_first_idx - log_next_idx;
332
333 if (free > size + sizeof(struct log))
334 break;
335
336 /* drop old messages until we have enough contiuous space */
337 log_first_idx = log_next(log_first_idx);
338 log_first_seq++;
339 }
340
341 if (log_next_idx + size + sizeof(struct log) >= log_buf_len) {
342 /*
343 * This message + an additional empty header does not fit
344 * at the end of the buffer. Add an empty header with len == 0
345 * to signify a wrap around.
346 */
347 memset(log_buf + log_next_idx, 0, sizeof(struct log));
348 log_next_idx = 0;
349 }
350
351 /* fill message */
352 msg = (struct log *)(log_buf + log_next_idx);
353 memcpy(log_text(msg), text, text_len);
354 msg->text_len = text_len;
355 memcpy(log_dict(msg), dict, dict_len);
356 msg->dict_len = dict_len;
Kay Sievers084681d2012-06-28 09:38:53 +0200357 msg->facility = facility;
358 msg->level = level & 7;
359 msg->flags = flags & 0x1f;
360 if (ts_nsec > 0)
361 msg->ts_nsec = ts_nsec;
362 else
363 msg->ts_nsec = local_clock();
Kay Sievers7ff95542012-05-03 02:29:13 +0200364 memset(log_dict(msg) + dict_len, 0, pad_len);
365 msg->len = sizeof(struct log) + text_len + dict_len + pad_len;
366
367 /* insert message */
368 log_next_idx += msg->len;
369 log_next_seq++;
370}
Matt Mackalld59745c2005-05-01 08:59:02 -0700371
Kay Sieverse11fea92012-05-03 02:29:41 +0200372/* /dev/kmsg - userspace message inject/listen interface */
373struct devkmsg_user {
374 u64 seq;
375 u32 idx;
Kay Sieversd39f3d72012-07-16 18:35:30 -0700376 enum log_flags prev;
Kay Sieverse11fea92012-05-03 02:29:41 +0200377 struct mutex lock;
378 char buf[8192];
379};
380
381static ssize_t devkmsg_writev(struct kiocb *iocb, const struct iovec *iv,
382 unsigned long count, loff_t pos)
383{
384 char *buf, *line;
385 int i;
386 int level = default_message_loglevel;
387 int facility = 1; /* LOG_USER */
388 size_t len = iov_length(iv, count);
389 ssize_t ret = len;
390
391 if (len > LOG_LINE_MAX)
392 return -EINVAL;
393 buf = kmalloc(len+1, GFP_KERNEL);
394 if (buf == NULL)
395 return -ENOMEM;
396
397 line = buf;
398 for (i = 0; i < count; i++) {
Kay Sieverscdf53442012-07-30 14:40:08 -0700399 if (copy_from_user(line, iv[i].iov_base, iv[i].iov_len)) {
400 ret = -EFAULT;
Kay Sieverse11fea92012-05-03 02:29:41 +0200401 goto out;
Kay Sieverscdf53442012-07-30 14:40:08 -0700402 }
Kay Sieverse11fea92012-05-03 02:29:41 +0200403 line += iv[i].iov_len;
404 }
405
406 /*
407 * Extract and skip the syslog prefix <[0-9]*>. Coming from userspace
408 * the decimal value represents 32bit, the lower 3 bit are the log
409 * level, the rest are the log facility.
410 *
411 * If no prefix or no userspace facility is specified, we
412 * enforce LOG_USER, to be able to reliably distinguish
413 * kernel-generated messages from userspace-injected ones.
414 */
415 line = buf;
416 if (line[0] == '<') {
417 char *endp = NULL;
418
419 i = simple_strtoul(line+1, &endp, 10);
420 if (endp && endp[0] == '>') {
421 level = i & 7;
422 if (i >> 3)
423 facility = i >> 3;
424 endp++;
425 len -= endp - line;
426 line = endp;
427 }
428 }
429 line[len] = '\0';
430
431 printk_emit(facility, level, NULL, 0, "%s", line);
432out:
433 kfree(buf);
434 return ret;
435}
436
437static ssize_t devkmsg_read(struct file *file, char __user *buf,
438 size_t count, loff_t *ppos)
439{
440 struct devkmsg_user *user = file->private_data;
441 struct log *msg;
Kay Sievers5fc324902012-05-08 13:04:17 +0200442 u64 ts_usec;
Kay Sieverse11fea92012-05-03 02:29:41 +0200443 size_t i;
Kay Sieversd39f3d72012-07-16 18:35:30 -0700444 char cont = '-';
Kay Sieverse11fea92012-05-03 02:29:41 +0200445 size_t len;
446 ssize_t ret;
447
448 if (!user)
449 return -EBADF;
450
Yuanhan Liu4a77a5a2012-06-16 21:21:51 +0800451 ret = mutex_lock_interruptible(&user->lock);
452 if (ret)
453 return ret;
liu chuansheng5c53d812012-07-06 09:50:08 -0700454 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200455 while (user->seq == log_next_seq) {
456 if (file->f_flags & O_NONBLOCK) {
457 ret = -EAGAIN;
liu chuansheng5c53d812012-07-06 09:50:08 -0700458 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200459 goto out;
460 }
461
liu chuansheng5c53d812012-07-06 09:50:08 -0700462 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200463 ret = wait_event_interruptible(log_wait,
464 user->seq != log_next_seq);
465 if (ret)
466 goto out;
liu chuansheng5c53d812012-07-06 09:50:08 -0700467 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200468 }
469
470 if (user->seq < log_first_seq) {
471 /* our last seen message is gone, return error and reset */
472 user->idx = log_first_idx;
473 user->seq = log_first_seq;
474 ret = -EPIPE;
liu chuansheng5c53d812012-07-06 09:50:08 -0700475 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200476 goto out;
477 }
478
479 msg = log_from_idx(user->idx);
Kay Sievers5fc324902012-05-08 13:04:17 +0200480 ts_usec = msg->ts_nsec;
481 do_div(ts_usec, 1000);
Kay Sieversd39f3d72012-07-16 18:35:30 -0700482
483 /*
484 * If we couldn't merge continuation line fragments during the print,
485 * export the stored flags to allow an optional external merge of the
486 * records. Merging the records isn't always neccessarily correct, like
487 * when we hit a race during printing. In most cases though, it produces
488 * better readable output. 'c' in the record flags mark the first
489 * fragment of a line, '+' the following.
490 */
491 if (msg->flags & LOG_CONT && !(user->prev & LOG_CONT))
492 cont = 'c';
493 else if ((msg->flags & LOG_CONT) ||
494 ((user->prev & LOG_CONT) && !(msg->flags & LOG_PREFIX)))
495 cont = '+';
496
497 len = sprintf(user->buf, "%u,%llu,%llu,%c;",
498 (msg->facility << 3) | msg->level,
499 user->seq, ts_usec, cont);
500 user->prev = msg->flags;
Kay Sieverse11fea92012-05-03 02:29:41 +0200501
502 /* escape non-printable characters */
503 for (i = 0; i < msg->text_len; i++) {
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200504 unsigned char c = log_text(msg)[i];
Kay Sieverse11fea92012-05-03 02:29:41 +0200505
Kay Sieverse3f5a5f2012-07-06 09:50:09 -0700506 if (c < ' ' || c >= 127 || c == '\\')
Kay Sieverse11fea92012-05-03 02:29:41 +0200507 len += sprintf(user->buf + len, "\\x%02x", c);
508 else
509 user->buf[len++] = c;
510 }
511 user->buf[len++] = '\n';
512
513 if (msg->dict_len) {
514 bool line = true;
515
516 for (i = 0; i < msg->dict_len; i++) {
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200517 unsigned char c = log_dict(msg)[i];
Kay Sieverse11fea92012-05-03 02:29:41 +0200518
519 if (line) {
520 user->buf[len++] = ' ';
521 line = false;
522 }
523
524 if (c == '\0') {
525 user->buf[len++] = '\n';
526 line = true;
527 continue;
528 }
529
Kay Sieverse3f5a5f2012-07-06 09:50:09 -0700530 if (c < ' ' || c >= 127 || c == '\\') {
Kay Sieverse11fea92012-05-03 02:29:41 +0200531 len += sprintf(user->buf + len, "\\x%02x", c);
532 continue;
533 }
534
535 user->buf[len++] = c;
536 }
537 user->buf[len++] = '\n';
538 }
539
540 user->idx = log_next(user->idx);
541 user->seq++;
liu chuansheng5c53d812012-07-06 09:50:08 -0700542 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200543
544 if (len > count) {
545 ret = -EINVAL;
546 goto out;
547 }
548
549 if (copy_to_user(buf, user->buf, len)) {
550 ret = -EFAULT;
551 goto out;
552 }
553 ret = len;
554out:
555 mutex_unlock(&user->lock);
556 return ret;
557}
558
559static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence)
560{
561 struct devkmsg_user *user = file->private_data;
562 loff_t ret = 0;
563
564 if (!user)
565 return -EBADF;
566 if (offset)
567 return -ESPIPE;
568
liu chuansheng5c53d812012-07-06 09:50:08 -0700569 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200570 switch (whence) {
571 case SEEK_SET:
572 /* the first record */
573 user->idx = log_first_idx;
574 user->seq = log_first_seq;
575 break;
576 case SEEK_DATA:
577 /*
578 * The first record after the last SYSLOG_ACTION_CLEAR,
579 * like issued by 'dmesg -c'. Reading /dev/kmsg itself
580 * changes no global state, and does not clear anything.
581 */
582 user->idx = clear_idx;
583 user->seq = clear_seq;
584 break;
585 case SEEK_END:
586 /* after the last record */
587 user->idx = log_next_idx;
588 user->seq = log_next_seq;
589 break;
590 default:
591 ret = -EINVAL;
592 }
liu chuansheng5c53d812012-07-06 09:50:08 -0700593 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200594 return ret;
595}
596
597static unsigned int devkmsg_poll(struct file *file, poll_table *wait)
598{
599 struct devkmsg_user *user = file->private_data;
600 int ret = 0;
601
602 if (!user)
603 return POLLERR|POLLNVAL;
604
605 poll_wait(file, &log_wait, wait);
606
liu chuansheng5c53d812012-07-06 09:50:08 -0700607 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200608 if (user->seq < log_next_seq) {
609 /* return error when data has vanished underneath us */
610 if (user->seq < log_first_seq)
611 ret = POLLIN|POLLRDNORM|POLLERR|POLLPRI;
612 ret = POLLIN|POLLRDNORM;
613 }
liu chuansheng5c53d812012-07-06 09:50:08 -0700614 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200615
616 return ret;
617}
618
619static int devkmsg_open(struct inode *inode, struct file *file)
620{
621 struct devkmsg_user *user;
622 int err;
623
624 /* write-only does not need any file context */
625 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
626 return 0;
627
628 err = security_syslog(SYSLOG_ACTION_READ_ALL);
629 if (err)
630 return err;
631
632 user = kmalloc(sizeof(struct devkmsg_user), GFP_KERNEL);
633 if (!user)
634 return -ENOMEM;
635
636 mutex_init(&user->lock);
637
liu chuansheng5c53d812012-07-06 09:50:08 -0700638 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200639 user->idx = log_first_idx;
640 user->seq = log_first_seq;
liu chuansheng5c53d812012-07-06 09:50:08 -0700641 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200642
643 file->private_data = user;
644 return 0;
645}
646
647static int devkmsg_release(struct inode *inode, struct file *file)
648{
649 struct devkmsg_user *user = file->private_data;
650
651 if (!user)
652 return 0;
653
654 mutex_destroy(&user->lock);
655 kfree(user);
656 return 0;
657}
658
659const struct file_operations kmsg_fops = {
660 .open = devkmsg_open,
661 .read = devkmsg_read,
662 .aio_write = devkmsg_writev,
663 .llseek = devkmsg_llseek,
664 .poll = devkmsg_poll,
665 .release = devkmsg_release,
666};
667
Neil Horman04d491a2009-04-02 16:58:57 -0700668#ifdef CONFIG_KEXEC
669/*
670 * This appends the listed symbols to /proc/vmcoreinfo
671 *
672 * /proc/vmcoreinfo is used by various utiilties, like crash and makedumpfile to
673 * obtain access to symbols that are otherwise very difficult to locate. These
674 * symbols are specifically used so that utilities can access and extract the
675 * dmesg log from a vmcore file after a crash.
676 */
677void log_buf_kexec_setup(void)
678{
679 VMCOREINFO_SYMBOL(log_buf);
Neil Horman04d491a2009-04-02 16:58:57 -0700680 VMCOREINFO_SYMBOL(log_buf_len);
Kay Sievers7ff95542012-05-03 02:29:13 +0200681 VMCOREINFO_SYMBOL(log_first_idx);
682 VMCOREINFO_SYMBOL(log_next_idx);
Vivek Goyal67914572012-07-18 13:18:12 -0400683 /*
684 * Export struct log size and field offsets. User space tools can
685 * parse it and detect any changes to structure down the line.
686 */
687 VMCOREINFO_STRUCT_SIZE(log);
688 VMCOREINFO_OFFSET(log, ts_nsec);
689 VMCOREINFO_OFFSET(log, len);
690 VMCOREINFO_OFFSET(log, text_len);
691 VMCOREINFO_OFFSET(log, dict_len);
Neil Horman04d491a2009-04-02 16:58:57 -0700692}
693#endif
694
Mike Travis162a7e72011-05-24 17:13:20 -0700695/* requested log_buf_len from kernel cmdline */
696static unsigned long __initdata new_log_buf_len;
697
698/* save requested log_buf_len since it's too early to process it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699static int __init log_buf_len_setup(char *str)
700{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800701 unsigned size = memparse(str, &str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 if (size)
704 size = roundup_pow_of_two(size);
Mike Travis162a7e72011-05-24 17:13:20 -0700705 if (size > log_buf_len)
706 new_log_buf_len = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Mike Travis162a7e72011-05-24 17:13:20 -0700708 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
Mike Travis162a7e72011-05-24 17:13:20 -0700710early_param("log_buf_len", log_buf_len_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Mike Travis162a7e72011-05-24 17:13:20 -0700712void __init setup_log_buf(int early)
713{
714 unsigned long flags;
Mike Travis162a7e72011-05-24 17:13:20 -0700715 char *new_log_buf;
716 int free;
717
718 if (!new_log_buf_len)
719 return;
720
721 if (early) {
722 unsigned long mem;
723
724 mem = memblock_alloc(new_log_buf_len, PAGE_SIZE);
Tejun Heo1f5026a2011-07-12 09:58:09 +0200725 if (!mem)
Mike Travis162a7e72011-05-24 17:13:20 -0700726 return;
727 new_log_buf = __va(mem);
728 } else {
729 new_log_buf = alloc_bootmem_nopanic(new_log_buf_len);
730 }
731
732 if (unlikely(!new_log_buf)) {
733 pr_err("log_buf_len: %ld bytes not available\n",
734 new_log_buf_len);
735 return;
736 }
737
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200738 raw_spin_lock_irqsave(&logbuf_lock, flags);
Mike Travis162a7e72011-05-24 17:13:20 -0700739 log_buf_len = new_log_buf_len;
740 log_buf = new_log_buf;
741 new_log_buf_len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +0200742 free = __LOG_BUF_LEN - log_next_idx;
743 memcpy(log_buf, __log_buf, __LOG_BUF_LEN);
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200744 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
Mike Travis162a7e72011-05-24 17:13:20 -0700745
746 pr_info("log_buf_len: %d\n", log_buf_len);
747 pr_info("early log buf free: %d(%d%%)\n",
748 free, (free * 100) / __LOG_BUF_LEN);
749}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800751static bool __read_mostly ignore_loglevel;
752
753static int __init ignore_loglevel_setup(char *str)
754{
755 ignore_loglevel = 1;
756 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
757
758 return 0;
759}
760
761early_param("ignore_loglevel", ignore_loglevel_setup);
762module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR);
763MODULE_PARM_DESC(ignore_loglevel, "ignore loglevel setting, to"
764 "print all kernel messages to the console.");
765
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700766#ifdef CONFIG_BOOT_PRINTK_DELAY
767
Namhyung Kim674dff62010-10-26 14:22:48 -0700768static int boot_delay; /* msecs delay after each printk during bootup */
Dave Young3a3b6ed2009-09-22 16:43:31 -0700769static unsigned long long loops_per_msec; /* based on boot_delay */
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700770
771static int __init boot_delay_setup(char *str)
772{
773 unsigned long lpj;
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700774
775 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
776 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
777
778 get_option(&str, &boot_delay);
779 if (boot_delay > 10 * 1000)
780 boot_delay = 0;
781
Dave Young3a3b6ed2009-09-22 16:43:31 -0700782 pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
783 "HZ: %d, loops_per_msec: %llu\n",
784 boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700785 return 1;
786}
787__setup("boot_delay=", boot_delay_setup);
788
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800789static void boot_delay_msec(int level)
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700790{
791 unsigned long long k;
792 unsigned long timeout;
793
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800794 if ((boot_delay == 0 || system_state != SYSTEM_BOOTING)
795 || (level >= console_loglevel && !ignore_loglevel)) {
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700796 return;
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800797 }
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700798
Dave Young3a3b6ed2009-09-22 16:43:31 -0700799 k = (unsigned long long)loops_per_msec * boot_delay;
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700800
801 timeout = jiffies + msecs_to_jiffies(boot_delay);
802 while (k) {
803 k--;
804 cpu_relax();
805 /*
806 * use (volatile) jiffies to prevent
807 * compiler reduction; loop termination via jiffies
808 * is secondary and may or may not happen.
809 */
810 if (time_after(jiffies, timeout))
811 break;
812 touch_nmi_watchdog();
813 }
814}
815#else
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800816static inline void boot_delay_msec(int level)
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700817{
818}
819#endif
820
Dan Rosenbergeaf06b22010-11-11 14:05:18 -0800821#ifdef CONFIG_SECURITY_DMESG_RESTRICT
822int dmesg_restrict = 1;
823#else
824int dmesg_restrict;
825#endif
826
Linus Torvaldsee24aeb2011-02-10 17:53:55 -0800827static int syslog_action_restricted(int type)
828{
829 if (dmesg_restrict)
830 return 1;
831 /* Unless restricted, we allow "read all" and "get buffer size" for everybody */
832 return type != SYSLOG_ACTION_READ_ALL && type != SYSLOG_ACTION_SIZE_BUFFER;
833}
834
835static int check_syslog_permissions(int type, bool from_file)
836{
837 /*
838 * If this is from /proc/kmsg and we've already opened it, then we've
839 * already done the capabilities checks at open time.
840 */
841 if (from_file && type != SYSLOG_ACTION_OPEN)
842 return 0;
843
844 if (syslog_action_restricted(type)) {
845 if (capable(CAP_SYSLOG))
846 return 0;
847 /* For historical reasons, accept CAP_SYS_ADMIN too, with a warning */
848 if (capable(CAP_SYS_ADMIN)) {
Jonathan Niederf2c0d022011-08-08 06:22:43 +0200849 printk_once(KERN_WARNING "%s (%d): "
850 "Attempt to access syslog with CAP_SYS_ADMIN "
851 "but no CAP_SYSLOG (deprecated).\n",
852 current->comm, task_pid_nr(current));
Linus Torvaldsee24aeb2011-02-10 17:53:55 -0800853 return 0;
854 }
855 return -EPERM;
856 }
857 return 0;
858}
859
Kay Sievers7ff95542012-05-03 02:29:13 +0200860#if defined(CONFIG_PRINTK_TIME)
861static bool printk_time = 1;
862#else
863static bool printk_time;
864#endif
865module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
866
Kay Sievers084681d2012-06-28 09:38:53 +0200867static size_t print_time(u64 ts, char *buf)
868{
869 unsigned long rem_nsec;
870
871 if (!printk_time)
872 return 0;
873
Kay Sievers084681d2012-06-28 09:38:53 +0200874 rem_nsec = do_div(ts, 1000000000);
Roland Dreier35dac272013-01-04 15:35:50 -0800875
876 if (!buf)
877 return snprintf(NULL, 0, "[%5lu.000000] ", (unsigned long)ts);
878
Kay Sievers084681d2012-06-28 09:38:53 +0200879 return sprintf(buf, "[%5lu.%06lu] ",
880 (unsigned long)ts, rem_nsec / 1000);
881}
882
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200883static size_t print_prefix(const struct log *msg, bool syslog, char *buf)
Kay Sievers649e6ee2012-05-10 04:30:45 +0200884{
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200885 size_t len = 0;
Kay Sievers43a73a52012-07-06 09:50:09 -0700886 unsigned int prefix = (msg->facility << 3) | msg->level;
Kay Sievers649e6ee2012-05-10 04:30:45 +0200887
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200888 if (syslog) {
889 if (buf) {
Kay Sievers43a73a52012-07-06 09:50:09 -0700890 len += sprintf(buf, "<%u>", prefix);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200891 } else {
892 len += 3;
Kay Sievers43a73a52012-07-06 09:50:09 -0700893 if (prefix > 999)
894 len += 3;
895 else if (prefix > 99)
896 len += 2;
897 else if (prefix > 9)
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200898 len++;
899 }
Kay Sievers7ff95542012-05-03 02:29:13 +0200900 }
901
Kay Sievers084681d2012-06-28 09:38:53 +0200902 len += print_time(msg->ts_nsec, buf ? buf + len : NULL);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200903 return len;
904}
905
Kay Sievers5becfb12012-07-09 12:15:42 -0700906static size_t msg_print_text(const struct log *msg, enum log_flags prev,
907 bool syslog, char *buf, size_t size)
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200908{
909 const char *text = log_text(msg);
910 size_t text_size = msg->text_len;
Kay Sievers5becfb12012-07-09 12:15:42 -0700911 bool prefix = true;
912 bool newline = true;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200913 size_t len = 0;
914
Kay Sievers5becfb12012-07-09 12:15:42 -0700915 if ((prev & LOG_CONT) && !(msg->flags & LOG_PREFIX))
916 prefix = false;
917
918 if (msg->flags & LOG_CONT) {
919 if ((prev & LOG_CONT) && !(prev & LOG_NEWLINE))
920 prefix = false;
921
922 if (!(msg->flags & LOG_NEWLINE))
923 newline = false;
924 }
925
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200926 do {
927 const char *next = memchr(text, '\n', text_size);
928 size_t text_len;
929
930 if (next) {
931 text_len = next - text;
932 next++;
933 text_size -= next - text;
934 } else {
935 text_len = text_size;
936 }
937
938 if (buf) {
939 if (print_prefix(msg, syslog, NULL) +
Kay Sievers70498252012-07-16 18:35:29 -0700940 text_len + 1 >= size - len)
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200941 break;
942
Kay Sievers5becfb12012-07-09 12:15:42 -0700943 if (prefix)
944 len += print_prefix(msg, syslog, buf + len);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200945 memcpy(buf + len, text, text_len);
946 len += text_len;
Kay Sievers5becfb12012-07-09 12:15:42 -0700947 if (next || newline)
948 buf[len++] = '\n';
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200949 } else {
950 /* SYSLOG_ACTION_* buffer size only calculation */
Kay Sievers5becfb12012-07-09 12:15:42 -0700951 if (prefix)
952 len += print_prefix(msg, syslog, NULL);
953 len += text_len;
954 if (next || newline)
955 len++;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200956 }
957
Kay Sievers5becfb12012-07-09 12:15:42 -0700958 prefix = true;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200959 text = next;
960 } while (text);
961
Kay Sievers7ff95542012-05-03 02:29:13 +0200962 return len;
963}
964
965static int syslog_print(char __user *buf, int size)
966{
967 char *text;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200968 struct log *msg;
Jan Beulich116e90b2012-06-22 16:36:09 +0100969 int len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +0200970
Kay Sievers70498252012-07-16 18:35:29 -0700971 text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
Kay Sievers7ff95542012-05-03 02:29:13 +0200972 if (!text)
973 return -ENOMEM;
974
Jan Beulich116e90b2012-06-22 16:36:09 +0100975 while (size > 0) {
976 size_t n;
Kay Sieverseb02dac2012-07-09 10:05:10 -0700977 size_t skip;
Kay Sievers7ff95542012-05-03 02:29:13 +0200978
Jan Beulich116e90b2012-06-22 16:36:09 +0100979 raw_spin_lock_irq(&logbuf_lock);
980 if (syslog_seq < log_first_seq) {
981 /* messages are gone, move to first one */
982 syslog_seq = log_first_seq;
983 syslog_idx = log_first_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -0700984 syslog_prev = 0;
Kay Sieverseb02dac2012-07-09 10:05:10 -0700985 syslog_partial = 0;
Jan Beulich116e90b2012-06-22 16:36:09 +0100986 }
987 if (syslog_seq == log_next_seq) {
988 raw_spin_unlock_irq(&logbuf_lock);
989 break;
990 }
Kay Sieverseb02dac2012-07-09 10:05:10 -0700991
992 skip = syslog_partial;
Jan Beulich116e90b2012-06-22 16:36:09 +0100993 msg = log_from_idx(syslog_idx);
Kay Sievers70498252012-07-16 18:35:29 -0700994 n = msg_print_text(msg, syslog_prev, true, text,
995 LOG_LINE_MAX + PREFIX_MAX);
Kay Sieverseb02dac2012-07-09 10:05:10 -0700996 if (n - syslog_partial <= size) {
997 /* message fits into buffer, move forward */
Jan Beulich116e90b2012-06-22 16:36:09 +0100998 syslog_idx = log_next(syslog_idx);
999 syslog_seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07001000 syslog_prev = msg->flags;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001001 n -= syslog_partial;
1002 syslog_partial = 0;
1003 } else if (!len){
1004 /* partial read(), remember position */
1005 n = size;
1006 syslog_partial += n;
Jan Beulich116e90b2012-06-22 16:36:09 +01001007 } else
1008 n = 0;
1009 raw_spin_unlock_irq(&logbuf_lock);
1010
1011 if (!n)
1012 break;
1013
Kay Sieverseb02dac2012-07-09 10:05:10 -07001014 if (copy_to_user(buf, text + skip, n)) {
Jan Beulich116e90b2012-06-22 16:36:09 +01001015 if (!len)
1016 len = -EFAULT;
1017 break;
1018 }
Kay Sieverseb02dac2012-07-09 10:05:10 -07001019
1020 len += n;
1021 size -= n;
1022 buf += n;
Jan Beulich116e90b2012-06-22 16:36:09 +01001023 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001024
1025 kfree(text);
1026 return len;
1027}
1028
1029static int syslog_print_all(char __user *buf, int size, bool clear)
1030{
1031 char *text;
1032 int len = 0;
1033
Kay Sievers70498252012-07-16 18:35:29 -07001034 text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
Kay Sievers7ff95542012-05-03 02:29:13 +02001035 if (!text)
1036 return -ENOMEM;
1037
1038 raw_spin_lock_irq(&logbuf_lock);
1039 if (buf) {
1040 u64 next_seq;
1041 u64 seq;
1042 u32 idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001043 enum log_flags prev;
Kay Sievers7ff95542012-05-03 02:29:13 +02001044
1045 if (clear_seq < log_first_seq) {
1046 /* messages are gone, move to first available one */
1047 clear_seq = log_first_seq;
1048 clear_idx = log_first_idx;
1049 }
1050
1051 /*
1052 * Find first record that fits, including all following records,
1053 * into the user-provided buffer for this dump.
Kay Sieverse2ae7152012-06-15 14:07:51 +02001054 */
Kay Sievers7ff95542012-05-03 02:29:13 +02001055 seq = clear_seq;
1056 idx = clear_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001057 prev = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001058 while (seq < log_next_seq) {
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001059 struct log *msg = log_from_idx(idx);
1060
Kay Sievers5becfb12012-07-09 12:15:42 -07001061 len += msg_print_text(msg, prev, true, NULL, 0);
Jeff Mahoneye3756472012-08-10 15:07:09 -04001062 prev = msg->flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02001063 idx = log_next(idx);
1064 seq++;
1065 }
Kay Sieverse2ae7152012-06-15 14:07:51 +02001066
1067 /* move first record forward until length fits into the buffer */
Kay Sievers7ff95542012-05-03 02:29:13 +02001068 seq = clear_seq;
1069 idx = clear_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001070 prev = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001071 while (len > size && seq < log_next_seq) {
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001072 struct log *msg = log_from_idx(idx);
1073
Kay Sievers5becfb12012-07-09 12:15:42 -07001074 len -= msg_print_text(msg, prev, true, NULL, 0);
Jeff Mahoneye3756472012-08-10 15:07:09 -04001075 prev = msg->flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02001076 idx = log_next(idx);
1077 seq++;
1078 }
1079
Kay Sieverse2ae7152012-06-15 14:07:51 +02001080 /* last message fitting into this dump */
Kay Sievers7ff95542012-05-03 02:29:13 +02001081 next_seq = log_next_seq;
1082
1083 len = 0;
Kay Sievers5becfb12012-07-09 12:15:42 -07001084 prev = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001085 while (len >= 0 && seq < next_seq) {
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001086 struct log *msg = log_from_idx(idx);
Kay Sievers7ff95542012-05-03 02:29:13 +02001087 int textlen;
1088
Kay Sievers70498252012-07-16 18:35:29 -07001089 textlen = msg_print_text(msg, prev, true, text,
1090 LOG_LINE_MAX + PREFIX_MAX);
Kay Sievers7ff95542012-05-03 02:29:13 +02001091 if (textlen < 0) {
1092 len = textlen;
1093 break;
1094 }
1095 idx = log_next(idx);
1096 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07001097 prev = msg->flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02001098
1099 raw_spin_unlock_irq(&logbuf_lock);
1100 if (copy_to_user(buf + len, text, textlen))
1101 len = -EFAULT;
1102 else
1103 len += textlen;
1104 raw_spin_lock_irq(&logbuf_lock);
1105
1106 if (seq < log_first_seq) {
1107 /* messages are gone, move to next one */
1108 seq = log_first_seq;
1109 idx = log_first_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001110 prev = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001111 }
1112 }
1113 }
1114
1115 if (clear) {
1116 clear_seq = log_next_seq;
1117 clear_idx = log_next_idx;
1118 }
1119 raw_spin_unlock_irq(&logbuf_lock);
1120
1121 kfree(text);
1122 return len;
1123}
1124
Kees Cook00234592010-02-03 15:36:43 -08001125int do_syslog(int type, char __user *buf, int len, bool from_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
Kay Sievers7ff95542012-05-03 02:29:13 +02001127 bool clear = false;
1128 static int saved_console_loglevel = -1;
Linus Torvaldsee24aeb2011-02-10 17:53:55 -08001129 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Linus Torvaldsee24aeb2011-02-10 17:53:55 -08001131 error = check_syslog_permissions(type, from_file);
1132 if (error)
1133 goto out;
Eric Paris12b30522010-11-15 18:36:29 -05001134
1135 error = security_syslog(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if (error)
1137 return error;
1138
1139 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08001140 case SYSLOG_ACTION_CLOSE: /* Close log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001142 case SYSLOG_ACTION_OPEN: /* Open log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001144 case SYSLOG_ACTION_READ: /* Read from log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 error = -EINVAL;
1146 if (!buf || len < 0)
1147 goto out;
1148 error = 0;
1149 if (!len)
1150 goto out;
1151 if (!access_ok(VERIFY_WRITE, buf, len)) {
1152 error = -EFAULT;
1153 goto out;
1154 }
Yuanhan Liu4a77a5a2012-06-16 21:21:51 +08001155 error = wait_event_interruptible(log_wait,
1156 syslog_seq != log_next_seq);
Kay Sieverscb424ff2012-07-06 09:50:09 -07001157 if (error)
Yuanhan Liu4a77a5a2012-06-16 21:21:51 +08001158 goto out;
Kay Sievers7ff95542012-05-03 02:29:13 +02001159 error = syslog_print(buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001161 /* Read/clear last kernel messages */
1162 case SYSLOG_ACTION_READ_CLEAR:
Kay Sievers7ff95542012-05-03 02:29:13 +02001163 clear = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 /* FALL THRU */
Kees Cookd78ca3c2010-02-03 15:37:13 -08001165 /* Read last kernel messages */
1166 case SYSLOG_ACTION_READ_ALL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 error = -EINVAL;
1168 if (!buf || len < 0)
1169 goto out;
1170 error = 0;
1171 if (!len)
1172 goto out;
1173 if (!access_ok(VERIFY_WRITE, buf, len)) {
1174 error = -EFAULT;
1175 goto out;
1176 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001177 error = syslog_print_all(buf, len, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001179 /* Clear ring buffer */
1180 case SYSLOG_ACTION_CLEAR:
Kay Sievers7ff95542012-05-03 02:29:13 +02001181 syslog_print_all(NULL, 0, true);
Alan Stern4661e352012-06-22 17:12:19 -04001182 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001183 /* Disable logging to console */
1184 case SYSLOG_ACTION_CONSOLE_OFF:
Frans Pop1aaad492009-07-06 13:31:48 +02001185 if (saved_console_loglevel == -1)
1186 saved_console_loglevel = console_loglevel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 console_loglevel = minimum_console_loglevel;
1188 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001189 /* Enable logging to console */
1190 case SYSLOG_ACTION_CONSOLE_ON:
Frans Pop1aaad492009-07-06 13:31:48 +02001191 if (saved_console_loglevel != -1) {
1192 console_loglevel = saved_console_loglevel;
1193 saved_console_loglevel = -1;
1194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001196 /* Set level of messages printed to console */
1197 case SYSLOG_ACTION_CONSOLE_LEVEL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 error = -EINVAL;
1199 if (len < 1 || len > 8)
1200 goto out;
1201 if (len < minimum_console_loglevel)
1202 len = minimum_console_loglevel;
1203 console_loglevel = len;
Frans Pop1aaad492009-07-06 13:31:48 +02001204 /* Implicitly re-enable logging to console */
1205 saved_console_loglevel = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 error = 0;
1207 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001208 /* Number of chars in the log buffer */
1209 case SYSLOG_ACTION_SIZE_UNREAD:
Kay Sievers7ff95542012-05-03 02:29:13 +02001210 raw_spin_lock_irq(&logbuf_lock);
1211 if (syslog_seq < log_first_seq) {
1212 /* messages are gone, move to first one */
1213 syslog_seq = log_first_seq;
1214 syslog_idx = log_first_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001215 syslog_prev = 0;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001216 syslog_partial = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001217 }
1218 if (from_file) {
1219 /*
1220 * Short-cut for poll(/"proc/kmsg") which simply checks
1221 * for pending data, not the size; return the count of
1222 * records, not the length.
1223 */
1224 error = log_next_idx - syslog_idx;
1225 } else {
Kay Sievers5becfb12012-07-09 12:15:42 -07001226 u64 seq = syslog_seq;
1227 u32 idx = syslog_idx;
1228 enum log_flags prev = syslog_prev;
Kay Sievers7ff95542012-05-03 02:29:13 +02001229
1230 error = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001231 while (seq < log_next_seq) {
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001232 struct log *msg = log_from_idx(idx);
1233
Kay Sievers5becfb12012-07-09 12:15:42 -07001234 error += msg_print_text(msg, prev, true, NULL, 0);
Kay Sievers7ff95542012-05-03 02:29:13 +02001235 idx = log_next(idx);
1236 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07001237 prev = msg->flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02001238 }
Kay Sieverseb02dac2012-07-09 10:05:10 -07001239 error -= syslog_partial;
Kay Sievers7ff95542012-05-03 02:29:13 +02001240 }
1241 raw_spin_unlock_irq(&logbuf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001243 /* Size of the log buffer */
1244 case SYSLOG_ACTION_SIZE_BUFFER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 error = log_buf_len;
1246 break;
1247 default:
1248 error = -EINVAL;
1249 break;
1250 }
1251out:
1252 return error;
1253}
1254
Heiko Carstens1e7bfb22009-01-14 14:14:29 +01001255SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Kees Cook00234592010-02-03 15:36:43 -08001257 return do_syslog(type, buf, len, SYSLOG_FROM_CALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 * Call the console drivers, asking them to write out
1262 * log_buf[start] to log_buf[end - 1].
Torben Hohnac751ef2011-01-25 15:07:35 -08001263 * The console_lock must be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 */
Kay Sievers7ff95542012-05-03 02:29:13 +02001265static void call_console_drivers(int level, const char *text, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
Kay Sievers7ff95542012-05-03 02:29:13 +02001267 struct console *con;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Kay Sievers7ff95542012-05-03 02:29:13 +02001269 trace_console(text, 0, len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Kay Sievers7ff95542012-05-03 02:29:13 +02001271 if (level >= console_loglevel && !ignore_loglevel)
1272 return;
1273 if (!console_drivers)
1274 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Kay Sievers7ff95542012-05-03 02:29:13 +02001276 for_each_console(con) {
1277 if (exclusive_console && con != exclusive_console)
1278 continue;
1279 if (!(con->flags & CON_ENABLED))
1280 continue;
1281 if (!con->write)
1282 continue;
1283 if (!cpu_online(smp_processor_id()) &&
1284 !(con->flags & CON_ANYTIME))
1285 continue;
1286 con->write(con, text, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
1290/*
1291 * Zap console related locks when oopsing. Only zap at most once
1292 * every 10 seconds, to leave time for slow consoles to print a
1293 * full oops.
1294 */
1295static void zap_locks(void)
1296{
1297 static unsigned long oops_timestamp;
1298
1299 if (time_after_eq(jiffies, oops_timestamp) &&
Jesper Juhl40dc5652005-10-30 15:02:46 -08001300 !time_after(jiffies, oops_timestamp + 30 * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 return;
1302
1303 oops_timestamp = jiffies;
1304
Peter Zijlstra94d24fc2011-06-07 11:17:30 +02001305 debug_locks_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 /* If a crash is occurring, make sure we can't deadlock */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001307 raw_spin_lock_init(&logbuf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 /* And make sure that we print immediately */
Thomas Gleixner5b8c4f22010-09-07 14:33:43 +00001309 sema_init(&console_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311
Michael Ellerman76a8ad22006-06-25 05:47:40 -07001312/* Check if we have any console registered that can be called early in boot. */
1313static int have_callable_console(void)
1314{
1315 struct console *con;
1316
Robin Getz4d091612009-07-01 21:08:37 -04001317 for_each_console(con)
Michael Ellerman76a8ad22006-06-25 05:47:40 -07001318 if (con->flags & CON_ANYTIME)
1319 return 1;
1320
1321 return 0;
1322}
1323
Linus Torvalds266c2e02008-03-24 19:25:08 -07001324/*
1325 * Can we actually use the console at this time on this cpu?
1326 *
1327 * Console drivers may assume that per-cpu resources have
1328 * been allocated. So unless they're explicitly marked as
1329 * being able to cope (CON_ANYTIME) don't call them until
1330 * this CPU is officially up.
1331 */
1332static inline int can_use_console(unsigned int cpu)
1333{
1334 return cpu_online(cpu) || have_callable_console();
1335}
1336
1337/*
1338 * Try to get console ownership to actually show the kernel
1339 * messages from a 'printk'. Return true (and with the
Torben Hohnac751ef2011-01-25 15:07:35 -08001340 * console_lock held, and 'console_locked' set) if it
Linus Torvalds266c2e02008-03-24 19:25:08 -07001341 * is successful, false otherwise.
1342 *
1343 * This gets called with the 'logbuf_lock' spinlock held and
1344 * interrupts disabled. It should return with 'lockbuf_lock'
1345 * released but interrupts still disabled.
1346 */
Torben Hohnac751ef2011-01-25 15:07:35 -08001347static int console_trylock_for_printk(unsigned int cpu)
Namhyung Kim8155c022010-10-26 14:22:47 -07001348 __releases(&logbuf_lock)
Linus Torvalds266c2e02008-03-24 19:25:08 -07001349{
Peter Zijlstra0b5e1c52011-06-07 11:15:33 +02001350 int retval = 0, wake = 0;
Linus Torvalds266c2e02008-03-24 19:25:08 -07001351
Torben Hohnac751ef2011-01-25 15:07:35 -08001352 if (console_trylock()) {
Linus Torvalds093a07e2008-04-15 13:09:54 -07001353 retval = 1;
1354
1355 /*
1356 * If we can't use the console, we need to release
1357 * the console semaphore by hand to avoid flushing
1358 * the buffer. We need to hold the console semaphore
1359 * in order to do this test safely.
1360 */
1361 if (!can_use_console(cpu)) {
1362 console_locked = 0;
Peter Zijlstra0b5e1c52011-06-07 11:15:33 +02001363 wake = 1;
Linus Torvalds093a07e2008-04-15 13:09:54 -07001364 retval = 0;
1365 }
1366 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001367 logbuf_cpu = UINT_MAX;
Peter Zijlstra0b5e1c52011-06-07 11:15:33 +02001368 if (wake)
1369 up(&console_sem);
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001370 raw_spin_unlock(&logbuf_lock);
Linus Torvalds266c2e02008-03-24 19:25:08 -07001371 return retval;
1372}
Ingo Molnar32a76002008-01-25 21:07:58 +01001373
Dave Youngaf913222009-09-22 16:43:33 -07001374int printk_delay_msec __read_mostly;
1375
1376static inline void printk_delay(void)
1377{
1378 if (unlikely(printk_delay_msec)) {
1379 int m = printk_delay_msec;
1380
1381 while (m--) {
1382 mdelay(1);
1383 touch_nmi_watchdog();
1384 }
1385 }
1386}
1387
Kay Sievers084681d2012-06-28 09:38:53 +02001388/*
1389 * Continuation lines are buffered, and not committed to the record buffer
1390 * until the line is complete, or a race forces it. The line fragments
1391 * though, are printed immediately to the consoles to ensure everything has
1392 * reached the console in case of a kernel crash.
1393 */
1394static struct cont {
1395 char buf[LOG_LINE_MAX];
1396 size_t len; /* length == 0 means unused buffer */
1397 size_t cons; /* bytes written to console */
1398 struct task_struct *owner; /* task of first print*/
1399 u64 ts_nsec; /* time of first print */
1400 u8 level; /* log level of first message */
1401 u8 facility; /* log level of first message */
Kay Sieverseab07262012-07-16 18:35:30 -07001402 enum log_flags flags; /* prefix, newline flags */
Kay Sievers084681d2012-06-28 09:38:53 +02001403 bool flushed:1; /* buffer sealed and committed */
1404} cont;
1405
Kay Sievers70498252012-07-16 18:35:29 -07001406static void cont_flush(enum log_flags flags)
Kay Sievers084681d2012-06-28 09:38:53 +02001407{
1408 if (cont.flushed)
1409 return;
1410 if (cont.len == 0)
1411 return;
1412
Kay Sieverseab07262012-07-16 18:35:30 -07001413 if (cont.cons) {
1414 /*
1415 * If a fragment of this line was directly flushed to the
1416 * console; wait for the console to pick up the rest of the
1417 * line. LOG_NOCONS suppresses a duplicated output.
1418 */
1419 log_store(cont.facility, cont.level, flags | LOG_NOCONS,
1420 cont.ts_nsec, NULL, 0, cont.buf, cont.len);
1421 cont.flags = flags;
1422 cont.flushed = true;
1423 } else {
1424 /*
1425 * If no fragment of this line ever reached the console,
1426 * just submit it to the store and free the buffer.
1427 */
1428 log_store(cont.facility, cont.level, flags, 0,
1429 NULL, 0, cont.buf, cont.len);
1430 cont.len = 0;
1431 }
Kay Sievers084681d2012-06-28 09:38:53 +02001432}
1433
1434static bool cont_add(int facility, int level, const char *text, size_t len)
1435{
1436 if (cont.len && cont.flushed)
1437 return false;
1438
1439 if (cont.len + len > sizeof(cont.buf)) {
Kay Sievers70498252012-07-16 18:35:29 -07001440 /* the line gets too long, split it up in separate records */
1441 cont_flush(LOG_CONT);
Kay Sievers084681d2012-06-28 09:38:53 +02001442 return false;
1443 }
1444
1445 if (!cont.len) {
1446 cont.facility = facility;
1447 cont.level = level;
1448 cont.owner = current;
1449 cont.ts_nsec = local_clock();
Kay Sieverseab07262012-07-16 18:35:30 -07001450 cont.flags = 0;
Kay Sievers084681d2012-06-28 09:38:53 +02001451 cont.cons = 0;
1452 cont.flushed = false;
1453 }
1454
1455 memcpy(cont.buf + cont.len, text, len);
1456 cont.len += len;
Kay Sieverseab07262012-07-16 18:35:30 -07001457
1458 if (cont.len > (sizeof(cont.buf) * 80) / 100)
1459 cont_flush(LOG_CONT);
1460
Kay Sievers084681d2012-06-28 09:38:53 +02001461 return true;
1462}
1463
1464static size_t cont_print_text(char *text, size_t size)
1465{
1466 size_t textlen = 0;
1467 size_t len;
1468
Kay Sieverseab07262012-07-16 18:35:30 -07001469 if (cont.cons == 0 && (console_prev & LOG_NEWLINE)) {
Kay Sievers084681d2012-06-28 09:38:53 +02001470 textlen += print_time(cont.ts_nsec, text);
1471 size -= textlen;
1472 }
1473
1474 len = cont.len - cont.cons;
1475 if (len > 0) {
1476 if (len+1 > size)
1477 len = size-1;
1478 memcpy(text + textlen, cont.buf + cont.cons, len);
1479 textlen += len;
1480 cont.cons = cont.len;
1481 }
1482
1483 if (cont.flushed) {
Kay Sieverseab07262012-07-16 18:35:30 -07001484 if (cont.flags & LOG_NEWLINE)
1485 text[textlen++] = '\n';
Kay Sievers084681d2012-06-28 09:38:53 +02001486 /* got everything, release buffer */
1487 cont.len = 0;
1488 }
1489 return textlen;
1490}
1491
Kay Sievers7ff95542012-05-03 02:29:13 +02001492asmlinkage int vprintk_emit(int facility, int level,
1493 const char *dict, size_t dictlen,
1494 const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Kay Sievers7ff95542012-05-03 02:29:13 +02001496 static int recursion_bug;
Kay Sievers7ff95542012-05-03 02:29:13 +02001497 static char textbuf[LOG_LINE_MAX];
1498 char *text = textbuf;
Kay Sieversc313af12012-05-14 20:46:27 +02001499 size_t text_len;
Kay Sievers5becfb12012-07-09 12:15:42 -07001500 enum log_flags lflags = 0;
Nick Andrewac60ad72008-05-12 21:21:04 +02001501 unsigned long flags;
Ingo Molnar32a76002008-01-25 21:07:58 +01001502 int this_cpu;
Kay Sievers7ff95542012-05-03 02:29:13 +02001503 int printed_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Andrew Cooks2fa72c82012-12-17 15:59:56 -08001505 boot_delay_msec(level);
Dave Youngaf913222009-09-22 16:43:33 -07001506 printk_delay();
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 /* This stops the holder of console_sem just where we want him */
Peter Zijlstra1a9a8ae2011-06-07 11:17:30 +02001509 local_irq_save(flags);
Ingo Molnar32a76002008-01-25 21:07:58 +01001510 this_cpu = smp_processor_id();
1511
1512 /*
1513 * Ouch, printk recursed into itself!
1514 */
Kay Sievers7ff95542012-05-03 02:29:13 +02001515 if (unlikely(logbuf_cpu == this_cpu)) {
Ingo Molnar32a76002008-01-25 21:07:58 +01001516 /*
1517 * If a crash is occurring during printk() on this CPU,
1518 * then try to get the crash message out but make sure
1519 * we can't deadlock. Otherwise just return to avoid the
1520 * recursion and return - but flag the recursion so that
1521 * it can be printed at the next appropriate moment:
1522 */
Peter Zijlstra94d24fc2011-06-07 11:17:30 +02001523 if (!oops_in_progress && !lockdep_recursing(current)) {
Tejun Heo3b8945e2008-05-12 21:21:04 +02001524 recursion_bug = 1;
Ingo Molnar32a76002008-01-25 21:07:58 +01001525 goto out_restore_irqs;
1526 }
1527 zap_locks();
1528 }
1529
Ingo Molnara0f1ccf2006-07-03 00:24:58 -07001530 lockdep_off();
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001531 raw_spin_lock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02001532 logbuf_cpu = this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Tejun Heo3b8945e2008-05-12 21:21:04 +02001534 if (recursion_bug) {
Kay Sievers7ff95542012-05-03 02:29:13 +02001535 static const char recursion_msg[] =
1536 "BUG: recent printk recursion!";
1537
Tejun Heo3b8945e2008-05-12 21:21:04 +02001538 recursion_bug = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001539 printed_len += strlen(recursion_msg);
1540 /* emit KERN_CRIT message */
Kay Sievers5becfb12012-07-09 12:15:42 -07001541 log_store(0, 2, LOG_PREFIX|LOG_NEWLINE, 0,
Kay Sievers084681d2012-06-28 09:38:53 +02001542 NULL, 0, recursion_msg, printed_len);
Linus Torvalds5fd29d62009-06-16 10:57:02 -07001543 }
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 /*
Kay Sievers7ff95542012-05-03 02:29:13 +02001546 * The printf needs to come first; we need the syslog
1547 * prefix which might be passed-in as a parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 */
Kay Sieversc313af12012-05-14 20:46:27 +02001549 text_len = vscnprintf(text, sizeof(textbuf), fmt, args);
Nick Andrewac60ad72008-05-12 21:21:04 +02001550
Kay Sievers7ff95542012-05-03 02:29:13 +02001551 /* mark and strip a trailing newline */
Kay Sieversc313af12012-05-14 20:46:27 +02001552 if (text_len && text[text_len-1] == '\n') {
1553 text_len--;
Kay Sievers5becfb12012-07-09 12:15:42 -07001554 lflags |= LOG_NEWLINE;
Kay Sievers7ff95542012-05-03 02:29:13 +02001555 }
Kay Sievers9d90c8d2011-03-13 03:19:51 +01001556
Joe Perches088a52a2012-07-30 14:40:19 -07001557 /* strip kernel syslog prefix and extract log level or control flags */
1558 if (facility == 0) {
1559 int kern_level = printk_get_level(text);
1560
1561 if (kern_level) {
1562 const char *end_of_header = printk_skip_level(text);
1563 switch (kern_level) {
1564 case '0' ... '7':
1565 if (level == -1)
1566 level = kern_level - '0';
1567 case 'd': /* KERN_DEFAULT */
1568 lflags |= LOG_PREFIX;
1569 case 'c': /* KERN_CONT */
1570 break;
1571 }
1572 text_len -= end_of_header - text;
1573 text = (char *)end_of_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001575 }
Nick Andrewac60ad72008-05-12 21:21:04 +02001576
Kay Sieversc313af12012-05-14 20:46:27 +02001577 if (level == -1)
1578 level = default_message_loglevel;
1579
Kay Sievers5becfb12012-07-09 12:15:42 -07001580 if (dict)
1581 lflags |= LOG_PREFIX|LOG_NEWLINE;
Kay Sievers7ff95542012-05-03 02:29:13 +02001582
Kay Sievers5becfb12012-07-09 12:15:42 -07001583 if (!(lflags & LOG_NEWLINE)) {
Kay Sievers084681d2012-06-28 09:38:53 +02001584 /*
1585 * Flush the conflicting buffer. An earlier newline was missing,
1586 * or another task also prints continuation lines.
1587 */
Kay Sievers5becfb12012-07-09 12:15:42 -07001588 if (cont.len && (lflags & LOG_PREFIX || cont.owner != current))
Kay Sieverseab07262012-07-16 18:35:30 -07001589 cont_flush(LOG_NEWLINE);
Kay Sieversc313af12012-05-14 20:46:27 +02001590
Kay Sievers084681d2012-06-28 09:38:53 +02001591 /* buffer line if possible, otherwise store it right away */
1592 if (!cont_add(facility, level, text, text_len))
Kay Sievers5becfb12012-07-09 12:15:42 -07001593 log_store(facility, level, lflags | LOG_CONT, 0,
Kay Sieversc313af12012-05-14 20:46:27 +02001594 dict, dictlen, text, text_len);
Kay Sievers084681d2012-06-28 09:38:53 +02001595 } else {
1596 bool stored = false;
1597
1598 /*
Steven Rostedtd3620822012-06-29 11:40:11 -04001599 * If an earlier newline was missing and it was the same task,
1600 * either merge it with the current buffer and flush, or if
1601 * there was a race with interrupts (prefix == true) then just
1602 * flush it out and store this line separately.
Kay Sievers084681d2012-06-28 09:38:53 +02001603 */
Kay Sievers084681d2012-06-28 09:38:53 +02001604 if (cont.len && cont.owner == current) {
Kay Sievers5becfb12012-07-09 12:15:42 -07001605 if (!(lflags & LOG_PREFIX))
Steven Rostedtd3620822012-06-29 11:40:11 -04001606 stored = cont_add(facility, level, text, text_len);
Kay Sieverseab07262012-07-16 18:35:30 -07001607 cont_flush(LOG_NEWLINE);
Kay Sieversc313af12012-05-14 20:46:27 +02001608 }
Kay Sievers084681d2012-06-28 09:38:53 +02001609
1610 if (!stored)
Kay Sievers5becfb12012-07-09 12:15:42 -07001611 log_store(facility, level, lflags, 0,
Kay Sievers084681d2012-06-28 09:38:53 +02001612 dict, dictlen, text, text_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
Kay Sievers084681d2012-06-28 09:38:53 +02001614 printed_len += text_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Linus Torvalds266c2e02008-03-24 19:25:08 -07001616 /*
Kay Sievers7ff95542012-05-03 02:29:13 +02001617 * Try to acquire and then immediately release the console semaphore.
1618 * The release will print out buffers and wake up /dev/kmsg and syslog()
1619 * users.
Linus Torvalds266c2e02008-03-24 19:25:08 -07001620 *
Kay Sievers7ff95542012-05-03 02:29:13 +02001621 * The console_trylock_for_printk() function will release 'logbuf_lock'
1622 * regardless of whether it actually gets the console semaphore or not.
Linus Torvalds266c2e02008-03-24 19:25:08 -07001623 */
Torben Hohnac751ef2011-01-25 15:07:35 -08001624 if (console_trylock_for_printk(this_cpu))
1625 console_unlock();
Michael Ellerman76a8ad22006-06-25 05:47:40 -07001626
Linus Torvalds266c2e02008-03-24 19:25:08 -07001627 lockdep_on();
Ingo Molnar32a76002008-01-25 21:07:58 +01001628out_restore_irqs:
Peter Zijlstra1a9a8ae2011-06-07 11:17:30 +02001629 local_irq_restore(flags);
Michael Ellerman76a8ad22006-06-25 05:47:40 -07001630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 return printed_len;
1632}
Kay Sievers7ff95542012-05-03 02:29:13 +02001633EXPORT_SYMBOL(vprintk_emit);
1634
1635asmlinkage int vprintk(const char *fmt, va_list args)
1636{
1637 return vprintk_emit(0, -1, NULL, 0, fmt, args);
1638}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639EXPORT_SYMBOL(vprintk);
1640
Kay Sievers7ff95542012-05-03 02:29:13 +02001641asmlinkage int printk_emit(int facility, int level,
1642 const char *dict, size_t dictlen,
1643 const char *fmt, ...)
1644{
1645 va_list args;
1646 int r;
1647
1648 va_start(args, fmt);
1649 r = vprintk_emit(facility, level, dict, dictlen, fmt, args);
1650 va_end(args);
1651
1652 return r;
1653}
1654EXPORT_SYMBOL(printk_emit);
1655
1656/**
1657 * printk - print a kernel message
1658 * @fmt: format string
1659 *
1660 * This is printk(). It can be called from any context. We want it to work.
1661 *
1662 * We try to grab the console_lock. If we succeed, it's easy - we log the
1663 * output and call the console drivers. If we fail to get the semaphore, we
1664 * place the output into the log buffer and return. The current holder of
1665 * the console_sem will notice the new output in console_unlock(); and will
1666 * send it to the consoles before releasing the lock.
1667 *
1668 * One effect of this deferred printing is that code which calls printk() and
1669 * then changes console_loglevel may break. This is because console_loglevel
1670 * is inspected when the actual printing occurs.
1671 *
1672 * See also:
1673 * printf(3)
1674 *
1675 * See the vsnprintf() documentation for format string extensions over C99.
1676 */
1677asmlinkage int printk(const char *fmt, ...)
1678{
1679 va_list args;
1680 int r;
1681
1682#ifdef CONFIG_KGDB_KDB
1683 if (unlikely(kdb_trap_printk)) {
1684 va_start(args, fmt);
1685 r = vkdb_printf(fmt, args);
1686 va_end(args);
1687 return r;
1688 }
1689#endif
1690 va_start(args, fmt);
1691 r = vprintk_emit(0, -1, NULL, 0, fmt, args);
1692 va_end(args);
1693
1694 return r;
1695}
1696EXPORT_SYMBOL(printk);
Kay Sievers7f3a7812012-05-09 01:37:51 +02001697
Kay Sievers96efedf2012-07-16 18:35:29 -07001698#else /* CONFIG_PRINTK */
Matt Mackalld59745c2005-05-01 08:59:02 -07001699
Kay Sievers70498252012-07-16 18:35:29 -07001700#define LOG_LINE_MAX 0
1701#define PREFIX_MAX 0
Kay Sievers7f3a7812012-05-09 01:37:51 +02001702#define LOG_LINE_MAX 0
Kay Sievers96efedf2012-07-16 18:35:29 -07001703static u64 syslog_seq;
1704static u32 syslog_idx;
Kay Sieverseab07262012-07-16 18:35:30 -07001705static u64 console_seq;
1706static u32 console_idx;
Kay Sievers96efedf2012-07-16 18:35:29 -07001707static enum log_flags syslog_prev;
1708static u64 log_first_seq;
1709static u32 log_first_idx;
1710static u64 log_next_seq;
Kay Sieverseab07262012-07-16 18:35:30 -07001711static enum log_flags console_prev;
Kay Sievers084681d2012-06-28 09:38:53 +02001712static struct cont {
1713 size_t len;
1714 size_t cons;
1715 u8 level;
1716 bool flushed:1;
1717} cont;
Kay Sievers7f3a7812012-05-09 01:37:51 +02001718static struct log *log_from_idx(u32 idx) { return NULL; }
1719static u32 log_next(u32 idx) { return 0; }
Kay Sievers7f3a7812012-05-09 01:37:51 +02001720static void call_console_drivers(int level, const char *text, size_t len) {}
Kay Sievers5becfb12012-07-09 12:15:42 -07001721static size_t msg_print_text(const struct log *msg, enum log_flags prev,
1722 bool syslog, char *buf, size_t size) { return 0; }
Kay Sievers084681d2012-06-28 09:38:53 +02001723static size_t cont_print_text(char *text, size_t size) { return 0; }
Matt Mackalld59745c2005-05-01 08:59:02 -07001724
Kay Sievers7f3a7812012-05-09 01:37:51 +02001725#endif /* CONFIG_PRINTK */
Matt Mackalld59745c2005-05-01 08:59:02 -07001726
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001727static int __add_preferred_console(char *name, int idx, char *options,
1728 char *brl_options)
1729{
1730 struct console_cmdline *c;
1731 int i;
1732
1733 /*
1734 * See if this tty is not yet registered, and
1735 * if we have a slot free.
1736 */
1737 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
1738 if (strcmp(console_cmdline[i].name, name) == 0 &&
1739 console_cmdline[i].index == idx) {
1740 if (!brl_options)
1741 selected_console = i;
1742 return 0;
1743 }
1744 if (i == MAX_CMDLINECONSOLES)
1745 return -E2BIG;
1746 if (!brl_options)
1747 selected_console = i;
1748 c = &console_cmdline[i];
1749 strlcpy(c->name, name, sizeof(c->name));
1750 c->options = options;
1751#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1752 c->brl_options = brl_options;
1753#endif
1754 c->index = idx;
1755 return 0;
1756}
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001757/*
1758 * Set up a list of consoles. Called from init/main.c
1759 */
1760static int __init console_setup(char *str)
1761{
Yinghai Lueaa944a2007-07-15 23:37:27 -07001762 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001763 char *s, *options, *brl_options = NULL;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001764 int idx;
1765
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001766#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1767 if (!memcmp(str, "brl,", 4)) {
1768 brl_options = "";
1769 str += 4;
1770 } else if (!memcmp(str, "brl=", 4)) {
1771 brl_options = str + 4;
1772 str = strchr(brl_options, ',');
1773 if (!str) {
1774 printk(KERN_ERR "need port name after brl=\n");
1775 return 1;
1776 }
1777 *(str++) = 0;
1778 }
1779#endif
1780
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001781 /*
1782 * Decode str into name, index, options.
1783 */
1784 if (str[0] >= '0' && str[0] <= '9') {
Yinghai Lueaa944a2007-07-15 23:37:27 -07001785 strcpy(buf, "ttyS");
1786 strncpy(buf + 4, str, sizeof(buf) - 5);
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001787 } else {
Yinghai Lueaa944a2007-07-15 23:37:27 -07001788 strncpy(buf, str, sizeof(buf) - 1);
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001789 }
Yinghai Lueaa944a2007-07-15 23:37:27 -07001790 buf[sizeof(buf) - 1] = 0;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001791 if ((options = strchr(str, ',')) != NULL)
1792 *(options++) = 0;
1793#ifdef __sparc__
1794 if (!strcmp(str, "ttya"))
Yinghai Lueaa944a2007-07-15 23:37:27 -07001795 strcpy(buf, "ttyS0");
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001796 if (!strcmp(str, "ttyb"))
Yinghai Lueaa944a2007-07-15 23:37:27 -07001797 strcpy(buf, "ttyS1");
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001798#endif
Yinghai Lueaa944a2007-07-15 23:37:27 -07001799 for (s = buf; *s; s++)
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001800 if ((*s >= '0' && *s <= '9') || *s == ',')
1801 break;
1802 idx = simple_strtoul(s, NULL, 10);
1803 *s = 0;
1804
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001805 __add_preferred_console(buf, idx, options, brl_options);
Markus Armbruster9e124fe2008-05-26 23:31:07 +01001806 console_set_on_cmdline = 1;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001807 return 1;
1808}
1809__setup("console=", console_setup);
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811/**
Matt Mackall3c0547b2005-05-16 21:53:47 -07001812 * add_preferred_console - add a device to the list of preferred consoles.
Martin Waitzddad86c2005-11-13 16:08:14 -08001813 * @name: device name
1814 * @idx: device index
1815 * @options: options for this console
Matt Mackall3c0547b2005-05-16 21:53:47 -07001816 *
1817 * The last preferred console added will be used for kernel messages
1818 * and stdin/out/err for init. Normally this is used by console_setup
1819 * above to handle user-supplied console arguments; however it can also
1820 * be used by arch-specific code either to override the user or more
1821 * commonly to provide a default console (ie from PROM variables) when
1822 * the user has not supplied one.
1823 */
David S. Millerfb445ee2007-12-29 01:19:49 -08001824int add_preferred_console(char *name, int idx, char *options)
Matt Mackall3c0547b2005-05-16 21:53:47 -07001825{
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001826 return __add_preferred_console(name, idx, options, NULL);
Matt Mackall3c0547b2005-05-16 21:53:47 -07001827}
1828
Daniel Ritzb6b1d872007-08-03 16:07:43 +02001829int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
Yinghai Lu18a8bd92007-07-15 23:37:59 -07001830{
1831 struct console_cmdline *c;
1832 int i;
1833
1834 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
1835 if (strcmp(console_cmdline[i].name, name) == 0 &&
1836 console_cmdline[i].index == idx) {
1837 c = &console_cmdline[i];
Markus Armbrusterf7352952008-04-30 00:54:52 -07001838 strlcpy(c->name, name_new, sizeof(c->name));
Yinghai Lu18a8bd92007-07-15 23:37:59 -07001839 c->name[sizeof(c->name) - 1] = 0;
1840 c->options = options;
1841 c->index = idx_new;
1842 return i;
1843 }
1844 /* not found */
1845 return -1;
1846}
1847
Rusty Russell2329abf2012-01-13 09:32:18 +10301848bool console_suspend_enabled = 1;
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001849EXPORT_SYMBOL(console_suspend_enabled);
1850
1851static int __init console_suspend_disable(char *str)
1852{
1853 console_suspend_enabled = 0;
1854 return 1;
1855}
1856__setup("no_console_suspend", console_suspend_disable);
Yanmin Zhang134620f2011-10-31 17:11:27 -07001857module_param_named(console_suspend, console_suspend_enabled,
1858 bool, S_IRUGO | S_IWUSR);
1859MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
1860 " and hibernate operations");
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001861
Matt Mackall3c0547b2005-05-16 21:53:47 -07001862/**
Linus Torvalds557240b2006-06-19 18:16:01 -07001863 * suspend_console - suspend the console subsystem
1864 *
1865 * This disables printk() while we go into suspend states
1866 */
1867void suspend_console(void)
1868{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001869 if (!console_suspend_enabled)
1870 return;
Pavel Machek0d630812008-07-23 21:28:32 -07001871 printk("Suspending console(s) (use no_console_suspend to debug)\n");
Torben Hohnac751ef2011-01-25 15:07:35 -08001872 console_lock();
Linus Torvalds557240b2006-06-19 18:16:01 -07001873 console_suspended = 1;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001874 up(&console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -07001875}
1876
1877void resume_console(void)
1878{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001879 if (!console_suspend_enabled)
1880 return;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001881 down(&console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -07001882 console_suspended = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08001883 console_unlock();
Linus Torvalds557240b2006-06-19 18:16:01 -07001884}
1885
1886/**
Kevin Cernekee034260d2010-06-03 22:11:25 -07001887 * console_cpu_notify - print deferred console messages after CPU hotplug
1888 * @self: notifier struct
1889 * @action: CPU hotplug event
1890 * @hcpu: unused
1891 *
1892 * If printk() is called from a CPU that is not online yet, the messages
1893 * will be spooled but will not show up on the console. This function is
1894 * called when a new CPU comes online (or fails to come up), and ensures
1895 * that any such output gets printed.
1896 */
1897static int __cpuinit console_cpu_notify(struct notifier_block *self,
1898 unsigned long action, void *hcpu)
1899{
1900 switch (action) {
1901 case CPU_ONLINE:
1902 case CPU_DEAD:
Kevin Cernekee034260d2010-06-03 22:11:25 -07001903 case CPU_DOWN_FAILED:
1904 case CPU_UP_CANCELED:
Torben Hohnac751ef2011-01-25 15:07:35 -08001905 console_lock();
1906 console_unlock();
Kevin Cernekee034260d2010-06-03 22:11:25 -07001907 }
1908 return NOTIFY_OK;
1909}
1910
1911/**
Torben Hohnac751ef2011-01-25 15:07:35 -08001912 * console_lock - lock the console system for exclusive use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 *
Torben Hohnac751ef2011-01-25 15:07:35 -08001914 * Acquires a lock which guarantees that the caller has
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 * exclusive access to the console system and the console_drivers list.
1916 *
1917 * Can sleep, returns nothing.
1918 */
Torben Hohnac751ef2011-01-25 15:07:35 -08001919void console_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
Daniel Vetter6b898c02012-09-17 23:03:31 +00001921 might_sleep();
1922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 down(&console_sem);
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001924 if (console_suspended)
1925 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 console_locked = 1;
1927 console_may_schedule = 1;
Daniel Vetterdaee7792012-09-22 19:52:11 +02001928 mutex_acquire(&console_lock_dep_map, 0, 0, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
Torben Hohnac751ef2011-01-25 15:07:35 -08001930EXPORT_SYMBOL(console_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Torben Hohnac751ef2011-01-25 15:07:35 -08001932/**
1933 * console_trylock - try to lock the console system for exclusive use.
1934 *
1935 * Tried to acquire a lock which guarantees that the caller has
1936 * exclusive access to the console system and the console_drivers list.
1937 *
1938 * returns 1 on success, and 0 on failure to acquire the lock.
1939 */
1940int console_trylock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
1942 if (down_trylock(&console_sem))
Torben Hohnac751ef2011-01-25 15:07:35 -08001943 return 0;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001944 if (console_suspended) {
1945 up(&console_sem);
Torben Hohnac751ef2011-01-25 15:07:35 -08001946 return 0;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 console_locked = 1;
1949 console_may_schedule = 0;
Daniel Vetterdaee7792012-09-22 19:52:11 +02001950 mutex_acquire(&console_lock_dep_map, 0, 1, _RET_IP_);
Torben Hohnac751ef2011-01-25 15:07:35 -08001951 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952}
Torben Hohnac751ef2011-01-25 15:07:35 -08001953EXPORT_SYMBOL(console_trylock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955int is_console_locked(void)
1956{
1957 return console_locked;
1958}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +01001960/*
Kay Sievers7ff95542012-05-03 02:29:13 +02001961 * Delayed printk version, for scheduler-internal messages:
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +01001962 */
1963#define PRINTK_BUF_SIZE 512
1964
1965#define PRINTK_PENDING_WAKEUP 0x01
1966#define PRINTK_PENDING_SCHED 0x02
1967
Peter Zijlstrab845b512008-08-08 21:47:09 +02001968static DEFINE_PER_CPU(int, printk_pending);
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +01001969static DEFINE_PER_CPU(char [PRINTK_BUF_SIZE], printk_sched_buf);
Peter Zijlstrab845b512008-08-08 21:47:09 +02001970
Frederic Weisbecker74876a92012-10-12 18:00:23 +02001971static void wake_up_klogd_work_func(struct irq_work *irq_work)
Peter Zijlstrab845b512008-08-08 21:47:09 +02001972{
Frederic Weisbecker74876a92012-10-12 18:00:23 +02001973 int pending = __this_cpu_xchg(printk_pending, 0);
1974
1975 if (pending & PRINTK_PENDING_SCHED) {
1976 char *buf = __get_cpu_var(printk_sched_buf);
1977 printk(KERN_WARNING "[sched_delayed] %s", buf);
Peter Zijlstrab845b512008-08-08 21:47:09 +02001978 }
Frederic Weisbecker74876a92012-10-12 18:00:23 +02001979
1980 if (pending & PRINTK_PENDING_WAKEUP)
1981 wake_up_interruptible(&log_wait);
Peter Zijlstrab845b512008-08-08 21:47:09 +02001982}
1983
Frederic Weisbecker74876a92012-10-12 18:00:23 +02001984static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) = {
1985 .func = wake_up_klogd_work_func,
1986 .flags = IRQ_WORK_LAZY,
1987};
Peter Zijlstrab845b512008-08-08 21:47:09 +02001988
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08001989void wake_up_klogd(void)
1990{
Frederic Weisbecker74876a92012-10-12 18:00:23 +02001991 preempt_disable();
1992 if (waitqueue_active(&log_wait)) {
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +01001993 this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP);
Frederic Weisbecker74876a92012-10-12 18:00:23 +02001994 irq_work_queue(&__get_cpu_var(wake_up_klogd_work));
1995 }
1996 preempt_enable();
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08001997}
1998
Kay Sieverseab07262012-07-16 18:35:30 -07001999static void console_cont_flush(char *text, size_t size)
2000{
2001 unsigned long flags;
2002 size_t len;
2003
2004 raw_spin_lock_irqsave(&logbuf_lock, flags);
2005
2006 if (!cont.len)
2007 goto out;
2008
2009 /*
2010 * We still queue earlier records, likely because the console was
2011 * busy. The earlier ones need to be printed before this one, we
2012 * did not flush any fragment so far, so just let it queue up.
2013 */
2014 if (console_seq < log_next_seq && !cont.cons)
2015 goto out;
2016
2017 len = cont_print_text(text, size);
2018 raw_spin_unlock(&logbuf_lock);
2019 stop_critical_timings();
2020 call_console_drivers(cont.level, text, len);
2021 start_critical_timings();
2022 local_irq_restore(flags);
2023 return;
2024out:
2025 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2026}
Kay Sievers7ff95542012-05-03 02:29:13 +02002027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028/**
Torben Hohnac751ef2011-01-25 15:07:35 -08002029 * console_unlock - unlock the console system
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002031 * Releases the console_lock which the caller holds on the console system
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 * and the console driver list.
2033 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002034 * While the console_lock was held, console output may have been buffered
2035 * by printk(). If this is the case, console_unlock(); emits
2036 * the output prior to releasing the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 *
Kay Sievers7f3a7812012-05-09 01:37:51 +02002038 * If there is output waiting, we wake /dev/kmsg and syslog() users.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002040 * console_unlock(); may be called from any context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 */
Torben Hohnac751ef2011-01-25 15:07:35 -08002042void console_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Kay Sievers70498252012-07-16 18:35:29 -07002044 static char text[LOG_LINE_MAX + PREFIX_MAX];
Kay Sievers7ff95542012-05-03 02:29:13 +02002045 static u64 seen_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 unsigned long flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02002047 bool wake_klogd = false;
2048 bool retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Linus Torvalds557240b2006-06-19 18:16:01 -07002050 if (console_suspended) {
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01002051 up(&console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -07002052 return;
2053 }
Antonino A. Daplas78944e52006-08-05 12:14:16 -07002054
2055 console_may_schedule = 0;
2056
Kay Sievers084681d2012-06-28 09:38:53 +02002057 /* flush buffered message fragment immediately to console */
Kay Sieverseab07262012-07-16 18:35:30 -07002058 console_cont_flush(text, sizeof(text));
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002059again:
Kay Sievers7ff95542012-05-03 02:29:13 +02002060 for (;;) {
2061 struct log *msg;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02002062 size_t len;
Kay Sievers7ff95542012-05-03 02:29:13 +02002063 int level;
2064
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002065 raw_spin_lock_irqsave(&logbuf_lock, flags);
Kay Sievers7ff95542012-05-03 02:29:13 +02002066 if (seen_seq != log_next_seq) {
2067 wake_klogd = true;
2068 seen_seq = log_next_seq;
2069 }
2070
2071 if (console_seq < log_first_seq) {
2072 /* messages are gone, move to first one */
2073 console_seq = log_first_seq;
2074 console_idx = log_first_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002075 console_prev = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02002076 }
Kay Sievers084681d2012-06-28 09:38:53 +02002077skip:
Kay Sievers7ff95542012-05-03 02:29:13 +02002078 if (console_seq == log_next_seq)
2079 break;
2080
2081 msg = log_from_idx(console_idx);
Kay Sievers084681d2012-06-28 09:38:53 +02002082 if (msg->flags & LOG_NOCONS) {
2083 /*
2084 * Skip record we have buffered and already printed
2085 * directly to the console when we received it.
2086 */
2087 console_idx = log_next(console_idx);
2088 console_seq++;
Kay Sievers68b65072012-07-06 09:50:09 -07002089 /*
2090 * We will get here again when we register a new
2091 * CON_PRINTBUFFER console. Clear the flag so we
2092 * will properly dump everything later.
2093 */
2094 msg->flags &= ~LOG_NOCONS;
Kay Sieverseab07262012-07-16 18:35:30 -07002095 console_prev = msg->flags;
Kay Sievers084681d2012-06-28 09:38:53 +02002096 goto skip;
2097 }
Kay Sievers649e6ee2012-05-10 04:30:45 +02002098
Kay Sievers084681d2012-06-28 09:38:53 +02002099 level = msg->level;
Kay Sievers5becfb12012-07-09 12:15:42 -07002100 len = msg_print_text(msg, console_prev, false,
2101 text, sizeof(text));
Kay Sievers7ff95542012-05-03 02:29:13 +02002102 console_idx = log_next(console_idx);
2103 console_seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07002104 console_prev = msg->flags;
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002105 raw_spin_unlock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02002106
Steven Rostedt81d68a92008-05-12 21:20:42 +02002107 stop_critical_timings(); /* don't trace print latency */
Kay Sievers7ff95542012-05-03 02:29:13 +02002108 call_console_drivers(level, text, len);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002109 start_critical_timings();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 local_irq_restore(flags);
2111 }
2112 console_locked = 0;
Daniel Vetterdaee7792012-09-22 19:52:11 +02002113 mutex_release(&console_lock_dep_map, 1, _RET_IP_);
Feng Tangfe3d8ad2011-03-22 16:34:21 -07002114
2115 /* Release the exclusive_console once it is used */
2116 if (unlikely(exclusive_console))
2117 exclusive_console = NULL;
2118
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002119 raw_spin_unlock(&logbuf_lock);
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002120
Peter Zijlstra0b5e1c52011-06-07 11:15:33 +02002121 up(&console_sem);
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002122
2123 /*
2124 * Someone could have filled up the buffer again, so re-check if there's
2125 * something to flush. In case we cannot trylock the console_sem again,
2126 * there's a new owner and the console_unlock() from them will do the
2127 * flush, no worries.
2128 */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002129 raw_spin_lock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02002130 retry = console_seq != log_next_seq;
Peter Zijlstra09dc3cf2011-12-08 14:34:13 -08002131 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2132
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002133 if (retry && console_trylock())
2134 goto again;
2135
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08002136 if (wake_klogd)
2137 wake_up_klogd();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138}
Torben Hohnac751ef2011-01-25 15:07:35 -08002139EXPORT_SYMBOL(console_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Martin Waitzddad86c2005-11-13 16:08:14 -08002141/**
2142 * console_conditional_schedule - yield the CPU if required
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 *
2144 * If the console code is currently allowed to sleep, and
2145 * if this CPU should yield the CPU to another task, do
2146 * so here.
2147 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002148 * Must be called within console_lock();.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 */
2150void __sched console_conditional_schedule(void)
2151{
2152 if (console_may_schedule)
2153 cond_resched();
2154}
2155EXPORT_SYMBOL(console_conditional_schedule);
2156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157void console_unblank(void)
2158{
2159 struct console *c;
2160
2161 /*
2162 * console_unblank can no longer be called in interrupt context unless
2163 * oops_in_progress is set to 1..
2164 */
2165 if (oops_in_progress) {
2166 if (down_trylock(&console_sem) != 0)
2167 return;
2168 } else
Torben Hohnac751ef2011-01-25 15:07:35 -08002169 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
2171 console_locked = 1;
2172 console_may_schedule = 0;
Robin Getz4d091612009-07-01 21:08:37 -04002173 for_each_console(c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 if ((c->flags & CON_ENABLED) && c->unblank)
2175 c->unblank();
Torben Hohnac751ef2011-01-25 15:07:35 -08002176 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179/*
2180 * Return the console tty driver structure and its associated index
2181 */
2182struct tty_driver *console_device(int *index)
2183{
2184 struct console *c;
2185 struct tty_driver *driver = NULL;
2186
Torben Hohnac751ef2011-01-25 15:07:35 -08002187 console_lock();
Robin Getz4d091612009-07-01 21:08:37 -04002188 for_each_console(c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 if (!c->device)
2190 continue;
2191 driver = c->device(c, index);
2192 if (driver)
2193 break;
2194 }
Torben Hohnac751ef2011-01-25 15:07:35 -08002195 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 return driver;
2197}
2198
2199/*
2200 * Prevent further output on the passed console device so that (for example)
2201 * serial drivers can disable console output before suspending a port, and can
2202 * re-enable output afterwards.
2203 */
2204void console_stop(struct console *console)
2205{
Torben Hohnac751ef2011-01-25 15:07:35 -08002206 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 console->flags &= ~CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08002208 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209}
2210EXPORT_SYMBOL(console_stop);
2211
2212void console_start(struct console *console)
2213{
Torben Hohnac751ef2011-01-25 15:07:35 -08002214 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 console->flags |= CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08002216 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217}
2218EXPORT_SYMBOL(console_start);
2219
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07002220static int __read_mostly keep_bootcon;
2221
2222static int __init keep_bootcon_setup(char *str)
2223{
2224 keep_bootcon = 1;
2225 printk(KERN_INFO "debug: skip boot console de-registration.\n");
2226
2227 return 0;
2228}
2229
2230early_param("keep_bootcon", keep_bootcon_setup);
2231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232/*
2233 * The console driver calls this routine during kernel initialization
2234 * to register the console printing procedure with printk() and to
2235 * print any messages that were printed by the kernel before the
2236 * console driver was initialized.
Robin Getz4d091612009-07-01 21:08:37 -04002237 *
2238 * This can happen pretty early during the boot process (because of
2239 * early_printk) - sometimes before setup_arch() completes - be careful
2240 * of what kernel features are used - they may not be initialised yet.
2241 *
2242 * There are two types of consoles - bootconsoles (early_printk) and
2243 * "real" consoles (everything which is not a bootconsole) which are
2244 * handled differently.
2245 * - Any number of bootconsoles can be registered at any time.
2246 * - As soon as a "real" console is registered, all bootconsoles
2247 * will be unregistered automatically.
2248 * - Once a "real" console is registered, any attempt to register a
2249 * bootconsoles will be rejected
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 */
Robin Getz4d091612009-07-01 21:08:37 -04002251void register_console(struct console *newcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
Jesper Juhl40dc5652005-10-30 15:02:46 -08002253 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 unsigned long flags;
Robin Getz4d091612009-07-01 21:08:37 -04002255 struct console *bcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Robin Getz4d091612009-07-01 21:08:37 -04002257 /*
2258 * before we register a new CON_BOOT console, make sure we don't
2259 * already have a valid console
2260 */
2261 if (console_drivers && newcon->flags & CON_BOOT) {
2262 /* find the last or real console */
2263 for_each_console(bcon) {
2264 if (!(bcon->flags & CON_BOOT)) {
2265 printk(KERN_INFO "Too late to register bootconsole %s%d\n",
2266 newcon->name, newcon->index);
2267 return;
2268 }
2269 }
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002270 }
2271
Robin Getz4d091612009-07-01 21:08:37 -04002272 if (console_drivers && console_drivers->flags & CON_BOOT)
2273 bcon = console_drivers;
2274
2275 if (preferred_console < 0 || bcon || !console_drivers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 preferred_console = selected_console;
2277
Robin Getz4d091612009-07-01 21:08:37 -04002278 if (newcon->early_setup)
2279 newcon->early_setup();
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 /*
2282 * See if we want to use this console driver. If we
2283 * didn't select a console we take the first one
2284 * that registers here.
2285 */
2286 if (preferred_console < 0) {
Robin Getz4d091612009-07-01 21:08:37 -04002287 if (newcon->index < 0)
2288 newcon->index = 0;
2289 if (newcon->setup == NULL ||
2290 newcon->setup(newcon, NULL) == 0) {
2291 newcon->flags |= CON_ENABLED;
2292 if (newcon->device) {
2293 newcon->flags |= CON_CONSDEV;
Jan Kiszkacd3a1b82008-05-12 21:21:04 +02002294 preferred_console = 0;
2295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 }
2297 }
2298
2299 /*
2300 * See if this console matches one we selected on
2301 * the command line.
2302 */
Jesper Juhl40dc5652005-10-30 15:02:46 -08002303 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
2304 i++) {
Robin Getz4d091612009-07-01 21:08:37 -04002305 if (strcmp(console_cmdline[i].name, newcon->name) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 continue;
Robin Getz4d091612009-07-01 21:08:37 -04002307 if (newcon->index >= 0 &&
2308 newcon->index != console_cmdline[i].index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 continue;
Robin Getz4d091612009-07-01 21:08:37 -04002310 if (newcon->index < 0)
2311 newcon->index = console_cmdline[i].index;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002312#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
2313 if (console_cmdline[i].brl_options) {
Robin Getz4d091612009-07-01 21:08:37 -04002314 newcon->flags |= CON_BRL;
2315 braille_register_console(newcon,
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002316 console_cmdline[i].index,
2317 console_cmdline[i].options,
2318 console_cmdline[i].brl_options);
2319 return;
2320 }
2321#endif
Robin Getz4d091612009-07-01 21:08:37 -04002322 if (newcon->setup &&
2323 newcon->setup(newcon, console_cmdline[i].options) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 break;
Robin Getz4d091612009-07-01 21:08:37 -04002325 newcon->flags |= CON_ENABLED;
2326 newcon->index = console_cmdline[i].index;
Greg Edwardsab4af032005-06-23 00:09:05 -07002327 if (i == selected_console) {
Robin Getz4d091612009-07-01 21:08:37 -04002328 newcon->flags |= CON_CONSDEV;
Greg Edwardsab4af032005-06-23 00:09:05 -07002329 preferred_console = selected_console;
2330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 break;
2332 }
2333
Robin Getz4d091612009-07-01 21:08:37 -04002334 if (!(newcon->flags & CON_ENABLED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 return;
2336
Robin Getz8259cf42009-07-09 13:08:37 -04002337 /*
2338 * If we have a bootconsole, and are switching to a real console,
2339 * don't print everything out again, since when the boot console, and
2340 * the real console are the same physical device, it's annoying to
2341 * see the beginning boot messages twice
2342 */
2343 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
Robin Getz4d091612009-07-01 21:08:37 -04002344 newcon->flags &= ~CON_PRINTBUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
2346 /*
2347 * Put this console in the list - keep the
2348 * preferred driver at the head of the list.
2349 */
Torben Hohnac751ef2011-01-25 15:07:35 -08002350 console_lock();
Robin Getz4d091612009-07-01 21:08:37 -04002351 if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
2352 newcon->next = console_drivers;
2353 console_drivers = newcon;
2354 if (newcon->next)
2355 newcon->next->flags &= ~CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 } else {
Robin Getz4d091612009-07-01 21:08:37 -04002357 newcon->next = console_drivers->next;
2358 console_drivers->next = newcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 }
Robin Getz4d091612009-07-01 21:08:37 -04002360 if (newcon->flags & CON_PRINTBUFFER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 /*
Torben Hohnac751ef2011-01-25 15:07:35 -08002362 * console_unlock(); will print out the buffered messages
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 * for us.
2364 */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002365 raw_spin_lock_irqsave(&logbuf_lock, flags);
Kay Sievers7ff95542012-05-03 02:29:13 +02002366 console_seq = syslog_seq;
2367 console_idx = syslog_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002368 console_prev = syslog_prev;
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002369 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
Feng Tangfe3d8ad2011-03-22 16:34:21 -07002370 /*
2371 * We're about to replay the log buffer. Only do this to the
2372 * just-registered console to avoid excessive message spam to
2373 * the already-registered consoles.
2374 */
2375 exclusive_console = newcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 }
Torben Hohnac751ef2011-01-25 15:07:35 -08002377 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01002378 console_sysfs_notify();
Robin Getz8259cf42009-07-09 13:08:37 -04002379
2380 /*
2381 * By unregistering the bootconsoles after we enable the real console
2382 * we get the "console xxx enabled" message on all the consoles -
2383 * boot consoles, real consoles, etc - this is to ensure that end
2384 * users know there might be something in the kernel's log buffer that
2385 * went to the bootconsole (that they do not see on the real console)
2386 */
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07002387 if (bcon &&
2388 ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
2389 !keep_bootcon) {
Robin Getz8259cf42009-07-09 13:08:37 -04002390 /* we need to iterate through twice, to make sure we print
2391 * everything out, before we unregister the console(s)
2392 */
2393 printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
2394 newcon->name, newcon->index);
2395 for_each_console(bcon)
2396 if (bcon->flags & CON_BOOT)
2397 unregister_console(bcon);
2398 } else {
2399 printk(KERN_INFO "%sconsole [%s%d] enabled\n",
2400 (newcon->flags & CON_BOOT) ? "boot" : "" ,
2401 newcon->name, newcon->index);
2402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403}
2404EXPORT_SYMBOL(register_console);
2405
Jesper Juhl40dc5652005-10-30 15:02:46 -08002406int unregister_console(struct console *console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407{
Jesper Juhl40dc5652005-10-30 15:02:46 -08002408 struct console *a, *b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 int res = 1;
2410
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002411#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
2412 if (console->flags & CON_BRL)
2413 return braille_unregister_console(console);
2414#endif
2415
Torben Hohnac751ef2011-01-25 15:07:35 -08002416 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 if (console_drivers == console) {
2418 console_drivers=console->next;
2419 res = 0;
Benjamin Herrenschmidte9b15b52005-11-23 13:37:44 -08002420 } else if (console_drivers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 for (a=console_drivers->next, b=console_drivers ;
2422 a; b=a, a=b->next) {
2423 if (a == console) {
2424 b->next = a->next;
2425 res = 0;
2426 break;
Jesper Juhl40dc5652005-10-30 15:02:46 -08002427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 }
2429 }
Jesper Juhl40dc5652005-10-30 15:02:46 -08002430
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002431 /*
Greg Edwardsab4af032005-06-23 00:09:05 -07002432 * If this isn't the last console and it has CON_CONSDEV set, we
2433 * need to set it on the next preferred console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 */
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002435 if (console_drivers != NULL && console->flags & CON_CONSDEV)
Greg Edwardsab4af032005-06-23 00:09:05 -07002436 console_drivers->flags |= CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Torben Hohnac751ef2011-01-25 15:07:35 -08002438 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01002439 console_sysfs_notify();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 return res;
2441}
2442EXPORT_SYMBOL(unregister_console);
Matt Mackalld59745c2005-05-01 08:59:02 -07002443
Kevin Cernekee034260d2010-06-03 22:11:25 -07002444static int __init printk_late_init(void)
Robin Getz0c5564b2007-08-20 15:22:47 -04002445{
Robin Getz4d091612009-07-01 21:08:37 -04002446 struct console *con;
2447
2448 for_each_console(con) {
Nishanth Aravamudan4c30c6f2011-08-25 15:59:11 -07002449 if (!keep_bootcon && con->flags & CON_BOOT) {
Robin Getzcb00e992007-08-21 23:14:58 -04002450 printk(KERN_INFO "turn off boot console %s%d\n",
Robin Getz4d091612009-07-01 21:08:37 -04002451 con->name, con->index);
Sonic Zhang42c2c8c2009-08-06 15:58:11 -07002452 unregister_console(con);
Robin Getzcb00e992007-08-21 23:14:58 -04002453 }
Robin Getz0c5564b2007-08-20 15:22:47 -04002454 }
Kevin Cernekee034260d2010-06-03 22:11:25 -07002455 hotcpu_notifier(console_cpu_notify, 0);
Robin Getz0c5564b2007-08-20 15:22:47 -04002456 return 0;
2457}
Kevin Cernekee034260d2010-06-03 22:11:25 -07002458late_initcall(printk_late_init);
Robin Getz0c5564b2007-08-20 15:22:47 -04002459
Joe Perches7ef3d2f2008-02-08 04:21:25 -08002460#if defined CONFIG_PRINTK
Dave Young717115e2008-07-25 01:45:58 -07002461
Peter Zijlstra600e1452012-03-15 12:35:37 +01002462int printk_sched(const char *fmt, ...)
2463{
2464 unsigned long flags;
2465 va_list args;
2466 char *buf;
2467 int r;
2468
2469 local_irq_save(flags);
2470 buf = __get_cpu_var(printk_sched_buf);
2471
2472 va_start(args, fmt);
2473 r = vsnprintf(buf, PRINTK_BUF_SIZE, fmt, args);
2474 va_end(args);
2475
2476 __this_cpu_or(printk_pending, PRINTK_PENDING_SCHED);
Frederic Weisbecker74876a92012-10-12 18:00:23 +02002477 irq_work_queue(&__get_cpu_var(wake_up_klogd_work));
Peter Zijlstra600e1452012-03-15 12:35:37 +01002478 local_irq_restore(flags);
2479
2480 return r;
2481}
2482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483/*
2484 * printk rate limiting, lifted from the networking subsystem.
2485 *
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07002486 * This enforces a rate limit: not more than 10 kernel messages
2487 * every 5s to make a denial-of-service attack impossible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 */
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07002489DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
2490
Christian Borntraeger5c828712009-10-23 14:58:11 +02002491int __printk_ratelimit(const char *func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492{
Christian Borntraeger5c828712009-10-23 14:58:11 +02002493 return ___ratelimit(&printk_ratelimit_state, func);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494}
Christian Borntraeger5c828712009-10-23 14:58:11 +02002495EXPORT_SYMBOL(__printk_ratelimit);
Andrew Mortonf46c4832006-11-02 22:07:16 -08002496
2497/**
2498 * printk_timed_ratelimit - caller-controlled printk ratelimiting
2499 * @caller_jiffies: pointer to caller's state
2500 * @interval_msecs: minimum interval between prints
2501 *
2502 * printk_timed_ratelimit() returns true if more than @interval_msecs
2503 * milliseconds have elapsed since the last time printk_timed_ratelimit()
2504 * returned true.
2505 */
2506bool printk_timed_ratelimit(unsigned long *caller_jiffies,
2507 unsigned int interval_msecs)
2508{
Guillaume Knispelf2d28a22009-03-17 16:18:42 +01002509 if (*caller_jiffies == 0
2510 || !time_in_range(jiffies, *caller_jiffies,
2511 *caller_jiffies
2512 + msecs_to_jiffies(interval_msecs))) {
2513 *caller_jiffies = jiffies;
Andrew Mortonf46c4832006-11-02 22:07:16 -08002514 return true;
2515 }
2516 return false;
2517}
2518EXPORT_SYMBOL(printk_timed_ratelimit);
Simon Kagstrom456b5652009-10-16 14:09:18 +02002519
2520static DEFINE_SPINLOCK(dump_list_lock);
2521static LIST_HEAD(dump_list);
2522
2523/**
2524 * kmsg_dump_register - register a kernel log dumper.
Randy Dunlap64855362009-12-17 15:27:27 -08002525 * @dumper: pointer to the kmsg_dumper structure
Simon Kagstrom456b5652009-10-16 14:09:18 +02002526 *
2527 * Adds a kernel log dumper to the system. The dump callback in the
2528 * structure will be called when the kernel oopses or panics and must be
2529 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
2530 */
2531int kmsg_dump_register(struct kmsg_dumper *dumper)
2532{
2533 unsigned long flags;
2534 int err = -EBUSY;
2535
2536 /* The dump callback needs to be set */
2537 if (!dumper->dump)
2538 return -EINVAL;
2539
2540 spin_lock_irqsave(&dump_list_lock, flags);
2541 /* Don't allow registering multiple times */
2542 if (!dumper->registered) {
2543 dumper->registered = 1;
Huang Yingfb842b02011-01-12 16:59:43 -08002544 list_add_tail_rcu(&dumper->list, &dump_list);
Simon Kagstrom456b5652009-10-16 14:09:18 +02002545 err = 0;
2546 }
2547 spin_unlock_irqrestore(&dump_list_lock, flags);
2548
2549 return err;
2550}
2551EXPORT_SYMBOL_GPL(kmsg_dump_register);
2552
2553/**
2554 * kmsg_dump_unregister - unregister a kmsg dumper.
Randy Dunlap64855362009-12-17 15:27:27 -08002555 * @dumper: pointer to the kmsg_dumper structure
Simon Kagstrom456b5652009-10-16 14:09:18 +02002556 *
2557 * Removes a dump device from the system. Returns zero on success and
2558 * %-EINVAL otherwise.
2559 */
2560int kmsg_dump_unregister(struct kmsg_dumper *dumper)
2561{
2562 unsigned long flags;
2563 int err = -EINVAL;
2564
2565 spin_lock_irqsave(&dump_list_lock, flags);
2566 if (dumper->registered) {
2567 dumper->registered = 0;
Huang Yingfb842b02011-01-12 16:59:43 -08002568 list_del_rcu(&dumper->list);
Simon Kagstrom456b5652009-10-16 14:09:18 +02002569 err = 0;
2570 }
2571 spin_unlock_irqrestore(&dump_list_lock, flags);
Huang Yingfb842b02011-01-12 16:59:43 -08002572 synchronize_rcu();
Simon Kagstrom456b5652009-10-16 14:09:18 +02002573
2574 return err;
2575}
2576EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
2577
Kay Sievers7ff95542012-05-03 02:29:13 +02002578static bool always_kmsg_dump;
2579module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR);
2580
Simon Kagstrom456b5652009-10-16 14:09:18 +02002581/**
2582 * kmsg_dump - dump kernel log to kernel message dumpers.
2583 * @reason: the reason (oops, panic etc) for dumping
2584 *
Kay Sieverse2ae7152012-06-15 14:07:51 +02002585 * Call each of the registered dumper's dump() callback, which can
2586 * retrieve the kmsg records with kmsg_dump_get_line() or
2587 * kmsg_dump_get_buffer().
Simon Kagstrom456b5652009-10-16 14:09:18 +02002588 */
2589void kmsg_dump(enum kmsg_dump_reason reason)
2590{
Simon Kagstrom456b5652009-10-16 14:09:18 +02002591 struct kmsg_dumper *dumper;
Simon Kagstrom456b5652009-10-16 14:09:18 +02002592 unsigned long flags;
2593
Matthew Garrettc22ab332012-03-05 14:59:10 -08002594 if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
2595 return;
2596
Huang Yingfb842b02011-01-12 16:59:43 -08002597 rcu_read_lock();
Kay Sieverse2ae7152012-06-15 14:07:51 +02002598 list_for_each_entry_rcu(dumper, &dump_list, list) {
2599 if (dumper->max_reason && reason > dumper->max_reason)
2600 continue;
2601
2602 /* initialize iterator with data about the stored records */
2603 dumper->active = true;
2604
2605 raw_spin_lock_irqsave(&logbuf_lock, flags);
2606 dumper->cur_seq = clear_seq;
2607 dumper->cur_idx = clear_idx;
2608 dumper->next_seq = log_next_seq;
2609 dumper->next_idx = log_next_idx;
2610 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2611
2612 /* invoke dumper which will iterate over records */
2613 dumper->dump(dumper, reason);
2614
2615 /* reset iterator */
2616 dumper->active = false;
2617 }
Huang Yingfb842b02011-01-12 16:59:43 -08002618 rcu_read_unlock();
Simon Kagstrom456b5652009-10-16 14:09:18 +02002619}
Kay Sieverse2ae7152012-06-15 14:07:51 +02002620
2621/**
Anton Vorontsov533827c2012-07-20 17:28:07 -07002622 * kmsg_dump_get_line_nolock - retrieve one kmsg log line (unlocked version)
2623 * @dumper: registered kmsg dumper
2624 * @syslog: include the "<4>" prefixes
2625 * @line: buffer to copy the line to
2626 * @size: maximum size of the buffer
2627 * @len: length of line placed into buffer
2628 *
2629 * Start at the beginning of the kmsg buffer, with the oldest kmsg
2630 * record, and copy one record into the provided buffer.
2631 *
2632 * Consecutive calls will return the next available record moving
2633 * towards the end of the buffer with the youngest messages.
2634 *
2635 * A return value of FALSE indicates that there are no more records to
2636 * read.
2637 *
2638 * The function is similar to kmsg_dump_get_line(), but grabs no locks.
2639 */
2640bool kmsg_dump_get_line_nolock(struct kmsg_dumper *dumper, bool syslog,
2641 char *line, size_t size, size_t *len)
2642{
2643 struct log *msg;
2644 size_t l = 0;
2645 bool ret = false;
2646
2647 if (!dumper->active)
2648 goto out;
2649
2650 if (dumper->cur_seq < log_first_seq) {
2651 /* messages are gone, move to first available one */
2652 dumper->cur_seq = log_first_seq;
2653 dumper->cur_idx = log_first_idx;
2654 }
2655
2656 /* last entry */
2657 if (dumper->cur_seq >= log_next_seq)
2658 goto out;
2659
2660 msg = log_from_idx(dumper->cur_idx);
2661 l = msg_print_text(msg, 0, syslog, line, size);
2662
2663 dumper->cur_idx = log_next(dumper->cur_idx);
2664 dumper->cur_seq++;
2665 ret = true;
2666out:
2667 if (len)
2668 *len = l;
2669 return ret;
2670}
2671
2672/**
Kay Sieverse2ae7152012-06-15 14:07:51 +02002673 * kmsg_dump_get_line - retrieve one kmsg log line
2674 * @dumper: registered kmsg dumper
2675 * @syslog: include the "<4>" prefixes
2676 * @line: buffer to copy the line to
2677 * @size: maximum size of the buffer
2678 * @len: length of line placed into buffer
2679 *
2680 * Start at the beginning of the kmsg buffer, with the oldest kmsg
2681 * record, and copy one record into the provided buffer.
2682 *
2683 * Consecutive calls will return the next available record moving
2684 * towards the end of the buffer with the youngest messages.
2685 *
2686 * A return value of FALSE indicates that there are no more records to
2687 * read.
2688 */
2689bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog,
2690 char *line, size_t size, size_t *len)
2691{
2692 unsigned long flags;
Anton Vorontsov533827c2012-07-20 17:28:07 -07002693 bool ret;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002694
2695 raw_spin_lock_irqsave(&logbuf_lock, flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07002696 ret = kmsg_dump_get_line_nolock(dumper, syslog, line, size, len);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002697 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07002698
Kay Sieverse2ae7152012-06-15 14:07:51 +02002699 return ret;
2700}
2701EXPORT_SYMBOL_GPL(kmsg_dump_get_line);
2702
2703/**
2704 * kmsg_dump_get_buffer - copy kmsg log lines
2705 * @dumper: registered kmsg dumper
2706 * @syslog: include the "<4>" prefixes
Randy Dunlap4f0f4af2012-06-30 15:37:24 -07002707 * @buf: buffer to copy the line to
Kay Sieverse2ae7152012-06-15 14:07:51 +02002708 * @size: maximum size of the buffer
2709 * @len: length of line placed into buffer
2710 *
2711 * Start at the end of the kmsg buffer and fill the provided buffer
2712 * with as many of the the *youngest* kmsg records that fit into it.
2713 * If the buffer is large enough, all available kmsg records will be
2714 * copied with a single call.
2715 *
2716 * Consecutive calls will fill the buffer with the next block of
2717 * available older records, not including the earlier retrieved ones.
2718 *
2719 * A return value of FALSE indicates that there are no more records to
2720 * read.
2721 */
2722bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
2723 char *buf, size_t size, size_t *len)
2724{
2725 unsigned long flags;
2726 u64 seq;
2727 u32 idx;
2728 u64 next_seq;
2729 u32 next_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002730 enum log_flags prev;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002731 size_t l = 0;
2732 bool ret = false;
2733
2734 if (!dumper->active)
2735 goto out;
2736
2737 raw_spin_lock_irqsave(&logbuf_lock, flags);
2738 if (dumper->cur_seq < log_first_seq) {
2739 /* messages are gone, move to first available one */
2740 dumper->cur_seq = log_first_seq;
2741 dumper->cur_idx = log_first_idx;
2742 }
2743
2744 /* last entry */
2745 if (dumper->cur_seq >= dumper->next_seq) {
2746 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2747 goto out;
2748 }
2749
2750 /* calculate length of entire buffer */
2751 seq = dumper->cur_seq;
2752 idx = dumper->cur_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002753 prev = 0;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002754 while (seq < dumper->next_seq) {
2755 struct log *msg = log_from_idx(idx);
2756
Kay Sievers5becfb12012-07-09 12:15:42 -07002757 l += msg_print_text(msg, prev, true, NULL, 0);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002758 idx = log_next(idx);
2759 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07002760 prev = msg->flags;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002761 }
2762
2763 /* move first record forward until length fits into the buffer */
2764 seq = dumper->cur_seq;
2765 idx = dumper->cur_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002766 prev = 0;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002767 while (l > size && seq < dumper->next_seq) {
2768 struct log *msg = log_from_idx(idx);
2769
Kay Sievers5becfb12012-07-09 12:15:42 -07002770 l -= msg_print_text(msg, prev, true, NULL, 0);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002771 idx = log_next(idx);
2772 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07002773 prev = msg->flags;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002774 }
2775
2776 /* last message in next interation */
2777 next_seq = seq;
2778 next_idx = idx;
2779
2780 l = 0;
Kay Sievers5becfb12012-07-09 12:15:42 -07002781 prev = 0;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002782 while (seq < dumper->next_seq) {
2783 struct log *msg = log_from_idx(idx);
2784
Kay Sievers5becfb12012-07-09 12:15:42 -07002785 l += msg_print_text(msg, prev, syslog, buf + l, size - l);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002786 idx = log_next(idx);
2787 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07002788 prev = msg->flags;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002789 }
2790
2791 dumper->next_seq = next_seq;
2792 dumper->next_idx = next_idx;
2793 ret = true;
2794 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2795out:
2796 if (len)
2797 *len = l;
2798 return ret;
2799}
2800EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer);
2801
2802/**
Anton Vorontsov533827c2012-07-20 17:28:07 -07002803 * kmsg_dump_rewind_nolock - reset the interator (unlocked version)
2804 * @dumper: registered kmsg dumper
2805 *
2806 * Reset the dumper's iterator so that kmsg_dump_get_line() and
2807 * kmsg_dump_get_buffer() can be called again and used multiple
2808 * times within the same dumper.dump() callback.
2809 *
2810 * The function is similar to kmsg_dump_rewind(), but grabs no locks.
2811 */
2812void kmsg_dump_rewind_nolock(struct kmsg_dumper *dumper)
2813{
2814 dumper->cur_seq = clear_seq;
2815 dumper->cur_idx = clear_idx;
2816 dumper->next_seq = log_next_seq;
2817 dumper->next_idx = log_next_idx;
2818}
2819
2820/**
Kay Sieverse2ae7152012-06-15 14:07:51 +02002821 * kmsg_dump_rewind - reset the interator
2822 * @dumper: registered kmsg dumper
2823 *
2824 * Reset the dumper's iterator so that kmsg_dump_get_line() and
2825 * kmsg_dump_get_buffer() can be called again and used multiple
2826 * times within the same dumper.dump() callback.
2827 */
2828void kmsg_dump_rewind(struct kmsg_dumper *dumper)
2829{
2830 unsigned long flags;
2831
2832 raw_spin_lock_irqsave(&logbuf_lock, flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07002833 kmsg_dump_rewind_nolock(dumper);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002834 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2835}
2836EXPORT_SYMBOL_GPL(kmsg_dump_rewind);
Joe Perches7ef3d2f2008-02-08 04:21:25 -08002837#endif