From fad5d04a1fa8aecff5cd45e1910b7aa88e9f1761 Mon Sep 17 00:00:00 2001 From: Jonathan Goyvaerts Date: Sun, 22 Nov 2026 17:18:02 -0300 Subject: [PATCH 1/2] drm/asahi: keep a submission's syncs and command copy off contiguous pages The submit ioctl copies the command buffer from userspace into a KVec, and parses the in and out syncs into another one. Userspace sets both sizes: in_sync_count and out_sync_count are u32s with no limit of their own, and only some GPUs bound the command buffer before it is copied (G17's max_cmdbuf_size() is the default, usize::MAX). kmalloc() needs both physically contiguous, so a large submission asks for a high-order block on every ioctl, which without compaction may end in the OOM killer instead of failing. Nothing needs them contiguous: only the ioctl reads them, and every generation's submit consumes the syncs before it returns. Make both KVVecs, so a large one falls back to vmalloc() while small ones keep their kmalloc memory, and charge them to the caller's memory cgroup (GFP_KERNEL_ACCOUNT). The sync list's type changes to KVVec in every generation's submit; nothing else about it does. The asahi-m4 submit-restart selftest, which builds that code on the host, gets the matching KVVec alias. Signed-off-by: Jonathan Goyvaerts Signed-off-by: Dj <43033058+DjDeveloperr@users.noreply.github.com> --- drivers/gpu/drm/asahi/file.rs | 20 ++++++++++--------- drivers/gpu/drm/asahi/g15/fence.rs | 4 ++-- drivers/gpu/drm/asahi/g15/queue.rs | 2 +- drivers/gpu/drm/asahi/g16/fence.rs | 4 ++-- drivers/gpu/drm/asahi/g16/queue.rs | 2 +- drivers/gpu/drm/asahi/g17/fence.rs | 4 ++-- drivers/gpu/drm/asahi/g17/queue/mod.rs | 2 +- drivers/gpu/drm/asahi/g18/drm.rs | 4 ++-- drivers/gpu/drm/asahi/m3/m3_submit.rs | 11 ++++++++-- drivers/gpu/drm/asahi/m5/drm.rs | 4 ++-- drivers/gpu/drm/asahi/queue/mod.rs | 4 ++-- .../gpu/asahi-m4/test-fence-signalling.py | 2 +- .../gpu/asahi-m4/test-submit-restart.py | 2 +- 13 files changed, 37 insertions(+), 28 deletions(-) diff --git a/drivers/gpu/drm/asahi/file.rs b/drivers/gpu/drm/asahi/file.rs index be78c01b39aff..abc3bd4426384 100644 --- a/drivers/gpu/drm/asahi/file.rs +++ b/drivers/gpu/drm/asahi/file.rs @@ -291,13 +291,15 @@ impl SyncItem { in_count: u32, out_count: u32, allow_cumulative: bool, - ) -> Result> { + ) -> Result> { let count = in_count.checked_add(out_count).ok_or(EOVERFLOW)?; const STRIDE: usize = core::mem::size_of::(); const CHUNK: usize = 8; let size = STRIDE.checked_mul(count as usize).ok_or(EOVERFLOW)?; - let mut vec = KVec::with_capacity(count as usize, GFP_KERNEL)?; + // Userspace sets the count. Charge the allocation to the caller and + // allow noncontiguous backing pages. + let mut vec = KVVec::with_capacity(count as usize, GFP_KERNEL_ACCOUNT)?; let mut anchor = None; // Read each record once in small stack chunks so validation and use see the @@ -330,7 +332,7 @@ impl SyncItem { } vec.push( Self::parse_one(file, sync, i >= in_count, allow_cumulative)?, - GFP_KERNEL, + GFP_KERNEL_ACCOUNT, )?; } base += chunk as u32; @@ -364,7 +366,7 @@ pub(crate) struct TableQueue { queue: KBox, /// Reusable staging buffer for `SUBMIT` command buffers, avoiding per-submit heap /// allocations and keeping large command buffers off the kernel stack. - cmdbuf: KVec, + cmdbuf: KVVec, _share: Option, } @@ -374,13 +376,13 @@ impl TableQueue { /// Takes the cached command-buffer staging allocation so userspace copies run outside the /// queue mutex (preventing `queue.lock()` -> `mmap_lock` lock-order inversion). - fn take_cmdbuf(&mut self) -> KVec { + fn take_cmdbuf(&mut self) -> KVVec { core::mem::take(&mut self.cmdbuf) } /// Returns a cleared command-buffer allocation to the per-queue cache when within the /// reuse capacity cap; oversized buffers are dropped. - fn return_cmdbuf(&mut self, mut buf: KVec) { + fn return_cmdbuf(&mut self, mut buf: KVVec) { buf.clear(); if buf.capacity() > 0 && buf.capacity() <= Self::REUSE_CMDBUF_CAP @@ -391,11 +393,11 @@ impl TableQueue { } } -fn copy_cmdbuf_from_user(buf: &mut KVec, addr: u64, size: usize) -> Result { +fn copy_cmdbuf_from_user(buf: &mut KVVec, addr: u64, size: usize) -> Result { buf.clear(); UserSlice::new(UserPtr::from_addr(addr as usize), size) .reader() - .read_all(buf, GFP_KERNEL) + .read_all(buf, GFP_KERNEL_ACCOUNT) } impl Deref for TableQueue { @@ -1799,7 +1801,7 @@ impl File { resv.fill(Arc::pin_init( new_mutex!(TableQueue { queue, - cmdbuf: KVec::new(), + cmdbuf: KVVec::new(), _share: share, }), GFP_KERNEL, diff --git a/drivers/gpu/drm/asahi/g15/fence.rs b/drivers/gpu/drm/asahi/g15/fence.rs index bb85764228070..55a85472a794d 100644 --- a/drivers/gpu/drm/asahi/g15/fence.rs +++ b/drivers/gpu/drm/asahi/g15/fence.rs @@ -179,12 +179,12 @@ fn install(outputs: impl Iterator, fence: &Fence) { /// accepted, a signalled error fence when the submission is refused (the shared `file.rs` /// contract of `tracked_submission`). pub(crate) struct Syncs { - syncs: KVec, + syncs: KVVec, input_count: usize, } impl Syncs { - pub(crate) fn new(syncs: KVec, input_count: usize) -> Self { + pub(crate) fn new(syncs: KVVec, input_count: usize) -> Self { Self { syncs, input_count } } diff --git a/drivers/gpu/drm/asahi/g15/queue.rs b/drivers/gpu/drm/asahi/g15/queue.rs index 03a8f46c8a0fc..3bdebb7995e44 100644 --- a/drivers/gpu/drm/asahi/g15/queue.rs +++ b/drivers/gpu/drm/asahi/g15/queue.rs @@ -212,7 +212,7 @@ impl crate::queue::Queue for Queue { &mut self, _reg_data: &crate::driver::AsahiRegData<'_>, id: u64, - syncs: KVec, + syncs: KVVec, in_sync_count: usize, cmdbuf_raw: &[u8], objects: Pin<&xarray::XArray>>, diff --git a/drivers/gpu/drm/asahi/g16/fence.rs b/drivers/gpu/drm/asahi/g16/fence.rs index 75ca2b6768c81..3d7e57f4744c3 100644 --- a/drivers/gpu/drm/asahi/g16/fence.rs +++ b/drivers/gpu/drm/asahi/g16/fence.rs @@ -562,13 +562,13 @@ fn install(outputs: impl Iterator, fence: &Fence) { /// contract of `tracked_submission`). Cumulative output bundles reuse the shared /// `g17::sync_output::SignalBundle` scope chain. pub(crate) struct Syncs { - syncs: KVec, + syncs: KVVec, input_count: usize, cumulative: Option, } impl Syncs { - pub(crate) fn new(mut syncs: KVec, input_count: usize) -> Self { + pub(crate) fn new(mut syncs: KVVec, input_count: usize) -> Self { let cumulative = syncs.iter_mut().find_map(|sync| sync.cumulative.take()); Self { syncs, diff --git a/drivers/gpu/drm/asahi/g16/queue.rs b/drivers/gpu/drm/asahi/g16/queue.rs index 28332f8de5e74..d64d57125b141 100644 --- a/drivers/gpu/drm/asahi/g16/queue.rs +++ b/drivers/gpu/drm/asahi/g16/queue.rs @@ -372,7 +372,7 @@ impl crate::queue::Queue for Queue { &mut self, _reg_data: &crate::driver::AsahiRegData<'_>, id: u64, - syncs: KVec, + syncs: KVVec, in_sync_count: usize, cmdbuf_raw: &[u8], objects: Pin<&xarray::XArray>>, diff --git a/drivers/gpu/drm/asahi/g17/fence.rs b/drivers/gpu/drm/asahi/g17/fence.rs index 04df5e4aa9fd6..1eaafeb443852 100644 --- a/drivers/gpu/drm/asahi/g17/fence.rs +++ b/drivers/gpu/drm/asahi/g17/fence.rs @@ -526,7 +526,7 @@ fn install(outputs: impl Iterator, fence: &Fence) { pub(crate) struct Outputs { fallback: Option>, - syncs: KVec, + syncs: KVVec, input_count: usize, submission: Option, result: Result, @@ -535,7 +535,7 @@ pub(crate) struct Outputs { impl Outputs { pub(crate) fn new( contexts: &FenceContexts, - mut syncs: KVec, + mut syncs: KVVec, input_count: usize, allocation_failure: &Fence, ) -> Result { diff --git a/drivers/gpu/drm/asahi/g17/queue/mod.rs b/drivers/gpu/drm/asahi/g17/queue/mod.rs index 1c64f3839ae22..989907521d488 100644 --- a/drivers/gpu/drm/asahi/g17/queue/mod.rs +++ b/drivers/gpu/drm/asahi/g17/queue/mod.rs @@ -519,7 +519,7 @@ impl crate::queue::Queue for Queue { &mut self, _reg_data: &crate::driver::AsahiRegData<'_>, id: u64, - syncs: KVec, + syncs: KVVec, input_count: usize, bytes: &[u8], objects: Pin<&xarray::XArray>>, diff --git a/drivers/gpu/drm/asahi/g18/drm.rs b/drivers/gpu/drm/asahi/g18/drm.rs index 14a726b24fc4c..f8c4e70574d51 100644 --- a/drivers/gpu/drm/asahi/g18/drm.rs +++ b/drivers/gpu/drm/asahi/g18/drm.rs @@ -286,7 +286,7 @@ impl FenceOps for G18SubmissionFence { /// Dropped unaccepted, it signals the fence with `result` and installs it. struct OutputPublication { fence: Option>, - syncs: KVec, + syncs: KVVec, input_count: usize, result: Result, } @@ -707,7 +707,7 @@ impl queue::Queue for G18Queue { /// userspace nothing to wait on (Mesa's G18G split compute prepass reads /// GPU results after such submissions). fn submit(&mut self, _reg_data: &AsahiRegData<'_>, submission_id: u64, - syncs: KVec, + syncs: KVVec, in_sync_count: usize, cmdbuf_raw: &[u8], objects: Pin<&xarray::XArray>>) -> Result { if in_sync_count > syncs.len() { return Err(EINVAL); } diff --git a/drivers/gpu/drm/asahi/m3/m3_submit.rs b/drivers/gpu/drm/asahi/m3/m3_submit.rs index 92b82e1c8097b..cb45a2cde3c54 100644 --- a/drivers/gpu/drm/asahi/m3/m3_submit.rs +++ b/drivers/gpu/drm/asahi/m3/m3_submit.rs @@ -363,8 +363,15 @@ impl Queue { } } impl queue::Queue for Queue { - fn submit(&mut self,_reg_data:&crate::driver::AsahiRegData<'_>,_id:u64,mut syncs:KVec,in_sync_count:usize, - raw:&[u8],objects:Pin<&xarray::XArray>>)->Result { + fn submit( + &mut self, + _reg_data: &crate::driver::AsahiRegData<'_>, + _id: u64, + mut syncs: KVVec, + in_sync_count: usize, + raw: &[u8], + objects: Pin<&xarray::XArray>>, + ) -> Result { let vm_job=self.vm.retain_job()?; let mut parser=agx_uapi::UapiCommandParser::new(raw);let mut commands=KVec::new();let mut timestamps=KVec::new();let mut flushes=KVec::new();let mut visibility=KVec::new();let mut render_attachments=KVec::new(); while let Some(command)=parser.next_hardware().map_err(|_|EINVAL)? { diff --git a/drivers/gpu/drm/asahi/m5/drm.rs b/drivers/gpu/drm/asahi/m5/drm.rs index 6ab2108d8813b..0eb1e8a8ded4b 100644 --- a/drivers/gpu/drm/asahi/m5/drm.rs +++ b/drivers/gpu/drm/asahi/m5/drm.rs @@ -196,7 +196,7 @@ impl FenceOps for M5SubmissionFence { /// allocation failure after userspace has already supplied its wait targets. struct OutputPublication { fence: UserFence, - syncs: KVec, + syncs: KVVec, input_count: usize, result: Result, } @@ -237,7 +237,7 @@ struct M5Queue { impl queue::Queue for M5Queue { fn submit(&mut self, _reg_data: &AsahiRegData<'_>, _submission_id: u64, - syncs: KVec, + syncs: KVVec, in_sync_count: usize, cmdbuf_raw: &[u8], _objects: Pin<&xarray::XArray>>) -> Result { if in_sync_count > syncs.len() { return Err(EINVAL); } diff --git a/drivers/gpu/drm/asahi/queue/mod.rs b/drivers/gpu/drm/asahi/queue/mod.rs index 94405e88c6b0d..af4cf98f600f3 100644 --- a/drivers/gpu/drm/asahi/queue/mod.rs +++ b/drivers/gpu/drm/asahi/queue/mod.rs @@ -197,7 +197,7 @@ pub(crate) trait Queue: Send + Sync { &mut self, reg_data: &AsahiRegData<'_>, id: u64, - syncs: KVec, + syncs: KVVec, in_sync_count: usize, cmdbuf_raw: &[u8], objects: Pin<&xarray::XArray>>, @@ -1010,7 +1010,7 @@ impl Queue for Queue::ver { &mut self, reg_data: &AsahiRegData<'drm>, id: u64, - mut syncs: KVec, + mut syncs: KVVec, in_sync_count: usize, cmdbuf_raw: &[u8], objects: Pin<&xarray::XArray>>, diff --git a/tools/testing/selftests/drivers/gpu/asahi-m4/test-fence-signalling.py b/tools/testing/selftests/drivers/gpu/asahi-m4/test-fence-signalling.py index 0843277bc9368..6f00c99f20e25 100644 --- a/tools/testing/selftests/drivers/gpu/asahi-m4/test-fence-signalling.py +++ b/tools/testing/selftests/drivers/gpu/asahi-m4/test-fence-signalling.py @@ -376,7 +376,7 @@ def extract_function(text, name): pub(crate) cumulative: Option, } - pub(crate) type KVec = Vec; + pub(crate) type KVVec = Vec; fn install(outputs: impl Iterator, fence: &Fence) { for sync in outputs { diff --git a/tools/testing/selftests/drivers/gpu/asahi-m4/test-submit-restart.py b/tools/testing/selftests/drivers/gpu/asahi-m4/test-submit-restart.py index adbbf7cdaa39f..8bbd90ad88e2f 100644 --- a/tools/testing/selftests/drivers/gpu/asahi-m4/test-submit-restart.py +++ b/tools/testing/selftests/drivers/gpu/asahi-m4/test-submit-restart.py @@ -11,7 +11,7 @@ #![allow(dead_code,unused_variables)] use std::pin::Pin;use std::sync::atomic::{AtomicU32,Ordering}; type Result = std::result::Result;const ERESTARTSYS:i32=512;const EBUSY:i32=16; -type KVec=Vec;type KBox=Box; +type KVec=Vec;type KVVec=Vec;type KBox=Box; mod xarray{pub struct XArray(pub T);} mod file{pub struct SyncItem;pub struct Object;} mod driver{pub struct AsahiRegData<'a>(pub &'a ());} From 58c130777d7bc9339eed3f4f11dd94b3d16efe8b Mon Sep 17 00:00:00 2001 From: Jonathan Goyvaerts Date: Mon, 23 Nov 2026 19:18:10 -0300 Subject: [PATCH 2/2] drm/asahi: g15: refuse an oversized command buffer before copying it G15 accepts a command stream of at most MAX_CMDBUF (4 MiB), but checks it only in check_stream(), after the submit ioctl has copied the whole cmdbuf_size from userspace. Its max_cmdbuf_size() was the default, usize::MAX, so a submit of up to 4 GiB was first copied into the kernel and only then refused. Return MAX_CMDBUF from max_cmdbuf_size(), as G16 does, so the ioctl refuses a longer stream with E2BIG before it allocates anything. A stream that check_stream() accepts is unaffected; a longer one now fails with E2BIG instead of EINVAL. Signed-off-by: Jonathan Goyvaerts Signed-off-by: Dj <43033058+DjDeveloperr@users.noreply.github.com> --- drivers/gpu/drm/asahi/g15/mod.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/gpu/drm/asahi/g15/mod.rs b/drivers/gpu/drm/asahi/g15/mod.rs index 4cb221fb0a5df..d5cad14a50411 100644 --- a/drivers/gpu/drm/asahi/g15/mod.rs +++ b/drivers/gpu/drm/asahi/g15/mod.rs @@ -578,6 +578,10 @@ impl Firmware { } impl gpu::Gpu for Gpu { + fn max_cmdbuf_size(&self) -> usize { + command::MAX_CMDBUF + } + fn stats_snapshot(&self) -> Option> { None }