[PATCH] rust: device: add platdata accessors

1064094935@qq.com posted 1 patch 1 month ago
There is a newer version of this series
rust/kernel/device.rs | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
[PATCH] rust: device: add platdata accessors
Posted by 1064094935@qq.com 1 month ago
From: pengfuyuan <pengfuyuan@kylinos.cn>

Implement generic accessors for the platform data of a device.

Platform data is typically set by platform code when creating the device
and points to platform-specific data structures. The accessor provides
type-safe access to this data without requiring unsafe code at the call
site.

The accessor is implemented for Device<Bound>, allowing drivers to access
platform data during probe() and other device lifecycle callbacks. Unlike
drvdata, platform data is managed by platform code and has a lifetime
tied to the device itself.

Signed-off-by: pengfuyuan <pengfuyuan@kylinos.cn>
---
 rust/kernel/device.rs | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
index c79be2e2bfe3..e16f8eaa52e0 100644
--- a/rust/kernel/device.rs
+++ b/rust/kernel/device.rs
@@ -325,6 +325,31 @@ pub fn drvdata<T: 'static>(&self) -> Result<Pin<&T>> {
         // - We've just checked that the type of the driver's private data is in fact `T`.
         Ok(unsafe { self.drvdata_unchecked() })
     }
+
+    /// Access the platform data for this device.
+    ///
+    /// The lifetime of the platform data is tied to the device's lifetime.
+    /// Returns a reference to the platform data of type `T`, or [`ENOENT`] if no platform data
+    /// is set.
+    ///
+    /// # Type Safety
+    ///
+    /// This function does not perform runtime type checking. The caller must ensure that the
+    /// platform data structure actually matches the type `T` for the specific platform device.
+    /// Incorrect type usage will result in undefined behavior.
+    pub fn platdata<T>(&self) -> Result<&T> {
+        // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.
+        let ptr = unsafe { (*self.as_raw()).platform_data };
+
+        if ptr.is_null() {
+            return Err(ENOENT);
+        }
+
+        // SAFETY:
+        // - `ptr` is not null, so it points to valid memory.
+        // - The caller must ensure that the platform data structure matches type `T`.
+        Ok(unsafe { &*ptr.cast::<T>() })
+    }
 }
 
 impl<Ctx: DeviceContext> Device<Ctx> {
-- 
2.25.1
Re: [PATCH] rust: device: add platdata accessors
Posted by Greg Kroah-Hartman 1 month ago
On Thu, Jan 08, 2026 at 03:42:51PM +0800, 1064094935@qq.com wrote:
> From: pengfuyuan <pengfuyuan@kylinos.cn>

Please use your "real" name (you can use native character sets if
wanted.)

> Implement generic accessors for the platform data of a device.
> 
> Platform data is typically set by platform code when creating the device
> and points to platform-specific data structures. The accessor provides
> type-safe access to this data without requiring unsafe code at the call
> site.
> 
> The accessor is implemented for Device<Bound>, allowing drivers to access
> platform data during probe() and other device lifecycle callbacks. Unlike
> drvdata, platform data is managed by platform code and has a lifetime
> tied to the device itself.

Do you have a user for this?  That would be good to see at the same time
here, we can't just add apis with no real users.

thanks,

greg k-h