[PATCH] accel/habanalabs: make hl_class constant

Greg Kroah-Hartman gregkh at linuxfoundation.org
Fri Oct 6 13:56:55 UTC 2023


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

This requires some passing of const struct class * around in the common
habanalabs code as well as converting the structure itself.

Cc: Dafna Hirschfeld <dhirschfeld at habana.ai>
Cc: Dani Liberman <dliberman at habana.ai>
Cc: Koby Elbaz <kelbaz at habana.ai>
Cc: Oded Gabbay <ogabbay at kernel.org>
Cc: Ofir Bitton <obitton at habana.ai>
Cc: Ohad Sharabi <osharabi at habana.ai>
Cc: Stanislaw Gruszka <stanislaw.gruszka at linux.intel.com>
Cc: Tal Cohen <talcohen at habana.ai>
Cc: Tomer Tayar <ttayar at habana.ai>
Signed-off-by: Greg Kroah-Hartman <gregkh at linuxfoundation.org>
---
 drivers/accel/habanalabs/common/device.c        |  2 +-
 drivers/accel/habanalabs/common/habanalabs.h    |  2 +-
 .../accel/habanalabs/common/habanalabs_drv.c    | 17 ++++++++++-------
 3 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/drivers/accel/habanalabs/common/device.c b/drivers/accel/habanalabs/common/device.c
index b97339d1f7c6..4c28d8cfbb68 100644
--- a/drivers/accel/habanalabs/common/device.c
+++ b/drivers/accel/habanalabs/common/device.c
@@ -652,7 +652,7 @@ static void device_release_func(struct device *dev)
  *
  * Initialize a cdev and a Linux device for habanalabs's device.
  */
-static int device_init_cdev(struct hl_device *hdev, struct class *class,
+static int device_init_cdev(struct hl_device *hdev, const struct class *class,
 				int minor, const struct file_operations *fops,
 				char *name, struct cdev *cdev,
 				struct device **dev)
diff --git a/drivers/accel/habanalabs/common/habanalabs.h b/drivers/accel/habanalabs/common/habanalabs.h
index 2f027d5a8206..f1c78555e611 100644
--- a/drivers/accel/habanalabs/common/habanalabs.h
+++ b/drivers/accel/habanalabs/common/habanalabs.h
@@ -3308,7 +3308,7 @@ struct hl_device {
 	u64				pcie_bar_phys[HL_PCI_NUM_BARS];
 	void __iomem			*pcie_bar[HL_PCI_NUM_BARS];
 	void __iomem			*rmmio;
-	struct class			*hclass;
+	const struct class		*hclass;
 	struct cdev			cdev;
 	struct cdev			cdev_ctrl;
 	struct device			*dev;
diff --git a/drivers/accel/habanalabs/common/habanalabs_drv.c b/drivers/accel/habanalabs/common/habanalabs_drv.c
index 7263e84c1a4d..4f1fdff3843d 100644
--- a/drivers/accel/habanalabs/common/habanalabs_drv.c
+++ b/drivers/accel/habanalabs/common/habanalabs_drv.c
@@ -27,7 +27,11 @@ MODULE_DESCRIPTION(HL_DRIVER_DESC);
 MODULE_LICENSE("GPL v2");
 
 static int hl_major;
-static struct class *hl_class;
+
+static const struct class hl_class = {
+	.name = HL_NAME,
+};
+
 static DEFINE_IDR(hl_devs_idr);
 static DEFINE_MUTEX(hl_devs_idr_lock);
 
@@ -317,7 +321,7 @@ static void copy_kernel_module_params_to_device(struct hl_device *hdev)
 	hdev->asic_prop.fw_security_enabled = is_asic_secured(hdev->asic_type);
 
 	hdev->major = hl_major;
-	hdev->hclass = hl_class;
+	hdev->hclass = &hl_class;
 	hdev->memory_scrub = memory_scrub;
 	hdev->reset_on_lockup = reset_on_lockup;
 	hdev->boot_error_status_mask = boot_error_status_mask;
@@ -691,10 +695,9 @@ static int __init hl_init(void)
 
 	hl_major = MAJOR(dev);
 
-	hl_class = class_create(HL_NAME);
-	if (IS_ERR(hl_class)) {
+	rc = class_register(&hl_class);
+	if (rc) {
 		pr_err("failed to allocate class\n");
-		rc = PTR_ERR(hl_class);
 		goto remove_major;
 	}
 
@@ -712,7 +715,7 @@ static int __init hl_init(void)
 
 remove_debugfs:
 	hl_debugfs_fini();
-	class_destroy(hl_class);
+	class_unregister(&hl_class);
 remove_major:
 	unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
 	return rc;
@@ -732,7 +735,7 @@ static void __exit hl_exit(void)
 	 */
 	hl_debugfs_fini();
 
-	class_destroy(hl_class);
+	class_unregister(&hl_class);
 	unregister_chrdev_region(MKDEV(hl_major, 0), HL_MAX_MINORS);
 
 	idr_destroy(&hl_devs_idr);
-- 
2.42.0



More information about the dri-devel mailing list