PCI/DOE: Poll briefly for a response before sleeping
PATCH RESEND 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.
---
I was asked if I can do this with interrupts instead. I don't see a way to do that during device enumeration that won't collide with the drivers.
--- drivers/pci/doe.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-)
drivers/pci/doe.c +23 -5
@@ -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
@@ -487,7 +490,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 );
@@ -525,10 +528,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.56.0.385.gd3acb90ef8-goog
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] pci/doe: CPU busy loop when HZ=100 due to PCI_DOE_POLL_INTERVAL=0 --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] pci/doe: CPU busy loop when HZ=100 due to PCI_DOE_POLL_INTERVAL=0 --
commit 16e5277536bbca37386848082d18257e2ffa71dd
Author: Michal Clapinski <[email protected]>
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.
drivers/pci/doe.c line 29
Quoted 8 lines
drivers/pci/doe.c +0
-0@@ -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)
[Severity: Medium]
When HZ=100, does this integer division truncate to 0?
If PCI_DOE_POLL_INTERVAL evaluates to 0, it permanently breaks the fallback sleep mechanism.
Quoted 3 lines
drivers/pci/doe.c +3
-0+#define PCI_DOE_POLL_FAST_MIN_US 20 +#define PCI_DOE_POLL_FAST_MAX_US 50 +#define PCI_DOE_POLL_FAST_TRIES 40
[ ... ]
Quoted 13 lines
drivers/pci/doe.c +5
-4@@ -525,10 +528,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.
drivers/pci/doe.c static void doe_statemachine_work(struct work_struct *work) · line 545
Quoted 10 lines
drivers/pci/doe.c +10
-0+*/ +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 );
[Severity: Medium]
Does passing 0 to this wait function when HZ=100 trigger a tight spin bug instead of yielding the CPU?
Since wait_event_timeout() immediately returns without sleeping when the timeout is 0, this regression results in a tight CPU busy loop spinning over pci_read_config_dword() continuously for up to 1 second during enumeration for affected hardware.
Quoted 7 lines
drivers/pci/doe.c +4
-0+if (rc) { +signal_task_abort (task, rc); +return ; + } } goto retry_resp; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 66 lines · On Mon, Oct 12, 2026 at 1:51 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] pci/doe: CPU busy loop when HZ=100 due to PCI_DOE_POLL_INTERVAL=0 --commit
16e5277536bbca37386848082d18257e2ffa71dd
Author: Michal Clapinski <[email protected]>PCI/DOE: Poll briefly for a response before sleeping
doe_statemachine_work()waits for a response by sleepingPCI_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.drivers/pci/doe.c line 29
Quoted 8 lines
drivers/pci/doe.c +0
-0@@ -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)[Severity: Medium]
When HZ=100, does this integer division truncate to 0?If
PCI_DOE_POLL_INTERVALevaluates to 0, it permanently breaks the fallback sleep mechanism.Quoted 3 lines
drivers/pci/doe.c +3
-0+#define PCI_DOE_POLL_FAST_MIN_US 20 +#define PCI_DOE_POLL_FAST_MAX_US 50 +#define PCI_DOE_POLL_FAST_TRIES 40[ ... ]
Quoted 13 lines
drivers/pci/doe.c +5
-4@@ -525,10 +528,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. drivers/pci/doe.c static void doe_statemachine_work(struct work_struct *work) · line 545
Quoted 10 lines
drivers/pci/doe.c +10
-0+*/ +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 );[Severity: Medium]
Does passing 0 to this wait function when HZ=100 trigger a tight spin bug instead of yielding the CPU?
Quoted 4 lines
Since
wait_event_timeout()immediately returns without sleeping when the timeout is 0, this regression results in a tight CPU busy loop spinning overpci_read_config_dword()continuously for up to 1 second during enumeration for affected hardware.
Yes, but it's a preexisting issue.
Quoted 10 lines
drivers/pci/doe.c static void doe_statemachine_work(struct work_struct *work) · line 552
Quoted 7 lines
drivers/pci/doe.c +4
-0+if (rc) { +signal_task_abort (task, rc); +return ; + } } goto retry_resp; }--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 11 lines · Michal Clapinski
doe_statemachine_work()waits for a response by sleepingPCI_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 inpci_device_add()for a DOE-capable endpoint drops from 41.8 ms to 14.0 ms.
Does any of this remaining time come from pci_doe_abort()? I see that is also called during mailbox creation and has the same call to pci_doe_wait(). If so, maybe the poll logic can be shared.
Quoted 15 lines
Signed-off-by Michal Clapinski <[email protected]>---
I was asked if I can do this with interrupts instead. I don't see a way to do that during device enumeration that won't collide with the drivers.--- drivers/pci/doe.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-)drivers/pci/doe.c +0
-0
drivers/pci/doe.c line 29
Quoted 3 lines
drivers/pci/doe.c +0
-0@@ -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)
It would be nice to also fix the HZ=100 issue, in a separate patch within the same series, since you are updating this code.
Quoted 19 lines
drivers/pci/doe.c +4
-1+#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@@ -487,7 +490,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 );@@ -525,10 +528,25 @@ static void doe_statemachine_work(struct work_struct *work)signal_task_abort (task, -EIO );return ; }
drivers/pci/doe.c static void doe_statemachine_work(struct work_struct *work) · line 536
Quoted 10 lines
drivers/pci/doe.c +6
-4- 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 tens of microsecond is the norm then is polling for up to 2ms overkill?
Quoted 19 lines
drivers/pci/doe.c +13
-0+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.56.0.385.gd3acb90ef8-goog
Quoted 9 lines · David Matlack
Quoted 7 lines · Michal Clapinski
doe_statemachine_work()waits for a response by sleepingPCI_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 8 lines
Quoted 4 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 inpci_device_add()for a DOE-capable endpoint drops from 41.8 ms to 14.0 ms.Does any of this remaining time come from
pci_doe_abort()? I see that is also called during mailbox creation and has the same call topci_doe_wait(). If so, maybe the poll logic can be shared.
Yes, but I'm not sure if the poll logic can be shared without making the code worse rather than better. I'll look into that.
Also, I'm planning on skipping pci_doe_abort for preserved devices so it was less of a priority for me.
Quoted 10 lines
Quoted 10 lines
Signed-off-by Michal Clapinski <[email protected]>---
I was asked if I can do this with interrupts instead. I don't see a way to do that during device enumeration that won't collide with the drivers.--- drivers/pci/doe.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-)
Quoted 11 lines
drivers/pci/doe.c line 29
Quoted 8 lines
drivers/pci/doe.c +0
-0@@ -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)It would be nice to also fix the HZ=100 issue, in a separate patch within the same series, since you are updating this code.
Okay, will do in v2.
Quoted 24 lines
Quoted 24 lines
drivers/pci/doe.c +5
-5+#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@@ -487,7 +490,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 );@@ -525,10 +528,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 ; +/*
Quoted 8 lines
drivers/pci/doe.c static void doe_statemachine_work(struct work_struct *work) · line 536
Quoted 5 lines
drivers/pci/doe.c +5
-0+* 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 tens of microsecond is the norm then is polling for up to 2ms overkill?
I guess. I can do more precise measurements and tweak this value; however, I can only speak for the device I have on hand, so generally I prefer overkilling it.
Quoted 19 lines
Quoted 19 lines
drivers/pci/doe.c +13
-1+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; } --2.56.0.385.gd3acb90ef8-goog
Quoted 20 lines · Michał Cłapiński
Quoted 9 lines · David Matlack
Quoted 7 lines · Michal Clapinski
doe_statemachine_work()waits for a response by sleepingPCI_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 8 lines
Quoted 4 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 inpci_device_add()for a DOE-capable endpoint drops from 41.8 ms to 14.0 ms.Does any of this remaining time come from
pci_doe_abort()? I see that is also called during mailbox creation and has the same call topci_doe_wait(). If so, maybe the poll logic can be shared.
Quoted 5 lines
Yes, but I'm not sure if the poll logic can be shared without making the code worse rather than better. I'll look into that.
Also, I'm planning on skipping
pci_doe_abortfor preserved devices so it was less of a priority for me.
I don't think that will be effective. A Live Update may have any number of preserved and non-preserved devices. We should try to find solutions that work for both types whenever possible. The less places we need to sprinkle pci_liveupdate_is_incoming() the better.
Quoted 81 lines
Quoted 11 lines
Quoted 11 lines
Signed-off-by Michal Clapinski <[email protected]>---
I was asked if I can do this with interrupts instead. I don't see a way to do that during device enumeration that won't collide with the drivers.--- drivers/pci/doe.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-)Quoted 11 lines
drivers/pci/doe.c line 29
Quoted 8 lines
drivers/pci/doe.c +0
-0@@ -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)It would be nice to also fix the HZ=100 issue, in a separate patch within the same series, since you are updating this code.
Okay, will do in v2.
Quoted 24 lines
Quoted 24 lines
drivers/pci/doe.c +5
-5+#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@@ -487,7 +490,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 );@@ -525,10 +528,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 ; +/* Quoted 8 lines
drivers/pci/doe.c static void doe_statemachine_work(struct work_struct *work) · line 536
Quoted 5 lines
drivers/pci/doe.c +5
-0+* 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 tens of microsecond is the norm then is polling for up to 2ms overkill?
I guess. I can do more precise measurements and tweak this value; however, I can only speak for the device I have on hand, so generally I prefer overkilling it.
Quoted 19 lines
Quoted 19 lines
drivers/pci/doe.c +13
-1+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; } --2.56.0.385.gd3acb90ef8-goog