Skip to content

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

Merged
arnaud-lb merged 1 commit into
php:PHP-8.4from
s2x:fix-opcache-hugetlb-overshoot
Sep 4, 2026
Merged

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

Conversation

@s2x

@s2x s2x commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

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.

@arnaud-lb arnaud-lb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
arnaud-lb merged commit 8484a9f into php:PHP-8.4 Sep 4, 2026
18 checks passed
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)
@arnaud-lb

Copy link
Copy Markdown
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.
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.

2 participants