blob: 4815c98ae175762eb1f9229b702107e9a0947f7c [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)
Stephen Warrenf8450fc2012-05-10 16:14:33 -0600270static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN);
Matt Mackalld59745c2005-05-01 08:59:02 -0700271static char *log_buf = __log_buf;
Kay Sievers7ff95542012-05-03 02:29:13 +0200272static u32 log_buf_len = __LOG_BUF_LEN;
273
Vasant Hegde14c40002014-08-09 11:15:30 +0530274/* Return log buffer address */
275char *log_buf_addr_get(void)
276{
277 return log_buf;
278}
279
280/* Return log buffer size */
281u32 log_buf_len_get(void)
282{
283 return log_buf_len;
284}
285
Kay Sievers7ff95542012-05-03 02:29:13 +0200286/* human readable text of the record */
Joe Perches62e32ac2013-07-31 13:53:47 -0700287static char *log_text(const struct printk_log *msg)
Kay Sievers7ff95542012-05-03 02:29:13 +0200288{
Joe Perches62e32ac2013-07-31 13:53:47 -0700289 return (char *)msg + sizeof(struct printk_log);
Kay Sievers7ff95542012-05-03 02:29:13 +0200290}
291
292/* optional key/value pair dictionary attached to the record */
Joe Perches62e32ac2013-07-31 13:53:47 -0700293static char *log_dict(const struct printk_log *msg)
Kay Sievers7ff95542012-05-03 02:29:13 +0200294{
Joe Perches62e32ac2013-07-31 13:53:47 -0700295 return (char *)msg + sizeof(struct printk_log) + msg->text_len;
Kay Sievers7ff95542012-05-03 02:29:13 +0200296}
297
298/* get record by index; idx must point to valid msg */
Joe Perches62e32ac2013-07-31 13:53:47 -0700299static struct printk_log *log_from_idx(u32 idx)
Kay Sievers7ff95542012-05-03 02:29:13 +0200300{
Joe Perches62e32ac2013-07-31 13:53:47 -0700301 struct printk_log *msg = (struct printk_log *)(log_buf + idx);
Kay Sievers7ff95542012-05-03 02:29:13 +0200302
303 /*
304 * A length == 0 record is the end of buffer marker. Wrap around and
305 * read the message at the start of the buffer.
306 */
307 if (!msg->len)
Joe Perches62e32ac2013-07-31 13:53:47 -0700308 return (struct printk_log *)log_buf;
Kay Sievers7ff95542012-05-03 02:29:13 +0200309 return msg;
310}
311
312/* get next record; idx must point to valid msg */
313static u32 log_next(u32 idx)
314{
Joe Perches62e32ac2013-07-31 13:53:47 -0700315 struct printk_log *msg = (struct printk_log *)(log_buf + idx);
Kay Sievers7ff95542012-05-03 02:29:13 +0200316
317 /* length == 0 indicates the end of the buffer; wrap */
318 /*
319 * A length == 0 record is the end of buffer marker. Wrap around and
320 * read the message at the start of the buffer as *this* one, and
321 * return the one after that.
322 */
323 if (!msg->len) {
Joe Perches62e32ac2013-07-31 13:53:47 -0700324 msg = (struct printk_log *)log_buf;
Kay Sievers7ff95542012-05-03 02:29:13 +0200325 return msg->len;
326 }
327 return idx + msg->len;
328}
329
Petr Mladekf40e4b92014-06-04 16:11:30 -0700330/*
331 * Check whether there is enough free space for the given message.
332 *
333 * The same values of first_idx and next_idx mean that the buffer
334 * is either empty or full.
335 *
336 * If the buffer is empty, we must respect the position of the indexes.
337 * They cannot be reset to the beginning of the buffer.
338 */
339static int logbuf_has_space(u32 msg_size, bool empty)
Petr Mladek0a581692014-06-04 16:11:28 -0700340{
341 u32 free;
342
Petr Mladekf40e4b92014-06-04 16:11:30 -0700343 if (log_next_idx > log_first_idx || empty)
Petr Mladek0a581692014-06-04 16:11:28 -0700344 free = max(log_buf_len - log_next_idx, log_first_idx);
345 else
346 free = log_first_idx - log_next_idx;
347
348 /*
349 * We need space also for an empty header that signalizes wrapping
350 * of the buffer.
351 */
352 return free >= msg_size + sizeof(struct printk_log);
353}
354
Petr Mladekf40e4b92014-06-04 16:11:30 -0700355static int log_make_free_space(u32 msg_size)
Petr Mladek0a581692014-06-04 16:11:28 -0700356{
357 while (log_first_seq < log_next_seq) {
Petr Mladekf40e4b92014-06-04 16:11:30 -0700358 if (logbuf_has_space(msg_size, false))
359 return 0;
Alex Elder0b90fec2014-08-06 16:09:03 -0700360 /* drop old messages until we have enough contiguous space */
Petr Mladek0a581692014-06-04 16:11:28 -0700361 log_first_idx = log_next(log_first_idx);
362 log_first_seq++;
363 }
Petr Mladekf40e4b92014-06-04 16:11:30 -0700364
365 /* sequence numbers are equal, so the log buffer is empty */
366 if (logbuf_has_space(msg_size, true))
367 return 0;
368
369 return -ENOMEM;
Petr Mladek0a581692014-06-04 16:11:28 -0700370}
371
Petr Mladek85c870432014-06-04 16:11:31 -0700372/* compute the message size including the padding bytes */
373static u32 msg_used_size(u16 text_len, u16 dict_len, u32 *pad_len)
374{
375 u32 size;
376
377 size = sizeof(struct printk_log) + text_len + dict_len;
378 *pad_len = (-size) & (LOG_ALIGN - 1);
379 size += *pad_len;
380
381 return size;
382}
383
Petr Mladek55bd53a2014-06-04 16:11:32 -0700384/*
385 * Define how much of the log buffer we could take at maximum. The value
386 * must be greater than two. Note that only half of the buffer is available
387 * when the index points to the middle.
388 */
389#define MAX_LOG_TAKE_PART 4
390static const char trunc_msg[] = "<truncated>";
391
392static u32 truncate_msg(u16 *text_len, u16 *trunc_msg_len,
393 u16 *dict_len, u32 *pad_len)
394{
395 /*
396 * The message should not take the whole buffer. Otherwise, it might
397 * get removed too soon.
398 */
399 u32 max_text_len = log_buf_len / MAX_LOG_TAKE_PART;
400 if (*text_len > max_text_len)
401 *text_len = max_text_len;
402 /* enable the warning message */
403 *trunc_msg_len = strlen(trunc_msg);
404 /* disable the "dict" completely */
405 *dict_len = 0;
406 /* compute the size again, count also the warning message */
407 return msg_used_size(*text_len + *trunc_msg_len, 0, pad_len);
408}
409
Kay Sievers7ff95542012-05-03 02:29:13 +0200410/* insert record into the buffer, discard old ones, update heads */
Petr Mladek034633c2014-06-04 16:11:33 -0700411static int log_store(int facility, int level,
412 enum log_flags flags, u64 ts_nsec,
413 const char *dict, u16 dict_len,
414 const char *text, u16 text_len)
Kay Sievers7ff95542012-05-03 02:29:13 +0200415{
Joe Perches62e32ac2013-07-31 13:53:47 -0700416 struct printk_log *msg;
Kay Sievers7ff95542012-05-03 02:29:13 +0200417 u32 size, pad_len;
Petr Mladek55bd53a2014-06-04 16:11:32 -0700418 u16 trunc_msg_len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +0200419
420 /* number of '\0' padding bytes to next message */
Petr Mladek85c870432014-06-04 16:11:31 -0700421 size = msg_used_size(text_len, dict_len, &pad_len);
Kay Sievers7ff95542012-05-03 02:29:13 +0200422
Petr Mladek55bd53a2014-06-04 16:11:32 -0700423 if (log_make_free_space(size)) {
424 /* truncate the message if it is too long for empty buffer */
425 size = truncate_msg(&text_len, &trunc_msg_len,
426 &dict_len, &pad_len);
427 /* survive when the log buffer is too small for trunc_msg */
428 if (log_make_free_space(size))
Petr Mladek034633c2014-06-04 16:11:33 -0700429 return 0;
Petr Mladek55bd53a2014-06-04 16:11:32 -0700430 }
Kay Sievers7ff95542012-05-03 02:29:13 +0200431
Petr Mladek39b25102014-04-03 14:48:42 -0700432 if (log_next_idx + size + sizeof(struct printk_log) > log_buf_len) {
Kay Sievers7ff95542012-05-03 02:29:13 +0200433 /*
434 * This message + an additional empty header does not fit
435 * at the end of the buffer. Add an empty header with len == 0
436 * to signify a wrap around.
437 */
Joe Perches62e32ac2013-07-31 13:53:47 -0700438 memset(log_buf + log_next_idx, 0, sizeof(struct printk_log));
Kay Sievers7ff95542012-05-03 02:29:13 +0200439 log_next_idx = 0;
440 }
441
442 /* fill message */
Joe Perches62e32ac2013-07-31 13:53:47 -0700443 msg = (struct printk_log *)(log_buf + log_next_idx);
Kay Sievers7ff95542012-05-03 02:29:13 +0200444 memcpy(log_text(msg), text, text_len);
445 msg->text_len = text_len;
Petr Mladek55bd53a2014-06-04 16:11:32 -0700446 if (trunc_msg_len) {
447 memcpy(log_text(msg) + text_len, trunc_msg, trunc_msg_len);
448 msg->text_len += trunc_msg_len;
449 }
Kay Sievers7ff95542012-05-03 02:29:13 +0200450 memcpy(log_dict(msg), dict, dict_len);
451 msg->dict_len = dict_len;
Kay Sievers084681d2012-06-28 09:38:53 +0200452 msg->facility = facility;
453 msg->level = level & 7;
454 msg->flags = flags & 0x1f;
455 if (ts_nsec > 0)
456 msg->ts_nsec = ts_nsec;
457 else
458 msg->ts_nsec = local_clock();
Kay Sievers7ff95542012-05-03 02:29:13 +0200459 memset(log_dict(msg) + dict_len, 0, pad_len);
Petr Mladekfce6e032014-04-03 14:48:43 -0700460 msg->len = size;
Kay Sievers7ff95542012-05-03 02:29:13 +0200461
462 /* insert message */
463 log_next_idx += msg->len;
464 log_next_seq++;
Petr Mladek034633c2014-06-04 16:11:33 -0700465
466 return msg->text_len;
Kay Sievers7ff95542012-05-03 02:29:13 +0200467}
Matt Mackalld59745c2005-05-01 08:59:02 -0700468
Alex Eldere99aa462014-08-06 16:09:05 -0700469int dmesg_restrict = IS_ENABLED(CONFIG_SECURITY_DMESG_RESTRICT);
Kees Cook637241a2013-06-12 14:04:39 -0700470
471static int syslog_action_restricted(int type)
472{
473 if (dmesg_restrict)
474 return 1;
475 /*
476 * Unless restricted, we allow "read all" and "get buffer size"
477 * for everybody.
478 */
479 return type != SYSLOG_ACTION_READ_ALL &&
480 type != SYSLOG_ACTION_SIZE_BUFFER;
481}
482
483static int check_syslog_permissions(int type, bool from_file)
484{
485 /*
486 * If this is from /proc/kmsg and we've already opened it, then we've
487 * already done the capabilities checks at open time.
488 */
489 if (from_file && type != SYSLOG_ACTION_OPEN)
490 return 0;
491
492 if (syslog_action_restricted(type)) {
493 if (capable(CAP_SYSLOG))
494 return 0;
495 /*
496 * For historical reasons, accept CAP_SYS_ADMIN too, with
497 * a warning.
498 */
499 if (capable(CAP_SYS_ADMIN)) {
500 pr_warn_once("%s (%d): Attempt to access syslog with "
501 "CAP_SYS_ADMIN but no CAP_SYSLOG "
502 "(deprecated).\n",
503 current->comm, task_pid_nr(current));
504 return 0;
505 }
506 return -EPERM;
507 }
508 return security_syslog(type);
509}
510
511
Kay Sieverse11fea92012-05-03 02:29:41 +0200512/* /dev/kmsg - userspace message inject/listen interface */
513struct devkmsg_user {
514 u64 seq;
515 u32 idx;
Kay Sieversd39f3d72012-07-16 18:35:30 -0700516 enum log_flags prev;
Kay Sieverse11fea92012-05-03 02:29:41 +0200517 struct mutex lock;
518 char buf[8192];
519};
520
Al Viro849f3122014-08-23 12:23:53 -0400521static ssize_t devkmsg_write(struct kiocb *iocb, struct iov_iter *from)
Kay Sieverse11fea92012-05-03 02:29:41 +0200522{
523 char *buf, *line;
524 int i;
525 int level = default_message_loglevel;
526 int facility = 1; /* LOG_USER */
Al Viro849f3122014-08-23 12:23:53 -0400527 size_t len = iocb->ki_nbytes;
Kay Sieverse11fea92012-05-03 02:29:41 +0200528 ssize_t ret = len;
529
530 if (len > LOG_LINE_MAX)
531 return -EINVAL;
532 buf = kmalloc(len+1, GFP_KERNEL);
533 if (buf == NULL)
534 return -ENOMEM;
535
Al Viro849f3122014-08-23 12:23:53 -0400536 buf[len] = '\0';
537 if (copy_from_iter(buf, len, from) != len) {
538 kfree(buf);
539 return -EFAULT;
Kay Sieverse11fea92012-05-03 02:29:41 +0200540 }
541
542 /*
543 * Extract and skip the syslog prefix <[0-9]*>. Coming from userspace
544 * the decimal value represents 32bit, the lower 3 bit are the log
545 * level, the rest are the log facility.
546 *
547 * If no prefix or no userspace facility is specified, we
548 * enforce LOG_USER, to be able to reliably distinguish
549 * kernel-generated messages from userspace-injected ones.
550 */
551 line = buf;
552 if (line[0] == '<') {
553 char *endp = NULL;
554
555 i = simple_strtoul(line+1, &endp, 10);
556 if (endp && endp[0] == '>') {
557 level = i & 7;
558 if (i >> 3)
559 facility = i >> 3;
560 endp++;
561 len -= endp - line;
562 line = endp;
563 }
564 }
Kay Sieverse11fea92012-05-03 02:29:41 +0200565
566 printk_emit(facility, level, NULL, 0, "%s", line);
Kay Sieverse11fea92012-05-03 02:29:41 +0200567 kfree(buf);
568 return ret;
569}
570
571static ssize_t devkmsg_read(struct file *file, char __user *buf,
572 size_t count, loff_t *ppos)
573{
574 struct devkmsg_user *user = file->private_data;
Joe Perches62e32ac2013-07-31 13:53:47 -0700575 struct printk_log *msg;
Kay Sievers5fc324902012-05-08 13:04:17 +0200576 u64 ts_usec;
Kay Sieverse11fea92012-05-03 02:29:41 +0200577 size_t i;
Kay Sieversd39f3d72012-07-16 18:35:30 -0700578 char cont = '-';
Kay Sieverse11fea92012-05-03 02:29:41 +0200579 size_t len;
580 ssize_t ret;
581
582 if (!user)
583 return -EBADF;
584
Yuanhan Liu4a77a5a2012-06-16 21:21:51 +0800585 ret = mutex_lock_interruptible(&user->lock);
586 if (ret)
587 return ret;
liu chuansheng5c53d812012-07-06 09:50:08 -0700588 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200589 while (user->seq == log_next_seq) {
590 if (file->f_flags & O_NONBLOCK) {
591 ret = -EAGAIN;
liu chuansheng5c53d812012-07-06 09:50:08 -0700592 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200593 goto out;
594 }
595
liu chuansheng5c53d812012-07-06 09:50:08 -0700596 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200597 ret = wait_event_interruptible(log_wait,
598 user->seq != log_next_seq);
599 if (ret)
600 goto out;
liu chuansheng5c53d812012-07-06 09:50:08 -0700601 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200602 }
603
604 if (user->seq < log_first_seq) {
605 /* our last seen message is gone, return error and reset */
606 user->idx = log_first_idx;
607 user->seq = log_first_seq;
608 ret = -EPIPE;
liu chuansheng5c53d812012-07-06 09:50:08 -0700609 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200610 goto out;
611 }
612
613 msg = log_from_idx(user->idx);
Kay Sievers5fc324902012-05-08 13:04:17 +0200614 ts_usec = msg->ts_nsec;
615 do_div(ts_usec, 1000);
Kay Sieversd39f3d72012-07-16 18:35:30 -0700616
617 /*
618 * If we couldn't merge continuation line fragments during the print,
619 * export the stored flags to allow an optional external merge of the
620 * records. Merging the records isn't always neccessarily correct, like
621 * when we hit a race during printing. In most cases though, it produces
622 * better readable output. 'c' in the record flags mark the first
623 * fragment of a line, '+' the following.
624 */
625 if (msg->flags & LOG_CONT && !(user->prev & LOG_CONT))
626 cont = 'c';
627 else if ((msg->flags & LOG_CONT) ||
628 ((user->prev & LOG_CONT) && !(msg->flags & LOG_PREFIX)))
629 cont = '+';
630
631 len = sprintf(user->buf, "%u,%llu,%llu,%c;",
632 (msg->facility << 3) | msg->level,
633 user->seq, ts_usec, cont);
634 user->prev = msg->flags;
Kay Sieverse11fea92012-05-03 02:29:41 +0200635
636 /* escape non-printable characters */
637 for (i = 0; i < msg->text_len; i++) {
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200638 unsigned char c = log_text(msg)[i];
Kay Sieverse11fea92012-05-03 02:29:41 +0200639
Kay Sieverse3f5a5f2012-07-06 09:50:09 -0700640 if (c < ' ' || c >= 127 || c == '\\')
Kay Sieverse11fea92012-05-03 02:29:41 +0200641 len += sprintf(user->buf + len, "\\x%02x", c);
642 else
643 user->buf[len++] = c;
644 }
645 user->buf[len++] = '\n';
646
647 if (msg->dict_len) {
648 bool line = true;
649
650 for (i = 0; i < msg->dict_len; i++) {
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200651 unsigned char c = log_dict(msg)[i];
Kay Sieverse11fea92012-05-03 02:29:41 +0200652
653 if (line) {
654 user->buf[len++] = ' ';
655 line = false;
656 }
657
658 if (c == '\0') {
659 user->buf[len++] = '\n';
660 line = true;
661 continue;
662 }
663
Kay Sieverse3f5a5f2012-07-06 09:50:09 -0700664 if (c < ' ' || c >= 127 || c == '\\') {
Kay Sieverse11fea92012-05-03 02:29:41 +0200665 len += sprintf(user->buf + len, "\\x%02x", c);
666 continue;
667 }
668
669 user->buf[len++] = c;
670 }
671 user->buf[len++] = '\n';
672 }
673
674 user->idx = log_next(user->idx);
675 user->seq++;
liu chuansheng5c53d812012-07-06 09:50:08 -0700676 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200677
678 if (len > count) {
679 ret = -EINVAL;
680 goto out;
681 }
682
683 if (copy_to_user(buf, user->buf, len)) {
684 ret = -EFAULT;
685 goto out;
686 }
687 ret = len;
688out:
689 mutex_unlock(&user->lock);
690 return ret;
691}
692
693static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence)
694{
695 struct devkmsg_user *user = file->private_data;
696 loff_t ret = 0;
697
698 if (!user)
699 return -EBADF;
700 if (offset)
701 return -ESPIPE;
702
liu chuansheng5c53d812012-07-06 09:50:08 -0700703 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200704 switch (whence) {
705 case SEEK_SET:
706 /* the first record */
707 user->idx = log_first_idx;
708 user->seq = log_first_seq;
709 break;
710 case SEEK_DATA:
711 /*
712 * The first record after the last SYSLOG_ACTION_CLEAR,
713 * like issued by 'dmesg -c'. Reading /dev/kmsg itself
714 * changes no global state, and does not clear anything.
715 */
716 user->idx = clear_idx;
717 user->seq = clear_seq;
718 break;
719 case SEEK_END:
720 /* after the last record */
721 user->idx = log_next_idx;
722 user->seq = log_next_seq;
723 break;
724 default:
725 ret = -EINVAL;
726 }
liu chuansheng5c53d812012-07-06 09:50:08 -0700727 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200728 return ret;
729}
730
731static unsigned int devkmsg_poll(struct file *file, poll_table *wait)
732{
733 struct devkmsg_user *user = file->private_data;
734 int ret = 0;
735
736 if (!user)
737 return POLLERR|POLLNVAL;
738
739 poll_wait(file, &log_wait, wait);
740
liu chuansheng5c53d812012-07-06 09:50:08 -0700741 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200742 if (user->seq < log_next_seq) {
743 /* return error when data has vanished underneath us */
744 if (user->seq < log_first_seq)
745 ret = POLLIN|POLLRDNORM|POLLERR|POLLPRI;
Nicolas Kaiser0a285312013-04-29 16:17:20 -0700746 else
747 ret = POLLIN|POLLRDNORM;
Kay Sieverse11fea92012-05-03 02:29:41 +0200748 }
liu chuansheng5c53d812012-07-06 09:50:08 -0700749 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200750
751 return ret;
752}
753
754static int devkmsg_open(struct inode *inode, struct file *file)
755{
756 struct devkmsg_user *user;
757 int err;
758
759 /* write-only does not need any file context */
760 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
761 return 0;
762
Kees Cook637241a2013-06-12 14:04:39 -0700763 err = check_syslog_permissions(SYSLOG_ACTION_READ_ALL,
764 SYSLOG_FROM_READER);
Kay Sieverse11fea92012-05-03 02:29:41 +0200765 if (err)
766 return err;
767
768 user = kmalloc(sizeof(struct devkmsg_user), GFP_KERNEL);
769 if (!user)
770 return -ENOMEM;
771
772 mutex_init(&user->lock);
773
liu chuansheng5c53d812012-07-06 09:50:08 -0700774 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200775 user->idx = log_first_idx;
776 user->seq = log_first_seq;
liu chuansheng5c53d812012-07-06 09:50:08 -0700777 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200778
779 file->private_data = user;
780 return 0;
781}
782
783static int devkmsg_release(struct inode *inode, struct file *file)
784{
785 struct devkmsg_user *user = file->private_data;
786
787 if (!user)
788 return 0;
789
790 mutex_destroy(&user->lock);
791 kfree(user);
792 return 0;
793}
794
795const struct file_operations kmsg_fops = {
796 .open = devkmsg_open,
797 .read = devkmsg_read,
Al Viro849f3122014-08-23 12:23:53 -0400798 .write_iter = devkmsg_write,
Kay Sieverse11fea92012-05-03 02:29:41 +0200799 .llseek = devkmsg_llseek,
800 .poll = devkmsg_poll,
801 .release = devkmsg_release,
802};
803
Neil Horman04d491a2009-04-02 16:58:57 -0700804#ifdef CONFIG_KEXEC
805/*
Dirk Gouders4c1ace62013-11-12 15:08:54 -0800806 * This appends the listed symbols to /proc/vmcore
Neil Horman04d491a2009-04-02 16:58:57 -0700807 *
Dirk Gouders4c1ace62013-11-12 15:08:54 -0800808 * /proc/vmcore is used by various utilities, like crash and makedumpfile to
Neil Horman04d491a2009-04-02 16:58:57 -0700809 * obtain access to symbols that are otherwise very difficult to locate. These
810 * symbols are specifically used so that utilities can access and extract the
811 * dmesg log from a vmcore file after a crash.
812 */
813void log_buf_kexec_setup(void)
814{
815 VMCOREINFO_SYMBOL(log_buf);
Neil Horman04d491a2009-04-02 16:58:57 -0700816 VMCOREINFO_SYMBOL(log_buf_len);
Kay Sievers7ff95542012-05-03 02:29:13 +0200817 VMCOREINFO_SYMBOL(log_first_idx);
818 VMCOREINFO_SYMBOL(log_next_idx);
Vivek Goyal67914572012-07-18 13:18:12 -0400819 /*
Joe Perches62e32ac2013-07-31 13:53:47 -0700820 * Export struct printk_log size and field offsets. User space tools can
Vivek Goyal67914572012-07-18 13:18:12 -0400821 * parse it and detect any changes to structure down the line.
822 */
Joe Perches62e32ac2013-07-31 13:53:47 -0700823 VMCOREINFO_STRUCT_SIZE(printk_log);
824 VMCOREINFO_OFFSET(printk_log, ts_nsec);
825 VMCOREINFO_OFFSET(printk_log, len);
826 VMCOREINFO_OFFSET(printk_log, text_len);
827 VMCOREINFO_OFFSET(printk_log, dict_len);
Neil Horman04d491a2009-04-02 16:58:57 -0700828}
829#endif
830
Mike Travis162a7e72011-05-24 17:13:20 -0700831/* requested log_buf_len from kernel cmdline */
832static unsigned long __initdata new_log_buf_len;
833
Luis R. Rodriguezc0a318a2014-08-06 16:08:52 -0700834/* we practice scaling the ring buffer by powers of 2 */
835static void __init log_buf_len_update(unsigned size)
836{
837 if (size)
838 size = roundup_pow_of_two(size);
839 if (size > log_buf_len)
840 new_log_buf_len = size;
841}
842
Mike Travis162a7e72011-05-24 17:13:20 -0700843/* save requested log_buf_len since it's too early to process it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844static int __init log_buf_len_setup(char *str)
845{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800846 unsigned size = memparse(str, &str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Luis R. Rodriguezc0a318a2014-08-06 16:08:52 -0700848 log_buf_len_update(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Mike Travis162a7e72011-05-24 17:13:20 -0700850 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
Mike Travis162a7e72011-05-24 17:13:20 -0700852early_param("log_buf_len", log_buf_len_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Geert Uytterhoeven2240a312014-10-13 15:51:11 -0700854#ifdef CONFIG_SMP
855#define __LOG_CPU_MAX_BUF_LEN (1 << CONFIG_LOG_CPU_MAX_BUF_SHIFT)
856
Luis R. Rodriguez23b28992014-08-06 16:08:56 -0700857static void __init log_buf_add_cpu(void)
858{
859 unsigned int cpu_extra;
860
861 /*
862 * archs should set up cpu_possible_bits properly with
863 * set_cpu_possible() after setup_arch() but just in
864 * case lets ensure this is valid.
865 */
866 if (num_possible_cpus() == 1)
867 return;
868
869 cpu_extra = (num_possible_cpus() - 1) * __LOG_CPU_MAX_BUF_LEN;
870
871 /* by default this will only continue through for large > 64 CPUs */
872 if (cpu_extra <= __LOG_BUF_LEN / 2)
873 return;
874
875 pr_info("log_buf_len individual max cpu contribution: %d bytes\n",
876 __LOG_CPU_MAX_BUF_LEN);
877 pr_info("log_buf_len total cpu_extra contributions: %d bytes\n",
878 cpu_extra);
879 pr_info("log_buf_len min size: %d bytes\n", __LOG_BUF_LEN);
880
881 log_buf_len_update(cpu_extra + __LOG_BUF_LEN);
882}
Geert Uytterhoeven2240a312014-10-13 15:51:11 -0700883#else /* !CONFIG_SMP */
884static inline void log_buf_add_cpu(void) {}
885#endif /* CONFIG_SMP */
Luis R. Rodriguez23b28992014-08-06 16:08:56 -0700886
Mike Travis162a7e72011-05-24 17:13:20 -0700887void __init setup_log_buf(int early)
888{
889 unsigned long flags;
Mike Travis162a7e72011-05-24 17:13:20 -0700890 char *new_log_buf;
891 int free;
892
Luis R. Rodriguez23b28992014-08-06 16:08:56 -0700893 if (log_buf != __log_buf)
894 return;
895
896 if (!early && !new_log_buf_len)
897 log_buf_add_cpu();
898
Mike Travis162a7e72011-05-24 17:13:20 -0700899 if (!new_log_buf_len)
900 return;
901
902 if (early) {
Santosh Shilimkar9da791d2014-01-21 15:50:23 -0800903 new_log_buf =
Luis R. Rodriguez70300172014-08-06 16:08:49 -0700904 memblock_virt_alloc(new_log_buf_len, LOG_ALIGN);
Mike Travis162a7e72011-05-24 17:13:20 -0700905 } else {
Luis R. Rodriguez70300172014-08-06 16:08:49 -0700906 new_log_buf = memblock_virt_alloc_nopanic(new_log_buf_len,
907 LOG_ALIGN);
Mike Travis162a7e72011-05-24 17:13:20 -0700908 }
909
910 if (unlikely(!new_log_buf)) {
911 pr_err("log_buf_len: %ld bytes not available\n",
912 new_log_buf_len);
913 return;
914 }
915
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200916 raw_spin_lock_irqsave(&logbuf_lock, flags);
Mike Travis162a7e72011-05-24 17:13:20 -0700917 log_buf_len = new_log_buf_len;
918 log_buf = new_log_buf;
919 new_log_buf_len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +0200920 free = __LOG_BUF_LEN - log_next_idx;
921 memcpy(log_buf, __log_buf, __LOG_BUF_LEN);
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200922 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
Mike Travis162a7e72011-05-24 17:13:20 -0700923
Luis R. Rodriguezf5405172014-08-06 16:08:54 -0700924 pr_info("log_buf_len: %d bytes\n", log_buf_len);
Mike Travis162a7e72011-05-24 17:13:20 -0700925 pr_info("early log buf free: %d(%d%%)\n",
926 free, (free * 100) / __LOG_BUF_LEN);
927}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800929static bool __read_mostly ignore_loglevel;
930
931static int __init ignore_loglevel_setup(char *str)
932{
Neil Zhangd25d9fe2014-08-06 16:09:12 -0700933 ignore_loglevel = true;
Andrew Morton27083ba2013-11-12 15:08:50 -0800934 pr_info("debug: ignoring loglevel setting.\n");
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800935
936 return 0;
937}
938
939early_param("ignore_loglevel", ignore_loglevel_setup);
940module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR);
941MODULE_PARM_DESC(ignore_loglevel, "ignore loglevel setting, to"
942 "print all kernel messages to the console.");
943
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700944#ifdef CONFIG_BOOT_PRINTK_DELAY
945
Namhyung Kim674dff62010-10-26 14:22:48 -0700946static int boot_delay; /* msecs delay after each printk during bootup */
Dave Young3a3b6ed2009-09-22 16:43:31 -0700947static unsigned long long loops_per_msec; /* based on boot_delay */
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700948
949static int __init boot_delay_setup(char *str)
950{
951 unsigned long lpj;
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700952
953 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
954 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
955
956 get_option(&str, &boot_delay);
957 if (boot_delay > 10 * 1000)
958 boot_delay = 0;
959
Dave Young3a3b6ed2009-09-22 16:43:31 -0700960 pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
961 "HZ: %d, loops_per_msec: %llu\n",
962 boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
Dave Young29e9d222013-11-12 15:08:53 -0800963 return 0;
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700964}
Dave Young29e9d222013-11-12 15:08:53 -0800965early_param("boot_delay", boot_delay_setup);
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700966
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800967static void boot_delay_msec(int level)
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700968{
969 unsigned long long k;
970 unsigned long timeout;
971
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800972 if ((boot_delay == 0 || system_state != SYSTEM_BOOTING)
973 || (level >= console_loglevel && !ignore_loglevel)) {
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700974 return;
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800975 }
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700976
Dave Young3a3b6ed2009-09-22 16:43:31 -0700977 k = (unsigned long long)loops_per_msec * boot_delay;
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700978
979 timeout = jiffies + msecs_to_jiffies(boot_delay);
980 while (k) {
981 k--;
982 cpu_relax();
983 /*
984 * use (volatile) jiffies to prevent
985 * compiler reduction; loop termination via jiffies
986 * is secondary and may or may not happen.
987 */
988 if (time_after(jiffies, timeout))
989 break;
990 touch_nmi_watchdog();
991 }
992}
993#else
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800994static inline void boot_delay_msec(int level)
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700995{
996}
997#endif
998
Alex Eldere99aa462014-08-06 16:09:05 -0700999static bool printk_time = IS_ENABLED(CONFIG_PRINTK_TIME);
Kay Sievers7ff95542012-05-03 02:29:13 +02001000module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
1001
Kay Sievers084681d2012-06-28 09:38:53 +02001002static size_t print_time(u64 ts, char *buf)
1003{
1004 unsigned long rem_nsec;
1005
1006 if (!printk_time)
1007 return 0;
1008
Kay Sievers084681d2012-06-28 09:38:53 +02001009 rem_nsec = do_div(ts, 1000000000);
Roland Dreier35dac272013-01-04 15:35:50 -08001010
1011 if (!buf)
1012 return snprintf(NULL, 0, "[%5lu.000000] ", (unsigned long)ts);
1013
Kay Sievers084681d2012-06-28 09:38:53 +02001014 return sprintf(buf, "[%5lu.%06lu] ",
1015 (unsigned long)ts, rem_nsec / 1000);
1016}
1017
Joe Perches62e32ac2013-07-31 13:53:47 -07001018static size_t print_prefix(const struct printk_log *msg, bool syslog, char *buf)
Kay Sievers649e6ee2012-05-10 04:30:45 +02001019{
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001020 size_t len = 0;
Kay Sievers43a73a52012-07-06 09:50:09 -07001021 unsigned int prefix = (msg->facility << 3) | msg->level;
Kay Sievers649e6ee2012-05-10 04:30:45 +02001022
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001023 if (syslog) {
1024 if (buf) {
Kay Sievers43a73a52012-07-06 09:50:09 -07001025 len += sprintf(buf, "<%u>", prefix);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001026 } else {
1027 len += 3;
Kay Sievers43a73a52012-07-06 09:50:09 -07001028 if (prefix > 999)
1029 len += 3;
1030 else if (prefix > 99)
1031 len += 2;
1032 else if (prefix > 9)
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001033 len++;
1034 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001035 }
1036
Kay Sievers084681d2012-06-28 09:38:53 +02001037 len += print_time(msg->ts_nsec, buf ? buf + len : NULL);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001038 return len;
1039}
1040
Joe Perches62e32ac2013-07-31 13:53:47 -07001041static size_t msg_print_text(const struct printk_log *msg, enum log_flags prev,
Kay Sievers5becfb12012-07-09 12:15:42 -07001042 bool syslog, char *buf, size_t size)
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001043{
1044 const char *text = log_text(msg);
1045 size_t text_size = msg->text_len;
Kay Sievers5becfb12012-07-09 12:15:42 -07001046 bool prefix = true;
1047 bool newline = true;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001048 size_t len = 0;
1049
Kay Sievers5becfb12012-07-09 12:15:42 -07001050 if ((prev & LOG_CONT) && !(msg->flags & LOG_PREFIX))
1051 prefix = false;
1052
1053 if (msg->flags & LOG_CONT) {
1054 if ((prev & LOG_CONT) && !(prev & LOG_NEWLINE))
1055 prefix = false;
1056
1057 if (!(msg->flags & LOG_NEWLINE))
1058 newline = false;
1059 }
1060
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001061 do {
1062 const char *next = memchr(text, '\n', text_size);
1063 size_t text_len;
1064
1065 if (next) {
1066 text_len = next - text;
1067 next++;
1068 text_size -= next - text;
1069 } else {
1070 text_len = text_size;
1071 }
1072
1073 if (buf) {
1074 if (print_prefix(msg, syslog, NULL) +
Kay Sievers70498252012-07-16 18:35:29 -07001075 text_len + 1 >= size - len)
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001076 break;
1077
Kay Sievers5becfb12012-07-09 12:15:42 -07001078 if (prefix)
1079 len += print_prefix(msg, syslog, buf + len);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001080 memcpy(buf + len, text, text_len);
1081 len += text_len;
Kay Sievers5becfb12012-07-09 12:15:42 -07001082 if (next || newline)
1083 buf[len++] = '\n';
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001084 } else {
1085 /* SYSLOG_ACTION_* buffer size only calculation */
Kay Sievers5becfb12012-07-09 12:15:42 -07001086 if (prefix)
1087 len += print_prefix(msg, syslog, NULL);
1088 len += text_len;
1089 if (next || newline)
1090 len++;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001091 }
1092
Kay Sievers5becfb12012-07-09 12:15:42 -07001093 prefix = true;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001094 text = next;
1095 } while (text);
1096
Kay Sievers7ff95542012-05-03 02:29:13 +02001097 return len;
1098}
1099
1100static int syslog_print(char __user *buf, int size)
1101{
1102 char *text;
Joe Perches62e32ac2013-07-31 13:53:47 -07001103 struct printk_log *msg;
Jan Beulich116e90b2012-06-22 16:36:09 +01001104 int len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001105
Kay Sievers70498252012-07-16 18:35:29 -07001106 text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
Kay Sievers7ff95542012-05-03 02:29:13 +02001107 if (!text)
1108 return -ENOMEM;
1109
Jan Beulich116e90b2012-06-22 16:36:09 +01001110 while (size > 0) {
1111 size_t n;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001112 size_t skip;
Kay Sievers7ff95542012-05-03 02:29:13 +02001113
Jan Beulich116e90b2012-06-22 16:36:09 +01001114 raw_spin_lock_irq(&logbuf_lock);
1115 if (syslog_seq < log_first_seq) {
1116 /* messages are gone, move to first one */
1117 syslog_seq = log_first_seq;
1118 syslog_idx = log_first_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001119 syslog_prev = 0;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001120 syslog_partial = 0;
Jan Beulich116e90b2012-06-22 16:36:09 +01001121 }
1122 if (syslog_seq == log_next_seq) {
1123 raw_spin_unlock_irq(&logbuf_lock);
1124 break;
1125 }
Kay Sieverseb02dac2012-07-09 10:05:10 -07001126
1127 skip = syslog_partial;
Jan Beulich116e90b2012-06-22 16:36:09 +01001128 msg = log_from_idx(syslog_idx);
Kay Sievers70498252012-07-16 18:35:29 -07001129 n = msg_print_text(msg, syslog_prev, true, text,
1130 LOG_LINE_MAX + PREFIX_MAX);
Kay Sieverseb02dac2012-07-09 10:05:10 -07001131 if (n - syslog_partial <= size) {
1132 /* message fits into buffer, move forward */
Jan Beulich116e90b2012-06-22 16:36:09 +01001133 syslog_idx = log_next(syslog_idx);
1134 syslog_seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07001135 syslog_prev = msg->flags;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001136 n -= syslog_partial;
1137 syslog_partial = 0;
1138 } else if (!len){
1139 /* partial read(), remember position */
1140 n = size;
1141 syslog_partial += n;
Jan Beulich116e90b2012-06-22 16:36:09 +01001142 } else
1143 n = 0;
1144 raw_spin_unlock_irq(&logbuf_lock);
1145
1146 if (!n)
1147 break;
1148
Kay Sieverseb02dac2012-07-09 10:05:10 -07001149 if (copy_to_user(buf, text + skip, n)) {
Jan Beulich116e90b2012-06-22 16:36:09 +01001150 if (!len)
1151 len = -EFAULT;
1152 break;
1153 }
Kay Sieverseb02dac2012-07-09 10:05:10 -07001154
1155 len += n;
1156 size -= n;
1157 buf += n;
Jan Beulich116e90b2012-06-22 16:36:09 +01001158 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001159
1160 kfree(text);
1161 return len;
1162}
1163
1164static int syslog_print_all(char __user *buf, int size, bool clear)
1165{
1166 char *text;
1167 int len = 0;
1168
Kay Sievers70498252012-07-16 18:35:29 -07001169 text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
Kay Sievers7ff95542012-05-03 02:29:13 +02001170 if (!text)
1171 return -ENOMEM;
1172
1173 raw_spin_lock_irq(&logbuf_lock);
1174 if (buf) {
1175 u64 next_seq;
1176 u64 seq;
1177 u32 idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001178 enum log_flags prev;
Kay Sievers7ff95542012-05-03 02:29:13 +02001179
1180 if (clear_seq < log_first_seq) {
1181 /* messages are gone, move to first available one */
1182 clear_seq = log_first_seq;
1183 clear_idx = log_first_idx;
1184 }
1185
1186 /*
1187 * Find first record that fits, including all following records,
1188 * into the user-provided buffer for this dump.
Kay Sieverse2ae7152012-06-15 14:07:51 +02001189 */
Kay Sievers7ff95542012-05-03 02:29:13 +02001190 seq = clear_seq;
1191 idx = clear_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001192 prev = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001193 while (seq < log_next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07001194 struct printk_log *msg = log_from_idx(idx);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001195
Kay Sievers5becfb12012-07-09 12:15:42 -07001196 len += msg_print_text(msg, prev, true, NULL, 0);
Jeff Mahoneye3756472012-08-10 15:07:09 -04001197 prev = msg->flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02001198 idx = log_next(idx);
1199 seq++;
1200 }
Kay Sieverse2ae7152012-06-15 14:07:51 +02001201
1202 /* move first record forward until length fits into the buffer */
Kay Sievers7ff95542012-05-03 02:29:13 +02001203 seq = clear_seq;
1204 idx = clear_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001205 prev = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001206 while (len > size && seq < log_next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07001207 struct printk_log *msg = log_from_idx(idx);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001208
Kay Sievers5becfb12012-07-09 12:15:42 -07001209 len -= msg_print_text(msg, prev, true, NULL, 0);
Jeff Mahoneye3756472012-08-10 15:07:09 -04001210 prev = msg->flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02001211 idx = log_next(idx);
1212 seq++;
1213 }
1214
Kay Sieverse2ae7152012-06-15 14:07:51 +02001215 /* last message fitting into this dump */
Kay Sievers7ff95542012-05-03 02:29:13 +02001216 next_seq = log_next_seq;
1217
1218 len = 0;
1219 while (len >= 0 && seq < next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07001220 struct printk_log *msg = log_from_idx(idx);
Kay Sievers7ff95542012-05-03 02:29:13 +02001221 int textlen;
1222
Kay Sievers70498252012-07-16 18:35:29 -07001223 textlen = msg_print_text(msg, prev, true, text,
1224 LOG_LINE_MAX + PREFIX_MAX);
Kay Sievers7ff95542012-05-03 02:29:13 +02001225 if (textlen < 0) {
1226 len = textlen;
1227 break;
1228 }
1229 idx = log_next(idx);
1230 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07001231 prev = msg->flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02001232
1233 raw_spin_unlock_irq(&logbuf_lock);
1234 if (copy_to_user(buf + len, text, textlen))
1235 len = -EFAULT;
1236 else
1237 len += textlen;
1238 raw_spin_lock_irq(&logbuf_lock);
1239
1240 if (seq < log_first_seq) {
1241 /* messages are gone, move to next one */
1242 seq = log_first_seq;
1243 idx = log_first_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001244 prev = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001245 }
1246 }
1247 }
1248
1249 if (clear) {
1250 clear_seq = log_next_seq;
1251 clear_idx = log_next_idx;
1252 }
1253 raw_spin_unlock_irq(&logbuf_lock);
1254
1255 kfree(text);
1256 return len;
1257}
1258
Kees Cook00234592010-02-03 15:36:43 -08001259int do_syslog(int type, char __user *buf, int len, bool from_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Kay Sievers7ff95542012-05-03 02:29:13 +02001261 bool clear = false;
1262 static int saved_console_loglevel = -1;
Linus Torvaldsee24aeb2011-02-10 17:53:55 -08001263 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Linus Torvaldsee24aeb2011-02-10 17:53:55 -08001265 error = check_syslog_permissions(type, from_file);
1266 if (error)
1267 goto out;
Eric Paris12b30522010-11-15 18:36:29 -05001268
1269 error = security_syslog(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (error)
1271 return error;
1272
1273 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08001274 case SYSLOG_ACTION_CLOSE: /* Close log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001276 case SYSLOG_ACTION_OPEN: /* Open log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001278 case SYSLOG_ACTION_READ: /* Read from log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 error = -EINVAL;
1280 if (!buf || len < 0)
1281 goto out;
1282 error = 0;
1283 if (!len)
1284 goto out;
1285 if (!access_ok(VERIFY_WRITE, buf, len)) {
1286 error = -EFAULT;
1287 goto out;
1288 }
Yuanhan Liu4a77a5a2012-06-16 21:21:51 +08001289 error = wait_event_interruptible(log_wait,
1290 syslog_seq != log_next_seq);
Kay Sieverscb424ff2012-07-06 09:50:09 -07001291 if (error)
Yuanhan Liu4a77a5a2012-06-16 21:21:51 +08001292 goto out;
Kay Sievers7ff95542012-05-03 02:29:13 +02001293 error = syslog_print(buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001295 /* Read/clear last kernel messages */
1296 case SYSLOG_ACTION_READ_CLEAR:
Kay Sievers7ff95542012-05-03 02:29:13 +02001297 clear = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 /* FALL THRU */
Kees Cookd78ca3c2010-02-03 15:37:13 -08001299 /* Read last kernel messages */
1300 case SYSLOG_ACTION_READ_ALL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 error = -EINVAL;
1302 if (!buf || len < 0)
1303 goto out;
1304 error = 0;
1305 if (!len)
1306 goto out;
1307 if (!access_ok(VERIFY_WRITE, buf, len)) {
1308 error = -EFAULT;
1309 goto out;
1310 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001311 error = syslog_print_all(buf, len, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001313 /* Clear ring buffer */
1314 case SYSLOG_ACTION_CLEAR:
Kay Sievers7ff95542012-05-03 02:29:13 +02001315 syslog_print_all(NULL, 0, true);
Alan Stern4661e352012-06-22 17:12:19 -04001316 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001317 /* Disable logging to console */
1318 case SYSLOG_ACTION_CONSOLE_OFF:
Frans Pop1aaad492009-07-06 13:31:48 +02001319 if (saved_console_loglevel == -1)
1320 saved_console_loglevel = console_loglevel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 console_loglevel = minimum_console_loglevel;
1322 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001323 /* Enable logging to console */
1324 case SYSLOG_ACTION_CONSOLE_ON:
Frans Pop1aaad492009-07-06 13:31:48 +02001325 if (saved_console_loglevel != -1) {
1326 console_loglevel = saved_console_loglevel;
1327 saved_console_loglevel = -1;
1328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001330 /* Set level of messages printed to console */
1331 case SYSLOG_ACTION_CONSOLE_LEVEL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 error = -EINVAL;
1333 if (len < 1 || len > 8)
1334 goto out;
1335 if (len < minimum_console_loglevel)
1336 len = minimum_console_loglevel;
1337 console_loglevel = len;
Frans Pop1aaad492009-07-06 13:31:48 +02001338 /* Implicitly re-enable logging to console */
1339 saved_console_loglevel = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 error = 0;
1341 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001342 /* Number of chars in the log buffer */
1343 case SYSLOG_ACTION_SIZE_UNREAD:
Kay Sievers7ff95542012-05-03 02:29:13 +02001344 raw_spin_lock_irq(&logbuf_lock);
1345 if (syslog_seq < log_first_seq) {
1346 /* messages are gone, move to first one */
1347 syslog_seq = log_first_seq;
1348 syslog_idx = log_first_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001349 syslog_prev = 0;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001350 syslog_partial = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001351 }
1352 if (from_file) {
1353 /*
1354 * Short-cut for poll(/"proc/kmsg") which simply checks
1355 * for pending data, not the size; return the count of
1356 * records, not the length.
1357 */
Alex Eldere97e1262014-08-06 16:08:59 -07001358 error = log_next_seq - syslog_seq;
Kay Sievers7ff95542012-05-03 02:29:13 +02001359 } else {
Kay Sievers5becfb12012-07-09 12:15:42 -07001360 u64 seq = syslog_seq;
1361 u32 idx = syslog_idx;
1362 enum log_flags prev = syslog_prev;
Kay Sievers7ff95542012-05-03 02:29:13 +02001363
1364 error = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001365 while (seq < log_next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07001366 struct printk_log *msg = log_from_idx(idx);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001367
Kay Sievers5becfb12012-07-09 12:15:42 -07001368 error += msg_print_text(msg, prev, true, NULL, 0);
Kay Sievers7ff95542012-05-03 02:29:13 +02001369 idx = log_next(idx);
1370 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07001371 prev = msg->flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02001372 }
Kay Sieverseb02dac2012-07-09 10:05:10 -07001373 error -= syslog_partial;
Kay Sievers7ff95542012-05-03 02:29:13 +02001374 }
1375 raw_spin_unlock_irq(&logbuf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001377 /* Size of the log buffer */
1378 case SYSLOG_ACTION_SIZE_BUFFER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 error = log_buf_len;
1380 break;
1381 default:
1382 error = -EINVAL;
1383 break;
1384 }
1385out:
1386 return error;
1387}
1388
Heiko Carstens1e7bfb22009-01-14 14:14:29 +01001389SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
Kees Cook637241a2013-06-12 14:04:39 -07001391 return do_syslog(type, buf, len, SYSLOG_FROM_READER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392}
1393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 * Call the console drivers, asking them to write out
1396 * log_buf[start] to log_buf[end - 1].
Torben Hohnac751ef2011-01-25 15:07:35 -08001397 * The console_lock must be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 */
Kay Sievers7ff95542012-05-03 02:29:13 +02001399static void call_console_drivers(int level, const char *text, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Kay Sievers7ff95542012-05-03 02:29:13 +02001401 struct console *con;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
zhangwei(Jovi)07c65f42013-04-29 16:17:16 -07001403 trace_console(text, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Kay Sievers7ff95542012-05-03 02:29:13 +02001405 if (level >= console_loglevel && !ignore_loglevel)
1406 return;
1407 if (!console_drivers)
1408 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Kay Sievers7ff95542012-05-03 02:29:13 +02001410 for_each_console(con) {
1411 if (exclusive_console && con != exclusive_console)
1412 continue;
1413 if (!(con->flags & CON_ENABLED))
1414 continue;
1415 if (!con->write)
1416 continue;
1417 if (!cpu_online(smp_processor_id()) &&
1418 !(con->flags & CON_ANYTIME))
1419 continue;
1420 con->write(con, text, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
1424/*
1425 * Zap console related locks when oopsing. Only zap at most once
1426 * every 10 seconds, to leave time for slow consoles to print a
1427 * full oops.
1428 */
1429static void zap_locks(void)
1430{
1431 static unsigned long oops_timestamp;
1432
1433 if (time_after_eq(jiffies, oops_timestamp) &&
Jesper Juhl40dc5652005-10-30 15:02:46 -08001434 !time_after(jiffies, oops_timestamp + 30 * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return;
1436
1437 oops_timestamp = jiffies;
1438
Peter Zijlstra94d24fc2011-06-07 11:17:30 +02001439 debug_locks_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 /* If a crash is occurring, make sure we can't deadlock */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001441 raw_spin_lock_init(&logbuf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 /* And make sure that we print immediately */
Thomas Gleixner5b8c4f22010-09-07 14:33:43 +00001443 sema_init(&console_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
1445
Jan Kara608873c2014-06-04 16:11:35 -07001446/*
1447 * Check if we have any console that is capable of printing while cpu is
1448 * booting or shutting down. Requires console_sem.
1449 */
Michael Ellerman76a8ad292006-06-25 05:47:40 -07001450static int have_callable_console(void)
1451{
1452 struct console *con;
1453
Robin Getz4d091612009-07-01 21:08:37 -04001454 for_each_console(con)
Michael Ellerman76a8ad292006-06-25 05:47:40 -07001455 if (con->flags & CON_ANYTIME)
1456 return 1;
1457
1458 return 0;
1459}
1460
Linus Torvalds266c2e02008-03-24 19:25:08 -07001461/*
1462 * Can we actually use the console at this time on this cpu?
1463 *
Jan Kara5874af22014-08-06 16:09:10 -07001464 * Console drivers may assume that per-cpu resources have been allocated. So
1465 * unless they're explicitly marked as being able to cope (CON_ANYTIME) don't
1466 * call them until this CPU is officially up.
Linus Torvalds266c2e02008-03-24 19:25:08 -07001467 */
1468static inline int can_use_console(unsigned int cpu)
1469{
1470 return cpu_online(cpu) || have_callable_console();
1471}
1472
1473/*
1474 * Try to get console ownership to actually show the kernel
1475 * messages from a 'printk'. Return true (and with the
Torben Hohnac751ef2011-01-25 15:07:35 -08001476 * console_lock held, and 'console_locked' set) if it
Linus Torvalds266c2e02008-03-24 19:25:08 -07001477 * is successful, false otherwise.
Linus Torvalds266c2e02008-03-24 19:25:08 -07001478 */
Jan Kara5874af22014-08-06 16:09:10 -07001479static int console_trylock_for_printk(void)
Linus Torvalds266c2e02008-03-24 19:25:08 -07001480{
Jan Kara5874af22014-08-06 16:09:10 -07001481 unsigned int cpu = smp_processor_id();
1482
Jan Kara608873c2014-06-04 16:11:35 -07001483 if (!console_trylock())
1484 return 0;
1485 /*
1486 * If we can't use the console, we need to release the console
1487 * semaphore by hand to avoid flushing the buffer. We need to hold the
1488 * console semaphore in order to do this test safely.
1489 */
1490 if (!can_use_console(cpu)) {
1491 console_locked = 0;
Jan Karabd8d7cf2014-06-04 16:11:36 -07001492 up_console_sem();
Jan Kara608873c2014-06-04 16:11:35 -07001493 return 0;
1494 }
1495 return 1;
Linus Torvalds266c2e02008-03-24 19:25:08 -07001496}
Ingo Molnar32a76002008-01-25 21:07:58 +01001497
Dave Youngaf913222009-09-22 16:43:33 -07001498int printk_delay_msec __read_mostly;
1499
1500static inline void printk_delay(void)
1501{
1502 if (unlikely(printk_delay_msec)) {
1503 int m = printk_delay_msec;
1504
1505 while (m--) {
1506 mdelay(1);
1507 touch_nmi_watchdog();
1508 }
1509 }
1510}
1511
Kay Sievers084681d2012-06-28 09:38:53 +02001512/*
1513 * Continuation lines are buffered, and not committed to the record buffer
1514 * until the line is complete, or a race forces it. The line fragments
1515 * though, are printed immediately to the consoles to ensure everything has
1516 * reached the console in case of a kernel crash.
1517 */
1518static struct cont {
1519 char buf[LOG_LINE_MAX];
1520 size_t len; /* length == 0 means unused buffer */
1521 size_t cons; /* bytes written to console */
1522 struct task_struct *owner; /* task of first print*/
1523 u64 ts_nsec; /* time of first print */
1524 u8 level; /* log level of first message */
Alex Elder0b90fec2014-08-06 16:09:03 -07001525 u8 facility; /* log facility of first message */
Kay Sieverseab07262012-07-16 18:35:30 -07001526 enum log_flags flags; /* prefix, newline flags */
Kay Sievers084681d2012-06-28 09:38:53 +02001527 bool flushed:1; /* buffer sealed and committed */
1528} cont;
1529
Kay Sievers70498252012-07-16 18:35:29 -07001530static void cont_flush(enum log_flags flags)
Kay Sievers084681d2012-06-28 09:38:53 +02001531{
1532 if (cont.flushed)
1533 return;
1534 if (cont.len == 0)
1535 return;
1536
Kay Sieverseab07262012-07-16 18:35:30 -07001537 if (cont.cons) {
1538 /*
1539 * If a fragment of this line was directly flushed to the
1540 * console; wait for the console to pick up the rest of the
1541 * line. LOG_NOCONS suppresses a duplicated output.
1542 */
1543 log_store(cont.facility, cont.level, flags | LOG_NOCONS,
1544 cont.ts_nsec, NULL, 0, cont.buf, cont.len);
1545 cont.flags = flags;
1546 cont.flushed = true;
1547 } else {
1548 /*
1549 * If no fragment of this line ever reached the console,
1550 * just submit it to the store and free the buffer.
1551 */
1552 log_store(cont.facility, cont.level, flags, 0,
1553 NULL, 0, cont.buf, cont.len);
1554 cont.len = 0;
1555 }
Kay Sievers084681d2012-06-28 09:38:53 +02001556}
1557
1558static bool cont_add(int facility, int level, const char *text, size_t len)
1559{
1560 if (cont.len && cont.flushed)
1561 return false;
1562
1563 if (cont.len + len > sizeof(cont.buf)) {
Kay Sievers70498252012-07-16 18:35:29 -07001564 /* the line gets too long, split it up in separate records */
1565 cont_flush(LOG_CONT);
Kay Sievers084681d2012-06-28 09:38:53 +02001566 return false;
1567 }
1568
1569 if (!cont.len) {
1570 cont.facility = facility;
1571 cont.level = level;
1572 cont.owner = current;
1573 cont.ts_nsec = local_clock();
Kay Sieverseab07262012-07-16 18:35:30 -07001574 cont.flags = 0;
Kay Sievers084681d2012-06-28 09:38:53 +02001575 cont.cons = 0;
1576 cont.flushed = false;
1577 }
1578
1579 memcpy(cont.buf + cont.len, text, len);
1580 cont.len += len;
Kay Sieverseab07262012-07-16 18:35:30 -07001581
1582 if (cont.len > (sizeof(cont.buf) * 80) / 100)
1583 cont_flush(LOG_CONT);
1584
Kay Sievers084681d2012-06-28 09:38:53 +02001585 return true;
1586}
1587
1588static size_t cont_print_text(char *text, size_t size)
1589{
1590 size_t textlen = 0;
1591 size_t len;
1592
Kay Sieverseab07262012-07-16 18:35:30 -07001593 if (cont.cons == 0 && (console_prev & LOG_NEWLINE)) {
Kay Sievers084681d2012-06-28 09:38:53 +02001594 textlen += print_time(cont.ts_nsec, text);
1595 size -= textlen;
1596 }
1597
1598 len = cont.len - cont.cons;
1599 if (len > 0) {
1600 if (len+1 > size)
1601 len = size-1;
1602 memcpy(text + textlen, cont.buf + cont.cons, len);
1603 textlen += len;
1604 cont.cons = cont.len;
1605 }
1606
1607 if (cont.flushed) {
Kay Sieverseab07262012-07-16 18:35:30 -07001608 if (cont.flags & LOG_NEWLINE)
1609 text[textlen++] = '\n';
Kay Sievers084681d2012-06-28 09:38:53 +02001610 /* got everything, release buffer */
1611 cont.len = 0;
1612 }
1613 return textlen;
1614}
1615
Kay Sievers7ff95542012-05-03 02:29:13 +02001616asmlinkage int vprintk_emit(int facility, int level,
1617 const char *dict, size_t dictlen,
1618 const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
Kay Sievers7ff95542012-05-03 02:29:13 +02001620 static int recursion_bug;
Kay Sievers7ff95542012-05-03 02:29:13 +02001621 static char textbuf[LOG_LINE_MAX];
1622 char *text = textbuf;
Steven Rostedt458df9f2014-06-04 16:11:38 -07001623 size_t text_len = 0;
Kay Sievers5becfb12012-07-09 12:15:42 -07001624 enum log_flags lflags = 0;
Nick Andrewac60ad72008-05-12 21:21:04 +02001625 unsigned long flags;
Ingo Molnar32a76002008-01-25 21:07:58 +01001626 int this_cpu;
Kay Sievers7ff95542012-05-03 02:29:13 +02001627 int printed_len = 0;
Steven Rostedt458df9f2014-06-04 16:11:38 -07001628 bool in_sched = false;
Jan Kara608873c2014-06-04 16:11:35 -07001629 /* cpu currently holding logbuf_lock in this function */
1630 static volatile unsigned int logbuf_cpu = UINT_MAX;
1631
Steven Rostedt458df9f2014-06-04 16:11:38 -07001632 if (level == SCHED_MESSAGE_LOGLEVEL) {
1633 level = -1;
1634 in_sched = true;
1635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Andrew Cooks2fa72c82012-12-17 15:59:56 -08001637 boot_delay_msec(level);
Dave Youngaf913222009-09-22 16:43:33 -07001638 printk_delay();
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 /* This stops the holder of console_sem just where we want him */
Peter Zijlstra1a9a8ae2011-06-07 11:17:30 +02001641 local_irq_save(flags);
Ingo Molnar32a76002008-01-25 21:07:58 +01001642 this_cpu = smp_processor_id();
1643
1644 /*
1645 * Ouch, printk recursed into itself!
1646 */
Kay Sievers7ff95542012-05-03 02:29:13 +02001647 if (unlikely(logbuf_cpu == this_cpu)) {
Ingo Molnar32a76002008-01-25 21:07:58 +01001648 /*
1649 * If a crash is occurring during printk() on this CPU,
1650 * then try to get the crash message out but make sure
1651 * we can't deadlock. Otherwise just return to avoid the
1652 * recursion and return - but flag the recursion so that
1653 * it can be printed at the next appropriate moment:
1654 */
Peter Zijlstra94d24fc2011-06-07 11:17:30 +02001655 if (!oops_in_progress && !lockdep_recursing(current)) {
Tejun Heo3b8945e2008-05-12 21:21:04 +02001656 recursion_bug = 1;
Jan Kara5874af22014-08-06 16:09:10 -07001657 local_irq_restore(flags);
1658 return 0;
Ingo Molnar32a76002008-01-25 21:07:58 +01001659 }
1660 zap_locks();
1661 }
1662
Ingo Molnara0f1ccf2006-07-03 00:24:58 -07001663 lockdep_off();
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001664 raw_spin_lock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02001665 logbuf_cpu = this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Patrick Palka000a7d62014-09-09 14:50:48 -07001667 if (unlikely(recursion_bug)) {
Kay Sievers7ff95542012-05-03 02:29:13 +02001668 static const char recursion_msg[] =
1669 "BUG: recent printk recursion!";
1670
Tejun Heo3b8945e2008-05-12 21:21:04 +02001671 recursion_bug = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001672 /* emit KERN_CRIT message */
Petr Mladek034633c2014-06-04 16:11:33 -07001673 printed_len += log_store(0, 2, LOG_PREFIX|LOG_NEWLINE, 0,
Patrick Palka000a7d62014-09-09 14:50:48 -07001674 NULL, 0, recursion_msg,
1675 strlen(recursion_msg));
Linus Torvalds5fd29d62009-06-16 10:57:02 -07001676 }
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 /*
Kay Sievers7ff95542012-05-03 02:29:13 +02001679 * The printf needs to come first; we need the syslog
1680 * prefix which might be passed-in as a parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 */
Markus Trippelsdorf98e35f52014-10-13 15:51:13 -07001682 text_len = vscnprintf(text, sizeof(textbuf), 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
Andi Kleen722a9f92014-05-02 00:44:38 +02001884asmlinkage __visible void early_printk(const char *fmt, ...)
Thomas Gleixnerd0380e62013-04-29 16:17:18 -07001885{
1886 va_list ap;
Joe Perches1dc62442014-12-10 15:45:53 -08001887 char buf[512];
1888 int n;
1889
1890 if (!early_console)
1891 return;
Thomas Gleixnerd0380e62013-04-29 16:17:18 -07001892
1893 va_start(ap, fmt);
Joe Perches1dc62442014-12-10 15:45:53 -08001894 n = vscnprintf(buf, sizeof(buf), fmt, ap);
Thomas Gleixnerd0380e62013-04-29 16:17:18 -07001895 va_end(ap);
Joe Perches1dc62442014-12-10 15:45:53 -08001896
1897 early_console->write(early_console, buf, n);
Thomas Gleixnerd0380e62013-04-29 16:17:18 -07001898}
1899#endif
1900
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001901static int __add_preferred_console(char *name, int idx, char *options,
1902 char *brl_options)
1903{
1904 struct console_cmdline *c;
1905 int i;
1906
1907 /*
1908 * See if this tty is not yet registered, and
1909 * if we have a slot free.
1910 */
Joe Perches23475402013-07-31 13:53:46 -07001911 for (i = 0, c = console_cmdline;
1912 i < MAX_CMDLINECONSOLES && c->name[0];
1913 i++, c++) {
1914 if (strcmp(c->name, name) == 0 && c->index == idx) {
1915 if (!brl_options)
1916 selected_console = i;
1917 return 0;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001918 }
Joe Perches23475402013-07-31 13:53:46 -07001919 }
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001920 if (i == MAX_CMDLINECONSOLES)
1921 return -E2BIG;
1922 if (!brl_options)
1923 selected_console = i;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001924 strlcpy(c->name, name, sizeof(c->name));
1925 c->options = options;
Joe Perchesbbeddf52013-07-31 13:53:45 -07001926 braille_set_options(c, brl_options);
1927
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001928 c->index = idx;
1929 return 0;
1930}
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001931/*
Alex Elder0b90fec2014-08-06 16:09:03 -07001932 * Set up a console. Called via do_early_param() in init/main.c
1933 * for each "console=" parameter in the boot command line.
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001934 */
1935static int __init console_setup(char *str)
1936{
Alex Elder0b90fec2014-08-06 16:09:03 -07001937 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for "ttyS" */
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001938 char *s, *options, *brl_options = NULL;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001939 int idx;
1940
Joe Perchesbbeddf52013-07-31 13:53:45 -07001941 if (_braille_console_setup(&str, &brl_options))
1942 return 1;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001943
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001944 /*
1945 * Decode str into name, index, options.
1946 */
1947 if (str[0] >= '0' && str[0] <= '9') {
Yinghai Lueaa944a2007-07-15 23:37:27 -07001948 strcpy(buf, "ttyS");
1949 strncpy(buf + 4, str, sizeof(buf) - 5);
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001950 } else {
Yinghai Lueaa944a2007-07-15 23:37:27 -07001951 strncpy(buf, str, sizeof(buf) - 1);
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001952 }
Yinghai Lueaa944a2007-07-15 23:37:27 -07001953 buf[sizeof(buf) - 1] = 0;
Alex Elder249771b2014-08-06 16:09:08 -07001954 options = strchr(str, ',');
1955 if (options)
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001956 *(options++) = 0;
1957#ifdef __sparc__
1958 if (!strcmp(str, "ttya"))
Yinghai Lueaa944a2007-07-15 23:37:27 -07001959 strcpy(buf, "ttyS0");
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001960 if (!strcmp(str, "ttyb"))
Yinghai Lueaa944a2007-07-15 23:37:27 -07001961 strcpy(buf, "ttyS1");
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001962#endif
Yinghai Lueaa944a2007-07-15 23:37:27 -07001963 for (s = buf; *s; s++)
Alex Elder249771b2014-08-06 16:09:08 -07001964 if (isdigit(*s) || *s == ',')
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001965 break;
1966 idx = simple_strtoul(s, NULL, 10);
1967 *s = 0;
1968
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001969 __add_preferred_console(buf, idx, options, brl_options);
Markus Armbruster9e124fe2008-05-26 23:31:07 +01001970 console_set_on_cmdline = 1;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001971 return 1;
1972}
1973__setup("console=", console_setup);
1974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975/**
Matt Mackall3c0547b2005-05-16 21:53:47 -07001976 * add_preferred_console - add a device to the list of preferred consoles.
Martin Waitzddad86c2005-11-13 16:08:14 -08001977 * @name: device name
1978 * @idx: device index
1979 * @options: options for this console
Matt Mackall3c0547b2005-05-16 21:53:47 -07001980 *
1981 * The last preferred console added will be used for kernel messages
1982 * and stdin/out/err for init. Normally this is used by console_setup
1983 * above to handle user-supplied console arguments; however it can also
1984 * be used by arch-specific code either to override the user or more
1985 * commonly to provide a default console (ie from PROM variables) when
1986 * the user has not supplied one.
1987 */
David S. Millerfb445ee2007-12-29 01:19:49 -08001988int add_preferred_console(char *name, int idx, char *options)
Matt Mackall3c0547b2005-05-16 21:53:47 -07001989{
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001990 return __add_preferred_console(name, idx, options, NULL);
Matt Mackall3c0547b2005-05-16 21:53:47 -07001991}
1992
Daniel Ritzb6b1d872007-08-03 16:07:43 +02001993int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
Yinghai Lu18a8bd92007-07-15 23:37:59 -07001994{
1995 struct console_cmdline *c;
1996 int i;
1997
Joe Perches23475402013-07-31 13:53:46 -07001998 for (i = 0, c = console_cmdline;
1999 i < MAX_CMDLINECONSOLES && c->name[0];
2000 i++, c++)
2001 if (strcmp(c->name, name) == 0 && c->index == idx) {
2002 strlcpy(c->name, name_new, sizeof(c->name));
Joe Perches23475402013-07-31 13:53:46 -07002003 c->options = options;
2004 c->index = idx_new;
2005 return i;
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002006 }
2007 /* not found */
2008 return -1;
2009}
2010
Neil Zhangd25d9fe2014-08-06 16:09:12 -07002011bool console_suspend_enabled = true;
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002012EXPORT_SYMBOL(console_suspend_enabled);
2013
2014static int __init console_suspend_disable(char *str)
2015{
Neil Zhangd25d9fe2014-08-06 16:09:12 -07002016 console_suspend_enabled = false;
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002017 return 1;
2018}
2019__setup("no_console_suspend", console_suspend_disable);
Yanmin Zhang134620f2011-10-31 17:11:27 -07002020module_param_named(console_suspend, console_suspend_enabled,
2021 bool, S_IRUGO | S_IWUSR);
2022MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
2023 " and hibernate operations");
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002024
Matt Mackall3c0547b2005-05-16 21:53:47 -07002025/**
Linus Torvalds557240b2006-06-19 18:16:01 -07002026 * suspend_console - suspend the console subsystem
2027 *
2028 * This disables printk() while we go into suspend states
2029 */
2030void suspend_console(void)
2031{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002032 if (!console_suspend_enabled)
2033 return;
Pavel Machek0d630812008-07-23 21:28:32 -07002034 printk("Suspending console(s) (use no_console_suspend to debug)\n");
Torben Hohnac751ef2011-01-25 15:07:35 -08002035 console_lock();
Linus Torvalds557240b2006-06-19 18:16:01 -07002036 console_suspended = 1;
Jan Karabd8d7cf2014-06-04 16:11:36 -07002037 up_console_sem();
Linus Torvalds557240b2006-06-19 18:16:01 -07002038}
2039
2040void resume_console(void)
2041{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002042 if (!console_suspend_enabled)
2043 return;
Jan Karabd8d7cf2014-06-04 16:11:36 -07002044 down_console_sem();
Linus Torvalds557240b2006-06-19 18:16:01 -07002045 console_suspended = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08002046 console_unlock();
Linus Torvalds557240b2006-06-19 18:16:01 -07002047}
2048
2049/**
Kevin Cernekee034260d2010-06-03 22:11:25 -07002050 * console_cpu_notify - print deferred console messages after CPU hotplug
2051 * @self: notifier struct
2052 * @action: CPU hotplug event
2053 * @hcpu: unused
2054 *
2055 * If printk() is called from a CPU that is not online yet, the messages
2056 * will be spooled but will not show up on the console. This function is
2057 * called when a new CPU comes online (or fails to come up), and ensures
2058 * that any such output gets printed.
2059 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04002060static int console_cpu_notify(struct notifier_block *self,
Kevin Cernekee034260d2010-06-03 22:11:25 -07002061 unsigned long action, void *hcpu)
2062{
2063 switch (action) {
2064 case CPU_ONLINE:
2065 case CPU_DEAD:
Kevin Cernekee034260d2010-06-03 22:11:25 -07002066 case CPU_DOWN_FAILED:
2067 case CPU_UP_CANCELED:
Torben Hohnac751ef2011-01-25 15:07:35 -08002068 console_lock();
2069 console_unlock();
Kevin Cernekee034260d2010-06-03 22:11:25 -07002070 }
2071 return NOTIFY_OK;
2072}
2073
2074/**
Torben Hohnac751ef2011-01-25 15:07:35 -08002075 * console_lock - lock the console system for exclusive use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002077 * Acquires a lock which guarantees that the caller has
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 * exclusive access to the console system and the console_drivers list.
2079 *
2080 * Can sleep, returns nothing.
2081 */
Torben Hohnac751ef2011-01-25 15:07:35 -08002082void console_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
Daniel Vetter6b898c02012-09-17 23:03:31 +00002084 might_sleep();
2085
Jan Karabd8d7cf2014-06-04 16:11:36 -07002086 down_console_sem();
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01002087 if (console_suspended)
2088 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 console_locked = 1;
2090 console_may_schedule = 1;
2091}
Torben Hohnac751ef2011-01-25 15:07:35 -08002092EXPORT_SYMBOL(console_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Torben Hohnac751ef2011-01-25 15:07:35 -08002094/**
2095 * console_trylock - try to lock the console system for exclusive use.
2096 *
Alex Elder0b90fec2014-08-06 16:09:03 -07002097 * Try to acquire a lock which guarantees that the caller has exclusive
2098 * access to the console system and the console_drivers list.
Torben Hohnac751ef2011-01-25 15:07:35 -08002099 *
2100 * returns 1 on success, and 0 on failure to acquire the lock.
2101 */
2102int console_trylock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
Jan Karabd8d7cf2014-06-04 16:11:36 -07002104 if (down_trylock_console_sem())
Torben Hohnac751ef2011-01-25 15:07:35 -08002105 return 0;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01002106 if (console_suspended) {
Jan Karabd8d7cf2014-06-04 16:11:36 -07002107 up_console_sem();
Torben Hohnac751ef2011-01-25 15:07:35 -08002108 return 0;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01002109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 console_locked = 1;
2111 console_may_schedule = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08002112 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113}
Torben Hohnac751ef2011-01-25 15:07:35 -08002114EXPORT_SYMBOL(console_trylock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116int is_console_locked(void)
2117{
2118 return console_locked;
2119}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Kay Sieverseab07262012-07-16 18:35:30 -07002121static void console_cont_flush(char *text, size_t size)
2122{
2123 unsigned long flags;
2124 size_t len;
2125
2126 raw_spin_lock_irqsave(&logbuf_lock, flags);
2127
2128 if (!cont.len)
2129 goto out;
2130
2131 /*
2132 * We still queue earlier records, likely because the console was
2133 * busy. The earlier ones need to be printed before this one, we
2134 * did not flush any fragment so far, so just let it queue up.
2135 */
2136 if (console_seq < log_next_seq && !cont.cons)
2137 goto out;
2138
2139 len = cont_print_text(text, size);
2140 raw_spin_unlock(&logbuf_lock);
2141 stop_critical_timings();
2142 call_console_drivers(cont.level, text, len);
2143 start_critical_timings();
2144 local_irq_restore(flags);
2145 return;
2146out:
2147 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2148}
Kay Sievers7ff95542012-05-03 02:29:13 +02002149
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150/**
Torben Hohnac751ef2011-01-25 15:07:35 -08002151 * console_unlock - unlock the console system
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002153 * Releases the console_lock which the caller holds on the console system
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 * and the console driver list.
2155 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002156 * While the console_lock was held, console output may have been buffered
2157 * by printk(). If this is the case, console_unlock(); emits
2158 * the output prior to releasing the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 *
Kay Sievers7f3a7812012-05-09 01:37:51 +02002160 * If there is output waiting, we wake /dev/kmsg and syslog() users.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002162 * console_unlock(); may be called from any context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 */
Torben Hohnac751ef2011-01-25 15:07:35 -08002164void console_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
Kay Sievers70498252012-07-16 18:35:29 -07002166 static char text[LOG_LINE_MAX + PREFIX_MAX];
Kay Sievers7ff95542012-05-03 02:29:13 +02002167 static u64 seen_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 unsigned long flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02002169 bool wake_klogd = false;
2170 bool retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Linus Torvalds557240b2006-06-19 18:16:01 -07002172 if (console_suspended) {
Jan Karabd8d7cf2014-06-04 16:11:36 -07002173 up_console_sem();
Linus Torvalds557240b2006-06-19 18:16:01 -07002174 return;
2175 }
Antonino A. Daplas78944e542006-08-05 12:14:16 -07002176
2177 console_may_schedule = 0;
2178
Kay Sievers084681d2012-06-28 09:38:53 +02002179 /* flush buffered message fragment immediately to console */
Kay Sieverseab07262012-07-16 18:35:30 -07002180 console_cont_flush(text, sizeof(text));
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002181again:
Kay Sievers7ff95542012-05-03 02:29:13 +02002182 for (;;) {
Joe Perches62e32ac2013-07-31 13:53:47 -07002183 struct printk_log *msg;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02002184 size_t len;
Kay Sievers7ff95542012-05-03 02:29:13 +02002185 int level;
2186
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002187 raw_spin_lock_irqsave(&logbuf_lock, flags);
Kay Sievers7ff95542012-05-03 02:29:13 +02002188 if (seen_seq != log_next_seq) {
2189 wake_klogd = true;
2190 seen_seq = log_next_seq;
2191 }
2192
2193 if (console_seq < log_first_seq) {
Will Deacon84b5ec82014-06-04 16:11:45 -07002194 len = sprintf(text, "** %u printk messages dropped ** ",
2195 (unsigned)(log_first_seq - console_seq));
2196
Kay Sievers7ff95542012-05-03 02:29:13 +02002197 /* messages are gone, move to first one */
2198 console_seq = log_first_seq;
2199 console_idx = log_first_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002200 console_prev = 0;
Will Deacon84b5ec82014-06-04 16:11:45 -07002201 } else {
2202 len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02002203 }
Kay Sievers084681d2012-06-28 09:38:53 +02002204skip:
Kay Sievers7ff95542012-05-03 02:29:13 +02002205 if (console_seq == log_next_seq)
2206 break;
2207
2208 msg = log_from_idx(console_idx);
Kay Sievers084681d2012-06-28 09:38:53 +02002209 if (msg->flags & LOG_NOCONS) {
2210 /*
2211 * Skip record we have buffered and already printed
2212 * directly to the console when we received it.
2213 */
2214 console_idx = log_next(console_idx);
2215 console_seq++;
Kay Sievers68b65072012-07-06 09:50:09 -07002216 /*
2217 * We will get here again when we register a new
2218 * CON_PRINTBUFFER console. Clear the flag so we
2219 * will properly dump everything later.
2220 */
2221 msg->flags &= ~LOG_NOCONS;
Kay Sieverseab07262012-07-16 18:35:30 -07002222 console_prev = msg->flags;
Kay Sievers084681d2012-06-28 09:38:53 +02002223 goto skip;
2224 }
Kay Sievers649e6ee2012-05-10 04:30:45 +02002225
Kay Sievers084681d2012-06-28 09:38:53 +02002226 level = msg->level;
Will Deacon84b5ec82014-06-04 16:11:45 -07002227 len += msg_print_text(msg, console_prev, false,
2228 text + len, sizeof(text) - len);
Kay Sievers7ff95542012-05-03 02:29:13 +02002229 console_idx = log_next(console_idx);
2230 console_seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07002231 console_prev = msg->flags;
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002232 raw_spin_unlock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02002233
Steven Rostedt81d68a92008-05-12 21:20:42 +02002234 stop_critical_timings(); /* don't trace print latency */
Kay Sievers7ff95542012-05-03 02:29:13 +02002235 call_console_drivers(level, text, len);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002236 start_critical_timings();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 local_irq_restore(flags);
2238 }
2239 console_locked = 0;
Feng Tangfe3d8ad2011-03-22 16:34:21 -07002240
2241 /* Release the exclusive_console once it is used */
2242 if (unlikely(exclusive_console))
2243 exclusive_console = NULL;
2244
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002245 raw_spin_unlock(&logbuf_lock);
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002246
Jan Karabd8d7cf2014-06-04 16:11:36 -07002247 up_console_sem();
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002248
2249 /*
2250 * Someone could have filled up the buffer again, so re-check if there's
2251 * something to flush. In case we cannot trylock the console_sem again,
2252 * there's a new owner and the console_unlock() from them will do the
2253 * flush, no worries.
2254 */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002255 raw_spin_lock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02002256 retry = console_seq != log_next_seq;
Peter Zijlstra09dc3cf2011-12-08 14:34:13 -08002257 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2258
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002259 if (retry && console_trylock())
2260 goto again;
2261
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08002262 if (wake_klogd)
2263 wake_up_klogd();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264}
Torben Hohnac751ef2011-01-25 15:07:35 -08002265EXPORT_SYMBOL(console_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Martin Waitzddad86c2005-11-13 16:08:14 -08002267/**
2268 * console_conditional_schedule - yield the CPU if required
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 *
2270 * If the console code is currently allowed to sleep, and
2271 * if this CPU should yield the CPU to another task, do
2272 * so here.
2273 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002274 * Must be called within console_lock();.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 */
2276void __sched console_conditional_schedule(void)
2277{
2278 if (console_may_schedule)
2279 cond_resched();
2280}
2281EXPORT_SYMBOL(console_conditional_schedule);
2282
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283void console_unblank(void)
2284{
2285 struct console *c;
2286
2287 /*
2288 * console_unblank can no longer be called in interrupt context unless
2289 * oops_in_progress is set to 1..
2290 */
2291 if (oops_in_progress) {
Jan Karabd8d7cf2014-06-04 16:11:36 -07002292 if (down_trylock_console_sem() != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 return;
2294 } else
Torben Hohnac751ef2011-01-25 15:07:35 -08002295 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
2297 console_locked = 1;
2298 console_may_schedule = 0;
Robin Getz4d091612009-07-01 21:08:37 -04002299 for_each_console(c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 if ((c->flags & CON_ENABLED) && c->unblank)
2301 c->unblank();
Torben Hohnac751ef2011-01-25 15:07:35 -08002302 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
2305/*
2306 * Return the console tty driver structure and its associated index
2307 */
2308struct tty_driver *console_device(int *index)
2309{
2310 struct console *c;
2311 struct tty_driver *driver = NULL;
2312
Torben Hohnac751ef2011-01-25 15:07:35 -08002313 console_lock();
Robin Getz4d091612009-07-01 21:08:37 -04002314 for_each_console(c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 if (!c->device)
2316 continue;
2317 driver = c->device(c, index);
2318 if (driver)
2319 break;
2320 }
Torben Hohnac751ef2011-01-25 15:07:35 -08002321 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return driver;
2323}
2324
2325/*
2326 * Prevent further output on the passed console device so that (for example)
2327 * serial drivers can disable console output before suspending a port, and can
2328 * re-enable output afterwards.
2329 */
2330void console_stop(struct console *console)
2331{
Torben Hohnac751ef2011-01-25 15:07:35 -08002332 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 console->flags &= ~CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08002334 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335}
2336EXPORT_SYMBOL(console_stop);
2337
2338void console_start(struct console *console)
2339{
Torben Hohnac751ef2011-01-25 15:07:35 -08002340 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 console->flags |= CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08002342 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344EXPORT_SYMBOL(console_start);
2345
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07002346static int __read_mostly keep_bootcon;
2347
2348static int __init keep_bootcon_setup(char *str)
2349{
2350 keep_bootcon = 1;
Andrew Morton27083ba2013-11-12 15:08:50 -08002351 pr_info("debug: skip boot console de-registration.\n");
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07002352
2353 return 0;
2354}
2355
2356early_param("keep_bootcon", keep_bootcon_setup);
2357
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358/*
2359 * The console driver calls this routine during kernel initialization
2360 * to register the console printing procedure with printk() and to
2361 * print any messages that were printed by the kernel before the
2362 * console driver was initialized.
Robin Getz4d091612009-07-01 21:08:37 -04002363 *
2364 * This can happen pretty early during the boot process (because of
2365 * early_printk) - sometimes before setup_arch() completes - be careful
2366 * of what kernel features are used - they may not be initialised yet.
2367 *
2368 * There are two types of consoles - bootconsoles (early_printk) and
2369 * "real" consoles (everything which is not a bootconsole) which are
2370 * handled differently.
2371 * - Any number of bootconsoles can be registered at any time.
2372 * - As soon as a "real" console is registered, all bootconsoles
2373 * will be unregistered automatically.
2374 * - Once a "real" console is registered, any attempt to register a
2375 * bootconsoles will be rejected
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 */
Robin Getz4d091612009-07-01 21:08:37 -04002377void register_console(struct console *newcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378{
Jesper Juhl40dc5652005-10-30 15:02:46 -08002379 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 unsigned long flags;
Robin Getz4d091612009-07-01 21:08:37 -04002381 struct console *bcon = NULL;
Joe Perches23475402013-07-31 13:53:46 -07002382 struct console_cmdline *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Andreas Bießmann16cf48a2013-08-02 12:23:34 +02002384 if (console_drivers)
2385 for_each_console(bcon)
2386 if (WARN(bcon == newcon,
2387 "console '%s%d' already registered\n",
2388 bcon->name, bcon->index))
2389 return;
2390
Robin Getz4d091612009-07-01 21:08:37 -04002391 /*
2392 * before we register a new CON_BOOT console, make sure we don't
2393 * already have a valid console
2394 */
2395 if (console_drivers && newcon->flags & CON_BOOT) {
2396 /* find the last or real console */
2397 for_each_console(bcon) {
2398 if (!(bcon->flags & CON_BOOT)) {
Andrew Morton27083ba2013-11-12 15:08:50 -08002399 pr_info("Too late to register bootconsole %s%d\n",
Robin Getz4d091612009-07-01 21:08:37 -04002400 newcon->name, newcon->index);
2401 return;
2402 }
2403 }
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002404 }
2405
Robin Getz4d091612009-07-01 21:08:37 -04002406 if (console_drivers && console_drivers->flags & CON_BOOT)
2407 bcon = console_drivers;
2408
2409 if (preferred_console < 0 || bcon || !console_drivers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 preferred_console = selected_console;
2411
Robin Getz4d091612009-07-01 21:08:37 -04002412 if (newcon->early_setup)
2413 newcon->early_setup();
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 /*
2416 * See if we want to use this console driver. If we
2417 * didn't select a console we take the first one
2418 * that registers here.
2419 */
2420 if (preferred_console < 0) {
Robin Getz4d091612009-07-01 21:08:37 -04002421 if (newcon->index < 0)
2422 newcon->index = 0;
2423 if (newcon->setup == NULL ||
2424 newcon->setup(newcon, NULL) == 0) {
2425 newcon->flags |= CON_ENABLED;
2426 if (newcon->device) {
2427 newcon->flags |= CON_CONSDEV;
Jan Kiszkacd3a1b82008-05-12 21:21:04 +02002428 preferred_console = 0;
2429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 }
2431 }
2432
2433 /*
2434 * See if this console matches one we selected on
2435 * the command line.
2436 */
Joe Perches23475402013-07-31 13:53:46 -07002437 for (i = 0, c = console_cmdline;
2438 i < MAX_CMDLINECONSOLES && c->name[0];
2439 i++, c++) {
2440 if (strcmp(c->name, newcon->name) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 continue;
Robin Getz4d091612009-07-01 21:08:37 -04002442 if (newcon->index >= 0 &&
Joe Perches23475402013-07-31 13:53:46 -07002443 newcon->index != c->index)
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;
Joe Perchesbbeddf52013-07-31 13:53:45 -07002447
Joe Perches23475402013-07-31 13:53:46 -07002448 if (_braille_register_console(newcon, c))
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002449 return;
Joe Perchesbbeddf52013-07-31 13:53:45 -07002450
Robin Getz4d091612009-07-01 21:08:37 -04002451 if (newcon->setup &&
2452 newcon->setup(newcon, console_cmdline[i].options) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 break;
Robin Getz4d091612009-07-01 21:08:37 -04002454 newcon->flags |= CON_ENABLED;
Joe Perches23475402013-07-31 13:53:46 -07002455 newcon->index = c->index;
Greg Edwardsab4af032005-06-23 00:09:05 -07002456 if (i == selected_console) {
Robin Getz4d091612009-07-01 21:08:37 -04002457 newcon->flags |= CON_CONSDEV;
Greg Edwardsab4af032005-06-23 00:09:05 -07002458 preferred_console = selected_console;
2459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 break;
2461 }
2462
Robin Getz4d091612009-07-01 21:08:37 -04002463 if (!(newcon->flags & CON_ENABLED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 return;
2465
Robin Getz8259cf42009-07-09 13:08:37 -04002466 /*
2467 * If we have a bootconsole, and are switching to a real console,
2468 * don't print everything out again, since when the boot console, and
2469 * the real console are the same physical device, it's annoying to
2470 * see the beginning boot messages twice
2471 */
2472 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
Robin Getz4d091612009-07-01 21:08:37 -04002473 newcon->flags &= ~CON_PRINTBUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
2475 /*
2476 * Put this console in the list - keep the
2477 * preferred driver at the head of the list.
2478 */
Torben Hohnac751ef2011-01-25 15:07:35 -08002479 console_lock();
Robin Getz4d091612009-07-01 21:08:37 -04002480 if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
2481 newcon->next = console_drivers;
2482 console_drivers = newcon;
2483 if (newcon->next)
2484 newcon->next->flags &= ~CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 } else {
Robin Getz4d091612009-07-01 21:08:37 -04002486 newcon->next = console_drivers->next;
2487 console_drivers->next = newcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
Robin Getz4d091612009-07-01 21:08:37 -04002489 if (newcon->flags & CON_PRINTBUFFER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 /*
Torben Hohnac751ef2011-01-25 15:07:35 -08002491 * console_unlock(); will print out the buffered messages
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 * for us.
2493 */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002494 raw_spin_lock_irqsave(&logbuf_lock, flags);
Kay Sievers7ff95542012-05-03 02:29:13 +02002495 console_seq = syslog_seq;
2496 console_idx = syslog_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002497 console_prev = syslog_prev;
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002498 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
Feng Tangfe3d8ad2011-03-22 16:34:21 -07002499 /*
2500 * We're about to replay the log buffer. Only do this to the
2501 * just-registered console to avoid excessive message spam to
2502 * the already-registered consoles.
2503 */
2504 exclusive_console = newcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 }
Torben Hohnac751ef2011-01-25 15:07:35 -08002506 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01002507 console_sysfs_notify();
Robin Getz8259cf42009-07-09 13:08:37 -04002508
2509 /*
2510 * By unregistering the bootconsoles after we enable the real console
2511 * we get the "console xxx enabled" message on all the consoles -
2512 * boot consoles, real consoles, etc - this is to ensure that end
2513 * users know there might be something in the kernel's log buffer that
2514 * went to the bootconsole (that they do not see on the real console)
2515 */
Andrew Morton27083ba2013-11-12 15:08:50 -08002516 pr_info("%sconsole [%s%d] enabled\n",
Kees Cook6b802392013-11-12 15:08:49 -08002517 (newcon->flags & CON_BOOT) ? "boot" : "" ,
2518 newcon->name, newcon->index);
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07002519 if (bcon &&
2520 ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
2521 !keep_bootcon) {
Kees Cook6b802392013-11-12 15:08:49 -08002522 /* We need to iterate through all boot consoles, to make
2523 * sure we print everything out, before we unregister them.
Robin Getz8259cf42009-07-09 13:08:37 -04002524 */
Robin Getz8259cf42009-07-09 13:08:37 -04002525 for_each_console(bcon)
2526 if (bcon->flags & CON_BOOT)
2527 unregister_console(bcon);
Robin Getz8259cf42009-07-09 13:08:37 -04002528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529}
2530EXPORT_SYMBOL(register_console);
2531
Jesper Juhl40dc5652005-10-30 15:02:46 -08002532int unregister_console(struct console *console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533{
Jesper Juhl40dc5652005-10-30 15:02:46 -08002534 struct console *a, *b;
Joe Perchesbbeddf52013-07-31 13:53:45 -07002535 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
Andrew Morton27083ba2013-11-12 15:08:50 -08002537 pr_info("%sconsole [%s%d] disabled\n",
Kees Cook6b802392013-11-12 15:08:49 -08002538 (console->flags & CON_BOOT) ? "boot" : "" ,
2539 console->name, console->index);
2540
Joe Perchesbbeddf52013-07-31 13:53:45 -07002541 res = _braille_unregister_console(console);
2542 if (res)
2543 return res;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002544
Joe Perchesbbeddf52013-07-31 13:53:45 -07002545 res = 1;
Torben Hohnac751ef2011-01-25 15:07:35 -08002546 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 if (console_drivers == console) {
2548 console_drivers=console->next;
2549 res = 0;
Benjamin Herrenschmidte9b15b52005-11-23 13:37:44 -08002550 } else if (console_drivers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 for (a=console_drivers->next, b=console_drivers ;
2552 a; b=a, a=b->next) {
2553 if (a == console) {
2554 b->next = a->next;
2555 res = 0;
2556 break;
Jesper Juhl40dc5652005-10-30 15:02:46 -08002557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 }
2559 }
Jesper Juhl40dc5652005-10-30 15:02:46 -08002560
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002561 /*
Greg Edwardsab4af032005-06-23 00:09:05 -07002562 * If this isn't the last console and it has CON_CONSDEV set, we
2563 * need to set it on the next preferred console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 */
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002565 if (console_drivers != NULL && console->flags & CON_CONSDEV)
Greg Edwardsab4af032005-06-23 00:09:05 -07002566 console_drivers->flags |= CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Stephen Chivers7fa21dd2014-05-14 08:04:39 +10002568 console->flags &= ~CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08002569 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01002570 console_sysfs_notify();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 return res;
2572}
2573EXPORT_SYMBOL(unregister_console);
Matt Mackalld59745c2005-05-01 08:59:02 -07002574
Kevin Cernekee034260d2010-06-03 22:11:25 -07002575static int __init printk_late_init(void)
Robin Getz0c5564b2007-08-20 15:22:47 -04002576{
Robin Getz4d091612009-07-01 21:08:37 -04002577 struct console *con;
2578
2579 for_each_console(con) {
Nishanth Aravamudan4c30c6f2011-08-25 15:59:11 -07002580 if (!keep_bootcon && con->flags & CON_BOOT) {
Sonic Zhang42c2c8c2009-08-06 15:58:11 -07002581 unregister_console(con);
Robin Getzcb00e992007-08-21 23:14:58 -04002582 }
Robin Getz0c5564b2007-08-20 15:22:47 -04002583 }
Kevin Cernekee034260d2010-06-03 22:11:25 -07002584 hotcpu_notifier(console_cpu_notify, 0);
Robin Getz0c5564b2007-08-20 15:22:47 -04002585 return 0;
2586}
Kevin Cernekee034260d2010-06-03 22:11:25 -07002587late_initcall(printk_late_init);
Robin Getz0c5564b2007-08-20 15:22:47 -04002588
Joe Perches7ef3d2f2008-02-08 04:21:25 -08002589#if defined CONFIG_PRINTK
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002590/*
2591 * Delayed printk version, for scheduler-internal messages:
2592 */
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002593#define PRINTK_PENDING_WAKEUP 0x01
Steven Rostedt458df9f2014-06-04 16:11:38 -07002594#define PRINTK_PENDING_OUTPUT 0x02
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002595
2596static DEFINE_PER_CPU(int, printk_pending);
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002597
2598static void wake_up_klogd_work_func(struct irq_work *irq_work)
2599{
2600 int pending = __this_cpu_xchg(printk_pending, 0);
2601
Steven Rostedt458df9f2014-06-04 16:11:38 -07002602 if (pending & PRINTK_PENDING_OUTPUT) {
2603 /* If trylock fails, someone else is doing the printing */
2604 if (console_trylock())
2605 console_unlock();
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002606 }
2607
2608 if (pending & PRINTK_PENDING_WAKEUP)
2609 wake_up_interruptible(&log_wait);
2610}
2611
2612static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) = {
2613 .func = wake_up_klogd_work_func,
2614 .flags = IRQ_WORK_LAZY,
2615};
2616
2617void wake_up_klogd(void)
2618{
2619 preempt_disable();
2620 if (waitqueue_active(&log_wait)) {
2621 this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP);
Christoph Lameterbb964a92014-08-17 12:30:24 -05002622 irq_work_queue(this_cpu_ptr(&wake_up_klogd_work));
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002623 }
2624 preempt_enable();
2625}
Dave Young717115e2008-07-25 01:45:58 -07002626
John Stultzaac74dc2014-06-04 16:11:40 -07002627int printk_deferred(const char *fmt, ...)
Peter Zijlstra600e1452012-03-15 12:35:37 +01002628{
Peter Zijlstra600e1452012-03-15 12:35:37 +01002629 va_list args;
Peter Zijlstra600e1452012-03-15 12:35:37 +01002630 int r;
2631
John Stultz81954602014-06-04 16:11:39 -07002632 preempt_disable();
Peter Zijlstra600e1452012-03-15 12:35:37 +01002633 va_start(args, fmt);
Steven Rostedt458df9f2014-06-04 16:11:38 -07002634 r = vprintk_emit(0, SCHED_MESSAGE_LOGLEVEL, NULL, 0, fmt, args);
Peter Zijlstra600e1452012-03-15 12:35:37 +01002635 va_end(args);
2636
Steven Rostedt458df9f2014-06-04 16:11:38 -07002637 __this_cpu_or(printk_pending, PRINTK_PENDING_OUTPUT);
Christoph Lameterbb964a92014-08-17 12:30:24 -05002638 irq_work_queue(this_cpu_ptr(&wake_up_klogd_work));
John Stultz81954602014-06-04 16:11:39 -07002639 preempt_enable();
Peter Zijlstra600e1452012-03-15 12:35:37 +01002640
2641 return r;
2642}
2643
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644/*
2645 * printk rate limiting, lifted from the networking subsystem.
2646 *
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07002647 * This enforces a rate limit: not more than 10 kernel messages
2648 * every 5s to make a denial-of-service attack impossible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 */
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07002650DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
2651
Christian Borntraeger5c828712009-10-23 14:58:11 +02002652int __printk_ratelimit(const char *func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653{
Christian Borntraeger5c828712009-10-23 14:58:11 +02002654 return ___ratelimit(&printk_ratelimit_state, func);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655}
Christian Borntraeger5c828712009-10-23 14:58:11 +02002656EXPORT_SYMBOL(__printk_ratelimit);
Andrew Mortonf46c4832006-11-02 22:07:16 -08002657
2658/**
2659 * printk_timed_ratelimit - caller-controlled printk ratelimiting
2660 * @caller_jiffies: pointer to caller's state
2661 * @interval_msecs: minimum interval between prints
2662 *
2663 * printk_timed_ratelimit() returns true if more than @interval_msecs
2664 * milliseconds have elapsed since the last time printk_timed_ratelimit()
2665 * returned true.
2666 */
2667bool printk_timed_ratelimit(unsigned long *caller_jiffies,
2668 unsigned int interval_msecs)
2669{
Alex Elder249771b2014-08-06 16:09:08 -07002670 unsigned long elapsed = jiffies - *caller_jiffies;
2671
2672 if (*caller_jiffies && elapsed <= msecs_to_jiffies(interval_msecs))
2673 return false;
2674
2675 *caller_jiffies = jiffies;
2676 return true;
Andrew Mortonf46c4832006-11-02 22:07:16 -08002677}
2678EXPORT_SYMBOL(printk_timed_ratelimit);
Simon Kagstrom456b5652009-10-16 14:09:18 +02002679
2680static DEFINE_SPINLOCK(dump_list_lock);
2681static LIST_HEAD(dump_list);
2682
2683/**
2684 * kmsg_dump_register - register a kernel log dumper.
Randy Dunlap64855362009-12-17 15:27:27 -08002685 * @dumper: pointer to the kmsg_dumper structure
Simon Kagstrom456b5652009-10-16 14:09:18 +02002686 *
2687 * Adds a kernel log dumper to the system. The dump callback in the
2688 * structure will be called when the kernel oopses or panics and must be
2689 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
2690 */
2691int kmsg_dump_register(struct kmsg_dumper *dumper)
2692{
2693 unsigned long flags;
2694 int err = -EBUSY;
2695
2696 /* The dump callback needs to be set */
2697 if (!dumper->dump)
2698 return -EINVAL;
2699
2700 spin_lock_irqsave(&dump_list_lock, flags);
2701 /* Don't allow registering multiple times */
2702 if (!dumper->registered) {
2703 dumper->registered = 1;
Huang Yingfb842b002011-01-12 16:59:43 -08002704 list_add_tail_rcu(&dumper->list, &dump_list);
Simon Kagstrom456b5652009-10-16 14:09:18 +02002705 err = 0;
2706 }
2707 spin_unlock_irqrestore(&dump_list_lock, flags);
2708
2709 return err;
2710}
2711EXPORT_SYMBOL_GPL(kmsg_dump_register);
2712
2713/**
2714 * kmsg_dump_unregister - unregister a kmsg dumper.
Randy Dunlap64855362009-12-17 15:27:27 -08002715 * @dumper: pointer to the kmsg_dumper structure
Simon Kagstrom456b5652009-10-16 14:09:18 +02002716 *
2717 * Removes a dump device from the system. Returns zero on success and
2718 * %-EINVAL otherwise.
2719 */
2720int kmsg_dump_unregister(struct kmsg_dumper *dumper)
2721{
2722 unsigned long flags;
2723 int err = -EINVAL;
2724
2725 spin_lock_irqsave(&dump_list_lock, flags);
2726 if (dumper->registered) {
2727 dumper->registered = 0;
Huang Yingfb842b002011-01-12 16:59:43 -08002728 list_del_rcu(&dumper->list);
Simon Kagstrom456b5652009-10-16 14:09:18 +02002729 err = 0;
2730 }
2731 spin_unlock_irqrestore(&dump_list_lock, flags);
Huang Yingfb842b002011-01-12 16:59:43 -08002732 synchronize_rcu();
Simon Kagstrom456b5652009-10-16 14:09:18 +02002733
2734 return err;
2735}
2736EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
2737
Kay Sievers7ff95542012-05-03 02:29:13 +02002738static bool always_kmsg_dump;
2739module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR);
2740
Simon Kagstrom456b5652009-10-16 14:09:18 +02002741/**
2742 * kmsg_dump - dump kernel log to kernel message dumpers.
2743 * @reason: the reason (oops, panic etc) for dumping
2744 *
Kay Sieverse2ae7152012-06-15 14:07:51 +02002745 * Call each of the registered dumper's dump() callback, which can
2746 * retrieve the kmsg records with kmsg_dump_get_line() or
2747 * kmsg_dump_get_buffer().
Simon Kagstrom456b5652009-10-16 14:09:18 +02002748 */
2749void kmsg_dump(enum kmsg_dump_reason reason)
2750{
Simon Kagstrom456b5652009-10-16 14:09:18 +02002751 struct kmsg_dumper *dumper;
Simon Kagstrom456b5652009-10-16 14:09:18 +02002752 unsigned long flags;
2753
Matthew Garrettc22ab3322012-03-05 14:59:10 -08002754 if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
2755 return;
2756
Huang Yingfb842b002011-01-12 16:59:43 -08002757 rcu_read_lock();
Kay Sieverse2ae7152012-06-15 14:07:51 +02002758 list_for_each_entry_rcu(dumper, &dump_list, list) {
2759 if (dumper->max_reason && reason > dumper->max_reason)
2760 continue;
2761
2762 /* initialize iterator with data about the stored records */
2763 dumper->active = true;
2764
2765 raw_spin_lock_irqsave(&logbuf_lock, flags);
2766 dumper->cur_seq = clear_seq;
2767 dumper->cur_idx = clear_idx;
2768 dumper->next_seq = log_next_seq;
2769 dumper->next_idx = log_next_idx;
2770 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2771
2772 /* invoke dumper which will iterate over records */
2773 dumper->dump(dumper, reason);
2774
2775 /* reset iterator */
2776 dumper->active = false;
2777 }
Huang Yingfb842b002011-01-12 16:59:43 -08002778 rcu_read_unlock();
Simon Kagstrom456b5652009-10-16 14:09:18 +02002779}
Kay Sieverse2ae7152012-06-15 14:07:51 +02002780
2781/**
Anton Vorontsov533827c2012-07-20 17:28:07 -07002782 * kmsg_dump_get_line_nolock - retrieve one kmsg log line (unlocked version)
2783 * @dumper: registered kmsg dumper
2784 * @syslog: include the "<4>" prefixes
2785 * @line: buffer to copy the line to
2786 * @size: maximum size of the buffer
2787 * @len: length of line placed into buffer
2788 *
2789 * Start at the beginning of the kmsg buffer, with the oldest kmsg
2790 * record, and copy one record into the provided buffer.
2791 *
2792 * Consecutive calls will return the next available record moving
2793 * towards the end of the buffer with the youngest messages.
2794 *
2795 * A return value of FALSE indicates that there are no more records to
2796 * read.
2797 *
2798 * The function is similar to kmsg_dump_get_line(), but grabs no locks.
2799 */
2800bool kmsg_dump_get_line_nolock(struct kmsg_dumper *dumper, bool syslog,
2801 char *line, size_t size, size_t *len)
2802{
Joe Perches62e32ac2013-07-31 13:53:47 -07002803 struct printk_log *msg;
Anton Vorontsov533827c2012-07-20 17:28:07 -07002804 size_t l = 0;
2805 bool ret = false;
2806
2807 if (!dumper->active)
2808 goto out;
2809
2810 if (dumper->cur_seq < log_first_seq) {
2811 /* messages are gone, move to first available one */
2812 dumper->cur_seq = log_first_seq;
2813 dumper->cur_idx = log_first_idx;
2814 }
2815
2816 /* last entry */
2817 if (dumper->cur_seq >= log_next_seq)
2818 goto out;
2819
2820 msg = log_from_idx(dumper->cur_idx);
2821 l = msg_print_text(msg, 0, syslog, line, size);
2822
2823 dumper->cur_idx = log_next(dumper->cur_idx);
2824 dumper->cur_seq++;
2825 ret = true;
2826out:
2827 if (len)
2828 *len = l;
2829 return ret;
2830}
2831
2832/**
Kay Sieverse2ae7152012-06-15 14:07:51 +02002833 * kmsg_dump_get_line - retrieve one kmsg log line
2834 * @dumper: registered kmsg dumper
2835 * @syslog: include the "<4>" prefixes
2836 * @line: buffer to copy the line to
2837 * @size: maximum size of the buffer
2838 * @len: length of line placed into buffer
2839 *
2840 * Start at the beginning of the kmsg buffer, with the oldest kmsg
2841 * record, and copy one record into the provided buffer.
2842 *
2843 * Consecutive calls will return the next available record moving
2844 * towards the end of the buffer with the youngest messages.
2845 *
2846 * A return value of FALSE indicates that there are no more records to
2847 * read.
2848 */
2849bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog,
2850 char *line, size_t size, size_t *len)
2851{
2852 unsigned long flags;
Anton Vorontsov533827c2012-07-20 17:28:07 -07002853 bool ret;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002854
2855 raw_spin_lock_irqsave(&logbuf_lock, flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07002856 ret = kmsg_dump_get_line_nolock(dumper, syslog, line, size, len);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002857 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07002858
Kay Sieverse2ae7152012-06-15 14:07:51 +02002859 return ret;
2860}
2861EXPORT_SYMBOL_GPL(kmsg_dump_get_line);
2862
2863/**
2864 * kmsg_dump_get_buffer - copy kmsg log lines
2865 * @dumper: registered kmsg dumper
2866 * @syslog: include the "<4>" prefixes
Randy Dunlap4f0f4af2012-06-30 15:37:24 -07002867 * @buf: buffer to copy the line to
Kay Sieverse2ae7152012-06-15 14:07:51 +02002868 * @size: maximum size of the buffer
2869 * @len: length of line placed into buffer
2870 *
2871 * Start at the end of the kmsg buffer and fill the provided buffer
2872 * with as many of the the *youngest* kmsg records that fit into it.
2873 * If the buffer is large enough, all available kmsg records will be
2874 * copied with a single call.
2875 *
2876 * Consecutive calls will fill the buffer with the next block of
2877 * available older records, not including the earlier retrieved ones.
2878 *
2879 * A return value of FALSE indicates that there are no more records to
2880 * read.
2881 */
2882bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
2883 char *buf, size_t size, size_t *len)
2884{
2885 unsigned long flags;
2886 u64 seq;
2887 u32 idx;
2888 u64 next_seq;
2889 u32 next_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002890 enum log_flags prev;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002891 size_t l = 0;
2892 bool ret = false;
2893
2894 if (!dumper->active)
2895 goto out;
2896
2897 raw_spin_lock_irqsave(&logbuf_lock, flags);
2898 if (dumper->cur_seq < log_first_seq) {
2899 /* messages are gone, move to first available one */
2900 dumper->cur_seq = log_first_seq;
2901 dumper->cur_idx = log_first_idx;
2902 }
2903
2904 /* last entry */
2905 if (dumper->cur_seq >= dumper->next_seq) {
2906 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2907 goto out;
2908 }
2909
2910 /* calculate length of entire buffer */
2911 seq = dumper->cur_seq;
2912 idx = dumper->cur_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002913 prev = 0;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002914 while (seq < dumper->next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07002915 struct printk_log *msg = log_from_idx(idx);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002916
Kay Sievers5becfb12012-07-09 12:15:42 -07002917 l += msg_print_text(msg, prev, true, NULL, 0);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002918 idx = log_next(idx);
2919 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07002920 prev = msg->flags;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002921 }
2922
2923 /* move first record forward until length fits into the buffer */
2924 seq = dumper->cur_seq;
2925 idx = dumper->cur_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002926 prev = 0;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002927 while (l > size && seq < dumper->next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07002928 struct printk_log *msg = log_from_idx(idx);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002929
Kay Sievers5becfb12012-07-09 12:15:42 -07002930 l -= msg_print_text(msg, prev, true, NULL, 0);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002931 idx = log_next(idx);
2932 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07002933 prev = msg->flags;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002934 }
2935
2936 /* last message in next interation */
2937 next_seq = seq;
2938 next_idx = idx;
2939
2940 l = 0;
2941 while (seq < dumper->next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07002942 struct printk_log *msg = log_from_idx(idx);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002943
Kay Sievers5becfb12012-07-09 12:15:42 -07002944 l += msg_print_text(msg, prev, syslog, buf + l, size - l);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002945 idx = log_next(idx);
2946 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07002947 prev = msg->flags;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002948 }
2949
2950 dumper->next_seq = next_seq;
2951 dumper->next_idx = next_idx;
2952 ret = true;
2953 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2954out:
2955 if (len)
2956 *len = l;
2957 return ret;
2958}
2959EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer);
2960
2961/**
Anton Vorontsov533827c2012-07-20 17:28:07 -07002962 * kmsg_dump_rewind_nolock - reset the interator (unlocked version)
2963 * @dumper: registered kmsg dumper
2964 *
2965 * Reset the dumper's iterator so that kmsg_dump_get_line() and
2966 * kmsg_dump_get_buffer() can be called again and used multiple
2967 * times within the same dumper.dump() callback.
2968 *
2969 * The function is similar to kmsg_dump_rewind(), but grabs no locks.
2970 */
2971void kmsg_dump_rewind_nolock(struct kmsg_dumper *dumper)
2972{
2973 dumper->cur_seq = clear_seq;
2974 dumper->cur_idx = clear_idx;
2975 dumper->next_seq = log_next_seq;
2976 dumper->next_idx = log_next_idx;
2977}
2978
2979/**
Kay Sieverse2ae7152012-06-15 14:07:51 +02002980 * kmsg_dump_rewind - reset the interator
2981 * @dumper: registered kmsg dumper
2982 *
2983 * Reset the dumper's iterator so that kmsg_dump_get_line() and
2984 * kmsg_dump_get_buffer() can be called again and used multiple
2985 * times within the same dumper.dump() callback.
2986 */
2987void kmsg_dump_rewind(struct kmsg_dumper *dumper)
2988{
2989 unsigned long flags;
2990
2991 raw_spin_lock_irqsave(&logbuf_lock, flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07002992 kmsg_dump_rewind_nolock(dumper);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002993 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2994}
2995EXPORT_SYMBOL_GPL(kmsg_dump_rewind);
Tejun Heo196779b2013-04-30 15:27:12 -07002996
Tejun Heo98e5e1b2013-04-30 15:27:15 -07002997static char dump_stack_arch_desc_str[128];
2998
2999/**
3000 * dump_stack_set_arch_desc - set arch-specific str to show with task dumps
3001 * @fmt: printf-style format string
3002 * @...: arguments for the format string
3003 *
3004 * The configured string will be printed right after utsname during task
3005 * dumps. Usually used to add arch-specific system identifiers. If an
3006 * arch wants to make use of such an ID string, it should initialize this
3007 * as soon as possible during boot.
3008 */
3009void __init dump_stack_set_arch_desc(const char *fmt, ...)
3010{
3011 va_list args;
3012
3013 va_start(args, fmt);
3014 vsnprintf(dump_stack_arch_desc_str, sizeof(dump_stack_arch_desc_str),
3015 fmt, args);
3016 va_end(args);
3017}
3018
Tejun Heo196779b2013-04-30 15:27:12 -07003019/**
3020 * dump_stack_print_info - print generic debug info for dump_stack()
3021 * @log_lvl: log level
3022 *
3023 * Arch-specific dump_stack() implementations can use this function to
3024 * print out the same debug information as the generic dump_stack().
3025 */
3026void dump_stack_print_info(const char *log_lvl)
3027{
3028 printk("%sCPU: %d PID: %d Comm: %.20s %s %s %.*s\n",
3029 log_lvl, raw_smp_processor_id(), current->pid, current->comm,
3030 print_tainted(), init_utsname()->release,
3031 (int)strcspn(init_utsname()->version, " "),
3032 init_utsname()->version);
Tejun Heo98e5e1b2013-04-30 15:27:15 -07003033
3034 if (dump_stack_arch_desc_str[0] != '\0')
3035 printk("%sHardware name: %s\n",
3036 log_lvl, dump_stack_arch_desc_str);
Tejun Heo3d1cb202013-04-30 15:27:22 -07003037
3038 print_worker_info(log_lvl, current);
Tejun Heo196779b2013-04-30 15:27:12 -07003039}
3040
Tejun Heoa43cb952013-04-30 15:27:17 -07003041/**
3042 * show_regs_print_info - print generic debug info for show_regs()
3043 * @log_lvl: log level
3044 *
3045 * show_regs() implementations can use this function to print out generic
3046 * debug information.
3047 */
3048void show_regs_print_info(const char *log_lvl)
3049{
3050 dump_stack_print_info(log_lvl);
3051
3052 printk("%stask: %p ti: %p task.ti: %p\n",
3053 log_lvl, current, current_thread_info(),
3054 task_thread_info(current));
3055}
3056
Joe Perches7ef3d2f2008-02-08 04:21:25 -08003057#endif