[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <DB42TQY2E57U.1PKC16LW38MH9@kernel.org>
Date: Sat, 05 Jul 2025 13:15:06 +0200
From: "Benno Lossin" <lossin@...nel.org>
To: "Danilo Krummrich" <dakr@...nel.org>, <gregkh@...uxfoundation.org>,
<rafael@...nel.org>, <ojeda@...nel.org>, <alex.gaynor@...il.com>,
<boqun.feng@...il.com>, <gary@...yguo.net>, <bjorn3_gh@...tonmail.com>,
<benno.lossin@...ton.me>, <a.hindborg@...nel.org>, <aliceryhl@...gle.com>,
<tmgross@...ch.edu>, <david.m.ertman@...el.com>, <ira.weiny@...el.com>,
<leon@...nel.org>, <kwilczynski@...nel.org>, <bhelgaas@...gle.com>
Cc: <rust-for-linux@...r.kernel.org>, <linux-kernel@...r.kernel.org>,
<linux-pci@...r.kernel.org>
Subject: Re: [PATCH 2/8] rust: device: add drvdata accessors
On Sat Jun 21, 2025 at 9:43 PM CEST, Danilo Krummrich wrote:
> +impl Device<Internal> {
> + /// Store a pointer to the bound driver's private data.
> + pub fn set_drvdata(&self, data: impl ForeignOwnable) {
> + // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.
> + unsafe { bindings::dev_set_drvdata(self.as_raw(), data.into_foreign().cast()) }
> + }
> +
> + /// Take ownership of the private data stored in this [`Device`].
> + ///
> + /// # Safety
> + ///
> + /// - Must only be called once after a preceding call to [`Device::set_drvdata`].
> + /// - The type `T` must match the type of the `ForeignOwnable` previously stored by
> + /// [`Device::set_drvdata`].
> + pub unsafe fn drvdata_obtain<T: ForeignOwnable>(&self) -> T {
> + // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.
> + let ptr = unsafe { bindings::dev_get_drvdata(self.as_raw()) };
> +
> + // SAFETY: By the safety requirements of this function, `ptr` comes from a previous call to
> + // `into_foreign()`.
Well, you're also relying on `dev_get_drvdata` to return the same
pointer that was given to `dev_set_drvdata`.
Otherwise the safety docs look fine.
---
Cheers,
Benno
> + unsafe { T::from_foreign(ptr.cast()) }
> + }
> +
> + /// Borrow the driver's private data bound to this [`Device`].
> + ///
> + /// # Safety
> + ///
> + /// - Must only be called after a preceding call to [`Device::set_drvdata`] and before
> + /// [`Device::drvdata_obtain`].
> + /// - The type `T` must match the type of the `ForeignOwnable` previously stored by
> + /// [`Device::set_drvdata`].
> + pub unsafe fn drvdata_borrow<T: ForeignOwnable>(&self) -> T::Borrowed<'_> {
> + // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.
> + let ptr = unsafe { bindings::dev_get_drvdata(self.as_raw()) };
> +
> + // SAFETY: By the safety requirements of this function, `ptr` comes from a previous call to
> + // `into_foreign()`.
> + unsafe { T::borrow(ptr.cast()) }
> + }
> +}
> +
> impl<Ctx: DeviceContext> Device<Ctx> {
> /// Obtain the raw `struct device *`.
> pub(crate) fn as_raw(&self) -> *mut bindings::device {
Powered by blists - more mailing lists