Skip to content

Commit 1bb86cf

Browse files
committed
net: protect threaded status of NAPI with netdev_lock()
Now that NAPI instances can't come and go without holding netdev->lock we can trivially switch from rtnl_lock() to netdev_lock() for setting netdev->threaded via sysfs. Note that since we do not lock netdev_lock around sysfs calls in the core we don't have to "trylock" like we do with rtnl_lock. Reviewed-by: Eric Dumazet <edumazet@google.com> Reviewed-by: Kuniyuki Iwashima <kuniyu@amazon.com> Link: https://patch.msgid.link/20250115035319.559603-9-kuba@kernel.org Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent eeeec1d commit 1bb86cf

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

include/linux/netdevice.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ struct napi_struct {
384384
int rx_count; /* length of rx_list */
385385
unsigned int napi_id; /* protected by netdev_lock */
386386
struct hrtimer timer;
387-
struct task_struct *thread;
387+
struct task_struct *thread; /* protected by netdev_lock */
388388
unsigned long gro_flush_timeout;
389389
unsigned long irq_suspend_timeout;
390390
u32 defer_hard_irqs;
@@ -2451,11 +2451,13 @@ struct net_device {
24512451
* Drivers are free to use it for other protection.
24522452
*
24532453
* Protects:
2454-
* @napi_list, @net_shaper_hierarchy, @reg_state
2454+
* @napi_list, @net_shaper_hierarchy, @reg_state, @threaded
24552455
*
24562456
* Partially protects (writers must hold both @lock and rtnl_lock):
24572457
* @up
24582458
*
2459+
* Also protects some fields in struct napi_struct.
2460+
*
24592461
* Ordering: take after rtnl_lock.
24602462
*/
24612463
struct mutex lock;
@@ -2697,6 +2699,13 @@ static inline void netdev_assert_locked(struct net_device *dev)
26972699
lockdep_assert_held(&dev->lock);
26982700
}
26992701

2702+
static inline void netdev_assert_locked_or_invisible(struct net_device *dev)
2703+
{
2704+
if (dev->reg_state == NETREG_REGISTERED ||
2705+
dev->reg_state == NETREG_UNREGISTERING)
2706+
netdev_assert_locked(dev);
2707+
}
2708+
27002709
static inline void netif_napi_set_irq(struct napi_struct *napi, int irq)
27012710
{
27022711
napi->irq = irq;

net/core/dev.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6785,6 +6785,8 @@ int dev_set_threaded(struct net_device *dev, bool threaded)
67856785
struct napi_struct *napi;
67866786
int err = 0;
67876787

6788+
netdev_assert_locked_or_invisible(dev);
6789+
67886790
if (dev->threaded == threaded)
67896791
return 0;
67906792

net/core/net-sysfs.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static const char fmt_uint[] = "%u\n";
3636
static const char fmt_ulong[] = "%lu\n";
3737
static const char fmt_u64[] = "%llu\n";
3838

39-
/* Caller holds RTNL or RCU */
39+
/* Caller holds RTNL, netdev->lock or RCU */
4040
static inline int dev_isalive(const struct net_device *dev)
4141
{
4242
return READ_ONCE(dev->reg_state) <= NETREG_REGISTERED;
@@ -108,6 +108,36 @@ static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
108108
return ret;
109109
}
110110

111+
/* Same as netdev_store() but takes netdev_lock() instead of rtnl_lock() */
112+
static ssize_t
113+
netdev_lock_store(struct device *dev, struct device_attribute *attr,
114+
const char *buf, size_t len,
115+
int (*set)(struct net_device *, unsigned long))
116+
{
117+
struct net_device *netdev = to_net_dev(dev);
118+
struct net *net = dev_net(netdev);
119+
unsigned long new;
120+
int ret;
121+
122+
if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
123+
return -EPERM;
124+
125+
ret = kstrtoul(buf, 0, &new);
126+
if (ret)
127+
return ret;
128+
129+
netdev_lock(netdev);
130+
131+
if (dev_isalive(netdev)) {
132+
ret = (*set)(netdev, new);
133+
if (ret == 0)
134+
ret = len;
135+
}
136+
netdev_unlock(netdev);
137+
138+
return ret;
139+
}
140+
111141
NETDEVICE_SHOW_RO(dev_id, fmt_hex);
112142
NETDEVICE_SHOW_RO(dev_port, fmt_dec);
113143
NETDEVICE_SHOW_RO(addr_assign_type, fmt_dec);
@@ -638,7 +668,7 @@ static ssize_t threaded_store(struct device *dev,
638668
struct device_attribute *attr,
639669
const char *buf, size_t len)
640670
{
641-
return netdev_store(dev, attr, buf, len, modify_napi_threaded);
671+
return netdev_lock_store(dev, attr, buf, len, modify_napi_threaded);
642672
}
643673
static DEVICE_ATTR_RW(threaded);
644674

0 commit comments

Comments
 (0)