Add Rust PCI SR-IOV support
Subsystems (11)
cover Add Rust PCI SR-IOV support
Rust PCI drivers need to enable and disable SR-IOV and share selected PF-owned functionality with their VF drivers. The shared data must remain valid while a VF driver is bound, including when the VF driver is in C.
This series builds on Peter Colberg's Rust PCI SR-IOV v3 series [1], extending it with typed PF registration data and C FFI support. The registration design follows Danilo Krummrich's Rust vGPU/VFIO PoC [2]. As the discussion on Rust/VFIO support is still ongoing [3], this series supports both C and Rust sample VF drivers.
Add the PCI SR-IOV operations and callback, managed VF removal during PF unbind, and typed PF registration data. A pinned registration publishes the PF payload. Rust VFs borrow the typed data directly; C VFs use an ABI-checked descriptor and generated C-to-Rust trampolines.
Include Rust and C VF samples and integrate the registration into nova-core. Nova implements sriov_configure() and publishes PF-readiness data before VFs are enabled. Document the sharing interface, its borrow lifetimes and teardown ordering.
[1] https://lore.kernel.org/rust-for-linux/[email protected]/
[2] https://lore.kernel.org/nova-gpu/[email protected]/
[3] https://lore.kernel.org/nova-gpu/[email protected]/
John Hubbard (1):
rust: pci: add is_virtfn(), to check for VFs
Peter Colberg (7):
PCI: add driver flag to opt into disabling SR-IOV on remove()
rust: pci: add {enable,disable}_sriov(), to control SR-IOV capability
rust: pci: add vtable attribute to pci::Driver trait
rust: pci: add bus callback sriov_configure(), to control SR-IOV from
sysfs
rust: pci: add is_physfn(), to check for PFs
rust: pci: add num_vf(), to return number of VFs
samples: rust: add Rust SR-IOV VF driver sample
Zhi Wang (6):
rust: pci: add typed SR-IOV PF registration data
rust: add C-to-Rust FFI descriptors and trampolines
rust: pci: add C FFI support to typed SR-IOV PF registration data
samples: rust: add C SR-IOV VF driver that calls into a Rust PF driver
gpu: nova-core: publish typed SR-IOV PF data for VF drivers
Documentation: rust: explain SR-IOV PF data sharing with VFs
Documentation/rust/index.rst | 1 + Documentation/rust/pci-sriov-pf-data.rst | 330 +++++++++++++++++++++ MAINTAINERS | 4 + drivers/gpu/nova-core/driver.rs | 34 ++- drivers/pci/iov.c | 103 ++++++- drivers/pci/pci-driver.c | 3 +- drivers/pci/pci.h | 2 + include/linux/pci.h | 44 +++ include/linux/rust_ffi.h | 88 ++++++ rust/bindings/bindings_helper.h | 5 + rust/kernel/interop.rs | 5 +- rust/kernel/interop/ffi.rs | 252 ++++++++++++++++ rust/kernel/pci.rs | 126 ++++++++ rust/kernel/pci/sriov.rs | 355 +++++++++++++++++++++++ rust/macros/ffi_vtable.rs | 148 ++++++++++ rust/macros/lib.rs | 90 ++++++ samples/rust/Kconfig | 26 ++ samples/rust/Makefile | 2 + samples/rust/rust_dma.rs | 1 + samples/rust/rust_driver_auxiliary.rs | 1 + samples/rust/rust_driver_pci.rs | 1 + samples/rust/rust_driver_sriov.h | 27 ++ samples/rust/rust_driver_sriov.rs | 270 +++++++++++++++++ samples/rust/rust_driver_sriov_c_vf.c | 61 ++++ 24 files changed, 1972 insertions(+), 7 deletions(-) create mode 100644 Documentation/rust/pci-sriov-pf-data.rst create mode 100644 include/linux/rust_ffi.h create mode 100644 rust/kernel/interop/ffi.rs create mode 100644 rust/kernel/pci/sriov.rs create mode 100644 rust/macros/ffi_vtable.rs create mode 100644 samples/rust/rust_driver_sriov.h create mode 100644 samples/rust/rust_driver_sriov.rs create mode 100644 samples/rust/rust_driver_sriov_c_vf.c
base-commit: 34a8ecc902e907d7b0596a8d38b437e90eac0701
PATCH 1/14 PCI: add driver flag to opt into disabling SR-IOV on remove()
From: Peter Colberg <[email protected]>
Add a flag managed_sriov to the pci_driver structure that allows a driver to opt into disabling the Single Root I/O Virtualization (SR-IOV) capability of the device when the driver is unbound.
Add a new function pci_iov_disable() that is invoked before the remove() callback of a PCI driver and checks for the presence of the new flag. If the flag is set, invoke the sriov_configure() callback to allow the driver to gracefully disable SR-IOV. Warn if the driver fails to do so and forcibly disable SR-IOV using sriov_disable().
Since a (broken) driver may theoretically re-enable SR-IOV during its remove() callback, extend pci_iov_remove() to forcibly disable SR-IOV after remove() if needed and only if the flag managed_sriov is set.
Create a persistent managed device link from each VF to its PF before the VF can probe when managed_sriov is set. The driver core therefore waits for an in-progress VF probe and unbinds a bound VF before unbinding the PF driver.
Altogether the flag ensures that when a Virtual Function (VF) is bound to a driver, the corresponding Physical Function (PF) is bound to a driver, too, since the VF devices are destroyed when the PF driver is unbound. This guarantee is a prerequisite for exposing a safe Rust API that allows a VF driver to obtain the PF device for a VF device and subsequently access the device private data of the PF device.
--- drivers/pci/iov.c | 52 +++++++++++++++++++++++++++++++++++++++- drivers/pci/pci-driver.c | 3 ++- drivers/pci/pci.h | 2 ++ include/linux/pci.h | 10 ++++++++ 4 files changed, 65 insertions(+), 2 deletions(-)
drivers/pci/iov.c +51 -1
@@ -382,6 +382,17 @@ int pci_iov_add_virtfn(struct pci_dev *dev, int id) }pci_device_add (virtfn, virtfn->bus); +/* +* managed_sriov makes the VF driver binding depend on the PF driver +* binding. Add the device link before the VF can probe so supplier +* unbind waits for an in-progress probe and unbinds the VF first. +*/ +if (dev->driver && dev->driver->managed_sriov && + !device_link_add (&virtfn->dev, &dev->dev,0 )) { +pci_err (virtfn,"failed to link VF to managed SR-IOV PF\n" ); + rc = -EINVAL ; + goto failed1; + } rc =pci_iov_sysfs_link (dev, virtfn, id);if (rc) goto failed1;
@@ -1015,20 +1026,59 @@ void pci_iov_release(struct pci_dev *dev)sriov_release (dev); } +/** +* pci_iov_disable - disable SR-IOV before PF driver is detached +* @dev: the PCI device +* +* Invoke sriov_configure() callback to allow the driver to gracefully disable +* SR-IOV. Warn if the driver fails to do so and forcibly disable SR-IOV. +*/ +void pci_iov_disable (struct pci_dev *dev) +{ +struct pci_driver *drv = dev->driver; +struct pci_sriov *iov = dev->sriov; + +if (WARN_ON (!drv)) +return ; + +if (!dev->is_physfn || !iov->num_VFs || !drv->managed_sriov) +return ; + +if (!drv->sriov_configure) { +sriov_disable (dev); +return ; + } + + drv->sriov_configure (dev,0 ); + +if (WARN_ON (iov->num_VFs)) +sriov_disable (dev); +} +/** * pci_iov_remove - clean up SR-IOV state after PF driver is detached * @dev: the PCI device */ void pci_iov_remove (struct pci_dev *dev) { +struct pci_driver *drv = dev->driver;struct pci_sriov *iov = dev->sriov; +if (WARN_ON (!drv)) +return ; +if (!dev->is_physfn)return ; iov->driver_max_VFs = iov->total_VFs; -if (iov->num_VFs) + +if (iov->num_VFs && !drv->managed_sriov) {pci_warn (dev,"driver left SR-IOV enabled after remove\n" ); +return ; + } + +if (WARN_ON (iov->num_VFs)) +sriov_disable (dev); }/**
drivers/pci/pci-driver.c +2 -1
@@ -523,6 +523,7 @@ static void pci_device_remove(struct device *dev)struct pci_dev *pci_dev =to_pci_dev (dev);struct pci_driver *drv = pci_dev->driver; +pci_iov_disable (pci_dev);if (drv->remove) {pm_runtime_get_sync (dev);/*
@@ -536,8 +537,8 @@ static void pci_device_remove(struct device *dev)pm_runtime_put_noidle (dev); }pcibios_free_irq (pci_dev); - pci_dev->driver =NULL ;pci_iov_remove (pci_dev); + pci_dev->driver =NULL ;/* Undo the runtime PM settings in local_pci_probe() */ pm_runtime_put_sync (dev);
drivers/pci/pci.h +2 -0
@@ -986,6 +986,7 @@ static inline void pci_restore_pasid_state(struct pci_dev *pdev) { }#ifdef CONFIG_PCI_IOV int pci_iov_init (struct pci_dev *dev);void pci_iov_release (struct pci_dev *dev); +void pci_iov_disable (struct pci_dev *dev);void pci_iov_remove (struct pci_dev *dev);void pci_iov_update_resource (struct pci_dev *dev,int resno); resource_size_t pci_sriov_resource_alignment(const struct pci_dev *dev,
@@ -1021,6 +1022,7 @@ static inline int pci_iov_init(struct pci_dev *dev)return -ENODEV ; }static inline void pci_iov_release (struct pci_dev *dev) { } +static inline void pci_iov_disable (struct pci_dev *dev) { }static inline void pci_iov_remove (struct pci_dev *dev) { }static inline void pci_iov_update_resource (struct pci_dev *dev,int resno) { } static inline resource_size_t pci_sriov_resource_alignment(const struct pci_dev *dev,
include/linux/pci.h +10 -0
@@ -1025,6 +1025,15 @@ struct module;* how to manage the DMA themselves and set this flag so that * the IOMMU layer will allow them to setup and manage their * own I/O address space. +* @managed_sriov: Disable SR-IOV on remove(). +* If set, the Single Root I/O Virtualization (SR-IOV) +* capability of the device is disabled when the driver is +* unbound from the device, by calling sriov_configure() +* before remove(). The PCI core creates a managed device link +* from each Virtual Function (VF) to its Physical Function (PF) +* before the VF is probed. The presence of this flag therefore +* guarantees that when a VF is bound to a driver, the PF is bound +* to a driver, too, and that the VF is unbound first. */ struct pci_driver {const char *name;
@@ -1043,6 +1052,7 @@ struct pci_driver {struct device_driver driver;struct pci_dynids dynids;bool driver_managed_dma; +bool managed_sriov; };#define to_pci_driver (__drv) \
PATCH 2/14 rust: pci: add {enable,disable}_sriov(), to control SR-IOV capability
In reply to Zhi WangFrom: Peter Colberg <[email protected]>
Add methods to enable and disable the Single Root I/O Virtualization (SR-IOV) capability for a PCI device. The wrapped C methods take care of validating whether the device is a Physical Function (PF), whether SR-IOV is currently disabled (or enabled), and whether the number of requested VFs does not exceed the total number of supported VFs.
Set the flag managed_sriov to always disable SR-IOV when a Rust PCI driver is unbound from a PF device. This ensures that when a Virtual Function (VF) is bound to a driver, the corresponding Physical Function (PF) is bound to a driver, too, which is a prerequisite for exposing a safe Rust API that allows a VF driver to obtain the PF device for a VF device and subsequently access the private data of the PF driver.
--- rust/kernel/pci.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
rust/kernel/pci.rs +33 -0
@@ -93,6 +93,7 @@ unsafe fn register( (*pdrv.get ()).probe =Some (Self ::probe_callback); (*pdrv.get ()).remove =Some (Self ::remove_callback); (*pdrv.get ()).id_table =T ::ID_TABLE .as_ptr (); + (*pdrv.get ()).managed_sriov =true ; }// SAFETY: `pdrv` is guaranteed to be a valid `DriverType`.
@@ -506,6 +507,38 @@ pub fn set_master(&self) {// SAFETY: `self.as_raw` is guaranteed to be a pointer to a valid `struct pci_dev`. unsafe { bindings::pci_set_master (self.as_raw ()) }; } + +/// Enable the Single Root I/O Virtualization (SR-IOV) capability for this device, +/// where `nr_virtfn` is number of Virtual Functions (VF) to enable. +#[cfg( CONFIG_PCI_IOV )] +pub fn enable_sriov (&self, nr_virtfn:i32 ) ->Result { +// SAFETY: +// `self.as_raw` returns a valid pointer to a `struct pci_dev`. +// +// `pci_enable_sriov()` checks that the enable operation is valid: +// - the device is a Physical Function (PF), +// - SR-IOV is currently disabled, and +// - `nr_virtfn` does not exceed the total number of supported VFs. +// +// The Core device context inherits from the Bound device context, +// which guarantees that the PF device is bound to a driver. +to_result (unsafe { bindings::pci_enable_sriov (self.as_raw (), nr_virtfn) }) + } + +/// Disable the Single Root I/O Virtualization (SR-IOV) capability for this device. +#[cfg( CONFIG_PCI_IOV )] +pub fn disable_sriov (&self) { +// SAFETY: +// `self.as_raw` returns a valid pointer to a `struct pci_dev`. +// +// `pci_disable_sriov()` checks that the disable operation is valid: +// - the device is a Physical Function (PF), and +// - SR-IOV is currently enabled. +// +// The Core device context inherits from the Bound device context, +// which guarantees that the PF device is bound to a driver. +unsafe { bindings::pci_disable_sriov (self.as_raw ()) }; + } }// SAFETY: `pci::Device` is a transparent wrapper of `struct pci_dev`.
PATCH 3/14 rust: pci: add vtable attribute to pci::Driver trait
In reply to Zhi WangFrom: Peter Colberg <[email protected]>
Add the #[vtable] attribute to pci::Driver trait and implementations, to prepare a subsequent patch that adds an optional bus callback sriov_configure() to enable or disable the SR-IOV capability.
--- drivers/gpu/nova-core/driver.rs | 1 + rust/kernel/pci.rs | 2 ++ samples/rust/rust_dma.rs | 1 + samples/rust/rust_driver_auxiliary.rs | 1 + samples/rust/rust_driver_pci.rs | 1 + 5 files changed, 6 insertions(+)
drivers/gpu/nova-core/driver.rs +1 -0
@@ -94,6 +94,7 @@ pub(crate) fn bar1_resource_index(pdev: &pci::Device<Bound>) -> Result<u32> { ] ); +#[vtable] impl pci::Driver for NovaCoreDriver {type IdInfo = ();type Data <'bound> =NovaCore <'bound>;
rust/kernel/pci.rs +2 -0
@@ -282,6 +282,7 @@ macro_rules! pci_device_table {/// ] /// ); /// +/// #[vtable] /// impl pci::Driver for MyDriver { /// type IdInfo = (); /// type Data<'bound> = Self;
@@ -297,6 +298,7 @@ macro_rules! pci_device_table {///``` /// Drivers must implement this trait in order to get a PCI driver registered. Please refer to the /// `Adapter` documentation for an example. +#[vtable] pub trait Driver {/// The type holding information about each device id supported by the driver. // TODO: Use `associated_type_defaults` once stabilized:
samples/rust/rust_dma.rs +1 -0
@@ -69,6 +69,7 @@ unsafe impl kernel::transmute::FromBytes for MyStruct {} [(pci::DeviceId ::from_id (pci::Vendor ::REDHAT ,0x5 ), ())] ); +#[vtable] impl pci::Driver for DmaSampleDriver {type IdInfo = ();type Data <'bound> =DmaSampleData <'bound>;
samples/rust/rust_driver_auxiliary.rs +1 -0
@@ -90,6 +90,7 @@ struct ParentData<'bound> { [(pci::DeviceId ::from_id (pci::Vendor ::REDHAT ,0x5 ), ())] ); +#[vtable] impl pci::Driver for ParentDriver {type IdInfo = ();type Data <'bound> =ParentData <'bound>;
samples/rust/rust_driver_pci.rs +1 -0
@@ -139,6 +139,7 @@ fn config_space(pdev: &pci::Device<Bound>) { } } +#[vtable] impl pci::Driver for SampleDriver {type IdInfo =TestIndex ;type Data <'bound> =SampleDriverData <'bound>;
PATCH 4/14 rust: pci: add bus callback sriov_configure(), to control SR-IOV from sysfs
In reply to Zhi WangFrom: Peter Colberg <[email protected]>
Add an optional bus callback sriov_configure() to pci::Driver trait, using the vtable attribute to query if the driver implements the callback. The callback is invoked when a user-space application writes the number of VFs to the sysfs file sriov_numvfs to enable SR-IOV, or zero to disable SR-IOV for a PCI device.
--- rust/kernel/pci.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+)
rust/kernel/pci.rs +56 -0
@@ -94,6 +94,10 @@ unsafe fn register( (*pdrv.get ()).remove =Some (Self ::remove_callback); (*pdrv.get ()).id_table =T ::ID_TABLE .as_ptr (); (*pdrv.get ()).managed_sriov =true ; +#[cfg( CONFIG_PCI_IOV )] +if T ::HAS_SRIOV_CONFIGURE { + (*pdrv.get ()).sriov_configure =Some (Self ::sriov_configure_callback); + } }// SAFETY: `pdrv` is guaranteed to be a valid `DriverType`.
@@ -150,6 +154,20 @@ extern "C" fn remove_callback(pdev: *mut bindings::pci_dev) {T ::unbind (pdev, data); } + +#[cfg( CONFIG_PCI_IOV )] +extern "C" fn sriov_configure_callback ( + pdev: *mut bindings::pci_dev , + nr_virtfn:c_int , + ) ->c_int { +// SAFETY: The PCI bus only ever calls the sriov_configure callback with a valid pointer to +// a `struct pci_dev`. +// +// INVARIANT: `pdev` is valid for the duration of `sriov_configure_callback()`. +let pdev =unsafe { &*pdev.cast ::<Device <device::CoreInternal <'_>>>() }; + +from_result (||T ::sriov_configure (pdev, nr_virtfn)) + } }/// Declares a kernel module that exposes a single PCI driver.
@@ -336,6 +354,44 @@ fn probe<'bound>(fn unbind <'bound>(dev: &'boundDevice <device::Core <'_>>, this:Pin <&Self ::Data <'bound>>) {let _ = (dev, this); } + +/// Single Root I/O Virtualization (SR-IOV) configure. +/// +/// Called when a user-space application enables or disables the SR-IOV capability for a +/// [`Device`] by writing the number of Virtual Functions (VF), `nr_virtfn` or zero to the +/// sysfs file `sriov_numvfs` for this device. Implementing this callback is optional. +/// +/// Further, and unlike for a PCI driver written in C, when a PF device with enabled VFs is +/// unbound from its bound [`Driver`], the `sriov_configure()` callback is invoked to disable +/// SR-IOV before the `unbind()` callback. This guarantees that when a VF device is bound to a +/// driver, the underlying PF device is bound to a driver, too. +/// +/// Upon success, this callback must return the number of VFs that were enabled, or zero if +/// SR-IOV was disabled. +/// +/// See [PCI Express I/O Virtualization]. +/// +/// [PCI Express I/O Virtualization]: https://docs.kernel.org/PCI/pci-iov-howto.html +/// +/// # Examples +/// +/// ``` +/// # use kernel::{device::Core, pci, prelude::*}; +/// #[cfg(CONFIG_PCI_IOV)] +/// fn sriov_configure(dev: &pci::Device<Core<'_>>, nr_virtfn: i32) -> Result<i32> { +/// if nr_virtfn == 0 { +/// dev.disable_sriov(); +/// } else { +/// dev.enable_sriov(nr_virtfn)?; +/// } +/// Ok(nr_virtfn) +/// } +/// ``` +#[cfg( CONFIG_PCI_IOV )] +fn sriov_configure (dev: &Device <device::Core <'_>>, nr_virtfn:i32 ) ->Result <i32 > { +let _ = (dev, nr_virtfn); +build_error! (crate ::error::VTABLE_DEFAULT_ERROR ) + } }/// The PCI device representation.
PATCH 5/14 rust: pci: add is_virtfn(), to check for VFs
In reply to Zhi WangFrom: John Hubbard <[email protected]>
Add a method to check if a PCI device is a Virtual Function (VF) created through Single Root I/O Virtualization (SR-IOV).
--- rust/kernel/pci.rs | 8 ++++++++ 1 file changed, 8 insertions(+)
rust/kernel/pci.rs +8 -0
@@ -507,6 +507,14 @@ pub fn resource_start(&self, bar: u32) -> Result<bindings::resource_size_t> {Ok (unsafe { bindings::pci_resource_start (self.as_raw (), bar.try_into ()?) }) } +/// Returns `true` if this device is a Virtual Function (VF). +#[inline] +#[expect(dead_code)] +pub (crate )fn is_virtfn (&self) ->bool { +// SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`. +unsafe { (*self.as_raw ()).is_virtfn () !=0 } + } +/// Returns the size of the given PCI BAR resource. pub fn resource_len (&self, bar:u32 ) ->Result <bindings::resource_size_t > {if !Bar ::index_is_valid (bar) {
PATCH 6/14 rust: pci: add is_physfn(), to check for PFs
In reply to Zhi WangFrom: Peter Colberg <[email protected]>
Add a method to check if a PCI device is a Physical Function (PF).
zhiw: Keep this role check private to the PCI SR-IOV implementation instead of extending the public Device API. Add a dead-code expectation because this standalone API commit has no in-tree caller.
--- rust/kernel/pci.rs | 8 ++++++++ 1 file changed, 8 insertions(+)
rust/kernel/pci.rs +8 -0
@@ -507,6 +507,14 @@ pub fn resource_start(&self, bar: u32) -> Result<bindings::resource_size_t> {Ok (unsafe { bindings::pci_resource_start (self.as_raw (), bar.try_into ()?) }) } +/// Returns `true` if this device is a Physical Function (PF). +#[inline] +#[expect(dead_code)] +pub (crate )fn is_physfn (&self) ->bool { +// SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`. +unsafe { (*self.as_raw ()).is_physfn () !=0 } + } +/// Returns `true` if this device is a Virtual Function (VF). #[inline] #[expect(dead_code)]
PATCH 7/14 rust: pci: add num_vf(), to return number of VFs
In reply to Zhi WangFrom: Peter Colberg <[email protected]>
Add a method to return the number of Virtual Functions (VF) enabled for a Physical Function (PF).
zhiw: Keep this helper private to the PCI SR-IOV implementation instead of extending the public Device API. Add a dead-code expectation because this standalone API commit has no in-tree caller.
--- rust/kernel/pci.rs | 8 ++++++++ 1 file changed, 8 insertions(+)
rust/kernel/pci.rs +8 -0
@@ -523,6 +523,14 @@ pub(crate) fn is_virtfn(&self) -> bool {unsafe { (*self.as_raw ()).is_virtfn () !=0 } } +/// Returns the number of Virtual Functions (VF) enabled for a Physical Function (PF). +#[cfg( CONFIG_PCI_IOV )] +#[expect(dead_code)] +pub (crate )fn num_vf (&self) ->i32 { +// SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`. +unsafe { bindings::pci_num_vf (self.as_raw ()) } + } +/// Returns the size of the given PCI BAR resource. pub fn resource_len (&self, bar:u32 ) ->Result <bindings::resource_size_t > {if !Bar ::index_is_valid (bar) {
PATCH 9/14 samples: rust: add Rust SR-IOV VF driver sample
In reply to Zhi WangFrom: Peter Colberg <[email protected]>
Add a new SR-IOV driver sample that demonstrates how to enable and disable the Single Root I/O Virtualization capability for a PCI device.
The sample may be exercised using QEMU's 82576 (igb) emulation.
Implement the PF and VF as ordinary PCI drivers. Publish pinned PF data with a mutex-protected counter inline in the PF driver data, then borrow and invoke it explicitly from VF probe. Register the VF driver first so it is ready before the PF can enable VFs.
--- MAINTAINERS | 1 + samples/rust/Kconfig | 11 ++ samples/rust/Makefile | 1 + samples/rust/rust_driver_sriov.rs | 241 ++++++++++++++++++++++++++++++ 4 files changed, 254 insertions(+) create mode 100644 samples/rust/rust_driver_sriov.rs
MAINTAINERS +1 -0
@@ -21141,6 +21141,7 @@ F: rust/helpers/pci.c F: rust/kernel/pci.rs F: rust/kernel/pci/ F: samples/rust/rust_driver_pci.rs +F: samples/rust/rust_driver_sriov.rs PCIE BANDWIDTH CONTROLLER M: Ilpo Järvinen <[email protected]>
samples/rust/Kconfig +11 -0
@@ -128,6 +128,17 @@ config SAMPLE_RUST_DRIVER_PLATFORM If unsure, say N. +config SAMPLE_RUST_DRIVER_SRIOV + tristate "SR-IOV Driver" + depends on PCI_IOV + help + This option builds the Rust SR-IOV driver sample. + + To compile this as a module, choose M here: + the module will be called rust_driver_sriov. + + If unsure, say N. + config SAMPLE_RUST_DRIVER_USB tristate "USB Driver" depends on USB = y
samples/rust/Makefile +1 -0
@@ -11,6 +11,7 @@ obj-$(CONFIG_SAMPLE_RUST_DRIVER_I2C) += rust_driver_i2c.o obj-$(CONFIG_SAMPLE_RUST_I2C_CLIENT) += rust_i2c_client.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_PCI) += rust_driver_pci.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_PLATFORM) += rust_driver_platform.o +obj-$(CONFIG_SAMPLE_RUST_DRIVER_SRIOV) += rust_driver_sriov.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_USB) += rust_driver_usb.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_FAUX) += rust_driver_faux.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_AUXILIARY) += rust_driver_auxiliary.o
samples/rust/rust_driver_sriov.rs new file +241 -0
241 lines not on this page · raw message
PATCH 8/14 rust: pci: add typed SR-IOV PF registration data
In reply to Zhi WangA VF driver may need to invoke PF-owned functionality without gaining access to all private data belonging to the PF driver. The PCI device alone cannot identify the restricted data type or express how long a borrow remains valid.
Add a Rust registration slot to struct pci_dev and VfRegistration to publish a ForLt-encoded payload. The registration is initialized and pinned inline in the PF driver data, then publishes its address as the final step of its own initialization. It remains inactive on conventional PCI functions and rejects VFs.
Check that no VFs or other registration exist before publication. Pinned drop disables SR-IOV before withdrawing the pointer and dropping the payload. Managed SR-IOV ordering additionally removes every VF before PF unbind.
Add vf_registration_data_with() for higher-ranked access and vf_registration_data() for covariant data. TypeId rejects mismatched Rust types without exposing the PF device or its complete driver data.
Keep SR-IOV operations behind a verified pci::sriov::Device view and pass a pinned reference to the PCI driver data into sriov_configure(). PF and VF consumers continue to use the ordinary pci::Driver abstraction.
Expose is_virtfn() so PF drivers can reject VFs before initializing hardware and publishing their registration.
--- include/linux/pci.h | 6 + rust/kernel/pci.rs | 69 +++++---- rust/kernel/pci/sriov.rs | 299 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 345 insertions(+), 29 deletions(-) create mode 100644 rust/kernel/pci/sriov.rs
include/linux/pci.h +6 -0
@@ -352,6 +352,9 @@ struct rcec_ea;* Such bridges are allocated additional MMIO and bus * number resources to allow for hierarchy expansion. * @is_pciehp: PCIe Hot-Plug Capable bridge. +* @vf_registration_data_rust: Rust registration data published by the PF +* before enabling VFs and retained until all VFs are +* removed. The PF driver must use managed_sriov. */ struct pci_dev {struct list_head bus_list;/* Node in per-bus list */
@@ -551,6 +554,9 @@ struct pci_dev {u16 ats_cap;/* ATS Capability offset */ u8 ats_stu;/* ATS Smallest Translation Unit */ #endif +#if defined(CONFIG_PCI_IOV ) && defined(CONFIG_RUST ) +void *vf_registration_data_rust; +#endif #ifdef CONFIG_PCI_PRI u16 pri_cap;/* PRI Capability offset */ u32 pri_reqs_alloc;/* Number of PRI requests allocated */
rust/kernel/pci.rs +40 -29
144 lines not on this page · raw message
rust/kernel/pci/sriov.rs new file +299 -0
299 lines not on this page · raw message
PATCH 11/14 rust: pci: add C FFI support to typed SR-IOV PF registration data
In reply to Zhi WangRust VF drivers can borrow PF-owned registration data directly, but C VF drivers need an ABI-checked operations table and a pinned context.
Place an optional struct rust_ffi descriptor at offset zero in the registration header while retaining the Rust TypeId. Initialize registrations created with new() with an empty descriptor, and add new_ffi() to construct a descriptor for the same pinned payload.
Add pci_iov_borrow_rust_pf_data() to locate the PF descriptor and validate the token, ABI version, and operations-table size. Require managed SR-IOV so device-link ordering and registration teardown keep the descriptor and its context alive until the VF driver is unbound.
--- drivers/pci/iov.c | 51 +++++++++++++++++ include/linux/pci.h | 34 +++++++++++- rust/kernel/interop/ffi.rs | 15 ++++- rust/kernel/pci/sriov.rs | 110 ++++++++++++++++++++++++++++--------- 4 files changed, 179 insertions(+), 31 deletions(-)
drivers/pci/iov.c +51 -0
@@ -11,6 +11,7 @@#include <linux/bits.h> #include <linux/log2.h> #include <linux/pci.h> +#include <linux/rust_ffi.h> #include <linux/sizes.h> #include <linux/slab.h> #include <linux/export.h>
@@ -80,6 +81,56 @@ void *pci_iov_get_pf_drvdata(struct pci_dev *dev, struct pci_driver *pf_driver) }EXPORT_SYMBOL_GPL (pci_iov_get_pf_drvdata); +#ifdef CONFIG_RUST +/** +* pci_iov_borrow_rust_pf_data - Validate and borrow Rust data from a VF's PF +* @dev: VF PCI device +* @token: Required FFI ABI token +* @abi_major: Required ABI major version +* @min_abi_minor: Minimum required ABI minor version +* @required_ops_size: Minimum required size of the operations table +* +* This may be called from a VF driver's probe() callback or from a context in +* which the VF driver is known to remain attached. If probe() succeeds, the +* returned pointer, its operations table, and its context are borrowed until +* the VF driver is fully unbound, including the return of its remove() callback +* when present. If probe() fails, the caller must discard the borrow before +* returning. The caller must drain all work that can use the FFI before the +* borrow ends. +* +* The PF must publish an immutable descriptor before enabling VFs, use +* managed_sriov, and retain the descriptor until its remove() callback. +* Managed SR-IOV installs a device link from every VF to its PF before the VF +* can probe. The driver core therefore waits for an in-progress VF probe and +* unbinds a bound VF before invoking the PF driver's remove() callback. +* +* Return: A borrowed FFI descriptor, or an ERR_PTR() value on failure. +*/ +const struct rust_ffi * +pci_iov_borrow_rust_pf_data (struct pci_dev *dev, +const struct rust_ffi_token *token, +u16 abi_major,u16 min_abi_minor, +size_t required_ops_size) +{ +const struct rust_ffi *ffi; +struct pci_dev *pf_dev; +struct pci_driver *pf_driver; + +if (!dev->is_virtfn) +return ERR_PTR (-EINVAL ); + + pf_dev =pci_physfn (dev); + pf_driver =READ_ONCE (pf_dev->driver); +if (!pf_driver || !READ_ONCE (pf_driver->managed_sriov)) +return ERR_PTR (-ENODEV ); + + ffi =READ_ONCE (pf_dev->vf_registration_data_rust); +return rust_ffi_borrow (ffi, token, abi_major, min_abi_minor, + required_ops_size); +} +EXPORT_SYMBOL_GPL (pci_iov_borrow_rust_pf_data); +#endif +/* * Per SR-IOV spec sec 3.3.10 and 3.3.11, First VF Offset and VF Stride may * change when NumVFs changes.
include/linux/pci.h +31 -3
@@ -339,6 +339,8 @@ struct pcie_link_state;struct pci_sriov ;struct pci_p2pdma ;struct rcec_ea ; +struct rust_ffi ; +struct rust_ffi_token ;/* struct pci_dev - describes a PCI device *
@@ -352,9 +354,10 @@ struct rcec_ea;* Such bridges are allocated additional MMIO and bus * number resources to allow for hierarchy expansion. * @is_pciehp: PCIe Hot-Plug Capable bridge. -* @vf_registration_data_rust: Rust registration data published by the PF -* before enabling VFs and retained until all VFs are -* removed. The PF driver must use managed_sriov. +* @vf_registration_data_rust: Rust registration data beginning with a +* struct rust_ffi, published by the PF before +* enabling VFs and retained until all VFs are removed. +* The PF driver must use managed_sriov. */ struct pci_dev {struct list_head bus_list;/* Node in per-bus list */
@@ -2614,6 +2617,22 @@ int pci_iov_virtfn_bus(struct pci_dev *dev, int id);int pci_iov_virtfn_devfn (struct pci_dev *dev,int id);int pci_iov_vf_id (struct pci_dev *dev);void *pci_iov_get_pf_drvdata (struct pci_dev *dev,struct pci_driver *pf_driver); +#ifdef CONFIG_RUST +const struct rust_ffi * +pci_iov_borrow_rust_pf_data (struct pci_dev *dev, +const struct rust_ffi_token *token, +u16 abi_major,u16 min_abi_minor, +size_t required_ops_size); +#else +static inline const struct rust_ffi * +pci_iov_borrow_rust_pf_data (struct pci_dev *dev, +const struct rust_ffi_token *token, +u16 abi_major,u16 min_abi_minor, +size_t required_ops_size) +{ +return ERR_PTR (-EOPNOTSUPP ); +} +#endif int pci_enable_sriov (struct pci_dev *dev,int nr_virtfn);void pci_disable_sriov (struct pci_dev *dev);
@@ -2656,6 +2675,15 @@ static inline void *pci_iov_get_pf_drvdata(struct pci_dev *dev,return ERR_PTR (-EINVAL ); } +static inline const struct rust_ffi * +pci_iov_borrow_rust_pf_data (struct pci_dev *dev, +const struct rust_ffi_token *token, +u16 abi_major,u16 min_abi_minor, +size_t required_ops_size) +{ +return ERR_PTR (-EOPNOTSUPP ); +} +static inline int pci_enable_sriov (struct pci_dev *dev,int nr_virtfn) {return -ENODEV ; }
rust/kernel/interop/ffi.rs +14 -1
@@ -99,10 +99,23 @@ pub unsafe trait Abi: 'static {/// [`struct rust_ffi`](srctree/include/linux/rust_ffi.h). It neither owns nor borrows the /// operations table or Rust context at the type level. The transport that publishes it must ensure /// that `ops` remains valid and that `context` remains alive at a stable address until all -/// consumers have stopped using the descriptor. +/// consumers have stopped using the descriptor. The default value exposes no operations. #[repr(transparent)] pub struct Descriptor (bindings::rust_ffi ); +impl Default for Descriptor { +fn default () ->Self { +Self (bindings::rust_ffi { + token: bindings::rust_ffi_token { high:0 , low:0 }, + abi_major:0 , + abi_minor:0 , + ops_size:0 , + ops: core::ptr::null (), + context: core::ptr::null (), + }) + } +} +impl Descriptor {/// Creates a descriptor for a pinned Rust context. ///
rust/kernel/pci/sriov.rs +83 -27
165 lines not on this page · raw message
PATCH 10/14 rust: add C-to-Rust FFI descriptors and trampolines
In reply to Zhi WangRust drivers may need to expose a restricted operations table to C consumers. Passing an opaque Rust pointer alone neither identifies the expected operations-table ABI nor provides type-checked C-compatible trampolines.
Add struct rust_ffi with a stable 128-bit ABI token, version fields, an operations-table size, and an opaque pinned context. Add rust_ffi_borrow() to validate those fields independently of the transport that publishes the descriptor. The token is an ABI type tag and does not provide device identity, authorization, or lifetime management.
Add matching Rust Token, Abi, and Descriptor abstractions and an ffi_vtable procedural macro. The macro checks the complete bindgen operations-table layout while generating private C ABI trampolines that recover a Pin<&T>. A sealed return conversion keeps raw return values unchanged and maps Result<()> and Result<c_int> to conventional C integer results. The publishing transport remains responsible for keeping the descriptor and context alive and pinned until every consumer has stopped calling it.
--- MAINTAINERS | 2 + include/linux/rust_ffi.h | 88 ++++++++++++ rust/bindings/bindings_helper.h | 1 + rust/kernel/interop.rs | 5 +- rust/kernel/interop/ffi.rs | 239 ++++++++++++++++++++++++++++++++ rust/macros/ffi_vtable.rs | 148 ++++++++++++++++++++ rust/macros/lib.rs | 90 ++++++++++++ 7 files changed, 571 insertions(+), 2 deletions(-) create mode 100644 include/linux/rust_ffi.h create mode 100644 rust/kernel/interop/ffi.rs create mode 100644 rust/macros/ffi_vtable.rs
MAINTAINERS +2 -0
@@ -24003,8 +24003,10 @@ M: Alexandre Courbot <[email protected]> L: [email protected] S: Maintained T: git https://github.com/Rust-for-Linux/linux.git interop-next +F: include/linux/rust_ffi.h F: rust/kernel/interop.rs F: rust/kernel/interop/ +F: rust/macros/ffi_vtable.rs RUST [NUM] M: Alexandre Courbot <[email protected]>
include/linux/rust_ffi.h new file +88 -0
88 lines not on this page · raw message
rust/bindings/bindings_helper.h +1 -0
@@ -85,6 +85,7 @@#include <linux/random.h> #include <linux/refcount.h> #include <linux/regulator/consumer.h> +#include <linux/rust_ffi.h> #include <linux/sched.h> #include <linux/security.h> #include <linux/serdev.h>
rust/kernel/interop.rs +3 -2
@@ -3,7 +3,8 @@//! Infrastructure for interfacing Rust code with C kernel subsystems. //! //! This module is intended for low-level, unsafe Rust infrastructure code -//! that interoperates between Rust and C. It is *not* for use directly in -//! Rust drivers. +//! that interoperates between Rust and C. Drivers should normally use the +//! generated adapters and safe subsystem abstractions built on top of it. +pub mod ffi;pub mod list;
rust/kernel/interop/ffi.rs new file +239 -0
239 lines not on this page · raw message
rust/macros/ffi_vtable.rs new file +148 -0
148 lines not on this page · raw message
rust/macros/lib.rs +90 -0
102 lines not on this page · raw message
PATCH 12/14 samples: rust: add C SR-IOV VF driver that calls into a Rust PF driver
In reply to Zhi WangC drivers need a type-checked way to invoke functionality implemented by Rust PF drivers without receiving the PF driver's complete private data. The Rust SR-IOV sample currently demonstrates only a Rust VF consumer.
Add an optional C VF module that borrows the sample's published PF data through a struct rust_ffi descriptor. Validate the ABI token, version, and operations-table size before calling an automatically generated Rust trampoline.
Route Rust and C VF requests through the same PF handler. The generated C callback forwards the requester's PCI ID and translates the Rust Result into the C errno convention. The C driver uses the borrow only while its VF remains bound, so all users must be drained before its remove callback returns.
--- MAINTAINERS | 2 +- rust/bindings/bindings_helper.h | 4 ++ samples/rust/Kconfig | 17 +++++++- samples/rust/Makefile | 1 + samples/rust/rust_driver_sriov.h | 27 ++++++++++++ samples/rust/rust_driver_sriov.rs | 43 ++++++++++++++++--- samples/rust/rust_driver_sriov_c_vf.c | 61 +++++++++++++++++++++++++++ 7 files changed, 146 insertions(+), 9 deletions(-) create mode 100644 samples/rust/rust_driver_sriov.h create mode 100644 samples/rust/rust_driver_sriov_c_vf.c
MAINTAINERS +1 -1
@@ -21141,7 +21141,7 @@ F: rust/helpers/pci.c F: rust/kernel/pci.rs F: rust/kernel/pci/ F: samples/rust/rust_driver_pci.rs -F: samples/rust/rust_driver_sriov.rs +F: samples/rust/rust_driver_sriov* PCIE BANDWIDTH CONTROLLER M: Ilpo Järvinen <[email protected]>
rust/bindings/bindings_helper.h +4 -0
@@ -112,6 +112,10 @@#include <drm/drm_panic.h> #endif +#if IS_ENABLED (CONFIG_SAMPLE_RUST_DRIVER_SRIOV ) +#include "../../samples/rust/rust_driver_sriov.h" +#endif +/* `bindgen` gets confused at certain things. */ const size_t RUST_CONST_HELPER_ARCH_SLAB_MINALIGN =ARCH_SLAB_MINALIGN ;const size_t RUST_CONST_HELPER_ARCH_KMALLOC_MINALIGN =ARCH_KMALLOC_MINALIGN ;
samples/rust/Kconfig +16 -1
@@ -132,13 +132,28 @@ config SAMPLE_RUST_DRIVER_SRIOV tristate "SR-IOV Driver" depends on PCI_IOV help - This option builds the Rust SR-IOV driver sample. + This option builds the Rust SR-IOV PF and VF driver sample. To compile this as a module, choose M here: the module will be called rust_driver_sriov. If unsure, say N. +config SAMPLE_RUST_DRIVER_SRIOV_C_VF + tristate "C VF consumer for the Rust SR-IOV driver" + depends on SAMPLE_RUST_DRIVER_SRIOV + help + This option builds a C VF driver that accesses PF data through FFI + published by the Rust SR-IOV PF sample. + + To compile this as a module, choose M here: the module will be called + rust_driver_sriov_c_vf. Load it before rust_driver_sriov and enable + VFs only after both drivers are registered. Ensure that no other VF + driver has claimed the device; driver_override may be used for a + deterministic test. + + If unsure, say N. + config SAMPLE_RUST_DRIVER_USB tristate "USB Driver" depends on USB = y
samples/rust/Makefile +1 -0
@@ -11,6 +11,7 @@ obj-$(CONFIG_SAMPLE_RUST_DRIVER_I2C) += rust_driver_i2c.o obj-$(CONFIG_SAMPLE_RUST_I2C_CLIENT) += rust_i2c_client.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_PCI) += rust_driver_pci.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_PLATFORM) += rust_driver_platform.o +obj-$(CONFIG_SAMPLE_RUST_DRIVER_SRIOV_C_VF) += rust_driver_sriov_c_vf.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_SRIOV) += rust_driver_sriov.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_USB) += rust_driver_usb.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_FAUX) += rust_driver_faux.o
samples/rust/rust_driver_sriov.h new file +27 -0
@@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _SAMPLES_RUST_DRIVER_SRIOV_H +#define _SAMPLES_RUST_DRIVER_SRIOV_H + +#include <linux/rust_ffi.h> + +#define RUST_DRIVER_SRIOV_FFI_TOKEN_HIGH 0x6c8686c04f7a4ba1ULL +#define RUST_DRIVER_SRIOV_FFI_TOKEN_LOW 0x77ab9a3c4d3cfb34ULL +#define RUST_DRIVER_SRIOV_FFI_ABI_MAJOR 1U +#define RUST_DRIVER_SRIOV_FFI_ABI_MINOR 0U + +/** +* struct rust_driver_sriov_ops - Operations published by the Rust PF sample +* @submit: Submit one request for a requester ID and return 0 or a negative +* errno +* +* The context passed to each operation must be the context from the borrowed +* struct rust_ffi. It remains valid until the VF driver is fully unbound, +* including the return of its remove() callback when present. +* +* @submit may sleep and must not be called from atomic context. +*/ +struct rust_driver_sriov_ops { +int (*submit)(const void *context,u16 requester_id); +}; + +#endif /* _SAMPLES_RUST_DRIVER_SRIOV_H */
samples/rust/rust_driver_sriov.rs +36 -7
89 lines not on this page · raw message
samples/rust/rust_driver_sriov_c_vf.c new file +61 -0
61 lines not on this page · raw message
PATCH 13/14 gpu: nova-core: publish typed SR-IOV PF data for VF drivers
In reply to Zhi WangNova-core is the PF-side GPU driver, but it does not implement the PCI callback for sriov_numvfs. Userspace therefore cannot enable or disable VFs on GPUs bound to the driver, and no typed PF data is available before a VF is created.
Embed a VfRegistration in the pinned NovaCore driver data and publish unit data, (), as a typed readiness marker on SR-IOV PFs. The registration remains inactive on conventional PCI functions and rejects VFs, so nova-core can keep using the ordinary pci::Driver abstraction with or without CONFIG_PCI_IOV.
Implement sriov_configure() using the verified PF view and pinned driver data. Managed SR-IOV removes every VF before the inline registration and the rest of NovaCore are dropped.
--- drivers/gpu/nova-core/driver.rs | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-)
drivers/gpu/nova-core/driver.rs +30 -3
@@ -35,6 +35,10 @@#[pin_data] pub (crate )struct NovaCore <'bound> { +#[cfg( CONFIG_PCI_IOV )] +#[allow(clippy::type_complexity)] +#[pin] + _vf_registration: pci::VfRegistration <'bound,CovariantForLt! (())>,#[pin] pub (crate ) gpu:Gpu <'bound>, bar: pci::Bar <'bound,BAR0_SIZE >,
@@ -107,10 +111,18 @@ fn probe<'bound>( pin_init::pin_init_scope(move || { dev_dbg!(pdev, "Probe Nova Core GPU driver.\n"); - pdev.enable_device_mem()?; - pdev.set_master(); - Ok(try_pin_init!(NovaCore { + #[cfg(CONFIG_PCI_IOV)] + // SAFETY: + // - probe has exclusive access before SR-IOV can be enabled; + // - the registration is pinned in driver data and is its first field; + // - no other registration is created for this device; and + // - the PCI adapter uses managed SR-IOV. + _vf_registration <- unsafe { pci::VfRegistration::new(pdev, Ok(())) }, + _: { + pdev.enable_device_mem()?; + pdev.set_master(); + }, bar: pdev.iomap_region_sized::<BAR0_SIZE>(0, c"nova-core/bar0")?, bar1: { let bar1_idx = bar1_resource_index(pdev)?;
@@ -149,4 +161,19 @@ fn probe<'bound>( })) }) } + +#[cfg( CONFIG_PCI_IOV )] +fn sriov_configure <'bound>( + dev: &'bound pci::sriov::Device <Core <'_>>, + _this:Pin <&Self ::Data <'bound>>, + nr_virtfn:i32 , + ) ->Result <i32 > { +if nr_virtfn ==0 { + dev.disable_sriov (); + }else { + dev.enable_sriov (nr_virtfn)?; + } + +Ok (nr_virtfn) + } }
PATCH 14/14 Documentation: rust: explain SR-IOV PF data sharing with VFs
In reply to Zhi WangRust and C VF drivers may need a narrow PF-owned interface without gaining access to all private data belonging to the PF driver. The typed PF registration and C FFI abstractions span PCI topology, type checking, driver lifetime, and synchronization, which need a single explanation.
The design embeds one pinned PF/VF contract inline in ordinary PCI driver data. Rust consumers borrow it as Pin<&T>; C consumers borrow a checked operations table and context. Both paths invoke the same Rust implementation, while managed SR-IOV orders VF teardown before PF data destruction.
Document the ownership and borrowing model, in-place initialization, publication and teardown rules, FFI versioning and trampolines, locking and asynchronous drain requirements, and limitations. Illustrate the storage, lifetime, and Rust and C call paths with ASCII diagrams.
--- Documentation/rust/index.rst | 1 + Documentation/rust/pci-sriov-pf-data.rst | 330 +++++++++++++++++++++++ MAINTAINERS | 1 + 3 files changed, 332 insertions(+) create mode 100644 Documentation/rust/pci-sriov-pf-data.rst
Documentation/rust/index.rst +1 -0
@@ -37,6 +37,7 @@ more details. coding-guidelines arch-support testing + pci-sriov-pf-data You can also find learning materials for Rust in its section in :doc:`../process/kernel-docs`.
Documentation/rust/pci-sriov-pf-data.rst new file +330 -0
330 lines not on this page · raw message
MAINTAINERS +1 -0
@@ -21137,6 +21137,7 @@ L: [email protected] S: Maintained C: irc://irc.oftc.net/linux-pci T: git git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci.git +F: Documentation/rust/pci-sriov-pf-data.rst F: rust/helpers/pci.c F: rust/kernel/pci.rs F: rust/kernel/pci/
Danilo Krummrich
maintainerQuoted 9 lines · Zhi Wang
Rust PCI drivers need to enable and disable SR-IOV and share selected PF-owned functionality with their VF drivers. The shared data must remain valid while a VF driver is bound, including when the VF driver is in C.
This series builds on Peter Colberg's Rust PCI SR-IOV v3 series [1], extending it with typed PF registration data and C FFI support. The registration design follows Danilo Krummrich's Rust vGPU/VFIO PoC [2]. As the discussion on Rust/VFIO support is still ongoing [3], this series supports both C and Rust sample VF drivers.
[...]
Quoted 4 lines
[1] https://lore.kernel.org/rust-for-linux/[email protected]/
[2] https://lore.kernel.org/nova-gpu/[email protected]/
[3] https://lore.kernel.org/nova-gpu/[email protected]/
Quoted 10 lines
drivers/pci/iov.c | 103 ++++++- drivers/pci/pci-driver.c | 3 +- drivers/pci/pci.h | 2 + include/linux/pci.h | 44 +++ include/linux/rust_ffi.h | 88 ++++++ rust/kernel/interop/ffi.rs | 252 ++++++++++++++++ rust/kernel/pci.rs | 126 ++++++++ rust/kernel/pci/sriov.rs | 355 +++++++++++++++++++++++ rust/macros/ffi_vtable.rs | 148 ++++++++++ rust/macros/lib.rs | 90 ++++++
Thanks for all the effort, Zhi! It is good to have a reference to see how this turns out.
Unfortunately, this raises the same concerns I already shared in the other thread in [2], plus additional ones:
(1) This FFI layer is already more code than would be needed to abstract the vfio-pci driver API surface in Rust. In addition, it is also much more complex and error prone.
(2) By creating a generic FFI layer to connect PF and VF drivers (which are technically the same driver project), we incentivise writing cross language drivers.
When the panthor DRM driver was considering to (re-)write parts in Rust I very much objected to this, as it would have created an arbitrary FFI boundary within DRM (which for obvious reasons would be a maintainance nightmare).
Now, this is not exactly the same, as it is not an arbitrary FFI boundary; it is well defined. But, it is still a workaround for a single driver project being split up in a Rust and in a C portion.
The FFI surface is best kept at the subsystem level abstracting the driver facing APIs, which provides a defined and comparatively stable API surface.
For those reasons I don't want to add this to the PCI (Rust and C) core code, i.e. I don't want to support a generic FFI layer between PF and VF drivers.
I really hope that we can find a way forward not having to end up doing this; but if we really have to, we should stick to a nova project internal FFI layer for this.
Thanks,
Danilo