Rust associative function
Subsystems (12)
PATCH 1/1 Rust associative function
Currently AlwaysRefCounted::inc_ref is implemented as a method taking &self. However, this function should not be called lightly. To prevent accidental usage, convert it into an associated function instead of a method.
This aligns with the intended API safety model and makes it clearer that inc_ref is not part of the usual object manipulation methods.
--- 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/pci.rs | 4 ++-- rust/kernel/pid_namespace.rs | 4 ++-- rust/kernel/platform.rs | 4 ++-- rust/kernel/sync/aref.rs | 6 +++--- rust/kernel/task.rs | 4 ++-- 15 files changed, 35 insertions(+), 35 deletions(-)
rust/kernel/auxiliary.rs +2 -2
@@ -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 >) {
rust/kernel/block/mq/request.rs +2 -2
@@ -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 );
rust/kernel/cred.rs +2 -2
@@ -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]
rust/kernel/device.rs +2 -2
@@ -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 >) {
rust/kernel/device/property.rs +2 -2
@@ -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 >) {
rust/kernel/drm/device.rs +2 -2
@@ -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 >) {
rust/kernel/drm/gem/mod.rs +2 -2
@@ -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 >) {
rust/kernel/fs/file.rs +4 -4
@@ -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]
rust/kernel/mm.rs +4 -4
@@ -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]
rust/kernel/mm/mmput_async.rs +2 -2
@@ -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]
rust/kernel/pci.rs +2 -2
@@ -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 >) {
rust/kernel/pid_namespace.rs +2 -2
@@ -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]
rust/kernel/platform.rs +2 -2
@@ -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 >) {
rust/kernel/sync/aref.rs +3 -3
@@ -24,7 +24,7 @@/// alive.) pub unsafe trait AlwaysRefCounted {/// Increments the reference count on the object. -fn inc_ref (&self); +fn inc_ref (obj: &Self );/// Decrements the reference count on the object. ///
@@ -122,7 +122,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 +139,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)) } }
rust/kernel/task.rs +2 -2
@@ -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]
Signature · 1 line
2.50.1
Benno Lossin
reviewerHi,
Thanks for the patch!
Quoted 9 lines · Gent Binaku
Currently
AlwaysRefCounted::inc_refis implemented as a method taking&self. However, this function should not be called lightly. To prevent accidental usage, convert it into an associated function instead of a method.This aligns with the intended API safety model and makes it clearer that
inc_refis not part of the usual object manipulation methods.Suggested-by Benno Lossin <[email protected]>
Please use my <[email protected]> email instead.
Quoted 11 lines
Signed-off-by Gent Binaku <[email protected]>--- 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 ++++----
Quoted 8 lines
rust/kernel/mm.rs | 8 ++++---- rust/kernel/mm/mmput_async.rs | 4 ++-- rust/kernel/pci.rs | 4 ++-- rust/kernel/pid_namespace.rs | 4 ++-- rust/kernel/platform.rs | 4 ++-- rust/kernel/sync/aref.rs | 6 +++--- rust/kernel/task.rs | 4 ++-- 15 files changed, 35 insertions(+), 35 deletions(-)
We already have someone else working on this, see [1]. Not sure if you two are coordinating.
You also didn't choose a meaningful commit title.
[1]: https://lore.kernel.org/all/CACvsgUCBk5Dt2yBSy9_NA0z38-wP68vvBX7XDYgJGinTURT1kQ@mail.gmail.com
---
Cheers,
Benno
kernel test robot
build botkernel test robot noticed the following build errors:
Hi Gent,
kernel test robot noticed the following build errors:
[auto build test ERROR on rust/rust-next]
[also build test ERROR on driver-core/driver-core-linus akpm-mm/mm-everything linus/master v6.17-rc3] [cannot apply to driver-core/driver-core-testing driver-core/driver-core-next next-20250825] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Gent-Binaku/Rust-associative-function/20250821-050335 base: https://github.com/Rust-for-Linux/linux rust-next patch link: https://lore.kernel.org/r/20250820210225.124288-1-binakugent%40gmail.com patch subject: [PATCH] Rust associative function config: x86_64-rhel-9.4-rust (https://download.01.org/0day-ci/archive/20250826/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) rustc: rustc 1.88.0 (6b00bc388 2025-06-23) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250826/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> error[E0185]: method `inc_ref` has a `&self` declaration in the impl, but not in the trait
--> rust/doctests_kernel_generated.rs:10624:5
|
10624 | fn inc_ref(&self) {}
| ^^^^^^^^^^^^^^^^^ &self used in impl
|
= note: inc_ref from trait: fn(&Self)
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki