public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: "Danilo Krummrich" <dakr@kernel.org>
To: "Markus Probst" <markus.probst@posteo.de>
Cc: "Rafael J. Wysocki" <rafael@kernel.org>,
	"Len Brown" <lenb@kernel.org>, "Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	linux-kernel@vger.kernel.org, linux-acpi@vger.kernel.org,
	rust-for-linux@vger.kernel.org, driver-core@lists.linux.dev
Subject: Re: [PATCH] rust: ACPI: fix missing match data for PRP0001
Date: Sun, 05 Apr 2026 00:07:30 +0200	[thread overview]
Message-ID: <DHKPNZT8IEO6.1JZ04J5AX26QF@kernel.org> (raw)
In-Reply-To: <20260401-rust_acpi_prp0001-v1-1-f6a4d2ef9244@posteo.de>

On Wed Apr 1, 2026 at 4:06 PM CEST, Markus Probst wrote:
> @@ -866,6 +866,7 @@ static bool acpi_of_match_device(const struct acpi_device *adev,
>  
>  	return false;
>  }
> +EXPORT_SYMBOL_GPL(acpi_of_match_device);

We only use this from Rust core code, so I'd rather avoid exporting this symbol.

We can work around the compiler inlining this into modules with this indirection
instead:

+#[inline(never)]
+#[allow(clippy::missing_safety_doc)]
+#[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_device_id`.
+    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
@@ -373,7 +385,7 @@ fn of_id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> {
             // - `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 { bindings::acpi_of_match_device(adev, table.as_ptr(), &raw mut raw_id) } {
+            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`.

Please see also [1].

[1] https://patch.msgid.link/20260213220718.82835-6-dakr@kernel.org

> @@ -329,35 +334,60 @@ fn acpi_id_info(dev: &device::Device) -> Option<&'static Self::IdInfo> {
>      ///
>      /// 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> {
> -        #[cfg(not(CONFIG_OF))]
> +        let table = Self::of_id_table()?;
> +
> +        #[cfg(not(any(CONFIG_OF, CONFIG_ACPI)))]
>          {
> -            let _ = dev;
> -            None
> +            let _ = (dev, table);
>          }
>  
>          #[cfg(CONFIG_OF)]
>          {
> -            let table = Self::of_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::of_match_device(table.as_ptr(), dev.as_raw()) };
>  
> -            if raw_id.is_null() {
> -                None
> -            } else {
> +            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>() };
>  
> -                Some(
> -                    table.info(<of::DeviceId as crate::device_id::RawDeviceIdIndex>::index(
> -                        id,
> -                    )),
> -                )
> +                return Some(table.info(
> +                    <of::DeviceId as crate::device_id::RawDeviceIdIndex>::index(id),
> +                ));
> +            }
> +        }
> +
> +        #[cfg(CONFIG_ACPI)]
> +        {
> +            let mut raw_id = ptr::null();
> +
> +            // SAFETY: `dev.fwnode().as_raw()` 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(dev.fwnode().map_or(ptr::null_mut(), FwNode::as_raw))
> +            };

Please only pass the final pointer to bindings::to_acpi_device_node() to keep
unsafe {} sections as short as possible.

> +
> +            // 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 { bindings::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>() };
> +
> +                return Some(table.info(
> +                    <of::DeviceId as crate::device_id::RawDeviceIdIndex>::index(id),
> +                ));
>              }
>          }
> +
> +        None
>      }

      parent reply	other threads:[~2026-04-04 22:07 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-01 14:06 [PATCH] rust: ACPI: fix missing match data for PRP0001 Markus Probst
2026-04-01 18:32 ` Greg Kroah-Hartman
2026-04-01 18:46   ` Markus Probst
2026-04-01 22:15     ` Danilo Krummrich
2026-04-02 12:31       ` Greg Kroah-Hartman
2026-04-04 21:23       ` Gary Guo
2026-04-04 21:32         ` Danilo Krummrich
2026-04-05  5:47           ` Greg Kroah-Hartman
2026-04-04 21:33         ` Markus Probst
2026-04-04 21:38           ` Danilo Krummrich
2026-04-04 22:08           ` Gary Guo
2026-04-04 22:07 ` Danilo Krummrich [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=DHKPNZT8IEO6.1JZ04J5AX26QF@kernel.org \
    --to=dakr@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=driver-core@lists.linux.dev \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=lenb@kernel.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=markus.probst@posteo.de \
    --cc=ojeda@kernel.org \
    --cc=rafael@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /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