On Sun, Apr 04, 2021 at 11:24:57AM +0100, Matthew Wilcox wrote:
Hmm, I'm sure i've written something like that at least once, but I
can't seem to find it :/
Does something like the completely untested below work for you?
---
diff --git a/include/linux/irqflags.h b/include/linux/irqflags.h
index 600c10da321a..d8aa1dc481b6 100644
--- a/include/linux/irqflags.h
+++ b/include/linux/irqflags.h
@@ -54,6 +54,8 @@ extern void trace_hardirqs_off_finish(void);
extern void trace_hardirqs_on(void);
extern void trace_hardirqs_off(void);
+extern void lockdep_validate_context_empty(void);
+
# define lockdep_hardirq_context() (raw_cpu_read(hardirq_context))
# define lockdep_softirq_context(p) ((p)->softirq_context)
# define lockdep_hardirqs_enabled() (this_cpu_read(hardirqs_enabled))
@@ -69,6 +71,7 @@ do { \
} while (0)
# define lockdep_hardirq_exit() \
do { \
+ lockdep_validate_context_empty(); \
__this_cpu_dec(hardirq_context); \
} while (0)
# define lockdep_softirq_enter() \
@@ -77,6 +80,7 @@ do { \
} while (0)
# define lockdep_softirq_exit() \
do { \
+ lockdep_validate_context_empty(); \
current->softirq_context--; \
} while (0)
diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
index 82db977eada8..09ac70d1b3a6 100644
--- a/kernel/locking/lockdep.c
+++ b/kernel/locking/lockdep.c
@@ -2697,6 +2697,37 @@ static int check_irq_usage(struct task_struct *curr, struct held_lock *prev,
return 0;
}
+void lockdep_validate_context_empty(void)
+{
+ struct task_struct *curr = current;
+ int depth, ctx = task_irq_context(curr);
+
+ if (debug_locks_silent)
+ return;
+
+ depth = curr->lockdep_depth;
+ if (!depth)
+ return;
+
+ if (curr->held_locks[depth-1].irq_context != ctx)
+ return;
+
+
+ pr_warn("\n");
+ pr_warn("====================================\n");
+ pr_warn("WARNING: Asymmetric locking detected\n");
+ print_kernel_ident();
+ pr_warn("------------------------------------\n");
+
+ pr_warn("%s/%d is leaving an IRQ context with extra locks on\n",
+ curr->comm, task_pid_nr(curr));
+
+ lockdep_printk_held_locks(curr);
+
+ printk("\nstack backtrace:\n");
+ dump_stack();
+}
+
#else
static inline int check_irq_usage(struct task_struct *curr,