[PATCH v3 1/4] LoongArch: Fix built-in DTB detection

Jiaxun Yang posted 4 patches 1 year, 8 months ago
[PATCH v3 1/4] LoongArch: Fix built-in DTB detection
Posted by Jiaxun Yang 1 year, 8 months ago
fdt_check_header(__dtb_start) will always success because kernel
provided a dummy dtb, and by coincidence __dtb_start clashed with
entry of this dummy dtb. The consequence is fdt passed from
firmware will never be taken.

Fix by trying to utilise __dtb_start only when CONFIG_BUILTIN_DTB
is enabled.

Cc: stable@vger.kernel.org
Fixes: 7b937cc243e5 ("of: Create of_root if no dtb provided by firmware")
Signed-off-by: Jiaxun Yang <jiaxun.yang@flygoat.com>
---
v3: Better reasoning in commit message, thanks Binbin and Huacai!
---
 arch/loongarch/kernel/setup.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/arch/loongarch/kernel/setup.c b/arch/loongarch/kernel/setup.c
index 60e0fe97f61a..ea6d5db6c878 100644
--- a/arch/loongarch/kernel/setup.c
+++ b/arch/loongarch/kernel/setup.c
@@ -275,16 +275,18 @@ static void __init arch_reserve_crashkernel(void)
 static void __init fdt_setup(void)
 {
 #ifdef CONFIG_OF_EARLY_FLATTREE
-	void *fdt_pointer;
+	void *fdt_pointer = NULL;
 
 	/* ACPI-based systems do not require parsing fdt */
 	if (acpi_os_get_root_pointer())
 		return;
 
+#ifdef CONFIG_BUILTIN_DTB
 	/* Prefer to use built-in dtb, checking its legality first. */
 	if (!fdt_check_header(__dtb_start))
 		fdt_pointer = __dtb_start;
-	else
+#endif
+	if (!fdt_pointer)
 		fdt_pointer = efi_fdt_pointer(); /* Fallback to firmware dtb */
 
 	if (!fdt_pointer || fdt_check_header(fdt_pointer))

-- 
2.43.0
Re: [PATCH v3 1/4] LoongArch: Fix built-in DTB detection
Posted by Thomas Weißschuh 1 year, 8 months ago
On 2024-05-22 23:02:17+0000, Jiaxun Yang wrote:
> fdt_check_header(__dtb_start) will always success because kernel
> provided a dummy dtb, and by coincidence __dtb_start clashed with
> entry of this dummy dtb. The consequence is fdt passed from
> firmware will never be taken.
> 
> Fix by trying to utilise __dtb_start only when CONFIG_BUILTIN_DTB
> is enabled.
> 
> Cc: stable@vger.kernel.org
> Fixes: 7b937cc243e5 ("of: Create of_root if no dtb provided by firmware")
> Signed-off-by: Jiaxun Yang <jiaxun.yang@flygoat.com>
> ---
> v3: Better reasoning in commit message, thanks Binbin and Huacai!
> ---
>  arch/loongarch/kernel/setup.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/loongarch/kernel/setup.c b/arch/loongarch/kernel/setup.c
> index 60e0fe97f61a..ea6d5db6c878 100644
> --- a/arch/loongarch/kernel/setup.c
> +++ b/arch/loongarch/kernel/setup.c
> @@ -275,16 +275,18 @@ static void __init arch_reserve_crashkernel(void)
>  static void __init fdt_setup(void)
>  {
>  #ifdef CONFIG_OF_EARLY_FLATTREE
> -	void *fdt_pointer;
> +	void *fdt_pointer = NULL;
>  
>  	/* ACPI-based systems do not require parsing fdt */
>  	if (acpi_os_get_root_pointer())
>  		return;
>  
> +#ifdef CONFIG_BUILTIN_DTB
>  	/* Prefer to use built-in dtb, checking its legality first. */
>  	if (!fdt_check_header(__dtb_start))
>  		fdt_pointer = __dtb_start;
> -	else
> +#endif
> +	if (!fdt_pointer)
>  		fdt_pointer = efi_fdt_pointer(); /* Fallback to firmware dtb */

Prefer to use non-ifdef logic:

	if (IS_ENABLED(CONFIG_BUILTIN_DTB) && !fdt_check_header(__dtb_start))
  		fdt_pointer = __dtb_start;

This is shorter, easier to read and will prevent bitrot.
The code will be typechecked but then optimized away, so no
runtime overhead exists.

>  
>  	if (!fdt_pointer || fdt_check_header(fdt_pointer))
> 
> -- 
> 2.43.0
>
Re: [PATCH v3 1/4] LoongArch: Fix built-in DTB detection
Posted by Huacai Chen 1 year, 8 months ago
Hi, Thomas,

On Thu, May 30, 2024 at 6:06 PM Thomas Weißschuh <thomas@t-8ch.de> wrote:
>
> On 2024-05-22 23:02:17+0000, Jiaxun Yang wrote:
> > fdt_check_header(__dtb_start) will always success because kernel
> > provided a dummy dtb, and by coincidence __dtb_start clashed with
> > entry of this dummy dtb. The consequence is fdt passed from
> > firmware will never be taken.
> >
> > Fix by trying to utilise __dtb_start only when CONFIG_BUILTIN_DTB
> > is enabled.
> >
> > Cc: stable@vger.kernel.org
> > Fixes: 7b937cc243e5 ("of: Create of_root if no dtb provided by firmware")
> > Signed-off-by: Jiaxun Yang <jiaxun.yang@flygoat.com>
> > ---
> > v3: Better reasoning in commit message, thanks Binbin and Huacai!
> > ---
> >  arch/loongarch/kernel/setup.c | 6 ++++--
> >  1 file changed, 4 insertions(+), 2 deletions(-)
> >
> > diff --git a/arch/loongarch/kernel/setup.c b/arch/loongarch/kernel/setup.c
> > index 60e0fe97f61a..ea6d5db6c878 100644
> > --- a/arch/loongarch/kernel/setup.c
> > +++ b/arch/loongarch/kernel/setup.c
> > @@ -275,16 +275,18 @@ static void __init arch_reserve_crashkernel(void)
> >  static void __init fdt_setup(void)
> >  {
> >  #ifdef CONFIG_OF_EARLY_FLATTREE
> > -     void *fdt_pointer;
> > +     void *fdt_pointer = NULL;
> >
> >       /* ACPI-based systems do not require parsing fdt */
> >       if (acpi_os_get_root_pointer())
> >               return;
> >
> > +#ifdef CONFIG_BUILTIN_DTB
> >       /* Prefer to use built-in dtb, checking its legality first. */
> >       if (!fdt_check_header(__dtb_start))
> >               fdt_pointer = __dtb_start;
> > -     else
> > +#endif
> > +     if (!fdt_pointer)
> >               fdt_pointer = efi_fdt_pointer(); /* Fallback to firmware dtb */
>
> Prefer to use non-ifdef logic:
>
>         if (IS_ENABLED(CONFIG_BUILTIN_DTB) && !fdt_check_header(__dtb_start))
>                 fdt_pointer = __dtb_start;
>
> This is shorter, easier to read and will prevent bitrot.
> The code will be typechecked but then optimized away, so no
> runtime overhead exists.
Good suggestion, I will take it.

Huacai
>
> >
> >       if (!fdt_pointer || fdt_check_header(fdt_pointer))
> >
> > --
> > 2.43.0
> >