Skip to contentSkip to conversation

libfdt: Remove special handling for unaligned reads

PATCH In review Tom Rini 6 participants 26 messages Jan 17, 2020 – Feb 7, 2020 13 messages not in the archive

lore
Share & tools
lore

Tom Rini

author
#0 lore raw

PATCH 1/1 libfdt: Remove special handling for unaligned reads

6dcb8ba4 "libfdt: Add helpers for accessing unaligned words" introduced changes to support unaligned reads for ARM platforms and 11738cf01f15 "libfdt: Don't use memcpy to handle unaligned reads on ARM" improved the performance of these helpers.

Ultimately however, these helpers should not exist. Unaligned access only occurs when images are forced out of alignment by the user. This unalignment is not supported and introduces problems later on as other parts of the system image are unaligned and they too require alignment.

Revert both of these changes.

---
By way of a little more explanation, looking at the archives it seems that the initial bug reporter said that they had a platform that was using U-Boot and had the "fdt_high=0xffffffff" set in the environment. What that does is to tell U-Boot to not do any of the sanity checks and relocation to ensure alignment that it would normally do because the user knows best. This later came up on the U-Boot list as once the DTB was loaded, Linux is unhappy because it demands correct alignment.

I only realized libfdt had introduced changes here when it was reported that boot time had gotten much slower once we merged this change in. It would be best to just drop it.

---
 fdtget.c        |  2 +-
 libfdt/fdt_ro.c | 18 +++++++++---------
 libfdt/libfdt.h | 33 +--------------------------------
 3 files changed, 11 insertions(+), 42 deletions(-)
fdtget.c +1 -1
index 777582e2d45f..7cee28718cbc 100644
@@ -62,7 +62,7 @@ static int show_cell_list(struct display_info *disp, const char *data, int len,
 	for (i = 0; i < len; i += size, p += size) {
 		if (i)
 			printf(" ");
-		value = size == 4 ? fdt32_ld((const fdt32_t *)p) :
+		value = size == 4 ? fdt32_to_cpu(*(const fdt32_t *)p) :
 			size == 2 ? (*p << 8) | p[1] : *p;
 		printf(fmt, value);
 	}
libfdt/fdt_ro.c +9 -9
index a5c2797cde65..01b4b8579a00 100644
@@ -167,8 +167,8 @@ int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
 	if (!re)
 		return -FDT_ERR_BADOFFSET;
 
-	*address = fdt64_ld(&re->address);
-	*size = fdt64_ld(&re->size);
+	*address = fdt64_to_cpu(re->address);
+	*size = fdt64_to_cpu(re->size);
 	return 0;
 }
 
@@ -178,7 +178,7 @@ int fdt_num_mem_rsv(const void *fdt)
 	const struct fdt_reserve_entry *re;
 
 	for (i = 0; (re = fdt_mem_rsv(fdt, i)) != NULL; i++) {
-		if (fdt64_ld(&re->size) == 0)
+		if (fdt64_to_cpu(re->size) == 0)
 			return i;
 	}
 	return -FDT_ERR_TRUNCATED;
@@ -355,7 +355,7 @@ static const struct fdt_property *fdt_get_property_by_offset_(const void *fdt,
 	prop = fdt_offset_ptr_(fdt, offset);
 
 	if (lenp)
-		*lenp = fdt32_ld(&prop->len);
+		*lenp = fdt32_to_cpu(prop->len);
 
 	return prop;
 }
@@ -392,7 +392,7 @@ static const struct fdt_property *fdt_get_property_namelen_(const void *fdt,
 			offset = -FDT_ERR_INTERNAL;
 			break;
 		}
-		if (fdt_string_eq_(fdt, fdt32_ld(&prop->nameoff),
+		if (fdt_string_eq_(fdt, fdt32_to_cpu(prop->nameoff),
 				   name, namelen)) {
 			if (poffset)
 				*poffset = offset;
@@ -445,7 +445,7 @@ const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
 
 	/* Handle realignment */
 	if (fdt_version(fdt) < 0x10 && (poffset + sizeof(*prop)) % 8 &&
-	    fdt32_ld(&prop->len) >= 8)
+	    fdt32_to_cpu(prop->len) >= 8)
 		return prop->data + 4;
 	return prop->data;
 }
@@ -461,7 +461,7 @@ const void *fdt_getprop_by_offset(const void *fdt, int offset,
 	if (namep) {
 		const char *name;
 		int namelen;
-		name = fdt_get_string(fdt, fdt32_ld(&prop->nameoff),
+		name = fdt_get_string(fdt, fdt32_to_cpu(prop->nameoff),
 				      &namelen);
 		if (!name) {
 			if (lenp)
@@ -473,7 +473,7 @@ const void *fdt_getprop_by_offset(const void *fdt, int offset,
 
 	/* Handle realignment */
 	if (fdt_version(fdt) < 0x10 && (offset + sizeof(*prop)) % 8 &&
-	    fdt32_ld(&prop->len) >= 8)
+	    fdt32_to_cpu(prop->len) >= 8)
 		return prop->data + 4;
 	return prop->data;
 }
@@ -498,7 +498,7 @@ uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
 			return 0;
 	}
 
-	return fdt32_ld(php);
+	return fdt32_to_cpu(*php);
 }
 
 const char *fdt_get_alias_namelen(const void *fdt,
libfdt/libfdt.h +1 -32
index fc4c4962a01c..d4ebe915cf46 100644
@@ -117,23 +117,6 @@ static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
 
 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
 
-/*
- * Alignment helpers:
- *     These helpers access words from a device tree blob.  They're
- *     built to work even with unaligned pointers on platforms (ike
- *     ARM) that don't like unaligned loads and stores
- */
-
-static inline uint32_t fdt32_ld(const fdt32_t *p)
-{
-	const uint8_t *bp = (const uint8_t *)p;
-
-	return ((uint32_t)bp[0] << 24)
-		| ((uint32_t)bp[1] << 16)
-		| ((uint32_t)bp[2] << 8)
-		| bp[3];
-}
-
 static inline void fdt32_st(void *property, uint32_t value)
 {
 	uint8_t *bp = (uint8_t *)property;
@@ -144,20 +127,6 @@ static inline void fdt32_st(void *property, uint32_t value)
 	bp[3] = value & 0xff;
 }
 
-static inline uint64_t fdt64_ld(const fdt64_t *p)
-{
-	const uint8_t *bp = (const uint8_t *)p;
-
-	return ((uint64_t)bp[0] << 56)
-		| ((uint64_t)bp[1] << 48)
-		| ((uint64_t)bp[2] << 40)
-		| ((uint64_t)bp[3] << 32)
-		| ((uint64_t)bp[4] << 24)
-		| ((uint64_t)bp[5] << 16)
-		| ((uint64_t)bp[6] << 8)
-		| bp[7];
-}
-
 static inline void fdt64_st(void *property, uint64_t value)
 {
 	uint8_t *bp = (uint8_t *)property;
@@ -232,7 +201,7 @@ int fdt_next_subnode(const void *fdt, int offset);
 /* General functions                                                  */
 /**********************************************************************/
 #define fdt_get_header(fdt, field) \
-	(fdt32_ld(&((const struct fdt_header *)(fdt))->field))
+	(fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))
 #define fdt_magic(fdt)			(fdt_get_header(fdt, magic))
 #define fdt_totalsize(fdt)		(fdt_get_header(fdt, totalsize))
 #define fdt_off_dt_struct(fdt)		(fdt_get_header(fdt, off_dt_struct))
Signature · 1 line
2.17.1

A message that is not in the archive (sent off-list or not yet received): 20200117153106.29909-1-trini-OWPKS81ov/FWk0Htik3J/[email protected]

Reviewed-by
#2 lore raw
Quoted 23 lines · Tom Rini

6dcb8ba4 "libfdt: Add helpers for accessing unaligned words" introduced changes to support unaligned reads for ARM platforms and 11738cf01f15 "libfdt: Don't use memcpy to handle unaligned reads on ARM" improved the performance of these helpers.

Ultimately however, these helpers should not exist. Unaligned access only occurs when images are forced out of alignment by the user. This unalignment is not supported and introduces problems later on as other parts of the system image are unaligned and they too require alignment.

Revert both of these changes.

---
By way of a little more explanation, looking at the archives it seems that the initial bug reporter said that they had a platform that was using U-Boot and had the "fdt_high=0xffffffff" set in the environment. What that does is to tell U-Boot to not do any of the sanity checks and relocation to ensure alignment that it would normally do because the user knows best. This later came up on the U-Boot list as once the DTB was loaded, Linux is unhappy because it demands correct alignment.

Quoted 8 lines

I only realized libfdt had introduced changes here when it was reported that boot time had gotten much slower once we merged this change in. It would be best to just drop it.

---
 fdtget.c        |  2 +-
 libfdt/fdt_ro.c | 18 +++++++++---------
 libfdt/libfdt.h | 33 +--------------------------------
 3 files changed, 11 insertions(+), 42 deletions(-)

(I note this commit had no test coverage anyway)

#3 lore raw
In reply to a message not in the archive
Quoted 22 lines · Tom Rini

6dcb8ba4 "libfdt: Add helpers for accessing unaligned words" introduced changes to support unaligned reads for ARM platforms and 11738cf01f15 "libfdt: Don't use memcpy to handle unaligned reads on ARM" improved the performance of these helpers.

Ultimately however, these helpers should not exist. Unaligned access only occurs when images are forced out of alignment by the user. This unalignment is not supported and introduces problems later on as other parts of the system image are unaligned and they too require alignment.

Revert both of these changes.

---
By way of a little more explanation, looking at the archives it seems that the initial bug reporter said that they had a platform that was using U-Boot and had the "fdt_high=0xffffffff" set in the environment. What that does is to tell U-Boot to not do any of the sanity checks and relocation to ensure alignment that it would normally do because the user knows best. This later came up on the U-Boot list as once the DTB was loaded, Linux is unhappy because it demands correct alignment.

Quoted 3 lines

I only realized libfdt had introduced changes here when it was reported that boot time had gotten much slower once we merged this change in. It would be best to just drop it.

Hmm. I'm not sure about this. The commit message makes a case for why the unaligned handling isn't necessary, but not a case for why it's bad. Even if handling an unaligned tree isn't a common case, isn't it better to be able to than not?

I gather from the previous discussion that there's a significant performance impact, but that rationale needs to go into the commit message for posterity.

[snip]

Quoted 14 lines
libfdt/libfdt.h +0 -6
index fc4c4962a01c..d4ebe915cf46 100644
@@ -117,23 +117,6 @@ static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
 
 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
 
-/*
- * Alignment helpers:
- *     These helpers access words from a device tree blob.  They're
- *     built to work even with unaligned pointers on platforms (ike
- *     ARM) that don't like unaligned loads and stores
- */

libfdt/libfdt.h static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen) · line 135

Quoted 10 lines
libfdt/libfdt.h +0 -10
-
-static inline uint32_t fdt32_ld(const fdt32_t *p)
-{
-	const uint8_t *bp = (const uint8_t *)p;
-
-	return ((uint32_t)bp[0] << 24)
-		| ((uint32_t)bp[1] << 16)
-		| ((uint32_t)bp[2] << 8)
-		| bp[3];
-}

In particular, I definitely think removing the helpers entirely is a no go. They're now part of the published interface of the library. Even if they're not used for reading the internal tags, they can be used to load integers from within particular properties. Those are frequently unaligned, since properties generally have packed representations.

How much of the performance loss would we get back if we put an actual conditional on an aligned address in the helpers?

Signature · 4 lines
David Gibson			| 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

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#5 lore raw
Quoted 27 lines · David Gibson
Quoted 22 lines · Tom Rini

6dcb8ba4 "libfdt: Add helpers for accessing unaligned words" introduced changes to support unaligned reads for ARM platforms and 11738cf01f15 "libfdt: Don't use memcpy to handle unaligned reads on ARM" improved the performance of these helpers.

Ultimately however, these helpers should not exist. Unaligned access only occurs when images are forced out of alignment by the user. This unalignment is not supported and introduces problems later on as other parts of the system image are unaligned and they too require alignment.

Revert both of these changes.

---
By way of a little more explanation, looking at the archives it seems that the initial bug reporter said that they had a platform that was using U-Boot and had the "fdt_high=0xffffffff" set in the environment. What that does is to tell U-Boot to not do any of the sanity checks and relocation to ensure alignment that it would normally do because the user knows best. This later came up on the U-Boot list as once the DTB was loaded, Linux is unhappy because it demands correct alignment.

Quoted 3 lines

I only realized libfdt had introduced changes here when it was reported that boot time had gotten much slower once we merged this change in. It would be best to just drop it.

Quoted 8 lines

Hmm. I'm not sure about this. The commit message makes a case for why the unaligned handling isn't necessary, but not a case for why it's bad. Even if handling an unaligned tree isn't a common case, isn't it better to be able to than not?

I gather from the previous discussion that there's a significant performance impact, but that rationale needs to go into the commit message for posterity.

I wanted to emphasize that the code simply isn't ever needed, not that it's a performance problem. A performance problem implies that we would keep this, if it was fast enough. That's why people noticed it (it slows things down to an unusable level). But it's functionally wrong.

Quoted 20 lines

[snip]

Quoted 19 lines
libfdt/libfdt.h +0 -11
index fc4c4962a01c..d4ebe915cf46 100644
@@ -117,23 +117,6 @@ static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
 
 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
 
-/*
- * Alignment helpers:
- *     These helpers access words from a device tree blob.  They're
- *     built to work even with unaligned pointers on platforms (ike
- *     ARM) that don't like unaligned loads and stores
- */
-
-static inline uint32_t fdt32_ld(const fdt32_t *p)
-{
-	const uint8_t *bp = (const uint8_t *)p;
-
Quoted 8 lines

libfdt/libfdt.h static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen) · line 135

Quoted 5 lines
libfdt/libfdt.h +0 -5
-	return ((uint32_t)bp[0] << 24)
-		| ((uint32_t)bp[1] << 16)
-		| ((uint32_t)bp[2] << 8)
-		| bp[3];
-}

In particular, I definitely think removing the helpers entirely is a no go. They're now part of the published interface of the library.

Perhaps "mistakes were made" ? I don't think we need to worry about removing an interface here as projects are avoiding upgrading libfdt now (TF-A, formerly ATF) or reverting the change (U-Boot).

Quoted 4 lines

Even if they're not used for reading the internal tags, they can be used to load integers from within particular properties. Those are frequently unaligned, since properties generally have packed representations.

I don't see the relevance. Go back to the initial problem report. It's not "I have a new unique platform I'm using libfdt on and I have problems". It's "I keep jabbing myself with a rusty nail and now I have problems".

Quoted 2 lines

How much of the performance loss would we get back if we put an actual conditional on an aligned address in the helpers?

I don't know but to be very clear, we don't ever need these on ARM. Keep in mind we've been using device trees on ARM for over 10 years at this point.

Signature · 1 line
Tom
#6 lore raw
Quoted 80 lines · Tom Rini
Quoted 27 lines · David Gibson
Quoted 22 lines · Tom Rini

6dcb8ba4 "libfdt: Add helpers for accessing unaligned words" introduced changes to support unaligned reads for ARM platforms and 11738cf01f15 "libfdt: Don't use memcpy to handle unaligned reads on ARM" improved the performance of these helpers.

Ultimately however, these helpers should not exist. Unaligned access only occurs when images are forced out of alignment by the user. This unalignment is not supported and introduces problems later on as other parts of the system image are unaligned and they too require alignment.

Revert both of these changes.

---
By way of a little more explanation, looking at the archives it seems that the initial bug reporter said that they had a platform that was using U-Boot and had the "fdt_high=0xffffffff" set in the environment. What that does is to tell U-Boot to not do any of the sanity checks and relocation to ensure alignment that it would normally do because the user knows best. This later came up on the U-Boot list as once the DTB was loaded, Linux is unhappy because it demands correct alignment.

Quoted 3 lines

I only realized libfdt had introduced changes here when it was reported that boot time had gotten much slower once we merged this change in. It would be best to just drop it.

Quoted 8 lines

Hmm. I'm not sure about this. The commit message makes a case for why the unaligned handling isn't necessary, but not a case for why it's bad. Even if handling an unaligned tree isn't a common case, isn't it better to be able to than not?

I gather from the previous discussion that there's a significant performance impact, but that rationale needs to go into the commit message for posterity.

I wanted to emphasize that the code simply isn't ever needed, not that it's a performance problem. A performance problem implies that we would keep this, if it was fast enough. That's why people noticed it (it slows things down to an unusable level). But it's functionally wrong.

Quoted 20 lines

[snip]

Quoted 19 lines
libfdt/libfdt.h +0 -11
index fc4c4962a01c..d4ebe915cf46 100644
@@ -117,23 +117,6 @@ static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
 
 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
 
-/*
- * Alignment helpers:
- *     These helpers access words from a device tree blob.  They're
- *     built to work even with unaligned pointers on platforms (ike
- *     ARM) that don't like unaligned loads and stores
- */
-
-static inline uint32_t fdt32_ld(const fdt32_t *p)
-{
-	const uint8_t *bp = (const uint8_t *)p;
-
Quoted 8 lines

libfdt/libfdt.h static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen) · line 135

Quoted 5 lines
libfdt/libfdt.h +0 -5
-	return ((uint32_t)bp[0] << 24)
-		| ((uint32_t)bp[1] << 16)
-		| ((uint32_t)bp[2] << 8)
-		| bp[3];
-}

In particular, I definitely think removing the helpers entirely is a no go. They're now part of the published interface of the library.

Perhaps "mistakes were made" ? I don't think we need to worry about removing an interface here as projects are avoiding upgrading libfdt now (TF-A, formerly ATF) or reverting the change (U-Boot).

Quoted 4 lines

Even if they're not used for reading the internal tags, they can be used to load integers from within particular properties. Those are frequently unaligned, since properties generally have packed representations.

Quoted 4 lines

I don't see the relevance. Go back to the initial problem report. It's not "I have a new unique platform I'm using libfdt on and I have problems". It's "I keep jabbing myself with a rusty nail and now I have problems".

The initial report isn't the only relevant thing here. Although it prompted the change, it wasn't the only consideration in making it.

There's also two separate questions here:
1) Do we want byteswapping integer load helpers?
2) Should those handle unaligned accesses?

In working out how to address the (as it turns out, non existent) problem, I realized an abstraction for loading big-endian integers from the blob would be a useful thing in its own right. I also realized that it's a useful thing not just inside the libfdt code, but as an external interface. Callers have always needed to interpret the contents of individual dt properties, and loading integers from them is often a part of that.

I know of people using those fdtXX_ld() helpers right now, so I'm not going to take them away.

For the case of external users we absolutely do need to handle unaligned accesses. There are a number of existing bindings that mix strings and integers in packed format, in a single encoded property. So regardless of the alignment of the whole property, we can easily get unaligned integers in there, and I don't want to expose multiple different helpers for different cases.

Now, we don't *have* to use the same helpers for internal use. We could open code the internal loads, or use a special aligned-only version inside. But using the existing external helpers is the obvious and simple choice.

So, we're back to: I need a case for changing this now, not just a case for claiming it wasn't needed in the first place.

Signature · 4 lines
David Gibson			| 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

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#8 lore raw
Quoted 111 lines · David Gibson
Quoted 80 lines · Tom Rini
Quoted 27 lines · David Gibson
Quoted 22 lines · Tom Rini

6dcb8ba4 "libfdt: Add helpers for accessing unaligned words" introduced changes to support unaligned reads for ARM platforms and 11738cf01f15 "libfdt: Don't use memcpy to handle unaligned reads on ARM" improved the performance of these helpers.

Ultimately however, these helpers should not exist. Unaligned access only occurs when images are forced out of alignment by the user. This unalignment is not supported and introduces problems later on as other parts of the system image are unaligned and they too require alignment.

Revert both of these changes.

---
By way of a little more explanation, looking at the archives it seems that the initial bug reporter said that they had a platform that was using U-Boot and had the "fdt_high=0xffffffff" set in the environment. What that does is to tell U-Boot to not do any of the sanity checks and relocation to ensure alignment that it would normally do because the user knows best. This later came up on the U-Boot list as once the DTB was loaded, Linux is unhappy because it demands correct alignment.

Quoted 3 lines

I only realized libfdt had introduced changes here when it was reported that boot time had gotten much slower once we merged this change in. It would be best to just drop it.

Quoted 8 lines

Hmm. I'm not sure about this. The commit message makes a case for why the unaligned handling isn't necessary, but not a case for why it's bad. Even if handling an unaligned tree isn't a common case, isn't it better to be able to than not?

I gather from the previous discussion that there's a significant performance impact, but that rationale needs to go into the commit message for posterity.

I wanted to emphasize that the code simply isn't ever needed, not that it's a performance problem. A performance problem implies that we would keep this, if it was fast enough. That's why people noticed it (it slows things down to an unusable level). But it's functionally wrong.

Quoted 20 lines

[snip]

Quoted 19 lines
libfdt/libfdt.h +0 -11
index fc4c4962a01c..d4ebe915cf46 100644
@@ -117,23 +117,6 @@ static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
 
 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
 
-/*
- * Alignment helpers:
- *     These helpers access words from a device tree blob.  They're
- *     built to work even with unaligned pointers on platforms (ike
- *     ARM) that don't like unaligned loads and stores
- */
-
-static inline uint32_t fdt32_ld(const fdt32_t *p)
-{
-	const uint8_t *bp = (const uint8_t *)p;
-
Quoted 8 lines

libfdt/libfdt.h static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen) · line 135

Quoted 5 lines
libfdt/libfdt.h +0 -5
-	return ((uint32_t)bp[0] << 24)
-		| ((uint32_t)bp[1] << 16)
-		| ((uint32_t)bp[2] << 8)
-		| bp[3];
-}

In particular, I definitely think removing the helpers entirely is a no go. They're now part of the published interface of the library.

Perhaps "mistakes were made" ? I don't think we need to worry about removing an interface here as projects are avoiding upgrading libfdt now (TF-A, formerly ATF) or reverting the change (U-Boot).

Quoted 4 lines

Even if they're not used for reading the internal tags, they can be used to load integers from within particular properties. Those are frequently unaligned, since properties generally have packed representations.

Quoted 4 lines

I don't see the relevance. Go back to the initial problem report. It's not "I have a new unique platform I'm using libfdt on and I have problems". It's "I keep jabbing myself with a rusty nail and now I have problems".

The initial report isn't the only relevant thing here. Although it prompted the change, it wasn't the only consideration in making it.

There's also two separate questions here:
1) Do we want byteswapping integer load helpers?
2) Should those handle unaligned accesses?

In working out how to address the (as it turns out, non existent) problem, I realized an abstraction for loading big-endian integers from the blob would be a useful thing in its own right. I also realized that it's a useful thing not just inside the libfdt code, but as an external interface. Callers have always needed to interpret the contents of individual dt properties, and loading integers from them is often a part of that.

I know of people using those fdtXX_ld() helpers right now, so I'm not going to take them away.

For the case of external users we absolutely do need to handle unaligned accesses. There are a number of existing bindings that mix strings and integers in packed format, in a single encoded property. So regardless of the alignment of the whole property, we can easily get unaligned integers in there, and I don't want to expose multiple different helpers for different cases.

Quoted 7 lines

Now, we don't *have* to use the same helpers for internal use. We could open code the internal loads, or use a special aligned-only version inside. But using the existing external helpers is the obvious and simple choice.

So, we're back to: I need a case for changing this now, not just a case for claiming it wasn't needed in the first place.

For U-Boot, I'm just going to revert this part of the changes. I would suggest that you look in to some way to fix the "fast path" here to go back to the previous way of working so that other project can continue to use libfdt as well and when callers need to access these helpers and are not otherwise in the fast path can do so.

You're adding visible boot time delay with things the way they exist today. That's not OK. I wouldn't have updated U-Boot with these changes if we had automated timing tests like the TF-A folks do.

Please do let us know if you have any changes to try that might alleviate the overall problem. Thanks!

Signature · 1 line
Tom
#9 lore raw

Hi Tom,

Quoted 127 lines · Tom Rini
Quoted 111 lines · David Gibson
Quoted 80 lines · Tom Rini
Quoted 27 lines · David Gibson
Quoted 22 lines · Tom Rini

6dcb8ba4 "libfdt: Add helpers for accessing unaligned words" introduced changes to support unaligned reads for ARM platforms and 11738cf01f15 "libfdt: Don't use memcpy to handle unaligned reads on ARM" improved the performance of these helpers.

Ultimately however, these helpers should not exist. Unaligned access only occurs when images are forced out of alignment by the user. This unalignment is not supported and introduces problems later on as other parts of the system image are unaligned and they too require alignment.

Revert both of these changes.

---
By way of a little more explanation, looking at the archives it seems that the initial bug reporter said that they had a platform that was using U-Boot and had the "fdt_high=0xffffffff" set in the environment. What that does is to tell U-Boot to not do any of the sanity checks and relocation to ensure alignment that it would normally do because the user knows best. This later came up on the U-Boot list as once the DTB was loaded, Linux is unhappy because it demands correct alignment.

Quoted 3 lines

I only realized libfdt had introduced changes here when it was reported that boot time had gotten much slower once we merged this change in. It would be best to just drop it.

Quoted 8 lines

Hmm. I'm not sure about this. The commit message makes a case for why the unaligned handling isn't necessary, but not a case for why it's bad. Even if handling an unaligned tree isn't a common case, isn't it better to be able to than not?

I gather from the previous discussion that there's a significant performance impact, but that rationale needs to go into the commit message for posterity.

I wanted to emphasize that the code simply isn't ever needed, not that it's a performance problem. A performance problem implies that we would keep this, if it was fast enough. That's why people noticed it (it slows things down to an unusable level). But it's functionally wrong.

Quoted 20 lines

[snip]

Quoted 19 lines
libfdt/libfdt.h +0 -11
index fc4c4962a01c..d4ebe915cf46 100644
@@ -117,23 +117,6 @@ static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
 
 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
 
-/*
- * Alignment helpers:
- *     These helpers access words from a device tree blob.  They're
- *     built to work even with unaligned pointers on platforms (ike
- *     ARM) that don't like unaligned loads and stores
- */
-
-static inline uint32_t fdt32_ld(const fdt32_t *p)
-{
-       const uint8_t *bp = (const uint8_t *)p;
-
Quoted 8 lines

libfdt/libfdt.h static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen) · line 135

Quoted 5 lines
libfdt/libfdt.h +0 -5
-       return ((uint32_t)bp[0] << 24)
-               | ((uint32_t)bp[1] << 16)
-               | ((uint32_t)bp[2] << 8)
-               | bp[3];
-}

In particular, I definitely think removing the helpers entirely is a no go. They're now part of the published interface of the library.

Perhaps "mistakes were made" ? I don't think we need to worry about removing an interface here as projects are avoiding upgrading libfdt now (TF-A, formerly ATF) or reverting the change (U-Boot).

Quoted 4 lines

Even if they're not used for reading the internal tags, they can be used to load integers from within particular properties. Those are frequently unaligned, since properties generally have packed representations.

Quoted 4 lines

I don't see the relevance. Go back to the initial problem report. It's not "I have a new unique platform I'm using libfdt on and I have problems". It's "I keep jabbing myself with a rusty nail and now I have problems".

The initial report isn't the only relevant thing here. Although it prompted the change, it wasn't the only consideration in making it.

There's also two separate questions here:
1) Do we want byteswapping integer load helpers?
2) Should those handle unaligned accesses?

In working out how to address the (as it turns out, non existent) problem, I realized an abstraction for loading big-endian integers from the blob would be a useful thing in its own right. I also realized that it's a useful thing not just inside the libfdt code, but as an external interface. Callers have always needed to interpret the contents of individual dt properties, and loading integers from them is often a part of that.

I know of people using those fdtXX_ld() helpers right now, so I'm not going to take them away.

For the case of external users we absolutely do need to handle unaligned accesses. There are a number of existing bindings that mix strings and integers in packed format, in a single encoded property. So regardless of the alignment of the whole property, we can easily get unaligned integers in there, and I don't want to expose multiple different helpers for different cases.

Quoted 7 lines

Now, we don't *have* to use the same helpers for internal use. We could open code the internal loads, or use a special aligned-only version inside. But using the existing external helpers is the obvious and simple choice.

So, we're back to: I need a case for changing this now, not just a case for claiming it wasn't needed in the first place.

For U-Boot, I'm just going to revert this part of the changes. I would suggest that you look in to some way to fix the "fast path" here to go back to the previous way of working so that other project can continue to use libfdt as well and when callers need to access these helpers and are not otherwise in the fast path can do so.

Quoted 6 lines

You're adding visible boot time delay with things the way they exist today. That's not OK. I wouldn't have updated U-Boot with these changes if we had automated timing tests like the TF-A folks do.

Please do let us know if you have any changes to try that might alleviate the overall problem. Thanks!

For my part I think it is best to revert the change in U-Boot for now. The code difference thus created is small. Hopefully it can be reverted in libfdt, perhaps along the lines that David suggests.

Regards,
Simon

#10 lore raw
In reply to Tom Rini
Quoted 117 lines · Tom Rini
Quoted 116 lines · David Gibson
Quoted 80 lines · Tom Rini
Quoted 27 lines · David Gibson
Quoted 22 lines · Tom Rini

6dcb8ba4 "libfdt: Add helpers for accessing unaligned words" introduced changes to support unaligned reads for ARM platforms and 11738cf01f15 "libfdt: Don't use memcpy to handle unaligned reads on ARM" improved the performance of these helpers.

Ultimately however, these helpers should not exist. Unaligned access only occurs when images are forced out of alignment by the user. This unalignment is not supported and introduces problems later on as other parts of the system image are unaligned and they too require alignment.

Revert both of these changes.

---
By way of a little more explanation, looking at the archives it seems that the initial bug reporter said that they had a platform that was using U-Boot and had the "fdt_high=0xffffffff" set in the environment. What that does is to tell U-Boot to not do any of the sanity checks and relocation to ensure alignment that it would normally do because the user knows best. This later came up on the U-Boot list as once the DTB was loaded, Linux is unhappy because it demands correct alignment.

Quoted 3 lines

I only realized libfdt had introduced changes here when it was reported that boot time had gotten much slower once we merged this change in. It would be best to just drop it.

Quoted 8 lines

Hmm. I'm not sure about this. The commit message makes a case for why the unaligned handling isn't necessary, but not a case for why it's bad. Even if handling an unaligned tree isn't a common case, isn't it better to be able to than not?

I gather from the previous discussion that there's a significant performance impact, but that rationale needs to go into the commit message for posterity.

I wanted to emphasize that the code simply isn't ever needed, not that it's a performance problem. A performance problem implies that we would keep this, if it was fast enough. That's why people noticed it (it slows things down to an unusable level). But it's functionally wrong.

Quoted 20 lines

[snip]

Quoted 19 lines
libfdt/libfdt.h +0 -11
index fc4c4962a01c..d4ebe915cf46 100644
@@ -117,23 +117,6 @@ static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
 
 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
 
-/*
- * Alignment helpers:
- *     These helpers access words from a device tree blob.  They're
- *     built to work even with unaligned pointers on platforms (ike
- *     ARM) that don't like unaligned loads and stores
- */
-
-static inline uint32_t fdt32_ld(const fdt32_t *p)
-{
-	const uint8_t *bp = (const uint8_t *)p;
-
Quoted 8 lines

libfdt/libfdt.h static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen) · line 135

Quoted 5 lines
libfdt/libfdt.h +0 -5
-	return ((uint32_t)bp[0] << 24)
-		| ((uint32_t)bp[1] << 16)
-		| ((uint32_t)bp[2] << 8)
-		| bp[3];
-}

In particular, I definitely think removing the helpers entirely is a no go. They're now part of the published interface of the library.

Perhaps "mistakes were made" ? I don't think we need to worry about removing an interface here as projects are avoiding upgrading libfdt now (TF-A, formerly ATF) or reverting the change (U-Boot).

Quoted 4 lines

Even if they're not used for reading the internal tags, they can be used to load integers from within particular properties. Those are frequently unaligned, since properties generally have packed representations.

Quoted 4 lines

I don't see the relevance. Go back to the initial problem report. It's not "I have a new unique platform I'm using libfdt on and I have problems". It's "I keep jabbing myself with a rusty nail and now I have problems".

The initial report isn't the only relevant thing here. Although it prompted the change, it wasn't the only consideration in making it.

There's also two separate questions here:
1) Do we want byteswapping integer load helpers?
2) Should those handle unaligned accesses?

In working out how to address the (as it turns out, non existent) problem, I realized an abstraction for loading big-endian integers from the blob would be a useful thing in its own right. I also realized that it's a useful thing not just inside the libfdt code, but as an external interface. Callers have always needed to interpret the contents of individual dt properties, and loading integers from them is often a part of that.

I know of people using those fdtXX_ld() helpers right now, so I'm not going to take them away.

For the case of external users we absolutely do need to handle unaligned accesses. There are a number of existing bindings that mix strings and integers in packed format, in a single encoded property. So regardless of the alignment of the whole property, we can easily get unaligned integers in there, and I don't want to expose multiple different helpers for different cases.

Now, we don't *have* to use the same helpers for internal use. We could open code the internal loads, or use a special aligned-only version inside. But using the existing external helpers is the obvious and simple choice.

Quoted 4 lines
Quoted 2 lines

So, we're back to: I need a case for changing this now, not just a case for claiming it wasn't needed in the first place.

For U-Boot, I'm just going to revert this part of the changes. I would

That seems reasonable for the interim.

Quoted 7 lines

suggest that you look in to some way to fix the "fast path" here to go back to the previous way of working so that other project can continue to use libfdt as well and when callers need to access these helpers and are not otherwise in the fast path can do so.

You're adding visible boot time delay with things the way they exist today. That's not OK.

That's a fair point, but you need to make that case in the commit message and merge request, not just expect me to find and collate the arguments from other threads.

Quoted 5 lines

I wouldn't have updated U-Boot with these
changes if we had automated timing tests like the TF-A folks do.

Please do let us know if you have any changes to try that might alleviate the overall problem. Thanks!

I finally got around to looking through Simon Glass's patches for reducing libfdt code size with various "asusmption" flags. I think it's a good concept and close to being ready.

I don't think it's the only thing we want to do, but one thing we could do is to add another ASSUME_ALIGNED flag that will simplify the load helpers.

Signature · 4 lines
David Gibson			| 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

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#12 lore raw
Quoted 130 lines · David Gibson
Quoted 117 lines · Tom Rini
Quoted 116 lines · David Gibson
Quoted 80 lines · Tom Rini
Quoted 27 lines · David Gibson
Quoted 22 lines · Tom Rini

6dcb8ba4 "libfdt: Add helpers for accessing unaligned words" introduced changes to support unaligned reads for ARM platforms and 11738cf01f15 "libfdt: Don't use memcpy to handle unaligned reads on ARM" improved the performance of these helpers.

Ultimately however, these helpers should not exist. Unaligned access only occurs when images are forced out of alignment by the user. This unalignment is not supported and introduces problems later on as other parts of the system image are unaligned and they too require alignment.

Revert both of these changes.

---
By way of a little more explanation, looking at the archives it seems that the initial bug reporter said that they had a platform that was using U-Boot and had the "fdt_high=0xffffffff" set in the environment. What that does is to tell U-Boot to not do any of the sanity checks and relocation to ensure alignment that it would normally do because the user knows best. This later came up on the U-Boot list as once the DTB was loaded, Linux is unhappy because it demands correct alignment.

Quoted 3 lines

I only realized libfdt had introduced changes here when it was reported that boot time had gotten much slower once we merged this change in. It would be best to just drop it.

Quoted 8 lines

Hmm. I'm not sure about this. The commit message makes a case for why the unaligned handling isn't necessary, but not a case for why it's bad. Even if handling an unaligned tree isn't a common case, isn't it better to be able to than not?

I gather from the previous discussion that there's a significant performance impact, but that rationale needs to go into the commit message for posterity.

I wanted to emphasize that the code simply isn't ever needed, not that it's a performance problem. A performance problem implies that we would keep this, if it was fast enough. That's why people noticed it (it slows things down to an unusable level). But it's functionally wrong.

Quoted 20 lines

[snip]

Quoted 19 lines
libfdt/libfdt.h +0 -11
index fc4c4962a01c..d4ebe915cf46 100644
@@ -117,23 +117,6 @@ static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
 
 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
 
-/*
- * Alignment helpers:
- *     These helpers access words from a device tree blob.  They're
- *     built to work even with unaligned pointers on platforms (ike
- *     ARM) that don't like unaligned loads and stores
- */
-
-static inline uint32_t fdt32_ld(const fdt32_t *p)
-{
-	const uint8_t *bp = (const uint8_t *)p;
-
Quoted 8 lines

libfdt/libfdt.h static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen) · line 135

Quoted 5 lines
libfdt/libfdt.h +0 -5
-	return ((uint32_t)bp[0] << 24)
-		| ((uint32_t)bp[1] << 16)
-		| ((uint32_t)bp[2] << 8)
-		| bp[3];
-}

In particular, I definitely think removing the helpers entirely is a no go. They're now part of the published interface of the library.

Perhaps "mistakes were made" ? I don't think we need to worry about removing an interface here as projects are avoiding upgrading libfdt now (TF-A, formerly ATF) or reverting the change (U-Boot).

Quoted 4 lines

Even if they're not used for reading the internal tags, they can be used to load integers from within particular properties. Those are frequently unaligned, since properties generally have packed representations.

Quoted 4 lines

I don't see the relevance. Go back to the initial problem report. It's not "I have a new unique platform I'm using libfdt on and I have problems". It's "I keep jabbing myself with a rusty nail and now I have problems".

The initial report isn't the only relevant thing here. Although it prompted the change, it wasn't the only consideration in making it.

There's also two separate questions here:
1) Do we want byteswapping integer load helpers?
2) Should those handle unaligned accesses?

In working out how to address the (as it turns out, non existent) problem, I realized an abstraction for loading big-endian integers from the blob would be a useful thing in its own right. I also realized that it's a useful thing not just inside the libfdt code, but as an external interface. Callers have always needed to interpret the contents of individual dt properties, and loading integers from them is often a part of that.

I know of people using those fdtXX_ld() helpers right now, so I'm not going to take them away.

For the case of external users we absolutely do need to handle unaligned accesses. There are a number of existing bindings that mix strings and integers in packed format, in a single encoded property. So regardless of the alignment of the whole property, we can easily get unaligned integers in there, and I don't want to expose multiple different helpers for different cases.

Now, we don't *have* to use the same helpers for internal use. We could open code the internal loads, or use a special aligned-only version inside. But using the existing external helpers is the obvious and simple choice.

Quoted 4 lines
Quoted 2 lines

So, we're back to: I need a case for changing this now, not just a case for claiming it wasn't needed in the first place.

For U-Boot, I'm just going to revert this part of the changes. I would

That seems reasonable for the interim.

Quoted 5 lines

suggest that you look in to some way to fix the "fast path" here to go back to the previous way of working so that other project can continue to use libfdt as well and when callers need to access these helpers and are not otherwise in the fast path can do so.

Quoted 6 lines
Quoted 2 lines

You're adding visible boot time delay with things the way they exist today. That's not OK.

That's a fair point, but you need to make that case in the commit message and merge request, not just expect me to find and collate the arguments from other threads.

If you want me to leave the helpers alone but otherwise revert things, I can do a v2 and expand the commit message. And perhaps I'm too nice sometimes then but I do pickup and tweak things that are close enough to what I want and reword as needed for clarity.

I still believe you have things wrong. There's not an unaligned access problem that libfdt needs to care about. ARM doesn't need help handling unaligned accesses. The only problem that's been reported is from when a user got themselves so far off in the weeds that nothing else matters.

Quoted 6 lines
Quoted 5 lines

I wouldn't have updated U-Boot with these
changes if we had automated timing tests like the TF-A folks do.

Please do let us know if you have any changes to try that might alleviate the overall problem. Thanks!

Quoted 7 lines

I finally got around to looking through Simon Glass's patches for reducing libfdt code size with various "asusmption" flags. I think it's a good concept and close to being ready.

I don't think it's the only thing we want to do, but one thing we could do is to add another ASSUME_ALIGNED flag that will simplify the load helpers.

You had mentioned other cases you now see as being possible problems. Can you make these cases fail without these helpers? If so then the assumption flag stuff could be used with the default being the way things were before.

Thanks!

Signature · 1 line
Tom
#13 lore raw
Quoted 135 lines · Tom Rini
Quoted 133 lines · David Gibson
Quoted 117 lines · Tom Rini
Quoted 116 lines · David Gibson
Quoted 80 lines · Tom Rini
Quoted 27 lines · David Gibson
Quoted 22 lines · Tom Rini

6dcb8ba4 "libfdt: Add helpers for accessing unaligned words" introduced changes to support unaligned reads for ARM platforms and 11738cf01f15 "libfdt: Don't use memcpy to handle unaligned reads on ARM" improved the performance of these helpers.

Ultimately however, these helpers should not exist. Unaligned access only occurs when images are forced out of alignment by the user. This unalignment is not supported and introduces problems later on as other parts of the system image are unaligned and they too require alignment.

Revert both of these changes.

---
By way of a little more explanation, looking at the archives it seems that the initial bug reporter said that they had a platform that was using U-Boot and had the "fdt_high=0xffffffff" set in the environment. What that does is to tell U-Boot to not do any of the sanity checks and relocation to ensure alignment that it would normally do because the user knows best. This later came up on the U-Boot list as once the DTB was loaded, Linux is unhappy because it demands correct alignment.

Quoted 3 lines

I only realized libfdt had introduced changes here when it was reported that boot time had gotten much slower once we merged this change in. It would be best to just drop it.

Quoted 8 lines

Hmm. I'm not sure about this. The commit message makes a case for why the unaligned handling isn't necessary, but not a case for why it's bad. Even if handling an unaligned tree isn't a common case, isn't it better to be able to than not?

I gather from the previous discussion that there's a significant performance impact, but that rationale needs to go into the commit message for posterity.

I wanted to emphasize that the code simply isn't ever needed, not that it's a performance problem. A performance problem implies that we would keep this, if it was fast enough. That's why people noticed it (it slows things down to an unusable level). But it's functionally wrong.

Quoted 20 lines

[snip]

Quoted 19 lines
libfdt/libfdt.h +0 -11
index fc4c4962a01c..d4ebe915cf46 100644
@@ -117,23 +117,6 @@ static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
 
 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
 
-/*
- * Alignment helpers:
- *     These helpers access words from a device tree blob.  They're
- *     built to work even with unaligned pointers on platforms (ike
- *     ARM) that don't like unaligned loads and stores
- */
-
-static inline uint32_t fdt32_ld(const fdt32_t *p)
-{
-   const uint8_t *bp = (const uint8_t *)p;
-
Quoted 8 lines

libfdt/libfdt.h static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen) · line 135

Quoted 5 lines
libfdt/libfdt.h +0 -5
-   return ((uint32_t)bp[0] << 24)
-           | ((uint32_t)bp[1] << 16)
-           | ((uint32_t)bp[2] << 8)
-           | bp[3];
-}

In particular, I definitely think removing the helpers entirely is a no go. They're now part of the published interface of the library.

Perhaps "mistakes were made" ? I don't think we need to worry about removing an interface here as projects are avoiding upgrading libfdt now (TF-A, formerly ATF) or reverting the change (U-Boot).

Quoted 4 lines

Even if they're not used for reading the internal tags, they can be used to load integers from within particular properties. Those are frequently unaligned, since properties generally have packed representations.

Quoted 4 lines

I don't see the relevance. Go back to the initial problem report. It's not "I have a new unique platform I'm using libfdt on and I have problems". It's "I keep jabbing myself with a rusty nail and now I have problems".

The initial report isn't the only relevant thing here. Although it prompted the change, it wasn't the only consideration in making it.

There's also two separate questions here:
1) Do we want byteswapping integer load helpers?
2) Should those handle unaligned accesses?

In working out how to address the (as it turns out, non existent) problem, I realized an abstraction for loading big-endian integers from the blob would be a useful thing in its own right. I also realized that it's a useful thing not just inside the libfdt code, but as an external interface. Callers have always needed to interpret the contents of individual dt properties, and loading integers from them is often a part of that.

I know of people using those fdtXX_ld() helpers right now, so I'm not going to take them away.

For the case of external users we absolutely do need to handle unaligned accesses. There are a number of existing bindings that mix strings and integers in packed format, in a single encoded property. So regardless of the alignment of the whole property, we can easily get unaligned integers in there, and I don't want to expose multiple different helpers for different cases.

Now, we don't *have* to use the same helpers for internal use. We could open code the internal loads, or use a special aligned-only version inside. But using the existing external helpers is the obvious and simple choice.

Quoted 4 lines
Quoted 2 lines

So, we're back to: I need a case for changing this now, not just a case for claiming it wasn't needed in the first place.

For U-Boot, I'm just going to revert this part of the changes. I would

That seems reasonable for the interim.

Quoted 7 lines

suggest that you look in to some way to fix the "fast path" here to go back to the previous way of working so that other project can continue to use libfdt as well and when callers need to access these helpers and are not otherwise in the fast path can do so.

You're adding visible boot time delay with things the way they exist today. That's not OK.

Quoted 8 lines
Quoted 3 lines

That's a fair point, but you need to make that case in the commit message and merge request, not just expect me to find and collate the arguments from other threads.

If you want me to leave the helpers alone but otherwise revert things, I can do a v2 and expand the commit message. And perhaps I'm too nice sometimes then but I do pickup and tweak things that are close enough to what I want and reword as needed for clarity.

Why not just fix the helpers for the aligned case and be done with it:

static inline uint32_t fdt32_ld(const fdt32_t *p)

{
       const uint8_t *bp = (const uint8_t *)p;

+ if (!((unsigned long)p & 0x3))
+ return fdt32_to_cpu(*p);
+

       return ((uint32_t)bp[0] << 24)
               | ((uint32_t)bp[1] << 16)
               | ((uint32_t)bp[2] << 8)
               | bp[3];
}
Quoted 4 lines

I still believe you have things wrong. There's not an unaligned access problem that libfdt needs to care about. ARM doesn't need help handling unaligned accesses. The only problem that's been reported is from when a user got themselves so far off in the weeds that nothing else matters.

I think while the vast majority of DTBs don't have anything that would cause unaligned accesses, that's not guaranteed by the FDT format. libfdt needs to handle the worst case.

What about ARMv5 and v4 which don't universally support unaligned accesses or any other architecture. Do all mips, openrisc, riscv, arc, microblaze, xtenza, etc. support unaligned accesses?

Rob

A message that is not in the archive (sent off-list or not yet received): CAL_JsqJ0Pp8e4iFOKuKESO4DJ0ky2aO+GB_HDiGcNhY1qBxgfw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

author
#15 lore raw
Quoted 158 lines · Rob Herring
Quoted 135 lines · Tom Rini
Quoted 133 lines · David Gibson
Quoted 117 lines · Tom Rini
Quoted 116 lines · David Gibson
Quoted 80 lines · Tom Rini
Quoted 27 lines · David Gibson
Quoted 22 lines · Tom Rini

6dcb8ba4 "libfdt: Add helpers for accessing unaligned words" introduced changes to support unaligned reads for ARM platforms and 11738cf01f15 "libfdt: Don't use memcpy to handle unaligned reads on ARM" improved the performance of these helpers.

Ultimately however, these helpers should not exist. Unaligned access only occurs when images are forced out of alignment by the user. This unalignment is not supported and introduces problems later on as other parts of the system image are unaligned and they too require alignment.

Revert both of these changes.

---
By way of a little more explanation, looking at the archives it seems that the initial bug reporter said that they had a platform that was using U-Boot and had the "fdt_high=0xffffffff" set in the environment. What that does is to tell U-Boot to not do any of the sanity checks and relocation to ensure alignment that it would normally do because the user knows best. This later came up on the U-Boot list as once the DTB was loaded, Linux is unhappy because it demands correct alignment.

Quoted 3 lines

I only realized libfdt had introduced changes here when it was reported that boot time had gotten much slower once we merged this change in. It would be best to just drop it.

Quoted 8 lines

Hmm. I'm not sure about this. The commit message makes a case for why the unaligned handling isn't necessary, but not a case for why it's bad. Even if handling an unaligned tree isn't a common case, isn't it better to be able to than not?

I gather from the previous discussion that there's a significant performance impact, but that rationale needs to go into the commit message for posterity.

I wanted to emphasize that the code simply isn't ever needed, not that it's a performance problem. A performance problem implies that we would keep this, if it was fast enough. That's why people noticed it (it slows things down to an unusable level). But it's functionally wrong.

Quoted 20 lines

[snip]

Quoted 19 lines
libfdt/libfdt.h +0 -11
index fc4c4962a01c..d4ebe915cf46 100644
@@ -117,23 +117,6 @@ static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
 
 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
 
-/*
- * Alignment helpers:
- *     These helpers access words from a device tree blob.  They're
- *     built to work even with unaligned pointers on platforms (ike
- *     ARM) that don't like unaligned loads and stores
- */
-
-static inline uint32_t fdt32_ld(const fdt32_t *p)
-{
-   const uint8_t *bp = (const uint8_t *)p;
-
Quoted 8 lines

libfdt/libfdt.h static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen) · line 135

Quoted 5 lines
libfdt/libfdt.h +0 -5
-   return ((uint32_t)bp[0] << 24)
-           | ((uint32_t)bp[1] << 16)
-           | ((uint32_t)bp[2] << 8)
-           | bp[3];
-}

In particular, I definitely think removing the helpers entirely is a no go. They're now part of the published interface of the library.

Perhaps "mistakes were made" ? I don't think we need to worry about removing an interface here as projects are avoiding upgrading libfdt now (TF-A, formerly ATF) or reverting the change (U-Boot).

Quoted 4 lines

Even if they're not used for reading the internal tags, they can be used to load integers from within particular properties. Those are frequently unaligned, since properties generally have packed representations.

Quoted 4 lines

I don't see the relevance. Go back to the initial problem report. It's not "I have a new unique platform I'm using libfdt on and I have problems". It's "I keep jabbing myself with a rusty nail and now I have problems".

The initial report isn't the only relevant thing here. Although it prompted the change, it wasn't the only consideration in making it.

There's also two separate questions here:
1) Do we want byteswapping integer load helpers?
2) Should those handle unaligned accesses?

In working out how to address the (as it turns out, non existent) problem, I realized an abstraction for loading big-endian integers from the blob would be a useful thing in its own right. I also realized that it's a useful thing not just inside the libfdt code, but as an external interface. Callers have always needed to interpret the contents of individual dt properties, and loading integers from them is often a part of that.

I know of people using those fdtXX_ld() helpers right now, so I'm not going to take them away.

For the case of external users we absolutely do need to handle unaligned accesses. There are a number of existing bindings that mix strings and integers in packed format, in a single encoded property. So regardless of the alignment of the whole property, we can easily get unaligned integers in there, and I don't want to expose multiple different helpers for different cases.

Now, we don't *have* to use the same helpers for internal use. We could open code the internal loads, or use a special aligned-only version inside. But using the existing external helpers is the obvious and simple choice.

Quoted 4 lines
Quoted 2 lines

So, we're back to: I need a case for changing this now, not just a case for claiming it wasn't needed in the first place.

For U-Boot, I'm just going to revert this part of the changes. I would

That seems reasonable for the interim.

Quoted 7 lines

suggest that you look in to some way to fix the "fast path" here to go back to the previous way of working so that other project can continue to use libfdt as well and when callers need to access these helpers and are not otherwise in the fast path can do so.

You're adding visible boot time delay with things the way they exist today. That's not OK.

Quoted 8 lines
Quoted 3 lines

That's a fair point, but you need to make that case in the commit message and merge request, not just expect me to find and collate the arguments from other threads.

If you want me to leave the helpers alone but otherwise revert things, I can do a v2 and expand the commit message. And perhaps I'm too nice sometimes then but I do pickup and tweak things that are close enough to what I want and reword as needed for clarity.

Why not just fix the helpers for the aligned case and be done with it:

static inline uint32_t fdt32_ld(const fdt32_t *p)

{
       const uint8_t *bp = (const uint8_t *)p;
(quoted diff) +3 -0
+       if (!((unsigned long)p & 0x3))
+               return fdt32_to_cpu(*p);
+
       return ((uint32_t)bp[0] << 24)
               | ((uint32_t)bp[1] << 16)
               | ((uint32_t)bp[2] << 8)
               | bp[3];
Quoted 1 line

}

I don't know if that was one of the ideas David tried when the problem was reported initially. It would be good to know what the size/performance impact of that is. But still, fast path needs to be fast.

Quoted 8 lines
Quoted 4 lines

I still believe you have things wrong. There's not an unaligned access problem that libfdt needs to care about. ARM doesn't need help handling unaligned accesses. The only problem that's been reported is from when a user got themselves so far off in the weeds that nothing else matters.

I think while the vast majority of DTBs don't have anything that would cause unaligned accesses, that's not guaranteed by the FDT format. libfdt needs to handle the worst case.

It also needs to be useful. Please note that two of the projects using libfdt are holding back upgrading (TF-A,
https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/log/lib/libfdt/fdt.c)
or reverting this change (U-Boot) because this is increasing boot time in visible ways. It's not "you added 100ms". It's "you added 1 second or more".

Quoted 3 lines

What about ARMv5 and v4 which don't universally support unaligned accesses or any other architecture. Do all mips, openrisc, riscv, arc, microblaze, xtenza, etc. support unaligned accesses?

It's been long enough since the last time I was in a what-about discussion over alignment issues that no, I don't recall just how much special casing you need to put in the common paths to handle the uncommon case. My general recollection is that no, we don't need to go all out on this as the cases where unaligned access is fatal (rather than just bad performance in that rare case) is small. It's so small that the problem wasn't found here because someone did that, it's because someone (inadvertently) turned off all the safety and sanity checks and then saw not safe and not sane results.

Thanks!

Signature · 1 line
Tom
#16 lore raw
Quoted 161 lines · Tom Rini
Quoted 158 lines · Rob Herring
Quoted 135 lines · Tom Rini
Quoted 133 lines · David Gibson
Quoted 117 lines · Tom Rini
Quoted 116 lines · David Gibson
Quoted 80 lines · Tom Rini
Quoted 27 lines · David Gibson
Quoted 22 lines · Tom Rini

6dcb8ba4 "libfdt: Add helpers for accessing unaligned words" introduced changes to support unaligned reads for ARM platforms and 11738cf01f15 "libfdt: Don't use memcpy to handle unaligned reads on ARM" improved the performance of these helpers.

Ultimately however, these helpers should not exist. Unaligned access only occurs when images are forced out of alignment by the user. This unalignment is not supported and introduces problems later on as other parts of the system image are unaligned and they too require alignment.

Revert both of these changes.

---
By way of a little more explanation, looking at the archives it seems that the initial bug reporter said that they had a platform that was using U-Boot and had the "fdt_high=0xffffffff" set in the environment. What that does is to tell U-Boot to not do any of the sanity checks and relocation to ensure alignment that it would normally do because the user knows best. This later came up on the U-Boot list as once the DTB was loaded, Linux is unhappy because it demands correct alignment.

Quoted 3 lines

I only realized libfdt had introduced changes here when it was reported that boot time had gotten much slower once we merged this change in. It would be best to just drop it.

Quoted 8 lines

Hmm. I'm not sure about this. The commit message makes a case for why the unaligned handling isn't necessary, but not a case for why it's bad. Even if handling an unaligned tree isn't a common case, isn't it better to be able to than not?

I gather from the previous discussion that there's a significant performance impact, but that rationale needs to go into the commit message for posterity.

I wanted to emphasize that the code simply isn't ever needed, not that it's a performance problem. A performance problem implies that we would keep this, if it was fast enough. That's why people noticed it (it slows things down to an unusable level). But it's functionally wrong.

Quoted 20 lines

[snip]

Quoted 19 lines
libfdt/libfdt.h +0 -11
index fc4c4962a01c..d4ebe915cf46 100644
@@ -117,23 +117,6 @@ static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
 
 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
 
-/*
- * Alignment helpers:
- *     These helpers access words from a device tree blob.  They're
- *     built to work even with unaligned pointers on platforms (ike
- *     ARM) that don't like unaligned loads and stores
- */
-
-static inline uint32_t fdt32_ld(const fdt32_t *p)
-{
-   const uint8_t *bp = (const uint8_t *)p;
-
Quoted 8 lines

libfdt/libfdt.h static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen) · line 135

Quoted 5 lines
libfdt/libfdt.h +0 -5
-   return ((uint32_t)bp[0] << 24)
-           | ((uint32_t)bp[1] << 16)
-           | ((uint32_t)bp[2] << 8)
-           | bp[3];
-}

In particular, I definitely think removing the helpers entirely is a no go. They're now part of the published interface of the library.

Perhaps "mistakes were made" ? I don't think we need to worry about removing an interface here as projects are avoiding upgrading libfdt now (TF-A, formerly ATF) or reverting the change (U-Boot).

Quoted 4 lines

Even if they're not used for reading the internal tags, they can be used to load integers from within particular properties. Those are frequently unaligned, since properties generally have packed representations.

Quoted 4 lines

I don't see the relevance. Go back to the initial problem report. It's not "I have a new unique platform I'm using libfdt on and I have problems". It's "I keep jabbing myself with a rusty nail and now I have problems".

The initial report isn't the only relevant thing here. Although it prompted the change, it wasn't the only consideration in making it.

There's also two separate questions here:
1) Do we want byteswapping integer load helpers?
2) Should those handle unaligned accesses?

In working out how to address the (as it turns out, non existent) problem, I realized an abstraction for loading big-endian integers from the blob would be a useful thing in its own right. I also realized that it's a useful thing not just inside the libfdt code, but as an external interface. Callers have always needed to interpret the contents of individual dt properties, and loading integers from them is often a part of that.

I know of people using those fdtXX_ld() helpers right now, so I'm not going to take them away.

For the case of external users we absolutely do need to handle unaligned accesses. There are a number of existing bindings that mix strings and integers in packed format, in a single encoded property. So regardless of the alignment of the whole property, we can easily get unaligned integers in there, and I don't want to expose multiple different helpers for different cases.

Now, we don't *have* to use the same helpers for internal use. We could open code the internal loads, or use a special aligned-only version inside. But using the existing external helpers is the obvious and simple choice.

Quoted 4 lines
Quoted 2 lines

So, we're back to: I need a case for changing this now, not just a case for claiming it wasn't needed in the first place.

For U-Boot, I'm just going to revert this part of the changes. I would

That seems reasonable for the interim.

Quoted 7 lines

suggest that you look in to some way to fix the "fast path" here to go back to the previous way of working so that other project can continue to use libfdt as well and when callers need to access these helpers and are not otherwise in the fast path can do so.

You're adding visible boot time delay with things the way they exist today. That's not OK.

Quoted 8 lines
Quoted 3 lines

That's a fair point, but you need to make that case in the commit message and merge request, not just expect me to find and collate the arguments from other threads.

If you want me to leave the helpers alone but otherwise revert things, I can do a v2 and expand the commit message. And perhaps I'm too nice sometimes then but I do pickup and tweak things that are close enough to what I want and reword as needed for clarity.

Why not just fix the helpers for the aligned case and be done with it:

static inline uint32_t fdt32_ld(const fdt32_t *p)

{
       const uint8_t *bp = (const uint8_t *)p;
(quoted diff) +3 -0
+       if (!((unsigned long)p & 0x3))
+               return fdt32_to_cpu(*p);
+
       return ((uint32_t)bp[0] << 24)
               | ((uint32_t)bp[1] << 16)
               | ((uint32_t)bp[2] << 8)
               | bp[3];
Quoted 1 line

}

Quoted 4 lines

I don't know if that was one of the ideas David tried when the problem was reported initially. It would be good to know what the size/performance impact of that is. But still, fast path needs to be fast.

I haven't tried this - I don't have easy access to systems to measure the performance impact. Or, rather, I can get access to systems, but I don't really have the bandwidth to prepare a performance testing setup.

I think this is worth a shot. I don't know much the conditional branch will impact the fast path.

If it's significantly better than what we have now, it might be a good interim step, even if it's not the final word.

Quoted 32 lines
Quoted 8 lines
Quoted 4 lines

I still believe you have things wrong. There's not an unaligned access problem that libfdt needs to care about. ARM doesn't need help handling unaligned accesses. The only problem that's been reported is from when a user got themselves so far off in the weeds that nothing else matters.

I think while the vast majority of DTBs don't have anything that would cause unaligned accesses, that's not guaranteed by the FDT format. libfdt needs to handle the worst case.

It also needs to be useful. Please note that two of the projects using libfdt are holding back upgrading (TF-A,
https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/log/lib/libfdt/fdt.c)
or reverting this change (U-Boot) because this is increasing boot time in visible ways. It's not "you added 100ms". It's "you added 1 second or more".

Quoted 3 lines

What about ARMv5 and v4 which don't universally support unaligned accesses or any other architecture. Do all mips, openrisc, riscv, arc, microblaze, xtenza, etc. support unaligned accesses?

It's been long enough since the last time I was in a what-about discussion over alignment issues that no, I don't recall just how much special casing you need to put in the common paths to handle the uncommon case. My general recollection is that no, we don't need to go all out on this as the cases where unaligned access is fatal (rather than just bad performance in that rare case) is small. It's so small that the problem wasn't found here because someone did that, it's because someone (inadvertently) turned off all the safety and sanity checks and then saw not safe and not sane results.

Thanks!

Signature · 4 lines
David Gibson			| 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
#17 lore raw
In reply to a message not in the archive
Quoted 161 lines · Rob Herring
Quoted 135 lines · Tom Rini
Quoted 133 lines · David Gibson
Quoted 117 lines · Tom Rini
Quoted 116 lines · David Gibson
Quoted 80 lines · Tom Rini
Quoted 27 lines · David Gibson
Quoted 22 lines · Tom Rini

6dcb8ba4 "libfdt: Add helpers for accessing unaligned words" introduced changes to support unaligned reads for ARM platforms and 11738cf01f15 "libfdt: Don't use memcpy to handle unaligned reads on ARM" improved the performance of these helpers.

Ultimately however, these helpers should not exist. Unaligned access only occurs when images are forced out of alignment by the user. This unalignment is not supported and introduces problems later on as other parts of the system image are unaligned and they too require alignment.

Revert both of these changes.

---
By way of a little more explanation, looking at the archives it seems that the initial bug reporter said that they had a platform that was using U-Boot and had the "fdt_high=0xffffffff" set in the environment. What that does is to tell U-Boot to not do any of the sanity checks and relocation to ensure alignment that it would normally do because the user knows best. This later came up on the U-Boot list as once the DTB was loaded, Linux is unhappy because it demands correct alignment.

Quoted 3 lines

I only realized libfdt had introduced changes here when it was reported that boot time had gotten much slower once we merged this change in. It would be best to just drop it.

Quoted 8 lines

Hmm. I'm not sure about this. The commit message makes a case for why the unaligned handling isn't necessary, but not a case for why it's bad. Even if handling an unaligned tree isn't a common case, isn't it better to be able to than not?

I gather from the previous discussion that there's a significant performance impact, but that rationale needs to go into the commit message for posterity.

I wanted to emphasize that the code simply isn't ever needed, not that it's a performance problem. A performance problem implies that we would keep this, if it was fast enough. That's why people noticed it (it slows things down to an unusable level). But it's functionally wrong.

Quoted 20 lines

[snip]

Quoted 19 lines
libfdt/libfdt.h +0 -11
index fc4c4962a01c..d4ebe915cf46 100644
@@ -117,23 +117,6 @@ static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
 
 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
 
-/*
- * Alignment helpers:
- *     These helpers access words from a device tree blob.  They're
- *     built to work even with unaligned pointers on platforms (ike
- *     ARM) that don't like unaligned loads and stores
- */
-
-static inline uint32_t fdt32_ld(const fdt32_t *p)
-{
-   const uint8_t *bp = (const uint8_t *)p;
-
Quoted 8 lines

libfdt/libfdt.h static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen) · line 135

Quoted 5 lines
libfdt/libfdt.h +0 -5
-   return ((uint32_t)bp[0] << 24)
-           | ((uint32_t)bp[1] << 16)
-           | ((uint32_t)bp[2] << 8)
-           | bp[3];
-}

In particular, I definitely think removing the helpers entirely is a no go. They're now part of the published interface of the library.

Perhaps "mistakes were made" ? I don't think we need to worry about removing an interface here as projects are avoiding upgrading libfdt now (TF-A, formerly ATF) or reverting the change (U-Boot).

Quoted 4 lines

Even if they're not used for reading the internal tags, they can be used to load integers from within particular properties. Those are frequently unaligned, since properties generally have packed representations.

Quoted 4 lines

I don't see the relevance. Go back to the initial problem report. It's not "I have a new unique platform I'm using libfdt on and I have problems". It's "I keep jabbing myself with a rusty nail and now I have problems".

The initial report isn't the only relevant thing here. Although it prompted the change, it wasn't the only consideration in making it.

There's also two separate questions here:
1) Do we want byteswapping integer load helpers?
2) Should those handle unaligned accesses?

In working out how to address the (as it turns out, non existent) problem, I realized an abstraction for loading big-endian integers from the blob would be a useful thing in its own right. I also realized that it's a useful thing not just inside the libfdt code, but as an external interface. Callers have always needed to interpret the contents of individual dt properties, and loading integers from them is often a part of that.

I know of people using those fdtXX_ld() helpers right now, so I'm not going to take them away.

For the case of external users we absolutely do need to handle unaligned accesses. There are a number of existing bindings that mix strings and integers in packed format, in a single encoded property. So regardless of the alignment of the whole property, we can easily get unaligned integers in there, and I don't want to expose multiple different helpers for different cases.

Now, we don't *have* to use the same helpers for internal use. We could open code the internal loads, or use a special aligned-only version inside. But using the existing external helpers is the obvious and simple choice.

Quoted 4 lines
Quoted 2 lines

So, we're back to: I need a case for changing this now, not just a case for claiming it wasn't needed in the first place.

For U-Boot, I'm just going to revert this part of the changes. I would

That seems reasonable for the interim.

Quoted 7 lines

suggest that you look in to some way to fix the "fast path" here to go back to the previous way of working so that other project can continue to use libfdt as well and when callers need to access these helpers and are not otherwise in the fast path can do so.

You're adding visible boot time delay with things the way they exist today. That's not OK.

Quoted 8 lines
Quoted 3 lines

That's a fair point, but you need to make that case in the commit message and merge request, not just expect me to find and collate the arguments from other threads.

If you want me to leave the helpers alone but otherwise revert things, I can do a v2 and expand the commit message. And perhaps I'm too nice sometimes then but I do pickup and tweak things that are close enough to what I want and reword as needed for clarity.

Why not just fix the helpers for the aligned case and be done with it:

static inline uint32_t fdt32_ld(const fdt32_t *p)

{
       const uint8_t *bp = (const uint8_t *)p;
(quoted diff) +3 -0
+       if (!((unsigned long)p & 0x3))
+               return fdt32_to_cpu(*p);
+
       return ((uint32_t)bp[0] << 24)
               | ((uint32_t)bp[1] << 16)
               | ((uint32_t)bp[2] << 8)
               | bp[3];

}

Quoted 8 lines
Quoted 4 lines

I still believe you have things wrong. There's not an unaligned access problem that libfdt needs to care about. ARM doesn't need help handling unaligned accesses. The only problem that's been reported is from when a user got themselves so far off in the weeds that nothing else matters.

I think while the vast majority of DTBs don't have anything that would cause unaligned accesses, that's not guaranteed by the FDT format. libfdt needs to handle the worst case.

That's true at a few different levels:

How the dtb is loaded isn't within scope for the fdt spec, and if the whole thing is loaded unaligned, obviously stuff within it will be unaligned. We could make it a requirement for libfdt that the dtb be loaded at an aligned addresss, though.

The fdt spec does require that the structure block be at an aligned offset from header, which ensures alignment of the tags (assuming the load address is aligned). However, the content of the properties is again out of scope for the fdt format itself, so bindings can define unaligned things in there.

In addition the structure block only requires 4-byte alignment (and only maintains 4-byte alignment even given a more-aligned start address), which means that 8 byte integers within properties may not be naturally aligned, even if we don't have weird bindings mixing strings and numbers.

Signature · 4 lines
David Gibson			| 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
#18 lore raw
In reply to a message not in the archive
Quoted 3 lines · Rob Herring
Quoted 2 lines · Tom Rini

[...]

Quoted 5 lines
Quoted 4 lines

I still believe you have things wrong. There's not an unaligned access problem that libfdt needs to care about. ARM doesn't need help handling unaligned accesses. The only problem that's been reported is from when a user got themselves so far off in the weeds that nothing else matters.

Quoted 8 lines

I think while the vast majority of DTBs don't have anything that would cause unaligned accesses, that's not guaranteed by the FDT format. libfdt needs to handle the worst case.

What about ARMv5 and v4 which don't universally support unaligned accesses or any other architecture. Do all mips, openrisc, riscv, arc, microblaze, xtenza, etc. support unaligned accesses?

Unaligned support is optional even on armv6 and armv7. For quite a long time freebsd ran armv7 chips with strict alignment (mostly because we had no real need to do otherwise until people started complaining that 3rd-party opensource software often failed on freebsd because it's written with the assumption that the whole world is linux, and linux used relaxed alignment).

-- Ian

A message that is not in the archive (sent off-list or not yet received): b5e2288c5b06419fbfd057b8895ade5c5493cde5.camel-h+KGxgPPiopAfugRpC6u6w@public.gmane.org

#20 lore raw
Quoted 20 lines · Ian Lepore
Quoted 3 lines · Rob Herring
Quoted 2 lines · Tom Rini

[...]

Quoted 5 lines
Quoted 4 lines

I still believe you have things wrong. There's not an unaligned access problem that libfdt needs to care about. ARM doesn't need help handling unaligned accesses. The only problem that's been reported is from when a user got themselves so far off in the weeds that nothing else matters.

Quoted 8 lines

I think while the vast majority of DTBs don't have anything that would cause unaligned accesses, that's not guaranteed by the FDT format. libfdt needs to handle the worst case.

What about ARMv5 and v4 which don't universally support unaligned accesses or any other architecture. Do all mips, openrisc, riscv, arc, microblaze, xtenza, etc. support unaligned accesses?

Quoted 6 lines

Unaligned support is optional even on armv6 and armv7. For quite a long time freebsd ran armv7 chips with strict alignment (mostly because we had no real need to do otherwise until people started complaining that 3rd-party opensource software often failed on freebsd because it's written with the assumption that the whole world is linux, and linux used relaxed alignment).

That's not really correct. See sections L.3.1 and O.3.1 of the Arm ARM for v7-A/R. First, unaligned support is always supported for armv7 and armv6 hardware. In armv6, alignment behavior can follow armv5 or armv6+ behavior (SCTLR.U bit). In armv6k, the armv5 behavior is deprecated. It is possible to configure the processor to fault on misaligned accesses (SCTLR.A bit) which is probably what you meant. However, gcc (and probably others) will themselves generate unaligned accesses for code compiled for armv7 (don't know about armv6). The specific case I remember was for packed structs.

Linux has always supported unaligned accesses for userspace code though that is configurable. I think this is because x86 always did and lots of software only got tested on x86 (less true today). For armv5 and earlier, the kernel had to handle the faults and perform the accesses.

There was a long debate about all of this for u-boot[1]. Though since then, it seems clearing SCTLR.A won out thanks to UEFI which also requires enabling unaligned accesses if the arch supports it (u-boot commit 78f90aaeeccc9e).

Rob

[1] http://u-boot.10912.n7.nabble.com/PATCH-arm-enable-unaligned-access-on-ARMv7-td126385.html

A message that is not in the archive (sent off-list or not yet received): CAL_JsqLYobRrLtF9z3eMt=YneGuxDpvpKU7DQruza-jq4n6B3Q-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#22 lore raw
Quoted 22 lines · Rob Herring
Quoted 21 lines · Ian Lepore
Quoted 3 lines · Rob Herring
Quoted 2 lines · Tom Rini

[...]

Quoted 13 lines
Quoted 4 lines

I still believe you have things wrong. There's not an unaligned access problem that libfdt needs to care about. ARM doesn't need help handling unaligned accesses. The only problem that's been reported is from when a user got themselves so far off in the weeds that nothing else matters.

I think while the vast majority of DTBs don't have anything that would cause unaligned accesses, that's not guaranteed by the FDT format. libfdt needs to handle the worst case.

What about ARMv5 and v4 which don't universally support unaligned accesses or any other architecture. Do all mips, openrisc, riscv, arc, microblaze, xtenza, etc. support unaligned accesses?

Quoted 8 lines
Quoted 6 lines

Unaligned support is optional even on armv6 and armv7. For quite a long time freebsd ran armv7 chips with strict alignment (mostly because we had no real need to do otherwise until people started complaining that 3rd-party opensource software often failed on freebsd because it's written with the assumption that the whole world is linux, and linux used relaxed alignment).

That's not really correct. See sections L.3.1 and O.3.1 of the Arm ARM

Well, no, it IS really correct, but I'm not going to get into a big pissing contest with you about it. I suspect your knowledge of freebsd and the toolchains it uses (which do not involve gcc) aren't up to the task, and the discussion would be nothing but noise on this list.

-- Ian

#23 lore raw
In reply to a message not in the archive

Hi Rob, Tom, David

Quoted 146 lines · Rob Herring
Quoted 125 lines · Tom Rini
Quoted 124 lines · David Gibson
Quoted 106 lines · Tom Rini
Quoted 105 lines · David Gibson
Quoted 71 lines · Tom Rini
Quoted 26 lines · David Gibson
Quoted 22 lines · Tom Rini

6dcb8ba4 "libfdt: Add helpers for accessing unaligned words" introduced changes to support unaligned reads for ARM platforms and 11738cf01f15 "libfdt: Don't use memcpy to handle unaligned reads on ARM" improved the performance of these helpers.

Ultimately however, these helpers should not exist. Unaligned access only occurs when images are forced out of alignment by the user. This unalignment is not supported and introduces problems later on as other parts of the system image are unaligned and they too require alignment.

Revert both of these changes.

---
By way of a little more explanation, looking at the archives it seems that the initial bug reporter said that they had a platform that was using U-Boot and had the "fdt_high=0xffffffff" set in the environment. What that does is to tell U-Boot to not do any of the sanity checks and relocation to ensure alignment that it would normally do because the user knows best. This later came up on the U-Boot list as once the DTB was loaded, Linux is unhappy because it demands correct alignment.

Quoted 3 lines

I only realized libfdt had introduced changes here when it was reported that boot time had gotten much slower once we merged this change in. It would be best to just drop it.

Quoted 8 lines

Hmm. I'm not sure about this. The commit message makes a case for why the unaligned handling isn't necessary, but not a case for why it's bad. Even if handling an unaligned tree isn't a common case, isn't it better to be able to than not?

I gather from the previous discussion that there's a significant performance impact, but that rationale needs to go into the commit message for posterity.

I wanted to emphasize that the code simply isn't ever needed, not that it's a performance problem. A performance problem implies that we would keep this, if it was fast enough. That's why people noticed it (it slows things down to an unusable level). But it's functionally wrong.

Quoted 19 lines

[snip]

Quoted 18 lines
libfdt/libfdt.h +0 -10
index fc4c4962a01c..d4ebe915cf46 100644
@@ -117,23 +117,6 @@ static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
 
 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
 
-/*
- * Alignment helpers:
- *     These helpers access words from a device tree blob.  They're
- *     built to work even with unaligned pointers on platforms (ike
- *     ARM) that don't like unaligned loads and stores
- */
-
-static inline uint32_t fdt32_ld(const fdt32_t *p)
-{
-   const uint8_t *bp = (const uint8_t *)p;
Quoted 8 lines

libfdt/libfdt.h static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen) · line 135

Quoted 6 lines
libfdt/libfdt.h +0 -6
-
-   return ((uint32_t)bp[0] << 24)
-           | ((uint32_t)bp[1] << 16)
-           | ((uint32_t)bp[2] << 8)
-           | bp[3];
-}

In particular, I definitely think removing the helpers entirely is a no go. They're now part of the published interface of the library.

Perhaps "mistakes were made" ? I don't think we need to worry about removing an interface here as projects are avoiding upgrading libfdt now (TF-A, formerly ATF) or reverting the change (U-Boot).

Quoted 8 lines
Quoted 4 lines

Even if they're not used for reading the internal tags, they can be used to load integers from within particular properties. Those are frequently unaligned, since properties generally have packed representations.

I don't see the relevance. Go back to the initial problem report. It's not "I have a new unique platform I'm using libfdt on and I have problems". It's "I keep jabbing myself with a rusty nail and now I have problems".

The initial report isn't the only relevant thing here. Although it prompted the change, it wasn't the only consideration in making it.

There's also two separate questions here:
1) Do we want byteswapping integer load helpers?
2) Should those handle unaligned accesses?

In working out how to address the (as it turns out, non existent) problem, I realized an abstraction for loading big-endian integers from the blob would be a useful thing in its own right. I also realized that it's a useful thing not just inside the libfdt code, but as an external interface. Callers have always needed to interpret the contents of individual dt properties, and loading integers from them is often a part of that.

I know of people using those fdtXX_ld() helpers right now, so I'm not going to take them away.

For the case of external users we absolutely do need to handle unaligned accesses. There are a number of existing bindings that mix strings and integers in packed format, in a single encoded property. So regardless of the alignment of the whole property, we can easily get unaligned integers in there, and I don't want to expose multiple different helpers for different cases.

Quoted 8 lines
Quoted 7 lines

Now, we don't *have* to use the same helpers for internal use. We could open code the internal loads, or use a special aligned-only version inside. But using the existing external helpers is the obvious and simple choice.

So, we're back to: I need a case for changing this now, not just a case for claiming it wasn't needed in the first place.

For U-Boot, I'm just going to revert this part of the changes. I would

That seems reasonable for the interim.

Quoted 7 lines

suggest that you look in to some way to fix the "fast path" here to go back to the previous way of working so that other project can continue to use libfdt as well and when callers need to access these helpers and are not otherwise in the fast path can do so.

You're adding visible boot time delay with things the way they exist today. That's not OK.

Quoted 7 lines
Quoted 3 lines

That's a fair point, but you need to make that case in the commit message and merge request, not just expect me to find and collate the arguments from other threads.

If you want me to leave the helpers alone but otherwise revert things, I can do a v2 and expand the commit message. And perhaps I'm too nice sometimes then but I do pickup and tweak things that are close enough to what I want and reword as needed for clarity.

Why not just fix the helpers for the aligned case and be done with it:

static inline uint32_t fdt32_ld(const fdt32_t *p)

{
       const uint8_t *bp = (const uint8_t *)p;
(quoted diff) +3 -0
+       if (!((unsigned long)p & 0x3))
+               return fdt32_to_cpu(*p);
+
       return ((uint32_t)bp[0] << 24)
               | ((uint32_t)bp[1] << 16)
               | ((uint32_t)bp[2] << 8)
               | bp[3];
Quoted 1 line

}

Here are the results, before and after your proposed patch:

tested tag: V2020.04-RC1

STM32MP> bootstage report
Timer summary in microseconds (12 records):
       Mark    Elapsed  Stage
          0          0  reset
     84,268     84,268  SPL
    962,921    878,653  end SPL
    965,800      2,879  board_init_f
  4,314,348  3,348,548  board_init_r
  4,863,763    549,415  id=64
  4,908,759     44,996  id=65
  4,909,459        700  main_loop
  5,322,309    412,850  id=175

Accumulated time:
                83,284  dm_r
                95,842  dm_spl
             1,502,020  dm_f

-------------------------------------------------------------------------------

tested tag : V2020.04-RC1 + following fdt32_ld patch :

static inline uint32_t fdt32_ld(const fdt32_t *p)
{
       const uint8_t *bp = (const uint8_t *)p;

+       if (!((unsigned long)p & 0x3))
+               return fdt32_to_cpu(*p);
+
       return ((uint32_t)bp[0] << 24)

               | ((uint32_t)bp[1] << 16)
               | ((uint32_t)bp[2] << 8)
               | bp[3];

}

STM32MP> bootstage report
Timer summary in microseconds (12 records):
       Mark    Elapsed  Stage
          0          0  reset
     84,264     84,264  SPL
    959,300    875,036  end SPL
    962,192      2,892  board_init_f
  4,310,598  3,348,406  board_init_r
  4,860,074    549,476  id=64
  4,905,119     45,045  id=65
  4,905,819        700  main_loop
  5,098,636    192,817  id=175

Accumulated time:
                83,202  dm_r
                95,252  dm_spl
             1,501,950  dm_f

There is no gain in board_init_r(), the added alignment test is expensive itself.

Thanks

Patrice

Quoted 14 lines
Quoted 5 lines

I still believe you have things wrong. There's not an unaligned access problem that libfdt needs to care about. ARM doesn't need help handling unaligned accesses. The only problem that's been reported is from when a user got themselves so far off in the weeds that nothing else matters.

I think while the vast majority of DTBs don't have anything that would cause unaligned accesses, that's not guaranteed by the FDT format. libfdt needs to handle the worst case.

What about ARMv5 and v4 which don't universally support unaligned accesses or any other architecture. Do all mips, openrisc, riscv, arc, microblaze, xtenza, etc. support unaligned accesses?

Rob

A message that is not in the archive (sent off-list or not yet received): c56c4206-1be1-1701-1423-1f74f6913bbf-qxv4g6HH51o@public.gmane.org

#25 lore raw
Quoted 207 lines · Patrice CHOTARD

Hi Rob, Tom, David

Quoted 146 lines · Rob Herring
Quoted 125 lines · Tom Rini
Quoted 124 lines · David Gibson
Quoted 106 lines · Tom Rini
Quoted 105 lines · David Gibson
Quoted 71 lines · Tom Rini
Quoted 26 lines · David Gibson
Quoted 22 lines · Tom Rini

6dcb8ba4 "libfdt: Add helpers for accessing unaligned words" introduced changes to support unaligned reads for ARM platforms and 11738cf01f15 "libfdt: Don't use memcpy to handle unaligned reads on ARM" improved the performance of these helpers.

Ultimately however, these helpers should not exist. Unaligned access only occurs when images are forced out of alignment by the user. This unalignment is not supported and introduces problems later on as other parts of the system image are unaligned and they too require alignment.

Revert both of these changes.

---
By way of a little more explanation, looking at the archives it seems that the initial bug reporter said that they had a platform that was using U-Boot and had the "fdt_high=0xffffffff" set in the environment. What that does is to tell U-Boot to not do any of the sanity checks and relocation to ensure alignment that it would normally do because the user knows best. This later came up on the U-Boot list as once the DTB was loaded, Linux is unhappy because it demands correct alignment.

Quoted 3 lines

I only realized libfdt had introduced changes here when it was reported that boot time had gotten much slower once we merged this change in. It would be best to just drop it.

Quoted 8 lines

Hmm. I'm not sure about this. The commit message makes a case for why the unaligned handling isn't necessary, but not a case for why it's bad. Even if handling an unaligned tree isn't a common case, isn't it better to be able to than not?

I gather from the previous discussion that there's a significant performance impact, but that rationale needs to go into the commit message for posterity.

I wanted to emphasize that the code simply isn't ever needed, not that it's a performance problem. A performance problem implies that we would keep this, if it was fast enough. That's why people noticed it (it slows things down to an unusable level). But it's functionally wrong.

Quoted 19 lines

[snip]

Quoted 18 lines
libfdt/libfdt.h +0 -10
index fc4c4962a01c..d4ebe915cf46 100644
@@ -117,23 +117,6 @@ static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
 
 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
 
-/*
- * Alignment helpers:
- *     These helpers access words from a device tree blob.  They're
- *     built to work even with unaligned pointers on platforms (ike
- *     ARM) that don't like unaligned loads and stores
- */
-
-static inline uint32_t fdt32_ld(const fdt32_t *p)
-{
-   const uint8_t *bp = (const uint8_t *)p;
Quoted 8 lines

libfdt/libfdt.h static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen) · line 135

Quoted 6 lines
libfdt/libfdt.h +0 -6
-
-   return ((uint32_t)bp[0] << 24)
-           | ((uint32_t)bp[1] << 16)
-           | ((uint32_t)bp[2] << 8)
-           | bp[3];
-}

In particular, I definitely think removing the helpers entirely is a no go. They're now part of the published interface of the library.

Perhaps "mistakes were made" ? I don't think we need to worry about removing an interface here as projects are avoiding upgrading libfdt now (TF-A, formerly ATF) or reverting the change (U-Boot).

Quoted 8 lines
Quoted 4 lines

Even if they're not used for reading the internal tags, they can be used to load integers from within particular properties. Those are frequently unaligned, since properties generally have packed representations.

I don't see the relevance. Go back to the initial problem report. It's not "I have a new unique platform I'm using libfdt on and I have problems". It's "I keep jabbing myself with a rusty nail and now I have problems".

The initial report isn't the only relevant thing here. Although it prompted the change, it wasn't the only consideration in making it.

There's also two separate questions here:
1) Do we want byteswapping integer load helpers?
2) Should those handle unaligned accesses?

In working out how to address the (as it turns out, non existent) problem, I realized an abstraction for loading big-endian integers from the blob would be a useful thing in its own right. I also realized that it's a useful thing not just inside the libfdt code, but as an external interface. Callers have always needed to interpret the contents of individual dt properties, and loading integers from them is often a part of that.

I know of people using those fdtXX_ld() helpers right now, so I'm not going to take them away.

For the case of external users we absolutely do need to handle unaligned accesses. There are a number of existing bindings that mix strings and integers in packed format, in a single encoded property. So regardless of the alignment of the whole property, we can easily get unaligned integers in there, and I don't want to expose multiple different helpers for different cases.

Quoted 8 lines
Quoted 7 lines

Now, we don't *have* to use the same helpers for internal use. We could open code the internal loads, or use a special aligned-only version inside. But using the existing external helpers is the obvious and simple choice.

So, we're back to: I need a case for changing this now, not just a case for claiming it wasn't needed in the first place.

For U-Boot, I'm just going to revert this part of the changes. I would

That seems reasonable for the interim.

Quoted 7 lines

suggest that you look in to some way to fix the "fast path" here to go back to the previous way of working so that other project can continue to use libfdt as well and when callers need to access these helpers and are not otherwise in the fast path can do so.

You're adding visible boot time delay with things the way they exist today. That's not OK.

Quoted 7 lines
Quoted 3 lines

That's a fair point, but you need to make that case in the commit message and merge request, not just expect me to find and collate the arguments from other threads.

If you want me to leave the helpers alone but otherwise revert things, I can do a v2 and expand the commit message. And perhaps I'm too nice sometimes then but I do pickup and tweak things that are close enough to what I want and reword as needed for clarity.

Why not just fix the helpers for the aligned case and be done with it:

static inline uint32_t fdt32_ld(const fdt32_t *p)

{
       const uint8_t *bp = (const uint8_t *)p;
(quoted diff) +3 -0
+       if (!((unsigned long)p & 0x3))
+               return fdt32_to_cpu(*p);
+
       return ((uint32_t)bp[0] << 24)
               | ((uint32_t)bp[1] << 16)
               | ((uint32_t)bp[2] << 8)
               | bp[3];
Quoted 1 line

}

Here are the results, before and after your proposed patch:

tested tag: V2020.04-RC1

STM32MP> bootstage report
Timer summary in microseconds (12 records):

       Mark    Elapsed  Stage
          0          0  reset
     84,268     84,268  SPL
    962,921    878,653  end SPL
    965,800      2,879  board_init_f
  4,314,348  3,348,548  board_init_r
  4,863,763    549,415  id=64
  4,908,759     44,996  id=65
  4,909,459        700  main_loop
  5,322,309    412,850  id=175

Accumulated time:

                83,284  dm_r
                95,842  dm_spl
             1,502,020  dm_f

-------------------------------------------------------------------------------

tested tag : V2020.04-RC1 + following fdt32_ld patch :

static inline uint32_t fdt32_ld(const fdt32_t *p)

{
       const uint8_t *bp = (const uint8_t *)p;
(quoted diff) +3 -0
+       if (!((unsigned long)p & 0x3))
+               return fdt32_to_cpu(*p);
+
       return ((uint32_t)bp[0] << 24)
               | ((uint32_t)bp[1] << 16)
               | ((uint32_t)bp[2] << 8)
               | bp[3];

}

STM32MP> bootstage report
Timer summary in microseconds (12 records):

       Mark    Elapsed  Stage
          0          0  reset
     84,264     84,264  SPL
    959,300    875,036  end SPL
    962,192      2,892  board_init_f
  4,310,598  3,348,406  board_init_r
  4,860,074    549,476  id=64
  4,905,119     45,045  id=65
  4,905,819        700  main_loop
  5,098,636    192,817  id=175
Quoted 7 lines

Accumulated time:

                83,202  dm_r
                95,252  dm_spl
             1,501,950  dm_f

There is no gain in board_init_r(), the added alignment test is expensive itself.

That's odd. It should be just an AND and a conditional branch added. Those should be a few cycles compared to tens to hundreds of cycles for 4 loads instead of 1.

Doesn't u-boot build with -Os typically? Maybe it's not actually inlining.

Rob

A message that is not in the archive (sent off-list or not yet received): CAL_Jsq+43Opib7vUpZ_VxbJig3gT8crBoL6nw_yC=xOfcsbOvA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

author
#27 lore raw
Quoted 214 lines · Rob Herring
Quoted 213 lines · Patrice CHOTARD

Hi Rob, Tom, David

Quoted 146 lines · Rob Herring
Quoted 125 lines · Tom Rini
Quoted 124 lines · David Gibson
Quoted 106 lines · Tom Rini
Quoted 105 lines · David Gibson
Quoted 71 lines · Tom Rini
Quoted 26 lines · David Gibson
Quoted 22 lines · Tom Rini

6dcb8ba4 "libfdt: Add helpers for accessing unaligned words" introduced changes to support unaligned reads for ARM platforms and 11738cf01f15 "libfdt: Don't use memcpy to handle unaligned reads on ARM" improved the performance of these helpers.

Ultimately however, these helpers should not exist. Unaligned access only occurs when images are forced out of alignment by the user. This unalignment is not supported and introduces problems later on as other parts of the system image are unaligned and they too require alignment.

Revert both of these changes.

---
By way of a little more explanation, looking at the archives it seems that the initial bug reporter said that they had a platform that was using U-Boot and had the "fdt_high=0xffffffff" set in the environment. What that does is to tell U-Boot to not do any of the sanity checks and relocation to ensure alignment that it would normally do because the user knows best. This later came up on the U-Boot list as once the DTB was loaded, Linux is unhappy because it demands correct alignment.

Quoted 3 lines

I only realized libfdt had introduced changes here when it was reported that boot time had gotten much slower once we merged this change in. It would be best to just drop it.

Quoted 8 lines

Hmm. I'm not sure about this. The commit message makes a case for why the unaligned handling isn't necessary, but not a case for why it's bad. Even if handling an unaligned tree isn't a common case, isn't it better to be able to than not?

I gather from the previous discussion that there's a significant performance impact, but that rationale needs to go into the commit message for posterity.

I wanted to emphasize that the code simply isn't ever needed, not that it's a performance problem. A performance problem implies that we would keep this, if it was fast enough. That's why people noticed it (it slows things down to an unusable level). But it's functionally wrong.

Quoted 19 lines

[snip]

Quoted 18 lines
libfdt/libfdt.h +0 -10
index fc4c4962a01c..d4ebe915cf46 100644
@@ -117,23 +117,6 @@ static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
 
 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
 
-/*
- * Alignment helpers:
- *     These helpers access words from a device tree blob.  They're
- *     built to work even with unaligned pointers on platforms (ike
- *     ARM) that don't like unaligned loads and stores
- */
-
-static inline uint32_t fdt32_ld(const fdt32_t *p)
-{
-   const uint8_t *bp = (const uint8_t *)p;
Quoted 8 lines

libfdt/libfdt.h static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen) · line 135

Quoted 6 lines
libfdt/libfdt.h +0 -6
-
-   return ((uint32_t)bp[0] << 24)
-           | ((uint32_t)bp[1] << 16)
-           | ((uint32_t)bp[2] << 8)
-           | bp[3];
-}

In particular, I definitely think removing the helpers entirely is a no go. They're now part of the published interface of the library.

Perhaps "mistakes were made" ? I don't think we need to worry about removing an interface here as projects are avoiding upgrading libfdt now (TF-A, formerly ATF) or reverting the change (U-Boot).

Quoted 8 lines
Quoted 4 lines

Even if they're not used for reading the internal tags, they can be used to load integers from within particular properties. Those are frequently unaligned, since properties generally have packed representations.

I don't see the relevance. Go back to the initial problem report. It's not "I have a new unique platform I'm using libfdt on and I have problems". It's "I keep jabbing myself with a rusty nail and now I have problems".

The initial report isn't the only relevant thing here. Although it prompted the change, it wasn't the only consideration in making it.

There's also two separate questions here:
1) Do we want byteswapping integer load helpers?
2) Should those handle unaligned accesses?

In working out how to address the (as it turns out, non existent) problem, I realized an abstraction for loading big-endian integers from the blob would be a useful thing in its own right. I also realized that it's a useful thing not just inside the libfdt code, but as an external interface. Callers have always needed to interpret the contents of individual dt properties, and loading integers from them is often a part of that.

I know of people using those fdtXX_ld() helpers right now, so I'm not going to take them away.

For the case of external users we absolutely do need to handle unaligned accesses. There are a number of existing bindings that mix strings and integers in packed format, in a single encoded property. So regardless of the alignment of the whole property, we can easily get unaligned integers in there, and I don't want to expose multiple different helpers for different cases.

Quoted 8 lines
Quoted 7 lines

Now, we don't *have* to use the same helpers for internal use. We could open code the internal loads, or use a special aligned-only version inside. But using the existing external helpers is the obvious and simple choice.

So, we're back to: I need a case for changing this now, not just a case for claiming it wasn't needed in the first place.

For U-Boot, I'm just going to revert this part of the changes. I would

That seems reasonable for the interim.

Quoted 7 lines

suggest that you look in to some way to fix the "fast path" here to go back to the previous way of working so that other project can continue to use libfdt as well and when callers need to access these helpers and are not otherwise in the fast path can do so.

You're adding visible boot time delay with things the way they exist today. That's not OK.

Quoted 7 lines
Quoted 3 lines

That's a fair point, but you need to make that case in the commit message and merge request, not just expect me to find and collate the arguments from other threads.

If you want me to leave the helpers alone but otherwise revert things, I can do a v2 and expand the commit message. And perhaps I'm too nice sometimes then but I do pickup and tweak things that are close enough to what I want and reword as needed for clarity.

Why not just fix the helpers for the aligned case and be done with it:

static inline uint32_t fdt32_ld(const fdt32_t *p)

{
       const uint8_t *bp = (const uint8_t *)p;
(quoted diff) +3 -0
+       if (!((unsigned long)p & 0x3))
+               return fdt32_to_cpu(*p);
+
       return ((uint32_t)bp[0] << 24)
               | ((uint32_t)bp[1] << 16)
               | ((uint32_t)bp[2] << 8)
               | bp[3];
Quoted 1 line

}

Here are the results, before and after your proposed patch:

tested tag: V2020.04-RC1

STM32MP> bootstage report
Timer summary in microseconds (12 records):

       Mark    Elapsed  Stage
          0          0  reset
     84,268     84,268  SPL
    962,921    878,653  end SPL
    965,800      2,879  board_init_f
  4,314,348  3,348,548  board_init_r
  4,863,763    549,415  id=64
  4,908,759     44,996  id=65
  4,909,459        700  main_loop
  5,322,309    412,850  id=175

Accumulated time:

                83,284  dm_r
                95,842  dm_spl
             1,502,020  dm_f

-------------------------------------------------------------------------------

tested tag : V2020.04-RC1 + following fdt32_ld patch :

static inline uint32_t fdt32_ld(const fdt32_t *p)

{
       const uint8_t *bp = (const uint8_t *)p;
(quoted diff) +3 -0
+       if (!((unsigned long)p & 0x3))
+               return fdt32_to_cpu(*p);
+
       return ((uint32_t)bp[0] << 24)
               | ((uint32_t)bp[1] << 16)
               | ((uint32_t)bp[2] << 8)
               | bp[3];

}

STM32MP> bootstage report
Timer summary in microseconds (12 records):

       Mark    Elapsed  Stage
          0          0  reset
     84,264     84,264  SPL
    959,300    875,036  end SPL
    962,192      2,892  board_init_f
  4,310,598  3,348,406  board_init_r
  4,860,074    549,476  id=64
  4,905,119     45,045  id=65
  4,905,819        700  main_loop
  5,098,636    192,817  id=175

Accumulated time:

                83,202  dm_r
                95,252  dm_spl
             1,501,950  dm_f
Quoted 7 lines
Quoted 1 line

There is no gain in board_init_r(), the added alignment test is expensive itself.

That's odd. It should be just an AND and a conditional branch added. Those should be a few cycles compared to tens to hundreds of cycles for 4 loads instead of 1.

Doesn't u-boot build with -Os typically? Maybe it's not actually inlining.

Checking everything over, yes, it's been inlined.

Signature · 1 line
Tom
#28 lore raw
In reply to a message not in the archive
Quoted 12 lines · Rob Herring
Quoted 11 lines · Patrice CHOTARD

Hi Rob, Tom, David

Quoted 7 lines · Rob Herring
Quoted 6 lines · Tom Rini
Quoted 5 lines · David Gibson
Quoted 4 lines · Tom Rini
Quoted 3 lines · David Gibson
Quoted 2 lines · Tom Rini
Quoted 1 line · David Gibson

On Fri, Jan 17, 2020 at 10:31:06AM -0500, Tom Rini wrote:

[snip]

Quoted 70 lines
Quoted 70 lines
Quoted 13 lines

Why not just fix the helpers for the aligned case and be done with it:

static inline uint32_t fdt32_ld(const fdt32_t *p)

{
       const uint8_t *bp = (const uint8_t *)p;
(quoted diff) +3 -0
+       if (!((unsigned long)p & 0x3))
+               return fdt32_to_cpu(*p);
+
       return ((uint32_t)bp[0] << 24)
               | ((uint32_t)bp[1] << 16)
               | ((uint32_t)bp[2] << 8)
               | bp[3];
Quoted 1 line

}

Here are the results, before and after your proposed patch:

tested tag: V2020.04-RC1

STM32MP> bootstage report
Timer summary in microseconds (12 records):

       Mark    Elapsed  Stage
          0          0  reset
     84,268     84,268  SPL
    962,921    878,653  end SPL
    965,800      2,879  board_init_f
  4,314,348  3,348,548  board_init_r
  4,863,763    549,415  id=64
  4,908,759     44,996  id=65
  4,909,459        700  main_loop
  5,322,309    412,850  id=175

Accumulated time:

                83,284  dm_r
                95,842  dm_spl
             1,502,020  dm_f

-------------------------------------------------------------------------------

tested tag : V2020.04-RC1 + following fdt32_ld patch :

static inline uint32_t fdt32_ld(const fdt32_t *p)

{
       const uint8_t *bp = (const uint8_t *)p;
(quoted diff) +3 -0
+       if (!((unsigned long)p & 0x3))
+               return fdt32_to_cpu(*p);
+
       return ((uint32_t)bp[0] << 24)
               | ((uint32_t)bp[1] << 16)
               | ((uint32_t)bp[2] << 8)
               | bp[3];

}

STM32MP> bootstage report
Timer summary in microseconds (12 records):

       Mark    Elapsed  Stage
          0          0  reset
     84,264     84,264  SPL
    959,300    875,036  end SPL
    962,192      2,892  board_init_f
  4,310,598  3,348,406  board_init_r
  4,860,074    549,476  id=64
  4,905,119     45,045  id=65
  4,905,819        700  main_loop
  5,098,636    192,817  id=175
Quoted 7 lines
Quoted 7 lines

Accumulated time:

                83,202  dm_r
                95,252  dm_spl
             1,501,950  dm_f

There is no gain in board_init_r(), the added alignment test is expensive itself.

Drat.

Quoted 3 lines

That's odd. It should be just an AND and a conditional branch added. Those should be a few cycles compared to tens to hundreds of cycles for 4 loads instead of 1.

The later loads are very likely to hit in cache though, right? Or is this done before the caches are activated?

Quoted 2 lines

Doesn't u-boot build with -Os typically? Maybe it's not actually inlining.

Apparently it is. And yet... it seems pretty suspicious that the numbers are so close, despite doing something quite different here. Can we somehow verify that the fast path is really being executed?

For comparison purposes, what are the numbers if we change these to unconditionally do an aligned load?

Signature · 4 lines
David Gibson			| 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

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#30 lore raw
Quoted 104 lines · David Gibson
Quoted 12 lines · Rob Herring
Quoted 11 lines · Patrice CHOTARD

Hi Rob, Tom, David

Quoted 7 lines · Rob Herring
Quoted 6 lines · Tom Rini
Quoted 5 lines · David Gibson
Quoted 4 lines · Tom Rini
Quoted 3 lines · David Gibson
Quoted 2 lines · Tom Rini
Quoted 1 line · David Gibson

On Fri, Jan 17, 2020 at 10:31:06AM -0500, Tom Rini wrote:

[snip]

Quoted 70 lines
Quoted 70 lines
Quoted 13 lines

Why not just fix the helpers for the aligned case and be done with it:

static inline uint32_t fdt32_ld(const fdt32_t *p)

{
       const uint8_t *bp = (const uint8_t *)p;
(quoted diff) +3 -0
+       if (!((unsigned long)p & 0x3))
+               return fdt32_to_cpu(*p);
+
       return ((uint32_t)bp[0] << 24)
               | ((uint32_t)bp[1] << 16)
               | ((uint32_t)bp[2] << 8)
               | bp[3];
Quoted 1 line

}

Here are the results, before and after your proposed patch:

tested tag: V2020.04-RC1

STM32MP> bootstage report
Timer summary in microseconds (12 records):

       Mark    Elapsed  Stage
          0          0  reset
     84,268     84,268  SPL
    962,921    878,653  end SPL
    965,800      2,879  board_init_f
  4,314,348  3,348,548  board_init_r
  4,863,763    549,415  id=64
  4,908,759     44,996  id=65
  4,909,459        700  main_loop
  5,322,309    412,850  id=175

Accumulated time:

                83,284  dm_r
                95,842  dm_spl
             1,502,020  dm_f

-------------------------------------------------------------------------------

tested tag : V2020.04-RC1 + following fdt32_ld patch :

static inline uint32_t fdt32_ld(const fdt32_t *p)

{
       const uint8_t *bp = (const uint8_t *)p;
(quoted diff) +3 -0
+       if (!((unsigned long)p & 0x3))
+               return fdt32_to_cpu(*p);
+
       return ((uint32_t)bp[0] << 24)
               | ((uint32_t)bp[1] << 16)
               | ((uint32_t)bp[2] << 8)
               | bp[3];

}

STM32MP> bootstage report
Timer summary in microseconds (12 records):

       Mark    Elapsed  Stage
          0          0  reset
     84,264     84,264  SPL
    959,300    875,036  end SPL
    962,192      2,892  board_init_f
  4,310,598  3,348,406  board_init_r
  4,860,074    549,476  id=64
  4,905,119     45,045  id=65
  4,905,819        700  main_loop
  5,098,636    192,817  id=175
Quoted 7 lines
Quoted 7 lines

Accumulated time:

                83,202  dm_r
                95,252  dm_spl
             1,501,950  dm_f

There is no gain in board_init_r(), the added alignment test is expensive itself.

Drat.

Quoted 3 lines

That's odd. It should be just an AND and a conditional branch added. Those should be a few cycles compared to tens to hundreds of cycles for 4 loads instead of 1.

The later loads are very likely to hit in cache though, right? Or is this done before the caches are activated?

Quoted 2 lines

Doesn't u-boot build with -Os typically? Maybe it's not actually inlining.

Quoted 6 lines

Apparently it is. And yet... it seems pretty suspicious that the numbers are so close, despite doing something quite different here. Can we somehow verify that the fast path is really being executed?

For comparison purposes, what are the numbers if we change these to unconditionally do an aligned load?

The before / after numbers can be seen at
https://lists.denx.de/pipermail/u-boot/2020-January/396707.html

Signature · 1 line
Tom
#31 lore raw
In reply to a message not in the archive

Hi

Quoted 97 lines · David Gibson
Quoted 11 lines · Rob Herring
Quoted 10 lines · Patrice CHOTARD

Hi Rob, Tom, David

Quoted 7 lines · Rob Herring
Quoted 6 lines · Tom Rini
Quoted 5 lines · David Gibson
Quoted 4 lines · Tom Rini
Quoted 3 lines · David Gibson
Quoted 2 lines · Tom Rini
Quoted 1 line · David Gibson

On Fri, Jan 17, 2020 at 10:31:06AM -0500, Tom Rini wrote:

[snip]

Quoted 69 lines
Quoted 69 lines
Quoted 13 lines

Why not just fix the helpers for the aligned case and be done with it:

static inline uint32_t fdt32_ld(const fdt32_t *p)

{
       const uint8_t *bp = (const uint8_t *)p;
(quoted diff) +3 -0
+       if (!((unsigned long)p & 0x3))
+               return fdt32_to_cpu(*p);
+
       return ((uint32_t)bp[0] << 24)
               | ((uint32_t)bp[1] << 16)
               | ((uint32_t)bp[2] << 8)
               | bp[3];
Quoted 1 line

}

Here are the results, before and after your proposed patch:

tested tag: V2020.04-RC1

STM32MP> bootstage report
Timer summary in microseconds (12 records):

       Mark    Elapsed  Stage
          0          0  reset
     84,268     84,268  SPL
    962,921    878,653  end SPL
    965,800      2,879  board_init_f
  4,314,348  3,348,548  board_init_r
  4,863,763    549,415  id=64
  4,908,759     44,996  id=65
  4,909,459        700  main_loop
  5,322,309    412,850  id=175

Accumulated time:

                83,284  dm_r
                95,842  dm_spl
             1,502,020  dm_f

-------------------------------------------------------------------------------

tested tag : V2020.04-RC1 + following fdt32_ld patch :

static inline uint32_t fdt32_ld(const fdt32_t *p)

{
       const uint8_t *bp = (const uint8_t *)p;
(quoted diff) +3 -0
+       if (!((unsigned long)p & 0x3))
+               return fdt32_to_cpu(*p);
+
       return ((uint32_t)bp[0] << 24)
               | ((uint32_t)bp[1] << 16)
               | ((uint32_t)bp[2] << 8)
               | bp[3];

}

STM32MP> bootstage report
Timer summary in microseconds (12 records):

       Mark    Elapsed  Stage
          0          0  reset
     84,264     84,264  SPL
    959,300    875,036  end SPL
    962,192      2,892  board_init_f
  4,310,598  3,348,406  board_init_r
  4,860,074    549,476  id=64
  4,905,119     45,045  id=65
  4,905,819        700  main_loop
  5,098,636    192,817  id=175
Quoted 7 lines
Quoted 7 lines

Accumulated time:

                83,202  dm_r
                95,252  dm_spl
             1,501,950  dm_f

There is no gain in board_init_r(), the added alignment test is expensive itself.

Drat.

Quoted 3 lines

That's odd. It should be just an AND and a conditional branch added. Those should be a few cycles compared to tens to hundreds of cycles for 4 loads instead of 1.

The later loads are very likely to hit in cache though, right? Or is this done before the caches are activated?

Quoted 9 lines
Quoted 2 lines

Doesn't u-boot build with -Os typically? Maybe it's not actually inlining.

Apparently it is. And yet... it seems pretty suspicious that the numbers are so close, despite doing something quite different here. Can we somehow verify that the fast path is really being executed?

For comparison purposes, what are the numbers if we change these to unconditionally do an aligned load?

I made one more test to be 100% sure and the result is weird ....

static inline uint32_t fdt32_ld(const fdt32_t *p)
{

       if (!((unsigned long)p & 0x3))
             return fdt32_to_cpu(*p);

       while (1) {};
}

the results are back to 'normal', board_init_r() execution is 1.9 second  faster .....

STM32MP> bootstage report
Timer summary in microseconds (12 records):
       Mark    Elapsed  Stage
          0          0  reset
     84,265     84,265  SPL
    868,330    784,065  end SPL
    871,163      2,833  board_init_f
  2,360,135  1,488,972  board_init_r
  2,786,920    426,785  id=64
  2,818,189     31,269  id=65
  2,818,889        700  main_loop
  2,877,661     58,772  id=175

Accumulated time:
                54,489  dm_r
                56,778  dm_spl
               644,883  dm_f

Is the compiler doing some optimization link to the CPU pipeline prediction or something else ?

Patrice

A message that is not in the archive (sent off-list or not yet received): 7bbbb5a8-8e35-e95c-8d03-6970fd6b023e-qxv4g6HH51o@public.gmane.org

#33 lore raw
Quoted 114 lines · Patrice CHOTARD

Hi

Quoted 97 lines · David Gibson
Quoted 11 lines · Rob Herring
Quoted 10 lines · Patrice CHOTARD

Hi Rob, Tom, David

Quoted 7 lines · Rob Herring
Quoted 6 lines · Tom Rini
Quoted 5 lines · David Gibson
Quoted 4 lines · Tom Rini
Quoted 3 lines · David Gibson
Quoted 2 lines · Tom Rini
Quoted 1 line · David Gibson

On Fri, Jan 17, 2020 at 10:31:06AM -0500, Tom Rini wrote:

[snip]

Quoted 69 lines
Quoted 69 lines
Quoted 13 lines

Why not just fix the helpers for the aligned case and be done with it:

static inline uint32_t fdt32_ld(const fdt32_t *p)

{
       const uint8_t *bp = (const uint8_t *)p;
(quoted diff) +3 -0
+       if (!((unsigned long)p & 0x3))
+               return fdt32_to_cpu(*p);
+
       return ((uint32_t)bp[0] << 24)
               | ((uint32_t)bp[1] << 16)
               | ((uint32_t)bp[2] << 8)
               | bp[3];
Quoted 1 line

}

Here are the results, before and after your proposed patch:

tested tag: V2020.04-RC1

STM32MP> bootstage report
Timer summary in microseconds (12 records):

       Mark    Elapsed  Stage
          0          0  reset
     84,268     84,268  SPL
    962,921    878,653  end SPL
    965,800      2,879  board_init_f
  4,314,348  3,348,548  board_init_r
  4,863,763    549,415  id=64
  4,908,759     44,996  id=65
  4,909,459        700  main_loop
  5,322,309    412,850  id=175

Accumulated time:

                83,284  dm_r
                95,842  dm_spl
             1,502,020  dm_f

-------------------------------------------------------------------------------

tested tag : V2020.04-RC1 + following fdt32_ld patch :

static inline uint32_t fdt32_ld(const fdt32_t *p)

{
       const uint8_t *bp = (const uint8_t *)p;
(quoted diff) +3 -0
+       if (!((unsigned long)p & 0x3))
+               return fdt32_to_cpu(*p);
+
       return ((uint32_t)bp[0] << 24)
               | ((uint32_t)bp[1] << 16)
               | ((uint32_t)bp[2] << 8)
               | bp[3];

}

STM32MP> bootstage report
Timer summary in microseconds (12 records):

       Mark    Elapsed  Stage
          0          0  reset
     84,264     84,264  SPL
    959,300    875,036  end SPL
    962,192      2,892  board_init_f
  4,310,598  3,348,406  board_init_r
  4,860,074    549,476  id=64
  4,905,119     45,045  id=65
  4,905,819        700  main_loop
  5,098,636    192,817  id=175
Quoted 7 lines
Quoted 7 lines

Accumulated time:

                83,202  dm_r
                95,252  dm_spl
             1,501,950  dm_f

There is no gain in board_init_r(), the added alignment test is expensive itself.

Drat.

Quoted 3 lines

That's odd. It should be just an AND and a conditional branch added. Those should be a few cycles compared to tens to hundreds of cycles for 4 loads instead of 1.

The later loads are very likely to hit in cache though, right? Or is this done before the caches are activated?

Quoted 9 lines
Quoted 2 lines

Doesn't u-boot build with -Os typically? Maybe it's not actually inlining.

Apparently it is. And yet... it seems pretty suspicious that the numbers are so close, despite doing something quite different here. Can we somehow verify that the fast path is really being executed?

For comparison purposes, what are the numbers if we change these to unconditionally do an aligned load?

I made one more test to be 100% sure and the result is weird ....

static inline uint32_t fdt32_ld(const fdt32_t *p)
{

Quoted 7 lines

       if (!((unsigned long)p & 0x3))
             return fdt32_to_cpu(*p);

       while (1) {};
}

the results are back to 'normal', board_init_r() execution is 1.9 second  faster .....

Huh. I dunno how, but it definitely seems like the conditional version wasn't operating properly.

Does putting a "likely" branch prediction on that if make any difference?

Quoted 21 lines

STM32MP> bootstage report
Timer summary in microseconds (12 records):
       Mark    Elapsed  Stage
          0          0  reset
     84,265     84,265  SPL
    868,330    784,065  end SPL
    871,163      2,833  board_init_f
  2,360,135  1,488,972  board_init_r
  2,786,920    426,785  id=64
  2,818,189     31,269  id=65
  2,818,889        700  main_loop
  2,877,661     58,772  id=175

Accumulated time:
                54,489  dm_r
                56,778  dm_spl
               644,883  dm_f

Is the compiler doing some optimization link to the CPU pipeline prediction or something else ?

Patrice

Signature · 4 lines
David Gibson			| 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

A message that is not in the archive (sent off-list or not yet received): [email protected]

#35 lore raw

Hi David

Quoted 117 lines · David Gibson
Quoted 116 lines · Patrice CHOTARD

Hi

Quoted 96 lines · David Gibson
Quoted 11 lines · Rob Herring
Quoted 10 lines · Patrice CHOTARD

Hi Rob, Tom, David

Quoted 7 lines · Rob Herring
Quoted 6 lines · Tom Rini
Quoted 5 lines · David Gibson
Quoted 4 lines · Tom Rini
Quoted 3 lines · David Gibson
Quoted 2 lines · Tom Rini
Quoted 1 line · David Gibson

On Fri, Jan 17, 2020 at 10:31:06AM -0500, Tom Rini wrote:

[snip]

Quoted 68 lines
Quoted 68 lines
Quoted 13 lines

Why not just fix the helpers for the aligned case and be done with it:

static inline uint32_t fdt32_ld(const fdt32_t *p)

{
       const uint8_t *bp = (const uint8_t *)p;
(quoted diff) +3 -0
+       if (!((unsigned long)p & 0x3))
+               return fdt32_to_cpu(*p);
+
       return ((uint32_t)bp[0] << 24)
               | ((uint32_t)bp[1] << 16)
               | ((uint32_t)bp[2] << 8)
               | bp[3];
Quoted 1 line

}

Here are the results, before and after your proposed patch:

tested tag: V2020.04-RC1

STM32MP> bootstage report
Timer summary in microseconds (12 records):

       Mark    Elapsed  Stage
          0          0  reset
     84,268     84,268  SPL
    962,921    878,653  end SPL
    965,800      2,879  board_init_f
  4,314,348  3,348,548  board_init_r
  4,863,763    549,415  id=64
  4,908,759     44,996  id=65
  4,909,459        700  main_loop
  5,322,309    412,850  id=175

Accumulated time:

                83,284  dm_r
                95,842  dm_spl
             1,502,020  dm_f

-------------------------------------------------------------------------------

tested tag : V2020.04-RC1 + following fdt32_ld patch :

static inline uint32_t fdt32_ld(const fdt32_t *p)

{
       const uint8_t *bp = (const uint8_t *)p;
(quoted diff) +3 -0
+       if (!((unsigned long)p & 0x3))
+               return fdt32_to_cpu(*p);
+
       return ((uint32_t)bp[0] << 24)
               | ((uint32_t)bp[1] << 16)
               | ((uint32_t)bp[2] << 8)
               | bp[3];

}

STM32MP> bootstage report
Timer summary in microseconds (12 records):

       Mark    Elapsed  Stage
          0          0  reset
     84,264     84,264  SPL
    959,300    875,036  end SPL
    962,192      2,892  board_init_f
  4,310,598  3,348,406  board_init_r
  4,860,074    549,476  id=64
  4,905,119     45,045  id=65
  4,905,819        700  main_loop
  5,098,636    192,817  id=175
Quoted 7 lines
Quoted 7 lines

Accumulated time:

                83,202  dm_r
                95,252  dm_spl
             1,501,950  dm_f

There is no gain in board_init_r(), the added alignment test is expensive itself.

Drat.

Quoted 3 lines

That's odd. It should be just an AND and a conditional branch added. Those should be a few cycles compared to tens to hundreds of cycles for 4 loads instead of 1.

The later loads are very likely to hit in cache though, right? Or is this done before the caches are activated?

Quoted 9 lines
Quoted 2 lines

Doesn't u-boot build with -Os typically? Maybe it's not actually inlining.

Apparently it is. And yet... it seems pretty suspicious that the numbers are so close, despite doing something quite different here. Can we somehow verify that the fast path is really being executed?

For comparison purposes, what are the numbers if we change these to unconditionally do an aligned load?

I made one more test to be 100% sure and the result is weird ....

static inline uint32_t fdt32_ld(const fdt32_t *p)
{

       if (!((unsigned long)p & 0x3))
             return fdt32_to_cpu(*p);

Quoted 8 lines
Quoted 4 lines

       while (1) {};
}

the results are back to 'normal', board_init_r() execution is 1.9 second  faster .....

Huh. I dunno how, but it definitely seems like the conditional version wasn't operating properly.

Does putting a "likely" branch prediction on that if make any difference?

I already did this test by adding a "likely" but it has no effect :-(

Patrice

Quoted 22 lines
Quoted 22 lines

STM32MP> bootstage report
Timer summary in microseconds (12 records):
       Mark    Elapsed  Stage
          0          0  reset
     84,265     84,265  SPL
    868,330    784,065  end SPL
    871,163      2,833  board_init_f
  2,360,135  1,488,972  board_init_r
  2,786,920    426,785  id=64
  2,818,189     31,269  id=65
  2,818,889        700  main_loop
  2,877,661     58,772  id=175

Accumulated time:
                54,489  dm_r
                56,778  dm_spl
               644,883  dm_f

Is the compiler doing some optimization link to the CPU pipeline prediction or something else ?

Patrice

A message that is not in the archive (sent off-list or not yet received): 1f4881a0-8d52-c280-3376-d66943d489a1-qxv4g6HH51o@public.gmane.org

#37 lore raw
Quoted 123 lines · Patrice CHOTARD

Hi David

Quoted 120 lines · David Gibson
Quoted 119 lines · Patrice CHOTARD

Hi

Quoted 96 lines · David Gibson
Quoted 11 lines · Rob Herring
Quoted 10 lines · Patrice CHOTARD

Hi Rob, Tom, David

Quoted 7 lines · Rob Herring
Quoted 6 lines · Tom Rini
Quoted 5 lines · David Gibson
Quoted 4 lines · Tom Rini
Quoted 3 lines · David Gibson
Quoted 2 lines · Tom Rini
Quoted 1 line · David Gibson

On Fri, Jan 17, 2020 at 10:31:06AM -0500, Tom Rini wrote:

[snip]

Quoted 68 lines
Quoted 68 lines
Quoted 13 lines

Why not just fix the helpers for the aligned case and be done with it:

static inline uint32_t fdt32_ld(const fdt32_t *p)

{
       const uint8_t *bp = (const uint8_t *)p;
(quoted diff) +3 -0
+       if (!((unsigned long)p & 0x3))
+               return fdt32_to_cpu(*p);
+
       return ((uint32_t)bp[0] << 24)
               | ((uint32_t)bp[1] << 16)
               | ((uint32_t)bp[2] << 8)
               | bp[3];
Quoted 1 line

}

Here are the results, before and after your proposed patch:

tested tag: V2020.04-RC1

STM32MP> bootstage report
Timer summary in microseconds (12 records):

       Mark    Elapsed  Stage
          0          0  reset
     84,268     84,268  SPL
    962,921    878,653  end SPL
    965,800      2,879  board_init_f
  4,314,348  3,348,548  board_init_r
  4,863,763    549,415  id=64
  4,908,759     44,996  id=65
  4,909,459        700  main_loop
  5,322,309    412,850  id=175

Accumulated time:

                83,284  dm_r
                95,842  dm_spl
             1,502,020  dm_f

-------------------------------------------------------------------------------

tested tag : V2020.04-RC1 + following fdt32_ld patch :

static inline uint32_t fdt32_ld(const fdt32_t *p)

{
       const uint8_t *bp = (const uint8_t *)p;
(quoted diff) +3 -0
+       if (!((unsigned long)p & 0x3))
+               return fdt32_to_cpu(*p);
+
       return ((uint32_t)bp[0] << 24)
               | ((uint32_t)bp[1] << 16)
               | ((uint32_t)bp[2] << 8)
               | bp[3];

}

STM32MP> bootstage report
Timer summary in microseconds (12 records):

       Mark    Elapsed  Stage
          0          0  reset
     84,264     84,264  SPL
    959,300    875,036  end SPL
    962,192      2,892  board_init_f
  4,310,598  3,348,406  board_init_r
  4,860,074    549,476  id=64
  4,905,119     45,045  id=65
  4,905,819        700  main_loop
  5,098,636    192,817  id=175
Quoted 7 lines
Quoted 7 lines

Accumulated time:

                83,202  dm_r
                95,252  dm_spl
             1,501,950  dm_f

There is no gain in board_init_r(), the added alignment test is expensive itself.

Drat.

Quoted 3 lines

That's odd. It should be just an AND and a conditional branch added. Those should be a few cycles compared to tens to hundreds of cycles for 4 loads instead of 1.

The later loads are very likely to hit in cache though, right? Or is this done before the caches are activated?

Quoted 9 lines
Quoted 2 lines

Doesn't u-boot build with -Os typically? Maybe it's not actually inlining.

Apparently it is. And yet... it seems pretty suspicious that the numbers are so close, despite doing something quite different here. Can we somehow verify that the fast path is really being executed?

For comparison purposes, what are the numbers if we change these to unconditionally do an aligned load?

I made one more test to be 100% sure and the result is weird ....

static inline uint32_t fdt32_ld(const fdt32_t *p)
{

       if (!((unsigned long)p & 0x3))
             return fdt32_to_cpu(*p);

       while (1) {};
}

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

the results are back to 'normal', board_init_r() execution is 1.9 second  faster .....

Huh. I dunno how, but it definitely seems like the conditional version wasn't operating properly.

Does putting a "likely" branch prediction on that if make any difference?

I already did this test by adding a "likely" but it has no effect :-(

Interesting. I think someone who knows ARM asm needs to look at the compiler output for the two cases to see what's going on.

Signature · 4 lines
David Gibson			| 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
#38 lore raw
In reply to Tom Rini
Quoted 143 lines · Tom Rini
Quoted 130 lines · David Gibson
Quoted 117 lines · Tom Rini
Quoted 116 lines · David Gibson
Quoted 80 lines · Tom Rini
Quoted 27 lines · David Gibson
Quoted 22 lines · Tom Rini

6dcb8ba4 "libfdt: Add helpers for accessing unaligned words" introduced changes to support unaligned reads for ARM platforms and 11738cf01f15 "libfdt: Don't use memcpy to handle unaligned reads on ARM" improved the performance of these helpers.

Ultimately however, these helpers should not exist. Unaligned access only occurs when images are forced out of alignment by the user. This unalignment is not supported and introduces problems later on as other parts of the system image are unaligned and they too require alignment.

Revert both of these changes.

---
By way of a little more explanation, looking at the archives it seems that the initial bug reporter said that they had a platform that was using U-Boot and had the "fdt_high=0xffffffff" set in the environment. What that does is to tell U-Boot to not do any of the sanity checks and relocation to ensure alignment that it would normally do because the user knows best. This later came up on the U-Boot list as once the DTB was loaded, Linux is unhappy because it demands correct alignment.

Quoted 3 lines

I only realized libfdt had introduced changes here when it was reported that boot time had gotten much slower once we merged this change in. It would be best to just drop it.

Quoted 8 lines

Hmm. I'm not sure about this. The commit message makes a case for why the unaligned handling isn't necessary, but not a case for why it's bad. Even if handling an unaligned tree isn't a common case, isn't it better to be able to than not?

I gather from the previous discussion that there's a significant performance impact, but that rationale needs to go into the commit message for posterity.

I wanted to emphasize that the code simply isn't ever needed, not that it's a performance problem. A performance problem implies that we would keep this, if it was fast enough. That's why people noticed it (it slows things down to an unusable level). But it's functionally wrong.

Quoted 20 lines

[snip]

Quoted 19 lines
libfdt/libfdt.h +0 -11
index fc4c4962a01c..d4ebe915cf46 100644
@@ -117,23 +117,6 @@ static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
 
 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
 
-/*
- * Alignment helpers:
- *     These helpers access words from a device tree blob.  They're
- *     built to work even with unaligned pointers on platforms (ike
- *     ARM) that don't like unaligned loads and stores
- */
-
-static inline uint32_t fdt32_ld(const fdt32_t *p)
-{
-	const uint8_t *bp = (const uint8_t *)p;
-
Quoted 8 lines

libfdt/libfdt.h static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen) · line 135

Quoted 5 lines
libfdt/libfdt.h +0 -5
-	return ((uint32_t)bp[0] << 24)
-		| ((uint32_t)bp[1] << 16)
-		| ((uint32_t)bp[2] << 8)
-		| bp[3];
-}

In particular, I definitely think removing the helpers entirely is a no go. They're now part of the published interface of the library.

Perhaps "mistakes were made" ? I don't think we need to worry about removing an interface here as projects are avoiding upgrading libfdt now (TF-A, formerly ATF) or reverting the change (U-Boot).

Quoted 4 lines

Even if they're not used for reading the internal tags, they can be used to load integers from within particular properties. Those are frequently unaligned, since properties generally have packed representations.

Quoted 4 lines

I don't see the relevance. Go back to the initial problem report. It's not "I have a new unique platform I'm using libfdt on and I have problems". It's "I keep jabbing myself with a rusty nail and now I have problems".

The initial report isn't the only relevant thing here. Although it prompted the change, it wasn't the only consideration in making it.

There's also two separate questions here:
1) Do we want byteswapping integer load helpers?
2) Should those handle unaligned accesses?

In working out how to address the (as it turns out, non existent) problem, I realized an abstraction for loading big-endian integers from the blob would be a useful thing in its own right. I also realized that it's a useful thing not just inside the libfdt code, but as an external interface. Callers have always needed to interpret the contents of individual dt properties, and loading integers from them is often a part of that.

I know of people using those fdtXX_ld() helpers right now, so I'm not going to take them away.

For the case of external users we absolutely do need to handle unaligned accesses. There are a number of existing bindings that mix strings and integers in packed format, in a single encoded property. So regardless of the alignment of the whole property, we can easily get unaligned integers in there, and I don't want to expose multiple different helpers for different cases.

Now, we don't *have* to use the same helpers for internal use. We could open code the internal loads, or use a special aligned-only version inside. But using the existing external helpers is the obvious and simple choice.

Quoted 4 lines
Quoted 2 lines

So, we're back to: I need a case for changing this now, not just a case for claiming it wasn't needed in the first place.

For U-Boot, I'm just going to revert this part of the changes. I would

That seems reasonable for the interim.

Quoted 5 lines

suggest that you look in to some way to fix the "fast path" here to go back to the previous way of working so that other project can continue to use libfdt as well and when callers need to access these helpers and are not otherwise in the fast path can do so.

Quoted 6 lines
Quoted 2 lines

You're adding visible boot time delay with things the way they exist today. That's not OK.

That's a fair point, but you need to make that case in the commit message and merge request, not just expect me to find and collate the arguments from other threads.

If you want me to leave the helpers alone but otherwise revert things, I can do a v2 and expand the commit message. And perhaps I'm too nice sometimes then but I do pickup and tweak things that are close enough to what I want and reword as needed for clarity.

Quoted 4 lines

I still believe you have things wrong. There's not an unaligned access problem that libfdt needs to care about. ARM doesn't need help handling unaligned accesses. The only problem that's been reported is from when a user got themselves so far off in the weeds that nothing else matters.

Ah! I think I jut figured out the cause of our misunderstanding. Looks like property encodings with mixed integers and strings are not as common as I thought. At least, not ones that have integer data *after* string data, which is the case that matters for alignment. The only examples I could find are IBM specific ones in PAPR, not in 1275 or a common binding (e.g. 'ibm,drc-info' and 'ibm,vpd').

So, chances are you've never encountered them in the ARM world. Nonetheless they do exist, and we do use libfdt on IBM PAPR systems as well.

Hrmm... so... chances are reverting this won't hit a problem in practice... for now. On ARM, properties with unaligned integers seem to essentially never exist, and POWER cpus can generally handle unaligned loads fine.

I remain uneasy about removing the unaligned handling, though. Even if you haven't hit it yet, it's entirely possible at some point someone will attach a device to an ARM system that has a binding with out-of-alignment integers. Likewise we have Microwatt (POWER based open ISA systems) chips on the way, which probably won't handle unaligned loads, and may or may not have devices with funny bindings.

I really don't want to have a load helper that works most of the time, but occasionally breaks horribly.

*ponders*

Signature · 4 lines
David Gibson			| 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