Skip to contentSkip to conversation

net: RDMA: explicitly enumerate ib_device attribute groups

PATCH In review Roman Gushchin 3 participants 5 messages Feb 26, 2025 InfiniBand

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 net: RDMA: explicitly enumerate ib_device attribute groups

Explicitly enumerate ib_device's attribute groups.

Cc Jason Gunthorpe <[email protected]>
Cc Leon Romanovsky <[email protected]>
Cc Maher Sanalla <[email protected]>
Cc Parav Pandit <[email protected]>
---
 drivers/infiniband/core/device.c |  4 ++--
 include/rdma/ib_verbs.h          | 14 ++++++++------
 2 files changed, 10 insertions(+), 8 deletions(-)
drivers/infiniband/core/device.c +2 -2
index 0ded91f056f3..8dea307addf1 100644
@@ -1404,8 +1404,8 @@ int ib_register_device(struct ib_device *device, const char *name,
 		return ret;
 	}
 
-	device->groups[0] = &ib_dev_attr_group;
-	device->groups[1] = device->ops.device_group;
+	device->groups[IB_ATTR_GROUP_DEV_ATTR] = &ib_dev_attr_group;
+	device->groups[IB_ATTR_GROUP_DRIVER_ATTR] = device->ops.device_group;
 	ret = ib_setup_device_attrs(device);
 	if (ret)
 		goto cache_cleanup;
include/rdma/ib_verbs.h +8 -6
index b59bf30de430..9c4c4238e6fc 100644
@@ -2728,6 +2728,13 @@ struct ib_core_device {
 	struct ib_device *owner; /* reach back to owner ib_device */
 };
 
+enum ib_device_attr_group {
+	IB_ATTR_GROUP_DEV_ATTR = 0,	/* Device attributes */
+	IB_ATTR_GROUP_DRIVER_ATTR = 1,	/* Driver-provided attributes */
+	IB_ATTR_GROUP_HW_STATS = 2,	/* hw_stats */
+	IB_ATTR_GROUP_LAST,		/* NULL pointer terminating array */
+};
+
 struct rdma_restrack_root;
 struct ib_device {
 	/* Do not access @dma_device directly from ULP nor from HW drivers. */
@@ -2761,12 +2768,7 @@ struct ib_device {
 		struct ib_core_device	coredev;
 	};
 
-	/* First group is for device attributes,
-	 * Second group is for driver provided attributes (optional).
-	 * Third group is for the hw_stats
-	 * It is a NULL terminated array.
-	 */
-	const struct attribute_group	*groups[4];
+	const struct attribute_group	*groups[IB_ATTR_GROUP_LAST + 1];
 
 	u64			     uverbs_cmd_mask;
 
Signature · 1 line
2.48.1.658.g4767266eb4-goog
#1 lore raw

PATCH 2/2 net: RDMA: don't expose hw_stats into non-init net namespaces

Commit 467f432a521a ("RDMA/core: Split port and device counter sysfs attributes") accidentally exposed hw_counters to non-init net namespaces.

Fix this by hiding the IB_ATTR_GROUP_HW_STATS group when initializing a non-init rdma device.

Fixes 467f432a521a ("RDMA/core: Split port and device counter sysfs attributes")
Cc Jason Gunthorpe <[email protected]>
Cc Leon Romanovsky <[email protected]>
Cc Maher Sanalla <[email protected]>
Cc Parav Pandit <[email protected]>
---
 drivers/infiniband/core/device.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/infiniband/core/device.c +6 -0
index 8dea307addf1..bf4a016ccb9d 100644
@@ -528,6 +528,8 @@ static struct class ib_class = {
 static void rdma_init_coredev(struct ib_core_device *coredev,
 			      struct ib_device *dev, struct net *net)
 {
+	bool is_full_dev = net_eq(net, &init_net);
+
 	/* This BUILD_BUG_ON is intended to catch layout change
 	 * of union of ib_core_device and device.
 	 * dev must be the first element as ib_core and providers
@@ -539,6 +541,10 @@ static void rdma_init_coredev(struct ib_core_device *coredev,
 
 	coredev->dev.class = &ib_class;
 	coredev->dev.groups = dev->groups;
+
+	if (!is_full_dev)
+		coredev->dev.groups[IB_ATTR_GROUP_HW_STATS] = NULL;
+
 	device_initialize(&coredev->dev);
 	coredev->owner = dev;
 	INIT_LIST_HEAD(&coredev->port_list);
Signature · 1 line
2.48.1.658.g4767266eb4-goog
#2 lore raw

RE: [PATCH 2/2] net: RDMA: don't expose hw_stats into non-init net namespaces

Quoted 28 lines

From: Roman Gushchin <[email protected]>
Sent: Wednesday, February 26, 2025 9:05 AM

Commit 467f432a521a ("RDMA/core: Split port and device counter sysfs attributes") accidentally exposed hw_counters to non-init net namespaces.

Fix this by hiding the IB_ATTR_GROUP_HW_STATS group when initializing a non-init rdma device.

Fixes 467f432a521a ("RDMA/core: Split port and device counter sysfs

attributes")

Cc Jason Gunthorpe <[email protected]>
Cc Leon Romanovsky <[email protected]>
Cc Maher Sanalla <[email protected]>
Cc Parav Pandit <[email protected]>
---
 drivers/infiniband/core/device.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/infiniband/core/device.c +0 -0
index 8dea307addf1..bf4a016ccb9d 100644

drivers/infiniband/core/device.c static struct class ib_class = { static void · line 531

Quoted 4 lines
drivers/infiniband/core/device.c +2 -0
@@ -528,6 +528,8 @@ static struct class ib_class = {  static void
 rdma_init_coredev(struct ib_core_device *coredev,
 			      struct ib_device *dev, struct net *net)  {
+	bool is_full_dev = net_eq(net, &init_net);
+

Instead of it, below is more elegant check because
a. its limited to do comparison on core dev area and other generic structure.
b. reuses the infra used in sysfs.c to detect coredev.
c. in upcoming future, I plan to expand device creation in non init ns too, where it still will be coredev. And this conficts with that idea, hinting that comparing in below manner is still more elegant.

bool is_full_dev = &device->coredev == coredev;

drivers/infiniband/core/device.c static void rdma_init_coredev(struct ib_core_device *coredev, · line 547

Quoted 11 lines
drivers/infiniband/core/device.c +5 -0
 	/* This BUILD_BUG_ON is intended to catch layout change
 	 * of union of ib_core_device and device.
 	 * dev must be the first element as ib_core and providers @@ -539,6
+541,10 @@ static void rdma_init_coredev(struct ib_core_device *coredev,
 
 	coredev->dev.class = &ib_class;
 	coredev->dev.groups = dev->groups;
+
+	if (!is_full_dev)
+		coredev->dev.groups[IB_ATTR_GROUP_HW_STATS] = NULL;
+

Static hard coding to HW_STATS enum is not enough because when IB_ATTR_GROUP_DRIVER_ATTR group is not used, ib_setup_device_attrs() will initialize HW_STATS at index = DRIVER_ATTR.
So you need to store the index of hw_stats group in the ib_device struct, so you can make it null for non_core_dev.

And if that can be done without introducing the new enums, applying the fix on previous stable kernels will be easy. Otherwise, this patch needs to do to -rc and other previous patch to -next and its difficult.

Quoted 5 lines
drivers/infiniband/core/device.c +0 -1
 	device_initialize(&coredev->dev);
 	coredev->owner = dev;
 	INIT_LIST_HEAD(&coredev->port_list);
--

2.48.1.658.g4767266eb4-goog

#3 lore raw
In reply to Roman Gushchin
Quoted 24 lines

From: Roman Gushchin <[email protected]>
Sent: Wednesday, February 26, 2025 9:05 AM

Explicitly enumerate ib_device's attribute groups.

Cc Jason Gunthorpe <[email protected]>
Cc Leon Romanovsky <[email protected]>
Cc Maher Sanalla <[email protected]>
Cc Parav Pandit <[email protected]>
---
 drivers/infiniband/core/device.c |  4 ++--
 include/rdma/ib_verbs.h          | 14 ++++++++------
 2 files changed, 10 insertions(+), 8 deletions(-)
drivers/infiniband/core/device.c +0 -0
index 0ded91f056f3..8dea307addf1 100644

drivers/infiniband/core/device.c int ib_register_device(struct ib_device *device, · line 1411

Quoted 10 lines
drivers/infiniband/core/device.c +2 -2
@@ -1404,8 +1404,8 @@ int ib_register_device(struct ib_device *device,
 const char *name,
 		return ret;
 	}
 
-	device->groups[0] = &ib_dev_attr_group;
-	device->groups[1] = device->ops.device_group;
+	device->groups[IB_ATTR_GROUP_DEV_ATTR] = &ib_dev_attr_group;
+	device->groups[IB_ATTR_GROUP_DRIVER_ATTR] = device-
 >ops.device_group;
 	ret = ib_setup_device_attrs(device);

This function may initialize the hw_stats at the index IB_ATTR_GROUP_DRIVER_ATTR when it is optional. It runs through a for loop to find the first free entry.
This needs to be captured in the comment above, and it will have effect on your next fix patch.

Quoted 21 lines
drivers/infiniband/core/device.c +0 -0
 	if (ret)
 		goto cache_cleanup;
include/rdma/ib_verbs.h index renamed from include/rdma/ib_verbs.h +0 -0

b59bf30de430..9c4c4238e6fc 100644

include/rdma/ib_verbs.h +7 -0
@@ -2728,6 +2728,13 @@ struct ib_core_device {
 	struct ib_device *owner; /* reach back to owner ib_device */  };
 
+enum ib_device_attr_group {
+	IB_ATTR_GROUP_DEV_ATTR = 0,	/* Device attributes */
+	IB_ATTR_GROUP_DRIVER_ATTR = 1,	/* Driver-provided attributes
 */
+	IB_ATTR_GROUP_HW_STATS = 2,	/* hw_stats */
+	IB_ATTR_GROUP_LAST,		/* NULL pointer terminating array */
+};
+
 struct rdma_restrack_root;
 struct ib_device {
 	/* Do not access @dma_device directly from ULP nor from HW

drivers. */ @@ -2761,12 +2768,7 @@ struct ib_device {

include/rdma/ib_verbs.h struct ib_device { · line 2765

Quoted 5 lines
include/rdma/ib_verbs.h +0 -2
 		struct ib_core_device	coredev;
 	};
 
-	/* First group is for device attributes,
-	 * Second group is for driver provided attributes (optional).

This text 'optional' is missing in above enums, please add it.
Also please add the optional text comment for hw_stats too, as its optional too.

Quoted 10 lines
include/rdma/ib_verbs.h +1 -5
-	 * Third group is for the hw_stats
-	 * It is a NULL terminated array.
-	 */
-	const struct attribute_group	*groups[4];
+	const struct attribute_group	*groups[IB_ATTR_GROUP_LAST + 1];
 
 	u64			     uverbs_cmd_mask;
 
--

2.48.1.658.g4767266eb4-goog

#4 lore raw
In reply to Roman Gushchin
Quoted 9 lines · Roman Gushchin

Explicitly enumerate ib_device's attribute groups.

Cc Jason Gunthorpe <[email protected]>
Cc Leon Romanovsky <[email protected]>
Cc Maher Sanalla <[email protected]>
Cc Parav Pandit <[email protected]>
Quoted 4 lines
---
 drivers/infiniband/core/device.c |  4 ++--
 include/rdma/ib_verbs.h          | 14 ++++++++------
 2 files changed, 10 insertions(+), 8 deletions(-)

Very minor comment as you will need to address Parav's comments anyway. The title should be "RDMA/core: Explicitly ..." and not as it is written.

Thanks