[PATCH v2] rust: kernel: add SAFETY comments in aref doc example

From: christopherjoshy

Date: Sun Jul 12 2026 - 11:32:20 EST


From: Christopher Joshy <christopherjoshy4@xxxxxxxxx>

Add the missing `// SAFETY` comments in the `ARef::into_raw` doc
example, replacing the `TODO` placeholders from issue #351.

For the `unsafe impl AlwaysRefCounted`, explain that `Empty` has no
data and its `inc_ref`/`dec_ref` are no-ops, so the liveness
contract is trivially satisfied.

For the `ARef::from_raw` call, explain that `ptr` is derived from a
valid reference (so it is valid) and that the required refcount
increment is trivially owned because `Empty`'s `AlwaysRefCounted`
manages no actual refcount.

Link: https://github.com/Rust-for-Linux/linux/issues/351
Suggested-by: Miguel Ojeda <ojeda@xxxxxxxxxx>
Signed-off-by: Christopher Joshy <christopherjoshy4@xxxxxxxxx>
---
rust/kernel/sync/aref.rs | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/rust/kernel/sync/aref.rs b/rust/kernel/sync/aref.rs
index b721b2e00..5e8b8476b 100644
--- a/rust/kernel/sync/aref.rs
+++ b/rust/kernel/sync/aref.rs
@@ -124,7 +124,11 @@ pub unsafe fn from_raw(ptr: NonNull<T>) -> Self {
///
/// struct Empty {}
///
- /// # // SAFETY: TODO.
+ /// // SAFETY: `Empty` has no data and its `inc_ref`/`dec_ref` are no-ops, so the
+ /// // `AlwaysRefCounted` contract holds: `inc_ref` need not keep anything alive (the
+ /// // zero-sized object lives on the stack while `data` is in scope) and `dec_ref` frees
+ /// // nothing. The caller ensures `data` outlives any `ARef<Empty>` (here `data` is a
+ /// // local that outlives `data_ref`).
/// unsafe impl AlwaysRefCounted for Empty {
/// fn inc_ref(&self) {}
/// unsafe fn dec_ref(_obj: NonNull<Self>) {}
@@ -132,7 +136,10 @@ pub unsafe fn from_raw(ptr: NonNull<T>) -> Self {
///
/// let mut data = Empty {};
/// let ptr = NonNull::<Empty>::new(&mut data).unwrap();
- /// # // SAFETY: TODO.
+ /// // SAFETY: `ptr` comes from a valid `&mut data` reference, hence it is non-null,
+ /// // aligned and points to a valid `Empty`. `ARef::from_raw` also requires owning a
+ /// // refcount increment; this is trivially met because `Empty`'s `AlwaysRefCounted` uses
+ /// // no-op `inc_ref`/`dec_ref` and so manages no actual refcount.
/// let data_ref: ARef<Empty> = unsafe { ARef::from_raw(ptr) };
/// let raw_ptr: NonNull<Empty> = ARef::into_raw(data_ref);
///
--
2.43.0