Re: [PATCH] rust: i2c: avoid locking when calling I2cAdapter::inc_ref

"Gary Guo" <[email protected]> Wed, 17 Jun 2026 15:12:51 +0100
Newsgroups dev.linux.lists.linux-kernel-mentees,org.kernel.vger.linux-kernel,org.kernel.vger.rust-for-linux
Message-ID <[email protected]>
On Mon Jun 15, 2026 at 9:10 PM BST, Nicolás Antinori wrote:
> diff --git a/rust/kernel/i2c.rs b/rust/kernel/i2c.rs
> index 624b971ca8b0..d89c42691dfe 100644
> --- a/rust/kernel/i2c.rs
> +++ b/rust/kernel/i2c.rs
> @@ -426,8 +426,11 @@ pub fn get(index: i32) -> Result<ARef<Self>> {
>  // SAFETY: Instances of `I2cAdapter` are always reference-counted.
>  unsafe impl AlwaysRefCounted for I2cAdapter {
>      fn inc_ref(&self) {
> -        // SAFETY: The existence of a shared reference guarantees that the refcount is non-zero.
> -        unsafe { bindings::i2c_get_adapter(self.index()) };
> +        // SAFETY: The existence of a shared reference guarantees that the refcounts are non-zero.
> +        unsafe {
> +            bindings::__module_get((*self.as_raw()).owner);
> +            bindings::get_device(&raw mut (*self.as_raw()).dev);

Instead of open coding this sequence, it would be better to add a C API that
does exactly this (getting another reference from existing one).

That said, is there an actual user that needs this, or are we just implementing
AlwaysRefCounted preemptively?

Best,
Gary

> +        }
>      }
>
>      unsafe fn dec_ref(obj: NonNull<Self>) {
> --
> 2.47.3