zyfeier opened a new pull request, #21774:
URL: https://github.com/apache/nuttx/pull/21774
## Summary
* Why: code that keeps a TCB pointer across a call that may release the
task currently relies on holding a big lock around the whole region; replacing
those big locks with finer-grained ones would risk lock recursion.
* What: give the TCB a reference count. `nxsched_get_tcb()` now takes a
reference on the TCB it returns and `nxsched_put_tcb()` drops it, so a TCB
pointer stays valid for as long as the reference is held.
* How: take the reference wherever code keeps a TCB pointer across a
potentially blocking operation (signal delivery, POSIX timers, work queues,
pthread join/cancel, child reaping, gdbstub, exception reporting). The last put
posts `exit_sem`, letting the exiting task finish releasing itself once all
holders are gone. `nxsched_releasepid()` is split out of
`nxsched_release_tcb()` so the pid hash entry and the TCB can be released
independently. Probe-style lookups that only test task existence use the new
`nxsched_verify_pid()` instead, so they do not pin a reference.
## Impact
* Is new feature added? YES - internal TCB lifetime management (no
user-visible API change).
* Impact on user: NO
* Impact on build: NO
* Impact on hardware: exception/fatal paths on arm64, armv7-a, risc-v and
x86_64 now drop the reference they take; no board changes.
* Impact on documentation: NO
* Impact on security: NO
* Impact on compatibility: NO
## Testing
I confirm that changes are verified on local setup and works as intended:
* Build Host(s): Linux x86_64 (Ubuntu 24.04), host GCC 13.3 for sim,
aarch64-linux-gnu-gcc 13.3 for qemu
* Target(s): sim:ostest, qemu-armv8a:nsh_smp (4 CPUs, GICv3)
* The sim:ostest configuration has CONFIG_MM_KASAN=y enabled; all runs
below are under KASAN.
* Note: the local qemu build uses an aarch64-linux-gnu toolchain which
needs a one-line `__getauxval()` link shim on the host; the shim is local-only
and not part of this patch.
Testing logs (build, qemu-armv8a:nsh_smp):
```
IN: fs/libfs.a -> staging/libfs.a
IN: sched/libsched.a -> staging/libsched.a
IN: libs/libc/libc.a -> staging/libc.a
IN: .../nuttx-apps/libapps.a -> staging/libapps.a
LD: nuttx
```
Testing logs (runtime, sim:ostest with 1 loop):
```
End of test memory usage:
VARIABLE BEFORE AFTER
======== ======== ========
arena 3effff8 3effff8
uordblks 45190 454e8
user_main: Exiting
ostest_main: Exiting with status 0
```
ostest with 5 loops was run 4 times on this branch (7151/7230/7203/7181
lines of output, exit 0 each, zero asserts, zero FAILED) and once on unpatched
master (7216 lines); diffing the two outputs after filtering timing-sensitive
lines (hrtimer latency warnings, spinlock throughput, timestamps) leaves zero
structural difference.
Testing logs (runtime, qemu-armv8a:nsh_smp SMP test, 4 CPUs):
```
Main[0]: Thread 1 created
...
Main[0]: Thread 8 created
Main[0]: Now running on CPU0
Main[0]: Thread 1 completed with result=0
Main[0]: Thread 2 completed with result=0
...
Main[0]: Thread 8 completed with result=0
```
Threads were observed migrating between CPU0 and CPU1 during the run.
Local CI check `tools/checkpatch.sh -c -u -m -g HEAD~1..HEAD` passes.
--
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]