blob: 61d188f4c672d52bbbf3d7a964dc1aa6c5dac79c [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 for_each_console(con) {
1776 if (exclusive_console && con != exclusive_console)
1777 continue;
1778 if (!(con->flags & CON_ENABLED))
1779 continue;
1780 if (!con->write)
1781 continue;
1782 if (!cpu_online(smp_processor_id()) &&
1783 !(con->flags & CON_ANYTIME))
1784 continue;
Tejun Heo6fe29352015-06-25 15:01:30 -07001785 if (con->flags & CON_EXTENDED)
1786 con->write(con, ext_text, ext_len);
1787 else
1788 con->write(con, text, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
1791
Dave Youngaf913222009-09-22 16:43:33 -07001792int printk_delay_msec __read_mostly;
1793
1794static inline void printk_delay(void)
1795{
1796 if (unlikely(printk_delay_msec)) {
1797 int m = printk_delay_msec;
1798
1799 while (m--) {
1800 mdelay(1);
1801 touch_nmi_watchdog();
1802 }
1803 }
1804}
1805
Tetsuo Handacbae05d2019-02-16 19:59:33 +09001806static inline u32 printk_caller_id(void)
1807{
1808 return in_task() ? task_pid_nr(current) :
1809 0x80000000 + raw_smp_processor_id();
1810}
1811
Kay Sievers084681d2012-06-28 09:38:53 +02001812/*
1813 * Continuation lines are buffered, and not committed to the record buffer
1814 * until the line is complete, or a race forces it. The line fragments
1815 * though, are printed immediately to the consoles to ensure everything has
1816 * reached the console in case of a kernel crash.
1817 */
1818static struct cont {
1819 char buf[LOG_LINE_MAX];
1820 size_t len; /* length == 0 means unused buffer */
Tetsuo Handacbae05d2019-02-16 19:59:33 +09001821 u32 caller_id; /* printk_caller_id() of first print */
Kay Sievers084681d2012-06-28 09:38:53 +02001822 u64 ts_nsec; /* time of first print */
1823 u8 level; /* log level of first message */
Alex Elder0b90fec2014-08-06 16:09:03 -07001824 u8 facility; /* log facility of first message */
Kay Sieverseab07262012-07-16 18:35:30 -07001825 enum log_flags flags; /* prefix, newline flags */
Kay Sievers084681d2012-06-28 09:38:53 +02001826} cont;
1827
Linus Torvalds5e467652016-10-09 11:53:00 -07001828static void cont_flush(void)
Kay Sievers084681d2012-06-28 09:38:53 +02001829{
Kay Sievers084681d2012-06-28 09:38:53 +02001830 if (cont.len == 0)
1831 return;
Linus Torvalds5c2992e2016-10-25 11:27:31 -07001832
Tetsuo Handacbae05d2019-02-16 19:59:33 +09001833 log_store(cont.caller_id, cont.facility, cont.level, cont.flags,
1834 cont.ts_nsec, NULL, 0, cont.buf, cont.len);
Linus Torvalds5c2992e2016-10-25 11:27:31 -07001835 cont.len = 0;
Kay Sievers084681d2012-06-28 09:38:53 +02001836}
1837
Tetsuo Handacbae05d2019-02-16 19:59:33 +09001838static bool cont_add(u32 caller_id, int facility, int level,
1839 enum log_flags flags, const char *text, size_t len)
Kay Sievers084681d2012-06-28 09:38:53 +02001840{
Sergey Senozhatsky96278082018-10-02 11:38:34 +09001841 /* If the line gets too long, split it up in separate records. */
1842 if (cont.len + len > sizeof(cont.buf)) {
Linus Torvalds5e467652016-10-09 11:53:00 -07001843 cont_flush();
Kay Sievers084681d2012-06-28 09:38:53 +02001844 return false;
1845 }
1846
1847 if (!cont.len) {
1848 cont.facility = facility;
1849 cont.level = level;
Tetsuo Handacbae05d2019-02-16 19:59:33 +09001850 cont.caller_id = caller_id;
Kay Sievers084681d2012-06-28 09:38:53 +02001851 cont.ts_nsec = local_clock();
Linus Torvalds5e467652016-10-09 11:53:00 -07001852 cont.flags = flags;
Kay Sievers084681d2012-06-28 09:38:53 +02001853 }
1854
1855 memcpy(cont.buf + cont.len, text, len);
1856 cont.len += len;
Kay Sieverseab07262012-07-16 18:35:30 -07001857
Linus Torvalds5e467652016-10-09 11:53:00 -07001858 // The original flags come from the first line,
1859 // but later continuations can add a newline.
1860 if (flags & LOG_NEWLINE) {
1861 cont.flags |= LOG_NEWLINE;
1862 cont_flush();
1863 }
1864
Kay Sievers084681d2012-06-28 09:38:53 +02001865 return true;
1866}
1867
Linus Torvaldsc362c7f2016-10-08 22:02:09 -07001868static size_t log_output(int facility, int level, enum log_flags lflags, const char *dict, size_t dictlen, char *text, size_t text_len)
1869{
Tetsuo Handacbae05d2019-02-16 19:59:33 +09001870 const u32 caller_id = printk_caller_id();
1871
Linus Torvaldsc362c7f2016-10-08 22:02:09 -07001872 /*
Linus Torvalds5e467652016-10-09 11:53:00 -07001873 * If an earlier line was buffered, and we're a continuation
Tetsuo Handacbae05d2019-02-16 19:59:33 +09001874 * write from the same context, try to add it to the buffer.
Linus Torvaldsc362c7f2016-10-08 22:02:09 -07001875 */
1876 if (cont.len) {
Tetsuo Handacbae05d2019-02-16 19:59:33 +09001877 if (cont.caller_id == caller_id && (lflags & LOG_CONT)) {
1878 if (cont_add(caller_id, facility, level, lflags, text, text_len))
Linus Torvalds5e467652016-10-09 11:53:00 -07001879 return text_len;
1880 }
1881 /* Otherwise, make sure it's flushed */
1882 cont_flush();
1883 }
Linus Torvaldsc362c7f2016-10-08 22:02:09 -07001884
Linus Torvalds8835ca52016-10-19 09:11:24 -07001885 /* Skip empty continuation lines that couldn't be added - they just flush */
1886 if (!text_len && (lflags & LOG_CONT))
1887 return 0;
1888
Linus Torvalds5e467652016-10-09 11:53:00 -07001889 /* If it doesn't end in a newline, try to buffer the current line */
1890 if (!(lflags & LOG_NEWLINE)) {
Tetsuo Handacbae05d2019-02-16 19:59:33 +09001891 if (cont_add(caller_id, facility, level, lflags, text, text_len))
Linus Torvaldsc362c7f2016-10-08 22:02:09 -07001892 return text_len;
1893 }
1894
Linus Torvalds5e467652016-10-09 11:53:00 -07001895 /* Store it in the record log */
Tetsuo Handacbae05d2019-02-16 19:59:33 +09001896 return log_store(caller_id, facility, level, lflags, 0,
1897 dict, dictlen, text, text_len);
Linus Torvaldsc362c7f2016-10-08 22:02:09 -07001898}
1899
Petr Mladekba552392018-06-27 16:08:15 +02001900/* Must be called under logbuf_lock. */
1901int vprintk_store(int facility, int level,
1902 const char *dict, size_t dictlen,
1903 const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
Kay Sievers7ff95542012-05-03 02:29:13 +02001905 static char textbuf[LOG_LINE_MAX];
1906 char *text = textbuf;
Pierre Kuoaec47ca2017-07-11 14:40:55 +08001907 size_t text_len;
Kay Sievers5becfb12012-07-09 12:15:42 -07001908 enum log_flags lflags = 0;
Jan Kara608873c2014-06-04 16:11:35 -07001909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 /*
Kay Sievers7ff95542012-05-03 02:29:13 +02001911 * The printf needs to come first; we need the syslog
1912 * prefix which might be passed-in as a parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 */
Markus Trippelsdorf98e35f52014-10-13 15:51:13 -07001914 text_len = vscnprintf(text, sizeof(textbuf), fmt, args);
Nick Andrewac60ad72008-05-12 21:21:04 +02001915
Kay Sievers7ff95542012-05-03 02:29:13 +02001916 /* mark and strip a trailing newline */
Kay Sieversc313af12012-05-14 20:46:27 +02001917 if (text_len && text[text_len-1] == '\n') {
1918 text_len--;
Kay Sievers5becfb12012-07-09 12:15:42 -07001919 lflags |= LOG_NEWLINE;
Kay Sievers7ff95542012-05-03 02:29:13 +02001920 }
Kay Sievers9d90c8d2011-03-13 03:19:51 +01001921
Joe Perches088a52a2012-07-30 14:40:19 -07001922 /* strip kernel syslog prefix and extract log level or control flags */
1923 if (facility == 0) {
Linus Torvalds4bcc5952016-10-08 20:32:40 -07001924 int kern_level;
Joe Perches088a52a2012-07-30 14:40:19 -07001925
Linus Torvalds4bcc5952016-10-08 20:32:40 -07001926 while ((kern_level = printk_get_level(text)) != 0) {
Joe Perches088a52a2012-07-30 14:40:19 -07001927 switch (kern_level) {
1928 case '0' ... '7':
Joe Perchesa39d4a82014-12-10 15:50:15 -08001929 if (level == LOGLEVEL_DEFAULT)
Joe Perches088a52a2012-07-30 14:40:19 -07001930 level = kern_level - '0';
Linus Torvalds4bcc5952016-10-08 20:32:40 -07001931 break;
1932 case 'c': /* KERN_CONT */
1933 lflags |= LOG_CONT;
Joe Perches088a52a2012-07-30 14:40:19 -07001934 }
Linus Torvalds4bcc5952016-10-08 20:32:40 -07001935
1936 text_len -= 2;
1937 text += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
Kay Sievers7ff95542012-05-03 02:29:13 +02001939 }
Nick Andrewac60ad72008-05-12 21:21:04 +02001940
Joe Perchesa39d4a82014-12-10 15:50:15 -08001941 if (level == LOGLEVEL_DEFAULT)
Kay Sieversc313af12012-05-14 20:46:27 +02001942 level = default_message_loglevel;
1943
Kay Sievers5becfb12012-07-09 12:15:42 -07001944 if (dict)
Tetsuo Handae36202a2019-02-22 18:59:40 +09001945 lflags |= LOG_NEWLINE;
Kay Sievers7ff95542012-05-03 02:29:13 +02001946
Petr Mladekba552392018-06-27 16:08:15 +02001947 return log_output(facility, level, lflags,
1948 dict, dictlen, text, text_len);
1949}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Petr Mladekba552392018-06-27 16:08:15 +02001951asmlinkage int vprintk_emit(int facility, int level,
1952 const char *dict, size_t dictlen,
1953 const char *fmt, va_list args)
1954{
1955 int printed_len;
Sergey Senozhatsky3ac37a92018-10-02 11:38:35 +09001956 bool in_sched = false, pending_output;
Petr Mladekba552392018-06-27 16:08:15 +02001957 unsigned long flags;
Sergey Senozhatsky3ac37a92018-10-02 11:38:35 +09001958 u64 curr_log_seq;
Petr Mladekba552392018-06-27 16:08:15 +02001959
Feng Tangc39ea0b2019-05-14 15:45:34 -07001960 /* Suppress unimportant messages after panic happens */
1961 if (unlikely(suppress_printk))
1962 return 0;
1963
Petr Mladekba552392018-06-27 16:08:15 +02001964 if (level == LOGLEVEL_SCHED) {
1965 level = LOGLEVEL_DEFAULT;
1966 in_sched = true;
1967 }
1968
1969 boot_delay_msec(level);
1970 printk_delay();
1971
1972 /* This stops the holder of console_sem just where we want him */
1973 logbuf_lock_irqsave(flags);
Sergey Senozhatsky3ac37a92018-10-02 11:38:35 +09001974 curr_log_seq = log_next_seq;
Petr Mladekba552392018-06-27 16:08:15 +02001975 printed_len = vprintk_store(facility, level, dict, dictlen, fmt, args);
Sergey Senozhatsky3ac37a92018-10-02 11:38:35 +09001976 pending_output = (curr_log_seq != log_next_seq);
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09001977 logbuf_unlock_irqrestore(flags);
Jan Kara939f04b2014-06-04 16:11:37 -07001978
Steven Rostedt458df9f2014-06-04 16:11:38 -07001979 /* If called from the scheduler, we can not call up(). */
Sergey Senozhatsky3ac37a92018-10-02 11:38:35 +09001980 if (!in_sched && pending_output) {
Jan Kara5874af22014-08-06 16:09:10 -07001981 /*
Sergey Senozhatskyfd5f7cd2018-01-16 13:47:16 +09001982 * Disable preemption to avoid being preempted while holding
1983 * console_sem which would prevent anyone from printing to
1984 * console
1985 */
1986 preempt_disable();
1987 /*
Andrew Mortond18bbc22014-07-02 15:22:38 -07001988 * Try to acquire and then immediately release the console
1989 * semaphore. The release will print out buffers and wake up
1990 * /dev/kmsg and syslog() users.
1991 */
Petr Mladekc162d5b2018-01-12 17:08:37 +01001992 if (console_trylock_spinning())
Andrew Mortond18bbc22014-07-02 15:22:38 -07001993 console_unlock();
Sergey Senozhatskyfd5f7cd2018-01-16 13:47:16 +09001994 preempt_enable();
Andrew Mortond18bbc22014-07-02 15:22:38 -07001995 }
Steven Rostedt458df9f2014-06-04 16:11:38 -07001996
Sergey Senozhatsky3ac37a92018-10-02 11:38:35 +09001997 if (pending_output)
1998 wake_up_klogd();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 return printed_len;
2000}
Kay Sievers7ff95542012-05-03 02:29:13 +02002001EXPORT_SYMBOL(vprintk_emit);
2002
2003asmlinkage int vprintk(const char *fmt, va_list args)
2004{
Sergey Senozhatskybd66a892016-12-27 23:16:04 +09002005 return vprintk_func(fmt, args);
Kay Sievers7ff95542012-05-03 02:29:13 +02002006}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007EXPORT_SYMBOL(vprintk);
2008
Linus Torvaldsa0cba212016-08-09 10:48:18 -07002009int vprintk_default(const char *fmt, va_list args)
Steven Rostedt (Red Hat)afdc34a2014-06-19 17:33:31 -04002010{
2011 int r;
2012
2013#ifdef CONFIG_KGDB_KDB
Petr Mladek34aaff42016-12-14 15:05:58 -08002014 /* Allow to pass printk() to kdb but avoid a recursion. */
2015 if (unlikely(kdb_trap_printk && kdb_printf_cpu < 0)) {
Daniel Thompsonf7d4ca82014-11-07 18:37:57 +00002016 r = vkdb_printf(KDB_MSGSRC_PRINTK, fmt, args);
Steven Rostedt (Red Hat)afdc34a2014-06-19 17:33:31 -04002017 return r;
2018 }
2019#endif
Linus Torvaldsa0cba212016-08-09 10:48:18 -07002020 r = vprintk_emit(0, LOGLEVEL_DEFAULT, NULL, 0, fmt, args);
Steven Rostedt (Red Hat)afdc34a2014-06-19 17:33:31 -04002021
2022 return r;
2023}
2024EXPORT_SYMBOL_GPL(vprintk_default);
2025
Kay Sievers7ff95542012-05-03 02:29:13 +02002026/**
2027 * printk - print a kernel message
2028 * @fmt: format string
2029 *
2030 * This is printk(). It can be called from any context. We want it to work.
2031 *
2032 * We try to grab the console_lock. If we succeed, it's easy - we log the
2033 * output and call the console drivers. If we fail to get the semaphore, we
2034 * place the output into the log buffer and return. The current holder of
2035 * the console_sem will notice the new output in console_unlock(); and will
2036 * send it to the consoles before releasing the lock.
2037 *
2038 * One effect of this deferred printing is that code which calls printk() and
2039 * then changes console_loglevel may break. This is because console_loglevel
2040 * is inspected when the actual printing occurs.
2041 *
2042 * See also:
2043 * printf(3)
2044 *
2045 * See the vsnprintf() documentation for format string extensions over C99.
2046 */
Andi Kleen722a9f92014-05-02 00:44:38 +02002047asmlinkage __visible int printk(const char *fmt, ...)
Kay Sievers7ff95542012-05-03 02:29:13 +02002048{
2049 va_list args;
2050 int r;
2051
Kay Sievers7ff95542012-05-03 02:29:13 +02002052 va_start(args, fmt);
Linus Torvaldsa0cba212016-08-09 10:48:18 -07002053 r = vprintk_func(fmt, args);
Kay Sievers7ff95542012-05-03 02:29:13 +02002054 va_end(args);
2055
2056 return r;
2057}
2058EXPORT_SYMBOL(printk);
Kay Sievers7f3a7812012-05-09 01:37:51 +02002059
Kay Sievers96efedf2012-07-16 18:35:29 -07002060#else /* CONFIG_PRINTK */
Matt Mackalld59745c2005-05-01 08:59:02 -07002061
Kay Sievers70498252012-07-16 18:35:29 -07002062#define LOG_LINE_MAX 0
2063#define PREFIX_MAX 0
Tetsuo Handae80c1a92018-12-04 19:00:01 +09002064#define printk_time false
Alex Elder249771b2014-08-06 16:09:08 -07002065
Kay Sievers96efedf2012-07-16 18:35:29 -07002066static u64 syslog_seq;
2067static u32 syslog_idx;
Kay Sieverseab07262012-07-16 18:35:30 -07002068static u64 console_seq;
2069static u32 console_idx;
Petr Mladekf92b0702018-09-13 14:34:06 +02002070static u64 exclusive_console_stop_seq;
Kay Sievers96efedf2012-07-16 18:35:29 -07002071static u64 log_first_seq;
2072static u32 log_first_idx;
2073static u64 log_next_seq;
Tejun Heo6fe29352015-06-25 15:01:30 -07002074static char *log_text(const struct printk_log *msg) { return NULL; }
2075static char *log_dict(const struct printk_log *msg) { return NULL; }
Joe Perches62e32ac2013-07-31 13:53:47 -07002076static struct printk_log *log_from_idx(u32 idx) { return NULL; }
Kay Sievers7f3a7812012-05-09 01:37:51 +02002077static u32 log_next(u32 idx) { return 0; }
Tejun Heo6fe29352015-06-25 15:01:30 -07002078static ssize_t msg_print_ext_header(char *buf, size_t size,
Linus Torvalds5aa068e2016-10-25 11:27:31 -07002079 struct printk_log *msg,
2080 u64 seq) { return 0; }
Tejun Heo6fe29352015-06-25 15:01:30 -07002081static ssize_t msg_print_ext_body(char *buf, size_t size,
2082 char *dict, size_t dict_len,
2083 char *text, size_t text_len) { return 0; }
Petr Mladekc162d5b2018-01-12 17:08:37 +01002084static void console_lock_spinning_enable(void) { }
2085static int console_lock_spinning_disable_and_check(void) { return 0; }
Sergey Senozhatskyd9c23522016-12-24 23:09:01 +09002086static void call_console_drivers(const char *ext_text, size_t ext_len,
Tejun Heo6fe29352015-06-25 15:01:30 -07002087 const char *text, size_t len) {}
Tetsuo Handae80c1a92018-12-04 19:00:01 +09002088static size_t msg_print_text(const struct printk_log *msg, bool syslog,
2089 bool time, char *buf, size_t size) { return 0; }
Petr Mladeka6ae9282018-09-10 15:52:06 +02002090static bool suppress_message_printing(int level) { return false; }
Matt Mackalld59745c2005-05-01 08:59:02 -07002091
Kay Sievers7f3a7812012-05-09 01:37:51 +02002092#endif /* CONFIG_PRINTK */
Matt Mackalld59745c2005-05-01 08:59:02 -07002093
Thomas Gleixnerd0380e62013-04-29 16:17:18 -07002094#ifdef CONFIG_EARLY_PRINTK
2095struct console *early_console;
2096
Andi Kleen722a9f92014-05-02 00:44:38 +02002097asmlinkage __visible void early_printk(const char *fmt, ...)
Thomas Gleixnerd0380e62013-04-29 16:17:18 -07002098{
2099 va_list ap;
Joe Perches1dc62442014-12-10 15:45:53 -08002100 char buf[512];
2101 int n;
2102
2103 if (!early_console)
2104 return;
Thomas Gleixnerd0380e62013-04-29 16:17:18 -07002105
2106 va_start(ap, fmt);
Joe Perches1dc62442014-12-10 15:45:53 -08002107 n = vscnprintf(buf, sizeof(buf), fmt, ap);
Thomas Gleixnerd0380e62013-04-29 16:17:18 -07002108 va_end(ap);
Joe Perches1dc62442014-12-10 15:45:53 -08002109
2110 early_console->write(early_console, buf, n);
Thomas Gleixnerd0380e62013-04-29 16:17:18 -07002111}
2112#endif
2113
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002114static int __add_preferred_console(char *name, int idx, char *options,
2115 char *brl_options)
2116{
2117 struct console_cmdline *c;
2118 int i;
2119
2120 /*
2121 * See if this tty is not yet registered, and
2122 * if we have a slot free.
2123 */
Petr Mladekdac8bbb2017-06-08 12:01:30 +02002124 for (i = 0, c = console_cmdline;
2125 i < MAX_CMDLINECONSOLES && c->name[0];
2126 i++, c++) {
Joe Perches23475402013-07-31 13:53:46 -07002127 if (strcmp(c->name, name) == 0 && c->index == idx) {
Petr Mladekdac8bbb2017-06-08 12:01:30 +02002128 if (!brl_options)
2129 preferred_console = i;
Joe Perches23475402013-07-31 13:53:46 -07002130 return 0;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002131 }
Joe Perches23475402013-07-31 13:53:46 -07002132 }
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002133 if (i == MAX_CMDLINECONSOLES)
2134 return -E2BIG;
2135 if (!brl_options)
Aleksey Makarovad86ee22017-03-15 13:28:51 +03002136 preferred_console = i;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002137 strlcpy(c->name, name, sizeof(c->name));
2138 c->options = options;
Joe Perchesbbeddf52013-07-31 13:53:45 -07002139 braille_set_options(c, brl_options);
2140
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002141 c->index = idx;
2142 return 0;
2143}
Sergey Senozhatskycca10d52017-12-21 14:41:49 +09002144
2145static int __init console_msg_format_setup(char *str)
2146{
2147 if (!strcmp(str, "syslog"))
2148 console_msg_format = MSG_FORMAT_SYSLOG;
2149 if (!strcmp(str, "default"))
2150 console_msg_format = MSG_FORMAT_DEFAULT;
2151 return 1;
2152}
2153__setup("console_msg_format=", console_msg_format_setup);
2154
John Z. Bohach2ea1c532006-03-24 03:18:19 -08002155/*
Alex Elder0b90fec2014-08-06 16:09:03 -07002156 * Set up a console. Called via do_early_param() in init/main.c
2157 * for each "console=" parameter in the boot command line.
John Z. Bohach2ea1c532006-03-24 03:18:19 -08002158 */
2159static int __init console_setup(char *str)
2160{
Alex Elder0b90fec2014-08-06 16:09:03 -07002161 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for "ttyS" */
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002162 char *s, *options, *brl_options = NULL;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08002163 int idx;
2164
Joe Perchesbbeddf52013-07-31 13:53:45 -07002165 if (_braille_console_setup(&str, &brl_options))
2166 return 1;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002167
John Z. Bohach2ea1c532006-03-24 03:18:19 -08002168 /*
2169 * Decode str into name, index, options.
2170 */
2171 if (str[0] >= '0' && str[0] <= '9') {
Yinghai Lueaa944a2007-07-15 23:37:27 -07002172 strcpy(buf, "ttyS");
2173 strncpy(buf + 4, str, sizeof(buf) - 5);
John Z. Bohach2ea1c532006-03-24 03:18:19 -08002174 } else {
Yinghai Lueaa944a2007-07-15 23:37:27 -07002175 strncpy(buf, str, sizeof(buf) - 1);
John Z. Bohach2ea1c532006-03-24 03:18:19 -08002176 }
Yinghai Lueaa944a2007-07-15 23:37:27 -07002177 buf[sizeof(buf) - 1] = 0;
Alex Elder249771b2014-08-06 16:09:08 -07002178 options = strchr(str, ',');
2179 if (options)
John Z. Bohach2ea1c532006-03-24 03:18:19 -08002180 *(options++) = 0;
2181#ifdef __sparc__
2182 if (!strcmp(str, "ttya"))
Yinghai Lueaa944a2007-07-15 23:37:27 -07002183 strcpy(buf, "ttyS0");
John Z. Bohach2ea1c532006-03-24 03:18:19 -08002184 if (!strcmp(str, "ttyb"))
Yinghai Lueaa944a2007-07-15 23:37:27 -07002185 strcpy(buf, "ttyS1");
John Z. Bohach2ea1c532006-03-24 03:18:19 -08002186#endif
Yinghai Lueaa944a2007-07-15 23:37:27 -07002187 for (s = buf; *s; s++)
Alex Elder249771b2014-08-06 16:09:08 -07002188 if (isdigit(*s) || *s == ',')
John Z. Bohach2ea1c532006-03-24 03:18:19 -08002189 break;
2190 idx = simple_strtoul(s, NULL, 10);
2191 *s = 0;
2192
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002193 __add_preferred_console(buf, idx, options, brl_options);
Markus Armbruster9e124fe2008-05-26 23:31:07 +01002194 console_set_on_cmdline = 1;
John Z. Bohach2ea1c532006-03-24 03:18:19 -08002195 return 1;
2196}
2197__setup("console=", console_setup);
2198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199/**
Matt Mackall3c0547b2005-05-16 21:53:47 -07002200 * add_preferred_console - add a device to the list of preferred consoles.
Martin Waitzddad86c2005-11-13 16:08:14 -08002201 * @name: device name
2202 * @idx: device index
2203 * @options: options for this console
Matt Mackall3c0547b2005-05-16 21:53:47 -07002204 *
2205 * The last preferred console added will be used for kernel messages
2206 * and stdin/out/err for init. Normally this is used by console_setup
2207 * above to handle user-supplied console arguments; however it can also
2208 * be used by arch-specific code either to override the user or more
2209 * commonly to provide a default console (ie from PROM variables) when
2210 * the user has not supplied one.
2211 */
David S. Millerfb445ee2007-12-29 01:19:49 -08002212int add_preferred_console(char *name, int idx, char *options)
Matt Mackall3c0547b2005-05-16 21:53:47 -07002213{
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002214 return __add_preferred_console(name, idx, options, NULL);
Matt Mackall3c0547b2005-05-16 21:53:47 -07002215}
2216
Neil Zhangd25d9fe2014-08-06 16:09:12 -07002217bool console_suspend_enabled = true;
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002218EXPORT_SYMBOL(console_suspend_enabled);
2219
2220static int __init console_suspend_disable(char *str)
2221{
Neil Zhangd25d9fe2014-08-06 16:09:12 -07002222 console_suspend_enabled = false;
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002223 return 1;
2224}
2225__setup("no_console_suspend", console_suspend_disable);
Yanmin Zhang134620f2011-10-31 17:11:27 -07002226module_param_named(console_suspend, console_suspend_enabled,
2227 bool, S_IRUGO | S_IWUSR);
2228MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
2229 " and hibernate operations");
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002230
Matt Mackall3c0547b2005-05-16 21:53:47 -07002231/**
Linus Torvalds557240b2006-06-19 18:16:01 -07002232 * suspend_console - suspend the console subsystem
2233 *
2234 * This disables printk() while we go into suspend states
2235 */
2236void suspend_console(void)
2237{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002238 if (!console_suspend_enabled)
2239 return;
Tomeu Vizoso47319f72018-03-22 14:58:33 +01002240 pr_info("Suspending console(s) (use no_console_suspend to debug)\n");
Torben Hohnac751ef2011-01-25 15:07:35 -08002241 console_lock();
Linus Torvalds557240b2006-06-19 18:16:01 -07002242 console_suspended = 1;
Jan Karabd8d7cf2014-06-04 16:11:36 -07002243 up_console_sem();
Linus Torvalds557240b2006-06-19 18:16:01 -07002244}
2245
2246void resume_console(void)
2247{
Andres Salomon8f4ce8c2007-10-18 03:04:50 -07002248 if (!console_suspend_enabled)
2249 return;
Jan Karabd8d7cf2014-06-04 16:11:36 -07002250 down_console_sem();
Linus Torvalds557240b2006-06-19 18:16:01 -07002251 console_suspended = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08002252 console_unlock();
Linus Torvalds557240b2006-06-19 18:16:01 -07002253}
2254
2255/**
Kevin Cernekee034260d2010-06-03 22:11:25 -07002256 * console_cpu_notify - print deferred console messages after CPU hotplug
Sebastian Andrzej Siewior90b14882016-11-03 15:49:58 +01002257 * @cpu: unused
Kevin Cernekee034260d2010-06-03 22:11:25 -07002258 *
2259 * If printk() is called from a CPU that is not online yet, the messages
Sergey Senozhatsky64ca7522017-01-21 19:47:29 +09002260 * will be printed on the console only if there are CON_ANYTIME consoles.
2261 * This function is called when a new CPU comes online (or fails to come
2262 * up) or goes offline.
Kevin Cernekee034260d2010-06-03 22:11:25 -07002263 */
Sebastian Andrzej Siewior90b14882016-11-03 15:49:58 +01002264static int console_cpu_notify(unsigned int cpu)
Kevin Cernekee034260d2010-06-03 22:11:25 -07002265{
Thomas Gleixnerf97960f2016-11-17 17:31:55 +01002266 if (!cpuhp_tasks_frozen) {
Sergey Senozhatsky64ca7522017-01-21 19:47:29 +09002267 /* If trylock fails, someone else is doing the printing */
2268 if (console_trylock())
2269 console_unlock();
Kevin Cernekee034260d2010-06-03 22:11:25 -07002270 }
Sebastian Andrzej Siewior90b14882016-11-03 15:49:58 +01002271 return 0;
Kevin Cernekee034260d2010-06-03 22:11:25 -07002272}
2273
2274/**
Torben Hohnac751ef2011-01-25 15:07:35 -08002275 * console_lock - lock the console system for exclusive use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002277 * Acquires a lock which guarantees that the caller has
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 * exclusive access to the console system and the console_drivers list.
2279 *
2280 * Can sleep, returns nothing.
2281 */
Torben Hohnac751ef2011-01-25 15:07:35 -08002282void console_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283{
Daniel Vetter6b898c02012-09-17 23:03:31 +00002284 might_sleep();
2285
Jan Karabd8d7cf2014-06-04 16:11:36 -07002286 down_console_sem();
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01002287 if (console_suspended)
2288 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 console_locked = 1;
2290 console_may_schedule = 1;
2291}
Torben Hohnac751ef2011-01-25 15:07:35 -08002292EXPORT_SYMBOL(console_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Torben Hohnac751ef2011-01-25 15:07:35 -08002294/**
2295 * console_trylock - try to lock the console system for exclusive use.
2296 *
Alex Elder0b90fec2014-08-06 16:09:03 -07002297 * Try to acquire a lock which guarantees that the caller has exclusive
2298 * access to the console system and the console_drivers list.
Torben Hohnac751ef2011-01-25 15:07:35 -08002299 *
2300 * returns 1 on success, and 0 on failure to acquire the lock.
2301 */
2302int console_trylock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303{
Jan Karabd8d7cf2014-06-04 16:11:36 -07002304 if (down_trylock_console_sem())
Torben Hohnac751ef2011-01-25 15:07:35 -08002305 return 0;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01002306 if (console_suspended) {
Jan Karabd8d7cf2014-06-04 16:11:36 -07002307 up_console_sem();
Torben Hohnac751ef2011-01-25 15:07:35 -08002308 return 0;
Arve Hjønnevåg403f3072009-02-14 02:07:24 +01002309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 console_locked = 1;
Sergey Senozhatskyfd5f7cd2018-01-16 13:47:16 +09002311 console_may_schedule = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08002312 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313}
Torben Hohnac751ef2011-01-25 15:07:35 -08002314EXPORT_SYMBOL(console_trylock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316int is_console_locked(void)
2317{
2318 return console_locked;
2319}
Hans de Goeded48de542018-06-28 15:20:27 +02002320EXPORT_SYMBOL(is_console_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Sergey Senozhatskya8199372016-03-17 14:21:20 -07002322/*
2323 * Check if we have any console that is capable of printing while cpu is
2324 * booting or shutting down. Requires console_sem.
2325 */
2326static int have_callable_console(void)
2327{
2328 struct console *con;
2329
2330 for_each_console(con)
Sergey Senozhatskyadaf6592016-03-17 14:21:27 -07002331 if ((con->flags & CON_ENABLED) &&
2332 (con->flags & CON_ANYTIME))
Sergey Senozhatskya8199372016-03-17 14:21:20 -07002333 return 1;
2334
2335 return 0;
2336}
2337
2338/*
2339 * Can we actually use the console at this time on this cpu?
2340 *
2341 * Console drivers may assume that per-cpu resources have been allocated. So
2342 * unless they're explicitly marked as being able to cope (CON_ANYTIME) don't
2343 * call them until this CPU is officially up.
2344 */
2345static inline int can_use_console(void)
2346{
2347 return cpu_online(raw_smp_processor_id()) || have_callable_console();
2348}
2349
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350/**
Torben Hohnac751ef2011-01-25 15:07:35 -08002351 * console_unlock - unlock the console system
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002353 * Releases the console_lock which the caller holds on the console system
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 * and the console driver list.
2355 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002356 * While the console_lock was held, console output may have been buffered
2357 * by printk(). If this is the case, console_unlock(); emits
2358 * the output prior to releasing the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 *
Kay Sievers7f3a7812012-05-09 01:37:51 +02002360 * If there is output waiting, we wake /dev/kmsg and syslog() users.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002362 * console_unlock(); may be called from any context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 */
Torben Hohnac751ef2011-01-25 15:07:35 -08002364void console_unlock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365{
Tejun Heo6fe29352015-06-25 15:01:30 -07002366 static char ext_text[CONSOLE_EXT_LOG_MAX];
Kay Sievers70498252012-07-16 18:35:29 -07002367 static char text[LOG_LINE_MAX + PREFIX_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 unsigned long flags;
Tejun Heo8d91f8b2016-01-15 16:58:24 -08002369 bool do_cond_resched, retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Linus Torvalds557240b2006-06-19 18:16:01 -07002371 if (console_suspended) {
Jan Karabd8d7cf2014-06-04 16:11:36 -07002372 up_console_sem();
Linus Torvalds557240b2006-06-19 18:16:01 -07002373 return;
2374 }
Antonino A. Daplas78944e542006-08-05 12:14:16 -07002375
Tejun Heo8d91f8b2016-01-15 16:58:24 -08002376 /*
Petr Mladek257ab442017-03-24 17:14:05 +01002377 * Console drivers are called with interrupts disabled, so
Tejun Heo8d91f8b2016-01-15 16:58:24 -08002378 * @console_may_schedule should be cleared before; however, we may
2379 * end up dumping a lot of lines, for example, if called from
2380 * console registration path, and should invoke cond_resched()
2381 * between lines if allowable. Not doing so can cause a very long
2382 * scheduling stall on a slow console leading to RCU stall and
2383 * softlockup warnings which exacerbate the issue with more
2384 * messages practically incapacitating the system.
Petr Mladek257ab442017-03-24 17:14:05 +01002385 *
2386 * console_trylock() is not able to detect the preemptive
2387 * context reliably. Therefore the value must be stored before
2388 * and cleared after the the "again" goto label.
Tejun Heo8d91f8b2016-01-15 16:58:24 -08002389 */
2390 do_cond_resched = console_may_schedule;
Petr Mladek257ab442017-03-24 17:14:05 +01002391again:
Antonino A. Daplas78944e542006-08-05 12:14:16 -07002392 console_may_schedule = 0;
2393
Sergey Senozhatskya8199372016-03-17 14:21:20 -07002394 /*
2395 * We released the console_sem lock, so we need to recheck if
2396 * cpu is online and (if not) is there at least one CON_ANYTIME
2397 * console.
2398 */
2399 if (!can_use_console()) {
2400 console_locked = 0;
2401 up_console_sem();
2402 return;
2403 }
2404
Kay Sievers7ff95542012-05-03 02:29:13 +02002405 for (;;) {
Joe Perches62e32ac2013-07-31 13:53:47 -07002406 struct printk_log *msg;
Tejun Heo6fe29352015-06-25 15:01:30 -07002407 size_t ext_len = 0;
Kay Sievers3ce9a7c2012-05-13 23:30:46 +02002408 size_t len;
Kay Sievers7ff95542012-05-03 02:29:13 +02002409
Sergey Senozhatskyf9752372016-12-27 23:16:09 +09002410 printk_safe_enter_irqsave(flags);
2411 raw_spin_lock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02002412 if (console_seq < log_first_seq) {
He Zhe51a72ab2018-09-30 00:45:51 +08002413 len = sprintf(text,
2414 "** %llu printk messages dropped **\n",
2415 log_first_seq - console_seq);
Will Deacon84b5ec82014-06-04 16:11:45 -07002416
Kay Sievers7ff95542012-05-03 02:29:13 +02002417 /* messages are gone, move to first one */
2418 console_seq = log_first_seq;
2419 console_idx = log_first_idx;
Will Deacon84b5ec82014-06-04 16:11:45 -07002420 } else {
2421 len = 0;
Kay Sievers7ff95542012-05-03 02:29:13 +02002422 }
Kay Sievers084681d2012-06-28 09:38:53 +02002423skip:
Kay Sievers7ff95542012-05-03 02:29:13 +02002424 if (console_seq == log_next_seq)
2425 break;
2426
2427 msg = log_from_idx(console_idx);
Petr Mladeka6ae9282018-09-10 15:52:06 +02002428 if (suppress_message_printing(msg->level)) {
Kay Sievers084681d2012-06-28 09:38:53 +02002429 /*
Petr Mladeka6ae9282018-09-10 15:52:06 +02002430 * Skip record we have buffered and already printed
2431 * directly to the console when we received it, and
2432 * record that has level above the console loglevel.
Kay Sievers084681d2012-06-28 09:38:53 +02002433 */
2434 console_idx = log_next(console_idx);
2435 console_seq++;
2436 goto skip;
2437 }
Kay Sievers649e6ee2012-05-10 04:30:45 +02002438
Petr Mladekf92b0702018-09-13 14:34:06 +02002439 /* Output to all consoles once old messages replayed. */
2440 if (unlikely(exclusive_console &&
2441 console_seq >= exclusive_console_stop_seq)) {
2442 exclusive_console = NULL;
2443 }
2444
Sergey Senozhatskycca10d52017-12-21 14:41:49 +09002445 len += msg_print_text(msg,
2446 console_msg_format & MSG_FORMAT_SYSLOG,
Tetsuo Handae80c1a92018-12-04 19:00:01 +09002447 printk_time, text + len, sizeof(text) - len);
Tejun Heo6fe29352015-06-25 15:01:30 -07002448 if (nr_ext_console_drivers) {
2449 ext_len = msg_print_ext_header(ext_text,
2450 sizeof(ext_text),
Linus Torvalds5aa068e2016-10-25 11:27:31 -07002451 msg, console_seq);
Tejun Heo6fe29352015-06-25 15:01:30 -07002452 ext_len += msg_print_ext_body(ext_text + ext_len,
2453 sizeof(ext_text) - ext_len,
2454 log_dict(msg), msg->dict_len,
2455 log_text(msg), msg->text_len);
2456 }
Kay Sievers7ff95542012-05-03 02:29:13 +02002457 console_idx = log_next(console_idx);
2458 console_seq++;
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002459 raw_spin_unlock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02002460
Steven Rostedt (VMware)dbdda842018-01-10 14:24:17 +01002461 /*
2462 * While actively printing out messages, if another printk()
2463 * were to occur on another CPU, it may wait for this one to
2464 * finish. This task can not be preempted if there is a
2465 * waiter waiting to take over.
2466 */
Petr Mladekc162d5b2018-01-12 17:08:37 +01002467 console_lock_spinning_enable();
Steven Rostedt (VMware)dbdda842018-01-10 14:24:17 +01002468
Steven Rostedt81d68a92008-05-12 21:20:42 +02002469 stop_critical_timings(); /* don't trace print latency */
Sergey Senozhatskyd9c23522016-12-24 23:09:01 +09002470 call_console_drivers(ext_text, ext_len, text, len);
Steven Rostedt81d68a92008-05-12 21:20:42 +02002471 start_critical_timings();
Steven Rostedt (VMware)dbdda842018-01-10 14:24:17 +01002472
Petr Mladekc162d5b2018-01-12 17:08:37 +01002473 if (console_lock_spinning_disable_and_check()) {
2474 printk_safe_exit_irqrestore(flags);
Sergey Senozhatsky43a17112018-04-19 10:42:50 +09002475 return;
Petr Mladekc162d5b2018-01-12 17:08:37 +01002476 }
Steven Rostedt (VMware)dbdda842018-01-10 14:24:17 +01002477
Sergey Senozhatskyf9752372016-12-27 23:16:09 +09002478 printk_safe_exit_irqrestore(flags);
Tejun Heo8d91f8b2016-01-15 16:58:24 -08002479
2480 if (do_cond_resched)
2481 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 }
Steven Rostedt (VMware)dbdda842018-01-10 14:24:17 +01002483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 console_locked = 0;
Feng Tangfe3d8ad2011-03-22 16:34:21 -07002485
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002486 raw_spin_unlock(&logbuf_lock);
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002487
Jan Karabd8d7cf2014-06-04 16:11:36 -07002488 up_console_sem();
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002489
2490 /*
2491 * Someone could have filled up the buffer again, so re-check if there's
2492 * something to flush. In case we cannot trylock the console_sem again,
2493 * there's a new owner and the console_unlock() from them will do the
2494 * flush, no worries.
2495 */
Thomas Gleixner07354eb2009-07-25 17:50:36 +02002496 raw_spin_lock(&logbuf_lock);
Kay Sievers7ff95542012-05-03 02:29:13 +02002497 retry = console_seq != log_next_seq;
Sergey Senozhatskyf9752372016-12-27 23:16:09 +09002498 raw_spin_unlock(&logbuf_lock);
2499 printk_safe_exit_irqrestore(flags);
Peter Zijlstra09dc3cf2011-12-08 14:34:13 -08002500
Peter Zijlstra4f2a8d32011-06-22 11:20:09 +02002501 if (retry && console_trylock())
2502 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
Torben Hohnac751ef2011-01-25 15:07:35 -08002504EXPORT_SYMBOL(console_unlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
Martin Waitzddad86c2005-11-13 16:08:14 -08002506/**
2507 * console_conditional_schedule - yield the CPU if required
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 *
2509 * If the console code is currently allowed to sleep, and
2510 * if this CPU should yield the CPU to another task, do
2511 * so here.
2512 *
Torben Hohnac751ef2011-01-25 15:07:35 -08002513 * Must be called within console_lock();.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 */
2515void __sched console_conditional_schedule(void)
2516{
2517 if (console_may_schedule)
2518 cond_resched();
2519}
2520EXPORT_SYMBOL(console_conditional_schedule);
2521
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522void console_unblank(void)
2523{
2524 struct console *c;
2525
2526 /*
2527 * console_unblank can no longer be called in interrupt context unless
2528 * oops_in_progress is set to 1..
2529 */
2530 if (oops_in_progress) {
Jan Karabd8d7cf2014-06-04 16:11:36 -07002531 if (down_trylock_console_sem() != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 return;
2533 } else
Torben Hohnac751ef2011-01-25 15:07:35 -08002534 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
2536 console_locked = 1;
2537 console_may_schedule = 0;
Robin Getz4d091612009-07-01 21:08:37 -04002538 for_each_console(c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 if ((c->flags & CON_ENABLED) && c->unblank)
2540 c->unblank();
Torben Hohnac751ef2011-01-25 15:07:35 -08002541 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
Tejun Heo8d91f8b2016-01-15 16:58:24 -08002544/**
2545 * console_flush_on_panic - flush console content on panic
Feng Tangde6da1e2019-05-17 14:31:50 -07002546 * @mode: flush all messages in buffer or just the pending ones
Tejun Heo8d91f8b2016-01-15 16:58:24 -08002547 *
2548 * Immediately output all pending messages no matter what.
2549 */
Feng Tangde6da1e2019-05-17 14:31:50 -07002550void console_flush_on_panic(enum con_flush_mode mode)
Tejun Heo8d91f8b2016-01-15 16:58:24 -08002551{
2552 /*
2553 * If someone else is holding the console lock, trylock will fail
2554 * and may_schedule may be set. Ignore and proceed to unlock so
2555 * that messages are flushed out. As this can be called from any
2556 * context and we don't want to get preempted while flushing,
2557 * ensure may_schedule is cleared.
2558 */
2559 console_trylock();
2560 console_may_schedule = 0;
Feng Tangde6da1e2019-05-17 14:31:50 -07002561
2562 if (mode == CONSOLE_REPLAY_ALL) {
2563 unsigned long flags;
2564
2565 logbuf_lock_irqsave(flags);
2566 console_seq = log_first_seq;
2567 console_idx = log_first_idx;
2568 logbuf_unlock_irqrestore(flags);
2569 }
Tejun Heo8d91f8b2016-01-15 16:58:24 -08002570 console_unlock();
2571}
2572
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573/*
2574 * Return the console tty driver structure and its associated index
2575 */
2576struct tty_driver *console_device(int *index)
2577{
2578 struct console *c;
2579 struct tty_driver *driver = NULL;
2580
Torben Hohnac751ef2011-01-25 15:07:35 -08002581 console_lock();
Robin Getz4d091612009-07-01 21:08:37 -04002582 for_each_console(c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 if (!c->device)
2584 continue;
2585 driver = c->device(c, index);
2586 if (driver)
2587 break;
2588 }
Torben Hohnac751ef2011-01-25 15:07:35 -08002589 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 return driver;
2591}
2592
2593/*
2594 * Prevent further output on the passed console device so that (for example)
2595 * serial drivers can disable console output before suspending a port, and can
2596 * re-enable output afterwards.
2597 */
2598void console_stop(struct console *console)
2599{
Torben Hohnac751ef2011-01-25 15:07:35 -08002600 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 console->flags &= ~CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08002602 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603}
2604EXPORT_SYMBOL(console_stop);
2605
2606void console_start(struct console *console)
2607{
Torben Hohnac751ef2011-01-25 15:07:35 -08002608 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 console->flags |= CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08002610 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611}
2612EXPORT_SYMBOL(console_start);
2613
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07002614static int __read_mostly keep_bootcon;
2615
2616static int __init keep_bootcon_setup(char *str)
2617{
2618 keep_bootcon = 1;
Andrew Morton27083ba2013-11-12 15:08:50 -08002619 pr_info("debug: skip boot console de-registration.\n");
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07002620
2621 return 0;
2622}
2623
2624early_param("keep_bootcon", keep_bootcon_setup);
2625
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626/*
2627 * The console driver calls this routine during kernel initialization
2628 * to register the console printing procedure with printk() and to
2629 * print any messages that were printed by the kernel before the
2630 * console driver was initialized.
Robin Getz4d091612009-07-01 21:08:37 -04002631 *
2632 * This can happen pretty early during the boot process (because of
2633 * early_printk) - sometimes before setup_arch() completes - be careful
2634 * of what kernel features are used - they may not be initialised yet.
2635 *
2636 * There are two types of consoles - bootconsoles (early_printk) and
2637 * "real" consoles (everything which is not a bootconsole) which are
2638 * handled differently.
2639 * - Any number of bootconsoles can be registered at any time.
2640 * - As soon as a "real" console is registered, all bootconsoles
2641 * will be unregistered automatically.
2642 * - Once a "real" console is registered, any attempt to register a
2643 * bootconsoles will be rejected
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 */
Robin Getz4d091612009-07-01 21:08:37 -04002645void register_console(struct console *newcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646{
Jesper Juhl40dc5652005-10-30 15:02:46 -08002647 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 unsigned long flags;
Robin Getz4d091612009-07-01 21:08:37 -04002649 struct console *bcon = NULL;
Joe Perches23475402013-07-31 13:53:46 -07002650 struct console_cmdline *c;
Aleksey Makarovb077baf2017-03-15 13:28:50 +03002651 static bool has_preferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
Andy Shevchenkocaa72c32020-02-03 15:31:25 +02002653 for_each_console(bcon) {
2654 if (WARN(bcon == newcon, "console '%s%d' already registered\n",
2655 bcon->name, bcon->index))
2656 return;
2657 }
Andreas Bießmann16cf48a2013-08-02 12:23:34 +02002658
Robin Getz4d091612009-07-01 21:08:37 -04002659 /*
2660 * before we register a new CON_BOOT console, make sure we don't
2661 * already have a valid console
2662 */
Andy Shevchenkocaa72c32020-02-03 15:31:25 +02002663 if (newcon->flags & CON_BOOT) {
Robin Getz4d091612009-07-01 21:08:37 -04002664 for_each_console(bcon) {
2665 if (!(bcon->flags & CON_BOOT)) {
Andrew Morton27083ba2013-11-12 15:08:50 -08002666 pr_info("Too late to register bootconsole %s%d\n",
Robin Getz4d091612009-07-01 21:08:37 -04002667 newcon->name, newcon->index);
2668 return;
2669 }
2670 }
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002671 }
2672
Robin Getz4d091612009-07-01 21:08:37 -04002673 if (console_drivers && console_drivers->flags & CON_BOOT)
2674 bcon = console_drivers;
2675
Aleksey Makarovb077baf2017-03-15 13:28:50 +03002676 if (!has_preferred || bcon || !console_drivers)
Aleksey Makarovad86ee22017-03-15 13:28:51 +03002677 has_preferred = preferred_console >= 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
2679 /*
2680 * See if we want to use this console driver. If we
2681 * didn't select a console we take the first one
2682 * that registers here.
2683 */
Aleksey Makarovb077baf2017-03-15 13:28:50 +03002684 if (!has_preferred) {
Robin Getz4d091612009-07-01 21:08:37 -04002685 if (newcon->index < 0)
2686 newcon->index = 0;
2687 if (newcon->setup == NULL ||
2688 newcon->setup(newcon, NULL) == 0) {
2689 newcon->flags |= CON_ENABLED;
2690 if (newcon->device) {
2691 newcon->flags |= CON_CONSDEV;
Aleksey Makarovb077baf2017-03-15 13:28:50 +03002692 has_preferred = true;
Jan Kiszkacd3a1b82008-05-12 21:21:04 +02002693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 }
2695 }
2696
2697 /*
Petr Mladekdac8bbb2017-06-08 12:01:30 +02002698 * See if this console matches one we selected on
2699 * the command line.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 */
Petr Mladekdac8bbb2017-06-08 12:01:30 +02002701 for (i = 0, c = console_cmdline;
2702 i < MAX_CMDLINECONSOLES && c->name[0];
2703 i++, c++) {
Peter Hurleyc7cef0a2015-03-09 16:27:12 -04002704 if (!newcon->match ||
2705 newcon->match(newcon, c->name, c->index, c->options) != 0) {
2706 /* default matching */
2707 BUILD_BUG_ON(sizeof(c->name) != sizeof(newcon->name));
2708 if (strcmp(c->name, newcon->name) != 0)
2709 continue;
2710 if (newcon->index >= 0 &&
2711 newcon->index != c->index)
2712 continue;
2713 if (newcon->index < 0)
2714 newcon->index = c->index;
Joe Perchesbbeddf52013-07-31 13:53:45 -07002715
Peter Hurleyc7cef0a2015-03-09 16:27:12 -04002716 if (_braille_register_console(newcon, c))
2717 return;
Joe Perchesbbeddf52013-07-31 13:53:45 -07002718
Peter Hurleyc7cef0a2015-03-09 16:27:12 -04002719 if (newcon->setup &&
2720 newcon->setup(newcon, c->options) != 0)
2721 break;
2722 }
2723
Robin Getz4d091612009-07-01 21:08:37 -04002724 newcon->flags |= CON_ENABLED;
Aleksey Makarovad86ee22017-03-15 13:28:51 +03002725 if (i == preferred_console) {
Robin Getz4d091612009-07-01 21:08:37 -04002726 newcon->flags |= CON_CONSDEV;
Aleksey Makarovb077baf2017-03-15 13:28:50 +03002727 has_preferred = true;
Greg Edwardsab4af032005-06-23 00:09:05 -07002728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 break;
2730 }
2731
Robin Getz4d091612009-07-01 21:08:37 -04002732 if (!(newcon->flags & CON_ENABLED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 return;
2734
Robin Getz8259cf42009-07-09 13:08:37 -04002735 /*
2736 * If we have a bootconsole, and are switching to a real console,
2737 * don't print everything out again, since when the boot console, and
2738 * the real console are the same physical device, it's annoying to
2739 * see the beginning boot messages twice
2740 */
2741 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
Robin Getz4d091612009-07-01 21:08:37 -04002742 newcon->flags &= ~CON_PRINTBUFFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
2744 /*
2745 * Put this console in the list - keep the
2746 * preferred driver at the head of the list.
2747 */
Torben Hohnac751ef2011-01-25 15:07:35 -08002748 console_lock();
Robin Getz4d091612009-07-01 21:08:37 -04002749 if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
2750 newcon->next = console_drivers;
2751 console_drivers = newcon;
2752 if (newcon->next)
2753 newcon->next->flags &= ~CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 } else {
Robin Getz4d091612009-07-01 21:08:37 -04002755 newcon->next = console_drivers->next;
2756 console_drivers->next = newcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 }
Tejun Heo6fe29352015-06-25 15:01:30 -07002758
2759 if (newcon->flags & CON_EXTENDED)
Sergey Senozhatsky96278082018-10-02 11:38:34 +09002760 nr_ext_console_drivers++;
Tejun Heo6fe29352015-06-25 15:01:30 -07002761
Robin Getz4d091612009-07-01 21:08:37 -04002762 if (newcon->flags & CON_PRINTBUFFER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 /*
Torben Hohnac751ef2011-01-25 15:07:35 -08002764 * console_unlock(); will print out the buffered messages
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 * for us.
2766 */
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09002767 logbuf_lock_irqsave(flags);
Feng Tangfe3d8ad2011-03-22 16:34:21 -07002768 /*
2769 * We're about to replay the log buffer. Only do this to the
2770 * just-registered console to avoid excessive message spam to
2771 * the already-registered consoles.
Sergey Senozhatsky884e3702018-09-28 18:53:04 +09002772 *
2773 * Set exclusive_console with disabled interrupts to reduce
2774 * race window with eventual console_flush_on_panic() that
2775 * ignores console_lock.
Feng Tangfe3d8ad2011-03-22 16:34:21 -07002776 */
2777 exclusive_console = newcon;
Petr Mladekf92b0702018-09-13 14:34:06 +02002778 exclusive_console_stop_seq = console_seq;
John Ognessdef97da2019-12-19 12:59:22 +01062779 console_seq = syslog_seq;
2780 console_idx = syslog_idx;
Sergey Senozhatsky884e3702018-09-28 18:53:04 +09002781 logbuf_unlock_irqrestore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 }
Torben Hohnac751ef2011-01-25 15:07:35 -08002783 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01002784 console_sysfs_notify();
Robin Getz8259cf42009-07-09 13:08:37 -04002785
2786 /*
2787 * By unregistering the bootconsoles after we enable the real console
2788 * we get the "console xxx enabled" message on all the consoles -
2789 * boot consoles, real consoles, etc - this is to ensure that end
2790 * users know there might be something in the kernel's log buffer that
2791 * went to the bootconsole (that they do not see on the real console)
2792 */
Andrew Morton27083ba2013-11-12 15:08:50 -08002793 pr_info("%sconsole [%s%d] enabled\n",
Kees Cook6b802392013-11-12 15:08:49 -08002794 (newcon->flags & CON_BOOT) ? "boot" : "" ,
2795 newcon->name, newcon->index);
Fabio M. Di Nitto7bf69392011-03-22 16:34:20 -07002796 if (bcon &&
2797 ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
2798 !keep_bootcon) {
Kees Cook6b802392013-11-12 15:08:49 -08002799 /* We need to iterate through all boot consoles, to make
2800 * sure we print everything out, before we unregister them.
Robin Getz8259cf42009-07-09 13:08:37 -04002801 */
Robin Getz8259cf42009-07-09 13:08:37 -04002802 for_each_console(bcon)
2803 if (bcon->flags & CON_BOOT)
2804 unregister_console(bcon);
Robin Getz8259cf42009-07-09 13:08:37 -04002805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806}
2807EXPORT_SYMBOL(register_console);
2808
Jesper Juhl40dc5652005-10-30 15:02:46 -08002809int unregister_console(struct console *console)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810{
Andy Shevchenko12825e62020-02-03 15:31:26 +02002811 struct console *con;
Joe Perchesbbeddf52013-07-31 13:53:45 -07002812 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
Andrew Morton27083ba2013-11-12 15:08:50 -08002814 pr_info("%sconsole [%s%d] disabled\n",
Kees Cook6b802392013-11-12 15:08:49 -08002815 (console->flags & CON_BOOT) ? "boot" : "" ,
2816 console->name, console->index);
2817
Joe Perchesbbeddf52013-07-31 13:53:45 -07002818 res = _braille_unregister_console(console);
Andy Shevchenkobb72e392020-02-03 15:31:28 +02002819 if (res < 0)
Joe Perchesbbeddf52013-07-31 13:53:45 -07002820 return res;
Andy Shevchenkobb72e392020-02-03 15:31:28 +02002821 if (res > 0)
2822 return 0;
Samuel Thibaultf7511d52008-04-30 00:54:51 -07002823
Andy Shevchenkobb72e392020-02-03 15:31:28 +02002824 res = -ENODEV;
Torben Hohnac751ef2011-01-25 15:07:35 -08002825 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 if (console_drivers == console) {
2827 console_drivers=console->next;
2828 res = 0;
Andy Shevchenko12825e62020-02-03 15:31:26 +02002829 } else {
2830 for_each_console(con) {
2831 if (con->next == console) {
2832 con->next = console->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 res = 0;
2834 break;
Jesper Juhl40dc5652005-10-30 15:02:46 -08002835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 }
2837 }
Jesper Juhl40dc5652005-10-30 15:02:46 -08002838
Tejun Heo6fe29352015-06-25 15:01:30 -07002839 if (!res && (console->flags & CON_EXTENDED))
2840 nr_ext_console_drivers--;
2841
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002842 /*
Greg Edwardsab4af032005-06-23 00:09:05 -07002843 * If this isn't the last console and it has CON_CONSDEV set, we
2844 * need to set it on the next preferred console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 */
Gerd Hoffmann69331af2007-05-08 00:26:49 -07002846 if (console_drivers != NULL && console->flags & CON_CONSDEV)
Greg Edwardsab4af032005-06-23 00:09:05 -07002847 console_drivers->flags |= CON_CONSDEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
Stephen Chivers7fa21dd2014-05-14 08:04:39 +10002849 console->flags &= ~CON_ENABLED;
Torben Hohnac751ef2011-01-25 15:07:35 -08002850 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01002851 console_sysfs_notify();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 return res;
2853}
2854EXPORT_SYMBOL(unregister_console);
Matt Mackalld59745c2005-05-01 08:59:02 -07002855
Thierry Reding81cc26f2016-01-15 16:58:21 -08002856/*
Nicolas Pitre0c688612017-04-12 18:37:14 -04002857 * Initialize the console device. This is called *early*, so
2858 * we can't necessarily depend on lots of kernel help here.
2859 * Just do some early initializations, and do the complex setup
2860 * later.
2861 */
2862void __init console_init(void)
2863{
Abderrahmane Benbachir58eacff2018-03-22 20:33:28 -04002864 int ret;
Ard Biesheuvel1b1eeca2018-08-21 21:56:13 -07002865 initcall_t call;
2866 initcall_entry_t *ce;
Nicolas Pitre0c688612017-04-12 18:37:14 -04002867
2868 /* Setup the default TTY line discipline. */
2869 n_tty_init();
2870
2871 /*
2872 * set up the console device so that later boot sequences can
2873 * inform about problems etc..
2874 */
Ard Biesheuvel1b1eeca2018-08-21 21:56:13 -07002875 ce = __con_initcall_start;
Abderrahmane Benbachir58eacff2018-03-22 20:33:28 -04002876 trace_initcall_level("console");
Ard Biesheuvel1b1eeca2018-08-21 21:56:13 -07002877 while (ce < __con_initcall_end) {
2878 call = initcall_from_entry(ce);
2879 trace_initcall_start(call);
2880 ret = call();
2881 trace_initcall_finish(call, ret);
2882 ce++;
Nicolas Pitre0c688612017-04-12 18:37:14 -04002883 }
2884}
2885
2886/*
Thierry Reding81cc26f2016-01-15 16:58:21 -08002887 * Some boot consoles access data that is in the init section and which will
2888 * be discarded after the initcalls have been run. To make sure that no code
2889 * will access this data, unregister the boot consoles in a late initcall.
2890 *
2891 * If for some reason, such as deferred probe or the driver being a loadable
2892 * module, the real console hasn't registered yet at this point, there will
2893 * be a brief interval in which no messages are logged to the console, which
2894 * makes it difficult to diagnose problems that occur during this time.
2895 *
2896 * To mitigate this problem somewhat, only unregister consoles whose memory
Matt Redfearn2b1be682017-07-14 14:51:12 +02002897 * intersects with the init section. Note that all other boot consoles will
2898 * get unregistred when the real preferred console is registered.
Thierry Reding81cc26f2016-01-15 16:58:21 -08002899 */
Kevin Cernekee034260d2010-06-03 22:11:25 -07002900static int __init printk_late_init(void)
Robin Getz0c5564b2007-08-20 15:22:47 -04002901{
Robin Getz4d091612009-07-01 21:08:37 -04002902 struct console *con;
Sebastian Andrzej Siewior90b14882016-11-03 15:49:58 +01002903 int ret;
Robin Getz4d091612009-07-01 21:08:37 -04002904
2905 for_each_console(con) {
Petr Mladek5a814232017-07-14 14:51:13 +02002906 if (!(con->flags & CON_BOOT))
2907 continue;
2908
2909 /* Check addresses that might be used for enabled consoles. */
2910 if (init_section_intersects(con, sizeof(*con)) ||
2911 init_section_contains(con->write, 0) ||
2912 init_section_contains(con->read, 0) ||
2913 init_section_contains(con->device, 0) ||
2914 init_section_contains(con->unblank, 0) ||
2915 init_section_contains(con->data, 0)) {
Thierry Reding81cc26f2016-01-15 16:58:21 -08002916 /*
Matt Redfearn2b1be682017-07-14 14:51:12 +02002917 * Please, consider moving the reported consoles out
2918 * of the init section.
Thierry Reding81cc26f2016-01-15 16:58:21 -08002919 */
Matt Redfearn2b1be682017-07-14 14:51:12 +02002920 pr_warn("bootconsole [%s%d] uses init memory and must be disabled even before the real one is ready\n",
2921 con->name, con->index);
2922 unregister_console(con);
Robin Getzcb00e992007-08-21 23:14:58 -04002923 }
Robin Getz0c5564b2007-08-20 15:22:47 -04002924 }
Sebastian Andrzej Siewior90b14882016-11-03 15:49:58 +01002925 ret = cpuhp_setup_state_nocalls(CPUHP_PRINTK_DEAD, "printk:dead", NULL,
2926 console_cpu_notify);
2927 WARN_ON(ret < 0);
2928 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "printk:online",
2929 console_cpu_notify, NULL);
2930 WARN_ON(ret < 0);
Robin Getz0c5564b2007-08-20 15:22:47 -04002931 return 0;
2932}
Kevin Cernekee034260d2010-06-03 22:11:25 -07002933late_initcall(printk_late_init);
Robin Getz0c5564b2007-08-20 15:22:47 -04002934
Joe Perches7ef3d2f2008-02-08 04:21:25 -08002935#if defined CONFIG_PRINTK
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002936/*
2937 * Delayed printk version, for scheduler-internal messages:
2938 */
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002939#define PRINTK_PENDING_WAKEUP 0x01
Steven Rostedt458df9f2014-06-04 16:11:38 -07002940#define PRINTK_PENDING_OUTPUT 0x02
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002941
2942static DEFINE_PER_CPU(int, printk_pending);
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002943
2944static void wake_up_klogd_work_func(struct irq_work *irq_work)
2945{
2946 int pending = __this_cpu_xchg(printk_pending, 0);
2947
Steven Rostedt458df9f2014-06-04 16:11:38 -07002948 if (pending & PRINTK_PENDING_OUTPUT) {
2949 /* If trylock fails, someone else is doing the printing */
2950 if (console_trylock())
2951 console_unlock();
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002952 }
2953
2954 if (pending & PRINTK_PENDING_WAKEUP)
2955 wake_up_interruptible(&log_wait);
2956}
2957
2958static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) = {
2959 .func = wake_up_klogd_work_func,
Frederic Weisbecker153bedb2019-11-08 17:08:55 +01002960 .flags = ATOMIC_INIT(IRQ_WORK_LAZY),
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002961};
2962
2963void wake_up_klogd(void)
2964{
2965 preempt_disable();
2966 if (waitqueue_active(&log_wait)) {
2967 this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP);
Christoph Lameterbb964a92014-08-17 12:30:24 -05002968 irq_work_queue(this_cpu_ptr(&wake_up_klogd_work));
Frederic Weisbeckerdc72c322013-03-22 15:04:39 -07002969 }
2970 preempt_enable();
2971}
Dave Young717115e2008-07-25 01:45:58 -07002972
Petr Mladeka338f842018-06-27 16:08:16 +02002973void defer_console_output(void)
2974{
2975 preempt_disable();
2976 __this_cpu_or(printk_pending, PRINTK_PENDING_OUTPUT);
2977 irq_work_queue(this_cpu_ptr(&wake_up_klogd_work));
2978 preempt_enable();
2979}
2980
Petr Mladek719f6a72017-04-20 10:52:31 +02002981int vprintk_deferred(const char *fmt, va_list args)
2982{
2983 int r;
2984
2985 r = vprintk_emit(0, LOGLEVEL_SCHED, NULL, 0, fmt, args);
Petr Mladeka338f842018-06-27 16:08:16 +02002986 defer_console_output();
Petr Mladek719f6a72017-04-20 10:52:31 +02002987
2988 return r;
2989}
2990
John Stultzaac74dc2014-06-04 16:11:40 -07002991int printk_deferred(const char *fmt, ...)
Peter Zijlstra600e1452012-03-15 12:35:37 +01002992{
Peter Zijlstra600e1452012-03-15 12:35:37 +01002993 va_list args;
Peter Zijlstra600e1452012-03-15 12:35:37 +01002994 int r;
2995
Peter Zijlstra600e1452012-03-15 12:35:37 +01002996 va_start(args, fmt);
Petr Mladek719f6a72017-04-20 10:52:31 +02002997 r = vprintk_deferred(fmt, args);
Peter Zijlstra600e1452012-03-15 12:35:37 +01002998 va_end(args);
2999
Peter Zijlstra600e1452012-03-15 12:35:37 +01003000 return r;
3001}
3002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003/*
3004 * printk rate limiting, lifted from the networking subsystem.
3005 *
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07003006 * This enforces a rate limit: not more than 10 kernel messages
3007 * every 5s to make a denial-of-service attack impossible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 */
Uwe Kleine-König641de9d2008-07-29 22:33:38 -07003009DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
3010
Christian Borntraeger5c828712009-10-23 14:58:11 +02003011int __printk_ratelimit(const char *func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012{
Christian Borntraeger5c828712009-10-23 14:58:11 +02003013 return ___ratelimit(&printk_ratelimit_state, func);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014}
Christian Borntraeger5c828712009-10-23 14:58:11 +02003015EXPORT_SYMBOL(__printk_ratelimit);
Andrew Mortonf46c4832006-11-02 22:07:16 -08003016
3017/**
3018 * printk_timed_ratelimit - caller-controlled printk ratelimiting
3019 * @caller_jiffies: pointer to caller's state
3020 * @interval_msecs: minimum interval between prints
3021 *
3022 * printk_timed_ratelimit() returns true if more than @interval_msecs
3023 * milliseconds have elapsed since the last time printk_timed_ratelimit()
3024 * returned true.
3025 */
3026bool printk_timed_ratelimit(unsigned long *caller_jiffies,
3027 unsigned int interval_msecs)
3028{
Alex Elder249771b2014-08-06 16:09:08 -07003029 unsigned long elapsed = jiffies - *caller_jiffies;
3030
3031 if (*caller_jiffies && elapsed <= msecs_to_jiffies(interval_msecs))
3032 return false;
3033
3034 *caller_jiffies = jiffies;
3035 return true;
Andrew Mortonf46c4832006-11-02 22:07:16 -08003036}
3037EXPORT_SYMBOL(printk_timed_ratelimit);
Simon Kagstrom456b5652009-10-16 14:09:18 +02003038
3039static DEFINE_SPINLOCK(dump_list_lock);
3040static LIST_HEAD(dump_list);
3041
3042/**
3043 * kmsg_dump_register - register a kernel log dumper.
Randy Dunlap64855362009-12-17 15:27:27 -08003044 * @dumper: pointer to the kmsg_dumper structure
Simon Kagstrom456b5652009-10-16 14:09:18 +02003045 *
3046 * Adds a kernel log dumper to the system. The dump callback in the
3047 * structure will be called when the kernel oopses or panics and must be
3048 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
3049 */
3050int kmsg_dump_register(struct kmsg_dumper *dumper)
3051{
3052 unsigned long flags;
3053 int err = -EBUSY;
3054
3055 /* The dump callback needs to be set */
3056 if (!dumper->dump)
3057 return -EINVAL;
3058
3059 spin_lock_irqsave(&dump_list_lock, flags);
3060 /* Don't allow registering multiple times */
3061 if (!dumper->registered) {
3062 dumper->registered = 1;
Huang Yingfb842b002011-01-12 16:59:43 -08003063 list_add_tail_rcu(&dumper->list, &dump_list);
Simon Kagstrom456b5652009-10-16 14:09:18 +02003064 err = 0;
3065 }
3066 spin_unlock_irqrestore(&dump_list_lock, flags);
3067
3068 return err;
3069}
3070EXPORT_SYMBOL_GPL(kmsg_dump_register);
3071
3072/**
3073 * kmsg_dump_unregister - unregister a kmsg dumper.
Randy Dunlap64855362009-12-17 15:27:27 -08003074 * @dumper: pointer to the kmsg_dumper structure
Simon Kagstrom456b5652009-10-16 14:09:18 +02003075 *
3076 * Removes a dump device from the system. Returns zero on success and
3077 * %-EINVAL otherwise.
3078 */
3079int kmsg_dump_unregister(struct kmsg_dumper *dumper)
3080{
3081 unsigned long flags;
3082 int err = -EINVAL;
3083
3084 spin_lock_irqsave(&dump_list_lock, flags);
3085 if (dumper->registered) {
3086 dumper->registered = 0;
Huang Yingfb842b002011-01-12 16:59:43 -08003087 list_del_rcu(&dumper->list);
Simon Kagstrom456b5652009-10-16 14:09:18 +02003088 err = 0;
3089 }
3090 spin_unlock_irqrestore(&dump_list_lock, flags);
Huang Yingfb842b002011-01-12 16:59:43 -08003091 synchronize_rcu();
Simon Kagstrom456b5652009-10-16 14:09:18 +02003092
3093 return err;
3094}
3095EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
3096
Kay Sievers7ff95542012-05-03 02:29:13 +02003097static bool always_kmsg_dump;
3098module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR);
3099
Simon Kagstrom456b5652009-10-16 14:09:18 +02003100/**
3101 * kmsg_dump - dump kernel log to kernel message dumpers.
3102 * @reason: the reason (oops, panic etc) for dumping
3103 *
Kay Sieverse2ae7152012-06-15 14:07:51 +02003104 * Call each of the registered dumper's dump() callback, which can
3105 * retrieve the kmsg records with kmsg_dump_get_line() or
3106 * kmsg_dump_get_buffer().
Simon Kagstrom456b5652009-10-16 14:09:18 +02003107 */
3108void kmsg_dump(enum kmsg_dump_reason reason)
3109{
Simon Kagstrom456b5652009-10-16 14:09:18 +02003110 struct kmsg_dumper *dumper;
Simon Kagstrom456b5652009-10-16 14:09:18 +02003111 unsigned long flags;
3112
Matthew Garrettc22ab3322012-03-05 14:59:10 -08003113 if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
3114 return;
3115
Huang Yingfb842b002011-01-12 16:59:43 -08003116 rcu_read_lock();
Kay Sieverse2ae7152012-06-15 14:07:51 +02003117 list_for_each_entry_rcu(dumper, &dump_list, list) {
3118 if (dumper->max_reason && reason > dumper->max_reason)
3119 continue;
3120
3121 /* initialize iterator with data about the stored records */
3122 dumper->active = true;
3123
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09003124 logbuf_lock_irqsave(flags);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003125 dumper->cur_seq = clear_seq;
3126 dumper->cur_idx = clear_idx;
3127 dumper->next_seq = log_next_seq;
3128 dumper->next_idx = log_next_idx;
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09003129 logbuf_unlock_irqrestore(flags);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003130
3131 /* invoke dumper which will iterate over records */
3132 dumper->dump(dumper, reason);
3133
3134 /* reset iterator */
3135 dumper->active = false;
3136 }
Huang Yingfb842b002011-01-12 16:59:43 -08003137 rcu_read_unlock();
Simon Kagstrom456b5652009-10-16 14:09:18 +02003138}
Kay Sieverse2ae7152012-06-15 14:07:51 +02003139
3140/**
Anton Vorontsov533827c2012-07-20 17:28:07 -07003141 * kmsg_dump_get_line_nolock - retrieve one kmsg log line (unlocked version)
3142 * @dumper: registered kmsg dumper
3143 * @syslog: include the "<4>" prefixes
3144 * @line: buffer to copy the line to
3145 * @size: maximum size of the buffer
3146 * @len: length of line placed into buffer
3147 *
3148 * Start at the beginning of the kmsg buffer, with the oldest kmsg
3149 * record, and copy one record into the provided buffer.
3150 *
3151 * Consecutive calls will return the next available record moving
3152 * towards the end of the buffer with the youngest messages.
3153 *
3154 * A return value of FALSE indicates that there are no more records to
3155 * read.
3156 *
3157 * The function is similar to kmsg_dump_get_line(), but grabs no locks.
3158 */
3159bool kmsg_dump_get_line_nolock(struct kmsg_dumper *dumper, bool syslog,
3160 char *line, size_t size, size_t *len)
3161{
Joe Perches62e32ac2013-07-31 13:53:47 -07003162 struct printk_log *msg;
Anton Vorontsov533827c2012-07-20 17:28:07 -07003163 size_t l = 0;
3164 bool ret = false;
3165
3166 if (!dumper->active)
3167 goto out;
3168
3169 if (dumper->cur_seq < log_first_seq) {
3170 /* messages are gone, move to first available one */
3171 dumper->cur_seq = log_first_seq;
3172 dumper->cur_idx = log_first_idx;
3173 }
3174
3175 /* last entry */
3176 if (dumper->cur_seq >= log_next_seq)
3177 goto out;
3178
3179 msg = log_from_idx(dumper->cur_idx);
Tetsuo Handae80c1a92018-12-04 19:00:01 +09003180 l = msg_print_text(msg, syslog, printk_time, line, size);
Anton Vorontsov533827c2012-07-20 17:28:07 -07003181
3182 dumper->cur_idx = log_next(dumper->cur_idx);
3183 dumper->cur_seq++;
3184 ret = true;
3185out:
3186 if (len)
3187 *len = l;
3188 return ret;
3189}
3190
3191/**
Kay Sieverse2ae7152012-06-15 14:07:51 +02003192 * kmsg_dump_get_line - retrieve one kmsg log line
3193 * @dumper: registered kmsg dumper
3194 * @syslog: include the "<4>" prefixes
3195 * @line: buffer to copy the line to
3196 * @size: maximum size of the buffer
3197 * @len: length of line placed into buffer
3198 *
3199 * Start at the beginning of the kmsg buffer, with the oldest kmsg
3200 * record, and copy one record into the provided buffer.
3201 *
3202 * Consecutive calls will return the next available record moving
3203 * towards the end of the buffer with the youngest messages.
3204 *
3205 * A return value of FALSE indicates that there are no more records to
3206 * read.
3207 */
3208bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog,
3209 char *line, size_t size, size_t *len)
3210{
3211 unsigned long flags;
Anton Vorontsov533827c2012-07-20 17:28:07 -07003212 bool ret;
Kay Sieverse2ae7152012-06-15 14:07:51 +02003213
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09003214 logbuf_lock_irqsave(flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07003215 ret = kmsg_dump_get_line_nolock(dumper, syslog, line, size, len);
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09003216 logbuf_unlock_irqrestore(flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07003217
Kay Sieverse2ae7152012-06-15 14:07:51 +02003218 return ret;
3219}
3220EXPORT_SYMBOL_GPL(kmsg_dump_get_line);
3221
3222/**
3223 * kmsg_dump_get_buffer - copy kmsg log lines
3224 * @dumper: registered kmsg dumper
3225 * @syslog: include the "<4>" prefixes
Randy Dunlap4f0f4af2012-06-30 15:37:24 -07003226 * @buf: buffer to copy the line to
Kay Sieverse2ae7152012-06-15 14:07:51 +02003227 * @size: maximum size of the buffer
3228 * @len: length of line placed into buffer
3229 *
3230 * Start at the end of the kmsg buffer and fill the provided buffer
3231 * with as many of the the *youngest* kmsg records that fit into it.
3232 * If the buffer is large enough, all available kmsg records will be
3233 * copied with a single call.
3234 *
3235 * Consecutive calls will fill the buffer with the next block of
3236 * available older records, not including the earlier retrieved ones.
3237 *
3238 * A return value of FALSE indicates that there are no more records to
3239 * read.
3240 */
3241bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
3242 char *buf, size_t size, size_t *len)
3243{
3244 unsigned long flags;
3245 u64 seq;
3246 u32 idx;
3247 u64 next_seq;
3248 u32 next_idx;
3249 size_t l = 0;
3250 bool ret = false;
Tetsuo Handae80c1a92018-12-04 19:00:01 +09003251 bool time = printk_time;
Kay Sieverse2ae7152012-06-15 14:07:51 +02003252
3253 if (!dumper->active)
3254 goto out;
3255
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09003256 logbuf_lock_irqsave(flags);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003257 if (dumper->cur_seq < log_first_seq) {
3258 /* messages are gone, move to first available one */
3259 dumper->cur_seq = log_first_seq;
3260 dumper->cur_idx = log_first_idx;
3261 }
3262
3263 /* last entry */
3264 if (dumper->cur_seq >= dumper->next_seq) {
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09003265 logbuf_unlock_irqrestore(flags);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003266 goto out;
3267 }
3268
3269 /* calculate length of entire buffer */
3270 seq = dumper->cur_seq;
3271 idx = dumper->cur_idx;
3272 while (seq < dumper->next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07003273 struct printk_log *msg = log_from_idx(idx);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003274
Tetsuo Handae80c1a92018-12-04 19:00:01 +09003275 l += msg_print_text(msg, true, time, NULL, 0);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003276 idx = log_next(idx);
3277 seq++;
3278 }
3279
3280 /* move first record forward until length fits into the buffer */
3281 seq = dumper->cur_seq;
3282 idx = dumper->cur_idx;
Vincent Whitchurchc9dccac2019-07-11 16:29:37 +02003283 while (l >= size && seq < dumper->next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07003284 struct printk_log *msg = log_from_idx(idx);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003285
Tetsuo Handae80c1a92018-12-04 19:00:01 +09003286 l -= msg_print_text(msg, true, time, NULL, 0);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003287 idx = log_next(idx);
3288 seq++;
3289 }
3290
3291 /* last message in next interation */
3292 next_seq = seq;
3293 next_idx = idx;
3294
3295 l = 0;
3296 while (seq < dumper->next_seq) {
Joe Perches62e32ac2013-07-31 13:53:47 -07003297 struct printk_log *msg = log_from_idx(idx);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003298
Tetsuo Handae80c1a92018-12-04 19:00:01 +09003299 l += msg_print_text(msg, syslog, time, buf + l, size - l);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003300 idx = log_next(idx);
3301 seq++;
3302 }
3303
3304 dumper->next_seq = next_seq;
3305 dumper->next_idx = next_idx;
3306 ret = true;
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09003307 logbuf_unlock_irqrestore(flags);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003308out:
3309 if (len)
3310 *len = l;
3311 return ret;
3312}
3313EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer);
3314
3315/**
Anton Vorontsov533827c2012-07-20 17:28:07 -07003316 * kmsg_dump_rewind_nolock - reset the interator (unlocked version)
3317 * @dumper: registered kmsg dumper
3318 *
3319 * Reset the dumper's iterator so that kmsg_dump_get_line() and
3320 * kmsg_dump_get_buffer() can be called again and used multiple
3321 * times within the same dumper.dump() callback.
3322 *
3323 * The function is similar to kmsg_dump_rewind(), but grabs no locks.
3324 */
3325void kmsg_dump_rewind_nolock(struct kmsg_dumper *dumper)
3326{
3327 dumper->cur_seq = clear_seq;
3328 dumper->cur_idx = clear_idx;
3329 dumper->next_seq = log_next_seq;
3330 dumper->next_idx = log_next_idx;
3331}
3332
3333/**
Kay Sieverse2ae7152012-06-15 14:07:51 +02003334 * kmsg_dump_rewind - reset the interator
3335 * @dumper: registered kmsg dumper
3336 *
3337 * Reset the dumper's iterator so that kmsg_dump_get_line() and
3338 * kmsg_dump_get_buffer() can be called again and used multiple
3339 * times within the same dumper.dump() callback.
3340 */
3341void kmsg_dump_rewind(struct kmsg_dumper *dumper)
3342{
3343 unsigned long flags;
3344
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09003345 logbuf_lock_irqsave(flags);
Anton Vorontsov533827c2012-07-20 17:28:07 -07003346 kmsg_dump_rewind_nolock(dumper);
Sergey Senozhatskyde6fcbd2016-12-27 23:16:11 +09003347 logbuf_unlock_irqrestore(flags);
Kay Sieverse2ae7152012-06-15 14:07:51 +02003348}
3349EXPORT_SYMBOL_GPL(kmsg_dump_rewind);
Tejun Heo196779b2013-04-30 15:27:12 -07003350
Joe Perches7ef3d2f2008-02-08 04:21:25 -08003351#endif