Skip to contentSkip to conversation

staging: pi433: Use class_create API.

PATCH In review Shahar Avidar 3 participants 12 messages May 1, 2024 – May 2, 2024 Staging

lore
Share & tools
lore
#0 lore raw

cover staging: pi433: Use class_create API.

This patchset reduces static allocated memory by using class_create instead of class_register.

Shahar Avidar (2):
staging: pi433: Use class_create instead of class_register.
staging: pi433: Rename goto label.

 drivers/staging/pi433/pi433_if.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

base-commit: 75ff53c44f5e151d21d949416633b56e56160124
prerequisite-patch-id: 91943193af2fea74182be67fb583235a3fbeb77b
prerequisite-patch-id: 2cad031ba6a0782a67ab1645ff034a8be65c2e76
prerequisite-patch-id: 1a852ed8f9d133aec7c651fd9007e59e39c55fb7

Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/2 staging: pi433: Use class_create instead of class_register.

Make use of a higher level API.
Reduce global memory allocation from struct class to pointer size.

---
 drivers/staging/pi433/pi433_if.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
drivers/staging/pi433/pi433_if.c +10 -10
index 31aeabb1f153..c8c1d296184b 100644
@@ -59,9 +59,7 @@ static DEFINE_MUTEX(minor_lock); /* Protect idr accesses */
 static struct dentry *root_dir;	/* debugfs root directory for the driver */
 
 /* mainly for udev to create /dev/pi433 */
-static const struct class pi433_class = {
-	.name = "pi433",
-};
+static const struct class *pi433_class;
 
 /*
  * tx config is instance specific
@@ -1263,7 +1261,7 @@ static int pi433_probe(struct spi_device *spi)
 
 	/* create device */
 	pi433->devt = MKDEV(MAJOR(pi433_devt), pi433->minor);
-	pi433->dev = device_create(&pi433_class,
+	pi433->dev = device_create(pi433_class,
 				   &spi->dev,
 				   pi433->devt,
 				   pi433,
@@ -1319,7 +1317,7 @@ static int pi433_probe(struct spi_device *spi)
 cdev_failed:
 	kthread_stop(pi433->tx_task_struct);
 send_thread_failed:
-	device_destroy(&pi433_class, pi433->devt);
+	device_destroy(pi433_class, pi433->devt);
 device_create_failed:
 	pi433_free_minor(pi433);
 minor_failed:
@@ -1346,7 +1344,7 @@ static void pi433_remove(struct spi_device *spi)
 
 	kthread_stop(pi433->tx_task_struct);
 
-	device_destroy(&pi433_class, pi433->devt);
+	device_destroy(pi433_class, pi433->devt);
 
 	cdev_del(pi433->cdev);
 
@@ -1401,9 +1399,11 @@ static int __init pi433_init(void)
 	if (status < 0)
 		return status;
 
-	status = class_register(&pi433_class);
-	if (status)
+	pi433_class = class_create("pi433");
+	if (IS_ERR(pi433_class)) {
+		status = PTR_ERR(pi433_class);
 		goto unreg_chrdev;
+	}
 
 	root_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
 
@@ -1415,7 +1415,7 @@ static int __init pi433_init(void)
 
 unreg_class_and_remove_dbfs:
 	debugfs_remove(root_dir);
-	class_unregister(&pi433_class);
+	class_destroy(pi433_class);
 unreg_chrdev:
 	unregister_chrdev(MAJOR(pi433_devt), pi433_spi_driver.driver.name);
 	return status;
@@ -1427,7 +1427,7 @@ static void __exit pi433_exit(void)
 {
 	spi_unregister_driver(&pi433_spi_driver);
 	debugfs_remove(root_dir);
-	class_unregister(&pi433_class);
+	class_destroy(pi433_class);
 	unregister_chrdev(MAJOR(pi433_devt), pi433_spi_driver.driver.name);
 }
 module_exit(pi433_exit);

base-commit: 75ff53c44f5e151d21d949416633b56e56160124
prerequisite-patch-id: 91943193af2fea74182be67fb583235a3fbeb77b
prerequisite-patch-id: 2cad031ba6a0782a67ab1645ff034a8be65c2e76
prerequisite-patch-id: 1a852ed8f9d133aec7c651fd9007e59e39c55fb7

Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/2] staging: pi433: Use class_create instead of class_register.

Quoted 2 lines · Shahar Avidar

Make use of a higher level API.
Reduce global memory allocation from struct class to pointer size.

Doesn't this move the memory in the opposite direction from what we want? Originally, it's static const. Isn't that the simplest best kind of memory?

regards,
dan carpenter

maintainer
#3 lore raw

Re: [PATCH 1/2] staging: pi433: Use class_create instead of class_register.

Quoted 7 lines · Dan Carpenter
Quoted 2 lines · Shahar Avidar

Make use of a higher level API.
Reduce global memory allocation from struct class to pointer size.

Doesn't this move the memory in the opposite direction from what we want? Originally, it's static const. Isn't that the simplest best kind of memory?

Our reviews just crossed... This is just a revert (in 2 steps oddly), of a previous commit that changed this api call, and for that reason alone we can't take it :)

thanks,

greg k-h

#4 lore raw

Re: [PATCH 1/2] staging: pi433: Use class_create instead of class_register.

Quoted 5 lines · Greg KH
Quoted 4 lines · Dan Carpenter
Quoted 2 lines · Shahar Avidar

Make use of a higher level API.
Reduce global memory allocation from struct class to pointer size.

Quoted 8 lines
Quoted 3 lines

Doesn't this move the memory in the opposite direction from what we want? Originally, it's static const. Isn't that the simplest best kind of memory?

Our reviews just crossed... This is just a revert (in 2 steps oddly), of a previous commit that changed this api call, and for that reason alone we can't take it :)

Thank you for your input.
I replied to Greg's review on another thread.
I won't pursue this change.

Signature · 3 lines
Regards,

Shahar
maintainer
#5 lore raw

Re: [PATCH 1/2] staging: pi433: Use class_create instead of class_register.

In reply to Shahar Avidar
Quoted 1 line · Shahar Avidar

Make use of a higher level API.

What does this mean?

Quoted 1 line

Reduce global memory allocation from struct class to pointer size.

No, you increased memory allocation here, why do you think you reduced it?

Also, this looks like a revert of commit f267da65bb6b ("staging: pi433:
make pi433_class constant"), accepted a few months ago, why not just call it out as an explicit revert if that's what you want to do?

class_create is going away "soon", why add this back when people are working so hard to remove its usage? What tutorial did you read that made you want to make this change?

thanks,

greg k-h

#6 lore raw

Re: [PATCH 1/2] staging: pi433: Use class_create instead of class_register.

Quoted 5 lines · Greg KH
Quoted 1 line · Shahar Avidar

Make use of a higher level API.

What does this mean?

By "higher level" I meant a wrapper function that includes the "class_register" call.

Quoted 5 lines
Quoted 1 line

Reduce global memory allocation from struct class to pointer size.

No, you increased memory allocation here, why do you think you reduced it?

Reducing *global* memory allocation.
I understand the tradeoff would be allocating in run time the class struct anyway, but than, it could also be freed.

Since the Pi433 is a RasPi expansion board and can be attached\removed in an asynchronous matter by the user, and only one can be attached at a time, I thought it is best not to statically allocate memory which won't be freed even if the hat is removed.

By using the class_create & class_destroy I thought of reducing memory allocated by the RasPi if the pi433 is removed.

But following your response I now actually see that the class struct will have the same lifespan anyway if allocated statically or dynamically if its alive between the init\exit calls.

Quoted 4 lines

Also, this looks like a revert of commit f267da65bb6b ("staging: pi433:
make pi433_class constant"), accepted a few months ago, why not just call it out as an explicit revert if that's what you want to do?

I actually saw this commit, but for some reason did not connect the dots when I wrote this patch. My bad.

Quoted 4 lines

class_create is going away "soon", why add this back when people are working so hard to remove its usage? What tutorial did you read that made you want to make this change?

It's true, I got it the wrong way I guess. I thought class_create is the preferred API (but now that you mentioned commit f267da65bb6b, I see it's not). I did notice it in many other drivers though, and took them as an example (e.g. gnss).

Quoted 3 lines

thanks,

greg k-h

I actually initially thought that the pi433 class should be removed since it doesn't bring any new attributes with it, and that spi_slave_class is more appropriate, but then I saw no other driver using it. Any thoughts about that?

Signature · 3 lines
Regards,

Shahar
maintainer
#7 lore raw

Re: [PATCH 1/2] staging: pi433: Use class_create instead of class_register.

Quoted 9 lines · Shahar Avidar
Quoted 5 lines · Greg KH
Quoted 1 line · Shahar Avidar

Make use of a higher level API.

What does this mean?

By "higher level" I meant a wrapper function that includes the "class_register" call.

Quoted 6 lines
Quoted 5 lines
Quoted 1 line

Reduce global memory allocation from struct class to pointer size.

No, you increased memory allocation here, why do you think you reduced it?

Reducing *global* memory allocation.

And again, you *increased* memory allocation by making this be dynamically created instead of the current code which is a static and can be placed into read-only memory with no padding required unlike a dynamic memory chunk is. You also removed the read-only markings of the structure for no reason, in a way, making the code a tad be more insecure as well as increasing memory usage.

So be careful please.

Quoted 2 lines

I understand the tradeoff would be allocating in run time the class struct anyway, but than, it could also be freed.

When is it freed that the current code is not also freed?

Quoted 4 lines

Since the Pi433 is a RasPi expansion board and can be attached\removed in an asynchronous matter by the user, and only one can be attached at a time, I thought it is best not to statically allocate memory which won't be freed even if the hat is removed.

Is that what happens in the code?

Quoted 2 lines

By using the class_create & class_destroy I thought of reducing memory allocated by the RasPi if the pi433 is removed.

Try it and see :)

Quoted 3 lines

But following your response I now actually see that the class struct will have the same lifespan anyway if allocated statically or dynamically if its alive between the init\exit calls.

Yes.

Quoted 7 lines
Quoted 4 lines

Also, this looks like a revert of commit f267da65bb6b ("staging: pi433:
make pi433_class constant"), accepted a few months ago, why not just call it out as an explicit revert if that's what you want to do?

I actually saw this commit, but for some reason did not connect the dots when I wrote this patch. My bad.

Quoted 8 lines
Quoted 4 lines

class_create is going away "soon", why add this back when people are working so hard to remove its usage? What tutorial did you read that made you want to make this change?

It's true, I got it the wrong way I guess. I thought class_create is the preferred API (but now that you mentioned commit f267da65bb6b, I see it's not). I did notice it in many other drivers though, and took them as an example (e.g. gnss).

There are patches out that replace almost all users of class_create() such that it should be almost gone from the tree.

Quoted 8 lines
Quoted 3 lines

thanks,

greg k-h

I actually initially thought that the pi433 class should be removed since it doesn't bring any new attributes with it, and that spi_slave_class is more appropriate, but then I saw no other driver using it. Any thoughts about that?

The whole driver is going to be removed soon, please see the mailing list archives for the details.

thanks,

greg k-h

#8 lore raw

PATCH 2/2 staging: pi433: Rename goto label.

In reply to Shahar Avidar

Use destroy_class_and_remove_dbfs instead of unreg_class_and_remove_dbfs.

---
 drivers/staging/pi433/pi433_if.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/pi433/pi433_if.c +2 -2
index c8c1d296184b..4fffd7007040 100644
@@ -1409,11 +1409,11 @@ static int __init pi433_init(void)
 
 	status = spi_register_driver(&pi433_spi_driver);
 	if (status < 0)
-		goto unreg_class_and_remove_dbfs;
+		goto destroy_class_and_remove_dbfs;
 
 	return 0;
 
-unreg_class_and_remove_dbfs:
+destroy_class_and_remove_dbfs:
 	debugfs_remove(root_dir);
 	class_destroy(pi433_class);
 unreg_chrdev:
Signature · 1 line
2.34.1
#9 lore raw

Re: [PATCH 2/2] staging: pi433: Rename goto label.

Quoted 14 lines · Shahar Avidar

Use destroy_class_and_remove_dbfs instead of unreg_class_and_remove_dbfs.

---
 drivers/staging/pi433/pi433_if.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/pi433/pi433_if.c +0 -0
index c8c1d296184b..4fffd7007040 100644
@@ -1409,11 +1409,11 @@ static int __init pi433_init(void)
 
 	status = spi_register_driver(&pi433_spi_driver);

drivers/staging/pi433/pi433_if.c static int __init pi433_init(void) · line 1418

Quoted 10 lines
drivers/staging/pi433/pi433_if.c +2 -2
 	if (status < 0)
-		goto unreg_class_and_remove_dbfs;
+		goto destroy_class_and_remove_dbfs;
 
 	return 0;
 
-unreg_class_and_remove_dbfs:
+destroy_class_and_remove_dbfs:
 	debugfs_remove(root_dir);
 	class_destroy(pi433_class);

This is cleaning up something which changed in patch 1 so it should have been done in patch 1.

regards,
dan carpenter

Quoted 3 lines
drivers/staging/pi433/pi433_if.c +0 -0
 unreg_chrdev:
Signature · 1 line
2.34.1
#10 lore raw

Re: [PATCH 2/2] staging: pi433: Rename goto label.

Quoted 21 lines · Dan Carpenter
Quoted 20 lines · Shahar Avidar

Use destroy_class_and_remove_dbfs instead of unreg_class_and_remove_dbfs.

---
  drivers/staging/pi433/pi433_if.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/pi433/pi433_if.c +1 -1
index c8c1d296184b..4fffd7007040 100644
@@ -1409,11 +1409,11 @@ static int __init pi433_init(void)
 
  	status = spi_register_driver(&pi433_spi_driver);
  	if (status < 0)
-		goto unreg_class_and_remove_dbfs;
+		goto destroy_class_and_remove_dbfs;
 
  	return 0;
 
Quoted 8 lines

drivers/staging/pi433/pi433_if.c static int __init pi433_init(void) · line 1418

Quoted 4 lines
drivers/staging/pi433/pi433_if.c +1 -1
-unreg_class_and_remove_dbfs:
+destroy_class_and_remove_dbfs:
  	debugfs_remove(root_dir);
  	class_destroy(pi433_class);

This is cleaning up something which changed in patch 1 so it should have been done in patch 1.

Thanks for your input.
I thought of a previous comment you had were you noted Greg preferred small patches, so I did my best to keep the first patch the with minimum changes without breaking git digest.

This patchset won't be accepted anyway.

Quoted 6 lines

regards,
dan carpenter

Quoted 3 lines
drivers/staging/pi433/pi433_if.c +0 -0
  unreg_chrdev:
Signature · 1 line
2.34.1
Signature · 3 lines
Regards,

Shahar
#11 lore raw

Re: [PATCH 2/2] staging: pi433: Rename goto label.

Quoted 27 lines · Shahar Avidar
Quoted 17 lines · Dan Carpenter
Quoted 16 lines · Shahar Avidar

Use destroy_class_and_remove_dbfs instead of unreg_class_and_remove_dbfs.

---
  drivers/staging/pi433/pi433_if.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/pi433/pi433_if.c +1 -1
index c8c1d296184b..4fffd7007040 100644
@@ -1409,11 +1409,11 @@ static int __init pi433_init(void)
  	status = spi_register_driver(&pi433_spi_driver);
  	if (status < 0)
-		goto unreg_class_and_remove_dbfs;
+		goto destroy_class_and_remove_dbfs;
Quoted 9 lines

drivers/staging/pi433/pi433_if.c static int __init pi433_init(void) · line 1418

Quoted 5 lines
drivers/staging/pi433/pi433_if.c +1 -1
  	return 0;
-unreg_class_and_remove_dbfs:
+destroy_class_and_remove_dbfs:
  	debugfs_remove(root_dir);
  	class_destroy(pi433_class);

This is cleaning up something which changed in patch 1 so it should have been done in patch 1.

Quoted 4 lines

Thanks for your input.
I thought of a previous comment you had were you noted Greg preferred small patches, so I did my best to keep the first patch the with minimum changes without breaking git digest.

The rule is not "as small as possible", it's "one thing per patch". It's a bit subtle, but you're doing half a thing in this patch. Not a huge deal. It's part of the learning process.

regards,
dan carpenter