Skip to content

Commit 5946d1f

Browse files
b49020Daniel Thompson
authored andcommitted
kdb: Switch to use safer dbg_io_ops over console APIs
In kgdb context, calling console handlers aren't safe due to locks used in those handlers which could in turn lead to a deadlock. Although, using oops_in_progress increases the chance to bypass locks in most console handlers but it might not be sufficient enough in case a console uses more locks (VT/TTY is good example). Currently when a driver provides both polling I/O and a console then kdb will output using the console. We can increase robustness by using the currently active polling I/O driver (which should be lockless) instead of the corresponding console. For several common cases (e.g. an embedded system with a single serial port that is used both for console output and debugger I/O) this will result in no console handler being used. In order to achieve this we need to reverse the order of preference to use dbg_io_ops (uses polling I/O mode) over console APIs. So we just store "struct console" that represents debugger I/O in dbg_io_ops and while emitting kdb messages, skip console that matches dbg_io_ops console in order to avoid duplicate messages. After this change, "is_console" param becomes redundant and hence removed. Suggested-by: Daniel Thompson <[email protected]> Signed-off-by: Sumit Garg <[email protected]> Link: https://lore.kernel.org/r/[email protected] Reviewed-by: Douglas Anderson <[email protected]> Reviewed-by: Petr Mladek <[email protected]> Acked-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Daniel Thompson <[email protected]>
1 parent 2a78b85 commit 5946d1f

File tree

5 files changed

+24
-22
lines changed

5 files changed

+24
-22
lines changed

drivers/tty/serial/kgdb_nmi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static int kgdb_nmi_console_setup(struct console *co, char *options)
5050
* I/O utilities that messages sent to the console will automatically
5151
* be displayed on the dbg_io.
5252
*/
53-
dbg_io_ops->is_console = true;
53+
dbg_io_ops->cons = co;
5454

5555
return 0;
5656
}

drivers/tty/serial/kgdboc.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ static struct platform_device *kgdboc_pdev;
4545

4646
#if IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE)
4747
static struct kgdb_io kgdboc_earlycon_io_ops;
48-
static struct console *earlycon;
4948
static int (*earlycon_orig_exit)(struct console *con);
5049
#endif /* IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE) */
5150

@@ -145,7 +144,7 @@ static void kgdboc_unregister_kbd(void)
145144
#if IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE)
146145
static void cleanup_earlycon(void)
147146
{
148-
if (earlycon)
147+
if (kgdboc_earlycon_io_ops.cons)
149148
kgdb_unregister_io_module(&kgdboc_earlycon_io_ops);
150149
}
151150
#else /* !IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE) */
@@ -178,7 +177,7 @@ static int configure_kgdboc(void)
178177
goto noconfig;
179178
}
180179

181-
kgdboc_io_ops.is_console = 0;
180+
kgdboc_io_ops.cons = NULL;
182181
kgdb_tty_driver = NULL;
183182

184183
kgdboc_use_kms = 0;
@@ -198,7 +197,7 @@ static int configure_kgdboc(void)
198197
int idx;
199198
if (cons->device && cons->device(cons, &idx) == p &&
200199
idx == tty_line) {
201-
kgdboc_io_ops.is_console = 1;
200+
kgdboc_io_ops.cons = cons;
202201
break;
203202
}
204203
}
@@ -433,15 +432,17 @@ static int kgdboc_earlycon_get_char(void)
433432
{
434433
char c;
435434

436-
if (!earlycon->read(earlycon, &c, 1))
435+
if (!kgdboc_earlycon_io_ops.cons->read(kgdboc_earlycon_io_ops.cons,
436+
&c, 1))
437437
return NO_POLL_CHAR;
438438

439439
return c;
440440
}
441441

442442
static void kgdboc_earlycon_put_char(u8 chr)
443443
{
444-
earlycon->write(earlycon, &chr, 1);
444+
kgdboc_earlycon_io_ops.cons->write(kgdboc_earlycon_io_ops.cons, &chr,
445+
1);
445446
}
446447

447448
static void kgdboc_earlycon_pre_exp_handler(void)
@@ -461,7 +462,7 @@ static void kgdboc_earlycon_pre_exp_handler(void)
461462
* boot if we detect this case.
462463
*/
463464
for_each_console(con)
464-
if (con == earlycon)
465+
if (con == kgdboc_earlycon_io_ops.cons)
465466
return;
466467

467468
already_warned = true;
@@ -484,25 +485,25 @@ static int kgdboc_earlycon_deferred_exit(struct console *con)
484485

485486
static void kgdboc_earlycon_deinit(void)
486487
{
487-
if (!earlycon)
488+
if (!kgdboc_earlycon_io_ops.cons)
488489
return;
489490

490-
if (earlycon->exit == kgdboc_earlycon_deferred_exit)
491+
if (kgdboc_earlycon_io_ops.cons->exit == kgdboc_earlycon_deferred_exit)
491492
/*
492493
* kgdboc_earlycon is exiting but original boot console exit
493494
* was never called (AKA kgdboc_earlycon_deferred_exit()
494495
* didn't ever run). Undo our trap.
495496
*/
496-
earlycon->exit = earlycon_orig_exit;
497-
else if (earlycon->exit)
497+
kgdboc_earlycon_io_ops.cons->exit = earlycon_orig_exit;
498+
else if (kgdboc_earlycon_io_ops.cons->exit)
498499
/*
499500
* We skipped calling the exit() routine so we could try to
500501
* keep using the boot console even after it went away. We're
501502
* finally done so call the function now.
502503
*/
503-
earlycon->exit(earlycon);
504+
kgdboc_earlycon_io_ops.cons->exit(kgdboc_earlycon_io_ops.cons);
504505

505-
earlycon = NULL;
506+
kgdboc_earlycon_io_ops.cons = NULL;
506507
}
507508

508509
static struct kgdb_io kgdboc_earlycon_io_ops = {
@@ -511,7 +512,6 @@ static struct kgdb_io kgdboc_earlycon_io_ops = {
511512
.write_char = kgdboc_earlycon_put_char,
512513
.pre_exception = kgdboc_earlycon_pre_exp_handler,
513514
.deinit = kgdboc_earlycon_deinit,
514-
.is_console = true,
515515
};
516516

517517
#define MAX_CONSOLE_NAME_LEN (sizeof((struct console *) 0)->name)
@@ -557,10 +557,10 @@ static int __init kgdboc_earlycon_init(char *opt)
557557
goto unlock;
558558
}
559559

560-
earlycon = con;
560+
kgdboc_earlycon_io_ops.cons = con;
561561
pr_info("Going to register kgdb with earlycon '%s'\n", con->name);
562562
if (kgdb_register_io_module(&kgdboc_earlycon_io_ops) != 0) {
563-
earlycon = NULL;
563+
kgdboc_earlycon_io_ops.cons = NULL;
564564
pr_info("Failed to register kgdb with earlycon\n");
565565
} else {
566566
/* Trap exit so we can keep earlycon longer if needed. */

drivers/usb/early/ehci-dbgp.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,8 @@ static int __init kgdbdbgp_parse_config(char *str)
10581058
kgdbdbgp_wait_time = simple_strtoul(ptr, &ptr, 10);
10591059
}
10601060
kgdb_register_io_module(&kgdbdbgp_io_ops);
1061-
kgdbdbgp_io_ops.is_console = early_dbgp_console.index != -1;
1061+
if (early_dbgp_console.index != -1)
1062+
kgdbdbgp_io_ops.cons = &early_dbgp_console;
10621063

10631064
return 0;
10641065
}

include/linux/kgdb.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,7 @@ struct kgdb_arch {
276276
* the I/O driver.
277277
* @post_exception: Pointer to a function that will do any cleanup work
278278
* for the I/O driver.
279-
* @is_console: 1 if the end device is a console 0 if the I/O device is
280-
* not a console
279+
* @cons: valid if the I/O device is a console; else NULL.
281280
*/
282281
struct kgdb_io {
283282
const char *name;
@@ -288,7 +287,7 @@ struct kgdb_io {
288287
void (*deinit) (void);
289288
void (*pre_exception) (void);
290289
void (*post_exception) (void);
291-
int is_console;
290+
struct console *cons;
292291
};
293292

294293
extern const struct kgdb_arch arch_kgdb_ops;

kernel/debug/kdb/kdb_io.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ static void kdb_msg_write(const char *msg, int msg_len)
549549
if (msg_len == 0)
550550
return;
551551

552-
if (dbg_io_ops && !dbg_io_ops->is_console) {
552+
if (dbg_io_ops) {
553553
const char *cp = msg;
554554
int len = msg_len;
555555

@@ -562,6 +562,8 @@ static void kdb_msg_write(const char *msg, int msg_len)
562562
for_each_console(c) {
563563
if (!(c->flags & CON_ENABLED))
564564
continue;
565+
if (c == dbg_io_ops->cons)
566+
continue;
565567
/*
566568
* Set oops_in_progress to encourage the console drivers to
567569
* disregard their internal spin locks: in the current calling

0 commit comments

Comments
 (0)