blob: 852269adad25afc878c3f10e7a3bf059d086cca9 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include <asm/uaccess.h>
47
Johannes Berg95100352011-11-24 20:03:08 +010048#define CREATE_TRACE_POINTS
49#include <trace/events/printk.h>
50
Ingo Molnar076f9772008-01-30 13:33:06 +010051/*
52 * Architectures can override it:
53 */
Jiri Slabye17ba732008-05-12 15:44:40 +020054void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
Ingo Molnar076f9772008-01-30 13:33:06 +010055{
56}
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/* printk's without a loglevel use this.. */
Mandeep Singh Baines5af5bcb2011-03-22 16:34:23 -070059#define DEFAULT_MESSAGE_LOGLEVEL CONFIG_DEFAULT_MESSAGE_LOGLEVEL
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/* We show everything that is MORE important than this.. */
62#define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
63#define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
64
65DECLARE_WAIT_QUEUE_HEAD(log_wait);
66
67int console_printk[4] = {
68 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
69 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
70 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
71 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
72};
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/*
Patrick Pletscher0bbfb7c2007-02-17 20:10:16 +010075 * Low level drivers may need that to know if they can schedule in
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 * their unblank() callback or not. So let's export it.
77 */
78int oops_in_progress;
79EXPORT_SYMBOL(oops_in_progress);
80
81/*
82 * console_sem protects the console_drivers list, and also
83 * provides serialisation for access to the entire console
84 * driver system.
85 */
Thomas Gleixner5b8c4f22010-09-07 14:33:43 +000086static DEFINE_SEMAPHORE(console_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087struct console *console_drivers;
Ingo Molnara29d1cf2008-06-02 13:19:08 +020088EXPORT_SYMBOL_GPL(console_drivers);
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/*
91 * This is used for debugging the mess that is the VT code by
92 * keeping track if we have the console semaphore held. It's
93 * definitely not the perfect debug tool (we don't know if _WE_
94 * hold it are racing, but it helps tracking those weird code
95 * path in the console code where we end up in places I want
96 * locked without the console sempahore held
97 */
Linus Torvalds557240b2006-06-19 18:16:01 -070098static int console_locked, console_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100/*
Feng Tangfe3d8ad2011-03-22 16:34:21 -0700101 * If exclusive_console is non-NULL then only this console is to be printed to.
102 */
103static struct console *exclusive_console;
104
105/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * Array of consoles built from command line options (console=)
107 */
108struct console_cmdline
109{
110 char name[8]; /* Name of the driver */
111 int index; /* Minor dev. to use */
112 char *options; /* Options for the driver */
Samuel Thibaultf7511d52008-04-30 00:54:51 -0700113#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
114 char *brl_options; /* Options for braille driver */
115#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116};
117
118#define MAX_CMDLINECONSOLES 8
119
120static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
121static int selected_console = -1;
122static int preferred_console = -1;
Markus Armbruster9e124fe2008-05-26 23:31:07 +0100123int console_set_on_cmdline;
124EXPORT_SYMBOL(console_set_on_cmdline);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126/* Flag: console code may call schedule() */
127static int console_may_schedule;
128
Kay Sievers7ff95542012-05-03 02:29:13 +0200129/*
130 * The printk log buffer consists of a chain of concatenated variable
131 * length records. Every record starts with a record header, containing
132 * the overall length of the record.
133 *
134 * The heads to the first and last entry in the buffer, as well as the
135 * sequence numbers of these both entries are maintained when messages
136 * are stored..
137 *
138 * If the heads indicate available messages, the length in the header
139 * tells the start next message. A length == 0 for the next message
140 * indicates a wrap-around to the beginning of the buffer.
141 *
142 * Every record carries the monotonic timestamp in microseconds, as well as
143 * the standard userspace syslog level and syslog facility. The usual
144 * kernel messages use LOG_KERN; userspace-injected messages always carry
145 * a matching syslog facility, by default LOG_USER. The origin of every
146 * message can be reliably determined that way.
147 *
148 * The human readable log message directly follows the message header. The
149 * length of the message text is stored in the header, the stored message
150 * is not terminated.
151 *
Kay Sieverse11fea92012-05-03 02:29:41 +0200152 * Optionally, a message can carry a dictionary of properties (key/value pairs),
153 * to provide userspace with a machine-readable message context.
154 *
155 * Examples for well-defined, commonly used property names are:
156 * DEVICE=b12:8 device identifier
157 * b12:8 block dev_t
158 * c127:3 char dev_t
159 * n8 netdev ifindex
160 * +sound:card0 subsystem:devname
161 * SUBSYSTEM=pci driver-core subsystem name
162 *
163 * Valid characters in property names are [a-zA-Z0-9.-_]. The plain text value
164 * follows directly after a '=' character. Every property is terminated by
165 * a '\0' character. The last property is not terminated.
166 *
167 * Example of a message structure:
168 * 0000 ff 8f 00 00 00 00 00 00 monotonic time in nsec
169 * 0008 34 00 record is 52 bytes long
170 * 000a 0b 00 text is 11 bytes long
171 * 000c 1f 00 dictionary is 23 bytes long
172 * 000e 03 00 LOG_KERN (facility) LOG_ERR (level)
173 * 0010 69 74 27 73 20 61 20 6c "it's a l"
174 * 69 6e 65 "ine"
175 * 001b 44 45 56 49 43 "DEVIC"
176 * 45 3d 62 38 3a 32 00 44 "E=b8:2\0D"
177 * 52 49 56 45 52 3d 62 75 "RIVER=bu"
178 * 67 "g"
179 * 0032 00 00 00 padding to next message header
180 *
181 * The 'struct log' buffer header must never be directly exported to
182 * userspace, it is a kernel-private implementation detail that might
183 * need to be changed in the future, when the requirements change.
184 *
185 * /dev/kmsg exports the structured data in the following line format:
186 * "level,sequnum,timestamp;<message text>\n"
187 *
188 * The optional key/value pairs are attached as continuation lines starting
189 * with a space character and terminated by a newline. All possible
190 * non-prinatable characters are escaped in the "\xff" notation.
191 *
192 * Users of the export format should ignore possible additional values
193 * separated by ',', and find the message after the ';' character.
Kay Sievers7ff95542012-05-03 02:29:13 +0200194 */
Matt Mackalld59745c2005-05-01 08:59:02 -0700195
Kay Sievers084681d2012-06-28 09:38:53 +0200196enum log_flags {
Kay Sievers5becfb12012-07-09 12:15:42 -0700197 LOG_NOCONS = 1, /* already flushed, do not print to console */
198 LOG_NEWLINE = 2, /* text ended with a newline */
199 LOG_PREFIX = 4, /* text started with a prefix */
200 LOG_CONT = 8, /* text is a fragment of a continuation line */
Kay Sievers084681d2012-06-28 09:38:53 +0200201};
202
Kay Sievers7ff95542012-05-03 02:29:13 +0200203struct log {
204 u64 ts_nsec; /* timestamp in nanoseconds */
205 u16 len; /* length of entire record */
206 u16 text_len; /* length of text buffer */
207 u16 dict_len; /* length of dictionary buffer */
Kay Sievers084681d2012-06-28 09:38:53 +0200208 u8 facility; /* syslog facility */
209 u8 flags:5; /* internal record flags */
210 u8 level:3; /* syslog level */
Kay Sievers7ff95542012-05-03 02:29:13 +0200211};
212
213/*
214 * The logbuf_lock protects kmsg buffer, indices, counters. It is also
215 * used in interesting ways to provide interlocking in console_unlock();
216 */
217static DEFINE_RAW_SPINLOCK(logbuf_lock);
218
Kay Sievers96efedf2012-07-16 18:35:29 -0700219#ifdef CONFIG_PRINTK
Kay Sievers7f3a7812012-05-09 01:37:51 +0200220/* the next printk record to read by syslog(READ) or /proc/kmsg */
221static u64 syslog_seq;
222static u32 syslog_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -0700223static enum log_flags syslog_prev;
Kay Sieverseb02dac2012-07-09 10:05:10 -0700224static size_t syslog_partial;
Kay Sievers7f3a7812012-05-09 01:37:51 +0200225
226/* index and sequence number of the first record stored in the buffer */
227static u64 log_first_seq;
228static u32 log_first_idx;
229
230/* index and sequence number of the next record to store in the buffer */
231static u64 log_next_seq;
Kay Sievers7f3a7812012-05-09 01:37:51 +0200232static u32 log_next_idx;
233
Kay Sieverseab07262012-07-16 18:35:30 -0700234/* the next printk record to write to the console */
235static u64 console_seq;
236static u32 console_idx;
237static enum log_flags console_prev;
238
Kay Sievers7f3a7812012-05-09 01:37:51 +0200239/* the next printk record to read after the last 'clear' command */
240static u64 clear_seq;
241static u32 clear_idx;
Kay Sievers7ff95542012-05-03 02:29:13 +0200242
Kay Sievers70498252012-07-16 18:35:29 -0700243#define PREFIX_MAX 32
244#define LOG_LINE_MAX 1024 - PREFIX_MAX
Kay Sievers7ff95542012-05-03 02:29:13 +0200245
246/* record buffer */
Andrew Lunn6ebb0172012-06-05 08:52:34 +0200247#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
Stephen Warrenf8450fc2012-05-10 16:14:33 -0600248#define LOG_ALIGN 4
249#else
Andrew Lunn6ebb0172012-06-05 08:52:34 +0200250#define LOG_ALIGN __alignof__(struct log)
Stephen Warrenf8450fc2012-05-10 16:14:33 -0600251#endif
Kay Sievers7ff95542012-05-03 02:29:13 +0200252#define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
Stephen Warrenf8450fc2012-05-10 16:14:33 -0600253static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN);
Matt Mackalld59745c2005-05-01 08:59:02 -0700254static char *log_buf = __log_buf;
Kay Sievers7ff95542012-05-03 02:29:13 +0200255static u32 log_buf_len = __LOG_BUF_LEN;
256
Kay Sievers7f3a7812012-05-09 01:37:51 +0200257/* cpu currently holding logbuf_lock */
258static volatile unsigned int logbuf_cpu = UINT_MAX;
Kay Sievers7ff95542012-05-03 02:29:13 +0200259
260/* human readable text of the record */
261static char *log_text(const struct log *msg)
262{
263 return (char *)msg + sizeof(struct log);
264}
265
266/* optional key/value pair dictionary attached to the record */
267static char *log_dict(const struct log *msg)
268{
269 return (char *)msg + sizeof(struct log) + msg->text_len;
270}
271
272/* get record by index; idx must point to valid msg */
273static struct log *log_from_idx(u32 idx)
274{
275 struct log *msg = (struct log *)(log_buf + idx);
276
277 /*
278 * A length == 0 record is the end of buffer marker. Wrap around and
279 * read the message at the start of the buffer.
280 */
281 if (!msg->len)
282 return (struct log *)log_buf;
283 return msg;
284}
285
286/* get next record; idx must point to valid msg */
287static u32 log_next(u32 idx)
288{
289 struct log *msg = (struct log *)(log_buf + idx);
290
291 /* length == 0 indicates the end of the buffer; wrap */
292 /*
293 * A length == 0 record is the end of buffer marker. Wrap around and
294 * read the message at the start of the buffer as *this* one, and
295 * return the one after that.
296 */
297 if (!msg->len) {
298 msg = (struct log *)log_buf;
299 return msg->len;
300 }
301 return idx + msg->len;
302}
303
Kay Sievers7ff95542012-05-03 02:29:13 +0200304/* insert record into the buffer, discard old ones, update heads */
305static void log_store(int facility, int level,
Kay Sievers084681d2012-06-28 09:38:53 +0200306 enum log_flags flags, u64 ts_nsec,
Kay Sievers7ff95542012-05-03 02:29:13 +0200307 const char *dict, u16 dict_len,
308 const char *text, u16 text_len)
309{
310 struct log *msg;
311 u32 size, pad_len;
312
313 /* number of '\0' padding bytes to next message */
314 size = sizeof(struct log) + text_len + dict_len;
315 pad_len = (-size) & (LOG_ALIGN - 1);
316 size += pad_len;
317
318 while (log_first_seq < log_next_seq) {
319 u32 free;
320
321 if (log_next_idx > log_first_idx)
322 free = max(log_buf_len - log_next_idx, log_first_idx);
323 else
324 free = log_first_idx - log_next_idx;
325
326 if (free > size + sizeof(struct log))
327 break;
328
329 /* drop old messages until we have enough contiuous space */
330 log_first_idx = log_next(log_first_idx);
331 log_first_seq++;
332 }
333
334 if (log_next_idx + size + sizeof(struct log) >= log_buf_len) {
335 /*
336 * This message + an additional empty header does not fit
337 * at the end of the buffer. Add an empty header with len == 0
338 * to signify a wrap around.
339 */
340 memset(log_buf + log_next_idx, 0, sizeof(struct log));
341 log_next_idx = 0;
342 }
343
344 /* fill message */
345 msg = (struct log *)(log_buf + log_next_idx);
346 memcpy(log_text(msg), text, text_len);
347 msg->text_len = text_len;
348 memcpy(log_dict(msg), dict, dict_len);
349 msg->dict_len = dict_len;
Kay Sievers084681d2012-06-28 09:38:53 +0200350 msg->facility = facility;
351 msg->level = level & 7;
352 msg->flags = flags & 0x1f;
353 if (ts_nsec > 0)
354 msg->ts_nsec = ts_nsec;
355 else
356 msg->ts_nsec = local_clock();
Kay Sievers7ff95542012-05-03 02:29:13 +0200357 memset(log_dict(msg) + dict_len, 0, pad_len);
358 msg->len = sizeof(struct log) + text_len + dict_len + pad_len;
359
360 /* insert message */
361 log_next_idx += msg->len;
362 log_next_seq++;
363}
Matt Mackalld59745c2005-05-01 08:59:02 -0700364
Kay Sieverse11fea92012-05-03 02:29:41 +0200365/* /dev/kmsg - userspace message inject/listen interface */
366struct devkmsg_user {
367 u64 seq;
368 u32 idx;
Kay Sieversd39f3d72012-07-16 18:35:30 -0700369 enum log_flags prev;
Kay Sieverse11fea92012-05-03 02:29:41 +0200370 struct mutex lock;
371 char buf[8192];
372};
373
374static ssize_t devkmsg_writev(struct kiocb *iocb, const struct iovec *iv,
375 unsigned long count, loff_t pos)
376{
377 char *buf, *line;
378 int i;
379 int level = default_message_loglevel;
380 int facility = 1; /* LOG_USER */
381 size_t len = iov_length(iv, count);
382 ssize_t ret = len;
383
384 if (len > LOG_LINE_MAX)
385 return -EINVAL;
386 buf = kmalloc(len+1, GFP_KERNEL);
387 if (buf == NULL)
388 return -ENOMEM;
389
390 line = buf;
391 for (i = 0; i < count; i++) {
Kay Sieverscdf53442012-07-30 14:40:08 -0700392 if (copy_from_user(line, iv[i].iov_base, iv[i].iov_len)) {
393 ret = -EFAULT;
Kay Sieverse11fea92012-05-03 02:29:41 +0200394 goto out;
Kay Sieverscdf53442012-07-30 14:40:08 -0700395 }
Kay Sieverse11fea92012-05-03 02:29:41 +0200396 line += iv[i].iov_len;
397 }
398
399 /*
400 * Extract and skip the syslog prefix <[0-9]*>. Coming from userspace
401 * the decimal value represents 32bit, the lower 3 bit are the log
402 * level, the rest are the log facility.
403 *
404 * If no prefix or no userspace facility is specified, we
405 * enforce LOG_USER, to be able to reliably distinguish
406 * kernel-generated messages from userspace-injected ones.
407 */
408 line = buf;
409 if (line[0] == '<') {
410 char *endp = NULL;
411
412 i = simple_strtoul(line+1, &endp, 10);
413 if (endp && endp[0] == '>') {
414 level = i & 7;
415 if (i >> 3)
416 facility = i >> 3;
417 endp++;
418 len -= endp - line;
419 line = endp;
420 }
421 }
422 line[len] = '\0';
423
424 printk_emit(facility, level, NULL, 0, "%s", line);
425out:
426 kfree(buf);
427 return ret;
428}
429
430static ssize_t devkmsg_read(struct file *file, char __user *buf,
431 size_t count, loff_t *ppos)
432{
433 struct devkmsg_user *user = file->private_data;
434 struct log *msg;
Kay Sievers5fc324902012-05-08 13:04:17 +0200435 u64 ts_usec;
Kay Sieverse11fea92012-05-03 02:29:41 +0200436 size_t i;
Kay Sieversd39f3d72012-07-16 18:35:30 -0700437 char cont = '-';
Kay Sieverse11fea92012-05-03 02:29:41 +0200438 size_t len;
439 ssize_t ret;
440
441 if (!user)
442 return -EBADF;
443
Yuanhan Liu4a77a5a2012-06-16 21:21:51 +0800444 ret = mutex_lock_interruptible(&user->lock);
445 if (ret)
446 return ret;
liu chuansheng5c53d812012-07-06 09:50:08 -0700447 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200448 while (user->seq == log_next_seq) {
449 if (file->f_flags & O_NONBLOCK) {
450 ret = -EAGAIN;
liu chuansheng5c53d812012-07-06 09:50:08 -0700451 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200452 goto out;
453 }
454
liu chuansheng5c53d812012-07-06 09:50:08 -0700455 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200456 ret = wait_event_interruptible(log_wait,
457 user->seq != log_next_seq);
458 if (ret)
459 goto out;
liu chuansheng5c53d812012-07-06 09:50:08 -0700460 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200461 }
462
463 if (user->seq < log_first_seq) {
464 /* our last seen message is gone, return error and reset */
465 user->idx = log_first_idx;
466 user->seq = log_first_seq;
467 ret = -EPIPE;
liu chuansheng5c53d812012-07-06 09:50:08 -0700468 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200469 goto out;
470 }
471
472 msg = log_from_idx(user->idx);
Kay Sievers5fc324902012-05-08 13:04:17 +0200473 ts_usec = msg->ts_nsec;
474 do_div(ts_usec, 1000);
Kay Sieversd39f3d72012-07-16 18:35:30 -0700475
476 /*
477 * If we couldn't merge continuation line fragments during the print,
478 * export the stored flags to allow an optional external merge of the
479 * records. Merging the records isn't always neccessarily correct, like
480 * when we hit a race during printing. In most cases though, it produces
481 * better readable output. 'c' in the record flags mark the first
482 * fragment of a line, '+' the following.
483 */
484 if (msg->flags & LOG_CONT && !(user->prev & LOG_CONT))
485 cont = 'c';
486 else if ((msg->flags & LOG_CONT) ||
487 ((user->prev & LOG_CONT) && !(msg->flags & LOG_PREFIX)))
488 cont = '+';
489
490 len = sprintf(user->buf, "%u,%llu,%llu,%c;",
491 (msg->facility << 3) | msg->level,
492 user->seq, ts_usec, cont);
493 user->prev = msg->flags;
Kay Sieverse11fea92012-05-03 02:29:41 +0200494
495 /* escape non-printable characters */
496 for (i = 0; i < msg->text_len; i++) {
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200497 unsigned char c = log_text(msg)[i];
Kay Sieverse11fea92012-05-03 02:29:41 +0200498
Kay Sieverse3f5a5f2012-07-06 09:50:09 -0700499 if (c < ' ' || c >= 127 || c == '\\')
Kay Sieverse11fea92012-05-03 02:29:41 +0200500 len += sprintf(user->buf + len, "\\x%02x", c);
501 else
502 user->buf[len++] = c;
503 }
504 user->buf[len++] = '\n';
505
506 if (msg->dict_len) {
507 bool line = true;
508
509 for (i = 0; i < msg->dict_len; i++) {
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200510 unsigned char c = log_dict(msg)[i];
Kay Sieverse11fea92012-05-03 02:29:41 +0200511
512 if (line) {
513 user->buf[len++] = ' ';
514 line = false;
515 }
516
517 if (c == '\0') {
518 user->buf[len++] = '\n';
519 line = true;
520 continue;
521 }
522
Kay Sieverse3f5a5f2012-07-06 09:50:09 -0700523 if (c < ' ' || c >= 127 || c == '\\') {
Kay Sieverse11fea92012-05-03 02:29:41 +0200524 len += sprintf(user->buf + len, "\\x%02x", c);
525 continue;
526 }
527
528 user->buf[len++] = c;
529 }
530 user->buf[len++] = '\n';
531 }
532
533 user->idx = log_next(user->idx);
534 user->seq++;
liu chuansheng5c53d812012-07-06 09:50:08 -0700535 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200536
537 if (len > count) {
538 ret = -EINVAL;
539 goto out;
540 }
541
542 if (copy_to_user(buf, user->buf, len)) {
543 ret = -EFAULT;
544 goto out;
545 }
546 ret = len;
547out:
548 mutex_unlock(&user->lock);
549 return ret;
550}
551
552static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence)
553{
554 struct devkmsg_user *user = file->private_data;
555 loff_t ret = 0;
556
557 if (!user)
558 return -EBADF;
559 if (offset)
560 return -ESPIPE;
561
liu chuansheng5c53d812012-07-06 09:50:08 -0700562 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200563 switch (whence) {
564 case SEEK_SET:
565 /* the first record */
566 user->idx = log_first_idx;
567 user->seq = log_first_seq;
568 break;
569 case SEEK_DATA:
570 /*
571 * The first record after the last SYSLOG_ACTION_CLEAR,
572 * like issued by 'dmesg -c'. Reading /dev/kmsg itself
573 * changes no global state, and does not clear anything.
574 */
575 user->idx = clear_idx;
576 user->seq = clear_seq;
577 break;
578 case SEEK_END:
579 /* after the last record */
580 user->idx = log_next_idx;
581 user->seq = log_next_seq;
582 break;
583 default:
584 ret = -EINVAL;
585 }
liu chuansheng5c53d812012-07-06 09:50:08 -0700586 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200587 return ret;
588}
589
590static unsigned int devkmsg_poll(struct file *file, poll_table *wait)
591{
592 struct devkmsg_user *user = file->private_data;
593 int ret = 0;
594
595 if (!user)
596 return POLLERR|POLLNVAL;
597
598 poll_wait(file, &log_wait, wait);
599
liu chuansheng5c53d812012-07-06 09:50:08 -0700600 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200601 if (user->seq < log_next_seq) {
602 /* return error when data has vanished underneath us */
603 if (user->seq < log_first_seq)
604 ret = POLLIN|POLLRDNORM|POLLERR|POLLPRI;
605 ret = POLLIN|POLLRDNORM;
606 }
liu chuansheng5c53d812012-07-06 09:50:08 -0700607 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200608
609 return ret;
610}
611
612static int devkmsg_open(struct inode *inode, struct file *file)
613{
614 struct devkmsg_user *user;
615 int err;
616
617 /* write-only does not need any file context */
618 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
619 return 0;
620
621 err = security_syslog(SYSLOG_ACTION_READ_ALL);
622 if (err)
623 return err;
624
625 user = kmalloc(sizeof(struct devkmsg_user), GFP_KERNEL);
626 if (!user)
627 return -ENOMEM;
628
629 mutex_init(&user->lock);
630
liu chuansheng5c53d812012-07-06 09:50:08 -0700631 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200632 user->idx = log_first_idx;
633 user->seq = log_first_seq;
liu chuansheng5c53d812012-07-06 09:50:08 -0700634 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200635
636 file->private_data = user;
637 return 0;
638}
639
640static int devkmsg_release(struct inode *inode, struct file *file)
641{
642 struct devkmsg_user *user = file->private_data;
643
644 if (!user)
645 return 0;
646
647 mutex_destroy(&user->lock);
648 kfree(user);
649 return 0;
650}
651
652const struct file_operations kmsg_fops = {
653 .open = devkmsg_open,
654 .read = devkmsg_read,
655 .aio_write = devkmsg_writev,
656 .llseek = devkmsg_llseek,
657 .poll = devkmsg_poll,
658 .release = devkmsg_release,
659};
660
Neil Horman04d491a2009-04-02 16:58:57 -0700661#ifdef CONFIG_KEXEC
662/*
663 * This appends the listed symbols to /proc/vmcoreinfo
664 *
665 * /proc/vmcoreinfo is used by various utiilties, like crash and makedumpfile to
666 * obtain access to symbols that are otherwise very difficult to locate. These
667 * symbols are specifically used so that utilities can access and extract the
668 * dmesg log from a vmcore file after a crash.
669 */
670void log_buf_kexec_setup(void)
671{
672 VMCOREINFO_SYMBOL(log_buf);
Neil Horman04d491a2009-04-02 16:58:57 -0700673 VMCOREINFO_SYMBOL(log_buf_len);
Kay Sievers7ff95542012-05-03 02:29:13 +0200674 VMCOREINFO_SYMBOL(log_first_idx);
675 VMCOREINFO_SYMBOL(log_next_idx);
Vivek Goyal67914572012-07-18 13:18:12 -0400676 /*
677 * Export struct log size and field offsets. User space tools can
678 * parse it and detect any changes to structure down the line.
679 */
680 VMCOREINFO_STRUCT_SIZE(log);
681 VMCOREINFO_OFFSET(log, ts_nsec);
682 VMCOREINFO_OFFSET(log, len);
683 VMCOREINFO_OFFSET(log, text_len);
684 VMCOREINFO_OFFSET(log, dict_len);
Neil Horman04d491a2009-04-02 16:58:57 -0700685}
686#endif
687
Mike Travis162a7e72011-05-24 17:13:20 -0700688/* requested log_buf_len from kernel cmdline */
689static unsigned long __initdata new_log_buf_len;
690
691/* save requested log_buf_len since it's too early to process it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692static int __init log_buf_len_setup(char *str)
693{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800694 unsigned size = memparse(str, &str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 if (size)
697 size = roundup_pow_of_two(size);
Mike Travis162a7e72011-05-24 17:13:20 -0700698 if (size > log_buf_len)
699 new_log_buf_len = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Mike Travis162a7e72011-05-24 17:13:20 -0700701 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
Mike Travis162a7e72011-05-24 17:13:20 -0700703early_param("log_buf_len", log_buf_len_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Mike Travis162a7e72011-05-24 17:13:20 -0700705void __init setup_log_buf(int early)
706{
707 unsigned long flags;
Mike Travis162a7e72011-05-24 17:13:20 -0700708 char *new_log_buf;
709 int free;
710
711 if (!new_log_buf_len)
712 return;
713
714 if (early) {
715 unsigned long mem;
716
717 mem = memblock_alloc(new_log_buf_len, PAGE_SIZE);
Tejun Heo1f5026a2011-07-12 09:58:09 +0200718 if (!mem)
Mike Travis162a7e72011-05-24 17:13:20 -0700719 return;
720 new_log_buf = __va(mem);
721 } else {
722 new_log_buf = alloc_bootmem_nopanic(new_log_buf_len);
723 }
724
725 if (unlikely(!new_log_buf)) {
726 pr_err("log_buf_len: %ld bytes not available\n",
727 new_log_buf_len);
728 return;
729 }
730
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200731 raw_spin_lock_irqsave(&logbuf_lock, flags);
Mike Travis162a7e72011-05-24 17:13:20 -0700732 log_buf_len = new_log_buf_len;
733 log_buf = new_log_buf;
734 new_log_buf_len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +0200735 free = __LOG_BUF_LEN - log_next_idx;
736 memcpy(log_buf, __log_buf, __LOG_BUF_LEN);
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200737 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
Mike Travis162a7e72011-05-24 17:13:20 -0700738
739 pr_info("log_buf_len: %d\n", log_buf_len);
740 pr_info("early log buf free: %d(%d%%)\n",
741 free, (free * 100) / __LOG_BUF_LEN);
742}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700744#ifdef CONFIG_BOOT_PRINTK_DELAY
745
Namhyung Kim674dff62010-10-26 14:22:48 -0700746static int boot_delay; /* msecs delay after each printk during bootup */
Dave Young3a3b6ed2009-09-22 16:43:31 -0700747static unsigned long long loops_per_msec; /* based on boot_delay */
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700748
749static int __init boot_delay_setup(char *str)
750{
751 unsigned long lpj;
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700752
753 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
754 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
755
756 get_option(&str, &boot_delay);
757 if (boot_delay > 10 * 1000)
758 boot_delay = 0;
759
Dave Young3a3b6ed2009-09-22 16:43:31 -0700760 pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
761 "HZ: %d, loops_per_msec: %llu\n",
762 boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700763 return 1;
764}
765__setup("boot_delay=", boot_delay_setup);
766
767static void boot_delay_msec(void)
768{
769 unsigned long long k;
770 unsigned long timeout;
771
772 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
773 return;
774
Dave Young3a3b6ed2009-09-22 16:43:31 -0700775 k = (unsigned long long)loops_per_msec * boot_delay;
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700776
777 timeout = jiffies + msecs_to_jiffies(boot_delay);
778 while (k) {
779 k--;
780 cpu_relax();
781 /*
782 * use (volatile) jiffies to prevent
783 * compiler reduction; loop termination via jiffies
784 * is secondary and may or may not happen.
785 */
786 if (time_after(jiffies, timeout))
787 break;
788 touch_nmi_watchdog();
789 }
790}
791#else
792static inline void boot_delay_msec(void)
793{
794}
795#endif
796
Dan Rosenbergeaf06b22010-11-11 14:05:18 -0800797#ifdef CONFIG_SECURITY_DMESG_RESTRICT
798int dmesg_restrict = 1;
799#else
800int dmesg_restrict;
801#endif
802
Linus Torvaldsee24aeb2011-02-10 17:53:55 -0800803static int syslog_action_restricted(int type)
804{
805 if (dmesg_restrict)
806 return 1;
807 /* Unless restricted, we allow "read all" and "get buffer size" for everybody */
808 return type != SYSLOG_ACTION_READ_ALL && type != SYSLOG_ACTION_SIZE_BUFFER;
809}
810
811static int check_syslog_permissions(int type, bool from_file)
812{
813 /*
814 * If this is from /proc/kmsg and we've already opened it, then we've
815 * already done the capabilities checks at open time.
816 */
817 if (from_file && type != SYSLOG_ACTION_OPEN)
818 return 0;
819
820 if (syslog_action_restricted(type)) {
821 if (capable(CAP_SYSLOG))
822 return 0;
823 /* For historical reasons, accept CAP_SYS_ADMIN too, with a warning */
824 if (capable(CAP_SYS_ADMIN)) {
Jonathan Niederf2c0d022011-08-08 06:22:43 +0200825 printk_once(KERN_WARNING "%s (%d): "
826 "Attempt to access syslog with CAP_SYS_ADMIN "
827 "but no CAP_SYSLOG (deprecated).\n",
828 current->comm, task_pid_nr(current));
Linus Torvaldsee24aeb2011-02-10 17:53:55 -0800829 return 0;
830 }
831 return -EPERM;
832 }
833 return 0;
834}
835
Kay Sievers7ff95542012-05-03 02:29:13 +0200836#if defined(CONFIG_PRINTK_TIME)
837static bool printk_time = 1;
838#else
839static bool printk_time;
840#endif
841module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
842
Kay Sievers084681d2012-06-28 09:38:53 +0200843static size_t print_time(u64 ts, char *buf)
844{
845 unsigned long rem_nsec;
846
847 if (!printk_time)
848 return 0;
849
850 if (!buf)
851 return 15;
852
853 rem_nsec = do_div(ts, 1000000000);
854 return sprintf(buf, "[%5lu.%06lu] ",
855 (unsigned long)ts, rem_nsec / 1000);
856}
857
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200858static size_t print_prefix(const struct log *msg, bool syslog, char *buf)
Kay Sievers649e6ee2012-05-10 04:30:45 +0200859{
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200860 size_t len = 0;
Kay Sievers43a73a52012-07-06 09:50:09 -0700861 unsigned int prefix = (msg->facility << 3) | msg->level;
Kay Sievers649e6ee2012-05-10 04:30:45 +0200862
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200863 if (syslog) {
864 if (buf) {
Kay Sievers43a73a52012-07-06 09:50:09 -0700865 len += sprintf(buf, "<%u>", prefix);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200866 } else {
867 len += 3;
Kay Sievers43a73a52012-07-06 09:50:09 -0700868 if (prefix > 999)
869 len += 3;
870 else if (prefix > 99)
871 len += 2;
872 else if (prefix > 9)
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200873 len++;
874 }
Kay Sievers7ff95542012-05-03 02:29:13 +0200875 }
876
Kay Sievers084681d2012-06-28 09:38:53 +0200877 len += print_time(msg->ts_nsec, buf ? buf + len : NULL);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200878 return len;
879}
880
Kay Sievers5becfb12012-07-09 12:15:42 -0700881static size_t msg_print_text(const struct log *msg, enum log_flags prev,
882 bool syslog, char *buf, size_t size)
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200883{
884 const char *text = log_text(msg);
885 size_t text_size = msg->text_len;
Kay Sievers5becfb12012-07-09 12:15:42 -0700886 bool prefix = true;
887 bool newline = true;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200888 size_t len = 0;
889
Kay Sievers5becfb12012-07-09 12:15:42 -0700890 if ((prev & LOG_CONT) && !(msg->flags & LOG_PREFIX))
891 prefix = false;
892
893 if (msg->flags & LOG_CONT) {
894 if ((prev & LOG_CONT) && !(prev & LOG_NEWLINE))
895 prefix = false;
896
897 if (!(msg->flags & LOG_NEWLINE))
898 newline = false;
899 }
900
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200901 do {
902 const char *next = memchr(text, '\n', text_size);
903 size_t text_len;
904
905 if (next) {
906 text_len = next - text;
907 next++;
908 text_size -= next - text;
909 } else {
910 text_len = text_size;
911 }
912
913 if (buf) {
914 if (print_prefix(msg, syslog, NULL) +
Kay Sievers70498252012-07-16 18:35:29 -0700915 text_len + 1 >= size - len)
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200916 break;
917
Kay Sievers5becfb12012-07-09 12:15:42 -0700918 if (prefix)
919 len += print_prefix(msg, syslog, buf + len);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200920 memcpy(buf + len, text, text_len);
921 len += text_len;
Kay Sievers5becfb12012-07-09 12:15:42 -0700922 if (next || newline)
923 buf[len++] = '\n';
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200924 } else {
925 /* SYSLOG_ACTION_* buffer size only calculation */
Kay Sievers5becfb12012-07-09 12:15:42 -0700926 if (prefix)
927 len += print_prefix(msg, syslog, NULL);
928 len += text_len;
929 if (next || newline)
930 len++;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200931 }
932
Kay Sievers5becfb12012-07-09 12:15:42 -0700933 prefix = true;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200934 text = next;
935 } while (text);
936
Kay Sievers7ff95542012-05-03 02:29:13 +0200937 return len;
938}
939
940static int syslog_print(char __user *buf, int size)
941{
942 char *text;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200943 struct log *msg;
Jan Beulich116e90b2012-06-22 16:36:09 +0100944 int len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +0200945
Kay Sievers70498252012-07-16 18:35:29 -0700946 text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
Kay Sievers7ff95542012-05-03 02:29:13 +0200947 if (!text)
948 return -ENOMEM;
949
Jan Beulich116e90b2012-06-22 16:36:09 +0100950 while (size > 0) {
951 size_t n;
Kay Sieverseb02dac2012-07-09 10:05:10 -0700952 size_t skip;
Kay Sievers7ff95542012-05-03 02:29:13 +0200953
Jan Beulich116e90b2012-06-22 16:36:09 +0100954 raw_spin_lock_irq(&logbuf_lock);
955 if (syslog_seq < log_first_seq) {
956 /* messages are gone, move to first one */
957 syslog_seq = log_first_seq;
958 syslog_idx = log_first_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -0700959 syslog_prev = 0;
Kay Sieverseb02dac2012-07-09 10:05:10 -0700960 syslog_partial = 0;
Jan Beulich116e90b2012-06-22 16:36:09 +0100961 }
962 if (syslog_seq == log_next_seq) {
963 raw_spin_unlock_irq(&logbuf_lock);
964 break;
965 }
Kay Sieverseb02dac2012-07-09 10:05:10 -0700966
967 skip = syslog_partial;
Jan Beulich116e90b2012-06-22 16:36:09 +0100968 msg = log_from_idx(syslog_idx);
Kay Sievers70498252012-07-16 18:35:29 -0700969 n = msg_print_text(msg, syslog_prev, true, text,
970 LOG_LINE_MAX + PREFIX_MAX);
Kay Sieverseb02dac2012-07-09 10:05:10 -0700971 if (n - syslog_partial <= size) {
972 /* message fits into buffer, move forward */
Jan Beulich116e90b2012-06-22 16:36:09 +0100973 syslog_idx = log_next(syslog_idx);
974 syslog_seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -0700975 syslog_prev = msg->flags;
Kay Sieverseb02dac2012-07-09 10:05:10 -0700976 n -= syslog_partial;
977 syslog_partial = 0;
978 } else if (!len){
979 /* partial read(), remember position */
980 n = size;
981 syslog_partial += n;
Jan Beulich116e90b2012-06-22 16:36:09 +0100982 } else
983 n = 0;
984 raw_spin_unlock_irq(&logbuf_lock);
985
986 if (!n)
987 break;
988
Kay Sieverseb02dac2012-07-09 10:05:10 -0700989 if (copy_to_user(buf, text + skip, n)) {
Jan Beulich116e90b2012-06-22 16:36:09 +0100990 if (!len)
991 len = -EFAULT;
992 break;
993 }
Kay Sieverseb02dac2012-07-09 10:05:10 -0700994
995 len += n;
996 size -= n;
997 buf += n;
Jan Beulich116e90b2012-06-22 16:36:09 +0100998 }
Kay Sievers7ff95542012-05-03 02:29:13 +0200999
1000 kfree(text);
1001 return len;
1002}
1003
1004static int syslog_print_all(char __user *buf, int size, bool clear)
1005{
1006 char *text;
1007 int len = 0;
1008
Kay Sievers70498252012-07-16 18:35:29 -07001009 text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
Kay Sievers7ff95542012-05-03 02:29:13 +02001010 if (!text)
1011 return -ENOMEM;
1012
1013 raw_spin_lock_irq(&logbuf_lock);
1014 if (buf) {
1015 u64 next_seq;
1016 u64 seq;
1017 u32 idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001018 enum log_flags prev;
Kay Sievers7ff95542012-05-03 02:29:13 +02001019
1020 if (clear_seq < log_first_seq) {
1021 /* messages are gone, move to first available one */
1022 clear_seq = log_first_seq;
1023 clear_idx = log_first_idx;
1024 }
1025
1026 /*
1027 * Find first record that fits, including all following records,
1028 * into the user-provided buffer for this dump.
Kay Sieverse2ae7152012-06-15 14:07:51 +02001029 */
Kay Sievers7ff95542012-05-03 02:29:13 +02001030 seq = clear_seq;
1031 idx = clear_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001032 prev = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001033 while (seq < log_next_seq) {
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001034 struct log *msg = log_from_idx(idx);
1035
Kay Sievers5becfb12012-07-09 12:15:42 -07001036 len += msg_print_text(msg, prev, true, NULL, 0);
Kay Sievers7ff95542012-05-03 02:29:13 +02001037 idx = log_next(idx);
1038 seq++;
1039 }
Kay Sieverse2ae7152012-06-15 14:07:51 +02001040
1041 /* move first record forward until length fits into the buffer */
Kay Sievers7ff95542012-05-03 02:29:13 +02001042 seq = clear_seq;
1043 idx = clear_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001044 prev = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001045 while (len > size && seq < log_next_seq) {
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001046 struct log *msg = log_from_idx(idx);
1047
Kay Sievers5becfb12012-07-09 12:15:42 -07001048 len -= msg_print_text(msg, prev, true, NULL, 0);
Kay Sievers7ff95542012-05-03 02:29:13 +02001049 idx = log_next(idx);
1050 seq++;
1051 }
1052
Kay Sieverse2ae7152012-06-15 14:07:51 +02001053 /* last message fitting into this dump */
Kay Sievers7ff95542012-05-03 02:29:13 +02001054 next_seq = log_next_seq;
1055
1056 len = 0;
Kay Sievers5becfb12012-07-09 12:15:42 -07001057 prev = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001058 while (len >= 0 && seq < next_seq) {
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001059 struct log *msg = log_from_idx(idx);
Kay Sievers7ff95542012-05-03 02:29:13 +02001060 int textlen;
1061
Kay Sievers70498252012-07-16 18:35:29 -07001062 textlen = msg_print_text(msg, prev, true, text,
1063 LOG_LINE_MAX + PREFIX_MAX);
Kay Sievers7ff95542012-05-03 02:29:13 +02001064 if (textlen < 0) {
1065 len = textlen;
1066 break;
1067 }
1068 idx = log_next(idx);
1069 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07001070 prev = msg->flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02001071
1072 raw_spin_unlock_irq(&logbuf_lock);
1073 if (copy_to_user(buf + len, text, textlen))
1074 len = -EFAULT;
1075 else
1076 len += textlen;
1077 raw_spin_lock_irq(&logbuf_lock);
1078
1079 if (seq < log_first_seq) {
1080 /* messages are gone, move to next one */
1081 seq = log_first_seq;
1082 idx = log_first_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001083 prev = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001084 }
1085 }
1086 }
1087
1088 if (clear) {
1089 clear_seq = log_next_seq;
1090 clear_idx = log_next_idx;
1091 }
1092 raw_spin_unlock_irq(&logbuf_lock);
1093
1094 kfree(text);
1095 return len;
1096}
1097
Kees Cook00234592010-02-03 15:36:43 -08001098int do_syslog(int type, char __user *buf, int len, bool from_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
Kay Sievers7ff95542012-05-03 02:29:13 +02001100 bool clear = false;
1101 static int saved_console_loglevel = -1;
Linus Torvaldsee24aeb2011-02-10 17:53:55 -08001102 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Linus Torvaldsee24aeb2011-02-10 17:53:55 -08001104 error = check_syslog_permissions(type, from_file);
1105 if (error)
1106 goto out;
Eric Paris12b30522010-11-15 18:36:29 -05001107
1108 error = security_syslog(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (error)
1110 return error;
1111
1112 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08001113 case SYSLOG_ACTION_CLOSE: /* Close log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001115 case SYSLOG_ACTION_OPEN: /* Open log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001117 case SYSLOG_ACTION_READ: /* Read from log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 error = -EINVAL;
1119 if (!buf || len < 0)
1120 goto out;
1121 error = 0;
1122 if (!len)
1123 goto out;
1124 if (!access_ok(VERIFY_WRITE, buf, len)) {
1125 error = -EFAULT;
1126 goto out;
1127 }
Yuanhan Liu4a77a5a2012-06-16 21:21:51 +08001128 error = wait_event_interruptible(log_wait,
1129 syslog_seq != log_next_seq);
Kay Sieverscb424ff2012-07-06 09:50:09 -07001130 if (error)
Yuanhan Liu4a77a5a2012-06-16 21:21:51 +08001131 goto out;
Kay Sievers7ff95542012-05-03 02:29:13 +02001132 error = syslog_print(buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001134 /* Read/clear last kernel messages */
1135 case SYSLOG_ACTION_READ_CLEAR:
Kay Sievers7ff95542012-05-03 02:29:13 +02001136 clear = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 /* FALL THRU */
Kees Cookd78ca3c2010-02-03 15:37:13 -08001138 /* Read last kernel messages */
1139 case SYSLOG_ACTION_READ_ALL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 error = -EINVAL;
1141 if (!buf || len < 0)
1142 goto out;
1143 error = 0;
1144 if (!len)
1145 goto out;
1146 if (!access_ok(VERIFY_WRITE, buf, len)) {
1147 error = -EFAULT;
1148 goto out;
1149 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001150 error = syslog_print_all(buf, len, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001152 /* Clear ring buffer */
1153 case SYSLOG_ACTION_CLEAR:
Kay Sievers7ff95542012-05-03 02:29:13 +02001154 syslog_print_all(NULL, 0, true);
Alan Stern4661e352012-06-22 17:12:19 -04001155 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001156 /* Disable logging to console */
1157 case SYSLOG_ACTION_CONSOLE_OFF:
Frans Pop1aaad492009-07-06 13:31:48 +02001158 if (saved_console_loglevel == -1)
1159 saved_console_loglevel = console_loglevel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 console_loglevel = minimum_console_loglevel;
1161 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001162 /* Enable logging to console */
1163 case SYSLOG_ACTION_CONSOLE_ON:
Frans Pop1aaad492009-07-06 13:31:48 +02001164 if (saved_console_loglevel != -1) {
1165 console_loglevel = saved_console_loglevel;
1166 saved_console_loglevel = -1;
1167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001169 /* Set level of messages printed to console */
1170 case SYSLOG_ACTION_CONSOLE_LEVEL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 error = -EINVAL;
1172 if (len < 1 || len > 8)
1173 goto out;
1174 if (len < minimum_console_loglevel)
1175 len = minimum_console_loglevel;
1176 console_loglevel = len;
Frans Pop1aaad492009-07-06 13:31:48 +02001177 /* Implicitly re-enable logging to console */
1178 saved_console_loglevel = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 error = 0;
1180 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001181 /* Number of chars in the log buffer */
1182 case SYSLOG_ACTION_SIZE_UNREAD:
Kay Sievers7ff95542012-05-03 02:29:13 +02001183 raw_spin_lock_irq(&logbuf_lock);
1184 if (syslog_seq < log_first_seq) {
1185 /* messages are gone, move to first one */
1186 syslog_seq = log_first_seq;
1187 syslog_idx = log_first_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001188 syslog_prev = 0;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001189 syslog_partial = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001190 }
1191 if (from_file) {
1192 /*
1193 * Short-cut for poll(/"proc/kmsg") which simply checks
1194 * for pending data, not the size; return the count of
1195 * records, not the length.
1196 */
1197 error = log_next_idx - syslog_idx;
1198 } else {
Kay Sievers5becfb12012-07-09 12:15:42 -07001199 u64 seq = syslog_seq;
1200 u32 idx = syslog_idx;
1201 enum log_flags prev = syslog_prev;
Kay Sievers7ff95542012-05-03 02:29:13 +02001202
1203 error = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001204 while (seq < log_next_seq) {
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001205 struct log *msg = log_from_idx(idx);
1206
Kay Sievers5becfb12012-07-09 12:15:42 -07001207 error += msg_print_text(msg, prev, true, NULL, 0);
Kay Sievers7ff95542012-05-03 02:29:13 +02001208 idx = log_next(idx);
1209 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07001210 prev = msg->flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02001211 }
Kay Sieverseb02dac2012-07-09 10:05:10 -07001212 error -= syslog_partial;
Kay Sievers7ff95542012-05-03 02:29:13 +02001213 }
1214 raw_spin_unlock_irq(&logbuf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001216 /* Size of the log buffer */
1217 case SYSLOG_ACTION_SIZE_BUFFER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 error = log_buf_len;
1219 break;
1220 default:
1221 error = -EINVAL;
1222 break;
1223 }
1224out:
1225 return error;
1226}
1227
Heiko Carstens1e7bfb22009-01-14 14:14:29 +01001228SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Kees Cook00234592010-02-03 15:36:43 -08001230 return do_syslog(type, buf, len, SYSLOG_FROM_CALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
Rusty Russell2329abf2012-01-13 09:32:18 +10301233static bool __read_mostly ignore_loglevel;
Ingo Molnar792908222006-12-06 20:40:51 -08001234
Adrian Bunk99eea6a2006-12-22 01:07:00 -08001235static int __init ignore_loglevel_setup(char *str)
Ingo Molnar792908222006-12-06 20:40:51 -08001236{
1237 ignore_loglevel = 1;
1238 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
1239
Ingo Molnarc4772d92008-01-31 22:45:23 +01001240 return 0;
Ingo Molnar792908222006-12-06 20:40:51 -08001241}
1242
Ingo Molnarc4772d92008-01-31 22:45:23 +01001243early_param("ignore_loglevel", ignore_loglevel_setup);
Rusty Russell29d4d6d2012-01-13 09:32:17 +10301244module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR);
Yanmin Zhang0eca6b72011-10-31 17:11:25 -07001245MODULE_PARM_DESC(ignore_loglevel, "ignore loglevel setting, to"
1246 "print all kernel messages to the console.");
Ingo Molnar792908222006-12-06 20:40:51 -08001247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 * Call the console drivers, asking them to write out
1250 * log_buf[start] to log_buf[end - 1].
Torben Hohnac751ef2011-01-25 15:07:35 -08001251 * The console_lock must be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 */
Kay Sievers7ff95542012-05-03 02:29:13 +02001253static void call_console_drivers(int level, const char *text, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Kay Sievers7ff95542012-05-03 02:29:13 +02001255 struct console *con;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Kay Sievers7ff95542012-05-03 02:29:13 +02001257 trace_console(text, 0, len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Kay Sievers7ff95542012-05-03 02:29:13 +02001259 if (level >= console_loglevel && !ignore_loglevel)
1260 return;
1261 if (!console_drivers)
1262 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Kay Sievers7ff95542012-05-03 02:29:13 +02001264 for_each_console(con) {
1265 if (exclusive_console && con != exclusive_console)
1266 continue;
1267 if (!(con->flags & CON_ENABLED))
1268 continue;
1269 if (!con->write)
1270 continue;
1271 if (!cpu_online(smp_processor_id()) &&
1272 !(con->flags & CON_ANYTIME))
1273 continue;
1274 con->write(con, text, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
1278/*
1279 * Zap console related locks when oopsing. Only zap at most once
1280 * every 10 seconds, to leave time for slow consoles to print a
1281 * full oops.
1282 */
1283static void zap_locks(void)
1284{
1285 static unsigned long oops_timestamp;
1286
1287 if (time_after_eq(jiffies, oops_timestamp) &&
Jesper Juhl40dc5652005-10-30 15:02:46 -08001288 !time_after(jiffies, oops_timestamp + 30 * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return;
1290
1291 oops_timestamp = jiffies;
1292
Peter Zijlstra94d24fc2011-06-07 11:17:30 +02001293 debug_locks_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 /* If a crash is occurring, make sure we can't deadlock */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001295 raw_spin_lock_init(&logbuf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 /* And make sure that we print immediately */
Thomas Gleixner5b8c4f22010-09-07 14:33:43 +00001297 sema_init(&console_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
1299
Michael Ellerman76a8ad22006-06-25 05:47:40 -07001300/* Check if we have any console registered that can be called early in boot. */
1301static int have_callable_console(void)
1302{
1303 struct console *con;
1304
Robin Getz4d091612009-07-01 21:08:37 -04001305 for_each_console(con)
Michael Ellerman76a8ad22006-06-25 05:47:40 -07001306 if (con->flags & CON_ANYTIME)
1307 return 1;
1308
1309 return 0;
1310}
1311
Linus Torvalds266c2e02008-03-24 19:25:08 -07001312/*
1313 * Can we actually use the console at this time on this cpu?
1314 *
1315 * Console drivers may assume that per-cpu resources have
1316 * been allocated. So unless they're explicitly marked as
1317 * being able to cope (CON_ANYTIME) don't call them until
1318 * this CPU is officially up.
1319 */
1320static inline int can_use_console(unsigned int cpu)
1321{
1322 return cpu_online(cpu) || have_callable_console();
1323}
1324
1325/*
1326 * Try to get console ownership to actually show the kernel
1327 * messages from a 'printk'. Return true (and with the
Torben Hohnac751ef2011-01-25 15:07:35 -08001328 * console_lock held, and 'console_locked' set) if it
Linus Torvalds266c2e02008-03-24 19:25:08 -07001329 * is successful, false otherwise.
1330 *
1331 * This gets called with the 'logbuf_lock' spinlock held and
1332 * interrupts disabled. It should return with 'lockbuf_lock'
1333 * released but interrupts still disabled.
1334 */
Torben Hohnac751ef2011-01-25 15:07:35 -08001335static int console_trylock_for_printk(unsigned int cpu)
Namhyung Kim8155c022010-10-26 14:22:47 -07001336 __releases(&logbuf_lock)
Linus Torvalds266c2e02008-03-24 19:25:08 -07001337{
Peter Zijlstra0b5e1c52011-06-07 11:15:33 +02001338 int retval = 0, wake = 0;
Linus Torvalds266c2e02008-03-24 19:25:08 -07001339
Torben Hohnac751ef2011-01-25 15:07:35 -08001340 if (console_trylock()) {
Linus Torvalds093a07e2008-04-15 13:09:54 -07001341 retval = 1;
1342
1343 /*
1344 * If we can't use the console, we need to release
1345 * the console semaphore by hand to avoid flushing
1346 * the buffer. We need to hold the console semaphore
1347 * in order to do this test safely.
1348 */
1349 if (!can_use_console(cpu)) {
1350 console_locked = 0;
Peter Zijlstra0b5e1c52011-06-07 11:15:33 +02001351 wake = 1;
Linus Torvalds093a07e2008-04-15 13:09:54 -07001352 retval = 0;
1353 }
1354 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001355 logbuf_cpu = UINT_MAX;
Peter Zijlstra0b5e1c52011-06-07 11:15:33 +02001356 if (wake)
1357 up(&console_sem);
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001358 raw_spin_unlock(&logbuf_lock);
Linus Torvalds266c2e02008-03-24 19:25:08 -07001359 return retval;
1360}
Ingo Molnar32a76002008-01-25 21:07:58 +01001361
Dave Youngaf913222009-09-22 16:43:33 -07001362int printk_delay_msec __read_mostly;
1363
1364static inline void printk_delay(void)
1365{
1366 if (unlikely(printk_delay_msec)) {
1367 int m = printk_delay_msec;
1368
1369 while (m--) {
1370 mdelay(1);
1371 touch_nmi_watchdog();
1372 }
1373 }
1374}
1375
Kay Sievers084681d2012-06-28 09:38:53 +02001376/*
1377 * Continuation lines are buffered, and not committed to the record buffer
1378 * until the line is complete, or a race forces it. The line fragments
1379 * though, are printed immediately to the consoles to ensure everything has
1380 * reached the console in case of a kernel crash.
1381 */
1382static struct cont {
1383 char buf[LOG_LINE_MAX];
1384 size_t len; /* length == 0 means unused buffer */
1385 size_t cons; /* bytes written to console */
1386 struct task_struct *owner; /* task of first print*/
1387 u64 ts_nsec; /* time of first print */
1388 u8 level; /* log level of first message */
1389 u8 facility; /* log level of first message */
Kay Sieverseab07262012-07-16 18:35:30 -07001390 enum log_flags flags; /* prefix, newline flags */
Kay Sievers084681d2012-06-28 09:38:53 +02001391 bool flushed:1; /* buffer sealed and committed */
1392} cont;
1393
Kay Sievers70498252012-07-16 18:35:29 -07001394static void cont_flush(enum log_flags flags)
Kay Sievers084681d2012-06-28 09:38:53 +02001395{
1396 if (cont.flushed)
1397 return;
1398 if (cont.len == 0)
1399 return;
1400
Kay Sieverseab07262012-07-16 18:35:30 -07001401 if (cont.cons) {
1402 /*
1403 * If a fragment of this line was directly flushed to the
1404 * console; wait for the console to pick up the rest of the
1405 * line. LOG_NOCONS suppresses a duplicated output.
1406 */
1407 log_store(cont.facility, cont.level, flags | LOG_NOCONS,
1408 cont.ts_nsec, NULL, 0, cont.buf, cont.len);
1409 cont.flags = flags;
1410 cont.flushed = true;
1411 } else {
1412 /*
1413 * If no fragment of this line ever reached the console,
1414 * just submit it to the store and free the buffer.
1415 */
1416 log_store(cont.facility, cont.level, flags, 0,
1417 NULL, 0, cont.buf, cont.len);
1418 cont.len = 0;
1419 }
Kay Sievers084681d2012-06-28 09:38:53 +02001420}
1421
1422static bool cont_add(int facility, int level, const char *text, size_t len)
1423{
1424 if (cont.len && cont.flushed)
1425 return false;
1426
1427 if (cont.len + len > sizeof(cont.buf)) {
Kay Sievers70498252012-07-16 18:35:29 -07001428 /* the line gets too long, split it up in separate records */
1429 cont_flush(LOG_CONT);
Kay Sievers084681d2012-06-28 09:38:53 +02001430 return false;
1431 }
1432
1433 if (!cont.len) {
1434 cont.facility = facility;
1435 cont.level = level;
1436 cont.owner = current;
1437 cont.ts_nsec = local_clock();
Kay Sieverseab07262012-07-16 18:35:30 -07001438 cont.flags = 0;
Kay Sievers084681d2012-06-28 09:38:53 +02001439 cont.cons = 0;
1440 cont.flushed = false;
1441 }
1442
1443 memcpy(cont.buf + cont.len, text, len);
1444 cont.len += len;
Kay Sieverseab07262012-07-16 18:35:30 -07001445
1446 if (cont.len > (sizeof(cont.buf) * 80) / 100)
1447 cont_flush(LOG_CONT);
1448
Kay Sievers084681d2012-06-28 09:38:53 +02001449 return true;
1450}
1451
1452static size_t cont_print_text(char *text, size_t size)
1453{
1454 size_t textlen = 0;
1455 size_t len;
1456
Kay Sieverseab07262012-07-16 18:35:30 -07001457 if (cont.cons == 0 && (console_prev & LOG_NEWLINE)) {
Kay Sievers084681d2012-06-28 09:38:53 +02001458 textlen += print_time(cont.ts_nsec, text);
1459 size -= textlen;
1460 }
1461
1462 len = cont.len - cont.cons;
1463 if (len > 0) {
1464 if (len+1 > size)
1465 len = size-1;
1466 memcpy(text + textlen, cont.buf + cont.cons, len);
1467 textlen += len;
1468 cont.cons = cont.len;
1469 }
1470
1471 if (cont.flushed) {
Kay Sieverseab07262012-07-16 18:35:30 -07001472 if (cont.flags & LOG_NEWLINE)
1473 text[textlen++] = '\n';
Kay Sievers084681d2012-06-28 09:38:53 +02001474 /* got everything, release buffer */
1475 cont.len = 0;
1476 }
1477 return textlen;
1478}
1479
Kay Sievers7ff95542012-05-03 02:29:13 +02001480asmlinkage int vprintk_emit(int facility, int level,
1481 const char *dict, size_t dictlen,
1482 const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
Kay Sievers7ff95542012-05-03 02:29:13 +02001484 static int recursion_bug;
Kay Sievers7ff95542012-05-03 02:29:13 +02001485 static char textbuf[LOG_LINE_MAX];
1486 char *text = textbuf;
Kay Sieversc313af12012-05-14 20:46:27 +02001487 size_t text_len;
Kay Sievers5becfb12012-07-09 12:15:42 -07001488 enum log_flags lflags = 0;
Nick Andrewac60ad72008-05-12 21:21:04 +02001489 unsigned long flags;
Ingo Molnar32a76002008-01-25 21:07:58 +01001490 int this_cpu;
Kay Sievers7ff95542012-05-03 02:29:13 +02001491 int printed_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001493 boot_delay_msec();
Dave Youngaf913222009-09-22 16:43:33 -07001494 printk_delay();
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 /* This stops the holder of console_sem just where we want him */
Peter Zijlstra1a9a8ae2011-06-07 11:17:30 +02001497 local_irq_save(flags);
Ingo Molnar32a76002008-01-25 21:07:58 +01001498 this_cpu = smp_processor_id();
1499
1500 /*
1501 * Ouch, printk recursed into itself!
1502 */
Kay Sievers7ff95542012-05-03 02:29:13 +02001503 if (unlikely(logbuf_cpu == this_cpu)) {
Ingo Molnar32a76002008-01-25 21:07:58 +01001504 /*
1505 * If a crash is occurring during printk() on this CPU,
1506 * then try to get the crash message out but make sure
1507 * we can't deadlock. Otherwise just return to avoid the
1508 * recursion and return - but flag the recursion so that
1509 * it can be printed at the next appropriate moment:
1510 */
Peter Zijlstra94d24fc2011-06-07 11:17:30 +02001511 if (!oops_in_progress && !lockdep_recursing(current)) {
Tejun Heo3b8945e2008-05-12 21:21:04 +02001512 recursion_bug = 1;
Ingo Molnar32a76002008-01-25 21:07:58 +01001513 goto out_restore_irqs;
1514 }
1515 zap_locks();
1516 }
1517
Ingo Molnara0f1ccf2006-07-03 00:24:58 -07001518 lockdep_off();
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001519 raw_spin_lock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02001520 logbuf_cpu = this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Tejun Heo3b8945e2008-05-12 21:21:04 +02001522 if (recursion_bug) {
Kay Sievers7ff95542012-05-03 02:29:13 +02001523 static const char recursion_msg[] =
1524 "BUG: recent printk recursion!";
1525
Tejun Heo3b8945e2008-05-12 21:21:04 +02001526 recursion_bug = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001527 printed_len += strlen(recursion_msg);
1528 /* emit KERN_CRIT message */
Kay Sievers5becfb12012-07-09 12:15:42 -07001529 log_store(0, 2, LOG_PREFIX|LOG_NEWLINE, 0,
Kay Sievers084681d2012-06-28 09:38:53 +02001530 NULL, 0, recursion_msg, printed_len);
Linus Torvalds5fd29d62009-06-16 10:57:02 -07001531 }
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 /*
Kay Sievers7ff95542012-05-03 02:29:13 +02001534 * The printf needs to come first; we need the syslog
1535 * prefix which might be passed-in as a parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 */
Kay Sieversc313af12012-05-14 20:46:27 +02001537 text_len = vscnprintf(text, sizeof(textbuf), fmt, args);
Nick Andrewac60ad72008-05-12 21:21:04 +02001538
Kay Sievers7ff95542012-05-03 02:29:13 +02001539 /* mark and strip a trailing newline */
Kay Sieversc313af12012-05-14 20:46:27 +02001540 if (text_len && text[text_len-1] == '\n') {
1541 text_len--;
Kay Sievers5becfb12012-07-09 12:15:42 -07001542 lflags |= LOG_NEWLINE;
Kay Sievers7ff95542012-05-03 02:29:13 +02001543 }
Kay Sievers9d90c8d2011-03-13 03:19:51 +01001544
Kay Sieversc313af12012-05-14 20:46:27 +02001545 /* strip syslog prefix and extract log level or control flags */
Kay Sievers7ff95542012-05-03 02:29:13 +02001546 if (text[0] == '<' && text[1] && text[2] == '>') {
1547 switch (text[1]) {
1548 case '0' ... '7':
1549 if (level == -1)
1550 level = text[1] - '0';
Kay Sievers7ff95542012-05-03 02:29:13 +02001551 case 'd': /* KERN_DEFAULT */
Kay Sievers5becfb12012-07-09 12:15:42 -07001552 lflags |= LOG_PREFIX;
Kay Sievers5c5d5ca2012-05-10 04:32:53 +02001553 case 'c': /* KERN_CONT */
Kay Sievers7ff95542012-05-03 02:29:13 +02001554 text += 3;
Kay Sieversc313af12012-05-14 20:46:27 +02001555 text_len -= 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001557 }
Nick Andrewac60ad72008-05-12 21:21:04 +02001558
Kay Sieversc313af12012-05-14 20:46:27 +02001559 if (level == -1)
1560 level = default_message_loglevel;
1561
Kay Sievers5becfb12012-07-09 12:15:42 -07001562 if (dict)
1563 lflags |= LOG_PREFIX|LOG_NEWLINE;
Kay Sievers7ff95542012-05-03 02:29:13 +02001564
Kay Sievers5becfb12012-07-09 12:15:42 -07001565 if (!(lflags & LOG_NEWLINE)) {
Kay Sievers084681d2012-06-28 09:38:53 +02001566 /*
1567 * Flush the conflicting buffer. An earlier newline was missing,
1568 * or another task also prints continuation lines.
1569 */
Kay Sievers5becfb12012-07-09 12:15:42 -07001570 if (cont.len && (lflags & LOG_PREFIX || cont.owner != current))
Kay Sieverseab07262012-07-16 18:35:30 -07001571 cont_flush(LOG_NEWLINE);
Kay Sieversc313af12012-05-14 20:46:27 +02001572
Kay Sievers084681d2012-06-28 09:38:53 +02001573 /* buffer line if possible, otherwise store it right away */
1574 if (!cont_add(facility, level, text, text_len))
Kay Sievers5becfb12012-07-09 12:15:42 -07001575 log_store(facility, level, lflags | LOG_CONT, 0,
Kay Sieversc313af12012-05-14 20:46:27 +02001576 dict, dictlen, text, text_len);
Kay Sievers084681d2012-06-28 09:38:53 +02001577 } else {
1578 bool stored = false;
1579
1580 /*
Steven Rostedtd3620822012-06-29 11:40:11 -04001581 * If an earlier newline was missing and it was the same task,
1582 * either merge it with the current buffer and flush, or if
1583 * there was a race with interrupts (prefix == true) then just
1584 * flush it out and store this line separately.
Kay Sievers084681d2012-06-28 09:38:53 +02001585 */
Kay Sievers084681d2012-06-28 09:38:53 +02001586 if (cont.len && cont.owner == current) {
Kay Sievers5becfb12012-07-09 12:15:42 -07001587 if (!(lflags & LOG_PREFIX))
Steven Rostedtd3620822012-06-29 11:40:11 -04001588 stored = cont_add(facility, level, text, text_len);
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
1592 if (!stored)
Kay Sievers5becfb12012-07-09 12:15:42 -07001593 log_store(facility, level, lflags, 0,
Kay Sievers084681d2012-06-28 09:38:53 +02001594 dict, dictlen, text, text_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 }
Kay Sievers084681d2012-06-28 09:38:53 +02001596 printed_len += text_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Linus Torvalds266c2e02008-03-24 19:25:08 -07001598 /*
Kay Sievers7ff95542012-05-03 02:29:13 +02001599 * Try to acquire and then immediately release the console semaphore.
1600 * The release will print out buffers and wake up /dev/kmsg and syslog()
1601 * users.
Linus Torvalds266c2e02008-03-24 19:25:08 -07001602 *
Kay Sievers7ff95542012-05-03 02:29:13 +02001603 * The console_trylock_for_printk() function will release 'logbuf_lock'
1604 * regardless of whether it actually gets the console semaphore or not.
Linus Torvalds266c2e02008-03-24 19:25:08 -07001605 */
Torben Hohnac751ef2011-01-25 15:07:35 -08001606 if (console_trylock_for_printk(this_cpu))
1607 console_unlock();
Michael Ellerman76a8ad22006-06-25 05:47:40 -07001608
Linus Torvalds266c2e02008-03-24 19:25:08 -07001609 lockdep_on();
Ingo Molnar32a76002008-01-25 21:07:58 +01001610out_restore_irqs:
Peter Zijlstra1a9a8ae2011-06-07 11:17:30 +02001611 local_irq_restore(flags);
Michael Ellerman76a8ad22006-06-25 05:47:40 -07001612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 return printed_len;
1614}
Kay Sievers7ff95542012-05-03 02:29:13 +02001615EXPORT_SYMBOL(vprintk_emit);
1616
1617asmlinkage int vprintk(const char *fmt, va_list args)
1618{
1619 return vprintk_emit(0, -1, NULL, 0, fmt, args);
1620}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621EXPORT_SYMBOL(vprintk);
1622
Kay Sievers7ff95542012-05-03 02:29:13 +02001623asmlinkage int printk_emit(int facility, int level,
1624 const char *dict, size_t dictlen,
1625 const char *fmt, ...)
1626{
1627 va_list args;
1628 int r;
1629
1630 va_start(args, fmt);
1631 r = vprintk_emit(facility, level, dict, dictlen, fmt, args);
1632 va_end(args);
1633
1634 return r;
1635}
1636EXPORT_SYMBOL(printk_emit);
1637
1638/**
1639 * printk - print a kernel message
1640 * @fmt: format string
1641 *
1642 * This is printk(). It can be called from any context. We want it to work.
1643 *
1644 * We try to grab the console_lock. If we succeed, it's easy - we log the
1645 * output and call the console drivers. If we fail to get the semaphore, we
1646 * place the output into the log buffer and return. The current holder of
1647 * the console_sem will notice the new output in console_unlock(); and will
1648 * send it to the consoles before releasing the lock.
1649 *
1650 * One effect of this deferred printing is that code which calls printk() and
1651 * then changes console_loglevel may break. This is because console_loglevel
1652 * is inspected when the actual printing occurs.
1653 *
1654 * See also:
1655 * printf(3)
1656 *
1657 * See the vsnprintf() documentation for format string extensions over C99.
1658 */
1659asmlinkage int printk(const char *fmt, ...)
1660{
1661 va_list args;
1662 int r;
1663
1664#ifdef CONFIG_KGDB_KDB
1665 if (unlikely(kdb_trap_printk)) {
1666 va_start(args, fmt);
1667 r = vkdb_printf(fmt, args);
1668 va_end(args);
1669 return r;
1670 }
1671#endif
1672 va_start(args, fmt);
1673 r = vprintk_emit(0, -1, NULL, 0, fmt, args);
1674 va_end(args);
1675
1676 return r;
1677}
1678EXPORT_SYMBOL(printk);
Kay Sievers7f3a7812012-05-09 01:37:51 +02001679
Kay Sievers96efedf2012-07-16 18:35:29 -07001680#else /* CONFIG_PRINTK */
Matt Mackalld59745c2005-05-01 08:59:02 -07001681
Kay Sievers70498252012-07-16 18:35:29 -07001682#define LOG_LINE_MAX 0
1683#define PREFIX_MAX 0
Kay Sievers7f3a7812012-05-09 01:37:51 +02001684#define LOG_LINE_MAX 0
Kay Sievers96efedf2012-07-16 18:35:29 -07001685static u64 syslog_seq;
1686static u32 syslog_idx;
Kay Sieverseab07262012-07-16 18:35:30 -07001687static u64 console_seq;
1688static u32 console_idx;
Kay Sievers96efedf2012-07-16 18:35:29 -07001689static enum log_flags syslog_prev;
1690static u64 log_first_seq;
1691static u32 log_first_idx;
1692static u64 log_next_seq;
Kay Sieverseab07262012-07-16 18:35:30 -07001693static enum log_flags console_prev;
Kay Sievers084681d2012-06-28 09:38:53 +02001694static struct cont {
1695 size_t len;
1696 size_t cons;
1697 u8 level;
1698 bool flushed:1;
1699} cont;
Kay Sievers7f3a7812012-05-09 01:37:51 +02001700static struct log *log_from_idx(u32 idx) { return NULL; }
1701static u32 log_next(u32 idx) { return 0; }
Kay Sievers7f3a7812012-05-09 01:37:51 +02001702static void call_console_drivers(int level, const char *text, size_t len) {}
Kay Sievers5becfb12012-07-09 12:15:42 -07001703static size_t msg_print_text(const struct log *msg, enum log_flags prev,
1704 bool syslog, char *buf, size_t size) { return 0; }
Kay Sievers084681d2012-06-28 09:38:53 +02001705static size_t cont_print_text(char *text, size_t size) { return 0; }
Matt Mackalld59745c2005-05-01 08:59:02 -07001706
Kay Sievers7f3a7812012-05-09 01:37:51 +02001707#endif /* CONFIG_PRINTK */
Matt Mackalld59745c2005-05-01 08:59:02 -07001708
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001709static int __add_preferred_console(char *name, int idx, char *options,
1710 char *brl_options)
1711{
1712 struct console_cmdline *c;
1713 int i;
1714
1715 /*
1716 * See if this tty is not yet registered, and
1717 * if we have a slot free.
1718 */
1719 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
1720 if (strcmp(console_cmdline[i].name, name) == 0 &&
1721 console_cmdline[i].index == idx) {
1722 if (!brl_options)
1723 selected_console = i;
1724 return 0;
1725 }
1726 if (i == MAX_CMDLINECONSOLES)
1727 return -E2BIG;
1728 if (!brl_options)
1729 selected_console = i;
1730 c = &console_cmdline[i];
1731 strlcpy(c->name, name, sizeof(c->name));
1732 c->options = options;
1733#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1734 c->brl_options = brl_options;
1735#endif
1736 c->index = idx;
1737 return 0;
1738}
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001739/*
1740 * Set up a list of consoles. Called from init/main.c
1741 */
1742static int __init console_setup(char *str)
1743{
Yinghai Lueaa944a2007-07-15 23:37:27 -07001744 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001745 char *s, *options, *brl_options = NULL;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001746 int idx;
1747
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001748#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1749 if (!memcmp(str, "brl,", 4)) {
1750 brl_options = "";
1751 str += 4;
1752 } else if (!memcmp(str, "brl=", 4)) {
1753 brl_options = str + 4;
1754 str = strchr(brl_options, ',');
1755 if (!str) {
1756 printk(KERN_ERR "need port name after brl=\n");
1757 return 1;
1758 }
1759 *(str++) = 0;
1760 }
1761#endif
1762
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001763 /*
1764 * Decode str into name, index, options.
1765 */
1766 if (str[0] >= '0' && str[0] <= '9') {
Yinghai Lueaa944a2007-07-15 23:37:27 -07001767 strcpy(buf, "ttyS");
1768 strncpy(buf + 4, str, sizeof(buf) - 5);
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001769 } else {
Yinghai Lueaa944a2007-07-15 23:37:27 -07001770 strncpy(buf, str, sizeof(buf) - 1);
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001771 }
Yinghai Lueaa944a2007-07-15 23:37:27 -07001772 buf[sizeof(buf) - 1] = 0;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001773 if ((options = strchr(str, ',')) != NULL)
1774 *(options++) = 0;
1775#ifdef __sparc__
1776 if (!strcmp(str, "ttya"))
Yinghai Lueaa944a2007-07-15 23:37:27 -07001777 strcpy(buf, "ttyS0");
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001778 if (!strcmp(str, "ttyb"))
Yinghai Lueaa944a2007-07-15 23:37:27 -07001779 strcpy(buf, "ttyS1");
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001780#endif
Yinghai Lueaa944a2007-07-15 23:37:27 -07001781 for (s = buf; *s; s++)
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001782 if ((*s >= '0' && *s <= '9') || *s == ',')
1783 break;
1784 idx = simple_strtoul(s, NULL, 10);
1785 *s = 0;
1786
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001787 __add_preferred_console(buf, idx, options, brl_options);
Markus Armbruster9e124fe2008-05-26 23:31:07 +01001788 console_set_on_cmdline = 1;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001789 return 1;
1790}
1791__setup("console=", console_setup);
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793/**
Matt Mackall3c0547b2005-05-16 21:53:47 -07001794 * add_preferred_console - add a device to the list of preferred consoles.
Martin Waitzddad86c2005-11-13 16:08:14 -08001795 * @name: device name
1796 * @idx: device index
1797 * @options: options for this console
Matt Mackall3c0547b2005-05-16 21:53:47 -07001798 *
1799 * The last preferred console added will be used for kernel messages
1800 * and stdin/out/err for init. Normally this is used by console_setup
1801 * above to handle user-supplied console arguments; however it can also
1802 * be used by arch-specific code either to override the user or more
1803 * commonly to provide a default console (ie from PROM variables) when
1804 * the user has not supplied one.
1805 */
David S. Millerfb445ee2007-12-29 01:19:49 -08001806int add_preferred_console(char *name, int idx, char *options)
Matt Mackall3c0547b2005-05-16 21:53:47 -07001807{
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001808 return __add_preferred_console(name, idx, options, NULL);
Matt Mackall3c0547b2005-05-16 21:53:47 -07001809}
1810
Daniel Ritzb6b1d872007-08-03 16:07:43 +02001811int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
Yinghai Lu18a8bd92007-07-15 23:37:59 -07001812{
1813 struct console_cmdline *c;
1814 int i;
1815
1816 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
1817 if (strcmp(console_cmdline[i].name, name) == 0 &&
1818 console_cmdline[i].index == idx) {
1819 c = &console_cmdline[i];
Markus Armbrusterf7352952008-04-30 00:54:52 -07001820 strlcpy(c->name, name_new, sizeof(c->name));
Yinghai Lu18a8bd92007-07-15 23:37:59 -07001821 c->name[sizeof(c->name) - 1] = 0;
1822 c->options = options;
1823 c->index = idx_new;
1824 return i;
1825 }
1826 /* not found */
1827 return -1;
1828}
1829
Rusty Russell2329abf2012-01-13 09:32:18 +10301830bool console_suspend_enabled = 1;
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001831EXPORT_SYMBOL(console_suspend_enabled);
1832
1833static int __init console_suspend_disable(char *str)
1834{
1835 console_suspend_enabled = 0;
1836 return 1;
1837}
1838__setup("no_console_suspend", console_suspend_disable);
Yanmin Zhang134620f2011-10-31 17:11:27 -07001839module_param_named(console_suspend, console_suspend_enabled,
1840 bool, S_IRUGO | S_IWUSR);
1841MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
1842 " and hibernate operations");
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001843
Matt Mackall3c0547b2005-05-16 21:53:47 -07001844/**
Linus Torvalds557240b2006-06-19 18:16:01 -07001845 * suspend_console - suspend the console subsystem
1846 *
1847 * This disables printk() while we go into suspend states
1848 */
1849void suspend_console(void)
1850{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001851 if (!console_suspend_enabled)
1852 return;
Pavel Machek0d630812008-07-23 21:28:32 -07001853 printk("Suspending console(s) (use no_console_suspend to debug)\n");
Torben Hohnac751ef2011-01-25 15:07:35 -08001854 console_lock();
Linus Torvalds557240b2006-06-19 18:16:01 -07001855 console_suspended = 1;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001856 up(&console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -07001857}
1858
1859void resume_console(void)
1860{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001861 if (!console_suspend_enabled)
1862 return;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001863 down(&console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -07001864 console_suspended = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08001865 console_unlock();
Linus Torvalds557240b2006-06-19 18:16:01 -07001866}
1867
1868/**
Kevin Cernekee034260d2010-06-03 22:11:25 -07001869 * console_cpu_notify - print deferred console messages after CPU hotplug
1870 * @self: notifier struct
1871 * @action: CPU hotplug event
1872 * @hcpu: unused
1873 *
1874 * If printk() is called from a CPU that is not online yet, the messages
1875 * will be spooled but will not show up on the console. This function is
1876 * called when a new CPU comes online (or fails to come up), and ensures
1877 * that any such output gets printed.
1878 */
1879static int __cpuinit console_cpu_notify(struct notifier_block *self,
1880 unsigned long action, void *hcpu)
1881{
1882 switch (action) {
1883 case CPU_ONLINE:
1884 case CPU_DEAD:
1885 case CPU_DYING:
1886 case CPU_DOWN_FAILED:
1887 case CPU_UP_CANCELED:
Torben Hohnac751ef2011-01-25 15:07:35 -08001888 console_lock();
1889 console_unlock();
Kevin Cernekee034260d2010-06-03 22:11:25 -07001890 }
1891 return NOTIFY_OK;
1892}
1893
1894/**
Torben Hohnac751ef2011-01-25 15:07:35 -08001895 * console_lock - lock the console system for exclusive use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 *
Torben Hohnac751ef2011-01-25 15:07:35 -08001897 * Acquires a lock which guarantees that the caller has
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 * exclusive access to the console system and the console_drivers list.
1899 *
1900 * Can sleep, returns nothing.
1901 */
Torben Hohnac751ef2011-01-25 15:07:35 -08001902void console_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
Eric Sesterhenn8abd8e22006-04-01 01:21:17 +02001904 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 down(&console_sem);
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001906 if (console_suspended)
1907 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 console_locked = 1;
1909 console_may_schedule = 1;
1910}
Torben Hohnac751ef2011-01-25 15:07:35 -08001911EXPORT_SYMBOL(console_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Torben Hohnac751ef2011-01-25 15:07:35 -08001913/**
1914 * console_trylock - try to lock the console system for exclusive use.
1915 *
1916 * Tried to acquire a lock which guarantees that the caller has
1917 * exclusive access to the console system and the console_drivers list.
1918 *
1919 * returns 1 on success, and 0 on failure to acquire the lock.
1920 */
1921int console_trylock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
1923 if (down_trylock(&console_sem))
Torben Hohnac751ef2011-01-25 15:07:35 -08001924 return 0;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001925 if (console_suspended) {
1926 up(&console_sem);
Torben Hohnac751ef2011-01-25 15:07:35 -08001927 return 0;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 console_locked = 1;
1930 console_may_schedule = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08001931 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932}
Torben Hohnac751ef2011-01-25 15:07:35 -08001933EXPORT_SYMBOL(console_trylock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935int is_console_locked(void)
1936{
1937 return console_locked;
1938}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +01001940/*
Kay Sievers7ff95542012-05-03 02:29:13 +02001941 * Delayed printk version, for scheduler-internal messages:
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +01001942 */
1943#define PRINTK_BUF_SIZE 512
1944
1945#define PRINTK_PENDING_WAKEUP 0x01
1946#define PRINTK_PENDING_SCHED 0x02
1947
Peter Zijlstrab845b512008-08-08 21:47:09 +02001948static DEFINE_PER_CPU(int, printk_pending);
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +01001949static DEFINE_PER_CPU(char [PRINTK_BUF_SIZE], printk_sched_buf);
Peter Zijlstrab845b512008-08-08 21:47:09 +02001950
1951void printk_tick(void)
1952{
Eric Dumazet40dc11f2010-11-26 17:22:16 +01001953 if (__this_cpu_read(printk_pending)) {
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +01001954 int pending = __this_cpu_xchg(printk_pending, 0);
1955 if (pending & PRINTK_PENDING_SCHED) {
1956 char *buf = __get_cpu_var(printk_sched_buf);
1957 printk(KERN_WARNING "[sched_delayed] %s", buf);
1958 }
1959 if (pending & PRINTK_PENDING_WAKEUP)
1960 wake_up_interruptible(&log_wait);
Peter Zijlstrab845b512008-08-08 21:47:09 +02001961 }
1962}
1963
1964int printk_needs_cpu(int cpu)
1965{
Eric Dumazet40dc11f2010-11-26 17:22:16 +01001966 if (cpu_is_offline(cpu))
Heiko Carstens61ab25442010-11-26 13:00:59 +01001967 printk_tick();
Eric Dumazet40dc11f2010-11-26 17:22:16 +01001968 return __this_cpu_read(printk_pending);
Peter Zijlstrab845b512008-08-08 21:47:09 +02001969}
1970
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08001971void wake_up_klogd(void)
1972{
Peter Zijlstrab845b512008-08-08 21:47:09 +02001973 if (waitqueue_active(&log_wait))
Peter Zijlstra3ccf3e82012-02-27 10:47:00 +01001974 this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP);
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08001975}
1976
Kay Sieverseab07262012-07-16 18:35:30 -07001977static void console_cont_flush(char *text, size_t size)
1978{
1979 unsigned long flags;
1980 size_t len;
1981
1982 raw_spin_lock_irqsave(&logbuf_lock, flags);
1983
1984 if (!cont.len)
1985 goto out;
1986
1987 /*
1988 * We still queue earlier records, likely because the console was
1989 * busy. The earlier ones need to be printed before this one, we
1990 * did not flush any fragment so far, so just let it queue up.
1991 */
1992 if (console_seq < log_next_seq && !cont.cons)
1993 goto out;
1994
1995 len = cont_print_text(text, size);
1996 raw_spin_unlock(&logbuf_lock);
1997 stop_critical_timings();
1998 call_console_drivers(cont.level, text, len);
1999 start_critical_timings();
2000 local_irq_restore(flags);
2001 return;
2002out:
2003 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2004}
Kay Sievers7ff95542012-05-03 02:29:13 +02002005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006/**
Torben Hohnac751ef2011-01-25 15:07:35 -08002007 * console_unlock - unlock the console system
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002009 * Releases the console_lock which the caller holds on the console system
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 * and the console driver list.
2011 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002012 * While the console_lock was held, console output may have been buffered
2013 * by printk(). If this is the case, console_unlock(); emits
2014 * the output prior to releasing the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 *
Kay Sievers7f3a7812012-05-09 01:37:51 +02002016 * If there is output waiting, we wake /dev/kmsg and syslog() users.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002018 * console_unlock(); may be called from any context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 */
Torben Hohnac751ef2011-01-25 15:07:35 -08002020void console_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021{
Kay Sievers70498252012-07-16 18:35:29 -07002022 static char text[LOG_LINE_MAX + PREFIX_MAX];
Kay Sievers7ff95542012-05-03 02:29:13 +02002023 static u64 seen_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 unsigned long flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02002025 bool wake_klogd = false;
2026 bool retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Linus Torvalds557240b2006-06-19 18:16:01 -07002028 if (console_suspended) {
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01002029 up(&console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -07002030 return;
2031 }
Antonino A. Daplas78944e52006-08-05 12:14:16 -07002032
2033 console_may_schedule = 0;
2034
Kay Sievers084681d2012-06-28 09:38:53 +02002035 /* flush buffered message fragment immediately to console */
Kay Sieverseab07262012-07-16 18:35:30 -07002036 console_cont_flush(text, sizeof(text));
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002037again:
Kay Sievers7ff95542012-05-03 02:29:13 +02002038 for (;;) {
2039 struct log *msg;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02002040 size_t len;
Kay Sievers7ff95542012-05-03 02:29:13 +02002041 int level;
2042
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002043 raw_spin_lock_irqsave(&logbuf_lock, flags);
Kay Sievers7ff95542012-05-03 02:29:13 +02002044 if (seen_seq != log_next_seq) {
2045 wake_klogd = true;
2046 seen_seq = log_next_seq;
2047 }
2048
2049 if (console_seq < log_first_seq) {
2050 /* messages are gone, move to first one */
2051 console_seq = log_first_seq;
2052 console_idx = log_first_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002053 console_prev = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02002054 }
Kay Sievers084681d2012-06-28 09:38:53 +02002055skip:
Kay Sievers7ff95542012-05-03 02:29:13 +02002056 if (console_seq == log_next_seq)
2057 break;
2058
2059 msg = log_from_idx(console_idx);
Kay Sievers084681d2012-06-28 09:38:53 +02002060 if (msg->flags & LOG_NOCONS) {
2061 /*
2062 * Skip record we have buffered and already printed
2063 * directly to the console when we received it.
2064 */
2065 console_idx = log_next(console_idx);
2066 console_seq++;
Kay Sievers68b65072012-07-06 09:50:09 -07002067 /*
2068 * We will get here again when we register a new
2069 * CON_PRINTBUFFER console. Clear the flag so we
2070 * will properly dump everything later.
2071 */
2072 msg->flags &= ~LOG_NOCONS;
Kay Sieverseab07262012-07-16 18:35:30 -07002073 console_prev = msg->flags;
Kay Sievers084681d2012-06-28 09:38:53 +02002074 goto skip;
2075 }
Kay Sievers649e6ee2012-05-10 04:30:45 +02002076
Kay Sievers084681d2012-06-28 09:38:53 +02002077 level = msg->level;
Kay Sievers5becfb12012-07-09 12:15:42 -07002078 len = msg_print_text(msg, console_prev, false,
2079 text, sizeof(text));
Kay Sievers7ff95542012-05-03 02:29:13 +02002080 console_idx = log_next(console_idx);
2081 console_seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07002082 console_prev = msg->flags;
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002083 raw_spin_unlock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02002084
Steven Rostedt81d68a92008-05-12 21:20:42 +02002085 stop_critical_timings(); /* don't trace print latency */
Kay Sievers7ff95542012-05-03 02:29:13 +02002086 call_console_drivers(level, text, len);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002087 start_critical_timings();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 local_irq_restore(flags);
2089 }
2090 console_locked = 0;
Feng Tangfe3d8ad2011-03-22 16:34:21 -07002091
2092 /* Release the exclusive_console once it is used */
2093 if (unlikely(exclusive_console))
2094 exclusive_console = NULL;
2095
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002096 raw_spin_unlock(&logbuf_lock);
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002097
Peter Zijlstra0b5e1c52011-06-07 11:15:33 +02002098 up(&console_sem);
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002099
2100 /*
2101 * Someone could have filled up the buffer again, so re-check if there's
2102 * something to flush. In case we cannot trylock the console_sem again,
2103 * there's a new owner and the console_unlock() from them will do the
2104 * flush, no worries.
2105 */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002106 raw_spin_lock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02002107 retry = console_seq != log_next_seq;
Peter Zijlstra09dc3cf2011-12-08 14:34:13 -08002108 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2109
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002110 if (retry && console_trylock())
2111 goto again;
2112
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08002113 if (wake_klogd)
2114 wake_up_klogd();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115}
Torben Hohnac751ef2011-01-25 15:07:35 -08002116EXPORT_SYMBOL(console_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Martin Waitzddad86c2005-11-13 16:08:14 -08002118/**
2119 * console_conditional_schedule - yield the CPU if required
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 *
2121 * If the console code is currently allowed to sleep, and
2122 * if this CPU should yield the CPU to another task, do
2123 * so here.
2124 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002125 * Must be called within console_lock();.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 */
2127void __sched console_conditional_schedule(void)
2128{
2129 if (console_may_schedule)
2130 cond_resched();
2131}
2132EXPORT_SYMBOL(console_conditional_schedule);
2133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134void console_unblank(void)
2135{
2136 struct console *c;
2137
2138 /*
2139 * console_unblank can no longer be called in interrupt context unless
2140 * oops_in_progress is set to 1..
2141 */
2142 if (oops_in_progress) {
2143 if (down_trylock(&console_sem) != 0)
2144 return;
2145 } else
Torben Hohnac751ef2011-01-25 15:07:35 -08002146 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 console_locked = 1;
2149 console_may_schedule = 0;
Robin Getz4d091612009-07-01 21:08:37 -04002150 for_each_console(c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 if ((c->flags & CON_ENABLED) && c->unblank)
2152 c->unblank();
Torben Hohnac751ef2011-01-25 15:07:35 -08002153 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156/*
2157 * Return the console tty driver structure and its associated index
2158 */
2159struct tty_driver *console_device(int *index)
2160{
2161 struct console *c;
2162 struct tty_driver *driver = NULL;
2163
Torben Hohnac751ef2011-01-25 15:07:35 -08002164 console_lock();
Robin Getz4d091612009-07-01 21:08:37 -04002165 for_each_console(c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 if (!c->device)
2167 continue;
2168 driver = c->device(c, index);
2169 if (driver)
2170 break;
2171 }
Torben Hohnac751ef2011-01-25 15:07:35 -08002172 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 return driver;
2174}
2175
2176/*
2177 * Prevent further output on the passed console device so that (for example)
2178 * serial drivers can disable console output before suspending a port, and can
2179 * re-enable output afterwards.
2180 */
2181void console_stop(struct console *console)
2182{
Torben Hohnac751ef2011-01-25 15:07:35 -08002183 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 console->flags &= ~CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08002185 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186}
2187EXPORT_SYMBOL(console_stop);
2188
2189void console_start(struct console *console)
2190{
Torben Hohnac751ef2011-01-25 15:07:35 -08002191 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 console->flags |= CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08002193 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194}
2195EXPORT_SYMBOL(console_start);
2196
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07002197static int __read_mostly keep_bootcon;
2198
2199static int __init keep_bootcon_setup(char *str)
2200{
2201 keep_bootcon = 1;
2202 printk(KERN_INFO "debug: skip boot console de-registration.\n");
2203
2204 return 0;
2205}
2206
2207early_param("keep_bootcon", keep_bootcon_setup);
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209/*
2210 * The console driver calls this routine during kernel initialization
2211 * to register the console printing procedure with printk() and to
2212 * print any messages that were printed by the kernel before the
2213 * console driver was initialized.
Robin Getz4d091612009-07-01 21:08:37 -04002214 *
2215 * This can happen pretty early during the boot process (because of
2216 * early_printk) - sometimes before setup_arch() completes - be careful
2217 * of what kernel features are used - they may not be initialised yet.
2218 *
2219 * There are two types of consoles - bootconsoles (early_printk) and
2220 * "real" consoles (everything which is not a bootconsole) which are
2221 * handled differently.
2222 * - Any number of bootconsoles can be registered at any time.
2223 * - As soon as a "real" console is registered, all bootconsoles
2224 * will be unregistered automatically.
2225 * - Once a "real" console is registered, any attempt to register a
2226 * bootconsoles will be rejected
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 */
Robin Getz4d091612009-07-01 21:08:37 -04002228void register_console(struct console *newcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
Jesper Juhl40dc5652005-10-30 15:02:46 -08002230 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 unsigned long flags;
Robin Getz4d091612009-07-01 21:08:37 -04002232 struct console *bcon = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Robin Getz4d091612009-07-01 21:08:37 -04002234 /*
2235 * before we register a new CON_BOOT console, make sure we don't
2236 * already have a valid console
2237 */
2238 if (console_drivers && newcon->flags & CON_BOOT) {
2239 /* find the last or real console */
2240 for_each_console(bcon) {
2241 if (!(bcon->flags & CON_BOOT)) {
2242 printk(KERN_INFO "Too late to register bootconsole %s%d\n",
2243 newcon->name, newcon->index);
2244 return;
2245 }
2246 }
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002247 }
2248
Robin Getz4d091612009-07-01 21:08:37 -04002249 if (console_drivers && console_drivers->flags & CON_BOOT)
2250 bcon = console_drivers;
2251
2252 if (preferred_console < 0 || bcon || !console_drivers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 preferred_console = selected_console;
2254
Robin Getz4d091612009-07-01 21:08:37 -04002255 if (newcon->early_setup)
2256 newcon->early_setup();
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 /*
2259 * See if we want to use this console driver. If we
2260 * didn't select a console we take the first one
2261 * that registers here.
2262 */
2263 if (preferred_console < 0) {
Robin Getz4d091612009-07-01 21:08:37 -04002264 if (newcon->index < 0)
2265 newcon->index = 0;
2266 if (newcon->setup == NULL ||
2267 newcon->setup(newcon, NULL) == 0) {
2268 newcon->flags |= CON_ENABLED;
2269 if (newcon->device) {
2270 newcon->flags |= CON_CONSDEV;
Jan Kiszkacd3a1b82008-05-12 21:21:04 +02002271 preferred_console = 0;
2272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
2274 }
2275
2276 /*
2277 * See if this console matches one we selected on
2278 * the command line.
2279 */
Jesper Juhl40dc5652005-10-30 15:02:46 -08002280 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
2281 i++) {
Robin Getz4d091612009-07-01 21:08:37 -04002282 if (strcmp(console_cmdline[i].name, newcon->name) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 continue;
Robin Getz4d091612009-07-01 21:08:37 -04002284 if (newcon->index >= 0 &&
2285 newcon->index != console_cmdline[i].index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 continue;
Robin Getz4d091612009-07-01 21:08:37 -04002287 if (newcon->index < 0)
2288 newcon->index = console_cmdline[i].index;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002289#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
2290 if (console_cmdline[i].brl_options) {
Robin Getz4d091612009-07-01 21:08:37 -04002291 newcon->flags |= CON_BRL;
2292 braille_register_console(newcon,
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002293 console_cmdline[i].index,
2294 console_cmdline[i].options,
2295 console_cmdline[i].brl_options);
2296 return;
2297 }
2298#endif
Robin Getz4d091612009-07-01 21:08:37 -04002299 if (newcon->setup &&
2300 newcon->setup(newcon, console_cmdline[i].options) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 break;
Robin Getz4d091612009-07-01 21:08:37 -04002302 newcon->flags |= CON_ENABLED;
2303 newcon->index = console_cmdline[i].index;
Greg Edwardsab4af032005-06-23 00:09:05 -07002304 if (i == selected_console) {
Robin Getz4d091612009-07-01 21:08:37 -04002305 newcon->flags |= CON_CONSDEV;
Greg Edwardsab4af032005-06-23 00:09:05 -07002306 preferred_console = selected_console;
2307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 break;
2309 }
2310
Robin Getz4d091612009-07-01 21:08:37 -04002311 if (!(newcon->flags & CON_ENABLED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 return;
2313
Robin Getz8259cf42009-07-09 13:08:37 -04002314 /*
2315 * If we have a bootconsole, and are switching to a real console,
2316 * don't print everything out again, since when the boot console, and
2317 * the real console are the same physical device, it's annoying to
2318 * see the beginning boot messages twice
2319 */
2320 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
Robin Getz4d091612009-07-01 21:08:37 -04002321 newcon->flags &= ~CON_PRINTBUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 /*
2324 * Put this console in the list - keep the
2325 * preferred driver at the head of the list.
2326 */
Torben Hohnac751ef2011-01-25 15:07:35 -08002327 console_lock();
Robin Getz4d091612009-07-01 21:08:37 -04002328 if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
2329 newcon->next = console_drivers;
2330 console_drivers = newcon;
2331 if (newcon->next)
2332 newcon->next->flags &= ~CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 } else {
Robin Getz4d091612009-07-01 21:08:37 -04002334 newcon->next = console_drivers->next;
2335 console_drivers->next = newcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 }
Robin Getz4d091612009-07-01 21:08:37 -04002337 if (newcon->flags & CON_PRINTBUFFER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 /*
Torben Hohnac751ef2011-01-25 15:07:35 -08002339 * console_unlock(); will print out the buffered messages
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 * for us.
2341 */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002342 raw_spin_lock_irqsave(&logbuf_lock, flags);
Kay Sievers7ff95542012-05-03 02:29:13 +02002343 console_seq = syslog_seq;
2344 console_idx = syslog_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002345 console_prev = syslog_prev;
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002346 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
Feng Tangfe3d8ad2011-03-22 16:34:21 -07002347 /*
2348 * We're about to replay the log buffer. Only do this to the
2349 * just-registered console to avoid excessive message spam to
2350 * the already-registered consoles.
2351 */
2352 exclusive_console = newcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 }
Torben Hohnac751ef2011-01-25 15:07:35 -08002354 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01002355 console_sysfs_notify();
Robin Getz8259cf42009-07-09 13:08:37 -04002356
2357 /*
2358 * By unregistering the bootconsoles after we enable the real console
2359 * we get the "console xxx enabled" message on all the consoles -
2360 * boot consoles, real consoles, etc - this is to ensure that end
2361 * users know there might be something in the kernel's log buffer that
2362 * went to the bootconsole (that they do not see on the real console)
2363 */
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07002364 if (bcon &&
2365 ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
2366 !keep_bootcon) {
Robin Getz8259cf42009-07-09 13:08:37 -04002367 /* we need to iterate through twice, to make sure we print
2368 * everything out, before we unregister the console(s)
2369 */
2370 printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
2371 newcon->name, newcon->index);
2372 for_each_console(bcon)
2373 if (bcon->flags & CON_BOOT)
2374 unregister_console(bcon);
2375 } else {
2376 printk(KERN_INFO "%sconsole [%s%d] enabled\n",
2377 (newcon->flags & CON_BOOT) ? "boot" : "" ,
2378 newcon->name, newcon->index);
2379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380}
2381EXPORT_SYMBOL(register_console);
2382
Jesper Juhl40dc5652005-10-30 15:02:46 -08002383int unregister_console(struct console *console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384{
Jesper Juhl40dc5652005-10-30 15:02:46 -08002385 struct console *a, *b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 int res = 1;
2387
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002388#ifdef CONFIG_A11Y_BRAILLE_CONSOLE
2389 if (console->flags & CON_BRL)
2390 return braille_unregister_console(console);
2391#endif
2392
Torben Hohnac751ef2011-01-25 15:07:35 -08002393 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 if (console_drivers == console) {
2395 console_drivers=console->next;
2396 res = 0;
Benjamin Herrenschmidte9b15b52005-11-23 13:37:44 -08002397 } else if (console_drivers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 for (a=console_drivers->next, b=console_drivers ;
2399 a; b=a, a=b->next) {
2400 if (a == console) {
2401 b->next = a->next;
2402 res = 0;
2403 break;
Jesper Juhl40dc5652005-10-30 15:02:46 -08002404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 }
2406 }
Jesper Juhl40dc5652005-10-30 15:02:46 -08002407
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002408 /*
Greg Edwardsab4af032005-06-23 00:09:05 -07002409 * If this isn't the last console and it has CON_CONSDEV set, we
2410 * need to set it on the next preferred console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 */
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002412 if (console_drivers != NULL && console->flags & CON_CONSDEV)
Greg Edwardsab4af032005-06-23 00:09:05 -07002413 console_drivers->flags |= CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Torben Hohnac751ef2011-01-25 15:07:35 -08002415 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01002416 console_sysfs_notify();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 return res;
2418}
2419EXPORT_SYMBOL(unregister_console);
Matt Mackalld59745c2005-05-01 08:59:02 -07002420
Kevin Cernekee034260d2010-06-03 22:11:25 -07002421static int __init printk_late_init(void)
Robin Getz0c5564b2007-08-20 15:22:47 -04002422{
Robin Getz4d091612009-07-01 21:08:37 -04002423 struct console *con;
2424
2425 for_each_console(con) {
Nishanth Aravamudan4c30c6f2011-08-25 15:59:11 -07002426 if (!keep_bootcon && con->flags & CON_BOOT) {
Robin Getzcb00e992007-08-21 23:14:58 -04002427 printk(KERN_INFO "turn off boot console %s%d\n",
Robin Getz4d091612009-07-01 21:08:37 -04002428 con->name, con->index);
Sonic Zhang42c2c8c2009-08-06 15:58:11 -07002429 unregister_console(con);
Robin Getzcb00e992007-08-21 23:14:58 -04002430 }
Robin Getz0c5564b2007-08-20 15:22:47 -04002431 }
Kevin Cernekee034260d2010-06-03 22:11:25 -07002432 hotcpu_notifier(console_cpu_notify, 0);
Robin Getz0c5564b2007-08-20 15:22:47 -04002433 return 0;
2434}
Kevin Cernekee034260d2010-06-03 22:11:25 -07002435late_initcall(printk_late_init);
Robin Getz0c5564b2007-08-20 15:22:47 -04002436
Joe Perches7ef3d2f2008-02-08 04:21:25 -08002437#if defined CONFIG_PRINTK
Dave Young717115e2008-07-25 01:45:58 -07002438
Peter Zijlstra600e1452012-03-15 12:35:37 +01002439int printk_sched(const char *fmt, ...)
2440{
2441 unsigned long flags;
2442 va_list args;
2443 char *buf;
2444 int r;
2445
2446 local_irq_save(flags);
2447 buf = __get_cpu_var(printk_sched_buf);
2448
2449 va_start(args, fmt);
2450 r = vsnprintf(buf, PRINTK_BUF_SIZE, fmt, args);
2451 va_end(args);
2452
2453 __this_cpu_or(printk_pending, PRINTK_PENDING_SCHED);
2454 local_irq_restore(flags);
2455
2456 return r;
2457}
2458
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459/*
2460 * printk rate limiting, lifted from the networking subsystem.
2461 *
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07002462 * This enforces a rate limit: not more than 10 kernel messages
2463 * every 5s to make a denial-of-service attack impossible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 */
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07002465DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
2466
Christian Borntraeger5c828712009-10-23 14:58:11 +02002467int __printk_ratelimit(const char *func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468{
Christian Borntraeger5c828712009-10-23 14:58:11 +02002469 return ___ratelimit(&printk_ratelimit_state, func);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470}
Christian Borntraeger5c828712009-10-23 14:58:11 +02002471EXPORT_SYMBOL(__printk_ratelimit);
Andrew Mortonf46c4832006-11-02 22:07:16 -08002472
2473/**
2474 * printk_timed_ratelimit - caller-controlled printk ratelimiting
2475 * @caller_jiffies: pointer to caller's state
2476 * @interval_msecs: minimum interval between prints
2477 *
2478 * printk_timed_ratelimit() returns true if more than @interval_msecs
2479 * milliseconds have elapsed since the last time printk_timed_ratelimit()
2480 * returned true.
2481 */
2482bool printk_timed_ratelimit(unsigned long *caller_jiffies,
2483 unsigned int interval_msecs)
2484{
Guillaume Knispelf2d28a22009-03-17 16:18:42 +01002485 if (*caller_jiffies == 0
2486 || !time_in_range(jiffies, *caller_jiffies,
2487 *caller_jiffies
2488 + msecs_to_jiffies(interval_msecs))) {
2489 *caller_jiffies = jiffies;
Andrew Mortonf46c4832006-11-02 22:07:16 -08002490 return true;
2491 }
2492 return false;
2493}
2494EXPORT_SYMBOL(printk_timed_ratelimit);
Simon Kagstrom456b5652009-10-16 14:09:18 +02002495
2496static DEFINE_SPINLOCK(dump_list_lock);
2497static LIST_HEAD(dump_list);
2498
2499/**
2500 * kmsg_dump_register - register a kernel log dumper.
Randy Dunlap64855362009-12-17 15:27:27 -08002501 * @dumper: pointer to the kmsg_dumper structure
Simon Kagstrom456b5652009-10-16 14:09:18 +02002502 *
2503 * Adds a kernel log dumper to the system. The dump callback in the
2504 * structure will be called when the kernel oopses or panics and must be
2505 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
2506 */
2507int kmsg_dump_register(struct kmsg_dumper *dumper)
2508{
2509 unsigned long flags;
2510 int err = -EBUSY;
2511
2512 /* The dump callback needs to be set */
2513 if (!dumper->dump)
2514 return -EINVAL;
2515
2516 spin_lock_irqsave(&dump_list_lock, flags);
2517 /* Don't allow registering multiple times */
2518 if (!dumper->registered) {
2519 dumper->registered = 1;
Huang Yingfb842b02011-01-12 16:59:43 -08002520 list_add_tail_rcu(&dumper->list, &dump_list);
Simon Kagstrom456b5652009-10-16 14:09:18 +02002521 err = 0;
2522 }
2523 spin_unlock_irqrestore(&dump_list_lock, flags);
2524
2525 return err;
2526}
2527EXPORT_SYMBOL_GPL(kmsg_dump_register);
2528
2529/**
2530 * kmsg_dump_unregister - unregister a kmsg dumper.
Randy Dunlap64855362009-12-17 15:27:27 -08002531 * @dumper: pointer to the kmsg_dumper structure
Simon Kagstrom456b5652009-10-16 14:09:18 +02002532 *
2533 * Removes a dump device from the system. Returns zero on success and
2534 * %-EINVAL otherwise.
2535 */
2536int kmsg_dump_unregister(struct kmsg_dumper *dumper)
2537{
2538 unsigned long flags;
2539 int err = -EINVAL;
2540
2541 spin_lock_irqsave(&dump_list_lock, flags);
2542 if (dumper->registered) {
2543 dumper->registered = 0;
Huang Yingfb842b02011-01-12 16:59:43 -08002544 list_del_rcu(&dumper->list);
Simon Kagstrom456b5652009-10-16 14:09:18 +02002545 err = 0;
2546 }
2547 spin_unlock_irqrestore(&dump_list_lock, flags);
Huang Yingfb842b02011-01-12 16:59:43 -08002548 synchronize_rcu();
Simon Kagstrom456b5652009-10-16 14:09:18 +02002549
2550 return err;
2551}
2552EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
2553
Kay Sievers7ff95542012-05-03 02:29:13 +02002554static bool always_kmsg_dump;
2555module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR);
2556
Simon Kagstrom456b5652009-10-16 14:09:18 +02002557/**
2558 * kmsg_dump - dump kernel log to kernel message dumpers.
2559 * @reason: the reason (oops, panic etc) for dumping
2560 *
Kay Sieverse2ae7152012-06-15 14:07:51 +02002561 * Call each of the registered dumper's dump() callback, which can
2562 * retrieve the kmsg records with kmsg_dump_get_line() or
2563 * kmsg_dump_get_buffer().
Simon Kagstrom456b5652009-10-16 14:09:18 +02002564 */
2565void kmsg_dump(enum kmsg_dump_reason reason)
2566{
Simon Kagstrom456b5652009-10-16 14:09:18 +02002567 struct kmsg_dumper *dumper;
Simon Kagstrom456b5652009-10-16 14:09:18 +02002568 unsigned long flags;
2569
Matthew Garrettc22ab332012-03-05 14:59:10 -08002570 if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
2571 return;
2572
Huang Yingfb842b02011-01-12 16:59:43 -08002573 rcu_read_lock();
Kay Sieverse2ae7152012-06-15 14:07:51 +02002574 list_for_each_entry_rcu(dumper, &dump_list, list) {
2575 if (dumper->max_reason && reason > dumper->max_reason)
2576 continue;
2577
2578 /* initialize iterator with data about the stored records */
2579 dumper->active = true;
2580
2581 raw_spin_lock_irqsave(&logbuf_lock, flags);
2582 dumper->cur_seq = clear_seq;
2583 dumper->cur_idx = clear_idx;
2584 dumper->next_seq = log_next_seq;
2585 dumper->next_idx = log_next_idx;
2586 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2587
2588 /* invoke dumper which will iterate over records */
2589 dumper->dump(dumper, reason);
2590
2591 /* reset iterator */
2592 dumper->active = false;
2593 }
Huang Yingfb842b02011-01-12 16:59:43 -08002594 rcu_read_unlock();
Simon Kagstrom456b5652009-10-16 14:09:18 +02002595}
Kay Sieverse2ae7152012-06-15 14:07:51 +02002596
2597/**
Anton Vorontsov533827c2012-07-20 17:28:07 -07002598 * kmsg_dump_get_line_nolock - retrieve one kmsg log line (unlocked version)
2599 * @dumper: registered kmsg dumper
2600 * @syslog: include the "<4>" prefixes
2601 * @line: buffer to copy the line to
2602 * @size: maximum size of the buffer
2603 * @len: length of line placed into buffer
2604 *
2605 * Start at the beginning of the kmsg buffer, with the oldest kmsg
2606 * record, and copy one record into the provided buffer.
2607 *
2608 * Consecutive calls will return the next available record moving
2609 * towards the end of the buffer with the youngest messages.
2610 *
2611 * A return value of FALSE indicates that there are no more records to
2612 * read.
2613 *
2614 * The function is similar to kmsg_dump_get_line(), but grabs no locks.
2615 */
2616bool kmsg_dump_get_line_nolock(struct kmsg_dumper *dumper, bool syslog,
2617 char *line, size_t size, size_t *len)
2618{
2619 struct log *msg;
2620 size_t l = 0;
2621 bool ret = false;
2622
2623 if (!dumper->active)
2624 goto out;
2625
2626 if (dumper->cur_seq < log_first_seq) {
2627 /* messages are gone, move to first available one */
2628 dumper->cur_seq = log_first_seq;
2629 dumper->cur_idx = log_first_idx;
2630 }
2631
2632 /* last entry */
2633 if (dumper->cur_seq >= log_next_seq)
2634 goto out;
2635
2636 msg = log_from_idx(dumper->cur_idx);
2637 l = msg_print_text(msg, 0, syslog, line, size);
2638
2639 dumper->cur_idx = log_next(dumper->cur_idx);
2640 dumper->cur_seq++;
2641 ret = true;
2642out:
2643 if (len)
2644 *len = l;
2645 return ret;
2646}
2647
2648/**
Kay Sieverse2ae7152012-06-15 14:07:51 +02002649 * kmsg_dump_get_line - retrieve one kmsg log line
2650 * @dumper: registered kmsg dumper
2651 * @syslog: include the "<4>" prefixes
2652 * @line: buffer to copy the line to
2653 * @size: maximum size of the buffer
2654 * @len: length of line placed into buffer
2655 *
2656 * Start at the beginning of the kmsg buffer, with the oldest kmsg
2657 * record, and copy one record into the provided buffer.
2658 *
2659 * Consecutive calls will return the next available record moving
2660 * towards the end of the buffer with the youngest messages.
2661 *
2662 * A return value of FALSE indicates that there are no more records to
2663 * read.
2664 */
2665bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog,
2666 char *line, size_t size, size_t *len)
2667{
2668 unsigned long flags;
Anton Vorontsov533827c2012-07-20 17:28:07 -07002669 bool ret;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002670
2671 raw_spin_lock_irqsave(&logbuf_lock, flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07002672 ret = kmsg_dump_get_line_nolock(dumper, syslog, line, size, len);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002673 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07002674
Kay Sieverse2ae7152012-06-15 14:07:51 +02002675 return ret;
2676}
2677EXPORT_SYMBOL_GPL(kmsg_dump_get_line);
2678
2679/**
2680 * kmsg_dump_get_buffer - copy kmsg log lines
2681 * @dumper: registered kmsg dumper
2682 * @syslog: include the "<4>" prefixes
Randy Dunlap4f0f4af2012-06-30 15:37:24 -07002683 * @buf: buffer to copy the line to
Kay Sieverse2ae7152012-06-15 14:07:51 +02002684 * @size: maximum size of the buffer
2685 * @len: length of line placed into buffer
2686 *
2687 * Start at the end of the kmsg buffer and fill the provided buffer
2688 * with as many of the the *youngest* kmsg records that fit into it.
2689 * If the buffer is large enough, all available kmsg records will be
2690 * copied with a single call.
2691 *
2692 * Consecutive calls will fill the buffer with the next block of
2693 * available older records, not including the earlier retrieved ones.
2694 *
2695 * A return value of FALSE indicates that there are no more records to
2696 * read.
2697 */
2698bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
2699 char *buf, size_t size, size_t *len)
2700{
2701 unsigned long flags;
2702 u64 seq;
2703 u32 idx;
2704 u64 next_seq;
2705 u32 next_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002706 enum log_flags prev;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002707 size_t l = 0;
2708 bool ret = false;
2709
2710 if (!dumper->active)
2711 goto out;
2712
2713 raw_spin_lock_irqsave(&logbuf_lock, flags);
2714 if (dumper->cur_seq < log_first_seq) {
2715 /* messages are gone, move to first available one */
2716 dumper->cur_seq = log_first_seq;
2717 dumper->cur_idx = log_first_idx;
2718 }
2719
2720 /* last entry */
2721 if (dumper->cur_seq >= dumper->next_seq) {
2722 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2723 goto out;
2724 }
2725
2726 /* calculate length of entire buffer */
2727 seq = dumper->cur_seq;
2728 idx = dumper->cur_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002729 prev = 0;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002730 while (seq < dumper->next_seq) {
2731 struct log *msg = log_from_idx(idx);
2732
Kay Sievers5becfb12012-07-09 12:15:42 -07002733 l += msg_print_text(msg, prev, true, NULL, 0);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002734 idx = log_next(idx);
2735 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07002736 prev = msg->flags;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002737 }
2738
2739 /* move first record forward until length fits into the buffer */
2740 seq = dumper->cur_seq;
2741 idx = dumper->cur_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002742 prev = 0;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002743 while (l > size && seq < dumper->next_seq) {
2744 struct log *msg = log_from_idx(idx);
2745
Kay Sievers5becfb12012-07-09 12:15:42 -07002746 l -= msg_print_text(msg, prev, true, NULL, 0);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002747 idx = log_next(idx);
2748 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07002749 prev = msg->flags;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002750 }
2751
2752 /* last message in next interation */
2753 next_seq = seq;
2754 next_idx = idx;
2755
2756 l = 0;
Kay Sievers5becfb12012-07-09 12:15:42 -07002757 prev = 0;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002758 while (seq < dumper->next_seq) {
2759 struct log *msg = log_from_idx(idx);
2760
Kay Sievers5becfb12012-07-09 12:15:42 -07002761 l += msg_print_text(msg, prev, syslog, buf + l, size - l);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002762 idx = log_next(idx);
2763 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07002764 prev = msg->flags;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002765 }
2766
2767 dumper->next_seq = next_seq;
2768 dumper->next_idx = next_idx;
2769 ret = true;
2770 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2771out:
2772 if (len)
2773 *len = l;
2774 return ret;
2775}
2776EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer);
2777
2778/**
Anton Vorontsov533827c2012-07-20 17:28:07 -07002779 * kmsg_dump_rewind_nolock - reset the interator (unlocked version)
2780 * @dumper: registered kmsg dumper
2781 *
2782 * Reset the dumper's iterator so that kmsg_dump_get_line() and
2783 * kmsg_dump_get_buffer() can be called again and used multiple
2784 * times within the same dumper.dump() callback.
2785 *
2786 * The function is similar to kmsg_dump_rewind(), but grabs no locks.
2787 */
2788void kmsg_dump_rewind_nolock(struct kmsg_dumper *dumper)
2789{
2790 dumper->cur_seq = clear_seq;
2791 dumper->cur_idx = clear_idx;
2792 dumper->next_seq = log_next_seq;
2793 dumper->next_idx = log_next_idx;
2794}
2795
2796/**
Kay Sieverse2ae7152012-06-15 14:07:51 +02002797 * kmsg_dump_rewind - reset the interator
2798 * @dumper: registered kmsg dumper
2799 *
2800 * Reset the dumper's iterator so that kmsg_dump_get_line() and
2801 * kmsg_dump_get_buffer() can be called again and used multiple
2802 * times within the same dumper.dump() callback.
2803 */
2804void kmsg_dump_rewind(struct kmsg_dumper *dumper)
2805{
2806 unsigned long flags;
2807
2808 raw_spin_lock_irqsave(&logbuf_lock, flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07002809 kmsg_dump_rewind_nolock(dumper);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002810 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2811}
2812EXPORT_SYMBOL_GPL(kmsg_dump_rewind);
Joe Perches7ef3d2f2008-02-08 04:21:25 -08002813#endif