🐎 Give the kext's chunks at multiples of 64 KiB of guest RAM
The bundle's slabs of the kext's chunks (PagesChunked) reach the host as runs of 64 KiB, but each chunk lay at any multiple of 4 KiB: the physical mask 0x0000FFFFFFFFF000 asks IOBufferMemoryDescriptor for page alignment alone (xnu's initWithPhysicalMask takes the alignment from the mask's clear low bits, read from its source). On RADV the host binds a buffer over several pieces of its import of guest RAM only where every piece lies at a multiple of the sparse buffer's alignment, 64 KiB on Mesa 26.2, so a buffer over a slab of several chunks took an import of its own, which every submission lists. On a 780M walk (macOS 26.7.2, Launchpad), 15 of 92 such buffers had their first chunk at a multiple of 64 KiB, and about 24 of them were live at once. The chunks now come with the mask 0x0000FFFFFFFF0000 (kRgfxPagesChunkMask), each at a multiple of its own 64 KiB, so a slab's runs start and end on the host's granule. A slab the kernel gives no such chunks for takes plain pages, as before (test_pages_slab_source_chunks). Nothing the guest sees changes: the chunks are zero-filled and mapped as before, and the host picks the same GPU addresses and ids. On ANV, which binds no dma-buf buffer in several pieces, nothing changes.
This commit is contained in:
1 parent
74f6a6a003
commit
f5b6cd95a5
5 files changed
+84
-22
No files matched your search
@@ -3874,9 +3874,14 @@ the host's import, and the kext's block table holds one entry per slab:
|
||||
deallocates its pages.
|
||||
- The kext's chunks first (selector 12, `PagesChunked`, where the kext's word
|
||||
holds `PagesChunked`): the kext takes the slab's bytes in physically
|
||||
contiguous chunks of 64 KiB, 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
|
||||
of guest RAM at most, where plain pages are up to 512. The host maps each
|
||||
contiguous chunks of 64 KiB, each at a multiple of 64 KiB of guest RAM
|
||||
(the physical mask `0x0000FFFFFFFF0000`), 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 of guest RAM at most, where plain pages are up to 512. Each run
|
||||
starts and ends at a multiple of 64 KiB, the granule at which the host binds
|
||||
a buffer over several pieces of its import of guest RAM (RADV's sparse
|
||||
buffer alignment), so such a buffer lies in the guest RAM's import rather
|
||||
than in an import of its own on every submission's list. The host maps each
|
||||
run of a view apart, against a budget of mappings per process
|
||||
(`vm.max_map_count` on Linux, 65530 by default): on NVIDIA, views of
|
||||
scattered pages, slabs' among them, used it up, and the buffers made after
|
||||
@@ -3886,8 +3891,8 @@ the host's import, and the kext's block table holds one entry per slab:
|
||||
kernel finds at once however long the guest has run: on the guest and host
|
||||
the superpages below were measured on, 4,096 chunks in a row each took
|
||||
0.033 ms, and 0.036 ms once 978 MiB of 4 KiB pages had been taken, the
|
||||
slowest 0.74 ms, none refused. A slab the kext gives no chunks for is plain
|
||||
pages.
|
||||
slowest 0.74 ms, none refused (chunks then at any multiple of 4 KiB). A slab
|
||||
the kext gives no chunks for is plain pages.
|
||||
- Plain pages, never superpages (`VM_FLAGS_SUPERPAGE_SIZE_2MB`, 2 MiB of RAM in
|
||||
one run): superpages run out, and asking for one stalls the process, on
|
||||
whatever thread asks. Measured on an x86-64 macOS 26.7.2 guest of 8 GiB on
|
||||
|
||||
@@ -2837,8 +2837,8 @@ IOReturn RgfxAccelerator::pagesChunked(RgfxAcceleratorUserClient *client, UInt32
|
||||
return kIOReturnUnsupported;
|
||||
if (length == 0 || length % kRgfxPagesChunk != 0 || length > kRgfxPagesChunkedMaxBytes)
|
||||
return kIOReturnBadArgument;
|
||||
// The chunks, each a run of RAM of its own, wired as the kernel's memory is, and zero-filled:
|
||||
// the process sees them.
|
||||
// The chunks, each a run of RAM of its own at a multiple of its bytes (kRgfxPagesChunkMask),
|
||||
// wired as the kernel's memory is, and zero-filled: the process sees them.
|
||||
const UInt32 count = static_cast<UInt32>(length / kRgfxPagesChunk);
|
||||
const vm_size_t listBytes = count * sizeof(IOMemoryDescriptor *);
|
||||
IOMemoryDescriptor **chunks = static_cast<IOMemoryDescriptor **>(IOMalloc(listBytes));
|
||||
@@ -2849,7 +2849,7 @@ IOReturn RgfxAccelerator::pagesChunked(RgfxAcceleratorUserClient *client, UInt32
|
||||
IOBufferMemoryDescriptor *chunk = IOBufferMemoryDescriptor::inTaskWithPhysicalMask(
|
||||
kernel_task,
|
||||
kIODirectionOutIn | kIOMemoryPhysicallyContiguous | kIOMemoryKernelUserShared,
|
||||
kRgfxPagesChunk, 0x0000FFFFFFFFF000ULL);
|
||||
kRgfxPagesChunk, kRgfxPagesChunkMask);
|
||||
if (!chunk)
|
||||
break;
|
||||
bzero(chunk->getBytesNoCopy(), kRgfxPagesChunk);
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
// the kext for as long as the block lives, which the device maps in place for the process's
|
||||
// buffers over them (RGFX_FEATURE_PAGES_BUFFERS, BUFFER_OVER_PAGES). A chunked pages block
|
||||
// (kRgfxAccelMethodPagesChunked, 12) is one over the kext's own chunks of kRgfxPagesChunk bytes
|
||||
// instead, mapped into the process, its runs found the same way.
|
||||
// instead, each one run of guest RAM at a multiple of its bytes (kRgfxPagesChunkMask), mapped into
|
||||
// the process, its runs found the same way.
|
||||
//
|
||||
// Memory the process maps read-only takes a block where the guest took RGFX_FEATURE_READ_ONLY_PAGES
|
||||
// alone: its pages are wired for reading, and the block's SURFACE_PAGES is marked
|
||||
@@ -26,11 +27,35 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "RgfxUserClientABI.hpp"
|
||||
#include "reims_graphics_wire.h"
|
||||
|
||||
// The most bytes one pages block covers, its pages rounded out: 64 GiB, more than a guest has.
|
||||
constexpr uint64_t kRgfxPagesBlockMaxBytes = 1ull << 36;
|
||||
|
||||
// The physical alignment of the memory IOBufferMemoryDescriptor::inTaskWithPhysicalMask gives
|
||||
// under `mask`: one more than the mask's clear low bits, and at least a `page`. Read from xnu's
|
||||
// source (iokit/Kernel/IOBufferMemoryDescriptor.cpp, initWithPhysicalMask, which
|
||||
// inTaskWithPhysicalMask calls with an alignment of 1): `alignment = (~mask & (mask - 1)) + 1`,
|
||||
// raised to the page; IOKernelAllocateWithPhysicalRestrict then asks kmem_alloc_contig for
|
||||
// physical pages at a multiple of it. A mask of 0 asks for no alignment: the page.
|
||||
constexpr uint64_t rgfxPhysicalMaskAlignment(uint64_t mask, uint64_t page)
|
||||
{
|
||||
const uint64_t alignment = mask == 0 ? page : (~mask & (mask - 1)) + 1;
|
||||
return alignment < page ? page : alignment;
|
||||
}
|
||||
|
||||
// The physical mask of the chunks of a chunked pages block (kRgfxAccelMethodPagesChunked): guest
|
||||
// RAM below 2^48, each chunk at a multiple of its own bytes, so a block of chunks is runs of
|
||||
// guest RAM that start and end at multiples of 64 KiB, the granule at which the host binds a
|
||||
// buffer over several pieces of its import of guest RAM (a sparse buffer's alignment on RADV),
|
||||
// rather than importing the block's pages apart.
|
||||
constexpr uint64_t kRgfxPagesChunkMask = 0x0000'FFFF'FFFF'0000ull;
|
||||
static_assert(rgfxPhysicalMaskAlignment(kRgfxPagesChunkMask, 0x1000) == kRgfxPagesChunk,
|
||||
"each chunk at a multiple of its own bytes");
|
||||
static_assert(rgfxPhysicalMaskAlignment(kRgfxPagesChunkMask, 0x4000) == kRgfxPagesChunk,
|
||||
"on an arm64 guest's 16 KiB pages too");
|
||||
|
||||
// The pages a call's range lies in: `length` bytes from `base`, a page, and the call's first byte
|
||||
// `offset` bytes into them.
|
||||
struct RgfxPagesRange {
|
||||
|
||||
@@ -112,12 +112,13 @@ enum {
|
||||
// A pages block over memory of the kext's own (kRgfxKextFeaturePagesChunked;
|
||||
// RgfxPagesBlock.hpp): in the memory's length, a multiple of kRgfxPagesChunk (below); out its address
|
||||
// in the process, the block's region offset and its size. The kext takes the length in
|
||||
// physically contiguous chunks of kRgfxPagesChunk bytes each, zero-filled, maps them into the
|
||||
// process in order, read and write, and makes the block of their runs as PagesBlock makes one
|
||||
// of the process's pages: so a 2 MiB slab is a few runs of the guest's RAM where the
|
||||
// process's own pages are hundreds. The memory is the block's: the process never unmaps it,
|
||||
// and kRgfxAccelMethodFree of the block takes it with the block, unmapped from the process and
|
||||
// freed once the device has answered the block's release, or the client's close.
|
||||
// physically contiguous chunks of kRgfxPagesChunk bytes each, each at a multiple of its bytes
|
||||
// in guest RAM, zero-filled, maps them into the process in order, read and write, and makes the
|
||||
// block of their runs as PagesBlock makes one of the process's pages: so a 2 MiB slab is a few
|
||||
// runs of the guest's RAM where the process's own pages are hundreds. The memory is the
|
||||
// block's: the process never unmaps it, and kRgfxAccelMethodFree of the block takes it with the
|
||||
// block, unmapped from the process and freed once the device has answered the block's release,
|
||||
// or the client's close.
|
||||
// kIOReturnUnsupported as for PagesBlock; kIOReturnBadArgument for a length of no chunks, not a
|
||||
// multiple of one, or past kRgfxPagesChunkedMaxBytes; kIOReturnNoMemory where the kernel has
|
||||
// not that many chunks to give, which the process then takes plain pages for.
|
||||
@@ -305,12 +306,13 @@ enum {
|
||||
kRgfxSharedEventMethodImport = 11,
|
||||
};
|
||||
|
||||
// PagesChunked's (12) chunk, 64 KiB, and the most bytes one call takes, 1 GiB. A 64 KiB run of
|
||||
// RAM is one the kernel finds at once, however long the guest has run: measured on an x86-64
|
||||
// macOS 26.7.2 guest of 8 GiB on the Radeon 780M host, 4,096 such chunks in a row each took
|
||||
// 0.033 ms, and 0.036 ms once 978 MiB of 4 KiB pages had been taken, none longer than 0.74 ms,
|
||||
// none refused. Of 128 runs of 2 MiB asked, it gave 28 on the quiet guest and 16 once memory had
|
||||
// been taken, the slowest in 264 ms.
|
||||
// PagesChunked's (12) chunk, 64 KiB, each at a multiple of 64 KiB of guest RAM
|
||||
// (RgfxPagesBlock.hpp, kRgfxPagesChunkMask), and the most bytes one call takes, 1 GiB. A 64 KiB
|
||||
// run of RAM is one the kernel finds at once, however long the guest has run: measured on an
|
||||
// x86-64 macOS 26.7.2 guest of 8 GiB on the Radeon 780M host, with chunks at any multiple of
|
||||
// 4 KiB, 4,096 such chunks in a row each took 0.033 ms, and 0.036 ms once 978 MiB of 4 KiB pages
|
||||
// had been taken, none longer than 0.74 ms, none refused. Of 128 runs of 2 MiB asked, it gave 28
|
||||
// on the quiet guest and 16 once memory had been taken, the slowest in 264 ms.
|
||||
enum {
|
||||
kRgfxPagesChunk = 0x10000,
|
||||
kRgfxPagesChunkedMaxBytes = 0x40000000,
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
// the call's offset in the first, its runs counted and then written as one per stretch of
|
||||
// contiguous segments, and its size in whole pages; and a span's runs and length taken to the
|
||||
// end of its last 16 KiB page, as PAGES_16K names them; and how a block's pages are wired, and
|
||||
// marked, for memory the process maps writable or read-only.
|
||||
// marked, for memory the process maps writable or read-only; and the physical alignment of the
|
||||
// kext's chunks.
|
||||
#include "RgfxPagesBlock.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
@@ -129,6 +130,35 @@ int main()
|
||||
check(rgfxPagesWiring(false, 0).access == RgfxPagesAccess::kRefused,
|
||||
"nor with no feature at all");
|
||||
|
||||
// The chunks' physical mask: each chunk at a multiple of its own 64 KiB, on 4 KiB and on
|
||||
// 16 KiB pages; a mask that clears only the page's bits gives the page alone.
|
||||
check(rgfxPhysicalMaskAlignment(kRgfxPagesChunkMask, kPage) == kRgfxPagesChunk,
|
||||
"the chunks' mask: 64 KiB");
|
||||
check(rgfxPhysicalMaskAlignment(kRgfxPagesChunkMask, kRgfxPage16K) == kRgfxPagesChunk,
|
||||
"the chunks' mask on 16 KiB pages: 64 KiB");
|
||||
check(rgfxPhysicalMaskAlignment(0x0000'FFFF'FFFF'F000ull, kPage) == kPage,
|
||||
"a mask clear of the page's bits alone: the page");
|
||||
check(rgfxPhysicalMaskAlignment(0x0000'FFFF'FFFF'F000ull, kRgfxPage16K) == kRgfxPage16K,
|
||||
"raised to the page");
|
||||
check(rgfxPhysicalMaskAlignment(0x0000'0000'FFFF'FFFFull, kPage) == kPage,
|
||||
"a 32-bit mask asks no more than the page");
|
||||
check(rgfxPhysicalMaskAlignment(0, kPage) == kPage, "no mask: the page");
|
||||
check(kRgfxPagesChunkMask == (((1ull << 48) - 1) & ~uint64_t{kRgfxPagesChunk - 1}),
|
||||
"any multiple of 64 KiB of guest RAM below 2^48");
|
||||
// A block of four such chunks, two of them meeting: its runs start and end at multiples of
|
||||
// the chunk, so every boundary between two lies on the host's 64 KiB granule.
|
||||
Pages chunks = {};
|
||||
for (uint64_t chunk : {0x4'0000ull, 0x5'0000ull, 0x9'0000ull, 0x2'0000ull})
|
||||
for (uint64_t at = 0; at < kRgfxPagesChunk; at += kPage)
|
||||
chunks.pages.push_back(chunk + at);
|
||||
RgfxPageRun chunkRuns[4] = {};
|
||||
check(rgfxPagesRuns(4 * kRgfxPagesChunk, chunks, chunkRuns, 4) == 3,
|
||||
"four chunks, two meeting: three runs");
|
||||
for (const RgfxPageRun &run : {chunkRuns[0], chunkRuns[1], chunkRuns[2]})
|
||||
check(run.address % kRgfxPagesChunk == 0 && run.length % kRgfxPagesChunk == 0,
|
||||
"each run of chunks starts and ends at a multiple of 64 KiB");
|
||||
check(chunkRuns[0].length == 2 * kRgfxPagesChunk, "the two chunks that meet are one run");
|
||||
|
||||
std::printf("pages_block_test: OK (%d checks)\n", checks);
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user