Add support to set napi threaded for individual napi
Subsystems (5)
PATCH v8 1/1 Add support to set napi threaded for individual napi
A net device has a threaded sysctl that can be used to enable threaded napi polling on all of the NAPI contexts under that device. Allow enabling threaded napi polling at individual napi level using netlink.
Extend the netlink operation napi-set and allow setting the threaded attribute of a NAPI. This will enable the threaded polling on a napi context.
Add a test in nl_netdev.py that verifies various cases of threaded napi being set at napi and at device level.
Tested
./tools/testing/selftests/net/nl_netdev.py TAP version 13
1..7
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.dev_set_threaded
ok 6 nl_netdev.napi_set_threaded
ok 7 nl_netdev.nsim_rxq_reset_down
# Totals: pass:7 fail:0 xfail:0 xpass:0 skip:0 error:0
---
v8:
- Fix napi_set_threaded kdoc in net/core/dev.h.
v7:
- rebase
- stop kthread when napi threaded is unset at napi level.
- Update selftest to verify that kthread PID is not set when threaded is disabled at napi level.
v6:
- Set the threaded property at device level even if the currently set value is same. This is to override any per napi settings. Update selftest to verify this scenario.
- Use u8 instead of uint in netdev_nl_napi_set_config implementation.
- Extend the selftest to verify the existing behaviour that the PID stays valid once threaded napi is enabled. It stays valid even after disabling the threaded napi. Also verify that the same kthread(PID) is reused when threaded napi is enabled again. Will keep this behaviour as based on the discussion on v5.
v5:
- This patch was part of:
https://lore.kernel.org/netdev/Z92e2kCYXQ_RsrJh@LQ3V64L9R2/T/
It is being sent separately for the first time.
- Change threaded attribute type to uint
- Set napi threaded state when set at napi level. When set at device level overwrite the state of each napi. This is the write all symantics that is also followed by other configurations.
- Add a test to verify write all symantics.
Documentation/netlink/specs/netdev.yaml | 10 +++ Documentation/networking/napi.rst | 13 +++- include/linux/netdevice.h | 1 + include/uapi/linux/netdev.h | 1 + net/core/dev.c | 30 +++++++- net/core/dev.h | 20 ++++++ net/core/netdev-genl-gen.c | 5 +- net/core/netdev-genl.c | 10 +++ tools/include/uapi/linux/netdev.h | 1 + tools/testing/selftests/net/nl_netdev.py | 91 +++++++++++++++++++++++- 10 files changed, 175 insertions(+), 7 deletions(-)
Documentation/netlink/specs/netdev.yaml +10 -0
@@ -283,6 +283,14 @@ attribute-sets: doc: The timeout, in nanoseconds, of how long to suspend irq processing, if event polling finds events type: uint + - + 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 + in threaded polling mode. + type: uint + checks: + max: 1 - name: xsk-info attributes: []
@@ -694,6 +702,7 @@ operations: - defer-hard-irqs - gro-flush-timeout - irq-suspend-timeout + - threaded dump: request: attributes:
@@ -746,6 +755,7 @@ operations: - defer-hard-irqs - gro-flush-timeout - irq-suspend-timeout + - threaded - name: bind-tx doc: Bind dmabuf to netdev for TX
Documentation/networking/napi.rst +12 -1
@@ -444,7 +444,18 @@ dependent). The NAPI instance IDs will be assigned in the opposite order than the process IDs of the kernel threads. Threaded NAPI is controlled by writing 0/1 to the ``threaded`` file in -netdev's sysfs directory. +netdev's sysfs directory. It can also be enabled for a specific napi using +netlink interface. + +For example, using the script: + +.. code-block:: bash + + $ kernel-source/tools/net/ynl/pyynl/cli.py \ + --spec Documentation/netlink/specs/netdev.yaml \ + --do napi-set \ + --json='{"id": 66, + "threaded": 1}' .. rubric:: Footnotes
include/linux/netdevice.h +1 -0
@@ -369,6 +369,7 @@ struct napi_config {u64 irq_suspend_timeout;u32 defer_hard_irqs;cpumask_t affinity_mask; +bool threaded;unsigned int napi_id; };
include/uapi/linux/netdev.h +1 -0
@@ -134,6 +134,7 @@ enum {NETDEV_A_NAPI_DEFER_HARD_IRQS ,NETDEV_A_NAPI_GRO_FLUSH_TIMEOUT ,NETDEV_A_NAPI_IRQ_SUSPEND_TIMEOUT , +NETDEV_A_NAPI_THREADED , __NETDEV_A_NAPI_MAX,NETDEV_A_NAPI_MAX = (__NETDEV_A_NAPI_MAX -1 )
net/core/dev.c +27 -3
@@ -6963,6 +6963,31 @@ static void napi_stop_kthread(struct napi_struct *napi) napi->thread =NULL ; } +int napi_set_threaded (struct napi_struct *napi,bool threaded) +{ +if (threaded) { +if (!napi->thread) { +int err =napi_kthread_create (napi); + +if (err) +return err; + } + } + +if (napi->config) + napi->config->threaded = threaded; + +if (!threaded && napi->thread) { +napi_stop_kthread (napi); + }else { +/* Make sure kthread is created before THREADED bit is set. */ +smp_mb__before_atomic (); +assign_bit (NAPI_STATE_THREADED , &napi->state, threaded); + } + +return 0 ; +} +int dev_set_threaded (struct net_device *dev,bool threaded) {struct napi_struct *napi;
@@ -6970,9 +6995,6 @@ int dev_set_threaded(struct net_device *dev, bool threaded)netdev_assert_locked_or_invisible (dev); -if (dev->threaded == threaded) -return 0 ; -if (threaded) {list_for_each_entry (napi, &dev->napi_list, dev_list) {if (!napi->thread) {
@@ -7223,6 +7245,8 @@ static void napi_restore_config(struct napi_struct *n)napi_hash_add (n); n->config->napi_id = n->napi_id; } + +napi_set_threaded (n, n->config->threaded); }static void napi_save_config (struct napi_struct *n)
net/core/dev.h +20 -0
@@ -315,6 +315,26 @@ static inline void napi_set_irq_suspend_timeout(struct napi_struct *n,WRITE_ONCE (n->irq_suspend_timeout, timeout); } +/** +* napi_get_threaded - get the napi threaded state +* @n: napi struct to get the threaded state from +* +* Return: the per-NAPI threaded state. +*/ +static inline bool napi_get_threaded (struct napi_struct *n) +{ +return test_bit (NAPI_STATE_THREADED , &n->state); +} + +/** +* napi_set_threaded - set napi threaded state +* @n: napi struct to set the threaded state on +* @threaded: whether this napi does threaded polling +* +* Return: 0 on success and negative errno on failure. +*/ +int napi_set_threaded (struct napi_struct *n,bool threaded); +int rps_cpumask_housekeeping (struct cpumask *mask);#if defined(CONFIG_DEBUG_NET ) && defined(CONFIG_BPF_SYSCALL )
net/core/netdev-genl-gen.c +3 -2
@@ -92,11 +92,12 @@ static const struct nla_policy netdev_bind_rx_nl_policy[NETDEV_A_DMABUF_FD + 1] };/* NETDEV_CMD_NAPI_SET - do */ -static const struct nla_policy netdev_napi_set_nl_policy[NETDEV_A_NAPI_IRQ_SUSPEND_TIMEOUT +1 ] = { +static const struct nla_policy netdev_napi_set_nl_policy[NETDEV_A_NAPI_THREADED +1 ] = { [NETDEV_A_NAPI_ID ] = { .type =NLA_U32 , }, [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_CMD_BIND_TX - do */
@@ -193,7 +194,7 @@ static const struct genl_split_ops netdev_nl_ops[] = { .cmd = NETDEV_CMD_NAPI_SET, .doit = netdev_nl_napi_set_doit, .policy = netdev_napi_set_nl_policy, - .maxattr = NETDEV_A_NAPI_IRQ_SUSPEND_TIMEOUT, + .maxattr = NETDEV_A_NAPI_THREADED, .flags = GENL_ADMIN_PERM | GENL_CMD_CAP_DO, }, {
net/core/netdev-genl.c +10 -0
@@ -184,6 +184,10 @@ netdev_nl_napi_fill_one(struct sk_buff *rsp, struct napi_struct *napi,if (napi->irq >=0 &&nla_put_u32 (rsp,NETDEV_A_NAPI_IRQ , napi->irq)) goto nla_put_failure; +if (nla_put_uint (rsp,NETDEV_A_NAPI_THREADED , +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))
@@ -322,8 +326,14 @@ netdev_nl_napi_set_config(struct napi_struct *napi, struct genl_info *info) {u64 irq_suspend_timeout =0 ;u64 gro_flush_timeout =0 ; +u8 threaded =0 ;u32 defer =0 ; +if (info->attrs[NETDEV_A_NAPI_THREADED ]) { + threaded =nla_get_u8 (info->attrs[NETDEV_A_NAPI_THREADED ]); +napi_set_threaded (napi, !!threaded); + } +if (info->attrs[NETDEV_A_NAPI_DEFER_HARD_IRQS ]) { defer =nla_get_u32 (info->attrs[NETDEV_A_NAPI_DEFER_HARD_IRQS ]);napi_set_defer_hard_irqs (napi, defer);
tools/include/uapi/linux/netdev.h +1 -0
@@ -134,6 +134,7 @@ enum {NETDEV_A_NAPI_DEFER_HARD_IRQS ,NETDEV_A_NAPI_GRO_FLUSH_TIMEOUT ,NETDEV_A_NAPI_IRQ_SUSPEND_TIMEOUT , +NETDEV_A_NAPI_THREADED , __NETDEV_A_NAPI_MAX,NETDEV_A_NAPI_MAX = (__NETDEV_A_NAPI_MAX -1 )
tools/testing/selftests/net/nl_netdev.py +90 -1
119 lines not on this page · raw message
Signature · 1 line
2.50.0.rc2.692.g299adb8693-goog
Jakub Kicinski
maintainerQuoted 29 lines · On Mon, 16 Jun 2025 16:57:06 +0000 Samiullah Khawaja wrote:
A net device has a threaded sysctl that can be used to enable threaded napi polling on all of the NAPI contexts under that device. Allow enabling threaded napi polling at individual napi level using netlink.
Extend the netlink operation
napi-setand allow setting the threaded attribute of a NAPI. This will enable the threaded polling on a napi context.Add a test in
nl_netdev.pythat verifies various cases of threaded napi being set at napi and at device level.Tested
./tools/testing/selftests/net/nl_netdev.pyTAP version 13
1..7
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.dev_set_threaded
ok 6 nl_netdev.napi_set_threaded
ok 7 nl_netdev.nsim_rxq_reset_down
# Totals: pass:7 fail:0 xfail:0 xpass:0 skip:0 error:0Documentation/netlink/specs/netdev.yaml +0
-0
Documentation/netlink/specs/netdev.yaml attribute-sets: · line 288
Quoted 6 lines
Documentation/netlink/specs/netdev.yaml +3
-0@@ -283,6 +283,14 @@ attribute-sets: doc: The timeout, in nanoseconds, of how long to suspend irq processing, if event polling finds events type: uint + - + name: threaded + doc: Whether the napi is configured to operate in threaded polling
lower case napi here
Documentation/netlink/specs/netdev.yaml attribute-sets: · line 289
Quoted 1 line
Documentation/netlink/specs/netdev.yaml +1
-0+ mode. If this is set to `1` then the NAPI context operates
upper case here, let's pick one form
This is technically a form of kernel documentation, fed into Sphinx I'm a bit unclear on Sphinx and backticks, but IIUC it wants double backticks? ``1`` ? Or none at all
Quoted 12 lines
Documentation/netlink/specs/netdev.yaml +4
-0+ in threaded polling mode. + type: uint + checks: + max: 1 - name: xsk-info attributes: []Threaded NAPI is controlled by writing 0/1 to the ``threaded`` file in
Documentation/netlink/specs/netdev.yaml +2
-1-netdev's sysfs directory. +netdev's sysfs directory. It can also be enabled for a specific napi using +netlink interface.
Documentation/netlink/specs/netdev.yaml
Quoted 10 lines
Documentation/netlink/specs/netdev.yaml +10
-0+ +For example, using the script: + +.. code-block:: bash + + $ kernel-source/tools/net/ynl/pyynl/cli.py \ + --spec Documentation/netlink/specs/netdev.yaml \ + --do napi-set \ + --json='{"id": 66, + "threaded": 1}'
I wonder if it's okay now to use ynl CLI in the form that is packaged for Fedora and RHEL ? It's much more concise, tho not sure if / when other distros will catch up:
$ ynl --family netdev --do napi-set --json='{"id": 66, "threaded": 1}'Documentation/netlink/specs/netdev.yaml
Quoted 8 lines
Documentation/netlink/specs/netdev.yaml +8
-0+/** + * napi_set_threaded - set napi threaded state + * @n: napi struct to set the threaded state on + * @threaded: whether this napi does threaded polling + * + * Return: 0 on success and negative errno on failure. + */ +int napi_set_threaded(struct napi_struct *n, bool threaded);
IIRC by the kernel coding standards the kdoc if necessary should be on the definition.
Documentation/netlink/specs/netdev.yaml netdev_nl_napi_set_config(struct napi_struct *napi, struct genl_info *info) · line 333
Quoted 9 lines
Documentation/netlink/specs/netdev.yaml +3
-0@@ -322,8 +326,14 @@ netdev_nl_napi_set_config(struct napi_struct *napi, struct genl_info *info) { u64 irq_suspend_timeout = 0; u64 gro_flush_timeout = 0; + u8 threaded = 0; u32 defer = 0; + if (info->attrs[NETDEV_A_NAPI_THREADED]) { + threaded = nla_get_u8(info->attrs[NETDEV_A_NAPI_THREADED]);
nla_get_uint(), nla_get_u8 will not work on big endian
Documentation/netlink/specs/netdev.yaml netdev_nl_napi_set_config(struct napi_struct *napi, struct genl_info *info) · line 334
Quoted 1 line
Documentation/netlink/specs/netdev.yaml +1
-0+ napi_set_threaded(napi, !!threaded);
why ignore the error?
Signature · 1 line
pw-bot: cr
Quoted 40 lines · Jakub Kicinski
Quoted 29 lines · On Mon, 16 Jun 2025 16:57:06 +0000 Samiullah Khawaja wrote:
A net device has a threaded sysctl that can be used to enable threaded napi polling on all of the NAPI contexts under that device. Allow enabling threaded napi polling at individual napi level using netlink.
Extend the netlink operation
napi-setand allow setting the threaded attribute of a NAPI. This will enable the threaded polling on a napi context.Add a test in
nl_netdev.pythat verifies various cases of threaded napi being set at napi and at device level.Tested
./tools/testing/selftests/net/nl_netdev.pyTAP version 13
1..7
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.dev_set_threaded
ok 6 nl_netdev.napi_set_threaded
ok 7 nl_netdev.nsim_rxq_reset_down
# Totals: pass:7 fail:0 xfail:0 xpass:0 skip:0 error:0Documentation/netlink/specs/netdev.yaml +0
-0Documentation/netlink/specs/netdev.yaml attribute-sets: · line 288
Quoted 6 lines
Documentation/netlink/specs/netdev.yaml +3
-0@@ -283,6 +283,14 @@ attribute-sets: doc: The timeout, in nanoseconds, of how long to suspend irq processing, if event polling finds events type: uint + - + name: threaded + doc: Whether the napi is configured to operate in threaded pollinglower case napi here
Quoted 3 lines
Documentation/netlink/specs/netdev.yaml attribute-sets: · line 289
Quoted 1 line
Documentation/netlink/specs/netdev.yaml +1
-0+ mode. If this is set to `1` then the NAPI context operatesupper case here, let's pick one form
+1
Quoted 4 lines
This is technically a form of kernel documentation, fed into Sphinx I'm a bit unclear on Sphinx and backticks, but IIUC it wants double backticks? ``1`` ? Or none at all
Will remove backticks.
Quoted 28 lines
Quoted 13 lines
Documentation/netlink/specs/netdev.yaml +4
-0+ in threaded polling mode. + type: uint + checks: + max: 1 - name: xsk-info attributes: []Threaded NAPI is controlled by writing 0/1 to the ``threaded`` file in
Documentation/netlink/specs/netdev.yaml +2
-1-netdev's sysfs directory. +netdev's sysfs directory. It can also be enabled for a specific napi using +netlink interface.Documentation/netlink/specs/netdev.yaml
Quoted 10 lines
Documentation/netlink/specs/netdev.yaml +10
-0+ +For example, using the script: + +.. code-block:: bash + + $ kernel-source/tools/net/ynl/pyynl/cli.py \ + --spec Documentation/netlink/specs/netdev.yaml \ + --do napi-set \ + --json='{"id": 66, + "threaded": 1}'I wonder if it's okay now to use ynl CLI in the form that is packaged for Fedora and RHEL ? It's much more concise, tho not sure if / when other distros will catch up:
Quoted 1 line
$ ynl --family netdev --do napi-set --json='{"id": 66, "threaded": 1}'+1
Will update
Quoted 12 lines
Documentation/netlink/specs/netdev.yaml
Quoted 9 lines
Documentation/netlink/specs/netdev.yaml +8
-0+/** + * napi_set_threaded - set napi threaded state + * @n: napi struct to set the threaded state on + * @threaded: whether this napi does threaded polling + * + * Return: 0 on success and negative errno on failure. + */ +int napi_set_threaded(struct napi_struct *n, bool threaded);IIRC by the kernel coding standards the kdoc if necessary should be on the definition.
Will move
Quoted 8 lines
Quoted 8 lines
Documentation/netlink/specs/netdev.yaml +1
-0@@ -322,8 +326,14 @@ netdev_nl_napi_set_config(struct napi_struct *napi, struct genl_info *info) { u64 irq_suspend_timeout = 0; u64 gro_flush_timeout = 0; + u8 threaded = 0; u32 defer = 0;
Quoted 8 lines
Documentation/netlink/specs/netdev.yaml netdev_nl_napi_set_config(struct napi_struct *napi, struct genl_info *info) · line 333
Quoted 2 lines
Documentation/netlink/specs/netdev.yaml +2
-0+ if (info->attrs[NETDEV_A_NAPI_THREADED]) { + threaded = nla_get_u8(info->attrs[NETDEV_A_NAPI_THREADED]);
nla_get_uint(),nla_get_u8will not work on big endianDocumentation/netlink/specs/netdev.yaml netdev_nl_napi_set_config(struct napi_struct *napi, struct genl_info *info) · line 334
Quoted 1 line
Documentation/netlink/specs/netdev.yaml +1
-0+ napi_set_threaded(napi, !!threaded);why ignore the error?
Will update
Quoted 2 lines
--
pw-bot: cr