lib: Allow unaligned memory access from decompressors selectively
Subsystems (5)
v2 cover lib: Allow unaligned memory access from decompressors selectively
Some decompressors rely on wide unaligned loads, which get very expensive on strict alignment architectures such as ARM due to the need for the compiler to reassemble each wide load byte at a time.
On the other hand, some ARM platforms such as Rockchip enable the MMU and d-cache early, making plain unaligned loads work too.
Current Makefiles force the compiler to work around the unaligned loads unconditionally, which in case of e.g. Zstd in SPL on Rockchip degrades its performance by a factor of 4+ compared to native unaligned loads.
Add an option for arches to opt out of the strict alignment flags for decompressors, when they can guarantee that wide unaligned loads are safe.
This improves the decompression performance and reduces the SPL size due to fewer instructions generated per each unaligned load.
CI for this series: https://git.u-boot-project.org/u-boot/contributors/alchark/u-boot/-/pipelines/1431
---
Changes in v2:
- More caution-forward messaging in the symbol name and help texts (thanks Tom)
- Link to v1: https://patch.msgid.link/[email protected]
To: [email protected]
To: Simon Glass <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: Ilias Apalodimas <[email protected]>
Cc: Alexey Charkov <[email protected]>
Cc: Johan Jonker <[email protected]>
Cc: Casey Connolly <[email protected]>
Cc: Vincent Jardin <[email protected]>
Cc: Michael Srba <[email protected]>
Cc: Cole Munz <[email protected]>
Cc: Quentin Schulz <[email protected]>
Cc: Kever Yang <[email protected]>
---
Alexey Charkov (2):
lib: Let the decompressors use unaligned loads if platform allows
rockchip: Declare unaligned access safe in the ARM64 SPL
arch/Kconfig | 35 +++++++++++++++++++++++++++++++++++ arch/arm/cpu/armv7/config.mk | 3 +++ arch/arm/cpu/armv8/config.mk | 3 +++ arch/arm/mach-rockchip/Kconfig | 5 +++++ lib/Makefile | 3 +++ lib/lzma/Makefile | 8 ++++++++ lib/lzo/Makefile | 8 ++++++++ lib/zlib/Makefile | 8 ++++++++ lib/zstd/Makefile | 8 ++++++++ 9 files changed, 81 insertions(+)
---
base-commit: ed28577c31d895fa766ffb71ec1c35c4b39db2a7
change-id: 20260918-unaligned-decomp-0efee141e754
Best regards,
Signature · 1 line
Alexey Charkov <[email protected]>
PATCH v2 1/2 lib: Let the decompressors use unaligned loads if platform allows
ARM passes -mstrict-align (-mno-unaligned-access on ARMv7) to everything, because when the MMU is off any unaligned access there faults. The compiler then has to assemble every wide unaligned load out of single
bytes: one ldr becomes eight ldrb and seven orr/lsl.
This is especially painful for decompressors which refill their buffer in 64-bit words. On a Rockchip RK3576 SPL, zstd unpacks a 30MiB Falcon mode Linux kernel in 1947ms built this way and 596ms without the flag, and the SPL gets 2.3KiB smaller by cutting down on the byte-wise loads and ORs. gzip, whose inflate walks its input a byte at a time anyway, gains 6%.
Add ASSUME_UNALIGNED_ACCESS_OK for a platform to state that the MMU is on by the time it unpacks an image, and let the decompression libraries drop the flag when it is set. The arch exports the flag it added rather than lib/ naming it, so this stays correct for anything else which adds one.
No functional change anywhere until the symbol is selected.
--- arch/Kconfig | 35 +++++++++++++++++++++++++++++++++++ arch/arm/cpu/armv7/config.mk | 3 +++ arch/arm/cpu/armv8/config.mk | 3 +++ lib/Makefile | 3 +++ lib/lzma/Makefile | 8 ++++++++ lib/lzo/Makefile | 8 ++++++++ lib/zlib/Makefile | 8 ++++++++ lib/zstd/Makefile | 8 ++++++++ 8 files changed, 76 insertions(+)
arch/Kconfig +35 -0
@@ -27,6 +27,41 @@ config SUPPORT_LITTLE_ENDIAN bool default y if !SUPPORT_BIG_ENDIAN +config ASSUME_UNALIGNED_ACCESS_OK + bool + help + Assume that an unaligned access cannot fault by the time U-Boot + proper unpacks an image, and build the decompression libraries + without the architecture's strict-alignment flag on that basis. + + Architectures whose unaligned accesses fault while the MMU is off + pass such a flag to everything, which makes the compiler assemble + each wide unaligned load out of single bytes. That is both slower + and larger, and a decompressor refilling its bit buffer in machine + words can spend up to four times as long for it. + + The assumption holds only once the MMU is on, which on most + platforms has not happened by the time an image is unpacked, and + nothing checks it. Select it from code which enables the MMU + first, under the same condition that code tests, so that the two + cannot drift apart. It has no prompt because there is no safe way + to answer it by hand. + + The flag is only dropped from the decompression libraries. Every + other part of U-Boot keeps it, including all of the code which runs + before the MMU comes up. + +config SPL_ASSUME_UNALIGNED_ACCESS_OK + bool + depends on SPL + help + The same assumption for SPL, which on most platforms runs with the + MMU off from start to finish and must therefore leave the flag + alone. See ASSUME_UNALIGNED_ACCESS_OK. + + There is deliberately no TPL or VPL variant: those phases run before + DRAM is up, so the flag always stays. + config SYS_CACHE_SHIFT_4 bool
arch/arm/cpu/armv7/config.mk +3 -0
@@ -9,3 +9,6 @@ # of our decision. PF_NO_UNALIGNED := $(call cc-option, -mno-unaligned-access,) PLATFORM_CPPFLAGS += $(PF_NO_UNALIGNED) +# Code which only runs with the MMU on may drop this again; see +# CONFIG_ASSUME_UNALIGNED_ACCESS_OK. +export PF_NO_UNALIGNED
arch/arm/cpu/armv8/config.mk +3 -0
@@ -6,6 +6,9 @@ PLATFORM_RELFLAGS += $(call cc-option,-mbranch-protection=none) PF_NO_UNALIGNED := $(call cc-option, -mstrict-align) PLATFORM_CPPFLAGS += $(PF_NO_UNALIGNED) +# Code which only runs with the MMU on may drop this again; see +# CONFIG_ASSUME_UNALIGNED_ACCESS_OK. +export PF_NO_UNALIGNED EFI_LDS := elf_aarch64_efi.lds EFI_CRT0 := crt0_aarch64_efi.o
lib/Makefile +3 -0
@@ -93,6 +93,9 @@ obj-$(CONFIG_$(PHASE_)GZIP) += gunzip.o obj-$(CONFIG_$(PHASE_)LZO) += lzo/ obj-$(CONFIG_$(PHASE_)LZMA) += lzma/ obj-$(CONFIG_$(PHASE_)LZ4) += lz4_wrapper.o +ifdef CONFIG_$(PHASE_)ASSUME_UNALIGNED_ACCESS_OK +CFLAGS_REMOVE_lz4_wrapper.o += $(PF_NO_UNALIGNED) +endif obj-$(CONFIG_$(PHASE_)LIB_RATIONAL) += rational.o
lib/lzma/Makefile +8 -0
@@ -10,3 +10,11 @@ ccflags-y += -DZ7_LZMA_PROB32 ccflags-$(CONFIG_$(PHASE_)LZMA_SIZE_OPTIMIZATION) += -DZ7_LZMA_SIZE_OPT obj-y += LzmaDec.o LzmaTools.o + +# These only run once an image is being unpacked, which on some platforms is +# late enough that unaligned accesses are safe. Where the platform says so, +# let the compiler use them: assembling a 64-bit read out of bytes is both +# slower and larger. +ifdef CONFIG_$(PHASE_)ASSUME_UNALIGNED_ACCESS_OK +ccflags-remove-y += $(PF_NO_UNALIGNED) +endif
lib/lzo/Makefile +8 -0
@@ -4,3 +4,11 @@ # Stefan Roese, DENX Software Engineering, [email protected]. obj-y += lzo1x_decompress.o + +# These only run once an image is being unpacked, which on some platforms is +# late enough that unaligned accesses are safe. Where the platform says so, +# let the compiler use them: assembling a 64-bit read out of bytes is both +# slower and larger. +ifdef CONFIG_$(PHASE_)ASSUME_UNALIGNED_ACCESS_OK +ccflags-remove-y += $(PF_NO_UNALIGNED) +endif
lib/zlib/Makefile +8 -0
@@ -4,3 +4,11 @@ # Wolfgang Denk, DENX Software Engineering, [email protected]. obj-y += zlib.o + +# These only run once an image is being unpacked, which on some platforms is +# late enough that unaligned accesses are safe. Where the platform says so, +# let the compiler use them: assembling a 64-bit read out of bytes is both +# slower and larger. +ifdef CONFIG_$(PHASE_)ASSUME_UNALIGNED_ACCESS_OK +ccflags-remove-y += $(PF_NO_UNALIGNED) +endif
lib/zstd/Makefile +8 -0
@@ -35,3 +35,11 @@ zstd_common-y := \ common/error_private.o \ common/fse_decompress.o \ common/zstd_common.o \ + +# These only run once an image is being unpacked, which on some platforms is +# late enough that unaligned accesses are safe. Where the platform says so, +# let the compiler use them: assembling a 64-bit read out of bytes is both +# slower and larger. +ifdef CONFIG_$(PHASE_)ASSUME_UNALIGNED_ACCESS_OK +ccflags-remove-y += $(PF_NO_UNALIGNED) +endif
Signature · 1 line
2.55.0
Re: [PATCH v2 1/2] lib: Let the decompressors use unaligned loads if platform allows
Quoted 12 lines · Alexey Charkov
ARM passes -mstrict-align (-mno-unaligned-access on ARMv7) to everything, because when the MMU is off any unaligned access there faults. The compiler then has to assemble every wide unaligned load out of single
bytes: one ldr becomes eight ldrb and seven orr/lsl.This is especially painful for decompressors which refill their buffer in 64-bit words. On a Rockchip RK3576 SPL, zstd unpacks a 30MiB Falcon mode Linux kernel in 1947ms built this way and 596ms without the flag, and the SPL gets 2.3KiB smaller by cutting down on the byte-wise loads and ORs. gzip, whose inflate walks its input a byte at a time anyway, gains 6%.
Quoted 8 lines
Add
ASSUME_UNALIGNED_ACCESS_OKfor a platform to state that the MMU is on by the time it unpacks an image, and let the decompression libraries drop the flag when it is set. The arch exports the flag it added rather thanlib/naming it, so this stays correct for anything else which adds one.No functional change anywhere until the symbol is selected.
Signed-off-by Alexey Charkov <[email protected]>
I think there is future room here to build on, I don't hate the idea of someone looking at how to invert the logic if and only if there's a clean way to say "here is the pre-MMU-enabled world". I don't know that such a line easily exists. It might also not really be a win for anything other than decompression and a few other operations anyhow. With that note for future work:
Signature · 1 line
Tom
Re: [PATCH v2 1/2] lib: Let the decompressors use unaligned loads if platform allows
In reply to Alexey CharkovHi Alexey,
Quoted 39 lines · Alexey Charkov
ARM passes -mstrict-align (-mno-unaligned-access on ARMv7) to everything, because when the MMU is off any unaligned access there faults. The compiler then has to assemble every wide unaligned load out of single
bytes: one ldr becomes eight ldrb and seven orr/lsl.This is especially painful for decompressors which refill their buffer in 64-bit words. On a Rockchip RK3576 SPL, zstd unpacks a 30MiB Falcon mode Linux kernel in 1947ms built this way and 596ms without the flag, and the SPL gets 2.3KiB smaller by cutting down on the byte-wise loads and ORs. gzip, whose inflate walks its input a byte at a time anyway, gains 6%.
Add
ASSUME_UNALIGNED_ACCESS_OKfor a platform to state that the MMU is on by the time it unpacks an image, and let the decompression libraries drop the flag when it is set. The arch exports the flag it added rather thanlib/naming it, so this stays correct for anything else which adds one.No functional change anywhere until the symbol is selected.
Signed-off-by Alexey Charkov <[email protected]>--- arch/Kconfig | 35 +++++++++++++++++++++++++++++++++++ arch/arm/cpu/armv7/config.mk | 3 +++ arch/arm/cpu/armv8/config.mk | 3 +++ lib/Makefile | 3 +++ lib/lzma/Makefile | 8 ++++++++ lib/lzo/Makefile | 8 ++++++++ lib/zlib/Makefile | 8 ++++++++ lib/zstd/Makefile | 8 ++++++++ 8 files changed, 76 insertions(+)arch/Kconfig +0
-0
arch/Kconfig config SUPPORT_LITTLE_ENDIAN · line 30
Quoted 4 lines
arch/Kconfig +1
-0@@ -27,6 +27,41 @@ config SUPPORT_LITTLE_ENDIAN bool default y if !SUPPORT_BIG_ENDIAN +config ASSUME_UNALIGNED_ACCESS_OK
I only see changes in ARM and ARM64 config.mk, should this (and SPL) symbol depends on ARM/ARM64 maybe? I see in the second patch you're adding another dependency on !SPL_SYS_DCACHE_OFF, any reason for it not to be a dependency in the main symbol in this file here?
This is very compressor/decompressor-specific, should we rename the symbol so it's clear only those would/should be impacted by this symbol (hoping that its usage doesn't get extended in the future).
Quoted 5 lines
arch/Kconfig +5
-0+ bool + help + Assume that an unaligned access cannot fault by the time U-Boot + proper unpacks an image, and build the decompression libraries + without the architecture's strict-alignment flag on that basis.
arch/Kconfig config SUPPORT_LITTLE_ENDIAN · line 45
Quoted 10 lines
arch/Kconfig +10
-0+ + Architectures whose unaligned accesses fault while the MMU is off + pass such a flag to everything, which makes the compiler assemble + each wide unaligned load out of single bytes. That is both slower + and larger, and a decompressor refilling its bit buffer in machine + words can spend up to four times as long for it. + + The assumption holds only once the MMU is on, which on most + platforms has not happened by the time an image is unpacked, and + nothing checks it. Select it from code which enables the MMU
Crazy idea, but should we add a flag that specifies whether the MMU (and dcache?) has been set up, and then read that flag in places where we have this assumption? Then we could at least clearly hard fail on this flag not being set and tell the user something went wrong before the system does a HW fault by itself?
Cheers,
Quentin
Re: [PATCH v2 1/2] lib: Let the decompressors use unaligned loads if platform allows
Hi Quentin,
Quoted 46 lines · Quentin Schulz
Hi Alexey,
Quoted 42 lines · Alexey Charkov
ARM passes -mstrict-align (-mno-unaligned-access on ARMv7) to everything, because when the MMU is off any unaligned access there faults. The compiler then has to assemble every wide unaligned load out of single
bytes: one ldr becomes eight ldrb and seven orr/lsl.This is especially painful for decompressors which refill their buffer in 64-bit words. On a Rockchip RK3576 SPL, zstd unpacks a 30MiB Falcon mode Linux kernel in 1947ms built this way and 596ms without the flag, and the SPL gets 2.3KiB smaller by cutting down on the byte-wise loads and ORs. gzip, whose inflate walks its input a byte at a time anyway, gains 6%.
Add
ASSUME_UNALIGNED_ACCESS_OKfor a platform to state that the MMU is on by the time it unpacks an image, and let the decompression libraries drop the flag when it is set. The arch exports the flag it added rather thanlib/naming it, so this stays correct for anything else which adds one.No functional change anywhere until the symbol is selected.
Signed-off-by Alexey Charkov <[email protected]>--- arch/Kconfig | 35 +++++++++++++++++++++++++++++++++++ arch/arm/cpu/armv7/config.mk | 3 +++ arch/arm/cpu/armv8/config.mk | 3 +++ lib/Makefile | 3 +++ lib/lzma/Makefile | 8 ++++++++ lib/lzo/Makefile | 8 ++++++++ lib/zlib/Makefile | 8 ++++++++ lib/zstd/Makefile | 8 ++++++++ 8 files changed, 76 insertions(+)arch/Kconfig +0
-0@@ -27,6 +27,41 @@ config SUPPORT_LITTLE_ENDIAN bool default y if !SUPPORT_BIG_ENDIAN
Quoted 6 lines
arch/Kconfig config SUPPORT_LITTLE_ENDIAN · line 30
Quoted 1 line
arch/Kconfig +1
-0+config ASSUME_UNALIGNED_ACCESS_OKI only see changes in ARM and ARM64 config.mk, should this (and SPL) symbol depends on ARM/ARM64 maybe? I see in the second patch you're adding another dependency on !SPL_SYS_DCACHE_OFF, any reason for it not to be a dependency in the main symbol in this file here?
Given that it's strictly opt-in by each platform and not user-selectable, I don't think adding explicit dependencies in the main symbol helps much. As for !SPL_SYS_DCACHE_OFF, that's arch-specific. E.g. on ARM/ARM64 unaligned accesses fault by default. The exception is when both the MMU and D-cache are up and running - in which case they work just fine. For some other strict-alignment architecture the set of conditions would probably be different - and I have no idea about those, so I'd rather leave it to the respective architecture/platform code owners to assert, not add any direct hints which may or may not apply to their case.
Quoted 3 lines
This is very compressor/decompressor-specific, should we rename the symbol so it's clear only those would/should be impacted by this symbol (hoping that its usage doesn't get extended in the future).
Compressors and decompressors are the primary beneficiaries and the only current users enabled by this patch, but the mechanism itself isn't strictly limited to them. I also hate overly long Kconfig symbol names, and the current one is already stretching it a bit :)
Quoted 13 lines
Quoted 13 lines
arch/Kconfig +13
-0+ bool + help + Assume that an unaligned access cannot fault by the time U-Boot + proper unpacks an image, and build the decompression libraries + without the architecture's strict-alignment flag on that basis. + + Architectures whose unaligned accesses fault while the MMU is off + pass such a flag to everything, which makes the compiler assemble + each wide unaligned load out of single bytes. That is both slower + and larger, and a decompressor refilling its bit buffer in machine + words can spend up to four times as long for it. + + The assumption holds only once the MMU is on, which on most
Quoted 8 lines
arch/Kconfig config SUPPORT_LITTLE_ENDIAN · line 45
Quoted 2 lines
arch/Kconfig +2
-0+ platforms has not happened by the time an image is unpacked, and + nothing checks it. Select it from code which enables the MMUCrazy idea, but should we add a flag that specifies whether the MMU (and dcache?) has been set up, and then read that flag in places where we have this assumption? Then we could at least clearly hard fail on this flag not being set and tell the user something went wrong before the system does a HW fault by itself?
Do you mean a runtime flag? I'm afraid that will add more global state affecting all platforms, and some platforms with tight SPL size requirements will not love it.
Best regards,
Alexey
Re: [PATCH v2 1/2] lib: Let the decompressors use unaligned loads if platform allows
Hi Alexey,
Quoted 56 lines · Alexey Charkov
Hi Quentin,
Quoted 46 lines · Quentin Schulz
Hi Alexey,
Quoted 42 lines · Alexey Charkov
ARM passes -mstrict-align (-mno-unaligned-access on ARMv7) to everything, because when the MMU is off any unaligned access there faults. The compiler then has to assemble every wide unaligned load out of single
bytes: one ldr becomes eight ldrb and seven orr/lsl.This is especially painful for decompressors which refill their buffer in 64-bit words. On a Rockchip RK3576 SPL, zstd unpacks a 30MiB Falcon mode Linux kernel in 1947ms built this way and 596ms without the flag, and the SPL gets 2.3KiB smaller by cutting down on the byte-wise loads and ORs. gzip, whose inflate walks its input a byte at a time anyway, gains 6%.
Add
ASSUME_UNALIGNED_ACCESS_OKfor a platform to state that the MMU is on by the time it unpacks an image, and let the decompression libraries drop the flag when it is set. The arch exports the flag it added rather thanlib/naming it, so this stays correct for anything else which adds one.No functional change anywhere until the symbol is selected.
Signed-off-by Alexey Charkov <[email protected]>--- arch/Kconfig | 35 +++++++++++++++++++++++++++++++++++ arch/arm/cpu/armv7/config.mk | 3 +++ arch/arm/cpu/armv8/config.mk | 3 +++ lib/Makefile | 3 +++ lib/lzma/Makefile | 8 ++++++++ lib/lzo/Makefile | 8 ++++++++ lib/zlib/Makefile | 8 ++++++++ lib/zstd/Makefile | 8 ++++++++ 8 files changed, 76 insertions(+)arch/Kconfig +0
-0@@ -27,6 +27,41 @@ config SUPPORT_LITTLE_ENDIAN bool default y if !SUPPORT_BIG_ENDIANQuoted 6 lines
arch/Kconfig config SUPPORT_LITTLE_ENDIAN · line 30
Quoted 1 line
arch/Kconfig +1
-0+config ASSUME_UNALIGNED_ACCESS_OKI only see changes in ARM and ARM64 config.mk, should this (and SPL) symbol depends on ARM/ARM64 maybe? I see in the second patch you're adding another dependency on !SPL_SYS_DCACHE_OFF, any reason for it not to be a dependency in the main symbol in this file here?
Quoted 11 lines
Given that it's strictly opt-in by each platform and not user-selectable, I don't think adding explicit dependencies in the main symbol helps much. As for !SPL_SYS_DCACHE_OFF, that's arch-specific. E.g. on ARM/ARM64 unaligned accesses fault by default. The exception is when both the MMU and D-cache are up and running - in which case they work just fine. For some other strict-alignment architecture the set of conditions would probably be different - and I have no idea about those, so I'd rather leave it to the respective architecture/platform code owners to assert, not add any direct hints which may or may not apply to their case.
I guess the point is that this is an invisible symbol and it's up to the arch/soc maintainers to enable it via a select (so joining you on not adding the dependencies here).
I know I'm usually against going into implementation details in Kconfig symbol's helptext, but maybe it'd be nice to give an example for ARM64 on which functions must be called before the code that can make use of that symbol is called? That could help other architecture maintainers to decide whether this could be applicable to them as well?
You also missed the -mno-unaligned-access in arch/arm/cpu/armv7m/config.mk, was this on purpose?
Quoted 9 lines
Quoted 3 lines
This is very compressor/decompressor-specific, should we rename the symbol so it's clear only those would/should be impacted by this symbol (hoping that its usage doesn't get extended in the future).
Compressors and decompressors are the primary beneficiaries and the only current users enabled by this patch, but the mechanism itself isn't strictly limited to them. I also hate overly long
Kconfigsymbol names, and the current one is already stretching it a bit :)
Fair enough.
Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
arch/Kconfig +13
-0+ bool + help + Assume that an unaligned access cannot fault by the time U-Boot + proper unpacks an image, and build the decompression libraries + without the architecture's strict-alignment flag on that basis. + + Architectures whose unaligned accesses fault while the MMU is off + pass such a flag to everything, which makes the compiler assemble + each wide unaligned load out of single bytes. That is both slower + and larger, and a decompressor refilling its bit buffer in machine + words can spend up to four times as long for it. + + The assumption holds only once the MMU is on, which on mostQuoted 8 lines
arch/Kconfig config SUPPORT_LITTLE_ENDIAN · line 45
Quoted 2 lines
arch/Kconfig +2
-0+ platforms has not happened by the time an image is unpacked, and + nothing checks it. Select it from code which enables the MMUCrazy idea, but should we add a flag that specifies whether the MMU (and dcache?) has been set up, and then read that flag in places where we have this assumption? Then we could at least clearly hard fail on this flag not being set and tell the user something went wrong before the system does a HW fault by itself?
Quoted 4 lines
Do you mean a runtime flag? I'm afraid that will add more global state affecting all platforms, and some platforms with tight SPL size requirements will not love it.
gd->flags with a new flag in enum gd_flags?
Cheers,
Quentin
Re: [PATCH v2 1/2] lib: Let the decompressors use unaligned loads if platform allows
Hi Quentin,
Quoted 76 lines · Quentin Schulz
Hi Alexey,
Quoted 56 lines · Alexey Charkov
Hi Quentin,
Quoted 46 lines · Quentin Schulz
Hi Alexey,
Quoted 42 lines · Alexey Charkov
ARM passes -mstrict-align (-mno-unaligned-access on ARMv7) to everything, because when the MMU is off any unaligned access there faults. The compiler then has to assemble every wide unaligned load out of single
bytes: one ldr becomes eight ldrb and seven orr/lsl.This is especially painful for decompressors which refill their buffer in 64-bit words. On a Rockchip RK3576 SPL, zstd unpacks a 30MiB Falcon mode Linux kernel in 1947ms built this way and 596ms without the flag, and the SPL gets 2.3KiB smaller by cutting down on the byte-wise loads and ORs. gzip, whose inflate walks its input a byte at a time anyway, gains 6%.
Add
ASSUME_UNALIGNED_ACCESS_OKfor a platform to state that the MMU is on by the time it unpacks an image, and let the decompression libraries drop the flag when it is set. The arch exports the flag it added rather thanlib/naming it, so this stays correct for anything else which adds one.No functional change anywhere until the symbol is selected.
Signed-off-by Alexey Charkov <[email protected]>--- arch/Kconfig | 35 +++++++++++++++++++++++++++++++++++ arch/arm/cpu/armv7/config.mk | 3 +++ arch/arm/cpu/armv8/config.mk | 3 +++ lib/Makefile | 3 +++ lib/lzma/Makefile | 8 ++++++++ lib/lzo/Makefile | 8 ++++++++ lib/zlib/Makefile | 8 ++++++++ lib/zstd/Makefile | 8 ++++++++ 8 files changed, 76 insertions(+)arch/Kconfig +0
-0@@ -27,6 +27,41 @@ config SUPPORT_LITTLE_ENDIAN bool default y if !SUPPORT_BIG_ENDIANQuoted 6 lines
arch/Kconfig config SUPPORT_LITTLE_ENDIAN · line 30
Quoted 1 line
arch/Kconfig +1
-0+config ASSUME_UNALIGNED_ACCESS_OKI only see changes in ARM and ARM64 config.mk, should this (and SPL) symbol depends on ARM/ARM64 maybe? I see in the second patch you're adding another dependency on !SPL_SYS_DCACHE_OFF, any reason for it not to be a dependency in the main symbol in this file here?
Quoted 11 lines
Given that it's strictly opt-in by each platform and not user-selectable, I don't think adding explicit dependencies in the main symbol helps much. As for !SPL_SYS_DCACHE_OFF, that's arch-specific. E.g. on ARM/ARM64 unaligned accesses fault by default. The exception is when both the MMU and D-cache are up and running - in which case they work just fine. For some other strict-alignment architecture the set of conditions would probably be different - and I have no idea about those, so I'd rather leave it to the respective architecture/platform code owners to assert, not add any direct hints which may or may not apply to their case.
I guess the point is that this is an invisible symbol and it's up to the
arch/socmaintainers to enable it via a select (so joining you on not adding the dependencies here).
Quoted 8 lines
I know I'm usually against going into implementation details in
Kconfigsymbol's helptext, but maybe it'd be nice to give an example for ARM64 on which functions must be called before the code that can make use of that symbol is called? That could help other architecture maintainers to decide whether this could be applicable to them as well?You also missed the -mno-unaligned-access in
arch/arm/cpu/armv7m/config.mk, was this on purpose?
I missed it because armv7m has no MMU, so I assumed it doesn't apply there. As it turns out though, it MAY apply: when it runs on memory marked as "Normal" via its MPU / default memory map, unaligned accesses should work too. I will therefore expand the help text and add a similar hook in arch/arm/cpu/armv7m/config.mk for v3.
[...]
Quoted 22 lines
Quoted 22 lines
Quoted 21 lines
Quoted 5 lines
arch/Kconfig +5
-0+ bool + help + Assume that an unaligned access cannot fault by the time U-Boot + proper unpacks an image, and build the decompression libraries + without the architecture's strict-alignment flag on that basis.arch/Kconfig config SUPPORT_LITTLE_ENDIAN · line 45
Quoted 10 lines
arch/Kconfig +10
-0+ + Architectures whose unaligned accesses fault while the MMU is off + pass such a flag to everything, which makes the compiler assemble + each wide unaligned load out of single bytes. That is both slower + and larger, and a decompressor refilling its bit buffer in machine + words can spend up to four times as long for it. + + The assumption holds only once the MMU is on, which on most + platforms has not happened by the time an image is unpacked, and + nothing checks it. Select it from code which enables the MMUCrazy idea, but should we add a flag that specifies whether the MMU (and dcache?) has been set up, and then read that flag in places where we have this assumption? Then we could at least clearly hard fail on this flag not being set and tell the user something went wrong before the system does a HW fault by itself?
Quoted 6 lines
Quoted 4 lines
Do you mean a runtime flag? I'm afraid that will add more global state affecting all platforms, and some platforms with tight SPL size requirements will not love it.
gd->flagswith a new flag in enumgd_flags?
I gave it some further thought, and after all I don't think adding a global flag with a separate panic adds much. If the processor runs into an unaligned load in a context where it cannot work, it will loudly fail with a synchronous abort pointing at the unaligned access exception. It won't silently skip over it, corrupt data, or do anything of that sort. So it is pretty much the same as a panic, and it will occur at exactly the offending location, so the program counter will point directly at the offending instruction for debugging, all with no extra code.
Best regards,
Alexey
Re: [PATCH v2 1/2] lib: Let the decompressors use unaligned loads if platform allows
Hi Alexey,
Quoted 14 lines · Alexey Charkov
Hi Quentin,
Quoted 11 lines · Quentin Schulz
Hi Alexey,
Quoted 7 lines · Alexey Charkov
Hi Quentin,
Quoted 4 lines · Quentin Schulz
Hi Alexey,
On 10/2/26 12:04 PM, Alexey Charkov wrote:
[...]
Quoted 29 lines
Quoted 22 lines
Quoted 22 lines
Quoted 21 lines
Quoted 5 lines
arch/Kconfig +5
-0+ bool + help + Assume that an unaligned access cannot fault by the time U-Boot + proper unpacks an image, and build the decompression libraries + without the architecture's strict-alignment flag on that basis.arch/Kconfig config SUPPORT_LITTLE_ENDIAN · line 45
Quoted 10 lines
arch/Kconfig +10
-0+ + Architectures whose unaligned accesses fault while the MMU is off + pass such a flag to everything, which makes the compiler assemble + each wide unaligned load out of single bytes. That is both slower + and larger, and a decompressor refilling its bit buffer in machine + words can spend up to four times as long for it. + + The assumption holds only once the MMU is on, which on most + platforms has not happened by the time an image is unpacked, and + nothing checks it. Select it from code which enables the MMUCrazy idea, but should we add a flag that specifies whether the MMU (and dcache?) has been set up, and then read that flag in places where we have this assumption? Then we could at least clearly hard fail on this flag not being set and tell the user something went wrong before the system does a HW fault by itself?
Quoted 6 lines
Quoted 4 lines
Do you mean a runtime flag? I'm afraid that will add more global state affecting all platforms, and some platforms with tight SPL size requirements will not love it.
gd->flagswith a new flag in enumgd_flags?
Quoted 10 lines
I gave it some further thought, and after all I don't think adding a global flag with a separate panic adds much. If the processor runs into an unaligned load in a context where it cannot work, it will loudly fail with a synchronous abort pointing at the unaligned access exception. It won't silently skip over it, corrupt data, or do anything of that sort. So it is pretty much the same as a panic, and it will occur at exactly the offending location, so the program counter will point directly at the offending instruction for debugging, all with no extra code.
Well, yes but this is assuming that code compiled without -mstrict-align will *always* be unaligned, which I don't know is true. For some additional context, we've had a number of issues with the Device Tree blob not being 8B aligned for example, and it wouldn't always be the case which made things "fun" to debug :)
I think there's value in reliably crashing the board if we are in a scenario we expect possible failures. Debugging a random unaligned issue may prove difficult as adding debug may move code around and have it align again.
Cheers,
Quentin
PATCH v2 2/2 rockchip: Declare unaligned access safe in the ARM64 SPL
In reply to Alexey Charkovspl_board_init() enables the MMU from ARM64 SPL, and runs before boot_from_devices() loads anything, so the decompressors can use unaligned loads.
Select it under the same condition that code tests before enabling caches, so the two cannot drift apart. SPL only: U-Boot proper is left as it is.
--- arch/arm/mach-rockchip/Kconfig | 5 +++++ 1 file changed, 5 insertions(+)
arch/arm/mach-rockchip/Kconfig +5 -0
@@ -631,11 +631,16 @@ config ROCKCHIP_COMMON_BOARD config SPL_ROCKCHIP_COMMON_BOARD bool "Rockchip SPL common board file" depends on SPL + select SPL_ASSUME_UNALIGNED_ACCESS_OK if ARM64 && !SPL_SYS_DCACHE_OFF help Rockchip SoCs have similar boot process, SPL is mainly in charge of load and boot Trust ATF/U-Boot firmware, and DRAM init if there is no TPL for the board. + On ARM64 it enables the MMU from spl_board_init(), which runs before + any image is loaded, hence the SPL_ASSUME_UNALIGNED_ACCESS_OK above + under the same condition that code tests. + config TPL_ROCKCHIP_COMMON_BOARD bool "Rockchip TPL common board file" depends on TPL
Signature · 1 line
2.55.0
Re: [PATCH v2 2/2] rockchip: Declare unaligned access safe in the ARM64 SPL
Hi Alexey,
Quoted 18 lines · Alexey Charkov
spl_board_init()enables the MMU from ARM64 SPL, and runs beforeboot_from_devices()loads anything, so the decompressors can use unaligned loads.Select it under the same condition that code tests before enabling caches, so the two cannot drift apart. SPL only: U-Boot proper is left as it is.
Signed-off-by Alexey Charkov <[email protected]>--- arch/arm/mach-rockchip/Kconfig | 5 +++++ 1 file changed, 5 insertions(+)arch/arm/mach-rockchip/Kconfig +0
-0
arch/arm/mach-rockchip/Kconfig config ROCKCHIP_COMMON_BOARD · line 640
Quoted 10 lines
arch/arm/mach-rockchip/Kconfig +2
-0@@ -631,11 +631,16 @@ config ROCKCHIP_COMMON_BOARD config SPL_ROCKCHIP_COMMON_BOARD bool "Rockchip SPL common board file" depends on SPL + select SPL_ASSUME_UNALIGNED_ACCESS_OK if ARM64 && !SPL_SYS_DCACHE_OFF help Rockchip SoCs have similar boot process, SPL is mainly in charge of load and boot Trust ATF/U-Boot firmware, and DRAM init if there is no TPL for the board. + On ARM64 it enables the MMU from spl_board_init(), which runs before
No? Only RK3399, anbernic-rgxx3-rk3566, jaguar-rk3588, RK3066, ringneck-px30 have CONFIG_SPL_BOARD_INIT() enabled AND it does not seem to be doing anything related to the MMU. I think you meant board_init_f() from SPL? But then the arch_reserve_mmu+enable_caches is only called when TPL is disabled (note that it is NOT "not in the TPL phase" check, it's whether U-Boot will have a TPL built at all), or SPL_RAM is enabled, which doesn't match the dependencies listed above too. Please add a comment next to the ifdef condition, the calls to arch_reserve_mmu+enable_caches and as a Kconfig comment next to the select that they need to be kept in sync.
Cheers,
Quentin
Re: [PATCH v2 2/2] rockchip: Declare unaligned access safe in the ARM64 SPL
Hi Quentin,
Quoted 31 lines · Quentin Schulz
Hi Alexey,
Quoted 27 lines · Alexey Charkov
spl_board_init()enables the MMU from ARM64 SPL, and runs beforeboot_from_devices()loads anything, so the decompressors can use unaligned loads.Select it under the same condition that code tests before enabling caches, so the two cannot drift apart. SPL only: U-Boot proper is left as it is.
Signed-off-by Alexey Charkov <[email protected]>--- arch/arm/mach-rockchip/Kconfig | 5 +++++ 1 file changed, 5 insertions(+)arch/arm/mach-rockchip/Kconfig +1
-0@@ -631,11 +631,16 @@ config ROCKCHIP_COMMON_BOARD config SPL_ROCKCHIP_COMMON_BOARD bool "Rockchip SPL common board file" depends on SPL + select SPL_ASSUME_UNALIGNED_ACCESS_OK if ARM64 && !SPL_SYS_DCACHE_OFF help Rockchip SoCs have similar boot process, SPL is mainly in charge of load and boot Trust ATF/U-Boot firmware, and DRAM init if there is no TPL for the board.
Quoted 12 lines
arch/arm/mach-rockchip/Kconfig config ROCKCHIP_COMMON_BOARD · line 640
Quoted 1 line
arch/arm/mach-rockchip/Kconfig +1
-0+ On ARM64 it enables the MMU from spl_board_init(), which runs beforeNo? Only RK3399, anbernic-rgxx3-rk3566, jaguar-rk3588, RK3066, ringneck-px30 have
CONFIG_SPL_BOARD_INIT()enabled AND it does not seem to be doing anything related to the MMU. I think you meantboard_init_f()from SPL? But then the arch_reserve_mmu+enable_caches is only called when TPL is disabled (note that it is NOT "not in the TPL phase" check, it's whether U-Boot will have a TPL built at all), orSPL_RAMis enabled, which doesn't match the dependencies listed above too. Please add a comment next to the ifdef condition, the calls to arch_reserve_mmu+enable_caches and as aKconfigcomment next to the select that they need to be kept in sync.
Right, thanks. I think I'll create a new hidden Kconfig symbol which both this function and the unaligned-access logic can test, guaranteeing they never diverge. This will also save the code comment.
FWIW, all in-tree ARM64 boards either have TPL disabled, or SPL_RAM enabled, from what qconfig.py is telling me. A stronger defensive guard is the right thing to have in any case, though.
Best regards,
Alexey