Skip to content
Merged
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
2 changes: 1 addition & 1 deletion drivers/gpu/drm/asahi/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ impl BufferManager::ver {
pub(crate) fn grow(&self, slot: u32) {
match self
.0
.with_inner(|inner| inner.owners[slot as usize].as_ref().cloned())
.with_inner(|inner| crate::event::slot_owner(&inner.owners, slot))
{
Some(owner) => {
pr_err!(
Expand Down
12 changes: 11 additions & 1 deletion drivers/gpu/drm/asahi/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,17 @@ where
self.ring.state.with(|raw, _inner| {
let wptr = T::wptr(raw, index);
let rptr = &mut self.rptr[index];
if wptr == *rptr {
if wptr >= self.count {
// The firmware writes this pointer. One outside the ring never equals our read
// pointer, so the receive loops would spin on stale entries forever: treat the
// channel as empty instead.
kernel::pr_err_once!(
"RxChannel: firmware write pointer {} outside the {}-entry ring\n",
wptr,
self.count
);
None
} else if wptr == *rptr {
None
} else {
let off = self.count as usize * index;
Expand Down
26 changes: 23 additions & 3 deletions drivers/gpu/drm/asahi/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ impl slotalloc::SlotItem for EventInner {
}
}

/// Returns the owner registered for `slot`. Slot numbers come from firmware messages, so one
/// outside the table is treated like an empty slot instead of being trusted as an index.
pub(crate) fn slot_owner<T: Clone>(owners: &[Option<T>], slot: u32) -> Option<T> {
owners.get(slot as usize).and_then(|owner| owner.as_ref().cloned())
}

/// Inner data for the event manager, to be protected by the SlotAllocator lock.
pub(crate) struct EventManagerInner {
stamps: GpuArray<Stamp>,
Expand Down Expand Up @@ -234,7 +240,7 @@ impl EventManager {
pub(crate) fn signal(&self, slot: u32) {
match self
.alloc
.with_inner(|inner| inner.owners[slot as usize].as_ref().cloned())
.with_inner(|inner| slot_owner(&inner.owners, slot))
{
Some(owner) => {
owner.signal();
Expand All @@ -249,7 +255,7 @@ impl EventManager {
pub(crate) fn mark_error(&self, slot: u32, wait_value: u32, error: workqueue::WorkError) {
match self
.alloc
.with_inner(|inner| inner.owners[slot as usize].as_ref().cloned())
.with_inner(|inner| slot_owner(&inner.owners, slot))
{
Some(owner) => {
owner.mark_error(EventValue(wait_value), error);
Expand All @@ -266,7 +272,7 @@ impl EventManager {
slot: u32,
) -> Option<Arc<dyn workqueue::WorkQueue + Send + Sync>> {
self.alloc
.with_inner(|inner| inner.owners[slot as usize].as_ref().cloned())
.with_inner(|inner| slot_owner(&inner.owners, slot))
}

/// Fail all commands, used when the GPU crashes.
Expand All @@ -286,3 +292,17 @@ impl EventManager {
}
}
}

#[kunit_tests(asahi_event_slot_owner)]
mod tests {
use super::slot_owner;

#[test]
fn firmware_slot_outside_the_table_has_no_owner() {
let owners = [Some(7u32), None];
assert_eq!(slot_owner(&owners, 0), Some(7));
assert_eq!(slot_owner(&owners, 1), None);
assert_eq!(slot_owner(&owners, 2), None);
assert_eq!(slot_owner(&owners, u32::MAX), None);
}
}
60 changes: 57 additions & 3 deletions drivers/gpu/drm/asahi/queue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,14 +851,28 @@ unsafe impl AnyBitPattern for uapi::drm_asahi_cmd_compute {}
// SAFETY: This UAPI record consists of integer fields and nested integer records.
unsafe impl AnyBitPattern for uapi::drm_asahi_attachment {}

fn build_attachments(reader: &mut Reader<'_>, size: usize) -> Result<microseq::Attachments> {
/// Returns the number of attachment records in a `SET_*_ATTACHMENTS` payload of `size` bytes.
///
/// The payload must hold whole records: both passes over the command buffer advance by the
/// header's size, so a remainder would be skipped by one and parsed as a header by the other.
fn attachment_count(size: usize) -> Result<usize> {
const STRIDE: usize = core::mem::size_of::<uapi::drm_asahi_attachment>();
let count = size / STRIDE;

if size % STRIDE != 0 {
return Err(EINVAL);
}

let count = size / STRIDE;
if count > microseq::MAX_ATTACHMENTS {
return Err(EINVAL);
}

Ok(count)
}

fn build_attachments(reader: &mut Reader<'_>, size: usize) -> Result<microseq::Attachments> {
let count = attachment_count(size)?;

let mut attachments = microseq::Attachments {
count: count as u32,
..Default::default()
Expand Down Expand Up @@ -1081,6 +1095,12 @@ impl Queue for Queue::ver {
return Err(EINVAL);
}
}
uapi::drm_asahi_cmd_type_DRM_ASAHI_SET_VERTEX_ATTACHMENTS
| uapi::drm_asahi_cmd_type_DRM_ASAHI_SET_FRAGMENT_ATTACHMENTS
| uapi::drm_asahi_cmd_type_DRM_ASAHI_SET_COMPUTE_ATTACHMENTS => {
attachment_count(payload_size)?;
cmdbuf.skip(payload_size);
}
_ => cmdbuf.skip(payload_size),
}
nr_commands += 1;
Expand Down Expand Up @@ -1480,8 +1500,13 @@ impl Queue for Queue::ver {

#[kunit_tests(asahi_legacy_inline_queue)]
mod tests {
use super::{can_submit_inline, init_was_lost, prune_tvb_uses, TvbUse};
use super::{
attachment_count, build_attachments, can_submit_inline, init_was_lost, prune_tvb_uses,
TvbUse,
};
use crate::util::Reader;
use kernel::prelude::*;
use kernel::uapi;

use TvbUse::{Done, FailedPublished as FailPub, FailedUnpublished as FailUnpub, Pending};

Expand All @@ -1494,6 +1519,35 @@ mod tests {
v
}

const ATTACHMENT: usize = core::mem::size_of::<uapi::drm_asahi_attachment>();

#[test]
fn attachment_payload_must_hold_whole_records() {
assert_eq!(attachment_count(0).ok(), Some(0));
assert_eq!(attachment_count(2 * ATTACHMENT).ok(), Some(2));
assert!(attachment_count(8).is_err());
assert!(attachment_count(ATTACHMENT + 8).is_err());
assert!(attachment_count(17 * ATTACHMENT).is_err());
}

#[test]
fn attachment_parse_consumes_the_declared_size() {
let raw = [0u8; 3 * ATTACHMENT];

// A remainder after the last record is refused, not left for the next header.
let mut reader = Reader::new(&raw);
assert!(build_attachments(&mut reader, ATTACHMENT + 8).is_err());
let mut reader = Reader::new(&raw);
assert!(build_attachments(&mut reader, 8).is_err());

// Whole records are consumed exactly, so the next header starts after them.
let mut reader = Reader::new(&raw);
let list = build_attachments(&mut reader, 2 * ATTACHMENT).expect("two records");
assert_eq!(list.count, 2);
reader.skip(ATTACHMENT);
assert!(reader.is_empty());
}

#[test]
fn tvb_use_from_fence_status() {
assert_eq!(TvbUse::from_status(0, false), Pending);
Expand Down
47 changes: 45 additions & 2 deletions drivers/gpu/drm/asahi/workqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,23 @@ impl Job::ver {
return Err(EBUSY);
}

// The done pointer is written by the firmware. One inside the slots this job is about to
// fill does not match our own count of pending commands: refuse the job rather than
// overwrite entries the firmware may still read.
// A done pointer outside the ring is no position at all: refuse it as well.
let doneptr = inner.doneptr();
if doneptr >= inner.size || ring_reaches_done(wptr, command_count, inner.size, doneptr) {
// Every refused job would log again; the first report is the useful one.
kernel::pr_err_once!(
"WorkQueue: firmware done pointer {} out of range or inside the {} slots after {} (ring size {})\n",
doneptr,
command_count,
wptr,
inner.size
);
return Err(EIO);
}

inner.pending.reserve(command_count, GFP_KERNEL)?;

inner.last_submitted = Some(self.event_info.value);
Expand All @@ -578,7 +595,6 @@ impl Job::ver {
command.as_mut().inner_mut().set_wptr(wptr);

let next_wptr = (wptr + 1) % inner.size;
assert!(inner.doneptr() != next_wptr);
inner.info_mut().ring[wptr as usize] = command.inner.gpu_va().get();
wptr = next_wptr;

Expand Down Expand Up @@ -1190,9 +1206,36 @@ impl WorkQueue for WorkQueue::ver {
}
}

/// Returns whether writing `count` ring entries from `wptr` would move the write pointer onto
/// `doneptr`, the firmware's done pointer, in a ring of `size` entries.
fn ring_reaches_done(wptr: u32, count: usize, size: u32, doneptr: u32) -> bool {
let mut wptr = wptr;
for _ in 0..count {
wptr = (wptr + 1) % size;
if wptr == doneptr {
return true;
}
}
false
}

#[kunit_tests(asahi_legacy_inline_workqueue)]
mod tests {
use super::can_submit_inline;
use super::{can_submit_inline, ring_reaches_done};

#[test]
fn done_pointer_inside_the_new_entries_is_refused() {
// Firmware caught up: done == wptr, the next entries are free.
assert!(!ring_reaches_done(5, 3, 8, 5));
// Done pointer in the slots the job would fill, including after the wrap.
assert!(ring_reaches_done(5, 3, 8, 6));
assert!(ring_reaches_done(5, 3, 8, 0));
assert!(!ring_reaches_done(5, 3, 8, 1));
// A done pointer outside the ring never matches here; submit() refuses it on its own
// (`doneptr >= size`) before asking.
assert!(!ring_reaches_done(5, 3, 8, 8));
assert!(!ring_reaches_done(5, 3, 8, u32::MAX));
}

#[test]
fn depth_192_stops_at_the_first_inflight_command() {
Expand Down
Loading