RDMA: Make device names unique per net namespace
Subsystems (10)
cover RDMA: Make device names unique per net namespace
From: Jiri Pirko <[email protected]>
RDMA device names are unique system-wide today:__ib_device_get_by_name() checks a requested name against every registered device regardless of the network namespace it lives in. A device in one network namespace therefore cannot use a name already taken in another, even in exclusive netns mode (netns_mode=0) where the two are otherwise isolated. Net devices have no such restriction - their names only need to be unique within a network namespace.
This series makes RDMA device names unique per network namespace, matching net device semantics, and adapts the users that assumed system-wide unique names.
Scoping reuses the existing rdma_dev_access_netns() predicate, so behavior only changes in exclusive mode:
- shared mode (default): names stay unique system-wide, no change;
- exclusive mode: names only need to be unique within a namespace;
- CONFIG_NET_NS=n: everything is init_net, names stay system-wide unique.
There are two users that cannot be made per-namespace and are documented as known limitations instead of changed:
- the rdma_cm configfs tree: configfs has no network namespace
support, so it cannot represent two same-named devices;
- SELinux ibendport labelling: endports are labelled by (device name, port) from a global policy; distinguishing same-named devices would need net namespace support in the SELinux policy language and tooling.
Tested with the new rxe_netns_names kselftest added in the last patch.
Jiri Pirko (13):
RDMA/core: Pass the net namespace to the device name lookups RDMA/core: Handle device name conflicts when changing net namespace RDMA/core: Support renaming a device when changing its net namespace RDMA/nldev: Report net namespace move errors through extack RDMA/nldev: Allow setting the device name while changing net namespace net/smc: Look up the pnetid ib device within the net namespace RDMA/srp: Make the SRP sysfs class net namespace aware RDMA/cgroup: Scope rdma cgroup device visibility to the net namespace RDMA/cma: Document that CM configfs cannot be net namespace scoped RDMA/core: Document the SELinux ibendport net namespace limitation RDMA/core: Make device names unique per net namespace RDMA/rxe: Implement disassociate_ucontext callback RDMA/selftests: Add rxe_netns_names test
Documentation/ABI/testing/configfs-rdma_cm | 4 + Documentation/admin-guide/cgroup-v2.rst | 7 + drivers/infiniband/core/cgroup.c | 12 + drivers/infiniband/core/cma_configfs.c | 4 + drivers/infiniband/core/core_priv.h | 15 +- drivers/infiniband/core/device.c | 256 +++++++++++++--- drivers/infiniband/core/nldev.c | 26 +- drivers/infiniband/core/security.c | 6 + drivers/infiniband/sw/rxe/rxe_verbs.c | 5 + drivers/infiniband/ulp/srp/ib_srp.c | 16 +- include/linux/cgroup_rdma.h | 10 + include/uapi/rdma/rdma_netlink.h | 5 +- kernel/cgroup/rdma.c | 20 +- net/smc/smc_pnet.c | 20 +- tools/testing/selftests/rdma/Makefile | 3 +- tools/testing/selftests/rdma/config | 2 + .../testing/selftests/rdma/rxe_netns_names.sh | 282 ++++++++++++++++++ 17 files changed, 639 insertions(+), 54 deletions(-) create mode 100755 tools/testing/selftests/rdma/rxe_netns_names.sh
Signature · 1 line
2.54.0
PATCH 1/13 RDMA/core: Pass the net namespace to the device name lookups
From: Jiri Pirko <[email protected]>
Prepare for per-netns RDMA device names by passing the target net namespace through the name lookup and allocation helpers. Keep current global uniqueness behaviour.
--- drivers/infiniband/core/device.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-)
drivers/infiniband/core/device.c +18 -8
@@ -351,7 +351,8 @@ void ib_device_put(struct ib_device *device) }EXPORT_SYMBOL (ib_device_put); -static struct ib_device *__ib_device_get_by_name (const char *name) +static struct ib_device *__ib_device_get_by_name (const char *name, +const struct net *net) {struct ib_device *device;unsigned long index;
@@ -395,7 +396,7 @@ int ib_device_rename(struct ib_device *ibdev, const char *name)return 0 ; } -if (__ib_device_get_by_name (name)) { +if (__ib_device_get_by_name (name,rdma_dev_net (ibdev))) {up_write (&devices_rwsem);return -EEXIST ; }
@@ -435,7 +436,8 @@ int ib_device_set_dim(struct ib_device *ibdev, u8 use_dim)return 0 ; } -static int alloc_name (struct ib_device *ibdev,const char *name) +/* Pick a free index for the '%d' style @name pattern. */ +static int alloc_name_id (struct net *net,const char *name) {struct ib_device *device;unsigned long index;
@@ -462,15 +464,22 @@ static int alloc_name(struct ib_device *ibdev, const char *name) } rc =ida_alloc (&inuse,GFP_KERNEL ); -if (rc <0 ) - goto out; - - rc =dev_set_name (&ibdev->dev, name, rc); out:ida_destroy (&inuse);return rc; } +static int alloc_name (struct ib_device *ibdev,const char *name) +{ +int id; + + id =alloc_name_id (rdma_dev_net (ibdev), name); +if (id <0 ) +return id; + +return dev_set_name (&ibdev->dev, name, id); +} +static void ib_device_release (struct device *device) { struct ib_device *dev = container_of(device, struct ib_device, dev);
@@ -1223,7 +1232,8 @@ static int assign_name(struct ib_device *device, const char *name)if (ret) goto out; -if (__ib_device_get_by_name (dev_name (&device->dev))) { +if (__ib_device_get_by_name (dev_name (&device->dev), +rdma_dev_net (device))) { ret = -ENFILE ; goto out; }
Signature · 1 line
2.54.0
PATCH 2/13 RDMA/core: Handle device name conflicts when changing net namespace
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Prepare namespace moves for per-netns names. Check user-initiated moves for destination-name conflicts before disabling the device, keep same-netns moves as no-ops, and make teardown moves detach from the exiting namespace even if fallback naming fails.
--- drivers/infiniband/core/device.c | 155 ++++++++++++++++++++++++++----- drivers/infiniband/core/nldev.c | 3 + 2 files changed, 137 insertions(+), 21 deletions(-)
drivers/infiniband/core/device.c +134 -21
@@ -268,7 +268,7 @@ static struct notifier_block ibdev_lsm_nb = { };static int rdma_dev_change_netns (struct ib_device *device,struct net *cur_net, -struct net *net); +struct net *net,const char *fallback_pattern);/* Pointer to the RCU head at the start of the ib_port_data array */ struct ib_port_data_rcu {
@@ -437,7 +437,8 @@ int ib_device_set_dim(struct ib_device *ibdev, u8 use_dim) }/* Pick a free index for the '%d' style @name pattern. */ -static int alloc_name_id (struct net *net,const char *name) +static int __alloc_name_id (struct net *net,const char *name, +const struct ib_device *skip) {struct ib_device *device;unsigned long index;
@@ -450,6 +451,8 @@ static int alloc_name_id(struct net *net, const char *name) xa_for_each (&devices, index, device) {char buf[IB_DEVICE_NAME_MAX ]; +if (device == skip) +continue ;if (sscanf (dev_name (&device->dev), name, &i) !=1 )continue ;if (i <0 || i >=INT_MAX )
@@ -469,6 +472,11 @@ static int alloc_name_id(struct net *net, const char *name)return rc; } +static int alloc_name_id (struct net *net,const char *name) +{ +return __alloc_name_id (net, name,NULL ); +} +static int alloc_name (struct ib_device *ibdev,const char *name) {int id;
@@ -1160,8 +1168,17 @@ static void rdma_dev_exit_net(struct net *net)/* * If the real device is in the NS then move it back to init. +* Provide a fallback pattern so a name conflict in init_net +* cannot make the teardown move fail. */ -rdma_dev_change_netns (dev, net, &init_net); +if (net_eq (net,read_pnet (&dev->coredev.rdma_net))) { + ret =rdma_dev_change_netns (dev, net, &init_net, +"ibdev%d" ); +if (ret) +WARN (1 , +"Failed to move RDMA device %s to init_net on netns exit: %d\n" , +dev_name (&dev->dev), ret); + }put_device (&dev->dev);down_read (&devices_rwsem);
@@ -1680,14 +1697,71 @@ void ib_unregister_device_queued(struct ib_device *ib_dev) }EXPORT_SYMBOL (ib_unregister_device_queued); +static bool rdma_dev_name_in_netns (struct ib_device *skip,struct net *net, +const char *name) +{ +struct ib_device *device; +unsigned long index; + +lockdep_assert_held_write (&devices_rwsem); + +xa_for_each (&devices, index, device) +if (device != skip && + !strcmp (name,dev_name (&device->dev))) +return true; + +return false; +} + +/* +* 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. +*/ +static int rdma_dev_pick_netns_name (struct ib_device *device,struct net *net, +const char *fallback_pattern, +char *buf,size_t buf_len, +const char **new_name) +{ +int id; + +lockdep_assert_held_write (&devices_rwsem); + +if (!rdma_dev_name_in_netns (device, net,dev_name (&device->dev))) { + *new_name =dev_name (&device->dev); +return 0 ; + } + +if (!fallback_pattern) +return -EEXIST ; + +snprintf (buf, buf_len,"ibdev%u" , device->index); +if (!rdma_dev_name_in_netns (device, net, buf)) { + *new_name = buf; +return 0 ; + } + + id =__alloc_name_id (net, fallback_pattern, device); +if (id <0 ) +return id; +snprintf (buf, buf_len, fallback_pattern, id); + *new_name = buf; +return 0 ; +} +/* * The caller must pass in a device that has the kref held and the refcount * released. If the device is in cur_net and still registered then it is moved * into 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) +struct net *net,const char *fallback_pattern) { +char buf[IB_DEVICE_NAME_MAX ]; +const char *new_name;int ret2 = -EINVAL ;int ret;
@@ -1704,30 +1778,63 @@ static int rdma_dev_change_netns(struct ib_device *device, struct net *cur_net, goto out; } +if (!fallback_pattern) { +/* +* Reject a predictable name conflict before tearing anything +* 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); +up_write (&devices_rwsem); +if (ret) + goto out; + } +kobject_uevent (&device->dev.kobj,KOBJ_REMOVE );disable_device (device);/* -* At this point no one can be using the device, so it is safe to -* change the namespace. +* Recompute the destination name under the write side of devices_rwsem +* now that the device is disabled, closing races with a concurrent +* registration or rename, then publish the new namespace at the sysfs +* level. */ -write_pnet (&device->coredev.rdma_net, net); +down_write (&devices_rwsem); + ret =rdma_dev_pick_netns_name (device, net, fallback_pattern, buf, +sizeof (buf), &new_name); +if (ret) { +if (fallback_pattern) { +WARN (1 , +"%s: failed to pick device name during namespace teardown: %d\n" , + __func__, ret); +write_pnet (&device->coredev.rdma_net, net); + ret =0 ; + } + goto rename_done; + } -down_read (&devices_rwsem); -/* -* Currently rdma devices are system wide unique. So the device name -* is guaranteed free in the new namespace. Publish the new namespace -* at the sysfs level. -*/ - ret =device_rename (&device->dev,dev_name (&device->dev)); -up_read (&devices_rwsem); +write_pnet (&device->coredev.rdma_net, net); + ret =device_rename (&device->dev, new_name);if (ret) { -dev_warn (&device->dev, -"%s: Couldn't rename device after namespace change\n" , - __func__); -/* Try and put things back and re-enable the device */ -write_pnet (&device->coredev.rdma_net, cur_net); +if (fallback_pattern) { +WARN (1 , +"%s: failed to rename device during namespace teardown: %d\n" , + __func__, ret); + ret =0 ; + }else { +dev_warn (&device->dev, +"%s: Couldn't rename device after namespace change\n" , + __func__); +/* Try and put things back and re-enable the device */ +write_pnet (&device->coredev.rdma_net, cur_net); + } + }else { +strscpy (device->name,dev_name (&device->dev), +IB_DEVICE_NAME_MAX ); } +rename_done: +up_write (&devices_rwsem); ret2 =enable_device_and_get (device);if (ret2) {
@@ -1766,6 +1873,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. */ +if (net_eq (net,read_pnet (&dev->coredev.rdma_net))) { + ret =0 ; + goto ns_err; + } +/* * All the ib_clients, including uverbs, are reset when the namespace is * changed and this cannot be blocked waiting for userspace to do
@@ -1778,7 +1891,7 @@ 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); + ret =rdma_dev_change_netns (dev, current->nsproxy->net_ns, net,NULL );put_device (&dev->dev);put_net (net);
drivers/infiniband/core/nldev.c +3 -0
@@ -1195,6 +1195,9 @@ static int nldev_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh, ns_fd =nla_get_u32 (tb[RDMA_NLDEV_NET_NS_FD ]); err =ib_device_set_netns_put (skb, device, ns_fd); +if (err == -EEXIST ) +NL_SET_ERR_MSG (extack, +"Device name already exists in the target net namespace" ); goto put_done; }
Signature · 1 line
2.54.0
PATCH 3/13 RDMA/core: Support renaming a device when changing its net namespace
In reply to Jiri PirkoFrom: 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.
--- 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(-)
drivers/infiniband/core/core_priv.h +1 -1
@@ -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);
drivers/infiniband/core/device.c +33 -15
111 lines not on this page · raw message
drivers/infiniband/core/nldev.c +1 -1
@@ -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" );
Signature · 1 line
2.54.0
PATCH 4/13 RDMA/nldev: Report net namespace move errors through extack
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Thread extack through the existing net namespace move helper and report the main failure reasons from the core path. Keep the existing move UAPI shape unchanged.
--- drivers/infiniband/core/core_priv.h | 3 ++- drivers/infiniband/core/device.c | 24 ++++++++++++++++++++++-- drivers/infiniband/core/nldev.c | 6 ++---- 3 files changed, 26 insertions(+), 7 deletions(-)
drivers/infiniband/core/core_priv.h +2 -1
@@ -356,7 +356,8 @@ 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,const char *name); +struct ib_device *dev,u32 ns_fd,const char *name, +struct netlink_ext_ack *extack);int rdma_nl_net_init (struct rdma_dev_net *rnet);void rdma_nl_net_exit (struct rdma_dev_net *rnet);
drivers/infiniband/core/device.c +22 -2
@@ -1871,18 +1871,22 @@ 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,const char *name) +struct ib_device *dev,u32 ns_fd,const char *name, +struct netlink_ext_ack *extack) {struct net *net;int ret; net =get_net_ns_by_fd (ns_fd);if (IS_ERR (net)) { +NL_SET_ERR_MSG (extack,"Invalid target net namespace fd" ); ret =PTR_ERR (net); goto net_err; }if (!netlink_ns_capable (skb, net->user_ns,CAP_NET_ADMIN )) { +NL_SET_ERR_MSG (extack, +"Missing CAP_NET_ADMIN in the target net namespace" ); ret = -EPERM ; goto ns_err; }
@@ -1893,6 +1897,10 @@ int ib_device_set_netns_put(struct sk_buff *skb,*/ if (net_eq (net,read_pnet (&dev->coredev.rdma_net))) { ret = name ?ib_device_rename (dev, name) :0 ; + +if (ret == -EEXIST ) +NL_SET_ERR_MSG (extack, +"Device name already exists in the target net namespace" ); goto ns_err; }
@@ -1901,7 +1909,16 @@ int ib_device_set_netns_put(struct sk_buff *skb,* changed and this cannot be blocked waiting for userspace to do * something, so disassociation is mandatory. */ -if (!dev->ops.disassociate_ucontext || ib_devices_shared_netns) { +if (ib_devices_shared_netns) { +NL_SET_ERR_MSG (extack, +"Cannot change net namespace of RDMA device in shared netns mode" ); + ret = -EOPNOTSUPP ; + goto ns_err; + } + +if (!dev->ops.disassociate_ucontext) { +NL_SET_ERR_MSG (extack, +"Device does not support namespace changes (no disassociate support)" ); ret = -EOPNOTSUPP ; goto ns_err; }
@@ -1911,6 +1928,9 @@ int ib_device_set_netns_put(struct sk_buff *skb, ret =rdma_dev_change_netns (dev, current->nsproxy->net_ns, net, name,NULL );put_device (&dev->dev); +if (ret == -EEXIST ) +NL_SET_ERR_MSG (extack, +"Device name already exists in the target net namespace" );put_net (net);return ret;
drivers/infiniband/core/nldev.c +2 -4
@@ -1194,10 +1194,8 @@ 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,NULL ); -if (err == -EEXIST ) -NL_SET_ERR_MSG (extack, -"Device name already exists in the target net namespace" ); + err =ib_device_set_netns_put (skb, device, ns_fd,NULL , + extack); goto put_done; }
Signature · 1 line
2.54.0
PATCH 5/13 RDMA/nldev: Allow setting the device name while changing net namespace
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Accept RDMA_NLDEV_ATTR_DEV_NAME together with RDMA_NLDEV_NET_NS_FD so a netlink move can rename the device in the destination namespace. Keep the name semantics aligned with the existing RDMA rename path.
--- drivers/infiniband/core/device.c | 6 ++++++ drivers/infiniband/core/nldev.c | 27 ++++++++++++++++++--------- include/uapi/rdma/rdma_netlink.h | 5 ++++- 3 files changed, 28 insertions(+), 10 deletions(-)
drivers/infiniband/core/device.c +6 -0
@@ -1901,6 +1901,9 @@ int ib_device_set_netns_put(struct sk_buff *skb,if (ret == -EEXIST )NL_SET_ERR_MSG (extack,"Device name already exists in the target net namespace" ); +else if (ret == -EINVAL && name) +NL_SET_ERR_MSG (extack, +"Unable to use requested device name in the target net namespace" ); goto ns_err; }
@@ -1931,6 +1934,9 @@ int ib_device_set_netns_put(struct sk_buff *skb,if (ret == -EEXIST )NL_SET_ERR_MSG (extack,"Device name already exists in the target net namespace" ); +else if (ret == -EINVAL && name) +NL_SET_ERR_MSG (extack, +"Unable to use requested device name in the target net namespace" );put_net (net);return ret;
drivers/infiniband/core/nldev.c +18 -9
@@ -1177,6 +1177,24 @@ static int nldev_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh,if (!device)return -EINVAL ; +if (tb[RDMA_NLDEV_NET_NS_FD ]) { +char name[IB_DEVICE_NAME_MAX ] = {}; +u32 ns_fd; + +if (tb[RDMA_NLDEV_ATTR_DEV_NAME ]) { +nla_strscpy (name, tb[RDMA_NLDEV_ATTR_DEV_NAME ], +IB_DEVICE_NAME_MAX ); +if (strlen (name) ==0 ) { + err = -EINVAL ; + goto done; + } + } + ns_fd =nla_get_u32 (tb[RDMA_NLDEV_NET_NS_FD ]); + err =ib_device_set_netns_put (skb, device, ns_fd, + name[0 ] ? name :NULL , extack); + goto put_done; + } +if (tb[RDMA_NLDEV_ATTR_DEV_NAME ]) {char name[IB_DEVICE_NAME_MAX ] = {};
@@ -1190,15 +1208,6 @@ static int nldev_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh, goto done; } -if (tb[RDMA_NLDEV_NET_NS_FD ]) { -u32 ns_fd; - - ns_fd =nla_get_u32 (tb[RDMA_NLDEV_NET_NS_FD ]); - err =ib_device_set_netns_put (skb, device, ns_fd,NULL , - extack); - goto put_done; - } -if (tb[RDMA_NLDEV_ATTR_DEV_DIM ]) {u8 use_dim;
include/uapi/rdma/rdma_netlink.h +4 -1
@@ -516,7 +516,10 @@ enum rdma_nldev_attr {RDMA_NLDEV_ATTR_DEV_PROTOCOL ,/* string */ /* -* File descriptor handle of the net namespace object +* File descriptor handle of the net namespace object. May be combined +* with RDMA_NLDEV_ATTR_DEV_NAME (a literal device name) to also rename +* the device in the destination namespace; the move fails with -EEXIST +* if that name is already taken there. */ RDMA_NLDEV_NET_NS_FD, /* u32 *//*
Signature · 1 line
2.54.0
PATCH 6/13 net/smc: Look up the pnetid ib device within the net namespace
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Scope smc_pnet_find_ib() to the caller's net namespace so pnetid setup cannot bind to a same-named RDMA device from another namespace once names become per-netns.
--- net/smc/smc_pnet.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
net/smc/smc_pnet.c +14 -6
@@ -304,13 +304,18 @@ static bool smc_pnetid_valid(const char *pnet_name, char *pnetid)return true; } -/* Find an infiniband device by a given name. The device might not exist. */ -static struct smc_ib_device *smc_pnet_find_ib (char *ib_name) +/* +* Find an infiniband device by a given name, restricted to the devices +* accessible from @net. The device might not exist. +*/ +static struct smc_ib_device *smc_pnet_find_ib (struct net *net,char *ib_name) {struct smc_ib_device *ibdev;mutex_lock (&smc_ib_devices.mutex);list_for_each_entry (ibdev, &smc_ib_devices.list, list) { +if (!rdma_dev_access_netns (ibdev->ibdev, net)) +continue ; if (!strncmp(ibdev->ibdev->name, ib_name, sizeof(ibdev->ibdev->name)) || (ibdev->ibdev->dev.parent &&
@@ -408,8 +413,8 @@ static int smc_pnet_add_eth(struct smc_pnettable *pnettable, struct net *net,return rc; } -static int smc_pnet_add_ib (struct smc_pnettable *pnettable,char *ib_name, -u8 ib_port,char *pnet_name) +static int smc_pnet_add_ib (struct smc_pnettable *pnettable,struct net *net, +char *ib_name,u8 ib_port,char *pnet_name) {struct smc_pnetentry *tmp_pe, *new_pe;struct smc_ib_device *ib_dev;
@@ -419,7 +424,7 @@ static int smc_pnet_add_ib(struct smc_pnettable *pnettable, char *ib_name,bool new_ibdev;/* try to apply the pnetid to active devices */ - ib_dev =smc_pnet_find_ib (ib_name); + ib_dev =smc_pnet_find_ib (net, ib_name);if (ib_dev) { ibdev_applied =smc_pnet_apply_ib (ib_dev, ib_port, pnet_name);if (ibdev_applied)
@@ -518,7 +523,7 @@ static int smc_pnet_enter(struct net *net, struct nlattr *tb[])if (ibport <1 || ibport >SMC_MAX_PORTS ) goto error; } - rc =smc_pnet_add_ib (pnettable, string, ibport, pnet_name); + rc =smc_pnet_add_ib (pnettable, net, string, ibport, pnet_name);if (!rc) new_ibdev = true;else if (rc != -EEXIST )
@@ -1170,6 +1175,9 @@ int smc_pnetid_by_table_ib(struct smc_ib_device *smcibdev, u8 ib_port)struct smc_net *sn;int rc = -ENOENT ; +if (!rdma_dev_access_netns (smcibdev->ibdev, &init_net)) +return -ENOENT ; +/* get pnettable for init namespace */ sn =net_generic (&init_net, smc_net_id); pnettable = &sn->pnettable;
Signature · 1 line
2.54.0
PATCH 7/13 RDMA/srp: Make the SRP sysfs class net namespace aware
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Tag srp_class by the RDMA device's net namespace so SRP hosts derived from same-named RDMA devices can coexist across namespaces.
--- drivers/infiniband/ulp/srp/ib_srp.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
drivers/infiniband/ulp/srp/ib_srp.c +15 -1
@@ -3189,10 +3189,24 @@ static struct attribute *srp_class_attrs[];ATTRIBUTE_GROUPS (srp_class); +/* +* SRP hosts are named after their ib device, so tag the class by the ib +* device's net namespace. +*/ +static const struct ns_common *srp_net_namespace (const struct device *dev) +{ + struct srp_host *host = container_of(dev, struct srp_host, dev); +struct net *net =rdma_dev_net (host->srp_dev->dev); + +return net ?to_ns_common (net) :NULL ; +} +static struct class srp_class = { .name ="infiniband_srp" , .dev_groups = srp_class_groups, - .dev_release = srp_release_dev + .dev_release = srp_release_dev, + .ns_type = &net_ns_type_operations, + .namespace = srp_net_namespace, };/**
Signature · 1 line
2.54.0
PATCH 8/13 RDMA/cgroup: Scope rdma cgroup device visibility to the net namespace
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Track each rdma cgroup device's net namespace and sharing mode, then filter name lookups and cgroupfs enumeration to devices visible from the caller's namespace. Keep the cached sharing mode synchronized across registration, netns moves, and runtime mode changes.
--- Documentation/admin-guide/cgroup-v2.rst | 7 +++++++ drivers/infiniband/core/cgroup.c | 12 ++++++++++++ drivers/infiniband/core/core_priv.h | 12 ++++++++++++ drivers/infiniband/core/device.c | 11 +++++++++++ include/linux/cgroup_rdma.h | 10 ++++++++++ kernel/cgroup/rdma.c | 20 +++++++++++++++++++- 6 files changed, 71 insertions(+), 1 deletion(-)
Documentation/admin-guide/cgroup-v2.rst +7 -0
@@ -2752,6 +2752,13 @@ RDMA The "rdma" controller regulates the distribution and accounting of RDMA resources. +When RDMA devices are isolated per network namespace (exclusive mode), +device names are unique only within a network namespace. The device lines +below are therefore scoped to the reading or writing process's network +namespace: only devices accessible from that namespace are listed, and a +limit is applied to the device of that name in that namespace. Configure +limits from the same network namespace as the workloads. + RDMA Interface Files ~~~~~~~~~~~~~~~~~~~~
drivers/infiniband/core/cgroup.c +12 -0
@@ -17,6 +17,8 @@void ib_device_register_rdmacg (struct ib_device *device) { device->cg_device.name = device->name; + device->cg_device.netns_shared = ib_devices_shared_netns; +write_pnet (&device->cg_device.net,rdma_dev_net (device));rdmacg_register_device (&device->cg_device); }
@@ -34,6 +36,16 @@ void ib_device_unregister_rdmacg(struct ib_device *device)rdmacg_unregister_device (&device->cg_device); } +void ib_device_rdmacg_change_netns (struct ib_device *device,struct net *net) +{ +write_pnet (&device->cg_device.net, net); +} + +void ib_device_rdmacg_set_netns_shared (struct ib_device *device,bool shared) +{ +WRITE_ONCE (device->cg_device.netns_shared, shared); +} +int ib_rdmacg_try_charge (struct ib_rdmacg_object *cg_obj,struct ib_device *device,enum rdmacg_resource_type resource_index)
drivers/infiniband/core/core_priv.h +12 -0
@@ -156,6 +156,8 @@ void ib_dispatch_event_clients(struct ib_event *event);#ifdef CONFIG_CGROUP_RDMA void ib_device_register_rdmacg (struct ib_device *device);void ib_device_unregister_rdmacg (struct ib_device *device); +void ib_device_rdmacg_change_netns (struct ib_device *device,struct net *net); +void ib_device_rdmacg_set_netns_shared (struct ib_device *device,bool shared); int ib_rdmacg_try_charge(struct ib_rdmacg_object *cg_obj, struct ib_device *device,
@@ -173,6 +175,16 @@ static inline void ib_device_unregister_rdmacg(struct ib_device *device) { } +static inline void ib_device_rdmacg_change_netns (struct ib_device *device, +struct net *net) +{ +} + +static inline void ib_device_rdmacg_set_netns_shared (struct ib_device *device, +bool shared) +{ +} +static inline int ib_rdmacg_try_charge (struct ib_rdmacg_object *cg_obj,struct ib_device *device,enum rdmacg_resource_type resource_index)
drivers/infiniband/core/device.c +11 -0
@@ -1112,6 +1112,7 @@ static int add_all_compat_devs(void)int rdma_compatdev_set (u8 enable) {struct rdma_dev_net *rnet; +struct ib_device *dev;unsigned long index;int ret =0 ;
@@ -1134,6 +1135,12 @@ int rdma_compatdev_set(u8 enable)if (ret)return -EBUSY ; +/* Keep each registered device's rdma cgroup visibility in sync. */ +down_read (&devices_rwsem); +xa_for_each_marked (&devices, index, dev,DEVICE_REGISTERED ) +ib_device_rdmacg_set_netns_shared (dev, enable); +up_read (&devices_rwsem); +if (enable) ret =add_all_compat_devs ();else
@@ -1350,6 +1357,7 @@ static int enable_device_and_get(struct ib_device *device)*/ refcount_set (&device->refcount,2 );down_write (&devices_rwsem); +ib_device_rdmacg_set_netns_shared (device, ib_devices_shared_netns);xa_set_mark (&devices, device->index,DEVICE_REGISTERED );/*
@@ -1823,12 +1831,14 @@ static int rdma_dev_change_netns(struct ib_device *device, struct net *cur_net,"%s: failed to pick device name during namespace teardown: %d\n" , __func__, ret);write_pnet (&device->coredev.rdma_net, net); +ib_device_rdmacg_change_netns (device, net); ret =0 ; } goto rename_done; }write_pnet (&device->coredev.rdma_net, net); +ib_device_rdmacg_change_netns (device, net); ret =device_rename (&device->dev, new_name);if (ret) {if (fallback_pattern) {
@@ -1842,6 +1852,7 @@ static int rdma_dev_change_netns(struct ib_device *device, struct net *cur_net, __func__);/* Try and put things back and re-enable the device */ write_pnet (&device->coredev.rdma_net, cur_net); +ib_device_rdmacg_change_netns (device, cur_net); } }else { strscpy(device->name, dev_name(&device->dev),
include/linux/cgroup_rdma.h +10 -0
@@ -7,6 +7,7 @@#define _CGROUP_RDMA_H#include <linux/cgroup.h> +#include <net/net_namespace.h> enum rdmacg_resource_type {RDMACG_RESOURCE_HCA_HANDLE ,
@@ -34,6 +35,15 @@ struct rdmacg_device {struct list_head dev_node;struct list_head rpools;char *name; +/* +* Net namespace the device belongs to. @netns_shared mirrors +* ib_devices_shared_netns: when true the device is visible from every +* net namespace (shared mode); otherwise @net is the only namespace +* that may see and configure it. @netns_shared is updated when the +* sharing mode changes, so use {READ,WRITE}_ONCE() to access it. +*/ +possible_net_t net; +bool netns_shared; };/*
kernel/cgroup/rdma.c +19 -1
@@ -15,6 +15,7 @@#include <linux/cgroup.h> #include <linux/parser.h> #include <linux/cgroup_rdma.h> +#include <linux/nsproxy.h> #define RDMACG_MAX_STR "max"
@@ -464,6 +465,13 @@ void rdmacg_unregister_device(struct rdmacg_device *device) }EXPORT_SYMBOL (rdmacg_unregister_device); +/* netns_shared is toggled without rdmacg_mutex, hence READ_ONCE(). */ +static bool rdmacg_device_visible (const struct rdmacg_device *device) +{ +return READ_ONCE (device->netns_shared) || +net_eq (read_pnet (&device->net), current->nsproxy->net_ns); +} +static struct rdmacg_device *rdmacg_get_device_locked (const char *name) {struct rdmacg_device *device;
@@ -471,7 +479,8 @@ static struct rdmacg_device *rdmacg_get_device_locked(const char *name)lockdep_assert_held (&rdmacg_mutex);list_for_each_entry (device, &rdmacg_devices, dev_node) -if (!strcmp (name, device->name)) +if (rdmacg_device_visible (device) && + !strcmp (name, device->name))return device;return NULL ;
@@ -626,6 +635,9 @@ static int rdmacg_resource_read(struct seq_file *sf, void *v)mutex_lock (&rdmacg_mutex);list_for_each_entry (device, &rdmacg_devices, dev_node) { +if (!rdmacg_device_visible (device)) +continue ; +seq_printf (sf,"%s " , device->name); rpool =find_cg_rpool_locked (cg, device);
@@ -648,6 +660,9 @@ static int rdmacg_events_show(struct seq_file *sf, void *v)mutex_lock (&rdmacg_mutex);list_for_each_entry (device, &rdmacg_devices, dev_node) { +if (!rdmacg_device_visible (device)) +continue ; + rpool =find_cg_rpool_locked (cg, device);seq_printf (sf,"%s " , device->name);
@@ -677,6 +692,9 @@ static int rdmacg_events_local_show(struct seq_file *sf, void *v)mutex_lock (&rdmacg_mutex);list_for_each_entry (device, &rdmacg_devices, dev_node) { +if (!rdmacg_device_visible (device)) +continue ; + rpool =find_cg_rpool_locked (cg, device);seq_printf (sf,"%s " , device->name);
Signature · 1 line
2.54.0
PATCH 9/13 RDMA/cma: Document that CM configfs cannot be net namespace scoped
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Document the rdma_cm configfs limitation: configfs is global, so same-named RDMA devices in different net namespaces cannot both be represented there.
--- Documentation/ABI/testing/configfs-rdma_cm | 4 ++++ drivers/infiniband/core/cma_configfs.c | 4 ++++ 2 files changed, 8 insertions(+)
Documentation/ABI/testing/configfs-rdma_cm +4 -0
@@ -12,6 +12,10 @@ Description: Interface is used to configure RDMA-cable HCAs in respect to for this HCA has to be created: mkdir -p /config/rdma_cm/<hca> + Note: configfs has no network namespace support, so this + interface cannot represent two devices that share a name in + different network namespaces (possible in exclusive netns mode). + What: /config/rdma_cm/<hca>/ports/<port-num>/default_roce_mode Date: November 29, 2015
drivers/infiniband/core/cma_configfs.c +4 -0
@@ -65,6 +65,10 @@ static struct cma_dev_port_group *to_dev_port_group(struct config_item *item) return container_of(group, struct cma_dev_port_group, group); } +/* +* configfs is not net namespace aware, so a name shared by devices in +* different namespaces resolves to the first match here. +*/ static bool filter_by_name (struct ib_device *ib_dev,void *cookie) {return !strcmp (dev_name (&ib_dev->dev), cookie);
Signature · 1 line
2.54.0
PATCH 10/13 RDMA/core: Document the SELinux ibendport net namespace limitation
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Document that SELinux ibendport labels use a global (device name, port) key, so same-named RDMA devices in different net namespaces share a label.
--- drivers/infiniband/core/security.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/infiniband/core/security.c +6 -0
@@ -700,6 +700,12 @@ int ib_mad_agent_security_setup(struct ib_mad_agent *agent,if (qp_type !=IB_QPT_SMI )return 0 ; +/* +* SELinux labels an endport by (device name, port) from a global +* policy. If devices in different net namespaces share a name, they get +* the same label; distinguishing them would need net namespace support +* in the policy language and tooling. +*/ spin_lock (&mad_agent_list_lock); ret = security_ib_endport_manage_subnet(agent->security, dev_name(&agent->device->dev),
Signature · 1 line
2.54.0
PATCH 11/13 RDMA/core: Make device names unique per net namespace
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Use rdma_dev_access_netns() to scope RDMA device name lookup and "%d" name allocation to the relevant net namespace. Keep shared mode and CONFIG_NET_NS=n behaviour system-wide.
--- drivers/infiniband/core/device.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-)
drivers/infiniband/core/device.c +15 -7
@@ -129,7 +129,7 @@ static DECLARE_RWSEM(rdma_nets_rwsem);bool ib_devices_shared_netns = true;module_param_named (netns_mode, ib_devices_shared_netns, bool,0444 );MODULE_PARM_DESC (netns_mode, -"Share device among net namespaces; default=1 (shared)" ); +"Share device among net namespaces; default=1 (shared). In exclusive mode device names are unique per net namespace" );/** * rdma_dev_access_netns() - Return whether an rdma device can be accessed * from a specified net namespace or not.
@@ -359,7 +359,8 @@ static struct ib_device *__ib_device_get_by_name(const char *name,unsigned long index; xa_for_each (&devices, index, device) -if (!strcmp (name,dev_name (&device->dev))) +if (rdma_dev_access_netns (device, net) && + !strcmp (name,dev_name (&device->dev)))return device;return NULL ;
@@ -437,7 +438,11 @@ int ib_device_set_dim(struct ib_device *ibdev, u8 use_dim)return 0 ; } -/* Pick a free index for the '%d' style @name pattern. */ +/* +* Pick a free index for the '%d' style @name pattern within net namespace +* @net. Returns the index on success or a negative errno. The caller builds +* the final unique device name from the returned index. +*/ static int __alloc_name_id (struct net *net,const char *name,const struct ib_device *skip) {
@@ -452,7 +457,7 @@ static int __alloc_name_id(struct net *net, const char *name, xa_for_each (&devices, index, device) {char buf[IB_DEVICE_NAME_MAX ]; -if (device == skip) +if (device == skip || !rdma_dev_access_netns (device, net))continue ;if (sscanf (dev_name (&device->dev), name, &i) !=1 )continue ;
@@ -1240,7 +1245,8 @@ static __net_init int rdma_dev_init_net(struct net *net) }/* -* Assign the unique string device name and the unique device index. This is +* Assign the unique string device name and the unique device index. The device +* name is unique within the net namespace the device is assigned to. This is * undone by ib_dealloc_device. */ static int assign_name (struct ib_device *device,const char *name)
@@ -1424,8 +1430,9 @@ static void ib_device_notify_register(struct ib_device *device)/** * ib_register_device - Register an IB device with IB core * @device: Device to register -* @name: unique string device name. This may include a '%' which will -* cause a unique index to be added to the passed device name. +* @name: device name, unique within the device's net namespace. This may +* include a '%' which will cause a unique index to be added to the +* passed device name. * @dma_device: pointer to a DMA-capable device. If %NULL, then the IB * device will be used. In this case the caller should fully * setup the ibdev for DMA. This usually means using dma_virt_ops.
@@ -1716,6 +1723,7 @@ static bool rdma_dev_name_in_netns(struct ib_device *skip, struct net *net,xa_for_each (&devices, index, device)if (device != skip && +rdma_dev_access_netns (device, net) && !strcmp (name,dev_name (&device->dev)))return true;
Signature · 1 line
2.54.0
PATCH 12/13 RDMA/rxe: Implement disassociate_ucontext callback
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Implement an empty disassociate_ucontext() callback so the RDMA core can move rxe devices between net namespaces. The core requires this callback to reset user contexts without waiting for userspace.
rxe needs no teardown here: its user-mapped queues live in reference-counted vmalloc memory (see rxe_mmap.c) that stays valid while userspace holds the mappings.
--- drivers/infiniband/sw/rxe/rxe_verbs.c | 5 +++++ 1 file changed, 5 insertions(+)
drivers/infiniband/sw/rxe/rxe_verbs.c +5 -0
@@ -240,6 +240,10 @@ static void rxe_dealloc_ucontext(struct ib_ucontext *ibuc)rxe_err_uc (uc,"cleanup failed, err = %d\n" , err); } +static void rxe_disassociate_ucontext (struct ib_ucontext *ibuc) +{ +} +/* pd */ static int rxe_alloc_pd (struct ib_pd *ibpd,struct ib_udata *udata) {
@@ -1478,6 +1482,7 @@ static const struct ib_device_ops rxe_dev_ops = { .destroy_srq = rxe_destroy_srq, .detach_mcast = rxe_detach_mcast, .device_group = &rxe_attr_group, + .disassociate_ucontext = rxe_disassociate_ucontext, .enable_driver = rxe_enable_driver, .get_dma_mr = rxe_get_dma_mr, .get_hw_stats = rxe_ib_get_hw_stats,
Signature · 1 line
2.54.0
PATCH 13/13 RDMA/selftests: Add rxe_netns_names test
In reply to Jiri PirkoFrom: Jiri Pirko <[email protected]>
Add a kselftest script that exercises per-netns RDMA device naming with RXE. Cover duplicate names across namespaces, move conflict handling, move-with-rename, and same-namespace rename requests.
--- tools/testing/selftests/rdma/Makefile | 3 +- tools/testing/selftests/rdma/config | 2 + .../testing/selftests/rdma/rxe_netns_names.sh | 282 ++++++++++++++++++ 3 files changed, 286 insertions(+), 1 deletion(-) create mode 100755 tools/testing/selftests/rdma/rxe_netns_names.sh
tools/testing/selftests/rdma/Makefile +2 -1
@@ -3,6 +3,7 @@ TEST_PROGS := rxe_rping_between_netns.sh \ rxe_ipv6.sh \ rxe_socket_with_netns.sh \ rxe_test_NETDEV_UNREGISTER.sh \ - rxe_sent_rcvd_bytes.sh + rxe_sent_rcvd_bytes.sh \ + rxe_netns_names.sh include ../lib.mk
tools/testing/selftests/rdma/config +2 -0
@@ -1,3 +1,5 @@ CONFIG_TUN CONFIG_VETH +CONFIG_DUMMY +CONFIG_NET_NS CONFIG_RDMA_RXE
tools/testing/selftests/rdma/rxe_netns_names.sh new file +282 -0
282 lines not on this page · raw message
Signature · 1 line
2.54.0
Michal Koutný
maintainerRe: [PATCH rdma-next 08/13] RDMA/cgroup: Scope rdma cgroup device visibility to the net namespace
In reply to Jiri PirkoHi.
Documentation/admin-guide/cgroup-v2.rst RDMA · line 2760
Quoted 13 lines · Jiri Pirko
index
993446ab66d0..4523c1884d67100644Documentation/admin-guide/cgroup-v2.rst +6
-0@@ -2752,6 +2752,13 @@ RDMA The "rdma" controller regulates the distribution and accounting of RDMA resources. +When RDMA devices are isolated per network namespace (exclusive mode), +device names are unique only within a network namespace. The device lines +below are therefore scoped to the reading or writing process's network +namespace: only devices accessible from that namespace are listed, and a +limit is applied to the device of that name in that namespace. Configure +limits from the same network namespace as the workloads.
OK.
Quoted 13 lines
include/linux/cgroup_rdma.h +1
-0@@ -7,6 +7,7 @@#define _CGROUP_RDMA_H#include <linux/cgroup.h> +#include <net/net_namespace.h> enum rdmacg_resource_type {RDMACG_RESOURCE_HCA_HANDLE ,@@ -34,6 +35,15 @@ struct rdmacg_device {struct list_head dev_node;struct list_head rpools;
include/linux/cgroup_rdma.h struct rdmacg_device { · line 46
Quoted 10 lines
include/linux/cgroup_rdma.h +9
-0char *name; +/* +* Net namespace the device belongs to. @netns_shared mirrors +* ib_devices_shared_netns: when true the device is visible from every +* net namespace (shared mode); otherwise @net is the only namespace +* that may see and configure it. @netns_shared is updated when the +* sharing mode changes, so use {READ,WRITE}_ONCE() to access it. +*/ +possible_net_t net; +bool netns_shared;
Any reason to store the netns_shared split per device? (IIUC, it's a global parameter.)
Thanks,
Michal
Re: [PATCH rdma-next 12/13] RDMA/rxe: Implement disassociate_ucontext callback
In reply to Jiri PirkoQuoted 9 lines · Jiri Pirko
From: Jiri Pirko <[email protected]>
Implement an empty
disassociate_ucontext()callback so the RDMA core can move rxe devices between net namespaces. The core requires this callback to reset user contexts without waiting for userspace.rxe needs no teardown here: its user-mapped queues live in reference-counted vmalloc memory (see
rxe_mmap.c) that stays valid while userspace holds the mappings.
The logic here is correct for enabling netns migration. Since RXE's user-mapped
queues rely on reference-counted vmalloc memory, this empty stub is safe enough
to prevent kernel panics or Use-After-Free (UAF) during device movement.
However, as a note for future improvement: keeping this callback entirely empty
means userspace won't be immediately notified (via SIGBUS or page table zapping)
when the underlying device is moved out of its netns. For now, this serves well as
a functional enabler, but we might want to introduce proper PTE tearing/zap_vma_ptes()
down the road if applications require strict, immediate disconnection semantics.
Thanks Jiri.
Zhu Yanjun
Quoted 28 lines
Signed-off-by Jiri Pirko <[email protected]>--- drivers/infiniband/sw/rxe/rxe_verbs.c | 5 +++++ 1 file changed, 5 insertions(+)drivers/infiniband/sw/rxe/rxe_verbs.c +5
-0@@ -240,6 +240,10 @@ static void rxe_dealloc_ucontext(struct ib_ucontext *ibuc)rxe_err_uc (uc,"cleanup failed, err = %d\n" , err); } +static void rxe_disassociate_ucontext (struct ib_ucontext *ibuc) +{ +} +/* pd */ static int rxe_alloc_pd (struct ib_pd *ibpd,struct ib_udata *udata) {@@ -1478,6 +1482,7 @@ static const struct ib_device_ops rxe_dev_ops = { .destroy_srq = rxe_destroy_srq, .detach_mcast = rxe_detach_mcast, .device_group = &rxe_attr_group, + .disassociate_ucontext = rxe_disassociate_ucontext, .enable_driver = rxe_enable_driver, .get_dma_mr = rxe_get_dma_mr, .get_hw_stats = rxe_ib_get_hw_stats,
Signature · 2 lines
Best Regards, Yanjun.Zhu
Re: [PATCH rdma-next 13/13] RDMA/selftests: Add rxe_netns_names test
In reply to Jiri PirkoQuoted 5 lines · Jiri Pirko
From: Jiri Pirko <[email protected]>
Add a kselftest script that exercises per-netns RDMA device naming with RXE. Cover duplicate names across namespaces, move conflict handling, move-with-rename, and same-namespace rename requests.
This is a very comprehensive test suite for the per-netns RDMA device naming infra.
I especially appreciate the inclusion of the 'TEST_TEARDOWN_RETURN' case—ensuring
that the kernel's automatic renaming and fallback mechanics work seamlessly during
netns deletion is crucial for long-term stability.
One minor thing to clean up before pushing to the tree: the script declares 'ktap_set_plan 7'
but actually defines 6 distinct test cases in the execution block. I will fix this plan count to 6
to avoid any "bad plan" warnings in automated CI frameworks (like KernelCI).
Aside from that, the cleanup paths and setup tracking are solid.
Thanks for adding this.
Zhu Yanjun
Quoted 320 lines
Signed-off-by Jiri Pirko <[email protected]>--- tools/testing/selftests/rdma/Makefile | 3 +- tools/testing/selftests/rdma/config | 2 + .../testing/selftests/rdma/rxe_netns_names.sh | 282 ++++++++++++++++++ 3 files changed, 286 insertions(+), 1 deletion(-)create mode 100755
tools/testing/selftests/rdma/rxe_netns_names.shtools/testing/selftests/rdma/Makefile +2
-1@@ -3,6 +3,7 @@ TEST_PROGS := rxe_rping_between_netns.sh \ rxe_ipv6.sh \ rxe_socket_with_netns.sh \ rxe_test_NETDEV_UNREGISTER.sh \ - rxe_sent_rcvd_bytes.sh + rxe_sent_rcvd_bytes.sh \ + rxe_netns_names.sh include ../lib.mktools/testing/selftests/rdma/config +2
-0@@ -1,3 +1,5 @@ CONFIG_TUN CONFIG_VETH +CONFIG_DUMMY +CONFIG_NET_NS CONFIG_RDMA_RXEtools/testing/selftests/rdma/rxe_netns_names.sh new file +282
-0@@ -0,0 +1,282 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# +# Exercise RDMA device name handling across network namespaces. + +source "$(dirname "$0")/../kselftest/ktap_helpers.sh" + +NAME_PREFIX="rxe_netns_names_$$" +NETDEV_PREFIX="rxn$$" +NS1="${NAME_PREFIX}ns1" +NS2="${NAME_PREFIX}ns2" +RXE_A="${NAME_PREFIX}rxe_a" +RXE_B="${NAME_PREFIX}rxe_b" +RXE_SAME="${NAME_PREFIX}rxe_same" +RXE_NEW="${NAME_PREFIX}rxe_new" +DUMMY_A="${NETDEV_PREFIX}a" +DUMMY_B="${NETDEV_PREFIX}b" +OLD_MODE="" +MODE_CHANGED=0 +MODS=("dummy" "rdma_rxe") +TEST_SAME_NAMES="same RDMA device name can exist in two net namespaces" +TEST_MOVE_CONFLICT="move without rename fails on destination name conflict" +TEST_MOVE_RENAME="move then rename succeeds" +TEST_COMBINED_MOVE_RENAME="move with requested destination name succeeds" +TEST_SAME_NETNS_DUP_RENAME="same-netns rename rejects duplicate name" +TEST_TEARDOWN_RETURN="netns delete returns device to init_net and renames on conflict" + +ksft_skip() +{ + ktap_skip_all "$*" + exit "$KSFT_SKIP" +} + +fail() +{ + ktap_exit_fail_msg "$*" +} + +need_cmd() +{ + command -v "$1" >/dev/null 2>&1 || ksft_skip "missing command: $1" +} + +rdma_ns() +{ + local ns=$1 + + shift + ip netns exec "$ns" rdma "$@" +} + +rdma_dev_exists() +{ + local ns=$1 + local dev=$2 + + if [ -n "$ns" ]; then + rdma_ns "$ns" dev show "$dev" >/dev/null 2>&1 + else + rdma dev show "$dev" >/dev/null 2>&1 + fi +} + +add_dummy() +{ + local netdev=$1 + + ip link add "$netdev" type dummy || return 1 + ip link set "$netdev" up || return 1 +} + +add_rxe() +{ + local dev=$1 + local netdev=$2 + + rdma link add "$dev" type rxe netdev "$netdev" +} + +rdma_dev_on_netdev() +{ + local netdev=$1 + + rdma link show 2>/dev/null | awk -v want="$netdev" ' + { + for (i = 1; i < NF; i++) + if ($i == "netdev" && $(i + 1) == want) { + dev = $2 + sub(/\/.*/, "", dev) + print dev + exit + } + }' +} + +wait_rdma_dev_on_netdev() +{ + local netdev=$1 + local dev + local i + + for i in $(seq 1 50); do + dev=$(rdma_dev_on_netdev "$netdev") + if [ -n "$dev" ]; then + echo "$dev" + return 0 + fi + sleep 0.1 + done + + return 1 +} + +setup_devs() +{ + cleanup_devs + + add_dummy "$DUMMY_A" || return 1 + add_dummy "$DUMMY_B" || return 1 + + add_rxe "$RXE_A" "$DUMMY_A" || return 1 + add_rxe "$RXE_B" "$DUMMY_B" || return 1 +} + +cleanup_devs() +{ + ip link del "$DUMMY_A" 2>/dev/null + ip link del "$DUMMY_B" 2>/dev/null +} + +setup() +{ + OLD_MODE=$(rdma system show 2>/dev/null | + sed -n 's/.*netns \([^ ]*\).*/\1/p') + [ -n "$OLD_MODE" ] || ksft_skip "failed to read RDMA netns mode" + + rdma system set netns exclusive >/dev/null 2>&1 || + ksft_skip "rdma netns exclusive mode is not supported" + MODE_CHANGED=1 + + ip netns add "$NS1" || return 1 + ip netns add "$NS2" || return 1 +} + +cleanup() +{ + cleanup_devs + + ip netns del "$NS1" 2>/dev/null + ip netns del "$NS2" 2>/dev/null + + if [ "$MODE_CHANGED" -eq 1 ]; then + rdma system set netns "$OLD_MODE" 2>/dev/null + fi + + for m in "${MODS[@]}"; do + modprobe -r "$m" 2>/dev/null + done +} + +rdma_supports_combined_move_rename() +{ + rdma dev help 2>&1 | grep -Eq 'netns .*name|name .*netns' +} + +[ "$(id -u)" -eq 0 ] || ksft_skip "must be run as root" +need_cmd ip +need_cmd rdma +need_cmd modprobe + +trap cleanup EXIT + +for m in "${MODS[@]}"; do + modinfo "$m" >/dev/null 2>&1 || ksft_skip "module $m not found" + modprobe "$m" || fail "failed to load $m" +done + +setup || fail "failed to create net namespaces" + +ktap_print_header +ktap_set_plan 7 + +if setup_devs && + rdma dev set "$RXE_A" netns "$NS1" && + rdma_ns "$NS1" dev set "$RXE_A" name "$RXE_SAME" && + rdma dev set "$RXE_B" netns "$NS2" && + rdma_ns "$NS2" dev set "$RXE_B" name "$RXE_SAME" && + rdma_dev_exists "$NS1" "$RXE_SAME" && + rdma_dev_exists "$NS2" "$RXE_SAME"; then + ktap_test_pass "$TEST_SAME_NAMES" +else + ktap_test_fail "$TEST_SAME_NAMES" +fi +cleanup_devs + +if ! setup_devs || + ! rdma dev set "$RXE_A" netns "$NS1" || + ! rdma_ns "$NS1" dev set "$RXE_A" name "$RXE_SAME" || + ! rdma dev set "$RXE_B" netns "$NS2" || + ! rdma_ns "$NS2" dev set "$RXE_B" name "$RXE_SAME"; then + ktap_test_fail "$TEST_MOVE_CONFLICT" +elif rdma_ns "$NS1" dev set "$RXE_SAME" netns "$NS2" >/dev/null 2>&1; then + ktap_test_fail "$TEST_MOVE_CONFLICT" +elif rdma_dev_exists "$NS1" "$RXE_SAME" && + rdma_dev_exists "$NS2" "$RXE_SAME"; then + ktap_test_pass "$TEST_MOVE_CONFLICT" +else + ktap_test_fail "$TEST_MOVE_CONFLICT" +fi +cleanup_devs + +if ! setup_devs; then + ktap_test_fail "$TEST_MOVE_RENAME" +elif rdma dev set "$RXE_A" netns "$NS2" && + rdma_ns "$NS2" dev set "$RXE_A" name "$RXE_NEW"; then + if rdma_dev_exists "$NS2" "$RXE_NEW" && + ! rdma_dev_exists "" "$RXE_A"; then + ktap_test_pass "$TEST_MOVE_RENAME" + else + ktap_test_fail "$TEST_MOVE_RENAME" + fi +else + ktap_test_fail "$TEST_MOVE_RENAME" +fi +cleanup_devs + +if ! rdma_supports_combined_move_rename; then + ktap_test_skip "$TEST_COMBINED_MOVE_RENAME" +elif ! setup_devs; then + ktap_test_fail "$TEST_COMBINED_MOVE_RENAME" +elif rdma dev set "$RXE_A" netns "$NS2" name "$RXE_NEW"; then + if rdma_dev_exists "$NS2" "$RXE_NEW" && + ! rdma_dev_exists "" "$RXE_A"; then + ktap_test_pass "$TEST_COMBINED_MOVE_RENAME" + else + ktap_test_fail "$TEST_COMBINED_MOVE_RENAME" + fi +else + ktap_test_fail "$TEST_COMBINED_MOVE_RENAME" +fi +cleanup_devs + +if ! setup_devs; then + ktap_test_fail "$TEST_SAME_NETNS_DUP_RENAME" +elif rdma dev set "$RXE_A" name "$RXE_SAME" && + rdma dev set "$RXE_B" name "$RXE_NEW"; then + if rdma dev set "$RXE_A" name "$RXE_NEW" >/dev/null 2>&1; then + ktap_test_fail "$TEST_SAME_NETNS_DUP_RENAME" + elif rdma_dev_exists "" "$RXE_SAME" && + rdma_dev_exists "" "$RXE_NEW"; then + ktap_test_pass "$TEST_SAME_NETNS_DUP_RENAME" + else + ktap_test_fail "$TEST_SAME_NETNS_DUP_RENAME" + fi +else + ktap_test_fail "$TEST_SAME_NETNS_DUP_RENAME" +fi +cleanup_devs + +if ! setup_devs; then + ktap_test_fail "$TEST_TEARDOWN_RETURN" +elif ! rdma dev set "$RXE_A" name "$RXE_SAME" || + ! rdma dev set "$RXE_B" netns "$NS2" || + ! rdma_ns "$NS2" dev set "$RXE_B" name "$RXE_SAME" || + ! rdma_dev_exists "$NS2" "$RXE_SAME"; then + ktap_test_fail "$TEST_TEARDOWN_RETURN" +else + ip netns del "$NS2" + returned=$(wait_rdma_dev_on_netdev "$DUMMY_B") + ktap_print_msg "device returned to init_net as '${returned:-<missing>}'" + if rdma_dev_exists "" "$RXE_SAME" && + [ -n "$returned" ] && + [ "$returned" != "$RXE_SAME" ] && + [ "${returned#ibdev}" != "$returned" ]; then + ktap_test_pass "$TEST_TEARDOWN_RETURN" + else + ktap_test_fail "$TEST_TEARDOWN_RETURN" + fi +fi +cleanup_devs + +ktap_finished
Signature · 2 lines
Best Regards, Yanjun.Zhu
[email protected]
AI reviewRe: [PATCH rdma-next 03/13] RDMA/core: Support renaming a device when changing its net namespace
In reply to Jiri PirkoThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The destination device name requested by userspace is ignored during a net namespace move…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The destination device name requested by userspace is ignored during a net namespace move because NULL is hardcoded in the netlink handler. --
--- Patch [3]: [PATCH rdma-next 03/13] RDMA/core: Support renaming a device when changing its net namespace --- commit 481a893ec5cd10a7f8be8c8de249d86cc283c470
Author: Jiri Pirko <[email protected]>
RDMA/core: Support renaming a device when changing its net namespace
This patch updates the RDMA core to allow requesting a destination device name when moving a device to a different network namespace. It modifies ib_device_set_netns_put to take a requested name and implements the naming rules during namespace teardown.
drivers/infiniband/core/nldev.c static int nldev_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh, · line 1197
Quoted 10 lines
drivers/infiniband/core/nldev.c +1
-1@@ -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 );
[Severity: Medium]
Does this hardcode NULL instead of extracting and passing the user-provided device name?
The commit message mentions allowing namespace moves to request a destination device name, but it appears ib_device_set_netns_put is always called with NULL here.
How does this interact with the rename logic in nldev_set_doit? If a user supplies both the RDMA_NLDEV_ATTR_DEV_NAME and RDMA_NLDEV_NET_NS_FD attributes in the netlink command, will the code enter the rename logic first and return, completely skipping the net namespace change instead of moving and renaming the device?
Quoted 3 lines
drivers/infiniband/core/nldev.c +0
-0if (err == -EEXIST )NL_SET_ERR_MSG (extack,"Device name already exists in the target net namespace" );
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
[email protected]
AI reviewRe: [PATCH rdma-next 02/13] RDMA/core: Handle device name conflicts when changing net namespace
In reply to Jiri PirkoThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] User-triggerable WARN() during concurrent RDMA device unregistration and netns exit, leading to…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] User-triggerable WARN() during concurrent RDMA device unregistration and netns exit, leading to DoS on panic_on_warn systems. --
--- Patch [2]: [PATCH rdma-next 02/13] RDMA/core: Handle device name conflicts when changing net namespace --- commit 05c3f87e2143d43f2f1e27d0f13e1b1f97f35bab
Author: Jiri Pirko <[email protected]>
RDMA/core: Handle device name conflicts when changing net namespace
This commit prepares namespace moves for per-netns names by checking user-initiated moves for destination-name conflicts before disabling the device. It also ensures teardown moves detach from the exiting namespace even if fallback naming fails.
Quoted 8 lines
drivers/infiniband/core/device.c +1
-0@@ -1160,8 +1168,17 @@ static void rdma_dev_exit_net(struct net *net)/* * If the real device is in the NS then move it back to init. +* Provide a fallback pattern so a name conflict in init_net
drivers/infiniband/core/device.c static void rdma_dev_exit_net(struct net *net) · line 1180
Quoted 10 lines
drivers/infiniband/core/device.c +8
-1+* cannot make the teardown move fail. */ -rdma_dev_change_netns (dev, net, &init_net); +if (net_eq (net,read_pnet (&dev->coredev.rdma_net))) { + ret =rdma_dev_change_netns (dev, net, &init_net, +"ibdev%d" ); +if (ret) +WARN (1 , +"Failed to move RDMA device %s to init_net on netns exit: %d\n" , +dev_name (&dev->dev), ret);
[Severity: High]
Can this WARN(1) be triggered by a concurrent device unregistration?
Looking at rdma_dev_change_netns(), if a device is concurrently unregistered, the refcount drops to 0 and the function legitimately returns -ENODEV:
drivers/infiniband/core/device.c:rdma_dev_change_netns() {
...
if (refcount_read(&device->refcount) == 0 ||
!net_eq(cur_net, read_pnet(&device->coredev.rdma_net))) {
ret = -ENODEV;
goto out;
}
...
}If rdma_dev_exit_net() runs concurrently with device unregistration (e.g., module unload, PCI hotplug, or link deletion of a software RDMA device like RXE), rdma_dev_change_netns() will return -ENODEV and trigger this warning.
Because an unprivileged user can acquire CAP_NET_ADMIN in a user namespace, create an RXE device, and race netns deletion with device deletion, could this be used to intentionally crash a host configured with panic_on_warn=1?
Quoted 4 lines
drivers/infiniband/core/device.c +1
-0+ }put_device (&dev->dev);down_read (&devices_rwsem);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH rdma-next 06/13] net/smc: Look up the pnetid ib device within the net namespace
In reply to Jiri PirkoThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] Pre-existing TOCTOU and Use-After-Free race where smc_pnet_find_ib() and smc_pnet_find_smcd() return un-refcounted device pointers.
- [High] Pre-existing Use-After-Free during SMC connection setup due to un-refcounted ib_dev pointer in smc_init_info. --
--- Patch [6]: [PATCH rdma-next 06/13] net/smc: Look up the pnetid ib device within the net namespace --- commit d209b8afd09f2213c9477f5a717fdf012a7c2077
Author: Jiri Pirko <[email protected]>
net/smc: Look up the pnetid ib device within the net namespace
This commit scopes smc_pnet_find_ib() to the caller's net namespace. It ensures that pnetid setup cannot bind to a same-named RDMA device from another namespace.
Quoted 22 lines
net/smc/smc_pnet.c +7
-2@@ -304,13 +304,18 @@ static bool smc_pnetid_valid(const char *pnet_name, char *pnetid)return true; } -/* Find an infiniband device by a given name. The device might not exist. */ -static struct smc_ib_device *smc_pnet_find_ib (char *ib_name) +/* +* Find an infiniband device by a given name, restricted to the devices +* accessible from @net. The device might not exist. +*/ +static struct smc_ib_device *smc_pnet_find_ib (struct net *net,char *ib_name) {struct smc_ib_device *ibdev;mutex_lock (&smc_ib_devices.mutex);list_for_each_entry (ibdev, &smc_ib_devices.list, list) { +if (!rdma_dev_access_netns (ibdev->ibdev, net)) +continue ;
[ ... ]
net/smc/smc_pnet.c static int smc_pnet_add_ib(struct smc_pnettable *pnettable, char *ib_name, · line 430
Quoted 9 lines
net/smc/smc_pnet.c +1
-1@@ -419,7 +424,7 @@ static int smc_pnet_add_ib(struct smc_pnettable *pnettable, char *ib_name,bool new_ibdev;/* try to apply the pnetid to active devices */ - ib_dev =smc_pnet_find_ib (ib_name); + ib_dev =smc_pnet_find_ib (net, ib_name);if (ib_dev) { ibdev_applied =smc_pnet_apply_ib (ib_dev, ib_port, pnet_name);if (ibdev_applied)
[Severity: High]
This isn't a bug introduced by this patch, but smc_pnet_find_ib() in net/smc/smc_pnet.c appears to return an un-refcounted device pointer.
If a user configures a PNETID for an IB or SMCD device via netlink and calls smc_pnet_add_ib(), it locates the device in the global list while holding smc_ib_devices.mutex. However, smc_pnet_find_ib() returns the raw pointer after dropping the mutex.
If the device is concurrently hot-unplugged, smc_ib_remove_dev() or smcd_unregister_dev() will acquire the mutex, remove the device from the list, and kfree() the device structure.
Could the original netlink thread then continue execution and dereference the now-freed pointer inside smc_pnet_apply_ib() or when printing ib_dev->ibdev->name?
[Severity: High]
This is another pre-existing issue in net/smc/smc_pnet.c related to un-refcounted pointers, but it happens during connection setup.
When a user calls connect() on an AF_SMC socket, _smc_pnet_find_roce_by_pnetid() locates an active IB device while holding smc_ib_devices.mutex, and stores its pointer in the smc_init_info structure (ini->ib_dev) before dropping the mutex.
Connection establishment then continues across multiple function calls:smc_find_proposal_devices() -> smc_connect_rdma() -> smc_conn_create() using this un-refcounted pointer.
If the IB device is concurrently hot-unplugged, smc_ib_remove_dev() will kfree() the device. Might the connection setup path then dereference the freed memory?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
[email protected]
AI reviewRe: [PATCH rdma-next 08/13] RDMA/cgroup: Scope rdma cgroup device visibility to the net namespace
In reply to Jiri PirkoThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Scoping RDMA cgroup device visibility to the reader's network namespace breaks host-level cgroup…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Scoping RDMA cgroup device visibility to the reader's network namespace breaks host-level cgroup administration and monitoring.
- [Medium] Concurrent lockless read of device->net using read_pnet() without READ_ONCE() leads to a data race. --
--- Patch [8]: [PATCH rdma-next 08/13] RDMA/cgroup: Scope rdma cgroup device visibility to the net namespace --- commit 18e4bddcfb08bd42bef710d6d1b72824d15fa2c9
Author: Jiri Pirko <[email protected]>
RDMA/cgroup: Scope rdma cgroup device visibility to the net namespace
Track each rdma cgroup device's net namespace and sharing mode, then filter name lookups and cgroupfs enumeration to devices visible from the caller's namespace. Keep the cached sharing mode synchronized across registration, netns moves, and runtime mode changes.
Quoted 4 lines
kernel/cgroup/rdma.c +0
-0
[ ... ]
kernel/cgroup/rdma.c void rdmacg_unregister_device(struct rdmacg_device *device) · line 472
Quoted 9 lines
kernel/cgroup/rdma.c +5
-0@@ -464,6 +465,13 @@ void rdmacg_unregister_device(struct rdmacg_device *device) }EXPORT_SYMBOL (rdmacg_unregister_device); +/* netns_shared is toggled without rdmacg_mutex, hence READ_ONCE(). */ +static bool rdmacg_device_visible (const struct rdmacg_device *device) +{ +return READ_ONCE (device->netns_shared) || +net_eq (read_pnet (&device->net), current->nsproxy->net_ns);
[Severity: Medium]
Does this lockless read of device->net using read_pnet() cause a data race?
When another thread concurrently moves the RDMA device to a new network namespace, it updates device->net:
drivers/infiniband/core/cgroup.c:ib_device_rdmacg_change_netns() {
write_pnet(&device->cg_device.net, net);
}While write_pnet() uses rcu_assign_pointer() to update the pointer concurrently, read_pnet() here expands to a plain pointer access without READ_ONCE(). Since rdmacg_mutex is not held during the write, could this plain read race with the assignment and cause undefined behavior?
[Severity: High]
Does tying RDMA cgroup device visibility to the reader's network namespace break host-level cgroup administration and monitoring?
If a device is moved to a non-init network namespace (exclusive mode), host administrators and orchestration tools (like systemd or Kubernetes running in init_net) will completely lose visibility and control over its RDMA cgroup resources. Attempting to write to rdma.max will result in -ENODEV, and reading rdma.current will show an empty list for these devices.
Since cgroup state must reflect the cgroup's configuration independent of the reading process's network namespace, does tying the UAPI to current->nsproxy->net_ns violate VFS semantics where an open file descriptor's behavior would dynamically change depending on which process calls write()?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
[email protected]
AI reviewRe: [PATCH rdma-next 12/13] RDMA/rxe: Implement disassociate_ucontext callback
In reply to Jiri PirkoThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] Implementing an empty `disassociate_ucontext` callback bypasses the core's VMA zapping because…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] Implementing an empty disassociate_ucontext callback bypasses the core's VMA zapping because RXE uses legacy, untracked VMAs. This leads to a Use-After-Free of ib_ucontext and rxe_dev when the VMA is eventually closed, and potential kernel panics upon module unload. --
--- Patch [12]: [PATCH rdma-next 12/13] RDMA/rxe: Implement disassociate_ucontext callback --- commit 8946fc9f2a378bad6f910a408699c6a70e97a6f6
Author: Jiri Pirko <[email protected]>
RDMA/rxe: Implement disassociate_ucontext callback
Implement an empty disassociate_ucontext() callback so the RDMA core can move rxe devices between net namespaces. The core requires this callback to reset user contexts without waiting for userspace.
drivers/infiniband/sw/rxe/rxe_verbs.c static void rxe_dealloc_ucontext(struct ib_ucontext *ibuc) · line 246
Quoted 12 lines
drivers/infiniband/sw/rxe/rxe_verbs.c +4
-0@@ -240,6 +240,10 @@ static void rxe_dealloc_ucontext(struct ib_ucontext *ibuc)rxe_err_uc (uc,"cleanup failed, err = %d\n" , err); } +static void rxe_disassociate_ucontext (struct ib_ucontext *ibuc) +{ +} +
[Severity: Critical]
By adding this callback, does this opt the driver into asynchronous teardown which can lead to a use-after-free and kernel panic?
Because RXE uses legacy, untracked VMAs (bypassing the core's rdma_user_mmap_entry mechanism), these VMAs are invisible to the core and are not zapped during teardown.
If a user process maps an RXE resource and the device is then removed or migrated to another network namespace, the asynchronous teardown will free the ib_ucontext and allow module unload while the process still holds the VMA.
When the process later unmaps the memory or exits, the untracked VMA's close callback (rxe_vma_close) will trigger rxe_mmap_release(), accessing the already-freed ib_ucontext and rxe_dev:
drivers/infiniband/sw/rxe/rxe_mmap.c:rxe_mmap_release() {
struct rxe_mmap_info *ip = container_of(ref,
struct rxe_mmap_info, ref);
struct rxe_dev *rxe = to_rdev(ip->context->device);
spin_lock_bh(&rxe->pending_lock);
...
}If the module was unloaded in the interim, jumping to the VMA's close callback could also trigger an immediate kernel panic.
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
[email protected]
AI reviewRe: [PATCH rdma-next 13/13] RDMA/selftests: Add rxe_netns_names test
In reply to Jiri PirkoThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] The test script declares a test plan of 7 tests, but only contains logic to execute 6 tests…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The test script declares a test plan of 7 tests, but only contains logic to execute 6 tests, causing the script to unconditionally fail.
- [Medium] The test TEST_SAME_NETNS_DUP_RENAME attempts to rename a non-existent device, causing the test to pass for the wrong reason and failing to verify duplicate name rejection. --
--- Patch [13]: [PATCH rdma-next 13/13] RDMA/selftests: Add rxe_netns_names test --- commit abd8a5c3d2f106017be1f5b869afa69da9c27f3a
Author: Jiri Pirko <[email protected]>
RDMA/selftests: Add rxe_netns_names test
This commit adds a kselftest script that exercises per-netns RDMA device naming with RXE. It covers duplicate names across namespaces, move conflict handling, move-with-rename, and same-namespace rename requests.
Quoted 5 lines
tools/testing/selftests/rdma/rxe_netns_names.sh new file +0
-0
[ ... ]
tools/testing/selftests/rdma/rxe_netns_names.sh line 181
Quoted 4 lines
tools/testing/selftests/rdma/rxe_netns_names.sh new file +4
-0+setup || fail "failed to create net namespaces" + +ktap_print_header +ktap_set_plan 7
[Severity: Medium]
Does this plan count match the number of tests executed? It looks like the script only runs 6 tests, which would cause the test suite to unconditionally fail during ktap_finished due to a missing test.
Quoted 2 lines
tools/testing/selftests/rdma/rxe_netns_names.sh new file +2
-0+ +if setup_devs &&
[ ... ]
tools/testing/selftests/rdma/rxe_netns_names.sh line 247
Quoted 5 lines
tools/testing/selftests/rdma/rxe_netns_names.sh new file +5
-0+if ! setup_devs; then + ktap_test_fail "$TEST_SAME_NETNS_DUP_RENAME" +elif rdma dev set "$RXE_A" name "$RXE_SAME" && + rdma dev set "$RXE_B" name "$RXE_NEW"; then + if rdma dev set "$RXE_A" name "$RXE_NEW" >/dev/null 2>&1; then
[Severity: Medium]
Will this command fail because of a duplicate name, or because the device is missing?
The device originally named RXE_A was just renamed to RXE_SAME in the previous condition. If we attempt to rename RXE_A again here, the command fails due to the device not being found, which seems to cause the test to pass for the wrong reason instead of properly verifying duplicate name rejection.
Quoted 4 lines
tools/testing/selftests/rdma/rxe_netns_names.sh new file +4
-0+ ktap_test_fail "$TEST_SAME_NETNS_DUP_RENAME" + elif rdma_dev_exists "" "$RXE_SAME" && + rdma_dev_exists "" "$RXE_NEW"; then + ktap_test_pass "$TEST_SAME_NETNS_DUP_RENAME"
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
Re: [PATCH rdma-next 13/13] RDMA/selftests: Add rxe_netns_names test
In reply to Jiri PirkoQuoted 5 lines · Jiri Pirko
From: Jiri Pirko <[email protected]>
Add a kselftest script that exercises per-netns RDMA device naming with RXE. Cover duplicate names across namespaces, move conflict handling, move-with-rename, and same-namespace rename requests.
# timeout set to 45 # selftests: rdma: rxe_netns_names.sh # TAP version 13 # 1..6 # ok 1 same RDMA device name can exist in two net namespaces # ok 2 move without rename fails on destination name conflict # ok 3 move then rename succeeds # ok 4 move with requested destination name succeeds # SKIP < --- This testcase skip
# ok 5 same-netns rename rejects duplicate name # # device returned to init_net as 'ibdev35' # ok 6 netns delete returns device to init_net and renames on conflict # # 1 skipped test(s) detected. Consider enabling relevant config options to improve coverage. # # Totals: pass:5 fail:0 xfail:0 xpass:0 skip:1 error:0 ok 6 selftests: rdma: rxe_netns_names.sh
The above are my test results. But one testcase is skipped.
Zhu Yanjun
Quoted 320 lines
Signed-off-by Jiri Pirko <[email protected]>--- tools/testing/selftests/rdma/Makefile | 3 +- tools/testing/selftests/rdma/config | 2 + .../testing/selftests/rdma/rxe_netns_names.sh | 282 ++++++++++++++++++ 3 files changed, 286 insertions(+), 1 deletion(-)create mode 100755
tools/testing/selftests/rdma/rxe_netns_names.shtools/testing/selftests/rdma/Makefile +2
-1@@ -3,6 +3,7 @@ TEST_PROGS := rxe_rping_between_netns.sh \ rxe_ipv6.sh \ rxe_socket_with_netns.sh \ rxe_test_NETDEV_UNREGISTER.sh \ - rxe_sent_rcvd_bytes.sh + rxe_sent_rcvd_bytes.sh \ + rxe_netns_names.sh include ../lib.mktools/testing/selftests/rdma/config +2
-0@@ -1,3 +1,5 @@ CONFIG_TUN CONFIG_VETH +CONFIG_DUMMY +CONFIG_NET_NS CONFIG_RDMA_RXEtools/testing/selftests/rdma/rxe_netns_names.sh new file +282
-0@@ -0,0 +1,282 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# +# Exercise RDMA device name handling across network namespaces. + +source "$(dirname "$0")/../kselftest/ktap_helpers.sh" + +NAME_PREFIX="rxe_netns_names_$$" +NETDEV_PREFIX="rxn$$" +NS1="${NAME_PREFIX}ns1" +NS2="${NAME_PREFIX}ns2" +RXE_A="${NAME_PREFIX}rxe_a" +RXE_B="${NAME_PREFIX}rxe_b" +RXE_SAME="${NAME_PREFIX}rxe_same" +RXE_NEW="${NAME_PREFIX}rxe_new" +DUMMY_A="${NETDEV_PREFIX}a" +DUMMY_B="${NETDEV_PREFIX}b" +OLD_MODE="" +MODE_CHANGED=0 +MODS=("dummy" "rdma_rxe") +TEST_SAME_NAMES="same RDMA device name can exist in two net namespaces" +TEST_MOVE_CONFLICT="move without rename fails on destination name conflict" +TEST_MOVE_RENAME="move then rename succeeds" +TEST_COMBINED_MOVE_RENAME="move with requested destination name succeeds" +TEST_SAME_NETNS_DUP_RENAME="same-netns rename rejects duplicate name" +TEST_TEARDOWN_RETURN="netns delete returns device to init_net and renames on conflict" + +ksft_skip() +{ + ktap_skip_all "$*" + exit "$KSFT_SKIP" +} + +fail() +{ + ktap_exit_fail_msg "$*" +} + +need_cmd() +{ + command -v "$1" >/dev/null 2>&1 || ksft_skip "missing command: $1" +} + +rdma_ns() +{ + local ns=$1 + + shift + ip netns exec "$ns" rdma "$@" +} + +rdma_dev_exists() +{ + local ns=$1 + local dev=$2 + + if [ -n "$ns" ]; then + rdma_ns "$ns" dev show "$dev" >/dev/null 2>&1 + else + rdma dev show "$dev" >/dev/null 2>&1 + fi +} + +add_dummy() +{ + local netdev=$1 + + ip link add "$netdev" type dummy || return 1 + ip link set "$netdev" up || return 1 +} + +add_rxe() +{ + local dev=$1 + local netdev=$2 + + rdma link add "$dev" type rxe netdev "$netdev" +} + +rdma_dev_on_netdev() +{ + local netdev=$1 + + rdma link show 2>/dev/null | awk -v want="$netdev" ' + { + for (i = 1; i < NF; i++) + if ($i == "netdev" && $(i + 1) == want) { + dev = $2 + sub(/\/.*/, "", dev) + print dev + exit + } + }' +} + +wait_rdma_dev_on_netdev() +{ + local netdev=$1 + local dev + local i + + for i in $(seq 1 50); do + dev=$(rdma_dev_on_netdev "$netdev") + if [ -n "$dev" ]; then + echo "$dev" + return 0 + fi + sleep 0.1 + done + + return 1 +} + +setup_devs() +{ + cleanup_devs + + add_dummy "$DUMMY_A" || return 1 + add_dummy "$DUMMY_B" || return 1 + + add_rxe "$RXE_A" "$DUMMY_A" || return 1 + add_rxe "$RXE_B" "$DUMMY_B" || return 1 +} + +cleanup_devs() +{ + ip link del "$DUMMY_A" 2>/dev/null + ip link del "$DUMMY_B" 2>/dev/null +} + +setup() +{ + OLD_MODE=$(rdma system show 2>/dev/null | + sed -n 's/.*netns \([^ ]*\).*/\1/p') + [ -n "$OLD_MODE" ] || ksft_skip "failed to read RDMA netns mode" + + rdma system set netns exclusive >/dev/null 2>&1 || + ksft_skip "rdma netns exclusive mode is not supported" + MODE_CHANGED=1 + + ip netns add "$NS1" || return 1 + ip netns add "$NS2" || return 1 +} + +cleanup() +{ + cleanup_devs + + ip netns del "$NS1" 2>/dev/null + ip netns del "$NS2" 2>/dev/null + + if [ "$MODE_CHANGED" -eq 1 ]; then + rdma system set netns "$OLD_MODE" 2>/dev/null + fi + + for m in "${MODS[@]}"; do + modprobe -r "$m" 2>/dev/null + done +} + +rdma_supports_combined_move_rename() +{ + rdma dev help 2>&1 | grep -Eq 'netns .*name|name .*netns' +} + +[ "$(id -u)" -eq 0 ] || ksft_skip "must be run as root" +need_cmd ip +need_cmd rdma +need_cmd modprobe + +trap cleanup EXIT + +for m in "${MODS[@]}"; do + modinfo "$m" >/dev/null 2>&1 || ksft_skip "module $m not found" + modprobe "$m" || fail "failed to load $m" +done + +setup || fail "failed to create net namespaces" + +ktap_print_header +ktap_set_plan 7 + +if setup_devs && + rdma dev set "$RXE_A" netns "$NS1" && + rdma_ns "$NS1" dev set "$RXE_A" name "$RXE_SAME" && + rdma dev set "$RXE_B" netns "$NS2" && + rdma_ns "$NS2" dev set "$RXE_B" name "$RXE_SAME" && + rdma_dev_exists "$NS1" "$RXE_SAME" && + rdma_dev_exists "$NS2" "$RXE_SAME"; then + ktap_test_pass "$TEST_SAME_NAMES" +else + ktap_test_fail "$TEST_SAME_NAMES" +fi +cleanup_devs + +if ! setup_devs || + ! rdma dev set "$RXE_A" netns "$NS1" || + ! rdma_ns "$NS1" dev set "$RXE_A" name "$RXE_SAME" || + ! rdma dev set "$RXE_B" netns "$NS2" || + ! rdma_ns "$NS2" dev set "$RXE_B" name "$RXE_SAME"; then + ktap_test_fail "$TEST_MOVE_CONFLICT" +elif rdma_ns "$NS1" dev set "$RXE_SAME" netns "$NS2" >/dev/null 2>&1; then + ktap_test_fail "$TEST_MOVE_CONFLICT" +elif rdma_dev_exists "$NS1" "$RXE_SAME" && + rdma_dev_exists "$NS2" "$RXE_SAME"; then + ktap_test_pass "$TEST_MOVE_CONFLICT" +else + ktap_test_fail "$TEST_MOVE_CONFLICT" +fi +cleanup_devs + +if ! setup_devs; then + ktap_test_fail "$TEST_MOVE_RENAME" +elif rdma dev set "$RXE_A" netns "$NS2" && + rdma_ns "$NS2" dev set "$RXE_A" name "$RXE_NEW"; then + if rdma_dev_exists "$NS2" "$RXE_NEW" && + ! rdma_dev_exists "" "$RXE_A"; then + ktap_test_pass "$TEST_MOVE_RENAME" + else + ktap_test_fail "$TEST_MOVE_RENAME" + fi +else + ktap_test_fail "$TEST_MOVE_RENAME" +fi +cleanup_devs + +if ! rdma_supports_combined_move_rename; then + ktap_test_skip "$TEST_COMBINED_MOVE_RENAME" +elif ! setup_devs; then + ktap_test_fail "$TEST_COMBINED_MOVE_RENAME" +elif rdma dev set "$RXE_A" netns "$NS2" name "$RXE_NEW"; then + if rdma_dev_exists "$NS2" "$RXE_NEW" && + ! rdma_dev_exists "" "$RXE_A"; then + ktap_test_pass "$TEST_COMBINED_MOVE_RENAME" + else + ktap_test_fail "$TEST_COMBINED_MOVE_RENAME" + fi +else + ktap_test_fail "$TEST_COMBINED_MOVE_RENAME" +fi +cleanup_devs + +if ! setup_devs; then + ktap_test_fail "$TEST_SAME_NETNS_DUP_RENAME" +elif rdma dev set "$RXE_A" name "$RXE_SAME" && + rdma dev set "$RXE_B" name "$RXE_NEW"; then + if rdma dev set "$RXE_A" name "$RXE_NEW" >/dev/null 2>&1; then + ktap_test_fail "$TEST_SAME_NETNS_DUP_RENAME" + elif rdma_dev_exists "" "$RXE_SAME" && + rdma_dev_exists "" "$RXE_NEW"; then + ktap_test_pass "$TEST_SAME_NETNS_DUP_RENAME" + else + ktap_test_fail "$TEST_SAME_NETNS_DUP_RENAME" + fi +else + ktap_test_fail "$TEST_SAME_NETNS_DUP_RENAME" +fi +cleanup_devs + +if ! setup_devs; then + ktap_test_fail "$TEST_TEARDOWN_RETURN" +elif ! rdma dev set "$RXE_A" name "$RXE_SAME" || + ! rdma dev set "$RXE_B" netns "$NS2" || + ! rdma_ns "$NS2" dev set "$RXE_B" name "$RXE_SAME" || + ! rdma_dev_exists "$NS2" "$RXE_SAME"; then + ktap_test_fail "$TEST_TEARDOWN_RETURN" +else + ip netns del "$NS2" + returned=$(wait_rdma_dev_on_netdev "$DUMMY_B") + ktap_print_msg "device returned to init_net as '${returned:-<missing>}'" + if rdma_dev_exists "" "$RXE_SAME" && + [ -n "$returned" ] && + [ "$returned" != "$RXE_SAME" ] && + [ "${returned#ibdev}" != "$returned" ]; then + ktap_test_pass "$TEST_TEARDOWN_RETURN" + else + ktap_test_fail "$TEST_TEARDOWN_RETURN" + fi +fi +cleanup_devs + +ktap_finished
Re: [PATCH rdma-next 13/13] RDMA/selftests: Add rxe_netns_names test
In reply to Zhu YanjunQuoted 17 lines · Fri, Jul 10, 2026 at 06:24:25AM +0200, [email protected] wrote:
Quoted 5 lines · Jiri Pirko
From: Jiri Pirko <[email protected]>
Add a kselftest script that exercises per-netns RDMA device naming with RXE. Cover duplicate names across namespaces, move conflict handling, move-with-rename, and same-namespace rename requests.
This is a very comprehensive test suite for the per-netns RDMA device naming infra.
I especially appreciate the inclusion of the '
TEST_TEARDOWN_RETURN' case—ensuringthat the kernel's automatic renaming and fallback mechanics work seamlessly during
Quoted 8 lines
netns deletion is crucial for long-term stability.
One minor thing to clean up before pushing to the tree: the script declares '
ktap_set_plan7'but actually defines 6 distinct test cases in the execution block. I will fix this plan count to 6
Correct. Leftover, will fix.
Quoted 9 lines
to avoid any "bad plan" warnings in automated CI frameworks (like KernelCI).
Aside from that, the cleanup paths and setup tracking are solid.
Thanks for adding this.
Reviewed-by Zhu Yanjun <[email protected]>
Thanks!
Quoted 327 lines
Zhu Yanjun
Quoted 319 lines
Signed-off-by Jiri Pirko <[email protected]>--- tools/testing/selftests/rdma/Makefile | 3 +- tools/testing/selftests/rdma/config | 2 + .../testing/selftests/rdma/rxe_netns_names.sh | 282 ++++++++++++++++++ 3 files changed, 286 insertions(+), 1 deletion(-)create mode 100755
tools/testing/selftests/rdma/rxe_netns_names.shtools/testing/selftests/rdma/Makefile +2
-1@@ -3,6 +3,7 @@ TEST_PROGS := rxe_rping_between_netns.sh \ rxe_ipv6.sh \ rxe_socket_with_netns.sh \ rxe_test_NETDEV_UNREGISTER.sh \ - rxe_sent_rcvd_bytes.sh + rxe_sent_rcvd_bytes.sh \ + rxe_netns_names.sh include ../lib.mktools/testing/selftests/rdma/config +2
-0@@ -1,3 +1,5 @@ CONFIG_TUN CONFIG_VETH +CONFIG_DUMMY +CONFIG_NET_NS CONFIG_RDMA_RXEtools/testing/selftests/rdma/rxe_netns_names.sh new file +282
-0@@ -0,0 +1,282 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# +# Exercise RDMA device name handling across network namespaces. + +source "$(dirname "$0")/../kselftest/ktap_helpers.sh" + +NAME_PREFIX="rxe_netns_names_$$" +NETDEV_PREFIX="rxn$$" +NS1="${NAME_PREFIX}ns1" +NS2="${NAME_PREFIX}ns2" +RXE_A="${NAME_PREFIX}rxe_a" +RXE_B="${NAME_PREFIX}rxe_b" +RXE_SAME="${NAME_PREFIX}rxe_same" +RXE_NEW="${NAME_PREFIX}rxe_new" +DUMMY_A="${NETDEV_PREFIX}a" +DUMMY_B="${NETDEV_PREFIX}b" +OLD_MODE="" +MODE_CHANGED=0 +MODS=("dummy" "rdma_rxe") +TEST_SAME_NAMES="same RDMA device name can exist in two net namespaces" +TEST_MOVE_CONFLICT="move without rename fails on destination name conflict" +TEST_MOVE_RENAME="move then rename succeeds" +TEST_COMBINED_MOVE_RENAME="move with requested destination name succeeds" +TEST_SAME_NETNS_DUP_RENAME="same-netns rename rejects duplicate name" +TEST_TEARDOWN_RETURN="netns delete returns device to init_net and renames on conflict" + +ksft_skip() +{ + ktap_skip_all "$*" + exit "$KSFT_SKIP" +} + +fail() +{ + ktap_exit_fail_msg "$*" +} + +need_cmd() +{ + command -v "$1" >/dev/null 2>&1 || ksft_skip "missing command: $1" +} + +rdma_ns() +{ + local ns=$1 + + shift + ip netns exec "$ns" rdma "$@" +} + +rdma_dev_exists() +{ + local ns=$1 + local dev=$2 + + if [ -n "$ns" ]; then + rdma_ns "$ns" dev show "$dev" >/dev/null 2>&1 + else + rdma dev show "$dev" >/dev/null 2>&1 + fi +} + +add_dummy() +{ + local netdev=$1 + + ip link add "$netdev" type dummy || return 1 + ip link set "$netdev" up || return 1 +} + +add_rxe() +{ + local dev=$1 + local netdev=$2 + + rdma link add "$dev" type rxe netdev "$netdev" +} + +rdma_dev_on_netdev() +{ + local netdev=$1 + + rdma link show 2>/dev/null | awk -v want="$netdev" ' + { + for (i = 1; i < NF; i++) + if ($i == "netdev" && $(i + 1) == want) { + dev = $2 + sub(/\/.*/, "", dev) + print dev + exit + } + }' +} + +wait_rdma_dev_on_netdev() +{ + local netdev=$1 + local dev + local i + + for i in $(seq 1 50); do + dev=$(rdma_dev_on_netdev "$netdev") + if [ -n "$dev" ]; then + echo "$dev" + return 0 + fi + sleep 0.1 + done + + return 1 +} + +setup_devs() +{ + cleanup_devs + + add_dummy "$DUMMY_A" || return 1 + add_dummy "$DUMMY_B" || return 1 + + add_rxe "$RXE_A" "$DUMMY_A" || return 1 + add_rxe "$RXE_B" "$DUMMY_B" || return 1 +} + +cleanup_devs() +{ + ip link del "$DUMMY_A" 2>/dev/null + ip link del "$DUMMY_B" 2>/dev/null +} + +setup() +{ + OLD_MODE=$(rdma system show 2>/dev/null | + sed -n 's/.*netns \([^ ]*\).*/\1/p') + [ -n "$OLD_MODE" ] || ksft_skip "failed to read RDMA netns mode" + + rdma system set netns exclusive >/dev/null 2>&1 || + ksft_skip "rdma netns exclusive mode is not supported" + MODE_CHANGED=1 + + ip netns add "$NS1" || return 1 + ip netns add "$NS2" || return 1 +} + +cleanup() +{ + cleanup_devs + + ip netns del "$NS1" 2>/dev/null + ip netns del "$NS2" 2>/dev/null + + if [ "$MODE_CHANGED" -eq 1 ]; then + rdma system set netns "$OLD_MODE" 2>/dev/null + fi + + for m in "${MODS[@]}"; do + modprobe -r "$m" 2>/dev/null + done +} + +rdma_supports_combined_move_rename() +{ + rdma dev help 2>&1 | grep -Eq 'netns .*name|name .*netns' +} + +[ "$(id -u)" -eq 0 ] || ksft_skip "must be run as root" +need_cmd ip +need_cmd rdma +need_cmd modprobe + +trap cleanup EXIT + +for m in "${MODS[@]}"; do + modinfo "$m" >/dev/null 2>&1 || ksft_skip "module $m not found" + modprobe "$m" || fail "failed to load $m" +done + +setup || fail "failed to create net namespaces" + +ktap_print_header +ktap_set_plan 7 + +if setup_devs && + rdma dev set "$RXE_A" netns "$NS1" && + rdma_ns "$NS1" dev set "$RXE_A" name "$RXE_SAME" && + rdma dev set "$RXE_B" netns "$NS2" && + rdma_ns "$NS2" dev set "$RXE_B" name "$RXE_SAME" && + rdma_dev_exists "$NS1" "$RXE_SAME" && + rdma_dev_exists "$NS2" "$RXE_SAME"; then + ktap_test_pass "$TEST_SAME_NAMES" +else + ktap_test_fail "$TEST_SAME_NAMES" +fi +cleanup_devs + +if ! setup_devs || + ! rdma dev set "$RXE_A" netns "$NS1" || + ! rdma_ns "$NS1" dev set "$RXE_A" name "$RXE_SAME" || + ! rdma dev set "$RXE_B" netns "$NS2" || + ! rdma_ns "$NS2" dev set "$RXE_B" name "$RXE_SAME"; then + ktap_test_fail "$TEST_MOVE_CONFLICT" +elif rdma_ns "$NS1" dev set "$RXE_SAME" netns "$NS2" >/dev/null 2>&1; then + ktap_test_fail "$TEST_MOVE_CONFLICT" +elif rdma_dev_exists "$NS1" "$RXE_SAME" && + rdma_dev_exists "$NS2" "$RXE_SAME"; then + ktap_test_pass "$TEST_MOVE_CONFLICT" +else + ktap_test_fail "$TEST_MOVE_CONFLICT" +fi +cleanup_devs + +if ! setup_devs; then + ktap_test_fail "$TEST_MOVE_RENAME" +elif rdma dev set "$RXE_A" netns "$NS2" && + rdma_ns "$NS2" dev set "$RXE_A" name "$RXE_NEW"; then + if rdma_dev_exists "$NS2" "$RXE_NEW" && + ! rdma_dev_exists "" "$RXE_A"; then + ktap_test_pass "$TEST_MOVE_RENAME" + else + ktap_test_fail "$TEST_MOVE_RENAME" + fi +else + ktap_test_fail "$TEST_MOVE_RENAME" +fi +cleanup_devs + +if ! rdma_supports_combined_move_rename; then + ktap_test_skip "$TEST_COMBINED_MOVE_RENAME" +elif ! setup_devs; then + ktap_test_fail "$TEST_COMBINED_MOVE_RENAME" +elif rdma dev set "$RXE_A" netns "$NS2" name "$RXE_NEW"; then + if rdma_dev_exists "$NS2" "$RXE_NEW" && + ! rdma_dev_exists "" "$RXE_A"; then + ktap_test_pass "$TEST_COMBINED_MOVE_RENAME" + else + ktap_test_fail "$TEST_COMBINED_MOVE_RENAME" + fi +else + ktap_test_fail "$TEST_COMBINED_MOVE_RENAME" +fi +cleanup_devs + +if ! setup_devs; then + ktap_test_fail "$TEST_SAME_NETNS_DUP_RENAME" +elif rdma dev set "$RXE_A" name "$RXE_SAME" && + rdma dev set "$RXE_B" name "$RXE_NEW"; then + if rdma dev set "$RXE_A" name "$RXE_NEW" >/dev/null 2>&1; then + ktap_test_fail "$TEST_SAME_NETNS_DUP_RENAME" + elif rdma_dev_exists "" "$RXE_SAME" && + rdma_dev_exists "" "$RXE_NEW"; then + ktap_test_pass "$TEST_SAME_NETNS_DUP_RENAME" + else + ktap_test_fail "$TEST_SAME_NETNS_DUP_RENAME" + fi +else + ktap_test_fail "$TEST_SAME_NETNS_DUP_RENAME" +fi +cleanup_devs + +if ! setup_devs; then + ktap_test_fail "$TEST_TEARDOWN_RETURN" +elif ! rdma dev set "$RXE_A" name "$RXE_SAME" || + ! rdma dev set "$RXE_B" netns "$NS2" || + ! rdma_ns "$NS2" dev set "$RXE_B" name "$RXE_SAME" || + ! rdma_dev_exists "$NS2" "$RXE_SAME"; then + ktap_test_fail "$TEST_TEARDOWN_RETURN" +else + ip netns del "$NS2" + returned=$(wait_rdma_dev_on_netdev "$DUMMY_B") + ktap_print_msg "device returned to init_net as '${returned:-<missing>}'" + if rdma_dev_exists "" "$RXE_SAME" && + [ -n "$returned" ] && + [ "$returned" != "$RXE_SAME" ] && + [ "${returned#ibdev}" != "$returned" ]; then + ktap_test_pass "$TEST_TEARDOWN_RETURN" + else + ktap_test_fail "$TEST_TEARDOWN_RETURN" + fi +fi +cleanup_devs + +ktap_finishedSignature · 2 lines
Best Regards, Yanjun.Zhu
Re: [PATCH rdma-next 13/13] RDMA/selftests: Add rxe_netns_names test
In reply to yanjun.zhuQuoted 25 lines · Sat, Jul 11, 2026 at 01:51:45AM +0200, [email protected] wrote:
Quoted 5 lines · Jiri Pirko
From: Jiri Pirko <[email protected]>
Add a kselftest script that exercises per-netns RDMA device naming with RXE. Cover duplicate names across namespaces, move conflict handling, move-with-rename, and same-namespace rename requests.
# timeout set to 45 # selftests: rdma: rxe_netns_names.sh # TAP version 13 # 1..6 # ok 1 same RDMA device name can exist in two net namespaces # ok 2 move without rename fails on destination name conflict # ok 3 move then rename succeeds # ok 4 move with requested destination name succeeds # SKIP < --- This testcase skip# ok 5 same-netns rename rejects duplicate name # # device returned to init_net as 'ibdev35' # ok 6 netns delete returns device to init_net and renames on conflict # # 1 skipped test(s) detected. Consider enabling relevant config options to improve coverage. # # Totals: pass:5 fail:0 xfail:0 xpass:0 skip:1 error:0 ok 6 selftests: rdma: rxe_netns_names.sh
Quoted 1 line
The above are my test results. But one testcase is skipped.
Yep, you need patched iproute2:
https://github.com/jpirko/iproute2_mlxsw/commit/cfb61f715fb16fc2b6d007408e048786ef3ed3f6
Plan to send this one after this patchset is merged.
Quoted 323 lines
Zhu Yanjun
Quoted 319 lines
Signed-off-by Jiri Pirko <[email protected]>--- tools/testing/selftests/rdma/Makefile | 3 +- tools/testing/selftests/rdma/config | 2 + .../testing/selftests/rdma/rxe_netns_names.sh | 282 ++++++++++++++++++ 3 files changed, 286 insertions(+), 1 deletion(-)create mode 100755
tools/testing/selftests/rdma/rxe_netns_names.shtools/testing/selftests/rdma/Makefile +2
-1@@ -3,6 +3,7 @@ TEST_PROGS := rxe_rping_between_netns.sh \ rxe_ipv6.sh \ rxe_socket_with_netns.sh \ rxe_test_NETDEV_UNREGISTER.sh \ - rxe_sent_rcvd_bytes.sh + rxe_sent_rcvd_bytes.sh \ + rxe_netns_names.sh include ../lib.mktools/testing/selftests/rdma/config +2
-0@@ -1,3 +1,5 @@ CONFIG_TUN CONFIG_VETH +CONFIG_DUMMY +CONFIG_NET_NS CONFIG_RDMA_RXEtools/testing/selftests/rdma/rxe_netns_names.sh new file +282
-0@@ -0,0 +1,282 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# +# Exercise RDMA device name handling across network namespaces. + +source "$(dirname "$0")/../kselftest/ktap_helpers.sh" + +NAME_PREFIX="rxe_netns_names_$$" +NETDEV_PREFIX="rxn$$" +NS1="${NAME_PREFIX}ns1" +NS2="${NAME_PREFIX}ns2" +RXE_A="${NAME_PREFIX}rxe_a" +RXE_B="${NAME_PREFIX}rxe_b" +RXE_SAME="${NAME_PREFIX}rxe_same" +RXE_NEW="${NAME_PREFIX}rxe_new" +DUMMY_A="${NETDEV_PREFIX}a" +DUMMY_B="${NETDEV_PREFIX}b" +OLD_MODE="" +MODE_CHANGED=0 +MODS=("dummy" "rdma_rxe") +TEST_SAME_NAMES="same RDMA device name can exist in two net namespaces" +TEST_MOVE_CONFLICT="move without rename fails on destination name conflict" +TEST_MOVE_RENAME="move then rename succeeds" +TEST_COMBINED_MOVE_RENAME="move with requested destination name succeeds" +TEST_SAME_NETNS_DUP_RENAME="same-netns rename rejects duplicate name" +TEST_TEARDOWN_RETURN="netns delete returns device to init_net and renames on conflict" + +ksft_skip() +{ + ktap_skip_all "$*" + exit "$KSFT_SKIP" +} + +fail() +{ + ktap_exit_fail_msg "$*" +} + +need_cmd() +{ + command -v "$1" >/dev/null 2>&1 || ksft_skip "missing command: $1" +} + +rdma_ns() +{ + local ns=$1 + + shift + ip netns exec "$ns" rdma "$@" +} + +rdma_dev_exists() +{ + local ns=$1 + local dev=$2 + + if [ -n "$ns" ]; then + rdma_ns "$ns" dev show "$dev" >/dev/null 2>&1 + else + rdma dev show "$dev" >/dev/null 2>&1 + fi +} + +add_dummy() +{ + local netdev=$1 + + ip link add "$netdev" type dummy || return 1 + ip link set "$netdev" up || return 1 +} + +add_rxe() +{ + local dev=$1 + local netdev=$2 + + rdma link add "$dev" type rxe netdev "$netdev" +} + +rdma_dev_on_netdev() +{ + local netdev=$1 + + rdma link show 2>/dev/null | awk -v want="$netdev" ' + { + for (i = 1; i < NF; i++) + if ($i == "netdev" && $(i + 1) == want) { + dev = $2 + sub(/\/.*/, "", dev) + print dev + exit + } + }' +} + +wait_rdma_dev_on_netdev() +{ + local netdev=$1 + local dev + local i + + for i in $(seq 1 50); do + dev=$(rdma_dev_on_netdev "$netdev") + if [ -n "$dev" ]; then + echo "$dev" + return 0 + fi + sleep 0.1 + done + + return 1 +} + +setup_devs() +{ + cleanup_devs + + add_dummy "$DUMMY_A" || return 1 + add_dummy "$DUMMY_B" || return 1 + + add_rxe "$RXE_A" "$DUMMY_A" || return 1 + add_rxe "$RXE_B" "$DUMMY_B" || return 1 +} + +cleanup_devs() +{ + ip link del "$DUMMY_A" 2>/dev/null + ip link del "$DUMMY_B" 2>/dev/null +} + +setup() +{ + OLD_MODE=$(rdma system show 2>/dev/null | + sed -n 's/.*netns \([^ ]*\).*/\1/p') + [ -n "$OLD_MODE" ] || ksft_skip "failed to read RDMA netns mode" + + rdma system set netns exclusive >/dev/null 2>&1 || + ksft_skip "rdma netns exclusive mode is not supported" + MODE_CHANGED=1 + + ip netns add "$NS1" || return 1 + ip netns add "$NS2" || return 1 +} + +cleanup() +{ + cleanup_devs + + ip netns del "$NS1" 2>/dev/null + ip netns del "$NS2" 2>/dev/null + + if [ "$MODE_CHANGED" -eq 1 ]; then + rdma system set netns "$OLD_MODE" 2>/dev/null + fi + + for m in "${MODS[@]}"; do + modprobe -r "$m" 2>/dev/null + done +} + +rdma_supports_combined_move_rename() +{ + rdma dev help 2>&1 | grep -Eq 'netns .*name|name .*netns' +} + +[ "$(id -u)" -eq 0 ] || ksft_skip "must be run as root" +need_cmd ip +need_cmd rdma +need_cmd modprobe + +trap cleanup EXIT + +for m in "${MODS[@]}"; do + modinfo "$m" >/dev/null 2>&1 || ksft_skip "module $m not found" + modprobe "$m" || fail "failed to load $m" +done + +setup || fail "failed to create net namespaces" + +ktap_print_header +ktap_set_plan 7 + +if setup_devs && + rdma dev set "$RXE_A" netns "$NS1" && + rdma_ns "$NS1" dev set "$RXE_A" name "$RXE_SAME" && + rdma dev set "$RXE_B" netns "$NS2" && + rdma_ns "$NS2" dev set "$RXE_B" name "$RXE_SAME" && + rdma_dev_exists "$NS1" "$RXE_SAME" && + rdma_dev_exists "$NS2" "$RXE_SAME"; then + ktap_test_pass "$TEST_SAME_NAMES" +else + ktap_test_fail "$TEST_SAME_NAMES" +fi +cleanup_devs + +if ! setup_devs || + ! rdma dev set "$RXE_A" netns "$NS1" || + ! rdma_ns "$NS1" dev set "$RXE_A" name "$RXE_SAME" || + ! rdma dev set "$RXE_B" netns "$NS2" || + ! rdma_ns "$NS2" dev set "$RXE_B" name "$RXE_SAME"; then + ktap_test_fail "$TEST_MOVE_CONFLICT" +elif rdma_ns "$NS1" dev set "$RXE_SAME" netns "$NS2" >/dev/null 2>&1; then + ktap_test_fail "$TEST_MOVE_CONFLICT" +elif rdma_dev_exists "$NS1" "$RXE_SAME" && + rdma_dev_exists "$NS2" "$RXE_SAME"; then + ktap_test_pass "$TEST_MOVE_CONFLICT" +else + ktap_test_fail "$TEST_MOVE_CONFLICT" +fi +cleanup_devs + +if ! setup_devs; then + ktap_test_fail "$TEST_MOVE_RENAME" +elif rdma dev set "$RXE_A" netns "$NS2" && + rdma_ns "$NS2" dev set "$RXE_A" name "$RXE_NEW"; then + if rdma_dev_exists "$NS2" "$RXE_NEW" && + ! rdma_dev_exists "" "$RXE_A"; then + ktap_test_pass "$TEST_MOVE_RENAME" + else + ktap_test_fail "$TEST_MOVE_RENAME" + fi +else + ktap_test_fail "$TEST_MOVE_RENAME" +fi +cleanup_devs + +if ! rdma_supports_combined_move_rename; then + ktap_test_skip "$TEST_COMBINED_MOVE_RENAME" +elif ! setup_devs; then + ktap_test_fail "$TEST_COMBINED_MOVE_RENAME" +elif rdma dev set "$RXE_A" netns "$NS2" name "$RXE_NEW"; then + if rdma_dev_exists "$NS2" "$RXE_NEW" && + ! rdma_dev_exists "" "$RXE_A"; then + ktap_test_pass "$TEST_COMBINED_MOVE_RENAME" + else + ktap_test_fail "$TEST_COMBINED_MOVE_RENAME" + fi +else + ktap_test_fail "$TEST_COMBINED_MOVE_RENAME" +fi +cleanup_devs + +if ! setup_devs; then + ktap_test_fail "$TEST_SAME_NETNS_DUP_RENAME" +elif rdma dev set "$RXE_A" name "$RXE_SAME" && + rdma dev set "$RXE_B" name "$RXE_NEW"; then + if rdma dev set "$RXE_A" name "$RXE_NEW" >/dev/null 2>&1; then + ktap_test_fail "$TEST_SAME_NETNS_DUP_RENAME" + elif rdma_dev_exists "" "$RXE_SAME" && + rdma_dev_exists "" "$RXE_NEW"; then + ktap_test_pass "$TEST_SAME_NETNS_DUP_RENAME" + else + ktap_test_fail "$TEST_SAME_NETNS_DUP_RENAME" + fi +else + ktap_test_fail "$TEST_SAME_NETNS_DUP_RENAME" +fi +cleanup_devs + +if ! setup_devs; then + ktap_test_fail "$TEST_TEARDOWN_RETURN" +elif ! rdma dev set "$RXE_A" name "$RXE_SAME" || + ! rdma dev set "$RXE_B" netns "$NS2" || + ! rdma_ns "$NS2" dev set "$RXE_B" name "$RXE_SAME" || + ! rdma_dev_exists "$NS2" "$RXE_SAME"; then + ktap_test_fail "$TEST_TEARDOWN_RETURN" +else + ip netns del "$NS2" + returned=$(wait_rdma_dev_on_netdev "$DUMMY_B") + ktap_print_msg "device returned to init_net as '${returned:-<missing>}'" + if rdma_dev_exists "" "$RXE_SAME" && + [ -n "$returned" ] && + [ "$returned" != "$RXE_SAME" ] && + [ "${returned#ibdev}" != "$returned" ]; then + ktap_test_pass "$TEST_TEARDOWN_RETURN" + else + ktap_test_fail "$TEST_TEARDOWN_RETURN" + fi +fi +cleanup_devs + +ktap_finished
Re: [PATCH rdma-next 08/13] RDMA/cgroup: Scope rdma cgroup device visibility to the net namespace
In reply to Michal KoutnýQuoted 37 lines · Thu, Jul 09, 2026 at 03:04:23PM +0200, [email protected] wrote:
Hi.
Documentation/admin-guide/cgroup-v2.rst RDMA · line 2760
Quoted 13 lines · Jiri Pirko
index
993446ab66d0..4523c1884d67100644Documentation/admin-guide/cgroup-v2.rst +6
-0@@ -2752,6 +2752,13 @@ RDMA The "rdma" controller regulates the distribution and accounting of RDMA resources. +When RDMA devices are isolated per network namespace (exclusive mode), +device names are unique only within a network namespace. The device lines +below are therefore scoped to the reading or writing process's network +namespace: only devices accessible from that namespace are listed, and a +limit is applied to the device of that name in that namespace. Configure +limits from the same network namespace as the workloads.OK.
Quoted 18 lines
include/linux/cgroup_rdma.h +5
-0@@ -7,6 +7,7 @@#define _CGROUP_RDMA_H#include <linux/cgroup.h> +#include <net/net_namespace.h> enum rdmacg_resource_type {RDMACG_RESOURCE_HCA_HANDLE ,@@ -34,6 +35,15 @@ struct rdmacg_device {struct list_head dev_node;struct list_head rpools;char *name; +/* +* Net namespace the device belongs to. @netns_shared mirrors +* ib_devices_shared_netns: when true the device is visible from every +* net namespace (shared mode); otherwise @net is the only namespace
Quoted 8 lines
include/linux/cgroup_rdma.h struct rdmacg_device { · line 46
Quoted 5 lines
include/linux/cgroup_rdma.h +5
-0+* that may see and configure it. @netns_shared is updated when the +* sharing mode changes, so use {READ,WRITE}_ONCE() to access it. +*/ +possible_net_t net; +bool netns_shared;Any reason to store the
netns_sharedsplit per device? (IIUC, it's a global parameter.)
No reason, changed.
Thanks!
Quoted 3 lines
Thanks,
Michal
Re: [PATCH rdma-next 03/13] RDMA/core: Support renaming a device when changing its net namespace
In reply to [email protected]Quoted 34 lines · Fri, Jul 10, 2026 at 11:56:26AM +0200, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The destination device name requested by userspace is ignored during a net namespace move because NULL is hardcoded in the netlink handler. ----- Patch [3]: [PATCH rdma-next 03/13] RDMA/core: Support renaming a device when changing its net namespace --- commit
481a893ec5cd10a7f8be8c8de249d86cc283c470
Author: Jiri Pirko <[email protected]>RDMA/core: Support renaming a device when changing its net namespace
This patch updates the RDMA core to allow requesting a destination device name when moving a device to a different network namespace. It modifies
ib_device_set_netns_putto take a requested name and implements the naming rules during namespace teardown.drivers/infiniband/core/nldev.c static int nldev_set_doit(struct sk_buff *skb, struct nlmsghdr *nlh, · line 1197
Quoted 10 lines
drivers/infiniband/core/nldev.c +1
-1@@ -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 );[Severity: Medium]
Does this hardcode NULL instead of extracting and passing the user-provided device name?The commit message mentions allowing namespace moves to request a destination device name, but it appears
ib_device_set_netns_putis always called with NULL here.
Quoted 5 lines
How does this interact with the rename logic in
nldev_set_doit? If a user supplies both theRDMA_NLDEV_ATTR_DEV_NAMEandRDMA_NLDEV_NET_NS_FDattributes in the netlink command, will the code enter the rename logic first and return, completely skipping the net namespace change instead of moving and renaming the device?
False positive. "RDMA/nldev: Allow setting the device name while changing net namespace" is doing this.
Quoted 7 lines
Quoted 4 lines
drivers/infiniband/core/nldev.c +0
-0if (err == -EEXIST )NL_SET_ERR_MSG (extack,"Device name already exists in the target net namespace" );Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH rdma-next 02/13] RDMA/core: Handle device name conflicts when changing net namespace
In reply to [email protected]Quoted 50 lines · Fri, Jul 10, 2026 at 11:56:27AM +0200, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] User-triggerableWARN()during concurrent RDMA device unregistration and netns exit, leading to DoS onpanic_on_warnsystems. ----- Patch [2]: [PATCH rdma-next 02/13] RDMA/core: Handle device name conflicts when changing net namespace --- commit
05c3f87e2143d43f2f1e27d0f13e1b1f97f35bab
Author: Jiri Pirko <[email protected]>RDMA/core: Handle device name conflicts when changing net namespace
This commit prepares namespace moves for per-netns names by checking user-initiated moves for destination-name conflicts before disabling the device. It also ensures teardown moves detach from the exiting namespace even if fallback naming fails.
Quoted 8 lines
drivers/infiniband/core/device.c +1
-0@@ -1160,8 +1168,17 @@ static void rdma_dev_exit_net(struct net *net)/* * If the real device is in the NS then move it back to init. +* Provide a fallback pattern so a name conflict in init_net drivers/infiniband/core/device.c static void rdma_dev_exit_net(struct net *net) · line 1180
Quoted 10 lines
drivers/infiniband/core/device.c +8
-1+* cannot make the teardown move fail. */ -rdma_dev_change_netns (dev, net, &init_net); +if (net_eq (net,read_pnet (&dev->coredev.rdma_net))) { + ret =rdma_dev_change_netns (dev, net, &init_net, +"ibdev%d" ); +if (ret) +WARN (1 , +"Failed to move RDMA device %s to init_net on netns exit: %d\n" , +dev_name (&dev->dev), ret);[Severity: High]
Can this WARN(1) be triggered by a concurrent device unregistration?Looking at
rdma_dev_change_netns(), if a device is concurrently unregistered, the refcount drops to 0 and the function legitimately returns-ENODEV:drivers/infiniband/core/device.c:rdma_dev_change_netns() { ... if (refcount_read(&device->refcount) == 0 || !net_eq(cur_net, read_pnet(&device->coredev.rdma_net))) { ret = -ENODEV; goto out; } ... }
Quoted 7 lines
If
rdma_dev_exit_net()runs concurrently with device unregistration (e.g., module unload, PCI hotplug, or link deletion of a software RDMA device like RXE),rdma_dev_change_netns()will return-ENODEVand trigger this warning.Because an unprivileged user can acquire
CAP_NET_ADMINin a user namespace, create an RXE device, and race netns deletion with device deletion, could this be used to intentionally crash a host configured with panic_on_warn=1?
Fixed.
Quoted 8 lines
Quoted 5 lines
drivers/infiniband/core/device.c +1
-0+ }put_device (&dev->dev);down_read (&devices_rwsem);Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH rdma-next 12/13] RDMA/rxe: Implement disassociate_ucontext callback
In reply to [email protected]Quoted 49 lines · Fri, Jul 10, 2026 at 11:56:30AM +0200, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] Implementing an emptydisassociate_ucontextcallback bypasses the core's VMA zapping because RXE uses legacy, untracked VMAs. This leads to a Use-After-Free ofib_ucontextandrxe_devwhen the VMA is eventually closed, and potential kernel panics upon module unload. ----- Patch [12]: [PATCH rdma-next 12/13] RDMA/rxe: Implement
disassociate_ucontextcallback --- commit8946fc9f2a378bad6f910a408699c6a70e97a6f6
Author: Jiri Pirko <[email protected]>RDMA/rxe: Implement
disassociate_ucontextcallbackImplement an empty
disassociate_ucontext()callback so the RDMA core can move rxe devices between net namespaces. The core requires this callback to reset user contexts without waiting for userspace.drivers/infiniband/sw/rxe/rxe_verbs.c static void rxe_dealloc_ucontext(struct ib_ucontext *ibuc) · line 246
Quoted 12 lines
drivers/infiniband/sw/rxe/rxe_verbs.c +4
-0@@ -240,6 +240,10 @@ static void rxe_dealloc_ucontext(struct ib_ucontext *ibuc)rxe_err_uc (uc,"cleanup failed, err = %d\n" , err); } +static void rxe_disassociate_ucontext (struct ib_ucontext *ibuc) +{ +} +[Severity: Critical]
By adding this callback, does this opt the driver into asynchronous teardown which can lead to a use-after-free and kernel panic?Because RXE uses legacy, untracked VMAs (bypassing the core's
rdma_user_mmap_entrymechanism), these VMAs are invisible to the core and are not zapped during teardown.If a user process maps an RXE resource and the device is then removed or migrated to another network namespace, the asynchronous teardown will free the
ib_ucontextand allow module unload while the process still holds the VMA.When the process later unmaps the memory or exits, the untracked VMA's close callback (
rxe_vma_close) will triggerrxe_mmap_release(), accessing the already-freedib_ucontextandrxe_dev:drivers/infiniband/sw/rxe/rxe_mmap.c:rxe_mmap_release() { struct rxe_mmap_info *ip = container_of(ref, struct rxe_mmap_info, ref); struct rxe_dev *rxe = to_rdev(ip->context->device); spin_lock_bh(&rxe->pending_lock);
Quoted 5 lines
...
}If the module was unloaded in the interim, jumping to the VMA's close callback could also trigger an immediate kernel panic.
Fixed.
Quoted 5 lines
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
Re: [PATCH rdma-next 13/13] RDMA/selftests: Add rxe_netns_names test
In reply to [email protected]Quoted 23 lines · Fri, Jul 10, 2026 at 11:56:31AM +0200, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The test script declares a test plan of 7 tests, but only contains logic to execute 6 tests, causing the script to unconditionally fail.
- [Medium] The testTEST_SAME_NETNS_DUP_RENAMEattempts to rename a non-existent device, causing the test to pass for the wrong reason and failing to verify duplicate name rejection. ----- Patch [13]: [PATCH rdma-next 13/13] RDMA/selftests: Add
rxe_netns_namestest --- commitabd8a5c3d2f106017be1f5b869afa69da9c27f3a
Author: Jiri Pirko <[email protected]>RDMA/selftests: Add
rxe_netns_namestestThis commit adds a kselftest script that exercises per-netns RDMA device naming with RXE. It covers duplicate names across namespaces, move conflict handling, move-with-rename, and same-namespace rename requests.
Quoted 5 lines
tools/testing/selftests/rdma/rxe_netns_names.sh new file +0
-0[ ... ]
Quoted 9 lines
tools/testing/selftests/rdma/rxe_netns_names.sh line 181
Quoted 4 lines
tools/testing/selftests/rdma/rxe_netns_names.sh new file +4
-0+setup || fail "failed to create net namespaces" + +ktap_print_header +ktap_set_plan 7[Severity: Medium]
Does this plan count match the number of tests executed? It looks like the script only runs 6 tests, which would cause the test suite to unconditionally fail duringktap_finisheddue to a missing test.
Fixed.
Quoted 16 lines
Quoted 3 lines
tools/testing/selftests/rdma/rxe_netns_names.sh new file +2
-0+ +if setup_devs &&[ ... ]
tools/testing/selftests/rdma/rxe_netns_names.sh line 247
Quoted 5 lines
tools/testing/selftests/rdma/rxe_netns_names.sh new file +5
-0+if ! setup_devs; then + ktap_test_fail "$TEST_SAME_NETNS_DUP_RENAME" +elif rdma dev set "$RXE_A" name "$RXE_SAME" && + rdma dev set "$RXE_B" name "$RXE_NEW"; then + if rdma dev set "$RXE_A" name "$RXE_NEW" >/dev/null 2>&1; then[Severity: Medium]
Will this command fail because of a duplicate name, or because the device is missing?
Quoted 4 lines
The device originally named
RXE_Awas just renamed toRXE_SAMEin the previous condition. If we attempt to renameRXE_Aagain here, the command fails due to the device not being found, which seems to cause the test to pass for the wrong reason instead of properly verifying duplicate name rejection.
Fixed.
Quoted 8 lines
Quoted 5 lines
tools/testing/selftests/rdma/rxe_netns_names.sh new file +4
-0+ ktap_test_fail "$TEST_SAME_NETNS_DUP_RENAME" + elif rdma_dev_exists "" "$RXE_SAME" && + rdma_dev_exists "" "$RXE_NEW"; then + ktap_test_pass "$TEST_SAME_NETNS_DUP_RENAME"Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
Re: [PATCH rdma-next 08/13] RDMA/cgroup: Scope rdma cgroup device visibility to the net namespace
In reply to Jiri PirkoQuoted 47 lines · Jiri Pirko
Quoted 37 lines · Thu, Jul 09, 2026 at 03:04:23PM +0200, [email protected] wrote:
Hi.
Documentation/admin-guide/cgroup-v2.rst RDMA · line 2760
Quoted 13 lines · Jiri Pirko
index
993446ab66d0..4523c1884d67100644Documentation/admin-guide/cgroup-v2.rst +6
-0@@ -2752,6 +2752,13 @@ RDMA The "rdma" controller regulates the distribution and accounting of RDMA resources. +When RDMA devices are isolated per network namespace (exclusive mode), +device names are unique only within a network namespace. The device lines +below are therefore scoped to the reading or writing process's network +namespace: only devices accessible from that namespace are listed, and a +limit is applied to the device of that name in that namespace. Configure +limits from the same network namespace as the workloads.OK.
Quoted 18 lines
include/linux/cgroup_rdma.h +5
-0@@ -7,6 +7,7 @@#define _CGROUP_RDMA_H#include <linux/cgroup.h> +#include <net/net_namespace.h> enum rdmacg_resource_type {RDMACG_RESOURCE_HCA_HANDLE ,@@ -34,6 +35,15 @@ struct rdmacg_device {struct list_head dev_node;struct list_head rpools;char *name; +/* +* Net namespace the device belongs to. @netns_shared mirrors +* ib_devices_shared_netns: when true the device is visible from every +* net namespace (shared mode); otherwise @net is the only namespace Quoted 8 lines
include/linux/cgroup_rdma.h struct rdmacg_device { · line 46
Quoted 5 lines
include/linux/cgroup_rdma.h +5
-0+* that may see and configure it. @netns_shared is updated when the +* sharing mode changes, so use {READ,WRITE}_ONCE() to access it. +*/ +possible_net_t net; +bool netns_shared;Any reason to store the
netns_sharedsplit per device? (IIUC, it's a global parameter.)
Quoted 2 lines
No reason, changed.
Hi Jiri,
A question on the v2 you mentioned to Michal.
Once netns_shared stops being cached per rdmacg_device, rdmacg_device_visible() in kernel/cgroup/rdma.c still needs the current sharing mode, whose authoritative value lives in the IB core (ib_devices_shared_netns). How do you plan to expose it there without the generic cgroup controller reaching back into drivers/infiniband/? Exporting the global, or keeping an IB-side update hook, both feel a bit awkward; it would be good to see which direction you took.
On the mechanism itself: it's the right call that rdmacg_try_charge() stays out of the scoping. Charging takes the rdmacg_device pointer directly (no name lookup), and a task can only charge a device it already holds a handle to, so applying visibility there would be wrong. The scoping deliberately touches only the name-based lookup (the write path) and the enumeration (read/show) paths -- worth keeping that invariant in mind so a later patch doesn't grow the filter.
Thanks,
Tao> Thanks!
Quoted 7 lines
Quoted 4 lines
Thanks,
Michal
Re: [PATCH rdma-next 08/13] RDMA/cgroup: Scope rdma cgroup device visibility to the net namespace
Quoted 56 lines · Tue, Jul 14, 2026 at 04:28:01AM +0200, [email protected] wrote:
Quoted 47 lines · Jiri Pirko
Quoted 37 lines · Thu, Jul 09, 2026 at 03:04:23PM +0200, [email protected] wrote:
Hi.
Documentation/admin-guide/cgroup-v2.rst RDMA · line 2760
Quoted 13 lines · Jiri Pirko
index
993446ab66d0..4523c1884d67100644Documentation/admin-guide/cgroup-v2.rst +6
-0@@ -2752,6 +2752,13 @@ RDMA The "rdma" controller regulates the distribution and accounting of RDMA resources. +When RDMA devices are isolated per network namespace (exclusive mode), +device names are unique only within a network namespace. The device lines +below are therefore scoped to the reading or writing process's network +namespace: only devices accessible from that namespace are listed, and a +limit is applied to the device of that name in that namespace. Configure +limits from the same network namespace as the workloads.OK.
Quoted 18 lines
include/linux/cgroup_rdma.h +5
-0@@ -7,6 +7,7 @@#define _CGROUP_RDMA_H#include <linux/cgroup.h> +#include <net/net_namespace.h> enum rdmacg_resource_type {RDMACG_RESOURCE_HCA_HANDLE ,@@ -34,6 +35,15 @@ struct rdmacg_device {struct list_head dev_node;struct list_head rpools;char *name; +/* +* Net namespace the device belongs to. @netns_shared mirrors +* ib_devices_shared_netns: when true the device is visible from every +* net namespace (shared mode); otherwise @net is the only namespace Quoted 8 lines
include/linux/cgroup_rdma.h struct rdmacg_device { · line 46
Quoted 5 lines
include/linux/cgroup_rdma.h +5
-0+* that may see and configure it. @netns_shared is updated when the +* sharing mode changes, so use {READ,WRITE}_ONCE() to access it. +*/ +possible_net_t net; +bool netns_shared;Any reason to store the
netns_sharedsplit per device? (IIUC, it's a global parameter.)Quoted 2 lines
No reason, changed.
Hi Jiri,
A question on the v2 you mentioned to Michal.
Quoted 7 lines
Once
netns_sharedstops being cached perrdmacg_device,rdmacg_device_visible()inkernel/cgroup/rdma.cstill needs the current sharing mode, whose authoritative value lives in the IB core (ib_devices_shared_netns). How do you plan to expose it there without the generic cgroup controller reaching back intodrivers/infiniband/? Exporting the global, or keeping an IB-side update hook, both feel a bit awkward; it would be good to see which direction you took.
Exposing this from ib side is not doable, as IB may be compiled as a module. So what I do is I intrododuce "static bool rdmacg_netns_shared" in cgroup/rdma.c and exported function rdmacg_set_netns_shared() to set it from IB.
Quoted 19 lines
On the mechanism itself: it's the right call that
rdmacg_try_charge()stays out of the scoping. Charging takes therdmacg_devicepointer directly (no name lookup), and a task can only charge a device it already holds a handle to, so applying visibility there would be wrong. The scoping deliberately touches only the name-based lookup (the write path) and the enumeration (read/show) paths -- worth keeping that invariant in mind so a later patch doesn't grow the filter.Thanks,
Tao> Thanks!Quoted 7 lines
Quoted 4 lines
Thanks,
Michal
Re: [PATCH rdma-next 08/13] RDMA/cgroup: Scope rdma cgroup device visibility to the net namespace
In reply to [email protected]Quoted 56 lines · Fri, Jul 10, 2026 at 11:56:29AM +0200, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Scoping RDMA cgroup device visibility to the reader's network namespace breaks host-level cgroup administration and monitoring.
- [Medium] Concurrent lockless read ofdevice->netusingread_pnet()withoutREAD_ONCE()leads to a data race. ----- Patch [8]: [PATCH rdma-next 08/13] RDMA/cgroup: Scope rdma cgroup device visibility to the net namespace --- commit
18e4bddcfb08bd42bef710d6d1b72824d15fa2c9
Author: Jiri Pirko <[email protected]>RDMA/cgroup: Scope rdma cgroup device visibility to the net namespace
Track each rdma cgroup device's net namespace and sharing mode, then filter name lookups and cgroupfs enumeration to devices visible from the caller's namespace. Keep the cached sharing mode synchronized across registration, netns moves, and runtime mode changes.
Quoted 4 lines
kernel/cgroup/rdma.c +0
-0[ ... ]
kernel/cgroup/rdma.c void rdmacg_unregister_device(struct rdmacg_device *device) · line 472
Quoted 9 lines
kernel/cgroup/rdma.c +5
-0@@ -464,6 +465,13 @@ void rdmacg_unregister_device(struct rdmacg_device *device) }EXPORT_SYMBOL (rdmacg_unregister_device); +/* netns_shared is toggled without rdmacg_mutex, hence READ_ONCE(). */ +static bool rdmacg_device_visible (const struct rdmacg_device *device) +{ +return READ_ONCE (device->netns_shared) || +net_eq (read_pnet (&device->net), current->nsproxy->net_ns);[Severity: Medium]
Does this lockless read ofdevice->netusingread_pnet()cause a data race?When another thread concurrently moves the RDMA device to a new network namespace, it updates
device->net:drivers/infiniband/core/cgroup.c:ib_device_rdmacg_change_netns() { write_pnet(&device->cg_device.net, net); }While
write_pnet()usesrcu_assign_pointer()to update the pointer concurrently,read_pnet()here expands to a plain pointer access withoutREAD_ONCE(). Sincerdmacg_mutexis not held during the write, could this plain read race with the assignment and cause undefined behavior?[Severity: High]
Does tying RDMA cgroup device visibility to the reader's network namespace break host-level cgroup administration and monitoring?If a device is moved to a non-init network namespace (exclusive mode), host administrators and orchestration tools (like systemd or Kubernetes running in
init_net) will completely lose visibility and control over its RDMA cgroup resources. Attempting to write to rdma.max will result in-ENODEV, and reading rdma.current will show an empty list for these devices.
Quoted 4 lines
Since cgroup state must reflect the cgroup's configuration independent of the reading process's network namespace, does tying the UAPI to
current->nsproxy->net_nsviolate VFS semantics where an open file descriptor's behavior would dynamically change depending on which process callswrite()?
This report made me think a bit more about the current approach. A cgroup may contain processes using devices from multiple network namespaces, so caller-relative filtering can hide part of its state from host administrators and make the same open file behave differently depending on the calling task. That does not look and feel correct.
Instead, to handle device names that are duplicated across network namespaces, v2 will use the system-wide RDMA device index for disambiguation. The existing name-only format will remain unchanged for unique names. For duplicate names, reads will include an index=N selector and writes will require it; ambiguous name-only writes will fail with -ENOTUNIQ.