blob: 7a6e69441f75504c5e6dcf19d5accd14aa03feb9 [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 Yingfb842b002011-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>
Alex Elder249771b2014-08-06 16:09:08 -070048#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <asm/uaccess.h>
51
Johannes Berg95100352011-11-24 20:03:08 +010052#define CREATE_TRACE_POINTS
53#include <trace/events/printk.h>
54
Joe Perchesd197c432013-07-31 13:53:44 -070055#include "console_cmdline.h"
Joe Perchesbbeddf52013-07-31 13:53:45 -070056#include "braille.h"
Joe Perchesd197c432013-07-31 13:53:44 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058int console_printk[4] = {
Borislav Petkova8fe19e2014-06-04 16:11:46 -070059 CONSOLE_LOGLEVEL_DEFAULT, /* console_loglevel */
Alex Elder42a9dc02014-08-06 16:09:01 -070060 MESSAGE_LOGLEVEL_DEFAULT, /* default_message_loglevel */
Borislav Petkova8fe19e2014-06-04 16:11:46 -070061 CONSOLE_LOGLEVEL_MIN, /* minimum_console_loglevel */
62 CONSOLE_LOGLEVEL_DEFAULT, /* default_console_loglevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
Steven Rostedt458df9f2014-06-04 16:11:38 -070065/* Deferred messaged from sched code are marked by this special level */
66#define SCHED_MESSAGE_LOGLEVEL -2
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/*
Patrick Pletscher0bbfb7c2007-02-17 20:10:16 +010069 * Low level drivers may need that to know if they can schedule in
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 * their unblank() callback or not. So let's export it.
71 */
72int oops_in_progress;
73EXPORT_SYMBOL(oops_in_progress);
74
75/*
76 * console_sem protects the console_drivers list, and also
77 * provides serialisation for access to the entire console
78 * driver system.
79 */
Thomas Gleixner5b8c4f22010-09-07 14:33:43 +000080static DEFINE_SEMAPHORE(console_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081struct console *console_drivers;
Ingo Molnara29d1cf2008-06-02 13:19:08 +020082EXPORT_SYMBOL_GPL(console_drivers);
83
Daniel Vetterdaee7792012-09-22 19:52:11 +020084#ifdef CONFIG_LOCKDEP
85static struct lockdep_map console_lock_dep_map = {
86 .name = "console_lock"
87};
88#endif
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/*
Jan Karabd8d7cf2014-06-04 16:11:36 -070091 * Helper macros to handle lockdep when locking/unlocking console_sem. We use
92 * macros instead of functions so that _RET_IP_ contains useful information.
93 */
94#define down_console_sem() do { \
95 down(&console_sem);\
96 mutex_acquire(&console_lock_dep_map, 0, 0, _RET_IP_);\
97} while (0)
98
99static int __down_trylock_console_sem(unsigned long ip)
100{
101 if (down_trylock(&console_sem))
102 return 1;
103 mutex_acquire(&console_lock_dep_map, 0, 1, ip);
104 return 0;
105}
106#define down_trylock_console_sem() __down_trylock_console_sem(_RET_IP_)
107
108#define up_console_sem() do { \
109 mutex_release(&console_lock_dep_map, 1, _RET_IP_);\
110 up(&console_sem);\
111} while (0)
112
113/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * This is used for debugging the mess that is the VT code by
115 * keeping track if we have the console semaphore held. It's
116 * definitely not the perfect debug tool (we don't know if _WE_
Alex Elder0b90fec2014-08-06 16:09:03 -0700117 * hold it and are racing, but it helps tracking those weird code
118 * paths in the console code where we end up in places I want
119 * locked without the console sempahore held).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 */
Linus Torvalds557240b2006-06-19 18:16:01 -0700121static int console_locked, console_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123/*
Feng Tangfe3d8ad2011-03-22 16:34:21 -0700124 * If exclusive_console is non-NULL then only this console is to be printed to.
125 */
126static struct console *exclusive_console;
127
128/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 * Array of consoles built from command line options (console=)
130 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132#define MAX_CMDLINECONSOLES 8
133
134static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
Joe Perchesd197c432013-07-31 13:53:44 -0700135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static int selected_console = -1;
137static int preferred_console = -1;
Markus Armbruster9e124fe2008-05-26 23:31:07 +0100138int console_set_on_cmdline;
139EXPORT_SYMBOL(console_set_on_cmdline);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141/* Flag: console code may call schedule() */
142static int console_may_schedule;
143
Kay Sievers7ff95542012-05-03 02:29:13 +0200144/*
145 * The printk log buffer consists of a chain of concatenated variable
146 * length records. Every record starts with a record header, containing
147 * the overall length of the record.
148 *
149 * The heads to the first and last entry in the buffer, as well as the
Alex Elder0b90fec2014-08-06 16:09:03 -0700150 * sequence numbers of these entries are maintained when messages are
151 * stored.
Kay Sievers7ff95542012-05-03 02:29:13 +0200152 *
153 * If the heads indicate available messages, the length in the header
154 * tells the start next message. A length == 0 for the next message
155 * indicates a wrap-around to the beginning of the buffer.
156 *
157 * Every record carries the monotonic timestamp in microseconds, as well as
158 * the standard userspace syslog level and syslog facility. The usual
159 * kernel messages use LOG_KERN; userspace-injected messages always carry
160 * a matching syslog facility, by default LOG_USER. The origin of every
161 * message can be reliably determined that way.
162 *
163 * The human readable log message directly follows the message header. The
164 * length of the message text is stored in the header, the stored message
165 * is not terminated.
166 *
Kay Sieverse11fea92012-05-03 02:29:41 +0200167 * Optionally, a message can carry a dictionary of properties (key/value pairs),
168 * to provide userspace with a machine-readable message context.
169 *
170 * Examples for well-defined, commonly used property names are:
171 * DEVICE=b12:8 device identifier
172 * b12:8 block dev_t
173 * c127:3 char dev_t
174 * n8 netdev ifindex
175 * +sound:card0 subsystem:devname
176 * SUBSYSTEM=pci driver-core subsystem name
177 *
178 * Valid characters in property names are [a-zA-Z0-9.-_]. The plain text value
179 * follows directly after a '=' character. Every property is terminated by
180 * a '\0' character. The last property is not terminated.
181 *
182 * Example of a message structure:
183 * 0000 ff 8f 00 00 00 00 00 00 monotonic time in nsec
184 * 0008 34 00 record is 52 bytes long
185 * 000a 0b 00 text is 11 bytes long
186 * 000c 1f 00 dictionary is 23 bytes long
187 * 000e 03 00 LOG_KERN (facility) LOG_ERR (level)
188 * 0010 69 74 27 73 20 61 20 6c "it's a l"
189 * 69 6e 65 "ine"
190 * 001b 44 45 56 49 43 "DEVIC"
191 * 45 3d 62 38 3a 32 00 44 "E=b8:2\0D"
192 * 52 49 56 45 52 3d 62 75 "RIVER=bu"
193 * 67 "g"
194 * 0032 00 00 00 padding to next message header
195 *
Joe Perches62e32ac2013-07-31 13:53:47 -0700196 * The 'struct printk_log' buffer header must never be directly exported to
Kay Sieverse11fea92012-05-03 02:29:41 +0200197 * userspace, it is a kernel-private implementation detail that might
198 * need to be changed in the future, when the requirements change.
199 *
200 * /dev/kmsg exports the structured data in the following line format:
201 * "level,sequnum,timestamp;<message text>\n"
202 *
203 * The optional key/value pairs are attached as continuation lines starting
204 * with a space character and terminated by a newline. All possible
205 * non-prinatable characters are escaped in the "\xff" notation.
206 *
207 * Users of the export format should ignore possible additional values
208 * separated by ',', and find the message after the ';' character.
Kay Sievers7ff95542012-05-03 02:29:13 +0200209 */
Matt Mackalld59745c2005-05-01 08:59:02 -0700210
Kay Sievers084681d2012-06-28 09:38:53 +0200211enum log_flags {
Kay Sievers5becfb12012-07-09 12:15:42 -0700212 LOG_NOCONS = 1, /* already flushed, do not print to console */
213 LOG_NEWLINE = 2, /* text ended with a newline */
214 LOG_PREFIX = 4, /* text started with a prefix */
215 LOG_CONT = 8, /* text is a fragment of a continuation line */
Kay Sievers084681d2012-06-28 09:38:53 +0200216};
217
Joe Perches62e32ac2013-07-31 13:53:47 -0700218struct printk_log {
Kay Sievers7ff95542012-05-03 02:29:13 +0200219 u64 ts_nsec; /* timestamp in nanoseconds */
220 u16 len; /* length of entire record */
221 u16 text_len; /* length of text buffer */
222 u16 dict_len; /* length of dictionary buffer */
Kay Sievers084681d2012-06-28 09:38:53 +0200223 u8 facility; /* syslog facility */
224 u8 flags:5; /* internal record flags */
225 u8 level:3; /* syslog level */
Kay Sievers7ff95542012-05-03 02:29:13 +0200226};
227
228/*
Steven Rostedt458df9f2014-06-04 16:11:38 -0700229 * The logbuf_lock protects kmsg buffer, indices, counters. This can be taken
230 * within the scheduler's rq lock. It must be released before calling
231 * console_unlock() or anything else that might wake up a process.
Kay Sievers7ff95542012-05-03 02:29:13 +0200232 */
233static DEFINE_RAW_SPINLOCK(logbuf_lock);
234
Kay Sievers96efedf2012-07-16 18:35:29 -0700235#ifdef CONFIG_PRINTK
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -0700236DECLARE_WAIT_QUEUE_HEAD(log_wait);
Kay Sievers7f3a7812012-05-09 01:37:51 +0200237/* the next printk record to read by syslog(READ) or /proc/kmsg */
238static u64 syslog_seq;
239static u32 syslog_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -0700240static enum log_flags syslog_prev;
Kay Sieverseb02dac2012-07-09 10:05:10 -0700241static size_t syslog_partial;
Kay Sievers7f3a7812012-05-09 01:37:51 +0200242
243/* index and sequence number of the first record stored in the buffer */
244static u64 log_first_seq;
245static u32 log_first_idx;
246
247/* index and sequence number of the next record to store in the buffer */
248static u64 log_next_seq;
Kay Sievers7f3a7812012-05-09 01:37:51 +0200249static u32 log_next_idx;
250
Kay Sieverseab07262012-07-16 18:35:30 -0700251/* the next printk record to write to the console */
252static u64 console_seq;
253static u32 console_idx;
254static enum log_flags console_prev;
255
Kay Sievers7f3a7812012-05-09 01:37:51 +0200256/* the next printk record to read after the last 'clear' command */
257static u64 clear_seq;
258static u32 clear_idx;
Kay Sievers7ff95542012-05-03 02:29:13 +0200259
Kay Sievers70498252012-07-16 18:35:29 -0700260#define PREFIX_MAX 32
Alex Elder249771b2014-08-06 16:09:08 -0700261#define LOG_LINE_MAX (1024 - PREFIX_MAX)
Kay Sievers7ff95542012-05-03 02:29:13 +0200262
263/* record buffer */
Andrew Lunn6ebb0172012-06-05 08:52:34 +0200264#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
Stephen Warrenf8450fc2012-05-10 16:14:33 -0600265#define LOG_ALIGN 4
266#else
Joe Perches62e32ac2013-07-31 13:53:47 -0700267#define LOG_ALIGN __alignof__(struct printk_log)
Stephen Warrenf8450fc2012-05-10 16:14:33 -0600268#endif
Kay Sievers7ff95542012-05-03 02:29:13 +0200269#define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
Luis R. Rodriguez23b28992014-08-06 16:08:56 -0700270#define __LOG_CPU_MAX_BUF_LEN (1 << CONFIG_LOG_CPU_MAX_BUF_SHIFT)
Stephen Warrenf8450fc2012-05-10 16:14:33 -0600271static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN);
Matt Mackalld59745c2005-05-01 08:59:02 -0700272static char *log_buf = __log_buf;
Kay Sievers7ff95542012-05-03 02:29:13 +0200273static u32 log_buf_len = __LOG_BUF_LEN;
274
Vasant Hegde14c40002014-08-09 11:15:30 +0530275/* Return log buffer address */
276char *log_buf_addr_get(void)
277{
278 return log_buf;
279}
280
281/* Return log buffer size */
282u32 log_buf_len_get(void)
283{
284 return log_buf_len;
285}
286
Kay Sievers7ff95542012-05-03 02:29:13 +0200287/* human readable text of the record */
Joe Perches62e32ac2013-07-31 13:53:47 -0700288static char *log_text(const struct printk_log *msg)
Kay Sievers7ff95542012-05-03 02:29:13 +0200289{
Joe Perches62e32ac2013-07-31 13:53:47 -0700290 return (char *)msg + sizeof(struct printk_log);
Kay Sievers7ff95542012-05-03 02:29:13 +0200291}
292
293/* optional key/value pair dictionary attached to the record */
Joe Perches62e32ac2013-07-31 13:53:47 -0700294static char *log_dict(const struct printk_log *msg)
Kay Sievers7ff95542012-05-03 02:29:13 +0200295{
Joe Perches62e32ac2013-07-31 13:53:47 -0700296 return (char *)msg + sizeof(struct printk_log) + msg->text_len;
Kay Sievers7ff95542012-05-03 02:29:13 +0200297}
298
299/* get record by index; idx must point to valid msg */
Joe Perches62e32ac2013-07-31 13:53:47 -0700300static struct printk_log *log_from_idx(u32 idx)
Kay Sievers7ff95542012-05-03 02:29:13 +0200301{
Joe Perches62e32ac2013-07-31 13:53:47 -0700302 struct printk_log *msg = (struct printk_log *)(log_buf + idx);
Kay Sievers7ff95542012-05-03 02:29:13 +0200303
304 /*
305 * A length == 0 record is the end of buffer marker. Wrap around and
306 * read the message at the start of the buffer.
307 */
308 if (!msg->len)
Joe Perches62e32ac2013-07-31 13:53:47 -0700309 return (struct printk_log *)log_buf;
Kay Sievers7ff95542012-05-03 02:29:13 +0200310 return msg;
311}
312
313/* get next record; idx must point to valid msg */
314static u32 log_next(u32 idx)
315{
Joe Perches62e32ac2013-07-31 13:53:47 -0700316 struct printk_log *msg = (struct printk_log *)(log_buf + idx);
Kay Sievers7ff95542012-05-03 02:29:13 +0200317
318 /* length == 0 indicates the end of the buffer; wrap */
319 /*
320 * A length == 0 record is the end of buffer marker. Wrap around and
321 * read the message at the start of the buffer as *this* one, and
322 * return the one after that.
323 */
324 if (!msg->len) {
Joe Perches62e32ac2013-07-31 13:53:47 -0700325 msg = (struct printk_log *)log_buf;
Kay Sievers7ff95542012-05-03 02:29:13 +0200326 return msg->len;
327 }
328 return idx + msg->len;
329}
330
Petr Mladekf40e4b92014-06-04 16:11:30 -0700331/*
332 * Check whether there is enough free space for the given message.
333 *
334 * The same values of first_idx and next_idx mean that the buffer
335 * is either empty or full.
336 *
337 * If the buffer is empty, we must respect the position of the indexes.
338 * They cannot be reset to the beginning of the buffer.
339 */
340static int logbuf_has_space(u32 msg_size, bool empty)
Petr Mladek0a581692014-06-04 16:11:28 -0700341{
342 u32 free;
343
Petr Mladekf40e4b92014-06-04 16:11:30 -0700344 if (log_next_idx > log_first_idx || empty)
Petr Mladek0a581692014-06-04 16:11:28 -0700345 free = max(log_buf_len - log_next_idx, log_first_idx);
346 else
347 free = log_first_idx - log_next_idx;
348
349 /*
350 * We need space also for an empty header that signalizes wrapping
351 * of the buffer.
352 */
353 return free >= msg_size + sizeof(struct printk_log);
354}
355
Petr Mladekf40e4b92014-06-04 16:11:30 -0700356static int log_make_free_space(u32 msg_size)
Petr Mladek0a581692014-06-04 16:11:28 -0700357{
358 while (log_first_seq < log_next_seq) {
Petr Mladekf40e4b92014-06-04 16:11:30 -0700359 if (logbuf_has_space(msg_size, false))
360 return 0;
Alex Elder0b90fec2014-08-06 16:09:03 -0700361 /* drop old messages until we have enough contiguous space */
Petr Mladek0a581692014-06-04 16:11:28 -0700362 log_first_idx = log_next(log_first_idx);
363 log_first_seq++;
364 }
Petr Mladekf40e4b92014-06-04 16:11:30 -0700365
366 /* sequence numbers are equal, so the log buffer is empty */
367 if (logbuf_has_space(msg_size, true))
368 return 0;
369
370 return -ENOMEM;
Petr Mladek0a581692014-06-04 16:11:28 -0700371}
372
Petr Mladek85c870432014-06-04 16:11:31 -0700373/* compute the message size including the padding bytes */
374static u32 msg_used_size(u16 text_len, u16 dict_len, u32 *pad_len)
375{
376 u32 size;
377
378 size = sizeof(struct printk_log) + text_len + dict_len;
379 *pad_len = (-size) & (LOG_ALIGN - 1);
380 size += *pad_len;
381
382 return size;
383}
384
Petr Mladek55bd53a2014-06-04 16:11:32 -0700385/*
386 * Define how much of the log buffer we could take at maximum. The value
387 * must be greater than two. Note that only half of the buffer is available
388 * when the index points to the middle.
389 */
390#define MAX_LOG_TAKE_PART 4
391static const char trunc_msg[] = "<truncated>";
392
393static u32 truncate_msg(u16 *text_len, u16 *trunc_msg_len,
394 u16 *dict_len, u32 *pad_len)
395{
396 /*
397 * The message should not take the whole buffer. Otherwise, it might
398 * get removed too soon.
399 */
400 u32 max_text_len = log_buf_len / MAX_LOG_TAKE_PART;
401 if (*text_len > max_text_len)
402 *text_len = max_text_len;
403 /* enable the warning message */
404 *trunc_msg_len = strlen(trunc_msg);
405 /* disable the "dict" completely */
406 *dict_len = 0;
407 /* compute the size again, count also the warning message */
408 return msg_used_size(*text_len + *trunc_msg_len, 0, pad_len);
409}
410
Kay Sievers7ff95542012-05-03 02:29:13 +0200411/* insert record into the buffer, discard old ones, update heads */
Petr Mladek034633c2014-06-04 16:11:33 -0700412static int log_store(int facility, int level,
413 enum log_flags flags, u64 ts_nsec,
414 const char *dict, u16 dict_len,
415 const char *text, u16 text_len)
Kay Sievers7ff95542012-05-03 02:29:13 +0200416{
Joe Perches62e32ac2013-07-31 13:53:47 -0700417 struct printk_log *msg;
Kay Sievers7ff95542012-05-03 02:29:13 +0200418 u32 size, pad_len;
Petr Mladek55bd53a2014-06-04 16:11:32 -0700419 u16 trunc_msg_len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +0200420
421 /* number of '\0' padding bytes to next message */
Petr Mladek85c870432014-06-04 16:11:31 -0700422 size = msg_used_size(text_len, dict_len, &pad_len);
Kay Sievers7ff95542012-05-03 02:29:13 +0200423
Petr Mladek55bd53a2014-06-04 16:11:32 -0700424 if (log_make_free_space(size)) {
425 /* truncate the message if it is too long for empty buffer */
426 size = truncate_msg(&text_len, &trunc_msg_len,
427 &dict_len, &pad_len);
428 /* survive when the log buffer is too small for trunc_msg */
429 if (log_make_free_space(size))
Petr Mladek034633c2014-06-04 16:11:33 -0700430 return 0;
Petr Mladek55bd53a2014-06-04 16:11:32 -0700431 }
Kay Sievers7ff95542012-05-03 02:29:13 +0200432
Petr Mladek39b25102014-04-03 14:48:42 -0700433 if (log_next_idx + size + sizeof(struct printk_log) > log_buf_len) {
Kay Sievers7ff95542012-05-03 02:29:13 +0200434 /*
435 * This message + an additional empty header does not fit
436 * at the end of the buffer. Add an empty header with len == 0
437 * to signify a wrap around.
438 */
Joe Perches62e32ac2013-07-31 13:53:47 -0700439 memset(log_buf + log_next_idx, 0, sizeof(struct printk_log));
Kay Sievers7ff95542012-05-03 02:29:13 +0200440 log_next_idx = 0;
441 }
442
443 /* fill message */
Joe Perches62e32ac2013-07-31 13:53:47 -0700444 msg = (struct printk_log *)(log_buf + log_next_idx);
Kay Sievers7ff95542012-05-03 02:29:13 +0200445 memcpy(log_text(msg), text, text_len);
446 msg->text_len = text_len;
Petr Mladek55bd53a2014-06-04 16:11:32 -0700447 if (trunc_msg_len) {
448 memcpy(log_text(msg) + text_len, trunc_msg, trunc_msg_len);
449 msg->text_len += trunc_msg_len;
450 }
Kay Sievers7ff95542012-05-03 02:29:13 +0200451 memcpy(log_dict(msg), dict, dict_len);
452 msg->dict_len = dict_len;
Kay Sievers084681d2012-06-28 09:38:53 +0200453 msg->facility = facility;
454 msg->level = level & 7;
455 msg->flags = flags & 0x1f;
456 if (ts_nsec > 0)
457 msg->ts_nsec = ts_nsec;
458 else
459 msg->ts_nsec = local_clock();
Kay Sievers7ff95542012-05-03 02:29:13 +0200460 memset(log_dict(msg) + dict_len, 0, pad_len);
Petr Mladekfce6e032014-04-03 14:48:43 -0700461 msg->len = size;
Kay Sievers7ff95542012-05-03 02:29:13 +0200462
463 /* insert message */
464 log_next_idx += msg->len;
465 log_next_seq++;
Petr Mladek034633c2014-06-04 16:11:33 -0700466
467 return msg->text_len;
Kay Sievers7ff95542012-05-03 02:29:13 +0200468}
Matt Mackalld59745c2005-05-01 08:59:02 -0700469
Alex Eldere99aa462014-08-06 16:09:05 -0700470int dmesg_restrict = IS_ENABLED(CONFIG_SECURITY_DMESG_RESTRICT);
Kees Cook637241a2013-06-12 14:04:39 -0700471
472static int syslog_action_restricted(int type)
473{
474 if (dmesg_restrict)
475 return 1;
476 /*
477 * Unless restricted, we allow "read all" and "get buffer size"
478 * for everybody.
479 */
480 return type != SYSLOG_ACTION_READ_ALL &&
481 type != SYSLOG_ACTION_SIZE_BUFFER;
482}
483
484static int check_syslog_permissions(int type, bool from_file)
485{
486 /*
487 * If this is from /proc/kmsg and we've already opened it, then we've
488 * already done the capabilities checks at open time.
489 */
490 if (from_file && type != SYSLOG_ACTION_OPEN)
491 return 0;
492
493 if (syslog_action_restricted(type)) {
494 if (capable(CAP_SYSLOG))
495 return 0;
496 /*
497 * For historical reasons, accept CAP_SYS_ADMIN too, with
498 * a warning.
499 */
500 if (capable(CAP_SYS_ADMIN)) {
501 pr_warn_once("%s (%d): Attempt to access syslog with "
502 "CAP_SYS_ADMIN but no CAP_SYSLOG "
503 "(deprecated).\n",
504 current->comm, task_pid_nr(current));
505 return 0;
506 }
507 return -EPERM;
508 }
509 return security_syslog(type);
510}
511
512
Kay Sieverse11fea92012-05-03 02:29:41 +0200513/* /dev/kmsg - userspace message inject/listen interface */
514struct devkmsg_user {
515 u64 seq;
516 u32 idx;
Kay Sieversd39f3d72012-07-16 18:35:30 -0700517 enum log_flags prev;
Kay Sieverse11fea92012-05-03 02:29:41 +0200518 struct mutex lock;
519 char buf[8192];
520};
521
Al Viro849f3122014-08-23 12:23:53 -0400522static ssize_t devkmsg_write(struct kiocb *iocb, struct iov_iter *from)
Kay Sieverse11fea92012-05-03 02:29:41 +0200523{
524 char *buf, *line;
525 int i;
526 int level = default_message_loglevel;
527 int facility = 1; /* LOG_USER */
Al Viro849f3122014-08-23 12:23:53 -0400528 size_t len = iocb->ki_nbytes;
Kay Sieverse11fea92012-05-03 02:29:41 +0200529 ssize_t ret = len;
530
531 if (len > LOG_LINE_MAX)
532 return -EINVAL;
533 buf = kmalloc(len+1, GFP_KERNEL);
534 if (buf == NULL)
535 return -ENOMEM;
536
Al Viro849f3122014-08-23 12:23:53 -0400537 buf[len] = '\0';
538 if (copy_from_iter(buf, len, from) != len) {
539 kfree(buf);
540 return -EFAULT;
Kay Sieverse11fea92012-05-03 02:29:41 +0200541 }
542
543 /*
544 * Extract and skip the syslog prefix <[0-9]*>. Coming from userspace
545 * the decimal value represents 32bit, the lower 3 bit are the log
546 * level, the rest are the log facility.
547 *
548 * If no prefix or no userspace facility is specified, we
549 * enforce LOG_USER, to be able to reliably distinguish
550 * kernel-generated messages from userspace-injected ones.
551 */
552 line = buf;
553 if (line[0] == '<') {
554 char *endp = NULL;
555
556 i = simple_strtoul(line+1, &endp, 10);
557 if (endp && endp[0] == '>') {
558 level = i & 7;
559 if (i >> 3)
560 facility = i >> 3;
561 endp++;
562 len -= endp - line;
563 line = endp;
564 }
565 }
Kay Sieverse11fea92012-05-03 02:29:41 +0200566
567 printk_emit(facility, level, NULL, 0, "%s", line);
Kay Sieverse11fea92012-05-03 02:29:41 +0200568 kfree(buf);
569 return ret;
570}
571
572static ssize_t devkmsg_read(struct file *file, char __user *buf,
573 size_t count, loff_t *ppos)
574{
575 struct devkmsg_user *user = file->private_data;
Joe Perches62e32ac2013-07-31 13:53:47 -0700576 struct printk_log *msg;
Kay Sievers5fc324902012-05-08 13:04:17 +0200577 u64 ts_usec;
Kay Sieverse11fea92012-05-03 02:29:41 +0200578 size_t i;
Kay Sieversd39f3d72012-07-16 18:35:30 -0700579 char cont = '-';
Kay Sieverse11fea92012-05-03 02:29:41 +0200580 size_t len;
581 ssize_t ret;
582
583 if (!user)
584 return -EBADF;
585
Yuanhan Liu4a77a5a2012-06-16 21:21:51 +0800586 ret = mutex_lock_interruptible(&user->lock);
587 if (ret)
588 return ret;
liu chuansheng5c53d812012-07-06 09:50:08 -0700589 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200590 while (user->seq == log_next_seq) {
591 if (file->f_flags & O_NONBLOCK) {
592 ret = -EAGAIN;
liu chuansheng5c53d812012-07-06 09:50:08 -0700593 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200594 goto out;
595 }
596
liu chuansheng5c53d812012-07-06 09:50:08 -0700597 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200598 ret = wait_event_interruptible(log_wait,
599 user->seq != log_next_seq);
600 if (ret)
601 goto out;
liu chuansheng5c53d812012-07-06 09:50:08 -0700602 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200603 }
604
605 if (user->seq < log_first_seq) {
606 /* our last seen message is gone, return error and reset */
607 user->idx = log_first_idx;
608 user->seq = log_first_seq;
609 ret = -EPIPE;
liu chuansheng5c53d812012-07-06 09:50:08 -0700610 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200611 goto out;
612 }
613
614 msg = log_from_idx(user->idx);
Kay Sievers5fc324902012-05-08 13:04:17 +0200615 ts_usec = msg->ts_nsec;
616 do_div(ts_usec, 1000);
Kay Sieversd39f3d72012-07-16 18:35:30 -0700617
618 /*
619 * If we couldn't merge continuation line fragments during the print,
620 * export the stored flags to allow an optional external merge of the
621 * records. Merging the records isn't always neccessarily correct, like
622 * when we hit a race during printing. In most cases though, it produces
623 * better readable output. 'c' in the record flags mark the first
624 * fragment of a line, '+' the following.
625 */
626 if (msg->flags & LOG_CONT && !(user->prev & LOG_CONT))
627 cont = 'c';
628 else if ((msg->flags & LOG_CONT) ||
629 ((user->prev & LOG_CONT) && !(msg->flags & LOG_PREFIX)))
630 cont = '+';
631
632 len = sprintf(user->buf, "%u,%llu,%llu,%c;",
633 (msg->facility << 3) | msg->level,
634 user->seq, ts_usec, cont);
635 user->prev = msg->flags;
Kay Sieverse11fea92012-05-03 02:29:41 +0200636
637 /* escape non-printable characters */
638 for (i = 0; i < msg->text_len; i++) {
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200639 unsigned char c = log_text(msg)[i];
Kay Sieverse11fea92012-05-03 02:29:41 +0200640
Kay Sieverse3f5a5f2012-07-06 09:50:09 -0700641 if (c < ' ' || c >= 127 || c == '\\')
Kay Sieverse11fea92012-05-03 02:29:41 +0200642 len += sprintf(user->buf + len, "\\x%02x", c);
643 else
644 user->buf[len++] = c;
645 }
646 user->buf[len++] = '\n';
647
648 if (msg->dict_len) {
649 bool line = true;
650
651 for (i = 0; i < msg->dict_len; i++) {
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200652 unsigned char c = log_dict(msg)[i];
Kay Sieverse11fea92012-05-03 02:29:41 +0200653
654 if (line) {
655 user->buf[len++] = ' ';
656 line = false;
657 }
658
659 if (c == '\0') {
660 user->buf[len++] = '\n';
661 line = true;
662 continue;
663 }
664
Kay Sieverse3f5a5f2012-07-06 09:50:09 -0700665 if (c < ' ' || c >= 127 || c == '\\') {
Kay Sieverse11fea92012-05-03 02:29:41 +0200666 len += sprintf(user->buf + len, "\\x%02x", c);
667 continue;
668 }
669
670 user->buf[len++] = c;
671 }
672 user->buf[len++] = '\n';
673 }
674
675 user->idx = log_next(user->idx);
676 user->seq++;
liu chuansheng5c53d812012-07-06 09:50:08 -0700677 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200678
679 if (len > count) {
680 ret = -EINVAL;
681 goto out;
682 }
683
684 if (copy_to_user(buf, user->buf, len)) {
685 ret = -EFAULT;
686 goto out;
687 }
688 ret = len;
689out:
690 mutex_unlock(&user->lock);
691 return ret;
692}
693
694static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence)
695{
696 struct devkmsg_user *user = file->private_data;
697 loff_t ret = 0;
698
699 if (!user)
700 return -EBADF;
701 if (offset)
702 return -ESPIPE;
703
liu chuansheng5c53d812012-07-06 09:50:08 -0700704 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200705 switch (whence) {
706 case SEEK_SET:
707 /* the first record */
708 user->idx = log_first_idx;
709 user->seq = log_first_seq;
710 break;
711 case SEEK_DATA:
712 /*
713 * The first record after the last SYSLOG_ACTION_CLEAR,
714 * like issued by 'dmesg -c'. Reading /dev/kmsg itself
715 * changes no global state, and does not clear anything.
716 */
717 user->idx = clear_idx;
718 user->seq = clear_seq;
719 break;
720 case SEEK_END:
721 /* after the last record */
722 user->idx = log_next_idx;
723 user->seq = log_next_seq;
724 break;
725 default:
726 ret = -EINVAL;
727 }
liu chuansheng5c53d812012-07-06 09:50:08 -0700728 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200729 return ret;
730}
731
732static unsigned int devkmsg_poll(struct file *file, poll_table *wait)
733{
734 struct devkmsg_user *user = file->private_data;
735 int ret = 0;
736
737 if (!user)
738 return POLLERR|POLLNVAL;
739
740 poll_wait(file, &log_wait, wait);
741
liu chuansheng5c53d812012-07-06 09:50:08 -0700742 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200743 if (user->seq < log_next_seq) {
744 /* return error when data has vanished underneath us */
745 if (user->seq < log_first_seq)
746 ret = POLLIN|POLLRDNORM|POLLERR|POLLPRI;
Nicolas Kaiser0a285312013-04-29 16:17:20 -0700747 else
748 ret = POLLIN|POLLRDNORM;
Kay Sieverse11fea92012-05-03 02:29:41 +0200749 }
liu chuansheng5c53d812012-07-06 09:50:08 -0700750 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200751
752 return ret;
753}
754
755static int devkmsg_open(struct inode *inode, struct file *file)
756{
757 struct devkmsg_user *user;
758 int err;
759
760 /* write-only does not need any file context */
761 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
762 return 0;
763
Kees Cook637241a2013-06-12 14:04:39 -0700764 err = check_syslog_permissions(SYSLOG_ACTION_READ_ALL,
765 SYSLOG_FROM_READER);
Kay Sieverse11fea92012-05-03 02:29:41 +0200766 if (err)
767 return err;
768
769 user = kmalloc(sizeof(struct devkmsg_user), GFP_KERNEL);
770 if (!user)
771 return -ENOMEM;
772
773 mutex_init(&user->lock);
774
liu chuansheng5c53d812012-07-06 09:50:08 -0700775 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200776 user->idx = log_first_idx;
777 user->seq = log_first_seq;
liu chuansheng5c53d812012-07-06 09:50:08 -0700778 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200779
780 file->private_data = user;
781 return 0;
782}
783
784static int devkmsg_release(struct inode *inode, struct file *file)
785{
786 struct devkmsg_user *user = file->private_data;
787
788 if (!user)
789 return 0;
790
791 mutex_destroy(&user->lock);
792 kfree(user);
793 return 0;
794}
795
796const struct file_operations kmsg_fops = {
797 .open = devkmsg_open,
798 .read = devkmsg_read,
Al Viro849f3122014-08-23 12:23:53 -0400799 .write_iter = devkmsg_write,
Kay Sieverse11fea92012-05-03 02:29:41 +0200800 .llseek = devkmsg_llseek,
801 .poll = devkmsg_poll,
802 .release = devkmsg_release,
803};
804
Neil Horman04d491a2009-04-02 16:58:57 -0700805#ifdef CONFIG_KEXEC
806/*
Dirk Gouders4c1ace62013-11-12 15:08:54 -0800807 * This appends the listed symbols to /proc/vmcore
Neil Horman04d491a2009-04-02 16:58:57 -0700808 *
Dirk Gouders4c1ace62013-11-12 15:08:54 -0800809 * /proc/vmcore is used by various utilities, like crash and makedumpfile to
Neil Horman04d491a2009-04-02 16:58:57 -0700810 * obtain access to symbols that are otherwise very difficult to locate. These
811 * symbols are specifically used so that utilities can access and extract the
812 * dmesg log from a vmcore file after a crash.
813 */
814void log_buf_kexec_setup(void)
815{
816 VMCOREINFO_SYMBOL(log_buf);
Neil Horman04d491a2009-04-02 16:58:57 -0700817 VMCOREINFO_SYMBOL(log_buf_len);
Kay Sievers7ff95542012-05-03 02:29:13 +0200818 VMCOREINFO_SYMBOL(log_first_idx);
819 VMCOREINFO_SYMBOL(log_next_idx);
Vivek Goyal67914572012-07-18 13:18:12 -0400820 /*
Joe Perches62e32ac2013-07-31 13:53:47 -0700821 * Export struct printk_log size and field offsets. User space tools can
Vivek Goyal67914572012-07-18 13:18:12 -0400822 * parse it and detect any changes to structure down the line.
823 */
Joe Perches62e32ac2013-07-31 13:53:47 -0700824 VMCOREINFO_STRUCT_SIZE(printk_log);
825 VMCOREINFO_OFFSET(printk_log, ts_nsec);
826 VMCOREINFO_OFFSET(printk_log, len);
827 VMCOREINFO_OFFSET(printk_log, text_len);
828 VMCOREINFO_OFFSET(printk_log, dict_len);
Neil Horman04d491a2009-04-02 16:58:57 -0700829}
830#endif
831
Mike Travis162a7e72011-05-24 17:13:20 -0700832/* requested log_buf_len from kernel cmdline */
833static unsigned long __initdata new_log_buf_len;
834
Luis R. Rodriguezc0a318a2014-08-06 16:08:52 -0700835/* we practice scaling the ring buffer by powers of 2 */
836static void __init log_buf_len_update(unsigned size)
837{
838 if (size)
839 size = roundup_pow_of_two(size);
840 if (size > log_buf_len)
841 new_log_buf_len = size;
842}
843
Mike Travis162a7e72011-05-24 17:13:20 -0700844/* save requested log_buf_len since it's too early to process it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845static int __init log_buf_len_setup(char *str)
846{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800847 unsigned size = memparse(str, &str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Luis R. Rodriguezc0a318a2014-08-06 16:08:52 -0700849 log_buf_len_update(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Mike Travis162a7e72011-05-24 17:13:20 -0700851 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
Mike Travis162a7e72011-05-24 17:13:20 -0700853early_param("log_buf_len", log_buf_len_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Luis R. Rodriguez23b28992014-08-06 16:08:56 -0700855static void __init log_buf_add_cpu(void)
856{
857 unsigned int cpu_extra;
858
859 /*
860 * archs should set up cpu_possible_bits properly with
861 * set_cpu_possible() after setup_arch() but just in
862 * case lets ensure this is valid.
863 */
864 if (num_possible_cpus() == 1)
865 return;
866
867 cpu_extra = (num_possible_cpus() - 1) * __LOG_CPU_MAX_BUF_LEN;
868
869 /* by default this will only continue through for large > 64 CPUs */
870 if (cpu_extra <= __LOG_BUF_LEN / 2)
871 return;
872
873 pr_info("log_buf_len individual max cpu contribution: %d bytes\n",
874 __LOG_CPU_MAX_BUF_LEN);
875 pr_info("log_buf_len total cpu_extra contributions: %d bytes\n",
876 cpu_extra);
877 pr_info("log_buf_len min size: %d bytes\n", __LOG_BUF_LEN);
878
879 log_buf_len_update(cpu_extra + __LOG_BUF_LEN);
880}
881
Mike Travis162a7e72011-05-24 17:13:20 -0700882void __init setup_log_buf(int early)
883{
884 unsigned long flags;
Mike Travis162a7e72011-05-24 17:13:20 -0700885 char *new_log_buf;
886 int free;
887
Luis R. Rodriguez23b28992014-08-06 16:08:56 -0700888 if (log_buf != __log_buf)
889 return;
890
891 if (!early && !new_log_buf_len)
892 log_buf_add_cpu();
893
Mike Travis162a7e72011-05-24 17:13:20 -0700894 if (!new_log_buf_len)
895 return;
896
897 if (early) {
Santosh Shilimkar9da791d2014-01-21 15:50:23 -0800898 new_log_buf =
Luis R. Rodriguez70300172014-08-06 16:08:49 -0700899 memblock_virt_alloc(new_log_buf_len, LOG_ALIGN);
Mike Travis162a7e72011-05-24 17:13:20 -0700900 } else {
Luis R. Rodriguez70300172014-08-06 16:08:49 -0700901 new_log_buf = memblock_virt_alloc_nopanic(new_log_buf_len,
902 LOG_ALIGN);
Mike Travis162a7e72011-05-24 17:13:20 -0700903 }
904
905 if (unlikely(!new_log_buf)) {
906 pr_err("log_buf_len: %ld bytes not available\n",
907 new_log_buf_len);
908 return;
909 }
910
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200911 raw_spin_lock_irqsave(&logbuf_lock, flags);
Mike Travis162a7e72011-05-24 17:13:20 -0700912 log_buf_len = new_log_buf_len;
913 log_buf = new_log_buf;
914 new_log_buf_len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +0200915 free = __LOG_BUF_LEN - log_next_idx;
916 memcpy(log_buf, __log_buf, __LOG_BUF_LEN);
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200917 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
Mike Travis162a7e72011-05-24 17:13:20 -0700918
Luis R. Rodriguezf5405172014-08-06 16:08:54 -0700919 pr_info("log_buf_len: %d bytes\n", log_buf_len);
Mike Travis162a7e72011-05-24 17:13:20 -0700920 pr_info("early log buf free: %d(%d%%)\n",
921 free, (free * 100) / __LOG_BUF_LEN);
922}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800924static bool __read_mostly ignore_loglevel;
925
926static int __init ignore_loglevel_setup(char *str)
927{
Neil Zhangd25d9fe2014-08-06 16:09:12 -0700928 ignore_loglevel = true;
Andrew Morton27083ba2013-11-12 15:08:50 -0800929 pr_info("debug: ignoring loglevel setting.\n");
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800930
931 return 0;
932}
933
934early_param("ignore_loglevel", ignore_loglevel_setup);
935module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR);
936MODULE_PARM_DESC(ignore_loglevel, "ignore loglevel setting, to"
937 "print all kernel messages to the console.");
938
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700939#ifdef CONFIG_BOOT_PRINTK_DELAY
940
Namhyung Kim674dff62010-10-26 14:22:48 -0700941static int boot_delay; /* msecs delay after each printk during bootup */
Dave Young3a3b6ed2009-09-22 16:43:31 -0700942static unsigned long long loops_per_msec; /* based on boot_delay */
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700943
944static int __init boot_delay_setup(char *str)
945{
946 unsigned long lpj;
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700947
948 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
949 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
950
951 get_option(&str, &boot_delay);
952 if (boot_delay > 10 * 1000)
953 boot_delay = 0;
954
Dave Young3a3b6ed2009-09-22 16:43:31 -0700955 pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
956 "HZ: %d, loops_per_msec: %llu\n",
957 boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
Dave Young29e9d222013-11-12 15:08:53 -0800958 return 0;
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700959}
Dave Young29e9d222013-11-12 15:08:53 -0800960early_param("boot_delay", boot_delay_setup);
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700961
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800962static void boot_delay_msec(int level)
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700963{
964 unsigned long long k;
965 unsigned long timeout;
966
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800967 if ((boot_delay == 0 || system_state != SYSTEM_BOOTING)
968 || (level >= console_loglevel && !ignore_loglevel)) {
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700969 return;
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800970 }
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700971
Dave Young3a3b6ed2009-09-22 16:43:31 -0700972 k = (unsigned long long)loops_per_msec * boot_delay;
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700973
974 timeout = jiffies + msecs_to_jiffies(boot_delay);
975 while (k) {
976 k--;
977 cpu_relax();
978 /*
979 * use (volatile) jiffies to prevent
980 * compiler reduction; loop termination via jiffies
981 * is secondary and may or may not happen.
982 */
983 if (time_after(jiffies, timeout))
984 break;
985 touch_nmi_watchdog();
986 }
987}
988#else
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800989static inline void boot_delay_msec(int level)
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700990{
991}
992#endif
993
Alex Eldere99aa462014-08-06 16:09:05 -0700994static bool printk_time = IS_ENABLED(CONFIG_PRINTK_TIME);
Kay Sievers7ff95542012-05-03 02:29:13 +0200995module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
996
Kay Sievers084681d2012-06-28 09:38:53 +0200997static size_t print_time(u64 ts, char *buf)
998{
999 unsigned long rem_nsec;
1000
1001 if (!printk_time)
1002 return 0;
1003
Kay Sievers084681d2012-06-28 09:38:53 +02001004 rem_nsec = do_div(ts, 1000000000);
Roland Dreier35dac272013-01-04 15:35:50 -08001005
1006 if (!buf)
1007 return snprintf(NULL, 0, "[%5lu.000000] ", (unsigned long)ts);
1008
Kay Sievers084681d2012-06-28 09:38:53 +02001009 return sprintf(buf, "[%5lu.%06lu] ",
1010 (unsigned long)ts, rem_nsec / 1000);
1011}
1012
Joe Perches62e32ac2013-07-31 13:53:47 -07001013static size_t print_prefix(const struct printk_log *msg, bool syslog, char *buf)
Kay Sievers649e6ee2012-05-10 04:30:45 +02001014{
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001015 size_t len = 0;
Kay Sievers43a73a52012-07-06 09:50:09 -07001016 unsigned int prefix = (msg->facility << 3) | msg->level;
Kay Sievers649e6ee2012-05-10 04:30:45 +02001017
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001018 if (syslog) {
1019 if (buf) {
Kay Sievers43a73a52012-07-06 09:50:09 -07001020 len += sprintf(buf, "<%u>", prefix);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001021 } else {
1022 len += 3;
Kay Sievers43a73a52012-07-06 09:50:09 -07001023 if (prefix > 999)
1024 len += 3;
1025 else if (prefix > 99)
1026 len += 2;
1027 else if (prefix > 9)
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001028 len++;
1029 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001030 }
1031
Kay Sievers084681d2012-06-28 09:38:53 +02001032 len += print_time(msg->ts_nsec, buf ? buf + len : NULL);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001033 return len;
1034}
1035
Joe Perches62e32ac2013-07-31 13:53:47 -07001036static size_t msg_print_text(const struct printk_log *msg, enum log_flags prev,
Kay Sievers5becfb12012-07-09 12:15:42 -07001037 bool syslog, char *buf, size_t size)
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001038{
1039 const char *text = log_text(msg);
1040 size_t text_size = msg->text_len;
Kay Sievers5becfb12012-07-09 12:15:42 -07001041 bool prefix = true;
1042 bool newline = true;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001043 size_t len = 0;
1044
Kay Sievers5becfb12012-07-09 12:15:42 -07001045 if ((prev & LOG_CONT) && !(msg->flags & LOG_PREFIX))
1046 prefix = false;
1047
1048 if (msg->flags & LOG_CONT) {
1049 if ((prev & LOG_CONT) && !(prev & LOG_NEWLINE))
1050 prefix = false;
1051
1052 if (!(msg->flags & LOG_NEWLINE))
1053 newline = false;
1054 }
1055
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001056 do {
1057 const char *next = memchr(text, '\n', text_size);
1058 size_t text_len;
1059
1060 if (next) {
1061 text_len = next - text;
1062 next++;
1063 text_size -= next - text;
1064 } else {
1065 text_len = text_size;
1066 }
1067
1068 if (buf) {
1069 if (print_prefix(msg, syslog, NULL) +
Kay Sievers70498252012-07-16 18:35:29 -07001070 text_len + 1 >= size - len)
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001071 break;
1072
Kay Sievers5becfb12012-07-09 12:15:42 -07001073 if (prefix)
1074 len += print_prefix(msg, syslog, buf + len);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001075 memcpy(buf + len, text, text_len);
1076 len += text_len;
Kay Sievers5becfb12012-07-09 12:15:42 -07001077 if (next || newline)
1078 buf[len++] = '\n';
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001079 } else {
1080 /* SYSLOG_ACTION_* buffer size only calculation */
Kay Sievers5becfb12012-07-09 12:15:42 -07001081 if (prefix)
1082 len += print_prefix(msg, syslog, NULL);
1083 len += text_len;
1084 if (next || newline)
1085 len++;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001086 }
1087
Kay Sievers5becfb12012-07-09 12:15:42 -07001088 prefix = true;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001089 text = next;
1090 } while (text);
1091
Kay Sievers7ff95542012-05-03 02:29:13 +02001092 return len;
1093}
1094
1095static int syslog_print(char __user *buf, int size)
1096{
1097 char *text;
Joe Perches62e32ac2013-07-31 13:53:47 -07001098 struct printk_log *msg;
Jan Beulich116e90b2012-06-22 16:36:09 +01001099 int len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001100
Kay Sievers70498252012-07-16 18:35:29 -07001101 text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
Kay Sievers7ff95542012-05-03 02:29:13 +02001102 if (!text)
1103 return -ENOMEM;
1104
Jan Beulich116e90b2012-06-22 16:36:09 +01001105 while (size > 0) {
1106 size_t n;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001107 size_t skip;
Kay Sievers7ff95542012-05-03 02:29:13 +02001108
Jan Beulich116e90b2012-06-22 16:36:09 +01001109 raw_spin_lock_irq(&logbuf_lock);
1110 if (syslog_seq < log_first_seq) {
1111 /* messages are gone, move to first one */
1112 syslog_seq = log_first_seq;
1113 syslog_idx = log_first_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001114 syslog_prev = 0;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001115 syslog_partial = 0;
Jan Beulich116e90b2012-06-22 16:36:09 +01001116 }
1117 if (syslog_seq == log_next_seq) {
1118 raw_spin_unlock_irq(&logbuf_lock);
1119 break;
1120 }
Kay Sieverseb02dac2012-07-09 10:05:10 -07001121
1122 skip = syslog_partial;
Jan Beulich116e90b2012-06-22 16:36:09 +01001123 msg = log_from_idx(syslog_idx);
Kay Sievers70498252012-07-16 18:35:29 -07001124 n = msg_print_text(msg, syslog_prev, true, text,
1125 LOG_LINE_MAX + PREFIX_MAX);
Kay Sieverseb02dac2012-07-09 10:05:10 -07001126 if (n - syslog_partial <= size) {
1127 /* message fits into buffer, move forward */
Jan Beulich116e90b2012-06-22 16:36:09 +01001128 syslog_idx = log_next(syslog_idx);
1129 syslog_seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07001130 syslog_prev = msg->flags;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001131 n -= syslog_partial;
1132 syslog_partial = 0;
1133 } else if (!len){
1134 /* partial read(), remember position */
1135 n = size;
1136 syslog_partial += n;
Jan Beulich116e90b2012-06-22 16:36:09 +01001137 } else
1138 n = 0;
1139 raw_spin_unlock_irq(&logbuf_lock);
1140
1141 if (!n)
1142 break;
1143
Kay Sieverseb02dac2012-07-09 10:05:10 -07001144 if (copy_to_user(buf, text + skip, n)) {
Jan Beulich116e90b2012-06-22 16:36:09 +01001145 if (!len)
1146 len = -EFAULT;
1147 break;
1148 }
Kay Sieverseb02dac2012-07-09 10:05:10 -07001149
1150 len += n;
1151 size -= n;
1152 buf += n;
Jan Beulich116e90b2012-06-22 16:36:09 +01001153 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001154
1155 kfree(text);
1156 return len;
1157}
1158
1159static int syslog_print_all(char __user *buf, int size, bool clear)
1160{
1161 char *text;
1162 int len = 0;
1163
Kay Sievers70498252012-07-16 18:35:29 -07001164 text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
Kay Sievers7ff95542012-05-03 02:29:13 +02001165 if (!text)
1166 return -ENOMEM;
1167
1168 raw_spin_lock_irq(&logbuf_lock);
1169 if (buf) {
1170 u64 next_seq;
1171 u64 seq;
1172 u32 idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001173 enum log_flags prev;
Kay Sievers7ff95542012-05-03 02:29:13 +02001174
1175 if (clear_seq < log_first_seq) {
1176 /* messages are gone, move to first available one */
1177 clear_seq = log_first_seq;
1178 clear_idx = log_first_idx;
1179 }
1180
1181 /*
1182 * Find first record that fits, including all following records,
1183 * into the user-provided buffer for this dump.
Kay Sieverse2ae7152012-06-15 14:07:51 +02001184 */
Kay Sievers7ff95542012-05-03 02:29:13 +02001185 seq = clear_seq;
1186 idx = clear_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001187 prev = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001188 while (seq < log_next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07001189 struct printk_log *msg = log_from_idx(idx);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001190
Kay Sievers5becfb12012-07-09 12:15:42 -07001191 len += msg_print_text(msg, prev, true, NULL, 0);
Jeff Mahoneye3756472012-08-10 15:07:09 -04001192 prev = msg->flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02001193 idx = log_next(idx);
1194 seq++;
1195 }
Kay Sieverse2ae7152012-06-15 14:07:51 +02001196
1197 /* move first record forward until length fits into the buffer */
Kay Sievers7ff95542012-05-03 02:29:13 +02001198 seq = clear_seq;
1199 idx = clear_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001200 prev = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001201 while (len > size && seq < log_next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07001202 struct printk_log *msg = log_from_idx(idx);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001203
Kay Sievers5becfb12012-07-09 12:15:42 -07001204 len -= msg_print_text(msg, prev, true, NULL, 0);
Jeff Mahoneye3756472012-08-10 15:07:09 -04001205 prev = msg->flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02001206 idx = log_next(idx);
1207 seq++;
1208 }
1209
Kay Sieverse2ae7152012-06-15 14:07:51 +02001210 /* last message fitting into this dump */
Kay Sievers7ff95542012-05-03 02:29:13 +02001211 next_seq = log_next_seq;
1212
1213 len = 0;
1214 while (len >= 0 && seq < next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07001215 struct printk_log *msg = log_from_idx(idx);
Kay Sievers7ff95542012-05-03 02:29:13 +02001216 int textlen;
1217
Kay Sievers70498252012-07-16 18:35:29 -07001218 textlen = msg_print_text(msg, prev, true, text,
1219 LOG_LINE_MAX + PREFIX_MAX);
Kay Sievers7ff95542012-05-03 02:29:13 +02001220 if (textlen < 0) {
1221 len = textlen;
1222 break;
1223 }
1224 idx = log_next(idx);
1225 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07001226 prev = msg->flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02001227
1228 raw_spin_unlock_irq(&logbuf_lock);
1229 if (copy_to_user(buf + len, text, textlen))
1230 len = -EFAULT;
1231 else
1232 len += textlen;
1233 raw_spin_lock_irq(&logbuf_lock);
1234
1235 if (seq < log_first_seq) {
1236 /* messages are gone, move to next one */
1237 seq = log_first_seq;
1238 idx = log_first_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001239 prev = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001240 }
1241 }
1242 }
1243
1244 if (clear) {
1245 clear_seq = log_next_seq;
1246 clear_idx = log_next_idx;
1247 }
1248 raw_spin_unlock_irq(&logbuf_lock);
1249
1250 kfree(text);
1251 return len;
1252}
1253
Kees Cook00234592010-02-03 15:36:43 -08001254int do_syslog(int type, char __user *buf, int len, bool from_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Kay Sievers7ff95542012-05-03 02:29:13 +02001256 bool clear = false;
1257 static int saved_console_loglevel = -1;
Linus Torvaldsee24aeb2011-02-10 17:53:55 -08001258 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Linus Torvaldsee24aeb2011-02-10 17:53:55 -08001260 error = check_syslog_permissions(type, from_file);
1261 if (error)
1262 goto out;
Eric Paris12b30522010-11-15 18:36:29 -05001263
1264 error = security_syslog(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (error)
1266 return error;
1267
1268 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08001269 case SYSLOG_ACTION_CLOSE: /* Close log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001271 case SYSLOG_ACTION_OPEN: /* Open log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001273 case SYSLOG_ACTION_READ: /* Read from log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 error = -EINVAL;
1275 if (!buf || len < 0)
1276 goto out;
1277 error = 0;
1278 if (!len)
1279 goto out;
1280 if (!access_ok(VERIFY_WRITE, buf, len)) {
1281 error = -EFAULT;
1282 goto out;
1283 }
Yuanhan Liu4a77a5a2012-06-16 21:21:51 +08001284 error = wait_event_interruptible(log_wait,
1285 syslog_seq != log_next_seq);
Kay Sieverscb424ff2012-07-06 09:50:09 -07001286 if (error)
Yuanhan Liu4a77a5a2012-06-16 21:21:51 +08001287 goto out;
Kay Sievers7ff95542012-05-03 02:29:13 +02001288 error = syslog_print(buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001290 /* Read/clear last kernel messages */
1291 case SYSLOG_ACTION_READ_CLEAR:
Kay Sievers7ff95542012-05-03 02:29:13 +02001292 clear = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 /* FALL THRU */
Kees Cookd78ca3c2010-02-03 15:37:13 -08001294 /* Read last kernel messages */
1295 case SYSLOG_ACTION_READ_ALL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 error = -EINVAL;
1297 if (!buf || len < 0)
1298 goto out;
1299 error = 0;
1300 if (!len)
1301 goto out;
1302 if (!access_ok(VERIFY_WRITE, buf, len)) {
1303 error = -EFAULT;
1304 goto out;
1305 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001306 error = syslog_print_all(buf, len, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001308 /* Clear ring buffer */
1309 case SYSLOG_ACTION_CLEAR:
Kay Sievers7ff95542012-05-03 02:29:13 +02001310 syslog_print_all(NULL, 0, true);
Alan Stern4661e352012-06-22 17:12:19 -04001311 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001312 /* Disable logging to console */
1313 case SYSLOG_ACTION_CONSOLE_OFF:
Frans Pop1aaad492009-07-06 13:31:48 +02001314 if (saved_console_loglevel == -1)
1315 saved_console_loglevel = console_loglevel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 console_loglevel = minimum_console_loglevel;
1317 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001318 /* Enable logging to console */
1319 case SYSLOG_ACTION_CONSOLE_ON:
Frans Pop1aaad492009-07-06 13:31:48 +02001320 if (saved_console_loglevel != -1) {
1321 console_loglevel = saved_console_loglevel;
1322 saved_console_loglevel = -1;
1323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001325 /* Set level of messages printed to console */
1326 case SYSLOG_ACTION_CONSOLE_LEVEL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 error = -EINVAL;
1328 if (len < 1 || len > 8)
1329 goto out;
1330 if (len < minimum_console_loglevel)
1331 len = minimum_console_loglevel;
1332 console_loglevel = len;
Frans Pop1aaad492009-07-06 13:31:48 +02001333 /* Implicitly re-enable logging to console */
1334 saved_console_loglevel = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 error = 0;
1336 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001337 /* Number of chars in the log buffer */
1338 case SYSLOG_ACTION_SIZE_UNREAD:
Kay Sievers7ff95542012-05-03 02:29:13 +02001339 raw_spin_lock_irq(&logbuf_lock);
1340 if (syslog_seq < log_first_seq) {
1341 /* messages are gone, move to first one */
1342 syslog_seq = log_first_seq;
1343 syslog_idx = log_first_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001344 syslog_prev = 0;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001345 syslog_partial = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001346 }
1347 if (from_file) {
1348 /*
1349 * Short-cut for poll(/"proc/kmsg") which simply checks
1350 * for pending data, not the size; return the count of
1351 * records, not the length.
1352 */
Alex Eldere97e1262014-08-06 16:08:59 -07001353 error = log_next_seq - syslog_seq;
Kay Sievers7ff95542012-05-03 02:29:13 +02001354 } else {
Kay Sievers5becfb12012-07-09 12:15:42 -07001355 u64 seq = syslog_seq;
1356 u32 idx = syslog_idx;
1357 enum log_flags prev = syslog_prev;
Kay Sievers7ff95542012-05-03 02:29:13 +02001358
1359 error = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001360 while (seq < log_next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07001361 struct printk_log *msg = log_from_idx(idx);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001362
Kay Sievers5becfb12012-07-09 12:15:42 -07001363 error += msg_print_text(msg, prev, true, NULL, 0);
Kay Sievers7ff95542012-05-03 02:29:13 +02001364 idx = log_next(idx);
1365 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07001366 prev = msg->flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02001367 }
Kay Sieverseb02dac2012-07-09 10:05:10 -07001368 error -= syslog_partial;
Kay Sievers7ff95542012-05-03 02:29:13 +02001369 }
1370 raw_spin_unlock_irq(&logbuf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001372 /* Size of the log buffer */
1373 case SYSLOG_ACTION_SIZE_BUFFER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 error = log_buf_len;
1375 break;
1376 default:
1377 error = -EINVAL;
1378 break;
1379 }
1380out:
1381 return error;
1382}
1383
Heiko Carstens1e7bfb22009-01-14 14:14:29 +01001384SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
Kees Cook637241a2013-06-12 14:04:39 -07001386 return do_syslog(type, buf, len, SYSLOG_FROM_READER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 * Call the console drivers, asking them to write out
1391 * log_buf[start] to log_buf[end - 1].
Torben Hohnac751ef2011-01-25 15:07:35 -08001392 * The console_lock must be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 */
Kay Sievers7ff95542012-05-03 02:29:13 +02001394static void call_console_drivers(int level, const char *text, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Kay Sievers7ff95542012-05-03 02:29:13 +02001396 struct console *con;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
zhangwei(Jovi)07c65f42013-04-29 16:17:16 -07001398 trace_console(text, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Kay Sievers7ff95542012-05-03 02:29:13 +02001400 if (level >= console_loglevel && !ignore_loglevel)
1401 return;
1402 if (!console_drivers)
1403 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Kay Sievers7ff95542012-05-03 02:29:13 +02001405 for_each_console(con) {
1406 if (exclusive_console && con != exclusive_console)
1407 continue;
1408 if (!(con->flags & CON_ENABLED))
1409 continue;
1410 if (!con->write)
1411 continue;
1412 if (!cpu_online(smp_processor_id()) &&
1413 !(con->flags & CON_ANYTIME))
1414 continue;
1415 con->write(con, text, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418
1419/*
1420 * Zap console related locks when oopsing. Only zap at most once
1421 * every 10 seconds, to leave time for slow consoles to print a
1422 * full oops.
1423 */
1424static void zap_locks(void)
1425{
1426 static unsigned long oops_timestamp;
1427
1428 if (time_after_eq(jiffies, oops_timestamp) &&
Jesper Juhl40dc5652005-10-30 15:02:46 -08001429 !time_after(jiffies, oops_timestamp + 30 * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return;
1431
1432 oops_timestamp = jiffies;
1433
Peter Zijlstra94d24fc2011-06-07 11:17:30 +02001434 debug_locks_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 /* If a crash is occurring, make sure we can't deadlock */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001436 raw_spin_lock_init(&logbuf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 /* And make sure that we print immediately */
Thomas Gleixner5b8c4f22010-09-07 14:33:43 +00001438 sema_init(&console_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439}
1440
Jan Kara608873c2014-06-04 16:11:35 -07001441/*
1442 * Check if we have any console that is capable of printing while cpu is
1443 * booting or shutting down. Requires console_sem.
1444 */
Michael Ellerman76a8ad292006-06-25 05:47:40 -07001445static int have_callable_console(void)
1446{
1447 struct console *con;
1448
Robin Getz4d091612009-07-01 21:08:37 -04001449 for_each_console(con)
Michael Ellerman76a8ad292006-06-25 05:47:40 -07001450 if (con->flags & CON_ANYTIME)
1451 return 1;
1452
1453 return 0;
1454}
1455
Linus Torvalds266c2e02008-03-24 19:25:08 -07001456/*
1457 * Can we actually use the console at this time on this cpu?
1458 *
Jan Kara5874af22014-08-06 16:09:10 -07001459 * Console drivers may assume that per-cpu resources have been allocated. So
1460 * unless they're explicitly marked as being able to cope (CON_ANYTIME) don't
1461 * call them until this CPU is officially up.
Linus Torvalds266c2e02008-03-24 19:25:08 -07001462 */
1463static inline int can_use_console(unsigned int cpu)
1464{
1465 return cpu_online(cpu) || have_callable_console();
1466}
1467
1468/*
1469 * Try to get console ownership to actually show the kernel
1470 * messages from a 'printk'. Return true (and with the
Torben Hohnac751ef2011-01-25 15:07:35 -08001471 * console_lock held, and 'console_locked' set) if it
Linus Torvalds266c2e02008-03-24 19:25:08 -07001472 * is successful, false otherwise.
Linus Torvalds266c2e02008-03-24 19:25:08 -07001473 */
Jan Kara5874af22014-08-06 16:09:10 -07001474static int console_trylock_for_printk(void)
Linus Torvalds266c2e02008-03-24 19:25:08 -07001475{
Jan Kara5874af22014-08-06 16:09:10 -07001476 unsigned int cpu = smp_processor_id();
1477
Jan Kara608873c2014-06-04 16:11:35 -07001478 if (!console_trylock())
1479 return 0;
1480 /*
1481 * If we can't use the console, we need to release the console
1482 * semaphore by hand to avoid flushing the buffer. We need to hold the
1483 * console semaphore in order to do this test safely.
1484 */
1485 if (!can_use_console(cpu)) {
1486 console_locked = 0;
Jan Karabd8d7cf2014-06-04 16:11:36 -07001487 up_console_sem();
Jan Kara608873c2014-06-04 16:11:35 -07001488 return 0;
1489 }
1490 return 1;
Linus Torvalds266c2e02008-03-24 19:25:08 -07001491}
Ingo Molnar32a76002008-01-25 21:07:58 +01001492
Dave Youngaf913222009-09-22 16:43:33 -07001493int printk_delay_msec __read_mostly;
1494
1495static inline void printk_delay(void)
1496{
1497 if (unlikely(printk_delay_msec)) {
1498 int m = printk_delay_msec;
1499
1500 while (m--) {
1501 mdelay(1);
1502 touch_nmi_watchdog();
1503 }
1504 }
1505}
1506
Kay Sievers084681d2012-06-28 09:38:53 +02001507/*
1508 * Continuation lines are buffered, and not committed to the record buffer
1509 * until the line is complete, or a race forces it. The line fragments
1510 * though, are printed immediately to the consoles to ensure everything has
1511 * reached the console in case of a kernel crash.
1512 */
1513static struct cont {
1514 char buf[LOG_LINE_MAX];
1515 size_t len; /* length == 0 means unused buffer */
1516 size_t cons; /* bytes written to console */
1517 struct task_struct *owner; /* task of first print*/
1518 u64 ts_nsec; /* time of first print */
1519 u8 level; /* log level of first message */
Alex Elder0b90fec2014-08-06 16:09:03 -07001520 u8 facility; /* log facility of first message */
Kay Sieverseab07262012-07-16 18:35:30 -07001521 enum log_flags flags; /* prefix, newline flags */
Kay Sievers084681d2012-06-28 09:38:53 +02001522 bool flushed:1; /* buffer sealed and committed */
1523} cont;
1524
Kay Sievers70498252012-07-16 18:35:29 -07001525static void cont_flush(enum log_flags flags)
Kay Sievers084681d2012-06-28 09:38:53 +02001526{
1527 if (cont.flushed)
1528 return;
1529 if (cont.len == 0)
1530 return;
1531
Kay Sieverseab07262012-07-16 18:35:30 -07001532 if (cont.cons) {
1533 /*
1534 * If a fragment of this line was directly flushed to the
1535 * console; wait for the console to pick up the rest of the
1536 * line. LOG_NOCONS suppresses a duplicated output.
1537 */
1538 log_store(cont.facility, cont.level, flags | LOG_NOCONS,
1539 cont.ts_nsec, NULL, 0, cont.buf, cont.len);
1540 cont.flags = flags;
1541 cont.flushed = true;
1542 } else {
1543 /*
1544 * If no fragment of this line ever reached the console,
1545 * just submit it to the store and free the buffer.
1546 */
1547 log_store(cont.facility, cont.level, flags, 0,
1548 NULL, 0, cont.buf, cont.len);
1549 cont.len = 0;
1550 }
Kay Sievers084681d2012-06-28 09:38:53 +02001551}
1552
1553static bool cont_add(int facility, int level, const char *text, size_t len)
1554{
1555 if (cont.len && cont.flushed)
1556 return false;
1557
1558 if (cont.len + len > sizeof(cont.buf)) {
Kay Sievers70498252012-07-16 18:35:29 -07001559 /* the line gets too long, split it up in separate records */
1560 cont_flush(LOG_CONT);
Kay Sievers084681d2012-06-28 09:38:53 +02001561 return false;
1562 }
1563
1564 if (!cont.len) {
1565 cont.facility = facility;
1566 cont.level = level;
1567 cont.owner = current;
1568 cont.ts_nsec = local_clock();
Kay Sieverseab07262012-07-16 18:35:30 -07001569 cont.flags = 0;
Kay Sievers084681d2012-06-28 09:38:53 +02001570 cont.cons = 0;
1571 cont.flushed = false;
1572 }
1573
1574 memcpy(cont.buf + cont.len, text, len);
1575 cont.len += len;
Kay Sieverseab07262012-07-16 18:35:30 -07001576
1577 if (cont.len > (sizeof(cont.buf) * 80) / 100)
1578 cont_flush(LOG_CONT);
1579
Kay Sievers084681d2012-06-28 09:38:53 +02001580 return true;
1581}
1582
1583static size_t cont_print_text(char *text, size_t size)
1584{
1585 size_t textlen = 0;
1586 size_t len;
1587
Kay Sieverseab07262012-07-16 18:35:30 -07001588 if (cont.cons == 0 && (console_prev & LOG_NEWLINE)) {
Kay Sievers084681d2012-06-28 09:38:53 +02001589 textlen += print_time(cont.ts_nsec, text);
1590 size -= textlen;
1591 }
1592
1593 len = cont.len - cont.cons;
1594 if (len > 0) {
1595 if (len+1 > size)
1596 len = size-1;
1597 memcpy(text + textlen, cont.buf + cont.cons, len);
1598 textlen += len;
1599 cont.cons = cont.len;
1600 }
1601
1602 if (cont.flushed) {
Kay Sieverseab07262012-07-16 18:35:30 -07001603 if (cont.flags & LOG_NEWLINE)
1604 text[textlen++] = '\n';
Kay Sievers084681d2012-06-28 09:38:53 +02001605 /* got everything, release buffer */
1606 cont.len = 0;
1607 }
1608 return textlen;
1609}
1610
Kay Sievers7ff95542012-05-03 02:29:13 +02001611asmlinkage int vprintk_emit(int facility, int level,
1612 const char *dict, size_t dictlen,
1613 const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
Kay Sievers7ff95542012-05-03 02:29:13 +02001615 static int recursion_bug;
Kay Sievers7ff95542012-05-03 02:29:13 +02001616 static char textbuf[LOG_LINE_MAX];
1617 char *text = textbuf;
Steven Rostedt458df9f2014-06-04 16:11:38 -07001618 size_t text_len = 0;
Kay Sievers5becfb12012-07-09 12:15:42 -07001619 enum log_flags lflags = 0;
Nick Andrewac60ad72008-05-12 21:21:04 +02001620 unsigned long flags;
Ingo Molnar32a76002008-01-25 21:07:58 +01001621 int this_cpu;
Kay Sievers7ff95542012-05-03 02:29:13 +02001622 int printed_len = 0;
Steven Rostedt458df9f2014-06-04 16:11:38 -07001623 bool in_sched = false;
Jan Kara608873c2014-06-04 16:11:35 -07001624 /* cpu currently holding logbuf_lock in this function */
1625 static volatile unsigned int logbuf_cpu = UINT_MAX;
1626
Steven Rostedt458df9f2014-06-04 16:11:38 -07001627 if (level == SCHED_MESSAGE_LOGLEVEL) {
1628 level = -1;
1629 in_sched = true;
1630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Andrew Cooks2fa72c82012-12-17 15:59:56 -08001632 boot_delay_msec(level);
Dave Youngaf913222009-09-22 16:43:33 -07001633 printk_delay();
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 /* This stops the holder of console_sem just where we want him */
Peter Zijlstra1a9a8ae2011-06-07 11:17:30 +02001636 local_irq_save(flags);
Ingo Molnar32a76002008-01-25 21:07:58 +01001637 this_cpu = smp_processor_id();
1638
1639 /*
1640 * Ouch, printk recursed into itself!
1641 */
Kay Sievers7ff95542012-05-03 02:29:13 +02001642 if (unlikely(logbuf_cpu == this_cpu)) {
Ingo Molnar32a76002008-01-25 21:07:58 +01001643 /*
1644 * If a crash is occurring during printk() on this CPU,
1645 * then try to get the crash message out but make sure
1646 * we can't deadlock. Otherwise just return to avoid the
1647 * recursion and return - but flag the recursion so that
1648 * it can be printed at the next appropriate moment:
1649 */
Peter Zijlstra94d24fc2011-06-07 11:17:30 +02001650 if (!oops_in_progress && !lockdep_recursing(current)) {
Tejun Heo3b8945e2008-05-12 21:21:04 +02001651 recursion_bug = 1;
Jan Kara5874af22014-08-06 16:09:10 -07001652 local_irq_restore(flags);
1653 return 0;
Ingo Molnar32a76002008-01-25 21:07:58 +01001654 }
1655 zap_locks();
1656 }
1657
Ingo Molnara0f1ccf2006-07-03 00:24:58 -07001658 lockdep_off();
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001659 raw_spin_lock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02001660 logbuf_cpu = this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Patrick Palka000a7d62014-09-09 14:50:48 -07001662 if (unlikely(recursion_bug)) {
Kay Sievers7ff95542012-05-03 02:29:13 +02001663 static const char recursion_msg[] =
1664 "BUG: recent printk recursion!";
1665
Tejun Heo3b8945e2008-05-12 21:21:04 +02001666 recursion_bug = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001667 /* emit KERN_CRIT message */
Petr Mladek034633c2014-06-04 16:11:33 -07001668 printed_len += log_store(0, 2, LOG_PREFIX|LOG_NEWLINE, 0,
Patrick Palka000a7d62014-09-09 14:50:48 -07001669 NULL, 0, recursion_msg,
1670 strlen(recursion_msg));
Linus Torvalds5fd29d62009-06-16 10:57:02 -07001671 }
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 /*
Kay Sievers7ff95542012-05-03 02:29:13 +02001674 * The printf needs to come first; we need the syslog
1675 * prefix which might be passed-in as a parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 */
Steven Rostedt458df9f2014-06-04 16:11:38 -07001677 if (in_sched)
1678 text_len = scnprintf(text, sizeof(textbuf),
1679 KERN_WARNING "[sched_delayed] ");
1680
1681 text_len += vscnprintf(text + text_len,
1682 sizeof(textbuf) - text_len, fmt, args);
Nick Andrewac60ad72008-05-12 21:21:04 +02001683
Kay Sievers7ff95542012-05-03 02:29:13 +02001684 /* mark and strip a trailing newline */
Kay Sieversc313af12012-05-14 20:46:27 +02001685 if (text_len && text[text_len-1] == '\n') {
1686 text_len--;
Kay Sievers5becfb12012-07-09 12:15:42 -07001687 lflags |= LOG_NEWLINE;
Kay Sievers7ff95542012-05-03 02:29:13 +02001688 }
Kay Sievers9d90c8d2011-03-13 03:19:51 +01001689
Joe Perches088a52a2012-07-30 14:40:19 -07001690 /* strip kernel syslog prefix and extract log level or control flags */
1691 if (facility == 0) {
1692 int kern_level = printk_get_level(text);
1693
1694 if (kern_level) {
1695 const char *end_of_header = printk_skip_level(text);
1696 switch (kern_level) {
1697 case '0' ... '7':
1698 if (level == -1)
1699 level = kern_level - '0';
1700 case 'd': /* KERN_DEFAULT */
1701 lflags |= LOG_PREFIX;
Joe Perches088a52a2012-07-30 14:40:19 -07001702 }
Petr Mladeke8c42d32014-04-03 14:48:41 -07001703 /*
1704 * No need to check length here because vscnprintf
1705 * put '\0' at the end of the string. Only valid and
1706 * newly printed level is detected.
1707 */
Joe Perches088a52a2012-07-30 14:40:19 -07001708 text_len -= end_of_header - text;
1709 text = (char *)end_of_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001711 }
Nick Andrewac60ad72008-05-12 21:21:04 +02001712
Kay Sieversc313af12012-05-14 20:46:27 +02001713 if (level == -1)
1714 level = default_message_loglevel;
1715
Kay Sievers5becfb12012-07-09 12:15:42 -07001716 if (dict)
1717 lflags |= LOG_PREFIX|LOG_NEWLINE;
Kay Sievers7ff95542012-05-03 02:29:13 +02001718
Kay Sievers5becfb12012-07-09 12:15:42 -07001719 if (!(lflags & LOG_NEWLINE)) {
Kay Sievers084681d2012-06-28 09:38:53 +02001720 /*
1721 * Flush the conflicting buffer. An earlier newline was missing,
1722 * or another task also prints continuation lines.
1723 */
Kay Sievers5becfb12012-07-09 12:15:42 -07001724 if (cont.len && (lflags & LOG_PREFIX || cont.owner != current))
Kay Sieverseab07262012-07-16 18:35:30 -07001725 cont_flush(LOG_NEWLINE);
Kay Sieversc313af12012-05-14 20:46:27 +02001726
Kay Sievers084681d2012-06-28 09:38:53 +02001727 /* buffer line if possible, otherwise store it right away */
Petr Mladek034633c2014-06-04 16:11:33 -07001728 if (cont_add(facility, level, text, text_len))
1729 printed_len += text_len;
1730 else
1731 printed_len += log_store(facility, level,
1732 lflags | LOG_CONT, 0,
1733 dict, dictlen, text, text_len);
Kay Sievers084681d2012-06-28 09:38:53 +02001734 } else {
1735 bool stored = false;
1736
1737 /*
Steven Rostedtd3620822012-06-29 11:40:11 -04001738 * If an earlier newline was missing and it was the same task,
1739 * either merge it with the current buffer and flush, or if
1740 * there was a race with interrupts (prefix == true) then just
1741 * flush it out and store this line separately.
Arun KS1d3fa372014-01-23 15:54:19 -08001742 * If the preceding printk was from a different task and missed
1743 * a newline, flush and append the newline.
Kay Sievers084681d2012-06-28 09:38:53 +02001744 */
Arun KS1d3fa372014-01-23 15:54:19 -08001745 if (cont.len) {
1746 if (cont.owner == current && !(lflags & LOG_PREFIX))
1747 stored = cont_add(facility, level, text,
1748 text_len);
Kay Sieverseab07262012-07-16 18:35:30 -07001749 cont_flush(LOG_NEWLINE);
Kay Sieversc313af12012-05-14 20:46:27 +02001750 }
Kay Sievers084681d2012-06-28 09:38:53 +02001751
Petr Mladek034633c2014-06-04 16:11:33 -07001752 if (stored)
1753 printed_len += text_len;
1754 else
1755 printed_len += log_store(facility, level, lflags, 0,
1756 dict, dictlen, text, text_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 }
1758
Jan Kara608873c2014-06-04 16:11:35 -07001759 logbuf_cpu = UINT_MAX;
1760 raw_spin_unlock(&logbuf_lock);
Jan Kara5874af22014-08-06 16:09:10 -07001761 lockdep_on();
1762 local_irq_restore(flags);
Jan Kara939f04b2014-06-04 16:11:37 -07001763
Steven Rostedt458df9f2014-06-04 16:11:38 -07001764 /* If called from the scheduler, we can not call up(). */
Andrew Mortond18bbc22014-07-02 15:22:38 -07001765 if (!in_sched) {
Jan Kara5874af22014-08-06 16:09:10 -07001766 lockdep_off();
1767 /*
1768 * Disable preemption to avoid being preempted while holding
1769 * console_sem which would prevent anyone from printing to
1770 * console
1771 */
1772 preempt_disable();
1773
Andrew Mortond18bbc22014-07-02 15:22:38 -07001774 /*
1775 * Try to acquire and then immediately release the console
1776 * semaphore. The release will print out buffers and wake up
1777 * /dev/kmsg and syslog() users.
1778 */
Jan Kara5874af22014-08-06 16:09:10 -07001779 if (console_trylock_for_printk())
Andrew Mortond18bbc22014-07-02 15:22:38 -07001780 console_unlock();
Jan Kara5874af22014-08-06 16:09:10 -07001781 preempt_enable();
1782 lockdep_on();
Andrew Mortond18bbc22014-07-02 15:22:38 -07001783 }
Steven Rostedt458df9f2014-06-04 16:11:38 -07001784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 return printed_len;
1786}
Kay Sievers7ff95542012-05-03 02:29:13 +02001787EXPORT_SYMBOL(vprintk_emit);
1788
1789asmlinkage int vprintk(const char *fmt, va_list args)
1790{
1791 return vprintk_emit(0, -1, NULL, 0, fmt, args);
1792}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793EXPORT_SYMBOL(vprintk);
1794
Kay Sievers7ff95542012-05-03 02:29:13 +02001795asmlinkage int printk_emit(int facility, int level,
1796 const char *dict, size_t dictlen,
1797 const char *fmt, ...)
1798{
1799 va_list args;
1800 int r;
1801
1802 va_start(args, fmt);
1803 r = vprintk_emit(facility, level, dict, dictlen, fmt, args);
1804 va_end(args);
1805
1806 return r;
1807}
1808EXPORT_SYMBOL(printk_emit);
1809
1810/**
1811 * printk - print a kernel message
1812 * @fmt: format string
1813 *
1814 * This is printk(). It can be called from any context. We want it to work.
1815 *
1816 * We try to grab the console_lock. If we succeed, it's easy - we log the
1817 * output and call the console drivers. If we fail to get the semaphore, we
1818 * place the output into the log buffer and return. The current holder of
1819 * the console_sem will notice the new output in console_unlock(); and will
1820 * send it to the consoles before releasing the lock.
1821 *
1822 * One effect of this deferred printing is that code which calls printk() and
1823 * then changes console_loglevel may break. This is because console_loglevel
1824 * is inspected when the actual printing occurs.
1825 *
1826 * See also:
1827 * printf(3)
1828 *
1829 * See the vsnprintf() documentation for format string extensions over C99.
1830 */
Andi Kleen722a9f92014-05-02 00:44:38 +02001831asmlinkage __visible int printk(const char *fmt, ...)
Kay Sievers7ff95542012-05-03 02:29:13 +02001832{
1833 va_list args;
1834 int r;
1835
1836#ifdef CONFIG_KGDB_KDB
1837 if (unlikely(kdb_trap_printk)) {
1838 va_start(args, fmt);
1839 r = vkdb_printf(fmt, args);
1840 va_end(args);
1841 return r;
1842 }
1843#endif
1844 va_start(args, fmt);
1845 r = vprintk_emit(0, -1, NULL, 0, fmt, args);
1846 va_end(args);
1847
1848 return r;
1849}
1850EXPORT_SYMBOL(printk);
Kay Sievers7f3a7812012-05-09 01:37:51 +02001851
Kay Sievers96efedf2012-07-16 18:35:29 -07001852#else /* CONFIG_PRINTK */
Matt Mackalld59745c2005-05-01 08:59:02 -07001853
Kay Sievers70498252012-07-16 18:35:29 -07001854#define LOG_LINE_MAX 0
1855#define PREFIX_MAX 0
Alex Elder249771b2014-08-06 16:09:08 -07001856
Kay Sievers96efedf2012-07-16 18:35:29 -07001857static u64 syslog_seq;
1858static u32 syslog_idx;
Kay Sieverseab07262012-07-16 18:35:30 -07001859static u64 console_seq;
1860static u32 console_idx;
Kay Sievers96efedf2012-07-16 18:35:29 -07001861static enum log_flags syslog_prev;
1862static u64 log_first_seq;
1863static u32 log_first_idx;
1864static u64 log_next_seq;
Kay Sieverseab07262012-07-16 18:35:30 -07001865static enum log_flags console_prev;
Kay Sievers084681d2012-06-28 09:38:53 +02001866static struct cont {
1867 size_t len;
1868 size_t cons;
1869 u8 level;
1870 bool flushed:1;
1871} cont;
Joe Perches62e32ac2013-07-31 13:53:47 -07001872static struct printk_log *log_from_idx(u32 idx) { return NULL; }
Kay Sievers7f3a7812012-05-09 01:37:51 +02001873static u32 log_next(u32 idx) { return 0; }
Kay Sievers7f3a7812012-05-09 01:37:51 +02001874static void call_console_drivers(int level, const char *text, size_t len) {}
Joe Perches62e32ac2013-07-31 13:53:47 -07001875static size_t msg_print_text(const struct printk_log *msg, enum log_flags prev,
Kay Sievers5becfb12012-07-09 12:15:42 -07001876 bool syslog, char *buf, size_t size) { return 0; }
Kay Sievers084681d2012-06-28 09:38:53 +02001877static size_t cont_print_text(char *text, size_t size) { return 0; }
Matt Mackalld59745c2005-05-01 08:59:02 -07001878
Kay Sievers7f3a7812012-05-09 01:37:51 +02001879#endif /* CONFIG_PRINTK */
Matt Mackalld59745c2005-05-01 08:59:02 -07001880
Thomas Gleixnerd0380e62013-04-29 16:17:18 -07001881#ifdef CONFIG_EARLY_PRINTK
1882struct console *early_console;
1883
1884void early_vprintk(const char *fmt, va_list ap)
1885{
1886 if (early_console) {
1887 char buf[512];
1888 int n = vscnprintf(buf, sizeof(buf), fmt, ap);
1889
1890 early_console->write(early_console, buf, n);
1891 }
1892}
1893
Andi Kleen722a9f92014-05-02 00:44:38 +02001894asmlinkage __visible void early_printk(const char *fmt, ...)
Thomas Gleixnerd0380e62013-04-29 16:17:18 -07001895{
1896 va_list ap;
1897
1898 va_start(ap, fmt);
1899 early_vprintk(fmt, ap);
1900 va_end(ap);
1901}
1902#endif
1903
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001904static int __add_preferred_console(char *name, int idx, char *options,
1905 char *brl_options)
1906{
1907 struct console_cmdline *c;
1908 int i;
1909
1910 /*
1911 * See if this tty is not yet registered, and
1912 * if we have a slot free.
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 if (!brl_options)
1919 selected_console = i;
1920 return 0;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001921 }
Joe Perches23475402013-07-31 13:53:46 -07001922 }
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001923 if (i == MAX_CMDLINECONSOLES)
1924 return -E2BIG;
1925 if (!brl_options)
1926 selected_console = i;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001927 strlcpy(c->name, name, sizeof(c->name));
1928 c->options = options;
Joe Perchesbbeddf52013-07-31 13:53:45 -07001929 braille_set_options(c, brl_options);
1930
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001931 c->index = idx;
1932 return 0;
1933}
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001934/*
Alex Elder0b90fec2014-08-06 16:09:03 -07001935 * Set up a console. Called via do_early_param() in init/main.c
1936 * for each "console=" parameter in the boot command line.
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001937 */
1938static int __init console_setup(char *str)
1939{
Alex Elder0b90fec2014-08-06 16:09:03 -07001940 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for "ttyS" */
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001941 char *s, *options, *brl_options = NULL;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001942 int idx;
1943
Joe Perchesbbeddf52013-07-31 13:53:45 -07001944 if (_braille_console_setup(&str, &brl_options))
1945 return 1;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001946
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001947 /*
1948 * Decode str into name, index, options.
1949 */
1950 if (str[0] >= '0' && str[0] <= '9') {
Yinghai Lueaa944a2007-07-15 23:37:27 -07001951 strcpy(buf, "ttyS");
1952 strncpy(buf + 4, str, sizeof(buf) - 5);
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001953 } else {
Yinghai Lueaa944a2007-07-15 23:37:27 -07001954 strncpy(buf, str, sizeof(buf) - 1);
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001955 }
Yinghai Lueaa944a2007-07-15 23:37:27 -07001956 buf[sizeof(buf) - 1] = 0;
Alex Elder249771b2014-08-06 16:09:08 -07001957 options = strchr(str, ',');
1958 if (options)
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001959 *(options++) = 0;
1960#ifdef __sparc__
1961 if (!strcmp(str, "ttya"))
Yinghai Lueaa944a2007-07-15 23:37:27 -07001962 strcpy(buf, "ttyS0");
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001963 if (!strcmp(str, "ttyb"))
Yinghai Lueaa944a2007-07-15 23:37:27 -07001964 strcpy(buf, "ttyS1");
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001965#endif
Yinghai Lueaa944a2007-07-15 23:37:27 -07001966 for (s = buf; *s; s++)
Alex Elder249771b2014-08-06 16:09:08 -07001967 if (isdigit(*s) || *s == ',')
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001968 break;
1969 idx = simple_strtoul(s, NULL, 10);
1970 *s = 0;
1971
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001972 __add_preferred_console(buf, idx, options, brl_options);
Markus Armbruster9e124fe2008-05-26 23:31:07 +01001973 console_set_on_cmdline = 1;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001974 return 1;
1975}
1976__setup("console=", console_setup);
1977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978/**
Matt Mackall3c0547b2005-05-16 21:53:47 -07001979 * add_preferred_console - add a device to the list of preferred consoles.
Martin Waitzddad86c2005-11-13 16:08:14 -08001980 * @name: device name
1981 * @idx: device index
1982 * @options: options for this console
Matt Mackall3c0547b2005-05-16 21:53:47 -07001983 *
1984 * The last preferred console added will be used for kernel messages
1985 * and stdin/out/err for init. Normally this is used by console_setup
1986 * above to handle user-supplied console arguments; however it can also
1987 * be used by arch-specific code either to override the user or more
1988 * commonly to provide a default console (ie from PROM variables) when
1989 * the user has not supplied one.
1990 */
David S. Millerfb445ee2007-12-29 01:19:49 -08001991int add_preferred_console(char *name, int idx, char *options)
Matt Mackall3c0547b2005-05-16 21:53:47 -07001992{
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001993 return __add_preferred_console(name, idx, options, NULL);
Matt Mackall3c0547b2005-05-16 21:53:47 -07001994}
1995
Daniel Ritzb6b1d872007-08-03 16:07:43 +02001996int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
Yinghai Lu18a8bd92007-07-15 23:37:59 -07001997{
1998 struct console_cmdline *c;
1999 int i;
2000
Joe Perches23475402013-07-31 13:53:46 -07002001 for (i = 0, c = console_cmdline;
2002 i < MAX_CMDLINECONSOLES && c->name[0];
2003 i++, c++)
2004 if (strcmp(c->name, name) == 0 && c->index == idx) {
2005 strlcpy(c->name, name_new, sizeof(c->name));
Joe Perches23475402013-07-31 13:53:46 -07002006 c->options = options;
2007 c->index = idx_new;
2008 return i;
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002009 }
2010 /* not found */
2011 return -1;
2012}
2013
Neil Zhangd25d9fe2014-08-06 16:09:12 -07002014bool console_suspend_enabled = true;
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002015EXPORT_SYMBOL(console_suspend_enabled);
2016
2017static int __init console_suspend_disable(char *str)
2018{
Neil Zhangd25d9fe2014-08-06 16:09:12 -07002019 console_suspend_enabled = false;
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002020 return 1;
2021}
2022__setup("no_console_suspend", console_suspend_disable);
Yanmin Zhang134620f2011-10-31 17:11:27 -07002023module_param_named(console_suspend, console_suspend_enabled,
2024 bool, S_IRUGO | S_IWUSR);
2025MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
2026 " and hibernate operations");
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002027
Matt Mackall3c0547b2005-05-16 21:53:47 -07002028/**
Linus Torvalds557240b2006-06-19 18:16:01 -07002029 * suspend_console - suspend the console subsystem
2030 *
2031 * This disables printk() while we go into suspend states
2032 */
2033void suspend_console(void)
2034{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002035 if (!console_suspend_enabled)
2036 return;
Pavel Machek0d630812008-07-23 21:28:32 -07002037 printk("Suspending console(s) (use no_console_suspend to debug)\n");
Torben Hohnac751ef2011-01-25 15:07:35 -08002038 console_lock();
Linus Torvalds557240b2006-06-19 18:16:01 -07002039 console_suspended = 1;
Jan Karabd8d7cf2014-06-04 16:11:36 -07002040 up_console_sem();
Linus Torvalds557240b2006-06-19 18:16:01 -07002041}
2042
2043void resume_console(void)
2044{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002045 if (!console_suspend_enabled)
2046 return;
Jan Karabd8d7cf2014-06-04 16:11:36 -07002047 down_console_sem();
Linus Torvalds557240b2006-06-19 18:16:01 -07002048 console_suspended = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08002049 console_unlock();
Linus Torvalds557240b2006-06-19 18:16:01 -07002050}
2051
2052/**
Kevin Cernekee034260d2010-06-03 22:11:25 -07002053 * console_cpu_notify - print deferred console messages after CPU hotplug
2054 * @self: notifier struct
2055 * @action: CPU hotplug event
2056 * @hcpu: unused
2057 *
2058 * If printk() is called from a CPU that is not online yet, the messages
2059 * will be spooled but will not show up on the console. This function is
2060 * called when a new CPU comes online (or fails to come up), and ensures
2061 * that any such output gets printed.
2062 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04002063static int console_cpu_notify(struct notifier_block *self,
Kevin Cernekee034260d2010-06-03 22:11:25 -07002064 unsigned long action, void *hcpu)
2065{
2066 switch (action) {
2067 case CPU_ONLINE:
2068 case CPU_DEAD:
Kevin Cernekee034260d2010-06-03 22:11:25 -07002069 case CPU_DOWN_FAILED:
2070 case CPU_UP_CANCELED:
Torben Hohnac751ef2011-01-25 15:07:35 -08002071 console_lock();
2072 console_unlock();
Kevin Cernekee034260d2010-06-03 22:11:25 -07002073 }
2074 return NOTIFY_OK;
2075}
2076
2077/**
Torben Hohnac751ef2011-01-25 15:07:35 -08002078 * console_lock - lock the console system for exclusive use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002080 * Acquires a lock which guarantees that the caller has
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 * exclusive access to the console system and the console_drivers list.
2082 *
2083 * Can sleep, returns nothing.
2084 */
Torben Hohnac751ef2011-01-25 15:07:35 -08002085void console_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
Daniel Vetter6b898c02012-09-17 23:03:31 +00002087 might_sleep();
2088
Jan Karabd8d7cf2014-06-04 16:11:36 -07002089 down_console_sem();
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01002090 if (console_suspended)
2091 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 console_locked = 1;
2093 console_may_schedule = 1;
2094}
Torben Hohnac751ef2011-01-25 15:07:35 -08002095EXPORT_SYMBOL(console_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Torben Hohnac751ef2011-01-25 15:07:35 -08002097/**
2098 * console_trylock - try to lock the console system for exclusive use.
2099 *
Alex Elder0b90fec2014-08-06 16:09:03 -07002100 * Try to acquire a lock which guarantees that the caller has exclusive
2101 * access to the console system and the console_drivers list.
Torben Hohnac751ef2011-01-25 15:07:35 -08002102 *
2103 * returns 1 on success, and 0 on failure to acquire the lock.
2104 */
2105int console_trylock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106{
Jan Karabd8d7cf2014-06-04 16:11:36 -07002107 if (down_trylock_console_sem())
Torben Hohnac751ef2011-01-25 15:07:35 -08002108 return 0;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01002109 if (console_suspended) {
Jan Karabd8d7cf2014-06-04 16:11:36 -07002110 up_console_sem();
Torben Hohnac751ef2011-01-25 15:07:35 -08002111 return 0;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01002112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 console_locked = 1;
2114 console_may_schedule = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08002115 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116}
Torben Hohnac751ef2011-01-25 15:07:35 -08002117EXPORT_SYMBOL(console_trylock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
2119int is_console_locked(void)
2120{
2121 return console_locked;
2122}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Kay Sieverseab07262012-07-16 18:35:30 -07002124static void console_cont_flush(char *text, size_t size)
2125{
2126 unsigned long flags;
2127 size_t len;
2128
2129 raw_spin_lock_irqsave(&logbuf_lock, flags);
2130
2131 if (!cont.len)
2132 goto out;
2133
2134 /*
2135 * We still queue earlier records, likely because the console was
2136 * busy. The earlier ones need to be printed before this one, we
2137 * did not flush any fragment so far, so just let it queue up.
2138 */
2139 if (console_seq < log_next_seq && !cont.cons)
2140 goto out;
2141
2142 len = cont_print_text(text, size);
2143 raw_spin_unlock(&logbuf_lock);
2144 stop_critical_timings();
2145 call_console_drivers(cont.level, text, len);
2146 start_critical_timings();
2147 local_irq_restore(flags);
2148 return;
2149out:
2150 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2151}
Kay Sievers7ff95542012-05-03 02:29:13 +02002152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153/**
Torben Hohnac751ef2011-01-25 15:07:35 -08002154 * console_unlock - unlock the console system
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002156 * Releases the console_lock which the caller holds on the console system
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 * and the console driver list.
2158 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002159 * While the console_lock was held, console output may have been buffered
2160 * by printk(). If this is the case, console_unlock(); emits
2161 * the output prior to releasing the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 *
Kay Sievers7f3a7812012-05-09 01:37:51 +02002163 * If there is output waiting, we wake /dev/kmsg and syslog() users.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002165 * console_unlock(); may be called from any context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 */
Torben Hohnac751ef2011-01-25 15:07:35 -08002167void console_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
Kay Sievers70498252012-07-16 18:35:29 -07002169 static char text[LOG_LINE_MAX + PREFIX_MAX];
Kay Sievers7ff95542012-05-03 02:29:13 +02002170 static u64 seen_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 unsigned long flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02002172 bool wake_klogd = false;
2173 bool retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Linus Torvalds557240b2006-06-19 18:16:01 -07002175 if (console_suspended) {
Jan Karabd8d7cf2014-06-04 16:11:36 -07002176 up_console_sem();
Linus Torvalds557240b2006-06-19 18:16:01 -07002177 return;
2178 }
Antonino A. Daplas78944e542006-08-05 12:14:16 -07002179
2180 console_may_schedule = 0;
2181
Kay Sievers084681d2012-06-28 09:38:53 +02002182 /* flush buffered message fragment immediately to console */
Kay Sieverseab07262012-07-16 18:35:30 -07002183 console_cont_flush(text, sizeof(text));
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002184again:
Kay Sievers7ff95542012-05-03 02:29:13 +02002185 for (;;) {
Joe Perches62e32ac2013-07-31 13:53:47 -07002186 struct printk_log *msg;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02002187 size_t len;
Kay Sievers7ff95542012-05-03 02:29:13 +02002188 int level;
2189
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002190 raw_spin_lock_irqsave(&logbuf_lock, flags);
Kay Sievers7ff95542012-05-03 02:29:13 +02002191 if (seen_seq != log_next_seq) {
2192 wake_klogd = true;
2193 seen_seq = log_next_seq;
2194 }
2195
2196 if (console_seq < log_first_seq) {
Will Deacon84b5ec82014-06-04 16:11:45 -07002197 len = sprintf(text, "** %u printk messages dropped ** ",
2198 (unsigned)(log_first_seq - console_seq));
2199
Kay Sievers7ff95542012-05-03 02:29:13 +02002200 /* messages are gone, move to first one */
2201 console_seq = log_first_seq;
2202 console_idx = log_first_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002203 console_prev = 0;
Will Deacon84b5ec82014-06-04 16:11:45 -07002204 } else {
2205 len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02002206 }
Kay Sievers084681d2012-06-28 09:38:53 +02002207skip:
Kay Sievers7ff95542012-05-03 02:29:13 +02002208 if (console_seq == log_next_seq)
2209 break;
2210
2211 msg = log_from_idx(console_idx);
Kay Sievers084681d2012-06-28 09:38:53 +02002212 if (msg->flags & LOG_NOCONS) {
2213 /*
2214 * Skip record we have buffered and already printed
2215 * directly to the console when we received it.
2216 */
2217 console_idx = log_next(console_idx);
2218 console_seq++;
Kay Sievers68b65072012-07-06 09:50:09 -07002219 /*
2220 * We will get here again when we register a new
2221 * CON_PRINTBUFFER console. Clear the flag so we
2222 * will properly dump everything later.
2223 */
2224 msg->flags &= ~LOG_NOCONS;
Kay Sieverseab07262012-07-16 18:35:30 -07002225 console_prev = msg->flags;
Kay Sievers084681d2012-06-28 09:38:53 +02002226 goto skip;
2227 }
Kay Sievers649e6ee2012-05-10 04:30:45 +02002228
Kay Sievers084681d2012-06-28 09:38:53 +02002229 level = msg->level;
Will Deacon84b5ec82014-06-04 16:11:45 -07002230 len += msg_print_text(msg, console_prev, false,
2231 text + len, sizeof(text) - len);
Kay Sievers7ff95542012-05-03 02:29:13 +02002232 console_idx = log_next(console_idx);
2233 console_seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07002234 console_prev = msg->flags;
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002235 raw_spin_unlock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02002236
Steven Rostedt81d68a92008-05-12 21:20:42 +02002237 stop_critical_timings(); /* don't trace print latency */
Kay Sievers7ff95542012-05-03 02:29:13 +02002238 call_console_drivers(level, text, len);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002239 start_critical_timings();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 local_irq_restore(flags);
2241 }
2242 console_locked = 0;
Feng Tangfe3d8ad2011-03-22 16:34:21 -07002243
2244 /* Release the exclusive_console once it is used */
2245 if (unlikely(exclusive_console))
2246 exclusive_console = NULL;
2247
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002248 raw_spin_unlock(&logbuf_lock);
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002249
Jan Karabd8d7cf2014-06-04 16:11:36 -07002250 up_console_sem();
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002251
2252 /*
2253 * Someone could have filled up the buffer again, so re-check if there's
2254 * something to flush. In case we cannot trylock the console_sem again,
2255 * there's a new owner and the console_unlock() from them will do the
2256 * flush, no worries.
2257 */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002258 raw_spin_lock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02002259 retry = console_seq != log_next_seq;
Peter Zijlstra09dc3cf2011-12-08 14:34:13 -08002260 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2261
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002262 if (retry && console_trylock())
2263 goto again;
2264
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08002265 if (wake_klogd)
2266 wake_up_klogd();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267}
Torben Hohnac751ef2011-01-25 15:07:35 -08002268EXPORT_SYMBOL(console_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Martin Waitzddad86c2005-11-13 16:08:14 -08002270/**
2271 * console_conditional_schedule - yield the CPU if required
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 *
2273 * If the console code is currently allowed to sleep, and
2274 * if this CPU should yield the CPU to another task, do
2275 * so here.
2276 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002277 * Must be called within console_lock();.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 */
2279void __sched console_conditional_schedule(void)
2280{
2281 if (console_may_schedule)
2282 cond_resched();
2283}
2284EXPORT_SYMBOL(console_conditional_schedule);
2285
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286void console_unblank(void)
2287{
2288 struct console *c;
2289
2290 /*
2291 * console_unblank can no longer be called in interrupt context unless
2292 * oops_in_progress is set to 1..
2293 */
2294 if (oops_in_progress) {
Jan Karabd8d7cf2014-06-04 16:11:36 -07002295 if (down_trylock_console_sem() != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 return;
2297 } else
Torben Hohnac751ef2011-01-25 15:07:35 -08002298 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300 console_locked = 1;
2301 console_may_schedule = 0;
Robin Getz4d091612009-07-01 21:08:37 -04002302 for_each_console(c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 if ((c->flags & CON_ENABLED) && c->unblank)
2304 c->unblank();
Torben Hohnac751ef2011-01-25 15:07:35 -08002305 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
2308/*
2309 * Return the console tty driver structure and its associated index
2310 */
2311struct tty_driver *console_device(int *index)
2312{
2313 struct console *c;
2314 struct tty_driver *driver = NULL;
2315
Torben Hohnac751ef2011-01-25 15:07:35 -08002316 console_lock();
Robin Getz4d091612009-07-01 21:08:37 -04002317 for_each_console(c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 if (!c->device)
2319 continue;
2320 driver = c->device(c, index);
2321 if (driver)
2322 break;
2323 }
Torben Hohnac751ef2011-01-25 15:07:35 -08002324 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 return driver;
2326}
2327
2328/*
2329 * Prevent further output on the passed console device so that (for example)
2330 * serial drivers can disable console output before suspending a port, and can
2331 * re-enable output afterwards.
2332 */
2333void console_stop(struct console *console)
2334{
Torben Hohnac751ef2011-01-25 15:07:35 -08002335 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 console->flags &= ~CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08002337 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338}
2339EXPORT_SYMBOL(console_stop);
2340
2341void console_start(struct console *console)
2342{
Torben Hohnac751ef2011-01-25 15:07:35 -08002343 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 console->flags |= CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08002345 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346}
2347EXPORT_SYMBOL(console_start);
2348
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07002349static int __read_mostly keep_bootcon;
2350
2351static int __init keep_bootcon_setup(char *str)
2352{
2353 keep_bootcon = 1;
Andrew Morton27083ba2013-11-12 15:08:50 -08002354 pr_info("debug: skip boot console de-registration.\n");
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07002355
2356 return 0;
2357}
2358
2359early_param("keep_bootcon", keep_bootcon_setup);
2360
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361/*
2362 * The console driver calls this routine during kernel initialization
2363 * to register the console printing procedure with printk() and to
2364 * print any messages that were printed by the kernel before the
2365 * console driver was initialized.
Robin Getz4d091612009-07-01 21:08:37 -04002366 *
2367 * This can happen pretty early during the boot process (because of
2368 * early_printk) - sometimes before setup_arch() completes - be careful
2369 * of what kernel features are used - they may not be initialised yet.
2370 *
2371 * There are two types of consoles - bootconsoles (early_printk) and
2372 * "real" consoles (everything which is not a bootconsole) which are
2373 * handled differently.
2374 * - Any number of bootconsoles can be registered at any time.
2375 * - As soon as a "real" console is registered, all bootconsoles
2376 * will be unregistered automatically.
2377 * - Once a "real" console is registered, any attempt to register a
2378 * bootconsoles will be rejected
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 */
Robin Getz4d091612009-07-01 21:08:37 -04002380void register_console(struct console *newcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381{
Jesper Juhl40dc5652005-10-30 15:02:46 -08002382 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 unsigned long flags;
Robin Getz4d091612009-07-01 21:08:37 -04002384 struct console *bcon = NULL;
Joe Perches23475402013-07-31 13:53:46 -07002385 struct console_cmdline *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Andreas Bießmann16cf48a2013-08-02 12:23:34 +02002387 if (console_drivers)
2388 for_each_console(bcon)
2389 if (WARN(bcon == newcon,
2390 "console '%s%d' already registered\n",
2391 bcon->name, bcon->index))
2392 return;
2393
Robin Getz4d091612009-07-01 21:08:37 -04002394 /*
2395 * before we register a new CON_BOOT console, make sure we don't
2396 * already have a valid console
2397 */
2398 if (console_drivers && newcon->flags & CON_BOOT) {
2399 /* find the last or real console */
2400 for_each_console(bcon) {
2401 if (!(bcon->flags & CON_BOOT)) {
Andrew Morton27083ba2013-11-12 15:08:50 -08002402 pr_info("Too late to register bootconsole %s%d\n",
Robin Getz4d091612009-07-01 21:08:37 -04002403 newcon->name, newcon->index);
2404 return;
2405 }
2406 }
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002407 }
2408
Robin Getz4d091612009-07-01 21:08:37 -04002409 if (console_drivers && console_drivers->flags & CON_BOOT)
2410 bcon = console_drivers;
2411
2412 if (preferred_console < 0 || bcon || !console_drivers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 preferred_console = selected_console;
2414
Robin Getz4d091612009-07-01 21:08:37 -04002415 if (newcon->early_setup)
2416 newcon->early_setup();
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 /*
2419 * See if we want to use this console driver. If we
2420 * didn't select a console we take the first one
2421 * that registers here.
2422 */
2423 if (preferred_console < 0) {
Robin Getz4d091612009-07-01 21:08:37 -04002424 if (newcon->index < 0)
2425 newcon->index = 0;
2426 if (newcon->setup == NULL ||
2427 newcon->setup(newcon, NULL) == 0) {
2428 newcon->flags |= CON_ENABLED;
2429 if (newcon->device) {
2430 newcon->flags |= CON_CONSDEV;
Jan Kiszkacd3a1b82008-05-12 21:21:04 +02002431 preferred_console = 0;
2432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 }
2434 }
2435
2436 /*
2437 * See if this console matches one we selected on
2438 * the command line.
2439 */
Joe Perches23475402013-07-31 13:53:46 -07002440 for (i = 0, c = console_cmdline;
2441 i < MAX_CMDLINECONSOLES && c->name[0];
2442 i++, c++) {
2443 if (strcmp(c->name, newcon->name) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 continue;
Robin Getz4d091612009-07-01 21:08:37 -04002445 if (newcon->index >= 0 &&
Joe Perches23475402013-07-31 13:53:46 -07002446 newcon->index != c->index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 continue;
Robin Getz4d091612009-07-01 21:08:37 -04002448 if (newcon->index < 0)
Joe Perches23475402013-07-31 13:53:46 -07002449 newcon->index = c->index;
Joe Perchesbbeddf52013-07-31 13:53:45 -07002450
Joe Perches23475402013-07-31 13:53:46 -07002451 if (_braille_register_console(newcon, c))
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002452 return;
Joe Perchesbbeddf52013-07-31 13:53:45 -07002453
Robin Getz4d091612009-07-01 21:08:37 -04002454 if (newcon->setup &&
2455 newcon->setup(newcon, console_cmdline[i].options) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 break;
Robin Getz4d091612009-07-01 21:08:37 -04002457 newcon->flags |= CON_ENABLED;
Joe Perches23475402013-07-31 13:53:46 -07002458 newcon->index = c->index;
Greg Edwardsab4af032005-06-23 00:09:05 -07002459 if (i == selected_console) {
Robin Getz4d091612009-07-01 21:08:37 -04002460 newcon->flags |= CON_CONSDEV;
Greg Edwardsab4af032005-06-23 00:09:05 -07002461 preferred_console = selected_console;
2462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 break;
2464 }
2465
Robin Getz4d091612009-07-01 21:08:37 -04002466 if (!(newcon->flags & CON_ENABLED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 return;
2468
Robin Getz8259cf42009-07-09 13:08:37 -04002469 /*
2470 * If we have a bootconsole, and are switching to a real console,
2471 * don't print everything out again, since when the boot console, and
2472 * the real console are the same physical device, it's annoying to
2473 * see the beginning boot messages twice
2474 */
2475 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
Robin Getz4d091612009-07-01 21:08:37 -04002476 newcon->flags &= ~CON_PRINTBUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
2478 /*
2479 * Put this console in the list - keep the
2480 * preferred driver at the head of the list.
2481 */
Torben Hohnac751ef2011-01-25 15:07:35 -08002482 console_lock();
Robin Getz4d091612009-07-01 21:08:37 -04002483 if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
2484 newcon->next = console_drivers;
2485 console_drivers = newcon;
2486 if (newcon->next)
2487 newcon->next->flags &= ~CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 } else {
Robin Getz4d091612009-07-01 21:08:37 -04002489 newcon->next = console_drivers->next;
2490 console_drivers->next = newcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 }
Robin Getz4d091612009-07-01 21:08:37 -04002492 if (newcon->flags & CON_PRINTBUFFER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 /*
Torben Hohnac751ef2011-01-25 15:07:35 -08002494 * console_unlock(); will print out the buffered messages
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 * for us.
2496 */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002497 raw_spin_lock_irqsave(&logbuf_lock, flags);
Kay Sievers7ff95542012-05-03 02:29:13 +02002498 console_seq = syslog_seq;
2499 console_idx = syslog_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002500 console_prev = syslog_prev;
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002501 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
Feng Tangfe3d8ad2011-03-22 16:34:21 -07002502 /*
2503 * We're about to replay the log buffer. Only do this to the
2504 * just-registered console to avoid excessive message spam to
2505 * the already-registered consoles.
2506 */
2507 exclusive_console = newcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 }
Torben Hohnac751ef2011-01-25 15:07:35 -08002509 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01002510 console_sysfs_notify();
Robin Getz8259cf42009-07-09 13:08:37 -04002511
2512 /*
2513 * By unregistering the bootconsoles after we enable the real console
2514 * we get the "console xxx enabled" message on all the consoles -
2515 * boot consoles, real consoles, etc - this is to ensure that end
2516 * users know there might be something in the kernel's log buffer that
2517 * went to the bootconsole (that they do not see on the real console)
2518 */
Andrew Morton27083ba2013-11-12 15:08:50 -08002519 pr_info("%sconsole [%s%d] enabled\n",
Kees Cook6b802392013-11-12 15:08:49 -08002520 (newcon->flags & CON_BOOT) ? "boot" : "" ,
2521 newcon->name, newcon->index);
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07002522 if (bcon &&
2523 ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
2524 !keep_bootcon) {
Kees Cook6b802392013-11-12 15:08:49 -08002525 /* We need to iterate through all boot consoles, to make
2526 * sure we print everything out, before we unregister them.
Robin Getz8259cf42009-07-09 13:08:37 -04002527 */
Robin Getz8259cf42009-07-09 13:08:37 -04002528 for_each_console(bcon)
2529 if (bcon->flags & CON_BOOT)
2530 unregister_console(bcon);
Robin Getz8259cf42009-07-09 13:08:37 -04002531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532}
2533EXPORT_SYMBOL(register_console);
2534
Jesper Juhl40dc5652005-10-30 15:02:46 -08002535int unregister_console(struct console *console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536{
Jesper Juhl40dc5652005-10-30 15:02:46 -08002537 struct console *a, *b;
Joe Perchesbbeddf52013-07-31 13:53:45 -07002538 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
Andrew Morton27083ba2013-11-12 15:08:50 -08002540 pr_info("%sconsole [%s%d] disabled\n",
Kees Cook6b802392013-11-12 15:08:49 -08002541 (console->flags & CON_BOOT) ? "boot" : "" ,
2542 console->name, console->index);
2543
Joe Perchesbbeddf52013-07-31 13:53:45 -07002544 res = _braille_unregister_console(console);
2545 if (res)
2546 return res;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002547
Joe Perchesbbeddf52013-07-31 13:53:45 -07002548 res = 1;
Torben Hohnac751ef2011-01-25 15:07:35 -08002549 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 if (console_drivers == console) {
2551 console_drivers=console->next;
2552 res = 0;
Benjamin Herrenschmidte9b15b52005-11-23 13:37:44 -08002553 } else if (console_drivers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 for (a=console_drivers->next, b=console_drivers ;
2555 a; b=a, a=b->next) {
2556 if (a == console) {
2557 b->next = a->next;
2558 res = 0;
2559 break;
Jesper Juhl40dc5652005-10-30 15:02:46 -08002560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 }
2562 }
Jesper Juhl40dc5652005-10-30 15:02:46 -08002563
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002564 /*
Greg Edwardsab4af032005-06-23 00:09:05 -07002565 * If this isn't the last console and it has CON_CONSDEV set, we
2566 * need to set it on the next preferred console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 */
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002568 if (console_drivers != NULL && console->flags & CON_CONSDEV)
Greg Edwardsab4af032005-06-23 00:09:05 -07002569 console_drivers->flags |= CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Stephen Chivers7fa21dd2014-05-14 08:04:39 +10002571 console->flags &= ~CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08002572 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01002573 console_sysfs_notify();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 return res;
2575}
2576EXPORT_SYMBOL(unregister_console);
Matt Mackalld59745c2005-05-01 08:59:02 -07002577
Kevin Cernekee034260d2010-06-03 22:11:25 -07002578static int __init printk_late_init(void)
Robin Getz0c5564b2007-08-20 15:22:47 -04002579{
Robin Getz4d091612009-07-01 21:08:37 -04002580 struct console *con;
2581
2582 for_each_console(con) {
Nishanth Aravamudan4c30c6f2011-08-25 15:59:11 -07002583 if (!keep_bootcon && con->flags & CON_BOOT) {
Sonic Zhang42c2c8c2009-08-06 15:58:11 -07002584 unregister_console(con);
Robin Getzcb00e992007-08-21 23:14:58 -04002585 }
Robin Getz0c5564b2007-08-20 15:22:47 -04002586 }
Kevin Cernekee034260d2010-06-03 22:11:25 -07002587 hotcpu_notifier(console_cpu_notify, 0);
Robin Getz0c5564b2007-08-20 15:22:47 -04002588 return 0;
2589}
Kevin Cernekee034260d2010-06-03 22:11:25 -07002590late_initcall(printk_late_init);
Robin Getz0c5564b2007-08-20 15:22:47 -04002591
Joe Perches7ef3d2f2008-02-08 04:21:25 -08002592#if defined CONFIG_PRINTK
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002593/*
2594 * Delayed printk version, for scheduler-internal messages:
2595 */
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002596#define PRINTK_PENDING_WAKEUP 0x01
Steven Rostedt458df9f2014-06-04 16:11:38 -07002597#define PRINTK_PENDING_OUTPUT 0x02
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002598
2599static DEFINE_PER_CPU(int, printk_pending);
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002600
2601static void wake_up_klogd_work_func(struct irq_work *irq_work)
2602{
2603 int pending = __this_cpu_xchg(printk_pending, 0);
2604
Steven Rostedt458df9f2014-06-04 16:11:38 -07002605 if (pending & PRINTK_PENDING_OUTPUT) {
2606 /* If trylock fails, someone else is doing the printing */
2607 if (console_trylock())
2608 console_unlock();
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002609 }
2610
2611 if (pending & PRINTK_PENDING_WAKEUP)
2612 wake_up_interruptible(&log_wait);
2613}
2614
2615static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) = {
2616 .func = wake_up_klogd_work_func,
2617 .flags = IRQ_WORK_LAZY,
2618};
2619
2620void wake_up_klogd(void)
2621{
2622 preempt_disable();
2623 if (waitqueue_active(&log_wait)) {
2624 this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP);
2625 irq_work_queue(&__get_cpu_var(wake_up_klogd_work));
2626 }
2627 preempt_enable();
2628}
Dave Young717115e2008-07-25 01:45:58 -07002629
John Stultzaac74dc2014-06-04 16:11:40 -07002630int printk_deferred(const char *fmt, ...)
Peter Zijlstra600e1452012-03-15 12:35:37 +01002631{
Peter Zijlstra600e1452012-03-15 12:35:37 +01002632 va_list args;
Peter Zijlstra600e1452012-03-15 12:35:37 +01002633 int r;
2634
John Stultz81954602014-06-04 16:11:39 -07002635 preempt_disable();
Peter Zijlstra600e1452012-03-15 12:35:37 +01002636 va_start(args, fmt);
Steven Rostedt458df9f2014-06-04 16:11:38 -07002637 r = vprintk_emit(0, SCHED_MESSAGE_LOGLEVEL, NULL, 0, fmt, args);
Peter Zijlstra600e1452012-03-15 12:35:37 +01002638 va_end(args);
2639
Steven Rostedt458df9f2014-06-04 16:11:38 -07002640 __this_cpu_or(printk_pending, PRINTK_PENDING_OUTPUT);
Frederic Weisbecker74876a92012-10-12 18:00:23 +02002641 irq_work_queue(&__get_cpu_var(wake_up_klogd_work));
John Stultz81954602014-06-04 16:11:39 -07002642 preempt_enable();
Peter Zijlstra600e1452012-03-15 12:35:37 +01002643
2644 return r;
2645}
2646
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647/*
2648 * printk rate limiting, lifted from the networking subsystem.
2649 *
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07002650 * This enforces a rate limit: not more than 10 kernel messages
2651 * every 5s to make a denial-of-service attack impossible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 */
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07002653DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
2654
Christian Borntraeger5c828712009-10-23 14:58:11 +02002655int __printk_ratelimit(const char *func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656{
Christian Borntraeger5c828712009-10-23 14:58:11 +02002657 return ___ratelimit(&printk_ratelimit_state, func);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}
Christian Borntraeger5c828712009-10-23 14:58:11 +02002659EXPORT_SYMBOL(__printk_ratelimit);
Andrew Mortonf46c4832006-11-02 22:07:16 -08002660
2661/**
2662 * printk_timed_ratelimit - caller-controlled printk ratelimiting
2663 * @caller_jiffies: pointer to caller's state
2664 * @interval_msecs: minimum interval between prints
2665 *
2666 * printk_timed_ratelimit() returns true if more than @interval_msecs
2667 * milliseconds have elapsed since the last time printk_timed_ratelimit()
2668 * returned true.
2669 */
2670bool printk_timed_ratelimit(unsigned long *caller_jiffies,
2671 unsigned int interval_msecs)
2672{
Alex Elder249771b2014-08-06 16:09:08 -07002673 unsigned long elapsed = jiffies - *caller_jiffies;
2674
2675 if (*caller_jiffies && elapsed <= msecs_to_jiffies(interval_msecs))
2676 return false;
2677
2678 *caller_jiffies = jiffies;
2679 return true;
Andrew Mortonf46c4832006-11-02 22:07:16 -08002680}
2681EXPORT_SYMBOL(printk_timed_ratelimit);
Simon Kagstrom456b5652009-10-16 14:09:18 +02002682
2683static DEFINE_SPINLOCK(dump_list_lock);
2684static LIST_HEAD(dump_list);
2685
2686/**
2687 * kmsg_dump_register - register a kernel log dumper.
Randy Dunlap64855362009-12-17 15:27:27 -08002688 * @dumper: pointer to the kmsg_dumper structure
Simon Kagstrom456b5652009-10-16 14:09:18 +02002689 *
2690 * Adds a kernel log dumper to the system. The dump callback in the
2691 * structure will be called when the kernel oopses or panics and must be
2692 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
2693 */
2694int kmsg_dump_register(struct kmsg_dumper *dumper)
2695{
2696 unsigned long flags;
2697 int err = -EBUSY;
2698
2699 /* The dump callback needs to be set */
2700 if (!dumper->dump)
2701 return -EINVAL;
2702
2703 spin_lock_irqsave(&dump_list_lock, flags);
2704 /* Don't allow registering multiple times */
2705 if (!dumper->registered) {
2706 dumper->registered = 1;
Huang Yingfb842b002011-01-12 16:59:43 -08002707 list_add_tail_rcu(&dumper->list, &dump_list);
Simon Kagstrom456b5652009-10-16 14:09:18 +02002708 err = 0;
2709 }
2710 spin_unlock_irqrestore(&dump_list_lock, flags);
2711
2712 return err;
2713}
2714EXPORT_SYMBOL_GPL(kmsg_dump_register);
2715
2716/**
2717 * kmsg_dump_unregister - unregister a kmsg dumper.
Randy Dunlap64855362009-12-17 15:27:27 -08002718 * @dumper: pointer to the kmsg_dumper structure
Simon Kagstrom456b5652009-10-16 14:09:18 +02002719 *
2720 * Removes a dump device from the system. Returns zero on success and
2721 * %-EINVAL otherwise.
2722 */
2723int kmsg_dump_unregister(struct kmsg_dumper *dumper)
2724{
2725 unsigned long flags;
2726 int err = -EINVAL;
2727
2728 spin_lock_irqsave(&dump_list_lock, flags);
2729 if (dumper->registered) {
2730 dumper->registered = 0;
Huang Yingfb842b002011-01-12 16:59:43 -08002731 list_del_rcu(&dumper->list);
Simon Kagstrom456b5652009-10-16 14:09:18 +02002732 err = 0;
2733 }
2734 spin_unlock_irqrestore(&dump_list_lock, flags);
Huang Yingfb842b002011-01-12 16:59:43 -08002735 synchronize_rcu();
Simon Kagstrom456b5652009-10-16 14:09:18 +02002736
2737 return err;
2738}
2739EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
2740
Kay Sievers7ff95542012-05-03 02:29:13 +02002741static bool always_kmsg_dump;
2742module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR);
2743
Simon Kagstrom456b5652009-10-16 14:09:18 +02002744/**
2745 * kmsg_dump - dump kernel log to kernel message dumpers.
2746 * @reason: the reason (oops, panic etc) for dumping
2747 *
Kay Sieverse2ae7152012-06-15 14:07:51 +02002748 * Call each of the registered dumper's dump() callback, which can
2749 * retrieve the kmsg records with kmsg_dump_get_line() or
2750 * kmsg_dump_get_buffer().
Simon Kagstrom456b5652009-10-16 14:09:18 +02002751 */
2752void kmsg_dump(enum kmsg_dump_reason reason)
2753{
Simon Kagstrom456b5652009-10-16 14:09:18 +02002754 struct kmsg_dumper *dumper;
Simon Kagstrom456b5652009-10-16 14:09:18 +02002755 unsigned long flags;
2756
Matthew Garrettc22ab3322012-03-05 14:59:10 -08002757 if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
2758 return;
2759
Huang Yingfb842b002011-01-12 16:59:43 -08002760 rcu_read_lock();
Kay Sieverse2ae7152012-06-15 14:07:51 +02002761 list_for_each_entry_rcu(dumper, &dump_list, list) {
2762 if (dumper->max_reason && reason > dumper->max_reason)
2763 continue;
2764
2765 /* initialize iterator with data about the stored records */
2766 dumper->active = true;
2767
2768 raw_spin_lock_irqsave(&logbuf_lock, flags);
2769 dumper->cur_seq = clear_seq;
2770 dumper->cur_idx = clear_idx;
2771 dumper->next_seq = log_next_seq;
2772 dumper->next_idx = log_next_idx;
2773 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2774
2775 /* invoke dumper which will iterate over records */
2776 dumper->dump(dumper, reason);
2777
2778 /* reset iterator */
2779 dumper->active = false;
2780 }
Huang Yingfb842b002011-01-12 16:59:43 -08002781 rcu_read_unlock();
Simon Kagstrom456b5652009-10-16 14:09:18 +02002782}
Kay Sieverse2ae7152012-06-15 14:07:51 +02002783
2784/**
Anton Vorontsov533827c2012-07-20 17:28:07 -07002785 * kmsg_dump_get_line_nolock - retrieve one kmsg log line (unlocked version)
2786 * @dumper: registered kmsg dumper
2787 * @syslog: include the "<4>" prefixes
2788 * @line: buffer to copy the line to
2789 * @size: maximum size of the buffer
2790 * @len: length of line placed into buffer
2791 *
2792 * Start at the beginning of the kmsg buffer, with the oldest kmsg
2793 * record, and copy one record into the provided buffer.
2794 *
2795 * Consecutive calls will return the next available record moving
2796 * towards the end of the buffer with the youngest messages.
2797 *
2798 * A return value of FALSE indicates that there are no more records to
2799 * read.
2800 *
2801 * The function is similar to kmsg_dump_get_line(), but grabs no locks.
2802 */
2803bool kmsg_dump_get_line_nolock(struct kmsg_dumper *dumper, bool syslog,
2804 char *line, size_t size, size_t *len)
2805{
Joe Perches62e32ac2013-07-31 13:53:47 -07002806 struct printk_log *msg;
Anton Vorontsov533827c2012-07-20 17:28:07 -07002807 size_t l = 0;
2808 bool ret = false;
2809
2810 if (!dumper->active)
2811 goto out;
2812
2813 if (dumper->cur_seq < log_first_seq) {
2814 /* messages are gone, move to first available one */
2815 dumper->cur_seq = log_first_seq;
2816 dumper->cur_idx = log_first_idx;
2817 }
2818
2819 /* last entry */
2820 if (dumper->cur_seq >= log_next_seq)
2821 goto out;
2822
2823 msg = log_from_idx(dumper->cur_idx);
2824 l = msg_print_text(msg, 0, syslog, line, size);
2825
2826 dumper->cur_idx = log_next(dumper->cur_idx);
2827 dumper->cur_seq++;
2828 ret = true;
2829out:
2830 if (len)
2831 *len = l;
2832 return ret;
2833}
2834
2835/**
Kay Sieverse2ae7152012-06-15 14:07:51 +02002836 * kmsg_dump_get_line - retrieve one kmsg log line
2837 * @dumper: registered kmsg dumper
2838 * @syslog: include the "<4>" prefixes
2839 * @line: buffer to copy the line to
2840 * @size: maximum size of the buffer
2841 * @len: length of line placed into buffer
2842 *
2843 * Start at the beginning of the kmsg buffer, with the oldest kmsg
2844 * record, and copy one record into the provided buffer.
2845 *
2846 * Consecutive calls will return the next available record moving
2847 * towards the end of the buffer with the youngest messages.
2848 *
2849 * A return value of FALSE indicates that there are no more records to
2850 * read.
2851 */
2852bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog,
2853 char *line, size_t size, size_t *len)
2854{
2855 unsigned long flags;
Anton Vorontsov533827c2012-07-20 17:28:07 -07002856 bool ret;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002857
2858 raw_spin_lock_irqsave(&logbuf_lock, flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07002859 ret = kmsg_dump_get_line_nolock(dumper, syslog, line, size, len);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002860 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07002861
Kay Sieverse2ae7152012-06-15 14:07:51 +02002862 return ret;
2863}
2864EXPORT_SYMBOL_GPL(kmsg_dump_get_line);
2865
2866/**
2867 * kmsg_dump_get_buffer - copy kmsg log lines
2868 * @dumper: registered kmsg dumper
2869 * @syslog: include the "<4>" prefixes
Randy Dunlap4f0f4af2012-06-30 15:37:24 -07002870 * @buf: buffer to copy the line to
Kay Sieverse2ae7152012-06-15 14:07:51 +02002871 * @size: maximum size of the buffer
2872 * @len: length of line placed into buffer
2873 *
2874 * Start at the end of the kmsg buffer and fill the provided buffer
2875 * with as many of the the *youngest* kmsg records that fit into it.
2876 * If the buffer is large enough, all available kmsg records will be
2877 * copied with a single call.
2878 *
2879 * Consecutive calls will fill the buffer with the next block of
2880 * available older records, not including the earlier retrieved ones.
2881 *
2882 * A return value of FALSE indicates that there are no more records to
2883 * read.
2884 */
2885bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
2886 char *buf, size_t size, size_t *len)
2887{
2888 unsigned long flags;
2889 u64 seq;
2890 u32 idx;
2891 u64 next_seq;
2892 u32 next_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002893 enum log_flags prev;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002894 size_t l = 0;
2895 bool ret = false;
2896
2897 if (!dumper->active)
2898 goto out;
2899
2900 raw_spin_lock_irqsave(&logbuf_lock, flags);
2901 if (dumper->cur_seq < log_first_seq) {
2902 /* messages are gone, move to first available one */
2903 dumper->cur_seq = log_first_seq;
2904 dumper->cur_idx = log_first_idx;
2905 }
2906
2907 /* last entry */
2908 if (dumper->cur_seq >= dumper->next_seq) {
2909 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2910 goto out;
2911 }
2912
2913 /* calculate length of entire buffer */
2914 seq = dumper->cur_seq;
2915 idx = dumper->cur_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002916 prev = 0;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002917 while (seq < dumper->next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07002918 struct printk_log *msg = log_from_idx(idx);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002919
Kay Sievers5becfb12012-07-09 12:15:42 -07002920 l += msg_print_text(msg, prev, true, NULL, 0);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002921 idx = log_next(idx);
2922 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07002923 prev = msg->flags;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002924 }
2925
2926 /* move first record forward until length fits into the buffer */
2927 seq = dumper->cur_seq;
2928 idx = dumper->cur_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002929 prev = 0;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002930 while (l > size && seq < dumper->next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07002931 struct printk_log *msg = log_from_idx(idx);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002932
Kay Sievers5becfb12012-07-09 12:15:42 -07002933 l -= msg_print_text(msg, prev, true, NULL, 0);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002934 idx = log_next(idx);
2935 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07002936 prev = msg->flags;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002937 }
2938
2939 /* last message in next interation */
2940 next_seq = seq;
2941 next_idx = idx;
2942
2943 l = 0;
2944 while (seq < dumper->next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07002945 struct printk_log *msg = log_from_idx(idx);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002946
Kay Sievers5becfb12012-07-09 12:15:42 -07002947 l += msg_print_text(msg, prev, syslog, buf + l, size - l);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002948 idx = log_next(idx);
2949 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07002950 prev = msg->flags;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002951 }
2952
2953 dumper->next_seq = next_seq;
2954 dumper->next_idx = next_idx;
2955 ret = true;
2956 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2957out:
2958 if (len)
2959 *len = l;
2960 return ret;
2961}
2962EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer);
2963
2964/**
Anton Vorontsov533827c2012-07-20 17:28:07 -07002965 * kmsg_dump_rewind_nolock - reset the interator (unlocked version)
2966 * @dumper: registered kmsg dumper
2967 *
2968 * Reset the dumper's iterator so that kmsg_dump_get_line() and
2969 * kmsg_dump_get_buffer() can be called again and used multiple
2970 * times within the same dumper.dump() callback.
2971 *
2972 * The function is similar to kmsg_dump_rewind(), but grabs no locks.
2973 */
2974void kmsg_dump_rewind_nolock(struct kmsg_dumper *dumper)
2975{
2976 dumper->cur_seq = clear_seq;
2977 dumper->cur_idx = clear_idx;
2978 dumper->next_seq = log_next_seq;
2979 dumper->next_idx = log_next_idx;
2980}
2981
2982/**
Kay Sieverse2ae7152012-06-15 14:07:51 +02002983 * kmsg_dump_rewind - reset the interator
2984 * @dumper: registered kmsg dumper
2985 *
2986 * Reset the dumper's iterator so that kmsg_dump_get_line() and
2987 * kmsg_dump_get_buffer() can be called again and used multiple
2988 * times within the same dumper.dump() callback.
2989 */
2990void kmsg_dump_rewind(struct kmsg_dumper *dumper)
2991{
2992 unsigned long flags;
2993
2994 raw_spin_lock_irqsave(&logbuf_lock, flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07002995 kmsg_dump_rewind_nolock(dumper);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002996 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2997}
2998EXPORT_SYMBOL_GPL(kmsg_dump_rewind);
Tejun Heo196779b2013-04-30 15:27:12 -07002999
Tejun Heo98e5e1b2013-04-30 15:27:15 -07003000static char dump_stack_arch_desc_str[128];
3001
3002/**
3003 * dump_stack_set_arch_desc - set arch-specific str to show with task dumps
3004 * @fmt: printf-style format string
3005 * @...: arguments for the format string
3006 *
3007 * The configured string will be printed right after utsname during task
3008 * dumps. Usually used to add arch-specific system identifiers. If an
3009 * arch wants to make use of such an ID string, it should initialize this
3010 * as soon as possible during boot.
3011 */
3012void __init dump_stack_set_arch_desc(const char *fmt, ...)
3013{
3014 va_list args;
3015
3016 va_start(args, fmt);
3017 vsnprintf(dump_stack_arch_desc_str, sizeof(dump_stack_arch_desc_str),
3018 fmt, args);
3019 va_end(args);
3020}
3021
Tejun Heo196779b2013-04-30 15:27:12 -07003022/**
3023 * dump_stack_print_info - print generic debug info for dump_stack()
3024 * @log_lvl: log level
3025 *
3026 * Arch-specific dump_stack() implementations can use this function to
3027 * print out the same debug information as the generic dump_stack().
3028 */
3029void dump_stack_print_info(const char *log_lvl)
3030{
3031 printk("%sCPU: %d PID: %d Comm: %.20s %s %s %.*s\n",
3032 log_lvl, raw_smp_processor_id(), current->pid, current->comm,
3033 print_tainted(), init_utsname()->release,
3034 (int)strcspn(init_utsname()->version, " "),
3035 init_utsname()->version);
Tejun Heo98e5e1b2013-04-30 15:27:15 -07003036
3037 if (dump_stack_arch_desc_str[0] != '\0')
3038 printk("%sHardware name: %s\n",
3039 log_lvl, dump_stack_arch_desc_str);
Tejun Heo3d1cb202013-04-30 15:27:22 -07003040
3041 print_worker_info(log_lvl, current);
Tejun Heo196779b2013-04-30 15:27:12 -07003042}
3043
Tejun Heoa43cb952013-04-30 15:27:17 -07003044/**
3045 * show_regs_print_info - print generic debug info for show_regs()
3046 * @log_lvl: log level
3047 *
3048 * show_regs() implementations can use this function to print out generic
3049 * debug information.
3050 */
3051void show_regs_print_info(const char *log_lvl)
3052{
3053 dump_stack_print_info(log_lvl);
3054
3055 printk("%stask: %p ti: %p task.ti: %p\n",
3056 log_lvl, current, current_thread_info(),
3057 task_thread_info(current));
3058}
3059
Joe Perches7ef3d2f2008-02-08 04:21:25 -08003060#endif