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.