From: "Nicolás Antinori" <nico.antinori.7@gmail.com>
To: "Nicolás Antinori" <nico.antinori.7@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Igor Korotin" <igor.korotin@linux.dev>
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: Mon, 06 Jul 2026 10:50:47 -0300 [thread overview]
Message-ID: <DJRJCCGCXYOL.2O4HP9N37SPAK@gmail.com> (raw)
In-Reply-To: <DJCA8F2F93US.1KMA54SKQ364K@gmail.com>
Hello Gary,
On Thu Jun 18, 2026 at 12:32 PM -03, Nicolás Antinori wrote:
> Hello Gary,
>
> On Wed Jun 17, 2026 at 11:12 AM -03, Gary Guo wrote:
>> 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).
>
> I based this solution on the logic used in I2cClient (where only
> get_device is needed) and by verifying which counters the C
> implementation (i2c_get_adapter) increments.
>
> The i2c_get_adapter function in i2c-core-base.c performs two increments
> (module increment is done by calling try_module_get, but in this case,
> because inc_ref operates on an already live instance, unconditionally
> incrementing the count with `__module_get` should be safe).
>
> If I understand correctly, the idea would be to introduce a helper
> function on the C side, for example:
> void i2c_adapter_increment(struct i2c_adap *adap);
>
> We would perform the increments there and call it from Rust. Using this
> in inc_ref would be safe because the existence of &self guarantees that
> we already have a valid, live instance.
>
> Is there any concern regarding this function beign exposed to the C
> side? To use it safely in C, callers would have to ensure that *adap
> points to a valid instance.
I tried implementing this with the following C helper:
...
void i2c_adapter_inc_ref(struct i2c_adapter *adapter)
{
__module_get(adapter->owner);
get_device(&adapter->dev);
}
...
Calling this from Rust results in much cleaner code. The function does
skips a NULL check for *adapter because the AlwaysRefCounted::inc_ref
signature suggest a non-fallibe operation. Did I understand your
suggestion correctly?
Also, does it make sense to declare a C API wrapper whose sole purpose
is to be called from the Rust side?
>
>>
>> That said, is there an actual user that needs this, or are we just implementing
>> AlwaysRefCounted preemptively?
>
> The only user I could find at the moment is the example driver
> (impl platform::Driver for SampleDriver ..) in
> samples/rust/rust_i2c_client.rs.
Does it make sense to move forward with this patch since there are no
real users yet? Does the Sashiko diagnostic makes sense? Maybe I
misunderstood it and it does not apply.
Thank you!
Nicolás
prev parent reply other threads:[~2026-07-06 13:50 UTC|newest]
Thread overview: 5+ 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 [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=DJRJCCGCXYOL.2O4HP9N37SPAK@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox