From: Alice Ryhl <aliceryhl@google.com>
To: Janne Grunau <j@jannau.net>, Sami Tolvanen <samitolvanen@google.com>
Cc: "Alice Ryhl" <aliceryhl@google.com>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
"Lyude Paul" <lyude@redhat.com>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
dri-devel@lists.freedesktop.org, nova-gpu@lists.linux.dev,
stable+noautosel@kernel.org
Subject: Re: [PATCH 1/3] rust: drm: gem: shmem: implement gem_create_object for prime imports
Date: Wed, 18 Nov 2026 11:41:01 +0000 [thread overview]
Message-ID: <20261118114102.490384-1-aliceryhl@google.com> (raw)
In-Reply-To: <20261026-rust-drm-gem-shmem-imported-sgt-v1-1-36f15ace04b1@jannau.net>
On Mon, Oct 26, 2026 at 12:14:08PM +0100, Janne Grunau wrote:
> From: Sami Tolvanen <samitolvanen@google.com>
>
> For a prime-imported BO with no `gem_create_object` callback,
> `__drm_gem_shmem_create()` allocates `kzalloc(sizeof(struct
> drm_gem_shmem_object))`, which is smaller than the Rust `Object<T>`
> wrapper. A later `container_of!()` from a `drm_gem_object` back to
> `Object<T>` then runs past the allocation and corrupts the heap.
>
> Provide a `gem_create_object` callback that allocates the full
> `Object<T>` via `KBox::pin_init`. Move `obj` to the first field so the
> returned pointer is at offset zero, satisfying the `kfree()` on the
> init-failure path. This is now compile-checked with `const_assert!`.
>
> Imported BOs have no userspace creation arguments, so extend
> `DriverObject` with a `create_imported(dev, size)` constructor and
> implement it for Tyr's `BoData`, Nova, and the in-tree KUnit driver.
...
> + /// Create a new driver data object for a GEM object that is being imported
> + /// from another driver (e.g. via dma-buf).
> + ///
> + /// Imported objects are allocated by the importing helper, which only
> + /// provides the device and size. There is no driver-supplied
> + /// [`DriverObject::Args`], so drivers must initialize the per-object
> + /// state without those arguments.
> + fn create_imported(dev: &drm::Device<Self::Driver>, size: usize) -> impl PinInit<Self, Error>;
...
> + // SAFETY: We never move out of `self`. Ownership of the allocation is transferred to the
> + // C side, which will run `__drm_gem_shmem_init()` next and later free us via
> + // `free_callback` (or `kfree()` on the `__drm_gem_shmem_init()` error path, matching the
> + // C kzalloc fallback contract).
> + let new = KBox::into_raw(unsafe { Pin::into_inner_unchecked(new) });
> +
> + // SAFETY: `new` was just produced by `KBox::into_raw` and so is a valid pointer to an
> + // initialized `Self`. `obj.base` is the embedded `drm_gem_object` the caller expects.
> + unsafe { &raw mut (*(*new).obj.get()).base }
> + }
I ran this through a review agent, and it found these two things. As far as I
can tell, these are valid concerns.
1. On the `__drm_gem_shmem_init()` failure path, `__drm_gem_shmem_create()`
calls `kfree(obj)` directly rather than reconstructing `KBox<Self>`.
Having `obj` at offset 0 ensures `kfree(obj)` frees the start of the slab
allocation, but it skips running `Drop` on `Object<T>` and `inner: T`. If a
driver's `T` owns any fields with non-trivial `Drop` implementations
initialized in `T::create_imported()`, they will be leaked if
`__drm_gem_shmem_init()` fails (e.g. if `drm_gem_create_mmap_offset()`
fails).
2. `Object<T>::ALLOC_OPS` also sets:
dumb_create: Some(bindings::drm_gem_shmem_dumb_create),
`drm_gem_shmem_dumb_create()` calls `drm_gem_shmem_create()` ->
`__drm_gem_shmem_create(dev, size, false)` -> `gem_create_object_callback()`,
and `DRM_IOCTL_MODE_CREATE_DUMB` only checks `dev->driver->dumb_create`
(not `DRIVER_MODESET`). So `T::create_imported()` can also be called to
create a non-imported dumb buffer (`obj.is_imported() == false`).
Instead of implementing `gem_create_object`, what do you think about
exporting a `private = true` counterpart to `drm_gem_shmem_init()` (similar to
commit e3f4bdaf2c5b ("drm/gem/shmem: Extract drm_gem_shmem_init() from
drm_gem_shmem_create()")), implementing a Rust `gem_prime_import_sg_table`
callback on `shmem::Object<T>` directly, and setting `dumb_create: None` in
`Object<T>::ALLOC_OPS`? That way `new: Pin<KBox<Self>>` stays owned by Rust
across `__drm_gem_shmem_init()` just like in `Object::new()`, so `Drop` runs
normally on error and `obj` does not need to sit at offset 0 for `kfree(obj)`.
Alice
next prev parent reply other threads:[~2026-11-18 11:41 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-26 11:14 [PATCH 0/3] Fix import of dma-buf buffers as DRM GEM shmem objects Janne Grunau
2026-10-26 11:14 ` [PATCH 1/3] rust: drm: gem: shmem: implement gem_create_object for prime imports Janne Grunau
2026-11-18 11:41 ` Alice Ryhl [this message]
2026-10-26 11:14 ` [PATCH 2/3] rust: drm: gem: add an is_imported() accessor Janne Grunau
2026-11-18 11:41 ` Alice Ryhl
2026-10-26 11:14 ` [PATCH 3/3] rust: drm: gem: shmem: Do not free SG table of imported objects Janne Grunau
2026-11-18 11:41 ` Alice Ryhl
2026-11-16 23:01 ` [PATCH 0/3] Fix import of dma-buf buffers as DRM GEM shmem objects Deborah Brouwer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261118114102.490384-1-aliceryhl@google.com \
--to=aliceryhl@google.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=airlied@gmail.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=gary@garyguo.net \
--cc=j@jannau.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=lyude@redhat.com \
--cc=nova-gpu@lists.linux.dev \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=samitolvanen@google.com \
--cc=simona@ffwll.ch \
--cc=stable+noautosel@kernel.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®