2020-09-01 10:34:40

by Sudeep Holla

[permalink] [raw]
Subject: [PATCH 1/2] tee: replace cdev_add + device_add with cdev_device_add

Commit 233ed09d7fda ("chardev: add helper function to register char devs
with a struct device") added a helper function 'cdev_device_add'.

Make use of cdev_device_add in tee_device_register to replace cdev_add
and device_add. Since cdev_device_add takes care of setting the
kobj->parent, drop explicit initialisation in tee_device_alloc.

Signed-off-by: Sudeep Holla <[email protected]>
---
drivers/tee/tee_core.c | 21 ++++-----------------
1 file changed, 4 insertions(+), 17 deletions(-)

diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c
index 64637e09a095..b4a8b362d78f 100644
--- a/drivers/tee/tee_core.c
+++ b/drivers/tee/tee_core.c
@@ -917,7 +917,6 @@ struct tee_device *tee_device_alloc(const struct tee_desc *teedesc,

cdev_init(&teedev->cdev, &tee_fops);
teedev->cdev.owner = teedesc->owner;
- teedev->cdev.kobj.parent = &teedev->dev.kobj;

dev_set_drvdata(&teedev->dev, driver_data);
device_initialize(&teedev->dev);
@@ -985,24 +984,15 @@ int tee_device_register(struct tee_device *teedev)
return -EINVAL;
}

- rc = cdev_add(&teedev->cdev, teedev->dev.devt, 1);
+ rc = cdev_device_add(&teedev->cdev, &teedev->dev);
if (rc) {
dev_err(&teedev->dev,
- "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
+ "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
teedev->name, MAJOR(teedev->dev.devt),
MINOR(teedev->dev.devt), rc);
return rc;
}

- rc = device_add(&teedev->dev);
- if (rc) {
- dev_err(&teedev->dev,
- "unable to device_add() %s, major %d, minor %d, err=%d\n",
- teedev->name, MAJOR(teedev->dev.devt),
- MINOR(teedev->dev.devt), rc);
- goto err_device_add;
- }
-
rc = sysfs_create_group(&teedev->dev.kobj, &tee_dev_group);
if (rc) {
dev_err(&teedev->dev,
@@ -1014,9 +1004,7 @@ int tee_device_register(struct tee_device *teedev)
return 0;

err_sysfs_create_group:
- device_del(&teedev->dev);
-err_device_add:
- cdev_del(&teedev->cdev);
+ cdev_device_del(&teedev->cdev, &teedev->dev);
return rc;
}
EXPORT_SYMBOL_GPL(tee_device_register);
@@ -1062,8 +1050,7 @@ void tee_device_unregister(struct tee_device *teedev)

if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED) {
sysfs_remove_group(&teedev->dev.kobj, &tee_dev_group);
- cdev_del(&teedev->cdev);
- device_del(&teedev->dev);
+ cdev_device_del(&teedev->cdev, &teedev->dev);
}

tee_device_put(teedev);
--
2.17.1


2020-09-01 10:35:25

by Sudeep Holla

[permalink] [raw]
Subject: [PATCH 2/2] tee: avoid explicit sysfs_create/delete_group by initialising dev->groups

If the dev->groups is initialised, the sysfs group is created as part
of device_add call. There is no need to call sysfs_create/delete_group
explicitly.

Signed-off-by: Sudeep Holla <[email protected]>
---
drivers/tee/tee_core.c | 21 ++++-----------------
1 file changed, 4 insertions(+), 17 deletions(-)

diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c
index b4a8b362d78f..f77d64fafead 100644
--- a/drivers/tee/tee_core.c
+++ b/drivers/tee/tee_core.c
@@ -962,9 +962,7 @@ static struct attribute *tee_dev_attrs[] = {
NULL
};

-static const struct attribute_group tee_dev_group = {
- .attrs = tee_dev_attrs,
-};
+ATTRIBUTE_GROUPS(tee_dev);

/**
* tee_device_register() - Registers a TEE device
@@ -984,6 +982,8 @@ int tee_device_register(struct tee_device *teedev)
return -EINVAL;
}

+ teedev->dev.groups = tee_dev_groups;
+
rc = cdev_device_add(&teedev->cdev, &teedev->dev);
if (rc) {
dev_err(&teedev->dev,
@@ -993,19 +993,8 @@ int tee_device_register(struct tee_device *teedev)
return rc;
}

- rc = sysfs_create_group(&teedev->dev.kobj, &tee_dev_group);
- if (rc) {
- dev_err(&teedev->dev,
- "failed to create sysfs attributes, err=%d\n", rc);
- goto err_sysfs_create_group;
- }
-
teedev->flags |= TEE_DEVICE_FLAG_REGISTERED;
return 0;
-
-err_sysfs_create_group:
- cdev_device_del(&teedev->cdev, &teedev->dev);
- return rc;
}
EXPORT_SYMBOL_GPL(tee_device_register);

@@ -1048,10 +1037,8 @@ void tee_device_unregister(struct tee_device *teedev)
if (!teedev)
return;

- if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED) {
- sysfs_remove_group(&teedev->dev.kobj, &tee_dev_group);
+ if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED)
cdev_device_del(&teedev->cdev, &teedev->dev);
- }

tee_device_put(teedev);
wait_for_completion(&teedev->c_no_users);
--
2.17.1

2020-09-18 08:44:00

by Jens Wiklander

[permalink] [raw]
Subject: Re: [PATCH 1/2] tee: replace cdev_add + device_add with cdev_device_add

On Tue, Sep 01, 2020 at 11:33:34AM +0100, Sudeep Holla wrote:
> Commit 233ed09d7fda ("chardev: add helper function to register char devs
> with a struct device") added a helper function 'cdev_device_add'.
>
> Make use of cdev_device_add in tee_device_register to replace cdev_add
> and device_add. Since cdev_device_add takes care of setting the
> kobj->parent, drop explicit initialisation in tee_device_alloc.
>
> Signed-off-by: Sudeep Holla <[email protected]>
> ---
> drivers/tee/tee_core.c | 21 ++++-----------------
> 1 file changed, 4 insertions(+), 17 deletions(-)

Thanks for the nice cleanups. I'm picking up this patch and
"[PATCH 2/2] tee: avoid explicit sysfs_create/delete_group by
initialising dev->groups".

Cheers,
Jens

>
> diff --git a/drivers/tee/tee_core.c b/drivers/tee/tee_core.c
> index 64637e09a095..b4a8b362d78f 100644
> --- a/drivers/tee/tee_core.c
> +++ b/drivers/tee/tee_core.c
> @@ -917,7 +917,6 @@ struct tee_device *tee_device_alloc(const struct tee_desc *teedesc,
>
> cdev_init(&teedev->cdev, &tee_fops);
> teedev->cdev.owner = teedesc->owner;
> - teedev->cdev.kobj.parent = &teedev->dev.kobj;
>
> dev_set_drvdata(&teedev->dev, driver_data);
> device_initialize(&teedev->dev);
> @@ -985,24 +984,15 @@ int tee_device_register(struct tee_device *teedev)
> return -EINVAL;
> }
>
> - rc = cdev_add(&teedev->cdev, teedev->dev.devt, 1);
> + rc = cdev_device_add(&teedev->cdev, &teedev->dev);
> if (rc) {
> dev_err(&teedev->dev,
> - "unable to cdev_add() %s, major %d, minor %d, err=%d\n",
> + "unable to cdev_device_add() %s, major %d, minor %d, err=%d\n",
> teedev->name, MAJOR(teedev->dev.devt),
> MINOR(teedev->dev.devt), rc);
> return rc;
> }
>
> - rc = device_add(&teedev->dev);
> - if (rc) {
> - dev_err(&teedev->dev,
> - "unable to device_add() %s, major %d, minor %d, err=%d\n",
> - teedev->name, MAJOR(teedev->dev.devt),
> - MINOR(teedev->dev.devt), rc);
> - goto err_device_add;
> - }
> -
> rc = sysfs_create_group(&teedev->dev.kobj, &tee_dev_group);
> if (rc) {
> dev_err(&teedev->dev,
> @@ -1014,9 +1004,7 @@ int tee_device_register(struct tee_device *teedev)
> return 0;
>
> err_sysfs_create_group:
> - device_del(&teedev->dev);
> -err_device_add:
> - cdev_del(&teedev->cdev);
> + cdev_device_del(&teedev->cdev, &teedev->dev);
> return rc;
> }
> EXPORT_SYMBOL_GPL(tee_device_register);
> @@ -1062,8 +1050,7 @@ void tee_device_unregister(struct tee_device *teedev)
>
> if (teedev->flags & TEE_DEVICE_FLAG_REGISTERED) {
> sysfs_remove_group(&teedev->dev.kobj, &tee_dev_group);
> - cdev_del(&teedev->cdev);
> - device_del(&teedev->dev);
> + cdev_device_del(&teedev->cdev, &teedev->dev);
> }
>
> tee_device_put(teedev);
> --
> 2.17.1
>