The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: "Onur Özkan" <work@onurozkan.dev>
To: "Nicolás Antinori" <nico.antinori.7@gmail.com>
Cc: "Igor Korotin" <igor.korotin@linux.dev>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Benno Lossin" <lossin@kernel.org>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Boqun Feng" <boqun@kernel.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Gary Guo" <gary@garyguo.net>, "Miguel Ojeda" <ojeda@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org
Subject: Re: [PATCH] i2c: rust: fix I2cAdapter refcounts double increment
Date: Mon, 25 May 2026 05:56:12 +0300	[thread overview]
Message-ID: <20260525025614.16352-1-work@onurozkan.dev> (raw)
In-Reply-To: <20260524181151.24988-1-nico.antinori.7@gmail.com>

Hi Nicolás

On Sun, 24 May 2026 15:11:50 -0300
Nicolás Antinori <nico.antinori.7@gmail.com> wrote:

> When `I2cAdapter::get` executes, it first calls
> `bindings::i2c_get_adapter()` which increments the device and module
> reference counts. It then takes a reference to the raw pointer and
> converts it to an `ARef` via `.into()`.
> 
> The implementation of `From<&T> for ARef<T>` where `T: AlwaysRefCounted`
> unconditionally calls `T::inc_ref()`. This leads to a second call to
> `bindings::i2c_get_adapter()` inside `I2cAdapter::inc_ref()`.
> 
> Since the returned `ARef` will only release a single reference when
> dropped via `dec_ref()`, this leaks one device and module reference count
> on every call.
> 
> This fix was suggested by sashiko.dev.

Including the link tag would be great.

> 
> Signed-off-by: Nicolás Antinori <nico.antinori.7@gmail.com>
> ---
>  rust/kernel/i2c.rs | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/rust/kernel/i2c.rs b/rust/kernel/i2c.rs
> index 7b908f0c5a58..ca6476cf4399 100644
> --- a/rust/kernel/i2c.rs
> +++ b/rust/kernel/i2c.rs
> @@ -405,7 +405,11 @@ pub fn get(index: i32) -> Result<ARef<Self>> {
>  
>          // SAFETY: `adapter` is non-null and points to a live `i2c_adapter`.
>          // `I2cAdapter` is #[repr(transparent)], so this cast is valid.
> -        Ok(unsafe { (&*adapter.as_ptr().cast::<I2cAdapter<device::Normal>>()).into() })
> +        Ok(unsafe {
> +            ARef::from_raw(NonNull::new_unchecked(
> +                adapter.as_ptr().cast::<I2cAdapter<device::Normal>>(),
> +            ))
> +        })
>      }
>  }
>  
> -- 
> 2.53.0
> 

  reply	other threads:[~2026-05-25  2:56 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-24 18:11 [PATCH] i2c: rust: fix I2cAdapter refcounts double increment Nicolás Antinori
2026-05-25  2:56 ` Onur Özkan [this message]
2026-05-26 16:48   ` Nicolás Antinori

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=20260525025614.16352-1-work@onurozkan.dev \
    --to=work@onurozkan.dev \
    --cc=a.hindborg@kernel.org \
    --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=igor.korotin@linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=nico.antinori.7@gmail.com \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox