History

Merge branch 'net-netns-refcount-tracking-base-series'

Eric Dumazet says: ==================== net: netns refcount tracking, base series We have 100+ syzbot reports about netns being dismantled too soon, still unresolved as of today. We think a missing get_net() or an extra put_net() is the root cause. In order to find the bug(s), and be able to spot future ones, this patch adds CONFIG_NET_NS_REFCNT_TRACKER and new helpers to precisely pair all put_net() with corresponding get_net(). To use these helpers, each data structure owning a refcount should also use a "netns_tracker" to pair the get() and put(). Small sections of codes where the get()/put() are in sight do not need to have a tracker, because they are short lived, but in theory it is also possible to declare an on-stack tracker. v2: Include core networking patches only. ====================

Jakub Kicinski committed 4 years ago

Commit 3d204082 parents e5d75fc 11b311a12 files · +102 −23Browse files

Unified · Split

12 files
  1. drivers/net/ppp/ppp_generic.c +3 −2
  2. fs/proc/proc_net.c +16 −3
  3. include/linux/netdevice.h +1 −8
  4. include/linux/seq_file_net.h +2 −1
  5. include/net/net_namespace.h +34 −0
  6. include/net/net_trackers.h +18 −0
  7. include/net/pkt_cls.h +6 −2
  8. include/net/sock.h +2 −0
  9. net/Kconfig.debug +9 −0
  10. net/core/net_namespace.c +3 −0
  11. net/core/sock.c +3 −3
  12. net/l2tp/l2tp_debugfs.c +5 −4
drivers/net/ppp/ppp_generic.c+3−2
@@ -173,6 +173,7 @@ struct channel { · 172 unmodified lines
173 spinlock_t downl; /* protects `chan', file.xq dequeue */173 spinlock_t downl; /* protects `chan', file.xq dequeue */
174 struct ppp *ppp; /* ppp unit we're connected to */174 struct ppp *ppp; /* ppp unit we're connected to */
175 struct net *chan_net; /* the net channel belongs to */175 struct net *chan_net; /* the net channel belongs to */
176 netns_tracker ns_tracker;
176 struct list_head clist; /* link in list of channels per unit */177 struct list_head clist; /* link in list of channels per unit */
177 rwlock_t upl; /* protects `ppp' and 'bridge' */178 rwlock_t upl; /* protects `ppp' and 'bridge' */
178 struct channel __rcu *bridge; /* "bridged" ppp channel */179 struct channel __rcu *bridge; /* "bridged" ppp channel */
@@ -2879,7 +2880,7 @@ int ppp_register_net_channel(struct net *net, struct ppp_channel *chan) · 2700 unmodified lines
2879 2880
2880 pch->ppp = NULL;2881 pch->ppp = NULL;
2881 pch->chan = chan;2882 pch->chan = chan;
2882 pch->chan_net = get_net(net);2883 pch->chan_net = get_net_track(net, &pch->ns_tracker, GFP_KERNEL);
2883 chan->ppp = pch;2884 chan->ppp = pch;
2884 init_ppp_file(&pch->file, CHANNEL);2885 init_ppp_file(&pch->file, CHANNEL);
2885 pch->file.hdrlen = chan->hdrlen;2886 pch->file.hdrlen = chan->hdrlen;
@@ -3519,7 +3520,7 @@ ppp_disconnect_channel(struct channel *pch) · 633 unmodified lines
3519 */3520 */
3520static void ppp_destroy_channel(struct channel *pch)3521static void ppp_destroy_channel(struct channel *pch)
3521{3522{
3522 put_net(pch->chan_net);3523 put_net_track(pch->chan_net, &pch->ns_tracker);
3523 pch->chan_net = NULL;3524 pch->chan_net = NULL;
3524 3525
3525 atomic_dec(&channel_count);3526 atomic_dec(&channel_count);
fs/proc/proc_net.c+16−3
@@ -61,15 +61,27 @@ static int seq_open_net(struct inode *inode, struct file *file) · 60 unmodified lines
61 }61 }
62#ifdef CONFIG_NET_NS62#ifdef CONFIG_NET_NS
63 p->net = net;63 p->net = net;
64 netns_tracker_alloc(net, &p->ns_tracker, GFP_KERNEL);
64#endif65#endif
65 return 0;66 return 0;
66}67}
67 68
69static void seq_file_net_put_net(struct seq_file *seq)
70{
71#ifdef CONFIG_NET_NS
72 struct seq_net_private *priv = seq->private;
73
74 put_net_track(priv->net, &priv->ns_tracker);
75#else
76 put_net(&init_net);
77#endif
78}
79
68static int seq_release_net(struct inode *ino, struct file *f)80static int seq_release_net(struct inode *ino, struct file *f)
69{81{
70 struct seq_file *seq = f->private_data;82 struct seq_file *seq = f->private_data;
71 83
72 put_net(seq_file_net(seq));84 seq_file_net_put_net(seq);
73 seq_release_private(ino, f);85 seq_release_private(ino, f);
74 return 0;86 return 0;
75}87}
@@ -87,7 +99,8 @@ int bpf_iter_init_seq_net(void *priv_data, struct bpf_iter_aux_info *aux) · 11 unmodified lines
87#ifdef CONFIG_NET_NS99#ifdef CONFIG_NET_NS
88 struct seq_net_private *p = priv_data;100 struct seq_net_private *p = priv_data;
89 101
90 p->net = get_net(current->nsproxy->net_ns);102 p->net = get_net_track(current->nsproxy->net_ns, &p->ns_tracker,
103 GFP_KERNEL);
91#endif104#endif
92 return 0;105 return 0;
93}106}
@@ -97,7 +110,7 @@ void bpf_iter_fini_seq_net(void *priv_data) · 3 unmodified lines
97#ifdef CONFIG_NET_NS110#ifdef CONFIG_NET_NS
98 struct seq_net_private *p = priv_data;111 struct seq_net_private *p = priv_data;
99 112
100 put_net(p->net);113 put_net_track(p->net, &p->ns_tracker);
101#endif114#endif
102}115}
103 116
include/linux/netdevice.h+1−8
@@ -48,7 +48,7 @@ · 47 unmodified lines
48#include <uapi/linux/pkt_cls.h>48#include <uapi/linux/pkt_cls.h>
49#include <linux/hashtable.h>49#include <linux/hashtable.h>
50#include <linux/rbtree.h>50#include <linux/rbtree.h>
51#include <linux/ref_tracker.h>51#include <net/net_trackers.h>
52 52
53struct netpoll_info;53struct netpoll_info;
54struct device;54struct device;
@@ -300,13 +300,6 @@ enum netdev_state_t { · 245 unmodified lines
300 __LINK_STATE_TESTING,300 __LINK_STATE_TESTING,
301};301};
302 302
303
304#ifdef CONFIG_NET_DEV_REFCNT_TRACKER
305typedef struct ref_tracker *netdevice_tracker;
306#else
307typedef struct {} netdevice_tracker;
308#endif
309
310struct gro_list {303struct gro_list {
311 struct list_head list;304 struct list_head list;
312 int count;305 int count;
include/linux/seq_file_net.h+2−1
@@ -9,7 +9,8 @@ extern struct net init_net; · 8 unmodified lines
9 9
10struct seq_net_private {10struct seq_net_private {
11#ifdef CONFIG_NET_NS11#ifdef CONFIG_NET_NS
12 struct net *net;12 struct net *net;
13 netns_tracker ns_tracker;
13#endif14#endif
14};15};
15 16
include/net/net_namespace.h+34−0
@@ -34,6 +34,7 @@ · 33 unmodified lines
34#include <net/netns/smc.h>34#include <net/netns/smc.h>
35#include <net/netns/bpf.h>35#include <net/netns/bpf.h>
36#include <net/netns/mctp.h>36#include <net/netns/mctp.h>
37#include <net/net_trackers.h>
37#include <linux/ns_common.h>38#include <linux/ns_common.h>
38#include <linux/idr.h>39#include <linux/idr.h>
39#include <linux/skbuff.h>40#include <linux/skbuff.h>
@@ -87,6 +88,7 @@ struct net { · 47 unmodified lines
87 struct idr netns_ids;88 struct idr netns_ids;
88 89
89 struct ns_common ns;90 struct ns_common ns;
91 struct ref_tracker_dir refcnt_tracker;
90 92
91 struct list_head dev_base_head;93 struct list_head dev_base_head;
92 struct proc_dir_entry *proc_net;94 struct proc_dir_entry *proc_net;
@@ -240,6 +242,7 @@ void ipx_unregister_sysctl(void); · 147 unmodified lines
240#ifdef CONFIG_NET_NS242#ifdef CONFIG_NET_NS
241void __put_net(struct net *net);243void __put_net(struct net *net);
242 244
245/* Try using get_net_track() instead */
243static inline struct net *get_net(struct net *net)246static inline struct net *get_net(struct net *net)
244{247{
245 refcount_inc(&net->ns.count);248 refcount_inc(&net->ns.count);
@@ -258,6 +261,7 @@ static inline struct net *maybe_get_net(struct net *net) · 12 unmodified lines
258 return net;261 return net;
259}262}
260 263
264/* Try using put_net_track() instead */
261static inline void put_net(struct net *net)265static inline void put_net(struct net *net)
262{266{
263 if (refcount_dec_and_test(&net->ns.count))267 if (refcount_dec_and_test(&net->ns.count))
@@ -308,6 +312,36 @@ static inline int check_net(const struct net *net) · 44 unmodified lines
308#endif312#endif
309 313
310 314
315static inline void netns_tracker_alloc(struct net *net,
316 netns_tracker *tracker, gfp_t gfp)
317{
318#ifdef CONFIG_NET_NS_REFCNT_TRACKER
319 ref_tracker_alloc(&net->refcnt_tracker, tracker, gfp);
320#endif
321}
322
323static inline void netns_tracker_free(struct net *net,
324 netns_tracker *tracker)
325{
326#ifdef CONFIG_NET_NS_REFCNT_TRACKER
327 ref_tracker_free(&net->refcnt_tracker, tracker);
328#endif
329}
330
331static inline struct net *get_net_track(struct net *net,
332 netns_tracker *tracker, gfp_t gfp)
333{
334 get_net(net);
335 netns_tracker_alloc(net, tracker, gfp);
336 return net;
337}
338
339static inline void put_net_track(struct net *net, netns_tracker *tracker)
340{
341 netns_tracker_free(net, tracker);
342 put_net(net);
343}
344
311typedef struct {345typedef struct {
312#ifdef CONFIG_NET_NS346#ifdef CONFIG_NET_NS
313 struct net *net;347 struct net *net;
include/net/net_trackers.h+18−0
@@ -0,0 +1,18 @@
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef __NET_NET_TRACKERS_H
3#define __NET_NET_TRACKERS_H
4#include <linux/ref_tracker.h>
5
6#ifdef CONFIG_NET_DEV_REFCNT_TRACKER
7typedef struct ref_tracker *netdevice_tracker;
8#else
9typedef struct {} netdevice_tracker;
10#endif
11
12#ifdef CONFIG_NET_NS_REFCNT_TRACKER
13typedef struct ref_tracker *netns_tracker;
14#else
15typedef struct {} netns_tracker;
16#endif
17
18#endif /* __NET_NET_TRACKERS_H */
include/net/pkt_cls.h+6−2
@@ -202,7 +202,8 @@ struct tcf_exts { · 201 unmodified lines
202 __u32 type; /* for backward compat(TCA_OLD_COMPAT) */202 __u32 type; /* for backward compat(TCA_OLD_COMPAT) */
203 int nr_actions;203 int nr_actions;
204 struct tc_action **actions;204 struct tc_action **actions;
205 struct net *net;205 struct net *net;
206 netns_tracker ns_tracker;
206#endif207#endif
207 /* Map to export classifier specific extension TLV types to the208 /* Map to export classifier specific extension TLV types to the
208 * generic extensions API. Unsupported extensions must be set to 0.209 * generic extensions API. Unsupported extensions must be set to 0.
@@ -218,6 +219,7 @@ static inline int tcf_exts_init(struct tcf_exts *exts, struct net *net, · 9 unmodified lines
218 exts->type = 0;219 exts->type = 0;
219 exts->nr_actions = 0;220 exts->nr_actions = 0;
220 exts->net = net;221 exts->net = net;
222 netns_tracker_alloc(net, &exts->ns_tracker, GFP_KERNEL);
221 exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),223 exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
222 GFP_KERNEL);224 GFP_KERNEL);
223 if (!exts->actions)225 if (!exts->actions)
@@ -236,6 +238,8 @@ static inline bool tcf_exts_get_net(struct tcf_exts *exts) · 12 unmodified lines
236{238{
237#ifdef CONFIG_NET_CLS_ACT239#ifdef CONFIG_NET_CLS_ACT
238 exts->net = maybe_get_net(exts->net);240 exts->net = maybe_get_net(exts->net);
241 if (exts->net)
242 netns_tracker_alloc(exts->net, &exts->ns_tracker, GFP_KERNEL);
239 return exts->net != NULL;243 return exts->net != NULL;
240#else244#else
241 return true;245 return true;
@@ -246,7 +250,7 @@ static inline void tcf_exts_put_net(struct tcf_exts *exts) · 4 unmodified lines
246{250{
247#ifdef CONFIG_NET_CLS_ACT251#ifdef CONFIG_NET_CLS_ACT
248 if (exts->net)252 if (exts->net)
249 put_net(exts->net);253 put_net_track(exts->net, &exts->ns_tracker);
250#endif254#endif
251}255}
252 256
include/net/sock.h+2−0
@@ -350,6 +350,7 @@ struct bpf_local_storage; · 349 unmodified lines
350 * @sk_txtime_deadline_mode: set deadline mode for SO_TXTIME350 * @sk_txtime_deadline_mode: set deadline mode for SO_TXTIME
351 * @sk_txtime_report_errors: set report errors mode for SO_TXTIME351 * @sk_txtime_report_errors: set report errors mode for SO_TXTIME
352 * @sk_txtime_unused: unused txtime flags352 * @sk_txtime_unused: unused txtime flags
353 * @ns_tracker: tracker for netns reference
353 */354 */
354struct sock {355struct sock {
355 /*356 /*
@@ -538,6 +539,7 @@ struct sock { · 182 unmodified lines
538 struct bpf_local_storage __rcu *sk_bpf_storage;539 struct bpf_local_storage __rcu *sk_bpf_storage;
539#endif540#endif
540 struct rcu_head sk_rcu;541 struct rcu_head sk_rcu;
542 netns_tracker ns_tracker;
541};543};
542 544
543enum sk_pacing {545enum sk_pacing {
net/Kconfig.debug+9−0
@@ -8,3 +8,12 @@ config NET_DEV_REFCNT_TRACKER · 7 unmodified lines
8 help8 help
9 Enable debugging feature to track device references.9 Enable debugging feature to track device references.
10 This adds memory and cpu costs.10 This adds memory and cpu costs.
11
12config NET_NS_REFCNT_TRACKER
13 bool "Enable networking namespace refcount tracking"
14 depends on DEBUG_KERNEL && STACKTRACE_SUPPORT
15 select REF_TRACKER
16 default n
17 help
18 Enable debugging feature to track netns references.
19 This adds memory and cpu costs.
net/core/net_namespace.c+3−0
@@ -311,6 +311,8 @@ static __net_init int setup_net(struct net *net, struct user_namespace *user_ns) · 310 unmodified lines
311 LIST_HEAD(net_exit_list);311 LIST_HEAD(net_exit_list);
312 312
313 refcount_set(&net->ns.count, 1);313 refcount_set(&net->ns.count, 1);
314 ref_tracker_dir_init(&net->refcnt_tracker, 128);
315
314 refcount_set(&net->passive, 1);316 refcount_set(&net->passive, 1);
315 get_random_bytes(&net->hash_mix, sizeof(u32));317 get_random_bytes(&net->hash_mix, sizeof(u32));
316 preempt_disable();318 preempt_disable();
@@ -635,6 +637,7 @@ static DECLARE_WORK(net_cleanup_work, cleanup_net); · 318 unmodified lines
635 637
636void __put_net(struct net *net)638void __put_net(struct net *net)
637{639{
640 ref_tracker_dir_exit(&net->refcnt_tracker);
638 /* Cleanup the network namespace in process context */641 /* Cleanup the network namespace in process context */
639 if (llist_add(&net->cleanup_list, &cleanup_list))642 if (llist_add(&net->cleanup_list, &cleanup_list))
640 queue_work(netns_wq, &net_cleanup_work);643 queue_work(netns_wq, &net_cleanup_work);
net/core/sock.c+3−3
@@ -1983,7 +1983,7 @@ struct sock *sk_alloc(struct net *net, int family, gfp_t priority, · 1982 unmodified lines
1983 sock_lock_init(sk);1983 sock_lock_init(sk);
1984 sk->sk_net_refcnt = kern ? 0 : 1;1984 sk->sk_net_refcnt = kern ? 0 : 1;
1985 if (likely(sk->sk_net_refcnt)) {1985 if (likely(sk->sk_net_refcnt)) {
1986 get_net(net);1986 get_net_track(net, &sk->ns_tracker, priority);
1987 sock_inuse_add(net, 1);1987 sock_inuse_add(net, 1);
1988 }1988 }
1989 1989
@@ -2039,7 +2039,7 @@ static void __sk_destruct(struct rcu_head *head) · 49 unmodified lines
2039 put_pid(sk->sk_peer_pid);2039 put_pid(sk->sk_peer_pid);
2040 2040
2041 if (likely(sk->sk_net_refcnt))2041 if (likely(sk->sk_net_refcnt))
2042 put_net(sock_net(sk));2042 put_net_track(sock_net(sk), &sk->ns_tracker);
2043 sk_prot_free(sk->sk_prot_creator, sk);2043 sk_prot_free(sk->sk_prot_creator, sk);
2044}2044}
2045 2045
@@ -2126,7 +2126,7 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority) · 80 unmodified lines
2126 2126
2127 /* SANITY */2127 /* SANITY */
2128 if (likely(newsk->sk_net_refcnt)) {2128 if (likely(newsk->sk_net_refcnt)) {
2129 get_net(sock_net(newsk));2129 get_net_track(sock_net(newsk), &newsk->ns_tracker, priority);
2130 sock_inuse_add(sock_net(newsk), 1);2130 sock_inuse_add(sock_net(newsk), 1);
2131 }2131 }
2132 sk_node_init(&newsk->sk_node);2132 sk_node_init(&newsk->sk_node);
net/l2tp/l2tp_debugfs.c+5−4
@@ -32,7 +32,8 @@ · 31 unmodified lines
32static struct dentry *rootdir;32static struct dentry *rootdir;
33 33
34struct l2tp_dfs_seq_data {34struct l2tp_dfs_seq_data {
35 struct net *net;35 struct net *net;
36 netns_tracker ns_tracker;
36 int tunnel_idx; /* current tunnel */37 int tunnel_idx; /* current tunnel */
37 int session_idx; /* index of session within current tunnel */38 int session_idx; /* index of session within current tunnel */
38 struct l2tp_tunnel *tunnel;39 struct l2tp_tunnel *tunnel;
@@ -281,7 +282,7 @@ static int l2tp_dfs_seq_open(struct inode *inode, struct file *file) · 242 unmodified lines
281 rc = PTR_ERR(pd->net);282 rc = PTR_ERR(pd->net);
282 goto err_free_pd;283 goto err_free_pd;
283 }284 }
284 285 netns_tracker_alloc(pd->net, &pd->ns_tracker, GFP_KERNEL);
285 rc = seq_open(file, &l2tp_dfs_seq_ops);286 rc = seq_open(file, &l2tp_dfs_seq_ops);
286 if (rc)287 if (rc)
287 goto err_free_net;288 goto err_free_net;
@@ -293,7 +294,7 @@ out: · 5 unmodified lines
293 return rc;294 return rc;
294 295
295err_free_net:296err_free_net:
296 put_net(pd->net);297 put_net_track(pd->net, &pd->ns_tracker);
297err_free_pd:298err_free_pd:
298 kfree(pd);299 kfree(pd);
299 goto out;300 goto out;
@@ -307,7 +308,7 @@ static int l2tp_dfs_seq_release(struct inode *inode, struct file *file) · 7 unmodified lines
307 seq = file->private_data;308 seq = file->private_data;
308 pd = seq->private;309 pd = seq->private;
309 if (pd->net)310 if (pd->net)
310 put_net(pd->net);311 put_net_track(pd->net, &pd->ns_tracker);
311 kfree(pd);312 kfree(pd);
312 seq_release(inode, file);313 seq_release(inode, file);
313 314

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t