Repository navigation
Conversation
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>
GUIDINGLI
approved these changes
Nov 17, 2026
xiaoxiang781216
approved these changes
Nov 17, 2026
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.
Summary
malloc(),calloc()andrealloc()must return memory that is aligned for every type (C11 7.22.3), that is toalignof(max_align_t). On arm64, x86_64 and 64-bit RISC-V,alignof(max_align_t)is 16 (long double).CONFIG_MM_DEFAULT_ALIGNMENThas 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 formalloc()on these targets: at -O2 it folds(uintptr_t)malloc(n) & 15to 0, so code that checks the alignment gets a wrong answer.This PR makes the default 16 when
CONFIG_ARCH_64BITis 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_ALIGNMENTget 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.savedefconfiggives 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 formalloc()and does not change.Testing
A new case of the mm cmocka suite checks the alignment of
malloc(),calloc()andrealloc()memory againstalignof(max_align_t): apache/nuttx-apps#4079 (test_nuttx_mm09).test_nuttx_mm09FAIL: 1023 pointers not alignedtest_nuttx_mm09FAIL: 997 pointers not alignedThe 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.