Skip to content

drm/asahi: allocate imported and dumb GEM objects as asahi objects - #240

Merged
RyanTheTide merged 2 commits into
aurora-wipfrom
j700/asahi-prime-import
Oct 11, 2026
Merged

RyanTheTide merged 2 commits into
aurora-wipfrom
j700/asahi-prime-import

Conversation

@RyanTheTide

Copy link
Copy Markdown
Contributor

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 an
asahi object, so ASAHI_VM_BIND read and wrote fields past the end of the allocation. KASAN reports it on every GNOME
login 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 buffer
reaches it through drm_gem_shmem_dumb_create(). Both helpers allocate the object in __drm_gem_shmem_create(), which
allocates a bare 456-byte struct drm_gem_shmem_object unless the driver provides gem_create_object. The Rust shmem
abstraction offers both helpers but sets no gem_create_object, while lookup_handle() and the GEM object callbacks
treat 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_BIND on every frame. With KASAN:

BUG: KASAN: slab-out-of-bounds in ...AsahiDriver...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 ...shmem::Object<asahi_core::gem::AsahiObject>::owned_sg_table+0x48/0x11c [asahi]
Write of size 8 at addr ffff00002ac179d0 by task gnome-shell/1175
Allocated by task 1175:
 __drm_gem_shmem_create <- drm_gem_shmem_prime_import_sg_table <- drm_gem_prime_import_dev
 <- drm_gem_prime_fd_to_handle <- drm_prime_fd_to_handle_ioctl
The buggy address is located 40 bytes to the right of allocated 456-byte region (kmalloc-512)
  • The 4-byte read is the asahi object's flags, which validate_vm_binding() checks.
  • The 8-byte read and write are the SG-table lease counter, sgt_access, which owned_sg_table() updates. So this is
    a write as well as reads.
  • The asahi fields end at byte 504 of the 512-byte kmalloc slot. On a kernel without KASAN the stray accesses
    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:

  • An imported object got the shmem helpers' default drm_gem_object_funcs, not the driver's. Closing its last handle
    in a file did not run asahi's close(), so its VM mappings in that file were not unbound.
  • A dumb buffer created on the asahi card node had the same layout problem.

The fix

Two commits:

  1. rust: drm: gem: shmem: let drivers allocate the objects the core creates
    • Adds an optional DriverObject::core_object_args(): the Args for objects the DRM core allocates on the
      driver's behalf.
    • When a driver implements it, the abstraction installs a gem_create_object callback. The callback allocates the
      whole Object<T>, with the driver data built from those arguments, and installs the driver's GEM vtable. The core
      then initialises the shmem part itself, as it does for C shmem drivers that provide gem_create_object.
    • Drivers that do not implement it are unchanged.
  2. drm/asahi: allocate imported and dumb GEM objects as asahi objects
    • asahi implements core_object_args(). Such objects are shared user objects: flags 0, exportable, not
      kernel-owned. That matches a GEM_CREATE object without flags.
    • They now also get asahi's close(), so closing the last handle in a file unbinds them from that file's VMs, like
      every other object.
    • They stay out of idle reclaim and GEM_MADVISE, because both reject exportable objects. The shmem helpers'
      reclaim and purge paths also refuse imported objects.
    • Their shmem map_wc stays as the core sets it, as before this change.

Upstream rust/kernel/drm/gem/shmem.rs (master) still has gem_create_object: None alongside the import and dumb
helpers, 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 on 259ee1d76704, which adds only the
t8140-aop-audio change (#239); the two commits are unchanged.

prime-test below is a small raw-ioctl exercise of every import and dumb-buffer path that reaches ASAHI_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.

Kernel GNOME login prime-test vkcube WARN/Oops
base, KASAN slab-out-of-bounds in ASAHI_VM_BIND at 16.3 s, gnome-shell, on an object from drm_gem_shmem_prime_import_sg_table() not run - the KASAN report
this change, KASAN no KASAN report 6 × 200 pass, no KASAN report pass 0
this change - 6 × 200 pass pass (600 frames) 0

glmark2 (-b build:duration=5 -b texture:duration=5, zink on Honeykrisp), three runs each:

Kernel Runs Mean ± sd
base 2273, 2268, 2250 2263.7 ± 12.1
this change 2267, 2267, 2275 2269.7 ± 4.6

MacBook Air (M2) and MacBook Pro (M1 Pro): the same driver and abstraction

Machine Kernel Display (modeset, swaps, backlight) vkcube prime-test WARN/Oops Kernel warnings vs the same tree without this change
M2 c679c8a + this PR pass (16 swaps) pass 6 × 200 pass, no kernel messages 0 none new, none gone
M1 Pro c679c8a + this PR pass (5 swaps) pass 6 × 200 pass, no kernel messages 0 none new, none gone

Checks

  • checkpatch.pl --strict: clean except the Assisted-by format warning.
  • J700 config, base and each commit: W=1 and CLIPPY=1 builds of rust/ and drivers/gpu/drm/asahi/, then Image
    and 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

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]>
@RyanTheTide
RyanTheTide merged commit fffde61 into aurora-wip Oct 11, 2026
@DjDeveloperr
DjDeveloperr deleted the j700/asahi-prime-import branch October 12, 2026 19:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant