Commit Graph
8 Commits
Author SHA1 Message Date
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