Commit Graph
16 Commits
Author SHA1 Message Date
Anees Iqbal 4b06fb8eb8 🐛 Wait for the slabs on their way back before refusing a buffer
A slab being let go leaves the slabs' books before its memory is back in its
source: its heap's destroy and its block's release, which the kext answers
once the host has let go of it, run outside the lock. A buffer create that
found no room, and whose own let-go of the empty and cached slabs found
nothing left to drop, asked its source again at once and answered nil while
another thread (or the background thread) was still giving those slabs back.
The region's reclaim answered the same way for any block it was asked for.

For the guest: newBuffer of a Shared or Managed buffer, and a block of the
region (a Shared heap, a texture's CPU copy, a staging window, a command
buffer's out-of-line message), could be nil near the region's or the pages'
end while another thread freed buffers. It found the memory a moment later.
test_slab_thread caught it once on the M3 (m3-max-25197): on that
4-thread test under Rosetta, development's slabs refused a 3 MiB take in 8,
9 and 3 of 1,000 runs (m3-max-28562, 28595, 28619). Each refusal came with
15 to 52 slabs, 42 to 154 MiB, out of the books and not yet back in the
source: a starved background thread had let doomed slabs pile up, and one
thread's evict was dropping them all. This branch: 0 of 1,000 and 0 of 2,000.

A take that finds no room now waits for the drops in flight and the doomed
slabs as it asks, then asks again; so does RgfxSlabs::reclaim, which then
answers true. Those are the drops its own dropDoomed(true) would have made
had it come first, on the same locks and the same host answers, so the wait
holds the thread on nothing that call does not. None of the region's
callers that reach the reclaim holds a lock the drop path takes (the
serializer's library-key, ICB, destroy and ring locks, the region's pages
lock): the argument encoders' answer blocks, an ICB's slots (its own slot
lock), a Shared heap, an IOSurface's shadow (its making lock), the upload
and body pools (an arena's lock, a queue's post lock), a library's staging
window (the stream lock), a texture's CPU copy. stats().dooming counts the
drops in flight too.

Tests: test_slab_take_waits_for_slabs_given_back and
test_region_reclaim_waits_for_slabs_given_back hold a slab's release at a
gate while another thread asks for its room: without the waits each answers
nil at once (both fail without their halves of the fix). test_slab_thread
also checks that every byte its source gave is a slab's.
2026-11-14 01:57:11 -08:00
Anees Iqbal c410a064b0 🐎 Make the bundle's slabs of the kext's 64 KiB chunks
A slab of the Metal bundle's own memory, 2 MiB of plain pages, reaches the host
as up to 512 runs of guest RAM, since the guest's kernel scatters 4 KiB pages.
The host maps each run of a view apart against a per-process budget of
mappings (vm.max_map_count, 65530 by default on Linux): on NVIDIA, views of
scattered pages, slabs' among them, used up the budget and the buffers made
after it were copies.

The kext now gives a slab's memory as physically contiguous 64 KiB chunks
(selector 12, PagesChunked, kext feature 1 << 8): it takes them zero-filled,
maps them into the process in order and makes the pages block of their runs,
so a 2 MiB slab is 32 runs at most. The memory is the block's: the slab's
release takes it, and the kext unmaps it from the process and frees the chunks
once the device has answered the block's release, or at the client's close. A
slab the kext gives no chunks for is plain pages, as before.

64 KiB is the size the kernel gives at once however long the guest has run. On
an 8 GiB x86-64 macOS 26.7.2 guest on the Radeon 780M host, 4,096 chunks in a
row took 0.033 ms each, and 0.036 ms each once 978 MiB of 4 KiB pages had been
taken, the slowest 0.74 ms, none refused. Of 128 runs of 2 MiB asked, the same
guest gave 28 quiet and 16 once memory had been taken, the slowest in 264 ms.

Measured with the chunked set against development's, each on its own boot.
On the Radeon 780M, an x86-64 macOS 12.7.6 guest (manypieces' m3-max-18395
against m3-max-18394), the host's copy-ins of guest pages fell from 632.8 to
0.9 million texels a second in a WebGL scene (gl3d), from 202.4 to 0.2 playing
1080p video in Safari, 96.4 to 4.7 scrolling, 84.6 to 22.2 in Maps, 31.8 to
8.1 in Launchpad and 21.7 to 1.1 in Mission Control; gl3d drew 39.5 frames a
second where it drew 34.1, the GPU 51.4% busy where it was 63.7%. The slabs,
WebKit's tiles, the WebGL drawing buffer and the video's BGRA buffer among
them, are now used in place there; what is still copied is no-copy buffers
over an app's own pages. On the Iris Xe, a 26.7.2 guest's walk (m3-max-18417
against m3-max-18418) peaked at 85,951 of QEMU's mappings where it peaked at
94,896, and scrolled and drew as before (58.8 and 58.7 frames a second
scrolling, 47.5 and 47.1 in gl3d); manypieces' Iris pair (m3-max-18398
against m3-max-18397) drew gl3d at 48.6 frames a second where it drew 43,
with the same copy-ins. An arm64 macOS 27.0.1 guest on the M3 Max under HVF
passed the arm64 gate's 15 checks with the arm64e set (m3-max-18496).

Tests: a heaped slab and a large Shared slab are the kext's chunks first, the
heap and the buffer over their block with no pages asked of the kernel; a slab
the kext gives no chunks for is plain pages; a chunked slab's memory goes with
its block's release alone, a plain one's pages after it. The user client's words
test holds selector 12, its feature bit and the chunk's size. make -C guest
test, prebuilt and prebuilt-arm64e on a Mac, and both sets committed here; make
-C guest prebuilt-check passes.
2026-11-11 19:35:50 -08:00
Anees Iqbal b415f5b61b 🔥 Lay every slab in plain pages, asking for no superpage
A 2 MiB slab of a process's own pages was a superpage the slabs' thread
asked for ahead of its take. Asking still stalled the process: the kernel's
search for a contiguous 2 MiB runs in vm_map_enter with the task's map
interlock held and the new entry locked exclusive (public xnu-12377.101.15
vm_map.c), so any walk of the map past it waits. Tailspin on an 8 GiB
macOS 26.7.2 guest on the Radeon 780M host: WindowServer's slabs thread was
in cpm_allocate while its main thread, making a texture over a layer's image
in a live resize, waited in mach_vm_region behind it, and Safari's page drew
no frame for 179 ms. No thread placement avoids that, superpages run out
minutes after a boot anyway, and the held one cost up to 38 MiB across the
19 processes that map the bundle.

So every slab of the process's own pages is plain pages, taken by the take
that needs it, as it already was once superpages ran out. The superpage
call, its record in the kext and the VM-guard tool go.

What changes for the guest: the kext no longer answers selector 12
(Superpages) nor sets kext feature bit 8; both lived only between the kext
and the bundle, which ship together, and reims_graphics_wire.h never had
them. The host sees the same pages blocks as a guest out of superpages
does: HEAP_OVER_PAGES and BUFFER_OVER_PAGES over plain pages' runs.
2026-11-09 04:56:53 -08:00
Anees Iqbal 1927ea3c4f 🔥 Drop the several-superpage call the slabs no longer make
Since the slabs' thread asks for one superpage ahead and a slab larger than
2 MiB is plain pages, nothing asked the kernel for several superpages in a
call: the reservation it was made in piece by piece, its test VM and its
test, and spguard's cases of it ran only that path.

RgfxSuperpages now makes one superpage, anywhere, in one call, and says why
one: a call for several that runs out partway raises a deallocation-gap
guard exception, fatal in processes whose VM guard is on. spguard keeps the
cases that show that exception (several.anywhere, several.fixed,
several.fatal, the old.* cases renamed) and no longer builds our module.
2026-11-09 04:56:53 -08:00
Anees Iqbal 4a3d517fec 🐎 Ask the kernel for superpages on the slabs' thread alone
A take of a slab asked the kernel for its superpage on the thread that took
it, and the kernel's search for a contiguous 2 MiB (cpm_allocate) runs on
the asking thread, given or refused. Sampled with tailspin on an x86-64
macOS 26.7.2 guest on the Radeon 780M host: WindowServer's main thread,
making a texture over a layer's image as a window moved, was in that search
in two samples 100 ms apart, and Safari's page drew no frame for 151 ms.

Now the slabs' background thread (RgfxSlabs::run) asks for one superpage
ahead (RgfxPagesSlabSource::prepare), at its start and after each slab of
the process's own pages, while the guest's superpage record is open. A take
of a 2 MiB slab uses the superpage held, or takes plain pages at once; no
thread that takes a slab asks the kernel. A process holds at most one
superpage it does not use. A dedicated slab larger than 2 MiB is plain
pages, as every slab was once the kernel ran out minutes after a boot; the
superpage module's several-superpage path is no longer reached by the
slabs.

The slab tests step the thread's work where a superpage is to be held, and
test_pages_source_asks_on_its_thread_alone runs the thread and checks that
every superpage ask came from it, not from the taking thread.
2026-11-09 04:56:53 -08:00
Anees Iqbal 5c1800745b 🆕 Ask the guest's kernel for superpages only while it has some
An 8 GiB x86-64 macOS 26.7.2 guest on the Radeon 780M host has no 2 MiB
superpage left a minute after its boot, yet every pages slab still asked the
kernel for one first. Each refusal cost the asking thread the kernel's search
for a contiguous 2 MiB: 140 to 190 ms in cpm_allocate, sampled with spindump,
in the first slabs of every process, which then took plain pages anyway. A
video decoder session's first access unit waited that long (the AVPlayer
probe's start hold there was 283 ms, against 133 to 150 ms on a native Mac),
and so did Safari's GPU process's layer drawing as a page opened.

The kext now keeps one record for all the guest's processes: 'RGFX' selector
12, Superpages, kext feature bit 1 << 8 (RgfxSuperpageRecord.hpp). A superpage
the kernel refuses for want of contiguous RAM closes it, a slab of superpages
given back to the kernel opens it, and while it is closed a pages slab takes
plain pages without asking, as a refused take did. A refused slab of several
superpages, or a refusal past the wire budget, leaves it as it was.

What the guest observes: the same slabs as before, without the search. The
host sees the same pages blocks, a superpage's one run or plain pages' many.
Where the kext lacks the bit, the bundle never calls the selector and asks the
kernel at every take, as before; where the call fails, the take asks too. A
bundle that knows no record leaves a kext's record untouched. The host plays
no part. test_pages_source_superpage_record holds the record's closing, the
takes it spares in another process and its reopening on a superpage slab
given back; user_client_words_test holds the selector, the bit and the
kext's record.
2026-11-08 19:26:08 -08:00
Anees Iqbal bcd75ba81e ✅ Give the heap tests views a heap of Metal's page fits
On a host of 4 KiB pages, a heap of 3 pages rounds up to Metal's 16 KiB page, past a
view of 3 pages; and a region slab's heap is its class's slab size, not 2 MiB.
2026-11-07 08:27:28 -08:00
Anees Iqbal ae0edc382e 🐎 Lay slabs in plain pages where no superpage is left
The pages slab source took only 2 MiB superpages, and the guest's kernel runs
out of them minutes after a boot: measured on an x86-64 macOS 26.7.2 guest of
8 GiB on the Radeon 780M host, a process got 19 at boot with 2.5 GiB free,
then KERN_NO_SPACE, and none three minutes later. Every Shared buffer, 4 KiB
up, then lay in the device region, which the guest maps uncached and an AMD
host applies: its stores ran at 13 MB/s, against 56-123 GB/s in a superpage.

Where the kernel refuses superpages, the source now takes plain pages,
zero-filled and wired into a pages block by the kext: a 2 MiB slab is one
HEAP_OVER_PAGES the host lays over the block's view, a larger dedicated
Shared one a BUFFER_OVER_PAGES over it, as before. Only where neither comes
is a slab the region's.

A queue's upload chunks, the bytes set*Bytes: hands in, are now Shared
buffers of the slabs, so they lie in the process's own pages too, and fall
back to region blocks only without slabs.
2026-11-07 08:27:28 -08:00
Anees Iqbal 93ea42a435 🐛 Lay Managed buffers in the Shared slabs, not region blocks
A Managed buffer had slabs of its own options. Where the device does not
place Managed CPU copies in heaps (MANAGED_AT_OFFSET, offered only where the
GPU has memory of its own) those slabs were region blocks, each buffer a
BUFFER_OVER_HOST_MEMORY over its range, and past a superpage they were region
blocks on every host. So Managed buffers and the Managed no-copy buffers'
shadows held app-sized parts of the 1 GiB region every process shares, and
WindowServer's reserve in it: 256 MiB Managed buffers were nil 9 times in 10.

Where the device took MANAGED_BUFFER_SLABS, a Managed buffer lies in the
Shared class of its cache and hazard tracking modes: its range is a Shared
buffer's, made as any (BUFFER_AT_OFFSET in a superpage's or region slab's
heap, BUFFER_OVER_PAGES over a larger slab of superpages), and the Managed
buffer a MANAGED_BUFFER_CREATE over it, at an address of its own. Its range
goes back once the copy's destroy follows the buffer's. A device without the
feature keeps the slabs of their own. Nil, as before, where no source has
room.

For the guest: a Managed buffer comes as a Shared buffer and a
MANAGED_BUFFER_CREATE in place of BUFFER_OVER_HOST_MEMORY over a region range
or BUFFER_MANAGED_AT_OFFSET, and lies in the process's own superpages at any
size.
2026-11-05 21:59:33 -08:00
Anees Iqbal ed0079f8cb 🐛 Make each superpage in a hole of its own, not over the reservation
An overwriting call that fails puts back what it took only where no entry
starts at its range's end on xnu-12377.81.4 and earlier (macOS 26.3 and
before): vm_map_lookup_entry(map, end) is true there, and the reservation's
next piece always starts there. So on those guests a refused superpage left
a hole in the run, and the one deallocation of the whole range would have
met the same DEALLOC_GAP guard the run was made to avoid.

Each piece of the reservation is now given back and a superpage made in its
hole, fixed and not overwriting, so nothing ever needs putting back. A
refusal gives back the superpages before the hole and the reservation after
it, both the run's own, and leaves the hole as it is: empty, or another
thread's, which only makes the slab the region's. The tests' kernel refuses
a fixed call over anything mapped, and lets another thread take a hole.
2026-11-05 21:59:28 -08:00
Anees Iqbal 87efcd206c 🐛 Make each superpage its own kernel call
WebKit's GPU process died of EXC_GUARD (VIRT_MEMORY, DEALLOC_GAP) inside
mach_vm_allocate when a WebGPU page asked for a large Shared buffer under the
app load. A call for several superpages that runs out of contiguous RAM
partway takes down the whole range it was asked for on macOS 26.4 and later:
vm_map_enter shrinks the size to what it made, then deletes up to the
request's end, over memory it never mapped. That gap is fatal in every
process whose VM guard is on: Apple's platform binaries and the hardened
browser processes. A third-party app just gets the refusal.

A dedicated slab of several superpages now reserves its range first and
makes each superpage over its own 2 MiB of it, fixed and overwriting. A
refused superpage leaves its piece of the reservation in place, and one
deallocation gives back the whole range, so the slab fails cleanly and is
the region's, as any refusal was. A one-superpage slab stays one call
anywhere. The tests' kernel acts as xnu does there; car 8's one call fails
them on the gap.
2026-11-05 21:59:28 -08:00
Anees Iqbal 7bf180c413 🆕 Lay large Shared buffers in the process's own superpages
A Shared buffer past a superpage took a region slab of its own, so an app
could hold no more large buffers than the region's 440 MiB. Its dedicated
slab is now superpages too, more than one run, with no heap: the one buffer
in it is a BUFFER_OVER_PAGES over the pages block's view, at an address of
its own. A create of that size is rare, so its own import costs little.
A large Managed buffer stays in the region, as no command places a CPU copy
over a view. A source now says which slab sizes it serves for a class, and
whether a slab has a heap.
2026-11-05 21:59:27 -08:00
Anees Iqbal f2f96a2479 🆕 Lay heaped slabs in the process's own superpages
The region gives an app about 440 MiB of slabs, past which its buffers'
creates fail. Where the host makes heaps over pages blocks (HEAP_OVER_PAGES)
and the kext wires them (PagesBlock), a heaped class's 2 MiB slab is now a
superpage of the process's own, one run of guest RAM, wired into a pages
block and made one HEAP_OVER_PAGES, its buffers placed in it as in a region
slab's heap: BUFFER_AT_OFFSET, and BUFFER_MANAGED_AT_OFFSET where the device
took MANAGED_AT_OFFSET. On the RTX 5080 such a heap takes 13.4 us and a
placed 4 KiB buffer 1.93 us, where a no-copy buffer over the block's view
took 65-75 us. A larger dedicated slab, which would be more than one run,
and a slab where the kernel has no superpage to give or the kext no pages
block, stay the region's. RgfxSlabs asks the first source before the
region's for each new slab, and a slab keeps the source it came from.
2026-11-05 21:59:27 -08:00
Anees Iqbal fc45b48065 🐎 Let empty slabs go once they sit idle 2 s
A class kept up to two empty slabs for good, so every process that had freed
small buffers held up to two slabs per class of the region after it came to
rest. The region is shared by every context: on msi a booted desktop's
processes held 13 MiB of it that way (region.fill 396 MiB with slabs against
409 MiB without), room another process is then refused.

A kept empty slab now goes once it has sat empty kCacheIdleMs (2 s) with no
buffer taken from it, on the clock the cache of dedicated slabs decays on;
the background thread wakes for whichever is due first. The third empty
slab of a class still goes at once. The tests set the clock, and check that
empty slabs go at 2000 ms and not at 1999, that one taken from again stays,
and that a cached slab decays on the same clock.
2026-11-05 21:59:25 -08:00
Anees Iqbal f03123fd88 🐛 Let the slabs go before the kext refuses a block
The slabs keep a process's empty slabs, two per class, and the slabs of its
freed large buffers, up to a sixteenth of the region for 2 s, for reuse. The
region is shared by every process, so that kept room could make one of the
process's own other blocks nil (a texture's CPU copy, a heap, an upload
chunk, an IOSurface's copy) where giving it back would have made room: a
refusal Metal does not make while the memory is there.

RgfxRegion::allocate now asks the device's slabs to let those go and asks the
kext once more, so a block is refused only where the process holds nothing it
could give back. The surface sources' refusals are not asked again: they also
answer how the kext shares a surface, and their callers fall back to
allocate().
2026-11-05 21:59:24 -08:00
Anees Iqbal 351724d07a 🐎 Lay Shared and Managed buffers in slabs of the region
Each Shared or Managed buffer took a kext block of its own: a blocking ALLOC
with a first-fit scan and an overwrite mapping, a BLOCK_GRANT, a memset of the
whole block, a host import per buffer, and a BLOCK_RELEASE round trip at its
free. In a 16 GiB guest on the 5080 that made 4 KiB creates 29 us (76 us late
in a hold, the scan), stopped them at the block table's 16,384 entries, and
made frees a round trip each, RES-01's trigger.

Buffers now take whole 4 KiB pages of slabs the bundle holds (RgfxSlabs):
- a Shared slab is one Placement HEAP_OVER_HOST_MEMORY of the class's
  options and each buffer a BUFFER_AT_OFFSET there; a Managed slab a plain
  block and each buffer a BUFFER_OVER_HOST_MEMORY over its own range;
- buffers up to 1 MiB share their class's slabs, which grow from 128 KiB to
  2 MiB; a larger one takes a slab of its own, cached once freed for reuse;
- a create zeroes only what of its range is dirty, and a background thread
  zeroes freed ranges and lets slabs go, so no free waits for the host;
- where the region has no block for a slab, the process's own empty and
  cached slabs go first, then smaller slabs down to the buffer's size: nil
  only where the region is full.

A create is a free-list take and one ring-0 post, with no kext call, and the
block table holds one entry per slab. Where slabs come from is their source's,
so guest-RAM slabs can take the region's place behind a feature bit.
2026-11-05 21:59:24 -08:00