git: 50bbc49becdf - main - vmm: don't hold the global PPT lock while resetting a device

From: Nick Price <nprice_at_FreeBSD.org>
Date: Tue, 24 Nov 2026 05:50:03 UTC
The branch main has been updated by nprice:

URL: https://cgit.FreeBSD.org/src/commit/?id=50bbc49becdf92be114969b5f8e26fbe614422e0

commit 50bbc49becdf92be114969b5f8e26fbe614422e0
Author:     Nick Price <nprice@FreeBSD.org>
AuthorDate: 2026-11-24 05:49:09 +0000
Commit:     Nick Price <nprice@FreeBSD.org>
CommitDate: 2026-11-24 05:49:54 +0000

    vmm: don't hold the global PPT lock while resetting a device
    
    `ppt_assign_device()` holds the global PPT lock across the save/reset/restore. `pcie_flr()` can wait up to the reset timeout, and every PPT operation on every VM stalls behind it.
    
    Reserve the function with `resetting` and drop the lock for the reset, as `ppt_reset_device()` already does (D58864). Set `ppt->vm` first so a competing assign or `ppt_detach()` gets EBUSY at once instead of sleeping out the reset. Clear it if the assign fails.
    
    Apply matching changes to `ppt_unassign_device()` which may actually take longer on properly-functioning hardware due to teardown duration.
    
    Reviewed by:    adrian, markj
    Differential Revision:  https://reviews.freebsd.org/D60188
    Signed-off-by:  Nick Price <nprice@FreeBSD.org>
---
 sys/amd64/vmm/io/ppt.c | 38 +++++++++++++++++++++++++++++---------
 1 file changed, 29 insertions(+), 9 deletions(-)

diff --git a/sys/amd64/vmm/io/ppt.c b/sys/amd64/vmm/io/ppt.c
index 961c0747b201..ac399ee54b3b 100644
--- a/sys/amd64/vmm/io/ppt.c
+++ b/sys/amd64/vmm/io/ppt.c
@@ -90,7 +90,7 @@ struct pptseg {
 struct pptdev {
 	device_t	dev;
 	struct vm	*vm;			/* owner of this device */
-	bool		resetting;		/* guest FLR in progress */
+	bool		resetting;		/* reset in progress */
 	TAILQ_ENTRY(pptdev)	next;
 	struct pptseg mmio[MAX_MMIOSEGS];
 	struct {
@@ -251,11 +251,12 @@ ppt_find(struct vm *vm, int bus, int slot, int func, struct pptdev **pptp)
 		if (!ppt->resetting)
 			break;
 		/*
-		 * Once resetting is set, every exit from ppt_reset_device()
-		 * reacquires ppt_mtx, clears resetting, and wakes us.  The FLR wait
-		 * itself is bounded.
+		 * Once resetting is set, every exit from ppt_reset_device(),
+		 * ppt_assign_device(), and ppt_unassign_device() reacquires
+		 * ppt_mtx, clears resetting, and wakes us. The wait itself
+		 * is bounded.
 		 */
-		sx_sleep(ppt, &ppt_mtx, 0, "pptflr", 0);
+		sx_sleep(ppt, &ppt_mtx, 0, "pptrst", 0);
 	}
 
 	*pptp = ppt;
@@ -439,20 +440,31 @@ ppt_assign_device(struct vm *vm, int bus, int slot, int func)
 	/* Passing NULL requires the device to be unowned. */
 	error = ppt_find(NULL, bus, slot, func, &ppt);
 	if (error != 0)
-		goto out;
+		goto out_locked;
+
+	ppt->resetting = true;
+	ppt->vm = vm;
 
+	/* Release the lock to allow for longer reset cycles */
+	PPT_UNLOCK();
 	pci_save_state(ppt->dev);
 	ppt_pci_reset(ppt->dev);
 	pci_restore_state(ppt->dev);
+	PPT_LOCK();
+
 	error = iommu_add_device(vm_iommu_domain(vm), ppt->dev,
 	    pci_get_rid(ppt->dev));
 	if (error != 0)
-		goto out;
-	ppt->vm = vm;
+		goto out_reset;
 	cmd = pci_read_config(ppt->dev, PCIR_COMMAND, 2);
 	cmd |= PCIM_CMD_BUSMASTEREN | ppt_bar_enables(ppt);
 	pci_write_config(ppt->dev, PCIR_COMMAND, cmd, 2);
-out:
+out_reset:
+	if (error != 0)
+		ppt->vm = NULL;
+	ppt->resetting = false;
+	wakeup(ppt);
+out_locked:
 	PPT_UNLOCK();
 	return (error);
 }
@@ -472,15 +484,23 @@ ppt_unassign_device(struct vm *vm, int bus, int slot, int func)
 	cmd = pci_read_config(ppt->dev, PCIR_COMMAND, 2);
 	cmd &= ~(PCIM_CMD_PORTEN | PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN);
 	pci_write_config(ppt->dev, PCIR_COMMAND, cmd, 2);
+	ppt->resetting = true;
+
+	/* Release the lock to allow for longer reset/teardown cycles */
+	PPT_UNLOCK();
 	pci_save_state(ppt->dev);
 	ppt_pci_reset(ppt->dev);
 	pci_restore_state(ppt->dev);
 	ppt_unmap_all_mmio(vm, ppt);
 	ppt_teardown_msi(ppt);
 	ppt_teardown_msix(ppt);
+	PPT_LOCK();
+
 	error = iommu_remove_device(vm_iommu_domain(vm), ppt->dev,
 	    pci_get_rid(ppt->dev));
 	ppt->vm = NULL;
+	ppt->resetting = false;
+	wakeup(ppt);
 out:
 	PPT_UNLOCK();
 	return (error);