Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 17 additions & 0 deletions drivers/gpu/drm/asahi/m3/m3_batch_policy.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT
//! Bounds and retirement decisions for completion-worker batching.

Expand Down Expand Up @@ -83,10 +83,27 @@
!pending && !active
}

/// A failed packet that no batch ever included can drop its VM job guard: the firmware never saw
/// it. Any other keeps the guard, and its VM stays quarantined, until the runtime drops it.
pub(crate) const fn release_unpublished(may_be_published: bool) -> bool {
!may_be_published
}

pub(crate) const fn retain_runtime(stop_failed: bool, gpu_pending: bool) -> bool {
stop_failed || gpu_pending
}

/// One firmware start per boot. `stop_asc` only clears RUN: it doesn't reset the firmware or
/// give back a reusable RTKit or InitData state, and TTBAT context 0 still names the lower root
/// the first start adopted, which is freed once ASC has stopped. Claimed just before the first
/// ASC start and never released.
pub(crate) struct StartLatch(core::sync::atomic::AtomicBool);
impl StartLatch {
pub(crate) const fn new() -> Self { Self(core::sync::atomic::AtomicBool::new(false)) }
/// True for the first claim only.
pub(crate) fn claim(&self) -> bool { !self.0.swap(true, core::sync::atomic::Ordering::AcqRel) }
}

#[derive(Default)]
pub(crate) struct PassCosts { recent: [Option<(u64, u64)>; 8] }
impl PassCosts {
Expand Down
25 changes: 23 additions & 2 deletions drivers/gpu/drm/asahi/m3/m3_runtime.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT
use core::{mem::ManuallyDrop, sync::atomic::Ordering};
use kernel::{device::Core, platform, prelude::*, soc::apple::rtkit, sync::Arc,
Expand Down Expand Up @@ -169,9 +169,15 @@
}
}
impl Inner {
/// Fail every packet that was never published. Their fences signal with the error.
/// Fail every packet not yet fully published. Their fences signal with the error. One that no
/// batch ever included releases its VM job guard first, so its VM isn't quarantined for work
/// the firmware never saw. One that may have been published keeps it: a partly published
/// packet, or one whose batch `fail_active` has already drained from `active`.
fn fail_pending(&mut self, error: Error) {
for (packet,_) in self.pending.drain_all() {packet.finish(Err(error));}
for (packet,_) in self.pending.drain_all() {
if policy::release_unpublished(packet.may_be_published()) {packet.release_guard();}
packet.finish(Err(error));
}
}
/// A published batch failed or never retired: latch the fault, log it, and fail its
/// packets. The packets stay retained (with their VM job guards) until teardown, because
Expand Down Expand Up @@ -374,6 +380,9 @@
for (p,_) in entries {
if !self.packets.iter().any(|q| Arc::ptr_eq(q,p)) {self.packets.push(p.clone(),GFP_KERNEL).map_err(|_|early(ENOMEM))?;}
}
// Before any producer state changes: an append the overlapping firmware consumes, or a
// partial publication, makes these packets visible to it.
for (p,_) in entries {p.mark_published();}
let found=self.jobs.iter().position(|job| matches!((job,control),
(NativeJob::Compute(_),crate::m3::m3_submit::Command::Compute(_)) |
(NativeJob::Render(_),crate::m3::m3_submit::Command::Render{..})));
Expand Down Expand Up @@ -519,6 +528,7 @@
// Keep the packet before any producer mutation. No other batch is live.
self.packets.clear();
self.packets.push(packet.clone(),GFP_KERNEL).map_err(|_|early(ENOMEM))?;
packet.mark_published();
let mut render_prepared=false;
let mut compute_prepared=false;
let prepared=(||->Result<usize> {
Expand Down Expand Up @@ -974,6 +984,8 @@
// Registration and Mutex initialization otherwise duplicates it on the bounded
// kernel stack. ManuallyDrop retains the whole allocation on failed ASC stop.
pub(crate) struct Runtime { inner: ManuallyDrop<KBox<Inner>> }
/// Claimed before this boot's first ASC start.
static FIRMWARE_START: policy::StartLatch = policy::StartLatch::new();
impl Runtime {
#[inline(never)]
pub(crate) fn new(pdev: &platform::Device<Core<'_>>, drm: &driver::AsahiDrmDevice, device: Device, contents: crate::m3::m3_adt_config::Contents) -> Result<Self> {
Expand All @@ -995,6 +1007,15 @@
if crate::m3::m3_adt_config::stop_before_asc(pdev.as_ref()) {
return Err(ENODEV);
}
// A second start (unbind and rebind, a module reload, a retry after a failed start)
// would set RUN while TTBAT context 0 still names the freed root of this one. Unloading
// is blocked once the latch is claimed, so a reload can't forget it.
if !FIRMWARE_START.claim() {
dev_err!(pdev.as_ref(), "M3: the GPU firmware already started once this boot and can't start again; reboot to start the GPU\n");
return Err(EBUSY);
}
// SAFETY: never released: the latch must outlive this instance of the module.
unsafe { kernel::bindings::__module_get(crate::THIS_MODULE.as_ptr()) };

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unconditional reference can be acquired after ordinary delete_module has already committed GOING. A racing first deferred/sysfs/async probe can then start, finish while unregister drains it, and unload; reloading loses this static latch. Use a fallible live-module pin before claiming/start, release only the extra reference when claim fails, and otherwise retain it permanently. The packet flag side looks sound.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A minimal replacement for the claim/pin block, preserving the existing fail-closed placement:

        // A deferred, async or sysfs probe may race an ordinary module removal.
        // Pin before claiming so a reload cannot forget an already-started firmware.
        // SAFETY: THIS_MODULE names this module, or is null when built in.
        if !unsafe { kernel::bindings::try_module_get(crate::THIS_MODULE.as_ptr()) } {
            dev_err!(pdev.as_ref(), "M3: the driver is being removed; GPU not started\n");
            return Err(ENODEV);
        }
        if !FIRMWARE_START.claim() {
            // SAFETY: release only this attempt's reference; the first start keeps its own.
            unsafe { kernel::bindings::module_put(crate::THIS_MODULE.as_ptr()) };
            dev_err!(pdev.as_ref(), "M3: the GPU firmware already started once this boot and can't start again; reboot to start the GPU\n");
            return Err(EBUSY);
        }
        // Keep the winning reference permanently so the latch lasts until reboot.

The unload interleaving model rejects both bad schedules with this ordering. This proposed replacement has not yet been natively compiled; the completed builds apply to the original6246 source. No branch changes from me.

@scottjones scottjones Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, thanks for catching this. Fixed in 80958cd5a4. The reference is now taken with try_module_get(THIS_MODULE) before the latch is claimed, and nothing is claimed or started if removal has begun. A start that loses the claim drops only its own reference, and the winner keeps its reference, so an ordinary unload stays blocked.

The order lives in the batch policy (claim_start), so the host test covers it:

  • a failed pin claims nothing;
  • a lost claim gives back its reference;
  • the winner keeps its reference.

Four mutations of that order each fail the test. The two commits you reviewed are unchanged.

// Whether the coprocessor runs and offers its endpoints, for the T8122 verdict.
let mut started=false;
// The adopted UAT and its window live outside the closure: once TTBAT context 0 names
Expand Down
9 changes: 8 additions & 1 deletion drivers/gpu/drm/asahi/m3/m3_submit.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT
//! Bounded M3 compute and graphics submissions through the common DRM scheduler/syncobjs.
use kernel::{c_str,dma_fence::*,drm::sched,prelude::*,sync::{Arc,Mutex},new_mutex,xarray};
Expand Down Expand Up @@ -43,6 +43,9 @@
pub(crate) wide_visibility:KVec<bool>,
completion:UserFence<Completion>,
finish_claimed: AtomicBool,
/// Set before a batch with any of this packet's commands can change firmware-visible state,
/// and never cleared.
published: AtomicBool,
/// Queue urgency (3 = UAPI realtime .. 0 = UAPI low): more urgent packets are published first.
pub(crate) urgency:u8,
/// Who submitted the packet, for the failure log only.
Expand Down Expand Up @@ -78,6 +81,10 @@
}
/// Called only after all published commands retired or before any publication.
pub(crate) fn release_guard(&self) {drop(self.vm_job.lock().take());}
/// Called before the first firmware-visible change of a batch that includes this packet.
pub(crate) fn mark_published(&self) {self.published.store(true,Ordering::Relaxed);}
/// Whether the firmware may have seen any of this packet's commands.
pub(crate) fn may_be_published(&self)->bool {self.published.load(Ordering::Relaxed)}
pub(crate) fn retired(&self) {self.release_guard();self.finish(Ok(()));}
pub(crate) fn finish(&self,result:Result) {
// Retirement, cancellation and a queued scheduler timeout can race.
Expand Down Expand Up @@ -232,7 +239,7 @@
parser.finish().map_err(|_|EINVAL)?;
let packet=Arc::new(Packet{vm:self.vm.clone(),commands,timestamps,attachments:flushes,wide_visibility:visibility,
completion:self.fences.new_fence(0,Completion)?.into(),
finish_claimed:AtomicBool::new(false),urgency:self.urgency,owner:Owner::current(),
finish_claimed:AtomicBool::new(false),published:AtomicBool::new(false),urgency:self.urgency,owner:Owner::current(),
vm_job:KBox::pin_init(new_mutex!(Some(vm_job)),GFP_KERNEL)?},GFP_KERNEL)?;
let mut job=self.entity.new_job(1,Job{shared:self.shared.clone(),packet})?;
for sync in syncs.drain(0..in_sync_count) {if let Some(f)=sync.fence {job.add_dependency(f)?;}}
Expand Down
27 changes: 27 additions & 0 deletions tools/asahi/m3-batching-tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT
//! Host tests of the production batch admission and lifetime policies.
//! Run: rustc --edition=2021 --test tools/asahi/m3-batching-tests.rs -o /tmp/m3-batching-tests
Expand Down Expand Up @@ -163,3 +163,30 @@
assert!(default_depth(true)<=SLOTS);
assert_eq!(MAX_PACKETS,8);
}

#[test]
fn firmware_starts_once_per_boot() {
let latch=StartLatch::new();
assert!(latch.claim());
// Unbind and rebind, a reload or a retry: every later start is refused.
for _ in 0..3 {assert!(!latch.claim());}
// Concurrent probes: exactly one claims it.
let latch=std::sync::Arc::new(StartLatch::new());
let claims:usize=(0..8).map(|_|{let l=latch.clone();std::thread::spawn(move||l.claim())})
.collect::<Vec<_>>().into_iter().map(|t|usize::from(t.join().unwrap())).sum();
assert_eq!(claims,1);
}

#[test]
fn failed_pending_packets_quarantine_only_what_the_firmware_may_have_seen() {
// (in a batch fail_active drained, partly published and still pending, never in a batch)
struct Packet {published:bool,guard:bool}
let mut active=vec![Packet{published:true,guard:true}];
let mut pending=vec![Packet{published:true,guard:true},Packet{published:false,guard:true}];
// fail_active drains active before fail_pending: a check against active can't see these.
let drained:Vec<_>=active.drain(..).collect();
for p in pending.iter_mut() {if release_unpublished(p.published) {p.guard=false;}}
assert!(drained[0].guard);
assert!(pending[0].guard,"a partly published packet keeps its guard");
assert!(!pending[1].guard,"a never-published packet releases its guard");
}