diff mbox series

[v2,1/3] fpga: bridge: make fpga_bridge_class a static const structure

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

Commit Message

Ivan Orlov Aug. 11, 2023, 7:30 a.m. UTC
Now that the driver core allows for struct class to be in read-only
memory, move the fpga_bridge_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-bridge.c | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)

Comments

Ivan Orlov Aug. 11, 2023, 7:33 a.m. UTC | #1
On 8/11/23 11:30, Ivan Orlov wrote:
> Now that the driver core allows for struct class to be in read-only
> memory, move the fpga_bridge_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-bridge.c | 25 ++++++++++++-------------
>   1 file changed, 12 insertions(+), 13 deletions(-)
> 
> diff --git a/drivers/fpga/fpga-bridge.c b/drivers/fpga/fpga-bridge.c
> index a6c25dee9cc1..fe93549d051d 100644
> --- a/drivers/fpga/fpga-bridge.c
> +++ b/drivers/fpga/fpga-bridge.c
> @@ -14,7 +14,7 @@
>   #include <linux/spinlock.h>
>   
>   static DEFINE_IDA(fpga_bridge_ida);
> -static struct class *fpga_bridge_class;
> +static const struct class fpga_bridge_class;
>   
>   /* Lock for adding/removing bridges to linked lists*/
>   static DEFINE_SPINLOCK(bridge_list_lock);
> @@ -99,7 +99,7 @@ struct fpga_bridge *of_fpga_bridge_get(struct device_node *np,
>   {
>   	struct device *dev;
>   
> -	dev = class_find_device_by_of_node(fpga_bridge_class, np);
> +	dev = class_find_device_by_of_node(&fpga_bridge_class, np);
>   	if (!dev)
>   		return ERR_PTR(-ENODEV);
>   
> @@ -126,7 +126,7 @@ struct fpga_bridge *fpga_bridge_get(struct device *dev,
>   {
>   	struct device *bridge_dev;
>   
> -	bridge_dev = class_find_device(fpga_bridge_class, NULL, dev,
> +	bridge_dev = class_find_device(&fpga_bridge_class, NULL, dev,
>   				       fpga_bridge_dev_match);
>   	if (!bridge_dev)
>   		return ERR_PTR(-ENODEV);
> @@ -359,7 +359,7 @@ fpga_bridge_register(struct device *parent, const char *name,
>   	bridge->priv = priv;
>   
>   	bridge->dev.groups = br_ops->groups;
> -	bridge->dev.class = fpga_bridge_class;
> +	bridge->dev.class = &fpga_bridge_class;
>   	bridge->dev.parent = parent;
>   	bridge->dev.of_node = parent->of_node;
>   	bridge->dev.id = id;
> @@ -415,21 +415,20 @@ static void fpga_bridge_dev_release(struct device *dev)
>   	kfree(bridge);
>   }
>   
> +static const struct class fpga_bridge_class = {
> +	.name = "fpga_bridge",
> +	.dev_groups = fpga_bridge_groups,
> +	.dev_release = fpga_bridge_dev_release,
> +};
> +
>   static int __init fpga_bridge_dev_init(void)
>   {
> -	fpga_bridge_class = class_create("fpga_bridge");
> -	if (IS_ERR(fpga_bridge_class))
> -		return PTR_ERR(fpga_bridge_class);
> -
> -	fpga_bridge_class->dev_groups = fpga_bridge_groups;
> -	fpga_bridge_class->dev_release = fpga_bridge_dev_release;
> -
> -	return 0;
> +	return class_register(&fpga_bridge_class);
>   }
>   
>   static void __exit fpga_bridge_dev_exit(void)
>   {
> -	class_destroy(fpga_bridge_class);
> +	class_unregister(&fpga_bridge_class);
>   	ida_destroy(&fpga_bridge_ida);
>   }
>   

And I forgot the changelog here.

V1 -> V2:
- Add forward declaration of fpga_bridge_class
- Move definition of fpga_bridge_class to more logical place
Xu Yilun Aug. 11, 2023, 3:09 p.m. UTC | #2
On 2023-08-11 at 11:30:41 +0400, Ivan Orlov wrote:
> Now that the driver core allows for struct class to be in read-only
> memory, move the fpga_bridge_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>

Acked-by: Xu Yilun <yilun.xu@intel.com>

Applied this series.
diff mbox series

Patch

diff --git a/drivers/fpga/fpga-bridge.c b/drivers/fpga/fpga-bridge.c
index a6c25dee9cc1..fe93549d051d 100644
--- a/drivers/fpga/fpga-bridge.c
+++ b/drivers/fpga/fpga-bridge.c
@@ -14,7 +14,7 @@ 
 #include <linux/spinlock.h>
 
 static DEFINE_IDA(fpga_bridge_ida);
-static struct class *fpga_bridge_class;
+static const struct class fpga_bridge_class;
 
 /* Lock for adding/removing bridges to linked lists*/
 static DEFINE_SPINLOCK(bridge_list_lock);
@@ -99,7 +99,7 @@  struct fpga_bridge *of_fpga_bridge_get(struct device_node *np,
 {
 	struct device *dev;
 
-	dev = class_find_device_by_of_node(fpga_bridge_class, np);
+	dev = class_find_device_by_of_node(&fpga_bridge_class, np);
 	if (!dev)
 		return ERR_PTR(-ENODEV);
 
@@ -126,7 +126,7 @@  struct fpga_bridge *fpga_bridge_get(struct device *dev,
 {
 	struct device *bridge_dev;
 
-	bridge_dev = class_find_device(fpga_bridge_class, NULL, dev,
+	bridge_dev = class_find_device(&fpga_bridge_class, NULL, dev,
 				       fpga_bridge_dev_match);
 	if (!bridge_dev)
 		return ERR_PTR(-ENODEV);
@@ -359,7 +359,7 @@  fpga_bridge_register(struct device *parent, const char *name,
 	bridge->priv = priv;
 
 	bridge->dev.groups = br_ops->groups;
-	bridge->dev.class = fpga_bridge_class;
+	bridge->dev.class = &fpga_bridge_class;
 	bridge->dev.parent = parent;
 	bridge->dev.of_node = parent->of_node;
 	bridge->dev.id = id;
@@ -415,21 +415,20 @@  static void fpga_bridge_dev_release(struct device *dev)
 	kfree(bridge);
 }
 
+static const struct class fpga_bridge_class = {
+	.name = "fpga_bridge",
+	.dev_groups = fpga_bridge_groups,
+	.dev_release = fpga_bridge_dev_release,
+};
+
 static int __init fpga_bridge_dev_init(void)
 {
-	fpga_bridge_class = class_create("fpga_bridge");
-	if (IS_ERR(fpga_bridge_class))
-		return PTR_ERR(fpga_bridge_class);
-
-	fpga_bridge_class->dev_groups = fpga_bridge_groups;
-	fpga_bridge_class->dev_release = fpga_bridge_dev_release;
-
-	return 0;
+	return class_register(&fpga_bridge_class);
 }
 
 static void __exit fpga_bridge_dev_exit(void)
 {
-	class_destroy(fpga_bridge_class);
+	class_unregister(&fpga_bridge_class);
 	ida_destroy(&fpga_bridge_ida);
 }