Skip to content

Commit 919067c

Browse files
edumazetdavem330
authored andcommitted
net: add CONFIG_PCPU_DEV_REFCNT
I was working on a syzbot issue, claiming one device could not be dismantled because its refcount was -1 unregister_netdevice: waiting for sit0 to become free. Usage count = -1 It would be nice if syzbot could trigger a warning at the time this reference count became negative. This patch adds CONFIG_PCPU_DEV_REFCNT options which defaults to per cpu variables (as before this patch) on SMP builds. v2: free_dev label in alloc_netdev_mqs() is moved to avoid a compiler warning (-Wunused-label), as reported by kernel test robot <lkp@intel.com> Signed-off-by: Eric Dumazet <edumazet@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 942f0c6 commit 919067c

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎include/linux/netdevice.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,7 +2092,12 @@ struct net_device {
20922092
u32 proto_down_reason;
20932093

20942094
struct list_head todo_list;
2095+
2096+
#ifdef CONFIG_PCPU_DEV_REFCNT
20952097
int __percpu *pcpu_refcnt;
2098+
#else
2099+
refcount_t dev_refcnt;
2100+
#endif
20962101

20972102
struct list_head link_watch_list;
20982103

@@ -4044,7 +4049,11 @@ void netdev_run_todo(void);
40444049
*/
40454050
static inline void dev_put(struct net_device *dev)
40464051
{
4052+
#ifdef CONFIG_PCPU_DEV_REFCNT
40474053
this_cpu_dec(*dev->pcpu_refcnt);
4054+
#else
4055+
refcount_dec(&dev->dev_refcnt);
4056+
#endif
40484057
}
40494058

40504059
/**
@@ -4055,7 +4064,11 @@ static inline void dev_put(struct net_device *dev)
40554064
*/
40564065
static inline void dev_hold(struct net_device *dev)
40574066
{
4067+
#ifdef CONFIG_PCPU_DEV_REFCNT
40584068
this_cpu_inc(*dev->pcpu_refcnt);
4069+
#else
4070+
refcount_inc(&dev->dev_refcnt);
4071+
#endif
40594072
}
40604073

40614074
/* Carrier loss detection, dial on demand. The functions netif_carrier_on

‎net/Kconfig‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,14 @@ source "net/l3mdev/Kconfig"
245245
source "net/qrtr/Kconfig"
246246
source "net/ncsi/Kconfig"
247247

248+
config PCPU_DEV_REFCNT
249+
bool "Use percpu variables to maintain network device refcount"
250+
depends on SMP
251+
default y
252+
help
253+
network device refcount are using per cpu variables if this option is set.
254+
This can be forced to N to detect underflows (with a performance drop).
255+
248256
config RPS
249257
bool
250258
depends on SMP && SYSFS

‎net/core/dev.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10312,11 +10312,15 @@ EXPORT_SYMBOL(register_netdev);
1031210312

1031310313
int netdev_refcnt_read(const struct net_device *dev)
1031410314
{
10315+
#ifdef CONFIG_PCPU_DEV_REFCNT
1031510316
int i, refcnt = 0;
1031610317

1031710318
for_each_possible_cpu(i)
1031810319
refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
1031910320
return refcnt;
10321+
#else
10322+
return refcount_read(&dev->dev_refcnt);
10323+
#endif
1032010324
}
1032110325
EXPORT_SYMBOL(netdev_refcnt_read);
1032210326

@@ -10674,9 +10678,11 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
1067410678
dev = PTR_ALIGN(p, NETDEV_ALIGN);
1067510679
dev->padded = (char *)dev - (char *)p;
1067610680

10681+
#ifdef CONFIG_PCPU_DEV_REFCNT
1067710682
dev->pcpu_refcnt = alloc_percpu(int);
1067810683
if (!dev->pcpu_refcnt)
1067910684
goto free_dev;
10685+
#endif
1068010686

1068110687
if (dev_addr_init(dev))
1068210688
goto free_pcpu;
@@ -10740,8 +10746,10 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
1074010746
return NULL;
1074110747

1074210748
free_pcpu:
10749+
#ifdef CONFIG_PCPU_DEV_REFCNT
1074310750
free_percpu(dev->pcpu_refcnt);
1074410751
free_dev:
10752+
#endif
1074510753
netdev_freemem(dev);
1074610754
return NULL;
1074710755
}
@@ -10783,8 +10791,10 @@ void free_netdev(struct net_device *dev)
1078310791
list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
1078410792
netif_napi_del(p);
1078510793

10794+
#ifdef CONFIG_PCPU_DEV_REFCNT
1078610795
free_percpu(dev->pcpu_refcnt);
1078710796
dev->pcpu_refcnt = NULL;
10797+
#endif
1078810798
free_percpu(dev->xdp_bulkq);
1078910799
dev->xdp_bulkq = NULL;
1079010800

0 commit comments

Comments
 (0)