diff mbox series

[2/3] fpga: fpga-mgr: make fpga_mgr_class a static const structure

Message ID 20230810172210.6338-2-ivan.orlov0322@gmail.com (mailing list archive)
State New
Headers show
Series [1/3] fpga: bridge: make fpga_bridge_class a static const structure | expand

Commit Message

Ivan Orlov Aug. 10, 2023, 5:22 p.m. UTC
Now that the driver core allows for struct class to be in read-only
memory, move the fpga_mgr_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@linuxfoundation.org>
Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
---
 drivers/fpga/fpga-mgr.c | 40 +++++++++++++++++++---------------------
 1 file changed, 19 insertions(+), 21 deletions(-)

Comments

Xu Yilun Aug. 11, 2023, 3:12 a.m. UTC | #1
On 2023-08-10 at 21:22:09 +0400, Ivan Orlov wrote:
> Now that the driver core allows for struct class to be in read-only
> memory, move the fpga_mgr_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@linuxfoundation.org>
> Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
> ---
>  drivers/fpga/fpga-mgr.c | 40 +++++++++++++++++++---------------------
>  1 file changed, 19 insertions(+), 21 deletions(-)
> 
> diff --git a/drivers/fpga/fpga-mgr.c b/drivers/fpga/fpga-mgr.c
> index eb583f86a0b9..cd5b7371495b 100644
> --- a/drivers/fpga/fpga-mgr.c
> +++ b/drivers/fpga/fpga-mgr.c
> @@ -19,7 +19,6 @@
>  #include <linux/highmem.h>
>  
>  static DEFINE_IDA(fpga_mgr_ida);
> -static struct class *fpga_mgr_class;
>  
>  struct fpga_mgr_devres {
>  	struct fpga_manager *mgr;
> @@ -664,6 +663,20 @@ static struct attribute *fpga_mgr_attrs[] = {
>  };
>  ATTRIBUTE_GROUPS(fpga_mgr);
>  
> +static void fpga_mgr_dev_release(struct device *dev)
> +{
> +	struct fpga_manager *mgr = to_fpga_manager(dev);
> +
> +	ida_free(&fpga_mgr_ida, mgr->dev.id);
> +	kfree(mgr);
> +}
> +
> +static const struct class fpga_mgr_class = {
> +	.name = "fpga_manager",
> +	.dev_groups = fpga_mgr_groups,
> +	.dev_release = fpga_mgr_dev_release,
> +};

Same concern as fpga_bridge, keep the forward declaration and put the
definiton below.

Thanks,
Yilun

> +
>  static struct fpga_manager *__fpga_mgr_get(struct device *dev)
>  {
>  	struct fpga_manager *mgr;
> @@ -693,7 +706,7 @@ static int fpga_mgr_dev_match(struct device *dev, const void *data)
>   */
>  struct fpga_manager *fpga_mgr_get(struct device *dev)
>  {
> -	struct device *mgr_dev = class_find_device(fpga_mgr_class, NULL, dev,
> +	struct device *mgr_dev = class_find_device(&fpga_mgr_class, NULL, dev,
>  						   fpga_mgr_dev_match);
>  	if (!mgr_dev)
>  		return ERR_PTR(-ENODEV);
> @@ -713,7 +726,7 @@ struct fpga_manager *of_fpga_mgr_get(struct device_node *node)
>  {
>  	struct device *dev;
>  
> -	dev = class_find_device_by_of_node(fpga_mgr_class, node);
> +	dev = class_find_device_by_of_node(&fpga_mgr_class, node);
>  	if (!dev)
>  		return ERR_PTR(-ENODEV);
>  
> @@ -809,7 +822,7 @@ fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *in
>  	mgr->priv = info->priv;
>  	mgr->compat_id = info->compat_id;
>  
> -	mgr->dev.class = fpga_mgr_class;
> +	mgr->dev.class = &fpga_mgr_class;
>  	mgr->dev.groups = mops->groups;
>  	mgr->dev.parent = parent;
>  	mgr->dev.of_node = parent->of_node;
> @@ -959,31 +972,16 @@ devm_fpga_mgr_register(struct device *parent, const char *name,
>  }
>  EXPORT_SYMBOL_GPL(devm_fpga_mgr_register);
>  
> -static void fpga_mgr_dev_release(struct device *dev)
> -{
> -	struct fpga_manager *mgr = to_fpga_manager(dev);
> -
> -	ida_free(&fpga_mgr_ida, mgr->dev.id);
> -	kfree(mgr);
> -}
> -
>  static int __init fpga_mgr_class_init(void)
>  {
>  	pr_info("FPGA manager framework\n");
>  
> -	fpga_mgr_class = class_create("fpga_manager");
> -	if (IS_ERR(fpga_mgr_class))
> -		return PTR_ERR(fpga_mgr_class);
> -
> -	fpga_mgr_class->dev_groups = fpga_mgr_groups;
> -	fpga_mgr_class->dev_release = fpga_mgr_dev_release;
> -
> -	return 0;
> +	return class_register(&fpga_mgr_class);
>  }
>  
>  static void __exit fpga_mgr_class_exit(void)
>  {
> -	class_destroy(fpga_mgr_class);
> +	class_unregister(&fpga_mgr_class);
>  	ida_destroy(&fpga_mgr_ida);
>  }
>  
> -- 
> 2.34.1
>
diff mbox series

Patch

diff --git a/drivers/fpga/fpga-mgr.c b/drivers/fpga/fpga-mgr.c
index eb583f86a0b9..cd5b7371495b 100644
--- a/drivers/fpga/fpga-mgr.c
+++ b/drivers/fpga/fpga-mgr.c
@@ -19,7 +19,6 @@ 
 #include <linux/highmem.h>
 
 static DEFINE_IDA(fpga_mgr_ida);
-static struct class *fpga_mgr_class;
 
 struct fpga_mgr_devres {
 	struct fpga_manager *mgr;
@@ -664,6 +663,20 @@  static struct attribute *fpga_mgr_attrs[] = {
 };
 ATTRIBUTE_GROUPS(fpga_mgr);
 
+static void fpga_mgr_dev_release(struct device *dev)
+{
+	struct fpga_manager *mgr = to_fpga_manager(dev);
+
+	ida_free(&fpga_mgr_ida, mgr->dev.id);
+	kfree(mgr);
+}
+
+static const struct class fpga_mgr_class = {
+	.name = "fpga_manager",
+	.dev_groups = fpga_mgr_groups,
+	.dev_release = fpga_mgr_dev_release,
+};
+
 static struct fpga_manager *__fpga_mgr_get(struct device *dev)
 {
 	struct fpga_manager *mgr;
@@ -693,7 +706,7 @@  static int fpga_mgr_dev_match(struct device *dev, const void *data)
  */
 struct fpga_manager *fpga_mgr_get(struct device *dev)
 {
-	struct device *mgr_dev = class_find_device(fpga_mgr_class, NULL, dev,
+	struct device *mgr_dev = class_find_device(&fpga_mgr_class, NULL, dev,
 						   fpga_mgr_dev_match);
 	if (!mgr_dev)
 		return ERR_PTR(-ENODEV);
@@ -713,7 +726,7 @@  struct fpga_manager *of_fpga_mgr_get(struct device_node *node)
 {
 	struct device *dev;
 
-	dev = class_find_device_by_of_node(fpga_mgr_class, node);
+	dev = class_find_device_by_of_node(&fpga_mgr_class, node);
 	if (!dev)
 		return ERR_PTR(-ENODEV);
 
@@ -809,7 +822,7 @@  fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *in
 	mgr->priv = info->priv;
 	mgr->compat_id = info->compat_id;
 
-	mgr->dev.class = fpga_mgr_class;
+	mgr->dev.class = &fpga_mgr_class;
 	mgr->dev.groups = mops->groups;
 	mgr->dev.parent = parent;
 	mgr->dev.of_node = parent->of_node;
@@ -959,31 +972,16 @@  devm_fpga_mgr_register(struct device *parent, const char *name,
 }
 EXPORT_SYMBOL_GPL(devm_fpga_mgr_register);
 
-static void fpga_mgr_dev_release(struct device *dev)
-{
-	struct fpga_manager *mgr = to_fpga_manager(dev);
-
-	ida_free(&fpga_mgr_ida, mgr->dev.id);
-	kfree(mgr);
-}
-
 static int __init fpga_mgr_class_init(void)
 {
 	pr_info("FPGA manager framework\n");
 
-	fpga_mgr_class = class_create("fpga_manager");
-	if (IS_ERR(fpga_mgr_class))
-		return PTR_ERR(fpga_mgr_class);
-
-	fpga_mgr_class->dev_groups = fpga_mgr_groups;
-	fpga_mgr_class->dev_release = fpga_mgr_dev_release;
-
-	return 0;
+	return class_register(&fpga_mgr_class);
 }
 
 static void __exit fpga_mgr_class_exit(void)
 {
-	class_destroy(fpga_mgr_class);
+	class_unregister(&fpga_mgr_class);
 	ida_destroy(&fpga_mgr_ida);
 }