forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/kernel/printk/printk.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * linux/kernel/printk.c
34 *
....@@ -16,6 +17,8 @@
1617 * 01Mar01 Andrew Morton
1718 */
1819
20
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
+
1922 #include <linux/kernel.h>
2023 #include <linux/mm.h>
2124 #include <linux/tty.h>
....@@ -29,11 +32,9 @@
2932 #include <linux/delay.h>
3033 #include <linux/smp.h>
3134 #include <linux/security.h>
32
-#include <linux/bootmem.h>
3335 #include <linux/memblock.h>
3436 #include <linux/syscalls.h>
3537 #include <linux/crash_core.h>
36
-#include <linux/kdb.h>
3738 #include <linux/ratelimit.h>
3839 #include <linux/kmsg_dump.h>
3940 #include <linux/syslog.h>
....@@ -43,6 +44,9 @@
4344 #include <linux/irq_work.h>
4445 #include <linux/ctype.h>
4546 #include <linux/uio.h>
47
+#include <linux/kthread.h>
48
+#include <linux/kdb.h>
49
+#include <linux/clocksource.h>
4650 #include <linux/sched/clock.h>
4751 #include <linux/sched/debug.h>
4852 #include <linux/sched/task_stack.h>
....@@ -53,28 +57,13 @@
5357 #include <trace/events/initcall.h>
5458 #define CREATE_TRACE_POINTS
5559 #include <trace/events/printk.h>
60
+#undef CREATE_TRACE_POINTS
61
+#include <trace/hooks/printk.h>
62
+#include <trace/hooks/logbuf.h>
5663
64
+#include "printk_ringbuffer.h"
5765 #include "console_cmdline.h"
5866 #include "braille.h"
59
-#include "internal.h"
60
-
61
-#ifdef CONFIG_PRINTK_TIME_FROM_ARM_ARCH_TIMER
62
-#include <clocksource/arm_arch_timer.h>
63
-static u64 get_local_clock(void)
64
-{
65
- u64 ns;
66
-
67
- ns = arch_timer_read_counter() * 1000;
68
- do_div(ns, 24);
69
-
70
- return ns;
71
-}
72
-#else
73
-static inline u64 get_local_clock(void)
74
-{
75
- return local_clock();
76
-}
77
-#endif
7867
7968 int console_printk[4] = {
8069 CONSOLE_LOGLEVEL_DEFAULT, /* console_loglevel */
....@@ -82,6 +71,7 @@
8271 CONSOLE_LOGLEVEL_MIN, /* minimum_console_loglevel */
8372 CONSOLE_LOGLEVEL_DEFAULT, /* default_console_loglevel */
8473 };
74
+EXPORT_SYMBOL_GPL(console_printk);
8575
8676 atomic_t ignore_console_lock_warning __read_mostly = ATOMIC_INIT(0);
8777 EXPORT_SYMBOL(ignore_console_lock_warning);
....@@ -101,6 +91,12 @@
10191 static DEFINE_SEMAPHORE(console_sem);
10292 struct console *console_drivers;
10393 EXPORT_SYMBOL_GPL(console_drivers);
94
+
95
+/*
96
+ * System may need to suppress printk message under certain
97
+ * circumstances, like after kernel panic happens.
98
+ */
99
+int __read_mostly suppress_printk;
104100
105101 #ifdef CONFIG_LOCKDEP
106102 static struct lockdep_map console_lock_dep_map = {
....@@ -127,26 +123,38 @@
127123
128124 static int __control_devkmsg(char *str)
129125 {
126
+ size_t len;
127
+
130128 if (!str)
131129 return -EINVAL;
132130
133
- if (!strncmp(str, "on", 2)) {
131
+ len = str_has_prefix(str, "on");
132
+ if (len) {
134133 devkmsg_log = DEVKMSG_LOG_MASK_ON;
135
- return 2;
136
- } else if (!strncmp(str, "off", 3)) {
137
- devkmsg_log = DEVKMSG_LOG_MASK_OFF;
138
- return 3;
139
- } else if (!strncmp(str, "ratelimit", 9)) {
140
- devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
141
- return 9;
134
+ return len;
142135 }
136
+
137
+ len = str_has_prefix(str, "off");
138
+ if (len) {
139
+ devkmsg_log = DEVKMSG_LOG_MASK_OFF;
140
+ return len;
141
+ }
142
+
143
+ len = str_has_prefix(str, "ratelimit");
144
+ if (len) {
145
+ devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT;
146
+ return len;
147
+ }
148
+
143149 return -EINVAL;
144150 }
145151
146152 static int __init control_devkmsg(char *str)
147153 {
148
- if (__control_devkmsg(str) < 0)
154
+ if (__control_devkmsg(str) < 0) {
155
+ pr_warn("printk.devkmsg: bad option string '%s'\n", str);
149156 return 1;
157
+ }
150158
151159 /*
152160 * Set sysctl string accordingly:
....@@ -165,14 +173,14 @@
165173 */
166174 devkmsg_log |= DEVKMSG_LOG_MASK_LOCK;
167175
168
- return 0;
176
+ return 1;
169177 }
170178 __setup("printk.devkmsg=", control_devkmsg);
171179
172180 char devkmsg_log_str[DEVKMSG_STR_MAX_SIZE] = "ratelimit";
173181
174182 int devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write,
175
- void __user *buffer, size_t *lenp, loff_t *ppos)
183
+ void *buffer, size_t *lenp, loff_t *ppos)
176184 {
177185 char old_str[DEVKMSG_STR_MAX_SIZE];
178186 unsigned int old;
....@@ -210,16 +218,7 @@
210218 return 0;
211219 }
212220
213
-/*
214
- * Number of registered extended console drivers.
215
- *
216
- * If extended consoles are present, in-kernel cont reassembly is disabled
217
- * and each fragment is stored as a separate log entry with proper
218
- * continuation flag so that every emitted message has full metadata. This
219
- * doesn't change the result for regular consoles or /proc/kmsg. For
220
- * /dev/kmsg, as long as the reader concatenates messages according to
221
- * consecutive continuation flags, the end result should be the same too.
222
- */
221
+/* Number of registered extended console drivers. */
223222 static int nr_ext_console_drivers;
224223
225224 /*
....@@ -233,19 +232,7 @@
233232
234233 static int __down_trylock_console_sem(unsigned long ip)
235234 {
236
- int lock_failed;
237
- unsigned long flags;
238
-
239
- /*
240
- * Here and in __up_console_sem() we need to be in safe mode,
241
- * because spindump/WARN/etc from under console ->lock will
242
- * deadlock in printk()->down_trylock_console_sem() otherwise.
243
- */
244
- printk_safe_enter_irqsave(flags);
245
- lock_failed = down_trylock(&console_sem);
246
- printk_safe_exit_irqrestore(flags);
247
-
248
- if (lock_failed)
235
+ if (down_trylock(&console_sem))
249236 return 1;
250237 mutex_acquire(&console_lock_dep_map, 0, 1, ip);
251238 return 0;
....@@ -254,13 +241,9 @@
254241
255242 static void __up_console_sem(unsigned long ip)
256243 {
257
- unsigned long flags;
244
+ mutex_release(&console_lock_dep_map, ip);
258245
259
- mutex_release(&console_lock_dep_map, 1, ip);
260
-
261
- printk_safe_enter_irqsave(flags);
262246 up(&console_sem);
263
- printk_safe_exit_irqrestore(flags);
264247 }
265248 #define up_console_sem() __up_console_sem(_RET_IP_)
266249
....@@ -275,11 +258,6 @@
275258 static int console_locked, console_suspended;
276259
277260 /*
278
- * If exclusive_console is non-NULL then only this console is to be printed to.
279
- */
280
-static struct console *exclusive_console;
281
-
282
-/*
283261 * Array of consoles built from command line options (console=)
284262 */
285263
....@@ -288,6 +266,7 @@
288266 static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES];
289267
290268 static int preferred_console = -1;
269
+static bool has_preferred_console;
291270 int console_set_on_cmdline;
292271 EXPORT_SYMBOL(console_set_on_cmdline);
293272
....@@ -302,30 +281,22 @@
302281 static int console_msg_format = MSG_FORMAT_DEFAULT;
303282
304283 /*
305
- * The printk log buffer consists of a chain of concatenated variable
306
- * length records. Every record starts with a record header, containing
307
- * the overall length of the record.
284
+ * The printk log buffer consists of a sequenced collection of records, each
285
+ * containing variable length message text. Every record also contains its
286
+ * own meta-data (@info).
308287 *
309
- * The heads to the first and last entry in the buffer, as well as the
310
- * sequence numbers of these entries are maintained when messages are
311
- * stored.
288
+ * Every record meta-data carries the timestamp in microseconds, as well as
289
+ * the standard userspace syslog level and syslog facility. The usual kernel
290
+ * messages use LOG_KERN; userspace-injected messages always carry a matching
291
+ * syslog facility, by default LOG_USER. The origin of every message can be
292
+ * reliably determined that way.
312293 *
313
- * If the heads indicate available messages, the length in the header
314
- * tells the start next message. A length == 0 for the next message
315
- * indicates a wrap-around to the beginning of the buffer.
294
+ * The human readable log message of a record is available in @text, the
295
+ * length of the message text in @text_len. The stored message is not
296
+ * terminated.
316297 *
317
- * Every record carries the monotonic timestamp in microseconds, as well as
318
- * the standard userspace syslog level and syslog facility. The usual
319
- * kernel messages use LOG_KERN; userspace-injected messages always carry
320
- * a matching syslog facility, by default LOG_USER. The origin of every
321
- * message can be reliably determined that way.
322
- *
323
- * The human readable log message directly follows the message header. The
324
- * length of the message text is stored in the header, the stored message
325
- * is not terminated.
326
- *
327
- * Optionally, a message can carry a dictionary of properties (key/value pairs),
328
- * to provide userspace with a machine-readable message context.
298
+ * Optionally, a record can carry a dictionary of properties (key/value
299
+ * pairs), to provide userspace with a machine-readable message context.
329300 *
330301 * Examples for well-defined, commonly used property names are:
331302 * DEVICE=b12:8 device identifier
....@@ -335,25 +306,22 @@
335306 * +sound:card0 subsystem:devname
336307 * SUBSYSTEM=pci driver-core subsystem name
337308 *
338
- * Valid characters in property names are [a-zA-Z0-9.-_]. The plain text value
339
- * follows directly after a '=' character. Every property is terminated by
340
- * a '\0' character. The last property is not terminated.
309
+ * Valid characters in property names are [a-zA-Z0-9.-_]. Property names
310
+ * and values are terminated by a '\0' character.
341311 *
342
- * Example of a message structure:
343
- * 0000 ff 8f 00 00 00 00 00 00 monotonic time in nsec
344
- * 0008 34 00 record is 52 bytes long
345
- * 000a 0b 00 text is 11 bytes long
346
- * 000c 1f 00 dictionary is 23 bytes long
347
- * 000e 03 00 LOG_KERN (facility) LOG_ERR (level)
348
- * 0010 69 74 27 73 20 61 20 6c "it's a l"
349
- * 69 6e 65 "ine"
350
- * 001b 44 45 56 49 43 "DEVIC"
351
- * 45 3d 62 38 3a 32 00 44 "E=b8:2\0D"
352
- * 52 49 56 45 52 3d 62 75 "RIVER=bu"
353
- * 67 "g"
354
- * 0032 00 00 00 padding to next message header
312
+ * Example of record values:
313
+ * record.text_buf = "it's a line" (unterminated)
314
+ * record.info.seq = 56
315
+ * record.info.ts_nsec = 36863
316
+ * record.info.text_len = 11
317
+ * record.info.facility = 0 (LOG_KERN)
318
+ * record.info.flags = 0
319
+ * record.info.level = 3 (LOG_ERR)
320
+ * record.info.caller_id = 299 (task 299)
321
+ * record.info.dev_info.subsystem = "pci" (terminated)
322
+ * record.info.dev_info.device = "+pci:0000:00:01.0" (terminated)
355323 *
356
- * The 'struct printk_log' buffer header must never be directly exported to
324
+ * The 'struct printk_info' buffer must never be directly exported to
357325 * userspace, it is a kernel-private implementation detail that might
358326 * need to be changed in the future, when the requirements change.
359327 *
....@@ -370,106 +338,75 @@
370338
371339 enum log_flags {
372340 LOG_NEWLINE = 2, /* text ended with a newline */
373
- LOG_PREFIX = 4, /* text started with a prefix */
374341 LOG_CONT = 8, /* text is a fragment of a continuation line */
375342 };
376343
377
-struct printk_log {
378
- u64 ts_nsec; /* timestamp in nanoseconds */
379
- u16 len; /* length of entire record */
380
- u16 text_len; /* length of text buffer */
381
- u16 dict_len; /* length of dictionary buffer */
382
- u8 facility; /* syslog facility */
383
- u8 flags:5; /* internal record flags */
384
- u8 level:3; /* syslog level */
385
-#ifdef CONFIG_PRINTK_PROCESS
386
- char process[16]; /* process name */
387
- pid_t pid; /* process id */
388
- u8 cpu; /* cpu id */
389
- u8 in_interrupt; /* interrupt context */
390
-#endif
391
-}
392
-#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
393
-__packed __aligned(4)
394
-#endif
395
-;
396
-
397
-/*
398
- * The logbuf_lock protects kmsg buffer, indices, counters. This can be taken
399
- * within the scheduler's rq lock. It must be released before calling
400
- * console_unlock() or anything else that might wake up a process.
401
- */
402
-DEFINE_RAW_SPINLOCK(logbuf_lock);
403
-
404
-/*
405
- * Helper macros to lock/unlock logbuf_lock and switch between
406
- * printk-safe/unsafe modes.
407
- */
408
-#define logbuf_lock_irq() \
409
- do { \
410
- printk_safe_enter_irq(); \
411
- raw_spin_lock(&logbuf_lock); \
412
- } while (0)
413
-
414
-#define logbuf_unlock_irq() \
415
- do { \
416
- raw_spin_unlock(&logbuf_lock); \
417
- printk_safe_exit_irq(); \
418
- } while (0)
419
-
420
-#define logbuf_lock_irqsave(flags) \
421
- do { \
422
- printk_safe_enter_irqsave(flags); \
423
- raw_spin_lock(&logbuf_lock); \
424
- } while (0)
425
-
426
-#define logbuf_unlock_irqrestore(flags) \
427
- do { \
428
- raw_spin_unlock(&logbuf_lock); \
429
- printk_safe_exit_irqrestore(flags); \
430
- } while (0)
431
-
432344 #ifdef CONFIG_PRINTK
345
+/* syslog_lock protects syslog_* variables and write access to clear_seq. */
346
+static DEFINE_SPINLOCK(syslog_lock);
347
+
348
+/* Set to enable sync mode. Once set, it is never cleared. */
349
+static bool sync_mode;
350
+
433351 DECLARE_WAIT_QUEUE_HEAD(log_wait);
352
+/* All 3 protected by @syslog_lock. */
434353 /* the next printk record to read by syslog(READ) or /proc/kmsg */
435354 static u64 syslog_seq;
436
-static u32 syslog_idx;
437355 static size_t syslog_partial;
356
+static bool syslog_time;
438357
439
-/* index and sequence number of the first record stored in the buffer */
440
-static u64 log_first_seq;
441
-static u32 log_first_idx;
358
+struct latched_seq {
359
+ seqcount_latch_t latch;
360
+ u64 val[2];
361
+};
442362
443
-/* index and sequence number of the next record to store in the buffer */
444
-static u64 log_next_seq;
445
-static u32 log_next_idx;
363
+/*
364
+ * The next printk record to read after the last 'clear' command. There are
365
+ * two copies (updated with seqcount_latch) so that reads can locklessly
366
+ * access a valid value. Writers are synchronized by @syslog_lock.
367
+ */
368
+static struct latched_seq clear_seq = {
369
+ .latch = SEQCNT_LATCH_ZERO(clear_seq.latch),
370
+ .val[0] = 0,
371
+ .val[1] = 0,
372
+};
446373
447
-/* the next printk record to write to the console */
448
-static u64 console_seq;
449
-static u32 console_idx;
450
-static u64 exclusive_console_stop_seq;
451
-
452
-/* the next printk record to read after the last 'clear' command */
453
-static u64 clear_seq;
454
-static u32 clear_idx;
455
-
456
-#ifdef CONFIG_PRINTK_PROCESS
374
+#ifdef CONFIG_PRINTK_CALLER
457375 #define PREFIX_MAX 48
458376 #else
459377 #define PREFIX_MAX 32
460378 #endif
379
+
380
+/* the maximum size allowed to be reserved for a record */
461381 #define LOG_LINE_MAX (1024 - PREFIX_MAX)
462382
463383 #define LOG_LEVEL(v) ((v) & 0x07)
464384 #define LOG_FACILITY(v) ((v) >> 3 & 0xff)
465385
466386 /* record buffer */
467
-#define LOG_ALIGN __alignof__(struct printk_log)
387
+#define LOG_ALIGN __alignof__(unsigned long)
468388 #define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)
469389 #define LOG_BUF_LEN_MAX (u32)(1 << 31)
470390 static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN);
471391 static char *log_buf = __log_buf;
472392 static u32 log_buf_len = __LOG_BUF_LEN;
393
+
394
+/*
395
+ * Define the average message size. This only affects the number of
396
+ * descriptors that will be available. Underestimating is better than
397
+ * overestimating (too many available descriptors is better than not enough).
398
+ */
399
+#define PRB_AVGBITS 5 /* 32 character average length */
400
+
401
+#if CONFIG_LOG_BUF_SHIFT <= PRB_AVGBITS
402
+#error CONFIG_LOG_BUF_SHIFT value too small.
403
+#endif
404
+_DEFINE_PRINTKRB(printk_rb_static, CONFIG_LOG_BUF_SHIFT - PRB_AVGBITS,
405
+ PRB_AVGBITS, &__log_buf[0]);
406
+
407
+static struct printk_ringbuffer printk_rb_dynamic;
408
+
409
+static struct printk_ringbuffer *prb = &printk_rb_static;
473410
474411 /*
475412 * We cannot access per-CPU data (e.g. per-CPU flush irq_work) before
....@@ -478,9 +415,34 @@
478415 */
479416 static bool __printk_percpu_data_ready __read_mostly;
480417
481
-bool printk_percpu_data_ready(void)
418
+static bool printk_percpu_data_ready(void)
482419 {
483420 return __printk_percpu_data_ready;
421
+}
422
+
423
+/* Must be called under syslog_lock. */
424
+static void latched_seq_write(struct latched_seq *ls, u64 val)
425
+{
426
+ raw_write_seqcount_latch(&ls->latch);
427
+ ls->val[0] = val;
428
+ raw_write_seqcount_latch(&ls->latch);
429
+ ls->val[1] = val;
430
+}
431
+
432
+/* Can be called from any context. */
433
+static u64 latched_seq_read_nolock(struct latched_seq *ls)
434
+{
435
+ unsigned int seq;
436
+ unsigned int idx;
437
+ u64 val;
438
+
439
+ do {
440
+ seq = raw_read_seqcount_latch(&ls->latch);
441
+ idx = seq & 0x1;
442
+ val = ls->val[idx];
443
+ } while (read_seqcount_latch_retry(&ls->latch, seq));
444
+
445
+ return val;
484446 }
485447
486448 /* Return log buffer address */
....@@ -497,127 +459,6 @@
497459 }
498460 EXPORT_SYMBOL_GPL(log_buf_len_get);
499461
500
-/* human readable text of the record */
501
-static char *log_text(const struct printk_log *msg)
502
-{
503
- return (char *)msg + sizeof(struct printk_log);
504
-}
505
-
506
-/* optional key/value pair dictionary attached to the record */
507
-static char *log_dict(const struct printk_log *msg)
508
-{
509
- return (char *)msg + sizeof(struct printk_log) + msg->text_len;
510
-}
511
-
512
-/* get record by index; idx must point to valid msg */
513
-static struct printk_log *log_from_idx(u32 idx)
514
-{
515
- struct printk_log *msg = (struct printk_log *)(log_buf + idx);
516
-
517
- /*
518
- * A length == 0 record is the end of buffer marker. Wrap around and
519
- * read the message at the start of the buffer.
520
- */
521
- if (!msg->len)
522
- return (struct printk_log *)log_buf;
523
- return msg;
524
-}
525
-
526
-/* get next record; idx must point to valid msg */
527
-static u32 log_next(u32 idx)
528
-{
529
- struct printk_log *msg = (struct printk_log *)(log_buf + idx);
530
-
531
- /* length == 0 indicates the end of the buffer; wrap */
532
- /*
533
- * A length == 0 record is the end of buffer marker. Wrap around and
534
- * read the message at the start of the buffer as *this* one, and
535
- * return the one after that.
536
- */
537
- if (!msg->len) {
538
- msg = (struct printk_log *)log_buf;
539
- return msg->len;
540
- }
541
- return idx + msg->len;
542
-}
543
-
544
-#ifdef CONFIG_PRINTK_PROCESS
545
-static bool printk_process = true;
546
-static size_t print_process(const struct printk_log *msg, char *buf)
547
-{
548
- if (!printk_process)
549
- return 0;
550
-
551
- if (!buf)
552
- return snprintf(NULL, 0, "%c[%1d:%15s:%5d] ", ' ', 0, " ", 0);
553
-
554
- return sprintf(buf, "%c[%1d:%15s:%5d] ",
555
- msg->in_interrupt ? 'I' : ' ',
556
- msg->cpu,
557
- msg->process,
558
- msg->pid);
559
-}
560
-module_param_named(process, printk_process, bool, 0644);
561
-#endif
562
-
563
-/*
564
- * Check whether there is enough free space for the given message.
565
- *
566
- * The same values of first_idx and next_idx mean that the buffer
567
- * is either empty or full.
568
- *
569
- * If the buffer is empty, we must respect the position of the indexes.
570
- * They cannot be reset to the beginning of the buffer.
571
- */
572
-static int logbuf_has_space(u32 msg_size, bool empty)
573
-{
574
- u32 free;
575
-
576
- if (log_next_idx > log_first_idx || empty)
577
- free = max(log_buf_len - log_next_idx, log_first_idx);
578
- else
579
- free = log_first_idx - log_next_idx;
580
-
581
- /*
582
- * We need space also for an empty header that signalizes wrapping
583
- * of the buffer.
584
- */
585
- return free >= msg_size + sizeof(struct printk_log);
586
-}
587
-
588
-static int log_make_free_space(u32 msg_size)
589
-{
590
- while (log_first_seq < log_next_seq &&
591
- !logbuf_has_space(msg_size, false)) {
592
- /* drop old messages until we have enough contiguous space */
593
- log_first_idx = log_next(log_first_idx);
594
- log_first_seq++;
595
- }
596
-
597
- if (clear_seq < log_first_seq) {
598
- clear_seq = log_first_seq;
599
- clear_idx = log_first_idx;
600
- }
601
-
602
- /* sequence numbers are equal, so the log buffer is empty */
603
- if (logbuf_has_space(msg_size, log_first_seq == log_next_seq))
604
- return 0;
605
-
606
- return -ENOMEM;
607
-}
608
-
609
-/* compute the message size including the padding bytes */
610
-static u32 msg_used_size(u16 text_len, u16 dict_len, u32 *pad_len)
611
-{
612
- u32 size;
613
-
614
- size = sizeof(struct printk_log) + text_len + dict_len;
615
- *pad_len = (-size) & (LOG_ALIGN - 1);
616
- size += *pad_len;
617
-
618
- return size;
619
-}
620
-
621462 /*
622463 * Define how much of the log buffer we could take at maximum. The value
623464 * must be greater than two. Note that only half of the buffer is available
....@@ -626,91 +467,23 @@
626467 #define MAX_LOG_TAKE_PART 4
627468 static const char trunc_msg[] = "<truncated>";
628469
629
-static u32 truncate_msg(u16 *text_len, u16 *trunc_msg_len,
630
- u16 *dict_len, u32 *pad_len)
470
+static void truncate_msg(u16 *text_len, u16 *trunc_msg_len)
631471 {
632472 /*
633473 * The message should not take the whole buffer. Otherwise, it might
634474 * get removed too soon.
635475 */
636476 u32 max_text_len = log_buf_len / MAX_LOG_TAKE_PART;
477
+
637478 if (*text_len > max_text_len)
638479 *text_len = max_text_len;
639
- /* enable the warning message */
480
+
481
+ /* enable the warning message (if there is room) */
640482 *trunc_msg_len = strlen(trunc_msg);
641
- /* disable the "dict" completely */
642
- *dict_len = 0;
643
- /* compute the size again, count also the warning message */
644
- return msg_used_size(*text_len + *trunc_msg_len, 0, pad_len);
645
-}
646
-
647
-/* insert record into the buffer, discard old ones, update heads */
648
-static int log_store(int facility, int level,
649
- enum log_flags flags, u64 ts_nsec,
650
- const char *dict, u16 dict_len,
651
- const char *text, u16 text_len)
652
-{
653
- struct printk_log *msg;
654
- u32 size, pad_len;
655
- u16 trunc_msg_len = 0;
656
-
657
- /* number of '\0' padding bytes to next message */
658
- size = msg_used_size(text_len, dict_len, &pad_len);
659
-
660
- if (log_make_free_space(size)) {
661
- /* truncate the message if it is too long for empty buffer */
662
- size = truncate_msg(&text_len, &trunc_msg_len,
663
- &dict_len, &pad_len);
664
- /* survive when the log buffer is too small for trunc_msg */
665
- if (log_make_free_space(size))
666
- return 0;
667
- }
668
-
669
- if (log_next_idx + size + sizeof(struct printk_log) > log_buf_len) {
670
- /*
671
- * This message + an additional empty header does not fit
672
- * at the end of the buffer. Add an empty header with len == 0
673
- * to signify a wrap around.
674
- */
675
- memset(log_buf + log_next_idx, 0, sizeof(struct printk_log));
676
- log_next_idx = 0;
677
- }
678
-
679
- /* fill message */
680
- msg = (struct printk_log *)(log_buf + log_next_idx);
681
- memcpy(log_text(msg), text, text_len);
682
- msg->text_len = text_len;
683
- if (trunc_msg_len) {
684
- memcpy(log_text(msg) + text_len, trunc_msg, trunc_msg_len);
685
- msg->text_len += trunc_msg_len;
686
- }
687
- memcpy(log_dict(msg), dict, dict_len);
688
- msg->dict_len = dict_len;
689
- msg->facility = facility;
690
- msg->level = level & 7;
691
- msg->flags = flags & 0x1f;
692
- if (ts_nsec > 0)
693
- msg->ts_nsec = ts_nsec;
483
+ if (*text_len >= *trunc_msg_len)
484
+ *text_len -= *trunc_msg_len;
694485 else
695
- msg->ts_nsec = get_local_clock();
696
- memset(log_dict(msg) + dict_len, 0, pad_len);
697
- msg->len = size;
698
-
699
-#ifdef CONFIG_PRINTK_PROCESS
700
- if (printk_process) {
701
- strncpy(msg->process, current->comm, sizeof(msg->process) - 1);
702
- msg->process[sizeof(msg->process) - 1] = '\0';
703
- msg->pid = task_pid_nr(current);
704
- msg->cpu = raw_smp_processor_id();
705
- msg->in_interrupt = in_interrupt() ? 1 : 0;
706
- }
707
-#endif
708
-
709
- /* insert message */
710
- log_next_idx += msg->len;
711
- log_next_seq++;
712
-
713
- return msg->text_len;
486
+ *trunc_msg_len = 0;
714487 }
715488
716489 int dmesg_restrict = IS_ENABLED(CONFIG_SECURITY_DMESG_RESTRICT);
....@@ -762,21 +535,30 @@
762535 *(*pp)++ = c;
763536 }
764537
765
-static ssize_t msg_print_ext_header(char *buf, size_t size,
766
- struct printk_log *msg, u64 seq)
538
+static ssize_t info_print_ext_header(char *buf, size_t size,
539
+ struct printk_info *info)
767540 {
768
- u64 ts_usec = msg->ts_nsec;
541
+ u64 ts_usec = info->ts_nsec;
542
+ char caller[20];
543
+#ifdef CONFIG_PRINTK_CALLER
544
+ u32 id = info->caller_id;
545
+
546
+ snprintf(caller, sizeof(caller), ",caller=%c%u",
547
+ id & 0x80000000 ? 'C' : 'T', id & ~0x80000000);
548
+#else
549
+ caller[0] = '\0';
550
+#endif
769551
770552 do_div(ts_usec, 1000);
771553
772
- return scnprintf(buf, size, "%u,%llu,%llu,%c;",
773
- (msg->facility << 3) | msg->level, seq, ts_usec,
774
- msg->flags & LOG_CONT ? 'c' : '-');
554
+ return scnprintf(buf, size, "%u,%llu,%llu,%c%s;",
555
+ (info->facility << 3) | info->level, info->seq,
556
+ ts_usec, info->flags & LOG_CONT ? 'c' : '-', caller);
775557 }
776558
777
-static ssize_t msg_print_ext_body(char *buf, size_t size,
778
- char *dict, size_t dict_len,
779
- char *text, size_t text_len)
559
+static ssize_t msg_add_ext_text(char *buf, size_t size,
560
+ const char *text, size_t text_len,
561
+ unsigned char endc)
780562 {
781563 char *p = buf, *e = buf + size;
782564 size_t i;
....@@ -790,46 +572,70 @@
790572 else
791573 append_char(&p, e, c);
792574 }
793
- append_char(&p, e, '\n');
794
-
795
- if (dict_len) {
796
- bool line = true;
797
-
798
- for (i = 0; i < dict_len; i++) {
799
- unsigned char c = dict[i];
800
-
801
- if (line) {
802
- append_char(&p, e, ' ');
803
- line = false;
804
- }
805
-
806
- if (c == '\0') {
807
- append_char(&p, e, '\n');
808
- line = true;
809
- continue;
810
- }
811
-
812
- if (c < ' ' || c >= 127 || c == '\\') {
813
- p += scnprintf(p, e - p, "\\x%02x", c);
814
- continue;
815
- }
816
-
817
- append_char(&p, e, c);
818
- }
819
- append_char(&p, e, '\n');
820
- }
575
+ append_char(&p, e, endc);
821576
822577 return p - buf;
823578 }
824579
580
+static ssize_t msg_add_dict_text(char *buf, size_t size,
581
+ const char *key, const char *val)
582
+{
583
+ size_t val_len = strlen(val);
584
+ ssize_t len;
585
+
586
+ if (!val_len)
587
+ return 0;
588
+
589
+ len = msg_add_ext_text(buf, size, "", 0, ' '); /* dict prefix */
590
+ len += msg_add_ext_text(buf + len, size - len, key, strlen(key), '=');
591
+ len += msg_add_ext_text(buf + len, size - len, val, val_len, '\n');
592
+
593
+ return len;
594
+}
595
+
596
+static ssize_t msg_print_ext_body(char *buf, size_t size,
597
+ char *text, size_t text_len,
598
+ struct dev_printk_info *dev_info)
599
+{
600
+ ssize_t len;
601
+
602
+ len = msg_add_ext_text(buf, size, text, text_len, '\n');
603
+
604
+ if (!dev_info)
605
+ goto out;
606
+
607
+ len += msg_add_dict_text(buf + len, size - len, "SUBSYSTEM",
608
+ dev_info->subsystem);
609
+ len += msg_add_dict_text(buf + len, size - len, "DEVICE",
610
+ dev_info->device);
611
+out:
612
+ return len;
613
+}
614
+
825615 /* /dev/kmsg - userspace message inject/listen interface */
826616 struct devkmsg_user {
827
- u64 seq;
828
- u32 idx;
617
+ atomic64_t seq;
829618 struct ratelimit_state rs;
830619 struct mutex lock;
831620 char buf[CONSOLE_EXT_LOG_MAX];
621
+
622
+ struct printk_info info;
623
+ char text_buf[CONSOLE_EXT_LOG_MAX];
624
+ struct printk_record record;
832625 };
626
+
627
+static __printf(3, 4) __cold
628
+int devkmsg_emit(int facility, int level, const char *fmt, ...)
629
+{
630
+ va_list args;
631
+ int r;
632
+
633
+ va_start(args, fmt);
634
+ r = vprintk_emit(facility, level, NULL, fmt, args);
635
+ va_end(args);
636
+
637
+ return r;
638
+}
833639
834640 static ssize_t devkmsg_write(struct kiocb *iocb, struct iov_iter *from)
835641 {
....@@ -889,7 +695,7 @@
889695 }
890696 }
891697
892
- printk_emit(facility, level, NULL, 0, "%s", line);
698
+ devkmsg_emit(facility, level, "%s", line);
893699 kfree(buf);
894700 return ret;
895701 }
....@@ -898,7 +704,7 @@
898704 size_t count, loff_t *ppos)
899705 {
900706 struct devkmsg_user *user = file->private_data;
901
- struct printk_log *msg;
707
+ struct printk_record *r = &user->record;
902708 size_t len;
903709 ssize_t ret;
904710
....@@ -909,41 +715,31 @@
909715 if (ret)
910716 return ret;
911717
912
- logbuf_lock_irq();
913
- while (user->seq == log_next_seq) {
718
+ if (!prb_read_valid(prb, atomic64_read(&user->seq), r)) {
914719 if (file->f_flags & O_NONBLOCK) {
915720 ret = -EAGAIN;
916
- logbuf_unlock_irq();
917721 goto out;
918722 }
919723
920
- logbuf_unlock_irq();
921724 ret = wait_event_interruptible(log_wait,
922
- user->seq != log_next_seq);
725
+ prb_read_valid(prb, atomic64_read(&user->seq), r));
923726 if (ret)
924727 goto out;
925
- logbuf_lock_irq();
926728 }
927729
928
- if (user->seq < log_first_seq) {
730
+ if (r->info->seq != atomic64_read(&user->seq)) {
929731 /* our last seen message is gone, return error and reset */
930
- user->idx = log_first_idx;
931
- user->seq = log_first_seq;
732
+ atomic64_set(&user->seq, r->info->seq);
932733 ret = -EPIPE;
933
- logbuf_unlock_irq();
934734 goto out;
935735 }
936736
937
- msg = log_from_idx(user->idx);
938
- len = msg_print_ext_header(user->buf, sizeof(user->buf),
939
- msg, user->seq);
737
+ len = info_print_ext_header(user->buf, sizeof(user->buf), r->info);
940738 len += msg_print_ext_body(user->buf + len, sizeof(user->buf) - len,
941
- log_dict(msg), msg->dict_len,
942
- log_text(msg), msg->text_len);
739
+ &r->text_buf[0], r->info->text_len,
740
+ &r->info->dev_info);
943741
944
- user->idx = log_next(user->idx);
945
- user->seq++;
946
- logbuf_unlock_irq();
742
+ atomic64_set(&user->seq, r->info->seq + 1);
947743
948744 if (len > count) {
949745 ret = -EINVAL;
....@@ -960,6 +756,14 @@
960756 return ret;
961757 }
962758
759
+/*
760
+ * Be careful when modifying this function!!!
761
+ *
762
+ * Only few operations are supported because the device works only with the
763
+ * entire variable length messages (records). Non-standard values are
764
+ * returned in the other cases and has been this way for quite some time.
765
+ * User space applications might depend on this behavior.
766
+ */
963767 static loff_t devkmsg_llseek(struct file *file, loff_t offset, int whence)
964768 {
965769 struct devkmsg_user *user = file->private_data;
....@@ -970,12 +774,10 @@
970774 if (offset)
971775 return -ESPIPE;
972776
973
- logbuf_lock_irq();
974777 switch (whence) {
975778 case SEEK_SET:
976779 /* the first record */
977
- user->idx = log_first_idx;
978
- user->seq = log_first_seq;
780
+ atomic64_set(&user->seq, prb_first_valid_seq(prb));
979781 break;
980782 case SEEK_DATA:
981783 /*
....@@ -983,24 +785,22 @@
983785 * like issued by 'dmesg -c'. Reading /dev/kmsg itself
984786 * changes no global state, and does not clear anything.
985787 */
986
- user->idx = clear_idx;
987
- user->seq = clear_seq;
788
+ atomic64_set(&user->seq, latched_seq_read_nolock(&clear_seq));
988789 break;
989790 case SEEK_END:
990791 /* after the last record */
991
- user->idx = log_next_idx;
992
- user->seq = log_next_seq;
792
+ atomic64_set(&user->seq, prb_next_seq(prb));
993793 break;
994794 default:
995795 ret = -EINVAL;
996796 }
997
- logbuf_unlock_irq();
998797 return ret;
999798 }
1000799
1001800 static __poll_t devkmsg_poll(struct file *file, poll_table *wait)
1002801 {
1003802 struct devkmsg_user *user = file->private_data;
803
+ struct printk_info info;
1004804 __poll_t ret = 0;
1005805
1006806 if (!user)
....@@ -1008,15 +808,13 @@
1008808
1009809 poll_wait(file, &log_wait, wait);
1010810
1011
- logbuf_lock_irq();
1012
- if (user->seq < log_next_seq) {
811
+ if (prb_read_valid_info(prb, atomic64_read(&user->seq), &info, NULL)) {
1013812 /* return error when data has vanished underneath us */
1014
- if (user->seq < log_first_seq)
813
+ if (info.seq != atomic64_read(&user->seq))
1015814 ret = EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI;
1016815 else
1017816 ret = EPOLLIN|EPOLLRDNORM;
1018817 }
1019
- logbuf_unlock_irq();
1020818
1021819 return ret;
1022820 }
....@@ -1046,10 +844,10 @@
1046844
1047845 mutex_init(&user->lock);
1048846
1049
- logbuf_lock_irq();
1050
- user->idx = log_first_idx;
1051
- user->seq = log_first_seq;
1052
- logbuf_unlock_irq();
847
+ prb_rec_init_rd(&user->record, &user->info,
848
+ &user->text_buf[0], sizeof(user->text_buf));
849
+
850
+ atomic64_set(&user->seq, prb_first_valid_seq(prb));
1053851
1054852 file->private_data = user;
1055853 return 0;
....@@ -1089,20 +887,61 @@
1089887 */
1090888 void log_buf_vmcoreinfo_setup(void)
1091889 {
1092
- VMCOREINFO_SYMBOL(log_buf);
1093
- VMCOREINFO_SYMBOL(log_buf_len);
1094
- VMCOREINFO_SYMBOL(log_first_idx);
1095
- VMCOREINFO_SYMBOL(clear_idx);
1096
- VMCOREINFO_SYMBOL(log_next_idx);
890
+ struct dev_printk_info *dev_info = NULL;
891
+
892
+ VMCOREINFO_SYMBOL(prb);
893
+ VMCOREINFO_SYMBOL(printk_rb_static);
894
+ VMCOREINFO_SYMBOL(clear_seq);
895
+
1097896 /*
1098
- * Export struct printk_log size and field offsets. User space tools can
897
+ * Export struct size and field offsets. User space tools can
1099898 * parse it and detect any changes to structure down the line.
1100899 */
1101
- VMCOREINFO_STRUCT_SIZE(printk_log);
1102
- VMCOREINFO_OFFSET(printk_log, ts_nsec);
1103
- VMCOREINFO_OFFSET(printk_log, len);
1104
- VMCOREINFO_OFFSET(printk_log, text_len);
1105
- VMCOREINFO_OFFSET(printk_log, dict_len);
900
+
901
+ VMCOREINFO_STRUCT_SIZE(printk_ringbuffer);
902
+ VMCOREINFO_OFFSET(printk_ringbuffer, desc_ring);
903
+ VMCOREINFO_OFFSET(printk_ringbuffer, text_data_ring);
904
+ VMCOREINFO_OFFSET(printk_ringbuffer, fail);
905
+
906
+ VMCOREINFO_STRUCT_SIZE(prb_desc_ring);
907
+ VMCOREINFO_OFFSET(prb_desc_ring, count_bits);
908
+ VMCOREINFO_OFFSET(prb_desc_ring, descs);
909
+ VMCOREINFO_OFFSET(prb_desc_ring, infos);
910
+ VMCOREINFO_OFFSET(prb_desc_ring, head_id);
911
+ VMCOREINFO_OFFSET(prb_desc_ring, tail_id);
912
+
913
+ VMCOREINFO_STRUCT_SIZE(prb_desc);
914
+ VMCOREINFO_OFFSET(prb_desc, state_var);
915
+ VMCOREINFO_OFFSET(prb_desc, text_blk_lpos);
916
+
917
+ VMCOREINFO_STRUCT_SIZE(prb_data_blk_lpos);
918
+ VMCOREINFO_OFFSET(prb_data_blk_lpos, begin);
919
+ VMCOREINFO_OFFSET(prb_data_blk_lpos, next);
920
+
921
+ VMCOREINFO_STRUCT_SIZE(printk_info);
922
+ VMCOREINFO_OFFSET(printk_info, seq);
923
+ VMCOREINFO_OFFSET(printk_info, ts_nsec);
924
+ VMCOREINFO_OFFSET(printk_info, text_len);
925
+ VMCOREINFO_OFFSET(printk_info, caller_id);
926
+ VMCOREINFO_OFFSET(printk_info, dev_info);
927
+
928
+ VMCOREINFO_STRUCT_SIZE(dev_printk_info);
929
+ VMCOREINFO_OFFSET(dev_printk_info, subsystem);
930
+ VMCOREINFO_LENGTH(printk_info_subsystem, sizeof(dev_info->subsystem));
931
+ VMCOREINFO_OFFSET(dev_printk_info, device);
932
+ VMCOREINFO_LENGTH(printk_info_device, sizeof(dev_info->device));
933
+
934
+ VMCOREINFO_STRUCT_SIZE(prb_data_ring);
935
+ VMCOREINFO_OFFSET(prb_data_ring, size_bits);
936
+ VMCOREINFO_OFFSET(prb_data_ring, data);
937
+ VMCOREINFO_OFFSET(prb_data_ring, head_lpos);
938
+ VMCOREINFO_OFFSET(prb_data_ring, tail_lpos);
939
+
940
+ VMCOREINFO_SIZE(atomic_long_t);
941
+ VMCOREINFO_TYPE_OFFSET(atomic_long_t, counter);
942
+
943
+ VMCOREINFO_STRUCT_SIZE(latched_seq);
944
+ VMCOREINFO_OFFSET(latched_seq, val);
1106945 }
1107946 #endif
1108947
....@@ -1174,17 +1013,48 @@
11741013
11751014 static void __init set_percpu_data_ready(void)
11761015 {
1177
- printk_safe_init();
1178
- /* Make sure we set this flag only after printk_safe() init is done */
1179
- barrier();
11801016 __printk_percpu_data_ready = true;
11811017 }
11821018
1019
+static unsigned int __init add_to_rb(struct printk_ringbuffer *rb,
1020
+ struct printk_record *r)
1021
+{
1022
+ struct prb_reserved_entry e;
1023
+ struct printk_record dest_r;
1024
+
1025
+ prb_rec_init_wr(&dest_r, r->info->text_len);
1026
+
1027
+ if (!prb_reserve(&e, rb, &dest_r))
1028
+ return 0;
1029
+
1030
+ memcpy(&dest_r.text_buf[0], &r->text_buf[0], r->info->text_len);
1031
+ dest_r.info->text_len = r->info->text_len;
1032
+ dest_r.info->facility = r->info->facility;
1033
+ dest_r.info->level = r->info->level;
1034
+ dest_r.info->flags = r->info->flags;
1035
+ dest_r.info->ts_nsec = r->info->ts_nsec;
1036
+ dest_r.info->caller_id = r->info->caller_id;
1037
+ memcpy(&dest_r.info->dev_info, &r->info->dev_info, sizeof(dest_r.info->dev_info));
1038
+
1039
+ prb_final_commit(&e);
1040
+
1041
+ return prb_record_text_space(&e);
1042
+}
1043
+
1044
+static char setup_text_buf[LOG_LINE_MAX] __initdata;
1045
+
11831046 void __init setup_log_buf(int early)
11841047 {
1185
- unsigned long flags;
1048
+ struct printk_info *new_infos;
1049
+ unsigned int new_descs_count;
1050
+ struct prb_desc *new_descs;
1051
+ struct printk_info info;
1052
+ struct printk_record r;
1053
+ size_t new_descs_size;
1054
+ size_t new_infos_size;
11861055 char *new_log_buf;
11871056 unsigned int free;
1057
+ u64 seq;
11881058
11891059 /*
11901060 * Some archs call setup_log_buf() multiple times - first is very
....@@ -1203,38 +1073,74 @@
12031073 if (!new_log_buf_len)
12041074 return;
12051075
1206
- if (early) {
1207
- new_log_buf =
1208
- memblock_virt_alloc(new_log_buf_len, LOG_ALIGN);
1209
- } else {
1210
- new_log_buf = memblock_virt_alloc_nopanic(new_log_buf_len,
1211
- LOG_ALIGN);
1212
- }
1213
-
1214
- if (unlikely(!new_log_buf)) {
1215
- pr_err("log_buf_len: %lu bytes not available\n",
1216
- new_log_buf_len);
1076
+ new_descs_count = new_log_buf_len >> PRB_AVGBITS;
1077
+ if (new_descs_count == 0) {
1078
+ pr_err("new_log_buf_len: %lu too small\n", new_log_buf_len);
12171079 return;
12181080 }
12191081
1220
- logbuf_lock_irqsave(flags);
1082
+ new_log_buf = memblock_alloc(new_log_buf_len, LOG_ALIGN);
1083
+ if (unlikely(!new_log_buf)) {
1084
+ pr_err("log_buf_len: %lu text bytes not available\n",
1085
+ new_log_buf_len);
1086
+ return;
1087
+ }
1088
+
1089
+ new_descs_size = new_descs_count * sizeof(struct prb_desc);
1090
+ new_descs = memblock_alloc(new_descs_size, LOG_ALIGN);
1091
+ if (unlikely(!new_descs)) {
1092
+ pr_err("log_buf_len: %zu desc bytes not available\n",
1093
+ new_descs_size);
1094
+ goto err_free_log_buf;
1095
+ }
1096
+
1097
+ new_infos_size = new_descs_count * sizeof(struct printk_info);
1098
+ new_infos = memblock_alloc(new_infos_size, LOG_ALIGN);
1099
+ if (unlikely(!new_infos)) {
1100
+ pr_err("log_buf_len: %zu info bytes not available\n",
1101
+ new_infos_size);
1102
+ goto err_free_descs;
1103
+ }
1104
+
1105
+ prb_rec_init_rd(&r, &info, &setup_text_buf[0], sizeof(setup_text_buf));
1106
+
1107
+ prb_init(&printk_rb_dynamic,
1108
+ new_log_buf, ilog2(new_log_buf_len),
1109
+ new_descs, ilog2(new_descs_count),
1110
+ new_infos);
1111
+
12211112 log_buf_len = new_log_buf_len;
12221113 log_buf = new_log_buf;
12231114 new_log_buf_len = 0;
1224
- free = __LOG_BUF_LEN - log_next_idx;
1225
- memcpy(log_buf, __log_buf, __LOG_BUF_LEN);
1226
- logbuf_unlock_irqrestore(flags);
1115
+
1116
+ free = __LOG_BUF_LEN;
1117
+ prb_for_each_record(0, &printk_rb_static, seq, &r)
1118
+ free -= add_to_rb(&printk_rb_dynamic, &r);
1119
+
1120
+ /*
1121
+ * This is early enough that everything is still running on the
1122
+ * boot CPU and interrupts are disabled. So no new messages will
1123
+ * appear during the transition to the dynamic buffer.
1124
+ */
1125
+ prb = &printk_rb_dynamic;
1126
+
1127
+ if (seq != prb_next_seq(&printk_rb_static)) {
1128
+ pr_err("dropped %llu messages\n",
1129
+ prb_next_seq(&printk_rb_static) - seq);
1130
+ }
12271131
12281132 pr_info("log_buf_len: %u bytes\n", log_buf_len);
12291133 pr_info("early log buf free: %u(%u%%)\n",
12301134 free, (free * 100) / __LOG_BUF_LEN);
1135
+ return;
1136
+
1137
+err_free_descs:
1138
+ memblock_free(__pa(new_descs), new_descs_size);
1139
+err_free_log_buf:
1140
+ memblock_free(__pa(new_log_buf), new_log_buf_len);
12311141 }
12321142
1233
-#ifdef CONFIG_PSTORE_CONSOLE_FORCE_ON
1234
-static bool __read_mostly ignore_loglevel = true;
1235
-#else
12361143 static bool __read_mostly ignore_loglevel;
1237
-#endif
12381144
12391145 static int __init ignore_loglevel_setup(char *str)
12401146 {
....@@ -1248,61 +1154,6 @@
12481154 module_param(ignore_loglevel, bool, S_IRUGO | S_IWUSR);
12491155 MODULE_PARM_DESC(ignore_loglevel,
12501156 "ignore loglevel setting (prints all kernel messages to the console)");
1251
-
1252
-#ifdef CONFIG_PSTORE_CONSOLE_FORCE
1253
-static bool __read_mostly pstore_con_force = IS_ENABLED(CONFIG_PSTORE_CONSOLE_FORCE_ON);
1254
-
1255
-static int __init pstore_con_force_setup(char *str)
1256
-{
1257
- bool force;
1258
- int ret = strtobool(str, &force);
1259
-
1260
- if (ret)
1261
- return ret;
1262
-
1263
- ignore_loglevel = force;
1264
- pstore_con_force = force;
1265
- if (force)
1266
- pr_info("debug: pstore console ignoring loglevel setting.\n");
1267
-
1268
- return 0;
1269
-}
1270
-
1271
-early_param("pstore_con_force", pstore_con_force_setup);
1272
-module_param(pstore_con_force, bool, S_IRUGO | S_IWUSR);
1273
-MODULE_PARM_DESC(pstore_con_force,
1274
- "ignore loglevel setting (prints all kernel messages to the pstore console)");
1275
-
1276
-static void call_console_drivers_level(int level, const char *ext_text, size_t ext_len,
1277
- const char *text, size_t len)
1278
-{
1279
- struct console *con;
1280
-
1281
- trace_console_rcuidle(text, len);
1282
-
1283
- if (!console_drivers)
1284
- return;
1285
-
1286
- for_each_console(con) {
1287
- if (pstore_con_force &&
1288
- !(con->flags & CON_PSTORE) && level >= console_loglevel)
1289
- continue;
1290
- if (exclusive_console && con != exclusive_console)
1291
- continue;
1292
- if (!(con->flags & CON_ENABLED))
1293
- continue;
1294
- if (!con->write)
1295
- continue;
1296
- if (!cpu_online(smp_processor_id()) &&
1297
- !(con->flags & CON_ANYTIME))
1298
- continue;
1299
- if (con->flags & CON_EXTENDED)
1300
- con->write(con, ext_text, ext_len);
1301
- else
1302
- con->write(con, text, len);
1303
- }
1304
-}
1305
-#endif
13061157
13071158 static bool suppress_message_printing(int level)
13081159 {
....@@ -1367,121 +1218,272 @@
13671218 static bool printk_time = IS_ENABLED(CONFIG_PRINTK_TIME);
13681219 module_param_named(time, printk_time, bool, S_IRUGO | S_IWUSR);
13691220
1221
+static size_t print_syslog(unsigned int level, char *buf)
1222
+{
1223
+ return sprintf(buf, "<%u>", level);
1224
+}
1225
+
13701226 static size_t print_time(u64 ts, char *buf)
13711227 {
1372
- unsigned long rem_nsec;
1228
+ unsigned long rem_nsec = do_div(ts, 1000000000);
13731229
1374
- if (!printk_time)
1375
- return 0;
1376
-
1377
- rem_nsec = do_div(ts, 1000000000);
1378
-
1379
- if (!buf)
1380
- return snprintf(NULL, 0, "[%5lu.000000] ", (unsigned long)ts);
1381
-
1382
- return sprintf(buf, "[%5lu.%06lu] ",
1230
+ return sprintf(buf, "[%5lu.%06lu]",
13831231 (unsigned long)ts, rem_nsec / 1000);
13841232 }
13851233
1386
-static size_t print_prefix(const struct printk_log *msg, bool syslog, char *buf)
1234
+#ifdef CONFIG_PRINTK_CALLER
1235
+static size_t print_caller(u32 id, char *buf)
1236
+{
1237
+ char caller[12];
1238
+
1239
+ snprintf(caller, sizeof(caller), "%c%u",
1240
+ id & 0x80000000 ? 'C' : 'T', id & ~0x80000000);
1241
+ return sprintf(buf, "[%6s]", caller);
1242
+}
1243
+#else
1244
+#define print_caller(id, buf) 0
1245
+#endif
1246
+
1247
+static size_t info_print_prefix(const struct printk_info *info, bool syslog,
1248
+ bool time, char *buf)
13871249 {
13881250 size_t len = 0;
1389
- unsigned int prefix = (msg->facility << 3) | msg->level;
13901251
1391
- if (syslog) {
1392
- if (buf) {
1393
- len += sprintf(buf, "<%u>", prefix);
1394
- } else {
1395
- len += 3;
1396
- if (prefix > 999)
1397
- len += 3;
1398
- else if (prefix > 99)
1399
- len += 2;
1400
- else if (prefix > 9)
1401
- len++;
1402
- }
1252
+ if (syslog)
1253
+ len = print_syslog((info->facility << 3) | info->level, buf);
1254
+
1255
+ if (time)
1256
+ len += print_time(info->ts_nsec, buf + len);
1257
+
1258
+ len += print_caller(info->caller_id, buf + len);
1259
+
1260
+ if (IS_ENABLED(CONFIG_PRINTK_CALLER) || time) {
1261
+ buf[len++] = ' ';
1262
+ buf[len] = '\0';
14031263 }
14041264
1405
- len += print_time(msg->ts_nsec, buf ? buf + len : NULL);
1406
-#ifdef CONFIG_PRINTK_PROCESS
1407
- len += print_process(msg, buf ? buf + len : NULL);
1408
-#endif
14091265 return len;
14101266 }
14111267
1412
-static size_t msg_print_text(const struct printk_log *msg, bool syslog, char *buf, size_t size)
1268
+/*
1269
+ * Prepare the record for printing. The text is shifted within the given
1270
+ * buffer to avoid a need for another one. The following operations are
1271
+ * done:
1272
+ *
1273
+ * - Add prefix for each line.
1274
+ * - Drop truncated lines that no longer fit into the buffer.
1275
+ * - Add the trailing newline that has been removed in vprintk_store().
1276
+ * - Add a string terminator.
1277
+ *
1278
+ * Since the produced string is always terminated, the maximum possible
1279
+ * return value is @r->text_buf_size - 1;
1280
+ *
1281
+ * Return: The length of the updated/prepared text, including the added
1282
+ * prefixes and the newline. The terminator is not counted. The dropped
1283
+ * line(s) are not counted.
1284
+ */
1285
+static size_t record_print_text(struct printk_record *r, bool syslog,
1286
+ bool time)
14131287 {
1414
- const char *text = log_text(msg);
1415
- size_t text_size = msg->text_len;
1288
+ size_t text_len = r->info->text_len;
1289
+ size_t buf_size = r->text_buf_size;
1290
+ char *text = r->text_buf;
1291
+ char prefix[PREFIX_MAX];
1292
+ bool truncated = false;
1293
+ size_t prefix_len;
1294
+ size_t line_len;
14161295 size_t len = 0;
1296
+ char *next;
14171297
1418
- do {
1419
- const char *next = memchr(text, '\n', text_size);
1420
- size_t text_len;
1298
+ /*
1299
+ * If the message was truncated because the buffer was not large
1300
+ * enough, treat the available text as if it were the full text.
1301
+ */
1302
+ if (text_len > buf_size)
1303
+ text_len = buf_size;
14211304
1305
+ prefix_len = info_print_prefix(r->info, syslog, time, prefix);
1306
+
1307
+ /*
1308
+ * @text_len: bytes of unprocessed text
1309
+ * @line_len: bytes of current line _without_ newline
1310
+ * @text: pointer to beginning of current line
1311
+ * @len: number of bytes prepared in r->text_buf
1312
+ */
1313
+ for (;;) {
1314
+ next = memchr(text, '\n', text_len);
14221315 if (next) {
1423
- text_len = next - text;
1424
- next++;
1425
- text_size -= next - text;
1316
+ line_len = next - text;
14261317 } else {
1427
- text_len = text_size;
1318
+ /* Drop truncated line(s). */
1319
+ if (truncated)
1320
+ break;
1321
+ line_len = text_len;
14281322 }
14291323
1430
- if (buf) {
1431
- if (print_prefix(msg, syslog, NULL) +
1432
- text_len + 1 >= size - len)
1324
+ /*
1325
+ * Truncate the text if there is not enough space to add the
1326
+ * prefix and a trailing newline and a terminator.
1327
+ */
1328
+ if (len + prefix_len + text_len + 1 + 1 > buf_size) {
1329
+ /* Drop even the current line if no space. */
1330
+ if (len + prefix_len + line_len + 1 + 1 > buf_size)
14331331 break;
14341332
1435
- len += print_prefix(msg, syslog, buf + len);
1436
- memcpy(buf + len, text, text_len);
1437
- len += text_len;
1438
- buf[len++] = '\n';
1439
- } else {
1440
- /* SYSLOG_ACTION_* buffer size only calculation */
1441
- len += print_prefix(msg, syslog, NULL);
1442
- len += text_len;
1443
- len++;
1333
+ text_len = buf_size - len - prefix_len - 1 - 1;
1334
+ truncated = true;
14441335 }
14451336
1446
- text = next;
1447
- } while (text);
1337
+ memmove(text + prefix_len, text, text_len);
1338
+ memcpy(text, prefix, prefix_len);
1339
+
1340
+ /*
1341
+ * Increment the prepared length to include the text and
1342
+ * prefix that were just moved+copied. Also increment for the
1343
+ * newline at the end of this line. If this is the last line,
1344
+ * there is no newline, but it will be added immediately below.
1345
+ */
1346
+ len += prefix_len + line_len + 1;
1347
+ if (text_len == line_len) {
1348
+ /*
1349
+ * This is the last line. Add the trailing newline
1350
+ * removed in vprintk_store().
1351
+ */
1352
+ text[prefix_len + line_len] = '\n';
1353
+ break;
1354
+ }
1355
+
1356
+ /*
1357
+ * Advance beyond the added prefix and the related line with
1358
+ * its newline.
1359
+ */
1360
+ text += prefix_len + line_len + 1;
1361
+
1362
+ /*
1363
+ * The remaining text has only decreased by the line with its
1364
+ * newline.
1365
+ *
1366
+ * Note that @text_len can become zero. It happens when @text
1367
+ * ended with a newline (either due to truncation or the
1368
+ * original string ending with "\n\n"). The loop is correctly
1369
+ * repeated and (if not truncated) an empty line with a prefix
1370
+ * will be prepared.
1371
+ */
1372
+ text_len -= line_len + 1;
1373
+ }
1374
+
1375
+ /*
1376
+ * If a buffer was provided, it will be terminated. Space for the
1377
+ * string terminator is guaranteed to be available. The terminator is
1378
+ * not counted in the return value.
1379
+ */
1380
+ if (buf_size > 0)
1381
+ r->text_buf[len] = 0;
14481382
14491383 return len;
1384
+}
1385
+
1386
+static size_t get_record_print_text_size(struct printk_info *info,
1387
+ unsigned int line_count,
1388
+ bool syslog, bool time)
1389
+{
1390
+ char prefix[PREFIX_MAX];
1391
+ size_t prefix_len;
1392
+
1393
+ prefix_len = info_print_prefix(info, syslog, time, prefix);
1394
+
1395
+ /*
1396
+ * Each line will be preceded with a prefix. The intermediate
1397
+ * newlines are already within the text, but a final trailing
1398
+ * newline will be added.
1399
+ */
1400
+ return ((prefix_len * line_count) + info->text_len + 1);
1401
+}
1402
+
1403
+/*
1404
+ * Beginning with @start_seq, find the first record where it and all following
1405
+ * records up to (but not including) @max_seq fit into @size.
1406
+ *
1407
+ * @max_seq is simply an upper bound and does not need to exist. If the caller
1408
+ * does not require an upper bound, -1 can be used for @max_seq.
1409
+ */
1410
+static u64 find_first_fitting_seq(u64 start_seq, u64 max_seq, size_t size,
1411
+ bool syslog, bool time)
1412
+{
1413
+ struct printk_info info;
1414
+ unsigned int line_count;
1415
+ size_t len = 0;
1416
+ u64 seq;
1417
+
1418
+ /* Determine the size of the records up to @max_seq. */
1419
+ prb_for_each_info(start_seq, prb, seq, &info, &line_count) {
1420
+ if (info.seq >= max_seq)
1421
+ break;
1422
+ len += get_record_print_text_size(&info, line_count, syslog, time);
1423
+ }
1424
+
1425
+ /*
1426
+ * Adjust the upper bound for the next loop to avoid subtracting
1427
+ * lengths that were never added.
1428
+ */
1429
+ if (seq < max_seq)
1430
+ max_seq = seq;
1431
+
1432
+ /*
1433
+ * Move first record forward until length fits into the buffer. Ignore
1434
+ * newest messages that were not counted in the above cycle. Messages
1435
+ * might appear and get lost in the meantime. This is a best effort
1436
+ * that prevents an infinite loop that could occur with a retry.
1437
+ */
1438
+ prb_for_each_info(start_seq, prb, seq, &info, &line_count) {
1439
+ if (len <= size || info.seq >= max_seq)
1440
+ break;
1441
+ len -= get_record_print_text_size(&info, line_count, syslog, time);
1442
+ }
1443
+
1444
+ return seq;
14501445 }
14511446
14521447 static int syslog_print(char __user *buf, int size)
14531448 {
1449
+ struct printk_info info;
1450
+ struct printk_record r;
14541451 char *text;
1455
- struct printk_log *msg;
14561452 int len = 0;
14571453
1458
- text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
1454
+ text = kmalloc(CONSOLE_LOG_MAX, GFP_KERNEL);
14591455 if (!text)
14601456 return -ENOMEM;
1457
+
1458
+ prb_rec_init_rd(&r, &info, text, CONSOLE_LOG_MAX);
14611459
14621460 while (size > 0) {
14631461 size_t n;
14641462 size_t skip;
14651463
1466
- logbuf_lock_irq();
1467
- if (syslog_seq < log_first_seq) {
1468
- /* messages are gone, move to first one */
1469
- syslog_seq = log_first_seq;
1470
- syslog_idx = log_first_idx;
1471
- syslog_partial = 0;
1472
- }
1473
- if (syslog_seq == log_next_seq) {
1474
- logbuf_unlock_irq();
1464
+ spin_lock_irq(&syslog_lock);
1465
+ if (!prb_read_valid(prb, syslog_seq, &r)) {
1466
+ spin_unlock_irq(&syslog_lock);
14751467 break;
14761468 }
1469
+ if (r.info->seq != syslog_seq) {
1470
+ /* message is gone, move to next valid one */
1471
+ syslog_seq = r.info->seq;
1472
+ syslog_partial = 0;
1473
+ }
1474
+
1475
+ /*
1476
+ * To keep reading/counting partial line consistent,
1477
+ * use printk_time value as of the beginning of a line.
1478
+ */
1479
+ if (!syslog_partial)
1480
+ syslog_time = printk_time;
14771481
14781482 skip = syslog_partial;
1479
- msg = log_from_idx(syslog_idx);
1480
- n = msg_print_text(msg, true, text, LOG_LINE_MAX + PREFIX_MAX);
1483
+ n = record_print_text(&r, true, syslog_time);
14811484 if (n - syslog_partial <= size) {
14821485 /* message fits into buffer, move forward */
1483
- syslog_idx = log_next(syslog_idx);
1484
- syslog_seq++;
1486
+ syslog_seq = r.info->seq + 1;
14851487 n -= syslog_partial;
14861488 syslog_partial = 0;
14871489 } else if (!len){
....@@ -1490,7 +1492,7 @@
14901492 syslog_partial += n;
14911493 } else
14921494 n = 0;
1493
- logbuf_unlock_irq();
1495
+ spin_unlock_irq(&syslog_lock);
14941496
14951497 if (!n)
14961498 break;
....@@ -1512,78 +1514,52 @@
15121514
15131515 static int syslog_print_all(char __user *buf, int size, bool clear)
15141516 {
1517
+ struct printk_info info;
1518
+ struct printk_record r;
15151519 char *text;
15161520 int len = 0;
1517
- u64 next_seq;
15181521 u64 seq;
1519
- u32 idx;
1522
+ bool time;
15201523
1521
- text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
1524
+ text = kmalloc(CONSOLE_LOG_MAX, GFP_KERNEL);
15221525 if (!text)
15231526 return -ENOMEM;
15241527
1525
- logbuf_lock_irq();
1528
+ time = printk_time;
15261529 /*
15271530 * Find first record that fits, including all following records,
15281531 * into the user-provided buffer for this dump.
15291532 */
1530
- seq = clear_seq;
1531
- idx = clear_idx;
1532
- while (seq < log_next_seq) {
1533
- struct printk_log *msg = log_from_idx(idx);
1533
+ seq = find_first_fitting_seq(latched_seq_read_nolock(&clear_seq), -1,
1534
+ size, true, time);
15341535
1535
- len += msg_print_text(msg, true, NULL, 0);
1536
- idx = log_next(idx);
1537
- seq++;
1538
- }
1539
-
1540
- /* move first record forward until length fits into the buffer */
1541
- seq = clear_seq;
1542
- idx = clear_idx;
1543
- while (len > size && seq < log_next_seq) {
1544
- struct printk_log *msg = log_from_idx(idx);
1545
-
1546
- len -= msg_print_text(msg, true, NULL, 0);
1547
- idx = log_next(idx);
1548
- seq++;
1549
- }
1550
-
1551
- /* last message fitting into this dump */
1552
- next_seq = log_next_seq;
1536
+ prb_rec_init_rd(&r, &info, text, CONSOLE_LOG_MAX);
15531537
15541538 len = 0;
1555
- while (len >= 0 && seq < next_seq) {
1556
- struct printk_log *msg = log_from_idx(idx);
1539
+ prb_for_each_record(seq, prb, seq, &r) {
15571540 int textlen;
15581541
1559
- textlen = msg_print_text(msg, true, text,
1560
- LOG_LINE_MAX + PREFIX_MAX);
1561
- if (textlen < 0) {
1562
- len = textlen;
1542
+ textlen = record_print_text(&r, true, time);
1543
+
1544
+ if (len + textlen > size) {
1545
+ seq--;
15631546 break;
15641547 }
1565
- idx = log_next(idx);
1566
- seq++;
15671548
1568
- logbuf_unlock_irq();
15691549 if (copy_to_user(buf + len, text, textlen))
15701550 len = -EFAULT;
15711551 else
15721552 len += textlen;
1573
- logbuf_lock_irq();
15741553
1575
- if (seq < log_first_seq) {
1576
- /* messages are gone, move to next one */
1577
- seq = log_first_seq;
1578
- idx = log_first_idx;
1579
- }
1554
+ if (len < 0)
1555
+ break;
15801556 }
15811557
15821558 if (clear) {
1583
- clear_seq = log_next_seq;
1584
- clear_idx = log_next_idx;
1559
+ spin_lock_irq(&syslog_lock);
1560
+ latched_seq_write(&clear_seq, seq);
1561
+ spin_unlock_irq(&syslog_lock);
15851562 }
1586
- logbuf_unlock_irq();
15871563
15881564 kfree(text);
15891565 return len;
....@@ -1591,14 +1567,26 @@
15911567
15921568 static void syslog_clear(void)
15931569 {
1594
- logbuf_lock_irq();
1595
- clear_seq = log_next_seq;
1596
- clear_idx = log_next_idx;
1597
- logbuf_unlock_irq();
1570
+ spin_lock_irq(&syslog_lock);
1571
+ latched_seq_write(&clear_seq, prb_next_seq(prb));
1572
+ spin_unlock_irq(&syslog_lock);
1573
+}
1574
+
1575
+/* Return a consistent copy of @syslog_seq. */
1576
+static u64 read_syslog_seq_irq(void)
1577
+{
1578
+ u64 seq;
1579
+
1580
+ spin_lock_irq(&syslog_lock);
1581
+ seq = syslog_seq;
1582
+ spin_unlock_irq(&syslog_lock);
1583
+
1584
+ return seq;
15981585 }
15991586
16001587 int do_syslog(int type, char __user *buf, int len, int source)
16011588 {
1589
+ struct printk_info info;
16021590 bool clear = false;
16031591 static int saved_console_loglevel = LOGLEVEL_DEFAULT;
16041592 int error;
....@@ -1617,10 +1605,11 @@
16171605 return -EINVAL;
16181606 if (!len)
16191607 return 0;
1620
- if (!access_ok(VERIFY_WRITE, buf, len))
1608
+ if (!access_ok(buf, len))
16211609 return -EFAULT;
1610
+
16221611 error = wait_event_interruptible(log_wait,
1623
- syslog_seq != log_next_seq);
1612
+ prb_read_valid(prb, read_syslog_seq_irq(), NULL));
16241613 if (error)
16251614 return error;
16261615 error = syslog_print(buf, len);
....@@ -1628,14 +1617,14 @@
16281617 /* Read/clear last kernel messages */
16291618 case SYSLOG_ACTION_READ_CLEAR:
16301619 clear = true;
1631
- /* FALL THRU */
1620
+ fallthrough;
16321621 /* Read last kernel messages */
16331622 case SYSLOG_ACTION_READ_ALL:
16341623 if (!buf || len < 0)
16351624 return -EINVAL;
16361625 if (!len)
16371626 return 0;
1638
- if (!access_ok(VERIFY_WRITE, buf, len))
1627
+ if (!access_ok(buf, len))
16391628 return -EFAULT;
16401629 error = syslog_print_all(buf, len, clear);
16411630 break;
....@@ -1668,11 +1657,15 @@
16681657 break;
16691658 /* Number of chars in the log buffer */
16701659 case SYSLOG_ACTION_SIZE_UNREAD:
1671
- logbuf_lock_irq();
1672
- if (syslog_seq < log_first_seq) {
1660
+ spin_lock_irq(&syslog_lock);
1661
+ if (!prb_read_valid_info(prb, syslog_seq, &info, NULL)) {
1662
+ /* No unread messages. */
1663
+ spin_unlock_irq(&syslog_lock);
1664
+ return 0;
1665
+ }
1666
+ if (info.seq != syslog_seq) {
16731667 /* messages are gone, move to first one */
1674
- syslog_seq = log_first_seq;
1675
- syslog_idx = log_first_idx;
1668
+ syslog_seq = info.seq;
16761669 syslog_partial = 0;
16771670 }
16781671 if (source == SYSLOG_FROM_PROC) {
....@@ -1681,21 +1674,21 @@
16811674 * for pending data, not the size; return the count of
16821675 * records, not the length.
16831676 */
1684
- error = log_next_seq - syslog_seq;
1677
+ error = prb_next_seq(prb) - syslog_seq;
16851678 } else {
1686
- u64 seq = syslog_seq;
1687
- u32 idx = syslog_idx;
1679
+ bool time = syslog_partial ? syslog_time : printk_time;
1680
+ unsigned int line_count;
1681
+ u64 seq;
16881682
1689
- while (seq < log_next_seq) {
1690
- struct printk_log *msg = log_from_idx(idx);
1691
-
1692
- error += msg_print_text(msg, true, NULL, 0);
1693
- idx = log_next(idx);
1694
- seq++;
1683
+ prb_for_each_info(syslog_seq, prb, seq, &info,
1684
+ &line_count) {
1685
+ error += get_record_print_text_size(&info, line_count,
1686
+ true, time);
1687
+ time = printk_time;
16951688 }
16961689 error -= syslog_partial;
16971690 }
1698
- logbuf_unlock_irq();
1691
+ spin_unlock_irq(&syslog_lock);
16991692 break;
17001693 /* Size of the log buffer */
17011694 case SYSLOG_ACTION_SIZE_BUFFER:
....@@ -1714,185 +1707,12 @@
17141707 return do_syslog(type, buf, len, SYSLOG_FROM_READER);
17151708 }
17161709
1717
-/*
1718
- * Special console_lock variants that help to reduce the risk of soft-lockups.
1719
- * They allow to pass console_lock to another printk() call using a busy wait.
1720
- */
1721
-
1722
-#ifdef CONFIG_LOCKDEP
1723
-static struct lockdep_map console_owner_dep_map = {
1724
- .name = "console_owner"
1725
-};
1726
-#endif
1727
-
1728
-static DEFINE_RAW_SPINLOCK(console_owner_lock);
1729
-static struct task_struct *console_owner;
1730
-static bool console_waiter;
1731
-
1732
-/**
1733
- * console_lock_spinning_enable - mark beginning of code where another
1734
- * thread might safely busy wait
1735
- *
1736
- * This basically converts console_lock into a spinlock. This marks
1737
- * the section where the console_lock owner can not sleep, because
1738
- * there may be a waiter spinning (like a spinlock). Also it must be
1739
- * ready to hand over the lock at the end of the section.
1740
- */
1741
-static void console_lock_spinning_enable(void)
1742
-{
1743
- raw_spin_lock(&console_owner_lock);
1744
- console_owner = current;
1745
- raw_spin_unlock(&console_owner_lock);
1746
-
1747
- /* The waiter may spin on us after setting console_owner */
1748
- spin_acquire(&console_owner_dep_map, 0, 0, _THIS_IP_);
1749
-}
1750
-
1751
-/**
1752
- * console_lock_spinning_disable_and_check - mark end of code where another
1753
- * thread was able to busy wait and check if there is a waiter
1754
- *
1755
- * This is called at the end of the section where spinning is allowed.
1756
- * It has two functions. First, it is a signal that it is no longer
1757
- * safe to start busy waiting for the lock. Second, it checks if
1758
- * there is a busy waiter and passes the lock rights to her.
1759
- *
1760
- * Important: Callers lose the lock if there was a busy waiter.
1761
- * They must not touch items synchronized by console_lock
1762
- * in this case.
1763
- *
1764
- * Return: 1 if the lock rights were passed, 0 otherwise.
1765
- */
1766
-static int console_lock_spinning_disable_and_check(void)
1767
-{
1768
- int waiter;
1769
-
1770
- raw_spin_lock(&console_owner_lock);
1771
- waiter = READ_ONCE(console_waiter);
1772
- console_owner = NULL;
1773
- raw_spin_unlock(&console_owner_lock);
1774
-
1775
- if (!waiter) {
1776
- spin_release(&console_owner_dep_map, 1, _THIS_IP_);
1777
- return 0;
1778
- }
1779
-
1780
- /* The waiter is now free to continue */
1781
- WRITE_ONCE(console_waiter, false);
1782
-
1783
- spin_release(&console_owner_dep_map, 1, _THIS_IP_);
1784
-
1785
- /*
1786
- * Hand off console_lock to waiter. The waiter will perform
1787
- * the up(). After this, the waiter is the console_lock owner.
1788
- */
1789
- mutex_release(&console_lock_dep_map, 1, _THIS_IP_);
1790
- return 1;
1791
-}
1792
-
1793
-/**
1794
- * console_trylock_spinning - try to get console_lock by busy waiting
1795
- *
1796
- * This allows to busy wait for the console_lock when the current
1797
- * owner is running in specially marked sections. It means that
1798
- * the current owner is running and cannot reschedule until it
1799
- * is ready to lose the lock.
1800
- *
1801
- * Return: 1 if we got the lock, 0 othrewise
1802
- */
1803
-static int console_trylock_spinning(void)
1804
-{
1805
- struct task_struct *owner = NULL;
1806
- bool waiter;
1807
- bool spin = false;
1808
- unsigned long flags;
1809
-
1810
- if (console_trylock())
1811
- return 1;
1812
-
1813
- printk_safe_enter_irqsave(flags);
1814
-
1815
- raw_spin_lock(&console_owner_lock);
1816
- owner = READ_ONCE(console_owner);
1817
- waiter = READ_ONCE(console_waiter);
1818
- if (!waiter && owner && owner != current) {
1819
- WRITE_ONCE(console_waiter, true);
1820
- spin = true;
1821
- }
1822
- raw_spin_unlock(&console_owner_lock);
1823
-
1824
- /*
1825
- * If there is an active printk() writing to the
1826
- * consoles, instead of having it write our data too,
1827
- * see if we can offload that load from the active
1828
- * printer, and do some printing ourselves.
1829
- * Go into a spin only if there isn't already a waiter
1830
- * spinning, and there is an active printer, and
1831
- * that active printer isn't us (recursive printk?).
1832
- */
1833
- if (!spin) {
1834
- printk_safe_exit_irqrestore(flags);
1835
- return 0;
1836
- }
1837
-
1838
- /* We spin waiting for the owner to release us */
1839
- spin_acquire(&console_owner_dep_map, 0, 0, _THIS_IP_);
1840
- /* Owner will clear console_waiter on hand off */
1841
- while (READ_ONCE(console_waiter))
1842
- cpu_relax();
1843
- spin_release(&console_owner_dep_map, 1, _THIS_IP_);
1844
-
1845
- printk_safe_exit_irqrestore(flags);
1846
- /*
1847
- * The owner passed the console lock to us.
1848
- * Since we did not spin on console lock, annotate
1849
- * this as a trylock. Otherwise lockdep will
1850
- * complain.
1851
- */
1852
- mutex_acquire(&console_lock_dep_map, 0, 1, _THIS_IP_);
1853
-
1854
- return 1;
1855
-}
1856
-
1857
-/*
1858
- * Call the console drivers, asking them to write out
1859
- * log_buf[start] to log_buf[end - 1].
1860
- * The console_lock must be held.
1861
- */
1862
-#ifdef CONFIG_PSTORE_CONSOLE_FORCE
1863
-__maybe_unused
1864
-#endif
1865
-static void call_console_drivers(const char *ext_text, size_t ext_len,
1866
- const char *text, size_t len)
1867
-{
1868
- struct console *con;
1869
-
1870
- trace_console_rcuidle(text, len);
1871
-
1872
- if (!console_drivers)
1873
- return;
1874
-
1875
- for_each_console(con) {
1876
- if (exclusive_console && con != exclusive_console)
1877
- continue;
1878
- if (!(con->flags & CON_ENABLED))
1879
- continue;
1880
- if (!con->write)
1881
- continue;
1882
- if (!cpu_online(smp_processor_id()) &&
1883
- !(con->flags & CON_ANYTIME))
1884
- continue;
1885
- if (con->flags & CON_EXTENDED)
1886
- con->write(con, ext_text, ext_len);
1887
- else
1888
- con->write(con, text, len);
1889
- }
1890
-}
1891
-
18921710 int printk_delay_msec __read_mostly;
18931711
1894
-static inline void printk_delay(void)
1712
+static inline void printk_delay(int level)
18951713 {
1714
+ boot_delay_msec(level);
1715
+
18961716 if (unlikely(printk_delay_msec)) {
18971717 int m = printk_delay_msec;
18981718
....@@ -1903,236 +1723,405 @@
19031723 }
19041724 }
19051725
1906
-/*
1907
- * Continuation lines are buffered, and not committed to the record buffer
1908
- * until the line is complete, or a race forces it. The line fragments
1909
- * though, are printed immediately to the consoles to ensure everything has
1910
- * reached the console in case of a kernel crash.
1911
- */
1912
-static struct cont {
1913
- char buf[LOG_LINE_MAX];
1914
- size_t len; /* length == 0 means unused buffer */
1915
- struct task_struct *owner; /* task of first print*/
1916
- u64 ts_nsec; /* time of first print */
1917
- u8 level; /* log level of first message */
1918
- u8 facility; /* log facility of first message */
1919
- enum log_flags flags; /* prefix, newline flags */
1920
-} cont;
1921
-
1922
-static void cont_flush(void)
1726
+static bool kernel_sync_mode(void)
19231727 {
1924
- if (cont.len == 0)
1925
- return;
1926
-
1927
- log_store(cont.facility, cont.level, cont.flags, cont.ts_nsec,
1928
- NULL, 0, cont.buf, cont.len);
1929
- cont.len = 0;
1728
+ return (oops_in_progress || sync_mode);
19301729 }
19311730
1932
-static bool cont_add(int facility, int level, enum log_flags flags, const char *text, size_t len)
1731
+static bool console_can_sync(struct console *con)
19331732 {
1934
- /*
1935
- * If ext consoles are present, flush and skip in-kernel
1936
- * continuation. See nr_ext_console_drivers definition. Also, if
1937
- * the line gets too long, split it up in separate records.
1938
- */
1939
- if (nr_ext_console_drivers || cont.len + len > sizeof(cont.buf)) {
1940
- cont_flush();
1733
+ if (!(con->flags & CON_ENABLED))
19411734 return false;
1942
- }
1735
+ if (con->write_atomic && kernel_sync_mode())
1736
+ return true;
1737
+ if (con->write_atomic && (con->flags & CON_HANDOVER) && !con->thread)
1738
+ return true;
1739
+ if (con->write && (con->flags & CON_BOOT) && !con->thread)
1740
+ return true;
1741
+ return false;
1742
+}
19431743
1944
- if (!cont.len) {
1945
- cont.facility = facility;
1946
- cont.level = level;
1947
- cont.owner = current;
1948
- cont.ts_nsec = get_local_clock();
1949
- cont.flags = flags;
1950
- }
1951
-
1952
- memcpy(cont.buf + cont.len, text, len);
1953
- cont.len += len;
1954
-
1955
- // The original flags come from the first line,
1956
- // but later continuations can add a newline.
1957
- if (flags & LOG_NEWLINE) {
1958
- cont.flags |= LOG_NEWLINE;
1959
- cont_flush();
1960
- }
1961
-
1962
- if (cont.len > (sizeof(cont.buf) * 80) / 100)
1963
- cont_flush();
1744
+static bool call_sync_console_driver(struct console *con, const char *text, size_t text_len)
1745
+{
1746
+ if (!(con->flags & CON_ENABLED))
1747
+ return false;
1748
+ if (con->write_atomic && kernel_sync_mode())
1749
+ con->write_atomic(con, text, text_len);
1750
+ else if (con->write_atomic && (con->flags & CON_HANDOVER) && !con->thread)
1751
+ con->write_atomic(con, text, text_len);
1752
+ else if (con->write && (con->flags & CON_BOOT) && !con->thread)
1753
+ con->write(con, text, text_len);
1754
+ else
1755
+ return false;
19641756
19651757 return true;
19661758 }
19671759
1968
-static size_t log_output(int facility, int level, enum log_flags lflags, const char *dict, size_t dictlen, char *text, size_t text_len)
1760
+static bool have_atomic_console(void)
19691761 {
1970
- /*
1971
- * If an earlier line was buffered, and we're a continuation
1972
- * write from the same process, try to add it to the buffer.
1973
- */
1974
- if (cont.len) {
1975
- if (cont.owner == current && (lflags & LOG_CONT)) {
1976
- if (cont_add(facility, level, lflags, text, text_len))
1977
- return text_len;
1978
- }
1979
- /* Otherwise, make sure it's flushed */
1980
- cont_flush();
1762
+ struct console *con;
1763
+
1764
+ for_each_console(con) {
1765
+ if (!(con->flags & CON_ENABLED))
1766
+ continue;
1767
+ if (con->write_atomic)
1768
+ return true;
19811769 }
1982
-
1983
- /* Skip empty continuation lines that couldn't be added - they just flush */
1984
- if (!text_len && (lflags & LOG_CONT))
1985
- return 0;
1986
-
1987
- /* If it doesn't end in a newline, try to buffer the current line */
1988
- if (!(lflags & LOG_NEWLINE)) {
1989
- if (cont_add(facility, level, lflags, text, text_len))
1990
- return text_len;
1991
- }
1992
-
1993
- /* Store it in the record log */
1994
- return log_store(facility, level, lflags, 0, dict, dictlen, text, text_len);
1770
+ return false;
19951771 }
19961772
1997
-/* Must be called under logbuf_lock. */
1998
-int vprintk_store(int facility, int level,
1999
- const char *dict, size_t dictlen,
2000
- const char *fmt, va_list args)
1773
+static bool print_sync(struct console *con, u64 *seq)
20011774 {
2002
- static char textbuf[LOG_LINE_MAX];
2003
- char *text = textbuf;
1775
+ struct printk_info info;
1776
+ struct printk_record r;
20041777 size_t text_len;
2005
- enum log_flags lflags = 0;
20061778
2007
- /*
2008
- * The printf needs to come first; we need the syslog
2009
- * prefix which might be passed-in as a parameter.
2010
- */
2011
- text_len = vscnprintf(text, sizeof(textbuf), fmt, args);
1779
+ prb_rec_init_rd(&r, &info, &con->sync_buf[0], sizeof(con->sync_buf));
20121780
2013
- /* mark and strip a trailing newline */
2014
- if (text_len && text[text_len-1] == '\n') {
2015
- text_len--;
2016
- lflags |= LOG_NEWLINE;
1781
+ if (!prb_read_valid(prb, *seq, &r))
1782
+ return false;
1783
+
1784
+ text_len = record_print_text(&r, console_msg_format & MSG_FORMAT_SYSLOG, printk_time);
1785
+
1786
+ if (!call_sync_console_driver(con, &con->sync_buf[0], text_len))
1787
+ return false;
1788
+
1789
+ *seq = r.info->seq;
1790
+
1791
+ touch_softlockup_watchdog_sync();
1792
+ clocksource_touch_watchdog();
1793
+ rcu_cpu_stall_reset();
1794
+ touch_nmi_watchdog();
1795
+
1796
+ if (text_len)
1797
+ printk_delay(r.info->level);
1798
+
1799
+ return true;
1800
+}
1801
+
1802
+static void print_sync_until(struct console *con, u64 seq)
1803
+{
1804
+ unsigned int flags;
1805
+ u64 printk_seq;
1806
+
1807
+ console_atomic_lock(&flags);
1808
+ for (;;) {
1809
+ printk_seq = atomic64_read(&con->printk_seq);
1810
+ if (printk_seq >= seq)
1811
+ break;
1812
+ if (!print_sync(con, &printk_seq))
1813
+ break;
1814
+ atomic64_set(&con->printk_seq, printk_seq + 1);
1815
+ }
1816
+ console_atomic_unlock(flags);
1817
+}
1818
+
1819
+#ifdef CONFIG_PRINTK_NMI
1820
+#define NUM_RECURSION_CTX 2
1821
+#else
1822
+#define NUM_RECURSION_CTX 1
1823
+#endif
1824
+
1825
+struct printk_recursion {
1826
+ char count[NUM_RECURSION_CTX];
1827
+};
1828
+
1829
+static DEFINE_PER_CPU(struct printk_recursion, percpu_printk_recursion);
1830
+static char printk_recursion_count[NUM_RECURSION_CTX];
1831
+
1832
+static char *printk_recursion_counter(void)
1833
+{
1834
+ struct printk_recursion *rec;
1835
+ char *count;
1836
+
1837
+ if (!printk_percpu_data_ready()) {
1838
+ count = &printk_recursion_count[0];
1839
+ } else {
1840
+ rec = this_cpu_ptr(&percpu_printk_recursion);
1841
+
1842
+ count = &rec->count[0];
20171843 }
20181844
2019
- /* strip kernel syslog prefix and extract log level or control flags */
1845
+#ifdef CONFIG_PRINTK_NMI
1846
+ if (in_nmi())
1847
+ count++;
1848
+#endif
1849
+
1850
+ return count;
1851
+}
1852
+
1853
+static bool printk_enter_irqsave(unsigned long *flags)
1854
+{
1855
+ char *count;
1856
+
1857
+ local_irq_save(*flags);
1858
+ count = printk_recursion_counter();
1859
+ /* Only 1 level of recursion allowed. */
1860
+ if (*count > 1) {
1861
+ local_irq_restore(*flags);
1862
+ return false;
1863
+ }
1864
+ (*count)++;
1865
+
1866
+ return true;
1867
+}
1868
+
1869
+static void printk_exit_irqrestore(unsigned long flags)
1870
+{
1871
+ char *count;
1872
+
1873
+ count = printk_recursion_counter();
1874
+ (*count)--;
1875
+ local_irq_restore(flags);
1876
+}
1877
+
1878
+static inline u32 printk_caller_id(void)
1879
+{
1880
+ return in_task() ? task_pid_nr(current) :
1881
+ 0x80000000 + raw_smp_processor_id();
1882
+}
1883
+
1884
+/**
1885
+ * parse_prefix - Parse level and control flags.
1886
+ *
1887
+ * @text: The terminated text message.
1888
+ * @level: A pointer to the current level value, will be updated.
1889
+ * @lflags: A pointer to the current log flags, will be updated.
1890
+ *
1891
+ * @level may be NULL if the caller is not interested in the parsed value.
1892
+ * Otherwise the variable pointed to by @level must be set to
1893
+ * LOGLEVEL_DEFAULT in order to be updated with the parsed value.
1894
+ *
1895
+ * @lflags may be NULL if the caller is not interested in the parsed value.
1896
+ * Otherwise the variable pointed to by @lflags will be OR'd with the parsed
1897
+ * value.
1898
+ *
1899
+ * Return: The length of the parsed level and control flags.
1900
+ */
1901
+static u16 parse_prefix(char *text, int *level, enum log_flags *lflags)
1902
+{
1903
+ u16 prefix_len = 0;
1904
+ int kern_level;
1905
+
1906
+ while (*text) {
1907
+ kern_level = printk_get_level(text);
1908
+ if (!kern_level)
1909
+ break;
1910
+
1911
+ switch (kern_level) {
1912
+ case '0' ... '7':
1913
+ if (level && *level == LOGLEVEL_DEFAULT)
1914
+ *level = kern_level - '0';
1915
+ break;
1916
+ case 'c': /* KERN_CONT */
1917
+ if (lflags)
1918
+ *lflags |= LOG_CONT;
1919
+ }
1920
+
1921
+ prefix_len += 2;
1922
+ text += 2;
1923
+ }
1924
+
1925
+ return prefix_len;
1926
+}
1927
+
1928
+static u16 printk_sprint(char *text, u16 size, int facility, enum log_flags *lflags,
1929
+ const char *fmt, va_list args)
1930
+{
1931
+ u16 text_len;
1932
+
1933
+ text_len = vscnprintf(text, size, fmt, args);
1934
+
1935
+ /* Mark and strip a trailing newline. */
1936
+ if (text_len && text[text_len - 1] == '\n') {
1937
+ text_len--;
1938
+ *lflags |= LOG_NEWLINE;
1939
+ }
1940
+
1941
+ /* Strip log level and control flags. */
20201942 if (facility == 0) {
2021
- int kern_level;
1943
+ u16 prefix_len;
20221944
2023
- while ((kern_level = printk_get_level(text)) != 0) {
2024
- switch (kern_level) {
2025
- case '0' ... '7':
2026
- if (level == LOGLEVEL_DEFAULT)
2027
- level = kern_level - '0';
2028
- /* fallthrough */
2029
- case 'd': /* KERN_DEFAULT */
2030
- lflags |= LOG_PREFIX;
2031
- break;
2032
- case 'c': /* KERN_CONT */
2033
- lflags |= LOG_CONT;
2034
- }
2035
-
2036
- text_len -= 2;
2037
- text += 2;
1945
+ prefix_len = parse_prefix(text, NULL, NULL);
1946
+ if (prefix_len) {
1947
+ text_len -= prefix_len;
1948
+ memmove(text, text + prefix_len, text_len);
20381949 }
20391950 }
1951
+
1952
+ return text_len;
1953
+}
1954
+
1955
+__printf(4, 0)
1956
+static int vprintk_store(int facility, int level,
1957
+ const struct dev_printk_info *dev_info,
1958
+ const char *fmt, va_list args)
1959
+{
1960
+ const u32 caller_id = printk_caller_id();
1961
+ struct prb_reserved_entry e;
1962
+ enum log_flags lflags = 0;
1963
+ bool final_commit = false;
1964
+ struct printk_record r;
1965
+ unsigned long irqflags;
1966
+ u16 trunc_msg_len = 0;
1967
+ char prefix_buf[8];
1968
+ u16 reserve_size;
1969
+ va_list args2;
1970
+ u16 text_len;
1971
+ int ret = 0;
1972
+ u64 ts_nsec;
1973
+ u64 seq;
1974
+
1975
+ /*
1976
+ * Since the duration of printk() can vary depending on the message
1977
+ * and state of the ringbuffer, grab the timestamp now so that it is
1978
+ * close to the call of printk(). This provides a more deterministic
1979
+ * timestamp with respect to the caller.
1980
+ */
1981
+ ts_nsec = local_clock();
1982
+
1983
+ if (!printk_enter_irqsave(&irqflags))
1984
+ return 0;
1985
+
1986
+ /*
1987
+ * The sprintf needs to come first since the syslog prefix might be
1988
+ * passed in as a parameter. An extra byte must be reserved so that
1989
+ * later the vscnprintf() into the reserved buffer has room for the
1990
+ * terminating '\0', which is not counted by vsnprintf().
1991
+ */
1992
+ va_copy(args2, args);
1993
+ reserve_size = vsnprintf(&prefix_buf[0], sizeof(prefix_buf), fmt, args2) + 1;
1994
+ va_end(args2);
1995
+
1996
+ if (reserve_size > LOG_LINE_MAX)
1997
+ reserve_size = LOG_LINE_MAX;
1998
+
1999
+ /* Extract log level or control flags. */
2000
+ if (facility == 0)
2001
+ parse_prefix(&prefix_buf[0], &level, &lflags);
20402002
20412003 if (level == LOGLEVEL_DEFAULT)
20422004 level = default_message_loglevel;
20432005
2044
- if (dict)
2045
- lflags |= LOG_PREFIX|LOG_NEWLINE;
2006
+ if (dev_info)
2007
+ lflags |= LOG_NEWLINE;
20462008
2047
- return log_output(facility, level, lflags,
2048
- dict, dictlen, text, text_len);
2009
+ if (lflags & LOG_CONT) {
2010
+ prb_rec_init_wr(&r, reserve_size);
2011
+ if (prb_reserve_in_last(&e, prb, &r, caller_id, LOG_LINE_MAX)) {
2012
+ seq = r.info->seq;
2013
+ text_len = printk_sprint(&r.text_buf[r.info->text_len], reserve_size,
2014
+ facility, &lflags, fmt, args);
2015
+ r.info->text_len += text_len;
2016
+
2017
+ if (lflags & LOG_NEWLINE) {
2018
+ r.info->flags |= LOG_NEWLINE;
2019
+ prb_final_commit(&e);
2020
+ final_commit = true;
2021
+ } else {
2022
+ prb_commit(&e);
2023
+ }
2024
+
2025
+ ret = text_len;
2026
+ goto out;
2027
+ }
2028
+ }
2029
+
2030
+ /*
2031
+ * Explicitly initialize the record before every prb_reserve() call.
2032
+ * prb_reserve_in_last() and prb_reserve() purposely invalidate the
2033
+ * structure when they fail.
2034
+ */
2035
+ prb_rec_init_wr(&r, reserve_size);
2036
+ if (!prb_reserve(&e, prb, &r)) {
2037
+ /* truncate the message if it is too long for empty buffer */
2038
+ truncate_msg(&reserve_size, &trunc_msg_len);
2039
+
2040
+ prb_rec_init_wr(&r, reserve_size + trunc_msg_len);
2041
+ if (!prb_reserve(&e, prb, &r))
2042
+ goto out;
2043
+ }
2044
+
2045
+ seq = r.info->seq;
2046
+
2047
+ /* fill message */
2048
+ text_len = printk_sprint(&r.text_buf[0], reserve_size, facility, &lflags, fmt, args);
2049
+ if (trunc_msg_len)
2050
+ memcpy(&r.text_buf[text_len], trunc_msg, trunc_msg_len);
2051
+ r.info->text_len = text_len + trunc_msg_len;
2052
+ r.info->facility = facility;
2053
+ r.info->level = level & 7;
2054
+ r.info->flags = lflags & 0x1f;
2055
+ r.info->ts_nsec = ts_nsec;
2056
+ r.info->caller_id = caller_id;
2057
+ if (dev_info)
2058
+ memcpy(&r.info->dev_info, dev_info, sizeof(r.info->dev_info));
2059
+
2060
+ /* A message without a trailing newline can be continued. */
2061
+ if (!(lflags & LOG_NEWLINE)) {
2062
+ prb_commit(&e);
2063
+ } else {
2064
+ prb_final_commit(&e);
2065
+ final_commit = true;
2066
+ }
2067
+
2068
+ ret = text_len + trunc_msg_len;
2069
+out:
2070
+ /* only the kernel may perform synchronous printing */
2071
+ if (facility == 0 && final_commit) {
2072
+ struct console *con;
2073
+
2074
+ for_each_console(con) {
2075
+ if (console_can_sync(con))
2076
+ print_sync_until(con, seq + 1);
2077
+ }
2078
+ }
2079
+
2080
+ printk_exit_irqrestore(irqflags);
2081
+ return ret;
20492082 }
20502083
20512084 asmlinkage int vprintk_emit(int facility, int level,
2052
- const char *dict, size_t dictlen,
2085
+ const struct dev_printk_info *dev_info,
20532086 const char *fmt, va_list args)
20542087 {
20552088 int printed_len;
2056
- bool in_sched = false, pending_output;
2057
- unsigned long flags;
2058
- u64 curr_log_seq;
20592089
2060
- if (level == LOGLEVEL_SCHED) {
2090
+ /* Suppress unimportant messages after panic happens */
2091
+ if (unlikely(suppress_printk))
2092
+ return 0;
2093
+
2094
+ if (level == LOGLEVEL_SCHED)
20612095 level = LOGLEVEL_DEFAULT;
2062
- in_sched = true;
2063
- }
20642096
2065
- boot_delay_msec(level);
2066
- printk_delay();
2097
+ printed_len = vprintk_store(facility, level, dev_info, fmt, args);
20672098
2068
- /* This stops the holder of console_sem just where we want him */
2069
- logbuf_lock_irqsave(flags);
2070
- curr_log_seq = log_next_seq;
2071
- printed_len = vprintk_store(facility, level, dict, dictlen, fmt, args);
2072
- pending_output = (curr_log_seq != log_next_seq);
2073
- logbuf_unlock_irqrestore(flags);
2074
-
2075
- /* If called from the scheduler, we can not call up(). */
2076
- if (!in_sched && pending_output) {
2077
- /*
2078
- * Disable preemption to avoid being preempted while holding
2079
- * console_sem which would prevent anyone from printing to
2080
- * console
2081
- */
2082
- preempt_disable();
2083
- /*
2084
- * Try to acquire and then immediately release the console
2085
- * semaphore. The release will print out buffers and wake up
2086
- * /dev/kmsg and syslog() users.
2087
- */
2088
- if (console_trylock_spinning())
2089
- console_unlock();
2090
- preempt_enable();
2091
- }
2092
-
2093
- if (pending_output)
2094
- wake_up_klogd();
2099
+ wake_up_klogd();
20952100 return printed_len;
20962101 }
20972102 EXPORT_SYMBOL(vprintk_emit);
2103
+
2104
+__printf(1, 0)
2105
+static int vprintk_default(const char *fmt, va_list args)
2106
+{
2107
+ return vprintk_emit(0, LOGLEVEL_DEFAULT, NULL, fmt, args);
2108
+}
2109
+__printf(1, 0)
2110
+static int vprintk_func(const char *fmt, va_list args)
2111
+{
2112
+#ifdef CONFIG_KGDB_KDB
2113
+ /* Allow to pass printk() to kdb but avoid a recursion. */
2114
+ if (unlikely(kdb_trap_printk && kdb_printf_cpu < 0))
2115
+ return vkdb_printf(KDB_MSGSRC_PRINTK, fmt, args);
2116
+#endif
2117
+ return vprintk_default(fmt, args);
2118
+}
20982119
20992120 asmlinkage int vprintk(const char *fmt, va_list args)
21002121 {
21012122 return vprintk_func(fmt, args);
21022123 }
21032124 EXPORT_SYMBOL(vprintk);
2104
-
2105
-asmlinkage int printk_emit(int facility, int level,
2106
- const char *dict, size_t dictlen,
2107
- const char *fmt, ...)
2108
-{
2109
- va_list args;
2110
- int r;
2111
-
2112
- va_start(args, fmt);
2113
- r = vprintk_emit(facility, level, dict, dictlen, fmt, args);
2114
- va_end(args);
2115
-
2116
- return r;
2117
-}
2118
-EXPORT_SYMBOL(printk_emit);
2119
-
2120
-int vprintk_default(const char *fmt, va_list args)
2121
-{
2122
- int r;
2123
-
2124
-#ifdef CONFIG_KGDB_KDB
2125
- /* Allow to pass printk() to kdb but avoid a recursion. */
2126
- if (unlikely(kdb_trap_printk && kdb_printf_cpu < 0)) {
2127
- r = vkdb_printf(KDB_MSGSRC_PRINTK, fmt, args);
2128
- return r;
2129
- }
2130
-#endif
2131
- r = vprintk_emit(0, LOGLEVEL_DEFAULT, NULL, 0, fmt, args);
2132
-
2133
- return r;
2134
-}
2135
-EXPORT_SYMBOL_GPL(vprintk_default);
21362125
21372126 /**
21382127 * printk - print a kernel message
....@@ -2168,36 +2157,162 @@
21682157 }
21692158 EXPORT_SYMBOL(printk);
21702159
2160
+static int printk_kthread_func(void *data)
2161
+{
2162
+ struct console *con = data;
2163
+ unsigned long dropped = 0;
2164
+ char *dropped_text = NULL;
2165
+ struct printk_info info;
2166
+ struct printk_record r;
2167
+ char *ext_text = NULL;
2168
+ size_t dropped_len;
2169
+ int ret = -ENOMEM;
2170
+ char *text = NULL;
2171
+ char *write_text;
2172
+ u64 printk_seq;
2173
+ size_t len;
2174
+ int error;
2175
+ u64 seq;
2176
+
2177
+ if (con->flags & CON_EXTENDED) {
2178
+ ext_text = kmalloc(CONSOLE_EXT_LOG_MAX, GFP_KERNEL);
2179
+ if (!ext_text)
2180
+ goto out;
2181
+ }
2182
+ text = kmalloc(LOG_LINE_MAX + PREFIX_MAX, GFP_KERNEL);
2183
+ dropped_text = kmalloc(64, GFP_KERNEL);
2184
+ if (!text || !dropped_text)
2185
+ goto out;
2186
+
2187
+ if (con->flags & CON_EXTENDED)
2188
+ write_text = ext_text;
2189
+ else
2190
+ write_text = text;
2191
+
2192
+ seq = atomic64_read(&con->printk_seq);
2193
+
2194
+ prb_rec_init_rd(&r, &info, text, LOG_LINE_MAX + PREFIX_MAX);
2195
+
2196
+ for (;;) {
2197
+ error = wait_event_interruptible(log_wait,
2198
+ prb_read_valid(prb, seq, &r) || kthread_should_stop());
2199
+
2200
+ if (kthread_should_stop())
2201
+ break;
2202
+
2203
+ if (error)
2204
+ continue;
2205
+
2206
+ if (seq != r.info->seq) {
2207
+ dropped += r.info->seq - seq;
2208
+ seq = r.info->seq;
2209
+ }
2210
+
2211
+ seq++;
2212
+
2213
+ if (!(con->flags & CON_ENABLED))
2214
+ continue;
2215
+
2216
+ if (suppress_message_printing(r.info->level))
2217
+ continue;
2218
+
2219
+ if (con->flags & CON_EXTENDED) {
2220
+ len = info_print_ext_header(ext_text,
2221
+ CONSOLE_EXT_LOG_MAX,
2222
+ r.info);
2223
+ len += msg_print_ext_body(ext_text + len,
2224
+ CONSOLE_EXT_LOG_MAX - len,
2225
+ &r.text_buf[0], r.info->text_len,
2226
+ &r.info->dev_info);
2227
+ } else {
2228
+ len = record_print_text(&r,
2229
+ console_msg_format & MSG_FORMAT_SYSLOG,
2230
+ printk_time);
2231
+ }
2232
+
2233
+ printk_seq = atomic64_read(&con->printk_seq);
2234
+
2235
+ console_lock();
2236
+ console_may_schedule = 0;
2237
+
2238
+ if (kernel_sync_mode() && con->write_atomic) {
2239
+ console_unlock();
2240
+ break;
2241
+ }
2242
+
2243
+ if (!(con->flags & CON_EXTENDED) && dropped) {
2244
+ dropped_len = snprintf(dropped_text, 64,
2245
+ "** %lu printk messages dropped **\n",
2246
+ dropped);
2247
+ dropped = 0;
2248
+
2249
+ con->write(con, dropped_text, dropped_len);
2250
+ printk_delay(r.info->level);
2251
+ }
2252
+
2253
+ con->write(con, write_text, len);
2254
+ if (len)
2255
+ printk_delay(r.info->level);
2256
+
2257
+ atomic64_cmpxchg_relaxed(&con->printk_seq, printk_seq, seq);
2258
+
2259
+ console_unlock();
2260
+ }
2261
+out:
2262
+ kfree(dropped_text);
2263
+ kfree(text);
2264
+ kfree(ext_text);
2265
+ pr_info("%sconsole [%s%d]: printing thread stopped\n",
2266
+ (con->flags & CON_BOOT) ? "boot" : "",
2267
+ con->name, con->index);
2268
+ return ret;
2269
+}
2270
+
2271
+/* Must be called within console_lock(). */
2272
+static void start_printk_kthread(struct console *con)
2273
+{
2274
+ /* No need to start a printing thread if the console cannot print. */
2275
+ if (!con->write)
2276
+ return;
2277
+
2278
+ con->thread = kthread_run(printk_kthread_func, con,
2279
+ "pr/%s%d", con->name, con->index);
2280
+ if (IS_ERR(con->thread)) {
2281
+ pr_err("%sconsole [%s%d]: unable to start printing thread\n",
2282
+ (con->flags & CON_BOOT) ? "boot" : "",
2283
+ con->name, con->index);
2284
+ return;
2285
+ }
2286
+ pr_info("%sconsole [%s%d]: printing thread started\n",
2287
+ (con->flags & CON_BOOT) ? "boot" : "",
2288
+ con->name, con->index);
2289
+}
2290
+
2291
+/* protected by console_lock */
2292
+static bool kthreads_started;
2293
+
2294
+/* Must be called within console_lock(). */
2295
+static void console_try_thread(struct console *con)
2296
+{
2297
+ if (kthreads_started) {
2298
+ start_printk_kthread(con);
2299
+ return;
2300
+ }
2301
+
2302
+ /*
2303
+ * The printing threads have not been started yet. If this console
2304
+ * can print synchronously, print all unprinted messages.
2305
+ */
2306
+ if (console_can_sync(con))
2307
+ print_sync_until(con, prb_next_seq(prb));
2308
+}
2309
+
21712310 #else /* CONFIG_PRINTK */
21722311
2173
-#define LOG_LINE_MAX 0
2174
-#define PREFIX_MAX 0
2312
+#define prb_first_valid_seq(rb) 0
2313
+#define prb_next_seq(rb) 0
21752314
2176
-static u64 syslog_seq;
2177
-static u32 syslog_idx;
2178
-static u64 console_seq;
2179
-static u32 console_idx;
2180
-static u64 exclusive_console_stop_seq;
2181
-static u64 log_first_seq;
2182
-static u32 log_first_idx;
2183
-static u64 log_next_seq;
2184
-static char *log_text(const struct printk_log *msg) { return NULL; }
2185
-static char *log_dict(const struct printk_log *msg) { return NULL; }
2186
-static struct printk_log *log_from_idx(u32 idx) { return NULL; }
2187
-static u32 log_next(u32 idx) { return 0; }
2188
-static ssize_t msg_print_ext_header(char *buf, size_t size,
2189
- struct printk_log *msg,
2190
- u64 seq) { return 0; }
2191
-static ssize_t msg_print_ext_body(char *buf, size_t size,
2192
- char *dict, size_t dict_len,
2193
- char *text, size_t text_len) { return 0; }
2194
-static void console_lock_spinning_enable(void) { }
2195
-static int console_lock_spinning_disable_and_check(void) { return 0; }
2196
-static void call_console_drivers(const char *ext_text, size_t ext_len,
2197
- const char *text, size_t len) {}
2198
-static size_t msg_print_text(const struct printk_log *msg,
2199
- bool syslog, char *buf, size_t size) { return 0; }
2200
-static bool suppress_message_printing(int level) { return false; }
2315
+#define console_try_thread(con)
22012316
22022317 #endif /* CONFIG_PRINTK */
22032318
....@@ -2222,7 +2337,7 @@
22222337 #endif
22232338
22242339 static int __add_preferred_console(char *name, int idx, char *options,
2225
- char *brl_options)
2340
+ char *brl_options, bool user_specified)
22262341 {
22272342 struct console_cmdline *c;
22282343 int i;
....@@ -2237,6 +2352,8 @@
22372352 if (strcmp(c->name, name) == 0 && c->index == idx) {
22382353 if (!brl_options)
22392354 preferred_console = i;
2355
+ if (user_specified)
2356
+ c->user_specified = true;
22402357 return 0;
22412358 }
22422359 }
....@@ -2246,6 +2363,7 @@
22462363 preferred_console = i;
22472364 strlcpy(c->name, name, sizeof(c->name));
22482365 c->options = options;
2366
+ c->user_specified = user_specified;
22492367 braille_set_options(c, brl_options);
22502368
22512369 c->index = idx;
....@@ -2278,7 +2396,7 @@
22782396 * for exacly this purpose.
22792397 */
22802398 if (str[0] == 0 || strcmp(str, "null") == 0) {
2281
- __add_preferred_console("ttynull", 0, NULL, NULL);
2399
+ __add_preferred_console("ttynull", 0, NULL, NULL, true);
22822400 return 1;
22832401 }
22842402
....@@ -2310,7 +2428,7 @@
23102428 idx = simple_strtoul(s, NULL, 10);
23112429 *s = 0;
23122430
2313
- __add_preferred_console(buf, idx, options, brl_options);
2431
+ __add_preferred_console(buf, idx, options, brl_options, true);
23142432 console_set_on_cmdline = 1;
23152433 return 1;
23162434 }
....@@ -2331,7 +2449,7 @@
23312449 */
23322450 int add_preferred_console(char *name, int idx, char *options)
23332451 {
2334
- return __add_preferred_console(name, idx, options, NULL);
2452
+ return __add_preferred_console(name, idx, options, NULL, false);
23352453 }
23362454
23372455 bool console_suspend_enabled = true;
....@@ -2383,6 +2501,12 @@
23832501 */
23842502 static int console_cpu_notify(unsigned int cpu)
23852503 {
2504
+ int flag = 0;
2505
+
2506
+ trace_android_vh_printk_hotplug(&flag);
2507
+ if (flag)
2508
+ return 0;
2509
+
23862510 if (!cpuhp_tasks_frozen) {
23872511 /* If trylock fails, someone else is doing the printing */
23882512 if (console_trylock())
....@@ -2439,34 +2563,6 @@
24392563 }
24402564 EXPORT_SYMBOL(is_console_locked);
24412565
2442
-/*
2443
- * Check if we have any console that is capable of printing while cpu is
2444
- * booting or shutting down. Requires console_sem.
2445
- */
2446
-static int have_callable_console(void)
2447
-{
2448
- struct console *con;
2449
-
2450
- for_each_console(con)
2451
- if ((con->flags & CON_ENABLED) &&
2452
- (con->flags & CON_ANYTIME))
2453
- return 1;
2454
-
2455
- return 0;
2456
-}
2457
-
2458
-/*
2459
- * Can we actually use the console at this time on this cpu?
2460
- *
2461
- * Console drivers may assume that per-cpu resources have been allocated. So
2462
- * unless they're explicitly marked as being able to cope (CON_ANYTIME) don't
2463
- * call them until this CPU is officially up.
2464
- */
2465
-static inline int can_use_console(void)
2466
-{
2467
- return cpu_online(raw_smp_processor_id()) || have_callable_console();
2468
-}
2469
-
24702566 /**
24712567 * console_unlock - unlock the console system
24722568 *
....@@ -2483,148 +2579,14 @@
24832579 */
24842580 void console_unlock(void)
24852581 {
2486
- static char ext_text[CONSOLE_EXT_LOG_MAX];
2487
- static char text[LOG_LINE_MAX + PREFIX_MAX];
2488
- unsigned long flags;
2489
- bool do_cond_resched, retry;
2490
-
24912582 if (console_suspended) {
24922583 up_console_sem();
24932584 return;
24942585 }
24952586
2496
- /*
2497
- * Console drivers are called with interrupts disabled, so
2498
- * @console_may_schedule should be cleared before; however, we may
2499
- * end up dumping a lot of lines, for example, if called from
2500
- * console registration path, and should invoke cond_resched()
2501
- * between lines if allowable. Not doing so can cause a very long
2502
- * scheduling stall on a slow console leading to RCU stall and
2503
- * softlockup warnings which exacerbate the issue with more
2504
- * messages practically incapacitating the system.
2505
- *
2506
- * console_trylock() is not able to detect the preemptive
2507
- * context reliably. Therefore the value must be stored before
2508
- * and cleared after the the "again" goto label.
2509
- */
2510
- do_cond_resched = console_may_schedule;
2511
-again:
2512
- console_may_schedule = 0;
2513
-
2514
- /*
2515
- * We released the console_sem lock, so we need to recheck if
2516
- * cpu is online and (if not) is there at least one CON_ANYTIME
2517
- * console.
2518
- */
2519
- if (!can_use_console()) {
2520
- console_locked = 0;
2521
- up_console_sem();
2522
- return;
2523
- }
2524
-
2525
- for (;;) {
2526
- struct printk_log *msg;
2527
- size_t ext_len = 0;
2528
- size_t len;
2529
-
2530
- printk_safe_enter_irqsave(flags);
2531
- raw_spin_lock(&logbuf_lock);
2532
- if (console_seq < log_first_seq) {
2533
- len = sprintf(text,
2534
- "** %llu printk messages dropped **\n",
2535
- log_first_seq - console_seq);
2536
-
2537
- /* messages are gone, move to first one */
2538
- console_seq = log_first_seq;
2539
- console_idx = log_first_idx;
2540
- } else {
2541
- len = 0;
2542
- }
2543
-skip:
2544
- if (console_seq == log_next_seq)
2545
- break;
2546
-
2547
- msg = log_from_idx(console_idx);
2548
- if (suppress_message_printing(msg->level)) {
2549
- /*
2550
- * Skip record we have buffered and already printed
2551
- * directly to the console when we received it, and
2552
- * record that has level above the console loglevel.
2553
- */
2554
- console_idx = log_next(console_idx);
2555
- console_seq++;
2556
- goto skip;
2557
- }
2558
-
2559
- /* Output to all consoles once old messages replayed. */
2560
- if (unlikely(exclusive_console &&
2561
- console_seq >= exclusive_console_stop_seq)) {
2562
- exclusive_console = NULL;
2563
- }
2564
-
2565
- len += msg_print_text(msg,
2566
- console_msg_format & MSG_FORMAT_SYSLOG,
2567
- text + len,
2568
- sizeof(text) - len);
2569
- if (nr_ext_console_drivers) {
2570
- ext_len = msg_print_ext_header(ext_text,
2571
- sizeof(ext_text),
2572
- msg, console_seq);
2573
- ext_len += msg_print_ext_body(ext_text + ext_len,
2574
- sizeof(ext_text) - ext_len,
2575
- log_dict(msg), msg->dict_len,
2576
- log_text(msg), msg->text_len);
2577
- }
2578
- console_idx = log_next(console_idx);
2579
- console_seq++;
2580
- raw_spin_unlock(&logbuf_lock);
2581
-
2582
- /*
2583
- * While actively printing out messages, if another printk()
2584
- * were to occur on another CPU, it may wait for this one to
2585
- * finish. This task can not be preempted if there is a
2586
- * waiter waiting to take over.
2587
- */
2588
- console_lock_spinning_enable();
2589
-
2590
- stop_critical_timings(); /* don't trace print latency */
2591
-#ifdef CONFIG_PSTORE_CONSOLE_FORCE
2592
- call_console_drivers_level(msg->level, ext_text, ext_len, text, len);
2593
-#else
2594
- call_console_drivers(ext_text, ext_len, text, len);
2595
-#endif
2596
- start_critical_timings();
2597
-
2598
- if (console_lock_spinning_disable_and_check()) {
2599
- printk_safe_exit_irqrestore(flags);
2600
- return;
2601
- }
2602
-
2603
- printk_safe_exit_irqrestore(flags);
2604
-
2605
- if (do_cond_resched)
2606
- cond_resched();
2607
- }
2608
-
26092587 console_locked = 0;
26102588
2611
- raw_spin_unlock(&logbuf_lock);
2612
-
26132589 up_console_sem();
2614
-
2615
- /*
2616
- * Someone could have filled up the buffer again, so re-check if there's
2617
- * something to flush. In case we cannot trylock the console_sem again,
2618
- * there's a new owner and the console_unlock() from them will do the
2619
- * flush, no worries.
2620
- */
2621
- raw_spin_lock(&logbuf_lock);
2622
- retry = console_seq != log_next_seq;
2623
- raw_spin_unlock(&logbuf_lock);
2624
- printk_safe_exit_irqrestore(flags);
2625
-
2626
- if (retry && console_trylock())
2627
- goto again;
26282590 }
26292591 EXPORT_SYMBOL(console_unlock);
26302592
....@@ -2668,20 +2630,26 @@
26682630
26692631 /**
26702632 * console_flush_on_panic - flush console content on panic
2633
+ * @mode: flush all messages in buffer or just the pending ones
26712634 *
26722635 * Immediately output all pending messages no matter what.
26732636 */
2674
-void console_flush_on_panic(void)
2637
+void console_flush_on_panic(enum con_flush_mode mode)
26752638 {
2676
- /*
2677
- * If someone else is holding the console lock, trylock will fail
2678
- * and may_schedule may be set. Ignore and proceed to unlock so
2679
- * that messages are flushed out. As this can be called from any
2680
- * context and we don't want to get preempted while flushing,
2681
- * ensure may_schedule is cleared.
2682
- */
2683
- console_trylock();
2639
+ struct console *c;
2640
+ u64 seq;
2641
+
2642
+ if (!console_trylock())
2643
+ return;
2644
+
26842645 console_may_schedule = 0;
2646
+
2647
+ if (mode == CONSOLE_REPLAY_ALL) {
2648
+ seq = prb_first_valid_seq(prb);
2649
+ for_each_console(c)
2650
+ atomic64_set(&c->printk_seq, seq);
2651
+ }
2652
+
26852653 console_unlock();
26862654 }
26872655
....@@ -2739,6 +2707,63 @@
27392707 early_param("keep_bootcon", keep_bootcon_setup);
27402708
27412709 /*
2710
+ * This is called by register_console() to try to match
2711
+ * the newly registered console with any of the ones selected
2712
+ * by either the command line or add_preferred_console() and
2713
+ * setup/enable it.
2714
+ *
2715
+ * Care need to be taken with consoles that are statically
2716
+ * enabled such as netconsole
2717
+ */
2718
+static int try_enable_new_console(struct console *newcon, bool user_specified)
2719
+{
2720
+ struct console_cmdline *c;
2721
+ int i, err;
2722
+
2723
+ for (i = 0, c = console_cmdline;
2724
+ i < MAX_CMDLINECONSOLES && c->name[0];
2725
+ i++, c++) {
2726
+ if (c->user_specified != user_specified)
2727
+ continue;
2728
+ if (!newcon->match ||
2729
+ newcon->match(newcon, c->name, c->index, c->options) != 0) {
2730
+ /* default matching */
2731
+ BUILD_BUG_ON(sizeof(c->name) != sizeof(newcon->name));
2732
+ if (strcmp(c->name, newcon->name) != 0)
2733
+ continue;
2734
+ if (newcon->index >= 0 &&
2735
+ newcon->index != c->index)
2736
+ continue;
2737
+ if (newcon->index < 0)
2738
+ newcon->index = c->index;
2739
+
2740
+ if (_braille_register_console(newcon, c))
2741
+ return 0;
2742
+
2743
+ if (newcon->setup &&
2744
+ (err = newcon->setup(newcon, c->options)) != 0)
2745
+ return err;
2746
+ }
2747
+ newcon->flags |= CON_ENABLED;
2748
+ if (i == preferred_console) {
2749
+ newcon->flags |= CON_CONSDEV;
2750
+ has_preferred_console = true;
2751
+ }
2752
+ return 0;
2753
+ }
2754
+
2755
+ /*
2756
+ * Some consoles, such as pstore and netconsole, can be enabled even
2757
+ * without matching. Accept the pre-enabled consoles only when match()
2758
+ * and setup() had a chance to be called.
2759
+ */
2760
+ if (newcon->flags & CON_ENABLED && c->user_specified == user_specified)
2761
+ return 0;
2762
+
2763
+ return -ENOENT;
2764
+}
2765
+
2766
+/*
27422767 * The console driver calls this routine during kernel initialization
27432768 * to register the console printing procedure with printk() and to
27442769 * print any messages that were printed by the kernel before the
....@@ -2759,25 +2784,20 @@
27592784 */
27602785 void register_console(struct console *newcon)
27612786 {
2762
- int i;
2763
- unsigned long flags;
27642787 struct console *bcon = NULL;
2765
- struct console_cmdline *c;
2766
- static bool has_preferred;
2788
+ int err;
27672789
2768
- if (console_drivers)
2769
- for_each_console(bcon)
2770
- if (WARN(bcon == newcon,
2771
- "console '%s%d' already registered\n",
2772
- bcon->name, bcon->index))
2773
- return;
2790
+ for_each_console(bcon) {
2791
+ if (WARN(bcon == newcon, "console '%s%d' already registered\n",
2792
+ bcon->name, bcon->index))
2793
+ return;
2794
+ }
27742795
27752796 /*
27762797 * before we register a new CON_BOOT console, make sure we don't
27772798 * already have a valid console
27782799 */
2779
- if (console_drivers && newcon->flags & CON_BOOT) {
2780
- /* find the last or real console */
2800
+ if (newcon->flags & CON_BOOT) {
27812801 for_each_console(bcon) {
27822802 if (!(bcon->flags & CON_BOOT)) {
27832803 pr_info("Too late to register bootconsole %s%d\n",
....@@ -2787,18 +2807,20 @@
27872807 }
27882808 }
27892809
2810
+ newcon->thread = NULL;
2811
+
27902812 if (console_drivers && console_drivers->flags & CON_BOOT)
27912813 bcon = console_drivers;
27922814
2793
- if (!has_preferred || bcon || !console_drivers)
2794
- has_preferred = preferred_console >= 0;
2815
+ if (!has_preferred_console || bcon || !console_drivers)
2816
+ has_preferred_console = preferred_console >= 0;
27952817
27962818 /*
27972819 * See if we want to use this console driver. If we
27982820 * didn't select a console we take the first one
27992821 * that registers here.
28002822 */
2801
- if (!has_preferred) {
2823
+ if (!has_preferred_console) {
28022824 if (newcon->index < 0)
28032825 newcon->index = 0;
28042826 if (newcon->setup == NULL ||
....@@ -2806,47 +2828,20 @@
28062828 newcon->flags |= CON_ENABLED;
28072829 if (newcon->device) {
28082830 newcon->flags |= CON_CONSDEV;
2809
- has_preferred = true;
2831
+ has_preferred_console = true;
28102832 }
28112833 }
28122834 }
28132835
2814
- /*
2815
- * See if this console matches one we selected on
2816
- * the command line.
2817
- */
2818
- for (i = 0, c = console_cmdline;
2819
- i < MAX_CMDLINECONSOLES && c->name[0];
2820
- i++, c++) {
2821
- if (!newcon->match ||
2822
- newcon->match(newcon, c->name, c->index, c->options) != 0) {
2823
- /* default matching */
2824
- BUILD_BUG_ON(sizeof(c->name) != sizeof(newcon->name));
2825
- if (strcmp(c->name, newcon->name) != 0)
2826
- continue;
2827
- if (newcon->index >= 0 &&
2828
- newcon->index != c->index)
2829
- continue;
2830
- if (newcon->index < 0)
2831
- newcon->index = c->index;
2836
+ /* See if this console matches one we selected on the command line */
2837
+ err = try_enable_new_console(newcon, true);
28322838
2833
- if (_braille_register_console(newcon, c))
2834
- return;
2839
+ /* If not, try to match against the platform default(s) */
2840
+ if (err == -ENOENT)
2841
+ err = try_enable_new_console(newcon, false);
28352842
2836
- if (newcon->setup &&
2837
- newcon->setup(newcon, c->options) != 0)
2838
- break;
2839
- }
2840
-
2841
- newcon->flags |= CON_ENABLED;
2842
- if (i == preferred_console) {
2843
- newcon->flags |= CON_CONSDEV;
2844
- has_preferred = true;
2845
- }
2846
- break;
2847
- }
2848
-
2849
- if (!(newcon->flags & CON_ENABLED))
2843
+ /* printk() messages are not printed to the Braille console. */
2844
+ if (err || newcon->flags & CON_BRL)
28502845 return;
28512846
28522847 /*
....@@ -2855,8 +2850,10 @@
28552850 * the real console are the same physical device, it's annoying to
28562851 * see the beginning boot messages twice
28572852 */
2858
- if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV))
2853
+ if (bcon && ((newcon->flags & (CON_CONSDEV | CON_BOOT)) == CON_CONSDEV)) {
28592854 newcon->flags &= ~CON_PRINTBUFFER;
2855
+ newcon->flags |= CON_HANDOVER;
2856
+ }
28602857
28612858 /*
28622859 * Put this console in the list - keep the
....@@ -2868,36 +2865,22 @@
28682865 console_drivers = newcon;
28692866 if (newcon->next)
28702867 newcon->next->flags &= ~CON_CONSDEV;
2868
+ /* Ensure this flag is always set for the head of the list */
2869
+ newcon->flags |= CON_CONSDEV;
28712870 } else {
28722871 newcon->next = console_drivers->next;
28732872 console_drivers->next = newcon;
28742873 }
28752874
28762875 if (newcon->flags & CON_EXTENDED)
2877
- if (!nr_ext_console_drivers++)
2878
- pr_info("printk: continuation disabled due to ext consoles, expect more fragments in /dev/kmsg\n");
2876
+ nr_ext_console_drivers++;
28792877
2880
- if (newcon->flags & CON_PRINTBUFFER) {
2881
- /*
2882
- * console_unlock(); will print out the buffered messages
2883
- * for us.
2884
- */
2885
- logbuf_lock_irqsave(flags);
2886
- /*
2887
- * We're about to replay the log buffer. Only do this to the
2888
- * just-registered console to avoid excessive message spam to
2889
- * the already-registered consoles.
2890
- *
2891
- * Set exclusive_console with disabled interrupts to reduce
2892
- * race window with eventual console_flush_on_panic() that
2893
- * ignores console_lock.
2894
- */
2895
- exclusive_console = newcon;
2896
- exclusive_console_stop_seq = console_seq;
2897
- console_seq = syslog_seq;
2898
- console_idx = syslog_idx;
2899
- logbuf_unlock_irqrestore(flags);
2900
- }
2878
+ if (newcon->flags & CON_PRINTBUFFER)
2879
+ atomic64_set(&newcon->printk_seq, 0);
2880
+ else
2881
+ atomic64_set(&newcon->printk_seq, prb_next_seq(prb));
2882
+
2883
+ console_try_thread(newcon);
29012884 console_unlock();
29022885 console_sysfs_notify();
29032886
....@@ -2926,7 +2909,7 @@
29262909
29272910 int unregister_console(struct console *console)
29282911 {
2929
- struct console *a, *b;
2912
+ struct console *con;
29302913 int res;
29312914
29322915 pr_info("%sconsole [%s%d] disabled\n",
....@@ -2934,26 +2917,30 @@
29342917 console->name, console->index);
29352918
29362919 res = _braille_unregister_console(console);
2937
- if (res)
2920
+ if (res < 0)
29382921 return res;
2922
+ if (res > 0)
2923
+ return 0;
29392924
2940
- res = 1;
2925
+ res = -ENODEV;
29412926 console_lock();
29422927 if (console_drivers == console) {
29432928 console_drivers=console->next;
29442929 res = 0;
2945
- } else if (console_drivers) {
2946
- for (a=console_drivers->next, b=console_drivers ;
2947
- a; b=a, a=b->next) {
2948
- if (a == console) {
2949
- b->next = a->next;
2930
+ } else {
2931
+ for_each_console(con) {
2932
+ if (con->next == console) {
2933
+ con->next = console->next;
29502934 res = 0;
29512935 break;
29522936 }
29532937 }
29542938 }
29552939
2956
- if (!res && (console->flags & CON_EXTENDED))
2940
+ if (res)
2941
+ goto out_disable_unlock;
2942
+
2943
+ if (console->flags & CON_EXTENDED)
29572944 nr_ext_console_drivers--;
29582945
29592946 /*
....@@ -2966,6 +2953,19 @@
29662953 console->flags &= ~CON_ENABLED;
29672954 console_unlock();
29682955 console_sysfs_notify();
2956
+
2957
+ if (console->thread && !IS_ERR(console->thread))
2958
+ kthread_stop(console->thread);
2959
+
2960
+ if (console->exit)
2961
+ res = console->exit(console);
2962
+
2963
+ return res;
2964
+
2965
+out_disable_unlock:
2966
+ console->flags &= ~CON_ENABLED;
2967
+ console_unlock();
2968
+
29692969 return res;
29702970 }
29712971 EXPORT_SYMBOL(unregister_console);
....@@ -3039,6 +3039,15 @@
30393039 unregister_console(con);
30403040 }
30413041 }
3042
+
3043
+#ifdef CONFIG_PRINTK
3044
+ console_lock();
3045
+ for_each_console(con)
3046
+ start_printk_kthread(con);
3047
+ kthreads_started = true;
3048
+ console_unlock();
3049
+#endif
3050
+
30423051 ret = cpuhp_setup_state_nocalls(CPUHP_PRINTK_DEAD, "printk:dead", NULL,
30433052 console_cpu_notify);
30443053 WARN_ON(ret < 0);
....@@ -3054,7 +3063,6 @@
30543063 * Delayed printk version, for scheduler-internal messages:
30553064 */
30563065 #define PRINTK_PENDING_WAKEUP 0x01
3057
-#define PRINTK_PENDING_OUTPUT 0x02
30583066
30593067 static DEFINE_PER_CPU(int, printk_pending);
30603068
....@@ -3062,19 +3070,13 @@
30623070 {
30633071 int pending = __this_cpu_xchg(printk_pending, 0);
30643072
3065
- if (pending & PRINTK_PENDING_OUTPUT) {
3066
- /* If trylock fails, someone else is doing the printing */
3067
- if (console_trylock())
3068
- console_unlock();
3069
- }
3070
-
30713073 if (pending & PRINTK_PENDING_WAKEUP)
3072
- wake_up_interruptible(&log_wait);
3074
+ wake_up_interruptible_all(&log_wait);
30733075 }
30743076
30753077 static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) = {
30763078 .func = wake_up_klogd_work_func,
3077
- .flags = IRQ_WORK_LAZY,
3079
+ .flags = ATOMIC_INIT(IRQ_WORK_LAZY),
30783080 };
30793081
30803082 void wake_up_klogd(void)
....@@ -3090,25 +3092,10 @@
30903092 preempt_enable();
30913093 }
30923094
3093
-void defer_console_output(void)
3095
+__printf(1, 0)
3096
+static int vprintk_deferred(const char *fmt, va_list args)
30943097 {
3095
- if (!printk_percpu_data_ready())
3096
- return;
3097
-
3098
- preempt_disable();
3099
- __this_cpu_or(printk_pending, PRINTK_PENDING_OUTPUT);
3100
- irq_work_queue(this_cpu_ptr(&wake_up_klogd_work));
3101
- preempt_enable();
3102
-}
3103
-
3104
-int vprintk_deferred(const char *fmt, va_list args)
3105
-{
3106
- int r;
3107
-
3108
- r = vprintk_emit(0, LOGLEVEL_SCHED, NULL, 0, fmt, args);
3109
- defer_console_output();
3110
-
3111
- return r;
3098
+ return vprintk_emit(0, LOGLEVEL_DEFAULT, NULL, fmt, args);
31123099 }
31133100
31143101 int printk_deferred(const char *fmt, ...)
....@@ -3122,6 +3109,7 @@
31223109
31233110 return r;
31243111 }
3112
+EXPORT_SYMBOL_GPL(printk_deferred);
31253113
31263114 /*
31273115 * printk rate limiting, lifted from the networking subsystem.
....@@ -3220,6 +3208,23 @@
32203208 static bool always_kmsg_dump;
32213209 module_param_named(always_kmsg_dump, always_kmsg_dump, bool, S_IRUGO | S_IWUSR);
32223210
3211
+const char *kmsg_dump_reason_str(enum kmsg_dump_reason reason)
3212
+{
3213
+ switch (reason) {
3214
+ case KMSG_DUMP_PANIC:
3215
+ return "Panic";
3216
+ case KMSG_DUMP_OOPS:
3217
+ return "Oops";
3218
+ case KMSG_DUMP_EMERG:
3219
+ return "Emergency";
3220
+ case KMSG_DUMP_SHUTDOWN:
3221
+ return "Shutdown";
3222
+ default:
3223
+ return "Unknown";
3224
+ }
3225
+}
3226
+EXPORT_SYMBOL_GPL(kmsg_dump_reason_str);
3227
+
32233228 /**
32243229 * kmsg_dump - dump kernel log to kernel message dumpers.
32253230 * @reason: the reason (oops, panic etc) for dumping
....@@ -3230,39 +3235,55 @@
32303235 */
32313236 void kmsg_dump(enum kmsg_dump_reason reason)
32323237 {
3238
+ struct kmsg_dumper_iter iter;
32333239 struct kmsg_dumper *dumper;
3234
- unsigned long flags;
32353240
3236
- if ((reason > KMSG_DUMP_OOPS) && !always_kmsg_dump)
3237
- return;
3241
+ if (!oops_in_progress) {
3242
+ /*
3243
+ * If atomic consoles are available, activate kernel sync mode
3244
+ * to make sure any final messages are visible. The trailing
3245
+ * printk message is important to flush any pending messages.
3246
+ */
3247
+ if (have_atomic_console()) {
3248
+ sync_mode = true;
3249
+ pr_info("enabled sync mode\n");
3250
+ }
3251
+
3252
+ /*
3253
+ * Give the printing threads time to flush, allowing up to
3254
+ * 1s of no printing forward progress before giving up.
3255
+ */
3256
+ pr_flush(1000, true);
3257
+ }
32383258
32393259 rcu_read_lock();
32403260 list_for_each_entry_rcu(dumper, &dump_list, list) {
3241
- if (dumper->max_reason && reason > dumper->max_reason)
3261
+ enum kmsg_dump_reason max_reason = dumper->max_reason;
3262
+
3263
+ /*
3264
+ * If client has not provided a specific max_reason, default
3265
+ * to KMSG_DUMP_OOPS, unless always_kmsg_dump was set.
3266
+ */
3267
+ if (max_reason == KMSG_DUMP_UNDEF) {
3268
+ max_reason = always_kmsg_dump ? KMSG_DUMP_MAX :
3269
+ KMSG_DUMP_OOPS;
3270
+ }
3271
+ if (reason > max_reason)
32423272 continue;
32433273
32443274 /* initialize iterator with data about the stored records */
3245
- dumper->active = true;
3246
-
3247
- logbuf_lock_irqsave(flags);
3248
- dumper->cur_seq = clear_seq;
3249
- dumper->cur_idx = clear_idx;
3250
- dumper->next_seq = log_next_seq;
3251
- dumper->next_idx = log_next_idx;
3252
- logbuf_unlock_irqrestore(flags);
3275
+ iter.active = true;
3276
+ kmsg_dump_rewind(&iter);
32533277
32543278 /* invoke dumper which will iterate over records */
3255
- dumper->dump(dumper, reason);
3256
-
3257
- /* reset iterator */
3258
- dumper->active = false;
3279
+ dumper->dump(dumper, reason, &iter);
32593280 }
32603281 rcu_read_unlock();
32613282 }
32623283
32633284 /**
3264
- * kmsg_dump_get_line_nolock - retrieve one kmsg log line (unlocked version)
3265
- * @dumper: registered kmsg dumper
3285
+ * kmsg_dump_get_line - retrieve one kmsg log line
3286
+ * @iter: kmsg dumper iterator
32663287 * @syslog: include the "<4>" prefixes
32673288 * @line: buffer to copy the line to
32683289 * @size: maximum size of the buffer
....@@ -3276,82 +3297,55 @@
32763297 *
32773298 * A return value of FALSE indicates that there are no more records to
32783299 * read.
3279
- *
3280
- * The function is similar to kmsg_dump_get_line(), but grabs no locks.
32813300 */
3282
-bool kmsg_dump_get_line_nolock(struct kmsg_dumper *dumper, bool syslog,
3283
- char *line, size_t size, size_t *len)
3301
+bool kmsg_dump_get_line(struct kmsg_dumper_iter *iter, bool syslog,
3302
+ char *line, size_t size, size_t *len)
32843303 {
3285
- struct printk_log *msg;
3304
+ struct printk_info info;
3305
+ unsigned int line_count;
3306
+ struct printk_record r;
32863307 size_t l = 0;
32873308 bool ret = false;
32883309
3289
- if (!dumper->active)
3310
+ prb_rec_init_rd(&r, &info, line, size);
3311
+
3312
+ if (!iter->active)
32903313 goto out;
32913314
3292
- if (dumper->cur_seq < log_first_seq) {
3293
- /* messages are gone, move to first available one */
3294
- dumper->cur_seq = log_first_seq;
3295
- dumper->cur_idx = log_first_idx;
3315
+ /* Read text or count text lines? */
3316
+ if (line) {
3317
+ if (!prb_read_valid(prb, iter->cur_seq, &r))
3318
+ goto out;
3319
+ l = record_print_text(&r, syslog, printk_time);
3320
+ } else {
3321
+ if (!prb_read_valid_info(prb, iter->cur_seq,
3322
+ &info, &line_count)) {
3323
+ goto out;
3324
+ }
3325
+ l = get_record_print_text_size(&info, line_count, syslog,
3326
+ printk_time);
3327
+
32963328 }
32973329
3298
- /* last entry */
3299
- if (dumper->cur_seq >= log_next_seq)
3300
- goto out;
3301
-
3302
- msg = log_from_idx(dumper->cur_idx);
3303
- l = msg_print_text(msg, syslog, line, size);
3304
-
3305
- dumper->cur_idx = log_next(dumper->cur_idx);
3306
- dumper->cur_seq++;
3330
+ iter->cur_seq = r.info->seq + 1;
33073331 ret = true;
33083332 out:
33093333 if (len)
33103334 *len = l;
33113335 return ret;
33123336 }
3313
-
3314
-/**
3315
- * kmsg_dump_get_line - retrieve one kmsg log line
3316
- * @dumper: registered kmsg dumper
3317
- * @syslog: include the "<4>" prefixes
3318
- * @line: buffer to copy the line to
3319
- * @size: maximum size of the buffer
3320
- * @len: length of line placed into buffer
3321
- *
3322
- * Start at the beginning of the kmsg buffer, with the oldest kmsg
3323
- * record, and copy one record into the provided buffer.
3324
- *
3325
- * Consecutive calls will return the next available record moving
3326
- * towards the end of the buffer with the youngest messages.
3327
- *
3328
- * A return value of FALSE indicates that there are no more records to
3329
- * read.
3330
- */
3331
-bool kmsg_dump_get_line(struct kmsg_dumper *dumper, bool syslog,
3332
- char *line, size_t size, size_t *len)
3333
-{
3334
- unsigned long flags;
3335
- bool ret;
3336
-
3337
- logbuf_lock_irqsave(flags);
3338
- ret = kmsg_dump_get_line_nolock(dumper, syslog, line, size, len);
3339
- logbuf_unlock_irqrestore(flags);
3340
-
3341
- return ret;
3342
-}
33433337 EXPORT_SYMBOL_GPL(kmsg_dump_get_line);
33443338
33453339 /**
33463340 * kmsg_dump_get_buffer - copy kmsg log lines
3347
- * @dumper: registered kmsg dumper
3341
+ * @iter: kmsg dumper iterator
33483342 * @syslog: include the "<4>" prefixes
33493343 * @buf: buffer to copy the line to
33503344 * @size: maximum size of the buffer
33513345 * @len: length of line placed into buffer
33523346 *
33533347 * Start at the end of the kmsg buffer and fill the provided buffer
3354
- * with as many of the the *youngest* kmsg records that fit into it.
3348
+ * with as many of the *youngest* kmsg records that fit into it.
33553349 * If the buffer is large enough, all available kmsg records will be
33563350 * copied with a single call.
33573351 *
....@@ -3361,113 +3355,258 @@
33613355 * A return value of FALSE indicates that there are no more records to
33623356 * read.
33633357 */
3364
-bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
3365
- char *buf, size_t size, size_t *len)
3358
+bool kmsg_dump_get_buffer(struct kmsg_dumper_iter *iter, bool syslog,
3359
+ char *buf, size_t size, size_t *len_out)
33663360 {
3367
- unsigned long flags;
3361
+ struct printk_info info;
3362
+ struct printk_record r;
33683363 u64 seq;
3369
- u32 idx;
33703364 u64 next_seq;
3371
- u32 next_idx;
3372
- size_t l = 0;
3365
+ size_t len = 0;
33733366 bool ret = false;
3367
+ bool time = printk_time;
33743368
3375
- if (!dumper->active)
3369
+ if (!iter->active || !buf || !size)
33763370 goto out;
33773371
3378
- logbuf_lock_irqsave(flags);
3379
- if (dumper->cur_seq < log_first_seq) {
3380
- /* messages are gone, move to first available one */
3381
- dumper->cur_seq = log_first_seq;
3382
- dumper->cur_idx = log_first_idx;
3372
+ if (prb_read_valid_info(prb, iter->cur_seq, &info, NULL)) {
3373
+ if (info.seq != iter->cur_seq) {
3374
+ /* messages are gone, move to first available one */
3375
+ iter->cur_seq = info.seq;
3376
+ }
33833377 }
33843378
33853379 /* last entry */
3386
- if (dumper->cur_seq >= dumper->next_seq) {
3387
- logbuf_unlock_irqrestore(flags);
3380
+ if (iter->cur_seq >= iter->next_seq)
33883381 goto out;
3389
- }
33903382
3391
- /* calculate length of entire buffer */
3392
- seq = dumper->cur_seq;
3393
- idx = dumper->cur_idx;
3394
- while (seq < dumper->next_seq) {
3395
- struct printk_log *msg = log_from_idx(idx);
3383
+ /*
3384
+ * Find first record that fits, including all following records,
3385
+ * into the user-provided buffer for this dump. Pass in size-1
3386
+ * because this function (by way of record_print_text()) will
3387
+ * not write more than size-1 bytes of text into @buf.
3388
+ */
3389
+ seq = find_first_fitting_seq(iter->cur_seq, iter->next_seq,
3390
+ size - 1, syslog, time);
33963391
3397
- l += msg_print_text(msg, true, NULL, 0);
3398
- idx = log_next(idx);
3399
- seq++;
3400
- }
3401
-
3402
- /* move first record forward until length fits into the buffer */
3403
- seq = dumper->cur_seq;
3404
- idx = dumper->cur_idx;
3405
- while (l >= size && seq < dumper->next_seq) {
3406
- struct printk_log *msg = log_from_idx(idx);
3407
-
3408
- l -= msg_print_text(msg, true, NULL, 0);
3409
- idx = log_next(idx);
3410
- seq++;
3411
- }
3412
-
3413
- /* last message in next interation */
3392
+ /*
3393
+ * Next kmsg_dump_get_buffer() invocation will dump block of
3394
+ * older records stored right before this one.
3395
+ */
34143396 next_seq = seq;
3415
- next_idx = idx;
34163397
3417
- l = 0;
3418
- while (seq < dumper->next_seq) {
3419
- struct printk_log *msg = log_from_idx(idx);
3398
+ prb_rec_init_rd(&r, &info, buf, size);
34203399
3421
- l += msg_print_text(msg, syslog, buf + l, size - l);
3422
- idx = log_next(idx);
3423
- seq++;
3400
+ len = 0;
3401
+ prb_for_each_record(seq, prb, seq, &r) {
3402
+ if (r.info->seq >= iter->next_seq)
3403
+ break;
3404
+
3405
+ len += record_print_text(&r, syslog, time);
3406
+
3407
+ /* Adjust record to store to remaining buffer space. */
3408
+ prb_rec_init_rd(&r, &info, buf + len, size - len);
34243409 }
34253410
3426
- dumper->next_seq = next_seq;
3427
- dumper->next_idx = next_idx;
3411
+ iter->next_seq = next_seq;
34283412 ret = true;
3429
- logbuf_unlock_irqrestore(flags);
34303413 out:
3431
- if (len)
3432
- *len = l;
3414
+ if (len_out)
3415
+ *len_out = len;
34333416 return ret;
34343417 }
34353418 EXPORT_SYMBOL_GPL(kmsg_dump_get_buffer);
34363419
34373420 /**
3438
- * kmsg_dump_rewind_nolock - reset the interator (unlocked version)
3439
- * @dumper: registered kmsg dumper
3440
- *
3441
- * Reset the dumper's iterator so that kmsg_dump_get_line() and
3442
- * kmsg_dump_get_buffer() can be called again and used multiple
3443
- * times within the same dumper.dump() callback.
3444
- *
3445
- * The function is similar to kmsg_dump_rewind(), but grabs no locks.
3446
- */
3447
-void kmsg_dump_rewind_nolock(struct kmsg_dumper *dumper)
3448
-{
3449
- dumper->cur_seq = clear_seq;
3450
- dumper->cur_idx = clear_idx;
3451
- dumper->next_seq = log_next_seq;
3452
- dumper->next_idx = log_next_idx;
3453
-}
3454
-
3455
-/**
3456
- * kmsg_dump_rewind - reset the interator
3457
- * @dumper: registered kmsg dumper
3421
+ * kmsg_dump_rewind - reset the iterator
3422
+ * @iter: kmsg dumper iterator
34583423 *
34593424 * Reset the dumper's iterator so that kmsg_dump_get_line() and
34603425 * kmsg_dump_get_buffer() can be called again and used multiple
34613426 * times within the same dumper.dump() callback.
34623427 */
3463
-void kmsg_dump_rewind(struct kmsg_dumper *dumper)
3428
+void kmsg_dump_rewind(struct kmsg_dumper_iter *iter)
34643429 {
3465
- unsigned long flags;
3466
-
3467
- logbuf_lock_irqsave(flags);
3468
- kmsg_dump_rewind_nolock(dumper);
3469
- logbuf_unlock_irqrestore(flags);
3430
+ iter->cur_seq = latched_seq_read_nolock(&clear_seq);
3431
+ iter->next_seq = prb_next_seq(prb);
34703432 }
34713433 EXPORT_SYMBOL_GPL(kmsg_dump_rewind);
34723434
34733435 #endif
3436
+
3437
+struct prb_cpulock {
3438
+ atomic_t owner;
3439
+ unsigned long __percpu *irqflags;
3440
+};
3441
+
3442
+#define DECLARE_STATIC_PRINTKRB_CPULOCK(name) \
3443
+static DEFINE_PER_CPU(unsigned long, _##name##_percpu_irqflags); \
3444
+static struct prb_cpulock name = { \
3445
+ .owner = ATOMIC_INIT(-1), \
3446
+ .irqflags = &_##name##_percpu_irqflags, \
3447
+}
3448
+
3449
+static bool __prb_trylock(struct prb_cpulock *cpu_lock,
3450
+ unsigned int *cpu_store)
3451
+{
3452
+ unsigned long *flags;
3453
+ unsigned int cpu;
3454
+
3455
+ cpu = get_cpu();
3456
+
3457
+ *cpu_store = atomic_read(&cpu_lock->owner);
3458
+ /* memory barrier to ensure the current lock owner is visible */
3459
+ smp_rmb();
3460
+ if (*cpu_store == -1) {
3461
+ flags = per_cpu_ptr(cpu_lock->irqflags, cpu);
3462
+ local_irq_save(*flags);
3463
+ if (atomic_try_cmpxchg_acquire(&cpu_lock->owner,
3464
+ cpu_store, cpu)) {
3465
+ return true;
3466
+ }
3467
+ local_irq_restore(*flags);
3468
+ } else if (*cpu_store == cpu) {
3469
+ return true;
3470
+ }
3471
+
3472
+ put_cpu();
3473
+ return false;
3474
+}
3475
+
3476
+/*
3477
+ * prb_lock: Perform a processor-reentrant spin lock.
3478
+ * @cpu_lock: A pointer to the lock object.
3479
+ * @cpu_store: A "flags" pointer to store lock status information.
3480
+ *
3481
+ * If no processor has the lock, the calling processor takes the lock and
3482
+ * becomes the owner. If the calling processor is already the owner of the
3483
+ * lock, this function succeeds immediately. If lock is locked by another
3484
+ * processor, this function spins until the calling processor becomes the
3485
+ * owner.
3486
+ *
3487
+ * It is safe to call this function from any context and state.
3488
+ */
3489
+static void prb_lock(struct prb_cpulock *cpu_lock, unsigned int *cpu_store)
3490
+{
3491
+ for (;;) {
3492
+ if (__prb_trylock(cpu_lock, cpu_store))
3493
+ break;
3494
+ cpu_relax();
3495
+ }
3496
+}
3497
+
3498
+/*
3499
+ * prb_unlock: Perform a processor-reentrant spin unlock.
3500
+ * @cpu_lock: A pointer to the lock object.
3501
+ * @cpu_store: A "flags" object storing lock status information.
3502
+ *
3503
+ * Release the lock. The calling processor must be the owner of the lock.
3504
+ *
3505
+ * It is safe to call this function from any context and state.
3506
+ */
3507
+static void prb_unlock(struct prb_cpulock *cpu_lock, unsigned int cpu_store)
3508
+{
3509
+ unsigned long *flags;
3510
+ unsigned int cpu;
3511
+
3512
+ cpu = atomic_read(&cpu_lock->owner);
3513
+ atomic_set_release(&cpu_lock->owner, cpu_store);
3514
+
3515
+ if (cpu_store == -1) {
3516
+ flags = per_cpu_ptr(cpu_lock->irqflags, cpu);
3517
+ local_irq_restore(*flags);
3518
+ }
3519
+
3520
+ put_cpu();
3521
+}
3522
+
3523
+DECLARE_STATIC_PRINTKRB_CPULOCK(printk_cpulock);
3524
+
3525
+void console_atomic_lock(unsigned int *flags)
3526
+{
3527
+ prb_lock(&printk_cpulock, flags);
3528
+}
3529
+EXPORT_SYMBOL(console_atomic_lock);
3530
+
3531
+void console_atomic_unlock(unsigned int flags)
3532
+{
3533
+ prb_unlock(&printk_cpulock, flags);
3534
+}
3535
+EXPORT_SYMBOL(console_atomic_unlock);
3536
+
3537
+static void pr_msleep(bool may_sleep, int ms)
3538
+{
3539
+ if (may_sleep) {
3540
+ msleep(ms);
3541
+ } else {
3542
+ while (ms--)
3543
+ udelay(1000);
3544
+ }
3545
+}
3546
+
3547
+/**
3548
+ * pr_flush() - Wait for printing threads to catch up.
3549
+ *
3550
+ * @timeout_ms: The maximum time (in ms) to wait.
3551
+ * @reset_on_progress: Reset the timeout if forward progress is seen.
3552
+ *
3553
+ * A value of 0 for @timeout_ms means no waiting will occur. A value of -1
3554
+ * represents infinite waiting.
3555
+ *
3556
+ * If @reset_on_progress is true, the timeout will be reset whenever any
3557
+ * printer has been seen to make some forward progress.
3558
+ *
3559
+ * Context: Any context.
3560
+ * Return: true if all enabled printers are caught up.
3561
+ */
3562
+bool pr_flush(int timeout_ms, bool reset_on_progress)
3563
+{
3564
+ int remaining = timeout_ms;
3565
+ struct console *con;
3566
+ u64 last_diff = 0;
3567
+ bool may_sleep;
3568
+ u64 printk_seq;
3569
+ u64 diff;
3570
+ u64 seq;
3571
+
3572
+ may_sleep = (preemptible() &&
3573
+ !in_softirq() &&
3574
+ system_state >= SYSTEM_RUNNING);
3575
+
3576
+ seq = prb_next_seq(prb);
3577
+
3578
+ for (;;) {
3579
+ diff = 0;
3580
+
3581
+ for_each_console(con) {
3582
+ if (!(con->flags & CON_ENABLED))
3583
+ continue;
3584
+ if (!con->write && !con->write_atomic)
3585
+ continue;
3586
+ printk_seq = atomic64_read(&con->printk_seq);
3587
+ if (printk_seq < seq)
3588
+ diff += seq - printk_seq;
3589
+ }
3590
+
3591
+ if (diff != last_diff && reset_on_progress)
3592
+ remaining = timeout_ms;
3593
+
3594
+ if (!diff || remaining == 0)
3595
+ break;
3596
+
3597
+ if (remaining < 0) {
3598
+ pr_msleep(may_sleep, 100);
3599
+ } else if (remaining < 100) {
3600
+ pr_msleep(may_sleep, remaining);
3601
+ remaining = 0;
3602
+ } else {
3603
+ pr_msleep(may_sleep, 100);
3604
+ remaining -= 100;
3605
+ }
3606
+
3607
+ last_diff = diff;
3608
+ }
3609
+
3610
+ return (diff == 0);
3611
+}
3612
+EXPORT_SYMBOL(pr_flush);