diff --git a/drivers/gpu/drm/asahi/g17/fence.rs b/drivers/gpu/drm/asahi/g17/fence.rs index 4c437599f53c..ce39b39c0d39 100644 --- a/drivers/gpu/drm/asahi/g17/fence.rs +++ b/drivers/gpu/drm/asahi/g17/fence.rs @@ -994,6 +994,8 @@ mod tests { let mut restart_outputs = Outputs::new(&contexts, restart_syncs, 0, &allocation_failure)?; let aggregate = restart_outputs.publish_with(None, false, false)?; let member = aggregate.member(); + // Queue::submit() installs the outputs once the first command is queued. + restart_outputs.install(); let point2 = syncobj.fence_get().unwrap().chain_find_seqno(2)?.unwrap(); assert_eq!(super::super::job::fence_status(&point2), 0); // SAFETY: `point2` owns a live dma_fence reference for this zero-timeout poll. @@ -1071,7 +1073,8 @@ mod tests { let s1 = independent(&contexts)?; let items1 = [cumulative_item(anchor.clone(), 1), binary_item(out.clone())]; let mut b1 = super::super::sync_output::SignalBundle::new(&items1, 0)?; - b1.publish(&Fence::from_fence(&s1), true)?; + b1.prepare(&Fence::from_fence(&s1), true)?; + b1.commit(); let scope1 = out.fence_get().unwrap(); // SAFETY: `scope1` owns a live dma_fence reference. assert!(unsafe { asahi_g17_is_scope(scope1.raw()) }); @@ -1085,7 +1088,8 @@ mod tests { let s2 = independent(&contexts)?; let items2 = [cumulative_item(anchor.clone(), 2), binary_item(out.clone())]; let mut b2 = super::super::sync_output::SignalBundle::new(&items2, 0)?; - b2.publish(&Fence::from_fence(&s2), true)?; + b2.prepare(&Fence::from_fence(&s2), true)?; + b2.commit(); let scope2 = out.fence_get().unwrap(); // SAFETY: `scope2` owns a live dma_fence reference. assert!(unsafe { asahi_g17_is_scope(scope2.raw()) }); @@ -1109,7 +1113,8 @@ mod tests { let s3 = independent(&contexts)?; let items3 = [cumulative_item(anchor.clone(), 3), binary_item(out.clone())]; let mut b3 = super::super::sync_output::SignalBundle::new(&items3, 0)?; - b3.publish(&Fence::from_fence(&s3), true)?; + b3.prepare(&Fence::from_fence(&s3), true)?; + b3.commit(); let scope3 = out.fence_get().unwrap(); // SAFETY: `scope3` owns a live dma_fence reference. assert!(unsafe { asahi_g17_is_scope(scope3.raw()) }); @@ -1173,6 +1178,7 @@ pub(crate) struct Outputs { chunks: KVec>, result: Result, cumulative: Option, + /// The submission's fence is in the outputs ([`Self::install`]). installed: bool, } impl Outputs { @@ -1215,6 +1221,23 @@ impl Outputs { pub(crate) fn published(&self) -> bool { self.installed } + /// Stores the submission's fence, created by [`Self::publish`] or + /// [`Self::publish_with_status`], in every output. Cannot fail. + pub(crate) fn install(&mut self) { + let Some(submission) = &self.submission else { + return; + }; + if self.installed { + return; + } + self.installed = true; + if let Some(bundle) = &mut self.cumulative { + bundle.commit(); + self.syncs.truncate(self.input_count); + } else { + install(self.syncs.drain(self.input_count..), &submission.fence()); + } + } pub(crate) fn inputs(&self) -> &[SyncItem] { &self.syncs[..self.input_count] } @@ -1262,13 +1285,10 @@ impl Outputs { fence.compute_only = compute_only; let submission = Aggregate(fence.into()); self.submission = Some(submission.clone()); + // Outputs get the fence only at `install()`, once the commands are queued. if let Some(bundle) = &mut self.cumulative { - bundle.publish(&submission.fence(), compute_only)?; - self.syncs.truncate(self.input_count); - } else { - install(self.syncs.drain(self.input_count..), &submission.fence()); + bundle.prepare(&submission.fence(), compute_only)?; } - self.installed = true; Ok(submission) } pub(crate) fn set_result(&mut self, result: Result) { @@ -1280,8 +1300,16 @@ impl Drop for Outputs { for chunk in &self.chunks { chunk.finish_enqueue(self.result); } - if let Some(submission) = self.submission.take() { - if !untouched_refusal(self.installed, &self.result) { + if self.submission.is_some() { + if !self.installed && !untouched_refusal(false, &self.result) { + self.install(); + } + let Some(submission) = self.submission.take() else { + return; + }; + // A prepared cumulative signal that could not be committed falls back to the + // submission's own fence; this does nothing once committed. + if self.installed { if let Some(bundle) = &mut self.cumulative { bundle.fail(&submission.fence()); } diff --git a/drivers/gpu/drm/asahi/g17/queue/mod.rs b/drivers/gpu/drm/asahi/g17/queue/mod.rs index 0fa26c381213..42f43977069c 100644 --- a/drivers/gpu/drm/asahi/g17/queue/mod.rs +++ b/drivers/gpu/drm/asahi/g17/queue/mod.rs @@ -502,10 +502,14 @@ impl Queue { fences.timestamp_waits = inherited_timestamp_waits; fences.producers = producer_check; self.sequence[index] = order.sequence; + // The command is queued: from here on no failure may read as a refusal (the room was + // reserved before the first command, so these cannot fail). if fences.publishes() { - self.publishers[index].push_within_capacity(order.sequence, fences.clone())?; + self.publishers[index] + .push_within_capacity(order.sequence, fences.clone()) + .map_err(|_| EIO)?; } - self.frontiers[index].push_within_capacity(order.sequence, fences)?; + self.frontiers[index].push_within_capacity(order.sequence, fences).map_err(|_| EIO)?; Ok(()) } /// Finish parsing, validation and VM pinning before entering publication, into the @@ -664,6 +668,8 @@ impl crate::queue::Queue for Queue { } }; let mut scratch = core::mem::take(&mut self.scratch); + // Commands queued so far; until the first is, nothing of the submission is visible. + let mut enqueued = 0usize; let result = (|| { if !self.backend.supports_submission() { return Err(ENOTSUPP); @@ -779,16 +785,6 @@ impl crate::queue::Queue for Queue { } outputs.publish(work, firmware_source, compute_only)? }; - let output_fence = aggregate.fence(); - super::trace::submit( - 1, - id, - self.context.id(), - input_count, - output_count, - 0, - Some(&output_fence), - ); let shared_inputs = Inputs::new(outputs.inputs().iter().filter_map(|sync| sync.fence.as_ref()))?; let mut work_states = work_states.into_iter(); @@ -804,9 +800,27 @@ impl crate::queue::Queue for Queue { shared_inputs.as_ref(), batch_renders, )?; + enqueued += 1; } Ok(()) })(); + // Queued commands need their outputs to carry the submission's fence. A refusal before + // the first one was queued leaves them untouched. + if !super::fence::untouched_refusal(enqueued != 0, &result) { + outputs.install(); + if outputs.published() { + let output_fence = outputs.output_fence(); + super::trace::submit( + 1, + id, + self.context.id(), + input_count, + output_count, + 0, + output_fence.as_ref(), + ); + } + } // What an error left unsubmitted is released here, as before the scratch was kept. scratch.pending.clear(); scratch.guards.clear(); diff --git a/drivers/gpu/drm/asahi/g17/sync_output.c b/drivers/gpu/drm/asahi/g17/sync_output.c index 25ef7ec29896..13d2771ed3b9 100644 --- a/drivers/gpu/drm/asahi/g17/sync_output.c +++ b/drivers/gpu/drm/asahi/g17/sync_output.c @@ -16,6 +16,8 @@ struct asahi_g17_signal_bundle { size_t count; size_t anchor; bool published; + /* The anchor's scope, prepared and not committed yet. */ + struct dma_fence *scope; struct asahi_g17_signal_target targets[]; }; @@ -25,6 +27,7 @@ void asahi_g17_signals_free(struct asahi_g17_signal_bundle *bundle) if (!bundle) return; + dma_fence_put(bundle->scope); for (i = 0; i < bundle->count; ++i) { struct asahi_g17_signal_target *target = &bundle->targets[i]; if (target->chain) @@ -95,21 +98,36 @@ static void asahi_g17_signal_install(struct asahi_g17_signal_target *target, } } -int asahi_g17_signals_publish(struct asahi_g17_signal_bundle *bundle, +/* + * Allocate the anchor's scope over its current fence and @submitted. Nothing is + * stored in a sync object until asahi_g17_signals_commit(), so a failure here, or a + * bundle freed after it, leaves every output as it was. + */ +int asahi_g17_signals_prepare(struct asahi_g17_signal_bundle *bundle, struct dma_fence *submitted, bool compute_only) { - struct dma_fence *previous, *scope; - size_t i; + struct dma_fence *previous; - if (bundle->published) + if (bundle->published || bundle->scope) return -EINVAL; previous = drm_syncobj_fence_get(bundle->targets[bundle->anchor].syncobj); - scope = asahi_g17_scope_create(previous, submitted, - bundle->targets[bundle->anchor].point, - compute_only); + bundle->scope = asahi_g17_scope_create(previous, submitted, + bundle->targets[bundle->anchor].point, + compute_only); dma_fence_put(previous); - if (!scope) - return -ENOMEM; + return bundle->scope ? 0 : -ENOMEM; +} + +/* Install the prepared scope in every output. Cannot fail. */ +void asahi_g17_signals_commit(struct asahi_g17_signal_bundle *bundle) +{ + struct dma_fence *previous, *scope = bundle->scope; + size_t i; + + if (bundle->published || !scope) + return; + bundle->scope = NULL; + asahi_g17_scope_arm(scope); /* Only exported completion includes earlier work. GPU execution gets no new wait. */ asahi_g17_signal_install(&bundle->targets[bundle->anchor], scope); previous = drm_syncobj_fence_get(bundle->targets[bundle->anchor].syncobj); @@ -120,7 +138,6 @@ int asahi_g17_signals_publish(struct asahi_g17_signal_bundle *bundle, asahi_g17_signal_install(&bundle->targets[i], scope); bundle->published = true; dma_fence_put(scope); - return 0; } /* Allocation or validation failed before enqueue: install an error promise without allocating. */ diff --git a/drivers/gpu/drm/asahi/g17/sync_output.h b/drivers/gpu/drm/asahi/g17/sync_output.h index ad8edca47c60..8f7525f17c3d 100644 --- a/drivers/gpu/drm/asahi/g17/sync_output.h +++ b/drivers/gpu/drm/asahi/g17/sync_output.h @@ -16,8 +16,9 @@ struct asahi_g17_signal_bundle * asahi_g17_signals_new(const struct asahi_g17_signal_spec *specs, size_t count, size_t anchor, int *error); void asahi_g17_signals_free(struct asahi_g17_signal_bundle *bundle); -int asahi_g17_signals_publish(struct asahi_g17_signal_bundle *bundle, +int asahi_g17_signals_prepare(struct asahi_g17_signal_bundle *bundle, struct dma_fence *submitted, bool compute_only); +void asahi_g17_signals_commit(struct asahi_g17_signal_bundle *bundle); void asahi_g17_signals_fail(struct asahi_g17_signal_bundle *bundle, struct dma_fence *failure); #ifdef CONFIG_KUNIT diff --git a/drivers/gpu/drm/asahi/g17/sync_output.rs b/drivers/gpu/drm/asahi/g17/sync_output.rs index 649762ab8665..fb62a1514c9b 100644 --- a/drivers/gpu/drm/asahi/g17/sync_output.rs +++ b/drivers/gpu/drm/asahi/g17/sync_output.rs @@ -12,8 +12,9 @@ unsafe extern "C" { fn asahi_g17_signals_new(syncs: *const SignalSpec, count: usize, anchor: usize, error: *mut i32) -> *mut core::ffi::c_void; fn asahi_g17_signals_free(bundle: *mut core::ffi::c_void); - fn asahi_g17_signals_publish(bundle: *mut core::ffi::c_void, + fn asahi_g17_signals_prepare(bundle: *mut core::ffi::c_void, current: *mut bindings::dma_fence, compute_only: bool) -> i32; + fn asahi_g17_signals_commit(bundle: *mut core::ffi::c_void); fn asahi_g17_signals_fail(bundle: *mut core::ffi::c_void, failure: *mut bindings::dma_fence); } @@ -74,13 +75,29 @@ impl SignalBundle { Ok(Self(raw)) } - pub(crate) fn publish(&mut self, current: &Fence, compute_only: bool) -> Result { + /// Allocates what publishing `current` needs, storing nothing in an output yet. + pub(crate) fn prepare(&mut self, current: &Fence, compute_only: bool) -> Result { // SAFETY: The bundle and current fence are owned for this call; C takes - // its own references on publication and reports failure before any store. - let error = unsafe { asahi_g17_signals_publish(self.0, current.raw(), compute_only) }; + // its own references and reports failure before any store. + let error = unsafe { asahi_g17_signals_prepare(self.0, current.raw(), compute_only) }; if error == 0 { Ok(()) } else { Err(Error::from_errno(error)) } } + /// [`Self::prepare`] and [`Self::commit`] at once: fails before any store. G16 publishes + /// this way; G17 commits only once its commands are queued. + #[cfg(CONFIG_DRM_ASAHI_G16)] + pub(crate) fn publish(&mut self, current: &Fence, compute_only: bool) -> Result { + self.prepare(current, compute_only)?; + self.commit(); + Ok(()) + } + + /// Publishes what [`Self::prepare`] allocated in every output. + pub(crate) fn commit(&mut self) { + // SAFETY: The bundle is owned; C stores only what prepare allocated. + unsafe { asahi_g17_signals_commit(self.0) }; + } + pub(crate) fn fail(&mut self, failure: &Fence) { // SAFETY: Live owned arguments; this only consumes uninstalled chain nodes. unsafe { asahi_g17_signals_fail(self.0, failure.raw()) }; diff --git a/drivers/gpu/drm/asahi/g17/sync_scope.c b/drivers/gpu/drm/asahi/g17/sync_scope.c index 0b3bc8edd970..63fb23d600cd 100644 --- a/drivers/gpu/drm/asahi/g17/sync_scope.c +++ b/drivers/gpu/drm/asahi/g17/sync_scope.c @@ -336,7 +336,6 @@ struct dma_fence *asahi_g17_scope_create(struct dma_fence *previous, struct dma_fence *parent = NULL; struct asahi_g17_scope *scope, *prior = NULL; u64 context; - unsigned int i; if (asahi_g17_is_scope(contained)) { prior = to_scope(contained); @@ -380,7 +379,17 @@ struct dma_fence *asahi_g17_scope_create(struct dma_fence *previous, /* The constructor guard prevents completion while callbacks are being installed. */ atomic_set(&scope->pending, parent ? 3 : 2); scope->base.error = 1; - for (i = parent ? 0 : 1; i < ARRAY_SIZE(scope->children); ++i) { + return &scope->base; +} + +/* The bundle owns the only reference until commit. */ +void asahi_g17_scope_arm(struct dma_fence *fence) +{ + struct asahi_g17_scope *scope = to_scope(fence); + unsigned int i; + + /* children[0] is NULL without a previous point, or when it already succeeded. */ + for (i = scope->children[0] ? 0 : 1; i < ARRAY_SIZE(scope->children); ++i) { struct dma_fence *child = scope->children[i]; int err; @@ -398,5 +407,4 @@ struct dma_fence *asahi_g17_scope_create(struct dma_fence *previous, } } scope_arrived(scope, 0); - return &scope->base; } diff --git a/drivers/gpu/drm/asahi/g17/sync_scope.h b/drivers/gpu/drm/asahi/g17/sync_scope.h index 25b9acb8d714..a442af0d25a2 100644 --- a/drivers/gpu/drm/asahi/g17/sync_scope.h +++ b/drivers/gpu/drm/asahi/g17/sync_scope.h @@ -15,6 +15,7 @@ int asahi_g17_completion_fence_status(struct dma_fence *fence); struct dma_fence *asahi_g17_scope_create(struct dma_fence *previous, struct dma_fence *submitted, u64 point, bool compute_only); +void asahi_g17_scope_arm(struct dma_fence *scope); void asahi_g17_scope_bind(struct dma_fence *scope, struct dma_fence *root); bool asahi_g17_is_scope(struct dma_fence *fence); /* Returns owned child references, or zero once the scope is signalled. */ diff --git a/tools/asahi/g17-submit-refusal-untouched-tests.py b/tools/asahi/g17-submit-refusal-untouched-tests.py index b7912adca0ad..d6b8f8deca47 100644 --- a/tools/asahi/g17-submit-refusal-untouched-tests.py +++ b/tools/asahi/g17-submit-refusal-untouched-tests.py @@ -12,12 +12,19 @@ - G17's submit reports no allocation_failure on a refusal, and passes its result through submit_result, so a late ENOMEM/ENOSPC is EIO; - drm_asahi_submit::flags stays MBZ; -- only G17 reports DRM_ASAHI_FEATURE_SUBMIT_REFUSAL_UNTOUCHED. +- only G17 reports DRM_ASAHI_FEATURE_SUBMIT_REFUSAL_UNTOUCHED; +- the submission's fence reaches the outputs only after the enqueue loop + (Outputs::install), and is left out only while no command was queued; +- creating the submission stores nothing in an output: the cumulative signal + is prepared there and committed by install; +- a failure after a command was queued is never ENOMEM. --mutation new-installs / drop-installs / no-errno-map / flags-accepted / -not-reported inject each fault and must be caught. +not-reported / install-early / ignore-queued / queued-enomem inject each fault +and must be caught. """ import argparse +import re import sys from pathlib import Path @@ -35,7 +42,7 @@ def item(source, signature): def sources(mutation): - files = {n: (ASAHI / n).read_text() for n in ('g17/fence.rs', 'g17/queue/mod.rs', 'file.rs', 'g17/mod.rs')} + files = {n: (ASAHI / n).read_text() for n in ('g17/fence.rs', 'g17/queue/mod.rs', 'file.rs', 'g17/mod.rs', 'g17/sync_scope.c', 'g17/sync_output.c')} if mutation == 'new-installs': files['g17/fence.rs'] = files['g17/fence.rs'].replace( 'if untouched_refusal(false, &Err(error)) {', 'if false {', 1) @@ -47,6 +54,17 @@ def sources(mutation): 'let result = super::fence::submit_result(outputs.published(), result);', '', 1) elif mutation == 'flags-accepted': files['file.rs'] = files['file.rs'].replace('if data.flags != 0 || data.pad != 0 {', 'if data.pad != 0 {', 1) + elif mutation == 'install-early': + files['g17/fence.rs'] = files['g17/fence.rs'].replace( + 'bundle.prepare(&submission.fence(), compute_only)?;\n }\n Ok(submission)', + 'bundle.prepare(&submission.fence(), compute_only)?;\n }\n self.install();\n Ok(submission)', 1) + elif mutation == 'ignore-queued': + files['g17/queue/mod.rs'] = files['g17/queue/mod.rs'].replace( + 'untouched_refusal(enqueued != 0, &result)', 'untouched_refusal(false, &result)', 1) + elif mutation == 'queued-enomem': + files['g17/queue/mod.rs'] = files['g17/queue/mod.rs'].replace( + '.push_within_capacity(order.sequence, fences).map_err(|_| EIO)?;', + '.push_within_capacity(order.sequence, fences)?;', 1) elif mutation == 'not-reported': files['g17/mod.rs'] = files['g17/mod.rs'].replace( '| uapi::DRM_ASAHI_FEATURE_SUBMIT_REFUSAL_UNTOUCHED as u64\n', '', 1) @@ -72,23 +90,49 @@ def check(files): publish = item(fence, 'fn publish_source(') state = item(fence, 'pub(crate) fn published(') if ('self.installed' not in state - or not 0 <= publish.find('bundle.publish(') < publish.find('self.installed = true;') - or 'if !untouched_refusal(self.installed, &self.result)' not in drop): + or 'bundle.prepare(' not in publish or 'self.installed = true;' in publish + or 'if !self.installed && !untouched_refusal(false, &self.result)' not in drop): failures.append('FAIL: cumulative allocation failure publishes the private aggregate') if 'if super::fence::untouched_refusal(outputs.published(), &result)' not in submit: failures.append('FAIL: refusal trace exposes a private fence') + scope = files['g17/sync_scope.c'] + create = item(scope, 'struct dma_fence *asahi_g17_scope_create(') + signals = files['g17/sync_output.c'] + commit = item(signals, 'void asahi_g17_signals_commit(') + free = item(signals, 'void asahi_g17_signals_free(') + if ('dma_fence_add_callback' in create or 'asahi_g17_scope_arm(' in create + or not 0 <= commit.find('asahi_g17_scope_arm(scope)') < commit.find('asahi_g17_signal_install') + or 'dma_fence_put(bundle->scope)' not in free): + failures.append('FAIL: an abandoned scope keeps callback or child references') ioctl = item(files['file.rs'], 'pub(crate) fn submit(\n device: &AsahiDrmDevice,') if 'if data.flags != 0 || data.pad != 0 {' not in ioctl: failures.append('FAIL line 5: drm_asahi_submit::flags is not MBZ') if 'DRM_ASAHI_FEATURE_SUBMIT_REFUSAL_UNTOUCHED' not in item(files['g17/mod.rs'], 'fn get_params('): failures.append('FAIL line 6: G17 does not report the feature') + publish = '\n'.join(l.split('//')[0] for l in item(fence, 'fn publish_source(').split('\n')) + if 'install(' in publish or 'commit(' in publish: + failures.append('FAIL line 7: creating the submission stores it in the outputs') + loop_end = submit.find('enqueued += 1;') + gate = submit.find('if !super::fence::untouched_refusal(enqueued != 0, &result) {\n outputs.install();') + if loop_end < 0 or gate < loop_end: + failures.append('FAIL line 8: the outputs are not installed after the loop, gated on queued commands') + install_trace = re.compile(r'super::trace::submit\(\s*1,') + trace_sites = list(install_trace.finditer(submit)) + published = 'if outputs.published() {' + if (len(trace_sites) != 1 or gate < 0 or published not in submit[gate:] + or not install_trace.search(item(submit[gate:], published))): + failures.append('FAIL: the installation trace must follow publication') + enqueue = item(queue, 'fn enqueue(') + if enqueue.count('.map_err(|_| EIO)?;') < 2: + failures.append('FAIL line 9: a failure after a command was queued can read as ENOMEM') return failures def main(): parser = argparse.ArgumentParser() parser.add_argument('--mutation', choices=['new-installs', 'drop-installs', 'no-errno-map', - 'flags-accepted', 'not-reported']) + 'flags-accepted', 'not-reported', 'install-early', + 'ignore-queued', 'queued-enomem']) args = parser.parse_args() failures = check(sources(args.mutation)) for f in failures: diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index d821f8a040ce..ef60aa3f7c19 100644 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -675,7 +675,7 @@ def mutations(flag, names, base, expect=KILL): 'unlocked-look-check', 'refuse', 'mem-pct-refuses'], []), 'g17-submit-refusal-untouched-tests.py': [([], PASS)] + mutations( '--mutation', ['new-installs', 'drop-installs', 'no-errno-map', 'flags-accepted', - 'not-reported'], []), + 'not-reported', 'install-early', 'ignore-queued', 'queued-enomem'], []), 'g17-t6041-initdata-tests.py': [([], PASS)], 'g17-uninstalled-cleanup-tests.py': [([], PASS)], 'g17-unpublished-tests.py': [([], PASS)],