Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion drivers/gpu/drm/asahi/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ impl Drop for Vm {
self.vm.close_idle_reclaim();
if let Some(status) = self.vm.status() {
// Retained graphs may keep this status alive; the client's mirror
// object must not outlive the client's VM handle.
// objects (status and fence-error count) must not outlive the
// client's VM handle.
status.release_mirror();
}
if self.vm.status().is_some() {
Expand Down Expand Up @@ -536,6 +537,13 @@ impl File {
uapi::DRM_ASAHI_PARAM_GROUP_VM_STATUS_MIRROR if gpu.supports_vm_status() => {
return Self::register_vm_status_mirror(data, file);
}
uapi::DRM_ASAHI_PARAM_GROUP_VM_FENCE_ERROR_MIRROR => {
// Only backends whose failed output fences are all counted (g17).
if !gpu.supports_vm_status() || !gpu.supports_vm_fence_error_count() {
return Err(EOPNOTSUPP);
}
return Self::register_vm_fence_error_mirror(data, file);
}
uapi::DRM_ASAHI_PARAM_GROUP_QUEUE_LIMITS => {
if data.pad != 0
|| data.size < core::mem::size_of::<uapi::drm_asahi_queue_limits>() as u64
Expand Down Expand Up @@ -649,6 +657,46 @@ impl File {
Ok(0)
}

/// Register a file-owned GEM word as the mirror of a file-owned VM's fence-error count.
fn register_vm_fence_error_mirror(
data: &uapi::drm_asahi_get_params,
file: &DrmFile,
) -> Result<u32> {
type Request = uapi::drm_asahi_vm_fence_error_mirror;
kernel::static_assert!(core::mem::size_of::<Request>() == 24);
kernel::static_assert!(core::mem::offset_of!(Request, handle) == 4);
kernel::static_assert!(core::mem::offset_of!(Request, offset) == 8);
kernel::static_assert!(core::mem::offset_of!(Request, flags) == 16);
let size = core::mem::size_of::<Request>();
if data.pad != 0 || data.size < size as u64 || data.size > 4096 {
return Err(EINVAL);
}
let user = UserPtr::from_addr(data.pointer as usize);
let mut reader = UserSlice::new(user, data.size as usize).reader();
let [ids, offset, flags] = reader.read::<[u64; 3]>()?;
// A longer request is a newer struct: fields this kernel does not know must be zero.
let mut tail = [0u8; 64];
while !reader.is_empty() {
let chunk = &mut tail[..reader.len().min(64)];
reader.read_slice(chunk)?;
if chunk.iter().any(|&byte| byte != 0) {
return Err(EINVAL);
}
}
// vm_id and handle share the first 8 bytes, in memory order.
let ids = ids.to_ne_bytes();
let vm_id = u32::from_ne_bytes([ids[0], ids[1], ids[2], ids[3]]);
let handle = u32::from_ne_bytes([ids[4], ids[5], ids[6], ids[7]]);
if flags != 0 || handle == 0 || offset % core::mem::align_of::<u64>() as u64 != 0 {
return Err(EINVAL);
}
let offset = usize::try_from(offset).map_err(|_| EINVAL)?;
let status = Self::vm_status(file, vm_id)?;
let owner = gem::ObjectRef::new(gem::Object::lookup_handle(file, handle)?);
status.attach_fence_error_mirror(owner, offset)?;
Ok(0)
}

fn vm_status(file: &DrmFile, vm_id: u32) -> Result<Arc<crate::g17::status::VmStatus>> {
let vms = file.inner().vms();
let guard = vms.lock();
Expand Down
82 changes: 78 additions & 4 deletions drivers/gpu/drm/asahi/g17/fence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//! Signalling belongs outside the device lock, after timestamp and VM-status writes.

use super::context::{Context, WorkStateLease};
use super::status::VmStatus;
use crate::file::SyncItem;
use core::sync::atomic::{AtomicBool, AtomicI32, AtomicU32, AtomicU64, AtomicU8, AtomicUsize, Ordering};
use kernel::{
Expand Down Expand Up @@ -190,6 +191,8 @@ pub(crate) struct Submission {
error: AtomicI32,
/// Live Member objects, plus the enqueue guard, independently of signalling.
owners: AtomicU8,
/// The VM's status, captured at publication: it counts this fence if it fails.
status: Arc<VmStatus>,
firmware_source: bool,
/// Set under the device mutex when the only member's successful retirement
/// is queued, before its fence is signalled off-lock: a consumer published
Expand All @@ -199,16 +202,22 @@ pub(crate) struct Submission {

impl Submission {
fn new(context: Arc<Context>, firmware_source: bool) -> Result<Arc<Self>> {
let status = context.status().clone();
let work = Arc::new(WorkStateLease::deferred(context)?, GFP_KERNEL)?;
Self::with_work(Some(work), firmware_source)
Self::with_work(Some(work), status, firmware_source)
}
fn with_work(work: Option<Arc<WorkStateLease>>, firmware_source: bool) -> Result<Arc<Self>> {
fn with_work(
work: Option<Arc<WorkStateLease>>,
status: Arc<VmStatus>,
firmware_source: bool,
) -> Result<Arc<Self>> {
Arc::pin_init(
pin_init!(Self {
state <- new_mutex!(State { work, ready: None }, "G17 submission work state"),
remaining: AtomicU8::new(1),
error: AtomicI32::new(0),
owners: AtomicU8::new(1),
status,
firmware_source,
retired: AtomicBool::new(false),
}),
Expand Down Expand Up @@ -267,8 +276,13 @@ impl Submission {
self.note_error(error);
}
if self.remaining.fetch_sub(1, Ordering::AcqRel) == 1 {
// The final error: note_error may have set it before any member completed.
let error = self.error.load(Ordering::Acquire);
if error != 0 {
// The only place a published submission's output fence takes an error, so
// every failed one is counted, once, before it signals below. A submission
// that fails before publication is counted in Outputs instead.
self.status.count_fence_error();
fence.set_error(Error::from_errno(error));
} else {
self.release_state();
Expand Down Expand Up @@ -366,11 +380,17 @@ mod tests {
fn contexts() -> FenceContexts {
FenceContexts::new(1, c_str!("asahi_promise_test"), KEY).unwrap()
}
fn aggregate(contexts: &FenceContexts) -> Aggregate {
fn status() -> Arc<VmStatus> {
Arc::pin_init(VmStatus::new(), GFP_KERNEL).unwrap()
}
fn aggregate_for(contexts: &FenceContexts, status: Arc<VmStatus>) -> Aggregate {
let mut fence = export_fallback(contexts).unwrap();
fence.source = Some(Submission::with_work(None, true).unwrap());
fence.source = Some(Submission::with_work(None, status, true).unwrap());
Aggregate(fence.into())
}
fn aggregate(contexts: &FenceContexts) -> Aggregate {
aggregate_for(contexts, status())
}

#[test]
fn exact_export_identity_recognizes_unpublished_source() {
Expand Down Expand Up @@ -417,6 +437,50 @@ mod tests {
);
}

#[test]
fn failed_output_fences_are_counted_once_before_they_signal() {
let contexts = contexts();
let status = status();
let fence_status =
|aggregate: &Aggregate| super::super::job::fence_status(&aggregate.fence());
// Success: no count.
let ok = aggregate_for(&contexts, status.clone());
let member = ok.member();
ok.finish_enqueue(Ok(()));
member.complete(Ok(()));
assert!(fence_status(&ok) > 0);
assert_eq!(status.fence_errors(), 0);
// Two failed members of one submission: one failed fence, counted once.
let failed = aggregate_for(&contexts, status.clone());
let (a, b) = (failed.member(), failed.member());
failed.finish_enqueue(Ok(()));
a.complete(Err(ECANCELED));
// Another member still holds the fence.
assert_eq!(status.fence_errors(), 0);
b.complete(Err(ENODATA));
assert_eq!(fence_status(&failed), ECANCELED.to_errno());
assert_eq!(status.fence_errors(), 1);
// An error noted early fails the fence even if every member then succeeds.
let noted = aggregate_for(&contexts, status.clone());
let member = noted.member();
noted.finish_enqueue(Ok(()));
member.note_error(EIO);
member.complete(Ok(()));
assert_eq!(fence_status(&noted), EIO.to_errno());
assert_eq!(status.fence_errors(), 2);
// A member dropped without completing cancels its fence; so does an enqueue error.
let dropped = aggregate_for(&contexts, status.clone());
drop(dropped.member());
dropped.finish_enqueue(Ok(()));
assert_eq!(status.fence_errors(), 3);
let rejected = aggregate_for(&contexts, status.clone());
rejected.finish_enqueue(Err(ENOMEM));
assert_eq!(status.fence_errors(), 4);
// The reported VM status ignores these collateral errors; the count does not.
assert_eq!(status.reported_error(), 0);
drop((a, b, member));
}

#[test]
fn rejected_private_fence_never_exports_source_state() {
let fence = export_fallback(&contexts()).unwrap();
Expand Down Expand Up @@ -471,13 +535,17 @@ pub(crate) struct Outputs {
submission: Option<Aggregate>,
result: Result,
cumulative: Option<super::sync_output::SignalBundle>,
/// The queue's VM status. An error fence installed instead of a published submission's
/// is counted in it first, as `Submission::member_done` counts a published one.
status: Arc<VmStatus>,
}
impl Outputs {
pub(crate) fn new(
contexts: &FenceContexts,
mut syncs: KVec<SyncItem>,
input_count: usize,
allocation_failure: &Fence,
status: &Arc<VmStatus>,
) -> Result<Self> {
if input_count > syncs.len() {
return Err(EINVAL);
Expand All @@ -486,6 +554,8 @@ impl Outputs {
let fallback = match export_fallback(contexts) {
Ok(fence) => fence,
Err(error) => {
// The outputs end with the queue's failed fence: count it before installing.
status.count_fence_error();
if let Some(bundle) = &mut cumulative {
bundle.fail(allocation_failure);
} else {
Expand All @@ -501,6 +571,7 @@ impl Outputs {
submission: None,
result: Err(ECANCELED),
cumulative,
status: status.clone(),
})
}
pub(crate) fn inputs(&self) -> &[SyncItem] {
Expand Down Expand Up @@ -548,6 +619,9 @@ impl Drop for Outputs {
}
if let Some(fallback) = self.fallback.take() {
if let Err(error) = self.result {
// Counted before the fallback is installed or signals. A restarted SUBMIT
// installs it at the same timeline points again, and the count tells.
self.status.count_fence_error();
fallback.set_error(error);
}
if let Some(bundle) = &mut self.cumulative {
Expand Down
7 changes: 7 additions & 0 deletions drivers/gpu/drm/asahi/g17/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2561,6 +2561,13 @@ impl gpu::Gpu for Gpu {
true
}

/// A published submission's output fence takes its error only in
/// `fence::Submission::member_done`, and a SUBMIT that fails before publication installs
/// its error fence through `fence::Outputs`; both count it in the VM status first.
fn supports_vm_fence_error_count(&self) -> bool {
true
}

/// The G17 runtime never programs fault_control into the firmware, so unmapped reads are not
/// guaranteed to return zero. Advertising soft faults made Mesa point out-of-bounds vec3/vec4
/// vertex fetches at address 0 and skip bounds checks on bounded loads; on T6040 those reads
Expand Down
1 change: 1 addition & 0 deletions drivers/gpu/drm/asahi/g17/queue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ impl<B: Backend> crate::queue::Queue for Queue<B> {
syncs,
input_count,
&self.allocation_failure,
self.context.status(),
) {
Ok(outputs) => outputs,
Err(error) => {
Expand Down
Loading
Loading