Skip to content

Commit 237f0b6

Browse files
wangzhi16Fix-Point
andcommitted
sched/spinlock: Add backtrace information of lock holder in spinlock debugging.
Add CONFIG_SPINLOCK_BACKTRACE: when enabled, record the full backtrace of the task that locked the spinlock and print it on misuse, replacing the single caller frame. Mark/clear the recorded backtrace in the spinlock mark_locked/mark_unlocked paths, and make the backtrace and pid-hash locks no-trace so the debug paths do not recurse into the spinlock mark machinery. Adapted for master: - sched/sched_gettcb.c: no g_pidhashlock in the master tree (already lock-free), the no-trace conversion has no target. - keeps the otherwise identical vela shape (stack field, conditional SPINLOCK_DEBUG_INIT, CONFIG_SPINLOCK_BACKTRACE). Assisted-By: mimo-v2.6-pro Co-authored-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com> Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com> Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
1 parent 9ce4850 commit 237f0b6

4 files changed

Lines changed: 59 additions & 8 deletions

File tree

‎include/nuttx/spinlock_type.h‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,16 @@ typedef struct spinlock_debug_s
4747
{
4848
sq_entry_t flink;
4949
FAR struct tcb_s *holder;
50-
FAR void *caller;
50+
# if defined(CONFIG_SPINLOCK_BACKTRACE)
51+
FAR void *stack;
52+
# endif
5153
} spinlock_debug_t;
5254
# define SPINLOCK_DEBUG_INFO spinlock_debug_t info;
53-
# define SPINLOCK_DEBUG_INIT {{NULL}, NULL, NULL}
55+
# if defined(CONFIG_SPINLOCK_BACKTRACE)
56+
# define SPINLOCK_DEBUG_INIT {{NULL}, NULL, NULL},
57+
# else
58+
# define SPINLOCK_DEBUG_INIT {{NULL}, NULL},
59+
# endif
5460
#else
5561
# define SPINLOCK_DEBUG_INFO
5662
# define SPINLOCK_DEBUG_INIT

‎libs/libc/misc/lib_backtrace.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
****************************************************************************/
4444

4545
#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
46-
# define backtrace_lock(bp) spin_lock_irqsave(&(bp)->lock)
47-
# define backtrace_unlock(bp, flags) spin_unlock_irqrestore(&(bp)->lock, flags)
46+
# define backtrace_lock(bp) spin_lock_irqsave_notrace(&(bp)->lock)
47+
# define backtrace_unlock(bp, flags) spin_unlock_irqrestore_notrace(&(bp)->lock, flags)
4848
#else
4949
# define backtrace_lock(bp) ((irqstate_t)nxmutex_lock(&(bp)->lock))
5050
# define backtrace_unlock(bp, flags) \

‎sched/Kconfig‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,13 @@ config SPINLOCK_DEBUG
326326
---help---
327327
Enable to support debug tools for spinlock.
328328

329+
config SPINLOCK_BACKTRACE
330+
bool "Enable spinlock backtrace"
331+
depends on SPINLOCK_DEBUG && LIBC_BACKTRACE_DEPTH > 0
332+
default n
333+
---help---
334+
Enable to record backtrace when a task holds a spinlock.
335+
329336
config SPINLOCK_INLINE
330337
bool "Inline spinlock helpers"
331338
default !DEFAULT_SMALL

‎sched/irq/irq_spinlock.c‎

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <debug.h>
4242
#include <sys/types.h>
4343
#include <arch/irq.h>
44+
#include <execinfo.h>
4445

4546
#include "sched/sched.h"
4647
#include "irq/irq.h"
@@ -53,6 +54,34 @@
5354
* Public Functions
5455
****************************************************************************/
5556

57+
#ifdef CONFIG_SPINLOCK_BACKTRACE
58+
59+
/****************************************************************************
60+
* Name: spinlock_add_backtrace / spinlock_remove_backtrace
61+
*
62+
* Description:
63+
* Record/remove the backtrace of the caller who locks the spinlock.
64+
*
65+
* NOTE: backtrace_record is relatively complex and involves malloc
66+
* (which may cause context switching).
67+
*
68+
****************************************************************************/
69+
70+
static void spinlock_add_backtrace(FAR spinlock_debug_t *info)
71+
{
72+
info->stack = backtrace_record(0);
73+
}
74+
75+
static void spinlock_remove_backtrace(FAR spinlock_debug_t *info)
76+
{
77+
backtrace_remove(info->stack);
78+
info->stack = NULL;
79+
}
80+
#else
81+
# define spinlock_add_backtrace(info)
82+
# define spinlock_remove_backtrace(info)
83+
#endif
84+
5685
#ifdef CONFIG_SPINLOCK_DEBUG
5786
static void dump_spinlock(FAR sq_queue_t *hold_spinlock)
5887
{
@@ -61,8 +90,17 @@ static void dump_spinlock(FAR sq_queue_t *hold_spinlock)
6190
sq_for_every(hold_spinlock, entry)
6291
{
6392
FAR spinlock_debug_t *info = (FAR spinlock_debug_t *)entry;
64-
65-
_alert("Spinlock(%p) has been locked by %p\n", info, info->caller);
93+
# ifdef CONFIG_SPINLOCK_BACKTRACE
94+
char buf[BACKTRACE_BUFFER_SIZE(CONFIG_LIBC_BACKTRACE_DEPTH)];
95+
FAR void **stack;
96+
int depth;
97+
98+
stack = backtrace_get(info->stack, &depth);
99+
backtrace_format(buf, sizeof(buf), stack, depth);
100+
_alert("Spinlock(%p) backtrace: %s\n", info, buf);
101+
# else
102+
_alert("Spinlock(%p) has been locked\n", info);
103+
# endif
66104
}
67105
}
68106

@@ -157,7 +195,7 @@ void spinlock_mark_locked(FAR spinlock_debug_t *info)
157195
/* Set info and enqueue. */
158196

159197
info->holder = rtcb;
160-
info->caller = return_address(0);
198+
spinlock_add_backtrace(info);
161199
sq_addlast(&info->flink, &rtcb->hold_spinlock);
162200

163201
/* Reset wait_spinlock. */
@@ -211,7 +249,7 @@ void spinlock_mark_unlocked(FAR spinlock_debug_t *info)
211249
/* Reset info and dequeue. */
212250

213251
info->holder = NULL;
214-
info->caller = NULL;
252+
spinlock_remove_backtrace(info);
215253
sq_remlast(&rtcb->hold_spinlock);
216254
}
217255

0 commit comments

Comments
 (0)