Repository navigation
!mm: record the heap stacks in the backtrace pool - #22810
Open
huojianchaoyy wants to merge 16 commits into
Open
huojianchaoyy wants to merge 16 commits into
huojianchaoyy wants to merge 16 commits into
Conversation
|
4 of 5 tasks
huojianchaoyy
force-pushed
the
nx-mm-backtrace-pool-v1
branch
from
December 5, 2026 07:41
b69c307 to
43d8412
Compare
huojianchaoyy
marked this pull request as ready for review
December 5, 2026 07:41
huojianchaoyy
requested review from
jerpelea,
pkarashchenko,
xiaoxiang781216 and
yamt
as code owners
December 5, 2026 07:41
xiaoxiang781216
approved these changes
Dec 5, 2026
Contributor
|
@huojianchaoyy please rebase to fix the ci error. |
Every heap node and mempool record kept its own array of CONFIG_MM_RECORD_STACK return addresses, although most allocations come from a few call paths. Record the stack in the backtrace pool of libc (backtrace_record(), sized by CONFIG_LIBC_BACKTRACE_DEPTH), which keeps each distinct stack once with a reference count, and store only the pointer to the pool entry in the node. The heap and the mempool drop the reference with backtrace_remove() when a node is freed or reallocated, and the memdump reads the stack back with backtrace_get(). CONFIG_MM_RECORD_STACK becomes a bool that depends on CONFIG_LIBC_BACKTRACE_DEPTH > 0. As in "mm: split backtrace pid and seqno", the TLSF heap records the stack when backtrace recording of the heap is on, without looking up the TCB, and the optional backtrace column of the memdump header is a macro. BREAKING CHANGE: CONFIG_MM_RECORD_STACK is a bool now, the backtrace depth is CONFIG_LIBC_BACKTRACE_DEPTH, and the backtrace[] array of struct mempool_record_s is replaced by the pointer "stack" to a backtrace pool entry. Quick fix: replace CONFIG_MM_RECORD_STACK=N with CONFIG_MM_RECORD_STACK=y and CONFIG_LIBC_BACKTRACE_DEPTH=N, and get the stack of a record with backtrace_get(record->stack, &depth). Assisted-by: Kiro:claude-opus-5-5 Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
With CONFIG_MM_RECORD_STACK the record of a TLSF block holds an entry of the backtrace pool, but: - memdump_backtrace() left the entry unset when the heap does not record backtraces, and the memdump then passed whatever the block held to backtrace_get(); - it skipped CONFIG_LIBC_BACKTRACE_DEPTH frames instead of CONFIG_MM_RECORD_STACK_SKIP, which drops the callers of malloc() from the record; - freeing or reallocating a block did not release its entry, so the pool kept the entries of all stacks that ever allocated and their reference counts only grew. Set the entry to NULL when the heap does not record backtraces, skip CONFIG_MM_RECORD_STACK_SKIP frames as the default heap does, and release the entry when the block is freed and after a successful tlsf_realloc(). Assisted-by: Kiro:claude-opus-5-5 Signed-off-by: huojianchao <huojianchao@xiaomi.com>
A heap node and a mempool record now keep the stack as an entry of the backtrace pool, of at most CONFIG_LIBC_BACKTRACE_DEPTH frames, and CONFIG_MM_RECORD_STACK only turns the record on. Read the stack of a node from its entry, take the depth from CONFIG_LIBC_BACKTRACE_DEPTH, and add the "backtracepool" command, which lists the entries of the pool with their reference counts. Assisted-by: Kiro:claude-opus-5-5 Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
Without CONFIG_LIBC_BACKTRACE_DEPTH, backtrace_get() is a macro that sets the size and evaluates to the int 0, while the function returns a pointer. Assigning the result to a pointer then warns: mempool/mempool.c:212:13: warning: assignment to 'void **' from 'int' makes pointer from integer without a cast [-Wint-conversion] Let the macro evaluate to NULL after setting the size. Assisted-by: Kiro:claude-opus-5-5 Signed-off-by: ligd <liguiding1@xiaomi.com>
mempool_record() read the task id into a local variable for the pid record and the backtrace. Since the backtrace comes from the backtrace pool, only the pid record uses it, and with CONFIG_MM_RECORD_STACK but without CONFIG_MM_RECORD_PID the variable is unused: mempool/mempool.c:108:9: warning: unused variable 'pid' [-Wunused-variable] Read the task id where the pid is recorded. Assisted-by: Kiro:claude-opus-5-5 Signed-off-by: ligd <liguiding1@xiaomi.com>
With CONFIG_MM_RECORD_STACK, mempool_release() now records the block as mempool_allocate() does, with the magic of a free block: the pid, the sequence number and the stack of the caller that freed it, so that a later use of the free block can be traced back to its release. mempool_record() takes the magic to set and drops the stack it held before, mempool_allocate() checks the free magic itself, and new blocks start without a stack. Assisted-by: Kiro:claude-opus-5-5 Signed-off-by: wangmingrong1 <wangmingrong1@xiaomi.com>
When a block is released, mempool_record() overwrote the pid and the stack of its allocation. Keep them, and record the release in two new members of struct mempool_record_s instead: freepid (with CONFIG_MM_RECORD_PID) and freestack (with CONFIG_MM_RECORD_STACK). "memdump free" prints them for the free mempool blocks, and the stack of an allocated block is only formatted when backtrace_get() returns one. The free stack is dropped from the backtrace pool before the next release records a new one, and mempool_record() no longer passes record->stack to backtrace_remove() when there is no stack member. Assisted-by: Kiro:claude-opus-5-5 Signed-off-by: huojianchao <huojianchao@xiaomi.com>
backtrace_get() returns NULL and leaves the size alone when a node has no stack, e.g. because backtrace recording was off when it was allocated. The memdump of the heap and of the TLSF heap then passed an uninitialized size and a NULL stack to backtrace_format(). Format the stack only when there is one. Assisted-by: Kiro:claude-opus-5-5 Signed-off-by: huojianchao <huojianchao@xiaomi.com>
should call mempool_record only when defined CONFIG_MM_RECORD to avoid the record->magic is not set to MEMPOOL_MAGIC_FREE when mempool_release() Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
The record code of mempool_release() is inside #ifdef CONFIG_MM_RECORD already, so the inner #ifdef CONFIG_MM_RECORD around mempool_record() is always true. Remove it. Assisted-by: Kiro:claude-opus-5-5 Signed-off-by: ligd <liguiding1@xiaomi.com>
Indent the preprocessor directives that are nested in other conditionals, merge the nested CONFIG_FS_PROCFS and CONFIG_BUILD_FLAT/__KERNEL__ conditions of mm_uninitialize(), use "#ifndef CONFIG_SMP" in mm_lock(), test the delay list with !!tmp, keep CONFIG_DEBUG_MM of the allocation failure path on the warning only (the dump and panic options there depend on CONFIG_DEBUG_MM themselves), put the TLSF prototypes and the "Private Functions" banner in front of the first private function, and drop two stray blank lines. No functional change. Assisted-by: Kiro:claude-opus-5-5 Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
mempool_release() recorded the release with the pool spinlock held. With CONFIG_MM_RECORD_STACK the record goes to the backtrace pool, which grows with malloc() when it is full, and that malloc() can come back to the same mempool and take its lock again: 1. mempool lock acquire 2. backtrace record 3. backtrace expand 4. malloc Take the lock after the record. Assisted-by: Kiro:claude-opus-5-5 Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
The macro values of CONFIG_MM_RECORD_STACK and CONFIG_LIBC_BACKTRACE_DEPTH can be parsed wrongly. Take CONFIG_MM_RECORD_STACK from the stack field of struct mm_freenode_s and the depth from the size of the stack array of struct backtrace_entry_s, and make utils.get_field_nitems() return 0 instead of None for a missing field. Assisted-by: Kiro:claude-opus-5-5 Signed-off-by: xuxingliang <xuxingliang@xiaomi.com>
…p free"
the output of command "memdump free":
before:
PID Size Overhead Sequence Address Backtrace
98576 16 0x44ce5c60
after:
Size Overhead Address
104 16 0x41dcef98
Signed-off-by: anpeiyun <anpeiyun@xiaomi.com>
The memory pool diagnostics paragraph gave the stack record as a CONFIG_MM_RECORD_STACK depth above 0. CONFIG_MM_RECORD_STACK is a bool now and the depth of the recorded stacks is CONFIG_LIBC_BACKTRACE_DEPTH. Assisted-by: Kiro:claude-opus-5-5 Signed-off-by: huojianchao <huojianchao@xiaomi.com>
A free node of the mm heap keeps no stack record: after the free the slot of the "stack" pointer is reused by the free list links, so the backtrace property of nxgdb can be handed an arbitrary value, and dereferencing it fails the free node search with a gdb.MemoryError. Catch the error and fall back to an empty backtrace, and make BacktraceEntry.format() return an empty string for the null entry it can now be called with. Signed-off-by: huojianchao <huojianchao@xiaomi.com>
huojianchaoyy
force-pushed
the
nx-mm-backtrace-pool-v1
branch
from
December 5, 2026 13:19
43d8412 to
721cc6b
Compare
xiaoxiang781216
approved these changes
Dec 5, 2026
GUIDINGLI
approved these changes
Dec 5, 2026
Contributor
please rebase again. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note: Please adhere to Contributing Guidelines.
The third of three PRs of the Xiaomi Vela memdump record changes, on top of the merged #22802 and #22809; rebased on current master.
Summary
The heap and the mempool keep the recorded stacks in the backtrace pool of libc, as in Xiaomi Vela: each distinct stack is stored once with a reference count, and a node keeps only a pointer to its entry.
CONFIG_MM_RECORD_STACKbecomes a bool onCONFIG_LIBC_BACKTRACE_DEPTH > 0. As in !mm: split CONFIG_MM_BACKTRACE into CONFIG_MM_RECORD_PID, _SEQNO and _STACK #22802, the TLSF heap records the stack when the backtrace recording of the heap is on, without looking up the TCB.CONFIG_LIBC_BACKTRACE_DEPTHframes instead ofCONFIG_MM_RECORD_STACK_SKIP, and never released the entry on free or realloc.backtracepoolcommand. The Vela commit also changes two GDB commands that only Vela has (memory visualize, memclassify.py); those hunks are left out.unused variable 'pid'warning of !mm: split CONFIG_MM_BACKTRACE into CONFIG_MM_RECORD_PID, _SEQNO and _STACK #22802).CONFIG_MM_RECORD_STACK).CONFIG_MM_RECORD_STACKis a bool now, the backtrace depth isCONFIG_LIBC_BACKTRACE_DEPTH, and the backtrace[] array of a heap node and of struct mempool_record_s is replaced by the pointer "stack" to a backtrace pool entry. Quick fix: replaceCONFIG_MM_RECORD_STACK=NwithCONFIG_MM_RECORD_STACK=yandCONFIG_LIBC_BACKTRACE_DEPTH=N, and get the stack of a record withbacktrace_get(record->stack, &depth). The first commit carries the marking.Impact
CONFIG_MM_RECORD_STACKpointers (the heap node overhead on sim:nsh with pid + seqno + stack drops from 96 to 40 bytes); the pool itself grows with the number of distinct stacks.memdump freeshows them).Testing
Host: Ubuntu 22.04 x86_64, GCC 13.4.0 (sim), Arm GNU Toolchain 13.2.rel1, x86_64-none-elf GCC, CMake 3.23 + Ninja 1.10. Base: master (post #22802 and #22809).
./tools/checkpatch.sh -c -u -m -g HEAD~15..HEADpasses, and-bpasses for the marked commit.Every commit was built (CMake) for
sim:nshwith the heap mempool on in the ten record configurations of #22802 (a stack record asLIBC_BACKTRACE_DEPTH=8andMM_RECORD_STACK=yfrom the first commit on): the head builds in all ten without new warnings. "mempool: Support recording backtrace when node is free" alone does not build with the pid record andLIBC_BACKTRACE_DEPTH > 0but no stack record ('struct mempool_record_s' has no member named 'stack'); the next commit fixes it. From the first commit until "mempool: fix warning", a stack record without the pid record warnsunused variable 'pid', as in Vela.The head also builds for
sim:smpandqemu-armv7a:nshwithSCHED_BACKTRACE=yand pid + seqno + stack, forsim:adb,sim:asan(also with pid + seqno) and with Make forsim:nshwithEXTRAFLAGS="-Wno-cpp -Werror"; with apache/nuttx-apps#4279,sim:citestandqemu-intel64:citest_smpbuild with the CI flags.cmocka_mm_testpasses onsim:citestwith and without pid + seqno + stack (12 tests).sim:nshwith pid + seqno + stack, default heap and TLSF: the memdump commands of #22802 run without assertions, and the heap node overhead is 40 bytes instead of 96. With the recording switched on (echo -n on > /proc/memdump), the nodes that a new task allocates carry their 8-entry backtraces.sim:ostestwith one loop (pid + seqno):ostest_main: Exiting with status 0.TLSF with pid + seqno + stack and
MM_RECORD_STACK_DEFAULT=y, after 20ls /and a memdump: the backtrace pool holds 87 entries with the first commit alone (the entries of freed blocks stay, their reference counts grow) and 54 with "mm/tlsf: clear and release the stack record of the backtrace pool".nxgdb on
sim:nshwith pid + seqno + stack (built with-g3):mm heapandmm dumpwork (32 bytes of overhead per node instead of 88 on master), andbacktracepoollists the entries of the pool.Not tested: real hardware.