Skip to contentSkip to conversation

vfio/pci: Latch module params, fix bitfield use and VGA unwind

PATCH In review Alex Williamson 3 participants 11 messages Jun 15 – Jun 17

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

v3 cover vfio/pci: Latch module params, fix bitfield use and VGA unwind

The vfio-pci module pushes module parameters into vfio-pci-core at initialization that change global policies for all devices, whether bound to vfio-pci itself or other variant drivers. Not only is this generally bad practice, but in the case of runtime PM, policy changes can affect initialized devices, even while they're in use, resulting in unbalanced operations.

This series first addresses the runtime PM case as a separate fix for stable, latching the runtime PM support into the device.

Then, in preparing for moving disable_vga to per device, it's discovered that we lack proper unwind support for a registration failure, leaving the VGA arbiter client registered. This is resolved before it turns into an issue, also marked for stable.

Next, we're again reminded of the risky bitfield use in both the vfio-pci-core and mlx5 variant driver, therefore before expanding use for more per-device flags, perform the binary split to categorize flags as either setup/release or runtime to avoid RMW hazards of the bitfields. Also for stable.

Finally, close the series by pulling vfio-pci module parameter policy out of the core, latching each into the device structure.

This latter change also has the effect of restoring the mutability of two of the module parameters, where that functionality was lost with the vfio-pci-core split. With this series, the current parameter values are latched into the device at probe time.

Please review. Thanks,
Alex

v3:
- patch 1: Remove the conditional runtime PM get/put in vfio_pci_dev_set_pm_runtime_get(), which failed to account for devices that might already be in low power due to the low power device feature ioctl. Now that disable_idle_d3 is no longer global, use the same unconditional runtime PM get/put around try_reset from the disable path. This is harmless regardless of the disable_idle_d3 flag and consistent with other unconditional use cases, such as read/write accesses.

- patch 6: New. Since we need a respin, implement the suggested debugfs exposure of the per-device flag state.

v2:
- Sashiko fixes, including pulling in the bitfield split series.

Alex Williamson (6):
vfio/pci: Latch disable_idle_d3 per device
vfio/pci: Release the VGA arbiter client on register_device() failure vfio/pci: Fix racy bitfields and tighten struct layout vfio/mlx5: Fix racy bitfields and tighten struct layout vfio/pci: Latch all module parameters per device
vfio/pci: Expose latched module parameter policy in debugfs

 Documentation/ABI/testing/debugfs-vfio |  26 +++++++
 drivers/vfio/pci/mlx5/cmd.h            |  15 ++--
 drivers/vfio/pci/vfio_pci.c            |  30 ++++++--
 drivers/vfio/pci/vfio_pci_core.c       | 101 ++++++++++++++++++-------
 include/linux/vfio_pci_core.h          |  18 +++--
 5 files changed, 142 insertions(+), 48 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH v3 1/6 vfio/pci: Latch disable_idle_d3 per device

When disable_idle_d3 was introduced in vfio-pci, it directly manipulated the device power state with pci_set_power_state(). There were no refcounts to maintain or balanced operations, we could unconditionally bring the device to D0 and conditionally move it to D3hot. Therefore the module parameter was made writable.

Later, in commit c61302aa48f7 ("vfio/pci: Move module parameters to vfio_pci.c"), as part of the vfio-pci-core split, the writable aspect of the module parameter was nullified. The parameter value could still be changed through sysfs, but the vfio-pci driver latched the values into vfio-pci-core globals at module init. Loading the vfio-pci module, or unloading and reloading, with non-default or different values could change the globals relative to existing devices bound to vfio-pci variant drivers.

Runtime PM was introduced in commit 7ab5e10eda02 ("vfio/pci: Move the unused device into low power state with runtime PM"), which marks the point where power states became refcounted. PM get and put operations need to be balanced, but the same module operations noted above can change the global variables relative to those devices already bound to vfio-pci variant drivers. This introduces a window where PM operations can now become unbalanced.

To resolve this with a narrow footprint for stable backports, the disable_idle_d3 flag is latched into the vfio_pci_core_device at the time of initialization, such that the device always operates with a consistent value.

NB. vfio_pci_dev_set_try_reset() now unconditionally raises the runtime PM usage count around bus reset to account for disable_idle_d3 becoming a per-device rather than global flag. When this flag is set, the additional get/put pair is harmless and allows continued use of the shared vfio_pci_dev_set_pm_runtime_get() helper.

Fixes 7ab5e10eda02 ("vfio/pci: Move the unused device into low power state with runtime PM")
Assisted-by Claude:claude-opus-4-8
---
 drivers/vfio/pci/vfio_pci_core.c | 19 ++++++++++---------
 include/linux/vfio_pci_core.h    |  1 +
 2 files changed, 11 insertions(+), 9 deletions(-)
drivers/vfio/pci/vfio_pci_core.c +10 -9
index a28f1e99362c..f8d1755de2ce 100644
@@ -538,7 +538,7 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
 	u16 cmd;
 	u8 msix_pos;
 
-	if (!disable_idle_d3) {
+	if (!vdev->disable_idle_d3) {
 		ret = pm_runtime_resume_and_get(&pdev->dev);
 		if (ret < 0)
 			return ret;
@@ -617,7 +617,7 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
 out_disable_device:
 	pci_disable_device(pdev);
 out_power:
-	if (!disable_idle_d3)
+	if (!vdev->disable_idle_d3)
 		pm_runtime_put(&pdev->dev);
 	return ret;
 }
@@ -753,7 +753,7 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)
 	vfio_pci_dev_set_try_reset(vdev->vdev.dev_set);
 
 	/* Put the pm-runtime usage counter acquired during enable */
-	if (!disable_idle_d3)
+	if (!vdev->disable_idle_d3)
 		pm_runtime_put(&pdev->dev);
 }
 EXPORT_SYMBOL_GPL(vfio_pci_core_disable);
@@ -2144,6 +2144,8 @@ int vfio_pci_core_init_dev(struct vfio_device *core_vdev)
 	init_rwsem(&vdev->memory_lock);
 	xa_init(&vdev->ctx);
 
+	vdev->disable_idle_d3 = disable_idle_d3;
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(vfio_pci_core_init_dev);
@@ -2239,7 +2241,7 @@ int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev)
 
 	dev->driver->pm = &vfio_pci_core_pm_ops;
 	pm_runtime_allow(dev);
-	if (!disable_idle_d3)
+	if (!vdev->disable_idle_d3)
 		pm_runtime_put(dev);
 
 	ret = vfio_register_group_dev(&vdev->vdev);
@@ -2248,7 +2250,7 @@ int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev)
 	return 0;
 
 out_power:
-	if (!disable_idle_d3)
+	if (!vdev->disable_idle_d3)
 		pm_runtime_get_noresume(dev);
 
 	pm_runtime_forbid(dev);
@@ -2267,7 +2269,7 @@ void vfio_pci_core_unregister_device(struct vfio_pci_core_device *vdev)
 	vfio_pci_vf_uninit(vdev);
 	vfio_pci_vga_uninit(vdev);
 
-	if (!disable_idle_d3)
+	if (!vdev->disable_idle_d3)
 		pm_runtime_get_noresume(&vdev->pdev->dev);
 
 	pm_runtime_forbid(&vdev->pdev->dev);
@@ -2599,7 +2601,7 @@ static void vfio_pci_dev_set_try_reset(struct vfio_device_set *dev_set)
 	 * state. Increment the usage count for all the devices in the dev_set
 	 * before reset and decrement the same after reset.
 	 */
-	if (!disable_idle_d3 && vfio_pci_dev_set_pm_runtime_get(dev_set))
+	if (vfio_pci_dev_set_pm_runtime_get(dev_set))
 		return;
 
 	if (!pci_reset_bus(pdev))
@@ -2609,8 +2611,7 @@ static void vfio_pci_dev_set_try_reset(struct vfio_device_set *dev_set)
 		if (reset_done)
 			cur->needs_reset = false;
 
-		if (!disable_idle_d3)
-			pm_runtime_put(&cur->pdev->dev);
+		pm_runtime_put(&cur->pdev->dev);
 	}
 }
 
include/linux/vfio_pci_core.h +1 -0
index 5fc6ce4dd786..27aab3fdbb91 100644
@@ -127,6 +127,7 @@ struct vfio_pci_core_device {
 	bool			needs_pm_restore:1;
 	bool			pm_intx_masked:1;
 	bool			pm_runtime_engaged:1;
+	bool			disable_idle_d3:1;
 	bool			sriov_active;
 	struct pci_saved_state	*pci_saved_state;
 	struct pci_saved_state	*pm_save;
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH v3 1/6] vfio/pci: Latch disable_idle_d3 per device

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] The driver's runtime suspend callback does not call synchronize_irq() before powering down the hardware, despite using a shared interrupt. --

commit a79806f5080762f4f8088e2fb816e867d4b78d1c
Author: Alex Williamson <[email protected]>

vfio/pci: Latch disable_idle_d3 per device

This commit resolves unbalanced PM operations by latching the disable_idle_d3 flag into vfio_pci_core_device at initialization. This ensures the device operates with a consistent value rather than relying on a global variable that might change at runtime. It updates usages across the driver to use the per-device flag.

Quoted 4 lines
drivers/vfio/pci/vfio_pci_core.c +0 -0
index a28f1e99362c1..f8d1755de2ce0 100644

[ ... ]

drivers/vfio/pci/vfio_pci_core.c int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev) · line 2245

Quoted 7 lines
drivers/vfio/pci/vfio_pci_core.c +1 -1
@@ -2239,7 +2241,7 @@ int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev)
 
 	dev->driver->pm = &vfio_pci_core_pm_ops;
 	pm_runtime_allow(dev);
-	if (!disable_idle_d3)
+	if (!vdev->disable_idle_d3)
 		pm_runtime_put(dev);

[Severity: High]
This is a pre-existing issue, but does the runtime suspend callback for these PM operations need to call synchronize_irq() before returning?

The driver requests the INTx interrupt with IRQF_SHARED (via vfio_intx_enable()). According to the Power Management subsystem guidelines, drivers using IRQF_SHARED must call synchronize_irq() in their runtime suspend callback before allowing the PCI core to power down the hardware.

Without synchronize_irq(), an interrupt handler executing concurrently on another CPU could continue executing and attempt to access the device's PCI configuration registers (e.g., inside pci_check_and_mask_intx()) after the device has transitioned to the D3hot (powered off) state.

drivers/vfio/pci/vfio_pci_core.c:vfio_pci_core_runtime_suspend() {
    ...
    vdev->pm_intx_masked = ((vdev->irq_type == VFIO_PCI_INTX_IRQ_INDEX) &&
                            vfio_pci_intx_mask(vdev));

    return 0;
}

Could accessing registers of a powered-down device cause SError panics or Target Aborts on some architectures, crashing the system?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH v3 2/6 vfio/pci: Release the VGA arbiter client on register_device() failure

In reply to Alex Williamson

The re-order in the Fixes commit below displaced vfio_pci_vga_init() as the last failure point of what is now vfio_pci_core_register_device() without introducing an unwind for the VGA arbiter registration.

In current kernels this is mostly benign because vfio_pci_set_decode() only uses pci_dev state, but the original failure path could leave a callback with a freed vdev cookie. The stale registration also becomes unsafe again once the callback follows drvdata to the vfio device.

Add the required VGA unwind callout.

Fixes 4aeec3984ddc ("vfio/pci: Re-order vfio_pci_probe()")
Assisted-by Claude:claude-opus-4-8
---
 drivers/vfio/pci/vfio_pci_core.c | 1 +
 1 file changed, 1 insertion(+)
drivers/vfio/pci/vfio_pci_core.c +1 -0
index f8d1755de2ce..dab82c078580 100644
@@ -2254,6 +2254,7 @@ int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev)
 		pm_runtime_get_noresume(dev);
 
 	pm_runtime_forbid(dev);
+	vfio_pci_vga_uninit(vdev);
 out_vf:
 	vfio_pci_vf_uninit(vdev);
 	return ret;
Signature · 1 line
2.53.0
#4 lore raw

PATCH v3 3/6 vfio/pci: Fix racy bitfields and tighten struct layout

In reply to Alex Williamson

Bitfield operations are not atomic, they use a read-modify-write pattern, therefore we should be careful not to pack bitfields that can be concurrently updated into the same storage unit.

This split takes a binary approach: flags that are only modified pre/post open/close remain bitfields, flags modified from user action, including actions that reach across to another device (ex. reset) use dedicated storage units.

Note that the virq_disabled and bardirty flags are relocated to fill an existing hole in the structure.

Bitfield justifications:

has_dyn_msix: written only in vfio_pci_core_enable()
pci_2_3: written only in vfio_pci_core_enable()
reset_works: written only in vfio_pci_core_enable()
extended_caps: written only in vfio_cap_len() under vfio_config_init()
has_vga: written only in vfio_pci_core_enable()
nointx: written only in vfio_pci_core_enable()
needs_pm_restore: written only in vfio_pci_probe_power_state()
disable_idle_d3: written only at .init in vfio_pci_core_init_dev()

Dedicated storage units:

virq_disabled: written by guest INTx command writes in
vfio_basic_config_write() while the device is open
bardirty: written by guest BAR writes in vfio_basic_config_write()
while the device is open
pm_intx_masked: written in the runtime-PM suspend path.
pm_runtime_engaged: written by low-power feature entry/exit paths
needs_reset: set in vfio_pci_core_disable() and cleared for devices in
the set by vfio_pci_dev_set_try_reset()
sriov_active: written by vfio_pci_core_sriov_configure() via sysfs
sriov_numvfs while bound.

Fixes 9cd0f6d5cbb6 ("vfio/pci: Use bitfield for struct vfio_pci_core_device flags")
Assisted-by Claude:claude-opus-4-8
---
 include/linux/vfio_pci_core.h | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
include/linux/vfio_pci_core.h +8 -5
index 27aab3fdbb91..985b8af5a04b 100644
@@ -101,6 +101,9 @@ struct vfio_pci_core_device {
 	const struct vfio_pci_device_ops *pci_ops;
 	void __iomem		*barmap[PCI_STD_NUM_BARS];
 	bool			bar_mmap_supported[PCI_STD_NUM_BARS];
+	/* Flags modified at runtime - dedicated storage unit */
+	bool			virq_disabled;
+	bool			bardirty;
 	u8			*pci_config_map;
 	u8			*vconfig;
 	struct perm_bits	*msi_perm;
@@ -115,19 +118,19 @@ struct vfio_pci_core_device {
 	u16			msix_size;
 	u32			msix_offset;
 	u32			rbar[7];
+	/* Flags only modified on setup/release - bitfield ok */
 	bool			has_dyn_msix:1;
 	bool			pci_2_3:1;
-	bool			virq_disabled:1;
 	bool			reset_works:1;
 	bool			extended_caps:1;
-	bool			bardirty:1;
 	bool			has_vga:1;
-	bool			needs_reset:1;
 	bool			nointx:1;
 	bool			needs_pm_restore:1;
-	bool			pm_intx_masked:1;
-	bool			pm_runtime_engaged:1;
 	bool			disable_idle_d3:1;
+	/* Flags modified at runtime - dedicated storage unit */
+	bool			needs_reset;
+	bool			pm_intx_masked;
+	bool			pm_runtime_engaged;
 	bool			sriov_active;
 	struct pci_saved_state	*pci_saved_state;
 	struct pci_saved_state	*pm_save;
Signature · 1 line
2.53.0
#5 lore raw

PATCH v3 4/6 vfio/mlx5: Fix racy bitfields and tighten struct layout

In reply to Alex Williamson

Bitfield operations are not atomic, they use a read-modify-write pattern, therefore we should be careful not to pack bitfields that can be concurrently updated into the same storage unit.

This split takes a binary approach: flags that are only modified pre/post open/close remain bitfields, flags modified from user action, including actions that reach across to another device (ex. reset) use dedicated storage units.

Note mlx5_vhca_page_tracker.status is relocated to fill the alignment hole this split exposes.

Bitfield justifications:

migrate_cap: written only in mlx5vf_cmd_set_migratable() at probe
chunk_mode: written only in mlx5vf_cmd_set_migratable() at probe
mig_state_cap: written only in mlx5vf_cmd_set_migratable() at probe

Dedicated storage units:

mdev_detach: written in the VF attach/detach event notifier
mlx5fv_vf_event() at runtime
log_active: written in mlx5vf_start_page_tracker()/
mlx5vf_stop_page_tracker() during runtime dirty tracking
deferred_reset: written in mlx5vf_state_mutex_unlock()/
mlx5vf_pci_aer_reset_done() during runtime reset handling
is_err: set by tracker error handling and dirty-log polling at runtime
object_changed: set by tracker event handling and cleared by dirty-log
polling at runtime

Fixes 61a2f1460fd0 ("vfio/mlx5: Manage the VF attach/detach callback from the PF")
Fixes 79c3cf279926 ("vfio/mlx5: Init QP based resources for dirty tracking")
Fixes f886473071d6 ("vfio/mlx5: Add support for tracker object change event")
Cc Yishai Hadas <[email protected]>
Assisted-by Claude:claude-opus-4-8
---
 drivers/vfio/pci/mlx5/cmd.h | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/vfio/pci/mlx5/cmd.h +9 -6
index deed0f132f39..c86d8b243a52 100644
@@ -158,26 +158,29 @@ struct mlx5_vhca_qp {
 struct mlx5_vhca_page_tracker {
 	u32 id;
 	u32 pdn;
-	u8 is_err:1;
-	u8 object_changed:1;
+	/* Flags modified at runtime - dedicated storage unit */
+	u8 is_err;
+	u8 object_changed;
+	int status;
 	struct mlx5_uars_page *uar;
 	struct mlx5_vhca_cq cq;
 	struct mlx5_vhca_qp *host_qp;
 	struct mlx5_vhca_qp *fw_qp;
 	struct mlx5_nb nb;
-	int status;
 };
 
 struct mlx5vf_pci_core_device {
 	struct vfio_pci_core_device core_device;
 	int vf_id;
 	u16 vhca_id;
+	/* Flags only modified on setup/release - bitfield ok */
 	u8 migrate_cap:1;
-	u8 deferred_reset:1;
-	u8 mdev_detach:1;
-	u8 log_active:1;
 	u8 chunk_mode:1;
 	u8 mig_state_cap:1;
+	/* Flags modified at runtime - dedicated storage unit */
+	u8 mdev_detach;
+	u8 log_active;
+	u8 deferred_reset;
 	struct completion tracker_comp;
 	/* protect migration state */
 	struct mutex state_mutex;
Signature · 1 line
2.53.0
#6 lore raw

PATCH v3 5/6 vfio/pci: Latch all module parameters per device

In reply to Alex Williamson

The vfio-pci module parameters of disable_idle_d3, nointxmask, and disable_vga latch vfio-pci policy into vfio-pci-core globals each time the vfio-pci module is initialized. The disable_idle_d3 parameter has already migrated to a per-device flag in order to provide consistency for refcounted PM operations for the lifetime of the device registration.

Pull the remaining vfio-pci module-parameter policy out of vfio-pci-core into per-device flags set at device initialization.

This also restores the mutable aspect of the disable_idle_d3 and nointxmask module parameters for vfio-pci, with the caveat that the parameters are latched into the device at probe.

A notable change for variant drivers is that their devices are no longer affected by vfio-pci module parameters and those drivers may need to adopt similar module parameters if any devices have a hidden dependency on vfio-pci setting non-default policy.

Assisted-by Claude:claude-opus-4-8
---
 drivers/vfio/pci/vfio_pci.c      | 30 ++++++++++++++++++++++--------
 drivers/vfio/pci/vfio_pci_core.c | 26 ++++++--------------------
 include/linux/vfio_pci_core.h    |  4 ++--
 3 files changed, 30 insertions(+), 30 deletions(-)
drivers/vfio/pci/vfio_pci.c +22 -8
index 0c771064c0b8..830369ff878d 100644
@@ -125,9 +125,30 @@ static int vfio_pci_open_device(struct vfio_device *core_vdev)
 	return 0;
 }
 
+static int vfio_pci_init_dev(struct vfio_device *core_vdev)
+{
+	struct vfio_pci_core_device *vdev =
+		container_of(core_vdev, struct vfio_pci_core_device, vdev);
+
+	/*
+	 * These behaviors originated in vfio-pci and moved into
+	 * vfio-pci-core when the driver was split; vfio-pci remains the
+	 * only driver that toggles them.  Latch our module parameters per
+	 * device at init time so that later parameter changes do not
+	 * affect already-initialized devices.
+	 */
+	vdev->nointxmask = nointxmask;
+	vdev->disable_idle_d3 = disable_idle_d3;
+#ifdef CONFIG_VFIO_PCI_VGA
+	vdev->disable_vga = disable_vga;
+#endif
+
+	return vfio_pci_core_init_dev(core_vdev);
+}
+
 static const struct vfio_device_ops vfio_pci_ops = {
 	.name		= "vfio-pci",
-	.init		= vfio_pci_core_init_dev,
+	.init		= vfio_pci_init_dev,
 	.release	= vfio_pci_core_release_dev,
 	.open_device	= vfio_pci_open_device,
 	.close_device	= vfio_pci_core_close_device,
@@ -256,13 +277,6 @@ static void __init vfio_pci_fill_ids(void)
 static int __init vfio_pci_init(void)
 {
 	int ret;
-	bool is_disable_vga = true;
-
-#ifdef CONFIG_VFIO_PCI_VGA
-	is_disable_vga = disable_vga;
-#endif
-
-	vfio_pci_core_set_params(nointxmask, is_disable_vga, disable_idle_d3);
 
 	/* Register and scan for devices */
 	ret = pci_register_driver(&vfio_pci_driver);
drivers/vfio/pci/vfio_pci_core.c +6 -20
index dab82c078580..db36d903dcd4 100644
@@ -38,10 +38,6 @@
 #define DRIVER_AUTHOR   "Alex Williamson <[email protected]>"
 #define DRIVER_DESC "core driver for VFIO based PCI devices"
 
-static bool nointxmask;
-static bool disable_vga;
-static bool disable_idle_d3;
-
 static void vfio_pci_eventfd_rcu_free(struct rcu_head *rcu)
 {
 	struct vfio_pci_eventfd *eventfd =
@@ -92,10 +88,10 @@ struct vfio_pci_vf_token {
 	int			users;
 };
 
-static inline bool vfio_vga_disabled(void)
+static inline bool vfio_vga_disabled(struct vfio_pci_core_device *vdev)
 {
 #ifdef CONFIG_VFIO_PCI_VGA
-	return disable_vga;
+	return vdev->disable_vga;
 #else
 	return true;
 #endif
@@ -111,11 +107,12 @@ static inline bool vfio_vga_disabled(void)
  */
 static unsigned int vfio_pci_set_decode(struct pci_dev *pdev, bool single_vga)
 {
+	struct vfio_pci_core_device *vdev = dev_get_drvdata(&pdev->dev);
 	struct pci_dev *tmp = NULL;
 	unsigned char max_busnr;
 	unsigned int decodes;
 
-	if (single_vga || !vfio_vga_disabled() || pci_is_root_bus(pdev->bus))
+	if (single_vga || !vfio_vga_disabled(vdev) || pci_is_root_bus(pdev->bus))
 		return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM |
 		       VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
 
@@ -562,7 +559,7 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
 	if (!vdev->pci_saved_state)
 		pci_dbg(pdev, "%s: Couldn't store saved state\n", __func__);
 
-	if (likely(!nointxmask)) {
+	if (likely(!vdev->nointxmask)) {
 		if (vfio_pci_nointx(pdev)) {
 			pci_info(pdev, "Masking broken INTx support\n");
 			vdev->nointx = true;
@@ -602,7 +599,7 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
 		vdev->has_dyn_msix = false;
 	}
 
-	if (!vfio_vga_disabled() && vfio_pci_is_vga(pdev))
+	if (!vfio_vga_disabled(vdev) && vfio_pci_is_vga(pdev))
 		vdev->has_vga = true;
 
 	vfio_pci_core_map_bars(vdev);
@@ -2144,8 +2141,6 @@ int vfio_pci_core_init_dev(struct vfio_device *core_vdev)
 	init_rwsem(&vdev->memory_lock);
 	xa_init(&vdev->ctx);
 
-	vdev->disable_idle_d3 = disable_idle_d3;
-
 	return 0;
 }
 EXPORT_SYMBOL_GPL(vfio_pci_core_init_dev);
@@ -2616,15 +2611,6 @@ static void vfio_pci_dev_set_try_reset(struct vfio_device_set *dev_set)
 	}
 }
 
-void vfio_pci_core_set_params(bool is_nointxmask, bool is_disable_vga,
-			      bool is_disable_idle_d3)
-{
-	nointxmask = is_nointxmask;
-	disable_vga = is_disable_vga;
-	disable_idle_d3 = is_disable_idle_d3;
-}
-EXPORT_SYMBOL_GPL(vfio_pci_core_set_params);
-
 static void vfio_pci_core_cleanup(void)
 {
 	vfio_pci_uninit_perm_bits();
include/linux/vfio_pci_core.h +2 -2
index 985b8af5a04b..9a1674c152aa 100644
@@ -127,6 +127,8 @@ struct vfio_pci_core_device {
 	bool			nointx:1;
 	bool			needs_pm_restore:1;
 	bool			disable_idle_d3:1;
+	bool			nointxmask:1;
+	bool			disable_vga:1;
 	/* Flags modified at runtime - dedicated storage unit */
 	bool			needs_reset;
 	bool			pm_intx_masked;
@@ -161,8 +163,6 @@ int vfio_pci_core_register_dev_region(struct vfio_pci_core_device *vdev,
 				      unsigned int type, unsigned int subtype,
 				      const struct vfio_pci_regops *ops,
 				      size_t size, u32 flags, void *data);
-void vfio_pci_core_set_params(bool nointxmask, bool is_disable_vga,
-			      bool is_disable_idle_d3);
 void vfio_pci_core_close_device(struct vfio_device *core_vdev);
 int vfio_pci_core_init_dev(struct vfio_device *core_vdev);
 void vfio_pci_core_release_dev(struct vfio_device *core_vdev);
Signature · 1 line
2.53.0
#7 lore raw

PATCH v3 6/6 vfio/pci: Expose latched module parameter policy in debugfs

In reply to Alex Williamson

The nointxmask and disable_idle_d3 module parameters remain writable, but vfio-pci now latches their values into each device at init. Once a device is registered, changing the module parameter only affects future devices, leaving no direct way to confirm the effective policy for an existing device.

Add a pci debugfs directory under the VFIO device debugfs root and report the per-device nointxmask and disable_idle_d3 values. These are read-only debugfs views and use the same Y/N bool output convention as the module parameters.

Read-only vfio-pci parameters, such as disable_vga, are not exposed here because they cannot drift from the latched device value, therefore the existing module parameter exposure via sysfs is sufficient.

Note that while only vfio-pci currently provides these options, the implementation is in vfio-pci-core and therefore properly reflects the device policy in the core, regardless of driver.

Assisted-by OpenAI Codex:gpt-5
Cc Guixin Liu <[email protected]>
---
 Documentation/ABI/testing/debugfs-vfio | 26 ++++++++++++
 drivers/vfio/pci/vfio_pci_core.c       | 59 ++++++++++++++++++++++++++
 2 files changed, 85 insertions(+)
Documentation/ABI/testing/debugfs-vfio +26 -0
index 70ec2d454686..ed2f29c3a9b4 100644
@@ -29,3 +29,29 @@ Date:		Oct 2025
 KernelVersion:	6.18
 Contact:	Cédric Le Goater <[email protected]>
 Description:	Read the migration features of the vfio device.
+
+What:		/sys/kernel/debug/vfio/<device>/pci
+Date:		June 2026
+KernelVersion:	7.2
+Contact:	Alex Williamson <[email protected]>
+Description:	This debugfs file directory is used for debugging
+		VFIO PCI devices.
+
+What:		/sys/kernel/debug/vfio/<device>/pci/nointxmask
+Date:		June 2026
+KernelVersion:	7.2
+Contact:	Alex Williamson <[email protected]>
+Description:	Read the nointxmask policy latched for this device.  This
+		policy governs whether the device may use PCI 2.3 style
+		INTx masking when supported, reporting a value of "N", or
+		requires APIC level INTx masking, reporting a value of "Y".
+
+What:		/sys/kernel/debug/vfio/<device>/pci/disable_idle_d3
+Date:		June 2026
+KernelVersion:	7.2
+Contact:	Alex Williamson <[email protected]>
+Description:	Read the disable_idle_d3 policy latched for this device.  This
+		policy governs whether the device PM runtime usage count is
+		kept elevated while the device is bound to the driver and
+		unused, reporting a value of "Y", or decremented to allow the
+		device to enter a low power state, reporting a value of "N".
drivers/vfio/pci/vfio_pci_core.c +59 -0

83 lines not on this page · raw message

Signature · 1 line
2.53.0
#8 lore raw

Re: [PATCH v3 6/6] vfio/pci: Expose latched module parameter policy in debugfs

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Use-After-Free in debugfs via devm-managed seqfile private data during device unbinding. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Use-After-Free in debugfs via devm-managed seqfile private data during device unbinding. --

commit 253a847be13fd59f8c265fb641107fa380c66e0c
Author: Alex Williamson <[email protected]>

vfio/pci: Expose latched module parameter policy in debugfs

This commit exposes the per-device latched values of the nointxmask and disable_idle_d3 module parameters via debugfs.

Quoted 4 lines
drivers/vfio/pci/vfio_pci_core.c +0 -0
index db36d903dcd40..3f11a9624b9c0 100644

[ ... ]

Quoted 1 line
drivers/vfio/pci/vfio_pci_core.c +0 -0
@@ -97,6 +99,60 @@ static inline bool vfio_vga_disabled(struct vfio_pci_core_device *vdev)

[ ... ]

drivers/vfio/pci/vfio_pci_core.c static inline bool vfio_vga_disabled(struct vfio_pci_core_device *vdev) · line 146

Quoted 11 lines
drivers/vfio/pci/vfio_pci_core.c +11 -0
+static void vfio_pci_core_debugfs_init(struct vfio_pci_core_device *vdev)
+{
+	struct device *dev = &vdev->vdev.device;
+	struct dentry *pci_dir;
+
+	if (IS_ERR_OR_NULL(vdev->vdev.debug_root))
+		return;
+
+	pci_dir = debugfs_create_dir("pci", vdev->vdev.debug_root);
+	debugfs_create_devm_seqfile(dev, "nointxmask", pci_dir,
+				    vfio_pci_core_debugfs_nointxmask);

[Severity: High]
Does this introduce a use-after-free during device unbinding?

When a device is unregistered, vfio_unregister_group_dev() invokes vfio_device_del() before tearing down the debugfs entries.

Because vfio_device_del() eventually calls device_del(), it will trigger devres_release_all() which frees the debugfs_devm_entry allocated by debugfs_create_devm_seqfile() here.

Following this, vfio_unregister_group_dev() can block waiting for userspace to close any open file descriptors for the device:

drivers/vfio/vfio_main.c:vfio_unregister_group_dev() {
    ...
	vfio_device_del(device);

	vfio_device_put_registration(device);
	rc = try_wait_for_completion(&device->comp);
    ...
}

Since the debugfs directory is not removed until vfio_device_debugfs_exit() is called after this wait completes, the debugfs files remain visible.

If these files are read during this wait window, will it dereference the freed devm-managed private data pointer?

Quoted 3 lines
drivers/vfio/pci/vfio_pci_core.c +3 -0
+	debugfs_create_devm_seqfile(dev, "disable_idle_d3", pci_dir,
+				    vfio_pci_core_debugfs_disable_idle_d3);
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#9 lore raw

[PATCH v3 7/6] vfio: Remove device debugfs before releasing devres

In reply to Alex Williamson

VFIO device debugfs files created with debugfs_create_devm_seqfile() store a devres allocated debugfs_devm_entry as inode private data. vfio_unregister_group_dev() currently calls vfio_device_del() before vfio_device_debugfs_exit(), but device_del() releases devres. This can leave debugfs entries visible with stale inode private data while unregister waits for userspace references to drain.

Remove the per-device debugfs tree before vfio_device_del(). The debugfs view is diagnostic only, so losing it at the start of unregister is preferable to preserving entries whose backing storage may already have been released.

Complete the teardown by clearing the per-device debugfs root after removal. This matches the global debugfs root cleanup and prevents future users from mistaking a removed dentry for a live debugfs tree during the remainder of unregister.

Fixes 2202844e4468 ("vfio/migration: Add debugfs to live migration driver")
Cc Longfang Liu <[email protected]>
Assisted-by OpenAI Codex:gpt-5

---

Avoiding a full respin, this is inteded to precede patch 6/ on commit.

 drivers/vfio/debugfs.c   | 1 +
 drivers/vfio/vfio_main.c | 8 +++++++-
 2 files changed, 8 insertions(+), 1 deletion(-)
drivers/vfio/debugfs.c +1 -0
index 8b0ca7a09064..8a2f1b0cce3f 100644
@@ -97,6 +97,7 @@ void vfio_device_debugfs_init(struct vfio_device *vdev)
 void vfio_device_debugfs_exit(struct vfio_device *vdev)
 {
 	debugfs_remove_recursive(vdev->debug_root);
+	vdev->debug_root = NULL;
 }
 
 void vfio_debugfs_create_root(void)
drivers/vfio/vfio_main.c +7 -1
index 5e0422014523..ed538aebb0b8 100644
@@ -406,6 +406,13 @@ void vfio_unregister_group_dev(struct vfio_device *device)
 	 */
 	vfio_device_group_unregister(device);
 
+	/*
+	 * Remove debugfs before device_del(), which releases devres.  Some
+	 * debugfs entries are created with debugfs_create_devm_seqfile() and
+	 * therefore rely on devres-managed inode private data.
+	 */
+	vfio_device_debugfs_exit(device);
+
 	/*
 	 * Balances vfio_device_add() in register path, also prevents
 	 * new device opened by userspace in the cdev path.
@@ -435,7 +442,6 @@ void vfio_unregister_group_dev(struct vfio_device *device)
 		}
 	}
 
-	vfio_device_debugfs_exit(device);
 	/* Balances vfio_device_set_group in register path */
 	vfio_device_remove_group(device);
 }
Signature · 1 line
2.53.0
reviewer Reviewed-by on the v3 series
#10 lore raw
In reply to Alex Williamson
Quoted 27 lines

From: Alex Williamson <[email protected]>
Sent: Tuesday, June 16, 2026 3:12 AM

The vfio-pci module pushes module parameters into vfio-pci-core at initialization that change global policies for all devices, whether bound to vfio-pci itself or other variant drivers. Not only is this generally bad practice, but in the case of runtime PM, policy changes can affect initialized devices, even while they're in use, resulting in unbalanced operations.

This series first addresses the runtime PM case as a separate fix for stable, latching the runtime PM support into the device.

Then, in preparing for moving disable_vga to per device, it's discovered that we lack proper unwind support for a registration failure, leaving the VGA arbiter client registered. This is resolved before it turns into an issue, also marked for stable.

Next, we're again reminded of the risky bitfield use in both the vfio-pci-core and mlx5 variant driver, therefore before expanding use for more per-device flags, perform the binary split to categorize flags as either setup/release or runtime to avoid RMW hazards of the bitfields. Also for stable.

Finally, close the series by pulling vfio-pci module parameter policy out of the core, latching each into the device structure.

Quoted 8 lines

This latter change also has the effect of restoring the mutability of two of the module parameters, where that functionality was lost with the vfio-pci-core split. With this series, the current parameter values are latched into the device at probe time.

Please review. Thanks,
Alex

Looks good. for the whole series: