Skip to content

Commit

Permalink
RDMA/core: Extend ib_device_get_by_index for net namespace
Browse files Browse the repository at this point in the history
Extend ib_device_get_by_index() API to check device access for
net namespace for serving netlink commands.

Also enforce net ns check on dumpit commands which iterate over all
registered rdma devices and which don't call ib_device_get_by_index().

Signed-off-by: Parav Pandit <parav@mellanox.com>
Signed-off-by: Leon Romanovsky <leonro@mellanox.com>
Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>
  • Loading branch information
Parav Pandit authored and Jason Gunthorpe committed Mar 28, 2019
1 parent 41c6140 commit 37eeab5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
3 changes: 2 additions & 1 deletion drivers/infiniband/core/core_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ static inline void ib_mad_agent_security_change(void)
}
#endif

struct ib_device *ib_device_get_by_index(u32 ifindex);
struct ib_device *ib_device_get_by_index(const struct net *net, u32 index);

/* RDMA device netlink */
void nldev_init(void);
void nldev_exit(void);
Expand Down
11 changes: 10 additions & 1 deletion drivers/infiniband/core/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,16 +250,22 @@ static int ib_device_check_mandatory(struct ib_device *device)
* Caller must perform ib_device_put() to return the device reference count
* when ib_device_get_by_index() returns valid device pointer.
*/
struct ib_device *ib_device_get_by_index(u32 index)
struct ib_device *ib_device_get_by_index(const struct net *net, u32 index)
{
struct ib_device *device;

down_read(&devices_rwsem);
device = xa_load(&devices, index);
if (device) {
if (!rdma_dev_access_netns(device, net)) {
device = NULL;
goto out;
}

if (!ib_device_try_get(device))
device = NULL;
}
out:
up_read(&devices_rwsem);
return device;
}
Expand Down Expand Up @@ -1815,6 +1821,9 @@ int ib_enum_all_devs(nldev_callback nldev_cb, struct sk_buff *skb,

down_read(&devices_rwsem);
xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) {
if (!rdma_dev_access_netns(dev, sock_net(skb->sk)))
continue;

ret = nldev_cb(dev, skb, cb, idx);
if (ret)
break;
Expand Down
18 changes: 9 additions & 9 deletions drivers/infiniband/core/nldev.c
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ static int nldev_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,

index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);

device = ib_device_get_by_index(index);
device = ib_device_get_by_index(sock_net(skb->sk), index);
if (!device)
return -EINVAL;

Expand Down Expand Up @@ -658,7 +658,7 @@ static int nldev_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
return -EINVAL;

index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
device = ib_device_get_by_index(index);
device = ib_device_get_by_index(sock_net(skb->sk), index);
if (!device)
return -EINVAL;

Expand Down Expand Up @@ -706,7 +706,7 @@ static int nldev_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
{
/*
* There is no need to take lock, because
* we are relying on ib_core's lists_rwsem
* we are relying on ib_core's locking.
*/
return ib_enum_all_devs(_nldev_get_dumpit, skb, cb);
}
Expand All @@ -729,7 +729,7 @@ static int nldev_port_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
return -EINVAL;

index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
device = ib_device_get_by_index(index);
device = ib_device_get_by_index(sock_net(skb->sk), index);
if (!device)
return -EINVAL;

Expand Down Expand Up @@ -783,7 +783,7 @@ static int nldev_port_get_dumpit(struct sk_buff *skb,
return -EINVAL;

ifindex = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
device = ib_device_get_by_index(ifindex);
device = ib_device_get_by_index(sock_net(skb->sk), ifindex);
if (!device)
return -EINVAL;

Expand Down Expand Up @@ -838,7 +838,7 @@ static int nldev_res_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
return -EINVAL;

index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
device = ib_device_get_by_index(index);
device = ib_device_get_by_index(sock_net(skb->sk), index);
if (!device)
return -EINVAL;

Expand Down Expand Up @@ -987,7 +987,7 @@ static int res_get_common_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
return -EINVAL;

index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
device = ib_device_get_by_index(index);
device = ib_device_get_by_index(sock_net(skb->sk), index);
if (!device)
return -EINVAL;

Expand Down Expand Up @@ -1084,7 +1084,7 @@ static int res_get_common_dumpit(struct sk_buff *skb,
return -EINVAL;

index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
device = ib_device_get_by_index(index);
device = ib_device_get_by_index(sock_net(skb->sk), index);
if (!device)
return -EINVAL;

Expand Down Expand Up @@ -1299,7 +1299,7 @@ static int nldev_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
return -EINVAL;

index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
device = ib_device_get_by_index(index);
device = ib_device_get_by_index(sock_net(skb->sk), index);
if (!device)
return -EINVAL;

Expand Down

0 comments on commit 37eeab5

Please sign in to comment.