Skip to content

Commit 4ead6c3

Browse files
dakrgregkh
authored andcommitted
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]>
1 parent 892fb84 commit 4ead6c3

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

rust/kernel/device.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ use core::ptr;
3030
///
3131
/// # Invariants
3232
///
33-
/// The pointer stored in `Self` is non-null and valid for the lifetime of the `ARef` instance. In
34-
/// particular, the `ARef` instance owns an increment on the underlying object’s reference count.
33+
/// A `Device` instance represents a valid `struct device` created by the C portion of the kernel.
34+
///
35+
/// Instances of this type are always reference-counted, that is, a call to `get_device` ensures
36+
/// that the allocation remains valid at least until the matching call to `put_device`.
3537
///
3638
/// `bindings::device::release` is valid to be called from any thread, hence `ARef<Device>` can be
3739
/// dropped from any thread.
@@ -58,7 +60,8 @@ impl Device {
5860
// CAST: `Self` is a `repr(transparent)` wrapper around `bindings::device`.
5961
let ptr = ptr.cast::<Self>();
6062

61-
// SAFETY: By the safety requirements, ptr is valid.
63+
// SAFETY: `ptr` is valid by the safety requirements of this function. By the above call to
64+
// `bindings::get_device` we also own a reference to the underlying `struct device`.
6265
unsafe { ARef::from_raw(ptr::NonNull::new_unchecked(ptr)) }
6366
}
6467

0 commit comments

Comments
 (0)