Repository navigation
drm/asahi: allocate imported and dumb GEM objects as asahi objects - #240
Merged
Merged
Conversation
The DRM core allocates some GEM objects itself. drm_gem_shmem_prime_import_sg_table() and drm_gem_shmem_dumb_create() both go through __drm_gem_shmem_create(), which allocates a bare struct drm_gem_shmem_object unless the driver provides gem_create_object. The shmem abstraction offers both helpers but no gem_create_object, while lookup_handle() and the GEM object callbacks take every object of the driver to be an Object<T>. An imported dma-buf or a dumb buffer therefore reaches the driver as an Object<T> whose Rust fields lie past the end of its allocation. Add an optional DriverObject::core_object_args() that gives the arguments for such objects. When a driver provides it, a gem_create_object callback allocates the whole Object<T> and leaves the shmem part to the core, as the C shmem drivers do with their own gem_create_object. Assisted-by: LLM Signed-off-by: Ryan Murray <[email protected]>
A dma-buf imported from another device comes back from drm_gem_shmem_prime_import_sg_table() as a bare 456-byte struct drm_gem_shmem_object. gnome-shell imports one at startup and binds it on every frame. ASAHI_VM_BIND then reads the object's flags and the SG lease counter behind its end, and updates the counter there. KASAN reports it on every boot: BUG: KASAN: slab-out-of-bounds in ...ASAHI_VM_BIND+0xb7c/0x1840 [asahi] Read of size 4 at addr ffff00002ac179f0 by task gnome-shell/1175 BUG: KASAN: slab-out-of-bounds in ...Object<AsahiObject>::owned_sg_table+0x48/0x11c [asahi] Write of size 8 at addr ffff00002ac179d0 by task gnome-shell/1175 ... The buggy address is located 40 bytes to the right of allocated 456-byte region [ffff00002ac17800, ffff00002ac179c8) A dumb buffer created on the card node has the same problem. Give the DRM core the arguments for the objects it allocates, so that imports and dumb buffers are allocated as whole asahi objects: shared, neither VM-private nor kernel-owned, like a GEM_CREATE object without flags. They now also get the driver's GEM callbacks, so closing the last handle in a file unbinds them from that file's VMs, as it does for every other object. Fixes: 80df573 ("rust: drm: gem: shmem: Add DRM shmem helper abstraction") Assisted-by: LLM Signed-off-by: Ryan Murray <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
drm/asahi: allocate imported and dumb GEM objects as asahi objects
Fixes a type confusion in the asahi driver for GEM objects the DRM core allocates itself: dma-bufs imported from
another device (PRIME) and dumb buffers. They were plain
drm_gem_shmem_objects, but the driver uses every handle as anasahi object, so
ASAHI_VM_BINDread and wrote fields past the end of the allocation. KASAN reports it on every GNOMElogin on the MacBook Neo (J700).
The bug
A dma-buf imported from another device reaches asahi through
drm_gem_shmem_prime_import_sg_table(). A dumb bufferreaches it through
drm_gem_shmem_dumb_create(). Both helpers allocate the object in__drm_gem_shmem_create(), whichallocates a bare 456-byte
struct drm_gem_shmem_objectunless the driver providesgem_create_object. The Rust shmemabstraction offers both helpers but sets no
gem_create_object, whilelookup_handle()and the GEM object callbackstreat every object of the driver as a
shmem::Object<AsahiObject>.So an imported buffer is used as an asahi object whose Rust fields lie past the end of its allocation. gnome-shell
imports one at startup and binds it with
ASAHI_VM_BINDon every frame. With KASAN:flags, whichvalidate_vm_binding()checks.sgt_access, whichowned_sg_table()updates. So this isa write as well as reads.
therefore land in the slot's unused tail, not in a neighbouring object, which is why nothing visibly broke. The
values read are still whatever that tail holds.
Two more consequences of the same type confusion:
drm_gem_object_funcs, not the driver's. Closing its last handlein a file did not run asahi's
close(), so its VM mappings in that file were not unbound.The fix
Two commits:
rust: drm: gem: shmem: let drivers allocate the objects the core createsDriverObject::core_object_args(): theArgsfor objects the DRM core allocates on thedriver's behalf.
gem_create_objectcallback. The callback allocates thewhole
Object<T>, with the driver data built from those arguments, and installs the driver's GEM vtable. The corethen initialises the shmem part itself, as it does for C shmem drivers that provide
gem_create_object.drm/asahi: allocate imported and dumb GEM objects as asahi objectscore_object_args(). Such objects are shared user objects:flags0, exportable, notkernel-owned. That matches a
GEM_CREATEobject without flags.close(), so closing the last handle in a file unbinds them from that file's VMs, likeevery other object.
GEM_MADVISE, because both reject exportable objects. The shmem helpers'reclaim and purge paths also refuse imported objects.
map_wcstays as the core sets it, as before this change.Upstream
rust/kernel/drm/gem/shmem.rs(master) still hasgem_create_object: Nonealongside the import and dumbhelpers, so the abstraction change applies there too. It is opt-in here to keep this change small. A follow-up could
stop offering the import and dumb-buffer helpers to drivers that do not implement it. In this tree asahi is the only
user of the shmem abstraction.
Testing
Written for aurora-wip; measured on the MacBook Neo (J700), a 13-inch MacBook Air (M2) and a 16-inch MacBook Pro
(M1 Pro), with the two commits on
c679c8a75a89. The branch is now on259ee1d76704, which adds only thet8140-aop-audio change (#239); the two commits are unchanged.
prime-testbelow is a small raw-ioctl exercise of every import and dumb-buffer path that reachesASAHI_VM_BIND:a dma-heap buffer imported into the asahi render node, bound read-write and read back through
GEM_MMAP_OFFSET;closing a bound import; importing an asahi object into itself; exporting an asahi object to the display device;
importing a display dumb buffer; and a dumb buffer on the asahi card node. 200 iterations of each.
MacBook Neo (J700)
Both KASAN kernels use the same configuration (the J700 default plus generic KASAN) and differ only in these two
commits.
ASAHI_VM_BINDat 16.3 s, gnome-shell, on an object fromdrm_gem_shmem_prime_import_sg_table()glmark2 (
-b build:duration=5 -b texture:duration=5, zink on Honeykrisp), three runs each:MacBook Air (M2) and MacBook Pro (M1 Pro): the same driver and abstraction
Checks
checkpatch.pl --strict: clean except theAssisted-byformat warning.W=1andCLIPPY=1builds ofrust/anddrivers/gpu/drm/asahi/, then Imageand modules. No warnings from W=1 or the full build, and the same clippy output before and after.
Provenance
Written for this tree against the existing in-tree abstraction and driver; measured on the machines above.
🤖 Generated with Claude Code