Skip to content

Commit e6d7626

Browse files
samikhawajakuba-moo
authored andcommitted
net: Update threaded state in napi config in netif_set_threaded
Commit 2677010 ("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: 2677010 ("Add support to set NAPI threaded for individual NAPI") Signed-off-by: Samiullah Khawaja <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 8d22aea commit e6d7626

File tree

3 files changed

+121
-17
lines changed

3 files changed

+121
-17
lines changed

net/core/dev.c

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6978,6 +6978,12 @@ int napi_set_threaded(struct napi_struct *napi,
69786978
if (napi->config)
69796979
napi->config->threaded = threaded;
69806980

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+
*/
69816987
if (!threaded && napi->thread) {
69826988
napi_stop_kthread(napi);
69836989
} else {
@@ -7011,23 +7017,9 @@ int netif_set_threaded(struct net_device *dev,
70117017

70127018
WRITE_ONCE(dev->threaded, threaded);
70137019

7014-
/* Make sure kthread is created before THREADED bit
7015-
* is set.
7016-
*/
7017-
smp_mb__before_atomic();
7018-
7019-
/* Setting/unsetting threaded mode on a napi might not immediately
7020-
* take effect, if the current napi instance is actively being
7021-
* polled. In this case, the switch between threaded mode and
7022-
* softirq mode will happen in the next round of napi_schedule().
7023-
* This should not cause hiccups/stalls to the live traffic.
7024-
*/
7025-
list_for_each_entry(napi, &dev->napi_list, dev_list) {
7026-
if (!threaded && napi->thread)
7027-
napi_stop_kthread(napi);
7028-
else
7029-
assign_bit(NAPI_STATE_THREADED, &napi->state, threaded);
7030-
}
7020+
/* The error should not occur as the kthreads are already created. */
7021+
list_for_each_entry(napi, &dev->napi_list, dev_list)
7022+
WARN_ON_ONCE(napi_set_threaded(napi, threaded));
70317023

70327024
return err;
70337025
}

tools/testing/selftests/drivers/net/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ TEST_GEN_FILES := \
1111

1212
TEST_PROGS := \
1313
napi_id.py \
14+
napi_threaded.py \
1415
netcons_basic.sh \
1516
netcons_cmdline.sh \
1617
netcons_fragmented_msg.sh \
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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()

0 commit comments

Comments
 (0)