diff mbox series

[net-next,v3,5/5] netns: enable to dump full nsid translation table

Message ID 20181122222215.23554-6-nicolas.dichtel@6wind.com
State Superseded, archived
Delegated to: David Miller
Headers show
Series Ease to interpret net-nsid | expand

Commit Message

Nicolas Dichtel Nov. 22, 2018, 10:22 p.m. UTC
Like the previous patch, the goal is to ease to convert nsids from one
netns to another netns.
A new attribute (NETNSA_CURRENT_NSID) is added to the kernel answer when
NETNSA_TARGET_NSID is provided, thus the user can easily convert nsids.

Signed-off-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
---
 include/uapi/linux/net_namespace.h |  1 +
 net/core/net_namespace.c           | 31 ++++++++++++++++++++++++------
 2 files changed, 26 insertions(+), 6 deletions(-)

Comments

David Ahern Nov. 24, 2018, 3:47 p.m. UTC | #1
On 11/22/18 3:22 PM, Nicolas Dichtel wrote:
> Like the previous patch, the goal is to ease to convert nsids from one
> netns to another netns.
> A new attribute (NETNSA_CURRENT_NSID) is added to the kernel answer when
> NETNSA_TARGET_NSID is provided, thus the user can easily convert nsids.
> 
> Signed-off-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
> ---
>  include/uapi/linux/net_namespace.h |  1 +
>  net/core/net_namespace.c           | 31 ++++++++++++++++++++++++------
>  2 files changed, 26 insertions(+), 6 deletions(-)
> 


Reviewed-by: David Ahern <dsahern@gmail.com>
kernel test robot Nov. 26, 2018, 10:06 a.m. UTC | #2
Hi Nicolas,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on net-next/master]

url:    https://github.com/0day-ci/linux/commits/Nicolas-Dichtel/netns-remove-net-arg-from-rtnl_net_fill/20181126-035032
config: x86_64-allmodconfig (attached as .config)
compiler: gcc-7 (Debian 7.3.0-1) 7.3.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

All warnings (new ones prefixed by >>):

   include/linux/slab.h:332:43: warning: dubious: x & !y
   include/linux/slab.h:332:43: warning: dubious: x & !y
>> net/core/net_namespace.c:963:23: warning: context imbalance in 'rtnl_net_dumpid' - different lock contexts for basic block

vim +/rtnl_net_dumpid +963 net/core/net_namespace.c

5589651eb Nicolas Dichtel 2018-11-22  929  
a143c40c3 Nicolas Dichtel 2015-04-07  930  static int rtnl_net_dumpid(struct sk_buff *skb, struct netlink_callback *cb)
a143c40c3 Nicolas Dichtel 2015-04-07  931  {
a143c40c3 Nicolas Dichtel 2015-04-07  932  	struct rtnl_net_dump_cb net_cb = {
5589651eb Nicolas Dichtel 2018-11-22  933  		.tgt_net = sock_net(skb->sk),
a143c40c3 Nicolas Dichtel 2015-04-07  934  		.skb = skb,
70955fc94 Nicolas Dichtel 2018-11-22  935  		.fillargs = {
70955fc94 Nicolas Dichtel 2018-11-22  936  			.portid = NETLINK_CB(cb->skb).portid,
70955fc94 Nicolas Dichtel 2018-11-22  937  			.seq = cb->nlh->nlmsg_seq,
70955fc94 Nicolas Dichtel 2018-11-22  938  			.flags = NLM_F_MULTI,
70955fc94 Nicolas Dichtel 2018-11-22  939  			.cmd = RTM_NEWNSID,
70955fc94 Nicolas Dichtel 2018-11-22  940  		},
a143c40c3 Nicolas Dichtel 2015-04-07  941  		.idx = 0,
a143c40c3 Nicolas Dichtel 2015-04-07  942  		.s_idx = cb->args[0],
a143c40c3 Nicolas Dichtel 2015-04-07  943  	};
5589651eb Nicolas Dichtel 2018-11-22  944  	int err = 0;
a143c40c3 Nicolas Dichtel 2015-04-07  945  
5589651eb Nicolas Dichtel 2018-11-22  946  	if (cb->strict_check) {
5589651eb Nicolas Dichtel 2018-11-22  947  		err = rtnl_valid_dump_net_req(cb->nlh, skb->sk, &net_cb, cb);
5589651eb Nicolas Dichtel 2018-11-22  948  		if (err < 0)
5589651eb Nicolas Dichtel 2018-11-22  949  			goto end;
f80f14c36 David Ahern     2018-10-07  950  	}
f80f14c36 David Ahern     2018-10-07  951  
5589651eb Nicolas Dichtel 2018-11-22  952  	spin_lock_bh(&net_cb.tgt_net->nsid_lock);
8a46e1ccc Nicolas Dichtel 2018-11-22  953  	if (net_cb.fillargs.add_ref &&
8a46e1ccc Nicolas Dichtel 2018-11-22  954  	    !net_eq(net_cb.ref_net, net_cb.tgt_net) &&
8a46e1ccc Nicolas Dichtel 2018-11-22  955  	    !spin_trylock_bh(&net_cb.ref_net->nsid_lock)) {
8a46e1ccc Nicolas Dichtel 2018-11-22  956  		err = -EAGAIN;
8a46e1ccc Nicolas Dichtel 2018-11-22  957  		goto end;
8a46e1ccc Nicolas Dichtel 2018-11-22  958  	}
5589651eb Nicolas Dichtel 2018-11-22  959  	idr_for_each(&net_cb.tgt_net->netns_ids, rtnl_net_dumpid_one, &net_cb);
8a46e1ccc Nicolas Dichtel 2018-11-22  960  	if (net_cb.fillargs.add_ref &&
8a46e1ccc Nicolas Dichtel 2018-11-22  961  	    !net_eq(net_cb.ref_net, net_cb.tgt_net))
8a46e1ccc Nicolas Dichtel 2018-11-22  962  		spin_unlock_bh(&net_cb.ref_net->nsid_lock);
5589651eb Nicolas Dichtel 2018-11-22 @963  	spin_unlock_bh(&net_cb.tgt_net->nsid_lock);
a143c40c3 Nicolas Dichtel 2015-04-07  964  
a143c40c3 Nicolas Dichtel 2015-04-07  965  	cb->args[0] = net_cb.idx;
5589651eb Nicolas Dichtel 2018-11-22  966  end:
8a46e1ccc Nicolas Dichtel 2018-11-22  967  	if (net_cb.fillargs.add_ref)
5589651eb Nicolas Dichtel 2018-11-22  968  		put_net(net_cb.tgt_net);
5589651eb Nicolas Dichtel 2018-11-22  969  	return err < 0 ? err : skb->len;
a143c40c3 Nicolas Dichtel 2015-04-07  970  }
a143c40c3 Nicolas Dichtel 2015-04-07  971  

:::::: The code at line 963 was first introduced by commit
:::::: 5589651eb5e06b32107a1d8830af4be67aa58b85 netns: add support of NETNSA_TARGET_NSID

:::::: TO: Nicolas Dichtel <nicolas.dichtel@6wind.com>
:::::: CC: 0day robot <lkp@intel.com>

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation
diff mbox series

Patch

diff --git a/include/uapi/linux/net_namespace.h b/include/uapi/linux/net_namespace.h
index 0ed9dd61d32a..9f9956809565 100644
--- a/include/uapi/linux/net_namespace.h
+++ b/include/uapi/linux/net_namespace.h
@@ -17,6 +17,7 @@  enum {
 	NETNSA_PID,
 	NETNSA_FD,
 	NETNSA_TARGET_NSID,
+	NETNSA_CURRENT_NSID,
 	__NETNSA_MAX,
 };
 
diff --git a/net/core/net_namespace.c b/net/core/net_namespace.c
index dd25fb22ad45..2f25d7f2a43b 100644
--- a/net/core/net_namespace.c
+++ b/net/core/net_namespace.c
@@ -736,6 +736,7 @@  static int rtnl_net_get_size(void)
 {
 	return NLMSG_ALIGN(sizeof(struct rtgenmsg))
 	       + nla_total_size(sizeof(s32)) /* NETNSA_NSID */
+	       + nla_total_size(sizeof(s32)) /* NETNSA_CURRENT_NSID */
 	       ;
 }
 
@@ -745,6 +746,8 @@  struct net_fill_args {
 	int flags;
 	int cmd;
 	int nsid;
+	bool add_ref;
+	int ref_nsid;
 };
 
 static int rtnl_net_fill(struct sk_buff *skb, struct net_fill_args *args)
@@ -763,6 +766,10 @@  static int rtnl_net_fill(struct sk_buff *skb, struct net_fill_args *args)
 	if (nla_put_s32(skb, NETNSA_NSID, args->nsid))
 		goto nla_put_failure;
 
+	if (args->add_ref &&
+	    nla_put_s32(skb, NETNSA_CURRENT_NSID, args->ref_nsid))
+		goto nla_put_failure;
+
 	nlmsg_end(skb, nlh);
 	return 0;
 
@@ -782,7 +789,6 @@  static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh,
 		.cmd = RTM_NEWNSID,
 	};
 	struct net *peer, *target = net;
-	bool put_target = false;
 	struct nlattr *nla;
 	struct sk_buff *msg;
 	int err;
@@ -824,7 +830,8 @@  static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh,
 			err = PTR_ERR(target);
 			goto out;
 		}
-		put_target = true;
+		fillargs.add_ref = true;
+		fillargs.ref_nsid = peernet2id(net, peer);
 	}
 
 	msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
@@ -844,7 +851,7 @@  static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh,
 err_out:
 	nlmsg_free(msg);
 out:
-	if (put_target)
+	if (fillargs.add_ref)
 		put_net(target);
 	put_net(peer);
 	return err;
@@ -852,11 +859,11 @@  static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh,
 
 struct rtnl_net_dump_cb {
 	struct net *tgt_net;
+	struct net *ref_net;
 	struct sk_buff *skb;
 	struct net_fill_args fillargs;
 	int idx;
 	int s_idx;
-	bool put_tgt_net;
 };
 
 static int rtnl_net_dumpid_one(int id, void *peer, void *data)
@@ -868,6 +875,8 @@  static int rtnl_net_dumpid_one(int id, void *peer, void *data)
 		goto cont;
 
 	net_cb->fillargs.nsid = id;
+	if (net_cb->fillargs.add_ref)
+		net_cb->fillargs.ref_nsid = __peernet2id(net_cb->ref_net, peer);
 	ret = rtnl_net_fill(net_cb->skb, &net_cb->fillargs);
 	if (ret < 0)
 		return ret;
@@ -904,8 +913,9 @@  static int rtnl_valid_dump_net_req(const struct nlmsghdr *nlh, struct sock *sk,
 					       "Invalid target network namespace id");
 				return PTR_ERR(net);
 			}
+			net_cb->fillargs.add_ref = true;
+			net_cb->ref_net = net_cb->tgt_net;
 			net_cb->tgt_net = net;
-			net_cb->put_tgt_net = true;
 		} else {
 			NL_SET_BAD_ATTR(extack, tb[i]);
 			NL_SET_ERR_MSG(extack,
@@ -940,12 +950,21 @@  static int rtnl_net_dumpid(struct sk_buff *skb, struct netlink_callback *cb)
 	}
 
 	spin_lock_bh(&net_cb.tgt_net->nsid_lock);
+	if (net_cb.fillargs.add_ref &&
+	    !net_eq(net_cb.ref_net, net_cb.tgt_net) &&
+	    !spin_trylock_bh(&net_cb.ref_net->nsid_lock)) {
+		err = -EAGAIN;
+		goto end;
+	}
 	idr_for_each(&net_cb.tgt_net->netns_ids, rtnl_net_dumpid_one, &net_cb);
+	if (net_cb.fillargs.add_ref &&
+	    !net_eq(net_cb.ref_net, net_cb.tgt_net))
+		spin_unlock_bh(&net_cb.ref_net->nsid_lock);
 	spin_unlock_bh(&net_cb.tgt_net->nsid_lock);
 
 	cb->args[0] = net_cb.idx;
 end:
-	if (net_cb.put_tgt_net)
+	if (net_cb.fillargs.add_ref)
 		put_net(net_cb.tgt_net);
 	return err < 0 ? err : skb->len;
 }