Repository navigation
Detect the effective virtual-address width at runtime instead of assu… - #1282
Open
hrabanazviking wants to merge 1 commit into
Open
hrabanazviking wants to merge 1 commit into
hrabanazviking wants to merge 1 commit into
Conversation
…ming 48 bits on aarch64 tcmalloc hardcodes a 48-bit virtual address space on Linux/aarch64 (kAddressBits = 48 in tcmalloc/internal/config.h). On kernels built with a narrower user VA width -- e.g. Raspberry Pi OS and Android GKI, which ship CONFIG_ARM64_VA_BITS=39 -- every mmap hint RandomMmapHint() generates (up to 2^47, with memory-tag bits at bit 42) is rejected by the kernel. After 1000 retries MmapAligned() returns null and the process aborts during allocator init. This makes tcmalloc unusable on these kernels with no workaround short of rebuilding the kernel. This change probes the running kernel's user VA width at runtime (reading the most-significant set bit of the stack pointer, the same probe ThreadSanitizer uses; no syscalls, no /proc parsing), clamped to [39, kAddressBits], and places mmap hints and memory-tag bits accordingly. A single binary now works on 39-bit, 42-bit, and 48-bit aarch64 kernels; on 48-bit kernels the behavior is bit-identical to before. It also implements the advice the OOM error message itself gives: the message suggests rebuilding "with TCMALLOC_ADDRESS_BITS defined", but no such macro existed anywhere in the tree. It does now (aarch64/Linux), lowering the compile-time maximum and shrinking the page map for builders who want a fixed width. Test evidence: - config_test and system_allocator_test pass on x86_64 Linux (48-bit kernel; patched lib reports bits=48/shift=42 -- identical to pristine). - A/B verification on a Raspberry Pi 5 (kernel 6.12, 39-bit VA): pristine library aborts with the exact reported crash; patched library reports EffectiveAddressBits()=39, TagShift()=35, and all checks pass. Fixes google#82.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Author
|
I signed the agreement. Don't think it is recognizing that though. |
Author
ckennelly
reviewed
Oct 5, 2026
| static_cast<uintptr_t>(MemoryTag::kNormal) << TagShift(); | ||
| // NB: dynamic (not constexpr): the tag placement now depends on the | ||
| // runtime-detected address width (see TagShift()/TagMask()). | ||
| static const uintptr_t kColdMask = static_cast<uintptr_t>(MemoryTag::kCold) |
Collaborator
There was a problem hiding this comment.
This is potentially unsafe: The initializer for this can happen after we've attempted the first allocation.
It also has significant performance consequences, since we need to do a load for many instructions when an immediate would have previously sufficed.
This branch has not been deployed
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.

…ming 48 bits on aarch64
tcmalloc hardcodes a 48-bit virtual address space on Linux/aarch64 (kAddressBits = 48 in tcmalloc/internal/config.h). On kernels built with a narrower user VA width -- e.g. Raspberry Pi OS and Android GKI, which ship CONFIG_ARM64_VA_BITS=39 -- every mmap hint RandomMmapHint() generates (up to 2^47, with memory-tag bits at bit 42) is rejected by the kernel. After 1000 retries MmapAligned() returns null and the process aborts during allocator init. This makes tcmalloc unusable on these kernels with no workaround short of rebuilding the kernel.
This change probes the running kernel's user VA width at runtime (reading the most-significant set bit of the stack pointer, the same probe ThreadSanitizer uses; no syscalls, no /proc parsing), clamped to [39, kAddressBits], and places mmap hints and memory-tag bits accordingly. A single binary now works on 39-bit, 42-bit, and 48-bit aarch64 kernels; on 48-bit kernels the behavior is bit-identical to before.
It also implements the advice the OOM error message itself gives: the message suggests rebuilding "with TCMALLOC_ADDRESS_BITS defined", but no such macro existed anywhere in the tree. It does now (aarch64/Linux), lowering the compile-time maximum and shrinking the page map for builders who want a fixed width.
Test evidence:
Fixes #82.