LCOV - code coverage report
Current view: top level - kernel/printk - printk.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 369 1041 35.4 %
Date: 2022-12-09 01:23:36 Functions: 28 84 33.3 %

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

Generated by: LCOV version 1.14