All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH] spinlock_debug: Save stacktrace at lock acquisition
@ 2021-06-30  1:28 Guru Das Srinagesh
  2021-06-30  2:01 ` Waiman Long
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Guru Das Srinagesh @ 2021-06-30  1:28 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Will Deacon, Waiman Long, Boqun Feng
  Cc: linux-kernel, Guru Das Srinagesh

In case of spinlock recursion bugs, knowing which entity acquired the
lock initially is key to debugging. Therefore, save the stack frames
when the lock is acquired and print them when spinlock recursion is
detected.

Signed-off-by: Guru Das Srinagesh <gurus@codeaurora.org>
---
 include/linux/spinlock_types.h  | 3 +++
 kernel/locking/spinlock_debug.c | 9 +++++++++
 2 files changed, 12 insertions(+)

diff --git a/include/linux/spinlock_types.h b/include/linux/spinlock_types.h
index b981caa..8a68d55 100644
--- a/include/linux/spinlock_types.h
+++ b/include/linux/spinlock_types.h
@@ -22,6 +22,9 @@ typedef struct raw_spinlock {
 #ifdef CONFIG_DEBUG_SPINLOCK
 	unsigned int magic, owner_cpu;
 	void *owner;
+#define MAX_STACK_LEN 16
+	int stack_len;
+	unsigned long stack_trace[MAX_STACK_LEN];
 #endif
 #ifdef CONFIG_DEBUG_LOCK_ALLOC
 	struct lockdep_map dep_map;
diff --git a/kernel/locking/spinlock_debug.c b/kernel/locking/spinlock_debug.c
index b9d9308..d34cccc 100644
--- a/kernel/locking/spinlock_debug.c
+++ b/kernel/locking/spinlock_debug.c
@@ -12,6 +12,7 @@
 #include <linux/debug_locks.h>
 #include <linux/delay.h>
 #include <linux/export.h>
+#include <linux/stacktrace.h>
 
 void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name,
 			  struct lock_class_key *key, short inner)
@@ -27,6 +28,7 @@ void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name,
 	lock->magic = SPINLOCK_MAGIC;
 	lock->owner = SPINLOCK_OWNER_INIT;
 	lock->owner_cpu = -1;
+	lock->stack_len = 0;
 }
 
 EXPORT_SYMBOL(__raw_spin_lock_init);
@@ -65,6 +67,10 @@ static void spin_dump(raw_spinlock_t *lock, const char *msg)
 		owner ? task_pid_nr(owner) : -1,
 		READ_ONCE(lock->owner_cpu));
 	dump_stack();
+	if (!strcmp(msg, "recursion")) {
+		printk(KERN_EMERG "Stack at lock acquisition: \n");
+		stack_trace_print(lock->stack_trace, lock->stack_len, 0);
+	}
 }
 
 static void spin_bug(raw_spinlock_t *lock, const char *msg)
@@ -90,6 +96,8 @@ static inline void debug_spin_lock_after(raw_spinlock_t *lock)
 {
 	WRITE_ONCE(lock->owner_cpu, raw_smp_processor_id());
 	WRITE_ONCE(lock->owner, current);
+	lock->stack_len = stack_trace_save(lock->stack_trace,
+					   ARRAY_SIZE(lock->stack_trace), 0);
 }
 
 static inline void debug_spin_unlock(raw_spinlock_t *lock)
@@ -101,6 +109,7 @@ static inline void debug_spin_unlock(raw_spinlock_t *lock)
 							lock, "wrong CPU");
 	WRITE_ONCE(lock->owner, SPINLOCK_OWNER_INIT);
 	WRITE_ONCE(lock->owner_cpu, -1);
+	WRITE_ONCE(lock->stack_len, 0);
 }
 
 /*
-- 
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-07-07  9:22 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-06-30  1:28 [RFC PATCH] spinlock_debug: Save stacktrace at lock acquisition Guru Das Srinagesh
2021-06-30  2:01 ` Waiman Long
2021-06-30  5:05 ` kernel test robot
2021-07-07  9:22 ` [spinlock_debug] 0263428b26: kernel-selftests.dma.dma_map_benchmark.average_unmap_latency 87.5% regression kernel test robot
2021-07-07  9:22   ` kernel test robot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.