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