From 158f7220ff87fa8779edbbf296e34a26d4a6a1fa Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sat, 7 Nov 2026 12:22:12 -0800 Subject: [PATCH] Pages the process maps read-only are a copy whose GPU writes are dropped, on every device On Linux, a no-copy buffer, heap or rebind over pages the process maps read-only went to the caller's dma-buf source wherever it gave one, and so did pages an import alignment above the host page left with no pointer import (by_pointer false), which skip the gate. A dma-buf of the pages takes the GPU's writes, which Metal drops (an M3 Max under macOS 27.0.1: the CPU reads read-only pages as they were). The runtime announced that as read-only-pages-written. Without a source the pages were refused, where Metal makes the buffer. The owner's rule (10-23) is to drop such writes on every device, never refuse them. Such pages are now a copy whose GPU writes are dropped (copied.rs), with or without a source, which is never called for them. The gate is still asked first where the pointer would be imported, and its refusal is still counted as a refusal. Counted: WorkCounts::copied_unwritable_pages (new, public), announced as copied-host-pages with its own cause. Degradation::ReadOnlyPagesWritten is removed (public: api_signatures, docs/API.md's catalogue, the lane's notice registry), and so are HostWindow::unwritable and degrade_read_only_pages. Tests: the dma_buf_imports lane test is now read_only_pages_are_a_copy_whose_gpu_writes_are_ dropped. It fails at the base, where the sourced buffer's fill reaches the file's pages and the unsourced one is refused. The host_memory lane test a_read_only_page_is_a_copy_on_linux_and_ read_elsewhere replaces the refusal test. The runtime's copied_device tests cover a new buffer and a rebind over read-only pages whose gate admits them: a copy, the source unasked. The keep-alive order test uses writable memfd pages, which RADV imports as dma-bufs all the same. (cherry picked from commit 68805d8a5742590e157b089f336c17e8e1dc4ef4) --- .../src/buffer_descriptor.rs | 102 +++++++--------- .../metal2vulkan-runtime/src/degradation.rs | 16 +-- .../metal2vulkan-runtime/src/host_backing.rs | 5 +- .../metal2vulkan-runtime/src/host_memory.rs | 35 +++--- .../src/tests/copied_device.rs | 87 ++++++++++---- .../metal2vulkan-runtime/src/work_counts.rs | 15 ++- .../src/runtime_lane/degrade_notices.rs | 6 +- .../src/runtime_lane/dma_buf_imports.rs | 112 +++++++++--------- .../src/runtime_lane/host_memory.rs | 54 +++++---- .../src/runtime_lane/pitched_surfaces.rs | 5 +- crates/metal2vulkan/tests/api_signatures.tsv | 2 +- docs/API.md | 18 +-- docs/HOWTO.md | 5 +- 13 files changed, 244 insertions(+), 218 deletions(-) diff --git a/crates/metal2vulkan-runtime/src/buffer_descriptor.rs b/crates/metal2vulkan-runtime/src/buffer_descriptor.rs index 8562e2941..f5ef8bf9b 100644 --- a/crates/metal2vulkan-runtime/src/buffer_descriptor.rs +++ b/crates/metal2vulkan-runtime/src/buffer_descriptor.rs @@ -579,7 +579,7 @@ impl Device { kept_runs.as_ref(), window.offset, ); - let (buffer, memory, again, unwritable) = match in_chunk { + let (buffer, memory, again) = match in_chunk { // A buffer (not a heap) bound in an import of its own run alone, its chunk not // imported yet, is released while idle and bound in the chunk's import at its next // use where that is made by then (`host_residency.rs`). @@ -590,15 +590,16 @@ impl Device { .map(|run| Again::ram_run(self, run, (length, window, class))); #[cfg(not(unix))] let again = None; - (buffer, memory, again, false) + (buffer, memory, again) } None => { // A no-copy buffer or heap whose pointer the device would import (not one whose GPU // writes are dropped) may be a copy instead (`copied.rs`): where the caller's gate // refuses the import, asked here with the pages it would map, holding no lock of // the runtime's, before their write permission is asked (a copy pins no page, and - // drops the GPU's writes to pages the process cannot write); or where the import it - // admitted fails, of the pointer or of the caller's dma-bufs. + // drops the GPU's writes to pages the process cannot write); where the process + // cannot write them; or where the import it admitted fails, of the pointer or of + // the caller's dma-bufs. let copyable = !drops_writes; let copy = (call, pointer, length, modes, placement, of_heap); // Memory whose GPU writes Metal's private option bit 24 drops, where the system @@ -634,8 +635,14 @@ impl Device { // Only a buffer that imports its pages pins them, and so has their writability // asked: one that lies in a live import pins none, that import's making asked for // the same window, and the pages stay writable while imported (`HostMemory::new`). - let host = writability_settled(host, writable, &shared.counts)?; - let unwritable = host.unwritable; + // Pages the process cannot write are a copy whose GPU writes are dropped, as + // Metal drops them, their source never called ([`unwritable_pages`]). + if unwritable_pages(&host, writable, &shared.counts)? { + // SAFETY: the caller's contract. + let copied = + unsafe { self.copied_buffer(copy, release, dirty, CopiedFor::Unwritable) }; + return copied.map(|buffer| noted(kept(buffer, log, kept_runs))); + } // SAFETY: the window lies in the pages of the caller's bytes, which stay mapped // (`HostMemory::new`), and the source's pieces hold them // (`HostMemory::with_dma_buf`). No lock of the runtime's is held. A no-copy @@ -680,12 +687,9 @@ impl Device { } Err(error) => return Err(error), }; - (buffer, memory, again, unwritable) + (buffer, memory, again) } }; - if unwritable { - self.degrade_read_only_pages(call, pointer, length); - } let (address, reserved) = match self.place_window(placement, &window, length) { Ok(held) => held, Err(error) => { @@ -925,13 +929,12 @@ impl Device { if refused_by_host { shared.counts.host_import_refused_by_host(); } - // Only pages imported here are pinned, and so have their writability asked. + // Only pages imported here are pinned, and so have their writability asked; pages + // the process cannot write are a copy whose GPU writes are dropped + // ([`unwritable_pages`]). let imports = !chunk_bound && !refused_by_host && !moved && !read_only_copy; - let host = match imports { - true => writability_settled(host, writable, &shared.counts)?, - false => host, - }; - let unwritable = imports && host.unwritable; + let unwritable = imports && unwritable_pages(&host, writable, &shared.counts)?; + let imports = imports && !unwritable; // The heap's images bound to its bytes, and the images in place over the bytes, // are made again over the copy (`Device::rebound_heap_images`, // `Device::rebound_in_place_images`): its memory holds whole pages from a multiple @@ -1056,15 +1059,13 @@ impl Device { (true, _) => CopiedBecause::Moved, (false, Some(_)) => CopiedBecause::Failed, (false, None) if read_only_copy => CopiedBecause::ReadOnly, + (false, None) if unwritable => CopiedBecause::Unwritable, (false, None) => CopiedBecause::Refused, }; let (host, placement, copied) = copy(failed)?; (host, placement, Remade::default(), Some((copied, cause))) } }; - if unwritable && copied.is_none() { - self.degrade_read_only_pages(call, pointer, length); - } // A copy lies at the offset it was laid out at: the old one where the bytes moved. let offset = match copied { Some(_) => copy_window.offset, @@ -1289,7 +1290,6 @@ impl Device { window, by_pointer, source, - unwritable: _, drops_writes, } = host; let refusal = match by_pointer { @@ -1401,21 +1401,6 @@ impl Device { match source {} } - /// Announce that `call` made the `length` bytes at `pointer`, over pages the process maps - /// read-only, from the caller's dma-buf source, whose memory the GPU writes - /// (`Degradation::ReadOnlyPagesWritten`). - fn degrade_read_only_pages(&self, call: &'static str, pointer: NonNull, length: u64) { - self.degrade(Degradation::ReadOnlyPagesWritten, || { - ( - format!("{call} over {length:#x} bytes at {pointer:p}"), - "the process maps some of their pages read-only, which Linux drivers do not \ - import by pointer, so they are imported as dma-bufs of their pages from the \ - caller's source" - .to_owned(), - ) - }); - } - /// A copy of the `length` bytes at `pointer` for a rebind's new pages (`copied.rs`), laid /// out as their import of `window` would be: a `VkBuffer` from the window's first page, the /// bytes `window.offset` into it, in host-visible memory of the device's own, so the texel @@ -1587,9 +1572,13 @@ impl Device { "Metal's private option bit 24 (0x1000000) makes them memory the GPU's \ writes do not reach, and no import of them here keeps the GPU's writes out" } + CopiedBecause::Unwritable => { + "the process maps some of them read-only, which Linux drivers pin no pointer \ + of, and whose dma-bufs would take the GPU's writes, which Metal drops" + } }; let dropped = match (drops, cause) { - (true, CopiedBecause::ReadOnly) => { + (true, CopiedBecause::ReadOnly | CopiedBecause::Unwritable) => { ", whose GPU writes are dropped, as Metal drops them, and never written to \ the pages" } @@ -1671,6 +1660,7 @@ impl Device { CopiedFor::Refused => (CopiedBecause::Refused, None), CopiedFor::Failed(failed) => (CopiedBecause::Failed, Some(failed)), CopiedFor::ReadOnly => (CopiedBecause::ReadOnly, None), + CopiedFor::Unwritable => (CopiedBecause::Unwritable, None), }; let shared = self.shared(); let raw_device = shared.vk(); @@ -2008,33 +1998,25 @@ struct HostWindow { by_pointer: bool, /// The caller's dma-buf source, where it gave one and the device imports dma-bufs. source: Option, - /// The process cannot write every page, so they go to the source alone - /// ([`writability_settled`]). - unwritable: bool, } -/// `host`, its pages' write permission settled, for a call that imports them itself (not a -/// buffer that lies in a live import of them): Linux drivers pin a pointer's imported pages for -/// writing, so where the process cannot write one of them ([`refuse_unwritable`]) the pointer is -/// not imported, and the pages go to the caller's dma-buf source where it gave one the device can -/// use (a dma-buf of the pages pins nothing of the process's mapping, and the GPU's writes reach -/// the pages: `unwritable`, announced), else are refused. Pages whose write permission the caller -/// vouched for (`vouched`, `HostMemory::assume_writable`) are not asked about. -fn writability_settled( - host: HostWindow, - vouched: bool, - counts: &Counters, -) -> Result { +/// Whether the process cannot write some of `host`'s pages, asked by a call that imports them +/// itself (not a buffer that lies in a live import of them): such pages are a copy whose GPU +/// writes are dropped (`copied.rs`), on every device, never imported and their dma-buf source +/// never called. Linux drivers pin a pointer's imported pages for writing (RADV refused such a +/// page, NVIDIA failed it as out of device memory, ANV imported it and lost the device at the +/// GPU's first read), and a dma-buf of them would take the GPU's writes, where Metal's leave the +/// CPU's view of them as it was (an M3 Max under macOS 27.0.1, private and shared memory alike). +/// Pages whose write permission the caller vouched for (`vouched`, `HostMemory::assume_writable`) +/// are not asked about. Refused where the process's mappings cannot be asked +/// ([`refuse_unwritable`]); off Linux nothing is asked. +fn unwritable_pages(host: &HostWindow, vouched: bool, counts: &Counters) -> Result { if vouched { - return Ok(host); + return Ok(false); } match refuse_unwritable(&host.window, counts) { - Ok(()) => Ok(host), - Err(error) if refused(&error) && host.source.is_some() => Ok(HostWindow { - by_pointer: false, - unwritable: true, - ..host - }), + Ok(()) => Ok(false), + Err(error) if refused(&error) => Ok(true), Err(error) => Err(error), } } @@ -2057,6 +2039,9 @@ enum CopiedFor { /// Metal's private option bit 24 drops the GPU's writes to the memory, and the system maps /// no read-only view of the pages (`read_only_view.rs`): the copy drops them. ReadOnly, + /// The process cannot write every page ([`unwritable_pages`]): the copy drops the GPU's + /// writes, as Metal drops them. + Unwritable, } /// Whether `error`, an import's failure that is not the device's refusal of the pages, leaves the @@ -2108,7 +2093,6 @@ fn host_window( window, by_pointer, source, - unwritable: false, }) } diff --git a/crates/metal2vulkan-runtime/src/degradation.rs b/crates/metal2vulkan-runtime/src/degradation.rs index c2f82b12e..7bbdf34bb 100644 --- a/crates/metal2vulkan-runtime/src/degradation.rs +++ b/crates/metal2vulkan-runtime/src/degradation.rs @@ -1970,17 +1970,6 @@ degradations! { (through a texture over them, or a table of the live buffers it holds) lets go of \ them, or the batches submitted before the import's memory went complete, where \ Metal's runs at the release; the pages stay mapped until then"; - /// A no-copy buffer, heap or rebind was made over host pages the process maps - /// read-only, from the caller's dma-buf source (`HostMemory::with_dma_buf`): on Linux, - /// where drivers pin a pointer's imported pages for writing, such pages are imported only - /// as dma-bufs of their pages, whose memory the GPU writes. Where the caller's gate - /// refuses their import, or that import fails, they are a copy whose GPU writes are - /// dropped instead (`CopiedHostPages`). Announced once per device, by the call that makes - /// it, or at each such call where `DeviceOptions::announce_each` asks. - ReadOnlyPagesWritten = "read-only-pages-written", Precision, - "the GPU's writes through it reach the pages, and the CPU then reads them there, \ - where after Metal's GPU writes into pages the process maps read-only the CPU reads \ - them as they were (an M3 Max under macOS 27.0.1, private and shared memory alike)"; /// A no-copy buffer, a no-copy heap or a buffer's rebind was made over a copy of the /// caller's host pages in memory of the device's own, which no import maps (`copied.rs`; /// a heap's textures shadows of its bytes): the caller's gate @@ -1989,7 +1978,10 @@ degradations! { /// memory, or another Vulkan error, such as a driver out of the mappings the host kernel /// allows), or the runtime kept pages the kernel cannot pin from a driver whose failed /// import of them breaks it (`RuntimeDriverDefect::UnpinnablePagesBreakHostMemory`) and no - /// dma-buf of them served; or, off macOS, the memory is of Metal's private option bit 24 + /// dma-buf of them served; or, on Linux, the process maps some of the pages read-only, + /// whose pointer drivers pin for writing and whose dma-bufs would take the GPU's writes + /// (`WorkCounts::copied_unwritable_pages`), the gate asked first where the pointer would + /// be imported; or, off macOS, the memory is of Metal's private option bit 24 /// (`ResourceOptions::PRIVATE_BIT`), which no import there keeps the GPU's writes from, and /// which is copied without asking the gate (`WorkCounts::copied_read_only_memory`). A copy /// of pages the process maps read-only, or of bit-24 memory, drops the GPU's writes, as diff --git a/crates/metal2vulkan-runtime/src/host_backing.rs b/crates/metal2vulkan-runtime/src/host_backing.rs index db88d813a..fcbd5c957 100644 --- a/crates/metal2vulkan-runtime/src/host_backing.rs +++ b/crates/metal2vulkan-runtime/src/host_backing.rs @@ -36,8 +36,9 @@ pub enum HostBacking { /// place: the caller's gate refused their import /// ([`HostMemory::with_import_gate`](crate::HostMemory::with_import_gate)), the import the /// device and the gate admitted failed, a rebind's new bytes lie at another offset in their - /// page than the old under a texture over the buffer, or off macOS the memory is of Metal's - /// private option bit 24, whose GPU writes the copy drops. Announced as the + /// page than the old under a texture over the buffer, on Linux the process maps some of the + /// pages read-only, or off macOS the memory is of Metal's private option bit 24; the copy of + /// read-only pages or bit-24 memory drops the GPU's writes. Announced as the /// [`copied-host-pages`](crate::Degradation::CopiedHostPages) degradation: each commit writes /// into the copy the bytes the CPU changed in the pages, and each batch's completion writes /// back the bytes the GPU changed, where Metal's GPU uses the pages themselves. diff --git a/crates/metal2vulkan-runtime/src/host_memory.rs b/crates/metal2vulkan-runtime/src/host_memory.rs index 47b159e8e..e7ba30920 100644 --- a/crates/metal2vulkan-runtime/src/host_memory.rs +++ b/crates/metal2vulkan-runtime/src/host_memory.rs @@ -21,11 +21,11 @@ //! alignment above the host page whose rounding would take pages the range does not touch, an //! offset in the pages that misses the buffer's memory alignment, no memory type both allow, or //! a driver that refuses the pointer. -//! - **Pages the process cannot write** are refused typed on Linux, before the buffer's own import, -//! unless the caller gave a dma-buf source the device can use: then they are imported from it -//! alone, never by their pointer, and the device announces `read-only-pages-written`, as the -//! GPU's writes reach them where Metal's leave the CPU's view of them as it was -//! ([`refuse_unwritable`] asks the kernel for the process's own mappings over the window, with +//! - **Pages the process cannot write** are never imported on Linux: the buffer is a copy of them +//! whose GPU writes are dropped (`copied.rs`, announced as `copied-host-pages`, +//! `WorkCounts::copied_unwritable_pages`), on every device, its dma-buf source never called, as +//! a dma-buf of them would take the GPU's writes where Metal's leave the CPU's view of them as it +//! was ([`refuse_unwritable`] asks the kernel for the process's own mappings over the window, with //! `PROCMAP_QUERY` on `/proc/self/maps`, a file kept open for them (`kept_maps`), or reads //! that file's lines where the kernel predates the query, Linux 6.11; //! `WorkCounts::host_mappings_queried`). A buffer that lies in a live @@ -34,9 +34,9 @@ //! writable. Nor does one over pages whose write permission the caller vouched for //! ([`HostMemory::assume_writable`]). Linux drivers pin user pages for writing: NVIDIA failed //! a read-only page as out of device memory, RADV refused it, and ANV imported it and lost the -//! device at the GPU's first read. lavapipe read one, so there the rule refuses a little more -//! than the driver would. On macOS, MoltenVK hands the range to Metal, which reads read-only -//! pages, so there is no check. +//! device at the GPU's first read. lavapipe read one, so there the rule copies a little more +//! than the driver would need. On macOS, MoltenVK hands the range to Metal, which reads +//! read-only pages, so there is no check. //! - **What drivers answered**. Private anonymous memory was imported on every venue measured. RADV //! refused shared memory typed: anonymous shared memory, and any mapping of a file, a shared //! memory object or a `memfd` among them (QEMU's `memory-backend-memfd,share=on` guest RAM). It @@ -244,16 +244,15 @@ impl HostMemory { /// Reading or writing them through the pointer while the GPU writes them is a data race, as /// Metal's `contents` is. /// - /// On Linux a buffer over a page this process maps without write permission is refused - /// (`Unsupported`, `HostMemory`), though Metal reads such pages: Linux drivers pin imported - /// pages for writing, and one (ANV) imported a read-only page and then lost the device at the - /// GPU's first read. So on Linux the pages also stay writable while they are imported: RADV - /// failed every command buffer submitted after a page of an import was made read-only - /// (`mprotect`). With a dma-buf source ([`HostMemory::with_dma_buf`]) pages the process maps - /// read-only are instead imported from the source alone, announced - /// (`read-only-pages-written`). Under a gate that refuses their import - /// ([`HostMemory::with_import_gate`]), or where the source's import fails, they are a copy - /// whose GPU writes are dropped, as Metal drops them (`copied-host-pages`). Some drivers also + /// On Linux a buffer over a page this process maps without write permission is a copy of the + /// pages whose GPU writes are dropped, as Metal drops them (`copied-host-pages`), with or + /// without a dma-buf source ([`HostMemory::with_dma_buf`]), which is never called for them: + /// Linux drivers pin imported pages for writing (one, ANV, imported a read-only page and then + /// lost the device at the GPU's first read), and a dma-buf of them would take the GPU's + /// writes. So on Linux the pages also stay writable while they are imported: RADV failed every + /// command buffer submitted after a page of an import was made read-only (`mprotect`). The + /// caller's gate ([`HostMemory::with_import_gate`]) is asked first where the pointer would be + /// imported. Some drivers also /// refuse shared memory (RADV refused anonymous shared memory and mappings of shared memory /// objects, `memfd` mappings among them); private memory imports everywhere measured. /// [`HostMemory::with_dma_buf`] imports such memory where the device imports dma-bufs. diff --git a/crates/metal2vulkan-runtime/src/tests/copied_device.rs b/crates/metal2vulkan-runtime/src/tests/copied_device.rs index 7607ff2a2..f42ad02d7 100644 --- a/crates/metal2vulkan-runtime/src/tests/copied_device.rs +++ b/crates/metal2vulkan-runtime/src/tests/copied_device.rs @@ -17,6 +17,10 @@ //! copied: the buffer is refused, as their import is. //! - **A dma-buf's mapping** (Linux, where `/dev/udmabuf` opens), whose pages the kernel's read //! cannot pin, is copied where the gate refuses its import, with the same coherence. +//! - **Read-only pages** (a read-only view of shared memory) are a copy whose GPU writes are +//! dropped, a buffer's and a rebind's: where the gate refuses their import, counted as a +//! refusal; on Linux where it admits it, counted as a copy of unwritable pages +//! (`WorkCounts::copied_unwritable_pages`). Their dma-buf source is never called. //! - **Rebinds.** A rebind's new pages are a copy where the gate refuses their import or the //! admitted import fails, with the same coherence; the old pages stay as they were, and a later //! rebind imports again. @@ -1739,14 +1743,13 @@ fn copied_heap_rebind_over_read_only_pages_drops_the_gpu_s_writes() { drop((buffer, imported)); } -/// A buffer's rebind to read-only pages whose import the gate admits goes to the caller's source -/// (Linux drivers pin a pointer's pages for writing); where the device's import of those dma-bufs -/// then fails (the RTX 5080's, out of device memory), the buffer is a copy of them all the same, -/// counted as degraded, whose GPU writes are dropped. Where the device imports them, or none, the -/// test says what it took and passes. +/// A buffer's rebind to read-only pages whose import the gate admits gives it a copy of them on +/// every device, their source never called (a dma-buf of them would take the GPU's writes, and +/// Linux drivers pin a pointer's pages for writing), counted as a copy of unwritable pages, whose +/// GPU writes are dropped; the old pages stay as they were. #[cfg(target_os = "linux")] #[test] -fn copied_rebind_over_read_only_pages_whose_admitted_import_fails_drops_the_gpu_s_writes() { +fn copied_rebind_over_read_only_pages_the_gate_admits_drops_the_gpu_s_writes() { let (first, second) = (Host::new(33), ReadOnlyHost::new(34)); let Some((device, heard)) = device() else { return; @@ -1761,26 +1764,64 @@ fn copied_rebind_over_read_only_pages_whose_admitted_import_fails_drops_the_gpu_ buffer.rebind_bytes_no_copy(second.memory(gate(&asked, true), &calls), None, None) }; let after = device.work_counts(); - let called = calls.load(std::sync::atomic::Ordering::Acquire); + rebound.expect("a rebind to read-only pages the gate admits is a copy"); + assert!(buffer.copied(), "a copy"); + assert_eq!(moved(&before, &after), [0, 1, 0, 0, 0]); + assert_eq!( + after.copied_unwritable_pages - before.copied_unwritable_pages, + 1 + ); assert_eq!(asked.lock().unwrap().len(), 1, "the gate was asked once"); - // An import that failed is never the answer: the copy is. - if let Err(error @ (crate::Error::OutOfMemory(_) | crate::Error::Vulkan(_))) = &rebound { - panic!("the admitted import failed and no copy was made: {error:?}"); - } - if !buffer.copied() { - eprintln!( - "COPIED-READ-ONLY-ADMITTED: not a copy here ({rebound:?}, the source called {called} \ - times, {} dma-buf imports made): nothing to test", - after.dma_buf_imports - before.dma_buf_imports - ); - return; - } - rebound.unwrap(); - assert_eq!(moved(&before, &after), [0, 1, 0, 1, 0]); - assert_eq!(called, 1, "the source was asked once"); + assert_eq!( + calls.load(std::sync::atomic::Ordering::Acquire), + 0, + "the source unasked" + ); + assert_eq!(after.dma_buf_imports, before.dma_buf_imports); assert_eq!(announced(&heard), 1, "announced once"); drops_the_gpu_s_writes(&device, (&buffer, 0), &second); - eprintln!("COPIED-READ-ONLY-ADMITTED: the source's import failed, and the copy was made"); + assert_eq!(first.bytes(), first.pattern(), "the old pages stay"); +} + +/// A no-copy buffer over read-only pages whose import the gate admits is a copy of them on every +/// device, their source never called, counted as a copy of unwritable pages, whose GPU writes are +/// dropped. +#[cfg(target_os = "linux")] +#[test] +fn copied_buffer_over_read_only_pages_the_gate_admits_drops_the_gpu_s_writes() { + let host = ReadOnlyHost::new(35); + let Some((device, heard)) = device() else { + return; + }; + let asked: Arc>> = Arc::default(); + let calls: Arc = Arc::default(); + let before = device.work_counts(); + // SAFETY: the view outlives the buffer and the device (dropped after both). + let buffer = unsafe { + device.new_buffer_with_bytes_no_copy( + host.memory(gate(&asked, true), &calls), + LENGTH as u64, + ResourceOptions::default(), + None, + ) + } + .expect("a buffer over read-only pages the gate admits is a copy"); + let after = device.work_counts(); + assert!(buffer.copied(), "a copy"); + assert_eq!(moved(&before, &after), [0, 1, 0, 0, 0]); + assert_eq!( + after.copied_unwritable_pages - before.copied_unwritable_pages, + 1 + ); + assert_eq!( + calls.load(std::sync::atomic::Ordering::Acquire), + 0, + "the source unasked" + ); + assert_eq!(after.dma_buf_imports, before.dma_buf_imports); + assert_eq!(announced(&heard), 1, "announced once"); + drops_the_gpu_s_writes(&device, (&buffer, 0), &host); + drop(buffer); } /// A batch that only reads a copied buffer leaves it uncompared at its merge back diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs index 12a030ca6..854c8d31d 100644 --- a/crates/metal2vulkan-runtime/src/work_counts.rs +++ b/crates/metal2vulkan-runtime/src/work_counts.rs @@ -328,6 +328,7 @@ pub(crate) struct Counters { copied_host_buffers_degraded: AtomicU64, copied_rebind_pages_moved: AtomicU64, copied_read_only_memory: AtomicU64, + copied_unwritable_pages: AtomicU64, copied_rebind_pages_made: AtomicU64, copied_rebind_images_made: AtomicU64, copied_heaps_made: AtomicU64, @@ -2707,6 +2708,7 @@ impl Counters { CopiedBecause::Refused => &self.copied_host_buffers_refused, CopiedBecause::Moved => &self.copied_rebind_pages_moved, CopiedBecause::ReadOnly => &self.copied_read_only_memory, + CopiedBecause::Unwritable => &self.copied_unwritable_pages, } .fetch_add(1, Ordering::Relaxed); } @@ -5676,7 +5678,7 @@ pub struct WorkCounts { pub host_imports_refused_by_host: u64, /// The copied host buffers made (`copied-host-pages`): no-copy buffers whose bytes the GPU /// uses in a copy of the caller's pages in memory of the device's own, which no import maps. - /// Each is one of the next three. + /// Each is one of the next five. pub copied_host_buffers_made: u64, /// Of those, the ones made because the caller's gate refused the import. pub copied_host_buffers_refused: u64, @@ -5692,6 +5694,13 @@ pub struct WorkCounts { /// Metal's private option bit 24 drops, made over a copy that drops them where the system /// maps no read-only view of the pages (`copied.rs`, `read_only_view.rs`). pub copied_read_only_memory: u64, + /// Of `copied_host_buffers_made`: no-copy buffers, heaps and rebinds over pages the process + /// maps read-only, on Linux, made over a copy that drops the GPU's writes, as Metal drops + /// them: Linux drivers pin a pointer's imported pages for writing, and a dma-buf of the pages + /// would take the GPU's writes (`buffer_descriptor.rs`, `copied.rs`). Their import is never + /// tried and their dma-buf source never called; the caller's gate is asked first where the + /// pointer would be imported, and a copy it refused is counted as refused. + pub copied_unwritable_pages: u64, /// Of the copies made, those a rebind gave a buffer as its new pages /// (`Buffer::rebind_bytes_no_copy`), in `copied_host_buffers_made` and its kin too. pub copied_rebind_pages_made: u64, @@ -7071,6 +7080,7 @@ impl Device { .load(Ordering::Relaxed), copied_rebind_pages_moved: counters.copied_rebind_pages_moved.load(Ordering::Relaxed), copied_read_only_memory: counters.copied_read_only_memory.load(Ordering::Relaxed), + copied_unwritable_pages: counters.copied_unwritable_pages.load(Ordering::Relaxed), copied_rebind_pages_made: counters.copied_rebind_pages_made.load(Ordering::Relaxed), copied_rebind_images_made: counters.copied_rebind_images_made.load(Ordering::Relaxed), copied_heaps_made: counters.copied_heaps_made.load(Ordering::Relaxed), @@ -7403,4 +7413,7 @@ pub(crate) enum CopiedBecause { /// Metal's private option bit 24 drops the GPU's writes to the memory, and the system maps no /// read-only view of the pages (`read_only_view.rs`): the copy drops them. ReadOnly, + /// The process maps some of the pages read-only, whose GPU writes Metal drops and no import + /// on Linux keeps out of them: the copy drops them. + Unwritable, } diff --git a/crates/metal2vulkan-validation/src/runtime_lane/degrade_notices.rs b/crates/metal2vulkan-validation/src/runtime_lane/degrade_notices.rs index 5e9033857..fb9409d4a 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/degrade_notices.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/degrade_notices.rs @@ -90,8 +90,7 @@ //! each command buffer's own copy on a device withholding //! `descriptorBindingUpdateUnusedWhilePending`, [`OWN_COPIES`]; a no-copy buffer's deallocator //! that waits for its pages' import, which a texture's image a submitted command buffer may read -//! holds, [`DELAYED_DEALLOCATORS`]), a no-copy buffer over pages the process maps read-only, -//! made from its dma-buf source in `dma_buf_imports` on Linux ([`READ_ONLY_PAGES`]), +//! holds, [`DELAYED_DEALLOCATORS`]), //! pass colors a draw keeps where Metal's compiler may fold the output //! and zero them in `color_reformat` ([`KEPT_COLORS`]), multisampled depth copied in by an //! inexact fragment-depth draw in `sample_copies` ([`DRAWN_DEPTH`]), textures bound beyond their @@ -181,7 +180,7 @@ use super::grouped_submissions::GROUPED; use super::heap_layouts::HEAP_LAYOUTS; use super::heap_metal_sizes::{PAST_METAL_SIZES, UNMODELLED_SIZES}; use super::heap_textures::{ALIASES, APART, COMPRESSED, OPTIMIZED}; -use super::host_memory::{not_importable, Mapping, GUEST_PAGE, READ_ONLY_PAGES}; +use super::host_memory::{not_importable, Mapping, GUEST_PAGE}; use super::index_fetches::INDEX_FETCHES; use super::indirect_command_buffers::UNMEASURED_IDENTIFIERS; use super::indirect_dispatches::INDIRECT_DISPATCHES; @@ -576,7 +575,6 @@ fn every_kind_has_a_site_a_lane_test_takes() { &CHOSEN_COPIES, &KEPT_BYTES, &DELAYED_DEALLOCATORS, - &READ_ONLY_PAGES, &MISPLACED, &STILL_MAKING, &COPIED_PAGES, diff --git a/crates/metal2vulkan-validation/src/runtime_lane/dma_buf_imports.rs b/crates/metal2vulkan-validation/src/runtime_lane/dma_buf_imports.rs index 773cbe146..205a78292 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/dma_buf_imports.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/dma_buf_imports.rs @@ -25,7 +25,6 @@ use super::degrade_notices; use super::first_use::{read_mark, release, scheduled, Busy, ENDED}; use super::first_use_writes::CHECK_WAIT; -use super::host_memory::READ_ONLY_PAGES; use super::host_memory::{ counting, gpu_fill, gpu_read, host_page, not_importable, on_device, run, Mapping, }; @@ -48,8 +47,8 @@ use metal2vulkan::vk::DriverId; use metal2vulkan_runtime::{ BlitOption, Buffer, BufferContents, BufferDescriptor, CommandBuffer, CommandQueue, DebugMessage, Degradation, Device, DmaBufChunk, DmaBufRequest, DmaBufSource, Error, EventRef, - Heap, HeapDescriptor, HeapType, HostMemory, KeepAlive, Origin, Problem, ResourceOptions, - RuntimeGap, Size, TextureDescriptor, + Heap, HeapDescriptor, HeapType, HostBacking, HostMemory, KeepAlive, Origin, Problem, + ResourceOptions, RuntimeGap, Size, TextureDescriptor, }; use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, OwnedFd}; use std::ptr::NonNull; @@ -856,16 +855,15 @@ fn a_buffer_that_lies_in_a_live_import_asks_about_no_mapping() { }); } -/// Memfd pages a view maps read-only, with a source: the process cannot write them, so no pointer -/// of them is imported (Linux drivers pin imported pages for writing), and the buffer is made from -/// the source alone on a device that imports dma-bufs, whatever it does with a pointer of such -/// memory (`ReadOnlyPagesWritten`, announced once for two such buffers over different pages). The -/// GPU reads the CPU's bytes through it, and its writes reach the file's pages, where the CPU -/// reads them through the file's own mapping (where Metal's leave the pages as they were). The -/// same pages with no source are refused typed, as before, and so is the source's memory on a -/// device that imports no dma-buf. +/// Memfd pages a view maps read-only: the process cannot write them, so on every device the +/// buffer over them is a copy whose GPU writes are dropped, as Metal drops them, made without a +/// source and with one, which is never called (`WorkCounts::copied_unwritable_pages`, announced +/// once as `copied-host-pages` for the two buffers over different pages). Neither pointer is +/// imported (Linux drivers pin imported pages for writing), nor the pages as dma-bufs, which would +/// take the GPU's writes. The GPU reads the CPU's bytes through each, and after its fill the +/// file's pages, which the CPU reads through the file's own mapping, are as they were. #[test] -fn read_only_pages_are_made_from_the_source() { +fn read_only_pages_are_a_copy_whose_gpu_writes_are_dropped() { const TEST: &str = "dma-buf read-only pages"; if !udmabuf_openable(TEST) { return; @@ -876,55 +874,52 @@ fn read_only_pages_are_made_from_the_source() { let ram = Memfd::new(6 * page); let bytes = counting(6 * page, 9); ram.whole.write(0, &bytes); - // Whether the device's route for these pages is a dma-buf import. A device that imports - // the pointer instead (NVIDIA, lavapipe) has no working import for pages the process maps - // read-only, which it refuses the pointer of: NVIDIA's 615.71.09 answers - // `ERROR_OUT_OF_DEVICE_MEMORY` with the whole card free. Its answer is printed, not held - // to a class, since it is the driver's. - let route = imports_dma_buf(device) == Some(true); - let read_only = |notices: &[DebugMessage]| { + let copied = |notices: &[DebugMessage]| { let kind = |m: &&DebugMessage| { - (m.degradation.as_ref()).is_some_and(|n| READ_ONLY_PAGES.contains(&n.degradation)) + (m.degradation.as_ref()) + .is_some_and(|n| n.degradation == Degradation::CopiedHostPages) }; notices.iter().filter(kind).count() }; let mut made = Vec::new(); - for (pieces, at) in [([(page, 2 * page)], page), ([(4 * page, page)], 4 * page)] { + let cases = [ + ([(page, 2 * page)], page, false), + ([(4 * page, page)], 4 * page, true), + ]; + for (pieces, at, sourced) in cases { let view = ram.view(&pieces); view.protect_read_only(); let length = pieces[0].1; - // SAFETY: the view outlives the buffer, and nothing is made of it. - let plain = unsafe { - device.new_buffer_with_bytes_no_copy( - HostMemory::new(view.pointer(0)), - length as u64, - ResourceOptions::default(), - None, - ) - }; - let error = plain.unwrap_err(); - assert!(not_importable(&error), "{TEST}: no source: {error:?}"); let calls = Calls::default(); - let before = dma_buf_counts(device); - let buffer = no_copy(device, &view, 0, length, ram.source(&view, &calls)); - let buffer = match (route, buffer) { - (true, Ok(buffer)) => buffer, - (true, made) => panic!("{TEST}: the dma-buf route: {:?}", made.err()), - (false, made) => { - eprintln!( - "{TEST}: on {}: the pages' route is not a dma-buf import: {:?}", - device.name(), - made.err() - ); - return; - } + let before = device.work_counts(); + let buffer = match sourced { + true => no_copy(device, &view, 0, length, ram.source(&view, &calls)), + // SAFETY: the view outlives the buffer, and nothing is made of it. + false => unsafe { + device.new_buffer_with_bytes_no_copy( + HostMemory::new(view.pointer(0)), + length as u64, + ResourceOptions::default(), + None, + ) + }, }; - let calls = calls.lock().unwrap().clone(); - assert_eq!(calls.len(), 1, "{TEST}: the source's calls: {calls:?}"); - assert!( - dma_buf_counts(device).0 > before.0, - "{TEST}: no chunk imported" + let buffer = buffer.unwrap_or_else(|error| { + panic!("{TEST}: read-only pages, a source {sourced}: {error:?}") + }); + let after = device.work_counts(); + assert_eq!( + ( + after.copied_unwritable_pages - before.copied_unwritable_pages, + after.dma_buf_imports - before.dma_buf_imports, + after.host_pointer_imports_made - before.host_pointer_imports_made, + ), + (1, 0, 0), + "{TEST}: a source {sourced}: a copy, and no import" ); + assert_eq!(buffer.host_backing(), Some(HostBacking::Copied)); + let calls = calls.lock().unwrap().clone(); + assert!(calls.is_empty(), "{TEST}: the source's calls: {calls:?}"); assert!( gpu_read(&queue, &buffer, 0..length as u64) == bytes[at..at + length], "{TEST}: the GPU's read" @@ -932,14 +927,14 @@ fn read_only_pages_are_made_from_the_source() { gpu_fill(&queue, &buffer, 0..64, 0x5a); assert_eq!( ram.whole.read(at..at + 64), - [0x5a; 64], - "{TEST}: the GPU's write in the file's pages" + bytes[at..at + 64], + "{TEST}: a source {sourced}: the GPU's write reached the file's pages" ); made.push((buffer, view)); } - assert_eq!(read_only(&messages.take_notices()), 1, "{TEST}: announced"); + assert_eq!(copied(&messages.take_notices()), 1, "{TEST}: announced"); eprintln!( - "{TEST}: on {}: made from the source, the GPU's writes in the pages", + "{TEST}: on {}: a copy with a source and without, the GPU's writes dropped", device.name() ); drop(made); @@ -962,8 +957,8 @@ impl Drop for LiveAtDrop { /// A no-copy buffer's keep-alive goes once the imports of its pages are freed, as /// `HostMemory`'s contract has it (the pages must stay mapped until then): a buffer over two -/// pages the process maps read-only, made from the source's dma-bufs on a device whose route for -/// such pages is a dma-buf import ([`imports_dma_buf`]), with a keep-alive and no deallocator, +/// memfd pages, made from the source's dma-bufs on a device whose route for such pages is a +/// dma-buf import ([`imports_dma_buf`]), with a keep-alive and no deallocator, /// released; as the keep-alive drops, every import the buffer made is freed (the live dma-buf /// imports are back to their count before it). #[test] @@ -973,8 +968,8 @@ fn a_keep_alive_goes_after_the_imports_of_its_pages() { return; } degrade_notices::on_device(|device, _| { - // As `read_only_pages_are_made_from_the_source`: only a device whose route for these - // pages is a dma-buf import has imports to order the keep-alive against. + // Only a device whose route for these pages is a dma-buf import has imports to order the + // keep-alive against. if imports_dma_buf(device) != Some(true) { eprintln!( "{TEST}: on {}: the pages' route is not a dma-buf import", @@ -985,7 +980,6 @@ fn a_keep_alive_goes_after_the_imports_of_its_pages() { let page = host_page(); let ram = Memfd::new(4 * page); let view = ram.view(&[(page, 2 * page)]); - view.protect_read_only(); let live = || device.work_counts().dma_buf_memories_live; let before = live(); let seen = Arc::new(Mutex::new(None)); diff --git a/crates/metal2vulkan-validation/src/runtime_lane/host_memory.rs b/crates/metal2vulkan-validation/src/runtime_lane/host_memory.rs index f9b3d46d2..c70aab62c 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/host_memory.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/host_memory.rs @@ -23,8 +23,8 @@ use metal2vulkan::metal::{PixelFormat, TextureUsage}; use metal2vulkan::vk::DriverId; use metal2vulkan_runtime::{ BlitOption, Buffer, BufferContents, BufferDescriptor, CommandBuffer, CommandBufferStatus, - CommandQueue, CopyProblem, CpuCacheMode, Deallocator, DebugMessage, Degradation, Device, Error, - EventRef, HazardTrackingMode, HostMemory, Invalid, Nil, Origin, Problem, PurgeableState, + CommandQueue, CopyProblem, CpuCacheMode, Deallocator, DebugMessage, Device, Error, EventRef, + HazardTrackingMode, HostBacking, HostMemory, Invalid, Nil, Origin, Problem, PurgeableState, Region, ResourceOptions, Size, StorageMode, TextureDescriptor, }; #[cfg(any(m2v_cpu_icd, m2v_gpu_hazards))] @@ -410,10 +410,6 @@ pub(super) fn gpu_fill( run(&command_buffer); } -/// The kind a no-copy buffer over pages the process maps read-only, made from its dma-buf source, -/// announces once (`dma_buf_imports`'s `read_only_pages_are_made_from_the_source`, on Linux). -pub(super) const READ_ONLY_PAGES: [Degradation; 1] = [Degradation::ReadOnlyPagesWritten]; - /// Bytes `i % 251` from `start`. pub(super) fn counting(len: usize, start: usize) -> Vec { (start..start + len).map(|i| (i % 251) as u8).collect() @@ -1514,21 +1510,22 @@ fn host_memory_kinds_are_imported_or_refused_typed() { }); } -/// A read-only page (Metal makes the buffer, and its GPU reads it). On Linux the runtime refuses -/// it typed before the import, as it refuses every page the process cannot write: Linux drivers -/// pin imported pages for writing (measured at 83a57ba2: RADV refused the page, -/// NVIDIA failed it as out of device memory, ANV made it and lost the device at the GPU's read; -/// lavapipe read it, so there the rule refuses more than the driver would). Elsewhere (MoltenVK, -/// which hands the range to Metal) it is made, and the GPU reads what the CPU wrote before the -/// page became read-only. The GPU never writes the page. +/// A read-only page (Metal makes the buffer, its GPU reads it, and its GPU's writes to it are +/// dropped). On Linux the runtime makes the buffer over a copy of it whose GPU writes are dropped +/// (`WorkCounts::copied_unwritable_pages`), as it does every page the process cannot write: Linux +/// drivers pin imported pages for writing (measured at 83a57ba2: RADV refused the page, NVIDIA +/// failed it as out of device memory, ANV made it and lost the device at the GPU's read; lavapipe +/// read it). Elsewhere (MoltenVK, which hands the range to Metal) it is imported. Either way the +/// GPU reads what the CPU wrote before the page became read-only; on Linux the GPU's fill of the +/// copy leaves the page as it was. #[test] -fn a_read_only_page_is_refused_on_linux_and_read_elsewhere() { +fn a_read_only_page_is_a_copy_on_linux_and_read_elsewhere() { on_device(|device| { let memory = Mapping::anonymous(GUEST_PAGE); let bytes = counting(GUEST_PAGE, 3); memory.write(0, &bytes); memory.protect_read_only(); - // SAFETY: the mapping outlives the buffer, and nothing writes the buffer. + // SAFETY: the mapping outlives the buffer, and the GPU writes only its copy (Linux). let made = unsafe { no_copy( device, @@ -1538,14 +1535,24 @@ fn a_read_only_page_is_refused_on_linux_and_read_elsewhere() { None, ) }; + let queue = device.new_command_queue().unwrap(); + let buffer = made.unwrap(); + assert_eq!(gpu_read(&queue, &buffer, 0..GUEST_PAGE as u64), bytes); if cfg!(target_os = "linux") { - let error = made.unwrap_err(); - assert!(not_importable(&error), "{error:?}"); - eprintln!("runtime read-only page: refused"); + assert_eq!(buffer.host_backing(), Some(HostBacking::Copied)); + assert_eq!( + device.work_counts().copied_unwritable_pages, + 1, + "one copy, counted" + ); + gpu_fill(&queue, &buffer, 0..64, 0x5a); + assert_eq!( + memory.read(0..GUEST_PAGE), + bytes, + "the GPU's write was dropped" + ); + eprintln!("runtime read-only page: a copy, read, the GPU's write dropped"); } else { - let queue = device.new_command_queue().unwrap(); - let buffer = made.unwrap(); - assert_eq!(gpu_read(&queue, &buffer, 0..GUEST_PAGE as u64), bytes); eprintln!("runtime read-only page: made, read"); } }); @@ -1731,9 +1738,8 @@ fn a_lost_device_s_command_buffers_let_go_of_their_pools_after_the_driver_s_comp /// every later one fail `DeviceLost`, and the device refuses new buffers `DeviceLost`; /// no runtime thread reaches the lost device outside a section of the exit /// guard on the way (a debug build once ended the process there). MoltenVK's reports of the failure -/// are the only messages. On MoltenVK alone: a Linux driver is refused the pages -/// (`a_read_only_page_is_refused_on_linux_and_read_elsewhere`), and lavapipe's CPU would fault -/// the process itself. A GPU hazard (`m2v_gpu_hazards`): it faults the GPU on purpose twice, and +/// are the only messages. On MoltenVK alone: on Linux the buffer is a copy whose GPU writes are +/// dropped, which no write faults (`a_read_only_page_is_a_copy_on_linux_and_read_elsewhere`). A GPU hazard (`m2v_gpu_hazards`): it faults the GPU on purpose twice, and /// Metal's recovery fails other processes' command buffers in flight. #[cfg(any(m2v_cpu_icd, m2v_gpu_hazards))] #[test] diff --git a/crates/metal2vulkan-validation/src/runtime_lane/pitched_surfaces.rs b/crates/metal2vulkan-validation/src/runtime_lane/pitched_surfaces.rs index 075f31b8e..c60b31094 100644 --- a/crates/metal2vulkan-validation/src/runtime_lane/pitched_surfaces.rs +++ b/crates/metal2vulkan-validation/src/runtime_lane/pitched_surfaces.rs @@ -822,8 +822,9 @@ const PACKED: [Layout; 2] = [ ]; /// A texture that only reads, of `layout`, over a no-copy buffer whose pages the process maps -/// read-only by its first use: made read-only after the buffer was made (Linux refuses read-only -/// pages at the import, `a_read_only_page_is_refused_on_linux_and_read_elsewhere`, but not later), +/// read-only by its first use: made read-only after the buffer was made (Linux makes a copy of +/// read-only pages at the import, `a_read_only_page_is_a_copy_on_linux_and_read_elsewhere`, but +/// not of pages made read-only later), /// or, where `from_the_start`, before it (a guest's pages the host maps read-only, which MoltenVK /// imports in place). It is a shadow, whose first use copies the pages' texels in and stores /// nothing into them, or in place ([`in_place_form`]), whose first move the device's check showed diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index ffd39cada..be53addeb 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -2586,6 +2586,7 @@ field metal2vulkan-runtime::WorkCounts.copied_merge_back_bytes pub copied_merge_ field metal2vulkan-runtime::WorkCounts.copied_merges_compared pub copied_merges_compared: u64 field metal2vulkan-runtime::WorkCounts.copied_merges_skipped pub copied_merges_skipped: u64 field metal2vulkan-runtime::WorkCounts.copied_read_only_memory pub copied_read_only_memory: u64 +field metal2vulkan-runtime::WorkCounts.copied_unwritable_pages pub copied_unwritable_pages: u64 field metal2vulkan-runtime::WorkCounts.copied_rebind_images_made pub copied_rebind_images_made: u64 field metal2vulkan-runtime::WorkCounts.copied_rebind_pages_made pub copied_rebind_pages_made: u64 field metal2vulkan-runtime::WorkCounts.copied_rebind_pages_moved pub copied_rebind_pages_moved: u64 @@ -5288,7 +5289,6 @@ macro-member metal2vulkan-runtime::Degradation::QuadsSplitByClipping QuadsSplitB macro-member metal2vulkan-runtime::Degradation::QueueSharing QueueSharing = "queue-sharing", Performance, macro-member metal2vulkan-runtime::Degradation::RasterizationRateMap RasterizationRateMap = "rasterization-rate-map"; macro-member metal2vulkan-runtime::Degradation::RateMapResetUnencoded RateMapResetUnencoded = "rate-map-reset-unencoded", Precision, -macro-member metal2vulkan-runtime::Degradation::ReadOnlyPagesWritten ReadOnlyPagesWritten = "read-only-pages-written", Precision, macro-member metal2vulkan-runtime::Degradation::ReductionOrder ReductionOrder = "reduction-order"; macro-member metal2vulkan-runtime::Degradation::ReleasedCommandState ReleasedCommandState = "released-command-state", Precision, macro-member metal2vulkan-runtime::Degradation::RenderPassSplit RenderPassSplit = "render-pass-split", Performance, diff --git a/docs/API.md b/docs/API.md index 7de485cc1..2756f3102 100644 --- a/docs/API.md +++ b/docs/API.md @@ -2856,14 +2856,6 @@ bound. buffer that may still reach the pages (through a texture over them, or a table of the live buffers it holds) lets go of them, or the batches submitted before the import's memory went complete, where Metal's runs at the release; the pages stay mapped until then. -- `metal2vulkan-degrade-read-only-pages-written` (precision). `ReadOnlyPagesWritten`: A no-copy - buffer, heap or rebind was made over host pages the process maps read-only, from the caller's - dma-buf source (`HostMemory::with_dma_buf`): on Linux, where drivers pin a pointer's imported - pages for writing, such pages are imported only as dma-bufs of their pages, whose memory the GPU - writes. Where the caller's gate refuses their import, or that import fails, they are a copy whose - GPU writes are dropped instead (`copied-host-pages`). Cost: the GPU's writes through it reach the pages, and the CPU then reads them there, - where after Metal's GPU writes into pages the process maps read-only the CPU reads them as they - were (an M3 Max under macOS 27.0.1, private and shared memory alike). - `metal2vulkan-degrade-copied-host-pages` (precision). `CopiedHostPages`: A no-copy buffer, a no-copy heap or a buffer's rebind was made over a copy of the caller's host pages in memory of the device's own, which no import maps (a heap's textures shadows of its bytes): @@ -2872,10 +2864,12 @@ bound. another Vulkan error, such as a driver out of the mappings the host kernel allows), or the runtime kept pages the kernel cannot pin (a dma-buf's mapping) from NVIDIA's driver, whose failed import of them breaks it (`RuntimeDriverDefect::UnpinnablePagesBreakHostMemory`), and no dma-buf - of them served. The gate is - asked before the pages' write permission: pages the process maps read-only (a VMM's alias of a - read-only replace) are copied as any, and the copy drops the GPU's writes, as Metal drops them: - each batch's completion takes them back out of the copy and writes nothing to the pages + of them served; or, on Linux, the process maps some of the pages read-only (a VMM's alias of a + read-only replace), whose pointer drivers pin for writing and whose dma-bufs would take the + GPU's writes (`copied_unwritable_pages`), with or without a dma-buf source, which is never + called for them. The gate is asked before the pages' write permission, where the pointer would + be imported. A copy of pages the process maps read-only drops the GPU's writes, as Metal drops + them: each batch's completion takes them back out of the copy and writes nothing to the pages (`copied_dropped_bytes`; the detail says so). The copy reads the pages through the kernel, and where the kernel cannot (on Linux it pins each page, which a dma-buf's mapping does not allow), with plain loads where the process maps every page readable. Pages not mapped, or mapped diff --git a/docs/HOWTO.md b/docs/HOWTO.md index 417e10745..cb58dce19 100644 --- a/docs/HOWTO.md +++ b/docs/HOWTO.md @@ -422,7 +422,10 @@ with read permission: on Linux the kernel's read pins each page, which a mapping without read permission, are never copied: the buffer is refused, as their import is. Pages the process maps read-only (a VMM's alias of `replaceBackingWithRanges:readOnly:`, its -maximum protection lowered to read) are copied the same way, and the copy drops the GPU's writes, +maximum protection lowered to read) are copied the same way on Linux, whatever the gate answers +and whether or not a dma-buf source was given, which is never called for them +(`copied_unwritable_pages`): Linux drivers pin an imported pointer's pages for writing, and a +dma-buf of the pages would take the GPU's writes. The copy drops the GPU's writes, as Metal drops them: each batch's completion takes the bytes the GPU changed back out of the copy and writes nothing to the pages (`copied_dropped_bytes`), so later commands of the same command buffer read the writes back and a later command buffer reads the pages' bytes. The runtime asks