Skip to contentSkip to conversation

of: Build warn for missing fn() in _OF_DECLARE

PATCH Applied Liya Huang 3 participants 5 messages Apr 17, 2025 – Apr 23, 2025 Device tree

lore
Share & tools
lore

Liya Huang

author
#0 lore raw

PATCH 1/1 of: Build warn for missing fn() in _OF_DECLARE

The function pointer fn() in _OF_DECLARE macro might be NULL. For example, in __reserved_mem_init_node(), only non-NULL cases are handled, and NULL function pointers are ignored.

This patch introduces a check to handle cases where fn() is NULL. If fn() is found to be NULL, a warning is issued during compilation to notify developers about the missing function pointer.

---
The function pointer fn() in _OF_DECLARE macro might be NULL. For example, in __reserved_mem_init_node(), only non-NULL cases are handled, and NULL function pointers are ignored.

This patch introduces a check to handle cases where fn() is NULL. If fn() is found to be NULL, a warning is issued during compilation to notify developers about the missing function pointer.

---
 include/linux/of.h | 2 ++
 1 file changed, 2 insertions(+)
include/linux/of.h +2 -0
index a62154aeda1b6a600c2b155ac486c0e0b56e0bf2..99d1d553e65b7970a3ecb5158774ca5185f297a0 100644
@@ -1523,6 +1523,7 @@ static inline int of_get_available_child_count(const struct device_node *np)
 }
 
 #define _OF_DECLARE_STUB(table, name, compat, fn, fn_type)		\
+	static_assert((fn) != NULL);	\
 	static const struct of_device_id __of_table_##name		\
 		__attribute__((unused))					\
 		 = { .compatible = compat,				\
@@ -1530,6 +1531,7 @@ static inline int of_get_available_child_count(const struct device_node *np)
 
 #if defined(CONFIG_OF) && !defined(MODULE)
 #define _OF_DECLARE(table, name, compat, fn, fn_type)			\
+	static_assert((fn) != NULL);	\
 	static const struct of_device_id __of_table_##name		\
 		__used __section("__" #table "_of_table")		\
 		__aligned(__alignof__(struct of_device_id))		\

---
base-commit: 8ffd015db85fea3e15a77027fda6c02ced4d2444
change-id: 20250417-_of_declare-ac80b59d952d

Best regards,

Signature · 1 line
Liya Huang <[email protected]>
maintainer Applied
#1 lore raw
Quoted 17 lines · Liya Huang

The function pointer fn() in _OF_DECLARE macro might be NULL. For example, in __reserved_mem_init_node(), only non-NULL cases are handled, and NULL function pointers are ignored.

This patch introduces a check to handle cases where fn() is NULL. If fn() is found to be NULL, a warning is issued during compilation to notify developers about the missing function pointer.

---
The function pointer fn() in _OF_DECLARE macro might be NULL. For example, in __reserved_mem_init_node(), only non-NULL cases are handled, and NULL function pointers are ignored.

This patch introduces a check to handle cases where fn() is NULL. If fn() is found to be NULL, a warning is issued during compilation to notify developers about the missing function pointer.

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

Applied, thanks!

#2 lore raw
In reply to Liya Huang
Quoted 8 lines · Liya Huang

The function pointer fn() in _OF_DECLARE macro might be NULL. For example, in __reserved_mem_init_node(), only non-NULL cases are handled, and NULL function pointers are ignored.

This patch introduces a check to handle cases where fn() is NULL. If fn() is found to be NULL, a warning is issued during compilation to notify developers about the missing function pointer.

Quoted 8 lines

---
The function pointer fn() in _OF_DECLARE macro might be NULL. For example, in __reserved_mem_init_node(), only non-NULL cases are handled, and NULL function pointers are ignored.

This patch introduces a check to handle cases where fn() is NULL. If fn() is found to be NULL, a warning is issued during compilation to notify developers about the missing function pointer.

This patch in -next appears to be responsible for syzbot complaining about build errors for some configs:

"
kernel/dma/coherent.c:410:1: error: static assertion expression is not an integral constant expression kernel/dma/contiguous.c:497:1: error: static assertion expression is not an integral constant expression "

https://lore.kernel.org/linux-iommu/[email protected]/

Also on closer inspection, just outside the diff context we still seem to be explicitly anticipating fn being NULL with:

	.data = (fn == (fn_type)NULL) ? fn : fn

so something doesn't seem quite right...

Thanks,
Robin.

Quoted 33 lines
---
  include/linux/of.h | 2 ++
  1 file changed, 2 insertions(+)
include/linux/of.h +2 -1
index a62154aeda1b6a600c2b155ac486c0e0b56e0bf2..99d1d553e65b7970a3ecb5158774ca5185f297a0 100644
@@ -1523,6 +1523,7 @@ static inline int of_get_available_child_count(const struct device_node *np)
  }
 
  #define _OF_DECLARE_STUB(table, name, compat, fn, fn_type)		\
+	static_assert((fn) != NULL);	\
  	static const struct of_device_id __of_table_##name		\
  		__attribute__((unused))					\
  		 = { .compatible = compat,				\
@@ -1530,6 +1531,7 @@ static inline int of_get_available_child_count(const struct device_node *np)
 
  #if defined(CONFIG_OF) && !defined(MODULE)
  #define _OF_DECLARE(table, name, compat, fn, fn_type)			\
+	static_assert((fn) != NULL);	\
  	static const struct of_device_id __of_table_##name		\
  		__used __section("__" #table "_of_table")		\
  		__aligned(__alignof__(struct of_device_id))		\
 
---

base-commit: 8ffd015db85fea3e15a77027fda6c02ced4d2444
change-id: 20250417-_of_declare-ac80b59d952d

Best regards,

#3 lore raw
Quoted 33 lines · Liya Huang
Quoted 8 lines

The function pointer fn() in _OF_DECLARE macro might be NULL. For example, in __reserved_mem_init_node(), only non-NULL cases are handled, and NULL function pointers are ignored.

This patch introduces a check to handle cases where fn() is NULL. If fn() is found to be NULL, a warning is issued during compilation to notify developers about the missing function pointer.

Quoted 8 lines

---
The function pointer fn() in _OF_DECLARE macro might be NULL. For example, in __reserved_mem_init_node(), only non-NULL cases are handled, and NULL function pointers are ignored.

This patch introduces a check to handle cases where fn() is NULL. If fn() is found to be NULL, a warning is issued during compilation to notify developers about the missing function pointer.

This patch in -next appears to be responsible for syzbot complaining about build errors for some configs:

"
kernel/dma/coherent.c:410:1: error: static assertion expression is not an integral constant expression kernel/dma/contiguous.c:497:1: error: static assertion expression is not an integral constant expression
"

https://lore.kernel.org/linux-iommu/[email protected]/

Also on closer inspection, just outside the diff context we still seem to be explicitly anticipating fn being NULL with:

Quoted 6 lines
	.data = (fn == (fn_type)NULL) ? fn : fn

so something doesn't seem quite right...

Thanks,
Robin.

I tested this patch, and it compiled successfully with GCC but failed with Clang.I couldn't find a better way to consistently check for null function pointers during compilation across these two compilers.
I even tried using the method of preventing negative array indexing, but it failed to compile with GCC instead.
Perhaps it would be better to abandon this patch. :(

--
Thanks,
Liya Huang <[email protected]>

maintainer
#4 lore raw
In reply to Robin Murphy
Quoted 22 lines · Robin Murphy
Quoted 8 lines · Liya Huang

The function pointer fn() in _OF_DECLARE macro might be NULL. For example, in __reserved_mem_init_node(), only non-NULL cases are handled, and NULL function pointers are ignored.

This patch introduces a check to handle cases where fn() is NULL. If fn() is found to be NULL, a warning is issued during compilation to notify developers about the missing function pointer.

Quoted 8 lines

---
The function pointer fn() in _OF_DECLARE macro might be NULL. For example, in __reserved_mem_init_node(), only non-NULL cases are handled, and NULL function pointers are ignored.

This patch introduces a check to handle cases where fn() is NULL. If fn() is found to be NULL, a warning is issued during compilation to notify developers about the missing function pointer.

This patch in -next appears to be responsible for syzbot complaining about build errors for some configs:

Quoted 8 lines

"
kernel/dma/coherent.c:410:1: error: static assertion expression is not an integral constant expression kernel/dma/contiguous.c:497:1: error: static assertion expression is not an integral constant expression
"

https://lore.kernel.org/linux-iommu/[email protected]/

Humm, doesn't seem to repro for me with clang-19.

Quoted 5 lines

Also on closer inspection, just outside the diff context we still seem to be explicitly anticipating fn being NULL with:

        .data = (fn == (fn_type)NULL) ? fn : fn

No, that is checking that the function parameters match the defined type. If fn's type doesn't match fn_type, then you get a compiler error.

Anyway, dropping it for now.

Rob