2023-06-20 18:49:39

by Greg Kroah-Hartman

[permalink] [raw]
Subject: [PATCH] accel: make accel_class a static const structure

From: Ivan Orlov <[email protected]>

Now that the driver core allows for struct class to be in read-only
memory, move the accel_class structure to be declared at build time
placing it into read-only memory, instead of having to be dynamically
allocated at boot time.

Cc: Oded Gabbay <[email protected]>
Cc: [email protected]
Suggested-by: Greg Kroah-Hartman <[email protected]>
Signed-off-by: Ivan Orlov <[email protected]>
Signed-off-by: Greg Kroah-Hartman <[email protected]>
---
drivers/accel/drm_accel.c | 21 ++++++++-------------
1 file changed, 8 insertions(+), 13 deletions(-)

diff --git a/drivers/accel/drm_accel.c b/drivers/accel/drm_accel.c
index 4a9baf02439e..2dc187e1ee41 100644
--- a/drivers/accel/drm_accel.c
+++ b/drivers/accel/drm_accel.c
@@ -21,7 +21,6 @@ static DEFINE_SPINLOCK(accel_minor_lock);
static struct idr accel_minors_idr;

static struct dentry *accel_debugfs_root;
-static struct class *accel_class;

static struct device_type accel_sysfs_device_minor = {
.name = "accel_minor"
@@ -32,23 +31,19 @@ static char *accel_devnode(const struct device *dev, umode_t *mode)
return kasprintf(GFP_KERNEL, "accel/%s", dev_name(dev));
}

+static const struct class accel_class = {
+ .name = "accel",
+ .devnode = accel_devnode,
+};
+
static int accel_sysfs_init(void)
{
- accel_class = class_create("accel");
- if (IS_ERR(accel_class))
- return PTR_ERR(accel_class);
-
- accel_class->devnode = accel_devnode;
-
- return 0;
+ return class_register(&accel_class);
}

static void accel_sysfs_destroy(void)
{
- if (IS_ERR_OR_NULL(accel_class))
- return;
- class_destroy(accel_class);
- accel_class = NULL;
+ class_unregister(&accel_class);
}

static int accel_name_info(struct seq_file *m, void *data)
@@ -116,7 +111,7 @@ void accel_debugfs_init(struct drm_minor *minor, int minor_id)
void accel_set_device_instance_params(struct device *kdev, int index)
{
kdev->devt = MKDEV(ACCEL_MAJOR, index);
- kdev->class = accel_class;
+ kdev->class = &accel_class;
kdev->type = &accel_sysfs_device_minor;
}

--
2.41.0



2023-07-03 12:16:56

by Tomer Tayar

[permalink] [raw]
Subject: Re: [PATCH] accel: make accel_class a static const structure

On 20/06/2023 21:25, Greg Kroah-Hartman wrote:
> From: Ivan Orlov <[email protected]>
>
> Now that the driver core allows for struct class to be in read-only
> memory, move the accel_class structure to be declared at build time
> placing it into read-only memory, instead of having to be dynamically
> allocated at boot time.
>
> Cc: Oded Gabbay <[email protected]>
> Cc: [email protected]
> Suggested-by: Greg Kroah-Hartman <[email protected]>
> Signed-off-by: Ivan Orlov <[email protected]>
> Signed-off-by: Greg Kroah-Hartman <[email protected]>

Reviewed-by: Tomer Tayar <[email protected]>

Thanks,
Tomer

> ---
> drivers/accel/drm_accel.c | 21 ++++++++-------------
> 1 file changed, 8 insertions(+), 13 deletions(-)
>
> diff --git a/drivers/accel/drm_accel.c b/drivers/accel/drm_accel.c
> index 4a9baf02439e..2dc187e1ee41 100644
> --- a/drivers/accel/drm_accel.c
> +++ b/drivers/accel/drm_accel.c
> @@ -21,7 +21,6 @@ static DEFINE_SPINLOCK(accel_minor_lock);
> static struct idr accel_minors_idr;
>
> static struct dentry *accel_debugfs_root;
> -static struct class *accel_class;
>
> static struct device_type accel_sysfs_device_minor = {
> .name = "accel_minor"
> @@ -32,23 +31,19 @@ static char *accel_devnode(const struct device *dev, umode_t *mode)
> return kasprintf(GFP_KERNEL, "accel/%s", dev_name(dev));
> }
>
> +static const struct class accel_class = {
> + .name = "accel",
> + .devnode = accel_devnode,
> +};
> +
> static int accel_sysfs_init(void)
> {
> - accel_class = class_create("accel");
> - if (IS_ERR(accel_class))
> - return PTR_ERR(accel_class);
> -
> - accel_class->devnode = accel_devnode;
> -
> - return 0;
> + return class_register(&accel_class);
> }
>
> static void accel_sysfs_destroy(void)
> {
> - if (IS_ERR_OR_NULL(accel_class))
> - return;
> - class_destroy(accel_class);
> - accel_class = NULL;
> + class_unregister(&accel_class);
> }
>
> static int accel_name_info(struct seq_file *m, void *data)
> @@ -116,7 +111,7 @@ void accel_debugfs_init(struct drm_minor *minor, int minor_id)
> void accel_set_device_instance_params(struct device *kdev, int index)
> {
> kdev->devt = MKDEV(ACCEL_MAJOR, index);
> - kdev->class = accel_class;
> + kdev->class = &accel_class;
> kdev->type = &accel_sysfs_device_minor;
> }
>


2023-07-03 12:41:20

by Oded Gabbay

[permalink] [raw]
Subject: Re: [PATCH] accel: make accel_class a static const structure

On Mon, Jul 3, 2023 at 3:09 PM Tomer Tayar <[email protected]> wrote:
>
> On 20/06/2023 21:25, Greg Kroah-Hartman wrote:
> > From: Ivan Orlov <[email protected]>
> >
> > Now that the driver core allows for struct class to be in read-only
> > memory, move the accel_class structure to be declared at build time
> > placing it into read-only memory, instead of having to be dynamically
> > allocated at boot time.
> >
> > Cc: Oded Gabbay <[email protected]>
> > Cc: [email protected]
> > Suggested-by: Greg Kroah-Hartman <[email protected]>
> > Signed-off-by: Ivan Orlov <[email protected]>
> > Signed-off-by: Greg Kroah-Hartman <[email protected]>
>
> Reviewed-by: Tomer Tayar <[email protected]>
Thanks Tomer.
Applied to habanalabs-next for 6.6.
Oded
>
> Thanks,
> Tomer
>
> > ---
> > drivers/accel/drm_accel.c | 21 ++++++++-------------
> > 1 file changed, 8 insertions(+), 13 deletions(-)
> >
> > diff --git a/drivers/accel/drm_accel.c b/drivers/accel/drm_accel.c
> > index 4a9baf02439e..2dc187e1ee41 100644
> > --- a/drivers/accel/drm_accel.c
> > +++ b/drivers/accel/drm_accel.c
> > @@ -21,7 +21,6 @@ static DEFINE_SPINLOCK(accel_minor_lock);
> > static struct idr accel_minors_idr;
> >
> > static struct dentry *accel_debugfs_root;
> > -static struct class *accel_class;
> >
> > static struct device_type accel_sysfs_device_minor = {
> > .name = "accel_minor"
> > @@ -32,23 +31,19 @@ static char *accel_devnode(const struct device *dev, umode_t *mode)
> > return kasprintf(GFP_KERNEL, "accel/%s", dev_name(dev));
> > }
> >
> > +static const struct class accel_class = {
> > + .name = "accel",
> > + .devnode = accel_devnode,
> > +};
> > +
> > static int accel_sysfs_init(void)
> > {
> > - accel_class = class_create("accel");
> > - if (IS_ERR(accel_class))
> > - return PTR_ERR(accel_class);
> > -
> > - accel_class->devnode = accel_devnode;
> > -
> > - return 0;
> > + return class_register(&accel_class);
> > }
> >
> > static void accel_sysfs_destroy(void)
> > {
> > - if (IS_ERR_OR_NULL(accel_class))
> > - return;
> > - class_destroy(accel_class);
> > - accel_class = NULL;
> > + class_unregister(&accel_class);
> > }
> >
> > static int accel_name_info(struct seq_file *m, void *data)
> > @@ -116,7 +111,7 @@ void accel_debugfs_init(struct drm_minor *minor, int minor_id)
> > void accel_set_device_instance_params(struct device *kdev, int index)
> > {
> > kdev->devt = MKDEV(ACCEL_MAJOR, index);
> > - kdev->class = accel_class;
> > + kdev->class = &accel_class;
> > kdev->type = &accel_sysfs_device_minor;
> > }
> >
>
>