[RFC net-next 2/5] net: Suspend softirq when prefer_busy_poll is set

From: Joe Damato
Date: Mon Aug 12 2024 - 08:58:20 EST


From: Martin Karsten <mkarsten@xxxxxxxxxxxx>

When NAPI_F_PREFER_BUSY_POLL is set during busy_poll_stop and the
irq_suspend_timeout sysfs is nonzero, this timeout is used to defer
softirq scheduling, potentially longer than gro_flush_timeout. This can
be used to effectively suspend softirq processing during the time it
takes for an application to process data and return to the next
busy-poll.

The call to napi->poll in busy_poll_stop might lead to an invocation of
napi_complete_done, but the prefer-busy flag is still set at that time,
so the same logic is used to defer softirq scheduling for
irq_suspend_timeout.

Signed-off-by: Martin Karsten <mkarsten@xxxxxxxxxxxx>
Co-developed-by: Joe Damato <jdamato@xxxxxxxxxx>
Signed-off-by: Joe Damato <jdamato@xxxxxxxxxx>
Tested-by: Joe Damato <jdamato@xxxxxxxxxx>
Tested-by: Martin Karsten <mkarsten@xxxxxxxxxxxx>
---
net/core/dev.c | 19 +++++++++++++------
1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/net/core/dev.c b/net/core/dev.c
index bf45c90aebcd..a19470253eb4 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -6214,7 +6214,6 @@ bool napi_complete_done(struct napi_struct *n, int work_done)
{
unsigned long flags, val, new, timeout = 0;
bool ret = true;
-
/*
* 1) Don't let napi dequeue from the cpu poll list
* just in case its running on a different cpu.
@@ -6230,7 +6229,12 @@ bool napi_complete_done(struct napi_struct *n, int work_done)
timeout = READ_ONCE(n->dev->gro_flush_timeout);
n->defer_hard_irqs_count = READ_ONCE(n->dev->napi_defer_hard_irqs);
}
- if (n->defer_hard_irqs_count > 0) {
+ if (napi_prefer_busy_poll(n)) {
+ timeout = READ_ONCE(n->dev->irq_suspend_timeout);
+ if (timeout)
+ ret = false;
+ }
+ if (ret && n->defer_hard_irqs_count > 0) {
n->defer_hard_irqs_count--;
timeout = READ_ONCE(n->dev->gro_flush_timeout);
if (timeout)
@@ -6349,7 +6353,6 @@ static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock,
bool skip_schedule = false;
unsigned long timeout;
int rc;
-
/* Busy polling means there is a high chance device driver hard irq
* could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was
* set in napi_schedule_prep().
@@ -6366,9 +6369,13 @@ static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock,
bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);

if (flags & NAPI_F_PREFER_BUSY_POLL) {
- napi->defer_hard_irqs_count = READ_ONCE(napi->dev->napi_defer_hard_irqs);
- timeout = READ_ONCE(napi->dev->gro_flush_timeout);
- if (napi->defer_hard_irqs_count && timeout) {
+ timeout = READ_ONCE(napi->dev->irq_suspend_timeout);
+ if (!timeout) {
+ napi->defer_hard_irqs_count = READ_ONCE(napi->dev->napi_defer_hard_irqs);
+ if (napi->defer_hard_irqs_count)
+ timeout = READ_ONCE(napi->dev->gro_flush_timeout);
+ }
+ if (timeout) {
hrtimer_start(&napi->timer, ns_to_ktime(timeout), HRTIMER_MODE_REL_PINNED);
skip_schedule = true;
}
--
2.25.1