accel/habanalabs: make hl_class constant
PATCH 1/1 accel/habanalabs: make hl_class constant
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.
--- 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(-)
drivers/accel/habanalabs/common/device.c +1 -1
@@ -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)
drivers/accel/habanalabs/common/habanalabs.h +1 -1
@@ -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;
drivers/accel/habanalabs/common/habanalabs_drv.c +10 -7
@@ -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);
Signature · 1 line
2.42.0
Oded Gabbay
maintainerQuoted 8 lines · Greg Kroah-Hartman
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.
Hi Greg,
Thanks for the patch but if you look at our tip of habanalabs-next branch (to be merged in 6.7), you will see the hl_class related code no longer exists, as we moved completely to the new accel char device class.
So, I'm dropping this patch.
Oded
Quoted 103 lines
Cc Dafna Hirschfeld <[email protected]>Cc Dani Liberman <[email protected]>Cc Koby Elbaz <[email protected]>Cc Oded Gabbay <[email protected]>Cc Ofir Bitton <[email protected]>Cc Ohad Sharabi <[email protected]>Cc Stanislaw Gruszka <[email protected]>Cc Tal Cohen <[email protected]>Cc Tomer Tayar <[email protected]>Signed-off-by Greg Kroah-Hartman <[email protected]>--- 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(-)drivers/accel/habanalabs/common/device.c +1
-1@@ -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)drivers/accel/habanalabs/common/habanalabs.h +1
-1@@ -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;drivers/accel/habanalabs/common/habanalabs_drv.c +10
-8@@ -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
Quoted 11 lines · Oded Gabbay
Quoted 8 lines · Greg Kroah-Hartman
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.
Quoted 6 lines
Hi Greg,
Thanks for the patch but if you look at our tip of habanalabs-next branch (to be merged in 6.7), you will see thehl_classrelated code no longer exists, as we moved completely to the new accel char device class.
So, I'm dropping this patch.
Ah, no code is better, thanks!
greg k-h