Skip to contentSkip to conversation

of: guard pointers to key OF nodes with an #ifdef

PATCH Applied Bartosz Golaszewski 3 participants 4 messages Jan 15 – Jan 16 Device tree

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 of: guard pointers to key OF nodes with an #ifdef

We declare the pointers to some key OF-nodes unconditionally in linux/of.h but only export them with CONFIG_OF=y. If anyone uses them in code built without devicetree support enabled, the problem will only come to light at link-time. Add an #ifdef guard to catch it at compile-time.

---
 include/linux/of.h | 2 ++
 1 file changed, 2 insertions(+)
include/linux/of.h +2 -0
index 9bbdcf25a2b4..bc57dff819f8 100644
@@ -138,10 +138,12 @@ static inline void of_node_put(struct device_node *node) { }
 DEFINE_FREE(device_node, struct device_node *, if (_T) of_node_put(_T))
 
 /* Pointer for first entry in chain of all nodes. */
+#if IS_ENABLED(CONFIG_OF)
 extern struct device_node *of_root;
 extern struct device_node *of_chosen;
 extern struct device_node *of_aliases;
 extern struct device_node *of_stdout;
+#endif
 
 /*
  * struct device_node flag descriptions
Signature · 1 line
2.47.3
#1 lore raw
kernel test robot noticed the following build errors:

Hi Bartosz,

kernel test robot noticed the following build errors:

[auto build test ERROR on robh/for-next]
[also build test ERROR on linus/master v6.19-rc5 next-20260115] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Bartosz-Golaszewski/of-guard-pointers-to-key-OF-nodes-with-an-ifdef/20260115-171949
base:   https://git.kernel.org/pub/scm/linux/kernel/git/robh/linux.git for-next
patch link:    https://lore.kernel.org/r/20260115091839.8206-1-bartosz.golaszewski%40oss.qualcomm.com
patch subject: [PATCH] of: guard pointers to key OF nodes with an #ifdef
config: sh-randconfig-r072-20260115 (https://download.01.org/0day-ci/archive/20260115/[email protected]/config)
compiler: sh4-linux-gcc (GCC) 14.3.0
smatch version: v0.5.0-8985-g2614ff1a
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260115/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

drivers/clocksource/timer-integrator-ap.c: In function 'integrator_ap_timer_init_of':

>> drivers/clocksource/timer-integrator-ap.c:181:39: error: 'of_aliases' undeclared (first use in this function)

181 | err = of_property_read_string(of_aliases,
| ^~~~~~~~~~
drivers/clocksource/timer-integrator-ap.c:181:39: note: each undeclared identifier is reported only once for each function it appears in
--
drivers/soc/imx/soc-imx8m.c: In function 'imx8m_soc_probe':

>> drivers/soc/imx/soc-imx8m.c:243:39: error: 'of_root' undeclared (first use in this function); did you mean 'ma_root'?
     243 |         ret = of_property_read_string(of_root, "model", &soc_dev_attr->machine);
         |                                       ^~~~~~~
         |                                       ma_root
   drivers/soc/imx/soc-imx8m.c:243:39: note: each undeclared identifier is reported only once for each function it appears in
--
   drivers/soc/imx/soc-imx9.c: In function 'imx9_soc_probe':
>> drivers/soc/imx/soc-imx9.c:32:39: error: 'of_root' undeclared (first use in this function); did you mean 'ma_root'?
      32 |         err = of_property_read_string(of_root, "model", &attr->machine);
         |                                       ^~~~~~~
         |                                       ma_root

drivers/soc/imx/soc-imx9.c:32:39: note: each undeclared identifier is reported only once for each function it appears in

vim +/of_aliases +181 drivers/clocksource/timer-integrator-ap.c

beb5818bd01295 Linus Walleij  2014-10-15  157  
76804d052316d3 Daniel Lezcano 2016-06-06  158  static int __init integrator_ap_timer_init_of(struct device_node *node)
beb5818bd01295 Linus Walleij  2014-10-15  159  {
beb5818bd01295 Linus Walleij  2014-10-15  160  	const char *path;
beb5818bd01295 Linus Walleij  2014-10-15  161  	void __iomem *base;
beb5818bd01295 Linus Walleij  2014-10-15  162  	int err;
beb5818bd01295 Linus Walleij  2014-10-15  163  	int irq;
beb5818bd01295 Linus Walleij  2014-10-15  164  	struct clk *clk;
beb5818bd01295 Linus Walleij  2014-10-15  165  	unsigned long rate;
5eb73c83117111 Yangtao Li     2018-11-25  166  	struct device_node *alias_node;
beb5818bd01295 Linus Walleij  2014-10-15  167  
beb5818bd01295 Linus Walleij  2014-10-15  168  	base = of_io_request_and_map(node, 0, "integrator-timer");
bd580e7ed4add8 Maxime Ripard  2015-05-02  169  	if (IS_ERR(base))
76804d052316d3 Daniel Lezcano 2016-06-06  170  		return PTR_ERR(base);
beb5818bd01295 Linus Walleij  2014-10-15  171  
beb5818bd01295 Linus Walleij  2014-10-15  172  	clk = of_clk_get(node, 0);
beb5818bd01295 Linus Walleij  2014-10-15  173  	if (IS_ERR(clk)) {
2a4849d2674b96 Rob Herring    2018-08-27  174  		pr_err("No clock for %pOFn\n", node);
76804d052316d3 Daniel Lezcano 2016-06-06  175  		return PTR_ERR(clk);
beb5818bd01295 Linus Walleij  2014-10-15  176  	}
beb5818bd01295 Linus Walleij  2014-10-15  177  	clk_prepare_enable(clk);
beb5818bd01295 Linus Walleij  2014-10-15  178  	rate = clk_get_rate(clk);
beb5818bd01295 Linus Walleij  2014-10-15  179  	writel(0, base + TIMER_CTRL);
beb5818bd01295 Linus Walleij  2014-10-15  180  
beb5818bd01295 Linus Walleij  2014-10-15 @181  	err = of_property_read_string(of_aliases,
beb5818bd01295 Linus Walleij  2014-10-15  182  				"arm,timer-primary", &path);
76804d052316d3 Daniel Lezcano 2016-06-06  183  	if (err) {
ac9ce6d1a0cc29 Rafał Miłecki  2017-03-09  184  		pr_warn("Failed to read property\n");
76804d052316d3 Daniel Lezcano 2016-06-06  185  		return err;
76804d052316d3 Daniel Lezcano 2016-06-06  186  	}
76804d052316d3 Daniel Lezcano 2016-06-06  187  
5eb73c83117111 Yangtao Li     2018-11-25  188  	alias_node = of_find_node_by_path(path);
5eb73c83117111 Yangtao Li     2018-11-25  189  
5eb73c83117111 Yangtao Li     2018-11-25  190  	/*
5eb73c83117111 Yangtao Li     2018-11-25  191  	 * The pointer is used as an identifier not as a pointer, we
5eb73c83117111 Yangtao Li     2018-11-25  192  	 * can drop the refcount on the of__node immediately after
5eb73c83117111 Yangtao Li     2018-11-25  193  	 * getting it.
5eb73c83117111 Yangtao Li     2018-11-25  194  	 */
5eb73c83117111 Yangtao Li     2018-11-25  195  	of_node_put(alias_node);
5eb73c83117111 Yangtao Li     2018-11-25  196  
5eb73c83117111 Yangtao Li     2018-11-25  197  	if (node == alias_node)
5eb73c83117111 Yangtao Li     2018-11-25  198  		/* The primary timer lacks IRQ, use as clocksource */
5eb73c83117111 Yangtao Li     2018-11-25  199  		return integrator_clocksource_init(rate, base);
76804d052316d3 Daniel Lezcano 2016-06-06  200  
beb5818bd01295 Linus Walleij  2014-10-15  201  	err = of_property_read_string(of_aliases,
beb5818bd01295 Linus Walleij  2014-10-15  202  				"arm,timer-secondary", &path);
76804d052316d3 Daniel Lezcano 2016-06-06  203  	if (err) {
ac9ce6d1a0cc29 Rafał Miłecki  2017-03-09  204  		pr_warn("Failed to read property\n");
76804d052316d3 Daniel Lezcano 2016-06-06  205  		return err;
76804d052316d3 Daniel Lezcano 2016-06-06  206  	}
76804d052316d3 Daniel Lezcano 2016-06-06  207  
5eb73c83117111 Yangtao Li     2018-11-25  208  	alias_node = of_find_node_by_path(path);
76804d052316d3 Daniel Lezcano 2016-06-06  209  
5eb73c83117111 Yangtao Li     2018-11-25  210  	of_node_put(alias_node);
beb5818bd01295 Linus Walleij  2014-10-15  211  
5eb73c83117111 Yangtao Li     2018-11-25  212  	if (node == alias_node) {
beb5818bd01295 Linus Walleij  2014-10-15  213  		/* The secondary timer will drive the clock event */
beb5818bd01295 Linus Walleij  2014-10-15  214  		irq = irq_of_parse_and_map(node, 0);
76804d052316d3 Daniel Lezcano 2016-06-06  215  		return integrator_clockevent_init(rate, base, irq);
beb5818bd01295 Linus Walleij  2014-10-15  216  	}
beb5818bd01295 Linus Walleij  2014-10-15  217  
beb5818bd01295 Linus Walleij  2014-10-15  218  	pr_info("Timer @%p unused\n", base);
beb5818bd01295 Linus Walleij  2014-10-15  219  	clk_disable_unprepare(clk);
76804d052316d3 Daniel Lezcano 2016-06-06  220  
76804d052316d3 Daniel Lezcano 2016-06-06  221  	return 0;
beb5818bd01295 Linus Walleij  2014-10-15  222  }
beb5818bd01295 Linus Walleij  2014-10-15  223  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
maintainer Applied
#2 lore raw
In reply to Bartosz Golaszewski
Quoted 10 lines · Bartosz Golaszewski

We declare the pointers to some key OF-nodes unconditionally in linux/of.h but only export them with CONFIG_OF=y. If anyone uses them in code built without devicetree support enabled, the problem will only come to light at link-time. Add an #ifdef guard to catch it at compile-time.

---
 include/linux/of.h | 2 ++
 1 file changed, 2 insertions(+)

Applied, thanks!

maintainer
#3 lore raw
Quoted 8 lines · Rob Herring (Arm)
Quoted 5 lines · Bartosz Golaszewski

We declare the pointers to some key OF-nodes unconditionally in linux/of.h but only export them with CONFIG_OF=y. If anyone uses them in code built without devicetree support enabled, the problem will only come to light at link-time. Add an #ifdef guard to catch it at compile-time.

Quoted 7 lines
Quoted 5 lines
---
 include/linux/of.h | 2 ++
 1 file changed, 2 insertions(+)

Applied, thanks!

Err, and dropped.

BTW, my ideal fix for this is to eliminate any users of these variables.

Rob