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