From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=165.173.180.11 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796418984; b=vW7GvD9DRx++GE2i1kfsbpZX/MQaJGhLCqdPZ9mtg8KpCdDQ5m2neVn1pdhOa8R+ltjP p71uZQD3KlFWUgqXCQA6LQrW2S2QwKgbtKxFTqzG/4ciaTm5awhVoheTR3EQcXqib2TgX 4KPnSG0JdYU+ESgGUVyvfOeh4rSGNfLCmDO566dalMg8zBCHWg3WNfe6NIVQpCKReN120 tHQ4Yx/M6VjcQFuk4asJg/NsI7YDExZDcvoPIDAKriMQr7+sHVNAFL3dQbvrKoI83LOrc NsWvtJox1bPef79qKgwzIQg/2ws6Il7/crpPGbyEmVhm58HdgqyZsxzG3Dr5fsPPFqg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796418984; h=DMARC-Filter:DKIM-Signature:Received:Content-Type:Mime-Version: Subject:From:In-Reply-To:Date:Cc:Content-Transfer-Encoding:Message-Id: References:To:X-Mailer:X-ZohoMailClient; bh=K0+8LOwtzJSkw4TETrIUEEk3Cy21Q6k4gERKd4vdpxU=; b=4JfYnV7nfeEhzN70xfFSb5bsFLTwmgbcGycDoTaU53zEzfobr7h/BuUF/0fMa6IXxPx4 moPmjJZxPBID2OYVCb/V8q0U52jr/vSd8l+abxQYUqtEwhHs2BNxmc4ZwL6X9/+qnY0bK voNu0anWY722XRbo3YCrRUxLFFMCSEasi9oNyGyH9nEiPYxYT9N65IeNmQQvu9LbNT4Gu TCJTj/cU3ZsXH3IaXn1Y0vw7gXS/xs5mKSpzmdRmwRe8hb+WjQ01qb5+bL+pGFIY7dBeX rZr9sp6cwXjcuUQPfAIV3l93QrgzQ1mlYy32I+171udUG5duLO0ojhMQXXbfETgUaDg== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass header.d=collabora.com header.i=@collabora.com header.a=rsa-sha256 header.s=zohomail header.b=eN53EJvz; arc=pass header.oldest-pass=0 smtp.remote-ip=165.173.180.11 Received: from sender6-op-o11.zoho.com (sender6-op-o11.zoho.com [165.173.180.11]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 384A1606A322; Fri, 4 Dec 2026 21:16:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 384A1606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=collabora.com header.i=@collabora.com header.a=rsa-sha256 header.s=zohomail header.b=eN53EJvz ARC-Seal: i=1; a=rsa-sha256; t=1796418938; cv=none; d=zohomail.com; s=zohoarc; b=nrRrhbJfVXhY9jlevnPlE0I5QGQPQBzy8ge9VPioLL5vZFTGdF2F7OXu7TLFgkLQujvNW7PvERmInVUf6OsO96Um4/7HuP+pe7ZsBqCbP/Qx0AtRy/Gvl1z4gxFzcDrNKKarDuk6goVSXJUhbmer7q1bfNP/Kb+nJQHwo2tEu8E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1796418938; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=K0+8LOwtzJSkw4TETrIUEEk3Cy21Q6k4gERKd4vdpxU=; b=jeyhC8kmC/m04F4dUnMo1Gim2c7Ovv0mDLjc2enPtHYjG62LTFfm/nTXnWsTkzWpKgL/k3HLJ3ZNME1uji0QCoS1mekTng2w+8li3qxHF1boXe8RszLzYdhE90YW4bMcmou791vdYoUbx7DteN2umqqx+fVvQH3+qHyia5FodNo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=collabora.com; spf=pass smtp.mailfrom=daniel.almeida@collabora.com; dmarc=pass header.from= DKIM-Signature: a=rsa-sha256; b=eN53EJvz1isNkBgFoATKHyOCnbFLw6eWTApBfhAbTmJmm7zRA79R/cVzvxqirAwSqtU708F46mlh+igrLY1tqVDGULMm0Jz9PMOiW9tazXC5E1m55GpQ6ejmyuGIk1n8b8nTCUfsr23zkJN67aWPKFLG++vhl7nsAukIQ3Wi8Z0=; c=relaxed/relaxed; s=zohomail; d=collabora.com; v=1; bh=K0+8LOwtzJSkw4TETrIUEEk3Cy21Q6k4gERKd4vdpxU=; h=Content-Description:Content-ID:Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Owner:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Resent-Sender:Resent-Cc:Resent-Date:Resent-To:Reply-To:Resent-Message-ID:Resent-From:Sender:Subject:To:Date:From:Message-Id:Reply-To:Subject:To:Cc; Received: by smtp.zohomail.com with SMTPS id 1796418936325140.1409830453241; Fri, 4 Dec 2026 13:15:36 -0800 (PST) Content-Type: text/plain; charset=us-ascii X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3901.200.71.1.16\)) Subject: Re: [PATCH v3 3/5] rust: drm: gem: shmem: Implement prime_import_sg_table callback From: Daniel Almeida In-Reply-To: <20261203-rust-drm-gem-shmem-imported-sgt-v3-3-8ca393e83e20@jannau.net> Date: Fri, 4 Dec 2026 18:15:17 -0300 Cc: Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Tamir Duberstein , Alexandre Courbot , =?utf-8?Q?Onur_=C3=96zkan?= , David Airlie , Simona Vetter , Sami Tolvanen , Lyude Paul , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , 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, Deborah Brouwer Content-Transfer-Encoding: quoted-printable Message-Id: <0FBD4F41-517B-4108-AC90-9D89F960AA5F@collabora.com> References: <20261203-rust-drm-gem-shmem-imported-sgt-v3-0-8ca393e83e20@jannau.net> <20261203-rust-drm-gem-shmem-imported-sgt-v3-3-8ca393e83e20@jannau.net> To: Janne Grunau X-Mailer: Apple Mail (2.3901.200.71.1.16) X-ZohoMailClient: External > On 3 Dec 2026, at 07:14, Janne Grunau wrote: >=20 > The C drm_gem_shmem_prime_import_sg_table() helper function uses > `__drm_gem_shmem_create()` to create shmem objects. In the absence of = a > `gem_create_object` callback this allocates just a `struct > drm_gem_shmem_object` resulting in buffer overflows when using it as > Rust `Object`. > Implementing the `gem_create_object` callback for RUST DRM drivers is > not sufficient as `__drm_gem_shmem_create()` uses `kfree(obj)` in = error > paths. This is unsound if the DriverObject contains fields with > non-trivial `Drop` implementations as noted in [1]. >=20 > 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. >=20 > Tested with the asahi downstream driver. >=20 > Link: = https://lore.kernel.org/rust-for-linux/20261118114102.490384-1-aliceryhl@g= oogle.com/ [1] > Fixes: 80df573af9ef ("rust: drm: gem: shmem: Add DRM shmem helper = abstraction") > Cc: stable+noautosel@kernel.org # no in tree users > Suggested-by: Alice Ryhl > Co-developed-by: Sami Tolvanen > Signed-off-by: Sami Tolvanen > Tested-by: Deborah Brouwer > Signed-off-by: Janne Grunau > --- > drivers/gpu/drm/nova/gem.rs | 4 +++ > drivers/gpu/drm/tyr/gem.rs | 4 +++ > rust/kernel/drm/driver.rs | 2 +- > rust/kernel/drm/gem/mod.rs | 9 ++++++ > rust/kernel/drm/gem/shmem.rs | 71 = +++++++++++++++++++++++++++++++++++++++++++- > 5 files changed, 88 insertions(+), 2 deletions(-) >=20 > diff --git a/drivers/gpu/drm/nova/gem.rs b/drivers/gpu/drm/nova/gem.rs > index 2b6fe9dc0bfa..2e17422e76f5 100644 > --- a/drivers/gpu/drm/nova/gem.rs > +++ b/drivers/gpu/drm/nova/gem.rs > @@ -27,6 +27,10 @@ impl gem::DriverObject for NovaObject { > fn new(_dev: &NovaDevice, _size: usize, _args: Self::Args) -> impl = PinInit { > try_pin_init!(NovaObject {}) > } > + > + fn create_imported(_dev: &NovaDevice, _size: usize) -> impl = PinInit { > + try_pin_init!(NovaObject {}) > + } > } >=20 > impl NovaObject { > diff --git a/drivers/gpu/drm/tyr/gem.rs b/drivers/gpu/drm/tyr/gem.rs > index 3bf3787f5c3f..4b61bb04c845 100644 > --- a/drivers/gpu/drm/tyr/gem.rs > +++ b/drivers/gpu/drm/tyr/gem.rs > @@ -48,6 +48,10 @@ impl gem::DriverObject for BoData { > fn new(_dev: &TyrDrmDevice, _size: usize, args: BoCreateArgs) -> = impl PinInit { > try_pin_init!(Self { flags: args.flags }) > } > + > + fn create_imported(_dev: &TyrDrmDevice, _size: usize) -> impl = PinInit { > + try_pin_init!(Self { flags: 0 }) > + } > } >=20 > /// Type alias for Tyr GEM buffer objects. > diff --git a/rust/kernel/drm/driver.rs b/rust/kernel/drm/driver.rs > index f6db91e81e79..7e8e842421a4 100644 > --- a/rust/kernel/drm/driver.rs > +++ b/rust/kernel/drm/driver.rs > @@ -118,7 +118,7 @@ pub trait Driver { > type RegistrationData<'a>: Send + Sync + 'a; >=20 > /// The type used to manage memory for this driver. > - type Object: AllocImpl; > + type Object: AllocImpl; I think this should be its own, preceding patch, IMHO. This change is needed regardless. The problem only became apparent now = with imported buffers, but it is otherwise a pre-existing issue. >=20 > /// The type used to represent a DRM File (client) > type File: drm::file::DriverFile; > diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs > index e1ebad77ebe2..e01b8311dd89 100644 > --- a/rust/kernel/drm/gem/mod.rs > +++ b/rust/kernel/drm/gem/mod.rs > @@ -98,6 +98,15 @@ fn new( > args: Self::Args, > ) -> impl PinInit; >=20 > + /// 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, size: usize) = -> impl PinInit; > + > /// Open a new handle to an existing object, associated with a = File. > fn open(_obj: &DriverAllocImpl, _file: &DriverFile) -> = Result { > Ok(()) > diff --git a/rust/kernel/drm/gem/shmem.rs = b/rust/kernel/drm/gem/shmem.rs > index 264ab188f77d..d6977673e21a 100644 > --- a/rust/kernel/drm/gem/shmem.rs > +++ b/rust/kernel/drm/gem/shmem.rs > @@ -32,6 +32,7 @@ > SysMem, > SysMemBackend, // > }, > + page::page_align, > prelude::*, > scatterlist, > sync::{ > @@ -345,6 +346,67 @@ pub fn new( > Ok(obj) > } >=20 > + /// Create a new shmem GEM object from another driver's = scatter/gather table of pinned pages. > + /// > + /// This is a Rust re-implemetation of = `drm_gem_shmem_prime_import_sg_table()` to avoid > + /// creating and destroying `Self` in C code. > + /// This function imports a scatter/gather table exported via = DMA-BUF by another driver. > + /// It should be only called via the = &drm_driver.gem_prime_import_sg_table callback. > + extern "C" fn prime_import_sg_table_callback( > + raw_dev: *mut bindings::drm_device, > + raw_attach: *mut bindings::dma_buf_attachment, > + raw_sgt: *mut bindings::sg_table, > + ) -> *mut bindings::drm_gem_object { > + // SAFETY: raw_attach is a valid pointer to = `bindings::dma_buf_attachment` created > + // by `dma_buf_attach` in `drm_gem_prime_import_dev` the only = caller of this > + // callback. > + let size =3D match page_align(unsafe { = (*(*raw_attach).dmabuf).size }) { > + Some(size) =3D> size, > + None =3D> return EINVAL.to_ptr(), > + }; > + > + // SAFETY: The DRM shmem helper only invokes this callback = for a registered DRM device of > + // type `T::Driver`, which is `Self`'s driver per the = `AllocImpl` impl below. > + let dev: &Device =3D unsafe { = Device::from_raw(raw_dev) }; > + > + let new: Pin> =3D match KBox::try_pin_init( > + try_pin_init!(Self { > + obj <- Opaque::init_zeroed(), > + parent_resv_obj: None, > + sgt_res: ManuallyDrop::new(SetOnce::new()), > + sgt_lock <- new_mutex!(()), > + inner <- T::create_imported(dev, size), > + }), > + GFP_KERNEL, > + ) { > + Ok(new) =3D> new, > + Err(e) =3D> return e.to_ptr(), > + }; > + > + // SAFETY: `new.as_raw()` is guaranteed to be valid by the = initialization above. > + unsafe { (*new.as_raw()).funcs =3D &Self::VTABLE }; > + > + // SAFETY: The arguments are all valid via the type = invariants. > + if let Err(e) =3D to_result(unsafe { > + bindings::drm_gem_shmem_init(dev.as_raw(), = new.as_raw_shmem(), size, true) > + }) { > + return e.to_ptr(); > + }; > + > + // SAFETY: `new` is an initialized `Self` never moved outside = of this function so it is > + // safe to initialized `drm_gem_shmem_object::sgt` with = `raw_sgt` as expected by the caller > + // of this function. > + unsafe { (*new.as_raw_shmem()).sgt =3D raw_sgt }; > + > + // SAFETY: We never move out of `self`. Ownership of the = allocation is transferred to the > + // C side. > + let new =3D 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 } > + } > + > /// Creates and returns an owned reference to a virtual kernel = memory mapping for this object. > #[inline] > pub fn owned_vmap(&self) -> Result> { > @@ -395,7 +457,7 @@ impl driver::AllocImpl for = Object { > prime_handle_to_fd: None, > prime_fd_to_handle: None, > gem_prime_import: None, > - gem_prime_import_sg_table: = Some(bindings::drm_gem_shmem_prime_import_sg_table), > + gem_prime_import_sg_table: = Some(Object::::prime_import_sg_table_callback), > dumb_create: None, > dumb_map_offset: None, > }; > @@ -625,6 +687,13 @@ fn new( > ) -> impl PinInit { > try_pin_init!(KunitObject {}) > } > + > + fn create_imported( > + _dev: &drm::Device, > + _size: usize, > + ) -> impl PinInit { > + try_pin_init!(KunitObject {}) > + } > } >=20 > #[vtable] >=20 > --=20 > 2.56.1 >=20 >=20 Reviewed-by: Daniel Almeida =