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
173173 spinlock_t downl; /* protects `chan', file.xq dequeue */
174174 struct ppp *ppp; /* ppp unit we're connected to */
175175 struct net *chan_net; /* the net channel belongs to */
176+ netns_tracker ns_tracker;
176177 struct list_head clist; /* link in list of channels per unit */
177178 rwlock_t upl; /* protects `ppp' and 'bridge' */
178179 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
28792880
28802881 pch->ppp = NULL;
28812882 pch->chan = chan;
2882− pch->chan_net = get_net(net);
2883+ pch->chan_net = get_net_track(net, &pch->ns_tracker, GFP_KERNEL);
28832884 chan->ppp = pch;
28842885 init_ppp_file(&pch->file, CHANNEL);
28852886 pch->file.hdrlen = chan->hdrlen;
@@ -3519,7 +3520,7 @@ ppp_disconnect_channel(struct channel *pch) · 633 unmodified lines
35193520 */
35203521 static void ppp_destroy_channel(struct channel *pch)
35213522 {
3522− put_net(pch->chan_net);
3523+ put_net_track(pch->chan_net, &pch->ns_tracker);
35233524 pch->chan_net = NULL;
35243525
35253526 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
6161 }
6262 #ifdef CONFIG_NET_NS
6363 p->net = net;
64+ netns_tracker_alloc(net, &p->ns_tracker, GFP_KERNEL);
6465 #endif
6566 return 0;
6667 }
6768
69+static 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+
6880 static int seq_release_net(struct inode *ino, struct file *f)
6981 {
7082 struct seq_file *seq = f->private_data;
7183
72− put_net(seq_file_net(seq));
84+ seq_file_net_put_net(seq);
7385 seq_release_private(ino, f);
7486 return 0;
7587 }
@@ -87,7 +99,8 @@ int bpf_iter_init_seq_net(void *priv_data, struct bpf_iter_aux_info *aux) · 11 unmodified lines
8799 #ifdef CONFIG_NET_NS
88100 struct seq_net_private *p = priv_data;
89101
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);
91104 #endif
92105 return 0;
93106 }
@@ -97,7 +110,7 @@ void bpf_iter_fini_seq_net(void *priv_data) · 3 unmodified lines
97110 #ifdef CONFIG_NET_NS
98111 struct seq_net_private *p = priv_data;
99112
100− put_net(p->net);
113+ put_net_track(p->net, &p->ns_tracker);
101114 #endif
102115 }
103116
include/linux/netdevice.h+1−8
@@ -48,7 +48,7 @@ · 47 unmodified lines
4848 #include <uapi/linux/pkt_cls.h>
4949 #include <linux/hashtable.h>
5050 #include <linux/rbtree.h>
51−#include <linux/ref_tracker.h>
51+#include <net/net_trackers.h>
5252
5353 struct netpoll_info;
5454 struct device;
@@ -300,13 +300,6 @@ enum netdev_state_t { · 245 unmodified lines
300300 __LINK_STATE_TESTING,
301301 };
302302
303−
304−#ifdef CONFIG_NET_DEV_REFCNT_TRACKER
305−typedef struct ref_tracker *netdevice_tracker;
306−#else
307−typedef struct {} netdevice_tracker;
308−#endif
309−
310303 struct gro_list {
311304 struct list_head list;
312305 int count;
include/linux/seq_file_net.h+2−1
@@ -9,7 +9,8 @@ extern struct net init_net; · 8 unmodified lines
99
1010 struct seq_net_private {
1111 #ifdef CONFIG_NET_NS
12− struct net *net;
12+ struct net *net;
13+ netns_tracker ns_tracker;
1314 #endif
1415 };
1516
include/net/net_namespace.h+34−0
@@ -34,6 +34,7 @@ · 33 unmodified lines
3434 #include <net/netns/smc.h>
3535 #include <net/netns/bpf.h>
3636 #include <net/netns/mctp.h>
37+#include <net/net_trackers.h>
3738 #include <linux/ns_common.h>
3839 #include <linux/idr.h>
3940 #include <linux/skbuff.h>
@@ -87,6 +88,7 @@ struct net { · 47 unmodified lines
8788 struct idr netns_ids;
8889
8990 struct ns_common ns;
91+ struct ref_tracker_dir refcnt_tracker;
9092
9193 struct list_head dev_base_head;
9294 struct proc_dir_entry *proc_net;
@@ -240,6 +242,7 @@ void ipx_unregister_sysctl(void); · 147 unmodified lines
240242 #ifdef CONFIG_NET_NS
241243 void __put_net(struct net *net);
242244
245+/* Try using get_net_track() instead */
243246 static inline struct net *get_net(struct net *net)
244247 {
245248 refcount_inc(&net->ns.count);
@@ -258,6 +261,7 @@ static inline struct net *maybe_get_net(struct net *net) · 12 unmodified lines
258261 return net;
259262 }
260263
264+/* Try using put_net_track() instead */
261265 static inline void put_net(struct net *net)
262266 {
263267 if (refcount_dec_and_test(&net->ns.count))
@@ -308,6 +312,36 @@ static inline int check_net(const struct net *net) · 44 unmodified lines
308312 #endif
309313
310314
315+static 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+
323+static 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+
331+static 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+
339+static inline void put_net_track(struct net *net, netns_tracker *tracker)
340+{
341+ netns_tracker_free(net, tracker);
342+ put_net(net);
343+}
344+
311345 typedef struct {
312346 #ifdef CONFIG_NET_NS
313347 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
7+typedef struct ref_tracker *netdevice_tracker;
8+#else
9+typedef struct {} netdevice_tracker;
10+#endif
11+
12+#ifdef CONFIG_NET_NS_REFCNT_TRACKER
13+typedef struct ref_tracker *netns_tracker;
14+#else
15+typedef 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
202202 __u32 type; /* for backward compat(TCA_OLD_COMPAT) */
203203 int nr_actions;
204204 struct tc_action **actions;
205− struct net *net;
205+ struct net *net;
206+ netns_tracker ns_tracker;
206207 #endif
207208 /* Map to export classifier specific extension TLV types to the
208209 * 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
218219 exts->type = 0;
219220 exts->nr_actions = 0;
220221 exts->net = net;
222+ netns_tracker_alloc(net, &exts->ns_tracker, GFP_KERNEL);
221223 exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
222224 GFP_KERNEL);
223225 if (!exts->actions)
@@ -236,6 +238,8 @@ static inline bool tcf_exts_get_net(struct tcf_exts *exts) · 12 unmodified lines
236238 {
237239 #ifdef CONFIG_NET_CLS_ACT
238240 exts->net = maybe_get_net(exts->net);
241+ if (exts->net)
242+ netns_tracker_alloc(exts->net, &exts->ns_tracker, GFP_KERNEL);
239243 return exts->net != NULL;
240244 #else
241245 return true;
@@ -246,7 +250,7 @@ static inline void tcf_exts_put_net(struct tcf_exts *exts) · 4 unmodified lines
246250 {
247251 #ifdef CONFIG_NET_CLS_ACT
248252 if (exts->net)
249− put_net(exts->net);
253+ put_net_track(exts->net, &exts->ns_tracker);
250254 #endif
251255 }
252256
include/net/sock.h+2−0
@@ -350,6 +350,7 @@ struct bpf_local_storage; · 349 unmodified lines
350350 * @sk_txtime_deadline_mode: set deadline mode for SO_TXTIME
351351 * @sk_txtime_report_errors: set report errors mode for SO_TXTIME
352352 * @sk_txtime_unused: unused txtime flags
353+ * @ns_tracker: tracker for netns reference
353354 */
354355 struct sock {
355356 /*
@@ -538,6 +539,7 @@ struct sock { · 182 unmodified lines
538539 struct bpf_local_storage __rcu *sk_bpf_storage;
539540 #endif
540541 struct rcu_head sk_rcu;
542+ netns_tracker ns_tracker;
541543 };
542544
543545 enum sk_pacing {
net/Kconfig.debug+9−0
@@ -8,3 +8,12 @@ config NET_DEV_REFCNT_TRACKER · 7 unmodified lines
88 help
99 Enable debugging feature to track device references.
1010 This adds memory and cpu costs.
11+
12+config 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
311311 LIST_HEAD(net_exit_list);
312312
313313 refcount_set(&net->ns.count, 1);
314+ ref_tracker_dir_init(&net->refcnt_tracker, 128);
315+
314316 refcount_set(&net->passive, 1);
315317 get_random_bytes(&net->hash_mix, sizeof(u32));
316318 preempt_disable();
@@ -635,6 +637,7 @@ static DECLARE_WORK(net_cleanup_work, cleanup_net); · 318 unmodified lines
635637
636638 void __put_net(struct net *net)
637639 {
640+ ref_tracker_dir_exit(&net->refcnt_tracker);
638641 /* Cleanup the network namespace in process context */
639642 if (llist_add(&net->cleanup_list, &cleanup_list))
640643 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
19831983 sock_lock_init(sk);
19841984 sk->sk_net_refcnt = kern ? 0 : 1;
19851985 if (likely(sk->sk_net_refcnt)) {
1986− get_net(net);
1986+ get_net_track(net, &sk->ns_tracker, priority);
19871987 sock_inuse_add(net, 1);
19881988 }
19891989
@@ -2039,7 +2039,7 @@ static void __sk_destruct(struct rcu_head *head) · 49 unmodified lines
20392039 put_pid(sk->sk_peer_pid);
20402040
20412041 if (likely(sk->sk_net_refcnt))
2042− put_net(sock_net(sk));
2042+ put_net_track(sock_net(sk), &sk->ns_tracker);
20432043 sk_prot_free(sk->sk_prot_creator, sk);
20442044 }
20452045
@@ -2126,7 +2126,7 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority) · 80 unmodified lines
21262126
21272127 /* SANITY */
21282128 if (likely(newsk->sk_net_refcnt)) {
2129− get_net(sock_net(newsk));
2129+ get_net_track(sock_net(newsk), &newsk->ns_tracker, priority);
21302130 sock_inuse_add(sock_net(newsk), 1);
21312131 }
21322132 sk_node_init(&newsk->sk_node);
net/l2tp/l2tp_debugfs.c+5−4
@@ -32,7 +32,8 @@ · 31 unmodified lines
3232 static struct dentry *rootdir;
3333
3434 struct l2tp_dfs_seq_data {
35− struct net *net;
35+ struct net *net;
36+ netns_tracker ns_tracker;
3637 int tunnel_idx; /* current tunnel */
3738 int session_idx; /* index of session within current tunnel */
3839 struct l2tp_tunnel *tunnel;
@@ -281,7 +282,7 @@ static int l2tp_dfs_seq_open(struct inode *inode, struct file *file) · 242 unmodified lines
281282 rc = PTR_ERR(pd->net);
282283 goto err_free_pd;
283284 }
284−
285+ netns_tracker_alloc(pd->net, &pd->ns_tracker, GFP_KERNEL);
285286 rc = seq_open(file, &l2tp_dfs_seq_ops);
286287 if (rc)
287288 goto err_free_net;
@@ -293,7 +294,7 @@ out: · 5 unmodified lines
293294 return rc;
294295
295296 err_free_net:
296− put_net(pd->net);
297+ put_net_track(pd->net, &pd->ns_tracker);
297298 err_free_pd:
298299 kfree(pd);
299300 goto out;
@@ -307,7 +308,7 @@ static int l2tp_dfs_seq_release(struct inode *inode, struct file *file) · 7 unmodified lines
307308 seq = file->private_data;
308309 pd = seq->private;
309310 if (pd->net)
310− put_net(pd->net);
311+ put_net_track(pd->net, &pd->ns_tracker);
311312 kfree(pd);
312313 seq_release(inode, file);
313314

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