net: Don't allow disabling napi kthread mode while napi instances are disabled
Subsystems (2)
PATCH v2 1/1 net: Don't allow disabling napi kthread mode while napi instances are disabled
Trying to disable the napi threaded state while the napi instance is disabled will hang forever in napi_stop_kthread(), as it waits for NAPIF_STATE_SCHED_THREADED to clear.
This won't happen until the next ____napi_schedule() call, which won't happen as it's disabled.
This happens in 2 instances :
- Drivers that create but don't use napi instances (stmmac's rxtx napi for AF_XDP for example), here it's a driver bug
- Drivers that create their napi instances in .probe(). Here, setting threaded off while the interface is down will hang :
ip link set eth0 down
echo 1 > /sys/class/net/eth0/threaded
echo 0 > /sys/class/net/eth0/threaded
-> hang
Checking on netif_running() isn't enough, as napi instances may be transiently disabled when changing the MTU for example, so let's check the NAPIF_STATE_NPSVC flag instead, that's clear when the napi instance is enabled.
This fix only addresses the second case. For the first case where drivers have unused napi instances, the "threaded" mode can't be switched off once enabled, these drivers will need fixing. As this hasn't been seen before on said drivers, I guess it's OK as it used to just freeze the system.
This was tested on mvpp2 (probe-time napi_add) and stmmac.
---
V2:
- Different approach as per Jakub's review, just don't allow setting the napi threaded mode while interface is down.
I'm unsure about the correctness of this, to me it seems that the NAPI_STATE_NPSVC flag indicates the condition we're interested in (is napi instance enabled or not ?), but I may be missing something obvious :(
V1:
https://lore.kernel.org/r/[email protected]
net/core/dev.c | 6 ++++++ 1 file changed, 6 insertions(+)
net/core/dev.c +6 -0
@@ -7315,6 +7315,12 @@ int netif_set_threaded(struct net_device *dev, } } } + }else { +list_for_each_entry (napi, &dev->napi_list, dev_list) { +if (napi->thread && +test_bit (NAPI_STATE_NPSVC , &napi->state)) +return -EBUSY ; + } }WRITE_ONCE (dev->threaded, threaded);
Signature · 1 line
2.55.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.
Critical: 0 · High: 1 · Medium: 2 · Low: 1
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] The fix is incomplete.
- [Medium] The new guard in netif_set_threaded() (net/core/dev.c:7352-7354) treats NAPI_STATE_NPSVC as meaning 'this…
- [Medium] After this patch, writing 0 to /sys/class/net/<iface>/threaded (or calling netif_set_threaded()/dev_set_threaded() with 0) returns…
- [Low] The commit message gets the hang mechanism wrong.
--
net: Don't allow disabling napi kthread mode while napi instances are disabled
This patch makes netif_set_threaded() return -EBUSY for a request to disable threaded mode when any NAPI instance with a kthread has NAPI_STATE_NPSVC set. The patch uses that bit to mean the instance is disabled. The goal is to avoid a hang in napi_stop_kthread() when threaded mode is turned off on a down interface whose driver added its NAPIs at probe time.
Quoted 3 lines
Trying to disable the napi threaded state while the napi instance is disabled will hang forever in
napi_stop_kthread(), as it waits forNAPIF_STATE_SCHED_THREADEDto clear.
[Severity: Low]
Is this the right loop? On a disabled NAPI, SCHED is set and SCHED_THREADED is clear. That means the hang is in the first wait loop of napi_stop_kthread():
net/core/dev.c:napi_stop_kthread() {
...
if ((val & NAPIF_STATE_SCHED_THREADED) ||
!(val & NAPIF_STATE_SCHED)) {
new = val & (~(NAPIF_STATE_THREADED |
NAPIF_STATE_THREADED_BUSY_POLL));
} else {
msleep(20);
continue;
}
...
}The second loop is the one that waits for SCHED_THREADED to clear. It would exit right away, but the code never gets there.
The real blocking condition is that napi_disable() or netif_napi_add() leaves SCHED set, and only napi_enable_locked() can clear it. That function needs the netdev lock, which the caller already holds.
Could the description be updated to match? With the right root cause it is easier to see whether other paths reach the same state (see the rollback comment below).
net/core/dev.c int netif_set_threaded(struct net_device *dev, · line 7353
Quoted 12 lines
net/core/dev.c +4
-0@@ -7347,6 +7347,12 @@ int netif_set_threaded(struct net_device *dev, } } } + }else { +list_for_each_entry (napi, &dev->napi_list, dev_list) { +if (napi->thread && +test_bit (NAPI_STATE_NPSVC , &napi->state))
[Severity: Medium]
Can NAPI_STATE_NPSVC be set here on a NAPI that is enabled? The bit is documented as a netpoll servicing flag:
include/linux/netdevice.h:
NAPI_STATE_NPSVC, /* Netpoll - don't dequeue from poll_list */
poll_one_napi() sets it for a short time around the poll, without holding the netdev instance lock:
net/core/netpoll.c:poll_one_napi() {
if (test_and_set_bit(NAPI_STATE_NPSVC, &napi->state))
return;
...
work = napi->poll(napi, 0);
...
clear_bit(NAPI_STATE_NPSVC, &napi->state);
}netpoll_poll_dev() only takes ni->dev_lock with down_trylock(), and nothing serializes it against netif_set_threaded().
Suppose netconsole or bonding is polling the device when 0 is written to /sys/class/net/<iface>/threaded. Would this return -EBUSY even though every NAPI is enabled and disabling would be safe?
A disabled NAPI always has NPSVC set. The reverse doesn't hold: NPSVC being set does not mean the NAPI is disabled.
net/core/dev.c int netif_set_threaded(struct net_device *dev, · line 7322
Quoted 1 line
net/core/dev.c +1
-0+return -EBUSY ;
[Severity: Medium]
Does this change the documented behaviour of /sys/class/net/<iface>/threaded? For drivers that add their NAPIs at probe time, writing 0 now fails with -EBUSY on any down interface. Enabling has no matching check:
ip link set eth0 down
echo 1 > /sys/class/net/eth0/threaded (succeeds, creates kthreads)
echo 0 > /sys/class/net/eth0/threaded (-EBUSY until link up)
Drivers that call netif_threaded_enable() at probe, such as atl1c and ravb, cannot leave threaded mode before the first link-up. Drivers whose NAPIs are added but never enabled can never leave it.
Documentation/ABI/testing/sysfs-class-net says the attribute lets the user "enable/disable threaded mode for all napi belonging to this device, without the need to do device up/down", and it lists no error cases.
Should that document be updated? Alternatively, should enabling be refused in the same state so the two directions behave the same way?
The V2 changelog note ("just don't allow setting the napi threaded mode while interface is down") doesn't match the code either. The code only refuses disabling.
net/core/dev.c int netif_set_threaded(struct net_device *dev, · line 7326
Quoted 4 lines
net/core/dev.c +1
-0+ } }WRITE_ONCE (dev->threaded, threaded);
[Severity: High]
Can the same hang still be reached through the rollback in the enable path? The new check only runs in the else branch, which handles an explicit disable. In the if (threaded) branch, napi_kthread_create() can succeed for some NAPIs and then fail for a later one:
net/core/dev.c:netif_set_threaded() {
if (threaded) {
list_for_each_entry(napi, &dev->napi_list, dev_list) {
if (!napi->thread) {
err = napi_kthread_create(napi);
if (err) {
threaded = NETDEV_NAPI_THREADED_DISABLED;
break;
}
}
}
}
...
list_for_each_entry(napi, &dev->napi_list, dev_list)
WARN_ON_ONCE(napi_set_threaded(napi, threaded));
...
}kthread_run() in napi_kthread_create() can fail with -ENOMEM. It can also fail with -EINTR if a fatal signal reaches the writer during the killable wait in kthread creation.
When that happens, threaded becomes DISABLED and napi_set_threaded(napi,
0) runs on every listed NAPI without the NPSVC check. Some NAPIs may be disabled but have a thread (SCHED and NPSVC set, SCHED_THREADED clear). The thread may have been created moments earlier in the same loop, or left over from before.
Each of those NAPIs would go into napi_stop_kthread() and spin in the first msleep(20) loop with the netdev lock held. Only napi_enable_locked() can clear SCHED, and it needs the same lock.
The first way to reach this is on a driver that adds its NAPIs at probe time (the mvpp2 case from the commit message), when a kthread creation fails part-way through:
ip link set eth0 down
echo 1 > /sys/class/net/eth0/threaded
The second is a probe-time netif_threaded_enable() after netif_napi_add(). For example, ravb_probe() adds the RAVB_BE and RAVB_NC NAPIs and then calls netif_threaded_enable(). If creating the second kthread fails, would probe hang here?
Would it be better to handle disabled NAPIs in the stop path itself, napi_stop_kthread() or napi_set_threaded(), rather than only guarding the explicit disable branch?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261006141119.85562-1-maxime.chevallier%40bootlin.com
Quoted 116 lines · On 10/8/26 04:11, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.
Critical: 0 · High: 1 · Medium: 2 · Low: 1
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] The fix is incomplete.
- [Medium] The new guard innetif_set_threaded()(net/core/dev.c:7352-7354) treatsNAPI_STATE_NPSVCas meaning 'this…
- [Medium] After this patch, writing 0 to /sys/class/net/<iface>/threaded (or calling netif_set_threaded()/dev_set_threaded() with 0) returns…
- [Low] The commit message gets the hang mechanism wrong.--
net: Don't allow disabling napi kthread mode while napi instances are disabled
This patch makes
netif_set_threaded()return-EBUSYfor a request to disable threaded mode when any NAPI instance with a kthread hasNAPI_STATE_NPSVCset. The patch uses that bit to mean the instance is disabled. The goal is to avoid a hang innapi_stop_kthread()when threaded mode is turned off on a down interface whose driver added its NAPIs at probe time.Quoted 3 lines
Trying to disable the napi threaded state while the napi instance is disabled will hang forever in
napi_stop_kthread(), as it waits forNAPIF_STATE_SCHED_THREADEDto clear.[Severity: Low]
Is this the right loop? On a disabled NAPI, SCHED is set andSCHED_THREADEDis clear. That means the hang is in the first wait loop ofnapi_stop_kthread():net/core/dev.c:napi_stop_kthread() { ... if ((val & NAPIF_STATE_SCHED_THREADED) || !(val & NAPIF_STATE_SCHED)) { new = val & (~(NAPIF_STATE_THREADED | NAPIF_STATE_THREADED_BUSY_POLL)); } else { msleep(20); continue; } ... }The second loop is the one that waits for
SCHED_THREADEDto clear. It would exit right away, but the code never gets there.The real blocking condition is that
napi_disable()ornetif_napi_add()leaves SCHED set, and onlynapi_enable_locked()can clear it. That function needs the netdev lock, which the caller already holds.Could the description be updated to match? With the right root cause it is easier to see whether other paths reach the same state (see the rollback comment below).
net/core/dev.c int netif_set_threaded(struct net_device *dev, · line 7353
Quoted 12 lines
net/core/dev.c +4
-0@@ -7347,6 +7347,12 @@ int netif_set_threaded(struct net_device *dev, } } } + }else { +list_for_each_entry (napi, &dev->napi_list, dev_list) { +if (napi->thread && +test_bit (NAPI_STATE_NPSVC , &napi->state))[Severity: Medium]
CanNAPI_STATE_NPSVCbe set here on a NAPI that is enabled? The bit is documented as a netpoll servicing flag:
include/linux/netdevice.h:
NAPI_STATE_NPSVC, /* Netpoll - don't dequeue frompoll_list*/
poll_one_napi()sets it for a short time around the poll, without holding the netdev instance lock:net/core/netpoll.c:poll_one_napi() { if (test_and_set_bit(NAPI_STATE_NPSVC, &napi->state)) return; ... work = napi->poll(napi, 0); ... clear_bit(NAPI_STATE_NPSVC, &napi->state); }
netpoll_poll_dev()only takesni->dev_lockwithdown_trylock(), and nothing serializes it againstnetif_set_threaded().Suppose netconsole or bonding is polling the device when 0 is written to /sys/class/net/<iface>/threaded. Would this return
-EBUSYeven though every NAPI is enabled and disabling would be safe?A disabled NAPI always has NPSVC set. The reverse doesn't hold: NPSVC being set does not mean the NAPI is disabled.
net/core/dev.c int netif_set_threaded(struct net_device *dev, · line 7322
Quoted 1 line
net/core/dev.c +1
-0+return -EBUSY ;[Severity: Medium]
Does this change the documented behaviour of /sys/class/net/<iface>/threaded? For drivers that add their NAPIs at probe time, writing 0 now fails with-EBUSYon any down interface. Enabling has no matching check:ip link set eth0 down
echo 1 >/sys/class/net/eth0/threaded(succeeds, creates kthreads)
echo 0 >/sys/class/net/eth0/threaded(-EBUSYuntil link up)
Quoted 8 lines
Drivers that call
netif_threaded_enable()at probe, such as atl1c and ravb, cannot leave threaded mode before the first link-up. Drivers whose NAPIs are added but never enabled can never leave it.
Documentation/ABI/testing/sysfs-class-netsays the attribute lets the user "enable/disable threaded mode for all napi belonging to this device, without the need to do device up/down", and it lists no error cases.
Ah true :/ hmmm ok is it even OK to have the check at disable time when there's this sentence in the doc then ?
Quoted 48 lines
net/core/dev.c int netif_set_threaded(struct net_device *dev, · line 7326
Quoted 5 lines
net/core/dev.c +1
-0+ } }WRITE_ONCE (dev->threaded, threaded);[Severity: High]
Can the same hang still be reached through the rollback in the enable path? The new check only runs in the else branch, which handles an explicit disable. In the if (threaded) branch,napi_kthread_create()can succeed for some NAPIs and then fail for a later one:net/core/dev.c:netif_set_threaded() { if (threaded) { list_for_each_entry(napi, &dev->napi_list, dev_list) { if (!napi->thread) { err = napi_kthread_create(napi); if (err) { threaded = NETDEV_NAPI_THREADED_DISABLED; break; } } } } ... list_for_each_entry(napi, &dev->napi_list, dev_list) WARN_ON_ONCE(napi_set_threaded(napi, threaded)); ... }
kthread_run()innapi_kthread_create()can fail with-ENOMEM. It can also fail with-EINTRif a fatal signal reaches the writer during the killable wait in kthread creation.When that happens, threaded becomes DISABLED and napi_set_threaded(napi,
0) runs on every listed NAPI without the NPSVC check. Some NAPIs may be disabled but have a thread (SCHED and NPSVC set,SCHED_THREADEDclear). The thread may have been created moments earlier in the same loop, or left over from before.Each of those NAPIs would go into
napi_stop_kthread()and spin in the first msleep(20) loop with the netdev lock held. Onlynapi_enable_locked()can clear SCHED, and it needs the same lock.The first way to reach this is on a driver that adds its NAPIs at probe time (the mvpp2 case from the commit message), when a kthread creation fails part-way through:
Quoted 7 lines
ip link set eth0 down
echo 1 >/sys/class/net/eth0/threadedThe second is a probe-time
netif_threaded_enable()afternetif_napi_add(). For example,ravb_probe()adds theRAVB_BEandRAVB_NCNAPIs and then callsnetif_threaded_enable(). If creating the second kthread fails, would probe hang here?
Is it a realistic thing to encounter though ? I'm ok with iterating on that, but this seems to be something very unlikely ?
Quoted 4 lines
Would it be better to handle disabled NAPIs in the stop path itself,
napi_stop_kthread()ornapi_set_threaded(), rather than only guarding the explicit disable branch?
Hmmm maybe yes
Maxime
Quoted 1 line
Paolo Abeni
maintainerQuoted 6 lines · Maxime Chevallier
Quoted 3 lines · On 10/8/26 04:11, [email protected] wrote:
Would it be better to handle disabled NAPIs in the stop path itself,
napi_stop_kthread()ornapi_set_threaded(), rather than only guarding the explicit disable branch?Hmmm maybe yes
My personal take is that the complexity of addressing the issue for good at the core level sounds like a good argument to fix this at the device level, one device at the time, as needed.
Otherwise I'll probably got for the suggested path above.
Thanks,
Paolo
Hi Paolo,
Quoted 10 lines · Paolo Abeni
Quoted 6 lines · Maxime Chevallier
Quoted 3 lines · On 10/8/26 04:11, [email protected] wrote:
Would it be better to handle disabled NAPIs in the stop path itself,
napi_stop_kthread()ornapi_set_threaded(), rather than only guarding the explicit disable branch?Hmmm maybe yes
My personal take is that the complexity of addressing the issue for good at the core level sounds like a good argument to fix this at the device level, one device at the time, as needed.
Fixing that in drivers isn't enough however. The visible problem of hanging while disabling threaded mode if the interface is down would be addressed, but not the corner cases such as "disabling threaded mode while changing the MTU" for example.
And this is a lot harder to test :(
Quoted 2 lines
Otherwise I'll probably got for the suggested path above.
My brain is becoming a bit more operational now that the conference flu I caught last week is healing, I'll spin another round see if I can get that addressed.
Maxime