Blob Blame History Raw
From: Parav Pandit <parav@mellanox.com>
Date: Wed, 5 Sep 2018 09:48:00 +0300
Subject: RDMA/uverbs: Use device.groups to initialize device attributes
Patch-mainline: v4.20-rc1
Git-commit: b53b1c08a23eb1091982daacb2122f90a7094a77
References: bsc#1103992 FATE#326009

Instead of explicitly adding device attribute files and handling such
error conditions, depend on device core layer to create device attributes
files based group pointer NULL terminated array.

Signed-off-by: Parav Pandit <parav@mellanox.com>
Reviewed-by: Daniel Jurgens <danielj@mellanox.com>
Signed-off-by: Leon Romanovsky <leonro@mellanox.com>
Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>
Acked-by: Thomas Bogendoerfer <tbogendoerfer@suse.de>
---
 drivers/infiniband/core/uverbs.h      |    2 ++
 drivers/infiniband/core/uverbs_main.c |   30 +++++++++++++++++-------------
 2 files changed, 19 insertions(+), 13 deletions(-)

--- a/drivers/infiniband/core/uverbs.h
+++ b/drivers/infiniband/core/uverbs.h
@@ -101,6 +101,8 @@ struct ib_uverbs_device {
 	int					num_comp_vectors;
 	struct completion			comp;
 	struct device				dev;
+	/* First group for device attributes, NULL terminated array */
+	const struct attribute_group		*groups[2];
 	struct ib_device	__rcu	       *ib_dev;
 	int					devnum;
 	struct cdev			        cdev;
--- a/drivers/infiniband/core/uverbs_main.c
+++ b/drivers/infiniband/core/uverbs_main.c
@@ -947,7 +947,7 @@ static struct ib_client uverbs_client =
 	.remove = ib_uverbs_remove_one
 };
 
-static ssize_t show_ibdev(struct device *device, struct device_attribute *attr,
+static ssize_t ibdev_show(struct device *device, struct device_attribute *attr,
 			  char *buf)
 {
 	struct ib_uverbs_device *dev =
@@ -964,10 +964,10 @@ static ssize_t show_ibdev(struct device
 
 	return ret;
 }
-static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
+static DEVICE_ATTR_RO(ibdev);
 
-static ssize_t show_dev_abi_version(struct device *device,
-				    struct device_attribute *attr, char *buf)
+static ssize_t abi_version_show(struct device *device,
+				struct device_attribute *attr, char *buf)
 {
 	struct ib_uverbs_device *dev =
 			container_of(device, struct ib_uverbs_device, dev);
@@ -983,7 +983,17 @@ static ssize_t show_dev_abi_version(stru
 
 	return ret;
 }
-static DEVICE_ATTR(abi_version, S_IRUGO, show_dev_abi_version, NULL);
+static DEVICE_ATTR_RO(abi_version);
+
+static struct attribute *ib_dev_attrs[] = {
+	&dev_attr_abi_version.attr,
+	&dev_attr_ibdev.attr,
+	NULL,
+};
+
+static const struct attribute_group dev_attr_group = {
+	.attrs = ib_dev_attrs,
+};
 
 static CLASS_ATTR_STRING(abi_version, S_IRUGO,
 			 __stringify(IB_USER_VERBS_ABI_VERSION));
@@ -1050,6 +1060,8 @@ static void ib_uverbs_add_one(struct ib_
 	uverbs_dev->dev.parent = device->dev.parent;
 	uverbs_dev->dev.devt = base;
 	uverbs_dev->dev.release = ib_uverbs_release_dev;
+	uverbs_dev->groups[0] = &dev_attr_group;
+	uverbs_dev->dev.groups = uverbs_dev->groups;
 	dev_set_name(&uverbs_dev->dev, "uverbs%d", uverbs_dev->devnum);
 
 	cdev_init(&uverbs_dev->cdev,
@@ -1060,17 +1072,9 @@ static void ib_uverbs_add_one(struct ib_
 	if (ret)
 		goto err_cdev;
 
-	if (device_create_file(&uverbs_dev->dev, &dev_attr_ibdev))
-		goto err_file;
-	if (device_create_file(&uverbs_dev->dev, &dev_attr_abi_version))
-		goto err_file;
-
 	ib_set_client_data(device, &uverbs_client, uverbs_dev);
-
 	return;
 
-err_file:
-	cdev_device_del(&uverbs_dev->cdev, &uverbs_dev->dev);
 err_cdev:
 	cdev_del(&uverbs_dev->cdev);
 	put_device(&uverbs_dev->dev);