Skip to contentSkip to conversation

libfdt: A few more size improvements

PATCH Applied Tom Rini 2 participants 7 messages Dec 10, 2025

lore
Share & tools
lore

Tom Rini

author
#0 lore raw

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
author
#1 lore raw

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.

Fixes 464962489dcc ("Add a way to control the level of checks in the code")
---
 libfdt/libfdt_internal.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libfdt/libfdt_internal.h +1 -1
index b60b5456f596..9eb32394eb79 100644
@@ -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
#2 lore raw

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, only ASSUME_PERFECT was used. Update the comment to reflect the correct name.

Fixes 464962489dcc ("Add a way to control the level of checks in the code")

Appplied, thanks.

Quoted 21 lines
---
 libfdt/libfdt_internal.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libfdt/libfdt_internal.h +1 -1
index b60b5456f596..9eb32394eb79 100644
@@ -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
author
#3 lore raw

PATCH 2/3 libfdt: Improve size savings in FDT_RO_PROBE slightly

In reply to Tom Rini

In 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
index 9eb32394eb79..0e103cafa714 100644
@@ -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
Applied
#4 lore raw

Re: [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_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.

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
index 9eb32394eb79..0e103cafa714 100644
@@ -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
author
#5 lore raw

PATCH 3/3 libfdt: fdt_get_name: Add can_assume(VALID_DTB) check

In reply to Tom Rini

In 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
index b78c4e48f1cb..63494fb7ad90 100644
@@ -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
Applied
#6 lore raw

Re: [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_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.

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
index b78c4e48f1cb..63494fb7ad90 100644
@@ -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