lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20260109080528.478731-2-pengfuyuan@kylinos.cn>
Date: Fri,  9 Jan 2026 16:05:28 +0800
From: pengfuyuan <pengfuyuan@...inos.cn>
To: Greg Kroah-Hartman <gregkh@...uxfoundation.org>
Cc: "Rafael J . Wysocki" <rafael@...nel.org>,
	Danilo Krummrich <dakr@...nel.org>,
	Miguel Ojeda <ojeda@...nel.org>,
	Boqun Feng <boqun.feng@...il.com>,
	Gary Guo <gary@...yguo.net>,
	Björn Roy Baron <bjorn3_gh@...tonmail.com>,
	Benno Lossin <lossin@...nel.org>,
	Andreas Hindborg <a.hindborg@...nel.org>,
	Alice Ryhl <aliceryhl@...gle.com>,
	Trevor Gross <tmgross@...ch.edu>,
	rust-for-linux@...r.kernel.org,
	linux-kernel@...r.kernel.org,
	pengfuyuan <pengfuyuan@...inos.cn>
Subject: [PATCH v2 v2 1/1] rust: device: add platdata accessors

Implement generic accessors for the platform data of a device.

Platform data is typically set by platform code when creating the device (e.g.
via `platform_device_add_data()`). Drivers may use it to obtain per-device,
platform-provided configuration.

The accessor is `unsafe` because the caller must ensure that the chosen `T`
matches the actual object referenced by `platform_data`.

Platform data is generally a C type, so the method returns `&Opaque<T>` to
avoid creating a Rust reference to potentially uninitialised or otherwise
invalid C data. Drivers can then perform the FFI dereference behind an explicit
`unsafe` block.

The method is implemented for `Device<Ctx>` so it is available in all device
states. If no platform data is present, `-ENOENT` is returned.

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

diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
index c79be2e2bfe3..90ccc433dfe7 100644
--- a/rust/kernel/device.rs
+++ b/rust/kernel/device.rs
@@ -483,6 +483,37 @@ pub fn fwnode(&self) -> Option<&property::FwNode> {
         // defined as a `#[repr(transparent)]` wrapper around `fwnode_handle`.
         Some(unsafe { &*fwnode_handle.cast() })
     }
+
+    /// Access the platform data for this device.
+    ///
+    /// Platform data is typically set by platform code when creating the device and is expected
+    /// to remain valid while the device is alive.
+    ///
+    /// Returns a reference to the opaque platform data, or [`ENOENT`] if no platform data
+    /// is set.
+    ///
+    /// # Safety
+    ///
+    /// Callers must ensure that:
+    /// - If platform data is set (i.e., `platform_data` is not null), the pointer points to valid,
+    ///   properly aligned storage for `T` and remains valid for the lifetime of the returned
+    ///   reference.
+    /// - The type `T` matches the type of the platform data structure set by platform code.
+    pub unsafe fn platdata<T>(&self) -> Result<&Opaque<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 (checked above).
+        // - By the safety requirements of this function, `ptr` points to valid, properly aligned
+        //   storage for `T` and remains valid for the lifetime of the returned reference.
+        // - `Opaque<T>` allows any bit pattern, so we can safely create a reference to it.
+        Ok(unsafe { &*ptr.cast::<Opaque<T>>() })
+    }
 }
 
 // SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic
-- 
2.25.1


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ