Skip to contentSkip to conversation

mei: make mei_class a static const structure

PATCH Superseded Ivan Orlov 2 participants 3 messages Aug 10, 2023 – Aug 11, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ivan Orlov

author
#0 lore raw

PATCH 1/1 mei: make mei_class a static const structure

Now that the driver core allows for struct class to be in read-only memory, move the mei_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/misc/mei/main.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
drivers/misc/mei/main.c +10 -9
index 51876da3fd65..902789d3106b 100644
@@ -27,7 +27,9 @@
 #include "mei_dev.h"
 #include "client.h"
 
-static struct class *mei_class;
+static const struct class mei_class = {
+	.name = "mei",
+};
 static dev_t mei_devt;
 #define MEI_MAX_DEVS  MINORMASK
 static DEFINE_MUTEX(mei_minor_lock);
@@ -1115,7 +1117,7 @@ void mei_set_devstate(struct mei_device *dev, enum mei_dev_state state)
 
 	dev->dev_state = state;
 
-	clsdev = class_find_device_by_devt(mei_class, dev->cdev.dev);
+	clsdev = class_find_device_by_devt(&mei_class, dev->cdev.dev);
 	if (clsdev) {
 		sysfs_notify(&clsdev->kobj, NULL, "dev_state");
 		put_device(clsdev);
@@ -1232,7 +1234,7 @@ int mei_register(struct mei_device *dev, struct device *parent)
 		goto err_dev_add;
 	}
 
-	clsdev = device_create_with_groups(mei_class, parent, devno,
+	clsdev = device_create_with_groups(&mei_class, parent, devno,
 					   dev, mei_groups,
 					   "mei%d", dev->minor);
 
@@ -1264,7 +1266,7 @@ void mei_deregister(struct mei_device *dev)
 
 	mei_dbgfs_deregister(dev);
 
-	device_destroy(mei_class, devno);
+	device_destroy(&mei_class, devno);
 
 	mei_minor_free(dev);
 }
@@ -1274,10 +1276,9 @@ static int __init mei_init(void)
 {
 	int ret;
 
-	mei_class = class_create("mei");
-	if (IS_ERR(mei_class)) {
+	ret = class_register(&mei_class);
+	if (ret) {
 		pr_err("couldn't create class\n");
-		ret = PTR_ERR(mei_class);
 		goto err;
 	}
 
@@ -1298,7 +1299,7 @@ static int __init mei_init(void)
 err_chrdev:
 	unregister_chrdev_region(mei_devt, MEI_MAX_DEVS);
 err_class:
-	class_destroy(mei_class);
+	class_unregister(&mei_class);
 err:
 	return ret;
 }
@@ -1306,7 +1307,7 @@ static int __init mei_init(void)
 static void __exit mei_exit(void)
 {
 	unregister_chrdev_region(mei_devt, MEI_MAX_DEVS);
-	class_destroy(mei_class);
+	class_unregister(&mei_class);
 	mei_cl_bus_exit();
 }
 
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 16 lines · Ivan Orlov

Now that the driver core allows for struct class to be in read-only memory, move the mei_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/misc/mei/main.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
drivers/misc/mei/main.c +0 -0
index 51876da3fd65..902789d3106b 100644

drivers/misc/mei/main.c line 33

Quoted 8 lines
drivers/misc/mei/main.c +3 -1
@@ -27,7 +27,9 @@
 #include "mei_dev.h"
 #include "client.h"
 
-static struct class *mei_class;
+static const struct class mei_class = {
+	.name = "mei",
+};
 static dev_t mei_devt;

Extra line after the new structure?

Quoted 30 lines
drivers/misc/mei/main.c +3 -3
 #define MEI_MAX_DEVS  MINORMASK
 static DEFINE_MUTEX(mei_minor_lock);
@@ -1115,7 +1117,7 @@ void mei_set_devstate(struct mei_device *dev, enum mei_dev_state state)
 
 	dev->dev_state = state;
 
-	clsdev = class_find_device_by_devt(mei_class, dev->cdev.dev);
+	clsdev = class_find_device_by_devt(&mei_class, dev->cdev.dev);
 	if (clsdev) {
 		sysfs_notify(&clsdev->kobj, NULL, "dev_state");
 		put_device(clsdev);
@@ -1232,7 +1234,7 @@ int mei_register(struct mei_device *dev, struct device *parent)
 		goto err_dev_add;
 	}
 
-	clsdev = device_create_with_groups(mei_class, parent, devno,
+	clsdev = device_create_with_groups(&mei_class, parent, devno,
 					   dev, mei_groups,
 					   "mei%d", dev->minor);
 
@@ -1264,7 +1266,7 @@ void mei_deregister(struct mei_device *dev)
 
 	mei_dbgfs_deregister(dev);
 
-	device_destroy(mei_class, devno);
+	device_destroy(&mei_class, devno);
 
 	mei_minor_free(dev);
 }

drivers/misc/mei/main.c static int __init mei_init(void) · line 1281

Quoted 8 lines
drivers/misc/mei/main.c +2 -2
@@ -1274,10 +1276,9 @@ static int __init mei_init(void)
 {
 	int ret;
 
-	mei_class = class_create("mei");
-	if (IS_ERR(mei_class)) {
+	ret = class_register(&mei_class);
+	if (ret) {
 		pr_err("couldn't create class\n");

This message doesn't make sense anymore. No need for it either, just remove it and do a simple:

		return err;

here instead.

thanks,

greg k-h

author
#2 lore raw
Quoted 69 lines · Greg KH
Quoted 16 lines · Ivan Orlov

Now that the driver core allows for struct class to be in read-only memory, move the mei_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/misc/mei/main.c | 19 ++++++++++---------
  1 file changed, 10 insertions(+), 9 deletions(-)
drivers/misc/mei/main.c +0 -0
index 51876da3fd65..902789d3106b 100644

drivers/misc/mei/main.c line 33

Quoted 8 lines
drivers/misc/mei/main.c +3 -1
@@ -27,7 +27,9 @@
  #include "mei_dev.h"
  #include "client.h"
 
-static struct class *mei_class;
+static const struct class mei_class = {
+	.name = "mei",
+};
  static dev_t mei_devt;

Extra line after the new structure?

Quoted 30 lines
drivers/misc/mei/main.c +3 -3
  #define MEI_MAX_DEVS  MINORMASK
  static DEFINE_MUTEX(mei_minor_lock);
@@ -1115,7 +1117,7 @@ void mei_set_devstate(struct mei_device *dev, enum mei_dev_state state)
 
  	dev->dev_state = state;
 
-	clsdev = class_find_device_by_devt(mei_class, dev->cdev.dev);
+	clsdev = class_find_device_by_devt(&mei_class, dev->cdev.dev);
  	if (clsdev) {
  		sysfs_notify(&clsdev->kobj, NULL, "dev_state");
  		put_device(clsdev);
@@ -1232,7 +1234,7 @@ int mei_register(struct mei_device *dev, struct device *parent)
  		goto err_dev_add;
  	}
 
-	clsdev = device_create_with_groups(mei_class, parent, devno,
+	clsdev = device_create_with_groups(&mei_class, parent, devno,
  					   dev, mei_groups,
  					   "mei%d", dev->minor);
 
@@ -1264,7 +1266,7 @@ void mei_deregister(struct mei_device *dev)
 
  	mei_dbgfs_deregister(dev);
 
-	device_destroy(mei_class, devno);
+	device_destroy(&mei_class, devno);
 
  	mei_minor_free(dev);
  }

drivers/misc/mei/main.c static int __init mei_init(void) · line 1281

Quoted 8 lines
drivers/misc/mei/main.c +2 -2
@@ -1274,10 +1276,9 @@ static int __init mei_init(void)
  {
  	int ret;
 
-	mei_class = class_create("mei");
-	if (IS_ERR(mei_class)) {
+	ret = class_register(&mei_class);
+	if (ret) {
  		pr_err("couldn't create class\n");

This message doesn't make sense anymore. No need for it either, just remove it and do a simple:

Quoted 6 lines
		return err;

here instead.

thanks,

greg k-h

Hi Greg,

Thank you for the review, I'll make the suggested changes and send you the V2.

--
Kind regards,
Ivan Orlov