Skip to content

Commit 5dde3b7

Browse files
jognesspmladek
authored andcommitted
printk: nbcon: Add unsafe flushing on panic
Add nbcon_atomic_flush_unsafe() to flush all nbcon consoles using the write_atomic() callback and allowing unsafe hostile takeovers. Call this at the end of panic() as a final attempt to flush any pending messages. Note that legacy consoles use unsafe methods for flushing from the beginning of panic (see bust_spinlocks()). Therefore, systems using both legacy and nbcon consoles may still fail to see panic messages due to unsafe legacy console usage. Signed-off-by: John Ogness <[email protected]> Reviewed-by: Petr Mladek <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Petr Mladek <[email protected]>
1 parent d2e85ca commit 5dde3b7

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

include/linux/printk.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ void printk_trigger_flush(void);
202202
void console_try_replay_all(void);
203203
extern bool nbcon_device_try_acquire(struct console *con);
204204
extern void nbcon_device_release(struct console *con);
205+
void nbcon_atomic_flush_unsafe(void);
205206
#else
206207
static inline __printf(1, 0)
207208
int vprintk(const char *s, va_list args)
@@ -294,6 +295,10 @@ static inline void nbcon_device_release(struct console *con)
294295
{
295296
}
296297

298+
static inline void nbcon_atomic_flush_unsafe(void)
299+
{
300+
}
301+
297302
#endif
298303

299304
bool this_cpu_in_panic(void);

kernel/panic.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ void panic(const char *fmt, ...)
463463
* Explicitly flush the kernel log buffer one last time.
464464
*/
465465
console_flush_on_panic(CONSOLE_FLUSH_PENDING);
466+
nbcon_atomic_flush_unsafe();
466467

467468
local_irq_enable();
468469
for (i = 0; ; i += PANIC_TIMER_STEP) {

kernel/printk/nbcon.c

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,7 @@ bool nbcon_legacy_emit_next_record(struct console *con, bool *handover,
10831083
* write_atomic() callback
10841084
* @con: The nbcon console to flush
10851085
* @stop_seq: Flush up until this record
1086+
* @allow_unsafe_takeover: True, to allow unsafe hostile takeovers
10861087
*
10871088
* Return: 0 if @con was flushed up to @stop_seq Otherwise, error code on
10881089
* failure.
@@ -1101,7 +1102,8 @@ bool nbcon_legacy_emit_next_record(struct console *con, bool *handover,
11011102
* returned, it cannot be expected that the unfinalized record will become
11021103
* available.
11031104
*/
1104-
static int __nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq)
1105+
static int __nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq,
1106+
bool allow_unsafe_takeover)
11051107
{
11061108
struct nbcon_write_context wctxt = { };
11071109
struct nbcon_context *ctxt = &ACCESS_PRIVATE(&wctxt, ctxt);
@@ -1110,6 +1112,7 @@ static int __nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq)
11101112
ctxt->console = con;
11111113
ctxt->spinwait_max_us = 2000;
11121114
ctxt->prio = nbcon_get_default_prio();
1115+
ctxt->allow_unsafe_takeover = allow_unsafe_takeover;
11131116

11141117
if (!nbcon_context_try_acquire(ctxt))
11151118
return -EPERM;
@@ -1140,13 +1143,15 @@ static int __nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq)
11401143
* write_atomic() callback
11411144
* @con: The nbcon console to flush
11421145
* @stop_seq: Flush up until this record
1146+
* @allow_unsafe_takeover: True, to allow unsafe hostile takeovers
11431147
*
11441148
* This will stop flushing before @stop_seq if another context has ownership.
11451149
* That context is then responsible for the flushing. Likewise, if new records
11461150
* are added while this context was flushing and there is no other context
11471151
* to handle the printing, this context must also flush those records.
11481152
*/
1149-
static void nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq)
1153+
static void nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq,
1154+
bool allow_unsafe_takeover)
11501155
{
11511156
unsigned long flags;
11521157
int err;
@@ -1160,7 +1165,7 @@ static void nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq)
11601165
*/
11611166
local_irq_save(flags);
11621167

1163-
err = __nbcon_atomic_flush_pending_con(con, stop_seq);
1168+
err = __nbcon_atomic_flush_pending_con(con, stop_seq, allow_unsafe_takeover);
11641169

11651170
local_irq_restore(flags);
11661171

@@ -1190,8 +1195,9 @@ static void nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq)
11901195
* __nbcon_atomic_flush_pending - Flush all nbcon consoles using their
11911196
* write_atomic() callback
11921197
* @stop_seq: Flush up until this record
1198+
* @allow_unsafe_takeover: True, to allow unsafe hostile takeovers
11931199
*/
1194-
static void __nbcon_atomic_flush_pending(u64 stop_seq)
1200+
static void __nbcon_atomic_flush_pending(u64 stop_seq, bool allow_unsafe_takeover)
11951201
{
11961202
struct console *con;
11971203
int cookie;
@@ -1209,7 +1215,7 @@ static void __nbcon_atomic_flush_pending(u64 stop_seq)
12091215
if (nbcon_seq_read(con) >= stop_seq)
12101216
continue;
12111217

1212-
nbcon_atomic_flush_pending_con(con, stop_seq);
1218+
nbcon_atomic_flush_pending_con(con, stop_seq, allow_unsafe_takeover);
12131219
}
12141220
console_srcu_read_unlock(cookie);
12151221
}
@@ -1225,7 +1231,19 @@ static void __nbcon_atomic_flush_pending(u64 stop_seq)
12251231
*/
12261232
void nbcon_atomic_flush_pending(void)
12271233
{
1228-
__nbcon_atomic_flush_pending(prb_next_reserve_seq(prb));
1234+
__nbcon_atomic_flush_pending(prb_next_reserve_seq(prb), false);
1235+
}
1236+
1237+
/**
1238+
* nbcon_atomic_flush_unsafe - Flush all nbcon consoles using their
1239+
* write_atomic() callback and allowing unsafe hostile takeovers
1240+
*
1241+
* Flush the backlog up through the currently newest record. Unsafe hostile
1242+
* takeovers will be performed, if necessary.
1243+
*/
1244+
void nbcon_atomic_flush_unsafe(void)
1245+
{
1246+
__nbcon_atomic_flush_pending(prb_next_reserve_seq(prb), true);
12291247
}
12301248

12311249
/**
@@ -1342,7 +1360,7 @@ void nbcon_device_release(struct console *con)
13421360
if (console_is_usable(con, console_srcu_read_flags(con)) &&
13431361
prb_read_valid(prb, nbcon_seq_read(con), NULL)) {
13441362
if (!have_boot_console) {
1345-
__nbcon_atomic_flush_pending_con(con, prb_next_reserve_seq(prb));
1363+
__nbcon_atomic_flush_pending_con(con, prb_next_reserve_seq(prb), false);
13461364
} else if (!is_printk_legacy_deferred()) {
13471365
if (console_trylock())
13481366
console_unlock();

0 commit comments

Comments
 (0)