From cde874ee627d75331949bedfdd1c853dfccda797 Mon Sep 17 00:00:00 2001 From: Jonathan Goyvaerts Date: Sat, 21 Nov 2026 14:15:03 -0300 Subject: [PATCH] drm/asahi: g17: keep a queue's frontier off contiguous pages A logical queue keeps one frontier entry per command whose completion has not signalled yet, and the pruned entries of the current ioctl in `finished`. Both are KVecs, so they grow by krealloc() into physically contiguous memory. The frontier is bounded only by how much work userspace keeps queued: vkd3d-proton's test_resolve_image_exhaustive_descriptors records 64 x 1024 resolves in one command list, and the frontier reaches thousands of entries. Growing it then needs an order-5 or order-6 block (512 KiB to 1 MiB with 16K pages). On a fragmented machine (4.3 GB free, nothing above order 4) the allocation fails. If it is `finished`, prune_frontier() falls back and only warn_alloc() dumps a trace. If it is the frontier's own reserve, the submit fails with ENOMEM after some of its commands may already have been queued, so Mesa marks the queue lost and reports VK_ERROR_DEVICE_LOST: vkd3d_queue: page allocation failure: order:6, mode:0x40cc0(GFP_KERNEL|__GFP_COMP) ... krealloc_node_align_noprof+0x154/0x238 rust_helper_krealloc_node_align+0x10/0x20 ..._asahi_core3g175queue...14prune_frontier... asahi 28a600000.gpu: [File 119992 Queue 1]: IOCTL: submit failed! (submission ID: 38521603 err: ENOMEM) The same test reproduces it alone on a loaded machine (nothing free above order 3): a perf probe on kmem:kmalloc shows the frontier's reserve asking for 196608 bytes (2048 -> 4096 entries) and getting NULL, and the submit fails with ENOMEM. Nothing needs these arrays contiguous: only the ioctl reads them. Make both KVVecs. kvrealloc() still tries kmalloc first, for a large size without direct reclaim or a warning, and falls back to vmalloc. Small frontiers keep their kmalloc memory, and a deep one no longer depends on high-order pages. Reserve the frontier room for a whole submission before its output fences are published, instead of one entry per command as it is queued, so that an allocation failure fails the submission cleanly instead of half-way. Userspace decides how long the frontier gets, and it can now grow past what contiguous pages ever allowed, so charge it and `finished` to the caller's memory cgroup (GFP_KERNEL_ACCOUNT). Fixes: 52ef95b9e763 ("drm/asahi: add the T8140 firmware interface") Fixes: 39bdd5b8e2d5 ("drm/asahi: release pruned G17 frontier entries after the ioctl publishes") Signed-off-by: Jonathan Goyvaerts --- drivers/gpu/drm/asahi/g17/job.rs | 12 ++++++--- drivers/gpu/drm/asahi/g17/queue/mod.rs | 34 +++++++++++++++----------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/drivers/gpu/drm/asahi/g17/job.rs b/drivers/gpu/drm/asahi/g17/job.rs index b6536c07b443f..1707686b0531a 100644 --- a/drivers/gpu/drm/asahi/g17/job.rs +++ b/drivers/gpu/drm/asahi/g17/job.rs @@ -37,6 +37,13 @@ pub(crate) enum Engine { } impl Engine { + pub(crate) fn of(command: &Validated) -> Self { + match command { + Validated::Render { .. } => Self::Render, + Validated::Compute { .. } => Self::Compute, + } + } + pub(crate) fn index(self) -> usize { match self { Self::Render => 0, @@ -176,10 +183,7 @@ impl Packet { } pub(crate) fn engine(&self) -> Engine { - match &self.command { - Validated::Render { .. } => Engine::Render, - Validated::Compute { .. } => Engine::Compute, - } + Engine::of(&self.command) } /// Replay holds the device mutex. Its original inputs were satisfied before diff --git a/drivers/gpu/drm/asahi/g17/queue/mod.rs b/drivers/gpu/drm/asahi/g17/queue/mod.rs index d322c2710ab9e..292317bbca82e 100644 --- a/drivers/gpu/drm/asahi/g17/queue/mod.rs +++ b/drivers/gpu/drm/asahi/g17/queue/mod.rs @@ -100,10 +100,13 @@ pub(crate) struct Queue { variant: &'static crate::g17::fw::Variant, proof: Proof, sequence: [u64; 2], - frontiers: [KVec<(u64, Fences)>; 2], + /// One entry per unfinished command, so as long as userspace keeps work queued + /// (thousands under vkd3d-proton). Neither it nor `finished` needs + /// contiguous pages, and their growth must not fail a submit on fragmentation. + frontiers: [KVVec<(u64, Fences)>; 2], /// Finished frontier entries pruned during this ioctl. Their last references /// can tear a whole completion down; that waits until the ioctl has published. - finished: KVec, + finished: KVVec, failures: [FailureRecord; 2], // Closing the userspace queue releases admission even if firmware still // retains one of its execution contexts or installed physical graphs. @@ -152,8 +155,8 @@ impl Queue { variant, proof: Proof::new(), sequence: [0; 2], - frontiers: core::array::from_fn(|_| KVec::new()), - finished: KVec::new(), + frontiers: core::array::from_fn(|_| KVVec::new()), + finished: KVVec::new(), failures: [FailureRecord::new(), FailureRecord::new()], _slot: slot, }) @@ -179,7 +182,7 @@ impl Queue { // Without room the entries are released here, as they always could be. let mut finished = if self .finished - .reserve(self.frontiers[index].len(), GFP_KERNEL) + .reserve(self.frontiers[index].len(), GFP_KERNEL_ACCOUNT) .is_ok() { Some(&mut self.finished) @@ -213,10 +216,7 @@ impl Queue { inputs: &[file::SyncItem], destinations: Destinations, ) -> Result { - let engine = match &command { - Validated::Render { .. } => Engine::Render, - Validated::Compute { .. } => Engine::Compute, - }; + let engine = Engine::of(&command); let index = engine.index(); if let Validated::Render { ray_query, .. } = &command { let blocks = self @@ -227,7 +227,6 @@ impl Queue { } } self.prune_frontier(index); - self.frontiers[index].reserve(1, GFP_KERNEL)?; if engine == Engine::Compute { self.ensure_compute(&command)?; } @@ -475,10 +474,7 @@ impl Queue { &command.payload, ) .map_err(|error| refused("the command's requirements", error))?; - let engine = match &payload { - Validated::Render { .. } => Engine::Render, - Validated::Compute { .. } => Engine::Compute, - }; + let engine = Engine::of(&payload); if !self.backend.supports_engine(engine) { return Err(ENOTSUPP); } self.backend .prepare_command(&mut payload) @@ -596,6 +592,16 @@ impl crate::queue::Queue for Queue { command.allow_long_job(); } } + // Room for every command's frontier entry, so that running out of memory fails + // the submission before any of it is queued. Userspace sets the frontier's + // length, so its memory is charged to the caller. + let mut room = [0; 2]; + for (command, _, _) in pending.iter() { + room[Engine::of(command).index()] += 1; + } + for (frontier, room) in self.frontiers.iter_mut().zip(room) { + frontier.reserve(room, GFP_KERNEL_ACCOUNT)?; + } let mut guards = guards.into_iter(); // Where a firmware wait reports completion before a weak stream tail's writes are // visible, only a command that declares a visible completion is waited for there.