From: christopherjoshy <christopherjoshy4@gmail.com>
To: ojeda@kernel.org
Cc: boqun@kernel.org, gary@garyguo.net, aliceryhl@google.com,
lyude@redhat.com, daniel.almeida@collabora.com,
work@onurozkan.dev, bjorn3_gh@protonmail.com, lossin@kernel.org,
a.hindborg@kernel.org, tmgross@umich.edu, dakr@kernel.org,
tamird@nvidia.com, acourbot@nvidia.com,
linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
Christopher Joshy <christopherjoshy4@gmail.com>
Subject: [PATCH v3] rust: kernel: add SAFETY comments in aref doc example
Date: Sun, 12 Jul 2026 21:19:13 +0530 [thread overview]
Message-ID: <20260712154913.1779-1-christopherjoshy4@gmail.com> (raw)
In-Reply-To: <20260712153028.896-1-christopherjoshy4@gmail.com>
From: Christopher Joshy <christopherjoshy4@gmail.com>
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` owns no
data and has no destructor, so the no-op `inc_ref`/`dec_ref` cannot
free or leak anything, and that this doc example keeps the backing
object alive for the `ARef`'s lifetime.
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@kernel.org>
Signed-off-by: Christopher Joshy <christopherjoshy4@gmail.com>
---
rust/kernel/sync/aref.rs | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/rust/kernel/sync/aref.rs b/rust/kernel/sync/aref.rs
index b721b2e00..3cf7bc27f 100644
--- a/rust/kernel/sync/aref.rs
+++ b/rust/kernel/sync/aref.rs
@@ -124,7 +124,10 @@ pub unsafe fn from_raw(ptr: NonNull<T>) -> Self {
///
/// struct Empty {}
///
- /// # // SAFETY: TODO.
+ /// // SAFETY: `Empty` owns no data and has no destructor, so its `inc_ref`/`dec_ref`
+ /// // are no-ops that cannot free or leak anything. This doc example keeps the backing
+ /// // `Empty` (`data`) alive for the whole lifetime of `data_ref`, so the `ARef` never
+ /// // dereferences freed memory.
/// unsafe impl AlwaysRefCounted for Empty {
/// fn inc_ref(&self) {}
/// unsafe fn dec_ref(_obj: NonNull<Self>) {}
@@ -132,7 +135,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
prev parent reply other threads:[~2026-07-12 15:49 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-12 12:35 [[PATCH]] rust: kernel: add SAFETY comments in aref doc example Christopher Joshy
2026-07-12 12:42 ` Miguel Ojeda
2026-07-12 15:30 ` [PATCH v2] " christopherjoshy
2026-07-12 15:49 ` christopherjoshy [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260712154913.1779-1-christopherjoshy4@gmail.com \
--to=christopherjoshy4@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=lyude@redhat.com \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@nvidia.com \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.