Skip to contentSkip to conversation

cpu: imx8_cpu: Avoid revision to corrupt device tree

PATCH Superseded Peng Fan (OSS) 4 participants 12 messages Oct 11, 2024 – Oct 17, 2024 Arm i.MX and Layerscape

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cpu: imx8_cpu: Avoid revision to corrupt device tree

From: Peng Fan <[email protected]>

U-Boot device tree is padded just after U-Boot proper. After the whole stuff loaded to DRAM space, the device tree area is conflict with BSS region before U-Boot relocation. So any write to BSS area before reloc_fdt will corrupt the device tree. Without the fix, there is issue that “binman_init failed:-2” on i.MX8MP-EVK board.

Move the variable to data section to fix the issue

---
 drivers/cpu/imx8_cpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cpu/imx8_cpu.c +1 -1
index 6c0a8c0cbe4..19c65c7ce47 100644
@@ -71,7 +71,7 @@ static const char *get_imx_type_str(u32 imxtype)
 
 static const char *get_imx_rev_str(u32 rev)
 {
-	static char revision[4];
+	static char revision[4] __section(".data");
 
 	if (IS_ENABLED(CONFIG_IMX8)) {
 		switch (rev) {
Signature · 1 line
2.35.3
#1 lore raw
Quoted 20 lines · Peng Fan (OSS)

From: Peng Fan <[email protected]>

U-Boot device tree is padded just after U-Boot proper. After the whole stuff loaded to DRAM space, the device tree area is conflict with BSS region before U-Boot relocation. So any write to BSS area before reloc_fdt will corrupt the device tree. Without the fix, there is issue that “binman_init failed:-2” on i.MX8MP-EVK board.

Move the variable to data section to fix the issue

---
 drivers/cpu/imx8_cpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cpu/imx8_cpu.c +0 -0
index 6c0a8c0cbe4..19c65c7ce47 100644

drivers/cpu/imx8_cpu.c static const char *get_imx_type_str(u32 imxtype) · line 77

Quoted 8 lines
drivers/cpu/imx8_cpu.c +1 -1
@@ -71,7 +71,7 @@ static const char *get_imx_type_str(u32 imxtype)
 
 static const char *get_imx_rev_str(u32 rev)
 {
-	static char revision[4];
+	static char revision[4] __section(".data");
 
 	if (IS_ENABLED(CONFIG_IMX8)) {
 		switch (rev) {

Why isn't this padding and alignment with the BSS being taken care of in either the linker script or the binman dts?

Signature · 1 line
Tom
reviewer
#2 lore raw
Quoted 2 lines

Why isn't this padding and alignment with the BSS being taken care of in either the linker script or the binman dts?

Peng, in addition to Tom's comment: which commit caused the problem?

Please add a Fixes tag.

#3 lore raw
Quoted 6 lines

Subject: Re: [PATCH] cpu: imx8_cpu: Avoid revision to corrupt device tree

Quoted 1 line

Why isn't this padding and alignment with the BSS being taken care

of

Quoted 1 line

in either the linker script or the binman dts?

Sorry, I am not sure what you mean.

Add a alignment in binman node for FIT dts to avoid dts area conflict with bss?

Quoted 5 lines

Peng, in addition to Tom's comment: which commit caused the problem?

Please add a Fixes tag.

It is just this commit makes this issue visible 51e7c1f822fa222d88d1ee06995cf355542427cc
mmc: fsl_esdhc_imx: Send 80 clocks before IDLE command

But the real commit should be
38e319782e9c3a1257329b8b7e276d10a30efb73
imx: imx8_cpu: support i.MX9

Regards,
Peng.

#4 lore raw
Quoted 8 lines · Peng Fan
Quoted 6 lines

Subject: Re: [PATCH] cpu: imx8_cpu: Avoid revision to corrupt device tree

Quoted 1 line

Why isn't this padding and alignment with the BSS being taken care

of

Quoted 1 line

in either the linker script or the binman dts?

Sorry, I am not sure what you mean.

I mean, why are things placed in this position to start with? Why is the device tree not already in a place where we aren't smashing it at run time? Is this a problem of the alignment / placement of items in a blob? A runtime placement problem? What?

Signature · 1 line
Tom
#5 lore raw
Quoted 11 lines

Subject: Re: [PATCH] cpu: imx8_cpu: Avoid revision to corrupt device tree

Quoted 1 line · Peng Fan
Quoted 1 line

Subject: Re: [PATCH] cpu: imx8_cpu: Avoid revision to corrupt

device

Quoted 3 lines
Quoted 3 lines

tree

Quoted 1 line

Why isn't this padding and alignment with the BSS being taken

care

Quoted 1 line
Quoted 1 line

of

Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line

in either the linker script or the binman dts?

Sorry, I am not sure what you mean.

I mean, why are things placed in this position to start with? Why is the device tree not already in a place where we aren't smashing it at run time? Is this a problem of the alignment / placement of items in a blob? A runtime placement problem? What?

Whether binman or not. I think u-boot.dtb is padded just end of u-boot-nodtb.bin. Not alignment or else.

This is a common issue, bss should not be written before reloc_fdt.

Regards,
Peng.

Quoted 3 lines

--
Tom

#6 lore raw
Quoted 20 lines · Peng Fan
Quoted 11 lines

Subject: Re: [PATCH] cpu: imx8_cpu: Avoid revision to corrupt device tree

Quoted 1 line · Peng Fan
Quoted 1 line

Subject: Re: [PATCH] cpu: imx8_cpu: Avoid revision to corrupt

device

Quoted 3 lines
Quoted 3 lines

tree

Quoted 1 line

Why isn't this padding and alignment with the BSS being taken

care

Quoted 1 line
Quoted 1 line

of

Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line

in either the linker script or the binman dts?

Sorry, I am not sure what you mean.

I mean, why are things placed in this position to start with? Why is the device tree not already in a place where we aren't smashing it at run time? Is this a problem of the alignment / placement of items in a blob? A runtime placement problem? What?

Quoted 4 lines

Whether binman or not. I think u-boot.dtb is padded just end of u-boot-nodtb.bin. Not alignment or else.

This is a common issue, bss should not be written before reloc_fdt.

It's possible I'm missing the examples in my quick grep right now but, yes, why are you needing to make this device tree change so early? We should not be using the "put this in the data section" kludge unless strictly necessary. Why is this necessary and cannot wait until, well, further along in the boot? This is supposed to be for pre-DRAM-initialization stuff.

Signature · 1 line
Tom
#7 lore raw
Quoted 30 lines

Subject: Re: [PATCH] cpu: imx8_cpu: Avoid revision to corrupt device tree

Quoted 1 line · Peng Fan
Quoted 1 line

Subject: Re: [PATCH] cpu: imx8_cpu: Avoid revision to corrupt

device

Quoted 8 lines
Quoted 8 lines

tree

Quoted 1 line · Peng Fan
Quoted 1 line

Subject: Re: [PATCH] cpu: imx8_cpu: Avoid revision to corrupt

device

Quoted 3 lines
Quoted 3 lines

tree

Quoted 1 line

Why isn't this padding and alignment with the BSS being

taken

Quoted 9 lines
Quoted 9 lines

care

Quoted 4 lines
Quoted 2 lines

of

Quoted 1 line

in either the linker script or the binman dts?

Sorry, I am not sure what you mean.

I mean, why are things placed in this position to start with? Why is the device tree not already in a place where we aren't smashing it at run time? Is this a problem of the alignment / placement of items

in a blob?

Quoted 5 lines
Quoted 1 line

A runtime placement problem? What?

Whether binman or not. I think u-boot.dtb is padded just end of u-boot-nodtb.bin. Not alignment or else.

Quoted 8 lines
Quoted 1 line

This is a common issue, bss should not be written before reloc_fdt.

It's possible I'm missing the examples in my quick grep right now but, yes, why are you needing to make this device tree change so early? We should not be using the "put this in the data section" kludge unless strictly necessary. Why is this necessary and cannot wait until, well, further along in the boot? This is supposed to be for pre-DRAM- initialization stuff.

You may misunderstand.
It is imx8_cpu driver probe at pre reloc stage before reloc_fdt, so the written to bss area in imx8_cpu driver will corrupt device tree which padded after u-boot-nodtb.bin.

Then after reloc_fdt, the new dtb is actually a broken one.

Regards,
Peng.

Quoted 3 lines

--
Tom

reviewer
#8 lore raw
In reply to Tom Rini

Hi Peng,

Quoted 2 lines · Tom Rini

Why isn't this padding and alignment with the BSS being taken care of in either the linker script or the binman dts?

I have the same concern as Tom. This fix looks fragile.

Can you propose another solution?

#9 lore raw
In reply to Peng Fan (OSS)
Quoted 20 lines · Peng Fan (OSS)

From: Peng Fan <[email protected]>

U-Boot device tree is padded just after U-Boot proper. After the whole stuff loaded to DRAM space, the device tree area is conflict with BSS region before U-Boot relocation. So any write to BSS area before reloc_fdt will corrupt the device tree. Without the fix, there is issue that “binman_init failed:-2” on i.MX8MP-EVK board.

Move the variable to data section to fix the issue

---
 drivers/cpu/imx8_cpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cpu/imx8_cpu.c +0 -0
index 6c0a8c0cbe4..19c65c7ce47 100644

drivers/cpu/imx8_cpu.c static const char *get_imx_type_str(u32 imxtype) · line 74

Quoted 5 lines
drivers/cpu/imx8_cpu.c +1 -1
@@ -71,7 +71,7 @@ static const char *get_imx_type_str(u32 imxtype)
 
 static const char *get_imx_rev_str(u32 rev)
 {
-	static char revision[4];
+	static char revision[4] __section(".data");

After my last mail, I pulled up the patch itself and started looking at the driver. Why is "revision" being done like this? This is not a good common practice.

Signature · 1 line
Tom
#10 lore raw
Quoted 28 lines

Subject: Re: [PATCH] cpu: imx8_cpu: Avoid revision to corrupt device tree

Quoted 3 lines · Peng Fan (OSS)

From: Peng Fan <[email protected]>

U-Boot device tree is padded just after U-Boot proper. After the

whole

drivers/cpu/imx8_cpu.c static const char *get_imx_type_str(u32

Quoted 17 lines

stuff loaded to DRAM space, the device tree area is conflict with BSS region before U-Boot relocation. So any write to BSS area before reloc_fdt will corrupt the device tree. Without the fix, there is issue that “binman_init failed:-2” on i.MX8MP-EVK board.

Move the variable to data section to fix the issue

---
 drivers/cpu/imx8_cpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cpu/imx8_cpu.c index renamed from drivers/cpu/imx8_cpu.c +0 -0

6c0a8c0cbe4..19c65c7ce47 100644

drivers/cpu/imx8_cpu.c +0 -0
@@ -71,7 +71,7 @@ static const char *get_imx_type_str(u32

imxtype)

Quoted 1 line
Quoted 7 lines

drivers/cpu/imx8_cpu.c static const char *get_imx_type_str(u32 imxtype) · line 74

Quoted 3 lines

static const char *get_imx_rev_str(u32 rev) {

drivers/cpu/imx8_cpu.c +1 -1
-	static char revision[4];
+	static char revision[4] __section(".data");

After my last mail, I pulled up the patch itself and started looking at the driver. Why is "revision" being done like this? This is not a good common practice.

Since the function returns a pointer to a string, so ... We could move it to malloc area.

Regards,
Peng.

Quoted 3 lines

--
Tom

#11 lore raw
Quoted 36 lines · Peng Fan
Quoted 28 lines

Subject: Re: [PATCH] cpu: imx8_cpu: Avoid revision to corrupt device tree

Quoted 3 lines · Peng Fan (OSS)

From: Peng Fan <[email protected]>

U-Boot device tree is padded just after U-Boot proper. After the

whole

drivers/cpu/imx8_cpu.c static const char *get_imx_type_str(u32

Quoted 17 lines

stuff loaded to DRAM space, the device tree area is conflict with BSS region before U-Boot relocation. So any write to BSS area before reloc_fdt will corrupt the device tree. Without the fix, there is issue that “binman_init failed:-2” on i.MX8MP-EVK board.

Move the variable to data section to fix the issue

---
 drivers/cpu/imx8_cpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cpu/imx8_cpu.c index renamed from drivers/cpu/imx8_cpu.c +0 -0

6c0a8c0cbe4..19c65c7ce47 100644

drivers/cpu/imx8_cpu.c +0 -0
@@ -71,7 +71,7 @@ static const char *get_imx_type_str(u32

imxtype)

Quoted 1 line
Quoted 7 lines

drivers/cpu/imx8_cpu.c static const char *get_imx_type_str(u32 imxtype) · line 74

Quoted 3 lines

static const char *get_imx_rev_str(u32 rev) {

drivers/cpu/imx8_cpu.c +1 -1
-	static char revision[4];
+	static char revision[4] __section(".data");

After my last mail, I pulled up the patch itself and started looking at the driver. Why is "revision" being done like this? This is not a good common practice.

Quoted 2 lines

Since the function returns a pointer to a string, so ... We could move it to malloc area.

Yes, something like that sounds better, thanks.

Signature · 1 line
Tom