libfdt: A few more size improvements
cover libfdt: A few more size improvements
Hey all,
Over on the U-Boot list we're talking about re-syncing our copy of libfdt with the Linux Kernel again (and so with upstream dtc)[1][2]. While we've worked out most of the size increase with the upgrade, there are still a few places we can do more. This series is three patches. The first patch fixes a comment from back when Simon was iterating on the whole assume framework and one spot wasn't updated. The second patch updates the FDT_RO_PROBE macro to optimize out quicker when we assume a valid DTB and the third patch brings in one more assume check that U-Boot has had for ages, but wasn't upstreamed. I did not see if there was old discussion about it, but it's something U-Boot really needs.
The end result of this series, when applied on top of [2] and some other fixes U-Boot itself needed is that our SPL size shrinks by 12 bytes on aarch64 without LTO.
I've run a meson build and test cycle in dtc and everything continues to pass. I've also thrown all of this through U-Boot and things are fine there as well.
[1]: https://lore.kernel.org/u-boot/[email protected]/
[2]: https://lore.kernel.org/u-boot/[email protected]/
Signature · 1 line
Tom
PATCH 1/3 libfdt: libfdt_internal.h correct final comment in ASSUME block
The value "ASSUME_SANE" was never used in the project here, only ASSUME_PERFECT was used. Update the comment to reflect the correct name.
--- libfdt/libfdt_internal.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
libfdt/libfdt_internal.h +1 -1
@@ -92,7 +92,7 @@ static inline uint64_t fdt64_ld_(const fdt64_t *p)* signature or hash check before using libfdt. * * For situations where security is not a concern it may be safe to enable -* ASSUME_SANE. +* ASSUME_PERFECT. */ enum {/*
Signature · 1 line
2.43.0
Re: [PATCH 1/3] libfdt: libfdt_internal.h correct final comment in ASSUME block
Quoted 5 lines · Tom Rini
The value "
ASSUME_SANE" was never used in the project here, onlyASSUME_PERFECTwas used. Update the comment to reflect the correct name.Fixes 464962489dcc ("Add a way to control the level of checks in the code")Signed-off-by Tom Rini <[email protected]>
Appplied, thanks.
Quoted 21 lines
--- libfdt/libfdt_internal.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)libfdt/libfdt_internal.h +1
-1@@ -92,7 +92,7 @@ static inline uint64_t fdt64_ld_(const fdt64_t *p)* signature or hash check before using libfdt. * * For situations where security is not a concern it may be safe to enable -* ASSUME_SANE. +* ASSUME_PERFECT. */ enum {/* Signature · 1 line
2.43.0
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
PATCH 2/3 libfdt: Improve size savings in FDT_RO_PROBE slightly
In reply to Tom RiniIn the case where we have set FDT_ASSUME_MASK to disable ASSUME_VALID_DTB checks, we can improve the FDT_RO_PROBE macro slightly. The first thing that fdt_ro_probe_() does when we can_assume(VALID_DTB) is true is to return whatever the contents of the totalsize field of the DTB is. Since the FDT_RO_PROBE macro only cares about a negative value there, we can optimize this check such that we are to assume it's a valid DTB, we don't need to do anything here.
---
In the case of U-Boot SPL (and similar very early stages) we are extremely concerned with binary size, and also assume the device tree is valid. This patch here is not a huge savings for us, but every little bit helps when talking about something that impacts more than half our build configurations.
--- libfdt/libfdt_internal.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
libfdt/libfdt_internal.h +7 -5
@@ -11,11 +11,13 @@#define FDT_TAGALIGN (x) (FDT_ALIGN((x), FDT_TAGSIZE))int32_t fdt_ro_probe_ (const void *fdt); -#define FDT_RO_PROBE (fdt) \ - { \ - int32_t totalsize_; \ - if ((totalsize_ = fdt_ro_probe_(fdt)) < 0) \ - return totalsize_; \ +#define FDT_RO_PROBE (fdt) \ + { \ + if (!can_assume(VALID_DTB)) { \ + int32_t totalsize_; \ + if ((totalsize_ = fdt_ro_probe_(fdt)) < 0) \ + return totalsize_; \ + } \ }int fdt_check_node_offset_ (const void *fdt,int offset);
Signature · 1 line
2.43.0
David Gibson
AppliedRe: [PATCH 2/3] libfdt: Improve size savings in FDT_RO_PROBE slightly
Quoted 9 lines · Tom Rini
In the case where we have set
FDT_ASSUME_MASKto disableASSUME_VALID_DTBchecks, we can improve theFDT_RO_PROBEmacro slightly. The first thing thatfdt_ro_probe_()does when wecan_assume(VALID_DTB)is true is to return whatever the contents of the totalsize field of the DTB is. Since theFDT_RO_PROBEmacro only cares about a negative value there, we can optimize this check such that we are to assume it's a valid DTB, we don't need to do anything here.Signed-off-by Tom Rini <[email protected]>
Applied, thanks.
Quoted 37 lines
---
In the case of U-Boot SPL (and similar very early stages) we are extremely concerned with binary size, and also assume the device tree is valid. This patch here is not a huge savings for us, but every little bit helps when talking about something that impacts more than half our build configurations.--- libfdt/libfdt_internal.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)libfdt/libfdt_internal.h +7
-5@@ -11,11 +11,13 @@#define FDT_TAGALIGN (x) (FDT_ALIGN((x), FDT_TAGSIZE))int32_t fdt_ro_probe_ (const void *fdt); -#define FDT_RO_PROBE (fdt) \ - { \ - int32_t totalsize_; \ - if ((totalsize_ = fdt_ro_probe_(fdt)) < 0) \ - return totalsize_; \ +#define FDT_RO_PROBE (fdt) \ + { \ + if (!can_assume(VALID_DTB)) { \ + int32_t totalsize_; \ + if ((totalsize_ = fdt_ro_probe_(fdt)) < 0) \ + return totalsize_; \ + } \ }int fdt_check_node_offset_ (const void *fdt,int offset);Signature · 1 line
2.43.0
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
PATCH 3/3 libfdt: fdt_get_name: Add can_assume(VALID_DTB) check
In reply to Tom RiniIn this function from fdt_ro.c we have (reasonably) some checks of the DTB before we begin work. However, we do this in a way that we cannot make use of the normal FDT_RO_PROBE macro and instead have a direct call to fdt_ro_probe_(). Add a test for !can_assume(VALID_DTB) here first so that in cases where we are assuming a valid DTB we can omit the checks.
---
This is another case which we see in U-Boot SPL, and for the last 6 years now have had the equivalent check. This change ends up being a noticeable size win for us.
--- libfdt/fdt_ro.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
libfdt/fdt_ro.c +2 -2
@@ -306,8 +306,8 @@ const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)const char *nameptr;int err; -if (((err =fdt_ro_probe_ (fdt)) <0 ) - || ((err =fdt_check_node_offset_ (fdt, nodeoffset)) <0 )) +if (!can_assume (VALID_DTB ) && (((err =fdt_ro_probe_ (fdt)) <0 ) + || ((err =fdt_check_node_offset_ (fdt, nodeoffset)) <0 ))) goto fail; nameptr = nh->name;
Signature · 1 line
2.43.0
David Gibson
AppliedRe: [PATCH 3/3] libfdt: fdt_get_name: Add can_assume(VALID_DTB) check
On Tue, Dec 09, 2025 at 03:50:53PM -0600, Tom Rini wrote:
11;rgb:ffff/ffff/ffff> In this function from fdt_ro.c we have (reasonably) some checks of the
Quoted 6 lines
DTB before we begin work. However, we do this in a way that we cannot make use of the normal
FDT_RO_PROBEmacro and instead have a direct call tofdt_ro_probe_(). Add a test for !can_assume(VALID_DTB) here first so that in cases where we are assuming a valid DTB we can omit the checks.Signed-off-by Tom Rini <[email protected]>
Applied, thanks.
Quoted 27 lines
---
This is another case which we see in U-Boot SPL, and for the last 6 years now have had the equivalent check. This change ends up being a noticeable size win for us.--- libfdt/fdt_ro.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)libfdt/fdt_ro.c +2
-2@@ -306,8 +306,8 @@ const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)const char *nameptr;int err; -if (((err =fdt_ro_probe_ (fdt)) <0 ) - || ((err =fdt_check_node_offset_ (fdt, nodeoffset)) <0 )) +if (!can_assume (VALID_DTB ) && (((err =fdt_ro_probe_ (fdt)) <0 ) + || ((err =fdt_check_node_offset_ (fdt, nodeoffset)) <0 ))) goto fail; nameptr = nh->name;Signature · 1 line
2.43.0
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson