aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanilo Krummrich <[email protected]>2024-06-19 15:39:17 +0200
committerGreg Kroah-Hartman <[email protected]>2024-06-20 16:14:58 +0200
commit4ead6c37b04aa35943ea270f09db18ebb38e63ff (patch)
treee803361a2bda01e6a2683dd518ca3608f7a34695
parent892fb846d6a04ad95c22ae8c758fd8998a0d237c (diff)
device: rust: improve safety comments
Improve the wording of safety comments to be more explicit about what exactly is guaranteed to be valid. Suggested-by: Benno Lossin <[email protected]> Signed-off-by: Danilo Krummrich <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
-rw-r--r--rust/kernel/device.rs9
1 files changed, 6 insertions, 3 deletions
diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
index e445e87fb7d7..851018eef885 100644
--- a/rust/kernel/device.rs
+++ b/rust/kernel/device.rs
@@ -30,8 +30,10 @@ use core::ptr;
///
/// # 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 @@ impl Device {
// 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)) }
}