Skip to content

mm: support task heap - #22619

Open
anjiahao1 wants to merge 7 commits into
apache:masterfrom
anjiahao1:20261130_mm-task-heap
Open

anjiahao1 wants to merge 7 commits into
apache:masterfrom
anjiahao1:20261130_mm-task-heap

Conversation

@anjiahao1

@anjiahao1 anjiahao1 commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Add a per-task-group heap (CONFIG_MM_TASK_HEAP). A task group may own a
private heap carved from the user heap, and the user-space allocators
(USR_HEAP via umm_getheap()) switch to it for the running task. The
series is ported from the Vela tree as a sequence of cherry-picks, with
the adaptations required by the current tree folded into the commits.

  • new CONFIG_MM_TASK_HEAP with MM_TASK_HEAP_DEFAULT_SIZE and
    MM_TASK_HEAP_DEFAULT_ALIGN. The default size is 0, which shares the
    user heap; a board can configure a private default.
  • the task information carries ta_heap; struct task_group_s gains
    tg_heap. group_allocate() sizes the heap, and a vfork() child
    shares the heap of its parent.
  • group_malloc(), group_realloc(), group_zalloc(), group_free()
    and umm_getheap() use the task heap.
  • the environment is allocated from the heap of the task it belongs to.
  • binfmt/builtin and posix_spawn() propagate the configured
    heapsize to the new task. nxtask_init() keeps its existing
    signature, and the new nxtask_init_with_heapsize() entry point is
    used by callers that have a heapsize, so out-of-tree callers such as
    the pinned Espressif HAL glue keep building.

Impact

New option, disabled by default; existing configurations are unchanged.

Known limitations:

  • only builds with a separate kernel heap (CONFIG_MM_KERNEL_HEAP) may
    enable the option, otherwise the kmm_* allocators would route
    kernel allocations into the task heap;
  • BUILD_KERNEL is not supported yet;
  • arch/arm64/src/common/arm64_addrenv_mpu.c already contains
    CONFIG_MM_TASK_HEAP blocks that were never compiled because the
    symbol did not exist; enabling the option together with
    ARCH_USE_MPU is not covered by this series;
  • moving task stacks into the group heap depends on the task-group
    lifetime rework and is left for a follow-up.

Testing

  • Built with cmake for sim:nsh, and with make for
    mps3-an547:nsh, mps3-an547:knsh and mps3-an547:knsh with
    CONFIG_MM_TASK_HEAP=y and CONFIG_MM_TASK_HEAP_DEFAULT_SIZE=65536.
  • Built with make for qemu-armv7a:citest_knsh (CONFIG_BUILD_KERNEL=y,
    task heap disabled) to cover the kernel-build path.
  • Runtime, qemu-system-arm -M mps3-an547 (protected build, kernel +
    user blob): nsh prompt and hello run with a 64 KiB per-task heap,
    no assertion failures.
  • The QEMU console of this board needs the RX/TX interrupt assignment
    swapped (TX=49, RX=50) on QEMU 8.x; the defaults in the tree match the
    ARM documentation and are left unchanged.

GUIDINGLI
GUIDINGLI previously approved these changes Nov 30, 2026
GUIDINGLI
GUIDINGLI previously approved these changes Nov 30, 2026
@github-actions github-actions Bot added Arch: xtensa Issues related to the Xtensa architecture Size: L The size of the change in this PR is large Area: BINFMT labels Nov 30, 2026
@anjiahao1
anjiahao1 force-pushed the 20261130_mm-task-heap branch from 5f8c883 to 0df419f Compare November 30, 2026 18:38
@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .text +102 B (+0.1%, 68,984 B / 262,144 B, total: 26% used)

esp32-devkitc

  • ROM: .flash.text +56 B (+0.0%, 135,044 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text +56 B (+0.1%, 100,696 B / 3,342,304 B, total: 3% used)

mirtoo

  • kseg0_progmem: .text +88 B (+0.1%, 63,844 B / 131,072 B, total: 49% used)

mps2-an500-citest

  • flash: .text +56 B (+0.0%, 254,556 B / 4,194,304 B, total: 6% used)

mps2-an500-citest-protected

  • flash: .text +64 B (+0.1%, 122,656 B / 4,194,304 B, total: 3% used)

qemu-armv8a

  • Code: .text.nx_start +8 B, .text.nxtask_init -344 B, .text.nxtask_setup_fork +8 B (+0.0%, 387,624 B)

qemu-intel64

  • Code: .text +49 B (+0.0%, 8,711,235 B)

rx65n-rsk2mb

  • ROM: .text +48 B (+0.1%, 95,072 B / 2,097,152 B, total: 5% used)

s698pm-dkit

  • Code: .text +128 B (+0.0%, 416,816 B)

stm32-nucleo-f103rb

  • flash: .text +48 B (+0.1%, 34,612 B / 131,072 B, total: 26% used)

No memory changes detected for:


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

@anjiahao1
anjiahao1 force-pushed the 20261130_mm-task-heap branch from 0df419f to 9686103 Compare November 30, 2026 21:12
@anjiahao1

Copy link
Copy Markdown
Contributor Author

The Linux (sim-03) failure is not related to this PR. sim:zblue fails
to link with undefined references to work_queue_init() and
work_queue_run() from
apps/wireless/bluetooth/zblue/port/src/work.c: the apps tree started
calling these functions in apache/nuttx-apps#4129 (merged 2026-11-30),
while the NuttX-side implementation is still pending in
#22268. Any NuttX PR that builds sim:zblue currently hits
the same error; nothing in this series touches wqueue or zblue.

@anjiahao1

Copy link
Copy Markdown
Contributor Author

The Linux (arm-sam) failure is also not related to this PR. The
sam3u-ek/knsh build overflows the kflash region by 212 bytes, but an
unrelated PR (run 46507824021, head 206db14) hits the same overflow
(148 bytes) on its own run against the same master line, so the current
master state is already over the region limit for this configuration.
The last green Linux (arm-sam) job was on master commit 60b2782, which
is no longer part of master after the recent history rewrite. Nothing in
this series touches the SAM3U board or the linker regions.

@anjiahao1
anjiahao1 force-pushed the 20261130_mm-task-heap branch from 9686103 to df4ca23 Compare December 1, 2026 04:38
@github-actions github-actions Bot removed the Arch: xtensa Issues related to the Xtensa architecture label Dec 1, 2026
Use MM_TASK_HEAP to control whether a task uses an internal task-specific heap.
Use MM_TASK_HEAPSIZE to define the default heap size for each task.
Additional APIs will be added later to assign a specific heap to a task

Each task will allocate its own task heap from the user heap.
The task heap is initialized when the task is initialized,
and the task's stack is allocated from the task heap.
The pthreads belonging to this task also use the task heap.
This ensures isolation between task heaps.

flat build and protect build can use task heap,
heap view like this:
|-kernal-|------------user heap----------------|
         |-task A heap--||-task B heap--|-free-|
         |-stack-|-heap-||-stack-|-heap-|

Next:
so if have mpu, We can change mpu region, when switch context,
it can protect task heap

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
The heapsize travels from the spawn attributes into the task
initialization so that the new group can size its heap accordingly.

nxtask_init() keeps its existing signature for callers that do not
pass a heap size, including the Espressif HAL glue that builds
against a fixed third-party revision.  nxtask_init_with_heapsize()
is the entry point for callers that do.

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
The region needs to be added to the public user heap
umm_addregion call when system starup, it mean add memroy to umm not task heap。
USR_HEAP is task heap when use CONFIG_TASK_HEAP

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
The builtin loader ignored the heapsize from the builtin table when
CONFIG_MM_TASK_HEAP is enabled, so the task heap size configured in
the app table was lost after loading. Copy builtin->heapsize to
binp->heapsize so the loader creates the task with the configured
heap size.

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
The init thread started by nx_start_application always uses the
default task heap size. Add CONFIG_INIT_HEAPSIZE so boards can
configure the init thread heap independently when MM_TASK_HEAP
is enabled.

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
A task heapsize of zero now shares the user heap instead of carving and
initializing a private one.  MM_TASK_HEAP_DEFAULT_SIZE defaults to zero,
so spawned tasks without an explicit heapsize share the user heap unless
a board sets a nonzero default.

group_heap_uninitialize never uninitializes the shared user heap, and
task_fork keeps the parent's sharing choice for the child.

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
The environment belongs to the task information of the task it describes,
which may not be the running task: nxtask_init() copies the environment of
the parent.  Under the task heap, allocate it from that task's heap rather
than the heap of whichever task happens to be running.

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
@anjiahao1
anjiahao1 force-pushed the 20261130_mm-task-heap branch from df4ca23 to a90b5a8 Compare December 2, 2026 08:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: BINFMT Size: L The size of the change in this PR is large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants