From 6305df378cf390d42dc0943a7b3d2faa427ada6f Mon Sep 17 00:00:00 2001 From: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> Date: Fri, 27 Nov 2026 01:07:24 +0000 Subject: [PATCH] drm/asahi: bound SUBMIT's user copies by what kmalloc() accepts SUBMIT copies the command stream and the sync array into kmalloc() buffers (KVec), with sizes that userspace picks. kmalloc() warns when asked for more than KMALLOC_MAX_SIZE, and with panic_on_warn that warning stops the machine. G16 and G17 refuse a longer stream before the copy, but: - The default max_cmdbuf_size() is usize::MAX. The GPUs that do not override it (g14_v27_2, G15, G18, M3 and M5) copy a stream of any size the ioctl allows. - G15's parser refuses a stream over its 4 MiB MAX_CMDBUF, but only after the copy. - parse_array() sizes the sync array from in_sync_count plus out_sync_count with no bound. - VM_BIND copies stride * num_binds bytes. A GPU without a bind batch limit does not bound either one. Make KMALLOC_MAX_SIZE the default bound, as g17::command::MAX_STREAM already asserts for G17. Have G15 return its MAX_CMDBUF, as G16 does, so a longer stream fails with E2BIG before the copy instead of EINVAL after it. Refuse a sync array larger than KMALLOC_MAX_SIZE with E2BIG. Charge both copies to the caller's memcg (GFP_KERNEL_ACCOUNT), since userspace picks their size. Copy the VM_BIND array with __GFP_NOWARN, so an oversized one fails with ENOMEM and no warning. No stream that could be copied before is refused: a larger copy already failed in kmalloc(), with the warning. G15 and G16 accept at most 4 MiB, and G17's MAX_STREAM is checked at build time to be at most KMALLOC_MAX_SIZE on arm64. The earlier version of this change also moved both copies to kvmalloc() and gave G17 its own bound. main has since given G17 MAX_STREAM and keeps the copies in kmalloc() buffers, so this version follows that. Signed-off-by: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> --- drivers/gpu/drm/asahi/file.rs | 16 ++++++++++++---- drivers/gpu/drm/asahi/g15/mod.rs | 4 ++++ drivers/gpu/drm/asahi/gpu.rs | 6 +++++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/drivers/gpu/drm/asahi/file.rs b/drivers/gpu/drm/asahi/file.rs index fc2d2dd6cedaf..8b8ab49161ff6 100644 --- a/drivers/gpu/drm/asahi/file.rs +++ b/drivers/gpu/drm/asahi/file.rs @@ -301,7 +301,12 @@ impl SyncItem { 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 picks the count, and the array is one kmalloc() buffer, which warns past + // KMALLOC_MAX_SIZE. Refuse a larger one first. Charge it to the caller's memcg. + if count as usize > bindings::KMALLOC_MAX_SIZE as usize / core::mem::size_of::() { + return Err(E2BIG); + } + let mut vec = KVec::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 @@ -425,11 +430,13 @@ impl TableQueue { } } +/// Copies the command stream. `max_cmdbuf_size()` has bounded `size`. The buffer is charged +/// to the caller's memcg, as userspace picks its size. fn copy_cmdbuf_from_user(buf: &mut KVec, 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 { @@ -1220,7 +1227,8 @@ impl File { .checked_mul(usize::try_from(data.num_binds)?) .ok_or(EOVERFLOW)?; let reader = UserSlice::new(UserPtr::from_addr(data.userptr as usize), size).reader(); - reader.read_all(&mut vec, GFP_KERNEL)?; + // Without a GPU batch limit, userspace picks the size: fail, do not warn. + reader.read_all(&mut vec, GFP_KERNEL | __GFP_NOWARN)?; let mut reader = Reader::new(&vec); for _i in 0..data.num_binds { @@ -1239,7 +1247,7 @@ impl File { let mut bytes = KVec::new(); UserSlice::new(UserPtr::from_addr(data.userptr as usize), size) .reader() - .read_all(&mut bytes, GFP_KERNEL)?; + .read_all(&mut bytes, GFP_KERNEL | __GFP_NOWARN)?; let (vm, user_range, kernel_range) = { let vms = file.inner().vms(); let guard = vms.lock(); diff --git a/drivers/gpu/drm/asahi/g15/mod.rs b/drivers/gpu/drm/asahi/g15/mod.rs index 6d5ca934a0ff2..f59cd893bf9e5 100644 --- a/drivers/gpu/drm/asahi/g15/mod.rs +++ b/drivers/gpu/drm/asahi/g15/mod.rs @@ -589,6 +589,10 @@ impl Firmware { } impl gpu::Gpu for Gpu { + fn max_cmdbuf_size(&self) -> usize { + command::MAX_CMDBUF + } + fn stats_snapshot(&self) -> Option> { None } diff --git a/drivers/gpu/drm/asahi/gpu.rs b/drivers/gpu/drm/asahi/gpu.rs index fb5359137b668..54dc75ad38b98 100644 --- a/drivers/gpu/drm/asahi/gpu.rs +++ b/drivers/gpu/drm/asahi/gpu.rs @@ -406,8 +406,12 @@ impl WorkItem for GpuManager::ver { /// Operations the DRM driver performs on a GPU, independent of its firmware interface. pub(crate) trait Gpu: Send + Sync { /// Admission bound checked before copying the userspace command stream. + /// + /// The copy is a kmalloc() buffer, which warns past `KMALLOC_MAX_SIZE`, so that is the + /// default. A GPU whose parser accepts less returns its own limit (G15 and G16: 4 MiB; + /// G17: see `g17::command::MAX_STREAM`). fn max_cmdbuf_size(&self) -> usize { - usize::MAX + kernel::bindings::KMALLOC_MAX_SIZE as usize } /// The firmware statistics snapshot of this GPU, if its firmware reports any.