zyfeier opened a new pull request, #22180:
URL: https://github.com/apache/nuttx/pull/22180

   ## Summary
   
   **Why.** Some memory cannot be freed in the context that drops its last 
user: a TCB, for example, is released while the scheduler holds a spinlock or 
runs on the stack of the dying task. The heap already queues such blocks on a 
delay list and frees them on a later allocation, but there is no public API for 
it, the delay list cannot run a callback before the block is freed, and a 
mempool block put on the delay list is later freed as if it were a heap chunk.
   
   **What.** Add a delay free API with an optional callback, and make the delay 
list return mempool blocks to their pool.
   
   **How.**
   1. `mm/mempool: add mempool_multiple_member() api`: tell whether an address 
belongs to a multiple mempool (used by the delay list debug check).
   2. `delayfree: add kmm_delayfree kumm_delayfree mm_delayfree api`: add 
`mm_delayfree()`, `kmm_delayfree()`, `umm_delayfree()`, `kumm_delayfree()`, and 
split the immediate free out of `mm_delayfree(heap, mem, delay)` into 
`mm_forcefree()` (tlsf and sim heap: `forcefree()`), which tries the mempool 
first. When `mm_lock()` fails (interrupt context or a context switch in 
progress) the block is queued without being filled or poisoned, as before.
   3. `kmm_free: delete check, check in mm_free and mm_delayfree`: drop a 
duplicate `heapmember` assertion.
   4. `mm/tlsf: fix compile error`: unused variable.
   5. `mm/delaylist: reduce use of this_cpu, only when SMP`: per-CPU delay list 
heads only on SMP.
   6. `mm: add delay callback mechanism for safe deferred execution`: add 
`mm_delayfreecb()`, `kmm_delayfreecb()`, `umm_delayfreecb()`; the callback runs 
when the delay list is drained, then the block is freed.
   7. `mm: fill a delayed block before linking it on the delay list`: the fill 
overwrote the list link and callback just written.
   8. `mm: fill each freed block only once on the delay list path`: with 
`CONFIG_MM_FREE_DELAYCOUNT_MAX > 0` a block was filled twice and the second 
fill wrote into poisoned memory (KASAN report). `mm_free()` again returns a 
mempool block to its pool directly; only blocks freed through the delay free 
API stay on the delay list.
   
   Commits 7 and 8 fix problems found while testing 2 and 6 with 
`CONFIG_MM_FILL_ALLOCATIONS` and `CONFIG_MM_FREE_DELAYCOUNT_MAX > 0`.
   
   **Related work.** The first user of `kmm_delayfreecb()` is the deferred TCB 
release, which will be posted on top of this PR and of #21774 (TCB reference 
count). A later, separate change may remove the `mm_lock()` fallback once every 
caller that frees memory where the heap lock cannot be taken uses the delay 
free API; this PR keeps the fallback, so it does not depend on that change.
   
   ## Impact
   
   - Users / API: new `mm_delayfree()`, `kmm_delayfree()`, `umm_delayfree()`, 
`kumm_delayfree()`, `mm_delayfreecb()`, `kmm_delayfreecb()`, 
`umm_delayfreecb()` and `mm_delaycb_t`. No existing API changes.
   - Behavior: none for existing callers. `mm_free()` and the `mm_lock()` 
fallback behave as before.
   - Build: none. Code size: at most +24 B on the boards below.
   - Hardware, documentation, security, compatibility: none.
   - Not covered yet: the callback path has no in-tree caller in this PR; it is 
exercised by the deferred TCB release.
   
   ## Testing
   
   Host: Ubuntu x86_64, base master b2b0bde9d0f, apps 8e60baf3472. 
`checkpatch.sh -c -u -m -g` passes on every commit; every commit builds with 
the default heap and with tlsf.
   
   | Config | Result |
   |---|---|
   | sim:ostest, KASAN: default heap / tlsf | status 0 |
   | sim:ostest + MM_HEAP_MEMPOOL_THRESHOLD=4096 + MM_FREE_DELAYCOUNT_MAX=8 + 
MM_FILL_ALLOCATIONS + KASAN: default heap / tlsf | status 0, no KASAN report |
   | sim:ostest + mempool + MM_FREE_DELAYCOUNT_MAX=0 + fill | status 0 |
   | sim:ostest + MM_FREE_DELAYCOUNT_MAX=8 + fill, no mempool | status 0 |
   | sim:ostest, sim heap (MM_UMM_CUSTOMIZE_MANAGER), delay count 0 / 8 | 
status 0 |
   | sim:smp (4 CPUs) ostest: default / mempool + delay count 8 + fill + KASAN, 
default heap and tlsf | status 0, no KASAN report (master same) |
   | qemu-armv8a:nsh_smp ostest: default / mempool + delay count 8 + fill | 
status 0 (master same) |
   | qemu-intel64:citest_smp LTP kill/signal | same as master |
   | smartl-c906:knsh (kernel build) | builds |
   | stm32l476vg-disco:knsh | ksram: 8136 B 8 KB 99.32% (same as master) |
   | ea3131:pgnsh / nucleo-g431rb:nsh, cansock / b-g431b-esc1:cansock / 
stm32l0538-disco:nsh | +0..24 B, no overflow |
   
   sim:ostest, mempool + delay count 8 + fill + KASAN:
   ```
   user_main: Exiting
   ostest_main: Exiting with status 0
   ```
   sim:smp, mempool + delay count 8 + fill + KASAN:
   ```
   user_main: Exiting
   ostest_main: Exiting with status 0
   ```
   qemu-armv8a:nsh_smp, mempool + delay count 8 + fill:
   ```
   user_main: Exiting
   ostest_main: Exiting with status 0
   ```
   citest_smp LTP:
   ```
   PASS: ltp_interfaces_kill_1_1
   PASS: ltp_interfaces_kill_2_1
   PASS: ltp_interfaces_killpg_1_1
   PASS: ltp_interfaces_killpg_2_1
   ```
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
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]

Reply via email to