From 583c76ebfd15eac51c5cb56821ead9613fb72cec Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 08:10:46 +1100 Subject: [PATCH 1/4] drm/asahi: queue: require whole records in attachment payloads The legacy submit path walks the command buffer twice. The first pass counts commands and skips every non-command payload by its declared size; the second pass parses SET_*_ATTACHMENTS payloads as size / STRIDE records and leaves any remainder unread. A payload that is not a multiple of the record size therefore makes the second pass read the remainder as the next command header, so the command counts taken in the first pass (submission limit, notification count, last-command stamps) no longer describe what is submitted. Refuse an attachment payload that does not hold whole records, in both passes, so both advance by the declared size. Signed-off-by: Ryan Murray --- drivers/gpu/drm/asahi/queue/mod.rs | 60 ++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/drivers/gpu/drm/asahi/queue/mod.rs b/drivers/gpu/drm/asahi/queue/mod.rs index d80395ca3431a..aee529989b41f 100644 --- a/drivers/gpu/drm/asahi/queue/mod.rs +++ b/drivers/gpu/drm/asahi/queue/mod.rs @@ -849,14 +849,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 { +/// 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 { const STRIDE: usize = core::mem::size_of::(); - 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 { + let count = attachment_count(size)?; + let mut attachments = microseq::Attachments { count: count as u32, ..Default::default() @@ -1079,6 +1093,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; @@ -1478,8 +1498,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}; @@ -1492,6 +1517,35 @@ mod tests { v } + const ATTACHMENT: usize = core::mem::size_of::(); + + #[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); From 7f30c011b0b7abaea72d6250095df4bdbcb78666 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 08:10:47 +1100 Subject: [PATCH 2/4] drm/asahi: workqueue: refuse a job when the firmware done pointer is in its slots Job::submit() asserts that the done pointer read from the firmware's queue info is not the next slot it is about to fill. The value is written by the firmware, so a pointer that disagrees with the driver's own count of pending commands panics the kernel in the submit path. Check the slots the job will use before taking any of them and fail the submission with EIO instead. The existing rollback handles a failed submit(). Signed-off-by: Ryan Murray --- drivers/gpu/drm/asahi/workqueue.rs | 44 ++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/asahi/workqueue.rs b/drivers/gpu/drm/asahi/workqueue.rs index 2e84269e7ec62..4dc2b4f0390bd 100644 --- a/drivers/gpu/drm/asahi/workqueue.rs +++ b/drivers/gpu/drm/asahi/workqueue.rs @@ -560,6 +560,21 @@ 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. + let doneptr = inner.doneptr(); + if ring_reaches_done(wptr, command_count, inner.size, doneptr) { + pr_err!( + "WorkQueue: firmware done pointer {} 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); @@ -578,7 +593,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; @@ -1190,9 +1204,35 @@ 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. + 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() { From 70a4f3be8166940c1fc652ee5d1892870a1c1ea4 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 08:10:47 +1100 Subject: [PATCH 3/4] drm/asahi: event: treat out-of-range firmware slot numbers as empty The event channel hands slot numbers from firmware messages (timeout, channel error, TVB grow) straight to the event and buffer managers, which index their owner tables with them. An out-of-range value hits the Rust bounds check and panics the event thread. Look owners up with get() and handle an out-of-range slot like an empty one, which every caller already reports as an error. Signed-off-by: Ryan Murray --- drivers/gpu/drm/asahi/buffer.rs | 2 +- drivers/gpu/drm/asahi/event.rs | 26 +++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/drivers/gpu/drm/asahi/buffer.rs b/drivers/gpu/drm/asahi/buffer.rs index 314087edcd3a9..d99fa9d446ea4 100644 --- a/drivers/gpu/drm/asahi/buffer.rs +++ b/drivers/gpu/drm/asahi/buffer.rs @@ -794,7 +794,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!( diff --git a/drivers/gpu/drm/asahi/event.rs b/drivers/gpu/drm/asahi/event.rs index edd7d701e665c..4c58e924ab4e5 100644 --- a/drivers/gpu/drm/asahi/event.rs +++ b/drivers/gpu/drm/asahi/event.rs @@ -130,6 +130,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(owners: &[Option], slot: u32) -> Option { + 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, @@ -203,7 +209,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(); @@ -218,7 +224,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); @@ -235,7 +241,7 @@ impl EventManager { slot: u32, ) -> Option> { 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. @@ -255,3 +261,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); + } +} From 1b36787501b41728c92479ede7e304316fc6c657 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 27 Nov 2026 16:26:05 +1100 Subject: [PATCH 4/4] drm/asahi: refuse a done pointer outside the ring and bound the RX write pointer Review follow-up. WorkQueue::submit() checked the firmware done pointer only against the slots the job is about to fill. ring_reaches_done() wraps the write pointer with % size, so a done pointer at or past the ring size never matches and the job went ahead. Refuse doneptr >= size with EIO next to the in-range check. Log the refusal once: every refused job would otherwise log again. RxChannel::get_or_peek() trusted the firmware write pointer. One at or past the ring size never equals the read pointer, so the while let Some(msg) = ch.get(0) receive loops would walk stale entries forever. Treat the channel as empty and log once instead. Signed-off-by: Ryan Murray --- drivers/gpu/drm/asahi/channel.rs | 12 +++++++++++- drivers/gpu/drm/asahi/workqueue.rs | 11 +++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/drivers/gpu/drm/asahi/channel.rs b/drivers/gpu/drm/asahi/channel.rs index 1a68469911a19..02ece72ea5424 100644 --- a/drivers/gpu/drm/asahi/channel.rs +++ b/drivers/gpu/drm/asahi/channel.rs @@ -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; diff --git a/drivers/gpu/drm/asahi/workqueue.rs b/drivers/gpu/drm/asahi/workqueue.rs index 4dc2b4f0390bd..a239d0ebe6a11 100644 --- a/drivers/gpu/drm/asahi/workqueue.rs +++ b/drivers/gpu/drm/asahi/workqueue.rs @@ -563,10 +563,12 @@ impl Job::ver { // 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 ring_reaches_done(wptr, command_count, inner.size, doneptr) { - pr_err!( - "WorkQueue: firmware done pointer {} inside the {} slots after {} (ring size {})\n", + 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, @@ -1229,7 +1231,8 @@ mod tests { 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. + // 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)); }