mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [RFC PATCH] rust: i2c: Remove use of bindings::i2c_match_id
@ 2026-11-11 20:52 Andrew Davis
  2026-11-11 23:10 ` [RFC PATCH v2] " Andrew Davis
  0 siblings, 1 reply; 5+ messages in thread
From: Andrew Davis @ 2026-11-11 20:52 UTC (permalink / raw)
  To: Igor Korotin, Danilo Krummrich, Daniel Almeida, Miguel Ojeda,
	Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan
  Cc: rust-for-linux, linux-kernel, Andrew Davis

The C function i2c_match_id() is slated for removal, remove use of this
from Rust I2C by directly finding the matching ID for I2C clients.

Signed-off-by: Andrew Davis <afd@ti.com>
---

Hello all,

Looking for some help on this one. I'm working on removing i2c_match_id()
from the kernel for various reasons, and ran into the issue of it being
used by Rust through a binding.

Wanted to try my hand at some Rust code and so tried to recreate this
simple ~7 line C function in Rust. Didn't go so well, tried writing it
by hand and got something that built, but a quick AI code review told me
every single line I wrote was wrong :D After some more fixing this is what
I ended up with. Seems to be correct but having a hard time believing this
mess I came up with is really how Rust should be written. So any suggestions
or hints on a better way to do this would be very welcome!

Thanks,
Andrew

 rust/kernel/i2c.rs | 33 ++++++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 5 deletions(-)

diff --git a/rust/kernel/i2c.rs b/rust/kernel/i2c.rs
index b4ff25c830fc0..acf96887253c8 100644
--- a/rust/kernel/i2c.rs
+++ b/rust/kernel/i2c.rs
@@ -197,12 +197,35 @@ fn i2c_id_info(dev: &I2cClient) -> Option<&'static <Self as driver::Adapter>::Id
         let table = Self::i2c_id_table()?;
 
         // SAFETY:
-        // - `table` has static lifetime, hence it's valid for reads
         // - `dev` is guaranteed to be valid while it's alive, and so is `dev.as_raw()`.
-        let raw_id = unsafe { bindings::i2c_match_id(table.as_ptr(), dev.as_raw()) };
-
-        if raw_id.is_null() {
-            return None;
+        // - `name` is filled with `strscpy()` when the client is created, so it is NUL-terminated,
+        //    and it is not modified for the lifetime of the client.
+        let client_name = unsafe { CStr::from_char_ptr((*dev.as_raw()).name.as_ptr()) };
+
+        // Get the raw pointer to the start of the C array
+        // It is never null as it points at the start of a static `IdArray`.
+        let mut raw_id = table.as_ptr();
+
+        // Iterate until hitting the sentinel entry or matching the client name
+        loop {
+            // SAFETY: `raw_id` points to an entry of `table`, which is terminated by a zeroed
+            // sentinel entry. We never advance past the sentinel (see below), so `raw_id` is
+            // valid for reads. `name` is NUL-terminated for every entry, including the
+            // sentinel, which is all zeroes.
+            let id_name = unsafe { CStr::from_char_ptr((*raw_id).name.as_ptr()) };
+
+            // An empty name is the sentinel: no match.
+            if id_name.is_empty() {
+                return None;
+            }
+
+            if id_name == client_name {
+                break;
+            }
+
+            // SAFETY: the current entry is not the sentinel, so the next entry is still
+            // within `table`.
+            raw_id = unsafe { raw_id.add(1) };
         }
 
         // SAFETY: `DeviceId` is a `#[repr(transparent)` wrapper of `struct i2c_device_id` and
-- 
2.43.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [RFC PATCH v2] rust: i2c: Remove use of bindings::i2c_match_id
  2026-11-11 20:52 [RFC PATCH] rust: i2c: Remove use of bindings::i2c_match_id Andrew Davis
@ 2026-11-11 23:10 ` Andrew Davis
  2026-11-16 18:25   ` Miguel Ojeda
  2026-11-17 11:37   ` Gary Guo
  0 siblings, 2 replies; 5+ messages in thread
From: Andrew Davis @ 2026-11-11 23:10 UTC (permalink / raw)
  To: Igor Korotin, Danilo Krummrich, Daniel Almeida, Miguel Ojeda,
	Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan
  Cc: rust-for-linux, linux-kernel, Andrew Davis

The C function i2c_match_id() is slated for removal, remove use of this
from Rust I2C by switching to bindings::i2c_get_match_data().

Signed-off-by: Andrew Davis <afd@ti.com>
---

Well Sashiko suggested I take the easy way and just re-use a different C
function instead of trying to open-code the loop in Rust. The C function
implements the same lookup as i2c_id_info did so we can drop that out and
its i2c_id_table helper. Still would like some help on checking if this
is actually correct though..

Thanks,
Andrew

 rust/kernel/i2c.rs | 39 +++++++--------------------------------
 1 file changed, 7 insertions(+), 32 deletions(-)

diff --git a/rust/kernel/i2c.rs b/rust/kernel/i2c.rs
index b4ff25c830fc0..37d914fcb3662 100644
--- a/rust/kernel/i2c.rs
+++ b/rust/kernel/i2c.rs
@@ -148,10 +148,13 @@ extern "C" fn probe_callback(idev: *mut bindings::i2c_client) -> kernel::ffi::c_
         // INVARIANT: `idev` is valid for the duration of `probe_callback()`.
         let idev = unsafe { &*idev.cast::<I2cClient<device::CoreInternal<'_>>>() };
 
-        let info = Self::i2c_id_info(idev).or_else(|| {
-            // SAFETY: `idev` matched data is of type `Self::IdInfo`.
-            unsafe { <Self as driver::Adapter>::id_info(idev.as_ref()) }
-        });
+        // SAFETY: `idev` is a valid `struct i2c_client` bound to this driver, so
+        // `i2c_get_match_data()` may read `idev.dev.driver`.
+        let raw_info = unsafe { bindings::i2c_get_match_data(idev.as_raw()) };
+
+        // SAFETY: Non-NULL match data is either the OF/ACPI entry data or the `driver_data` of an
+        // I2C id entry. Both are built from `&'static Self::IdInfo` by this driver's tables.
+        let info = unsafe { raw_info.cast::<T::IdInfo>().as_ref() };
 
         from_result(|| {
             let data = T::probe(idev, info);
@@ -184,34 +187,6 @@ extern "C" fn shutdown_callback(idev: *mut bindings::i2c_client) {
 
         T::shutdown(idev, data);
     }
-
-    /// The [`i2c::IdTable`] of the corresponding driver.
-    fn i2c_id_table() -> Option<IdTable<<Self as driver::Adapter>::IdInfo>> {
-        T::I2C_ID_TABLE
-    }
-
-    /// Returns the driver's private data from the matching entry in the [`i2c::IdTable`], if any.
-    ///
-    /// If this returns `None`, it means there is no match with an entry in the [`i2c::IdTable`].
-    fn i2c_id_info(dev: &I2cClient) -> Option<&'static <Self as driver::Adapter>::IdInfo> {
-        let table = Self::i2c_id_table()?;
-
-        // SAFETY:
-        // - `table` has static lifetime, hence it's valid for reads
-        // - `dev` is guaranteed to be valid while it's alive, and so is `dev.as_raw()`.
-        let raw_id = unsafe { bindings::i2c_match_id(table.as_ptr(), dev.as_raw()) };
-
-        if raw_id.is_null() {
-            return None;
-        }
-
-        // SAFETY: `DeviceId` is a `#[repr(transparent)` wrapper of `struct i2c_device_id` and
-        // does not add additional invariants, so it's safe to transmute.
-        let id = unsafe { &*raw_id.cast::<DeviceId>() };
-
-        // SAFETY: `id` comes from `table` which is of type `IdArray<_, Self::IdInfo>`.
-        Some(unsafe { id.info_unchecked::<T::IdInfo>() })
-    }
 }
 
 impl<T: Driver> driver::Adapter for Adapter<T> {
-- 
2.43.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [RFC PATCH v2] rust: i2c: Remove use of bindings::i2c_match_id
  2026-11-11 23:10 ` [RFC PATCH v2] " Andrew Davis
@ 2026-11-16 18:25   ` Miguel Ojeda
  2026-11-17  1:40     ` Andrew Davis
  2026-11-17 11:37   ` Gary Guo
  1 sibling, 1 reply; 5+ messages in thread
From: Miguel Ojeda @ 2026-11-16 18:25 UTC (permalink / raw)
  To: Andrew Davis
  Cc: Igor Korotin, Danilo Krummrich, Daniel Almeida, Miguel Ojeda,
	Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan, rust-for-linux, linux-kernel

On Thu, Nov 12, 2026 at 12:11 AM Andrew Davis <afd@ti.com> wrote:
>
> Well Sashiko suggested I take the easy way and just re-use a different C
> function instead of trying to open-code the loop in Rust. The C function

I noticed this series while going through Lore threads, and yeah, it
is generally better to avoid re-implementing logic in Rust if a
suitable function already exists in C in the subsystem (unless there
is a good reason to not do so, of course).

By the way, while the maintainers may already know about the removal,
I would suggest adding a Link: or commit reference to the patch
removing that C function, i.e. to give context for others.

I hope that helps!

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [RFC PATCH v2] rust: i2c: Remove use of bindings::i2c_match_id
  2026-11-16 18:25   ` Miguel Ojeda
@ 2026-11-17  1:40     ` Andrew Davis
  0 siblings, 0 replies; 5+ messages in thread
From: Andrew Davis @ 2026-11-17  1:40 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Igor Korotin, Danilo Krummrich, Daniel Almeida, Miguel Ojeda,
	Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
	Andreas Hindborg, Alice Ryhl, Trevor Gross, Tamir Duberstein,
	Alexandre Courbot, Onur Özkan, rust-for-linux, linux-kernel

On 11/16/26 12:25 PM, Miguel Ojeda wrote:
> On Thu, Nov 12, 2026 at 12:11 AM Andrew Davis <afd@ti.com> wrote:
>>
>> Well Sashiko suggested I take the easy way and just re-use a different C
>> function instead of trying to open-code the loop in Rust. The C function
> 
> I noticed this series while going through Lore threads, and yeah, it
> is generally better to avoid re-implementing logic in Rust if a
> suitable function already exists in C in the subsystem (unless there
> is a good reason to not do so, of course).
> 

Perfect, sounds like v2 here is the right to go then. I'll resend
this RFC as a normal PATCH then.

> By the way, while the maintainers may already know about the removal,
> I would suggest adding a Link: or commit reference to the patch
> removing that C function, i.e. to give context for others.
> 

It's not been coordinated much, I've been just converting drivers over
to better functions until there are none left using this old one. Then
will send a patch to drop the then unused old function. Only a few users
left after this Rust one.

> I hope that helps!
> 

It does, thanks!

Andrew

> Cheers,
> Miguel
> 


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [RFC PATCH v2] rust: i2c: Remove use of bindings::i2c_match_id
  2026-11-11 23:10 ` [RFC PATCH v2] " Andrew Davis
  2026-11-16 18:25   ` Miguel Ojeda
@ 2026-11-17 11:37   ` Gary Guo
  1 sibling, 0 replies; 5+ messages in thread
From: Gary Guo @ 2026-11-17 11:37 UTC (permalink / raw)
  To: Andrew Davis, Igor Korotin, Danilo Krummrich, Daniel Almeida,
	Miguel Ojeda, Boqun Feng, Gary Guo, Björn Roy Baron,
	Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
	Tamir Duberstein, Alexandre Courbot, Onur Özkan
  Cc: rust-for-linux, linux-kernel

On Wed Nov 11, 2026 at 11:10 PM GMT, Andrew Davis wrote:
> The C function i2c_match_id() is slated for removal, remove use of this
> from Rust I2C by switching to bindings::i2c_get_match_data().
>
> Signed-off-by: Andrew Davis <afd@ti.com>
> ---
>
> Well Sashiko suggested I take the easy way and just re-use a different C
> function instead of trying to open-code the loop in Rust. The C function
> implements the same lookup as i2c_id_info did so we can drop that out and
> its i2c_id_table helper. Still would like some help on checking if this
> is actually correct though..

We historically use open-coded matching in Rust as integer indices are used
instead of pointers, but we have cleaned it up since
https://lore.kernel.org/driver-core/20260629-id_info-v2-0-56fccbe9c5ef@garyguo.net
So open-coded logic should be all removable now.

Reviewed-by: Gary Guo <gary@garyguo.net>

>
> Thanks,
> Andrew
>
>  rust/kernel/i2c.rs | 39 +++++++--------------------------------
>  1 file changed, 7 insertions(+), 32 deletions(-)


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-17 11:37 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-11 20:52 [RFC PATCH] rust: i2c: Remove use of bindings::i2c_match_id Andrew Davis
2026-11-11 23:10 ` [RFC PATCH v2] " Andrew Davis
2026-11-16 18:25   ` Miguel Ojeda
2026-11-17  1:40     ` Andrew Davis
2026-11-17 11:37   ` Gary Guo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®