Skip to content

ext/opcache: drop the pre-emptive munmap before the huge page remap - #23574

Open
s2x wants to merge 1 commit into
php:masterfrom
s2x:opcache-mmap-no-premature-munmap
Open

ext/opcache: drop the pre-emptive munmap before the huge page remap#23574
s2x wants to merge 1 commit into
php:masterfrom
s2x:opcache-mmap-no-premature-munmap

Conversation

@s2x

@s2x s2x commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #23554, suggested by @arnaud-lb: MAP_FIXED already replaces the overlapped part of the reservation, so the munmap() before it is unnecessary and only opens a window for another thread to take the address.

This keeps the reservation, maps the huge pages into it, and releases only the head and the tail that are left over — they always add up to huge_page_size. On failure the whole reservation is released, because a failed MAP_FIXED can leave
a hole in it.

As with my last patch, please check this carefully. This part of the code is still new to me.

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.

1 participant