Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

net: Update threaded state in napi config in netif_set_threaded

Commit 2677010e7793 ("Add support to set NAPI threaded for individual
NAPI") added support to enable/disable threaded napi using netlink. This
also extended the napi config save/restore functionality to set the napi
threaded state. This breaks netdev reset for drivers that use napi
threaded at device level and also use napi config save/restore on
napi_disable/napi_enable. Basically on netdev with napi threaded enabled
at device level, a napi_enable call will get stuck trying to stop the
napi kthread. This is because the napi->config->threaded is set to
disabled when threaded is enabled at device level.

The issue can be reproduced on virtio-net device using qemu. To
reproduce the issue run following,

echo 1 > /sys/class/net/threaded
ethtool -L eth0 combined 1

Update the threaded state in napi config in netif_set_threaded and add a
new test that verifies this scenario.

Tested on qemu with virtio-net:
NETIF=eth0 ./tools/testing/selftests/drivers/net/napi_threaded.py
TAP version 13
1..2
ok 1 napi_threaded.change_num_queues
ok 2 napi_threaded.enable_dev_threaded_disable_napi_threaded
# Totals: pass:2 fail:0 xfail:0 xpass:0 skip:0 error:0

Fixes: 2677010e7793 ("Add support to set NAPI threaded for individual NAPI")
Signed-off-by: Samiullah Khawaja <skhawaja@google.com>
Link: https://patch.msgid.link/20250804164457.2494390-1-skhawaja@google.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Samiullah Khawaja and committed by
Jakub Kicinski
e6d76268 8d22aea8

+121 -17
+9 -17
net/core/dev.c
··· 6978 6978 if (napi->config) 6979 6979 napi->config->threaded = threaded; 6980 6980 6981 + /* Setting/unsetting threaded mode on a napi might not immediately 6982 + * take effect, if the current napi instance is actively being 6983 + * polled. In this case, the switch between threaded mode and 6984 + * softirq mode will happen in the next round of napi_schedule(). 6985 + * This should not cause hiccups/stalls to the live traffic. 6986 + */ 6981 6987 if (!threaded && napi->thread) { 6982 6988 napi_stop_kthread(napi); 6983 6989 } else { ··· 7017 7011 7018 7012 WRITE_ONCE(dev->threaded, threaded); 7019 7013 7020 - /* Make sure kthread is created before THREADED bit 7021 - * is set. 7022 - */ 7023 - smp_mb__before_atomic(); 7024 - 7025 - /* Setting/unsetting threaded mode on a napi might not immediately 7026 - * take effect, if the current napi instance is actively being 7027 - * polled. In this case, the switch between threaded mode and 7028 - * softirq mode will happen in the next round of napi_schedule(). 7029 - * This should not cause hiccups/stalls to the live traffic. 7030 - */ 7031 - list_for_each_entry(napi, &dev->napi_list, dev_list) { 7032 - if (!threaded && napi->thread) 7033 - napi_stop_kthread(napi); 7034 - else 7035 - assign_bit(NAPI_STATE_THREADED, &napi->state, threaded); 7036 - } 7014 + /* The error should not occur as the kthreads are already created. */ 7015 + list_for_each_entry(napi, &dev->napi_list, dev_list) 7016 + WARN_ON_ONCE(napi_set_threaded(napi, threaded)); 7037 7017 7038 7018 return err; 7039 7019 }
+1
tools/testing/selftests/drivers/net/Makefile
··· 11 11 12 12 TEST_PROGS := \ 13 13 napi_id.py \ 14 + napi_threaded.py \ 14 15 netcons_basic.sh \ 15 16 netcons_cmdline.sh \ 16 17 netcons_fragmented_msg.sh \
+111
tools/testing/selftests/drivers/net/napi_threaded.py
··· 1 + #!/usr/bin/env python3 2 + # SPDX-License-Identifier: GPL-2.0 3 + 4 + """ 5 + Test napi threaded states. 6 + """ 7 + 8 + from lib.py import ksft_run, ksft_exit 9 + from lib.py import ksft_eq, ksft_ne, ksft_ge 10 + from lib.py import NetDrvEnv, NetdevFamily 11 + from lib.py import cmd, defer, ethtool 12 + 13 + 14 + def _assert_napi_threaded_enabled(nl, napi_id) -> None: 15 + napi = nl.napi_get({'id': napi_id}) 16 + ksft_eq(napi['threaded'], 'enabled') 17 + ksft_ne(napi.get('pid'), None) 18 + 19 + 20 + def _assert_napi_threaded_disabled(nl, napi_id) -> None: 21 + napi = nl.napi_get({'id': napi_id}) 22 + ksft_eq(napi['threaded'], 'disabled') 23 + ksft_eq(napi.get('pid'), None) 24 + 25 + 26 + def _set_threaded_state(cfg, threaded) -> None: 27 + cmd(f"echo {threaded} > /sys/class/net/{cfg.ifname}/threaded") 28 + 29 + 30 + def _setup_deferred_cleanup(cfg) -> None: 31 + combined = ethtool(f"-l {cfg.ifname}", json=True)[0].get("combined", 0) 32 + ksft_ge(combined, 2) 33 + defer(ethtool, f"-L {cfg.ifname} combined {combined}") 34 + 35 + threaded = cmd(f"cat /sys/class/net/{cfg.ifname}/threaded").stdout 36 + defer(_set_threaded_state, cfg, threaded) 37 + 38 + 39 + def enable_dev_threaded_disable_napi_threaded(cfg, nl) -> None: 40 + """ 41 + Test that when napi threaded is enabled at device level and 42 + then disabled at napi level for one napi, the threaded state 43 + of all napis is preserved after a change in number of queues. 44 + """ 45 + 46 + napis = nl.napi_get({'ifindex': cfg.ifindex}, dump=True) 47 + ksft_ge(len(napis), 2) 48 + 49 + napi0_id = napis[0]['id'] 50 + napi1_id = napis[1]['id'] 51 + 52 + _setup_deferred_cleanup(cfg) 53 + 54 + # set threaded 55 + _set_threaded_state(cfg, 1) 56 + 57 + # check napi threaded is set for both napis 58 + _assert_napi_threaded_enabled(nl, napi0_id) 59 + _assert_napi_threaded_enabled(nl, napi1_id) 60 + 61 + # disable threaded for napi1 62 + nl.napi_set({'id': napi1_id, 'threaded': 'disabled'}) 63 + 64 + cmd(f"ethtool -L {cfg.ifname} combined 1") 65 + cmd(f"ethtool -L {cfg.ifname} combined 2") 66 + _assert_napi_threaded_enabled(nl, napi0_id) 67 + _assert_napi_threaded_disabled(nl, napi1_id) 68 + 69 + 70 + def change_num_queues(cfg, nl) -> None: 71 + """ 72 + Test that when napi threaded is enabled at device level, 73 + the napi threaded state is preserved after a change in 74 + number of queues. 75 + """ 76 + 77 + napis = nl.napi_get({'ifindex': cfg.ifindex}, dump=True) 78 + ksft_ge(len(napis), 2) 79 + 80 + napi0_id = napis[0]['id'] 81 + napi1_id = napis[1]['id'] 82 + 83 + _setup_deferred_cleanup(cfg) 84 + 85 + # set threaded 86 + _set_threaded_state(cfg, 1) 87 + 88 + # check napi threaded is set for both napis 89 + _assert_napi_threaded_enabled(nl, napi0_id) 90 + _assert_napi_threaded_enabled(nl, napi1_id) 91 + 92 + cmd(f"ethtool -L {cfg.ifname} combined 1") 93 + cmd(f"ethtool -L {cfg.ifname} combined 2") 94 + 95 + # check napi threaded is set for both napis 96 + _assert_napi_threaded_enabled(nl, napi0_id) 97 + _assert_napi_threaded_enabled(nl, napi1_id) 98 + 99 + 100 + def main() -> None: 101 + """ Ksft boiler plate main """ 102 + 103 + with NetDrvEnv(__file__, queue_count=2) as cfg: 104 + ksft_run([change_num_queues, 105 + enable_dev_threaded_disable_napi_threaded], 106 + args=(cfg, NetdevFamily())) 107 + ksft_exit() 108 + 109 + 110 + if __name__ == "__main__": 111 + main()