blob: 040fb948924ede0a042d200ada2ed7646b43792c [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/delay.h>
30#include <linux/smp.h>
31#include <linux/security.h>
32#include <linux/bootmem.h>
Mike Travis162a7e72011-05-24 17:13:20 -070033#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/syscalls.h>
Hari Bathini692f66f2017-05-08 15:56:18 -070035#include <linux/crash_core.h>
Jason Wesseld37d39a2010-05-20 21:04:27 -050036#include <linux/kdb.h>
Ingo Molnar3fff4c42009-09-22 16:18:09 +020037#include <linux/ratelimit.h>
Simon Kagstrom456b5652009-10-16 14:09:18 +020038#include <linux/kmsg_dump.h>
Kees Cook00234592010-02-03 15:36:43 -080039#include <linux/syslog.h>
Kevin Cernekee034260d2010-06-03 22:11:25 -070040#include <linux/cpu.h>
41#include <linux/notifier.h>
Huang Yingfb842b002011-01-12 16:59:43 -080042#include <linux/rculist.h>
Kay Sieverse11fea92012-05-03 02:29:41 +020043#include <linux/poll.h>
Frederic Weisbecker74876a92012-10-12 18:00:23 +020044#include <linux/irq_work.h>
Tejun Heo196779b2013-04-30 15:27:12 -070045#include <linux/utsname.h>
Alex Elder249771b2014-08-06 16:09:08 -070046#include <linux/ctype.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080047#include <linux/uio.h>
Ingo Molnare6017572017-02-01 16:36:40 +010048#include <linux/sched/clock.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010049#include <linux/sched/debug.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010050#include <linux/sched/task_stack.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080052#include <linux/uaccess.h>
Christoph Hellwig40a7d9f2016-08-02 14:03:59 -070053#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Johannes Berg95100352011-11-24 20:03:08 +010055#define CREATE_TRACE_POINTS
56#include <trace/events/printk.h>
57
Joe Perchesd197c432013-07-31 13:53:44 -070058#include "console_cmdline.h"
Joe Perchesbbeddf52013-07-31 13:53:45 -070059#include "braille.h"
Petr Mladek42a0bb32016-05-20 17:00:33 -070060#include "internal.h"
Joe Perchesd197c432013-07-31 13:53:44 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062int console_printk[4] = {
Borislav Petkova8fe19e2014-06-04 16:11:46 -070063 CONSOLE_LOGLEVEL_DEFAULT, /* console_loglevel */
Alex Elder42a9dc02014-08-06 16:09:01 -070064 MESSAGE_LOGLEVEL_DEFAULT, /* default_message_loglevel */
Borislav Petkova8fe19e2014-06-04 16:11:46 -070065 CONSOLE_LOGLEVEL_MIN, /* minimum_console_loglevel */
66 CONSOLE_LOGLEVEL_DEFAULT, /* default_console_loglevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/*
Patrick Pletscher0bbfb7c2007-02-17 20:10:16 +010070 * Low level drivers may need that to know if they can schedule in
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * their unblank() callback or not. So let's export it.
72 */
73int oops_in_progress;
74EXPORT_SYMBOL(oops_in_progress);
75
76/*
77 * console_sem protects the console_drivers list, and also
78 * provides serialisation for access to the entire console
79 * driver system.
80 */
Thomas Gleixner5b8c4f22010-09-07 14:33:43 +000081static DEFINE_SEMAPHORE(console_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082struct console *console_drivers;
Ingo Molnara29d1cf2008-06-02 13:19:08 +020083EXPORT_SYMBOL_GPL(console_drivers);
84
Daniel Vetterdaee7792012-09-22 19:52:11 +020085#ifdef CONFIG_LOCKDEP
86static struct lockdep_map console_lock_dep_map = {
87 .name = "console_lock"
88};
Steven Rostedt (VMware)dbdda842018-01-10 14:24:17 +010089static struct lockdep_map console_owner_dep_map = {
90 .name = "console_owner"
91};
Daniel Vetterdaee7792012-09-22 19:52:11 +020092#endif
93
Steven Rostedt (VMware)dbdda842018-01-10 14:24:17 +010094static DEFINE_RAW_SPINLOCK(console_owner_lock);
95static struct task_struct *console_owner;
96static bool console_waiter;
97
Borislav Petkov750afe72016-08-02 14:04:07 -070098enum devkmsg_log_bits {
99 __DEVKMSG_LOG_BIT_ON = 0,
100 __DEVKMSG_LOG_BIT_OFF,
101 __DEVKMSG_LOG_BIT_LOCK,
102};
103
104enum devkmsg_log_masks {
105 DEVKMSG_LOG_MASK_ON = BIT(__DEVKMSG_LOG_BIT_ON),
106 DEVKMSG_LOG_MASK_OFF = BIT(__DEVKMSG_LOG_BIT_OFF),
107 DEVKMSG_LOG_MASK_LOCK = BIT(__DEVKMSG_LOG_BIT_LOCK),
108};
109
110/* Keep both the 'on' and 'off' bits clear, i.e. ratelimit by default: */
111#define DEVKMSG_LOG_MASK_DEFAULT 0
112
113static unsigned int __read_mostly devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
114
115static int __control_devkmsg(char *str)
116{
117 if (!str)
118 return -EINVAL;
119
120 if (!strncmp(str, "on", 2)) {
121 devkmsg_log = DEVKMSG_LOG_MASK_ON;
122 return 2;
123 } else if (!strncmp(str, "off", 3)) {
124 devkmsg_log = DEVKMSG_LOG_MASK_OFF;
125 return 3;
126 } else if (!strncmp(str, "ratelimit", 9)) {
127 devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
128 return 9;
129 }
130 return -EINVAL;
131}
132
133static int __init control_devkmsg(char *str)
134{
135 if (__control_devkmsg(str) < 0)
136 return 1;
137
138 /*
139 * Set sysctl string accordingly:
140 */
141 if (devkmsg_log == DEVKMSG_LOG_MASK_ON) {
142 memset(devkmsg_log_str, 0, DEVKMSG_STR_MAX_SIZE);
143 strncpy(devkmsg_log_str, "on", 2);
144 } else if (devkmsg_log == DEVKMSG_LOG_MASK_OFF) {
145 memset(devkmsg_log_str, 0, DEVKMSG_STR_MAX_SIZE);
146 strncpy(devkmsg_log_str, "off", 3);
147 }
148 /* else "ratelimit" which is set by default. */
149
150 /*
151 * Sysctl cannot change it anymore. The kernel command line setting of
152 * this parameter is to force the setting to be permanent throughout the
153 * runtime of the system. This is a precation measure against userspace
154 * trying to be a smarta** and attempting to change it up on us.
155 */
156 devkmsg_log |= DEVKMSG_LOG_MASK_LOCK;
157
158 return 0;
159}
160__setup("printk.devkmsg=", control_devkmsg);
161
162char devkmsg_log_str[DEVKMSG_STR_MAX_SIZE] = "ratelimit";
163
164int devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write,
165 void __user *buffer, size_t *lenp, loff_t *ppos)
166{
167 char old_str[DEVKMSG_STR_MAX_SIZE];
168 unsigned int old;
169 int err;
170
171 if (write) {
172 if (devkmsg_log & DEVKMSG_LOG_MASK_LOCK)
173 return -EINVAL;
174
175 old = devkmsg_log;
176 strncpy(old_str, devkmsg_log_str, DEVKMSG_STR_MAX_SIZE);
177 }
178
179 err = proc_dostring(table, write, buffer, lenp, ppos);
180 if (err)
181 return err;
182
183 if (write) {
184 err = __control_devkmsg(devkmsg_log_str);
185
186 /*
187 * Do not accept an unknown string OR a known string with
188 * trailing crap...
189 */
190 if (err < 0 || (err + 1 != *lenp)) {
191
192 /* ... and restore old setting. */
193 devkmsg_log = old;
194 strncpy(devkmsg_log_str, old_str, DEVKMSG_STR_MAX_SIZE);
195
196 return -EINVAL;
197 }
198 }
199
200 return 0;
201}
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/*
Tejun Heo6fe29352015-06-25 15:01:30 -0700204 * Number of registered extended console drivers.
205 *
206 * If extended consoles are present, in-kernel cont reassembly is disabled
207 * and each fragment is stored as a separate log entry with proper
208 * continuation flag so that every emitted message has full metadata. This
209 * doesn't change the result for regular consoles or /proc/kmsg. For
210 * /dev/kmsg, as long as the reader concatenates messages according to
211 * consecutive continuation flags, the end result should be the same too.
212 */
213static int nr_ext_console_drivers;
214
215/*
Jan Karabd8d7cf2014-06-04 16:11:36 -0700216 * Helper macros to handle lockdep when locking/unlocking console_sem. We use
217 * macros instead of functions so that _RET_IP_ contains useful information.
218 */
219#define down_console_sem() do { \
220 down(&console_sem);\
221 mutex_acquire(&console_lock_dep_map, 0, 0, _RET_IP_);\
222} while (0)
223
224static int __down_trylock_console_sem(unsigned long ip)
225{
Sergey Senozhatskyf9752372016-12-27 23:16:09 +0900226 int lock_failed;
227 unsigned long flags;
228
229 /*
230 * Here and in __up_console_sem() we need to be in safe mode,
231 * because spindump/WARN/etc from under console ->lock will
232 * deadlock in printk()->down_trylock_console_sem() otherwise.
233 */
234 printk_safe_enter_irqsave(flags);
235 lock_failed = down_trylock(&console_sem);
236 printk_safe_exit_irqrestore(flags);
237
238 if (lock_failed)
Jan Karabd8d7cf2014-06-04 16:11:36 -0700239 return 1;
240 mutex_acquire(&console_lock_dep_map, 0, 1, ip);
241 return 0;
242}
243#define down_trylock_console_sem() __down_trylock_console_sem(_RET_IP_)
244
Sergey Senozhatskyf9752372016-12-27 23:16:09 +0900245static void __up_console_sem(unsigned long ip)
246{
247 unsigned long flags;
248
249 mutex_release(&console_lock_dep_map, 1, ip);
250
251 printk_safe_enter_irqsave(flags);
252 up(&console_sem);
253 printk_safe_exit_irqrestore(flags);
254}
255#define up_console_sem() __up_console_sem(_RET_IP_)
Jan Karabd8d7cf2014-06-04 16:11:36 -0700256
257/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 * This is used for debugging the mess that is the VT code by
259 * keeping track if we have the console semaphore held. It's
260 * definitely not the perfect debug tool (we don't know if _WE_
Alex Elder0b90fec2014-08-06 16:09:03 -0700261 * hold it and are racing, but it helps tracking those weird code
262 * paths in the console code where we end up in places I want
263 * locked without the console sempahore held).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 */
Linus Torvalds557240b2006-06-19 18:16:01 -0700265static int console_locked, console_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267/*
Feng Tangfe3d8ad2011-03-22 16:34:21 -0700268 * If exclusive_console is non-NULL then only this console is to be printed to.
269 */
270static struct console *exclusive_console;
271
272/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * Array of consoles built from command line options (console=)
274 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276#define MAX_CMDLINECONSOLES 8
277
278static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
Joe Perchesd197c432013-07-31 13:53:44 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280static int preferred_console = -1;
Markus Armbruster9e124fe2008-05-26 23:31:07 +0100281int console_set_on_cmdline;
282EXPORT_SYMBOL(console_set_on_cmdline);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284/* Flag: console code may call schedule() */
285static int console_may_schedule;
286
Kay Sievers7ff95542012-05-03 02:29:13 +0200287/*
288 * The printk log buffer consists of a chain of concatenated variable
289 * length records. Every record starts with a record header, containing
290 * the overall length of the record.
291 *
292 * The heads to the first and last entry in the buffer, as well as the
Alex Elder0b90fec2014-08-06 16:09:03 -0700293 * sequence numbers of these entries are maintained when messages are
294 * stored.
Kay Sievers7ff95542012-05-03 02:29:13 +0200295 *
296 * If the heads indicate available messages, the length in the header
297 * tells the start next message. A length == 0 for the next message
298 * indicates a wrap-around to the beginning of the buffer.
299 *
300 * Every record carries the monotonic timestamp in microseconds, as well as
301 * the standard userspace syslog level and syslog facility. The usual
302 * kernel messages use LOG_KERN; userspace-injected messages always carry
303 * a matching syslog facility, by default LOG_USER. The origin of every
304 * message can be reliably determined that way.
305 *
306 * The human readable log message directly follows the message header. The
307 * length of the message text is stored in the header, the stored message
308 * is not terminated.
309 *
Kay Sieverse11fea92012-05-03 02:29:41 +0200310 * Optionally, a message can carry a dictionary of properties (key/value pairs),
311 * to provide userspace with a machine-readable message context.
312 *
313 * Examples for well-defined, commonly used property names are:
314 * DEVICE=b12:8 device identifier
315 * b12:8 block dev_t
316 * c127:3 char dev_t
317 * n8 netdev ifindex
318 * +sound:card0 subsystem:devname
319 * SUBSYSTEM=pci driver-core subsystem name
320 *
321 * Valid characters in property names are [a-zA-Z0-9.-_]. The plain text value
322 * follows directly after a '=' character. Every property is terminated by
323 * a '\0' character. The last property is not terminated.
324 *
325 * Example of a message structure:
326 * 0000 ff 8f 00 00 00 00 00 00 monotonic time in nsec
327 * 0008 34 00 record is 52 bytes long
328 * 000a 0b 00 text is 11 bytes long
329 * 000c 1f 00 dictionary is 23 bytes long
330 * 000e 03 00 LOG_KERN (facility) LOG_ERR (level)
331 * 0010 69 74 27 73 20 61 20 6c "it's a l"
332 * 69 6e 65 "ine"
333 * 001b 44 45 56 49 43 "DEVIC"
334 * 45 3d 62 38 3a 32 00 44 "E=b8:2\0D"
335 * 52 49 56 45 52 3d 62 75 "RIVER=bu"
336 * 67 "g"
337 * 0032 00 00 00 padding to next message header
338 *
Joe Perches62e32ac2013-07-31 13:53:47 -0700339 * The 'struct printk_log' buffer header must never be directly exported to
Kay Sieverse11fea92012-05-03 02:29:41 +0200340 * userspace, it is a kernel-private implementation detail that might
341 * need to be changed in the future, when the requirements change.
342 *
343 * /dev/kmsg exports the structured data in the following line format:
Antonio Ospiteb3896452015-06-30 14:59:03 -0700344 * "<level>,<sequnum>,<timestamp>,<contflag>[,additional_values, ... ];<message text>\n"
345 *
346 * Users of the export format should ignore possible additional values
347 * separated by ',', and find the message after the ';' character.
Kay Sieverse11fea92012-05-03 02:29:41 +0200348 *
349 * The optional key/value pairs are attached as continuation lines starting
350 * with a space character and terminated by a newline. All possible
351 * non-prinatable characters are escaped in the "\xff" notation.
Kay Sievers7ff95542012-05-03 02:29:13 +0200352 */
Matt Mackalld59745c2005-05-01 08:59:02 -0700353
Kay Sievers084681d2012-06-28 09:38:53 +0200354enum log_flags {
Kay Sievers5becfb12012-07-09 12:15:42 -0700355 LOG_NOCONS = 1, /* already flushed, do not print to console */
356 LOG_NEWLINE = 2, /* text ended with a newline */
357 LOG_PREFIX = 4, /* text started with a prefix */
358 LOG_CONT = 8, /* text is a fragment of a continuation line */
Kay Sievers084681d2012-06-28 09:38:53 +0200359};
360
Joe Perches62e32ac2013-07-31 13:53:47 -0700361struct printk_log {
Kay Sievers7ff95542012-05-03 02:29:13 +0200362 u64 ts_nsec; /* timestamp in nanoseconds */
363 u16 len; /* length of entire record */
364 u16 text_len; /* length of text buffer */
365 u16 dict_len; /* length of dictionary buffer */
Kay Sievers084681d2012-06-28 09:38:53 +0200366 u8 facility; /* syslog facility */
367 u8 flags:5; /* internal record flags */
368 u8 level:3; /* syslog level */
Andrey Ryabinin5c9cf8a2016-01-20 15:00:48 -0800369}
370#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
371__packed __aligned(4)
372#endif
373;
Kay Sievers7ff95542012-05-03 02:29:13 +0200374
375/*
Steven Rostedt458df9f2014-06-04 16:11:38 -0700376 * The logbuf_lock protects kmsg buffer, indices, counters. This can be taken
377 * within the scheduler's rq lock. It must be released before calling
378 * console_unlock() or anything else that might wake up a process.
Kay Sievers7ff95542012-05-03 02:29:13 +0200379 */
Petr Mladekcf9b1102016-05-20 17:00:42 -0700380DEFINE_RAW_SPINLOCK(logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +0200381
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +0900382/*
383 * Helper macros to lock/unlock logbuf_lock and switch between
384 * printk-safe/unsafe modes.
385 */
386#define logbuf_lock_irq() \
387 do { \
388 printk_safe_enter_irq(); \
389 raw_spin_lock(&logbuf_lock); \
390 } while (0)
391
392#define logbuf_unlock_irq() \
393 do { \
394 raw_spin_unlock(&logbuf_lock); \
395 printk_safe_exit_irq(); \
396 } while (0)
397
398#define logbuf_lock_irqsave(flags) \
399 do { \
400 printk_safe_enter_irqsave(flags); \
401 raw_spin_lock(&logbuf_lock); \
402 } while (0)
403
404#define logbuf_unlock_irqrestore(flags) \
405 do { \
406 raw_spin_unlock(&logbuf_lock); \
407 printk_safe_exit_irqrestore(flags); \
408 } while (0)
409
Kay Sievers96efedf2012-07-16 18:35:29 -0700410#ifdef CONFIG_PRINTK
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -0700411DECLARE_WAIT_QUEUE_HEAD(log_wait);
Kay Sievers7f3a7812012-05-09 01:37:51 +0200412/* the next printk record to read by syslog(READ) or /proc/kmsg */
413static u64 syslog_seq;
414static u32 syslog_idx;
Kay Sieverseb02dac2012-07-09 10:05:10 -0700415static size_t syslog_partial;
Kay Sievers7f3a7812012-05-09 01:37:51 +0200416
417/* index and sequence number of the first record stored in the buffer */
418static u64 log_first_seq;
419static u32 log_first_idx;
420
421/* index and sequence number of the next record to store in the buffer */
422static u64 log_next_seq;
Kay Sievers7f3a7812012-05-09 01:37:51 +0200423static u32 log_next_idx;
424
Kay Sieverseab07262012-07-16 18:35:30 -0700425/* the next printk record to write to the console */
426static u64 console_seq;
427static u32 console_idx;
Kay Sieverseab07262012-07-16 18:35:30 -0700428
Kay Sievers7f3a7812012-05-09 01:37:51 +0200429/* the next printk record to read after the last 'clear' command */
430static u64 clear_seq;
431static u32 clear_idx;
Kay Sievers7ff95542012-05-03 02:29:13 +0200432
Kay Sievers70498252012-07-16 18:35:29 -0700433#define PREFIX_MAX 32
Alex Elder249771b2014-08-06 16:09:08 -0700434#define LOG_LINE_MAX (1024 - PREFIX_MAX)
Kay Sievers7ff95542012-05-03 02:29:13 +0200435
Mathias Krause38246572015-11-06 16:30:38 -0800436#define LOG_LEVEL(v) ((v) & 0x07)
437#define LOG_FACILITY(v) ((v) >> 3 & 0xff)
438
Kay Sievers7ff95542012-05-03 02:29:13 +0200439/* record buffer */
Joe Perches62e32ac2013-07-31 13:53:47 -0700440#define LOG_ALIGN __alignof__(struct printk_log)
Kay Sievers7ff95542012-05-03 02:29:13 +0200441#define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
Stephen Warrenf8450fc2012-05-10 16:14:33 -0600442static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN);
Matt Mackalld59745c2005-05-01 08:59:02 -0700443static char *log_buf = __log_buf;
Kay Sievers7ff95542012-05-03 02:29:13 +0200444static u32 log_buf_len = __LOG_BUF_LEN;
445
Vasant Hegde14c40002014-08-09 11:15:30 +0530446/* Return log buffer address */
447char *log_buf_addr_get(void)
448{
449 return log_buf;
450}
451
452/* Return log buffer size */
453u32 log_buf_len_get(void)
454{
455 return log_buf_len;
456}
457
Kay Sievers7ff95542012-05-03 02:29:13 +0200458/* human readable text of the record */
Joe Perches62e32ac2013-07-31 13:53:47 -0700459static char *log_text(const struct printk_log *msg)
Kay Sievers7ff95542012-05-03 02:29:13 +0200460{
Joe Perches62e32ac2013-07-31 13:53:47 -0700461 return (char *)msg + sizeof(struct printk_log);
Kay Sievers7ff95542012-05-03 02:29:13 +0200462}
463
464/* optional key/value pair dictionary attached to the record */
Joe Perches62e32ac2013-07-31 13:53:47 -0700465static char *log_dict(const struct printk_log *msg)
Kay Sievers7ff95542012-05-03 02:29:13 +0200466{
Joe Perches62e32ac2013-07-31 13:53:47 -0700467 return (char *)msg + sizeof(struct printk_log) + msg->text_len;
Kay Sievers7ff95542012-05-03 02:29:13 +0200468}
469
470/* get record by index; idx must point to valid msg */
Joe Perches62e32ac2013-07-31 13:53:47 -0700471static struct printk_log *log_from_idx(u32 idx)
Kay Sievers7ff95542012-05-03 02:29:13 +0200472{
Joe Perches62e32ac2013-07-31 13:53:47 -0700473 struct printk_log *msg = (struct printk_log *)(log_buf + idx);
Kay Sievers7ff95542012-05-03 02:29:13 +0200474
475 /*
476 * A length == 0 record is the end of buffer marker. Wrap around and
477 * read the message at the start of the buffer.
478 */
479 if (!msg->len)
Joe Perches62e32ac2013-07-31 13:53:47 -0700480 return (struct printk_log *)log_buf;
Kay Sievers7ff95542012-05-03 02:29:13 +0200481 return msg;
482}
483
484/* get next record; idx must point to valid msg */
485static u32 log_next(u32 idx)
486{
Joe Perches62e32ac2013-07-31 13:53:47 -0700487 struct printk_log *msg = (struct printk_log *)(log_buf + idx);
Kay Sievers7ff95542012-05-03 02:29:13 +0200488
489 /* length == 0 indicates the end of the buffer; wrap */
490 /*
491 * A length == 0 record is the end of buffer marker. Wrap around and
492 * read the message at the start of the buffer as *this* one, and
493 * return the one after that.
494 */
495 if (!msg->len) {
Joe Perches62e32ac2013-07-31 13:53:47 -0700496 msg = (struct printk_log *)log_buf;
Kay Sievers7ff95542012-05-03 02:29:13 +0200497 return msg->len;
498 }
499 return idx + msg->len;
500}
501
Petr Mladekf40e4b92014-06-04 16:11:30 -0700502/*
503 * Check whether there is enough free space for the given message.
504 *
505 * The same values of first_idx and next_idx mean that the buffer
506 * is either empty or full.
507 *
508 * If the buffer is empty, we must respect the position of the indexes.
509 * They cannot be reset to the beginning of the buffer.
510 */
511static int logbuf_has_space(u32 msg_size, bool empty)
Petr Mladek0a581692014-06-04 16:11:28 -0700512{
513 u32 free;
514
Petr Mladekf40e4b92014-06-04 16:11:30 -0700515 if (log_next_idx > log_first_idx || empty)
Petr Mladek0a581692014-06-04 16:11:28 -0700516 free = max(log_buf_len - log_next_idx, log_first_idx);
517 else
518 free = log_first_idx - log_next_idx;
519
520 /*
521 * We need space also for an empty header that signalizes wrapping
522 * of the buffer.
523 */
524 return free >= msg_size + sizeof(struct printk_log);
525}
526
Petr Mladekf40e4b92014-06-04 16:11:30 -0700527static int log_make_free_space(u32 msg_size)
Petr Mladek0a581692014-06-04 16:11:28 -0700528{
Ivan Delalandef4689082016-03-17 14:21:30 -0700529 while (log_first_seq < log_next_seq &&
530 !logbuf_has_space(msg_size, false)) {
Alex Elder0b90fec2014-08-06 16:09:03 -0700531 /* drop old messages until we have enough contiguous space */
Petr Mladek0a581692014-06-04 16:11:28 -0700532 log_first_idx = log_next(log_first_idx);
533 log_first_seq++;
534 }
Petr Mladekf40e4b92014-06-04 16:11:30 -0700535
Ivan Delalandef4689082016-03-17 14:21:30 -0700536 if (clear_seq < log_first_seq) {
537 clear_seq = log_first_seq;
538 clear_idx = log_first_idx;
539 }
540
Petr Mladekf40e4b92014-06-04 16:11:30 -0700541 /* sequence numbers are equal, so the log buffer is empty */
Ivan Delalandef4689082016-03-17 14:21:30 -0700542 if (logbuf_has_space(msg_size, log_first_seq == log_next_seq))
Petr Mladekf40e4b92014-06-04 16:11:30 -0700543 return 0;
544
545 return -ENOMEM;
Petr Mladek0a581692014-06-04 16:11:28 -0700546}
547
Petr Mladek85c870432014-06-04 16:11:31 -0700548/* compute the message size including the padding bytes */
549static u32 msg_used_size(u16 text_len, u16 dict_len, u32 *pad_len)
550{
551 u32 size;
552
553 size = sizeof(struct printk_log) + text_len + dict_len;
554 *pad_len = (-size) & (LOG_ALIGN - 1);
555 size += *pad_len;
556
557 return size;
558}
559
Petr Mladek55bd53a2014-06-04 16:11:32 -0700560/*
561 * Define how much of the log buffer we could take at maximum. The value
562 * must be greater than two. Note that only half of the buffer is available
563 * when the index points to the middle.
564 */
565#define MAX_LOG_TAKE_PART 4
566static const char trunc_msg[] = "<truncated>";
567
568static u32 truncate_msg(u16 *text_len, u16 *trunc_msg_len,
569 u16 *dict_len, u32 *pad_len)
570{
571 /*
572 * The message should not take the whole buffer. Otherwise, it might
573 * get removed too soon.
574 */
575 u32 max_text_len = log_buf_len / MAX_LOG_TAKE_PART;
576 if (*text_len > max_text_len)
577 *text_len = max_text_len;
578 /* enable the warning message */
579 *trunc_msg_len = strlen(trunc_msg);
580 /* disable the "dict" completely */
581 *dict_len = 0;
582 /* compute the size again, count also the warning message */
583 return msg_used_size(*text_len + *trunc_msg_len, 0, pad_len);
584}
585
Kay Sievers7ff95542012-05-03 02:29:13 +0200586/* insert record into the buffer, discard old ones, update heads */
Petr Mladek034633c2014-06-04 16:11:33 -0700587static int log_store(int facility, int level,
588 enum log_flags flags, u64 ts_nsec,
589 const char *dict, u16 dict_len,
590 const char *text, u16 text_len)
Kay Sievers7ff95542012-05-03 02:29:13 +0200591{
Joe Perches62e32ac2013-07-31 13:53:47 -0700592 struct printk_log *msg;
Kay Sievers7ff95542012-05-03 02:29:13 +0200593 u32 size, pad_len;
Petr Mladek55bd53a2014-06-04 16:11:32 -0700594 u16 trunc_msg_len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +0200595
596 /* number of '\0' padding bytes to next message */
Petr Mladek85c870432014-06-04 16:11:31 -0700597 size = msg_used_size(text_len, dict_len, &pad_len);
Kay Sievers7ff95542012-05-03 02:29:13 +0200598
Petr Mladek55bd53a2014-06-04 16:11:32 -0700599 if (log_make_free_space(size)) {
600 /* truncate the message if it is too long for empty buffer */
601 size = truncate_msg(&text_len, &trunc_msg_len,
602 &dict_len, &pad_len);
603 /* survive when the log buffer is too small for trunc_msg */
604 if (log_make_free_space(size))
Petr Mladek034633c2014-06-04 16:11:33 -0700605 return 0;
Petr Mladek55bd53a2014-06-04 16:11:32 -0700606 }
Kay Sievers7ff95542012-05-03 02:29:13 +0200607
Petr Mladek39b25102014-04-03 14:48:42 -0700608 if (log_next_idx + size + sizeof(struct printk_log) > log_buf_len) {
Kay Sievers7ff95542012-05-03 02:29:13 +0200609 /*
610 * This message + an additional empty header does not fit
611 * at the end of the buffer. Add an empty header with len == 0
612 * to signify a wrap around.
613 */
Joe Perches62e32ac2013-07-31 13:53:47 -0700614 memset(log_buf + log_next_idx, 0, sizeof(struct printk_log));
Kay Sievers7ff95542012-05-03 02:29:13 +0200615 log_next_idx = 0;
616 }
617
618 /* fill message */
Joe Perches62e32ac2013-07-31 13:53:47 -0700619 msg = (struct printk_log *)(log_buf + log_next_idx);
Kay Sievers7ff95542012-05-03 02:29:13 +0200620 memcpy(log_text(msg), text, text_len);
621 msg->text_len = text_len;
Petr Mladek55bd53a2014-06-04 16:11:32 -0700622 if (trunc_msg_len) {
623 memcpy(log_text(msg) + text_len, trunc_msg, trunc_msg_len);
624 msg->text_len += trunc_msg_len;
625 }
Kay Sievers7ff95542012-05-03 02:29:13 +0200626 memcpy(log_dict(msg), dict, dict_len);
627 msg->dict_len = dict_len;
Kay Sievers084681d2012-06-28 09:38:53 +0200628 msg->facility = facility;
629 msg->level = level & 7;
630 msg->flags = flags & 0x1f;
631 if (ts_nsec > 0)
632 msg->ts_nsec = ts_nsec;
633 else
634 msg->ts_nsec = local_clock();
Kay Sievers7ff95542012-05-03 02:29:13 +0200635 memset(log_dict(msg) + dict_len, 0, pad_len);
Petr Mladekfce6e032014-04-03 14:48:43 -0700636 msg->len = size;
Kay Sievers7ff95542012-05-03 02:29:13 +0200637
638 /* insert message */
639 log_next_idx += msg->len;
640 log_next_seq++;
Petr Mladek034633c2014-06-04 16:11:33 -0700641
642 return msg->text_len;
Kay Sievers7ff95542012-05-03 02:29:13 +0200643}
Matt Mackalld59745c2005-05-01 08:59:02 -0700644
Alex Eldere99aa462014-08-06 16:09:05 -0700645int dmesg_restrict = IS_ENABLED(CONFIG_SECURITY_DMESG_RESTRICT);
Kees Cook637241a2013-06-12 14:04:39 -0700646
647static int syslog_action_restricted(int type)
648{
649 if (dmesg_restrict)
650 return 1;
651 /*
652 * Unless restricted, we allow "read all" and "get buffer size"
653 * for everybody.
654 */
655 return type != SYSLOG_ACTION_READ_ALL &&
656 type != SYSLOG_ACTION_SIZE_BUFFER;
657}
658
Kees Cookc71b02e2017-08-09 21:11:00 -0700659static int check_syslog_permissions(int type, int source)
Kees Cook637241a2013-06-12 14:04:39 -0700660{
661 /*
662 * If this is from /proc/kmsg and we've already opened it, then we've
663 * already done the capabilities checks at open time.
664 */
Vasily Averin3ea43312015-06-25 15:01:47 -0700665 if (source == SYSLOG_FROM_PROC && type != SYSLOG_ACTION_OPEN)
Vasily Averind194e5d2015-06-25 15:01:44 -0700666 goto ok;
Kees Cook637241a2013-06-12 14:04:39 -0700667
668 if (syslog_action_restricted(type)) {
669 if (capable(CAP_SYSLOG))
Vasily Averind194e5d2015-06-25 15:01:44 -0700670 goto ok;
Kees Cook637241a2013-06-12 14:04:39 -0700671 /*
672 * For historical reasons, accept CAP_SYS_ADMIN too, with
673 * a warning.
674 */
675 if (capable(CAP_SYS_ADMIN)) {
676 pr_warn_once("%s (%d): Attempt to access syslog with "
677 "CAP_SYS_ADMIN but no CAP_SYSLOG "
678 "(deprecated).\n",
679 current->comm, task_pid_nr(current));
Vasily Averind194e5d2015-06-25 15:01:44 -0700680 goto ok;
Kees Cook637241a2013-06-12 14:04:39 -0700681 }
682 return -EPERM;
683 }
Vasily Averind194e5d2015-06-25 15:01:44 -0700684ok:
Kees Cook637241a2013-06-12 14:04:39 -0700685 return security_syslog(type);
686}
687
Tejun Heod43ff432015-06-25 15:01:24 -0700688static void append_char(char **pp, char *e, char c)
689{
690 if (*pp < e)
691 *(*pp)++ = c;
692}
Kees Cook637241a2013-06-12 14:04:39 -0700693
Tejun Heo0a295e672015-06-25 15:01:27 -0700694static ssize_t msg_print_ext_header(char *buf, size_t size,
Linus Torvalds5aa068e2016-10-25 11:27:31 -0700695 struct printk_log *msg, u64 seq)
Tejun Heo0a295e672015-06-25 15:01:27 -0700696{
697 u64 ts_usec = msg->ts_nsec;
Tejun Heo0a295e672015-06-25 15:01:27 -0700698
699 do_div(ts_usec, 1000);
700
Tejun Heo0a295e672015-06-25 15:01:27 -0700701 return scnprintf(buf, size, "%u,%llu,%llu,%c;",
Linus Torvalds5aa068e2016-10-25 11:27:31 -0700702 (msg->facility << 3) | msg->level, seq, ts_usec,
703 msg->flags & LOG_CONT ? 'c' : '-');
Tejun Heo0a295e672015-06-25 15:01:27 -0700704}
705
706static ssize_t msg_print_ext_body(char *buf, size_t size,
707 char *dict, size_t dict_len,
708 char *text, size_t text_len)
709{
710 char *p = buf, *e = buf + size;
711 size_t i;
712
713 /* escape non-printable characters */
714 for (i = 0; i < text_len; i++) {
715 unsigned char c = text[i];
716
717 if (c < ' ' || c >= 127 || c == '\\')
718 p += scnprintf(p, e - p, "\\x%02x", c);
719 else
720 append_char(&p, e, c);
721 }
722 append_char(&p, e, '\n');
723
724 if (dict_len) {
725 bool line = true;
726
727 for (i = 0; i < dict_len; i++) {
728 unsigned char c = dict[i];
729
730 if (line) {
731 append_char(&p, e, ' ');
732 line = false;
733 }
734
735 if (c == '\0') {
736 append_char(&p, e, '\n');
737 line = true;
738 continue;
739 }
740
741 if (c < ' ' || c >= 127 || c == '\\') {
742 p += scnprintf(p, e - p, "\\x%02x", c);
743 continue;
744 }
745
746 append_char(&p, e, c);
747 }
748 append_char(&p, e, '\n');
749 }
750
751 return p - buf;
752}
753
Kay Sieverse11fea92012-05-03 02:29:41 +0200754/* /dev/kmsg - userspace message inject/listen interface */
755struct devkmsg_user {
756 u64 seq;
757 u32 idx;
Borislav Petkov750afe72016-08-02 14:04:07 -0700758 struct ratelimit_state rs;
Kay Sieverse11fea92012-05-03 02:29:41 +0200759 struct mutex lock;
Tejun Heod43ff432015-06-25 15:01:24 -0700760 char buf[CONSOLE_EXT_LOG_MAX];
Kay Sieverse11fea92012-05-03 02:29:41 +0200761};
762
Al Viro849f3122014-08-23 12:23:53 -0400763static ssize_t devkmsg_write(struct kiocb *iocb, struct iov_iter *from)
Kay Sieverse11fea92012-05-03 02:29:41 +0200764{
765 char *buf, *line;
Kay Sieverse11fea92012-05-03 02:29:41 +0200766 int level = default_message_loglevel;
767 int facility = 1; /* LOG_USER */
Borislav Petkov750afe72016-08-02 14:04:07 -0700768 struct file *file = iocb->ki_filp;
769 struct devkmsg_user *user = file->private_data;
Christoph Hellwig66ee59a2015-02-11 19:56:46 +0100770 size_t len = iov_iter_count(from);
Kay Sieverse11fea92012-05-03 02:29:41 +0200771 ssize_t ret = len;
772
Borislav Petkov750afe72016-08-02 14:04:07 -0700773 if (!user || len > LOG_LINE_MAX)
Kay Sieverse11fea92012-05-03 02:29:41 +0200774 return -EINVAL;
Borislav Petkov750afe72016-08-02 14:04:07 -0700775
776 /* Ignore when user logging is disabled. */
777 if (devkmsg_log & DEVKMSG_LOG_MASK_OFF)
778 return len;
779
780 /* Ratelimit when not explicitly enabled. */
781 if (!(devkmsg_log & DEVKMSG_LOG_MASK_ON)) {
782 if (!___ratelimit(&user->rs, current->comm))
783 return ret;
784 }
785
Kay Sieverse11fea92012-05-03 02:29:41 +0200786 buf = kmalloc(len+1, GFP_KERNEL);
787 if (buf == NULL)
788 return -ENOMEM;
789
Al Viro849f3122014-08-23 12:23:53 -0400790 buf[len] = '\0';
Al Virocbbd26b2016-11-01 22:09:04 -0400791 if (!copy_from_iter_full(buf, len, from)) {
Al Viro849f3122014-08-23 12:23:53 -0400792 kfree(buf);
793 return -EFAULT;
Kay Sieverse11fea92012-05-03 02:29:41 +0200794 }
795
796 /*
797 * Extract and skip the syslog prefix <[0-9]*>. Coming from userspace
798 * the decimal value represents 32bit, the lower 3 bit are the log
799 * level, the rest are the log facility.
800 *
801 * If no prefix or no userspace facility is specified, we
802 * enforce LOG_USER, to be able to reliably distinguish
803 * kernel-generated messages from userspace-injected ones.
804 */
805 line = buf;
806 if (line[0] == '<') {
807 char *endp = NULL;
Mathias Krause38246572015-11-06 16:30:38 -0800808 unsigned int u;
Kay Sieverse11fea92012-05-03 02:29:41 +0200809
Mathias Krause38246572015-11-06 16:30:38 -0800810 u = simple_strtoul(line + 1, &endp, 10);
Kay Sieverse11fea92012-05-03 02:29:41 +0200811 if (endp && endp[0] == '>') {
Mathias Krause38246572015-11-06 16:30:38 -0800812 level = LOG_LEVEL(u);
813 if (LOG_FACILITY(u) != 0)
814 facility = LOG_FACILITY(u);
Kay Sieverse11fea92012-05-03 02:29:41 +0200815 endp++;
816 len -= endp - line;
817 line = endp;
818 }
819 }
Kay Sieverse11fea92012-05-03 02:29:41 +0200820
821 printk_emit(facility, level, NULL, 0, "%s", line);
Kay Sieverse11fea92012-05-03 02:29:41 +0200822 kfree(buf);
823 return ret;
824}
825
826static ssize_t devkmsg_read(struct file *file, char __user *buf,
827 size_t count, loff_t *ppos)
828{
829 struct devkmsg_user *user = file->private_data;
Joe Perches62e32ac2013-07-31 13:53:47 -0700830 struct printk_log *msg;
Kay Sieverse11fea92012-05-03 02:29:41 +0200831 size_t len;
832 ssize_t ret;
833
834 if (!user)
835 return -EBADF;
836
Yuanhan Liu4a77a5a2012-06-16 21:21:51 +0800837 ret = mutex_lock_interruptible(&user->lock);
838 if (ret)
839 return ret;
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +0900840
841 logbuf_lock_irq();
Kay Sieverse11fea92012-05-03 02:29:41 +0200842 while (user->seq == log_next_seq) {
843 if (file->f_flags & O_NONBLOCK) {
844 ret = -EAGAIN;
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +0900845 logbuf_unlock_irq();
Kay Sieverse11fea92012-05-03 02:29:41 +0200846 goto out;
847 }
848
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +0900849 logbuf_unlock_irq();
Kay Sieverse11fea92012-05-03 02:29:41 +0200850 ret = wait_event_interruptible(log_wait,
851 user->seq != log_next_seq);
852 if (ret)
853 goto out;
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +0900854 logbuf_lock_irq();
Kay Sieverse11fea92012-05-03 02:29:41 +0200855 }
856
857 if (user->seq < log_first_seq) {
858 /* our last seen message is gone, return error and reset */
859 user->idx = log_first_idx;
860 user->seq = log_first_seq;
861 ret = -EPIPE;
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +0900862 logbuf_unlock_irq();
Kay Sieverse11fea92012-05-03 02:29:41 +0200863 goto out;
864 }
865
866 msg = log_from_idx(user->idx);
Tejun Heo0a295e672015-06-25 15:01:27 -0700867 len = msg_print_ext_header(user->buf, sizeof(user->buf),
Linus Torvalds5aa068e2016-10-25 11:27:31 -0700868 msg, user->seq);
Tejun Heo0a295e672015-06-25 15:01:27 -0700869 len += msg_print_ext_body(user->buf + len, sizeof(user->buf) - len,
870 log_dict(msg), msg->dict_len,
871 log_text(msg), msg->text_len);
Kay Sieversd39f3d72012-07-16 18:35:30 -0700872
Kay Sieverse11fea92012-05-03 02:29:41 +0200873 user->idx = log_next(user->idx);
874 user->seq++;
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +0900875 logbuf_unlock_irq();
Kay Sieverse11fea92012-05-03 02:29:41 +0200876
877 if (len > count) {
878 ret = -EINVAL;
879 goto out;
880 }
881
882 if (copy_to_user(buf, user->buf, len)) {
883 ret = -EFAULT;
884 goto out;
885 }
886 ret = len;
887out:
888 mutex_unlock(&user->lock);
889 return ret;
890}
891
892static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence)
893{
894 struct devkmsg_user *user = file->private_data;
895 loff_t ret = 0;
896
897 if (!user)
898 return -EBADF;
899 if (offset)
900 return -ESPIPE;
901
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +0900902 logbuf_lock_irq();
Kay Sieverse11fea92012-05-03 02:29:41 +0200903 switch (whence) {
904 case SEEK_SET:
905 /* the first record */
906 user->idx = log_first_idx;
907 user->seq = log_first_seq;
908 break;
909 case SEEK_DATA:
910 /*
911 * The first record after the last SYSLOG_ACTION_CLEAR,
912 * like issued by 'dmesg -c'. Reading /dev/kmsg itself
913 * changes no global state, and does not clear anything.
914 */
915 user->idx = clear_idx;
916 user->seq = clear_seq;
917 break;
918 case SEEK_END:
919 /* after the last record */
920 user->idx = log_next_idx;
921 user->seq = log_next_seq;
922 break;
923 default:
924 ret = -EINVAL;
925 }
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +0900926 logbuf_unlock_irq();
Kay Sieverse11fea92012-05-03 02:29:41 +0200927 return ret;
928}
929
930static unsigned int devkmsg_poll(struct file *file, poll_table *wait)
931{
932 struct devkmsg_user *user = file->private_data;
933 int ret = 0;
934
935 if (!user)
936 return POLLERR|POLLNVAL;
937
938 poll_wait(file, &log_wait, wait);
939
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +0900940 logbuf_lock_irq();
Kay Sieverse11fea92012-05-03 02:29:41 +0200941 if (user->seq < log_next_seq) {
942 /* return error when data has vanished underneath us */
943 if (user->seq < log_first_seq)
944 ret = POLLIN|POLLRDNORM|POLLERR|POLLPRI;
Nicolas Kaiser0a285312013-04-29 16:17:20 -0700945 else
946 ret = POLLIN|POLLRDNORM;
Kay Sieverse11fea92012-05-03 02:29:41 +0200947 }
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +0900948 logbuf_unlock_irq();
Kay Sieverse11fea92012-05-03 02:29:41 +0200949
950 return ret;
951}
952
953static int devkmsg_open(struct inode *inode, struct file *file)
954{
955 struct devkmsg_user *user;
956 int err;
957
Borislav Petkov750afe72016-08-02 14:04:07 -0700958 if (devkmsg_log & DEVKMSG_LOG_MASK_OFF)
959 return -EPERM;
Kay Sieverse11fea92012-05-03 02:29:41 +0200960
Borislav Petkov750afe72016-08-02 14:04:07 -0700961 /* write-only does not need any file context */
962 if ((file->f_flags & O_ACCMODE) != O_WRONLY) {
963 err = check_syslog_permissions(SYSLOG_ACTION_READ_ALL,
964 SYSLOG_FROM_READER);
965 if (err)
966 return err;
967 }
Kay Sieverse11fea92012-05-03 02:29:41 +0200968
969 user = kmalloc(sizeof(struct devkmsg_user), GFP_KERNEL);
970 if (!user)
971 return -ENOMEM;
972
Borislav Petkov750afe72016-08-02 14:04:07 -0700973 ratelimit_default_init(&user->rs);
974 ratelimit_set_flags(&user->rs, RATELIMIT_MSG_ON_RELEASE);
975
Kay Sieverse11fea92012-05-03 02:29:41 +0200976 mutex_init(&user->lock);
977
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +0900978 logbuf_lock_irq();
Kay Sieverse11fea92012-05-03 02:29:41 +0200979 user->idx = log_first_idx;
980 user->seq = log_first_seq;
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +0900981 logbuf_unlock_irq();
Kay Sieverse11fea92012-05-03 02:29:41 +0200982
983 file->private_data = user;
984 return 0;
985}
986
987static int devkmsg_release(struct inode *inode, struct file *file)
988{
989 struct devkmsg_user *user = file->private_data;
990
991 if (!user)
992 return 0;
993
Borislav Petkov750afe72016-08-02 14:04:07 -0700994 ratelimit_state_exit(&user->rs);
995
Kay Sieverse11fea92012-05-03 02:29:41 +0200996 mutex_destroy(&user->lock);
997 kfree(user);
998 return 0;
999}
1000
1001const struct file_operations kmsg_fops = {
1002 .open = devkmsg_open,
1003 .read = devkmsg_read,
Al Viro849f3122014-08-23 12:23:53 -04001004 .write_iter = devkmsg_write,
Kay Sieverse11fea92012-05-03 02:29:41 +02001005 .llseek = devkmsg_llseek,
1006 .poll = devkmsg_poll,
1007 .release = devkmsg_release,
1008};
1009
Hari Bathini692f66f2017-05-08 15:56:18 -07001010#ifdef CONFIG_CRASH_CORE
Neil Horman04d491a2009-04-02 16:58:57 -07001011/*
Dirk Gouders4c1ace62013-11-12 15:08:54 -08001012 * This appends the listed symbols to /proc/vmcore
Neil Horman04d491a2009-04-02 16:58:57 -07001013 *
Dirk Gouders4c1ace62013-11-12 15:08:54 -08001014 * /proc/vmcore is used by various utilities, like crash and makedumpfile to
Neil Horman04d491a2009-04-02 16:58:57 -07001015 * obtain access to symbols that are otherwise very difficult to locate. These
1016 * symbols are specifically used so that utilities can access and extract the
1017 * dmesg log from a vmcore file after a crash.
1018 */
Hari Bathini692f66f2017-05-08 15:56:18 -07001019void log_buf_vmcoreinfo_setup(void)
Neil Horman04d491a2009-04-02 16:58:57 -07001020{
1021 VMCOREINFO_SYMBOL(log_buf);
Neil Horman04d491a2009-04-02 16:58:57 -07001022 VMCOREINFO_SYMBOL(log_buf_len);
Kay Sievers7ff95542012-05-03 02:29:13 +02001023 VMCOREINFO_SYMBOL(log_first_idx);
Ivan Delalandef4689082016-03-17 14:21:30 -07001024 VMCOREINFO_SYMBOL(clear_idx);
Kay Sievers7ff95542012-05-03 02:29:13 +02001025 VMCOREINFO_SYMBOL(log_next_idx);
Vivek Goyal67914572012-07-18 13:18:12 -04001026 /*
Joe Perches62e32ac2013-07-31 13:53:47 -07001027 * Export struct printk_log size and field offsets. User space tools can
Vivek Goyal67914572012-07-18 13:18:12 -04001028 * parse it and detect any changes to structure down the line.
1029 */
Joe Perches62e32ac2013-07-31 13:53:47 -07001030 VMCOREINFO_STRUCT_SIZE(printk_log);
1031 VMCOREINFO_OFFSET(printk_log, ts_nsec);
1032 VMCOREINFO_OFFSET(printk_log, len);
1033 VMCOREINFO_OFFSET(printk_log, text_len);
1034 VMCOREINFO_OFFSET(printk_log, dict_len);
Neil Horman04d491a2009-04-02 16:58:57 -07001035}
1036#endif
1037
Mike Travis162a7e72011-05-24 17:13:20 -07001038/* requested log_buf_len from kernel cmdline */
1039static unsigned long __initdata new_log_buf_len;
1040
Luis R. Rodriguezc0a318a2014-08-06 16:08:52 -07001041/* we practice scaling the ring buffer by powers of 2 */
1042static void __init log_buf_len_update(unsigned size)
1043{
1044 if (size)
1045 size = roundup_pow_of_two(size);
1046 if (size > log_buf_len)
1047 new_log_buf_len = size;
1048}
1049
Mike Travis162a7e72011-05-24 17:13:20 -07001050/* save requested log_buf_len since it's too early to process it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051static int __init log_buf_len_setup(char *str)
1052{
Denys Vlasenkoeed4a2a2008-02-06 01:37:02 -08001053 unsigned size = memparse(str, &str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Luis R. Rodriguezc0a318a2014-08-06 16:08:52 -07001055 log_buf_len_update(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Mike Travis162a7e72011-05-24 17:13:20 -07001057 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
Mike Travis162a7e72011-05-24 17:13:20 -07001059early_param("log_buf_len", log_buf_len_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Geert Uytterhoeven2240a312014-10-13 15:51:11 -07001061#ifdef CONFIG_SMP
1062#define __LOG_CPU_MAX_BUF_LEN (1 << CONFIG_LOG_CPU_MAX_BUF_SHIFT)
1063
Luis R. Rodriguez23b28992014-08-06 16:08:56 -07001064static void __init log_buf_add_cpu(void)
1065{
1066 unsigned int cpu_extra;
1067
1068 /*
1069 * archs should set up cpu_possible_bits properly with
1070 * set_cpu_possible() after setup_arch() but just in
1071 * case lets ensure this is valid.
1072 */
1073 if (num_possible_cpus() == 1)
1074 return;
1075
1076 cpu_extra = (num_possible_cpus() - 1) * __LOG_CPU_MAX_BUF_LEN;
1077
1078 /* by default this will only continue through for large > 64 CPUs */
1079 if (cpu_extra <= __LOG_BUF_LEN / 2)
1080 return;
1081
1082 pr_info("log_buf_len individual max cpu contribution: %d bytes\n",
1083 __LOG_CPU_MAX_BUF_LEN);
1084 pr_info("log_buf_len total cpu_extra contributions: %d bytes\n",
1085 cpu_extra);
1086 pr_info("log_buf_len min size: %d bytes\n", __LOG_BUF_LEN);
1087
1088 log_buf_len_update(cpu_extra + __LOG_BUF_LEN);
1089}
Geert Uytterhoeven2240a312014-10-13 15:51:11 -07001090#else /* !CONFIG_SMP */
1091static inline void log_buf_add_cpu(void) {}
1092#endif /* CONFIG_SMP */
Luis R. Rodriguez23b28992014-08-06 16:08:56 -07001093
Mike Travis162a7e72011-05-24 17:13:20 -07001094void __init setup_log_buf(int early)
1095{
1096 unsigned long flags;
Mike Travis162a7e72011-05-24 17:13:20 -07001097 char *new_log_buf;
1098 int free;
1099
Luis R. Rodriguez23b28992014-08-06 16:08:56 -07001100 if (log_buf != __log_buf)
1101 return;
1102
1103 if (!early && !new_log_buf_len)
1104 log_buf_add_cpu();
1105
Mike Travis162a7e72011-05-24 17:13:20 -07001106 if (!new_log_buf_len)
1107 return;
1108
1109 if (early) {
Santosh Shilimkar9da791d2014-01-21 15:50:23 -08001110 new_log_buf =
Luis R. Rodriguez70300172014-08-06 16:08:49 -07001111 memblock_virt_alloc(new_log_buf_len, LOG_ALIGN);
Mike Travis162a7e72011-05-24 17:13:20 -07001112 } else {
Luis R. Rodriguez70300172014-08-06 16:08:49 -07001113 new_log_buf = memblock_virt_alloc_nopanic(new_log_buf_len,
1114 LOG_ALIGN);
Mike Travis162a7e72011-05-24 17:13:20 -07001115 }
1116
1117 if (unlikely(!new_log_buf)) {
1118 pr_err("log_buf_len: %ld bytes not available\n",
1119 new_log_buf_len);
1120 return;
1121 }
1122
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09001123 logbuf_lock_irqsave(flags);
Mike Travis162a7e72011-05-24 17:13:20 -07001124 log_buf_len = new_log_buf_len;
1125 log_buf = new_log_buf;
1126 new_log_buf_len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001127 free = __LOG_BUF_LEN - log_next_idx;
1128 memcpy(log_buf, __log_buf, __LOG_BUF_LEN);
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09001129 logbuf_unlock_irqrestore(flags);
Mike Travis162a7e72011-05-24 17:13:20 -07001130
Luis R. Rodriguezf5405172014-08-06 16:08:54 -07001131 pr_info("log_buf_len: %d bytes\n", log_buf_len);
Mike Travis162a7e72011-05-24 17:13:20 -07001132 pr_info("early log buf free: %d(%d%%)\n",
1133 free, (free * 100) / __LOG_BUF_LEN);
1134}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Andrew Cooks2fa72c82012-12-17 15:59:56 -08001136static bool __read_mostly ignore_loglevel;
1137
1138static int __init ignore_loglevel_setup(char *str)
1139{
Neil Zhangd25d9fe2014-08-06 16:09:12 -07001140 ignore_loglevel = true;
Andrew Morton27083ba2013-11-12 15:08:50 -08001141 pr_info("debug: ignoring loglevel setting.\n");
Andrew Cooks2fa72c82012-12-17 15:59:56 -08001142
1143 return 0;
1144}
1145
1146early_param("ignore_loglevel", ignore_loglevel_setup);
1147module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR);
Joe Perches205bd3d2015-02-12 15:01:34 -08001148MODULE_PARM_DESC(ignore_loglevel,
1149 "ignore loglevel setting (prints all kernel messages to the console)");
Andrew Cooks2fa72c82012-12-17 15:59:56 -08001150
Sergey Senozhatskycf775442016-08-02 14:03:56 -07001151static bool suppress_message_printing(int level)
1152{
1153 return (level >= console_loglevel && !ignore_loglevel);
1154}
1155
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001156#ifdef CONFIG_BOOT_PRINTK_DELAY
1157
Namhyung Kim674dff62010-10-26 14:22:48 -07001158static int boot_delay; /* msecs delay after each printk during bootup */
Dave Young3a3b6ed2009-09-22 16:43:31 -07001159static unsigned long long loops_per_msec; /* based on boot_delay */
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001160
1161static int __init boot_delay_setup(char *str)
1162{
1163 unsigned long lpj;
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001164
1165 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
1166 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
1167
1168 get_option(&str, &boot_delay);
1169 if (boot_delay > 10 * 1000)
1170 boot_delay = 0;
1171
Dave Young3a3b6ed2009-09-22 16:43:31 -07001172 pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
1173 "HZ: %d, loops_per_msec: %llu\n",
1174 boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
Dave Young29e9d222013-11-12 15:08:53 -08001175 return 0;
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001176}
Dave Young29e9d222013-11-12 15:08:53 -08001177early_param("boot_delay", boot_delay_setup);
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001178
Andrew Cooks2fa72c82012-12-17 15:59:56 -08001179static void boot_delay_msec(int level)
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001180{
1181 unsigned long long k;
1182 unsigned long timeout;
1183
Thomas Gleixnerff48cd22017-05-16 20:42:45 +02001184 if ((boot_delay == 0 || system_state >= SYSTEM_RUNNING)
Sergey Senozhatskycf775442016-08-02 14:03:56 -07001185 || suppress_message_printing(level)) {
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001186 return;
Andrew Cooks2fa72c82012-12-17 15:59:56 -08001187 }
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001188
Dave Young3a3b6ed2009-09-22 16:43:31 -07001189 k = (unsigned long long)loops_per_msec * boot_delay;
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001190
1191 timeout = jiffies + msecs_to_jiffies(boot_delay);
1192 while (k) {
1193 k--;
1194 cpu_relax();
1195 /*
1196 * use (volatile) jiffies to prevent
1197 * compiler reduction; loop termination via jiffies
1198 * is secondary and may or may not happen.
1199 */
1200 if (time_after(jiffies, timeout))
1201 break;
1202 touch_nmi_watchdog();
1203 }
1204}
1205#else
Andrew Cooks2fa72c82012-12-17 15:59:56 -08001206static inline void boot_delay_msec(int level)
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001207{
1208}
1209#endif
1210
Alex Eldere99aa462014-08-06 16:09:05 -07001211static bool printk_time = IS_ENABLED(CONFIG_PRINTK_TIME);
Kay Sievers7ff95542012-05-03 02:29:13 +02001212module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
1213
Kay Sievers084681d2012-06-28 09:38:53 +02001214static size_t print_time(u64 ts, char *buf)
1215{
1216 unsigned long rem_nsec;
1217
1218 if (!printk_time)
1219 return 0;
1220
Kay Sievers084681d2012-06-28 09:38:53 +02001221 rem_nsec = do_div(ts, 1000000000);
Roland Dreier35dac272013-01-04 15:35:50 -08001222
1223 if (!buf)
1224 return snprintf(NULL, 0, "[%5lu.000000] ", (unsigned long)ts);
1225
Kay Sievers084681d2012-06-28 09:38:53 +02001226 return sprintf(buf, "[%5lu.%06lu] ",
1227 (unsigned long)ts, rem_nsec / 1000);
1228}
1229
Joe Perches62e32ac2013-07-31 13:53:47 -07001230static size_t print_prefix(const struct printk_log *msg, bool syslog, char *buf)
Kay Sievers649e6ee2012-05-10 04:30:45 +02001231{
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001232 size_t len = 0;
Kay Sievers43a73a52012-07-06 09:50:09 -07001233 unsigned int prefix = (msg->facility << 3) | msg->level;
Kay Sievers649e6ee2012-05-10 04:30:45 +02001234
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001235 if (syslog) {
1236 if (buf) {
Kay Sievers43a73a52012-07-06 09:50:09 -07001237 len += sprintf(buf, "<%u>", prefix);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001238 } else {
1239 len += 3;
Kay Sievers43a73a52012-07-06 09:50:09 -07001240 if (prefix > 999)
1241 len += 3;
1242 else if (prefix > 99)
1243 len += 2;
1244 else if (prefix > 9)
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001245 len++;
1246 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001247 }
1248
Kay Sievers084681d2012-06-28 09:38:53 +02001249 len += print_time(msg->ts_nsec, buf ? buf + len : NULL);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001250 return len;
1251}
1252
Linus Torvalds5aa068e2016-10-25 11:27:31 -07001253static size_t msg_print_text(const struct printk_log *msg, bool syslog, char *buf, size_t size)
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001254{
1255 const char *text = log_text(msg);
1256 size_t text_size = msg->text_len;
1257 size_t len = 0;
1258
1259 do {
1260 const char *next = memchr(text, '\n', text_size);
1261 size_t text_len;
1262
1263 if (next) {
1264 text_len = next - text;
1265 next++;
1266 text_size -= next - text;
1267 } else {
1268 text_len = text_size;
1269 }
1270
1271 if (buf) {
1272 if (print_prefix(msg, syslog, NULL) +
Kay Sievers70498252012-07-16 18:35:29 -07001273 text_len + 1 >= size - len)
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001274 break;
1275
Linus Torvalds5aa068e2016-10-25 11:27:31 -07001276 len += print_prefix(msg, syslog, buf + len);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001277 memcpy(buf + len, text, text_len);
1278 len += text_len;
Linus Torvalds5aa068e2016-10-25 11:27:31 -07001279 buf[len++] = '\n';
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001280 } else {
1281 /* SYSLOG_ACTION_* buffer size only calculation */
Linus Torvalds5aa068e2016-10-25 11:27:31 -07001282 len += print_prefix(msg, syslog, NULL);
Kay Sievers5becfb12012-07-09 12:15:42 -07001283 len += text_len;
Linus Torvalds5aa068e2016-10-25 11:27:31 -07001284 len++;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001285 }
1286
1287 text = next;
1288 } while (text);
1289
Kay Sievers7ff95542012-05-03 02:29:13 +02001290 return len;
1291}
1292
1293static int syslog_print(char __user *buf, int size)
1294{
1295 char *text;
Joe Perches62e32ac2013-07-31 13:53:47 -07001296 struct printk_log *msg;
Jan Beulich116e90b2012-06-22 16:36:09 +01001297 int len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001298
Kay Sievers70498252012-07-16 18:35:29 -07001299 text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
Kay Sievers7ff95542012-05-03 02:29:13 +02001300 if (!text)
1301 return -ENOMEM;
1302
Jan Beulich116e90b2012-06-22 16:36:09 +01001303 while (size > 0) {
1304 size_t n;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001305 size_t skip;
Kay Sievers7ff95542012-05-03 02:29:13 +02001306
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09001307 logbuf_lock_irq();
Jan Beulich116e90b2012-06-22 16:36:09 +01001308 if (syslog_seq < log_first_seq) {
1309 /* messages are gone, move to first one */
1310 syslog_seq = log_first_seq;
1311 syslog_idx = log_first_idx;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001312 syslog_partial = 0;
Jan Beulich116e90b2012-06-22 16:36:09 +01001313 }
1314 if (syslog_seq == log_next_seq) {
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09001315 logbuf_unlock_irq();
Jan Beulich116e90b2012-06-22 16:36:09 +01001316 break;
1317 }
Kay Sieverseb02dac2012-07-09 10:05:10 -07001318
1319 skip = syslog_partial;
Jan Beulich116e90b2012-06-22 16:36:09 +01001320 msg = log_from_idx(syslog_idx);
Linus Torvalds5aa068e2016-10-25 11:27:31 -07001321 n = msg_print_text(msg, true, text, LOG_LINE_MAX + PREFIX_MAX);
Kay Sieverseb02dac2012-07-09 10:05:10 -07001322 if (n - syslog_partial <= size) {
1323 /* message fits into buffer, move forward */
Jan Beulich116e90b2012-06-22 16:36:09 +01001324 syslog_idx = log_next(syslog_idx);
1325 syslog_seq++;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001326 n -= syslog_partial;
1327 syslog_partial = 0;
1328 } else if (!len){
1329 /* partial read(), remember position */
1330 n = size;
1331 syslog_partial += n;
Jan Beulich116e90b2012-06-22 16:36:09 +01001332 } else
1333 n = 0;
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09001334 logbuf_unlock_irq();
Jan Beulich116e90b2012-06-22 16:36:09 +01001335
1336 if (!n)
1337 break;
1338
Kay Sieverseb02dac2012-07-09 10:05:10 -07001339 if (copy_to_user(buf, text + skip, n)) {
Jan Beulich116e90b2012-06-22 16:36:09 +01001340 if (!len)
1341 len = -EFAULT;
1342 break;
1343 }
Kay Sieverseb02dac2012-07-09 10:05:10 -07001344
1345 len += n;
1346 size -= n;
1347 buf += n;
Jan Beulich116e90b2012-06-22 16:36:09 +01001348 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001349
1350 kfree(text);
1351 return len;
1352}
1353
1354static int syslog_print_all(char __user *buf, int size, bool clear)
1355{
1356 char *text;
1357 int len = 0;
1358
Kay Sievers70498252012-07-16 18:35:29 -07001359 text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
Kay Sievers7ff95542012-05-03 02:29:13 +02001360 if (!text)
1361 return -ENOMEM;
1362
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09001363 logbuf_lock_irq();
Kay Sievers7ff95542012-05-03 02:29:13 +02001364 if (buf) {
1365 u64 next_seq;
1366 u64 seq;
1367 u32 idx;
1368
Kay Sievers7ff95542012-05-03 02:29:13 +02001369 /*
1370 * Find first record that fits, including all following records,
1371 * into the user-provided buffer for this dump.
Kay Sieverse2ae7152012-06-15 14:07:51 +02001372 */
Kay Sievers7ff95542012-05-03 02:29:13 +02001373 seq = clear_seq;
1374 idx = clear_idx;
1375 while (seq < log_next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07001376 struct printk_log *msg = log_from_idx(idx);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001377
Linus Torvalds5aa068e2016-10-25 11:27:31 -07001378 len += msg_print_text(msg, true, NULL, 0);
Kay Sievers7ff95542012-05-03 02:29:13 +02001379 idx = log_next(idx);
1380 seq++;
1381 }
Kay Sieverse2ae7152012-06-15 14:07:51 +02001382
1383 /* move first record forward until length fits into the buffer */
Kay Sievers7ff95542012-05-03 02:29:13 +02001384 seq = clear_seq;
1385 idx = clear_idx;
1386 while (len > size && seq < log_next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07001387 struct printk_log *msg = log_from_idx(idx);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001388
Linus Torvalds5aa068e2016-10-25 11:27:31 -07001389 len -= msg_print_text(msg, true, NULL, 0);
Kay Sievers7ff95542012-05-03 02:29:13 +02001390 idx = log_next(idx);
1391 seq++;
1392 }
1393
Kay Sieverse2ae7152012-06-15 14:07:51 +02001394 /* last message fitting into this dump */
Kay Sievers7ff95542012-05-03 02:29:13 +02001395 next_seq = log_next_seq;
1396
1397 len = 0;
1398 while (len >= 0 && seq < next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07001399 struct printk_log *msg = log_from_idx(idx);
Kay Sievers7ff95542012-05-03 02:29:13 +02001400 int textlen;
1401
Linus Torvalds5aa068e2016-10-25 11:27:31 -07001402 textlen = msg_print_text(msg, true, text,
Kay Sievers70498252012-07-16 18:35:29 -07001403 LOG_LINE_MAX + PREFIX_MAX);
Kay Sievers7ff95542012-05-03 02:29:13 +02001404 if (textlen < 0) {
1405 len = textlen;
1406 break;
1407 }
1408 idx = log_next(idx);
1409 seq++;
1410
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09001411 logbuf_unlock_irq();
Kay Sievers7ff95542012-05-03 02:29:13 +02001412 if (copy_to_user(buf + len, text, textlen))
1413 len = -EFAULT;
1414 else
1415 len += textlen;
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09001416 logbuf_lock_irq();
Kay Sievers7ff95542012-05-03 02:29:13 +02001417
1418 if (seq < log_first_seq) {
1419 /* messages are gone, move to next one */
1420 seq = log_first_seq;
1421 idx = log_first_idx;
1422 }
1423 }
1424 }
1425
1426 if (clear) {
1427 clear_seq = log_next_seq;
1428 clear_idx = log_next_idx;
1429 }
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09001430 logbuf_unlock_irq();
Kay Sievers7ff95542012-05-03 02:29:13 +02001431
1432 kfree(text);
1433 return len;
1434}
1435
Vasily Averin3ea43312015-06-25 15:01:47 -07001436int do_syslog(int type, char __user *buf, int len, int source)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Kay Sievers7ff95542012-05-03 02:29:13 +02001438 bool clear = false;
Joe Perchesa39d4a82014-12-10 15:50:15 -08001439 static int saved_console_loglevel = LOGLEVEL_DEFAULT;
Linus Torvaldsee24aeb2011-02-10 17:53:55 -08001440 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Vasily Averin3ea43312015-06-25 15:01:47 -07001442 error = check_syslog_permissions(type, source);
Linus Torvaldsee24aeb2011-02-10 17:53:55 -08001443 if (error)
Nikitas Angelinas077a1cc2017-07-29 20:36:36 -07001444 return error;
Eric Paris12b30522010-11-15 18:36:29 -05001445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08001447 case SYSLOG_ACTION_CLOSE: /* Close log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001449 case SYSLOG_ACTION_OPEN: /* Open log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001451 case SYSLOG_ACTION_READ: /* Read from log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (!buf || len < 0)
Nikitas Angelinas077a1cc2017-07-29 20:36:36 -07001453 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (!len)
Nikitas Angelinas077a1cc2017-07-29 20:36:36 -07001455 return 0;
1456 if (!access_ok(VERIFY_WRITE, buf, len))
1457 return -EFAULT;
Yuanhan Liu4a77a5a2012-06-16 21:21:51 +08001458 error = wait_event_interruptible(log_wait,
1459 syslog_seq != log_next_seq);
Kay Sieverscb424ff2012-07-06 09:50:09 -07001460 if (error)
Nikitas Angelinas077a1cc2017-07-29 20:36:36 -07001461 return error;
Kay Sievers7ff95542012-05-03 02:29:13 +02001462 error = syslog_print(buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001464 /* Read/clear last kernel messages */
1465 case SYSLOG_ACTION_READ_CLEAR:
Kay Sievers7ff95542012-05-03 02:29:13 +02001466 clear = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 /* FALL THRU */
Kees Cookd78ca3c2010-02-03 15:37:13 -08001468 /* Read last kernel messages */
1469 case SYSLOG_ACTION_READ_ALL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if (!buf || len < 0)
Nikitas Angelinas077a1cc2017-07-29 20:36:36 -07001471 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (!len)
Nikitas Angelinas077a1cc2017-07-29 20:36:36 -07001473 return 0;
1474 if (!access_ok(VERIFY_WRITE, buf, len))
1475 return -EFAULT;
Kay Sievers7ff95542012-05-03 02:29:13 +02001476 error = syslog_print_all(buf, len, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001478 /* Clear ring buffer */
1479 case SYSLOG_ACTION_CLEAR:
Kay Sievers7ff95542012-05-03 02:29:13 +02001480 syslog_print_all(NULL, 0, true);
Alan Stern4661e352012-06-22 17:12:19 -04001481 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001482 /* Disable logging to console */
1483 case SYSLOG_ACTION_CONSOLE_OFF:
Joe Perchesa39d4a82014-12-10 15:50:15 -08001484 if (saved_console_loglevel == LOGLEVEL_DEFAULT)
Frans Pop1aaad492009-07-06 13:31:48 +02001485 saved_console_loglevel = console_loglevel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 console_loglevel = minimum_console_loglevel;
1487 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001488 /* Enable logging to console */
1489 case SYSLOG_ACTION_CONSOLE_ON:
Joe Perchesa39d4a82014-12-10 15:50:15 -08001490 if (saved_console_loglevel != LOGLEVEL_DEFAULT) {
Frans Pop1aaad492009-07-06 13:31:48 +02001491 console_loglevel = saved_console_loglevel;
Joe Perchesa39d4a82014-12-10 15:50:15 -08001492 saved_console_loglevel = LOGLEVEL_DEFAULT;
Frans Pop1aaad492009-07-06 13:31:48 +02001493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001495 /* Set level of messages printed to console */
1496 case SYSLOG_ACTION_CONSOLE_LEVEL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (len < 1 || len > 8)
Nikitas Angelinas077a1cc2017-07-29 20:36:36 -07001498 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (len < minimum_console_loglevel)
1500 len = minimum_console_loglevel;
1501 console_loglevel = len;
Frans Pop1aaad492009-07-06 13:31:48 +02001502 /* Implicitly re-enable logging to console */
Joe Perchesa39d4a82014-12-10 15:50:15 -08001503 saved_console_loglevel = LOGLEVEL_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001505 /* Number of chars in the log buffer */
1506 case SYSLOG_ACTION_SIZE_UNREAD:
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09001507 logbuf_lock_irq();
Kay Sievers7ff95542012-05-03 02:29:13 +02001508 if (syslog_seq < log_first_seq) {
1509 /* messages are gone, move to first one */
1510 syslog_seq = log_first_seq;
1511 syslog_idx = log_first_idx;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001512 syslog_partial = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001513 }
Vasily Averin3ea43312015-06-25 15:01:47 -07001514 if (source == SYSLOG_FROM_PROC) {
Kay Sievers7ff95542012-05-03 02:29:13 +02001515 /*
1516 * Short-cut for poll(/"proc/kmsg") which simply checks
1517 * for pending data, not the size; return the count of
1518 * records, not the length.
1519 */
Alex Eldere97e1262014-08-06 16:08:59 -07001520 error = log_next_seq - syslog_seq;
Kay Sievers7ff95542012-05-03 02:29:13 +02001521 } else {
Kay Sievers5becfb12012-07-09 12:15:42 -07001522 u64 seq = syslog_seq;
1523 u32 idx = syslog_idx;
Kay Sievers7ff95542012-05-03 02:29:13 +02001524
Kay Sievers7ff95542012-05-03 02:29:13 +02001525 while (seq < log_next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07001526 struct printk_log *msg = log_from_idx(idx);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001527
Linus Torvalds5aa068e2016-10-25 11:27:31 -07001528 error += msg_print_text(msg, true, NULL, 0);
Kay Sievers7ff95542012-05-03 02:29:13 +02001529 idx = log_next(idx);
1530 seq++;
1531 }
Kay Sieverseb02dac2012-07-09 10:05:10 -07001532 error -= syslog_partial;
Kay Sievers7ff95542012-05-03 02:29:13 +02001533 }
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09001534 logbuf_unlock_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001536 /* Size of the log buffer */
1537 case SYSLOG_ACTION_SIZE_BUFFER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 error = log_buf_len;
1539 break;
1540 default:
1541 error = -EINVAL;
1542 break;
1543 }
Nikitas Angelinas077a1cc2017-07-29 20:36:36 -07001544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 return error;
1546}
1547
Heiko Carstens1e7bfb22009-01-14 14:14:29 +01001548SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
Kees Cook637241a2013-06-12 14:04:39 -07001550 return do_syslog(type, buf, len, SYSLOG_FROM_READER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 * Call the console drivers, asking them to write out
1555 * log_buf[start] to log_buf[end - 1].
Torben Hohnac751ef2011-01-25 15:07:35 -08001556 * The console_lock must be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 */
Sergey Senozhatskyd9c23522016-12-24 23:09:01 +09001558static void call_console_drivers(const char *ext_text, size_t ext_len,
Tejun Heo6fe29352015-06-25 15:01:30 -07001559 const char *text, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
Kay Sievers7ff95542012-05-03 02:29:13 +02001561 struct console *con;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Sergey Senozhatskyfc98c3c2017-02-18 03:42:54 -08001563 trace_console_rcuidle(text, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Kay Sievers7ff95542012-05-03 02:29:13 +02001565 if (!console_drivers)
1566 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Kay Sievers7ff95542012-05-03 02:29:13 +02001568 for_each_console(con) {
1569 if (exclusive_console && con != exclusive_console)
1570 continue;
1571 if (!(con->flags & CON_ENABLED))
1572 continue;
1573 if (!con->write)
1574 continue;
1575 if (!cpu_online(smp_processor_id()) &&
1576 !(con->flags & CON_ANYTIME))
1577 continue;
Tejun Heo6fe29352015-06-25 15:01:30 -07001578 if (con->flags & CON_EXTENDED)
1579 con->write(con, ext_text, ext_len);
1580 else
1581 con->write(con, text, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
1584
Dave Youngaf913222009-09-22 16:43:33 -07001585int printk_delay_msec __read_mostly;
1586
1587static inline void printk_delay(void)
1588{
1589 if (unlikely(printk_delay_msec)) {
1590 int m = printk_delay_msec;
1591
1592 while (m--) {
1593 mdelay(1);
1594 touch_nmi_watchdog();
1595 }
1596 }
1597}
1598
Kay Sievers084681d2012-06-28 09:38:53 +02001599/*
1600 * Continuation lines are buffered, and not committed to the record buffer
1601 * until the line is complete, or a race forces it. The line fragments
1602 * though, are printed immediately to the consoles to ensure everything has
1603 * reached the console in case of a kernel crash.
1604 */
1605static struct cont {
1606 char buf[LOG_LINE_MAX];
1607 size_t len; /* length == 0 means unused buffer */
Kay Sievers084681d2012-06-28 09:38:53 +02001608 struct task_struct *owner; /* task of first print*/
1609 u64 ts_nsec; /* time of first print */
1610 u8 level; /* log level of first message */
Alex Elder0b90fec2014-08-06 16:09:03 -07001611 u8 facility; /* log facility of first message */
Kay Sieverseab07262012-07-16 18:35:30 -07001612 enum log_flags flags; /* prefix, newline flags */
Kay Sievers084681d2012-06-28 09:38:53 +02001613} cont;
1614
Linus Torvalds5e467652016-10-09 11:53:00 -07001615static void cont_flush(void)
Kay Sievers084681d2012-06-28 09:38:53 +02001616{
Kay Sievers084681d2012-06-28 09:38:53 +02001617 if (cont.len == 0)
1618 return;
Linus Torvalds5c2992e2016-10-25 11:27:31 -07001619
1620 log_store(cont.facility, cont.level, cont.flags, cont.ts_nsec,
1621 NULL, 0, cont.buf, cont.len);
1622 cont.len = 0;
Kay Sievers084681d2012-06-28 09:38:53 +02001623}
1624
Linus Torvalds5e467652016-10-09 11:53:00 -07001625static bool cont_add(int facility, int level, enum log_flags flags, const char *text, size_t len)
Kay Sievers084681d2012-06-28 09:38:53 +02001626{
Tejun Heo6fe29352015-06-25 15:01:30 -07001627 /*
1628 * If ext consoles are present, flush and skip in-kernel
1629 * continuation. See nr_ext_console_drivers definition. Also, if
1630 * the line gets too long, split it up in separate records.
1631 */
1632 if (nr_ext_console_drivers || cont.len + len > sizeof(cont.buf)) {
Linus Torvalds5e467652016-10-09 11:53:00 -07001633 cont_flush();
Kay Sievers084681d2012-06-28 09:38:53 +02001634 return false;
1635 }
1636
1637 if (!cont.len) {
1638 cont.facility = facility;
1639 cont.level = level;
1640 cont.owner = current;
1641 cont.ts_nsec = local_clock();
Linus Torvalds5e467652016-10-09 11:53:00 -07001642 cont.flags = flags;
Kay Sievers084681d2012-06-28 09:38:53 +02001643 }
1644
1645 memcpy(cont.buf + cont.len, text, len);
1646 cont.len += len;
Kay Sieverseab07262012-07-16 18:35:30 -07001647
Linus Torvalds5e467652016-10-09 11:53:00 -07001648 // The original flags come from the first line,
1649 // but later continuations can add a newline.
1650 if (flags & LOG_NEWLINE) {
1651 cont.flags |= LOG_NEWLINE;
1652 cont_flush();
1653 }
1654
Kay Sieverseab07262012-07-16 18:35:30 -07001655 if (cont.len > (sizeof(cont.buf) * 80) / 100)
Linus Torvalds5e467652016-10-09 11:53:00 -07001656 cont_flush();
Kay Sieverseab07262012-07-16 18:35:30 -07001657
Kay Sievers084681d2012-06-28 09:38:53 +02001658 return true;
1659}
1660
Linus Torvaldsc362c7f2016-10-08 22:02:09 -07001661static size_t log_output(int facility, int level, enum log_flags lflags, const char *dict, size_t dictlen, char *text, size_t text_len)
1662{
Linus Torvaldsc362c7f2016-10-08 22:02:09 -07001663 /*
Linus Torvalds5e467652016-10-09 11:53:00 -07001664 * If an earlier line was buffered, and we're a continuation
1665 * write from the same process, try to add it to the buffer.
Linus Torvaldsc362c7f2016-10-08 22:02:09 -07001666 */
1667 if (cont.len) {
Linus Torvalds5e467652016-10-09 11:53:00 -07001668 if (cont.owner == current && (lflags & LOG_CONT)) {
1669 if (cont_add(facility, level, lflags, text, text_len))
1670 return text_len;
1671 }
1672 /* Otherwise, make sure it's flushed */
1673 cont_flush();
1674 }
Linus Torvaldsc362c7f2016-10-08 22:02:09 -07001675
Linus Torvalds8835ca52016-10-19 09:11:24 -07001676 /* Skip empty continuation lines that couldn't be added - they just flush */
1677 if (!text_len && (lflags & LOG_CONT))
1678 return 0;
1679
Linus Torvalds5e467652016-10-09 11:53:00 -07001680 /* If it doesn't end in a newline, try to buffer the current line */
1681 if (!(lflags & LOG_NEWLINE)) {
1682 if (cont_add(facility, level, lflags, text, text_len))
Linus Torvaldsc362c7f2016-10-08 22:02:09 -07001683 return text_len;
1684 }
1685
Linus Torvalds5e467652016-10-09 11:53:00 -07001686 /* Store it in the record log */
Linus Torvaldsc362c7f2016-10-08 22:02:09 -07001687 return log_store(facility, level, lflags, 0, dict, dictlen, text, text_len);
1688}
1689
Kay Sievers7ff95542012-05-03 02:29:13 +02001690asmlinkage int vprintk_emit(int facility, int level,
1691 const char *dict, size_t dictlen,
1692 const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
Kay Sievers7ff95542012-05-03 02:29:13 +02001694 static char textbuf[LOG_LINE_MAX];
1695 char *text = textbuf;
Pierre Kuoaec47ca2017-07-11 14:40:55 +08001696 size_t text_len;
Kay Sievers5becfb12012-07-09 12:15:42 -07001697 enum log_flags lflags = 0;
Nick Andrewac60ad72008-05-12 21:21:04 +02001698 unsigned long flags;
Pierre Kuoaec47ca2017-07-11 14:40:55 +08001699 int printed_len;
Steven Rostedt458df9f2014-06-04 16:11:38 -07001700 bool in_sched = false;
Jan Kara608873c2014-06-04 16:11:35 -07001701
Joe Perchesa39d4a82014-12-10 15:50:15 -08001702 if (level == LOGLEVEL_SCHED) {
1703 level = LOGLEVEL_DEFAULT;
Steven Rostedt458df9f2014-06-04 16:11:38 -07001704 in_sched = true;
1705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Andrew Cooks2fa72c82012-12-17 15:59:56 -08001707 boot_delay_msec(level);
Dave Youngaf913222009-09-22 16:43:33 -07001708 printk_delay();
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001709
Sergey Senozhatskya8199372016-03-17 14:21:20 -07001710 /* This stops the holder of console_sem just where we want him */
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09001711 logbuf_lock_irqsave(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 /*
Kay Sievers7ff95542012-05-03 02:29:13 +02001713 * The printf needs to come first; we need the syslog
1714 * prefix which might be passed-in as a parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 */
Markus Trippelsdorf98e35f52014-10-13 15:51:13 -07001716 text_len = vscnprintf(text, sizeof(textbuf), fmt, args);
Nick Andrewac60ad72008-05-12 21:21:04 +02001717
Kay Sievers7ff95542012-05-03 02:29:13 +02001718 /* mark and strip a trailing newline */
Kay Sieversc313af12012-05-14 20:46:27 +02001719 if (text_len && text[text_len-1] == '\n') {
1720 text_len--;
Kay Sievers5becfb12012-07-09 12:15:42 -07001721 lflags |= LOG_NEWLINE;
Kay Sievers7ff95542012-05-03 02:29:13 +02001722 }
Kay Sievers9d90c8d2011-03-13 03:19:51 +01001723
Joe Perches088a52a2012-07-30 14:40:19 -07001724 /* strip kernel syslog prefix and extract log level or control flags */
1725 if (facility == 0) {
Linus Torvalds4bcc5952016-10-08 20:32:40 -07001726 int kern_level;
Joe Perches088a52a2012-07-30 14:40:19 -07001727
Linus Torvalds4bcc5952016-10-08 20:32:40 -07001728 while ((kern_level = printk_get_level(text)) != 0) {
Joe Perches088a52a2012-07-30 14:40:19 -07001729 switch (kern_level) {
1730 case '0' ... '7':
Joe Perchesa39d4a82014-12-10 15:50:15 -08001731 if (level == LOGLEVEL_DEFAULT)
Joe Perches088a52a2012-07-30 14:40:19 -07001732 level = kern_level - '0';
Joe Perchesa39d4a82014-12-10 15:50:15 -08001733 /* fallthrough */
Joe Perches088a52a2012-07-30 14:40:19 -07001734 case 'd': /* KERN_DEFAULT */
1735 lflags |= LOG_PREFIX;
Linus Torvalds4bcc5952016-10-08 20:32:40 -07001736 break;
1737 case 'c': /* KERN_CONT */
1738 lflags |= LOG_CONT;
Joe Perches088a52a2012-07-30 14:40:19 -07001739 }
Linus Torvalds4bcc5952016-10-08 20:32:40 -07001740
1741 text_len -= 2;
1742 text += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001744 }
Nick Andrewac60ad72008-05-12 21:21:04 +02001745
Joe Perchesa39d4a82014-12-10 15:50:15 -08001746 if (level == LOGLEVEL_DEFAULT)
Kay Sieversc313af12012-05-14 20:46:27 +02001747 level = default_message_loglevel;
1748
Kay Sievers5becfb12012-07-09 12:15:42 -07001749 if (dict)
1750 lflags |= LOG_PREFIX|LOG_NEWLINE;
Kay Sievers7ff95542012-05-03 02:29:13 +02001751
Pierre Kuoaec47ca2017-07-11 14:40:55 +08001752 printed_len = log_output(facility, level, lflags, dict, dictlen, text, text_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09001754 logbuf_unlock_irqrestore(flags);
Jan Kara939f04b2014-06-04 16:11:37 -07001755
Steven Rostedt458df9f2014-06-04 16:11:38 -07001756 /* If called from the scheduler, we can not call up(). */
Andrew Mortond18bbc22014-07-02 15:22:38 -07001757 if (!in_sched) {
Jan Kara5874af22014-08-06 16:09:10 -07001758 /*
Andrew Mortond18bbc22014-07-02 15:22:38 -07001759 * Try to acquire and then immediately release the console
1760 * semaphore. The release will print out buffers and wake up
1761 * /dev/kmsg and syslog() users.
1762 */
Steven Rostedt (VMware)dbdda842018-01-10 14:24:17 +01001763 if (console_trylock()) {
Andrew Mortond18bbc22014-07-02 15:22:38 -07001764 console_unlock();
Steven Rostedt (VMware)dbdda842018-01-10 14:24:17 +01001765 } else {
1766 struct task_struct *owner = NULL;
1767 bool waiter;
1768 bool spin = false;
1769
1770 printk_safe_enter_irqsave(flags);
1771
1772 raw_spin_lock(&console_owner_lock);
1773 owner = READ_ONCE(console_owner);
1774 waiter = READ_ONCE(console_waiter);
1775 if (!waiter && owner && owner != current) {
1776 WRITE_ONCE(console_waiter, true);
1777 spin = true;
1778 }
1779 raw_spin_unlock(&console_owner_lock);
1780
1781 /*
1782 * If there is an active printk() writing to the
1783 * consoles, instead of having it write our data too,
1784 * see if we can offload that load from the active
1785 * printer, and do some printing ourselves.
1786 * Go into a spin only if there isn't already a waiter
1787 * spinning, and there is an active printer, and
1788 * that active printer isn't us (recursive printk?).
1789 */
1790 if (spin) {
1791 /* We spin waiting for the owner to release us */
1792 spin_acquire(&console_owner_dep_map, 0, 0, _THIS_IP_);
1793 /* Owner will clear console_waiter on hand off */
1794 while (READ_ONCE(console_waiter))
1795 cpu_relax();
1796
1797 spin_release(&console_owner_dep_map, 1, _THIS_IP_);
1798 printk_safe_exit_irqrestore(flags);
1799
1800 /*
1801 * The owner passed the console lock to us.
1802 * Since we did not spin on console lock, annotate
1803 * this as a trylock. Otherwise lockdep will
1804 * complain.
1805 */
1806 mutex_acquire(&console_lock_dep_map, 0, 1, _THIS_IP_);
1807 console_unlock();
1808 printk_safe_enter_irqsave(flags);
1809 }
1810 printk_safe_exit_irqrestore(flags);
1811
1812 }
Andrew Mortond18bbc22014-07-02 15:22:38 -07001813 }
Steven Rostedt458df9f2014-06-04 16:11:38 -07001814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return printed_len;
1816}
Kay Sievers7ff95542012-05-03 02:29:13 +02001817EXPORT_SYMBOL(vprintk_emit);
1818
1819asmlinkage int vprintk(const char *fmt, va_list args)
1820{
Sergey Senozhatskybd66a892016-12-27 23:16:04 +09001821 return vprintk_func(fmt, args);
Kay Sievers7ff95542012-05-03 02:29:13 +02001822}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823EXPORT_SYMBOL(vprintk);
1824
Kay Sievers7ff95542012-05-03 02:29:13 +02001825asmlinkage int printk_emit(int facility, int level,
1826 const char *dict, size_t dictlen,
1827 const char *fmt, ...)
1828{
1829 va_list args;
1830 int r;
1831
1832 va_start(args, fmt);
1833 r = vprintk_emit(facility, level, dict, dictlen, fmt, args);
1834 va_end(args);
1835
1836 return r;
1837}
1838EXPORT_SYMBOL(printk_emit);
1839
Linus Torvaldsa0cba212016-08-09 10:48:18 -07001840int vprintk_default(const char *fmt, va_list args)
Steven Rostedt (Red Hat)afdc34a2014-06-19 17:33:31 -04001841{
1842 int r;
1843
1844#ifdef CONFIG_KGDB_KDB
Petr Mladek34aaff42016-12-14 15:05:58 -08001845 /* Allow to pass printk() to kdb but avoid a recursion. */
1846 if (unlikely(kdb_trap_printk && kdb_printf_cpu < 0)) {
Daniel Thompsonf7d4ca82014-11-07 18:37:57 +00001847 r = vkdb_printf(KDB_MSGSRC_PRINTK, fmt, args);
Steven Rostedt (Red Hat)afdc34a2014-06-19 17:33:31 -04001848 return r;
1849 }
1850#endif
Linus Torvaldsa0cba212016-08-09 10:48:18 -07001851 r = vprintk_emit(0, LOGLEVEL_DEFAULT, NULL, 0, fmt, args);
Steven Rostedt (Red Hat)afdc34a2014-06-19 17:33:31 -04001852
1853 return r;
1854}
1855EXPORT_SYMBOL_GPL(vprintk_default);
1856
Kay Sievers7ff95542012-05-03 02:29:13 +02001857/**
1858 * printk - print a kernel message
1859 * @fmt: format string
1860 *
1861 * This is printk(). It can be called from any context. We want it to work.
1862 *
1863 * We try to grab the console_lock. If we succeed, it's easy - we log the
1864 * output and call the console drivers. If we fail to get the semaphore, we
1865 * place the output into the log buffer and return. The current holder of
1866 * the console_sem will notice the new output in console_unlock(); and will
1867 * send it to the consoles before releasing the lock.
1868 *
1869 * One effect of this deferred printing is that code which calls printk() and
1870 * then changes console_loglevel may break. This is because console_loglevel
1871 * is inspected when the actual printing occurs.
1872 *
1873 * See also:
1874 * printf(3)
1875 *
1876 * See the vsnprintf() documentation for format string extensions over C99.
1877 */
Andi Kleen722a9f92014-05-02 00:44:38 +02001878asmlinkage __visible int printk(const char *fmt, ...)
Kay Sievers7ff95542012-05-03 02:29:13 +02001879{
1880 va_list args;
1881 int r;
1882
Kay Sievers7ff95542012-05-03 02:29:13 +02001883 va_start(args, fmt);
Linus Torvaldsa0cba212016-08-09 10:48:18 -07001884 r = vprintk_func(fmt, args);
Kay Sievers7ff95542012-05-03 02:29:13 +02001885 va_end(args);
1886
1887 return r;
1888}
1889EXPORT_SYMBOL(printk);
Kay Sievers7f3a7812012-05-09 01:37:51 +02001890
Kay Sievers96efedf2012-07-16 18:35:29 -07001891#else /* CONFIG_PRINTK */
Matt Mackalld59745c2005-05-01 08:59:02 -07001892
Kay Sievers70498252012-07-16 18:35:29 -07001893#define LOG_LINE_MAX 0
1894#define PREFIX_MAX 0
Alex Elder249771b2014-08-06 16:09:08 -07001895
Kay Sievers96efedf2012-07-16 18:35:29 -07001896static u64 syslog_seq;
1897static u32 syslog_idx;
Kay Sieverseab07262012-07-16 18:35:30 -07001898static u64 console_seq;
1899static u32 console_idx;
Kay Sievers96efedf2012-07-16 18:35:29 -07001900static u64 log_first_seq;
1901static u32 log_first_idx;
1902static u64 log_next_seq;
Tejun Heo6fe29352015-06-25 15:01:30 -07001903static char *log_text(const struct printk_log *msg) { return NULL; }
1904static char *log_dict(const struct printk_log *msg) { return NULL; }
Joe Perches62e32ac2013-07-31 13:53:47 -07001905static struct printk_log *log_from_idx(u32 idx) { return NULL; }
Kay Sievers7f3a7812012-05-09 01:37:51 +02001906static u32 log_next(u32 idx) { return 0; }
Tejun Heo6fe29352015-06-25 15:01:30 -07001907static ssize_t msg_print_ext_header(char *buf, size_t size,
Linus Torvalds5aa068e2016-10-25 11:27:31 -07001908 struct printk_log *msg,
1909 u64 seq) { return 0; }
Tejun Heo6fe29352015-06-25 15:01:30 -07001910static ssize_t msg_print_ext_body(char *buf, size_t size,
1911 char *dict, size_t dict_len,
1912 char *text, size_t text_len) { return 0; }
Sergey Senozhatskyd9c23522016-12-24 23:09:01 +09001913static void call_console_drivers(const char *ext_text, size_t ext_len,
Tejun Heo6fe29352015-06-25 15:01:30 -07001914 const char *text, size_t len) {}
Linus Torvalds5aa068e2016-10-25 11:27:31 -07001915static size_t msg_print_text(const struct printk_log *msg,
Kay Sievers5becfb12012-07-09 12:15:42 -07001916 bool syslog, char *buf, size_t size) { return 0; }
Sergey Senozhatskycf775442016-08-02 14:03:56 -07001917static bool suppress_message_printing(int level) { return false; }
Matt Mackalld59745c2005-05-01 08:59:02 -07001918
Kay Sievers7f3a7812012-05-09 01:37:51 +02001919#endif /* CONFIG_PRINTK */
Matt Mackalld59745c2005-05-01 08:59:02 -07001920
Thomas Gleixnerd0380e62013-04-29 16:17:18 -07001921#ifdef CONFIG_EARLY_PRINTK
1922struct console *early_console;
1923
Andi Kleen722a9f92014-05-02 00:44:38 +02001924asmlinkage __visible void early_printk(const char *fmt, ...)
Thomas Gleixnerd0380e62013-04-29 16:17:18 -07001925{
1926 va_list ap;
Joe Perches1dc62442014-12-10 15:45:53 -08001927 char buf[512];
1928 int n;
1929
1930 if (!early_console)
1931 return;
Thomas Gleixnerd0380e62013-04-29 16:17:18 -07001932
1933 va_start(ap, fmt);
Joe Perches1dc62442014-12-10 15:45:53 -08001934 n = vscnprintf(buf, sizeof(buf), fmt, ap);
Thomas Gleixnerd0380e62013-04-29 16:17:18 -07001935 va_end(ap);
Joe Perches1dc62442014-12-10 15:45:53 -08001936
1937 early_console->write(early_console, buf, n);
Thomas Gleixnerd0380e62013-04-29 16:17:18 -07001938}
1939#endif
1940
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001941static int __add_preferred_console(char *name, int idx, char *options,
1942 char *brl_options)
1943{
1944 struct console_cmdline *c;
1945 int i;
1946
1947 /*
1948 * See if this tty is not yet registered, and
1949 * if we have a slot free.
1950 */
Petr Mladekdac8bbb2017-06-08 12:01:30 +02001951 for (i = 0, c = console_cmdline;
1952 i < MAX_CMDLINECONSOLES && c->name[0];
1953 i++, c++) {
Joe Perches23475402013-07-31 13:53:46 -07001954 if (strcmp(c->name, name) == 0 && c->index == idx) {
Petr Mladekdac8bbb2017-06-08 12:01:30 +02001955 if (!brl_options)
1956 preferred_console = i;
Joe Perches23475402013-07-31 13:53:46 -07001957 return 0;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001958 }
Joe Perches23475402013-07-31 13:53:46 -07001959 }
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001960 if (i == MAX_CMDLINECONSOLES)
1961 return -E2BIG;
1962 if (!brl_options)
Aleksey Makarovad86ee22017-03-15 13:28:51 +03001963 preferred_console = i;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001964 strlcpy(c->name, name, sizeof(c->name));
1965 c->options = options;
Joe Perchesbbeddf52013-07-31 13:53:45 -07001966 braille_set_options(c, brl_options);
1967
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001968 c->index = idx;
1969 return 0;
1970}
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001971/*
Alex Elder0b90fec2014-08-06 16:09:03 -07001972 * Set up a console. Called via do_early_param() in init/main.c
1973 * for each "console=" parameter in the boot command line.
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001974 */
1975static int __init console_setup(char *str)
1976{
Alex Elder0b90fec2014-08-06 16:09:03 -07001977 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for "ttyS" */
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001978 char *s, *options, *brl_options = NULL;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001979 int idx;
1980
Joe Perchesbbeddf52013-07-31 13:53:45 -07001981 if (_braille_console_setup(&str, &brl_options))
1982 return 1;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07001983
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001984 /*
1985 * Decode str into name, index, options.
1986 */
1987 if (str[0] >= '0' && str[0] <= '9') {
Yinghai Lueaa944a2007-07-15 23:37:27 -07001988 strcpy(buf, "ttyS");
1989 strncpy(buf + 4, str, sizeof(buf) - 5);
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001990 } else {
Yinghai Lueaa944a2007-07-15 23:37:27 -07001991 strncpy(buf, str, sizeof(buf) - 1);
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001992 }
Yinghai Lueaa944a2007-07-15 23:37:27 -07001993 buf[sizeof(buf) - 1] = 0;
Alex Elder249771b2014-08-06 16:09:08 -07001994 options = strchr(str, ',');
1995 if (options)
John Z. Bohach2ea1c532006-03-24 03:18:19 -08001996 *(options++) = 0;
1997#ifdef __sparc__
1998 if (!strcmp(str, "ttya"))
Yinghai Lueaa944a2007-07-15 23:37:27 -07001999 strcpy(buf, "ttyS0");
John Z. Bohach2ea1c532006-03-24 03:18:19 -08002000 if (!strcmp(str, "ttyb"))
Yinghai Lueaa944a2007-07-15 23:37:27 -07002001 strcpy(buf, "ttyS1");
John Z. Bohach2ea1c532006-03-24 03:18:19 -08002002#endif
Yinghai Lueaa944a2007-07-15 23:37:27 -07002003 for (s = buf; *s; s++)
Alex Elder249771b2014-08-06 16:09:08 -07002004 if (isdigit(*s) || *s == ',')
John Z. Bohach2ea1c532006-03-24 03:18:19 -08002005 break;
2006 idx = simple_strtoul(s, NULL, 10);
2007 *s = 0;
2008
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002009 __add_preferred_console(buf, idx, options, brl_options);
Markus Armbruster9e124fe2008-05-26 23:31:07 +01002010 console_set_on_cmdline = 1;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08002011 return 1;
2012}
2013__setup("console=", console_setup);
2014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015/**
Matt Mackall3c0547b2005-05-16 21:53:47 -07002016 * add_preferred_console - add a device to the list of preferred consoles.
Martin Waitzddad86c2005-11-13 16:08:14 -08002017 * @name: device name
2018 * @idx: device index
2019 * @options: options for this console
Matt Mackall3c0547b2005-05-16 21:53:47 -07002020 *
2021 * The last preferred console added will be used for kernel messages
2022 * and stdin/out/err for init. Normally this is used by console_setup
2023 * above to handle user-supplied console arguments; however it can also
2024 * be used by arch-specific code either to override the user or more
2025 * commonly to provide a default console (ie from PROM variables) when
2026 * the user has not supplied one.
2027 */
David S. Millerfb445ee2007-12-29 01:19:49 -08002028int add_preferred_console(char *name, int idx, char *options)
Matt Mackall3c0547b2005-05-16 21:53:47 -07002029{
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002030 return __add_preferred_console(name, idx, options, NULL);
Matt Mackall3c0547b2005-05-16 21:53:47 -07002031}
2032
Neil Zhangd25d9fe2014-08-06 16:09:12 -07002033bool console_suspend_enabled = true;
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002034EXPORT_SYMBOL(console_suspend_enabled);
2035
2036static int __init console_suspend_disable(char *str)
2037{
Neil Zhangd25d9fe2014-08-06 16:09:12 -07002038 console_suspend_enabled = false;
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002039 return 1;
2040}
2041__setup("no_console_suspend", console_suspend_disable);
Yanmin Zhang134620f2011-10-31 17:11:27 -07002042module_param_named(console_suspend, console_suspend_enabled,
2043 bool, S_IRUGO | S_IWUSR);
2044MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
2045 " and hibernate operations");
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002046
Matt Mackall3c0547b2005-05-16 21:53:47 -07002047/**
Linus Torvalds557240b2006-06-19 18:16:01 -07002048 * suspend_console - suspend the console subsystem
2049 *
2050 * This disables printk() while we go into suspend states
2051 */
2052void suspend_console(void)
2053{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002054 if (!console_suspend_enabled)
2055 return;
Pavel Machek0d630812008-07-23 21:28:32 -07002056 printk("Suspending console(s) (use no_console_suspend to debug)\n");
Torben Hohnac751ef2011-01-25 15:07:35 -08002057 console_lock();
Linus Torvalds557240b2006-06-19 18:16:01 -07002058 console_suspended = 1;
Jan Karabd8d7cf2014-06-04 16:11:36 -07002059 up_console_sem();
Linus Torvalds557240b2006-06-19 18:16:01 -07002060}
2061
2062void resume_console(void)
2063{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002064 if (!console_suspend_enabled)
2065 return;
Jan Karabd8d7cf2014-06-04 16:11:36 -07002066 down_console_sem();
Linus Torvalds557240b2006-06-19 18:16:01 -07002067 console_suspended = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08002068 console_unlock();
Linus Torvalds557240b2006-06-19 18:16:01 -07002069}
2070
2071/**
Kevin Cernekee034260d2010-06-03 22:11:25 -07002072 * console_cpu_notify - print deferred console messages after CPU hotplug
Sebastian Andrzej Siewior90b14882016-11-03 15:49:58 +01002073 * @cpu: unused
Kevin Cernekee034260d2010-06-03 22:11:25 -07002074 *
2075 * If printk() is called from a CPU that is not online yet, the messages
Sergey Senozhatsky64ca7522017-01-21 19:47:29 +09002076 * will be printed on the console only if there are CON_ANYTIME consoles.
2077 * This function is called when a new CPU comes online (or fails to come
2078 * up) or goes offline.
Kevin Cernekee034260d2010-06-03 22:11:25 -07002079 */
Sebastian Andrzej Siewior90b14882016-11-03 15:49:58 +01002080static int console_cpu_notify(unsigned int cpu)
Kevin Cernekee034260d2010-06-03 22:11:25 -07002081{
Thomas Gleixnerf97960f2016-11-17 17:31:55 +01002082 if (!cpuhp_tasks_frozen) {
Sergey Senozhatsky64ca7522017-01-21 19:47:29 +09002083 /* If trylock fails, someone else is doing the printing */
2084 if (console_trylock())
2085 console_unlock();
Kevin Cernekee034260d2010-06-03 22:11:25 -07002086 }
Sebastian Andrzej Siewior90b14882016-11-03 15:49:58 +01002087 return 0;
Kevin Cernekee034260d2010-06-03 22:11:25 -07002088}
2089
2090/**
Torben Hohnac751ef2011-01-25 15:07:35 -08002091 * console_lock - lock the console system for exclusive use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002093 * Acquires a lock which guarantees that the caller has
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 * exclusive access to the console system and the console_drivers list.
2095 *
2096 * Can sleep, returns nothing.
2097 */
Torben Hohnac751ef2011-01-25 15:07:35 -08002098void console_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
Daniel Vetter6b898c02012-09-17 23:03:31 +00002100 might_sleep();
2101
Jan Karabd8d7cf2014-06-04 16:11:36 -07002102 down_console_sem();
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01002103 if (console_suspended)
2104 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 console_locked = 1;
2106 console_may_schedule = 1;
2107}
Torben Hohnac751ef2011-01-25 15:07:35 -08002108EXPORT_SYMBOL(console_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
Torben Hohnac751ef2011-01-25 15:07:35 -08002110/**
2111 * console_trylock - try to lock the console system for exclusive use.
2112 *
Alex Elder0b90fec2014-08-06 16:09:03 -07002113 * Try to acquire a lock which guarantees that the caller has exclusive
2114 * access to the console system and the console_drivers list.
Torben Hohnac751ef2011-01-25 15:07:35 -08002115 *
2116 * returns 1 on success, and 0 on failure to acquire the lock.
2117 */
2118int console_trylock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
Jan Karabd8d7cf2014-06-04 16:11:36 -07002120 if (down_trylock_console_sem())
Torben Hohnac751ef2011-01-25 15:07:35 -08002121 return 0;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01002122 if (console_suspended) {
Jan Karabd8d7cf2014-06-04 16:11:36 -07002123 up_console_sem();
Torben Hohnac751ef2011-01-25 15:07:35 -08002124 return 0;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01002125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 console_locked = 1;
Sergey Senozhatsky6b97a202016-03-17 14:21:23 -07002127 /*
2128 * When PREEMPT_COUNT disabled we can't reliably detect if it's
2129 * safe to schedule (e.g. calling printk while holding a spin_lock),
2130 * because preempt_disable()/preempt_enable() are just barriers there
2131 * and preempt_count() is always 0.
2132 *
2133 * RCU read sections have a separate preemption counter when
2134 * PREEMPT_RCU enabled thus we must take extra care and check
2135 * rcu_preempt_depth(), otherwise RCU read sections modify
2136 * preempt_count().
2137 */
2138 console_may_schedule = !oops_in_progress &&
2139 preemptible() &&
2140 !rcu_preempt_depth();
Torben Hohnac751ef2011-01-25 15:07:35 -08002141 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
Torben Hohnac751ef2011-01-25 15:07:35 -08002143EXPORT_SYMBOL(console_trylock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145int is_console_locked(void)
2146{
2147 return console_locked;
2148}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Sergey Senozhatskya8199372016-03-17 14:21:20 -07002150/*
2151 * Check if we have any console that is capable of printing while cpu is
2152 * booting or shutting down. Requires console_sem.
2153 */
2154static int have_callable_console(void)
2155{
2156 struct console *con;
2157
2158 for_each_console(con)
Sergey Senozhatskyadaf6592016-03-17 14:21:27 -07002159 if ((con->flags & CON_ENABLED) &&
2160 (con->flags & CON_ANYTIME))
Sergey Senozhatskya8199372016-03-17 14:21:20 -07002161 return 1;
2162
2163 return 0;
2164}
2165
2166/*
2167 * Can we actually use the console at this time on this cpu?
2168 *
2169 * Console drivers may assume that per-cpu resources have been allocated. So
2170 * unless they're explicitly marked as being able to cope (CON_ANYTIME) don't
2171 * call them until this CPU is officially up.
2172 */
2173static inline int can_use_console(void)
2174{
2175 return cpu_online(raw_smp_processor_id()) || have_callable_console();
2176}
2177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178/**
Torben Hohnac751ef2011-01-25 15:07:35 -08002179 * console_unlock - unlock the console system
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002181 * Releases the console_lock which the caller holds on the console system
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 * and the console driver list.
2183 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002184 * While the console_lock was held, console output may have been buffered
2185 * by printk(). If this is the case, console_unlock(); emits
2186 * the output prior to releasing the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 *
Kay Sievers7f3a7812012-05-09 01:37:51 +02002188 * If there is output waiting, we wake /dev/kmsg and syslog() users.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002190 * console_unlock(); may be called from any context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 */
Torben Hohnac751ef2011-01-25 15:07:35 -08002192void console_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
Tejun Heo6fe29352015-06-25 15:01:30 -07002194 static char ext_text[CONSOLE_EXT_LOG_MAX];
Kay Sievers70498252012-07-16 18:35:29 -07002195 static char text[LOG_LINE_MAX + PREFIX_MAX];
Kay Sievers7ff95542012-05-03 02:29:13 +02002196 static u64 seen_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 unsigned long flags;
Kay Sievers7ff95542012-05-03 02:29:13 +02002198 bool wake_klogd = false;
Steven Rostedt (VMware)dbdda842018-01-10 14:24:17 +01002199 bool waiter = false;
Tejun Heo8d91f8b2016-01-15 16:58:24 -08002200 bool do_cond_resched, retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Linus Torvalds557240b2006-06-19 18:16:01 -07002202 if (console_suspended) {
Jan Karabd8d7cf2014-06-04 16:11:36 -07002203 up_console_sem();
Linus Torvalds557240b2006-06-19 18:16:01 -07002204 return;
2205 }
Antonino A. Daplas78944e542006-08-05 12:14:16 -07002206
Tejun Heo8d91f8b2016-01-15 16:58:24 -08002207 /*
Petr Mladek257ab442017-03-24 17:14:05 +01002208 * Console drivers are called with interrupts disabled, so
Tejun Heo8d91f8b2016-01-15 16:58:24 -08002209 * @console_may_schedule should be cleared before; however, we may
2210 * end up dumping a lot of lines, for example, if called from
2211 * console registration path, and should invoke cond_resched()
2212 * between lines if allowable. Not doing so can cause a very long
2213 * scheduling stall on a slow console leading to RCU stall and
2214 * softlockup warnings which exacerbate the issue with more
2215 * messages practically incapacitating the system.
Petr Mladek257ab442017-03-24 17:14:05 +01002216 *
2217 * console_trylock() is not able to detect the preemptive
2218 * context reliably. Therefore the value must be stored before
2219 * and cleared after the the "again" goto label.
Tejun Heo8d91f8b2016-01-15 16:58:24 -08002220 */
2221 do_cond_resched = console_may_schedule;
Petr Mladek257ab442017-03-24 17:14:05 +01002222again:
Antonino A. Daplas78944e542006-08-05 12:14:16 -07002223 console_may_schedule = 0;
2224
Sergey Senozhatskya8199372016-03-17 14:21:20 -07002225 /*
2226 * We released the console_sem lock, so we need to recheck if
2227 * cpu is online and (if not) is there at least one CON_ANYTIME
2228 * console.
2229 */
2230 if (!can_use_console()) {
2231 console_locked = 0;
2232 up_console_sem();
2233 return;
2234 }
2235
Kay Sievers7ff95542012-05-03 02:29:13 +02002236 for (;;) {
Joe Perches62e32ac2013-07-31 13:53:47 -07002237 struct printk_log *msg;
Tejun Heo6fe29352015-06-25 15:01:30 -07002238 size_t ext_len = 0;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02002239 size_t len;
Kay Sievers7ff95542012-05-03 02:29:13 +02002240
Sergey Senozhatskyf9752372016-12-27 23:16:09 +09002241 printk_safe_enter_irqsave(flags);
2242 raw_spin_lock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02002243 if (seen_seq != log_next_seq) {
2244 wake_klogd = true;
2245 seen_seq = log_next_seq;
2246 }
2247
2248 if (console_seq < log_first_seq) {
Maxim Akristiniy9afe77e2017-10-23 19:51:48 +03002249 len = sprintf(text, "** %u printk messages dropped **\n",
Will Deacon84b5ec82014-06-04 16:11:45 -07002250 (unsigned)(log_first_seq - console_seq));
2251
Kay Sievers7ff95542012-05-03 02:29:13 +02002252 /* messages are gone, move to first one */
2253 console_seq = log_first_seq;
2254 console_idx = log_first_idx;
Will Deacon84b5ec82014-06-04 16:11:45 -07002255 } else {
2256 len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02002257 }
Kay Sievers084681d2012-06-28 09:38:53 +02002258skip:
Kay Sievers7ff95542012-05-03 02:29:13 +02002259 if (console_seq == log_next_seq)
2260 break;
2261
2262 msg = log_from_idx(console_idx);
Sergey Senozhatskyd9c23522016-12-24 23:09:01 +09002263 if (suppress_message_printing(msg->level)) {
Kay Sievers084681d2012-06-28 09:38:53 +02002264 /*
2265 * Skip record we have buffered and already printed
Sergey Senozhatskycf775442016-08-02 14:03:56 -07002266 * directly to the console when we received it, and
2267 * record that has level above the console loglevel.
Kay Sievers084681d2012-06-28 09:38:53 +02002268 */
2269 console_idx = log_next(console_idx);
2270 console_seq++;
2271 goto skip;
2272 }
Kay Sievers649e6ee2012-05-10 04:30:45 +02002273
Linus Torvalds5aa068e2016-10-25 11:27:31 -07002274 len += msg_print_text(msg, false, text + len, sizeof(text) - len);
Tejun Heo6fe29352015-06-25 15:01:30 -07002275 if (nr_ext_console_drivers) {
2276 ext_len = msg_print_ext_header(ext_text,
2277 sizeof(ext_text),
Linus Torvalds5aa068e2016-10-25 11:27:31 -07002278 msg, console_seq);
Tejun Heo6fe29352015-06-25 15:01:30 -07002279 ext_len += msg_print_ext_body(ext_text + ext_len,
2280 sizeof(ext_text) - ext_len,
2281 log_dict(msg), msg->dict_len,
2282 log_text(msg), msg->text_len);
2283 }
Kay Sievers7ff95542012-05-03 02:29:13 +02002284 console_idx = log_next(console_idx);
2285 console_seq++;
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002286 raw_spin_unlock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02002287
Steven Rostedt (VMware)dbdda842018-01-10 14:24:17 +01002288 /*
2289 * While actively printing out messages, if another printk()
2290 * were to occur on another CPU, it may wait for this one to
2291 * finish. This task can not be preempted if there is a
2292 * waiter waiting to take over.
2293 */
2294 raw_spin_lock(&console_owner_lock);
2295 console_owner = current;
2296 raw_spin_unlock(&console_owner_lock);
2297
2298 /* The waiter may spin on us after setting console_owner */
2299 spin_acquire(&console_owner_dep_map, 0, 0, _THIS_IP_);
2300
Steven Rostedt81d68a92008-05-12 21:20:42 +02002301 stop_critical_timings(); /* don't trace print latency */
Sergey Senozhatskyd9c23522016-12-24 23:09:01 +09002302 call_console_drivers(ext_text, ext_len, text, len);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002303 start_critical_timings();
Steven Rostedt (VMware)dbdda842018-01-10 14:24:17 +01002304
2305 raw_spin_lock(&console_owner_lock);
2306 waiter = READ_ONCE(console_waiter);
2307 console_owner = NULL;
2308 raw_spin_unlock(&console_owner_lock);
2309
2310 /*
2311 * If there is a waiter waiting for us, then pass the
2312 * rest of the work load over to that waiter.
2313 */
2314 if (waiter)
2315 break;
2316
2317 /* There was no waiter, and nothing will spin on us here */
2318 spin_release(&console_owner_dep_map, 1, _THIS_IP_);
2319
Sergey Senozhatskyf9752372016-12-27 23:16:09 +09002320 printk_safe_exit_irqrestore(flags);
Tejun Heo8d91f8b2016-01-15 16:58:24 -08002321
2322 if (do_cond_resched)
2323 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 }
Steven Rostedt (VMware)dbdda842018-01-10 14:24:17 +01002325
2326 /*
2327 * If there is an active waiter waiting on the console_lock.
2328 * Pass off the printing to the waiter, and the waiter
2329 * will continue printing on its CPU, and when all writing
2330 * has finished, the last printer will wake up klogd.
2331 */
2332 if (waiter) {
2333 WRITE_ONCE(console_waiter, false);
2334 /* The waiter is now free to continue */
2335 spin_release(&console_owner_dep_map, 1, _THIS_IP_);
2336 /*
2337 * Hand off console_lock to waiter. The waiter will perform
2338 * the up(). After this, the waiter is the console_lock owner.
2339 */
2340 mutex_release(&console_lock_dep_map, 1, _THIS_IP_);
2341 printk_safe_exit_irqrestore(flags);
2342 /* Note, if waiter is set, logbuf_lock is not held */
2343 return;
2344 }
2345
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 console_locked = 0;
Feng Tangfe3d8ad2011-03-22 16:34:21 -07002347
2348 /* Release the exclusive_console once it is used */
2349 if (unlikely(exclusive_console))
2350 exclusive_console = NULL;
2351
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002352 raw_spin_unlock(&logbuf_lock);
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002353
Jan Karabd8d7cf2014-06-04 16:11:36 -07002354 up_console_sem();
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002355
2356 /*
2357 * Someone could have filled up the buffer again, so re-check if there's
2358 * something to flush. In case we cannot trylock the console_sem again,
2359 * there's a new owner and the console_unlock() from them will do the
2360 * flush, no worries.
2361 */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002362 raw_spin_lock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02002363 retry = console_seq != log_next_seq;
Sergey Senozhatskyf9752372016-12-27 23:16:09 +09002364 raw_spin_unlock(&logbuf_lock);
2365 printk_safe_exit_irqrestore(flags);
Peter Zijlstra09dc3cf2011-12-08 14:34:13 -08002366
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002367 if (retry && console_trylock())
2368 goto again;
2369
Kirill Korotaeve3e8a752007-02-10 01:46:19 -08002370 if (wake_klogd)
2371 wake_up_klogd();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372}
Torben Hohnac751ef2011-01-25 15:07:35 -08002373EXPORT_SYMBOL(console_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Martin Waitzddad86c2005-11-13 16:08:14 -08002375/**
2376 * console_conditional_schedule - yield the CPU if required
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 *
2378 * If the console code is currently allowed to sleep, and
2379 * if this CPU should yield the CPU to another task, do
2380 * so here.
2381 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002382 * Must be called within console_lock();.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 */
2384void __sched console_conditional_schedule(void)
2385{
2386 if (console_may_schedule)
2387 cond_resched();
2388}
2389EXPORT_SYMBOL(console_conditional_schedule);
2390
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391void console_unblank(void)
2392{
2393 struct console *c;
2394
2395 /*
2396 * console_unblank can no longer be called in interrupt context unless
2397 * oops_in_progress is set to 1..
2398 */
2399 if (oops_in_progress) {
Jan Karabd8d7cf2014-06-04 16:11:36 -07002400 if (down_trylock_console_sem() != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 return;
2402 } else
Torben Hohnac751ef2011-01-25 15:07:35 -08002403 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 console_locked = 1;
2406 console_may_schedule = 0;
Robin Getz4d091612009-07-01 21:08:37 -04002407 for_each_console(c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 if ((c->flags & CON_ENABLED) && c->unblank)
2409 c->unblank();
Torben Hohnac751ef2011-01-25 15:07:35 -08002410 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
Tejun Heo8d91f8b2016-01-15 16:58:24 -08002413/**
2414 * console_flush_on_panic - flush console content on panic
2415 *
2416 * Immediately output all pending messages no matter what.
2417 */
2418void console_flush_on_panic(void)
2419{
2420 /*
2421 * If someone else is holding the console lock, trylock will fail
2422 * and may_schedule may be set. Ignore and proceed to unlock so
2423 * that messages are flushed out. As this can be called from any
2424 * context and we don't want to get preempted while flushing,
2425 * ensure may_schedule is cleared.
2426 */
2427 console_trylock();
2428 console_may_schedule = 0;
2429 console_unlock();
2430}
2431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432/*
2433 * Return the console tty driver structure and its associated index
2434 */
2435struct tty_driver *console_device(int *index)
2436{
2437 struct console *c;
2438 struct tty_driver *driver = NULL;
2439
Torben Hohnac751ef2011-01-25 15:07:35 -08002440 console_lock();
Robin Getz4d091612009-07-01 21:08:37 -04002441 for_each_console(c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 if (!c->device)
2443 continue;
2444 driver = c->device(c, index);
2445 if (driver)
2446 break;
2447 }
Torben Hohnac751ef2011-01-25 15:07:35 -08002448 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 return driver;
2450}
2451
2452/*
2453 * Prevent further output on the passed console device so that (for example)
2454 * serial drivers can disable console output before suspending a port, and can
2455 * re-enable output afterwards.
2456 */
2457void console_stop(struct console *console)
2458{
Torben Hohnac751ef2011-01-25 15:07:35 -08002459 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 console->flags &= ~CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08002461 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462}
2463EXPORT_SYMBOL(console_stop);
2464
2465void console_start(struct console *console)
2466{
Torben Hohnac751ef2011-01-25 15:07:35 -08002467 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 console->flags |= CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08002469 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470}
2471EXPORT_SYMBOL(console_start);
2472
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07002473static int __read_mostly keep_bootcon;
2474
2475static int __init keep_bootcon_setup(char *str)
2476{
2477 keep_bootcon = 1;
Andrew Morton27083ba2013-11-12 15:08:50 -08002478 pr_info("debug: skip boot console de-registration.\n");
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07002479
2480 return 0;
2481}
2482
2483early_param("keep_bootcon", keep_bootcon_setup);
2484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485/*
2486 * The console driver calls this routine during kernel initialization
2487 * to register the console printing procedure with printk() and to
2488 * print any messages that were printed by the kernel before the
2489 * console driver was initialized.
Robin Getz4d091612009-07-01 21:08:37 -04002490 *
2491 * This can happen pretty early during the boot process (because of
2492 * early_printk) - sometimes before setup_arch() completes - be careful
2493 * of what kernel features are used - they may not be initialised yet.
2494 *
2495 * There are two types of consoles - bootconsoles (early_printk) and
2496 * "real" consoles (everything which is not a bootconsole) which are
2497 * handled differently.
2498 * - Any number of bootconsoles can be registered at any time.
2499 * - As soon as a "real" console is registered, all bootconsoles
2500 * will be unregistered automatically.
2501 * - Once a "real" console is registered, any attempt to register a
2502 * bootconsoles will be rejected
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 */
Robin Getz4d091612009-07-01 21:08:37 -04002504void register_console(struct console *newcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
Jesper Juhl40dc5652005-10-30 15:02:46 -08002506 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 unsigned long flags;
Robin Getz4d091612009-07-01 21:08:37 -04002508 struct console *bcon = NULL;
Joe Perches23475402013-07-31 13:53:46 -07002509 struct console_cmdline *c;
Aleksey Makarovb077baf2017-03-15 13:28:50 +03002510 static bool has_preferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Andreas Bießmann16cf48a2013-08-02 12:23:34 +02002512 if (console_drivers)
2513 for_each_console(bcon)
2514 if (WARN(bcon == newcon,
2515 "console '%s%d' already registered\n",
2516 bcon->name, bcon->index))
2517 return;
2518
Robin Getz4d091612009-07-01 21:08:37 -04002519 /*
2520 * before we register a new CON_BOOT console, make sure we don't
2521 * already have a valid console
2522 */
2523 if (console_drivers && newcon->flags & CON_BOOT) {
2524 /* find the last or real console */
2525 for_each_console(bcon) {
2526 if (!(bcon->flags & CON_BOOT)) {
Andrew Morton27083ba2013-11-12 15:08:50 -08002527 pr_info("Too late to register bootconsole %s%d\n",
Robin Getz4d091612009-07-01 21:08:37 -04002528 newcon->name, newcon->index);
2529 return;
2530 }
2531 }
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002532 }
2533
Robin Getz4d091612009-07-01 21:08:37 -04002534 if (console_drivers && console_drivers->flags & CON_BOOT)
2535 bcon = console_drivers;
2536
Aleksey Makarovb077baf2017-03-15 13:28:50 +03002537 if (!has_preferred || bcon || !console_drivers)
Aleksey Makarovad86ee22017-03-15 13:28:51 +03002538 has_preferred = preferred_console >= 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
2540 /*
2541 * See if we want to use this console driver. If we
2542 * didn't select a console we take the first one
2543 * that registers here.
2544 */
Aleksey Makarovb077baf2017-03-15 13:28:50 +03002545 if (!has_preferred) {
Robin Getz4d091612009-07-01 21:08:37 -04002546 if (newcon->index < 0)
2547 newcon->index = 0;
2548 if (newcon->setup == NULL ||
2549 newcon->setup(newcon, NULL) == 0) {
2550 newcon->flags |= CON_ENABLED;
2551 if (newcon->device) {
2552 newcon->flags |= CON_CONSDEV;
Aleksey Makarovb077baf2017-03-15 13:28:50 +03002553 has_preferred = true;
Jan Kiszkacd3a1b82008-05-12 21:21:04 +02002554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 }
2556 }
2557
2558 /*
Petr Mladekdac8bbb2017-06-08 12:01:30 +02002559 * See if this console matches one we selected on
2560 * the command line.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 */
Petr Mladekdac8bbb2017-06-08 12:01:30 +02002562 for (i = 0, c = console_cmdline;
2563 i < MAX_CMDLINECONSOLES && c->name[0];
2564 i++, c++) {
Peter Hurleyc7cef0a2015-03-09 16:27:12 -04002565 if (!newcon->match ||
2566 newcon->match(newcon, c->name, c->index, c->options) != 0) {
2567 /* default matching */
2568 BUILD_BUG_ON(sizeof(c->name) != sizeof(newcon->name));
2569 if (strcmp(c->name, newcon->name) != 0)
2570 continue;
2571 if (newcon->index >= 0 &&
2572 newcon->index != c->index)
2573 continue;
2574 if (newcon->index < 0)
2575 newcon->index = c->index;
Joe Perchesbbeddf52013-07-31 13:53:45 -07002576
Peter Hurleyc7cef0a2015-03-09 16:27:12 -04002577 if (_braille_register_console(newcon, c))
2578 return;
Joe Perchesbbeddf52013-07-31 13:53:45 -07002579
Peter Hurleyc7cef0a2015-03-09 16:27:12 -04002580 if (newcon->setup &&
2581 newcon->setup(newcon, c->options) != 0)
2582 break;
2583 }
2584
Robin Getz4d091612009-07-01 21:08:37 -04002585 newcon->flags |= CON_ENABLED;
Aleksey Makarovad86ee22017-03-15 13:28:51 +03002586 if (i == preferred_console) {
Robin Getz4d091612009-07-01 21:08:37 -04002587 newcon->flags |= CON_CONSDEV;
Aleksey Makarovb077baf2017-03-15 13:28:50 +03002588 has_preferred = true;
Greg Edwardsab4af032005-06-23 00:09:05 -07002589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 break;
2591 }
2592
Robin Getz4d091612009-07-01 21:08:37 -04002593 if (!(newcon->flags & CON_ENABLED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 return;
2595
Robin Getz8259cf42009-07-09 13:08:37 -04002596 /*
2597 * If we have a bootconsole, and are switching to a real console,
2598 * don't print everything out again, since when the boot console, and
2599 * the real console are the same physical device, it's annoying to
2600 * see the beginning boot messages twice
2601 */
2602 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
Robin Getz4d091612009-07-01 21:08:37 -04002603 newcon->flags &= ~CON_PRINTBUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
2605 /*
2606 * Put this console in the list - keep the
2607 * preferred driver at the head of the list.
2608 */
Torben Hohnac751ef2011-01-25 15:07:35 -08002609 console_lock();
Robin Getz4d091612009-07-01 21:08:37 -04002610 if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
2611 newcon->next = console_drivers;
2612 console_drivers = newcon;
2613 if (newcon->next)
2614 newcon->next->flags &= ~CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 } else {
Robin Getz4d091612009-07-01 21:08:37 -04002616 newcon->next = console_drivers->next;
2617 console_drivers->next = newcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 }
Tejun Heo6fe29352015-06-25 15:01:30 -07002619
2620 if (newcon->flags & CON_EXTENDED)
2621 if (!nr_ext_console_drivers++)
2622 pr_info("printk: continuation disabled due to ext consoles, expect more fragments in /dev/kmsg\n");
2623
Robin Getz4d091612009-07-01 21:08:37 -04002624 if (newcon->flags & CON_PRINTBUFFER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 /*
Torben Hohnac751ef2011-01-25 15:07:35 -08002626 * console_unlock(); will print out the buffered messages
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 * for us.
2628 */
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09002629 logbuf_lock_irqsave(flags);
Kay Sievers7ff95542012-05-03 02:29:13 +02002630 console_seq = syslog_seq;
2631 console_idx = syslog_idx;
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09002632 logbuf_unlock_irqrestore(flags);
Feng Tangfe3d8ad2011-03-22 16:34:21 -07002633 /*
2634 * We're about to replay the log buffer. Only do this to the
2635 * just-registered console to avoid excessive message spam to
2636 * the already-registered consoles.
2637 */
2638 exclusive_console = newcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 }
Torben Hohnac751ef2011-01-25 15:07:35 -08002640 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01002641 console_sysfs_notify();
Robin Getz8259cf42009-07-09 13:08:37 -04002642
2643 /*
2644 * By unregistering the bootconsoles after we enable the real console
2645 * we get the "console xxx enabled" message on all the consoles -
2646 * boot consoles, real consoles, etc - this is to ensure that end
2647 * users know there might be something in the kernel's log buffer that
2648 * went to the bootconsole (that they do not see on the real console)
2649 */
Andrew Morton27083ba2013-11-12 15:08:50 -08002650 pr_info("%sconsole [%s%d] enabled\n",
Kees Cook6b802392013-11-12 15:08:49 -08002651 (newcon->flags & CON_BOOT) ? "boot" : "" ,
2652 newcon->name, newcon->index);
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07002653 if (bcon &&
2654 ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
2655 !keep_bootcon) {
Kees Cook6b802392013-11-12 15:08:49 -08002656 /* We need to iterate through all boot consoles, to make
2657 * sure we print everything out, before we unregister them.
Robin Getz8259cf42009-07-09 13:08:37 -04002658 */
Robin Getz8259cf42009-07-09 13:08:37 -04002659 for_each_console(bcon)
2660 if (bcon->flags & CON_BOOT)
2661 unregister_console(bcon);
Robin Getz8259cf42009-07-09 13:08:37 -04002662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663}
2664EXPORT_SYMBOL(register_console);
2665
Jesper Juhl40dc5652005-10-30 15:02:46 -08002666int unregister_console(struct console *console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
Jesper Juhl40dc5652005-10-30 15:02:46 -08002668 struct console *a, *b;
Joe Perchesbbeddf52013-07-31 13:53:45 -07002669 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
Andrew Morton27083ba2013-11-12 15:08:50 -08002671 pr_info("%sconsole [%s%d] disabled\n",
Kees Cook6b802392013-11-12 15:08:49 -08002672 (console->flags & CON_BOOT) ? "boot" : "" ,
2673 console->name, console->index);
2674
Joe Perchesbbeddf52013-07-31 13:53:45 -07002675 res = _braille_unregister_console(console);
2676 if (res)
2677 return res;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002678
Joe Perchesbbeddf52013-07-31 13:53:45 -07002679 res = 1;
Torben Hohnac751ef2011-01-25 15:07:35 -08002680 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 if (console_drivers == console) {
2682 console_drivers=console->next;
2683 res = 0;
Benjamin Herrenschmidte9b15b52005-11-23 13:37:44 -08002684 } else if (console_drivers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 for (a=console_drivers->next, b=console_drivers ;
2686 a; b=a, a=b->next) {
2687 if (a == console) {
2688 b->next = a->next;
2689 res = 0;
2690 break;
Jesper Juhl40dc5652005-10-30 15:02:46 -08002691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 }
2693 }
Jesper Juhl40dc5652005-10-30 15:02:46 -08002694
Tejun Heo6fe29352015-06-25 15:01:30 -07002695 if (!res && (console->flags & CON_EXTENDED))
2696 nr_ext_console_drivers--;
2697
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002698 /*
Greg Edwardsab4af032005-06-23 00:09:05 -07002699 * If this isn't the last console and it has CON_CONSDEV set, we
2700 * need to set it on the next preferred console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 */
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002702 if (console_drivers != NULL && console->flags & CON_CONSDEV)
Greg Edwardsab4af032005-06-23 00:09:05 -07002703 console_drivers->flags |= CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Stephen Chivers7fa21dd2014-05-14 08:04:39 +10002705 console->flags &= ~CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08002706 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01002707 console_sysfs_notify();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 return res;
2709}
2710EXPORT_SYMBOL(unregister_console);
Matt Mackalld59745c2005-05-01 08:59:02 -07002711
Thierry Reding81cc26f2016-01-15 16:58:21 -08002712/*
Nicolas Pitre0c688612017-04-12 18:37:14 -04002713 * Initialize the console device. This is called *early*, so
2714 * we can't necessarily depend on lots of kernel help here.
2715 * Just do some early initializations, and do the complex setup
2716 * later.
2717 */
2718void __init console_init(void)
2719{
2720 initcall_t *call;
2721
2722 /* Setup the default TTY line discipline. */
2723 n_tty_init();
2724
2725 /*
2726 * set up the console device so that later boot sequences can
2727 * inform about problems etc..
2728 */
2729 call = __con_initcall_start;
2730 while (call < __con_initcall_end) {
2731 (*call)();
2732 call++;
2733 }
2734}
2735
2736/*
Thierry Reding81cc26f2016-01-15 16:58:21 -08002737 * Some boot consoles access data that is in the init section and which will
2738 * be discarded after the initcalls have been run. To make sure that no code
2739 * will access this data, unregister the boot consoles in a late initcall.
2740 *
2741 * If for some reason, such as deferred probe or the driver being a loadable
2742 * module, the real console hasn't registered yet at this point, there will
2743 * be a brief interval in which no messages are logged to the console, which
2744 * makes it difficult to diagnose problems that occur during this time.
2745 *
2746 * To mitigate this problem somewhat, only unregister consoles whose memory
Matt Redfearn2b1be682017-07-14 14:51:12 +02002747 * intersects with the init section. Note that all other boot consoles will
2748 * get unregistred when the real preferred console is registered.
Thierry Reding81cc26f2016-01-15 16:58:21 -08002749 */
Kevin Cernekee034260d2010-06-03 22:11:25 -07002750static int __init printk_late_init(void)
Robin Getz0c5564b2007-08-20 15:22:47 -04002751{
Robin Getz4d091612009-07-01 21:08:37 -04002752 struct console *con;
Sebastian Andrzej Siewior90b14882016-11-03 15:49:58 +01002753 int ret;
Robin Getz4d091612009-07-01 21:08:37 -04002754
2755 for_each_console(con) {
Petr Mladek5a814232017-07-14 14:51:13 +02002756 if (!(con->flags & CON_BOOT))
2757 continue;
2758
2759 /* Check addresses that might be used for enabled consoles. */
2760 if (init_section_intersects(con, sizeof(*con)) ||
2761 init_section_contains(con->write, 0) ||
2762 init_section_contains(con->read, 0) ||
2763 init_section_contains(con->device, 0) ||
2764 init_section_contains(con->unblank, 0) ||
2765 init_section_contains(con->data, 0)) {
Thierry Reding81cc26f2016-01-15 16:58:21 -08002766 /*
Matt Redfearn2b1be682017-07-14 14:51:12 +02002767 * Please, consider moving the reported consoles out
2768 * of the init section.
Thierry Reding81cc26f2016-01-15 16:58:21 -08002769 */
Matt Redfearn2b1be682017-07-14 14:51:12 +02002770 pr_warn("bootconsole [%s%d] uses init memory and must be disabled even before the real one is ready\n",
2771 con->name, con->index);
2772 unregister_console(con);
Robin Getzcb00e992007-08-21 23:14:58 -04002773 }
Robin Getz0c5564b2007-08-20 15:22:47 -04002774 }
Sebastian Andrzej Siewior90b14882016-11-03 15:49:58 +01002775 ret = cpuhp_setup_state_nocalls(CPUHP_PRINTK_DEAD, "printk:dead", NULL,
2776 console_cpu_notify);
2777 WARN_ON(ret < 0);
2778 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "printk:online",
2779 console_cpu_notify, NULL);
2780 WARN_ON(ret < 0);
Robin Getz0c5564b2007-08-20 15:22:47 -04002781 return 0;
2782}
Kevin Cernekee034260d2010-06-03 22:11:25 -07002783late_initcall(printk_late_init);
Robin Getz0c5564b2007-08-20 15:22:47 -04002784
Joe Perches7ef3d2f2008-02-08 04:21:25 -08002785#if defined CONFIG_PRINTK
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002786/*
2787 * Delayed printk version, for scheduler-internal messages:
2788 */
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002789#define PRINTK_PENDING_WAKEUP 0x01
Steven Rostedt458df9f2014-06-04 16:11:38 -07002790#define PRINTK_PENDING_OUTPUT 0x02
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002791
2792static DEFINE_PER_CPU(int, printk_pending);
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002793
2794static void wake_up_klogd_work_func(struct irq_work *irq_work)
2795{
2796 int pending = __this_cpu_xchg(printk_pending, 0);
2797
Steven Rostedt458df9f2014-06-04 16:11:38 -07002798 if (pending & PRINTK_PENDING_OUTPUT) {
2799 /* If trylock fails, someone else is doing the printing */
2800 if (console_trylock())
2801 console_unlock();
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002802 }
2803
2804 if (pending & PRINTK_PENDING_WAKEUP)
2805 wake_up_interruptible(&log_wait);
2806}
2807
2808static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) = {
2809 .func = wake_up_klogd_work_func,
2810 .flags = IRQ_WORK_LAZY,
2811};
2812
2813void wake_up_klogd(void)
2814{
2815 preempt_disable();
2816 if (waitqueue_active(&log_wait)) {
2817 this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP);
Christoph Lameterbb964a92014-08-17 12:30:24 -05002818 irq_work_queue(this_cpu_ptr(&wake_up_klogd_work));
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002819 }
2820 preempt_enable();
2821}
Dave Young717115e2008-07-25 01:45:58 -07002822
Petr Mladek719f6a72017-04-20 10:52:31 +02002823int vprintk_deferred(const char *fmt, va_list args)
2824{
2825 int r;
2826
2827 r = vprintk_emit(0, LOGLEVEL_SCHED, NULL, 0, fmt, args);
2828
2829 preempt_disable();
2830 __this_cpu_or(printk_pending, PRINTK_PENDING_OUTPUT);
2831 irq_work_queue(this_cpu_ptr(&wake_up_klogd_work));
2832 preempt_enable();
2833
2834 return r;
2835}
2836
John Stultzaac74dc2014-06-04 16:11:40 -07002837int printk_deferred(const char *fmt, ...)
Peter Zijlstra600e1452012-03-15 12:35:37 +01002838{
Peter Zijlstra600e1452012-03-15 12:35:37 +01002839 va_list args;
Peter Zijlstra600e1452012-03-15 12:35:37 +01002840 int r;
2841
Peter Zijlstra600e1452012-03-15 12:35:37 +01002842 va_start(args, fmt);
Petr Mladek719f6a72017-04-20 10:52:31 +02002843 r = vprintk_deferred(fmt, args);
Peter Zijlstra600e1452012-03-15 12:35:37 +01002844 va_end(args);
2845
Peter Zijlstra600e1452012-03-15 12:35:37 +01002846 return r;
2847}
2848
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849/*
2850 * printk rate limiting, lifted from the networking subsystem.
2851 *
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07002852 * This enforces a rate limit: not more than 10 kernel messages
2853 * every 5s to make a denial-of-service attack impossible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 */
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07002855DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
2856
Christian Borntraeger5c828712009-10-23 14:58:11 +02002857int __printk_ratelimit(const char *func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858{
Christian Borntraeger5c828712009-10-23 14:58:11 +02002859 return ___ratelimit(&printk_ratelimit_state, func);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860}
Christian Borntraeger5c828712009-10-23 14:58:11 +02002861EXPORT_SYMBOL(__printk_ratelimit);
Andrew Mortonf46c4832006-11-02 22:07:16 -08002862
2863/**
2864 * printk_timed_ratelimit - caller-controlled printk ratelimiting
2865 * @caller_jiffies: pointer to caller's state
2866 * @interval_msecs: minimum interval between prints
2867 *
2868 * printk_timed_ratelimit() returns true if more than @interval_msecs
2869 * milliseconds have elapsed since the last time printk_timed_ratelimit()
2870 * returned true.
2871 */
2872bool printk_timed_ratelimit(unsigned long *caller_jiffies,
2873 unsigned int interval_msecs)
2874{
Alex Elder249771b2014-08-06 16:09:08 -07002875 unsigned long elapsed = jiffies - *caller_jiffies;
2876
2877 if (*caller_jiffies && elapsed <= msecs_to_jiffies(interval_msecs))
2878 return false;
2879
2880 *caller_jiffies = jiffies;
2881 return true;
Andrew Mortonf46c4832006-11-02 22:07:16 -08002882}
2883EXPORT_SYMBOL(printk_timed_ratelimit);
Simon Kagstrom456b5652009-10-16 14:09:18 +02002884
2885static DEFINE_SPINLOCK(dump_list_lock);
2886static LIST_HEAD(dump_list);
2887
2888/**
2889 * kmsg_dump_register - register a kernel log dumper.
Randy Dunlap64855362009-12-17 15:27:27 -08002890 * @dumper: pointer to the kmsg_dumper structure
Simon Kagstrom456b5652009-10-16 14:09:18 +02002891 *
2892 * Adds a kernel log dumper to the system. The dump callback in the
2893 * structure will be called when the kernel oopses or panics and must be
2894 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
2895 */
2896int kmsg_dump_register(struct kmsg_dumper *dumper)
2897{
2898 unsigned long flags;
2899 int err = -EBUSY;
2900
2901 /* The dump callback needs to be set */
2902 if (!dumper->dump)
2903 return -EINVAL;
2904
2905 spin_lock_irqsave(&dump_list_lock, flags);
2906 /* Don't allow registering multiple times */
2907 if (!dumper->registered) {
2908 dumper->registered = 1;
Huang Yingfb842b002011-01-12 16:59:43 -08002909 list_add_tail_rcu(&dumper->list, &dump_list);
Simon Kagstrom456b5652009-10-16 14:09:18 +02002910 err = 0;
2911 }
2912 spin_unlock_irqrestore(&dump_list_lock, flags);
2913
2914 return err;
2915}
2916EXPORT_SYMBOL_GPL(kmsg_dump_register);
2917
2918/**
2919 * kmsg_dump_unregister - unregister a kmsg dumper.
Randy Dunlap64855362009-12-17 15:27:27 -08002920 * @dumper: pointer to the kmsg_dumper structure
Simon Kagstrom456b5652009-10-16 14:09:18 +02002921 *
2922 * Removes a dump device from the system. Returns zero on success and
2923 * %-EINVAL otherwise.
2924 */
2925int kmsg_dump_unregister(struct kmsg_dumper *dumper)
2926{
2927 unsigned long flags;
2928 int err = -EINVAL;
2929
2930 spin_lock_irqsave(&dump_list_lock, flags);
2931 if (dumper->registered) {
2932 dumper->registered = 0;
Huang Yingfb842b002011-01-12 16:59:43 -08002933 list_del_rcu(&dumper->list);
Simon Kagstrom456b5652009-10-16 14:09:18 +02002934 err = 0;
2935 }
2936 spin_unlock_irqrestore(&dump_list_lock, flags);
Huang Yingfb842b002011-01-12 16:59:43 -08002937 synchronize_rcu();
Simon Kagstrom456b5652009-10-16 14:09:18 +02002938
2939 return err;
2940}
2941EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
2942
Kay Sievers7ff95542012-05-03 02:29:13 +02002943static bool always_kmsg_dump;
2944module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR);
2945
Simon Kagstrom456b5652009-10-16 14:09:18 +02002946/**
2947 * kmsg_dump - dump kernel log to kernel message dumpers.
2948 * @reason: the reason (oops, panic etc) for dumping
2949 *
Kay Sieverse2ae7152012-06-15 14:07:51 +02002950 * Call each of the registered dumper's dump() callback, which can
2951 * retrieve the kmsg records with kmsg_dump_get_line() or
2952 * kmsg_dump_get_buffer().
Simon Kagstrom456b5652009-10-16 14:09:18 +02002953 */
2954void kmsg_dump(enum kmsg_dump_reason reason)
2955{
Simon Kagstrom456b5652009-10-16 14:09:18 +02002956 struct kmsg_dumper *dumper;
Simon Kagstrom456b5652009-10-16 14:09:18 +02002957 unsigned long flags;
2958
Matthew Garrettc22ab3322012-03-05 14:59:10 -08002959 if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
2960 return;
2961
Huang Yingfb842b002011-01-12 16:59:43 -08002962 rcu_read_lock();
Kay Sieverse2ae7152012-06-15 14:07:51 +02002963 list_for_each_entry_rcu(dumper, &dump_list, list) {
2964 if (dumper->max_reason && reason > dumper->max_reason)
2965 continue;
2966
2967 /* initialize iterator with data about the stored records */
2968 dumper->active = true;
2969
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09002970 logbuf_lock_irqsave(flags);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002971 dumper->cur_seq = clear_seq;
2972 dumper->cur_idx = clear_idx;
2973 dumper->next_seq = log_next_seq;
2974 dumper->next_idx = log_next_idx;
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09002975 logbuf_unlock_irqrestore(flags);
Kay Sieverse2ae7152012-06-15 14:07:51 +02002976
2977 /* invoke dumper which will iterate over records */
2978 dumper->dump(dumper, reason);
2979
2980 /* reset iterator */
2981 dumper->active = false;
2982 }
Huang Yingfb842b002011-01-12 16:59:43 -08002983 rcu_read_unlock();
Simon Kagstrom456b5652009-10-16 14:09:18 +02002984}
Kay Sieverse2ae7152012-06-15 14:07:51 +02002985
2986/**
Anton Vorontsov533827c2012-07-20 17:28:07 -07002987 * kmsg_dump_get_line_nolock - retrieve one kmsg log line (unlocked version)
2988 * @dumper: registered kmsg dumper
2989 * @syslog: include the "<4>" prefixes
2990 * @line: buffer to copy the line to
2991 * @size: maximum size of the buffer
2992 * @len: length of line placed into buffer
2993 *
2994 * Start at the beginning of the kmsg buffer, with the oldest kmsg
2995 * record, and copy one record into the provided buffer.
2996 *
2997 * Consecutive calls will return the next available record moving
2998 * towards the end of the buffer with the youngest messages.
2999 *
3000 * A return value of FALSE indicates that there are no more records to
3001 * read.
3002 *
3003 * The function is similar to kmsg_dump_get_line(), but grabs no locks.
3004 */
3005bool kmsg_dump_get_line_nolock(struct kmsg_dumper *dumper, bool syslog,
3006 char *line, size_t size, size_t *len)
3007{
Joe Perches62e32ac2013-07-31 13:53:47 -07003008 struct printk_log *msg;
Anton Vorontsov533827c2012-07-20 17:28:07 -07003009 size_t l = 0;
3010 bool ret = false;
3011
3012 if (!dumper->active)
3013 goto out;
3014
3015 if (dumper->cur_seq < log_first_seq) {
3016 /* messages are gone, move to first available one */
3017 dumper->cur_seq = log_first_seq;
3018 dumper->cur_idx = log_first_idx;
3019 }
3020
3021 /* last entry */
3022 if (dumper->cur_seq >= log_next_seq)
3023 goto out;
3024
3025 msg = log_from_idx(dumper->cur_idx);
Linus Torvalds5aa068e2016-10-25 11:27:31 -07003026 l = msg_print_text(msg, syslog, line, size);
Anton Vorontsov533827c2012-07-20 17:28:07 -07003027
3028 dumper->cur_idx = log_next(dumper->cur_idx);
3029 dumper->cur_seq++;
3030 ret = true;
3031out:
3032 if (len)
3033 *len = l;
3034 return ret;
3035}
3036
3037/**
Kay Sieverse2ae7152012-06-15 14:07:51 +02003038 * kmsg_dump_get_line - retrieve one kmsg log line
3039 * @dumper: registered kmsg dumper
3040 * @syslog: include the "<4>" prefixes
3041 * @line: buffer to copy the line to
3042 * @size: maximum size of the buffer
3043 * @len: length of line placed into buffer
3044 *
3045 * Start at the beginning of the kmsg buffer, with the oldest kmsg
3046 * record, and copy one record into the provided buffer.
3047 *
3048 * Consecutive calls will return the next available record moving
3049 * towards the end of the buffer with the youngest messages.
3050 *
3051 * A return value of FALSE indicates that there are no more records to
3052 * read.
3053 */
3054bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog,
3055 char *line, size_t size, size_t *len)
3056{
3057 unsigned long flags;
Anton Vorontsov533827c2012-07-20 17:28:07 -07003058 bool ret;
Kay Sieverse2ae7152012-06-15 14:07:51 +02003059
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09003060 logbuf_lock_irqsave(flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07003061 ret = kmsg_dump_get_line_nolock(dumper, syslog, line, size, len);
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09003062 logbuf_unlock_irqrestore(flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07003063
Kay Sieverse2ae7152012-06-15 14:07:51 +02003064 return ret;
3065}
3066EXPORT_SYMBOL_GPL(kmsg_dump_get_line);
3067
3068/**
3069 * kmsg_dump_get_buffer - copy kmsg log lines
3070 * @dumper: registered kmsg dumper
3071 * @syslog: include the "<4>" prefixes
Randy Dunlap4f0f4af2012-06-30 15:37:24 -07003072 * @buf: buffer to copy the line to
Kay Sieverse2ae7152012-06-15 14:07:51 +02003073 * @size: maximum size of the buffer
3074 * @len: length of line placed into buffer
3075 *
3076 * Start at the end of the kmsg buffer and fill the provided buffer
3077 * with as many of the the *youngest* kmsg records that fit into it.
3078 * If the buffer is large enough, all available kmsg records will be
3079 * copied with a single call.
3080 *
3081 * Consecutive calls will fill the buffer with the next block of
3082 * available older records, not including the earlier retrieved ones.
3083 *
3084 * A return value of FALSE indicates that there are no more records to
3085 * read.
3086 */
3087bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
3088 char *buf, size_t size, size_t *len)
3089{
3090 unsigned long flags;
3091 u64 seq;
3092 u32 idx;
3093 u64 next_seq;
3094 u32 next_idx;
3095 size_t l = 0;
3096 bool ret = false;
3097
3098 if (!dumper->active)
3099 goto out;
3100
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09003101 logbuf_lock_irqsave(flags);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003102 if (dumper->cur_seq < log_first_seq) {
3103 /* messages are gone, move to first available one */
3104 dumper->cur_seq = log_first_seq;
3105 dumper->cur_idx = log_first_idx;
3106 }
3107
3108 /* last entry */
3109 if (dumper->cur_seq >= dumper->next_seq) {
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09003110 logbuf_unlock_irqrestore(flags);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003111 goto out;
3112 }
3113
3114 /* calculate length of entire buffer */
3115 seq = dumper->cur_seq;
3116 idx = dumper->cur_idx;
3117 while (seq < dumper->next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07003118 struct printk_log *msg = log_from_idx(idx);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003119
Linus Torvalds5aa068e2016-10-25 11:27:31 -07003120 l += msg_print_text(msg, true, NULL, 0);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003121 idx = log_next(idx);
3122 seq++;
3123 }
3124
3125 /* move first record forward until length fits into the buffer */
3126 seq = dumper->cur_seq;
3127 idx = dumper->cur_idx;
3128 while (l > size && seq < dumper->next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07003129 struct printk_log *msg = log_from_idx(idx);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003130
Linus Torvalds5aa068e2016-10-25 11:27:31 -07003131 l -= msg_print_text(msg, true, NULL, 0);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003132 idx = log_next(idx);
3133 seq++;
3134 }
3135
3136 /* last message in next interation */
3137 next_seq = seq;
3138 next_idx = idx;
3139
3140 l = 0;
3141 while (seq < dumper->next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07003142 struct printk_log *msg = log_from_idx(idx);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003143
Linus Torvalds5aa068e2016-10-25 11:27:31 -07003144 l += msg_print_text(msg, syslog, buf + l, size - l);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003145 idx = log_next(idx);
3146 seq++;
3147 }
3148
3149 dumper->next_seq = next_seq;
3150 dumper->next_idx = next_idx;
3151 ret = true;
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09003152 logbuf_unlock_irqrestore(flags);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003153out:
3154 if (len)
3155 *len = l;
3156 return ret;
3157}
3158EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer);
3159
3160/**
Anton Vorontsov533827c2012-07-20 17:28:07 -07003161 * kmsg_dump_rewind_nolock - reset the interator (unlocked version)
3162 * @dumper: registered kmsg dumper
3163 *
3164 * Reset the dumper's iterator so that kmsg_dump_get_line() and
3165 * kmsg_dump_get_buffer() can be called again and used multiple
3166 * times within the same dumper.dump() callback.
3167 *
3168 * The function is similar to kmsg_dump_rewind(), but grabs no locks.
3169 */
3170void kmsg_dump_rewind_nolock(struct kmsg_dumper *dumper)
3171{
3172 dumper->cur_seq = clear_seq;
3173 dumper->cur_idx = clear_idx;
3174 dumper->next_seq = log_next_seq;
3175 dumper->next_idx = log_next_idx;
3176}
3177
3178/**
Kay Sieverse2ae7152012-06-15 14:07:51 +02003179 * kmsg_dump_rewind - reset the interator
3180 * @dumper: registered kmsg dumper
3181 *
3182 * Reset the dumper's iterator so that kmsg_dump_get_line() and
3183 * kmsg_dump_get_buffer() can be called again and used multiple
3184 * times within the same dumper.dump() callback.
3185 */
3186void kmsg_dump_rewind(struct kmsg_dumper *dumper)
3187{
3188 unsigned long flags;
3189
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09003190 logbuf_lock_irqsave(flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07003191 kmsg_dump_rewind_nolock(dumper);
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09003192 logbuf_unlock_irqrestore(flags);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003193}
3194EXPORT_SYMBOL_GPL(kmsg_dump_rewind);
Tejun Heo196779b2013-04-30 15:27:12 -07003195
Tejun Heo98e5e1b2013-04-30 15:27:15 -07003196static char dump_stack_arch_desc_str[128];
3197
3198/**
3199 * dump_stack_set_arch_desc - set arch-specific str to show with task dumps
3200 * @fmt: printf-style format string
3201 * @...: arguments for the format string
3202 *
3203 * The configured string will be printed right after utsname during task
3204 * dumps. Usually used to add arch-specific system identifiers. If an
3205 * arch wants to make use of such an ID string, it should initialize this
3206 * as soon as possible during boot.
3207 */
3208void __init dump_stack_set_arch_desc(const char *fmt, ...)
3209{
3210 va_list args;
3211
3212 va_start(args, fmt);
3213 vsnprintf(dump_stack_arch_desc_str, sizeof(dump_stack_arch_desc_str),
3214 fmt, args);
3215 va_end(args);
3216}
3217
Tejun Heo196779b2013-04-30 15:27:12 -07003218/**
3219 * dump_stack_print_info - print generic debug info for dump_stack()
3220 * @log_lvl: log level
3221 *
3222 * Arch-specific dump_stack() implementations can use this function to
3223 * print out the same debug information as the generic dump_stack().
3224 */
3225void dump_stack_print_info(const char *log_lvl)
3226{
3227 printk("%sCPU: %d PID: %d Comm: %.20s %s %s %.*s\n",
3228 log_lvl, raw_smp_processor_id(), current->pid, current->comm,
3229 print_tainted(), init_utsname()->release,
3230 (int)strcspn(init_utsname()->version, " "),
3231 init_utsname()->version);
Tejun Heo98e5e1b2013-04-30 15:27:15 -07003232
3233 if (dump_stack_arch_desc_str[0] != '\0')
3234 printk("%sHardware name: %s\n",
3235 log_lvl, dump_stack_arch_desc_str);
Tejun Heo3d1cb202013-04-30 15:27:22 -07003236
3237 print_worker_info(log_lvl, current);
Tejun Heo196779b2013-04-30 15:27:12 -07003238}
3239
Tejun Heoa43cb952013-04-30 15:27:17 -07003240/**
3241 * show_regs_print_info - print generic debug info for show_regs()
3242 * @log_lvl: log level
3243 *
3244 * show_regs() implementations can use this function to print out generic
3245 * debug information.
3246 */
3247void show_regs_print_info(const char *log_lvl)
3248{
3249 dump_stack_print_info(log_lvl);
3250
Andy Lutomirski8b70ca62016-07-28 15:48:23 -07003251 printk("%stask: %p task.stack: %p\n",
3252 log_lvl, current, task_stack_page(current));
Tejun Heoa43cb952013-04-30 15:27:17 -07003253}
3254
Joe Perches7ef3d2f2008-02-08 04:21:25 -08003255#endif