Skip to content

!mm: record the heap stacks in the backtrace pool - #22810

Open
huojianchaoyy wants to merge 16 commits into
apache:masterfrom
huojianchaoyy:nx-mm-backtrace-pool-v1
Open

huojianchaoyy wants to merge 16 commits into
apache:masterfrom
huojianchaoyy:nx-mm-backtrace-pool-v1

Conversation

@huojianchaoyy

@huojianchaoyy huojianchaoyy commented Dec 3, 2026 •

Copy link
Copy Markdown

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.

  • !mm: use backtrace pool to replace mm backtrace: backtrace_record() / backtrace_remove() / backtrace_get() replace the per-node array; CONFIG_MM_RECORD_STACK becomes a bool on CONFIG_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.
  • mm/tlsf: clear and release the stack record of the backtrace pool: a fix of the previous commit, which Vela has as well: the TLSF record left the entry unset when the heap does not record backtraces (the memdump then passed whatever the block held to backtrace_get()), skipped CONFIG_LIBC_BACKTRACE_DEPTH frames instead of CONFIG_MM_RECORD_STACK_SKIP, and never released the entry on free or realloc.
  • pynuttx: use backtrace pool to replace mm backtrace: nxgdb reads the stack from the pool entry and gets a backtracepool command. The Vela commit also changes two GDB commands that only Vela has (memory visualize, memclassify.py); those hunks are left out.
  • execinfo.h: fix warning and mempool: fix warning: the backtrace_get() stub evaluates to NULL, and the task id is read where the pid is recorded (this also removes the seqno-only unused variable 'pid' warning of !mm: split CONFIG_MM_BACKTRACE into CONFIG_MM_RECORD_PID, _SEQNO and _STACK #22802).
  • mempool: Support recording backtrace when node is free, then mempool: add freestack record and mm: memdump backtrace access null pointer bugfix: a released block records the release (pid, stack) in its own members, and the memdump formats a stack only when there is one. The last two come later in Vela and are moved up to follow the commit they fix (it left a backtrace_remove(record->stack) outside CONFIG_MM_RECORD_STACK).
  • mm/mempool/mempool.c: fix the record magic value error, mempool: remove unsed code, mm: Fix the coding style issue, mempool: fix recursive malloc in backtrace (the release is recorded before the pool lock is taken: growing the backtrace pool can malloc() from the same mempool), nxgdb/mm: fix wrong macro parsed and mm/mm_heap/mm_memdump.c: correct the output format of command "memdump free": Vela fixes of this code.
  • Documentation/os/memory: CONFIG_MM_RECORD_STACK is a bool.
  • BREAKING CHANGE: CONFIG_MM_RECORD_STACK is a bool now, the backtrace depth is CONFIG_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: 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). The first commit carries the marking.

Impact

  • Kconfig and struct members as in the BREAKING CHANGE line; no in-tree configuration sets a stack record, and tools/pynuttx and Documentation/os/memory follow.
  • Memory: a node with a stack record keeps a pointer instead of CONFIG_MM_RECORD_STACK pointers (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.
  • A released mempool block keeps the pid and stack of its allocation and records the release separately (memdump free shows 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..HEAD passes, and -b passes for the marked commit.
Every commit was built (CMake) for sim:nsh with the heap mempool on in the ten record configurations of #22802 (a stack record as LIBC_BACKTRACE_DEPTH=8 and MM_RECORD_STACK=y from 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 and LIBC_BACKTRACE_DEPTH > 0 but 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 warns unused variable 'pid', as in Vela.
The head also builds for sim:smp and qemu-armv7a:nsh with SCHED_BACKTRACE=y and pid + seqno + stack, for sim:adb, sim:asan (also with pid + seqno) and with Make for sim:nsh with EXTRAFLAGS="-Wno-cpp -Werror"; with apache/nuttx-apps#4279, sim:citest and qemu-intel64:citest_smp build with the CI flags. cmocka_mm_test passes on sim:citest with and without pid + seqno + stack (12 tests).
sim:nsh with 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:ostest with one loop (pid + seqno): ostest_main: Exiting with status 0.
TLSF with pid + seqno + stack and MM_RECORD_STACK_DEFAULT=y, after 20 ls / 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:nsh with pid + seqno + stack (built with -g3): mm heap and mm dump work (32 bytes of overhead per node instead of 88 on master), and backtracepool lists the entries of the pool.
Not tested: real hardware.

@github-actions github-actions Bot added Area: Documentation Improvements or additions to documentation Arch: simulator Issues related to the SIMulator Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces. Board: arm Board: simulator Board: xtensa labels Dec 3, 2026
@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

esp32-devkitc

  • ROM: .flash.rodata +16 B, .flash.text +32 B (+0.0%, 138,052 B / 4,194,272 B, total: 3% used)
  • drom0_0_seg: .flash.rodata +16 B (+0.1%, 14,060 B / 4,194,272 B, total: 0% used)
  • irom0_0_seg: .flash.text +32 B (+0.0%, 104,156 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text +52 B (+0.1%, 88,452 B / 4,194,304 B, total: 2% used)

mps2-an500-citest

  • flash: .text +40 B (+0.0%, 262,052 B / 4,194,304 B, total: 6% used)

mps2-an500-citest-protected

  • flash: .text +40 B (+0.0%, 125,996 B / 4,194,304 B, total: 3% used)

qemu-armv8a

  • Code: .text.mm_memdump +60 B (+0.0%, 398,132 B)

qemu-intel64

  • Code: .text +105 B (+0.0%, 8,789,910 B)

s698pm-dkit

  • Code: .text +80 B (+0.0%, 429,072 B)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@huojianchaoyy
huojianchaoyy force-pushed the nx-mm-backtrace-pool-v1 branch from b69c307 to 43d8412 Compare December 5, 2026 07:41
@huojianchaoyy
huojianchaoyy marked this pull request as ready for review December 5, 2026 07:41
@github-actions github-actions Bot added Size: M The size of the change in this PR is medium and removed Arch: simulator Issues related to the SIMulator Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces. Board: arm Board: simulator Board: xtensa labels Dec 5, 2026
@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@huojianchaoyy please rebase to fix the ci error.

Gary-Hobson and others added 8 commits December 5, 2026 21:19
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>
CV-Bowen and others added 8 commits December 5, 2026 21:19
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
huojianchaoyy force-pushed the nx-mm-backtrace-pool-v1 branch from 43d8412 to 721cc6b Compare December 5, 2026 13:19
@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@huojianchaoyy please rebase to fix the ci error.

please rebase again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Documentation Improvements or additions to documentation Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants