Commit 084681d14e429cb6192262ac7437f00e2c02f26a

Authored by Kay Sievers
Committed by Greg Kroah-Hartman
1 parent 116e90b23f

printk: flush continuation lines immediately to console

Continuation lines are buffered internally, intended to merge the
chunked printk()s into a single record, and to isolate potentially
racy continuation users from usual terminated line users.

This though, has the effect that partial lines are not printed to
the console in the moment they are emitted. In case the kernel
crashes in the meantime, the potentially interesting printed
information would never reach the consoles.

Here we share the continuation buffer with the console copy logic,
and partial lines are always immediately flushed to the available
consoles. They are still buffered internally to improve the
readability and integrity of the messages and minimize the amount
of needed record headers to store.

Signed-off-by: Kay Sievers <kay@vrfy.org>
Tested-by: Steven Rostedt <rostedt@goodmis.org>
Acked-by: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Showing 1 changed file with 176 additions and 68 deletions Inline Diff

1 /* 1 /*
2 * linux/kernel/printk.c 2 * linux/kernel/printk.c
3 * 3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds 4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * 5 *
6 * Modified to make sys_syslog() more flexible: added commands to 6 * Modified to make sys_syslog() more flexible: added commands to
7 * return the last 4k of kernel messages, regardless of whether 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 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 9 * to the console. Added hook for sending the console messages
10 * elsewhere, in preparation for a serial line console (someday). 10 * elsewhere, in preparation for a serial line console (someday).
11 * Ted Ts'o, 2/11/93. 11 * Ted Ts'o, 2/11/93.
12 * Modified for sysctl support, 1/8/97, Chris Horn. 12 * Modified for sysctl support, 1/8/97, Chris Horn.
13 * Fixed SMP synchronization, 08/08/99, Manfred Spraul 13 * Fixed SMP synchronization, 08/08/99, Manfred Spraul
14 * manfred@colorfullife.com 14 * manfred@colorfullife.com
15 * Rewrote bits to get rid of console_lock 15 * Rewrote bits to get rid of console_lock
16 * 01Mar01 Andrew Morton 16 * 01Mar01 Andrew Morton
17 */ 17 */
18 18
19 #include <linux/kernel.h> 19 #include <linux/kernel.h>
20 #include <linux/mm.h> 20 #include <linux/mm.h>
21 #include <linux/tty.h> 21 #include <linux/tty.h>
22 #include <linux/tty_driver.h> 22 #include <linux/tty_driver.h>
23 #include <linux/console.h> 23 #include <linux/console.h>
24 #include <linux/init.h> 24 #include <linux/init.h>
25 #include <linux/jiffies.h> 25 #include <linux/jiffies.h>
26 #include <linux/nmi.h> 26 #include <linux/nmi.h>
27 #include <linux/module.h> 27 #include <linux/module.h>
28 #include <linux/moduleparam.h> 28 #include <linux/moduleparam.h>
29 #include <linux/interrupt.h> /* For in_interrupt() */ 29 #include <linux/interrupt.h> /* For in_interrupt() */
30 #include <linux/delay.h> 30 #include <linux/delay.h>
31 #include <linux/smp.h> 31 #include <linux/smp.h>
32 #include <linux/security.h> 32 #include <linux/security.h>
33 #include <linux/bootmem.h> 33 #include <linux/bootmem.h>
34 #include <linux/memblock.h> 34 #include <linux/memblock.h>
35 #include <linux/syscalls.h> 35 #include <linux/syscalls.h>
36 #include <linux/kexec.h> 36 #include <linux/kexec.h>
37 #include <linux/kdb.h> 37 #include <linux/kdb.h>
38 #include <linux/ratelimit.h> 38 #include <linux/ratelimit.h>
39 #include <linux/kmsg_dump.h> 39 #include <linux/kmsg_dump.h>
40 #include <linux/syslog.h> 40 #include <linux/syslog.h>
41 #include <linux/cpu.h> 41 #include <linux/cpu.h>
42 #include <linux/notifier.h> 42 #include <linux/notifier.h>
43 #include <linux/rculist.h> 43 #include <linux/rculist.h>
44 #include <linux/poll.h> 44 #include <linux/poll.h>
45 45
46 #include <asm/uaccess.h> 46 #include <asm/uaccess.h>
47 47
48 #define CREATE_TRACE_POINTS 48 #define CREATE_TRACE_POINTS
49 #include <trace/events/printk.h> 49 #include <trace/events/printk.h>
50 50
51 /* 51 /*
52 * Architectures can override it: 52 * Architectures can override it:
53 */ 53 */
54 void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...) 54 void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...)
55 { 55 {
56 } 56 }
57 57
58 /* printk's without a loglevel use this.. */ 58 /* printk's without a loglevel use this.. */
59 #define DEFAULT_MESSAGE_LOGLEVEL CONFIG_DEFAULT_MESSAGE_LOGLEVEL 59 #define DEFAULT_MESSAGE_LOGLEVEL CONFIG_DEFAULT_MESSAGE_LOGLEVEL
60 60
61 /* We show everything that is MORE important than this.. */ 61 /* We show everything that is MORE important than this.. */
62 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */ 62 #define MINIMUM_CONSOLE_LOGLEVEL 1 /* Minimum loglevel we let people use */
63 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */ 63 #define DEFAULT_CONSOLE_LOGLEVEL 7 /* anything MORE serious than KERN_DEBUG */
64 64
65 DECLARE_WAIT_QUEUE_HEAD(log_wait); 65 DECLARE_WAIT_QUEUE_HEAD(log_wait);
66 66
67 int console_printk[4] = { 67 int console_printk[4] = {
68 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */ 68 DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
69 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */ 69 DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
70 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */ 70 MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
71 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */ 71 DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
72 }; 72 };
73 73
74 /* 74 /*
75 * Low level drivers may need that to know if they can schedule in 75 * Low level drivers may need that to know if they can schedule in
76 * their unblank() callback or not. So let's export it. 76 * their unblank() callback or not. So let's export it.
77 */ 77 */
78 int oops_in_progress; 78 int oops_in_progress;
79 EXPORT_SYMBOL(oops_in_progress); 79 EXPORT_SYMBOL(oops_in_progress);
80 80
81 /* 81 /*
82 * console_sem protects the console_drivers list, and also 82 * console_sem protects the console_drivers list, and also
83 * provides serialisation for access to the entire console 83 * provides serialisation for access to the entire console
84 * driver system. 84 * driver system.
85 */ 85 */
86 static DEFINE_SEMAPHORE(console_sem); 86 static DEFINE_SEMAPHORE(console_sem);
87 struct console *console_drivers; 87 struct console *console_drivers;
88 EXPORT_SYMBOL_GPL(console_drivers); 88 EXPORT_SYMBOL_GPL(console_drivers);
89 89
90 /* 90 /*
91 * This is used for debugging the mess that is the VT code by 91 * This is used for debugging the mess that is the VT code by
92 * keeping track if we have the console semaphore held. It's 92 * keeping track if we have the console semaphore held. It's
93 * definitely not the perfect debug tool (we don't know if _WE_ 93 * definitely not the perfect debug tool (we don't know if _WE_
94 * hold it are racing, but it helps tracking those weird code 94 * hold it are racing, but it helps tracking those weird code
95 * path in the console code where we end up in places I want 95 * path in the console code where we end up in places I want
96 * locked without the console sempahore held 96 * locked without the console sempahore held
97 */ 97 */
98 static int console_locked, console_suspended; 98 static int console_locked, console_suspended;
99 99
100 /* 100 /*
101 * If exclusive_console is non-NULL then only this console is to be printed to. 101 * If exclusive_console is non-NULL then only this console is to be printed to.
102 */ 102 */
103 static struct console *exclusive_console; 103 static struct console *exclusive_console;
104 104
105 /* 105 /*
106 * Array of consoles built from command line options (console=) 106 * Array of consoles built from command line options (console=)
107 */ 107 */
108 struct console_cmdline 108 struct console_cmdline
109 { 109 {
110 char name[8]; /* Name of the driver */ 110 char name[8]; /* Name of the driver */
111 int index; /* Minor dev. to use */ 111 int index; /* Minor dev. to use */
112 char *options; /* Options for the driver */ 112 char *options; /* Options for the driver */
113 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE 113 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
114 char *brl_options; /* Options for braille driver */ 114 char *brl_options; /* Options for braille driver */
115 #endif 115 #endif
116 }; 116 };
117 117
118 #define MAX_CMDLINECONSOLES 8 118 #define MAX_CMDLINECONSOLES 8
119 119
120 static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES]; 120 static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
121 static int selected_console = -1; 121 static int selected_console = -1;
122 static int preferred_console = -1; 122 static int preferred_console = -1;
123 int console_set_on_cmdline; 123 int console_set_on_cmdline;
124 EXPORT_SYMBOL(console_set_on_cmdline); 124 EXPORT_SYMBOL(console_set_on_cmdline);
125 125
126 /* Flag: console code may call schedule() */ 126 /* Flag: console code may call schedule() */
127 static int console_may_schedule; 127 static int console_may_schedule;
128 128
129 /* 129 /*
130 * The printk log buffer consists of a chain of concatenated variable 130 * The printk log buffer consists of a chain of concatenated variable
131 * length records. Every record starts with a record header, containing 131 * length records. Every record starts with a record header, containing
132 * the overall length of the record. 132 * the overall length of the record.
133 * 133 *
134 * The heads to the first and last entry in the buffer, as well as the 134 * The heads to the first and last entry in the buffer, as well as the
135 * sequence numbers of these both entries are maintained when messages 135 * sequence numbers of these both entries are maintained when messages
136 * are stored.. 136 * are stored..
137 * 137 *
138 * If the heads indicate available messages, the length in the header 138 * If the heads indicate available messages, the length in the header
139 * tells the start next message. A length == 0 for the next message 139 * tells the start next message. A length == 0 for the next message
140 * indicates a wrap-around to the beginning of the buffer. 140 * indicates a wrap-around to the beginning of the buffer.
141 * 141 *
142 * Every record carries the monotonic timestamp in microseconds, as well as 142 * Every record carries the monotonic timestamp in microseconds, as well as
143 * the standard userspace syslog level and syslog facility. The usual 143 * the standard userspace syslog level and syslog facility. The usual
144 * kernel messages use LOG_KERN; userspace-injected messages always carry 144 * kernel messages use LOG_KERN; userspace-injected messages always carry
145 * a matching syslog facility, by default LOG_USER. The origin of every 145 * a matching syslog facility, by default LOG_USER. The origin of every
146 * message can be reliably determined that way. 146 * message can be reliably determined that way.
147 * 147 *
148 * The human readable log message directly follows the message header. The 148 * The human readable log message directly follows the message header. The
149 * length of the message text is stored in the header, the stored message 149 * length of the message text is stored in the header, the stored message
150 * is not terminated. 150 * is not terminated.
151 * 151 *
152 * Optionally, a message can carry a dictionary of properties (key/value pairs), 152 * Optionally, a message can carry a dictionary of properties (key/value pairs),
153 * to provide userspace with a machine-readable message context. 153 * to provide userspace with a machine-readable message context.
154 * 154 *
155 * Examples for well-defined, commonly used property names are: 155 * Examples for well-defined, commonly used property names are:
156 * DEVICE=b12:8 device identifier 156 * DEVICE=b12:8 device identifier
157 * b12:8 block dev_t 157 * b12:8 block dev_t
158 * c127:3 char dev_t 158 * c127:3 char dev_t
159 * n8 netdev ifindex 159 * n8 netdev ifindex
160 * +sound:card0 subsystem:devname 160 * +sound:card0 subsystem:devname
161 * SUBSYSTEM=pci driver-core subsystem name 161 * SUBSYSTEM=pci driver-core subsystem name
162 * 162 *
163 * Valid characters in property names are [a-zA-Z0-9.-_]. The plain text value 163 * Valid characters in property names are [a-zA-Z0-9.-_]. The plain text value
164 * follows directly after a '=' character. Every property is terminated by 164 * follows directly after a '=' character. Every property is terminated by
165 * a '\0' character. The last property is not terminated. 165 * a '\0' character. The last property is not terminated.
166 * 166 *
167 * Example of a message structure: 167 * Example of a message structure:
168 * 0000 ff 8f 00 00 00 00 00 00 monotonic time in nsec 168 * 0000 ff 8f 00 00 00 00 00 00 monotonic time in nsec
169 * 0008 34 00 record is 52 bytes long 169 * 0008 34 00 record is 52 bytes long
170 * 000a 0b 00 text is 11 bytes long 170 * 000a 0b 00 text is 11 bytes long
171 * 000c 1f 00 dictionary is 23 bytes long 171 * 000c 1f 00 dictionary is 23 bytes long
172 * 000e 03 00 LOG_KERN (facility) LOG_ERR (level) 172 * 000e 03 00 LOG_KERN (facility) LOG_ERR (level)
173 * 0010 69 74 27 73 20 61 20 6c "it's a l" 173 * 0010 69 74 27 73 20 61 20 6c "it's a l"
174 * 69 6e 65 "ine" 174 * 69 6e 65 "ine"
175 * 001b 44 45 56 49 43 "DEVIC" 175 * 001b 44 45 56 49 43 "DEVIC"
176 * 45 3d 62 38 3a 32 00 44 "E=b8:2\0D" 176 * 45 3d 62 38 3a 32 00 44 "E=b8:2\0D"
177 * 52 49 56 45 52 3d 62 75 "RIVER=bu" 177 * 52 49 56 45 52 3d 62 75 "RIVER=bu"
178 * 67 "g" 178 * 67 "g"
179 * 0032 00 00 00 padding to next message header 179 * 0032 00 00 00 padding to next message header
180 * 180 *
181 * The 'struct log' buffer header must never be directly exported to 181 * The 'struct log' buffer header must never be directly exported to
182 * userspace, it is a kernel-private implementation detail that might 182 * userspace, it is a kernel-private implementation detail that might
183 * need to be changed in the future, when the requirements change. 183 * need to be changed in the future, when the requirements change.
184 * 184 *
185 * /dev/kmsg exports the structured data in the following line format: 185 * /dev/kmsg exports the structured data in the following line format:
186 * "level,sequnum,timestamp;<message text>\n" 186 * "level,sequnum,timestamp;<message text>\n"
187 * 187 *
188 * The optional key/value pairs are attached as continuation lines starting 188 * The optional key/value pairs are attached as continuation lines starting
189 * with a space character and terminated by a newline. All possible 189 * with a space character and terminated by a newline. All possible
190 * non-prinatable characters are escaped in the "\xff" notation. 190 * non-prinatable characters are escaped in the "\xff" notation.
191 * 191 *
192 * Users of the export format should ignore possible additional values 192 * Users of the export format should ignore possible additional values
193 * separated by ',', and find the message after the ';' character. 193 * separated by ',', and find the message after the ';' character.
194 */ 194 */
195 195
196 enum log_flags {
197 LOG_DEFAULT = 0,
198 LOG_NOCONS = 1, /* already flushed, do not print to console */
199 };
200
196 struct log { 201 struct log {
197 u64 ts_nsec; /* timestamp in nanoseconds */ 202 u64 ts_nsec; /* timestamp in nanoseconds */
198 u16 len; /* length of entire record */ 203 u16 len; /* length of entire record */
199 u16 text_len; /* length of text buffer */ 204 u16 text_len; /* length of text buffer */
200 u16 dict_len; /* length of dictionary buffer */ 205 u16 dict_len; /* length of dictionary buffer */
201 u16 level; /* syslog level + facility */ 206 u8 facility; /* syslog facility */
207 u8 flags:5; /* internal record flags */
208 u8 level:3; /* syslog level */
202 }; 209 };
203 210
204 /* 211 /*
205 * The logbuf_lock protects kmsg buffer, indices, counters. It is also 212 * The logbuf_lock protects kmsg buffer, indices, counters. It is also
206 * used in interesting ways to provide interlocking in console_unlock(); 213 * used in interesting ways to provide interlocking in console_unlock();
207 */ 214 */
208 static DEFINE_RAW_SPINLOCK(logbuf_lock); 215 static DEFINE_RAW_SPINLOCK(logbuf_lock);
209 216
210 /* the next printk record to read by syslog(READ) or /proc/kmsg */ 217 /* the next printk record to read by syslog(READ) or /proc/kmsg */
211 static u64 syslog_seq; 218 static u64 syslog_seq;
212 static u32 syslog_idx; 219 static u32 syslog_idx;
213 220
214 /* index and sequence number of the first record stored in the buffer */ 221 /* index and sequence number of the first record stored in the buffer */
215 static u64 log_first_seq; 222 static u64 log_first_seq;
216 static u32 log_first_idx; 223 static u32 log_first_idx;
217 224
218 /* index and sequence number of the next record to store in the buffer */ 225 /* index and sequence number of the next record to store in the buffer */
219 static u64 log_next_seq; 226 static u64 log_next_seq;
220 #ifdef CONFIG_PRINTK 227 #ifdef CONFIG_PRINTK
221 static u32 log_next_idx; 228 static u32 log_next_idx;
222 229
223 /* the next printk record to read after the last 'clear' command */ 230 /* the next printk record to read after the last 'clear' command */
224 static u64 clear_seq; 231 static u64 clear_seq;
225 static u32 clear_idx; 232 static u32 clear_idx;
226 233
227 #define LOG_LINE_MAX 1024 234 #define LOG_LINE_MAX 1024
228 235
229 /* record buffer */ 236 /* record buffer */
230 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) 237 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
231 #define LOG_ALIGN 4 238 #define LOG_ALIGN 4
232 #else 239 #else
233 #define LOG_ALIGN __alignof__(struct log) 240 #define LOG_ALIGN __alignof__(struct log)
234 #endif 241 #endif
235 #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT) 242 #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
236 static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN); 243 static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN);
237 static char *log_buf = __log_buf; 244 static char *log_buf = __log_buf;
238 static u32 log_buf_len = __LOG_BUF_LEN; 245 static u32 log_buf_len = __LOG_BUF_LEN;
239 246
240 /* cpu currently holding logbuf_lock */ 247 /* cpu currently holding logbuf_lock */
241 static volatile unsigned int logbuf_cpu = UINT_MAX; 248 static volatile unsigned int logbuf_cpu = UINT_MAX;
242 249
243 /* human readable text of the record */ 250 /* human readable text of the record */
244 static char *log_text(const struct log *msg) 251 static char *log_text(const struct log *msg)
245 { 252 {
246 return (char *)msg + sizeof(struct log); 253 return (char *)msg + sizeof(struct log);
247 } 254 }
248 255
249 /* optional key/value pair dictionary attached to the record */ 256 /* optional key/value pair dictionary attached to the record */
250 static char *log_dict(const struct log *msg) 257 static char *log_dict(const struct log *msg)
251 { 258 {
252 return (char *)msg + sizeof(struct log) + msg->text_len; 259 return (char *)msg + sizeof(struct log) + msg->text_len;
253 } 260 }
254 261
255 /* get record by index; idx must point to valid msg */ 262 /* get record by index; idx must point to valid msg */
256 static struct log *log_from_idx(u32 idx) 263 static struct log *log_from_idx(u32 idx)
257 { 264 {
258 struct log *msg = (struct log *)(log_buf + idx); 265 struct log *msg = (struct log *)(log_buf + idx);
259 266
260 /* 267 /*
261 * A length == 0 record is the end of buffer marker. Wrap around and 268 * A length == 0 record is the end of buffer marker. Wrap around and
262 * read the message at the start of the buffer. 269 * read the message at the start of the buffer.
263 */ 270 */
264 if (!msg->len) 271 if (!msg->len)
265 return (struct log *)log_buf; 272 return (struct log *)log_buf;
266 return msg; 273 return msg;
267 } 274 }
268 275
269 /* get next record; idx must point to valid msg */ 276 /* get next record; idx must point to valid msg */
270 static u32 log_next(u32 idx) 277 static u32 log_next(u32 idx)
271 { 278 {
272 struct log *msg = (struct log *)(log_buf + idx); 279 struct log *msg = (struct log *)(log_buf + idx);
273 280
274 /* length == 0 indicates the end of the buffer; wrap */ 281 /* length == 0 indicates the end of the buffer; wrap */
275 /* 282 /*
276 * A length == 0 record is the end of buffer marker. Wrap around and 283 * A length == 0 record is the end of buffer marker. Wrap around and
277 * read the message at the start of the buffer as *this* one, and 284 * read the message at the start of the buffer as *this* one, and
278 * return the one after that. 285 * return the one after that.
279 */ 286 */
280 if (!msg->len) { 287 if (!msg->len) {
281 msg = (struct log *)log_buf; 288 msg = (struct log *)log_buf;
282 return msg->len; 289 return msg->len;
283 } 290 }
284 return idx + msg->len; 291 return idx + msg->len;
285 } 292 }
286 293
287 /* insert record into the buffer, discard old ones, update heads */ 294 /* insert record into the buffer, discard old ones, update heads */
288 static void log_store(int facility, int level, 295 static void log_store(int facility, int level,
296 enum log_flags flags, u64 ts_nsec,
289 const char *dict, u16 dict_len, 297 const char *dict, u16 dict_len,
290 const char *text, u16 text_len) 298 const char *text, u16 text_len)
291 { 299 {
292 struct log *msg; 300 struct log *msg;
293 u32 size, pad_len; 301 u32 size, pad_len;
294 302
295 /* number of '\0' padding bytes to next message */ 303 /* number of '\0' padding bytes to next message */
296 size = sizeof(struct log) + text_len + dict_len; 304 size = sizeof(struct log) + text_len + dict_len;
297 pad_len = (-size) & (LOG_ALIGN - 1); 305 pad_len = (-size) & (LOG_ALIGN - 1);
298 size += pad_len; 306 size += pad_len;
299 307
300 while (log_first_seq < log_next_seq) { 308 while (log_first_seq < log_next_seq) {
301 u32 free; 309 u32 free;
302 310
303 if (log_next_idx > log_first_idx) 311 if (log_next_idx > log_first_idx)
304 free = max(log_buf_len - log_next_idx, log_first_idx); 312 free = max(log_buf_len - log_next_idx, log_first_idx);
305 else 313 else
306 free = log_first_idx - log_next_idx; 314 free = log_first_idx - log_next_idx;
307 315
308 if (free > size + sizeof(struct log)) 316 if (free > size + sizeof(struct log))
309 break; 317 break;
310 318
311 /* drop old messages until we have enough contiuous space */ 319 /* drop old messages until we have enough contiuous space */
312 log_first_idx = log_next(log_first_idx); 320 log_first_idx = log_next(log_first_idx);
313 log_first_seq++; 321 log_first_seq++;
314 } 322 }
315 323
316 if (log_next_idx + size + sizeof(struct log) >= log_buf_len) { 324 if (log_next_idx + size + sizeof(struct log) >= log_buf_len) {
317 /* 325 /*
318 * This message + an additional empty header does not fit 326 * This message + an additional empty header does not fit
319 * at the end of the buffer. Add an empty header with len == 0 327 * at the end of the buffer. Add an empty header with len == 0
320 * to signify a wrap around. 328 * to signify a wrap around.
321 */ 329 */
322 memset(log_buf + log_next_idx, 0, sizeof(struct log)); 330 memset(log_buf + log_next_idx, 0, sizeof(struct log));
323 log_next_idx = 0; 331 log_next_idx = 0;
324 } 332 }
325 333
326 /* fill message */ 334 /* fill message */
327 msg = (struct log *)(log_buf + log_next_idx); 335 msg = (struct log *)(log_buf + log_next_idx);
328 memcpy(log_text(msg), text, text_len); 336 memcpy(log_text(msg), text, text_len);
329 msg->text_len = text_len; 337 msg->text_len = text_len;
330 memcpy(log_dict(msg), dict, dict_len); 338 memcpy(log_dict(msg), dict, dict_len);
331 msg->dict_len = dict_len; 339 msg->dict_len = dict_len;
332 msg->level = (facility << 3) | (level & 7); 340 msg->facility = facility;
333 msg->ts_nsec = local_clock(); 341 msg->level = level & 7;
342 msg->flags = flags & 0x1f;
343 if (ts_nsec > 0)
344 msg->ts_nsec = ts_nsec;
345 else
346 msg->ts_nsec = local_clock();
334 memset(log_dict(msg) + dict_len, 0, pad_len); 347 memset(log_dict(msg) + dict_len, 0, pad_len);
335 msg->len = sizeof(struct log) + text_len + dict_len + pad_len; 348 msg->len = sizeof(struct log) + text_len + dict_len + pad_len;
336 349
337 /* insert message */ 350 /* insert message */
338 log_next_idx += msg->len; 351 log_next_idx += msg->len;
339 log_next_seq++; 352 log_next_seq++;
340 } 353 }
341 354
342 /* /dev/kmsg - userspace message inject/listen interface */ 355 /* /dev/kmsg - userspace message inject/listen interface */
343 struct devkmsg_user { 356 struct devkmsg_user {
344 u64 seq; 357 u64 seq;
345 u32 idx; 358 u32 idx;
346 struct mutex lock; 359 struct mutex lock;
347 char buf[8192]; 360 char buf[8192];
348 }; 361 };
349 362
350 static ssize_t devkmsg_writev(struct kiocb *iocb, const struct iovec *iv, 363 static ssize_t devkmsg_writev(struct kiocb *iocb, const struct iovec *iv,
351 unsigned long count, loff_t pos) 364 unsigned long count, loff_t pos)
352 { 365 {
353 char *buf, *line; 366 char *buf, *line;
354 int i; 367 int i;
355 int level = default_message_loglevel; 368 int level = default_message_loglevel;
356 int facility = 1; /* LOG_USER */ 369 int facility = 1; /* LOG_USER */
357 size_t len = iov_length(iv, count); 370 size_t len = iov_length(iv, count);
358 ssize_t ret = len; 371 ssize_t ret = len;
359 372
360 if (len > LOG_LINE_MAX) 373 if (len > LOG_LINE_MAX)
361 return -EINVAL; 374 return -EINVAL;
362 buf = kmalloc(len+1, GFP_KERNEL); 375 buf = kmalloc(len+1, GFP_KERNEL);
363 if (buf == NULL) 376 if (buf == NULL)
364 return -ENOMEM; 377 return -ENOMEM;
365 378
366 line = buf; 379 line = buf;
367 for (i = 0; i < count; i++) { 380 for (i = 0; i < count; i++) {
368 if (copy_from_user(line, iv[i].iov_base, iv[i].iov_len)) 381 if (copy_from_user(line, iv[i].iov_base, iv[i].iov_len))
369 goto out; 382 goto out;
370 line += iv[i].iov_len; 383 line += iv[i].iov_len;
371 } 384 }
372 385
373 /* 386 /*
374 * Extract and skip the syslog prefix <[0-9]*>. Coming from userspace 387 * Extract and skip the syslog prefix <[0-9]*>. Coming from userspace
375 * the decimal value represents 32bit, the lower 3 bit are the log 388 * the decimal value represents 32bit, the lower 3 bit are the log
376 * level, the rest are the log facility. 389 * level, the rest are the log facility.
377 * 390 *
378 * If no prefix or no userspace facility is specified, we 391 * If no prefix or no userspace facility is specified, we
379 * enforce LOG_USER, to be able to reliably distinguish 392 * enforce LOG_USER, to be able to reliably distinguish
380 * kernel-generated messages from userspace-injected ones. 393 * kernel-generated messages from userspace-injected ones.
381 */ 394 */
382 line = buf; 395 line = buf;
383 if (line[0] == '<') { 396 if (line[0] == '<') {
384 char *endp = NULL; 397 char *endp = NULL;
385 398
386 i = simple_strtoul(line+1, &endp, 10); 399 i = simple_strtoul(line+1, &endp, 10);
387 if (endp && endp[0] == '>') { 400 if (endp && endp[0] == '>') {
388 level = i & 7; 401 level = i & 7;
389 if (i >> 3) 402 if (i >> 3)
390 facility = i >> 3; 403 facility = i >> 3;
391 endp++; 404 endp++;
392 len -= endp - line; 405 len -= endp - line;
393 line = endp; 406 line = endp;
394 } 407 }
395 } 408 }
396 line[len] = '\0'; 409 line[len] = '\0';
397 410
398 printk_emit(facility, level, NULL, 0, "%s", line); 411 printk_emit(facility, level, NULL, 0, "%s", line);
399 out: 412 out:
400 kfree(buf); 413 kfree(buf);
401 return ret; 414 return ret;
402 } 415 }
403 416
404 static ssize_t devkmsg_read(struct file *file, char __user *buf, 417 static ssize_t devkmsg_read(struct file *file, char __user *buf,
405 size_t count, loff_t *ppos) 418 size_t count, loff_t *ppos)
406 { 419 {
407 struct devkmsg_user *user = file->private_data; 420 struct devkmsg_user *user = file->private_data;
408 struct log *msg; 421 struct log *msg;
409 u64 ts_usec; 422 u64 ts_usec;
410 size_t i; 423 size_t i;
411 size_t len; 424 size_t len;
412 ssize_t ret; 425 ssize_t ret;
413 426
414 if (!user) 427 if (!user)
415 return -EBADF; 428 return -EBADF;
416 429
417 ret = mutex_lock_interruptible(&user->lock); 430 ret = mutex_lock_interruptible(&user->lock);
418 if (ret) 431 if (ret)
419 return ret; 432 return ret;
420 raw_spin_lock(&logbuf_lock); 433 raw_spin_lock(&logbuf_lock);
421 while (user->seq == log_next_seq) { 434 while (user->seq == log_next_seq) {
422 if (file->f_flags & O_NONBLOCK) { 435 if (file->f_flags & O_NONBLOCK) {
423 ret = -EAGAIN; 436 ret = -EAGAIN;
424 raw_spin_unlock(&logbuf_lock); 437 raw_spin_unlock(&logbuf_lock);
425 goto out; 438 goto out;
426 } 439 }
427 440
428 raw_spin_unlock(&logbuf_lock); 441 raw_spin_unlock(&logbuf_lock);
429 ret = wait_event_interruptible(log_wait, 442 ret = wait_event_interruptible(log_wait,
430 user->seq != log_next_seq); 443 user->seq != log_next_seq);
431 if (ret) 444 if (ret)
432 goto out; 445 goto out;
433 raw_spin_lock(&logbuf_lock); 446 raw_spin_lock(&logbuf_lock);
434 } 447 }
435 448
436 if (user->seq < log_first_seq) { 449 if (user->seq < log_first_seq) {
437 /* our last seen message is gone, return error and reset */ 450 /* our last seen message is gone, return error and reset */
438 user->idx = log_first_idx; 451 user->idx = log_first_idx;
439 user->seq = log_first_seq; 452 user->seq = log_first_seq;
440 ret = -EPIPE; 453 ret = -EPIPE;
441 raw_spin_unlock(&logbuf_lock); 454 raw_spin_unlock(&logbuf_lock);
442 goto out; 455 goto out;
443 } 456 }
444 457
445 msg = log_from_idx(user->idx); 458 msg = log_from_idx(user->idx);
446 ts_usec = msg->ts_nsec; 459 ts_usec = msg->ts_nsec;
447 do_div(ts_usec, 1000); 460 do_div(ts_usec, 1000);
448 len = sprintf(user->buf, "%u,%llu,%llu;", 461 len = sprintf(user->buf, "%u,%llu,%llu;",
449 msg->level, user->seq, ts_usec); 462 (msg->facility << 3) | msg->level, user->seq, ts_usec);
450 463
451 /* escape non-printable characters */ 464 /* escape non-printable characters */
452 for (i = 0; i < msg->text_len; i++) { 465 for (i = 0; i < msg->text_len; i++) {
453 unsigned char c = log_text(msg)[i]; 466 unsigned char c = log_text(msg)[i];
454 467
455 if (c < ' ' || c >= 128) 468 if (c < ' ' || c >= 128)
456 len += sprintf(user->buf + len, "\\x%02x", c); 469 len += sprintf(user->buf + len, "\\x%02x", c);
457 else 470 else
458 user->buf[len++] = c; 471 user->buf[len++] = c;
459 } 472 }
460 user->buf[len++] = '\n'; 473 user->buf[len++] = '\n';
461 474
462 if (msg->dict_len) { 475 if (msg->dict_len) {
463 bool line = true; 476 bool line = true;
464 477
465 for (i = 0; i < msg->dict_len; i++) { 478 for (i = 0; i < msg->dict_len; i++) {
466 unsigned char c = log_dict(msg)[i]; 479 unsigned char c = log_dict(msg)[i];
467 480
468 if (line) { 481 if (line) {
469 user->buf[len++] = ' '; 482 user->buf[len++] = ' ';
470 line = false; 483 line = false;
471 } 484 }
472 485
473 if (c == '\0') { 486 if (c == '\0') {
474 user->buf[len++] = '\n'; 487 user->buf[len++] = '\n';
475 line = true; 488 line = true;
476 continue; 489 continue;
477 } 490 }
478 491
479 if (c < ' ' || c >= 128) { 492 if (c < ' ' || c >= 128) {
480 len += sprintf(user->buf + len, "\\x%02x", c); 493 len += sprintf(user->buf + len, "\\x%02x", c);
481 continue; 494 continue;
482 } 495 }
483 496
484 user->buf[len++] = c; 497 user->buf[len++] = c;
485 } 498 }
486 user->buf[len++] = '\n'; 499 user->buf[len++] = '\n';
487 } 500 }
488 501
489 user->idx = log_next(user->idx); 502 user->idx = log_next(user->idx);
490 user->seq++; 503 user->seq++;
491 raw_spin_unlock(&logbuf_lock); 504 raw_spin_unlock(&logbuf_lock);
492 505
493 if (len > count) { 506 if (len > count) {
494 ret = -EINVAL; 507 ret = -EINVAL;
495 goto out; 508 goto out;
496 } 509 }
497 510
498 if (copy_to_user(buf, user->buf, len)) { 511 if (copy_to_user(buf, user->buf, len)) {
499 ret = -EFAULT; 512 ret = -EFAULT;
500 goto out; 513 goto out;
501 } 514 }
502 ret = len; 515 ret = len;
503 out: 516 out:
504 mutex_unlock(&user->lock); 517 mutex_unlock(&user->lock);
505 return ret; 518 return ret;
506 } 519 }
507 520
508 static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence) 521 static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence)
509 { 522 {
510 struct devkmsg_user *user = file->private_data; 523 struct devkmsg_user *user = file->private_data;
511 loff_t ret = 0; 524 loff_t ret = 0;
512 525
513 if (!user) 526 if (!user)
514 return -EBADF; 527 return -EBADF;
515 if (offset) 528 if (offset)
516 return -ESPIPE; 529 return -ESPIPE;
517 530
518 raw_spin_lock(&logbuf_lock); 531 raw_spin_lock(&logbuf_lock);
519 switch (whence) { 532 switch (whence) {
520 case SEEK_SET: 533 case SEEK_SET:
521 /* the first record */ 534 /* the first record */
522 user->idx = log_first_idx; 535 user->idx = log_first_idx;
523 user->seq = log_first_seq; 536 user->seq = log_first_seq;
524 break; 537 break;
525 case SEEK_DATA: 538 case SEEK_DATA:
526 /* 539 /*
527 * The first record after the last SYSLOG_ACTION_CLEAR, 540 * The first record after the last SYSLOG_ACTION_CLEAR,
528 * like issued by 'dmesg -c'. Reading /dev/kmsg itself 541 * like issued by 'dmesg -c'. Reading /dev/kmsg itself
529 * changes no global state, and does not clear anything. 542 * changes no global state, and does not clear anything.
530 */ 543 */
531 user->idx = clear_idx; 544 user->idx = clear_idx;
532 user->seq = clear_seq; 545 user->seq = clear_seq;
533 break; 546 break;
534 case SEEK_END: 547 case SEEK_END:
535 /* after the last record */ 548 /* after the last record */
536 user->idx = log_next_idx; 549 user->idx = log_next_idx;
537 user->seq = log_next_seq; 550 user->seq = log_next_seq;
538 break; 551 break;
539 default: 552 default:
540 ret = -EINVAL; 553 ret = -EINVAL;
541 } 554 }
542 raw_spin_unlock(&logbuf_lock); 555 raw_spin_unlock(&logbuf_lock);
543 return ret; 556 return ret;
544 } 557 }
545 558
546 static unsigned int devkmsg_poll(struct file *file, poll_table *wait) 559 static unsigned int devkmsg_poll(struct file *file, poll_table *wait)
547 { 560 {
548 struct devkmsg_user *user = file->private_data; 561 struct devkmsg_user *user = file->private_data;
549 int ret = 0; 562 int ret = 0;
550 563
551 if (!user) 564 if (!user)
552 return POLLERR|POLLNVAL; 565 return POLLERR|POLLNVAL;
553 566
554 poll_wait(file, &log_wait, wait); 567 poll_wait(file, &log_wait, wait);
555 568
556 raw_spin_lock(&logbuf_lock); 569 raw_spin_lock(&logbuf_lock);
557 if (user->seq < log_next_seq) { 570 if (user->seq < log_next_seq) {
558 /* return error when data has vanished underneath us */ 571 /* return error when data has vanished underneath us */
559 if (user->seq < log_first_seq) 572 if (user->seq < log_first_seq)
560 ret = POLLIN|POLLRDNORM|POLLERR|POLLPRI; 573 ret = POLLIN|POLLRDNORM|POLLERR|POLLPRI;
561 ret = POLLIN|POLLRDNORM; 574 ret = POLLIN|POLLRDNORM;
562 } 575 }
563 raw_spin_unlock(&logbuf_lock); 576 raw_spin_unlock(&logbuf_lock);
564 577
565 return ret; 578 return ret;
566 } 579 }
567 580
568 static int devkmsg_open(struct inode *inode, struct file *file) 581 static int devkmsg_open(struct inode *inode, struct file *file)
569 { 582 {
570 struct devkmsg_user *user; 583 struct devkmsg_user *user;
571 int err; 584 int err;
572 585
573 /* write-only does not need any file context */ 586 /* write-only does not need any file context */
574 if ((file->f_flags & O_ACCMODE) == O_WRONLY) 587 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
575 return 0; 588 return 0;
576 589
577 err = security_syslog(SYSLOG_ACTION_READ_ALL); 590 err = security_syslog(SYSLOG_ACTION_READ_ALL);
578 if (err) 591 if (err)
579 return err; 592 return err;
580 593
581 user = kmalloc(sizeof(struct devkmsg_user), GFP_KERNEL); 594 user = kmalloc(sizeof(struct devkmsg_user), GFP_KERNEL);
582 if (!user) 595 if (!user)
583 return -ENOMEM; 596 return -ENOMEM;
584 597
585 mutex_init(&user->lock); 598 mutex_init(&user->lock);
586 599
587 raw_spin_lock(&logbuf_lock); 600 raw_spin_lock(&logbuf_lock);
588 user->idx = log_first_idx; 601 user->idx = log_first_idx;
589 user->seq = log_first_seq; 602 user->seq = log_first_seq;
590 raw_spin_unlock(&logbuf_lock); 603 raw_spin_unlock(&logbuf_lock);
591 604
592 file->private_data = user; 605 file->private_data = user;
593 return 0; 606 return 0;
594 } 607 }
595 608
596 static int devkmsg_release(struct inode *inode, struct file *file) 609 static int devkmsg_release(struct inode *inode, struct file *file)
597 { 610 {
598 struct devkmsg_user *user = file->private_data; 611 struct devkmsg_user *user = file->private_data;
599 612
600 if (!user) 613 if (!user)
601 return 0; 614 return 0;
602 615
603 mutex_destroy(&user->lock); 616 mutex_destroy(&user->lock);
604 kfree(user); 617 kfree(user);
605 return 0; 618 return 0;
606 } 619 }
607 620
608 const struct file_operations kmsg_fops = { 621 const struct file_operations kmsg_fops = {
609 .open = devkmsg_open, 622 .open = devkmsg_open,
610 .read = devkmsg_read, 623 .read = devkmsg_read,
611 .aio_write = devkmsg_writev, 624 .aio_write = devkmsg_writev,
612 .llseek = devkmsg_llseek, 625 .llseek = devkmsg_llseek,
613 .poll = devkmsg_poll, 626 .poll = devkmsg_poll,
614 .release = devkmsg_release, 627 .release = devkmsg_release,
615 }; 628 };
616 629
617 #ifdef CONFIG_KEXEC 630 #ifdef CONFIG_KEXEC
618 /* 631 /*
619 * This appends the listed symbols to /proc/vmcoreinfo 632 * This appends the listed symbols to /proc/vmcoreinfo
620 * 633 *
621 * /proc/vmcoreinfo is used by various utiilties, like crash and makedumpfile to 634 * /proc/vmcoreinfo is used by various utiilties, like crash and makedumpfile to
622 * obtain access to symbols that are otherwise very difficult to locate. These 635 * obtain access to symbols that are otherwise very difficult to locate. These
623 * symbols are specifically used so that utilities can access and extract the 636 * symbols are specifically used so that utilities can access and extract the
624 * dmesg log from a vmcore file after a crash. 637 * dmesg log from a vmcore file after a crash.
625 */ 638 */
626 void log_buf_kexec_setup(void) 639 void log_buf_kexec_setup(void)
627 { 640 {
628 VMCOREINFO_SYMBOL(log_buf); 641 VMCOREINFO_SYMBOL(log_buf);
629 VMCOREINFO_SYMBOL(log_buf_len); 642 VMCOREINFO_SYMBOL(log_buf_len);
630 VMCOREINFO_SYMBOL(log_first_idx); 643 VMCOREINFO_SYMBOL(log_first_idx);
631 VMCOREINFO_SYMBOL(log_next_idx); 644 VMCOREINFO_SYMBOL(log_next_idx);
632 } 645 }
633 #endif 646 #endif
634 647
635 /* requested log_buf_len from kernel cmdline */ 648 /* requested log_buf_len from kernel cmdline */
636 static unsigned long __initdata new_log_buf_len; 649 static unsigned long __initdata new_log_buf_len;
637 650
638 /* save requested log_buf_len since it's too early to process it */ 651 /* save requested log_buf_len since it's too early to process it */
639 static int __init log_buf_len_setup(char *str) 652 static int __init log_buf_len_setup(char *str)
640 { 653 {
641 unsigned size = memparse(str, &str); 654 unsigned size = memparse(str, &str);
642 655
643 if (size) 656 if (size)
644 size = roundup_pow_of_two(size); 657 size = roundup_pow_of_two(size);
645 if (size > log_buf_len) 658 if (size > log_buf_len)
646 new_log_buf_len = size; 659 new_log_buf_len = size;
647 660
648 return 0; 661 return 0;
649 } 662 }
650 early_param("log_buf_len", log_buf_len_setup); 663 early_param("log_buf_len", log_buf_len_setup);
651 664
652 void __init setup_log_buf(int early) 665 void __init setup_log_buf(int early)
653 { 666 {
654 unsigned long flags; 667 unsigned long flags;
655 char *new_log_buf; 668 char *new_log_buf;
656 int free; 669 int free;
657 670
658 if (!new_log_buf_len) 671 if (!new_log_buf_len)
659 return; 672 return;
660 673
661 if (early) { 674 if (early) {
662 unsigned long mem; 675 unsigned long mem;
663 676
664 mem = memblock_alloc(new_log_buf_len, PAGE_SIZE); 677 mem = memblock_alloc(new_log_buf_len, PAGE_SIZE);
665 if (!mem) 678 if (!mem)
666 return; 679 return;
667 new_log_buf = __va(mem); 680 new_log_buf = __va(mem);
668 } else { 681 } else {
669 new_log_buf = alloc_bootmem_nopanic(new_log_buf_len); 682 new_log_buf = alloc_bootmem_nopanic(new_log_buf_len);
670 } 683 }
671 684
672 if (unlikely(!new_log_buf)) { 685 if (unlikely(!new_log_buf)) {
673 pr_err("log_buf_len: %ld bytes not available\n", 686 pr_err("log_buf_len: %ld bytes not available\n",
674 new_log_buf_len); 687 new_log_buf_len);
675 return; 688 return;
676 } 689 }
677 690
678 raw_spin_lock_irqsave(&logbuf_lock, flags); 691 raw_spin_lock_irqsave(&logbuf_lock, flags);
679 log_buf_len = new_log_buf_len; 692 log_buf_len = new_log_buf_len;
680 log_buf = new_log_buf; 693 log_buf = new_log_buf;
681 new_log_buf_len = 0; 694 new_log_buf_len = 0;
682 free = __LOG_BUF_LEN - log_next_idx; 695 free = __LOG_BUF_LEN - log_next_idx;
683 memcpy(log_buf, __log_buf, __LOG_BUF_LEN); 696 memcpy(log_buf, __log_buf, __LOG_BUF_LEN);
684 raw_spin_unlock_irqrestore(&logbuf_lock, flags); 697 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
685 698
686 pr_info("log_buf_len: %d\n", log_buf_len); 699 pr_info("log_buf_len: %d\n", log_buf_len);
687 pr_info("early log buf free: %d(%d%%)\n", 700 pr_info("early log buf free: %d(%d%%)\n",
688 free, (free * 100) / __LOG_BUF_LEN); 701 free, (free * 100) / __LOG_BUF_LEN);
689 } 702 }
690 703
691 #ifdef CONFIG_BOOT_PRINTK_DELAY 704 #ifdef CONFIG_BOOT_PRINTK_DELAY
692 705
693 static int boot_delay; /* msecs delay after each printk during bootup */ 706 static int boot_delay; /* msecs delay after each printk during bootup */
694 static unsigned long long loops_per_msec; /* based on boot_delay */ 707 static unsigned long long loops_per_msec; /* based on boot_delay */
695 708
696 static int __init boot_delay_setup(char *str) 709 static int __init boot_delay_setup(char *str)
697 { 710 {
698 unsigned long lpj; 711 unsigned long lpj;
699 712
700 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */ 713 lpj = preset_lpj ? preset_lpj : 1000000; /* some guess */
701 loops_per_msec = (unsigned long long)lpj / 1000 * HZ; 714 loops_per_msec = (unsigned long long)lpj / 1000 * HZ;
702 715
703 get_option(&str, &boot_delay); 716 get_option(&str, &boot_delay);
704 if (boot_delay > 10 * 1000) 717 if (boot_delay > 10 * 1000)
705 boot_delay = 0; 718 boot_delay = 0;
706 719
707 pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, " 720 pr_debug("boot_delay: %u, preset_lpj: %ld, lpj: %lu, "
708 "HZ: %d, loops_per_msec: %llu\n", 721 "HZ: %d, loops_per_msec: %llu\n",
709 boot_delay, preset_lpj, lpj, HZ, loops_per_msec); 722 boot_delay, preset_lpj, lpj, HZ, loops_per_msec);
710 return 1; 723 return 1;
711 } 724 }
712 __setup("boot_delay=", boot_delay_setup); 725 __setup("boot_delay=", boot_delay_setup);
713 726
714 static void boot_delay_msec(void) 727 static void boot_delay_msec(void)
715 { 728 {
716 unsigned long long k; 729 unsigned long long k;
717 unsigned long timeout; 730 unsigned long timeout;
718 731
719 if (boot_delay == 0 || system_state != SYSTEM_BOOTING) 732 if (boot_delay == 0 || system_state != SYSTEM_BOOTING)
720 return; 733 return;
721 734
722 k = (unsigned long long)loops_per_msec * boot_delay; 735 k = (unsigned long long)loops_per_msec * boot_delay;
723 736
724 timeout = jiffies + msecs_to_jiffies(boot_delay); 737 timeout = jiffies + msecs_to_jiffies(boot_delay);
725 while (k) { 738 while (k) {
726 k--; 739 k--;
727 cpu_relax(); 740 cpu_relax();
728 /* 741 /*
729 * use (volatile) jiffies to prevent 742 * use (volatile) jiffies to prevent
730 * compiler reduction; loop termination via jiffies 743 * compiler reduction; loop termination via jiffies
731 * is secondary and may or may not happen. 744 * is secondary and may or may not happen.
732 */ 745 */
733 if (time_after(jiffies, timeout)) 746 if (time_after(jiffies, timeout))
734 break; 747 break;
735 touch_nmi_watchdog(); 748 touch_nmi_watchdog();
736 } 749 }
737 } 750 }
738 #else 751 #else
739 static inline void boot_delay_msec(void) 752 static inline void boot_delay_msec(void)
740 { 753 {
741 } 754 }
742 #endif 755 #endif
743 756
744 #ifdef CONFIG_SECURITY_DMESG_RESTRICT 757 #ifdef CONFIG_SECURITY_DMESG_RESTRICT
745 int dmesg_restrict = 1; 758 int dmesg_restrict = 1;
746 #else 759 #else
747 int dmesg_restrict; 760 int dmesg_restrict;
748 #endif 761 #endif
749 762
750 static int syslog_action_restricted(int type) 763 static int syslog_action_restricted(int type)
751 { 764 {
752 if (dmesg_restrict) 765 if (dmesg_restrict)
753 return 1; 766 return 1;
754 /* Unless restricted, we allow "read all" and "get buffer size" for everybody */ 767 /* Unless restricted, we allow "read all" and "get buffer size" for everybody */
755 return type != SYSLOG_ACTION_READ_ALL && type != SYSLOG_ACTION_SIZE_BUFFER; 768 return type != SYSLOG_ACTION_READ_ALL && type != SYSLOG_ACTION_SIZE_BUFFER;
756 } 769 }
757 770
758 static int check_syslog_permissions(int type, bool from_file) 771 static int check_syslog_permissions(int type, bool from_file)
759 { 772 {
760 /* 773 /*
761 * If this is from /proc/kmsg and we've already opened it, then we've 774 * If this is from /proc/kmsg and we've already opened it, then we've
762 * already done the capabilities checks at open time. 775 * already done the capabilities checks at open time.
763 */ 776 */
764 if (from_file && type != SYSLOG_ACTION_OPEN) 777 if (from_file && type != SYSLOG_ACTION_OPEN)
765 return 0; 778 return 0;
766 779
767 if (syslog_action_restricted(type)) { 780 if (syslog_action_restricted(type)) {
768 if (capable(CAP_SYSLOG)) 781 if (capable(CAP_SYSLOG))
769 return 0; 782 return 0;
770 /* For historical reasons, accept CAP_SYS_ADMIN too, with a warning */ 783 /* For historical reasons, accept CAP_SYS_ADMIN too, with a warning */
771 if (capable(CAP_SYS_ADMIN)) { 784 if (capable(CAP_SYS_ADMIN)) {
772 printk_once(KERN_WARNING "%s (%d): " 785 printk_once(KERN_WARNING "%s (%d): "
773 "Attempt to access syslog with CAP_SYS_ADMIN " 786 "Attempt to access syslog with CAP_SYS_ADMIN "
774 "but no CAP_SYSLOG (deprecated).\n", 787 "but no CAP_SYSLOG (deprecated).\n",
775 current->comm, task_pid_nr(current)); 788 current->comm, task_pid_nr(current));
776 return 0; 789 return 0;
777 } 790 }
778 return -EPERM; 791 return -EPERM;
779 } 792 }
780 return 0; 793 return 0;
781 } 794 }
782 795
783 #if defined(CONFIG_PRINTK_TIME) 796 #if defined(CONFIG_PRINTK_TIME)
784 static bool printk_time = 1; 797 static bool printk_time = 1;
785 #else 798 #else
786 static bool printk_time; 799 static bool printk_time;
787 #endif 800 #endif
788 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR); 801 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
789 802
803 static size_t print_time(u64 ts, char *buf)
804 {
805 unsigned long rem_nsec;
806
807 if (!printk_time)
808 return 0;
809
810 if (!buf)
811 return 15;
812
813 rem_nsec = do_div(ts, 1000000000);
814 return sprintf(buf, "[%5lu.%06lu] ",
815 (unsigned long)ts, rem_nsec / 1000);
816 }
817
790 static size_t print_prefix(const struct log *msg, bool syslog, char *buf) 818 static size_t print_prefix(const struct log *msg, bool syslog, char *buf)
791 { 819 {
792 size_t len = 0; 820 size_t len = 0;
793 821
794 if (syslog) { 822 if (syslog) {
795 if (buf) { 823 if (buf) {
796 len += sprintf(buf, "<%u>", msg->level); 824 len += sprintf(buf, "<%u>", msg->level);
797 } else { 825 } else {
798 len += 3; 826 len += 3;
799 if (msg->level > 9) 827 if (msg->level > 9)
800 len++; 828 len++;
801 if (msg->level > 99) 829 if (msg->level > 99)
802 len++; 830 len++;
803 } 831 }
804 } 832 }
805 833
806 if (printk_time) { 834 len += print_time(msg->ts_nsec, buf ? buf + len : NULL);
807 if (buf) {
808 unsigned long long ts = msg->ts_nsec;
809 unsigned long rem_nsec = do_div(ts, 1000000000);
810
811 len += sprintf(buf + len, "[%5lu.%06lu] ",
812 (unsigned long) ts, rem_nsec / 1000);
813 } else {
814 len += 15;
815 }
816 }
817
818 return len; 835 return len;
819 } 836 }
820 837
821 static size_t msg_print_text(const struct log *msg, bool syslog, 838 static size_t msg_print_text(const struct log *msg, bool syslog,
822 char *buf, size_t size) 839 char *buf, size_t size)
823 { 840 {
824 const char *text = log_text(msg); 841 const char *text = log_text(msg);
825 size_t text_size = msg->text_len; 842 size_t text_size = msg->text_len;
826 size_t len = 0; 843 size_t len = 0;
827 844
828 do { 845 do {
829 const char *next = memchr(text, '\n', text_size); 846 const char *next = memchr(text, '\n', text_size);
830 size_t text_len; 847 size_t text_len;
831 848
832 if (next) { 849 if (next) {
833 text_len = next - text; 850 text_len = next - text;
834 next++; 851 next++;
835 text_size -= next - text; 852 text_size -= next - text;
836 } else { 853 } else {
837 text_len = text_size; 854 text_len = text_size;
838 } 855 }
839 856
840 if (buf) { 857 if (buf) {
841 if (print_prefix(msg, syslog, NULL) + 858 if (print_prefix(msg, syslog, NULL) +
842 text_len + 1>= size - len) 859 text_len + 1>= size - len)
843 break; 860 break;
844 861
845 len += print_prefix(msg, syslog, buf + len); 862 len += print_prefix(msg, syslog, buf + len);
846 memcpy(buf + len, text, text_len); 863 memcpy(buf + len, text, text_len);
847 len += text_len; 864 len += text_len;
848 buf[len++] = '\n'; 865 buf[len++] = '\n';
849 } else { 866 } else {
850 /* SYSLOG_ACTION_* buffer size only calculation */ 867 /* SYSLOG_ACTION_* buffer size only calculation */
851 len += print_prefix(msg, syslog, NULL); 868 len += print_prefix(msg, syslog, NULL);
852 len += text_len + 1; 869 len += text_len + 1;
853 } 870 }
854 871
855 text = next; 872 text = next;
856 } while (text); 873 } while (text);
857 874
858 return len; 875 return len;
859 } 876 }
860 877
861 static int syslog_print(char __user *buf, int size) 878 static int syslog_print(char __user *buf, int size)
862 { 879 {
863 char *text; 880 char *text;
864 struct log *msg; 881 struct log *msg;
865 int len = 0; 882 int len = 0;
866 883
867 text = kmalloc(LOG_LINE_MAX, GFP_KERNEL); 884 text = kmalloc(LOG_LINE_MAX, GFP_KERNEL);
868 if (!text) 885 if (!text)
869 return -ENOMEM; 886 return -ENOMEM;
870 887
871 while (size > 0) { 888 while (size > 0) {
872 size_t n; 889 size_t n;
873 890
874 raw_spin_lock_irq(&logbuf_lock); 891 raw_spin_lock_irq(&logbuf_lock);
875 if (syslog_seq < log_first_seq) { 892 if (syslog_seq < log_first_seq) {
876 /* messages are gone, move to first one */ 893 /* messages are gone, move to first one */
877 syslog_seq = log_first_seq; 894 syslog_seq = log_first_seq;
878 syslog_idx = log_first_idx; 895 syslog_idx = log_first_idx;
879 } 896 }
880 if (syslog_seq == log_next_seq) { 897 if (syslog_seq == log_next_seq) {
881 raw_spin_unlock_irq(&logbuf_lock); 898 raw_spin_unlock_irq(&logbuf_lock);
882 break; 899 break;
883 } 900 }
884 msg = log_from_idx(syslog_idx); 901 msg = log_from_idx(syslog_idx);
885 n = msg_print_text(msg, true, text, LOG_LINE_MAX); 902 n = msg_print_text(msg, true, text, LOG_LINE_MAX);
886 if (n <= size) { 903 if (n <= size) {
887 syslog_idx = log_next(syslog_idx); 904 syslog_idx = log_next(syslog_idx);
888 syslog_seq++; 905 syslog_seq++;
889 } else 906 } else
890 n = 0; 907 n = 0;
891 raw_spin_unlock_irq(&logbuf_lock); 908 raw_spin_unlock_irq(&logbuf_lock);
892 909
893 if (!n) 910 if (!n)
894 break; 911 break;
895 912
896 len += n; 913 len += n;
897 size -= n; 914 size -= n;
898 buf += n; 915 buf += n;
899 n = copy_to_user(buf - n, text, n); 916 n = copy_to_user(buf - n, text, n);
900 917
901 if (n) { 918 if (n) {
902 len -= n; 919 len -= n;
903 if (!len) 920 if (!len)
904 len = -EFAULT; 921 len = -EFAULT;
905 break; 922 break;
906 } 923 }
907 } 924 }
908 925
909 kfree(text); 926 kfree(text);
910 return len; 927 return len;
911 } 928 }
912 929
913 static int syslog_print_all(char __user *buf, int size, bool clear) 930 static int syslog_print_all(char __user *buf, int size, bool clear)
914 { 931 {
915 char *text; 932 char *text;
916 int len = 0; 933 int len = 0;
917 934
918 text = kmalloc(LOG_LINE_MAX, GFP_KERNEL); 935 text = kmalloc(LOG_LINE_MAX, GFP_KERNEL);
919 if (!text) 936 if (!text)
920 return -ENOMEM; 937 return -ENOMEM;
921 938
922 raw_spin_lock_irq(&logbuf_lock); 939 raw_spin_lock_irq(&logbuf_lock);
923 if (buf) { 940 if (buf) {
924 u64 next_seq; 941 u64 next_seq;
925 u64 seq; 942 u64 seq;
926 u32 idx; 943 u32 idx;
927 944
928 if (clear_seq < log_first_seq) { 945 if (clear_seq < log_first_seq) {
929 /* messages are gone, move to first available one */ 946 /* messages are gone, move to first available one */
930 clear_seq = log_first_seq; 947 clear_seq = log_first_seq;
931 clear_idx = log_first_idx; 948 clear_idx = log_first_idx;
932 } 949 }
933 950
934 /* 951 /*
935 * Find first record that fits, including all following records, 952 * Find first record that fits, including all following records,
936 * into the user-provided buffer for this dump. 953 * into the user-provided buffer for this dump.
937 */ 954 */
938 seq = clear_seq; 955 seq = clear_seq;
939 idx = clear_idx; 956 idx = clear_idx;
940 while (seq < log_next_seq) { 957 while (seq < log_next_seq) {
941 struct log *msg = log_from_idx(idx); 958 struct log *msg = log_from_idx(idx);
942 959
943 len += msg_print_text(msg, true, NULL, 0); 960 len += msg_print_text(msg, true, NULL, 0);
944 idx = log_next(idx); 961 idx = log_next(idx);
945 seq++; 962 seq++;
946 } 963 }
947 964
948 /* move first record forward until length fits into the buffer */ 965 /* move first record forward until length fits into the buffer */
949 seq = clear_seq; 966 seq = clear_seq;
950 idx = clear_idx; 967 idx = clear_idx;
951 while (len > size && seq < log_next_seq) { 968 while (len > size && seq < log_next_seq) {
952 struct log *msg = log_from_idx(idx); 969 struct log *msg = log_from_idx(idx);
953 970
954 len -= msg_print_text(msg, true, NULL, 0); 971 len -= msg_print_text(msg, true, NULL, 0);
955 idx = log_next(idx); 972 idx = log_next(idx);
956 seq++; 973 seq++;
957 } 974 }
958 975
959 /* last message fitting into this dump */ 976 /* last message fitting into this dump */
960 next_seq = log_next_seq; 977 next_seq = log_next_seq;
961 978
962 len = 0; 979 len = 0;
963 while (len >= 0 && seq < next_seq) { 980 while (len >= 0 && seq < next_seq) {
964 struct log *msg = log_from_idx(idx); 981 struct log *msg = log_from_idx(idx);
965 int textlen; 982 int textlen;
966 983
967 textlen = msg_print_text(msg, true, text, LOG_LINE_MAX); 984 textlen = msg_print_text(msg, true, text, LOG_LINE_MAX);
968 if (textlen < 0) { 985 if (textlen < 0) {
969 len = textlen; 986 len = textlen;
970 break; 987 break;
971 } 988 }
972 idx = log_next(idx); 989 idx = log_next(idx);
973 seq++; 990 seq++;
974 991
975 raw_spin_unlock_irq(&logbuf_lock); 992 raw_spin_unlock_irq(&logbuf_lock);
976 if (copy_to_user(buf + len, text, textlen)) 993 if (copy_to_user(buf + len, text, textlen))
977 len = -EFAULT; 994 len = -EFAULT;
978 else 995 else
979 len += textlen; 996 len += textlen;
980 raw_spin_lock_irq(&logbuf_lock); 997 raw_spin_lock_irq(&logbuf_lock);
981 998
982 if (seq < log_first_seq) { 999 if (seq < log_first_seq) {
983 /* messages are gone, move to next one */ 1000 /* messages are gone, move to next one */
984 seq = log_first_seq; 1001 seq = log_first_seq;
985 idx = log_first_idx; 1002 idx = log_first_idx;
986 } 1003 }
987 } 1004 }
988 } 1005 }
989 1006
990 if (clear) { 1007 if (clear) {
991 clear_seq = log_next_seq; 1008 clear_seq = log_next_seq;
992 clear_idx = log_next_idx; 1009 clear_idx = log_next_idx;
993 } 1010 }
994 raw_spin_unlock_irq(&logbuf_lock); 1011 raw_spin_unlock_irq(&logbuf_lock);
995 1012
996 kfree(text); 1013 kfree(text);
997 return len; 1014 return len;
998 } 1015 }
999 1016
1000 int do_syslog(int type, char __user *buf, int len, bool from_file) 1017 int do_syslog(int type, char __user *buf, int len, bool from_file)
1001 { 1018 {
1002 bool clear = false; 1019 bool clear = false;
1003 static int saved_console_loglevel = -1; 1020 static int saved_console_loglevel = -1;
1004 static DEFINE_MUTEX(syslog_mutex); 1021 static DEFINE_MUTEX(syslog_mutex);
1005 int error; 1022 int error;
1006 1023
1007 error = check_syslog_permissions(type, from_file); 1024 error = check_syslog_permissions(type, from_file);
1008 if (error) 1025 if (error)
1009 goto out; 1026 goto out;
1010 1027
1011 error = security_syslog(type); 1028 error = security_syslog(type);
1012 if (error) 1029 if (error)
1013 return error; 1030 return error;
1014 1031
1015 switch (type) { 1032 switch (type) {
1016 case SYSLOG_ACTION_CLOSE: /* Close log */ 1033 case SYSLOG_ACTION_CLOSE: /* Close log */
1017 break; 1034 break;
1018 case SYSLOG_ACTION_OPEN: /* Open log */ 1035 case SYSLOG_ACTION_OPEN: /* Open log */
1019 break; 1036 break;
1020 case SYSLOG_ACTION_READ: /* Read from log */ 1037 case SYSLOG_ACTION_READ: /* Read from log */
1021 error = -EINVAL; 1038 error = -EINVAL;
1022 if (!buf || len < 0) 1039 if (!buf || len < 0)
1023 goto out; 1040 goto out;
1024 error = 0; 1041 error = 0;
1025 if (!len) 1042 if (!len)
1026 goto out; 1043 goto out;
1027 if (!access_ok(VERIFY_WRITE, buf, len)) { 1044 if (!access_ok(VERIFY_WRITE, buf, len)) {
1028 error = -EFAULT; 1045 error = -EFAULT;
1029 goto out; 1046 goto out;
1030 } 1047 }
1031 error = mutex_lock_interruptible(&syslog_mutex); 1048 error = mutex_lock_interruptible(&syslog_mutex);
1032 if (error) 1049 if (error)
1033 goto out; 1050 goto out;
1034 error = wait_event_interruptible(log_wait, 1051 error = wait_event_interruptible(log_wait,
1035 syslog_seq != log_next_seq); 1052 syslog_seq != log_next_seq);
1036 if (error) { 1053 if (error) {
1037 mutex_unlock(&syslog_mutex); 1054 mutex_unlock(&syslog_mutex);
1038 goto out; 1055 goto out;
1039 } 1056 }
1040 error = syslog_print(buf, len); 1057 error = syslog_print(buf, len);
1041 mutex_unlock(&syslog_mutex); 1058 mutex_unlock(&syslog_mutex);
1042 break; 1059 break;
1043 /* Read/clear last kernel messages */ 1060 /* Read/clear last kernel messages */
1044 case SYSLOG_ACTION_READ_CLEAR: 1061 case SYSLOG_ACTION_READ_CLEAR:
1045 clear = true; 1062 clear = true;
1046 /* FALL THRU */ 1063 /* FALL THRU */
1047 /* Read last kernel messages */ 1064 /* Read last kernel messages */
1048 case SYSLOG_ACTION_READ_ALL: 1065 case SYSLOG_ACTION_READ_ALL:
1049 error = -EINVAL; 1066 error = -EINVAL;
1050 if (!buf || len < 0) 1067 if (!buf || len < 0)
1051 goto out; 1068 goto out;
1052 error = 0; 1069 error = 0;
1053 if (!len) 1070 if (!len)
1054 goto out; 1071 goto out;
1055 if (!access_ok(VERIFY_WRITE, buf, len)) { 1072 if (!access_ok(VERIFY_WRITE, buf, len)) {
1056 error = -EFAULT; 1073 error = -EFAULT;
1057 goto out; 1074 goto out;
1058 } 1075 }
1059 error = syslog_print_all(buf, len, clear); 1076 error = syslog_print_all(buf, len, clear);
1060 break; 1077 break;
1061 /* Clear ring buffer */ 1078 /* Clear ring buffer */
1062 case SYSLOG_ACTION_CLEAR: 1079 case SYSLOG_ACTION_CLEAR:
1063 syslog_print_all(NULL, 0, true); 1080 syslog_print_all(NULL, 0, true);
1064 break; 1081 break;
1065 /* Disable logging to console */ 1082 /* Disable logging to console */
1066 case SYSLOG_ACTION_CONSOLE_OFF: 1083 case SYSLOG_ACTION_CONSOLE_OFF:
1067 if (saved_console_loglevel == -1) 1084 if (saved_console_loglevel == -1)
1068 saved_console_loglevel = console_loglevel; 1085 saved_console_loglevel = console_loglevel;
1069 console_loglevel = minimum_console_loglevel; 1086 console_loglevel = minimum_console_loglevel;
1070 break; 1087 break;
1071 /* Enable logging to console */ 1088 /* Enable logging to console */
1072 case SYSLOG_ACTION_CONSOLE_ON: 1089 case SYSLOG_ACTION_CONSOLE_ON:
1073 if (saved_console_loglevel != -1) { 1090 if (saved_console_loglevel != -1) {
1074 console_loglevel = saved_console_loglevel; 1091 console_loglevel = saved_console_loglevel;
1075 saved_console_loglevel = -1; 1092 saved_console_loglevel = -1;
1076 } 1093 }
1077 break; 1094 break;
1078 /* Set level of messages printed to console */ 1095 /* Set level of messages printed to console */
1079 case SYSLOG_ACTION_CONSOLE_LEVEL: 1096 case SYSLOG_ACTION_CONSOLE_LEVEL:
1080 error = -EINVAL; 1097 error = -EINVAL;
1081 if (len < 1 || len > 8) 1098 if (len < 1 || len > 8)
1082 goto out; 1099 goto out;
1083 if (len < minimum_console_loglevel) 1100 if (len < minimum_console_loglevel)
1084 len = minimum_console_loglevel; 1101 len = minimum_console_loglevel;
1085 console_loglevel = len; 1102 console_loglevel = len;
1086 /* Implicitly re-enable logging to console */ 1103 /* Implicitly re-enable logging to console */
1087 saved_console_loglevel = -1; 1104 saved_console_loglevel = -1;
1088 error = 0; 1105 error = 0;
1089 break; 1106 break;
1090 /* Number of chars in the log buffer */ 1107 /* Number of chars in the log buffer */
1091 case SYSLOG_ACTION_SIZE_UNREAD: 1108 case SYSLOG_ACTION_SIZE_UNREAD:
1092 raw_spin_lock_irq(&logbuf_lock); 1109 raw_spin_lock_irq(&logbuf_lock);
1093 if (syslog_seq < log_first_seq) { 1110 if (syslog_seq < log_first_seq) {
1094 /* messages are gone, move to first one */ 1111 /* messages are gone, move to first one */
1095 syslog_seq = log_first_seq; 1112 syslog_seq = log_first_seq;
1096 syslog_idx = log_first_idx; 1113 syslog_idx = log_first_idx;
1097 } 1114 }
1098 if (from_file) { 1115 if (from_file) {
1099 /* 1116 /*
1100 * Short-cut for poll(/"proc/kmsg") which simply checks 1117 * Short-cut for poll(/"proc/kmsg") which simply checks
1101 * for pending data, not the size; return the count of 1118 * for pending data, not the size; return the count of
1102 * records, not the length. 1119 * records, not the length.
1103 */ 1120 */
1104 error = log_next_idx - syslog_idx; 1121 error = log_next_idx - syslog_idx;
1105 } else { 1122 } else {
1106 u64 seq; 1123 u64 seq;
1107 u32 idx; 1124 u32 idx;
1108 1125
1109 error = 0; 1126 error = 0;
1110 seq = syslog_seq; 1127 seq = syslog_seq;
1111 idx = syslog_idx; 1128 idx = syslog_idx;
1112 while (seq < log_next_seq) { 1129 while (seq < log_next_seq) {
1113 struct log *msg = log_from_idx(idx); 1130 struct log *msg = log_from_idx(idx);
1114 1131
1115 error += msg_print_text(msg, true, NULL, 0); 1132 error += msg_print_text(msg, true, NULL, 0);
1116 idx = log_next(idx); 1133 idx = log_next(idx);
1117 seq++; 1134 seq++;
1118 } 1135 }
1119 } 1136 }
1120 raw_spin_unlock_irq(&logbuf_lock); 1137 raw_spin_unlock_irq(&logbuf_lock);
1121 break; 1138 break;
1122 /* Size of the log buffer */ 1139 /* Size of the log buffer */
1123 case SYSLOG_ACTION_SIZE_BUFFER: 1140 case SYSLOG_ACTION_SIZE_BUFFER:
1124 error = log_buf_len; 1141 error = log_buf_len;
1125 break; 1142 break;
1126 default: 1143 default:
1127 error = -EINVAL; 1144 error = -EINVAL;
1128 break; 1145 break;
1129 } 1146 }
1130 out: 1147 out:
1131 return error; 1148 return error;
1132 } 1149 }
1133 1150
1134 SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len) 1151 SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
1135 { 1152 {
1136 return do_syslog(type, buf, len, SYSLOG_FROM_CALL); 1153 return do_syslog(type, buf, len, SYSLOG_FROM_CALL);
1137 } 1154 }
1138 1155
1139 #ifdef CONFIG_KGDB_KDB 1156 #ifdef CONFIG_KGDB_KDB
1140 /* kdb dmesg command needs access to the syslog buffer. do_syslog() 1157 /* kdb dmesg command needs access to the syslog buffer. do_syslog()
1141 * uses locks so it cannot be used during debugging. Just tell kdb 1158 * uses locks so it cannot be used during debugging. Just tell kdb
1142 * where the start and end of the physical and logical logs are. This 1159 * where the start and end of the physical and logical logs are. This
1143 * is equivalent to do_syslog(3). 1160 * is equivalent to do_syslog(3).
1144 */ 1161 */
1145 void kdb_syslog_data(char *syslog_data[4]) 1162 void kdb_syslog_data(char *syslog_data[4])
1146 { 1163 {
1147 syslog_data[0] = log_buf; 1164 syslog_data[0] = log_buf;
1148 syslog_data[1] = log_buf + log_buf_len; 1165 syslog_data[1] = log_buf + log_buf_len;
1149 syslog_data[2] = log_buf + log_first_idx; 1166 syslog_data[2] = log_buf + log_first_idx;
1150 syslog_data[3] = log_buf + log_next_idx; 1167 syslog_data[3] = log_buf + log_next_idx;
1151 } 1168 }
1152 #endif /* CONFIG_KGDB_KDB */ 1169 #endif /* CONFIG_KGDB_KDB */
1153 1170
1154 static bool __read_mostly ignore_loglevel; 1171 static bool __read_mostly ignore_loglevel;
1155 1172
1156 static int __init ignore_loglevel_setup(char *str) 1173 static int __init ignore_loglevel_setup(char *str)
1157 { 1174 {
1158 ignore_loglevel = 1; 1175 ignore_loglevel = 1;
1159 printk(KERN_INFO "debug: ignoring loglevel setting.\n"); 1176 printk(KERN_INFO "debug: ignoring loglevel setting.\n");
1160 1177
1161 return 0; 1178 return 0;
1162 } 1179 }
1163 1180
1164 early_param("ignore_loglevel", ignore_loglevel_setup); 1181 early_param("ignore_loglevel", ignore_loglevel_setup);
1165 module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR); 1182 module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR);
1166 MODULE_PARM_DESC(ignore_loglevel, "ignore loglevel setting, to" 1183 MODULE_PARM_DESC(ignore_loglevel, "ignore loglevel setting, to"
1167 "print all kernel messages to the console."); 1184 "print all kernel messages to the console.");
1168 1185
1169 /* 1186 /*
1170 * Call the console drivers, asking them to write out 1187 * Call the console drivers, asking them to write out
1171 * log_buf[start] to log_buf[end - 1]. 1188 * log_buf[start] to log_buf[end - 1].
1172 * The console_lock must be held. 1189 * The console_lock must be held.
1173 */ 1190 */
1174 static void call_console_drivers(int level, const char *text, size_t len) 1191 static void call_console_drivers(int level, const char *text, size_t len)
1175 { 1192 {
1176 struct console *con; 1193 struct console *con;
1177 1194
1178 trace_console(text, 0, len, len); 1195 trace_console(text, 0, len, len);
1179 1196
1180 if (level >= console_loglevel && !ignore_loglevel) 1197 if (level >= console_loglevel && !ignore_loglevel)
1181 return; 1198 return;
1182 if (!console_drivers) 1199 if (!console_drivers)
1183 return; 1200 return;
1184 1201
1185 for_each_console(con) { 1202 for_each_console(con) {
1186 if (exclusive_console && con != exclusive_console) 1203 if (exclusive_console && con != exclusive_console)
1187 continue; 1204 continue;
1188 if (!(con->flags & CON_ENABLED)) 1205 if (!(con->flags & CON_ENABLED))
1189 continue; 1206 continue;
1190 if (!con->write) 1207 if (!con->write)
1191 continue; 1208 continue;
1192 if (!cpu_online(smp_processor_id()) && 1209 if (!cpu_online(smp_processor_id()) &&
1193 !(con->flags & CON_ANYTIME)) 1210 !(con->flags & CON_ANYTIME))
1194 continue; 1211 continue;
1195 con->write(con, text, len); 1212 con->write(con, text, len);
1196 } 1213 }
1197 } 1214 }
1198 1215
1199 /* 1216 /*
1200 * Zap console related locks when oopsing. Only zap at most once 1217 * Zap console related locks when oopsing. Only zap at most once
1201 * every 10 seconds, to leave time for slow consoles to print a 1218 * every 10 seconds, to leave time for slow consoles to print a
1202 * full oops. 1219 * full oops.
1203 */ 1220 */
1204 static void zap_locks(void) 1221 static void zap_locks(void)
1205 { 1222 {
1206 static unsigned long oops_timestamp; 1223 static unsigned long oops_timestamp;
1207 1224
1208 if (time_after_eq(jiffies, oops_timestamp) && 1225 if (time_after_eq(jiffies, oops_timestamp) &&
1209 !time_after(jiffies, oops_timestamp + 30 * HZ)) 1226 !time_after(jiffies, oops_timestamp + 30 * HZ))
1210 return; 1227 return;
1211 1228
1212 oops_timestamp = jiffies; 1229 oops_timestamp = jiffies;
1213 1230
1214 debug_locks_off(); 1231 debug_locks_off();
1215 /* If a crash is occurring, make sure we can't deadlock */ 1232 /* If a crash is occurring, make sure we can't deadlock */
1216 raw_spin_lock_init(&logbuf_lock); 1233 raw_spin_lock_init(&logbuf_lock);
1217 /* And make sure that we print immediately */ 1234 /* And make sure that we print immediately */
1218 sema_init(&console_sem, 1); 1235 sema_init(&console_sem, 1);
1219 } 1236 }
1220 1237
1221 /* Check if we have any console registered that can be called early in boot. */ 1238 /* Check if we have any console registered that can be called early in boot. */
1222 static int have_callable_console(void) 1239 static int have_callable_console(void)
1223 { 1240 {
1224 struct console *con; 1241 struct console *con;
1225 1242
1226 for_each_console(con) 1243 for_each_console(con)
1227 if (con->flags & CON_ANYTIME) 1244 if (con->flags & CON_ANYTIME)
1228 return 1; 1245 return 1;
1229 1246
1230 return 0; 1247 return 0;
1231 } 1248 }
1232 1249
1233 /* 1250 /*
1234 * Can we actually use the console at this time on this cpu? 1251 * Can we actually use the console at this time on this cpu?
1235 * 1252 *
1236 * Console drivers may assume that per-cpu resources have 1253 * Console drivers may assume that per-cpu resources have
1237 * been allocated. So unless they're explicitly marked as 1254 * been allocated. So unless they're explicitly marked as
1238 * being able to cope (CON_ANYTIME) don't call them until 1255 * being able to cope (CON_ANYTIME) don't call them until
1239 * this CPU is officially up. 1256 * this CPU is officially up.
1240 */ 1257 */
1241 static inline int can_use_console(unsigned int cpu) 1258 static inline int can_use_console(unsigned int cpu)
1242 { 1259 {
1243 return cpu_online(cpu) || have_callable_console(); 1260 return cpu_online(cpu) || have_callable_console();
1244 } 1261 }
1245 1262
1246 /* 1263 /*
1247 * Try to get console ownership to actually show the kernel 1264 * Try to get console ownership to actually show the kernel
1248 * messages from a 'printk'. Return true (and with the 1265 * messages from a 'printk'. Return true (and with the
1249 * console_lock held, and 'console_locked' set) if it 1266 * console_lock held, and 'console_locked' set) if it
1250 * is successful, false otherwise. 1267 * is successful, false otherwise.
1251 * 1268 *
1252 * This gets called with the 'logbuf_lock' spinlock held and 1269 * This gets called with the 'logbuf_lock' spinlock held and
1253 * interrupts disabled. It should return with 'lockbuf_lock' 1270 * interrupts disabled. It should return with 'lockbuf_lock'
1254 * released but interrupts still disabled. 1271 * released but interrupts still disabled.
1255 */ 1272 */
1256 static int console_trylock_for_printk(unsigned int cpu) 1273 static int console_trylock_for_printk(unsigned int cpu)
1257 __releases(&logbuf_lock) 1274 __releases(&logbuf_lock)
1258 { 1275 {
1259 int retval = 0, wake = 0; 1276 int retval = 0, wake = 0;
1260 1277
1261 if (console_trylock()) { 1278 if (console_trylock()) {
1262 retval = 1; 1279 retval = 1;
1263 1280
1264 /* 1281 /*
1265 * If we can't use the console, we need to release 1282 * If we can't use the console, we need to release
1266 * the console semaphore by hand to avoid flushing 1283 * the console semaphore by hand to avoid flushing
1267 * the buffer. We need to hold the console semaphore 1284 * the buffer. We need to hold the console semaphore
1268 * in order to do this test safely. 1285 * in order to do this test safely.
1269 */ 1286 */
1270 if (!can_use_console(cpu)) { 1287 if (!can_use_console(cpu)) {
1271 console_locked = 0; 1288 console_locked = 0;
1272 wake = 1; 1289 wake = 1;
1273 retval = 0; 1290 retval = 0;
1274 } 1291 }
1275 } 1292 }
1276 logbuf_cpu = UINT_MAX; 1293 logbuf_cpu = UINT_MAX;
1277 if (wake) 1294 if (wake)
1278 up(&console_sem); 1295 up(&console_sem);
1279 raw_spin_unlock(&logbuf_lock); 1296 raw_spin_unlock(&logbuf_lock);
1280 return retval; 1297 return retval;
1281 } 1298 }
1282 1299
1283 int printk_delay_msec __read_mostly; 1300 int printk_delay_msec __read_mostly;
1284 1301
1285 static inline void printk_delay(void) 1302 static inline void printk_delay(void)
1286 { 1303 {
1287 if (unlikely(printk_delay_msec)) { 1304 if (unlikely(printk_delay_msec)) {
1288 int m = printk_delay_msec; 1305 int m = printk_delay_msec;
1289 1306
1290 while (m--) { 1307 while (m--) {
1291 mdelay(1); 1308 mdelay(1);
1292 touch_nmi_watchdog(); 1309 touch_nmi_watchdog();
1293 } 1310 }
1294 } 1311 }
1295 } 1312 }
1296 1313
1314 /*
1315 * Continuation lines are buffered, and not committed to the record buffer
1316 * until the line is complete, or a race forces it. The line fragments
1317 * though, are printed immediately to the consoles to ensure everything has
1318 * reached the console in case of a kernel crash.
1319 */
1320 static struct cont {
1321 char buf[LOG_LINE_MAX];
1322 size_t len; /* length == 0 means unused buffer */
1323 size_t cons; /* bytes written to console */
1324 struct task_struct *owner; /* task of first print*/
1325 u64 ts_nsec; /* time of first print */
1326 u8 level; /* log level of first message */
1327 u8 facility; /* log level of first message */
1328 bool flushed:1; /* buffer sealed and committed */
1329 } cont;
1330
1331 static void cont_flush(void)
1332 {
1333 if (cont.flushed)
1334 return;
1335 if (cont.len == 0)
1336 return;
1337
1338 log_store(cont.facility, cont.level, LOG_NOCONS, cont.ts_nsec,
1339 NULL, 0, cont.buf, cont.len);
1340
1341 cont.flushed = true;
1342 }
1343
1344 static bool cont_add(int facility, int level, const char *text, size_t len)
1345 {
1346 if (cont.len && cont.flushed)
1347 return false;
1348
1349 if (cont.len + len > sizeof(cont.buf)) {
1350 cont_flush();
1351 return false;
1352 }
1353
1354 if (!cont.len) {
1355 cont.facility = facility;
1356 cont.level = level;
1357 cont.owner = current;
1358 cont.ts_nsec = local_clock();
1359 cont.cons = 0;
1360 cont.flushed = false;
1361 }
1362
1363 memcpy(cont.buf + cont.len, text, len);
1364 cont.len += len;
1365 return true;
1366 }
1367
1368 static size_t cont_print_text(char *text, size_t size)
1369 {
1370 size_t textlen = 0;
1371 size_t len;
1372
1373 if (cont.cons == 0) {
1374 textlen += print_time(cont.ts_nsec, text);
1375 size -= textlen;
1376 }
1377
1378 len = cont.len - cont.cons;
1379 if (len > 0) {
1380 if (len+1 > size)
1381 len = size-1;
1382 memcpy(text + textlen, cont.buf + cont.cons, len);
1383 textlen += len;
1384 cont.cons = cont.len;
1385 }
1386
1387 if (cont.flushed) {
1388 text[textlen++] = '\n';
1389 /* got everything, release buffer */
1390 cont.len = 0;
1391 }
1392 return textlen;
1393 }
1394
1297 asmlinkage int vprintk_emit(int facility, int level, 1395 asmlinkage int vprintk_emit(int facility, int level,
1298 const char *dict, size_t dictlen, 1396 const char *dict, size_t dictlen,
1299 const char *fmt, va_list args) 1397 const char *fmt, va_list args)
1300 { 1398 {
1301 static int recursion_bug; 1399 static int recursion_bug;
1302 static char cont_buf[LOG_LINE_MAX];
1303 static size_t cont_len;
1304 static int cont_level;
1305 static struct task_struct *cont_task;
1306 static char textbuf[LOG_LINE_MAX]; 1400 static char textbuf[LOG_LINE_MAX];
1307 char *text = textbuf; 1401 char *text = textbuf;
1308 size_t text_len; 1402 size_t text_len;
1309 unsigned long flags; 1403 unsigned long flags;
1310 int this_cpu; 1404 int this_cpu;
1311 bool newline = false; 1405 bool newline = false;
1312 bool prefix = false; 1406 bool prefix = false;
1313 int printed_len = 0; 1407 int printed_len = 0;
1314 1408
1315 boot_delay_msec(); 1409 boot_delay_msec();
1316 printk_delay(); 1410 printk_delay();
1317 1411
1318 /* This stops the holder of console_sem just where we want him */ 1412 /* This stops the holder of console_sem just where we want him */
1319 local_irq_save(flags); 1413 local_irq_save(flags);
1320 this_cpu = smp_processor_id(); 1414 this_cpu = smp_processor_id();
1321 1415
1322 /* 1416 /*
1323 * Ouch, printk recursed into itself! 1417 * Ouch, printk recursed into itself!
1324 */ 1418 */
1325 if (unlikely(logbuf_cpu == this_cpu)) { 1419 if (unlikely(logbuf_cpu == this_cpu)) {
1326 /* 1420 /*
1327 * If a crash is occurring during printk() on this CPU, 1421 * If a crash is occurring during printk() on this CPU,
1328 * then try to get the crash message out but make sure 1422 * then try to get the crash message out but make sure
1329 * we can't deadlock. Otherwise just return to avoid the 1423 * we can't deadlock. Otherwise just return to avoid the
1330 * recursion and return - but flag the recursion so that 1424 * recursion and return - but flag the recursion so that
1331 * it can be printed at the next appropriate moment: 1425 * it can be printed at the next appropriate moment:
1332 */ 1426 */
1333 if (!oops_in_progress && !lockdep_recursing(current)) { 1427 if (!oops_in_progress && !lockdep_recursing(current)) {
1334 recursion_bug = 1; 1428 recursion_bug = 1;
1335 goto out_restore_irqs; 1429 goto out_restore_irqs;
1336 } 1430 }
1337 zap_locks(); 1431 zap_locks();
1338 } 1432 }
1339 1433
1340 lockdep_off(); 1434 lockdep_off();
1341 raw_spin_lock(&logbuf_lock); 1435 raw_spin_lock(&logbuf_lock);
1342 logbuf_cpu = this_cpu; 1436 logbuf_cpu = this_cpu;
1343 1437
1344 if (recursion_bug) { 1438 if (recursion_bug) {
1345 static const char recursion_msg[] = 1439 static const char recursion_msg[] =
1346 "BUG: recent printk recursion!"; 1440 "BUG: recent printk recursion!";
1347 1441
1348 recursion_bug = 0; 1442 recursion_bug = 0;
1349 printed_len += strlen(recursion_msg); 1443 printed_len += strlen(recursion_msg);
1350 /* emit KERN_CRIT message */ 1444 /* emit KERN_CRIT message */
1351 log_store(0, 2, NULL, 0, recursion_msg, printed_len); 1445 log_store(0, 2, LOG_DEFAULT, 0,
1446 NULL, 0, recursion_msg, printed_len);
1352 } 1447 }
1353 1448
1354 /* 1449 /*
1355 * The printf needs to come first; we need the syslog 1450 * The printf needs to come first; we need the syslog
1356 * prefix which might be passed-in as a parameter. 1451 * prefix which might be passed-in as a parameter.
1357 */ 1452 */
1358 text_len = vscnprintf(text, sizeof(textbuf), fmt, args); 1453 text_len = vscnprintf(text, sizeof(textbuf), fmt, args);
1359 1454
1360 /* mark and strip a trailing newline */ 1455 /* mark and strip a trailing newline */
1361 if (text_len && text[text_len-1] == '\n') { 1456 if (text_len && text[text_len-1] == '\n') {
1362 text_len--; 1457 text_len--;
1363 newline = true; 1458 newline = true;
1364 } 1459 }
1365 1460
1366 /* strip syslog prefix and extract log level or control flags */ 1461 /* strip syslog prefix and extract log level or control flags */
1367 if (text[0] == '<' && text[1] && text[2] == '>') { 1462 if (text[0] == '<' && text[1] && text[2] == '>') {
1368 switch (text[1]) { 1463 switch (text[1]) {
1369 case '0' ... '7': 1464 case '0' ... '7':
1370 if (level == -1) 1465 if (level == -1)
1371 level = text[1] - '0'; 1466 level = text[1] - '0';
1372 case 'd': /* KERN_DEFAULT */ 1467 case 'd': /* KERN_DEFAULT */
1373 prefix = true; 1468 prefix = true;
1374 case 'c': /* KERN_CONT */ 1469 case 'c': /* KERN_CONT */
1375 text += 3; 1470 text += 3;
1376 text_len -= 3; 1471 text_len -= 3;
1377 } 1472 }
1378 } 1473 }
1379 1474
1380 if (level == -1) 1475 if (level == -1)
1381 level = default_message_loglevel; 1476 level = default_message_loglevel;
1382 1477
1383 if (dict) { 1478 if (dict) {
1384 prefix = true; 1479 prefix = true;
1385 newline = true; 1480 newline = true;
1386 } 1481 }
1387 1482
1388 if (!newline) { 1483 if (!newline) {
1389 if (cont_len && (prefix || cont_task != current)) { 1484 /*
1390 /* 1485 * Flush the conflicting buffer. An earlier newline was missing,
1391 * Flush earlier buffer, which is either from a 1486 * or another task also prints continuation lines.
1392 * different thread, or when we got a new prefix. 1487 */
1393 */ 1488 if (cont.len && (prefix || cont.owner != current))
1394 log_store(facility, cont_level, NULL, 0, cont_buf, cont_len); 1489 cont_flush();
1395 cont_len = 0;
1396 }
1397 1490
1398 if (!cont_len) { 1491 /* buffer line if possible, otherwise store it right away */
1399 cont_level = level; 1492 if (!cont_add(facility, level, text, text_len))
1400 cont_task = current; 1493 log_store(facility, level, LOG_DEFAULT, 0,
1401 } 1494 dict, dictlen, text, text_len);
1402
1403 /* buffer or append to earlier buffer from the same thread */
1404 if (cont_len + text_len > sizeof(cont_buf))
1405 text_len = sizeof(cont_buf) - cont_len;
1406 memcpy(cont_buf + cont_len, text, text_len);
1407 cont_len += text_len;
1408 } else { 1495 } else {
1409 if (cont_len && cont_task == current) { 1496 bool stored = false;
1410 if (prefix) {
1411 /*
1412 * New prefix from the same thread; flush. We
1413 * either got no earlier newline, or we race
1414 * with an interrupt.
1415 */
1416 log_store(facility, cont_level,
1417 NULL, 0, cont_buf, cont_len);
1418 cont_len = 0;
1419 }
1420 1497
1421 /* append to the earlier buffer and flush */ 1498 /*
1422 if (cont_len + text_len > sizeof(cont_buf)) 1499 * Flush the conflicting buffer. An earlier newline was missing,
1423 text_len = sizeof(cont_buf) - cont_len; 1500 * or we race with a continuation line from an interrupt.
1424 memcpy(cont_buf + cont_len, text, text_len); 1501 */
1425 cont_len += text_len; 1502 if (cont.len && prefix && cont.owner == current)
1426 log_store(facility, cont_level, 1503 cont_flush();
1427 NULL, 0, cont_buf, cont_len); 1504
1428 cont_len = 0; 1505 /* Merge with our buffer if possible; flush it in any case */
1429 cont_task = NULL; 1506 if (cont.len && cont.owner == current) {
1430 printed_len = cont_len; 1507 stored = cont_add(facility, level, text, text_len);
1431 } else { 1508 cont_flush();
1432 /* ordinary single and terminated line */
1433 log_store(facility, level,
1434 dict, dictlen, text, text_len);
1435 printed_len = text_len;
1436 } 1509 }
1510
1511 if (!stored)
1512 log_store(facility, level, LOG_DEFAULT, 0,
1513 dict, dictlen, text, text_len);
1437 } 1514 }
1515 printed_len += text_len;
1438 1516
1439 /* 1517 /*
1440 * Try to acquire and then immediately release the console semaphore. 1518 * Try to acquire and then immediately release the console semaphore.
1441 * The release will print out buffers and wake up /dev/kmsg and syslog() 1519 * The release will print out buffers and wake up /dev/kmsg and syslog()
1442 * users. 1520 * users.
1443 * 1521 *
1444 * The console_trylock_for_printk() function will release 'logbuf_lock' 1522 * The console_trylock_for_printk() function will release 'logbuf_lock'
1445 * regardless of whether it actually gets the console semaphore or not. 1523 * regardless of whether it actually gets the console semaphore or not.
1446 */ 1524 */
1447 if (console_trylock_for_printk(this_cpu)) 1525 if (console_trylock_for_printk(this_cpu))
1448 console_unlock(); 1526 console_unlock();
1449 1527
1450 lockdep_on(); 1528 lockdep_on();
1451 out_restore_irqs: 1529 out_restore_irqs:
1452 local_irq_restore(flags); 1530 local_irq_restore(flags);
1453 1531
1454 return printed_len; 1532 return printed_len;
1455 } 1533 }
1456 EXPORT_SYMBOL(vprintk_emit); 1534 EXPORT_SYMBOL(vprintk_emit);
1457 1535
1458 asmlinkage int vprintk(const char *fmt, va_list args) 1536 asmlinkage int vprintk(const char *fmt, va_list args)
1459 { 1537 {
1460 return vprintk_emit(0, -1, NULL, 0, fmt, args); 1538 return vprintk_emit(0, -1, NULL, 0, fmt, args);
1461 } 1539 }
1462 EXPORT_SYMBOL(vprintk); 1540 EXPORT_SYMBOL(vprintk);
1463 1541
1464 asmlinkage int printk_emit(int facility, int level, 1542 asmlinkage int printk_emit(int facility, int level,
1465 const char *dict, size_t dictlen, 1543 const char *dict, size_t dictlen,
1466 const char *fmt, ...) 1544 const char *fmt, ...)
1467 { 1545 {
1468 va_list args; 1546 va_list args;
1469 int r; 1547 int r;
1470 1548
1471 va_start(args, fmt); 1549 va_start(args, fmt);
1472 r = vprintk_emit(facility, level, dict, dictlen, fmt, args); 1550 r = vprintk_emit(facility, level, dict, dictlen, fmt, args);
1473 va_end(args); 1551 va_end(args);
1474 1552
1475 return r; 1553 return r;
1476 } 1554 }
1477 EXPORT_SYMBOL(printk_emit); 1555 EXPORT_SYMBOL(printk_emit);
1478 1556
1479 /** 1557 /**
1480 * printk - print a kernel message 1558 * printk - print a kernel message
1481 * @fmt: format string 1559 * @fmt: format string
1482 * 1560 *
1483 * This is printk(). It can be called from any context. We want it to work. 1561 * This is printk(). It can be called from any context. We want it to work.
1484 * 1562 *
1485 * We try to grab the console_lock. If we succeed, it's easy - we log the 1563 * We try to grab the console_lock. If we succeed, it's easy - we log the
1486 * output and call the console drivers. If we fail to get the semaphore, we 1564 * output and call the console drivers. If we fail to get the semaphore, we
1487 * place the output into the log buffer and return. The current holder of 1565 * place the output into the log buffer and return. The current holder of
1488 * the console_sem will notice the new output in console_unlock(); and will 1566 * the console_sem will notice the new output in console_unlock(); and will
1489 * send it to the consoles before releasing the lock. 1567 * send it to the consoles before releasing the lock.
1490 * 1568 *
1491 * One effect of this deferred printing is that code which calls printk() and 1569 * One effect of this deferred printing is that code which calls printk() and
1492 * then changes console_loglevel may break. This is because console_loglevel 1570 * then changes console_loglevel may break. This is because console_loglevel
1493 * is inspected when the actual printing occurs. 1571 * is inspected when the actual printing occurs.
1494 * 1572 *
1495 * See also: 1573 * See also:
1496 * printf(3) 1574 * printf(3)
1497 * 1575 *
1498 * See the vsnprintf() documentation for format string extensions over C99. 1576 * See the vsnprintf() documentation for format string extensions over C99.
1499 */ 1577 */
1500 asmlinkage int printk(const char *fmt, ...) 1578 asmlinkage int printk(const char *fmt, ...)
1501 { 1579 {
1502 va_list args; 1580 va_list args;
1503 int r; 1581 int r;
1504 1582
1505 #ifdef CONFIG_KGDB_KDB 1583 #ifdef CONFIG_KGDB_KDB
1506 if (unlikely(kdb_trap_printk)) { 1584 if (unlikely(kdb_trap_printk)) {
1507 va_start(args, fmt); 1585 va_start(args, fmt);
1508 r = vkdb_printf(fmt, args); 1586 r = vkdb_printf(fmt, args);
1509 va_end(args); 1587 va_end(args);
1510 return r; 1588 return r;
1511 } 1589 }
1512 #endif 1590 #endif
1513 va_start(args, fmt); 1591 va_start(args, fmt);
1514 r = vprintk_emit(0, -1, NULL, 0, fmt, args); 1592 r = vprintk_emit(0, -1, NULL, 0, fmt, args);
1515 va_end(args); 1593 va_end(args);
1516 1594
1517 return r; 1595 return r;
1518 } 1596 }
1519 EXPORT_SYMBOL(printk); 1597 EXPORT_SYMBOL(printk);
1520 1598
1521 #else 1599 #else
1522 1600
1523 #define LOG_LINE_MAX 0 1601 #define LOG_LINE_MAX 0
1602 static struct cont {
1603 size_t len;
1604 size_t cons;
1605 u8 level;
1606 bool flushed:1;
1607 } cont;
1524 static struct log *log_from_idx(u32 idx) { return NULL; } 1608 static struct log *log_from_idx(u32 idx) { return NULL; }
1525 static u32 log_next(u32 idx) { return 0; } 1609 static u32 log_next(u32 idx) { return 0; }
1526 static void call_console_drivers(int level, const char *text, size_t len) {} 1610 static void call_console_drivers(int level, const char *text, size_t len) {}
1527 static size_t msg_print_text(const struct log *msg, bool syslog, 1611 static size_t msg_print_text(const struct log *msg, bool syslog,
1528 char *buf, size_t size) { return 0; } 1612 char *buf, size_t size) { return 0; }
1613 static size_t cont_print_text(char *text, size_t size) { return 0; }
1529 1614
1530 #endif /* CONFIG_PRINTK */ 1615 #endif /* CONFIG_PRINTK */
1531 1616
1532 static int __add_preferred_console(char *name, int idx, char *options, 1617 static int __add_preferred_console(char *name, int idx, char *options,
1533 char *brl_options) 1618 char *brl_options)
1534 { 1619 {
1535 struct console_cmdline *c; 1620 struct console_cmdline *c;
1536 int i; 1621 int i;
1537 1622
1538 /* 1623 /*
1539 * See if this tty is not yet registered, and 1624 * See if this tty is not yet registered, and
1540 * if we have a slot free. 1625 * if we have a slot free.
1541 */ 1626 */
1542 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++) 1627 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
1543 if (strcmp(console_cmdline[i].name, name) == 0 && 1628 if (strcmp(console_cmdline[i].name, name) == 0 &&
1544 console_cmdline[i].index == idx) { 1629 console_cmdline[i].index == idx) {
1545 if (!brl_options) 1630 if (!brl_options)
1546 selected_console = i; 1631 selected_console = i;
1547 return 0; 1632 return 0;
1548 } 1633 }
1549 if (i == MAX_CMDLINECONSOLES) 1634 if (i == MAX_CMDLINECONSOLES)
1550 return -E2BIG; 1635 return -E2BIG;
1551 if (!brl_options) 1636 if (!brl_options)
1552 selected_console = i; 1637 selected_console = i;
1553 c = &console_cmdline[i]; 1638 c = &console_cmdline[i];
1554 strlcpy(c->name, name, sizeof(c->name)); 1639 strlcpy(c->name, name, sizeof(c->name));
1555 c->options = options; 1640 c->options = options;
1556 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE 1641 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1557 c->brl_options = brl_options; 1642 c->brl_options = brl_options;
1558 #endif 1643 #endif
1559 c->index = idx; 1644 c->index = idx;
1560 return 0; 1645 return 0;
1561 } 1646 }
1562 /* 1647 /*
1563 * Set up a list of consoles. Called from init/main.c 1648 * Set up a list of consoles. Called from init/main.c
1564 */ 1649 */
1565 static int __init console_setup(char *str) 1650 static int __init console_setup(char *str)
1566 { 1651 {
1567 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */ 1652 char buf[sizeof(console_cmdline[0].name) + 4]; /* 4 for index */
1568 char *s, *options, *brl_options = NULL; 1653 char *s, *options, *brl_options = NULL;
1569 int idx; 1654 int idx;
1570 1655
1571 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE 1656 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
1572 if (!memcmp(str, "brl,", 4)) { 1657 if (!memcmp(str, "brl,", 4)) {
1573 brl_options = ""; 1658 brl_options = "";
1574 str += 4; 1659 str += 4;
1575 } else if (!memcmp(str, "brl=", 4)) { 1660 } else if (!memcmp(str, "brl=", 4)) {
1576 brl_options = str + 4; 1661 brl_options = str + 4;
1577 str = strchr(brl_options, ','); 1662 str = strchr(brl_options, ',');
1578 if (!str) { 1663 if (!str) {
1579 printk(KERN_ERR "need port name after brl=\n"); 1664 printk(KERN_ERR "need port name after brl=\n");
1580 return 1; 1665 return 1;
1581 } 1666 }
1582 *(str++) = 0; 1667 *(str++) = 0;
1583 } 1668 }
1584 #endif 1669 #endif
1585 1670
1586 /* 1671 /*
1587 * Decode str into name, index, options. 1672 * Decode str into name, index, options.
1588 */ 1673 */
1589 if (str[0] >= '0' && str[0] <= '9') { 1674 if (str[0] >= '0' && str[0] <= '9') {
1590 strcpy(buf, "ttyS"); 1675 strcpy(buf, "ttyS");
1591 strncpy(buf + 4, str, sizeof(buf) - 5); 1676 strncpy(buf + 4, str, sizeof(buf) - 5);
1592 } else { 1677 } else {
1593 strncpy(buf, str, sizeof(buf) - 1); 1678 strncpy(buf, str, sizeof(buf) - 1);
1594 } 1679 }
1595 buf[sizeof(buf) - 1] = 0; 1680 buf[sizeof(buf) - 1] = 0;
1596 if ((options = strchr(str, ',')) != NULL) 1681 if ((options = strchr(str, ',')) != NULL)
1597 *(options++) = 0; 1682 *(options++) = 0;
1598 #ifdef __sparc__ 1683 #ifdef __sparc__
1599 if (!strcmp(str, "ttya")) 1684 if (!strcmp(str, "ttya"))
1600 strcpy(buf, "ttyS0"); 1685 strcpy(buf, "ttyS0");
1601 if (!strcmp(str, "ttyb")) 1686 if (!strcmp(str, "ttyb"))
1602 strcpy(buf, "ttyS1"); 1687 strcpy(buf, "ttyS1");
1603 #endif 1688 #endif
1604 for (s = buf; *s; s++) 1689 for (s = buf; *s; s++)
1605 if ((*s >= '0' && *s <= '9') || *s == ',') 1690 if ((*s >= '0' && *s <= '9') || *s == ',')
1606 break; 1691 break;
1607 idx = simple_strtoul(s, NULL, 10); 1692 idx = simple_strtoul(s, NULL, 10);
1608 *s = 0; 1693 *s = 0;
1609 1694
1610 __add_preferred_console(buf, idx, options, brl_options); 1695 __add_preferred_console(buf, idx, options, brl_options);
1611 console_set_on_cmdline = 1; 1696 console_set_on_cmdline = 1;
1612 return 1; 1697 return 1;
1613 } 1698 }
1614 __setup("console=", console_setup); 1699 __setup("console=", console_setup);
1615 1700
1616 /** 1701 /**
1617 * add_preferred_console - add a device to the list of preferred consoles. 1702 * add_preferred_console - add a device to the list of preferred consoles.
1618 * @name: device name 1703 * @name: device name
1619 * @idx: device index 1704 * @idx: device index
1620 * @options: options for this console 1705 * @options: options for this console
1621 * 1706 *
1622 * The last preferred console added will be used for kernel messages 1707 * The last preferred console added will be used for kernel messages
1623 * and stdin/out/err for init. Normally this is used by console_setup 1708 * and stdin/out/err for init. Normally this is used by console_setup
1624 * above to handle user-supplied console arguments; however it can also 1709 * above to handle user-supplied console arguments; however it can also
1625 * be used by arch-specific code either to override the user or more 1710 * be used by arch-specific code either to override the user or more
1626 * commonly to provide a default console (ie from PROM variables) when 1711 * commonly to provide a default console (ie from PROM variables) when
1627 * the user has not supplied one. 1712 * the user has not supplied one.
1628 */ 1713 */
1629 int add_preferred_console(char *name, int idx, char *options) 1714 int add_preferred_console(char *name, int idx, char *options)
1630 { 1715 {
1631 return __add_preferred_console(name, idx, options, NULL); 1716 return __add_preferred_console(name, idx, options, NULL);
1632 } 1717 }
1633 1718
1634 int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options) 1719 int update_console_cmdline(char *name, int idx, char *name_new, int idx_new, char *options)
1635 { 1720 {
1636 struct console_cmdline *c; 1721 struct console_cmdline *c;
1637 int i; 1722 int i;
1638 1723
1639 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++) 1724 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; i++)
1640 if (strcmp(console_cmdline[i].name, name) == 0 && 1725 if (strcmp(console_cmdline[i].name, name) == 0 &&
1641 console_cmdline[i].index == idx) { 1726 console_cmdline[i].index == idx) {
1642 c = &console_cmdline[i]; 1727 c = &console_cmdline[i];
1643 strlcpy(c->name, name_new, sizeof(c->name)); 1728 strlcpy(c->name, name_new, sizeof(c->name));
1644 c->name[sizeof(c->name) - 1] = 0; 1729 c->name[sizeof(c->name) - 1] = 0;
1645 c->options = options; 1730 c->options = options;
1646 c->index = idx_new; 1731 c->index = idx_new;
1647 return i; 1732 return i;
1648 } 1733 }
1649 /* not found */ 1734 /* not found */
1650 return -1; 1735 return -1;
1651 } 1736 }
1652 1737
1653 bool console_suspend_enabled = 1; 1738 bool console_suspend_enabled = 1;
1654 EXPORT_SYMBOL(console_suspend_enabled); 1739 EXPORT_SYMBOL(console_suspend_enabled);
1655 1740
1656 static int __init console_suspend_disable(char *str) 1741 static int __init console_suspend_disable(char *str)
1657 { 1742 {
1658 console_suspend_enabled = 0; 1743 console_suspend_enabled = 0;
1659 return 1; 1744 return 1;
1660 } 1745 }
1661 __setup("no_console_suspend", console_suspend_disable); 1746 __setup("no_console_suspend", console_suspend_disable);
1662 module_param_named(console_suspend, console_suspend_enabled, 1747 module_param_named(console_suspend, console_suspend_enabled,
1663 bool, S_IRUGO | S_IWUSR); 1748 bool, S_IRUGO | S_IWUSR);
1664 MODULE_PARM_DESC(console_suspend, "suspend console during suspend" 1749 MODULE_PARM_DESC(console_suspend, "suspend console during suspend"
1665 " and hibernate operations"); 1750 " and hibernate operations");
1666 1751
1667 /** 1752 /**
1668 * suspend_console - suspend the console subsystem 1753 * suspend_console - suspend the console subsystem
1669 * 1754 *
1670 * This disables printk() while we go into suspend states 1755 * This disables printk() while we go into suspend states
1671 */ 1756 */
1672 void suspend_console(void) 1757 void suspend_console(void)
1673 { 1758 {
1674 if (!console_suspend_enabled) 1759 if (!console_suspend_enabled)
1675 return; 1760 return;
1676 printk("Suspending console(s) (use no_console_suspend to debug)\n"); 1761 printk("Suspending console(s) (use no_console_suspend to debug)\n");
1677 console_lock(); 1762 console_lock();
1678 console_suspended = 1; 1763 console_suspended = 1;
1679 up(&console_sem); 1764 up(&console_sem);
1680 } 1765 }
1681 1766
1682 void resume_console(void) 1767 void resume_console(void)
1683 { 1768 {
1684 if (!console_suspend_enabled) 1769 if (!console_suspend_enabled)
1685 return; 1770 return;
1686 down(&console_sem); 1771 down(&console_sem);
1687 console_suspended = 0; 1772 console_suspended = 0;
1688 console_unlock(); 1773 console_unlock();
1689 } 1774 }
1690 1775
1691 /** 1776 /**
1692 * console_cpu_notify - print deferred console messages after CPU hotplug 1777 * console_cpu_notify - print deferred console messages after CPU hotplug
1693 * @self: notifier struct 1778 * @self: notifier struct
1694 * @action: CPU hotplug event 1779 * @action: CPU hotplug event
1695 * @hcpu: unused 1780 * @hcpu: unused
1696 * 1781 *
1697 * If printk() is called from a CPU that is not online yet, the messages 1782 * If printk() is called from a CPU that is not online yet, the messages
1698 * will be spooled but will not show up on the console. This function is 1783 * will be spooled but will not show up on the console. This function is
1699 * called when a new CPU comes online (or fails to come up), and ensures 1784 * called when a new CPU comes online (or fails to come up), and ensures
1700 * that any such output gets printed. 1785 * that any such output gets printed.
1701 */ 1786 */
1702 static int __cpuinit console_cpu_notify(struct notifier_block *self, 1787 static int __cpuinit console_cpu_notify(struct notifier_block *self,
1703 unsigned long action, void *hcpu) 1788 unsigned long action, void *hcpu)
1704 { 1789 {
1705 switch (action) { 1790 switch (action) {
1706 case CPU_ONLINE: 1791 case CPU_ONLINE:
1707 case CPU_DEAD: 1792 case CPU_DEAD:
1708 case CPU_DYING: 1793 case CPU_DYING:
1709 case CPU_DOWN_FAILED: 1794 case CPU_DOWN_FAILED:
1710 case CPU_UP_CANCELED: 1795 case CPU_UP_CANCELED:
1711 console_lock(); 1796 console_lock();
1712 console_unlock(); 1797 console_unlock();
1713 } 1798 }
1714 return NOTIFY_OK; 1799 return NOTIFY_OK;
1715 } 1800 }
1716 1801
1717 /** 1802 /**
1718 * console_lock - lock the console system for exclusive use. 1803 * console_lock - lock the console system for exclusive use.
1719 * 1804 *
1720 * Acquires a lock which guarantees that the caller has 1805 * Acquires a lock which guarantees that the caller has
1721 * exclusive access to the console system and the console_drivers list. 1806 * exclusive access to the console system and the console_drivers list.
1722 * 1807 *
1723 * Can sleep, returns nothing. 1808 * Can sleep, returns nothing.
1724 */ 1809 */
1725 void console_lock(void) 1810 void console_lock(void)
1726 { 1811 {
1727 BUG_ON(in_interrupt()); 1812 BUG_ON(in_interrupt());
1728 down(&console_sem); 1813 down(&console_sem);
1729 if (console_suspended) 1814 if (console_suspended)
1730 return; 1815 return;
1731 console_locked = 1; 1816 console_locked = 1;
1732 console_may_schedule = 1; 1817 console_may_schedule = 1;
1733 } 1818 }
1734 EXPORT_SYMBOL(console_lock); 1819 EXPORT_SYMBOL(console_lock);
1735 1820
1736 /** 1821 /**
1737 * console_trylock - try to lock the console system for exclusive use. 1822 * console_trylock - try to lock the console system for exclusive use.
1738 * 1823 *
1739 * Tried to acquire a lock which guarantees that the caller has 1824 * Tried to acquire a lock which guarantees that the caller has
1740 * exclusive access to the console system and the console_drivers list. 1825 * exclusive access to the console system and the console_drivers list.
1741 * 1826 *
1742 * returns 1 on success, and 0 on failure to acquire the lock. 1827 * returns 1 on success, and 0 on failure to acquire the lock.
1743 */ 1828 */
1744 int console_trylock(void) 1829 int console_trylock(void)
1745 { 1830 {
1746 if (down_trylock(&console_sem)) 1831 if (down_trylock(&console_sem))
1747 return 0; 1832 return 0;
1748 if (console_suspended) { 1833 if (console_suspended) {
1749 up(&console_sem); 1834 up(&console_sem);
1750 return 0; 1835 return 0;
1751 } 1836 }
1752 console_locked = 1; 1837 console_locked = 1;
1753 console_may_schedule = 0; 1838 console_may_schedule = 0;
1754 return 1; 1839 return 1;
1755 } 1840 }
1756 EXPORT_SYMBOL(console_trylock); 1841 EXPORT_SYMBOL(console_trylock);
1757 1842
1758 int is_console_locked(void) 1843 int is_console_locked(void)
1759 { 1844 {
1760 return console_locked; 1845 return console_locked;
1761 } 1846 }
1762 1847
1763 /* 1848 /*
1764 * Delayed printk version, for scheduler-internal messages: 1849 * Delayed printk version, for scheduler-internal messages:
1765 */ 1850 */
1766 #define PRINTK_BUF_SIZE 512 1851 #define PRINTK_BUF_SIZE 512
1767 1852
1768 #define PRINTK_PENDING_WAKEUP 0x01 1853 #define PRINTK_PENDING_WAKEUP 0x01
1769 #define PRINTK_PENDING_SCHED 0x02 1854 #define PRINTK_PENDING_SCHED 0x02
1770 1855
1771 static DEFINE_PER_CPU(int, printk_pending); 1856 static DEFINE_PER_CPU(int, printk_pending);
1772 static DEFINE_PER_CPU(char [PRINTK_BUF_SIZE], printk_sched_buf); 1857 static DEFINE_PER_CPU(char [PRINTK_BUF_SIZE], printk_sched_buf);
1773 1858
1774 void printk_tick(void) 1859 void printk_tick(void)
1775 { 1860 {
1776 if (__this_cpu_read(printk_pending)) { 1861 if (__this_cpu_read(printk_pending)) {
1777 int pending = __this_cpu_xchg(printk_pending, 0); 1862 int pending = __this_cpu_xchg(printk_pending, 0);
1778 if (pending & PRINTK_PENDING_SCHED) { 1863 if (pending & PRINTK_PENDING_SCHED) {
1779 char *buf = __get_cpu_var(printk_sched_buf); 1864 char *buf = __get_cpu_var(printk_sched_buf);
1780 printk(KERN_WARNING "[sched_delayed] %s", buf); 1865 printk(KERN_WARNING "[sched_delayed] %s", buf);
1781 } 1866 }
1782 if (pending & PRINTK_PENDING_WAKEUP) 1867 if (pending & PRINTK_PENDING_WAKEUP)
1783 wake_up_interruptible(&log_wait); 1868 wake_up_interruptible(&log_wait);
1784 } 1869 }
1785 } 1870 }
1786 1871
1787 int printk_needs_cpu(int cpu) 1872 int printk_needs_cpu(int cpu)
1788 { 1873 {
1789 if (cpu_is_offline(cpu)) 1874 if (cpu_is_offline(cpu))
1790 printk_tick(); 1875 printk_tick();
1791 return __this_cpu_read(printk_pending); 1876 return __this_cpu_read(printk_pending);
1792 } 1877 }
1793 1878
1794 void wake_up_klogd(void) 1879 void wake_up_klogd(void)
1795 { 1880 {
1796 if (waitqueue_active(&log_wait)) 1881 if (waitqueue_active(&log_wait))
1797 this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP); 1882 this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP);
1798 } 1883 }
1799 1884
1800 /* the next printk record to write to the console */ 1885 /* the next printk record to write to the console */
1801 static u64 console_seq; 1886 static u64 console_seq;
1802 static u32 console_idx; 1887 static u32 console_idx;
1803 1888
1804 /** 1889 /**
1805 * console_unlock - unlock the console system 1890 * console_unlock - unlock the console system
1806 * 1891 *
1807 * Releases the console_lock which the caller holds on the console system 1892 * Releases the console_lock which the caller holds on the console system
1808 * and the console driver list. 1893 * and the console driver list.
1809 * 1894 *
1810 * While the console_lock was held, console output may have been buffered 1895 * While the console_lock was held, console output may have been buffered
1811 * by printk(). If this is the case, console_unlock(); emits 1896 * by printk(). If this is the case, console_unlock(); emits
1812 * the output prior to releasing the lock. 1897 * the output prior to releasing the lock.
1813 * 1898 *
1814 * If there is output waiting, we wake /dev/kmsg and syslog() users. 1899 * If there is output waiting, we wake /dev/kmsg and syslog() users.
1815 * 1900 *
1816 * console_unlock(); may be called from any context. 1901 * console_unlock(); may be called from any context.
1817 */ 1902 */
1818 void console_unlock(void) 1903 void console_unlock(void)
1819 { 1904 {
1905 static char text[LOG_LINE_MAX];
1820 static u64 seen_seq; 1906 static u64 seen_seq;
1821 unsigned long flags; 1907 unsigned long flags;
1822 bool wake_klogd = false; 1908 bool wake_klogd = false;
1823 bool retry; 1909 bool retry;
1824 1910
1825 if (console_suspended) { 1911 if (console_suspended) {
1826 up(&console_sem); 1912 up(&console_sem);
1827 return; 1913 return;
1828 } 1914 }
1829 1915
1830 console_may_schedule = 0; 1916 console_may_schedule = 0;
1831 1917
1918 /* flush buffered message fragment immediately to console */
1919 raw_spin_lock_irqsave(&logbuf_lock, flags);
1920 if (cont.len && (cont.cons < cont.len || cont.flushed)) {
1921 size_t len;
1922
1923 len = cont_print_text(text, sizeof(text));
1924 raw_spin_unlock(&logbuf_lock);
1925 stop_critical_timings();
1926 call_console_drivers(cont.level, text, len);
1927 start_critical_timings();
1928 local_irq_restore(flags);
1929 } else
1930 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
1931
1832 again: 1932 again:
1833 for (;;) { 1933 for (;;) {
1834 struct log *msg; 1934 struct log *msg;
1835 static char text[LOG_LINE_MAX];
1836 size_t len; 1935 size_t len;
1837 int level; 1936 int level;
1838 1937
1839 raw_spin_lock_irqsave(&logbuf_lock, flags); 1938 raw_spin_lock_irqsave(&logbuf_lock, flags);
1840 if (seen_seq != log_next_seq) { 1939 if (seen_seq != log_next_seq) {
1841 wake_klogd = true; 1940 wake_klogd = true;
1842 seen_seq = log_next_seq; 1941 seen_seq = log_next_seq;
1843 } 1942 }
1844 1943
1845 if (console_seq < log_first_seq) { 1944 if (console_seq < log_first_seq) {
1846 /* messages are gone, move to first one */ 1945 /* messages are gone, move to first one */
1847 console_seq = log_first_seq; 1946 console_seq = log_first_seq;
1848 console_idx = log_first_idx; 1947 console_idx = log_first_idx;
1849 } 1948 }
1850 1949 skip:
1851 if (console_seq == log_next_seq) 1950 if (console_seq == log_next_seq)
1852 break; 1951 break;
1853 1952
1854 msg = log_from_idx(console_idx); 1953 msg = log_from_idx(console_idx);
1855 level = msg->level & 7; 1954 if (msg->flags & LOG_NOCONS) {
1955 /*
1956 * Skip record we have buffered and already printed
1957 * directly to the console when we received it.
1958 */
1959 console_idx = log_next(console_idx);
1960 console_seq++;
1961 goto skip;
1962 }
1856 1963
1964 level = msg->level;
1857 len = msg_print_text(msg, false, text, sizeof(text)); 1965 len = msg_print_text(msg, false, text, sizeof(text));
1858 1966
1859 console_idx = log_next(console_idx); 1967 console_idx = log_next(console_idx);
1860 console_seq++; 1968 console_seq++;
1861 raw_spin_unlock(&logbuf_lock); 1969 raw_spin_unlock(&logbuf_lock);
1862 1970
1863 stop_critical_timings(); /* don't trace print latency */ 1971 stop_critical_timings(); /* don't trace print latency */
1864 call_console_drivers(level, text, len); 1972 call_console_drivers(level, text, len);
1865 start_critical_timings(); 1973 start_critical_timings();
1866 local_irq_restore(flags); 1974 local_irq_restore(flags);
1867 } 1975 }
1868 console_locked = 0; 1976 console_locked = 0;
1869 1977
1870 /* Release the exclusive_console once it is used */ 1978 /* Release the exclusive_console once it is used */
1871 if (unlikely(exclusive_console)) 1979 if (unlikely(exclusive_console))
1872 exclusive_console = NULL; 1980 exclusive_console = NULL;
1873 1981
1874 raw_spin_unlock(&logbuf_lock); 1982 raw_spin_unlock(&logbuf_lock);
1875 1983
1876 up(&console_sem); 1984 up(&console_sem);
1877 1985
1878 /* 1986 /*
1879 * Someone could have filled up the buffer again, so re-check if there's 1987 * Someone could have filled up the buffer again, so re-check if there's
1880 * something to flush. In case we cannot trylock the console_sem again, 1988 * something to flush. In case we cannot trylock the console_sem again,
1881 * there's a new owner and the console_unlock() from them will do the 1989 * there's a new owner and the console_unlock() from them will do the
1882 * flush, no worries. 1990 * flush, no worries.
1883 */ 1991 */
1884 raw_spin_lock(&logbuf_lock); 1992 raw_spin_lock(&logbuf_lock);
1885 retry = console_seq != log_next_seq; 1993 retry = console_seq != log_next_seq;
1886 raw_spin_unlock_irqrestore(&logbuf_lock, flags); 1994 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
1887 1995
1888 if (retry && console_trylock()) 1996 if (retry && console_trylock())
1889 goto again; 1997 goto again;
1890 1998
1891 if (wake_klogd) 1999 if (wake_klogd)
1892 wake_up_klogd(); 2000 wake_up_klogd();
1893 } 2001 }
1894 EXPORT_SYMBOL(console_unlock); 2002 EXPORT_SYMBOL(console_unlock);
1895 2003
1896 /** 2004 /**
1897 * console_conditional_schedule - yield the CPU if required 2005 * console_conditional_schedule - yield the CPU if required
1898 * 2006 *
1899 * If the console code is currently allowed to sleep, and 2007 * If the console code is currently allowed to sleep, and
1900 * if this CPU should yield the CPU to another task, do 2008 * if this CPU should yield the CPU to another task, do
1901 * so here. 2009 * so here.
1902 * 2010 *
1903 * Must be called within console_lock();. 2011 * Must be called within console_lock();.
1904 */ 2012 */
1905 void __sched console_conditional_schedule(void) 2013 void __sched console_conditional_schedule(void)
1906 { 2014 {
1907 if (console_may_schedule) 2015 if (console_may_schedule)
1908 cond_resched(); 2016 cond_resched();
1909 } 2017 }
1910 EXPORT_SYMBOL(console_conditional_schedule); 2018 EXPORT_SYMBOL(console_conditional_schedule);
1911 2019
1912 void console_unblank(void) 2020 void console_unblank(void)
1913 { 2021 {
1914 struct console *c; 2022 struct console *c;
1915 2023
1916 /* 2024 /*
1917 * console_unblank can no longer be called in interrupt context unless 2025 * console_unblank can no longer be called in interrupt context unless
1918 * oops_in_progress is set to 1.. 2026 * oops_in_progress is set to 1..
1919 */ 2027 */
1920 if (oops_in_progress) { 2028 if (oops_in_progress) {
1921 if (down_trylock(&console_sem) != 0) 2029 if (down_trylock(&console_sem) != 0)
1922 return; 2030 return;
1923 } else 2031 } else
1924 console_lock(); 2032 console_lock();
1925 2033
1926 console_locked = 1; 2034 console_locked = 1;
1927 console_may_schedule = 0; 2035 console_may_schedule = 0;
1928 for_each_console(c) 2036 for_each_console(c)
1929 if ((c->flags & CON_ENABLED) && c->unblank) 2037 if ((c->flags & CON_ENABLED) && c->unblank)
1930 c->unblank(); 2038 c->unblank();
1931 console_unlock(); 2039 console_unlock();
1932 } 2040 }
1933 2041
1934 /* 2042 /*
1935 * Return the console tty driver structure and its associated index 2043 * Return the console tty driver structure and its associated index
1936 */ 2044 */
1937 struct tty_driver *console_device(int *index) 2045 struct tty_driver *console_device(int *index)
1938 { 2046 {
1939 struct console *c; 2047 struct console *c;
1940 struct tty_driver *driver = NULL; 2048 struct tty_driver *driver = NULL;
1941 2049
1942 console_lock(); 2050 console_lock();
1943 for_each_console(c) { 2051 for_each_console(c) {
1944 if (!c->device) 2052 if (!c->device)
1945 continue; 2053 continue;
1946 driver = c->device(c, index); 2054 driver = c->device(c, index);
1947 if (driver) 2055 if (driver)
1948 break; 2056 break;
1949 } 2057 }
1950 console_unlock(); 2058 console_unlock();
1951 return driver; 2059 return driver;
1952 } 2060 }
1953 2061
1954 /* 2062 /*
1955 * Prevent further output on the passed console device so that (for example) 2063 * Prevent further output on the passed console device so that (for example)
1956 * serial drivers can disable console output before suspending a port, and can 2064 * serial drivers can disable console output before suspending a port, and can
1957 * re-enable output afterwards. 2065 * re-enable output afterwards.
1958 */ 2066 */
1959 void console_stop(struct console *console) 2067 void console_stop(struct console *console)
1960 { 2068 {
1961 console_lock(); 2069 console_lock();
1962 console->flags &= ~CON_ENABLED; 2070 console->flags &= ~CON_ENABLED;
1963 console_unlock(); 2071 console_unlock();
1964 } 2072 }
1965 EXPORT_SYMBOL(console_stop); 2073 EXPORT_SYMBOL(console_stop);
1966 2074
1967 void console_start(struct console *console) 2075 void console_start(struct console *console)
1968 { 2076 {
1969 console_lock(); 2077 console_lock();
1970 console->flags |= CON_ENABLED; 2078 console->flags |= CON_ENABLED;
1971 console_unlock(); 2079 console_unlock();
1972 } 2080 }
1973 EXPORT_SYMBOL(console_start); 2081 EXPORT_SYMBOL(console_start);
1974 2082
1975 static int __read_mostly keep_bootcon; 2083 static int __read_mostly keep_bootcon;
1976 2084
1977 static int __init keep_bootcon_setup(char *str) 2085 static int __init keep_bootcon_setup(char *str)
1978 { 2086 {
1979 keep_bootcon = 1; 2087 keep_bootcon = 1;
1980 printk(KERN_INFO "debug: skip boot console de-registration.\n"); 2088 printk(KERN_INFO "debug: skip boot console de-registration.\n");
1981 2089
1982 return 0; 2090 return 0;
1983 } 2091 }
1984 2092
1985 early_param("keep_bootcon", keep_bootcon_setup); 2093 early_param("keep_bootcon", keep_bootcon_setup);
1986 2094
1987 /* 2095 /*
1988 * The console driver calls this routine during kernel initialization 2096 * The console driver calls this routine during kernel initialization
1989 * to register the console printing procedure with printk() and to 2097 * to register the console printing procedure with printk() and to
1990 * print any messages that were printed by the kernel before the 2098 * print any messages that were printed by the kernel before the
1991 * console driver was initialized. 2099 * console driver was initialized.
1992 * 2100 *
1993 * This can happen pretty early during the boot process (because of 2101 * This can happen pretty early during the boot process (because of
1994 * early_printk) - sometimes before setup_arch() completes - be careful 2102 * early_printk) - sometimes before setup_arch() completes - be careful
1995 * of what kernel features are used - they may not be initialised yet. 2103 * of what kernel features are used - they may not be initialised yet.
1996 * 2104 *
1997 * There are two types of consoles - bootconsoles (early_printk) and 2105 * There are two types of consoles - bootconsoles (early_printk) and
1998 * "real" consoles (everything which is not a bootconsole) which are 2106 * "real" consoles (everything which is not a bootconsole) which are
1999 * handled differently. 2107 * handled differently.
2000 * - Any number of bootconsoles can be registered at any time. 2108 * - Any number of bootconsoles can be registered at any time.
2001 * - As soon as a "real" console is registered, all bootconsoles 2109 * - As soon as a "real" console is registered, all bootconsoles
2002 * will be unregistered automatically. 2110 * will be unregistered automatically.
2003 * - Once a "real" console is registered, any attempt to register a 2111 * - Once a "real" console is registered, any attempt to register a
2004 * bootconsoles will be rejected 2112 * bootconsoles will be rejected
2005 */ 2113 */
2006 void register_console(struct console *newcon) 2114 void register_console(struct console *newcon)
2007 { 2115 {
2008 int i; 2116 int i;
2009 unsigned long flags; 2117 unsigned long flags;
2010 struct console *bcon = NULL; 2118 struct console *bcon = NULL;
2011 2119
2012 /* 2120 /*
2013 * before we register a new CON_BOOT console, make sure we don't 2121 * before we register a new CON_BOOT console, make sure we don't
2014 * already have a valid console 2122 * already have a valid console
2015 */ 2123 */
2016 if (console_drivers && newcon->flags & CON_BOOT) { 2124 if (console_drivers && newcon->flags & CON_BOOT) {
2017 /* find the last or real console */ 2125 /* find the last or real console */
2018 for_each_console(bcon) { 2126 for_each_console(bcon) {
2019 if (!(bcon->flags & CON_BOOT)) { 2127 if (!(bcon->flags & CON_BOOT)) {
2020 printk(KERN_INFO "Too late to register bootconsole %s%d\n", 2128 printk(KERN_INFO "Too late to register bootconsole %s%d\n",
2021 newcon->name, newcon->index); 2129 newcon->name, newcon->index);
2022 return; 2130 return;
2023 } 2131 }
2024 } 2132 }
2025 } 2133 }
2026 2134
2027 if (console_drivers && console_drivers->flags & CON_BOOT) 2135 if (console_drivers && console_drivers->flags & CON_BOOT)
2028 bcon = console_drivers; 2136 bcon = console_drivers;
2029 2137
2030 if (preferred_console < 0 || bcon || !console_drivers) 2138 if (preferred_console < 0 || bcon || !console_drivers)
2031 preferred_console = selected_console; 2139 preferred_console = selected_console;
2032 2140
2033 if (newcon->early_setup) 2141 if (newcon->early_setup)
2034 newcon->early_setup(); 2142 newcon->early_setup();
2035 2143
2036 /* 2144 /*
2037 * See if we want to use this console driver. If we 2145 * See if we want to use this console driver. If we
2038 * didn't select a console we take the first one 2146 * didn't select a console we take the first one
2039 * that registers here. 2147 * that registers here.
2040 */ 2148 */
2041 if (preferred_console < 0) { 2149 if (preferred_console < 0) {
2042 if (newcon->index < 0) 2150 if (newcon->index < 0)
2043 newcon->index = 0; 2151 newcon->index = 0;
2044 if (newcon->setup == NULL || 2152 if (newcon->setup == NULL ||
2045 newcon->setup(newcon, NULL) == 0) { 2153 newcon->setup(newcon, NULL) == 0) {
2046 newcon->flags |= CON_ENABLED; 2154 newcon->flags |= CON_ENABLED;
2047 if (newcon->device) { 2155 if (newcon->device) {
2048 newcon->flags |= CON_CONSDEV; 2156 newcon->flags |= CON_CONSDEV;
2049 preferred_console = 0; 2157 preferred_console = 0;
2050 } 2158 }
2051 } 2159 }
2052 } 2160 }
2053 2161
2054 /* 2162 /*
2055 * See if this console matches one we selected on 2163 * See if this console matches one we selected on
2056 * the command line. 2164 * the command line.
2057 */ 2165 */
2058 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0]; 2166 for (i = 0; i < MAX_CMDLINECONSOLES && console_cmdline[i].name[0];
2059 i++) { 2167 i++) {
2060 if (strcmp(console_cmdline[i].name, newcon->name) != 0) 2168 if (strcmp(console_cmdline[i].name, newcon->name) != 0)
2061 continue; 2169 continue;
2062 if (newcon->index >= 0 && 2170 if (newcon->index >= 0 &&
2063 newcon->index != console_cmdline[i].index) 2171 newcon->index != console_cmdline[i].index)
2064 continue; 2172 continue;
2065 if (newcon->index < 0) 2173 if (newcon->index < 0)
2066 newcon->index = console_cmdline[i].index; 2174 newcon->index = console_cmdline[i].index;
2067 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE 2175 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
2068 if (console_cmdline[i].brl_options) { 2176 if (console_cmdline[i].brl_options) {
2069 newcon->flags |= CON_BRL; 2177 newcon->flags |= CON_BRL;
2070 braille_register_console(newcon, 2178 braille_register_console(newcon,
2071 console_cmdline[i].index, 2179 console_cmdline[i].index,
2072 console_cmdline[i].options, 2180 console_cmdline[i].options,
2073 console_cmdline[i].brl_options); 2181 console_cmdline[i].brl_options);
2074 return; 2182 return;
2075 } 2183 }
2076 #endif 2184 #endif
2077 if (newcon->setup && 2185 if (newcon->setup &&
2078 newcon->setup(newcon, console_cmdline[i].options) != 0) 2186 newcon->setup(newcon, console_cmdline[i].options) != 0)
2079 break; 2187 break;
2080 newcon->flags |= CON_ENABLED; 2188 newcon->flags |= CON_ENABLED;
2081 newcon->index = console_cmdline[i].index; 2189 newcon->index = console_cmdline[i].index;
2082 if (i == selected_console) { 2190 if (i == selected_console) {
2083 newcon->flags |= CON_CONSDEV; 2191 newcon->flags |= CON_CONSDEV;
2084 preferred_console = selected_console; 2192 preferred_console = selected_console;
2085 } 2193 }
2086 break; 2194 break;
2087 } 2195 }
2088 2196
2089 if (!(newcon->flags & CON_ENABLED)) 2197 if (!(newcon->flags & CON_ENABLED))
2090 return; 2198 return;
2091 2199
2092 /* 2200 /*
2093 * If we have a bootconsole, and are switching to a real console, 2201 * If we have a bootconsole, and are switching to a real console,
2094 * don't print everything out again, since when the boot console, and 2202 * don't print everything out again, since when the boot console, and
2095 * the real console are the same physical device, it's annoying to 2203 * the real console are the same physical device, it's annoying to
2096 * see the beginning boot messages twice 2204 * see the beginning boot messages twice
2097 */ 2205 */
2098 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV)) 2206 if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
2099 newcon->flags &= ~CON_PRINTBUFFER; 2207 newcon->flags &= ~CON_PRINTBUFFER;
2100 2208
2101 /* 2209 /*
2102 * Put this console in the list - keep the 2210 * Put this console in the list - keep the
2103 * preferred driver at the head of the list. 2211 * preferred driver at the head of the list.
2104 */ 2212 */
2105 console_lock(); 2213 console_lock();
2106 if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) { 2214 if ((newcon->flags & CON_CONSDEV) || console_drivers == NULL) {
2107 newcon->next = console_drivers; 2215 newcon->next = console_drivers;
2108 console_drivers = newcon; 2216 console_drivers = newcon;
2109 if (newcon->next) 2217 if (newcon->next)
2110 newcon->next->flags &= ~CON_CONSDEV; 2218 newcon->next->flags &= ~CON_CONSDEV;
2111 } else { 2219 } else {
2112 newcon->next = console_drivers->next; 2220 newcon->next = console_drivers->next;
2113 console_drivers->next = newcon; 2221 console_drivers->next = newcon;
2114 } 2222 }
2115 if (newcon->flags & CON_PRINTBUFFER) { 2223 if (newcon->flags & CON_PRINTBUFFER) {
2116 /* 2224 /*
2117 * console_unlock(); will print out the buffered messages 2225 * console_unlock(); will print out the buffered messages
2118 * for us. 2226 * for us.
2119 */ 2227 */
2120 raw_spin_lock_irqsave(&logbuf_lock, flags); 2228 raw_spin_lock_irqsave(&logbuf_lock, flags);
2121 console_seq = syslog_seq; 2229 console_seq = syslog_seq;
2122 console_idx = syslog_idx; 2230 console_idx = syslog_idx;
2123 raw_spin_unlock_irqrestore(&logbuf_lock, flags); 2231 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2124 /* 2232 /*
2125 * We're about to replay the log buffer. Only do this to the 2233 * We're about to replay the log buffer. Only do this to the
2126 * just-registered console to avoid excessive message spam to 2234 * just-registered console to avoid excessive message spam to
2127 * the already-registered consoles. 2235 * the already-registered consoles.
2128 */ 2236 */
2129 exclusive_console = newcon; 2237 exclusive_console = newcon;
2130 } 2238 }
2131 console_unlock(); 2239 console_unlock();
2132 console_sysfs_notify(); 2240 console_sysfs_notify();
2133 2241
2134 /* 2242 /*
2135 * By unregistering the bootconsoles after we enable the real console 2243 * By unregistering the bootconsoles after we enable the real console
2136 * we get the "console xxx enabled" message on all the consoles - 2244 * we get the "console xxx enabled" message on all the consoles -
2137 * boot consoles, real consoles, etc - this is to ensure that end 2245 * boot consoles, real consoles, etc - this is to ensure that end
2138 * users know there might be something in the kernel's log buffer that 2246 * users know there might be something in the kernel's log buffer that
2139 * went to the bootconsole (that they do not see on the real console) 2247 * went to the bootconsole (that they do not see on the real console)
2140 */ 2248 */
2141 if (bcon && 2249 if (bcon &&
2142 ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) && 2250 ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV) &&
2143 !keep_bootcon) { 2251 !keep_bootcon) {
2144 /* we need to iterate through twice, to make sure we print 2252 /* we need to iterate through twice, to make sure we print
2145 * everything out, before we unregister the console(s) 2253 * everything out, before we unregister the console(s)
2146 */ 2254 */
2147 printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n", 2255 printk(KERN_INFO "console [%s%d] enabled, bootconsole disabled\n",
2148 newcon->name, newcon->index); 2256 newcon->name, newcon->index);
2149 for_each_console(bcon) 2257 for_each_console(bcon)
2150 if (bcon->flags & CON_BOOT) 2258 if (bcon->flags & CON_BOOT)
2151 unregister_console(bcon); 2259 unregister_console(bcon);
2152 } else { 2260 } else {
2153 printk(KERN_INFO "%sconsole [%s%d] enabled\n", 2261 printk(KERN_INFO "%sconsole [%s%d] enabled\n",
2154 (newcon->flags & CON_BOOT) ? "boot" : "" , 2262 (newcon->flags & CON_BOOT) ? "boot" : "" ,
2155 newcon->name, newcon->index); 2263 newcon->name, newcon->index);
2156 } 2264 }
2157 } 2265 }
2158 EXPORT_SYMBOL(register_console); 2266 EXPORT_SYMBOL(register_console);
2159 2267
2160 int unregister_console(struct console *console) 2268 int unregister_console(struct console *console)
2161 { 2269 {
2162 struct console *a, *b; 2270 struct console *a, *b;
2163 int res = 1; 2271 int res = 1;
2164 2272
2165 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE 2273 #ifdef CONFIG_A11Y_BRAILLE_CONSOLE
2166 if (console->flags & CON_BRL) 2274 if (console->flags & CON_BRL)
2167 return braille_unregister_console(console); 2275 return braille_unregister_console(console);
2168 #endif 2276 #endif
2169 2277
2170 console_lock(); 2278 console_lock();
2171 if (console_drivers == console) { 2279 if (console_drivers == console) {
2172 console_drivers=console->next; 2280 console_drivers=console->next;
2173 res = 0; 2281 res = 0;
2174 } else if (console_drivers) { 2282 } else if (console_drivers) {
2175 for (a=console_drivers->next, b=console_drivers ; 2283 for (a=console_drivers->next, b=console_drivers ;
2176 a; b=a, a=b->next) { 2284 a; b=a, a=b->next) {
2177 if (a == console) { 2285 if (a == console) {
2178 b->next = a->next; 2286 b->next = a->next;
2179 res = 0; 2287 res = 0;
2180 break; 2288 break;
2181 } 2289 }
2182 } 2290 }
2183 } 2291 }
2184 2292
2185 /* 2293 /*
2186 * If this isn't the last console and it has CON_CONSDEV set, we 2294 * If this isn't the last console and it has CON_CONSDEV set, we
2187 * need to set it on the next preferred console. 2295 * need to set it on the next preferred console.
2188 */ 2296 */
2189 if (console_drivers != NULL && console->flags & CON_CONSDEV) 2297 if (console_drivers != NULL && console->flags & CON_CONSDEV)
2190 console_drivers->flags |= CON_CONSDEV; 2298 console_drivers->flags |= CON_CONSDEV;
2191 2299
2192 console_unlock(); 2300 console_unlock();
2193 console_sysfs_notify(); 2301 console_sysfs_notify();
2194 return res; 2302 return res;
2195 } 2303 }
2196 EXPORT_SYMBOL(unregister_console); 2304 EXPORT_SYMBOL(unregister_console);
2197 2305
2198 static int __init printk_late_init(void) 2306 static int __init printk_late_init(void)
2199 { 2307 {
2200 struct console *con; 2308 struct console *con;
2201 2309
2202 for_each_console(con) { 2310 for_each_console(con) {
2203 if (!keep_bootcon && con->flags & CON_BOOT) { 2311 if (!keep_bootcon && con->flags & CON_BOOT) {
2204 printk(KERN_INFO "turn off boot console %s%d\n", 2312 printk(KERN_INFO "turn off boot console %s%d\n",
2205 con->name, con->index); 2313 con->name, con->index);
2206 unregister_console(con); 2314 unregister_console(con);
2207 } 2315 }
2208 } 2316 }
2209 hotcpu_notifier(console_cpu_notify, 0); 2317 hotcpu_notifier(console_cpu_notify, 0);
2210 return 0; 2318 return 0;
2211 } 2319 }
2212 late_initcall(printk_late_init); 2320 late_initcall(printk_late_init);
2213 2321
2214 #if defined CONFIG_PRINTK 2322 #if defined CONFIG_PRINTK
2215 2323
2216 int printk_sched(const char *fmt, ...) 2324 int printk_sched(const char *fmt, ...)
2217 { 2325 {
2218 unsigned long flags; 2326 unsigned long flags;
2219 va_list args; 2327 va_list args;
2220 char *buf; 2328 char *buf;
2221 int r; 2329 int r;
2222 2330
2223 local_irq_save(flags); 2331 local_irq_save(flags);
2224 buf = __get_cpu_var(printk_sched_buf); 2332 buf = __get_cpu_var(printk_sched_buf);
2225 2333
2226 va_start(args, fmt); 2334 va_start(args, fmt);
2227 r = vsnprintf(buf, PRINTK_BUF_SIZE, fmt, args); 2335 r = vsnprintf(buf, PRINTK_BUF_SIZE, fmt, args);
2228 va_end(args); 2336 va_end(args);
2229 2337
2230 __this_cpu_or(printk_pending, PRINTK_PENDING_SCHED); 2338 __this_cpu_or(printk_pending, PRINTK_PENDING_SCHED);
2231 local_irq_restore(flags); 2339 local_irq_restore(flags);
2232 2340
2233 return r; 2341 return r;
2234 } 2342 }
2235 2343
2236 /* 2344 /*
2237 * printk rate limiting, lifted from the networking subsystem. 2345 * printk rate limiting, lifted from the networking subsystem.
2238 * 2346 *
2239 * This enforces a rate limit: not more than 10 kernel messages 2347 * This enforces a rate limit: not more than 10 kernel messages
2240 * every 5s to make a denial-of-service attack impossible. 2348 * every 5s to make a denial-of-service attack impossible.
2241 */ 2349 */
2242 DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10); 2350 DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10);
2243 2351
2244 int __printk_ratelimit(const char *func) 2352 int __printk_ratelimit(const char *func)
2245 { 2353 {
2246 return ___ratelimit(&printk_ratelimit_state, func); 2354 return ___ratelimit(&printk_ratelimit_state, func);
2247 } 2355 }
2248 EXPORT_SYMBOL(__printk_ratelimit); 2356 EXPORT_SYMBOL(__printk_ratelimit);
2249 2357
2250 /** 2358 /**
2251 * printk_timed_ratelimit - caller-controlled printk ratelimiting 2359 * printk_timed_ratelimit - caller-controlled printk ratelimiting
2252 * @caller_jiffies: pointer to caller's state 2360 * @caller_jiffies: pointer to caller's state
2253 * @interval_msecs: minimum interval between prints 2361 * @interval_msecs: minimum interval between prints
2254 * 2362 *
2255 * printk_timed_ratelimit() returns true if more than @interval_msecs 2363 * printk_timed_ratelimit() returns true if more than @interval_msecs
2256 * milliseconds have elapsed since the last time printk_timed_ratelimit() 2364 * milliseconds have elapsed since the last time printk_timed_ratelimit()
2257 * returned true. 2365 * returned true.
2258 */ 2366 */
2259 bool printk_timed_ratelimit(unsigned long *caller_jiffies, 2367 bool printk_timed_ratelimit(unsigned long *caller_jiffies,
2260 unsigned int interval_msecs) 2368 unsigned int interval_msecs)
2261 { 2369 {
2262 if (*caller_jiffies == 0 2370 if (*caller_jiffies == 0
2263 || !time_in_range(jiffies, *caller_jiffies, 2371 || !time_in_range(jiffies, *caller_jiffies,
2264 *caller_jiffies 2372 *caller_jiffies
2265 + msecs_to_jiffies(interval_msecs))) { 2373 + msecs_to_jiffies(interval_msecs))) {
2266 *caller_jiffies = jiffies; 2374 *caller_jiffies = jiffies;
2267 return true; 2375 return true;
2268 } 2376 }
2269 return false; 2377 return false;
2270 } 2378 }
2271 EXPORT_SYMBOL(printk_timed_ratelimit); 2379 EXPORT_SYMBOL(printk_timed_ratelimit);
2272 2380
2273 static DEFINE_SPINLOCK(dump_list_lock); 2381 static DEFINE_SPINLOCK(dump_list_lock);
2274 static LIST_HEAD(dump_list); 2382 static LIST_HEAD(dump_list);
2275 2383
2276 /** 2384 /**
2277 * kmsg_dump_register - register a kernel log dumper. 2385 * kmsg_dump_register - register a kernel log dumper.
2278 * @dumper: pointer to the kmsg_dumper structure 2386 * @dumper: pointer to the kmsg_dumper structure
2279 * 2387 *
2280 * Adds a kernel log dumper to the system. The dump callback in the 2388 * Adds a kernel log dumper to the system. The dump callback in the
2281 * structure will be called when the kernel oopses or panics and must be 2389 * structure will be called when the kernel oopses or panics and must be
2282 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise. 2390 * set. Returns zero on success and %-EINVAL or %-EBUSY otherwise.
2283 */ 2391 */
2284 int kmsg_dump_register(struct kmsg_dumper *dumper) 2392 int kmsg_dump_register(struct kmsg_dumper *dumper)
2285 { 2393 {
2286 unsigned long flags; 2394 unsigned long flags;
2287 int err = -EBUSY; 2395 int err = -EBUSY;
2288 2396
2289 /* The dump callback needs to be set */ 2397 /* The dump callback needs to be set */
2290 if (!dumper->dump) 2398 if (!dumper->dump)
2291 return -EINVAL; 2399 return -EINVAL;
2292 2400
2293 spin_lock_irqsave(&dump_list_lock, flags); 2401 spin_lock_irqsave(&dump_list_lock, flags);
2294 /* Don't allow registering multiple times */ 2402 /* Don't allow registering multiple times */
2295 if (!dumper->registered) { 2403 if (!dumper->registered) {
2296 dumper->registered = 1; 2404 dumper->registered = 1;
2297 list_add_tail_rcu(&dumper->list, &dump_list); 2405 list_add_tail_rcu(&dumper->list, &dump_list);
2298 err = 0; 2406 err = 0;
2299 } 2407 }
2300 spin_unlock_irqrestore(&dump_list_lock, flags); 2408 spin_unlock_irqrestore(&dump_list_lock, flags);
2301 2409
2302 return err; 2410 return err;
2303 } 2411 }
2304 EXPORT_SYMBOL_GPL(kmsg_dump_register); 2412 EXPORT_SYMBOL_GPL(kmsg_dump_register);
2305 2413
2306 /** 2414 /**
2307 * kmsg_dump_unregister - unregister a kmsg dumper. 2415 * kmsg_dump_unregister - unregister a kmsg dumper.
2308 * @dumper: pointer to the kmsg_dumper structure 2416 * @dumper: pointer to the kmsg_dumper structure
2309 * 2417 *
2310 * Removes a dump device from the system. Returns zero on success and 2418 * Removes a dump device from the system. Returns zero on success and
2311 * %-EINVAL otherwise. 2419 * %-EINVAL otherwise.
2312 */ 2420 */
2313 int kmsg_dump_unregister(struct kmsg_dumper *dumper) 2421 int kmsg_dump_unregister(struct kmsg_dumper *dumper)
2314 { 2422 {
2315 unsigned long flags; 2423 unsigned long flags;
2316 int err = -EINVAL; 2424 int err = -EINVAL;
2317 2425
2318 spin_lock_irqsave(&dump_list_lock, flags); 2426 spin_lock_irqsave(&dump_list_lock, flags);
2319 if (dumper->registered) { 2427 if (dumper->registered) {
2320 dumper->registered = 0; 2428 dumper->registered = 0;
2321 list_del_rcu(&dumper->list); 2429 list_del_rcu(&dumper->list);
2322 err = 0; 2430 err = 0;
2323 } 2431 }
2324 spin_unlock_irqrestore(&dump_list_lock, flags); 2432 spin_unlock_irqrestore(&dump_list_lock, flags);
2325 synchronize_rcu(); 2433 synchronize_rcu();
2326 2434
2327 return err; 2435 return err;
2328 } 2436 }
2329 EXPORT_SYMBOL_GPL(kmsg_dump_unregister); 2437 EXPORT_SYMBOL_GPL(kmsg_dump_unregister);
2330 2438
2331 static bool always_kmsg_dump; 2439 static bool always_kmsg_dump;
2332 module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR); 2440 module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR);
2333 2441
2334 /** 2442 /**
2335 * kmsg_dump - dump kernel log to kernel message dumpers. 2443 * kmsg_dump - dump kernel log to kernel message dumpers.
2336 * @reason: the reason (oops, panic etc) for dumping 2444 * @reason: the reason (oops, panic etc) for dumping
2337 * 2445 *
2338 * Call each of the registered dumper's dump() callback, which can 2446 * Call each of the registered dumper's dump() callback, which can
2339 * retrieve the kmsg records with kmsg_dump_get_line() or 2447 * retrieve the kmsg records with kmsg_dump_get_line() or
2340 * kmsg_dump_get_buffer(). 2448 * kmsg_dump_get_buffer().
2341 */ 2449 */
2342 void kmsg_dump(enum kmsg_dump_reason reason) 2450 void kmsg_dump(enum kmsg_dump_reason reason)
2343 { 2451 {
2344 struct kmsg_dumper *dumper; 2452 struct kmsg_dumper *dumper;
2345 unsigned long flags; 2453 unsigned long flags;
2346 2454
2347 if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump) 2455 if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
2348 return; 2456 return;
2349 2457
2350 rcu_read_lock(); 2458 rcu_read_lock();
2351 list_for_each_entry_rcu(dumper, &dump_list, list) { 2459 list_for_each_entry_rcu(dumper, &dump_list, list) {
2352 if (dumper->max_reason && reason > dumper->max_reason) 2460 if (dumper->max_reason && reason > dumper->max_reason)
2353 continue; 2461 continue;
2354 2462
2355 /* initialize iterator with data about the stored records */ 2463 /* initialize iterator with data about the stored records */
2356 dumper->active = true; 2464 dumper->active = true;
2357 2465
2358 raw_spin_lock_irqsave(&logbuf_lock, flags); 2466 raw_spin_lock_irqsave(&logbuf_lock, flags);
2359 dumper->cur_seq = clear_seq; 2467 dumper->cur_seq = clear_seq;
2360 dumper->cur_idx = clear_idx; 2468 dumper->cur_idx = clear_idx;
2361 dumper->next_seq = log_next_seq; 2469 dumper->next_seq = log_next_seq;
2362 dumper->next_idx = log_next_idx; 2470 dumper->next_idx = log_next_idx;
2363 raw_spin_unlock_irqrestore(&logbuf_lock, flags); 2471 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2364 2472
2365 /* invoke dumper which will iterate over records */ 2473 /* invoke dumper which will iterate over records */
2366 dumper->dump(dumper, reason); 2474 dumper->dump(dumper, reason);
2367 2475
2368 /* reset iterator */ 2476 /* reset iterator */
2369 dumper->active = false; 2477 dumper->active = false;
2370 } 2478 }
2371 rcu_read_unlock(); 2479 rcu_read_unlock();
2372 } 2480 }
2373 2481
2374 /** 2482 /**
2375 * kmsg_dump_get_line - retrieve one kmsg log line 2483 * kmsg_dump_get_line - retrieve one kmsg log line
2376 * @dumper: registered kmsg dumper 2484 * @dumper: registered kmsg dumper
2377 * @syslog: include the "<4>" prefixes 2485 * @syslog: include the "<4>" prefixes
2378 * @line: buffer to copy the line to 2486 * @line: buffer to copy the line to
2379 * @size: maximum size of the buffer 2487 * @size: maximum size of the buffer
2380 * @len: length of line placed into buffer 2488 * @len: length of line placed into buffer
2381 * 2489 *
2382 * Start at the beginning of the kmsg buffer, with the oldest kmsg 2490 * Start at the beginning of the kmsg buffer, with the oldest kmsg
2383 * record, and copy one record into the provided buffer. 2491 * record, and copy one record into the provided buffer.
2384 * 2492 *
2385 * Consecutive calls will return the next available record moving 2493 * Consecutive calls will return the next available record moving
2386 * towards the end of the buffer with the youngest messages. 2494 * towards the end of the buffer with the youngest messages.
2387 * 2495 *
2388 * A return value of FALSE indicates that there are no more records to 2496 * A return value of FALSE indicates that there are no more records to
2389 * read. 2497 * read.
2390 */ 2498 */
2391 bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog, 2499 bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog,
2392 char *line, size_t size, size_t *len) 2500 char *line, size_t size, size_t *len)
2393 { 2501 {
2394 unsigned long flags; 2502 unsigned long flags;
2395 struct log *msg; 2503 struct log *msg;
2396 size_t l = 0; 2504 size_t l = 0;
2397 bool ret = false; 2505 bool ret = false;
2398 2506
2399 if (!dumper->active) 2507 if (!dumper->active)
2400 goto out; 2508 goto out;
2401 2509
2402 raw_spin_lock_irqsave(&logbuf_lock, flags); 2510 raw_spin_lock_irqsave(&logbuf_lock, flags);
2403 if (dumper->cur_seq < log_first_seq) { 2511 if (dumper->cur_seq < log_first_seq) {
2404 /* messages are gone, move to first available one */ 2512 /* messages are gone, move to first available one */
2405 dumper->cur_seq = log_first_seq; 2513 dumper->cur_seq = log_first_seq;
2406 dumper->cur_idx = log_first_idx; 2514 dumper->cur_idx = log_first_idx;
2407 } 2515 }
2408 2516
2409 /* last entry */ 2517 /* last entry */
2410 if (dumper->cur_seq >= log_next_seq) { 2518 if (dumper->cur_seq >= log_next_seq) {
2411 raw_spin_unlock_irqrestore(&logbuf_lock, flags); 2519 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2412 goto out; 2520 goto out;
2413 } 2521 }
2414 2522
2415 msg = log_from_idx(dumper->cur_idx); 2523 msg = log_from_idx(dumper->cur_idx);
2416 l = msg_print_text(msg, syslog, 2524 l = msg_print_text(msg, syslog,
2417 line, size); 2525 line, size);
2418 2526
2419 dumper->cur_idx = log_next(dumper->cur_idx); 2527 dumper->cur_idx = log_next(dumper->cur_idx);
2420 dumper->cur_seq++; 2528 dumper->cur_seq++;
2421 ret = true; 2529 ret = true;
2422 raw_spin_unlock_irqrestore(&logbuf_lock, flags); 2530 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2423 out: 2531 out:
2424 if (len) 2532 if (len)
2425 *len = l; 2533 *len = l;
2426 return ret; 2534 return ret;
2427 } 2535 }
2428 EXPORT_SYMBOL_GPL(kmsg_dump_get_line); 2536 EXPORT_SYMBOL_GPL(kmsg_dump_get_line);
2429 2537
2430 /** 2538 /**
2431 * kmsg_dump_get_buffer - copy kmsg log lines 2539 * kmsg_dump_get_buffer - copy kmsg log lines
2432 * @dumper: registered kmsg dumper 2540 * @dumper: registered kmsg dumper
2433 * @syslog: include the "<4>" prefixes 2541 * @syslog: include the "<4>" prefixes
2434 * @line: buffer to copy the line to 2542 * @line: buffer to copy the line to
2435 * @size: maximum size of the buffer 2543 * @size: maximum size of the buffer
2436 * @len: length of line placed into buffer 2544 * @len: length of line placed into buffer
2437 * 2545 *
2438 * Start at the end of the kmsg buffer and fill the provided buffer 2546 * Start at the end of the kmsg buffer and fill the provided buffer
2439 * with as many of the the *youngest* kmsg records that fit into it. 2547 * with as many of the the *youngest* kmsg records that fit into it.
2440 * If the buffer is large enough, all available kmsg records will be 2548 * If the buffer is large enough, all available kmsg records will be
2441 * copied with a single call. 2549 * copied with a single call.
2442 * 2550 *
2443 * Consecutive calls will fill the buffer with the next block of 2551 * Consecutive calls will fill the buffer with the next block of
2444 * available older records, not including the earlier retrieved ones. 2552 * available older records, not including the earlier retrieved ones.
2445 * 2553 *
2446 * A return value of FALSE indicates that there are no more records to 2554 * A return value of FALSE indicates that there are no more records to
2447 * read. 2555 * read.
2448 */ 2556 */
2449 bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog, 2557 bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
2450 char *buf, size_t size, size_t *len) 2558 char *buf, size_t size, size_t *len)
2451 { 2559 {
2452 unsigned long flags; 2560 unsigned long flags;
2453 u64 seq; 2561 u64 seq;
2454 u32 idx; 2562 u32 idx;
2455 u64 next_seq; 2563 u64 next_seq;
2456 u32 next_idx; 2564 u32 next_idx;
2457 size_t l = 0; 2565 size_t l = 0;
2458 bool ret = false; 2566 bool ret = false;
2459 2567
2460 if (!dumper->active) 2568 if (!dumper->active)
2461 goto out; 2569 goto out;
2462 2570
2463 raw_spin_lock_irqsave(&logbuf_lock, flags); 2571 raw_spin_lock_irqsave(&logbuf_lock, flags);
2464 if (dumper->cur_seq < log_first_seq) { 2572 if (dumper->cur_seq < log_first_seq) {
2465 /* messages are gone, move to first available one */ 2573 /* messages are gone, move to first available one */
2466 dumper->cur_seq = log_first_seq; 2574 dumper->cur_seq = log_first_seq;
2467 dumper->cur_idx = log_first_idx; 2575 dumper->cur_idx = log_first_idx;
2468 } 2576 }
2469 2577
2470 /* last entry */ 2578 /* last entry */
2471 if (dumper->cur_seq >= dumper->next_seq) { 2579 if (dumper->cur_seq >= dumper->next_seq) {
2472 raw_spin_unlock_irqrestore(&logbuf_lock, flags); 2580 raw_spin_unlock_irqrestore(&logbuf_lock, flags);
2473 goto out; 2581 goto out;
2474 } 2582 }
2475 2583
2476 /* calculate length of entire buffer */ 2584 /* calculate length of entire buffer */
2477 seq = dumper->cur_seq; 2585 seq = dumper->cur_seq;
2478 idx = dumper->cur_idx; 2586 idx = dumper->cur_idx;
2479 while (seq < dumper->next_seq) { 2587 while (seq < dumper->next_seq) {
2480 struct log *msg = log_from_idx(idx); 2588 struct log *msg = log_from_idx(idx);
2481 2589
2482 l += msg_print_text(msg, true, NULL, 0); 2590 l += msg_print_text(msg, true, NULL, 0);
2483 idx = log_next(idx); 2591 idx = log_next(idx);
2484 seq++; 2592 seq++;
2485 } 2593 }
2486 2594
2487 /* move first record forward until length fits into the buffer */ 2595 /* move first record forward until length fits into the buffer */
2488 seq = dumper->cur_seq; 2596 seq = dumper->cur_seq;
2489 idx = dumper->cur_idx; 2597 idx = dumper->cur_idx;
2490 while (l > size && seq < dumper->next_seq) { 2598 while (l > size && seq < dumper->next_seq) {
2491 struct log *msg = log_from_idx(idx); 2599 struct log *msg = log_from_idx(idx);
2492 2600
2493 l -= msg_print_text(msg, true, NULL, 0); 2601 l -= msg_print_text(msg, true, NULL, 0);
2494 idx = log_next(idx); 2602 idx = log_next(idx);
2495 seq++; 2603 seq++;
2496 } 2604 }
2497 2605
2498 /* last message in next interation */ 2606 /* last message in next interation */
2499 next_seq = seq; 2607 next_seq = seq;
2500 next_idx = idx; 2608 next_idx = idx;
2501 2609
2502 l = 0; 2610 l = 0;
2503 while (seq < dumper->next_seq) { 2611 while (seq < dumper->next_seq) {
2504 struct log *msg = log_from_idx(idx); 2612 struct log *msg = log_from_idx(idx);
2505 2613
2506 l += msg_print_text(msg, syslog, 2614 l += msg_print_text(msg, syslog,
2507 buf + l, size - l); 2615 buf + l, size - l);