rt's BlockInfo::zeroed (metal2vulkan dd4193cab3) says a block reads zero as
rt hands it out: the driver zeroed its allocation and no block held a byte at
or past it before. The owner's rule: zero unless the block says zeroed and
these bytes were never handed out. A block's mark of the bytes ever handed out
(Placed) rises past each placement, and only bytes below it are zeroed:
- own() and own_managed(): a Shared or Private buffer in a block of its own
skips rt's zero or the platform's memset where the block is zeroed;
- heap zeroed(): a heap's block of its own likewise;
- in_known(): a Private buffer pinned in a known block skips rt's GPU fill
where none of its bytes is below the block's mark, which every lookup in
the block raises (buffers, heaps and Managed device copies pinned there);
- the Shared take worker zeroes only the first bytes of a carve below its
piece's mark, raised as the carve is carved.
Skipped bytes are counted by class: rt-zero-skipped-private-bytes,
rt-zero-skipped-shared-bytes and pool-take-zero-skipped-bytes. Every byte
still reads zero to the guest; on a device without zeroInitializeDeviceMemory
nothing changes.
On the Vulkan platform (metal2vulkan's old runtime) rvg_quiesce_for_exit
answered true once the release's let-go took the GPU devices, so QEMU called
exit(). The let-go does not end the runtime's work: a pipeline making of the
warm pass can stay in the driver past the device's last drop, and the
runtime's exit hook, registered as the device was made, then waits for it and
on Linux ends the process at once with _exit. QEMU's exit notifiers never ran:
on an RTX 2080 (620.70.06) a quit 24 s into a boot, its let-go 315 ms after
the quit, ended 9.6 s after it with QEMU's pidfile left (job m3-max-53092).
Now the quiesce answers false once the platform asked the runtime for a device
in the process, kept or let go of since: each of the platform's makes (the
default device, a window's device, MTLCopyAllDevices's, the device choice's
timed devices) and each adoption mark it (let_go::asked_the_runtime_for_devices).
What changes for the host: on the Vulkan platform, a quit after the release
that let go of the device now gets false, and QEMU runs its own exit work (its
exit notifiers, the terminal's restore, the streams' flush) and ends with
_exit and the cleanup's status, as it did with a device kept. The runtime's
exit hook and the driver's exit handlers no longer run then, and the library
writes its degradation counts in the quiesce. A process that never opened a
GPU device still answers true. The platform over metal2vulkan-rt is unchanged.
The let-go's child-process test now expects false after the let-go, then
_exit with status 0; a new one holds true and exit() for a process that asked
for no device. The C harness ends as QEMU does on the answer, and its test
expects false on the Vulkan platform and true over metal2vulkan-rt. The API
docs, the header, docs/api.md and docs/lessons.md say the same.
A message the decoder did not carry out was logged once per kind at warn,
and every later one only at debug, so a boot's degradation counts showed
none of them. On rt without the guest RAM lease, a walk refused 805 no-copy
creates and 701 textures over them, and the counts said nothing.
Each is now counted by why: decode-malformed, -unknown-command, -refused,
-bad-reference and -bad-resource-id from DecodeError, and decode-faulted
for a raise. This applies at the stage-3 report, a pipeline or library
create's refusal, and a deferred op's raise. A message of a context closed
before it came is owed nothing and is not counted. The lines are unchanged.
docs/conventions.md lists the codes. A test refuses two event creates: one
warn line, and decode-refused counts 2.
On the rt platform every cache's copies are staged, and a download's command
buffer ended in a completed handler that copied the staging buffer into the
pages on the host's CPU. The platform sees every queue's completions on one
thread, so a large surface's copy held all of them: 144 MiB at 3 GB/s is
48 ms, and every completion of every queue behind it waited that long.
The handler now hands the copy into the pages, the copy's landing and the
answers that wait for it to a worker of the caches' own
(reims-vgpu.graphics-into-pages-<i>), keyed by the block, so a block's copies
land in the order their command buffers completed, and returns. Only what
waits for the pages waits for that thread: the download's or release's
answer and the threads that wait for the copy to land.
For the host: raise_interrupt is now also called on those threads, which the
header, docs/api.md and the raise list now name. QEMU's callback already
takes any of the library's threads, so its side needs no change. The tests
that check a staged download's answer now wait for the worker, and a new
test shows another queue's completion going through while the copy waits.
The rt allocation-stall walks ran trees branched from b266 and were read
against development's surface_cache.rs, where b267 had already removed the
zero they measured. The lesson says how to spot the next one: a walk's record
names its base, and a finding is checked at development's tip first.
A comparison of the old platform's boot with rt's read the old platform as
importing the same udmabuf-mapped view rt failed on. It had imported a view
mapped from its files: the old platform probes each device and, on one that
refuses a dma-buf-mapped view, maps views from their files from then on, a
probe rt never runs. The entry says how to tell the mappings apart before
calling two imports the same.
The Vulkan platform's quiesce ahead of the exit (rvg_quiesce_for_exit) did
nothing and answered true, so a quit with a GPU device kept to the exit went
on through exit(). On Linux metal2vulkan's exit hook, which runs before QEMU's
exit notifiers, waits 2 s for the runtime's work in progress and then ends the
process with _exit itself: with a device kept to a paused VM's quit, 5 of 9
quits on an RTX 3090 and 5 of 9 on an RTX 2080 left QEMU's pidfile behind,
and usb-host's return of host devices to their drivers is skipped the same
way.
Now it closes the exit gate ahead of the exit, waits for the library's steps
in progress within its bound, and answers false while the platform keeps a GPU
device (let_go::gpus_kept), as the platform over metal2vulkan-rt does. What
changes for the host, where QEMU's quit finds a device kept (a paused VM, a
shutdown action that pauses, a display read that kept it): QEMU's exit work
runs (its exit notifiers, the terminal's restore, the streams' flush), then
_exit with the cleanup's status. metal2vulkan's exit hook and the driver's
exit handlers no longer run then, and the degradation counts are written in
the quiesce rather than by the exit gate's handler. A quit whose release let
go of the device answers true and exits as before.
New tests hold both answers in a child process: false with a device kept,
true once the let-go took it, then exit() with status 0. The C tests that
quiesce with nothing on a GPU still answer true. The API docs, the header
and docs/api.md say the same for both platforms; docs/lessons.md has the trap.
A session's end line now says how many surfaces' memory its decodes landed
in and the most of them live at once, told apart by the memory's identity
(the device cache's, else the mapping its pages lie in), and, where the CPU
copied pictures, the copies' mean and longest time. These size the exported
surfaces a session would need on rt and the CPU's cost of landing there
today. Log lines only: nothing the guest or host observes changes.
On the rt platform the VA-API arm now answers the guest: its decoders
land pictures in rt's exported blocks, which rt makes as dma-bufs
(new_dma_buf_buffer_of_length_padding_unobserved, export_dma_buf over
new_exported_block(DmaBuf), linear, the named byte on a multiple of 256).
rt's contract after an acquire is that the external side does not touch
the block until its next whole overwrite, and VA reads a reference from
the surface it was decoded into. So each codec's decoder now tells its
accelerator whether a later picture may predict from the picture it asks
storage for (PictureRole: H.264's nal_ref_idc, HEVC's sub-layer
non-reference picture of the highest sub-layer), and where the platform's
rules keep references apart (CachedReferences::Apart, rt's) a reference
is decoded into a surface of the decoder's own and lands in its cache by
video processing, while a picture no later one predicts from is decoded
straight into its cache. Each session counts its pictures by role and the
references it kept apart, and logs the counts at its end.
The rt arm answers no encoder: rt releases no encoder's sources to the
driver yet. The Vulkan platform's rules keep every picture in its cache
and its encoders, as before.
rt checks a heap texture's place in the device's memory (block base, heap
offset and texture offset together) against the driver's image alignment,
where Metal checks it against the heap's start. The guest's heaps on the
32 KiB granule therefore had every texture refused on GPUs aligning images
at 64 KiB or more, while the 3090's tests passed. The lesson says how it
showed, what fixed it, and how to spot the next one: the count
rt-refused-heap-texture-place.
metal2vulkan-rt supports no quiesce inside an exit handler. QEMU's exit
handler for an exit() that skips its cleanup (a later device's refused
realize, a failed incoming migration) called rvg_quiesce_for_exit there and
let the exit go on where it answered true, though no release had let go of
the GPU device on that path. vendor/qemu ba447463 makes the handler call no
quiesce: it runs QEMU's own exit work and ends with _exit(EXIT_FAILURE), and
after the cleanup's true answer it does nothing.
For the host: rvg_quiesce_for_exit's doc says it is not for an exit handler,
and on that path QEMU now ends with _exit, the library writing no
degradation counts. QEMU's new trace event reims_graphics_end names each end
through _exit.
metal2vulkan-rt's exit contract (its README, "Quiesce and exit", at
d48664f6d2) supports exit() with a device live on no device: a process that
still holds one quiesces it and ends with _exit, whatever the quiesce
answered. On an RTX 3090 (620.70.06), 3 of 9 power-offs that called exit()
after the quiesce stopped every rt thread in time ended in SIGSEGV in the
driver's exit handler.
What changes for the host: rvg_quiesce_for_exit now answers whether the host
may call exit(). On the rt platform it still parks the library's threads and
quiesces a device that lives, but answers false wherever one lives: a device
kept to the exit (a quit of a stopped or shutdown-paused VM, a display read
that kept it) or one a let-go could not tear down. It answers true where the
release of the last graphics device let go of it. On false the library writes
its degradation counts then, since the host's _exit runs no exit handler.
The warning on false now comes from the platform and only where a wait
outlasted its bound. The Vulkan platform still answers true.
QEMU (vendor/qemu e8f0178b): on false it runs its exit notifiers, the
terminal's restore and the flush of stdio, then ends with _exit.
Tests: the rt child test with the device live now expects false, with rt's
own quiesce answering true, and ends with _exit.
The rt platform kept the process's one rt device in a OnceLock until the
exit. On an RTX 3090 (NVIDIA 620.70.06), 3 of 9 power-offs of an x86-64 macOS
guest then ended in SIGSEGV inside exit(): the driver's exit handler waited
on a lock while a thread of the driver's own read near a null pointer, with
the VkDevice live and every rt thread already stopped by the quiesce.
metal2vulkan-rt's contract asks the consumer to drop the device's last
handle before the process exits.
What changes for the host: the release of the last graphics device
(rvg_graphics_device_release) now lets go of rt's device, as it does of the
Vulkan platform's. Once nothing beyond the platform holds it (at most 5 s),
rt's quiesce runs with what is left of the bound, then the drop of the last
handle tears the device down on the releasing thread, and the let-go waits
for rt's gauges of it to read zero. A device whose quiesce answered false is
kept to the exit. A let-go that waits out its bound logs a warning and keeps
the device, as on the Vulkan platform. A graphics device created after it
runs on a new rt device.
Tests: the let-go test runs on the default platform, so on rt too, and the
test that pinned rt keeping its device (9df71bca5) is removed. A child
process test checks the order of a quit: the let-go, then the quiesce, then
exit().
Where QEMU keeps the reims-graphics device to its exit (a shutdown action
that pauses, a quit of a stopped VM, a display read past its 5 s), and on
the rt platform even after the release of the last graphics device, rt's
submit, completion and block-maker threads can still be in the GPU's driver
as the driver's exit handler tears it down: on NVIDIA's driver, SIGSEGV
inside that handler.
rvg_quiesce_for_exit(timeout_ms) is the host's last call before its exit.
It goes to a new method of core's platform, quiesce: on the rt platform it
closes the exit gate ahead of the exit, so the library's threads park at
their next step, waits for the steps in progress, then runs rt's
Device::quiesce, all within the bound; it frees nothing, answers false where
a thread may still be in the driver, and a second call gives the first
answer at once. The exit gate's hook still runs at the exit and writes the
counts. On the Vulkan platform and the testkit it does nothing and answers
true, so their exits stay as they were.
QEMU calls it at the end of its cleanup, before exit() runs any handler, and
from an exit handler registered as the device is made for exits that skip
the cleanup; it ends with _exit on a false answer (vendor/qemu 88337859).
The C harness and a unit test call it with nothing to stop; an rt GPU test
checks the device's handlers ran, a second call, the closed gate and the
refusal after it; an exit-gate test checks the wait, its deadline and the
counts written at the exit.
Guest-visible: a device heap the bundle pins in a Private block now starts
at a multiple of 256 KiB from that block's first byte, on both platforms,
where it started at the next 32 KiB granule. Heaps' gpuAddress values move;
no other pick does, and no wire field changes.
metal2vulkan-rt binds a heap's texture at the heap's offset in its block's
memory plus the texture's offset, and refuses one whose place there is off
the image's alignment, though Metal's rule (the offset a multiple of
heapTextureSizeAndAlign's align) places it. With heaps on the 32 KiB granule
every texture whose image asks 64 KiB or more (the 780M, Iris Xe, the XTX)
could land off it: pl8rt counted 85 such refusals in one boot, and the
window overview's thumbnails stayed grey.
256 KiB is the largest image alignment measured on the hosts' GPUs (a
multisample target on an RX 7900 XTX) and the current runtime's own heap
base alignment. The pick counts from the block's first byte, not from 0,
because the block's address is aligned only to the host's 64 KiB while its
memory starts at a multiple of 2 MiB on rt. Where no block is held the heap
lies at a multiple of 256 KiB in the context's range. The leading gap stays
in the block's free list, so later smaller picks reuse it once picks wrap.
CUDA maps a buffer of an import made dedicated only from offset 0: from 224,
256, 4096 or 65536 bytes in, cuExternalMemoryGetMappedBuffer answers
CUDA_ERROR_INVALID_VALUE, while a mapping of any length from offset 0 maps
(measured on an RTX 2080, and at 224 on an RTX 3090). ImportedBuffer::import
mapped from the buffer's offset, so no buffer past the first byte of an
exporter's dedicated allocation was ever imported. Where a driver makes each
exportable buffer its own dedicated allocation (docs/video/devices.md says
metal2vulkan does on the RTX 5080, which no check here reaches), a device
cache whose first plane the platform places past its first byte would be
declined, and its pictures read back and copied by the CPU.
The import now maps from the allocation's first byte to the buffer's last and
gives the buffer's address as its offset into that mapping, so no exporter's
layout, on either platform or any driver, depends on CUDA taking an offset.
What the host sees changes only where the import failed before: there a
picture lands in the device cache by the GPU's copy. A test of the pattern
writes a buffer 224 bytes and one a page into a Vulkan device's dedicated
allocation through their own imports and reads where the writes landed.
In the per-shape form every surface's GPU form is its device cache, and the
platform makes no texture over a surface's buffer that is not a shape's. A
block in place named cached whose pages the host made no view of, or no
buffer over, was refused, and the kext gave the surface a copy block whose
textures then lay over no shape. On metal2vulkan-rt, whose platform holds no
lease of the guest's RAM to map views by, that was every such block: a
measured x86-64 boot refused 246 of them, each NotMapped.
Such a block now takes its cache all the same and is answered OK: its pages
are copied by the host's CPU through its mapping of the guest's RAM, each
fill and download through a Shared buffer of its own, as a deferred block's
copied pages already are, and the buffers over the block are the cache. It
is counted as rt-surface-pages-copied, the first logged. Runs that are not
the surface's bytes in whole guest pages, or that the host maps no part of,
are refused as before, and the default form keeps its refusal.
What the guest sees: a cached SURFACE_PAGES answered RGFX_STATUS_OK where it
was RGFX_STATUS_REFUSED, so the surface lies in place over the cache rather
than over a copy block.
Where the device caches every surface in place (SURFACE_CACHE_EVERY), no
host texture lies over a surface's pages: each is a view of an image the host
fills from the plane's place and pitch in the cache and writes back there, by
copies that address them in texels of the view's format. The host's linear
texture alignment, 256 bytes, then turned surfaces into copies for nothing:
15 of the 259 copy views of an rt boot, an x420 3840x2160 video output with
plane 0 at 568256 bytes and rows of 8000 among them, and rt refuses every
texture over a copy.
Under the feature a plane's offset and rows need only be multiples of 16
bytes, the largest texel of a format of texels, so every view's fill and
write-back address the plane whatever format of texels it takes. Without
the feature nothing changes.
For the guest: such a surface's view is in place, cached, where it was a
copy counted for alignment.
Where the device caches every surface the kext gives in place, a view that is
a copy is the fallback. It is still made, never nil, and counted by why:
alignment, unsupported, refused by the kext, or no room in the kext (now
apart from refused). The first of each reason goes out at the default level
with the surface's format and shape, and so does the minute's copies line
where a copy was made in it. A walk's guest log, which keeps the default
level, then shows what still takes copy blocks on rt.
On metal2vulkan-rt (Row 9, ruled (a)), a pages block of pages its process
maps read-only gets one known import of its view, made at its SURFACE_PAGES,
as a writable block does.
What the guest sees on rt:
- That SURFACE_PAGES is answered with the import's GPU address where it was
answered with 0.
- Each BUFFER_OVER_PAGES of bit 24 over the block lies at that address plus
its offset, pinned in the import, over the block's pages. Before, it lay at
the guest's own pick with an import of its own, unpinned.
- A GPU write through such a buffer reaches the pages, where Metal drops it.
That is rt's rt-no-copy-read-only, which held before too.
- A heap, an ICB's slots and a Managed CPU copy over such a block are still
refused.
The Vulkan platform is unchanged: no address, and buffers over copies of the
pages.
The known-import test now holds a read-only block's address, its pinned
buffer and its import's forget. The old platform's test still holds the 0.
Private blocks added a message whose waiter names PRIVATE_BLOCK without adding
it to rgfxLifecycleAnswer, so every take failed in the kext with only an IOLog
line, and the Vulkan platform, which keeps any pin, hid it. The lesson says how
the next one shows: a feature whose host side never sees a single message.
The pool started with no piece, so the guest's first take waited while the
pool's thread made a block and the host mapped it: 47.7 to 58.7 ms on a
Windows host, where the block's pages are touched one by one. The pool now
keeps its one empty piece from the moment the guest's driver takes POOL.
What the host observes: the first piece's pool_add now follows the guest's
DRIVER_FEATURES write that takes RGFX_FEATURE_POOL, from the pool's thread,
instead of following the first POOL_TAKE. A driver that does not take POOL
gets no piece. A take of POOL after a reset asks none while a piece the
guest may use is empty or one is asked. pool_add still never comes before
the device's make returned. The guest's first take is carved from that
piece where the host mapped it by then.
The device tells its host through a new Host::pool_taken, called inside
the write each time the bit goes from not taken to taken; the graphics host
answers it with the pool's headroom rule, the test State::use_piece makes.
The tests that took a block first now map the piece the take of POOL asked
for before the take.
An x86-64 guest on a Windows host now takes the Shared pool. Record what
the pool costs it under WHPX against the region on the mini, with the job
ids: the backing of each range, frames in two boots with the pool and two
without, the probe's bandwidth, and the wait for the boot's first piece.
Put the same probe's numbers on Linux under KVM beside them.
A Windows host offered no pool, so an x86-64 guest under WHP kept the region's
slabs and its processes' own pages until the pool's cost there was measured
beside the region's: what the guest's CPUs get from a pool block, what making
a block stalls, and the frames of a desktop walk, on homelab-mac-mini-2018's
UHD 630 (docs/qemu.md, "Windows hosts: the Shared pool"). An x86-64 guest on a
Windows host is now offered POOL and POOL_ADDRESSES, as on a Linux or macOS
host, and its processes take pool blocks for their buffers' slabs. An arm64
guest, which runs under TCG on a Windows host and was not measured there, is
still offered none.
The pool's tests run on Windows too now, with the two test-only GPU address
reads they use.
An Apple GPU's device (an arm64 guest's) makes a Shared heap, which until now
was a block of the device region, HEAP_OVER_HOST_MEMORY. Since the arm64
guest takes the Shared pool too, that heap was the last of the bundle's
buffers and heaps the region held where the pool is on.
What the guest sees: where the device has the pool (the slabs' one source),
newHeapWithDescriptor: of Shared storage takes a block of the pool of the
heap's size rounded up to 64 KiB and makes it one HEAP_OVER_POOL of the heap's
size, type and options, at the block's GPU address where the host gives one
(POOL_ADDRESSES), else at the process's pick. The heap's gpuAddress is that
address, its buffers lie at their offsets from it, and its bytes are the
block's, which the host zeroed. Its drop gives the block back, and no address
where the host gave it. A heap the pool has no block for, past the 4 GiB
longest block, or with no address left, is nil, as Metal's heap is where its
memory runs out; nothing falls back to the region. Where the pool is off, a
Shared heap is a region block as before. A new shell test makes one at the
host's address and its buffer, and the two nils.
A queue's upload chunk, where set*Bytes: puts its bytes, is a buffer of the
device's slabs, and where the slabs had none for it, a block of the device
region of its own. Where the slabs are the Shared pool's alone (the commit
before), that block was the one region use left for an upload chunk.
What the guest sees: where the pool is on, a chunk the pool has no block for
is none, with no BUFFER_OVER_HOST_MEMORY over a region block, and the command
buffer whose bytes found no room completes with MTLCommandBufferErrorOutOfMemory,
as it did where the region was full, and as Metal's does where its memory runs
out. RgfxSlabs::inRegion says which the slabs are. A new shell test takes a
chunk of a pool that refuses and then gives a block. Where the pool is off,
chunks fall back to the region as before.
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.
The pool's docs said an arm64 26 boot under TCG read its first blocks
unaligned through the kext's own mapping, but the probe's kernel log lines
never appeared, and the range sits just past the guest's RAM, not above 2^36.
Name what the boots showed instead: processes ran on blocks mapped
write-back with no fault under TCG (macOS 26 and 14) and under HVF on an M3
Max (macOS 27.0.1), where each piece maps into the VM as RAM, 16 KiB aligned.
The arm64e kext now carries POOL and POOL_ADDRESSES as the x86-64 kext does:
its processes lay their buffers' slabs in pool blocks the kext maps
write-back, at the GPU addresses the host gives the blocks, where they used
the region's slabs and the kext's chunked pages before. The pool's carried
constants and the arm64 build of the pool blocks' test go, since neither
differs by architecture any more. design.md says which hosts offer the pool
and what the probe boot showed.
metal2vulkan's replay player plays the host video recordings, whose surface
caches the hardware writes outside the runtime: VA-API's decode or video
processing into a dma-buf cache, NVDEC's copy through CUDA's import of an
exportable one. It must refill what lies over such a cache where those writes
become visible to the device: the platform's external_write_done, the
runtime's acquire. The platform made that call unrecorded, so no stream held
the point, and a player had to infer it.
external_write_done now records m2v.externalWriteDone on the buffer, with no
arguments, as the call returns, as rebindNoCopy and every other hooked call
are recorded. The host's video makes the call on the session's landing thread
once the picture is in the cache, right before the session's
setSignaledValue: that releases the decode's command buffer past its wait, so
the record lies after the hardware's writes and before every command that
reads them. A refused acquire is recorded as any refusal is. The record is
counted by its selector in the stats file, and the recorder_fix
"external-write-done" names the change.
Nothing changes for the guest or the C API's host. With REIMS_VGPU_RECORD
unset the call costs one relaxed load more.
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.
This change is visible to the guest. Where the kext and the device both took
PRIVATE_BLOCKS, these objects now get their gpuAddress inside blocks the
host gives the context, rather than in the context's own range in
[2^46, 2^47):
- Private buffers;
- heaps that are not Shared, and the buffers in them;
- Managed buffers, whose GPU copies are what the address names.
On the Vulkan platform those addresses lie in [2^44, 2^45). On
metal2vulkan-rt's device they are the Vulkan device addresses of the host's
memory, which is the reason for the move. Shared buffers, the pool's heaps
and no-copy buffers keep their addresses.
How the bundle picks inside its blocks:
- Picks are next fit through the blocks, on the same span and granule
rules as the context's range.
- A block is taken from the kext when none has room: 256 MiB, or the pick's
span and alignment rounded up to 64 KiB where that is more. 256 MiB is
rt's pooled block, and it keeps a freed span from being picked again soon
after its destroy.
- Free ranges never join across blocks, so no pick spans two of them.
- One empty block is kept for the next picks. Any further empty block is
given back after the last object's destroy has been posted.
- Where the host refuses a take, the bundle picks in the context's range as
before and logs the first refusal.
The serializer tests cover picks in blocks, a block of its own for a large
pick, giving back and keeping, blocks side by side, refusal and threads.
docs/graphics-kext/design.md gains "Private blocks" and the two selectors.
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.
On the arm64 device, a command buffer named in SURFACE_USE only the cached
IOSurfaces it encoded a use of. A surface the GPU reached only through a
residency set the buffer used, or its queue held, was never named: its device
cache kept the generation of its last named use, and a copy-mode surface's
copy was not synced, so the GPU read what it held before the CPU's writes.
What the guest observes changes: at its commit, a buffer names each texture
over an IOSurface, or view of one, committed to a residency set it uses or its
queue holds, as a resource it reads. A cached surface's cache is filled first
where the CPU wrote it since, and a copy syncs. The set keeps its committed
IOSurface members as its own commit changes them, so a buffer's commit reads
them without walking the set. The x86 device makes no residency sets, and is
unchanged.
A cached IOSurface's device cache was filled at the read-write lock that
moves the surface's generation, not at its unlock. A command buffer committed
while a CPU held the lock filled the cache with the pages mid-write, and the
CPU's writes after it reached the GPU only at the surface's next lock.
What the guest observes changes: the first command buffer committed after the
unlock that uses the surface now reads the CPU's writes made under the lock.
The bundle tracks a cached surface's unlock seed from a commit that found it
locked for writing, as copy mode does, and that buffer's SURFACE_USE carries
the new RGFX_SURFACE_USE_REFILL flag. The host fills the cache again at the
generation it holds, unless a command buffer that may write it was committed
since its last copy into the pages, whose write a fill would lose.
The flag is under the new feature bit RGFX_FEATURE_SURFACE_REFILL (bit 58),
which every host offers and both kexts take; the bundle sets it only where
its kext took the bit and answers the surface seeds. The bundle counts the
surfaces named mid-write and the refills, and the host its refills and the
refills it kept from, in their summaries.
A boot found it: the kext's event handler routed answers from a list of its
own, POOL_BLOCK was not in it, and POOL_TAKE's waiter, asleep uninterruptibly
with no deadline, hung WindowServer for good with no fatal line on the host.
Say how to spot the next one.
design.md gains "The Shared pool", which the wire, the host and the kext
cite: the range and who gets it, a take and its wait with no deadline, the
heap over a block, a give and a close, what moves to the pool and what stays
in the region, and why no empty slab of the pool is kept. The slab section
says the pool comes first where the device has one.
The x86-64 kext takes the device's POOL where offered, and sets its own Pool
bit (1 << 15) where it did. A process takes a block through selector 16
(PoolTake): the kext posts its context's POOL_TAKE, maps the block the host
answers with into the process write-back, and lists it. It gives the block
back through selector 17 (PoolGive): the kext unmaps it, then posts the
POOL_GIVE. At the client's close the kext unmaps every block it lists before
the CONTEXT_CLOSE, which gives them all back. HEAP_OVER_POOL joins the
context ring's commands a process submits itself.
The take waits for the host's answer with no deadline, THREAD_UNINT like
the kext's other asks of the host: the host answers each take of an open
context, and the close waits for its takes in flight, so the kext never
gives up on a take whose answer then comes late. Where the host stops
answering, each taking thread waits, holding one of the 32 lifecycle
waiters, until it does.
The arm64e kext takes no POOL and sets no Pool bit: selectors 16 and 17
answer kIOReturnUnsupported there, and its processes keep the region's
blocks and the kext's chunked pages. Selectors 16 and 17 are numbers the
sandbox lets a web content process call.
The times recorded came from a job at potluck's memory priority 4, which
Windows trims and a user's QEMU does not run at (docs/lessons.md). A run at
5 populated 256, 548 and 1024 MiB in 40, 71 and 119 ms, where the run at 4
took 37, 86 and 151 ms.
The walks on the 3090 and the 780M measured what the populate costs and
saves: a removal's BQL hold back under 0.8 ms with its slot delete on the
library's thread, adds as before at the median with a 3-of-72 tail of single
slot creates, and no workload slower beyond the walks' spread.
The populate maps a whole pool piece, so KVM's delete of a removed piece's
slot zaps all of it: 3.0-8.3 ms for a 256 MiB piece on the 780M and 3090
hosts, under the BQL, where a removal held it 0.8-1.4 ms p50 before the
populate. QEMU's 7ad9c809 deletes the slots on the library's thread before
the removal is queued (kvm_unmap_before_del), as WHPX unmaps a removed
piece there, so the removal's transaction finds no slot.
QEMU's reims-graphics-pci now fills the second-level tables of each pool
piece once its add is answered, on a thread of its own in 2 MiB slices, so
the guest's first touch of a piece takes no fault where it took one a 4 KiB
page: 140,289 for 548 MiB, 184 ms on um870's 3090 and 262 ms on um790's
780M under KVM, and 1.3 s on the Windows mini under WHPX. Under KVM the
call is KVM_PRE_FAULT_MEMORY on a vCPU of the thread's that never runs,
which keeps KVM's master clock on across guest reboots and is never waited
for under the BQL; under WHPX it is WHvAdviseGpaRange's populate.
vendor/qemu moves to b561d79c (dd0e0d4a..b561d79c on 0590cfde), and
docs/qemu.md says what the populate does.
metal2vulkan's replay player must stop, typed, where the GPU may reach a
no-copy buffer through an address in memory alone. The guest says so with
BUFFER_ESCAPED, but the note ended in the decoder: no stream carried it, and
only the stats file's counted totals showed it.
The decoder's first note for each buffer now also records it, where the
process records (REIMS_VGPU_RECORD): an m2v.bufferEscaped call on the
buffer's stream id, [kind], the note's RGFX_BUFFER_ESCAPED_* act (0
gpuAddress, 1 argument encoder, 2 resource id, 3 residency set), of the whole
buffer. It is queued on the decoder's thread at the note, so it lies after
the m2v.write records of each commit made before the note and before those of
each commit made after it. A later note, a note for a refused create and a
bad reference write none. Each record is counted by its selector in the stats
file, and the recorder_fix "buffer-escaped" names the change.
Nothing changes for the guest or the C API's host. With REIMS_VGPU_RECORD
unset the note costs one more relaxed load (recorder::on).
The pass that zeroes the scanout's room for a new owner runs only when a new
WindowServer process opens the server connection, never at boot or on the
same WindowServer's reopen. It zeroes the display's whole share, so the host
backs all of it from the first replacement on. The paragraph now says so, with
the measured stores on KVM (103 ms) and WHP (802 ms).
Measured on the UHD 630 Windows host, a guest page under WHPX costs about
20 us the first time the guest touches it, guest RAM and the device's
region alike, and a page the host already backs about 1.5 us. qemu.md now
holds those costs, the region's use at the desktop, and the largest
first-touch burst between two presents, so later sizing reads them there.
The same runs found that potluck's Windows jobs start at memory priority 4,
and that Windows trims a priority-4 QEMU's working set with tens of GB free:
703,604 RAM and 9,637 region pages in one walk, each costing 19 to 32 us to
fault back, and none at priority 5, which a user's shell gives. lessons.md
records the trap and how to spot it, since a timing taken in such a job pays
faults a user's VM does not.
The kext zeroed each span of a new block through a kernel mapping it made for
that span and took down after it. Over a boot to the desktop and a probe's 905
new bodies (rzboot2, a macOS 26.7.2 guest on a 780M host), making and taking
down those mappings took 402.5 ms of the zeroing's 433.3 ms, and they were its
longest waits at boot: 114 ms for one staging chunk, 19 ms for one surface
header.
The kext now maps the blocks' range into the kernel once for each cache mode at
its start, write-back from the region and write-combined from the region's
write-combined descriptor. It keeps both mappings for the kernel's life, as it
keeps the doorbells' mapping, and writes a new block's zeros through them, so a
block's zeros cost only their stores. The kept mappings hold the kernel's
address space and page tables only, and none of the host's memory: no page of
the region is touched until it is zeroed or used. On a Windows host (WHP) the
kext's zeros are the first touch of a region page that was never touched
before. Where the kernel will not make a kept mapping, each span is mapped
alone, as before. The kext logs each kept mapping's size and how long it took
to make, once at its start.
The scanout's room holds the display's pixels, and the region keeps them past
the process that wrote them. A task that opened the framebuffer's server
connection after another (the next WindowServer, after a crash or a logout)
was handed the room through the VRAM range, a surface lock or the scanout
grant with the last owner's last frame still in it.
The framebuffer now zeroes the display's room when a task that is not its
last owner opens the server connection, through a write-back mapping, the mode
the windows and the locks map the scanout in. The task owns nothing until the
zeros are in, and every other task's hold of the scanout is taken back first.
A room the kernel will not map for the zeros fails the open. The last owner
opening again keeps its bytes, and the first owner finds the boot screen,
which no process wrote and which the first present clears, as before.
What the guest sees: the screen shows black, not the old owner's last frame,
from the new owner's open until its first present. The pass is one zeroing of
the display's room, up to 128 MiB, per new owner, logged with its mapping and
store time.
scanout_owner_test checks that a new owner's first read of the room is zero
and that it owns nothing before, and that the last owner's open again keeps
its bytes.
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.
On Linux and Windows, a no-copy buffer over pages the process maps read-only
lies over a copy of those pages. Once such a buffer's identity has escaped,
the GPU may read the copy through memory after the guest's CPU has changed
the pages, with no binding naming the buffer. This commit records how often
that situation arises, and pins the runtime behaviour that keeps it correct.
The behaviour is unchanged: the decoder adds nothing to any command buffer.
Metal requires an encoder to make resident (useResource:usage:) any buffer a
function reaches through memory, and that names the buffer as a use. At the
commit, the runtime copies in every copy that a command buffer's uses name,
and every copy where its work may reach memory that no use names
(metal2vulkan's command_buffer/mod.rs:4747-4772 at the pinned commit, which
picks the copies at 4754-4766). A reach of a buffer that no encoder made
resident is undefined in Metal, and on the runtime it reads zero
(residency.rs).
What the decoder adds is bookkeeping:
- each process's count of its live escaped copies (Context::escaped_copies),
which a copy joins at its first note and leaves when it is dropped;
- escaped-copy-commits, which counts each command buffer begun by a process
that holds one, in the degradation counts line and the recorder's stats.
The first such command buffer is logged at info.
on_vulkan_an_escaped_copy_reached_by_its_address_reads_the_cpus_writes pins
today's runtime. A kernel reaches the escaped copy only through an address it
loads, with the encoder having made the copy resident. It reads the pages' old
bytes, then their new ones after the CPU's write.
The host reads a no-copy buffer's bytes in place. The guest's Metal can hand
that buffer's identity to the app, or write it where the GPU reads, with no
message naming the buffer, and from then on the GPU may reach the buffer
through memory alone. The host had no way to know which buffers that happened
to. This commit adds a note for it, so the host can tell them apart.
The note is RGFX_CMD_BUFFER_ESCAPED (0x23b), RgfxBufferEscaped of 32 bytes:
the buffer's object id and the act, one of GPU_ADDRESS, ARGUMENT_ENCODER,
RESOURCE_ID and RESIDENCY_SET. The device offers it as
RGFX_FEATURE_BUFFER_ESCAPED (bit 56), and both kexts carry it. The submit gate
takes it in a batch, like a Managed range's write: unwaited, a fixed size,
naming no block and making no object.
The bundle marks each covered buffer as it is made (_rgfxEscape, set by
rgfxOwesEscapeNote) where the host took the feature. The first of these acts
in the buffer's life posts the note, before the act returns:
- a read of the buffer's gpuAddress;
- an argument encoder's setBuffer(s) of it, or setTexture(s) of a texture or
view over it;
- a read of such a texture's or view's gpuResourceID;
- the buffer, or such a texture, joining a residency set.
Every later act is one relaxed load.
Wire order holds the note ahead of any command buffer that could use the
identity. The note goes on ring 0, posted or held in the serialiser to go
before the next commit's messages, and the device carries out a command buffer
only after every ring-0 message its process posted before it. Across threads,
the act's slow path posts the note under the buffer's lock and then clears the
mark with a release store. Every commit takes an acquire fence before it posts,
so a commit after an act that found the mark clear posts after the note.
The covered buffers are the app's no-copy buffers over its own pages
(BUFFER_OVER_PAGES, writable or read-only, bit 24) and over bytes already in
a block of the process (BUFFER_OVER_HOST_MEMORY). Managed buffers, heaps over
pages, IOSurface shadows and buffers placed in a slab's heap
(BUFFER_AT_OFFSET) are not covered.
A slab buffer made with an address of its own is covered too: a Shared buffer
past 2 MiB over a slab of the process's own pages, a device buffer, or a
no-copy buffer's shadow (RgfxSlabSource::Made's ownsAddress). It reaches the
host as the same BUFFER_OVER_PAGES or BUFFER_OVER_HOST_MEMORY message as an
app's no-copy buffer, and the host takes every such message alike. Leaving
slab buffers out would leave the host unable to read the mark as "this
buffer's identity never left".
The host marks the buffer's entry escaped, once for its life, and a later
note changes nothing. A note for an object that is not such a buffer is a bad
reference. A note for an object whose create the host refused is dropped, as
any naming of one is. Escapes are counted in the degradation counts line and
in the recorder's stats (`counted <code> <n>` lines), with no new stream
records:
- buffer-escaped-gpu-address, -argument-encoder, -resource-id and
-residency-set, by the act that noted the escape;
- buffer-escaped-read-only-pages, the escaped buffers over a copy of their
pages;
- no-copy-buffers, every no-copy buffer made, of which the escapes are a
share.
Each act's first escape is also logged at info.
This changes nothing the guest observes. No one waits for the note, it has no
reply, and the mark changes no answer, object, address or command. Both
prebuilt sets and the option ROM's record are rebuilt from these sources. The
ROM's bytes do not change: its record changes only by the wire header's hash.