Skip to contentSkip to conversation

of: Drop redundant check in linker section OF match table

PATCH In review Mukesh Ojha 2 participants 3 messages Mar 20, 2019 – Mar 21, 2019 Device tree

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 of: Drop redundant check in linker section OF match table

Existing check of fn against NULL inside OF match table is redundant. Remove the check.

Cc Rob Herring <[email protected]>
Cc Frank Rowand <[email protected]>
Cc Pantelis Antoniou <[email protected]>
---
 include/linux/of.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/of.h +2 -2
index e240992..b86c00a 100644
@@ -1283,13 +1283,13 @@ static inline int of_get_available_child_count(const struct device_node *np)
 	static const struct of_device_id __of_table_##name		\
 		__used __section(__##table##_of_table)			\
 		 = { .compatible = compat,				\
-		     .data = (fn == (fn_type)NULL) ? fn : fn  }
+		     .data = fn }
 #else
 #define _OF_DECLARE(table, name, compat, fn, fn_type)			\
 	static const struct of_device_id __of_table_##name		\
 		__attribute__((unused))					\
 		 = { .compatible = compat,				\
-		     .data = (fn == (fn_type)NULL) ? fn : fn }
+		     .data = fn }
 #endif
 
 typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);
Signature · 2 lines
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center,
Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project
#1 lore raw
Quoted 23 lines · Mukesh Ojha

Existing check of fn against NULL inside OF match table is redundant. Remove the check.

Cc Rob Herring <[email protected]>
Cc Frank Rowand <[email protected]>
Cc Pantelis Antoniou <[email protected]>
---
 include/linux/of.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/of.h +1 -1
index e240992..b86c00a 100644
@@ -1283,13 +1283,13 @@ static inline int of_get_available_child_count(const struct device_node *np)
 	static const struct of_device_id __of_table_##name		\
 		__used __section(__##table##_of_table)			\
 		 = { .compatible = compat,				\
-		     .data = (fn == (fn_type)NULL) ? fn : fn  }
+		     .data = fn }

include/linux/of.h static inline int of_get_available_child_count(const struct device_node *np) · line 1295

Quoted 10 lines
include/linux/of.h +1 -1
 #else
 #define _OF_DECLARE(table, name, compat, fn, fn_type)			\
 	static const struct of_device_id __of_table_##name		\
 		__attribute__((unused))					\
 		 = { .compatible = compat,				\
-		     .data = (fn == (fn_type)NULL) ? fn : fn }
+		     .data = fn }
 #endif
 
 typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);

The check is not redundant and does serve a purpose.

The purpose is not very obvious on the surface, but it is checking that the function fn() is of the proper type.

An example of a compiler warning with a bad function type is created by applying the following patch:

drivers/of/unittest.c:62:1: warning: comparison of distinct pointer types lacks a cast [enabled by default]

Note that you need to have CONFIG_UNITTEST enabled to compile unittest.c.

Line 62 is
OF_DECLARE_1(__unittest_of_table, unittest_setup_2_bad, "unittest_compat",

---
 drivers/of/unittest.c |   17 +++++++++++++++++
 1 file changed, 17 insertions(+)
drivers/of/unittest.c +17 -0
Index: b/drivers/of/unittest.c
===================================================================
@@ -45,6 +45,23 @@ static struct unittest_results {
 	failed; \
 })
 
+struct of_device_id __unittest_of_table;
+
+static void __init unittest_setup_1_good(struct device_node *np)
+{
+}
+
+static void __init unittest_setup_2_bad(struct device_node *np_1,
+	struct device_node *np_2)
+{
+}
+
+OF_DECLARE_1(__unittest_of_table, unittest_setup_1_good, "unittest_compat",
+	unittest_setup_1_good);
+
+OF_DECLARE_1(__unittest_of_table, unittest_setup_2_bad, "unittest_compat",
+	unittest_setup_2_bad);
+
 static void __init of_unittest_find_node_by_name(void)
 {
 	struct device_node *np;
#2 lore raw
Quoted 32 lines · Frank Rowand
Quoted 31 lines · Mukesh Ojha

Existing check of fn against NULL inside OF match table is redundant. Remove the check.

Cc Rob Herring <[email protected]>
Cc Frank Rowand <[email protected]>
Cc Pantelis Antoniou <[email protected]>
---
  include/linux/of.h | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/of.h +2 -2
index e240992..b86c00a 100644
@@ -1283,13 +1283,13 @@ static inline int of_get_available_child_count(const struct device_node *np)
  	static const struct of_device_id __of_table_##name		\
  		__used __section(__##table##_of_table)			\
  		 = { .compatible = compat,				\
-		     .data = (fn == (fn_type)NULL) ? fn : fn  }
+		     .data = fn }
  #else
  #define _OF_DECLARE(table, name, compat, fn, fn_type)			\
  	static const struct of_device_id __of_table_##name		\
  		__attribute__((unused))					\
  		 = { .compatible = compat,				\
-		     .data = (fn == (fn_type)NULL) ? fn : fn }
+		     .data = fn }
  #endif
 
Quoted 6 lines

include/linux/of.h static inline int of_get_available_child_count(const struct device_node *np) · line 1295

Quoted 2 lines
include/linux/of.h +0 -0
  typedef int (*of_init_fn_2)(struct device_node *, struct device_node *);

The check is not redundant and does serve a purpose.

The purpose is not very obvious on the surface, but it is checking that the function fn() is of the proper type.

Frank,

Thanks for taking out time to explain the stuff, Yeah some miscellaneous driver can do this type of mistake.

-Mukesh

Quoted 42 lines

An example of a compiler warning with a bad function type is created by applying the following patch:

drivers/of/unittest.c:62:1: warning: comparison of distinct pointer types lacks a cast [enabled by default]

Note that you need to have CONFIG_UNITTEST enabled to compile unittest.c.

Line 62 is
OF_DECLARE_1(__unittest_of_table, unittest_setup_2_bad, "unittest_compat",

---
  drivers/of/unittest.c |   17 +++++++++++++++++
  1 file changed, 17 insertions(+)

Index: b/drivers/of/unittest.c
===================================================================

drivers/of/unittest.c +17 -0
@@ -45,6 +45,23 @@ static struct unittest_results {
  	failed; \
  })
 
+struct of_device_id __unittest_of_table;
+
+static void __init unittest_setup_1_good(struct device_node *np)
+{
+}
+
+static void __init unittest_setup_2_bad(struct device_node *np_1,
+	struct device_node *np_2)
+{
+}
+
+OF_DECLARE_1(__unittest_of_table, unittest_setup_1_good, "unittest_compat",
+	unittest_setup_1_good);
+
+OF_DECLARE_1(__unittest_of_table, unittest_setup_2_bad, "unittest_compat",
+	unittest_setup_2_bad);
+
  static void __init of_unittest_find_node_by_name(void)
  {
  	struct device_node *np;