net: Use a dedicated flag to track disabled napi instances

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, let's use a
dedicated flag to track if the instance is disabled.

This allows immediately disabling the kthread when the instance is
disabled.

This was tested on mvpp2 (probe-time napi_add) and stmmac.

Fixes: 689883de94 ("net: stop napi kthreads when THREADED napi is disabled")
Tested-by: Daniel Machon <daniel.machon@microchip.com>
Signed-off-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
Link: https://patch.msgid.link/20261029153130.121204-1-maxime.chevallier@bootlin.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
Maxime Chevallier 2026-10-29 16:31:29 +01:00 committed by Jakub Kicinski
parent ed6c53b27b
commit 8dc87fc5d6
2 changed files with 15 additions and 2 deletions

View File

@ -440,6 +440,7 @@ enum {
NAPI_STATE_HAS_NOTIFIER, /* Napi has an IRQ notifier */
NAPI_STATE_THREADED_BUSY_POLL, /* The threaded NAPI poller will busy poll */
NAPI_STATE_PERCPU, /* NAPI handler is run per-CPU - incompatible with threaded mode */
NAPI_STATE_DISABLED, /* NAPI is disabled */
};
enum {
@ -456,6 +457,7 @@ enum {
NAPIF_STATE_HAS_NOTIFIER = BIT(NAPI_STATE_HAS_NOTIFIER),
NAPIF_STATE_THREADED_BUSY_POLL = BIT(NAPI_STATE_THREADED_BUSY_POLL),
NAPIF_STATE_PERCPU = BIT(NAPI_STATE_PERCPU),
NAPIF_STATE_DISABLED = BIT(NAPI_STATE_DISABLED),
};
enum gro_result {

View File

@ -7389,6 +7389,13 @@ static void napi_stop_kthread(struct napi_struct *napi)
{
unsigned long val, new;
/* Instance is disabled, we can immediately stop the kthread */
if (test_bit(NAPI_STATE_DISABLED, &napi->state)) {
clear_bit(NAPI_STATE_THREADED, &napi->state);
clear_bit(NAPI_STATE_THREADED_BUSY_POLL, &napi->state);
goto stop;
}
/* Wait until the napi STATE_THREADED is unset. */
while (true) {
val = READ_ONCE(napi->state);
@ -7419,6 +7426,7 @@ static void napi_stop_kthread(struct napi_struct *napi)
msleep(20);
}
stop:
kthread_stop(napi->thread);
napi->thread = NULL;
}
@ -7814,6 +7822,7 @@ void netif_napi_add_weight_locked(struct net_device *dev,
napi->list_owner = -1;
set_bit(NAPI_STATE_SCHED, &napi->state);
set_bit(NAPI_STATE_NPSVC, &napi->state);
set_bit(NAPI_STATE_DISABLED, &napi->state);
netif_napi_dev_list_add(dev, napi);
/* default settings from sysfs are applied to all NAPIs. any per-NAPI
@ -7850,7 +7859,8 @@ void napi_disable_locked(struct napi_struct *n)
val = READ_ONCE(n->state);
}
new = val | NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC;
new = val | NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC |
NAPIF_STATE_DISABLED;
new &= ~(NAPIF_STATE_THREADED |
NAPIF_STATE_THREADED_BUSY_POLL |
NAPIF_STATE_PREFER_BUSY_POLL);
@ -7895,7 +7905,8 @@ void napi_enable_locked(struct napi_struct *n)
do {
BUG_ON(!test_bit(NAPI_STATE_SCHED, &val));
new = val & ~(NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC);
new = val & ~(NAPIF_STATE_SCHED | NAPIF_STATE_NPSVC |
NAPIF_STATE_DISABLED);
if (n->dev->threaded && n->thread)
new |= NAPIF_STATE_THREADED;
} while (!try_cmpxchg(&n->state, &val, new));