Skip to contentSkip to conversation

block/rnbd: make all 'class' structures const

PATCH Applied Greg Kroah-Hartman 3 participants 6 messages Jun 20, 2023 – Jun 21, 2023

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

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.

---
 drivers/block/rnbd/rnbd-clt-sysfs.c | 20 +++++++++++---------
 drivers/block/rnbd/rnbd-srv-sysfs.c | 22 ++++++++++++----------
 2 files changed, 23 insertions(+), 19 deletions(-)
drivers/block/rnbd/rnbd-clt-sysfs.c +11 -9
index 8c6087949794..e84abac04f4f 100644
@@ -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);
 }
drivers/block/rnbd/rnbd-srv-sysfs.c +12 -10
index d5d9267e1fa5..5e69c0112e23 100644
@@ -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);
 }
Signature · 1 line
2.41.0
#1 lore raw

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.

---
 drivers/block/aoe/aoechr.c | 30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)
drivers/block/aoe/aoechr.c +17 -13
index 4c666f72203f..a42c4bcc85ba 100644
@@ -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");
 }
 
Signature · 1 line
2.41.0
#2 lore raw

PATCH 3/4 ublk: make ublk_chr_class a static const structure

In reply to Greg Kroah-Hartman

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.

---
 drivers/block/ublk_drv.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/block/ublk_drv.c +8 -7
index 33d3298a0da1..f25d93365701 100644
@@ -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);
Signature · 1 line
2.41.0
#3 lore raw

PATCH 4/4 bsg: make bsg_class a static const structure

In reply to Greg Kroah-Hartman

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.

---
 block/bsg.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
block/bsg.c +11 -7
index 7eca43f33d7f..e04fb075d604 100644
@@ -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;
 }
 
Signature · 1 line
2.41.0
maintainer Acked-by on v1 1/4
#4 lore raw
In reply to Greg Kroah-Hartman
Quoted 8 lines · Greg Kroah-Hartman

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.

Quoted 7 lines

lgtm.

Quoted 114 lines
---
 drivers/block/rnbd/rnbd-clt-sysfs.c | 20 +++++++++++---------
 drivers/block/rnbd/rnbd-srv-sysfs.c | 22 ++++++++++++----------
 2 files changed, 23 insertions(+), 19 deletions(-)
drivers/block/rnbd/rnbd-clt-sysfs.c +11 -9
index 8c6087949794..e84abac04f4f 100644
@@ -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);
 }
drivers/block/rnbd/rnbd-srv-sysfs.c +12 -11
index d5d9267e1fa5..5e69c0112e23 100644
@@ -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

maintainer Applied
#5 lore raw
In reply to Greg Kroah-Hartman
Quoted 6 lines · Greg Kroah-Hartman

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,

Signature · 1 line
Jens Axboe