Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 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 Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 14 | * Fixed SMP synchronization, 08/08/99, Manfred Spraul |
Christian Kujau | 624dffc | 2006-01-15 02:43:54 +0100 | [diff] [blame] | 15 | * manfred@colorfullife.com |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | * Rewrote bits to get rid of console_lock |
Francois Cami | e1f8e87 | 2008-10-15 22:01:59 -0700 | [diff] [blame] | 17 | * 01Mar01 Andrew Morton |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
He Zhe | dd5adbf | 2018-09-30 00:45:52 +0800 | [diff] [blame] | 20 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/kernel.h> |
| 23 | #include <linux/mm.h> |
| 24 | #include <linux/tty.h> |
| 25 | #include <linux/tty_driver.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/console.h> |
| 27 | #include <linux/init.h> |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 28 | #include <linux/jiffies.h> |
| 29 | #include <linux/nmi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/module.h> |
Jan Engelhardt | 3b9c041 | 2006-06-25 05:48:15 -0700 | [diff] [blame] | 31 | #include <linux/moduleparam.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/delay.h> |
| 33 | #include <linux/smp.h> |
| 34 | #include <linux/security.h> |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 35 | #include <linux/memblock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/syscalls.h> |
Hari Bathini | 692f66f | 2017-05-08 15:56:18 -0700 | [diff] [blame] | 37 | #include <linux/crash_core.h> |
Jason Wessel | d37d39a | 2010-05-20 21:04:27 -0500 | [diff] [blame] | 38 | #include <linux/kdb.h> |
Ingo Molnar | 3fff4c4 | 2009-09-22 16:18:09 +0200 | [diff] [blame] | 39 | #include <linux/ratelimit.h> |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 40 | #include <linux/kmsg_dump.h> |
Kees Cook | 0023459 | 2010-02-03 15:36:43 -0800 | [diff] [blame] | 41 | #include <linux/syslog.h> |
Kevin Cernekee | 034260d | 2010-06-03 22:11:25 -0700 | [diff] [blame] | 42 | #include <linux/cpu.h> |
Huang Ying | fb842b00 | 2011-01-12 16:59:43 -0800 | [diff] [blame] | 43 | #include <linux/rculist.h> |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 44 | #include <linux/poll.h> |
Frederic Weisbecker | 74876a9 | 2012-10-12 18:00:23 +0200 | [diff] [blame] | 45 | #include <linux/irq_work.h> |
Alex Elder | 249771b | 2014-08-06 16:09:08 -0700 | [diff] [blame] | 46 | #include <linux/ctype.h> |
Christoph Hellwig | e2e40f2 | 2015-02-22 08:58:50 -0800 | [diff] [blame] | 47 | #include <linux/uio.h> |
Ingo Molnar | e601757 | 2017-02-01 16:36:40 +0100 | [diff] [blame] | 48 | #include <linux/sched/clock.h> |
Ingo Molnar | b17b015 | 2017-02-08 18:51:35 +0100 | [diff] [blame] | 49 | #include <linux/sched/debug.h> |
Ingo Molnar | 68db0cf | 2017-02-08 18:51:37 +0100 | [diff] [blame] | 50 | #include <linux/sched/task_stack.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 52 | #include <linux/uaccess.h> |
Christoph Hellwig | 40a7d9f | 2016-08-02 14:03:59 -0700 | [diff] [blame] | 53 | #include <asm/sections.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Abderrahmane Benbachir | 58eacff | 2018-03-22 20:33:28 -0400 | [diff] [blame] | 55 | #include <trace/events/initcall.h> |
Johannes Berg | 9510035 | 2011-11-24 20:03:08 +0100 | [diff] [blame] | 56 | #define CREATE_TRACE_POINTS |
| 57 | #include <trace/events/printk.h> |
| 58 | |
Joe Perches | d197c43 | 2013-07-31 13:53:44 -0700 | [diff] [blame] | 59 | #include "console_cmdline.h" |
Joe Perches | bbeddf5 | 2013-07-31 13:53:45 -0700 | [diff] [blame] | 60 | #include "braille.h" |
Petr Mladek | 42a0bb3 | 2016-05-20 17:00:33 -0700 | [diff] [blame] | 61 | #include "internal.h" |
Joe Perches | d197c43 | 2013-07-31 13:53:44 -0700 | [diff] [blame] | 62 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | int console_printk[4] = { |
Borislav Petkov | a8fe19e | 2014-06-04 16:11:46 -0700 | [diff] [blame] | 64 | CONSOLE_LOGLEVEL_DEFAULT, /* console_loglevel */ |
Alex Elder | 42a9dc0 | 2014-08-06 16:09:01 -0700 | [diff] [blame] | 65 | MESSAGE_LOGLEVEL_DEFAULT, /* default_message_loglevel */ |
Borislav Petkov | a8fe19e | 2014-06-04 16:11:46 -0700 | [diff] [blame] | 66 | CONSOLE_LOGLEVEL_MIN, /* minimum_console_loglevel */ |
| 67 | CONSOLE_LOGLEVEL_DEFAULT, /* default_console_loglevel */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | }; |
Prarit Bhargava | a193918 | 2019-02-08 19:24:49 +0100 | [diff] [blame] | 69 | EXPORT_SYMBOL_GPL(console_printk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Thomas Zimmermann | 56e6c10 | 2018-07-31 13:06:57 +0200 | [diff] [blame] | 71 | atomic_t ignore_console_lock_warning __read_mostly = ATOMIC_INIT(0); |
| 72 | EXPORT_SYMBOL(ignore_console_lock_warning); |
| 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | /* |
Patrick Pletscher | 0bbfb7c | 2007-02-17 20:10:16 +0100 | [diff] [blame] | 75 | * Low level drivers may need that to know if they can schedule in |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | * their unblank() callback or not. So let's export it. |
| 77 | */ |
| 78 | int oops_in_progress; |
| 79 | EXPORT_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 Gleixner | 5b8c4f2 | 2010-09-07 14:33:43 +0000 | [diff] [blame] | 86 | static DEFINE_SEMAPHORE(console_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | struct console *console_drivers; |
Ingo Molnar | a29d1cf | 2008-06-02 13:19:08 +0200 | [diff] [blame] | 88 | EXPORT_SYMBOL_GPL(console_drivers); |
| 89 | |
Feng Tang | c39ea0b | 2019-05-14 15:45:34 -0700 | [diff] [blame] | 90 | /* |
| 91 | * System may need to suppress printk message under certain |
| 92 | * circumstances, like after kernel panic happens. |
| 93 | */ |
| 94 | int __read_mostly suppress_printk; |
| 95 | |
Daniel Vetter | daee779 | 2012-09-22 19:52:11 +0200 | [diff] [blame] | 96 | #ifdef CONFIG_LOCKDEP |
| 97 | static struct lockdep_map console_lock_dep_map = { |
| 98 | .name = "console_lock" |
| 99 | }; |
| 100 | #endif |
| 101 | |
Borislav Petkov | 750afe7 | 2016-08-02 14:04:07 -0700 | [diff] [blame] | 102 | enum devkmsg_log_bits { |
| 103 | __DEVKMSG_LOG_BIT_ON = 0, |
| 104 | __DEVKMSG_LOG_BIT_OFF, |
| 105 | __DEVKMSG_LOG_BIT_LOCK, |
| 106 | }; |
| 107 | |
| 108 | enum 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 | |
| 117 | static unsigned int __read_mostly devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT; |
| 118 | |
| 119 | static int __control_devkmsg(char *str) |
| 120 | { |
Chuhong Yuan | 35c3549 | 2019-08-09 15:10:34 +0800 | [diff] [blame] | 121 | size_t len; |
| 122 | |
Borislav Petkov | 750afe7 | 2016-08-02 14:04:07 -0700 | [diff] [blame] | 123 | if (!str) |
| 124 | return -EINVAL; |
| 125 | |
Chuhong Yuan | 35c3549 | 2019-08-09 15:10:34 +0800 | [diff] [blame] | 126 | len = str_has_prefix(str, "on"); |
| 127 | if (len) { |
Borislav Petkov | 750afe7 | 2016-08-02 14:04:07 -0700 | [diff] [blame] | 128 | devkmsg_log = DEVKMSG_LOG_MASK_ON; |
Chuhong Yuan | 35c3549 | 2019-08-09 15:10:34 +0800 | [diff] [blame] | 129 | return len; |
Borislav Petkov | 750afe7 | 2016-08-02 14:04:07 -0700 | [diff] [blame] | 130 | } |
Chuhong Yuan | 35c3549 | 2019-08-09 15:10:34 +0800 | [diff] [blame] | 131 | |
| 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 Petkov | 750afe7 | 2016-08-02 14:04:07 -0700 | [diff] [blame] | 144 | return -EINVAL; |
| 145 | } |
| 146 | |
| 147 | static 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 Senozhatsky | 6fd78a1 | 2018-01-19 13:39:01 +0900 | [diff] [blame] | 155 | 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 Petkov | 750afe7 | 2016-08-02 14:04:07 -0700 | [diff] [blame] | 159 | /* 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 | |
| 173 | char devkmsg_log_str[DEVKMSG_STR_MAX_SIZE] = "ratelimit"; |
| 174 | |
| 175 | int 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 Senozhatsky | 9627808 | 2018-10-02 11:38:34 +0900 | [diff] [blame] | 214 | /* Number of registered extended console drivers. */ |
Tejun Heo | 6fe2935 | 2015-06-25 15:01:30 -0700 | [diff] [blame] | 215 | static int nr_ext_console_drivers; |
| 216 | |
| 217 | /* |
Jan Kara | bd8d7cf | 2014-06-04 16:11:36 -0700 | [diff] [blame] | 218 | * 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 | |
| 226 | static int __down_trylock_console_sem(unsigned long ip) |
| 227 | { |
Sergey Senozhatsky | f975237 | 2016-12-27 23:16:09 +0900 | [diff] [blame] | 228 | 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 Kara | bd8d7cf | 2014-06-04 16:11:36 -0700 | [diff] [blame] | 241 | 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 Senozhatsky | f975237 | 2016-12-27 23:16:09 +0900 | [diff] [blame] | 247 | static void __up_console_sem(unsigned long ip) |
| 248 | { |
| 249 | unsigned long flags; |
| 250 | |
Qian Cai | 5facae4 | 2019-09-19 12:09:40 -0400 | [diff] [blame] | 251 | mutex_release(&console_lock_dep_map, ip); |
Sergey Senozhatsky | f975237 | 2016-12-27 23:16:09 +0900 | [diff] [blame] | 252 | |
| 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 Kara | bd8d7cf | 2014-06-04 16:11:36 -0700 | [diff] [blame] | 258 | |
| 259 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | * 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 Elder | 0b90fec | 2014-08-06 16:09:03 -0700 | [diff] [blame] | 263 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | */ |
Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 267 | static int console_locked, console_suspended; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
| 269 | /* |
Feng Tang | fe3d8ad | 2011-03-22 16:34:21 -0700 | [diff] [blame] | 270 | * If exclusive_console is non-NULL then only this console is to be printed to. |
| 271 | */ |
| 272 | static struct console *exclusive_console; |
| 273 | |
| 274 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | * Array of consoles built from command line options (console=) |
| 276 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
| 278 | #define MAX_CMDLINECONSOLES 8 |
| 279 | |
| 280 | static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES]; |
Joe Perches | d197c43 | 2013-07-31 13:53:44 -0700 | [diff] [blame] | 281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | static int preferred_console = -1; |
Markus Armbruster | 9e124fe | 2008-05-26 23:31:07 +0100 | [diff] [blame] | 283 | int console_set_on_cmdline; |
| 284 | EXPORT_SYMBOL(console_set_on_cmdline); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
| 286 | /* Flag: console code may call schedule() */ |
| 287 | static int console_may_schedule; |
| 288 | |
Sergey Senozhatsky | cca10d5 | 2017-12-21 14:41:49 +0900 | [diff] [blame] | 289 | enum con_msg_format_flags { |
| 290 | MSG_FORMAT_DEFAULT = 0, |
| 291 | MSG_FORMAT_SYSLOG = (1 << 0), |
| 292 | }; |
| 293 | |
| 294 | static int console_msg_format = MSG_FORMAT_DEFAULT; |
| 295 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 296 | /* |
| 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 Elder | 0b90fec | 2014-08-06 16:09:03 -0700 | [diff] [blame] | 302 | * sequence numbers of these entries are maintained when messages are |
| 303 | * stored. |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 304 | * |
| 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 Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 319 | * 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 Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 348 | * The 'struct printk_log' buffer header must never be directly exported to |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 349 | * 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 Ospite | b389645 | 2015-06-30 14:59:03 -0700 | [diff] [blame] | 353 | * "<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 Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 357 | * |
| 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 Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 361 | */ |
Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 362 | |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 363 | enum log_flags { |
Kay Sievers | 5becfb1 | 2012-07-09 12:15:42 -0700 | [diff] [blame] | 364 | LOG_NEWLINE = 2, /* text ended with a newline */ |
Kay Sievers | 5becfb1 | 2012-07-09 12:15:42 -0700 | [diff] [blame] | 365 | LOG_CONT = 8, /* text is a fragment of a continuation line */ |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 366 | }; |
| 367 | |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 368 | struct printk_log { |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 369 | 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 Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 373 | u8 facility; /* syslog facility */ |
| 374 | u8 flags:5; /* internal record flags */ |
| 375 | u8 level:3; /* syslog level */ |
Tetsuo Handa | 15ff206 | 2018-12-18 06:05:04 +0900 | [diff] [blame] | 376 | #ifdef CONFIG_PRINTK_CALLER |
| 377 | u32 caller_id; /* thread id or processor id */ |
| 378 | #endif |
Andrey Ryabinin | 5c9cf8a | 2016-01-20 15:00:48 -0800 | [diff] [blame] | 379 | } |
| 380 | #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS |
| 381 | __packed __aligned(4) |
| 382 | #endif |
| 383 | ; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 384 | |
| 385 | /* |
Steven Rostedt | 458df9f | 2014-06-04 16:11:38 -0700 | [diff] [blame] | 386 | * 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 Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 389 | */ |
Petr Mladek | cf9b110 | 2016-05-20 17:00:42 -0700 | [diff] [blame] | 390 | DEFINE_RAW_SPINLOCK(logbuf_lock); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 391 | |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 392 | /* |
| 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 Sievers | 96efedf | 2012-07-16 18:35:29 -0700 | [diff] [blame] | 420 | #ifdef CONFIG_PRINTK |
Frederic Weisbecker | dc72c32 | 2013-03-22 15:04:39 -0700 | [diff] [blame] | 421 | DECLARE_WAIT_QUEUE_HEAD(log_wait); |
Kay Sievers | 7f3a781 | 2012-05-09 01:37:51 +0200 | [diff] [blame] | 422 | /* the next printk record to read by syslog(READ) or /proc/kmsg */ |
| 423 | static u64 syslog_seq; |
| 424 | static u32 syslog_idx; |
Kay Sievers | eb02dac | 2012-07-09 10:05:10 -0700 | [diff] [blame] | 425 | static size_t syslog_partial; |
Tetsuo Handa | e80c1a9 | 2018-12-04 19:00:01 +0900 | [diff] [blame] | 426 | static bool syslog_time; |
Kay Sievers | 7f3a781 | 2012-05-09 01:37:51 +0200 | [diff] [blame] | 427 | |
| 428 | /* index and sequence number of the first record stored in the buffer */ |
| 429 | static u64 log_first_seq; |
| 430 | static u32 log_first_idx; |
| 431 | |
| 432 | /* index and sequence number of the next record to store in the buffer */ |
| 433 | static u64 log_next_seq; |
Kay Sievers | 7f3a781 | 2012-05-09 01:37:51 +0200 | [diff] [blame] | 434 | static u32 log_next_idx; |
| 435 | |
Kay Sievers | eab0726 | 2012-07-16 18:35:30 -0700 | [diff] [blame] | 436 | /* the next printk record to write to the console */ |
| 437 | static u64 console_seq; |
| 438 | static u32 console_idx; |
Petr Mladek | f92b070 | 2018-09-13 14:34:06 +0200 | [diff] [blame] | 439 | static u64 exclusive_console_stop_seq; |
Kay Sievers | eab0726 | 2012-07-16 18:35:30 -0700 | [diff] [blame] | 440 | |
Kay Sievers | 7f3a781 | 2012-05-09 01:37:51 +0200 | [diff] [blame] | 441 | /* the next printk record to read after the last 'clear' command */ |
| 442 | static u64 clear_seq; |
| 443 | static u32 clear_idx; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 444 | |
Tetsuo Handa | 15ff206 | 2018-12-18 06:05:04 +0900 | [diff] [blame] | 445 | #ifdef CONFIG_PRINTK_CALLER |
| 446 | #define PREFIX_MAX 48 |
| 447 | #else |
Kay Sievers | 7049825 | 2012-07-16 18:35:29 -0700 | [diff] [blame] | 448 | #define PREFIX_MAX 32 |
Tetsuo Handa | 15ff206 | 2018-12-18 06:05:04 +0900 | [diff] [blame] | 449 | #endif |
Alex Elder | 249771b | 2014-08-06 16:09:08 -0700 | [diff] [blame] | 450 | #define LOG_LINE_MAX (1024 - PREFIX_MAX) |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 451 | |
Mathias Krause | 3824657 | 2015-11-06 16:30:38 -0800 | [diff] [blame] | 452 | #define LOG_LEVEL(v) ((v) & 0x07) |
| 453 | #define LOG_FACILITY(v) ((v) >> 3 & 0xff) |
| 454 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 455 | /* record buffer */ |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 456 | #define LOG_ALIGN __alignof__(struct printk_log) |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 457 | #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT) |
He Zhe | e6fe3e5 | 2018-09-30 00:45:53 +0800 | [diff] [blame] | 458 | #define LOG_BUF_LEN_MAX (u32)(1 << 31) |
Stephen Warren | f8450fc | 2012-05-10 16:14:33 -0600 | [diff] [blame] | 459 | static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN); |
Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 460 | static char *log_buf = __log_buf; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 461 | static u32 log_buf_len = __LOG_BUF_LEN; |
| 462 | |
Vasant Hegde | 14c4000 | 2014-08-09 11:15:30 +0530 | [diff] [blame] | 463 | /* Return log buffer address */ |
| 464 | char *log_buf_addr_get(void) |
| 465 | { |
| 466 | return log_buf; |
| 467 | } |
| 468 | |
| 469 | /* Return log buffer size */ |
| 470 | u32 log_buf_len_get(void) |
| 471 | { |
| 472 | return log_buf_len; |
| 473 | } |
| 474 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 475 | /* human readable text of the record */ |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 476 | static char *log_text(const struct printk_log *msg) |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 477 | { |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 478 | return (char *)msg + sizeof(struct printk_log); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /* optional key/value pair dictionary attached to the record */ |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 482 | static char *log_dict(const struct printk_log *msg) |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 483 | { |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 484 | return (char *)msg + sizeof(struct printk_log) + msg->text_len; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /* get record by index; idx must point to valid msg */ |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 488 | static struct printk_log *log_from_idx(u32 idx) |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 489 | { |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 490 | struct printk_log *msg = (struct printk_log *)(log_buf + idx); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 491 | |
| 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 Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 497 | return (struct printk_log *)log_buf; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 498 | return msg; |
| 499 | } |
| 500 | |
| 501 | /* get next record; idx must point to valid msg */ |
| 502 | static u32 log_next(u32 idx) |
| 503 | { |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 504 | struct printk_log *msg = (struct printk_log *)(log_buf + idx); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 505 | |
| 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 Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 513 | msg = (struct printk_log *)log_buf; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 514 | return msg->len; |
| 515 | } |
| 516 | return idx + msg->len; |
| 517 | } |
| 518 | |
Petr Mladek | f40e4b9 | 2014-06-04 16:11:30 -0700 | [diff] [blame] | 519 | /* |
| 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 | */ |
| 528 | static int logbuf_has_space(u32 msg_size, bool empty) |
Petr Mladek | 0a58169 | 2014-06-04 16:11:28 -0700 | [diff] [blame] | 529 | { |
| 530 | u32 free; |
| 531 | |
Petr Mladek | f40e4b9 | 2014-06-04 16:11:30 -0700 | [diff] [blame] | 532 | if (log_next_idx > log_first_idx || empty) |
Petr Mladek | 0a58169 | 2014-06-04 16:11:28 -0700 | [diff] [blame] | 533 | 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 Mladek | f40e4b9 | 2014-06-04 16:11:30 -0700 | [diff] [blame] | 544 | static int log_make_free_space(u32 msg_size) |
Petr Mladek | 0a58169 | 2014-06-04 16:11:28 -0700 | [diff] [blame] | 545 | { |
Ivan Delalande | f468908 | 2016-03-17 14:21:30 -0700 | [diff] [blame] | 546 | while (log_first_seq < log_next_seq && |
| 547 | !logbuf_has_space(msg_size, false)) { |
Alex Elder | 0b90fec | 2014-08-06 16:09:03 -0700 | [diff] [blame] | 548 | /* drop old messages until we have enough contiguous space */ |
Petr Mladek | 0a58169 | 2014-06-04 16:11:28 -0700 | [diff] [blame] | 549 | log_first_idx = log_next(log_first_idx); |
| 550 | log_first_seq++; |
| 551 | } |
Petr Mladek | f40e4b9 | 2014-06-04 16:11:30 -0700 | [diff] [blame] | 552 | |
Ivan Delalande | f468908 | 2016-03-17 14:21:30 -0700 | [diff] [blame] | 553 | if (clear_seq < log_first_seq) { |
| 554 | clear_seq = log_first_seq; |
| 555 | clear_idx = log_first_idx; |
| 556 | } |
| 557 | |
Petr Mladek | f40e4b9 | 2014-06-04 16:11:30 -0700 | [diff] [blame] | 558 | /* sequence numbers are equal, so the log buffer is empty */ |
Ivan Delalande | f468908 | 2016-03-17 14:21:30 -0700 | [diff] [blame] | 559 | if (logbuf_has_space(msg_size, log_first_seq == log_next_seq)) |
Petr Mladek | f40e4b9 | 2014-06-04 16:11:30 -0700 | [diff] [blame] | 560 | return 0; |
| 561 | |
| 562 | return -ENOMEM; |
Petr Mladek | 0a58169 | 2014-06-04 16:11:28 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Petr Mladek | 85c87043 | 2014-06-04 16:11:31 -0700 | [diff] [blame] | 565 | /* compute the message size including the padding bytes */ |
| 566 | static 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 Mladek | 55bd53a | 2014-06-04 16:11:32 -0700 | [diff] [blame] | 577 | /* |
| 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 |
| 583 | static const char trunc_msg[] = "<truncated>"; |
| 584 | |
| 585 | static 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 Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 603 | /* insert record into the buffer, discard old ones, update heads */ |
Tetsuo Handa | cbae05d | 2019-02-16 19:59:33 +0900 | [diff] [blame] | 604 | static int log_store(u32 caller_id, int facility, int level, |
Petr Mladek | 034633c | 2014-06-04 16:11:33 -0700 | [diff] [blame] | 605 | enum log_flags flags, u64 ts_nsec, |
| 606 | const char *dict, u16 dict_len, |
| 607 | const char *text, u16 text_len) |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 608 | { |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 609 | struct printk_log *msg; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 610 | u32 size, pad_len; |
Petr Mladek | 55bd53a | 2014-06-04 16:11:32 -0700 | [diff] [blame] | 611 | u16 trunc_msg_len = 0; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 612 | |
| 613 | /* number of '\0' padding bytes to next message */ |
Petr Mladek | 85c87043 | 2014-06-04 16:11:31 -0700 | [diff] [blame] | 614 | size = msg_used_size(text_len, dict_len, &pad_len); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 615 | |
Petr Mladek | 55bd53a | 2014-06-04 16:11:32 -0700 | [diff] [blame] | 616 | 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 Mladek | 034633c | 2014-06-04 16:11:33 -0700 | [diff] [blame] | 622 | return 0; |
Petr Mladek | 55bd53a | 2014-06-04 16:11:32 -0700 | [diff] [blame] | 623 | } |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 624 | |
Petr Mladek | 39b2510 | 2014-04-03 14:48:42 -0700 | [diff] [blame] | 625 | if (log_next_idx + size + sizeof(struct printk_log) > log_buf_len) { |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 626 | /* |
| 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 Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 631 | memset(log_buf + log_next_idx, 0, sizeof(struct printk_log)); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 632 | log_next_idx = 0; |
| 633 | } |
| 634 | |
| 635 | /* fill message */ |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 636 | msg = (struct printk_log *)(log_buf + log_next_idx); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 637 | memcpy(log_text(msg), text, text_len); |
| 638 | msg->text_len = text_len; |
Petr Mladek | 55bd53a | 2014-06-04 16:11:32 -0700 | [diff] [blame] | 639 | 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 Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 643 | memcpy(log_dict(msg), dict, dict_len); |
| 644 | msg->dict_len = dict_len; |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 645 | 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 Handa | 15ff206 | 2018-12-18 06:05:04 +0900 | [diff] [blame] | 652 | #ifdef CONFIG_PRINTK_CALLER |
Tetsuo Handa | cbae05d | 2019-02-16 19:59:33 +0900 | [diff] [blame] | 653 | msg->caller_id = caller_id; |
Tetsuo Handa | 15ff206 | 2018-12-18 06:05:04 +0900 | [diff] [blame] | 654 | #endif |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 655 | memset(log_dict(msg) + dict_len, 0, pad_len); |
Petr Mladek | fce6e03 | 2014-04-03 14:48:43 -0700 | [diff] [blame] | 656 | msg->len = size; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 657 | |
| 658 | /* insert message */ |
| 659 | log_next_idx += msg->len; |
| 660 | log_next_seq++; |
Petr Mladek | 034633c | 2014-06-04 16:11:33 -0700 | [diff] [blame] | 661 | |
| 662 | return msg->text_len; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 663 | } |
Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 664 | |
Alex Elder | e99aa46 | 2014-08-06 16:09:05 -0700 | [diff] [blame] | 665 | int dmesg_restrict = IS_ENABLED(CONFIG_SECURITY_DMESG_RESTRICT); |
Kees Cook | 637241a | 2013-06-12 14:04:39 -0700 | [diff] [blame] | 666 | |
| 667 | static 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 Cook | c71b02e | 2017-08-09 21:11:00 -0700 | [diff] [blame] | 679 | static int check_syslog_permissions(int type, int source) |
Kees Cook | 637241a | 2013-06-12 14:04:39 -0700 | [diff] [blame] | 680 | { |
| 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 Averin | 3ea4331 | 2015-06-25 15:01:47 -0700 | [diff] [blame] | 685 | if (source == SYSLOG_FROM_PROC && type != SYSLOG_ACTION_OPEN) |
Vasily Averin | d194e5d | 2015-06-25 15:01:44 -0700 | [diff] [blame] | 686 | goto ok; |
Kees Cook | 637241a | 2013-06-12 14:04:39 -0700 | [diff] [blame] | 687 | |
| 688 | if (syslog_action_restricted(type)) { |
| 689 | if (capable(CAP_SYSLOG)) |
Vasily Averin | d194e5d | 2015-06-25 15:01:44 -0700 | [diff] [blame] | 690 | goto ok; |
Kees Cook | 637241a | 2013-06-12 14:04:39 -0700 | [diff] [blame] | 691 | /* |
| 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 Averin | d194e5d | 2015-06-25 15:01:44 -0700 | [diff] [blame] | 700 | goto ok; |
Kees Cook | 637241a | 2013-06-12 14:04:39 -0700 | [diff] [blame] | 701 | } |
| 702 | return -EPERM; |
| 703 | } |
Vasily Averin | d194e5d | 2015-06-25 15:01:44 -0700 | [diff] [blame] | 704 | ok: |
Kees Cook | 637241a | 2013-06-12 14:04:39 -0700 | [diff] [blame] | 705 | return security_syslog(type); |
| 706 | } |
| 707 | |
Tejun Heo | d43ff43 | 2015-06-25 15:01:24 -0700 | [diff] [blame] | 708 | static void append_char(char **pp, char *e, char c) |
| 709 | { |
| 710 | if (*pp < e) |
| 711 | *(*pp)++ = c; |
| 712 | } |
Kees Cook | 637241a | 2013-06-12 14:04:39 -0700 | [diff] [blame] | 713 | |
Tejun Heo | 0a295e67 | 2015-06-25 15:01:27 -0700 | [diff] [blame] | 714 | static ssize_t msg_print_ext_header(char *buf, size_t size, |
Linus Torvalds | 5aa068e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 715 | struct printk_log *msg, u64 seq) |
Tejun Heo | 0a295e67 | 2015-06-25 15:01:27 -0700 | [diff] [blame] | 716 | { |
| 717 | u64 ts_usec = msg->ts_nsec; |
Tetsuo Handa | 15ff206 | 2018-12-18 06:05:04 +0900 | [diff] [blame] | 718 | 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 Heo | 0a295e67 | 2015-06-25 15:01:27 -0700 | [diff] [blame] | 727 | |
| 728 | do_div(ts_usec, 1000); |
| 729 | |
Tetsuo Handa | 15ff206 | 2018-12-18 06:05:04 +0900 | [diff] [blame] | 730 | 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 Heo | 0a295e67 | 2015-06-25 15:01:27 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | static 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 Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 783 | /* /dev/kmsg - userspace message inject/listen interface */ |
| 784 | struct devkmsg_user { |
| 785 | u64 seq; |
| 786 | u32 idx; |
Borislav Petkov | 750afe7 | 2016-08-02 14:04:07 -0700 | [diff] [blame] | 787 | struct ratelimit_state rs; |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 788 | struct mutex lock; |
Tejun Heo | d43ff43 | 2015-06-25 15:01:24 -0700 | [diff] [blame] | 789 | char buf[CONSOLE_EXT_LOG_MAX]; |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 790 | }; |
| 791 | |
Tetsuo Handa | 9adcfaf | 2018-11-24 13:10:25 +0900 | [diff] [blame] | 792 | static __printf(3, 4) __cold |
| 793 | int 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 Viro | 849f312 | 2014-08-23 12:23:53 -0400 | [diff] [blame] | 805 | static ssize_t devkmsg_write(struct kiocb *iocb, struct iov_iter *from) |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 806 | { |
| 807 | char *buf, *line; |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 808 | int level = default_message_loglevel; |
| 809 | int facility = 1; /* LOG_USER */ |
Borislav Petkov | 750afe7 | 2016-08-02 14:04:07 -0700 | [diff] [blame] | 810 | struct file *file = iocb->ki_filp; |
| 811 | struct devkmsg_user *user = file->private_data; |
Christoph Hellwig | 66ee59a | 2015-02-11 19:56:46 +0100 | [diff] [blame] | 812 | size_t len = iov_iter_count(from); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 813 | ssize_t ret = len; |
| 814 | |
Borislav Petkov | 750afe7 | 2016-08-02 14:04:07 -0700 | [diff] [blame] | 815 | if (!user || len > LOG_LINE_MAX) |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 816 | return -EINVAL; |
Borislav Petkov | 750afe7 | 2016-08-02 14:04:07 -0700 | [diff] [blame] | 817 | |
| 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 Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 828 | buf = kmalloc(len+1, GFP_KERNEL); |
| 829 | if (buf == NULL) |
| 830 | return -ENOMEM; |
| 831 | |
Al Viro | 849f312 | 2014-08-23 12:23:53 -0400 | [diff] [blame] | 832 | buf[len] = '\0'; |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 833 | if (!copy_from_iter_full(buf, len, from)) { |
Al Viro | 849f312 | 2014-08-23 12:23:53 -0400 | [diff] [blame] | 834 | kfree(buf); |
| 835 | return -EFAULT; |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 836 | } |
| 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 Krause | 3824657 | 2015-11-06 16:30:38 -0800 | [diff] [blame] | 850 | unsigned int u; |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 851 | |
Mathias Krause | 3824657 | 2015-11-06 16:30:38 -0800 | [diff] [blame] | 852 | u = simple_strtoul(line + 1, &endp, 10); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 853 | if (endp && endp[0] == '>') { |
Mathias Krause | 3824657 | 2015-11-06 16:30:38 -0800 | [diff] [blame] | 854 | level = LOG_LEVEL(u); |
| 855 | if (LOG_FACILITY(u) != 0) |
| 856 | facility = LOG_FACILITY(u); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 857 | endp++; |
| 858 | len -= endp - line; |
| 859 | line = endp; |
| 860 | } |
| 861 | } |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 862 | |
Tetsuo Handa | 9adcfaf | 2018-11-24 13:10:25 +0900 | [diff] [blame] | 863 | devkmsg_emit(facility, level, "%s", line); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 864 | kfree(buf); |
| 865 | return ret; |
| 866 | } |
| 867 | |
| 868 | static 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 Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 872 | struct printk_log *msg; |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 873 | size_t len; |
| 874 | ssize_t ret; |
| 875 | |
| 876 | if (!user) |
| 877 | return -EBADF; |
| 878 | |
Yuanhan Liu | 4a77a5a | 2012-06-16 21:21:51 +0800 | [diff] [blame] | 879 | ret = mutex_lock_interruptible(&user->lock); |
| 880 | if (ret) |
| 881 | return ret; |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 882 | |
| 883 | logbuf_lock_irq(); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 884 | while (user->seq == log_next_seq) { |
| 885 | if (file->f_flags & O_NONBLOCK) { |
| 886 | ret = -EAGAIN; |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 887 | logbuf_unlock_irq(); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 888 | goto out; |
| 889 | } |
| 890 | |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 891 | logbuf_unlock_irq(); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 892 | ret = wait_event_interruptible(log_wait, |
| 893 | user->seq != log_next_seq); |
| 894 | if (ret) |
| 895 | goto out; |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 896 | logbuf_lock_irq(); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 897 | } |
| 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 Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 904 | logbuf_unlock_irq(); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 905 | goto out; |
| 906 | } |
| 907 | |
| 908 | msg = log_from_idx(user->idx); |
Tejun Heo | 0a295e67 | 2015-06-25 15:01:27 -0700 | [diff] [blame] | 909 | len = msg_print_ext_header(user->buf, sizeof(user->buf), |
Linus Torvalds | 5aa068e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 910 | msg, user->seq); |
Tejun Heo | 0a295e67 | 2015-06-25 15:01:27 -0700 | [diff] [blame] | 911 | 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 Sievers | d39f3d7 | 2012-07-16 18:35:30 -0700 | [diff] [blame] | 914 | |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 915 | user->idx = log_next(user->idx); |
| 916 | user->seq++; |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 917 | logbuf_unlock_irq(); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 918 | |
| 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; |
| 929 | out: |
| 930 | mutex_unlock(&user->lock); |
| 931 | return ret; |
| 932 | } |
| 933 | |
| 934 | static 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 Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 944 | logbuf_lock_irq(); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 945 | 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 Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 968 | logbuf_unlock_irq(); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 969 | return ret; |
| 970 | } |
| 971 | |
Al Viro | 9dd9574 | 2017-07-03 00:42:43 -0400 | [diff] [blame] | 972 | static __poll_t devkmsg_poll(struct file *file, poll_table *wait) |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 973 | { |
| 974 | struct devkmsg_user *user = file->private_data; |
Al Viro | 9dd9574 | 2017-07-03 00:42:43 -0400 | [diff] [blame] | 975 | __poll_t ret = 0; |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 976 | |
| 977 | if (!user) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 978 | return EPOLLERR|EPOLLNVAL; |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 979 | |
| 980 | poll_wait(file, &log_wait, wait); |
| 981 | |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 982 | logbuf_lock_irq(); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 983 | if (user->seq < log_next_seq) { |
| 984 | /* return error when data has vanished underneath us */ |
| 985 | if (user->seq < log_first_seq) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 986 | ret = EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI; |
Nicolas Kaiser | 0a28531 | 2013-04-29 16:17:20 -0700 | [diff] [blame] | 987 | else |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 988 | ret = EPOLLIN|EPOLLRDNORM; |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 989 | } |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 990 | logbuf_unlock_irq(); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 991 | |
| 992 | return ret; |
| 993 | } |
| 994 | |
| 995 | static int devkmsg_open(struct inode *inode, struct file *file) |
| 996 | { |
| 997 | struct devkmsg_user *user; |
| 998 | int err; |
| 999 | |
Borislav Petkov | 750afe7 | 2016-08-02 14:04:07 -0700 | [diff] [blame] | 1000 | if (devkmsg_log & DEVKMSG_LOG_MASK_OFF) |
| 1001 | return -EPERM; |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 1002 | |
Borislav Petkov | 750afe7 | 2016-08-02 14:04:07 -0700 | [diff] [blame] | 1003 | /* 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 Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 1010 | |
| 1011 | user = kmalloc(sizeof(struct devkmsg_user), GFP_KERNEL); |
| 1012 | if (!user) |
| 1013 | return -ENOMEM; |
| 1014 | |
Borislav Petkov | 750afe7 | 2016-08-02 14:04:07 -0700 | [diff] [blame] | 1015 | ratelimit_default_init(&user->rs); |
| 1016 | ratelimit_set_flags(&user->rs, RATELIMIT_MSG_ON_RELEASE); |
| 1017 | |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 1018 | mutex_init(&user->lock); |
| 1019 | |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 1020 | logbuf_lock_irq(); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 1021 | user->idx = log_first_idx; |
| 1022 | user->seq = log_first_seq; |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 1023 | logbuf_unlock_irq(); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 1024 | |
| 1025 | file->private_data = user; |
| 1026 | return 0; |
| 1027 | } |
| 1028 | |
| 1029 | static 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 Petkov | 750afe7 | 2016-08-02 14:04:07 -0700 | [diff] [blame] | 1036 | ratelimit_state_exit(&user->rs); |
| 1037 | |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 1038 | mutex_destroy(&user->lock); |
| 1039 | kfree(user); |
| 1040 | return 0; |
| 1041 | } |
| 1042 | |
| 1043 | const struct file_operations kmsg_fops = { |
| 1044 | .open = devkmsg_open, |
| 1045 | .read = devkmsg_read, |
Al Viro | 849f312 | 2014-08-23 12:23:53 -0400 | [diff] [blame] | 1046 | .write_iter = devkmsg_write, |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 1047 | .llseek = devkmsg_llseek, |
| 1048 | .poll = devkmsg_poll, |
| 1049 | .release = devkmsg_release, |
| 1050 | }; |
| 1051 | |
Hari Bathini | 692f66f | 2017-05-08 15:56:18 -0700 | [diff] [blame] | 1052 | #ifdef CONFIG_CRASH_CORE |
Neil Horman | 04d491a | 2009-04-02 16:58:57 -0700 | [diff] [blame] | 1053 | /* |
Dirk Gouders | 4c1ace6 | 2013-11-12 15:08:54 -0800 | [diff] [blame] | 1054 | * This appends the listed symbols to /proc/vmcore |
Neil Horman | 04d491a | 2009-04-02 16:58:57 -0700 | [diff] [blame] | 1055 | * |
Dirk Gouders | 4c1ace6 | 2013-11-12 15:08:54 -0800 | [diff] [blame] | 1056 | * /proc/vmcore is used by various utilities, like crash and makedumpfile to |
Neil Horman | 04d491a | 2009-04-02 16:58:57 -0700 | [diff] [blame] | 1057 | * 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 Bathini | 692f66f | 2017-05-08 15:56:18 -0700 | [diff] [blame] | 1061 | void log_buf_vmcoreinfo_setup(void) |
Neil Horman | 04d491a | 2009-04-02 16:58:57 -0700 | [diff] [blame] | 1062 | { |
| 1063 | VMCOREINFO_SYMBOL(log_buf); |
Neil Horman | 04d491a | 2009-04-02 16:58:57 -0700 | [diff] [blame] | 1064 | VMCOREINFO_SYMBOL(log_buf_len); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1065 | VMCOREINFO_SYMBOL(log_first_idx); |
Ivan Delalande | f468908 | 2016-03-17 14:21:30 -0700 | [diff] [blame] | 1066 | VMCOREINFO_SYMBOL(clear_idx); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1067 | VMCOREINFO_SYMBOL(log_next_idx); |
Vivek Goyal | 6791457 | 2012-07-18 13:18:12 -0400 | [diff] [blame] | 1068 | /* |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 1069 | * Export struct printk_log size and field offsets. User space tools can |
Vivek Goyal | 6791457 | 2012-07-18 13:18:12 -0400 | [diff] [blame] | 1070 | * parse it and detect any changes to structure down the line. |
| 1071 | */ |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 1072 | 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 Handa | 15ff206 | 2018-12-18 06:05:04 +0900 | [diff] [blame] | 1077 | #ifdef CONFIG_PRINTK_CALLER |
| 1078 | VMCOREINFO_OFFSET(printk_log, caller_id); |
| 1079 | #endif |
Neil Horman | 04d491a | 2009-04-02 16:58:57 -0700 | [diff] [blame] | 1080 | } |
| 1081 | #endif |
| 1082 | |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 1083 | /* requested log_buf_len from kernel cmdline */ |
| 1084 | static unsigned long __initdata new_log_buf_len; |
| 1085 | |
Luis R. Rodriguez | c0a318a | 2014-08-06 16:08:52 -0700 | [diff] [blame] | 1086 | /* we practice scaling the ring buffer by powers of 2 */ |
He Zhe | e6fe3e5 | 2018-09-30 00:45:53 +0800 | [diff] [blame] | 1087 | static void __init log_buf_len_update(u64 size) |
Luis R. Rodriguez | c0a318a | 2014-08-06 16:08:52 -0700 | [diff] [blame] | 1088 | { |
He Zhe | e6fe3e5 | 2018-09-30 00:45:53 +0800 | [diff] [blame] | 1089 | 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. Rodriguez | c0a318a | 2014-08-06 16:08:52 -0700 | [diff] [blame] | 1094 | if (size) |
| 1095 | size = roundup_pow_of_two(size); |
| 1096 | if (size > log_buf_len) |
He Zhe | e6fe3e5 | 2018-09-30 00:45:53 +0800 | [diff] [blame] | 1097 | new_log_buf_len = (unsigned long)size; |
Luis R. Rodriguez | c0a318a | 2014-08-06 16:08:52 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 1100 | /* save requested log_buf_len since it's too early to process it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | static int __init log_buf_len_setup(char *str) |
| 1102 | { |
He Zhe | e6fe3e5 | 2018-09-30 00:45:53 +0800 | [diff] [blame] | 1103 | u64 size; |
He Zhe | 277fcdb | 2018-09-30 00:45:50 +0800 | [diff] [blame] | 1104 | |
| 1105 | if (!str) |
| 1106 | return -EINVAL; |
| 1107 | |
| 1108 | size = memparse(str, &str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | |
Luis R. Rodriguez | c0a318a | 2014-08-06 16:08:52 -0700 | [diff] [blame] | 1110 | log_buf_len_update(size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 1112 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | } |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 1114 | early_param("log_buf_len", log_buf_len_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | |
Geert Uytterhoeven | 2240a31 | 2014-10-13 15:51:11 -0700 | [diff] [blame] | 1116 | #ifdef CONFIG_SMP |
| 1117 | #define __LOG_CPU_MAX_BUF_LEN (1 << CONFIG_LOG_CPU_MAX_BUF_SHIFT) |
| 1118 | |
Luis R. Rodriguez | 23b2899 | 2014-08-06 16:08:56 -0700 | [diff] [blame] | 1119 | static 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 Uytterhoeven | 2240a31 | 2014-10-13 15:51:11 -0700 | [diff] [blame] | 1145 | #else /* !CONFIG_SMP */ |
| 1146 | static inline void log_buf_add_cpu(void) {} |
| 1147 | #endif /* CONFIG_SMP */ |
Luis R. Rodriguez | 23b2899 | 2014-08-06 16:08:56 -0700 | [diff] [blame] | 1148 | |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 1149 | void __init setup_log_buf(int early) |
| 1150 | { |
| 1151 | unsigned long flags; |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 1152 | char *new_log_buf; |
Sergey Senozhatsky | d2130e8 | 2018-10-10 20:33:08 +0900 | [diff] [blame] | 1153 | unsigned int free; |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 1154 | |
Luis R. Rodriguez | 23b2899 | 2014-08-06 16:08:56 -0700 | [diff] [blame] | 1155 | if (log_buf != __log_buf) |
| 1156 | return; |
| 1157 | |
| 1158 | if (!early && !new_log_buf_len) |
| 1159 | log_buf_add_cpu(); |
| 1160 | |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 1161 | if (!new_log_buf_len) |
| 1162 | return; |
| 1163 | |
Mike Rapoport | 26fb3da | 2019-03-11 23:30:42 -0700 | [diff] [blame] | 1164 | new_log_buf = memblock_alloc(new_log_buf_len, LOG_ALIGN); |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 1165 | if (unlikely(!new_log_buf)) { |
He Zhe | e6fe3e5 | 2018-09-30 00:45:53 +0800 | [diff] [blame] | 1166 | pr_err("log_buf_len: %lu bytes not available\n", |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 1167 | new_log_buf_len); |
| 1168 | return; |
| 1169 | } |
| 1170 | |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 1171 | logbuf_lock_irqsave(flags); |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 1172 | log_buf_len = new_log_buf_len; |
| 1173 | log_buf = new_log_buf; |
| 1174 | new_log_buf_len = 0; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1175 | free = __LOG_BUF_LEN - log_next_idx; |
| 1176 | memcpy(log_buf, __log_buf, __LOG_BUF_LEN); |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 1177 | logbuf_unlock_irqrestore(flags); |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 1178 | |
He Zhe | e6fe3e5 | 2018-09-30 00:45:53 +0800 | [diff] [blame] | 1179 | pr_info("log_buf_len: %u bytes\n", log_buf_len); |
| 1180 | pr_info("early log buf free: %u(%u%%)\n", |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 1181 | free, (free * 100) / __LOG_BUF_LEN); |
| 1182 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | |
Andrew Cooks | 2fa72c8 | 2012-12-17 15:59:56 -0800 | [diff] [blame] | 1184 | static bool __read_mostly ignore_loglevel; |
| 1185 | |
| 1186 | static int __init ignore_loglevel_setup(char *str) |
| 1187 | { |
Neil Zhang | d25d9fe | 2014-08-06 16:09:12 -0700 | [diff] [blame] | 1188 | ignore_loglevel = true; |
Andrew Morton | 27083ba | 2013-11-12 15:08:50 -0800 | [diff] [blame] | 1189 | pr_info("debug: ignoring loglevel setting.\n"); |
Andrew Cooks | 2fa72c8 | 2012-12-17 15:59:56 -0800 | [diff] [blame] | 1190 | |
| 1191 | return 0; |
| 1192 | } |
| 1193 | |
| 1194 | early_param("ignore_loglevel", ignore_loglevel_setup); |
| 1195 | module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR); |
Joe Perches | 205bd3d | 2015-02-12 15:01:34 -0800 | [diff] [blame] | 1196 | MODULE_PARM_DESC(ignore_loglevel, |
| 1197 | "ignore loglevel setting (prints all kernel messages to the console)"); |
Andrew Cooks | 2fa72c8 | 2012-12-17 15:59:56 -0800 | [diff] [blame] | 1198 | |
Sergey Senozhatsky | cf77544 | 2016-08-02 14:03:56 -0700 | [diff] [blame] | 1199 | static bool suppress_message_printing(int level) |
| 1200 | { |
| 1201 | return (level >= console_loglevel && !ignore_loglevel); |
| 1202 | } |
| 1203 | |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 1204 | #ifdef CONFIG_BOOT_PRINTK_DELAY |
| 1205 | |
Namhyung Kim | 674dff6 | 2010-10-26 14:22:48 -0700 | [diff] [blame] | 1206 | static int boot_delay; /* msecs delay after each printk during bootup */ |
Dave Young | 3a3b6ed | 2009-09-22 16:43:31 -0700 | [diff] [blame] | 1207 | static unsigned long long loops_per_msec; /* based on boot_delay */ |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 1208 | |
| 1209 | static int __init boot_delay_setup(char *str) |
| 1210 | { |
| 1211 | unsigned long lpj; |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 1212 | |
| 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 Young | 3a3b6ed | 2009-09-22 16:43:31 -0700 | [diff] [blame] | 1220 | 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 Young | 29e9d22 | 2013-11-12 15:08:53 -0800 | [diff] [blame] | 1223 | return 0; |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 1224 | } |
Dave Young | 29e9d22 | 2013-11-12 15:08:53 -0800 | [diff] [blame] | 1225 | early_param("boot_delay", boot_delay_setup); |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 1226 | |
Andrew Cooks | 2fa72c8 | 2012-12-17 15:59:56 -0800 | [diff] [blame] | 1227 | static void boot_delay_msec(int level) |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 1228 | { |
| 1229 | unsigned long long k; |
| 1230 | unsigned long timeout; |
| 1231 | |
Thomas Gleixner | ff48cd2 | 2017-05-16 20:42:45 +0200 | [diff] [blame] | 1232 | if ((boot_delay == 0 || system_state >= SYSTEM_RUNNING) |
Sergey Senozhatsky | cf77544 | 2016-08-02 14:03:56 -0700 | [diff] [blame] | 1233 | || suppress_message_printing(level)) { |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 1234 | return; |
Andrew Cooks | 2fa72c8 | 2012-12-17 15:59:56 -0800 | [diff] [blame] | 1235 | } |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 1236 | |
Dave Young | 3a3b6ed | 2009-09-22 16:43:31 -0700 | [diff] [blame] | 1237 | k = (unsigned long long)loops_per_msec * boot_delay; |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 1238 | |
| 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 Cooks | 2fa72c8 | 2012-12-17 15:59:56 -0800 | [diff] [blame] | 1254 | static inline void boot_delay_msec(int level) |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 1255 | { |
| 1256 | } |
| 1257 | #endif |
| 1258 | |
Alex Elder | e99aa46 | 2014-08-06 16:09:05 -0700 | [diff] [blame] | 1259 | static bool printk_time = IS_ENABLED(CONFIG_PRINTK_TIME); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1260 | module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR); |
| 1261 | |
Tetsuo Handa | 07c1773 | 2018-12-11 18:49:05 +0900 | [diff] [blame] | 1262 | static size_t print_syslog(unsigned int level, char *buf) |
| 1263 | { |
| 1264 | return sprintf(buf, "<%u>", level); |
| 1265 | } |
| 1266 | |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1267 | static size_t print_time(u64 ts, char *buf) |
| 1268 | { |
Tetsuo Handa | e80c1a9 | 2018-12-04 19:00:01 +0900 | [diff] [blame] | 1269 | unsigned long rem_nsec = do_div(ts, 1000000000); |
Roland Dreier | 35dac27 | 2013-01-04 15:35:50 -0800 | [diff] [blame] | 1270 | |
Tetsuo Handa | 15ff206 | 2018-12-18 06:05:04 +0900 | [diff] [blame] | 1271 | return sprintf(buf, "[%5lu.%06lu]", |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1272 | (unsigned long)ts, rem_nsec / 1000); |
| 1273 | } |
| 1274 | |
Tetsuo Handa | 15ff206 | 2018-12-18 06:05:04 +0900 | [diff] [blame] | 1275 | #ifdef CONFIG_PRINTK_CALLER |
| 1276 | static 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 Handa | e80c1a9 | 2018-12-04 19:00:01 +0900 | [diff] [blame] | 1288 | static size_t print_prefix(const struct printk_log *msg, bool syslog, |
| 1289 | bool time, char *buf) |
Kay Sievers | 649e6ee | 2012-05-10 04:30:45 +0200 | [diff] [blame] | 1290 | { |
Kay Sievers | 3ce9a7c | 2012-05-13 23:30:46 +0200 | [diff] [blame] | 1291 | size_t len = 0; |
Kay Sievers | 649e6ee | 2012-05-10 04:30:45 +0200 | [diff] [blame] | 1292 | |
Tetsuo Handa | 07c1773 | 2018-12-11 18:49:05 +0900 | [diff] [blame] | 1293 | if (syslog) |
| 1294 | len = print_syslog((msg->facility << 3) | msg->level, buf); |
Tetsuo Handa | 15ff206 | 2018-12-18 06:05:04 +0900 | [diff] [blame] | 1295 | |
Tetsuo Handa | e80c1a9 | 2018-12-04 19:00:01 +0900 | [diff] [blame] | 1296 | if (time) |
Tetsuo Handa | 07c1773 | 2018-12-11 18:49:05 +0900 | [diff] [blame] | 1297 | len += print_time(msg->ts_nsec, buf + len); |
Tetsuo Handa | 15ff206 | 2018-12-18 06:05:04 +0900 | [diff] [blame] | 1298 | |
| 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 Sievers | 3ce9a7c | 2012-05-13 23:30:46 +0200 | [diff] [blame] | 1306 | return len; |
| 1307 | } |
| 1308 | |
Tetsuo Handa | e80c1a9 | 2018-12-04 19:00:01 +0900 | [diff] [blame] | 1309 | static size_t msg_print_text(const struct printk_log *msg, bool syslog, |
| 1310 | bool time, char *buf, size_t size) |
Kay Sievers | 3ce9a7c | 2012-05-13 23:30:46 +0200 | [diff] [blame] | 1311 | { |
| 1312 | const char *text = log_text(msg); |
| 1313 | size_t text_size = msg->text_len; |
| 1314 | size_t len = 0; |
Tetsuo Handa | 07c1773 | 2018-12-11 18:49:05 +0900 | [diff] [blame] | 1315 | char prefix[PREFIX_MAX]; |
| 1316 | const size_t prefix_len = print_prefix(msg, syslog, time, prefix); |
Kay Sievers | 3ce9a7c | 2012-05-13 23:30:46 +0200 | [diff] [blame] | 1317 | |
| 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 Handa | 07c1773 | 2018-12-11 18:49:05 +0900 | [diff] [blame] | 1331 | if (prefix_len + text_len + 1 >= size - len) |
Kay Sievers | 3ce9a7c | 2012-05-13 23:30:46 +0200 | [diff] [blame] | 1332 | break; |
| 1333 | |
Tetsuo Handa | 07c1773 | 2018-12-11 18:49:05 +0900 | [diff] [blame] | 1334 | memcpy(buf + len, prefix, prefix_len); |
| 1335 | len += prefix_len; |
Kay Sievers | 3ce9a7c | 2012-05-13 23:30:46 +0200 | [diff] [blame] | 1336 | memcpy(buf + len, text, text_len); |
| 1337 | len += text_len; |
Linus Torvalds | 5aa068e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 1338 | buf[len++] = '\n'; |
Kay Sievers | 3ce9a7c | 2012-05-13 23:30:46 +0200 | [diff] [blame] | 1339 | } else { |
| 1340 | /* SYSLOG_ACTION_* buffer size only calculation */ |
Tetsuo Handa | 07c1773 | 2018-12-11 18:49:05 +0900 | [diff] [blame] | 1341 | len += prefix_len + text_len + 1; |
Kay Sievers | 3ce9a7c | 2012-05-13 23:30:46 +0200 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | text = next; |
| 1345 | } while (text); |
| 1346 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1347 | return len; |
| 1348 | } |
| 1349 | |
| 1350 | static int syslog_print(char __user *buf, int size) |
| 1351 | { |
| 1352 | char *text; |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 1353 | struct printk_log *msg; |
Jan Beulich | 116e90b | 2012-06-22 16:36:09 +0100 | [diff] [blame] | 1354 | int len = 0; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1355 | |
Kay Sievers | 7049825 | 2012-07-16 18:35:29 -0700 | [diff] [blame] | 1356 | text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1357 | if (!text) |
| 1358 | return -ENOMEM; |
| 1359 | |
Jan Beulich | 116e90b | 2012-06-22 16:36:09 +0100 | [diff] [blame] | 1360 | while (size > 0) { |
| 1361 | size_t n; |
Kay Sievers | eb02dac | 2012-07-09 10:05:10 -0700 | [diff] [blame] | 1362 | size_t skip; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1363 | |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 1364 | logbuf_lock_irq(); |
Jan Beulich | 116e90b | 2012-06-22 16:36:09 +0100 | [diff] [blame] | 1365 | 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 Sievers | eb02dac | 2012-07-09 10:05:10 -0700 | [diff] [blame] | 1369 | syslog_partial = 0; |
Jan Beulich | 116e90b | 2012-06-22 16:36:09 +0100 | [diff] [blame] | 1370 | } |
| 1371 | if (syslog_seq == log_next_seq) { |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 1372 | logbuf_unlock_irq(); |
Jan Beulich | 116e90b | 2012-06-22 16:36:09 +0100 | [diff] [blame] | 1373 | break; |
| 1374 | } |
Kay Sievers | eb02dac | 2012-07-09 10:05:10 -0700 | [diff] [blame] | 1375 | |
Tetsuo Handa | e80c1a9 | 2018-12-04 19:00:01 +0900 | [diff] [blame] | 1376 | /* |
| 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 Sievers | eb02dac | 2012-07-09 10:05:10 -0700 | [diff] [blame] | 1383 | skip = syslog_partial; |
Jan Beulich | 116e90b | 2012-06-22 16:36:09 +0100 | [diff] [blame] | 1384 | msg = log_from_idx(syslog_idx); |
Tetsuo Handa | e80c1a9 | 2018-12-04 19:00:01 +0900 | [diff] [blame] | 1385 | n = msg_print_text(msg, true, syslog_time, text, |
| 1386 | LOG_LINE_MAX + PREFIX_MAX); |
Kay Sievers | eb02dac | 2012-07-09 10:05:10 -0700 | [diff] [blame] | 1387 | if (n - syslog_partial <= size) { |
| 1388 | /* message fits into buffer, move forward */ |
Jan Beulich | 116e90b | 2012-06-22 16:36:09 +0100 | [diff] [blame] | 1389 | syslog_idx = log_next(syslog_idx); |
| 1390 | syslog_seq++; |
Kay Sievers | eb02dac | 2012-07-09 10:05:10 -0700 | [diff] [blame] | 1391 | n -= syslog_partial; |
| 1392 | syslog_partial = 0; |
| 1393 | } else if (!len){ |
| 1394 | /* partial read(), remember position */ |
| 1395 | n = size; |
| 1396 | syslog_partial += n; |
Jan Beulich | 116e90b | 2012-06-22 16:36:09 +0100 | [diff] [blame] | 1397 | } else |
| 1398 | n = 0; |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 1399 | logbuf_unlock_irq(); |
Jan Beulich | 116e90b | 2012-06-22 16:36:09 +0100 | [diff] [blame] | 1400 | |
| 1401 | if (!n) |
| 1402 | break; |
| 1403 | |
Kay Sievers | eb02dac | 2012-07-09 10:05:10 -0700 | [diff] [blame] | 1404 | if (copy_to_user(buf, text + skip, n)) { |
Jan Beulich | 116e90b | 2012-06-22 16:36:09 +0100 | [diff] [blame] | 1405 | if (!len) |
| 1406 | len = -EFAULT; |
| 1407 | break; |
| 1408 | } |
Kay Sievers | eb02dac | 2012-07-09 10:05:10 -0700 | [diff] [blame] | 1409 | |
| 1410 | len += n; |
| 1411 | size -= n; |
| 1412 | buf += n; |
Jan Beulich | 116e90b | 2012-06-22 16:36:09 +0100 | [diff] [blame] | 1413 | } |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1414 | |
| 1415 | kfree(text); |
| 1416 | return len; |
| 1417 | } |
| 1418 | |
| 1419 | static int syslog_print_all(char __user *buf, int size, bool clear) |
| 1420 | { |
| 1421 | char *text; |
| 1422 | int len = 0; |
Namit Gupta | 63842c2 | 2018-06-20 19:26:19 +0530 | [diff] [blame] | 1423 | u64 next_seq; |
| 1424 | u64 seq; |
| 1425 | u32 idx; |
Tetsuo Handa | e80c1a9 | 2018-12-04 19:00:01 +0900 | [diff] [blame] | 1426 | bool time; |
Namit Gupta | 63842c2 | 2018-06-20 19:26:19 +0530 | [diff] [blame] | 1427 | |
Kay Sievers | 7049825 | 2012-07-16 18:35:29 -0700 | [diff] [blame] | 1428 | text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1429 | if (!text) |
| 1430 | return -ENOMEM; |
| 1431 | |
Tetsuo Handa | e80c1a9 | 2018-12-04 19:00:01 +0900 | [diff] [blame] | 1432 | time = printk_time; |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 1433 | logbuf_lock_irq(); |
Namit Gupta | 63842c2 | 2018-06-20 19:26:19 +0530 | [diff] [blame] | 1434 | /* |
| 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 Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1442 | |
Tetsuo Handa | e80c1a9 | 2018-12-04 19:00:01 +0900 | [diff] [blame] | 1443 | len += msg_print_text(msg, true, time, NULL, 0); |
Namit Gupta | 63842c2 | 2018-06-20 19:26:19 +0530 | [diff] [blame] | 1444 | idx = log_next(idx); |
| 1445 | seq++; |
| 1446 | } |
Kay Sievers | 3ce9a7c | 2012-05-13 23:30:46 +0200 | [diff] [blame] | 1447 | |
Namit Gupta | 63842c2 | 2018-06-20 19:26:19 +0530 | [diff] [blame] | 1448 | /* 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 Handa | e80c1a9 | 2018-12-04 19:00:01 +0900 | [diff] [blame] | 1454 | len -= msg_print_text(msg, true, time, NULL, 0); |
Namit Gupta | 63842c2 | 2018-06-20 19:26:19 +0530 | [diff] [blame] | 1455 | 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 Handa | e80c1a9 | 2018-12-04 19:00:01 +0900 | [diff] [blame] | 1465 | int textlen = msg_print_text(msg, true, time, text, |
| 1466 | LOG_LINE_MAX + PREFIX_MAX); |
Namit Gupta | 63842c2 | 2018-06-20 19:26:19 +0530 | [diff] [blame] | 1467 | |
Namit Gupta | 63842c2 | 2018-06-20 19:26:19 +0530 | [diff] [blame] | 1468 | idx = log_next(idx); |
| 1469 | seq++; |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 1470 | |
Namit Gupta | 63842c2 | 2018-06-20 19:26:19 +0530 | [diff] [blame] | 1471 | 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 Sievers | 3ce9a7c | 2012-05-13 23:30:46 +0200 | [diff] [blame] | 1477 | |
Namit Gupta | 63842c2 | 2018-06-20 19:26:19 +0530 | [diff] [blame] | 1478 | if (seq < log_first_seq) { |
| 1479 | /* messages are gone, move to next one */ |
| 1480 | seq = log_first_seq; |
| 1481 | idx = log_first_idx; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1482 | } |
| 1483 | } |
| 1484 | |
| 1485 | if (clear) { |
| 1486 | clear_seq = log_next_seq; |
| 1487 | clear_idx = log_next_idx; |
| 1488 | } |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 1489 | logbuf_unlock_irq(); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1490 | |
| 1491 | kfree(text); |
| 1492 | return len; |
| 1493 | } |
| 1494 | |
Petr Mladek | 8599dc7 | 2018-06-27 17:06:41 +0200 | [diff] [blame] | 1495 | static 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 Averin | 3ea4331 | 2015-06-25 15:01:47 -0700 | [diff] [blame] | 1503 | int do_syslog(int type, char __user *buf, int len, int source) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | { |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1505 | bool clear = false; |
Joe Perches | a39d4a8 | 2014-12-10 15:50:15 -0800 | [diff] [blame] | 1506 | static int saved_console_loglevel = LOGLEVEL_DEFAULT; |
Linus Torvalds | ee24aeb | 2011-02-10 17:53:55 -0800 | [diff] [blame] | 1507 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | |
Vasily Averin | 3ea4331 | 2015-06-25 15:01:47 -0700 | [diff] [blame] | 1509 | error = check_syslog_permissions(type, source); |
Linus Torvalds | ee24aeb | 2011-02-10 17:53:55 -0800 | [diff] [blame] | 1510 | if (error) |
Nikitas Angelinas | 077a1cc | 2017-07-29 20:36:36 -0700 | [diff] [blame] | 1511 | return error; |
Eric Paris | 12b3052 | 2010-11-15 18:36:29 -0500 | [diff] [blame] | 1512 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | switch (type) { |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 1514 | case SYSLOG_ACTION_CLOSE: /* Close log */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 1516 | case SYSLOG_ACTION_OPEN: /* Open log */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 1518 | case SYSLOG_ACTION_READ: /* Read from log */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | if (!buf || len < 0) |
Nikitas Angelinas | 077a1cc | 2017-07-29 20:36:36 -0700 | [diff] [blame] | 1520 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | if (!len) |
Nikitas Angelinas | 077a1cc | 2017-07-29 20:36:36 -0700 | [diff] [blame] | 1522 | return 0; |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 1523 | if (!access_ok(buf, len)) |
Nikitas Angelinas | 077a1cc | 2017-07-29 20:36:36 -0700 | [diff] [blame] | 1524 | return -EFAULT; |
Yuanhan Liu | 4a77a5a | 2012-06-16 21:21:51 +0800 | [diff] [blame] | 1525 | error = wait_event_interruptible(log_wait, |
| 1526 | syslog_seq != log_next_seq); |
Kay Sievers | cb424ff | 2012-07-06 09:50:09 -0700 | [diff] [blame] | 1527 | if (error) |
Nikitas Angelinas | 077a1cc | 2017-07-29 20:36:36 -0700 | [diff] [blame] | 1528 | return error; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1529 | error = syslog_print(buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 1531 | /* Read/clear last kernel messages */ |
| 1532 | case SYSLOG_ACTION_READ_CLEAR: |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1533 | clear = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | /* FALL THRU */ |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 1535 | /* Read last kernel messages */ |
| 1536 | case SYSLOG_ACTION_READ_ALL: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | if (!buf || len < 0) |
Nikitas Angelinas | 077a1cc | 2017-07-29 20:36:36 -0700 | [diff] [blame] | 1538 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | if (!len) |
Nikitas Angelinas | 077a1cc | 2017-07-29 20:36:36 -0700 | [diff] [blame] | 1540 | return 0; |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 1541 | if (!access_ok(buf, len)) |
Nikitas Angelinas | 077a1cc | 2017-07-29 20:36:36 -0700 | [diff] [blame] | 1542 | return -EFAULT; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1543 | error = syslog_print_all(buf, len, clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 1545 | /* Clear ring buffer */ |
| 1546 | case SYSLOG_ACTION_CLEAR: |
Petr Mladek | 8599dc7 | 2018-06-27 17:06:41 +0200 | [diff] [blame] | 1547 | syslog_clear(); |
Alan Stern | 4661e35 | 2012-06-22 17:12:19 -0400 | [diff] [blame] | 1548 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 1549 | /* Disable logging to console */ |
| 1550 | case SYSLOG_ACTION_CONSOLE_OFF: |
Joe Perches | a39d4a8 | 2014-12-10 15:50:15 -0800 | [diff] [blame] | 1551 | if (saved_console_loglevel == LOGLEVEL_DEFAULT) |
Frans Pop | 1aaad49 | 2009-07-06 13:31:48 +0200 | [diff] [blame] | 1552 | saved_console_loglevel = console_loglevel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | console_loglevel = minimum_console_loglevel; |
| 1554 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 1555 | /* Enable logging to console */ |
| 1556 | case SYSLOG_ACTION_CONSOLE_ON: |
Joe Perches | a39d4a8 | 2014-12-10 15:50:15 -0800 | [diff] [blame] | 1557 | if (saved_console_loglevel != LOGLEVEL_DEFAULT) { |
Frans Pop | 1aaad49 | 2009-07-06 13:31:48 +0200 | [diff] [blame] | 1558 | console_loglevel = saved_console_loglevel; |
Joe Perches | a39d4a8 | 2014-12-10 15:50:15 -0800 | [diff] [blame] | 1559 | saved_console_loglevel = LOGLEVEL_DEFAULT; |
Frans Pop | 1aaad49 | 2009-07-06 13:31:48 +0200 | [diff] [blame] | 1560 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 1562 | /* Set level of messages printed to console */ |
| 1563 | case SYSLOG_ACTION_CONSOLE_LEVEL: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | if (len < 1 || len > 8) |
Nikitas Angelinas | 077a1cc | 2017-07-29 20:36:36 -0700 | [diff] [blame] | 1565 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | if (len < minimum_console_loglevel) |
| 1567 | len = minimum_console_loglevel; |
| 1568 | console_loglevel = len; |
Frans Pop | 1aaad49 | 2009-07-06 13:31:48 +0200 | [diff] [blame] | 1569 | /* Implicitly re-enable logging to console */ |
Joe Perches | a39d4a8 | 2014-12-10 15:50:15 -0800 | [diff] [blame] | 1570 | saved_console_loglevel = LOGLEVEL_DEFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 1572 | /* Number of chars in the log buffer */ |
| 1573 | case SYSLOG_ACTION_SIZE_UNREAD: |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 1574 | logbuf_lock_irq(); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1575 | 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 Sievers | eb02dac | 2012-07-09 10:05:10 -0700 | [diff] [blame] | 1579 | syslog_partial = 0; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1580 | } |
Vasily Averin | 3ea4331 | 2015-06-25 15:01:47 -0700 | [diff] [blame] | 1581 | if (source == SYSLOG_FROM_PROC) { |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1582 | /* |
| 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 Elder | e97e126 | 2014-08-06 16:08:59 -0700 | [diff] [blame] | 1587 | error = log_next_seq - syslog_seq; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1588 | } else { |
Kay Sievers | 5becfb1 | 2012-07-09 12:15:42 -0700 | [diff] [blame] | 1589 | u64 seq = syslog_seq; |
| 1590 | u32 idx = syslog_idx; |
Tetsuo Handa | e80c1a9 | 2018-12-04 19:00:01 +0900 | [diff] [blame] | 1591 | bool time = syslog_partial ? syslog_time : printk_time; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1592 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1593 | while (seq < log_next_seq) { |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 1594 | struct printk_log *msg = log_from_idx(idx); |
Kay Sievers | 3ce9a7c | 2012-05-13 23:30:46 +0200 | [diff] [blame] | 1595 | |
Tetsuo Handa | e80c1a9 | 2018-12-04 19:00:01 +0900 | [diff] [blame] | 1596 | error += msg_print_text(msg, true, time, NULL, |
| 1597 | 0); |
| 1598 | time = printk_time; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1599 | idx = log_next(idx); |
| 1600 | seq++; |
| 1601 | } |
Kay Sievers | eb02dac | 2012-07-09 10:05:10 -0700 | [diff] [blame] | 1602 | error -= syslog_partial; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1603 | } |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 1604 | logbuf_unlock_irq(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 1606 | /* Size of the log buffer */ |
| 1607 | case SYSLOG_ACTION_SIZE_BUFFER: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | error = log_buf_len; |
| 1609 | break; |
| 1610 | default: |
| 1611 | error = -EINVAL; |
| 1612 | break; |
| 1613 | } |
Nikitas Angelinas | 077a1cc | 2017-07-29 20:36:36 -0700 | [diff] [blame] | 1614 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | return error; |
| 1616 | } |
| 1617 | |
Heiko Carstens | 1e7bfb2 | 2009-01-14 14:14:29 +0100 | [diff] [blame] | 1618 | SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | { |
Kees Cook | 637241a | 2013-06-12 14:04:39 -0700 | [diff] [blame] | 1620 | return do_syslog(type, buf, len, SYSLOG_FROM_READER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | } |
| 1622 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | /* |
Petr Mladek | c162d5b | 2018-01-12 17:08:37 +0100 | [diff] [blame] | 1624 | * 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 |
| 1629 | static struct lockdep_map console_owner_dep_map = { |
| 1630 | .name = "console_owner" |
| 1631 | }; |
| 1632 | #endif |
| 1633 | |
| 1634 | static DEFINE_RAW_SPINLOCK(console_owner_lock); |
| 1635 | static struct task_struct *console_owner; |
| 1636 | static 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 | */ |
| 1647 | static 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 | */ |
| 1672 | static 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 Cai | 5facae4 | 2019-09-19 12:09:40 -0400 | [diff] [blame] | 1682 | spin_release(&console_owner_dep_map, _THIS_IP_); |
Petr Mladek | c162d5b | 2018-01-12 17:08:37 +0100 | [diff] [blame] | 1683 | return 0; |
| 1684 | } |
| 1685 | |
| 1686 | /* The waiter is now free to continue */ |
| 1687 | WRITE_ONCE(console_waiter, false); |
| 1688 | |
Qian Cai | 5facae4 | 2019-09-19 12:09:40 -0400 | [diff] [blame] | 1689 | spin_release(&console_owner_dep_map, _THIS_IP_); |
Petr Mladek | c162d5b | 2018-01-12 17:08:37 +0100 | [diff] [blame] | 1690 | |
| 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 Cai | 5facae4 | 2019-09-19 12:09:40 -0400 | [diff] [blame] | 1695 | mutex_release(&console_lock_dep_map, _THIS_IP_); |
Petr Mladek | c162d5b | 2018-01-12 17:08:37 +0100 | [diff] [blame] | 1696 | 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 | */ |
| 1709 | static 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 Cai | 5facae4 | 2019-09-19 12:09:40 -0400 | [diff] [blame] | 1749 | spin_release(&console_owner_dep_map, _THIS_IP_); |
Petr Mladek | c162d5b | 2018-01-12 17:08:37 +0100 | [diff] [blame] | 1750 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | * Call the console drivers, asking them to write out |
| 1765 | * log_buf[start] to log_buf[end - 1]. |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1766 | * The console_lock must be held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | */ |
Sergey Senozhatsky | d9c2352 | 2016-12-24 23:09:01 +0900 | [diff] [blame] | 1768 | static void call_console_drivers(const char *ext_text, size_t ext_len, |
Tejun Heo | 6fe2935 | 2015-06-25 15:01:30 -0700 | [diff] [blame] | 1769 | const char *text, size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | { |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1771 | struct console *con; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | |
Sergey Senozhatsky | fc98c3c | 2017-02-18 03:42:54 -0800 | [diff] [blame] | 1773 | trace_console_rcuidle(text, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1775 | 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 Heo | 6fe2935 | 2015-06-25 15:01:30 -0700 | [diff] [blame] | 1785 | if (con->flags & CON_EXTENDED) |
| 1786 | con->write(con, ext_text, ext_len); |
| 1787 | else |
| 1788 | con->write(con, text, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | } |
| 1791 | |
Dave Young | af91322 | 2009-09-22 16:43:33 -0700 | [diff] [blame] | 1792 | int printk_delay_msec __read_mostly; |
| 1793 | |
| 1794 | static 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 Handa | cbae05d | 2019-02-16 19:59:33 +0900 | [diff] [blame] | 1806 | static inline u32 printk_caller_id(void) |
| 1807 | { |
| 1808 | return in_task() ? task_pid_nr(current) : |
| 1809 | 0x80000000 + raw_smp_processor_id(); |
| 1810 | } |
| 1811 | |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1812 | /* |
| 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 | */ |
| 1818 | static struct cont { |
| 1819 | char buf[LOG_LINE_MAX]; |
| 1820 | size_t len; /* length == 0 means unused buffer */ |
Tetsuo Handa | cbae05d | 2019-02-16 19:59:33 +0900 | [diff] [blame] | 1821 | u32 caller_id; /* printk_caller_id() of first print */ |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1822 | u64 ts_nsec; /* time of first print */ |
| 1823 | u8 level; /* log level of first message */ |
Alex Elder | 0b90fec | 2014-08-06 16:09:03 -0700 | [diff] [blame] | 1824 | u8 facility; /* log facility of first message */ |
Kay Sievers | eab0726 | 2012-07-16 18:35:30 -0700 | [diff] [blame] | 1825 | enum log_flags flags; /* prefix, newline flags */ |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1826 | } cont; |
| 1827 | |
Linus Torvalds | 5e46765 | 2016-10-09 11:53:00 -0700 | [diff] [blame] | 1828 | static void cont_flush(void) |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1829 | { |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1830 | if (cont.len == 0) |
| 1831 | return; |
Linus Torvalds | 5c2992e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 1832 | |
Tetsuo Handa | cbae05d | 2019-02-16 19:59:33 +0900 | [diff] [blame] | 1833 | log_store(cont.caller_id, cont.facility, cont.level, cont.flags, |
| 1834 | cont.ts_nsec, NULL, 0, cont.buf, cont.len); |
Linus Torvalds | 5c2992e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 1835 | cont.len = 0; |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1836 | } |
| 1837 | |
Tetsuo Handa | cbae05d | 2019-02-16 19:59:33 +0900 | [diff] [blame] | 1838 | static bool cont_add(u32 caller_id, int facility, int level, |
| 1839 | enum log_flags flags, const char *text, size_t len) |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1840 | { |
Sergey Senozhatsky | 9627808 | 2018-10-02 11:38:34 +0900 | [diff] [blame] | 1841 | /* If the line gets too long, split it up in separate records. */ |
| 1842 | if (cont.len + len > sizeof(cont.buf)) { |
Linus Torvalds | 5e46765 | 2016-10-09 11:53:00 -0700 | [diff] [blame] | 1843 | cont_flush(); |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1844 | return false; |
| 1845 | } |
| 1846 | |
| 1847 | if (!cont.len) { |
| 1848 | cont.facility = facility; |
| 1849 | cont.level = level; |
Tetsuo Handa | cbae05d | 2019-02-16 19:59:33 +0900 | [diff] [blame] | 1850 | cont.caller_id = caller_id; |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1851 | cont.ts_nsec = local_clock(); |
Linus Torvalds | 5e46765 | 2016-10-09 11:53:00 -0700 | [diff] [blame] | 1852 | cont.flags = flags; |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | memcpy(cont.buf + cont.len, text, len); |
| 1856 | cont.len += len; |
Kay Sievers | eab0726 | 2012-07-16 18:35:30 -0700 | [diff] [blame] | 1857 | |
Linus Torvalds | 5e46765 | 2016-10-09 11:53:00 -0700 | [diff] [blame] | 1858 | // 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 Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1865 | return true; |
| 1866 | } |
| 1867 | |
Linus Torvalds | c362c7f | 2016-10-08 22:02:09 -0700 | [diff] [blame] | 1868 | static 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 Handa | cbae05d | 2019-02-16 19:59:33 +0900 | [diff] [blame] | 1870 | const u32 caller_id = printk_caller_id(); |
| 1871 | |
Linus Torvalds | c362c7f | 2016-10-08 22:02:09 -0700 | [diff] [blame] | 1872 | /* |
Linus Torvalds | 5e46765 | 2016-10-09 11:53:00 -0700 | [diff] [blame] | 1873 | * If an earlier line was buffered, and we're a continuation |
Tetsuo Handa | cbae05d | 2019-02-16 19:59:33 +0900 | [diff] [blame] | 1874 | * write from the same context, try to add it to the buffer. |
Linus Torvalds | c362c7f | 2016-10-08 22:02:09 -0700 | [diff] [blame] | 1875 | */ |
| 1876 | if (cont.len) { |
Tetsuo Handa | cbae05d | 2019-02-16 19:59:33 +0900 | [diff] [blame] | 1877 | if (cont.caller_id == caller_id && (lflags & LOG_CONT)) { |
| 1878 | if (cont_add(caller_id, facility, level, lflags, text, text_len)) |
Linus Torvalds | 5e46765 | 2016-10-09 11:53:00 -0700 | [diff] [blame] | 1879 | return text_len; |
| 1880 | } |
| 1881 | /* Otherwise, make sure it's flushed */ |
| 1882 | cont_flush(); |
| 1883 | } |
Linus Torvalds | c362c7f | 2016-10-08 22:02:09 -0700 | [diff] [blame] | 1884 | |
Linus Torvalds | 8835ca5 | 2016-10-19 09:11:24 -0700 | [diff] [blame] | 1885 | /* Skip empty continuation lines that couldn't be added - they just flush */ |
| 1886 | if (!text_len && (lflags & LOG_CONT)) |
| 1887 | return 0; |
| 1888 | |
Linus Torvalds | 5e46765 | 2016-10-09 11:53:00 -0700 | [diff] [blame] | 1889 | /* If it doesn't end in a newline, try to buffer the current line */ |
| 1890 | if (!(lflags & LOG_NEWLINE)) { |
Tetsuo Handa | cbae05d | 2019-02-16 19:59:33 +0900 | [diff] [blame] | 1891 | if (cont_add(caller_id, facility, level, lflags, text, text_len)) |
Linus Torvalds | c362c7f | 2016-10-08 22:02:09 -0700 | [diff] [blame] | 1892 | return text_len; |
| 1893 | } |
| 1894 | |
Linus Torvalds | 5e46765 | 2016-10-09 11:53:00 -0700 | [diff] [blame] | 1895 | /* Store it in the record log */ |
Tetsuo Handa | cbae05d | 2019-02-16 19:59:33 +0900 | [diff] [blame] | 1896 | return log_store(caller_id, facility, level, lflags, 0, |
| 1897 | dict, dictlen, text, text_len); |
Linus Torvalds | c362c7f | 2016-10-08 22:02:09 -0700 | [diff] [blame] | 1898 | } |
| 1899 | |
Petr Mladek | ba55239 | 2018-06-27 16:08:15 +0200 | [diff] [blame] | 1900 | /* Must be called under logbuf_lock. */ |
| 1901 | int vprintk_store(int facility, int level, |
| 1902 | const char *dict, size_t dictlen, |
| 1903 | const char *fmt, va_list args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | { |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1905 | static char textbuf[LOG_LINE_MAX]; |
| 1906 | char *text = textbuf; |
Pierre Kuo | aec47ca | 2017-07-11 14:40:55 +0800 | [diff] [blame] | 1907 | size_t text_len; |
Kay Sievers | 5becfb1 | 2012-07-09 12:15:42 -0700 | [diff] [blame] | 1908 | enum log_flags lflags = 0; |
Jan Kara | 608873c | 2014-06-04 16:11:35 -0700 | [diff] [blame] | 1909 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | /* |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1911 | * The printf needs to come first; we need the syslog |
| 1912 | * prefix which might be passed-in as a parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | */ |
Markus Trippelsdorf | 98e35f5 | 2014-10-13 15:51:13 -0700 | [diff] [blame] | 1914 | text_len = vscnprintf(text, sizeof(textbuf), fmt, args); |
Nick Andrew | ac60ad7 | 2008-05-12 21:21:04 +0200 | [diff] [blame] | 1915 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1916 | /* mark and strip a trailing newline */ |
Kay Sievers | c313af1 | 2012-05-14 20:46:27 +0200 | [diff] [blame] | 1917 | if (text_len && text[text_len-1] == '\n') { |
| 1918 | text_len--; |
Kay Sievers | 5becfb1 | 2012-07-09 12:15:42 -0700 | [diff] [blame] | 1919 | lflags |= LOG_NEWLINE; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1920 | } |
Kay Sievers | 9d90c8d | 2011-03-13 03:19:51 +0100 | [diff] [blame] | 1921 | |
Joe Perches | 088a52a | 2012-07-30 14:40:19 -0700 | [diff] [blame] | 1922 | /* strip kernel syslog prefix and extract log level or control flags */ |
| 1923 | if (facility == 0) { |
Linus Torvalds | 4bcc595 | 2016-10-08 20:32:40 -0700 | [diff] [blame] | 1924 | int kern_level; |
Joe Perches | 088a52a | 2012-07-30 14:40:19 -0700 | [diff] [blame] | 1925 | |
Linus Torvalds | 4bcc595 | 2016-10-08 20:32:40 -0700 | [diff] [blame] | 1926 | while ((kern_level = printk_get_level(text)) != 0) { |
Joe Perches | 088a52a | 2012-07-30 14:40:19 -0700 | [diff] [blame] | 1927 | switch (kern_level) { |
| 1928 | case '0' ... '7': |
Joe Perches | a39d4a8 | 2014-12-10 15:50:15 -0800 | [diff] [blame] | 1929 | if (level == LOGLEVEL_DEFAULT) |
Joe Perches | 088a52a | 2012-07-30 14:40:19 -0700 | [diff] [blame] | 1930 | level = kern_level - '0'; |
Linus Torvalds | 4bcc595 | 2016-10-08 20:32:40 -0700 | [diff] [blame] | 1931 | break; |
| 1932 | case 'c': /* KERN_CONT */ |
| 1933 | lflags |= LOG_CONT; |
Joe Perches | 088a52a | 2012-07-30 14:40:19 -0700 | [diff] [blame] | 1934 | } |
Linus Torvalds | 4bcc595 | 2016-10-08 20:32:40 -0700 | [diff] [blame] | 1935 | |
| 1936 | text_len -= 2; |
| 1937 | text += 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | } |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1939 | } |
Nick Andrew | ac60ad7 | 2008-05-12 21:21:04 +0200 | [diff] [blame] | 1940 | |
Joe Perches | a39d4a8 | 2014-12-10 15:50:15 -0800 | [diff] [blame] | 1941 | if (level == LOGLEVEL_DEFAULT) |
Kay Sievers | c313af1 | 2012-05-14 20:46:27 +0200 | [diff] [blame] | 1942 | level = default_message_loglevel; |
| 1943 | |
Kay Sievers | 5becfb1 | 2012-07-09 12:15:42 -0700 | [diff] [blame] | 1944 | if (dict) |
Tetsuo Handa | e36202a | 2019-02-22 18:59:40 +0900 | [diff] [blame] | 1945 | lflags |= LOG_NEWLINE; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1946 | |
Petr Mladek | ba55239 | 2018-06-27 16:08:15 +0200 | [diff] [blame] | 1947 | return log_output(facility, level, lflags, |
| 1948 | dict, dictlen, text, text_len); |
| 1949 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1950 | |
Petr Mladek | ba55239 | 2018-06-27 16:08:15 +0200 | [diff] [blame] | 1951 | asmlinkage 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 Senozhatsky | 3ac37a9 | 2018-10-02 11:38:35 +0900 | [diff] [blame] | 1956 | bool in_sched = false, pending_output; |
Petr Mladek | ba55239 | 2018-06-27 16:08:15 +0200 | [diff] [blame] | 1957 | unsigned long flags; |
Sergey Senozhatsky | 3ac37a9 | 2018-10-02 11:38:35 +0900 | [diff] [blame] | 1958 | u64 curr_log_seq; |
Petr Mladek | ba55239 | 2018-06-27 16:08:15 +0200 | [diff] [blame] | 1959 | |
Feng Tang | c39ea0b | 2019-05-14 15:45:34 -0700 | [diff] [blame] | 1960 | /* Suppress unimportant messages after panic happens */ |
| 1961 | if (unlikely(suppress_printk)) |
| 1962 | return 0; |
| 1963 | |
Petr Mladek | ba55239 | 2018-06-27 16:08:15 +0200 | [diff] [blame] | 1964 | 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 Senozhatsky | 3ac37a9 | 2018-10-02 11:38:35 +0900 | [diff] [blame] | 1974 | curr_log_seq = log_next_seq; |
Petr Mladek | ba55239 | 2018-06-27 16:08:15 +0200 | [diff] [blame] | 1975 | printed_len = vprintk_store(facility, level, dict, dictlen, fmt, args); |
Sergey Senozhatsky | 3ac37a9 | 2018-10-02 11:38:35 +0900 | [diff] [blame] | 1976 | pending_output = (curr_log_seq != log_next_seq); |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 1977 | logbuf_unlock_irqrestore(flags); |
Jan Kara | 939f04b | 2014-06-04 16:11:37 -0700 | [diff] [blame] | 1978 | |
Steven Rostedt | 458df9f | 2014-06-04 16:11:38 -0700 | [diff] [blame] | 1979 | /* If called from the scheduler, we can not call up(). */ |
Sergey Senozhatsky | 3ac37a9 | 2018-10-02 11:38:35 +0900 | [diff] [blame] | 1980 | if (!in_sched && pending_output) { |
Jan Kara | 5874af2 | 2014-08-06 16:09:10 -0700 | [diff] [blame] | 1981 | /* |
Sergey Senozhatsky | fd5f7cd | 2018-01-16 13:47:16 +0900 | [diff] [blame] | 1982 | * 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 Morton | d18bbc2 | 2014-07-02 15:22:38 -0700 | [diff] [blame] | 1988 | * 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 Mladek | c162d5b | 2018-01-12 17:08:37 +0100 | [diff] [blame] | 1992 | if (console_trylock_spinning()) |
Andrew Morton | d18bbc2 | 2014-07-02 15:22:38 -0700 | [diff] [blame] | 1993 | console_unlock(); |
Sergey Senozhatsky | fd5f7cd | 2018-01-16 13:47:16 +0900 | [diff] [blame] | 1994 | preempt_enable(); |
Andrew Morton | d18bbc2 | 2014-07-02 15:22:38 -0700 | [diff] [blame] | 1995 | } |
Steven Rostedt | 458df9f | 2014-06-04 16:11:38 -0700 | [diff] [blame] | 1996 | |
Sergey Senozhatsky | 3ac37a9 | 2018-10-02 11:38:35 +0900 | [diff] [blame] | 1997 | if (pending_output) |
| 1998 | wake_up_klogd(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | return printed_len; |
| 2000 | } |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2001 | EXPORT_SYMBOL(vprintk_emit); |
| 2002 | |
| 2003 | asmlinkage int vprintk(const char *fmt, va_list args) |
| 2004 | { |
Sergey Senozhatsky | bd66a89 | 2016-12-27 23:16:04 +0900 | [diff] [blame] | 2005 | return vprintk_func(fmt, args); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2006 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2007 | EXPORT_SYMBOL(vprintk); |
| 2008 | |
Linus Torvalds | a0cba21 | 2016-08-09 10:48:18 -0700 | [diff] [blame] | 2009 | int vprintk_default(const char *fmt, va_list args) |
Steven Rostedt (Red Hat) | afdc34a | 2014-06-19 17:33:31 -0400 | [diff] [blame] | 2010 | { |
| 2011 | int r; |
| 2012 | |
| 2013 | #ifdef CONFIG_KGDB_KDB |
Petr Mladek | 34aaff4 | 2016-12-14 15:05:58 -0800 | [diff] [blame] | 2014 | /* Allow to pass printk() to kdb but avoid a recursion. */ |
| 2015 | if (unlikely(kdb_trap_printk && kdb_printf_cpu < 0)) { |
Daniel Thompson | f7d4ca8 | 2014-11-07 18:37:57 +0000 | [diff] [blame] | 2016 | r = vkdb_printf(KDB_MSGSRC_PRINTK, fmt, args); |
Steven Rostedt (Red Hat) | afdc34a | 2014-06-19 17:33:31 -0400 | [diff] [blame] | 2017 | return r; |
| 2018 | } |
| 2019 | #endif |
Linus Torvalds | a0cba21 | 2016-08-09 10:48:18 -0700 | [diff] [blame] | 2020 | r = vprintk_emit(0, LOGLEVEL_DEFAULT, NULL, 0, fmt, args); |
Steven Rostedt (Red Hat) | afdc34a | 2014-06-19 17:33:31 -0400 | [diff] [blame] | 2021 | |
| 2022 | return r; |
| 2023 | } |
| 2024 | EXPORT_SYMBOL_GPL(vprintk_default); |
| 2025 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2026 | /** |
| 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 Kleen | 722a9f9 | 2014-05-02 00:44:38 +0200 | [diff] [blame] | 2047 | asmlinkage __visible int printk(const char *fmt, ...) |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2048 | { |
| 2049 | va_list args; |
| 2050 | int r; |
| 2051 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2052 | va_start(args, fmt); |
Linus Torvalds | a0cba21 | 2016-08-09 10:48:18 -0700 | [diff] [blame] | 2053 | r = vprintk_func(fmt, args); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2054 | va_end(args); |
| 2055 | |
| 2056 | return r; |
| 2057 | } |
| 2058 | EXPORT_SYMBOL(printk); |
Kay Sievers | 7f3a781 | 2012-05-09 01:37:51 +0200 | [diff] [blame] | 2059 | |
Kay Sievers | 96efedf | 2012-07-16 18:35:29 -0700 | [diff] [blame] | 2060 | #else /* CONFIG_PRINTK */ |
Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 2061 | |
Kay Sievers | 7049825 | 2012-07-16 18:35:29 -0700 | [diff] [blame] | 2062 | #define LOG_LINE_MAX 0 |
| 2063 | #define PREFIX_MAX 0 |
Tetsuo Handa | e80c1a9 | 2018-12-04 19:00:01 +0900 | [diff] [blame] | 2064 | #define printk_time false |
Alex Elder | 249771b | 2014-08-06 16:09:08 -0700 | [diff] [blame] | 2065 | |
Kay Sievers | 96efedf | 2012-07-16 18:35:29 -0700 | [diff] [blame] | 2066 | static u64 syslog_seq; |
| 2067 | static u32 syslog_idx; |
Kay Sievers | eab0726 | 2012-07-16 18:35:30 -0700 | [diff] [blame] | 2068 | static u64 console_seq; |
| 2069 | static u32 console_idx; |
Petr Mladek | f92b070 | 2018-09-13 14:34:06 +0200 | [diff] [blame] | 2070 | static u64 exclusive_console_stop_seq; |
Kay Sievers | 96efedf | 2012-07-16 18:35:29 -0700 | [diff] [blame] | 2071 | static u64 log_first_seq; |
| 2072 | static u32 log_first_idx; |
| 2073 | static u64 log_next_seq; |
Tejun Heo | 6fe2935 | 2015-06-25 15:01:30 -0700 | [diff] [blame] | 2074 | static char *log_text(const struct printk_log *msg) { return NULL; } |
| 2075 | static char *log_dict(const struct printk_log *msg) { return NULL; } |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 2076 | static struct printk_log *log_from_idx(u32 idx) { return NULL; } |
Kay Sievers | 7f3a781 | 2012-05-09 01:37:51 +0200 | [diff] [blame] | 2077 | static u32 log_next(u32 idx) { return 0; } |
Tejun Heo | 6fe2935 | 2015-06-25 15:01:30 -0700 | [diff] [blame] | 2078 | static ssize_t msg_print_ext_header(char *buf, size_t size, |
Linus Torvalds | 5aa068e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 2079 | struct printk_log *msg, |
| 2080 | u64 seq) { return 0; } |
Tejun Heo | 6fe2935 | 2015-06-25 15:01:30 -0700 | [diff] [blame] | 2081 | static 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 Mladek | c162d5b | 2018-01-12 17:08:37 +0100 | [diff] [blame] | 2084 | static void console_lock_spinning_enable(void) { } |
| 2085 | static int console_lock_spinning_disable_and_check(void) { return 0; } |
Sergey Senozhatsky | d9c2352 | 2016-12-24 23:09:01 +0900 | [diff] [blame] | 2086 | static void call_console_drivers(const char *ext_text, size_t ext_len, |
Tejun Heo | 6fe2935 | 2015-06-25 15:01:30 -0700 | [diff] [blame] | 2087 | const char *text, size_t len) {} |
Tetsuo Handa | e80c1a9 | 2018-12-04 19:00:01 +0900 | [diff] [blame] | 2088 | static size_t msg_print_text(const struct printk_log *msg, bool syslog, |
| 2089 | bool time, char *buf, size_t size) { return 0; } |
Petr Mladek | a6ae928 | 2018-09-10 15:52:06 +0200 | [diff] [blame] | 2090 | static bool suppress_message_printing(int level) { return false; } |
Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 2091 | |
Kay Sievers | 7f3a781 | 2012-05-09 01:37:51 +0200 | [diff] [blame] | 2092 | #endif /* CONFIG_PRINTK */ |
Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 2093 | |
Thomas Gleixner | d0380e6 | 2013-04-29 16:17:18 -0700 | [diff] [blame] | 2094 | #ifdef CONFIG_EARLY_PRINTK |
| 2095 | struct console *early_console; |
| 2096 | |
Andi Kleen | 722a9f9 | 2014-05-02 00:44:38 +0200 | [diff] [blame] | 2097 | asmlinkage __visible void early_printk(const char *fmt, ...) |
Thomas Gleixner | d0380e6 | 2013-04-29 16:17:18 -0700 | [diff] [blame] | 2098 | { |
| 2099 | va_list ap; |
Joe Perches | 1dc6244 | 2014-12-10 15:45:53 -0800 | [diff] [blame] | 2100 | char buf[512]; |
| 2101 | int n; |
| 2102 | |
| 2103 | if (!early_console) |
| 2104 | return; |
Thomas Gleixner | d0380e6 | 2013-04-29 16:17:18 -0700 | [diff] [blame] | 2105 | |
| 2106 | va_start(ap, fmt); |
Joe Perches | 1dc6244 | 2014-12-10 15:45:53 -0800 | [diff] [blame] | 2107 | n = vscnprintf(buf, sizeof(buf), fmt, ap); |
Thomas Gleixner | d0380e6 | 2013-04-29 16:17:18 -0700 | [diff] [blame] | 2108 | va_end(ap); |
Joe Perches | 1dc6244 | 2014-12-10 15:45:53 -0800 | [diff] [blame] | 2109 | |
| 2110 | early_console->write(early_console, buf, n); |
Thomas Gleixner | d0380e6 | 2013-04-29 16:17:18 -0700 | [diff] [blame] | 2111 | } |
| 2112 | #endif |
| 2113 | |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 2114 | static 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 Mladek | dac8bbb | 2017-06-08 12:01:30 +0200 | [diff] [blame] | 2124 | for (i = 0, c = console_cmdline; |
| 2125 | i < MAX_CMDLINECONSOLES && c->name[0]; |
| 2126 | i++, c++) { |
Joe Perches | 2347540 | 2013-07-31 13:53:46 -0700 | [diff] [blame] | 2127 | if (strcmp(c->name, name) == 0 && c->index == idx) { |
Petr Mladek | dac8bbb | 2017-06-08 12:01:30 +0200 | [diff] [blame] | 2128 | if (!brl_options) |
| 2129 | preferred_console = i; |
Joe Perches | 2347540 | 2013-07-31 13:53:46 -0700 | [diff] [blame] | 2130 | return 0; |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 2131 | } |
Joe Perches | 2347540 | 2013-07-31 13:53:46 -0700 | [diff] [blame] | 2132 | } |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 2133 | if (i == MAX_CMDLINECONSOLES) |
| 2134 | return -E2BIG; |
| 2135 | if (!brl_options) |
Aleksey Makarov | ad86ee2 | 2017-03-15 13:28:51 +0300 | [diff] [blame] | 2136 | preferred_console = i; |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 2137 | strlcpy(c->name, name, sizeof(c->name)); |
| 2138 | c->options = options; |
Joe Perches | bbeddf5 | 2013-07-31 13:53:45 -0700 | [diff] [blame] | 2139 | braille_set_options(c, brl_options); |
| 2140 | |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 2141 | c->index = idx; |
| 2142 | return 0; |
| 2143 | } |
Sergey Senozhatsky | cca10d5 | 2017-12-21 14:41:49 +0900 | [diff] [blame] | 2144 | |
| 2145 | static 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. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 2155 | /* |
Alex Elder | 0b90fec | 2014-08-06 16:09:03 -0700 | [diff] [blame] | 2156 | * 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. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 2158 | */ |
| 2159 | static int __init console_setup(char *str) |
| 2160 | { |
Alex Elder | 0b90fec | 2014-08-06 16:09:03 -0700 | [diff] [blame] | 2161 | char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for "ttyS" */ |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 2162 | char *s, *options, *brl_options = NULL; |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 2163 | int idx; |
| 2164 | |
Joe Perches | bbeddf5 | 2013-07-31 13:53:45 -0700 | [diff] [blame] | 2165 | if (_braille_console_setup(&str, &brl_options)) |
| 2166 | return 1; |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 2167 | |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 2168 | /* |
| 2169 | * Decode str into name, index, options. |
| 2170 | */ |
| 2171 | if (str[0] >= '0' && str[0] <= '9') { |
Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 2172 | strcpy(buf, "ttyS"); |
| 2173 | strncpy(buf + 4, str, sizeof(buf) - 5); |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 2174 | } else { |
Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 2175 | strncpy(buf, str, sizeof(buf) - 1); |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 2176 | } |
Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 2177 | buf[sizeof(buf) - 1] = 0; |
Alex Elder | 249771b | 2014-08-06 16:09:08 -0700 | [diff] [blame] | 2178 | options = strchr(str, ','); |
| 2179 | if (options) |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 2180 | *(options++) = 0; |
| 2181 | #ifdef __sparc__ |
| 2182 | if (!strcmp(str, "ttya")) |
Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 2183 | strcpy(buf, "ttyS0"); |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 2184 | if (!strcmp(str, "ttyb")) |
Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 2185 | strcpy(buf, "ttyS1"); |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 2186 | #endif |
Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 2187 | for (s = buf; *s; s++) |
Alex Elder | 249771b | 2014-08-06 16:09:08 -0700 | [diff] [blame] | 2188 | if (isdigit(*s) || *s == ',') |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 2189 | break; |
| 2190 | idx = simple_strtoul(s, NULL, 10); |
| 2191 | *s = 0; |
| 2192 | |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 2193 | __add_preferred_console(buf, idx, options, brl_options); |
Markus Armbruster | 9e124fe | 2008-05-26 23:31:07 +0100 | [diff] [blame] | 2194 | console_set_on_cmdline = 1; |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 2195 | return 1; |
| 2196 | } |
| 2197 | __setup("console=", console_setup); |
| 2198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2199 | /** |
Matt Mackall | 3c0547b | 2005-05-16 21:53:47 -0700 | [diff] [blame] | 2200 | * add_preferred_console - add a device to the list of preferred consoles. |
Martin Waitz | ddad86c | 2005-11-13 16:08:14 -0800 | [diff] [blame] | 2201 | * @name: device name |
| 2202 | * @idx: device index |
| 2203 | * @options: options for this console |
Matt Mackall | 3c0547b | 2005-05-16 21:53:47 -0700 | [diff] [blame] | 2204 | * |
| 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. Miller | fb445ee | 2007-12-29 01:19:49 -0800 | [diff] [blame] | 2212 | int add_preferred_console(char *name, int idx, char *options) |
Matt Mackall | 3c0547b | 2005-05-16 21:53:47 -0700 | [diff] [blame] | 2213 | { |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 2214 | return __add_preferred_console(name, idx, options, NULL); |
Matt Mackall | 3c0547b | 2005-05-16 21:53:47 -0700 | [diff] [blame] | 2215 | } |
| 2216 | |
Neil Zhang | d25d9fe | 2014-08-06 16:09:12 -0700 | [diff] [blame] | 2217 | bool console_suspend_enabled = true; |
Andres Salomon | 8f4ce8c | 2007-10-18 03:04:50 -0700 | [diff] [blame] | 2218 | EXPORT_SYMBOL(console_suspend_enabled); |
| 2219 | |
| 2220 | static int __init console_suspend_disable(char *str) |
| 2221 | { |
Neil Zhang | d25d9fe | 2014-08-06 16:09:12 -0700 | [diff] [blame] | 2222 | console_suspend_enabled = false; |
Andres Salomon | 8f4ce8c | 2007-10-18 03:04:50 -0700 | [diff] [blame] | 2223 | return 1; |
| 2224 | } |
| 2225 | __setup("no_console_suspend", console_suspend_disable); |
Yanmin Zhang | 134620f | 2011-10-31 17:11:27 -0700 | [diff] [blame] | 2226 | module_param_named(console_suspend, console_suspend_enabled, |
| 2227 | bool, S_IRUGO | S_IWUSR); |
| 2228 | MODULE_PARM_DESC(console_suspend, "suspend console during suspend" |
| 2229 | " and hibernate operations"); |
Andres Salomon | 8f4ce8c | 2007-10-18 03:04:50 -0700 | [diff] [blame] | 2230 | |
Matt Mackall | 3c0547b | 2005-05-16 21:53:47 -0700 | [diff] [blame] | 2231 | /** |
Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 2232 | * suspend_console - suspend the console subsystem |
| 2233 | * |
| 2234 | * This disables printk() while we go into suspend states |
| 2235 | */ |
| 2236 | void suspend_console(void) |
| 2237 | { |
Andres Salomon | 8f4ce8c | 2007-10-18 03:04:50 -0700 | [diff] [blame] | 2238 | if (!console_suspend_enabled) |
| 2239 | return; |
Tomeu Vizoso | 47319f7 | 2018-03-22 14:58:33 +0100 | [diff] [blame] | 2240 | pr_info("Suspending console(s) (use no_console_suspend to debug)\n"); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2241 | console_lock(); |
Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 2242 | console_suspended = 1; |
Jan Kara | bd8d7cf | 2014-06-04 16:11:36 -0700 | [diff] [blame] | 2243 | up_console_sem(); |
Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | void resume_console(void) |
| 2247 | { |
Andres Salomon | 8f4ce8c | 2007-10-18 03:04:50 -0700 | [diff] [blame] | 2248 | if (!console_suspend_enabled) |
| 2249 | return; |
Jan Kara | bd8d7cf | 2014-06-04 16:11:36 -0700 | [diff] [blame] | 2250 | down_console_sem(); |
Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 2251 | console_suspended = 0; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2252 | console_unlock(); |
Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | /** |
Kevin Cernekee | 034260d | 2010-06-03 22:11:25 -0700 | [diff] [blame] | 2256 | * console_cpu_notify - print deferred console messages after CPU hotplug |
Sebastian Andrzej Siewior | 90b1488 | 2016-11-03 15:49:58 +0100 | [diff] [blame] | 2257 | * @cpu: unused |
Kevin Cernekee | 034260d | 2010-06-03 22:11:25 -0700 | [diff] [blame] | 2258 | * |
| 2259 | * If printk() is called from a CPU that is not online yet, the messages |
Sergey Senozhatsky | 64ca752 | 2017-01-21 19:47:29 +0900 | [diff] [blame] | 2260 | * 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 Cernekee | 034260d | 2010-06-03 22:11:25 -0700 | [diff] [blame] | 2263 | */ |
Sebastian Andrzej Siewior | 90b1488 | 2016-11-03 15:49:58 +0100 | [diff] [blame] | 2264 | static int console_cpu_notify(unsigned int cpu) |
Kevin Cernekee | 034260d | 2010-06-03 22:11:25 -0700 | [diff] [blame] | 2265 | { |
Thomas Gleixner | f97960f | 2016-11-17 17:31:55 +0100 | [diff] [blame] | 2266 | if (!cpuhp_tasks_frozen) { |
Sergey Senozhatsky | 64ca752 | 2017-01-21 19:47:29 +0900 | [diff] [blame] | 2267 | /* If trylock fails, someone else is doing the printing */ |
| 2268 | if (console_trylock()) |
| 2269 | console_unlock(); |
Kevin Cernekee | 034260d | 2010-06-03 22:11:25 -0700 | [diff] [blame] | 2270 | } |
Sebastian Andrzej Siewior | 90b1488 | 2016-11-03 15:49:58 +0100 | [diff] [blame] | 2271 | return 0; |
Kevin Cernekee | 034260d | 2010-06-03 22:11:25 -0700 | [diff] [blame] | 2272 | } |
| 2273 | |
| 2274 | /** |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2275 | * console_lock - lock the console system for exclusive use. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2276 | * |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2277 | * Acquires a lock which guarantees that the caller has |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | * exclusive access to the console system and the console_drivers list. |
| 2279 | * |
| 2280 | * Can sleep, returns nothing. |
| 2281 | */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2282 | void console_lock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | { |
Daniel Vetter | 6b898c0 | 2012-09-17 23:03:31 +0000 | [diff] [blame] | 2284 | might_sleep(); |
| 2285 | |
Jan Kara | bd8d7cf | 2014-06-04 16:11:36 -0700 | [diff] [blame] | 2286 | down_console_sem(); |
Arve Hjønnevåg | 403f307 | 2009-02-14 02:07:24 +0100 | [diff] [blame] | 2287 | if (console_suspended) |
| 2288 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | console_locked = 1; |
| 2290 | console_may_schedule = 1; |
| 2291 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2292 | EXPORT_SYMBOL(console_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2294 | /** |
| 2295 | * console_trylock - try to lock the console system for exclusive use. |
| 2296 | * |
Alex Elder | 0b90fec | 2014-08-06 16:09:03 -0700 | [diff] [blame] | 2297 | * Try to acquire a lock which guarantees that the caller has exclusive |
| 2298 | * access to the console system and the console_drivers list. |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2299 | * |
| 2300 | * returns 1 on success, and 0 on failure to acquire the lock. |
| 2301 | */ |
| 2302 | int console_trylock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2303 | { |
Jan Kara | bd8d7cf | 2014-06-04 16:11:36 -0700 | [diff] [blame] | 2304 | if (down_trylock_console_sem()) |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2305 | return 0; |
Arve Hjønnevåg | 403f307 | 2009-02-14 02:07:24 +0100 | [diff] [blame] | 2306 | if (console_suspended) { |
Jan Kara | bd8d7cf | 2014-06-04 16:11:36 -0700 | [diff] [blame] | 2307 | up_console_sem(); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2308 | return 0; |
Arve Hjønnevåg | 403f307 | 2009-02-14 02:07:24 +0100 | [diff] [blame] | 2309 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | console_locked = 1; |
Sergey Senozhatsky | fd5f7cd | 2018-01-16 13:47:16 +0900 | [diff] [blame] | 2311 | console_may_schedule = 0; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2312 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2313 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2314 | EXPORT_SYMBOL(console_trylock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | |
| 2316 | int is_console_locked(void) |
| 2317 | { |
| 2318 | return console_locked; |
| 2319 | } |
Hans de Goede | d48de54 | 2018-06-28 15:20:27 +0200 | [diff] [blame] | 2320 | EXPORT_SYMBOL(is_console_locked); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | |
Sergey Senozhatsky | a819937 | 2016-03-17 14:21:20 -0700 | [diff] [blame] | 2322 | /* |
| 2323 | * Check if we have any console that is capable of printing while cpu is |
| 2324 | * booting or shutting down. Requires console_sem. |
| 2325 | */ |
| 2326 | static int have_callable_console(void) |
| 2327 | { |
| 2328 | struct console *con; |
| 2329 | |
| 2330 | for_each_console(con) |
Sergey Senozhatsky | adaf659 | 2016-03-17 14:21:27 -0700 | [diff] [blame] | 2331 | if ((con->flags & CON_ENABLED) && |
| 2332 | (con->flags & CON_ANYTIME)) |
Sergey Senozhatsky | a819937 | 2016-03-17 14:21:20 -0700 | [diff] [blame] | 2333 | 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 | */ |
| 2345 | static inline int can_use_console(void) |
| 2346 | { |
| 2347 | return cpu_online(raw_smp_processor_id()) || have_callable_console(); |
| 2348 | } |
| 2349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | /** |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2351 | * console_unlock - unlock the console system |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2352 | * |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2353 | * Releases the console_lock which the caller holds on the console system |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2354 | * and the console driver list. |
| 2355 | * |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2356 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2359 | * |
Kay Sievers | 7f3a781 | 2012-05-09 01:37:51 +0200 | [diff] [blame] | 2360 | * If there is output waiting, we wake /dev/kmsg and syslog() users. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2361 | * |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2362 | * console_unlock(); may be called from any context. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2364 | void console_unlock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | { |
Tejun Heo | 6fe2935 | 2015-06-25 15:01:30 -0700 | [diff] [blame] | 2366 | static char ext_text[CONSOLE_EXT_LOG_MAX]; |
Kay Sievers | 7049825 | 2012-07-16 18:35:29 -0700 | [diff] [blame] | 2367 | static char text[LOG_LINE_MAX + PREFIX_MAX]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | unsigned long flags; |
Tejun Heo | 8d91f8b | 2016-01-15 16:58:24 -0800 | [diff] [blame] | 2369 | bool do_cond_resched, retry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2370 | |
Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 2371 | if (console_suspended) { |
Jan Kara | bd8d7cf | 2014-06-04 16:11:36 -0700 | [diff] [blame] | 2372 | up_console_sem(); |
Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 2373 | return; |
| 2374 | } |
Antonino A. Daplas | 78944e54 | 2006-08-05 12:14:16 -0700 | [diff] [blame] | 2375 | |
Tejun Heo | 8d91f8b | 2016-01-15 16:58:24 -0800 | [diff] [blame] | 2376 | /* |
Petr Mladek | 257ab44 | 2017-03-24 17:14:05 +0100 | [diff] [blame] | 2377 | * Console drivers are called with interrupts disabled, so |
Tejun Heo | 8d91f8b | 2016-01-15 16:58:24 -0800 | [diff] [blame] | 2378 | * @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 Mladek | 257ab44 | 2017-03-24 17:14:05 +0100 | [diff] [blame] | 2385 | * |
| 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 Heo | 8d91f8b | 2016-01-15 16:58:24 -0800 | [diff] [blame] | 2389 | */ |
| 2390 | do_cond_resched = console_may_schedule; |
Petr Mladek | 257ab44 | 2017-03-24 17:14:05 +0100 | [diff] [blame] | 2391 | again: |
Antonino A. Daplas | 78944e54 | 2006-08-05 12:14:16 -0700 | [diff] [blame] | 2392 | console_may_schedule = 0; |
| 2393 | |
Sergey Senozhatsky | a819937 | 2016-03-17 14:21:20 -0700 | [diff] [blame] | 2394 | /* |
| 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 Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2405 | for (;;) { |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 2406 | struct printk_log *msg; |
Tejun Heo | 6fe2935 | 2015-06-25 15:01:30 -0700 | [diff] [blame] | 2407 | size_t ext_len = 0; |
Kay Sievers | 3ce9a7c | 2012-05-13 23:30:46 +0200 | [diff] [blame] | 2408 | size_t len; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2409 | |
Sergey Senozhatsky | f975237 | 2016-12-27 23:16:09 +0900 | [diff] [blame] | 2410 | printk_safe_enter_irqsave(flags); |
| 2411 | raw_spin_lock(&logbuf_lock); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2412 | if (console_seq < log_first_seq) { |
He Zhe | 51a72ab | 2018-09-30 00:45:51 +0800 | [diff] [blame] | 2413 | len = sprintf(text, |
| 2414 | "** %llu printk messages dropped **\n", |
| 2415 | log_first_seq - console_seq); |
Will Deacon | 84b5ec8 | 2014-06-04 16:11:45 -0700 | [diff] [blame] | 2416 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2417 | /* messages are gone, move to first one */ |
| 2418 | console_seq = log_first_seq; |
| 2419 | console_idx = log_first_idx; |
Will Deacon | 84b5ec8 | 2014-06-04 16:11:45 -0700 | [diff] [blame] | 2420 | } else { |
| 2421 | len = 0; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2422 | } |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 2423 | skip: |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2424 | if (console_seq == log_next_seq) |
| 2425 | break; |
| 2426 | |
| 2427 | msg = log_from_idx(console_idx); |
Petr Mladek | a6ae928 | 2018-09-10 15:52:06 +0200 | [diff] [blame] | 2428 | if (suppress_message_printing(msg->level)) { |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 2429 | /* |
Petr Mladek | a6ae928 | 2018-09-10 15:52:06 +0200 | [diff] [blame] | 2430 | * 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 Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 2433 | */ |
| 2434 | console_idx = log_next(console_idx); |
| 2435 | console_seq++; |
| 2436 | goto skip; |
| 2437 | } |
Kay Sievers | 649e6ee | 2012-05-10 04:30:45 +0200 | [diff] [blame] | 2438 | |
Petr Mladek | f92b070 | 2018-09-13 14:34:06 +0200 | [diff] [blame] | 2439 | /* 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 Senozhatsky | cca10d5 | 2017-12-21 14:41:49 +0900 | [diff] [blame] | 2445 | len += msg_print_text(msg, |
| 2446 | console_msg_format & MSG_FORMAT_SYSLOG, |
Tetsuo Handa | e80c1a9 | 2018-12-04 19:00:01 +0900 | [diff] [blame] | 2447 | printk_time, text + len, sizeof(text) - len); |
Tejun Heo | 6fe2935 | 2015-06-25 15:01:30 -0700 | [diff] [blame] | 2448 | if (nr_ext_console_drivers) { |
| 2449 | ext_len = msg_print_ext_header(ext_text, |
| 2450 | sizeof(ext_text), |
Linus Torvalds | 5aa068e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 2451 | msg, console_seq); |
Tejun Heo | 6fe2935 | 2015-06-25 15:01:30 -0700 | [diff] [blame] | 2452 | 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 Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2457 | console_idx = log_next(console_idx); |
| 2458 | console_seq++; |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 2459 | raw_spin_unlock(&logbuf_lock); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2460 | |
Steven Rostedt (VMware) | dbdda84 | 2018-01-10 14:24:17 +0100 | [diff] [blame] | 2461 | /* |
| 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 Mladek | c162d5b | 2018-01-12 17:08:37 +0100 | [diff] [blame] | 2467 | console_lock_spinning_enable(); |
Steven Rostedt (VMware) | dbdda84 | 2018-01-10 14:24:17 +0100 | [diff] [blame] | 2468 | |
Steven Rostedt | 81d68a9 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2469 | stop_critical_timings(); /* don't trace print latency */ |
Sergey Senozhatsky | d9c2352 | 2016-12-24 23:09:01 +0900 | [diff] [blame] | 2470 | call_console_drivers(ext_text, ext_len, text, len); |
Steven Rostedt | 81d68a9 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2471 | start_critical_timings(); |
Steven Rostedt (VMware) | dbdda84 | 2018-01-10 14:24:17 +0100 | [diff] [blame] | 2472 | |
Petr Mladek | c162d5b | 2018-01-12 17:08:37 +0100 | [diff] [blame] | 2473 | if (console_lock_spinning_disable_and_check()) { |
| 2474 | printk_safe_exit_irqrestore(flags); |
Sergey Senozhatsky | 43a1711 | 2018-04-19 10:42:50 +0900 | [diff] [blame] | 2475 | return; |
Petr Mladek | c162d5b | 2018-01-12 17:08:37 +0100 | [diff] [blame] | 2476 | } |
Steven Rostedt (VMware) | dbdda84 | 2018-01-10 14:24:17 +0100 | [diff] [blame] | 2477 | |
Sergey Senozhatsky | f975237 | 2016-12-27 23:16:09 +0900 | [diff] [blame] | 2478 | printk_safe_exit_irqrestore(flags); |
Tejun Heo | 8d91f8b | 2016-01-15 16:58:24 -0800 | [diff] [blame] | 2479 | |
| 2480 | if (do_cond_resched) |
| 2481 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2482 | } |
Steven Rostedt (VMware) | dbdda84 | 2018-01-10 14:24:17 +0100 | [diff] [blame] | 2483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2484 | console_locked = 0; |
Feng Tang | fe3d8ad | 2011-03-22 16:34:21 -0700 | [diff] [blame] | 2485 | |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 2486 | raw_spin_unlock(&logbuf_lock); |
Peter Zijlstra | 4f2a8d3 | 2011-06-22 11:20:09 +0200 | [diff] [blame] | 2487 | |
Jan Kara | bd8d7cf | 2014-06-04 16:11:36 -0700 | [diff] [blame] | 2488 | up_console_sem(); |
Peter Zijlstra | 4f2a8d3 | 2011-06-22 11:20:09 +0200 | [diff] [blame] | 2489 | |
| 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 Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 2496 | raw_spin_lock(&logbuf_lock); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2497 | retry = console_seq != log_next_seq; |
Sergey Senozhatsky | f975237 | 2016-12-27 23:16:09 +0900 | [diff] [blame] | 2498 | raw_spin_unlock(&logbuf_lock); |
| 2499 | printk_safe_exit_irqrestore(flags); |
Peter Zijlstra | 09dc3cf | 2011-12-08 14:34:13 -0800 | [diff] [blame] | 2500 | |
Peter Zijlstra | 4f2a8d3 | 2011-06-22 11:20:09 +0200 | [diff] [blame] | 2501 | if (retry && console_trylock()) |
| 2502 | goto again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2504 | EXPORT_SYMBOL(console_unlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2505 | |
Martin Waitz | ddad86c | 2005-11-13 16:08:14 -0800 | [diff] [blame] | 2506 | /** |
| 2507 | * console_conditional_schedule - yield the CPU if required |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2508 | * |
| 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 Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2513 | * Must be called within console_lock();. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2514 | */ |
| 2515 | void __sched console_conditional_schedule(void) |
| 2516 | { |
| 2517 | if (console_may_schedule) |
| 2518 | cond_resched(); |
| 2519 | } |
| 2520 | EXPORT_SYMBOL(console_conditional_schedule); |
| 2521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2522 | void 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 Kara | bd8d7cf | 2014-06-04 16:11:36 -0700 | [diff] [blame] | 2531 | if (down_trylock_console_sem() != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2532 | return; |
| 2533 | } else |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2534 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2535 | |
| 2536 | console_locked = 1; |
| 2537 | console_may_schedule = 0; |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2538 | for_each_console(c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2539 | if ((c->flags & CON_ENABLED) && c->unblank) |
| 2540 | c->unblank(); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2541 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2542 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2543 | |
Tejun Heo | 8d91f8b | 2016-01-15 16:58:24 -0800 | [diff] [blame] | 2544 | /** |
| 2545 | * console_flush_on_panic - flush console content on panic |
Feng Tang | de6da1e | 2019-05-17 14:31:50 -0700 | [diff] [blame] | 2546 | * @mode: flush all messages in buffer or just the pending ones |
Tejun Heo | 8d91f8b | 2016-01-15 16:58:24 -0800 | [diff] [blame] | 2547 | * |
| 2548 | * Immediately output all pending messages no matter what. |
| 2549 | */ |
Feng Tang | de6da1e | 2019-05-17 14:31:50 -0700 | [diff] [blame] | 2550 | void console_flush_on_panic(enum con_flush_mode mode) |
Tejun Heo | 8d91f8b | 2016-01-15 16:58:24 -0800 | [diff] [blame] | 2551 | { |
| 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 Tang | de6da1e | 2019-05-17 14:31:50 -0700 | [diff] [blame] | 2561 | |
| 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 Heo | 8d91f8b | 2016-01-15 16:58:24 -0800 | [diff] [blame] | 2570 | console_unlock(); |
| 2571 | } |
| 2572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2573 | /* |
| 2574 | * Return the console tty driver structure and its associated index |
| 2575 | */ |
| 2576 | struct tty_driver *console_device(int *index) |
| 2577 | { |
| 2578 | struct console *c; |
| 2579 | struct tty_driver *driver = NULL; |
| 2580 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2581 | console_lock(); |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2582 | for_each_console(c) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2583 | if (!c->device) |
| 2584 | continue; |
| 2585 | driver = c->device(c, index); |
| 2586 | if (driver) |
| 2587 | break; |
| 2588 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2589 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2590 | 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 | */ |
| 2598 | void console_stop(struct console *console) |
| 2599 | { |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2600 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2601 | console->flags &= ~CON_ENABLED; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2602 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2603 | } |
| 2604 | EXPORT_SYMBOL(console_stop); |
| 2605 | |
| 2606 | void console_start(struct console *console) |
| 2607 | { |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2608 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2609 | console->flags |= CON_ENABLED; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2610 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2611 | } |
| 2612 | EXPORT_SYMBOL(console_start); |
| 2613 | |
Fabio M. Di Nitto | 7bf6939 | 2011-03-22 16:34:20 -0700 | [diff] [blame] | 2614 | static int __read_mostly keep_bootcon; |
| 2615 | |
| 2616 | static int __init keep_bootcon_setup(char *str) |
| 2617 | { |
| 2618 | keep_bootcon = 1; |
Andrew Morton | 27083ba | 2013-11-12 15:08:50 -0800 | [diff] [blame] | 2619 | pr_info("debug: skip boot console de-registration.\n"); |
Fabio M. Di Nitto | 7bf6939 | 2011-03-22 16:34:20 -0700 | [diff] [blame] | 2620 | |
| 2621 | return 0; |
| 2622 | } |
| 2623 | |
| 2624 | early_param("keep_bootcon", keep_bootcon_setup); |
| 2625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2626 | /* |
| 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 Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2631 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2644 | */ |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2645 | void register_console(struct console *newcon) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2646 | { |
Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 2647 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2648 | unsigned long flags; |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2649 | struct console *bcon = NULL; |
Joe Perches | 2347540 | 2013-07-31 13:53:46 -0700 | [diff] [blame] | 2650 | struct console_cmdline *c; |
Aleksey Makarov | b077baf | 2017-03-15 13:28:50 +0300 | [diff] [blame] | 2651 | static bool has_preferred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2652 | |
Andy Shevchenko | caa72c3 | 2020-02-03 15:31:25 +0200 | [diff] [blame] | 2653 | 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ßmann | 16cf48a | 2013-08-02 12:23:34 +0200 | [diff] [blame] | 2658 | |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2659 | /* |
| 2660 | * before we register a new CON_BOOT console, make sure we don't |
| 2661 | * already have a valid console |
| 2662 | */ |
Andy Shevchenko | caa72c3 | 2020-02-03 15:31:25 +0200 | [diff] [blame] | 2663 | if (newcon->flags & CON_BOOT) { |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2664 | for_each_console(bcon) { |
| 2665 | if (!(bcon->flags & CON_BOOT)) { |
Andrew Morton | 27083ba | 2013-11-12 15:08:50 -0800 | [diff] [blame] | 2666 | pr_info("Too late to register bootconsole %s%d\n", |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2667 | newcon->name, newcon->index); |
| 2668 | return; |
| 2669 | } |
| 2670 | } |
Gerd Hoffmann | 69331af | 2007-05-08 00:26:49 -0700 | [diff] [blame] | 2671 | } |
| 2672 | |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2673 | if (console_drivers && console_drivers->flags & CON_BOOT) |
| 2674 | bcon = console_drivers; |
| 2675 | |
Aleksey Makarov | b077baf | 2017-03-15 13:28:50 +0300 | [diff] [blame] | 2676 | if (!has_preferred || bcon || !console_drivers) |
Aleksey Makarov | ad86ee2 | 2017-03-15 13:28:51 +0300 | [diff] [blame] | 2677 | has_preferred = preferred_console >= 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2678 | |
| 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 Makarov | b077baf | 2017-03-15 13:28:50 +0300 | [diff] [blame] | 2684 | if (!has_preferred) { |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2685 | 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 Makarov | b077baf | 2017-03-15 13:28:50 +0300 | [diff] [blame] | 2692 | has_preferred = true; |
Jan Kiszka | cd3a1b8 | 2008-05-12 21:21:04 +0200 | [diff] [blame] | 2693 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2694 | } |
| 2695 | } |
| 2696 | |
| 2697 | /* |
Petr Mladek | dac8bbb | 2017-06-08 12:01:30 +0200 | [diff] [blame] | 2698 | * See if this console matches one we selected on |
| 2699 | * the command line. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2700 | */ |
Petr Mladek | dac8bbb | 2017-06-08 12:01:30 +0200 | [diff] [blame] | 2701 | for (i = 0, c = console_cmdline; |
| 2702 | i < MAX_CMDLINECONSOLES && c->name[0]; |
| 2703 | i++, c++) { |
Peter Hurley | c7cef0a | 2015-03-09 16:27:12 -0400 | [diff] [blame] | 2704 | 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 Perches | bbeddf5 | 2013-07-31 13:53:45 -0700 | [diff] [blame] | 2715 | |
Peter Hurley | c7cef0a | 2015-03-09 16:27:12 -0400 | [diff] [blame] | 2716 | if (_braille_register_console(newcon, c)) |
| 2717 | return; |
Joe Perches | bbeddf5 | 2013-07-31 13:53:45 -0700 | [diff] [blame] | 2718 | |
Peter Hurley | c7cef0a | 2015-03-09 16:27:12 -0400 | [diff] [blame] | 2719 | if (newcon->setup && |
| 2720 | newcon->setup(newcon, c->options) != 0) |
| 2721 | break; |
| 2722 | } |
| 2723 | |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2724 | newcon->flags |= CON_ENABLED; |
Aleksey Makarov | ad86ee2 | 2017-03-15 13:28:51 +0300 | [diff] [blame] | 2725 | if (i == preferred_console) { |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2726 | newcon->flags |= CON_CONSDEV; |
Aleksey Makarov | b077baf | 2017-03-15 13:28:50 +0300 | [diff] [blame] | 2727 | has_preferred = true; |
Greg Edwards | ab4af03 | 2005-06-23 00:09:05 -0700 | [diff] [blame] | 2728 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2729 | break; |
| 2730 | } |
| 2731 | |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2732 | if (!(newcon->flags & CON_ENABLED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2733 | return; |
| 2734 | |
Robin Getz | 8259cf4 | 2009-07-09 13:08:37 -0400 | [diff] [blame] | 2735 | /* |
| 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 Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2742 | newcon->flags &= ~CON_PRINTBUFFER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2743 | |
| 2744 | /* |
| 2745 | * Put this console in the list - keep the |
| 2746 | * preferred driver at the head of the list. |
| 2747 | */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2748 | console_lock(); |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2749 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2754 | } else { |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2755 | newcon->next = console_drivers->next; |
| 2756 | console_drivers->next = newcon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2757 | } |
Tejun Heo | 6fe2935 | 2015-06-25 15:01:30 -0700 | [diff] [blame] | 2758 | |
| 2759 | if (newcon->flags & CON_EXTENDED) |
Sergey Senozhatsky | 9627808 | 2018-10-02 11:38:34 +0900 | [diff] [blame] | 2760 | nr_ext_console_drivers++; |
Tejun Heo | 6fe2935 | 2015-06-25 15:01:30 -0700 | [diff] [blame] | 2761 | |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2762 | if (newcon->flags & CON_PRINTBUFFER) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2763 | /* |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2764 | * console_unlock(); will print out the buffered messages |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2765 | * for us. |
| 2766 | */ |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 2767 | logbuf_lock_irqsave(flags); |
Feng Tang | fe3d8ad | 2011-03-22 16:34:21 -0700 | [diff] [blame] | 2768 | /* |
| 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 Senozhatsky | 884e370 | 2018-09-28 18:53:04 +0900 | [diff] [blame] | 2772 | * |
| 2773 | * Set exclusive_console with disabled interrupts to reduce |
| 2774 | * race window with eventual console_flush_on_panic() that |
| 2775 | * ignores console_lock. |
Feng Tang | fe3d8ad | 2011-03-22 16:34:21 -0700 | [diff] [blame] | 2776 | */ |
| 2777 | exclusive_console = newcon; |
Petr Mladek | f92b070 | 2018-09-13 14:34:06 +0200 | [diff] [blame] | 2778 | exclusive_console_stop_seq = console_seq; |
John Ogness | def97da | 2019-12-19 12:59:22 +0106 | [diff] [blame] | 2779 | console_seq = syslog_seq; |
| 2780 | console_idx = syslog_idx; |
Sergey Senozhatsky | 884e370 | 2018-09-28 18:53:04 +0900 | [diff] [blame] | 2781 | logbuf_unlock_irqrestore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2782 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2783 | console_unlock(); |
Kay Sievers | fbc92a3 | 2010-12-01 18:51:05 +0100 | [diff] [blame] | 2784 | console_sysfs_notify(); |
Robin Getz | 8259cf4 | 2009-07-09 13:08:37 -0400 | [diff] [blame] | 2785 | |
| 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 Morton | 27083ba | 2013-11-12 15:08:50 -0800 | [diff] [blame] | 2793 | pr_info("%sconsole [%s%d] enabled\n", |
Kees Cook | 6b80239 | 2013-11-12 15:08:49 -0800 | [diff] [blame] | 2794 | (newcon->flags & CON_BOOT) ? "boot" : "" , |
| 2795 | newcon->name, newcon->index); |
Fabio M. Di Nitto | 7bf6939 | 2011-03-22 16:34:20 -0700 | [diff] [blame] | 2796 | if (bcon && |
| 2797 | ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) && |
| 2798 | !keep_bootcon) { |
Kees Cook | 6b80239 | 2013-11-12 15:08:49 -0800 | [diff] [blame] | 2799 | /* We need to iterate through all boot consoles, to make |
| 2800 | * sure we print everything out, before we unregister them. |
Robin Getz | 8259cf4 | 2009-07-09 13:08:37 -0400 | [diff] [blame] | 2801 | */ |
Robin Getz | 8259cf4 | 2009-07-09 13:08:37 -0400 | [diff] [blame] | 2802 | for_each_console(bcon) |
| 2803 | if (bcon->flags & CON_BOOT) |
| 2804 | unregister_console(bcon); |
Robin Getz | 8259cf4 | 2009-07-09 13:08:37 -0400 | [diff] [blame] | 2805 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2806 | } |
| 2807 | EXPORT_SYMBOL(register_console); |
| 2808 | |
Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 2809 | int unregister_console(struct console *console) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2810 | { |
Andy Shevchenko | 12825e6 | 2020-02-03 15:31:26 +0200 | [diff] [blame] | 2811 | struct console *con; |
Joe Perches | bbeddf5 | 2013-07-31 13:53:45 -0700 | [diff] [blame] | 2812 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2813 | |
Andrew Morton | 27083ba | 2013-11-12 15:08:50 -0800 | [diff] [blame] | 2814 | pr_info("%sconsole [%s%d] disabled\n", |
Kees Cook | 6b80239 | 2013-11-12 15:08:49 -0800 | [diff] [blame] | 2815 | (console->flags & CON_BOOT) ? "boot" : "" , |
| 2816 | console->name, console->index); |
| 2817 | |
Joe Perches | bbeddf5 | 2013-07-31 13:53:45 -0700 | [diff] [blame] | 2818 | res = _braille_unregister_console(console); |
Andy Shevchenko | bb72e39 | 2020-02-03 15:31:28 +0200 | [diff] [blame^] | 2819 | if (res < 0) |
Joe Perches | bbeddf5 | 2013-07-31 13:53:45 -0700 | [diff] [blame] | 2820 | return res; |
Andy Shevchenko | bb72e39 | 2020-02-03 15:31:28 +0200 | [diff] [blame^] | 2821 | if (res > 0) |
| 2822 | return 0; |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 2823 | |
Andy Shevchenko | bb72e39 | 2020-02-03 15:31:28 +0200 | [diff] [blame^] | 2824 | res = -ENODEV; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2825 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2826 | if (console_drivers == console) { |
| 2827 | console_drivers=console->next; |
| 2828 | res = 0; |
Andy Shevchenko | 12825e6 | 2020-02-03 15:31:26 +0200 | [diff] [blame] | 2829 | } else { |
| 2830 | for_each_console(con) { |
| 2831 | if (con->next == console) { |
| 2832 | con->next = console->next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2833 | res = 0; |
| 2834 | break; |
Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 2835 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2836 | } |
| 2837 | } |
Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 2838 | |
Tejun Heo | 6fe2935 | 2015-06-25 15:01:30 -0700 | [diff] [blame] | 2839 | if (!res && (console->flags & CON_EXTENDED)) |
| 2840 | nr_ext_console_drivers--; |
| 2841 | |
Gerd Hoffmann | 69331af | 2007-05-08 00:26:49 -0700 | [diff] [blame] | 2842 | /* |
Greg Edwards | ab4af03 | 2005-06-23 00:09:05 -0700 | [diff] [blame] | 2843 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2845 | */ |
Gerd Hoffmann | 69331af | 2007-05-08 00:26:49 -0700 | [diff] [blame] | 2846 | if (console_drivers != NULL && console->flags & CON_CONSDEV) |
Greg Edwards | ab4af03 | 2005-06-23 00:09:05 -0700 | [diff] [blame] | 2847 | console_drivers->flags |= CON_CONSDEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2848 | |
Stephen Chivers | 7fa21dd | 2014-05-14 08:04:39 +1000 | [diff] [blame] | 2849 | console->flags &= ~CON_ENABLED; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2850 | console_unlock(); |
Kay Sievers | fbc92a3 | 2010-12-01 18:51:05 +0100 | [diff] [blame] | 2851 | console_sysfs_notify(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2852 | return res; |
| 2853 | } |
| 2854 | EXPORT_SYMBOL(unregister_console); |
Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 2855 | |
Thierry Reding | 81cc26f | 2016-01-15 16:58:21 -0800 | [diff] [blame] | 2856 | /* |
Nicolas Pitre | 0c68861 | 2017-04-12 18:37:14 -0400 | [diff] [blame] | 2857 | * 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 | */ |
| 2862 | void __init console_init(void) |
| 2863 | { |
Abderrahmane Benbachir | 58eacff | 2018-03-22 20:33:28 -0400 | [diff] [blame] | 2864 | int ret; |
Ard Biesheuvel | 1b1eeca | 2018-08-21 21:56:13 -0700 | [diff] [blame] | 2865 | initcall_t call; |
| 2866 | initcall_entry_t *ce; |
Nicolas Pitre | 0c68861 | 2017-04-12 18:37:14 -0400 | [diff] [blame] | 2867 | |
| 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 Biesheuvel | 1b1eeca | 2018-08-21 21:56:13 -0700 | [diff] [blame] | 2875 | ce = __con_initcall_start; |
Abderrahmane Benbachir | 58eacff | 2018-03-22 20:33:28 -0400 | [diff] [blame] | 2876 | trace_initcall_level("console"); |
Ard Biesheuvel | 1b1eeca | 2018-08-21 21:56:13 -0700 | [diff] [blame] | 2877 | 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 Pitre | 0c68861 | 2017-04-12 18:37:14 -0400 | [diff] [blame] | 2883 | } |
| 2884 | } |
| 2885 | |
| 2886 | /* |
Thierry Reding | 81cc26f | 2016-01-15 16:58:21 -0800 | [diff] [blame] | 2887 | * 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 Redfearn | 2b1be68 | 2017-07-14 14:51:12 +0200 | [diff] [blame] | 2897 | * intersects with the init section. Note that all other boot consoles will |
| 2898 | * get unregistred when the real preferred console is registered. |
Thierry Reding | 81cc26f | 2016-01-15 16:58:21 -0800 | [diff] [blame] | 2899 | */ |
Kevin Cernekee | 034260d | 2010-06-03 22:11:25 -0700 | [diff] [blame] | 2900 | static int __init printk_late_init(void) |
Robin Getz | 0c5564b | 2007-08-20 15:22:47 -0400 | [diff] [blame] | 2901 | { |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2902 | struct console *con; |
Sebastian Andrzej Siewior | 90b1488 | 2016-11-03 15:49:58 +0100 | [diff] [blame] | 2903 | int ret; |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2904 | |
| 2905 | for_each_console(con) { |
Petr Mladek | 5a81423 | 2017-07-14 14:51:13 +0200 | [diff] [blame] | 2906 | 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 Reding | 81cc26f | 2016-01-15 16:58:21 -0800 | [diff] [blame] | 2916 | /* |
Matt Redfearn | 2b1be68 | 2017-07-14 14:51:12 +0200 | [diff] [blame] | 2917 | * Please, consider moving the reported consoles out |
| 2918 | * of the init section. |
Thierry Reding | 81cc26f | 2016-01-15 16:58:21 -0800 | [diff] [blame] | 2919 | */ |
Matt Redfearn | 2b1be68 | 2017-07-14 14:51:12 +0200 | [diff] [blame] | 2920 | 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 Getz | cb00e99 | 2007-08-21 23:14:58 -0400 | [diff] [blame] | 2923 | } |
Robin Getz | 0c5564b | 2007-08-20 15:22:47 -0400 | [diff] [blame] | 2924 | } |
Sebastian Andrzej Siewior | 90b1488 | 2016-11-03 15:49:58 +0100 | [diff] [blame] | 2925 | 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 Getz | 0c5564b | 2007-08-20 15:22:47 -0400 | [diff] [blame] | 2931 | return 0; |
| 2932 | } |
Kevin Cernekee | 034260d | 2010-06-03 22:11:25 -0700 | [diff] [blame] | 2933 | late_initcall(printk_late_init); |
Robin Getz | 0c5564b | 2007-08-20 15:22:47 -0400 | [diff] [blame] | 2934 | |
Joe Perches | 7ef3d2f | 2008-02-08 04:21:25 -0800 | [diff] [blame] | 2935 | #if defined CONFIG_PRINTK |
Frederic Weisbecker | dc72c32 | 2013-03-22 15:04:39 -0700 | [diff] [blame] | 2936 | /* |
| 2937 | * Delayed printk version, for scheduler-internal messages: |
| 2938 | */ |
Frederic Weisbecker | dc72c32 | 2013-03-22 15:04:39 -0700 | [diff] [blame] | 2939 | #define PRINTK_PENDING_WAKEUP 0x01 |
Steven Rostedt | 458df9f | 2014-06-04 16:11:38 -0700 | [diff] [blame] | 2940 | #define PRINTK_PENDING_OUTPUT 0x02 |
Frederic Weisbecker | dc72c32 | 2013-03-22 15:04:39 -0700 | [diff] [blame] | 2941 | |
| 2942 | static DEFINE_PER_CPU(int, printk_pending); |
Frederic Weisbecker | dc72c32 | 2013-03-22 15:04:39 -0700 | [diff] [blame] | 2943 | |
| 2944 | static void wake_up_klogd_work_func(struct irq_work *irq_work) |
| 2945 | { |
| 2946 | int pending = __this_cpu_xchg(printk_pending, 0); |
| 2947 | |
Steven Rostedt | 458df9f | 2014-06-04 16:11:38 -0700 | [diff] [blame] | 2948 | if (pending & PRINTK_PENDING_OUTPUT) { |
| 2949 | /* If trylock fails, someone else is doing the printing */ |
| 2950 | if (console_trylock()) |
| 2951 | console_unlock(); |
Frederic Weisbecker | dc72c32 | 2013-03-22 15:04:39 -0700 | [diff] [blame] | 2952 | } |
| 2953 | |
| 2954 | if (pending & PRINTK_PENDING_WAKEUP) |
| 2955 | wake_up_interruptible(&log_wait); |
| 2956 | } |
| 2957 | |
| 2958 | static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) = { |
| 2959 | .func = wake_up_klogd_work_func, |
Frederic Weisbecker | 153bedb | 2019-11-08 17:08:55 +0100 | [diff] [blame] | 2960 | .flags = ATOMIC_INIT(IRQ_WORK_LAZY), |
Frederic Weisbecker | dc72c32 | 2013-03-22 15:04:39 -0700 | [diff] [blame] | 2961 | }; |
| 2962 | |
| 2963 | void wake_up_klogd(void) |
| 2964 | { |
| 2965 | preempt_disable(); |
| 2966 | if (waitqueue_active(&log_wait)) { |
| 2967 | this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP); |
Christoph Lameter | bb964a9 | 2014-08-17 12:30:24 -0500 | [diff] [blame] | 2968 | irq_work_queue(this_cpu_ptr(&wake_up_klogd_work)); |
Frederic Weisbecker | dc72c32 | 2013-03-22 15:04:39 -0700 | [diff] [blame] | 2969 | } |
| 2970 | preempt_enable(); |
| 2971 | } |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 2972 | |
Petr Mladek | a338f84 | 2018-06-27 16:08:16 +0200 | [diff] [blame] | 2973 | void 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 Mladek | 719f6a7 | 2017-04-20 10:52:31 +0200 | [diff] [blame] | 2981 | int 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 Mladek | a338f84 | 2018-06-27 16:08:16 +0200 | [diff] [blame] | 2986 | defer_console_output(); |
Petr Mladek | 719f6a7 | 2017-04-20 10:52:31 +0200 | [diff] [blame] | 2987 | |
| 2988 | return r; |
| 2989 | } |
| 2990 | |
John Stultz | aac74dc | 2014-06-04 16:11:40 -0700 | [diff] [blame] | 2991 | int printk_deferred(const char *fmt, ...) |
Peter Zijlstra | 600e145 | 2012-03-15 12:35:37 +0100 | [diff] [blame] | 2992 | { |
Peter Zijlstra | 600e145 | 2012-03-15 12:35:37 +0100 | [diff] [blame] | 2993 | va_list args; |
Peter Zijlstra | 600e145 | 2012-03-15 12:35:37 +0100 | [diff] [blame] | 2994 | int r; |
| 2995 | |
Peter Zijlstra | 600e145 | 2012-03-15 12:35:37 +0100 | [diff] [blame] | 2996 | va_start(args, fmt); |
Petr Mladek | 719f6a7 | 2017-04-20 10:52:31 +0200 | [diff] [blame] | 2997 | r = vprintk_deferred(fmt, args); |
Peter Zijlstra | 600e145 | 2012-03-15 12:35:37 +0100 | [diff] [blame] | 2998 | va_end(args); |
| 2999 | |
Peter Zijlstra | 600e145 | 2012-03-15 12:35:37 +0100 | [diff] [blame] | 3000 | return r; |
| 3001 | } |
| 3002 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3003 | /* |
| 3004 | * printk rate limiting, lifted from the networking subsystem. |
| 3005 | * |
Uwe Kleine-König | 641de9d | 2008-07-29 22:33:38 -0700 | [diff] [blame] | 3006 | * This enforces a rate limit: not more than 10 kernel messages |
| 3007 | * every 5s to make a denial-of-service attack impossible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3008 | */ |
Uwe Kleine-König | 641de9d | 2008-07-29 22:33:38 -0700 | [diff] [blame] | 3009 | DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10); |
| 3010 | |
Christian Borntraeger | 5c82871 | 2009-10-23 14:58:11 +0200 | [diff] [blame] | 3011 | int __printk_ratelimit(const char *func) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3012 | { |
Christian Borntraeger | 5c82871 | 2009-10-23 14:58:11 +0200 | [diff] [blame] | 3013 | return ___ratelimit(&printk_ratelimit_state, func); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3014 | } |
Christian Borntraeger | 5c82871 | 2009-10-23 14:58:11 +0200 | [diff] [blame] | 3015 | EXPORT_SYMBOL(__printk_ratelimit); |
Andrew Morton | f46c483 | 2006-11-02 22:07:16 -0800 | [diff] [blame] | 3016 | |
| 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 | */ |
| 3026 | bool printk_timed_ratelimit(unsigned long *caller_jiffies, |
| 3027 | unsigned int interval_msecs) |
| 3028 | { |
Alex Elder | 249771b | 2014-08-06 16:09:08 -0700 | [diff] [blame] | 3029 | 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 Morton | f46c483 | 2006-11-02 22:07:16 -0800 | [diff] [blame] | 3036 | } |
| 3037 | EXPORT_SYMBOL(printk_timed_ratelimit); |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 3038 | |
| 3039 | static DEFINE_SPINLOCK(dump_list_lock); |
| 3040 | static LIST_HEAD(dump_list); |
| 3041 | |
| 3042 | /** |
| 3043 | * kmsg_dump_register - register a kernel log dumper. |
Randy Dunlap | 6485536 | 2009-12-17 15:27:27 -0800 | [diff] [blame] | 3044 | * @dumper: pointer to the kmsg_dumper structure |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 3045 | * |
| 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 | */ |
| 3050 | int 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 Ying | fb842b00 | 2011-01-12 16:59:43 -0800 | [diff] [blame] | 3063 | list_add_tail_rcu(&dumper->list, &dump_list); |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 3064 | err = 0; |
| 3065 | } |
| 3066 | spin_unlock_irqrestore(&dump_list_lock, flags); |
| 3067 | |
| 3068 | return err; |
| 3069 | } |
| 3070 | EXPORT_SYMBOL_GPL(kmsg_dump_register); |
| 3071 | |
| 3072 | /** |
| 3073 | * kmsg_dump_unregister - unregister a kmsg dumper. |
Randy Dunlap | 6485536 | 2009-12-17 15:27:27 -0800 | [diff] [blame] | 3074 | * @dumper: pointer to the kmsg_dumper structure |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 3075 | * |
| 3076 | * Removes a dump device from the system. Returns zero on success and |
| 3077 | * %-EINVAL otherwise. |
| 3078 | */ |
| 3079 | int 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 Ying | fb842b00 | 2011-01-12 16:59:43 -0800 | [diff] [blame] | 3087 | list_del_rcu(&dumper->list); |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 3088 | err = 0; |
| 3089 | } |
| 3090 | spin_unlock_irqrestore(&dump_list_lock, flags); |
Huang Ying | fb842b00 | 2011-01-12 16:59:43 -0800 | [diff] [blame] | 3091 | synchronize_rcu(); |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 3092 | |
| 3093 | return err; |
| 3094 | } |
| 3095 | EXPORT_SYMBOL_GPL(kmsg_dump_unregister); |
| 3096 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 3097 | static bool always_kmsg_dump; |
| 3098 | module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR); |
| 3099 | |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 3100 | /** |
| 3101 | * kmsg_dump - dump kernel log to kernel message dumpers. |
| 3102 | * @reason: the reason (oops, panic etc) for dumping |
| 3103 | * |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3104 | * 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 Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 3107 | */ |
| 3108 | void kmsg_dump(enum kmsg_dump_reason reason) |
| 3109 | { |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 3110 | struct kmsg_dumper *dumper; |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 3111 | unsigned long flags; |
| 3112 | |
Matthew Garrett | c22ab332 | 2012-03-05 14:59:10 -0800 | [diff] [blame] | 3113 | if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump) |
| 3114 | return; |
| 3115 | |
Huang Ying | fb842b00 | 2011-01-12 16:59:43 -0800 | [diff] [blame] | 3116 | rcu_read_lock(); |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3117 | 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 Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 3124 | logbuf_lock_irqsave(flags); |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3125 | 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 Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 3129 | logbuf_unlock_irqrestore(flags); |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3130 | |
| 3131 | /* invoke dumper which will iterate over records */ |
| 3132 | dumper->dump(dumper, reason); |
| 3133 | |
| 3134 | /* reset iterator */ |
| 3135 | dumper->active = false; |
| 3136 | } |
Huang Ying | fb842b00 | 2011-01-12 16:59:43 -0800 | [diff] [blame] | 3137 | rcu_read_unlock(); |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 3138 | } |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3139 | |
| 3140 | /** |
Anton Vorontsov | 533827c | 2012-07-20 17:28:07 -0700 | [diff] [blame] | 3141 | * 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 | */ |
| 3159 | bool kmsg_dump_get_line_nolock(struct kmsg_dumper *dumper, bool syslog, |
| 3160 | char *line, size_t size, size_t *len) |
| 3161 | { |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 3162 | struct printk_log *msg; |
Anton Vorontsov | 533827c | 2012-07-20 17:28:07 -0700 | [diff] [blame] | 3163 | 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 Handa | e80c1a9 | 2018-12-04 19:00:01 +0900 | [diff] [blame] | 3180 | l = msg_print_text(msg, syslog, printk_time, line, size); |
Anton Vorontsov | 533827c | 2012-07-20 17:28:07 -0700 | [diff] [blame] | 3181 | |
| 3182 | dumper->cur_idx = log_next(dumper->cur_idx); |
| 3183 | dumper->cur_seq++; |
| 3184 | ret = true; |
| 3185 | out: |
| 3186 | if (len) |
| 3187 | *len = l; |
| 3188 | return ret; |
| 3189 | } |
| 3190 | |
| 3191 | /** |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3192 | * 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 | */ |
| 3208 | bool 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 Vorontsov | 533827c | 2012-07-20 17:28:07 -0700 | [diff] [blame] | 3212 | bool ret; |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3213 | |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 3214 | logbuf_lock_irqsave(flags); |
Anton Vorontsov | 533827c | 2012-07-20 17:28:07 -0700 | [diff] [blame] | 3215 | ret = kmsg_dump_get_line_nolock(dumper, syslog, line, size, len); |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 3216 | logbuf_unlock_irqrestore(flags); |
Anton Vorontsov | 533827c | 2012-07-20 17:28:07 -0700 | [diff] [blame] | 3217 | |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3218 | return ret; |
| 3219 | } |
| 3220 | EXPORT_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 Dunlap | 4f0f4af | 2012-06-30 15:37:24 -0700 | [diff] [blame] | 3226 | * @buf: buffer to copy the line to |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3227 | * @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 | */ |
| 3241 | bool 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 Handa | e80c1a9 | 2018-12-04 19:00:01 +0900 | [diff] [blame] | 3251 | bool time = printk_time; |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3252 | |
| 3253 | if (!dumper->active) |
| 3254 | goto out; |
| 3255 | |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 3256 | logbuf_lock_irqsave(flags); |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3257 | 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 Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 3265 | logbuf_unlock_irqrestore(flags); |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3266 | 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 Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 3273 | struct printk_log *msg = log_from_idx(idx); |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3274 | |
Tetsuo Handa | e80c1a9 | 2018-12-04 19:00:01 +0900 | [diff] [blame] | 3275 | l += msg_print_text(msg, true, time, NULL, 0); |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3276 | 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 Whitchurch | c9dccac | 2019-07-11 16:29:37 +0200 | [diff] [blame] | 3283 | while (l >= size && seq < dumper->next_seq) { |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 3284 | struct printk_log *msg = log_from_idx(idx); |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3285 | |
Tetsuo Handa | e80c1a9 | 2018-12-04 19:00:01 +0900 | [diff] [blame] | 3286 | l -= msg_print_text(msg, true, time, NULL, 0); |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3287 | 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 Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 3297 | struct printk_log *msg = log_from_idx(idx); |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3298 | |
Tetsuo Handa | e80c1a9 | 2018-12-04 19:00:01 +0900 | [diff] [blame] | 3299 | l += msg_print_text(msg, syslog, time, buf + l, size - l); |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3300 | idx = log_next(idx); |
| 3301 | seq++; |
| 3302 | } |
| 3303 | |
| 3304 | dumper->next_seq = next_seq; |
| 3305 | dumper->next_idx = next_idx; |
| 3306 | ret = true; |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 3307 | logbuf_unlock_irqrestore(flags); |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3308 | out: |
| 3309 | if (len) |
| 3310 | *len = l; |
| 3311 | return ret; |
| 3312 | } |
| 3313 | EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer); |
| 3314 | |
| 3315 | /** |
Anton Vorontsov | 533827c | 2012-07-20 17:28:07 -0700 | [diff] [blame] | 3316 | * 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 | */ |
| 3325 | void 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 Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3334 | * 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 | */ |
| 3341 | void kmsg_dump_rewind(struct kmsg_dumper *dumper) |
| 3342 | { |
| 3343 | unsigned long flags; |
| 3344 | |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 3345 | logbuf_lock_irqsave(flags); |
Anton Vorontsov | 533827c | 2012-07-20 17:28:07 -0700 | [diff] [blame] | 3346 | kmsg_dump_rewind_nolock(dumper); |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 3347 | logbuf_unlock_irqrestore(flags); |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3348 | } |
| 3349 | EXPORT_SYMBOL_GPL(kmsg_dump_rewind); |
Tejun Heo | 196779b | 2013-04-30 15:27:12 -0700 | [diff] [blame] | 3350 | |
Joe Perches | 7ef3d2f | 2008-02-08 04:21:25 -0800 | [diff] [blame] | 3351 | #endif |