This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 1e4722bf6e31fd1bb3b8d0625c4698e941fc36b6 Author: wangzhi16 <[email protected]> AuthorDate: Tue Mar 11 11:28:58 2025 +0800 sched/spinlock: Debug tools for spinlock. The currently used spin_lock_irqsave does not support recursive calls and does not support automatic acquisition and release of spin locks, so developers need to pay attention to the correct use of spinlocks, such as not being able to schedule in spinlocks. Two functions: 1. check if switchout when holding spinlock. 2. check if re-lock when holding non-recursion spinlock Assisted-By: mimo-v2.6-pro Co-authored-by: ouyangxiangzhen <[email protected]> Signed-off-by: wangzhi16 <[email protected]> Signed-off-by: ouyangxiangzhen <[email protected]> --- include/nuttx/sched.h | 6 +++ include/nuttx/spinlock.h | 90 +++++++++++++++++++++++++++++---------- include/nuttx/spinlock_type.h | 63 +++++++++++++++++++++------ sched/Kconfig | 7 +++ sched/irq/irq_spinlock.c | 81 +++++++++++++++++++++++++++++++++++ sched/sched/sched_switchcontext.c | 8 ++++ 6 files changed, 219 insertions(+), 36 deletions(-) diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h index fa5883b350f..1f87559b1c1 100644 --- a/include/nuttx/sched.h +++ b/include/nuttx/sched.h @@ -684,6 +684,12 @@ struct tcb_s FAR struct sporadic_s *sporadic; /* Sporadic scheduling parameters */ #endif + /* Spinlock debug support *************************************************/ + +#ifdef CONFIG_SPINLOCK_DEBUG + sq_queue_t hold_spinlock; +#endif + struct wdog_s waitdog; /* All timed waits use this timer */ /* Stack-Related Fields ***************************************************/ diff --git a/include/nuttx/spinlock.h b/include/nuttx/spinlock.h index 4672b4a135e..c88d71fdcd8 100644 --- a/include/nuttx/spinlock.h +++ b/include/nuttx/spinlock.h @@ -91,6 +91,16 @@ void nxsched_critmon_busywait(bool state, FAR void *caller); # define nxsched_critmon_busywait(state, caller) #endif +#ifdef CONFIG_SPINLOCK_DEBUG +void spinlock_mark_locked(FAR spinlock_debug_t *info); +void spinlock_mark_unlocked(FAR spinlock_debug_t *info); +void spinlock_switch_context(FAR struct tcb_s *from); +#else +# define spinlock_mark_locked(info) +# define spinlock_mark_unlocked(info) +# define spinlock_switch_context(from) +#endif + /**************************************************************************** * Public Data Types ****************************************************************************/ @@ -130,7 +140,7 @@ void nxsched_critmon_busywait(bool state, FAR void *caller); static inline_function void rspin_lock_init(FAR rspinlock_t *lock) { - lock->val = 0; + *lock = RSPINLOCK_INITIALIZER; } /**************************************************************************** @@ -167,7 +177,7 @@ void spin_lock_notrace(FAR volatile spinlock_t *lock) SPIN_WAIT(); } #else /* CONFIG_TICKET_SPINLOCK */ - while (atomic_xchg_acquire((FAR atomic_t *)lock, SP_LOCKED) == SP_LOCKED) + while (atomic_xchg_acquire(&lock->lock, 1) == 1) { SPIN_WAIT(); } @@ -226,6 +236,10 @@ static inline_function void spin_lock(FAR volatile spinlock_t *lock) /* Notify that we have the spinlock */ sched_note_spinlock_locked(lock); + + /* Mark the spinlock has been locked */ + + spinlock_mark_locked(&lock->info); } #else # define spin_lock(lock) ((void)(lock)) @@ -245,8 +259,8 @@ static inline_function void spin_lock(FAR volatile spinlock_t *lock) * lock - A reference to the spinlock object to lock. * * Returned Value: - * SP_LOCKED - Failure, the spinlock was already locked - * SP_UNLOCKED - Success, the spinlock was successfully locked + * false - Failure, the spinlock was already locked + * true - Success, the spinlock was successfully locked * * Assumptions: * Not running at the interrupt level. @@ -271,7 +285,7 @@ spin_trylock_notrace(FAR volatile spinlock_t *lock) return atomic_cmpxchg_acquire(&lock->next, &expected, expected + 1); #else /* CONFIG_TICKET_SPINLOCK */ - return atomic_xchg_acquire((FAR atomic_t *)lock, SP_LOCKED) != SP_LOCKED; + return atomic_xchg_acquire(&lock->lock, 1) != 1; #endif /* CONFIG_TICKET_SPINLOCK */ } #else @@ -289,8 +303,8 @@ spin_trylock_notrace(FAR volatile spinlock_t *lock) * lock - A reference to the spinlock object to lock. * * Returned Value: - * SP_LOCKED - Failure, the spinlock was already locked - * SP_UNLOCKED - Success, the spinlock was successfully locked + * false - Failure, the spinlock was already locked + * true - Success, the spinlock was successfully locked * * Assumptions: * Not running at the interrupt level. @@ -314,6 +328,10 @@ static inline_function bool spin_trylock(FAR volatile spinlock_t *lock) /* Notify that we have the spinlock */ sched_note_spinlock_locked(lock); + + /* Mark the spinlock has been locked */ + + spinlock_mark_locked(&lock->info); } else { @@ -355,7 +373,7 @@ spin_unlock_notrace(FAR volatile spinlock_t *lock) #ifdef CONFIG_TICKET_SPINLOCK atomic_set_release(&lock->owner, lock->owner + 1); #else - atomic_set_release((FAR atomic_t *)lock, SP_UNLOCKED); + atomic_set_release(&lock->lock, 0); #endif SPIN_KICK(); } @@ -390,6 +408,10 @@ static inline_function void spin_unlock(FAR volatile spinlock_t *lock) /* Notify that we are unlocking the spinlock */ sched_note_spinlock_unlock(lock); + + /* Mark the spinlock has been unlocked */ + + spinlock_mark_unlocked(&lock->info); } #else # define spin_unlock(lock) ((void)(lock)) @@ -414,7 +436,7 @@ static inline_function void spin_unlock(FAR volatile spinlock_t *lock) # define spin_is_locked(l) \ (atomic_read(&(*l).owner) != atomic_read(&(*l).next)) #else -# define spin_is_locked(l) (*(l) == SP_LOCKED) +# define spin_is_locked(l) (atomic_read(&(l)->lock) == 1) #endif /**************************************************************************** @@ -490,6 +512,10 @@ irqstate_t spin_lock_irqsave(FAR volatile spinlock_t *lock) sched_note_spinlock_locked(lock); + /* Mark the spinlock has been locked */ + + spinlock_mark_locked(&lock->info); + return flags; } #else @@ -690,6 +716,10 @@ void rspin_lock(FAR rspinlock_t *lock) */ compiler_assume(lock->local_val == new_val); + + /* Mark locked only when the lock is acquired for the first time. */ + + spinlock_mark_locked(&lock->info); } } # else @@ -772,6 +802,8 @@ SPINLOCK_INLINE bool rspin_trylock(FAR rspinlock_t *lock) if (ret) { + spinlock_mark_locked(&lock->info); + /* The local value shares the same memory location with the * global atomic value. So if the atomic operation success, * the local value should also change to the new_val. @@ -961,6 +993,10 @@ void spin_unlock_irqrestore(FAR volatile spinlock_t *lock, irqstate_t flags) /* Notify that we are unlocking the spinlock */ sched_note_spinlock_unlock(lock); + + /* Mark the spinlock has been unlocked */ + + spinlock_mark_unlocked(&lock->info); } #else # define spin_unlock_irqrestore(l, f) ((void)(l), up_irq_restore(f)) @@ -1037,6 +1073,7 @@ bool rspin_unlock(FAR rspinlock_t *lock) if (RSPINLOCK_COUNT(val) == 1u) { + spinlock_mark_unlocked(&lock->info); atomic_set_release(&lock->val, 0u); SPIN_KICK(); ret = true; @@ -1125,7 +1162,7 @@ void rspin_restorelock(FAR rspinlock_t *lock, uint16_t count) * ****************************************************************************/ -#define rwlock_init(l) do { *(l) = RW_SP_UNLOCKED; } while(0) +#define rwlock_init(l) do { (l->lock) = RW_SP_UNLOCKED; } while(0) /**************************************************************************** * Name: read_lock @@ -1158,11 +1195,11 @@ SPINLOCK_INLINE void read_lock(FAR volatile rwlock_t *lock) nxsched_critmon_busywait(true, return_address(0)); for (; ; ) { - int32_t old = atomic_read(lock); + int32_t old = atomic_read(&lock->lock); if (old > RW_SP_WRITE_LOCKED) { - if (atomic_cmpxchg_acquire(lock, &old, old + 1)) + if (atomic_cmpxchg_acquire(&lock->lock, &old, old + 1)) { break; } @@ -1212,14 +1249,14 @@ SPINLOCK_INLINE bool read_trylock(FAR volatile rwlock_t *lock) for (; ; ) { - int32_t old = atomic_read(lock); + int32_t old = atomic_read(&lock->lock); if (old <= RW_SP_WRITE_LOCKED) { DEBUGASSERT(old == RW_SP_WRITE_LOCKED); break; } - else if (atomic_cmpxchg_acquire(lock, &old, old + 1)) + else if (atomic_cmpxchg_acquire(&lock->lock, &old, old + 1)) { ret = true; break; @@ -1251,9 +1288,9 @@ bool read_trylock(FAR volatile rwlock_t *lock); static inline_function void read_unlock(FAR volatile rwlock_t *lock) { - DEBUGASSERT(atomic_read(lock) >= RW_SP_READ_LOCKED); + DEBUGASSERT(atomic_read(&lock->lock) >= RW_SP_READ_LOCKED); - atomic_sub_release(lock, 1); + atomic_sub_release(&lock->lock, 1); SPIN_KICK(); } @@ -1290,8 +1327,8 @@ SPINLOCK_INLINE void write_lock(FAR volatile rwlock_t *lock) for (; ; ) { - int zero = RW_SP_UNLOCKED; - if (atomic_cmpxchg_acquire((FAR atomic_int *)lock, &zero, + atomic_t zero = RW_SP_UNLOCKED; + if (atomic_cmpxchg_acquire(&lock->lock, &zero, RW_SP_WRITE_LOCKED)) { break; @@ -1300,6 +1337,8 @@ SPINLOCK_INLINE void write_lock(FAR volatile rwlock_t *lock) SPIN_WAIT(); } + spinlock_mark_locked(&lock->info); + nxsched_critmon_busywait(false, return_address(0)); } # else @@ -1334,9 +1373,15 @@ void write_lock(FAR volatile rwlock_t *lock); static inline_function bool write_trylock(FAR volatile rwlock_t *lock) { - int zero = RW_SP_UNLOCKED; + atomic_t zero = RW_SP_UNLOCKED; + bool ret = atomic_cmpxchg_acquire(&lock->lock, &zero, RW_SP_WRITE_LOCKED); - return atomic_cmpxchg_acquire(lock, &zero, RW_SP_WRITE_LOCKED); + if (ret) + { + spinlock_mark_locked(&lock->info); + } + + return ret; } /**************************************************************************** @@ -1360,9 +1405,10 @@ static inline_function void write_unlock(FAR volatile rwlock_t *lock) { /* Ensure this cpu already get write lock */ - DEBUGASSERT(atomic_read(lock) == RW_SP_WRITE_LOCKED); + DEBUGASSERT(atomic_read(&lock->lock) == RW_SP_WRITE_LOCKED); - atomic_set_release(lock, RW_SP_UNLOCKED); + spinlock_mark_unlocked(&lock->info); + atomic_set_release(&lock->lock, RW_SP_UNLOCKED); SPIN_KICK(); } diff --git a/include/nuttx/spinlock_type.h b/include/nuttx/spinlock_type.h index b52d1d381e0..11b950419d4 100644 --- a/include/nuttx/spinlock_type.h +++ b/include/nuttx/spinlock_type.h @@ -29,6 +29,9 @@ #include <nuttx/config.h> #include <nuttx/atomic.h> +#if defined(CONFIG_SPINLOCK_DEBUG) +# include <nuttx/queue.h> +#endif /**************************************************************************** * Public Type Definitions @@ -39,47 +42,79 @@ extern "C" { #endif +#if defined(CONFIG_SPINLOCK_DEBUG) +typedef struct spinlock_debug_s +{ + FAR sq_entry_t *flink; + FAR struct tcb_s *holder; + FAR void *caller; +} spinlock_debug_t; +# define SPINLOCK_DEBUG_INFO spinlock_debug_t info; +# define SPINLOCK_DEBUG_INIT {NULL, NULL, NULL} +#else +# define SPINLOCK_DEBUG_INFO +# define SPINLOCK_DEBUG_INIT +#endif + #if defined(CONFIG_RW_SPINLOCK) -typedef atomic_t rwlock_t; +typedef struct rwlock_s +{ + atomic_t lock; + SPINLOCK_DEBUG_INFO +} rwlock_t; # define RW_SP_UNLOCKED 0 # define RW_SP_READ_LOCKED 1 # define RW_SP_WRITE_LOCKED -1 #endif #ifndef CONFIG_SPINLOCK -# define SP_UNLOCKED 0 /* The Un-locked state */ -# define SP_LOCKED 1 /* The Locked state */ +# define SP_UNLOCKED {0, SPINLOCK_DEBUG_INIT} /* The Un-locked state */ +# define SP_LOCKED {1, SPINLOCK_DEBUG_INIT} /* The Locked state */ -typedef atomic_t spinlock_t; +typedef struct spinlock_s +{ + atomic_t lock; + SPINLOCK_DEBUG_INFO +} spinlock_t; #elif defined(CONFIG_TICKET_SPINLOCK) typedef struct spinlock_s { atomic_t owner; atomic_t next; + SPINLOCK_DEBUG_INFO } spinlock_t; -# define SP_UNLOCKED (spinlock_t){0, 0} -# define SP_LOCKED (spinlock_t){0, 1} +# define SP_UNLOCKED {0, 0, SPINLOCK_DEBUG_INIT} +# define SP_LOCKED {0, 1, SPINLOCK_DEBUG_INIT} #else -typedef atomic_t spinlock_t; +typedef struct spinlock_s +{ + atomic_t lock; + SPINLOCK_DEBUG_INFO +} spinlock_t; -# define SP_UNLOCKED 0 -# define SP_LOCKED 1 +# define SP_UNLOCKED {0, SPINLOCK_DEBUG_INIT} +# define SP_LOCKED {1, SPINLOCK_DEBUG_INIT} #endif /* CONFIG_SPINLOCK */ #define RSPINLOCK_CPU_INVALID (-1) -#define RSPINLOCK_INITIALIZER {0} +#define RSPINLOCK_INITIALIZER (rspinlock_t){{0}, SPINLOCK_DEBUG_INIT} -typedef union rspinlock_u +typedef struct rspinlock_s { - /* Which cpu is holding spinlock, and taking recursive count */ + union + { + /* Which cpu is holding spinlock, and taking recursive count */ + + atomic_t val; /* Shared lock value. */ + uint32_t local_val; /* Local copy to avoid atomic read/write. */ + }; - atomic_t val; /* Shared lock value. */ - uint32_t local_val; /* Local copy to avoid atomic read/write. */ + SPINLOCK_DEBUG_INFO } rspinlock_t; #define RSPINLOCK_OWNER(val) ((uint32_t)(val) >> 16u) /* The high 16 bits are the owner */ diff --git a/sched/Kconfig b/sched/Kconfig index 848e3bd9ca9..9c43a9f7c5f 100644 --- a/sched/Kconfig +++ b/sched/Kconfig @@ -319,6 +319,13 @@ config RW_SPINLOCK Reader can take read lock simultaneously and only one writer can take write lock. +config SPINLOCK_DEBUG + bool "Support debugging safe use of spinlock" + depends on DEBUG_FEATURES + default n + ---help--- + Enable to support debug tools for spinlock. + config SPINLOCK_INLINE bool "Inline spinlock helpers" default !DEFAULT_SMALL diff --git a/sched/irq/irq_spinlock.c b/sched/irq/irq_spinlock.c index a7a8231bb64..e35f4061d52 100644 --- a/sched/irq/irq_spinlock.c +++ b/sched/irq/irq_spinlock.c @@ -38,6 +38,7 @@ #include <nuttx/spinlock.h> #include <assert.h> +#include <debug.h> #include <sys/types.h> #include <arch/irq.h> @@ -53,4 +54,84 @@ * Public Functions ****************************************************************************/ +#ifdef CONFIG_SPINLOCK_DEBUG +static void dump_spinlock(FAR sq_queue_t *hold_spinlock) +{ + FAR sq_entry_t *entry; + + sq_for_every(hold_spinlock, entry) + { + FAR spinlock_debug_t *info = (FAR spinlock_debug_t *)entry; + + _alert("Spinlock(%p) has been locked by %p\n", info, info->caller); + } +} + +void spinlock_mark_locked(FAR spinlock_debug_t *info) +{ + FAR struct tcb_s *rtcb = running_task(); + + /* Check if the spinlock has been held. */ + + if (info->holder != NULL) + { + _alert("Detect thread(%d) relocks spinlock(%p)!\n", + rtcb->pid, info); + dump_spinlock(&rtcb->hold_spinlock); + PANIC(); + } + + /* Set info and enqueue. */ + + info->holder = rtcb; + info->caller = return_address(0); + sq_addlast(info->flink, &rtcb->hold_spinlock); +} + +void spinlock_mark_unlocked(FAR spinlock_debug_t *info) +{ + FAR struct tcb_s *rtcb = running_task(); + + /* Check if locking and unlocking are the same task. */ + + if (info->holder != rtcb) + { + _alert("Detect thread(%d) unlocks spinlock(%p) which is not held!\n", + rtcb->pid, info); + dump_spinlock(&rtcb->hold_spinlock); + PANIC(); + } + + /* Currently, NuttX does not support unlock in the reverse order, since + * this will pollute flags. So check if unlocking is the reverse order. + */ + + if (sq_tail(&rtcb->hold_spinlock) != info->flink) + { + _alert("Detect thread(%d) unlocks spinlock(%p) in the wrong order!\n", + rtcb->pid, info); + dump_spinlock(&rtcb->hold_spinlock); + PANIC(); + } + + /* Reset info and dequeue. */ + + info->holder = NULL; + info->caller = NULL; + sq_remlast(&rtcb->hold_spinlock); +} + +void spinlock_switch_context(FAR struct tcb_s *from) +{ + if (!sq_empty(&from->hold_spinlock)) + { + _alert("Detect thread(%d) switches context while holding spinlock!\n", + from->pid); + dump_spinlock(&from->hold_spinlock); + PANIC(); + } +} + +#endif /* CONFIG_SPINLOCK_DEBUG */ + #endif /* CONFIG_SPINLOCK */ diff --git a/sched/sched/sched_switchcontext.c b/sched/sched/sched_switchcontext.c index 1d37bee8a53..1402897e84f 100644 --- a/sched/sched/sched_switchcontext.c +++ b/sched/sched/sched_switchcontext.c @@ -28,6 +28,10 @@ #include <nuttx/sched_note.h> +#ifdef CONFIG_SPINLOCK_DEBUG +# include <nuttx/spinlock.h> +#endif + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -62,6 +66,10 @@ void nxsched_switch_context(FAR struct tcb_s *from, FAR struct tcb_s *to) } #endif +#ifdef CONFIG_SPINLOCK_DEBUG + spinlock_switch_context(from); +#endif + #ifdef CONFIG_SCHED_SPORADIC /* Perform sporadic schedule operations */
