From nobody Sat Jun 13 15:11:34 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C97942036E9; Wed, 6 May 2026 22:10:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778105442; cv=none; b=ET8ID2MkEfYFssAo78QcrDz1mXeYkhO91yzmm3C5xeiaoBeiWJOLVsgDNP2ZM12fnqp9ozc10kP5kaHtSvyYgY00y1gDYaadR2ej6RDWbDCFB8gnFXX/kISv83tl1EqKEYKA2zQwk2ZsWaeEzYo8vuWddFddNyEzwN1M7D+e0Ks= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778105442; c=relaxed/simple; bh=bALF/IYTJIqtzlU1XsvpEU8iNO0MoFwZPtOvka/AV1Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gYzRnui/4IYqAe5+UqcIWKRYqAIDPC+mhweB/DH3jDeW+CqP1cwJntijc4jpslD7W2xykdSJ6WMce+c4Y2sTPDDUngQFnEnB1g/WmIadZjzmfedVYNOK3v9G5I53WfGbhxtL6a0a5NhGU4XLRcuYfCwDISmXNNS86KxbC8y1paw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jW4NqOd2; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="jW4NqOd2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 71481C2BCF6; Wed, 6 May 2026 22:10:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778105442; bh=bALF/IYTJIqtzlU1XsvpEU8iNO0MoFwZPtOvka/AV1Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jW4NqOd2ITWs7bcupuc+pcnamlrDh/tUSYzAO/vw0oaffZIMZh6Lxo4Ugu/dqCJnA 6Ix3aue/N93hwT8uISFOOvVwnDM41rkJ51fk505IIW4MhbQ0rf0pXX32LWXkqGTgBL WkkmNHy2PcOd0FM8akF+6vYeqY0a0DR2mii7yjKWArOi9Ei5E8BQ8lkI8auOnvHASP b+6jbO2oafL2KgedAFwkIvldI3+SClMKgdWE8L9jPWFXQLp1caJK0bWbQqvtTOWFgu /j8MOi8+nXg3CzDnEVbQgrDFyPaprKKuDWHtr2+I9Mesc0JV6vaecSk5CzXeeGPQ6x mSe+k2BkP2h1A== From: Danilo Krummrich To: aliceryhl@google.com, airlied@gmail.com, simona@ffwll.ch, daniel.almeida@collabora.com, acourbot@nvidia.com, apopple@nvidia.com, ecourtney@nvidia.com, deborah.brouwer@collabora.com, lyude@redhat.com, ojeda@kernel.org, boqun@kernel.org, gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org, a.hindborg@kernel.org, tmgross@umich.edu Cc: driver-core@lists.linux.dev, nova-gpu@lists.linux.dev, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Danilo Krummrich Subject: [PATCH 1/6] rust: drm: Add Driver::ParentDevice associated type Date: Thu, 7 May 2026 00:05:59 +0200 Message-ID: <20260506221027.858481-2-dakr@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260506221027.858481-1-dakr@kernel.org> References: <20260506221027.858481-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a ParentDevice associated type to the Driver trait, allowing each DRM driver to declare its parent bus device type (e.g. auxiliary::Device, platform::Device). Change UnregisteredDevice::new() to take &T::ParentDevice, ensuring at the type level that the DRM device's parent matches the declared bus device type. Signed-off-by: Danilo Krummrich Reviewed-by: Lyude Paul --- drivers/gpu/drm/nova/driver.rs | 8 ++++++-- drivers/gpu/drm/tyr/driver.rs | 6 ++++-- rust/kernel/drm/device.rs | 7 +++++-- rust/kernel/drm/driver.rs | 3 +++ 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/drivers/gpu/drm/nova/driver.rs b/drivers/gpu/drm/nova/driver.rs index 9eef28b506bb..9d4100f01ea7 100644 --- a/drivers/gpu/drm/nova/driver.rs +++ b/drivers/gpu/drm/nova/driver.rs @@ -2,7 +2,10 @@ =20 use kernel::{ auxiliary, - device::Core, + device::{ + Core, + DeviceContext, // + }, drm::{ self, gem, @@ -59,7 +62,7 @@ fn probe( ) -> impl PinInit + 'bound { let data =3D try_pin_init!(NovaData { adev: adev.into() }); =20 - let drm =3D drm::UnregisteredDevice::::new(adev.as_ref(), da= ta)?; + let drm =3D drm::UnregisteredDevice::::new(adev, data)?; let drm =3D drm::Registration::new_foreign_owned(drm, adev.as_ref(= ), 0)?; =20 Ok(Self { drm: drm.into() }) @@ -71,6 +74,7 @@ impl drm::Driver for NovaDriver { type Data =3D NovaData; type File =3D File; type Object =3D gem::Object; + type ParentDevice =3D auxiliary::Device; =20 const INFO: drm::DriverInfo =3D INFO; =20 diff --git a/drivers/gpu/drm/tyr/driver.rs b/drivers/gpu/drm/tyr/driver.rs index 2743178af325..747745d23f31 100644 --- a/drivers/gpu/drm/tyr/driver.rs +++ b/drivers/gpu/drm/tyr/driver.rs @@ -8,7 +8,8 @@ device::{ Bound, Core, - Device, // + Device, + DeviceContext, // }, drm, drm::ioctl, @@ -131,7 +132,7 @@ fn probe( gpu_info, }); =20 - let tdev =3D drm::UnregisteredDevice::::new(pdev.as_= ref(), data)?; + let tdev =3D drm::UnregisteredDevice::::new(pdev, da= ta)?; let tdev =3D drm::driver::Registration::new_foreign_owned(tdev, pd= ev.as_ref(), 0)?; =20 let driver =3D TyrPlatformDriverData { @@ -176,6 +177,7 @@ impl drm::Driver for TyrDrmDriver { type Data =3D TyrDrmDeviceData; type File =3D TyrDrmFileData; type Object =3D drm::gem::Object; + type ParentDevice =3D platform::Device; =20 const INFO: drm::DriverInfo =3D INFO; =20 diff --git a/rust/kernel/drm/device.rs b/rust/kernel/drm/device.rs index d91f19dcc375..492c2f2c7ca4 100644 --- a/rust/kernel/drm/device.rs +++ b/rust/kernel/drm/device.rs @@ -197,7 +197,10 @@ impl UnregisteredDevice { /// Create a new `UnregisteredDevice` for a `drm::Driver`. /// /// This can be used to create a [`Registration`](kernel::drm::Registr= ation). - pub fn new(dev: &device::Device, data: impl PinInit) -= > Result { + pub fn new( + dev: &T::ParentDevice, + data: impl PinInit, + ) -> Result { // `__drm_dev_alloc` uses `kmalloc()` to allocate memory, hence en= sure a `kmalloc()` // compatible `Layout`. let layout =3D Kmalloc::aligned_layout(Layout::new::>()); @@ -207,7 +210,7 @@ pub fn new(dev: &device::Device, data: impl PinInit) -> Result =3D unsafe { bindings::__drm_dev_alloc( - dev.as_raw(), + dev.as_ref().as_raw(), &Self::VTABLE, layout.size(), mem::offset_of!(Device, dev), diff --git a/rust/kernel/drm/driver.rs b/rust/kernel/drm/driver.rs index 294e17af86a3..9d06f8c5b2da 100644 --- a/rust/kernel/drm/driver.rs +++ b/rust/kernel/drm/driver.rs @@ -114,6 +114,9 @@ pub trait Driver { /// The type used to represent a DRM File (client) type File: drm::file::DriverFile; =20 + /// The bus device type of the parent device that the DRM device is as= sociated with. + type ParentDevice: device::AsBusDevice; + /// Driver metadata const INFO: DriverInfo; =20 --=20 2.54.0 From nobody Sat Jun 13 15:11:34 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A72573B52E3; Wed, 6 May 2026 22:10:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778105447; cv=none; b=YFxFEjWaJX7FXHmubz5fiVulfkyOVVqtCUQeIaw3nBDXaeS40PDJ8z0K6SWBIZMv6XoEbzXtgl4YCyg5R9lY5NqRpRgX1fRdtXATbvW1eMzfROJi1XGfOrK8oK4gl9Z0ZeaZma+2oKygXD/vZUD8j7tDv5NdnAJMzBQFPTk3PDw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778105447; c=relaxed/simple; bh=TcNbTks32PZMaqi4Rmd1A9s7dMtJPREgNvFJTYp1+Do=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZmS3YwN4MNu6QHidFloYTcSIa+ArzVlMQv8PHyy9Ffgp42dBF0OE2ZihMsUg9VE0E9FvKtDOgZBB+pPZrPqX94iYPJ0i14J1+KE5CQjJyTQvgO+4Z6YDlyPE8WRFfKJJoVSX33LlkLcwyN0T/nIHmC5HOTOQ7ATdlaLEaJGx5Hc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=VnFlSaTA; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="VnFlSaTA" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1B04BC2BCC4; Wed, 6 May 2026 22:10:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778105447; bh=TcNbTks32PZMaqi4Rmd1A9s7dMtJPREgNvFJTYp1+Do=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VnFlSaTAD/f0e5baL8v+kavr1bwseoFY0Z2YgsGDC21grEHakKcHbWla6OLmSI+9Z /e8FgYoHUteFbNm8AFH+bXzme9p1NJ5hmHR0B4ZL8mQnHiZ5G0ulf5XQwtZmkdAOEA xwM9gbOSAZtVhbGwyZElERJrbRIMW1MM5qXJbEoyd0u9DIxpFnIuZNPHwhfIUL9TJ/ NMBI+n0qZ69REl7rejYva1lmrv4S6PEFGcGqp77NqMC1NCbkszid2/NZmXN5NkeY88 2wEINAEyHLXqn3RW3bOOrE6/OpsORRf7B0RCxj6Txcl0yAPOXhUTqfJs9+riY21eBj Gp05PSIRKqhnw== From: Danilo Krummrich To: aliceryhl@google.com, airlied@gmail.com, simona@ffwll.ch, daniel.almeida@collabora.com, acourbot@nvidia.com, apopple@nvidia.com, ecourtney@nvidia.com, deborah.brouwer@collabora.com, lyude@redhat.com, ojeda@kernel.org, boqun@kernel.org, gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org, a.hindborg@kernel.org, tmgross@umich.edu Cc: driver-core@lists.linux.dev, nova-gpu@lists.linux.dev, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Danilo Krummrich Subject: [PATCH 2/6] rust: drm: Add UnbindGuard for drm_dev_enter/exit critical sections Date: Thu, 7 May 2026 00:06:00 +0200 Message-ID: <20260506221027.858481-3-dakr@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260506221027.858481-1-dakr@kernel.org> References: <20260506221027.858481-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" DRM ioctls do not guarantee that the parent bus device is still bound. However, since DRM device registration is managed through Devres, using drm_dev_unplug() on unregistration ensures that between drm_dev_enter() and drm_dev_exit() the parent device must be bound. Add UnbindGuard, a guard object representing a drm_dev_enter/exit SRCU critical section that dereferences to &Device of the parent bus device. The guard is only available on Device, ensuring it cannot be used on unregistered devices. Also add with_unbind_guard() as a convenience helper that executes a closure with the bound device reference. Switch Registration::drop from drm_dev_unregister() to drm_dev_unplug() to provide the SRCU barrier that UnbindGuard's safety argument relies on. Signed-off-by: Danilo Krummrich Reviewed-by: Alistair Popple --- rust/kernel/drm/device.rs | 80 ++++++++++++++++++++++++++++++++++++++- rust/kernel/drm/driver.rs | 10 ++++- 2 files changed, 87 insertions(+), 3 deletions(-) diff --git a/rust/kernel/drm/device.rs b/rust/kernel/drm/device.rs index 492c2f2c7ca4..bb685165032d 100644 --- a/rust/kernel/drm/device.rs +++ b/rust/kernel/drm/device.rs @@ -6,7 +6,11 @@ =20 use crate::{ alloc::allocator::Kmalloc, - bindings, device, + bindings, + device::{ + self, + AsBusDevice as _, // + }, drm::{ self, driver::AllocImpl, @@ -334,6 +338,80 @@ pub(crate) unsafe fn assume_ctx= (&self) -> &Device Device { + /// Guard against the parent bus device being unbound. + /// + /// Returns an [`UnbindGuard`] if the device has not been unplugged, [= `None`] otherwise. + /// + /// The returned guard dereferences to the parent bus device in the [`= device::Bound`] context + /// (see [`Driver::ParentDevice`](drm::Driver::ParentDevice)). + /// Between `drm_dev_enter()` and `drm_dev_exit()` the parent device i= s guaranteed to be bound. + #[must_use] + pub fn unbind_guard(&self) -> Option> { + let mut idx: i32 =3D 0; + // SAFETY: `self.as_raw()` is a valid pointer to a `struct drm_dev= ice` by the type + // invariants of `Device`. + if unsafe { bindings::drm_dev_enter(self.as_raw(), &mut idx) } { + Some(UnbindGuard { dev: self, idx }) + } else { + None + } + } + + /// Execute a closure while the parent bus device is guaranteed to be = bound. + /// + /// Acquires the [`UnbindGuard`] and, if the device has not been unplu= gged, calls `f` with the + /// parent bus device. Returns `None` if the device has been unplugged. + pub fn with_unbind_guard( + &self, + f: impl FnOnce(&T::ParentDevice) -> R, + ) -> Option { + let guard =3D self.unbind_guard()?; + Some(f(&guard)) + } +} + +/// A guard preventing the parent bus device from being unbound. +/// +/// The guard dereferences to [`Driver::ParentDevice`](drm::Driver:= :ParentDevice), providing +/// access to the parent bus device with the guarantee that it is bound fo= r the entire duration of +/// the critical section. +/// +/// Internally this is backed by a `drm_dev_enter()` / `drm_dev_exit()` SR= CU critical section. +/// +/// See [`Device::unbind_guard`] for details on the safety argument. +/// +/// # Invariants +/// +/// - `idx` is the SRCU read lock index returned by a successful `drm_dev_= enter()` call. +/// - The parent bus device of `dev` is bound for the lifetime of this gua= rd. +#[must_use] +pub struct UnbindGuard<'a, T: drm::Driver> { + dev: &'a Device, + idx: i32, +} + +impl Deref for UnbindGuard<'_, T> { + type Target =3D T::ParentDevice; + + fn deref(&self) -> &Self::Target { + // SAFETY: + // - The parent `struct device` is embedded in a `T::ParentDevice`= , as guaranteed by + // `UnregisteredDevice::new` taking a `&T::ParentDevice`. + // - By the type invariants of `UnbindGuard`, the parent device is= bound for the lifetime + // of this guard. + unsafe { T::ParentDevice::from_device(self.dev.as_ref().as_bound()= ) } + } +} + +impl Drop for UnbindGuard<'_, T> { + fn drop(&mut self) { + // SAFETY: `self.idx` was returned by a successful `drm_dev_enter(= )` call, as guaranteed + // by the type invariants of `UnbindGuard`. + unsafe { bindings::drm_dev_exit(self.idx) }; + } +} + impl Deref for Device { type Target =3D T::Data; =20 diff --git a/rust/kernel/drm/driver.rs b/rust/kernel/drm/driver.rs index 9d06f8c5b2da..751a68bb27e1 100644 --- a/rust/kernel/drm/driver.rs +++ b/rust/kernel/drm/driver.rs @@ -187,8 +187,14 @@ unsafe impl Send for Registration {} =20 impl Drop for Registration { fn drop(&mut self) { + // Use `drm_dev_unplug` rather than `drm_dev_unregister` to ensure= that existing + // `drm_dev_enter()` critical sections complete before unregistrat= ion proceeds. This + // is required for the safety of `UnbindGuard`, which relies on th= e SRCU barrier in + // `drm_dev_unplug()` to guarantee that the parent device is still= bound within the + // critical section. + // // SAFETY: Safe by the invariant of `ARef>`. The ex= istence of this - // `Registration` also guarantees the this `drm::Device` is actual= ly registered. - unsafe { bindings::drm_dev_unregister(self.0.as_raw()) }; + // `Registration` also guarantees that this `drm::Device` is actua= lly registered. + unsafe { bindings::drm_dev_unplug(self.0.as_raw()) }; } } --=20 2.54.0 From nobody Sat Jun 13 15:11:34 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4BBCD3B4E97; Wed, 6 May 2026 22:10:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778105452; cv=none; b=PBfBmA5H9oh564dVnArOu/l2fO1gpBQU41FSlmJOCX2A7Y+Jj7UzqNdUOhm1AS4CBvgDSXo7rUPgOeBuNygfRpi2Gk7OX9b0Hkz1o2iNQS5QB61miwwf0wTPkJORbi3xDlCZhoPydtFdARxgFKxvQqATNDuKee8Sv/QJhrrWLjE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778105452; c=relaxed/simple; bh=23fi8AzSa5LgBrTmJFnzH5IJw9u3nJt2QbZBLA69eG4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=o7dh7PIZ4vRyvLV7wEe5Wns0NALk191LjSYHOxDXvYqOerYH+JtPsriGtK5HsMkuuGBpMJhHsrC8X5ndQncOevpYQhLnUm9HymsTV/2L5bKRkAeUGNHyvL3g6Qm13lLzZZNXvPTHF7qKjljOsu3hA7qHqVpAv6xwpRJIS01at74= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EH9Ox6+P; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="EH9Ox6+P" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B8701C2BCB0; Wed, 6 May 2026 22:10:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778105452; bh=23fi8AzSa5LgBrTmJFnzH5IJw9u3nJt2QbZBLA69eG4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EH9Ox6+PkcLRQKLNMQVrsAkea9Y5Xz/W4FgqttWlmhedId5n0qJeAjNK1WhjiblE/ mhpRP3tpyjUgGJVjel6gYKxtdy0I0Xu2qcJYamu7EVTf7uchkr56KLakZGvymiuAun koLhE6qGW6rPzg7a736HuiagJelLDwNOmVfUXZpfh9StiIoo6fi+a/4p0zPchjOgfe s2/hU6pG6wCBuoEZq69s6y31MxW2K5h8V0dMk3vG3EXSNBoGoRPTXtry0RpjuCT3Ms IgRXcVMuFj/RT1s41KUpPpvzILYCktRlWMwwzTbIfsW9bN9I/kPY75SFonvahYEC6i KaufuvXNvj/lA== From: Danilo Krummrich To: aliceryhl@google.com, airlied@gmail.com, simona@ffwll.ch, daniel.almeida@collabora.com, acourbot@nvidia.com, apopple@nvidia.com, ecourtney@nvidia.com, deborah.brouwer@collabora.com, lyude@redhat.com, ojeda@kernel.org, boqun@kernel.org, gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org, a.hindborg@kernel.org, tmgross@umich.edu Cc: driver-core@lists.linux.dev, nova-gpu@lists.linux.dev, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Danilo Krummrich Subject: [PATCH 3/6] rust: drm: Add RegistrationData to drm::Driver Date: Thu, 7 May 2026 00:06:01 +0200 Message-ID: <20260506221027.858481-4-dakr@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260506221027.858481-1-dakr@kernel.org> References: <20260506221027.858481-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a RegistrationData associated type to drm::Driver. This is a ForLt type whose lifetime is tied to the parent bus device binding scope. Registration takes ownership of the data via Pin>, erasing the lifetime to 'static for storage. The pointer is written to drm::Device before drm_dev_register() to ensure it is already in place when ioctls arrive. UnbindGuard::registration_data() provides access with the lifetime shortened from 'static via ForLt::cast_ref. Since Registration::drop() calls drm_dev_unplug() -- which performs an SRCU barrier waiting for all drm_dev_enter() critical sections to complete -- the data is guaranteed to remain valid for the duration of any UnbindGuard. Signed-off-by: Danilo Krummrich --- drivers/gpu/drm/nova/driver.rs | 6 ++- drivers/gpu/drm/tyr/driver.rs | 6 ++- rust/kernel/drm/device.rs | 40 +++++++++++++++ rust/kernel/drm/driver.rs | 89 +++++++++++++++++++++++++++------- rust/kernel/drm/mod.rs | 1 + 5 files changed, 121 insertions(+), 21 deletions(-) diff --git a/drivers/gpu/drm/nova/driver.rs b/drivers/gpu/drm/nova/driver.rs index 9d4100f01ea7..54a3391371ba 100644 --- a/drivers/gpu/drm/nova/driver.rs +++ b/drivers/gpu/drm/nova/driver.rs @@ -12,7 +12,8 @@ ioctl, // }, prelude::*, - sync::aref::ARef, // + sync::aref::ARef, + types::ForLt, // }; =20 use crate::file::File; @@ -63,7 +64,7 @@ fn probe( let data =3D try_pin_init!(NovaData { adev: adev.into() }); =20 let drm =3D drm::UnregisteredDevice::::new(adev, data)?; - let drm =3D drm::Registration::new_foreign_owned(drm, adev.as_ref(= ), 0)?; + let drm =3D drm::Registration::new_foreign_owned(drm, adev.as_ref(= ), (), 0)?; =20 Ok(Self { drm: drm.into() }) } @@ -72,6 +73,7 @@ fn probe( #[vtable] impl drm::Driver for NovaDriver { type Data =3D NovaData; + type RegistrationData =3D ForLt!(()); type File =3D File; type Object =3D gem::Object; type ParentDevice =3D auxiliary::Device; diff --git a/drivers/gpu/drm/tyr/driver.rs b/drivers/gpu/drm/tyr/driver.rs index 747745d23f31..7ac3707823b6 100644 --- a/drivers/gpu/drm/tyr/driver.rs +++ b/drivers/gpu/drm/tyr/driver.rs @@ -25,7 +25,8 @@ aref::ARef, Mutex, // }, - time, // + time, + types::ForLt, // }; =20 use crate::{ @@ -133,7 +134,7 @@ fn probe( }); =20 let tdev =3D drm::UnregisteredDevice::::new(pdev, da= ta)?; - let tdev =3D drm::driver::Registration::new_foreign_owned(tdev, pd= ev.as_ref(), 0)?; + let tdev =3D drm::driver::Registration::new_foreign_owned(tdev, pd= ev.as_ref(), (), 0)?; =20 let driver =3D TyrPlatformDriverData { _device: tdev.into(), @@ -175,6 +176,7 @@ fn drop(self: Pin<&mut Self>) { #[vtable] impl drm::Driver for TyrDrmDriver { type Data =3D TyrDrmDeviceData; + type RegistrationData =3D ForLt!(()); type File =3D TyrDrmFileData; type Object =3D drm::gem::Object; type ParentDevice =3D platform::Device; diff --git a/rust/kernel/drm/device.rs b/rust/kernel/drm/device.rs index bb685165032d..11edbe6f9f42 100644 --- a/rust/kernel/drm/device.rs +++ b/rust/kernel/drm/device.rs @@ -23,6 +23,7 @@ AlwaysRefCounted, // }, types::{ + ForLt, NotThreadSafe, Opaque, // }, @@ -35,6 +36,7 @@ }; use core::{ alloc::Layout, + cell::UnsafeCell, marker::PhantomData, mem, ops::Deref, @@ -239,6 +241,9 @@ pub fn new( unsafe { bindings::drm_dev_put(drm_dev) }; })?; =20 + // SAFETY: `raw_drm` is valid; no concurrent access before registr= ation. + unsafe { (*raw_drm.as_ptr()).registration_data =3D UnsafeCell::new= (NonNull::dangling()) }; + // SAFETY: The reference count is one, and now we take ownership o= f that reference as a // `drm::Device`. // INVARIANT: We just created the device above, but have yet to ca= ll `drm_dev_register`. @@ -270,6 +275,7 @@ pub fn new( pub struct Device { dev: Opaque, data: T::Data, + pub(super) registration_data: UnsafeCell::Of<'static>>>, _ctx: PhantomData, } =20 @@ -278,6 +284,23 @@ pub(crate) fn as_raw(&self) -> *mut bindings::drm_devi= ce { self.dev.get() } =20 + /// Returns a reference to the registration data with lifetime shorten= ed + /// from `'static`. + /// + /// # Safety + /// + /// The caller must ensure the parent bus device is bound. This is + /// typically guaranteed by holding an active `drm_dev_enter()` critic= al + /// section (e.g. via [`UnbindGuard`]). + #[doc(hidden)] + pub unsafe fn raw_registration_data(&self) -> &::Of<'_> { + // SAFETY: Caller guarantees the parent bus device is bound, hence + // the pointer is valid. + let static_ref =3D unsafe { (*self.registration_data.get()).as_ref= () }; + + T::RegistrationData::cast_ref(static_ref) + } + /// # Safety /// /// `ptr` must be a valid pointer to a `struct device` embedded in `Se= lf`. @@ -391,6 +414,23 @@ pub struct UnbindGuard<'a, T: drm::Driver> { idx: i32, } =20 +impl UnbindGuard<'_, T> { + /// Returns a reference to the registration data with its lifetime sho= rtened from `'static` + /// to the guard's borrow lifetime. + /// + /// The data is owned by [`Registration`](drm::driver::Registration) a= nd is guaranteed to + /// remain valid for the duration of this guard, since + /// [`Registration`](drm::driver::Registration)'s `drop` calls + /// `drm_dev_unplug()` which waits for all `drm_dev_enter()` critical = sections to complete. + pub fn registration_data(&self) -> &::Of= <'_> { + // SAFETY: The pointer was set in `Registration::new()` before `dr= m_dev_register()`, and + // is only invalidated after `drm_dev_unplug()` in `Registration::= drop()`. Since we hold + // an active `drm_dev_enter()` critical section, the SRCU barrier = in `drm_dev_unplug()` + // guarantees the pointer is still valid. + unsafe { self.dev.raw_registration_data() } + } +} + impl Deref for UnbindGuard<'_, T> { type Target =3D T::ParentDevice; =20 diff --git a/rust/kernel/drm/driver.rs b/rust/kernel/drm/driver.rs index 751a68bb27e1..3a49ef324ada 100644 --- a/rust/kernel/drm/driver.rs +++ b/rust/kernel/drm/driver.rs @@ -11,7 +11,8 @@ drm, error::to_result, prelude::*, - sync::aref::ARef, // + sync::aref::ARef, + types::ForLt, // }; use core::{ mem, @@ -108,6 +109,16 @@ pub trait Driver { /// Context data associated with the DRM driver type Data: Sync + Send; =20 + /// Data owned by the [`Registration`] and accessible through [`drm::d= evice::UnbindGuard`]. + /// + /// This is a [`ForLt`](trait@ForLt) type whose lifetime is tied to th= e parent bus + /// device binding scope. + /// The data is only accessible while the parent bus device is bound (= i.e. within a + /// `drm_dev_enter/exit` critical section), and references handed out = by + /// [`UnbindGuard::registration_data()`](drm::device::UnbindGuard::reg= istration_data) have + /// their lifetime shortened accordingly via [`ForLt::cast_ref`]. + type RegistrationData: ForLt; + /// The type used to manage memory for this driver. type Object: AllocImpl; =20 @@ -127,12 +138,44 @@ pub trait Driver { /// The registration type of a `drm::Device`. /// /// Once the `Registration` structure is dropped, the device is unregister= ed. -pub struct Registration(ARef>); +pub struct Registration { + drm: ARef>, + #[allow(dead_code)] + reg_data: Pin::Of<'static>>>, +} =20 -impl Registration { - fn new(drm: drm::UnregisteredDevice, flags: usize) -> Result { - // SAFETY: `drm.as_raw()` is valid by the invariants of `drm::Devi= ce`. - to_result(unsafe { bindings::drm_dev_register(drm.as_raw(), flags)= })?; +impl Registration +where + for<'a> ::Of<'a>: Send, +{ + fn new<'bound, E>( + drm: drm::UnregisteredDevice, + reg_data: impl PinInit<::Of<'bound>,= E>, + flags: usize, + ) -> Result + where + Error: From, + { + let reg_data: Pin::Of<'bound>>= > =3D + KBox::pin_init(reg_data, GFP_KERNEL)?; + + // SAFETY: `ForLt` guarantees covariance; lifetimes do not affect = layout. + let reg_data: Pin::Of<'static>= >> =3D + unsafe { mem::transmute(reg_data) }; + + // Store the registration data pointer in the device before regist= ration, so that it is + // visible once ioctls can be called. + // + // SAFETY: No concurrent access; the device is not yet registered. + unsafe { *drm.registration_data.get() =3D NonNull::from(Pin::get_r= ef(reg_data.as_ref())) } + + // SAFETY: `drm` is a valid, initialized but not yet registered DR= M device. + let ret =3D unsafe { bindings::drm_dev_register(drm.as_raw(), flag= s) }; + if let Err(e) =3D to_result(ret) { + // SAFETY: No concurrent access; registration failed. + unsafe { *drm.registration_data.get() =3D NonNull::dangling() = }; + return Err(e); + } =20 // SAFETY: We just called `drm_dev_register` above let new =3D NonNull::from(unsafe { drm.assume_ctx() }); @@ -144,46 +187,55 @@ fn new(drm: drm::UnregisteredDevice, flags: usize)= -> Result { // one reference to the device - which we take ownership over here. let new =3D unsafe { ARef::from_raw(new) }; =20 - Ok(Self(new)) + Ok(Self { drm: new, reg_data }) } =20 /// Registers a new [`UnregisteredDevice`](drm::UnregisteredDevice) wi= th userspace. /// /// Ownership of the [`Registration`] object is passed to [`devres::re= gister`]. - pub fn new_foreign_owned<'a>( + pub fn new_foreign_owned<'bound, E>( drm: drm::UnregisteredDevice, - dev: &'a device::Device, + dev: &'bound device::Device, + reg_data: impl PinInit<::Of<'bound>,= E>, flags: usize, - ) -> Result<&'a drm::Device> + ) -> Result<&'bound drm::Device> where T: 'static, + Error: From, { if drm.as_ref().as_raw() !=3D dev.as_raw() { return Err(EINVAL); } =20 - let reg =3D Registration::::new(drm, flags)?; + let reg =3D Registration::::new(drm, reg_data, flags)?; let drm =3D NonNull::from(reg.device()); =20 - devres::register(dev, reg, GFP_KERNEL)?; + devres::register::<_, core::convert::Infallible>(dev, reg, GFP_KER= NEL)?; =20 // SAFETY: Since `reg` was passed to devres::register(), the devic= e now owns the lifetime - // of the DRM registration - ensuring that this references lives f= or at least as long as 'a. + // of the DRM registration - ensuring that this reference lives for + // at least as long as 'bound. Ok(unsafe { drm.as_ref() }) } =20 /// Returns a reference to the `Device` instance for this registration. pub fn device(&self) -> &drm::Device { - &self.0 + &self.drm } } =20 // SAFETY: `Registration` doesn't offer any methods or access to fields wh= en shared between // threads, hence it's safe to share it. -unsafe impl Sync for Registration {} +unsafe impl Sync for Registration where + for<'a> ::Of<'a>: Send +{ +} =20 // SAFETY: Registration with and unregistration from the DRM subsystem can= happen from any thread. -unsafe impl Send for Registration {} +unsafe impl Send for Registration where + for<'a> ::Of<'a>: Send +{ +} =20 impl Drop for Registration { fn drop(&mut self) { @@ -195,6 +247,9 @@ fn drop(&mut self) { // // SAFETY: Safe by the invariant of `ARef>`. The ex= istence of this // `Registration` also guarantees that this `drm::Device` is actua= lly registered. - unsafe { bindings::drm_dev_unplug(self.0.as_raw()) }; + unsafe { bindings::drm_dev_unplug(self.drm.as_raw()) }; + // After drm_dev_unplug(), the SRCU barrier guarantees that all Un= bindGuard critical + // sections have completed, so no one holds a reference to reg_dat= a anymore. + // reg_data is dropped here automatically. } } diff --git a/rust/kernel/drm/mod.rs b/rust/kernel/drm/mod.rs index 64a43cb0fe57..6c0ba9c82b92 100644 --- a/rust/kernel/drm/mod.rs +++ b/rust/kernel/drm/mod.rs @@ -11,6 +11,7 @@ pub use self::device::Device; pub use self::device::DeviceContext; pub use self::device::Registered; +pub use self::device::UnbindGuard; pub use self::device::Uninit; pub use self::device::UnregisteredDevice; pub use self::driver::Driver; --=20 2.54.0 From nobody Sat Jun 13 15:11:34 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E42683B52E9; Wed, 6 May 2026 22:10:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778105457; cv=none; b=qdjUSU79YGXk4iKVKXKV80Awj9X4VPEfZTYe3rjGkSu85EFwQMXP3FF7YfA//Ut/S3Cs4AFlCb3o8xPk75kbtQeVMraj90bhPV+Id8Dr5+1aBx8w0aZ5u5q9CoPGsDyuAELmly3/wbflhbpBCKc5NsgfT8mvLQmROqYGL1hhnAE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778105457; c=relaxed/simple; bh=5z11vUM2l987/nev8SzbXOKwxTlBkMgkO23FG+Hi7kQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CNrSpOrbnW0qk5F/dzKqkTAExM7CiMyToWjLFfeepMq/PwJWa/csuwarmGujR6HjJF4UdHOajdf8z1BlbIf5IqebTQhp2IfwLMbFwf7hvTV9vjIXUf1/rCgGQVne7vV9oJ3zKys+FGWcrl2RIMaTsq2nkCcR+WRRrZsInV1Dxas= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WgSii8GN; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="WgSii8GN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 62279C2BCC4; Wed, 6 May 2026 22:10:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778105456; bh=5z11vUM2l987/nev8SzbXOKwxTlBkMgkO23FG+Hi7kQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WgSii8GNp6mTSfBPZjyHKef2aj/2DCoa9yAT+/MVTyXFgLUHOHEJCbex9oJWRougp JuXYl999I8AmmhgkdSBFL2kO5xdNzkK2a/Q5p3eBKVN0rj0EjGfL4holT1KNuBqaIe Ed4C2La5I44YhS7mCDfdZ0RmXs9h3eOGS9ZrDRCP5fuAGh5A/0oJFe2mvPju1gIWt3 aDWYW3sDWyyLN7Ro8izTTKDwU6/ZPvejvItoNNAnV+3zzFPQP+oDYMsONKF8gkOc6b nmT6E8SdGeH3DHR5ZQFhsicspkbaM1jvxN3b5ZUIPyQa74NHwHemPKI6o9KqnZEudN GmHL8uLJBbbBA== From: Danilo Krummrich To: aliceryhl@google.com, airlied@gmail.com, simona@ffwll.ch, daniel.almeida@collabora.com, acourbot@nvidia.com, apopple@nvidia.com, ecourtney@nvidia.com, deborah.brouwer@collabora.com, lyude@redhat.com, ojeda@kernel.org, boqun@kernel.org, gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org, a.hindborg@kernel.org, tmgross@umich.edu Cc: driver-core@lists.linux.dev, nova-gpu@lists.linux.dev, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Danilo Krummrich Subject: [PATCH 4/6] rust: drm: Wrap ioctl dispatch in UnbindGuard Date: Thu, 7 May 2026 00:06:02 +0200 Message-ID: <20260506221027.858481-5-dakr@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260506221027.858481-1-dakr@kernel.org> References: <20260506221027.858481-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Run every ioctl handler inside a drm_dev_enter/exit critical section via UnbindGuard. If the device has been unplugged, the ioctl returns ENODEV without calling the handler. A free-function wrapper is added because the macro context prevents method resolution from inferring the driver type. Signed-off-by: Danilo Krummrich --- rust/kernel/drm/device.rs | 7 +++++++ rust/kernel/drm/ioctl.rs | 13 +++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/rust/kernel/drm/device.rs b/rust/kernel/drm/device.rs index 11edbe6f9f42..0049ea69f716 100644 --- a/rust/kernel/drm/device.rs +++ b/rust/kernel/drm/device.rs @@ -452,6 +452,13 @@ fn drop(&mut self) { } } =20 +/// Free-function equivalent of [`Device::unbind_guard()`] for use in macr= o contexts where method +/// resolution cannot infer the driver type. +#[doc(hidden)] +pub fn unbind_guard(dev: &Device) -> Option= > { + dev.unbind_guard() +} + impl Deref for Device { type Target =3D T::Data; =20 diff --git a/rust/kernel/drm/ioctl.rs b/rust/kernel/drm/ioctl.rs index cf328101dde4..aa72b44f645d 100644 --- a/rust/kernel/drm/ioctl.rs +++ b/rust/kernel/drm/ioctl.rs @@ -87,7 +87,10 @@ pub mod internal { /// file: &kernel::drm::File, /// ) -> Result /// ``` -/// where `Self` is the drm::drv::Driver implementation these ioctls are b= eing declared within. +/// where `Self` is the `drm::Driver` implementation these ioctls are bein= g declared within. +/// +/// The ioctl runs inside a `drm_dev_enter/exit` critical section. If the = device has been +/// unplugged, the ioctl returns `ENODEV` without calling the handler. /// /// # Examples /// @@ -134,7 +137,13 @@ macro_rules! declare_drm_ioctls { // FIXME: Currently there is nothing enforcing= that the types of the // dev/file match the current driver these ioc= tls are being declared // for, and it's not clear how to enforce this= within the type system. - let dev =3D $crate::drm::device::Device::from_= raw(raw_dev); + let dev =3D unsafe { + $crate::drm::device::Device::from_raw(raw_= dev) + }; + let _guard =3D match $crate::drm::device::unbi= nd_guard(dev) { + Some(g) =3D> g, + None =3D> return $crate::error::code::ENOD= EV.to_errno(), + }; // SAFETY: The ioctl argument has size `_IOC_S= IZE(cmd)`, which we // asserted above matches the size of this typ= e, and all bit patterns of // UAPI structs must be valid. --=20 2.54.0 From nobody Sat Jun 13 15:11:34 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8D2603B4EA0; Wed, 6 May 2026 22:11:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778105461; cv=none; b=qfojYTYaTqOuWZx2AUy6cOKYYOH8RWRedevhb60AJpQLQ5jeEhYBdsKl37Cy6uJi2rPgwCLn79PAUwTZHctFFqyrdxI+6l3DlwUktwM+44446D/edDmtg5no762QTr6A+LNdzvawEEE+NsO5j/0RBIEuklAimQxjUrlXLct3f2U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778105461; c=relaxed/simple; bh=4gpeNkYznkS5u18sTwi1hOTl/D8Z4CKRgi4PFia4sX8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YLb/oaTcdsk7qt0mYa88l9nQqHBBxM8dPcI9xIks1Rzc1kyjAuQPycfiqj+40KWypOVUe3M77c0k/sKiOVlvMEqR2I10N7e1CmeMZ9wo1wmzAI5gdBTIlIuG7WHdHWeEKkAeEY2wsG+oFQ/0M2lfZyH0c36L/aCs1lugspcaCns= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=s36YLkep; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="s36YLkep" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0AFC0C2BCB0; Wed, 6 May 2026 22:10:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778105461; bh=4gpeNkYznkS5u18sTwi1hOTl/D8Z4CKRgi4PFia4sX8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=s36YLkep2+aju+NWosG/+wea1gVKc2iTCHedYgx8H38jG7/upiSSM9xpw106qi5Ai G+B842X4hep/2hkZMB0F+ckFnOaSUWLRF+v8XIYxv6Chv+5IEzdjcNnbNFdllZ2wA6 eGe0xde8ksohDCHfUVzEwXr4ZxEiJMp4AmVnB4LQ6GYgWEUrL7ENgzTCZtglmmxJC1 9aEv5eg5fnHMkWEfill6/0AnCS3hZKMuwNhVbbC90iEJxWeUJDPcvGT7LjSNdiOfN8 00jKV/5OTFIbNfoRnitTYYdkNkDxwsO8vSbGGn9ZVYKBMpV5DS0wIU4tJH0JdgN3ag VIncg6kBrfJDA== From: Danilo Krummrich To: aliceryhl@google.com, airlied@gmail.com, simona@ffwll.ch, daniel.almeida@collabora.com, acourbot@nvidia.com, apopple@nvidia.com, ecourtney@nvidia.com, deborah.brouwer@collabora.com, lyude@redhat.com, ojeda@kernel.org, boqun@kernel.org, gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org, a.hindborg@kernel.org, tmgross@umich.edu Cc: driver-core@lists.linux.dev, nova-gpu@lists.linux.dev, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Danilo Krummrich Subject: [PATCH 5/6] rust: drm: Pass registration data to ioctl handlers Date: Thu, 7 May 2026 00:06:03 +0200 Message-ID: <20260506221027.858481-6-dakr@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260506221027.858481-1-dakr@kernel.org> References: <20260506221027.858481-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Pass &::Of<'_> after the device argument to ioctl handlers. The lifetime is shortened from 'static via ForLt::cast_ref; the reference is valid for the duration of the drm_dev_enter/exit critical section held by UnbindGuard. Signed-off-by: Danilo Krummrich --- drivers/gpu/drm/nova/file.rs | 3 +++ drivers/gpu/drm/tyr/file.rs | 1 + rust/kernel/drm/ioctl.rs | 5 +++-- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/nova/file.rs b/drivers/gpu/drm/nova/file.rs index a3b7bd36792c..4c9fa409a8be 100644 --- a/drivers/gpu/drm/nova/file.rs +++ b/drivers/gpu/drm/nova/file.rs @@ -24,6 +24,7 @@ impl File { /// IOCTL: get_param: Query GPU / driver metadata. pub(crate) fn get_param( dev: &NovaDevice, + _reg_data: &(), getparam: &mut uapi::drm_nova_getparam, _file: &drm::File, ) -> Result { @@ -44,6 +45,7 @@ pub(crate) fn get_param( /// IOCTL: gem_create: Create a new DRM GEM object. pub(crate) fn gem_create( dev: &NovaDevice, + _reg_data: &(), req: &mut uapi::drm_nova_gem_create, file: &drm::File, ) -> Result { @@ -57,6 +59,7 @@ pub(crate) fn gem_create( /// IOCTL: gem_info: Query GEM metadata. pub(crate) fn gem_info( _dev: &NovaDevice, + _reg_data: &(), req: &mut uapi::drm_nova_gem_info, file: &drm::File, ) -> Result { diff --git a/drivers/gpu/drm/tyr/file.rs b/drivers/gpu/drm/tyr/file.rs index 31411da203c5..6262114c6a8d 100644 --- a/drivers/gpu/drm/tyr/file.rs +++ b/drivers/gpu/drm/tyr/file.rs @@ -29,6 +29,7 @@ fn open(_dev: &drm::Device) -> Result>> { impl TyrDrmFileData { pub(crate) fn dev_query( ddev: &TyrDrmDevice, + _reg_data: &(), devquery: &mut uapi::drm_panthor_dev_query, _file: &TyrDrmFile, ) -> Result { diff --git a/rust/kernel/drm/ioctl.rs b/rust/kernel/drm/ioctl.rs index aa72b44f645d..dd177bc399b2 100644 --- a/rust/kernel/drm/ioctl.rs +++ b/rust/kernel/drm/ioctl.rs @@ -83,6 +83,7 @@ pub mod internal { /// /// ```ignore /// fn foo(device: &kernel::drm::Device, +/// reg_data: &::Of= <'_>, /// data: &mut uapi::argument_type, /// file: &kernel::drm::File, /// ) -> Result @@ -140,7 +141,7 @@ macro_rules! declare_drm_ioctls { let dev =3D unsafe { $crate::drm::device::Device::from_raw(raw_= dev) }; - let _guard =3D match $crate::drm::device::unbi= nd_guard(dev) { + let guard =3D match $crate::drm::device::unbin= d_guard(dev) { Some(g) =3D> g, None =3D> return $crate::error::code::ENOD= EV.to_errno(), }; @@ -156,7 +157,7 @@ macro_rules! declare_drm_ioctls { // SAFETY: This is just the DRM file structure let file =3D unsafe { $crate::drm::File::from_= raw(raw_file) }; =20 - match $func(dev, data, file) { + match $func(dev, guard.registration_data(), da= ta, file) { Err(e) =3D> e.to_errno(), Ok(i) =3D> i.try_into() .unwrap_or($crate::error::code= ::ERANGE.to_errno()), --=20 2.54.0 From nobody Sat Jun 13 15:11:34 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A745C3537C0; Wed, 6 May 2026 22:11:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778105466; cv=none; b=u/VRXVCc3GIhqmOSzf7mETLgGRjC+WZF0bzZBO0vGi8IINSzzZce3eqqykeltcsVYpb9bW8w9aKYEDJoT1eUKTXLauYyffcwd9eamcqY3kQ3NY0gPSHawec/GfpAfmJgUyhJSgeD8rMiIR4GqNeS6WKU9bhhZyLnxUCLRLoCjbk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778105466; c=relaxed/simple; bh=WqnqVDXE7PEFLewRIxuny1MyhNaqsV7/dGlY5N1/SHA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=txwpN6seaFXiq1ZP62d60NHei0wXfyD0tfKhbiwox2Ifwrsw4Bs93pnm6W3BgonKpFz81c62f9cUn7Dq11s6w0h7/CrtqZIo+txvHzVAAmaFLyCTN/0BHMPZBLQFT3WRcmBBc/7XzZVGyVCzx9dZ9R6AslJfWqZ68uarlNgXPOA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DyJKl05g; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="DyJKl05g" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A83AFC2BCC4; Wed, 6 May 2026 22:11:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778105465; bh=WqnqVDXE7PEFLewRIxuny1MyhNaqsV7/dGlY5N1/SHA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DyJKl05gBAQGXeOfRoVsLh+QTk+kni40nVqUpz1StnfzGOKTDskr7NobSPIsPFHsx mDDhctvxjgilowaWmV05iQ+HCzMZOUT39rhK3HapJmzw2dH0u59dnchXL30IS15n4H Khu4umK4WINDOoDsuq7WmaXF+nQDwCgSGjo/4/TYMZ/P2ndwMmhRr06AvBFimkzcjg GK/RpiC6yMxCP2/7cQP2bYlhFWbeF7QaUVrmdS+qzlhBWkrGy19upft12z8LRMwJs9 7WoH2Qazd6t4CMG3QCia4Bu7eQ+bI3/neaREJD3r0iYU9zqxP2dIGOYEKrV5M1/t+b etGHLjDWrLbFw== From: Danilo Krummrich To: aliceryhl@google.com, airlied@gmail.com, simona@ffwll.ch, daniel.almeida@collabora.com, acourbot@nvidia.com, apopple@nvidia.com, ecourtney@nvidia.com, deborah.brouwer@collabora.com, lyude@redhat.com, ojeda@kernel.org, boqun@kernel.org, gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org, a.hindborg@kernel.org, tmgross@umich.edu Cc: driver-core@lists.linux.dev, nova-gpu@lists.linux.dev, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Danilo Krummrich Subject: [PATCH 6/6] rust: drm: Pass bound parent device to ioctl handlers Date: Thu, 7 May 2026 00:06:04 +0200 Message-ID: <20260506221027.858481-7-dakr@kernel.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260506221027.858481-1-dakr@kernel.org> References: <20260506221027.858481-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Pass &Self::ParentDevice as the second argument to ioctl handlers, obtained from the UnbindGuard via Deref. This gives ioctl handlers direct access to the bound parent bus device. Signed-off-by: Danilo Krummrich --- drivers/gpu/drm/nova/file.rs | 5 +++++ drivers/gpu/drm/tyr/file.rs | 3 +++ rust/kernel/drm/ioctl.rs | 3 ++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/nova/file.rs b/drivers/gpu/drm/nova/file.rs index 4c9fa409a8be..df9760947d58 100644 --- a/drivers/gpu/drm/nova/file.rs +++ b/drivers/gpu/drm/nova/file.rs @@ -4,6 +4,8 @@ use crate::gem::NovaObject; use kernel::{ alloc::flags::*, + auxiliary, + device::Bound, drm::{self, gem::BaseObject}, pci, prelude::*, @@ -24,6 +26,7 @@ impl File { /// IOCTL: get_param: Query GPU / driver metadata. pub(crate) fn get_param( dev: &NovaDevice, + _adev: &auxiliary::Device, _reg_data: &(), getparam: &mut uapi::drm_nova_getparam, _file: &drm::File, @@ -45,6 +48,7 @@ pub(crate) fn get_param( /// IOCTL: gem_create: Create a new DRM GEM object. pub(crate) fn gem_create( dev: &NovaDevice, + _adev: &auxiliary::Device, _reg_data: &(), req: &mut uapi::drm_nova_gem_create, file: &drm::File, @@ -59,6 +63,7 @@ pub(crate) fn gem_create( /// IOCTL: gem_info: Query GEM metadata. pub(crate) fn gem_info( _dev: &NovaDevice, + _adev: &auxiliary::Device, _reg_data: &(), req: &mut uapi::drm_nova_gem_info, file: &drm::File, diff --git a/drivers/gpu/drm/tyr/file.rs b/drivers/gpu/drm/tyr/file.rs index 6262114c6a8d..9f53da7633ab 100644 --- a/drivers/gpu/drm/tyr/file.rs +++ b/drivers/gpu/drm/tyr/file.rs @@ -1,7 +1,9 @@ // SPDX-License-Identifier: GPL-2.0 or MIT =20 use kernel::{ + device, drm, + platform, prelude::*, uaccess::UserSlice, uapi, // @@ -29,6 +31,7 @@ fn open(_dev: &drm::Device) -> Result>> { impl TyrDrmFileData { pub(crate) fn dev_query( ddev: &TyrDrmDevice, + _pdev: &platform::Device, _reg_data: &(), devquery: &mut uapi::drm_panthor_dev_query, _file: &TyrDrmFile, diff --git a/rust/kernel/drm/ioctl.rs b/rust/kernel/drm/ioctl.rs index dd177bc399b2..181918303f2f 100644 --- a/rust/kernel/drm/ioctl.rs +++ b/rust/kernel/drm/ioctl.rs @@ -83,6 +83,7 @@ pub mod internal { /// /// ```ignore /// fn foo(device: &kernel::drm::Device, +/// parent: &Self::ParentDevice, /// reg_data: &::Of= <'_>, /// data: &mut uapi::argument_type, /// file: &kernel::drm::File, @@ -157,7 +158,7 @@ macro_rules! declare_drm_ioctls { // SAFETY: This is just the DRM file structure let file =3D unsafe { $crate::drm::File::from_= raw(raw_file) }; =20 - match $func(dev, guard.registration_data(), da= ta, file) { + match $func(dev, &*guard, guard.registration_d= ata(), data, file) { Err(e) =3D> e.to_errno(), Ok(i) =3D> i.try_into() .unwrap_or($crate::error::code= ::ERANGE.to_errno()), --=20 2.54.0