All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Nicolás Antinori" <nico.antinori.7@gmail.com>
To: "Igor Korotin" <igor.korotin@linux.dev>, "Gary Guo" <gary@garyguo.net>
Cc: "Alexandre Courbot" <acourbot@nvidia.com>,
	"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>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Onur Özkan" <work@onurozkan.dev>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	linux-kernel-mentees@lists.linux.dev,
	linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
	Sashiko <sashiko-bot@kernel.org>
Subject: Re: [PATCH] rust: i2c: avoid locking when calling I2cAdapter::inc_ref
Date: Tue, 11 Aug 2026 11:43:29 -0300	[thread overview]
Message-ID: <DKM70AZE2094.3397PWQQ3TC3@gmail.com> (raw)
In-Reply-To: <178628144572.5226.7072490581990266256@linux.dev>

On Sun Aug 9, 2026 at 10:17 AM -03, Igor Korotin wrote:
> Hello Nicolás
>
> Sorry for the delay.
>
> Yes, worth fixing regardless of real users -- inc_ref taking a lock it
> doesn't need is a real bug, not speculative work.
>
> Instead of adding a separate Rust-only wrapper, please consider splitting
> i2c_get_adapter itself: pull the increment into a helper, export it, and
> call it directly from inc_ref -- no lock, no lookup:
>
> +bool __i2c_adapter_get(struct i2c_adapter *adapter)
> +{
> +	if (try_module_get(adapter->owner)) {
> +		get_device(&adapter->dev);
> +		return true;
> +	}
> +	return false;
> +}
> +EXPORT_SYMBOL(__i2c_adapter_get);
> +
>  struct i2c_adapter *i2c_get_adapter(int nr)
>  {
>  	struct i2c_adapter *adapter;
>
>  	mutex_lock(&core_lock);
>  	adapter = idr_find(&i2c_adapter_idr, nr);
> -	if (!adapter)
> -		goto exit;
> -
> -	if (try_module_get(adapter->owner))
> -		get_device(&adapter->dev);
> -	else
> +	if (adapter && !__i2c_adapter_get(adapter))
>  		adapter = NULL;
> -
> - exit:
>  	mutex_unlock(&core_lock);
>  	return adapter;
>  }
>  EXPORT_SYMBOL(i2c_get_adapter);

Excellent! Thank you for this suggestion and the explanation!

>
> Heads up for v2: Trevor Chan has a patch changing AlwaysRefCounted::inc_ref
> to an associated function (fn inc_ref(obj: &Self)). Not merged yet --
> rebase onto it if it lands first.

I think it is better to wait until Trevor Chan's patch gets merged since
it is in v7 [1] and already has been reviewed by some mantainers. If I send 
this fix and gets merged first he will probably have conflicts and have
to send a v8.

I'll keep an eye on the progress of that patch and once it is merged I
am going to send this fix.

Thank you,
Nicolás

[1] https://lore.kernel.org/rust-for-linux/20260628100731.64885-1-trev@trevrosa.dev/T/#u

      reply	other threads:[~2026-08-11 14:43 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-15 20:10 [PATCH] rust: i2c: avoid locking when calling I2cAdapter::inc_ref Nicolás Antinori
2026-06-16 21:15 ` Nicolás Antinori
2026-06-17 14:12 ` Gary Guo
2026-06-18 15:32   ` Nicolás Antinori
2026-07-06 13:50     ` Nicolás Antinori
2026-08-09 13:17       ` Igor Korotin
2026-08-11 14:43         ` Nicolás Antinori [this message]

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=DKM70AZE2094.3397PWQQ3TC3@gmail.com \
    --to=nico.antinori.7@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --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-mentees@lists.linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=sashiko-bot@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=work@onurozkan.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.