Skip to content

Commit 6420695

Browse files
Daniel Thompsongregkh
authored andcommitted
serial: kgdboc: Fix NMI-safety problems from keyboard reset code
commit b2aba15 upstream. Currently, when kdb is compiled with keyboard support, then we will use schedule_work() to provoke reset of the keyboard status. Unfortunately schedule_work() gets called from the kgdboc post-debug-exception handler. That risks deadlock since schedule_work() is not NMI-safe and, even on platforms where the NMI is not directly used for debugging, the debug trap can have NMI-like behaviour depending on where breakpoints are placed. Fix this by using the irq work system, which is NMI-safe, to defer the call to schedule_work() to a point when it is safe to call. Reported-by: Liuye <liu.yeC@h3c.com> Closes: https://lore.kernel.org/all/20240228025602.3087748-1-liu.yeC@h3c.com/ Cc: stable@vger.kernel.org Reviewed-by: Douglas Anderson <dianders@chromium.org> Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Link: https://lore.kernel.org/r/20240424-kgdboc_fix_schedule_work-v2-1-50f5a490aec5@linaro.org Signed-off-by: Daniel Thompson <daniel.thompson@linaro.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 40b963e commit 6420695

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

drivers/tty/serial/kgdboc.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/console.h>
2020
#include <linux/vt_kern.h>
2121
#include <linux/input.h>
22+
#include <linux/irq_work.h>
2223
#include <linux/module.h>
2324
#include <linux/platform_device.h>
2425
#include <linux/serial_core.h>
@@ -48,6 +49,25 @@ static struct kgdb_io kgdboc_earlycon_io_ops;
4849
static int (*earlycon_orig_exit)(struct console *con);
4950
#endif /* IS_BUILTIN(CONFIG_KGDB_SERIAL_CONSOLE) */
5051

52+
/*
53+
* When we leave the debug trap handler we need to reset the keyboard status
54+
* (since the original keyboard state gets partially clobbered by kdb use of
55+
* the keyboard).
56+
*
57+
* The path to deliver the reset is somewhat circuitous.
58+
*
59+
* To deliver the reset we register an input handler, reset the keyboard and
60+
* then deregister the input handler. However, to get this done right, we do
61+
* have to carefully manage the calling context because we can only register
62+
* input handlers from task context.
63+
*
64+
* In particular we need to trigger the action from the debug trap handler with
65+
* all its NMI and/or NMI-like oddities. To solve this the kgdboc trap exit code
66+
* (the "post_exception" callback) uses irq_work_queue(), which is NMI-safe, to
67+
* schedule a callback from a hardirq context. From there we have to defer the
68+
* work again, this time using schedule_work(), to get a callback using the
69+
* system workqueue, which runs in task context.
70+
*/
5171
#ifdef CONFIG_KDB_KEYBOARD
5272
static int kgdboc_reset_connect(struct input_handler *handler,
5373
struct input_dev *dev,
@@ -99,10 +119,17 @@ static void kgdboc_restore_input_helper(struct work_struct *dummy)
99119

100120
static DECLARE_WORK(kgdboc_restore_input_work, kgdboc_restore_input_helper);
101121

122+
static void kgdboc_queue_restore_input_helper(struct irq_work *unused)
123+
{
124+
schedule_work(&kgdboc_restore_input_work);
125+
}
126+
127+
static DEFINE_IRQ_WORK(kgdboc_restore_input_irq_work, kgdboc_queue_restore_input_helper);
128+
102129
static void kgdboc_restore_input(void)
103130
{
104131
if (likely(system_state == SYSTEM_RUNNING))
105-
schedule_work(&kgdboc_restore_input_work);
132+
irq_work_queue(&kgdboc_restore_input_irq_work);
106133
}
107134

108135
static int kgdboc_register_kbd(char **cptr)
@@ -133,6 +160,7 @@ static void kgdboc_unregister_kbd(void)
133160
i--;
134161
}
135162
}
163+
irq_work_sync(&kgdboc_restore_input_irq_work);
136164
flush_work(&kgdboc_restore_input_work);
137165
}
138166
#else /* ! CONFIG_KDB_KEYBOARD */

0 commit comments

Comments
 (0)