Skip to content

Commit

Permalink
devlink: call peernet2id_alloc() with net pointer under RCU read lock
Browse files Browse the repository at this point in the history
peernet2id_alloc() allows to be called lockless with peer net pointer
obtained in RCU critical section and makes sure to return ns ID if net
namespaces is not being removed concurrently. Benefit from
read_pnet_rcu() helper addition, use it to obtain net pointer under RCU
read lock and pass it to peernet2id_alloc() to get ns ID.

Fixes: c137743 ("devlink: introduce object and nested devlink relationship infra")
Signed-off-by: Jiri Pirko <jiri@nvidia.com>
Reviewed-by: Simon Horman <horms@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Jiri Pirko authored and David S. Miller committed Oct 18, 2023
1 parent 2034d90 commit c503bc7
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions net/devlink/netlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,24 @@ int devlink_nl_put_nested_handle(struct sk_buff *msg, struct net *net,
struct devlink *devlink, int attrtype)
{
struct nlattr *nested_attr;
struct net *devl_net;

nested_attr = nla_nest_start(msg, attrtype);
if (!nested_attr)
return -EMSGSIZE;
if (devlink_nl_put_handle(msg, devlink))
goto nla_put_failure;
if (!net_eq(net, devlink_net(devlink))) {
int id = peernet2id_alloc(net, devlink_net(devlink),
GFP_KERNEL);

rcu_read_lock();
devl_net = read_pnet_rcu(&devlink->_net);
if (!net_eq(net, devl_net)) {
int id = peernet2id_alloc(net, devl_net, GFP_ATOMIC);

rcu_read_unlock();
if (nla_put_s32(msg, DEVLINK_ATTR_NETNS_ID, id))
return -EMSGSIZE;
} else {
rcu_read_unlock();
}

nla_nest_end(msg, nested_attr);
Expand Down

0 comments on commit c503bc7

Please sign in to comment.