blob: c8be5a0f525958ee9f8fbf8d3df371eccc4354fb [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/kernel/printk.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 *
7 * Modified to make sys_syslog() more flexible: added commands to
8 * return the last 4k of kernel messages, regardless of whether
9 * they've been read or not. Added option to suppress kernel printk's
10 * to the console. Added hook for sending the console messages
11 * elsewhere, in preparation for a serial line console (someday).
12 * Ted Ts'o, 2/11/93.
13 * Modified for sysctl support, 1/8/97, Chris Horn.
Jesper Juhl40dc5652005-10-30 15:02:46 -080014 * Fixed SMP synchronization, 08/08/99, Manfred Spraul
Christian Kujau624dffc2006-01-15 02:43:54 +010015 * manfred@colorfullife.com
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * Rewrote bits to get rid of console_lock
Francois Camie1f8e872008-10-15 22:01:59 -070017 * 01Mar01 Andrew Morton
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
19
He Zhedd5adbf2018-09-30 00:45:52 +080020#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
23#include <linux/mm.h>
24#include <linux/tty.h>
25#include <linux/tty_driver.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/console.h>
27#include <linux/init.h>
Randy Dunlapbfe8df32007-10-16 01:23:46 -070028#include <linux/jiffies.h>
29#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
Jan Engelhardt3b9c0412006-06-25 05:48:15 -070031#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/delay.h>
33#include <linux/smp.h>
34#include <linux/security.h>
Mike Travis162a7e72011-05-24 17:13:20 -070035#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/syscalls.h>
Hari Bathini692f66f2017-05-08 15:56:18 -070037#include <linux/crash_core.h>
Jason Wesseld37d39a2010-05-20 21:04:27 -050038#include <linux/kdb.h>
Ingo Molnar3fff4c42009-09-22 16:18:09 +020039#include <linux/ratelimit.h>
Simon Kagstrom456b5652009-10-16 14:09:18 +020040#include <linux/kmsg_dump.h>
Kees Cook00234592010-02-03 15:36:43 -080041#include <linux/syslog.h>
Kevin Cernekee034260d2010-06-03 22:11:25 -070042#include <linux/cpu.h>
Huang Yingfb842b002011-01-12 16:59:43 -080043#include <linux/rculist.h>
Kay Sieverse11fea92012-05-03 02:29:41 +020044#include <linux/poll.h>
Frederic Weisbecker74876a92012-10-12 18:00:23 +020045#include <linux/irq_work.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
Abderrahmane Benbachir58eacff2018-03-22 20:33:28 -040055#include <trace/events/initcall.h>
Johannes Berg95100352011-11-24 20:03:08 +010056#define CREATE_TRACE_POINTS
57#include <trace/events/printk.h>
58
Joe Perchesd197c432013-07-31 13:53:44 -070059#include "console_cmdline.h"
Joe Perchesbbeddf52013-07-31 13:53:45 -070060#include "braille.h"
Petr Mladek42a0bb32016-05-20 17:00:33 -070061#include "internal.h"
Joe Perchesd197c432013-07-31 13:53:44 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063int console_printk[4] = {
Borislav Petkova8fe19e2014-06-04 16:11:46 -070064 CONSOLE_LOGLEVEL_DEFAULT, /* console_loglevel */
Alex Elder42a9dc02014-08-06 16:09:01 -070065 MESSAGE_LOGLEVEL_DEFAULT, /* default_message_loglevel */
Borislav Petkova8fe19e2014-06-04 16:11:46 -070066 CONSOLE_LOGLEVEL_MIN, /* minimum_console_loglevel */
67 CONSOLE_LOGLEVEL_DEFAULT, /* default_console_loglevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
Prarit Bhargavaa1939182019-02-08 19:24:49 +010069EXPORT_SYMBOL_GPL(console_printk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Thomas Zimmermann56e6c102018-07-31 13:06:57 +020071atomic_t ignore_console_lock_warning __read_mostly = ATOMIC_INIT(0);
72EXPORT_SYMBOL(ignore_console_lock_warning);
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/*
Patrick Pletscher0bbfb7c2007-02-17 20:10:16 +010075 * Low level drivers may need that to know if they can schedule in
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 * their unblank() callback or not. So let's export it.
77 */
78int oops_in_progress;
79EXPORT_SYMBOL(oops_in_progress);
80
81/*
82 * console_sem protects the console_drivers list, and also
83 * provides serialisation for access to the entire console
84 * driver system.
85 */
Thomas Gleixner5b8c4f22010-09-07 14:33:43 +000086static DEFINE_SEMAPHORE(console_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087struct console *console_drivers;
Ingo Molnara29d1cf2008-06-02 13:19:08 +020088EXPORT_SYMBOL_GPL(console_drivers);
89
Feng Tangc39ea0b2019-05-14 15:45:34 -070090/*
91 * System may need to suppress printk message under certain
92 * circumstances, like after kernel panic happens.
93 */
94int __read_mostly suppress_printk;
95
Daniel Vetterdaee7792012-09-22 19:52:11 +020096#ifdef CONFIG_LOCKDEP
97static struct lockdep_map console_lock_dep_map = {
98 .name = "console_lock"
99};
100#endif
101
Borislav Petkov750afe72016-08-02 14:04:07 -0700102enum devkmsg_log_bits {
103 __DEVKMSG_LOG_BIT_ON = 0,
104 __DEVKMSG_LOG_BIT_OFF,
105 __DEVKMSG_LOG_BIT_LOCK,
106};
107
108enum devkmsg_log_masks {
109 DEVKMSG_LOG_MASK_ON = BIT(__DEVKMSG_LOG_BIT_ON),
110 DEVKMSG_LOG_MASK_OFF = BIT(__DEVKMSG_LOG_BIT_OFF),
111 DEVKMSG_LOG_MASK_LOCK = BIT(__DEVKMSG_LOG_BIT_LOCK),
112};
113
114/* Keep both the 'on' and 'off' bits clear, i.e. ratelimit by default: */
115#define DEVKMSG_LOG_MASK_DEFAULT 0
116
117static unsigned int __read_mostly devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
118
119static int __control_devkmsg(char *str)
120{
Chuhong Yuan35c35492019-08-09 15:10:34 +0800121 size_t len;
122
Borislav Petkov750afe72016-08-02 14:04:07 -0700123 if (!str)
124 return -EINVAL;
125
Chuhong Yuan35c35492019-08-09 15:10:34 +0800126 len = str_has_prefix(str, "on");
127 if (len) {
Borislav Petkov750afe72016-08-02 14:04:07 -0700128 devkmsg_log = DEVKMSG_LOG_MASK_ON;
Chuhong Yuan35c35492019-08-09 15:10:34 +0800129 return len;
Borislav Petkov750afe72016-08-02 14:04:07 -0700130 }
Chuhong Yuan35c35492019-08-09 15:10:34 +0800131
132 len = str_has_prefix(str, "off");
133 if (len) {
134 devkmsg_log = DEVKMSG_LOG_MASK_OFF;
135 return len;
136 }
137
138 len = str_has_prefix(str, "ratelimit");
139 if (len) {
140 devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
141 return len;
142 }
143
Borislav Petkov750afe72016-08-02 14:04:07 -0700144 return -EINVAL;
145}
146
147static int __init control_devkmsg(char *str)
148{
149 if (__control_devkmsg(str) < 0)
150 return 1;
151
152 /*
153 * Set sysctl string accordingly:
154 */
Sergey Senozhatsky6fd78a12018-01-19 13:39:01 +0900155 if (devkmsg_log == DEVKMSG_LOG_MASK_ON)
156 strcpy(devkmsg_log_str, "on");
157 else if (devkmsg_log == DEVKMSG_LOG_MASK_OFF)
158 strcpy(devkmsg_log_str, "off");
Borislav Petkov750afe72016-08-02 14:04:07 -0700159 /* else "ratelimit" which is set by default. */
160
161 /*
162 * Sysctl cannot change it anymore. The kernel command line setting of
163 * this parameter is to force the setting to be permanent throughout the
164 * runtime of the system. This is a precation measure against userspace
165 * trying to be a smarta** and attempting to change it up on us.
166 */
167 devkmsg_log |= DEVKMSG_LOG_MASK_LOCK;
168
169 return 0;
170}
171__setup("printk.devkmsg=", control_devkmsg);
172
173char devkmsg_log_str[DEVKMSG_STR_MAX_SIZE] = "ratelimit";
174
175int devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write,
176 void __user *buffer, size_t *lenp, loff_t *ppos)
177{
178 char old_str[DEVKMSG_STR_MAX_SIZE];
179 unsigned int old;
180 int err;
181
182 if (write) {
183 if (devkmsg_log & DEVKMSG_LOG_MASK_LOCK)
184 return -EINVAL;
185
186 old = devkmsg_log;
187 strncpy(old_str, devkmsg_log_str, DEVKMSG_STR_MAX_SIZE);
188 }
189
190 err = proc_dostring(table, write, buffer, lenp, ppos);
191 if (err)
192 return err;
193
194 if (write) {
195 err = __control_devkmsg(devkmsg_log_str);
196
197 /*
198 * Do not accept an unknown string OR a known string with
199 * trailing crap...
200 */
201 if (err < 0 || (err + 1 != *lenp)) {
202
203 /* ... and restore old setting. */
204 devkmsg_log = old;
205 strncpy(devkmsg_log_str, old_str, DEVKMSG_STR_MAX_SIZE);
206
207 return -EINVAL;
208 }
209 }
210
211 return 0;
212}
213
Sergey Senozhatsky96278082018-10-02 11:38:34 +0900214/* Number of registered extended console drivers. */
Tejun Heo6fe29352015-06-25 15:01:30 -0700215static int nr_ext_console_drivers;
216
217/*
Jan Karabd8d7cf2014-06-04 16:11:36 -0700218 * Helper macros to handle lockdep when locking/unlocking console_sem. We use
219 * macros instead of functions so that _RET_IP_ contains useful information.
220 */
221#define down_console_sem() do { \
222 down(&console_sem);\
223 mutex_acquire(&console_lock_dep_map, 0, 0, _RET_IP_);\
224} while (0)
225
226static int __down_trylock_console_sem(unsigned long ip)
227{
Sergey Senozhatskyf9752372016-12-27 23:16:09 +0900228 int lock_failed;
229 unsigned long flags;
230
231 /*
232 * Here and in __up_console_sem() we need to be in safe mode,
233 * because spindump/WARN/etc from under console ->lock will
234 * deadlock in printk()->down_trylock_console_sem() otherwise.
235 */
236 printk_safe_enter_irqsave(flags);
237 lock_failed = down_trylock(&console_sem);
238 printk_safe_exit_irqrestore(flags);
239
240 if (lock_failed)
Jan Karabd8d7cf2014-06-04 16:11:36 -0700241 return 1;
242 mutex_acquire(&console_lock_dep_map, 0, 1, ip);
243 return 0;
244}
245#define down_trylock_console_sem() __down_trylock_console_sem(_RET_IP_)
246
Sergey Senozhatskyf9752372016-12-27 23:16:09 +0900247static void __up_console_sem(unsigned long ip)
248{
249 unsigned long flags;
250
Qian Cai5facae42019-09-19 12:09:40 -0400251 mutex_release(&console_lock_dep_map, ip);
Sergey Senozhatskyf9752372016-12-27 23:16:09 +0900252
253 printk_safe_enter_irqsave(flags);
254 up(&console_sem);
255 printk_safe_exit_irqrestore(flags);
256}
257#define up_console_sem() __up_console_sem(_RET_IP_)
Jan Karabd8d7cf2014-06-04 16:11:36 -0700258
259/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 * This is used for debugging the mess that is the VT code by
261 * keeping track if we have the console semaphore held. It's
262 * definitely not the perfect debug tool (we don't know if _WE_
Alex Elder0b90fec2014-08-06 16:09:03 -0700263 * hold it and are racing, but it helps tracking those weird code
264 * paths in the console code where we end up in places I want
265 * locked without the console sempahore held).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 */
Linus Torvalds557240b2006-06-19 18:16:01 -0700267static int console_locked, console_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269/*
Feng Tangfe3d8ad2011-03-22 16:34:21 -0700270 * If exclusive_console is non-NULL then only this console is to be printed to.
271 */
272static struct console *exclusive_console;
273
274/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 * Array of consoles built from command line options (console=)
276 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278#define MAX_CMDLINECONSOLES 8
279
280static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
Joe Perchesd197c432013-07-31 13:53:44 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282static int preferred_console = -1;
Markus Armbruster9e124fe2008-05-26 23:31:07 +0100283int console_set_on_cmdline;
284EXPORT_SYMBOL(console_set_on_cmdline);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286/* Flag: console code may call schedule() */
287static int console_may_schedule;
288
Sergey Senozhatskycca10d52017-12-21 14:41:49 +0900289enum con_msg_format_flags {
290 MSG_FORMAT_DEFAULT = 0,
291 MSG_FORMAT_SYSLOG = (1 << 0),
292};
293
294static int console_msg_format = MSG_FORMAT_DEFAULT;
295
Kay Sievers7ff95542012-05-03 02:29:13 +0200296/*
297 * The printk log buffer consists of a chain of concatenated variable
298 * length records. Every record starts with a record header, containing
299 * the overall length of the record.
300 *
301 * The heads to the first and last entry in the buffer, as well as the
Alex Elder0b90fec2014-08-06 16:09:03 -0700302 * sequence numbers of these entries are maintained when messages are
303 * stored.
Kay Sievers7ff95542012-05-03 02:29:13 +0200304 *
305 * If the heads indicate available messages, the length in the header
306 * tells the start next message. A length == 0 for the next message
307 * indicates a wrap-around to the beginning of the buffer.
308 *
309 * Every record carries the monotonic timestamp in microseconds, as well as
310 * the standard userspace syslog level and syslog facility. The usual
311 * kernel messages use LOG_KERN; userspace-injected messages always carry
312 * a matching syslog facility, by default LOG_USER. The origin of every
313 * message can be reliably determined that way.
314 *
315 * The human readable log message directly follows the message header. The
316 * length of the message text is stored in the header, the stored message
317 * is not terminated.
318 *
Kay Sieverse11fea92012-05-03 02:29:41 +0200319 * Optionally, a message can carry a dictionary of properties (key/value pairs),
320 * to provide userspace with a machine-readable message context.
321 *
322 * Examples for well-defined, commonly used property names are:
323 * DEVICE=b12:8 device identifier
324 * b12:8 block dev_t
325 * c127:3 char dev_t
326 * n8 netdev ifindex
327 * +sound:card0 subsystem:devname
328 * SUBSYSTEM=pci driver-core subsystem name
329 *
330 * Valid characters in property names are [a-zA-Z0-9.-_]. The plain text value
331 * follows directly after a '=' character. Every property is terminated by
332 * a '\0' character. The last property is not terminated.
333 *
334 * Example of a message structure:
335 * 0000 ff 8f 00 00 00 00 00 00 monotonic time in nsec
336 * 0008 34 00 record is 52 bytes long
337 * 000a 0b 00 text is 11 bytes long
338 * 000c 1f 00 dictionary is 23 bytes long
339 * 000e 03 00 LOG_KERN (facility) LOG_ERR (level)
340 * 0010 69 74 27 73 20 61 20 6c "it's a l"
341 * 69 6e 65 "ine"
342 * 001b 44 45 56 49 43 "DEVIC"
343 * 45 3d 62 38 3a 32 00 44 "E=b8:2\0D"
344 * 52 49 56 45 52 3d 62 75 "RIVER=bu"
345 * 67 "g"
346 * 0032 00 00 00 padding to next message header
347 *
Joe Perches62e32ac2013-07-31 13:53:47 -0700348 * The 'struct printk_log' buffer header must never be directly exported to
Kay Sieverse11fea92012-05-03 02:29:41 +0200349 * userspace, it is a kernel-private implementation detail that might
350 * need to be changed in the future, when the requirements change.
351 *
352 * /dev/kmsg exports the structured data in the following line format:
Antonio Ospiteb3896452015-06-30 14:59:03 -0700353 * "<level>,<sequnum>,<timestamp>,<contflag>[,additional_values, ... ];<message text>\n"
354 *
355 * Users of the export format should ignore possible additional values
356 * separated by ',', and find the message after the ';' character.
Kay Sieverse11fea92012-05-03 02:29:41 +0200357 *
358 * The optional key/value pairs are attached as continuation lines starting
359 * with a space character and terminated by a newline. All possible
360 * non-prinatable characters are escaped in the "\xff" notation.
Kay Sievers7ff95542012-05-03 02:29:13 +0200361 */
Matt Mackalld59745c2005-05-01 08:59:02 -0700362
Kay Sievers084681d2012-06-28 09:38:53 +0200363enum log_flags {
Kay Sievers5becfb12012-07-09 12:15:42 -0700364 LOG_NEWLINE = 2, /* text ended with a newline */
Kay Sievers5becfb12012-07-09 12:15:42 -0700365 LOG_CONT = 8, /* text is a fragment of a continuation line */
Kay Sievers084681d2012-06-28 09:38:53 +0200366};
367
Joe Perches62e32ac2013-07-31 13:53:47 -0700368struct printk_log {
Kay Sievers7ff95542012-05-03 02:29:13 +0200369 u64 ts_nsec; /* timestamp in nanoseconds */
370 u16 len; /* length of entire record */
371 u16 text_len; /* length of text buffer */
372 u16 dict_len; /* length of dictionary buffer */
Kay Sievers084681d2012-06-28 09:38:53 +0200373 u8 facility; /* syslog facility */
374 u8 flags:5; /* internal record flags */
375 u8 level:3; /* syslog level */
Tetsuo Handa15ff2062018-12-18 06:05:04 +0900376#ifdef CONFIG_PRINTK_CALLER
377 u32 caller_id; /* thread id or processor id */
378#endif
Andrey Ryabinin5c9cf8a2016-01-20 15:00:48 -0800379}
380#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
381__packed __aligned(4)
382#endif
383;
Kay Sievers7ff95542012-05-03 02:29:13 +0200384
385/*
Steven Rostedt458df9f2014-06-04 16:11:38 -0700386 * The logbuf_lock protects kmsg buffer, indices, counters. This can be taken
387 * within the scheduler's rq lock. It must be released before calling
388 * console_unlock() or anything else that might wake up a process.
Kay Sievers7ff95542012-05-03 02:29:13 +0200389 */
Petr Mladekcf9b1102016-05-20 17:00:42 -0700390DEFINE_RAW_SPINLOCK(logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +0200391
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +0900392/*
393 * Helper macros to lock/unlock logbuf_lock and switch between
394 * printk-safe/unsafe modes.
395 */
396#define logbuf_lock_irq() \
397 do { \
398 printk_safe_enter_irq(); \
399 raw_spin_lock(&logbuf_lock); \
400 } while (0)
401
402#define logbuf_unlock_irq() \
403 do { \
404 raw_spin_unlock(&logbuf_lock); \
405 printk_safe_exit_irq(); \
406 } while (0)
407
408#define logbuf_lock_irqsave(flags) \
409 do { \
410 printk_safe_enter_irqsave(flags); \
411 raw_spin_lock(&logbuf_lock); \
412 } while (0)
413
414#define logbuf_unlock_irqrestore(flags) \
415 do { \
416 raw_spin_unlock(&logbuf_lock); \
417 printk_safe_exit_irqrestore(flags); \
418 } while (0)
419
Kay Sievers96efedf2012-07-16 18:35:29 -0700420#ifdef CONFIG_PRINTK
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -0700421DECLARE_WAIT_QUEUE_HEAD(log_wait);
Kay Sievers7f3a7812012-05-09 01:37:51 +0200422/* the next printk record to read by syslog(READ) or /proc/kmsg */
423static u64 syslog_seq;
424static u32 syslog_idx;
Kay Sieverseb02dac2012-07-09 10:05:10 -0700425static size_t syslog_partial;
Tetsuo Handae80c1a92018-12-04 19:00:01 +0900426static bool syslog_time;
Kay Sievers7f3a7812012-05-09 01:37:51 +0200427
428/* index and sequence number of the first record stored in the buffer */
429static u64 log_first_seq;
430static u32 log_first_idx;
431
432/* index and sequence number of the next record to store in the buffer */
433static u64 log_next_seq;
Kay Sievers7f3a7812012-05-09 01:37:51 +0200434static u32 log_next_idx;
435
Kay Sieverseab07262012-07-16 18:35:30 -0700436/* the next printk record to write to the console */
437static u64 console_seq;
438static u32 console_idx;
Petr Mladekf92b0702018-09-13 14:34:06 +0200439static u64 exclusive_console_stop_seq;
Kay Sieverseab07262012-07-16 18:35:30 -0700440
Kay Sievers7f3a7812012-05-09 01:37:51 +0200441/* the next printk record to read after the last 'clear' command */
442static u64 clear_seq;
443static u32 clear_idx;
Kay Sievers7ff95542012-05-03 02:29:13 +0200444
Tetsuo Handa15ff2062018-12-18 06:05:04 +0900445#ifdef CONFIG_PRINTK_CALLER
446#define PREFIX_MAX 48
447#else
Kay Sievers70498252012-07-16 18:35:29 -0700448#define PREFIX_MAX 32
Tetsuo Handa15ff2062018-12-18 06:05:04 +0900449#endif
Alex Elder249771b2014-08-06 16:09:08 -0700450#define LOG_LINE_MAX (1024 - PREFIX_MAX)
Kay Sievers7ff95542012-05-03 02:29:13 +0200451
Mathias Krause38246572015-11-06 16:30:38 -0800452#define LOG_LEVEL(v) ((v) & 0x07)
453#define LOG_FACILITY(v) ((v) >> 3 & 0xff)
454
Kay Sievers7ff95542012-05-03 02:29:13 +0200455/* record buffer */
Joe Perches62e32ac2013-07-31 13:53:47 -0700456#define LOG_ALIGN __alignof__(struct printk_log)
Kay Sievers7ff95542012-05-03 02:29:13 +0200457#define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
He Zhee6fe3e52018-09-30 00:45:53 +0800458#define LOG_BUF_LEN_MAX (u32)(1 << 31)
Stephen Warrenf8450fc2012-05-10 16:14:33 -0600459static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN);
Matt Mackalld59745c2005-05-01 08:59:02 -0700460static char *log_buf = __log_buf;
Kay Sievers7ff95542012-05-03 02:29:13 +0200461static u32 log_buf_len = __LOG_BUF_LEN;
462
Vasant Hegde14c40002014-08-09 11:15:30 +0530463/* Return log buffer address */
464char *log_buf_addr_get(void)
465{
466 return log_buf;
467}
468
469/* Return log buffer size */
470u32 log_buf_len_get(void)
471{
472 return log_buf_len;
473}
474
Kay Sievers7ff95542012-05-03 02:29:13 +0200475/* human readable text of the record */
Joe Perches62e32ac2013-07-31 13:53:47 -0700476static char *log_text(const struct printk_log *msg)
Kay Sievers7ff95542012-05-03 02:29:13 +0200477{
Joe Perches62e32ac2013-07-31 13:53:47 -0700478 return (char *)msg + sizeof(struct printk_log);
Kay Sievers7ff95542012-05-03 02:29:13 +0200479}
480
481/* optional key/value pair dictionary attached to the record */
Joe Perches62e32ac2013-07-31 13:53:47 -0700482static char *log_dict(const struct printk_log *msg)
Kay Sievers7ff95542012-05-03 02:29:13 +0200483{
Joe Perches62e32ac2013-07-31 13:53:47 -0700484 return (char *)msg + sizeof(struct printk_log) + msg->text_len;
Kay Sievers7ff95542012-05-03 02:29:13 +0200485}
486
487/* get record by index; idx must point to valid msg */
Joe Perches62e32ac2013-07-31 13:53:47 -0700488static struct printk_log *log_from_idx(u32 idx)
Kay Sievers7ff95542012-05-03 02:29:13 +0200489{
Joe Perches62e32ac2013-07-31 13:53:47 -0700490 struct printk_log *msg = (struct printk_log *)(log_buf + idx);
Kay Sievers7ff95542012-05-03 02:29:13 +0200491
492 /*
493 * A length == 0 record is the end of buffer marker. Wrap around and
494 * read the message at the start of the buffer.
495 */
496 if (!msg->len)
Joe Perches62e32ac2013-07-31 13:53:47 -0700497 return (struct printk_log *)log_buf;
Kay Sievers7ff95542012-05-03 02:29:13 +0200498 return msg;
499}
500
501/* get next record; idx must point to valid msg */
502static u32 log_next(u32 idx)
503{
Joe Perches62e32ac2013-07-31 13:53:47 -0700504 struct printk_log *msg = (struct printk_log *)(log_buf + idx);
Kay Sievers7ff95542012-05-03 02:29:13 +0200505
506 /* length == 0 indicates the end of the buffer; wrap */
507 /*
508 * A length == 0 record is the end of buffer marker. Wrap around and
509 * read the message at the start of the buffer as *this* one, and
510 * return the one after that.
511 */
512 if (!msg->len) {
Joe Perches62e32ac2013-07-31 13:53:47 -0700513 msg = (struct printk_log *)log_buf;
Kay Sievers7ff95542012-05-03 02:29:13 +0200514 return msg->len;
515 }
516 return idx + msg->len;
517}
518
Petr Mladekf40e4b92014-06-04 16:11:30 -0700519/*
520 * Check whether there is enough free space for the given message.
521 *
522 * The same values of first_idx and next_idx mean that the buffer
523 * is either empty or full.
524 *
525 * If the buffer is empty, we must respect the position of the indexes.
526 * They cannot be reset to the beginning of the buffer.
527 */
528static int logbuf_has_space(u32 msg_size, bool empty)
Petr Mladek0a581692014-06-04 16:11:28 -0700529{
530 u32 free;
531
Petr Mladekf40e4b92014-06-04 16:11:30 -0700532 if (log_next_idx > log_first_idx || empty)
Petr Mladek0a581692014-06-04 16:11:28 -0700533 free = max(log_buf_len - log_next_idx, log_first_idx);
534 else
535 free = log_first_idx - log_next_idx;
536
537 /*
538 * We need space also for an empty header that signalizes wrapping
539 * of the buffer.
540 */
541 return free >= msg_size + sizeof(struct printk_log);
542}
543
Petr Mladekf40e4b92014-06-04 16:11:30 -0700544static int log_make_free_space(u32 msg_size)
Petr Mladek0a581692014-06-04 16:11:28 -0700545{
Ivan Delalandef4689082016-03-17 14:21:30 -0700546 while (log_first_seq < log_next_seq &&
547 !logbuf_has_space(msg_size, false)) {
Alex Elder0b90fec2014-08-06 16:09:03 -0700548 /* drop old messages until we have enough contiguous space */
Petr Mladek0a581692014-06-04 16:11:28 -0700549 log_first_idx = log_next(log_first_idx);
550 log_first_seq++;
551 }
Petr Mladekf40e4b92014-06-04 16:11:30 -0700552
Ivan Delalandef4689082016-03-17 14:21:30 -0700553 if (clear_seq < log_first_seq) {
554 clear_seq = log_first_seq;
555 clear_idx = log_first_idx;
556 }
557
Petr Mladekf40e4b92014-06-04 16:11:30 -0700558 /* sequence numbers are equal, so the log buffer is empty */
Ivan Delalandef4689082016-03-17 14:21:30 -0700559 if (logbuf_has_space(msg_size, log_first_seq == log_next_seq))
Petr Mladekf40e4b92014-06-04 16:11:30 -0700560 return 0;
561
562 return -ENOMEM;
Petr Mladek0a581692014-06-04 16:11:28 -0700563}
564
Petr Mladek85c870432014-06-04 16:11:31 -0700565/* compute the message size including the padding bytes */
566static u32 msg_used_size(u16 text_len, u16 dict_len, u32 *pad_len)
567{
568 u32 size;
569
570 size = sizeof(struct printk_log) + text_len + dict_len;
571 *pad_len = (-size) & (LOG_ALIGN - 1);
572 size += *pad_len;
573
574 return size;
575}
576
Petr Mladek55bd53a2014-06-04 16:11:32 -0700577/*
578 * Define how much of the log buffer we could take at maximum. The value
579 * must be greater than two. Note that only half of the buffer is available
580 * when the index points to the middle.
581 */
582#define MAX_LOG_TAKE_PART 4
583static const char trunc_msg[] = "<truncated>";
584
585static u32 truncate_msg(u16 *text_len, u16 *trunc_msg_len,
586 u16 *dict_len, u32 *pad_len)
587{
588 /*
589 * The message should not take the whole buffer. Otherwise, it might
590 * get removed too soon.
591 */
592 u32 max_text_len = log_buf_len / MAX_LOG_TAKE_PART;
593 if (*text_len > max_text_len)
594 *text_len = max_text_len;
595 /* enable the warning message */
596 *trunc_msg_len = strlen(trunc_msg);
597 /* disable the "dict" completely */
598 *dict_len = 0;
599 /* compute the size again, count also the warning message */
600 return msg_used_size(*text_len + *trunc_msg_len, 0, pad_len);
601}
602
Kay Sievers7ff95542012-05-03 02:29:13 +0200603/* insert record into the buffer, discard old ones, update heads */
Tetsuo Handacbae05d2019-02-16 19:59:33 +0900604static int log_store(u32 caller_id, int facility, int level,
Petr Mladek034633c2014-06-04 16:11:33 -0700605 enum log_flags flags, u64 ts_nsec,
606 const char *dict, u16 dict_len,
607 const char *text, u16 text_len)
Kay Sievers7ff95542012-05-03 02:29:13 +0200608{
Joe Perches62e32ac2013-07-31 13:53:47 -0700609 struct printk_log *msg;
Kay Sievers7ff95542012-05-03 02:29:13 +0200610 u32 size, pad_len;
Petr Mladek55bd53a2014-06-04 16:11:32 -0700611 u16 trunc_msg_len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +0200612
613 /* number of '\0' padding bytes to next message */
Petr Mladek85c870432014-06-04 16:11:31 -0700614 size = msg_used_size(text_len, dict_len, &pad_len);
Kay Sievers7ff95542012-05-03 02:29:13 +0200615
Petr Mladek55bd53a2014-06-04 16:11:32 -0700616 if (log_make_free_space(size)) {
617 /* truncate the message if it is too long for empty buffer */
618 size = truncate_msg(&text_len, &trunc_msg_len,
619 &dict_len, &pad_len);
620 /* survive when the log buffer is too small for trunc_msg */
621 if (log_make_free_space(size))
Petr Mladek034633c2014-06-04 16:11:33 -0700622 return 0;
Petr Mladek55bd53a2014-06-04 16:11:32 -0700623 }
Kay Sievers7ff95542012-05-03 02:29:13 +0200624
Petr Mladek39b25102014-04-03 14:48:42 -0700625 if (log_next_idx + size + sizeof(struct printk_log) > log_buf_len) {
Kay Sievers7ff95542012-05-03 02:29:13 +0200626 /*
627 * This message + an additional empty header does not fit
628 * at the end of the buffer. Add an empty header with len == 0
629 * to signify a wrap around.
630 */
Joe Perches62e32ac2013-07-31 13:53:47 -0700631 memset(log_buf + log_next_idx, 0, sizeof(struct printk_log));
Kay Sievers7ff95542012-05-03 02:29:13 +0200632 log_next_idx = 0;
633 }
634
635 /* fill message */
Joe Perches62e32ac2013-07-31 13:53:47 -0700636 msg = (struct printk_log *)(log_buf + log_next_idx);
Kay Sievers7ff95542012-05-03 02:29:13 +0200637 memcpy(log_text(msg), text, text_len);
638 msg->text_len = text_len;
Petr Mladek55bd53a2014-06-04 16:11:32 -0700639 if (trunc_msg_len) {
640 memcpy(log_text(msg) + text_len, trunc_msg, trunc_msg_len);
641 msg->text_len += trunc_msg_len;
642 }
Kay Sievers7ff95542012-05-03 02:29:13 +0200643 memcpy(log_dict(msg), dict, dict_len);
644 msg->dict_len = dict_len;
Kay Sievers084681d2012-06-28 09:38:53 +0200645 msg->facility = facility;
646 msg->level = level & 7;
647 msg->flags = flags & 0x1f;
648 if (ts_nsec > 0)
649 msg->ts_nsec = ts_nsec;
650 else
651 msg->ts_nsec = local_clock();
Tetsuo Handa15ff2062018-12-18 06:05:04 +0900652#ifdef CONFIG_PRINTK_CALLER
Tetsuo Handacbae05d2019-02-16 19:59:33 +0900653 msg->caller_id = caller_id;
Tetsuo Handa15ff2062018-12-18 06:05:04 +0900654#endif
Kay Sievers7ff95542012-05-03 02:29:13 +0200655 memset(log_dict(msg) + dict_len, 0, pad_len);
Petr Mladekfce6e032014-04-03 14:48:43 -0700656 msg->len = size;
Kay Sievers7ff95542012-05-03 02:29:13 +0200657
658 /* insert message */
659 log_next_idx += msg->len;
660 log_next_seq++;
Petr Mladek034633c2014-06-04 16:11:33 -0700661
662 return msg->text_len;
Kay Sievers7ff95542012-05-03 02:29:13 +0200663}
Matt Mackalld59745c2005-05-01 08:59:02 -0700664
Alex Eldere99aa462014-08-06 16:09:05 -0700665int dmesg_restrict = IS_ENABLED(CONFIG_SECURITY_DMESG_RESTRICT);
Kees Cook637241a2013-06-12 14:04:39 -0700666
667static int syslog_action_restricted(int type)
668{
669 if (dmesg_restrict)
670 return 1;
671 /*
672 * Unless restricted, we allow "read all" and "get buffer size"
673 * for everybody.
674 */
675 return type != SYSLOG_ACTION_READ_ALL &&
676 type != SYSLOG_ACTION_SIZE_BUFFER;
677}
678
Kees Cookc71b02e2017-08-09 21:11:00 -0700679static int check_syslog_permissions(int type, int source)
Kees Cook637241a2013-06-12 14:04:39 -0700680{
681 /*
682 * If this is from /proc/kmsg and we've already opened it, then we've
683 * already done the capabilities checks at open time.
684 */
Vasily Averin3ea43312015-06-25 15:01:47 -0700685 if (source == SYSLOG_FROM_PROC && type != SYSLOG_ACTION_OPEN)
Vasily Averind194e5d2015-06-25 15:01:44 -0700686 goto ok;
Kees Cook637241a2013-06-12 14:04:39 -0700687
688 if (syslog_action_restricted(type)) {
689 if (capable(CAP_SYSLOG))
Vasily Averind194e5d2015-06-25 15:01:44 -0700690 goto ok;
Kees Cook637241a2013-06-12 14:04:39 -0700691 /*
692 * For historical reasons, accept CAP_SYS_ADMIN too, with
693 * a warning.
694 */
695 if (capable(CAP_SYS_ADMIN)) {
696 pr_warn_once("%s (%d): Attempt to access syslog with "
697 "CAP_SYS_ADMIN but no CAP_SYSLOG "
698 "(deprecated).\n",
699 current->comm, task_pid_nr(current));
Vasily Averind194e5d2015-06-25 15:01:44 -0700700 goto ok;
Kees Cook637241a2013-06-12 14:04:39 -0700701 }
702 return -EPERM;
703 }
Vasily Averind194e5d2015-06-25 15:01:44 -0700704ok:
Kees Cook637241a2013-06-12 14:04:39 -0700705 return security_syslog(type);
706}
707
Tejun Heod43ff432015-06-25 15:01:24 -0700708static void append_char(char **pp, char *e, char c)
709{
710 if (*pp < e)
711 *(*pp)++ = c;
712}
Kees Cook637241a2013-06-12 14:04:39 -0700713
Tejun Heo0a295e672015-06-25 15:01:27 -0700714static ssize_t msg_print_ext_header(char *buf, size_t size,
Linus Torvalds5aa068e2016-10-25 11:27:31 -0700715 struct printk_log *msg, u64 seq)
Tejun Heo0a295e672015-06-25 15:01:27 -0700716{
717 u64 ts_usec = msg->ts_nsec;
Tetsuo Handa15ff2062018-12-18 06:05:04 +0900718 char caller[20];
719#ifdef CONFIG_PRINTK_CALLER
720 u32 id = msg->caller_id;
721
722 snprintf(caller, sizeof(caller), ",caller=%c%u",
723 id & 0x80000000 ? 'C' : 'T', id & ~0x80000000);
724#else
725 caller[0] = '\0';
726#endif
Tejun Heo0a295e672015-06-25 15:01:27 -0700727
728 do_div(ts_usec, 1000);
729
Tetsuo Handa15ff2062018-12-18 06:05:04 +0900730 return scnprintf(buf, size, "%u,%llu,%llu,%c%s;",
731 (msg->facility << 3) | msg->level, seq, ts_usec,
732 msg->flags & LOG_CONT ? 'c' : '-', caller);
Tejun Heo0a295e672015-06-25 15:01:27 -0700733}
734
735static ssize_t msg_print_ext_body(char *buf, size_t size,
736 char *dict, size_t dict_len,
737 char *text, size_t text_len)
738{
739 char *p = buf, *e = buf + size;
740 size_t i;
741
742 /* escape non-printable characters */
743 for (i = 0; i < text_len; i++) {
744 unsigned char c = text[i];
745
746 if (c < ' ' || c >= 127 || c == '\\')
747 p += scnprintf(p, e - p, "\\x%02x", c);
748 else
749 append_char(&p, e, c);
750 }
751 append_char(&p, e, '\n');
752
753 if (dict_len) {
754 bool line = true;
755
756 for (i = 0; i < dict_len; i++) {
757 unsigned char c = dict[i];
758
759 if (line) {
760 append_char(&p, e, ' ');
761 line = false;
762 }
763
764 if (c == '\0') {
765 append_char(&p, e, '\n');
766 line = true;
767 continue;
768 }
769
770 if (c < ' ' || c >= 127 || c == '\\') {
771 p += scnprintf(p, e - p, "\\x%02x", c);
772 continue;
773 }
774
775 append_char(&p, e, c);
776 }
777 append_char(&p, e, '\n');
778 }
779
780 return p - buf;
781}
782
Kay Sieverse11fea92012-05-03 02:29:41 +0200783/* /dev/kmsg - userspace message inject/listen interface */
784struct devkmsg_user {
785 u64 seq;
786 u32 idx;
Borislav Petkov750afe72016-08-02 14:04:07 -0700787 struct ratelimit_state rs;
Kay Sieverse11fea92012-05-03 02:29:41 +0200788 struct mutex lock;
Tejun Heod43ff432015-06-25 15:01:24 -0700789 char buf[CONSOLE_EXT_LOG_MAX];
Kay Sieverse11fea92012-05-03 02:29:41 +0200790};
791
Tetsuo Handa9adcfaf2018-11-24 13:10:25 +0900792static __printf(3, 4) __cold
793int devkmsg_emit(int facility, int level, const char *fmt, ...)
794{
795 va_list args;
796 int r;
797
798 va_start(args, fmt);
799 r = vprintk_emit(facility, level, NULL, 0, fmt, args);
800 va_end(args);
801
802 return r;
803}
804
Al Viro849f3122014-08-23 12:23:53 -0400805static ssize_t devkmsg_write(struct kiocb *iocb, struct iov_iter *from)
Kay Sieverse11fea92012-05-03 02:29:41 +0200806{
807 char *buf, *line;
Kay Sieverse11fea92012-05-03 02:29:41 +0200808 int level = default_message_loglevel;
809 int facility = 1; /* LOG_USER */
Borislav Petkov750afe72016-08-02 14:04:07 -0700810 struct file *file = iocb->ki_filp;
811 struct devkmsg_user *user = file->private_data;
Christoph Hellwig66ee59a2015-02-11 19:56:46 +0100812 size_t len = iov_iter_count(from);
Kay Sieverse11fea92012-05-03 02:29:41 +0200813 ssize_t ret = len;
814
Borislav Petkov750afe72016-08-02 14:04:07 -0700815 if (!user || len > LOG_LINE_MAX)
Kay Sieverse11fea92012-05-03 02:29:41 +0200816 return -EINVAL;
Borislav Petkov750afe72016-08-02 14:04:07 -0700817
818 /* Ignore when user logging is disabled. */
819 if (devkmsg_log & DEVKMSG_LOG_MASK_OFF)
820 return len;
821
822 /* Ratelimit when not explicitly enabled. */
823 if (!(devkmsg_log & DEVKMSG_LOG_MASK_ON)) {
824 if (!___ratelimit(&user->rs, current->comm))
825 return ret;
826 }
827
Kay Sieverse11fea92012-05-03 02:29:41 +0200828 buf = kmalloc(len+1, GFP_KERNEL);
829 if (buf == NULL)
830 return -ENOMEM;
831
Al Viro849f3122014-08-23 12:23:53 -0400832 buf[len] = '\0';
Al Virocbbd26b2016-11-01 22:09:04 -0400833 if (!copy_from_iter_full(buf, len, from)) {
Al Viro849f3122014-08-23 12:23:53 -0400834 kfree(buf);
835 return -EFAULT;
Kay Sieverse11fea92012-05-03 02:29:41 +0200836 }
837
838 /*
839 * Extract and skip the syslog prefix <[0-9]*>. Coming from userspace
840 * the decimal value represents 32bit, the lower 3 bit are the log
841 * level, the rest are the log facility.
842 *
843 * If no prefix or no userspace facility is specified, we
844 * enforce LOG_USER, to be able to reliably distinguish
845 * kernel-generated messages from userspace-injected ones.
846 */
847 line = buf;
848 if (line[0] == '<') {
849 char *endp = NULL;
Mathias Krause38246572015-11-06 16:30:38 -0800850 unsigned int u;
Kay Sieverse11fea92012-05-03 02:29:41 +0200851
Mathias Krause38246572015-11-06 16:30:38 -0800852 u = simple_strtoul(line + 1, &endp, 10);
Kay Sieverse11fea92012-05-03 02:29:41 +0200853 if (endp && endp[0] == '>') {
Mathias Krause38246572015-11-06 16:30:38 -0800854 level = LOG_LEVEL(u);
855 if (LOG_FACILITY(u) != 0)
856 facility = LOG_FACILITY(u);
Kay Sieverse11fea92012-05-03 02:29:41 +0200857 endp++;
858 len -= endp - line;
859 line = endp;
860 }
861 }
Kay Sieverse11fea92012-05-03 02:29:41 +0200862
Tetsuo Handa9adcfaf2018-11-24 13:10:25 +0900863 devkmsg_emit(facility, level, "%s", line);
Kay Sieverse11fea92012-05-03 02:29:41 +0200864 kfree(buf);
865 return ret;
866}
867
868static ssize_t devkmsg_read(struct file *file, char __user *buf,
869 size_t count, loff_t *ppos)
870{
871 struct devkmsg_user *user = file->private_data;
Joe Perches62e32ac2013-07-31 13:53:47 -0700872 struct printk_log *msg;
Kay Sieverse11fea92012-05-03 02:29:41 +0200873 size_t len;
874 ssize_t ret;
875
876 if (!user)
877 return -EBADF;
878
Yuanhan Liu4a77a5a2012-06-16 21:21:51 +0800879 ret = mutex_lock_interruptible(&user->lock);
880 if (ret)
881 return ret;
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +0900882
883 logbuf_lock_irq();
Kay Sieverse11fea92012-05-03 02:29:41 +0200884 while (user->seq == log_next_seq) {
885 if (file->f_flags & O_NONBLOCK) {
886 ret = -EAGAIN;
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +0900887 logbuf_unlock_irq();
Kay Sieverse11fea92012-05-03 02:29:41 +0200888 goto out;
889 }
890
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +0900891 logbuf_unlock_irq();
Kay Sieverse11fea92012-05-03 02:29:41 +0200892 ret = wait_event_interruptible(log_wait,
893 user->seq != log_next_seq);
894 if (ret)
895 goto out;
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +0900896 logbuf_lock_irq();
Kay Sieverse11fea92012-05-03 02:29:41 +0200897 }
898
899 if (user->seq < log_first_seq) {
900 /* our last seen message is gone, return error and reset */
901 user->idx = log_first_idx;
902 user->seq = log_first_seq;
903 ret = -EPIPE;
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +0900904 logbuf_unlock_irq();
Kay Sieverse11fea92012-05-03 02:29:41 +0200905 goto out;
906 }
907
908 msg = log_from_idx(user->idx);
Tejun Heo0a295e672015-06-25 15:01:27 -0700909 len = msg_print_ext_header(user->buf, sizeof(user->buf),
Linus Torvalds5aa068e2016-10-25 11:27:31 -0700910 msg, user->seq);
Tejun Heo0a295e672015-06-25 15:01:27 -0700911 len += msg_print_ext_body(user->buf + len, sizeof(user->buf) - len,
912 log_dict(msg), msg->dict_len,
913 log_text(msg), msg->text_len);
Kay Sieversd39f3d72012-07-16 18:35:30 -0700914
Kay Sieverse11fea92012-05-03 02:29:41 +0200915 user->idx = log_next(user->idx);
916 user->seq++;
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +0900917 logbuf_unlock_irq();
Kay Sieverse11fea92012-05-03 02:29:41 +0200918
919 if (len > count) {
920 ret = -EINVAL;
921 goto out;
922 }
923
924 if (copy_to_user(buf, user->buf, len)) {
925 ret = -EFAULT;
926 goto out;
927 }
928 ret = len;
929out:
930 mutex_unlock(&user->lock);
931 return ret;
932}
933
934static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence)
935{
936 struct devkmsg_user *user = file->private_data;
937 loff_t ret = 0;
938
939 if (!user)
940 return -EBADF;
941 if (offset)
942 return -ESPIPE;
943
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +0900944 logbuf_lock_irq();
Kay Sieverse11fea92012-05-03 02:29:41 +0200945 switch (whence) {
946 case SEEK_SET:
947 /* the first record */
948 user->idx = log_first_idx;
949 user->seq = log_first_seq;
950 break;
951 case SEEK_DATA:
952 /*
953 * The first record after the last SYSLOG_ACTION_CLEAR,
954 * like issued by 'dmesg -c'. Reading /dev/kmsg itself
955 * changes no global state, and does not clear anything.
956 */
957 user->idx = clear_idx;
958 user->seq = clear_seq;
959 break;
960 case SEEK_END:
961 /* after the last record */
962 user->idx = log_next_idx;
963 user->seq = log_next_seq;
964 break;
965 default:
966 ret = -EINVAL;
967 }
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +0900968 logbuf_unlock_irq();
Kay Sieverse11fea92012-05-03 02:29:41 +0200969 return ret;
970}
971
Al Viro9dd95742017-07-03 00:42:43 -0400972static __poll_t devkmsg_poll(struct file *file, poll_table *wait)
Kay Sieverse11fea92012-05-03 02:29:41 +0200973{
974 struct devkmsg_user *user = file->private_data;
Al Viro9dd95742017-07-03 00:42:43 -0400975 __poll_t ret = 0;
Kay Sieverse11fea92012-05-03 02:29:41 +0200976
977 if (!user)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800978 return EPOLLERR|EPOLLNVAL;
Kay Sieverse11fea92012-05-03 02:29:41 +0200979
980 poll_wait(file, &log_wait, wait);
981
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +0900982 logbuf_lock_irq();
Kay Sieverse11fea92012-05-03 02:29:41 +0200983 if (user->seq < log_next_seq) {
984 /* return error when data has vanished underneath us */
985 if (user->seq < log_first_seq)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800986 ret = EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI;
Nicolas Kaiser0a285312013-04-29 16:17:20 -0700987 else
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800988 ret = EPOLLIN|EPOLLRDNORM;
Kay Sieverse11fea92012-05-03 02:29:41 +0200989 }
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +0900990 logbuf_unlock_irq();
Kay Sieverse11fea92012-05-03 02:29:41 +0200991
992 return ret;
993}
994
995static int devkmsg_open(struct inode *inode, struct file *file)
996{
997 struct devkmsg_user *user;
998 int err;
999
Borislav Petkov750afe72016-08-02 14:04:07 -07001000 if (devkmsg_log & DEVKMSG_LOG_MASK_OFF)
1001 return -EPERM;
Kay Sieverse11fea92012-05-03 02:29:41 +02001002
Borislav Petkov750afe72016-08-02 14:04:07 -07001003 /* write-only does not need any file context */
1004 if ((file->f_flags & O_ACCMODE) != O_WRONLY) {
1005 err = check_syslog_permissions(SYSLOG_ACTION_READ_ALL,
1006 SYSLOG_FROM_READER);
1007 if (err)
1008 return err;
1009 }
Kay Sieverse11fea92012-05-03 02:29:41 +02001010
1011 user = kmalloc(sizeof(struct devkmsg_user), GFP_KERNEL);
1012 if (!user)
1013 return -ENOMEM;
1014
Borislav Petkov750afe72016-08-02 14:04:07 -07001015 ratelimit_default_init(&user->rs);
1016 ratelimit_set_flags(&user->rs, RATELIMIT_MSG_ON_RELEASE);
1017
Kay Sieverse11fea92012-05-03 02:29:41 +02001018 mutex_init(&user->lock);
1019
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09001020 logbuf_lock_irq();
Kay Sieverse11fea92012-05-03 02:29:41 +02001021 user->idx = log_first_idx;
1022 user->seq = log_first_seq;
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09001023 logbuf_unlock_irq();
Kay Sieverse11fea92012-05-03 02:29:41 +02001024
1025 file->private_data = user;
1026 return 0;
1027}
1028
1029static int devkmsg_release(struct inode *inode, struct file *file)
1030{
1031 struct devkmsg_user *user = file->private_data;
1032
1033 if (!user)
1034 return 0;
1035
Borislav Petkov750afe72016-08-02 14:04:07 -07001036 ratelimit_state_exit(&user->rs);
1037
Kay Sieverse11fea92012-05-03 02:29:41 +02001038 mutex_destroy(&user->lock);
1039 kfree(user);
1040 return 0;
1041}
1042
1043const struct file_operations kmsg_fops = {
1044 .open = devkmsg_open,
1045 .read = devkmsg_read,
Al Viro849f3122014-08-23 12:23:53 -04001046 .write_iter = devkmsg_write,
Kay Sieverse11fea92012-05-03 02:29:41 +02001047 .llseek = devkmsg_llseek,
1048 .poll = devkmsg_poll,
1049 .release = devkmsg_release,
1050};
1051
Hari Bathini692f66f2017-05-08 15:56:18 -07001052#ifdef CONFIG_CRASH_CORE
Neil Horman04d491a2009-04-02 16:58:57 -07001053/*
Dirk Gouders4c1ace62013-11-12 15:08:54 -08001054 * This appends the listed symbols to /proc/vmcore
Neil Horman04d491a2009-04-02 16:58:57 -07001055 *
Dirk Gouders4c1ace62013-11-12 15:08:54 -08001056 * /proc/vmcore is used by various utilities, like crash and makedumpfile to
Neil Horman04d491a2009-04-02 16:58:57 -07001057 * obtain access to symbols that are otherwise very difficult to locate. These
1058 * symbols are specifically used so that utilities can access and extract the
1059 * dmesg log from a vmcore file after a crash.
1060 */
Hari Bathini692f66f2017-05-08 15:56:18 -07001061void log_buf_vmcoreinfo_setup(void)
Neil Horman04d491a2009-04-02 16:58:57 -07001062{
1063 VMCOREINFO_SYMBOL(log_buf);
Neil Horman04d491a2009-04-02 16:58:57 -07001064 VMCOREINFO_SYMBOL(log_buf_len);
Kay Sievers7ff95542012-05-03 02:29:13 +02001065 VMCOREINFO_SYMBOL(log_first_idx);
Ivan Delalandef4689082016-03-17 14:21:30 -07001066 VMCOREINFO_SYMBOL(clear_idx);
Kay Sievers7ff95542012-05-03 02:29:13 +02001067 VMCOREINFO_SYMBOL(log_next_idx);
Vivek Goyal67914572012-07-18 13:18:12 -04001068 /*
Joe Perches62e32ac2013-07-31 13:53:47 -07001069 * Export struct printk_log size and field offsets. User space tools can
Vivek Goyal67914572012-07-18 13:18:12 -04001070 * parse it and detect any changes to structure down the line.
1071 */
Joe Perches62e32ac2013-07-31 13:53:47 -07001072 VMCOREINFO_STRUCT_SIZE(printk_log);
1073 VMCOREINFO_OFFSET(printk_log, ts_nsec);
1074 VMCOREINFO_OFFSET(printk_log, len);
1075 VMCOREINFO_OFFSET(printk_log, text_len);
1076 VMCOREINFO_OFFSET(printk_log, dict_len);
Tetsuo Handa15ff2062018-12-18 06:05:04 +09001077#ifdef CONFIG_PRINTK_CALLER
1078 VMCOREINFO_OFFSET(printk_log, caller_id);
1079#endif
Neil Horman04d491a2009-04-02 16:58:57 -07001080}
1081#endif
1082
Mike Travis162a7e72011-05-24 17:13:20 -07001083/* requested log_buf_len from kernel cmdline */
1084static unsigned long __initdata new_log_buf_len;
1085
Luis R. Rodriguezc0a318a2014-08-06 16:08:52 -07001086/* we practice scaling the ring buffer by powers of 2 */
He Zhee6fe3e52018-09-30 00:45:53 +08001087static void __init log_buf_len_update(u64 size)
Luis R. Rodriguezc0a318a2014-08-06 16:08:52 -07001088{
He Zhee6fe3e52018-09-30 00:45:53 +08001089 if (size > (u64)LOG_BUF_LEN_MAX) {
1090 size = (u64)LOG_BUF_LEN_MAX;
1091 pr_err("log_buf over 2G is not supported.\n");
1092 }
1093
Luis R. Rodriguezc0a318a2014-08-06 16:08:52 -07001094 if (size)
1095 size = roundup_pow_of_two(size);
1096 if (size > log_buf_len)
He Zhee6fe3e52018-09-30 00:45:53 +08001097 new_log_buf_len = (unsigned long)size;
Luis R. Rodriguezc0a318a2014-08-06 16:08:52 -07001098}
1099
Mike Travis162a7e72011-05-24 17:13:20 -07001100/* save requested log_buf_len since it's too early to process it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101static int __init log_buf_len_setup(char *str)
1102{
He Zhee6fe3e52018-09-30 00:45:53 +08001103 u64 size;
He Zhe277fcdb2018-09-30 00:45:50 +08001104
1105 if (!str)
1106 return -EINVAL;
1107
1108 size = memparse(str, &str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Luis R. Rodriguezc0a318a2014-08-06 16:08:52 -07001110 log_buf_len_update(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Mike Travis162a7e72011-05-24 17:13:20 -07001112 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
Mike Travis162a7e72011-05-24 17:13:20 -07001114early_param("log_buf_len", log_buf_len_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Geert Uytterhoeven2240a312014-10-13 15:51:11 -07001116#ifdef CONFIG_SMP
1117#define __LOG_CPU_MAX_BUF_LEN (1 << CONFIG_LOG_CPU_MAX_BUF_SHIFT)
1118
Luis R. Rodriguez23b28992014-08-06 16:08:56 -07001119static void __init log_buf_add_cpu(void)
1120{
1121 unsigned int cpu_extra;
1122
1123 /*
1124 * archs should set up cpu_possible_bits properly with
1125 * set_cpu_possible() after setup_arch() but just in
1126 * case lets ensure this is valid.
1127 */
1128 if (num_possible_cpus() == 1)
1129 return;
1130
1131 cpu_extra = (num_possible_cpus() - 1) * __LOG_CPU_MAX_BUF_LEN;
1132
1133 /* by default this will only continue through for large > 64 CPUs */
1134 if (cpu_extra <= __LOG_BUF_LEN / 2)
1135 return;
1136
1137 pr_info("log_buf_len individual max cpu contribution: %d bytes\n",
1138 __LOG_CPU_MAX_BUF_LEN);
1139 pr_info("log_buf_len total cpu_extra contributions: %d bytes\n",
1140 cpu_extra);
1141 pr_info("log_buf_len min size: %d bytes\n", __LOG_BUF_LEN);
1142
1143 log_buf_len_update(cpu_extra + __LOG_BUF_LEN);
1144}
Geert Uytterhoeven2240a312014-10-13 15:51:11 -07001145#else /* !CONFIG_SMP */
1146static inline void log_buf_add_cpu(void) {}
1147#endif /* CONFIG_SMP */
Luis R. Rodriguez23b28992014-08-06 16:08:56 -07001148
Mike Travis162a7e72011-05-24 17:13:20 -07001149void __init setup_log_buf(int early)
1150{
1151 unsigned long flags;
Mike Travis162a7e72011-05-24 17:13:20 -07001152 char *new_log_buf;
Sergey Senozhatskyd2130e82018-10-10 20:33:08 +09001153 unsigned int free;
Mike Travis162a7e72011-05-24 17:13:20 -07001154
Luis R. Rodriguez23b28992014-08-06 16:08:56 -07001155 if (log_buf != __log_buf)
1156 return;
1157
1158 if (!early && !new_log_buf_len)
1159 log_buf_add_cpu();
1160
Mike Travis162a7e72011-05-24 17:13:20 -07001161 if (!new_log_buf_len)
1162 return;
1163
Mike Rapoport26fb3da2019-03-11 23:30:42 -07001164 new_log_buf = memblock_alloc(new_log_buf_len, LOG_ALIGN);
Mike Travis162a7e72011-05-24 17:13:20 -07001165 if (unlikely(!new_log_buf)) {
He Zhee6fe3e52018-09-30 00:45:53 +08001166 pr_err("log_buf_len: %lu bytes not available\n",
Mike Travis162a7e72011-05-24 17:13:20 -07001167 new_log_buf_len);
1168 return;
1169 }
1170
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09001171 logbuf_lock_irqsave(flags);
Mike Travis162a7e72011-05-24 17:13:20 -07001172 log_buf_len = new_log_buf_len;
1173 log_buf = new_log_buf;
1174 new_log_buf_len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001175 free = __LOG_BUF_LEN - log_next_idx;
1176 memcpy(log_buf, __log_buf, __LOG_BUF_LEN);
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09001177 logbuf_unlock_irqrestore(flags);
Mike Travis162a7e72011-05-24 17:13:20 -07001178
He Zhee6fe3e52018-09-30 00:45:53 +08001179 pr_info("log_buf_len: %u bytes\n", log_buf_len);
1180 pr_info("early log buf free: %u(%u%%)\n",
Mike Travis162a7e72011-05-24 17:13:20 -07001181 free, (free * 100) / __LOG_BUF_LEN);
1182}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Andrew Cooks2fa72c82012-12-17 15:59:56 -08001184static bool __read_mostly ignore_loglevel;
1185
1186static int __init ignore_loglevel_setup(char *str)
1187{
Neil Zhangd25d9fe2014-08-06 16:09:12 -07001188 ignore_loglevel = true;
Andrew Morton27083ba2013-11-12 15:08:50 -08001189 pr_info("debug: ignoring loglevel setting.\n");
Andrew Cooks2fa72c82012-12-17 15:59:56 -08001190
1191 return 0;
1192}
1193
1194early_param("ignore_loglevel", ignore_loglevel_setup);
1195module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR);
Joe Perches205bd3d2015-02-12 15:01:34 -08001196MODULE_PARM_DESC(ignore_loglevel,
1197 "ignore loglevel setting (prints all kernel messages to the console)");
Andrew Cooks2fa72c82012-12-17 15:59:56 -08001198
Sergey Senozhatskycf775442016-08-02 14:03:56 -07001199static bool suppress_message_printing(int level)
1200{
1201 return (level >= console_loglevel && !ignore_loglevel);
1202}
1203
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001204#ifdef CONFIG_BOOT_PRINTK_DELAY
1205
Namhyung Kim674dff62010-10-26 14:22:48 -07001206static int boot_delay; /* msecs delay after each printk during bootup */
Dave Young3a3b6ed2009-09-22 16:43:31 -07001207static unsigned long long loops_per_msec; /* based on boot_delay */
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001208
1209static int __init boot_delay_setup(char *str)
1210{
1211 unsigned long lpj;
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001212
1213 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
1214 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
1215
1216 get_option(&str, &boot_delay);
1217 if (boot_delay > 10 * 1000)
1218 boot_delay = 0;
1219
Dave Young3a3b6ed2009-09-22 16:43:31 -07001220 pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
1221 "HZ: %d, loops_per_msec: %llu\n",
1222 boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
Dave Young29e9d222013-11-12 15:08:53 -08001223 return 0;
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001224}
Dave Young29e9d222013-11-12 15:08:53 -08001225early_param("boot_delay", boot_delay_setup);
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001226
Andrew Cooks2fa72c82012-12-17 15:59:56 -08001227static void boot_delay_msec(int level)
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001228{
1229 unsigned long long k;
1230 unsigned long timeout;
1231
Thomas Gleixnerff48cd22017-05-16 20:42:45 +02001232 if ((boot_delay == 0 || system_state >= SYSTEM_RUNNING)
Sergey Senozhatskycf775442016-08-02 14:03:56 -07001233 || suppress_message_printing(level)) {
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001234 return;
Andrew Cooks2fa72c82012-12-17 15:59:56 -08001235 }
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001236
Dave Young3a3b6ed2009-09-22 16:43:31 -07001237 k = (unsigned long long)loops_per_msec * boot_delay;
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001238
1239 timeout = jiffies + msecs_to_jiffies(boot_delay);
1240 while (k) {
1241 k--;
1242 cpu_relax();
1243 /*
1244 * use (volatile) jiffies to prevent
1245 * compiler reduction; loop termination via jiffies
1246 * is secondary and may or may not happen.
1247 */
1248 if (time_after(jiffies, timeout))
1249 break;
1250 touch_nmi_watchdog();
1251 }
1252}
1253#else
Andrew Cooks2fa72c82012-12-17 15:59:56 -08001254static inline void boot_delay_msec(int level)
Randy Dunlapbfe8df32007-10-16 01:23:46 -07001255{
1256}
1257#endif
1258
Alex Eldere99aa462014-08-06 16:09:05 -07001259static bool printk_time = IS_ENABLED(CONFIG_PRINTK_TIME);
Kay Sievers7ff95542012-05-03 02:29:13 +02001260module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
1261
Tetsuo Handa07c17732018-12-11 18:49:05 +09001262static size_t print_syslog(unsigned int level, char *buf)
1263{
1264 return sprintf(buf, "<%u>", level);
1265}
1266
Kay Sievers084681d2012-06-28 09:38:53 +02001267static size_t print_time(u64 ts, char *buf)
1268{
Tetsuo Handae80c1a92018-12-04 19:00:01 +09001269 unsigned long rem_nsec = do_div(ts, 1000000000);
Roland Dreier35dac272013-01-04 15:35:50 -08001270
Tetsuo Handa15ff2062018-12-18 06:05:04 +09001271 return sprintf(buf, "[%5lu.%06lu]",
Kay Sievers084681d2012-06-28 09:38:53 +02001272 (unsigned long)ts, rem_nsec / 1000);
1273}
1274
Tetsuo Handa15ff2062018-12-18 06:05:04 +09001275#ifdef CONFIG_PRINTK_CALLER
1276static size_t print_caller(u32 id, char *buf)
1277{
1278 char caller[12];
1279
1280 snprintf(caller, sizeof(caller), "%c%u",
1281 id & 0x80000000 ? 'C' : 'T', id & ~0x80000000);
1282 return sprintf(buf, "[%6s]", caller);
1283}
1284#else
1285#define print_caller(id, buf) 0
1286#endif
1287
Tetsuo Handae80c1a92018-12-04 19:00:01 +09001288static size_t print_prefix(const struct printk_log *msg, bool syslog,
1289 bool time, char *buf)
Kay Sievers649e6ee2012-05-10 04:30:45 +02001290{
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001291 size_t len = 0;
Kay Sievers649e6ee2012-05-10 04:30:45 +02001292
Tetsuo Handa07c17732018-12-11 18:49:05 +09001293 if (syslog)
1294 len = print_syslog((msg->facility << 3) | msg->level, buf);
Tetsuo Handa15ff2062018-12-18 06:05:04 +09001295
Tetsuo Handae80c1a92018-12-04 19:00:01 +09001296 if (time)
Tetsuo Handa07c17732018-12-11 18:49:05 +09001297 len += print_time(msg->ts_nsec, buf + len);
Tetsuo Handa15ff2062018-12-18 06:05:04 +09001298
1299 len += print_caller(msg->caller_id, buf + len);
1300
1301 if (IS_ENABLED(CONFIG_PRINTK_CALLER) || time) {
1302 buf[len++] = ' ';
1303 buf[len] = '\0';
1304 }
1305
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001306 return len;
1307}
1308
Tetsuo Handae80c1a92018-12-04 19:00:01 +09001309static size_t msg_print_text(const struct printk_log *msg, bool syslog,
1310 bool time, char *buf, size_t size)
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001311{
1312 const char *text = log_text(msg);
1313 size_t text_size = msg->text_len;
1314 size_t len = 0;
Tetsuo Handa07c17732018-12-11 18:49:05 +09001315 char prefix[PREFIX_MAX];
1316 const size_t prefix_len = print_prefix(msg, syslog, time, prefix);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001317
1318 do {
1319 const char *next = memchr(text, '\n', text_size);
1320 size_t text_len;
1321
1322 if (next) {
1323 text_len = next - text;
1324 next++;
1325 text_size -= next - text;
1326 } else {
1327 text_len = text_size;
1328 }
1329
1330 if (buf) {
Tetsuo Handa07c17732018-12-11 18:49:05 +09001331 if (prefix_len + text_len + 1 >= size - len)
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001332 break;
1333
Tetsuo Handa07c17732018-12-11 18:49:05 +09001334 memcpy(buf + len, prefix, prefix_len);
1335 len += prefix_len;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001336 memcpy(buf + len, text, text_len);
1337 len += text_len;
Linus Torvalds5aa068e2016-10-25 11:27:31 -07001338 buf[len++] = '\n';
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001339 } else {
1340 /* SYSLOG_ACTION_* buffer size only calculation */
Tetsuo Handa07c17732018-12-11 18:49:05 +09001341 len += prefix_len + text_len + 1;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001342 }
1343
1344 text = next;
1345 } while (text);
1346
Kay Sievers7ff95542012-05-03 02:29:13 +02001347 return len;
1348}
1349
1350static int syslog_print(char __user *buf, int size)
1351{
1352 char *text;
Joe Perches62e32ac2013-07-31 13:53:47 -07001353 struct printk_log *msg;
Jan Beulich116e90b2012-06-22 16:36:09 +01001354 int len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001355
Kay Sievers70498252012-07-16 18:35:29 -07001356 text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
Kay Sievers7ff95542012-05-03 02:29:13 +02001357 if (!text)
1358 return -ENOMEM;
1359
Jan Beulich116e90b2012-06-22 16:36:09 +01001360 while (size > 0) {
1361 size_t n;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001362 size_t skip;
Kay Sievers7ff95542012-05-03 02:29:13 +02001363
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09001364 logbuf_lock_irq();
Jan Beulich116e90b2012-06-22 16:36:09 +01001365 if (syslog_seq < log_first_seq) {
1366 /* messages are gone, move to first one */
1367 syslog_seq = log_first_seq;
1368 syslog_idx = log_first_idx;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001369 syslog_partial = 0;
Jan Beulich116e90b2012-06-22 16:36:09 +01001370 }
1371 if (syslog_seq == log_next_seq) {
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09001372 logbuf_unlock_irq();
Jan Beulich116e90b2012-06-22 16:36:09 +01001373 break;
1374 }
Kay Sieverseb02dac2012-07-09 10:05:10 -07001375
Tetsuo Handae80c1a92018-12-04 19:00:01 +09001376 /*
1377 * To keep reading/counting partial line consistent,
1378 * use printk_time value as of the beginning of a line.
1379 */
1380 if (!syslog_partial)
1381 syslog_time = printk_time;
1382
Kay Sieverseb02dac2012-07-09 10:05:10 -07001383 skip = syslog_partial;
Jan Beulich116e90b2012-06-22 16:36:09 +01001384 msg = log_from_idx(syslog_idx);
Tetsuo Handae80c1a92018-12-04 19:00:01 +09001385 n = msg_print_text(msg, true, syslog_time, text,
1386 LOG_LINE_MAX + PREFIX_MAX);
Kay Sieverseb02dac2012-07-09 10:05:10 -07001387 if (n - syslog_partial <= size) {
1388 /* message fits into buffer, move forward */
Jan Beulich116e90b2012-06-22 16:36:09 +01001389 syslog_idx = log_next(syslog_idx);
1390 syslog_seq++;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001391 n -= syslog_partial;
1392 syslog_partial = 0;
1393 } else if (!len){
1394 /* partial read(), remember position */
1395 n = size;
1396 syslog_partial += n;
Jan Beulich116e90b2012-06-22 16:36:09 +01001397 } else
1398 n = 0;
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09001399 logbuf_unlock_irq();
Jan Beulich116e90b2012-06-22 16:36:09 +01001400
1401 if (!n)
1402 break;
1403
Kay Sieverseb02dac2012-07-09 10:05:10 -07001404 if (copy_to_user(buf, text + skip, n)) {
Jan Beulich116e90b2012-06-22 16:36:09 +01001405 if (!len)
1406 len = -EFAULT;
1407 break;
1408 }
Kay Sieverseb02dac2012-07-09 10:05:10 -07001409
1410 len += n;
1411 size -= n;
1412 buf += n;
Jan Beulich116e90b2012-06-22 16:36:09 +01001413 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001414
1415 kfree(text);
1416 return len;
1417}
1418
1419static int syslog_print_all(char __user *buf, int size, bool clear)
1420{
1421 char *text;
1422 int len = 0;
Namit Gupta63842c22018-06-20 19:26:19 +05301423 u64 next_seq;
1424 u64 seq;
1425 u32 idx;
Tetsuo Handae80c1a92018-12-04 19:00:01 +09001426 bool time;
Namit Gupta63842c22018-06-20 19:26:19 +05301427
Kay Sievers70498252012-07-16 18:35:29 -07001428 text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
Kay Sievers7ff95542012-05-03 02:29:13 +02001429 if (!text)
1430 return -ENOMEM;
1431
Tetsuo Handae80c1a92018-12-04 19:00:01 +09001432 time = printk_time;
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09001433 logbuf_lock_irq();
Namit Gupta63842c22018-06-20 19:26:19 +05301434 /*
1435 * Find first record that fits, including all following records,
1436 * into the user-provided buffer for this dump.
1437 */
1438 seq = clear_seq;
1439 idx = clear_idx;
1440 while (seq < log_next_seq) {
1441 struct printk_log *msg = log_from_idx(idx);
Kay Sievers7ff95542012-05-03 02:29:13 +02001442
Tetsuo Handae80c1a92018-12-04 19:00:01 +09001443 len += msg_print_text(msg, true, time, NULL, 0);
Namit Gupta63842c22018-06-20 19:26:19 +05301444 idx = log_next(idx);
1445 seq++;
1446 }
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001447
Namit Gupta63842c22018-06-20 19:26:19 +05301448 /* move first record forward until length fits into the buffer */
1449 seq = clear_seq;
1450 idx = clear_idx;
1451 while (len > size && seq < log_next_seq) {
1452 struct printk_log *msg = log_from_idx(idx);
1453
Tetsuo Handae80c1a92018-12-04 19:00:01 +09001454 len -= msg_print_text(msg, true, time, NULL, 0);
Namit Gupta63842c22018-06-20 19:26:19 +05301455 idx = log_next(idx);
1456 seq++;
1457 }
1458
1459 /* last message fitting into this dump */
1460 next_seq = log_next_seq;
1461
1462 len = 0;
1463 while (len >= 0 && seq < next_seq) {
1464 struct printk_log *msg = log_from_idx(idx);
Tetsuo Handae80c1a92018-12-04 19:00:01 +09001465 int textlen = msg_print_text(msg, true, time, text,
1466 LOG_LINE_MAX + PREFIX_MAX);
Namit Gupta63842c22018-06-20 19:26:19 +05301467
Namit Gupta63842c22018-06-20 19:26:19 +05301468 idx = log_next(idx);
1469 seq++;
Kay Sieverse2ae7152012-06-15 14:07:51 +02001470
Namit Gupta63842c22018-06-20 19:26:19 +05301471 logbuf_unlock_irq();
1472 if (copy_to_user(buf + len, text, textlen))
1473 len = -EFAULT;
1474 else
1475 len += textlen;
1476 logbuf_lock_irq();
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001477
Namit Gupta63842c22018-06-20 19:26:19 +05301478 if (seq < log_first_seq) {
1479 /* messages are gone, move to next one */
1480 seq = log_first_seq;
1481 idx = log_first_idx;
Kay Sievers7ff95542012-05-03 02:29:13 +02001482 }
1483 }
1484
1485 if (clear) {
1486 clear_seq = log_next_seq;
1487 clear_idx = log_next_idx;
1488 }
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09001489 logbuf_unlock_irq();
Kay Sievers7ff95542012-05-03 02:29:13 +02001490
1491 kfree(text);
1492 return len;
1493}
1494
Petr Mladek8599dc72018-06-27 17:06:41 +02001495static void syslog_clear(void)
1496{
1497 logbuf_lock_irq();
1498 clear_seq = log_next_seq;
1499 clear_idx = log_next_idx;
1500 logbuf_unlock_irq();
1501}
1502
Vasily Averin3ea43312015-06-25 15:01:47 -07001503int do_syslog(int type, char __user *buf, int len, int source)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Kay Sievers7ff95542012-05-03 02:29:13 +02001505 bool clear = false;
Joe Perchesa39d4a82014-12-10 15:50:15 -08001506 static int saved_console_loglevel = LOGLEVEL_DEFAULT;
Linus Torvaldsee24aeb2011-02-10 17:53:55 -08001507 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Vasily Averin3ea43312015-06-25 15:01:47 -07001509 error = check_syslog_permissions(type, source);
Linus Torvaldsee24aeb2011-02-10 17:53:55 -08001510 if (error)
Nikitas Angelinas077a1cc2017-07-29 20:36:36 -07001511 return error;
Eric Paris12b30522010-11-15 18:36:29 -05001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08001514 case SYSLOG_ACTION_CLOSE: /* Close log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001516 case SYSLOG_ACTION_OPEN: /* Open log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001518 case SYSLOG_ACTION_READ: /* Read from log */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (!buf || len < 0)
Nikitas Angelinas077a1cc2017-07-29 20:36:36 -07001520 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (!len)
Nikitas Angelinas077a1cc2017-07-29 20:36:36 -07001522 return 0;
Linus Torvalds96d4f262019-01-03 18:57:57 -08001523 if (!access_ok(buf, len))
Nikitas Angelinas077a1cc2017-07-29 20:36:36 -07001524 return -EFAULT;
Yuanhan Liu4a77a5a2012-06-16 21:21:51 +08001525 error = wait_event_interruptible(log_wait,
1526 syslog_seq != log_next_seq);
Kay Sieverscb424ff2012-07-06 09:50:09 -07001527 if (error)
Nikitas Angelinas077a1cc2017-07-29 20:36:36 -07001528 return error;
Kay Sievers7ff95542012-05-03 02:29:13 +02001529 error = syslog_print(buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001531 /* Read/clear last kernel messages */
1532 case SYSLOG_ACTION_READ_CLEAR:
Kay Sievers7ff95542012-05-03 02:29:13 +02001533 clear = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 /* FALL THRU */
Kees Cookd78ca3c2010-02-03 15:37:13 -08001535 /* Read last kernel messages */
1536 case SYSLOG_ACTION_READ_ALL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (!buf || len < 0)
Nikitas Angelinas077a1cc2017-07-29 20:36:36 -07001538 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 if (!len)
Nikitas Angelinas077a1cc2017-07-29 20:36:36 -07001540 return 0;
Linus Torvalds96d4f262019-01-03 18:57:57 -08001541 if (!access_ok(buf, len))
Nikitas Angelinas077a1cc2017-07-29 20:36:36 -07001542 return -EFAULT;
Kay Sievers7ff95542012-05-03 02:29:13 +02001543 error = syslog_print_all(buf, len, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001545 /* Clear ring buffer */
1546 case SYSLOG_ACTION_CLEAR:
Petr Mladek8599dc72018-06-27 17:06:41 +02001547 syslog_clear();
Alan Stern4661e352012-06-22 17:12:19 -04001548 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001549 /* Disable logging to console */
1550 case SYSLOG_ACTION_CONSOLE_OFF:
Joe Perchesa39d4a82014-12-10 15:50:15 -08001551 if (saved_console_loglevel == LOGLEVEL_DEFAULT)
Frans Pop1aaad492009-07-06 13:31:48 +02001552 saved_console_loglevel = console_loglevel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 console_loglevel = minimum_console_loglevel;
1554 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001555 /* Enable logging to console */
1556 case SYSLOG_ACTION_CONSOLE_ON:
Joe Perchesa39d4a82014-12-10 15:50:15 -08001557 if (saved_console_loglevel != LOGLEVEL_DEFAULT) {
Frans Pop1aaad492009-07-06 13:31:48 +02001558 console_loglevel = saved_console_loglevel;
Joe Perchesa39d4a82014-12-10 15:50:15 -08001559 saved_console_loglevel = LOGLEVEL_DEFAULT;
Frans Pop1aaad492009-07-06 13:31:48 +02001560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001562 /* Set level of messages printed to console */
1563 case SYSLOG_ACTION_CONSOLE_LEVEL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 if (len < 1 || len > 8)
Nikitas Angelinas077a1cc2017-07-29 20:36:36 -07001565 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 if (len < minimum_console_loglevel)
1567 len = minimum_console_loglevel;
1568 console_loglevel = len;
Frans Pop1aaad492009-07-06 13:31:48 +02001569 /* Implicitly re-enable logging to console */
Joe Perchesa39d4a82014-12-10 15:50:15 -08001570 saved_console_loglevel = LOGLEVEL_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001572 /* Number of chars in the log buffer */
1573 case SYSLOG_ACTION_SIZE_UNREAD:
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09001574 logbuf_lock_irq();
Kay Sievers7ff95542012-05-03 02:29:13 +02001575 if (syslog_seq < log_first_seq) {
1576 /* messages are gone, move to first one */
1577 syslog_seq = log_first_seq;
1578 syslog_idx = log_first_idx;
Kay Sieverseb02dac2012-07-09 10:05:10 -07001579 syslog_partial = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02001580 }
Vasily Averin3ea43312015-06-25 15:01:47 -07001581 if (source == SYSLOG_FROM_PROC) {
Kay Sievers7ff95542012-05-03 02:29:13 +02001582 /*
1583 * Short-cut for poll(/"proc/kmsg") which simply checks
1584 * for pending data, not the size; return the count of
1585 * records, not the length.
1586 */
Alex Eldere97e1262014-08-06 16:08:59 -07001587 error = log_next_seq - syslog_seq;
Kay Sievers7ff95542012-05-03 02:29:13 +02001588 } else {
Kay Sievers5becfb12012-07-09 12:15:42 -07001589 u64 seq = syslog_seq;
1590 u32 idx = syslog_idx;
Tetsuo Handae80c1a92018-12-04 19:00:01 +09001591 bool time = syslog_partial ? syslog_time : printk_time;
Kay Sievers7ff95542012-05-03 02:29:13 +02001592
Kay Sievers7ff95542012-05-03 02:29:13 +02001593 while (seq < log_next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07001594 struct printk_log *msg = log_from_idx(idx);
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02001595
Tetsuo Handae80c1a92018-12-04 19:00:01 +09001596 error += msg_print_text(msg, true, time, NULL,
1597 0);
1598 time = printk_time;
Kay Sievers7ff95542012-05-03 02:29:13 +02001599 idx = log_next(idx);
1600 seq++;
1601 }
Kay Sieverseb02dac2012-07-09 10:05:10 -07001602 error -= syslog_partial;
Kay Sievers7ff95542012-05-03 02:29:13 +02001603 }
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09001604 logbuf_unlock_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001606 /* Size of the log buffer */
1607 case SYSLOG_ACTION_SIZE_BUFFER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 error = log_buf_len;
1609 break;
1610 default:
1611 error = -EINVAL;
1612 break;
1613 }
Nikitas Angelinas077a1cc2017-07-29 20:36:36 -07001614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 return error;
1616}
1617
Heiko Carstens1e7bfb22009-01-14 14:14:29 +01001618SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
Kees Cook637241a2013-06-12 14:04:39 -07001620 return do_syslog(type, buf, len, SYSLOG_FROM_READER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
1622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623/*
Petr Mladekc162d5b2018-01-12 17:08:37 +01001624 * Special console_lock variants that help to reduce the risk of soft-lockups.
1625 * They allow to pass console_lock to another printk() call using a busy wait.
1626 */
1627
1628#ifdef CONFIG_LOCKDEP
1629static struct lockdep_map console_owner_dep_map = {
1630 .name = "console_owner"
1631};
1632#endif
1633
1634static DEFINE_RAW_SPINLOCK(console_owner_lock);
1635static struct task_struct *console_owner;
1636static bool console_waiter;
1637
1638/**
1639 * console_lock_spinning_enable - mark beginning of code where another
1640 * thread might safely busy wait
1641 *
1642 * This basically converts console_lock into a spinlock. This marks
1643 * the section where the console_lock owner can not sleep, because
1644 * there may be a waiter spinning (like a spinlock). Also it must be
1645 * ready to hand over the lock at the end of the section.
1646 */
1647static void console_lock_spinning_enable(void)
1648{
1649 raw_spin_lock(&console_owner_lock);
1650 console_owner = current;
1651 raw_spin_unlock(&console_owner_lock);
1652
1653 /* The waiter may spin on us after setting console_owner */
1654 spin_acquire(&console_owner_dep_map, 0, 0, _THIS_IP_);
1655}
1656
1657/**
1658 * console_lock_spinning_disable_and_check - mark end of code where another
1659 * thread was able to busy wait and check if there is a waiter
1660 *
1661 * This is called at the end of the section where spinning is allowed.
1662 * It has two functions. First, it is a signal that it is no longer
1663 * safe to start busy waiting for the lock. Second, it checks if
1664 * there is a busy waiter and passes the lock rights to her.
1665 *
1666 * Important: Callers lose the lock if there was a busy waiter.
1667 * They must not touch items synchronized by console_lock
1668 * in this case.
1669 *
1670 * Return: 1 if the lock rights were passed, 0 otherwise.
1671 */
1672static int console_lock_spinning_disable_and_check(void)
1673{
1674 int waiter;
1675
1676 raw_spin_lock(&console_owner_lock);
1677 waiter = READ_ONCE(console_waiter);
1678 console_owner = NULL;
1679 raw_spin_unlock(&console_owner_lock);
1680
1681 if (!waiter) {
Qian Cai5facae42019-09-19 12:09:40 -04001682 spin_release(&console_owner_dep_map, _THIS_IP_);
Petr Mladekc162d5b2018-01-12 17:08:37 +01001683 return 0;
1684 }
1685
1686 /* The waiter is now free to continue */
1687 WRITE_ONCE(console_waiter, false);
1688
Qian Cai5facae42019-09-19 12:09:40 -04001689 spin_release(&console_owner_dep_map, _THIS_IP_);
Petr Mladekc162d5b2018-01-12 17:08:37 +01001690
1691 /*
1692 * Hand off console_lock to waiter. The waiter will perform
1693 * the up(). After this, the waiter is the console_lock owner.
1694 */
Qian Cai5facae42019-09-19 12:09:40 -04001695 mutex_release(&console_lock_dep_map, _THIS_IP_);
Petr Mladekc162d5b2018-01-12 17:08:37 +01001696 return 1;
1697}
1698
1699/**
1700 * console_trylock_spinning - try to get console_lock by busy waiting
1701 *
1702 * This allows to busy wait for the console_lock when the current
1703 * owner is running in specially marked sections. It means that
1704 * the current owner is running and cannot reschedule until it
1705 * is ready to lose the lock.
1706 *
1707 * Return: 1 if we got the lock, 0 othrewise
1708 */
1709static int console_trylock_spinning(void)
1710{
1711 struct task_struct *owner = NULL;
1712 bool waiter;
1713 bool spin = false;
1714 unsigned long flags;
1715
1716 if (console_trylock())
1717 return 1;
1718
1719 printk_safe_enter_irqsave(flags);
1720
1721 raw_spin_lock(&console_owner_lock);
1722 owner = READ_ONCE(console_owner);
1723 waiter = READ_ONCE(console_waiter);
1724 if (!waiter && owner && owner != current) {
1725 WRITE_ONCE(console_waiter, true);
1726 spin = true;
1727 }
1728 raw_spin_unlock(&console_owner_lock);
1729
1730 /*
1731 * If there is an active printk() writing to the
1732 * consoles, instead of having it write our data too,
1733 * see if we can offload that load from the active
1734 * printer, and do some printing ourselves.
1735 * Go into a spin only if there isn't already a waiter
1736 * spinning, and there is an active printer, and
1737 * that active printer isn't us (recursive printk?).
1738 */
1739 if (!spin) {
1740 printk_safe_exit_irqrestore(flags);
1741 return 0;
1742 }
1743
1744 /* We spin waiting for the owner to release us */
1745 spin_acquire(&console_owner_dep_map, 0, 0, _THIS_IP_);
1746 /* Owner will clear console_waiter on hand off */
1747 while (READ_ONCE(console_waiter))
1748 cpu_relax();
Qian Cai5facae42019-09-19 12:09:40 -04001749 spin_release(&console_owner_dep_map, _THIS_IP_);
Petr Mladekc162d5b2018-01-12 17:08:37 +01001750
1751 printk_safe_exit_irqrestore(flags);
1752 /*
1753 * The owner passed the console lock to us.
1754 * Since we did not spin on console lock, annotate
1755 * this as a trylock. Otherwise lockdep will
1756 * complain.
1757 */
1758 mutex_acquire(&console_lock_dep_map, 0, 1, _THIS_IP_);
1759
1760 return 1;
1761}
1762
1763/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 * Call the console drivers, asking them to write out
1765 * log_buf[start] to log_buf[end - 1].
Torben Hohnac751ef2011-01-25 15:07:35 -08001766 * The console_lock must be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 */
Sergey Senozhatskyd9c23522016-12-24 23:09:01 +09001768static void call_console_drivers(const char *ext_text, size_t ext_len,
Tejun Heo6fe29352015-06-25 15:01:30 -07001769 const char *text, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Kay Sievers7ff95542012-05-03 02:29:13 +02001771 struct console *con;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Sergey Senozhatskyfc98c3c2017-02-18 03:42:54 -08001773 trace_console_rcuidle(text, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Kay Sievers7ff95542012-05-03 02:29:13 +02001775 if (!console_drivers)
1776 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Kay Sievers7ff95542012-05-03 02:29:13 +02001778 for_each_console(con) {
1779 if (exclusive_console && con != exclusive_console)
1780 continue;
1781 if (!(con->flags & CON_ENABLED))
1782 continue;
1783 if (!con->write)
1784 continue;
1785 if (!cpu_online(smp_processor_id()) &&
1786 !(con->flags & CON_ANYTIME))
1787 continue;
Tejun Heo6fe29352015-06-25 15:01:30 -07001788 if (con->flags & CON_EXTENDED)
1789 con->write(con, ext_text, ext_len);
1790 else
1791 con->write(con, text, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793}
1794
Dave Youngaf913222009-09-22 16:43:33 -07001795int printk_delay_msec __read_mostly;
1796
1797static inline void printk_delay(void)
1798{
1799 if (unlikely(printk_delay_msec)) {
1800 int m = printk_delay_msec;
1801
1802 while (m--) {
1803 mdelay(1);
1804 touch_nmi_watchdog();
1805 }
1806 }
1807}
1808
Tetsuo Handacbae05d2019-02-16 19:59:33 +09001809static inline u32 printk_caller_id(void)
1810{
1811 return in_task() ? task_pid_nr(current) :
1812 0x80000000 + raw_smp_processor_id();
1813}
1814
Kay Sievers084681d2012-06-28 09:38:53 +02001815/*
1816 * Continuation lines are buffered, and not committed to the record buffer
1817 * until the line is complete, or a race forces it. The line fragments
1818 * though, are printed immediately to the consoles to ensure everything has
1819 * reached the console in case of a kernel crash.
1820 */
1821static struct cont {
1822 char buf[LOG_LINE_MAX];
1823 size_t len; /* length == 0 means unused buffer */
Tetsuo Handacbae05d2019-02-16 19:59:33 +09001824 u32 caller_id; /* printk_caller_id() of first print */
Kay Sievers084681d2012-06-28 09:38:53 +02001825 u64 ts_nsec; /* time of first print */
1826 u8 level; /* log level of first message */
Alex Elder0b90fec2014-08-06 16:09:03 -07001827 u8 facility; /* log facility of first message */
Kay Sieverseab07262012-07-16 18:35:30 -07001828 enum log_flags flags; /* prefix, newline flags */
Kay Sievers084681d2012-06-28 09:38:53 +02001829} cont;
1830
Linus Torvalds5e467652016-10-09 11:53:00 -07001831static void cont_flush(void)
Kay Sievers084681d2012-06-28 09:38:53 +02001832{
Kay Sievers084681d2012-06-28 09:38:53 +02001833 if (cont.len == 0)
1834 return;
Linus Torvalds5c2992e2016-10-25 11:27:31 -07001835
Tetsuo Handacbae05d2019-02-16 19:59:33 +09001836 log_store(cont.caller_id, cont.facility, cont.level, cont.flags,
1837 cont.ts_nsec, NULL, 0, cont.buf, cont.len);
Linus Torvalds5c2992e2016-10-25 11:27:31 -07001838 cont.len = 0;
Kay Sievers084681d2012-06-28 09:38:53 +02001839}
1840
Tetsuo Handacbae05d2019-02-16 19:59:33 +09001841static bool cont_add(u32 caller_id, int facility, int level,
1842 enum log_flags flags, const char *text, size_t len)
Kay Sievers084681d2012-06-28 09:38:53 +02001843{
Sergey Senozhatsky96278082018-10-02 11:38:34 +09001844 /* If the line gets too long, split it up in separate records. */
1845 if (cont.len + len > sizeof(cont.buf)) {
Linus Torvalds5e467652016-10-09 11:53:00 -07001846 cont_flush();
Kay Sievers084681d2012-06-28 09:38:53 +02001847 return false;
1848 }
1849
1850 if (!cont.len) {
1851 cont.facility = facility;
1852 cont.level = level;
Tetsuo Handacbae05d2019-02-16 19:59:33 +09001853 cont.caller_id = caller_id;
Kay Sievers084681d2012-06-28 09:38:53 +02001854 cont.ts_nsec = local_clock();
Linus Torvalds5e467652016-10-09 11:53:00 -07001855 cont.flags = flags;
Kay Sievers084681d2012-06-28 09:38:53 +02001856 }
1857
1858 memcpy(cont.buf + cont.len, text, len);
1859 cont.len += len;
Kay Sieverseab07262012-07-16 18:35:30 -07001860
Linus Torvalds5e467652016-10-09 11:53:00 -07001861 // The original flags come from the first line,
1862 // but later continuations can add a newline.
1863 if (flags & LOG_NEWLINE) {
1864 cont.flags |= LOG_NEWLINE;
1865 cont_flush();
1866 }
1867
Kay Sievers084681d2012-06-28 09:38:53 +02001868 return true;
1869}
1870
Linus Torvaldsc362c7f2016-10-08 22:02:09 -07001871static size_t log_output(int facility, int level, enum log_flags lflags, const char *dict, size_t dictlen, char *text, size_t text_len)
1872{
Tetsuo Handacbae05d2019-02-16 19:59:33 +09001873 const u32 caller_id = printk_caller_id();
1874
Linus Torvaldsc362c7f2016-10-08 22:02:09 -07001875 /*
Linus Torvalds5e467652016-10-09 11:53:00 -07001876 * If an earlier line was buffered, and we're a continuation
Tetsuo Handacbae05d2019-02-16 19:59:33 +09001877 * write from the same context, try to add it to the buffer.
Linus Torvaldsc362c7f2016-10-08 22:02:09 -07001878 */
1879 if (cont.len) {
Tetsuo Handacbae05d2019-02-16 19:59:33 +09001880 if (cont.caller_id == caller_id && (lflags & LOG_CONT)) {
1881 if (cont_add(caller_id, facility, level, lflags, text, text_len))
Linus Torvalds5e467652016-10-09 11:53:00 -07001882 return text_len;
1883 }
1884 /* Otherwise, make sure it's flushed */
1885 cont_flush();
1886 }
Linus Torvaldsc362c7f2016-10-08 22:02:09 -07001887
Linus Torvalds8835ca52016-10-19 09:11:24 -07001888 /* Skip empty continuation lines that couldn't be added - they just flush */
1889 if (!text_len && (lflags & LOG_CONT))
1890 return 0;
1891
Linus Torvalds5e467652016-10-09 11:53:00 -07001892 /* If it doesn't end in a newline, try to buffer the current line */
1893 if (!(lflags & LOG_NEWLINE)) {
Tetsuo Handacbae05d2019-02-16 19:59:33 +09001894 if (cont_add(caller_id, facility, level, lflags, text, text_len))
Linus Torvaldsc362c7f2016-10-08 22:02:09 -07001895 return text_len;
1896 }
1897
Linus Torvalds5e467652016-10-09 11:53:00 -07001898 /* Store it in the record log */
Tetsuo Handacbae05d2019-02-16 19:59:33 +09001899 return log_store(caller_id, facility, level, lflags, 0,
1900 dict, dictlen, text, text_len);
Linus Torvaldsc362c7f2016-10-08 22:02:09 -07001901}
1902
Petr Mladekba552392018-06-27 16:08:15 +02001903/* Must be called under logbuf_lock. */
1904int vprintk_store(int facility, int level,
1905 const char *dict, size_t dictlen,
1906 const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
Kay Sievers7ff95542012-05-03 02:29:13 +02001908 static char textbuf[LOG_LINE_MAX];
1909 char *text = textbuf;
Pierre Kuoaec47ca2017-07-11 14:40:55 +08001910 size_t text_len;
Kay Sievers5becfb12012-07-09 12:15:42 -07001911 enum log_flags lflags = 0;
Jan Kara608873c2014-06-04 16:11:35 -07001912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 /*
Kay Sievers7ff95542012-05-03 02:29:13 +02001914 * The printf needs to come first; we need the syslog
1915 * prefix which might be passed-in as a parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 */
Markus Trippelsdorf98e35f52014-10-13 15:51:13 -07001917 text_len = vscnprintf(text, sizeof(textbuf), fmt, args);
Nick Andrewac60ad72008-05-12 21:21:04 +02001918
Kay Sievers7ff95542012-05-03 02:29:13 +02001919 /* mark and strip a trailing newline */
Kay Sieversc313af12012-05-14 20:46:27 +02001920 if (text_len && text[text_len-1] == '\n') {
1921 text_len--;
Kay Sievers5becfb12012-07-09 12:15:42 -07001922 lflags |= LOG_NEWLINE;
Kay Sievers7ff95542012-05-03 02:29:13 +02001923 }
Kay Sievers9d90c8d2011-03-13 03:19:51 +01001924
Joe Perches088a52a2012-07-30 14:40:19 -07001925 /* strip kernel syslog prefix and extract log level or control flags */
1926 if (facility == 0) {
Linus Torvalds4bcc5952016-10-08 20:32:40 -07001927 int kern_level;
Joe Perches088a52a2012-07-30 14:40:19 -07001928
Linus Torvalds4bcc5952016-10-08 20:32:40 -07001929 while ((kern_level = printk_get_level(text)) != 0) {
Joe Perches088a52a2012-07-30 14:40:19 -07001930 switch (kern_level) {
1931 case '0' ... '7':
Joe Perchesa39d4a82014-12-10 15:50:15 -08001932 if (level == LOGLEVEL_DEFAULT)
Joe Perches088a52a2012-07-30 14:40:19 -07001933 level = kern_level - '0';
Linus Torvalds4bcc5952016-10-08 20:32:40 -07001934 break;
1935 case 'c': /* KERN_CONT */
1936 lflags |= LOG_CONT;
Joe Perches088a52a2012-07-30 14:40:19 -07001937 }
Linus Torvalds4bcc5952016-10-08 20:32:40 -07001938
1939 text_len -= 2;
1940 text += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001942 }
Nick Andrewac60ad72008-05-12 21:21:04 +02001943
Joe Perchesa39d4a82014-12-10 15:50:15 -08001944 if (level == LOGLEVEL_DEFAULT)
Kay Sieversc313af12012-05-14 20:46:27 +02001945 level = default_message_loglevel;
1946
Kay Sievers5becfb12012-07-09 12:15:42 -07001947 if (dict)
Tetsuo Handae36202a2019-02-22 18:59:40 +09001948 lflags |= LOG_NEWLINE;
Kay Sievers7ff95542012-05-03 02:29:13 +02001949
Petr Mladekba552392018-06-27 16:08:15 +02001950 return log_output(facility, level, lflags,
1951 dict, dictlen, text, text_len);
1952}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Petr Mladekba552392018-06-27 16:08:15 +02001954asmlinkage int vprintk_emit(int facility, int level,
1955 const char *dict, size_t dictlen,
1956 const char *fmt, va_list args)
1957{
1958 int printed_len;
Sergey Senozhatsky3ac37a92018-10-02 11:38:35 +09001959 bool in_sched = false, pending_output;
Petr Mladekba552392018-06-27 16:08:15 +02001960 unsigned long flags;
Sergey Senozhatsky3ac37a92018-10-02 11:38:35 +09001961 u64 curr_log_seq;
Petr Mladekba552392018-06-27 16:08:15 +02001962
Feng Tangc39ea0b2019-05-14 15:45:34 -07001963 /* Suppress unimportant messages after panic happens */
1964 if (unlikely(suppress_printk))
1965 return 0;
1966
Petr Mladekba552392018-06-27 16:08:15 +02001967 if (level == LOGLEVEL_SCHED) {
1968 level = LOGLEVEL_DEFAULT;
1969 in_sched = true;
1970 }
1971
1972 boot_delay_msec(level);
1973 printk_delay();
1974
1975 /* This stops the holder of console_sem just where we want him */
1976 logbuf_lock_irqsave(flags);
Sergey Senozhatsky3ac37a92018-10-02 11:38:35 +09001977 curr_log_seq = log_next_seq;
Petr Mladekba552392018-06-27 16:08:15 +02001978 printed_len = vprintk_store(facility, level, dict, dictlen, fmt, args);
Sergey Senozhatsky3ac37a92018-10-02 11:38:35 +09001979 pending_output = (curr_log_seq != log_next_seq);
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09001980 logbuf_unlock_irqrestore(flags);
Jan Kara939f04b2014-06-04 16:11:37 -07001981
Steven Rostedt458df9f2014-06-04 16:11:38 -07001982 /* If called from the scheduler, we can not call up(). */
Sergey Senozhatsky3ac37a92018-10-02 11:38:35 +09001983 if (!in_sched && pending_output) {
Jan Kara5874af22014-08-06 16:09:10 -07001984 /*
Sergey Senozhatskyfd5f7cd2018-01-16 13:47:16 +09001985 * Disable preemption to avoid being preempted while holding
1986 * console_sem which would prevent anyone from printing to
1987 * console
1988 */
1989 preempt_disable();
1990 /*
Andrew Mortond18bbc22014-07-02 15:22:38 -07001991 * Try to acquire and then immediately release the console
1992 * semaphore. The release will print out buffers and wake up
1993 * /dev/kmsg and syslog() users.
1994 */
Petr Mladekc162d5b2018-01-12 17:08:37 +01001995 if (console_trylock_spinning())
Andrew Mortond18bbc22014-07-02 15:22:38 -07001996 console_unlock();
Sergey Senozhatskyfd5f7cd2018-01-16 13:47:16 +09001997 preempt_enable();
Andrew Mortond18bbc22014-07-02 15:22:38 -07001998 }
Steven Rostedt458df9f2014-06-04 16:11:38 -07001999
Sergey Senozhatsky3ac37a92018-10-02 11:38:35 +09002000 if (pending_output)
2001 wake_up_klogd();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 return printed_len;
2003}
Kay Sievers7ff95542012-05-03 02:29:13 +02002004EXPORT_SYMBOL(vprintk_emit);
2005
2006asmlinkage int vprintk(const char *fmt, va_list args)
2007{
Sergey Senozhatskybd66a892016-12-27 23:16:04 +09002008 return vprintk_func(fmt, args);
Kay Sievers7ff95542012-05-03 02:29:13 +02002009}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010EXPORT_SYMBOL(vprintk);
2011
Linus Torvaldsa0cba212016-08-09 10:48:18 -07002012int vprintk_default(const char *fmt, va_list args)
Steven Rostedt (Red Hat)afdc34a2014-06-19 17:33:31 -04002013{
2014 int r;
2015
2016#ifdef CONFIG_KGDB_KDB
Petr Mladek34aaff42016-12-14 15:05:58 -08002017 /* Allow to pass printk() to kdb but avoid a recursion. */
2018 if (unlikely(kdb_trap_printk && kdb_printf_cpu < 0)) {
Daniel Thompsonf7d4ca82014-11-07 18:37:57 +00002019 r = vkdb_printf(KDB_MSGSRC_PRINTK, fmt, args);
Steven Rostedt (Red Hat)afdc34a2014-06-19 17:33:31 -04002020 return r;
2021 }
2022#endif
Linus Torvaldsa0cba212016-08-09 10:48:18 -07002023 r = vprintk_emit(0, LOGLEVEL_DEFAULT, NULL, 0, fmt, args);
Steven Rostedt (Red Hat)afdc34a2014-06-19 17:33:31 -04002024
2025 return r;
2026}
2027EXPORT_SYMBOL_GPL(vprintk_default);
2028
Kay Sievers7ff95542012-05-03 02:29:13 +02002029/**
2030 * printk - print a kernel message
2031 * @fmt: format string
2032 *
2033 * This is printk(). It can be called from any context. We want it to work.
2034 *
2035 * We try to grab the console_lock. If we succeed, it's easy - we log the
2036 * output and call the console drivers. If we fail to get the semaphore, we
2037 * place the output into the log buffer and return. The current holder of
2038 * the console_sem will notice the new output in console_unlock(); and will
2039 * send it to the consoles before releasing the lock.
2040 *
2041 * One effect of this deferred printing is that code which calls printk() and
2042 * then changes console_loglevel may break. This is because console_loglevel
2043 * is inspected when the actual printing occurs.
2044 *
2045 * See also:
2046 * printf(3)
2047 *
2048 * See the vsnprintf() documentation for format string extensions over C99.
2049 */
Andi Kleen722a9f92014-05-02 00:44:38 +02002050asmlinkage __visible int printk(const char *fmt, ...)
Kay Sievers7ff95542012-05-03 02:29:13 +02002051{
2052 va_list args;
2053 int r;
2054
Kay Sievers7ff95542012-05-03 02:29:13 +02002055 va_start(args, fmt);
Linus Torvaldsa0cba212016-08-09 10:48:18 -07002056 r = vprintk_func(fmt, args);
Kay Sievers7ff95542012-05-03 02:29:13 +02002057 va_end(args);
2058
2059 return r;
2060}
2061EXPORT_SYMBOL(printk);
Kay Sievers7f3a7812012-05-09 01:37:51 +02002062
Kay Sievers96efedf2012-07-16 18:35:29 -07002063#else /* CONFIG_PRINTK */
Matt Mackalld59745c2005-05-01 08:59:02 -07002064
Kay Sievers70498252012-07-16 18:35:29 -07002065#define LOG_LINE_MAX 0
2066#define PREFIX_MAX 0
Tetsuo Handae80c1a92018-12-04 19:00:01 +09002067#define printk_time false
Alex Elder249771b2014-08-06 16:09:08 -07002068
Kay Sievers96efedf2012-07-16 18:35:29 -07002069static u64 syslog_seq;
2070static u32 syslog_idx;
Kay Sieverseab07262012-07-16 18:35:30 -07002071static u64 console_seq;
2072static u32 console_idx;
Petr Mladekf92b0702018-09-13 14:34:06 +02002073static u64 exclusive_console_stop_seq;
Kay Sievers96efedf2012-07-16 18:35:29 -07002074static u64 log_first_seq;
2075static u32 log_first_idx;
2076static u64 log_next_seq;
Tejun Heo6fe29352015-06-25 15:01:30 -07002077static char *log_text(const struct printk_log *msg) { return NULL; }
2078static char *log_dict(const struct printk_log *msg) { return NULL; }
Joe Perches62e32ac2013-07-31 13:53:47 -07002079static struct printk_log *log_from_idx(u32 idx) { return NULL; }
Kay Sievers7f3a7812012-05-09 01:37:51 +02002080static u32 log_next(u32 idx) { return 0; }
Tejun Heo6fe29352015-06-25 15:01:30 -07002081static ssize_t msg_print_ext_header(char *buf, size_t size,
Linus Torvalds5aa068e2016-10-25 11:27:31 -07002082 struct printk_log *msg,
2083 u64 seq) { return 0; }
Tejun Heo6fe29352015-06-25 15:01:30 -07002084static ssize_t msg_print_ext_body(char *buf, size_t size,
2085 char *dict, size_t dict_len,
2086 char *text, size_t text_len) { return 0; }
Petr Mladekc162d5b2018-01-12 17:08:37 +01002087static void console_lock_spinning_enable(void) { }
2088static int console_lock_spinning_disable_and_check(void) { return 0; }
Sergey Senozhatskyd9c23522016-12-24 23:09:01 +09002089static void call_console_drivers(const char *ext_text, size_t ext_len,
Tejun Heo6fe29352015-06-25 15:01:30 -07002090 const char *text, size_t len) {}
Tetsuo Handae80c1a92018-12-04 19:00:01 +09002091static size_t msg_print_text(const struct printk_log *msg, bool syslog,
2092 bool time, char *buf, size_t size) { return 0; }
Petr Mladeka6ae9282018-09-10 15:52:06 +02002093static bool suppress_message_printing(int level) { return false; }
Matt Mackalld59745c2005-05-01 08:59:02 -07002094
Kay Sievers7f3a7812012-05-09 01:37:51 +02002095#endif /* CONFIG_PRINTK */
Matt Mackalld59745c2005-05-01 08:59:02 -07002096
Thomas Gleixnerd0380e62013-04-29 16:17:18 -07002097#ifdef CONFIG_EARLY_PRINTK
2098struct console *early_console;
2099
Andi Kleen722a9f92014-05-02 00:44:38 +02002100asmlinkage __visible void early_printk(const char *fmt, ...)
Thomas Gleixnerd0380e62013-04-29 16:17:18 -07002101{
2102 va_list ap;
Joe Perches1dc62442014-12-10 15:45:53 -08002103 char buf[512];
2104 int n;
2105
2106 if (!early_console)
2107 return;
Thomas Gleixnerd0380e62013-04-29 16:17:18 -07002108
2109 va_start(ap, fmt);
Joe Perches1dc62442014-12-10 15:45:53 -08002110 n = vscnprintf(buf, sizeof(buf), fmt, ap);
Thomas Gleixnerd0380e62013-04-29 16:17:18 -07002111 va_end(ap);
Joe Perches1dc62442014-12-10 15:45:53 -08002112
2113 early_console->write(early_console, buf, n);
Thomas Gleixnerd0380e62013-04-29 16:17:18 -07002114}
2115#endif
2116
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002117static int __add_preferred_console(char *name, int idx, char *options,
2118 char *brl_options)
2119{
2120 struct console_cmdline *c;
2121 int i;
2122
2123 /*
2124 * See if this tty is not yet registered, and
2125 * if we have a slot free.
2126 */
Petr Mladekdac8bbb2017-06-08 12:01:30 +02002127 for (i = 0, c = console_cmdline;
2128 i < MAX_CMDLINECONSOLES && c->name[0];
2129 i++, c++) {
Joe Perches23475402013-07-31 13:53:46 -07002130 if (strcmp(c->name, name) == 0 && c->index == idx) {
Petr Mladekdac8bbb2017-06-08 12:01:30 +02002131 if (!brl_options)
2132 preferred_console = i;
Joe Perches23475402013-07-31 13:53:46 -07002133 return 0;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002134 }
Joe Perches23475402013-07-31 13:53:46 -07002135 }
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002136 if (i == MAX_CMDLINECONSOLES)
2137 return -E2BIG;
2138 if (!brl_options)
Aleksey Makarovad86ee22017-03-15 13:28:51 +03002139 preferred_console = i;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002140 strlcpy(c->name, name, sizeof(c->name));
2141 c->options = options;
Joe Perchesbbeddf52013-07-31 13:53:45 -07002142 braille_set_options(c, brl_options);
2143
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002144 c->index = idx;
2145 return 0;
2146}
Sergey Senozhatskycca10d52017-12-21 14:41:49 +09002147
2148static int __init console_msg_format_setup(char *str)
2149{
2150 if (!strcmp(str, "syslog"))
2151 console_msg_format = MSG_FORMAT_SYSLOG;
2152 if (!strcmp(str, "default"))
2153 console_msg_format = MSG_FORMAT_DEFAULT;
2154 return 1;
2155}
2156__setup("console_msg_format=", console_msg_format_setup);
2157
John Z. Bohach2ea1c532006-03-24 03:18:19 -08002158/*
Alex Elder0b90fec2014-08-06 16:09:03 -07002159 * Set up a console. Called via do_early_param() in init/main.c
2160 * for each "console=" parameter in the boot command line.
John Z. Bohach2ea1c532006-03-24 03:18:19 -08002161 */
2162static int __init console_setup(char *str)
2163{
Alex Elder0b90fec2014-08-06 16:09:03 -07002164 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for "ttyS" */
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002165 char *s, *options, *brl_options = NULL;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08002166 int idx;
2167
Joe Perchesbbeddf52013-07-31 13:53:45 -07002168 if (_braille_console_setup(&str, &brl_options))
2169 return 1;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002170
John Z. Bohach2ea1c532006-03-24 03:18:19 -08002171 /*
2172 * Decode str into name, index, options.
2173 */
2174 if (str[0] >= '0' && str[0] <= '9') {
Yinghai Lueaa944a2007-07-15 23:37:27 -07002175 strcpy(buf, "ttyS");
2176 strncpy(buf + 4, str, sizeof(buf) - 5);
John Z. Bohach2ea1c532006-03-24 03:18:19 -08002177 } else {
Yinghai Lueaa944a2007-07-15 23:37:27 -07002178 strncpy(buf, str, sizeof(buf) - 1);
John Z. Bohach2ea1c532006-03-24 03:18:19 -08002179 }
Yinghai Lueaa944a2007-07-15 23:37:27 -07002180 buf[sizeof(buf) - 1] = 0;
Alex Elder249771b2014-08-06 16:09:08 -07002181 options = strchr(str, ',');
2182 if (options)
John Z. Bohach2ea1c532006-03-24 03:18:19 -08002183 *(options++) = 0;
2184#ifdef __sparc__
2185 if (!strcmp(str, "ttya"))
Yinghai Lueaa944a2007-07-15 23:37:27 -07002186 strcpy(buf, "ttyS0");
John Z. Bohach2ea1c532006-03-24 03:18:19 -08002187 if (!strcmp(str, "ttyb"))
Yinghai Lueaa944a2007-07-15 23:37:27 -07002188 strcpy(buf, "ttyS1");
John Z. Bohach2ea1c532006-03-24 03:18:19 -08002189#endif
Yinghai Lueaa944a2007-07-15 23:37:27 -07002190 for (s = buf; *s; s++)
Alex Elder249771b2014-08-06 16:09:08 -07002191 if (isdigit(*s) || *s == ',')
John Z. Bohach2ea1c532006-03-24 03:18:19 -08002192 break;
2193 idx = simple_strtoul(s, NULL, 10);
2194 *s = 0;
2195
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002196 __add_preferred_console(buf, idx, options, brl_options);
Markus Armbruster9e124fe2008-05-26 23:31:07 +01002197 console_set_on_cmdline = 1;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08002198 return 1;
2199}
2200__setup("console=", console_setup);
2201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202/**
Matt Mackall3c0547b2005-05-16 21:53:47 -07002203 * add_preferred_console - add a device to the list of preferred consoles.
Martin Waitzddad86c2005-11-13 16:08:14 -08002204 * @name: device name
2205 * @idx: device index
2206 * @options: options for this console
Matt Mackall3c0547b2005-05-16 21:53:47 -07002207 *
2208 * The last preferred console added will be used for kernel messages
2209 * and stdin/out/err for init. Normally this is used by console_setup
2210 * above to handle user-supplied console arguments; however it can also
2211 * be used by arch-specific code either to override the user or more
2212 * commonly to provide a default console (ie from PROM variables) when
2213 * the user has not supplied one.
2214 */
David S. Millerfb445ee2007-12-29 01:19:49 -08002215int add_preferred_console(char *name, int idx, char *options)
Matt Mackall3c0547b2005-05-16 21:53:47 -07002216{
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002217 return __add_preferred_console(name, idx, options, NULL);
Matt Mackall3c0547b2005-05-16 21:53:47 -07002218}
2219
Neil Zhangd25d9fe2014-08-06 16:09:12 -07002220bool console_suspend_enabled = true;
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002221EXPORT_SYMBOL(console_suspend_enabled);
2222
2223static int __init console_suspend_disable(char *str)
2224{
Neil Zhangd25d9fe2014-08-06 16:09:12 -07002225 console_suspend_enabled = false;
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002226 return 1;
2227}
2228__setup("no_console_suspend", console_suspend_disable);
Yanmin Zhang134620f2011-10-31 17:11:27 -07002229module_param_named(console_suspend, console_suspend_enabled,
2230 bool, S_IRUGO | S_IWUSR);
2231MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
2232 " and hibernate operations");
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002233
Matt Mackall3c0547b2005-05-16 21:53:47 -07002234/**
Linus Torvalds557240b2006-06-19 18:16:01 -07002235 * suspend_console - suspend the console subsystem
2236 *
2237 * This disables printk() while we go into suspend states
2238 */
2239void suspend_console(void)
2240{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002241 if (!console_suspend_enabled)
2242 return;
Tomeu Vizoso47319f72018-03-22 14:58:33 +01002243 pr_info("Suspending console(s) (use no_console_suspend to debug)\n");
Torben Hohnac751ef2011-01-25 15:07:35 -08002244 console_lock();
Linus Torvalds557240b2006-06-19 18:16:01 -07002245 console_suspended = 1;
Jan Karabd8d7cf2014-06-04 16:11:36 -07002246 up_console_sem();
Linus Torvalds557240b2006-06-19 18:16:01 -07002247}
2248
2249void resume_console(void)
2250{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002251 if (!console_suspend_enabled)
2252 return;
Jan Karabd8d7cf2014-06-04 16:11:36 -07002253 down_console_sem();
Linus Torvalds557240b2006-06-19 18:16:01 -07002254 console_suspended = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08002255 console_unlock();
Linus Torvalds557240b2006-06-19 18:16:01 -07002256}
2257
2258/**
Kevin Cernekee034260d2010-06-03 22:11:25 -07002259 * console_cpu_notify - print deferred console messages after CPU hotplug
Sebastian Andrzej Siewior90b14882016-11-03 15:49:58 +01002260 * @cpu: unused
Kevin Cernekee034260d2010-06-03 22:11:25 -07002261 *
2262 * If printk() is called from a CPU that is not online yet, the messages
Sergey Senozhatsky64ca7522017-01-21 19:47:29 +09002263 * will be printed on the console only if there are CON_ANYTIME consoles.
2264 * This function is called when a new CPU comes online (or fails to come
2265 * up) or goes offline.
Kevin Cernekee034260d2010-06-03 22:11:25 -07002266 */
Sebastian Andrzej Siewior90b14882016-11-03 15:49:58 +01002267static int console_cpu_notify(unsigned int cpu)
Kevin Cernekee034260d2010-06-03 22:11:25 -07002268{
Thomas Gleixnerf97960f2016-11-17 17:31:55 +01002269 if (!cpuhp_tasks_frozen) {
Sergey Senozhatsky64ca7522017-01-21 19:47:29 +09002270 /* If trylock fails, someone else is doing the printing */
2271 if (console_trylock())
2272 console_unlock();
Kevin Cernekee034260d2010-06-03 22:11:25 -07002273 }
Sebastian Andrzej Siewior90b14882016-11-03 15:49:58 +01002274 return 0;
Kevin Cernekee034260d2010-06-03 22:11:25 -07002275}
2276
2277/**
Torben Hohnac751ef2011-01-25 15:07:35 -08002278 * console_lock - lock the console system for exclusive use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002280 * Acquires a lock which guarantees that the caller has
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 * exclusive access to the console system and the console_drivers list.
2282 *
2283 * Can sleep, returns nothing.
2284 */
Torben Hohnac751ef2011-01-25 15:07:35 -08002285void console_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
Daniel Vetter6b898c02012-09-17 23:03:31 +00002287 might_sleep();
2288
Jan Karabd8d7cf2014-06-04 16:11:36 -07002289 down_console_sem();
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01002290 if (console_suspended)
2291 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 console_locked = 1;
2293 console_may_schedule = 1;
2294}
Torben Hohnac751ef2011-01-25 15:07:35 -08002295EXPORT_SYMBOL(console_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
Torben Hohnac751ef2011-01-25 15:07:35 -08002297/**
2298 * console_trylock - try to lock the console system for exclusive use.
2299 *
Alex Elder0b90fec2014-08-06 16:09:03 -07002300 * Try to acquire a lock which guarantees that the caller has exclusive
2301 * access to the console system and the console_drivers list.
Torben Hohnac751ef2011-01-25 15:07:35 -08002302 *
2303 * returns 1 on success, and 0 on failure to acquire the lock.
2304 */
2305int console_trylock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
Jan Karabd8d7cf2014-06-04 16:11:36 -07002307 if (down_trylock_console_sem())
Torben Hohnac751ef2011-01-25 15:07:35 -08002308 return 0;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01002309 if (console_suspended) {
Jan Karabd8d7cf2014-06-04 16:11:36 -07002310 up_console_sem();
Torben Hohnac751ef2011-01-25 15:07:35 -08002311 return 0;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01002312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 console_locked = 1;
Sergey Senozhatskyfd5f7cd2018-01-16 13:47:16 +09002314 console_may_schedule = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08002315 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316}
Torben Hohnac751ef2011-01-25 15:07:35 -08002317EXPORT_SYMBOL(console_trylock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
2319int is_console_locked(void)
2320{
2321 return console_locked;
2322}
Hans de Goeded48de542018-06-28 15:20:27 +02002323EXPORT_SYMBOL(is_console_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Sergey Senozhatskya8199372016-03-17 14:21:20 -07002325/*
2326 * Check if we have any console that is capable of printing while cpu is
2327 * booting or shutting down. Requires console_sem.
2328 */
2329static int have_callable_console(void)
2330{
2331 struct console *con;
2332
2333 for_each_console(con)
Sergey Senozhatskyadaf6592016-03-17 14:21:27 -07002334 if ((con->flags & CON_ENABLED) &&
2335 (con->flags & CON_ANYTIME))
Sergey Senozhatskya8199372016-03-17 14:21:20 -07002336 return 1;
2337
2338 return 0;
2339}
2340
2341/*
2342 * Can we actually use the console at this time on this cpu?
2343 *
2344 * Console drivers may assume that per-cpu resources have been allocated. So
2345 * unless they're explicitly marked as being able to cope (CON_ANYTIME) don't
2346 * call them until this CPU is officially up.
2347 */
2348static inline int can_use_console(void)
2349{
2350 return cpu_online(raw_smp_processor_id()) || have_callable_console();
2351}
2352
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353/**
Torben Hohnac751ef2011-01-25 15:07:35 -08002354 * console_unlock - unlock the console system
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002356 * Releases the console_lock which the caller holds on the console system
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 * and the console driver list.
2358 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002359 * While the console_lock was held, console output may have been buffered
2360 * by printk(). If this is the case, console_unlock(); emits
2361 * the output prior to releasing the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 *
Kay Sievers7f3a7812012-05-09 01:37:51 +02002363 * If there is output waiting, we wake /dev/kmsg and syslog() users.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002365 * console_unlock(); may be called from any context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 */
Torben Hohnac751ef2011-01-25 15:07:35 -08002367void console_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368{
Tejun Heo6fe29352015-06-25 15:01:30 -07002369 static char ext_text[CONSOLE_EXT_LOG_MAX];
Kay Sievers70498252012-07-16 18:35:29 -07002370 static char text[LOG_LINE_MAX + PREFIX_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 unsigned long flags;
Tejun Heo8d91f8b2016-01-15 16:58:24 -08002372 bool do_cond_resched, retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
Linus Torvalds557240b2006-06-19 18:16:01 -07002374 if (console_suspended) {
Jan Karabd8d7cf2014-06-04 16:11:36 -07002375 up_console_sem();
Linus Torvalds557240b2006-06-19 18:16:01 -07002376 return;
2377 }
Antonino A. Daplas78944e542006-08-05 12:14:16 -07002378
Tejun Heo8d91f8b2016-01-15 16:58:24 -08002379 /*
Petr Mladek257ab442017-03-24 17:14:05 +01002380 * Console drivers are called with interrupts disabled, so
Tejun Heo8d91f8b2016-01-15 16:58:24 -08002381 * @console_may_schedule should be cleared before; however, we may
2382 * end up dumping a lot of lines, for example, if called from
2383 * console registration path, and should invoke cond_resched()
2384 * between lines if allowable. Not doing so can cause a very long
2385 * scheduling stall on a slow console leading to RCU stall and
2386 * softlockup warnings which exacerbate the issue with more
2387 * messages practically incapacitating the system.
Petr Mladek257ab442017-03-24 17:14:05 +01002388 *
2389 * console_trylock() is not able to detect the preemptive
2390 * context reliably. Therefore the value must be stored before
2391 * and cleared after the the "again" goto label.
Tejun Heo8d91f8b2016-01-15 16:58:24 -08002392 */
2393 do_cond_resched = console_may_schedule;
Petr Mladek257ab442017-03-24 17:14:05 +01002394again:
Antonino A. Daplas78944e542006-08-05 12:14:16 -07002395 console_may_schedule = 0;
2396
Sergey Senozhatskya8199372016-03-17 14:21:20 -07002397 /*
2398 * We released the console_sem lock, so we need to recheck if
2399 * cpu is online and (if not) is there at least one CON_ANYTIME
2400 * console.
2401 */
2402 if (!can_use_console()) {
2403 console_locked = 0;
2404 up_console_sem();
2405 return;
2406 }
2407
Kay Sievers7ff95542012-05-03 02:29:13 +02002408 for (;;) {
Joe Perches62e32ac2013-07-31 13:53:47 -07002409 struct printk_log *msg;
Tejun Heo6fe29352015-06-25 15:01:30 -07002410 size_t ext_len = 0;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02002411 size_t len;
Kay Sievers7ff95542012-05-03 02:29:13 +02002412
Sergey Senozhatskyf9752372016-12-27 23:16:09 +09002413 printk_safe_enter_irqsave(flags);
2414 raw_spin_lock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02002415 if (console_seq < log_first_seq) {
He Zhe51a72ab2018-09-30 00:45:51 +08002416 len = sprintf(text,
2417 "** %llu printk messages dropped **\n",
2418 log_first_seq - console_seq);
Will Deacon84b5ec82014-06-04 16:11:45 -07002419
Kay Sievers7ff95542012-05-03 02:29:13 +02002420 /* messages are gone, move to first one */
2421 console_seq = log_first_seq;
2422 console_idx = log_first_idx;
Will Deacon84b5ec82014-06-04 16:11:45 -07002423 } else {
2424 len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02002425 }
Kay Sievers084681d2012-06-28 09:38:53 +02002426skip:
Kay Sievers7ff95542012-05-03 02:29:13 +02002427 if (console_seq == log_next_seq)
2428 break;
2429
2430 msg = log_from_idx(console_idx);
Petr Mladeka6ae9282018-09-10 15:52:06 +02002431 if (suppress_message_printing(msg->level)) {
Kay Sievers084681d2012-06-28 09:38:53 +02002432 /*
Petr Mladeka6ae9282018-09-10 15:52:06 +02002433 * Skip record we have buffered and already printed
2434 * directly to the console when we received it, and
2435 * record that has level above the console loglevel.
Kay Sievers084681d2012-06-28 09:38:53 +02002436 */
2437 console_idx = log_next(console_idx);
2438 console_seq++;
2439 goto skip;
2440 }
Kay Sievers649e6ee2012-05-10 04:30:45 +02002441
Petr Mladekf92b0702018-09-13 14:34:06 +02002442 /* Output to all consoles once old messages replayed. */
2443 if (unlikely(exclusive_console &&
2444 console_seq >= exclusive_console_stop_seq)) {
2445 exclusive_console = NULL;
2446 }
2447
Sergey Senozhatskycca10d52017-12-21 14:41:49 +09002448 len += msg_print_text(msg,
2449 console_msg_format & MSG_FORMAT_SYSLOG,
Tetsuo Handae80c1a92018-12-04 19:00:01 +09002450 printk_time, text + len, sizeof(text) - len);
Tejun Heo6fe29352015-06-25 15:01:30 -07002451 if (nr_ext_console_drivers) {
2452 ext_len = msg_print_ext_header(ext_text,
2453 sizeof(ext_text),
Linus Torvalds5aa068e2016-10-25 11:27:31 -07002454 msg, console_seq);
Tejun Heo6fe29352015-06-25 15:01:30 -07002455 ext_len += msg_print_ext_body(ext_text + ext_len,
2456 sizeof(ext_text) - ext_len,
2457 log_dict(msg), msg->dict_len,
2458 log_text(msg), msg->text_len);
2459 }
Kay Sievers7ff95542012-05-03 02:29:13 +02002460 console_idx = log_next(console_idx);
2461 console_seq++;
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002462 raw_spin_unlock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02002463
Steven Rostedt (VMware)dbdda842018-01-10 14:24:17 +01002464 /*
2465 * While actively printing out messages, if another printk()
2466 * were to occur on another CPU, it may wait for this one to
2467 * finish. This task can not be preempted if there is a
2468 * waiter waiting to take over.
2469 */
Petr Mladekc162d5b2018-01-12 17:08:37 +01002470 console_lock_spinning_enable();
Steven Rostedt (VMware)dbdda842018-01-10 14:24:17 +01002471
Steven Rostedt81d68a92008-05-12 21:20:42 +02002472 stop_critical_timings(); /* don't trace print latency */
Sergey Senozhatskyd9c23522016-12-24 23:09:01 +09002473 call_console_drivers(ext_text, ext_len, text, len);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002474 start_critical_timings();
Steven Rostedt (VMware)dbdda842018-01-10 14:24:17 +01002475
Petr Mladekc162d5b2018-01-12 17:08:37 +01002476 if (console_lock_spinning_disable_and_check()) {
2477 printk_safe_exit_irqrestore(flags);
Sergey Senozhatsky43a17112018-04-19 10:42:50 +09002478 return;
Petr Mladekc162d5b2018-01-12 17:08:37 +01002479 }
Steven Rostedt (VMware)dbdda842018-01-10 14:24:17 +01002480
Sergey Senozhatskyf9752372016-12-27 23:16:09 +09002481 printk_safe_exit_irqrestore(flags);
Tejun Heo8d91f8b2016-01-15 16:58:24 -08002482
2483 if (do_cond_resched)
2484 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 }
Steven Rostedt (VMware)dbdda842018-01-10 14:24:17 +01002486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 console_locked = 0;
Feng Tangfe3d8ad2011-03-22 16:34:21 -07002488
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002489 raw_spin_unlock(&logbuf_lock);
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002490
Jan Karabd8d7cf2014-06-04 16:11:36 -07002491 up_console_sem();
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002492
2493 /*
2494 * Someone could have filled up the buffer again, so re-check if there's
2495 * something to flush. In case we cannot trylock the console_sem again,
2496 * there's a new owner and the console_unlock() from them will do the
2497 * flush, no worries.
2498 */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002499 raw_spin_lock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02002500 retry = console_seq != log_next_seq;
Sergey Senozhatskyf9752372016-12-27 23:16:09 +09002501 raw_spin_unlock(&logbuf_lock);
2502 printk_safe_exit_irqrestore(flags);
Peter Zijlstra09dc3cf2011-12-08 14:34:13 -08002503
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002504 if (retry && console_trylock())
2505 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
Torben Hohnac751ef2011-01-25 15:07:35 -08002507EXPORT_SYMBOL(console_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Martin Waitzddad86c2005-11-13 16:08:14 -08002509/**
2510 * console_conditional_schedule - yield the CPU if required
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 *
2512 * If the console code is currently allowed to sleep, and
2513 * if this CPU should yield the CPU to another task, do
2514 * so here.
2515 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002516 * Must be called within console_lock();.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 */
2518void __sched console_conditional_schedule(void)
2519{
2520 if (console_may_schedule)
2521 cond_resched();
2522}
2523EXPORT_SYMBOL(console_conditional_schedule);
2524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525void console_unblank(void)
2526{
2527 struct console *c;
2528
2529 /*
2530 * console_unblank can no longer be called in interrupt context unless
2531 * oops_in_progress is set to 1..
2532 */
2533 if (oops_in_progress) {
Jan Karabd8d7cf2014-06-04 16:11:36 -07002534 if (down_trylock_console_sem() != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 return;
2536 } else
Torben Hohnac751ef2011-01-25 15:07:35 -08002537 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
2539 console_locked = 1;
2540 console_may_schedule = 0;
Robin Getz4d091612009-07-01 21:08:37 -04002541 for_each_console(c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 if ((c->flags & CON_ENABLED) && c->unblank)
2543 c->unblank();
Torben Hohnac751ef2011-01-25 15:07:35 -08002544 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Tejun Heo8d91f8b2016-01-15 16:58:24 -08002547/**
2548 * console_flush_on_panic - flush console content on panic
Feng Tangde6da1e2019-05-17 14:31:50 -07002549 * @mode: flush all messages in buffer or just the pending ones
Tejun Heo8d91f8b2016-01-15 16:58:24 -08002550 *
2551 * Immediately output all pending messages no matter what.
2552 */
Feng Tangde6da1e2019-05-17 14:31:50 -07002553void console_flush_on_panic(enum con_flush_mode mode)
Tejun Heo8d91f8b2016-01-15 16:58:24 -08002554{
2555 /*
2556 * If someone else is holding the console lock, trylock will fail
2557 * and may_schedule may be set. Ignore and proceed to unlock so
2558 * that messages are flushed out. As this can be called from any
2559 * context and we don't want to get preempted while flushing,
2560 * ensure may_schedule is cleared.
2561 */
2562 console_trylock();
2563 console_may_schedule = 0;
Feng Tangde6da1e2019-05-17 14:31:50 -07002564
2565 if (mode == CONSOLE_REPLAY_ALL) {
2566 unsigned long flags;
2567
2568 logbuf_lock_irqsave(flags);
2569 console_seq = log_first_seq;
2570 console_idx = log_first_idx;
2571 logbuf_unlock_irqrestore(flags);
2572 }
Tejun Heo8d91f8b2016-01-15 16:58:24 -08002573 console_unlock();
2574}
2575
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576/*
2577 * Return the console tty driver structure and its associated index
2578 */
2579struct tty_driver *console_device(int *index)
2580{
2581 struct console *c;
2582 struct tty_driver *driver = NULL;
2583
Torben Hohnac751ef2011-01-25 15:07:35 -08002584 console_lock();
Robin Getz4d091612009-07-01 21:08:37 -04002585 for_each_console(c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 if (!c->device)
2587 continue;
2588 driver = c->device(c, index);
2589 if (driver)
2590 break;
2591 }
Torben Hohnac751ef2011-01-25 15:07:35 -08002592 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 return driver;
2594}
2595
2596/*
2597 * Prevent further output on the passed console device so that (for example)
2598 * serial drivers can disable console output before suspending a port, and can
2599 * re-enable output afterwards.
2600 */
2601void console_stop(struct console *console)
2602{
Torben Hohnac751ef2011-01-25 15:07:35 -08002603 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 console->flags &= ~CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08002605 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606}
2607EXPORT_SYMBOL(console_stop);
2608
2609void console_start(struct console *console)
2610{
Torben Hohnac751ef2011-01-25 15:07:35 -08002611 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 console->flags |= CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08002613 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614}
2615EXPORT_SYMBOL(console_start);
2616
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07002617static int __read_mostly keep_bootcon;
2618
2619static int __init keep_bootcon_setup(char *str)
2620{
2621 keep_bootcon = 1;
Andrew Morton27083ba2013-11-12 15:08:50 -08002622 pr_info("debug: skip boot console de-registration.\n");
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07002623
2624 return 0;
2625}
2626
2627early_param("keep_bootcon", keep_bootcon_setup);
2628
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629/*
2630 * The console driver calls this routine during kernel initialization
2631 * to register the console printing procedure with printk() and to
2632 * print any messages that were printed by the kernel before the
2633 * console driver was initialized.
Robin Getz4d091612009-07-01 21:08:37 -04002634 *
2635 * This can happen pretty early during the boot process (because of
2636 * early_printk) - sometimes before setup_arch() completes - be careful
2637 * of what kernel features are used - they may not be initialised yet.
2638 *
2639 * There are two types of consoles - bootconsoles (early_printk) and
2640 * "real" consoles (everything which is not a bootconsole) which are
2641 * handled differently.
2642 * - Any number of bootconsoles can be registered at any time.
2643 * - As soon as a "real" console is registered, all bootconsoles
2644 * will be unregistered automatically.
2645 * - Once a "real" console is registered, any attempt to register a
2646 * bootconsoles will be rejected
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 */
Robin Getz4d091612009-07-01 21:08:37 -04002648void register_console(struct console *newcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649{
Jesper Juhl40dc5652005-10-30 15:02:46 -08002650 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 unsigned long flags;
Robin Getz4d091612009-07-01 21:08:37 -04002652 struct console *bcon = NULL;
Joe Perches23475402013-07-31 13:53:46 -07002653 struct console_cmdline *c;
Aleksey Makarovb077baf2017-03-15 13:28:50 +03002654 static bool has_preferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
Andreas Bießmann16cf48a2013-08-02 12:23:34 +02002656 if (console_drivers)
2657 for_each_console(bcon)
2658 if (WARN(bcon == newcon,
2659 "console '%s%d' already registered\n",
2660 bcon->name, bcon->index))
2661 return;
2662
Robin Getz4d091612009-07-01 21:08:37 -04002663 /*
2664 * before we register a new CON_BOOT console, make sure we don't
2665 * already have a valid console
2666 */
2667 if (console_drivers && newcon->flags & CON_BOOT) {
2668 /* find the last or real console */
2669 for_each_console(bcon) {
2670 if (!(bcon->flags & CON_BOOT)) {
Andrew Morton27083ba2013-11-12 15:08:50 -08002671 pr_info("Too late to register bootconsole %s%d\n",
Robin Getz4d091612009-07-01 21:08:37 -04002672 newcon->name, newcon->index);
2673 return;
2674 }
2675 }
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002676 }
2677
Robin Getz4d091612009-07-01 21:08:37 -04002678 if (console_drivers && console_drivers->flags & CON_BOOT)
2679 bcon = console_drivers;
2680
Aleksey Makarovb077baf2017-03-15 13:28:50 +03002681 if (!has_preferred || bcon || !console_drivers)
Aleksey Makarovad86ee22017-03-15 13:28:51 +03002682 has_preferred = preferred_console >= 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
2684 /*
2685 * See if we want to use this console driver. If we
2686 * didn't select a console we take the first one
2687 * that registers here.
2688 */
Aleksey Makarovb077baf2017-03-15 13:28:50 +03002689 if (!has_preferred) {
Robin Getz4d091612009-07-01 21:08:37 -04002690 if (newcon->index < 0)
2691 newcon->index = 0;
2692 if (newcon->setup == NULL ||
2693 newcon->setup(newcon, NULL) == 0) {
2694 newcon->flags |= CON_ENABLED;
2695 if (newcon->device) {
2696 newcon->flags |= CON_CONSDEV;
Aleksey Makarovb077baf2017-03-15 13:28:50 +03002697 has_preferred = true;
Jan Kiszkacd3a1b82008-05-12 21:21:04 +02002698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 }
2700 }
2701
2702 /*
Petr Mladekdac8bbb2017-06-08 12:01:30 +02002703 * See if this console matches one we selected on
2704 * the command line.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 */
Petr Mladekdac8bbb2017-06-08 12:01:30 +02002706 for (i = 0, c = console_cmdline;
2707 i < MAX_CMDLINECONSOLES && c->name[0];
2708 i++, c++) {
Peter Hurleyc7cef0a2015-03-09 16:27:12 -04002709 if (!newcon->match ||
2710 newcon->match(newcon, c->name, c->index, c->options) != 0) {
2711 /* default matching */
2712 BUILD_BUG_ON(sizeof(c->name) != sizeof(newcon->name));
2713 if (strcmp(c->name, newcon->name) != 0)
2714 continue;
2715 if (newcon->index >= 0 &&
2716 newcon->index != c->index)
2717 continue;
2718 if (newcon->index < 0)
2719 newcon->index = c->index;
Joe Perchesbbeddf52013-07-31 13:53:45 -07002720
Peter Hurleyc7cef0a2015-03-09 16:27:12 -04002721 if (_braille_register_console(newcon, c))
2722 return;
Joe Perchesbbeddf52013-07-31 13:53:45 -07002723
Peter Hurleyc7cef0a2015-03-09 16:27:12 -04002724 if (newcon->setup &&
2725 newcon->setup(newcon, c->options) != 0)
2726 break;
2727 }
2728
Robin Getz4d091612009-07-01 21:08:37 -04002729 newcon->flags |= CON_ENABLED;
Aleksey Makarovad86ee22017-03-15 13:28:51 +03002730 if (i == preferred_console) {
Robin Getz4d091612009-07-01 21:08:37 -04002731 newcon->flags |= CON_CONSDEV;
Aleksey Makarovb077baf2017-03-15 13:28:50 +03002732 has_preferred = true;
Greg Edwardsab4af032005-06-23 00:09:05 -07002733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 break;
2735 }
2736
Robin Getz4d091612009-07-01 21:08:37 -04002737 if (!(newcon->flags & CON_ENABLED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 return;
2739
Robin Getz8259cf42009-07-09 13:08:37 -04002740 /*
2741 * If we have a bootconsole, and are switching to a real console,
2742 * don't print everything out again, since when the boot console, and
2743 * the real console are the same physical device, it's annoying to
2744 * see the beginning boot messages twice
2745 */
2746 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
Robin Getz4d091612009-07-01 21:08:37 -04002747 newcon->flags &= ~CON_PRINTBUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
2749 /*
2750 * Put this console in the list - keep the
2751 * preferred driver at the head of the list.
2752 */
Torben Hohnac751ef2011-01-25 15:07:35 -08002753 console_lock();
Robin Getz4d091612009-07-01 21:08:37 -04002754 if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
2755 newcon->next = console_drivers;
2756 console_drivers = newcon;
2757 if (newcon->next)
2758 newcon->next->flags &= ~CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 } else {
Robin Getz4d091612009-07-01 21:08:37 -04002760 newcon->next = console_drivers->next;
2761 console_drivers->next = newcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 }
Tejun Heo6fe29352015-06-25 15:01:30 -07002763
2764 if (newcon->flags & CON_EXTENDED)
Sergey Senozhatsky96278082018-10-02 11:38:34 +09002765 nr_ext_console_drivers++;
Tejun Heo6fe29352015-06-25 15:01:30 -07002766
Robin Getz4d091612009-07-01 21:08:37 -04002767 if (newcon->flags & CON_PRINTBUFFER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 /*
Torben Hohnac751ef2011-01-25 15:07:35 -08002769 * console_unlock(); will print out the buffered messages
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 * for us.
2771 */
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09002772 logbuf_lock_irqsave(flags);
Kay Sievers7ff95542012-05-03 02:29:13 +02002773 console_seq = syslog_seq;
2774 console_idx = syslog_idx;
Feng Tangfe3d8ad2011-03-22 16:34:21 -07002775 /*
2776 * We're about to replay the log buffer. Only do this to the
2777 * just-registered console to avoid excessive message spam to
2778 * the already-registered consoles.
Sergey Senozhatsky884e3702018-09-28 18:53:04 +09002779 *
2780 * Set exclusive_console with disabled interrupts to reduce
2781 * race window with eventual console_flush_on_panic() that
2782 * ignores console_lock.
Feng Tangfe3d8ad2011-03-22 16:34:21 -07002783 */
2784 exclusive_console = newcon;
Petr Mladekf92b0702018-09-13 14:34:06 +02002785 exclusive_console_stop_seq = console_seq;
Sergey Senozhatsky884e3702018-09-28 18:53:04 +09002786 logbuf_unlock_irqrestore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 }
Torben Hohnac751ef2011-01-25 15:07:35 -08002788 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01002789 console_sysfs_notify();
Robin Getz8259cf42009-07-09 13:08:37 -04002790
2791 /*
2792 * By unregistering the bootconsoles after we enable the real console
2793 * we get the "console xxx enabled" message on all the consoles -
2794 * boot consoles, real consoles, etc - this is to ensure that end
2795 * users know there might be something in the kernel's log buffer that
2796 * went to the bootconsole (that they do not see on the real console)
2797 */
Andrew Morton27083ba2013-11-12 15:08:50 -08002798 pr_info("%sconsole [%s%d] enabled\n",
Kees Cook6b802392013-11-12 15:08:49 -08002799 (newcon->flags & CON_BOOT) ? "boot" : "" ,
2800 newcon->name, newcon->index);
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07002801 if (bcon &&
2802 ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
2803 !keep_bootcon) {
Kees Cook6b802392013-11-12 15:08:49 -08002804 /* We need to iterate through all boot consoles, to make
2805 * sure we print everything out, before we unregister them.
Robin Getz8259cf42009-07-09 13:08:37 -04002806 */
Robin Getz8259cf42009-07-09 13:08:37 -04002807 for_each_console(bcon)
2808 if (bcon->flags & CON_BOOT)
2809 unregister_console(bcon);
Robin Getz8259cf42009-07-09 13:08:37 -04002810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811}
2812EXPORT_SYMBOL(register_console);
2813
Jesper Juhl40dc5652005-10-30 15:02:46 -08002814int unregister_console(struct console *console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815{
Jesper Juhl40dc5652005-10-30 15:02:46 -08002816 struct console *a, *b;
Joe Perchesbbeddf52013-07-31 13:53:45 -07002817 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
Andrew Morton27083ba2013-11-12 15:08:50 -08002819 pr_info("%sconsole [%s%d] disabled\n",
Kees Cook6b802392013-11-12 15:08:49 -08002820 (console->flags & CON_BOOT) ? "boot" : "" ,
2821 console->name, console->index);
2822
Joe Perchesbbeddf52013-07-31 13:53:45 -07002823 res = _braille_unregister_console(console);
2824 if (res)
2825 return res;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002826
Joe Perchesbbeddf52013-07-31 13:53:45 -07002827 res = 1;
Torben Hohnac751ef2011-01-25 15:07:35 -08002828 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 if (console_drivers == console) {
2830 console_drivers=console->next;
2831 res = 0;
Benjamin Herrenschmidte9b15b52005-11-23 13:37:44 -08002832 } else if (console_drivers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 for (a=console_drivers->next, b=console_drivers ;
2834 a; b=a, a=b->next) {
2835 if (a == console) {
2836 b->next = a->next;
2837 res = 0;
2838 break;
Jesper Juhl40dc5652005-10-30 15:02:46 -08002839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 }
2841 }
Jesper Juhl40dc5652005-10-30 15:02:46 -08002842
Tejun Heo6fe29352015-06-25 15:01:30 -07002843 if (!res && (console->flags & CON_EXTENDED))
2844 nr_ext_console_drivers--;
2845
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002846 /*
Greg Edwardsab4af032005-06-23 00:09:05 -07002847 * If this isn't the last console and it has CON_CONSDEV set, we
2848 * need to set it on the next preferred console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 */
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002850 if (console_drivers != NULL && console->flags & CON_CONSDEV)
Greg Edwardsab4af032005-06-23 00:09:05 -07002851 console_drivers->flags |= CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Stephen Chivers7fa21dd2014-05-14 08:04:39 +10002853 console->flags &= ~CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08002854 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01002855 console_sysfs_notify();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 return res;
2857}
2858EXPORT_SYMBOL(unregister_console);
Matt Mackalld59745c2005-05-01 08:59:02 -07002859
Thierry Reding81cc26f2016-01-15 16:58:21 -08002860/*
Nicolas Pitre0c688612017-04-12 18:37:14 -04002861 * Initialize the console device. This is called *early*, so
2862 * we can't necessarily depend on lots of kernel help here.
2863 * Just do some early initializations, and do the complex setup
2864 * later.
2865 */
2866void __init console_init(void)
2867{
Abderrahmane Benbachir58eacff2018-03-22 20:33:28 -04002868 int ret;
Ard Biesheuvel1b1eeca2018-08-21 21:56:13 -07002869 initcall_t call;
2870 initcall_entry_t *ce;
Nicolas Pitre0c688612017-04-12 18:37:14 -04002871
2872 /* Setup the default TTY line discipline. */
2873 n_tty_init();
2874
2875 /*
2876 * set up the console device so that later boot sequences can
2877 * inform about problems etc..
2878 */
Ard Biesheuvel1b1eeca2018-08-21 21:56:13 -07002879 ce = __con_initcall_start;
Abderrahmane Benbachir58eacff2018-03-22 20:33:28 -04002880 trace_initcall_level("console");
Ard Biesheuvel1b1eeca2018-08-21 21:56:13 -07002881 while (ce < __con_initcall_end) {
2882 call = initcall_from_entry(ce);
2883 trace_initcall_start(call);
2884 ret = call();
2885 trace_initcall_finish(call, ret);
2886 ce++;
Nicolas Pitre0c688612017-04-12 18:37:14 -04002887 }
2888}
2889
2890/*
Thierry Reding81cc26f2016-01-15 16:58:21 -08002891 * Some boot consoles access data that is in the init section and which will
2892 * be discarded after the initcalls have been run. To make sure that no code
2893 * will access this data, unregister the boot consoles in a late initcall.
2894 *
2895 * If for some reason, such as deferred probe or the driver being a loadable
2896 * module, the real console hasn't registered yet at this point, there will
2897 * be a brief interval in which no messages are logged to the console, which
2898 * makes it difficult to diagnose problems that occur during this time.
2899 *
2900 * To mitigate this problem somewhat, only unregister consoles whose memory
Matt Redfearn2b1be682017-07-14 14:51:12 +02002901 * intersects with the init section. Note that all other boot consoles will
2902 * get unregistred when the real preferred console is registered.
Thierry Reding81cc26f2016-01-15 16:58:21 -08002903 */
Kevin Cernekee034260d2010-06-03 22:11:25 -07002904static int __init printk_late_init(void)
Robin Getz0c5564b2007-08-20 15:22:47 -04002905{
Robin Getz4d091612009-07-01 21:08:37 -04002906 struct console *con;
Sebastian Andrzej Siewior90b14882016-11-03 15:49:58 +01002907 int ret;
Robin Getz4d091612009-07-01 21:08:37 -04002908
2909 for_each_console(con) {
Petr Mladek5a814232017-07-14 14:51:13 +02002910 if (!(con->flags & CON_BOOT))
2911 continue;
2912
2913 /* Check addresses that might be used for enabled consoles. */
2914 if (init_section_intersects(con, sizeof(*con)) ||
2915 init_section_contains(con->write, 0) ||
2916 init_section_contains(con->read, 0) ||
2917 init_section_contains(con->device, 0) ||
2918 init_section_contains(con->unblank, 0) ||
2919 init_section_contains(con->data, 0)) {
Thierry Reding81cc26f2016-01-15 16:58:21 -08002920 /*
Matt Redfearn2b1be682017-07-14 14:51:12 +02002921 * Please, consider moving the reported consoles out
2922 * of the init section.
Thierry Reding81cc26f2016-01-15 16:58:21 -08002923 */
Matt Redfearn2b1be682017-07-14 14:51:12 +02002924 pr_warn("bootconsole [%s%d] uses init memory and must be disabled even before the real one is ready\n",
2925 con->name, con->index);
2926 unregister_console(con);
Robin Getzcb00e992007-08-21 23:14:58 -04002927 }
Robin Getz0c5564b2007-08-20 15:22:47 -04002928 }
Sebastian Andrzej Siewior90b14882016-11-03 15:49:58 +01002929 ret = cpuhp_setup_state_nocalls(CPUHP_PRINTK_DEAD, "printk:dead", NULL,
2930 console_cpu_notify);
2931 WARN_ON(ret < 0);
2932 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "printk:online",
2933 console_cpu_notify, NULL);
2934 WARN_ON(ret < 0);
Robin Getz0c5564b2007-08-20 15:22:47 -04002935 return 0;
2936}
Kevin Cernekee034260d2010-06-03 22:11:25 -07002937late_initcall(printk_late_init);
Robin Getz0c5564b2007-08-20 15:22:47 -04002938
Joe Perches7ef3d2f2008-02-08 04:21:25 -08002939#if defined CONFIG_PRINTK
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002940/*
2941 * Delayed printk version, for scheduler-internal messages:
2942 */
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002943#define PRINTK_PENDING_WAKEUP 0x01
Steven Rostedt458df9f2014-06-04 16:11:38 -07002944#define PRINTK_PENDING_OUTPUT 0x02
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002945
2946static DEFINE_PER_CPU(int, printk_pending);
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002947
2948static void wake_up_klogd_work_func(struct irq_work *irq_work)
2949{
2950 int pending = __this_cpu_xchg(printk_pending, 0);
2951
Steven Rostedt458df9f2014-06-04 16:11:38 -07002952 if (pending & PRINTK_PENDING_OUTPUT) {
2953 /* If trylock fails, someone else is doing the printing */
2954 if (console_trylock())
2955 console_unlock();
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002956 }
2957
2958 if (pending & PRINTK_PENDING_WAKEUP)
2959 wake_up_interruptible(&log_wait);
2960}
2961
2962static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) = {
2963 .func = wake_up_klogd_work_func,
2964 .flags = IRQ_WORK_LAZY,
2965};
2966
2967void wake_up_klogd(void)
2968{
2969 preempt_disable();
2970 if (waitqueue_active(&log_wait)) {
2971 this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP);
Christoph Lameterbb964a92014-08-17 12:30:24 -05002972 irq_work_queue(this_cpu_ptr(&wake_up_klogd_work));
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002973 }
2974 preempt_enable();
2975}
Dave Young717115e2008-07-25 01:45:58 -07002976
Petr Mladeka338f842018-06-27 16:08:16 +02002977void defer_console_output(void)
2978{
2979 preempt_disable();
2980 __this_cpu_or(printk_pending, PRINTK_PENDING_OUTPUT);
2981 irq_work_queue(this_cpu_ptr(&wake_up_klogd_work));
2982 preempt_enable();
2983}
2984
Petr Mladek719f6a72017-04-20 10:52:31 +02002985int vprintk_deferred(const char *fmt, va_list args)
2986{
2987 int r;
2988
2989 r = vprintk_emit(0, LOGLEVEL_SCHED, NULL, 0, fmt, args);
Petr Mladeka338f842018-06-27 16:08:16 +02002990 defer_console_output();
Petr Mladek719f6a72017-04-20 10:52:31 +02002991
2992 return r;
2993}
2994
John Stultzaac74dc2014-06-04 16:11:40 -07002995int printk_deferred(const char *fmt, ...)
Peter Zijlstra600e1452012-03-15 12:35:37 +01002996{
Peter Zijlstra600e1452012-03-15 12:35:37 +01002997 va_list args;
Peter Zijlstra600e1452012-03-15 12:35:37 +01002998 int r;
2999
Peter Zijlstra600e1452012-03-15 12:35:37 +01003000 va_start(args, fmt);
Petr Mladek719f6a72017-04-20 10:52:31 +02003001 r = vprintk_deferred(fmt, args);
Peter Zijlstra600e1452012-03-15 12:35:37 +01003002 va_end(args);
3003
Peter Zijlstra600e1452012-03-15 12:35:37 +01003004 return r;
3005}
3006
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007/*
3008 * printk rate limiting, lifted from the networking subsystem.
3009 *
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07003010 * This enforces a rate limit: not more than 10 kernel messages
3011 * every 5s to make a denial-of-service attack impossible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 */
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07003013DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
3014
Christian Borntraeger5c828712009-10-23 14:58:11 +02003015int __printk_ratelimit(const char *func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016{
Christian Borntraeger5c828712009-10-23 14:58:11 +02003017 return ___ratelimit(&printk_ratelimit_state, func);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018}
Christian Borntraeger5c828712009-10-23 14:58:11 +02003019EXPORT_SYMBOL(__printk_ratelimit);
Andrew Mortonf46c4832006-11-02 22:07:16 -08003020
3021/**
3022 * printk_timed_ratelimit - caller-controlled printk ratelimiting
3023 * @caller_jiffies: pointer to caller's state
3024 * @interval_msecs: minimum interval between prints
3025 *
3026 * printk_timed_ratelimit() returns true if more than @interval_msecs
3027 * milliseconds have elapsed since the last time printk_timed_ratelimit()
3028 * returned true.
3029 */
3030bool printk_timed_ratelimit(unsigned long *caller_jiffies,
3031 unsigned int interval_msecs)
3032{
Alex Elder249771b2014-08-06 16:09:08 -07003033 unsigned long elapsed = jiffies - *caller_jiffies;
3034
3035 if (*caller_jiffies && elapsed <= msecs_to_jiffies(interval_msecs))
3036 return false;
3037
3038 *caller_jiffies = jiffies;
3039 return true;
Andrew Mortonf46c4832006-11-02 22:07:16 -08003040}
3041EXPORT_SYMBOL(printk_timed_ratelimit);
Simon Kagstrom456b5652009-10-16 14:09:18 +02003042
3043static DEFINE_SPINLOCK(dump_list_lock);
3044static LIST_HEAD(dump_list);
3045
3046/**
3047 * kmsg_dump_register - register a kernel log dumper.
Randy Dunlap64855362009-12-17 15:27:27 -08003048 * @dumper: pointer to the kmsg_dumper structure
Simon Kagstrom456b5652009-10-16 14:09:18 +02003049 *
3050 * Adds a kernel log dumper to the system. The dump callback in the
3051 * structure will be called when the kernel oopses or panics and must be
3052 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
3053 */
3054int kmsg_dump_register(struct kmsg_dumper *dumper)
3055{
3056 unsigned long flags;
3057 int err = -EBUSY;
3058
3059 /* The dump callback needs to be set */
3060 if (!dumper->dump)
3061 return -EINVAL;
3062
3063 spin_lock_irqsave(&dump_list_lock, flags);
3064 /* Don't allow registering multiple times */
3065 if (!dumper->registered) {
3066 dumper->registered = 1;
Huang Yingfb842b002011-01-12 16:59:43 -08003067 list_add_tail_rcu(&dumper->list, &dump_list);
Simon Kagstrom456b5652009-10-16 14:09:18 +02003068 err = 0;
3069 }
3070 spin_unlock_irqrestore(&dump_list_lock, flags);
3071
3072 return err;
3073}
3074EXPORT_SYMBOL_GPL(kmsg_dump_register);
3075
3076/**
3077 * kmsg_dump_unregister - unregister a kmsg dumper.
Randy Dunlap64855362009-12-17 15:27:27 -08003078 * @dumper: pointer to the kmsg_dumper structure
Simon Kagstrom456b5652009-10-16 14:09:18 +02003079 *
3080 * Removes a dump device from the system. Returns zero on success and
3081 * %-EINVAL otherwise.
3082 */
3083int kmsg_dump_unregister(struct kmsg_dumper *dumper)
3084{
3085 unsigned long flags;
3086 int err = -EINVAL;
3087
3088 spin_lock_irqsave(&dump_list_lock, flags);
3089 if (dumper->registered) {
3090 dumper->registered = 0;
Huang Yingfb842b002011-01-12 16:59:43 -08003091 list_del_rcu(&dumper->list);
Simon Kagstrom456b5652009-10-16 14:09:18 +02003092 err = 0;
3093 }
3094 spin_unlock_irqrestore(&dump_list_lock, flags);
Huang Yingfb842b002011-01-12 16:59:43 -08003095 synchronize_rcu();
Simon Kagstrom456b5652009-10-16 14:09:18 +02003096
3097 return err;
3098}
3099EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
3100
Kay Sievers7ff95542012-05-03 02:29:13 +02003101static bool always_kmsg_dump;
3102module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR);
3103
Simon Kagstrom456b5652009-10-16 14:09:18 +02003104/**
3105 * kmsg_dump - dump kernel log to kernel message dumpers.
3106 * @reason: the reason (oops, panic etc) for dumping
3107 *
Kay Sieverse2ae7152012-06-15 14:07:51 +02003108 * Call each of the registered dumper's dump() callback, which can
3109 * retrieve the kmsg records with kmsg_dump_get_line() or
3110 * kmsg_dump_get_buffer().
Simon Kagstrom456b5652009-10-16 14:09:18 +02003111 */
3112void kmsg_dump(enum kmsg_dump_reason reason)
3113{
Simon Kagstrom456b5652009-10-16 14:09:18 +02003114 struct kmsg_dumper *dumper;
Simon Kagstrom456b5652009-10-16 14:09:18 +02003115 unsigned long flags;
3116
Matthew Garrettc22ab3322012-03-05 14:59:10 -08003117 if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
3118 return;
3119
Huang Yingfb842b002011-01-12 16:59:43 -08003120 rcu_read_lock();
Kay Sieverse2ae7152012-06-15 14:07:51 +02003121 list_for_each_entry_rcu(dumper, &dump_list, list) {
3122 if (dumper->max_reason && reason > dumper->max_reason)
3123 continue;
3124
3125 /* initialize iterator with data about the stored records */
3126 dumper->active = true;
3127
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09003128 logbuf_lock_irqsave(flags);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003129 dumper->cur_seq = clear_seq;
3130 dumper->cur_idx = clear_idx;
3131 dumper->next_seq = log_next_seq;
3132 dumper->next_idx = log_next_idx;
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09003133 logbuf_unlock_irqrestore(flags);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003134
3135 /* invoke dumper which will iterate over records */
3136 dumper->dump(dumper, reason);
3137
3138 /* reset iterator */
3139 dumper->active = false;
3140 }
Huang Yingfb842b002011-01-12 16:59:43 -08003141 rcu_read_unlock();
Simon Kagstrom456b5652009-10-16 14:09:18 +02003142}
Kay Sieverse2ae7152012-06-15 14:07:51 +02003143
3144/**
Anton Vorontsov533827c2012-07-20 17:28:07 -07003145 * kmsg_dump_get_line_nolock - retrieve one kmsg log line (unlocked version)
3146 * @dumper: registered kmsg dumper
3147 * @syslog: include the "<4>" prefixes
3148 * @line: buffer to copy the line to
3149 * @size: maximum size of the buffer
3150 * @len: length of line placed into buffer
3151 *
3152 * Start at the beginning of the kmsg buffer, with the oldest kmsg
3153 * record, and copy one record into the provided buffer.
3154 *
3155 * Consecutive calls will return the next available record moving
3156 * towards the end of the buffer with the youngest messages.
3157 *
3158 * A return value of FALSE indicates that there are no more records to
3159 * read.
3160 *
3161 * The function is similar to kmsg_dump_get_line(), but grabs no locks.
3162 */
3163bool kmsg_dump_get_line_nolock(struct kmsg_dumper *dumper, bool syslog,
3164 char *line, size_t size, size_t *len)
3165{
Joe Perches62e32ac2013-07-31 13:53:47 -07003166 struct printk_log *msg;
Anton Vorontsov533827c2012-07-20 17:28:07 -07003167 size_t l = 0;
3168 bool ret = false;
3169
3170 if (!dumper->active)
3171 goto out;
3172
3173 if (dumper->cur_seq < log_first_seq) {
3174 /* messages are gone, move to first available one */
3175 dumper->cur_seq = log_first_seq;
3176 dumper->cur_idx = log_first_idx;
3177 }
3178
3179 /* last entry */
3180 if (dumper->cur_seq >= log_next_seq)
3181 goto out;
3182
3183 msg = log_from_idx(dumper->cur_idx);
Tetsuo Handae80c1a92018-12-04 19:00:01 +09003184 l = msg_print_text(msg, syslog, printk_time, line, size);
Anton Vorontsov533827c2012-07-20 17:28:07 -07003185
3186 dumper->cur_idx = log_next(dumper->cur_idx);
3187 dumper->cur_seq++;
3188 ret = true;
3189out:
3190 if (len)
3191 *len = l;
3192 return ret;
3193}
3194
3195/**
Kay Sieverse2ae7152012-06-15 14:07:51 +02003196 * kmsg_dump_get_line - retrieve one kmsg log line
3197 * @dumper: registered kmsg dumper
3198 * @syslog: include the "<4>" prefixes
3199 * @line: buffer to copy the line to
3200 * @size: maximum size of the buffer
3201 * @len: length of line placed into buffer
3202 *
3203 * Start at the beginning of the kmsg buffer, with the oldest kmsg
3204 * record, and copy one record into the provided buffer.
3205 *
3206 * Consecutive calls will return the next available record moving
3207 * towards the end of the buffer with the youngest messages.
3208 *
3209 * A return value of FALSE indicates that there are no more records to
3210 * read.
3211 */
3212bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog,
3213 char *line, size_t size, size_t *len)
3214{
3215 unsigned long flags;
Anton Vorontsov533827c2012-07-20 17:28:07 -07003216 bool ret;
Kay Sieverse2ae7152012-06-15 14:07:51 +02003217
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09003218 logbuf_lock_irqsave(flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07003219 ret = kmsg_dump_get_line_nolock(dumper, syslog, line, size, len);
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09003220 logbuf_unlock_irqrestore(flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07003221
Kay Sieverse2ae7152012-06-15 14:07:51 +02003222 return ret;
3223}
3224EXPORT_SYMBOL_GPL(kmsg_dump_get_line);
3225
3226/**
3227 * kmsg_dump_get_buffer - copy kmsg log lines
3228 * @dumper: registered kmsg dumper
3229 * @syslog: include the "<4>" prefixes
Randy Dunlap4f0f4af2012-06-30 15:37:24 -07003230 * @buf: buffer to copy the line to
Kay Sieverse2ae7152012-06-15 14:07:51 +02003231 * @size: maximum size of the buffer
3232 * @len: length of line placed into buffer
3233 *
3234 * Start at the end of the kmsg buffer and fill the provided buffer
3235 * with as many of the the *youngest* kmsg records that fit into it.
3236 * If the buffer is large enough, all available kmsg records will be
3237 * copied with a single call.
3238 *
3239 * Consecutive calls will fill the buffer with the next block of
3240 * available older records, not including the earlier retrieved ones.
3241 *
3242 * A return value of FALSE indicates that there are no more records to
3243 * read.
3244 */
3245bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
3246 char *buf, size_t size, size_t *len)
3247{
3248 unsigned long flags;
3249 u64 seq;
3250 u32 idx;
3251 u64 next_seq;
3252 u32 next_idx;
3253 size_t l = 0;
3254 bool ret = false;
Tetsuo Handae80c1a92018-12-04 19:00:01 +09003255 bool time = printk_time;
Kay Sieverse2ae7152012-06-15 14:07:51 +02003256
3257 if (!dumper->active)
3258 goto out;
3259
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09003260 logbuf_lock_irqsave(flags);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003261 if (dumper->cur_seq < log_first_seq) {
3262 /* messages are gone, move to first available one */
3263 dumper->cur_seq = log_first_seq;
3264 dumper->cur_idx = log_first_idx;
3265 }
3266
3267 /* last entry */
3268 if (dumper->cur_seq >= dumper->next_seq) {
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09003269 logbuf_unlock_irqrestore(flags);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003270 goto out;
3271 }
3272
3273 /* calculate length of entire buffer */
3274 seq = dumper->cur_seq;
3275 idx = dumper->cur_idx;
3276 while (seq < dumper->next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07003277 struct printk_log *msg = log_from_idx(idx);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003278
Tetsuo Handae80c1a92018-12-04 19:00:01 +09003279 l += msg_print_text(msg, true, time, NULL, 0);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003280 idx = log_next(idx);
3281 seq++;
3282 }
3283
3284 /* move first record forward until length fits into the buffer */
3285 seq = dumper->cur_seq;
3286 idx = dumper->cur_idx;
Vincent Whitchurchc9dccac2019-07-11 16:29:37 +02003287 while (l >= size && seq < dumper->next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07003288 struct printk_log *msg = log_from_idx(idx);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003289
Tetsuo Handae80c1a92018-12-04 19:00:01 +09003290 l -= msg_print_text(msg, true, time, NULL, 0);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003291 idx = log_next(idx);
3292 seq++;
3293 }
3294
3295 /* last message in next interation */
3296 next_seq = seq;
3297 next_idx = idx;
3298
3299 l = 0;
3300 while (seq < dumper->next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07003301 struct printk_log *msg = log_from_idx(idx);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003302
Tetsuo Handae80c1a92018-12-04 19:00:01 +09003303 l += msg_print_text(msg, syslog, time, buf + l, size - l);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003304 idx = log_next(idx);
3305 seq++;
3306 }
3307
3308 dumper->next_seq = next_seq;
3309 dumper->next_idx = next_idx;
3310 ret = true;
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09003311 logbuf_unlock_irqrestore(flags);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003312out:
3313 if (len)
3314 *len = l;
3315 return ret;
3316}
3317EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer);
3318
3319/**
Anton Vorontsov533827c2012-07-20 17:28:07 -07003320 * kmsg_dump_rewind_nolock - reset the interator (unlocked version)
3321 * @dumper: registered kmsg dumper
3322 *
3323 * Reset the dumper's iterator so that kmsg_dump_get_line() and
3324 * kmsg_dump_get_buffer() can be called again and used multiple
3325 * times within the same dumper.dump() callback.
3326 *
3327 * The function is similar to kmsg_dump_rewind(), but grabs no locks.
3328 */
3329void kmsg_dump_rewind_nolock(struct kmsg_dumper *dumper)
3330{
3331 dumper->cur_seq = clear_seq;
3332 dumper->cur_idx = clear_idx;
3333 dumper->next_seq = log_next_seq;
3334 dumper->next_idx = log_next_idx;
3335}
3336
3337/**
Kay Sieverse2ae7152012-06-15 14:07:51 +02003338 * kmsg_dump_rewind - reset the interator
3339 * @dumper: registered kmsg dumper
3340 *
3341 * Reset the dumper's iterator so that kmsg_dump_get_line() and
3342 * kmsg_dump_get_buffer() can be called again and used multiple
3343 * times within the same dumper.dump() callback.
3344 */
3345void kmsg_dump_rewind(struct kmsg_dumper *dumper)
3346{
3347 unsigned long flags;
3348
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09003349 logbuf_lock_irqsave(flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07003350 kmsg_dump_rewind_nolock(dumper);
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09003351 logbuf_unlock_irqrestore(flags);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003352}
3353EXPORT_SYMBOL_GPL(kmsg_dump_rewind);
Tejun Heo196779b2013-04-30 15:27:12 -07003354
Joe Perches7ef3d2f2008-02-08 04:21:25 -08003355#endif