net: device tracking improvements
Subsystems (3)
v2 cover net: device tracking improvements
From: Eric Dumazet <[email protected]>
Main goal of this series is to be able to detect the following case which apparently is still haunting us.
dev_hold_track(dev, tracker_1, GFP_ATOMIC);
dev_hold(dev);
dev_put(dev);
dev_put(dev); // Should complain loudly here.dev_put_track(dev, tracker_1); // instead of here (as before this series)
v2: third patch:
I replaced the dev_put() in linkwatch_do_dev() with __dev_put().
Eric Dumazet (3):
ref_tracker: implement use-after-free detection
ref_tracker: add a count of untracked references
net: refine dev_put()/dev_hold() debugging
include/linux/netdevice.h | 69 ++++++++++++++++++++++++------------- include/linux/ref_tracker.h | 4 +++ lib/ref_tracker.c | 17 ++++++++- net/core/dev.c | 2 +- net/core/link_watch.c | 6 ++-- 5 files changed, 70 insertions(+), 28 deletions(-)
Signature · 1 line
2.35.0.263.gb82422642f-goog
PATCH v2 1/3 ref_tracker: implement use-after-free detection
From: Eric Dumazet <[email protected]>
Whenever ref_tracker_dir_init() is called, mark the struct ref_tracker_dir as dead.
Test the dead status from ref_tracker_alloc() and ref_tracker_free()
This should detect buggy dev_put()/dev_hold() happening too late in netdevice dismantle process.
--- include/linux/ref_tracker.h | 2 ++ lib/ref_tracker.c | 5 +++++ 2 files changed, 7 insertions(+)
include/linux/ref_tracker.h +2 -0
@@ -13,6 +13,7 @@ struct ref_tracker_dir {spinlock_t lock;unsigned int quarantine_avail;refcount_t untracked; +bool dead;struct list_head list;/* List of active trackers */ struct list_head quarantine;/* List of dead trackers */ #endif
@@ -26,6 +27,7 @@ static inline void ref_tracker_dir_init(struct ref_tracker_dir *dir,INIT_LIST_HEAD (&dir->quarantine);spin_lock_init (&dir->lock); dir->quarantine_avail = quarantine_count; + dir->dead = false;refcount_set (&dir->untracked,1 );stack_depot_init (); }
lib/ref_tracker.c +5 -0
@@ -20,6 +20,7 @@ void ref_tracker_dir_exit(struct ref_tracker_dir *dir)unsigned long flags;bool leak = false; + dir->dead = true;spin_lock_irqsave (&dir->lock, flags);list_for_each_entry_safe (tracker, n, &dir->quarantine, head) {list_del (&tracker->head);
@@ -72,6 +73,8 @@ int ref_tracker_alloc(struct ref_tracker_dir *dir,gfp_t gfp_mask = gfp;unsigned long flags; +WARN_ON_ONCE (dir->dead); +if (gfp & __GFP_DIRECT_RECLAIM) gfp_mask |= __GFP_NOFAIL; *trackerp = tracker =kzalloc (sizeof (*tracker), gfp_mask);
@@ -100,6 +103,8 @@ int ref_tracker_free(struct ref_tracker_dir *dir,unsigned int nr_entries;unsigned long flags; +WARN_ON_ONCE (dir->dead); +if (!tracker) {refcount_dec (&dir->untracked);return -EEXIST ;
Signature · 1 line
2.35.0.263.gb82422642f-goog
PATCH v2 2/3 ref_tracker: add a count of untracked references
In reply to Eric DumazetFrom: Eric Dumazet <[email protected]>
We are still chasing a netdev refcount imbalance, and we suspect we have one rogue dev_put() that is consuming a reference taken from a dev_hold_track()
To detect this case, allow ref_tracker_alloc() and ref_tracker_free() to be called with a NULL @trackerp parameter, and use a dedicated refcount_t just for them.
--- include/linux/ref_tracker.h | 2 ++ lib/ref_tracker.c | 12 +++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-)
include/linux/ref_tracker.h +2 -0
@@ -13,6 +13,7 @@ struct ref_tracker_dir {spinlock_t lock;unsigned int quarantine_avail;refcount_t untracked; +refcount_t no_tracker;bool dead;struct list_head list;/* List of active trackers */ struct list_head quarantine;/* List of dead trackers */
@@ -29,6 +30,7 @@ static inline void ref_tracker_dir_init(struct ref_tracker_dir *dir, dir->quarantine_avail = quarantine_count; dir->dead = false;refcount_set (&dir->untracked,1 ); +refcount_set (&dir->no_tracker,1 );stack_depot_init (); }
lib/ref_tracker.c +11 -1
@@ -38,6 +38,7 @@ void ref_tracker_dir_exit(struct ref_tracker_dir *dir)spin_unlock_irqrestore (&dir->lock, flags);WARN_ON_ONCE (leak);WARN_ON_ONCE (refcount_read (&dir->untracked) !=1 ); +WARN_ON_ONCE (refcount_read (&dir->no_tracker) !=1 ); }EXPORT_SYMBOL (ref_tracker_dir_exit);
@@ -75,6 +76,10 @@ int ref_tracker_alloc(struct ref_tracker_dir *dir,WARN_ON_ONCE (dir->dead); +if (!trackerp) { +refcount_inc (&dir->no_tracker); +return 0 ; + }if (gfp & __GFP_DIRECT_RECLAIM) gfp_mask |= __GFP_NOFAIL; *trackerp = tracker =kzalloc (sizeof (*tracker), gfp_mask);
@@ -98,13 +103,18 @@ int ref_tracker_free(struct ref_tracker_dir *dir, struct ref_tracker **trackerp) {unsigned long entries[REF_TRACKER_STACK_ENTRIES ]; -struct ref_tracker *tracker = *trackerp;depot_stack_handle_t stack_handle; +struct ref_tracker *tracker;unsigned int nr_entries;unsigned long flags;WARN_ON_ONCE (dir->dead); +if (!trackerp) { +refcount_dec (&dir->no_tracker); +return 0 ; + } + tracker = *trackerp;if (!tracker) {refcount_dec (&dir->untracked);return -EEXIST ;
Signature · 1 line
2.35.0.263.gb82422642f-goog
PATCH v2 3/3 net: refine dev_put()/dev_hold() debugging
In reply to Eric DumazetFrom: Eric Dumazet <[email protected]>
We are still chasing some syzbot reports where we think a rogue dev_put() is called with no corresponding prior dev_hold(). Unfortunately it eats a reference on dev->dev_refcnt taken by innocent dev_hold_track(), meaning that the refcount saturation splat comes too late to be useful.
Make sure that 'not tracked' dev_put() and dev_hold() better use CONFIG_NET_DEV_REFCNT_TRACKER=y debug infrastructure:
Prior patch in the series allowed ref_tracker_alloc() and ref_tracker_free() to be called with a NULL @trackerp parameter, and to use a separate refcount only to detect too many put() even in the following case:
dev_hold_track(dev, tracker_1, GFP_ATOMIC);
dev_hold(dev);
dev_put(dev);
dev_put(dev); // Should complain loudly here.dev_put_track(dev, tracker_1); // instead of here
Add clarification about netdev_tracker_alloc() role.
v2: I replaced the dev_put() in linkwatch_do_dev()
with __dev_put() because callers called netdev_tracker_free().
--- include/linux/netdevice.h | 69 ++++++++++++++++++++++++++------------- net/core/dev.c | 2 +- net/core/link_watch.c | 6 ++-- 3 files changed, 50 insertions(+), 27 deletions(-)
include/linux/netdevice.h +46 -23
@@ -3817,14 +3817,7 @@ extern unsigned int netdev_budget_usecs;/* Called by rtnetlink.c:rtnl_unlock() */ void netdev_run_todo (void ); -/** -* dev_put - release reference to device -* @dev: network device -* -* Release reference to device to allow it to be freed. -* Try using dev_put_track() instead. -*/ -static inline void dev_put (struct net_device *dev) +static inline void __dev_put (struct net_device *dev) {if (dev) {#ifdef CONFIG_PCPU_DEV_REFCNT
@@ -3835,14 +3828,7 @@ static inline void dev_put(struct net_device *dev) } } -/** -* dev_hold - get reference to device -* @dev: network device -* -* Hold reference to device to keep it from being freed. -* Try using dev_hold_track() instead. -*/ -static inline void dev_hold (struct net_device *dev) +static inline void __dev_hold (struct net_device *dev) {if (dev) {#ifdef CONFIG_PCPU_DEV_REFCNT
@@ -3853,11 +3839,24 @@ static inline void dev_hold(struct net_device *dev) } } +static inline void __netdev_tracker_alloc (struct net_device *dev, +netdevice_tracker *tracker, +gfp_t gfp) +{ +#ifdef CONFIG_NET_DEV_REFCNT_TRACKER +ref_tracker_alloc (&dev->refcnt_tracker, tracker, gfp); +#endif +} + +/* netdev_tracker_alloc() can upgrade a prior untracked reference +* taken by dev_get_by_name()/dev_get_by_index() to a tracked one. +*/ static inline void netdev_tracker_alloc (struct net_device *dev,netdevice_tracker *tracker,gfp_t gfp) {#ifdef CONFIG_NET_DEV_REFCNT_TRACKER -ref_tracker_alloc (&dev->refcnt_tracker, tracker, gfp); +refcount_dec (&dev->refcnt_tracker.no_tracker); +__netdev_tracker_alloc (dev, tracker, gfp);#endif }
@@ -3873,8 +3872,8 @@ static inline void dev_hold_track(struct net_device *dev, netdevice_tracker *tracker, gfp_t gfp) {if (dev) { -dev_hold (dev); -netdev_tracker_alloc (dev, tracker, gfp); +__dev_hold (dev); +__netdev_tracker_alloc (dev, tracker, gfp); } }
@@ -3883,10 +3882,34 @@ static inline void dev_put_track(struct net_device *dev, {if (dev) {netdev_tracker_free (dev, tracker); -dev_put (dev); +__dev_put (dev); } } +/** +* dev_hold - get reference to device +* @dev: network device +* +* Hold reference to device to keep it from being freed. +* Try using dev_hold_track() instead. +*/ +static inline void dev_hold (struct net_device *dev) +{ +dev_hold_track (dev,NULL ,GFP_ATOMIC ); +} + +/** +* dev_put - release reference to device +* @dev: network device +* +* Release reference to device to allow it to be freed. +* Try using dev_put_track() instead. +*/ +static inline void dev_put (struct net_device *dev) +{ +dev_put_track (dev,NULL ); +} + static inline void dev_replace_track(struct net_device *odev, struct net_device *ndev, netdevice_tracker *tracker,
@@ -3895,11 +3918,11 @@ static inline void dev_replace_track(struct net_device *odev,if (odev)netdev_tracker_free (odev, tracker); -dev_hold (ndev); -dev_put (odev); +__dev_hold (ndev); +__dev_put (odev);if (ndev) -netdev_tracker_alloc (ndev, tracker, gfp); +__netdev_tracker_alloc (ndev, tracker, gfp); }/* Carrier loss detection, dial on demand. The functions netif_carrier_on
net/core/dev.c +1 -1
@@ -10172,7 +10172,7 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name, dev->pcpu_refcnt =alloc_percpu (int);if (!dev->pcpu_refcnt) goto free_dev; -dev_hold (dev); +__dev_hold (dev);#else refcount_set (&dev->dev_refcnt,1 );#endif
net/core/link_watch.c +3 -3
@@ -166,10 +166,10 @@ static void linkwatch_do_dev(struct net_device *dev)netdev_state_change (dev); } -/* Note: our callers are responsible for -* calling netdev_tracker_free(). +/* Note: our callers are responsible for calling netdev_tracker_free(). +* This is the reason we use __dev_put() instead of dev_put(). */ -dev_put (dev); +__dev_put (dev); }static void __linkwatch_run_queue (int urgent_only)
Signature · 1 line
2.35.0.263.gb82422642f-goog
This series was applied to netdev/net-next.git (master) by David S. Miller <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (master) by David S. Miller <[email protected]>:
Quoted 5 lines · On Fri, 4 Feb 2022 14:42:34 -0800 you wrote:
From: Eric Dumazet <[email protected]>
Main goal of this series is to be able to detect the following case which apparently is still haunting us.
Quoted 7 lines
dev_hold_track(dev, tracker_1, GFP_ATOMIC); dev_hold(dev); dev_put(dev); dev_put(dev); // Should complain loudly here.
dev_put_track(dev, tracker_1); // instead of here (as before this series)[...]
Here is the summary with links:
- [v2,net-next,1/3] ref_tracker: implement use-after-free detection
https://git.kernel.org/netdev/net-next/c/e3ececfe668f - [v2,net-next,2/3] ref_tracker: add a count of untracked references https://git.kernel.org/netdev/net-next/c/8fd5522f44dc - [v2,net-next,3/3] net: refine dev_put()/dev_hold() debugging https://git.kernel.org/netdev/net-next/c/4c6c11ea0f7b
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html