Linux PCI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Danilo Krummrich" <dakr@kernel.org>
Cc: linux-pci@vger.kernel.org, ojeda@kernel.org
Subject: Re: [PATCH v3 10/27] rust: pci: make Driver trait lifetime-parameterized
Date: Sun, 17 May 2026 00:29:02 +0000	[thread overview]
Message-ID: <20260517002903.26386C19425@smtp.kernel.org> (raw)
In-Reply-To: <20260517000149.3226762-11-dakr@kernel.org>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] The introduction of the `'bound` lifetime parameter allows drivers to capture short-lived references into driver private data, leading to a formal use-after-free (provenance violation) and Undefined Behavior when accessed later.
- [Low] The signature of `unbind` in `samples/rust/rust_driver_pci.rs` does not match the newly parameterized `pci::Driver` trait, causing a compilation error.
--

commit e20151fd03221a2c3958612a0dabaa50cdd67768
Author: Danilo Krummrich <dakr@kernel.org>

rust: pci: make Driver trait lifetime-parameterized

This changes the pci::Driver trait to add a 'bound lifetime to the
associated Data type, allowing bus device private data to capture the
device and driver bound lifetime.

> diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs
> index c06ac3fb2a64f..8060861199d41 100644
> --- a/rust/kernel/pci.rs
> +++ b/rust/kernel/pci.rs

[ ... ]

> @@ -130,7 +130,7 @@ extern "C" fn remove_callback(pdev: *mut bindings::pci_dev) {
>          // SAFETY: `remove_callback` is only ever called after a successful call to
>          // `probe_callback`, hence it's guaranteed that `Device::set_drvdata()` has been called
>          // and stored a `Pin<KBox<T::Data>>`.
> -        let data = unsafe { pdev.as_ref().drvdata_borrow::<T::Data>() };
> +        let data = unsafe { pdev.as_ref().drvdata_borrow::<T::Data<'_>>() };

Does this lead to a formal use-after-free and provenance violation under
Rust's aliasing rules?

In probe_callback, a reference to pdev is created with a lifetime restricted
to the callback's scope, which is stored in Data<'probe>.

Since set_drvdata stores this data by converting it to a raw pointer, the
'probe lifetime formally ends when probe_callback returns.

Between probe and remove, the C subsystem can mutate the pci_dev struct using
its parent raw pointer, which invalidates the child reference created during
probe.

When remove_callback calls drvdata_borrow, does this execute an unchecked
transmute from the expired 'probe lifetime to a fresh 'remove lifetime?

When the driver accesses these references, will it use a reference with an
invalidated provenance tag and trigger undefined behavior?

Would it be safer to use ARef<Device>, which stores a raw pointer and creates
fresh references on demand, rather than circumventing the borrow checker with
synthetic lifetimes?

[ ... ]

> diff --git a/samples/rust/rust_driver_pci.rs b/samples/rust/rust_driver_pci.rs
> index f43c6a660b395..30b9311812f3e 100644
> --- a/samples/rust/rust_driver_pci.rs
> +++ b/samples/rust/rust_driver_pci.rs
> @@ -140,11 +140,14 @@ fn config_space(pdev: &pci::Device<Bound>) {
>  
>  impl pci::Driver for SampleDriver {
>      type IdInfo = TestIndex;
> -    type Data = Self;
> +    type Data<'bound> = Self;
>  
>      const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
>  
> -    fn probe(pdev: &pci::Device<Core>, info: &Self::IdInfo) -> impl PinInit<Self, Error> {
> +    fn probe<'bound>(
> +        pdev: &'bound pci::Device<Core>,
> +        info: &'bound Self::IdInfo,
> +    ) -> impl PinInit<Self, Error> + 'bound {
>          pin_init::pin_init_scope(move || {
>              let vendor = pdev.vendor_id();
>              dev_dbg!(

Does the unbind method in this file also need to be updated to match the new
pci::Driver trait signature?

Since the trait adds a 'bound lifetime parameter to unbind, failing to update
the implementation here leaves elided lifetimes that resolve to two separate
anonymous lifetimes.

Will the compiler reject this with a lifetime parameter mismatch error,
breaking the build?

I note this regression is fixed later in the patch series by the commit
"samples: rust: rust_driver_pci: use HRT lifetime for Bar", but does it break
git bisectability here?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260517000149.3226762-1-dakr@kernel.org?part=10

  reply	other threads:[~2026-05-17  0:29 UTC|newest]

Thread overview: 98+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-17  0:00 [PATCH v3 00/27] rust: device: Higher-Ranked Lifetime Types for device drivers Danilo Krummrich
2026-05-17  0:00 ` [PATCH v3 01/27] rust: alloc: remove `'static` bound on `ForeignOwnable` Danilo Krummrich
2026-05-18 14:42   ` Alexandre Courbot
2026-05-17  0:00 ` [PATCH v3 02/27] rust: driver: move 'static bounds to constructor Danilo Krummrich
2026-05-18 14:42   ` Alexandre Courbot
2026-05-17  0:00 ` [PATCH v3 03/27] rust: driver: decouple driver private data from driver type Danilo Krummrich
2026-05-17  0:19   ` sashiko-bot
2026-05-17 14:32   ` Danilo Krummrich
2026-05-19 12:47     ` Gary Guo
2026-05-18 14:43   ` Alexandre Courbot
2026-05-17  0:00 ` [PATCH v3 04/27] rust: driver core: drop drvdata before devres release Danilo Krummrich
2026-05-17  0:37   ` sashiko-bot
2026-05-18 14:45   ` Alexandre Courbot
2026-05-19 12:47   ` Gary Guo
2026-05-17  0:00 ` [PATCH v3 05/27] rust: pci: implement Sync for Device<Bound> Danilo Krummrich
2026-05-17  0:40   ` sashiko-bot
2026-05-18 14:46   ` Alexandre Courbot
2026-05-19 13:01   ` Gary Guo
2026-05-17  0:00 ` [PATCH v3 06/27] rust: platform: " Danilo Krummrich
2026-05-18 14:46   ` Alexandre Courbot
2026-05-19 13:01   ` Gary Guo
2026-05-17  0:00 ` [PATCH v3 07/27] rust: auxiliary: " Danilo Krummrich
2026-05-17  0:36   ` sashiko-bot
2026-05-18 14:47   ` Alexandre Courbot
2026-05-19 13:02   ` Gary Guo
2026-05-17  0:00 ` [PATCH v3 08/27] rust: usb: " Danilo Krummrich
2026-05-17  0:33   ` sashiko-bot
2026-05-18 14:47   ` Alexandre Courbot
2026-05-19 13:02   ` Gary Guo
2026-05-17  0:00 ` [PATCH v3 09/27] rust: device: " Danilo Krummrich
2026-05-17  0:25   ` sashiko-bot
2026-05-18 14:48   ` Alexandre Courbot
2026-05-19 13:02   ` Gary Guo
2026-05-17  0:00 ` [PATCH v3 10/27] rust: pci: make Driver trait lifetime-parameterized Danilo Krummrich
2026-05-17  0:29   ` sashiko-bot [this message]
2026-05-18 14:53   ` Alexandre Courbot
2026-05-18 15:36   ` Gary Guo
2026-05-18 16:10     ` Danilo Krummrich
2026-05-19  4:52   ` Eliot Courtney
2026-05-19 10:39     ` Danilo Krummrich
2026-05-19 11:48       ` Gary Guo
2026-05-19 12:36         ` Danilo Krummrich
2026-05-20  6:14           ` Eliot Courtney
2026-05-17  0:00 ` [PATCH v3 11/27] rust: platform: " Danilo Krummrich
2026-05-18 14:55   ` Alexandre Courbot
2026-05-17  0:01 ` [PATCH v3 12/27] rust: auxiliary: " Danilo Krummrich
2026-05-18 15:39   ` Alexandre Courbot
2026-05-17  0:01 ` [PATCH v3 13/27] rust: usb: " Danilo Krummrich
2026-05-17  0:25   ` sashiko-bot
2026-05-18 15:40   ` Alexandre Courbot
2026-05-17  0:01 ` [PATCH v3 14/27] rust: i2c: " Danilo Krummrich
2026-05-17  0:39   ` sashiko-bot
2026-05-18 15:41   ` Alexandre Courbot
2026-05-17  0:01 ` [PATCH v3 15/27] rust: driver: update module documentation for GAT-based Data type Danilo Krummrich
2026-05-18 15:46   ` Alexandre Courbot
2026-05-17  0:01 ` [PATCH v3 16/27] rust: types: add `ForLt` trait for higher-ranked lifetime support Danilo Krummrich
2026-05-17  0:23   ` sashiko-bot
2026-05-19  6:02   ` Eliot Courtney
2026-05-19 11:23     ` Gary Guo
2026-05-19 11:07   ` Alexandre Courbot
2026-05-19 11:39     ` Gary Guo
2026-05-19 13:03       ` Danilo Krummrich
2026-05-19 13:34         ` Miguel Ojeda
2026-05-17  0:01 ` [PATCH v3 17/27] rust: auxiliary: generalize Registration over ForLt Danilo Krummrich
2026-05-17  0:31   ` sashiko-bot
2026-05-19  7:56   ` Eliot Courtney
2026-05-19 10:39     ` Danilo Krummrich
2026-05-19 11:20       ` Gary Guo
2026-05-19 16:45   ` Gary Guo
2026-05-20  0:33     ` Danilo Krummrich
2026-05-20  9:34       ` Gary Guo
2026-05-17  0:01 ` [PATCH v3 18/27] samples: rust: rust_driver_auxiliary: showcase lifetime-bound registration data Danilo Krummrich
2026-05-19  6:52   ` Eliot Courtney
2026-05-19 15:48   ` Gary Guo
2026-05-17  0:01 ` [PATCH v3 19/27] rust: pci: make Bar lifetime-parameterized Danilo Krummrich
2026-05-17  0:57   ` sashiko-bot
2026-05-19  6:36   ` Eliot Courtney
2026-05-19 16:24   ` Gary Guo
2026-05-19 17:27     ` Danilo Krummrich
2026-05-17  0:01 ` [PATCH v3 20/27] rust: io: make IoMem and ExclusiveIoMem lifetime-parameterized Danilo Krummrich
2026-05-17  1:31   ` sashiko-bot
2026-05-19  6:39   ` Eliot Courtney
2026-05-17  0:01 ` [PATCH v3 21/27] samples: rust: rust_driver_pci: use HRT lifetime for Bar Danilo Krummrich
2026-05-17  0:57   ` sashiko-bot
2026-05-19  6:41   ` Eliot Courtney
2026-05-17  0:01 ` [PATCH v3 22/27] rust: driver-core: rename 'a lifetime to 'bound Danilo Krummrich
2026-05-17  0:31   ` sashiko-bot
2026-05-19  6:42   ` Eliot Courtney
2026-05-19 16:56   ` Gary Guo
2026-05-19 17:23     ` Danilo Krummrich
2026-05-17  0:01 ` [PATCH REF v3 23/27] gpu: nova-core: " Danilo Krummrich
2026-05-17  0:01 ` [PATCH REF v3 24/27] gpu: nova-core: use lifetime for Bar Danilo Krummrich
2026-05-17  0:58   ` sashiko-bot
2026-05-17  0:01 ` [PATCH REF v3 25/27] gpu: nova-core: unregister sysmem flush page from Drop Danilo Krummrich
2026-05-17  0:50   ` sashiko-bot
2026-05-17  0:01 ` [PATCH REF v3 26/27] gpu: nova-core: replace ARef<Device> with &'bound Device in SysmemFlush Danilo Krummrich
2026-05-17  0:01 ` [PATCH REF v3 27/27] gpu: drm: tyr: use lifetime for IoMem Danilo Krummrich
2026-05-17  0:47   ` sashiko-bot

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=20260517002903.26386C19425@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dakr@kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=sashiko-reviews@lists.linux.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