Fix-Point commented on PR #22164:
URL: https://github.com/apache/nuttx/pull/22164#issuecomment-7090844485
Thanks for the analysis — the deadlock is real. Under SMP, two CPUs acquire
the queue spinlock and the critical section (the global kernel lock) in
opposite orders, forming a circular wait. The TCP-close-on-task-exit scenario
triggers it directly: CPU1 holds the critical section and waits for the LPWORK
queue lock, while the LPWORK thread on CPU2 holds the queue lock and waits for
the critical section inside `wd_start()`.
We took a different fix, implemented and validated in our tree: **replace
the wqueue timer from WDOG with HRTIMER, and drive all wakeups from the timer
callback**. The queue spinlock stays as-is; lock dependencies become
one-directional, and the lock-holding paths no longer enter the critical
section.
## Before the fix: two-way dependency forms a cycle
```mermaid
flowchart LR
A["wqueue spinlock"] <--> B["critical section"]
```
- Forward edge: with the queue lock held, `wd_start()` / `wd_cancel()` (and
`nxsem_post()` while holding the lock) enter the critical section;
- Backward edge: code running inside the critical section (task exit,
watchdog callbacks, drivers) calls `work_queue()` / `work_cancel()` and takes
the queue spinlock.
The two-way dependency forms a cycle — the circular wait condition holds, so
deadlock is possible on SMP.
## The fix: three measures
1. **Timer replacement**: the wqueue timer is switched from WDOG to HRTIMER.
hrtimer is protected by its own lock (`g_hrtimer_lock`, a seqlock independent
of the critical section); calling `hrtimer_start()` / `hrtimer_cancel()` while
holding the queue lock no longer enters the critical section.
2. **Unified wakeup path**: wakeups (`nxsem_post`) are issued only from the
timer callback `work_timer_expired`; zero-delay work also arms the timer —
hrtimer accepts zero delay, with the expiration set to the current time, so the
callback fires immediately. The callback runs in interrupt context, so it
cannot be preempted by a thread — there is no wait for a full timer period, and
no unbounded window where a post after unlocking could be delayed by preemption.
3. **Narrowed lock scope**: while holding the queue lock, no function that
enters the critical section is called (no `wd_start` / `wd_cancel` /
`nxsem_post`).
## After the fix: one-directional, acyclic
```mermaid
flowchart LR
B["critical section"] --> A["wqueue spinlock"] --> H["hrtimer lock"]
```
- The queue lock's only neighbor is the one-directional edge "queue lock →
hrtimer lock"; there is no "hrtimer lock → queue lock" path — the callback runs
lock-free and only posts the semaphore, never taking the queue lock;
- "Critical section → queue lock" remains one-directional (task exit, driver
paths); no back edge;
- The lock-dependency graph is acyclic — the circular wait condition no
longer holds, and the deadlock path described in the PR cannot occur.
## Comparison with this PR's approach
This PR merges the queue lock into the critical section (`work_lock =
enter_critical_section() + sched_lock()`), i.e. a "remove the lock" approach.
We keep the queue spinlock and take an "remove the edge" approach:
- critical-section hold time and the scope of disabled scheduling are not
affected by queue operations;
- the spinlock semantics of queue operations are unchanged; the change is
limited to the timer replacement and the wakeup path;
- cost: wakeups for zero-delay work now go through the timer callback
instead of an immediate post — the added latency is one interrupt-handling leg
(microsecond range), not a full timer period, because zero-delay timers expire
at the current time and fire immediately. The trade-off is determinism: the
unbounded delay of "post after unlock" (dictated by the preempting task's
runtime) is replaced by the bounded, thread-preemption-free interrupt path.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]