This is an automated email from the ASF dual-hosted git repository.
GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new 75cecc8b4a1 sched/signal: protect the signal free lists with a spinlock
75cecc8b4a1 is described below
commit 75cecc8b4a137caa8f0577e8725587c3d62c86a2
Author: hujun5 <[email protected]>
AuthorDate: Thu Mar 13 18:58:01 2025 +0800
sched/signal: protect the signal free lists with a spinlock
Protect the global signal free lists (g_sigfreeaction,
g_sigpendingaction, g_sigpendingirqaction, g_sigpendingsignal and
g_sigpendingirqsignal) with a dedicated spinlock, g_sigspinlock, so
that releasing a signal structure no longer takes the global critical
section. The sigaction free list used a private lock; unify it onto
the same spinlock.
The allocation paths relied on the critical section held by their
caller. Since the release paths now only take the spinlock, take it
in the allocation paths as well, including the interrupt-context
ones.
nxsig_alloc_dyn_pending() refills the free lists ahead of use inside
the critical section of nxsig_tcbdispatch(); add the refill under the
spinlock too. The critical section of nxsig_tcbdispatch() itself is
left in place, so the reserved structures still cannot be taken by
another sender before use.
Signed-off-by: hujun5 <[email protected]>
Signed-off-by: zhangyuan29 <[email protected]>
---
sched/signal/sig_action.c | 29 +++++++++++------------------
sched/signal/sig_allocpendingsigaction.c | 7 +++++--
sched/signal/sig_dispatch.c | 13 +++++++++++--
sched/signal/sig_initialize.c | 2 ++
sched/signal/sig_releasependingsigaction.c | 8 ++++----
sched/signal/sig_releasependingsignal.c | 8 ++++----
sched/signal/signal.h | 3 +++
7 files changed, 40 insertions(+), 30 deletions(-)
diff --git a/sched/signal/sig_action.c b/sched/signal/sig_action.c
index 8e18a6c71b7..7547b28f051 100644
--- a/sched/signal/sig_action.c
+++ b/sched/signal/sig_action.c
@@ -60,12 +60,6 @@
# define IS_PREALLOC_ACTION(x) false
#endif
-/****************************************************************************
- * Private Data
- ****************************************************************************/
-
-static spinlock_t g_sigaction_spin;
-
/****************************************************************************
* Private Functions
****************************************************************************/
@@ -90,14 +84,14 @@ static void nxsig_alloc_actionblock(void)
sigact = kmm_malloc((sizeof(sigactq_t)) * CONFIG_SIG_ALLOC_ACTIONS);
if (sigact != NULL)
{
- flags = spin_lock_irqsave(&g_sigaction_spin);
+ flags = spin_lock_irqsave(&g_sigspinlock);
for (i = 0; i < CONFIG_SIG_ALLOC_ACTIONS; i++)
{
sq_addlast((FAR sq_entry_t *)sigact++, &g_sigfreeaction);
}
- spin_unlock_irqrestore(&g_sigaction_spin, flags);
+ spin_unlock_irqrestore(&g_sigspinlock, flags);
}
}
@@ -116,9 +110,9 @@ static FAR sigactq_t *nxsig_alloc_action(void)
/* Try to get the signal action structure from the free list */
- flags = spin_lock_irqsave(&g_sigaction_spin);
+ flags = spin_lock_irqsave(&g_sigspinlock);
sigact = (FAR sigactq_t *)sq_remfirst(&g_sigfreeaction);
- spin_unlock_irqrestore(&g_sigaction_spin, flags);
+ spin_unlock_irqrestore(&g_sigspinlock, flags);
/* Check if we got one via loop as not in critical section now */
@@ -130,9 +124,9 @@ static FAR sigactq_t *nxsig_alloc_action(void)
/* And try again */
- flags = spin_lock_irqsave(&g_sigaction_spin);
+ flags = spin_lock_irqsave(&g_sigspinlock);
sigact = (FAR sigactq_t *)sq_remfirst(&g_sigfreeaction);
- spin_unlock_irqrestore(&g_sigaction_spin, flags);
+ spin_unlock_irqrestore(&g_sigspinlock, flags);
}
return sigact;
@@ -393,10 +387,9 @@ int nxsig_action(int signo, FAR const struct sigaction
*act,
sq_rem((FAR sq_entry_t *)sigpend, &group->tg_sigpendingq);
/* Add it to the temporary queue for later release
- * We cannot call nxsig_release_pendingsignal() here: it
- * enters a critical section and may call kmm_free(), and
- * neither may happen while holding the group spinlock
- * (the critical section would nest with tg_lock).
+ * We cannot call nxsig_release_pendingsignal() here: it may
+ * call kmm_free(), which must not happen while holding the
+ * group spinlock.
*/
sq_addlast((FAR sq_entry_t *)sigpend, &sigpendq);
@@ -618,9 +611,9 @@ void nxsig_release_action(FAR sigactq_t *sigact)
{
/* Non-preallocated instances will never return to heap! */
- flags = spin_lock_irqsave(&g_sigaction_spin);
+ flags = spin_lock_irqsave(&g_sigspinlock);
sq_addlast((FAR sq_entry_t *)sigact, &g_sigfreeaction);
- spin_unlock_irqrestore(&g_sigaction_spin, flags);
+ spin_unlock_irqrestore(&g_sigspinlock, flags);
}
else
{
diff --git a/sched/signal/sig_allocpendingsigaction.c
b/sched/signal/sig_allocpendingsigaction.c
index 58c9be41fb0..1d3cf6d045a 100644
--- a/sched/signal/sig_allocpendingsigaction.c
+++ b/sched/signal/sig_allocpendingsigaction.c
@@ -57,6 +57,7 @@ FAR sigq_t *nxsig_alloc_pendingsigaction(void)
{
/* Try to get the pending signal action structure from the free list */
+ flags = spin_lock_irqsave(&g_sigspinlock);
sigq = (FAR sigq_t *)sq_remfirst(&g_sigpendingaction);
/* If so, then try the special list of structures reserved for
@@ -67,6 +68,8 @@ FAR sigq_t *nxsig_alloc_pendingsigaction(void)
{
sigq = (FAR sigq_t *)sq_remfirst(&g_sigpendingirqaction);
}
+
+ spin_unlock_irqrestore(&g_sigspinlock, flags);
}
/* If we were not called from an interrupt handler, then we are
@@ -77,9 +80,9 @@ FAR sigq_t *nxsig_alloc_pendingsigaction(void)
{
/* Try to get the pending signal action structure from the free list */
- flags = enter_critical_section();
+ flags = spin_lock_irqsave(&g_sigspinlock);
sigq = (FAR sigq_t *)sq_remfirst(&g_sigpendingaction);
- leave_critical_section(flags);
+ spin_unlock_irqrestore(&g_sigspinlock, flags);
}
return sigq;
diff --git a/sched/signal/sig_dispatch.c b/sched/signal/sig_dispatch.c
index 81e32e74909..010d479a070 100644
--- a/sched/signal/sig_dispatch.c
+++ b/sched/signal/sig_dispatch.c
@@ -228,9 +228,11 @@ static int nxsig_queue_action(FAR struct tcb_s *stcb,
static FAR sigpendq_t *nxsig_alloc_pendingsignal(void)
{
FAR sigpendq_t *sigpend;
+ irqstate_t flags;
/* Try to get the pending signal structure from the free list */
+ flags = spin_lock_irqsave(&g_sigspinlock);
sigpend = (FAR sigpendq_t *)sq_remfirst(&g_sigpendingsignal);
if (!sigpend && up_interrupt_context())
{
@@ -242,6 +244,7 @@ static FAR sigpendq_t *nxsig_alloc_pendingsignal(void)
sigpend = (FAR sigpendq_t *)sq_remfirst(&g_sigpendingirqsignal);
}
+ spin_unlock_irqrestore(&g_sigspinlock, flags);
return sigpend;
}
@@ -373,13 +376,16 @@ static FAR sigpendq_t *nxsig_add_pendingsignal(FAR struct
tcb_s *stcb,
* structures are freed after they get used.
*
* Assumptions:
- * Called with g_sigpendingsignal and g_sigpendingaction locked by
- * critical section.
+ * Called inside the critical section of nxsig_tcbdispatch(), which
+ * serializes all consumers of the free lists. The lists themselves are
+ * modified under g_sigspinlock; the release paths only add to them, so
+ * an unlocked sq_empty() check can at worst cause one extra allocation.
*
****************************************************************************/
static int nxsig_alloc_dyn_pending(FAR irqstate_t *flags)
{
+ irqstate_t lockflags;
int ret = OK;
bool alloc_signal = sq_empty(&g_sigpendingsignal);
#ifdef CONFIG_ENABLE_ALL_SIGNALS
@@ -448,6 +454,7 @@ static int nxsig_alloc_dyn_pending(FAR irqstate_t *flags)
*/
*flags = enter_critical_section();
+ lockflags = spin_lock_irqsave(&g_sigspinlock);
if (alloc_signal)
{
@@ -476,6 +483,8 @@ static int nxsig_alloc_dyn_pending(FAR irqstate_t *flags)
}
}
#endif
+
+ spin_unlock_irqrestore(&g_sigspinlock, lockflags);
}
return ret;
diff --git a/sched/signal/sig_initialize.c b/sched/signal/sig_initialize.c
index 255164976ba..44165420a39 100644
--- a/sched/signal/sig_initialize.c
+++ b/sched/signal/sig_initialize.c
@@ -93,6 +93,8 @@ sq_queue_t g_sigpendingsignal;
sq_queue_t g_sigpendingirqsignal;
+spinlock_t g_sigspinlock = SP_UNLOCKED;
+
/****************************************************************************
* Private Data
****************************************************************************/
diff --git a/sched/signal/sig_releasependingsigaction.c
b/sched/signal/sig_releasependingsigaction.c
index cb91441cb32..4c39ae02571 100644
--- a/sched/signal/sig_releasependingsigaction.c
+++ b/sched/signal/sig_releasependingsigaction.c
@@ -58,9 +58,9 @@ void nxsig_release_pendingsigaction(FAR sigq_t *sigq)
* list from interrupt handlers.
*/
- flags = enter_critical_section();
+ flags = spin_lock_irqsave(&g_sigspinlock);
sq_addlast((FAR sq_entry_t *)sigq, &g_sigpendingaction);
- leave_critical_section(flags);
+ spin_unlock_irqrestore(&g_sigspinlock, flags);
}
/* If this is a message pre-allocated for interrupts,
@@ -73,9 +73,9 @@ void nxsig_release_pendingsigaction(FAR sigq_t *sigq)
* list from interrupt handlers.
*/
- flags = enter_critical_section();
+ flags = spin_lock_irqsave(&g_sigspinlock);
sq_addlast((FAR sq_entry_t *)sigq, &g_sigpendingirqaction);
- leave_critical_section(flags);
+ spin_unlock_irqrestore(&g_sigspinlock, flags);
}
/* Otherwise, deallocate it. Note: interrupt handlers
diff --git a/sched/signal/sig_releasependingsignal.c
b/sched/signal/sig_releasependingsignal.c
index 1186572be78..65fcfe91cca 100644
--- a/sched/signal/sig_releasependingsignal.c
+++ b/sched/signal/sig_releasependingsignal.c
@@ -66,9 +66,9 @@ void nxsig_release_pendingsignal(FAR sigpendq_t *sigpend)
* list from interrupt handlers.
*/
- flags = enter_critical_section();
+ flags = spin_lock_irqsave(&g_sigspinlock);
sq_addlast((FAR sq_entry_t *)sigpend, &g_sigpendingsignal);
- leave_critical_section(flags);
+ spin_unlock_irqrestore(&g_sigspinlock, flags);
}
/* If this is a message pre-allocated for interrupts,
@@ -81,9 +81,9 @@ void nxsig_release_pendingsignal(FAR sigpendq_t *sigpend)
* list from interrupt handlers.
*/
- flags = enter_critical_section();
+ flags = spin_lock_irqsave(&g_sigspinlock);
sq_addlast((FAR sq_entry_t *)sigpend, &g_sigpendingirqsignal);
- leave_critical_section(flags);
+ spin_unlock_irqrestore(&g_sigspinlock, flags);
}
/* Otherwise, deallocate it. Note: interrupt handlers
diff --git a/sched/signal/signal.h b/sched/signal/signal.h
index 6a5b74ba8a0..8041f315461 100644
--- a/sched/signal/signal.h
+++ b/sched/signal/signal.h
@@ -37,6 +37,7 @@
#include <nuttx/signal.h>
#include <nuttx/kmalloc.h>
#include <nuttx/queue.h>
+#include <nuttx/spinlock.h>
/****************************************************************************
* Pre-processor Definitions
@@ -157,6 +158,8 @@ extern sq_queue_t g_sigpendingsignal;
extern sq_queue_t g_sigpendingirqsignal;
+extern spinlock_t g_sigspinlock;
+
/****************************************************************************
* Public Function Prototypes
****************************************************************************/