ext/opcache: keep huge page remap inside the reserved range - #23554
Open
s2x wants to merge 1 commit into
Open
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.
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.