vfio/pci: Only lock the upstream bridge if the reset may need it
Subsystems (4)
PATCH v2 1/1 vfio/pci: Only lock the upstream bridge if the reset may need it
Commit 962ae6892d8b ("vfio/pci: Lock upstream bridge for vfio_pci_core_disable()") makes vfio_pci_core_disable() trylock the upstream bridge before resetting the device, and skips the reset if the trylock fails.
All VFs of a PF have the same upstream bridge. Take a device with two VFs, both used by one process. When the process is killed, both VFs can be released at the same time. The first one takes the bridge lock and does its FLR, the second one fails the trylock and is never reset.
The bridge lock is only needed for a bus reset, which is done through the bridge. Add pci_reset_function_needs_bridge_lock(), which returns false if there is no upstream bridge, if bus resets are disabled for the device, or if the device is a VF. Use it in vfio_pci_core_disable() and pci_reset_function() to only lock the bridge when it is needed.
---
v2:
- Replace pci_reset_needs_bridge_lock() with pci_reset_function_needs_bridge_lock() as suggested by Alex, and use it in pci_reset_function() too.
--- drivers/pci/pci.c | 25 ++++++++++++++++++++++--- drivers/vfio/pci/vfio_pci_core.c | 3 ++- include/linux/pci.h | 1 + 3 files changed, 25 insertions(+), 4 deletions(-)
drivers/pci/pci.c +22 -3
@@ -5256,6 +5256,24 @@ int __pci_reset_function_locked(struct pci_dev *dev) }EXPORT_SYMBOL_GPL (__pci_reset_function_locked); +/** +* pci_reset_function_needs_bridge_lock - check whether a function reset of +* @dev may need the upstream bridge to be locked +* @dev: PCI device to check +* +* A bus reset of @dev is done through its upstream bridge, so the caller must +* hold the bridge lock. The bridge doesn't need to be locked if there is no +* upstream bridge, if bus resets are disabled for @dev, or if @dev is a VF. +* +* Return: true if a function reset of @dev may be a bus reset. +*/ +bool pci_reset_function_needs_bridge_lock (struct pci_dev *dev) +{ +return pci_upstream_bridge (dev) && + !(dev->dev_flags &PCI_DEV_FLAGS_NO_BUS_RESET ) && !dev->is_virtfn; +} +EXPORT_SYMBOL_GPL (pci_reset_function_needs_bridge_lock); +/** * pci_init_reset_methods - check whether device can be safely reset * and store supported reset mechanisms.
@@ -5313,10 +5331,11 @@ int pci_reset_function(struct pci_dev *dev)return -ENOTTY ;/* -* If there's no upstream bridge, no locking is needed since there is -* no upstream bridge configuration to hold consistent. +* The upstream bridge only needs to be locked if the reset may be a +* bus reset, which is done through the bridge. */ - bridge =pci_upstream_bridge (dev); + bridge =pci_reset_function_needs_bridge_lock (dev) ? +pci_upstream_bridge (dev) :NULL ;if (bridge)pci_dev_lock (bridge);
drivers/vfio/pci/vfio_pci_core.c +2 -1
@@ -777,7 +777,8 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)* overwrite the previously restored configuration information. */ if (vdev->reset_works) { - bridge =pci_upstream_bridge (pdev); + bridge =pci_reset_function_needs_bridge_lock (pdev) ? +pci_upstream_bridge (pdev) :NULL ;if (bridge && !pci_dev_trylock (bridge)) goto out_restore_state;if (pci_dev_trylock (pdev)) {
include/linux/pci.h +1 -0
@@ -1486,6 +1486,7 @@ void pcie_print_link_status(struct pci_dev *dev);int pcie_reset_flr (struct pci_dev *dev,bool probe);int pcie_flr (struct pci_dev *dev);int __pci_reset_function_locked (struct pci_dev *dev); +bool pci_reset_function_needs_bridge_lock (struct pci_dev *dev);int pci_reset_function (struct pci_dev *dev);int pci_reset_function_locked (struct pci_dev *dev);int pci_try_reset_function (struct pci_dev *dev);
base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
Signature · 1 line
2.56.1.831.g720f3f5937-goog
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 16 lines
Commit
962ae6892d8b("vfio/pci: Lock upstream bridge forvfio_pci_core_disable()") makesvfio_pci_core_disable()trylock the upstream bridge before resetting the device, and skips the reset if the trylock fails.All VFs of a PF have the same upstream bridge. Take a device with two VFs, both used by one process. When the process is killed, both VFs can be released at the same time. The first one takes the bridge lock and does its FLR, the second one fails the trylock and is never reset.
The bridge lock is only needed for a bus reset, which is done through the bridge. Add
pci_reset_function_needs_bridge_lock(), which returns false if there is no upstream bridge, if bus resets are disabled for the device, or if the device is a VF. Use it invfio_pci_core_disable()andpci_reset_function()to only lock the bridge when it is needed.
Quoted 3 lines
Fixes 962ae6892d8b ("vfio/pci: Lock upstream bridge for vfio_pci_core_disable()")Signed-off-by Samiullah Khawaja <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 24 lines · On Tue, 24 Nov 2026 21:28:42 +0000 Samiullah Khawaja <[email protected]>…
Commit
962ae6892d8b("vfio/pci: Lock upstream bridge forvfio_pci_core_disable()") makesvfio_pci_core_disable()trylock the upstream bridge before resetting the device, and skips the reset if the trylock fails.All VFs of a PF have the same upstream bridge. Take a device with two VFs, both used by one process. When the process is killed, both VFs can be released at the same time. The first one takes the bridge lock and does its FLR, the second one fails the trylock and is never reset.
The bridge lock is only needed for a bus reset, which is done through the bridge. Add
pci_reset_function_needs_bridge_lock(), which returns false if there is no upstream bridge, if bus resets are disabled for the device, or if the device is a VF. Use it invfio_pci_core_disable()andpci_reset_function()to only lock the bridge when it is needed.Fixes 962ae6892d8b ("vfio/pci: Lock upstream bridge for vfio_pci_core_disable()")Signed-off-by Samiullah Khawaja <[email protected]>---
v2:
- Replacepci_reset_needs_bridge_lock()withpci_reset_function_needs_bridge_lock()as suggested by Alex, and use it inpci_reset_function()too.
Quoted 5 lines
--- drivers/pci/pci.c | 25 ++++++++++++++++++++++--- drivers/vfio/pci/vfio_pci_core.c | 3 ++- include/linux/pci.h | 1 + 3 files changed, 25 insertions(+), 4 deletions(-)
Quoted 72 lines
drivers/pci/pci.c +22
-3@@ -5256,6 +5256,24 @@ int __pci_reset_function_locked(struct pci_dev *dev) }EXPORT_SYMBOL_GPL (__pci_reset_function_locked); +/** +* pci_reset_function_needs_bridge_lock - check whether a function reset of +* @dev may need the upstream bridge to be locked +* @dev: PCI device to check +* +* A bus reset of @dev is done through its upstream bridge, so the caller must +* hold the bridge lock. The bridge doesn't need to be locked if there is no +* upstream bridge, if bus resets are disabled for @dev, or if @dev is a VF. +* +* Return: true if a function reset of @dev may be a bus reset. +*/ +bool pci_reset_function_needs_bridge_lock (struct pci_dev *dev) +{ +return pci_upstream_bridge (dev) && + !(dev->dev_flags &PCI_DEV_FLAGS_NO_BUS_RESET ) && !dev->is_virtfn; +} +EXPORT_SYMBOL_GPL (pci_reset_function_needs_bridge_lock); +/** * pci_init_reset_methods - check whether device can be safely reset * and store supported reset mechanisms. @@ -5313,10 +5331,11 @@ int pci_reset_function(struct pci_dev *dev)return -ENOTTY ;/* -* If there's no upstream bridge, no locking is needed since there is -* no upstream bridge configuration to hold consistent. +* The upstream bridge only needs to be locked if the reset may be a +* bus reset, which is done through the bridge. */ - bridge =pci_upstream_bridge (dev); + bridge =pci_reset_function_needs_bridge_lock (dev) ? +pci_upstream_bridge (dev) :NULL ;if (bridge)pci_dev_lock (bridge);drivers/vfio/pci/vfio_pci_core.c +2
-1@@ -777,7 +777,8 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)* overwrite the previously restored configuration information. */ if (vdev->reset_works) { - bridge =pci_upstream_bridge (pdev); + bridge =pci_reset_function_needs_bridge_lock (pdev) ? +pci_upstream_bridge (pdev) :NULL ;if (bridge && !pci_dev_trylock (bridge)) goto out_restore_state;if (pci_dev_trylock (pdev)) {include/linux/pci.h +1
-0@@ -1486,6 +1486,7 @@ void pcie_print_link_status(struct pci_dev *dev);int pcie_reset_flr (struct pci_dev *dev,bool probe);int pcie_flr (struct pci_dev *dev);int __pci_reset_function_locked (struct pci_dev *dev); +bool pci_reset_function_needs_bridge_lock (struct pci_dev *dev);int pci_reset_function (struct pci_dev *dev);int pci_reset_function_locked (struct pci_dev *dev);int pci_try_reset_function (struct pci_dev *dev);base-commit:
7475ea8b58616b33c04afb976cf0fbaad9dd63fb
Bjorn Helgaas
AppliedQuoted 16 lines · Samiullah Khawaja
Commit
962ae6892d8b("vfio/pci: Lock upstream bridge forvfio_pci_core_disable()") makesvfio_pci_core_disable()trylock the upstream bridge before resetting the device, and skips the reset if the trylock fails.All VFs of a PF have the same upstream bridge. Take a device with two VFs, both used by one process. When the process is killed, both VFs can be released at the same time. The first one takes the bridge lock and does its FLR, the second one fails the trylock and is never reset.
The bridge lock is only needed for a bus reset, which is done through the bridge. Add
pci_reset_function_needs_bridge_lock(), which returns false if there is no upstream bridge, if bus resets are disabled for the device, or if the device is a VF. Use it invfio_pci_core_disable()andpci_reset_function()to only lock the bridge when it is needed.
Quoted 3 lines
Fixes 962ae6892d8b ("vfio/pci: Lock upstream bridge for vfio_pci_core_disable()")Signed-off-by Samiullah Khawaja <[email protected]>
Applied to pci/reset for v7.5, thanks!
Minor conflict with Ionut's "PCI: Don't block bridge config access while waiting for the device lock", so double check if you have a chance:
https://git.kernel.org/pub/scm/linux/kernel/git/pci/pci.git/log/?h=reset
Quoted 86 lines
---
v2:
- Replacepci_reset_needs_bridge_lock()withpci_reset_function_needs_bridge_lock()as suggested by Alex, and use it inpci_reset_function()too.--- drivers/pci/pci.c | 25 ++++++++++++++++++++++--- drivers/vfio/pci/vfio_pci_core.c | 3 ++- include/linux/pci.h | 1 + 3 files changed, 25 insertions(+), 4 deletions(-)drivers/pci/pci.c +22
-3@@ -5256,6 +5256,24 @@ int __pci_reset_function_locked(struct pci_dev *dev) }EXPORT_SYMBOL_GPL (__pci_reset_function_locked); +/** +* pci_reset_function_needs_bridge_lock - check whether a function reset of +* @dev may need the upstream bridge to be locked +* @dev: PCI device to check +* +* A bus reset of @dev is done through its upstream bridge, so the caller must +* hold the bridge lock. The bridge doesn't need to be locked if there is no +* upstream bridge, if bus resets are disabled for @dev, or if @dev is a VF. +* +* Return: true if a function reset of @dev may be a bus reset. +*/ +bool pci_reset_function_needs_bridge_lock (struct pci_dev *dev) +{ +return pci_upstream_bridge (dev) && + !(dev->dev_flags &PCI_DEV_FLAGS_NO_BUS_RESET ) && !dev->is_virtfn; +} +EXPORT_SYMBOL_GPL (pci_reset_function_needs_bridge_lock); +/** * pci_init_reset_methods - check whether device can be safely reset * and store supported reset mechanisms. @@ -5313,10 +5331,11 @@ int pci_reset_function(struct pci_dev *dev)return -ENOTTY ;/* -* If there's no upstream bridge, no locking is needed since there is -* no upstream bridge configuration to hold consistent. +* The upstream bridge only needs to be locked if the reset may be a +* bus reset, which is done through the bridge. */ - bridge =pci_upstream_bridge (dev); + bridge =pci_reset_function_needs_bridge_lock (dev) ? +pci_upstream_bridge (dev) :NULL ;if (bridge)pci_dev_lock (bridge);drivers/vfio/pci/vfio_pci_core.c +2
-1@@ -777,7 +777,8 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)* overwrite the previously restored configuration information. */ if (vdev->reset_works) { - bridge =pci_upstream_bridge (pdev); + bridge =pci_reset_function_needs_bridge_lock (pdev) ? +pci_upstream_bridge (pdev) :NULL ;if (bridge && !pci_dev_trylock (bridge)) goto out_restore_state;if (pci_dev_trylock (pdev)) {include/linux/pci.h +1
-0@@ -1486,6 +1486,7 @@ void pcie_print_link_status(struct pci_dev *dev);int pcie_reset_flr (struct pci_dev *dev,bool probe);int pcie_flr (struct pci_dev *dev);int __pci_reset_function_locked (struct pci_dev *dev); +bool pci_reset_function_needs_bridge_lock (struct pci_dev *dev);int pci_reset_function (struct pci_dev *dev);int pci_reset_function_locked (struct pci_dev *dev);int pci_try_reset_function (struct pci_dev *dev);base-commit:
7475ea8b58616b33c04afb976cf0fbaad9dd63fbSignature · 1 line
2.56.1.831.g720f3f5937-goog