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