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