ext/opcache: keep huge page remap inside the reserved range - #23554
Merged
Conversation
create_segments() reserves requested_size bytes with MAP_32BIT, frees them, rounds the address up to the 2 MB huge page boundary, and then MAP_FIXED-maps requested_size bytes at the new address. The address goes up but the size stays the same, so the mapping ends up to 2 MB above the memory we reserved, and MAP_FIXED discards what is mapped there. If huge pages are available the remap succeeds and replaces that memory. If they are not, mmap() fails, but the kernel has already removed it and leaves a hole (mm/vma.c, vms_abort_munmap_vmas). On a normal host there is usually nothing above the reservation, so this is not visible. Under Rosetta 2 MAP_32BIT is not honored, the reservation lands directly below libc, and the overshoot unmaps its first pages: php-fpm then dies with SIGSEGV shortly after start. Reserve one extra huge page, so the aligned range always stays inside the reservation. zend_mm_chunk_alloc_int() already does this for 2 MB aligned chunks.
arnaud-lb
reviewed
Sep 4, 2026
arnaud-lb
left a comment
Member
There was a problem hiding this comment.
This looks good to me! In master we should try to move the munmap() after the second mmap() as it looks unnecessary and it creates a race window.
arnaud-lb
added a commit
that referenced
this pull request
Sep 4, 2026
* PHP-8.5: ext/opcache: keep huge page remap inside the reserved range (#23554)
Member
|
Thank you! |
pull Bot
pushed a commit
to AmirulAndalib/php-src
that referenced
this pull request
Sep 4, 2026
* PHP-8.4: ext/opcache: keep huge page remap inside the reserved range (php#23554)
s2x
added a commit
to s2x/php-src
that referenced
this pull request
Sep 4, 2026
create_segments() reserved requested_size + huge_page_size, unmapped the whole reservation, and only then MAP_FIXED-mapped the huge pages at the aligned address inside it. That munmap is unnecessary: MAP_FIXED replaces the overlapped part of the reservation atomically. It also opens a window in which another thread can map something at that address before the remap runs. Keep the reservation, map the huge pages into it, and release only the head and the tail that are left over. Their sizes always add up to exactly huge_page_size, because the reservation is requested_size + huge_page_size and the mapping is requested_size. When the remap fails the whole reservation is released before falling back to normal pages: the kernel may already have discarded the overlapped part, so the reservation cannot be reused. Suggested by Arnaud Le Blanc in phpGH-23554.
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.
Hi, We moved our dev machines from Docker Desktop to Colima and Podman on Apple Silicon. Some of our images are amd64 only, so we turned on Rosetta 2. After that
php-fpmstarted to crash: exit 139, one second after start, no log and no error. The same happens on Podman with Rosetta, and on Podman with QEMU.First I thought this is a Rosetta or Colima problem. It is reported like that here: abiosoft/colima#1452 . But then I saw the same crash with QEMU, so I started to look at PHP.
In
create_segments()OPcache reserves memory withMAP_32BIT, frees it, moves the address up to the 2 MB boundary, and then mapsrequested_sizeagain withMAP_FIXED. The address goes up, but the size stays the same. So the new mapping ends up to 2 MB above the memory we reserved, andMAP_FIXEDdeletes what is mapped there.Other places do this correctly.
zend_mm_chunk_alloc_int()inZend/zend_alloc.creservessize + alignment - REAL_PAGE_SIZEfirst, so its aligned address always stays inside its own memory. Andfind_prefered_mmap_base(), in this same file, aligns the address and then checks it: iflast_candidate + requested_sizedoes not fit any more, it moves one huge page down. Here I do not see either of these.My patch reserves one huge page more.
I am not sure this is the right fix, or the right place for it. It fixes the crash for us, and on native Linux with huge pages OPcache still gets its 2 MB mapping. Can somebody with more experience please look at this? I have a test script and logs if that is useful.