Skip to contentSkip to conversation

cmd: net: flush cache cacheline aligned

PATCH In review Stefan Agner 5 participants 13 messages Aug 2, 2016 – Aug 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cmd: net: flush cache cacheline aligned

From: Stefan Agner <[email protected]>

Flush loaded data cacheline aligned. This avoids warnings such as
CACHE: Misaligned operation at range [81000000, 816d0fa8]

---
Why do we actually have to flush caches after load? It seems to have worked so far despite the caches did not get flushed (due to missalignment).

--
Stefan

 cmd/net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
cmd/net.c +1 -1
index b2f3c7b..540daeb 100644
@@ -244,7 +244,7 @@ static int netboot_common(enum proto_t proto, cmd_tbl_t *cmdtp, int argc,
 	}
 
 	/* flush cache */
-	flush_cache(load_addr, size);
+	flush_cache(load_addr, ALIGN(size, CONFIG_SYS_CACHELINE_SIZE));
 
 	bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
 
Signature · 1 line
2.9.0
#2 lore raw

PATCH 1/1 cmd: net: flush cache cacheline aligned

In reply to Stefan Agner
Quoted 11 lines · Stefan Agner

From: Stefan Agner <[email protected]>

Flush loaded data cacheline aligned. This avoids warnings such as
CACHE: Misaligned operation at range [81000000, 816d0fa8]

---
Why do we actually have to flush caches after load? It seems to have worked so far despite the caches did not get flushed (due to missalignment).

Quoted 5 lines

--
Stefan

 cmd/net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
#3 lore raw

PATCH 1/1 cmd: net: flush cache cacheline aligned

In reply to Stefan Agner
Quoted 7 lines · Stefan Agner

From: Stefan Agner <[email protected]>

Flush loaded data cacheline aligned. This avoids warnings such as
CACHE: Misaligned operation at range [81000000, 816d0fa8]

---

This was already rejected once.
http://lists.denx.de/pipermail/u-boot/2012-April/121564.html

Quoted 3 lines

Why do we actually have to flush caches after load? It seems to have worked so far despite the caches did not get flushed (due to missalignment).

I'm not sure that we do, but it's been there since as far back as the git history goes. Maybe Wolfgang has memory of a reason.

-Joe

Quoted 26 lines

--
Stefan

 cmd/net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
cmd/net.c +1 -2
index b2f3c7b..540daeb 100644
@@ -244,7 +244,7 @@ static int netboot_common(enum proto_t proto, cmd_tbl_t *cmdtp, int argc,
        }
 
        /* flush cache */
-       flush_cache(load_addr, size);
+       flush_cache(load_addr, ALIGN(size, CONFIG_SYS_CACHELINE_SIZE));
 
        bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
 
--

2.9.0

_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot

#4 lore raw

PATCH 1/1 cmd: net: flush cache cacheline aligned

Hi Joe,

Quoted 12 lines · Joe Hershberger
Quoted 7 lines · Stefan Agner

From: Stefan Agner <[email protected]>

Flush loaded data cacheline aligned. This avoids warnings such as
CACHE: Misaligned operation at range [81000000, 816d0fa8]

---

This was already rejected once.
http://lists.denx.de/pipermail/u-boot/2012-April/121564.html

Quoted 6 lines
Quoted 3 lines

Why do we actually have to flush caches after load? It seems to have worked so far despite the caches did not get flushed (due to missalignment).

I'm not sure that we do, but it's been there since as far back as the git history goes. Maybe Wolfgang has memory of a reason.

I think this is the correct solution. If you load something to memory you have to flush the cache. If someone is brave enough to load an image that finishes immediately before something where there is data that will be overwritten by the flush, then they deserve some pain.

Anytime we flush the cache we have to do it according the rules of the cache. One of those rules is that cache lines are larger than one byte. So either we don't flush or we do, and if we do, we must align.

Regards,
Simon

#5 lore raw

PATCH 1/1 cmd: net: flush cache cacheline aligned

Hi Simon,

Quoted 19 lines · Simon Glass

Hi Joe,

Quoted 16 lines · Joe Hershberger
Quoted 7 lines · Stefan Agner

From: Stefan Agner <[email protected]>

Flush loaded data cacheline aligned. This avoids warnings such as
CACHE: Misaligned operation at range [81000000, 816d0fa8]

---

This was already rejected once.
http://lists.denx.de/pipermail/u-boot/2012-April/121564.html

Quoted 3 lines

Why do we actually have to flush caches after load? It seems to have worked so far despite the caches did not get flushed (due to missalignment).

Quoted 7 lines
Quoted 2 lines

I'm not sure that we do, but it's been there since as far back as the git history goes. Maybe Wolfgang has memory of a reason.

I think this is the correct solution. If you load something to memory you have to flush the cache. If someone is brave enough to load an image that finishes immediately before something where there is data that will be overwritten by the flush, then they deserve some pain.

I'm not at all worried about the overwrite case.

Quoted 3 lines

Anytime we flush the cache we have to do it according the rules of the cache. One of those rules is that cache lines are larger than one byte. So either we don't flush or we do, and if we do, we must align.

For sure... I was just wondering why we need this flush at all. Stefan's comment as to why seems reasonable now, but that reasoning surely wasn't a concern pre-2000. Maybe some early arch needed it for a different reason? Maybe someone was planning to DMA directly from the memory that was loaded, but in that case it seems like the code starting the DMA would ensure the flush, not the code that is memcpy'ing from somewhere.

Cheers,
-Joe

#6 lore raw

PATCH 1/1 cmd: net: flush cache cacheline aligned

Quoted 36 lines · Joe Hershberger

Hi Simon,

Quoted 19 lines · Simon Glass

Hi Joe,

Quoted 16 lines · Joe Hershberger
Quoted 7 lines · Stefan Agner

From: Stefan Agner <[email protected]>

Flush loaded data cacheline aligned. This avoids warnings such as
CACHE: Misaligned operation at range [81000000, 816d0fa8]

---

This was already rejected once.
http://lists.denx.de/pipermail/u-boot/2012-April/121564.html

Quoted 3 lines

Why do we actually have to flush caches after load? It seems to have worked so far despite the caches did not get flushed (due to missalignment).

Quoted 7 lines
Quoted 2 lines

I'm not sure that we do, but it's been there since as far back as the git history goes. Maybe Wolfgang has memory of a reason.

I think this is the correct solution. If you load something to memory you have to flush the cache. If someone is brave enough to load an image that finishes immediately before something where there is data that will be overwritten by the flush, then they deserve some pain.

I'm not at all worried about the overwrite case.

Quoted 3 lines

Anytime we flush the cache we have to do it according the rules of the cache. One of those rules is that cache lines are larger than one byte. So either we don't flush or we do, and if we do, we must align.

Quoted 7 lines

For sure... I was just wondering why we need this flush at all. Stefan's comment as to why seems reasonable now, but that reasoning surely wasn't a concern pre-2000. Maybe some early arch needed it for a different reason? Maybe someone was planning to DMA directly from the memory that was loaded, but in that case it seems like the code starting the DMA would ensure the flush, not the code that is memcpy'ing from somewhere.

Just to clarify my point on this:
Non cache coherent (peripheral) CPU's was the only reason I could think of, but I don't think it is a reason to actually do it.

IMHO it should be handled the same way as DMA gets handled: if a non cache coherent CPU is about to get started, that code should make sure to handle the cache flush... (e.g. the bootaux command in the i.MX 7 case).

--
Stefan

#7 lore raw

PATCH 1/1 cmd: net: flush cache cacheline aligned

In reply to Joe Hershberger
Quoted 11 lines · Joe Hershberger
Quoted 7 lines · Stefan Agner

From: Stefan Agner <[email protected]>

Flush loaded data cacheline aligned. This avoids warnings such as
CACHE: Misaligned operation at range [81000000, 816d0fa8]

---

This was already rejected once.
http://lists.denx.de/pipermail/u-boot/2012-April/121564.html

Oh I see, and in the end the message was converted to a debug() call, in essence turning the whole problem back under a stone... :-)

FWIW, I largely support Mike Frysinger's position in that discussion, and think it should be fine to flush these extra bytes...

Quoted 4 lines
Quoted 4 lines

Why do we actually have to flush caches after load? It seems to have worked so far despite the caches did not get flushed (due to missalignment).

Btw, I need to correct myself here: flush_dcache ultimately calls v7_dcache_clean_inval_range, which does not bail out in the unaligned case. Instead, afaict, it flushes until mva < stop, hence flushes the last line anyway...

Quoted 3 lines

I'm not sure that we do, but it's been there since as far back as the git history goes. Maybe Wolfgang has memory of a reason.

The only reason why it might be preferable to have loaded stuff in main memory is with nowadays heterogeneous architectures (e.g. i.MX 7 with its secondary M4 core). In those cases we need to flush caches since those two cores are no cache coherent. However, I don't think that all load commands are doing the flush, so not sure if can rely on that today...

--
Stefan

Quoted 29 lines

-Joe

Quoted 26 lines

--
Stefan

 cmd/net.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
cmd/net.c +1 -2
index b2f3c7b..540daeb 100644
@@ -244,7 +244,7 @@ static int netboot_common(enum proto_t proto, cmd_tbl_t *cmdtp, int argc,
        }
 
        /* flush cache */
-       flush_cache(load_addr, size);
+       flush_cache(load_addr, ALIGN(size, CONFIG_SYS_CACHELINE_SIZE));
 
        bootstage_mark(BOOTSTAGE_ID_NET_LOADED);
 
--

2.9.0

_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot

#8 lore raw

PATCH 1/1 cmd: net: flush cache cacheline aligned

Quoted 10 lines · Stefan Agner
Quoted 9 lines · Joe Hershberger
Quoted 7 lines · Stefan Agner

From: Stefan Agner <[email protected]>

Flush loaded data cacheline aligned. This avoids warnings such as
CACHE: Misaligned operation at range [81000000, 816d0fa8]

---

Quoted 8 lines
Quoted 2 lines

This was already rejected once.
http://lists.denx.de/pipermail/u-boot/2012-April/121564.html

Oh I see, and in the end the message was converted to a debug() call, in essence turning the whole problem back under a stone... :-)

FWIW, I largely support Mike Frysinger's position in that discussion, and think it should be fine to flush these extra bytes...

I re-read most of that thread last-night and yes, it seems like rather than solving the problem we just put it into a debug(). But if I followed Mike's point right, we shouldn't be having a debug there either, the flushes are fine? And as to Marek's point there about driver bugs, it's perhaps only sub-optimal rather than a broken thing?

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20160804/d8641a35/attachment.sig>
#9 lore raw

PATCH 1/1 cmd: net: flush cache cacheline aligned

Quoted 17 lines · Tom Rini
Quoted 16 lines · Stefan Agner
Quoted 11 lines · Joe Hershberger
Quoted 7 lines · Stefan Agner

From: Stefan Agner <[email protected]>

Flush loaded data cacheline aligned. This avoids warnings such as
CACHE: Misaligned operation at range [81000000, 816d0fa8]

---

This was already rejected once.
http://lists.denx.de/pipermail/u-boot/2012-April/121564.html

Oh I see, and in the end the message was converted to a debug() call, in essence turning the whole problem back under a stone... :-)

Quoted 8 lines
Quoted 2 lines

FWIW, I largely support Mike Frysinger's position in that discussion, and think it should be fine to flush these extra bytes...

I re-read most of that thread last-night and yes, it seems like rather than solving the problem we just put it into a debug(). But if I followed Mike's point right, we shouldn't be having a debug there either, the flushes are fine? And as to Marek's point there about driver bugs, it's perhaps only sub-optimal rather than a broken thing?

[added Mike to CC]

It seems that back then another cache driver was affected (arch/arm/cpu/arm926ejs/cache.c).

I don't agree with Mike: We definitely should warn if a cache flush is unaligned. Hence I think Siomons change is the right thing to do. Not sure if all cache driver behave the same, but the ARMv7 cache driver (arch/arm/cpu/armv7/cache_v7.c) flushes a unaligned line. This might not be an issue in that particular case in cmd/net.c (since there are likely no critical data immediately following the loaded binary), but it definitely can be an issue if we flush something on the heap: Assume we have an array of 32 byte long DMA descriptors, and the cache line is 64 bytes. If a driver tries to flush one descriptor only, we would always silently flush a second DMA descriptor... If that other descriptor is updated by the DMA controller, we would run into an issue. So if a driver flushes cache line unaligned, we should warn!

Since the cache driver anyway flushes cache aligned, the only thing this patch changes is actually to be explicit about that... And compared to the patch above, this patch uses CONFIG_SYS_CACHELINE_SIZE, which makes it clear that this is not a DMA alignment issue but a cache line alignment issue...

So I vote for either explicitly flush cache aligned (what this patch is doing) or remove that flush entirely...

--
Stefan

#10 lore raw
In reply to Stefan Agner
Quoted 5 lines · Stefan Agner

From: Stefan Agner <[email protected]>

Flush loaded data cacheline aligned. This avoids warnings such as
CACHE: Misaligned operation at range [81000000, 816d0fa8]

Quoted 7 lines

---
Why do we actually have to flush caches after load? It seems to have worked so far despite the caches did not get flushed (due to missalignment).

Joe, would you prefer to just drop this flush, given the rest of the thread? Thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20160814/ae9ac3c5/attachment.sig>
#11 lore raw
Quoted 15 lines · Tom Rini
Quoted 5 lines · Stefan Agner

From: Stefan Agner <[email protected]>

Flush loaded data cacheline aligned. This avoids warnings such as
CACHE: Misaligned operation at range [81000000, 816d0fa8]

Quoted 7 lines

---
Why do we actually have to flush caches after load? It seems to have worked so far despite the caches did not get flushed (due to missalignment).

Quoted 2 lines

Joe, would you prefer to just drop this flush, given the rest of the thread? Thanks!

Just found Diana Craciuns (added to CC) commit 99ffccbd3e5b7bc715e2eed6ea6d36f4020b56d8 ("Flush cache after the OS image is loaded into the memory.") which adds a flush_cache in cmd_bootm.c:

Quoted 1 line

--snip

Flush cache after the OS image is loaded into the memory.

Since we are loading an executable image into memory we need flush it out of the cache to possible maintain coherence on CPUs with split instruction and data caches. We do this for other executable image loading command.

On PowerPC once we do this we no longer need to explicitly flush the dcache on multi-core systems in the BOOTM_STATE_OS_PREP phase. We now treat the BOOTM_STATE_OS_PREP as a no-op to maintain backwards compatibility with the bootm subcommand.
<--snip

That might be actually the case in this situation too, although I feel this should be taken care of before kernel entry. At least on ARM, caches get flushed before jumping to the kernel. Is PowerPC different in that matter?

--
Stefan

#12 lore raw
In reply to Tom Rini
Quoted 15 lines · Tom Rini
Quoted 5 lines · Stefan Agner

From: Stefan Agner <[email protected]>

Flush loaded data cacheline aligned. This avoids warnings such as
CACHE: Misaligned operation at range [81000000, 816d0fa8]

Quoted 7 lines

---
Why do we actually have to flush caches after load? It seems to have worked so far despite the caches did not get flushed (due to missalignment).

Quoted 2 lines

Joe, would you prefer to just drop this flush, given the rest of the thread? Thanks!

That's what I'd prefer. I'm willing to wait for a bit more discussion. though.

Cheers,
-Joe