blob: 7131dd4d0e3af721a86f67776b5bc917ce6d73e3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/printk.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * Modified to make sys_syslog() more flexible: added commands to
7 * return the last 4k of kernel messages, regardless of whether
8 * they've been read or not. Added option to suppress kernel printk's
9 * to the console. Added hook for sending the console messages
10 * elsewhere, in preparation for a serial line console (someday).
11 * Ted Ts'o, 2/11/93.
12 * Modified for sysctl support, 1/8/97, Chris Horn.
Jesper Juhl40dc5652005-10-30 15:02:46 -080013 * Fixed SMP synchronization, 08/08/99, Manfred Spraul
Christian Kujau624dffc2006-01-15 02:43:54 +010014 * manfred@colorfullife.com
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Rewrote bits to get rid of console_lock
Francois Camie1f8e872008-10-15 22:01:59 -070016 * 01Mar01 Andrew Morton
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
19#include <linux/kernel.h>
20#include <linux/mm.h>
21#include <linux/tty.h>
22#include <linux/tty_driver.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/console.h>
24#include <linux/init.h>
Randy Dunlapbfe8df32007-10-16 01:23:46 -070025#include <linux/jiffies.h>
26#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
Jan Engelhardt3b9c0412006-06-25 05:48:15 -070028#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/interrupt.h> /* For in_interrupt() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/delay.h>
31#include <linux/smp.h>
32#include <linux/security.h>
33#include <linux/bootmem.h>
Mike Travis162a7e72011-05-24 17:13:20 -070034#include <linux/memblock.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070035#include <linux/aio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/syscalls.h>
Neil Horman04d491a2009-04-02 16:58:57 -070037#include <linux/kexec.h>
Jason Wesseld37d39a2010-05-20 21:04:27 -050038#include <linux/kdb.h>
Ingo Molnar3fff4c42009-09-22 16:18:09 +020039#include <linux/ratelimit.h>
Simon Kagstrom456b5652009-10-16 14:09:18 +020040#include <linux/kmsg_dump.h>
Kees Cook00234592010-02-03 15:36:43 -080041#include <linux/syslog.h>
Kevin Cernekee034260d2010-06-03 22:11:25 -070042#include <linux/cpu.h>
43#include <linux/notifier.h>
Huang Yingfb842b02011-01-12 16:59:43 -080044#include <linux/rculist.h>
Kay Sieverse11fea92012-05-03 02:29:41 +020045#include <linux/poll.h>
Frederic Weisbecker74876a92012-10-12 18:00:23 +020046#include <linux/irq_work.h>
Tejun Heo196779b2013-04-30 15:27:12 -070047#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include <asm/uaccess.h>
50
Johannes Berg95100352011-11-24 20:03:08 +010051#define CREATE_TRACE_POINTS
52#include <trace/events/printk.h>
53
Joe Perchesd197c432013-07-31 13:53:44 -070054#include "console_cmdline.h"
Joe Perchesbbeddf52013-07-31 13:53:45 -070055#include "braille.h"
Joe Perchesd197c432013-07-31 13:53:44 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/* printk's without a loglevel use this.. */
Mandeep Singh Baines5af5bcb2011-03-22 16:34:23 -070058#define DEFAULT_MESSAGE_LOGLEVEL CONFIG_DEFAULT_MESSAGE_LOGLEVEL
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/* We show everything that is MORE important than this.. */
61#define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
62#define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064int console_printk[4] = {
65 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
66 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
67 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
68 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
69};
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/*
Patrick Pletscher0bbfb7c2007-02-17 20:10:16 +010072 * Low level drivers may need that to know if they can schedule in
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * their unblank() callback or not. So let's export it.
74 */
75int oops_in_progress;
76EXPORT_SYMBOL(oops_in_progress);
77
78/*
79 * console_sem protects the console_drivers list, and also
80 * provides serialisation for access to the entire console
81 * driver system.
82 */
Thomas Gleixner5b8c4f22010-09-07 14:33:43 +000083static DEFINE_SEMAPHORE(console_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084struct console *console_drivers;
Ingo Molnara29d1cf2008-06-02 13:19:08 +020085EXPORT_SYMBOL_GPL(console_drivers);
86
Daniel Vetterdaee7792012-09-22 19:52:11 +020087#ifdef CONFIG_LOCKDEP
88static struct lockdep_map console_lock_dep_map = {
89 .name = "console_lock"
90};
91#endif
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/*
94 * This is used for debugging the mess that is the VT code by
95 * keeping track if we have the console semaphore held. It's
96 * definitely not the perfect debug tool (we don't know if _WE_
97 * hold it are racing, but it helps tracking those weird code
98 * path in the console code where we end up in places I want
99 * locked without the console sempahore held
100 */
Linus Torvalds557240b2006-06-19 18:16:01 -0700101static int console_locked, console_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/*
Feng Tangfe3d8ad2011-03-22 16:34:21 -0700104 * If exclusive_console is non-NULL then only this console is to be printed to.
105 */
106static struct console *exclusive_console;
107
108/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * Array of consoles built from command line options (console=)
110 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112#define MAX_CMDLINECONSOLES 8
113
114static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
Joe Perchesd197c432013-07-31 13:53:44 -0700115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116static int selected_console = -1;
117static int preferred_console = -1;
Markus Armbruster9e124fe2008-05-26 23:31:07 +0100118int console_set_on_cmdline;
119EXPORT_SYMBOL(console_set_on_cmdline);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121/* Flag: console code may call schedule() */
122static int console_may_schedule;
123
Kay Sievers7ff95542012-05-03 02:29:13 +0200124/*
125 * The printk log buffer consists of a chain of concatenated variable
126 * length records. Every record starts with a record header, containing
127 * the overall length of the record.
128 *
129 * The heads to the first and last entry in the buffer, as well as the
130 * sequence numbers of these both entries are maintained when messages
131 * are stored..
132 *
133 * If the heads indicate available messages, the length in the header
134 * tells the start next message. A length == 0 for the next message
135 * indicates a wrap-around to the beginning of the buffer.
136 *
137 * Every record carries the monotonic timestamp in microseconds, as well as
138 * the standard userspace syslog level and syslog facility. The usual
139 * kernel messages use LOG_KERN; userspace-injected messages always carry
140 * a matching syslog facility, by default LOG_USER. The origin of every
141 * message can be reliably determined that way.
142 *
143 * The human readable log message directly follows the message header. The
144 * length of the message text is stored in the header, the stored message
145 * is not terminated.
146 *
Kay Sieverse11fea92012-05-03 02:29:41 +0200147 * Optionally, a message can carry a dictionary of properties (key/value pairs),
148 * to provide userspace with a machine-readable message context.
149 *
150 * Examples for well-defined, commonly used property names are:
151 * DEVICE=b12:8 device identifier
152 * b12:8 block dev_t
153 * c127:3 char dev_t
154 * n8 netdev ifindex
155 * +sound:card0 subsystem:devname
156 * SUBSYSTEM=pci driver-core subsystem name
157 *
158 * Valid characters in property names are [a-zA-Z0-9.-_]. The plain text value
159 * follows directly after a '=' character. Every property is terminated by
160 * a '\0' character. The last property is not terminated.
161 *
162 * Example of a message structure:
163 * 0000 ff 8f 00 00 00 00 00 00 monotonic time in nsec
164 * 0008 34 00 record is 52 bytes long
165 * 000a 0b 00 text is 11 bytes long
166 * 000c 1f 00 dictionary is 23 bytes long
167 * 000e 03 00 LOG_KERN (facility) LOG_ERR (level)
168 * 0010 69 74 27 73 20 61 20 6c "it's a l"
169 * 69 6e 65 "ine"
170 * 001b 44 45 56 49 43 "DEVIC"
171 * 45 3d 62 38 3a 32 00 44 "E=b8:2\0D"
172 * 52 49 56 45 52 3d 62 75 "RIVER=bu"
173 * 67 "g"
174 * 0032 00 00 00 padding to next message header
175 *
Joe Perches62e32ac2013-07-31 13:53:47 -0700176 * The 'struct printk_log' buffer header must never be directly exported to
Kay Sieverse11fea92012-05-03 02:29:41 +0200177 * userspace, it is a kernel-private implementation detail that might
178 * need to be changed in the future, when the requirements change.
179 *
180 * /dev/kmsg exports the structured data in the following line format:
181 * "level,sequnum,timestamp;<message text>\n"
182 *
183 * The optional key/value pairs are attached as continuation lines starting
184 * with a space character and terminated by a newline. All possible
185 * non-prinatable characters are escaped in the "\xff" notation.
186 *
187 * Users of the export format should ignore possible additional values
188 * separated by ',', and find the message after the ';' character.
Kay Sievers7ff95542012-05-03 02:29:13 +0200189 */
Matt Mackalld59745c2005-05-01 08:59:02 -0700190
Kay Sievers084681d2012-06-28 09:38:53 +0200191enum log_flags {
Kay Sievers5becfb12012-07-09 12:15:42 -0700192 LOG_NOCONS = 1, /* already flushed, do not print to console */
193 LOG_NEWLINE = 2, /* text ended with a newline */
194 LOG_PREFIX = 4, /* text started with a prefix */
195 LOG_CONT = 8, /* text is a fragment of a continuation line */
Kay Sievers084681d2012-06-28 09:38:53 +0200196};
197
Joe Perches62e32ac2013-07-31 13:53:47 -0700198struct printk_log {
Kay Sievers7ff95542012-05-03 02:29:13 +0200199 u64 ts_nsec; /* timestamp in nanoseconds */
200 u16 len; /* length of entire record */
201 u16 text_len; /* length of text buffer */
202 u16 dict_len; /* length of dictionary buffer */
Kay Sievers084681d2012-06-28 09:38:53 +0200203 u8 facility; /* syslog facility */
204 u8 flags:5; /* internal record flags */
205 u8 level:3; /* syslog level */
Kay Sievers7ff95542012-05-03 02:29:13 +0200206};
207
208/*
209 * The logbuf_lock protects kmsg buffer, indices, counters. It is also
210 * used in interesting ways to provide interlocking in console_unlock();
211 */
212static DEFINE_RAW_SPINLOCK(logbuf_lock);
213
Kay Sievers96efedf2012-07-16 18:35:29 -0700214#ifdef CONFIG_PRINTK
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -0700215DECLARE_WAIT_QUEUE_HEAD(log_wait);
Kay Sievers7f3a7812012-05-09 01:37:51 +0200216/* the next printk record to read by syslog(READ) or /proc/kmsg */
217static u64 syslog_seq;
218static u32 syslog_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -0700219static enum log_flags syslog_prev;
Kay Sieverseb02dac2012-07-09 10:05:10 -0700220static size_t syslog_partial;
Kay Sievers7f3a7812012-05-09 01:37:51 +0200221
222/* index and sequence number of the first record stored in the buffer */
223static u64 log_first_seq;
224static u32 log_first_idx;
225
226/* index and sequence number of the next record to store in the buffer */
227static u64 log_next_seq;
Kay Sievers7f3a7812012-05-09 01:37:51 +0200228static u32 log_next_idx;
229
Kay Sieverseab07262012-07-16 18:35:30 -0700230/* the next printk record to write to the console */
231static u64 console_seq;
232static u32 console_idx;
233static enum log_flags console_prev;
234
Kay Sievers7f3a7812012-05-09 01:37:51 +0200235/* the next printk record to read after the last 'clear' command */
236static u64 clear_seq;
237static u32 clear_idx;
Kay Sievers7ff95542012-05-03 02:29:13 +0200238
Kay Sievers70498252012-07-16 18:35:29 -0700239#define PREFIX_MAX 32
240#define LOG_LINE_MAX 1024 - PREFIX_MAX
Kay Sievers7ff95542012-05-03 02:29:13 +0200241
242/* record buffer */
Andrew Lunn6ebb0172012-06-05 08:52:34 +0200243#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
Stephen Warrenf8450fc2012-05-10 16:14:33 -0600244#define LOG_ALIGN 4
245#else
Joe Perches62e32ac2013-07-31 13:53:47 -0700246#define LOG_ALIGN __alignof__(struct printk_log)
Stephen Warrenf8450fc2012-05-10 16:14:33 -0600247#endif
Kay Sievers7ff95542012-05-03 02:29:13 +0200248#define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
Stephen Warrenf8450fc2012-05-10 16:14:33 -0600249static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN);
Matt Mackalld59745c2005-05-01 08:59:02 -0700250static char *log_buf = __log_buf;
Kay Sievers7ff95542012-05-03 02:29:13 +0200251static u32 log_buf_len = __LOG_BUF_LEN;
252
Kay Sievers7f3a7812012-05-09 01:37:51 +0200253/* cpu currently holding logbuf_lock */
254static volatile unsigned int logbuf_cpu = UINT_MAX;
Kay Sievers7ff95542012-05-03 02:29:13 +0200255
256/* human readable text of the record */
Joe Perches62e32ac2013-07-31 13:53:47 -0700257static char *log_text(const struct printk_log *msg)
Kay Sievers7ff95542012-05-03 02:29:13 +0200258{
Joe Perches62e32ac2013-07-31 13:53:47 -0700259 return (char *)msg + sizeof(struct printk_log);
Kay Sievers7ff95542012-05-03 02:29:13 +0200260}
261
262/* optional key/value pair dictionary attached to the record */
Joe Perches62e32ac2013-07-31 13:53:47 -0700263static char *log_dict(const struct printk_log *msg)
Kay Sievers7ff95542012-05-03 02:29:13 +0200264{
Joe Perches62e32ac2013-07-31 13:53:47 -0700265 return (char *)msg + sizeof(struct printk_log) + msg->text_len;
Kay Sievers7ff95542012-05-03 02:29:13 +0200266}
267
268/* get record by index; idx must point to valid msg */
Joe Perches62e32ac2013-07-31 13:53:47 -0700269static struct printk_log *log_from_idx(u32 idx)
Kay Sievers7ff95542012-05-03 02:29:13 +0200270{
Joe Perches62e32ac2013-07-31 13:53:47 -0700271 struct printk_log *msg = (struct printk_log *)(log_buf + idx);
Kay Sievers7ff95542012-05-03 02:29:13 +0200272
273 /*
274 * A length == 0 record is the end of buffer marker. Wrap around and
275 * read the message at the start of the buffer.
276 */
277 if (!msg->len)
Joe Perches62e32ac2013-07-31 13:53:47 -0700278 return (struct printk_log *)log_buf;
Kay Sievers7ff95542012-05-03 02:29:13 +0200279 return msg;
280}
281
282/* get next record; idx must point to valid msg */
283static u32 log_next(u32 idx)
284{
Joe Perches62e32ac2013-07-31 13:53:47 -0700285 struct printk_log *msg = (struct printk_log *)(log_buf + idx);
Kay Sievers7ff95542012-05-03 02:29:13 +0200286
287 /* length == 0 indicates the end of the buffer; wrap */
288 /*
289 * A length == 0 record is the end of buffer marker. Wrap around and
290 * read the message at the start of the buffer as *this* one, and
291 * return the one after that.
292 */
293 if (!msg->len) {
Joe Perches62e32ac2013-07-31 13:53:47 -0700294 msg = (struct printk_log *)log_buf;
Kay Sievers7ff95542012-05-03 02:29:13 +0200295 return msg->len;
296 }
297 return idx + msg->len;
298}
299
Petr Mladekf40e4b92014-06-04 16:11:30 -0700300/*
301 * Check whether there is enough free space for the given message.
302 *
303 * The same values of first_idx and next_idx mean that the buffer
304 * is either empty or full.
305 *
306 * If the buffer is empty, we must respect the position of the indexes.
307 * They cannot be reset to the beginning of the buffer.
308 */
309static int logbuf_has_space(u32 msg_size, bool empty)
Petr Mladek0a581692014-06-04 16:11:28 -0700310{
311 u32 free;
312
Petr Mladekf40e4b92014-06-04 16:11:30 -0700313 if (log_next_idx > log_first_idx || empty)
Petr Mladek0a581692014-06-04 16:11:28 -0700314 free = max(log_buf_len - log_next_idx, log_first_idx);
315 else
316 free = log_first_idx - log_next_idx;
317
318 /*
319 * We need space also for an empty header that signalizes wrapping
320 * of the buffer.
321 */
322 return free >= msg_size + sizeof(struct printk_log);
323}
324
Petr Mladekf40e4b92014-06-04 16:11:30 -0700325static int log_make_free_space(u32 msg_size)
Petr Mladek0a581692014-06-04 16:11:28 -0700326{
327 while (log_first_seq < log_next_seq) {
Petr Mladekf40e4b92014-06-04 16:11:30 -0700328 if (logbuf_has_space(msg_size, false))
329 return 0;
Petr Mladek0a581692014-06-04 16:11:28 -0700330 /* drop old messages until we have enough continuous space */
331 log_first_idx = log_next(log_first_idx);
332 log_first_seq++;
333 }
Petr Mladekf40e4b92014-06-04 16:11:30 -0700334
335 /* sequence numbers are equal, so the log buffer is empty */
336 if (logbuf_has_space(msg_size, true))
337 return 0;
338
339 return -ENOMEM;
Petr Mladek0a581692014-06-04 16:11:28 -0700340}
341
Petr Mladek85c870432014-06-04 16:11:31 -0700342/* compute the message size including the padding bytes */
343static u32 msg_used_size(u16 text_len, u16 dict_len, u32 *pad_len)
344{
345 u32 size;
346
347 size = sizeof(struct printk_log) + text_len + dict_len;
348 *pad_len = (-size) & (LOG_ALIGN - 1);
349 size += *pad_len;
350
351 return size;
352}
353
Petr Mladek55bd53a2014-06-04 16:11:32 -0700354/*
355 * Define how much of the log buffer we could take at maximum. The value
356 * must be greater than two. Note that only half of the buffer is available
357 * when the index points to the middle.
358 */
359#define MAX_LOG_TAKE_PART 4
360static const char trunc_msg[] = "<truncated>";
361
362static u32 truncate_msg(u16 *text_len, u16 *trunc_msg_len,
363 u16 *dict_len, u32 *pad_len)
364{
365 /*
366 * The message should not take the whole buffer. Otherwise, it might
367 * get removed too soon.
368 */
369 u32 max_text_len = log_buf_len / MAX_LOG_TAKE_PART;
370 if (*text_len > max_text_len)
371 *text_len = max_text_len;
372 /* enable the warning message */
373 *trunc_msg_len = strlen(trunc_msg);
374 /* disable the "dict" completely */
375 *dict_len = 0;
376 /* compute the size again, count also the warning message */
377 return msg_used_size(*text_len + *trunc_msg_len, 0, pad_len);
378}
379
Kay Sievers7ff95542012-05-03 02:29:13 +0200380/* insert record into the buffer, discard old ones, update heads */
381static void log_store(int facility, int level,
Kay Sievers084681d2012-06-28 09:38:53 +0200382 enum log_flags flags, u64 ts_nsec,
Kay Sievers7ff95542012-05-03 02:29:13 +0200383 const char *dict, u16 dict_len,
384 const char *text, u16 text_len)
385{
Joe Perches62e32ac2013-07-31 13:53:47 -0700386 struct printk_log *msg;
Kay Sievers7ff95542012-05-03 02:29:13 +0200387 u32 size, pad_len;
Petr Mladek55bd53a2014-06-04 16:11:32 -0700388 u16 trunc_msg_len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +0200389
390 /* number of '\0' padding bytes to next message */
Petr Mladek85c870432014-06-04 16:11:31 -0700391 size = msg_used_size(text_len, dict_len, &pad_len);
Kay Sievers7ff95542012-05-03 02:29:13 +0200392
Petr Mladek55bd53a2014-06-04 16:11:32 -0700393 if (log_make_free_space(size)) {
394 /* truncate the message if it is too long for empty buffer */
395 size = truncate_msg(&text_len, &trunc_msg_len,
396 &dict_len, &pad_len);
397 /* survive when the log buffer is too small for trunc_msg */
398 if (log_make_free_space(size))
399 return;
400 }
Kay Sievers7ff95542012-05-03 02:29:13 +0200401
Petr Mladek39b25102014-04-03 14:48:42 -0700402 if (log_next_idx + size + sizeof(struct printk_log) > log_buf_len) {
Kay Sievers7ff95542012-05-03 02:29:13 +0200403 /*
404 * This message + an additional empty header does not fit
405 * at the end of the buffer. Add an empty header with len == 0
406 * to signify a wrap around.
407 */
Joe Perches62e32ac2013-07-31 13:53:47 -0700408 memset(log_buf + log_next_idx, 0, sizeof(struct printk_log));
Kay Sievers7ff95542012-05-03 02:29:13 +0200409 log_next_idx = 0;
410 }
411
412 /* fill message */
Joe Perches62e32ac2013-07-31 13:53:47 -0700413 msg = (struct printk_log *)(log_buf + log_next_idx);
Kay Sievers7ff95542012-05-03 02:29:13 +0200414 memcpy(log_text(msg), text, text_len);
415 msg->text_len = text_len;
Petr Mladek55bd53a2014-06-04 16:11:32 -0700416 if (trunc_msg_len) {
417 memcpy(log_text(msg) + text_len, trunc_msg, trunc_msg_len);
418 msg->text_len += trunc_msg_len;
419 }
Kay Sievers7ff95542012-05-03 02:29:13 +0200420 memcpy(log_dict(msg), dict, dict_len);
421 msg->dict_len = dict_len;
Kay Sievers084681d2012-06-28 09:38:53 +0200422 msg->facility = facility;
423 msg->level = level & 7;
424 msg->flags = flags & 0x1f;
425 if (ts_nsec > 0)
426 msg->ts_nsec = ts_nsec;
427 else
428 msg->ts_nsec = local_clock();
Kay Sievers7ff95542012-05-03 02:29:13 +0200429 memset(log_dict(msg) + dict_len, 0, pad_len);
Petr Mladekfce6e032014-04-03 14:48:43 -0700430 msg->len = size;
Kay Sievers7ff95542012-05-03 02:29:13 +0200431
432 /* insert message */
433 log_next_idx += msg->len;
434 log_next_seq++;
435}
Matt Mackalld59745c2005-05-01 08:59:02 -0700436
Kees Cook637241a2013-06-12 14:04:39 -0700437#ifdef CONFIG_SECURITY_DMESG_RESTRICT
438int dmesg_restrict = 1;
439#else
440int dmesg_restrict;
441#endif
442
443static int syslog_action_restricted(int type)
444{
445 if (dmesg_restrict)
446 return 1;
447 /*
448 * Unless restricted, we allow "read all" and "get buffer size"
449 * for everybody.
450 */
451 return type != SYSLOG_ACTION_READ_ALL &&
452 type != SYSLOG_ACTION_SIZE_BUFFER;
453}
454
455static int check_syslog_permissions(int type, bool from_file)
456{
457 /*
458 * If this is from /proc/kmsg and we've already opened it, then we've
459 * already done the capabilities checks at open time.
460 */
461 if (from_file && type != SYSLOG_ACTION_OPEN)
462 return 0;
463
464 if (syslog_action_restricted(type)) {
465 if (capable(CAP_SYSLOG))
466 return 0;
467 /*
468 * For historical reasons, accept CAP_SYS_ADMIN too, with
469 * a warning.
470 */
471 if (capable(CAP_SYS_ADMIN)) {
472 pr_warn_once("%s (%d): Attempt to access syslog with "
473 "CAP_SYS_ADMIN but no CAP_SYSLOG "
474 "(deprecated).\n",
475 current->comm, task_pid_nr(current));
476 return 0;
477 }
478 return -EPERM;
479 }
480 return security_syslog(type);
481}
482
483
Kay Sieverse11fea92012-05-03 02:29:41 +0200484/* /dev/kmsg - userspace message inject/listen interface */
485struct devkmsg_user {
486 u64 seq;
487 u32 idx;
Kay Sieversd39f3d72012-07-16 18:35:30 -0700488 enum log_flags prev;
Kay Sieverse11fea92012-05-03 02:29:41 +0200489 struct mutex lock;
490 char buf[8192];
491};
492
493static ssize_t devkmsg_writev(struct kiocb *iocb, const struct iovec *iv,
494 unsigned long count, loff_t pos)
495{
496 char *buf, *line;
497 int i;
498 int level = default_message_loglevel;
499 int facility = 1; /* LOG_USER */
500 size_t len = iov_length(iv, count);
501 ssize_t ret = len;
502
503 if (len > LOG_LINE_MAX)
504 return -EINVAL;
505 buf = kmalloc(len+1, GFP_KERNEL);
506 if (buf == NULL)
507 return -ENOMEM;
508
509 line = buf;
510 for (i = 0; i < count; i++) {
Kay Sieverscdf53442012-07-30 14:40:08 -0700511 if (copy_from_user(line, iv[i].iov_base, iv[i].iov_len)) {
512 ret = -EFAULT;
Kay Sieverse11fea92012-05-03 02:29:41 +0200513 goto out;
Kay Sieverscdf53442012-07-30 14:40:08 -0700514 }
Kay Sieverse11fea92012-05-03 02:29:41 +0200515 line += iv[i].iov_len;
516 }
517
518 /*
519 * Extract and skip the syslog prefix <[0-9]*>. Coming from userspace
520 * the decimal value represents 32bit, the lower 3 bit are the log
521 * level, the rest are the log facility.
522 *
523 * If no prefix or no userspace facility is specified, we
524 * enforce LOG_USER, to be able to reliably distinguish
525 * kernel-generated messages from userspace-injected ones.
526 */
527 line = buf;
528 if (line[0] == '<') {
529 char *endp = NULL;
530
531 i = simple_strtoul(line+1, &endp, 10);
532 if (endp && endp[0] == '>') {
533 level = i & 7;
534 if (i >> 3)
535 facility = i >> 3;
536 endp++;
537 len -= endp - line;
538 line = endp;
539 }
540 }
541 line[len] = '\0';
542
543 printk_emit(facility, level, NULL, 0, "%s", line);
544out:
545 kfree(buf);
546 return ret;
547}
548
549static ssize_t devkmsg_read(struct file *file, char __user *buf,
550 size_t count, loff_t *ppos)
551{
552 struct devkmsg_user *user = file->private_data;
Joe Perches62e32ac2013-07-31 13:53:47 -0700553 struct printk_log *msg;
Kay Sievers5fc324902012-05-08 13:04:17 +0200554 u64 ts_usec;
Kay Sieverse11fea92012-05-03 02:29:41 +0200555 size_t i;
Kay Sieversd39f3d72012-07-16 18:35:30 -0700556 char cont = '-';
Kay Sieverse11fea92012-05-03 02:29:41 +0200557 size_t len;
558 ssize_t ret;
559
560 if (!user)
561 return -EBADF;
562
Yuanhan Liu4a77a5a2012-06-16 21:21:51 +0800563 ret = mutex_lock_interruptible(&user->lock);
564 if (ret)
565 return ret;
liu chuansheng5c53d812012-07-06 09:50:08 -0700566 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200567 while (user->seq == log_next_seq) {
568 if (file->f_flags & O_NONBLOCK) {
569 ret = -EAGAIN;
liu chuansheng5c53d812012-07-06 09:50:08 -0700570 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200571 goto out;
572 }
573
liu chuansheng5c53d812012-07-06 09:50:08 -0700574 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200575 ret = wait_event_interruptible(log_wait,
576 user->seq != log_next_seq);
577 if (ret)
578 goto out;
liu chuansheng5c53d812012-07-06 09:50:08 -0700579 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200580 }
581
582 if (user->seq < log_first_seq) {
583 /* our last seen message is gone, return error and reset */
584 user->idx = log_first_idx;
585 user->seq = log_first_seq;
586 ret = -EPIPE;
liu chuansheng5c53d812012-07-06 09:50:08 -0700587 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200588 goto out;
589 }
590
591 msg = log_from_idx(user->idx);
Kay Sievers5fc324902012-05-08 13:04:17 +0200592 ts_usec = msg->ts_nsec;
593 do_div(ts_usec, 1000);
Kay Sieversd39f3d72012-07-16 18:35:30 -0700594
595 /*
596 * If we couldn't merge continuation line fragments during the print,
597 * export the stored flags to allow an optional external merge of the
598 * records. Merging the records isn't always neccessarily correct, like
599 * when we hit a race during printing. In most cases though, it produces
600 * better readable output. 'c' in the record flags mark the first
601 * fragment of a line, '+' the following.
602 */
603 if (msg->flags & LOG_CONT && !(user->prev & LOG_CONT))
604 cont = 'c';
605 else if ((msg->flags & LOG_CONT) ||
606 ((user->prev & LOG_CONT) && !(msg->flags & LOG_PREFIX)))
607 cont = '+';
608
609 len = sprintf(user->buf, "%u,%llu,%llu,%c;",
610 (msg->facility << 3) | msg->level,
611 user->seq, ts_usec, cont);
612 user->prev = msg->flags;
Kay Sieverse11fea92012-05-03 02:29:41 +0200613
614 /* escape non-printable characters */
615 for (i = 0; i < msg->text_len; i++) {
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200616 unsigned char c = log_text(msg)[i];
Kay Sieverse11fea92012-05-03 02:29:41 +0200617
Kay Sieverse3f5a5f2012-07-06 09:50:09 -0700618 if (c < ' ' || c >= 127 || c == '\\')
Kay Sieverse11fea92012-05-03 02:29:41 +0200619 len += sprintf(user->buf + len, "\\x%02x", c);
620 else
621 user->buf[len++] = c;
622 }
623 user->buf[len++] = '\n';
624
625 if (msg->dict_len) {
626 bool line = true;
627
628 for (i = 0; i < msg->dict_len; i++) {
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200629 unsigned char c = log_dict(msg)[i];
Kay Sieverse11fea92012-05-03 02:29:41 +0200630
631 if (line) {
632 user->buf[len++] = ' ';
633 line = false;
634 }
635
636 if (c == '\0') {
637 user->buf[len++] = '\n';
638 line = true;
639 continue;
640 }
641
Kay Sieverse3f5a5f2012-07-06 09:50:09 -0700642 if (c < ' ' || c >= 127 || c == '\\') {
Kay Sieverse11fea92012-05-03 02:29:41 +0200643 len += sprintf(user->buf + len, "\\x%02x", c);
644 continue;
645 }
646
647 user->buf[len++] = c;
648 }
649 user->buf[len++] = '\n';
650 }
651
652 user->idx = log_next(user->idx);
653 user->seq++;
liu chuansheng5c53d812012-07-06 09:50:08 -0700654 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200655
656 if (len > count) {
657 ret = -EINVAL;
658 goto out;
659 }
660
661 if (copy_to_user(buf, user->buf, len)) {
662 ret = -EFAULT;
663 goto out;
664 }
665 ret = len;
666out:
667 mutex_unlock(&user->lock);
668 return ret;
669}
670
671static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence)
672{
673 struct devkmsg_user *user = file->private_data;
674 loff_t ret = 0;
675
676 if (!user)
677 return -EBADF;
678 if (offset)
679 return -ESPIPE;
680
liu chuansheng5c53d812012-07-06 09:50:08 -0700681 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200682 switch (whence) {
683 case SEEK_SET:
684 /* the first record */
685 user->idx = log_first_idx;
686 user->seq = log_first_seq;
687 break;
688 case SEEK_DATA:
689 /*
690 * The first record after the last SYSLOG_ACTION_CLEAR,
691 * like issued by 'dmesg -c'. Reading /dev/kmsg itself
692 * changes no global state, and does not clear anything.
693 */
694 user->idx = clear_idx;
695 user->seq = clear_seq;
696 break;
697 case SEEK_END:
698 /* after the last record */
699 user->idx = log_next_idx;
700 user->seq = log_next_seq;
701 break;
702 default:
703 ret = -EINVAL;
704 }
liu chuansheng5c53d812012-07-06 09:50:08 -0700705 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200706 return ret;
707}
708
709static unsigned int devkmsg_poll(struct file *file, poll_table *wait)
710{
711 struct devkmsg_user *user = file->private_data;
712 int ret = 0;
713
714 if (!user)
715 return POLLERR|POLLNVAL;
716
717 poll_wait(file, &log_wait, wait);
718
liu chuansheng5c53d812012-07-06 09:50:08 -0700719 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200720 if (user->seq < log_next_seq) {
721 /* return error when data has vanished underneath us */
722 if (user->seq < log_first_seq)
723 ret = POLLIN|POLLRDNORM|POLLERR|POLLPRI;
Nicolas Kaiser0a285312013-04-29 16:17:20 -0700724 else
725 ret = POLLIN|POLLRDNORM;
Kay Sieverse11fea92012-05-03 02:29:41 +0200726 }
liu chuansheng5c53d812012-07-06 09:50:08 -0700727 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200728
729 return ret;
730}
731
732static int devkmsg_open(struct inode *inode, struct file *file)
733{
734 struct devkmsg_user *user;
735 int err;
736
737 /* write-only does not need any file context */
738 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
739 return 0;
740
Kees Cook637241a2013-06-12 14:04:39 -0700741 err = check_syslog_permissions(SYSLOG_ACTION_READ_ALL,
742 SYSLOG_FROM_READER);
Kay Sieverse11fea92012-05-03 02:29:41 +0200743 if (err)
744 return err;
745
746 user = kmalloc(sizeof(struct devkmsg_user), GFP_KERNEL);
747 if (!user)
748 return -ENOMEM;
749
750 mutex_init(&user->lock);
751
liu chuansheng5c53d812012-07-06 09:50:08 -0700752 raw_spin_lock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200753 user->idx = log_first_idx;
754 user->seq = log_first_seq;
liu chuansheng5c53d812012-07-06 09:50:08 -0700755 raw_spin_unlock_irq(&logbuf_lock);
Kay Sieverse11fea92012-05-03 02:29:41 +0200756
757 file->private_data = user;
758 return 0;
759}
760
761static int devkmsg_release(struct inode *inode, struct file *file)
762{
763 struct devkmsg_user *user = file->private_data;
764
765 if (!user)
766 return 0;
767
768 mutex_destroy(&user->lock);
769 kfree(user);
770 return 0;
771}
772
773const struct file_operations kmsg_fops = {
774 .open = devkmsg_open,
775 .read = devkmsg_read,
776 .aio_write = devkmsg_writev,
777 .llseek = devkmsg_llseek,
778 .poll = devkmsg_poll,
779 .release = devkmsg_release,
780};
781
Neil Horman04d491a2009-04-02 16:58:57 -0700782#ifdef CONFIG_KEXEC
783/*
Dirk Gouders4c1ace62013-11-12 15:08:54 -0800784 * This appends the listed symbols to /proc/vmcore
Neil Horman04d491a2009-04-02 16:58:57 -0700785 *
Dirk Gouders4c1ace62013-11-12 15:08:54 -0800786 * /proc/vmcore is used by various utilities, like crash and makedumpfile to
Neil Horman04d491a2009-04-02 16:58:57 -0700787 * obtain access to symbols that are otherwise very difficult to locate. These
788 * symbols are specifically used so that utilities can access and extract the
789 * dmesg log from a vmcore file after a crash.
790 */
791void log_buf_kexec_setup(void)
792{
793 VMCOREINFO_SYMBOL(log_buf);
Neil Horman04d491a2009-04-02 16:58:57 -0700794 VMCOREINFO_SYMBOL(log_buf_len);
Kay Sievers7ff95542012-05-03 02:29:13 +0200795 VMCOREINFO_SYMBOL(log_first_idx);
796 VMCOREINFO_SYMBOL(log_next_idx);
Vivek Goyal67914572012-07-18 13:18:12 -0400797 /*
Joe Perches62e32ac2013-07-31 13:53:47 -0700798 * Export struct printk_log size and field offsets. User space tools can
Vivek Goyal67914572012-07-18 13:18:12 -0400799 * parse it and detect any changes to structure down the line.
800 */
Joe Perches62e32ac2013-07-31 13:53:47 -0700801 VMCOREINFO_STRUCT_SIZE(printk_log);
802 VMCOREINFO_OFFSET(printk_log, ts_nsec);
803 VMCOREINFO_OFFSET(printk_log, len);
804 VMCOREINFO_OFFSET(printk_log, text_len);
805 VMCOREINFO_OFFSET(printk_log, dict_len);
Neil Horman04d491a2009-04-02 16:58:57 -0700806}
807#endif
808
Mike Travis162a7e72011-05-24 17:13:20 -0700809/* requested log_buf_len from kernel cmdline */
810static unsigned long __initdata new_log_buf_len;
811
812/* save requested log_buf_len since it's too early to process it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813static int __init log_buf_len_setup(char *str)
814{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -0800815 unsigned size = memparse(str, &str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 if (size)
818 size = roundup_pow_of_two(size);
Mike Travis162a7e72011-05-24 17:13:20 -0700819 if (size > log_buf_len)
820 new_log_buf_len = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Mike Travis162a7e72011-05-24 17:13:20 -0700822 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
Mike Travis162a7e72011-05-24 17:13:20 -0700824early_param("log_buf_len", log_buf_len_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Mike Travis162a7e72011-05-24 17:13:20 -0700826void __init setup_log_buf(int early)
827{
828 unsigned long flags;
Mike Travis162a7e72011-05-24 17:13:20 -0700829 char *new_log_buf;
830 int free;
831
832 if (!new_log_buf_len)
833 return;
834
835 if (early) {
Santosh Shilimkar9da791d2014-01-21 15:50:23 -0800836 new_log_buf =
837 memblock_virt_alloc(new_log_buf_len, PAGE_SIZE);
Mike Travis162a7e72011-05-24 17:13:20 -0700838 } else {
Santosh Shilimkar9da791d2014-01-21 15:50:23 -0800839 new_log_buf = memblock_virt_alloc_nopanic(new_log_buf_len, 0);
Mike Travis162a7e72011-05-24 17:13:20 -0700840 }
841
842 if (unlikely(!new_log_buf)) {
843 pr_err("log_buf_len: %ld bytes not available\n",
844 new_log_buf_len);
845 return;
846 }
847
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200848 raw_spin_lock_irqsave(&logbuf_lock, flags);
Mike Travis162a7e72011-05-24 17:13:20 -0700849 log_buf_len = new_log_buf_len;
850 log_buf = new_log_buf;
851 new_log_buf_len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +0200852 free = __LOG_BUF_LEN - log_next_idx;
853 memcpy(log_buf, __log_buf, __LOG_BUF_LEN);
Thomas Gleixner07354eb2009-07-25 17:50:36 +0200854 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
Mike Travis162a7e72011-05-24 17:13:20 -0700855
856 pr_info("log_buf_len: %d\n", log_buf_len);
857 pr_info("early log buf free: %d(%d%%)\n",
858 free, (free * 100) / __LOG_BUF_LEN);
859}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800861static bool __read_mostly ignore_loglevel;
862
863static int __init ignore_loglevel_setup(char *str)
864{
865 ignore_loglevel = 1;
Andrew Morton27083ba2013-11-12 15:08:50 -0800866 pr_info("debug: ignoring loglevel setting.\n");
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800867
868 return 0;
869}
870
871early_param("ignore_loglevel", ignore_loglevel_setup);
872module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR);
873MODULE_PARM_DESC(ignore_loglevel, "ignore loglevel setting, to"
874 "print all kernel messages to the console.");
875
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700876#ifdef CONFIG_BOOT_PRINTK_DELAY
877
Namhyung Kim674dff62010-10-26 14:22:48 -0700878static int boot_delay; /* msecs delay after each printk during bootup */
Dave Young3a3b6ed2009-09-22 16:43:31 -0700879static unsigned long long loops_per_msec; /* based on boot_delay */
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700880
881static int __init boot_delay_setup(char *str)
882{
883 unsigned long lpj;
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700884
885 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
886 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
887
888 get_option(&str, &boot_delay);
889 if (boot_delay > 10 * 1000)
890 boot_delay = 0;
891
Dave Young3a3b6ed2009-09-22 16:43:31 -0700892 pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
893 "HZ: %d, loops_per_msec: %llu\n",
894 boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
Dave Young29e9d222013-11-12 15:08:53 -0800895 return 0;
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700896}
Dave Young29e9d222013-11-12 15:08:53 -0800897early_param("boot_delay", boot_delay_setup);
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700898
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800899static void boot_delay_msec(int level)
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700900{
901 unsigned long long k;
902 unsigned long timeout;
903
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800904 if ((boot_delay == 0 || system_state != SYSTEM_BOOTING)
905 || (level >= console_loglevel && !ignore_loglevel)) {
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700906 return;
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800907 }
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700908
Dave Young3a3b6ed2009-09-22 16:43:31 -0700909 k = (unsigned long long)loops_per_msec * boot_delay;
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700910
911 timeout = jiffies + msecs_to_jiffies(boot_delay);
912 while (k) {
913 k--;
914 cpu_relax();
915 /*
916 * use (volatile) jiffies to prevent
917 * compiler reduction; loop termination via jiffies
918 * is secondary and may or may not happen.
919 */
920 if (time_after(jiffies, timeout))
921 break;
922 touch_nmi_watchdog();
923 }
924}
925#else
Andrew Cooks2fa72c82012-12-17 15:59:56 -0800926static inline void boot_delay_msec(int level)
Randy Dunlapbfe8df32007-10-16 01:23:46 -0700927{
928}
929#endif
930
Kay Sievers7ff95542012-05-03 02:29:13 +0200931#if defined(CONFIG_PRINTK_TIME)
932static bool printk_time = 1;
933#else
934static bool printk_time;
935#endif
936module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
937
Kay Sievers084681d2012-06-28 09:38:53 +0200938static size_t print_time(u64 ts, char *buf)
939{
940 unsigned long rem_nsec;
941
942 if (!printk_time)
943 return 0;
944
Kay Sievers084681d2012-06-28 09:38:53 +0200945 rem_nsec = do_div(ts, 1000000000);
Roland Dreier35dac272013-01-04 15:35:50 -0800946
947 if (!buf)
948 return snprintf(NULL, 0, "[%5lu.000000] ", (unsigned long)ts);
949
Kay Sievers084681d2012-06-28 09:38:53 +0200950 return sprintf(buf, "[%5lu.%06lu] ",
951 (unsigned long)ts, rem_nsec / 1000);
952}
953
Joe Perches62e32ac2013-07-31 13:53:47 -0700954static size_t print_prefix(const struct printk_log *msg, bool syslog, char *buf)
Kay Sievers649e6ee2012-05-10 04:30:45 +0200955{
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200956 size_t len = 0;
Kay Sievers43a73a52012-07-06 09:50:09 -0700957 unsigned int prefix = (msg->facility << 3) | msg->level;
Kay Sievers649e6ee2012-05-10 04:30:45 +0200958
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200959 if (syslog) {
960 if (buf) {
Kay Sievers43a73a52012-07-06 09:50:09 -0700961 len += sprintf(buf, "<%u>", prefix);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200962 } else {
963 len += 3;
Kay Sievers43a73a52012-07-06 09:50:09 -0700964 if (prefix > 999)
965 len += 3;
966 else if (prefix > 99)
967 len += 2;
968 else if (prefix > 9)
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200969 len++;
970 }
Kay Sievers7ff95542012-05-03 02:29:13 +0200971 }
972
Kay Sievers084681d2012-06-28 09:38:53 +0200973 len += print_time(msg->ts_nsec, buf ? buf + len : NULL);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200974 return len;
975}
976
Joe Perches62e32ac2013-07-31 13:53:47 -0700977static size_t msg_print_text(const struct printk_log *msg, enum log_flags prev,
Kay Sievers5becfb12012-07-09 12:15:42 -0700978 bool syslog, char *buf, size_t size)
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200979{
980 const char *text = log_text(msg);
981 size_t text_size = msg->text_len;
Kay Sievers5becfb12012-07-09 12:15:42 -0700982 bool prefix = true;
983 bool newline = true;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200984 size_t len = 0;
985
Kay Sievers5becfb12012-07-09 12:15:42 -0700986 if ((prev & LOG_CONT) && !(msg->flags & LOG_PREFIX))
987 prefix = false;
988
989 if (msg->flags & LOG_CONT) {
990 if ((prev & LOG_CONT) && !(prev & LOG_NEWLINE))
991 prefix = false;
992
993 if (!(msg->flags & LOG_NEWLINE))
994 newline = false;
995 }
996
Kay Sievers3ce9a7c2012-05-13 23:30:46 +0200997 do {
998 const char *next = memchr(text, '\n', text_size);
999 size_t text_len;
1000
1001 if (next) {
1002 text_len = next - text;
1003 next++;
1004 text_size -= next - text;
1005 } else {
1006 text_len = text_size;
1007 }
1008
1009 if (buf) {
1010 if (print_prefix(msg, syslog, NULL) +
Kay Sievers70498252012-07-16 18:35:29 -07001011 text_len + 1 >= size - len)
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001012 break;
1013
Kay Sievers5becfb12012-07-09 12:15:42 -07001014 if (prefix)
1015 len += print_prefix(msg, syslog, buf + len);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001016 memcpy(buf + len, text, text_len);
1017 len += text_len;
Kay Sievers5becfb12012-07-09 12:15:42 -07001018 if (next || newline)
1019 buf[len++] = '\n';
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001020 } else {
1021 /* SYSLOG_ACTION_* buffer size only calculation */
Kay Sievers5becfb12012-07-09 12:15:42 -07001022 if (prefix)
1023 len += print_prefix(msg, syslog, NULL);
1024 len += text_len;
1025 if (next || newline)
1026 len++;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001027 }
1028
Kay Sievers5becfb12012-07-09 12:15:42 -07001029 prefix = true;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001030 text = next;
1031 } while (text);
1032
Kay Sievers7ff95542012-05-03 02:29:13 +02001033 return len;
1034}
1035
1036static int syslog_print(char __user *buf, int size)
1037{
1038 char *text;
Joe Perches62e32ac2013-07-31 13:53:47 -07001039 struct printk_log *msg;
Jan Beulich116e90b2012-06-22 16:36:09 +01001040 int len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001041
Kay Sievers70498252012-07-16 18:35:29 -07001042 text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
Kay Sievers7ff95542012-05-03 02:29:13 +02001043 if (!text)
1044 return -ENOMEM;
1045
Jan Beulich116e90b2012-06-22 16:36:09 +01001046 while (size > 0) {
1047 size_t n;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001048 size_t skip;
Kay Sievers7ff95542012-05-03 02:29:13 +02001049
Jan Beulich116e90b2012-06-22 16:36:09 +01001050 raw_spin_lock_irq(&logbuf_lock);
1051 if (syslog_seq < log_first_seq) {
1052 /* messages are gone, move to first one */
1053 syslog_seq = log_first_seq;
1054 syslog_idx = log_first_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001055 syslog_prev = 0;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001056 syslog_partial = 0;
Jan Beulich116e90b2012-06-22 16:36:09 +01001057 }
1058 if (syslog_seq == log_next_seq) {
1059 raw_spin_unlock_irq(&logbuf_lock);
1060 break;
1061 }
Kay Sieverseb02dac2012-07-09 10:05:10 -07001062
1063 skip = syslog_partial;
Jan Beulich116e90b2012-06-22 16:36:09 +01001064 msg = log_from_idx(syslog_idx);
Kay Sievers70498252012-07-16 18:35:29 -07001065 n = msg_print_text(msg, syslog_prev, true, text,
1066 LOG_LINE_MAX + PREFIX_MAX);
Kay Sieverseb02dac2012-07-09 10:05:10 -07001067 if (n - syslog_partial <= size) {
1068 /* message fits into buffer, move forward */
Jan Beulich116e90b2012-06-22 16:36:09 +01001069 syslog_idx = log_next(syslog_idx);
1070 syslog_seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07001071 syslog_prev = msg->flags;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001072 n -= syslog_partial;
1073 syslog_partial = 0;
1074 } else if (!len){
1075 /* partial read(), remember position */
1076 n = size;
1077 syslog_partial += n;
Jan Beulich116e90b2012-06-22 16:36:09 +01001078 } else
1079 n = 0;
1080 raw_spin_unlock_irq(&logbuf_lock);
1081
1082 if (!n)
1083 break;
1084
Kay Sieverseb02dac2012-07-09 10:05:10 -07001085 if (copy_to_user(buf, text + skip, n)) {
Jan Beulich116e90b2012-06-22 16:36:09 +01001086 if (!len)
1087 len = -EFAULT;
1088 break;
1089 }
Kay Sieverseb02dac2012-07-09 10:05:10 -07001090
1091 len += n;
1092 size -= n;
1093 buf += n;
Jan Beulich116e90b2012-06-22 16:36:09 +01001094 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001095
1096 kfree(text);
1097 return len;
1098}
1099
1100static int syslog_print_all(char __user *buf, int size, bool clear)
1101{
1102 char *text;
1103 int len = 0;
1104
Kay Sievers70498252012-07-16 18:35:29 -07001105 text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
Kay Sievers7ff95542012-05-03 02:29:13 +02001106 if (!text)
1107 return -ENOMEM;
1108
1109 raw_spin_lock_irq(&logbuf_lock);
1110 if (buf) {
1111 u64 next_seq;
1112 u64 seq;
1113 u32 idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001114 enum log_flags prev;
Kay Sievers7ff95542012-05-03 02:29:13 +02001115
1116 if (clear_seq < log_first_seq) {
1117 /* messages are gone, move to first available one */
1118 clear_seq = log_first_seq;
1119 clear_idx = log_first_idx;
1120 }
1121
1122 /*
1123 * Find first record that fits, including all following records,
1124 * into the user-provided buffer for this dump.
Kay Sieverse2ae7152012-06-15 14:07:51 +02001125 */
Kay Sievers7ff95542012-05-03 02:29:13 +02001126 seq = clear_seq;
1127 idx = clear_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001128 prev = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001129 while (seq < log_next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07001130 struct printk_log *msg = log_from_idx(idx);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001131
Kay Sievers5becfb12012-07-09 12:15:42 -07001132 len += msg_print_text(msg, prev, true, NULL, 0);
Jeff Mahoneye3756472012-08-10 15:07:09 -04001133 prev = msg->flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02001134 idx = log_next(idx);
1135 seq++;
1136 }
Kay Sieverse2ae7152012-06-15 14:07:51 +02001137
1138 /* move first record forward until length fits into the buffer */
Kay Sievers7ff95542012-05-03 02:29:13 +02001139 seq = clear_seq;
1140 idx = clear_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001141 prev = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001142 while (len > size && seq < log_next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07001143 struct printk_log *msg = log_from_idx(idx);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001144
Kay Sievers5becfb12012-07-09 12:15:42 -07001145 len -= msg_print_text(msg, prev, true, NULL, 0);
Jeff Mahoneye3756472012-08-10 15:07:09 -04001146 prev = msg->flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02001147 idx = log_next(idx);
1148 seq++;
1149 }
1150
Kay Sieverse2ae7152012-06-15 14:07:51 +02001151 /* last message fitting into this dump */
Kay Sievers7ff95542012-05-03 02:29:13 +02001152 next_seq = log_next_seq;
1153
1154 len = 0;
1155 while (len >= 0 && seq < next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07001156 struct printk_log *msg = log_from_idx(idx);
Kay Sievers7ff95542012-05-03 02:29:13 +02001157 int textlen;
1158
Kay Sievers70498252012-07-16 18:35:29 -07001159 textlen = msg_print_text(msg, prev, true, text,
1160 LOG_LINE_MAX + PREFIX_MAX);
Kay Sievers7ff95542012-05-03 02:29:13 +02001161 if (textlen < 0) {
1162 len = textlen;
1163 break;
1164 }
1165 idx = log_next(idx);
1166 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07001167 prev = msg->flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02001168
1169 raw_spin_unlock_irq(&logbuf_lock);
1170 if (copy_to_user(buf + len, text, textlen))
1171 len = -EFAULT;
1172 else
1173 len += textlen;
1174 raw_spin_lock_irq(&logbuf_lock);
1175
1176 if (seq < log_first_seq) {
1177 /* messages are gone, move to next one */
1178 seq = log_first_seq;
1179 idx = log_first_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001180 prev = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001181 }
1182 }
1183 }
1184
1185 if (clear) {
1186 clear_seq = log_next_seq;
1187 clear_idx = log_next_idx;
1188 }
1189 raw_spin_unlock_irq(&logbuf_lock);
1190
1191 kfree(text);
1192 return len;
1193}
1194
Kees Cook00234592010-02-03 15:36:43 -08001195int do_syslog(int type, char __user *buf, int len, bool from_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Kay Sievers7ff95542012-05-03 02:29:13 +02001197 bool clear = false;
1198 static int saved_console_loglevel = -1;
Linus Torvaldsee24aeb2011-02-10 17:53:55 -08001199 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Linus Torvaldsee24aeb2011-02-10 17:53:55 -08001201 error = check_syslog_permissions(type, from_file);
1202 if (error)
1203 goto out;
Eric Paris12b30522010-11-15 18:36:29 -05001204
1205 error = security_syslog(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 if (error)
1207 return error;
1208
1209 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08001210 case SYSLOG_ACTION_CLOSE: /* Close log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001212 case SYSLOG_ACTION_OPEN: /* Open log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001214 case SYSLOG_ACTION_READ: /* Read from log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 error = -EINVAL;
1216 if (!buf || len < 0)
1217 goto out;
1218 error = 0;
1219 if (!len)
1220 goto out;
1221 if (!access_ok(VERIFY_WRITE, buf, len)) {
1222 error = -EFAULT;
1223 goto out;
1224 }
Yuanhan Liu4a77a5a2012-06-16 21:21:51 +08001225 error = wait_event_interruptible(log_wait,
1226 syslog_seq != log_next_seq);
Kay Sieverscb424ff2012-07-06 09:50:09 -07001227 if (error)
Yuanhan Liu4a77a5a2012-06-16 21:21:51 +08001228 goto out;
Kay Sievers7ff95542012-05-03 02:29:13 +02001229 error = syslog_print(buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001231 /* Read/clear last kernel messages */
1232 case SYSLOG_ACTION_READ_CLEAR:
Kay Sievers7ff95542012-05-03 02:29:13 +02001233 clear = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 /* FALL THRU */
Kees Cookd78ca3c2010-02-03 15:37:13 -08001235 /* Read last kernel messages */
1236 case SYSLOG_ACTION_READ_ALL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 error = -EINVAL;
1238 if (!buf || len < 0)
1239 goto out;
1240 error = 0;
1241 if (!len)
1242 goto out;
1243 if (!access_ok(VERIFY_WRITE, buf, len)) {
1244 error = -EFAULT;
1245 goto out;
1246 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001247 error = syslog_print_all(buf, len, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001249 /* Clear ring buffer */
1250 case SYSLOG_ACTION_CLEAR:
Kay Sievers7ff95542012-05-03 02:29:13 +02001251 syslog_print_all(NULL, 0, true);
Alan Stern4661e352012-06-22 17:12:19 -04001252 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001253 /* Disable logging to console */
1254 case SYSLOG_ACTION_CONSOLE_OFF:
Frans Pop1aaad492009-07-06 13:31:48 +02001255 if (saved_console_loglevel == -1)
1256 saved_console_loglevel = console_loglevel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 console_loglevel = minimum_console_loglevel;
1258 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001259 /* Enable logging to console */
1260 case SYSLOG_ACTION_CONSOLE_ON:
Frans Pop1aaad492009-07-06 13:31:48 +02001261 if (saved_console_loglevel != -1) {
1262 console_loglevel = saved_console_loglevel;
1263 saved_console_loglevel = -1;
1264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001266 /* Set level of messages printed to console */
1267 case SYSLOG_ACTION_CONSOLE_LEVEL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 error = -EINVAL;
1269 if (len < 1 || len > 8)
1270 goto out;
1271 if (len < minimum_console_loglevel)
1272 len = minimum_console_loglevel;
1273 console_loglevel = len;
Frans Pop1aaad492009-07-06 13:31:48 +02001274 /* Implicitly re-enable logging to console */
1275 saved_console_loglevel = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 error = 0;
1277 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001278 /* Number of chars in the log buffer */
1279 case SYSLOG_ACTION_SIZE_UNREAD:
Kay Sievers7ff95542012-05-03 02:29:13 +02001280 raw_spin_lock_irq(&logbuf_lock);
1281 if (syslog_seq < log_first_seq) {
1282 /* messages are gone, move to first one */
1283 syslog_seq = log_first_seq;
1284 syslog_idx = log_first_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07001285 syslog_prev = 0;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001286 syslog_partial = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001287 }
1288 if (from_file) {
1289 /*
1290 * Short-cut for poll(/"proc/kmsg") which simply checks
1291 * for pending data, not the size; return the count of
1292 * records, not the length.
1293 */
1294 error = log_next_idx - syslog_idx;
1295 } else {
Kay Sievers5becfb12012-07-09 12:15:42 -07001296 u64 seq = syslog_seq;
1297 u32 idx = syslog_idx;
1298 enum log_flags prev = syslog_prev;
Kay Sievers7ff95542012-05-03 02:29:13 +02001299
1300 error = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001301 while (seq < log_next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07001302 struct printk_log *msg = log_from_idx(idx);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001303
Kay Sievers5becfb12012-07-09 12:15:42 -07001304 error += msg_print_text(msg, prev, true, NULL, 0);
Kay Sievers7ff95542012-05-03 02:29:13 +02001305 idx = log_next(idx);
1306 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07001307 prev = msg->flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02001308 }
Kay Sieverseb02dac2012-07-09 10:05:10 -07001309 error -= syslog_partial;
Kay Sievers7ff95542012-05-03 02:29:13 +02001310 }
1311 raw_spin_unlock_irq(&logbuf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001313 /* Size of the log buffer */
1314 case SYSLOG_ACTION_SIZE_BUFFER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 error = log_buf_len;
1316 break;
1317 default:
1318 error = -EINVAL;
1319 break;
1320 }
1321out:
1322 return error;
1323}
1324
Heiko Carstens1e7bfb22009-01-14 14:14:29 +01001325SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Kees Cook637241a2013-06-12 14:04:39 -07001327 return do_syslog(type, buf, len, SYSLOG_FROM_READER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 * Call the console drivers, asking them to write out
1332 * log_buf[start] to log_buf[end - 1].
Torben Hohnac751ef2011-01-25 15:07:35 -08001333 * The console_lock must be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 */
Kay Sievers7ff95542012-05-03 02:29:13 +02001335static void call_console_drivers(int level, const char *text, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
Kay Sievers7ff95542012-05-03 02:29:13 +02001337 struct console *con;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
zhangwei(Jovi)07c65f42013-04-29 16:17:16 -07001339 trace_console(text, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Kay Sievers7ff95542012-05-03 02:29:13 +02001341 if (level >= console_loglevel && !ignore_loglevel)
1342 return;
1343 if (!console_drivers)
1344 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Kay Sievers7ff95542012-05-03 02:29:13 +02001346 for_each_console(con) {
1347 if (exclusive_console && con != exclusive_console)
1348 continue;
1349 if (!(con->flags & CON_ENABLED))
1350 continue;
1351 if (!con->write)
1352 continue;
1353 if (!cpu_online(smp_processor_id()) &&
1354 !(con->flags & CON_ANYTIME))
1355 continue;
1356 con->write(con, text, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
1359
1360/*
1361 * Zap console related locks when oopsing. Only zap at most once
1362 * every 10 seconds, to leave time for slow consoles to print a
1363 * full oops.
1364 */
1365static void zap_locks(void)
1366{
1367 static unsigned long oops_timestamp;
1368
1369 if (time_after_eq(jiffies, oops_timestamp) &&
Jesper Juhl40dc5652005-10-30 15:02:46 -08001370 !time_after(jiffies, oops_timestamp + 30 * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return;
1372
1373 oops_timestamp = jiffies;
1374
Peter Zijlstra94d24fc2011-06-07 11:17:30 +02001375 debug_locks_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 /* If a crash is occurring, make sure we can't deadlock */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001377 raw_spin_lock_init(&logbuf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 /* And make sure that we print immediately */
Thomas Gleixner5b8c4f22010-09-07 14:33:43 +00001379 sema_init(&console_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
1381
Michael Ellerman76a8ad22006-06-25 05:47:40 -07001382/* Check if we have any console registered that can be called early in boot. */
1383static int have_callable_console(void)
1384{
1385 struct console *con;
1386
Robin Getz4d091612009-07-01 21:08:37 -04001387 for_each_console(con)
Michael Ellerman76a8ad22006-06-25 05:47:40 -07001388 if (con->flags & CON_ANYTIME)
1389 return 1;
1390
1391 return 0;
1392}
1393
Linus Torvalds266c2e02008-03-24 19:25:08 -07001394/*
1395 * Can we actually use the console at this time on this cpu?
1396 *
1397 * Console drivers may assume that per-cpu resources have
1398 * been allocated. So unless they're explicitly marked as
1399 * being able to cope (CON_ANYTIME) don't call them until
1400 * this CPU is officially up.
1401 */
1402static inline int can_use_console(unsigned int cpu)
1403{
1404 return cpu_online(cpu) || have_callable_console();
1405}
1406
1407/*
1408 * Try to get console ownership to actually show the kernel
1409 * messages from a 'printk'. Return true (and with the
Torben Hohnac751ef2011-01-25 15:07:35 -08001410 * console_lock held, and 'console_locked' set) if it
Linus Torvalds266c2e02008-03-24 19:25:08 -07001411 * is successful, false otherwise.
1412 *
1413 * This gets called with the 'logbuf_lock' spinlock held and
1414 * interrupts disabled. It should return with 'lockbuf_lock'
1415 * released but interrupts still disabled.
1416 */
Torben Hohnac751ef2011-01-25 15:07:35 -08001417static int console_trylock_for_printk(unsigned int cpu)
Namhyung Kim8155c022010-10-26 14:22:47 -07001418 __releases(&logbuf_lock)
Linus Torvalds266c2e02008-03-24 19:25:08 -07001419{
Peter Zijlstra0b5e1c52011-06-07 11:15:33 +02001420 int retval = 0, wake = 0;
Linus Torvalds266c2e02008-03-24 19:25:08 -07001421
Torben Hohnac751ef2011-01-25 15:07:35 -08001422 if (console_trylock()) {
Linus Torvalds093a07e2008-04-15 13:09:54 -07001423 retval = 1;
1424
1425 /*
1426 * If we can't use the console, we need to release
1427 * the console semaphore by hand to avoid flushing
1428 * the buffer. We need to hold the console semaphore
1429 * in order to do this test safely.
1430 */
1431 if (!can_use_console(cpu)) {
1432 console_locked = 0;
Peter Zijlstra0b5e1c52011-06-07 11:15:33 +02001433 wake = 1;
Linus Torvalds093a07e2008-04-15 13:09:54 -07001434 retval = 0;
1435 }
1436 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001437 logbuf_cpu = UINT_MAX;
Bu, Yitiandbda92d2013-02-18 12:53:37 +00001438 raw_spin_unlock(&logbuf_lock);
Peter Zijlstra0b5e1c52011-06-07 11:15:33 +02001439 if (wake)
1440 up(&console_sem);
Linus Torvalds266c2e02008-03-24 19:25:08 -07001441 return retval;
1442}
Ingo Molnar32a76002008-01-25 21:07:58 +01001443
Dave Youngaf913222009-09-22 16:43:33 -07001444int printk_delay_msec __read_mostly;
1445
1446static inline void printk_delay(void)
1447{
1448 if (unlikely(printk_delay_msec)) {
1449 int m = printk_delay_msec;
1450
1451 while (m--) {
1452 mdelay(1);
1453 touch_nmi_watchdog();
1454 }
1455 }
1456}
1457
Kay Sievers084681d2012-06-28 09:38:53 +02001458/*
1459 * Continuation lines are buffered, and not committed to the record buffer
1460 * until the line is complete, or a race forces it. The line fragments
1461 * though, are printed immediately to the consoles to ensure everything has
1462 * reached the console in case of a kernel crash.
1463 */
1464static struct cont {
1465 char buf[LOG_LINE_MAX];
1466 size_t len; /* length == 0 means unused buffer */
1467 size_t cons; /* bytes written to console */
1468 struct task_struct *owner; /* task of first print*/
1469 u64 ts_nsec; /* time of first print */
1470 u8 level; /* log level of first message */
1471 u8 facility; /* log level of first message */
Kay Sieverseab07262012-07-16 18:35:30 -07001472 enum log_flags flags; /* prefix, newline flags */
Kay Sievers084681d2012-06-28 09:38:53 +02001473 bool flushed:1; /* buffer sealed and committed */
1474} cont;
1475
Kay Sievers70498252012-07-16 18:35:29 -07001476static void cont_flush(enum log_flags flags)
Kay Sievers084681d2012-06-28 09:38:53 +02001477{
1478 if (cont.flushed)
1479 return;
1480 if (cont.len == 0)
1481 return;
1482
Kay Sieverseab07262012-07-16 18:35:30 -07001483 if (cont.cons) {
1484 /*
1485 * If a fragment of this line was directly flushed to the
1486 * console; wait for the console to pick up the rest of the
1487 * line. LOG_NOCONS suppresses a duplicated output.
1488 */
1489 log_store(cont.facility, cont.level, flags | LOG_NOCONS,
1490 cont.ts_nsec, NULL, 0, cont.buf, cont.len);
1491 cont.flags = flags;
1492 cont.flushed = true;
1493 } else {
1494 /*
1495 * If no fragment of this line ever reached the console,
1496 * just submit it to the store and free the buffer.
1497 */
1498 log_store(cont.facility, cont.level, flags, 0,
1499 NULL, 0, cont.buf, cont.len);
1500 cont.len = 0;
1501 }
Kay Sievers084681d2012-06-28 09:38:53 +02001502}
1503
1504static bool cont_add(int facility, int level, const char *text, size_t len)
1505{
1506 if (cont.len && cont.flushed)
1507 return false;
1508
1509 if (cont.len + len > sizeof(cont.buf)) {
Kay Sievers70498252012-07-16 18:35:29 -07001510 /* the line gets too long, split it up in separate records */
1511 cont_flush(LOG_CONT);
Kay Sievers084681d2012-06-28 09:38:53 +02001512 return false;
1513 }
1514
1515 if (!cont.len) {
1516 cont.facility = facility;
1517 cont.level = level;
1518 cont.owner = current;
1519 cont.ts_nsec = local_clock();
Kay Sieverseab07262012-07-16 18:35:30 -07001520 cont.flags = 0;
Kay Sievers084681d2012-06-28 09:38:53 +02001521 cont.cons = 0;
1522 cont.flushed = false;
1523 }
1524
1525 memcpy(cont.buf + cont.len, text, len);
1526 cont.len += len;
Kay Sieverseab07262012-07-16 18:35:30 -07001527
1528 if (cont.len > (sizeof(cont.buf) * 80) / 100)
1529 cont_flush(LOG_CONT);
1530
Kay Sievers084681d2012-06-28 09:38:53 +02001531 return true;
1532}
1533
1534static size_t cont_print_text(char *text, size_t size)
1535{
1536 size_t textlen = 0;
1537 size_t len;
1538
Kay Sieverseab07262012-07-16 18:35:30 -07001539 if (cont.cons == 0 && (console_prev & LOG_NEWLINE)) {
Kay Sievers084681d2012-06-28 09:38:53 +02001540 textlen += print_time(cont.ts_nsec, text);
1541 size -= textlen;
1542 }
1543
1544 len = cont.len - cont.cons;
1545 if (len > 0) {
1546 if (len+1 > size)
1547 len = size-1;
1548 memcpy(text + textlen, cont.buf + cont.cons, len);
1549 textlen += len;
1550 cont.cons = cont.len;
1551 }
1552
1553 if (cont.flushed) {
Kay Sieverseab07262012-07-16 18:35:30 -07001554 if (cont.flags & LOG_NEWLINE)
1555 text[textlen++] = '\n';
Kay Sievers084681d2012-06-28 09:38:53 +02001556 /* got everything, release buffer */
1557 cont.len = 0;
1558 }
1559 return textlen;
1560}
1561
Kay Sievers7ff95542012-05-03 02:29:13 +02001562asmlinkage int vprintk_emit(int facility, int level,
1563 const char *dict, size_t dictlen,
1564 const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
Kay Sievers7ff95542012-05-03 02:29:13 +02001566 static int recursion_bug;
Kay Sievers7ff95542012-05-03 02:29:13 +02001567 static char textbuf[LOG_LINE_MAX];
1568 char *text = textbuf;
Kay Sieversc313af12012-05-14 20:46:27 +02001569 size_t text_len;
Kay Sievers5becfb12012-07-09 12:15:42 -07001570 enum log_flags lflags = 0;
Nick Andrewac60ad72008-05-12 21:21:04 +02001571 unsigned long flags;
Ingo Molnar32a76002008-01-25 21:07:58 +01001572 int this_cpu;
Kay Sievers7ff95542012-05-03 02:29:13 +02001573 int printed_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Andrew Cooks2fa72c82012-12-17 15:59:56 -08001575 boot_delay_msec(level);
Dave Youngaf913222009-09-22 16:43:33 -07001576 printk_delay();
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 /* This stops the holder of console_sem just where we want him */
Peter Zijlstra1a9a8ae2011-06-07 11:17:30 +02001579 local_irq_save(flags);
Ingo Molnar32a76002008-01-25 21:07:58 +01001580 this_cpu = smp_processor_id();
1581
1582 /*
1583 * Ouch, printk recursed into itself!
1584 */
Kay Sievers7ff95542012-05-03 02:29:13 +02001585 if (unlikely(logbuf_cpu == this_cpu)) {
Ingo Molnar32a76002008-01-25 21:07:58 +01001586 /*
1587 * If a crash is occurring during printk() on this CPU,
1588 * then try to get the crash message out but make sure
1589 * we can't deadlock. Otherwise just return to avoid the
1590 * recursion and return - but flag the recursion so that
1591 * it can be printed at the next appropriate moment:
1592 */
Peter Zijlstra94d24fc2011-06-07 11:17:30 +02001593 if (!oops_in_progress && !lockdep_recursing(current)) {
Tejun Heo3b8945e2008-05-12 21:21:04 +02001594 recursion_bug = 1;
Ingo Molnar32a76002008-01-25 21:07:58 +01001595 goto out_restore_irqs;
1596 }
1597 zap_locks();
1598 }
1599
Ingo Molnara0f1ccf2006-07-03 00:24:58 -07001600 lockdep_off();
Thomas Gleixner07354eb2009-07-25 17:50:36 +02001601 raw_spin_lock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02001602 logbuf_cpu = this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Tejun Heo3b8945e2008-05-12 21:21:04 +02001604 if (recursion_bug) {
Kay Sievers7ff95542012-05-03 02:29:13 +02001605 static const char recursion_msg[] =
1606 "BUG: recent printk recursion!";
1607
Tejun Heo3b8945e2008-05-12 21:21:04 +02001608 recursion_bug = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001609 printed_len += strlen(recursion_msg);
1610 /* emit KERN_CRIT message */
Kay Sievers5becfb12012-07-09 12:15:42 -07001611 log_store(0, 2, LOG_PREFIX|LOG_NEWLINE, 0,
Kay Sievers084681d2012-06-28 09:38:53 +02001612 NULL, 0, recursion_msg, printed_len);
Linus Torvalds5fd29d62009-06-16 10:57:02 -07001613 }
1614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 /*
Kay Sievers7ff95542012-05-03 02:29:13 +02001616 * The printf needs to come first; we need the syslog
1617 * prefix which might be passed-in as a parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 */
Kay Sieversc313af12012-05-14 20:46:27 +02001619 text_len = vscnprintf(text, sizeof(textbuf), fmt, args);
Nick Andrewac60ad72008-05-12 21:21:04 +02001620
Kay Sievers7ff95542012-05-03 02:29:13 +02001621 /* mark and strip a trailing newline */
Kay Sieversc313af12012-05-14 20:46:27 +02001622 if (text_len && text[text_len-1] == '\n') {
1623 text_len--;
Kay Sievers5becfb12012-07-09 12:15:42 -07001624 lflags |= LOG_NEWLINE;
Kay Sievers7ff95542012-05-03 02:29:13 +02001625 }
Kay Sievers9d90c8d2011-03-13 03:19:51 +01001626
Joe Perches088a52a2012-07-30 14:40:19 -07001627 /* strip kernel syslog prefix and extract log level or control flags */
1628 if (facility == 0) {
1629 int kern_level = printk_get_level(text);
1630
1631 if (kern_level) {
1632 const char *end_of_header = printk_skip_level(text);
1633 switch (kern_level) {
1634 case '0' ... '7':
1635 if (level == -1)
1636 level = kern_level - '0';
1637 case 'd': /* KERN_DEFAULT */
1638 lflags |= LOG_PREFIX;
Joe Perches088a52a2012-07-30 14:40:19 -07001639 }
Petr Mladeke8c42d32014-04-03 14:48:41 -07001640 /*
1641 * No need to check length here because vscnprintf
1642 * put '\0' at the end of the string. Only valid and
1643 * newly printed level is detected.
1644 */
Joe Perches088a52a2012-07-30 14:40:19 -07001645 text_len -= end_of_header - text;
1646 text = (char *)end_of_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001648 }
Nick Andrewac60ad72008-05-12 21:21:04 +02001649
Kay Sieversc313af12012-05-14 20:46:27 +02001650 if (level == -1)
1651 level = default_message_loglevel;
1652
Kay Sievers5becfb12012-07-09 12:15:42 -07001653 if (dict)
1654 lflags |= LOG_PREFIX|LOG_NEWLINE;
Kay Sievers7ff95542012-05-03 02:29:13 +02001655
Kay Sievers5becfb12012-07-09 12:15:42 -07001656 if (!(lflags & LOG_NEWLINE)) {
Kay Sievers084681d2012-06-28 09:38:53 +02001657 /*
1658 * Flush the conflicting buffer. An earlier newline was missing,
1659 * or another task also prints continuation lines.
1660 */
Kay Sievers5becfb12012-07-09 12:15:42 -07001661 if (cont.len && (lflags & LOG_PREFIX || cont.owner != current))
Kay Sieverseab07262012-07-16 18:35:30 -07001662 cont_flush(LOG_NEWLINE);
Kay Sieversc313af12012-05-14 20:46:27 +02001663
Kay Sievers084681d2012-06-28 09:38:53 +02001664 /* buffer line if possible, otherwise store it right away */
1665 if (!cont_add(facility, level, text, text_len))
Kay Sievers5becfb12012-07-09 12:15:42 -07001666 log_store(facility, level, lflags | LOG_CONT, 0,
Kay Sieversc313af12012-05-14 20:46:27 +02001667 dict, dictlen, text, text_len);
Kay Sievers084681d2012-06-28 09:38:53 +02001668 } else {
1669 bool stored = false;
1670
1671 /*
Steven Rostedtd3620822012-06-29 11:40:11 -04001672 * If an earlier newline was missing and it was the same task,
1673 * either merge it with the current buffer and flush, or if
1674 * there was a race with interrupts (prefix == true) then just
1675 * flush it out and store this line separately.
Arun KS1d3fa372014-01-23 15:54:19 -08001676 * If the preceding printk was from a different task and missed
1677 * a newline, flush and append the newline.
Kay Sievers084681d2012-06-28 09:38:53 +02001678 */
Arun KS1d3fa372014-01-23 15:54:19 -08001679 if (cont.len) {
1680 if (cont.owner == current && !(lflags & LOG_PREFIX))
1681 stored = cont_add(facility, level, text,
1682 text_len);
Kay Sieverseab07262012-07-16 18:35:30 -07001683 cont_flush(LOG_NEWLINE);
Kay Sieversc313af12012-05-14 20:46:27 +02001684 }
Kay Sievers084681d2012-06-28 09:38:53 +02001685
1686 if (!stored)
Kay Sievers5becfb12012-07-09 12:15:42 -07001687 log_store(facility, level, lflags, 0,
Kay Sievers084681d2012-06-28 09:38:53 +02001688 dict, dictlen, text, text_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 }
Kay Sievers084681d2012-06-28 09:38:53 +02001690 printed_len += text_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Linus Torvalds266c2e02008-03-24 19:25:08 -07001692 /*
Kay Sievers7ff95542012-05-03 02:29:13 +02001693 * Try to acquire and then immediately release the console semaphore.
1694 * The release will print out buffers and wake up /dev/kmsg and syslog()
1695 * users.
Linus Torvalds266c2e02008-03-24 19:25:08 -07001696 *
Kay Sievers7ff95542012-05-03 02:29:13 +02001697 * The console_trylock_for_printk() function will release 'logbuf_lock'
1698 * regardless of whether it actually gets the console semaphore or not.
Linus Torvalds266c2e02008-03-24 19:25:08 -07001699 */
Torben Hohnac751ef2011-01-25 15:07:35 -08001700 if (console_trylock_for_printk(this_cpu))
1701 console_unlock();
Michael Ellerman76a8ad22006-06-25 05:47:40 -07001702
Linus Torvalds266c2e02008-03-24 19:25:08 -07001703 lockdep_on();
Ingo Molnar32a76002008-01-25 21:07:58 +01001704out_restore_irqs:
Peter Zijlstra1a9a8ae2011-06-07 11:17:30 +02001705 local_irq_restore(flags);
Michael Ellerman76a8ad22006-06-25 05:47:40 -07001706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 return printed_len;
1708}
Kay Sievers7ff95542012-05-03 02:29:13 +02001709EXPORT_SYMBOL(vprintk_emit);
1710
1711asmlinkage int vprintk(const char *fmt, va_list args)
1712{
1713 return vprintk_emit(0, -1, NULL, 0, fmt, args);
1714}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715EXPORT_SYMBOL(vprintk);
1716
Kay Sievers7ff95542012-05-03 02:29:13 +02001717asmlinkage int printk_emit(int facility, int level,
1718 const char *dict, size_t dictlen,
1719 const char *fmt, ...)
1720{
1721 va_list args;
1722 int r;
1723
1724 va_start(args, fmt);
1725 r = vprintk_emit(facility, level, dict, dictlen, fmt, args);
1726 va_end(args);
1727
1728 return r;
1729}
1730EXPORT_SYMBOL(printk_emit);
1731
1732/**
1733 * printk - print a kernel message
1734 * @fmt: format string
1735 *
1736 * This is printk(). It can be called from any context. We want it to work.
1737 *
1738 * We try to grab the console_lock. If we succeed, it's easy - we log the
1739 * output and call the console drivers. If we fail to get the semaphore, we
1740 * place the output into the log buffer and return. The current holder of
1741 * the console_sem will notice the new output in console_unlock(); and will
1742 * send it to the consoles before releasing the lock.
1743 *
1744 * One effect of this deferred printing is that code which calls printk() and
1745 * then changes console_loglevel may break. This is because console_loglevel
1746 * is inspected when the actual printing occurs.
1747 *
1748 * See also:
1749 * printf(3)
1750 *
1751 * See the vsnprintf() documentation for format string extensions over C99.
1752 */
Andi Kleen722a9f92014-05-02 00:44:38 +02001753asmlinkage __visible int printk(const char *fmt, ...)
Kay Sievers7ff95542012-05-03 02:29:13 +02001754{
1755 va_list args;
1756 int r;
1757
1758#ifdef CONFIG_KGDB_KDB
1759 if (unlikely(kdb_trap_printk)) {
1760 va_start(args, fmt);
1761 r = vkdb_printf(fmt, args);
1762 va_end(args);
1763 return r;
1764 }
1765#endif
1766 va_start(args, fmt);
1767 r = vprintk_emit(0, -1, NULL, 0, fmt, args);
1768 va_end(args);
1769
1770 return r;
1771}
1772EXPORT_SYMBOL(printk);
Kay Sievers7f3a7812012-05-09 01:37:51 +02001773
Kay Sievers96efedf2012-07-16 18:35:29 -07001774#else /* CONFIG_PRINTK */
Matt Mackalld59745c2005-05-01 08:59:02 -07001775
Kay Sievers70498252012-07-16 18:35:29 -07001776#define LOG_LINE_MAX 0
1777#define PREFIX_MAX 0
Kay Sievers7f3a7812012-05-09 01:37:51 +02001778#define LOG_LINE_MAX 0
Kay Sievers96efedf2012-07-16 18:35:29 -07001779static u64 syslog_seq;
1780static u32 syslog_idx;
Kay Sieverseab07262012-07-16 18:35:30 -07001781static u64 console_seq;
1782static u32 console_idx;
Kay Sievers96efedf2012-07-16 18:35:29 -07001783static enum log_flags syslog_prev;
1784static u64 log_first_seq;
1785static u32 log_first_idx;
1786static u64 log_next_seq;
Kay Sieverseab07262012-07-16 18:35:30 -07001787static enum log_flags console_prev;
Kay Sievers084681d2012-06-28 09:38:53 +02001788static struct cont {
1789 size_t len;
1790 size_t cons;
1791 u8 level;
1792 bool flushed:1;
1793} cont;
Joe Perches62e32ac2013-07-31 13:53:47 -07001794static struct printk_log *log_from_idx(u32 idx) { return NULL; }
Kay Sievers7f3a7812012-05-09 01:37:51 +02001795static u32 log_next(u32 idx) { return 0; }
Kay Sievers7f3a7812012-05-09 01:37:51 +02001796static void call_console_drivers(int level, const char *text, size_t len) {}
Joe Perches62e32ac2013-07-31 13:53:47 -07001797static size_t msg_print_text(const struct printk_log *msg, enum log_flags prev,
Kay Sievers5becfb12012-07-09 12:15:42 -07001798 bool syslog, char *buf, size_t size) { return 0; }
Kay Sievers084681d2012-06-28 09:38:53 +02001799static size_t cont_print_text(char *text, size_t size) { return 0; }
Matt Mackalld59745c2005-05-01 08:59:02 -07001800
Kay Sievers7f3a7812012-05-09 01:37:51 +02001801#endif /* CONFIG_PRINTK */
Matt Mackalld59745c2005-05-01 08:59:02 -07001802
Thomas Gleixnerd0380e62013-04-29 16:17:18 -07001803#ifdef CONFIG_EARLY_PRINTK
1804struct console *early_console;
1805
1806void early_vprintk(const char *fmt, va_list ap)
1807{
1808 if (early_console) {
1809 char buf[512];
1810 int n = vscnprintf(buf, sizeof(buf), fmt, ap);
1811
1812 early_console->write(early_console, buf, n);
1813 }
1814}
1815
Andi Kleen722a9f92014-05-02 00:44:38 +02001816asmlinkage __visible void early_printk(const char *fmt, ...)
Thomas Gleixnerd0380e62013-04-29 16:17:18 -07001817{
1818 va_list ap;
1819
1820 va_start(ap, fmt);
1821 early_vprintk(fmt, ap);
1822 va_end(ap);
1823}
1824#endif
1825
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001826static int __add_preferred_console(char *name, int idx, char *options,
1827 char *brl_options)
1828{
1829 struct console_cmdline *c;
1830 int i;
1831
1832 /*
1833 * See if this tty is not yet registered, and
1834 * if we have a slot free.
1835 */
Joe Perches23475402013-07-31 13:53:46 -07001836 for (i = 0, c = console_cmdline;
1837 i < MAX_CMDLINECONSOLES && c->name[0];
1838 i++, c++) {
1839 if (strcmp(c->name, name) == 0 && c->index == idx) {
1840 if (!brl_options)
1841 selected_console = i;
1842 return 0;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001843 }
Joe Perches23475402013-07-31 13:53:46 -07001844 }
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001845 if (i == MAX_CMDLINECONSOLES)
1846 return -E2BIG;
1847 if (!brl_options)
1848 selected_console = i;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001849 strlcpy(c->name, name, sizeof(c->name));
1850 c->options = options;
Joe Perchesbbeddf52013-07-31 13:53:45 -07001851 braille_set_options(c, brl_options);
1852
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001853 c->index = idx;
1854 return 0;
1855}
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001856/*
1857 * Set up a list of consoles. Called from init/main.c
1858 */
1859static int __init console_setup(char *str)
1860{
Yinghai Lueaa944a2007-07-15 23:37:27 -07001861 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001862 char *s, *options, *brl_options = NULL;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001863 int idx;
1864
Joe Perchesbbeddf52013-07-31 13:53:45 -07001865 if (_braille_console_setup(&str, &brl_options))
1866 return 1;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001867
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001868 /*
1869 * Decode str into name, index, options.
1870 */
1871 if (str[0] >= '0' && str[0] <= '9') {
Yinghai Lueaa944a2007-07-15 23:37:27 -07001872 strcpy(buf, "ttyS");
1873 strncpy(buf + 4, str, sizeof(buf) - 5);
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001874 } else {
Yinghai Lueaa944a2007-07-15 23:37:27 -07001875 strncpy(buf, str, sizeof(buf) - 1);
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001876 }
Yinghai Lueaa944a2007-07-15 23:37:27 -07001877 buf[sizeof(buf) - 1] = 0;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001878 if ((options = strchr(str, ',')) != NULL)
1879 *(options++) = 0;
1880#ifdef __sparc__
1881 if (!strcmp(str, "ttya"))
Yinghai Lueaa944a2007-07-15 23:37:27 -07001882 strcpy(buf, "ttyS0");
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001883 if (!strcmp(str, "ttyb"))
Yinghai Lueaa944a2007-07-15 23:37:27 -07001884 strcpy(buf, "ttyS1");
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001885#endif
Yinghai Lueaa944a2007-07-15 23:37:27 -07001886 for (s = buf; *s; s++)
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001887 if ((*s >= '0' && *s <= '9') || *s == ',')
1888 break;
1889 idx = simple_strtoul(s, NULL, 10);
1890 *s = 0;
1891
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001892 __add_preferred_console(buf, idx, options, brl_options);
Markus Armbruster9e124fe2008-05-26 23:31:07 +01001893 console_set_on_cmdline = 1;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001894 return 1;
1895}
1896__setup("console=", console_setup);
1897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898/**
Matt Mackall3c0547b2005-05-16 21:53:47 -07001899 * add_preferred_console - add a device to the list of preferred consoles.
Martin Waitzddad86c2005-11-13 16:08:14 -08001900 * @name: device name
1901 * @idx: device index
1902 * @options: options for this console
Matt Mackall3c0547b2005-05-16 21:53:47 -07001903 *
1904 * The last preferred console added will be used for kernel messages
1905 * and stdin/out/err for init. Normally this is used by console_setup
1906 * above to handle user-supplied console arguments; however it can also
1907 * be used by arch-specific code either to override the user or more
1908 * commonly to provide a default console (ie from PROM variables) when
1909 * the user has not supplied one.
1910 */
David S. Millerfb445ee2007-12-29 01:19:49 -08001911int add_preferred_console(char *name, int idx, char *options)
Matt Mackall3c0547b2005-05-16 21:53:47 -07001912{
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001913 return __add_preferred_console(name, idx, options, NULL);
Matt Mackall3c0547b2005-05-16 21:53:47 -07001914}
1915
Daniel Ritzb6b1d872007-08-03 16:07:43 +02001916int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
Yinghai Lu18a8bd92007-07-15 23:37:59 -07001917{
1918 struct console_cmdline *c;
1919 int i;
1920
Joe Perches23475402013-07-31 13:53:46 -07001921 for (i = 0, c = console_cmdline;
1922 i < MAX_CMDLINECONSOLES && c->name[0];
1923 i++, c++)
1924 if (strcmp(c->name, name) == 0 && c->index == idx) {
1925 strlcpy(c->name, name_new, sizeof(c->name));
1926 c->name[sizeof(c->name) - 1] = 0;
1927 c->options = options;
1928 c->index = idx_new;
1929 return i;
Yinghai Lu18a8bd92007-07-15 23:37:59 -07001930 }
1931 /* not found */
1932 return -1;
1933}
1934
Rusty Russell2329abf2012-01-13 09:32:18 +10301935bool console_suspend_enabled = 1;
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001936EXPORT_SYMBOL(console_suspend_enabled);
1937
1938static int __init console_suspend_disable(char *str)
1939{
1940 console_suspend_enabled = 0;
1941 return 1;
1942}
1943__setup("no_console_suspend", console_suspend_disable);
Yanmin Zhang134620f2011-10-31 17:11:27 -07001944module_param_named(console_suspend, console_suspend_enabled,
1945 bool, S_IRUGO | S_IWUSR);
1946MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
1947 " and hibernate operations");
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001948
Matt Mackall3c0547b2005-05-16 21:53:47 -07001949/**
Linus Torvalds557240b2006-06-19 18:16:01 -07001950 * suspend_console - suspend the console subsystem
1951 *
1952 * This disables printk() while we go into suspend states
1953 */
1954void suspend_console(void)
1955{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001956 if (!console_suspend_enabled)
1957 return;
Pavel Machek0d630812008-07-23 21:28:32 -07001958 printk("Suspending console(s) (use no_console_suspend to debug)\n");
Torben Hohnac751ef2011-01-25 15:07:35 -08001959 console_lock();
Linus Torvalds557240b2006-06-19 18:16:01 -07001960 console_suspended = 1;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001961 up(&console_sem);
Jane Li72581482014-04-03 14:48:45 -07001962 mutex_release(&console_lock_dep_map, 1, _RET_IP_);
Linus Torvalds557240b2006-06-19 18:16:01 -07001963}
1964
1965void resume_console(void)
1966{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07001967 if (!console_suspend_enabled)
1968 return;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01001969 down(&console_sem);
Jane Li72581482014-04-03 14:48:45 -07001970 mutex_acquire(&console_lock_dep_map, 0, 0, _RET_IP_);
Linus Torvalds557240b2006-06-19 18:16:01 -07001971 console_suspended = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08001972 console_unlock();
Linus Torvalds557240b2006-06-19 18:16:01 -07001973}
1974
1975/**
Kevin Cernekee034260d2010-06-03 22:11:25 -07001976 * console_cpu_notify - print deferred console messages after CPU hotplug
1977 * @self: notifier struct
1978 * @action: CPU hotplug event
1979 * @hcpu: unused
1980 *
1981 * If printk() is called from a CPU that is not online yet, the messages
1982 * will be spooled but will not show up on the console. This function is
1983 * called when a new CPU comes online (or fails to come up), and ensures
1984 * that any such output gets printed.
1985 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04001986static int console_cpu_notify(struct notifier_block *self,
Kevin Cernekee034260d2010-06-03 22:11:25 -07001987 unsigned long action, void *hcpu)
1988{
1989 switch (action) {
1990 case CPU_ONLINE:
1991 case CPU_DEAD:
Kevin Cernekee034260d2010-06-03 22:11:25 -07001992 case CPU_DOWN_FAILED:
1993 case CPU_UP_CANCELED:
Torben Hohnac751ef2011-01-25 15:07:35 -08001994 console_lock();
1995 console_unlock();
Kevin Cernekee034260d2010-06-03 22:11:25 -07001996 }
1997 return NOTIFY_OK;
1998}
1999
2000/**
Torben Hohnac751ef2011-01-25 15:07:35 -08002001 * console_lock - lock the console system for exclusive use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002003 * Acquires a lock which guarantees that the caller has
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 * exclusive access to the console system and the console_drivers list.
2005 *
2006 * Can sleep, returns nothing.
2007 */
Torben Hohnac751ef2011-01-25 15:07:35 -08002008void console_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
Daniel Vetter6b898c02012-09-17 23:03:31 +00002010 might_sleep();
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 down(&console_sem);
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01002013 if (console_suspended)
2014 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 console_locked = 1;
2016 console_may_schedule = 1;
Daniel Vetterdaee7792012-09-22 19:52:11 +02002017 mutex_acquire(&console_lock_dep_map, 0, 0, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018}
Torben Hohnac751ef2011-01-25 15:07:35 -08002019EXPORT_SYMBOL(console_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Torben Hohnac751ef2011-01-25 15:07:35 -08002021/**
2022 * console_trylock - try to lock the console system for exclusive use.
2023 *
2024 * Tried to acquire a lock which guarantees that the caller has
2025 * exclusive access to the console system and the console_drivers list.
2026 *
2027 * returns 1 on success, and 0 on failure to acquire the lock.
2028 */
2029int console_trylock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
2031 if (down_trylock(&console_sem))
Torben Hohnac751ef2011-01-25 15:07:35 -08002032 return 0;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01002033 if (console_suspended) {
2034 up(&console_sem);
Torben Hohnac751ef2011-01-25 15:07:35 -08002035 return 0;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01002036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 console_locked = 1;
2038 console_may_schedule = 0;
Daniel Vetterdaee7792012-09-22 19:52:11 +02002039 mutex_acquire(&console_lock_dep_map, 0, 1, _RET_IP_);
Torben Hohnac751ef2011-01-25 15:07:35 -08002040 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041}
Torben Hohnac751ef2011-01-25 15:07:35 -08002042EXPORT_SYMBOL(console_trylock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
2044int is_console_locked(void)
2045{
2046 return console_locked;
2047}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Kay Sieverseab07262012-07-16 18:35:30 -07002049static void console_cont_flush(char *text, size_t size)
2050{
2051 unsigned long flags;
2052 size_t len;
2053
2054 raw_spin_lock_irqsave(&logbuf_lock, flags);
2055
2056 if (!cont.len)
2057 goto out;
2058
2059 /*
2060 * We still queue earlier records, likely because the console was
2061 * busy. The earlier ones need to be printed before this one, we
2062 * did not flush any fragment so far, so just let it queue up.
2063 */
2064 if (console_seq < log_next_seq && !cont.cons)
2065 goto out;
2066
2067 len = cont_print_text(text, size);
2068 raw_spin_unlock(&logbuf_lock);
2069 stop_critical_timings();
2070 call_console_drivers(cont.level, text, len);
2071 start_critical_timings();
2072 local_irq_restore(flags);
2073 return;
2074out:
2075 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2076}
Kay Sievers7ff95542012-05-03 02:29:13 +02002077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078/**
Torben Hohnac751ef2011-01-25 15:07:35 -08002079 * console_unlock - unlock the console system
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002081 * Releases the console_lock which the caller holds on the console system
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 * and the console driver list.
2083 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002084 * While the console_lock was held, console output may have been buffered
2085 * by printk(). If this is the case, console_unlock(); emits
2086 * the output prior to releasing the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 *
Kay Sievers7f3a7812012-05-09 01:37:51 +02002088 * If there is output waiting, we wake /dev/kmsg and syslog() users.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002090 * console_unlock(); may be called from any context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 */
Torben Hohnac751ef2011-01-25 15:07:35 -08002092void console_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
Kay Sievers70498252012-07-16 18:35:29 -07002094 static char text[LOG_LINE_MAX + PREFIX_MAX];
Kay Sievers7ff95542012-05-03 02:29:13 +02002095 static u64 seen_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 unsigned long flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02002097 bool wake_klogd = false;
2098 bool retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Linus Torvalds557240b2006-06-19 18:16:01 -07002100 if (console_suspended) {
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01002101 up(&console_sem);
Linus Torvalds557240b2006-06-19 18:16:01 -07002102 return;
2103 }
Antonino A. Daplas78944e52006-08-05 12:14:16 -07002104
2105 console_may_schedule = 0;
2106
Kay Sievers084681d2012-06-28 09:38:53 +02002107 /* flush buffered message fragment immediately to console */
Kay Sieverseab07262012-07-16 18:35:30 -07002108 console_cont_flush(text, sizeof(text));
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002109again:
Kay Sievers7ff95542012-05-03 02:29:13 +02002110 for (;;) {
Joe Perches62e32ac2013-07-31 13:53:47 -07002111 struct printk_log *msg;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02002112 size_t len;
Kay Sievers7ff95542012-05-03 02:29:13 +02002113 int level;
2114
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002115 raw_spin_lock_irqsave(&logbuf_lock, flags);
Kay Sievers7ff95542012-05-03 02:29:13 +02002116 if (seen_seq != log_next_seq) {
2117 wake_klogd = true;
2118 seen_seq = log_next_seq;
2119 }
2120
2121 if (console_seq < log_first_seq) {
2122 /* messages are gone, move to first one */
2123 console_seq = log_first_seq;
2124 console_idx = log_first_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002125 console_prev = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02002126 }
Kay Sievers084681d2012-06-28 09:38:53 +02002127skip:
Kay Sievers7ff95542012-05-03 02:29:13 +02002128 if (console_seq == log_next_seq)
2129 break;
2130
2131 msg = log_from_idx(console_idx);
Kay Sievers084681d2012-06-28 09:38:53 +02002132 if (msg->flags & LOG_NOCONS) {
2133 /*
2134 * Skip record we have buffered and already printed
2135 * directly to the console when we received it.
2136 */
2137 console_idx = log_next(console_idx);
2138 console_seq++;
Kay Sievers68b65072012-07-06 09:50:09 -07002139 /*
2140 * We will get here again when we register a new
2141 * CON_PRINTBUFFER console. Clear the flag so we
2142 * will properly dump everything later.
2143 */
2144 msg->flags &= ~LOG_NOCONS;
Kay Sieverseab07262012-07-16 18:35:30 -07002145 console_prev = msg->flags;
Kay Sievers084681d2012-06-28 09:38:53 +02002146 goto skip;
2147 }
Kay Sievers649e6ee2012-05-10 04:30:45 +02002148
Kay Sievers084681d2012-06-28 09:38:53 +02002149 level = msg->level;
Kay Sievers5becfb12012-07-09 12:15:42 -07002150 len = msg_print_text(msg, console_prev, false,
2151 text, sizeof(text));
Kay Sievers7ff95542012-05-03 02:29:13 +02002152 console_idx = log_next(console_idx);
2153 console_seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07002154 console_prev = msg->flags;
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002155 raw_spin_unlock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02002156
Steven Rostedt81d68a92008-05-12 21:20:42 +02002157 stop_critical_timings(); /* don't trace print latency */
Kay Sievers7ff95542012-05-03 02:29:13 +02002158 call_console_drivers(level, text, len);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002159 start_critical_timings();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 local_irq_restore(flags);
2161 }
2162 console_locked = 0;
Daniel Vetterdaee7792012-09-22 19:52:11 +02002163 mutex_release(&console_lock_dep_map, 1, _RET_IP_);
Feng Tangfe3d8ad2011-03-22 16:34:21 -07002164
2165 /* Release the exclusive_console once it is used */
2166 if (unlikely(exclusive_console))
2167 exclusive_console = NULL;
2168
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002169 raw_spin_unlock(&logbuf_lock);
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002170
Peter Zijlstra0b5e1c52011-06-07 11:15:33 +02002171 up(&console_sem);
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002172
2173 /*
2174 * Someone could have filled up the buffer again, so re-check if there's
2175 * something to flush. In case we cannot trylock the console_sem again,
2176 * there's a new owner and the console_unlock() from them will do the
2177 * flush, no worries.
2178 */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002179 raw_spin_lock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02002180 retry = console_seq != log_next_seq;
Peter Zijlstra09dc3cf2011-12-08 14:34:13 -08002181 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2182
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002183 if (retry && console_trylock())
2184 goto again;
2185
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08002186 if (wake_klogd)
2187 wake_up_klogd();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188}
Torben Hohnac751ef2011-01-25 15:07:35 -08002189EXPORT_SYMBOL(console_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Martin Waitzddad86c2005-11-13 16:08:14 -08002191/**
2192 * console_conditional_schedule - yield the CPU if required
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 *
2194 * If the console code is currently allowed to sleep, and
2195 * if this CPU should yield the CPU to another task, do
2196 * so here.
2197 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002198 * Must be called within console_lock();.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 */
2200void __sched console_conditional_schedule(void)
2201{
2202 if (console_may_schedule)
2203 cond_resched();
2204}
2205EXPORT_SYMBOL(console_conditional_schedule);
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207void console_unblank(void)
2208{
2209 struct console *c;
2210
2211 /*
2212 * console_unblank can no longer be called in interrupt context unless
2213 * oops_in_progress is set to 1..
2214 */
2215 if (oops_in_progress) {
2216 if (down_trylock(&console_sem) != 0)
2217 return;
2218 } else
Torben Hohnac751ef2011-01-25 15:07:35 -08002219 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
2221 console_locked = 1;
2222 console_may_schedule = 0;
Robin Getz4d091612009-07-01 21:08:37 -04002223 for_each_console(c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 if ((c->flags & CON_ENABLED) && c->unblank)
2225 c->unblank();
Torben Hohnac751ef2011-01-25 15:07:35 -08002226 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229/*
2230 * Return the console tty driver structure and its associated index
2231 */
2232struct tty_driver *console_device(int *index)
2233{
2234 struct console *c;
2235 struct tty_driver *driver = NULL;
2236
Torben Hohnac751ef2011-01-25 15:07:35 -08002237 console_lock();
Robin Getz4d091612009-07-01 21:08:37 -04002238 for_each_console(c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 if (!c->device)
2240 continue;
2241 driver = c->device(c, index);
2242 if (driver)
2243 break;
2244 }
Torben Hohnac751ef2011-01-25 15:07:35 -08002245 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return driver;
2247}
2248
2249/*
2250 * Prevent further output on the passed console device so that (for example)
2251 * serial drivers can disable console output before suspending a port, and can
2252 * re-enable output afterwards.
2253 */
2254void console_stop(struct console *console)
2255{
Torben Hohnac751ef2011-01-25 15:07:35 -08002256 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 console->flags &= ~CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08002258 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259}
2260EXPORT_SYMBOL(console_stop);
2261
2262void console_start(struct console *console)
2263{
Torben Hohnac751ef2011-01-25 15:07:35 -08002264 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 console->flags |= CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08002266 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267}
2268EXPORT_SYMBOL(console_start);
2269
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07002270static int __read_mostly keep_bootcon;
2271
2272static int __init keep_bootcon_setup(char *str)
2273{
2274 keep_bootcon = 1;
Andrew Morton27083ba2013-11-12 15:08:50 -08002275 pr_info("debug: skip boot console de-registration.\n");
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07002276
2277 return 0;
2278}
2279
2280early_param("keep_bootcon", keep_bootcon_setup);
2281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282/*
2283 * The console driver calls this routine during kernel initialization
2284 * to register the console printing procedure with printk() and to
2285 * print any messages that were printed by the kernel before the
2286 * console driver was initialized.
Robin Getz4d091612009-07-01 21:08:37 -04002287 *
2288 * This can happen pretty early during the boot process (because of
2289 * early_printk) - sometimes before setup_arch() completes - be careful
2290 * of what kernel features are used - they may not be initialised yet.
2291 *
2292 * There are two types of consoles - bootconsoles (early_printk) and
2293 * "real" consoles (everything which is not a bootconsole) which are
2294 * handled differently.
2295 * - Any number of bootconsoles can be registered at any time.
2296 * - As soon as a "real" console is registered, all bootconsoles
2297 * will be unregistered automatically.
2298 * - Once a "real" console is registered, any attempt to register a
2299 * bootconsoles will be rejected
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 */
Robin Getz4d091612009-07-01 21:08:37 -04002301void register_console(struct console *newcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
Jesper Juhl40dc5652005-10-30 15:02:46 -08002303 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 unsigned long flags;
Robin Getz4d091612009-07-01 21:08:37 -04002305 struct console *bcon = NULL;
Joe Perches23475402013-07-31 13:53:46 -07002306 struct console_cmdline *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Andreas Bießmann16cf48a2013-08-02 12:23:34 +02002308 if (console_drivers)
2309 for_each_console(bcon)
2310 if (WARN(bcon == newcon,
2311 "console '%s%d' already registered\n",
2312 bcon->name, bcon->index))
2313 return;
2314
Robin Getz4d091612009-07-01 21:08:37 -04002315 /*
2316 * before we register a new CON_BOOT console, make sure we don't
2317 * already have a valid console
2318 */
2319 if (console_drivers && newcon->flags & CON_BOOT) {
2320 /* find the last or real console */
2321 for_each_console(bcon) {
2322 if (!(bcon->flags & CON_BOOT)) {
Andrew Morton27083ba2013-11-12 15:08:50 -08002323 pr_info("Too late to register bootconsole %s%d\n",
Robin Getz4d091612009-07-01 21:08:37 -04002324 newcon->name, newcon->index);
2325 return;
2326 }
2327 }
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002328 }
2329
Robin Getz4d091612009-07-01 21:08:37 -04002330 if (console_drivers && console_drivers->flags & CON_BOOT)
2331 bcon = console_drivers;
2332
2333 if (preferred_console < 0 || bcon || !console_drivers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 preferred_console = selected_console;
2335
Robin Getz4d091612009-07-01 21:08:37 -04002336 if (newcon->early_setup)
2337 newcon->early_setup();
Yinghai Lu18a8bd92007-07-15 23:37:59 -07002338
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 /*
2340 * See if we want to use this console driver. If we
2341 * didn't select a console we take the first one
2342 * that registers here.
2343 */
2344 if (preferred_console < 0) {
Robin Getz4d091612009-07-01 21:08:37 -04002345 if (newcon->index < 0)
2346 newcon->index = 0;
2347 if (newcon->setup == NULL ||
2348 newcon->setup(newcon, NULL) == 0) {
2349 newcon->flags |= CON_ENABLED;
2350 if (newcon->device) {
2351 newcon->flags |= CON_CONSDEV;
Jan Kiszkacd3a1b82008-05-12 21:21:04 +02002352 preferred_console = 0;
2353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 }
2355 }
2356
2357 /*
2358 * See if this console matches one we selected on
2359 * the command line.
2360 */
Joe Perches23475402013-07-31 13:53:46 -07002361 for (i = 0, c = console_cmdline;
2362 i < MAX_CMDLINECONSOLES && c->name[0];
2363 i++, c++) {
2364 if (strcmp(c->name, newcon->name) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 continue;
Robin Getz4d091612009-07-01 21:08:37 -04002366 if (newcon->index >= 0 &&
Joe Perches23475402013-07-31 13:53:46 -07002367 newcon->index != c->index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 continue;
Robin Getz4d091612009-07-01 21:08:37 -04002369 if (newcon->index < 0)
Joe Perches23475402013-07-31 13:53:46 -07002370 newcon->index = c->index;
Joe Perchesbbeddf52013-07-31 13:53:45 -07002371
Joe Perches23475402013-07-31 13:53:46 -07002372 if (_braille_register_console(newcon, c))
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002373 return;
Joe Perchesbbeddf52013-07-31 13:53:45 -07002374
Robin Getz4d091612009-07-01 21:08:37 -04002375 if (newcon->setup &&
2376 newcon->setup(newcon, console_cmdline[i].options) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 break;
Robin Getz4d091612009-07-01 21:08:37 -04002378 newcon->flags |= CON_ENABLED;
Joe Perches23475402013-07-31 13:53:46 -07002379 newcon->index = c->index;
Greg Edwardsab4af032005-06-23 00:09:05 -07002380 if (i == selected_console) {
Robin Getz4d091612009-07-01 21:08:37 -04002381 newcon->flags |= CON_CONSDEV;
Greg Edwardsab4af032005-06-23 00:09:05 -07002382 preferred_console = selected_console;
2383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 break;
2385 }
2386
Robin Getz4d091612009-07-01 21:08:37 -04002387 if (!(newcon->flags & CON_ENABLED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 return;
2389
Robin Getz8259cf42009-07-09 13:08:37 -04002390 /*
2391 * If we have a bootconsole, and are switching to a real console,
2392 * don't print everything out again, since when the boot console, and
2393 * the real console are the same physical device, it's annoying to
2394 * see the beginning boot messages twice
2395 */
2396 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
Robin Getz4d091612009-07-01 21:08:37 -04002397 newcon->flags &= ~CON_PRINTBUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
2399 /*
2400 * Put this console in the list - keep the
2401 * preferred driver at the head of the list.
2402 */
Torben Hohnac751ef2011-01-25 15:07:35 -08002403 console_lock();
Robin Getz4d091612009-07-01 21:08:37 -04002404 if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
2405 newcon->next = console_drivers;
2406 console_drivers = newcon;
2407 if (newcon->next)
2408 newcon->next->flags &= ~CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 } else {
Robin Getz4d091612009-07-01 21:08:37 -04002410 newcon->next = console_drivers->next;
2411 console_drivers->next = newcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 }
Robin Getz4d091612009-07-01 21:08:37 -04002413 if (newcon->flags & CON_PRINTBUFFER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 /*
Torben Hohnac751ef2011-01-25 15:07:35 -08002415 * console_unlock(); will print out the buffered messages
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 * for us.
2417 */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002418 raw_spin_lock_irqsave(&logbuf_lock, flags);
Kay Sievers7ff95542012-05-03 02:29:13 +02002419 console_seq = syslog_seq;
2420 console_idx = syslog_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002421 console_prev = syslog_prev;
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002422 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
Feng Tangfe3d8ad2011-03-22 16:34:21 -07002423 /*
2424 * We're about to replay the log buffer. Only do this to the
2425 * just-registered console to avoid excessive message spam to
2426 * the already-registered consoles.
2427 */
2428 exclusive_console = newcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 }
Torben Hohnac751ef2011-01-25 15:07:35 -08002430 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01002431 console_sysfs_notify();
Robin Getz8259cf42009-07-09 13:08:37 -04002432
2433 /*
2434 * By unregistering the bootconsoles after we enable the real console
2435 * we get the "console xxx enabled" message on all the consoles -
2436 * boot consoles, real consoles, etc - this is to ensure that end
2437 * users know there might be something in the kernel's log buffer that
2438 * went to the bootconsole (that they do not see on the real console)
2439 */
Andrew Morton27083ba2013-11-12 15:08:50 -08002440 pr_info("%sconsole [%s%d] enabled\n",
Kees Cook6b802392013-11-12 15:08:49 -08002441 (newcon->flags & CON_BOOT) ? "boot" : "" ,
2442 newcon->name, newcon->index);
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07002443 if (bcon &&
2444 ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
2445 !keep_bootcon) {
Kees Cook6b802392013-11-12 15:08:49 -08002446 /* We need to iterate through all boot consoles, to make
2447 * sure we print everything out, before we unregister them.
Robin Getz8259cf42009-07-09 13:08:37 -04002448 */
Robin Getz8259cf42009-07-09 13:08:37 -04002449 for_each_console(bcon)
2450 if (bcon->flags & CON_BOOT)
2451 unregister_console(bcon);
Robin Getz8259cf42009-07-09 13:08:37 -04002452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453}
2454EXPORT_SYMBOL(register_console);
2455
Jesper Juhl40dc5652005-10-30 15:02:46 -08002456int unregister_console(struct console *console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457{
Jesper Juhl40dc5652005-10-30 15:02:46 -08002458 struct console *a, *b;
Joe Perchesbbeddf52013-07-31 13:53:45 -07002459 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Andrew Morton27083ba2013-11-12 15:08:50 -08002461 pr_info("%sconsole [%s%d] disabled\n",
Kees Cook6b802392013-11-12 15:08:49 -08002462 (console->flags & CON_BOOT) ? "boot" : "" ,
2463 console->name, console->index);
2464
Joe Perchesbbeddf52013-07-31 13:53:45 -07002465 res = _braille_unregister_console(console);
2466 if (res)
2467 return res;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002468
Joe Perchesbbeddf52013-07-31 13:53:45 -07002469 res = 1;
Torben Hohnac751ef2011-01-25 15:07:35 -08002470 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 if (console_drivers == console) {
2472 console_drivers=console->next;
2473 res = 0;
Benjamin Herrenschmidte9b15b52005-11-23 13:37:44 -08002474 } else if (console_drivers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 for (a=console_drivers->next, b=console_drivers ;
2476 a; b=a, a=b->next) {
2477 if (a == console) {
2478 b->next = a->next;
2479 res = 0;
2480 break;
Jesper Juhl40dc5652005-10-30 15:02:46 -08002481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 }
2483 }
Jesper Juhl40dc5652005-10-30 15:02:46 -08002484
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002485 /*
Greg Edwardsab4af032005-06-23 00:09:05 -07002486 * If this isn't the last console and it has CON_CONSDEV set, we
2487 * need to set it on the next preferred console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 */
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002489 if (console_drivers != NULL && console->flags & CON_CONSDEV)
Greg Edwardsab4af032005-06-23 00:09:05 -07002490 console_drivers->flags |= CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
Stephen Chivers7fa21dd2014-05-14 08:04:39 +10002492 console->flags &= ~CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08002493 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01002494 console_sysfs_notify();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 return res;
2496}
2497EXPORT_SYMBOL(unregister_console);
Matt Mackalld59745c2005-05-01 08:59:02 -07002498
Kevin Cernekee034260d2010-06-03 22:11:25 -07002499static int __init printk_late_init(void)
Robin Getz0c5564b2007-08-20 15:22:47 -04002500{
Robin Getz4d091612009-07-01 21:08:37 -04002501 struct console *con;
2502
2503 for_each_console(con) {
Nishanth Aravamudan4c30c6f2011-08-25 15:59:11 -07002504 if (!keep_bootcon && con->flags & CON_BOOT) {
Sonic Zhang42c2c8c2009-08-06 15:58:11 -07002505 unregister_console(con);
Robin Getzcb00e992007-08-21 23:14:58 -04002506 }
Robin Getz0c5564b2007-08-20 15:22:47 -04002507 }
Kevin Cernekee034260d2010-06-03 22:11:25 -07002508 hotcpu_notifier(console_cpu_notify, 0);
Robin Getz0c5564b2007-08-20 15:22:47 -04002509 return 0;
2510}
Kevin Cernekee034260d2010-06-03 22:11:25 -07002511late_initcall(printk_late_init);
Robin Getz0c5564b2007-08-20 15:22:47 -04002512
Joe Perches7ef3d2f2008-02-08 04:21:25 -08002513#if defined CONFIG_PRINTK
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002514/*
2515 * Delayed printk version, for scheduler-internal messages:
2516 */
2517#define PRINTK_BUF_SIZE 512
2518
2519#define PRINTK_PENDING_WAKEUP 0x01
2520#define PRINTK_PENDING_SCHED 0x02
2521
2522static DEFINE_PER_CPU(int, printk_pending);
2523static DEFINE_PER_CPU(char [PRINTK_BUF_SIZE], printk_sched_buf);
2524
2525static void wake_up_klogd_work_func(struct irq_work *irq_work)
2526{
2527 int pending = __this_cpu_xchg(printk_pending, 0);
2528
2529 if (pending & PRINTK_PENDING_SCHED) {
2530 char *buf = __get_cpu_var(printk_sched_buf);
Andrew Morton27083ba2013-11-12 15:08:50 -08002531 pr_warn("[sched_delayed] %s", buf);
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002532 }
2533
2534 if (pending & PRINTK_PENDING_WAKEUP)
2535 wake_up_interruptible(&log_wait);
2536}
2537
2538static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) = {
2539 .func = wake_up_klogd_work_func,
2540 .flags = IRQ_WORK_LAZY,
2541};
2542
2543void wake_up_klogd(void)
2544{
2545 preempt_disable();
2546 if (waitqueue_active(&log_wait)) {
2547 this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP);
2548 irq_work_queue(&__get_cpu_var(wake_up_klogd_work));
2549 }
2550 preempt_enable();
2551}
Dave Young717115e2008-07-25 01:45:58 -07002552
Peter Zijlstra600e1452012-03-15 12:35:37 +01002553int printk_sched(const char *fmt, ...)
2554{
2555 unsigned long flags;
2556 va_list args;
2557 char *buf;
2558 int r;
2559
2560 local_irq_save(flags);
2561 buf = __get_cpu_var(printk_sched_buf);
2562
2563 va_start(args, fmt);
2564 r = vsnprintf(buf, PRINTK_BUF_SIZE, fmt, args);
2565 va_end(args);
2566
2567 __this_cpu_or(printk_pending, PRINTK_PENDING_SCHED);
Frederic Weisbecker74876a92012-10-12 18:00:23 +02002568 irq_work_queue(&__get_cpu_var(wake_up_klogd_work));
Peter Zijlstra600e1452012-03-15 12:35:37 +01002569 local_irq_restore(flags);
2570
2571 return r;
2572}
2573
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574/*
2575 * printk rate limiting, lifted from the networking subsystem.
2576 *
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07002577 * This enforces a rate limit: not more than 10 kernel messages
2578 * every 5s to make a denial-of-service attack impossible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 */
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07002580DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
2581
Christian Borntraeger5c828712009-10-23 14:58:11 +02002582int __printk_ratelimit(const char *func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583{
Christian Borntraeger5c828712009-10-23 14:58:11 +02002584 return ___ratelimit(&printk_ratelimit_state, func);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585}
Christian Borntraeger5c828712009-10-23 14:58:11 +02002586EXPORT_SYMBOL(__printk_ratelimit);
Andrew Mortonf46c4832006-11-02 22:07:16 -08002587
2588/**
2589 * printk_timed_ratelimit - caller-controlled printk ratelimiting
2590 * @caller_jiffies: pointer to caller's state
2591 * @interval_msecs: minimum interval between prints
2592 *
2593 * printk_timed_ratelimit() returns true if more than @interval_msecs
2594 * milliseconds have elapsed since the last time printk_timed_ratelimit()
2595 * returned true.
2596 */
2597bool printk_timed_ratelimit(unsigned long *caller_jiffies,
2598 unsigned int interval_msecs)
2599{
Guillaume Knispelf2d28a22009-03-17 16:18:42 +01002600 if (*caller_jiffies == 0
2601 || !time_in_range(jiffies, *caller_jiffies,
2602 *caller_jiffies
2603 + msecs_to_jiffies(interval_msecs))) {
2604 *caller_jiffies = jiffies;
Andrew Mortonf46c4832006-11-02 22:07:16 -08002605 return true;
2606 }
2607 return false;
2608}
2609EXPORT_SYMBOL(printk_timed_ratelimit);
Simon Kagstrom456b5652009-10-16 14:09:18 +02002610
2611static DEFINE_SPINLOCK(dump_list_lock);
2612static LIST_HEAD(dump_list);
2613
2614/**
2615 * kmsg_dump_register - register a kernel log dumper.
Randy Dunlap64855362009-12-17 15:27:27 -08002616 * @dumper: pointer to the kmsg_dumper structure
Simon Kagstrom456b5652009-10-16 14:09:18 +02002617 *
2618 * Adds a kernel log dumper to the system. The dump callback in the
2619 * structure will be called when the kernel oopses or panics and must be
2620 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
2621 */
2622int kmsg_dump_register(struct kmsg_dumper *dumper)
2623{
2624 unsigned long flags;
2625 int err = -EBUSY;
2626
2627 /* The dump callback needs to be set */
2628 if (!dumper->dump)
2629 return -EINVAL;
2630
2631 spin_lock_irqsave(&dump_list_lock, flags);
2632 /* Don't allow registering multiple times */
2633 if (!dumper->registered) {
2634 dumper->registered = 1;
Huang Yingfb842b02011-01-12 16:59:43 -08002635 list_add_tail_rcu(&dumper->list, &dump_list);
Simon Kagstrom456b5652009-10-16 14:09:18 +02002636 err = 0;
2637 }
2638 spin_unlock_irqrestore(&dump_list_lock, flags);
2639
2640 return err;
2641}
2642EXPORT_SYMBOL_GPL(kmsg_dump_register);
2643
2644/**
2645 * kmsg_dump_unregister - unregister a kmsg dumper.
Randy Dunlap64855362009-12-17 15:27:27 -08002646 * @dumper: pointer to the kmsg_dumper structure
Simon Kagstrom456b5652009-10-16 14:09:18 +02002647 *
2648 * Removes a dump device from the system. Returns zero on success and
2649 * %-EINVAL otherwise.
2650 */
2651int kmsg_dump_unregister(struct kmsg_dumper *dumper)
2652{
2653 unsigned long flags;
2654 int err = -EINVAL;
2655
2656 spin_lock_irqsave(&dump_list_lock, flags);
2657 if (dumper->registered) {
2658 dumper->registered = 0;
Huang Yingfb842b02011-01-12 16:59:43 -08002659 list_del_rcu(&dumper->list);
Simon Kagstrom456b5652009-10-16 14:09:18 +02002660 err = 0;
2661 }
2662 spin_unlock_irqrestore(&dump_list_lock, flags);
Huang Yingfb842b02011-01-12 16:59:43 -08002663 synchronize_rcu();
Simon Kagstrom456b5652009-10-16 14:09:18 +02002664
2665 return err;
2666}
2667EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
2668
Kay Sievers7ff95542012-05-03 02:29:13 +02002669static bool always_kmsg_dump;
2670module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR);
2671
Simon Kagstrom456b5652009-10-16 14:09:18 +02002672/**
2673 * kmsg_dump - dump kernel log to kernel message dumpers.
2674 * @reason: the reason (oops, panic etc) for dumping
2675 *
Kay Sieverse2ae7152012-06-15 14:07:51 +02002676 * Call each of the registered dumper's dump() callback, which can
2677 * retrieve the kmsg records with kmsg_dump_get_line() or
2678 * kmsg_dump_get_buffer().
Simon Kagstrom456b5652009-10-16 14:09:18 +02002679 */
2680void kmsg_dump(enum kmsg_dump_reason reason)
2681{
Simon Kagstrom456b5652009-10-16 14:09:18 +02002682 struct kmsg_dumper *dumper;
Simon Kagstrom456b5652009-10-16 14:09:18 +02002683 unsigned long flags;
2684
Matthew Garrettc22ab332012-03-05 14:59:10 -08002685 if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
2686 return;
2687
Huang Yingfb842b02011-01-12 16:59:43 -08002688 rcu_read_lock();
Kay Sieverse2ae7152012-06-15 14:07:51 +02002689 list_for_each_entry_rcu(dumper, &dump_list, list) {
2690 if (dumper->max_reason && reason > dumper->max_reason)
2691 continue;
2692
2693 /* initialize iterator with data about the stored records */
2694 dumper->active = true;
2695
2696 raw_spin_lock_irqsave(&logbuf_lock, flags);
2697 dumper->cur_seq = clear_seq;
2698 dumper->cur_idx = clear_idx;
2699 dumper->next_seq = log_next_seq;
2700 dumper->next_idx = log_next_idx;
2701 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2702
2703 /* invoke dumper which will iterate over records */
2704 dumper->dump(dumper, reason);
2705
2706 /* reset iterator */
2707 dumper->active = false;
2708 }
Huang Yingfb842b02011-01-12 16:59:43 -08002709 rcu_read_unlock();
Simon Kagstrom456b5652009-10-16 14:09:18 +02002710}
Kay Sieverse2ae7152012-06-15 14:07:51 +02002711
2712/**
Anton Vorontsov533827c2012-07-20 17:28:07 -07002713 * kmsg_dump_get_line_nolock - retrieve one kmsg log line (unlocked version)
2714 * @dumper: registered kmsg dumper
2715 * @syslog: include the "<4>" prefixes
2716 * @line: buffer to copy the line to
2717 * @size: maximum size of the buffer
2718 * @len: length of line placed into buffer
2719 *
2720 * Start at the beginning of the kmsg buffer, with the oldest kmsg
2721 * record, and copy one record into the provided buffer.
2722 *
2723 * Consecutive calls will return the next available record moving
2724 * towards the end of the buffer with the youngest messages.
2725 *
2726 * A return value of FALSE indicates that there are no more records to
2727 * read.
2728 *
2729 * The function is similar to kmsg_dump_get_line(), but grabs no locks.
2730 */
2731bool kmsg_dump_get_line_nolock(struct kmsg_dumper *dumper, bool syslog,
2732 char *line, size_t size, size_t *len)
2733{
Joe Perches62e32ac2013-07-31 13:53:47 -07002734 struct printk_log *msg;
Anton Vorontsov533827c2012-07-20 17:28:07 -07002735 size_t l = 0;
2736 bool ret = false;
2737
2738 if (!dumper->active)
2739 goto out;
2740
2741 if (dumper->cur_seq < log_first_seq) {
2742 /* messages are gone, move to first available one */
2743 dumper->cur_seq = log_first_seq;
2744 dumper->cur_idx = log_first_idx;
2745 }
2746
2747 /* last entry */
2748 if (dumper->cur_seq >= log_next_seq)
2749 goto out;
2750
2751 msg = log_from_idx(dumper->cur_idx);
2752 l = msg_print_text(msg, 0, syslog, line, size);
2753
2754 dumper->cur_idx = log_next(dumper->cur_idx);
2755 dumper->cur_seq++;
2756 ret = true;
2757out:
2758 if (len)
2759 *len = l;
2760 return ret;
2761}
2762
2763/**
Kay Sieverse2ae7152012-06-15 14:07:51 +02002764 * kmsg_dump_get_line - retrieve one kmsg log line
2765 * @dumper: registered kmsg dumper
2766 * @syslog: include the "<4>" prefixes
2767 * @line: buffer to copy the line to
2768 * @size: maximum size of the buffer
2769 * @len: length of line placed into buffer
2770 *
2771 * Start at the beginning of the kmsg buffer, with the oldest kmsg
2772 * record, and copy one record into the provided buffer.
2773 *
2774 * Consecutive calls will return the next available record moving
2775 * towards the end of the buffer with the youngest messages.
2776 *
2777 * A return value of FALSE indicates that there are no more records to
2778 * read.
2779 */
2780bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog,
2781 char *line, size_t size, size_t *len)
2782{
2783 unsigned long flags;
Anton Vorontsov533827c2012-07-20 17:28:07 -07002784 bool ret;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002785
2786 raw_spin_lock_irqsave(&logbuf_lock, flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07002787 ret = kmsg_dump_get_line_nolock(dumper, syslog, line, size, len);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002788 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07002789
Kay Sieverse2ae7152012-06-15 14:07:51 +02002790 return ret;
2791}
2792EXPORT_SYMBOL_GPL(kmsg_dump_get_line);
2793
2794/**
2795 * kmsg_dump_get_buffer - copy kmsg log lines
2796 * @dumper: registered kmsg dumper
2797 * @syslog: include the "<4>" prefixes
Randy Dunlap4f0f4af2012-06-30 15:37:24 -07002798 * @buf: buffer to copy the line to
Kay Sieverse2ae7152012-06-15 14:07:51 +02002799 * @size: maximum size of the buffer
2800 * @len: length of line placed into buffer
2801 *
2802 * Start at the end of the kmsg buffer and fill the provided buffer
2803 * with as many of the the *youngest* kmsg records that fit into it.
2804 * If the buffer is large enough, all available kmsg records will be
2805 * copied with a single call.
2806 *
2807 * Consecutive calls will fill the buffer with the next block of
2808 * available older records, not including the earlier retrieved ones.
2809 *
2810 * A return value of FALSE indicates that there are no more records to
2811 * read.
2812 */
2813bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
2814 char *buf, size_t size, size_t *len)
2815{
2816 unsigned long flags;
2817 u64 seq;
2818 u32 idx;
2819 u64 next_seq;
2820 u32 next_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002821 enum log_flags prev;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002822 size_t l = 0;
2823 bool ret = false;
2824
2825 if (!dumper->active)
2826 goto out;
2827
2828 raw_spin_lock_irqsave(&logbuf_lock, flags);
2829 if (dumper->cur_seq < log_first_seq) {
2830 /* messages are gone, move to first available one */
2831 dumper->cur_seq = log_first_seq;
2832 dumper->cur_idx = log_first_idx;
2833 }
2834
2835 /* last entry */
2836 if (dumper->cur_seq >= dumper->next_seq) {
2837 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2838 goto out;
2839 }
2840
2841 /* calculate length of entire buffer */
2842 seq = dumper->cur_seq;
2843 idx = dumper->cur_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002844 prev = 0;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002845 while (seq < dumper->next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07002846 struct printk_log *msg = log_from_idx(idx);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002847
Kay Sievers5becfb12012-07-09 12:15:42 -07002848 l += msg_print_text(msg, prev, true, NULL, 0);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002849 idx = log_next(idx);
2850 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07002851 prev = msg->flags;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002852 }
2853
2854 /* move first record forward until length fits into the buffer */
2855 seq = dumper->cur_seq;
2856 idx = dumper->cur_idx;
Kay Sievers5becfb12012-07-09 12:15:42 -07002857 prev = 0;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002858 while (l > size && seq < dumper->next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07002859 struct printk_log *msg = log_from_idx(idx);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002860
Kay Sievers5becfb12012-07-09 12:15:42 -07002861 l -= msg_print_text(msg, prev, true, NULL, 0);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002862 idx = log_next(idx);
2863 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07002864 prev = msg->flags;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002865 }
2866
2867 /* last message in next interation */
2868 next_seq = seq;
2869 next_idx = idx;
2870
2871 l = 0;
2872 while (seq < dumper->next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07002873 struct printk_log *msg = log_from_idx(idx);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002874
Kay Sievers5becfb12012-07-09 12:15:42 -07002875 l += msg_print_text(msg, prev, syslog, buf + l, size - l);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002876 idx = log_next(idx);
2877 seq++;
Kay Sievers5becfb12012-07-09 12:15:42 -07002878 prev = msg->flags;
Kay Sieverse2ae7152012-06-15 14:07:51 +02002879 }
2880
2881 dumper->next_seq = next_seq;
2882 dumper->next_idx = next_idx;
2883 ret = true;
2884 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2885out:
2886 if (len)
2887 *len = l;
2888 return ret;
2889}
2890EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer);
2891
2892/**
Anton Vorontsov533827c2012-07-20 17:28:07 -07002893 * kmsg_dump_rewind_nolock - reset the interator (unlocked version)
2894 * @dumper: registered kmsg dumper
2895 *
2896 * Reset the dumper's iterator so that kmsg_dump_get_line() and
2897 * kmsg_dump_get_buffer() can be called again and used multiple
2898 * times within the same dumper.dump() callback.
2899 *
2900 * The function is similar to kmsg_dump_rewind(), but grabs no locks.
2901 */
2902void kmsg_dump_rewind_nolock(struct kmsg_dumper *dumper)
2903{
2904 dumper->cur_seq = clear_seq;
2905 dumper->cur_idx = clear_idx;
2906 dumper->next_seq = log_next_seq;
2907 dumper->next_idx = log_next_idx;
2908}
2909
2910/**
Kay Sieverse2ae7152012-06-15 14:07:51 +02002911 * kmsg_dump_rewind - reset the interator
2912 * @dumper: registered kmsg dumper
2913 *
2914 * Reset the dumper's iterator so that kmsg_dump_get_line() and
2915 * kmsg_dump_get_buffer() can be called again and used multiple
2916 * times within the same dumper.dump() callback.
2917 */
2918void kmsg_dump_rewind(struct kmsg_dumper *dumper)
2919{
2920 unsigned long flags;
2921
2922 raw_spin_lock_irqsave(&logbuf_lock, flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07002923 kmsg_dump_rewind_nolock(dumper);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002924 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2925}
2926EXPORT_SYMBOL_GPL(kmsg_dump_rewind);
Tejun Heo196779b2013-04-30 15:27:12 -07002927
Tejun Heo98e5e1b2013-04-30 15:27:15 -07002928static char dump_stack_arch_desc_str[128];
2929
2930/**
2931 * dump_stack_set_arch_desc - set arch-specific str to show with task dumps
2932 * @fmt: printf-style format string
2933 * @...: arguments for the format string
2934 *
2935 * The configured string will be printed right after utsname during task
2936 * dumps. Usually used to add arch-specific system identifiers. If an
2937 * arch wants to make use of such an ID string, it should initialize this
2938 * as soon as possible during boot.
2939 */
2940void __init dump_stack_set_arch_desc(const char *fmt, ...)
2941{
2942 va_list args;
2943
2944 va_start(args, fmt);
2945 vsnprintf(dump_stack_arch_desc_str, sizeof(dump_stack_arch_desc_str),
2946 fmt, args);
2947 va_end(args);
2948}
2949
Tejun Heo196779b2013-04-30 15:27:12 -07002950/**
2951 * dump_stack_print_info - print generic debug info for dump_stack()
2952 * @log_lvl: log level
2953 *
2954 * Arch-specific dump_stack() implementations can use this function to
2955 * print out the same debug information as the generic dump_stack().
2956 */
2957void dump_stack_print_info(const char *log_lvl)
2958{
2959 printk("%sCPU: %d PID: %d Comm: %.20s %s %s %.*s\n",
2960 log_lvl, raw_smp_processor_id(), current->pid, current->comm,
2961 print_tainted(), init_utsname()->release,
2962 (int)strcspn(init_utsname()->version, " "),
2963 init_utsname()->version);
Tejun Heo98e5e1b2013-04-30 15:27:15 -07002964
2965 if (dump_stack_arch_desc_str[0] != '\0')
2966 printk("%sHardware name: %s\n",
2967 log_lvl, dump_stack_arch_desc_str);
Tejun Heo3d1cb202013-04-30 15:27:22 -07002968
2969 print_worker_info(log_lvl, current);
Tejun Heo196779b2013-04-30 15:27:12 -07002970}
2971
Tejun Heoa43cb952013-04-30 15:27:17 -07002972/**
2973 * show_regs_print_info - print generic debug info for show_regs()
2974 * @log_lvl: log level
2975 *
2976 * show_regs() implementations can use this function to print out generic
2977 * debug information.
2978 */
2979void show_regs_print_info(const char *log_lvl)
2980{
2981 dump_stack_print_info(log_lvl);
2982
2983 printk("%stask: %p ti: %p task.ti: %p\n",
2984 log_lvl, current, current_thread_info(),
2985 task_thread_info(current));
2986}
2987
Joe Perches7ef3d2f2008-02-08 04:21:25 -08002988#endif