Re: [PATCH v2] i2c: rust: avoid locking when performing I2cAdapter::inc_ref
From: Nicolás Antinori
Date: Tue Oct 13 2026 - 09:53:25 EST
Hello,
On Fri Sep 4, 2026 at 10:44 AM -03, Nicolás Antinori wrote:
> The current implementation of `<I2cAdapter as
> AlwaysRefCounted>::inc_ref` relies on the C function `i2c_get_adapter`
> to increment module and device counters. This function acquires a lock,
> looks for the adapter in the IDR table, and, if found, increments the
> named counters before returning the adapter.
>
> In the Rust API, the `I2cAdapter::get` method returns an
> `ARef<I2cAdapter>` upon success. Incrementing this reference count in an
> atomic context (for example, via `ARef::clone`, which relies on
> `AlwaysRefCounted::inc_ref`) could trigger a sleep-in-atomic bug due to
> the mutex locking inside `i2c_get_adapter`.
>
> Since cloning an `ARef` implies we already hold a valid reference to the
> adapter, the IDR table lookup and its associated lock are unnecessary.
>
> The fix consists of bypassing `i2c_get_adapter` and instead calling
> `__i2c_adapter_get` directly to increment the counters.
>
> Reported-by: Sashiko <sashiko-bot@xxxxxxxxxx>
> Closes: https://sashiko.dev/#/patchset/20260524181151.24988-1-nico.antinori.7@xxxxxxxxx
> Signed-off-by: Nicolás Antinori <nico.antinori.7@xxxxxxxxx>
Following up on this patch. Please let me know if you have any feedback
or if any changes are needed!
Regards,
Nicolás