Skip to content

ext/opcache: keep huge page remap inside the reserved range - #23554

Open
s2x wants to merge 1 commit into
php:PHP-8.4from
s2x:fix-opcache-hugetlb-overshoot
Open

ext/opcache: keep huge page remap inside the reserved range#23554
s2x wants to merge 1 commit into
php:PHP-8.4from
s2x:fix-opcache-hugetlb-overshoot

Conversation

@s2x

@s2x s2x commented Sep 3, 2026

Copy link
Copy Markdown

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-fpm started 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 with MAP_32BIT, frees it, moves the address up to the 2 MB boundary, and then maps requested_size again with MAP_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, and MAP_FIXED deletes what is mapped there.

Other places do this correctly. zend_mm_chunk_alloc_int() in Zend/zend_alloc.c reserves size + alignment - REAL_PAGE_SIZE first, so its aligned address always stays inside its own memory. And find_prefered_mmap_base(), in this same file, aligns the address and then checks it: if last_candidate + requested_size does 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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant