[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250621195118.124245-4-dakr@kernel.org>
Date: Sat, 21 Jun 2025 21:43:29 +0200
From: Danilo Krummrich <dakr@...nel.org>
To: 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,
Danilo Krummrich <dakr@...nel.org>
Subject: [PATCH 3/8] rust: platform: use generic device drvdata accessors
Take advantage of the generic drvdata accessors of the generic Device
type.
While at it, use from_result() instead of match.
Signed-off-by: Danilo Krummrich <dakr@...nel.org>
---
rust/helpers/platform.c | 10 ----------
rust/kernel/platform.rs | 36 +++++++++++++++++-------------------
2 files changed, 17 insertions(+), 29 deletions(-)
diff --git a/rust/helpers/platform.c b/rust/helpers/platform.c
index 82171233d12f..1ce89c1a36f7 100644
--- a/rust/helpers/platform.c
+++ b/rust/helpers/platform.c
@@ -2,16 +2,6 @@
#include <linux/platform_device.h>
-void *rust_helper_platform_get_drvdata(const struct platform_device *pdev)
-{
- return platform_get_drvdata(pdev);
-}
-
-void rust_helper_platform_set_drvdata(struct platform_device *pdev, void *data)
-{
- platform_set_drvdata(pdev, data);
-}
-
bool rust_helper_dev_is_platform(const struct device *dev)
{
return dev_is_platform(dev);
diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs
index 5b21fa517e55..dc0c36d70963 100644
--- a/rust/kernel/platform.rs
+++ b/rust/kernel/platform.rs
@@ -6,11 +6,11 @@
use crate::{
bindings, container_of, device, driver,
- error::{to_result, Result},
+ error::{from_result, to_result, Result},
of,
prelude::*,
str::CStr,
- types::{ForeignOwnable, Opaque},
+ types::Opaque,
ThisModule,
};
@@ -61,30 +61,28 @@ extern "C" fn probe_callback(pdev: *mut bindings::platform_device) -> kernel::ff
// `struct platform_device`.
//
// INVARIANT: `pdev` is valid for the duration of `probe_callback()`.
- let pdev = unsafe { &*pdev.cast::<Device<device::Core>>() };
-
+ let pdev = unsafe { &*pdev.cast::<Device<device::Internal>>() };
let info = <Self as driver::Adapter>::id_info(pdev.as_ref());
- match T::probe(pdev, info) {
- Ok(data) => {
- // Let the `struct platform_device` own a reference of the driver's private data.
- // SAFETY: By the type invariant `pdev.as_raw` returns a valid pointer to a
- // `struct platform_device`.
- unsafe { bindings::platform_set_drvdata(pdev.as_raw(), data.into_foreign() as _) };
- }
- Err(err) => return Error::to_errno(err),
- }
- 0
+ from_result(|| {
+ let data = T::probe(pdev, info)?;
+
+ pdev.as_ref().set_drvdata(data);
+ Ok(0)
+ })
}
extern "C" fn remove_callback(pdev: *mut bindings::platform_device) {
- // SAFETY: `pdev` is a valid pointer to a `struct platform_device`.
- let ptr = unsafe { bindings::platform_get_drvdata(pdev) }.cast();
+ // SAFETY: The platform bus only ever calls the remove callback with a valid pointer to a
+ // `struct platform_device`.
+ //
+ // INVARIANT: `pdev` is valid for the duration of `probe_callback()`.
+ let pdev = unsafe { &*pdev.cast::<Device<device::Internal>>() };
// SAFETY: `remove_callback` is only ever called after a successful call to
- // `probe_callback`, hence it's guaranteed that `ptr` points to a valid and initialized
- // `KBox<T>` pointer created through `KBox::into_foreign`.
- let _ = unsafe { KBox::<T>::from_foreign(ptr) };
+ // `probe_callback`, hence it's guaranteed that `Device::set_drvdata()` has been called
+ // and stored a `Pin<KBox<T>>`.
+ let _ = unsafe { pdev.as_ref().drvdata_obtain::<Pin<KBox<T>>>() };
}
}
--
2.49.0
Powered by blists - more mailing lists