Skip to contentSkip to conversation

PCI/DOE: Poll briefly for a response before sleeping

PATCH Superseded Michal Clapinski 5 participants 6 messages Sep 17 – Oct 16 PCI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI/DOE: Poll briefly for a response before sleeping

doe_statemachine_work() waits for a response by sleeping PCI_DOE_POLL_INTERVAL (PCI_DOE_TIMEOUT / 128) between status reads, which is 7 ms at HZ=1000 and 4 ms at HZ=250. Exchanges are served from the device's local mailbox logic and typically complete within tens of microseconds, so nearly all of that sleep is wasted. Every DOE-capable endpoint pays it once per supported feature during enumeration.

Poll with usleep_range(20, 50) for the first ~2 ms before falling back to the jiffies-based wait. On an Intel Emerald Rapids system, time spent in pci_device_add() for a DOE-capable endpoint drops from 41.8 ms to 14.0 ms.

---
 drivers/pci/doe.c | 28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)
drivers/pci/doe.c +23 -5
index ac95b1d2d999..6dc85133a88e 100644
@@ -27,6 +27,9 @@
 /* Timeout of 1 second from 6.30.2 Operation, PCI Spec r6.0 */
 #define PCI_DOE_TIMEOUT HZ
 #define PCI_DOE_POLL_INTERVAL	(PCI_DOE_TIMEOUT / 128)
+#define PCI_DOE_POLL_FAST_MIN_US	20
+#define PCI_DOE_POLL_FAST_MAX_US	50
+#define PCI_DOE_POLL_FAST_TRIES		40
 
 #define PCI_DOE_FLAG_CANCEL	0
 #define PCI_DOE_FLAG_DEAD	1
@@ -486,7 +489,7 @@ static void doe_statemachine_work(struct work_struct *work)
 	int offset = doe_mb->cap_offset;
 	unsigned long timeout_jiffies;
 	u32 val;
-	int rc;
+	int rc, tries = 0;
 
 	if (test_bit(PCI_DOE_FLAG_DEAD, &doe_mb->flags)) {
 		signal_task_complete(task, -EIO);
@@ -524,10 +527,25 @@ static void doe_statemachine_work(struct work_struct *work)
 			signal_task_abort(task, -EIO);
 			return;
 		}
-		rc = pci_doe_wait(doe_mb, PCI_DOE_POLL_INTERVAL);
-		if (rc) {
-			signal_task_abort(task, rc);
-			return;
+		/*
+		 * Exchanges are served from the device's local mailbox
+		 * logic and usually complete in tens of microseconds, far
+		 * below the jiffies granularity of PCI_DOE_POLL_INTERVAL.
+		 * Poll for up to ~2 ms before falling back to it.
+		 */
+		if (tries++ < PCI_DOE_POLL_FAST_TRIES) {
+			if (test_bit(PCI_DOE_FLAG_CANCEL, &doe_mb->flags)) {
+				signal_task_abort(task, -EIO);
+				return;
+			}
+			usleep_range(PCI_DOE_POLL_FAST_MIN_US,
+				     PCI_DOE_POLL_FAST_MAX_US);
+		} else {
+			rc = pci_doe_wait(doe_mb, PCI_DOE_POLL_INTERVAL);
+			if (rc) {
+				signal_task_abort(task, rc);
+				return;
+			}
 		}
 		goto retry_resp;
 	}
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines

doe_statemachine_work() waits for a response by sleeping PCI_DOE_POLL_INTERVAL (PCI_DOE_TIMEOUT / 128) between status reads, which is 7 ms at HZ=1000 and 4 ms at HZ=250. Exchanges are served from the device's local mailbox logic and typically complete within tens of microseconds, so nearly all of that sleep is wasted. Every DOE-capable endpoint pays it once per supported feature during enumeration.

Quoted 6 lines

Poll with usleep_range(20, 50) for the first ~2 ms before falling back to the jiffies-based wait. On an Intel Emerald Rapids system, time spent in pci_device_add() for a DOE-capable endpoint drops from 41.8 ms to 14.0 ms.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Michal Clapinski
Quoted 11 lines · Michal Clapinski

doe_statemachine_work() waits for a response by sleeping PCI_DOE_POLL_INTERVAL (PCI_DOE_TIMEOUT / 128) between status reads, which is 7 ms at HZ=1000 and 4 ms at HZ=250. Exchanges are served from the device's local mailbox logic and typically complete within tens of microseconds, so nearly all of that sleep is wasted. Every DOE-capable endpoint pays it once per supported feature during enumeration.

Poll with usleep_range(20, 50) for the first ~2 ms before falling back to the jiffies-based wait. On an Intel Emerald Rapids system, time spent in pci_device_add() for a DOE-capable endpoint drops from 41.8 ms to 14.0 ms.

Polling is just a poor man's replacement for interrupts. DOE supports interrupt-driven transfers but we're not taking advantage of it so far.

Perhaps you could look into adding that? It would seem more worthwhile than optimizing polling.

Thanks,

Lukas

#3 lore raw
Quoted 14 lines · Lukas Wunner
Quoted 11 lines · Michal Clapinski

doe_statemachine_work() waits for a response by sleeping PCI_DOE_POLL_INTERVAL (PCI_DOE_TIMEOUT / 128) between status reads, which is 7 ms at HZ=1000 and 4 ms at HZ=250. Exchanges are served from the device's local mailbox logic and typically complete within tens of microseconds, so nearly all of that sleep is wasted. Every DOE-capable endpoint pays it once per supported feature during enumeration.

Poll with usleep_range(20, 50) for the first ~2 ms before falling back to the jiffies-based wait. On an Intel Emerald Rapids system, time spent in pci_device_add() for a DOE-capable endpoint drops from 41.8 ms to 14.0 ms.

Quoted 5 lines

Polling is just a poor man's replacement for interrupts. DOE supports interrupt-driven transfers but we're not taking advantage of it so far.

Perhaps you could look into adding that? It would seem more worthwhile than optimizing polling.

Thanks for the quick response.

This code runs during PCI device enumeration, before a driver is bound, so the core doesn't really have an interrupt to work with: MSI/MSI-X vectors belong to the driver, and enabling them in the core would collide with its later pci_alloc_irq_vectors(); INTx isn't routed yet either, since dev->irq only becomes valid via acpi_pci_irq_enable() at pci_enable_device() time.

#4 lore raw

[+cc Jonathan, Alistair, thread at
https://lore.kernel.org/all/[email protected]]

Quoted 21 lines · Michał Cłapiński
Quoted 14 lines · Lukas Wunner
Quoted 11 lines · Michal Clapinski

doe_statemachine_work() waits for a response by sleeping PCI_DOE_POLL_INTERVAL (PCI_DOE_TIMEOUT / 128) between status reads, which is 7 ms at HZ=1000 and 4 ms at HZ=250. Exchanges are served from the device's local mailbox logic and typically complete within tens of microseconds, so nearly all of that sleep is wasted. Every DOE-capable endpoint pays it once per supported feature during enumeration.

Poll with usleep_range(20, 50) for the first ~2 ms before falling back to the jiffies-based wait. On an Intel Emerald Rapids system, time spent in pci_device_add() for a DOE-capable endpoint drops from 41.8 ms to 14.0 ms.

Quoted 5 lines

Polling is just a poor man's replacement for interrupts. DOE supports interrupt-driven transfers but we're not taking advantage of it so far.

Perhaps you could look into adding that? It would seem more worthwhile than optimizing polling.

Quoted 8 lines

Thanks for the quick response.

This code runs during PCI device enumeration, before a driver is bound, so the core doesn't really have an interrupt to work with:
MSI/MSI-X vectors belong to the driver, and enabling them in the core would collide with its later pci_alloc_irq_vectors(); INTx isn't routed yet either, since dev->irq only becomes valid via acpi_pci_irq_enable() at pci_enable_device() time.

Do we have to run the DOE setup code during enumeration at all? Maybe we can wait until a user that needs DOE functionality comes along?

#5 lore raw
Quoted 39 lines · Bjorn Helgaas

[+cc Jonathan, Alistair, thread at
https://lore.kernel.org/all/[email protected]
]

Quoted 34 lines · Michał Cłapiński
Quoted 21 lines · Lukas Wunner
Quoted 11 lines · Michal Clapinski

doe_statemachine_work() waits for a response by sleeping PCI_DOE_POLL_INTERVAL (PCI_DOE_TIMEOUT / 128) between status reads,
which is 7 ms at HZ=1000 and 4 ms at HZ=250. Exchanges are served from
the device's local mailbox logic and typically complete within tens of
microseconds, so nearly all of that sleep is wasted. Every DOE- capable
endpoint pays it once per supported feature during enumeration.

Quoted 7 lines

Poll with usleep_range(20, 50) for the first ~2 ms before falling back
to the jiffies-based wait. On an Intel Emerald Rapids system, time spent
in pci_device_add() for a DOE-capable endpoint drops from 41.8 ms to
14.0 ms.

Quoted 8 lines

Polling is just a poor man's replacement for interrupts.  DOE supports
interrupt-driven transfers but we're not taking advantage of it so far.

Perhaps you could look into adding that?  It would seem more worthwhile
than optimizing polling.

Thanks for the quick response.

Quoted 8 lines
Quoted 6 lines

This code runs during PCI device enumeration, before a driver is bound, so the core doesn't really have an interrupt to work with:
MSI/MSI-X vectors belong to the driver, and enabling them in the core would collide with its later pci_alloc_irq_vectors(); INTx isn't routed yet either, since dev->irq only becomes valid via acpi_pci_irq_enable() at pci_enable_device() time.

Do we have to run the DOE setup code during enumeration at all?

Yeah, I think we do.

We need to probe DOE to know if the device supports CMA, at which point we need to attest and authenticate the device.

Granted that isn't upstream yet, but in general we will need DOE at enumeration.

Quoted 2 lines

Maybe
we can wait until a user that needs DOE functionality comes along?

We don't know who that user is until we know the DOE features is the issue

Alistair