From: Jiri Pirko <[email protected]>

Allow namespace moves to request a destination device name. Keep requested
names on the same literal-name path as the existing RDMA rename operation,
and keep teardown fallback naming on the trusted kernel-controlled path.

Signed-off-by: Jiri Pirko <[email protected]>
---
 drivers/infiniband/core/core_priv.h |  2 +-
 drivers/infiniband/core/device.c    | 48 ++++++++++++++++++++---------
 drivers/infiniband/core/nldev.c     |  2 +-
 3 files changed, 35 insertions(+), 17 deletions(-)

diff --git a/drivers/infiniband/core/core_priv.h 
b/drivers/infiniband/core/core_priv.h
index 19104c542b27..3bd5bb7135a3 100644
--- a/drivers/infiniband/core/core_priv.h
+++ b/drivers/infiniband/core/core_priv.h
@@ -356,7 +356,7 @@ void ib_port_unregister_client_groups(struct ib_device 
*ibdev, u32 port_num,
                                     const struct attribute_group **groups);
 
 int ib_device_set_netns_put(struct sk_buff *skb,
-                           struct ib_device *dev, u32 ns_fd);
+                           struct ib_device *dev, u32 ns_fd, const char *name);
 
 int rdma_nl_net_init(struct rdma_dev_net *rnet);
 void rdma_nl_net_exit(struct rdma_dev_net *rnet);
diff --git a/drivers/infiniband/core/device.c b/drivers/infiniband/core/device.c
index 8d169658e312..191f05898bae 100644
--- a/drivers/infiniband/core/device.c
+++ b/drivers/infiniband/core/device.c
@@ -268,7 +268,8 @@ static struct notifier_block ibdev_lsm_nb = {
 };
 
 static int rdma_dev_change_netns(struct ib_device *device, struct net *cur_net,
-                                struct net *net, const char *fallback_pattern);
+                                struct net *net, const char *requested_name,
+                                const char *fallback_pattern);
 
 /* Pointer to the RCU head at the start of the ib_port_data array */
 struct ib_port_data_rcu {
@@ -1173,7 +1174,7 @@ static void rdma_dev_exit_net(struct net *net)
                 */
                if (net_eq(net, read_pnet(&dev->coredev.rdma_net))) {
                        ret = rdma_dev_change_netns(dev, net, &init_net,
-                                                   "ibdev%d");
+                                                   NULL, "ibdev%d");
                        if (ret)
                                WARN(1,
                                     "Failed to move RDMA device %s to init_net 
on netns exit: %d\n",
@@ -1714,12 +1715,13 @@ static bool rdma_dev_name_in_netns(struct ib_device 
*skip, struct net *net,
 }
 
 /*
- * Choose the name @device should use in net namespace @net: keep the current
- * name when it is free, otherwise use a trusted '%d' @fallback_pattern
- * (namespace teardown) to pick a free index. The caller must hold the write
- * side of devices_rwsem.
+ * Choose the name @device should use in net namespace @net. @requested_name
+ * is used as a literal device name when set. Otherwise keep the current name
+ * when it is free, or use a trusted '%d' @fallback_pattern for teardown. The
+ * caller must hold the write side of devices_rwsem.
  */
 static int rdma_dev_pick_netns_name(struct ib_device *device, struct net *net,
+                                   const char *requested_name,
                                    const char *fallback_pattern,
                                    char *buf, size_t buf_len,
                                    const char **new_name)
@@ -1728,6 +1730,15 @@ static int rdma_dev_pick_netns_name(struct ib_device 
*device, struct net *net,
 
        lockdep_assert_held_write(&devices_rwsem);
 
+       if (requested_name) {
+               if (!rdma_dev_name_in_netns(device, net, requested_name)) {
+                       *new_name = requested_name;
+                       return 0;
+               }
+
+               return -EEXIST;
+       }
+
        if (!rdma_dev_name_in_netns(device, net, dev_name(&device->dev))) {
                *new_name = dev_name(&device->dev);
                return 0;
@@ -1758,7 +1769,8 @@ static int rdma_dev_pick_netns_name(struct ib_device 
*device, struct net *net,
  * Naming rules are handled by rdma_dev_pick_netns_name().
  */
 static int rdma_dev_change_netns(struct ib_device *device, struct net *cur_net,
-                                struct net *net, const char *fallback_pattern)
+                                struct net *net, const char *requested_name,
+                                const char *fallback_pattern)
 {
        char buf[IB_DEVICE_NAME_MAX];
        const char *new_name;
@@ -1784,8 +1796,9 @@ static int rdma_dev_change_netns(struct ib_device 
*device, struct net *cur_net,
                 * down, so a doomed user move does not disable a live device.
                 */
                down_write(&devices_rwsem);
-               ret = rdma_dev_pick_netns_name(device, net, fallback_pattern,
-                                              buf, sizeof(buf), &new_name);
+               ret = rdma_dev_pick_netns_name(device, net, requested_name,
+                                              fallback_pattern, buf,
+                                              sizeof(buf), &new_name);
                up_write(&devices_rwsem);
                if (ret)
                        goto out;
@@ -1801,8 +1814,9 @@ static int rdma_dev_change_netns(struct ib_device 
*device, struct net *cur_net,
         * level.
         */
        down_write(&devices_rwsem);
-       ret = rdma_dev_pick_netns_name(device, net, fallback_pattern, buf,
-                                      sizeof(buf), &new_name);
+       ret = rdma_dev_pick_netns_name(device, net, requested_name,
+                                      fallback_pattern, buf, sizeof(buf),
+                                      &new_name);
        if (ret) {
                if (fallback_pattern) {
                        WARN(1,
@@ -1857,7 +1871,7 @@ static int rdma_dev_change_netns(struct ib_device 
*device, struct net *cur_net,
 }
 
 int ib_device_set_netns_put(struct sk_buff *skb,
-                           struct ib_device *dev, u32 ns_fd)
+                           struct ib_device *dev, u32 ns_fd, const char *name)
 {
        struct net *net;
        int ret;
@@ -1873,9 +1887,12 @@ int ib_device_set_netns_put(struct sk_buff *skb,
                goto ns_err;
        }
 
-       /* Moving a device to the namespace it already lives in is a no-op. */
+       /*
+        * Moving a device to the namespace it already lives in is a no-op; a
+        * supplied name still renames it in place.
+        */
        if (net_eq(net, read_pnet(&dev->coredev.rdma_net))) {
-               ret = 0;
+               ret = name ? ib_device_rename(dev, name) : 0;
                goto ns_err;
        }
 
@@ -1891,7 +1908,8 @@ int ib_device_set_netns_put(struct sk_buff *skb,
 
        get_device(&dev->dev);
        ib_device_put(dev);
-       ret = rdma_dev_change_netns(dev, current->nsproxy->net_ns, net, NULL);
+       ret = rdma_dev_change_netns(dev, current->nsproxy->net_ns, net, name,
+                                   NULL);
        put_device(&dev->dev);
 
        put_net(net);
diff --git a/drivers/infiniband/core/nldev.c b/drivers/infiniband/core/nldev.c
index 3a9ec43a16f1..3540cb0b9d4f 100644
--- a/drivers/infiniband/core/nldev.c
+++ b/drivers/infiniband/core/nldev.c
@@ -1194,7 +1194,7 @@ static int nldev_set_doit(struct sk_buff *skb, struct 
nlmsghdr *nlh,
                u32 ns_fd;
 
                ns_fd = nla_get_u32(tb[RDMA_NLDEV_NET_NS_FD]);
-               err = ib_device_set_netns_put(skb, device, ns_fd);
+               err = ib_device_set_netns_put(skb, device, ns_fd, NULL);
                if (err == -EEXIST)
                        NL_SET_ERR_MSG(extack,
                                       "Device name already exists in the 
target net namespace");
-- 
2.54.0


Reply via email to