[PATCH] printk/nbcon: Add assert that CPU migration is disabled when calling nbcon_context_try_acquire()
From: Petr Mladek
Date: Wed Sep 20 2023 - 08:51:50 EST
The nbcon console is locked when the 'prio' and 'cpu' fields in
console->nbcon_state stay the same. The locking algorithm would
break when the locked context got migrated to another CPU.
Add assert into nbcon_context_try_acquire(). It would warn when
the function is called in a context where the CPU migration
is possible.
Signed-off-by: Petr Mladek <pmladek@xxxxxxxx>
---
I propose this patch on top of the patchset adding basic support
for nbcon consoles, see
https://lore.kernel.org/r/20230916192007.608398-1-john.ogness@xxxxxxxxxxxxx
Or another way, it is on top of rework/nbcon-base branch in
printk/linux.git tree.
---
kernel/printk/nbcon.c | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
index b96077152f49..2c2e98d61660 100644
--- a/kernel/printk/nbcon.c
+++ b/kernel/printk/nbcon.c
@@ -234,6 +234,19 @@ static void nbcon_seq_try_update(struct nbcon_context *ctxt, u64 new_seq)
}
}
+#ifdef CONFIG_PROVE_LOCKING
+static inline void nbcon_assert_cpu_migration_disabled(void)
+{
+ WARN_ON_ONCE(IS_ENABLED(CONFIG_SMP) &&
+ __lockdep_enabled &&
+ this_cpu_read(hardirqs_enabled) &&
+ preempt_count() == 0 &&
+ !current->migration_disabled);
+}
+#else
+#define nbcon_assert_cpu_migration(void) {}
+#endif
+
/**
* nbcon_context_try_acquire_direct - Try to acquire directly
* @ctxt: The context of the caller
@@ -579,6 +592,8 @@ static bool nbcon_context_try_acquire(struct nbcon_context *ctxt)
struct nbcon_state cur;
int err;
+ nbcon_assert_cpu_migration_disabled();
+
nbcon_state_read(con, &cur);
try_again:
err = nbcon_context_try_acquire_direct(ctxt, &cur);
--
2.35.3