From mboxrd@z Thu Jan 1 00:00:00 1970 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: driver-core@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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 @@ 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 is guaranteed to be bound. + #[must_use] + pub fn unbind_guard(&self) -> Option> { + let mut idx: i32 = 0; + // SAFETY: `self.as_raw()` is a valid pointer to a `struct drm_device` 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 unplugged, 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 = 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 for the entire duration of +/// the critical section. +/// +/// Internally this is backed by a `drm_dev_enter()` / `drm_dev_exit()` SRCU 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 guard. +#[must_use] +pub struct UnbindGuard<'a, T: drm::Driver> { + dev: &'a Device, + idx: i32, +} + +impl Deref for UnbindGuard<'_, T> { + type Target = 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 = T::Data; 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 {} 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 unregistration proceeds. This + // is required for the safety of `UnbindGuard`, which relies on the 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 existence of this - // `Registration` also guarantees the this `drm::Device` is actually registered. - unsafe { bindings::drm_dev_unregister(self.0.as_raw()) }; + // `Registration` also guarantees that this `drm::Device` is actually registered. + unsafe { bindings::drm_dev_unplug(self.0.as_raw()) }; } } -- 2.54.0