Skip to content

Commit 286b113

Browse files
marcospspmladek
authored andcommitted
printk: nbcon: Allow KDB to acquire the NBCON context
KDB can interrupt any console to execute the "mirrored printing" at any time, so add an exception to nbcon_context_try_acquire_direct to allow to get the context if the current CPU is the same as kdb_printf_cpu. This change will be necessary for the next patch, which fixes kdb_msg_write to work with NBCON consoles by calling ->write_atomic on such consoles. But to print it first needs to acquire the ownership of the console, so nbcon_context_try_acquire_direct is fixed here. Reviewed-by: John Ogness <john.ogness@linutronix.de> Signed-off-by: Marcos Paulo de Souza <mpdesouza@suse.com> Reviewed-by: Petr Mladek <pmladek@suse.com> Link: https://patch.msgid.link/20251016-nbcon-kgdboc-v6-3-866aac60a80e@suse.com [pmladek@suse.com: Fix compilation with !CONFIG_KGDB_KDB.] Signed-off-by: Petr Mladek <pmladek@suse.com>
1 parent 49f7d30 commit 286b113

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

include/linux/kdb.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
#include <linux/list.h>
17+
#include <linux/smp.h>
1718

1819
/* Shifted versions of the command enable bits are be used if the command
1920
* has no arguments (see kdb_check_flags). This allows commands, such as
@@ -207,11 +208,26 @@ static inline const char *kdb_walk_kallsyms(loff_t *pos)
207208
/* Dynamic kdb shell command registration */
208209
extern int kdb_register(kdbtab_t *cmd);
209210
extern void kdb_unregister(kdbtab_t *cmd);
211+
212+
/* Return true when KDB as locked for printing a message on this CPU. */
213+
static inline
214+
bool kdb_printf_on_this_cpu(void)
215+
{
216+
/*
217+
* We can use raw_smp_processor_id() here because the task could
218+
* not get migrated when KDB has locked for printing on this CPU.
219+
*/
220+
return unlikely(READ_ONCE(kdb_printf_cpu) == raw_smp_processor_id());
221+
}
222+
210223
#else /* ! CONFIG_KGDB_KDB */
211224
static inline __printf(1, 2) int kdb_printf(const char *fmt, ...) { return 0; }
212225
static inline void kdb_init(int level) {}
213226
static inline int kdb_register(kdbtab_t *cmd) { return 0; }
214227
static inline void kdb_unregister(kdbtab_t *cmd) {}
228+
229+
static inline bool kdb_printf_on_this_cpu(void) { return false; }
230+
215231
#endif /* CONFIG_KGDB_KDB */
216232
enum {
217233
KDB_NOT_INITIALIZED,

kernel/printk/nbcon.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/export.h>
1111
#include <linux/init.h>
1212
#include <linux/irqflags.h>
13+
#include <linux/kdb.h>
1314
#include <linux/kthread.h>
1415
#include <linux/minmax.h>
1516
#include <linux/panic.h>
@@ -249,13 +250,16 @@ static int nbcon_context_try_acquire_direct(struct nbcon_context *ctxt,
249250
* since all non-panic CPUs are stopped during panic(), it
250251
* is safer to have them avoid gaining console ownership.
251252
*
252-
* If this acquire is a reacquire (and an unsafe takeover
253+
* One exception is when kdb has locked for printing on this CPU.
254+
*
255+
* Second exception is a reacquire (and an unsafe takeover
253256
* has not previously occurred) then it is allowed to attempt
254257
* a direct acquire in panic. This gives console drivers an
255258
* opportunity to perform any necessary cleanup if they were
256259
* interrupted by the panic CPU while printing.
257260
*/
258261
if (panic_on_other_cpu() &&
262+
!kdb_printf_on_this_cpu() &&
259263
(!is_reacquire || cur->unsafe_takeover)) {
260264
return -EPERM;
261265
}

0 commit comments

Comments
 (0)