Skip to contentSkip to conversation

Reuse threaded NAPI kthread across napi_del()/napi_add().

RFC New Shuhao Tan 1 participant 3 messages Jun 12

Subsystems (3)
lore
Share & tools
lore

Shuhao Tan

author
#0 lore raw

cover Reuse threaded NAPI kthread across napi_del()/napi_add().

Currently the lifetime of the kthread of a threaded NAPI is tied to the napi_struct. netif_napi_del() stops the kthread when it destroys the NAPI struct.

This patch series reuses the same kthread (thus preserving all of its attributes) across napi_del/napi_add. This series now tie the lifetime of the kthread to net_device instead of napi_struct.

A normal use case for threaded NAPI will be "enable threaded" -> "configure the thread". Driver reset (that can be caused by a NIC configuration change) often destroys the configuration and causes a usability issue. This series aims to solve the issue.

There is a downside of this approach: If a device reduces number of queues while its NAPIs are threaded. The kthread associated with removed queues will not be stopped. Since the mapping between the index passed to napi_add_config() and NAPI is an implementation detail of individual drivers, it is not straightfoward to perform a garbage collection and stop kthreads that are no longer associated with a queue. This patch series tries to minimize the effect by parking the kthread in napi_del. The kthread still shows up in /proc, but should not consume CPU cycles.

There was a discussion
https://lore.kernel.org/CAAywjhR0TPKZ-xzqjSP709OVmZWUisDNv2CVc_VxgOrXRtop+g@mail.gmail.com/
around what to do with the kthread between napi_disable/napi_enable. It seems that there was an intention to keep user configuration for the kthread across NIC configuration change. This patch series extends the effort to cover more NIC configuration changes. Roughly tracing through the call hierarchy of napi_del reveals that at least the following drivers will not preserve the user configurations:
- idpf: idpf_set_channels(), idpf_set_ringparam()
- mlx4: mlx4_en_set_ringparam(), mlx4_en_set_rxfh(), mlx4_en_setchannels()
- bnx2: bnx2_set_ringparam(), bnx2_set_channels(), bnx2_change_mtu()
- gve: gve_set_channels(), gve_set_ringparam()
- ena2: ena_set_ringparam(), ena_set_channels()
- (non exhaustive)

These drivers destroy and recreate queues during configuration changes. If a NAPI was threaded before destruction, during the creation, a new kthread will be spawned for the NAPI.

Some drivers do not have this problem, e.g. fbnic, netdevsim. But these drivers and the drivers mentioned above will still lose kthread during link flap (ndo_stop/ndo_open).

Because the kthreads before and after these configuration changes are different, all the attributes associated with the kthread are lost. These include CPU mask, priority, scheduler policy, etc.. If the threaded state is preserved for a NAPI, it makes sense to want to preserve the attributes of the thread as well.

Shuhao Tan (2):
net: Save kthread of threaded NAPI in napi_config in napi_del and
restore in napi_add
selftests: net: Add kthread preserving test in napi_threaded and
busy_poll_test

 include/linux/netdevice.h                     |  13 +-
 net/core/dev.c                                | 151 ++++++++++++++----
 net/core/netdev-genl.c                        |  12 +-
 .../selftests/drivers/net/napi_threaded.py    |  41 ++++-
 tools/testing/selftests/net/busy_poll_test.sh |  24 +++
 5 files changed, 203 insertions(+), 38 deletions(-)
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
author
#1 lore raw

RFC 1/2 net: Save kthread of threaded NAPI in napi_config in napi_del and restore in napi_add

Replace napi->thread with a new thread_node struct that has a back pointer to napi_struct.

Make the NAPI thread to use the thread_node as data pointer so that it can poll on different NAPIs thoughout its lifetime.

Park the thread and save the thread_node in napi_config on napi_del. Restore the node and unpark the thread on napi_add_config.

---
 include/linux/netdevice.h |  13 +++-
 net/core/dev.c            | 151 +++++++++++++++++++++++++++++---------
 net/core/netdev-genl.c    |  12 ++-
 3 files changed, 139 insertions(+), 37 deletions(-)
include/linux/netdevice.h +12 -1
index 7f4f0837c09f..1cda88607e99 100644
@@ -63,6 +63,7 @@ struct dsa_port;
 struct ip_tunnel_parm_kern;
 struct macsec_context;
 struct macsec_ops;
+struct napi_struct;
 struct netdev_config;
 struct netdev_name_node;
 struct sd_flow_limit;
@@ -363,10 +364,20 @@ struct gro_node {
 	u32			cached_napi_id;
 };
 
+/*
+ * Structure for persisting threaded NAPI kthread
+ */
+struct napi_thread_node {
+	struct task_struct *thread;
+	struct napi_struct *napi;
+	struct rcu_head rcu;
+};
+
 /*
  * Structure for per-NAPI config
  */
 struct napi_config {
+	struct napi_thread_node *thread_node;
 	u64 gro_flush_timeout;
 	u64 irq_suspend_timeout;
 	u32 defer_hard_irqs;
@@ -403,7 +414,7 @@ struct napi_struct {
 	struct gro_node		gro;
 	struct hrtimer		timer;
 	/* all fields past this point are write-protected by netdev_lock */
-	struct task_struct	*thread;
+	struct napi_thread_node __rcu *thread_node;
 	unsigned long		gro_flush_timeout;
 	unsigned long		irq_suspend_timeout;
 	u32			defer_hard_irqs;
net/core/dev.c +118 -33

316 lines not on this page · raw message

net/core/netdev-genl.c +9 -3
index 11b0b91683d7..f2ecdb26d6f1 100644
@@ -162,6 +162,7 @@ static int
 netdev_nl_napi_fill_one(struct sk_buff *rsp, struct napi_struct *napi,
 			const struct genl_info *info)
 {
+	struct napi_thread_node *thread_node;
 	unsigned long irq_suspend_timeout;
 	unsigned long gro_flush_timeout;
 	u32 napi_defer_hard_irqs;
@@ -188,11 +189,16 @@ netdev_nl_napi_fill_one(struct sk_buff *rsp, struct napi_struct *napi,
 			 napi_get_threaded(napi)))
 		goto nla_put_failure;
 
-	if (napi->thread) {
-		pid = task_pid_nr(napi->thread);
-		if (nla_put_u32(rsp, NETDEV_A_NAPI_PID, pid))
+	rcu_read_lock();
+	thread_node = rcu_dereference(napi->thread_node);
+	if (thread_node) {
+		pid = task_pid_nr(thread_node->thread);
+		if (nla_put_u32(rsp, NETDEV_A_NAPI_PID, pid)) {
+			rcu_read_unlock();
 			goto nla_put_failure;
+		}
 	}
+	rcu_read_unlock();
 
 	napi_defer_hard_irqs = napi_get_defer_hard_irqs(napi);
 	if (nla_put_s32(rsp, NETDEV_A_NAPI_DEFER_HARD_IRQS,
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
author
#2 lore raw

RFC 2/2 selftests: net: Add kthread preserving test in napi_threaded and busy_poll_test

In reply to Shuhao Tan

Add testcase to ensure the kthread stays the same across NIC link flap.

Add testcase to ensure the same kthread can poll different napis across NIC link flap.

---
 .../selftests/drivers/net/napi_threaded.py    | 41 ++++++++++++++++++-
 tools/testing/selftests/net/busy_poll_test.sh | 24 +++++++++++
 2 files changed, 64 insertions(+), 1 deletion(-)
tools/testing/selftests/drivers/net/napi_threaded.py +40 -1
index f4be72b2145a..20110fb6942e 100755
@@ -127,6 +127,44 @@ def change_num_queues(cfg, nl) -> None:
     _assert_napi_threaded_enabled(nl, napi0_id)
     _assert_napi_threaded_enabled(nl, napi1_id)
 
+def nic_link_flap(cfg, nl) -> None:
+    """
+    Test that if threaded is enabled, and NIC goes through
+    a reset, the kthread stays unchanged across the link flap.
+    """
+    napis = nl.napi_get({'ifindex': cfg.ifindex}, dump=True)
+    ksft_ge(len(napis), 2)
+
+    napi0_id = napis[0]['id']
+    napi1_id = napis[1]['id']
+
+    _setup_deferred_cleanup(cfg)
+
+    # set threaded
+    _set_threaded_state(cfg, 1)
+    napis = nl.napi_get({'ifindex': cfg.ifindex}, dump=True)
+
+    # check napi threaded is set for both napis
+    _assert_napi_threaded_enabled(nl, napi0_id)
+    _assert_napi_threaded_enabled(nl, napi1_id)
+
+    pid0 = napis[0].get('pid')
+    pid1 = napis[1].get('pid')
+
+    cmd(f"ip link set {cfg.ifname} down")
+    cmd(f"ip link set {cfg.ifname} up")
+
+    # re-acquire napi info
+    napis = nl.napi_get({'ifindex': cfg.ifindex}, dump=True)
+    ksft_ge(len(napis), 2)
+
+    # check napi threaded is set for both napis
+    _assert_napi_threaded_enabled(nl, napi0_id)
+    _assert_napi_threaded_enabled(nl, napi1_id)
+
+    # check the kthread remains the same
+    ksft_eq(napis[0].get('pid'), pid0)
+    ksft_eq(napis[1].get('pid'), pid1)
 
 def main() -> None:
     """ Ksft boiler plate main """
@@ -134,7 +172,8 @@ def main() -> None:
     with NetDrvEnv(__file__, queue_count=2) as cfg:
         ksft_run([napi_init,
                   change_num_queues,
-                  enable_dev_threaded_disable_napi_threaded],
+                  enable_dev_threaded_disable_napi_threaded,
+                  nic_link_flap],
                  args=(cfg, NetdevFamily()))
     ksft_exit()
 
tools/testing/selftests/net/busy_poll_test.sh +24 -0
index 5ec1c85c1623..897ce6700601 100755
@@ -124,6 +124,23 @@ test_busypoll_with_napi_threaded()
 	return $?
 }
 
+test_busypoll_with_napi_threaded_link_flap()
+{
+	# Only enable napi threaded poll. Set suspend timeout and prefer busy
+	# poll to 0. Run again after a link flap.
+	test_busypoll 0 ${NAPI_THREADED_MODE_BUSY_POLL} 0 || return $?
+
+	ip netns exec nssv ip link set dev $NSIM_SV_NAME down
+	ip netns exec nscl ip link set dev $NSIM_CL_NAME down
+
+	ip netns exec nssv ip link set dev $NSIM_SV_NAME up
+	ip netns exec nscl ip link set dev $NSIM_CL_NAME up
+
+	test_busypoll 0 ${NAPI_THREADED_MODE_BUSY_POLL} 0
+
+	return $?
+}
+
 ###
 ### Code start
 ###
@@ -176,6 +193,13 @@ if [ $? -ne 0 ]; then
 	exit 1
 fi
 
+test_busypoll_with_napi_threaded_link_flap
+if [ $? -ne 0 ]; then
+	echo "test_busypoll_with_napi_threaded_link_flap 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
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog