Skip to contentSkip to conversation

Add support to do threaded napi busy poll

PATCH Superseded Samiullah Khawaja 6 participants 23 messages Apr 24, 2025 – Apr 30, 2025

Subsystems (9)

v5 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v5 cover Add support to do threaded napi busy poll

Extend the already existing support of threaded napi poll to do continuous busy polling.

This is used for doing continuous polling of napi to fetch descriptors from backing RX/TX queues for low latency applications. Allow enabling of threaded busypoll using netlink so this can be enabled on a set of dedicated napis for low latency applications.

Once enabled user can fetch the PID of the kthread doing NAPI polling and set affinity, priority and scheduler for it depending on the low-latency requirements.

Currently threaded napi is only enabled at device level using sysfs. Add support to enable/disable threaded mode for a napi individually. This can be done using the netlink interface. Extend napi-set op in netlink spec that allows setting the threaded attribute of a napi.

Extend the threaded attribute in napi struct to add an option to enable continuous busy polling. Extend the netlink and sysfs interface to allow enabling/disabling threaded busypolling at device or individual napi level.

We use this for our AF_XDP based hard low-latency usecase with usecs level latency requirement. For our usecase we want low jitter and stable latency at P99.

Following is an analysis and comparison of available (and compatible) busy poll interfaces for a low latency usecase with stable P99. Please note that the throughput and cpu efficiency is a non-goal.

For analysis we use an AF_XDP based benchmarking tool xdp_rr. The description of the tool and how it tries to simulate the real workload is following,

- It sends UDP packets between 2 machines.
- The client machine sends packets at a fixed frequency. To maintain the frequency of the packet being sent, we use open-loop sampling. That is the packets are sent in a separate thread.
- The server replies to the packet inline by reading the pkt from the recv ring and replies using the tx ring.
- To simulate the application processing time, we use a configurable delay in usecs on the client side after a reply is received from the server.

The xdp_rr tool is posted separately as an RFC for tools/testing/selftest.

We use this tool with following napi polling configurations,

- Interrupts only
- SO_BUSYPOLL (inline in the same thread where the client receives the packet).
- SO_BUSYPOLL (separate thread and separate core)
- Threaded NAPI busypoll

System is configured using following script in all 4 cases,

```
echo 0 | sudo tee /sys/class/net/eth0/threaded
echo 0 | sudo tee /proc/sys/kernel/timer_migration
echo off | sudo tee  /sys/devices/system/cpu/smt/control

sudo ethtool -L eth0 rx 1 tx 1
sudo ethtool -G eth0 rx 1024

echo 0 | sudo tee /proc/sys/net/core/rps_sock_flow_entries
echo 0 | sudo tee /sys/class/net/eth0/queues/rx-0/rps_cpus

 # pin IRQs on CPU 2
IRQS="$(gawk '/eth0-(TxRx-)?1/ {match($1, /([0-9]+)/, arr); \
				print arr[0]}' < /proc/interrupts)"
for irq in "${IRQS}"; \
	do echo 2 | sudo tee /proc/irq/$irq/smp_affinity_list; done

echo -1 | sudo tee /proc/sys/kernel/sched_rt_runtime_us

for i in /sys/devices/virtual/workqueue/*/cpumask; \
			do echo $i; echo 1,2,3,4,5,6 > $i; done

if [[ -z "$1" ]]; then
  echo 400 | sudo tee /proc/sys/net/core/busy_read
  echo 100 | sudo tee /sys/class/net/eth0/napi_defer_hard_irqs
  echo 15000   | sudo tee /sys/class/net/eth0/gro_flush_timeout
fi

sudo ethtool -C eth0 adaptive-rx off adaptive-tx off rx-usecs 0 tx-usecs 0

if [[ "$1" == "enable_threaded" ]]; then
  echo 0 | sudo tee /proc/sys/net/core/busy_poll
  echo 0 | sudo tee /proc/sys/net/core/busy_read
  echo 100 | sudo tee /sys/class/net/eth0/napi_defer_hard_irqs
  echo 15000 | sudo tee /sys/class/net/eth0/gro_flush_timeout
  echo 2 | sudo tee /sys/class/net/eth0/threaded
  NAPI_T=$(ps -ef | grep napi | grep -v grep | awk '{ print $2 }')
  sudo chrt -f  -p 50 $NAPI_T

  # pin threaded poll thread to CPU 2
  sudo taskset -pc 2 $NAPI_T
fi

if [[ "$1" == "enable_interrupt" ]]; then
  echo 0 | sudo tee /proc/sys/net/core/busy_read
  echo 0 | sudo tee /sys/class/net/eth0/napi_defer_hard_irqs
  echo 15000 | sudo tee /sys/class/net/eth0/gro_flush_timeout
fi
```

To enable various configurations, script can be run as following,

- Interrupt Only

  ```
  <script> enable_interrupt
  ```

- SO_BUSYPOLL (no arguments to script)

  ```
  <script>
  ```

- NAPI threaded busypoll

  ```
  <script> enable_threaded
  ```

If using idpf, the script needs to be run again after launching the workload just to make sure that the configurations are not reverted. As idpf reverts some configurations on software reset when AF_XDP program is attached.

Once configured, the workload is run with various configurations using following commands. Set period (1/frequency) and delay in usecs to produce results for packet frequency and application processing delay.

## Interrupt Only and SO_BUSY_POLL (inline)

- Server

```
sudo chrt -f 50 taskset -c 3-5 ./xsk_rr -o 0 -B 400 -i eth0 -4 \
	-D <IP-dest> -S <IP-src> -M <MAC-dst> -m <MAC-src> -p 54321 -h -v
```

- Client

```
sudo chrt -f 50 taskset -c 3-5 ./xsk_rr -o 0 -B 400 -i eth0 -4 \
	-S <IP-src> -D <IP-dest> -m <MAC-src> -M <MAC-dst> -p 54321 \
	-P <Period-usecs> -d <Delay-usecs>  -T -l 1 -v
```

## SO_BUSY_POLL(done in separate core using recvfrom)

Argument -t spawns a seprate thread and continuously calls recvfrom.

- Server

```
sudo chrt -f 50 taskset -c 3-5 ./xsk_rr -o 0 -B 400 -i eth0 -4 \
	-D <IP-dest> -S <IP-src> -M <MAC-dst> -m <MAC-src> -p 54321 \
	-h -v -t
```

- Client

```
sudo chrt -f 50 taskset -c 3-5 ./xsk_rr -o 0 -B 400 -i eth0 -4 \
	-S <IP-src> -D <IP-dest> -m <MAC-src> -M <MAC-dst> -p 54321 \
	-P <Period-usecs> -d <Delay-usecs>  -T -l 1 -v -t
```

## NAPI Threaded Busy Poll

Argument -n skips the recvfrom call as there is no recv kick needed.

- Server

```
sudo chrt -f 50 taskset -c 3-5 ./xsk_rr -o 0 -B 400 -i eth0 -4 \
	-D <IP-dest> -S <IP-src> -M <MAC-dst> -m <MAC-src> -p 54321 \
	-h -v -n
```

- Client

```
sudo chrt -f 50 taskset -c 3-5 ./xsk_rr -o 0 -B 400 -i eth0 -4 \
	-S <IP-src> -D <IP-dest> -m <MAC-src> -M <MAC-dst> -p 54321 \
	-P <Period-usecs> -d <Delay-usecs>  -T -l 1 -v -n
```
| Experiment | interrupts | SO_BUSYPOLL | SO_BUSYPOLL(separate) | NAPI threaded |
|---|---|---|---|---|
| 12 Kpkt/s + 0us delay | | | | |
|  | p5: 12700 | p5: 12900 | p5: 13300 | p5: 12800 |
|  | p50: 13100 | p50: 13600 | p50: 14100 | p50: 13000 |
|  | p95: 13200 | p95: 13800 | p95: 14400 | p95: 13000 |
|  | p99: 13200 | p99: 13800 | p99: 14400 | p99: 13000 |
| 32 Kpkt/s + 30us delay | | | | |
|  | p5: 19900 | p5: 16600 | p5: 13100 | p5: 12800 |
|  | p50: 21100 | p50: 17000 | p50: 13700 | p50: 13000 |
|  | p95: 21200 | p95: 17100 | p95: 14000 | p95: 13000 |
|  | p99: 21200 | p99: 17100 | p99: 14000 | p99: 13000 |
| 125 Kpkt/s + 6us delay | | | | |
|  | p5: 14600 | p5: 17100 | p5: 13300 | p5: 12900 |
|  | p50: 15400 | p50: 17400 | p50: 13800 | p50: 13100 |
|  | p95: 15600 | p95: 17600 | p95: 14000 | p95: 13100 |
|  | p99: 15600 | p99: 17600 | p99: 14000 | p99: 13100 |
| 12 Kpkt/s + 78us delay | | | | |
|  | p5: 14100 | p5: 16700 | p5: 13200 | p5: 12600 |
|  | p50: 14300 | p50: 17100 | p50: 13900 | p50: 12800 |
|  | p95: 14300 | p95: 17200 | p95: 14200 | p95: 12800 |
|  | p99: 14300 | p99: 17200 | p99: 14200 | p99: 12800 |
| 25 Kpkt/s + 38us delay | | | | |
|  | p5: 19900 | p5: 16600 | p5: 13000 | p5: 12700 |
|  | p50: 21000 | p50: 17100 | p50: 13800 | p50: 12900 |
|  | p95: 21100 | p95: 17100 | p95: 14100 | p95: 12900 |
|  | p99: 21100 | p99: 17100 | p99: 14100 | p99: 12900 |

## Observations

- Here without application processing all the approaches give the same latency within 1usecs range and NAPI threaded gives minimum latency.
- With application processing the latency increases by 3-4usecs when doing inline polling.
- Using a dedicated core to drive napi polling keeps the latency same even with application processing. This is observed both in userspace and threaded napi (in kernel).
- Using napi threaded polling in kernel gives lower latency by 1-1.5usecs as compared to userspace driven polling in separate core.
- With application processing userspace will get the packet from recv ring and spend some time doing application processing and then do napi polling. While application processing is happening a dedicated core doing napi polling can pull the packet of the NAPI RX queue and populate the AF_XDP recv ring. This means that when the application thread is done with application processing it has new packets ready to recv and process in recv ring.
- Napi threaded busy polling in the kernel with a dedicated core gives the consistent P5-P99 latency.

Following histogram is generated to measure the time spent in recvfrom while using inline thread with SO_BUSYPOLL. The histogram is generated using the following bpftrace command. In this experiment there are 32K packets per second and the application processing delay is 30usecs. This is to measure whether there is significant time spent pulling packets from the descriptor queue that it will affect the overall latency if done inline.

```
bpftrace -e '
        kprobe:xsk_recvmsg {
                @start[tid] = nsecs;
        }
        kretprobe:xsk_recvmsg {
                if (@start[tid]) {
                        $sample = (nsecs - @start[tid]);
                        @xsk_recvfrom_hist = hist($sample);
                        delete(@start[tid]);
                }
        }
        END { clear(@start);}'
```

Here in case of inline busypolling around 35 percent of calls are taking 1-2usecs and around 50 percent are taking 0.5-2usecs.

@xsk_recvfrom_hist:

[128, 256)         24073 |@@@@@@@@@@@@@@@@@@@@@@                              |
[256, 512)         55633 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
[512, 1K)          20974 |@@@@@@@@@@@@@@@@@@@                                 |
[1K, 2K)           34234 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@                     |
[2K, 4K)            3266 |@@@                                                 |
[4K, 8K)              19 |                                                    |

v5:
- Updated experiment data with 'SO_PREFER_BUSY_POLL' usage as suggested.
- Sent 'Add support to set napi threaded for individual napi' separately. This series depends on top of that patch.
https://lore.kernel.org/netdev/[email protected]/
- Added a separate patch to use enum for napi threaded state. Updated the nl_netdev python test.
- Using "write all" semantics when napi settings set at device level. This aligns with already existing behaviour for other settings.
- Fix comments to make them kdoc compatible.
- Updated Documentation/networking/net_cachelines/net_device.rst
- Updated the missed gro_flush modification in napi_complete_done

v4:
- Using AF_XDP based benchmark for experiments.
- Re-enable dev level napi threaded busypoll after soft reset.

v3:
- Fixed calls to dev_set_threaded in drivers

v2:
- Add documentation in napi.rst.
- Provide experiment data and usecase details.
- Update busy_poller selftest to include napi threaded poll testcase.
- Define threaded mode enum in netlink interface.
- Included NAPI threaded state in napi config to save/restore.

Samiullah Khawaja (4):
net: Create separate gro_flush helper function
net: define an enum for the napi threaded state Extend napi threaded polling to allow kthread based busy polling
selftests: Add napi threaded busy poll test in busy_poller

 Documentation/ABI/testing/sysfs-class-net     |   3 +-
 Documentation/netlink/specs/netdev.yaml       |  14 ++-
 Documentation/networking/napi.rst             |  67 ++++++++++-
 .../networking/net_cachelines/net_device.rst  |   2 +-
 .../net/ethernet/atheros/atl1c/atl1c_main.c   |   2 +-
 drivers/net/ethernet/mellanox/mlxsw/pci.c     |   2 +-
 drivers/net/ethernet/renesas/ravb_main.c      |   2 +-
 drivers/net/wireless/ath/ath10k/snoc.c        |   2 +-
 include/linux/netdevice.h                     |  15 ++-
 include/uapi/linux/netdev.h                   |   6 +
 net/core/dev.c                                | 110 ++++++++++++++----
 net/core/dev.h                                |  15 ++-
 net/core/net-sysfs.c                          |   2 +-
 net/core/netdev-genl-gen.c                    |   2 +-
 net/core/netdev-genl.c                        |   4 +-
 tools/include/uapi/linux/netdev.h             |   6 +
 tools/testing/selftests/net/busy_poll_test.sh |  25 +++-
 tools/testing/selftests/net/busy_poller.c     |  14 ++-
 tools/testing/selftests/net/nl_netdev.py      |  28 ++---
 19 files changed, 259 insertions(+), 62 deletions(-)

base-commit: b65999e7238e6f2a48dc77c8c2109c48318ff41b
prerequisite-patch-id: 776deffc3e807688dc224901adcb183fc59ea9d1

Signature · 1 line
2.49.0.850.g28803427d3-goog
#1 lore raw

PATCH v5 1/4 net: Create separate gro_flush helper function

Move multiple copies of same code snippet doing gro_flush and gro_normal_list into separate helper functions.

---
 net/core/dev.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
net/core/dev.c +10 -6
index ea3c8a30bb97..3ff275bbf6e2 100644
@@ -6506,6 +6506,13 @@ void __napi_schedule_irqoff(struct napi_struct *n)
 }
 EXPORT_SYMBOL(__napi_schedule_irqoff);
 
+static void __napi_gro_flush_helper(struct napi_struct *napi,
+				    bool flush_old)
+{
+	gro_flush(&napi->gro, flush_old);
+	gro_normal_list(&napi->gro);
+}
+
 bool napi_complete_done(struct napi_struct *n, int work_done)
 {
 	unsigned long flags, val, new, timeout = 0;
@@ -6538,8 +6545,7 @@ bool napi_complete_done(struct napi_struct *n, int work_done)
 	 * it, we need to bound somehow the time packets are kept in
 	 * the GRO layer.
 	 */
-	gro_flush(&n->gro, !!timeout);
-	gro_normal_list(&n->gro);
+	__napi_gro_flush_helper(n, !!timeout);
 
 	if (unlikely(!list_empty(&n->poll_list))) {
 		/* If n->poll_list is not empty, we need to mask irqs */
@@ -6609,8 +6615,7 @@ static void __busy_poll_stop(struct napi_struct *napi, bool skip_schedule)
 	}
 
 	/* Flush too old packets. If HZ < 1000, flush all packets */
-	gro_flush(&napi->gro, HZ >= 1000);
-	gro_normal_list(&napi->gro);
+	__napi_gro_flush_helper(napi, HZ >= 1000);
 
 	clear_bit(NAPI_STATE_SCHED, &napi->state);
 }
@@ -7433,8 +7438,7 @@ static int __napi_poll(struct napi_struct *n, bool *repoll)
 	}
 
 	/* Flush too old packets. If HZ < 1000, flush all packets */
-	gro_flush(&n->gro, HZ >= 1000);
-	gro_normal_list(&n->gro);
+	__napi_gro_flush_helper(n, HZ >= 1000);
 
 	/* Some drivers may have called napi_schedule
 	 * prior to exhausting their budget.
Signature · 1 line
2.49.0.850.g28803427d3-goog
#2 lore raw

Re: [PATCH net-next v5 1/4] net: Create separate gro_flush helper function

Quoted 7 lines · Samiullah Khawaja

Move multiple copies of same code snippet doing gro_flush and gro_normal_list into separate helper functions.

---
 net/core/dev.c | 16 ++++++++++------

Looks like there's an instance in kernel/bpf/cpumap.c that can be replaced as well?

Maybe the code proposed should be moved to a helper in net/gro.h and then it can be used in net/core and kernel/bpf ?

#3 lore raw

PATCH v5 2/4 net: define an enum for the napi threaded state

In reply to Samiullah Khawaja

Instead of using '0' and '1' for napi threaded state, use an enum with 'disable' and 'enable' states, preparing for the next patch to add a new 'busy-poll-enable' state. Also update the 'threaded' field in struct net_device to u8 instead of bool.

Tested:
./tools/testing/selftests/net/nl_netdev.py TAP version 13
1..6
ok 1 nl_netdev.empty_check
ok 2 nl_netdev.lo_check
ok 3 nl_netdev.page_pool_check
ok 4 nl_netdev.napi_list_check
ok 5 nl_netdev.napi_set_threaded
ok 6 nl_netdev.nsim_rxq_reset_down
# Totals: pass:6 fail:0 xfail:0 xpass:0 skip:0 error:0

---
 Documentation/netlink/specs/netdev.yaml       | 11 +++++---
 .../networking/net_cachelines/net_device.rst  |  2 +-
 .../net/ethernet/atheros/atl1c/atl1c_main.c   |  2 +-
 drivers/net/ethernet/mellanox/mlxsw/pci.c     |  2 +-
 drivers/net/ethernet/renesas/ravb_main.c      |  2 +-
 drivers/net/wireless/ath/ath10k/snoc.c        |  2 +-
 include/linux/netdevice.h                     |  7 +++--
 include/uapi/linux/netdev.h                   |  5 ++++
 net/core/dev.c                                | 25 ++++++++++++++---
 net/core/dev.h                                | 12 +++++---
 net/core/netdev-genl-gen.c                    |  2 +-
 net/core/netdev-genl.c                        |  4 +--
 tools/include/uapi/linux/netdev.h             |  5 ++++
 tools/testing/selftests/net/nl_netdev.py      | 28 +++++++++----------
 14 files changed, 72 insertions(+), 37 deletions(-)
Documentation/netlink/specs/netdev.yaml +7 -4
index c9d190fe1f05..c8834161e8ec 100644
@@ -82,6 +82,10 @@ definitions:
     name: qstats-scope
     type: flags
     entries: [ queue ]
+  -
+    name: napi-threaded
+    type: enum
+    entries: [ disable, enable ]
 
 attribute-sets:
   -
@@ -283,11 +287,10 @@ attribute-sets:
       -
         name: threaded
         doc: Whether the napi is configured to operate in threaded polling
-             mode. If this is set to `1` then the NAPI context operates
+             mode. If this is set to `enable` then the NAPI context operates
              in threaded polling mode.
-        type: uint
-        checks:
-          max: 1
+        type: u32
+        enum: napi-threaded
   -
     name: xsk-info
     attributes: []
Documentation/networking/net_cachelines/net_device.rst +1 -1
index 6327e689e8a8..e9796adc2fb0 100644
@@ -164,7 +164,7 @@ struct sfp_bus*                     sfp_bus
 struct lock_class_key*              qdisc_tx_busylock
 bool                                proto_down
 unsigned:1                          wol_enabled
-unsigned:1                          threaded                                                            napi_poll(napi_enable,dev_set_threaded)
+u8                                  threaded                                                            napi_poll(napi_enable,dev_set_threaded)
 unsigned_long:1                     see_all_hwtstamp_requests
 unsigned_long:1                     change_proto_down
 unsigned_long:1                     netns_immutable
drivers/net/ethernet/atheros/atl1c/atl1c_main.c +1 -1
index 82137f9deae9..58254113678c 100644
@@ -2688,7 +2688,7 @@ static int atl1c_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 	adapter->mii.mdio_write = atl1c_mdio_write;
 	adapter->mii.phy_id_mask = 0x1f;
 	adapter->mii.reg_num_mask = MDIO_CTRL_REG_MASK;
-	dev_set_threaded(netdev, true);
+	dev_set_threaded(netdev, NETDEV_NAPI_THREADED_ENABLE);
 	for (i = 0; i < adapter->rx_queue_count; ++i)
 		netif_napi_add(netdev, &adapter->rrd_ring[i].napi,
 			       atl1c_clean_rx);
drivers/net/ethernet/mellanox/mlxsw/pci.c +1 -1
index 058dcabfaa2e..2ed3b9263be2 100644
@@ -156,7 +156,7 @@ static int mlxsw_pci_napi_devs_init(struct mlxsw_pci *mlxsw_pci)
 	}
 	strscpy(mlxsw_pci->napi_dev_rx->name, "mlxsw_rx",
 		sizeof(mlxsw_pci->napi_dev_rx->name));
-	dev_set_threaded(mlxsw_pci->napi_dev_rx, true);
+	dev_set_threaded(mlxsw_pci->napi_dev_rx, NETDEV_NAPI_THREADED_ENABLE);
 
 	return 0;
 
drivers/net/ethernet/renesas/ravb_main.c +1 -1
index c9f4976a3527..12e4f68c0c8f 100644
@@ -3075,7 +3075,7 @@ static int ravb_probe(struct platform_device *pdev)
 	if (info->coalesce_irqs) {
 		netdev_sw_irq_coalesce_default_on(ndev);
 		if (num_present_cpus() == 1)
-			dev_set_threaded(ndev, true);
+			dev_set_threaded(ndev, NETDEV_NAPI_THREADED_ENABLE);
 	}
 
 	/* Network device register */
drivers/net/wireless/ath/ath10k/snoc.c +1 -1
index 866bad2db334..902517bed686 100644
@@ -935,7 +935,7 @@ static int ath10k_snoc_hif_start(struct ath10k *ar)
 
 	bitmap_clear(ar_snoc->pending_ce_irqs, 0, CE_COUNT_MAX);
 
-	dev_set_threaded(ar->napi_dev, true);
+	dev_set_threaded(ar->napi_dev, NETDEV_NAPI_THREADED_ENABLE);
 	ath10k_core_napi_enable(ar);
 	ath10k_snoc_irq_enable(ar);
 	ath10k_snoc_rx_post(ar);
include/linux/netdevice.h +4 -3
index 3817720e8b24..2eda563307f9 100644
@@ -369,7 +369,7 @@ struct napi_config {
 	u64 irq_suspend_timeout;
 	u32 defer_hard_irqs;
 	cpumask_t affinity_mask;
-	bool threaded;
+	u8 threaded;
 	unsigned int napi_id;
 };
 
@@ -589,7 +589,8 @@ static inline bool napi_complete(struct napi_struct *n)
 	return napi_complete_done(n, 0);
 }
 
-int dev_set_threaded(struct net_device *dev, bool threaded);
+int dev_set_threaded(struct net_device *dev,
+		     enum netdev_napi_threaded threaded);
 
 void napi_disable(struct napi_struct *n);
 void napi_disable_locked(struct napi_struct *n);
@@ -2428,7 +2429,7 @@ struct net_device {
 	struct sfp_bus		*sfp_bus;
 	struct lock_class_key	*qdisc_tx_busylock;
 	bool			proto_down;
-	bool			threaded;
+	u8			threaded;
 	bool			irq_affinity_auto;
 	bool			rx_cpu_rmap_auto;
 
include/uapi/linux/netdev.h +5 -0
index fac1b8ffeb55..a5737572ce92 100644
@@ -77,6 +77,11 @@ enum netdev_qstats_scope {
 	NETDEV_QSTATS_SCOPE_QUEUE = 1,
 };
 
+enum netdev_napi_threaded {
+	NETDEV_NAPI_THREADED_DISABLE,
+	NETDEV_NAPI_THREADED_ENABLE,
+};
+
 enum {
 	NETDEV_A_DEV_IFINDEX = 1,
 	NETDEV_A_DEV_PAD,
net/core/dev.c +21 -4
index 3ff275bbf6e2..41d809f2a7f7 100644
@@ -6893,7 +6893,19 @@ static enum hrtimer_restart napi_watchdog(struct hrtimer *timer)
 	return HRTIMER_NORESTART;
 }
 
-int napi_set_threaded(struct napi_struct *napi, bool threaded)
+static void napi_set_threaded_state(struct napi_struct *napi,
+				    enum netdev_napi_threaded threaded)
+{
+	unsigned long val;
+
+	val = 0;
+	if (threaded)
+		val |= NAPIF_STATE_THREADED;
+	set_mask_bits(&napi->state, NAPIF_STATE_THREADED, val);
+}
+
+int napi_set_threaded(struct napi_struct *napi,
+		      enum netdev_napi_threaded threaded)
 {
 	if (threaded) {
 		if (!napi->thread) {
@@ -6909,14 +6921,16 @@ int napi_set_threaded(struct napi_struct *napi, bool threaded)
 
 	/* Make sure kthread is created before THREADED bit is set. */
 	smp_mb__before_atomic();
-	assign_bit(NAPI_STATE_THREADED, &napi->state, threaded);
+	napi_set_threaded_state(napi, threaded);
 
 	return 0;
 }
 
-int dev_set_threaded(struct net_device *dev, bool threaded)
+int dev_set_threaded(struct net_device *dev,
+		     enum netdev_napi_threaded threaded)
 {
 	struct napi_struct *napi;
+	unsigned long val;
 	int err = 0;
 
 	netdev_assert_locked_or_invisible(dev);
@@ -6924,12 +6938,15 @@ int dev_set_threaded(struct net_device *dev, bool threaded)
 	if (dev->threaded == threaded)
 		return 0;
 
+	val = 0;
 	if (threaded) {
+		val |= NAPIF_STATE_THREADED;
+
 		list_for_each_entry(napi, &dev->napi_list, dev_list) {
 			if (!napi->thread) {
 				err = napi_kthread_create(napi);
 				if (err) {
-					threaded = false;
+					threaded = NETDEV_NAPI_THREADED_DISABLE;
 					break;
 				}
 			}
net/core/dev.h +8 -4
index b50d118ad014..3924996ae85c 100644
@@ -321,19 +321,23 @@ static inline void napi_set_irq_suspend_timeout(struct napi_struct *n,
  *
  * Return: the per-NAPI threaded state.
  */
-static inline bool napi_get_threaded(struct napi_struct *n)
+static inline enum netdev_napi_threaded napi_get_threaded(struct napi_struct *n)
 {
-	return test_bit(NAPI_STATE_THREADED, &n->state);
+	if (test_bit(NAPI_STATE_THREADED, &n->state))
+		return NETDEV_NAPI_THREADED_ENABLE;
+
+	return NETDEV_NAPI_THREADED_DISABLE;
 }
 
 /**
  * napi_set_threaded - set napi threaded state
  * @n: napi struct to set the threaded state on
- * @threaded: whether this napi does threaded polling
+ * @threaded: napi threaded state
  *
  * Return 0 on success and negative errno on failure.
  */
-int napi_set_threaded(struct napi_struct *n, bool threaded);
+int napi_set_threaded(struct napi_struct *n,
+		      enum netdev_napi_threaded threaded);
 
 int rps_cpumask_housekeeping(struct cpumask *mask);
 
net/core/netdev-genl-gen.c +1 -1
index 2791b6b232fa..c2e5cee857d2 100644
@@ -97,7 +97,7 @@ static const struct nla_policy netdev_napi_set_nl_policy[NETDEV_A_NAPI_THREADED
 	[NETDEV_A_NAPI_DEFER_HARD_IRQS] = NLA_POLICY_FULL_RANGE(NLA_U32, &netdev_a_napi_defer_hard_irqs_range),
 	[NETDEV_A_NAPI_GRO_FLUSH_TIMEOUT] = { .type = NLA_UINT, },
 	[NETDEV_A_NAPI_IRQ_SUSPEND_TIMEOUT] = { .type = NLA_UINT, },
-	[NETDEV_A_NAPI_THREADED] = NLA_POLICY_MAX(NLA_UINT, 1),
+	[NETDEV_A_NAPI_THREADED] = NLA_POLICY_MAX(NLA_U32, 1),
 };
 
 /* Ops table for netdev */
net/core/netdev-genl.c +2 -2
index f7d000a600cf..7335c8f6ef68 100644
@@ -330,8 +330,8 @@ netdev_nl_napi_set_config(struct napi_struct *napi, struct genl_info *info)
 	u32 defer = 0;
 
 	if (info->attrs[NETDEV_A_NAPI_THREADED]) {
-		threaded = nla_get_uint(info->attrs[NETDEV_A_NAPI_THREADED]);
-		napi_set_threaded(napi, !!threaded);
+		threaded = nla_get_u32(info->attrs[NETDEV_A_NAPI_THREADED]);
+		napi_set_threaded(napi, threaded);
 	}
 
 	if (info->attrs[NETDEV_A_NAPI_DEFER_HARD_IRQS]) {
tools/include/uapi/linux/netdev.h +5 -0
index fac1b8ffeb55..a5737572ce92 100644
@@ -77,6 +77,11 @@ enum netdev_qstats_scope {
 	NETDEV_QSTATS_SCOPE_QUEUE = 1,
 };
 
+enum netdev_napi_threaded {
+	NETDEV_NAPI_THREADED_DISABLE,
+	NETDEV_NAPI_THREADED_ENABLE,
+};
+
 enum {
 	NETDEV_A_DEV_IFINDEX = 1,
 	NETDEV_A_DEV_PAD,
tools/testing/selftests/net/nl_netdev.py +14 -14
index 505564818fa8..ec60bff34f5e 100755
@@ -52,53 +52,53 @@ def napi_set_threaded(nf) -> None:
         napi1_id = napis[1]['id']
 
         # set napi threaded and verify
-        nf.napi_set({'id': napi0_id, 'threaded': 1})
+        nf.napi_set({'id': napi0_id, 'threaded': "enable"})
         napi0 = nf.napi_get({'id': napi0_id})
-        ksft_eq(napi0['threaded'], 1)
+        ksft_eq(napi0['threaded'], "enable")
 
         ip(f"link set dev {nsim.ifname} down")
         ip(f"link set dev {nsim.ifname} up")
 
         # verify if napi threaded is still set
         napi0 = nf.napi_get({'id': napi0_id})
-        ksft_eq(napi0['threaded'], 1)
+        ksft_eq(napi0['threaded'], "enable")
 
         # unset napi threaded and verify
-        nf.napi_set({'id': napi0_id, 'threaded': 0})
+        nf.napi_set({'id': napi0_id, 'threaded': "disable"})
         napi0 = nf.napi_get({'id': napi0_id})
-        ksft_eq(napi0['threaded'], 0)
+        ksft_eq(napi0['threaded'], "disable")
 
         # set napi threaded for napi0
-        nf.napi_set({'id': napi0_id, 'threaded': 1})
+        nf.napi_set({'id': napi0_id, 'threaded': "enable"})
         napi0 = nf.napi_get({'id': napi0_id})
-        ksft_eq(napi0['threaded'], 1)
+        ksft_eq(napi0['threaded'], "enable")
 
         # check it is not set for napi1
         napi1 = nf.napi_get({'id': napi1_id})
-        ksft_eq(napi1['threaded'], 0)
+        ksft_eq(napi1['threaded'], "disable")
 
         # set threaded at device level
         system(f"echo 1 > /sys/class/net/{nsim.ifname}/threaded")
 
         # check napi threaded is set for both napis
         napi0 = nf.napi_get({'id': napi0_id})
-        ksft_eq(napi0['threaded'], 1)
+        ksft_eq(napi0['threaded'], "enable")
         napi1 = nf.napi_get({'id': napi1_id})
-        ksft_eq(napi1['threaded'], 1)
+        ksft_eq(napi1['threaded'], "enable")
 
         # set napi threaded for napi0
-        nf.napi_set({'id': napi0_id, 'threaded': 1})
+        nf.napi_set({'id': napi0_id, 'threaded': "enable"})
         napi0 = nf.napi_get({'id': napi0_id})
-        ksft_eq(napi0['threaded'], 1)
+        ksft_eq(napi0['threaded'], "enable")
 
         # unset threaded at device level
         system(f"echo 0 > /sys/class/net/{nsim.ifname}/threaded")
 
         # check napi threaded is unset for both napis
         napi0 = nf.napi_get({'id': napi0_id})
-        ksft_eq(napi0['threaded'], 0)
+        ksft_eq(napi0['threaded'], "disable")
         napi1 = nf.napi_get({'id': napi1_id})
-        ksft_eq(napi1['threaded'], 0)
+        ksft_eq(napi1['threaded'], "disable")
 
 def nsim_rxq_reset_down(nf) -> None:
     """
Signature · 1 line
2.49.0.850.g28803427d3-goog
#4 lore raw

Re: [PATCH net-next v5 2/4] net: define an enum for the napi threaded state

Quoted 4 lines · Samiullah Khawaja

Instead of using '0' and '1' for napi threaded state, use an enum with 'disable' and 'enable' states, preparing for the next patch to add a new 'busy-poll-enable' state. Also update the 'threaded' field in struct net_device to u8 instead of bool.

[...]

Quoted 5 lines
Documentation/netlink/specs/netdev.yaml +0 -0
index c9d190fe1f05..c8834161e8ec 100644

[...]

Documentation/netlink/specs/netdev.yaml attribute-sets: · line 293

Quoted 12 lines
Documentation/netlink/specs/netdev.yaml +3 -4
@@ -283,11 +287,10 @@ attribute-sets:
       -
         name: threaded
         doc: Whether the napi is configured to operate in threaded polling
-             mode. If this is set to `1` then the NAPI context operates
+             mode. If this is set to `enable` then the NAPI context operates
              in threaded polling mode.
-        type: uint
-        checks:
-          max: 1
+        type: u32
+        enum: napi-threaded

I think this can still be uint even if associated with an enum? IIUC (I could be wrong) uint is preferred.

include/linux/netdevice.h struct net_device { · line 2432

Quoted 11 lines
include/linux/netdevice.h +1 -1
index 3817720e8b24..2eda563307f9 100644
@@ -2428,7 +2429,7 @@ struct net_device {
 	struct sfp_bus		*sfp_bus;
 	struct lock_class_key	*qdisc_tx_busylock;
 	bool			proto_down;
-	bool			threaded;
+	u8			threaded;

Looks like there's a 1 byte hole in a cacheline further up after

unsigned char lower_level

Not sure if putting the u8 there and filling that cacheline makes you feel anything in particular.

(I feel nothing.)

Quoted 4 lines
net/core/dev.c +0 -0
index 3ff275bbf6e2..41d809f2a7f7 100644

[...]

Quoted 7 lines
net/core/dev.c +2 -0
@@ -6924,12 +6938,15 @@ int dev_set_threaded(struct net_device *dev, bool threaded)
 	if (dev->threaded == threaded)
 		return 0;
 
+	val = 0;
 	if (threaded) {
+		val |= NAPIF_STATE_THREADED;

net/core/dev.c int dev_set_threaded(struct net_device *dev, bool threaded) · line 6952

Quoted 10 lines
net/core/dev.c +2 -1
+
 		list_for_each_entry(napi, &dev->napi_list, dev_list) {
 			if (!napi->thread) {
 				err = napi_kthread_create(napi);
 				if (err) {
-					threaded = false;
+					threaded = NETDEV_NAPI_THREADED_DISABLE;
 					break;
 				}
 			}

Re the feedback on the per-NAPI threading setting patch, I think if you used napi_set_threaded in dev_set_threaded (as mentioned in the feedback to that patch) you might reduce the changes here.

maintainer
#5 lore raw

Re: [PATCH net-next v5 2/4] net: define an enum for the napi threaded state

In reply to Samiullah Khawaja

drivers/net/ethernet/renesas/ravb_main.c static int ravb_probe(struct platform_device *pdev) · line 3078

Quoted 2 lines · On Thu, 24 Apr 2025 20:02:20 +0000 Samiullah Khawaja wrote:
drivers/net/ethernet/renesas/ravb_main.c +1 -1
-			dev_set_threaded(ndev, true);
+			dev_set_threaded(ndev, NETDEV_NAPI_THREADED_ENABLE);

The drivers having to specify the type of threading is too much. The drivers are just indicating to the core that they are too IRQ-constrained to reasonably handle load..

Please add a wrapper, something like dev_set_threaded_hint(netdev). All the drivers pass true now, the second argument is already pointless. For extra points you can export just the dev_set_threaded_hint and make dev_set_threaded() be a core-only function.

#6 lore raw

Re: [PATCH net-next v5 2/4] net: define an enum for the napi threaded state

Quoted 5 lines · Jakub Kicinski

drivers/net/ethernet/renesas/ravb_main.c static int ravb_probe(struct platform_device *pdev) · line 3078

Quoted 2 lines · On Thu, 24 Apr 2025 20:02:20 +0000 Samiullah Khawaja wrote:
drivers/net/ethernet/renesas/ravb_main.c +1 -1
-                     dev_set_threaded(ndev, true);
+                     dev_set_threaded(ndev, NETDEV_NAPI_THREADED_ENABLE);
Quoted 8 lines

The drivers having to specify the type of threading is too much. The drivers are just indicating to the core that they are too IRQ-constrained to reasonably handle load..

Please add a wrapper, something like dev_set_threaded_hint(netdev). All the drivers pass true now, the second argument is already pointless. For extra points you can export just the dev_set_threaded_hint and make dev_set_threaded() be a core-only function.

Agree. Will do this

#7 lore raw

PATCH v5 3/4 Extend napi threaded polling to allow kthread based busy polling

In reply to Samiullah Khawaja

Add a new state to napi state enum:

- STATE_THREADED_BUSY_POLL
Threaded busy poll is enabled/running for this napi.

Following changes are introduced in the napi scheduling and state logic:

- When threaded busy poll is enabled through sysfs it also enables NAPI_STATE_THREADED so a kthread is created per napi. It also sets NAPI_STATE_THREADED_BUSY_POLL bit on each napi to indicate that we are supposed to busy poll for each napi.

- When napi is scheduled with STATE_SCHED_THREADED and associated kthread is woken up, the kthread owns the context. If NAPI_STATE_THREADED_BUSY_POLL and NAPI_SCHED_THREADED both are set then it means that we can busy poll.

- To keep busy polling and to avoid scheduling of the interrupts, the napi_complete_done returns false when both SCHED_THREADED and THREADED_BUSY_POLL flags are set. Also napi_complete_done returns early to avoid the STATE_SCHED_THREADED being unset.

- If at any point STATE_THREADED_BUSY_POLL is unset, the napi_complete_done will run and unset the SCHED_THREADED bit also. This will make the associated kthread go to sleep as per existing logic.

---
 Documentation/ABI/testing/sysfs-class-net |  3 +-
 Documentation/netlink/specs/netdev.yaml   |  5 +-
 Documentation/networking/napi.rst         | 67 ++++++++++++++++++++-
 include/linux/netdevice.h                 |  8 +++
 include/uapi/linux/netdev.h               |  1 +
 net/core/dev.c                            | 71 +++++++++++++++++++----
 net/core/dev.h                            |  3 +
 net/core/net-sysfs.c                      |  2 +-
 net/core/netdev-genl-gen.c                |  2 +-
 tools/include/uapi/linux/netdev.h         |  1 +
 10 files changed, 145 insertions(+), 18 deletions(-)
Documentation/ABI/testing/sysfs-class-net +2 -1
index ebf21beba846..15d7d36a8294 100644
@@ -343,7 +343,7 @@ Date:		Jan 2021
 KernelVersion:	5.12
 Contact:	[email protected]
 Description:
-		Boolean value to control the threaded mode per device. User could
+		Integer value to control the threaded mode per device. User could
 		set this value to enable/disable threaded mode for all napi
 		belonging to this device, without the need to do device up/down.
 
@@ -351,4 +351,5 @@ Description:
 		== ==================================
 		0  threaded mode disabled for this dev
 		1  threaded mode enabled for this dev
+		2  threaded mode enabled, and busy polling enabled.
 		== ==================================
Documentation/netlink/specs/netdev.yaml +3 -2
index c8834161e8ec..650179559558 100644
@@ -85,7 +85,7 @@ definitions:
   -
     name: napi-threaded
     type: enum
-    entries: [ disable, enable ]
+    entries: [ disable, enable, busy-poll-enable ]
 
 attribute-sets:
   -
@@ -288,7 +288,8 @@ attribute-sets:
         name: threaded
         doc: Whether the napi is configured to operate in threaded polling
              mode. If this is set to `enable` then the NAPI context operates
-             in threaded polling mode.
+             in threaded polling mode. If this is set to `busy-poll-enable`
+             then the NAPI kthread also does busypolling.
         type: u32
         enum: napi-threaded
   -
Documentation/networking/napi.rst +66 -1

79 lines not on this page · raw message

include/linux/netdevice.h +8 -0
index 2eda563307f9..c67a7424605e 100644
@@ -427,6 +427,8 @@ enum {
 	NAPI_STATE_THREADED,		/* The poll is performed inside its own thread*/
 	NAPI_STATE_SCHED_THREADED,	/* Napi is currently scheduled in threaded mode */
 	NAPI_STATE_HAS_NOTIFIER,	/* Napi has an IRQ notifier */
+	NAPI_STATE_THREADED_BUSY_POLL,	/* The threaded napi poller will busy poll */
+	NAPI_STATE_SCHED_THREADED_BUSY_POLL,  /* The threaded napi poller is busy polling */
 };
 
 enum {
@@ -441,8 +443,14 @@ enum {
 	NAPIF_STATE_THREADED		= BIT(NAPI_STATE_THREADED),
 	NAPIF_STATE_SCHED_THREADED	= BIT(NAPI_STATE_SCHED_THREADED),
 	NAPIF_STATE_HAS_NOTIFIER	= BIT(NAPI_STATE_HAS_NOTIFIER),
+	NAPIF_STATE_THREADED_BUSY_POLL	= BIT(NAPI_STATE_THREADED_BUSY_POLL),
+	NAPIF_STATE_SCHED_THREADED_BUSY_POLL  =
+			BIT(NAPI_STATE_SCHED_THREADED_BUSY_POLL),
 };
 
+#define NAPIF_STATE_THREADED_BUSY_POLL_MASK \
+	(NAPIF_STATE_THREADED | NAPIF_STATE_THREADED_BUSY_POLL)
+
 enum gro_result {
 	GRO_MERGED,
 	GRO_MERGED_FREE,
include/uapi/linux/netdev.h +1 -0
index a5737572ce92..b9b59d60957f 100644
@@ -80,6 +80,7 @@ enum netdev_qstats_scope {
 enum netdev_napi_threaded {
 	NETDEV_NAPI_THREADED_DISABLE,
 	NETDEV_NAPI_THREADED_ENABLE,
+	NETDEV_NAPI_THREADED_BUSY_POLL_ENABLE,
 };
 
 enum {
net/core/dev.c +59 -12

154 lines not on this page · raw message

net/core/dev.h +3 -0
index 3924996ae85c..bd9d26b4a6ba 100644
@@ -323,6 +323,9 @@ static inline void napi_set_irq_suspend_timeout(struct napi_struct *n,
  */
 static inline enum netdev_napi_threaded napi_get_threaded(struct napi_struct *n)
 {
+	if (test_bit(NAPI_STATE_THREADED_BUSY_POLL, &n->state))
+		return NETDEV_NAPI_THREADED_BUSY_POLL_ENABLE;
+
 	if (test_bit(NAPI_STATE_THREADED, &n->state))
 		return NETDEV_NAPI_THREADED_ENABLE;
 
net/core/net-sysfs.c +1 -1
index 1ace0cd01adc..0b7624236896 100644
@@ -741,7 +741,7 @@ static int modify_napi_threaded(struct net_device *dev, unsigned long val)
 	if (list_empty(&dev->napi_list))
 		return -EOPNOTSUPP;
 
-	if (val != 0 && val != 1)
+	if (val > NETDEV_NAPI_THREADED_BUSY_POLL_ENABLE)
 		return -EOPNOTSUPP;
 
 	ret = dev_set_threaded(dev, val);
net/core/netdev-genl-gen.c +1 -1
index c2e5cee857d2..1dbe5f19a192 100644
@@ -97,7 +97,7 @@ static const struct nla_policy netdev_napi_set_nl_policy[NETDEV_A_NAPI_THREADED
 	[NETDEV_A_NAPI_DEFER_HARD_IRQS] = NLA_POLICY_FULL_RANGE(NLA_U32, &netdev_a_napi_defer_hard_irqs_range),
 	[NETDEV_A_NAPI_GRO_FLUSH_TIMEOUT] = { .type = NLA_UINT, },
 	[NETDEV_A_NAPI_IRQ_SUSPEND_TIMEOUT] = { .type = NLA_UINT, },
-	[NETDEV_A_NAPI_THREADED] = NLA_POLICY_MAX(NLA_U32, 1),
+	[NETDEV_A_NAPI_THREADED] = NLA_POLICY_MAX(NLA_U32, 2),
 };
 
 /* Ops table for netdev */
tools/include/uapi/linux/netdev.h +1 -0
index a5737572ce92..b9b59d60957f 100644
@@ -80,6 +80,7 @@ enum netdev_qstats_scope {
 enum netdev_napi_threaded {
 	NETDEV_NAPI_THREADED_DISABLE,
 	NETDEV_NAPI_THREADED_ENABLE,
+	NETDEV_NAPI_THREADED_BUSY_POLL_ENABLE,
 };
 
 enum {
Signature · 1 line
2.49.0.850.g28803427d3-goog
#8 lore raw

Re: [PATCH net-next v5 3/4] Extend napi threaded polling to allow kthread based busy polling

Quoted 1 line · Samiullah Khawaja

Add a new state to napi state enum:

[...]

include/linux/netdevice.h enum { · line 432

Quoted 11 lines
include/linux/netdevice.h +2 -0
index 2eda563307f9..c67a7424605e 100644
@@ -427,6 +427,8 @@ enum {
 	NAPI_STATE_THREADED,		/* The poll is performed inside its own thread*/
 	NAPI_STATE_SCHED_THREADED,	/* Napi is currently scheduled in threaded mode */
 	NAPI_STATE_HAS_NOTIFIER,	/* Napi has an IRQ notifier */
+	NAPI_STATE_THREADED_BUSY_POLL,	/* The threaded napi poller will busy poll */
+	NAPI_STATE_SCHED_THREADED_BUSY_POLL,  /* The threaded napi poller is busy polling */
 };

Now that threaded has three states, doesn't the comment for struct net_device need to be updated?

#9 lore raw

PATCH v5 4/4 selftests: Add napi threaded busy poll test in `busy_poller`

In reply to Samiullah Khawaja

Add testcase to run busy poll test with threaded napi busy poll enabled.

---
 tools/testing/selftests/net/busy_poll_test.sh | 25 ++++++++++++++++++-
 tools/testing/selftests/net/busy_poller.c     | 14 ++++++++---
 2 files changed, 35 insertions(+), 4 deletions(-)
tools/testing/selftests/net/busy_poll_test.sh +24 -1
index 7db292ec4884..aeca610dc989 100755
@@ -27,6 +27,9 @@ NAPI_DEFER_HARD_IRQS=100
 GRO_FLUSH_TIMEOUT=50000
 SUSPEND_TIMEOUT=20000000
 
+# NAPI threaded busy poll config
+NAPI_THREADED_POLL=2
+
 setup_ns()
 {
 	set -e
@@ -62,6 +65,9 @@ cleanup_ns()
 test_busypoll()
 {
 	suspend_value=${1:-0}
+	napi_threaded_value=${2:-0}
+	prefer_busy_poll_value=${3:-$PREFER_BUSY_POLL}
+
 	tmp_file=$(mktemp)
 	out_file=$(mktemp)
 
@@ -73,10 +79,11 @@ test_busypoll()
 					     -b${SERVER_IP}        \
 					     -m${MAX_EVENTS}       \
 					     -u${BUSY_POLL_USECS}  \
-					     -P${PREFER_BUSY_POLL} \
+					     -P${prefer_busy_poll_value} \
 					     -g${BUSY_POLL_BUDGET} \
 					     -i${NSIM_SV_IFIDX}    \
 					     -s${suspend_value}    \
+					     -t${napi_threaded_value} \
 					     -o${out_file}&
 
 	wait_local_port_listen nssv ${SERVER_PORT} tcp
@@ -109,6 +116,15 @@ test_busypoll_with_suspend()
 	return $?
 }
 
+test_busypoll_with_napi_threaded()
+{
+	# Only enable napi threaded poll. Set suspend timeout and prefer busy
+	# poll to 0.
+	test_busypoll 0 ${NAPI_THREADED_POLL} 0
+
+	return $?
+}
+
 ###
 ### Code start
 ###
@@ -154,6 +170,13 @@ if [ $? -ne 0 ]; then
 	exit 1
 fi
 
+test_busypoll_with_napi_threaded
+if [ $? -ne 0 ]; then
+	echo "test_busypoll_with_napi_threaded failed"
+	cleanup_ns
+	exit 1
+fi
+
 echo "$NSIM_SV_FD:$NSIM_SV_IFIDX" > $NSIM_DEV_SYS_UNLINK
 
 echo $NSIM_CL_ID > $NSIM_DEV_SYS_DEL
tools/testing/selftests/net/busy_poller.c +11 -3
index 04c7ff577bb8..f7407f09f635 100644
@@ -65,15 +65,16 @@ static uint32_t cfg_busy_poll_usecs;
 static uint16_t cfg_busy_poll_budget;
 static uint8_t cfg_prefer_busy_poll;
 
-/* IRQ params */
+/* NAPI params */
 static uint32_t cfg_defer_hard_irqs;
 static uint64_t cfg_gro_flush_timeout;
 static uint64_t cfg_irq_suspend_timeout;
+static enum netdev_napi_threaded cfg_napi_threaded_poll = NETDEV_NAPI_THREADED_DISABLE;
 
 static void usage(const char *filepath)
 {
 	error(1, 0,
-	      "Usage: %s -p<port> -b<addr> -m<max_events> -u<busy_poll_usecs> -P<prefer_busy_poll> -g<busy_poll_budget> -o<outfile> -d<defer_hard_irqs> -r<gro_flush_timeout> -s<irq_suspend_timeout> -i<ifindex>",
+	      "Usage: %s -p<port> -b<addr> -m<max_events> -u<busy_poll_usecs> -P<prefer_busy_poll> -g<busy_poll_budget> -o<outfile> -d<defer_hard_irqs> -r<gro_flush_timeout> -s<irq_suspend_timeout> -t<napi_threaded_poll> -i<ifindex>",
 	      filepath);
 }
 
@@ -86,7 +87,7 @@ static void parse_opts(int argc, char **argv)
 	if (argc <= 1)
 		usage(argv[0]);
 
-	while ((c = getopt(argc, argv, "p:m:b:u:P:g:o:d:r:s:i:")) != -1) {
+	while ((c = getopt(argc, argv, "p:m:b:u:P:g:o:d:r:s:i:t:")) != -1) {
 		/* most options take integer values, except o and b, so reduce
 		 * code duplication a bit for the common case by calling
 		 * strtoull here and leave bounds checking and casting per
@@ -168,6 +169,12 @@ static void parse_opts(int argc, char **argv)
 
 			cfg_ifindex = (int)tmp;
 			break;
+		case 't':
+			if (tmp == ULLONG_MAX || tmp > 2)
+				error(1, ERANGE, "napi threaded poll value must be 0-2");
+
+			cfg_napi_threaded_poll = (enum netdev_napi_threaded)tmp;
+			break;
 		}
 	}
 
@@ -246,6 +253,7 @@ static void setup_queue(void)
 						  cfg_gro_flush_timeout);
 	netdev_napi_set_req_set_irq_suspend_timeout(set_req,
 						    cfg_irq_suspend_timeout);
+	netdev_napi_set_req_set_threaded(set_req, cfg_napi_threaded_poll);
 
 	if (netdev_napi_set(ys, set_req))
 		error(1, 0, "can't set NAPI params: %s\n", yerr.msg);
Signature · 1 line
2.49.0.850.g28803427d3-goog
#10 lore raw
In reply to Samiullah Khawaja
Quoted 223 lines · Samiullah Khawaja

Extend the already existing support of threaded napi poll to do continuous busy polling.

This is used for doing continuous polling of napi to fetch descriptors from backing RX/TX queues for low latency applications. Allow enabling of threaded busypoll using netlink so this can be enabled on a set of dedicated napis for low latency applications.

Once enabled user can fetch the PID of the kthread doing NAPI polling and set affinity, priority and scheduler for it depending on the low-latency requirements.

Currently threaded napi is only enabled at device level using sysfs. Add support to enable/disable threaded mode for a napi individually. This can be done using the netlink interface. Extend napi-set op in netlink spec that allows setting the threaded attribute of a napi.

Extend the threaded attribute in napi struct to add an option to enable continuous busy polling. Extend the netlink and sysfs interface to allow enabling/disabling threaded busypolling at device or individual napi level.

We use this for our AF_XDP based hard low-latency usecase with usecs level latency requirement. For our usecase we want low jitter and stable latency at P99.

Following is an analysis and comparison of available (and compatible) busy poll interfaces for a low latency usecase with stable P99. Please note that the throughput and cpu efficiency is a non-goal.

For analysis we use an AF_XDP based benchmarking tool xdp_rr. The description of the tool and how it tries to simulate the real workload is following,

- It sends UDP packets between 2 machines.
- The client machine sends packets at a fixed frequency. To maintain the frequency of the packet being sent, we use open-loop sampling. That is the packets are sent in a separate thread.
- The server replies to the packet inline by reading the pkt from the recv ring and replies using the tx ring.
- To simulate the application processing time, we use a configurable delay in usecs on the client side after a reply is received from the server.

The xdp_rr tool is posted separately as an RFC for tools/testing/selftest.

We use this tool with following napi polling configurations,

- Interrupts only
- SO_BUSYPOLL (inline in the same thread where the client receives the packet).
- SO_BUSYPOLL (separate thread and separate core)
- Threaded NAPI busypoll

System is configured using following script in all 4 cases,

```
echo 0 | sudo tee /sys/class/net/eth0/threaded
echo 0 | sudo tee /proc/sys/kernel/timer_migration
echo off | sudo tee  /sys/devices/system/cpu/smt/control

sudo ethtool -L eth0 rx 1 tx 1
sudo ethtool -G eth0 rx 1024

echo 0 | sudo tee /proc/sys/net/core/rps_sock_flow_entries
echo 0 | sudo tee /sys/class/net/eth0/queues/rx-0/rps_cpus

  # pin IRQs on CPU 2
IRQS="$(gawk '/eth0-(TxRx-)?1/ {match($1, /([0-9]+)/, arr); \
				print arr[0]}' < /proc/interrupts)"
for irq in "${IRQS}"; \
	do echo 2 | sudo tee /proc/irq/$irq/smp_affinity_list; done

echo -1 | sudo tee /proc/sys/kernel/sched_rt_runtime_us

for i in /sys/devices/virtual/workqueue/*/cpumask; \
			do echo $i; echo 1,2,3,4,5,6 > $i; done

if [[ -z "$1" ]]; then
   echo 400 | sudo tee /proc/sys/net/core/busy_read
   echo 100 | sudo tee /sys/class/net/eth0/napi_defer_hard_irqs
   echo 15000   | sudo tee /sys/class/net/eth0/gro_flush_timeout
fi

sudo ethtool -C eth0 adaptive-rx off adaptive-tx off rx-usecs 0 tx-usecs 0

if [[ "$1" == "enable_threaded" ]]; then
   echo 0 | sudo tee /proc/sys/net/core/busy_poll
   echo 0 | sudo tee /proc/sys/net/core/busy_read
   echo 100 | sudo tee /sys/class/net/eth0/napi_defer_hard_irqs
   echo 15000 | sudo tee /sys/class/net/eth0/gro_flush_timeout
   echo 2 | sudo tee /sys/class/net/eth0/threaded
   NAPI_T=$(ps -ef | grep napi | grep -v grep | awk '{ print $2 }')
   sudo chrt -f  -p 50 $NAPI_T

   # pin threaded poll thread to CPU 2
   sudo taskset -pc 2 $NAPI_T
fi

if [[ "$1" == "enable_interrupt" ]]; then
   echo 0 | sudo tee /proc/sys/net/core/busy_read
   echo 0 | sudo tee /sys/class/net/eth0/napi_defer_hard_irqs
   echo 15000 | sudo tee /sys/class/net/eth0/gro_flush_timeout
fi
```

To enable various configurations, script can be run as following,

- Interrupt Only

   ```
   <script> enable_interrupt
   ```

- SO_BUSYPOLL (no arguments to script)

   ```
   <script>
   ```

- NAPI threaded busypoll

   ```
   <script> enable_threaded
   ```

If using idpf, the script needs to be run again after launching the workload just to make sure that the configurations are not reverted. As idpf reverts some configurations on software reset when AF_XDP program is attached.

Once configured, the workload is run with various configurations using following commands. Set period (1/frequency) and delay in usecs to produce results for packet frequency and application processing delay.

## Interrupt Only and SO_BUSY_POLL (inline)

- Server

```
sudo chrt -f 50 taskset -c 3-5 ./xsk_rr -o 0 -B 400 -i eth0 -4 \
	-D <IP-dest> -S <IP-src> -M <MAC-dst> -m <MAC-src> -p 54321 -h -v
```

- Client

```
sudo chrt -f 50 taskset -c 3-5 ./xsk_rr -o 0 -B 400 -i eth0 -4 \
	-S <IP-src> -D <IP-dest> -m <MAC-src> -M <MAC-dst> -p 54321 \
	-P <Period-usecs> -d <Delay-usecs>  -T -l 1 -v
```

## SO_BUSY_POLL(done in separate core using recvfrom)

Argument -t spawns a seprate thread and continuously calls recvfrom.

- Server

```
sudo chrt -f 50 taskset -c 3-5 ./xsk_rr -o 0 -B 400 -i eth0 -4 \
	-D <IP-dest> -S <IP-src> -M <MAC-dst> -m <MAC-src> -p 54321 \
	-h -v -t
```

- Client

```
sudo chrt -f 50 taskset -c 3-5 ./xsk_rr -o 0 -B 400 -i eth0 -4 \
	-S <IP-src> -D <IP-dest> -m <MAC-src> -M <MAC-dst> -p 54321 \
	-P <Period-usecs> -d <Delay-usecs>  -T -l 1 -v -t
```

## NAPI Threaded Busy Poll

Argument -n skips the recvfrom call as there is no recv kick needed.

- Server

```
sudo chrt -f 50 taskset -c 3-5 ./xsk_rr -o 0 -B 400 -i eth0 -4 \
	-D <IP-dest> -S <IP-src> -M <MAC-dst> -m <MAC-src> -p 54321 \
	-h -v -n
```

- Client

```
sudo chrt -f 50 taskset -c 3-5 ./xsk_rr -o 0 -B 400 -i eth0 -4 \
	-S <IP-src> -D <IP-dest> -m <MAC-src> -M <MAC-dst> -p 54321 \
	-P <Period-usecs> -d <Delay-usecs>  -T -l 1 -v -n
```
| Experiment | interrupts | SO_BUSYPOLL | SO_BUSYPOLL(separate) | NAPI threaded |
|---|---|---|---|---|
| 12 Kpkt/s + 0us delay | | | | |
|  | p5: 12700 | p5: 12900 | p5: 13300 | p5: 12800 |
|  | p50: 13100 | p50: 13600 | p50: 14100 | p50: 13000 |
|  | p95: 13200 | p95: 13800 | p95: 14400 | p95: 13000 |
|  | p99: 13200 | p99: 13800 | p99: 14400 | p99: 13000 |
| 32 Kpkt/s + 30us delay | | | | |
|  | p5: 19900 | p5: 16600 | p5: 13100 | p5: 12800 |
|  | p50: 21100 | p50: 17000 | p50: 13700 | p50: 13000 |
|  | p95: 21200 | p95: 17100 | p95: 14000 | p95: 13000 |
|  | p99: 21200 | p99: 17100 | p99: 14000 | p99: 13000 |
| 125 Kpkt/s + 6us delay | | | | |
|  | p5: 14600 | p5: 17100 | p5: 13300 | p5: 12900 |
|  | p50: 15400 | p50: 17400 | p50: 13800 | p50: 13100 |
|  | p95: 15600 | p95: 17600 | p95: 14000 | p95: 13100 |
|  | p99: 15600 | p99: 17600 | p99: 14000 | p99: 13100 |
| 12 Kpkt/s + 78us delay | | | | |
|  | p5: 14100 | p5: 16700 | p5: 13200 | p5: 12600 |
|  | p50: 14300 | p50: 17100 | p50: 13900 | p50: 12800 |
|  | p95: 14300 | p95: 17200 | p95: 14200 | p95: 12800 |
|  | p99: 14300 | p99: 17200 | p99: 14200 | p99: 12800 |
| 25 Kpkt/s + 38us delay | | | | |
|  | p5: 19900 | p5: 16600 | p5: 13000 | p5: 12700 |
|  | p50: 21000 | p50: 17100 | p50: 13800 | p50: 12900 |
|  | p95: 21100 | p95: 17100 | p95: 14100 | p95: 12900 |
|  | p99: 21100 | p99: 17100 | p99: 14100 | p99: 12900 |

## Observations

- Here without application processing all the approaches give the same latency within 1usecs range and NAPI threaded gives minimum latency.
- With application processing the latency increases by 3-4usecs when doing inline polling.
- Using a dedicated core to drive napi polling keeps the latency same even with application processing. This is observed both in userspace and threaded napi (in kernel).
- Using napi threaded polling in kernel gives lower latency by 1-1.5usecs as compared to userspace driven polling in separate core.
- With application processing userspace will get the packet from recv

Quoted 8 lines

ring and spend some time doing application processing and then do napi polling. While application processing is happening a dedicated core doing napi polling can pull the packet of the NAPI RX queue and populate the AF_XDP recv ring. This means that when the application thread is done with application processing it has new packets ready to recv and process in recv ring.
- Napi threaded busy polling in the kernel with a dedicated core gives the consistent P5-P99 latency.

I've experimented with this some more. I can confirm latency savings of about 1 usec arising from busy-looping a NAPI thread on a dedicated core when compared to in-thread busy-polling. A few more comments:

1) I note that the experiment results above show that 'interrupts' is almost as fast as 'NAPI threaded' in the base case. I cannot confirm these results, because I currently only have (very) old hardware available for testing. However, these results worry me in terms of necessity of the threaded busy-polling mechanism - also see Item 4) below.

2) The experiments reported here are symmetric in that they use the same polling variant at both the client and the server. When mixing things up by combining different polling variants, it becomes clear that the latency savings are split between both ends. The total savings of 1 usec are thus a combination of 0.5 usec are either end. So the ultimate trade-off is 0.5 usec latency gain for burning 1 core.

3) I believe the savings arise from running two tight loops (separate NAPI and application) instead of one longer loop. The shorter loops likely result in better cache utilization on their respective dedicated cores (and L1 caches). However I am not sure right how to explicitly confirm this.

4) I still believe that the additional experiments with setting both delay and period are meaningless. They create corner cases where rate * delay is about 1. Nobody would run a latency-critical system at 100% load. I also note that the experiment program xsk_rr fails when trying to increase the load beyond saturation (client fails with 'xsk_rr: oustanding array full').

5) I worry that a mechanism like this might be misinterpreted as some kind of magic wand for improving performance and might end up being used in practice and cause substantial overhead without much gain. If accepted, I would hope that this will be documented very clearly and have appropriate warnings attached. Given that the patch cover letter is often used as a basis for documentation, I believe this should be spelled out in the cover letter.

With the above in mind, someone else will need to judge whether (at most) 0.5 usec for burning a core is a worthy enough trade-off to justify inclusion of this mechanism. Maybe someone else can take a closer look at the 'interrupts' variant on modern hardware.

Thanks,
Martin

#11 lore raw
Quoted 270 lines · Martin Karsten
Quoted 223 lines · Samiullah Khawaja

Extend the already existing support of threaded napi poll to do continuous busy polling.

This is used for doing continuous polling of napi to fetch descriptors from backing RX/TX queues for low latency applications. Allow enabling of threaded busypoll using netlink so this can be enabled on a set of dedicated napis for low latency applications.

Once enabled user can fetch the PID of the kthread doing NAPI polling and set affinity, priority and scheduler for it depending on the low-latency requirements.

Currently threaded napi is only enabled at device level using sysfs. Add support to enable/disable threaded mode for a napi individually. This can be done using the netlink interface. Extend napi-set op in netlink spec that allows setting the threaded attribute of a napi.

Extend the threaded attribute in napi struct to add an option to enable continuous busy polling. Extend the netlink and sysfs interface to allow enabling/disabling threaded busypolling at device or individual napi level.

We use this for our AF_XDP based hard low-latency usecase with usecs level latency requirement. For our usecase we want low jitter and stable latency at P99.

Following is an analysis and comparison of available (and compatible) busy poll interfaces for a low latency usecase with stable P99. Please note that the throughput and cpu efficiency is a non-goal.

For analysis we use an AF_XDP based benchmarking tool xdp_rr. The description of the tool and how it tries to simulate the real workload is following,

- It sends UDP packets between 2 machines.
- The client machine sends packets at a fixed frequency. To maintain the frequency of the packet being sent, we use open-loop sampling. That is the packets are sent in a separate thread.
- The server replies to the packet inline by reading the pkt from the recv ring and replies using the tx ring.
- To simulate the application processing time, we use a configurable delay in usecs on the client side after a reply is received from the server.

The xdp_rr tool is posted separately as an RFC for tools/testing/selftest.

We use this tool with following napi polling configurations,

- Interrupts only
- SO_BUSYPOLL (inline in the same thread where the client receives the packet).
- SO_BUSYPOLL (separate thread and separate core)
- Threaded NAPI busypoll

System is configured using following script in all 4 cases,

```
echo 0 | sudo tee /sys/class/net/eth0/threaded
echo 0 | sudo tee /proc/sys/kernel/timer_migration
echo off | sudo tee  /sys/devices/system/cpu/smt/control

sudo ethtool -L eth0 rx 1 tx 1
sudo ethtool -G eth0 rx 1024

echo 0 | sudo tee /proc/sys/net/core/rps_sock_flow_entries
echo 0 | sudo tee /sys/class/net/eth0/queues/rx-0/rps_cpus

  # pin IRQs on CPU 2
IRQS="$(gawk '/eth0-(TxRx-)?1/ {match($1, /([0-9]+)/, arr); \
				print arr[0]}' < /proc/interrupts)"
for irq in "${IRQS}"; \
	do echo 2 | sudo tee /proc/irq/$irq/smp_affinity_list; done

echo -1 | sudo tee /proc/sys/kernel/sched_rt_runtime_us

for i in /sys/devices/virtual/workqueue/*/cpumask; \
			do echo $i; echo 1,2,3,4,5,6 > $i; done

if [[ -z "$1" ]]; then
   echo 400 | sudo tee /proc/sys/net/core/busy_read
   echo 100 | sudo tee /sys/class/net/eth0/napi_defer_hard_irqs
   echo 15000   | sudo tee /sys/class/net/eth0/gro_flush_timeout
fi

sudo ethtool -C eth0 adaptive-rx off adaptive-tx off rx-usecs 0 tx-usecs 0

if [[ "$1" == "enable_threaded" ]]; then
   echo 0 | sudo tee /proc/sys/net/core/busy_poll
   echo 0 | sudo tee /proc/sys/net/core/busy_read
   echo 100 | sudo tee /sys/class/net/eth0/napi_defer_hard_irqs
   echo 15000 | sudo tee /sys/class/net/eth0/gro_flush_timeout
   echo 2 | sudo tee /sys/class/net/eth0/threaded
   NAPI_T=$(ps -ef | grep napi | grep -v grep | awk '{ print $2 }')
   sudo chrt -f  -p 50 $NAPI_T

   # pin threaded poll thread to CPU 2
   sudo taskset -pc 2 $NAPI_T
fi

if [[ "$1" == "enable_interrupt" ]]; then
   echo 0 | sudo tee /proc/sys/net/core/busy_read
   echo 0 | sudo tee /sys/class/net/eth0/napi_defer_hard_irqs
   echo 15000 | sudo tee /sys/class/net/eth0/gro_flush_timeout
fi
```

To enable various configurations, script can be run as following,

- Interrupt Only

   ```
   <script> enable_interrupt
   ```

- SO_BUSYPOLL (no arguments to script)

   ```
   <script>
   ```

- NAPI threaded busypoll

   ```
   <script> enable_threaded
   ```

If using idpf, the script needs to be run again after launching the workload just to make sure that the configurations are not reverted. As idpf reverts some configurations on software reset when AF_XDP program is attached.

Once configured, the workload is run with various configurations using following commands. Set period (1/frequency) and delay in usecs to produce results for packet frequency and application processing delay.

## Interrupt Only and SO_BUSY_POLL (inline)

- Server

```
sudo chrt -f 50 taskset -c 3-5 ./xsk_rr -o 0 -B 400 -i eth0 -4 \
	-D <IP-dest> -S <IP-src> -M <MAC-dst> -m <MAC-src> -p 54321 -h -v
```

- Client

```
sudo chrt -f 50 taskset -c 3-5 ./xsk_rr -o 0 -B 400 -i eth0 -4 \
	-S <IP-src> -D <IP-dest> -m <MAC-src> -M <MAC-dst> -p 54321 \
	-P <Period-usecs> -d <Delay-usecs>  -T -l 1 -v
```

## SO_BUSY_POLL(done in separate core using recvfrom)

Argument -t spawns a seprate thread and continuously calls recvfrom.

- Server

```
sudo chrt -f 50 taskset -c 3-5 ./xsk_rr -o 0 -B 400 -i eth0 -4 \
	-D <IP-dest> -S <IP-src> -M <MAC-dst> -m <MAC-src> -p 54321 \
	-h -v -t
```

- Client

```
sudo chrt -f 50 taskset -c 3-5 ./xsk_rr -o 0 -B 400 -i eth0 -4 \
	-S <IP-src> -D <IP-dest> -m <MAC-src> -M <MAC-dst> -p 54321 \
	-P <Period-usecs> -d <Delay-usecs>  -T -l 1 -v -t
```

## NAPI Threaded Busy Poll

Argument -n skips the recvfrom call as there is no recv kick needed.

- Server

```
sudo chrt -f 50 taskset -c 3-5 ./xsk_rr -o 0 -B 400 -i eth0 -4 \
	-D <IP-dest> -S <IP-src> -M <MAC-dst> -m <MAC-src> -p 54321 \
	-h -v -n
```

- Client

```
sudo chrt -f 50 taskset -c 3-5 ./xsk_rr -o 0 -B 400 -i eth0 -4 \
	-S <IP-src> -D <IP-dest> -m <MAC-src> -M <MAC-dst> -p 54321 \
	-P <Period-usecs> -d <Delay-usecs>  -T -l 1 -v -n
```
| Experiment | interrupts | SO_BUSYPOLL | SO_BUSYPOLL(separate) | NAPI threaded |
|---|---|---|---|---|
| 12 Kpkt/s + 0us delay | | | | |
|  | p5: 12700 | p5: 12900 | p5: 13300 | p5: 12800 |
|  | p50: 13100 | p50: 13600 | p50: 14100 | p50: 13000 |
|  | p95: 13200 | p95: 13800 | p95: 14400 | p95: 13000 |
|  | p99: 13200 | p99: 13800 | p99: 14400 | p99: 13000 |
| 32 Kpkt/s + 30us delay | | | | |
|  | p5: 19900 | p5: 16600 | p5: 13100 | p5: 12800 |
|  | p50: 21100 | p50: 17000 | p50: 13700 | p50: 13000 |
|  | p95: 21200 | p95: 17100 | p95: 14000 | p95: 13000 |
|  | p99: 21200 | p99: 17100 | p99: 14000 | p99: 13000 |
| 125 Kpkt/s + 6us delay | | | | |
|  | p5: 14600 | p5: 17100 | p5: 13300 | p5: 12900 |
|  | p50: 15400 | p50: 17400 | p50: 13800 | p50: 13100 |
|  | p95: 15600 | p95: 17600 | p95: 14000 | p95: 13100 |
|  | p99: 15600 | p99: 17600 | p99: 14000 | p99: 13100 |
| 12 Kpkt/s + 78us delay | | | | |
|  | p5: 14100 | p5: 16700 | p5: 13200 | p5: 12600 |
|  | p50: 14300 | p50: 17100 | p50: 13900 | p50: 12800 |
|  | p95: 14300 | p95: 17200 | p95: 14200 | p95: 12800 |
|  | p99: 14300 | p99: 17200 | p99: 14200 | p99: 12800 |
| 25 Kpkt/s + 38us delay | | | | |
|  | p5: 19900 | p5: 16600 | p5: 13000 | p5: 12700 |
|  | p50: 21000 | p50: 17100 | p50: 13800 | p50: 12900 |
|  | p95: 21100 | p95: 17100 | p95: 14100 | p95: 12900 |
|  | p99: 21100 | p99: 17100 | p99: 14100 | p99: 12900 |

## Observations

- Here without application processing all the approaches give the same latency within 1usecs range and NAPI threaded gives minimum latency.
- With application processing the latency increases by 3-4usecs when doing inline polling.
- Using a dedicated core to drive napi polling keeps the latency same even with application processing. This is observed both in userspace and threaded napi (in kernel).
- Using napi threaded polling in kernel gives lower latency by 1-1.5usecs as compared to userspace driven polling in separate core.
- With application processing userspace will get the packet from recv

Quoted 8 lines

ring and spend some time doing application processing and then do napi polling. While application processing is happening a dedicated core doing napi polling can pull the packet of the NAPI RX queue and populate the AF_XDP recv ring. This means that when the application thread is done with application processing it has new packets ready to recv and process in recv ring.
- Napi threaded busy polling in the kernel with a dedicated core gives the consistent P5-P99 latency.

I've experimented with this some more. I can confirm latency savings of about 1 usec arising from busy-looping a NAPI thread on a dedicated core when compared to in-thread busy-polling. A few more comments:

1) I note that the experiment results above show that 'interrupts' is almost as fast as 'NAPI threaded' in the base case. I cannot confirm these results, because I currently only have (very) old hardware available for testing. However, these results worry me in terms of necessity of the threaded busy-polling mechanism - also see Item 4) below.

2) The experiments reported here are symmetric in that they use the same polling variant at both the client and the server. When mixing things up by combining different polling variants, it becomes clear that the latency savings are split between both ends. The total savings of 1 usec are thus a combination of 0.5 usec are either end. So the ultimate trade-off is 0.5 usec latency gain for burning 1 core.

3) I believe the savings arise from running two tight loops (separate NAPI and application) instead of one longer loop. The shorter loops likely result in better cache utilization on their respective dedicated cores (and L1 caches). However I am not sure right how to explicitly confirm this.

4) I still believe that the additional experiments with setting both delay and period are meaningless. They create corner cases where rate * delay is about 1. Nobody would run a latency-critical system at 100% load. I also note that the experiment program xsk_rr fails when trying to increase the load beyond saturation (client fails with 'xsk_rr:
oustanding array full').

5) I worry that a mechanism like this might be misinterpreted as some kind of magic wand for improving performance and might end up being used in practice and cause substantial overhead without much gain. If accepted, I would hope that this will be documented very clearly and have appropriate warnings attached. Given that the patch cover letter is often used as a basis for documentation, I believe this should be spelled out in the cover letter.

Quoted 4 lines

With the above in mind, someone else will need to judge whether (at most) 0.5 usec for burning a core is a worthy enough trade-off to justify inclusion of this mechanism. Maybe someone else can take a closer look at the 'interrupts' variant on modern hardware.

Ack on documentation of the pros/cons.

There is also a functional argument for this feature. It brings parity with userspace network stacks like DPDK and Google's SNAP [1]. These also run packet (and L4+) network processing on dedicated cores, and by default do so in polling mode. An XDP plane currently lacks this well understood configuration. This brings it closer to parity.

Users of such advanced environments can be expected to be well familiar with the cost of polling. The cost/benefit can be debated and benchmarked for individual applications. But there clearly are active uses for polling, so I think it should be an operating system facility.

[1] https://research.google/pubs/snap-a-microkernel-approach-to-host-networking/

#12 lore raw
Quoted 4 lines · Willem de Bruijn
Quoted 1 line · Martin Karsten

On 2025-04-24 16:02, Samiullah Khawaja wrote:

Ack on documentation of the pros/cons.

In my mind this includes documenting CPU usage which I know is considered as "non-goal" of this series. It can be a "non-goal" but it is still very relevant to the conversation and documentation.

Quoted 5 lines

There is also a functional argument for this feature. It brings parity with userspace network stacks like DPDK and Google's SNAP [1]. These also run packet (and L4+) network processing on dedicated cores, and by default do so in polling mode. An XDP plane currently lacks this well understood configuration. This brings it closer to parity.

It would be good if this could also be included in the cover letter, I think, possibly with example use cases.

Quoted 5 lines

Users of such advanced environments can be expected to be well familiar with the cost of polling. The cost/benefit can be debated and benchmarked for individual applications. But there clearly are active uses for polling, so I think it should be an operating system facility.

You mention users of advanced environments, but I think it's important to consider the average user who is not necessarily a kernel programmer.

Will that user understand that not all apps support this? Or will they think that they can simply run a few YNL commands burning CPUs at 100% for apps that don't even support this thinking they are making their networking faster?

I think providing a mechanism to burn CPU cores at 100% CPU by enabling threaded busy poll has serious consequences on power consumption, cooling requirements, and, errr, earth. I don't think it's a decision to be taken lightly.

#13 lore raw
Quoted 25 lines · Joe Damato
Quoted 4 lines · Willem de Bruijn
Quoted 1 line · Martin Karsten

On 2025-04-24 16:02, Samiullah Khawaja wrote:

Ack on documentation of the pros/cons.

In my mind this includes documenting CPU usage which I know is considered as "non-goal" of this series. It can be a "non-goal" but it is still very relevant to the conversation and documentation.

Quoted 5 lines

There is also a functional argument for this feature. It brings parity with userspace network stacks like DPDK and Google's SNAP [1]. These also run packet (and L4+) network processing on dedicated cores, and by default do so in polling mode. An XDP plane currently lacks this well understood configuration. This brings it closer to parity.

It would be good if this could also be included in the cover letter, I think, possibly with example use cases.

Quoted 5 lines

Users of such advanced environments can be expected to be well familiar with the cost of polling. The cost/benefit can be debated and benchmarked for individual applications. But there clearly are active uses for polling, so I think it should be an operating system facility.

Quoted 8 lines

You mention users of advanced environments, but I think it's important to consider the average user who is not necessarily a kernel programmer.

Will that user understand that not all apps support this? Or will they think that they can simply run a few YNL commands burning CPUs at 100% for apps that don't even support this thinking they are making their networking faster?

Busy polling can already be configured through sysfs.

I have not seen any conversations where this is suggested to non expert users. I don't think this will be different.

But we can and should definitely increase the confidence by making sure that any documentation of the feature contains a clear warning to the impact and that this is for expert users only.

Quoted 4 lines

I think providing a mechanism to burn CPU cores at 100% CPU by enabling threaded busy poll has serious consequences on power consumption, cooling requirements, and, errr, earth. I don't think it's a decision to be taken lightly.

Good point.

#14 lore raw
Quoted 4 lines · Willem de Bruijn
Quoted 3 lines · Joe Damato
Quoted 2 lines · Willem de Bruijn
Quoted 1 line · Martin Karsten

On 2025-04-24 16:02, Samiullah Khawaja wrote:

[snip]

Quoted 10 lines
Quoted 10 lines
Quoted 5 lines

Users of such advanced environments can be expected to be well familiar with the cost of polling. The cost/benefit can be debated and benchmarked for individual applications. But there clearly are active uses for polling, so I think it should be an operating system facility.

You mention users of advanced environments, but I think it's important to consider the average user who is not necessarily a kernel programmer.

Quoted 6 lines
Quoted 4 lines

Will that user understand that not all apps support this? Or will they think that they can simply run a few YNL commands burning CPUs at 100% for apps that don't even support this thinking they are making their networking faster?

Busy polling can already be configured through sysfs.

That's not the same. The existing busy-poll mechanism still needs an application thread. This thing will just go off and spin, whether there's an application or not.

#15 lore raw

On Mon, 28 Apr 2025 14:05:17 -0400
Martin Karsten <[email protected]> wrote:

...

Quoted 3 lines

That's not the same. The existing busy-poll mechanism still needs an application thread. This thing will just go off and spin, whether there's an application or not.

I think this (and elsewhere) should be 'busy spin' not 'busy poll'. That would make it much more obvious that it really is a cpu intensive spin loop.

Note that on some cpu all the 'cores' run at the same speed. So that putting one into a 'spin' will cause the frequency of all of them to increase - thus speeding up a benchmark.

Rather the opposite of tests where a cpu busy thread (doing work) gets bounced around physical cpu - so keeps being run on ones running a low clock speed.

David

#16 lore raw
Quoted 4 lines · David Laight

On Mon, 28 Apr 2025 14:05:17 -0400
Martin Karsten <[email protected]> wrote:

Quoted 8 lines

...

Quoted 3 lines

That's not the same. The existing busy-poll mechanism still needs an application thread. This thing will just go off and spin, whether there's an application or not.

I think this (and elsewhere) should be 'busy spin' not 'busy poll'. That would make it much more obvious that it really is a cpu intensive spin loop.

"Busy Poll" is synonymous to polling a socket/napi/queue while spinning. For example it is used for socket busy poll and epoll busy poll. Calling it "busy spin" would be confusing I think. I will add a note in documentation, as suggested, to explain that it will spin a core and depending on requirements one might want to share the core by using affinity and priority configurations.

Quoted 10 lines

Note that on some cpu all the 'cores' run at the same speed. So that putting one into a 'spin' will cause the frequency of all of them to increase - thus speeding up a benchmark.

Rather the opposite of tests where a cpu busy thread (doing work) gets bounced around physical cpu - so keeps being run on ones running a low clock speed.

David

#17 lore raw
In reply to Willem de Bruijn
Quoted 2 lines · Willem de Bruijn
Quoted 1 line · Martin Karsten

On 2025-04-24 16:02, Samiullah Khawaja wrote:

[snip]

Quoted 5 lines

There is also a functional argument for this feature. It brings parity with userspace network stacks like DPDK and Google's SNAP [1]. These also run packet (and L4+) network processing on dedicated cores, and by default do so in polling mode. An XDP plane currently lacks this well understood configuration. This brings it closer to parity.

I believe these existing user-level stacks burn one core per queue. This scheme burns two.

#18 lore raw
Quoted 6 lines · Martin Karsten
Quoted 2 lines · Willem de Bruijn
Quoted 1 line · Martin Karsten

On 2025-04-24 16:02, Samiullah Khawaja wrote:

[snip]

Quoted 7 lines
Quoted 5 lines

There is also a functional argument for this feature. It brings parity with userspace network stacks like DPDK and Google's SNAP [1]. These also run packet (and L4+) network processing on dedicated cores, and by default do so in polling mode. An XDP plane currently lacks this well understood configuration. This brings it closer to parity.

I believe these existing user-level stacks burn one core per queue. This scheme burns two.

That's specific to the AF_XDP use-case where AF_XDP also runs in polling mode.

There are other use cases that more closely match DPDK, such as an XDP dataplane. Conversely, DPDK with reinject into the host kernel will also have to handle the signal or polling of the second stage.

#19 lore raw
In reply to Martin Karsten
Quoted 1 line · Martin Karsten

On 2025-04-24 16:02, Samiullah Khawaja wrote:

[snip]

Quoted 48 lines
Quoted 40 lines
| Experiment | interrupts | SO_BUSYPOLL | SO_BUSYPOLL(separate) | NAPI threaded |
|---|---|---|---|---|
| 12 Kpkt/s + 0us delay | | | | |
|  | p5: 12700 | p5: 12900 | p5: 13300 | p5: 12800 |
|  | p50: 13100 | p50: 13600 | p50: 14100 | p50: 13000 |
|  | p95: 13200 | p95: 13800 | p95: 14400 | p95: 13000 |
|  | p99: 13200 | p99: 13800 | p99: 14400 | p99: 13000 |
| 32 Kpkt/s + 30us delay | | | | |
|  | p5: 19900 | p5: 16600 | p5: 13100 | p5: 12800 |
|  | p50: 21100 | p50: 17000 | p50: 13700 | p50: 13000 |
|  | p95: 21200 | p95: 17100 | p95: 14000 | p95: 13000 |
|  | p99: 21200 | p99: 17100 | p99: 14000 | p99: 13000 |
| 125 Kpkt/s + 6us delay | | | | |
|  | p5: 14600 | p5: 17100 | p5: 13300 | p5: 12900 |
|  | p50: 15400 | p50: 17400 | p50: 13800 | p50: 13100 |
|  | p95: 15600 | p95: 17600 | p95: 14000 | p95: 13100 |
|  | p99: 15600 | p99: 17600 | p99: 14000 | p99: 13100 |
| 12 Kpkt/s + 78us delay | | | | |
|  | p5: 14100 | p5: 16700 | p5: 13200 | p5: 12600 |
|  | p50: 14300 | p50: 17100 | p50: 13900 | p50: 12800 |
|  | p95: 14300 | p95: 17200 | p95: 14200 | p95: 12800 |
|  | p99: 14300 | p99: 17200 | p99: 14200 | p99: 12800 |
| 25 Kpkt/s + 38us delay | | | | |
|  | p5: 19900 | p5: 16600 | p5: 13000 | p5: 12700 |
|  | p50: 21000 | p50: 17100 | p50: 13800 | p50: 12900 |
|  | p95: 21100 | p95: 17100 | p95: 14100 | p95: 12900 |
|  | p99: 21100 | p99: 17100 | p99: 14100 | p99: 12900 |

  ## Observations

- Here without application processing all the approaches give the same latency within 1usecs range and NAPI threaded gives minimum latency.
- With application processing the latency increases by 3-4usecs when doing inline polling.
- Using a dedicated core to drive napi polling keeps the latency same even with application processing. This is observed both in userspace and threaded napi (in kernel).
- Using napi threaded polling in kernel gives lower latency by 1-1.5usecs as compared to userspace driven polling in separate core.
- With application processing userspace will get the packet from recv

Quoted 8 lines

   ring and spend some time doing application processing and then do napi polling. While application processing is happening a dedicated core doing napi polling can pull the packet of the NAPI RX queue and populate the AF_XDP recv ring. This means that when the application thread is done with application processing it has new packets ready to recv and process in recv ring.
- Napi threaded busy polling in the kernel with a dedicated core gives the consistent P5-P99 latency.

Quoted 3 lines

I've experimented with this some more. I can confirm latency savings of about 1 usec arising from busy-looping a NAPI thread on a dedicated core when compared to in-thread busy-polling. A few more comments:

1) I note that the experiment results above show that 'interrupts' is almost as fast as 'NAPI threaded' in the base case. I cannot confirm these results, because I currently only have (very) old hardware available for testing. However, these results worry me in terms of necessity of the threaded busy-polling mechanism - also see Item 4) below.

I want to add one more thought, just to spell this out explicitly: Assuming the latency benefits result from better cache utilization of two shorter processing loops (NAPI and application) using a dedicated core each, it would make sense to see softirq processing on the NAPI core being almost as fast. While there might be small penalty for the initial hardware interrupt, the following softirq processing does not differ much from what a NAPI spin-loop does? The experiments seem to corroborate this, because latency results for 'interrupts' and 'NAPI threaded' are extremely close.

In this case, it would be essential that interrupt handling happens on a dedicated empty core, so it can react to hardware interrupts right away and its local cache isn't dirtied by other code than softirq processing. While this also means dedicating a entire core to NAPI processing, at least the core wouldn't have to spin all the time, hopefully reducing power consumption and heat generation.

Thanks,
Martin

Quoted 12 lines

2) The experiments reported here are symmetric in that they use the same polling variant at both the client and the server. When mixing things up by combining different polling variants, it becomes clear that the latency savings are split between both ends. The total savings of 1 usec are thus a combination of 0.5 usec are either end. So the ultimate trade-off is 0.5 usec latency gain for burning 1 core.

3) I believe the savings arise from running two tight loops (separate NAPI and application) instead of one longer loop. The shorter loops likely result in better cache utilization on their respective dedicated cores (and L1 caches). However I am not sure right how to explicitly confirm this.

4) I still believe that the additional experiments with setting both delay and period are meaningless. They create corner cases where rate * delay is about 1. Nobody would run a latency-critical system at 100% load. I also note that the experiment program xsk_rr fails when trying to increase the load beyond saturation (client fails with 'xsk_rr: oustanding array full').

5) I worry that a mechanism like this might be misinterpreted as some kind of magic wand for improving performance and might end up being used in practice and cause substantial overhead without much gain. If accepted, I would hope that this will be documented very clearly and have appropriate warnings attached. Given that the patch cover letter is often used as a basis for documentation, I believe this should be spelled out in the cover letter.

With the above in mind, someone else will need to judge whether (at most) 0.5 usec for burning a core is a worthy enough trade-off to justify inclusion of this mechanism. Maybe someone else can take a closer look at the 'interrupts' variant on modern hardware.

Thanks,
Martin

#20 lore raw
Quoted 55 lines · Martin Karsten
Quoted 1 line · Martin Karsten

On 2025-04-24 16:02, Samiullah Khawaja wrote:

[snip]

Quoted 49 lines
Quoted 49 lines
| Experiment | interrupts | SO_BUSYPOLL | SO_BUSYPOLL(separate) | NAPI
threaded |
|---|---|---|---|---|
| 12 Kpkt/s + 0us delay | | | | |
|  | p5: 12700 | p5: 12900 | p5: 13300 | p5: 12800 |
|  | p50: 13100 | p50: 13600 | p50: 14100 | p50: 13000 |
|  | p95: 13200 | p95: 13800 | p95: 14400 | p95: 13000 |
|  | p99: 13200 | p99: 13800 | p99: 14400 | p99: 13000 |
| 32 Kpkt/s + 30us delay | | | | |
|  | p5: 19900 | p5: 16600 | p5: 13100 | p5: 12800 |
|  | p50: 21100 | p50: 17000 | p50: 13700 | p50: 13000 |
|  | p95: 21200 | p95: 17100 | p95: 14000 | p95: 13000 |
|  | p99: 21200 | p99: 17100 | p99: 14000 | p99: 13000 |
| 125 Kpkt/s + 6us delay | | | | |
|  | p5: 14600 | p5: 17100 | p5: 13300 | p5: 12900 |
|  | p50: 15400 | p50: 17400 | p50: 13800 | p50: 13100 |
|  | p95: 15600 | p95: 17600 | p95: 14000 | p95: 13100 |
|  | p99: 15600 | p99: 17600 | p99: 14000 | p99: 13100 |
| 12 Kpkt/s + 78us delay | | | | |
|  | p5: 14100 | p5: 16700 | p5: 13200 | p5: 12600 |
|  | p50: 14300 | p50: 17100 | p50: 13900 | p50: 12800 |
|  | p95: 14300 | p95: 17200 | p95: 14200 | p95: 12800 |
|  | p99: 14300 | p99: 17200 | p99: 14200 | p99: 12800 |
| 25 Kpkt/s + 38us delay | | | | |
|  | p5: 19900 | p5: 16600 | p5: 13000 | p5: 12700 |
|  | p50: 21000 | p50: 17100 | p50: 13800 | p50: 12900 |
|  | p95: 21100 | p95: 17100 | p95: 14100 | p95: 12900 |
|  | p99: 21100 | p99: 17100 | p99: 14100 | p99: 12900 |

## Observations

- Here without application processing all the approaches give the same latency within 1usecs range and NAPI threaded gives minimum latency.
- With application processing the latency increases by 3-4usecs when doing inline polling.
- Using a dedicated core to drive napi polling keeps the latency same even with application processing. This is observed both in userspace and threaded napi (in kernel).
- Using napi threaded polling in kernel gives lower latency by 1-1.5usecs as compared to userspace driven polling in separate core.
- With application processing userspace will get the packet from recv ring and spend some time doing application processing and then do napi polling. While application processing is happening a dedicated core doing napi polling can pull the packet of the NAPI RX queue and populate the AF_XDP recv ring. This means that when the application thread is done with application processing it has new packets ready to recv and process in recv ring.
- Napi threaded busy polling in the kernel with a dedicated core gives the consistent P5-P99 latency.

Quoted 3 lines
Quoted 3 lines

I've experimented with this some more. I can confirm latency savings of about 1 usec arising from busy-looping a NAPI thread on a dedicated core when compared to in-thread busy-polling. A few more comments:

Thanks for the experiments and reproducing this. I really appreciate it.

Quoted 7 lines
Quoted 6 lines

1) I note that the experiment results above show that 'interrupts' is almost as fast as 'NAPI threaded' in the base case. I cannot confirm these results, because I currently only have (very) old hardware available for testing. However, these results worry me in terms of necessity of the threaded busy-polling mechanism - also see Item 4) below.

Quoted 6 lines

I want to add one more thought, just to spell this out explicitly:
Assuming the latency benefits result from better cache utilization of two shorter processing loops (NAPI and application) using a dedicated core each, it would make sense to see softirq processing on the NAPI core being almost as fast. While there might be small penalty for the initial hardware interrupt, the following softirq processing does not

The interrupt experiment in the last row demonstrates the penalty you mentioned. While this effect might be acceptable for some use cases, it could be problematic in scenarios sensitive to jitter (P99 latency).

Quoted 49 lines

differ much from what a NAPI spin-loop does? The experiments seem to corroborate this, because latency results for 'interrupts' and 'NAPI threaded' are extremely close.

In this case, it would be essential that interrupt handling happens on a dedicated empty core, so it can react to hardware interrupts right away and its local cache isn't dirtied by other code than softirq processing. While this also means dedicating a entire core to NAPI processing, at least the core wouldn't have to spin all the time, hopefully reducing power consumption and heat generation.

Thanks,
Martin

Quoted 35 lines

2) The experiments reported here are symmetric in that they use the same polling variant at both the client and the server. When mixing things up by combining different polling variants, it becomes clear that the latency savings are split between both ends. The total savings of 1 usec are thus a combination of 0.5 usec are either end. So the ultimate trade-off is 0.5 usec latency gain for burning 1 core.

3) I believe the savings arise from running two tight loops (separate NAPI and application) instead of one longer loop. The shorter loops likely result in better cache utilization on their respective dedicated cores (and L1 caches). However I am not sure right how to explicitly confirm this.

4) I still believe that the additional experiments with setting both delay and period are meaningless. They create corner cases where rate * delay is about 1. Nobody would run a latency-critical system at 100% load. I also note that the experiment program xsk_rr fails when trying to increase the load beyond saturation (client fails with 'xsk_rr:
oustanding array full').

5) I worry that a mechanism like this might be misinterpreted as some kind of magic wand for improving performance and might end up being used in practice and cause substantial overhead without much gain. If accepted, I would hope that this will be documented very clearly and have appropriate warnings attached. Given that the patch cover letter is often used as a basis for documentation, I believe this should be spelled out in the cover letter.

With the above in mind, someone else will need to judge whether (at most) 0.5 usec for burning a core is a worthy enough trade-off to justify inclusion of this mechanism. Maybe someone else can take a closer look at the 'interrupts' variant on modern hardware.

Thanks,
Martin

#21 lore raw
Quoted 73 lines · Samiullah Khawaja
Quoted 55 lines · Martin Karsten
Quoted 1 line · Martin Karsten

On 2025-04-24 16:02, Samiullah Khawaja wrote:

[snip]

Quoted 49 lines
Quoted 49 lines
| Experiment | interrupts | SO_BUSYPOLL | SO_BUSYPOLL(separate) | NAPI
threaded |
|---|---|---|---|---|
| 12 Kpkt/s + 0us delay | | | | |
|  | p5: 12700 | p5: 12900 | p5: 13300 | p5: 12800 |
|  | p50: 13100 | p50: 13600 | p50: 14100 | p50: 13000 |
|  | p95: 13200 | p95: 13800 | p95: 14400 | p95: 13000 |
|  | p99: 13200 | p99: 13800 | p99: 14400 | p99: 13000 |
| 32 Kpkt/s + 30us delay | | | | |
|  | p5: 19900 | p5: 16600 | p5: 13100 | p5: 12800 |
|  | p50: 21100 | p50: 17000 | p50: 13700 | p50: 13000 |
|  | p95: 21200 | p95: 17100 | p95: 14000 | p95: 13000 |
|  | p99: 21200 | p99: 17100 | p99: 14000 | p99: 13000 |
| 125 Kpkt/s + 6us delay | | | | |
|  | p5: 14600 | p5: 17100 | p5: 13300 | p5: 12900 |
|  | p50: 15400 | p50: 17400 | p50: 13800 | p50: 13100 |
|  | p95: 15600 | p95: 17600 | p95: 14000 | p95: 13100 |
|  | p99: 15600 | p99: 17600 | p99: 14000 | p99: 13100 |
| 12 Kpkt/s + 78us delay | | | | |
|  | p5: 14100 | p5: 16700 | p5: 13200 | p5: 12600 |
|  | p50: 14300 | p50: 17100 | p50: 13900 | p50: 12800 |
|  | p95: 14300 | p95: 17200 | p95: 14200 | p95: 12800 |
|  | p99: 14300 | p99: 17200 | p99: 14200 | p99: 12800 |
| 25 Kpkt/s + 38us delay | | | | |
|  | p5: 19900 | p5: 16600 | p5: 13000 | p5: 12700 |
|  | p50: 21000 | p50: 17100 | p50: 13800 | p50: 12900 |
|  | p95: 21100 | p95: 17100 | p95: 14100 | p95: 12900 |
|  | p99: 21100 | p99: 17100 | p99: 14100 | p99: 12900 |

## Observations

- Here without application processing all the approaches give the same latency within 1usecs range and NAPI threaded gives minimum latency.
- With application processing the latency increases by 3-4usecs when doing inline polling.
- Using a dedicated core to drive napi polling keeps the latency same even with application processing. This is observed both in userspace and threaded napi (in kernel).
- Using napi threaded polling in kernel gives lower latency by 1-1.5usecs as compared to userspace driven polling in separate core.
- With application processing userspace will get the packet from recv ring and spend some time doing application processing and then do napi polling. While application processing is happening a dedicated core doing napi polling can pull the packet of the NAPI RX queue and populate the AF_XDP recv ring. This means that when the application thread is done with application processing it has new packets ready to recv and process in recv ring.
- Napi threaded busy polling in the kernel with a dedicated core gives the consistent P5-P99 latency.

Quoted 3 lines
Quoted 3 lines

I've experimented with this some more. I can confirm latency savings of about 1 usec arising from busy-looping a NAPI thread on a dedicated core when compared to in-thread busy-polling. A few more comments:

Thanks for the experiments and reproducing this. I really appreciate it.

Quoted 7 lines
Quoted 6 lines

1) I note that the experiment results above show that 'interrupts' is almost as fast as 'NAPI threaded' in the base case. I cannot confirm these results, because I currently only have (very) old hardware available for testing. However, these results worry me in terms of necessity of the threaded busy-polling mechanism - also see Item 4) below.

Quoted 6 lines

I want to add one more thought, just to spell this out explicitly:
Assuming the latency benefits result from better cache utilization of two shorter processing loops (NAPI and application) using a dedicated core each, it would make sense to see softirq processing on the NAPI core being almost as fast. While there might be small penalty for the initial hardware interrupt, the following softirq processing does not

Quoted 4 lines

The interrupt experiment in the last row demonstrates the penalty you mentioned. While this effect might be acceptable for some use cases, it could be problematic in scenarios sensitive to jitter (P99 latency).

Just to be clear andexplicit: The difference is 200 nsecs for P99 (13200 vs 13000), i.e, 100 nsecs per core burned on either side. As I mentioned before, I don't think the 100%-load experiments (those with nonzero delay setting) are representative of any real-world scenario.

Thanks,
Martin

Quoted 49 lines
Quoted 49 lines

differ much from what a NAPI spin-loop does? The experiments seem to corroborate this, because latency results for 'interrupts' and 'NAPI threaded' are extremely close.

In this case, it would be essential that interrupt handling happens on a dedicated empty core, so it can react to hardware interrupts right away and its local cache isn't dirtied by other code than softirq processing. While this also means dedicating a entire core to NAPI processing, at least the core wouldn't have to spin all the time, hopefully reducing power consumption and heat generation.

Thanks,
Martin

Quoted 35 lines

2) The experiments reported here are symmetric in that they use the same polling variant at both the client and the server. When mixing things up by combining different polling variants, it becomes clear that the latency savings are split between both ends. The total savings of 1 usec are thus a combination of 0.5 usec are either end. So the ultimate trade-off is 0.5 usec latency gain for burning 1 core.

3) I believe the savings arise from running two tight loops (separate NAPI and application) instead of one longer loop. The shorter loops likely result in better cache utilization on their respective dedicated cores (and L1 caches). However I am not sure right how to explicitly confirm this.

4) I still believe that the additional experiments with setting both delay and period are meaningless. They create corner cases where rate * delay is about 1. Nobody would run a latency-critical system at 100% load. I also note that the experiment program xsk_rr fails when trying to increase the load beyond saturation (client fails with 'xsk_rr:
oustanding array full').

5) I worry that a mechanism like this might be misinterpreted as some kind of magic wand for improving performance and might end up being used in practice and cause substantial overhead without much gain. If accepted, I would hope that this will be documented very clearly and have appropriate warnings attached. Given that the patch cover letter is often used as a basis for documentation, I believe this should be spelled out in the cover letter.

With the above in mind, someone else will need to judge whether (at most) 0.5 usec for burning a core is a worthy enough trade-off to justify inclusion of this mechanism. Maybe someone else can take a closer look at the 'interrupts' variant on modern hardware.

Thanks,
Martin

#22 lore raw
Quoted 80 lines · Martin Karsten
Quoted 73 lines · Samiullah Khawaja
Quoted 55 lines · Martin Karsten
Quoted 1 line · Martin Karsten

On 2025-04-24 16:02, Samiullah Khawaja wrote:

[snip]

Quoted 49 lines
Quoted 49 lines
| Experiment | interrupts | SO_BUSYPOLL | SO_BUSYPOLL(separate) | NAPI
threaded |
|---|---|---|---|---|
| 12 Kpkt/s + 0us delay | | | | |
|  | p5: 12700 | p5: 12900 | p5: 13300 | p5: 12800 |
|  | p50: 13100 | p50: 13600 | p50: 14100 | p50: 13000 |
|  | p95: 13200 | p95: 13800 | p95: 14400 | p95: 13000 |
|  | p99: 13200 | p99: 13800 | p99: 14400 | p99: 13000 |
| 32 Kpkt/s + 30us delay | | | | |
|  | p5: 19900 | p5: 16600 | p5: 13100 | p5: 12800 |
|  | p50: 21100 | p50: 17000 | p50: 13700 | p50: 13000 |
|  | p95: 21200 | p95: 17100 | p95: 14000 | p95: 13000 |
|  | p99: 21200 | p99: 17100 | p99: 14000 | p99: 13000 |
| 125 Kpkt/s + 6us delay | | | | |
|  | p5: 14600 | p5: 17100 | p5: 13300 | p5: 12900 |
|  | p50: 15400 | p50: 17400 | p50: 13800 | p50: 13100 |
|  | p95: 15600 | p95: 17600 | p95: 14000 | p95: 13100 |
|  | p99: 15600 | p99: 17600 | p99: 14000 | p99: 13100 |
| 12 Kpkt/s + 78us delay | | | | |
|  | p5: 14100 | p5: 16700 | p5: 13200 | p5: 12600 |
|  | p50: 14300 | p50: 17100 | p50: 13900 | p50: 12800 |
|  | p95: 14300 | p95: 17200 | p95: 14200 | p95: 12800 |
|  | p99: 14300 | p99: 17200 | p99: 14200 | p99: 12800 |
| 25 Kpkt/s + 38us delay | | | | |
|  | p5: 19900 | p5: 16600 | p5: 13000 | p5: 12700 |
|  | p50: 21000 | p50: 17100 | p50: 13800 | p50: 12900 |
|  | p95: 21100 | p95: 17100 | p95: 14100 | p95: 12900 |
|  | p99: 21100 | p99: 17100 | p99: 14100 | p99: 12900 |

## Observations

- Here without application processing all the approaches give the same latency within 1usecs range and NAPI threaded gives minimum latency.
- With application processing the latency increases by 3-4usecs when doing inline polling.
- Using a dedicated core to drive napi polling keeps the latency same even with application processing. This is observed both in userspace and threaded napi (in kernel).
- Using napi threaded polling in kernel gives lower latency by 1-1.5usecs as compared to userspace driven polling in separate core.
- With application processing userspace will get the packet from recv ring and spend some time doing application processing and then do napi polling. While application processing is happening a dedicated core doing napi polling can pull the packet of the NAPI RX queue and populate the AF_XDP recv ring. This means that when the application thread is done with application processing it has new packets ready to recv and process in recv ring.
- Napi threaded busy polling in the kernel with a dedicated core gives the consistent P5-P99 latency.

Quoted 3 lines
Quoted 3 lines

I've experimented with this some more. I can confirm latency savings of about 1 usec arising from busy-looping a NAPI thread on a dedicated core when compared to in-thread busy-polling. A few more comments:

Thanks for the experiments and reproducing this. I really appreciate it.

Quoted 7 lines
Quoted 6 lines

1) I note that the experiment results above show that 'interrupts' is almost as fast as 'NAPI threaded' in the base case. I cannot confirm these results, because I currently only have (very) old hardware available for testing. However, these results worry me in terms of necessity of the threaded busy-polling mechanism - also see Item 4) below.

Quoted 6 lines

I want to add one more thought, just to spell this out explicitly:
Assuming the latency benefits result from better cache utilization of two shorter processing loops (NAPI and application) using a dedicated core each, it would make sense to see softirq processing on the NAPI core being almost as fast. While there might be small penalty for the initial hardware interrupt, the following softirq processing does not

Quoted 4 lines

The interrupt experiment in the last row demonstrates the penalty you mentioned. While this effect might be acceptable for some use cases, it could be problematic in scenarios sensitive to jitter (P99 latency).

Quoted 4 lines

Just to be clear andexplicit: The difference is 200 nsecs for P99 (13200 vs 13000), i.e, 100 nsecs per core burned on either side. As I mentioned before, I don't think the 100%-load experiments (those with nonzero delay setting) are representative of any real-world scenario.

oh.. you are only considering the first row. Yes, with zero delay it would (mostly) be equal. I agree with you that there is very little difference in that particular scenario.

Quoted 54 lines

Thanks,
Martin

Quoted 49 lines
Quoted 49 lines

differ much from what a NAPI spin-loop does? The experiments seem to corroborate this, because latency results for 'interrupts' and 'NAPI threaded' are extremely close.

In this case, it would be essential that interrupt handling happens on a dedicated empty core, so it can react to hardware interrupts right away and its local cache isn't dirtied by other code than softirq processing. While this also means dedicating a entire core to NAPI processing, at least the core wouldn't have to spin all the time, hopefully reducing power consumption and heat generation.

Thanks,
Martin

Quoted 35 lines

2) The experiments reported here are symmetric in that they use the same polling variant at both the client and the server. When mixing things up by combining different polling variants, it becomes clear that the latency savings are split between both ends. The total savings of 1 usec are thus a combination of 0.5 usec are either end. So the ultimate trade-off is 0.5 usec latency gain for burning 1 core.

3) I believe the savings arise from running two tight loops (separate NAPI and application) instead of one longer loop. The shorter loops likely result in better cache utilization on their respective dedicated cores (and L1 caches). However I am not sure right how to explicitly confirm this.

4) I still believe that the additional experiments with setting both delay and period are meaningless. They create corner cases where rate * delay is about 1. Nobody would run a latency-critical system at 100% load. I also note that the experiment program xsk_rr fails when trying to increase the load beyond saturation (client fails with 'xsk_rr:
oustanding array full').

5) I worry that a mechanism like this might be misinterpreted as some kind of magic wand for improving performance and might end up being used in practice and cause substantial overhead without much gain. If accepted, I would hope that this will be documented very clearly and have appropriate warnings attached. Given that the patch cover letter is often used as a basis for documentation, I believe this should be spelled out in the cover letter.

With the above in mind, someone else will need to judge whether (at most) 0.5 usec for burning a core is a worthy enough trade-off to justify inclusion of this mechanism. Maybe someone else can take a closer look at the 'interrupts' variant on modern hardware.

Thanks,
Martin