VM locking and lifetime rules¶
Different protections serve different purposes¶
Mechanism |
What to establish |
|---|---|
Map lock |
Whether an entry, its range, and its mapping attributes may change. |
Object lock |
Whether object metadata and resident-page associations may change. |
Page busy state |
Whether an operation has the required shared or exclusive access to the page during a VM transition. |
Object reference |
Whether the object itself remains alive. |
Paging-in-progress protection |
Whether paging activity prevents incompatible object teardown or restructuring while work is in progress. |
Page reference and wiring |
Whether the page remains retained and whether it is excluded from ordinary reclamation. |
Page-queue locks |
Whether queue membership and queue bookkeeping may change. |
None of these is a universal replacement for the others. For example, an object reference does not grant exclusive access to its pages, and a wired page does not grant write access through every mapping of it.
Map and object locking¶
vm_map.h defines the map-locking interfaces. Ordinary process maps and
system maps have different lock representations. Use the map API rather
than assuming every map is protected by an identical primitive.
vm_object.h defines shared/exclusive object-lock operations and assertion
macros. The field annotations in that header identify object-lock-protected,
atomic, and lifetime-constant state. A fault fast path that can read an
existing page does not establish the locking requirements for allocating,
inserting, or removing one.
Follow each function’s assertions and entry/exit contract. In particular,
vm_map_find() acquires a map lock internally, while lower-level map
helpers can expect the caller to supply one. Nesting them under an assumed
common contract can introduce recursive acquisition or lock-order problems.
Dropping a lock is a validation boundary¶
Before dropping a lock to sleep, identify the references, busy state, or other protections that keep the required objects alive. After reacquisition, revalidate the state that another thread could have changed.
The fault path is a useful example: entry lookup, object selection, vnode coordination, and pager activity can span several lock transitions. Holding an old pointer across those transitions is not evidence that the old mapping still exists. Error and retry paths must release the same lifetime and busy protections acquired by the successful path.
Wiring and release¶
vm_page_wire() and vm_page_unwire() change a page’s wiring
state. The unwire interface also receives a queue choice in this checkout.
That argument matters for the page’s subsequent treatment by VM; wiring is
not just an interchangeable increment/decrement around any page operation.
When reviewing a VM caller, write down the object reference, page ownership, lock state, and release operation separately. Check both successful and failed allocation, pager failure, and retry paths. Use the source assertions and the INVARIANTS/WITNESS diagnostic configurations when validating a change.