[PATCH] of: reserved-mem: Warn for missing initfn in __reservedmem_of_table

Liya Huang posted 1 patch 8 months, 1 week ago
drivers/of/of_reserved_mem.c | 5 +++++
1 file changed, 5 insertions(+)
[PATCH] of: reserved-mem: Warn for missing initfn in __reservedmem_of_table
Posted by Liya Huang 8 months, 1 week ago
For the data in __reservedmem_of_table, its function pointer initfn might
be NULL. However, __reserved_mem_init_node() only considers non-NULL cases
and ignores NULL function pointers.

Therefore, a check for the possibility of initfn being NULL has been added
here, along with skipping the initfn() and issuing a warning.

To: Rob Herring <robh@kernel.org>
To: Saravana Kannan <saravanak@google.com>
Cc: Marek Szyprowski <m.szyprowski@samsung.com>
Cc: devicetree@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Liya Huang <1425075683@qq.com>
---
For the data in __reservedmem_of_table, its function pointer initfn might 
be NULL. However, __reserved_mem_init_node() only considers non-NULL cases
and ignores NULL function pointers.

Therefore, a check for the possibility of initfn being NULL has been added
here, along with skipping the initfn() and issuing a warning.
---
 drivers/of/of_reserved_mem.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c
index ee2e31522d7ef69d816127a9003c423d5fb4023d..0a7cc599c0ca68001b2395759310f3585f247db9 100644
--- a/drivers/of/of_reserved_mem.c
+++ b/drivers/of/of_reserved_mem.c
@@ -496,6 +496,11 @@ static int __init __reserved_mem_init_node(struct reserved_mem *rmem)
 		if (!of_flat_dt_is_compatible(rmem->fdt_node, compat))
 			continue;
 
+		if (!initfn) {
+			pr_warn("no init function for %s\n", rmem->name);
+			continue;
+		}
+
 		ret = initfn(rmem);
 		if (ret == 0) {
 			pr_info("initialized node %s, compatible id %s\n",

---
base-commit: 8ffd015db85fea3e15a77027fda6c02ced4d2444
change-id: 20250415-__reserved_mem_init_node-689cbe9d54c4

Best regards,
-- 
Liya Huang <1425075683@qq.com>
Re: [PATCH] of: reserved-mem: Warn for missing initfn in __reservedmem_of_table
Posted by Rob Herring 8 months, 1 week ago
On Tue, Apr 15, 2025 at 9:16 AM Liya Huang <1425075683@qq.com> wrote:
>
> For the data in __reservedmem_of_table, its function pointer initfn might
> be NULL. However, __reserved_mem_init_node() only considers non-NULL cases
> and ignores NULL function pointers.

If initfn is NULL, there's no point to the entry and that's a bug.
Unless you have a build time check, there's no point to add this.

Rob
Re: [PATCH] of: reserved-mem: Warn for missing initfn in __reservedmem_of_table
Posted by 1425075683@qq.com 8 months, 1 week ago
> On Tue, Apr 15, 2025 at 9:16 AM Liya Huang <1425075683@qq.com> wrote:
> >
> > For the data in __reservedmem_of_table, its function pointer initfn might
> > be NULL. However, __reserved_mem_init_node() only considers non-NULL cases
> > and ignores NULL function pointers.
>
> If initfn is NULL, there's no point to the entry and that's a bug.
> Unless you have a build time check, there's no point to add this.
> 
> Rob

Thank you for your response. Based on your suggestion, I have made the 
modifications and used static_assert() to perform the check at compile 
time. The specific code is as follows. Could you please review whether 
this modification is reasonable? If it is acceptable, I will proceed with 
submitting the patch.

I did not find any usage of static_assert() for null pointer checks in the
kernel code. Additionally, BUILD_BUG_ON() cannot be used globally.

---
diff --git a/include/linux/of_reserved_mem.h b/include/linux/of_reserved_mem.h
index e338282da652..87446ad2deb2 100644
--- a/include/linux/of_reserved_mem.h
+++ b/include/linux/of_reserved_mem.h
@@ -29,6 +29,7 @@ typedef int (*reservedmem_of_init_fn)(struct reserved_mem *rmem);
 #ifdef CONFIG_OF_RESERVED_MEM
 
 #define RESERVEDMEM_OF_DECLARE(name, compat, init)			\
+	static_assert((init) != NULL);	\
 	_OF_DECLARE(reservedmem, name, compat, init, reservedmem_of_init_fn)
 
 int of_reserved_mem_device_init_by_idx(struct device *dev,
---

Thanks,
-- 
Liya Huang <1425075683@qq.com>

Re: [PATCH] of: reserved-mem: Warn for missing initfn in __reservedmem_of_table
Posted by Rob Herring 8 months ago
On Tue, Apr 15, 2025 at 9:02 PM <1425075683@qq.com> wrote:
>
> > On Tue, Apr 15, 2025 at 9:16 AM Liya Huang <1425075683@qq.com> wrote:
> > >
> > > For the data in __reservedmem_of_table, its function pointer initfn might
> > > be NULL. However, __reserved_mem_init_node() only considers non-NULL cases
> > > and ignores NULL function pointers.
> >
> > If initfn is NULL, there's no point to the entry and that's a bug.
> > Unless you have a build time check, there's no point to add this.
> >
> > Rob
>
> Thank you for your response. Based on your suggestion, I have made the
> modifications and used static_assert() to perform the check at compile
> time. The specific code is as follows. Could you please review whether
> this modification is reasonable? If it is acceptable, I will proceed with
> submitting the patch.
>
> I did not find any usage of static_assert() for null pointer checks in the
> kernel code.

That's a bit strange, but the use of static_assert() is a bit new.

> Additionally, BUILD_BUG_ON() cannot be used globally.
>
> ---
> diff --git a/include/linux/of_reserved_mem.h b/include/linux/of_reserved_mem.h
> index e338282da652..87446ad2deb2 100644
> --- a/include/linux/of_reserved_mem.h
> +++ b/include/linux/of_reserved_mem.h
> @@ -29,6 +29,7 @@ typedef int (*reservedmem_of_init_fn)(struct reserved_mem *rmem);
>  #ifdef CONFIG_OF_RESERVED_MEM
>
>  #define RESERVEDMEM_OF_DECLARE(name, compat, init)                     \
> +       static_assert((init) != NULL);  \
>         _OF_DECLARE(reservedmem, name, compat, init, reservedmem_of_init_fn)

I'm pretty sure we can apply this globally in _OF_DECLARE() as any
NULL init function would be useless. The special case is
CLK_OF_DECLARE which wraps the init function.

Rob