Conversation
mmap_lock has the default mutex type, so an owner that takes it a second time deadlocks with no trace, and an unlock from a thread that does not hold it is undefined. Nothing at the raw pthread call sites could catch either mistake. mmap_lock_acquire and mmap_lock_release record the owner in a thread-local flag and assert on a recursive acquire or an unowned release, so the mistake aborts at the offending call instead of hanging the VM. The lock order and every critical section are unchanged.
jserv
reviewed
Sep 29, 2026
| static pthread_mutex_t mmap_lock = | ||
| PTHREAD_MUTEX_INITIALIZER; /* Lock order: 1 */ | ||
|
|
||
| static _Thread_local bool mmap_lock_owned; |
Contributor
There was a problem hiding this comment.
The flag catches misuse at these two wrappers, but the preconditions it could enforce live elsewhere: gva_resolve_perm, guest_split_block, guest_update_perms, and guest_materialize_lazy all carry a "callers MUST hold mmap_lock" comment in src/core/guest.h, and mem.c repeats the same prose above the region-splitting and overlay helpers. Exporting a mmap_lock_held() reader over mmap_lock_owned would let a follow-up turn those into asserts at the callee, which is where a missing acquire actually corrupts the region table.
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.
This opens a series of PRs that split #372 into smaller parts so the mmap changes are easier to review.
This PR routes all mmap_lock through
mmap_lock_acquire()andmmap_lock_release(), so later PRs hook one place instead of every call site, and misuse trips an assertion.mmap_lock has the default mutex type, so an owner that takes it a second time deadlocks with no trace, and an unlock from a thread that does not hold it is undefined. Nothing at the raw pthread call sites could catch either mistake.
mmap_lock_acquire and mmap_lock_release record the owner in a thread-local flag and assert on a recursive acquire or an unowned release, so the mistake aborts at the offending call instead of hanging the VM.
The lock order and every critical section are unchanged.