Skip to contentSkip to conversation

device: rust: improve safety comments

PATCH New Danilo Krummrich 1 participant 2 messages Jun 19, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 device: rust: improve safety comments

Improve the wording of safety comments to be more explicit about what exactly is guaranteed to be valid.

---
 rust/kernel/device.rs | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
rust/kernel/device.rs +6 -3
index e445e87fb7d7..851018eef885 100644
@@ -30,8 +30,10 @@
 ///
 /// # Invariants
 ///
-/// The pointer stored in `Self` is non-null and valid for the lifetime of the `ARef` instance. In
-/// particular, the `ARef` instance owns an increment on the underlying object’s reference count.
+/// A `Device` instance represents a valid `struct device` created by the C portion of the kernel.
+///
+/// Instances of this type are always reference-counted, that is, a call to `get_device` ensures
+/// that the allocation remains valid at least until the matching call to `put_device`.
 ///
 /// `bindings::device::release` is valid to be called from any thread, hence `ARef<Device>` can be
 /// dropped from any thread.
@@ -58,7 +60,8 @@ pub unsafe fn from_raw(ptr: *mut bindings::device) -> ARef<Self> {
         // CAST: `Self` is a `repr(transparent)` wrapper around `bindings::device`.
         let ptr = ptr.cast::<Self>();
 
-        // SAFETY: By the safety requirements, ptr is valid.
+        // SAFETY: `ptr` is valid by the safety requirements of this function. By the above call to
+        // `bindings::get_device` we also own a reference to the underlying `struct device`.
         unsafe { ARef::from_raw(ptr::NonNull::new_unchecked(ptr)) }
     }
 

base-commit: de6582833db0e695ba0c548e3cc2ad7dbb6aa260

Signature · 1 line
2.45.1
#1 lore raw

PATCH 2/2 MAINTAINERS: add Rust device abstractions to DRIVER CORE

Add missing file path of the Rust abstractions to the maintainers entry, until we can move it to 'drivers/base/'.

---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
index cf9c9221c388..6dc6f264f800 100644
@@ -6714,6 +6714,7 @@ F:	include/linux/fwnode.h
 F:	include/linux/kobj*
 F:	include/linux/property.h
 F:	lib/kobj*
+F:	rust/kernel/device.rs
 
 DRIVERS FOR OMAP ADAPTIVE VOLTAGE SCALING (AVS)
 M:	Nishanth Menon <[email protected]>
Signature · 1 line
2.45.1