[PATCH] dca: make dca_class a static const structure

From: Ivan Orlov
Date: Fri Apr 12 2024 - 11:21:11 EST


The driver core allows for struct class to be in read-only memory. Move
the dca_class structure to be declared at build time placing it into
read-only memory, instead of having to be dynamically allocated at boot
time.

Suggested-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
Signed-off-by: Ivan Orlov <ivan.orlov0322@xxxxxxxxx>
---
drivers/dca/dca-sysfs.c | 20 ++++++++++++--------
1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/drivers/dca/dca-sysfs.c b/drivers/dca/dca-sysfs.c
index fcc83ede0909..b915c2b4601e 100644
--- a/drivers/dca/dca-sysfs.c
+++ b/drivers/dca/dca-sysfs.c
@@ -13,7 +13,9 @@
#include <linux/gfp.h>
#include <linux/export.h>

-static struct class *dca_class;
+static const struct class dca_class = {
+ .name = "dca",
+};
static struct idr dca_idr;
static spinlock_t dca_idr_lock;

@@ -22,14 +24,14 @@ int dca_sysfs_add_req(struct dca_provider *dca, struct device *dev, int slot)
struct device *cd;
static int req_count;

- cd = device_create(dca_class, dca->cd, MKDEV(0, slot + 1), NULL,
+ cd = device_create(&dca_class, dca->cd, MKDEV(0, slot + 1), NULL,
"requester%d", req_count++);
return PTR_ERR_OR_ZERO(cd);
}

void dca_sysfs_remove_req(struct dca_provider *dca, int slot)
{
- device_destroy(dca_class, MKDEV(0, slot + 1));
+ device_destroy(&dca_class, MKDEV(0, slot + 1));
}

int dca_sysfs_add_provider(struct dca_provider *dca, struct device *dev)
@@ -49,7 +51,7 @@ int dca_sysfs_add_provider(struct dca_provider *dca, struct device *dev)
if (ret < 0)
return ret;

- cd = device_create(dca_class, dev, MKDEV(0, 0), NULL, "dca%d", dca->id);
+ cd = device_create(&dca_class, dev, MKDEV(0, 0), NULL, "dca%d", dca->id);
if (IS_ERR(cd)) {
spin_lock(&dca_idr_lock);
idr_remove(&dca_idr, dca->id);
@@ -71,20 +73,22 @@ void dca_sysfs_remove_provider(struct dca_provider *dca)

int __init dca_sysfs_init(void)
{
+ int err;
+
idr_init(&dca_idr);
spin_lock_init(&dca_idr_lock);

- dca_class = class_create("dca");
- if (IS_ERR(dca_class)) {
+ err = class_register(&dca_class);
+ if (err) {
idr_destroy(&dca_idr);
- return PTR_ERR(dca_class);
+ return err;
}
return 0;
}

void __exit dca_sysfs_exit(void)
{
- class_destroy(dca_class);
+ class_unregister(&dca_class);
idr_destroy(&dca_idr);
}

--
2.34.1