rust: driver: remove open-coded matching logic

With device ID info now including pointers instead of indices, the
open-coded ACPI/OF matching is no longer needed and can be replaced with
`device_get_match_data`.

With the removal of open-coded matching, the exposed functions and helpers
are also removed; this effectively reverts most of commit 2690d07158
("rust: ACPI: fix missing match data for PRP0001").

Signed-off-by: Gary Guo <gary@garyguo.net>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Link: https://patch.msgid.link/20260629-id_info-v2-9-56fccbe9c5ef@garyguo.net
[ Consider the serdev code merged in the meantime. - Danilo ]
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
This commit is contained in:
Gary Guo
2026-06-29 13:39:43 +01:00
committed by Danilo Krummrich
parent 0b76335e8f
commit 2c5d17cdc4
9 changed files with 21 additions and 161 deletions

View File

@@ -297,7 +297,6 @@ F: include/linux/acpi.h
F: include/linux/fwnode.h
F: include/linux/fw_table.h
F: lib/fw_table.c
F: rust/helpers/acpi.c
F: rust/kernel/acpi.rs
F: tools/power/acpi/

View File

@@ -898,9 +898,9 @@ const struct acpi_device *acpi_companion_match(const struct device *dev)
* identifiers and a _DSD object with the "compatible" property, use that
* property to match against the given list of identifiers.
*/
bool acpi_of_match_device(const struct acpi_device *adev,
const struct of_device_id *of_match_table,
const struct of_device_id **of_id)
static bool acpi_of_match_device(const struct acpi_device *adev,
const struct of_device_id *of_match_table,
const struct of_device_id **of_id)
{
const union acpi_object *of_compatible, *obj;
int i, nval;

View File

@@ -189,10 +189,6 @@ struct acpi_driver {
* -----------
*/
bool acpi_of_match_device(const struct acpi_device *adev,
const struct of_device_id *of_match_table,
const struct of_device_id **of_id);
/* Status (_STA) */
struct acpi_device_status {
@@ -1000,13 +996,6 @@ int acpi_scan_add_dep(acpi_handle handle, struct acpi_handle_list *dep_devices);
u32 arch_acpi_add_auto_dep(acpi_handle handle);
#else /* CONFIG_ACPI */
static inline bool acpi_of_match_device(const struct acpi_device *adev,
const struct of_device_id *of_match_table,
const struct of_device_id **of_id)
{
return false;
}
static inline int register_acpi_bus_type(void *bus) { return 0; }
static inline int unregister_acpi_bus_type(void *bus) { return 0; }

View File

@@ -1,16 +0,0 @@
// SPDX-License-Identifier: GPL-2.0
#include <linux/acpi.h>
#include <acpi/acpi_bus.h>
__rust_helper bool rust_helper_acpi_of_match_device(const struct acpi_device *adev,
const struct of_device_id *of_match_table,
const struct of_device_id **of_id)
{
return acpi_of_match_device(adev, of_match_table, of_id);
}
__rust_helper struct acpi_device *rust_helper_to_acpi_device_node(struct fwnode_handle *fwnode)
{
return to_acpi_device_node(fwnode);
}

View File

@@ -38,7 +38,6 @@
#define __rust_helper __always_inline
#endif
#include "acpi.c"
#include "atomic.c"
#include "atomic_ext.c"
#include "auxiliary.c"

View File

@@ -107,7 +107,6 @@
use crate::{
acpi,
device,
device_id::RawDeviceIdIndex,
of,
prelude::*,
types::Opaque,
@@ -292,26 +291,6 @@ fn init(
}
}
// Calling the FFI function directly from the `Adapter` impl may result in it being called
// directly from driver modules. This happens since the Rust compiler will use monomorphisation, so
// it might happen that functions are instantiated within the calling driver module. For now, work
// around this with `#[inline(never)]` helpers.
//
// TODO: Remove once a more generic solution has been implemented. For instance, we may be able to
// leverage `bindgen` to take care of this depending on whether a symbol is (already) exported.
#[inline(never)]
#[allow(clippy::missing_safety_doc)]
#[allow(dead_code)]
#[must_use]
unsafe fn acpi_of_match_device(
adev: *const bindings::acpi_device,
of_match_table: *const bindings::of_device_id,
of_id: *mut *const bindings::of_device_id,
) -> bool {
// SAFETY: Safety requirements are the same as `bindings::acpi_of_match_device`.
unsafe { bindings::acpi_of_match_device(adev, of_match_table, of_id) }
}
/// The bus independent adapter to match a drivers and a devices.
///
/// This trait should be implemented by the bus specific adapter, which represents the connection
@@ -325,117 +304,23 @@ pub trait Adapter {
/// The [`acpi::IdTable`] of the corresponding driver
fn acpi_id_table() -> Option<acpi::IdTable<Self::IdInfo>>;
/// Returns the driver's private data from the matching entry in the [`acpi::IdTable`], if any.
///
/// If this returns `None`, it means there is no match with an entry in the [`acpi::IdTable`].
fn acpi_id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> {
#[cfg(not(CONFIG_ACPI))]
{
let _ = dev;
None
}
#[cfg(CONFIG_ACPI)]
{
let table = Self::acpi_id_table()?;
// SAFETY:
// - `table` has static lifetime, hence it's valid for read,
// - `dev` is guaranteed to be valid while it's alive, and so is `dev.as_raw()`.
let raw_id = unsafe { bindings::acpi_match_device(table.as_ptr(), dev.as_raw()) };
if raw_id.is_null() {
None
} else {
// SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `struct acpi_device_id`
// and does not add additional invariants, so it's safe to transmute.
let id = unsafe { &*raw_id.cast::<acpi::DeviceId>() };
// SAFETY: `id` comes from `table` which is of type `IdArray<_, Self::IdInfo>`.
Some(unsafe { id.info_unchecked::<Self::IdInfo>() })
}
}
}
/// The [`of::IdTable`] of the corresponding driver.
fn of_id_table() -> Option<of::IdTable<Self::IdInfo>>;
/// Returns the driver's private data from the matching entry in the [`of::IdTable`], if any.
///
/// If this returns `None`, it means there is no match with an entry in the [`of::IdTable`].
fn of_id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> {
let table = Self::of_id_table()?;
#[cfg(not(any(CONFIG_OF, CONFIG_ACPI)))]
{
let _ = (dev, table);
}
#[cfg(CONFIG_OF)]
{
// SAFETY:
// - `table` has static lifetime, hence it's valid for read,
// - `dev` is guaranteed to be valid while it's alive, and so is `dev.as_raw()`.
let raw_id = unsafe { bindings::of_match_device(table.as_ptr(), dev.as_raw()) };
if !raw_id.is_null() {
// SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `struct of_device_id`
// and does not add additional invariants, so it's safe to transmute.
let id = unsafe { &*raw_id.cast::<of::DeviceId>() };
// SAFETY: `id` comes from `table` which is of type `IdArray<_, Self::IdInfo>`.
return Some(unsafe { id.info_unchecked::<Self::IdInfo>() });
}
}
#[cfg(CONFIG_ACPI)]
{
use core::ptr;
use device::property::FwNode;
let mut raw_id = ptr::null();
let fwnode = dev.fwnode().map_or(ptr::null_mut(), FwNode::as_raw);
// SAFETY: `fwnode` is a pointer to a valid `fwnode_handle`. A null pointer will be
// passed through the function.
let adev = unsafe { bindings::to_acpi_device_node(fwnode) };
// SAFETY:
// - `adev` is a valid pointer to `acpi_device` or is null. It is guaranteed to be
// valid as long as `dev` is alive.
// - `table` has static lifetime, hence it's valid for read.
if unsafe { acpi_of_match_device(adev, table.as_ptr(), &raw mut raw_id) } {
// SAFETY:
// - the function returns true, therefore `raw_id` has been set to a pointer to a
// valid `of_device_id`.
// - `DeviceId` is a `#[repr(transparent)]` wrapper of `struct of_device_id`
// and does not add additional invariants, so it's safe to transmute.
let id = unsafe { &*raw_id.cast::<of::DeviceId>() };
// SAFETY: `id` comes from `table` which is of type `IdArray<_, Self::IdInfo>`.
return Some(unsafe { id.info_unchecked::<Self::IdInfo>() });
}
}
None
}
/// Returns the driver's private data from the matching entry of any of the ID tables, if any.
///
/// If this returns `None`, it means that there is no match in any of the ID tables directly
/// associated with a [`device::Device`].
fn id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> {
let id = Self::acpi_id_info(dev);
if id.is_some() {
return id;
}
///
/// # Safety
///
/// The caller must ensure that the `dev` matched data is of type `Self::IdInfo`.
#[inline]
unsafe fn id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> {
// SAFETY: `dev` is guaranteed to be valid while it's alive, and so is `dev.as_raw()`.
let data = unsafe { bindings::device_get_match_data(dev.as_raw()) };
let id = Self::of_id_info(dev);
if id.is_some() {
return id;
}
None
// SAFETY: Per safety requirement, `data` is of type `Self::IdInfo`.
unsafe { data.cast::<Self::IdInfo>().as_ref() }
}
}

View File

@@ -149,8 +149,10 @@ 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(|| <Self as driver::Adapter>::id_info(idev.as_ref()));
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()) }
});
from_result(|| {
let data = T::probe(idev, info);

View File

@@ -100,7 +100,8 @@ extern "C" fn probe_callback(pdev: *mut bindings::platform_device) -> kernel::ff
//
// INVARIANT: `pdev` is valid for the duration of `probe_callback()`.
let pdev = unsafe { &*pdev.cast::<Device<device::CoreInternal<'_>>>() };
let info = <Self as driver::Adapter>::id_info(pdev.as_ref());
// SAFETY: `pdev` matched data is of type `Self::IdInfo`.
let info = unsafe { <Self as driver::Adapter>::id_info(pdev.as_ref()) };
from_result(|| {
let data = T::probe(pdev, info);

View File

@@ -162,7 +162,8 @@ extern "C" fn probe_callback(sdev: *mut bindings::serdev_device) -> kernel::ffi:
//
// INVARIANT: `sdev` is valid for the duration of `probe_callback()`.
let sdev = unsafe { &*sdev.cast::<Device<device::CoreInternal<'_>>>() };
let info = <Self as driver::Adapter>::id_info(sdev.as_ref());
// SAFETY: `sdev` matched data is of type `Self::IdInfo`.
let info = unsafe { <Self as driver::Adapter>::id_info(sdev.as_ref()) };
from_result(|| {
sdev.as_ref().set_drvdata(try_pin_init!(PrivateData::<T> {