Commit Graph
30 Commits
Author SHA1 Message Date
Anees Iqbal d27ab921c2 🐎 Start a class with no slab again at its first size
A class's next slab doubled with each slab it made, up to kSlabMax, and never
came down once its slabs went. A buffer of a page that came after its class
had grown and let go took a slab of the largest size the class reached: 2 MiB
from the region or the process's pages, and 16 MiB from the Shared pool, which
the host zeroes as it gives it. Keeping empty slabs (the commit before) stops
this for a buffer made and freed again and again, but not for one that comes
more than 2 s after its class's last slab went.

Now a class left with no slab, none holding a buffer and none kept, starts
again from kSlabFirst (128 KiB), over every source. While it keeps an empty
slab it goes on growing as before.

What changes for the guest: after an idle let-go, a class's next slab is 128
KiB, or twice its buffer, rather than the size the class grew to, so its
heap's size and the buffers' GPU addresses in it differ, and the host takes
and zeroes a smaller block. test_slab_class_starts_again_once_empty holds it
over the test's source and the pool's; the pool tests that took the grown size
now read 128 KiB.

The guest sets (guest/prebuilt, guest/prebuilt-arm64e) are rebuilt here from
the bundle of this commit and the one before, which changed it too.
2026-11-26 21:03:22 -08:00
Anees Iqbal 2591c9022e 🐎 Keep the Shared pool's empty slabs as the region's are
A class's slab of the Shared pool went back the moment its last buffer did
(RgfxPoolSlabSource::keepsFreed() answered false), and a large buffer's own slab
skipped the cache. A process that makes one buffer and frees it again and again
in an otherwise empty class then took a pool block for each buffer: a take the
thread waits for, a HEAP_OVER_POOL, the host zeroing the block, then the heap's
destroy and the block's give. Each new slab doubles its class's next one, so
after seven rounds every block was 16 MiB, for a buffer of one page. A user's
guest logged one process making and giving back about 17 such slabs a second,
none of them taken from again. The serialiser test of that pattern at
805c93a65 counted 100 small slabs (1539968 KiB) and 100 large slabs for 100
rounds of each, and 200 pool takes.

keepsFreed() is gone, so every source's slabs follow the one form. Each class
keeps two empty slabs, and large buffers' own slabs are cached, each for
kCacheIdleMs (2 s) at most. The pool source now takes a capacity, as the pages
source does, and the device passes the guest's RAM: the kext tells the bundle
nothing of the pool's length, so the pool's cache holds a sixteenth of the
guest's RAM at most. A refused take still lets the process's own kept and
cached slabs go first.

What changes for the guest: a buffer of the pool may lie in a slab an earlier
buffer emptied, at that heap's GPU address and offset, rather than in a new
block's heap, and the host sees fewer POOL_TAKE, HEAP_OVER_POOL,
OBJECT_DESTROY and POOL_GIVE messages. A kept block is its process's alone for
up to 2 s after its last buffer goes. A new buffer still reads zeros: the test
writes 0xa5 over each buffer before its free and reads zeros in the next.
test_pool_slab_churn_is_bounded holds the bound: one small slab, one large one
and two takes for those rounds. The pool tests that held the old let-go now
let kept slabs go through the idle clock or reclaim(). The guest sets are
rebuilt with the next commit, which changes the bundle too.
2026-11-26 21:03:21 -08:00
Anees Iqbal 0b3bb668b7 🐛 Give back slabs let go while a buffer's create waited
A Shared or Managed newBuffer in the guest could answer nil while the process's
own memory was on its way back to the region. A create that found no room let
the process's empty, cached and doomed slabs go, waited for the slabs other
threads were giving back at that moment, and asked once more. Under CPU load the
slabs' background thread, at utility QoS, falls behind on its drops: the slabs
other threads let go while the wait lasted stayed doomed, their memory out of
the source, and the second ask was refused while 26 to 52 of them held it.

A create now goes round again for as long as a round gave memory back, so its
answer is nil only where the region, or the pool, is full. A round after the
first asks again only where it gave something back, so a create in a quiet
process does what it did.

test_slab_take_gives_back_slabs_let_go_while_it_waited holds the slabs' releases
at the test source's gate while a create waits, so the slabs let go meanwhile
stay doomed: it failed in 20 of 20 runs on the old code and passes in 20 of 20.
test_slab_thread, which saw it once under Rosetta beside a 16-job cargo build,
refused a 3 MiB take in 6 of 3,828 runs beside 48 busy loops and 8 dd copies on
the M3 Max's 16 CPUs before, and in none of 3,144 after.

Both prebuilt sets are rebuilt from the change.
2026-11-21 16:59:23 -08:00
Anees Iqbal 318864dca5 🆕 Read once the file pages no guest process can ever write
Read-only memory under a no-copy buffer lies over a copy on rt wherever the
GPU cannot import it as it is, and every commit that uses the copy compared
all of its pages with their base. Much of that memory is a file's pages on
the guest's read-only system volume, which nothing can change while the
buffer lives, so the compare buys nothing there.

What the guest sees: the device offers feature bit 31, UNCHANGING_PAGES, with
READ_ONLY_PAGES, and the kext takes it (kext feature 1 << 19). At a no-copy
buffer's make over read-only memory, the bundle asks the guest's kernel once
whether the range is one file-backed region whose maximum protection has no
write (a shared mapping, or a private one whose maximum was lowered), whose
every page is the file's own, on a volume mounted read-only (proc_pidinfo's
fsid against getfsstat; no path is opened). Such a block is the app's own
memory, as before, marked RGFX_SURFACE_PAGES_UNCHANGING (and
_COPY_ON_WRITE); the kext's wire of a private mapping makes the process's
copy, as a Mac's does. Any other read-only block carries why not in its
flags' bits 8 to 11, which the host only counts.

What the host does: an unchanging block's copy is filled once, as its view is
made, never compared at a commit, and written again whole at the completions
of the command buffers that can write it, which drops the GPU's writes as
before. The views' summary counts the other read-only blocks by why, and
each texture over one (read-only-pages-textures-why-N, with one line per
format, size and why).

A private mapping whose maximum protection still has write is not provably
unchanging, since its process could raise its protection and write its copy:
it stays a plain read-only block, why "writable max protection".
2026-11-20 10:45:23 -08:00
Anees Iqbal f13f992e49 🆕 Let the guest place a Managed buffer at its CPU copy's address
On a GPU that shares the CPU's memory a Managed buffer is its bytes, so its
GPU address is its CPU copy's. The guest's bundle picked it an address of its
own, in a Private block, which metal2vulkan-rt cannot keep for a buffer that
lies over the copy's bytes: rt made the buffer at the copy's address, while
the guest's pointers held the pick, and each pick held a Private take.

Feature bit 63, MANAGED_AT_COPY, names the other form: where the guest took
it, a MANAGED_BUFFER_CREATE's gpu_address is its CPU copy's, the guest takes
no address for the buffer, and the address goes back with the copy. Both
kexts carry the bit, the bundle's serializer, slabs and no-copy buffers post
the copy's address, and the device tells its host the features the driver
took as it becomes ready (Host::driver_ready), before any message, so the
host's decoder can read the creates in that form.

No host offers the bit in this commit, so nothing changes for a guest. The
wire header changes, so the ROM's record and the guest's prebuilt sets are
rebuilt in their own commit.
2026-11-19 15:43:47 -08:00
Anees Iqbal 04885a693d 🔥 Take every slab of the Shared pool, with no region behind it
Where the device has a Shared pool, the kext maps its blocks and Managed
buffers lie over Shared copies (POOL and MANAGED_BUFFER_SLABS, which every
host that offers the pool offers), the pool is now the slabs' one source. A
Shared or Managed buffer, a Managed texture's CPU copy or an upload chunk the
pool has no block for (the kext refuses the take for the slab and for slabs
half as large down to the buffer's size), or one past the 4 GiB longest block,
is nil, as Metal's newBufferWithLength: is where memory runs out. Before, such
a buffer took a slab of the device region, which every process shares and
which the planner's ruling R1 keeps for the rings and the scanout alone.

What the guest sees: no HEAP_OVER_HOST_MEMORY for a slab where the pool is
on; a refused take is followed by the same take once more and then halved
takes, all of the pool. The halving that was the region's alone is the own
source's, so the pool's slabs halve too. The pool's tests change to the pool
alone: the Managed class without a heap, the take past 4 GiB and the refused
take are nil, and a new case shows a halved slab taken where only 64 KiB is
free.

Where the pool is off (Windows hosts, arm64 guests), the slabs are as before.
2026-11-19 03:59:31 -08:00
Anees Iqbal 66370e18be 🆕 Place no-copy buffers at the address the host gives their block
On metal2vulkan-rt a no-copy buffer's GPU address is the device address of the
host's import of its pages, which only the host knows, so a guest pick cannot
match it. Where the kext and the device both took IMPORT_ADDRESSES, the bundle
asks PagesBlock for its third output, the GPU address the host gave the block's
view, and makes each BUFFER_OVER_PAGES over the block there plus the pointer's
offset in its page, with no pick of its own and none given back at its destroy.

What the guest sees changes: a no-copy Shared buffer over writable memory the
kext wires answers a gpuAddress in [2^43, 2^44) on the Vulkan platform (at its
pages' import on rt) instead of a pick in the context's range, and two no-copy
buffers over the same bytes, which join one block, answer the same gpuAddress.
A Managed no-copy buffer's own address stays its GPU copy's pick; only its CPU
copy, the Shared buffer over the pages, moves. Read-only memory, copied memory
and blocks given no address keep their picks, and a guest without the bit sees
nothing change.

design.md gains the "Import addresses" paragraph, PagesBlock's third output and
the kext's bit 18.
2026-11-19 02:54:55 -08:00
Anees Iqbal 938fc76617 ✅ Name the grown class slab's size from its take in the test
The host-address test's second take is the class's second slab, which grows
past the first's 128 KiB, so its give names a larger block than the first's.
The check names the size the take logged, as the class-reuse test does.
2026-11-19 01:38:52 -08:00
Anees Iqbal d4a4c4beb3 🆕 Place each Shared pool heap at the GPU address the host gives it
Where the device and the kext both took POOL_ADDRESSES, the bundle asks
PoolTake for its fourth output and makes the block's HEAP_OVER_POOL at that
address, picking none of the context's: on metal2vulkan-rt's device the
address is the block's Vulkan device address, which the guest cannot pick.
The heap's destroy gives no address of the process's back (ownsAddress), so
a host address that meets one of the process's picks never frees the pick.
The pool takes' log line counts the heaps placed at the host's addresses.

What moves for the guest: every Shared pool slab's heap, and so each
buffer placed in it (gpuAddress, contents' GPU address), moves from the
context's range [2^46, 2^47) to the host's address: [2^45, 2^46) on the
Vulkan platform, the block's device address on rt. A buffer's address is
still its heap's plus its offset; Managed buffers' own addresses, Private
buffers and the region's slabs are the process's picks as before.
test_pool_slab_source_at_host_addresses holds the new placement; the
existing pool slab test holds the old one for a kext without the bit.
2026-11-19 01:38:52 -08:00
Anees Iqbal 72ce6655ae 🆕 Take the bundle's heaped slabs from the Shared pool
Where the device has a Shared pool and the x86-64 kext maps its blocks
(RGFX_FEATURE_POOL, kRgfxKextFeaturePool), the slab allocator's first
source is the pool: each slab of a heaped class, and each large buffer's
own, is a block the kext takes from the host, zero as it comes, made a
Shared heap over the block (HEAP_OVER_POOL), its buffers placed in it as in
a region slab's heap. A class shares slabs up to 16 MiB for buffers of up to
4 MiB, as the kext's chunks do. The region's and pages sources stay where
the device or the kext has no pool: arm64 guests and Windows hosts.

The allocator keeps no slab of the pool once empty: the heap's destroy goes,
then the block's give. On the 780M walk (boot to desktop, a 31 s window
drag, a GPU benchmark) the empty slabs a class kept were taken from again 5
times before the desktop and never after, and no large buffer took a cached
slab, so keeping them would save no take there.

The churn line gains the pool's takes: blocks taken and given, refusals,
and how long the takes waited for the host.
2026-11-19 00:00:12 -08:00
Anees Iqbal 8d5d4d331e 🆕 Count what the bundle's kept slabs save and log it per process
The pool moves the bundle's slabs out of guest RAM, so an empty slab a class
keeps is host memory held past rt's own reuse. The planner pre-registered
deleting the class slabs' cache if the give-backs and takes it saves cost
under 5% of the window drag and of Geekbench's timed step. This counts them
on today's bundle: slabs made, small slabs emptied, kept empties taken from
again, empties let go, and cached large slabs taken. The background thread
logs one line per process at most every 5 s, only after the counts change.
2026-11-19 00:00:11 -08:00
Anees Iqbal 82a8d1b00d 🐛 Zero each new region block before a process's window maps it
A region block's offset goes to any process once the device answers its
release or its holder's close, and the region keeps its bytes across the
device's reset and the guest's reboot. A new block held whatever its last
holder left: another app's command buffers' bodies, its shader library's
staging chunks, its buffers, or a surface copy of its window's pixels.

The kext now writes zeros over every span a window maps of a new block
before the window maps it, each through a kernel mapping of the cache mode
the window maps it with (write-combined from the region's write-combined
descriptor, write-back from its own), and allocBlock maps from the same
spans, so what is zeroed is what is mapped (RgfxBlockZero.hpp). A surface's
new block is zeroed over the part its holders map: the whole copy for a copy
block, the header for one in place. A span the kernel will not map fails the
grant. A block held again (a shared or revived surface block) and a pages
block, never in a window, are left.

The kext logs what the zeroing took by kind (bodies, split, write-back,
surface copies, surface headers), and offers it as feature bit 1 << 14
ZeroedBlocks, after which the bundle takes the region's slabs as clean
rather than zero them again. block_zero_test checks the spans and the
cross-process cases natively; guest/bundle/tools/block_zero_probe.c checks
them in a guest, a block filled by one process and read by the next.
2026-11-18 20:48:32 -08:00
Anees Iqbal 7338abf05a 🐎 Grow a class's chunked slabs to 16 MiB, buffers to 4 MiB in them
Each slab of the process's own pages is a pages block the host maps as a
view of its own and imports once, and RADV and ANV on i915 list every
live import in every kernel submission. A class's slabs stopped at 2 MiB
and every buffer past 1 MiB took a slab of its own, so a process with
many large buffers held as many imports. Where the kext gives chunks, a
class's slabs now grow, doubling, to 16 MiB (256 chunks, one dma-buf at
udmabuf's stock limits), each one heap, and a buffer of up to 4 MiB lies
in them; the region's slabs, and slabs of plain pages, keep their 2 MiB.
A class's slab past 2 MiB is never plain pages: where the kext gives no
chunks it is the region's, of 2 MiB at most.
2026-11-16 11:41:25 -08:00
Anees Iqbal 61084eab14 🆕 Tell the host which bytes of a new Managed buffer read zero
On a GPU without unified memory the runtime makes a Managed buffer's GPU copy
by copying all of its CPU copy over the link as the buffer is made. Most of
those bytes are zeros the guest never wrote: on the RX 7900 XTX, Geekbench 7.1
Metal's making copies carried ~4.5 GB beyond what the guest marked written, up
to ~0.94 s of GPU time (one 131 ms job copied six new buffers' 631 MB, 12 MB
of it written). The guest knows which bytes read zero; the host cannot.

What changes for the guest and the host:
- MANAGED_BUFFER_CREATE takes a tail, RgfxManagedBufferZero at byte 56
  (RGFX_MANAGED_BUFFER_ZERO_OFFSET): its CPU copy reads zero from zero_from to
  the length as the create is posted. The bundle sends it with every create:
  0 for a buffer made with no bytes, whose slab range take() zeroed; the
  length, which says nothing, for one made with bytes or over the app's own
  memory. The kext's SubmitBatch takes the create at its new size, 64 bytes.
- The host passes a zero_from below the length to the platform
  (BufferDesc::zero_from), and the Vulkan platform to the runtime
  (HostMemory::with_zero_from): the GPU copy starts with zeros the GPU writes
  there, rather than with those bytes copied in. A guest write there reaches
  the GPU copy by a didModifyRange:, as on a Mac's discrete GPU, whose GPU
  copy is made at the buffer's making; before, a write made before the host
  took the create reached it too. A GPU that shares the CPU's memory keeps one
  copy, unchanged, and a create without the tail (an older guest's) is copied
  in whole, as before. An older host reads the create and ignores the tail.

Tests: the decoder passes the tail's offset and none without it or at the
length; the platform's zero bytes are the device's on a GPU of its own memory;
the serialiser, the slabs and the device buffers send each make path's value.
Both guest sets and the ROM's record are rebuilt in the next commit.
2026-11-15 18:44:04 -08:00
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