Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/kernel/printk.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | * |
| 6 | * Modified to make sys_syslog() more flexible: added commands to |
| 7 | * return the last 4k of kernel messages, regardless of whether |
| 8 | * they've been read or not. Added option to suppress kernel printk's |
| 9 | * to the console. Added hook for sending the console messages |
| 10 | * elsewhere, in preparation for a serial line console (someday). |
| 11 | * Ted Ts'o, 2/11/93. |
| 12 | * Modified for sysctl support, 1/8/97, Chris Horn. |
Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 13 | * Fixed SMP synchronization, 08/08/99, Manfred Spraul |
Christian Kujau | 624dffc | 2006-01-15 02:43:54 +0100 | [diff] [blame] | 14 | * manfred@colorfullife.com |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * Rewrote bits to get rid of console_lock |
Francois Cami | e1f8e87 | 2008-10-15 22:01:59 -0700 | [diff] [blame] | 16 | * 01Mar01 Andrew Morton |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/mm.h> |
| 21 | #include <linux/tty.h> |
| 22 | #include <linux/tty_driver.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/console.h> |
| 24 | #include <linux/init.h> |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 25 | #include <linux/jiffies.h> |
| 26 | #include <linux/nmi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/module.h> |
Jan Engelhardt | 3b9c041 | 2006-06-25 05:48:15 -0700 | [diff] [blame] | 28 | #include <linux/moduleparam.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/delay.h> |
| 30 | #include <linux/smp.h> |
| 31 | #include <linux/security.h> |
| 32 | #include <linux/bootmem.h> |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 33 | #include <linux/memblock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/syscalls.h> |
Hari Bathini | 692f66f | 2017-05-08 15:56:18 -0700 | [diff] [blame] | 35 | #include <linux/crash_core.h> |
Jason Wessel | d37d39a | 2010-05-20 21:04:27 -0500 | [diff] [blame] | 36 | #include <linux/kdb.h> |
Ingo Molnar | 3fff4c4 | 2009-09-22 16:18:09 +0200 | [diff] [blame] | 37 | #include <linux/ratelimit.h> |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 38 | #include <linux/kmsg_dump.h> |
Kees Cook | 0023459 | 2010-02-03 15:36:43 -0800 | [diff] [blame] | 39 | #include <linux/syslog.h> |
Kevin Cernekee | 034260d | 2010-06-03 22:11:25 -0700 | [diff] [blame] | 40 | #include <linux/cpu.h> |
| 41 | #include <linux/notifier.h> |
Huang Ying | fb842b00 | 2011-01-12 16:59:43 -0800 | [diff] [blame] | 42 | #include <linux/rculist.h> |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 43 | #include <linux/poll.h> |
Frederic Weisbecker | 74876a9 | 2012-10-12 18:00:23 +0200 | [diff] [blame] | 44 | #include <linux/irq_work.h> |
Tejun Heo | 196779b | 2013-04-30 15:27:12 -0700 | [diff] [blame] | 45 | #include <linux/utsname.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 | |
Johannes Berg | 9510035 | 2011-11-24 20:03:08 +0100 | [diff] [blame] | 55 | #define CREATE_TRACE_POINTS |
| 56 | #include <trace/events/printk.h> |
| 57 | |
Joe Perches | d197c43 | 2013-07-31 13:53:44 -0700 | [diff] [blame] | 58 | #include "console_cmdline.h" |
Joe Perches | bbeddf5 | 2013-07-31 13:53:45 -0700 | [diff] [blame] | 59 | #include "braille.h" |
Petr Mladek | 42a0bb3 | 2016-05-20 17:00:33 -0700 | [diff] [blame] | 60 | #include "internal.h" |
Joe Perches | d197c43 | 2013-07-31 13:53:44 -0700 | [diff] [blame] | 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | int console_printk[4] = { |
Borislav Petkov | a8fe19e | 2014-06-04 16:11:46 -0700 | [diff] [blame] | 63 | CONSOLE_LOGLEVEL_DEFAULT, /* console_loglevel */ |
Alex Elder | 42a9dc0 | 2014-08-06 16:09:01 -0700 | [diff] [blame] | 64 | MESSAGE_LOGLEVEL_DEFAULT, /* default_message_loglevel */ |
Borislav Petkov | a8fe19e | 2014-06-04 16:11:46 -0700 | [diff] [blame] | 65 | CONSOLE_LOGLEVEL_MIN, /* minimum_console_loglevel */ |
| 66 | CONSOLE_LOGLEVEL_DEFAULT, /* default_console_loglevel */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | /* |
Patrick Pletscher | 0bbfb7c | 2007-02-17 20:10:16 +0100 | [diff] [blame] | 70 | * 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] | 71 | * their unblank() callback or not. So let's export it. |
| 72 | */ |
| 73 | int oops_in_progress; |
| 74 | EXPORT_SYMBOL(oops_in_progress); |
| 75 | |
| 76 | /* |
| 77 | * console_sem protects the console_drivers list, and also |
| 78 | * provides serialisation for access to the entire console |
| 79 | * driver system. |
| 80 | */ |
Thomas Gleixner | 5b8c4f2 | 2010-09-07 14:33:43 +0000 | [diff] [blame] | 81 | static DEFINE_SEMAPHORE(console_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | struct console *console_drivers; |
Ingo Molnar | a29d1cf | 2008-06-02 13:19:08 +0200 | [diff] [blame] | 83 | EXPORT_SYMBOL_GPL(console_drivers); |
| 84 | |
Daniel Vetter | daee779 | 2012-09-22 19:52:11 +0200 | [diff] [blame] | 85 | #ifdef CONFIG_LOCKDEP |
| 86 | static struct lockdep_map console_lock_dep_map = { |
| 87 | .name = "console_lock" |
| 88 | }; |
Steven Rostedt (VMware) | dbdda84 | 2018-01-10 14:24:17 +0100 | [diff] [blame^] | 89 | static struct lockdep_map console_owner_dep_map = { |
| 90 | .name = "console_owner" |
| 91 | }; |
Daniel Vetter | daee779 | 2012-09-22 19:52:11 +0200 | [diff] [blame] | 92 | #endif |
| 93 | |
Steven Rostedt (VMware) | dbdda84 | 2018-01-10 14:24:17 +0100 | [diff] [blame^] | 94 | static DEFINE_RAW_SPINLOCK(console_owner_lock); |
| 95 | static struct task_struct *console_owner; |
| 96 | static bool console_waiter; |
| 97 | |
Borislav Petkov | 750afe7 | 2016-08-02 14:04:07 -0700 | [diff] [blame] | 98 | enum devkmsg_log_bits { |
| 99 | __DEVKMSG_LOG_BIT_ON = 0, |
| 100 | __DEVKMSG_LOG_BIT_OFF, |
| 101 | __DEVKMSG_LOG_BIT_LOCK, |
| 102 | }; |
| 103 | |
| 104 | enum devkmsg_log_masks { |
| 105 | DEVKMSG_LOG_MASK_ON = BIT(__DEVKMSG_LOG_BIT_ON), |
| 106 | DEVKMSG_LOG_MASK_OFF = BIT(__DEVKMSG_LOG_BIT_OFF), |
| 107 | DEVKMSG_LOG_MASK_LOCK = BIT(__DEVKMSG_LOG_BIT_LOCK), |
| 108 | }; |
| 109 | |
| 110 | /* Keep both the 'on' and 'off' bits clear, i.e. ratelimit by default: */ |
| 111 | #define DEVKMSG_LOG_MASK_DEFAULT 0 |
| 112 | |
| 113 | static unsigned int __read_mostly devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT; |
| 114 | |
| 115 | static int __control_devkmsg(char *str) |
| 116 | { |
| 117 | if (!str) |
| 118 | return -EINVAL; |
| 119 | |
| 120 | if (!strncmp(str, "on", 2)) { |
| 121 | devkmsg_log = DEVKMSG_LOG_MASK_ON; |
| 122 | return 2; |
| 123 | } else if (!strncmp(str, "off", 3)) { |
| 124 | devkmsg_log = DEVKMSG_LOG_MASK_OFF; |
| 125 | return 3; |
| 126 | } else if (!strncmp(str, "ratelimit", 9)) { |
| 127 | devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT; |
| 128 | return 9; |
| 129 | } |
| 130 | return -EINVAL; |
| 131 | } |
| 132 | |
| 133 | static int __init control_devkmsg(char *str) |
| 134 | { |
| 135 | if (__control_devkmsg(str) < 0) |
| 136 | return 1; |
| 137 | |
| 138 | /* |
| 139 | * Set sysctl string accordingly: |
| 140 | */ |
| 141 | if (devkmsg_log == DEVKMSG_LOG_MASK_ON) { |
| 142 | memset(devkmsg_log_str, 0, DEVKMSG_STR_MAX_SIZE); |
| 143 | strncpy(devkmsg_log_str, "on", 2); |
| 144 | } else if (devkmsg_log == DEVKMSG_LOG_MASK_OFF) { |
| 145 | memset(devkmsg_log_str, 0, DEVKMSG_STR_MAX_SIZE); |
| 146 | strncpy(devkmsg_log_str, "off", 3); |
| 147 | } |
| 148 | /* else "ratelimit" which is set by default. */ |
| 149 | |
| 150 | /* |
| 151 | * Sysctl cannot change it anymore. The kernel command line setting of |
| 152 | * this parameter is to force the setting to be permanent throughout the |
| 153 | * runtime of the system. This is a precation measure against userspace |
| 154 | * trying to be a smarta** and attempting to change it up on us. |
| 155 | */ |
| 156 | devkmsg_log |= DEVKMSG_LOG_MASK_LOCK; |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | __setup("printk.devkmsg=", control_devkmsg); |
| 161 | |
| 162 | char devkmsg_log_str[DEVKMSG_STR_MAX_SIZE] = "ratelimit"; |
| 163 | |
| 164 | int devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write, |
| 165 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 166 | { |
| 167 | char old_str[DEVKMSG_STR_MAX_SIZE]; |
| 168 | unsigned int old; |
| 169 | int err; |
| 170 | |
| 171 | if (write) { |
| 172 | if (devkmsg_log & DEVKMSG_LOG_MASK_LOCK) |
| 173 | return -EINVAL; |
| 174 | |
| 175 | old = devkmsg_log; |
| 176 | strncpy(old_str, devkmsg_log_str, DEVKMSG_STR_MAX_SIZE); |
| 177 | } |
| 178 | |
| 179 | err = proc_dostring(table, write, buffer, lenp, ppos); |
| 180 | if (err) |
| 181 | return err; |
| 182 | |
| 183 | if (write) { |
| 184 | err = __control_devkmsg(devkmsg_log_str); |
| 185 | |
| 186 | /* |
| 187 | * Do not accept an unknown string OR a known string with |
| 188 | * trailing crap... |
| 189 | */ |
| 190 | if (err < 0 || (err + 1 != *lenp)) { |
| 191 | |
| 192 | /* ... and restore old setting. */ |
| 193 | devkmsg_log = old; |
| 194 | strncpy(devkmsg_log_str, old_str, DEVKMSG_STR_MAX_SIZE); |
| 195 | |
| 196 | return -EINVAL; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | /* |
Tejun Heo | 6fe2935 | 2015-06-25 15:01:30 -0700 | [diff] [blame] | 204 | * Number of registered extended console drivers. |
| 205 | * |
| 206 | * If extended consoles are present, in-kernel cont reassembly is disabled |
| 207 | * and each fragment is stored as a separate log entry with proper |
| 208 | * continuation flag so that every emitted message has full metadata. This |
| 209 | * doesn't change the result for regular consoles or /proc/kmsg. For |
| 210 | * /dev/kmsg, as long as the reader concatenates messages according to |
| 211 | * consecutive continuation flags, the end result should be the same too. |
| 212 | */ |
| 213 | static int nr_ext_console_drivers; |
| 214 | |
| 215 | /* |
Jan Kara | bd8d7cf | 2014-06-04 16:11:36 -0700 | [diff] [blame] | 216 | * Helper macros to handle lockdep when locking/unlocking console_sem. We use |
| 217 | * macros instead of functions so that _RET_IP_ contains useful information. |
| 218 | */ |
| 219 | #define down_console_sem() do { \ |
| 220 | down(&console_sem);\ |
| 221 | mutex_acquire(&console_lock_dep_map, 0, 0, _RET_IP_);\ |
| 222 | } while (0) |
| 223 | |
| 224 | static int __down_trylock_console_sem(unsigned long ip) |
| 225 | { |
Sergey Senozhatsky | f975237 | 2016-12-27 23:16:09 +0900 | [diff] [blame] | 226 | int lock_failed; |
| 227 | unsigned long flags; |
| 228 | |
| 229 | /* |
| 230 | * Here and in __up_console_sem() we need to be in safe mode, |
| 231 | * because spindump/WARN/etc from under console ->lock will |
| 232 | * deadlock in printk()->down_trylock_console_sem() otherwise. |
| 233 | */ |
| 234 | printk_safe_enter_irqsave(flags); |
| 235 | lock_failed = down_trylock(&console_sem); |
| 236 | printk_safe_exit_irqrestore(flags); |
| 237 | |
| 238 | if (lock_failed) |
Jan Kara | bd8d7cf | 2014-06-04 16:11:36 -0700 | [diff] [blame] | 239 | return 1; |
| 240 | mutex_acquire(&console_lock_dep_map, 0, 1, ip); |
| 241 | return 0; |
| 242 | } |
| 243 | #define down_trylock_console_sem() __down_trylock_console_sem(_RET_IP_) |
| 244 | |
Sergey Senozhatsky | f975237 | 2016-12-27 23:16:09 +0900 | [diff] [blame] | 245 | static void __up_console_sem(unsigned long ip) |
| 246 | { |
| 247 | unsigned long flags; |
| 248 | |
| 249 | mutex_release(&console_lock_dep_map, 1, ip); |
| 250 | |
| 251 | printk_safe_enter_irqsave(flags); |
| 252 | up(&console_sem); |
| 253 | printk_safe_exit_irqrestore(flags); |
| 254 | } |
| 255 | #define up_console_sem() __up_console_sem(_RET_IP_) |
Jan Kara | bd8d7cf | 2014-06-04 16:11:36 -0700 | [diff] [blame] | 256 | |
| 257 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | * This is used for debugging the mess that is the VT code by |
| 259 | * keeping track if we have the console semaphore held. It's |
| 260 | * definitely not the perfect debug tool (we don't know if _WE_ |
Alex Elder | 0b90fec | 2014-08-06 16:09:03 -0700 | [diff] [blame] | 261 | * hold it and are racing, but it helps tracking those weird code |
| 262 | * paths in the console code where we end up in places I want |
| 263 | * locked without the console sempahore held). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | */ |
Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 265 | static int console_locked, console_suspended; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
| 267 | /* |
Feng Tang | fe3d8ad | 2011-03-22 16:34:21 -0700 | [diff] [blame] | 268 | * If exclusive_console is non-NULL then only this console is to be printed to. |
| 269 | */ |
| 270 | static struct console *exclusive_console; |
| 271 | |
| 272 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | * Array of consoles built from command line options (console=) |
| 274 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | |
| 276 | #define MAX_CMDLINECONSOLES 8 |
| 277 | |
| 278 | static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES]; |
Joe Perches | d197c43 | 2013-07-31 13:53:44 -0700 | [diff] [blame] | 279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | static int preferred_console = -1; |
Markus Armbruster | 9e124fe | 2008-05-26 23:31:07 +0100 | [diff] [blame] | 281 | int console_set_on_cmdline; |
| 282 | EXPORT_SYMBOL(console_set_on_cmdline); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
| 284 | /* Flag: console code may call schedule() */ |
| 285 | static int console_may_schedule; |
| 286 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 287 | /* |
| 288 | * The printk log buffer consists of a chain of concatenated variable |
| 289 | * length records. Every record starts with a record header, containing |
| 290 | * the overall length of the record. |
| 291 | * |
| 292 | * The heads to the first and last entry in the buffer, as well as the |
Alex Elder | 0b90fec | 2014-08-06 16:09:03 -0700 | [diff] [blame] | 293 | * sequence numbers of these entries are maintained when messages are |
| 294 | * stored. |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 295 | * |
| 296 | * If the heads indicate available messages, the length in the header |
| 297 | * tells the start next message. A length == 0 for the next message |
| 298 | * indicates a wrap-around to the beginning of the buffer. |
| 299 | * |
| 300 | * Every record carries the monotonic timestamp in microseconds, as well as |
| 301 | * the standard userspace syslog level and syslog facility. The usual |
| 302 | * kernel messages use LOG_KERN; userspace-injected messages always carry |
| 303 | * a matching syslog facility, by default LOG_USER. The origin of every |
| 304 | * message can be reliably determined that way. |
| 305 | * |
| 306 | * The human readable log message directly follows the message header. The |
| 307 | * length of the message text is stored in the header, the stored message |
| 308 | * is not terminated. |
| 309 | * |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 310 | * Optionally, a message can carry a dictionary of properties (key/value pairs), |
| 311 | * to provide userspace with a machine-readable message context. |
| 312 | * |
| 313 | * Examples for well-defined, commonly used property names are: |
| 314 | * DEVICE=b12:8 device identifier |
| 315 | * b12:8 block dev_t |
| 316 | * c127:3 char dev_t |
| 317 | * n8 netdev ifindex |
| 318 | * +sound:card0 subsystem:devname |
| 319 | * SUBSYSTEM=pci driver-core subsystem name |
| 320 | * |
| 321 | * Valid characters in property names are [a-zA-Z0-9.-_]. The plain text value |
| 322 | * follows directly after a '=' character. Every property is terminated by |
| 323 | * a '\0' character. The last property is not terminated. |
| 324 | * |
| 325 | * Example of a message structure: |
| 326 | * 0000 ff 8f 00 00 00 00 00 00 monotonic time in nsec |
| 327 | * 0008 34 00 record is 52 bytes long |
| 328 | * 000a 0b 00 text is 11 bytes long |
| 329 | * 000c 1f 00 dictionary is 23 bytes long |
| 330 | * 000e 03 00 LOG_KERN (facility) LOG_ERR (level) |
| 331 | * 0010 69 74 27 73 20 61 20 6c "it's a l" |
| 332 | * 69 6e 65 "ine" |
| 333 | * 001b 44 45 56 49 43 "DEVIC" |
| 334 | * 45 3d 62 38 3a 32 00 44 "E=b8:2\0D" |
| 335 | * 52 49 56 45 52 3d 62 75 "RIVER=bu" |
| 336 | * 67 "g" |
| 337 | * 0032 00 00 00 padding to next message header |
| 338 | * |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 339 | * The 'struct printk_log' buffer header must never be directly exported to |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 340 | * userspace, it is a kernel-private implementation detail that might |
| 341 | * need to be changed in the future, when the requirements change. |
| 342 | * |
| 343 | * /dev/kmsg exports the structured data in the following line format: |
Antonio Ospite | b389645 | 2015-06-30 14:59:03 -0700 | [diff] [blame] | 344 | * "<level>,<sequnum>,<timestamp>,<contflag>[,additional_values, ... ];<message text>\n" |
| 345 | * |
| 346 | * Users of the export format should ignore possible additional values |
| 347 | * separated by ',', and find the message after the ';' character. |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 348 | * |
| 349 | * The optional key/value pairs are attached as continuation lines starting |
| 350 | * with a space character and terminated by a newline. All possible |
| 351 | * non-prinatable characters are escaped in the "\xff" notation. |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 352 | */ |
Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 353 | |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 354 | enum log_flags { |
Kay Sievers | 5becfb1 | 2012-07-09 12:15:42 -0700 | [diff] [blame] | 355 | LOG_NOCONS = 1, /* already flushed, do not print to console */ |
| 356 | LOG_NEWLINE = 2, /* text ended with a newline */ |
| 357 | LOG_PREFIX = 4, /* text started with a prefix */ |
| 358 | LOG_CONT = 8, /* text is a fragment of a continuation line */ |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 359 | }; |
| 360 | |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 361 | struct printk_log { |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 362 | u64 ts_nsec; /* timestamp in nanoseconds */ |
| 363 | u16 len; /* length of entire record */ |
| 364 | u16 text_len; /* length of text buffer */ |
| 365 | u16 dict_len; /* length of dictionary buffer */ |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 366 | u8 facility; /* syslog facility */ |
| 367 | u8 flags:5; /* internal record flags */ |
| 368 | u8 level:3; /* syslog level */ |
Andrey Ryabinin | 5c9cf8a | 2016-01-20 15:00:48 -0800 | [diff] [blame] | 369 | } |
| 370 | #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS |
| 371 | __packed __aligned(4) |
| 372 | #endif |
| 373 | ; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 374 | |
| 375 | /* |
Steven Rostedt | 458df9f | 2014-06-04 16:11:38 -0700 | [diff] [blame] | 376 | * The logbuf_lock protects kmsg buffer, indices, counters. This can be taken |
| 377 | * within the scheduler's rq lock. It must be released before calling |
| 378 | * console_unlock() or anything else that might wake up a process. |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 379 | */ |
Petr Mladek | cf9b110 | 2016-05-20 17:00:42 -0700 | [diff] [blame] | 380 | DEFINE_RAW_SPINLOCK(logbuf_lock); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 381 | |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 382 | /* |
| 383 | * Helper macros to lock/unlock logbuf_lock and switch between |
| 384 | * printk-safe/unsafe modes. |
| 385 | */ |
| 386 | #define logbuf_lock_irq() \ |
| 387 | do { \ |
| 388 | printk_safe_enter_irq(); \ |
| 389 | raw_spin_lock(&logbuf_lock); \ |
| 390 | } while (0) |
| 391 | |
| 392 | #define logbuf_unlock_irq() \ |
| 393 | do { \ |
| 394 | raw_spin_unlock(&logbuf_lock); \ |
| 395 | printk_safe_exit_irq(); \ |
| 396 | } while (0) |
| 397 | |
| 398 | #define logbuf_lock_irqsave(flags) \ |
| 399 | do { \ |
| 400 | printk_safe_enter_irqsave(flags); \ |
| 401 | raw_spin_lock(&logbuf_lock); \ |
| 402 | } while (0) |
| 403 | |
| 404 | #define logbuf_unlock_irqrestore(flags) \ |
| 405 | do { \ |
| 406 | raw_spin_unlock(&logbuf_lock); \ |
| 407 | printk_safe_exit_irqrestore(flags); \ |
| 408 | } while (0) |
| 409 | |
Kay Sievers | 96efedf | 2012-07-16 18:35:29 -0700 | [diff] [blame] | 410 | #ifdef CONFIG_PRINTK |
Frederic Weisbecker | dc72c32 | 2013-03-22 15:04:39 -0700 | [diff] [blame] | 411 | DECLARE_WAIT_QUEUE_HEAD(log_wait); |
Kay Sievers | 7f3a781 | 2012-05-09 01:37:51 +0200 | [diff] [blame] | 412 | /* the next printk record to read by syslog(READ) or /proc/kmsg */ |
| 413 | static u64 syslog_seq; |
| 414 | static u32 syslog_idx; |
Kay Sievers | eb02dac | 2012-07-09 10:05:10 -0700 | [diff] [blame] | 415 | static size_t syslog_partial; |
Kay Sievers | 7f3a781 | 2012-05-09 01:37:51 +0200 | [diff] [blame] | 416 | |
| 417 | /* index and sequence number of the first record stored in the buffer */ |
| 418 | static u64 log_first_seq; |
| 419 | static u32 log_first_idx; |
| 420 | |
| 421 | /* index and sequence number of the next record to store in the buffer */ |
| 422 | static u64 log_next_seq; |
Kay Sievers | 7f3a781 | 2012-05-09 01:37:51 +0200 | [diff] [blame] | 423 | static u32 log_next_idx; |
| 424 | |
Kay Sievers | eab0726 | 2012-07-16 18:35:30 -0700 | [diff] [blame] | 425 | /* the next printk record to write to the console */ |
| 426 | static u64 console_seq; |
| 427 | static u32 console_idx; |
Kay Sievers | eab0726 | 2012-07-16 18:35:30 -0700 | [diff] [blame] | 428 | |
Kay Sievers | 7f3a781 | 2012-05-09 01:37:51 +0200 | [diff] [blame] | 429 | /* the next printk record to read after the last 'clear' command */ |
| 430 | static u64 clear_seq; |
| 431 | static u32 clear_idx; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 432 | |
Kay Sievers | 7049825 | 2012-07-16 18:35:29 -0700 | [diff] [blame] | 433 | #define PREFIX_MAX 32 |
Alex Elder | 249771b | 2014-08-06 16:09:08 -0700 | [diff] [blame] | 434 | #define LOG_LINE_MAX (1024 - PREFIX_MAX) |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 435 | |
Mathias Krause | 3824657 | 2015-11-06 16:30:38 -0800 | [diff] [blame] | 436 | #define LOG_LEVEL(v) ((v) & 0x07) |
| 437 | #define LOG_FACILITY(v) ((v) >> 3 & 0xff) |
| 438 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 439 | /* record buffer */ |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 440 | #define LOG_ALIGN __alignof__(struct printk_log) |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 441 | #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT) |
Stephen Warren | f8450fc | 2012-05-10 16:14:33 -0600 | [diff] [blame] | 442 | static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN); |
Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 443 | static char *log_buf = __log_buf; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 444 | static u32 log_buf_len = __LOG_BUF_LEN; |
| 445 | |
Vasant Hegde | 14c4000 | 2014-08-09 11:15:30 +0530 | [diff] [blame] | 446 | /* Return log buffer address */ |
| 447 | char *log_buf_addr_get(void) |
| 448 | { |
| 449 | return log_buf; |
| 450 | } |
| 451 | |
| 452 | /* Return log buffer size */ |
| 453 | u32 log_buf_len_get(void) |
| 454 | { |
| 455 | return log_buf_len; |
| 456 | } |
| 457 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 458 | /* human readable text of the record */ |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 459 | static char *log_text(const struct printk_log *msg) |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 460 | { |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 461 | return (char *)msg + sizeof(struct printk_log); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /* optional key/value pair dictionary attached to the record */ |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 465 | static char *log_dict(const struct printk_log *msg) |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 466 | { |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 467 | return (char *)msg + sizeof(struct printk_log) + msg->text_len; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | /* get record by index; idx must point to valid msg */ |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 471 | static struct printk_log *log_from_idx(u32 idx) |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 472 | { |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 473 | struct printk_log *msg = (struct printk_log *)(log_buf + idx); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 474 | |
| 475 | /* |
| 476 | * A length == 0 record is the end of buffer marker. Wrap around and |
| 477 | * read the message at the start of the buffer. |
| 478 | */ |
| 479 | if (!msg->len) |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 480 | return (struct printk_log *)log_buf; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 481 | return msg; |
| 482 | } |
| 483 | |
| 484 | /* get next record; idx must point to valid msg */ |
| 485 | static u32 log_next(u32 idx) |
| 486 | { |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 487 | struct printk_log *msg = (struct printk_log *)(log_buf + idx); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 488 | |
| 489 | /* length == 0 indicates the end of the buffer; wrap */ |
| 490 | /* |
| 491 | * A length == 0 record is the end of buffer marker. Wrap around and |
| 492 | * read the message at the start of the buffer as *this* one, and |
| 493 | * return the one after that. |
| 494 | */ |
| 495 | if (!msg->len) { |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 496 | msg = (struct printk_log *)log_buf; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 497 | return msg->len; |
| 498 | } |
| 499 | return idx + msg->len; |
| 500 | } |
| 501 | |
Petr Mladek | f40e4b9 | 2014-06-04 16:11:30 -0700 | [diff] [blame] | 502 | /* |
| 503 | * Check whether there is enough free space for the given message. |
| 504 | * |
| 505 | * The same values of first_idx and next_idx mean that the buffer |
| 506 | * is either empty or full. |
| 507 | * |
| 508 | * If the buffer is empty, we must respect the position of the indexes. |
| 509 | * They cannot be reset to the beginning of the buffer. |
| 510 | */ |
| 511 | static int logbuf_has_space(u32 msg_size, bool empty) |
Petr Mladek | 0a58169 | 2014-06-04 16:11:28 -0700 | [diff] [blame] | 512 | { |
| 513 | u32 free; |
| 514 | |
Petr Mladek | f40e4b9 | 2014-06-04 16:11:30 -0700 | [diff] [blame] | 515 | if (log_next_idx > log_first_idx || empty) |
Petr Mladek | 0a58169 | 2014-06-04 16:11:28 -0700 | [diff] [blame] | 516 | free = max(log_buf_len - log_next_idx, log_first_idx); |
| 517 | else |
| 518 | free = log_first_idx - log_next_idx; |
| 519 | |
| 520 | /* |
| 521 | * We need space also for an empty header that signalizes wrapping |
| 522 | * of the buffer. |
| 523 | */ |
| 524 | return free >= msg_size + sizeof(struct printk_log); |
| 525 | } |
| 526 | |
Petr Mladek | f40e4b9 | 2014-06-04 16:11:30 -0700 | [diff] [blame] | 527 | static int log_make_free_space(u32 msg_size) |
Petr Mladek | 0a58169 | 2014-06-04 16:11:28 -0700 | [diff] [blame] | 528 | { |
Ivan Delalande | f468908 | 2016-03-17 14:21:30 -0700 | [diff] [blame] | 529 | while (log_first_seq < log_next_seq && |
| 530 | !logbuf_has_space(msg_size, false)) { |
Alex Elder | 0b90fec | 2014-08-06 16:09:03 -0700 | [diff] [blame] | 531 | /* drop old messages until we have enough contiguous space */ |
Petr Mladek | 0a58169 | 2014-06-04 16:11:28 -0700 | [diff] [blame] | 532 | log_first_idx = log_next(log_first_idx); |
| 533 | log_first_seq++; |
| 534 | } |
Petr Mladek | f40e4b9 | 2014-06-04 16:11:30 -0700 | [diff] [blame] | 535 | |
Ivan Delalande | f468908 | 2016-03-17 14:21:30 -0700 | [diff] [blame] | 536 | if (clear_seq < log_first_seq) { |
| 537 | clear_seq = log_first_seq; |
| 538 | clear_idx = log_first_idx; |
| 539 | } |
| 540 | |
Petr Mladek | f40e4b9 | 2014-06-04 16:11:30 -0700 | [diff] [blame] | 541 | /* sequence numbers are equal, so the log buffer is empty */ |
Ivan Delalande | f468908 | 2016-03-17 14:21:30 -0700 | [diff] [blame] | 542 | if (logbuf_has_space(msg_size, log_first_seq == log_next_seq)) |
Petr Mladek | f40e4b9 | 2014-06-04 16:11:30 -0700 | [diff] [blame] | 543 | return 0; |
| 544 | |
| 545 | return -ENOMEM; |
Petr Mladek | 0a58169 | 2014-06-04 16:11:28 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Petr Mladek | 85c87043 | 2014-06-04 16:11:31 -0700 | [diff] [blame] | 548 | /* compute the message size including the padding bytes */ |
| 549 | static u32 msg_used_size(u16 text_len, u16 dict_len, u32 *pad_len) |
| 550 | { |
| 551 | u32 size; |
| 552 | |
| 553 | size = sizeof(struct printk_log) + text_len + dict_len; |
| 554 | *pad_len = (-size) & (LOG_ALIGN - 1); |
| 555 | size += *pad_len; |
| 556 | |
| 557 | return size; |
| 558 | } |
| 559 | |
Petr Mladek | 55bd53a | 2014-06-04 16:11:32 -0700 | [diff] [blame] | 560 | /* |
| 561 | * Define how much of the log buffer we could take at maximum. The value |
| 562 | * must be greater than two. Note that only half of the buffer is available |
| 563 | * when the index points to the middle. |
| 564 | */ |
| 565 | #define MAX_LOG_TAKE_PART 4 |
| 566 | static const char trunc_msg[] = "<truncated>"; |
| 567 | |
| 568 | static u32 truncate_msg(u16 *text_len, u16 *trunc_msg_len, |
| 569 | u16 *dict_len, u32 *pad_len) |
| 570 | { |
| 571 | /* |
| 572 | * The message should not take the whole buffer. Otherwise, it might |
| 573 | * get removed too soon. |
| 574 | */ |
| 575 | u32 max_text_len = log_buf_len / MAX_LOG_TAKE_PART; |
| 576 | if (*text_len > max_text_len) |
| 577 | *text_len = max_text_len; |
| 578 | /* enable the warning message */ |
| 579 | *trunc_msg_len = strlen(trunc_msg); |
| 580 | /* disable the "dict" completely */ |
| 581 | *dict_len = 0; |
| 582 | /* compute the size again, count also the warning message */ |
| 583 | return msg_used_size(*text_len + *trunc_msg_len, 0, pad_len); |
| 584 | } |
| 585 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 586 | /* insert record into the buffer, discard old ones, update heads */ |
Petr Mladek | 034633c | 2014-06-04 16:11:33 -0700 | [diff] [blame] | 587 | static int log_store(int facility, int level, |
| 588 | enum log_flags flags, u64 ts_nsec, |
| 589 | const char *dict, u16 dict_len, |
| 590 | const char *text, u16 text_len) |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 591 | { |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 592 | struct printk_log *msg; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 593 | u32 size, pad_len; |
Petr Mladek | 55bd53a | 2014-06-04 16:11:32 -0700 | [diff] [blame] | 594 | u16 trunc_msg_len = 0; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 595 | |
| 596 | /* number of '\0' padding bytes to next message */ |
Petr Mladek | 85c87043 | 2014-06-04 16:11:31 -0700 | [diff] [blame] | 597 | size = msg_used_size(text_len, dict_len, &pad_len); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 598 | |
Petr Mladek | 55bd53a | 2014-06-04 16:11:32 -0700 | [diff] [blame] | 599 | if (log_make_free_space(size)) { |
| 600 | /* truncate the message if it is too long for empty buffer */ |
| 601 | size = truncate_msg(&text_len, &trunc_msg_len, |
| 602 | &dict_len, &pad_len); |
| 603 | /* survive when the log buffer is too small for trunc_msg */ |
| 604 | if (log_make_free_space(size)) |
Petr Mladek | 034633c | 2014-06-04 16:11:33 -0700 | [diff] [blame] | 605 | return 0; |
Petr Mladek | 55bd53a | 2014-06-04 16:11:32 -0700 | [diff] [blame] | 606 | } |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 607 | |
Petr Mladek | 39b2510 | 2014-04-03 14:48:42 -0700 | [diff] [blame] | 608 | if (log_next_idx + size + sizeof(struct printk_log) > log_buf_len) { |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 609 | /* |
| 610 | * This message + an additional empty header does not fit |
| 611 | * at the end of the buffer. Add an empty header with len == 0 |
| 612 | * to signify a wrap around. |
| 613 | */ |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 614 | memset(log_buf + log_next_idx, 0, sizeof(struct printk_log)); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 615 | log_next_idx = 0; |
| 616 | } |
| 617 | |
| 618 | /* fill message */ |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 619 | msg = (struct printk_log *)(log_buf + log_next_idx); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 620 | memcpy(log_text(msg), text, text_len); |
| 621 | msg->text_len = text_len; |
Petr Mladek | 55bd53a | 2014-06-04 16:11:32 -0700 | [diff] [blame] | 622 | if (trunc_msg_len) { |
| 623 | memcpy(log_text(msg) + text_len, trunc_msg, trunc_msg_len); |
| 624 | msg->text_len += trunc_msg_len; |
| 625 | } |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 626 | memcpy(log_dict(msg), dict, dict_len); |
| 627 | msg->dict_len = dict_len; |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 628 | msg->facility = facility; |
| 629 | msg->level = level & 7; |
| 630 | msg->flags = flags & 0x1f; |
| 631 | if (ts_nsec > 0) |
| 632 | msg->ts_nsec = ts_nsec; |
| 633 | else |
| 634 | msg->ts_nsec = local_clock(); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 635 | memset(log_dict(msg) + dict_len, 0, pad_len); |
Petr Mladek | fce6e03 | 2014-04-03 14:48:43 -0700 | [diff] [blame] | 636 | msg->len = size; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 637 | |
| 638 | /* insert message */ |
| 639 | log_next_idx += msg->len; |
| 640 | log_next_seq++; |
Petr Mladek | 034633c | 2014-06-04 16:11:33 -0700 | [diff] [blame] | 641 | |
| 642 | return msg->text_len; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 643 | } |
Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 644 | |
Alex Elder | e99aa46 | 2014-08-06 16:09:05 -0700 | [diff] [blame] | 645 | int dmesg_restrict = IS_ENABLED(CONFIG_SECURITY_DMESG_RESTRICT); |
Kees Cook | 637241a | 2013-06-12 14:04:39 -0700 | [diff] [blame] | 646 | |
| 647 | static int syslog_action_restricted(int type) |
| 648 | { |
| 649 | if (dmesg_restrict) |
| 650 | return 1; |
| 651 | /* |
| 652 | * Unless restricted, we allow "read all" and "get buffer size" |
| 653 | * for everybody. |
| 654 | */ |
| 655 | return type != SYSLOG_ACTION_READ_ALL && |
| 656 | type != SYSLOG_ACTION_SIZE_BUFFER; |
| 657 | } |
| 658 | |
Kees Cook | c71b02e | 2017-08-09 21:11:00 -0700 | [diff] [blame] | 659 | static int check_syslog_permissions(int type, int source) |
Kees Cook | 637241a | 2013-06-12 14:04:39 -0700 | [diff] [blame] | 660 | { |
| 661 | /* |
| 662 | * If this is from /proc/kmsg and we've already opened it, then we've |
| 663 | * already done the capabilities checks at open time. |
| 664 | */ |
Vasily Averin | 3ea4331 | 2015-06-25 15:01:47 -0700 | [diff] [blame] | 665 | if (source == SYSLOG_FROM_PROC && type != SYSLOG_ACTION_OPEN) |
Vasily Averin | d194e5d | 2015-06-25 15:01:44 -0700 | [diff] [blame] | 666 | goto ok; |
Kees Cook | 637241a | 2013-06-12 14:04:39 -0700 | [diff] [blame] | 667 | |
| 668 | if (syslog_action_restricted(type)) { |
| 669 | if (capable(CAP_SYSLOG)) |
Vasily Averin | d194e5d | 2015-06-25 15:01:44 -0700 | [diff] [blame] | 670 | goto ok; |
Kees Cook | 637241a | 2013-06-12 14:04:39 -0700 | [diff] [blame] | 671 | /* |
| 672 | * For historical reasons, accept CAP_SYS_ADMIN too, with |
| 673 | * a warning. |
| 674 | */ |
| 675 | if (capable(CAP_SYS_ADMIN)) { |
| 676 | pr_warn_once("%s (%d): Attempt to access syslog with " |
| 677 | "CAP_SYS_ADMIN but no CAP_SYSLOG " |
| 678 | "(deprecated).\n", |
| 679 | current->comm, task_pid_nr(current)); |
Vasily Averin | d194e5d | 2015-06-25 15:01:44 -0700 | [diff] [blame] | 680 | goto ok; |
Kees Cook | 637241a | 2013-06-12 14:04:39 -0700 | [diff] [blame] | 681 | } |
| 682 | return -EPERM; |
| 683 | } |
Vasily Averin | d194e5d | 2015-06-25 15:01:44 -0700 | [diff] [blame] | 684 | ok: |
Kees Cook | 637241a | 2013-06-12 14:04:39 -0700 | [diff] [blame] | 685 | return security_syslog(type); |
| 686 | } |
| 687 | |
Tejun Heo | d43ff43 | 2015-06-25 15:01:24 -0700 | [diff] [blame] | 688 | static void append_char(char **pp, char *e, char c) |
| 689 | { |
| 690 | if (*pp < e) |
| 691 | *(*pp)++ = c; |
| 692 | } |
Kees Cook | 637241a | 2013-06-12 14:04:39 -0700 | [diff] [blame] | 693 | |
Tejun Heo | 0a295e67 | 2015-06-25 15:01:27 -0700 | [diff] [blame] | 694 | static ssize_t msg_print_ext_header(char *buf, size_t size, |
Linus Torvalds | 5aa068e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 695 | struct printk_log *msg, u64 seq) |
Tejun Heo | 0a295e67 | 2015-06-25 15:01:27 -0700 | [diff] [blame] | 696 | { |
| 697 | u64 ts_usec = msg->ts_nsec; |
Tejun Heo | 0a295e67 | 2015-06-25 15:01:27 -0700 | [diff] [blame] | 698 | |
| 699 | do_div(ts_usec, 1000); |
| 700 | |
Tejun Heo | 0a295e67 | 2015-06-25 15:01:27 -0700 | [diff] [blame] | 701 | return scnprintf(buf, size, "%u,%llu,%llu,%c;", |
Linus Torvalds | 5aa068e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 702 | (msg->facility << 3) | msg->level, seq, ts_usec, |
| 703 | msg->flags & LOG_CONT ? 'c' : '-'); |
Tejun Heo | 0a295e67 | 2015-06-25 15:01:27 -0700 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | static ssize_t msg_print_ext_body(char *buf, size_t size, |
| 707 | char *dict, size_t dict_len, |
| 708 | char *text, size_t text_len) |
| 709 | { |
| 710 | char *p = buf, *e = buf + size; |
| 711 | size_t i; |
| 712 | |
| 713 | /* escape non-printable characters */ |
| 714 | for (i = 0; i < text_len; i++) { |
| 715 | unsigned char c = text[i]; |
| 716 | |
| 717 | if (c < ' ' || c >= 127 || c == '\\') |
| 718 | p += scnprintf(p, e - p, "\\x%02x", c); |
| 719 | else |
| 720 | append_char(&p, e, c); |
| 721 | } |
| 722 | append_char(&p, e, '\n'); |
| 723 | |
| 724 | if (dict_len) { |
| 725 | bool line = true; |
| 726 | |
| 727 | for (i = 0; i < dict_len; i++) { |
| 728 | unsigned char c = dict[i]; |
| 729 | |
| 730 | if (line) { |
| 731 | append_char(&p, e, ' '); |
| 732 | line = false; |
| 733 | } |
| 734 | |
| 735 | if (c == '\0') { |
| 736 | append_char(&p, e, '\n'); |
| 737 | line = true; |
| 738 | continue; |
| 739 | } |
| 740 | |
| 741 | if (c < ' ' || c >= 127 || c == '\\') { |
| 742 | p += scnprintf(p, e - p, "\\x%02x", c); |
| 743 | continue; |
| 744 | } |
| 745 | |
| 746 | append_char(&p, e, c); |
| 747 | } |
| 748 | append_char(&p, e, '\n'); |
| 749 | } |
| 750 | |
| 751 | return p - buf; |
| 752 | } |
| 753 | |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 754 | /* /dev/kmsg - userspace message inject/listen interface */ |
| 755 | struct devkmsg_user { |
| 756 | u64 seq; |
| 757 | u32 idx; |
Borislav Petkov | 750afe7 | 2016-08-02 14:04:07 -0700 | [diff] [blame] | 758 | struct ratelimit_state rs; |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 759 | struct mutex lock; |
Tejun Heo | d43ff43 | 2015-06-25 15:01:24 -0700 | [diff] [blame] | 760 | char buf[CONSOLE_EXT_LOG_MAX]; |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 761 | }; |
| 762 | |
Al Viro | 849f312 | 2014-08-23 12:23:53 -0400 | [diff] [blame] | 763 | static ssize_t devkmsg_write(struct kiocb *iocb, struct iov_iter *from) |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 764 | { |
| 765 | char *buf, *line; |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 766 | int level = default_message_loglevel; |
| 767 | int facility = 1; /* LOG_USER */ |
Borislav Petkov | 750afe7 | 2016-08-02 14:04:07 -0700 | [diff] [blame] | 768 | struct file *file = iocb->ki_filp; |
| 769 | struct devkmsg_user *user = file->private_data; |
Christoph Hellwig | 66ee59a | 2015-02-11 19:56:46 +0100 | [diff] [blame] | 770 | size_t len = iov_iter_count(from); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 771 | ssize_t ret = len; |
| 772 | |
Borislav Petkov | 750afe7 | 2016-08-02 14:04:07 -0700 | [diff] [blame] | 773 | if (!user || len > LOG_LINE_MAX) |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 774 | return -EINVAL; |
Borislav Petkov | 750afe7 | 2016-08-02 14:04:07 -0700 | [diff] [blame] | 775 | |
| 776 | /* Ignore when user logging is disabled. */ |
| 777 | if (devkmsg_log & DEVKMSG_LOG_MASK_OFF) |
| 778 | return len; |
| 779 | |
| 780 | /* Ratelimit when not explicitly enabled. */ |
| 781 | if (!(devkmsg_log & DEVKMSG_LOG_MASK_ON)) { |
| 782 | if (!___ratelimit(&user->rs, current->comm)) |
| 783 | return ret; |
| 784 | } |
| 785 | |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 786 | buf = kmalloc(len+1, GFP_KERNEL); |
| 787 | if (buf == NULL) |
| 788 | return -ENOMEM; |
| 789 | |
Al Viro | 849f312 | 2014-08-23 12:23:53 -0400 | [diff] [blame] | 790 | buf[len] = '\0'; |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 791 | if (!copy_from_iter_full(buf, len, from)) { |
Al Viro | 849f312 | 2014-08-23 12:23:53 -0400 | [diff] [blame] | 792 | kfree(buf); |
| 793 | return -EFAULT; |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | /* |
| 797 | * Extract and skip the syslog prefix <[0-9]*>. Coming from userspace |
| 798 | * the decimal value represents 32bit, the lower 3 bit are the log |
| 799 | * level, the rest are the log facility. |
| 800 | * |
| 801 | * If no prefix or no userspace facility is specified, we |
| 802 | * enforce LOG_USER, to be able to reliably distinguish |
| 803 | * kernel-generated messages from userspace-injected ones. |
| 804 | */ |
| 805 | line = buf; |
| 806 | if (line[0] == '<') { |
| 807 | char *endp = NULL; |
Mathias Krause | 3824657 | 2015-11-06 16:30:38 -0800 | [diff] [blame] | 808 | unsigned int u; |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 809 | |
Mathias Krause | 3824657 | 2015-11-06 16:30:38 -0800 | [diff] [blame] | 810 | u = simple_strtoul(line + 1, &endp, 10); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 811 | if (endp && endp[0] == '>') { |
Mathias Krause | 3824657 | 2015-11-06 16:30:38 -0800 | [diff] [blame] | 812 | level = LOG_LEVEL(u); |
| 813 | if (LOG_FACILITY(u) != 0) |
| 814 | facility = LOG_FACILITY(u); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 815 | endp++; |
| 816 | len -= endp - line; |
| 817 | line = endp; |
| 818 | } |
| 819 | } |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 820 | |
| 821 | printk_emit(facility, level, NULL, 0, "%s", line); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 822 | kfree(buf); |
| 823 | return ret; |
| 824 | } |
| 825 | |
| 826 | static ssize_t devkmsg_read(struct file *file, char __user *buf, |
| 827 | size_t count, loff_t *ppos) |
| 828 | { |
| 829 | struct devkmsg_user *user = file->private_data; |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 830 | struct printk_log *msg; |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 831 | size_t len; |
| 832 | ssize_t ret; |
| 833 | |
| 834 | if (!user) |
| 835 | return -EBADF; |
| 836 | |
Yuanhan Liu | 4a77a5a | 2012-06-16 21:21:51 +0800 | [diff] [blame] | 837 | ret = mutex_lock_interruptible(&user->lock); |
| 838 | if (ret) |
| 839 | return ret; |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 840 | |
| 841 | logbuf_lock_irq(); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 842 | while (user->seq == log_next_seq) { |
| 843 | if (file->f_flags & O_NONBLOCK) { |
| 844 | ret = -EAGAIN; |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 845 | logbuf_unlock_irq(); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 846 | goto out; |
| 847 | } |
| 848 | |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 849 | logbuf_unlock_irq(); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 850 | ret = wait_event_interruptible(log_wait, |
| 851 | user->seq != log_next_seq); |
| 852 | if (ret) |
| 853 | goto out; |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 854 | logbuf_lock_irq(); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | if (user->seq < log_first_seq) { |
| 858 | /* our last seen message is gone, return error and reset */ |
| 859 | user->idx = log_first_idx; |
| 860 | user->seq = log_first_seq; |
| 861 | ret = -EPIPE; |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 862 | logbuf_unlock_irq(); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 863 | goto out; |
| 864 | } |
| 865 | |
| 866 | msg = log_from_idx(user->idx); |
Tejun Heo | 0a295e67 | 2015-06-25 15:01:27 -0700 | [diff] [blame] | 867 | len = msg_print_ext_header(user->buf, sizeof(user->buf), |
Linus Torvalds | 5aa068e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 868 | msg, user->seq); |
Tejun Heo | 0a295e67 | 2015-06-25 15:01:27 -0700 | [diff] [blame] | 869 | len += msg_print_ext_body(user->buf + len, sizeof(user->buf) - len, |
| 870 | log_dict(msg), msg->dict_len, |
| 871 | log_text(msg), msg->text_len); |
Kay Sievers | d39f3d7 | 2012-07-16 18:35:30 -0700 | [diff] [blame] | 872 | |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 873 | user->idx = log_next(user->idx); |
| 874 | user->seq++; |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 875 | logbuf_unlock_irq(); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 876 | |
| 877 | if (len > count) { |
| 878 | ret = -EINVAL; |
| 879 | goto out; |
| 880 | } |
| 881 | |
| 882 | if (copy_to_user(buf, user->buf, len)) { |
| 883 | ret = -EFAULT; |
| 884 | goto out; |
| 885 | } |
| 886 | ret = len; |
| 887 | out: |
| 888 | mutex_unlock(&user->lock); |
| 889 | return ret; |
| 890 | } |
| 891 | |
| 892 | static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence) |
| 893 | { |
| 894 | struct devkmsg_user *user = file->private_data; |
| 895 | loff_t ret = 0; |
| 896 | |
| 897 | if (!user) |
| 898 | return -EBADF; |
| 899 | if (offset) |
| 900 | return -ESPIPE; |
| 901 | |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 902 | logbuf_lock_irq(); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 903 | switch (whence) { |
| 904 | case SEEK_SET: |
| 905 | /* the first record */ |
| 906 | user->idx = log_first_idx; |
| 907 | user->seq = log_first_seq; |
| 908 | break; |
| 909 | case SEEK_DATA: |
| 910 | /* |
| 911 | * The first record after the last SYSLOG_ACTION_CLEAR, |
| 912 | * like issued by 'dmesg -c'. Reading /dev/kmsg itself |
| 913 | * changes no global state, and does not clear anything. |
| 914 | */ |
| 915 | user->idx = clear_idx; |
| 916 | user->seq = clear_seq; |
| 917 | break; |
| 918 | case SEEK_END: |
| 919 | /* after the last record */ |
| 920 | user->idx = log_next_idx; |
| 921 | user->seq = log_next_seq; |
| 922 | break; |
| 923 | default: |
| 924 | ret = -EINVAL; |
| 925 | } |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 926 | logbuf_unlock_irq(); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 927 | return ret; |
| 928 | } |
| 929 | |
| 930 | static unsigned int devkmsg_poll(struct file *file, poll_table *wait) |
| 931 | { |
| 932 | struct devkmsg_user *user = file->private_data; |
| 933 | int ret = 0; |
| 934 | |
| 935 | if (!user) |
| 936 | return POLLERR|POLLNVAL; |
| 937 | |
| 938 | poll_wait(file, &log_wait, wait); |
| 939 | |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 940 | logbuf_lock_irq(); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 941 | if (user->seq < log_next_seq) { |
| 942 | /* return error when data has vanished underneath us */ |
| 943 | if (user->seq < log_first_seq) |
| 944 | ret = POLLIN|POLLRDNORM|POLLERR|POLLPRI; |
Nicolas Kaiser | 0a28531 | 2013-04-29 16:17:20 -0700 | [diff] [blame] | 945 | else |
| 946 | ret = POLLIN|POLLRDNORM; |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 947 | } |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 948 | logbuf_unlock_irq(); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 949 | |
| 950 | return ret; |
| 951 | } |
| 952 | |
| 953 | static int devkmsg_open(struct inode *inode, struct file *file) |
| 954 | { |
| 955 | struct devkmsg_user *user; |
| 956 | int err; |
| 957 | |
Borislav Petkov | 750afe7 | 2016-08-02 14:04:07 -0700 | [diff] [blame] | 958 | if (devkmsg_log & DEVKMSG_LOG_MASK_OFF) |
| 959 | return -EPERM; |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 960 | |
Borislav Petkov | 750afe7 | 2016-08-02 14:04:07 -0700 | [diff] [blame] | 961 | /* write-only does not need any file context */ |
| 962 | if ((file->f_flags & O_ACCMODE) != O_WRONLY) { |
| 963 | err = check_syslog_permissions(SYSLOG_ACTION_READ_ALL, |
| 964 | SYSLOG_FROM_READER); |
| 965 | if (err) |
| 966 | return err; |
| 967 | } |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 968 | |
| 969 | user = kmalloc(sizeof(struct devkmsg_user), GFP_KERNEL); |
| 970 | if (!user) |
| 971 | return -ENOMEM; |
| 972 | |
Borislav Petkov | 750afe7 | 2016-08-02 14:04:07 -0700 | [diff] [blame] | 973 | ratelimit_default_init(&user->rs); |
| 974 | ratelimit_set_flags(&user->rs, RATELIMIT_MSG_ON_RELEASE); |
| 975 | |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 976 | mutex_init(&user->lock); |
| 977 | |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 978 | logbuf_lock_irq(); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 979 | user->idx = log_first_idx; |
| 980 | user->seq = log_first_seq; |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 981 | logbuf_unlock_irq(); |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 982 | |
| 983 | file->private_data = user; |
| 984 | return 0; |
| 985 | } |
| 986 | |
| 987 | static int devkmsg_release(struct inode *inode, struct file *file) |
| 988 | { |
| 989 | struct devkmsg_user *user = file->private_data; |
| 990 | |
| 991 | if (!user) |
| 992 | return 0; |
| 993 | |
Borislav Petkov | 750afe7 | 2016-08-02 14:04:07 -0700 | [diff] [blame] | 994 | ratelimit_state_exit(&user->rs); |
| 995 | |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 996 | mutex_destroy(&user->lock); |
| 997 | kfree(user); |
| 998 | return 0; |
| 999 | } |
| 1000 | |
| 1001 | const struct file_operations kmsg_fops = { |
| 1002 | .open = devkmsg_open, |
| 1003 | .read = devkmsg_read, |
Al Viro | 849f312 | 2014-08-23 12:23:53 -0400 | [diff] [blame] | 1004 | .write_iter = devkmsg_write, |
Kay Sievers | e11fea9 | 2012-05-03 02:29:41 +0200 | [diff] [blame] | 1005 | .llseek = devkmsg_llseek, |
| 1006 | .poll = devkmsg_poll, |
| 1007 | .release = devkmsg_release, |
| 1008 | }; |
| 1009 | |
Hari Bathini | 692f66f | 2017-05-08 15:56:18 -0700 | [diff] [blame] | 1010 | #ifdef CONFIG_CRASH_CORE |
Neil Horman | 04d491a | 2009-04-02 16:58:57 -0700 | [diff] [blame] | 1011 | /* |
Dirk Gouders | 4c1ace6 | 2013-11-12 15:08:54 -0800 | [diff] [blame] | 1012 | * This appends the listed symbols to /proc/vmcore |
Neil Horman | 04d491a | 2009-04-02 16:58:57 -0700 | [diff] [blame] | 1013 | * |
Dirk Gouders | 4c1ace6 | 2013-11-12 15:08:54 -0800 | [diff] [blame] | 1014 | * /proc/vmcore is used by various utilities, like crash and makedumpfile to |
Neil Horman | 04d491a | 2009-04-02 16:58:57 -0700 | [diff] [blame] | 1015 | * obtain access to symbols that are otherwise very difficult to locate. These |
| 1016 | * symbols are specifically used so that utilities can access and extract the |
| 1017 | * dmesg log from a vmcore file after a crash. |
| 1018 | */ |
Hari Bathini | 692f66f | 2017-05-08 15:56:18 -0700 | [diff] [blame] | 1019 | void log_buf_vmcoreinfo_setup(void) |
Neil Horman | 04d491a | 2009-04-02 16:58:57 -0700 | [diff] [blame] | 1020 | { |
| 1021 | VMCOREINFO_SYMBOL(log_buf); |
Neil Horman | 04d491a | 2009-04-02 16:58:57 -0700 | [diff] [blame] | 1022 | VMCOREINFO_SYMBOL(log_buf_len); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1023 | VMCOREINFO_SYMBOL(log_first_idx); |
Ivan Delalande | f468908 | 2016-03-17 14:21:30 -0700 | [diff] [blame] | 1024 | VMCOREINFO_SYMBOL(clear_idx); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1025 | VMCOREINFO_SYMBOL(log_next_idx); |
Vivek Goyal | 6791457 | 2012-07-18 13:18:12 -0400 | [diff] [blame] | 1026 | /* |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 1027 | * Export struct printk_log size and field offsets. User space tools can |
Vivek Goyal | 6791457 | 2012-07-18 13:18:12 -0400 | [diff] [blame] | 1028 | * parse it and detect any changes to structure down the line. |
| 1029 | */ |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 1030 | VMCOREINFO_STRUCT_SIZE(printk_log); |
| 1031 | VMCOREINFO_OFFSET(printk_log, ts_nsec); |
| 1032 | VMCOREINFO_OFFSET(printk_log, len); |
| 1033 | VMCOREINFO_OFFSET(printk_log, text_len); |
| 1034 | VMCOREINFO_OFFSET(printk_log, dict_len); |
Neil Horman | 04d491a | 2009-04-02 16:58:57 -0700 | [diff] [blame] | 1035 | } |
| 1036 | #endif |
| 1037 | |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 1038 | /* requested log_buf_len from kernel cmdline */ |
| 1039 | static unsigned long __initdata new_log_buf_len; |
| 1040 | |
Luis R. Rodriguez | c0a318a | 2014-08-06 16:08:52 -0700 | [diff] [blame] | 1041 | /* we practice scaling the ring buffer by powers of 2 */ |
| 1042 | static void __init log_buf_len_update(unsigned size) |
| 1043 | { |
| 1044 | if (size) |
| 1045 | size = roundup_pow_of_two(size); |
| 1046 | if (size > log_buf_len) |
| 1047 | new_log_buf_len = size; |
| 1048 | } |
| 1049 | |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 1050 | /* 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] | 1051 | static int __init log_buf_len_setup(char *str) |
| 1052 | { |
Denys Vlasenko | eed4a2a | 2008-02-06 01:37:02 -0800 | [diff] [blame] | 1053 | unsigned size = memparse(str, &str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | |
Luis R. Rodriguez | c0a318a | 2014-08-06 16:08:52 -0700 | [diff] [blame] | 1055 | log_buf_len_update(size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 1057 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | } |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 1059 | early_param("log_buf_len", log_buf_len_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | |
Geert Uytterhoeven | 2240a31 | 2014-10-13 15:51:11 -0700 | [diff] [blame] | 1061 | #ifdef CONFIG_SMP |
| 1062 | #define __LOG_CPU_MAX_BUF_LEN (1 << CONFIG_LOG_CPU_MAX_BUF_SHIFT) |
| 1063 | |
Luis R. Rodriguez | 23b2899 | 2014-08-06 16:08:56 -0700 | [diff] [blame] | 1064 | static void __init log_buf_add_cpu(void) |
| 1065 | { |
| 1066 | unsigned int cpu_extra; |
| 1067 | |
| 1068 | /* |
| 1069 | * archs should set up cpu_possible_bits properly with |
| 1070 | * set_cpu_possible() after setup_arch() but just in |
| 1071 | * case lets ensure this is valid. |
| 1072 | */ |
| 1073 | if (num_possible_cpus() == 1) |
| 1074 | return; |
| 1075 | |
| 1076 | cpu_extra = (num_possible_cpus() - 1) * __LOG_CPU_MAX_BUF_LEN; |
| 1077 | |
| 1078 | /* by default this will only continue through for large > 64 CPUs */ |
| 1079 | if (cpu_extra <= __LOG_BUF_LEN / 2) |
| 1080 | return; |
| 1081 | |
| 1082 | pr_info("log_buf_len individual max cpu contribution: %d bytes\n", |
| 1083 | __LOG_CPU_MAX_BUF_LEN); |
| 1084 | pr_info("log_buf_len total cpu_extra contributions: %d bytes\n", |
| 1085 | cpu_extra); |
| 1086 | pr_info("log_buf_len min size: %d bytes\n", __LOG_BUF_LEN); |
| 1087 | |
| 1088 | log_buf_len_update(cpu_extra + __LOG_BUF_LEN); |
| 1089 | } |
Geert Uytterhoeven | 2240a31 | 2014-10-13 15:51:11 -0700 | [diff] [blame] | 1090 | #else /* !CONFIG_SMP */ |
| 1091 | static inline void log_buf_add_cpu(void) {} |
| 1092 | #endif /* CONFIG_SMP */ |
Luis R. Rodriguez | 23b2899 | 2014-08-06 16:08:56 -0700 | [diff] [blame] | 1093 | |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 1094 | void __init setup_log_buf(int early) |
| 1095 | { |
| 1096 | unsigned long flags; |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 1097 | char *new_log_buf; |
| 1098 | int free; |
| 1099 | |
Luis R. Rodriguez | 23b2899 | 2014-08-06 16:08:56 -0700 | [diff] [blame] | 1100 | if (log_buf != __log_buf) |
| 1101 | return; |
| 1102 | |
| 1103 | if (!early && !new_log_buf_len) |
| 1104 | log_buf_add_cpu(); |
| 1105 | |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 1106 | if (!new_log_buf_len) |
| 1107 | return; |
| 1108 | |
| 1109 | if (early) { |
Santosh Shilimkar | 9da791d | 2014-01-21 15:50:23 -0800 | [diff] [blame] | 1110 | new_log_buf = |
Luis R. Rodriguez | 7030017 | 2014-08-06 16:08:49 -0700 | [diff] [blame] | 1111 | memblock_virt_alloc(new_log_buf_len, LOG_ALIGN); |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 1112 | } else { |
Luis R. Rodriguez | 7030017 | 2014-08-06 16:08:49 -0700 | [diff] [blame] | 1113 | new_log_buf = memblock_virt_alloc_nopanic(new_log_buf_len, |
| 1114 | LOG_ALIGN); |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | if (unlikely(!new_log_buf)) { |
| 1118 | pr_err("log_buf_len: %ld bytes not available\n", |
| 1119 | new_log_buf_len); |
| 1120 | return; |
| 1121 | } |
| 1122 | |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 1123 | logbuf_lock_irqsave(flags); |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 1124 | log_buf_len = new_log_buf_len; |
| 1125 | log_buf = new_log_buf; |
| 1126 | new_log_buf_len = 0; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1127 | free = __LOG_BUF_LEN - log_next_idx; |
| 1128 | memcpy(log_buf, __log_buf, __LOG_BUF_LEN); |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 1129 | logbuf_unlock_irqrestore(flags); |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 1130 | |
Luis R. Rodriguez | f540517 | 2014-08-06 16:08:54 -0700 | [diff] [blame] | 1131 | pr_info("log_buf_len: %d bytes\n", log_buf_len); |
Mike Travis | 162a7e7 | 2011-05-24 17:13:20 -0700 | [diff] [blame] | 1132 | pr_info("early log buf free: %d(%d%%)\n", |
| 1133 | free, (free * 100) / __LOG_BUF_LEN); |
| 1134 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | |
Andrew Cooks | 2fa72c8 | 2012-12-17 15:59:56 -0800 | [diff] [blame] | 1136 | static bool __read_mostly ignore_loglevel; |
| 1137 | |
| 1138 | static int __init ignore_loglevel_setup(char *str) |
| 1139 | { |
Neil Zhang | d25d9fe | 2014-08-06 16:09:12 -0700 | [diff] [blame] | 1140 | ignore_loglevel = true; |
Andrew Morton | 27083ba | 2013-11-12 15:08:50 -0800 | [diff] [blame] | 1141 | pr_info("debug: ignoring loglevel setting.\n"); |
Andrew Cooks | 2fa72c8 | 2012-12-17 15:59:56 -0800 | [diff] [blame] | 1142 | |
| 1143 | return 0; |
| 1144 | } |
| 1145 | |
| 1146 | early_param("ignore_loglevel", ignore_loglevel_setup); |
| 1147 | module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR); |
Joe Perches | 205bd3d | 2015-02-12 15:01:34 -0800 | [diff] [blame] | 1148 | MODULE_PARM_DESC(ignore_loglevel, |
| 1149 | "ignore loglevel setting (prints all kernel messages to the console)"); |
Andrew Cooks | 2fa72c8 | 2012-12-17 15:59:56 -0800 | [diff] [blame] | 1150 | |
Sergey Senozhatsky | cf77544 | 2016-08-02 14:03:56 -0700 | [diff] [blame] | 1151 | static bool suppress_message_printing(int level) |
| 1152 | { |
| 1153 | return (level >= console_loglevel && !ignore_loglevel); |
| 1154 | } |
| 1155 | |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 1156 | #ifdef CONFIG_BOOT_PRINTK_DELAY |
| 1157 | |
Namhyung Kim | 674dff6 | 2010-10-26 14:22:48 -0700 | [diff] [blame] | 1158 | static int boot_delay; /* msecs delay after each printk during bootup */ |
Dave Young | 3a3b6ed | 2009-09-22 16:43:31 -0700 | [diff] [blame] | 1159 | static unsigned long long loops_per_msec; /* based on boot_delay */ |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 1160 | |
| 1161 | static int __init boot_delay_setup(char *str) |
| 1162 | { |
| 1163 | unsigned long lpj; |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 1164 | |
| 1165 | lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */ |
| 1166 | loops_per_msec = (unsigned long long)lpj / 1000 * HZ; |
| 1167 | |
| 1168 | get_option(&str, &boot_delay); |
| 1169 | if (boot_delay > 10 * 1000) |
| 1170 | boot_delay = 0; |
| 1171 | |
Dave Young | 3a3b6ed | 2009-09-22 16:43:31 -0700 | [diff] [blame] | 1172 | pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, " |
| 1173 | "HZ: %d, loops_per_msec: %llu\n", |
| 1174 | boot_delay, preset_lpj, lpj, HZ, loops_per_msec); |
Dave Young | 29e9d22 | 2013-11-12 15:08:53 -0800 | [diff] [blame] | 1175 | return 0; |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 1176 | } |
Dave Young | 29e9d22 | 2013-11-12 15:08:53 -0800 | [diff] [blame] | 1177 | early_param("boot_delay", boot_delay_setup); |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 1178 | |
Andrew Cooks | 2fa72c8 | 2012-12-17 15:59:56 -0800 | [diff] [blame] | 1179 | static void boot_delay_msec(int level) |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 1180 | { |
| 1181 | unsigned long long k; |
| 1182 | unsigned long timeout; |
| 1183 | |
Thomas Gleixner | ff48cd2 | 2017-05-16 20:42:45 +0200 | [diff] [blame] | 1184 | if ((boot_delay == 0 || system_state >= SYSTEM_RUNNING) |
Sergey Senozhatsky | cf77544 | 2016-08-02 14:03:56 -0700 | [diff] [blame] | 1185 | || suppress_message_printing(level)) { |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 1186 | return; |
Andrew Cooks | 2fa72c8 | 2012-12-17 15:59:56 -0800 | [diff] [blame] | 1187 | } |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 1188 | |
Dave Young | 3a3b6ed | 2009-09-22 16:43:31 -0700 | [diff] [blame] | 1189 | k = (unsigned long long)loops_per_msec * boot_delay; |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 1190 | |
| 1191 | timeout = jiffies + msecs_to_jiffies(boot_delay); |
| 1192 | while (k) { |
| 1193 | k--; |
| 1194 | cpu_relax(); |
| 1195 | /* |
| 1196 | * use (volatile) jiffies to prevent |
| 1197 | * compiler reduction; loop termination via jiffies |
| 1198 | * is secondary and may or may not happen. |
| 1199 | */ |
| 1200 | if (time_after(jiffies, timeout)) |
| 1201 | break; |
| 1202 | touch_nmi_watchdog(); |
| 1203 | } |
| 1204 | } |
| 1205 | #else |
Andrew Cooks | 2fa72c8 | 2012-12-17 15:59:56 -0800 | [diff] [blame] | 1206 | static inline void boot_delay_msec(int level) |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 1207 | { |
| 1208 | } |
| 1209 | #endif |
| 1210 | |
Alex Elder | e99aa46 | 2014-08-06 16:09:05 -0700 | [diff] [blame] | 1211 | static bool printk_time = IS_ENABLED(CONFIG_PRINTK_TIME); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1212 | module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR); |
| 1213 | |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1214 | static size_t print_time(u64 ts, char *buf) |
| 1215 | { |
| 1216 | unsigned long rem_nsec; |
| 1217 | |
| 1218 | if (!printk_time) |
| 1219 | return 0; |
| 1220 | |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1221 | rem_nsec = do_div(ts, 1000000000); |
Roland Dreier | 35dac27 | 2013-01-04 15:35:50 -0800 | [diff] [blame] | 1222 | |
| 1223 | if (!buf) |
| 1224 | return snprintf(NULL, 0, "[%5lu.000000] ", (unsigned long)ts); |
| 1225 | |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1226 | return sprintf(buf, "[%5lu.%06lu] ", |
| 1227 | (unsigned long)ts, rem_nsec / 1000); |
| 1228 | } |
| 1229 | |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 1230 | static size_t print_prefix(const struct printk_log *msg, bool syslog, char *buf) |
Kay Sievers | 649e6ee | 2012-05-10 04:30:45 +0200 | [diff] [blame] | 1231 | { |
Kay Sievers | 3ce9a7c | 2012-05-13 23:30:46 +0200 | [diff] [blame] | 1232 | size_t len = 0; |
Kay Sievers | 43a73a5 | 2012-07-06 09:50:09 -0700 | [diff] [blame] | 1233 | unsigned int prefix = (msg->facility << 3) | msg->level; |
Kay Sievers | 649e6ee | 2012-05-10 04:30:45 +0200 | [diff] [blame] | 1234 | |
Kay Sievers | 3ce9a7c | 2012-05-13 23:30:46 +0200 | [diff] [blame] | 1235 | if (syslog) { |
| 1236 | if (buf) { |
Kay Sievers | 43a73a5 | 2012-07-06 09:50:09 -0700 | [diff] [blame] | 1237 | len += sprintf(buf, "<%u>", prefix); |
Kay Sievers | 3ce9a7c | 2012-05-13 23:30:46 +0200 | [diff] [blame] | 1238 | } else { |
| 1239 | len += 3; |
Kay Sievers | 43a73a5 | 2012-07-06 09:50:09 -0700 | [diff] [blame] | 1240 | if (prefix > 999) |
| 1241 | len += 3; |
| 1242 | else if (prefix > 99) |
| 1243 | len += 2; |
| 1244 | else if (prefix > 9) |
Kay Sievers | 3ce9a7c | 2012-05-13 23:30:46 +0200 | [diff] [blame] | 1245 | len++; |
| 1246 | } |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1247 | } |
| 1248 | |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1249 | len += print_time(msg->ts_nsec, buf ? buf + len : NULL); |
Kay Sievers | 3ce9a7c | 2012-05-13 23:30:46 +0200 | [diff] [blame] | 1250 | return len; |
| 1251 | } |
| 1252 | |
Linus Torvalds | 5aa068e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 1253 | static size_t msg_print_text(const struct printk_log *msg, bool syslog, char *buf, size_t size) |
Kay Sievers | 3ce9a7c | 2012-05-13 23:30:46 +0200 | [diff] [blame] | 1254 | { |
| 1255 | const char *text = log_text(msg); |
| 1256 | size_t text_size = msg->text_len; |
| 1257 | size_t len = 0; |
| 1258 | |
| 1259 | do { |
| 1260 | const char *next = memchr(text, '\n', text_size); |
| 1261 | size_t text_len; |
| 1262 | |
| 1263 | if (next) { |
| 1264 | text_len = next - text; |
| 1265 | next++; |
| 1266 | text_size -= next - text; |
| 1267 | } else { |
| 1268 | text_len = text_size; |
| 1269 | } |
| 1270 | |
| 1271 | if (buf) { |
| 1272 | if (print_prefix(msg, syslog, NULL) + |
Kay Sievers | 7049825 | 2012-07-16 18:35:29 -0700 | [diff] [blame] | 1273 | text_len + 1 >= size - len) |
Kay Sievers | 3ce9a7c | 2012-05-13 23:30:46 +0200 | [diff] [blame] | 1274 | break; |
| 1275 | |
Linus Torvalds | 5aa068e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 1276 | len += print_prefix(msg, syslog, buf + len); |
Kay Sievers | 3ce9a7c | 2012-05-13 23:30:46 +0200 | [diff] [blame] | 1277 | memcpy(buf + len, text, text_len); |
| 1278 | len += text_len; |
Linus Torvalds | 5aa068e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 1279 | buf[len++] = '\n'; |
Kay Sievers | 3ce9a7c | 2012-05-13 23:30:46 +0200 | [diff] [blame] | 1280 | } else { |
| 1281 | /* SYSLOG_ACTION_* buffer size only calculation */ |
Linus Torvalds | 5aa068e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 1282 | len += print_prefix(msg, syslog, NULL); |
Kay Sievers | 5becfb1 | 2012-07-09 12:15:42 -0700 | [diff] [blame] | 1283 | len += text_len; |
Linus Torvalds | 5aa068e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 1284 | len++; |
Kay Sievers | 3ce9a7c | 2012-05-13 23:30:46 +0200 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | text = next; |
| 1288 | } while (text); |
| 1289 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1290 | return len; |
| 1291 | } |
| 1292 | |
| 1293 | static int syslog_print(char __user *buf, int size) |
| 1294 | { |
| 1295 | char *text; |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 1296 | struct printk_log *msg; |
Jan Beulich | 116e90b | 2012-06-22 16:36:09 +0100 | [diff] [blame] | 1297 | int len = 0; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1298 | |
Kay Sievers | 7049825 | 2012-07-16 18:35:29 -0700 | [diff] [blame] | 1299 | text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1300 | if (!text) |
| 1301 | return -ENOMEM; |
| 1302 | |
Jan Beulich | 116e90b | 2012-06-22 16:36:09 +0100 | [diff] [blame] | 1303 | while (size > 0) { |
| 1304 | size_t n; |
Kay Sievers | eb02dac | 2012-07-09 10:05:10 -0700 | [diff] [blame] | 1305 | size_t skip; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1306 | |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 1307 | logbuf_lock_irq(); |
Jan Beulich | 116e90b | 2012-06-22 16:36:09 +0100 | [diff] [blame] | 1308 | if (syslog_seq < log_first_seq) { |
| 1309 | /* messages are gone, move to first one */ |
| 1310 | syslog_seq = log_first_seq; |
| 1311 | syslog_idx = log_first_idx; |
Kay Sievers | eb02dac | 2012-07-09 10:05:10 -0700 | [diff] [blame] | 1312 | syslog_partial = 0; |
Jan Beulich | 116e90b | 2012-06-22 16:36:09 +0100 | [diff] [blame] | 1313 | } |
| 1314 | if (syslog_seq == log_next_seq) { |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 1315 | logbuf_unlock_irq(); |
Jan Beulich | 116e90b | 2012-06-22 16:36:09 +0100 | [diff] [blame] | 1316 | break; |
| 1317 | } |
Kay Sievers | eb02dac | 2012-07-09 10:05:10 -0700 | [diff] [blame] | 1318 | |
| 1319 | skip = syslog_partial; |
Jan Beulich | 116e90b | 2012-06-22 16:36:09 +0100 | [diff] [blame] | 1320 | msg = log_from_idx(syslog_idx); |
Linus Torvalds | 5aa068e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 1321 | n = msg_print_text(msg, true, text, LOG_LINE_MAX + PREFIX_MAX); |
Kay Sievers | eb02dac | 2012-07-09 10:05:10 -0700 | [diff] [blame] | 1322 | if (n - syslog_partial <= size) { |
| 1323 | /* message fits into buffer, move forward */ |
Jan Beulich | 116e90b | 2012-06-22 16:36:09 +0100 | [diff] [blame] | 1324 | syslog_idx = log_next(syslog_idx); |
| 1325 | syslog_seq++; |
Kay Sievers | eb02dac | 2012-07-09 10:05:10 -0700 | [diff] [blame] | 1326 | n -= syslog_partial; |
| 1327 | syslog_partial = 0; |
| 1328 | } else if (!len){ |
| 1329 | /* partial read(), remember position */ |
| 1330 | n = size; |
| 1331 | syslog_partial += n; |
Jan Beulich | 116e90b | 2012-06-22 16:36:09 +0100 | [diff] [blame] | 1332 | } else |
| 1333 | n = 0; |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 1334 | logbuf_unlock_irq(); |
Jan Beulich | 116e90b | 2012-06-22 16:36:09 +0100 | [diff] [blame] | 1335 | |
| 1336 | if (!n) |
| 1337 | break; |
| 1338 | |
Kay Sievers | eb02dac | 2012-07-09 10:05:10 -0700 | [diff] [blame] | 1339 | if (copy_to_user(buf, text + skip, n)) { |
Jan Beulich | 116e90b | 2012-06-22 16:36:09 +0100 | [diff] [blame] | 1340 | if (!len) |
| 1341 | len = -EFAULT; |
| 1342 | break; |
| 1343 | } |
Kay Sievers | eb02dac | 2012-07-09 10:05:10 -0700 | [diff] [blame] | 1344 | |
| 1345 | len += n; |
| 1346 | size -= n; |
| 1347 | buf += n; |
Jan Beulich | 116e90b | 2012-06-22 16:36:09 +0100 | [diff] [blame] | 1348 | } |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1349 | |
| 1350 | kfree(text); |
| 1351 | return len; |
| 1352 | } |
| 1353 | |
| 1354 | static int syslog_print_all(char __user *buf, int size, bool clear) |
| 1355 | { |
| 1356 | char *text; |
| 1357 | int len = 0; |
| 1358 | |
Kay Sievers | 7049825 | 2012-07-16 18:35:29 -0700 | [diff] [blame] | 1359 | text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1360 | if (!text) |
| 1361 | return -ENOMEM; |
| 1362 | |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 1363 | logbuf_lock_irq(); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1364 | if (buf) { |
| 1365 | u64 next_seq; |
| 1366 | u64 seq; |
| 1367 | u32 idx; |
| 1368 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1369 | /* |
| 1370 | * Find first record that fits, including all following records, |
| 1371 | * into the user-provided buffer for this dump. |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 1372 | */ |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1373 | seq = clear_seq; |
| 1374 | idx = clear_idx; |
| 1375 | while (seq < log_next_seq) { |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 1376 | struct printk_log *msg = log_from_idx(idx); |
Kay Sievers | 3ce9a7c | 2012-05-13 23:30:46 +0200 | [diff] [blame] | 1377 | |
Linus Torvalds | 5aa068e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 1378 | len += msg_print_text(msg, true, NULL, 0); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1379 | idx = log_next(idx); |
| 1380 | seq++; |
| 1381 | } |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 1382 | |
| 1383 | /* move first record forward until length fits into the buffer */ |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1384 | seq = clear_seq; |
| 1385 | idx = clear_idx; |
| 1386 | while (len > size && seq < log_next_seq) { |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 1387 | struct printk_log *msg = log_from_idx(idx); |
Kay Sievers | 3ce9a7c | 2012-05-13 23:30:46 +0200 | [diff] [blame] | 1388 | |
Linus Torvalds | 5aa068e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 1389 | len -= msg_print_text(msg, true, NULL, 0); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1390 | idx = log_next(idx); |
| 1391 | seq++; |
| 1392 | } |
| 1393 | |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 1394 | /* last message fitting into this dump */ |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1395 | next_seq = log_next_seq; |
| 1396 | |
| 1397 | len = 0; |
| 1398 | while (len >= 0 && seq < next_seq) { |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 1399 | struct printk_log *msg = log_from_idx(idx); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1400 | int textlen; |
| 1401 | |
Linus Torvalds | 5aa068e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 1402 | textlen = msg_print_text(msg, true, text, |
Kay Sievers | 7049825 | 2012-07-16 18:35:29 -0700 | [diff] [blame] | 1403 | LOG_LINE_MAX + PREFIX_MAX); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1404 | if (textlen < 0) { |
| 1405 | len = textlen; |
| 1406 | break; |
| 1407 | } |
| 1408 | idx = log_next(idx); |
| 1409 | seq++; |
| 1410 | |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 1411 | logbuf_unlock_irq(); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1412 | if (copy_to_user(buf + len, text, textlen)) |
| 1413 | len = -EFAULT; |
| 1414 | else |
| 1415 | len += textlen; |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 1416 | logbuf_lock_irq(); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1417 | |
| 1418 | if (seq < log_first_seq) { |
| 1419 | /* messages are gone, move to next one */ |
| 1420 | seq = log_first_seq; |
| 1421 | idx = log_first_idx; |
| 1422 | } |
| 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | if (clear) { |
| 1427 | clear_seq = log_next_seq; |
| 1428 | clear_idx = log_next_idx; |
| 1429 | } |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 1430 | logbuf_unlock_irq(); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1431 | |
| 1432 | kfree(text); |
| 1433 | return len; |
| 1434 | } |
| 1435 | |
Vasily Averin | 3ea4331 | 2015-06-25 15:01:47 -0700 | [diff] [blame] | 1436 | int do_syslog(int type, char __user *buf, int len, int source) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | { |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1438 | bool clear = false; |
Joe Perches | a39d4a8 | 2014-12-10 15:50:15 -0800 | [diff] [blame] | 1439 | static int saved_console_loglevel = LOGLEVEL_DEFAULT; |
Linus Torvalds | ee24aeb | 2011-02-10 17:53:55 -0800 | [diff] [blame] | 1440 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | |
Vasily Averin | 3ea4331 | 2015-06-25 15:01:47 -0700 | [diff] [blame] | 1442 | error = check_syslog_permissions(type, source); |
Linus Torvalds | ee24aeb | 2011-02-10 17:53:55 -0800 | [diff] [blame] | 1443 | if (error) |
Nikitas Angelinas | 077a1cc | 2017-07-29 20:36:36 -0700 | [diff] [blame] | 1444 | return error; |
Eric Paris | 12b3052 | 2010-11-15 18:36:29 -0500 | [diff] [blame] | 1445 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | switch (type) { |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 1447 | case SYSLOG_ACTION_CLOSE: /* Close log */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 1449 | case SYSLOG_ACTION_OPEN: /* Open log */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 1451 | case SYSLOG_ACTION_READ: /* Read from log */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | if (!buf || len < 0) |
Nikitas Angelinas | 077a1cc | 2017-07-29 20:36:36 -0700 | [diff] [blame] | 1453 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | if (!len) |
Nikitas Angelinas | 077a1cc | 2017-07-29 20:36:36 -0700 | [diff] [blame] | 1455 | return 0; |
| 1456 | if (!access_ok(VERIFY_WRITE, buf, len)) |
| 1457 | return -EFAULT; |
Yuanhan Liu | 4a77a5a | 2012-06-16 21:21:51 +0800 | [diff] [blame] | 1458 | error = wait_event_interruptible(log_wait, |
| 1459 | syslog_seq != log_next_seq); |
Kay Sievers | cb424ff | 2012-07-06 09:50:09 -0700 | [diff] [blame] | 1460 | if (error) |
Nikitas Angelinas | 077a1cc | 2017-07-29 20:36:36 -0700 | [diff] [blame] | 1461 | return error; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1462 | error = syslog_print(buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 1464 | /* Read/clear last kernel messages */ |
| 1465 | case SYSLOG_ACTION_READ_CLEAR: |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1466 | clear = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | /* FALL THRU */ |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 1468 | /* Read last kernel messages */ |
| 1469 | case SYSLOG_ACTION_READ_ALL: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | if (!buf || len < 0) |
Nikitas Angelinas | 077a1cc | 2017-07-29 20:36:36 -0700 | [diff] [blame] | 1471 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | if (!len) |
Nikitas Angelinas | 077a1cc | 2017-07-29 20:36:36 -0700 | [diff] [blame] | 1473 | return 0; |
| 1474 | if (!access_ok(VERIFY_WRITE, buf, len)) |
| 1475 | return -EFAULT; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1476 | error = syslog_print_all(buf, len, clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 1478 | /* Clear ring buffer */ |
| 1479 | case SYSLOG_ACTION_CLEAR: |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1480 | syslog_print_all(NULL, 0, true); |
Alan Stern | 4661e35 | 2012-06-22 17:12:19 -0400 | [diff] [blame] | 1481 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 1482 | /* Disable logging to console */ |
| 1483 | case SYSLOG_ACTION_CONSOLE_OFF: |
Joe Perches | a39d4a8 | 2014-12-10 15:50:15 -0800 | [diff] [blame] | 1484 | if (saved_console_loglevel == LOGLEVEL_DEFAULT) |
Frans Pop | 1aaad49 | 2009-07-06 13:31:48 +0200 | [diff] [blame] | 1485 | saved_console_loglevel = console_loglevel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | console_loglevel = minimum_console_loglevel; |
| 1487 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 1488 | /* Enable logging to console */ |
| 1489 | case SYSLOG_ACTION_CONSOLE_ON: |
Joe Perches | a39d4a8 | 2014-12-10 15:50:15 -0800 | [diff] [blame] | 1490 | if (saved_console_loglevel != LOGLEVEL_DEFAULT) { |
Frans Pop | 1aaad49 | 2009-07-06 13:31:48 +0200 | [diff] [blame] | 1491 | console_loglevel = saved_console_loglevel; |
Joe Perches | a39d4a8 | 2014-12-10 15:50:15 -0800 | [diff] [blame] | 1492 | saved_console_loglevel = LOGLEVEL_DEFAULT; |
Frans Pop | 1aaad49 | 2009-07-06 13:31:48 +0200 | [diff] [blame] | 1493 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 1495 | /* Set level of messages printed to console */ |
| 1496 | case SYSLOG_ACTION_CONSOLE_LEVEL: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | if (len < 1 || len > 8) |
Nikitas Angelinas | 077a1cc | 2017-07-29 20:36:36 -0700 | [diff] [blame] | 1498 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | if (len < minimum_console_loglevel) |
| 1500 | len = minimum_console_loglevel; |
| 1501 | console_loglevel = len; |
Frans Pop | 1aaad49 | 2009-07-06 13:31:48 +0200 | [diff] [blame] | 1502 | /* Implicitly re-enable logging to console */ |
Joe Perches | a39d4a8 | 2014-12-10 15:50:15 -0800 | [diff] [blame] | 1503 | saved_console_loglevel = LOGLEVEL_DEFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 1505 | /* Number of chars in the log buffer */ |
| 1506 | case SYSLOG_ACTION_SIZE_UNREAD: |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 1507 | logbuf_lock_irq(); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1508 | if (syslog_seq < log_first_seq) { |
| 1509 | /* messages are gone, move to first one */ |
| 1510 | syslog_seq = log_first_seq; |
| 1511 | syslog_idx = log_first_idx; |
Kay Sievers | eb02dac | 2012-07-09 10:05:10 -0700 | [diff] [blame] | 1512 | syslog_partial = 0; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1513 | } |
Vasily Averin | 3ea4331 | 2015-06-25 15:01:47 -0700 | [diff] [blame] | 1514 | if (source == SYSLOG_FROM_PROC) { |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1515 | /* |
| 1516 | * Short-cut for poll(/"proc/kmsg") which simply checks |
| 1517 | * for pending data, not the size; return the count of |
| 1518 | * records, not the length. |
| 1519 | */ |
Alex Elder | e97e126 | 2014-08-06 16:08:59 -0700 | [diff] [blame] | 1520 | error = log_next_seq - syslog_seq; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1521 | } else { |
Kay Sievers | 5becfb1 | 2012-07-09 12:15:42 -0700 | [diff] [blame] | 1522 | u64 seq = syslog_seq; |
| 1523 | u32 idx = syslog_idx; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1524 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1525 | while (seq < log_next_seq) { |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 1526 | struct printk_log *msg = log_from_idx(idx); |
Kay Sievers | 3ce9a7c | 2012-05-13 23:30:46 +0200 | [diff] [blame] | 1527 | |
Linus Torvalds | 5aa068e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 1528 | error += msg_print_text(msg, true, NULL, 0); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1529 | idx = log_next(idx); |
| 1530 | seq++; |
| 1531 | } |
Kay Sievers | eb02dac | 2012-07-09 10:05:10 -0700 | [diff] [blame] | 1532 | error -= syslog_partial; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1533 | } |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 1534 | logbuf_unlock_irq(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | break; |
Kees Cook | d78ca3c | 2010-02-03 15:37:13 -0800 | [diff] [blame] | 1536 | /* Size of the log buffer */ |
| 1537 | case SYSLOG_ACTION_SIZE_BUFFER: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | error = log_buf_len; |
| 1539 | break; |
| 1540 | default: |
| 1541 | error = -EINVAL; |
| 1542 | break; |
| 1543 | } |
Nikitas Angelinas | 077a1cc | 2017-07-29 20:36:36 -0700 | [diff] [blame] | 1544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | return error; |
| 1546 | } |
| 1547 | |
Heiko Carstens | 1e7bfb2 | 2009-01-14 14:14:29 +0100 | [diff] [blame] | 1548 | SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | { |
Kees Cook | 637241a | 2013-06-12 14:04:39 -0700 | [diff] [blame] | 1550 | return do_syslog(type, buf, len, SYSLOG_FROM_READER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | } |
| 1552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | * Call the console drivers, asking them to write out |
| 1555 | * log_buf[start] to log_buf[end - 1]. |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 1556 | * The console_lock must be held. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | */ |
Sergey Senozhatsky | d9c2352 | 2016-12-24 23:09:01 +0900 | [diff] [blame] | 1558 | 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] | 1559 | const char *text, size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | { |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1561 | struct console *con; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | |
Sergey Senozhatsky | fc98c3c | 2017-02-18 03:42:54 -0800 | [diff] [blame] | 1563 | trace_console_rcuidle(text, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1565 | if (!console_drivers) |
| 1566 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1568 | for_each_console(con) { |
| 1569 | if (exclusive_console && con != exclusive_console) |
| 1570 | continue; |
| 1571 | if (!(con->flags & CON_ENABLED)) |
| 1572 | continue; |
| 1573 | if (!con->write) |
| 1574 | continue; |
| 1575 | if (!cpu_online(smp_processor_id()) && |
| 1576 | !(con->flags & CON_ANYTIME)) |
| 1577 | continue; |
Tejun Heo | 6fe2935 | 2015-06-25 15:01:30 -0700 | [diff] [blame] | 1578 | if (con->flags & CON_EXTENDED) |
| 1579 | con->write(con, ext_text, ext_len); |
| 1580 | else |
| 1581 | con->write(con, text, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | } |
| 1584 | |
Dave Young | af91322 | 2009-09-22 16:43:33 -0700 | [diff] [blame] | 1585 | int printk_delay_msec __read_mostly; |
| 1586 | |
| 1587 | static inline void printk_delay(void) |
| 1588 | { |
| 1589 | if (unlikely(printk_delay_msec)) { |
| 1590 | int m = printk_delay_msec; |
| 1591 | |
| 1592 | while (m--) { |
| 1593 | mdelay(1); |
| 1594 | touch_nmi_watchdog(); |
| 1595 | } |
| 1596 | } |
| 1597 | } |
| 1598 | |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1599 | /* |
| 1600 | * Continuation lines are buffered, and not committed to the record buffer |
| 1601 | * until the line is complete, or a race forces it. The line fragments |
| 1602 | * though, are printed immediately to the consoles to ensure everything has |
| 1603 | * reached the console in case of a kernel crash. |
| 1604 | */ |
| 1605 | static struct cont { |
| 1606 | char buf[LOG_LINE_MAX]; |
| 1607 | size_t len; /* length == 0 means unused buffer */ |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1608 | struct task_struct *owner; /* task of first print*/ |
| 1609 | u64 ts_nsec; /* time of first print */ |
| 1610 | u8 level; /* log level of first message */ |
Alex Elder | 0b90fec | 2014-08-06 16:09:03 -0700 | [diff] [blame] | 1611 | u8 facility; /* log facility of first message */ |
Kay Sievers | eab0726 | 2012-07-16 18:35:30 -0700 | [diff] [blame] | 1612 | enum log_flags flags; /* prefix, newline flags */ |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1613 | } cont; |
| 1614 | |
Linus Torvalds | 5e46765 | 2016-10-09 11:53:00 -0700 | [diff] [blame] | 1615 | static void cont_flush(void) |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1616 | { |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1617 | if (cont.len == 0) |
| 1618 | return; |
Linus Torvalds | 5c2992e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 1619 | |
| 1620 | log_store(cont.facility, cont.level, cont.flags, cont.ts_nsec, |
| 1621 | NULL, 0, cont.buf, cont.len); |
| 1622 | cont.len = 0; |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1623 | } |
| 1624 | |
Linus Torvalds | 5e46765 | 2016-10-09 11:53:00 -0700 | [diff] [blame] | 1625 | static bool cont_add(int facility, int level, enum log_flags flags, const char *text, size_t len) |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1626 | { |
Tejun Heo | 6fe2935 | 2015-06-25 15:01:30 -0700 | [diff] [blame] | 1627 | /* |
| 1628 | * If ext consoles are present, flush and skip in-kernel |
| 1629 | * continuation. See nr_ext_console_drivers definition. Also, if |
| 1630 | * the line gets too long, split it up in separate records. |
| 1631 | */ |
| 1632 | if (nr_ext_console_drivers || cont.len + len > sizeof(cont.buf)) { |
Linus Torvalds | 5e46765 | 2016-10-09 11:53:00 -0700 | [diff] [blame] | 1633 | cont_flush(); |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1634 | return false; |
| 1635 | } |
| 1636 | |
| 1637 | if (!cont.len) { |
| 1638 | cont.facility = facility; |
| 1639 | cont.level = level; |
| 1640 | cont.owner = current; |
| 1641 | cont.ts_nsec = local_clock(); |
Linus Torvalds | 5e46765 | 2016-10-09 11:53:00 -0700 | [diff] [blame] | 1642 | cont.flags = flags; |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | memcpy(cont.buf + cont.len, text, len); |
| 1646 | cont.len += len; |
Kay Sievers | eab0726 | 2012-07-16 18:35:30 -0700 | [diff] [blame] | 1647 | |
Linus Torvalds | 5e46765 | 2016-10-09 11:53:00 -0700 | [diff] [blame] | 1648 | // The original flags come from the first line, |
| 1649 | // but later continuations can add a newline. |
| 1650 | if (flags & LOG_NEWLINE) { |
| 1651 | cont.flags |= LOG_NEWLINE; |
| 1652 | cont_flush(); |
| 1653 | } |
| 1654 | |
Kay Sievers | eab0726 | 2012-07-16 18:35:30 -0700 | [diff] [blame] | 1655 | if (cont.len > (sizeof(cont.buf) * 80) / 100) |
Linus Torvalds | 5e46765 | 2016-10-09 11:53:00 -0700 | [diff] [blame] | 1656 | cont_flush(); |
Kay Sievers | eab0726 | 2012-07-16 18:35:30 -0700 | [diff] [blame] | 1657 | |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 1658 | return true; |
| 1659 | } |
| 1660 | |
Linus Torvalds | c362c7f | 2016-10-08 22:02:09 -0700 | [diff] [blame] | 1661 | 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) |
| 1662 | { |
Linus Torvalds | c362c7f | 2016-10-08 22:02:09 -0700 | [diff] [blame] | 1663 | /* |
Linus Torvalds | 5e46765 | 2016-10-09 11:53:00 -0700 | [diff] [blame] | 1664 | * If an earlier line was buffered, and we're a continuation |
| 1665 | * write from the same process, try to add it to the buffer. |
Linus Torvalds | c362c7f | 2016-10-08 22:02:09 -0700 | [diff] [blame] | 1666 | */ |
| 1667 | if (cont.len) { |
Linus Torvalds | 5e46765 | 2016-10-09 11:53:00 -0700 | [diff] [blame] | 1668 | if (cont.owner == current && (lflags & LOG_CONT)) { |
| 1669 | if (cont_add(facility, level, lflags, text, text_len)) |
| 1670 | return text_len; |
| 1671 | } |
| 1672 | /* Otherwise, make sure it's flushed */ |
| 1673 | cont_flush(); |
| 1674 | } |
Linus Torvalds | c362c7f | 2016-10-08 22:02:09 -0700 | [diff] [blame] | 1675 | |
Linus Torvalds | 8835ca5 | 2016-10-19 09:11:24 -0700 | [diff] [blame] | 1676 | /* Skip empty continuation lines that couldn't be added - they just flush */ |
| 1677 | if (!text_len && (lflags & LOG_CONT)) |
| 1678 | return 0; |
| 1679 | |
Linus Torvalds | 5e46765 | 2016-10-09 11:53:00 -0700 | [diff] [blame] | 1680 | /* If it doesn't end in a newline, try to buffer the current line */ |
| 1681 | if (!(lflags & LOG_NEWLINE)) { |
| 1682 | if (cont_add(facility, level, lflags, text, text_len)) |
Linus Torvalds | c362c7f | 2016-10-08 22:02:09 -0700 | [diff] [blame] | 1683 | return text_len; |
| 1684 | } |
| 1685 | |
Linus Torvalds | 5e46765 | 2016-10-09 11:53:00 -0700 | [diff] [blame] | 1686 | /* Store it in the record log */ |
Linus Torvalds | c362c7f | 2016-10-08 22:02:09 -0700 | [diff] [blame] | 1687 | return log_store(facility, level, lflags, 0, dict, dictlen, text, text_len); |
| 1688 | } |
| 1689 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1690 | asmlinkage int vprintk_emit(int facility, int level, |
| 1691 | const char *dict, size_t dictlen, |
| 1692 | const char *fmt, va_list args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | { |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1694 | static char textbuf[LOG_LINE_MAX]; |
| 1695 | char *text = textbuf; |
Pierre Kuo | aec47ca | 2017-07-11 14:40:55 +0800 | [diff] [blame] | 1696 | size_t text_len; |
Kay Sievers | 5becfb1 | 2012-07-09 12:15:42 -0700 | [diff] [blame] | 1697 | enum log_flags lflags = 0; |
Nick Andrew | ac60ad7 | 2008-05-12 21:21:04 +0200 | [diff] [blame] | 1698 | unsigned long flags; |
Pierre Kuo | aec47ca | 2017-07-11 14:40:55 +0800 | [diff] [blame] | 1699 | int printed_len; |
Steven Rostedt | 458df9f | 2014-06-04 16:11:38 -0700 | [diff] [blame] | 1700 | bool in_sched = false; |
Jan Kara | 608873c | 2014-06-04 16:11:35 -0700 | [diff] [blame] | 1701 | |
Joe Perches | a39d4a8 | 2014-12-10 15:50:15 -0800 | [diff] [blame] | 1702 | if (level == LOGLEVEL_SCHED) { |
| 1703 | level = LOGLEVEL_DEFAULT; |
Steven Rostedt | 458df9f | 2014-06-04 16:11:38 -0700 | [diff] [blame] | 1704 | in_sched = true; |
| 1705 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | |
Andrew Cooks | 2fa72c8 | 2012-12-17 15:59:56 -0800 | [diff] [blame] | 1707 | boot_delay_msec(level); |
Dave Young | af91322 | 2009-09-22 16:43:33 -0700 | [diff] [blame] | 1708 | printk_delay(); |
Randy Dunlap | bfe8df3 | 2007-10-16 01:23:46 -0700 | [diff] [blame] | 1709 | |
Sergey Senozhatsky | a819937 | 2016-03-17 14:21:20 -0700 | [diff] [blame] | 1710 | /* This stops the holder of console_sem just where we want him */ |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 1711 | logbuf_lock_irqsave(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | /* |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1713 | * The printf needs to come first; we need the syslog |
| 1714 | * prefix which might be passed-in as a parameter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | */ |
Markus Trippelsdorf | 98e35f5 | 2014-10-13 15:51:13 -0700 | [diff] [blame] | 1716 | text_len = vscnprintf(text, sizeof(textbuf), fmt, args); |
Nick Andrew | ac60ad7 | 2008-05-12 21:21:04 +0200 | [diff] [blame] | 1717 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1718 | /* mark and strip a trailing newline */ |
Kay Sievers | c313af1 | 2012-05-14 20:46:27 +0200 | [diff] [blame] | 1719 | if (text_len && text[text_len-1] == '\n') { |
| 1720 | text_len--; |
Kay Sievers | 5becfb1 | 2012-07-09 12:15:42 -0700 | [diff] [blame] | 1721 | lflags |= LOG_NEWLINE; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1722 | } |
Kay Sievers | 9d90c8d | 2011-03-13 03:19:51 +0100 | [diff] [blame] | 1723 | |
Joe Perches | 088a52a | 2012-07-30 14:40:19 -0700 | [diff] [blame] | 1724 | /* strip kernel syslog prefix and extract log level or control flags */ |
| 1725 | if (facility == 0) { |
Linus Torvalds | 4bcc595 | 2016-10-08 20:32:40 -0700 | [diff] [blame] | 1726 | int kern_level; |
Joe Perches | 088a52a | 2012-07-30 14:40:19 -0700 | [diff] [blame] | 1727 | |
Linus Torvalds | 4bcc595 | 2016-10-08 20:32:40 -0700 | [diff] [blame] | 1728 | while ((kern_level = printk_get_level(text)) != 0) { |
Joe Perches | 088a52a | 2012-07-30 14:40:19 -0700 | [diff] [blame] | 1729 | switch (kern_level) { |
| 1730 | case '0' ... '7': |
Joe Perches | a39d4a8 | 2014-12-10 15:50:15 -0800 | [diff] [blame] | 1731 | if (level == LOGLEVEL_DEFAULT) |
Joe Perches | 088a52a | 2012-07-30 14:40:19 -0700 | [diff] [blame] | 1732 | level = kern_level - '0'; |
Joe Perches | a39d4a8 | 2014-12-10 15:50:15 -0800 | [diff] [blame] | 1733 | /* fallthrough */ |
Joe Perches | 088a52a | 2012-07-30 14:40:19 -0700 | [diff] [blame] | 1734 | case 'd': /* KERN_DEFAULT */ |
| 1735 | lflags |= LOG_PREFIX; |
Linus Torvalds | 4bcc595 | 2016-10-08 20:32:40 -0700 | [diff] [blame] | 1736 | break; |
| 1737 | case 'c': /* KERN_CONT */ |
| 1738 | lflags |= LOG_CONT; |
Joe Perches | 088a52a | 2012-07-30 14:40:19 -0700 | [diff] [blame] | 1739 | } |
Linus Torvalds | 4bcc595 | 2016-10-08 20:32:40 -0700 | [diff] [blame] | 1740 | |
| 1741 | text_len -= 2; |
| 1742 | text += 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | } |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1744 | } |
Nick Andrew | ac60ad7 | 2008-05-12 21:21:04 +0200 | [diff] [blame] | 1745 | |
Joe Perches | a39d4a8 | 2014-12-10 15:50:15 -0800 | [diff] [blame] | 1746 | if (level == LOGLEVEL_DEFAULT) |
Kay Sievers | c313af1 | 2012-05-14 20:46:27 +0200 | [diff] [blame] | 1747 | level = default_message_loglevel; |
| 1748 | |
Kay Sievers | 5becfb1 | 2012-07-09 12:15:42 -0700 | [diff] [blame] | 1749 | if (dict) |
| 1750 | lflags |= LOG_PREFIX|LOG_NEWLINE; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1751 | |
Pierre Kuo | aec47ca | 2017-07-11 14:40:55 +0800 | [diff] [blame] | 1752 | printed_len = log_output(facility, level, lflags, dict, dictlen, text, text_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 1754 | logbuf_unlock_irqrestore(flags); |
Jan Kara | 939f04b | 2014-06-04 16:11:37 -0700 | [diff] [blame] | 1755 | |
Steven Rostedt | 458df9f | 2014-06-04 16:11:38 -0700 | [diff] [blame] | 1756 | /* If called from the scheduler, we can not call up(). */ |
Andrew Morton | d18bbc2 | 2014-07-02 15:22:38 -0700 | [diff] [blame] | 1757 | if (!in_sched) { |
Jan Kara | 5874af2 | 2014-08-06 16:09:10 -0700 | [diff] [blame] | 1758 | /* |
Andrew Morton | d18bbc2 | 2014-07-02 15:22:38 -0700 | [diff] [blame] | 1759 | * Try to acquire and then immediately release the console |
| 1760 | * semaphore. The release will print out buffers and wake up |
| 1761 | * /dev/kmsg and syslog() users. |
| 1762 | */ |
Steven Rostedt (VMware) | dbdda84 | 2018-01-10 14:24:17 +0100 | [diff] [blame^] | 1763 | if (console_trylock()) { |
Andrew Morton | d18bbc2 | 2014-07-02 15:22:38 -0700 | [diff] [blame] | 1764 | console_unlock(); |
Steven Rostedt (VMware) | dbdda84 | 2018-01-10 14:24:17 +0100 | [diff] [blame^] | 1765 | } else { |
| 1766 | struct task_struct *owner = NULL; |
| 1767 | bool waiter; |
| 1768 | bool spin = false; |
| 1769 | |
| 1770 | printk_safe_enter_irqsave(flags); |
| 1771 | |
| 1772 | raw_spin_lock(&console_owner_lock); |
| 1773 | owner = READ_ONCE(console_owner); |
| 1774 | waiter = READ_ONCE(console_waiter); |
| 1775 | if (!waiter && owner && owner != current) { |
| 1776 | WRITE_ONCE(console_waiter, true); |
| 1777 | spin = true; |
| 1778 | } |
| 1779 | raw_spin_unlock(&console_owner_lock); |
| 1780 | |
| 1781 | /* |
| 1782 | * If there is an active printk() writing to the |
| 1783 | * consoles, instead of having it write our data too, |
| 1784 | * see if we can offload that load from the active |
| 1785 | * printer, and do some printing ourselves. |
| 1786 | * Go into a spin only if there isn't already a waiter |
| 1787 | * spinning, and there is an active printer, and |
| 1788 | * that active printer isn't us (recursive printk?). |
| 1789 | */ |
| 1790 | if (spin) { |
| 1791 | /* We spin waiting for the owner to release us */ |
| 1792 | spin_acquire(&console_owner_dep_map, 0, 0, _THIS_IP_); |
| 1793 | /* Owner will clear console_waiter on hand off */ |
| 1794 | while (READ_ONCE(console_waiter)) |
| 1795 | cpu_relax(); |
| 1796 | |
| 1797 | spin_release(&console_owner_dep_map, 1, _THIS_IP_); |
| 1798 | printk_safe_exit_irqrestore(flags); |
| 1799 | |
| 1800 | /* |
| 1801 | * The owner passed the console lock to us. |
| 1802 | * Since we did not spin on console lock, annotate |
| 1803 | * this as a trylock. Otherwise lockdep will |
| 1804 | * complain. |
| 1805 | */ |
| 1806 | mutex_acquire(&console_lock_dep_map, 0, 1, _THIS_IP_); |
| 1807 | console_unlock(); |
| 1808 | printk_safe_enter_irqsave(flags); |
| 1809 | } |
| 1810 | printk_safe_exit_irqrestore(flags); |
| 1811 | |
| 1812 | } |
Andrew Morton | d18bbc2 | 2014-07-02 15:22:38 -0700 | [diff] [blame] | 1813 | } |
Steven Rostedt | 458df9f | 2014-06-04 16:11:38 -0700 | [diff] [blame] | 1814 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | return printed_len; |
| 1816 | } |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1817 | EXPORT_SYMBOL(vprintk_emit); |
| 1818 | |
| 1819 | asmlinkage int vprintk(const char *fmt, va_list args) |
| 1820 | { |
Sergey Senozhatsky | bd66a89 | 2016-12-27 23:16:04 +0900 | [diff] [blame] | 1821 | return vprintk_func(fmt, args); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1822 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | EXPORT_SYMBOL(vprintk); |
| 1824 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1825 | asmlinkage int printk_emit(int facility, int level, |
| 1826 | const char *dict, size_t dictlen, |
| 1827 | const char *fmt, ...) |
| 1828 | { |
| 1829 | va_list args; |
| 1830 | int r; |
| 1831 | |
| 1832 | va_start(args, fmt); |
| 1833 | r = vprintk_emit(facility, level, dict, dictlen, fmt, args); |
| 1834 | va_end(args); |
| 1835 | |
| 1836 | return r; |
| 1837 | } |
| 1838 | EXPORT_SYMBOL(printk_emit); |
| 1839 | |
Linus Torvalds | a0cba21 | 2016-08-09 10:48:18 -0700 | [diff] [blame] | 1840 | int vprintk_default(const char *fmt, va_list args) |
Steven Rostedt (Red Hat) | afdc34a | 2014-06-19 17:33:31 -0400 | [diff] [blame] | 1841 | { |
| 1842 | int r; |
| 1843 | |
| 1844 | #ifdef CONFIG_KGDB_KDB |
Petr Mladek | 34aaff4 | 2016-12-14 15:05:58 -0800 | [diff] [blame] | 1845 | /* Allow to pass printk() to kdb but avoid a recursion. */ |
| 1846 | if (unlikely(kdb_trap_printk && kdb_printf_cpu < 0)) { |
Daniel Thompson | f7d4ca8 | 2014-11-07 18:37:57 +0000 | [diff] [blame] | 1847 | r = vkdb_printf(KDB_MSGSRC_PRINTK, fmt, args); |
Steven Rostedt (Red Hat) | afdc34a | 2014-06-19 17:33:31 -0400 | [diff] [blame] | 1848 | return r; |
| 1849 | } |
| 1850 | #endif |
Linus Torvalds | a0cba21 | 2016-08-09 10:48:18 -0700 | [diff] [blame] | 1851 | r = vprintk_emit(0, LOGLEVEL_DEFAULT, NULL, 0, fmt, args); |
Steven Rostedt (Red Hat) | afdc34a | 2014-06-19 17:33:31 -0400 | [diff] [blame] | 1852 | |
| 1853 | return r; |
| 1854 | } |
| 1855 | EXPORT_SYMBOL_GPL(vprintk_default); |
| 1856 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1857 | /** |
| 1858 | * printk - print a kernel message |
| 1859 | * @fmt: format string |
| 1860 | * |
| 1861 | * This is printk(). It can be called from any context. We want it to work. |
| 1862 | * |
| 1863 | * We try to grab the console_lock. If we succeed, it's easy - we log the |
| 1864 | * output and call the console drivers. If we fail to get the semaphore, we |
| 1865 | * place the output into the log buffer and return. The current holder of |
| 1866 | * the console_sem will notice the new output in console_unlock(); and will |
| 1867 | * send it to the consoles before releasing the lock. |
| 1868 | * |
| 1869 | * One effect of this deferred printing is that code which calls printk() and |
| 1870 | * then changes console_loglevel may break. This is because console_loglevel |
| 1871 | * is inspected when the actual printing occurs. |
| 1872 | * |
| 1873 | * See also: |
| 1874 | * printf(3) |
| 1875 | * |
| 1876 | * See the vsnprintf() documentation for format string extensions over C99. |
| 1877 | */ |
Andi Kleen | 722a9f9 | 2014-05-02 00:44:38 +0200 | [diff] [blame] | 1878 | asmlinkage __visible int printk(const char *fmt, ...) |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1879 | { |
| 1880 | va_list args; |
| 1881 | int r; |
| 1882 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1883 | va_start(args, fmt); |
Linus Torvalds | a0cba21 | 2016-08-09 10:48:18 -0700 | [diff] [blame] | 1884 | r = vprintk_func(fmt, args); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 1885 | va_end(args); |
| 1886 | |
| 1887 | return r; |
| 1888 | } |
| 1889 | EXPORT_SYMBOL(printk); |
Kay Sievers | 7f3a781 | 2012-05-09 01:37:51 +0200 | [diff] [blame] | 1890 | |
Kay Sievers | 96efedf | 2012-07-16 18:35:29 -0700 | [diff] [blame] | 1891 | #else /* CONFIG_PRINTK */ |
Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 1892 | |
Kay Sievers | 7049825 | 2012-07-16 18:35:29 -0700 | [diff] [blame] | 1893 | #define LOG_LINE_MAX 0 |
| 1894 | #define PREFIX_MAX 0 |
Alex Elder | 249771b | 2014-08-06 16:09:08 -0700 | [diff] [blame] | 1895 | |
Kay Sievers | 96efedf | 2012-07-16 18:35:29 -0700 | [diff] [blame] | 1896 | static u64 syslog_seq; |
| 1897 | static u32 syslog_idx; |
Kay Sievers | eab0726 | 2012-07-16 18:35:30 -0700 | [diff] [blame] | 1898 | static u64 console_seq; |
| 1899 | static u32 console_idx; |
Kay Sievers | 96efedf | 2012-07-16 18:35:29 -0700 | [diff] [blame] | 1900 | static u64 log_first_seq; |
| 1901 | static u32 log_first_idx; |
| 1902 | static u64 log_next_seq; |
Tejun Heo | 6fe2935 | 2015-06-25 15:01:30 -0700 | [diff] [blame] | 1903 | static char *log_text(const struct printk_log *msg) { return NULL; } |
| 1904 | static char *log_dict(const struct printk_log *msg) { return NULL; } |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 1905 | static struct printk_log *log_from_idx(u32 idx) { return NULL; } |
Kay Sievers | 7f3a781 | 2012-05-09 01:37:51 +0200 | [diff] [blame] | 1906 | static u32 log_next(u32 idx) { return 0; } |
Tejun Heo | 6fe2935 | 2015-06-25 15:01:30 -0700 | [diff] [blame] | 1907 | static ssize_t msg_print_ext_header(char *buf, size_t size, |
Linus Torvalds | 5aa068e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 1908 | struct printk_log *msg, |
| 1909 | u64 seq) { return 0; } |
Tejun Heo | 6fe2935 | 2015-06-25 15:01:30 -0700 | [diff] [blame] | 1910 | static ssize_t msg_print_ext_body(char *buf, size_t size, |
| 1911 | char *dict, size_t dict_len, |
| 1912 | char *text, size_t text_len) { return 0; } |
Sergey Senozhatsky | d9c2352 | 2016-12-24 23:09:01 +0900 | [diff] [blame] | 1913 | 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] | 1914 | const char *text, size_t len) {} |
Linus Torvalds | 5aa068e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 1915 | static size_t msg_print_text(const struct printk_log *msg, |
Kay Sievers | 5becfb1 | 2012-07-09 12:15:42 -0700 | [diff] [blame] | 1916 | bool syslog, char *buf, size_t size) { return 0; } |
Sergey Senozhatsky | cf77544 | 2016-08-02 14:03:56 -0700 | [diff] [blame] | 1917 | static bool suppress_message_printing(int level) { return false; } |
Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 1918 | |
Kay Sievers | 7f3a781 | 2012-05-09 01:37:51 +0200 | [diff] [blame] | 1919 | #endif /* CONFIG_PRINTK */ |
Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 1920 | |
Thomas Gleixner | d0380e6 | 2013-04-29 16:17:18 -0700 | [diff] [blame] | 1921 | #ifdef CONFIG_EARLY_PRINTK |
| 1922 | struct console *early_console; |
| 1923 | |
Andi Kleen | 722a9f9 | 2014-05-02 00:44:38 +0200 | [diff] [blame] | 1924 | asmlinkage __visible void early_printk(const char *fmt, ...) |
Thomas Gleixner | d0380e6 | 2013-04-29 16:17:18 -0700 | [diff] [blame] | 1925 | { |
| 1926 | va_list ap; |
Joe Perches | 1dc6244 | 2014-12-10 15:45:53 -0800 | [diff] [blame] | 1927 | char buf[512]; |
| 1928 | int n; |
| 1929 | |
| 1930 | if (!early_console) |
| 1931 | return; |
Thomas Gleixner | d0380e6 | 2013-04-29 16:17:18 -0700 | [diff] [blame] | 1932 | |
| 1933 | va_start(ap, fmt); |
Joe Perches | 1dc6244 | 2014-12-10 15:45:53 -0800 | [diff] [blame] | 1934 | n = vscnprintf(buf, sizeof(buf), fmt, ap); |
Thomas Gleixner | d0380e6 | 2013-04-29 16:17:18 -0700 | [diff] [blame] | 1935 | va_end(ap); |
Joe Perches | 1dc6244 | 2014-12-10 15:45:53 -0800 | [diff] [blame] | 1936 | |
| 1937 | early_console->write(early_console, buf, n); |
Thomas Gleixner | d0380e6 | 2013-04-29 16:17:18 -0700 | [diff] [blame] | 1938 | } |
| 1939 | #endif |
| 1940 | |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 1941 | static int __add_preferred_console(char *name, int idx, char *options, |
| 1942 | char *brl_options) |
| 1943 | { |
| 1944 | struct console_cmdline *c; |
| 1945 | int i; |
| 1946 | |
| 1947 | /* |
| 1948 | * See if this tty is not yet registered, and |
| 1949 | * if we have a slot free. |
| 1950 | */ |
Petr Mladek | dac8bbb | 2017-06-08 12:01:30 +0200 | [diff] [blame] | 1951 | for (i = 0, c = console_cmdline; |
| 1952 | i < MAX_CMDLINECONSOLES && c->name[0]; |
| 1953 | i++, c++) { |
Joe Perches | 2347540 | 2013-07-31 13:53:46 -0700 | [diff] [blame] | 1954 | if (strcmp(c->name, name) == 0 && c->index == idx) { |
Petr Mladek | dac8bbb | 2017-06-08 12:01:30 +0200 | [diff] [blame] | 1955 | if (!brl_options) |
| 1956 | preferred_console = i; |
Joe Perches | 2347540 | 2013-07-31 13:53:46 -0700 | [diff] [blame] | 1957 | return 0; |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 1958 | } |
Joe Perches | 2347540 | 2013-07-31 13:53:46 -0700 | [diff] [blame] | 1959 | } |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 1960 | if (i == MAX_CMDLINECONSOLES) |
| 1961 | return -E2BIG; |
| 1962 | if (!brl_options) |
Aleksey Makarov | ad86ee2 | 2017-03-15 13:28:51 +0300 | [diff] [blame] | 1963 | preferred_console = i; |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 1964 | strlcpy(c->name, name, sizeof(c->name)); |
| 1965 | c->options = options; |
Joe Perches | bbeddf5 | 2013-07-31 13:53:45 -0700 | [diff] [blame] | 1966 | braille_set_options(c, brl_options); |
| 1967 | |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 1968 | c->index = idx; |
| 1969 | return 0; |
| 1970 | } |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 1971 | /* |
Alex Elder | 0b90fec | 2014-08-06 16:09:03 -0700 | [diff] [blame] | 1972 | * Set up a console. Called via do_early_param() in init/main.c |
| 1973 | * for each "console=" parameter in the boot command line. |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 1974 | */ |
| 1975 | static int __init console_setup(char *str) |
| 1976 | { |
Alex Elder | 0b90fec | 2014-08-06 16:09:03 -0700 | [diff] [blame] | 1977 | char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for "ttyS" */ |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 1978 | char *s, *options, *brl_options = NULL; |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 1979 | int idx; |
| 1980 | |
Joe Perches | bbeddf5 | 2013-07-31 13:53:45 -0700 | [diff] [blame] | 1981 | if (_braille_console_setup(&str, &brl_options)) |
| 1982 | return 1; |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 1983 | |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 1984 | /* |
| 1985 | * Decode str into name, index, options. |
| 1986 | */ |
| 1987 | if (str[0] >= '0' && str[0] <= '9') { |
Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 1988 | strcpy(buf, "ttyS"); |
| 1989 | strncpy(buf + 4, str, sizeof(buf) - 5); |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 1990 | } else { |
Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 1991 | strncpy(buf, str, sizeof(buf) - 1); |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 1992 | } |
Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 1993 | buf[sizeof(buf) - 1] = 0; |
Alex Elder | 249771b | 2014-08-06 16:09:08 -0700 | [diff] [blame] | 1994 | options = strchr(str, ','); |
| 1995 | if (options) |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 1996 | *(options++) = 0; |
| 1997 | #ifdef __sparc__ |
| 1998 | if (!strcmp(str, "ttya")) |
Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 1999 | strcpy(buf, "ttyS0"); |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 2000 | if (!strcmp(str, "ttyb")) |
Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 2001 | strcpy(buf, "ttyS1"); |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 2002 | #endif |
Yinghai Lu | eaa944a | 2007-07-15 23:37:27 -0700 | [diff] [blame] | 2003 | for (s = buf; *s; s++) |
Alex Elder | 249771b | 2014-08-06 16:09:08 -0700 | [diff] [blame] | 2004 | if (isdigit(*s) || *s == ',') |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 2005 | break; |
| 2006 | idx = simple_strtoul(s, NULL, 10); |
| 2007 | *s = 0; |
| 2008 | |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 2009 | __add_preferred_console(buf, idx, options, brl_options); |
Markus Armbruster | 9e124fe | 2008-05-26 23:31:07 +0100 | [diff] [blame] | 2010 | console_set_on_cmdline = 1; |
John Z. Bohach | 2ea1c53 | 2006-03-24 03:18:19 -0800 | [diff] [blame] | 2011 | return 1; |
| 2012 | } |
| 2013 | __setup("console=", console_setup); |
| 2014 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | /** |
Matt Mackall | 3c0547b | 2005-05-16 21:53:47 -0700 | [diff] [blame] | 2016 | * add_preferred_console - add a device to the list of preferred consoles. |
Martin Waitz | ddad86c | 2005-11-13 16:08:14 -0800 | [diff] [blame] | 2017 | * @name: device name |
| 2018 | * @idx: device index |
| 2019 | * @options: options for this console |
Matt Mackall | 3c0547b | 2005-05-16 21:53:47 -0700 | [diff] [blame] | 2020 | * |
| 2021 | * The last preferred console added will be used for kernel messages |
| 2022 | * and stdin/out/err for init. Normally this is used by console_setup |
| 2023 | * above to handle user-supplied console arguments; however it can also |
| 2024 | * be used by arch-specific code either to override the user or more |
| 2025 | * commonly to provide a default console (ie from PROM variables) when |
| 2026 | * the user has not supplied one. |
| 2027 | */ |
David S. Miller | fb445ee | 2007-12-29 01:19:49 -0800 | [diff] [blame] | 2028 | int add_preferred_console(char *name, int idx, char *options) |
Matt Mackall | 3c0547b | 2005-05-16 21:53:47 -0700 | [diff] [blame] | 2029 | { |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 2030 | return __add_preferred_console(name, idx, options, NULL); |
Matt Mackall | 3c0547b | 2005-05-16 21:53:47 -0700 | [diff] [blame] | 2031 | } |
| 2032 | |
Neil Zhang | d25d9fe | 2014-08-06 16:09:12 -0700 | [diff] [blame] | 2033 | bool console_suspend_enabled = true; |
Andres Salomon | 8f4ce8c | 2007-10-18 03:04:50 -0700 | [diff] [blame] | 2034 | EXPORT_SYMBOL(console_suspend_enabled); |
| 2035 | |
| 2036 | static int __init console_suspend_disable(char *str) |
| 2037 | { |
Neil Zhang | d25d9fe | 2014-08-06 16:09:12 -0700 | [diff] [blame] | 2038 | console_suspend_enabled = false; |
Andres Salomon | 8f4ce8c | 2007-10-18 03:04:50 -0700 | [diff] [blame] | 2039 | return 1; |
| 2040 | } |
| 2041 | __setup("no_console_suspend", console_suspend_disable); |
Yanmin Zhang | 134620f | 2011-10-31 17:11:27 -0700 | [diff] [blame] | 2042 | module_param_named(console_suspend, console_suspend_enabled, |
| 2043 | bool, S_IRUGO | S_IWUSR); |
| 2044 | MODULE_PARM_DESC(console_suspend, "suspend console during suspend" |
| 2045 | " and hibernate operations"); |
Andres Salomon | 8f4ce8c | 2007-10-18 03:04:50 -0700 | [diff] [blame] | 2046 | |
Matt Mackall | 3c0547b | 2005-05-16 21:53:47 -0700 | [diff] [blame] | 2047 | /** |
Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 2048 | * suspend_console - suspend the console subsystem |
| 2049 | * |
| 2050 | * This disables printk() while we go into suspend states |
| 2051 | */ |
| 2052 | void suspend_console(void) |
| 2053 | { |
Andres Salomon | 8f4ce8c | 2007-10-18 03:04:50 -0700 | [diff] [blame] | 2054 | if (!console_suspend_enabled) |
| 2055 | return; |
Pavel Machek | 0d63081 | 2008-07-23 21:28:32 -0700 | [diff] [blame] | 2056 | printk("Suspending console(s) (use no_console_suspend to debug)\n"); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2057 | console_lock(); |
Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 2058 | console_suspended = 1; |
Jan Kara | bd8d7cf | 2014-06-04 16:11:36 -0700 | [diff] [blame] | 2059 | up_console_sem(); |
Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 2060 | } |
| 2061 | |
| 2062 | void resume_console(void) |
| 2063 | { |
Andres Salomon | 8f4ce8c | 2007-10-18 03:04:50 -0700 | [diff] [blame] | 2064 | if (!console_suspend_enabled) |
| 2065 | return; |
Jan Kara | bd8d7cf | 2014-06-04 16:11:36 -0700 | [diff] [blame] | 2066 | down_console_sem(); |
Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 2067 | console_suspended = 0; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2068 | console_unlock(); |
Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | /** |
Kevin Cernekee | 034260d | 2010-06-03 22:11:25 -0700 | [diff] [blame] | 2072 | * console_cpu_notify - print deferred console messages after CPU hotplug |
Sebastian Andrzej Siewior | 90b1488 | 2016-11-03 15:49:58 +0100 | [diff] [blame] | 2073 | * @cpu: unused |
Kevin Cernekee | 034260d | 2010-06-03 22:11:25 -0700 | [diff] [blame] | 2074 | * |
| 2075 | * 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] | 2076 | * will be printed on the console only if there are CON_ANYTIME consoles. |
| 2077 | * This function is called when a new CPU comes online (or fails to come |
| 2078 | * up) or goes offline. |
Kevin Cernekee | 034260d | 2010-06-03 22:11:25 -0700 | [diff] [blame] | 2079 | */ |
Sebastian Andrzej Siewior | 90b1488 | 2016-11-03 15:49:58 +0100 | [diff] [blame] | 2080 | static int console_cpu_notify(unsigned int cpu) |
Kevin Cernekee | 034260d | 2010-06-03 22:11:25 -0700 | [diff] [blame] | 2081 | { |
Thomas Gleixner | f97960f | 2016-11-17 17:31:55 +0100 | [diff] [blame] | 2082 | if (!cpuhp_tasks_frozen) { |
Sergey Senozhatsky | 64ca752 | 2017-01-21 19:47:29 +0900 | [diff] [blame] | 2083 | /* If trylock fails, someone else is doing the printing */ |
| 2084 | if (console_trylock()) |
| 2085 | console_unlock(); |
Kevin Cernekee | 034260d | 2010-06-03 22:11:25 -0700 | [diff] [blame] | 2086 | } |
Sebastian Andrzej Siewior | 90b1488 | 2016-11-03 15:49:58 +0100 | [diff] [blame] | 2087 | return 0; |
Kevin Cernekee | 034260d | 2010-06-03 22:11:25 -0700 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | /** |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2091 | * console_lock - lock the console system for exclusive use. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | * |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2093 | * Acquires a lock which guarantees that the caller has |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | * exclusive access to the console system and the console_drivers list. |
| 2095 | * |
| 2096 | * Can sleep, returns nothing. |
| 2097 | */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2098 | void console_lock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | { |
Daniel Vetter | 6b898c0 | 2012-09-17 23:03:31 +0000 | [diff] [blame] | 2100 | might_sleep(); |
| 2101 | |
Jan Kara | bd8d7cf | 2014-06-04 16:11:36 -0700 | [diff] [blame] | 2102 | down_console_sem(); |
Arve Hjønnevåg | 403f307 | 2009-02-14 02:07:24 +0100 | [diff] [blame] | 2103 | if (console_suspended) |
| 2104 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | console_locked = 1; |
| 2106 | console_may_schedule = 1; |
| 2107 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2108 | EXPORT_SYMBOL(console_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2110 | /** |
| 2111 | * console_trylock - try to lock the console system for exclusive use. |
| 2112 | * |
Alex Elder | 0b90fec | 2014-08-06 16:09:03 -0700 | [diff] [blame] | 2113 | * Try to acquire a lock which guarantees that the caller has exclusive |
| 2114 | * access to the console system and the console_drivers list. |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2115 | * |
| 2116 | * returns 1 on success, and 0 on failure to acquire the lock. |
| 2117 | */ |
| 2118 | int console_trylock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | { |
Jan Kara | bd8d7cf | 2014-06-04 16:11:36 -0700 | [diff] [blame] | 2120 | if (down_trylock_console_sem()) |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2121 | return 0; |
Arve Hjønnevåg | 403f307 | 2009-02-14 02:07:24 +0100 | [diff] [blame] | 2122 | if (console_suspended) { |
Jan Kara | bd8d7cf | 2014-06-04 16:11:36 -0700 | [diff] [blame] | 2123 | up_console_sem(); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2124 | return 0; |
Arve Hjønnevåg | 403f307 | 2009-02-14 02:07:24 +0100 | [diff] [blame] | 2125 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2126 | console_locked = 1; |
Sergey Senozhatsky | 6b97a20 | 2016-03-17 14:21:23 -0700 | [diff] [blame] | 2127 | /* |
| 2128 | * When PREEMPT_COUNT disabled we can't reliably detect if it's |
| 2129 | * safe to schedule (e.g. calling printk while holding a spin_lock), |
| 2130 | * because preempt_disable()/preempt_enable() are just barriers there |
| 2131 | * and preempt_count() is always 0. |
| 2132 | * |
| 2133 | * RCU read sections have a separate preemption counter when |
| 2134 | * PREEMPT_RCU enabled thus we must take extra care and check |
| 2135 | * rcu_preempt_depth(), otherwise RCU read sections modify |
| 2136 | * preempt_count(). |
| 2137 | */ |
| 2138 | console_may_schedule = !oops_in_progress && |
| 2139 | preemptible() && |
| 2140 | !rcu_preempt_depth(); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2141 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2143 | EXPORT_SYMBOL(console_trylock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | |
| 2145 | int is_console_locked(void) |
| 2146 | { |
| 2147 | return console_locked; |
| 2148 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2149 | |
Sergey Senozhatsky | a819937 | 2016-03-17 14:21:20 -0700 | [diff] [blame] | 2150 | /* |
| 2151 | * Check if we have any console that is capable of printing while cpu is |
| 2152 | * booting or shutting down. Requires console_sem. |
| 2153 | */ |
| 2154 | static int have_callable_console(void) |
| 2155 | { |
| 2156 | struct console *con; |
| 2157 | |
| 2158 | for_each_console(con) |
Sergey Senozhatsky | adaf659 | 2016-03-17 14:21:27 -0700 | [diff] [blame] | 2159 | if ((con->flags & CON_ENABLED) && |
| 2160 | (con->flags & CON_ANYTIME)) |
Sergey Senozhatsky | a819937 | 2016-03-17 14:21:20 -0700 | [diff] [blame] | 2161 | return 1; |
| 2162 | |
| 2163 | return 0; |
| 2164 | } |
| 2165 | |
| 2166 | /* |
| 2167 | * Can we actually use the console at this time on this cpu? |
| 2168 | * |
| 2169 | * Console drivers may assume that per-cpu resources have been allocated. So |
| 2170 | * unless they're explicitly marked as being able to cope (CON_ANYTIME) don't |
| 2171 | * call them until this CPU is officially up. |
| 2172 | */ |
| 2173 | static inline int can_use_console(void) |
| 2174 | { |
| 2175 | return cpu_online(raw_smp_processor_id()) || have_callable_console(); |
| 2176 | } |
| 2177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | /** |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2179 | * console_unlock - unlock the console system |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | * |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2181 | * Releases the console_lock which the caller holds on the console system |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | * and the console driver list. |
| 2183 | * |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2184 | * While the console_lock was held, console output may have been buffered |
| 2185 | * by printk(). If this is the case, console_unlock(); emits |
| 2186 | * the output prior to releasing the lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | * |
Kay Sievers | 7f3a781 | 2012-05-09 01:37:51 +0200 | [diff] [blame] | 2188 | * If there is output waiting, we wake /dev/kmsg and syslog() users. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | * |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2190 | * console_unlock(); may be called from any context. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2192 | void console_unlock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | { |
Tejun Heo | 6fe2935 | 2015-06-25 15:01:30 -0700 | [diff] [blame] | 2194 | static char ext_text[CONSOLE_EXT_LOG_MAX]; |
Kay Sievers | 7049825 | 2012-07-16 18:35:29 -0700 | [diff] [blame] | 2195 | static char text[LOG_LINE_MAX + PREFIX_MAX]; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2196 | static u64 seen_seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | unsigned long flags; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2198 | bool wake_klogd = false; |
Steven Rostedt (VMware) | dbdda84 | 2018-01-10 14:24:17 +0100 | [diff] [blame^] | 2199 | bool waiter = false; |
Tejun Heo | 8d91f8b | 2016-01-15 16:58:24 -0800 | [diff] [blame] | 2200 | bool do_cond_resched, retry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | |
Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 2202 | if (console_suspended) { |
Jan Kara | bd8d7cf | 2014-06-04 16:11:36 -0700 | [diff] [blame] | 2203 | up_console_sem(); |
Linus Torvalds | 557240b | 2006-06-19 18:16:01 -0700 | [diff] [blame] | 2204 | return; |
| 2205 | } |
Antonino A. Daplas | 78944e54 | 2006-08-05 12:14:16 -0700 | [diff] [blame] | 2206 | |
Tejun Heo | 8d91f8b | 2016-01-15 16:58:24 -0800 | [diff] [blame] | 2207 | /* |
Petr Mladek | 257ab44 | 2017-03-24 17:14:05 +0100 | [diff] [blame] | 2208 | * Console drivers are called with interrupts disabled, so |
Tejun Heo | 8d91f8b | 2016-01-15 16:58:24 -0800 | [diff] [blame] | 2209 | * @console_may_schedule should be cleared before; however, we may |
| 2210 | * end up dumping a lot of lines, for example, if called from |
| 2211 | * console registration path, and should invoke cond_resched() |
| 2212 | * between lines if allowable. Not doing so can cause a very long |
| 2213 | * scheduling stall on a slow console leading to RCU stall and |
| 2214 | * softlockup warnings which exacerbate the issue with more |
| 2215 | * messages practically incapacitating the system. |
Petr Mladek | 257ab44 | 2017-03-24 17:14:05 +0100 | [diff] [blame] | 2216 | * |
| 2217 | * console_trylock() is not able to detect the preemptive |
| 2218 | * context reliably. Therefore the value must be stored before |
| 2219 | * and cleared after the the "again" goto label. |
Tejun Heo | 8d91f8b | 2016-01-15 16:58:24 -0800 | [diff] [blame] | 2220 | */ |
| 2221 | do_cond_resched = console_may_schedule; |
Petr Mladek | 257ab44 | 2017-03-24 17:14:05 +0100 | [diff] [blame] | 2222 | again: |
Antonino A. Daplas | 78944e54 | 2006-08-05 12:14:16 -0700 | [diff] [blame] | 2223 | console_may_schedule = 0; |
| 2224 | |
Sergey Senozhatsky | a819937 | 2016-03-17 14:21:20 -0700 | [diff] [blame] | 2225 | /* |
| 2226 | * We released the console_sem lock, so we need to recheck if |
| 2227 | * cpu is online and (if not) is there at least one CON_ANYTIME |
| 2228 | * console. |
| 2229 | */ |
| 2230 | if (!can_use_console()) { |
| 2231 | console_locked = 0; |
| 2232 | up_console_sem(); |
| 2233 | return; |
| 2234 | } |
| 2235 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2236 | for (;;) { |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 2237 | struct printk_log *msg; |
Tejun Heo | 6fe2935 | 2015-06-25 15:01:30 -0700 | [diff] [blame] | 2238 | size_t ext_len = 0; |
Kay Sievers | 3ce9a7c | 2012-05-13 23:30:46 +0200 | [diff] [blame] | 2239 | size_t len; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2240 | |
Sergey Senozhatsky | f975237 | 2016-12-27 23:16:09 +0900 | [diff] [blame] | 2241 | printk_safe_enter_irqsave(flags); |
| 2242 | raw_spin_lock(&logbuf_lock); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2243 | if (seen_seq != log_next_seq) { |
| 2244 | wake_klogd = true; |
| 2245 | seen_seq = log_next_seq; |
| 2246 | } |
| 2247 | |
| 2248 | if (console_seq < log_first_seq) { |
Maxim Akristiniy | 9afe77e | 2017-10-23 19:51:48 +0300 | [diff] [blame] | 2249 | len = sprintf(text, "** %u printk messages dropped **\n", |
Will Deacon | 84b5ec8 | 2014-06-04 16:11:45 -0700 | [diff] [blame] | 2250 | (unsigned)(log_first_seq - console_seq)); |
| 2251 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2252 | /* messages are gone, move to first one */ |
| 2253 | console_seq = log_first_seq; |
| 2254 | console_idx = log_first_idx; |
Will Deacon | 84b5ec8 | 2014-06-04 16:11:45 -0700 | [diff] [blame] | 2255 | } else { |
| 2256 | len = 0; |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2257 | } |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 2258 | skip: |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2259 | if (console_seq == log_next_seq) |
| 2260 | break; |
| 2261 | |
| 2262 | msg = log_from_idx(console_idx); |
Sergey Senozhatsky | d9c2352 | 2016-12-24 23:09:01 +0900 | [diff] [blame] | 2263 | if (suppress_message_printing(msg->level)) { |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 2264 | /* |
| 2265 | * Skip record we have buffered and already printed |
Sergey Senozhatsky | cf77544 | 2016-08-02 14:03:56 -0700 | [diff] [blame] | 2266 | * directly to the console when we received it, and |
| 2267 | * record that has level above the console loglevel. |
Kay Sievers | 084681d | 2012-06-28 09:38:53 +0200 | [diff] [blame] | 2268 | */ |
| 2269 | console_idx = log_next(console_idx); |
| 2270 | console_seq++; |
| 2271 | goto skip; |
| 2272 | } |
Kay Sievers | 649e6ee | 2012-05-10 04:30:45 +0200 | [diff] [blame] | 2273 | |
Linus Torvalds | 5aa068e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 2274 | len += msg_print_text(msg, false, text + len, sizeof(text) - len); |
Tejun Heo | 6fe2935 | 2015-06-25 15:01:30 -0700 | [diff] [blame] | 2275 | if (nr_ext_console_drivers) { |
| 2276 | ext_len = msg_print_ext_header(ext_text, |
| 2277 | sizeof(ext_text), |
Linus Torvalds | 5aa068e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 2278 | msg, console_seq); |
Tejun Heo | 6fe2935 | 2015-06-25 15:01:30 -0700 | [diff] [blame] | 2279 | ext_len += msg_print_ext_body(ext_text + ext_len, |
| 2280 | sizeof(ext_text) - ext_len, |
| 2281 | log_dict(msg), msg->dict_len, |
| 2282 | log_text(msg), msg->text_len); |
| 2283 | } |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2284 | console_idx = log_next(console_idx); |
| 2285 | console_seq++; |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 2286 | raw_spin_unlock(&logbuf_lock); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2287 | |
Steven Rostedt (VMware) | dbdda84 | 2018-01-10 14:24:17 +0100 | [diff] [blame^] | 2288 | /* |
| 2289 | * While actively printing out messages, if another printk() |
| 2290 | * were to occur on another CPU, it may wait for this one to |
| 2291 | * finish. This task can not be preempted if there is a |
| 2292 | * waiter waiting to take over. |
| 2293 | */ |
| 2294 | raw_spin_lock(&console_owner_lock); |
| 2295 | console_owner = current; |
| 2296 | raw_spin_unlock(&console_owner_lock); |
| 2297 | |
| 2298 | /* The waiter may spin on us after setting console_owner */ |
| 2299 | spin_acquire(&console_owner_dep_map, 0, 0, _THIS_IP_); |
| 2300 | |
Steven Rostedt | 81d68a9 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2301 | stop_critical_timings(); /* don't trace print latency */ |
Sergey Senozhatsky | d9c2352 | 2016-12-24 23:09:01 +0900 | [diff] [blame] | 2302 | call_console_drivers(ext_text, ext_len, text, len); |
Steven Rostedt | 81d68a9 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 2303 | start_critical_timings(); |
Steven Rostedt (VMware) | dbdda84 | 2018-01-10 14:24:17 +0100 | [diff] [blame^] | 2304 | |
| 2305 | raw_spin_lock(&console_owner_lock); |
| 2306 | waiter = READ_ONCE(console_waiter); |
| 2307 | console_owner = NULL; |
| 2308 | raw_spin_unlock(&console_owner_lock); |
| 2309 | |
| 2310 | /* |
| 2311 | * If there is a waiter waiting for us, then pass the |
| 2312 | * rest of the work load over to that waiter. |
| 2313 | */ |
| 2314 | if (waiter) |
| 2315 | break; |
| 2316 | |
| 2317 | /* There was no waiter, and nothing will spin on us here */ |
| 2318 | spin_release(&console_owner_dep_map, 1, _THIS_IP_); |
| 2319 | |
Sergey Senozhatsky | f975237 | 2016-12-27 23:16:09 +0900 | [diff] [blame] | 2320 | printk_safe_exit_irqrestore(flags); |
Tejun Heo | 8d91f8b | 2016-01-15 16:58:24 -0800 | [diff] [blame] | 2321 | |
| 2322 | if (do_cond_resched) |
| 2323 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | } |
Steven Rostedt (VMware) | dbdda84 | 2018-01-10 14:24:17 +0100 | [diff] [blame^] | 2325 | |
| 2326 | /* |
| 2327 | * If there is an active waiter waiting on the console_lock. |
| 2328 | * Pass off the printing to the waiter, and the waiter |
| 2329 | * will continue printing on its CPU, and when all writing |
| 2330 | * has finished, the last printer will wake up klogd. |
| 2331 | */ |
| 2332 | if (waiter) { |
| 2333 | WRITE_ONCE(console_waiter, false); |
| 2334 | /* The waiter is now free to continue */ |
| 2335 | spin_release(&console_owner_dep_map, 1, _THIS_IP_); |
| 2336 | /* |
| 2337 | * Hand off console_lock to waiter. The waiter will perform |
| 2338 | * the up(). After this, the waiter is the console_lock owner. |
| 2339 | */ |
| 2340 | mutex_release(&console_lock_dep_map, 1, _THIS_IP_); |
| 2341 | printk_safe_exit_irqrestore(flags); |
| 2342 | /* Note, if waiter is set, logbuf_lock is not held */ |
| 2343 | return; |
| 2344 | } |
| 2345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2346 | console_locked = 0; |
Feng Tang | fe3d8ad | 2011-03-22 16:34:21 -0700 | [diff] [blame] | 2347 | |
| 2348 | /* Release the exclusive_console once it is used */ |
| 2349 | if (unlikely(exclusive_console)) |
| 2350 | exclusive_console = NULL; |
| 2351 | |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 2352 | raw_spin_unlock(&logbuf_lock); |
Peter Zijlstra | 4f2a8d3 | 2011-06-22 11:20:09 +0200 | [diff] [blame] | 2353 | |
Jan Kara | bd8d7cf | 2014-06-04 16:11:36 -0700 | [diff] [blame] | 2354 | up_console_sem(); |
Peter Zijlstra | 4f2a8d3 | 2011-06-22 11:20:09 +0200 | [diff] [blame] | 2355 | |
| 2356 | /* |
| 2357 | * Someone could have filled up the buffer again, so re-check if there's |
| 2358 | * something to flush. In case we cannot trylock the console_sem again, |
| 2359 | * there's a new owner and the console_unlock() from them will do the |
| 2360 | * flush, no worries. |
| 2361 | */ |
Thomas Gleixner | 07354eb | 2009-07-25 17:50:36 +0200 | [diff] [blame] | 2362 | raw_spin_lock(&logbuf_lock); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2363 | retry = console_seq != log_next_seq; |
Sergey Senozhatsky | f975237 | 2016-12-27 23:16:09 +0900 | [diff] [blame] | 2364 | raw_spin_unlock(&logbuf_lock); |
| 2365 | printk_safe_exit_irqrestore(flags); |
Peter Zijlstra | 09dc3cf | 2011-12-08 14:34:13 -0800 | [diff] [blame] | 2366 | |
Peter Zijlstra | 4f2a8d3 | 2011-06-22 11:20:09 +0200 | [diff] [blame] | 2367 | if (retry && console_trylock()) |
| 2368 | goto again; |
| 2369 | |
Kirill Korotaev | e3e8a75 | 2007-02-10 01:46:19 -0800 | [diff] [blame] | 2370 | if (wake_klogd) |
| 2371 | wake_up_klogd(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2372 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2373 | EXPORT_SYMBOL(console_unlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2374 | |
Martin Waitz | ddad86c | 2005-11-13 16:08:14 -0800 | [diff] [blame] | 2375 | /** |
| 2376 | * console_conditional_schedule - yield the CPU if required |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | * |
| 2378 | * If the console code is currently allowed to sleep, and |
| 2379 | * if this CPU should yield the CPU to another task, do |
| 2380 | * so here. |
| 2381 | * |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2382 | * Must be called within console_lock();. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2383 | */ |
| 2384 | void __sched console_conditional_schedule(void) |
| 2385 | { |
| 2386 | if (console_may_schedule) |
| 2387 | cond_resched(); |
| 2388 | } |
| 2389 | EXPORT_SYMBOL(console_conditional_schedule); |
| 2390 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2391 | void console_unblank(void) |
| 2392 | { |
| 2393 | struct console *c; |
| 2394 | |
| 2395 | /* |
| 2396 | * console_unblank can no longer be called in interrupt context unless |
| 2397 | * oops_in_progress is set to 1.. |
| 2398 | */ |
| 2399 | if (oops_in_progress) { |
Jan Kara | bd8d7cf | 2014-06-04 16:11:36 -0700 | [diff] [blame] | 2400 | if (down_trylock_console_sem() != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2401 | return; |
| 2402 | } else |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2403 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | |
| 2405 | console_locked = 1; |
| 2406 | console_may_schedule = 0; |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2407 | for_each_console(c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | if ((c->flags & CON_ENABLED) && c->unblank) |
| 2409 | c->unblank(); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2410 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2411 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2412 | |
Tejun Heo | 8d91f8b | 2016-01-15 16:58:24 -0800 | [diff] [blame] | 2413 | /** |
| 2414 | * console_flush_on_panic - flush console content on panic |
| 2415 | * |
| 2416 | * Immediately output all pending messages no matter what. |
| 2417 | */ |
| 2418 | void console_flush_on_panic(void) |
| 2419 | { |
| 2420 | /* |
| 2421 | * If someone else is holding the console lock, trylock will fail |
| 2422 | * and may_schedule may be set. Ignore and proceed to unlock so |
| 2423 | * that messages are flushed out. As this can be called from any |
| 2424 | * context and we don't want to get preempted while flushing, |
| 2425 | * ensure may_schedule is cleared. |
| 2426 | */ |
| 2427 | console_trylock(); |
| 2428 | console_may_schedule = 0; |
| 2429 | console_unlock(); |
| 2430 | } |
| 2431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2432 | /* |
| 2433 | * Return the console tty driver structure and its associated index |
| 2434 | */ |
| 2435 | struct tty_driver *console_device(int *index) |
| 2436 | { |
| 2437 | struct console *c; |
| 2438 | struct tty_driver *driver = NULL; |
| 2439 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2440 | console_lock(); |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2441 | for_each_console(c) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | if (!c->device) |
| 2443 | continue; |
| 2444 | driver = c->device(c, index); |
| 2445 | if (driver) |
| 2446 | break; |
| 2447 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2448 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | return driver; |
| 2450 | } |
| 2451 | |
| 2452 | /* |
| 2453 | * Prevent further output on the passed console device so that (for example) |
| 2454 | * serial drivers can disable console output before suspending a port, and can |
| 2455 | * re-enable output afterwards. |
| 2456 | */ |
| 2457 | void console_stop(struct console *console) |
| 2458 | { |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2459 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | console->flags &= ~CON_ENABLED; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2461 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2462 | } |
| 2463 | EXPORT_SYMBOL(console_stop); |
| 2464 | |
| 2465 | void console_start(struct console *console) |
| 2466 | { |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2467 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2468 | console->flags |= CON_ENABLED; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2469 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2470 | } |
| 2471 | EXPORT_SYMBOL(console_start); |
| 2472 | |
Fabio M. Di Nitto | 7bf6939 | 2011-03-22 16:34:20 -0700 | [diff] [blame] | 2473 | static int __read_mostly keep_bootcon; |
| 2474 | |
| 2475 | static int __init keep_bootcon_setup(char *str) |
| 2476 | { |
| 2477 | keep_bootcon = 1; |
Andrew Morton | 27083ba | 2013-11-12 15:08:50 -0800 | [diff] [blame] | 2478 | pr_info("debug: skip boot console de-registration.\n"); |
Fabio M. Di Nitto | 7bf6939 | 2011-03-22 16:34:20 -0700 | [diff] [blame] | 2479 | |
| 2480 | return 0; |
| 2481 | } |
| 2482 | |
| 2483 | early_param("keep_bootcon", keep_bootcon_setup); |
| 2484 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | /* |
| 2486 | * The console driver calls this routine during kernel initialization |
| 2487 | * to register the console printing procedure with printk() and to |
| 2488 | * print any messages that were printed by the kernel before the |
| 2489 | * console driver was initialized. |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2490 | * |
| 2491 | * This can happen pretty early during the boot process (because of |
| 2492 | * early_printk) - sometimes before setup_arch() completes - be careful |
| 2493 | * of what kernel features are used - they may not be initialised yet. |
| 2494 | * |
| 2495 | * There are two types of consoles - bootconsoles (early_printk) and |
| 2496 | * "real" consoles (everything which is not a bootconsole) which are |
| 2497 | * handled differently. |
| 2498 | * - Any number of bootconsoles can be registered at any time. |
| 2499 | * - As soon as a "real" console is registered, all bootconsoles |
| 2500 | * will be unregistered automatically. |
| 2501 | * - Once a "real" console is registered, any attempt to register a |
| 2502 | * bootconsoles will be rejected |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 | */ |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2504 | void register_console(struct console *newcon) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2505 | { |
Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 2506 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2507 | unsigned long flags; |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2508 | struct console *bcon = NULL; |
Joe Perches | 2347540 | 2013-07-31 13:53:46 -0700 | [diff] [blame] | 2509 | struct console_cmdline *c; |
Aleksey Makarov | b077baf | 2017-03-15 13:28:50 +0300 | [diff] [blame] | 2510 | static bool has_preferred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2511 | |
Andreas Bießmann | 16cf48a | 2013-08-02 12:23:34 +0200 | [diff] [blame] | 2512 | if (console_drivers) |
| 2513 | for_each_console(bcon) |
| 2514 | if (WARN(bcon == newcon, |
| 2515 | "console '%s%d' already registered\n", |
| 2516 | bcon->name, bcon->index)) |
| 2517 | return; |
| 2518 | |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2519 | /* |
| 2520 | * before we register a new CON_BOOT console, make sure we don't |
| 2521 | * already have a valid console |
| 2522 | */ |
| 2523 | if (console_drivers && newcon->flags & CON_BOOT) { |
| 2524 | /* find the last or real console */ |
| 2525 | for_each_console(bcon) { |
| 2526 | if (!(bcon->flags & CON_BOOT)) { |
Andrew Morton | 27083ba | 2013-11-12 15:08:50 -0800 | [diff] [blame] | 2527 | pr_info("Too late to register bootconsole %s%d\n", |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2528 | newcon->name, newcon->index); |
| 2529 | return; |
| 2530 | } |
| 2531 | } |
Gerd Hoffmann | 69331af | 2007-05-08 00:26:49 -0700 | [diff] [blame] | 2532 | } |
| 2533 | |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2534 | if (console_drivers && console_drivers->flags & CON_BOOT) |
| 2535 | bcon = console_drivers; |
| 2536 | |
Aleksey Makarov | b077baf | 2017-03-15 13:28:50 +0300 | [diff] [blame] | 2537 | if (!has_preferred || bcon || !console_drivers) |
Aleksey Makarov | ad86ee2 | 2017-03-15 13:28:51 +0300 | [diff] [blame] | 2538 | has_preferred = preferred_console >= 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2539 | |
| 2540 | /* |
| 2541 | * See if we want to use this console driver. If we |
| 2542 | * didn't select a console we take the first one |
| 2543 | * that registers here. |
| 2544 | */ |
Aleksey Makarov | b077baf | 2017-03-15 13:28:50 +0300 | [diff] [blame] | 2545 | if (!has_preferred) { |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2546 | if (newcon->index < 0) |
| 2547 | newcon->index = 0; |
| 2548 | if (newcon->setup == NULL || |
| 2549 | newcon->setup(newcon, NULL) == 0) { |
| 2550 | newcon->flags |= CON_ENABLED; |
| 2551 | if (newcon->device) { |
| 2552 | newcon->flags |= CON_CONSDEV; |
Aleksey Makarov | b077baf | 2017-03-15 13:28:50 +0300 | [diff] [blame] | 2553 | has_preferred = true; |
Jan Kiszka | cd3a1b8 | 2008-05-12 21:21:04 +0200 | [diff] [blame] | 2554 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2555 | } |
| 2556 | } |
| 2557 | |
| 2558 | /* |
Petr Mladek | dac8bbb | 2017-06-08 12:01:30 +0200 | [diff] [blame] | 2559 | * See if this console matches one we selected on |
| 2560 | * the command line. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2561 | */ |
Petr Mladek | dac8bbb | 2017-06-08 12:01:30 +0200 | [diff] [blame] | 2562 | for (i = 0, c = console_cmdline; |
| 2563 | i < MAX_CMDLINECONSOLES && c->name[0]; |
| 2564 | i++, c++) { |
Peter Hurley | c7cef0a | 2015-03-09 16:27:12 -0400 | [diff] [blame] | 2565 | if (!newcon->match || |
| 2566 | newcon->match(newcon, c->name, c->index, c->options) != 0) { |
| 2567 | /* default matching */ |
| 2568 | BUILD_BUG_ON(sizeof(c->name) != sizeof(newcon->name)); |
| 2569 | if (strcmp(c->name, newcon->name) != 0) |
| 2570 | continue; |
| 2571 | if (newcon->index >= 0 && |
| 2572 | newcon->index != c->index) |
| 2573 | continue; |
| 2574 | if (newcon->index < 0) |
| 2575 | newcon->index = c->index; |
Joe Perches | bbeddf5 | 2013-07-31 13:53:45 -0700 | [diff] [blame] | 2576 | |
Peter Hurley | c7cef0a | 2015-03-09 16:27:12 -0400 | [diff] [blame] | 2577 | if (_braille_register_console(newcon, c)) |
| 2578 | return; |
Joe Perches | bbeddf5 | 2013-07-31 13:53:45 -0700 | [diff] [blame] | 2579 | |
Peter Hurley | c7cef0a | 2015-03-09 16:27:12 -0400 | [diff] [blame] | 2580 | if (newcon->setup && |
| 2581 | newcon->setup(newcon, c->options) != 0) |
| 2582 | break; |
| 2583 | } |
| 2584 | |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2585 | newcon->flags |= CON_ENABLED; |
Aleksey Makarov | ad86ee2 | 2017-03-15 13:28:51 +0300 | [diff] [blame] | 2586 | if (i == preferred_console) { |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2587 | newcon->flags |= CON_CONSDEV; |
Aleksey Makarov | b077baf | 2017-03-15 13:28:50 +0300 | [diff] [blame] | 2588 | has_preferred = true; |
Greg Edwards | ab4af03 | 2005-06-23 00:09:05 -0700 | [diff] [blame] | 2589 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2590 | break; |
| 2591 | } |
| 2592 | |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2593 | if (!(newcon->flags & CON_ENABLED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2594 | return; |
| 2595 | |
Robin Getz | 8259cf4 | 2009-07-09 13:08:37 -0400 | [diff] [blame] | 2596 | /* |
| 2597 | * If we have a bootconsole, and are switching to a real console, |
| 2598 | * don't print everything out again, since when the boot console, and |
| 2599 | * the real console are the same physical device, it's annoying to |
| 2600 | * see the beginning boot messages twice |
| 2601 | */ |
| 2602 | if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV)) |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2603 | newcon->flags &= ~CON_PRINTBUFFER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2604 | |
| 2605 | /* |
| 2606 | * Put this console in the list - keep the |
| 2607 | * preferred driver at the head of the list. |
| 2608 | */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2609 | console_lock(); |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2610 | if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) { |
| 2611 | newcon->next = console_drivers; |
| 2612 | console_drivers = newcon; |
| 2613 | if (newcon->next) |
| 2614 | newcon->next->flags &= ~CON_CONSDEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2615 | } else { |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2616 | newcon->next = console_drivers->next; |
| 2617 | console_drivers->next = newcon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2618 | } |
Tejun Heo | 6fe2935 | 2015-06-25 15:01:30 -0700 | [diff] [blame] | 2619 | |
| 2620 | if (newcon->flags & CON_EXTENDED) |
| 2621 | if (!nr_ext_console_drivers++) |
| 2622 | pr_info("printk: continuation disabled due to ext consoles, expect more fragments in /dev/kmsg\n"); |
| 2623 | |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2624 | if (newcon->flags & CON_PRINTBUFFER) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2625 | /* |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2626 | * console_unlock(); will print out the buffered messages |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2627 | * for us. |
| 2628 | */ |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 2629 | logbuf_lock_irqsave(flags); |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2630 | console_seq = syslog_seq; |
| 2631 | console_idx = syslog_idx; |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 2632 | logbuf_unlock_irqrestore(flags); |
Feng Tang | fe3d8ad | 2011-03-22 16:34:21 -0700 | [diff] [blame] | 2633 | /* |
| 2634 | * We're about to replay the log buffer. Only do this to the |
| 2635 | * just-registered console to avoid excessive message spam to |
| 2636 | * the already-registered consoles. |
| 2637 | */ |
| 2638 | exclusive_console = newcon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2639 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2640 | console_unlock(); |
Kay Sievers | fbc92a3 | 2010-12-01 18:51:05 +0100 | [diff] [blame] | 2641 | console_sysfs_notify(); |
Robin Getz | 8259cf4 | 2009-07-09 13:08:37 -0400 | [diff] [blame] | 2642 | |
| 2643 | /* |
| 2644 | * By unregistering the bootconsoles after we enable the real console |
| 2645 | * we get the "console xxx enabled" message on all the consoles - |
| 2646 | * boot consoles, real consoles, etc - this is to ensure that end |
| 2647 | * users know there might be something in the kernel's log buffer that |
| 2648 | * went to the bootconsole (that they do not see on the real console) |
| 2649 | */ |
Andrew Morton | 27083ba | 2013-11-12 15:08:50 -0800 | [diff] [blame] | 2650 | pr_info("%sconsole [%s%d] enabled\n", |
Kees Cook | 6b80239 | 2013-11-12 15:08:49 -0800 | [diff] [blame] | 2651 | (newcon->flags & CON_BOOT) ? "boot" : "" , |
| 2652 | newcon->name, newcon->index); |
Fabio M. Di Nitto | 7bf6939 | 2011-03-22 16:34:20 -0700 | [diff] [blame] | 2653 | if (bcon && |
| 2654 | ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) && |
| 2655 | !keep_bootcon) { |
Kees Cook | 6b80239 | 2013-11-12 15:08:49 -0800 | [diff] [blame] | 2656 | /* We need to iterate through all boot consoles, to make |
| 2657 | * sure we print everything out, before we unregister them. |
Robin Getz | 8259cf4 | 2009-07-09 13:08:37 -0400 | [diff] [blame] | 2658 | */ |
Robin Getz | 8259cf4 | 2009-07-09 13:08:37 -0400 | [diff] [blame] | 2659 | for_each_console(bcon) |
| 2660 | if (bcon->flags & CON_BOOT) |
| 2661 | unregister_console(bcon); |
Robin Getz | 8259cf4 | 2009-07-09 13:08:37 -0400 | [diff] [blame] | 2662 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2663 | } |
| 2664 | EXPORT_SYMBOL(register_console); |
| 2665 | |
Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 2666 | int unregister_console(struct console *console) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2667 | { |
Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 2668 | struct console *a, *b; |
Joe Perches | bbeddf5 | 2013-07-31 13:53:45 -0700 | [diff] [blame] | 2669 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2670 | |
Andrew Morton | 27083ba | 2013-11-12 15:08:50 -0800 | [diff] [blame] | 2671 | pr_info("%sconsole [%s%d] disabled\n", |
Kees Cook | 6b80239 | 2013-11-12 15:08:49 -0800 | [diff] [blame] | 2672 | (console->flags & CON_BOOT) ? "boot" : "" , |
| 2673 | console->name, console->index); |
| 2674 | |
Joe Perches | bbeddf5 | 2013-07-31 13:53:45 -0700 | [diff] [blame] | 2675 | res = _braille_unregister_console(console); |
| 2676 | if (res) |
| 2677 | return res; |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 2678 | |
Joe Perches | bbeddf5 | 2013-07-31 13:53:45 -0700 | [diff] [blame] | 2679 | res = 1; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2680 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2681 | if (console_drivers == console) { |
| 2682 | console_drivers=console->next; |
| 2683 | res = 0; |
Benjamin Herrenschmidt | e9b15b5 | 2005-11-23 13:37:44 -0800 | [diff] [blame] | 2684 | } else if (console_drivers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2685 | for (a=console_drivers->next, b=console_drivers ; |
| 2686 | a; b=a, a=b->next) { |
| 2687 | if (a == console) { |
| 2688 | b->next = a->next; |
| 2689 | res = 0; |
| 2690 | break; |
Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 2691 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2692 | } |
| 2693 | } |
Jesper Juhl | 40dc565 | 2005-10-30 15:02:46 -0800 | [diff] [blame] | 2694 | |
Tejun Heo | 6fe2935 | 2015-06-25 15:01:30 -0700 | [diff] [blame] | 2695 | if (!res && (console->flags & CON_EXTENDED)) |
| 2696 | nr_ext_console_drivers--; |
| 2697 | |
Gerd Hoffmann | 69331af | 2007-05-08 00:26:49 -0700 | [diff] [blame] | 2698 | /* |
Greg Edwards | ab4af03 | 2005-06-23 00:09:05 -0700 | [diff] [blame] | 2699 | * If this isn't the last console and it has CON_CONSDEV set, we |
| 2700 | * need to set it on the next preferred console. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2701 | */ |
Gerd Hoffmann | 69331af | 2007-05-08 00:26:49 -0700 | [diff] [blame] | 2702 | if (console_drivers != NULL && console->flags & CON_CONSDEV) |
Greg Edwards | ab4af03 | 2005-06-23 00:09:05 -0700 | [diff] [blame] | 2703 | console_drivers->flags |= CON_CONSDEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2704 | |
Stephen Chivers | 7fa21dd | 2014-05-14 08:04:39 +1000 | [diff] [blame] | 2705 | console->flags &= ~CON_ENABLED; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2706 | console_unlock(); |
Kay Sievers | fbc92a3 | 2010-12-01 18:51:05 +0100 | [diff] [blame] | 2707 | console_sysfs_notify(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2708 | return res; |
| 2709 | } |
| 2710 | EXPORT_SYMBOL(unregister_console); |
Matt Mackall | d59745c | 2005-05-01 08:59:02 -0700 | [diff] [blame] | 2711 | |
Thierry Reding | 81cc26f | 2016-01-15 16:58:21 -0800 | [diff] [blame] | 2712 | /* |
Nicolas Pitre | 0c68861 | 2017-04-12 18:37:14 -0400 | [diff] [blame] | 2713 | * Initialize the console device. This is called *early*, so |
| 2714 | * we can't necessarily depend on lots of kernel help here. |
| 2715 | * Just do some early initializations, and do the complex setup |
| 2716 | * later. |
| 2717 | */ |
| 2718 | void __init console_init(void) |
| 2719 | { |
| 2720 | initcall_t *call; |
| 2721 | |
| 2722 | /* Setup the default TTY line discipline. */ |
| 2723 | n_tty_init(); |
| 2724 | |
| 2725 | /* |
| 2726 | * set up the console device so that later boot sequences can |
| 2727 | * inform about problems etc.. |
| 2728 | */ |
| 2729 | call = __con_initcall_start; |
| 2730 | while (call < __con_initcall_end) { |
| 2731 | (*call)(); |
| 2732 | call++; |
| 2733 | } |
| 2734 | } |
| 2735 | |
| 2736 | /* |
Thierry Reding | 81cc26f | 2016-01-15 16:58:21 -0800 | [diff] [blame] | 2737 | * Some boot consoles access data that is in the init section and which will |
| 2738 | * be discarded after the initcalls have been run. To make sure that no code |
| 2739 | * will access this data, unregister the boot consoles in a late initcall. |
| 2740 | * |
| 2741 | * If for some reason, such as deferred probe or the driver being a loadable |
| 2742 | * module, the real console hasn't registered yet at this point, there will |
| 2743 | * be a brief interval in which no messages are logged to the console, which |
| 2744 | * makes it difficult to diagnose problems that occur during this time. |
| 2745 | * |
| 2746 | * To mitigate this problem somewhat, only unregister consoles whose memory |
Matt Redfearn | 2b1be68 | 2017-07-14 14:51:12 +0200 | [diff] [blame] | 2747 | * intersects with the init section. Note that all other boot consoles will |
| 2748 | * get unregistred when the real preferred console is registered. |
Thierry Reding | 81cc26f | 2016-01-15 16:58:21 -0800 | [diff] [blame] | 2749 | */ |
Kevin Cernekee | 034260d | 2010-06-03 22:11:25 -0700 | [diff] [blame] | 2750 | static int __init printk_late_init(void) |
Robin Getz | 0c5564b | 2007-08-20 15:22:47 -0400 | [diff] [blame] | 2751 | { |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2752 | struct console *con; |
Sebastian Andrzej Siewior | 90b1488 | 2016-11-03 15:49:58 +0100 | [diff] [blame] | 2753 | int ret; |
Robin Getz | 4d09161 | 2009-07-01 21:08:37 -0400 | [diff] [blame] | 2754 | |
| 2755 | for_each_console(con) { |
Petr Mladek | 5a81423 | 2017-07-14 14:51:13 +0200 | [diff] [blame] | 2756 | if (!(con->flags & CON_BOOT)) |
| 2757 | continue; |
| 2758 | |
| 2759 | /* Check addresses that might be used for enabled consoles. */ |
| 2760 | if (init_section_intersects(con, sizeof(*con)) || |
| 2761 | init_section_contains(con->write, 0) || |
| 2762 | init_section_contains(con->read, 0) || |
| 2763 | init_section_contains(con->device, 0) || |
| 2764 | init_section_contains(con->unblank, 0) || |
| 2765 | init_section_contains(con->data, 0)) { |
Thierry Reding | 81cc26f | 2016-01-15 16:58:21 -0800 | [diff] [blame] | 2766 | /* |
Matt Redfearn | 2b1be68 | 2017-07-14 14:51:12 +0200 | [diff] [blame] | 2767 | * Please, consider moving the reported consoles out |
| 2768 | * of the init section. |
Thierry Reding | 81cc26f | 2016-01-15 16:58:21 -0800 | [diff] [blame] | 2769 | */ |
Matt Redfearn | 2b1be68 | 2017-07-14 14:51:12 +0200 | [diff] [blame] | 2770 | pr_warn("bootconsole [%s%d] uses init memory and must be disabled even before the real one is ready\n", |
| 2771 | con->name, con->index); |
| 2772 | unregister_console(con); |
Robin Getz | cb00e99 | 2007-08-21 23:14:58 -0400 | [diff] [blame] | 2773 | } |
Robin Getz | 0c5564b | 2007-08-20 15:22:47 -0400 | [diff] [blame] | 2774 | } |
Sebastian Andrzej Siewior | 90b1488 | 2016-11-03 15:49:58 +0100 | [diff] [blame] | 2775 | ret = cpuhp_setup_state_nocalls(CPUHP_PRINTK_DEAD, "printk:dead", NULL, |
| 2776 | console_cpu_notify); |
| 2777 | WARN_ON(ret < 0); |
| 2778 | ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "printk:online", |
| 2779 | console_cpu_notify, NULL); |
| 2780 | WARN_ON(ret < 0); |
Robin Getz | 0c5564b | 2007-08-20 15:22:47 -0400 | [diff] [blame] | 2781 | return 0; |
| 2782 | } |
Kevin Cernekee | 034260d | 2010-06-03 22:11:25 -0700 | [diff] [blame] | 2783 | late_initcall(printk_late_init); |
Robin Getz | 0c5564b | 2007-08-20 15:22:47 -0400 | [diff] [blame] | 2784 | |
Joe Perches | 7ef3d2f | 2008-02-08 04:21:25 -0800 | [diff] [blame] | 2785 | #if defined CONFIG_PRINTK |
Frederic Weisbecker | dc72c32 | 2013-03-22 15:04:39 -0700 | [diff] [blame] | 2786 | /* |
| 2787 | * Delayed printk version, for scheduler-internal messages: |
| 2788 | */ |
Frederic Weisbecker | dc72c32 | 2013-03-22 15:04:39 -0700 | [diff] [blame] | 2789 | #define PRINTK_PENDING_WAKEUP 0x01 |
Steven Rostedt | 458df9f | 2014-06-04 16:11:38 -0700 | [diff] [blame] | 2790 | #define PRINTK_PENDING_OUTPUT 0x02 |
Frederic Weisbecker | dc72c32 | 2013-03-22 15:04:39 -0700 | [diff] [blame] | 2791 | |
| 2792 | static DEFINE_PER_CPU(int, printk_pending); |
Frederic Weisbecker | dc72c32 | 2013-03-22 15:04:39 -0700 | [diff] [blame] | 2793 | |
| 2794 | static void wake_up_klogd_work_func(struct irq_work *irq_work) |
| 2795 | { |
| 2796 | int pending = __this_cpu_xchg(printk_pending, 0); |
| 2797 | |
Steven Rostedt | 458df9f | 2014-06-04 16:11:38 -0700 | [diff] [blame] | 2798 | if (pending & PRINTK_PENDING_OUTPUT) { |
| 2799 | /* If trylock fails, someone else is doing the printing */ |
| 2800 | if (console_trylock()) |
| 2801 | console_unlock(); |
Frederic Weisbecker | dc72c32 | 2013-03-22 15:04:39 -0700 | [diff] [blame] | 2802 | } |
| 2803 | |
| 2804 | if (pending & PRINTK_PENDING_WAKEUP) |
| 2805 | wake_up_interruptible(&log_wait); |
| 2806 | } |
| 2807 | |
| 2808 | static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) = { |
| 2809 | .func = wake_up_klogd_work_func, |
| 2810 | .flags = IRQ_WORK_LAZY, |
| 2811 | }; |
| 2812 | |
| 2813 | void wake_up_klogd(void) |
| 2814 | { |
| 2815 | preempt_disable(); |
| 2816 | if (waitqueue_active(&log_wait)) { |
| 2817 | this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP); |
Christoph Lameter | bb964a9 | 2014-08-17 12:30:24 -0500 | [diff] [blame] | 2818 | irq_work_queue(this_cpu_ptr(&wake_up_klogd_work)); |
Frederic Weisbecker | dc72c32 | 2013-03-22 15:04:39 -0700 | [diff] [blame] | 2819 | } |
| 2820 | preempt_enable(); |
| 2821 | } |
Dave Young | 717115e | 2008-07-25 01:45:58 -0700 | [diff] [blame] | 2822 | |
Petr Mladek | 719f6a7 | 2017-04-20 10:52:31 +0200 | [diff] [blame] | 2823 | int vprintk_deferred(const char *fmt, va_list args) |
| 2824 | { |
| 2825 | int r; |
| 2826 | |
| 2827 | r = vprintk_emit(0, LOGLEVEL_SCHED, NULL, 0, fmt, args); |
| 2828 | |
| 2829 | preempt_disable(); |
| 2830 | __this_cpu_or(printk_pending, PRINTK_PENDING_OUTPUT); |
| 2831 | irq_work_queue(this_cpu_ptr(&wake_up_klogd_work)); |
| 2832 | preempt_enable(); |
| 2833 | |
| 2834 | return r; |
| 2835 | } |
| 2836 | |
John Stultz | aac74dc | 2014-06-04 16:11:40 -0700 | [diff] [blame] | 2837 | int printk_deferred(const char *fmt, ...) |
Peter Zijlstra | 600e145 | 2012-03-15 12:35:37 +0100 | [diff] [blame] | 2838 | { |
Peter Zijlstra | 600e145 | 2012-03-15 12:35:37 +0100 | [diff] [blame] | 2839 | va_list args; |
Peter Zijlstra | 600e145 | 2012-03-15 12:35:37 +0100 | [diff] [blame] | 2840 | int r; |
| 2841 | |
Peter Zijlstra | 600e145 | 2012-03-15 12:35:37 +0100 | [diff] [blame] | 2842 | va_start(args, fmt); |
Petr Mladek | 719f6a7 | 2017-04-20 10:52:31 +0200 | [diff] [blame] | 2843 | r = vprintk_deferred(fmt, args); |
Peter Zijlstra | 600e145 | 2012-03-15 12:35:37 +0100 | [diff] [blame] | 2844 | va_end(args); |
| 2845 | |
Peter Zijlstra | 600e145 | 2012-03-15 12:35:37 +0100 | [diff] [blame] | 2846 | return r; |
| 2847 | } |
| 2848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2849 | /* |
| 2850 | * printk rate limiting, lifted from the networking subsystem. |
| 2851 | * |
Uwe Kleine-König | 641de9d | 2008-07-29 22:33:38 -0700 | [diff] [blame] | 2852 | * This enforces a rate limit: not more than 10 kernel messages |
| 2853 | * every 5s to make a denial-of-service attack impossible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2854 | */ |
Uwe Kleine-König | 641de9d | 2008-07-29 22:33:38 -0700 | [diff] [blame] | 2855 | DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10); |
| 2856 | |
Christian Borntraeger | 5c82871 | 2009-10-23 14:58:11 +0200 | [diff] [blame] | 2857 | int __printk_ratelimit(const char *func) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2858 | { |
Christian Borntraeger | 5c82871 | 2009-10-23 14:58:11 +0200 | [diff] [blame] | 2859 | return ___ratelimit(&printk_ratelimit_state, func); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2860 | } |
Christian Borntraeger | 5c82871 | 2009-10-23 14:58:11 +0200 | [diff] [blame] | 2861 | EXPORT_SYMBOL(__printk_ratelimit); |
Andrew Morton | f46c483 | 2006-11-02 22:07:16 -0800 | [diff] [blame] | 2862 | |
| 2863 | /** |
| 2864 | * printk_timed_ratelimit - caller-controlled printk ratelimiting |
| 2865 | * @caller_jiffies: pointer to caller's state |
| 2866 | * @interval_msecs: minimum interval between prints |
| 2867 | * |
| 2868 | * printk_timed_ratelimit() returns true if more than @interval_msecs |
| 2869 | * milliseconds have elapsed since the last time printk_timed_ratelimit() |
| 2870 | * returned true. |
| 2871 | */ |
| 2872 | bool printk_timed_ratelimit(unsigned long *caller_jiffies, |
| 2873 | unsigned int interval_msecs) |
| 2874 | { |
Alex Elder | 249771b | 2014-08-06 16:09:08 -0700 | [diff] [blame] | 2875 | unsigned long elapsed = jiffies - *caller_jiffies; |
| 2876 | |
| 2877 | if (*caller_jiffies && elapsed <= msecs_to_jiffies(interval_msecs)) |
| 2878 | return false; |
| 2879 | |
| 2880 | *caller_jiffies = jiffies; |
| 2881 | return true; |
Andrew Morton | f46c483 | 2006-11-02 22:07:16 -0800 | [diff] [blame] | 2882 | } |
| 2883 | EXPORT_SYMBOL(printk_timed_ratelimit); |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 2884 | |
| 2885 | static DEFINE_SPINLOCK(dump_list_lock); |
| 2886 | static LIST_HEAD(dump_list); |
| 2887 | |
| 2888 | /** |
| 2889 | * kmsg_dump_register - register a kernel log dumper. |
Randy Dunlap | 6485536 | 2009-12-17 15:27:27 -0800 | [diff] [blame] | 2890 | * @dumper: pointer to the kmsg_dumper structure |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 2891 | * |
| 2892 | * Adds a kernel log dumper to the system. The dump callback in the |
| 2893 | * structure will be called when the kernel oopses or panics and must be |
| 2894 | * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise. |
| 2895 | */ |
| 2896 | int kmsg_dump_register(struct kmsg_dumper *dumper) |
| 2897 | { |
| 2898 | unsigned long flags; |
| 2899 | int err = -EBUSY; |
| 2900 | |
| 2901 | /* The dump callback needs to be set */ |
| 2902 | if (!dumper->dump) |
| 2903 | return -EINVAL; |
| 2904 | |
| 2905 | spin_lock_irqsave(&dump_list_lock, flags); |
| 2906 | /* Don't allow registering multiple times */ |
| 2907 | if (!dumper->registered) { |
| 2908 | dumper->registered = 1; |
Huang Ying | fb842b00 | 2011-01-12 16:59:43 -0800 | [diff] [blame] | 2909 | list_add_tail_rcu(&dumper->list, &dump_list); |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 2910 | err = 0; |
| 2911 | } |
| 2912 | spin_unlock_irqrestore(&dump_list_lock, flags); |
| 2913 | |
| 2914 | return err; |
| 2915 | } |
| 2916 | EXPORT_SYMBOL_GPL(kmsg_dump_register); |
| 2917 | |
| 2918 | /** |
| 2919 | * kmsg_dump_unregister - unregister a kmsg dumper. |
Randy Dunlap | 6485536 | 2009-12-17 15:27:27 -0800 | [diff] [blame] | 2920 | * @dumper: pointer to the kmsg_dumper structure |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 2921 | * |
| 2922 | * Removes a dump device from the system. Returns zero on success and |
| 2923 | * %-EINVAL otherwise. |
| 2924 | */ |
| 2925 | int kmsg_dump_unregister(struct kmsg_dumper *dumper) |
| 2926 | { |
| 2927 | unsigned long flags; |
| 2928 | int err = -EINVAL; |
| 2929 | |
| 2930 | spin_lock_irqsave(&dump_list_lock, flags); |
| 2931 | if (dumper->registered) { |
| 2932 | dumper->registered = 0; |
Huang Ying | fb842b00 | 2011-01-12 16:59:43 -0800 | [diff] [blame] | 2933 | list_del_rcu(&dumper->list); |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 2934 | err = 0; |
| 2935 | } |
| 2936 | spin_unlock_irqrestore(&dump_list_lock, flags); |
Huang Ying | fb842b00 | 2011-01-12 16:59:43 -0800 | [diff] [blame] | 2937 | synchronize_rcu(); |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 2938 | |
| 2939 | return err; |
| 2940 | } |
| 2941 | EXPORT_SYMBOL_GPL(kmsg_dump_unregister); |
| 2942 | |
Kay Sievers | 7ff9554 | 2012-05-03 02:29:13 +0200 | [diff] [blame] | 2943 | static bool always_kmsg_dump; |
| 2944 | module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR); |
| 2945 | |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 2946 | /** |
| 2947 | * kmsg_dump - dump kernel log to kernel message dumpers. |
| 2948 | * @reason: the reason (oops, panic etc) for dumping |
| 2949 | * |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 2950 | * Call each of the registered dumper's dump() callback, which can |
| 2951 | * retrieve the kmsg records with kmsg_dump_get_line() or |
| 2952 | * kmsg_dump_get_buffer(). |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 2953 | */ |
| 2954 | void kmsg_dump(enum kmsg_dump_reason reason) |
| 2955 | { |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 2956 | struct kmsg_dumper *dumper; |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 2957 | unsigned long flags; |
| 2958 | |
Matthew Garrett | c22ab332 | 2012-03-05 14:59:10 -0800 | [diff] [blame] | 2959 | if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump) |
| 2960 | return; |
| 2961 | |
Huang Ying | fb842b00 | 2011-01-12 16:59:43 -0800 | [diff] [blame] | 2962 | rcu_read_lock(); |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 2963 | list_for_each_entry_rcu(dumper, &dump_list, list) { |
| 2964 | if (dumper->max_reason && reason > dumper->max_reason) |
| 2965 | continue; |
| 2966 | |
| 2967 | /* initialize iterator with data about the stored records */ |
| 2968 | dumper->active = true; |
| 2969 | |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 2970 | logbuf_lock_irqsave(flags); |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 2971 | dumper->cur_seq = clear_seq; |
| 2972 | dumper->cur_idx = clear_idx; |
| 2973 | dumper->next_seq = log_next_seq; |
| 2974 | dumper->next_idx = log_next_idx; |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 2975 | logbuf_unlock_irqrestore(flags); |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 2976 | |
| 2977 | /* invoke dumper which will iterate over records */ |
| 2978 | dumper->dump(dumper, reason); |
| 2979 | |
| 2980 | /* reset iterator */ |
| 2981 | dumper->active = false; |
| 2982 | } |
Huang Ying | fb842b00 | 2011-01-12 16:59:43 -0800 | [diff] [blame] | 2983 | rcu_read_unlock(); |
Simon Kagstrom | 456b565 | 2009-10-16 14:09:18 +0200 | [diff] [blame] | 2984 | } |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 2985 | |
| 2986 | /** |
Anton Vorontsov | 533827c | 2012-07-20 17:28:07 -0700 | [diff] [blame] | 2987 | * kmsg_dump_get_line_nolock - retrieve one kmsg log line (unlocked version) |
| 2988 | * @dumper: registered kmsg dumper |
| 2989 | * @syslog: include the "<4>" prefixes |
| 2990 | * @line: buffer to copy the line to |
| 2991 | * @size: maximum size of the buffer |
| 2992 | * @len: length of line placed into buffer |
| 2993 | * |
| 2994 | * Start at the beginning of the kmsg buffer, with the oldest kmsg |
| 2995 | * record, and copy one record into the provided buffer. |
| 2996 | * |
| 2997 | * Consecutive calls will return the next available record moving |
| 2998 | * towards the end of the buffer with the youngest messages. |
| 2999 | * |
| 3000 | * A return value of FALSE indicates that there are no more records to |
| 3001 | * read. |
| 3002 | * |
| 3003 | * The function is similar to kmsg_dump_get_line(), but grabs no locks. |
| 3004 | */ |
| 3005 | bool kmsg_dump_get_line_nolock(struct kmsg_dumper *dumper, bool syslog, |
| 3006 | char *line, size_t size, size_t *len) |
| 3007 | { |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 3008 | struct printk_log *msg; |
Anton Vorontsov | 533827c | 2012-07-20 17:28:07 -0700 | [diff] [blame] | 3009 | size_t l = 0; |
| 3010 | bool ret = false; |
| 3011 | |
| 3012 | if (!dumper->active) |
| 3013 | goto out; |
| 3014 | |
| 3015 | if (dumper->cur_seq < log_first_seq) { |
| 3016 | /* messages are gone, move to first available one */ |
| 3017 | dumper->cur_seq = log_first_seq; |
| 3018 | dumper->cur_idx = log_first_idx; |
| 3019 | } |
| 3020 | |
| 3021 | /* last entry */ |
| 3022 | if (dumper->cur_seq >= log_next_seq) |
| 3023 | goto out; |
| 3024 | |
| 3025 | msg = log_from_idx(dumper->cur_idx); |
Linus Torvalds | 5aa068e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 3026 | l = msg_print_text(msg, syslog, line, size); |
Anton Vorontsov | 533827c | 2012-07-20 17:28:07 -0700 | [diff] [blame] | 3027 | |
| 3028 | dumper->cur_idx = log_next(dumper->cur_idx); |
| 3029 | dumper->cur_seq++; |
| 3030 | ret = true; |
| 3031 | out: |
| 3032 | if (len) |
| 3033 | *len = l; |
| 3034 | return ret; |
| 3035 | } |
| 3036 | |
| 3037 | /** |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3038 | * kmsg_dump_get_line - retrieve one kmsg log line |
| 3039 | * @dumper: registered kmsg dumper |
| 3040 | * @syslog: include the "<4>" prefixes |
| 3041 | * @line: buffer to copy the line to |
| 3042 | * @size: maximum size of the buffer |
| 3043 | * @len: length of line placed into buffer |
| 3044 | * |
| 3045 | * Start at the beginning of the kmsg buffer, with the oldest kmsg |
| 3046 | * record, and copy one record into the provided buffer. |
| 3047 | * |
| 3048 | * Consecutive calls will return the next available record moving |
| 3049 | * towards the end of the buffer with the youngest messages. |
| 3050 | * |
| 3051 | * A return value of FALSE indicates that there are no more records to |
| 3052 | * read. |
| 3053 | */ |
| 3054 | bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog, |
| 3055 | char *line, size_t size, size_t *len) |
| 3056 | { |
| 3057 | unsigned long flags; |
Anton Vorontsov | 533827c | 2012-07-20 17:28:07 -0700 | [diff] [blame] | 3058 | bool ret; |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3059 | |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 3060 | logbuf_lock_irqsave(flags); |
Anton Vorontsov | 533827c | 2012-07-20 17:28:07 -0700 | [diff] [blame] | 3061 | ret = kmsg_dump_get_line_nolock(dumper, syslog, line, size, len); |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 3062 | logbuf_unlock_irqrestore(flags); |
Anton Vorontsov | 533827c | 2012-07-20 17:28:07 -0700 | [diff] [blame] | 3063 | |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3064 | return ret; |
| 3065 | } |
| 3066 | EXPORT_SYMBOL_GPL(kmsg_dump_get_line); |
| 3067 | |
| 3068 | /** |
| 3069 | * kmsg_dump_get_buffer - copy kmsg log lines |
| 3070 | * @dumper: registered kmsg dumper |
| 3071 | * @syslog: include the "<4>" prefixes |
Randy Dunlap | 4f0f4af | 2012-06-30 15:37:24 -0700 | [diff] [blame] | 3072 | * @buf: buffer to copy the line to |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3073 | * @size: maximum size of the buffer |
| 3074 | * @len: length of line placed into buffer |
| 3075 | * |
| 3076 | * Start at the end of the kmsg buffer and fill the provided buffer |
| 3077 | * with as many of the the *youngest* kmsg records that fit into it. |
| 3078 | * If the buffer is large enough, all available kmsg records will be |
| 3079 | * copied with a single call. |
| 3080 | * |
| 3081 | * Consecutive calls will fill the buffer with the next block of |
| 3082 | * available older records, not including the earlier retrieved ones. |
| 3083 | * |
| 3084 | * A return value of FALSE indicates that there are no more records to |
| 3085 | * read. |
| 3086 | */ |
| 3087 | bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog, |
| 3088 | char *buf, size_t size, size_t *len) |
| 3089 | { |
| 3090 | unsigned long flags; |
| 3091 | u64 seq; |
| 3092 | u32 idx; |
| 3093 | u64 next_seq; |
| 3094 | u32 next_idx; |
| 3095 | size_t l = 0; |
| 3096 | bool ret = false; |
| 3097 | |
| 3098 | if (!dumper->active) |
| 3099 | goto out; |
| 3100 | |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 3101 | logbuf_lock_irqsave(flags); |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3102 | if (dumper->cur_seq < log_first_seq) { |
| 3103 | /* messages are gone, move to first available one */ |
| 3104 | dumper->cur_seq = log_first_seq; |
| 3105 | dumper->cur_idx = log_first_idx; |
| 3106 | } |
| 3107 | |
| 3108 | /* last entry */ |
| 3109 | if (dumper->cur_seq >= dumper->next_seq) { |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 3110 | logbuf_unlock_irqrestore(flags); |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3111 | goto out; |
| 3112 | } |
| 3113 | |
| 3114 | /* calculate length of entire buffer */ |
| 3115 | seq = dumper->cur_seq; |
| 3116 | idx = dumper->cur_idx; |
| 3117 | while (seq < dumper->next_seq) { |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 3118 | struct printk_log *msg = log_from_idx(idx); |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3119 | |
Linus Torvalds | 5aa068e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 3120 | l += msg_print_text(msg, true, NULL, 0); |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3121 | idx = log_next(idx); |
| 3122 | seq++; |
| 3123 | } |
| 3124 | |
| 3125 | /* move first record forward until length fits into the buffer */ |
| 3126 | seq = dumper->cur_seq; |
| 3127 | idx = dumper->cur_idx; |
| 3128 | while (l > size && seq < dumper->next_seq) { |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 3129 | struct printk_log *msg = log_from_idx(idx); |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3130 | |
Linus Torvalds | 5aa068e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 3131 | l -= msg_print_text(msg, true, NULL, 0); |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3132 | idx = log_next(idx); |
| 3133 | seq++; |
| 3134 | } |
| 3135 | |
| 3136 | /* last message in next interation */ |
| 3137 | next_seq = seq; |
| 3138 | next_idx = idx; |
| 3139 | |
| 3140 | l = 0; |
| 3141 | while (seq < dumper->next_seq) { |
Joe Perches | 62e32ac | 2013-07-31 13:53:47 -0700 | [diff] [blame] | 3142 | struct printk_log *msg = log_from_idx(idx); |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3143 | |
Linus Torvalds | 5aa068e | 2016-10-25 11:27:31 -0700 | [diff] [blame] | 3144 | l += msg_print_text(msg, syslog, buf + l, size - l); |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3145 | idx = log_next(idx); |
| 3146 | seq++; |
| 3147 | } |
| 3148 | |
| 3149 | dumper->next_seq = next_seq; |
| 3150 | dumper->next_idx = next_idx; |
| 3151 | ret = true; |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 3152 | logbuf_unlock_irqrestore(flags); |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3153 | out: |
| 3154 | if (len) |
| 3155 | *len = l; |
| 3156 | return ret; |
| 3157 | } |
| 3158 | EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer); |
| 3159 | |
| 3160 | /** |
Anton Vorontsov | 533827c | 2012-07-20 17:28:07 -0700 | [diff] [blame] | 3161 | * kmsg_dump_rewind_nolock - reset the interator (unlocked version) |
| 3162 | * @dumper: registered kmsg dumper |
| 3163 | * |
| 3164 | * Reset the dumper's iterator so that kmsg_dump_get_line() and |
| 3165 | * kmsg_dump_get_buffer() can be called again and used multiple |
| 3166 | * times within the same dumper.dump() callback. |
| 3167 | * |
| 3168 | * The function is similar to kmsg_dump_rewind(), but grabs no locks. |
| 3169 | */ |
| 3170 | void kmsg_dump_rewind_nolock(struct kmsg_dumper *dumper) |
| 3171 | { |
| 3172 | dumper->cur_seq = clear_seq; |
| 3173 | dumper->cur_idx = clear_idx; |
| 3174 | dumper->next_seq = log_next_seq; |
| 3175 | dumper->next_idx = log_next_idx; |
| 3176 | } |
| 3177 | |
| 3178 | /** |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3179 | * kmsg_dump_rewind - reset the interator |
| 3180 | * @dumper: registered kmsg dumper |
| 3181 | * |
| 3182 | * Reset the dumper's iterator so that kmsg_dump_get_line() and |
| 3183 | * kmsg_dump_get_buffer() can be called again and used multiple |
| 3184 | * times within the same dumper.dump() callback. |
| 3185 | */ |
| 3186 | void kmsg_dump_rewind(struct kmsg_dumper *dumper) |
| 3187 | { |
| 3188 | unsigned long flags; |
| 3189 | |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 3190 | logbuf_lock_irqsave(flags); |
Anton Vorontsov | 533827c | 2012-07-20 17:28:07 -0700 | [diff] [blame] | 3191 | kmsg_dump_rewind_nolock(dumper); |
Sergey Senozhatsky | de6fcbd | 2016-12-27 23:16:11 +0900 | [diff] [blame] | 3192 | logbuf_unlock_irqrestore(flags); |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 3193 | } |
| 3194 | EXPORT_SYMBOL_GPL(kmsg_dump_rewind); |
Tejun Heo | 196779b | 2013-04-30 15:27:12 -0700 | [diff] [blame] | 3195 | |
Tejun Heo | 98e5e1b | 2013-04-30 15:27:15 -0700 | [diff] [blame] | 3196 | static char dump_stack_arch_desc_str[128]; |
| 3197 | |
| 3198 | /** |
| 3199 | * dump_stack_set_arch_desc - set arch-specific str to show with task dumps |
| 3200 | * @fmt: printf-style format string |
| 3201 | * @...: arguments for the format string |
| 3202 | * |
| 3203 | * The configured string will be printed right after utsname during task |
| 3204 | * dumps. Usually used to add arch-specific system identifiers. If an |
| 3205 | * arch wants to make use of such an ID string, it should initialize this |
| 3206 | * as soon as possible during boot. |
| 3207 | */ |
| 3208 | void __init dump_stack_set_arch_desc(const char *fmt, ...) |
| 3209 | { |
| 3210 | va_list args; |
| 3211 | |
| 3212 | va_start(args, fmt); |
| 3213 | vsnprintf(dump_stack_arch_desc_str, sizeof(dump_stack_arch_desc_str), |
| 3214 | fmt, args); |
| 3215 | va_end(args); |
| 3216 | } |
| 3217 | |
Tejun Heo | 196779b | 2013-04-30 15:27:12 -0700 | [diff] [blame] | 3218 | /** |
| 3219 | * dump_stack_print_info - print generic debug info for dump_stack() |
| 3220 | * @log_lvl: log level |
| 3221 | * |
| 3222 | * Arch-specific dump_stack() implementations can use this function to |
| 3223 | * print out the same debug information as the generic dump_stack(). |
| 3224 | */ |
| 3225 | void dump_stack_print_info(const char *log_lvl) |
| 3226 | { |
| 3227 | printk("%sCPU: %d PID: %d Comm: %.20s %s %s %.*s\n", |
| 3228 | log_lvl, raw_smp_processor_id(), current->pid, current->comm, |
| 3229 | print_tainted(), init_utsname()->release, |
| 3230 | (int)strcspn(init_utsname()->version, " "), |
| 3231 | init_utsname()->version); |
Tejun Heo | 98e5e1b | 2013-04-30 15:27:15 -0700 | [diff] [blame] | 3232 | |
| 3233 | if (dump_stack_arch_desc_str[0] != '\0') |
| 3234 | printk("%sHardware name: %s\n", |
| 3235 | log_lvl, dump_stack_arch_desc_str); |
Tejun Heo | 3d1cb20 | 2013-04-30 15:27:22 -0700 | [diff] [blame] | 3236 | |
| 3237 | print_worker_info(log_lvl, current); |
Tejun Heo | 196779b | 2013-04-30 15:27:12 -0700 | [diff] [blame] | 3238 | } |
| 3239 | |
Tejun Heo | a43cb95 | 2013-04-30 15:27:17 -0700 | [diff] [blame] | 3240 | /** |
| 3241 | * show_regs_print_info - print generic debug info for show_regs() |
| 3242 | * @log_lvl: log level |
| 3243 | * |
| 3244 | * show_regs() implementations can use this function to print out generic |
| 3245 | * debug information. |
| 3246 | */ |
| 3247 | void show_regs_print_info(const char *log_lvl) |
| 3248 | { |
| 3249 | dump_stack_print_info(log_lvl); |
| 3250 | |
Andy Lutomirski | 8b70ca6 | 2016-07-28 15:48:23 -0700 | [diff] [blame] | 3251 | printk("%stask: %p task.stack: %p\n", |
| 3252 | log_lvl, current, task_stack_page(current)); |
Tejun Heo | a43cb95 | 2013-04-30 15:27:17 -0700 | [diff] [blame] | 3253 | } |
| 3254 | |
Joe Perches | 7ef3d2f | 2008-02-08 04:21:25 -0800 | [diff] [blame] | 3255 | #endif |