diff options
author | Kay Sievers <kay@vrfy.org> | 2012-05-02 20:29:13 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2012-05-07 19:53:02 -0400 |
commit | 7ff9554bb578ba02166071d2d487b7fc7d860d62 (patch) | |
tree | fcd01f3dadfb451af453300663c60054d3e702cf /kernel | |
parent | 89528127fa5f4aca0483203c87c945555d057770 (diff) |
printk: convert byte-buffer to variable-length record buffer
- Record-based stream instead of the traditional byte stream
buffer. All records carry a 64 bit timestamp, the syslog facility
and priority in the record header.
- Records consume almost the same amount, sometimes less memory than
the traditional byte stream buffer (if printk_time is enabled). The record
header is 16 bytes long, plus some padding bytes at the end if needed.
The byte-stream buffer needed 3 chars for the syslog prefix, 15 char for
the timestamp and a newline.
- Buffer management is based on message sequence numbers. When records
need to be discarded, the reading heads move on to the next full
record. Unlike the byte-stream buffer, no old logged lines get
truncated or partly overwritten by new ones. Sequence numbers also
allow consumers of the log stream to get notified if any message in
the stream they are about to read gets discarded during the time
of reading.
- Better buffered IO support for KERN_CONT continuation lines, when printk()
is called multiple times for a single line. The use of KERN_CONT is now
mandatory to use continuation; a few places in the kernel need trivial fixes
here. The buffering could possibly be extended to per-cpu variables to allow
better thread-safety for multiple printk() invocations for a single line.
- Full-featured syslog facility value support. Different facilities
can tag their messages. All userspace-injected messages enforce a
facility value > 0 now, to be able to reliably distinguish them from
the kernel-generated messages. Independent subsystems like a
baseband processor running its own firmware, or a kernel-related
userspace process can use their own unique facility values. Multiple
independent log streams can co-exist that way in the same
buffer. All share the same global sequence number counter to ensure
proper ordering (and interleaving) and to allow the consumers of the
log to reliably correlate the events from different facilities.
Tested-by: William Douglas <william.douglas@intel.com>
Signed-off-by: Kay Sievers <kay@vrfy.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'kernel')
-rw-r--r-- | kernel/printk.c | 1014 |
1 files changed, 590 insertions, 424 deletions
diff --git a/kernel/printk.c b/kernel/printk.c index b663c2c95d39..74357329550f 100644 --- a/kernel/printk.c +++ b/kernel/printk.c | |||
@@ -54,8 +54,6 @@ void asmlinkage __attribute__((weak)) early_printk(const char *fmt, ...) | |||
54 | { | 54 | { |
55 | } | 55 | } |
56 | 56 | ||
57 | #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT) | ||
58 | |||
59 | /* printk's without a loglevel use this.. */ | 57 | /* printk's without a loglevel use this.. */ |
60 | #define DEFAULT_MESSAGE_LOGLEVEL CONFIG_DEFAULT_MESSAGE_LOGLEVEL | 58 | #define DEFAULT_MESSAGE_LOGLEVEL CONFIG_DEFAULT_MESSAGE_LOGLEVEL |
61 | 59 | ||
@@ -99,24 +97,6 @@ EXPORT_SYMBOL_GPL(console_drivers); | |||
99 | static int console_locked, console_suspended; | 97 | static int console_locked, console_suspended; |
100 | 98 | ||
101 | /* | 99 | /* |
102 | * logbuf_lock protects log_buf, log_start, log_end, con_start and logged_chars | ||
103 | * It is also used in interesting ways to provide interlocking in | ||
104 | * console_unlock();. | ||
105 | */ | ||
106 | static DEFINE_RAW_SPINLOCK(logbuf_lock); | ||
107 | |||
108 | #define LOG_BUF_MASK (log_buf_len-1) | ||
109 | #define LOG_BUF(idx) (log_buf[(idx) & LOG_BUF_MASK]) | ||
110 | |||
111 | /* | ||
112 | * The indices into log_buf are not constrained to log_buf_len - they | ||
113 | * must be masked before subscripting | ||
114 | */ | ||
115 | static unsigned log_start; /* Index into log_buf: next char to be read by syslog() */ | ||
116 | static unsigned con_start; /* Index into log_buf: next char to be sent to consoles */ | ||
117 | static unsigned log_end; /* Index into log_buf: most-recently-written-char + 1 */ | ||
118 | |||
119 | /* | ||
120 | * If exclusive_console is non-NULL then only this console is to be printed to. | 100 | * If exclusive_console is non-NULL then only this console is to be printed to. |
121 | */ | 101 | */ |
122 | static struct console *exclusive_console; | 102 | static struct console *exclusive_console; |
@@ -146,12 +126,176 @@ EXPORT_SYMBOL(console_set_on_cmdline); | |||
146 | static int console_may_schedule; | 126 | static int console_may_schedule; |
147 | 127 | ||
148 | #ifdef CONFIG_PRINTK | 128 | #ifdef CONFIG_PRINTK |
129 | /* | ||
130 | * The printk log buffer consists of a chain of concatenated variable | ||
131 | * length records. Every record starts with a record header, containing | ||
132 | * the overall length of the record. | ||
133 | * | ||
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 | ||
136 | * are stored.. | ||
137 | * | ||
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 | ||
140 | * indicates a wrap-around to the beginning of the buffer. | ||
141 | * | ||
142 | * Every record carries the monotonic timestamp in microseconds, as well as | ||
143 | * the standard userspace syslog level and syslog facility. The usual | ||
144 | * kernel messages use LOG_KERN; userspace-injected messages always carry | ||
145 | * a matching syslog facility, by default LOG_USER. The origin of every | ||
146 | * message can be reliably determined that way. | ||
147 | * | ||
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 | ||
150 | * is not terminated. | ||
151 | * | ||
152 | */ | ||
153 | |||
154 | struct log { | ||
155 | u64 ts_nsec; /* timestamp in nanoseconds */ | ||
156 | u16 len; /* length of entire record */ | ||
157 | u16 text_len; /* length of text buffer */ | ||
158 | u16 dict_len; /* length of dictionary buffer */ | ||
159 | u16 level; /* syslog level + facility */ | ||
160 | }; | ||
161 | |||
162 | /* | ||
163 | * The logbuf_lock protects kmsg buffer, indices, counters. It is also | ||
164 | * used in interesting ways to provide interlocking in console_unlock(); | ||
165 | */ | ||
166 | static DEFINE_RAW_SPINLOCK(logbuf_lock); | ||
149 | 167 | ||
168 | /* cpu currently holding logbuf_lock */ | ||
169 | static volatile unsigned int logbuf_cpu = UINT_MAX; | ||
170 | |||
171 | #define LOG_LINE_MAX 1024 | ||
172 | |||
173 | /* record buffer */ | ||
174 | #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT) | ||
150 | static char __log_buf[__LOG_BUF_LEN]; | 175 | static char __log_buf[__LOG_BUF_LEN]; |
151 | static char *log_buf = __log_buf; | 176 | static char *log_buf = __log_buf; |
152 | static int log_buf_len = __LOG_BUF_LEN; | 177 | static u32 log_buf_len = __LOG_BUF_LEN; |
153 | static unsigned logged_chars; /* Number of chars produced since last read+clear operation */ | 178 | |
154 | static int saved_console_loglevel = -1; | 179 | /* index and sequence number of the first record stored in the buffer */ |
180 | static u64 log_first_seq; | ||
181 | static u32 log_first_idx; | ||
182 | |||
183 | /* index and sequence number of the next record to store in the buffer */ | ||
184 | static u64 log_next_seq; | ||
185 | static u32 log_next_idx; | ||
186 | |||
187 | /* the next printk record to read after the last 'clear' command */ | ||
188 | static u64 clear_seq; | ||
189 | static u32 clear_idx; | ||
190 | |||
191 | /* the next printk record to read by syslog(READ) or /proc/kmsg */ | ||
192 | static u64 syslog_seq; | ||
193 | static u32 syslog_idx; | ||
194 | |||
195 | /* human readable text of the record */ | ||
196 | static char *log_text(const struct log *msg) | ||
197 | { | ||
198 | return (char *)msg + sizeof(struct log); | ||
199 | } | ||
200 | |||
201 | /* optional key/value pair dictionary attached to the record */ | ||
202 | static char *log_dict(const struct log *msg) | ||
203 | { | ||
204 | return (char *)msg + sizeof(struct log) + msg->text_len; | ||
205 | } | ||
206 | |||
207 | /* get record by index; idx must point to valid msg */ | ||
208 | static struct log *log_from_idx(u32 idx) | ||
209 | { | ||
210 | struct log *msg = (struct log *)(log_buf + idx); | ||
211 | |||
212 | /* | ||
213 | * A length == 0 record is the end of buffer marker. Wrap around and | ||
214 | * read the message at the start of the buffer. | ||
215 | */ | ||
216 | if (!msg->len) | ||
217 | return (struct log *)log_buf; | ||
218 | return msg; | ||
219 | } | ||
220 | |||
221 | /* get next record; idx must point to valid msg */ | ||
222 | static u32 log_next(u32 idx) | ||
223 | { | ||
224 | struct log *msg = (struct log *)(log_buf + idx); | ||
225 | |||
226 | /* length == 0 indicates the end of the buffer; wrap */ | ||
227 | /* | ||
228 | * A length == 0 record is the end of buffer marker. Wrap around and | ||
229 | * read the message at the start of the buffer as *this* one, and | ||
230 | * return the one after that. | ||
231 | */ | ||
232 | if (!msg->len) { | ||
233 | msg = (struct log *)log_buf; | ||
234 | return msg->len; | ||
235 | } | ||
236 | return idx + msg->len; | ||
237 | } | ||
238 | |||
239 | #if !defined(CONFIG_64BIT) || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) | ||
240 | #define LOG_ALIGN 4 | ||
241 | #else | ||
242 | #define LOG_ALIGN 8 | ||
243 | #endif | ||
244 | |||
245 | /* insert record into the buffer, discard old ones, update heads */ | ||
246 | static void log_store(int facility, int level, | ||
247 | const char *dict, u16 dict_len, | ||
248 | const char *text, u16 text_len) | ||
249 | { | ||
250 | struct log *msg; | ||
251 | u32 size, pad_len; | ||
252 | |||
253 | /* number of '\0' padding bytes to next message */ | ||
254 | size = sizeof(struct log) + text_len + dict_len; | ||
255 | pad_len = (-size) & (LOG_ALIGN - 1); | ||
256 | size += pad_len; | ||
257 | |||
258 | while (log_first_seq < log_next_seq) { | ||
259 | u32 free; | ||
260 | |||
261 | if (log_next_idx > log_first_idx) | ||
262 | free = max(log_buf_len - log_next_idx, log_first_idx); | ||
263 | else | ||
264 | free = log_first_idx - log_next_idx; | ||
265 | |||
266 | if (free > size + sizeof(struct log)) | ||
267 | break; | ||
268 | |||
269 | /* drop old messages until we have enough contiuous space */ | ||
270 | log_first_idx = log_next(log_first_idx); | ||
271 | log_first_seq++; | ||
272 | } | ||
273 | |||
274 | if (log_next_idx + size + sizeof(struct log) >= log_buf_len) { | ||
275 | /* | ||
276 | * This message + an additional empty header does not fit | ||
277 | * at the end of the buffer. Add an empty header with len == 0 | ||
278 | * to signify a wrap around. | ||
279 | */ | ||
280 | memset(log_buf + log_next_idx, 0, sizeof(struct log)); | ||
281 | log_next_idx = 0; | ||
282 | } | ||
283 | |||
284 | /* fill message */ | ||
285 | msg = (struct log *)(log_buf + log_next_idx); | ||
286 | memcpy(log_text(msg), text, text_len); | ||
287 | msg->text_len = text_len; | ||
288 | memcpy(log_dict(msg), dict, dict_len); | ||
289 | msg->dict_len = dict_len; | ||
290 | msg->level = (facility << 3) | (level & 7); | ||
291 | msg->ts_nsec = local_clock(); | ||
292 | memset(log_dict(msg) + dict_len, 0, pad_len); | ||
293 | msg->len = sizeof(struct log) + text_len + dict_len + pad_len; | ||
294 | |||
295 | /* insert message */ | ||
296 | log_next_idx += msg->len; | ||
297 | log_next_seq++; | ||
298 | } | ||
155 | 299 | ||
156 | #ifdef CONFIG_KEXEC | 300 | #ifdef CONFIG_KEXEC |
157 | /* | 301 | /* |
@@ -165,9 +309,9 @@ static int saved_console_loglevel = -1; | |||
165 | void log_buf_kexec_setup(void) | 309 | void log_buf_kexec_setup(void) |
166 | { | 310 | { |
167 | VMCOREINFO_SYMBOL(log_buf); | 311 | VMCOREINFO_SYMBOL(log_buf); |
168 | VMCOREINFO_SYMBOL(log_end); | ||
169 | VMCOREINFO_SYMBOL(log_buf_len); | 312 | VMCOREINFO_SYMBOL(log_buf_len); |
170 | VMCOREINFO_SYMBOL(logged_chars); | 313 | VMCOREINFO_SYMBOL(log_first_idx); |
314 | VMCOREINFO_SYMBOL(log_next_idx); | ||
171 | } | 315 | } |
172 | #endif | 316 | #endif |
173 | 317 | ||
@@ -191,7 +335,6 @@ early_param("log_buf_len", log_buf_len_setup); | |||
191 | void __init setup_log_buf(int early) | 335 | void __init setup_log_buf(int early) |
192 | { | 336 | { |
193 | unsigned long flags; | 337 | unsigned long flags; |
194 | unsigned start, dest_idx, offset; | ||
195 | char *new_log_buf; | 338 | char *new_log_buf; |
196 | int free; | 339 | int free; |
197 | 340 | ||
@@ -219,20 +362,8 @@ void __init setup_log_buf(int early) | |||
219 | log_buf_len = new_log_buf_len; | 362 | log_buf_len = new_log_buf_len; |
220 | log_buf = new_log_buf; | 363 | log_buf = new_log_buf; |
221 | new_log_buf_len = 0; | 364 | new_log_buf_len = 0; |
222 | free = __LOG_BUF_LEN - log_end; | 365 | free = __LOG_BUF_LEN - log_next_idx; |
223 | 366 | memcpy(log_buf, __log_buf, __LOG_BUF_LEN); | |
224 | offset = start = min(con_start, log_start); | ||
225 | dest_idx = 0; | ||
226 | while (start != log_end) { | ||
227 | unsigned log_idx_mask = start & (__LOG_BUF_LEN - 1); | ||
228 | |||
229 | log_buf[dest_idx] = __log_buf[log_idx_mask]; | ||
230 | start++; | ||
231 | dest_idx++; | ||
232 | } | ||
233 | log_start -= offset; | ||
234 | con_start -= offset; | ||
235 | log_end -= offset; | ||
236 | raw_spin_unlock_irqrestore(&logbuf_lock, flags); | 367 | raw_spin_unlock_irqrestore(&logbuf_lock, flags); |
237 | 368 | ||
238 | pr_info("log_buf_len: %d\n", log_buf_len); | 369 | pr_info("log_buf_len: %d\n", log_buf_len); |
@@ -332,11 +463,165 @@ static int check_syslog_permissions(int type, bool from_file) | |||
332 | return 0; | 463 | return 0; |
333 | } | 464 | } |
334 | 465 | ||
466 | #if defined(CONFIG_PRINTK_TIME) | ||
467 | static bool printk_time = 1; | ||
468 | #else | ||
469 | static bool printk_time; | ||
470 | #endif | ||
471 | module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR); | ||
472 | |||
473 | static int syslog_print_line(u32 idx, char *text, size_t size) | ||
474 | { | ||
475 | struct log *msg; | ||
476 | size_t len; | ||
477 | |||
478 | msg = log_from_idx(idx); | ||
479 | if (!text) { | ||
480 | /* calculate length only */ | ||
481 | len = 3; | ||
482 | |||
483 | if (msg->level > 9) | ||
484 | len++; | ||
485 | if (msg->level > 99) | ||
486 | len++; | ||
487 | |||
488 | if (printk_time) | ||
489 | len += 15; | ||
490 | |||
491 | len += msg->text_len; | ||
492 | len++; | ||
493 | return len; | ||
494 | } | ||
495 | |||
496 | len = sprintf(text, "<%u>", msg->level); | ||
497 | |||
498 | if (printk_time) { | ||
499 | unsigned long long t = msg->ts_nsec; | ||
500 | unsigned long rem_ns = do_div(t, 1000000000); | ||
501 | |||
502 | len += sprintf(text + len, "[%5lu.%06lu] ", | ||
503 | (unsigned long) t, rem_ns / 1000); | ||
504 | } | ||
505 | |||
506 | if (len + msg->text_len > size) | ||
507 | return -EINVAL; | ||
508 | memcpy(text + len, log_text(msg), msg->text_len); | ||
509 | len += msg->text_len; | ||
510 | text[len++] = '\n'; | ||
511 | return len; | ||
512 | } | ||
513 | |||
514 | static int syslog_print(char __user *buf, int size) | ||
515 | { | ||
516 | char *text; | ||
517 | int len; | ||
518 | |||
519 | text = kmalloc(LOG_LINE_MAX, GFP_KERNEL); | ||
520 | if (!text) | ||
521 | return -ENOMEM; | ||
522 | |||
523 | raw_spin_lock_irq(&logbuf_lock); | ||
524 | if (syslog_seq < log_first_seq) { | ||
525 | /* messages are gone, move to first one */ | ||
526 | syslog_seq = log_first_seq; | ||
527 | syslog_idx = log_first_idx; | ||
528 | } | ||
529 | len = syslog_print_line(syslog_idx, text, LOG_LINE_MAX); | ||
530 | syslog_idx = log_next(syslog_idx); | ||
531 | syslog_seq++; | ||
532 | raw_spin_unlock_irq(&logbuf_lock); | ||
533 | |||
534 | if (len > 0 && copy_to_user(buf, text, len)) | ||
535 | len = -EFAULT; | ||
536 | |||
537 | kfree(text); | ||
538 | return len; | ||
539 | } | ||
540 | |||
541 | static int syslog_print_all(char __user *buf, int size, bool clear) | ||
542 | { | ||
543 | char *text; | ||
544 | int len = 0; | ||
545 | |||
546 | text = kmalloc(LOG_LINE_MAX, GFP_KERNEL); | ||
547 | if (!text) | ||
548 | return -ENOMEM; | ||
549 | |||
550 | raw_spin_lock_irq(&logbuf_lock); | ||
551 | if (buf) { | ||
552 | u64 next_seq; | ||
553 | u64 seq; | ||
554 | u32 idx; | ||
555 | |||
556 | if (clear_seq < log_first_seq) { | ||
557 | /* messages are gone, move to first available one */ | ||
558 | clear_seq = log_first_seq; | ||
559 | clear_idx = log_first_idx; | ||
560 | } | ||
561 | |||
562 | /* | ||
563 | * Find first record that fits, including all following records, | ||
564 | * into the user-provided buffer for this dump. | ||
565 | */ | ||
566 | seq = clear_seq; | ||
567 | idx = clear_idx; | ||
568 | while (seq < log_next_seq) { | ||
569 | len += syslog_print_line(idx, NULL, 0); | ||
570 | idx = log_next(idx); | ||
571 | seq++; | ||
572 | } | ||
573 | seq = clear_seq; | ||
574 | idx = clear_idx; | ||
575 | while (len > size && seq < log_next_seq) { | ||
576 | len -= syslog_print_line(idx, NULL, 0); | ||
577 | idx = log_next(idx); | ||
578 | seq++; | ||
579 | } | ||
580 | |||
581 | /* last message in this dump */ | ||
582 | next_seq = log_next_seq; | ||
583 | |||
584 | len = 0; | ||
585 | while (len >= 0 && seq < next_seq) { | ||
586 | int textlen; | ||
587 | |||
588 | textlen = syslog_print_line(idx, text, LOG_LINE_MAX); | ||
589 | if (textlen < 0) { | ||
590 | len = textlen; | ||
591 | break; | ||
592 | } | ||
593 | idx = log_next(idx); | ||
594 | seq++; | ||
595 | |||
596 | raw_spin_unlock_irq(&logbuf_lock); | ||
597 | if (copy_to_user(buf + len, text, textlen)) | ||
598 | len = -EFAULT; | ||
599 | else | ||
600 | len += textlen; | ||
601 | raw_spin_lock_irq(&logbuf_lock); | ||
602 | |||
603 | if (seq < log_first_seq) { | ||
604 | /* messages are gone, move to next one */ | ||
605 | seq = log_first_seq; | ||
606 | idx = log_first_idx; | ||
607 | } | ||
608 | } | ||
609 | } | ||
610 | |||
611 | if (clear) { | ||
612 | clear_seq = log_next_seq; | ||
613 | clear_idx = log_next_idx; | ||
614 | } | ||
615 | raw_spin_unlock_irq(&logbuf_lock); | ||
616 | |||
617 | kfree(text); | ||
618 | return len; | ||
619 | } | ||
620 | |||
335 | int do_syslog(int type, char __user *buf, int len, bool from_file) | 621 | int do_syslog(int type, char __user *buf, int len, bool from_file) |
336 | { | 622 | { |
337 | unsigned i, j, limit, count; | 623 | bool clear = false; |
338 | int do_clear = 0; | 624 | static int saved_console_loglevel = -1; |
339 | char c; | ||
340 | int error; | 625 | int error; |
341 | 626 | ||
342 | error = check_syslog_permissions(type, from_file); | 627 | error = check_syslog_permissions(type, from_file); |
@@ -364,28 +649,14 @@ int do_syslog(int type, char __user *buf, int len, bool from_file) | |||
364 | goto out; | 649 | goto out; |
365 | } | 650 | } |
366 | error = wait_event_interruptible(log_wait, | 651 | error = wait_event_interruptible(log_wait, |
367 | (log_start - log_end)); | 652 | syslog_seq != log_next_seq); |
368 | if (error) | 653 | if (error) |
369 | goto out; | 654 | goto out; |
370 | i = 0; | 655 | error = syslog_print(buf, len); |
371 | raw_spin_lock_irq(&logbuf_lock); | ||
372 | while (!error && (log_start != log_end) && i < len) { | ||
373 | c = LOG_BUF(log_start); | ||
374 | log_start++; | ||
375 | raw_spin_unlock_irq(&logbuf_lock); | ||
376 | error = __put_user(c,buf); | ||
377 | buf++; | ||
378 | i++; | ||
379 | cond_resched(); | ||
380 | raw_spin_lock_irq(&logbuf_lock); | ||
381 | } | ||
382 | raw_spin_unlock_irq(&logbuf_lock); | ||
383 | if (!error) | ||
384 | error = i; | ||
385 | break; | 656 | break; |
386 | /* Read/clear last kernel messages */ | 657 | /* Read/clear last kernel messages */ |
387 | case SYSLOG_ACTION_READ_CLEAR: | 658 | case SYSLOG_ACTION_READ_CLEAR: |
388 | do_clear = 1; | 659 | clear = true; |
389 | /* FALL THRU */ | 660 | /* FALL THRU */ |
390 | /* Read last kernel messages */ | 661 | /* Read last kernel messages */ |
391 | case SYSLOG_ACTION_READ_ALL: | 662 | case SYSLOG_ACTION_READ_ALL: |
@@ -399,52 +670,11 @@ int do_syslog(int type, char __user *buf, int len, bool from_file) | |||
399 | error = -EFAULT; | 670 | error = -EFAULT; |
400 | goto out; | 671 | goto out; |
401 | } | 672 | } |
402 | count = len; | 673 | error = syslog_print_all(buf, len, clear); |
403 | if (count > log_buf_len) | ||
404 | count = log_buf_len; | ||
405 | raw_spin_lock_irq(&logbuf_lock); | ||
406 | if (count > logged_chars) | ||
407 | count = logged_chars; | ||
408 | if (do_clear) | ||
409 | logged_chars = 0; | ||
410 | limit = log_end; | ||
411 | /* | ||
412 | * __put_user() could sleep, and while we sleep | ||
413 | * printk() could overwrite the messages | ||
414 | * we try to copy to user space. Therefore | ||
415 | * the messages are copied in reverse. <manfreds> | ||
416 | */ | ||
417 | for (i = 0; i < count && !error; i++) { | ||
418 | j = limit-1-i; | ||
419 | if (j + log_buf_len < log_end) | ||
420 | break; | ||
421 | c = LOG_BUF(j); | ||
422 | raw_spin_unlock_irq(&logbuf_lock); | ||
423 | error = __put_user(c,&buf[count-1-i]); | ||
424 | cond_resched(); | ||
425 | raw_spin_lock_irq(&logbuf_lock); | ||
426 | } | ||
427 | raw_spin_unlock_irq(&logbuf_lock); | ||
428 | if (error) | ||
429 | break; | ||
430 | error = i; | ||
431 | if (i != count) { | ||
432 | int offset = count-error; | ||
433 | /* buffer overflow during copy, correct user buffer. */ | ||
434 | for (i = 0; i < error; i++) { | ||
435 | if (__get_user(c,&buf[i+offset]) || | ||
436 | __put_user(c,&buf[i])) { | ||
437 | error = -EFAULT; | ||
438 | break; | ||
439 | } | ||
440 | cond_resched(); | ||
441 | } | ||
442 | } | ||
443 | break; | 674 | break; |
444 | /* Clear ring buffer */ | 675 | /* Clear ring buffer */ |
445 | case SYSLOG_ACTION_CLEAR: | 676 | case SYSLOG_ACTION_CLEAR: |
446 | logged_chars = 0; | 677 | syslog_print_all(NULL, 0, true); |
447 | break; | ||
448 | /* Disable logging to console */ | 678 | /* Disable logging to console */ |
449 | case SYSLOG_ACTION_CONSOLE_OFF: | 679 | case SYSLOG_ACTION_CONSOLE_OFF: |
450 | if (saved_console_loglevel == -1) | 680 | if (saved_console_loglevel == -1) |
@@ -472,7 +702,33 @@ int do_syslog(int type, char __user *buf, int len, bool from_file) | |||
472 | break; | 702 | break; |
473 | /* Number of chars in the log buffer */ | 703 | /* Number of chars in the log buffer */ |
474 | case SYSLOG_ACTION_SIZE_UNREAD: | 704 | case SYSLOG_ACTION_SIZE_UNREAD: |
475 | error = log_end - log_start; | 705 | raw_spin_lock_irq(&logbuf_lock); |
706 | if (syslog_seq < log_first_seq) { | ||
707 | /* messages are gone, move to first one */ | ||
708 | syslog_seq = log_first_seq; | ||
709 | syslog_idx = log_first_idx; | ||
710 | } | ||
711 | if (from_file) { | ||
712 | /* | ||
713 | * Short-cut for poll(/"proc/kmsg") which simply checks | ||
714 | * for pending data, not the size; return the count of | ||
715 | * records, not the length. | ||
716 | */ | ||
717 | error = log_next_idx - syslog_idx; | ||
718 | } else { | ||
719 | u64 seq; | ||
720 | u32 idx; | ||
721 | |||
722 | error = 0; | ||
723 | seq = syslog_seq; | ||
724 | idx = syslog_idx; | ||
725 | while (seq < log_next_seq) { | ||
726 | error += syslog_print_line(idx, NULL, 0); | ||
727 | idx = log_next(idx); | ||
728 | seq++; | ||
729 | } | ||
730 | } | ||
731 | raw_spin_unlock_irq(&logbuf_lock); | ||
476 | break; | 732 | break; |
477 | /* Size of the log buffer */ | 733 | /* Size of the log buffer */ |
478 | case SYSLOG_ACTION_SIZE_BUFFER: | 734 | case SYSLOG_ACTION_SIZE_BUFFER: |
@@ -501,29 +757,11 @@ void kdb_syslog_data(char *syslog_data[4]) | |||
501 | { | 757 | { |
502 | syslog_data[0] = log_buf; | 758 | syslog_data[0] = log_buf; |
503 | syslog_data[1] = log_buf + log_buf_len; | 759 | syslog_data[1] = log_buf + log_buf_len; |
504 | syslog_data[2] = log_buf + log_end - | 760 | syslog_data[2] = log_buf + log_first_idx; |
505 | (logged_chars < log_buf_len ? logged_chars : log_buf_len); | 761 | syslog_data[3] = log_buf + log_next_idx; |
506 | syslog_data[3] = log_buf + log_end; | ||
507 | } | 762 | } |
508 | #endif /* CONFIG_KGDB_KDB */ | 763 | #endif /* CONFIG_KGDB_KDB */ |
509 | 764 | ||
510 | /* | ||
511 | * Call the console drivers on a range of log_buf | ||
512 | */ | ||
513 | static void __call_console_drivers(unsigned start, unsigned end) | ||
514 | { | ||
515 | struct console *con; | ||
516 | |||
517 | for_each_console(con) { | ||
518 | if (exclusive_console && con != exclusive_console) | ||
519 | continue; | ||
520 | if ((con->flags & CON_ENABLED) && con->write && | ||
521 | (cpu_online(smp_processor_id()) || | ||
522 | (con->flags & CON_ANYTIME))) | ||
523 | con->write(con, &LOG_BUF(start), end - start); | ||
524 | } | ||
525 | } | ||
526 | |||
527 | static bool __read_mostly ignore_loglevel; | 765 | static bool __read_mostly ignore_loglevel; |
528 | 766 | ||
529 | static int __init ignore_loglevel_setup(char *str) | 767 | static int __init ignore_loglevel_setup(char *str) |
@@ -540,142 +778,33 @@ MODULE_PARM_DESC(ignore_loglevel, "ignore loglevel setting, to" | |||
540 | "print all kernel messages to the console."); | 778 | "print all kernel messages to the console."); |
541 | 779 | ||
542 | /* | 780 | /* |
543 | * Write out chars from start to end - 1 inclusive | ||
544 | */ | ||
545 | static void _call_console_drivers(unsigned start, | ||
546 | unsigned end, int msg_log_level) | ||
547 | { | ||
548 | trace_console(&LOG_BUF(0), start, end, log_buf_len); | ||
549 | |||
550 | if ((msg_log_level < console_loglevel || ignore_loglevel) && | ||
551 | console_drivers && start != end) { | ||
552 | if ((start & LOG_BUF_MASK) > (end & LOG_BUF_MASK)) { | ||
553 | /* wrapped write */ | ||
554 | __call_console_drivers(start & LOG_BUF_MASK, | ||
555 | log_buf_len); | ||
556 | __call_console_drivers(0, end & LOG_BUF_MASK); | ||
557 | } else { | ||
558 | __call_console_drivers(start, end); | ||
559 | } | ||
560 | } | ||
561 | } | ||
562 | |||
563 | /* | ||
564 | * Parse the syslog header <[0-9]*>. The decimal value represents 32bit, the | ||
565 | * lower 3 bit are the log level, the rest are the log facility. In case | ||
566 | * userspace passes usual userspace syslog messages to /dev/kmsg or | ||
567 | * /dev/ttyprintk, the log prefix might contain the facility. Printk needs | ||
568 | * to extract the correct log level for in-kernel processing, and not mangle | ||
569 | * the original value. | ||
570 | * | ||
571 | * If a prefix is found, the length of the prefix is returned. If 'level' is | ||
572 | * passed, it will be filled in with the log level without a possible facility | ||
573 | * value. If 'special' is passed, the special printk prefix chars are accepted | ||
574 | * and returned. If no valid header is found, 0 is returned and the passed | ||
575 | * variables are not touched. | ||
576 | */ | ||
577 | static size_t log_prefix(const char *p, unsigned int *level, char *special) | ||
578 | { | ||
579 | unsigned int lev = 0; | ||
580 | char sp = '\0'; | ||
581 | size_t len; | ||
582 | |||
583 | if (p[0] != '<' || !p[1]) | ||
584 | return 0; | ||
585 | if (p[2] == '>') { | ||
586 | /* usual single digit level number or special char */ | ||
587 | switch (p[1]) { | ||
588 | case '0' ... '7': | ||
589 | lev = p[1] - '0'; | ||
590 | break; | ||
591 | case 'c': /* KERN_CONT */ | ||
592 | case 'd': /* KERN_DEFAULT */ | ||
593 | sp = p[1]; | ||
594 | break; | ||
595 | default: | ||
596 | return 0; | ||
597 | } | ||
598 | len = 3; | ||
599 | } else { | ||
600 | /* multi digit including the level and facility number */ | ||
601 | char *endp = NULL; | ||
602 | |||
603 | lev = (simple_strtoul(&p[1], &endp, 10) & 7); | ||
604 | if (endp == NULL || endp[0] != '>') | ||
605 | return 0; | ||
606 | len = (endp + 1) - p; | ||
607 | } | ||
608 | |||
609 | /* do not accept special char if not asked for */ | ||
610 | if (sp && !special) | ||
611 | return 0; | ||
612 | |||
613 | if (special) { | ||
614 | *special = sp; | ||
615 | /* return special char, do not touch level */ | ||
616 | if (sp) | ||
617 | return len; | ||
618 | } | ||
619 | |||
620 | if (level) | ||
621 | *level = lev; | ||
622 | return len; | ||
623 | } | ||
624 | |||
625 | /* | ||
626 | * Call the console drivers, asking them to write out | 781 | * Call the console drivers, asking them to write out |
627 | * log_buf[start] to log_buf[end - 1]. | 782 | * log_buf[start] to log_buf[end - 1]. |
628 | * The console_lock must be held. | 783 | * The console_lock must be held. |
629 | */ | 784 | */ |
630 | static void call_console_drivers(unsigned start, unsigned end) | 785 | static void call_console_drivers(int level, const char *text, size_t len) |
631 | { | 786 | { |
632 | unsigned cur_index, start_print; | 787 | struct console *con; |
633 | static int msg_level = -1; | ||
634 | |||
635 | BUG_ON(((int)(start - end)) > 0); | ||
636 | |||
637 | cur_index = start; | ||
638 | start_print = start; | ||
639 | while (cur_index != end) { | ||
640 | if (msg_level < 0 && ((end - cur_index) > 2)) { | ||
641 | /* strip log prefix */ | ||
642 | cur_index += log_prefix(&LOG_BUF(cur_index), &msg_level, NULL); | ||
643 | start_print = cur_index; | ||
644 | } | ||
645 | while (cur_index != end) { | ||
646 | char c = LOG_BUF(cur_index); | ||
647 | |||
648 | cur_index++; | ||
649 | if (c == '\n') { | ||
650 | if (msg_level < 0) { | ||
651 | /* | ||
652 | * printk() has already given us loglevel tags in | ||
653 | * the buffer. This code is here in case the | ||
654 | * log buffer has wrapped right round and scribbled | ||
655 | * on those tags | ||
656 | */ | ||
657 | msg_level = default_message_loglevel; | ||
658 | } | ||
659 | _call_console_drivers(start_print, cur_index, msg_level); | ||
660 | msg_level = -1; | ||
661 | start_print = cur_index; | ||
662 | break; | ||
663 | } | ||
664 | } | ||
665 | } | ||
666 | _call_console_drivers(start_print, end, msg_level); | ||
667 | } | ||
668 | 788 | ||
669 | static void emit_log_char(char c) | 789 | trace_console(text, 0, len, len); |
670 | { | 790 | |
671 | LOG_BUF(log_end) = c; | 791 | if (level >= console_loglevel && !ignore_loglevel) |
672 | log_end++; | 792 | return; |
673 | if (log_end - log_start > log_buf_len) | 793 | if (!console_drivers) |
674 | log_start = log_end - log_buf_len; | 794 | return; |
675 | if (log_end - con_start > log_buf_len) | 795 | |
676 | con_start = log_end - log_buf_len; | 796 | for_each_console(con) { |
677 | if (logged_chars < log_buf_len) | 797 | if (exclusive_console && con != exclusive_console) |
678 | logged_chars++; | 798 | continue; |
799 | if (!(con->flags & CON_ENABLED)) | ||
800 | continue; | ||
801 | if (!con->write) | ||
802 | continue; | ||
803 | if (!cpu_online(smp_processor_id()) && | ||
804 | !(con->flags & CON_ANYTIME)) | ||
805 | continue; | ||
806 | con->write(con, text, len); | ||
807 | } | ||
679 | } | 808 | } |
680 | 809 | ||
681 | /* | 810 | /* |
@@ -700,16 +829,6 @@ static void zap_locks(void) | |||
700 | sema_init(&console_sem, 1); | 829 | sema_init(&console_sem, 1); |
701 | } | 830 | } |
702 | 831 | ||
703 | #if defined(CONFIG_PRINTK_TIME) | ||
704 | static bool printk_time = 1; | ||
705 | #else | ||
706 | static bool printk_time = 0; | ||
707 | #endif | ||
708 | module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR); | ||
709 | |||
710 | static bool always_kmsg_dump; | ||
711 | module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR); | ||
712 | |||
713 | /* Check if we have any console registered that can be called early in boot. */ | 832 | /* Check if we have any console registered that can be called early in boot. */ |
714 | static int have_callable_console(void) | 833 | static int have_callable_console(void) |
715 | { | 834 | { |
@@ -722,51 +841,6 @@ static int have_callable_console(void) | |||
722 | return 0; | 841 | return 0; |
723 | } | 842 | } |
724 | 843 | ||
725 | /** | ||
726 | * printk - print a kernel message | ||
727 | * @fmt: format string | ||
728 | * | ||
729 | * This is printk(). It can be called from any context. We want it to work. | ||
730 | * | ||
731 | * We try to grab the console_lock. If we succeed, it's easy - we log the output and | ||
732 | * call the console drivers. If we fail to get the semaphore we place the output | ||
733 | * into the log buffer and return. The current holder of the console_sem will | ||
734 | * notice the new output in console_unlock(); and will send it to the | ||
735 | * consoles before releasing the lock. | ||
736 | * | ||
737 | * One effect of this deferred printing is that code which calls printk() and | ||
738 | * then changes console_loglevel may break. This is because console_loglevel | ||
739 | * is inspected when the actual printing occurs. | ||
740 | * | ||
741 | * See also: | ||
742 | * printf(3) | ||
743 | * | ||
744 | * See the vsnprintf() documentation for format string extensions over C99. | ||
745 | */ | ||
746 | |||
747 | asmlinkage int printk(const char *fmt, ...) | ||
748 | { | ||
749 | va_list args; | ||
750 | int r; | ||
751 | |||
752 | #ifdef CONFIG_KGDB_KDB | ||
753 | if (unlikely(kdb_trap_printk)) { | ||
754 | va_start(args, fmt); | ||
755 | r = vkdb_printf(fmt, args); | ||
756 | va_end(args); | ||
757 | return r; | ||
758 | } | ||
759 | #endif | ||
760 | va_start(args, fmt); | ||
761 | r = vprintk(fmt, args); | ||
762 | va_end(args); | ||
763 | |||
764 | return r; | ||
765 | } | ||
766 | |||
767 | /* cpu currently holding logbuf_lock */ | ||
768 | static volatile unsigned int printk_cpu = UINT_MAX; | ||
769 | |||
770 | /* | 844 | /* |
771 | * Can we actually use the console at this time on this cpu? | 845 | * Can we actually use the console at this time on this cpu? |
772 | * | 846 | * |
@@ -810,17 +884,12 @@ static int console_trylock_for_printk(unsigned int cpu) | |||
810 | retval = 0; | 884 | retval = 0; |
811 | } | 885 | } |
812 | } | 886 | } |
813 | printk_cpu = UINT_MAX; | 887 | logbuf_cpu = UINT_MAX; |
814 | if (wake) | 888 | if (wake) |
815 | up(&console_sem); | 889 | up(&console_sem); |
816 | raw_spin_unlock(&logbuf_lock); | 890 | raw_spin_unlock(&logbuf_lock); |
817 | return retval; | 891 | return retval; |
818 | } | 892 | } |
819 | static const char recursion_bug_msg [] = | ||
820 | KERN_CRIT "BUG: recent printk recursion!\n"; | ||
821 | static int recursion_bug; | ||
822 | static int new_text_line = 1; | ||
823 | static char printk_buf[1024]; | ||
824 | 893 | ||
825 | int printk_delay_msec __read_mostly; | 894 | int printk_delay_msec __read_mostly; |
826 | 895 | ||
@@ -836,15 +905,22 @@ static inline void printk_delay(void) | |||
836 | } | 905 | } |
837 | } | 906 | } |
838 | 907 | ||
839 | asmlinkage int vprintk(const char *fmt, va_list args) | 908 | asmlinkage int vprintk_emit(int facility, int level, |
909 | const char *dict, size_t dictlen, | ||
910 | const char *fmt, va_list args) | ||
840 | { | 911 | { |
841 | int printed_len = 0; | 912 | static int recursion_bug; |
842 | int current_log_level = default_message_loglevel; | 913 | static char buf[LOG_LINE_MAX]; |
914 | static size_t buflen; | ||
915 | static int buflevel; | ||
916 | static char textbuf[LOG_LINE_MAX]; | ||
917 | char *text = textbuf; | ||
918 | size_t textlen; | ||
843 | unsigned long flags; | 919 | unsigned long flags; |
844 | int this_cpu; | 920 | int this_cpu; |
845 | char *p; | 921 | bool newline = false; |
846 | size_t plen; | 922 | bool cont = false; |
847 | char special; | 923 | int printed_len = 0; |
848 | 924 | ||
849 | boot_delay_msec(); | 925 | boot_delay_msec(); |
850 | printk_delay(); | 926 | printk_delay(); |
@@ -856,7 +932,7 @@ asmlinkage int vprintk(const char *fmt, va_list args) | |||
856 | /* | 932 | /* |
857 | * Ouch, printk recursed into itself! | 933 | * Ouch, printk recursed into itself! |
858 | */ | 934 | */ |
859 | if (unlikely(printk_cpu == this_cpu)) { | 935 | if (unlikely(logbuf_cpu == this_cpu)) { |
860 | /* | 936 | /* |
861 | * If a crash is occurring during printk() on this CPU, | 937 | * If a crash is occurring during printk() on this CPU, |
862 | * then try to get the crash message out but make sure | 938 | * then try to get the crash message out but make sure |
@@ -873,97 +949,92 @@ asmlinkage int vprintk(const char *fmt, va_list args) | |||
873 | 949 | ||
874 | lockdep_off(); | 950 | lockdep_off(); |
875 | raw_spin_lock(&logbuf_lock); | 951 | raw_spin_lock(&logbuf_lock); |
876 | printk_cpu = this_cpu; | 952 | logbuf_cpu = this_cpu; |
877 | 953 | ||
878 | if (recursion_bug) { | 954 | if (recursion_bug) { |
955 | static const char recursion_msg[] = | ||
956 | "BUG: recent printk recursion!"; | ||
957 | |||
879 | recursion_bug = 0; | 958 | recursion_bug = 0; |
880 | strcpy(printk_buf, recursion_bug_msg); | 959 | printed_len += strlen(recursion_msg); |
881 | printed_len = strlen(recursion_bug_msg); | 960 | /* emit KERN_CRIT message */ |
961 | log_store(0, 2, NULL, 0, recursion_msg, printed_len); | ||
882 | } | 962 | } |
883 | /* Emit the output into the temporary buffer */ | ||
884 | printed_len += vscnprintf(printk_buf + printed_len, | ||
885 | sizeof(printk_buf) - printed_len, fmt, args); | ||
886 | 963 | ||
887 | p = printk_buf; | 964 | /* |
965 | * The printf needs to come first; we need the syslog | ||
966 | * prefix which might be passed-in as a parameter. | ||
967 | */ | ||
968 | textlen = vscnprintf(text, sizeof(textbuf), fmt, args); | ||
888 | 969 | ||
889 | /* Read log level and handle special printk prefix */ | 970 | /* mark and strip a trailing newline */ |
890 | plen = log_prefix(p, ¤t_log_level, &special); | 971 | if (textlen && text[textlen-1] == '\n') { |
891 | if (plen) { | 972 | textlen--; |
892 | p += plen; | 973 | newline = true; |
974 | } | ||
893 | 975 | ||
894 | switch (special) { | 976 | /* strip syslog prefix and extract log level or flags */ |
895 | case 'c': /* Strip <c> KERN_CONT, continue line */ | 977 | if (text[0] == '<' && text[1] && text[2] == '>') { |
896 | plen = 0; | 978 | switch (text[1]) { |
979 | case '0' ... '7': | ||
980 | if (level == -1) | ||
981 | level = text[1] - '0'; | ||
982 | text += 3; | ||
983 | textlen -= 3; | ||
984 | break; | ||
985 | case 'c': /* KERN_CONT */ | ||
986 | cont = true; | ||
987 | case 'd': /* KERN_DEFAULT */ | ||
988 | text += 3; | ||
989 | textlen -= 3; | ||
897 | break; | 990 | break; |
898 | case 'd': /* Strip <d> KERN_DEFAULT, start new line */ | ||
899 | plen = 0; | ||
900 | default: | ||
901 | if (!new_text_line) { | ||
902 | emit_log_char('\n'); | ||
903 | new_text_line = 1; | ||
904 | } | ||
905 | } | 991 | } |
906 | } | 992 | } |
907 | 993 | ||
908 | /* | 994 | if (buflen && (!cont || dict)) { |
909 | * Copy the output into log_buf. If the caller didn't provide | 995 | /* no continuation; flush existing buffer */ |
910 | * the appropriate log prefix, we insert them here | 996 | log_store(facility, buflevel, NULL, 0, buf, buflen); |
911 | */ | 997 | printed_len += buflen; |
912 | for (; *p; p++) { | 998 | buflen = 0; |
913 | if (new_text_line) { | 999 | } |
914 | new_text_line = 0; | ||
915 | |||
916 | if (plen) { | ||
917 | /* Copy original log prefix */ | ||
918 | int i; | ||
919 | |||
920 | for (i = 0; i < plen; i++) | ||
921 | emit_log_char(printk_buf[i]); | ||
922 | printed_len += plen; | ||
923 | } else { | ||
924 | /* Add log prefix */ | ||
925 | emit_log_char('<'); | ||
926 | emit_log_char(current_log_level + '0'); | ||
927 | emit_log_char('>'); | ||
928 | printed_len += 3; | ||
929 | } | ||
930 | 1000 | ||
931 | if (printk_time) { | 1001 | if (buflen == 0) { |
932 | /* Add the current time stamp */ | 1002 | /* remember level for first message in the buffer */ |
933 | char tbuf[50], *tp; | 1003 | if (level == -1) |
934 | unsigned tlen; | 1004 | buflevel = default_message_loglevel; |
935 | unsigned long long t; | 1005 | else |
936 | unsigned long nanosec_rem; | 1006 | buflevel = level; |
937 | 1007 | } | |
938 | t = cpu_clock(printk_cpu); | ||
939 | nanosec_rem = do_div(t, 1000000000); | ||
940 | tlen = sprintf(tbuf, "[%5lu.%06lu] ", | ||
941 | (unsigned long) t, | ||
942 | nanosec_rem / 1000); | ||
943 | |||
944 | for (tp = tbuf; tp < tbuf + tlen; tp++) | ||
945 | emit_log_char(*tp); | ||
946 | printed_len += tlen; | ||
947 | } | ||
948 | 1008 | ||
949 | if (!*p) | 1009 | if (buflen || !newline) { |
950 | break; | 1010 | /* append to existing buffer, or buffer until next message */ |
951 | } | 1011 | if (buflen + textlen > sizeof(buf)) |
1012 | textlen = sizeof(buf) - buflen; | ||
1013 | memcpy(buf + buflen, text, textlen); | ||
1014 | buflen += textlen; | ||
1015 | } | ||
952 | 1016 | ||
953 | emit_log_char(*p); | 1017 | if (newline) { |
954 | if (*p == '\n') | 1018 | /* end of line; flush buffer */ |
955 | new_text_line = 1; | 1019 | if (buflen) { |
1020 | log_store(facility, buflevel, | ||
1021 | dict, dictlen, buf, buflen); | ||
1022 | printed_len += buflen; | ||
1023 | buflen = 0; | ||
1024 | } else { | ||
1025 | log_store(facility, buflevel, | ||
1026 | dict, dictlen, text, textlen); | ||
1027 | printed_len += textlen; | ||
1028 | } | ||
956 | } | 1029 | } |
957 | 1030 | ||
958 | /* | 1031 | /* |
959 | * Try to acquire and then immediately release the | 1032 | * Try to acquire and then immediately release the console semaphore. |
960 | * console semaphore. The release will do all the | 1033 | * The release will print out buffers and wake up /dev/kmsg and syslog() |
961 | * actual magic (print out buffers, wake up klogd, | 1034 | * users. |
962 | * etc). | ||
963 | * | 1035 | * |
964 | * The console_trylock_for_printk() function | 1036 | * The console_trylock_for_printk() function will release 'logbuf_lock' |
965 | * will release 'logbuf_lock' regardless of whether it | 1037 | * regardless of whether it actually gets the console semaphore or not. |
966 | * actually gets the semaphore or not. | ||
967 | */ | 1038 | */ |
968 | if (console_trylock_for_printk(this_cpu)) | 1039 | if (console_trylock_for_printk(this_cpu)) |
969 | console_unlock(); | 1040 | console_unlock(); |
@@ -974,12 +1045,73 @@ out_restore_irqs: | |||
974 | 1045 | ||
975 | return printed_len; | 1046 | return printed_len; |
976 | } | 1047 | } |
977 | EXPORT_SYMBOL(printk); | 1048 | EXPORT_SYMBOL(vprintk_emit); |
1049 | |||
1050 | asmlinkage int vprintk(const char *fmt, va_list args) | ||
1051 | { | ||
1052 | return vprintk_emit(0, -1, NULL, 0, fmt, args); | ||
1053 | } | ||
978 | EXPORT_SYMBOL(vprintk); | 1054 | EXPORT_SYMBOL(vprintk); |
979 | 1055 | ||
1056 | asmlinkage int printk_emit(int facility, int level, | ||
1057 | const char *dict, size_t dictlen, | ||
1058 | const char *fmt, ...) | ||
1059 | { | ||
1060 | va_list args; | ||
1061 | int r; | ||
1062 | |||
1063 | va_start(args, fmt); | ||
1064 | r = vprintk_emit(facility, level, dict, dictlen, fmt, args); | ||
1065 | va_end(args); | ||
1066 | |||
1067 | return r; | ||
1068 | } | ||
1069 | EXPORT_SYMBOL(printk_emit); | ||
1070 | |||
1071 | /** | ||
1072 | * printk - print a kernel message | ||
1073 | * @fmt: format string | ||
1074 | * | ||
1075 | * This is printk(). It can be called from any context. We want it to work. | ||
1076 | * | ||
1077 | * We try to grab the console_lock. If we succeed, it's easy - we log the | ||
1078 | * output and call the console drivers. If we fail to get the semaphore, we | ||
1079 | * place the output into the log buffer and return. The current holder of | ||
1080 | * the console_sem will notice the new output in console_unlock(); and will | ||
1081 | * send it to the consoles before releasing the lock. | ||
1082 | * | ||
1083 | * One effect of this deferred printing is that code which calls printk() and | ||
1084 | * then changes console_loglevel may break. This is because console_loglevel | ||
1085 | * is inspected when the actual printing occurs. | ||
1086 | * | ||
1087 | * See also: | ||
1088 | * printf(3) | ||
1089 | * | ||
1090 | * See the vsnprintf() documentation for format string extensions over C99. | ||
1091 | */ | ||
1092 | asmlinkage int printk(const char *fmt, ...) | ||
1093 | { | ||
1094 | va_list args; | ||
1095 | int r; | ||
1096 | |||
1097 | #ifdef CONFIG_KGDB_KDB | ||
1098 | if (unlikely(kdb_trap_printk)) { | ||
1099 | va_start(args, fmt); | ||
1100 | r = vkdb_printf(fmt, args); | ||
1101 | va_end(args); | ||
1102 | return r; | ||
1103 | } | ||
1104 | #endif | ||
1105 | va_start(args, fmt); | ||
1106 | r = vprintk_emit(0, -1, NULL, 0, fmt, args); | ||
1107 | va_end(args); | ||
1108 | |||
1109 | return r; | ||
1110 | } | ||
1111 | EXPORT_SYMBOL(printk); | ||
980 | #else | 1112 | #else |
981 | 1113 | ||
982 | static void call_console_drivers(unsigned start, unsigned end) | 1114 | static void call_console_drivers(int level, const char *text, size_t len) |
983 | { | 1115 | { |
984 | } | 1116 | } |
985 | 1117 | ||
@@ -1217,7 +1349,7 @@ int is_console_locked(void) | |||
1217 | } | 1349 | } |
1218 | 1350 | ||
1219 | /* | 1351 | /* |
1220 | * Delayed printk facility, for scheduler-internal messages: | 1352 | * Delayed printk version, for scheduler-internal messages: |
1221 | */ | 1353 | */ |
1222 | #define PRINTK_BUF_SIZE 512 | 1354 | #define PRINTK_BUF_SIZE 512 |
1223 | 1355 | ||
@@ -1253,6 +1385,10 @@ void wake_up_klogd(void) | |||
1253 | this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP); | 1385 | this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP); |
1254 | } | 1386 | } |
1255 | 1387 | ||
1388 | /* the next printk record to write to the console */ | ||
1389 | static u64 console_seq; | ||
1390 | static u32 console_idx; | ||
1391 | |||
1256 | /** | 1392 | /** |
1257 | * console_unlock - unlock the console system | 1393 | * console_unlock - unlock the console system |
1258 | * | 1394 | * |
@@ -1263,15 +1399,16 @@ void wake_up_klogd(void) | |||
1263 | * by printk(). If this is the case, console_unlock(); emits | 1399 | * by printk(). If this is the case, console_unlock(); emits |
1264 | * the output prior to releasing the lock. | 1400 | * the output prior to releasing the lock. |
1265 | * | 1401 | * |
1266 | * If there is output waiting for klogd, we wake it up. | 1402 | * If there is output waiting, we wake it /dev/kmsg and syslog() users. |
1267 | * | 1403 | * |
1268 | * console_unlock(); may be called from any context. | 1404 | * console_unlock(); may be called from any context. |
1269 | */ | 1405 | */ |
1270 | void console_unlock(void) | 1406 | void console_unlock(void) |
1271 | { | 1407 | { |
1408 | static u64 seen_seq; | ||
1272 | unsigned long flags; | 1409 | unsigned long flags; |
1273 | unsigned _con_start, _log_end; | 1410 | bool wake_klogd = false; |
1274 | unsigned wake_klogd = 0, retry = 0; | 1411 | bool retry; |
1275 | 1412 | ||
1276 | if (console_suspended) { | 1413 | if (console_suspended) { |
1277 | up(&console_sem); | 1414 | up(&console_sem); |
@@ -1281,17 +1418,41 @@ void console_unlock(void) | |||
1281 | console_may_schedule = 0; | 1418 | console_may_schedule = 0; |
1282 | 1419 | ||
1283 | again: | 1420 | again: |
1284 | for ( ; ; ) { | 1421 | for (;;) { |
1422 | struct log *msg; | ||
1423 | static char text[LOG_LINE_MAX]; | ||
1424 | size_t len; | ||
1425 | int level; | ||
1426 | |||
1285 | raw_spin_lock_irqsave(&logbuf_lock, flags); | 1427 | raw_spin_lock_irqsave(&logbuf_lock, flags); |
1286 | wake_klogd |= log_start - log_end; | 1428 | if (seen_seq != log_next_seq) { |
1287 | if (con_start == log_end) | 1429 | wake_klogd = true; |
1288 | break; /* Nothing to print */ | 1430 | seen_seq = log_next_seq; |
1289 | _con_start = con_start; | 1431 | } |
1290 | _log_end = log_end; | 1432 | |
1291 | con_start = log_end; /* Flush */ | 1433 | if (console_seq < log_first_seq) { |
1434 | /* messages are gone, move to first one */ | ||
1435 | console_seq = log_first_seq; | ||
1436 | console_idx = log_first_idx; | ||
1437 | } | ||
1438 | |||
1439 | if (console_seq == log_next_seq) | ||
1440 | break; | ||
1441 | |||
1442 | msg = log_from_idx(console_idx); | ||
1443 | level = msg->level & 7; | ||
1444 | len = msg->text_len; | ||
1445 | if (len+1 >= sizeof(text)) | ||
1446 | len = sizeof(text)-1; | ||
1447 | memcpy(text, log_text(msg), len); | ||
1448 | text[len++] = '\n'; | ||
1449 | |||
1450 | console_idx = log_next(console_idx); | ||
1451 | console_seq++; | ||
1292 | raw_spin_unlock(&logbuf_lock); | 1452 | raw_spin_unlock(&logbuf_lock); |
1453 | |||
1293 | stop_critical_timings(); /* don't trace print latency */ | 1454 | stop_critical_timings(); /* don't trace print latency */ |
1294 | call_console_drivers(_con_start, _log_end); | 1455 | call_console_drivers(level, text, len); |
1295 | start_critical_timings(); | 1456 | start_critical_timings(); |
1296 | local_irq_restore(flags); | 1457 | local_irq_restore(flags); |
1297 | } | 1458 | } |
@@ -1312,8 +1473,7 @@ again: | |||
1312 | * flush, no worries. | 1473 | * flush, no worries. |
1313 | */ | 1474 | */ |
1314 | raw_spin_lock(&logbuf_lock); | 1475 | raw_spin_lock(&logbuf_lock); |
1315 | if (con_start != log_end) | 1476 | retry = console_seq != log_next_seq; |
1316 | retry = 1; | ||
1317 | raw_spin_unlock_irqrestore(&logbuf_lock, flags); | 1477 | raw_spin_unlock_irqrestore(&logbuf_lock, flags); |
1318 | 1478 | ||
1319 | if (retry && console_trylock()) | 1479 | if (retry && console_trylock()) |
@@ -1549,7 +1709,8 @@ void register_console(struct console *newcon) | |||
1549 | * for us. | 1709 | * for us. |
1550 | */ | 1710 | */ |
1551 | raw_spin_lock_irqsave(&logbuf_lock, flags); | 1711 | raw_spin_lock_irqsave(&logbuf_lock, flags); |
1552 | con_start = log_start; | 1712 | console_seq = syslog_seq; |
1713 | console_idx = syslog_idx; | ||
1553 | raw_spin_unlock_irqrestore(&logbuf_lock, flags); | 1714 | raw_spin_unlock_irqrestore(&logbuf_lock, flags); |
1554 | /* | 1715 | /* |
1555 | * We're about to replay the log buffer. Only do this to the | 1716 | * We're about to replay the log buffer. Only do this to the |
@@ -1758,6 +1919,9 @@ int kmsg_dump_unregister(struct kmsg_dumper *dumper) | |||
1758 | } | 1919 | } |
1759 | EXPORT_SYMBOL_GPL(kmsg_dump_unregister); | 1920 | EXPORT_SYMBOL_GPL(kmsg_dump_unregister); |
1760 | 1921 | ||
1922 | static bool always_kmsg_dump; | ||
1923 | module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR); | ||
1924 | |||
1761 | /** | 1925 | /** |
1762 | * kmsg_dump - dump kernel log to kernel message dumpers. | 1926 | * kmsg_dump - dump kernel log to kernel message dumpers. |
1763 | * @reason: the reason (oops, panic etc) for dumping | 1927 | * @reason: the reason (oops, panic etc) for dumping |
@@ -1767,8 +1931,7 @@ EXPORT_SYMBOL_GPL(kmsg_dump_unregister); | |||
1767 | */ | 1931 | */ |
1768 | void kmsg_dump(enum kmsg_dump_reason reason) | 1932 | void kmsg_dump(enum kmsg_dump_reason reason) |
1769 | { | 1933 | { |
1770 | unsigned long end; | 1934 | u64 idx; |
1771 | unsigned chars; | ||
1772 | struct kmsg_dumper *dumper; | 1935 | struct kmsg_dumper *dumper; |
1773 | const char *s1, *s2; | 1936 | const char *s1, *s2; |
1774 | unsigned long l1, l2; | 1937 | unsigned long l1, l2; |
@@ -1780,24 +1943,27 @@ void kmsg_dump(enum kmsg_dump_reason reason) | |||
1780 | /* Theoretically, the log could move on after we do this, but | 1943 | /* Theoretically, the log could move on after we do this, but |
1781 | there's not a lot we can do about that. The new messages | 1944 | there's not a lot we can do about that. The new messages |
1782 | will overwrite the start of what we dump. */ | 1945 | will overwrite the start of what we dump. */ |
1946 | |||
1783 | raw_spin_lock_irqsave(&logbuf_lock, flags); | 1947 | raw_spin_lock_irqsave(&logbuf_lock, flags); |
1784 | end = log_end & LOG_BUF_MASK; | 1948 | if (syslog_seq < log_first_seq) |
1785 | chars = logged_chars; | 1949 | idx = syslog_idx; |
1786 | raw_spin_unlock_irqrestore(&logbuf_lock, flags); | 1950 | else |
1951 | idx = log_first_idx; | ||
1787 | 1952 | ||
1788 | if (chars > end) { | 1953 | if (idx > log_next_idx) { |
1789 | s1 = log_buf + log_buf_len - chars + end; | 1954 | s1 = log_buf; |
1790 | l1 = chars - end; | 1955 | l1 = log_next_idx; |
1791 | 1956 | ||
1792 | s2 = log_buf; | 1957 | s2 = log_buf + idx; |
1793 | l2 = end; | 1958 | l2 = log_buf_len - idx; |
1794 | } else { | 1959 | } else { |
1795 | s1 = ""; | 1960 | s1 = ""; |
1796 | l1 = 0; | 1961 | l1 = 0; |
1797 | 1962 | ||
1798 | s2 = log_buf + end - chars; | 1963 | s2 = log_buf + idx; |
1799 | l2 = chars; | 1964 | l2 = log_next_idx - idx; |
1800 | } | 1965 | } |
1966 | raw_spin_unlock_irqrestore(&logbuf_lock, flags); | ||
1801 | 1967 | ||
1802 | rcu_read_lock(); | 1968 | rcu_read_lock(); |
1803 | list_for_each_entry_rcu(dumper, &dump_list, list) | 1969 | list_for_each_entry_rcu(dumper, &dump_list, list) |