Skip to content

Commit de6da1e

Browse files
ftang1torvalds
authored andcommitted
panic: add an option to replay all the printk message in buffer
Currently on panic, kernel will lower the loglevel and print out pending printk msg only with console_flush_on_panic(). Add an option for users to configure the "panic_print" to replay all dmesg in buffer, some of which they may have never seen due to the loglevel setting, which will help panic debugging . [[email protected]: keep the original console_flush_on_panic() inside panic()] Link: http://lkml.kernel.org/r/[email protected] [[email protected]: use logbuf lock to protect the console log index] Link: http://lkml.kernel.org/r/[email protected] Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Feng Tang <[email protected]> Reviewed-by: Petr Mladek <[email protected]> Cc: Aaro Koskinen <[email protected]> Cc: Petr Mladek <[email protected]> Cc: Steven Rostedt <[email protected]> Cc: Sergey Senozhatsky <[email protected]> Cc: Kees Cook <[email protected]> Cc: Borislav Petkov <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 5d59aa8 commit de6da1e

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

Documentation/admin-guide/kernel-parameters.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3212,6 +3212,7 @@
32123212
bit 2: print timer info
32133213
bit 3: print locks info if CONFIG_LOCKDEP is on
32143214
bit 4: print ftrace buffer
3215+
bit 5: print all printk messages in buffer
32153216

32163217
panic_on_warn panic() instead of WARN(). Useful to cause kdump
32173218
on a WARN().

arch/powerpc/kernel/traps.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ extern void panic_flush_kmsg_end(void)
179179
kmsg_dump(KMSG_DUMP_PANIC);
180180
bust_spinlocks(0);
181181
debug_locks_off();
182-
console_flush_on_panic();
182+
console_flush_on_panic(CONSOLE_FLUSH_PENDING);
183183
}
184184

185185
static unsigned long oops_begin(struct pt_regs *regs)

include/linux/console.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ struct console {
166166
extern int console_set_on_cmdline;
167167
extern struct console *early_console;
168168

169+
enum con_flush_mode {
170+
CONSOLE_FLUSH_PENDING,
171+
CONSOLE_REPLAY_ALL,
172+
};
173+
169174
extern int add_preferred_console(char *name, int idx, char *options);
170175
extern void register_console(struct console *);
171176
extern int unregister_console(struct console *);
@@ -175,7 +180,7 @@ extern int console_trylock(void);
175180
extern void console_unlock(void);
176181
extern void console_conditional_schedule(void);
177182
extern void console_unblank(void);
178-
extern void console_flush_on_panic(void);
183+
extern void console_flush_on_panic(enum con_flush_mode mode);
179184
extern struct tty_driver *console_device(int *);
180185
extern void console_stop(struct console *);
181186
extern void console_start(struct console *);

kernel/panic.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ EXPORT_SYMBOL_GPL(panic_timeout);
5151
#define PANIC_PRINT_TIMER_INFO 0x00000004
5252
#define PANIC_PRINT_LOCK_INFO 0x00000008
5353
#define PANIC_PRINT_FTRACE_INFO 0x00000010
54+
#define PANIC_PRINT_ALL_PRINTK_MSG 0x00000020
5455
unsigned long panic_print;
5556

5657
ATOMIC_NOTIFIER_HEAD(panic_notifier_list);
@@ -134,6 +135,9 @@ EXPORT_SYMBOL(nmi_panic);
134135

135136
static void panic_print_sys_info(void)
136137
{
138+
if (panic_print & PANIC_PRINT_ALL_PRINTK_MSG)
139+
console_flush_on_panic(CONSOLE_REPLAY_ALL);
140+
137141
if (panic_print & PANIC_PRINT_TASK_INFO)
138142
show_state();
139143

@@ -277,7 +281,7 @@ void panic(const char *fmt, ...)
277281
* panic() is not being callled from OOPS.
278282
*/
279283
debug_locks_off();
280-
console_flush_on_panic();
284+
console_flush_on_panic(CONSOLE_FLUSH_PENDING);
281285

282286
panic_print_sys_info();
283287

kernel/printk/printk.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2535,10 +2535,11 @@ void console_unblank(void)
25352535

25362536
/**
25372537
* console_flush_on_panic - flush console content on panic
2538+
* @mode: flush all messages in buffer or just the pending ones
25382539
*
25392540
* Immediately output all pending messages no matter what.
25402541
*/
2541-
void console_flush_on_panic(void)
2542+
void console_flush_on_panic(enum con_flush_mode mode)
25422543
{
25432544
/*
25442545
* If someone else is holding the console lock, trylock will fail
@@ -2549,6 +2550,15 @@ void console_flush_on_panic(void)
25492550
*/
25502551
console_trylock();
25512552
console_may_schedule = 0;
2553+
2554+
if (mode == CONSOLE_REPLAY_ALL) {
2555+
unsigned long flags;
2556+
2557+
logbuf_lock_irqsave(flags);
2558+
console_seq = log_first_seq;
2559+
console_idx = log_first_idx;
2560+
logbuf_unlock_irqrestore(flags);
2561+
}
25522562
console_unlock();
25532563
}
25542564

0 commit comments

Comments
 (0)