Skip to contentSkip to conversation

rust: Update PCI binding safety comments and add inline compiler hint

PATCH In review Alistair Popple 3 participants 4 messages Jul 30, 2025 – Aug 15, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 rust: Update PCI binding safety comments and add inline compiler hint

Update the safety comments to be consistent with other safety comments in the PCI bindings. Also add an inline compiler hint.

Cc Danilo Krummrich <[email protected]>
Cc Bjorn Helgaas <[email protected]>
Cc Krzysztof Wilczyński <[email protected]>
Cc Miguel Ojeda <[email protected]>
Cc Alex Gaynor <[email protected]>
Cc Boqun Feng <[email protected]>
Cc Gary Guo <[email protected]>
Cc Björn Roy Baron <[email protected]>
Cc Benno Lossin <[email protected]>
Cc Andreas Hindborg <[email protected]>
Cc Alice Ryhl <[email protected]>
Cc Trevor Gross <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Rafael J. Wysocki <[email protected]>
Cc John Hubbard <[email protected]>
Cc Alexandre Courbot <[email protected]>

---

Changes for v3:

- Updated capitalisation of SAFETY comments

---
 rust/kernel/pci.rs | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
rust/kernel/pci.rs +6 -2
index 8435f8132e381..7da1712398938 100644
@@ -371,14 +371,18 @@ fn as_raw(&self) -> *mut bindings::pci_dev {
 
 impl Device {
     /// Returns the PCI vendor ID.
+    #[inline]
     pub fn vendor_id(&self) -> u16 {
-        // SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`.
+        // SAFETY: By its type invariant `self.as_raw` is always a valid pointer to a
+        // `struct pci_dev`.
         unsafe { (*self.as_raw()).vendor }
     }
 
     /// Returns the PCI device ID.
+    #[inline]
     pub fn device_id(&self) -> u16 {
-        // SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`.
+        // SAFETY: By its type invariant `self.as_raw` is always a valid pointer to a
+        // `struct pci_dev`.
         unsafe { (*self.as_raw()).device }
     }
 
Signature · 1 line
2.47.2
#1 lore raw

PATCH v3 2/2 rust: Add several miscellaneous PCI helpers

Add bindings to obtain a PCI device's resource start address, bus/ device function, revision ID and subsystem device and vendor IDs.

These will be used by the nova-core GPU driver which is currently in development.

Cc Danilo Krummrich <[email protected]>
Cc Bjorn Helgaas <[email protected]>
Cc Krzysztof Wilczyński <[email protected]>
Cc Miguel Ojeda <[email protected]>
Cc Alex Gaynor <[email protected]>
Cc Boqun Feng <[email protected]>
Cc Gary Guo <[email protected]>
Cc Björn Roy Baron <[email protected]>
Cc Benno Lossin <[email protected]>
Cc Andreas Hindborg <[email protected]>
Cc Alice Ryhl <[email protected]>
Cc Trevor Gross <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Rafael J. Wysocki <[email protected]>
Cc John Hubbard <[email protected]>
Cc Alexandre Courbot <[email protected]>

---

Changes for v3:

- Fixed capitalisation of SAFETY comments.
- There was a long discussion about the SAFETY comments on v2 of the series[1]. I don't think anything actionable came of it so I haven't made any changes as a result of that discussion.

[1] - https://lore.kernel.org/rust-for-linux/[email protected]/

---
 rust/helpers/pci.c | 10 ++++++++++
 rust/kernel/pci.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+)
rust/helpers/pci.c +10 -0
index cd0e6bf2cc4d9..59d15bd4bdb13 100644
@@ -12,6 +12,16 @@ void *rust_helper_pci_get_drvdata(struct pci_dev *pdev)
 	return pci_get_drvdata(pdev);
 }
 
+u16 rust_helper_pci_dev_id(struct pci_dev *dev)
+{
+	return PCI_DEVID(dev->bus->number, dev->devfn);
+}
+
+resource_size_t rust_helper_pci_resource_start(struct pci_dev *pdev, int bar)
+{
+	return pci_resource_start(pdev, bar);
+}
+
 resource_size_t rust_helper_pci_resource_len(struct pci_dev *pdev, int bar)
 {
 	return pci_resource_len(pdev, bar);
rust/kernel/pci.rs +44 -0

50 lines not on this page · raw message

Signature · 1 line
2.47.2
#2 lore raw
In reply to Alistair Popple
Quoted 2 lines · Alistair Popple

I think this list of Ccs is automated, given even the LKML is included, but the Cc: tag inside the commit message is not meant to be used to mention every maintainer/reviewer, i.e. it is normally used when you want to document that someone was contacted (i.e. for a particular reason, not in general).

Otherwise, all commits would have very long lists of Ccs and the value of the tag is diminished.

Cheers,
Miguel

maintainer
#3 lore raw
In reply to Alistair Popple
Quoted 15 lines · Alistair Popple

Update the safety comments to be consistent with other safety comments in the PCI bindings. Also add an inline compiler hint.

Cc Danilo Krummrich <[email protected]>
Cc Bjorn Helgaas <[email protected]>
Cc Krzysztof Wilczyński <[email protected]>
Cc Miguel Ojeda <[email protected]>
Cc Alex Gaynor <[email protected]>
Cc Boqun Feng <[email protected]>
Cc Gary Guo <[email protected]>
Cc Björn Roy Baron <[email protected]>
Cc Benno Lossin <[email protected]>
Cc Andreas Hindborg <[email protected]>
Cc Alice Ryhl <[email protected]>
Quoted 8 lines
Cc Trevor Gross <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Rafael J. Wysocki <[email protected]>
Cc John Hubbard <[email protected]>
Cc Alexandre Courbot <[email protected]>

Applied to driver-core-testing, thanks!

Alistair, do you mind sending a follow-up patch to replace the raw bindings:resource_size_t return types on resource_start() and resource_len() with the Rust ResourceSize type?