Skip to contentSkip to conversation

fpga: bridge: make fpga_bridge_class a static const structure

PATCH In review Ivan Orlov 2 participants 7 messages Aug 11, 2023 FPGA manager framework

lore
Share & tools
lore

Ivan Orlov

author
#0 lore raw

PATCH v2 1/3 fpga: bridge: make fpga_bridge_class a static const structure

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.

---
 drivers/fpga/fpga-bridge.c | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)
drivers/fpga/fpga-bridge.c +12 -13
index a6c25dee9cc1..fe93549d051d 100644
@@ -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);
 }
 
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 2/3 fpga: fpga-mgr: make fpga_mgr_class a static const structure

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.

---
 drivers/fpga/fpga-mgr.c | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)
drivers/fpga/fpga-mgr.c +12 -13
index eb583f86a0b9..06651389c592 100644
@@ -19,7 +19,7 @@
 #include <linux/highmem.h>
 
 static DEFINE_IDA(fpga_mgr_ida);
-static struct class *fpga_mgr_class;
+static const struct class fpga_mgr_class;
 
 struct fpga_mgr_devres {
 	struct fpga_manager *mgr;
@@ -693,7 +693,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 +713,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 +809,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;
@@ -967,23 +967,22 @@ static void fpga_mgr_dev_release(struct device *dev)
 	kfree(mgr);
 }
 
+static const struct class fpga_mgr_class = {
+	.name = "fpga_manager",
+	.dev_groups = fpga_mgr_groups,
+	.dev_release = fpga_mgr_dev_release,
+};
+
 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);
 }
 
Signature · 1 line
2.34.1
author
#2 lore raw

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

Quoted 73 lines · Ivan Orlov

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.

---
  drivers/fpga/fpga-mgr.c | 25 ++++++++++++-------------
  1 file changed, 12 insertions(+), 13 deletions(-)
drivers/fpga/fpga-mgr.c +10 -12
index eb583f86a0b9..06651389c592 100644
@@ -19,7 +19,7 @@
  #include <linux/highmem.h>
 
  static DEFINE_IDA(fpga_mgr_ida);
-static struct class *fpga_mgr_class;
+static const struct class fpga_mgr_class;
 
  struct fpga_mgr_devres {
  	struct fpga_manager *mgr;
@@ -693,7 +693,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 +713,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 +809,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;
@@ -967,23 +967,22 @@ static void fpga_mgr_dev_release(struct device *dev)
  	kfree(mgr);
  }
 
+static const struct class fpga_mgr_class = {
+	.name = "fpga_manager",
+	.dev_groups = fpga_mgr_groups,
+	.dev_release = fpga_mgr_dev_release,
+};
+
  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;

drivers/fpga/fpga-mgr.c static void fpga_mgr_dev_release(struct device *dev) · line 988

Quoted 10 lines
drivers/fpga/fpga-mgr.c +2 -1
+	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);
  }
 

And I forgot the changelog here.

V1 -> V2:
- Add forward declaration of fpga_mgr_class
- Move definition of fpga_mgr_class to more logical place

author
#3 lore raw

PATCH v2 3/3 fpga: region: make fpga_region_class a static const structure

In reply to Ivan Orlov

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

---
 drivers/fpga/fpga-region.c | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)
drivers/fpga/fpga-region.c +11 -12
index ccf6fdab1360..6abd8a154047 100644
@@ -16,7 +16,7 @@
 #include <linux/spinlock.h>
 
 static DEFINE_IDA(fpga_region_ida);
-static struct class *fpga_region_class;
+static const struct class fpga_region_class;
 
 struct fpga_region *
 fpga_region_class_find(struct device *start, const void *data,
@@ -24,7 +24,7 @@ fpga_region_class_find(struct device *start, const void *data,
 {
 	struct device *dev;
 
-	dev = class_find_device(fpga_region_class, start, data, match);
+	dev = class_find_device(&fpga_region_class, start, data, match);
 	if (!dev)
 		return NULL;
 
@@ -216,7 +216,7 @@ fpga_region_register_full(struct device *parent, const struct fpga_region_info *
 	mutex_init(&region->mutex);
 	INIT_LIST_HEAD(&region->bridge_list);
 
-	region->dev.class = fpga_region_class;
+	region->dev.class = &fpga_region_class;
 	region->dev.parent = parent;
 	region->dev.of_node = parent->of_node;
 	region->dev.id = id;
@@ -287,25 +287,24 @@ static void fpga_region_dev_release(struct device *dev)
 	kfree(region);
 }
 
+static const struct class fpga_region_class = {
+	.name = "fpga_region",
+	.dev_groups = fpga_region_groups,
+	.dev_release = fpga_region_dev_release,
+};
+
 /**
  * fpga_region_init - init function for fpga_region class
  * Creates the fpga_region class and registers a reconfig notifier.
  */
 static int __init fpga_region_init(void)
 {
-	fpga_region_class = class_create("fpga_region");
-	if (IS_ERR(fpga_region_class))
-		return PTR_ERR(fpga_region_class);
-
-	fpga_region_class->dev_groups = fpga_region_groups;
-	fpga_region_class->dev_release = fpga_region_dev_release;
-
-	return 0;
+	return class_register(&fpga_region_class);
 }
 
 static void __exit fpga_region_exit(void)
 {
-	class_destroy(fpga_region_class);
+	class_unregister(&fpga_region_class);
 	ida_destroy(&fpga_region_ida);
 }
 
Signature · 1 line
2.34.1
author
#4 lore raw

Re: [PATCH v2 3/3] fpga: region: make fpga_region_class a static const structure

Quoted 66 lines · Ivan Orlov

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

---
  drivers/fpga/fpga-region.c | 23 +++++++++++------------
  1 file changed, 11 insertions(+), 12 deletions(-)
drivers/fpga/fpga-region.c +9 -11
index ccf6fdab1360..6abd8a154047 100644
@@ -16,7 +16,7 @@
  #include <linux/spinlock.h>
 
  static DEFINE_IDA(fpga_region_ida);
-static struct class *fpga_region_class;
+static const struct class fpga_region_class;
 
  struct fpga_region *
  fpga_region_class_find(struct device *start, const void *data,
@@ -24,7 +24,7 @@ fpga_region_class_find(struct device *start, const void *data,
  {
  	struct device *dev;
 
-	dev = class_find_device(fpga_region_class, start, data, match);
+	dev = class_find_device(&fpga_region_class, start, data, match);
  	if (!dev)
  		return NULL;
 
@@ -216,7 +216,7 @@ fpga_region_register_full(struct device *parent, const struct fpga_region_info *
  	mutex_init(&region->mutex);
  	INIT_LIST_HEAD(&region->bridge_list);
 
-	region->dev.class = fpga_region_class;
+	region->dev.class = &fpga_region_class;
  	region->dev.parent = parent;
  	region->dev.of_node = parent->of_node;
  	region->dev.id = id;
@@ -287,25 +287,24 @@ static void fpga_region_dev_release(struct device *dev)
  	kfree(region);
  }
 
+static const struct class fpga_region_class = {
+	.name = "fpga_region",
+	.dev_groups = fpga_region_groups,
+	.dev_release = fpga_region_dev_release,
+};
+
  /**
   * fpga_region_init - init function for fpga_region class
   * Creates the fpga_region class and registers a reconfig notifier.
   */
  static int __init fpga_region_init(void)
  {
-	fpga_region_class = class_create("fpga_region");
-	if (IS_ERR(fpga_region_class))
-		return PTR_ERR(fpga_region_class);
-
-	fpga_region_class->dev_groups = fpga_region_groups;
-	fpga_region_class->dev_release = fpga_region_dev_release;
-
-	return 0;

drivers/fpga/fpga-region.c static void fpga_region_dev_release(struct device *dev) · line 310

Quoted 10 lines
drivers/fpga/fpga-region.c +2 -1
+	return class_register(&fpga_region_class);
  }
 
  static void __exit fpga_region_exit(void)
  {
-	class_destroy(fpga_region_class);
+	class_unregister(&fpga_region_class);
  	ida_destroy(&fpga_region_ida);
  }
 

And I forgot the changelog here.

V1 -> V2:
- Add forward declaration of fpga_region_class
- Move definition of fpga_region_class to more logical place

author
#5 lore raw
In reply to Ivan Orlov
Quoted 72 lines · Ivan Orlov

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.

---
  drivers/fpga/fpga-bridge.c | 25 ++++++++++++-------------
  1 file changed, 12 insertions(+), 13 deletions(-)
drivers/fpga/fpga-bridge.c +10 -12
index a6c25dee9cc1..fe93549d051d 100644
@@ -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;

drivers/fpga/fpga-bridge.c static void fpga_bridge_dev_release(struct device *dev) · line 434

Quoted 10 lines
drivers/fpga/fpga-bridge.c +2 -1
+	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

maintainer Acked-by on v2 1/3
#6 lore raw
In reply to Ivan Orlov
Quoted 7 lines · Ivan Orlov

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.

Applied this series.