blob: 6e1b21a8a49725f21c1d0251b268fb61b35a7cc3 [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>
Kent Overstreeta27bb332013-05-07 16:19:08 -070035#include <linux/aio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/syscalls.h>
Neil Horman04d491a2009-04-02 16:58:57 -070037#include <linux/kexec.h>
Jason Wesseld37d39a2010-05-20 21:04:27 -050038#include <linux/kdb.h>
Ingo Molnar3fff4c42009-09-22 16:18:09 +020039#include <linux/ratelimit.h>
Simon Kagstrom456b5652009-10-16 14:09:18 +020040#include <linux/kmsg_dump.h>
Kees Cook00234592010-02-03 15:36:43 -080041#include <linux/syslog.h>
Kevin Cernekee034260d2010-06-03 22:11:25 -070042#include <linux/cpu.h>
43#include <linux/notifier.h>
Huang Yingfb842b02011-01-12 16:59:43 -080044#include <linux/rculist.h>
Kay Sieverse11fea92012-05-03 02:29:41 +020045#include <linux/poll.h>
Frederic Weisbecker74876a92012-10-12 18:00:23 +020046#include <linux/irq_work.h>
Tejun Heo196779b2013-04-30 15:27:12 -070047#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include <asm/uaccess.h>
50
Johannes Berg95100352011-11-24 20:03:08 +010051#define CREATE_TRACE_POINTS
52#include <trace/events/printk.h>
53
Joe Perchesd197c432013-07-31 13:53:44 -070054#include "console_cmdline.h"
Joe Perchesbbeddf52013-07-31 13:53:45 -070055#include "braille.h"
Joe Perchesd197c432013-07-31 13:53:44 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/* printk's without a loglevel use this.. */
Mandeep Singh Baines5af5bcb2011-03-22 16:34:23 -070058#define DEFAULT_MESSAGE_LOGLEVEL CONFIG_DEFAULT_MESSAGE_LOGLEVEL
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/* We show everything that is MORE important than this.. */
61#define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
62#define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064int console_printk[4] = {
65 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
66 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
67 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
68 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
69};
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/*
Patrick Pletscher0bbfb7c2007-02-17 20:10:16 +010072 * Low level drivers may need that to know if they can schedule in
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * their unblank() callback or not. So let's export it.
74 */
75int oops_in_progress;
76EXPORT_SYMBOL(oops_in_progress);
77
78/*
79 * console_sem protects the console_drivers list, and also
80 * provides serialisation for access to the entire console
81 * driver system.
82 */
Thomas Gleixner5b8c4f22010-09-07 14:33:43 +000083static DEFINE_SEMAPHORE(console_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084struct console *console_drivers;
Ingo Molnara29d1cf2008-06-02 13:19:08 +020085EXPORT_SYMBOL_GPL(console_drivers);
86
Daniel Vetterdaee7792012-09-22 19:52:11 +020087#ifdef CONFIG_LOCKDEP
88static struct lockdep_map console_lock_dep_map = {
89 .name = "console_lock"
90};
91#endif
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/*
94 * This is used for debugging the mess that is the VT code by
95 * keeping track if we have the console semaphore held. It's
96 * definitely not the perfect debug tool (we don't know if _WE_
97 * hold it are racing, but it helps tracking those weird code
98 * path in the console code where we end up in places I want
99 * locked without the console sempahore held
100 */
Linus Torvalds557240b2006-06-19 18:16:01 -0700101static int console_locked, console_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/*
Feng Tangfe3d8ad2011-03-22 16:34:21 -0700104 * If exclusive_console is non-NULL then only this console is to be printed to.
105 */
106static struct console *exclusive_console;
107
108/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * Array of consoles built from command line options (console=)
110 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112#define MAX_CMDLINECONSOLES 8
113
114static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
Joe Perchesd197c432013-07-31 13:53:44 -0700115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116static int selected_console = -1;
117static int preferred_console = -1;
Markus Armbruster9e124fe2008-05-26 23:31:07 +0100118int console_set_on_cmdline;
119EXPORT_SYMBOL(console_set_on_cmdline);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121/* Flag: console code may call schedule() */
122static int console_may_schedule;
123
Kay Sievers7ff95542012-05-03 02:29:13 +0200124/*
125 * The printk log buffer consists of a chain of concatenated variable
126 * length records. Every record starts with a record header, containing
127 * the overall length of the record.
128 *
129 * The heads to the first and last entry in the buffer, as well as the
130 * sequence numbers of these both entries are maintained when messages
131 * are stored..
132 *
133 * If the heads indicate available messages, the length in the header
134 * tells the start next message. A length == 0 for the next message
135 * indicates a wrap-around to the beginning of the buffer.
136 *
137 * Every record carries the monotonic timestamp in microseconds, as well as
138 * the standard userspace syslog level and syslog facility. The usual
139 * kernel messages use LOG_KERN; userspace-injected messages always carry
140 * a matching syslog facility, by default LOG_USER. The origin of every
141 * message can be reliably determined that way.
142 *
143 * The human readable log message directly follows the message header. The
144 * length of the message text is stored in the header, the stored message
145 * is not terminated.
146 *
Kay Sieverse11fea92012-05-03 02:29:41 +0200147 * Optionally, a message can carry a dictionary of properties (key/value pairs),
148 * to provide userspace with a machine-readable message context.
149 *
150 * Examples for well-defined, commonly used property names are:
151 * DEVICE=b12:8 device identifier
152 * b12:8 block dev_t
153 * c127:3 char dev_t
154 * n8 netdev ifindex
155 * +sound:card0 subsystem:devname
156 * SUBSYSTEM=pci driver-core subsystem name
157 *
158 * Valid characters in property names are [a-zA-Z0-9.-_]. The plain text value
159 * follows directly after a '=' character. Every property is terminated by
160 * a '\0' character. The last property is not terminated.
161 *
162 * Example of a message structure:
163 * 0000 ff 8f 00 00 00 00 00 00 monotonic time in nsec
164 * 0008 34 00 record is 52 bytes long
165 * 000a 0b 00 text is 11 bytes long
166 * 000c 1f 00 dictionary is 23 bytes long
167 * 000e 03 00 LOG_KERN (facility) LOG_ERR (level)
168 * 0010 69 74 27 73 20 61 20 6c "it's a l"
169 * 69 6e 65 "ine"
170 * 001b 44 45 56 49 43 "DEVIC"
171 * 45 3d 62 38 3a 32 00 44 "E=b8:2\0D"
172 * 52 49 56 45 52 3d 62 75 "RIVER=bu"
173 * 67 "g"
174 * 0032 00 00 00 padding to next message header
175 *
Joe Perches62e32ac2013-07-31 13:53:47 -0700176 * The 'struct printk_log' buffer header must never be directly exported to
Kay Sieverse11fea92012-05-03 02:29:41 +0200177 * userspace, it is a kernel-private implementation detail that might
178 * need to be changed in the future, when the requirements change.
179 *
180 * /dev/kmsg exports the structured data in the following line format:
181 * "level,sequnum,timestamp;<message text>\n"
182 *
183 * The optional key/value pairs are attached as continuation lines starting
184 * with a space character and terminated by a newline. All possible
185 * non-prinatable characters are escaped in the "\xff" notation.
186 *
187 * Users of the export format should ignore possible additional values
188 * separated by ',', and find the message after the ';' character.
Kay Sievers7ff95542012-05-03 02:29:13 +0200189 */
Matt Mackalld59745c2005-05-01 08:59:02 -0700190
Kay Sievers084681d2012-06-28 09:38:53 +0200191enum log_flags {
Kay Sievers5becfb12012-07-09 12:15:42 -0700192 LOG_NOCONS = 1, /* already flushed, do not print to console */
193 LOG_NEWLINE = 2, /* text ended with a newline */
194 LOG_PREFIX = 4, /* text started with a prefix */
195 LOG_CONT = 8, /* text is a fragment of a continuation line */
Kay Sievers084681d2012-06-28 09:38:53 +0200196};
197
Joe Perches62e32ac2013-07-31 13:53:47 -0700198struct printk_log {
Kay Sievers7ff95542012-05-03 02:29:13 +0200199 u64 ts_nsec; /* timestamp in nanoseconds */
200 u16 len; /* length of entire record */
201 u16 text_len; /* length of text buffer */
202 u16 dict_len; /* length of dictionary buffer */
Kay Sievers084681d2012-06-28 09:38:53 +0200203 u8 facility; /* syslog facility */
204 u8 flags:5; /* internal record flags */
205 u8 level:3; /* syslog level */
Kay Sievers7ff95542012-05-03 02:29:13 +0200206};
207
208/*
Jan Karaca1d4322014-06-04 16:11:34 -0700209 * The logbuf_lock protects kmsg buffer, indices, counters.
Kay Sievers7ff95542012-05-03 02:29:13 +0200210 */
211static DEFINE_RAW_SPINLOCK(logbuf_lock);
212
Kay Sievers96efedf2012-07-16 18:35:29 -0700213#ifdef CONFIG_PRINTK
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -0700214DECLARE_WAIT_QUEUE_HEAD(log_wait);
Kay Sievers7f3a7812012-05-09 01:37:51 +0200215/* the next printk record to read by syslog(READ) or /proc/kmsg */
216static u64 syslog_seq;
217static u32 syslog_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -0700218static enum log_flags syslog_prev;
Kay Sieverseb02dac2012-07-09 10:05:10 -0700219static size_t syslog_partial;
Kay Sievers7f3a7812012-05-09 01:37:51 +0200220
221/* index and sequence number of the first record stored in the buffer */
222static u64 log_first_seq;
223static u32 log_first_idx;
224
225/* index and sequence number of the next record to store in the buffer */
226static u64 log_next_seq;
Kay Sievers7f3a7812012-05-09 01:37:51 +0200227static u32 log_next_idx;
228
Kay Sieverseab07262012-07-16 18:35:30 -0700229/* the next printk record to write to the console */
230static u64 console_seq;
231static u32 console_idx;
232static enum log_flags console_prev;
233
Kay Sievers7f3a7812012-05-09 01:37:51 +0200234/* the next printk record to read after the last 'clear' command */
235static u64 clear_seq;
236static u32 clear_idx;
Kay Sievers7ff95542012-05-03 02:29:13 +0200237
Kay Sievers70498252012-07-16 18:35:29 -0700238#define PREFIX_MAX 32
239#define LOG_LINE_MAX 1024 - PREFIX_MAX
Kay Sievers7ff95542012-05-03 02:29:13 +0200240
241/* record buffer */
Andrew Lunn6ebb0172012-06-05 08:52:34 +0200242#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
Stephen Warrenf8450fc2012-05-10 16:14:33 -0600243#define LOG_ALIGN 4
244#else
Joe Perches62e32ac2013-07-31 13:53:47 -0700245#define LOG_ALIGN __alignof__(struct printk_log)
Stephen Warrenf8450fc2012-05-10 16:14:33 -0600246#endif
Kay Sievers7ff95542012-05-03 02:29:13 +0200247#define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
Stephen Warrenf8450fc2012-05-10 16:14:33 -0600248static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN);
Matt Mackalld59745c2005-05-01 08:59:02 -0700249static char *log_buf = __log_buf;
Kay Sievers7ff95542012-05-03 02:29:13 +0200250static u32 log_buf_len = __LOG_BUF_LEN;
251
Kay Sievers7ff95542012-05-03 02:29:13 +0200252/* human readable text of the record */
Joe Perches62e32ac2013-07-31 13:53:47 -0700253static char *log_text(const struct printk_log *msg)
Kay Sievers7ff95542012-05-03 02:29:13 +0200254{
Joe Perches62e32ac2013-07-31 13:53:47 -0700255 return (char *)msg + sizeof(struct printk_log);
Kay Sievers7ff95542012-05-03 02:29:13 +0200256}
257
258/* optional key/value pair dictionary attached to the record */
Joe Perches62e32ac2013-07-31 13:53:47 -0700259static char *log_dict(const struct printk_log *msg)
Kay Sievers7ff95542012-05-03 02:29:13 +0200260{
Joe Perches62e32ac2013-07-31 13:53:47 -0700261 return (char *)msg + sizeof(struct printk_log) + msg->text_len;
Kay Sievers7ff95542012-05-03 02:29:13 +0200262}
263
264/* get record by index; idx must point to valid msg */
Joe Perches62e32ac2013-07-31 13:53:47 -0700265static struct printk_log *log_from_idx(u32 idx)
Kay Sievers7ff95542012-05-03 02:29:13 +0200266{
Joe Perches62e32ac2013-07-31 13:53:47 -0700267 struct printk_log *msg = (struct printk_log *)(log_buf + idx);
Kay Sievers7ff95542012-05-03 02:29:13 +0200268
269 /*
270 * A length == 0 record is the end of buffer marker. Wrap around and
271 * read the message at the start of the buffer.
272 */
273 if (!msg->len)
Joe Perches62e32ac2013-07-31 13:53:47 -0700274 return (struct printk_log *)log_buf;
Kay Sievers7ff95542012-05-03 02:29:13 +0200275 return msg;
276}
277
278/* get next record; idx must point to valid msg */
279static u32 log_next(u32 idx)
280{
Joe Perches62e32ac2013-07-31 13:53:47 -0700281 struct printk_log *msg = (struct printk_log *)(log_buf + idx);
Kay Sievers7ff95542012-05-03 02:29:13 +0200282
283 /* length == 0 indicates the end of the buffer; wrap */
284 /*
285 * A length == 0 record is the end of buffer marker. Wrap around and
286 * read the message at the start of the buffer as *this* one, and
287 * return the one after that.
288 */
289 if (!msg->len) {
Joe Perches62e32ac2013-07-31 13:53:47 -0700290 msg = (struct printk_log *)log_buf;
Kay Sievers7ff95542012-05-03 02:29:13 +0200291 return msg->len;
292 }
293 return idx + msg->len;
294}
295
Petr Mladekf40e4b92014-06-04 16:11:30 -0700296/*
297 * Check whether there is enough free space for the given message.
298 *
299 * The same values of first_idx and next_idx mean that the buffer
300 * is either empty or full.
301 *
302 * If the buffer is empty, we must respect the position of the indexes.
303 * They cannot be reset to the beginning of the buffer.
304 */
305static int logbuf_has_space(u32 msg_size, bool empty)
Petr Mladek0a581692014-06-04 16:11:28 -0700306{
307 u32 free;
308
Petr Mladekf40e4b92014-06-04 16:11:30 -0700309 if (log_next_idx > log_first_idx || empty)
Petr Mladek0a581692014-06-04 16:11:28 -0700310 free = max(log_buf_len - log_next_idx, log_first_idx);
311 else
312 free = log_first_idx - log_next_idx;
313
314 /*
315 * We need space also for an empty header that signalizes wrapping
316 * of the buffer.
317 */
318 return free >= msg_size + sizeof(struct printk_log);
319}
320
Petr Mladekf40e4b92014-06-04 16:11:30 -0700321static int log_make_free_space(u32 msg_size)
Petr Mladek0a581692014-06-04 16:11:28 -0700322{
323 while (log_first_seq < log_next_seq) {
Petr Mladekf40e4b92014-06-04 16:11:30 -0700324 if (logbuf_has_space(msg_size, false))
325 return 0;
Petr Mladek0a581692014-06-04 16:11:28 -0700326 /* drop old messages until we have enough continuous space */
327 log_first_idx = log_next(log_first_idx);
328 log_first_seq++;
329 }
Petr Mladekf40e4b92014-06-04 16:11:30 -0700330
331 /* sequence numbers are equal, so the log buffer is empty */
332 if (logbuf_has_space(msg_size, true))
333 return 0;
334
335 return -ENOMEM;
Petr Mladek0a581692014-06-04 16:11:28 -0700336}
337
Petr Mladek85c870432014-06-04 16:11:31 -0700338/* compute the message size including the padding bytes */
339static u32 msg_used_size(u16 text_len, u16 dict_len, u32 *pad_len)
340{
341 u32 size;
342
343 size = sizeof(struct printk_log) + text_len + dict_len;
344 *pad_len = (-size) & (LOG_ALIGN - 1);
345 size += *pad_len;
346
347 return size;
348}
349
Petr Mladek55bd53a2014-06-04 16:11:32 -0700350/*
351 * Define how much of the log buffer we could take at maximum. The value
352 * must be greater than two. Note that only half of the buffer is available
353 * when the index points to the middle.
354 */
355#define MAX_LOG_TAKE_PART 4
356static const char trunc_msg[] = "<truncated>";
357
358static u32 truncate_msg(u16 *text_len, u16 *trunc_msg_len,
359 u16 *dict_len, u32 *pad_len)
360{
361 /*
362 * The message should not take the whole buffer. Otherwise, it might
363 * get removed too soon.
364 */
365 u32 max_text_len = log_buf_len / MAX_LOG_TAKE_PART;
366 if (*text_len > max_text_len)
367 *text_len = max_text_len;
368 /* enable the warning message */
369 *trunc_msg_len = strlen(trunc_msg);
370 /* disable the "dict" completely */
371 *dict_len = 0;
372 /* compute the size again, count also the warning message */
373 return msg_used_size(*text_len + *trunc_msg_len, 0, pad_len);
374}
375
Kay Sievers7ff95542012-05-03 02:29:13 +0200376/* insert record into the buffer, discard old ones, update heads */
Petr Mladek034633c2014-06-04 16:11:33 -0700377static int log_store(int facility, int level,
378 enum log_flags flags, u64 ts_nsec,
379 const char *dict, u16 dict_len,
380 const char *text, u16 text_len)
Kay Sievers7ff95542012-05-03 02:29:13 +0200381{
Joe Perches62e32ac2013-07-31 13:53:47 -0700382 struct printk_log *msg;
Kay Sievers7ff95542012-05-03 02:29:13 +0200383 u32 size, pad_len;
Petr Mladek55bd53a2014-06-04 16:11:32 -0700384 u16 trunc_msg_len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +0200385
386 /* number of '\0' padding bytes to next message */
Petr Mladek85c870432014-06-04 16:11:31 -0700387 size = msg_used_size(text_len, dict_len, &pad_len);
Kay Sievers7ff95542012-05-03 02:29:13 +0200388
Petr Mladek55bd53a2014-06-04 16:11:32 -0700389 if (log_make_free_space(size)) {
390 /* truncate the message if it is too long for empty buffer */
391 size = truncate_msg(&text_len, &trunc_msg_len,
392 &dict_len, &pad_len);
393 /* survive when the log buffer is too small for trunc_msg */
394 if (log_make_free_space(size))
Petr Mladek034633c2014-06-04 16:11:33 -0700395 return 0;
Petr Mladek55bd53a2014-06-04 16:11:32 -0700396 }
Kay Sievers7ff95542012-05-03 02:29:13 +0200397
Petr Mladek39b25102014-04-03 14:48:42 -0700398 if (log_next_idx + size + sizeof(struct printk_log) > log_buf_len) {
Kay Sievers7ff95542012-05-03 02:29:13 +0200399 /*
400 * This message + an additional empty header does not fit
401 * at the end of the buffer. Add an empty header with len == 0
402 * to signify a wrap around.
403 */
Joe Perches62e32ac2013-07-31 13:53:47 -0700404 memset(log_buf + log_next_idx, 0, sizeof(struct printk_log));
Kay Sievers7ff95542012-05-03 02:29:13 +0200405 log_next_idx = 0;
406 }
407
408 /* fill message */
Joe Perches62e32ac2013-07-31 13:53:47 -0700409 msg = (struct printk_log *)(log_buf + log_next_idx);
Kay Sievers7ff95542012-05-03 02:29:13 +0200410 memcpy(log_text(msg), text, text_len);
411 msg->text_len = text_len;
Petr Mladek55bd53a2014-06-04 16:11:32 -0700412 if (trunc_msg_len) {
413 memcpy(log_text(msg) + text_len, trunc_msg, trunc_msg_len);
414 msg->text_len += trunc_msg_len;
415 }
Kay Sievers7ff95542012-05-03 02:29:13 +0200416 memcpy(log_dict(msg), dict, dict_len);
417 msg->dict_len = dict_len;
Kay Sievers084681d2012-06-28 09:38:53 +0200418 msg->facility = facility;
419 msg->level = level & 7;
420 msg->flags = flags & 0x1f;
421 if (ts_nsec > 0)
422 msg->ts_nsec = ts_nsec;
423 else
424 msg->ts_nsec = local_clock();
Kay Sievers7ff95542012-05-03 02:29:13 +0200425 memset(log_dict(msg) + dict_len, 0, pad_len);
Petr Mladekfce6e032014-04-03 14:48:43 -0700426 msg->len = size;
Kay Sievers7ff95542012-05-03 02:29:13 +0200427
428 /* insert message */
429 log_next_idx += msg->len;
430 log_next_seq++;
Petr Mladek034633c2014-06-04 16:11:33 -0700431
432 return msg->text_len;
Kay Sievers7ff95542012-05-03 02:29:13 +0200433}
Matt Mackalld59745c2005-05-01 08:59:02 -0700434
Kees Cook637241a2013-06-12 14:04:39 -0700435#ifdef CONFIG_SECURITY_DMESG_RESTRICT
436int dmesg_restrict = 1;
437#else
438int dmesg_restrict;
439#endif
440
441static int syslog_action_restricted(int type)
442{
443 if (dmesg_restrict)
444 return 1;
445 /*
446 * Unless restricted, we allow "read all" and "get buffer size"
447 * for everybody.
448 */
449 return type != SYSLOG_ACTION_READ_ALL &&
450 type != SYSLOG_ACTION_SIZE_BUFFER;
451}
452
453static int check_syslog_permissions(int type, bool from_file)
454{
455 /*
456 * If this is from /proc/kmsg and we've already opened it, then we've
457 * already done the capabilities checks at open time.
458 */
459 if (from_file && type != SYSLOG_ACTION_OPEN)
460 return 0;
461
462 if (syslog_action_restricted(type)) {
463 if (capable(CAP_SYSLOG))
464 return 0;
465 /*
466 * For historical reasons, accept CAP_SYS_ADMIN too, with
467 * a warning.
468 */
469 if (capable(CAP_SYS_ADMIN)) {
470 pr_warn_once("%s (%d): Attempt to access syslog with "
471 "CAP_SYS_ADMIN but no CAP_SYSLOG "
472 "(deprecated).\n",
473 current->comm, task_pid_nr(current));
474 return 0;
475 }
476 return -EPERM;
477 }
478 return security_syslog(type);
479}
480
481
Kay Sieverse11fea92012-05-03 02:29:41 +0200482/* /dev/kmsg - userspace message inject/listen interface */
483struct devkmsg_user {
484 u64 seq;
485 u32 idx;
Kay Sieversd39f3d72012-07-16 18:35:30 -0700486 enum log_flags prev;
Kay Sieverse11fea92012-05-03 02:29:41 +0200487 struct mutex lock;
488 char buf[8192];
489};
490
491static ssize_t devkmsg_writev(struct kiocb *iocb, const struct iovec *iv,
492 unsigned long count, loff_t pos)
493{
494 char *buf, *line;
495 int i;
496 int level = default_message_loglevel;
497 int facility = 1; /* LOG_USER */
498 size_t len = iov_length(iv, count);
499 ssize_t ret = len;
500
501 if (len > LOG_LINE_MAX)
502 return -EINVAL;
503 buf = kmalloc(len+1, GFP_KERNEL);
504 if (buf == NULL)
505 return -ENOMEM;
506
507 line = buf;
508 for (i = 0; i < count; i++) {
Kay Sieverscdf53442012-07-30 14:40:08 -0700509 if (copy_from_user(line, iv[i].iov_base, iv[i].iov_len)) {
510 ret = -EFAULT;
Kay Sieverse11fea92012-05-03 02:29:41 +0200511 goto out;
Kay Sieverscdf53442012-07-30 14:40:08 -0700512 }
Kay Sieverse11fea92012-05-03 02:29:41 +0200513 line += iv[i].iov_len;
514 }
515
516 /*
517 * Extract and skip the syslog prefix <[0-9]*>. Coming from userspace
518 * the decimal value represents 32bit, the lower 3 bit are the log
519 * level, the rest are the log facility.
520 *
521 * If no prefix or no userspace facility is specified, we
522 * enforce LOG_USER, to be able to reliably distinguish
523 * kernel-generated messages from userspace-injected ones.
524 */
525 line = buf;
526 if (line[0] == '<') {
527 char *endp = NULL;
528
529 i = simple_strtoul(line+1, &endp, 10);
530 if (endp && endp[0] == '>') {
531 level = i & 7;
532 if (i >> 3)
533 facility = i >> 3;
534 endp++;
535 len -= endp - line;
536 line = endp;
537 }
538 }
539 line[len] = '\0';
540
541 printk_emit(facility, level, NULL, 0, "%s", line);
542out:
543 kfree(buf);
544 return ret;
545}
546
547static ssize_t devkmsg_read(struct file *file, char __user *buf,
548 size_t count, loff_t *ppos)
549{
550 struct devkmsg_user *user = file->private_data;
Joe Perches62e32ac2013-07-31 13:53:47 -0700551 struct printk_log *msg;
Kay Sievers5fc324902012-05-08 13:04:17 +0200552 u64 ts_usec;
Kay Sieverse11fea92012-05-03 02:29:41 +0200553 size_t i;
Kay Sieversd39f3d72012-07-16 18:35:30 -0700554 char cont = '-';
Kay Sieverse11fea92012-05-03 02:29:41 +0200555 size_t len;
556 ssize_t ret;
557
558 if (!user)
559 return -EBADF;
560
Yuanhan Liu4a77a5a2012-06-16 21:21:51 +0800561 ret = mutex_lock_interruptible(&user->lock);
562 if (ret)
563 return ret;
liu chuansheng5c53d812012-07-06 09:50:08 -0700564 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200565 while (user->seq == log_next_seq) {
566 if (file->f_flags & O_NONBLOCK) {
567 ret = -EAGAIN;
liu chuansheng5c53d812012-07-06 09:50:08 -0700568 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200569 goto out;
570 }
571
liu chuansheng5c53d812012-07-06 09:50:08 -0700572 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200573 ret = wait_event_interruptible(log_wait,
574 user->seq != log_next_seq);
575 if (ret)
576 goto out;
liu chuansheng5c53d812012-07-06 09:50:08 -0700577 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200578 }
579
580 if (user->seq < log_first_seq) {
581 /* our last seen message is gone, return error and reset */
582 user->idx = log_first_idx;
583 user->seq = log_first_seq;
584 ret = -EPIPE;
liu chuansheng5c53d812012-07-06 09:50:08 -0700585 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200586 goto out;
587 }
588
589 msg = log_from_idx(user->idx);
Kay Sievers5fc324902012-05-08 13:04:17 +0200590 ts_usec = msg->ts_nsec;
591 do_div(ts_usec, 1000);
Kay Sieversd39f3d72012-07-16 18:35:30 -0700592
593 /*
594 * If we couldn't merge continuation line fragments during the print,
595 * export the stored flags to allow an optional external merge of the
596 * records. Merging the records isn't always neccessarily correct, like
597 * when we hit a race during printing. In most cases though, it produces
598 * better readable output. 'c' in the record flags mark the first
599 * fragment of a line, '+' the following.
600 */
601 if (msg->flags & LOG_CONT && !(user->prev & LOG_CONT))
602 cont = 'c';
603 else if ((msg->flags & LOG_CONT) ||
604 ((user->prev & LOG_CONT) && !(msg->flags & LOG_PREFIX)))
605 cont = '+';
606
607 len = sprintf(user->buf, "%u,%llu,%llu,%c;",
608 (msg->facility << 3) | msg->level,
609 user->seq, ts_usec, cont);
610 user->prev = msg->flags;
Kay Sieverse11fea92012-05-03 02:29:41 +0200611
612 /* escape non-printable characters */
613 for (i = 0; i < msg->text_len; i++) {
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200614 unsigned char c = log_text(msg)[i];
Kay Sieverse11fea92012-05-03 02:29:41 +0200615
Kay Sieverse3f5a5f2012-07-06 09:50:09 -0700616 if (c < ' ' || c >= 127 || c == '\\')
Kay Sieverse11fea92012-05-03 02:29:41 +0200617 len += sprintf(user->buf + len, "\\x%02x", c);
618 else
619 user->buf[len++] = c;
620 }
621 user->buf[len++] = '\n';
622
623 if (msg->dict_len) {
624 bool line = true;
625
626 for (i = 0; i < msg->dict_len; i++) {
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200627 unsigned char c = log_dict(msg)[i];
Kay Sieverse11fea92012-05-03 02:29:41 +0200628
629 if (line) {
630 user->buf[len++] = ' ';
631 line = false;
632 }
633
634 if (c == '\0') {
635 user->buf[len++] = '\n';
636 line = true;
637 continue;
638 }
639
Kay Sieverse3f5a5f2012-07-06 09:50:09 -0700640 if (c < ' ' || c >= 127 || c == '\\') {
Kay Sieverse11fea92012-05-03 02:29:41 +0200641 len += sprintf(user->buf + len, "\\x%02x", c);
642 continue;
643 }
644
645 user->buf[len++] = c;
646 }
647 user->buf[len++] = '\n';
648 }
649
650 user->idx = log_next(user->idx);
651 user->seq++;
liu chuansheng5c53d812012-07-06 09:50:08 -0700652 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200653
654 if (len > count) {
655 ret = -EINVAL;
656 goto out;
657 }
658
659 if (copy_to_user(buf, user->buf, len)) {
660 ret = -EFAULT;
661 goto out;
662 }
663 ret = len;
664out:
665 mutex_unlock(&user->lock);
666 return ret;
667}
668
669static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence)
670{
671 struct devkmsg_user *user = file->private_data;
672 loff_t ret = 0;
673
674 if (!user)
675 return -EBADF;
676 if (offset)
677 return -ESPIPE;
678
liu chuansheng5c53d812012-07-06 09:50:08 -0700679 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200680 switch (whence) {
681 case SEEK_SET:
682 /* the first record */
683 user->idx = log_first_idx;
684 user->seq = log_first_seq;
685 break;
686 case SEEK_DATA:
687 /*
688 * The first record after the last SYSLOG_ACTION_CLEAR,
689 * like issued by 'dmesg -c'. Reading /dev/kmsg itself
690 * changes no global state, and does not clear anything.
691 */
692 user->idx = clear_idx;
693 user->seq = clear_seq;
694 break;
695 case SEEK_END:
696 /* after the last record */
697 user->idx = log_next_idx;
698 user->seq = log_next_seq;
699 break;
700 default:
701 ret = -EINVAL;
702 }
liu chuansheng5c53d812012-07-06 09:50:08 -0700703 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200704 return ret;
705}
706
707static unsigned int devkmsg_poll(struct file *file, poll_table *wait)
708{
709 struct devkmsg_user *user = file->private_data;
710 int ret = 0;
711
712 if (!user)
713 return POLLERR|POLLNVAL;
714
715 poll_wait(file, &log_wait, wait);
716
liu chuansheng5c53d812012-07-06 09:50:08 -0700717 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200718 if (user->seq < log_next_seq) {
719 /* return error when data has vanished underneath us */
720 if (user->seq < log_first_seq)
721 ret = POLLIN|POLLRDNORM|POLLERR|POLLPRI;
Nicolas Kaiser0a285312013-04-29 16:17:20 -0700722 else
723 ret = POLLIN|POLLRDNORM;
Kay Sieverse11fea92012-05-03 02:29:41 +0200724 }
liu chuansheng5c53d812012-07-06 09:50:08 -0700725 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200726
727 return ret;
728}
729
730static int devkmsg_open(struct inode *inode, struct file *file)
731{
732 struct devkmsg_user *user;
733 int err;
734
735 /* write-only does not need any file context */
736 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
737 return 0;
738
Kees Cook637241a2013-06-12 14:04:39 -0700739 err = check_syslog_permissions(SYSLOG_ACTION_READ_ALL,
740 SYSLOG_FROM_READER);
Kay Sieverse11fea92012-05-03 02:29:41 +0200741 if (err)
742 return err;
743
744 user = kmalloc(sizeof(struct devkmsg_user), GFP_KERNEL);
745 if (!user)
746 return -ENOMEM;
747
748 mutex_init(&user->lock);
749
liu chuansheng5c53d812012-07-06 09:50:08 -0700750 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200751 user->idx = log_first_idx;
752 user->seq = log_first_seq;
liu chuansheng5c53d812012-07-06 09:50:08 -0700753 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200754
755 file->private_data = user;
756 return 0;
757}
758
759static int devkmsg_release(struct inode *inode, struct file *file)
760{
761 struct devkmsg_user *user = file->private_data;
762
763 if (!user)
764 return 0;
765
766 mutex_destroy(&user->lock);
767 kfree(user);
768 return 0;
769}
770
771const struct file_operations kmsg_fops = {
772 .open = devkmsg_open,
773 .read = devkmsg_read,
774 .aio_write = devkmsg_writev,
775 .llseek = devkmsg_llseek,
776 .poll = devkmsg_poll,
777 .release = devkmsg_release,
778};
779
Neil Horman04d491a2009-04-02 16:58:57 -0700780#ifdef CONFIG_KEXEC
781/*
Dirk Gouders4c1ace62013-11-12 15:08:54 -0800782 * This appends the listed symbols to /proc/vmcore
Neil Horman04d491a2009-04-02 16:58:57 -0700783 *
Dirk Gouders4c1ace62013-11-12 15:08:54 -0800784 * /proc/vmcore is used by various utilities, like crash and makedumpfile to
Neil Horman04d491a2009-04-02 16:58:57 -0700785 * obtain access to symbols that are otherwise very difficult to locate. These
786 * symbols are specifically used so that utilities can access and extract the
787 * dmesg log from a vmcore file after a crash.
788 */
789void log_buf_kexec_setup(void)
790{
791 VMCOREINFO_SYMBOL(log_buf);
Neil Horman04d491a2009-04-02 16:58:57 -0700792 VMCOREINFO_SYMBOL(log_buf_len);
Kay Sievers7ff95542012-05-03 02:29:13 +0200793 VMCOREINFO_SYMBOL(log_first_idx);
794 VMCOREINFO_SYMBOL(log_next_idx);
Vivek Goyal67914572012-07-18 13:18:12 -0400795 /*
Joe Perches62e32ac2013-07-31 13:53:47 -0700796 * Export struct printk_log size and field offsets. User space tools can
Vivek Goyal67914572012-07-18 13:18:12 -0400797 * parse it and detect any changes to structure down the line.
798 */
Joe Perches62e32ac2013-07-31 13:53:47 -0700799 VMCOREINFO_STRUCT_SIZE(printk_log);
800 VMCOREINFO_OFFSET(printk_log, ts_nsec);
801 VMCOREINFO_OFFSET(printk_log, len);
802 VMCOREINFO_OFFSET(printk_log, text_len);
803 VMCOREINFO_OFFSET(printk_log, dict_len);
Neil Horman04d491a2009-04-02 16:58:57 -0700804}
805#endif
806
Mike Travis162a7e72011-05-24 17:13:20 -0700807/* requested log_buf_len from kernel cmdline */
808static unsigned long __initdata new_log_buf_len;
809
810/* save requested log_buf_len since it's too early to process it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811static int __init log_buf_len_setup(char *str)
812{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800813 unsigned size = memparse(str, &str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 if (size)
816 size = roundup_pow_of_two(size);
Mike Travis162a7e72011-05-24 17:13:20 -0700817 if (size > log_buf_len)
818 new_log_buf_len = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Mike Travis162a7e72011-05-24 17:13:20 -0700820 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
Mike Travis162a7e72011-05-24 17:13:20 -0700822early_param("log_buf_len", log_buf_len_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Mike Travis162a7e72011-05-24 17:13:20 -0700824void __init setup_log_buf(int early)
825{
826 unsigned long flags;
Mike Travis162a7e72011-05-24 17:13:20 -0700827 char *new_log_buf;
828 int free;
829
830 if (!new_log_buf_len)
831 return;
832
833 if (early) {
Santosh Shilimkar9da791d2014-01-21 15:50:23 -0800834 new_log_buf =
835 memblock_virt_alloc(new_log_buf_len, PAGE_SIZE);
Mike Travis162a7e72011-05-24 17:13:20 -0700836 } else {
Santosh Shilimkar9da791d2014-01-21 15:50:23 -0800837 new_log_buf = memblock_virt_alloc_nopanic(new_log_buf_len, 0);
Mike Travis162a7e72011-05-24 17:13:20 -0700838 }
839
840 if (unlikely(!new_log_buf)) {
841 pr_err("log_buf_len: %ld bytes not available\n",
842 new_log_buf_len);
843 return;
844 }
845
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200846 raw_spin_lock_irqsave(&logbuf_lock, flags);
Mike Travis162a7e72011-05-24 17:13:20 -0700847 log_buf_len = new_log_buf_len;
848 log_buf = new_log_buf;
849 new_log_buf_len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +0200850 free = __LOG_BUF_LEN - log_next_idx;
851 memcpy(log_buf, __log_buf, __LOG_BUF_LEN);
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200852 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
Mike Travis162a7e72011-05-24 17:13:20 -0700853
854 pr_info("log_buf_len: %d\n", log_buf_len);
855 pr_info("early log buf free: %d(%d%%)\n",
856 free, (free * 100) / __LOG_BUF_LEN);
857}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800859static bool __read_mostly ignore_loglevel;
860
861static int __init ignore_loglevel_setup(char *str)
862{
863 ignore_loglevel = 1;
Andrew Morton27083ba2013-11-12 15:08:50 -0800864 pr_info("debug: ignoring loglevel setting.\n");
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800865
866 return 0;
867}
868
869early_param("ignore_loglevel", ignore_loglevel_setup);
870module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR);
871MODULE_PARM_DESC(ignore_loglevel, "ignore loglevel setting, to"
872 "print all kernel messages to the console.");
873
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700874#ifdef CONFIG_BOOT_PRINTK_DELAY
875
Namhyung Kim674dff62010-10-26 14:22:48 -0700876static int boot_delay; /* msecs delay after each printk during bootup */
Dave Young3a3b6ed2009-09-22 16:43:31 -0700877static unsigned long long loops_per_msec; /* based on boot_delay */
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700878
879static int __init boot_delay_setup(char *str)
880{
881 unsigned long lpj;
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700882
883 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
884 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
885
886 get_option(&str, &boot_delay);
887 if (boot_delay > 10 * 1000)
888 boot_delay = 0;
889
Dave Young3a3b6ed2009-09-22 16:43:31 -0700890 pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
891 "HZ: %d, loops_per_msec: %llu\n",
892 boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
Dave Young29e9d222013-11-12 15:08:53 -0800893 return 0;
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700894}
Dave Young29e9d222013-11-12 15:08:53 -0800895early_param("boot_delay", boot_delay_setup);
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700896
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800897static void boot_delay_msec(int level)
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700898{
899 unsigned long long k;
900 unsigned long timeout;
901
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800902 if ((boot_delay == 0 || system_state != SYSTEM_BOOTING)
903 || (level >= console_loglevel && !ignore_loglevel)) {
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700904 return;
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800905 }
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700906
Dave Young3a3b6ed2009-09-22 16:43:31 -0700907 k = (unsigned long long)loops_per_msec * boot_delay;
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700908
909 timeout = jiffies + msecs_to_jiffies(boot_delay);
910 while (k) {
911 k--;
912 cpu_relax();
913 /*
914 * use (volatile) jiffies to prevent
915 * compiler reduction; loop termination via jiffies
916 * is secondary and may or may not happen.
917 */
918 if (time_after(jiffies, timeout))
919 break;
920 touch_nmi_watchdog();
921 }
922}
923#else
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800924static inline void boot_delay_msec(int level)
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700925{
926}
927#endif
928
Kay Sievers7ff95542012-05-03 02:29:13 +0200929#if defined(CONFIG_PRINTK_TIME)
930static bool printk_time = 1;
931#else
932static bool printk_time;
933#endif
934module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
935
Kay Sievers084681d2012-06-28 09:38:53 +0200936static size_t print_time(u64 ts, char *buf)
937{
938 unsigned long rem_nsec;
939
940 if (!printk_time)
941 return 0;
942
Kay Sievers084681d2012-06-28 09:38:53 +0200943 rem_nsec = do_div(ts, 1000000000);
Roland Dreier35dac272013-01-04 15:35:50 -0800944
945 if (!buf)
946 return snprintf(NULL, 0, "[%5lu.000000] ", (unsigned long)ts);
947
Kay Sievers084681d2012-06-28 09:38:53 +0200948 return sprintf(buf, "[%5lu.%06lu] ",
949 (unsigned long)ts, rem_nsec / 1000);
950}
951
Joe Perches62e32ac2013-07-31 13:53:47 -0700952static size_t print_prefix(const struct printk_log *msg, bool syslog, char *buf)
Kay Sievers649e6ee2012-05-10 04:30:45 +0200953{
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200954 size_t len = 0;
Kay Sievers43a73a52012-07-06 09:50:09 -0700955 unsigned int prefix = (msg->facility << 3) | msg->level;
Kay Sievers649e6ee2012-05-10 04:30:45 +0200956
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200957 if (syslog) {
958 if (buf) {
Kay Sievers43a73a52012-07-06 09:50:09 -0700959 len += sprintf(buf, "<%u>", prefix);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200960 } else {
961 len += 3;
Kay Sievers43a73a52012-07-06 09:50:09 -0700962 if (prefix > 999)
963 len += 3;
964 else if (prefix > 99)
965 len += 2;
966 else if (prefix > 9)
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200967 len++;
968 }
Kay Sievers7ff95542012-05-03 02:29:13 +0200969 }
970
Kay Sievers084681d2012-06-28 09:38:53 +0200971 len += print_time(msg->ts_nsec, buf ? buf + len : NULL);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200972 return len;
973}
974
Joe Perches62e32ac2013-07-31 13:53:47 -0700975static size_t msg_print_text(const struct printk_log *msg, enum log_flags prev,
Kay Sievers5becfb12012-07-09 12:15:42 -0700976 bool syslog, char *buf, size_t size)
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200977{
978 const char *text = log_text(msg);
979 size_t text_size = msg->text_len;
Kay Sievers5becfb12012-07-09 12:15:42 -0700980 bool prefix = true;
981 bool newline = true;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200982 size_t len = 0;
983
Kay Sievers5becfb12012-07-09 12:15:42 -0700984 if ((prev & LOG_CONT) && !(msg->flags & LOG_PREFIX))
985 prefix = false;
986
987 if (msg->flags & LOG_CONT) {
988 if ((prev & LOG_CONT) && !(prev & LOG_NEWLINE))
989 prefix = false;
990
991 if (!(msg->flags & LOG_NEWLINE))
992 newline = false;
993 }
994
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200995 do {
996 const char *next = memchr(text, '\n', text_size);
997 size_t text_len;
998
999 if (next) {
1000 text_len = next - text;
1001 next++;
1002 text_size -= next - text;
1003 } else {
1004 text_len = text_size;
1005 }
1006
1007 if (buf) {
1008 if (print_prefix(msg, syslog, NULL) +
Kay Sievers70498252012-07-16 18:35:29 -07001009 text_len + 1 >= size - len)
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001010 break;
1011
Kay Sievers5becfb12012-07-09 12:15:42 -07001012 if (prefix)
1013 len += print_prefix(msg, syslog, buf + len);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001014 memcpy(buf + len, text, text_len);
1015 len += text_len;
Kay Sievers5becfb12012-07-09 12:15:42 -07001016 if (next || newline)
1017 buf[len++] = '\n';
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001018 } else {
1019 /* SYSLOG_ACTION_* buffer size only calculation */
Kay Sievers5becfb12012-07-09 12:15:42 -07001020 if (prefix)
1021 len += print_prefix(msg, syslog, NULL);
1022 len += text_len;
1023 if (next || newline)
1024 len++;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001025 }
1026
Kay Sievers5becfb12012-07-09 12:15:42 -07001027 prefix = true;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001028 text = next;
1029 } while (text);
1030
Kay Sievers7ff95542012-05-03 02:29:13 +02001031 return len;
1032}
1033
1034static int syslog_print(char __user *buf, int size)
1035{
1036 char *text;
Joe Perches62e32ac2013-07-31 13:53:47 -07001037 struct printk_log *msg;
Jan Beulich116e90b2012-06-22 16:36:09 +01001038 int len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001039
Kay Sievers70498252012-07-16 18:35:29 -07001040 text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
Kay Sievers7ff95542012-05-03 02:29:13 +02001041 if (!text)
1042 return -ENOMEM;
1043
Jan Beulich116e90b2012-06-22 16:36:09 +01001044 while (size > 0) {
1045 size_t n;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001046 size_t skip;
Kay Sievers7ff95542012-05-03 02:29:13 +02001047
Jan Beulich116e90b2012-06-22 16:36:09 +01001048 raw_spin_lock_irq(&logbuf_lock);
1049 if (syslog_seq < log_first_seq) {
1050 /* messages are gone, move to first one */
1051 syslog_seq = log_first_seq;
1052 syslog_idx = log_first_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001053 syslog_prev = 0;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001054 syslog_partial = 0;
Jan Beulich116e90b2012-06-22 16:36:09 +01001055 }
1056 if (syslog_seq == log_next_seq) {
1057 raw_spin_unlock_irq(&logbuf_lock);
1058 break;
1059 }
Kay Sieverseb02dac2012-07-09 10:05:10 -07001060
1061 skip = syslog_partial;
Jan Beulich116e90b2012-06-22 16:36:09 +01001062 msg = log_from_idx(syslog_idx);
Kay Sievers70498252012-07-16 18:35:29 -07001063 n = msg_print_text(msg, syslog_prev, true, text,
1064 LOG_LINE_MAX + PREFIX_MAX);
Kay Sieverseb02dac2012-07-09 10:05:10 -07001065 if (n - syslog_partial <= size) {
1066 /* message fits into buffer, move forward */
Jan Beulich116e90b2012-06-22 16:36:09 +01001067 syslog_idx = log_next(syslog_idx);
1068 syslog_seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07001069 syslog_prev = msg->flags;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001070 n -= syslog_partial;
1071 syslog_partial = 0;
1072 } else if (!len){
1073 /* partial read(), remember position */
1074 n = size;
1075 syslog_partial += n;
Jan Beulich116e90b2012-06-22 16:36:09 +01001076 } else
1077 n = 0;
1078 raw_spin_unlock_irq(&logbuf_lock);
1079
1080 if (!n)
1081 break;
1082
Kay Sieverseb02dac2012-07-09 10:05:10 -07001083 if (copy_to_user(buf, text + skip, n)) {
Jan Beulich116e90b2012-06-22 16:36:09 +01001084 if (!len)
1085 len = -EFAULT;
1086 break;
1087 }
Kay Sieverseb02dac2012-07-09 10:05:10 -07001088
1089 len += n;
1090 size -= n;
1091 buf += n;
Jan Beulich116e90b2012-06-22 16:36:09 +01001092 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001093
1094 kfree(text);
1095 return len;
1096}
1097
1098static int syslog_print_all(char __user *buf, int size, bool clear)
1099{
1100 char *text;
1101 int len = 0;
1102
Kay Sievers70498252012-07-16 18:35:29 -07001103 text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
Kay Sievers7ff95542012-05-03 02:29:13 +02001104 if (!text)
1105 return -ENOMEM;
1106
1107 raw_spin_lock_irq(&logbuf_lock);
1108 if (buf) {
1109 u64 next_seq;
1110 u64 seq;
1111 u32 idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001112 enum log_flags prev;
Kay Sievers7ff95542012-05-03 02:29:13 +02001113
1114 if (clear_seq < log_first_seq) {
1115 /* messages are gone, move to first available one */
1116 clear_seq = log_first_seq;
1117 clear_idx = log_first_idx;
1118 }
1119
1120 /*
1121 * Find first record that fits, including all following records,
1122 * into the user-provided buffer for this dump.
Kay Sieverse2ae7152012-06-15 14:07:51 +02001123 */
Kay Sievers7ff95542012-05-03 02:29:13 +02001124 seq = clear_seq;
1125 idx = clear_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001126 prev = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001127 while (seq < log_next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07001128 struct printk_log *msg = log_from_idx(idx);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001129
Kay Sievers5becfb12012-07-09 12:15:42 -07001130 len += msg_print_text(msg, prev, true, NULL, 0);
Jeff Mahoneye3756472012-08-10 15:07:09 -04001131 prev = msg->flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02001132 idx = log_next(idx);
1133 seq++;
1134 }
Kay Sieverse2ae7152012-06-15 14:07:51 +02001135
1136 /* move first record forward until length fits into the buffer */
Kay Sievers7ff95542012-05-03 02:29:13 +02001137 seq = clear_seq;
1138 idx = clear_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001139 prev = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001140 while (len > size && seq < log_next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07001141 struct printk_log *msg = log_from_idx(idx);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001142
Kay Sievers5becfb12012-07-09 12:15:42 -07001143 len -= msg_print_text(msg, prev, true, NULL, 0);
Jeff Mahoneye3756472012-08-10 15:07:09 -04001144 prev = msg->flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02001145 idx = log_next(idx);
1146 seq++;
1147 }
1148
Kay Sieverse2ae7152012-06-15 14:07:51 +02001149 /* last message fitting into this dump */
Kay Sievers7ff95542012-05-03 02:29:13 +02001150 next_seq = log_next_seq;
1151
1152 len = 0;
1153 while (len >= 0 && seq < next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07001154 struct printk_log *msg = log_from_idx(idx);
Kay Sievers7ff95542012-05-03 02:29:13 +02001155 int textlen;
1156
Kay Sievers70498252012-07-16 18:35:29 -07001157 textlen = msg_print_text(msg, prev, true, text,
1158 LOG_LINE_MAX + PREFIX_MAX);
Kay Sievers7ff95542012-05-03 02:29:13 +02001159 if (textlen < 0) {
1160 len = textlen;
1161 break;
1162 }
1163 idx = log_next(idx);
1164 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07001165 prev = msg->flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02001166
1167 raw_spin_unlock_irq(&logbuf_lock);
1168 if (copy_to_user(buf + len, text, textlen))
1169 len = -EFAULT;
1170 else
1171 len += textlen;
1172 raw_spin_lock_irq(&logbuf_lock);
1173
1174 if (seq < log_first_seq) {
1175 /* messages are gone, move to next one */
1176 seq = log_first_seq;
1177 idx = log_first_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001178 prev = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001179 }
1180 }
1181 }
1182
1183 if (clear) {
1184 clear_seq = log_next_seq;
1185 clear_idx = log_next_idx;
1186 }
1187 raw_spin_unlock_irq(&logbuf_lock);
1188
1189 kfree(text);
1190 return len;
1191}
1192
Kees Cook00234592010-02-03 15:36:43 -08001193int do_syslog(int type, char __user *buf, int len, bool from_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
Kay Sievers7ff95542012-05-03 02:29:13 +02001195 bool clear = false;
1196 static int saved_console_loglevel = -1;
Linus Torvaldsee24aeb2011-02-10 17:53:55 -08001197 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Linus Torvaldsee24aeb2011-02-10 17:53:55 -08001199 error = check_syslog_permissions(type, from_file);
1200 if (error)
1201 goto out;
Eric Paris12b30522010-11-15 18:36:29 -05001202
1203 error = security_syslog(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (error)
1205 return error;
1206
1207 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08001208 case SYSLOG_ACTION_CLOSE: /* Close log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001210 case SYSLOG_ACTION_OPEN: /* Open log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001212 case SYSLOG_ACTION_READ: /* Read from log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 error = -EINVAL;
1214 if (!buf || len < 0)
1215 goto out;
1216 error = 0;
1217 if (!len)
1218 goto out;
1219 if (!access_ok(VERIFY_WRITE, buf, len)) {
1220 error = -EFAULT;
1221 goto out;
1222 }
Yuanhan Liu4a77a5a2012-06-16 21:21:51 +08001223 error = wait_event_interruptible(log_wait,
1224 syslog_seq != log_next_seq);
Kay Sieverscb424ff2012-07-06 09:50:09 -07001225 if (error)
Yuanhan Liu4a77a5a2012-06-16 21:21:51 +08001226 goto out;
Kay Sievers7ff95542012-05-03 02:29:13 +02001227 error = syslog_print(buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001229 /* Read/clear last kernel messages */
1230 case SYSLOG_ACTION_READ_CLEAR:
Kay Sievers7ff95542012-05-03 02:29:13 +02001231 clear = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 /* FALL THRU */
Kees Cookd78ca3c2010-02-03 15:37:13 -08001233 /* Read last kernel messages */
1234 case SYSLOG_ACTION_READ_ALL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 error = -EINVAL;
1236 if (!buf || len < 0)
1237 goto out;
1238 error = 0;
1239 if (!len)
1240 goto out;
1241 if (!access_ok(VERIFY_WRITE, buf, len)) {
1242 error = -EFAULT;
1243 goto out;
1244 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001245 error = syslog_print_all(buf, len, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001247 /* Clear ring buffer */
1248 case SYSLOG_ACTION_CLEAR:
Kay Sievers7ff95542012-05-03 02:29:13 +02001249 syslog_print_all(NULL, 0, true);
Alan Stern4661e352012-06-22 17:12:19 -04001250 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001251 /* Disable logging to console */
1252 case SYSLOG_ACTION_CONSOLE_OFF:
Frans Pop1aaad492009-07-06 13:31:48 +02001253 if (saved_console_loglevel == -1)
1254 saved_console_loglevel = console_loglevel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 console_loglevel = minimum_console_loglevel;
1256 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001257 /* Enable logging to console */
1258 case SYSLOG_ACTION_CONSOLE_ON:
Frans Pop1aaad492009-07-06 13:31:48 +02001259 if (saved_console_loglevel != -1) {
1260 console_loglevel = saved_console_loglevel;
1261 saved_console_loglevel = -1;
1262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001264 /* Set level of messages printed to console */
1265 case SYSLOG_ACTION_CONSOLE_LEVEL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 error = -EINVAL;
1267 if (len < 1 || len > 8)
1268 goto out;
1269 if (len < minimum_console_loglevel)
1270 len = minimum_console_loglevel;
1271 console_loglevel = len;
Frans Pop1aaad492009-07-06 13:31:48 +02001272 /* Implicitly re-enable logging to console */
1273 saved_console_loglevel = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 error = 0;
1275 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001276 /* Number of chars in the log buffer */
1277 case SYSLOG_ACTION_SIZE_UNREAD:
Kay Sievers7ff95542012-05-03 02:29:13 +02001278 raw_spin_lock_irq(&logbuf_lock);
1279 if (syslog_seq < log_first_seq) {
1280 /* messages are gone, move to first one */
1281 syslog_seq = log_first_seq;
1282 syslog_idx = log_first_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001283 syslog_prev = 0;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001284 syslog_partial = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001285 }
1286 if (from_file) {
1287 /*
1288 * Short-cut for poll(/"proc/kmsg") which simply checks
1289 * for pending data, not the size; return the count of
1290 * records, not the length.
1291 */
1292 error = log_next_idx - syslog_idx;
1293 } else {
Kay Sievers5becfb12012-07-09 12:15:42 -07001294 u64 seq = syslog_seq;
1295 u32 idx = syslog_idx;
1296 enum log_flags prev = syslog_prev;
Kay Sievers7ff95542012-05-03 02:29:13 +02001297
1298 error = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001299 while (seq < log_next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07001300 struct printk_log *msg = log_from_idx(idx);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001301
Kay Sievers5becfb12012-07-09 12:15:42 -07001302 error += msg_print_text(msg, prev, true, NULL, 0);
Kay Sievers7ff95542012-05-03 02:29:13 +02001303 idx = log_next(idx);
1304 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07001305 prev = msg->flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02001306 }
Kay Sieverseb02dac2012-07-09 10:05:10 -07001307 error -= syslog_partial;
Kay Sievers7ff95542012-05-03 02:29:13 +02001308 }
1309 raw_spin_unlock_irq(&logbuf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001311 /* Size of the log buffer */
1312 case SYSLOG_ACTION_SIZE_BUFFER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 error = log_buf_len;
1314 break;
1315 default:
1316 error = -EINVAL;
1317 break;
1318 }
1319out:
1320 return error;
1321}
1322
Heiko Carstens1e7bfb22009-01-14 14:14:29 +01001323SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Kees Cook637241a2013-06-12 14:04:39 -07001325 return do_syslog(type, buf, len, SYSLOG_FROM_READER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 * Call the console drivers, asking them to write out
1330 * log_buf[start] to log_buf[end - 1].
Torben Hohnac751ef2011-01-25 15:07:35 -08001331 * The console_lock must be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 */
Kay Sievers7ff95542012-05-03 02:29:13 +02001333static void call_console_drivers(int level, const char *text, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
Kay Sievers7ff95542012-05-03 02:29:13 +02001335 struct console *con;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
zhangwei(Jovi)07c65f42013-04-29 16:17:16 -07001337 trace_console(text, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Kay Sievers7ff95542012-05-03 02:29:13 +02001339 if (level >= console_loglevel && !ignore_loglevel)
1340 return;
1341 if (!console_drivers)
1342 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Kay Sievers7ff95542012-05-03 02:29:13 +02001344 for_each_console(con) {
1345 if (exclusive_console && con != exclusive_console)
1346 continue;
1347 if (!(con->flags & CON_ENABLED))
1348 continue;
1349 if (!con->write)
1350 continue;
1351 if (!cpu_online(smp_processor_id()) &&
1352 !(con->flags & CON_ANYTIME))
1353 continue;
1354 con->write(con, text, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
1358/*
1359 * Zap console related locks when oopsing. Only zap at most once
1360 * every 10 seconds, to leave time for slow consoles to print a
1361 * full oops.
1362 */
1363static void zap_locks(void)
1364{
1365 static unsigned long oops_timestamp;
1366
1367 if (time_after_eq(jiffies, oops_timestamp) &&
Jesper Juhl40dc5652005-10-30 15:02:46 -08001368 !time_after(jiffies, oops_timestamp + 30 * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return;
1370
1371 oops_timestamp = jiffies;
1372
Peter Zijlstra94d24fc2011-06-07 11:17:30 +02001373 debug_locks_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 /* If a crash is occurring, make sure we can't deadlock */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001375 raw_spin_lock_init(&logbuf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 /* And make sure that we print immediately */
Thomas Gleixner5b8c4f22010-09-07 14:33:43 +00001377 sema_init(&console_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
Jan Kara608873c2014-06-04 16:11:35 -07001380/*
1381 * Check if we have any console that is capable of printing while cpu is
1382 * booting or shutting down. Requires console_sem.
1383 */
Michael Ellerman76a8ad22006-06-25 05:47:40 -07001384static int have_callable_console(void)
1385{
1386 struct console *con;
1387
Robin Getz4d091612009-07-01 21:08:37 -04001388 for_each_console(con)
Michael Ellerman76a8ad22006-06-25 05:47:40 -07001389 if (con->flags & CON_ANYTIME)
1390 return 1;
1391
1392 return 0;
1393}
1394
Linus Torvalds266c2e02008-03-24 19:25:08 -07001395/*
1396 * Can we actually use the console at this time on this cpu?
1397 *
1398 * Console drivers may assume that per-cpu resources have
1399 * been allocated. So unless they're explicitly marked as
1400 * being able to cope (CON_ANYTIME) don't call them until
1401 * this CPU is officially up.
1402 */
1403static inline int can_use_console(unsigned int cpu)
1404{
1405 return cpu_online(cpu) || have_callable_console();
1406}
1407
1408/*
1409 * Try to get console ownership to actually show the kernel
1410 * messages from a 'printk'. Return true (and with the
Torben Hohnac751ef2011-01-25 15:07:35 -08001411 * console_lock held, and 'console_locked' set) if it
Linus Torvalds266c2e02008-03-24 19:25:08 -07001412 * is successful, false otherwise.
Linus Torvalds266c2e02008-03-24 19:25:08 -07001413 */
Torben Hohnac751ef2011-01-25 15:07:35 -08001414static int console_trylock_for_printk(unsigned int cpu)
Linus Torvalds266c2e02008-03-24 19:25:08 -07001415{
Jan Kara608873c2014-06-04 16:11:35 -07001416 if (!console_trylock())
1417 return 0;
1418 /*
1419 * If we can't use the console, we need to release the console
1420 * semaphore by hand to avoid flushing the buffer. We need to hold the
1421 * console semaphore in order to do this test safely.
1422 */
1423 if (!can_use_console(cpu)) {
1424 console_locked = 0;
Peter Zijlstra0b5e1c52011-06-07 11:15:33 +02001425 up(&console_sem);
Jan Kara608873c2014-06-04 16:11:35 -07001426 return 0;
1427 }
1428 return 1;
Linus Torvalds266c2e02008-03-24 19:25:08 -07001429}
Ingo Molnar32a76002008-01-25 21:07:58 +01001430
Dave Youngaf913222009-09-22 16:43:33 -07001431int printk_delay_msec __read_mostly;
1432
1433static inline void printk_delay(void)
1434{
1435 if (unlikely(printk_delay_msec)) {
1436 int m = printk_delay_msec;
1437
1438 while (m--) {
1439 mdelay(1);
1440 touch_nmi_watchdog();
1441 }
1442 }
1443}
1444
Kay Sievers084681d2012-06-28 09:38:53 +02001445/*
1446 * Continuation lines are buffered, and not committed to the record buffer
1447 * until the line is complete, or a race forces it. The line fragments
1448 * though, are printed immediately to the consoles to ensure everything has
1449 * reached the console in case of a kernel crash.
1450 */
1451static struct cont {
1452 char buf[LOG_LINE_MAX];
1453 size_t len; /* length == 0 means unused buffer */
1454 size_t cons; /* bytes written to console */
1455 struct task_struct *owner; /* task of first print*/
1456 u64 ts_nsec; /* time of first print */
1457 u8 level; /* log level of first message */
1458 u8 facility; /* log level of first message */
Kay Sieverseab07262012-07-16 18:35:30 -07001459 enum log_flags flags; /* prefix, newline flags */
Kay Sievers084681d2012-06-28 09:38:53 +02001460 bool flushed:1; /* buffer sealed and committed */
1461} cont;
1462
Kay Sievers70498252012-07-16 18:35:29 -07001463static void cont_flush(enum log_flags flags)
Kay Sievers084681d2012-06-28 09:38:53 +02001464{
1465 if (cont.flushed)
1466 return;
1467 if (cont.len == 0)
1468 return;
1469
Kay Sieverseab07262012-07-16 18:35:30 -07001470 if (cont.cons) {
1471 /*
1472 * If a fragment of this line was directly flushed to the
1473 * console; wait for the console to pick up the rest of the
1474 * line. LOG_NOCONS suppresses a duplicated output.
1475 */
1476 log_store(cont.facility, cont.level, flags | LOG_NOCONS,
1477 cont.ts_nsec, NULL, 0, cont.buf, cont.len);
1478 cont.flags = flags;
1479 cont.flushed = true;
1480 } else {
1481 /*
1482 * If no fragment of this line ever reached the console,
1483 * just submit it to the store and free the buffer.
1484 */
1485 log_store(cont.facility, cont.level, flags, 0,
1486 NULL, 0, cont.buf, cont.len);
1487 cont.len = 0;
1488 }
Kay Sievers084681d2012-06-28 09:38:53 +02001489}
1490
1491static bool cont_add(int facility, int level, const char *text, size_t len)
1492{
1493 if (cont.len && cont.flushed)
1494 return false;
1495
1496 if (cont.len + len > sizeof(cont.buf)) {
Kay Sievers70498252012-07-16 18:35:29 -07001497 /* the line gets too long, split it up in separate records */
1498 cont_flush(LOG_CONT);
Kay Sievers084681d2012-06-28 09:38:53 +02001499 return false;
1500 }
1501
1502 if (!cont.len) {
1503 cont.facility = facility;
1504 cont.level = level;
1505 cont.owner = current;
1506 cont.ts_nsec = local_clock();
Kay Sieverseab07262012-07-16 18:35:30 -07001507 cont.flags = 0;
Kay Sievers084681d2012-06-28 09:38:53 +02001508 cont.cons = 0;
1509 cont.flushed = false;
1510 }
1511
1512 memcpy(cont.buf + cont.len, text, len);
1513 cont.len += len;
Kay Sieverseab07262012-07-16 18:35:30 -07001514
1515 if (cont.len > (sizeof(cont.buf) * 80) / 100)
1516 cont_flush(LOG_CONT);
1517
Kay Sievers084681d2012-06-28 09:38:53 +02001518 return true;
1519}
1520
1521static size_t cont_print_text(char *text, size_t size)
1522{
1523 size_t textlen = 0;
1524 size_t len;
1525
Kay Sieverseab07262012-07-16 18:35:30 -07001526 if (cont.cons == 0 && (console_prev & LOG_NEWLINE)) {
Kay Sievers084681d2012-06-28 09:38:53 +02001527 textlen += print_time(cont.ts_nsec, text);
1528 size -= textlen;
1529 }
1530
1531 len = cont.len - cont.cons;
1532 if (len > 0) {
1533 if (len+1 > size)
1534 len = size-1;
1535 memcpy(text + textlen, cont.buf + cont.cons, len);
1536 textlen += len;
1537 cont.cons = cont.len;
1538 }
1539
1540 if (cont.flushed) {
Kay Sieverseab07262012-07-16 18:35:30 -07001541 if (cont.flags & LOG_NEWLINE)
1542 text[textlen++] = '\n';
Kay Sievers084681d2012-06-28 09:38:53 +02001543 /* got everything, release buffer */
1544 cont.len = 0;
1545 }
1546 return textlen;
1547}
1548
Kay Sievers7ff95542012-05-03 02:29:13 +02001549asmlinkage int vprintk_emit(int facility, int level,
1550 const char *dict, size_t dictlen,
1551 const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Kay Sievers7ff95542012-05-03 02:29:13 +02001553 static int recursion_bug;
Kay Sievers7ff95542012-05-03 02:29:13 +02001554 static char textbuf[LOG_LINE_MAX];
1555 char *text = textbuf;
Kay Sieversc313af12012-05-14 20:46:27 +02001556 size_t text_len;
Kay Sievers5becfb12012-07-09 12:15:42 -07001557 enum log_flags lflags = 0;
Nick Andrewac60ad72008-05-12 21:21:04 +02001558 unsigned long flags;
Ingo Molnar32a76002008-01-25 21:07:58 +01001559 int this_cpu;
Kay Sievers7ff95542012-05-03 02:29:13 +02001560 int printed_len = 0;
Jan Kara608873c2014-06-04 16:11:35 -07001561 /* cpu currently holding logbuf_lock in this function */
1562 static volatile unsigned int logbuf_cpu = UINT_MAX;
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Andrew Cooks2fa72c82012-12-17 15:59:56 -08001565 boot_delay_msec(level);
Dave Youngaf913222009-09-22 16:43:33 -07001566 printk_delay();
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 /* This stops the holder of console_sem just where we want him */
Peter Zijlstra1a9a8ae2011-06-07 11:17:30 +02001569 local_irq_save(flags);
Ingo Molnar32a76002008-01-25 21:07:58 +01001570 this_cpu = smp_processor_id();
1571
1572 /*
1573 * Ouch, printk recursed into itself!
1574 */
Kay Sievers7ff95542012-05-03 02:29:13 +02001575 if (unlikely(logbuf_cpu == this_cpu)) {
Ingo Molnar32a76002008-01-25 21:07:58 +01001576 /*
1577 * If a crash is occurring during printk() on this CPU,
1578 * then try to get the crash message out but make sure
1579 * we can't deadlock. Otherwise just return to avoid the
1580 * recursion and return - but flag the recursion so that
1581 * it can be printed at the next appropriate moment:
1582 */
Peter Zijlstra94d24fc2011-06-07 11:17:30 +02001583 if (!oops_in_progress && !lockdep_recursing(current)) {
Tejun Heo3b8945e2008-05-12 21:21:04 +02001584 recursion_bug = 1;
Ingo Molnar32a76002008-01-25 21:07:58 +01001585 goto out_restore_irqs;
1586 }
1587 zap_locks();
1588 }
1589
Ingo Molnara0f1ccf2006-07-03 00:24:58 -07001590 lockdep_off();
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001591 raw_spin_lock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02001592 logbuf_cpu = this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Tejun Heo3b8945e2008-05-12 21:21:04 +02001594 if (recursion_bug) {
Kay Sievers7ff95542012-05-03 02:29:13 +02001595 static const char recursion_msg[] =
1596 "BUG: recent printk recursion!";
1597
Tejun Heo3b8945e2008-05-12 21:21:04 +02001598 recursion_bug = 0;
Petr Mladek034633c2014-06-04 16:11:33 -07001599 text_len = strlen(recursion_msg);
Kay Sievers7ff95542012-05-03 02:29:13 +02001600 /* emit KERN_CRIT message */
Petr Mladek034633c2014-06-04 16:11:33 -07001601 printed_len += log_store(0, 2, LOG_PREFIX|LOG_NEWLINE, 0,
1602 NULL, 0, recursion_msg, text_len);
Linus Torvalds5fd29d62009-06-16 10:57:02 -07001603 }
1604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 /*
Kay Sievers7ff95542012-05-03 02:29:13 +02001606 * The printf needs to come first; we need the syslog
1607 * prefix which might be passed-in as a parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 */
Kay Sieversc313af12012-05-14 20:46:27 +02001609 text_len = vscnprintf(text, sizeof(textbuf), fmt, args);
Nick Andrewac60ad72008-05-12 21:21:04 +02001610
Kay Sievers7ff95542012-05-03 02:29:13 +02001611 /* mark and strip a trailing newline */
Kay Sieversc313af12012-05-14 20:46:27 +02001612 if (text_len && text[text_len-1] == '\n') {
1613 text_len--;
Kay Sievers5becfb12012-07-09 12:15:42 -07001614 lflags |= LOG_NEWLINE;
Kay Sievers7ff95542012-05-03 02:29:13 +02001615 }
Kay Sievers9d90c8d2011-03-13 03:19:51 +01001616
Joe Perches088a52a2012-07-30 14:40:19 -07001617 /* strip kernel syslog prefix and extract log level or control flags */
1618 if (facility == 0) {
1619 int kern_level = printk_get_level(text);
1620
1621 if (kern_level) {
1622 const char *end_of_header = printk_skip_level(text);
1623 switch (kern_level) {
1624 case '0' ... '7':
1625 if (level == -1)
1626 level = kern_level - '0';
1627 case 'd': /* KERN_DEFAULT */
1628 lflags |= LOG_PREFIX;
Joe Perches088a52a2012-07-30 14:40:19 -07001629 }
Petr Mladeke8c42d32014-04-03 14:48:41 -07001630 /*
1631 * No need to check length here because vscnprintf
1632 * put '\0' at the end of the string. Only valid and
1633 * newly printed level is detected.
1634 */
Joe Perches088a52a2012-07-30 14:40:19 -07001635 text_len -= end_of_header - text;
1636 text = (char *)end_of_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001638 }
Nick Andrewac60ad72008-05-12 21:21:04 +02001639
Kay Sieversc313af12012-05-14 20:46:27 +02001640 if (level == -1)
1641 level = default_message_loglevel;
1642
Kay Sievers5becfb12012-07-09 12:15:42 -07001643 if (dict)
1644 lflags |= LOG_PREFIX|LOG_NEWLINE;
Kay Sievers7ff95542012-05-03 02:29:13 +02001645
Kay Sievers5becfb12012-07-09 12:15:42 -07001646 if (!(lflags & LOG_NEWLINE)) {
Kay Sievers084681d2012-06-28 09:38:53 +02001647 /*
1648 * Flush the conflicting buffer. An earlier newline was missing,
1649 * or another task also prints continuation lines.
1650 */
Kay Sievers5becfb12012-07-09 12:15:42 -07001651 if (cont.len && (lflags & LOG_PREFIX || cont.owner != current))
Kay Sieverseab07262012-07-16 18:35:30 -07001652 cont_flush(LOG_NEWLINE);
Kay Sieversc313af12012-05-14 20:46:27 +02001653
Kay Sievers084681d2012-06-28 09:38:53 +02001654 /* buffer line if possible, otherwise store it right away */
Petr Mladek034633c2014-06-04 16:11:33 -07001655 if (cont_add(facility, level, text, text_len))
1656 printed_len += text_len;
1657 else
1658 printed_len += log_store(facility, level,
1659 lflags | LOG_CONT, 0,
1660 dict, dictlen, text, text_len);
Kay Sievers084681d2012-06-28 09:38:53 +02001661 } else {
1662 bool stored = false;
1663
1664 /*
Steven Rostedtd3620822012-06-29 11:40:11 -04001665 * If an earlier newline was missing and it was the same task,
1666 * either merge it with the current buffer and flush, or if
1667 * there was a race with interrupts (prefix == true) then just
1668 * flush it out and store this line separately.
Arun KS1d3fa372014-01-23 15:54:19 -08001669 * If the preceding printk was from a different task and missed
1670 * a newline, flush and append the newline.
Kay Sievers084681d2012-06-28 09:38:53 +02001671 */
Arun KS1d3fa372014-01-23 15:54:19 -08001672 if (cont.len) {
1673 if (cont.owner == current && !(lflags & LOG_PREFIX))
1674 stored = cont_add(facility, level, text,
1675 text_len);
Kay Sieverseab07262012-07-16 18:35:30 -07001676 cont_flush(LOG_NEWLINE);
Kay Sieversc313af12012-05-14 20:46:27 +02001677 }
Kay Sievers084681d2012-06-28 09:38:53 +02001678
Petr Mladek034633c2014-06-04 16:11:33 -07001679 if (stored)
1680 printed_len += text_len;
1681 else
1682 printed_len += log_store(facility, level, lflags, 0,
1683 dict, dictlen, text, text_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 }
1685
Jan Kara608873c2014-06-04 16:11:35 -07001686 logbuf_cpu = UINT_MAX;
1687 raw_spin_unlock(&logbuf_lock);
Linus Torvalds266c2e02008-03-24 19:25:08 -07001688 /*
Kay Sievers7ff95542012-05-03 02:29:13 +02001689 * Try to acquire and then immediately release the console semaphore.
1690 * The release will print out buffers and wake up /dev/kmsg and syslog()
1691 * users.
Linus Torvalds266c2e02008-03-24 19:25:08 -07001692 */
Torben Hohnac751ef2011-01-25 15:07:35 -08001693 if (console_trylock_for_printk(this_cpu))
1694 console_unlock();
Michael Ellerman76a8ad22006-06-25 05:47:40 -07001695
Linus Torvalds266c2e02008-03-24 19:25:08 -07001696 lockdep_on();
Ingo Molnar32a76002008-01-25 21:07:58 +01001697out_restore_irqs:
Peter Zijlstra1a9a8ae2011-06-07 11:17:30 +02001698 local_irq_restore(flags);
Michael Ellerman76a8ad22006-06-25 05:47:40 -07001699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 return printed_len;
1701}
Kay Sievers7ff95542012-05-03 02:29:13 +02001702EXPORT_SYMBOL(vprintk_emit);
1703
1704asmlinkage int vprintk(const char *fmt, va_list args)
1705{
1706 return vprintk_emit(0, -1, NULL, 0, fmt, args);
1707}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708EXPORT_SYMBOL(vprintk);
1709
Kay Sievers7ff95542012-05-03 02:29:13 +02001710asmlinkage int printk_emit(int facility, int level,
1711 const char *dict, size_t dictlen,
1712 const char *fmt, ...)
1713{
1714 va_list args;
1715 int r;
1716
1717 va_start(args, fmt);
1718 r = vprintk_emit(facility, level, dict, dictlen, fmt, args);
1719 va_end(args);
1720
1721 return r;
1722}
1723EXPORT_SYMBOL(printk_emit);
1724
1725/**
1726 * printk - print a kernel message
1727 * @fmt: format string
1728 *
1729 * This is printk(). It can be called from any context. We want it to work.
1730 *
1731 * We try to grab the console_lock. If we succeed, it's easy - we log the
1732 * output and call the console drivers. If we fail to get the semaphore, we
1733 * place the output into the log buffer and return. The current holder of
1734 * the console_sem will notice the new output in console_unlock(); and will
1735 * send it to the consoles before releasing the lock.
1736 *
1737 * One effect of this deferred printing is that code which calls printk() and
1738 * then changes console_loglevel may break. This is because console_loglevel
1739 * is inspected when the actual printing occurs.
1740 *
1741 * See also:
1742 * printf(3)
1743 *
1744 * See the vsnprintf() documentation for format string extensions over C99.
1745 */
Andi Kleen722a9f92014-05-02 00:44:38 +02001746asmlinkage __visible int printk(const char *fmt, ...)
Kay Sievers7ff95542012-05-03 02:29:13 +02001747{
1748 va_list args;
1749 int r;
1750
1751#ifdef CONFIG_KGDB_KDB
1752 if (unlikely(kdb_trap_printk)) {
1753 va_start(args, fmt);
1754 r = vkdb_printf(fmt, args);
1755 va_end(args);
1756 return r;
1757 }
1758#endif
1759 va_start(args, fmt);
1760 r = vprintk_emit(0, -1, NULL, 0, fmt, args);
1761 va_end(args);
1762
1763 return r;
1764}
1765EXPORT_SYMBOL(printk);
Kay Sievers7f3a7812012-05-09 01:37:51 +02001766
Kay Sievers96efedf2012-07-16 18:35:29 -07001767#else /* CONFIG_PRINTK */
Matt Mackalld59745c2005-05-01 08:59:02 -07001768
Kay Sievers70498252012-07-16 18:35:29 -07001769#define LOG_LINE_MAX 0
1770#define PREFIX_MAX 0
Kay Sievers7f3a7812012-05-09 01:37:51 +02001771#define LOG_LINE_MAX 0
Kay Sievers96efedf2012-07-16 18:35:29 -07001772static u64 syslog_seq;
1773static u32 syslog_idx;
Kay Sieverseab07262012-07-16 18:35:30 -07001774static u64 console_seq;
1775static u32 console_idx;
Kay Sievers96efedf2012-07-16 18:35:29 -07001776static enum log_flags syslog_prev;
1777static u64 log_first_seq;
1778static u32 log_first_idx;
1779static u64 log_next_seq;
Kay Sieverseab07262012-07-16 18:35:30 -07001780static enum log_flags console_prev;
Kay Sievers084681d2012-06-28 09:38:53 +02001781static struct cont {
1782 size_t len;
1783 size_t cons;
1784 u8 level;
1785 bool flushed:1;
1786} cont;
Joe Perches62e32ac2013-07-31 13:53:47 -07001787static struct printk_log *log_from_idx(u32 idx) { return NULL; }
Kay Sievers7f3a7812012-05-09 01:37:51 +02001788static u32 log_next(u32 idx) { return 0; }
Kay Sievers7f3a7812012-05-09 01:37:51 +02001789static void call_console_drivers(int level, const char *text, size_t len) {}
Joe Perches62e32ac2013-07-31 13:53:47 -07001790static size_t msg_print_text(const struct printk_log *msg, enum log_flags prev,
Kay Sievers5becfb12012-07-09 12:15:42 -07001791 bool syslog, char *buf, size_t size) { return 0; }
Kay Sievers084681d2012-06-28 09:38:53 +02001792static size_t cont_print_text(char *text, size_t size) { return 0; }
Matt Mackalld59745c2005-05-01 08:59:02 -07001793
Kay Sievers7f3a7812012-05-09 01:37:51 +02001794#endif /* CONFIG_PRINTK */
Matt Mackalld59745c2005-05-01 08:59:02 -07001795
Thomas Gleixnerd0380e62013-04-29 16:17:18 -07001796#ifdef CONFIG_EARLY_PRINTK
1797struct console *early_console;
1798
1799void early_vprintk(const char *fmt, va_list ap)
1800{
1801 if (early_console) {
1802 char buf[512];
1803 int n = vscnprintf(buf, sizeof(buf), fmt, ap);
1804
1805 early_console->write(early_console, buf, n);
1806 }
1807}
1808
Andi Kleen722a9f92014-05-02 00:44:38 +02001809asmlinkage __visible void early_printk(const char *fmt, ...)
Thomas Gleixnerd0380e62013-04-29 16:17:18 -07001810{
1811 va_list ap;
1812
1813 va_start(ap, fmt);
1814 early_vprintk(fmt, ap);
1815 va_end(ap);
1816}
1817#endif
1818
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001819static int __add_preferred_console(char *name, int idx, char *options,
1820 char *brl_options)
1821{
1822 struct console_cmdline *c;
1823 int i;
1824
1825 /*
1826 * See if this tty is not yet registered, and
1827 * if we have a slot free.
1828 */
Joe Perches23475402013-07-31 13:53:46 -07001829 for (i = 0, c = console_cmdline;
1830 i < MAX_CMDLINECONSOLES && c->name[0];
1831 i++, c++) {
1832 if (strcmp(c->name, name) == 0 && c->index == idx) {
1833 if (!brl_options)
1834 selected_console = i;
1835 return 0;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001836 }
Joe Perches23475402013-07-31 13:53:46 -07001837 }
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001838 if (i == MAX_CMDLINECONSOLES)
1839 return -E2BIG;
1840 if (!brl_options)
1841 selected_console = i;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001842 strlcpy(c->name, name, sizeof(c->name));
1843 c->options = options;
Joe Perchesbbeddf52013-07-31 13:53:45 -07001844 braille_set_options(c, brl_options);
1845
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001846 c->index = idx;
1847 return 0;
1848}
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001849/*
1850 * Set up a list of consoles. Called from init/main.c
1851 */
1852static int __init console_setup(char *str)
1853{
Yinghai Lueaa944a2007-07-15 23:37:27 -07001854 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001855 char *s, *options, *brl_options = NULL;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001856 int idx;
1857
Joe Perchesbbeddf52013-07-31 13:53:45 -07001858 if (_braille_console_setup(&str, &brl_options))
1859 return 1;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001860
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001861 /*
1862 * Decode str into name, index, options.
1863 */
1864 if (str[0] >= '0' && str[0] <= '9') {
Yinghai Lueaa944a2007-07-15 23:37:27 -07001865 strcpy(buf, "ttyS");
1866 strncpy(buf + 4, str, sizeof(buf) - 5);
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001867 } else {
Yinghai Lueaa944a2007-07-15 23:37:27 -07001868 strncpy(buf, str, sizeof(buf) - 1);
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001869 }
Yinghai Lueaa944a2007-07-15 23:37:27 -07001870 buf[sizeof(buf) - 1] = 0;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001871 if ((options = strchr(str, ',')) != NULL)
1872 *(options++) = 0;
1873#ifdef __sparc__
1874 if (!strcmp(str, "ttya"))
Yinghai Lueaa944a2007-07-15 23:37:27 -07001875 strcpy(buf, "ttyS0");
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001876 if (!strcmp(str, "ttyb"))
Yinghai Lueaa944a2007-07-15 23:37:27 -07001877 strcpy(buf, "ttyS1");
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001878#endif
Yinghai Lueaa944a2007-07-15 23:37:27 -07001879 for (s = buf; *s; s++)
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001880 if ((*s >= '0' && *s <= '9') || *s == ',')
1881 break;
1882 idx = simple_strtoul(s, NULL, 10);
1883 *s = 0;
1884
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001885 __add_preferred_console(buf, idx, options, brl_options);
Markus Armbruster9e124fe2008-05-26 23:31:07 +01001886 console_set_on_cmdline = 1;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001887 return 1;
1888}
1889__setup("console=", console_setup);
1890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891/**
Matt Mackall3c0547b2005-05-16 21:53:47 -07001892 * add_preferred_console - add a device to the list of preferred consoles.
Martin Waitzddad86c2005-11-13 16:08:14 -08001893 * @name: device name
1894 * @idx: device index
1895 * @options: options for this console
Matt Mackall3c0547b2005-05-16 21:53:47 -07001896 *
1897 * The last preferred console added will be used for kernel messages
1898 * and stdin/out/err for init. Normally this is used by console_setup
1899 * above to handle user-supplied console arguments; however it can also
1900 * be used by arch-specific code either to override the user or more
1901 * commonly to provide a default console (ie from PROM variables) when
1902 * the user has not supplied one.
1903 */
David S. Millerfb445ee2007-12-29 01:19:49 -08001904int add_preferred_console(char *name, int idx, char *options)
Matt Mackall3c0547b2005-05-16 21:53:47 -07001905{
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001906 return __add_preferred_console(name, idx, options, NULL);
Matt Mackall3c0547b2005-05-16 21:53:47 -07001907}
1908
Daniel Ritzb6b1d872007-08-03 16:07:43 +02001909int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
Yinghai Lu18a8bd92007-07-15 23:37:59 -07001910{
1911 struct console_cmdline *c;
1912 int i;
1913
Joe Perches23475402013-07-31 13:53:46 -07001914 for (i = 0, c = console_cmdline;
1915 i < MAX_CMDLINECONSOLES && c->name[0];
1916 i++, c++)
1917 if (strcmp(c->name, name) == 0 && c->index == idx) {
1918 strlcpy(c->name, name_new, sizeof(c->name));
1919 c->name[sizeof(c->name) - 1] = 0;
1920 c->options = options;
1921 c->index = idx_new;
1922 return i;
Yinghai Lu18a8bd92007-07-15 23:37:59 -07001923 }
1924 /* not found */
1925 return -1;
1926}
1927
Rusty Russell2329abf2012-01-13 09:32:18 +10301928bool console_suspend_enabled = 1;
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001929EXPORT_SYMBOL(console_suspend_enabled);
1930
1931static int __init console_suspend_disable(char *str)
1932{
1933 console_suspend_enabled = 0;
1934 return 1;
1935}
1936__setup("no_console_suspend", console_suspend_disable);
Yanmin Zhang134620f2011-10-31 17:11:27 -07001937module_param_named(console_suspend, console_suspend_enabled,
1938 bool, S_IRUGO | S_IWUSR);
1939MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
1940 " and hibernate operations");
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001941
Matt Mackall3c0547b2005-05-16 21:53:47 -07001942/**
Linus Torvalds557240b2006-06-19 18:16:01 -07001943 * suspend_console - suspend the console subsystem
1944 *
1945 * This disables printk() while we go into suspend states
1946 */
1947void suspend_console(void)
1948{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001949 if (!console_suspend_enabled)
1950 return;
Pavel Machek0d630812008-07-23 21:28:32 -07001951 printk("Suspending console(s) (use no_console_suspend to debug)\n");
Torben Hohnac751ef2011-01-25 15:07:35 -08001952 console_lock();
Linus Torvalds557240b2006-06-19 18:16:01 -07001953 console_suspended = 1;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001954 up(&console_sem);
Jane Li72581482014-04-03 14:48:45 -07001955 mutex_release(&console_lock_dep_map, 1, _RET_IP_);
Linus Torvalds557240b2006-06-19 18:16:01 -07001956}
1957
1958void resume_console(void)
1959{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001960 if (!console_suspend_enabled)
1961 return;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001962 down(&console_sem);
Jane Li72581482014-04-03 14:48:45 -07001963 mutex_acquire(&console_lock_dep_map, 0, 0, _RET_IP_);
Linus Torvalds557240b2006-06-19 18:16:01 -07001964 console_suspended = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08001965 console_unlock();
Linus Torvalds557240b2006-06-19 18:16:01 -07001966}
1967
1968/**
Kevin Cernekee034260d2010-06-03 22:11:25 -07001969 * console_cpu_notify - print deferred console messages after CPU hotplug
1970 * @self: notifier struct
1971 * @action: CPU hotplug event
1972 * @hcpu: unused
1973 *
1974 * If printk() is called from a CPU that is not online yet, the messages
1975 * will be spooled but will not show up on the console. This function is
1976 * called when a new CPU comes online (or fails to come up), and ensures
1977 * that any such output gets printed.
1978 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04001979static int console_cpu_notify(struct notifier_block *self,
Kevin Cernekee034260d2010-06-03 22:11:25 -07001980 unsigned long action, void *hcpu)
1981{
1982 switch (action) {
1983 case CPU_ONLINE:
1984 case CPU_DEAD:
Kevin Cernekee034260d2010-06-03 22:11:25 -07001985 case CPU_DOWN_FAILED:
1986 case CPU_UP_CANCELED:
Torben Hohnac751ef2011-01-25 15:07:35 -08001987 console_lock();
1988 console_unlock();
Kevin Cernekee034260d2010-06-03 22:11:25 -07001989 }
1990 return NOTIFY_OK;
1991}
1992
1993/**
Torben Hohnac751ef2011-01-25 15:07:35 -08001994 * console_lock - lock the console system for exclusive use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 *
Torben Hohnac751ef2011-01-25 15:07:35 -08001996 * Acquires a lock which guarantees that the caller has
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 * exclusive access to the console system and the console_drivers list.
1998 *
1999 * Can sleep, returns nothing.
2000 */
Torben Hohnac751ef2011-01-25 15:07:35 -08002001void console_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
Daniel Vetter6b898c02012-09-17 23:03:31 +00002003 might_sleep();
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 down(&console_sem);
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01002006 if (console_suspended)
2007 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 console_locked = 1;
2009 console_may_schedule = 1;
Daniel Vetterdaee7792012-09-22 19:52:11 +02002010 mutex_acquire(&console_lock_dep_map, 0, 0, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
Torben Hohnac751ef2011-01-25 15:07:35 -08002012EXPORT_SYMBOL(console_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Torben Hohnac751ef2011-01-25 15:07:35 -08002014/**
2015 * console_trylock - try to lock the console system for exclusive use.
2016 *
2017 * Tried to acquire a lock which guarantees that the caller has
2018 * exclusive access to the console system and the console_drivers list.
2019 *
2020 * returns 1 on success, and 0 on failure to acquire the lock.
2021 */
2022int console_trylock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
2024 if (down_trylock(&console_sem))
Torben Hohnac751ef2011-01-25 15:07:35 -08002025 return 0;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01002026 if (console_suspended) {
2027 up(&console_sem);
Torben Hohnac751ef2011-01-25 15:07:35 -08002028 return 0;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01002029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 console_locked = 1;
2031 console_may_schedule = 0;
Daniel Vetterdaee7792012-09-22 19:52:11 +02002032 mutex_acquire(&console_lock_dep_map, 0, 1, _RET_IP_);
Torben Hohnac751ef2011-01-25 15:07:35 -08002033 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034}
Torben Hohnac751ef2011-01-25 15:07:35 -08002035EXPORT_SYMBOL(console_trylock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037int is_console_locked(void)
2038{
2039 return console_locked;
2040}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Kay Sieverseab07262012-07-16 18:35:30 -07002042static void console_cont_flush(char *text, size_t size)
2043{
2044 unsigned long flags;
2045 size_t len;
2046
2047 raw_spin_lock_irqsave(&logbuf_lock, flags);
2048
2049 if (!cont.len)
2050 goto out;
2051
2052 /*
2053 * We still queue earlier records, likely because the console was
2054 * busy. The earlier ones need to be printed before this one, we
2055 * did not flush any fragment so far, so just let it queue up.
2056 */
2057 if (console_seq < log_next_seq && !cont.cons)
2058 goto out;
2059
2060 len = cont_print_text(text, size);
2061 raw_spin_unlock(&logbuf_lock);
2062 stop_critical_timings();
2063 call_console_drivers(cont.level, text, len);
2064 start_critical_timings();
2065 local_irq_restore(flags);
2066 return;
2067out:
2068 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2069}
Kay Sievers7ff95542012-05-03 02:29:13 +02002070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071/**
Torben Hohnac751ef2011-01-25 15:07:35 -08002072 * console_unlock - unlock the console system
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002074 * Releases the console_lock which the caller holds on the console system
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 * and the console driver list.
2076 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002077 * While the console_lock was held, console output may have been buffered
2078 * by printk(). If this is the case, console_unlock(); emits
2079 * the output prior to releasing the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 *
Kay Sievers7f3a7812012-05-09 01:37:51 +02002081 * If there is output waiting, we wake /dev/kmsg and syslog() users.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002083 * console_unlock(); may be called from any context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 */
Torben Hohnac751ef2011-01-25 15:07:35 -08002085void console_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
Kay Sievers70498252012-07-16 18:35:29 -07002087 static char text[LOG_LINE_MAX + PREFIX_MAX];
Kay Sievers7ff95542012-05-03 02:29:13 +02002088 static u64 seen_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 unsigned long flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02002090 bool wake_klogd = false;
2091 bool retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Linus Torvalds557240b2006-06-19 18:16:01 -07002093 if (console_suspended) {
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01002094 up(&console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -07002095 return;
2096 }
Antonino A. Daplas78944e52006-08-05 12:14:16 -07002097
2098 console_may_schedule = 0;
2099
Kay Sievers084681d2012-06-28 09:38:53 +02002100 /* flush buffered message fragment immediately to console */
Kay Sieverseab07262012-07-16 18:35:30 -07002101 console_cont_flush(text, sizeof(text));
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002102again:
Kay Sievers7ff95542012-05-03 02:29:13 +02002103 for (;;) {
Joe Perches62e32ac2013-07-31 13:53:47 -07002104 struct printk_log *msg;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02002105 size_t len;
Kay Sievers7ff95542012-05-03 02:29:13 +02002106 int level;
2107
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002108 raw_spin_lock_irqsave(&logbuf_lock, flags);
Kay Sievers7ff95542012-05-03 02:29:13 +02002109 if (seen_seq != log_next_seq) {
2110 wake_klogd = true;
2111 seen_seq = log_next_seq;
2112 }
2113
2114 if (console_seq < log_first_seq) {
2115 /* messages are gone, move to first one */
2116 console_seq = log_first_seq;
2117 console_idx = log_first_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002118 console_prev = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02002119 }
Kay Sievers084681d2012-06-28 09:38:53 +02002120skip:
Kay Sievers7ff95542012-05-03 02:29:13 +02002121 if (console_seq == log_next_seq)
2122 break;
2123
2124 msg = log_from_idx(console_idx);
Kay Sievers084681d2012-06-28 09:38:53 +02002125 if (msg->flags & LOG_NOCONS) {
2126 /*
2127 * Skip record we have buffered and already printed
2128 * directly to the console when we received it.
2129 */
2130 console_idx = log_next(console_idx);
2131 console_seq++;
Kay Sievers68b65072012-07-06 09:50:09 -07002132 /*
2133 * We will get here again when we register a new
2134 * CON_PRINTBUFFER console. Clear the flag so we
2135 * will properly dump everything later.
2136 */
2137 msg->flags &= ~LOG_NOCONS;
Kay Sieverseab07262012-07-16 18:35:30 -07002138 console_prev = msg->flags;
Kay Sievers084681d2012-06-28 09:38:53 +02002139 goto skip;
2140 }
Kay Sievers649e6ee2012-05-10 04:30:45 +02002141
Kay Sievers084681d2012-06-28 09:38:53 +02002142 level = msg->level;
Kay Sievers5becfb12012-07-09 12:15:42 -07002143 len = msg_print_text(msg, console_prev, false,
2144 text, sizeof(text));
Kay Sievers7ff95542012-05-03 02:29:13 +02002145 console_idx = log_next(console_idx);
2146 console_seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07002147 console_prev = msg->flags;
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002148 raw_spin_unlock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02002149
Steven Rostedt81d68a92008-05-12 21:20:42 +02002150 stop_critical_timings(); /* don't trace print latency */
Kay Sievers7ff95542012-05-03 02:29:13 +02002151 call_console_drivers(level, text, len);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002152 start_critical_timings();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 local_irq_restore(flags);
2154 }
2155 console_locked = 0;
Daniel Vetterdaee7792012-09-22 19:52:11 +02002156 mutex_release(&console_lock_dep_map, 1, _RET_IP_);
Feng Tangfe3d8ad2011-03-22 16:34:21 -07002157
2158 /* Release the exclusive_console once it is used */
2159 if (unlikely(exclusive_console))
2160 exclusive_console = NULL;
2161
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002162 raw_spin_unlock(&logbuf_lock);
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002163
Peter Zijlstra0b5e1c52011-06-07 11:15:33 +02002164 up(&console_sem);
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002165
2166 /*
2167 * Someone could have filled up the buffer again, so re-check if there's
2168 * something to flush. In case we cannot trylock the console_sem again,
2169 * there's a new owner and the console_unlock() from them will do the
2170 * flush, no worries.
2171 */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002172 raw_spin_lock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02002173 retry = console_seq != log_next_seq;
Peter Zijlstra09dc3cf2011-12-08 14:34:13 -08002174 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2175
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002176 if (retry && console_trylock())
2177 goto again;
2178
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08002179 if (wake_klogd)
2180 wake_up_klogd();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
Torben Hohnac751ef2011-01-25 15:07:35 -08002182EXPORT_SYMBOL(console_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Martin Waitzddad86c2005-11-13 16:08:14 -08002184/**
2185 * console_conditional_schedule - yield the CPU if required
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 *
2187 * If the console code is currently allowed to sleep, and
2188 * if this CPU should yield the CPU to another task, do
2189 * so here.
2190 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002191 * Must be called within console_lock();.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 */
2193void __sched console_conditional_schedule(void)
2194{
2195 if (console_may_schedule)
2196 cond_resched();
2197}
2198EXPORT_SYMBOL(console_conditional_schedule);
2199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200void console_unblank(void)
2201{
2202 struct console *c;
2203
2204 /*
2205 * console_unblank can no longer be called in interrupt context unless
2206 * oops_in_progress is set to 1..
2207 */
2208 if (oops_in_progress) {
2209 if (down_trylock(&console_sem) != 0)
2210 return;
2211 } else
Torben Hohnac751ef2011-01-25 15:07:35 -08002212 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 console_locked = 1;
2215 console_may_schedule = 0;
Robin Getz4d091612009-07-01 21:08:37 -04002216 for_each_console(c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if ((c->flags & CON_ENABLED) && c->unblank)
2218 c->unblank();
Torben Hohnac751ef2011-01-25 15:07:35 -08002219 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
2222/*
2223 * Return the console tty driver structure and its associated index
2224 */
2225struct tty_driver *console_device(int *index)
2226{
2227 struct console *c;
2228 struct tty_driver *driver = NULL;
2229
Torben Hohnac751ef2011-01-25 15:07:35 -08002230 console_lock();
Robin Getz4d091612009-07-01 21:08:37 -04002231 for_each_console(c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 if (!c->device)
2233 continue;
2234 driver = c->device(c, index);
2235 if (driver)
2236 break;
2237 }
Torben Hohnac751ef2011-01-25 15:07:35 -08002238 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 return driver;
2240}
2241
2242/*
2243 * Prevent further output on the passed console device so that (for example)
2244 * serial drivers can disable console output before suspending a port, and can
2245 * re-enable output afterwards.
2246 */
2247void console_stop(struct console *console)
2248{
Torben Hohnac751ef2011-01-25 15:07:35 -08002249 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 console->flags &= ~CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08002251 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252}
2253EXPORT_SYMBOL(console_stop);
2254
2255void console_start(struct console *console)
2256{
Torben Hohnac751ef2011-01-25 15:07:35 -08002257 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 console->flags |= CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08002259 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260}
2261EXPORT_SYMBOL(console_start);
2262
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07002263static int __read_mostly keep_bootcon;
2264
2265static int __init keep_bootcon_setup(char *str)
2266{
2267 keep_bootcon = 1;
Andrew Morton27083ba2013-11-12 15:08:50 -08002268 pr_info("debug: skip boot console de-registration.\n");
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07002269
2270 return 0;
2271}
2272
2273early_param("keep_bootcon", keep_bootcon_setup);
2274
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275/*
2276 * The console driver calls this routine during kernel initialization
2277 * to register the console printing procedure with printk() and to
2278 * print any messages that were printed by the kernel before the
2279 * console driver was initialized.
Robin Getz4d091612009-07-01 21:08:37 -04002280 *
2281 * This can happen pretty early during the boot process (because of
2282 * early_printk) - sometimes before setup_arch() completes - be careful
2283 * of what kernel features are used - they may not be initialised yet.
2284 *
2285 * There are two types of consoles - bootconsoles (early_printk) and
2286 * "real" consoles (everything which is not a bootconsole) which are
2287 * handled differently.
2288 * - Any number of bootconsoles can be registered at any time.
2289 * - As soon as a "real" console is registered, all bootconsoles
2290 * will be unregistered automatically.
2291 * - Once a "real" console is registered, any attempt to register a
2292 * bootconsoles will be rejected
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 */
Robin Getz4d091612009-07-01 21:08:37 -04002294void register_console(struct console *newcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
Jesper Juhl40dc5652005-10-30 15:02:46 -08002296 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 unsigned long flags;
Robin Getz4d091612009-07-01 21:08:37 -04002298 struct console *bcon = NULL;
Joe Perches23475402013-07-31 13:53:46 -07002299 struct console_cmdline *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Andreas Bießmann16cf48a2013-08-02 12:23:34 +02002301 if (console_drivers)
2302 for_each_console(bcon)
2303 if (WARN(bcon == newcon,
2304 "console '%s%d' already registered\n",
2305 bcon->name, bcon->index))
2306 return;
2307
Robin Getz4d091612009-07-01 21:08:37 -04002308 /*
2309 * before we register a new CON_BOOT console, make sure we don't
2310 * already have a valid console
2311 */
2312 if (console_drivers && newcon->flags & CON_BOOT) {
2313 /* find the last or real console */
2314 for_each_console(bcon) {
2315 if (!(bcon->flags & CON_BOOT)) {
Andrew Morton27083ba2013-11-12 15:08:50 -08002316 pr_info("Too late to register bootconsole %s%d\n",
Robin Getz4d091612009-07-01 21:08:37 -04002317 newcon->name, newcon->index);
2318 return;
2319 }
2320 }
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002321 }
2322
Robin Getz4d091612009-07-01 21:08:37 -04002323 if (console_drivers && console_drivers->flags & CON_BOOT)
2324 bcon = console_drivers;
2325
2326 if (preferred_console < 0 || bcon || !console_drivers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 preferred_console = selected_console;
2328
Robin Getz4d091612009-07-01 21:08:37 -04002329 if (newcon->early_setup)
2330 newcon->early_setup();
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002331
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 /*
2333 * See if we want to use this console driver. If we
2334 * didn't select a console we take the first one
2335 * that registers here.
2336 */
2337 if (preferred_console < 0) {
Robin Getz4d091612009-07-01 21:08:37 -04002338 if (newcon->index < 0)
2339 newcon->index = 0;
2340 if (newcon->setup == NULL ||
2341 newcon->setup(newcon, NULL) == 0) {
2342 newcon->flags |= CON_ENABLED;
2343 if (newcon->device) {
2344 newcon->flags |= CON_CONSDEV;
Jan Kiszkacd3a1b82008-05-12 21:21:04 +02002345 preferred_console = 0;
2346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 }
2348 }
2349
2350 /*
2351 * See if this console matches one we selected on
2352 * the command line.
2353 */
Joe Perches23475402013-07-31 13:53:46 -07002354 for (i = 0, c = console_cmdline;
2355 i < MAX_CMDLINECONSOLES && c->name[0];
2356 i++, c++) {
2357 if (strcmp(c->name, newcon->name) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 continue;
Robin Getz4d091612009-07-01 21:08:37 -04002359 if (newcon->index >= 0 &&
Joe Perches23475402013-07-31 13:53:46 -07002360 newcon->index != c->index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 continue;
Robin Getz4d091612009-07-01 21:08:37 -04002362 if (newcon->index < 0)
Joe Perches23475402013-07-31 13:53:46 -07002363 newcon->index = c->index;
Joe Perchesbbeddf52013-07-31 13:53:45 -07002364
Joe Perches23475402013-07-31 13:53:46 -07002365 if (_braille_register_console(newcon, c))
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002366 return;
Joe Perchesbbeddf52013-07-31 13:53:45 -07002367
Robin Getz4d091612009-07-01 21:08:37 -04002368 if (newcon->setup &&
2369 newcon->setup(newcon, console_cmdline[i].options) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 break;
Robin Getz4d091612009-07-01 21:08:37 -04002371 newcon->flags |= CON_ENABLED;
Joe Perches23475402013-07-31 13:53:46 -07002372 newcon->index = c->index;
Greg Edwardsab4af032005-06-23 00:09:05 -07002373 if (i == selected_console) {
Robin Getz4d091612009-07-01 21:08:37 -04002374 newcon->flags |= CON_CONSDEV;
Greg Edwardsab4af032005-06-23 00:09:05 -07002375 preferred_console = selected_console;
2376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 break;
2378 }
2379
Robin Getz4d091612009-07-01 21:08:37 -04002380 if (!(newcon->flags & CON_ENABLED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return;
2382
Robin Getz8259cf42009-07-09 13:08:37 -04002383 /*
2384 * If we have a bootconsole, and are switching to a real console,
2385 * don't print everything out again, since when the boot console, and
2386 * the real console are the same physical device, it's annoying to
2387 * see the beginning boot messages twice
2388 */
2389 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
Robin Getz4d091612009-07-01 21:08:37 -04002390 newcon->flags &= ~CON_PRINTBUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
2392 /*
2393 * Put this console in the list - keep the
2394 * preferred driver at the head of the list.
2395 */
Torben Hohnac751ef2011-01-25 15:07:35 -08002396 console_lock();
Robin Getz4d091612009-07-01 21:08:37 -04002397 if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
2398 newcon->next = console_drivers;
2399 console_drivers = newcon;
2400 if (newcon->next)
2401 newcon->next->flags &= ~CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 } else {
Robin Getz4d091612009-07-01 21:08:37 -04002403 newcon->next = console_drivers->next;
2404 console_drivers->next = newcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 }
Robin Getz4d091612009-07-01 21:08:37 -04002406 if (newcon->flags & CON_PRINTBUFFER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 /*
Torben Hohnac751ef2011-01-25 15:07:35 -08002408 * console_unlock(); will print out the buffered messages
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 * for us.
2410 */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002411 raw_spin_lock_irqsave(&logbuf_lock, flags);
Kay Sievers7ff95542012-05-03 02:29:13 +02002412 console_seq = syslog_seq;
2413 console_idx = syslog_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002414 console_prev = syslog_prev;
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002415 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
Feng Tangfe3d8ad2011-03-22 16:34:21 -07002416 /*
2417 * We're about to replay the log buffer. Only do this to the
2418 * just-registered console to avoid excessive message spam to
2419 * the already-registered consoles.
2420 */
2421 exclusive_console = newcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 }
Torben Hohnac751ef2011-01-25 15:07:35 -08002423 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01002424 console_sysfs_notify();
Robin Getz8259cf42009-07-09 13:08:37 -04002425
2426 /*
2427 * By unregistering the bootconsoles after we enable the real console
2428 * we get the "console xxx enabled" message on all the consoles -
2429 * boot consoles, real consoles, etc - this is to ensure that end
2430 * users know there might be something in the kernel's log buffer that
2431 * went to the bootconsole (that they do not see on the real console)
2432 */
Andrew Morton27083ba2013-11-12 15:08:50 -08002433 pr_info("%sconsole [%s%d] enabled\n",
Kees Cook6b802392013-11-12 15:08:49 -08002434 (newcon->flags & CON_BOOT) ? "boot" : "" ,
2435 newcon->name, newcon->index);
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07002436 if (bcon &&
2437 ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
2438 !keep_bootcon) {
Kees Cook6b802392013-11-12 15:08:49 -08002439 /* We need to iterate through all boot consoles, to make
2440 * sure we print everything out, before we unregister them.
Robin Getz8259cf42009-07-09 13:08:37 -04002441 */
Robin Getz8259cf42009-07-09 13:08:37 -04002442 for_each_console(bcon)
2443 if (bcon->flags & CON_BOOT)
2444 unregister_console(bcon);
Robin Getz8259cf42009-07-09 13:08:37 -04002445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446}
2447EXPORT_SYMBOL(register_console);
2448
Jesper Juhl40dc5652005-10-30 15:02:46 -08002449int unregister_console(struct console *console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450{
Jesper Juhl40dc5652005-10-30 15:02:46 -08002451 struct console *a, *b;
Joe Perchesbbeddf52013-07-31 13:53:45 -07002452 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Andrew Morton27083ba2013-11-12 15:08:50 -08002454 pr_info("%sconsole [%s%d] disabled\n",
Kees Cook6b802392013-11-12 15:08:49 -08002455 (console->flags & CON_BOOT) ? "boot" : "" ,
2456 console->name, console->index);
2457
Joe Perchesbbeddf52013-07-31 13:53:45 -07002458 res = _braille_unregister_console(console);
2459 if (res)
2460 return res;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002461
Joe Perchesbbeddf52013-07-31 13:53:45 -07002462 res = 1;
Torben Hohnac751ef2011-01-25 15:07:35 -08002463 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 if (console_drivers == console) {
2465 console_drivers=console->next;
2466 res = 0;
Benjamin Herrenschmidte9b15b52005-11-23 13:37:44 -08002467 } else if (console_drivers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 for (a=console_drivers->next, b=console_drivers ;
2469 a; b=a, a=b->next) {
2470 if (a == console) {
2471 b->next = a->next;
2472 res = 0;
2473 break;
Jesper Juhl40dc5652005-10-30 15:02:46 -08002474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
2476 }
Jesper Juhl40dc5652005-10-30 15:02:46 -08002477
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002478 /*
Greg Edwardsab4af032005-06-23 00:09:05 -07002479 * If this isn't the last console and it has CON_CONSDEV set, we
2480 * need to set it on the next preferred console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 */
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002482 if (console_drivers != NULL && console->flags & CON_CONSDEV)
Greg Edwardsab4af032005-06-23 00:09:05 -07002483 console_drivers->flags |= CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
Stephen Chivers7fa21dd2014-05-14 08:04:39 +10002485 console->flags &= ~CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08002486 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01002487 console_sysfs_notify();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 return res;
2489}
2490EXPORT_SYMBOL(unregister_console);
Matt Mackalld59745c2005-05-01 08:59:02 -07002491
Kevin Cernekee034260d2010-06-03 22:11:25 -07002492static int __init printk_late_init(void)
Robin Getz0c5564b2007-08-20 15:22:47 -04002493{
Robin Getz4d091612009-07-01 21:08:37 -04002494 struct console *con;
2495
2496 for_each_console(con) {
Nishanth Aravamudan4c30c6f2011-08-25 15:59:11 -07002497 if (!keep_bootcon && con->flags & CON_BOOT) {
Sonic Zhang42c2c8c2009-08-06 15:58:11 -07002498 unregister_console(con);
Robin Getzcb00e992007-08-21 23:14:58 -04002499 }
Robin Getz0c5564b2007-08-20 15:22:47 -04002500 }
Kevin Cernekee034260d2010-06-03 22:11:25 -07002501 hotcpu_notifier(console_cpu_notify, 0);
Robin Getz0c5564b2007-08-20 15:22:47 -04002502 return 0;
2503}
Kevin Cernekee034260d2010-06-03 22:11:25 -07002504late_initcall(printk_late_init);
Robin Getz0c5564b2007-08-20 15:22:47 -04002505
Joe Perches7ef3d2f2008-02-08 04:21:25 -08002506#if defined CONFIG_PRINTK
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002507/*
2508 * Delayed printk version, for scheduler-internal messages:
2509 */
2510#define PRINTK_BUF_SIZE 512
2511
2512#define PRINTK_PENDING_WAKEUP 0x01
2513#define PRINTK_PENDING_SCHED 0x02
2514
2515static DEFINE_PER_CPU(int, printk_pending);
2516static DEFINE_PER_CPU(char [PRINTK_BUF_SIZE], printk_sched_buf);
2517
2518static void wake_up_klogd_work_func(struct irq_work *irq_work)
2519{
2520 int pending = __this_cpu_xchg(printk_pending, 0);
2521
2522 if (pending & PRINTK_PENDING_SCHED) {
2523 char *buf = __get_cpu_var(printk_sched_buf);
Andrew Morton27083ba2013-11-12 15:08:50 -08002524 pr_warn("[sched_delayed] %s", buf);
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002525 }
2526
2527 if (pending & PRINTK_PENDING_WAKEUP)
2528 wake_up_interruptible(&log_wait);
2529}
2530
2531static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) = {
2532 .func = wake_up_klogd_work_func,
2533 .flags = IRQ_WORK_LAZY,
2534};
2535
2536void wake_up_klogd(void)
2537{
2538 preempt_disable();
2539 if (waitqueue_active(&log_wait)) {
2540 this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP);
2541 irq_work_queue(&__get_cpu_var(wake_up_klogd_work));
2542 }
2543 preempt_enable();
2544}
Dave Young717115e2008-07-25 01:45:58 -07002545
Peter Zijlstra600e1452012-03-15 12:35:37 +01002546int printk_sched(const char *fmt, ...)
2547{
2548 unsigned long flags;
2549 va_list args;
2550 char *buf;
2551 int r;
2552
2553 local_irq_save(flags);
2554 buf = __get_cpu_var(printk_sched_buf);
2555
2556 va_start(args, fmt);
2557 r = vsnprintf(buf, PRINTK_BUF_SIZE, fmt, args);
2558 va_end(args);
2559
2560 __this_cpu_or(printk_pending, PRINTK_PENDING_SCHED);
Frederic Weisbecker74876a92012-10-12 18:00:23 +02002561 irq_work_queue(&__get_cpu_var(wake_up_klogd_work));
Peter Zijlstra600e1452012-03-15 12:35:37 +01002562 local_irq_restore(flags);
2563
2564 return r;
2565}
2566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567/*
2568 * printk rate limiting, lifted from the networking subsystem.
2569 *
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07002570 * This enforces a rate limit: not more than 10 kernel messages
2571 * every 5s to make a denial-of-service attack impossible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 */
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07002573DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
2574
Christian Borntraeger5c828712009-10-23 14:58:11 +02002575int __printk_ratelimit(const char *func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576{
Christian Borntraeger5c828712009-10-23 14:58:11 +02002577 return ___ratelimit(&printk_ratelimit_state, func);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578}
Christian Borntraeger5c828712009-10-23 14:58:11 +02002579EXPORT_SYMBOL(__printk_ratelimit);
Andrew Mortonf46c4832006-11-02 22:07:16 -08002580
2581/**
2582 * printk_timed_ratelimit - caller-controlled printk ratelimiting
2583 * @caller_jiffies: pointer to caller's state
2584 * @interval_msecs: minimum interval between prints
2585 *
2586 * printk_timed_ratelimit() returns true if more than @interval_msecs
2587 * milliseconds have elapsed since the last time printk_timed_ratelimit()
2588 * returned true.
2589 */
2590bool printk_timed_ratelimit(unsigned long *caller_jiffies,
2591 unsigned int interval_msecs)
2592{
Guillaume Knispelf2d28a22009-03-17 16:18:42 +01002593 if (*caller_jiffies == 0
2594 || !time_in_range(jiffies, *caller_jiffies,
2595 *caller_jiffies
2596 + msecs_to_jiffies(interval_msecs))) {
2597 *caller_jiffies = jiffies;
Andrew Mortonf46c4832006-11-02 22:07:16 -08002598 return true;
2599 }
2600 return false;
2601}
2602EXPORT_SYMBOL(printk_timed_ratelimit);
Simon Kagstrom456b5652009-10-16 14:09:18 +02002603
2604static DEFINE_SPINLOCK(dump_list_lock);
2605static LIST_HEAD(dump_list);
2606
2607/**
2608 * kmsg_dump_register - register a kernel log dumper.
Randy Dunlap64855362009-12-17 15:27:27 -08002609 * @dumper: pointer to the kmsg_dumper structure
Simon Kagstrom456b5652009-10-16 14:09:18 +02002610 *
2611 * Adds a kernel log dumper to the system. The dump callback in the
2612 * structure will be called when the kernel oopses or panics and must be
2613 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
2614 */
2615int kmsg_dump_register(struct kmsg_dumper *dumper)
2616{
2617 unsigned long flags;
2618 int err = -EBUSY;
2619
2620 /* The dump callback needs to be set */
2621 if (!dumper->dump)
2622 return -EINVAL;
2623
2624 spin_lock_irqsave(&dump_list_lock, flags);
2625 /* Don't allow registering multiple times */
2626 if (!dumper->registered) {
2627 dumper->registered = 1;
Huang Yingfb842b02011-01-12 16:59:43 -08002628 list_add_tail_rcu(&dumper->list, &dump_list);
Simon Kagstrom456b5652009-10-16 14:09:18 +02002629 err = 0;
2630 }
2631 spin_unlock_irqrestore(&dump_list_lock, flags);
2632
2633 return err;
2634}
2635EXPORT_SYMBOL_GPL(kmsg_dump_register);
2636
2637/**
2638 * kmsg_dump_unregister - unregister a kmsg dumper.
Randy Dunlap64855362009-12-17 15:27:27 -08002639 * @dumper: pointer to the kmsg_dumper structure
Simon Kagstrom456b5652009-10-16 14:09:18 +02002640 *
2641 * Removes a dump device from the system. Returns zero on success and
2642 * %-EINVAL otherwise.
2643 */
2644int kmsg_dump_unregister(struct kmsg_dumper *dumper)
2645{
2646 unsigned long flags;
2647 int err = -EINVAL;
2648
2649 spin_lock_irqsave(&dump_list_lock, flags);
2650 if (dumper->registered) {
2651 dumper->registered = 0;
Huang Yingfb842b02011-01-12 16:59:43 -08002652 list_del_rcu(&dumper->list);
Simon Kagstrom456b5652009-10-16 14:09:18 +02002653 err = 0;
2654 }
2655 spin_unlock_irqrestore(&dump_list_lock, flags);
Huang Yingfb842b02011-01-12 16:59:43 -08002656 synchronize_rcu();
Simon Kagstrom456b5652009-10-16 14:09:18 +02002657
2658 return err;
2659}
2660EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
2661
Kay Sievers7ff95542012-05-03 02:29:13 +02002662static bool always_kmsg_dump;
2663module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR);
2664
Simon Kagstrom456b5652009-10-16 14:09:18 +02002665/**
2666 * kmsg_dump - dump kernel log to kernel message dumpers.
2667 * @reason: the reason (oops, panic etc) for dumping
2668 *
Kay Sieverse2ae7152012-06-15 14:07:51 +02002669 * Call each of the registered dumper's dump() callback, which can
2670 * retrieve the kmsg records with kmsg_dump_get_line() or
2671 * kmsg_dump_get_buffer().
Simon Kagstrom456b5652009-10-16 14:09:18 +02002672 */
2673void kmsg_dump(enum kmsg_dump_reason reason)
2674{
Simon Kagstrom456b5652009-10-16 14:09:18 +02002675 struct kmsg_dumper *dumper;
Simon Kagstrom456b5652009-10-16 14:09:18 +02002676 unsigned long flags;
2677
Matthew Garrettc22ab332012-03-05 14:59:10 -08002678 if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
2679 return;
2680
Huang Yingfb842b02011-01-12 16:59:43 -08002681 rcu_read_lock();
Kay Sieverse2ae7152012-06-15 14:07:51 +02002682 list_for_each_entry_rcu(dumper, &dump_list, list) {
2683 if (dumper->max_reason && reason > dumper->max_reason)
2684 continue;
2685
2686 /* initialize iterator with data about the stored records */
2687 dumper->active = true;
2688
2689 raw_spin_lock_irqsave(&logbuf_lock, flags);
2690 dumper->cur_seq = clear_seq;
2691 dumper->cur_idx = clear_idx;
2692 dumper->next_seq = log_next_seq;
2693 dumper->next_idx = log_next_idx;
2694 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2695
2696 /* invoke dumper which will iterate over records */
2697 dumper->dump(dumper, reason);
2698
2699 /* reset iterator */
2700 dumper->active = false;
2701 }
Huang Yingfb842b02011-01-12 16:59:43 -08002702 rcu_read_unlock();
Simon Kagstrom456b5652009-10-16 14:09:18 +02002703}
Kay Sieverse2ae7152012-06-15 14:07:51 +02002704
2705/**
Anton Vorontsov533827c2012-07-20 17:28:07 -07002706 * kmsg_dump_get_line_nolock - retrieve one kmsg log line (unlocked version)
2707 * @dumper: registered kmsg dumper
2708 * @syslog: include the "<4>" prefixes
2709 * @line: buffer to copy the line to
2710 * @size: maximum size of the buffer
2711 * @len: length of line placed into buffer
2712 *
2713 * Start at the beginning of the kmsg buffer, with the oldest kmsg
2714 * record, and copy one record into the provided buffer.
2715 *
2716 * Consecutive calls will return the next available record moving
2717 * towards the end of the buffer with the youngest messages.
2718 *
2719 * A return value of FALSE indicates that there are no more records to
2720 * read.
2721 *
2722 * The function is similar to kmsg_dump_get_line(), but grabs no locks.
2723 */
2724bool kmsg_dump_get_line_nolock(struct kmsg_dumper *dumper, bool syslog,
2725 char *line, size_t size, size_t *len)
2726{
Joe Perches62e32ac2013-07-31 13:53:47 -07002727 struct printk_log *msg;
Anton Vorontsov533827c2012-07-20 17:28:07 -07002728 size_t l = 0;
2729 bool ret = false;
2730
2731 if (!dumper->active)
2732 goto out;
2733
2734 if (dumper->cur_seq < log_first_seq) {
2735 /* messages are gone, move to first available one */
2736 dumper->cur_seq = log_first_seq;
2737 dumper->cur_idx = log_first_idx;
2738 }
2739
2740 /* last entry */
2741 if (dumper->cur_seq >= log_next_seq)
2742 goto out;
2743
2744 msg = log_from_idx(dumper->cur_idx);
2745 l = msg_print_text(msg, 0, syslog, line, size);
2746
2747 dumper->cur_idx = log_next(dumper->cur_idx);
2748 dumper->cur_seq++;
2749 ret = true;
2750out:
2751 if (len)
2752 *len = l;
2753 return ret;
2754}
2755
2756/**
Kay Sieverse2ae7152012-06-15 14:07:51 +02002757 * kmsg_dump_get_line - retrieve one kmsg log line
2758 * @dumper: registered kmsg dumper
2759 * @syslog: include the "<4>" prefixes
2760 * @line: buffer to copy the line to
2761 * @size: maximum size of the buffer
2762 * @len: length of line placed into buffer
2763 *
2764 * Start at the beginning of the kmsg buffer, with the oldest kmsg
2765 * record, and copy one record into the provided buffer.
2766 *
2767 * Consecutive calls will return the next available record moving
2768 * towards the end of the buffer with the youngest messages.
2769 *
2770 * A return value of FALSE indicates that there are no more records to
2771 * read.
2772 */
2773bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog,
2774 char *line, size_t size, size_t *len)
2775{
2776 unsigned long flags;
Anton Vorontsov533827c2012-07-20 17:28:07 -07002777 bool ret;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002778
2779 raw_spin_lock_irqsave(&logbuf_lock, flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07002780 ret = kmsg_dump_get_line_nolock(dumper, syslog, line, size, len);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002781 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07002782
Kay Sieverse2ae7152012-06-15 14:07:51 +02002783 return ret;
2784}
2785EXPORT_SYMBOL_GPL(kmsg_dump_get_line);
2786
2787/**
2788 * kmsg_dump_get_buffer - copy kmsg log lines
2789 * @dumper: registered kmsg dumper
2790 * @syslog: include the "<4>" prefixes
Randy Dunlap4f0f4af2012-06-30 15:37:24 -07002791 * @buf: buffer to copy the line to
Kay Sieverse2ae7152012-06-15 14:07:51 +02002792 * @size: maximum size of the buffer
2793 * @len: length of line placed into buffer
2794 *
2795 * Start at the end of the kmsg buffer and fill the provided buffer
2796 * with as many of the the *youngest* kmsg records that fit into it.
2797 * If the buffer is large enough, all available kmsg records will be
2798 * copied with a single call.
2799 *
2800 * Consecutive calls will fill the buffer with the next block of
2801 * available older records, not including the earlier retrieved ones.
2802 *
2803 * A return value of FALSE indicates that there are no more records to
2804 * read.
2805 */
2806bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
2807 char *buf, size_t size, size_t *len)
2808{
2809 unsigned long flags;
2810 u64 seq;
2811 u32 idx;
2812 u64 next_seq;
2813 u32 next_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002814 enum log_flags prev;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002815 size_t l = 0;
2816 bool ret = false;
2817
2818 if (!dumper->active)
2819 goto out;
2820
2821 raw_spin_lock_irqsave(&logbuf_lock, flags);
2822 if (dumper->cur_seq < log_first_seq) {
2823 /* messages are gone, move to first available one */
2824 dumper->cur_seq = log_first_seq;
2825 dumper->cur_idx = log_first_idx;
2826 }
2827
2828 /* last entry */
2829 if (dumper->cur_seq >= dumper->next_seq) {
2830 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2831 goto out;
2832 }
2833
2834 /* calculate length of entire buffer */
2835 seq = dumper->cur_seq;
2836 idx = dumper->cur_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002837 prev = 0;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002838 while (seq < dumper->next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07002839 struct printk_log *msg = log_from_idx(idx);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002840
Kay Sievers5becfb12012-07-09 12:15:42 -07002841 l += msg_print_text(msg, prev, true, NULL, 0);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002842 idx = log_next(idx);
2843 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07002844 prev = msg->flags;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002845 }
2846
2847 /* move first record forward until length fits into the buffer */
2848 seq = dumper->cur_seq;
2849 idx = dumper->cur_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002850 prev = 0;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002851 while (l > size && seq < dumper->next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07002852 struct printk_log *msg = log_from_idx(idx);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002853
Kay Sievers5becfb12012-07-09 12:15:42 -07002854 l -= msg_print_text(msg, prev, true, NULL, 0);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002855 idx = log_next(idx);
2856 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07002857 prev = msg->flags;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002858 }
2859
2860 /* last message in next interation */
2861 next_seq = seq;
2862 next_idx = idx;
2863
2864 l = 0;
2865 while (seq < dumper->next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07002866 struct printk_log *msg = log_from_idx(idx);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002867
Kay Sievers5becfb12012-07-09 12:15:42 -07002868 l += msg_print_text(msg, prev, syslog, buf + l, size - l);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002869 idx = log_next(idx);
2870 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07002871 prev = msg->flags;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002872 }
2873
2874 dumper->next_seq = next_seq;
2875 dumper->next_idx = next_idx;
2876 ret = true;
2877 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2878out:
2879 if (len)
2880 *len = l;
2881 return ret;
2882}
2883EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer);
2884
2885/**
Anton Vorontsov533827c2012-07-20 17:28:07 -07002886 * kmsg_dump_rewind_nolock - reset the interator (unlocked version)
2887 * @dumper: registered kmsg dumper
2888 *
2889 * Reset the dumper's iterator so that kmsg_dump_get_line() and
2890 * kmsg_dump_get_buffer() can be called again and used multiple
2891 * times within the same dumper.dump() callback.
2892 *
2893 * The function is similar to kmsg_dump_rewind(), but grabs no locks.
2894 */
2895void kmsg_dump_rewind_nolock(struct kmsg_dumper *dumper)
2896{
2897 dumper->cur_seq = clear_seq;
2898 dumper->cur_idx = clear_idx;
2899 dumper->next_seq = log_next_seq;
2900 dumper->next_idx = log_next_idx;
2901}
2902
2903/**
Kay Sieverse2ae7152012-06-15 14:07:51 +02002904 * kmsg_dump_rewind - reset the interator
2905 * @dumper: registered kmsg dumper
2906 *
2907 * Reset the dumper's iterator so that kmsg_dump_get_line() and
2908 * kmsg_dump_get_buffer() can be called again and used multiple
2909 * times within the same dumper.dump() callback.
2910 */
2911void kmsg_dump_rewind(struct kmsg_dumper *dumper)
2912{
2913 unsigned long flags;
2914
2915 raw_spin_lock_irqsave(&logbuf_lock, flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07002916 kmsg_dump_rewind_nolock(dumper);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002917 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2918}
2919EXPORT_SYMBOL_GPL(kmsg_dump_rewind);
Tejun Heo196779b2013-04-30 15:27:12 -07002920
Tejun Heo98e5e1b2013-04-30 15:27:15 -07002921static char dump_stack_arch_desc_str[128];
2922
2923/**
2924 * dump_stack_set_arch_desc - set arch-specific str to show with task dumps
2925 * @fmt: printf-style format string
2926 * @...: arguments for the format string
2927 *
2928 * The configured string will be printed right after utsname during task
2929 * dumps. Usually used to add arch-specific system identifiers. If an
2930 * arch wants to make use of such an ID string, it should initialize this
2931 * as soon as possible during boot.
2932 */
2933void __init dump_stack_set_arch_desc(const char *fmt, ...)
2934{
2935 va_list args;
2936
2937 va_start(args, fmt);
2938 vsnprintf(dump_stack_arch_desc_str, sizeof(dump_stack_arch_desc_str),
2939 fmt, args);
2940 va_end(args);
2941}
2942
Tejun Heo196779b2013-04-30 15:27:12 -07002943/**
2944 * dump_stack_print_info - print generic debug info for dump_stack()
2945 * @log_lvl: log level
2946 *
2947 * Arch-specific dump_stack() implementations can use this function to
2948 * print out the same debug information as the generic dump_stack().
2949 */
2950void dump_stack_print_info(const char *log_lvl)
2951{
2952 printk("%sCPU: %d PID: %d Comm: %.20s %s %s %.*s\n",
2953 log_lvl, raw_smp_processor_id(), current->pid, current->comm,
2954 print_tainted(), init_utsname()->release,
2955 (int)strcspn(init_utsname()->version, " "),
2956 init_utsname()->version);
Tejun Heo98e5e1b2013-04-30 15:27:15 -07002957
2958 if (dump_stack_arch_desc_str[0] != '\0')
2959 printk("%sHardware name: %s\n",
2960 log_lvl, dump_stack_arch_desc_str);
Tejun Heo3d1cb202013-04-30 15:27:22 -07002961
2962 print_worker_info(log_lvl, current);
Tejun Heo196779b2013-04-30 15:27:12 -07002963}
2964
Tejun Heoa43cb952013-04-30 15:27:17 -07002965/**
2966 * show_regs_print_info - print generic debug info for show_regs()
2967 * @log_lvl: log level
2968 *
2969 * show_regs() implementations can use this function to print out generic
2970 * debug information.
2971 */
2972void show_regs_print_info(const char *log_lvl)
2973{
2974 dump_stack_print_info(log_lvl);
2975
2976 printk("%stask: %p ti: %p task.ti: %p\n",
2977 log_lvl, current, current_thread_info(),
2978 task_thread_info(current));
2979}
2980
Joe Perches7ef3d2f2008-02-08 04:21:25 -08002981#endif