diff options
| author | Danilo Krummrich <dakr@kernel.org> | 2025-10-16 14:55:28 +0200 |
|---|---|---|
| committer | Danilo Krummrich <dakr@kernel.org> | 2025-10-21 18:40:48 +0200 |
| commit | 0242623384c767b1156b61b67894b4ecf6682b8b (patch) | |
| tree | af5131064b1aac4ecdafe8884a95464429fab020 /rust/kernel/device.rs | |
| parent | fc2b38de4c01710ecb9ebb9ecdce9a7bf433e9a8 (diff) | |
rust: driver: let probe() return impl PinInit<Self, Error>
The driver model defines the lifetime of the private data stored in (and
owned by) a bus device to be valid from when the driver is bound to a
device (i.e. from successful probe()) until the driver is unbound from
the device.
This is already taken care of by the Rust implementation of the driver
model. However, we still ask drivers to return a Result<Pin<KBox<Self>>>
from probe().
Unlike in C, where we do not have the concept of initializers, but
rather deal with uninitialized memory, drivers can just return an
impl PinInit<Self, Error> instead.
This contributes to more clarity to the fact that a driver returns it's
device private data in probe() and the Rust driver model owns the data,
manages the lifetime and - considering the lifetime - provides (safe)
accessors for the driver.
Hence, let probe() functions return an impl PinInit<Self, Error> instead
of Result<Pin<KBox<Self>>>.
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
Diffstat (limited to 'rust/kernel/device.rs')
| -rw-r--r-- | rust/kernel/device.rs | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index 1321e6f0b53c..23a95324cb0f 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -6,6 +6,7 @@ use crate::{ bindings, fmt, + prelude::*, sync::aref::ARef, types::{ForeignOwnable, Opaque}, }; @@ -198,9 +199,13 @@ impl Device { impl Device<CoreInternal> { /// Store a pointer to the bound driver's private data. - pub fn set_drvdata(&self, data: impl ForeignOwnable) { + pub fn set_drvdata<T: 'static>(&self, data: impl PinInit<T, Error>) -> Result { + let data = KBox::pin_init(data, GFP_KERNEL)?; + // 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()) } + unsafe { bindings::dev_set_drvdata(self.as_raw(), data.into_foreign().cast()) }; + + Ok(()) } /// Take ownership of the private data stored in this [`Device`]. |