Skip to contentSkip to conversation

rust: sync: extend module documentation of aref

PATCH Applied Benno Lossin 4 participants 5 messages Jul 22, 2025 – Sep 14, 2025

Subsystems (2)
lore
Share & tools
lore

Benno Lossin

author reviewer
#0 lore raw

PATCH 1/1 rust: sync: extend module documentation of aref

Commit 07dad44aa9a9 ("rust: kernel: move ARef and AlwaysRefCounted to sync::aref") moved ARef and AlwaysRefCounted into their own module. In that process only a short, single line description of the module was added. Extend the description by explaining what is meant by "internal reference counting", the two items in the trait & the difference to Arc.

---
 rust/kernel/sync/aref.rs | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
rust/kernel/sync/aref.rs +15 -0
index dbd77bb68617..1c212238c0e5 100644
@@ -1,6 +1,21 @@
 // SPDX-License-Identifier: GPL-2.0
 
 //! Internal reference counting support.
+//!
+//! Many C types already have their own reference counting mechanism (e.g. by storing a
+//! `refcount_t`). This module provides support for directly using their internal reference count
+//! from Rust; instead of making users have to use an additional Rust-reference count in the form of
+//! [`Arc`].
+//!
+//! The smart pointer [`ARef<T>`] acts similarly to [`Arc<T>`] in that it holds a refcount on the
+//! underlying object, but this refcount is internal to the object. It essentially is a Rust
+//! implementation of the `get_` and `put_` pattern used in C for reference counting.
+//!
+//! To make use of [`ARef<MyType>`], `MyType` needs to implement [`AlwaysRefCounted`]. It is a trait
+//! for accessing the internal reference count of an object of the `MyType` type.
+//!
+//! [`Arc`]: crate::sync::Arc
+//! [`Arc<T>`]: crate::sync::Arc
 
 use core::{marker::PhantomData, mem::ManuallyDrop, ops::Deref, ptr::NonNull};
 

base-commit: 07dad44aa9a93b16af19e8609a10b241c352b440

Signature · 1 line
2.50.0
#1 lore raw

Hi Benno,

Quoted 25 lines · Benno Lossin

Commit 07dad44aa9a9 ("rust: kernel: move ARef and AlwaysRefCounted to sync::aref") moved ARef and AlwaysRefCounted into their own module. In that process only a short, single line description of the module was added. Extend the description by explaining what is meant by "internal reference counting", the two items in the trait & the difference to Arc.

---
  rust/kernel/sync/aref.rs | 15 +++++++++++++++
  1 file changed, 15 insertions(+)
rust/kernel/sync/aref.rs +5 -0
index dbd77bb68617..1c212238c0e5 100644
@@ -1,6 +1,21 @@
  // SPDX-License-Identifier: GPL-2.0
 
  //! Internal reference counting support.
+//!
+//! Many C types already have their own reference counting mechanism (e.g. by storing a
+//! `refcount_t`). This module provides support for directly using their internal reference count
+//! from Rust; instead of making users have to use an additional Rust-reference count in the form of
+//! [`Arc`].

rust/kernel/sync/aref.rs line 18

Quoted 10 lines
rust/kernel/sync/aref.rs +10 -0
+//!
+//! The smart pointer [`ARef<T>`] acts similarly to [`Arc<T>`] in that it holds a refcount on the
+//! underlying object, but this refcount is internal to the object. It essentially is a Rust
+//! implementation of the `get_` and `put_` pattern used in C for reference counting.
+//!
+//! To make use of [`ARef<MyType>`], `MyType` needs to implement [`AlwaysRefCounted`]. It is a trait
+//! for accessing the internal reference count of an object of the `MyType` type.
+//!
+//! [`Arc`]: crate::sync::Arc
+//! [`Arc<T>`]: crate::sync::Arc

It got me curios. Why is it required to declare the doc reference for Arc and Arc<T>, but not ARef<MyType> and ARef<T>?

Is it because ARef is in file scope but not the Arc?

If so, you could just add

	use crate::sync::Arc;

in the file imports and you wouldn't have to duplicate the

	//! [`Arc`]: crate::sync::Arc
	//! [`Arc<T>`]: crate::sync::Arc

And even cleanup it a bit by simplifying

	[`Arc`](crate::sync::Arc) to [`Arc`]

in the AlwaysRefCounted trait.

Thanks!
Daniel

#2 lore raw
Quoted 9 lines · Daniel Sedlak

It got me curios. Why is it required to declare the doc reference for Arc and Arc<T>, but not ARef<MyType> and ARef<T>?

Is it because ARef is in file scope but not the Arc?

If so, you could just add

        use crate::sync::Arc;

That would be unused -- i.e. we don't add imports in the actual code only for docs.

The compiler also warns about it (unused_imports).

Thanks!

Cheers,
Miguel

reviewer Reviewed-by on v1 1/1
#3 lore raw
In reply to Benno Lossin
Quoted 25 lines · Benno Lossin

Commit 07dad44aa9a9 ("rust: kernel: move ARef and AlwaysRefCounted to sync::aref") moved ARef and AlwaysRefCounted into their own module. In that process only a short, single line description of the module was added. Extend the description by explaining what is meant by "internal reference counting", the two items in the trait & the difference to Arc.

---
 rust/kernel/sync/aref.rs | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
rust/kernel/sync/aref.rs +5 -0
index dbd77bb68617..1c212238c0e5 100644
@@ -1,6 +1,21 @@
 // SPDX-License-Identifier: GPL-2.0
 
 //! Internal reference counting support.
+//!
+//! Many C types already have their own reference counting mechanism (e.g. by storing a
+//! `refcount_t`). This module provides support for directly using their internal reference count
+//! from Rust; instead of making users have to use an additional Rust-reference count in the form of
+//! [`Arc`].

rust/kernel/sync/aref.rs line 18

Quoted 10 lines
rust/kernel/sync/aref.rs +10 -0
+//!
+//! The smart pointer [`ARef<T>`] acts similarly to [`Arc<T>`] in that it holds a refcount on the
+//! underlying object, but this refcount is internal to the object. It essentially is a Rust
+//! implementation of the `get_` and `put_` pattern used in C for reference counting.
+//!
+//! To make use of [`ARef<MyType>`], `MyType` needs to implement [`AlwaysRefCounted`]. It is a trait
+//! for accessing the internal reference count of an object of the `MyType` type.
+//!
+//! [`Arc`]: crate::sync::Arc
+//! [`Arc<T>`]: crate::sync::Arc

Very useful explanation IMHO.

Applied
#4 lore raw
In reply to Benno Lossin
Quoted 9 lines · Benno Lossin

Commit 07dad44aa9a9 ("rust: kernel: move ARef and AlwaysRefCounted to sync::aref") moved ARef and AlwaysRefCounted into their own module. In that process only a short, single line description of the module was added. Extend the description by explaining what is meant by "internal reference counting", the two items in the trait & the difference to Arc.

Applied to rust-next -- thanks everyone!

Cheers,
Miguel