Skip to content

mm: Default the heap alignment to 16 on 64-bit architectures. - #21961

Open
casaroli wants to merge 2 commits into
apache:masterfrom
casaroli:n222-malloc-align16-up
Open

casaroli wants to merge 2 commits into
apache:masterfrom
casaroli:n222-malloc-align16-up

Conversation

@casaroli

@casaroli casaroli commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

malloc(), calloc() and realloc() must return memory that is aligned for every type (C11 7.22.3), that is to alignof(max_align_t). On arm64, x86_64 and 64-bit RISC-V, alignof(max_align_t) is 16 (long double). CONFIG_MM_DEFAULT_ALIGNMENT has the default 8, so about half of the heap blocks are 8 modulo 16. On x86_64, an aligned SSE access (movaps, movdqa) to such a block gives a general protection fault. GCC also assumes the alignment of 16 for malloc() on these targets: at -O2 it folds (uintptr_t)malloc(n) & 15 to 0, so code that checks the alignment gets a wrong answer.

This PR makes the default 16 when CONFIG_ARCH_64BIT is set (arm64, x86_64, RV64, and sim on a 64-bit host). The other architectures keep the default 8. A configuration that sets the value keeps it. The second commit updates Documentation/os/memory/index.rst, which said that all allocations are aligned to 8 or 4 bytes.

Impact

64-bit configurations that do not set CONFIG_MM_DEFAULT_ALIGNMENT get 16-byte heap blocks. The code does not change (the same text size). The heap uses a few bytes more: at boot, +144 bytes for 58 allocations on qemu-armv8a:nsh and +232 bytes on rv-virt:nsh64; an allocation uses at most 8 bytes more. The API does not change, in flat, protected and kernel builds. No defconfig changes: no 64-bit defconfig sets the value to 8 or 16, and qemu-intel64:nxterm keeps its 64. savedefconfig gives the in-tree defconfig for qemu-armv8a nsh/knsh, rv-virt nsh64/knsh64/nsh, qemu-intel64 nsh/nxterm, mps2-an500:nsh, esp32s3-devkit:nsh and linum-stm32h753bi:littlefs. The TLSF manager (CONFIG_MM_TLSF_MANAGER) uses its own alignment for malloc() and does not change.

Testing

A new case of the mm cmocka suite checks the alignment of malloc(), calloc() and realloc() memory against alignof(max_align_t): apache/nuttx-apps#4079 (test_nuttx_mm09).

configuration master this PR
qemu-armv8a:nsh (flat, arm64, QEMU HVF) test_nuttx_mm09 FAIL: 1023 pointers not aligned 9 of 9 PASS
rv-virt:nsh64 (flat, RV64, QEMU TCG) test_nuttx_mm09 FAIL: 997 pointers not aligned 9 of 9 PASS

The user heap of the kernel build (qemu-armv8a:knsh) has the same problem on master: two malloc(45) blocks in a row are at 0xc00077e0 and 0xc0007818. ostest passes (exit status 0) with this PR on qemu-armv8a:nsh and qemu-armv8a:knsh. x86_64 (qemu-intel64) was not built.

tools/checkpatch.sh passes. The Documentation build (Sphinx) gives no warning for the changed page.

malloc(), calloc() and realloc() must return memory that is aligned
for every type (C11 7.22.3), that is to alignof(max_align_t).  On
arm64, x86_64 and 64-bit RISC-V, alignof(max_align_t) is 16 (long
double), but CONFIG_MM_DEFAULT_ALIGNMENT is 8, so half of the heap
blocks are 8 modulo 16.  On x86_64 an aligned SSE access to such a
block (movaps, movdqa) gives a general protection fault.  GCC also
assumes the alignment of 16: at -O2 it folds (uintptr_t)malloc(n) & 15
to 0.

Make the default 16 when ARCH_64BIT is set.  The default of the other
architectures and the value of configurations that set it do not
change.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
The memory management page says that all allocations are aligned to
8 or 4 bytes.  The alignment is CONFIG_MM_DEFAULT_ALIGNMENT: 16 by
default on 64-bit architectures and 8 on the others.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@github-actions github-actions Bot added Area: Documentation Improvements or additions to documentation Size: XS The size of the change in this PR is very small labels Nov 17, 2026
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: XS The size of the change in this PR is very small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants