In-place check: the fresh-pages class in fresh pages wherever the device takes them

The check made its Fresh-class buffer as the product makes a long buffer, which lies in fresh
pages only where the device's submission checks found a held import costs submissions nothing.
On RADV (where it does) the class was never checked, so its verdicts stayed Unchecked for good,
while a rebind's copy under a texture in place takes fresh pages wherever that class passed
(copied_pages, through fresh_page_buffer, whatever the cost): such rebinds took device memory
instead and were refused where the driver's linear image lies at another pitch.

The class's buffer is now made by Device::fresh_pages_check_buffer: Shared, outside the Metal
space, in fresh pages wherever the device takes them (FreshPages::Wherever), and never parked
nor taken from the recycler, so its import lives for its round alone. placed_new_buffer takes
whose buffer it makes (NewBufferOf: a heap's, or the runtime's with its FreshPages), the product's
buffers keeping WherePaid. The class no longer waits for the submission checks' answer; ANSWER_WAIT
now serves the guest RAM's dma-buf path alone. CHECK_VERSION goes to 4, so records that kept the
class Unchecked are not read again.

Test: the_in_place_check_checks_fresh_pages_where_held_imports_cost_submissions recalls answers
where the import tax holds and asserts every Fresh verdict is checked wherever a device that
recalls no tax lays a long buffer in fresh pages (Unchecked where none does). The placement
test's record helpers take the answers they write.

Verified: cargo fmt only here; builds and runs are potluck jobs.
(cherry picked from commit c288318e03c45152c8245ebf452e07bbf954af08)
This commit is contained in:
Anees Iqbal committed 2026-11-07 16:25:24 -08:00
1 parent 9529b4f605
commit e71e19aace
6 files changed
+161 -50

No files matched your search

+68 -7
View File
@@ -2204,8 +2204,12 @@ impl Device {
of_heap: bool,
placement: impl Into<Place>,
) -> Result<Buffer, Error> {
let of = match of_heap {
true => NewBufferOf::Heap,
false => NewBufferOf::Runtime(FreshPages::WherePaid),
};
let (buffer, bytes) =
self.placed_new_buffer(call, length, modes, class, of_heap, placement.into())?;
self.placed_new_buffer(call, length, modes, class, of, placement.into())?;
// Fresh pages read zero as mapped (`fresh_pages.rs`): nothing is written.
if bytes == NewBytes::Zero {
return Ok(buffer);
@@ -2215,6 +2219,33 @@ impl Device {
Ok(buffer)
}
/// A `Shared` buffer of `length` bytes (above half a pooled block, `FRESH_PAGES_PAST`), all
/// zero, outside the Metal address space, in fresh pages wherever the device takes them, the
/// cost of a held import to its submissions aside ([`FreshPages::Wherever`]): the in-place
/// check's fresh-pages class (`in_place_check.rs`). Where the device takes no such pages, a
/// buffer as ever, not in them (`Buffer::in_fresh_pages`). Refused as [`Device::new_buffer`]
/// refuses its length.
pub(super) fn fresh_pages_check_buffer(&self, length: u64) -> Result<Buffer, Error> {
const CALL: &str = "new_buffer";
let modes = Modes::of(ResourceOptions::shared())?;
let class = buffer_class(CALL, modes)?;
if length == 0 || length > self.properties().max_buffer_length {
return Err(nil(CALL, Problem::BufferLength { length }));
}
let (buffer, bytes) = self.placed_new_buffer(
CALL,
length,
modes,
class,
NewBufferOf::Runtime(FreshPages::Wherever),
AddressPlacement::Outside.into(),
)?;
if bytes == NewBytes::AsPlaced {
buffer.clear_first()?;
}
Ok(buffer)
}
/// A `Private` buffer of `length` bytes that the runtime makes for its own use and whose
/// bytes hold no value before the GPU writes them: made without a submission, so that one of
/// the runtime's sections, a host step's replay of an emulated mesh draw among them, makes
@@ -2262,24 +2293,31 @@ impl Device {
length,
modes,
class,
false,
NewBufferOf::Runtime(FreshPages::WherePaid),
AddressPlacement::Outside.into(),
)
}
/// [`zeroed_buffer`](Device::zeroed_buffer)'s buffer before its bytes are set to zero, and
/// what they hold: its memory's bytes as placed, or zero where it lies in fresh pages it
/// imported (`fresh_pages.rs`). A buffer not of a heap is a parked one of the same making
/// where the recycler holds one, and is noted as one it may park as it goes (`recycle.rs`).
/// imported (`fresh_pages.rs`), which `of` says where it takes. A buffer of the runtime's own
/// made where fresh pages are paid for is a parked one of the same making where the recycler
/// holds one, and is noted as one it may park as it goes (`recycle.rs`); one made wherever the
/// device takes them is neither: it is the in-place check's, whose import is held only for its
/// round.
fn placed_new_buffer(
&self,
call: &'static str,
length: u64,
modes: Modes,
class: MemoryClass,
of_heap: bool,
of: NewBufferOf,
placement: Place,
) -> Result<(Buffer, NewBytes), Error> {
let (of_heap, fresh) = match of {
NewBufferOf::Heap => (true, FreshPages::WherePaid),
NewBufferOf::Runtime(fresh) => (false, fresh),
};
if self.is_lost() {
return Err(Error::DeviceLost);
}
@@ -2311,8 +2349,8 @@ impl Device {
let wants_fresh = !of_heap
&& fresh_pages::takes_fresh_pages(size, class)
&& shared.host_import.is_ok()
&& self.fresh_pages_pay();
let making = (!of_heap).then_some(BufferMaking {
&& (fresh == FreshPages::Wherever || self.fresh_pages_pay());
let making = (!of_heap && fresh == FreshPages::WherePaid).then_some(BufferMaking {
size,
class,
fresh_pages: wants_fresh,
@@ -2853,6 +2891,29 @@ impl Device {
}
}
/// Whose a buffer [`Device::placed_new_buffer`] makes is: a heap's own buffer, or one of the
/// runtime's own, with where it lies in fresh pages.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum NewBufferOf {
/// A heap's own buffer (`heap.rs`), never in fresh pages.
Heap,
/// A buffer of the runtime's own, in fresh pages where it says.
Runtime(FreshPages),
}
/// Where [`Device::placed_new_buffer`] lays a long host-visible buffer of the runtime's own in
/// fresh pages (`fresh_pages.rs`).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum FreshPages {
/// Where the device takes them and its submission checks found that a held import adds
/// nothing to its submissions: the product's buffers.
WherePaid,
/// Wherever the device takes them: the in-place check's buffer of its fresh-pages class,
/// which lies where a rebind's copy under an image in place lies whatever that cost
/// (`buffer_descriptor.rs`'s `copied_pages`), and whose import lives only for its round.
Wherever,
}
/// What a new buffer's bytes hold as [`Device::placed_new_buffer`] makes it.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum NewBytes {
+1 -1
View File
@@ -484,7 +484,7 @@ impl InPlaceChecks {
/// differs, and so does the version it holds). The runtime's own version is not in the record: a
/// release that changes none of these keeps the verdicts the driver gave, so a device made after
/// it reads them again rather than checking anew.
pub(crate) const CHECK_VERSION: u32 = 3;
pub(crate) const CHECK_VERSION: u32 = 4;
/// A record's first bytes.
const MAGIC: [u8; 8] = *b"M2VINPL\0";
@@ -9,16 +9,18 @@
//! refuses by their pointer, imported as dma-bufs), fills a window of it with a pattern, and
//! makes a texture over it as the program does (`Buffer::new_runtime_texture`): the texture takes
//! the image in place exactly as the product's would ([`checking`](crate::in_place::checking)
//! lets it whatever the verdict); the fresh-pages class once the device's submission checks
//! answered whether its buffers take fresh pages (`submit_check.rs`), unchecked where they do
//! not, as the product then makes no such buffer. A thread of its own then writes the
//! pattern's complement into the window, each byte once, at a stride that reaches texels, the
//! bytes between rows and the bytes after the image from its first writes, while the device
//! makes the image's first move
//! (an internal submission naming the texture, waited for) and until it completed. Every byte
//! must then read the CPU's write where the CPU wrote it, and the pattern elsewhere. Each class
//! runs every shape of [`SHAPES`] (texels of 1, 2, 4, 8 and 16 bytes, rows apart or one after
//! another, and 3840x2160 at 4 bytes) [`ROUNDS`] times.
//! lets it whatever the verdict); the fresh-pages class's buffer in fresh pages wherever the
//! device takes them, whatever its submission checks found a held import costs
//! (`submit_check.rs`): the product's long buffers lie in them only where that cost is none
//! (`fresh_pages.rs`), but a rebind's copy under an image in place lies in them wherever the
//! class passed (`buffer_descriptor.rs`), and the check's buffer, never parked, holds its import
//! for its round alone; unchecked where the device takes none. A thread of its own then writes
//! the pattern's complement into the window, each byte once, at a stride that reaches texels, the
//! bytes between rows and the bytes after the image from its first writes, while the device makes
//! the image's first move (an internal submission naming the texture, waited for) and until it
//! completed. Every byte must then read the CPU's write where the CPU wrote it, and the pattern
//! elsewhere. Each class runs every shape of [`SHAPES`] (texels of 1, 2, 4, 8 and 16 bytes, rows
//! apart or one after another, and 3840x2160 at 4 bytes) [`ROUNDS`] times.
//! - **An image that renders** ([`Access::Renders`]) is checked apart, in rounds of their own: the
//! texture is made with `RenderTarget` usage as well, as the program's render targets are, so
//! its image takes the feedback loop usage where the product's does; the writer writes the bytes
@@ -607,19 +609,15 @@ impl ClassPages {
/// A buffer of `length` bytes in pages of `class` on `device`, where the device makes one
/// there; inside a section of its own.
fn make(device: &Device, class: PagesClass, length: u64) -> Option<ClassPages> {
// Whether the device's buffers take fresh pages is its submission checks' answer
// (`submit_check.rs`): the class is checked once they gave it, as the product makes its
// buffers then.
if matches!(class, PagesClass::Fresh) {
device.settle_submit_checks(crate::submit_check::ANSWER_WAIT);
}
let _busy = exit_guard::enter()?;
match class {
PagesClass::Pointer => pointer_pages(device, length),
// In fresh pages wherever the device takes them, whatever its submission checks
// found a held import costs: a rebind's copy under an image in place lies in them
// where this class passed (`buffer_descriptor.rs`'s `copied_pages`), the product's
// long buffers where that cost is none (`fresh_pages.rs`).
PagesClass::Fresh => {
let buffer = device
.new_buffer_placed(length, ResourceOptions::shared(), AddressPlacement::Outside)
.ok()?;
let buffer = device.fresh_pages_check_buffer(length).ok()?;
if !buffer.in_fresh_pages() {
return None;
}
@@ -180,9 +180,8 @@ pub(crate) const TAX_REPEATS: usize = 2;
/// How long one wait for a fence lasts before it is repeated.
const WAIT_SLICE: Duration = Duration::from_millis(100);
/// The longest a making whose path the verdicts choose once for good waits for them (the in-place
/// check's fresh-pages class, `in_place_check.rs`; a guest RAM with a source of dma-bufs,
/// `host_ram.rs`): their run takes milliseconds.
/// The longest a making whose path the verdicts choose once for good waits for them (a guest RAM
/// with a source of dma-bufs, `host_ram.rs`): their run takes milliseconds.
pub(crate) const ANSWER_WAIT: Duration = Duration::from_secs(10);
/// The bytes the follows check copies.
@@ -3,6 +3,8 @@
//! space"), so no pin or pick of the program's depends on whether or when the check runs, and the
//! space's state changes no verdict. Nor does it take a texture ID: its textures are the
//! runtime's own (`resource_ids.rs`), so the program's IDs are Metal's whether or when it runs.
//! And it checks fresh pages wherever the device takes them, also where its held imports cost its
//! submissions and no long buffer of the product's lies in them.
//!
//! A device test: compiled only under the validation crate's GPU safety cfgs (`m2v_gpu`,
//! `m2v_cpu_icd`), on the kind of device the cfg selects. Each device's shader cache is a fresh
@@ -11,6 +13,8 @@
use super::blit_holds::{device, Scratch};
use crate::address::{PAGE, SPACE_BASE, SPACE_END};
use crate::fresh_pages::FRESH_PAGES_PAST;
use crate::in_place::{Access, PagesClass, Verdict as InPlaceVerdict};
use crate::submit_check::{encode, record_name, Check, Measured, Verdict};
use crate::{
AddressPlacement, Buffer, BufferContents, BufferDescriptor, Device, Error, GpuAddress,
@@ -33,21 +37,26 @@ const PINNED: u64 = 16 << 10;
/// The submission checks' answers each device the placement test compares recalls
/// ([`answering`]), in `Check::ALL`'s order: a held import adds nothing to a submission, so the
/// device's long buffers take fresh pages where it makes them and the check's `Fresh` class is
/// checked; the merge and the follows check answer as a driver without either property does.
/// device's long buffers take fresh pages where it makes them; the merge and the follows check
/// answer as a driver without either property does.
const ANSWERS: [Verdict; 3] = [Verdict::Fails, Verdict::Fails, Verdict::Fails];
/// A record of [`ANSWERS`] for the driver of the device the cfg selects, and its path under a
/// shader cache's directory.
/// [`ANSWERS`] but that a held import adds to every submission's cost, as on RADV: the device's
/// long buffers take no fresh pages (`fresh_pages.rs`).
const TAXED: [Verdict; 3] = [Verdict::Fails, Verdict::Holds, Verdict::Fails];
/// A record of submission-check answers for the driver of the device the cfg selects, and its
/// path under a shader cache's directory.
struct AnswersRecord {
answers: [Verdict; 3],
bytes: Vec<u8>,
under: PathBuf,
}
impl AnswersRecord {
/// The record, its identity and path read from a device made over a cache of its own and
/// dropped before any device that recalls the record is made.
fn new() -> AnswersRecord {
/// The record of `answers`, its identity and path read from a device made over a cache of its
/// own and dropped before any device that recalls the record is made.
fn new(answers: [Verdict; 3]) -> AnswersRecord {
let scratch = Scratch::new("in-place-check-identity");
let probe = device(&scratch.0);
let identity = probe.driver_identity();
@@ -55,19 +64,19 @@ impl AnswersRecord {
.record_path(&record_name(&identity));
let under = path.strip_prefix(&scratch.0).unwrap().to_owned();
AnswersRecord {
bytes: encode(&identity, ANSWERS, Measured::default()),
answers,
bytes: encode(&identity, answers, Measured::default()),
under,
}
}
}
/// A device over `scratch`'s shader cache that recalls [`ANSWERS`] from `record`, written there
/// before it is made, and runs no submission check (`submit_check.rs`): what places a long buffer
/// in fresh pages or not (`fresh_pages.rs`), so what the check's `Fresh` class can check at all.
/// Two devices made so differ in no answer the check's classes follow, where the submission
/// checks each timed under the load beside it can answer apart: a held import measured to cost
/// 1.2 us a submission on one llvmpipe device and 0.3 us on the next, either side of the check's
/// 1 us (`TAX_FLOOR`).
/// A device over `scratch`'s shader cache that recalls `record`'s answers, written there before
/// it is made, and runs no submission check (`submit_check.rs`): what places the product's long
/// buffers in fresh pages or not (`fresh_pages.rs`). Two devices made so place them alike, where
/// the submission checks each timed under the load beside it can answer apart: a held import
/// measured to cost 1.2 us a submission on one llvmpipe device and 0.3 us on the next, either side
/// of the check's 1 us (`TAX_FLOOR`).
fn answering(scratch: &Scratch, record: &AnswersRecord) -> Device {
let path = scratch.0.join(&record.under);
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
@@ -80,7 +89,10 @@ fn answering(scratch: &Scratch, record: &AnswersRecord) -> Device {
"the device recalled the written answers and ran no submission check"
);
let recalled = Check::ALL.map(|check| device.shared().submit_checks.verdict(check));
assert_eq!(recalled, ANSWERS, "the recalled submission-check verdicts");
assert_eq!(
recalled, record.answers,
"the recalled submission-check verdicts"
);
device
}
@@ -115,7 +127,7 @@ fn the_in_place_check_takes_no_range_of_the_metal_space() {
.take(lines)
.collect()
};
let record = AnswersRecord::new();
let record = AnswersRecord::new(ANSWERS);
let reference = {
let scratch = Scratch::new("in-place-check-reference");
let device = answering(&scratch, &record);
@@ -162,6 +174,47 @@ fn the_in_place_check_takes_no_range_of_the_metal_space() {
drop((held, pool));
}
/// A device whose held imports cost its submissions (recalled: [`TAXED`]), so that no long buffer
/// of the product's lies in fresh pages, checks the `Fresh` class all the same wherever it takes
/// such pages, as a rebind's copy under a texture in place lies in them where that class passed
/// (`buffer_descriptor.rs`'s `copied_pages`): each of its `Fresh` verdicts is passed or failed
/// where a device recalling [`ANSWERS`] lays a long buffer of its own in fresh pages, and
/// unchecked where it does not.
#[test]
fn the_in_place_check_checks_fresh_pages_where_held_imports_cost_submissions() {
let takes = {
let scratch = Scratch::new("in-place-check-untaxed");
let device = answering(&scratch, &AnswersRecord::new(ANSWERS));
let buffer = device.new_buffer(2 * FRESH_PAGES_PAST, ResourceOptions::shared());
buffer.unwrap().in_fresh_pages()
};
let scratch = Scratch::new("in-place-check-taxed");
let device = answering(&scratch, &AnswersRecord::new(TAXED));
assert!(
!device.fresh_pages_pay(),
"the recalled answers keep long buffers out of fresh pages"
);
assert!(device.settle_in_place_checks(WAIT), "the check answered");
let fresh = Access::ALL.map(|access| device.in_place_verdict(PagesClass::Fresh, access));
eprintln!(
"IN-PLACE-CHECK-TAXED on {}: a long buffer in fresh pages where held imports cost \
nothing {takes}; the Fresh verdicts where they cost {fresh:?}; fresh-page buffers made {}",
device.name(),
device.work_counts().fresh_page_buffers_made
);
match takes {
true => assert!(
!fresh.contains(&InPlaceVerdict::Unchecked),
"every Fresh verdict checked: {fresh:?}"
),
false => assert_eq!(
fresh,
[InPlaceVerdict::Unchecked; 3],
"no fresh pages to check"
),
}
}
/// A private 4x4 `RGBA8Unorm` texture of the program's, at a plain ID.
fn texture(device: &Device) -> Texture {
let mut descriptor = TextureDescriptor::texture_2d(PixelFormat::RGBA8Unorm, 4, 4, false);
@@ -313,9 +313,9 @@ fn sampled_textures_over_host_pages_take_the_path_their_check_answered() {
};
// A long buffer of the device's own lies in fresh pages where its submission
// checks found a held import adds nothing to a submission (`fresh_pages.rs`),
// made then or parked by an earlier one of its length (the device's in-place
// check makes one): none does on a device whose held imports cost its
// submissions, or which imports no host memory, and those alone skip the class.
// made then or parked by an earlier one of its length: none does on a device
// whose held imports cost its submissions, or which imports no host memory, and
// those alone skip the class.
if class == "Fresh" && !device.fresh_pages_pay() {
let report = device.submit_check_report();
let taxed = report.contains("import tax Holds");