* [PATCH v3] rust: aref: make `AlwaysRefCounted::inc_ref` an associated function
@ 2025-09-24 5:24 Trevor Chan
2025-09-24 8:31 ` Miguel Ojeda
0 siblings, 1 reply; 4+ messages in thread
From: Trevor Chan @ 2025-09-24 5:24 UTC (permalink / raw)
To: alex.gaynor, ojeda, gregkh, rafael, dakr, airlied, simona,
aliceryhl, vireshk, nm, sboyd, viro, brauner
Cc: boqun.feng, gary, bjorn3_gh, lossin, a.hindborg, tmgross,
rust-for-linux, david.m.ertman, ira.weiny, leon, lorenzo.stoakes,
Liam.Howlett, bhelgaas, kwilczynski, jack
`AlwaysRefCounted::inc_ref` is a function that shouldn't be called lightly.
To prevent accidentally calling it, `inc_ref` is now changed to be an
associated function.
Modifies all `AlwaysRefCounted` implementors to work with this change.
Suggested-by: Benno Lossin <lossin@kernel.org>
Closes: https://github.com/Rust-for-Linux/linux/issues/1177
Signed-off-by: Trevor Chan <trev@trevrosa.dev>
---
Changes in v2:
- Don't word wrap the patch
Changes in v3:
- Make argument name of `Empty::inc_ref` consistent with `Empty::dec_ref`
---
rust/kernel/auxiliary.rs | 4 ++--
rust/kernel/block/mq/request.rs | 4 ++--
rust/kernel/cred.rs | 4 ++--
rust/kernel/device.rs | 4 ++--
rust/kernel/device/property.rs | 4 ++--
rust/kernel/drm/device.rs | 4 ++--
rust/kernel/drm/gem/mod.rs | 4 ++--
rust/kernel/fs/file.rs | 8 ++++----
rust/kernel/mm.rs | 8 ++++----
rust/kernel/mm/mmput_async.rs | 4 ++--
rust/kernel/opp.rs | 4 ++--
rust/kernel/pci.rs | 4 ++--
rust/kernel/pid_namespace.rs | 4 ++--
rust/kernel/platform.rs | 4 ++--
rust/kernel/sync/aref.rs | 10 ++++++----
rust/kernel/task.rs | 4 ++--
16 files changed, 40 insertions(+), 38 deletions(-)
diff --git a/rust/kernel/auxiliary.rs b/rust/kernel/auxiliary.rs
index 4749fb6bffef..b205c9d4f057 100644
--- a/rust/kernel/auxiliary.rs
+++ b/rust/kernel/auxiliary.rs
@@ -246,9 +246,9 @@ extern "C" fn release(dev: *mut bindings::device) {
// SAFETY: Instances of `Device` are always reference-counted.
unsafe impl crate::types::AlwaysRefCounted for Device {
- fn inc_ref(&self) {
+ fn inc_ref(obj: &Self) {
// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero.
- unsafe { bindings::get_device(self.as_ref().as_raw()) };
+ unsafe { bindings::get_device(obj.as_ref().as_raw()) };
}
unsafe fn dec_ref(obj: NonNull<Self>) {
diff --git a/rust/kernel/block/mq/request.rs b/rust/kernel/block/mq/request.rs
index fefd394f064a..8124c72cd674 100644
--- a/rust/kernel/block/mq/request.rs
+++ b/rust/kernel/block/mq/request.rs
@@ -236,8 +236,8 @@ fn atomic_relaxed_op_unless(target: &AtomicU64, op: impl Fn(u64) -> u64, pred: u
// keeps the object alive in memory at least until a matching reference count
// decrement is executed.
unsafe impl<T: Operations> AlwaysRefCounted for Request<T> {
- fn inc_ref(&self) {
- let refcount = &self.wrapper_ref().refcount();
+ fn inc_ref(obj: &Self) {
+ let refcount = &obj.wrapper_ref().refcount();
#[cfg_attr(not(CONFIG_DEBUG_MISC), allow(unused_variables))]
let updated = atomic_relaxed_op_unless(refcount, |x| x + 1, 0);
diff --git a/rust/kernel/cred.rs b/rust/kernel/cred.rs
index 2599f01e8b28..6fb5d854c374 100644
--- a/rust/kernel/cred.rs
+++ b/rust/kernel/cred.rs
@@ -76,9 +76,9 @@ pub fn euid(&self) -> Kuid {
// SAFETY: The type invariants guarantee that `Credential` is always ref-counted.
unsafe impl AlwaysRefCounted for Credential {
#[inline]
- fn inc_ref(&self) {
+ fn inc_ref(obj: &Self) {
// SAFETY: The existence of a shared reference means that the refcount is nonzero.
- unsafe { bindings::get_cred(self.0.get()) };
+ unsafe { bindings::get_cred(obj.0.get()) };
}
#[inline]
diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
index b8613289de8e..63b2d47df833 100644
--- a/rust/kernel/device.rs
+++ b/rust/kernel/device.rs
@@ -293,9 +293,9 @@ pub fn fwnode(&self) -> Option<&property::FwNode> {
// SAFETY: Instances of `Device` are always reference-counted.
unsafe impl crate::types::AlwaysRefCounted for Device {
- fn inc_ref(&self) {
+ fn inc_ref(obj: &Self) {
// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero.
- unsafe { bindings::get_device(self.as_raw()) };
+ unsafe { bindings::get_device(obj.as_raw()) };
}
unsafe fn dec_ref(obj: ptr::NonNull<Self>) {
diff --git a/rust/kernel/device/property.rs b/rust/kernel/device/property.rs
index 49ee12a906db..ccb83856745e 100644
--- a/rust/kernel/device/property.rs
+++ b/rust/kernel/device/property.rs
@@ -359,10 +359,10 @@ fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
// SAFETY: Instances of `FwNode` are always reference-counted.
unsafe impl crate::types::AlwaysRefCounted for FwNode {
- fn inc_ref(&self) {
+ fn inc_ref(obj: &Self) {
// SAFETY: The existence of a shared reference guarantees that the
// refcount is non-zero.
- unsafe { bindings::fwnode_handle_get(self.as_raw()) };
+ unsafe { bindings::fwnode_handle_get(obj.as_raw()) };
}
unsafe fn dec_ref(obj: ptr::NonNull<Self>) {
diff --git a/rust/kernel/drm/device.rs b/rust/kernel/drm/device.rs
index 3bb7c83966cf..affbb58dad98 100644
--- a/rust/kernel/drm/device.rs
+++ b/rust/kernel/drm/device.rs
@@ -183,9 +183,9 @@ fn deref(&self) -> &Self::Target {
// SAFETY: DRM device objects are always reference counted and the get/put functions
// satisfy the requirements.
unsafe impl<T: drm::Driver> AlwaysRefCounted for Device<T> {
- fn inc_ref(&self) {
+ fn inc_ref(obj: &Self) {
// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero.
- unsafe { bindings::drm_dev_get(self.as_raw()) };
+ unsafe { bindings::drm_dev_get(obj.as_raw()) };
}
unsafe fn dec_ref(obj: NonNull<Self>) {
diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs
index b71821cfb5ea..febb6cc83fd9 100644
--- a/rust/kernel/drm/gem/mod.rs
+++ b/rust/kernel/drm/gem/mod.rs
@@ -56,9 +56,9 @@ pub trait IntoGEMObject: Sized + super::private::Sealed + AlwaysRefCounted {
// SAFETY: All gem objects are refcounted.
unsafe impl<T: IntoGEMObject> AlwaysRefCounted for T {
- fn inc_ref(&self) {
+ fn inc_ref(obj: &Self) {
// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero.
- unsafe { bindings::drm_gem_object_get(self.as_raw()) };
+ unsafe { bindings::drm_gem_object_get(obj.as_raw()) };
}
unsafe fn dec_ref(obj: NonNull<Self>) {
diff --git a/rust/kernel/fs/file.rs b/rust/kernel/fs/file.rs
index 35fd5db35c46..831327e65c94 100644
--- a/rust/kernel/fs/file.rs
+++ b/rust/kernel/fs/file.rs
@@ -192,9 +192,9 @@ unsafe impl Sync for File {}
// makes `ARef<File>` own a normal refcount.
unsafe impl AlwaysRefCounted for File {
#[inline]
- fn inc_ref(&self) {
+ fn inc_ref(obj: &Self) {
// SAFETY: The existence of a shared reference means that the refcount is nonzero.
- unsafe { bindings::get_file(self.as_ptr()) };
+ unsafe { bindings::get_file(obj.as_ptr()) };
}
#[inline]
@@ -228,9 +228,9 @@ pub struct LocalFile {
// makes `ARef<LocalFile>` own a normal refcount.
unsafe impl AlwaysRefCounted for LocalFile {
#[inline]
- fn inc_ref(&self) {
+ fn inc_ref(obj: &Self) {
// SAFETY: The existence of a shared reference means that the refcount is nonzero.
- unsafe { bindings::get_file(self.as_ptr()) };
+ unsafe { bindings::get_file(obj.as_ptr()) };
}
#[inline]
diff --git a/rust/kernel/mm.rs b/rust/kernel/mm.rs
index 43f525c0d16c..a030763c25fd 100644
--- a/rust/kernel/mm.rs
+++ b/rust/kernel/mm.rs
@@ -56,9 +56,9 @@ unsafe impl Sync for Mm {}
// SAFETY: By the type invariants, this type is always refcounted.
unsafe impl AlwaysRefCounted for Mm {
#[inline]
- fn inc_ref(&self) {
+ fn inc_ref(obj: &Self) {
// SAFETY: The pointer is valid since self is a reference.
- unsafe { bindings::mmgrab(self.as_raw()) };
+ unsafe { bindings::mmgrab(obj.as_raw()) };
}
#[inline]
@@ -92,9 +92,9 @@ unsafe impl Sync for MmWithUser {}
// SAFETY: By the type invariants, this type is always refcounted.
unsafe impl AlwaysRefCounted for MmWithUser {
#[inline]
- fn inc_ref(&self) {
+ fn inc_ref(obj: &Self) {
// SAFETY: The pointer is valid since self is a reference.
- unsafe { bindings::mmget(self.as_raw()) };
+ unsafe { bindings::mmget(obj.as_raw()) };
}
#[inline]
diff --git a/rust/kernel/mm/mmput_async.rs b/rust/kernel/mm/mmput_async.rs
index 9289e05f7a67..2d7117a76e33 100644
--- a/rust/kernel/mm/mmput_async.rs
+++ b/rust/kernel/mm/mmput_async.rs
@@ -36,9 +36,9 @@ unsafe impl Sync for MmWithUserAsync {}
// SAFETY: By the type invariants, this type is always refcounted.
unsafe impl AlwaysRefCounted for MmWithUserAsync {
#[inline]
- fn inc_ref(&self) {
+ fn inc_ref(obj: &Self) {
// SAFETY: The pointer is valid since self is a reference.
- unsafe { bindings::mmget(self.as_raw()) };
+ unsafe { bindings::mmget(obj.as_raw()) };
}
#[inline]
diff --git a/rust/kernel/opp.rs b/rust/kernel/opp.rs
index 08126035d2c6..e0b8802f3c5f 100644
--- a/rust/kernel/opp.rs
+++ b/rust/kernel/opp.rs
@@ -1043,9 +1043,9 @@ unsafe impl Sync for OPP {}
/// SAFETY: The type invariants guarantee that [`OPP`] is always refcounted.
unsafe impl AlwaysRefCounted for OPP {
- fn inc_ref(&self) {
+ fn inc_ref(obj: &Self) {
// SAFETY: The existence of a shared reference means that the refcount is nonzero.
- unsafe { bindings::dev_pm_opp_get(self.0.get()) };
+ unsafe { bindings::dev_pm_opp_get(obj.0.get()) };
}
unsafe fn dec_ref(obj: ptr::NonNull<Self>) {
diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs
index 887ee611b553..579b2405d808 100644
--- a/rust/kernel/pci.rs
+++ b/rust/kernel/pci.rs
@@ -456,9 +456,9 @@ impl crate::dma::Device for Device<device::Core> {}
// SAFETY: Instances of `Device` are always reference-counted.
unsafe impl crate::types::AlwaysRefCounted for Device {
- fn inc_ref(&self) {
+ fn inc_ref(obj: &Self) {
// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero.
- unsafe { bindings::pci_dev_get(self.as_raw()) };
+ unsafe { bindings::pci_dev_get(obj.as_raw()) };
}
unsafe fn dec_ref(obj: NonNull<Self>) {
diff --git a/rust/kernel/pid_namespace.rs b/rust/kernel/pid_namespace.rs
index 0e93808e4639..d3302b86b864 100644
--- a/rust/kernel/pid_namespace.rs
+++ b/rust/kernel/pid_namespace.rs
@@ -46,9 +46,9 @@ pub unsafe fn from_ptr<'a>(ptr: *const bindings::pid_namespace) -> &'a Self {
// SAFETY: Instances of `PidNamespace` are always reference-counted.
unsafe impl AlwaysRefCounted for PidNamespace {
#[inline]
- fn inc_ref(&self) {
+ fn inc_ref(obj: &Self) {
// SAFETY: The existence of a shared reference means that the refcount is nonzero.
- unsafe { bindings::get_pid_ns(self.as_ptr()) };
+ unsafe { bindings::get_pid_ns(obj.as_ptr()) };
}
#[inline]
diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs
index 8f028c76f9fa..04e650e6218f 100644
--- a/rust/kernel/platform.rs
+++ b/rust/kernel/platform.rs
@@ -293,9 +293,9 @@ impl crate::dma::Device for Device<device::Core> {}
// SAFETY: Instances of `Device` are always reference-counted.
unsafe impl crate::types::AlwaysRefCounted for Device {
- fn inc_ref(&self) {
+ fn inc_ref(obj: &Self) {
// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero.
- unsafe { bindings::get_device(self.as_ref().as_raw()) };
+ unsafe { bindings::get_device(obj.as_ref().as_raw()) };
}
unsafe fn dec_ref(obj: NonNull<Self>) {
diff --git a/rust/kernel/sync/aref.rs b/rust/kernel/sync/aref.rs
index dbd77bb68617..3a3d5052786c 100644
--- a/rust/kernel/sync/aref.rs
+++ b/rust/kernel/sync/aref.rs
@@ -24,7 +24,9 @@
/// alive.)
pub unsafe trait AlwaysRefCounted {
/// Increments the reference count on the object.
- fn inc_ref(&self);
+ ///
+ /// This function should not be called lightly.
+ fn inc_ref(obj: &Self);
/// Decrements the reference count on the object.
///
@@ -103,7 +105,7 @@ pub unsafe fn from_raw(ptr: NonNull<T>) -> Self {
///
/// # // SAFETY: TODO.
/// unsafe impl AlwaysRefCounted for Empty {
- /// fn inc_ref(&self) {}
+ /// fn inc_ref(_obj: &Self) {}
/// unsafe fn dec_ref(_obj: NonNull<Self>) {}
/// }
///
@@ -122,7 +124,7 @@ pub fn into_raw(me: Self) -> NonNull<T> {
impl<T: AlwaysRefCounted> Clone for ARef<T> {
fn clone(&self) -> Self {
- self.inc_ref();
+ T::inc_ref(self);
// SAFETY: We just incremented the refcount above.
unsafe { Self::from_raw(self.ptr) }
}
@@ -139,7 +141,7 @@ fn deref(&self) -> &Self::Target {
impl<T: AlwaysRefCounted> From<&T> for ARef<T> {
fn from(b: &T) -> Self {
- b.inc_ref();
+ T::inc_ref(b);
// SAFETY: We just incremented the refcount above.
unsafe { Self::from_raw(NonNull::from(b)) }
}
diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs
index 7d0935bc325c..b0577b00280c 100644
--- a/rust/kernel/task.rs
+++ b/rust/kernel/task.rs
@@ -349,9 +349,9 @@ pub fn active_pid_ns(&self) -> Option<&PidNamespace> {
// SAFETY: The type invariants guarantee that `Task` is always refcounted.
unsafe impl crate::types::AlwaysRefCounted for Task {
#[inline]
- fn inc_ref(&self) {
+ fn inc_ref(obj: &Self) {
// SAFETY: The existence of a shared reference means that the refcount is nonzero.
- unsafe { bindings::get_task_struct(self.as_ptr()) };
+ unsafe { bindings::get_task_struct(obj.as_ptr()) };
}
#[inline]
--
2.39.5
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v3] rust: aref: make `AlwaysRefCounted::inc_ref` an associated function
2025-09-24 5:24 [PATCH v3] rust: aref: make `AlwaysRefCounted::inc_ref` an associated function Trevor Chan
@ 2025-09-24 8:31 ` Miguel Ojeda
2025-09-24 17:08 ` Benno Lossin
2025-09-29 7:17 ` Viresh Kumar
0 siblings, 2 replies; 4+ messages in thread
From: Miguel Ojeda @ 2025-09-24 8:31 UTC (permalink / raw)
To: Trevor Chan
Cc: alex.gaynor, ojeda, gregkh, rafael, dakr, airlied, simona,
aliceryhl, vireshk, nm, sboyd, viro, brauner, boqun.feng, gary,
bjorn3_gh, lossin, a.hindborg, tmgross, rust-for-linux,
david.m.ertman, ira.weiny, leon, lorenzo.stoakes, Liam.Howlett,
bhelgaas, kwilczynski, jack
On Wed, Sep 24, 2025 at 7:24 AM Trevor Chan <trev@trevrosa.dev> wrote:
>
> rust/kernel/auxiliary.rs | 4 ++--
> rust/kernel/block/mq/request.rs | 4 ++--
> rust/kernel/cred.rs | 4 ++--
> rust/kernel/device.rs | 4 ++--
> rust/kernel/device/property.rs | 4 ++--
> rust/kernel/drm/device.rs | 4 ++--
> rust/kernel/drm/gem/mod.rs | 4 ++--
> rust/kernel/fs/file.rs | 8 ++++----
> rust/kernel/mm.rs | 8 ++++----
> rust/kernel/mm/mmput_async.rs | 4 ++--
> rust/kernel/opp.rs | 4 ++--
> rust/kernel/pci.rs | 4 ++--
> rust/kernel/pid_namespace.rs | 4 ++--
> rust/kernel/platform.rs | 4 ++--
> rust/kernel/task.rs | 4 ++--
Acked-by's from maintainers appreciated!
> + /// This function should not be called lightly.
Can we give a reason here, please?
I would also mention "... For this reason, it is an associated
function instead of a method." or similar.
Thanks!
Cheers,
Miguel
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v3] rust: aref: make `AlwaysRefCounted::inc_ref` an associated function
2025-09-24 8:31 ` Miguel Ojeda
@ 2025-09-24 17:08 ` Benno Lossin
2025-09-29 7:17 ` Viresh Kumar
1 sibling, 0 replies; 4+ messages in thread
From: Benno Lossin @ 2025-09-24 17:08 UTC (permalink / raw)
To: Miguel Ojeda, Trevor Chan
Cc: alex.gaynor, ojeda, gregkh, rafael, dakr, airlied, simona,
aliceryhl, vireshk, nm, sboyd, viro, brauner, boqun.feng, gary,
bjorn3_gh, a.hindborg, tmgross, rust-for-linux, david.m.ertman,
ira.weiny, leon, lorenzo.stoakes, Liam.Howlett, bhelgaas,
kwilczynski, jack
On Wed Sep 24, 2025 at 10:31 AM CEST, Miguel Ojeda wrote:
> On Wed, Sep 24, 2025 at 7:24 AM Trevor Chan <trev@trevrosa.dev> wrote:
>>
>> rust/kernel/auxiliary.rs | 4 ++--
>> rust/kernel/block/mq/request.rs | 4 ++--
>> rust/kernel/cred.rs | 4 ++--
>> rust/kernel/device.rs | 4 ++--
>> rust/kernel/device/property.rs | 4 ++--
>> rust/kernel/drm/device.rs | 4 ++--
>> rust/kernel/drm/gem/mod.rs | 4 ++--
>> rust/kernel/fs/file.rs | 8 ++++----
>> rust/kernel/mm.rs | 8 ++++----
>> rust/kernel/mm/mmput_async.rs | 4 ++--
>> rust/kernel/opp.rs | 4 ++--
>> rust/kernel/pci.rs | 4 ++--
>> rust/kernel/pid_namespace.rs | 4 ++--
>> rust/kernel/platform.rs | 4 ++--
>> rust/kernel/task.rs | 4 ++--
>
> Acked-by's from maintainers appreciated!
>
>> + /// This function should not be called lightly.
>
> Can we give a reason here, please?
I think we should change "lightly" to "accidentally". My motivation for
this was that a type might declare their own `inc_ref` function & it
shouldn't be confused with the one from `AlwaysRefCounted`. So it should
be a conscious action to increment the refcount manually (of course
cloning the ARef etc is fine).
---
Cheers,
Benno
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v3] rust: aref: make `AlwaysRefCounted::inc_ref` an associated function
2025-09-24 8:31 ` Miguel Ojeda
2025-09-24 17:08 ` Benno Lossin
@ 2025-09-29 7:17 ` Viresh Kumar
1 sibling, 0 replies; 4+ messages in thread
From: Viresh Kumar @ 2025-09-29 7:17 UTC (permalink / raw)
To: Miguel Ojeda
Cc: Trevor Chan, alex.gaynor, ojeda, gregkh, rafael, dakr, airlied,
simona, aliceryhl, vireshk, nm, sboyd, viro, brauner, boqun.feng,
gary, bjorn3_gh, lossin, a.hindborg, tmgross, rust-for-linux,
david.m.ertman, ira.weiny, leon, lorenzo.stoakes, Liam.Howlett,
bhelgaas, kwilczynski, jack
On 24-09-25, 10:31, Miguel Ojeda wrote:
> On Wed, Sep 24, 2025 at 7:24 AM Trevor Chan <trev@trevrosa.dev> wrote:
> >
> > rust/kernel/auxiliary.rs | 4 ++--
> > rust/kernel/block/mq/request.rs | 4 ++--
> > rust/kernel/cred.rs | 4 ++--
> > rust/kernel/device.rs | 4 ++--
> > rust/kernel/device/property.rs | 4 ++--
> > rust/kernel/drm/device.rs | 4 ++--
> > rust/kernel/drm/gem/mod.rs | 4 ++--
> > rust/kernel/fs/file.rs | 8 ++++----
> > rust/kernel/mm.rs | 8 ++++----
> > rust/kernel/mm/mmput_async.rs | 4 ++--
> > rust/kernel/opp.rs | 4 ++--
> > rust/kernel/pci.rs | 4 ++--
> > rust/kernel/pid_namespace.rs | 4 ++--
> > rust/kernel/platform.rs | 4 ++--
> > rust/kernel/task.rs | 4 ++--
>
> Acked-by's from maintainers appreciated!
I (and Alice) already Acked the V2 version. Trevor, please add the
already collected tags in the newer versions.
--
viresh
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-09-29 7:17 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-24 5:24 [PATCH v3] rust: aref: make `AlwaysRefCounted::inc_ref` an associated function Trevor Chan
2025-09-24 8:31 ` Miguel Ojeda
2025-09-24 17:08 ` Benno Lossin
2025-09-29 7:17 ` Viresh Kumar
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).