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