2023-10-24 11:49:24

by Greg Kroah-Hartman

[permalink] [raw]
Subject: [PATCH] ocxl: make ocxl_class constant

Now that the driver core allows for struct class to be in read-only
memory, we should make all 'class' structures declared at build time
placing them into read-only memory, instead of having to be dynamically
allocated at runtime.

Cc: Frederic Barrat <[email protected]>
Cc: Andrew Donnellan <[email protected]>
Cc: Arnd Bergmann <[email protected]>
Cc: [email protected]
Signed-off-by: Greg Kroah-Hartman <[email protected]>
---
drivers/misc/ocxl/file.c | 27 +++++++++++++++------------
1 file changed, 15 insertions(+), 12 deletions(-)

diff --git a/drivers/misc/ocxl/file.c b/drivers/misc/ocxl/file.c
index 6e63f060e4cc..ac69b7f361f5 100644
--- a/drivers/misc/ocxl/file.c
+++ b/drivers/misc/ocxl/file.c
@@ -14,7 +14,6 @@
#define OCXL_NUM_MINORS 256 /* Total to reserve */

static dev_t ocxl_dev;
-static struct class *ocxl_class;
static DEFINE_MUTEX(minors_idr_lock);
static struct idr minors_idr;

@@ -509,6 +508,16 @@ static void ocxl_file_make_invisible(struct ocxl_file_info *info)
cdev_del(&info->cdev);
}

+static char *ocxl_devnode(const struct device *dev, umode_t *mode)
+{
+ return kasprintf(GFP_KERNEL, "ocxl/%s", dev_name(dev));
+}
+
+static const struct class ocxl_class = {
+ .name = "ocxl",
+ .devnode = ocxl_devnode,
+};
+
int ocxl_file_register_afu(struct ocxl_afu *afu)
{
int minor;
@@ -529,7 +538,7 @@ int ocxl_file_register_afu(struct ocxl_afu *afu)

info->dev.parent = &fn->dev;
info->dev.devt = MKDEV(MAJOR(ocxl_dev), minor);
- info->dev.class = ocxl_class;
+ info->dev.class = &ocxl_class;
info->dev.release = info_release;

info->afu = afu;
@@ -584,11 +593,6 @@ void ocxl_file_unregister_afu(struct ocxl_afu *afu)
device_unregister(&info->dev);
}

-static char *ocxl_devnode(const struct device *dev, umode_t *mode)
-{
- return kasprintf(GFP_KERNEL, "ocxl/%s", dev_name(dev));
-}
-
int ocxl_file_init(void)
{
int rc;
@@ -601,20 +605,19 @@ int ocxl_file_init(void)
return rc;
}

- ocxl_class = class_create("ocxl");
- if (IS_ERR(ocxl_class)) {
+ rc = class_register(&ocxl_class);
+ if (rc) {
pr_err("Unable to create ocxl class\n");
unregister_chrdev_region(ocxl_dev, OCXL_NUM_MINORS);
- return PTR_ERR(ocxl_class);
+ return rc;
}

- ocxl_class->devnode = ocxl_devnode;
return 0;
}

void ocxl_file_exit(void)
{
- class_destroy(ocxl_class);
+ class_unregister(&ocxl_class);
unregister_chrdev_region(ocxl_dev, OCXL_NUM_MINORS);
idr_destroy(&minors_idr);
}
--
2.42.0


2023-10-25 08:20:20

by Frederic Barrat

[permalink] [raw]
Subject: Re: [PATCH] ocxl: make ocxl_class constant



On 24/10/2023 13:49, Greg Kroah-Hartman wrote:
> Now that the driver core allows for struct class to be in read-only
> memory, we should make all 'class' structures declared at build time
> placing them into read-only memory, instead of having to be dynamically
> allocated at runtime.
>
> Cc: Frederic Barrat <[email protected]>
> Cc: Andrew Donnellan <[email protected]>
> Cc: Arnd Bergmann <[email protected]>
> Cc: [email protected]
> Signed-off-by: Greg Kroah-Hartman <[email protected]>
> ---

Thanks!

Acked-by: Frederic Barrat <[email protected]>

Fred


> drivers/misc/ocxl/file.c | 27 +++++++++++++++------------
> 1 file changed, 15 insertions(+), 12 deletions(-)
>
> diff --git a/drivers/misc/ocxl/file.c b/drivers/misc/ocxl/file.c
> index 6e63f060e4cc..ac69b7f361f5 100644
> --- a/drivers/misc/ocxl/file.c
> +++ b/drivers/misc/ocxl/file.c
> @@ -14,7 +14,6 @@
> #define OCXL_NUM_MINORS 256 /* Total to reserve */
>
> static dev_t ocxl_dev;
> -static struct class *ocxl_class;
> static DEFINE_MUTEX(minors_idr_lock);
> static struct idr minors_idr;
>
> @@ -509,6 +508,16 @@ static void ocxl_file_make_invisible(struct ocxl_file_info *info)
> cdev_del(&info->cdev);
> }
>
> +static char *ocxl_devnode(const struct device *dev, umode_t *mode)
> +{
> + return kasprintf(GFP_KERNEL, "ocxl/%s", dev_name(dev));
> +}
> +
> +static const struct class ocxl_class = {
> + .name = "ocxl",
> + .devnode = ocxl_devnode,
> +};
> +
> int ocxl_file_register_afu(struct ocxl_afu *afu)
> {
> int minor;
> @@ -529,7 +538,7 @@ int ocxl_file_register_afu(struct ocxl_afu *afu)
>
> info->dev.parent = &fn->dev;
> info->dev.devt = MKDEV(MAJOR(ocxl_dev), minor);
> - info->dev.class = ocxl_class;
> + info->dev.class = &ocxl_class;
> info->dev.release = info_release;
>
> info->afu = afu;
> @@ -584,11 +593,6 @@ void ocxl_file_unregister_afu(struct ocxl_afu *afu)
> device_unregister(&info->dev);
> }
>
> -static char *ocxl_devnode(const struct device *dev, umode_t *mode)
> -{
> - return kasprintf(GFP_KERNEL, "ocxl/%s", dev_name(dev));
> -}
> -
> int ocxl_file_init(void)
> {
> int rc;
> @@ -601,20 +605,19 @@ int ocxl_file_init(void)
> return rc;
> }
>
> - ocxl_class = class_create("ocxl");
> - if (IS_ERR(ocxl_class)) {
> + rc = class_register(&ocxl_class);
> + if (rc) {
> pr_err("Unable to create ocxl class\n");
> unregister_chrdev_region(ocxl_dev, OCXL_NUM_MINORS);
> - return PTR_ERR(ocxl_class);
> + return rc;
> }
>
> - ocxl_class->devnode = ocxl_devnode;
> return 0;
> }
>
> void ocxl_file_exit(void)
> {
> - class_destroy(ocxl_class);
> + class_unregister(&ocxl_class);
> unregister_chrdev_region(ocxl_dev, OCXL_NUM_MINORS);
> idr_destroy(&minors_idr);
> }