From efb879063f590a6c8725486bda8264d45a09c577 Mon Sep 17 00:00:00 2001 From: Raul Paiva <63853678+Raul-Paiva@users.noreply.github.com> Date: Sat, 28 Nov 2026 19:34:54 +0000 Subject: [PATCH] D3D12.X: DRED's VA 0 output follows the device state it comes with When the device is removed, DRED's page-fault output can be VA 0 with no allocations named. The log said "DRED page fault at GPU VA 0x0; allocations there: []", which reads as a real fault at VA 0. DRED gives that when it recorded no fault, but the allocation lists may be empty for a real fault too. The device state read just before tells the two apart: - state 7 (PAGEFAULT): a page fault happened; it is logged as a fault at VA 0x0, as before (the null-address faults of #2920); - another known state (3 hung, 6 fault): "no page fault: the device state is another removal"; - state 0 (UNKNOWN) or no state: "no fault recorded, or a fault DRED could not place". An address other than 0, or any allocation named, is logged as before. Here, removals with device state 0 and that output were not GPU page faults. DRED device state 0 is UNKNOWN, not "the GPU's work did not fault": it is now logged as "0 (unknown)" and the comment no longer infers it. A failed GetPageFaultAllocationOutput was silent. Its HRESULT is now logged when XCL switched the page-fault output on; without XCL_DRED the log says it is not switched on (agility::dred_switched_on). With XCL_DRED=pagefault the breadcrumbs are never switched on, yet the log said "no list was in flight". It now says they are not switched on; with them on and no node returned, it says "empty output" instead of inferring that no list was in flight. Review by Prime1997 (device state 0 as unknown, the page-fault query error, VA 0 kept ambiguous, "empty output") and Foniass (the device state decides VA 0, the output not switched on, tests of the wording). Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/agility.rs | 12 +++ crates/xcl/src/gpu/agility_tests.rs | 13 ++++ crates/xcl/src/hle/d3d12x/real.rs | 113 ++++++++++++++++++++++++---- 3 files changed, 125 insertions(+), 13 deletions(-) diff --git a/crates/xcl/src/gpu/agility.rs b/crates/xcl/src/gpu/agility.rs index 82d229e48f..4543faa4a3 100644 --- a/crates/xcl/src/gpu/agility.rs +++ b/crates/xcl/src/gpu/agility.rs @@ -402,6 +402,18 @@ pub fn enable_dred_once() -> &'static Option> { }) } +/// What `enable_dred_once` (`done`) switched on for the request `request`: None, nothing (unset, +/// or the settings were unavailable); else the page-fault output, and whether the breadcrumbs too. +fn dred_on(done: Option<&Option>>, request: Option) -> Option { + if matches!(done, Some(Some(Ok(())))) { request } else { None } +} + +/// What `enable_dred_once` switched on: None, nothing; else whether the breadcrumbs are on too +/// (not with `XCL_DRED=pagefault`). +pub fn dred_switched_on() -> Option { + dred_on(DRED.get(), dred_request(std::env::var_os("XCL_DRED"))) +} + /// Enable the debug layer at most once per process. The GPU test runner may create a /// device before D3D12.X starts; enabling it again after that removes the device. pub fn enable_debug_layer() -> &'static Result<(), String> { diff --git a/crates/xcl/src/gpu/agility_tests.rs b/crates/xcl/src/gpu/agility_tests.rs index 5317a3e3ac..75f81c2cb3 100644 --- a/crates/xcl/src/gpu/agility_tests.rs +++ b/crates/xcl/src/gpu/agility_tests.rs @@ -268,3 +268,16 @@ fn resolve_dir_handles_explicit_override_and_implicit_fallback() { let _ = std::fs::remove_dir_all(&root); } + +/// DRED's outputs count as switched on only when `enable_dred_once` set them: the breadcrumbs +/// with `XCL_DRED=1`, the page faults with it or `=pagefault`. +#[test] +fn dred_outputs_are_on_only_when_the_settings_took() { + let ok: Option> = Some(Ok(())); + assert_eq!(dred_on(Some(&ok), Some(true)), Some(true), "XCL_DRED=1"); + assert_eq!(dred_on(Some(&ok), Some(false)), Some(false), "XCL_DRED=pagefault: page faults only"); + let failed: Option> = Some(Err("unavailable".into())); + assert_eq!(dred_on(Some(&failed), Some(true)), None, "requested, but the settings were unavailable"); + assert_eq!(dred_on(Some(&None), None), None, "not requested"); + assert_eq!(dred_on(None, Some(true)), None, "not set up yet"); +} diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index a9dad2dd11..50d94cfe33 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -12427,6 +12427,42 @@ mod dred_tests { assert_eq!(over_video_budget(30 << 30, 0), None, "no budget known (the query failed)"); } + /// DRED's page-fault output at VA 0 with no allocations named is a fault at VA 0 when the + /// device state says a page fault happened (7), none when the state rules one out, and either + /// when the state is unknown (0) or not available. + #[test] + fn dred_output_at_va_0_with_no_allocations_follows_the_device_state() { + let (unknown, hung, fault, page_fault) = (D3D12_DRED_DEVICE_STATE_UNKNOWN.0, D3D12_DRED_DEVICE_STATE_HUNG.0, D3D12_DRED_DEVICE_STATE_FAULT.0, D3D12_DRED_DEVICE_STATE_PAGEFAULT.0); + assert_eq!(dred_unplaced_page_fault(Some(page_fault), 0, false, false), None, "state 7: a real fault at VA 0, logged as one"); + for state in [Some(unknown), None] { + let line = dred_unplaced_page_fault(state, 0, false, false).expect("VA 0 and no allocations is not logged as a fault at VA 0"); + assert!(line.contains("no fault recorded, or"), "{state:?}: neither reading is ruled out: {line}"); + } + for state in [hung, fault] { + let line = dred_unplaced_page_fault(Some(state), 0, false, false).expect("not a page fault"); + assert!(line.contains("no page fault"), "state {state}: {line}"); + } + for state in [Some(unknown), Some(page_fault), None] { + assert_eq!(dred_unplaced_page_fault(state, 0x1_2345_0000, false, false), None); + assert_eq!(dred_unplaced_page_fault(state, 0, true, false), None, "an allocation named: logged as a fault"); + assert_eq!(dred_unplaced_page_fault(state, 0, false, true), None); + } + } + + /// An output DRED did not give is told apart from one it was not asked for. + #[test] + fn missing_dred_output_says_whether_it_was_switched_on() { + assert_eq!(dred_page_fault_missing(0x8000_4005, true), "DRED page-fault output not available (0x80004005)"); + assert!(dred_page_fault_missing(0x8000_4005, false).contains("not switched on")); + assert_eq!(dred_breadcrumbs_missing(Ok(true), true), None, "breadcrumbs there"); + assert_eq!(dred_breadcrumbs_missing(Ok(true), false), None); + for got in [Ok(false), Err(0x8000_4005)] { + assert!(dred_breadcrumbs_missing(got, false).is_some_and(|l| l.contains("not switched on; XCL_DRED=1 does")), "{got:?}"); + } + assert_eq!(dred_breadcrumbs_missing(Err(0x8000_4005), true).as_deref(), Some("DRED: no breadcrumbs (0x80004005)")); + assert_eq!(dred_breadcrumbs_missing(Ok(false), true).as_deref(), Some("DRED: no breadcrumbs (empty output)")); + } + #[test] fn a_late_partially_executed_list_is_reported_before_unstarted_lists() { let progress = [0u32, 1, 2]; @@ -12449,6 +12485,42 @@ mod dred_tests { } } +/// The line for DRED's page-fault output when it is VA 0 with no allocations named, given the +/// device state DRED reported (`state`, None: not available). That is what it gives with no fault +/// recorded, but the lists may be empty for a real fault too: the state tells them apart when it +/// is known (a page fault, or another removal), else it is logged as either. `None`: an address +/// other than 0, an allocation named or a page-fault state, a fault to log. +fn dred_unplaced_page_fault(state: Option, va: u64, existing: bool, freed: bool) -> Option<&'static str> { + if va != 0 || existing || freed || state == Some(D3D12_DRED_DEVICE_STATE_PAGEFAULT.0) { + return None; + } + Some(match state { + Some(s) if s != D3D12_DRED_DEVICE_STATE_UNKNOWN.0 => "DRED page-fault output: VA 0x0, no allocations named (no page fault: the device state is another removal)", + _ => "DRED page-fault output: VA 0x0, no allocations named (no fault recorded, or a fault DRED could not place)", + }) +} + +/// The line for a page-fault output DRED did not give (`hr`), with the page-fault output +/// switched on (`on`, `agility::dred_switched_on`) or not. +fn dred_page_fault_missing(hr: u32, on: bool) -> String { + if on { + format!("DRED page-fault output not available ({hr:#x})") + } else { + "DRED: no page-fault output (not switched on; XCL_DRED=1 or XCL_DRED=pagefault does)".into() + } +} + +/// The line for breadcrumbs DRED gave (`Ok(true)`: some, `Ok(false)`: none) or did not (`Err`), +/// with the breadcrumbs switched on (`on`) or not. None: there are some to report. +fn dred_breadcrumbs_missing(got: Result, on: bool) -> Option { + match got { + Ok(true) => None, + _ if !on => Some("DRED: no breadcrumbs (not switched on; XCL_DRED=1 does)".into()), + Err(hr) => Some(format!("DRED: no breadcrumbs ({hr:#x})")), + Ok(false) => Some("DRED: no breadcrumbs (empty output)".into()), + } +} + /// If the device was removed: the reason, DRED's page fault (address, the allocations around /// it) and the operations each command list got to – into the log, once. pub fn log_device_removed() { @@ -12467,14 +12539,26 @@ pub fn log_device_removed() { crate::xlog_error!("system commit at the removal: {} of {} MB ({} MB left)", commit >> 20, limit >> 20, limit.saturating_sub(commit) >> 20); } unsafe { - // (DRED 1.2: 3 hung, 6 fault, 7 page fault; 0 when the removal did not come from work - // the GPU was doing – then there is no page fault and there are no breadcrumbs either) - match dev.cast::() { - Ok(d) => crate::xlog_error!("DRED device state: {}", d.GetDeviceState().0), - Err(e) => crate::xlog_error!("DRED device state: not available ({:#x})", e.code().0 as u32), - } + // (DRED 1.2: 3 hung, 6 fault, 7 page fault; 0 is unknown, which does not rule out a fault + // of the GPU's work or a page fault) + let state = match dev.cast::() { + Ok(d) => { + let state = d.GetDeviceState(); + crate::xlog_error!("DRED device state: {}{}", state.0, if state == D3D12_DRED_DEVICE_STATE_UNKNOWN { " (unknown)" } else { "" }); + Some(state.0) + } + Err(e) => { + crate::xlog_error!("DRED device state: not available ({:#x})", e.code().0 as u32); + None + } + }; + let switched_on = crate::gpu::agility::dred_switched_on(); let Ok(dred) = dev.cast::() else { return }; - if let Ok(pf) = dred.GetPageFaultAllocationOutput() { + let page_fault = dred.GetPageFaultAllocationOutput(); + if let Err(e) = &page_fault { + crate::xlog_error!("{}", dred_page_fault_missing(e.code().0 as u32, switched_on.is_some())); + } + if let Ok(pf) = page_fault { let names = |mut n: *const D3D12_DRED_ALLOCATION_NODE| { let mut v = Vec::new(); while !n.is_null() && v.len() < 8 { @@ -12486,14 +12570,17 @@ pub fn log_device_removed() { v.join(", ") }; let va = pf.PageFaultVA; - let window = uma_window_vas().iter().position(|&w| va >= w && va < w + 0x1_0000_0000).map(|k| format!(" = window {k} + {:#x}", va - uma_window_vas()[k])).unwrap_or_default(); - crate::xlog_error!("DRED page fault at GPU VA {va:#x}{window}; allocations there: [{}]; recently freed: [{}]", names(pf.pHeadExistingAllocationNode), names(pf.pHeadRecentFreedAllocationNode)); + if let Some(line) = dred_unplaced_page_fault(state, va, !pf.pHeadExistingAllocationNode.is_null(), !pf.pHeadRecentFreedAllocationNode.is_null()) { + crate::xlog_error!("{line}"); + } else { + let window = uma_window_vas().iter().position(|&w| va >= w && va < w + 0x1_0000_0000).map(|k| format!(" = window {k} + {:#x}", va - uma_window_vas()[k])).unwrap_or_default(); + crate::xlog_error!("DRED page fault at GPU VA {va:#x}{window}; allocations there: [{}]; recently freed: [{}]", names(pf.pHeadExistingAllocationNode), names(pf.pHeadRecentFreedAllocationNode)); + } } let breadcrumbs = dred.GetAutoBreadcrumbsOutput(); - match &breadcrumbs { - Err(e) => crate::xlog_error!("DRED: no breadcrumbs ({:#x})", e.code().0 as u32), - Ok(bc) if bc.pHeadAutoBreadcrumbNode.is_null() => crate::xlog_error!("DRED: no breadcrumbs (no list was in flight)"), - Ok(_) => {} + let got = breadcrumbs.as_ref().map(|bc| !bc.pHeadAutoBreadcrumbNode.is_null()).map_err(|e| e.code().0 as u32); + if let Some(line) = dred_breadcrumbs_missing(got, switched_on == Some(true)) { + crate::xlog_error!("{line}"); } if let Ok(bc) = breadcrumbs { report_dred_lists(bc.pHeadAutoBreadcrumbNode, |node, done| {