2023-06-20 18:30:49

by Greg Kroah-Hartman

[permalink] [raw]
Subject: [PATCH 1/4] block/rnbd: make all 'class' structures const

From: Ivan Orlov <[email protected]>

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

Cc: "Md. Haris Iqbal" <[email protected]>
Cc: Jack Wang <[email protected]>
Cc: Jens Axboe <[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/block/rnbd/rnbd-clt-sysfs.c | 20 +++++++++++---------
drivers/block/rnbd/rnbd-srv-sysfs.c | 22 ++++++++++++----------
2 files changed, 23 insertions(+), 19 deletions(-)

diff --git a/drivers/block/rnbd/rnbd-clt-sysfs.c b/drivers/block/rnbd/rnbd-clt-sysfs.c
index 8c6087949794..e84abac04f4f 100644
--- a/drivers/block/rnbd/rnbd-clt-sysfs.c
+++ b/drivers/block/rnbd/rnbd-clt-sysfs.c
@@ -24,7 +24,9 @@
#include "rnbd-clt.h"

static struct device *rnbd_dev;
-static struct class *rnbd_dev_class;
+static const struct class rnbd_dev_class = {
+ .name = "rnbd_client",
+};
static struct kobject *rnbd_devs_kobj;

enum {
@@ -646,11 +648,11 @@ int rnbd_clt_create_sysfs_files(void)
{
int err;

- rnbd_dev_class = class_create("rnbd-client");
- if (IS_ERR(rnbd_dev_class))
- return PTR_ERR(rnbd_dev_class);
+ err = class_register(&rnbd_dev_class);
+ if (err)
+ return err;

- rnbd_dev = device_create_with_groups(rnbd_dev_class, NULL,
+ rnbd_dev = device_create_with_groups(&rnbd_dev_class, NULL,
MKDEV(0, 0), NULL,
default_attr_groups, "ctl");
if (IS_ERR(rnbd_dev)) {
@@ -666,9 +668,9 @@ int rnbd_clt_create_sysfs_files(void)
return 0;

dev_destroy:
- device_destroy(rnbd_dev_class, MKDEV(0, 0));
+ device_destroy(&rnbd_dev_class, MKDEV(0, 0));
cls_destroy:
- class_destroy(rnbd_dev_class);
+ class_unregister(&rnbd_dev_class);

return err;
}
@@ -678,6 +680,6 @@ void rnbd_clt_destroy_sysfs_files(void)
sysfs_remove_group(&rnbd_dev->kobj, &default_attr_group);
kobject_del(rnbd_devs_kobj);
kobject_put(rnbd_devs_kobj);
- device_destroy(rnbd_dev_class, MKDEV(0, 0));
- class_destroy(rnbd_dev_class);
+ device_destroy(&rnbd_dev_class, MKDEV(0, 0));
+ class_unregister(&rnbd_dev_class);
}
diff --git a/drivers/block/rnbd/rnbd-srv-sysfs.c b/drivers/block/rnbd/rnbd-srv-sysfs.c
index d5d9267e1fa5..5e69c0112e23 100644
--- a/drivers/block/rnbd/rnbd-srv-sysfs.c
+++ b/drivers/block/rnbd/rnbd-srv-sysfs.c
@@ -20,7 +20,9 @@
#include "rnbd-srv.h"

static struct device *rnbd_dev;
-static struct class *rnbd_dev_class;
+static const struct class rnbd_dev_class = {
+ .name = "rnbd-server",
+};
static struct kobject *rnbd_devs_kobj;

static void rnbd_srv_dev_release(struct kobject *kobj)
@@ -215,12 +217,12 @@ int rnbd_srv_create_sysfs_files(void)
{
int err;

- rnbd_dev_class = class_create("rnbd-server");
- if (IS_ERR(rnbd_dev_class))
- return PTR_ERR(rnbd_dev_class);
+ err = class_register(&rnbd_dev_class);
+ if (err)
+ return err;

- rnbd_dev = device_create(rnbd_dev_class, NULL,
- MKDEV(0, 0), NULL, "ctl");
+ rnbd_dev = device_create(&rnbd_dev_class, NULL,
+ MKDEV(0, 0), NULL, "ctl");
if (IS_ERR(rnbd_dev)) {
err = PTR_ERR(rnbd_dev);
goto cls_destroy;
@@ -234,9 +236,9 @@ int rnbd_srv_create_sysfs_files(void)
return 0;

dev_destroy:
- device_destroy(rnbd_dev_class, MKDEV(0, 0));
+ device_destroy(&rnbd_dev_class, MKDEV(0, 0));
cls_destroy:
- class_destroy(rnbd_dev_class);
+ class_unregister(&rnbd_dev_class);

return err;
}
@@ -245,6 +247,6 @@ void rnbd_srv_destroy_sysfs_files(void)
{
kobject_del(rnbd_devs_kobj);
kobject_put(rnbd_devs_kobj);
- device_destroy(rnbd_dev_class, MKDEV(0, 0));
- class_destroy(rnbd_dev_class);
+ device_destroy(&rnbd_dev_class, MKDEV(0, 0));
+ class_unregister(&rnbd_dev_class);
}
--
2.41.0



2023-06-20 18:33:40

by Greg Kroah-Hartman

[permalink] [raw]
Subject: [PATCH 4/4] bsg: make bsg_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 bsg_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: FUJITA Tomonori <[email protected]>
Cc: Jens Axboe <[email protected]>
Cc: [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]>
---
block/bsg.c | 18 +++++++++++-------
1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/block/bsg.c b/block/bsg.c
index 7eca43f33d7f..e04fb075d604 100644
--- a/block/bsg.c
+++ b/block/bsg.c
@@ -39,7 +39,7 @@ static inline struct bsg_device *to_bsg_device(struct inode *inode)
#define BSG_MAX_DEVS 32768

static DEFINE_IDA(bsg_minor_ida);
-static struct class *bsg_class;
+static const struct class bsg_class;
static int bsg_major;

static unsigned int bsg_timeout(struct bsg_device *bd, struct sg_io_v4 *hdr)
@@ -206,7 +206,7 @@ struct bsg_device *bsg_register_queue(struct request_queue *q,
return ERR_PTR(ret);
}
bd->device.devt = MKDEV(bsg_major, ret);
- bd->device.class = bsg_class;
+ bd->device.class = &bsg_class;
bd->device.parent = parent;
bd->device.release = bsg_device_release;
dev_set_name(&bd->device, "%s", name);
@@ -240,15 +240,19 @@ static char *bsg_devnode(const struct device *dev, umode_t *mode)
return kasprintf(GFP_KERNEL, "bsg/%s", dev_name(dev));
}

+static const struct class bsg_class = {
+ .name = "bsg",
+ .devnode = bsg_devnode,
+};
+
static int __init bsg_init(void)
{
dev_t devid;
int ret;

- bsg_class = class_create("bsg");
- if (IS_ERR(bsg_class))
- return PTR_ERR(bsg_class);
- bsg_class->devnode = bsg_devnode;
+ ret = class_register(&bsg_class);
+ if (ret)
+ return ret;

ret = alloc_chrdev_region(&devid, 0, BSG_MAX_DEVS, "bsg");
if (ret)
@@ -260,7 +264,7 @@ static int __init bsg_init(void)
return 0;

destroy_bsg_class:
- class_destroy(bsg_class);
+ class_unregister(&bsg_class);
return ret;
}

--
2.41.0


2023-06-20 18:54:52

by Greg Kroah-Hartman

[permalink] [raw]
Subject: [PATCH 2/4] aoe: make aoe_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 aoe_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: Justin Sanders <[email protected]>
Cc: Jens Axboe <[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/block/aoe/aoechr.c | 30 +++++++++++++++++-------------
1 file changed, 17 insertions(+), 13 deletions(-)

diff --git a/drivers/block/aoe/aoechr.c b/drivers/block/aoe/aoechr.c
index 4c666f72203f..a42c4bcc85ba 100644
--- a/drivers/block/aoe/aoechr.c
+++ b/drivers/block/aoe/aoechr.c
@@ -49,7 +49,7 @@ static int emsgs_head_idx, emsgs_tail_idx;
static struct completion emsgs_comp;
static spinlock_t emsgs_lock;
static int nblocked_emsgs_readers;
-static struct class *aoe_class;
+
static struct aoe_chardev chardevs[] = {
{ MINOR_ERR, "err" },
{ MINOR_DISCOVER, "discover" },
@@ -58,6 +58,16 @@ static struct aoe_chardev chardevs[] = {
{ MINOR_FLUSH, "flush" },
};

+static char *aoe_devnode(const struct device *dev, umode_t *mode)
+{
+ return kasprintf(GFP_KERNEL, "etherd/%s", dev_name(dev));
+}
+
+static const struct class aoe_class = {
+ .name = "aoe",
+ .devnode = aoe_devnode,
+};
+
static int
discover(void)
{
@@ -273,11 +283,6 @@ static const struct file_operations aoe_fops = {
.llseek = noop_llseek,
};

-static char *aoe_devnode(const struct device *dev, umode_t *mode)
-{
- return kasprintf(GFP_KERNEL, "etherd/%s", dev_name(dev));
-}
-
int __init
aoechr_init(void)
{
@@ -290,15 +295,14 @@ aoechr_init(void)
}
init_completion(&emsgs_comp);
spin_lock_init(&emsgs_lock);
- aoe_class = class_create("aoe");
- if (IS_ERR(aoe_class)) {
+ n = class_register(&aoe_class);
+ if (n) {
unregister_chrdev(AOE_MAJOR, "aoechr");
- return PTR_ERR(aoe_class);
+ return n;
}
- aoe_class->devnode = aoe_devnode;

for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
- device_create(aoe_class, NULL,
+ device_create(&aoe_class, NULL,
MKDEV(AOE_MAJOR, chardevs[i].minor), NULL,
chardevs[i].name);

@@ -311,8 +315,8 @@ aoechr_exit(void)
int i;

for (i = 0; i < ARRAY_SIZE(chardevs); ++i)
- device_destroy(aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor));
- class_destroy(aoe_class);
+ device_destroy(&aoe_class, MKDEV(AOE_MAJOR, chardevs[i].minor));
+ class_unregister(&aoe_class);
unregister_chrdev(AOE_MAJOR, "aoechr");
}

--
2.41.0


2023-06-20 18:55:22

by Greg Kroah-Hartman

[permalink] [raw]
Subject: [PATCH 3/4] ublk: make ublk_chr_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 ublk_chr_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: Ming Lei <[email protected]>
Cc: Jens Axboe <[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/block/ublk_drv.c | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c
index 33d3298a0da1..f25d93365701 100644
--- a/drivers/block/ublk_drv.c
+++ b/drivers/block/ublk_drv.c
@@ -183,7 +183,9 @@ struct ublk_params_header {
};

static dev_t ublk_chr_devt;
-static struct class *ublk_chr_class;
+static const struct class ublk_chr_class = {
+ .name = "ublk-char",
+};

static DEFINE_IDR(ublk_index_idr);
static DEFINE_SPINLOCK(ublk_idr_lock);
@@ -1547,7 +1549,7 @@ static int ublk_add_chdev(struct ublk_device *ub)

dev->parent = ublk_misc.this_device;
dev->devt = MKDEV(MAJOR(ublk_chr_devt), minor);
- dev->class = ublk_chr_class;
+ dev->class = &ublk_chr_class;
dev->release = ublk_cdev_rel;
device_initialize(dev);

@@ -2347,11 +2349,10 @@ static int __init ublk_init(void)
if (ret)
goto unregister_mis;

- ublk_chr_class = class_create("ublk-char");
- if (IS_ERR(ublk_chr_class)) {
- ret = PTR_ERR(ublk_chr_class);
+ ret = class_register(&ublk_chr_class);
+ if (ret)
goto free_chrdev_region;
- }
+
return 0;

free_chrdev_region:
@@ -2369,7 +2370,7 @@ static void __exit ublk_exit(void)
idr_for_each_entry(&ublk_index_idr, ub, id)
ublk_remove(ub);

- class_destroy(ublk_chr_class);
+ class_unregister(&ublk_chr_class);
misc_deregister(&ublk_misc);

idr_destroy(&ublk_index_idr);
--
2.41.0


2023-06-21 10:17:30

by Jinpu Wang

[permalink] [raw]
Subject: Re: [PATCH 1/4] block/rnbd: make all 'class' structures const

On Tue, Jun 20, 2023 at 8:01 PM Greg Kroah-Hartman
<[email protected]> wrote:
>
> From: Ivan Orlov <[email protected]>
>
> Now that the driver core allows for struct class to be in read-only
> memory, making all 'class' structures to be declared at build time
> placing them into read-only memory, instead of having to be dynamically
> allocated at load time.
>
> Cc: "Md. Haris Iqbal" <[email protected]>
> Cc: Jack Wang <[email protected]>
> Cc: Jens Axboe <[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]>
lgtm.
Acked-by: Jack Wang <[email protected]>
> ---
> drivers/block/rnbd/rnbd-clt-sysfs.c | 20 +++++++++++---------
> drivers/block/rnbd/rnbd-srv-sysfs.c | 22 ++++++++++++----------
> 2 files changed, 23 insertions(+), 19 deletions(-)
>
> diff --git a/drivers/block/rnbd/rnbd-clt-sysfs.c b/drivers/block/rnbd/rnbd-clt-sysfs.c
> index 8c6087949794..e84abac04f4f 100644
> --- a/drivers/block/rnbd/rnbd-clt-sysfs.c
> +++ b/drivers/block/rnbd/rnbd-clt-sysfs.c
> @@ -24,7 +24,9 @@
> #include "rnbd-clt.h"
>
> static struct device *rnbd_dev;
> -static struct class *rnbd_dev_class;
> +static const struct class rnbd_dev_class = {
> + .name = "rnbd_client",
> +};
> static struct kobject *rnbd_devs_kobj;
>
> enum {
> @@ -646,11 +648,11 @@ int rnbd_clt_create_sysfs_files(void)
> {
> int err;
>
> - rnbd_dev_class = class_create("rnbd-client");
> - if (IS_ERR(rnbd_dev_class))
> - return PTR_ERR(rnbd_dev_class);
> + err = class_register(&rnbd_dev_class);
> + if (err)
> + return err;
>
> - rnbd_dev = device_create_with_groups(rnbd_dev_class, NULL,
> + rnbd_dev = device_create_with_groups(&rnbd_dev_class, NULL,
> MKDEV(0, 0), NULL,
> default_attr_groups, "ctl");
> if (IS_ERR(rnbd_dev)) {
> @@ -666,9 +668,9 @@ int rnbd_clt_create_sysfs_files(void)
> return 0;
>
> dev_destroy:
> - device_destroy(rnbd_dev_class, MKDEV(0, 0));
> + device_destroy(&rnbd_dev_class, MKDEV(0, 0));
> cls_destroy:
> - class_destroy(rnbd_dev_class);
> + class_unregister(&rnbd_dev_class);
>
> return err;
> }
> @@ -678,6 +680,6 @@ void rnbd_clt_destroy_sysfs_files(void)
> sysfs_remove_group(&rnbd_dev->kobj, &default_attr_group);
> kobject_del(rnbd_devs_kobj);
> kobject_put(rnbd_devs_kobj);
> - device_destroy(rnbd_dev_class, MKDEV(0, 0));
> - class_destroy(rnbd_dev_class);
> + device_destroy(&rnbd_dev_class, MKDEV(0, 0));
> + class_unregister(&rnbd_dev_class);
> }
> diff --git a/drivers/block/rnbd/rnbd-srv-sysfs.c b/drivers/block/rnbd/rnbd-srv-sysfs.c
> index d5d9267e1fa5..5e69c0112e23 100644
> --- a/drivers/block/rnbd/rnbd-srv-sysfs.c
> +++ b/drivers/block/rnbd/rnbd-srv-sysfs.c
> @@ -20,7 +20,9 @@
> #include "rnbd-srv.h"
>
> static struct device *rnbd_dev;
> -static struct class *rnbd_dev_class;
> +static const struct class rnbd_dev_class = {
> + .name = "rnbd-server",
> +};
> static struct kobject *rnbd_devs_kobj;
>
> static void rnbd_srv_dev_release(struct kobject *kobj)
> @@ -215,12 +217,12 @@ int rnbd_srv_create_sysfs_files(void)
> {
> int err;
>
> - rnbd_dev_class = class_create("rnbd-server");
> - if (IS_ERR(rnbd_dev_class))
> - return PTR_ERR(rnbd_dev_class);
> + err = class_register(&rnbd_dev_class);
> + if (err)
> + return err;
>
> - rnbd_dev = device_create(rnbd_dev_class, NULL,
> - MKDEV(0, 0), NULL, "ctl");
> + rnbd_dev = device_create(&rnbd_dev_class, NULL,
> + MKDEV(0, 0), NULL, "ctl");
> if (IS_ERR(rnbd_dev)) {
> err = PTR_ERR(rnbd_dev);
> goto cls_destroy;
> @@ -234,9 +236,9 @@ int rnbd_srv_create_sysfs_files(void)
> return 0;
>
> dev_destroy:
> - device_destroy(rnbd_dev_class, MKDEV(0, 0));
> + device_destroy(&rnbd_dev_class, MKDEV(0, 0));
> cls_destroy:
> - class_destroy(rnbd_dev_class);
> + class_unregister(&rnbd_dev_class);
>
> return err;
> }
> @@ -245,6 +247,6 @@ void rnbd_srv_destroy_sysfs_files(void)
> {
> kobject_del(rnbd_devs_kobj);
> kobject_put(rnbd_devs_kobj);
> - device_destroy(rnbd_dev_class, MKDEV(0, 0));
> - class_destroy(rnbd_dev_class);
> + device_destroy(&rnbd_dev_class, MKDEV(0, 0));
> + class_unregister(&rnbd_dev_class);
> }
> --
> 2.41.0
>

2023-06-21 14:09:10

by Jens Axboe

[permalink] [raw]
Subject: Re: [PATCH 1/4] block/rnbd: make all 'class' structures const


On Tue, 20 Jun 2023 20:01:30 +0200, Greg Kroah-Hartman wrote:
> Now that the driver core allows for struct class to be in read-only
> memory, making all 'class' structures to be declared at build time
> placing them into read-only memory, instead of having to be dynamically
> allocated at load time.
>
>

Applied, thanks!

[1/4] block/rnbd: make all 'class' structures const
commit: 137380c0ec40710cbaf57c7878726c41a6da81cd
[2/4] aoe: make aoe_class a static const structure
commit: 65d7a37d4e3e226bb4a4ddf73a827d0dbc77f530
[3/4] ublk: make ublk_chr_class a static const structure
commit: 2eefd399d28a52739fdbeebe84775275f016171c
[4/4] bsg: make bsg_class a static const structure
commit: 72ef02b8dfa009029fa713e8a731a92d27d14e35

Best regards,
--
Jens Axboe