Re: [PATCH] of: reserved-mem: Warn for missing initfn in __reservedmem_of_table
From: Rob Herring
Date: Wed Apr 16 2025 - 15:26:06 EST
On Tue, Apr 15, 2025 at 9:02 PM <1425075683@xxxxxx> wrote:
>
> > On Tue, Apr 15, 2025 at 9:16 AM Liya Huang <1425075683@xxxxxx> 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