Skip to contentSkip to conversation

arm64: add DSB after icache flush in __flush_icache_all()

PATCH Superseded Vinayak Kale 4 participants 10 messages Jan 28, 2014 – Feb 5, 2014 arm64

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 arm64: add DSB after icache flush in __flush_icache_all()

Add DSB after icache flush. It's needed to complete the cache maintenance operation. The function __flush_icache_all() is used only for user space mappings and an ISB is not required because of an exception return before executing user instructions. An exception return would behave like an ISB.

This patch also uses 'memory' clobber for flush operation instruction to prevent instruction re-ordering by compiler.

---

V2: - Add more desciption in the commit message as suggested by Catalin & Will
- Use 'memory' clobber for flush instruction as suggested by Will

 arch/arm64/include/asm/cacheflush.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/include/asm/cacheflush.h +2 -1
index fea9ee3..bd30f42 100644
@@ -115,7 +115,8 @@ extern void flush_dcache_page(struct page *);
 
 static inline void __flush_icache_all(void)
 {
-	asm("ic	ialluis");
+	asm volatile("ic ialluis" : : : "memory");
+	dsb();
 }
 
 #define flush_dcache_mmap_lock(mapping) \
Signature · 1 line
1.8.2.1
#1 lore raw

PATCH v2 1/1 arm64: add DSB after icache flush in __flush_icache_all()

Quoted 5 lines · Vinayak Kale

Add DSB after icache flush. It's needed to complete the cache maintenance operation. The function __flush_icache_all() is used only for user space mappings and an ISB is not required because of an exception return before executing user instructions. An exception return would behave like an ISB.

Quoted 8 lines

This patch also uses 'memory' clobber for flush operation instruction to prevent instruction re-ordering by compiler.

---

V2: - Add more desciption in the commit message as suggested by Catalin & Will
- Use 'memory' clobber for flush instruction as suggested by Will

Please can you check and fix other occurrences of this bug too, as I asked in v1? For example, a 2 second grep shows problems with data-cache maintenance in kvm. I can also see the same problem for system register writes followed up with isb.

I also don't buy you not being able to test AArch32 kernels. Does KVM not work for you?

Will

Quoted 22 lines
 arch/arm64/include/asm/cacheflush.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/include/asm/cacheflush.h +2 -1
index fea9ee3..bd30f42 100644
@@ -115,7 +115,8 @@ extern void flush_dcache_page(struct page *);
 
 static inline void __flush_icache_all(void)
 {
-	asm("ic	ialluis");
+	asm volatile("ic ialluis" : : : "memory");
+	dsb();
 }
 
 #define flush_dcache_mmap_lock(mapping) \
Signature · 1 line
1.8.2.1
#2 lore raw

PATCH v2 1/1 arm64: add DSB after icache flush in __flush_icache_all()

Hi Will,

Quoted 12 lines · Will Deacon
Quoted 11 lines · Vinayak Kale

Add DSB after icache flush. It's needed to complete the cache maintenance operation. The function __flush_icache_all() is used only for user space mappings and an ISB is not required because of an exception return before executing user instructions. An exception return would behave like an ISB.

This patch also uses 'memory' clobber for flush operation instruction to prevent instruction re-ordering by compiler.

---

Quoted 7 lines
Quoted 2 lines

V2: - Add more desciption in the commit message as suggested by Catalin & Will
- Use 'memory' clobber for flush instruction as suggested by Will

Please can you check and fix other occurrences of this bug too, as I asked in v1? For example, a 2 second grep shows problems with data-cache maintenance in kvm. I can also see the same problem for system register writes followed up with isb.

Can you please elaborate whether you are referring to lack of memory clobber or missing barriers?

Quoted 3 lines

I also don't buy you not being able to test AArch32 kernels. Does KVM not work for you?

Let me see what I can do to address this.

Quoted 25 lines

Will

Quoted 22 lines
 arch/arm64/include/asm/cacheflush.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/include/asm/cacheflush.h +2 -2
index fea9ee3..bd30f42 100644
@@ -115,7 +115,8 @@ extern void flush_dcache_page(struct page *);
 
 static inline void __flush_icache_all(void)
 {
-     asm("ic ialluis");
+     asm volatile("ic ialluis" : : : "memory");
+     dsb();
 }
 
 #define flush_dcache_mmap_lock(mapping) \
--

1.8.2.1

#3 lore raw

PATCH v2 1/1 arm64: add DSB after icache flush in __flush_icache_all()

Quoted 11 lines · Vinayak Kale
Quoted 8 lines · Will Deacon
Quoted 2 lines · Vinayak Kale

V2: - Add more desciption in the commit message as suggested by Catalin & Will
- Use 'memory' clobber for flush instruction as suggested by Will

Please can you check and fix other occurrences of this bug too, as I asked in v1? For example, a 2 second grep shows problems with data-cache maintenance in kvm. I can also see the same problem for system register writes followed up with isb.

Can you please elaborate whether you are referring to lack of memory clobber or missing barriers?

The clobbers. For example:

arch/arm64/kvm/sys_regs.c:

        /* Make sure noone else changes CSSELR during this! */
        local_irq_disable();
        /* Put value into CSSELR */
        asm volatile("msr csselr_el1, %x0" : : "r" (csselr));
        isb();
        /* Read result out of CCSIDR */
        asm volatile("mrs %0, ccsidr_el1" : "=r" (ccsidr));
        local_irq_enable();

Just about everything can be re-ordered in that block, because the asm volatile statements don't have "memory" clobbers.

I think it's worth checking the rest of the kernel, too.

Will

#4 lore raw

PATCH v2 1/1 arm64: add DSB after icache flush in __flush_icache_all()

Quoted 26 lines · Will Deacon
Quoted 11 lines · Vinayak Kale
Quoted 8 lines · Will Deacon
Quoted 2 lines · Vinayak Kale

V2: - Add more desciption in the commit message as suggested by Catalin & Will
- Use 'memory' clobber for flush instruction as suggested by Will

Please can you check and fix other occurrences of this bug too, as I asked in v1? For example, a 2 second grep shows problems with data-cache maintenance in kvm. I can also see the same problem for system register writes followed up with isb.

Can you please elaborate whether you are referring to lack of memory clobber or missing barriers?

The clobbers. For example:

arch/arm64/kvm/sys_regs.c:

        /* Make sure noone else changes CSSELR during this! */
        local_irq_disable();
        /* Put value into CSSELR */
        asm volatile("msr csselr_el1, %x0" : : "r" (csselr));
        isb();
        /* Read result out of CCSIDR */
        asm volatile("mrs %0, ccsidr_el1" : "=r" (ccsidr));
        local_irq_enable();
Quoted 2 lines

Just about everything can be re-ordered in that block, because the asm volatile statements don't have "memory" clobbers.

I don't think they would be reordered at all with the volatile qualifiers.

Nicolas

#5 lore raw

PATCH v2 1/1 arm64: add DSB after icache flush in __flush_icache_all()

Hi Nico,

Quoted 18 lines · Nicolas Pitre
Quoted 17 lines · Will Deacon
Quoted 2 lines · Vinayak Kale

Can you please elaborate whether you are referring to lack of memory clobber or missing barriers?

The clobbers. For example:

arch/arm64/kvm/sys_regs.c:

        /* Make sure noone else changes CSSELR during this! */
        local_irq_disable();
        /* Put value into CSSELR */
        asm volatile("msr csselr_el1, %x0" : : "r" (csselr));
        isb();
        /* Read result out of CCSIDR */
        asm volatile("mrs %0, ccsidr_el1" : "=r" (ccsidr));
        local_irq_enable();
Quoted 5 lines
Quoted 2 lines

Just about everything can be re-ordered in that block, because the asm volatile statements don't have "memory" clobbers.

I don't think they would be reordered at all with the volatile qualifiers.

Whilst that may be the case in current compilers (i.e. I've not actually seen the above sequence get re-ordered), the GCC documentation states that:

Similarly, you can't expect a sequence of volatile asm instructions to remain perfectly consecutive. If you want consecutive output, use a single asm. Also, GCC performs some optimizations across a volatile asm instruction; GCC does not `forget everything' when it encounters a volatile asm instruction the way some other compilers do.

so I really think that the "memory" clobbers are needed to ensure strict ordering. This matches my understanding from discussions with the compiler engineers at ARM.

Will

#6 lore raw

PATCH v2 1/1 arm64: add DSB after icache flush in __flush_icache_all()

Quoted 36 lines · Will Deacon

Hi Nico,

Quoted 18 lines · Nicolas Pitre
Quoted 17 lines · Will Deacon
Quoted 2 lines · Vinayak Kale

Can you please elaborate whether you are referring to lack of memory clobber or missing barriers?

The clobbers. For example:

arch/arm64/kvm/sys_regs.c:

        /* Make sure noone else changes CSSELR during this! */
        local_irq_disable();
        /* Put value into CSSELR */
        asm volatile("msr csselr_el1, %x0" : : "r" (csselr));
        isb();
        /* Read result out of CCSIDR */
        asm volatile("mrs %0, ccsidr_el1" : "=r" (ccsidr));
        local_irq_enable();
Quoted 5 lines
Quoted 2 lines

Just about everything can be re-ordered in that block, because the asm volatile statements don't have "memory" clobbers.

I don't think they would be reordered at all with the volatile qualifiers.

Whilst that may be the case in current compilers (i.e. I've not actually seen the above sequence get re-ordered), the GCC documentation states that:

Similarly, you can't expect a sequence of volatile asm instructions to remain perfectly consecutive. If you want consecutive output, use a single asm. Also, GCC performs some optimizations across a volatile asm instruction; GCC does not `forget everything' when it encounters a volatile asm instruction the way some other compilers do.

Quoted 3 lines

so I really think that the "memory" clobbers are needed to ensure strict ordering. This matches my understanding from discussions with the compiler engineers at ARM.

Well, the key sentence above is: "you can't expect a sequence of volatile asm instructions to remain perfectly consecutive." That doesn't mean the "ordering" won't be respected.

If you need a certain ordering, then a volatile is all that you need. If nothing else should happen in between, then just use a single asm statement as suggested.

Only if you need ordering with regards to other memory accesses as well then yes you need a memory clobber in that case.

Nicolas

#7 lore raw

PATCH v2 1/1 arm64: add DSB after icache flush in __flush_icache_all()

In reply to Will Deacon
Quoted 36 lines · Will Deacon

Hi Nico,

Quoted 18 lines · Nicolas Pitre
Quoted 17 lines · Will Deacon
Quoted 2 lines · Vinayak Kale

Can you please elaborate whether you are referring to lack of memory clobber or missing barriers?

The clobbers. For example:

arch/arm64/kvm/sys_regs.c:

        /* Make sure noone else changes CSSELR during this! */
        local_irq_disable();
        /* Put value into CSSELR */
        asm volatile("msr csselr_el1, %x0" : : "r" (csselr));
        isb();
        /* Read result out of CCSIDR */
        asm volatile("mrs %0, ccsidr_el1" : "=r" (ccsidr));
        local_irq_enable();
Quoted 5 lines
Quoted 2 lines

Just about everything can be re-ordered in that block, because the asm volatile statements don't have "memory" clobbers.

I don't think they would be reordered at all with the volatile qualifiers.

Whilst that may be the case in current compilers (i.e. I've not actually seen the above sequence get re-ordered), the GCC documentation states that:

Similarly, you can't expect a sequence of volatile asm instructions to remain perfectly consecutive. If you want consecutive output, use a single asm. Also, GCC performs some optimizations across a volatile asm instruction; GCC does not `forget everything' when it encounters a volatile asm instruction the way some other compilers do.

Quoted 3 lines

so I really think that the "memory" clobbers are needed to ensure strict ordering. This matches my understanding from discussions with the compiler engineers at ARM.

What it means is that the compiler may introduce additional instructions between your consecutive asm() statements. So there's no guarantee that the ISB will immediately follow the MSR instruction - there may be other instructions which the compiler may decide to schedule between the two.

For example, instructions to load the address of the variable(s) may be inserted between the assembly specified in the asm() statements which may involve loading from a literal pool.

Signature · 3 lines
FTTC broadband for 0.8mile line: 5.8Mbps down 500kbps up.  Estimation
in database were 13.1 to 19Mbit for a good line, about 7.5+ for a bad.
Estimate before purchase was "up to 13.2Mbit".
#8 lore raw

PATCH v2 1/1 arm64: add DSB after icache flush in __flush_icache_all()

Hi Nico, Russell,

Thanks for the replies.

Quoted 18 lines · Russell King - ARM Linux
Quoted 13 lines · Will Deacon
Quoted 2 lines · Nicolas Pitre

I don't think they would be reordered at all with the volatile qualifiers.

Whilst that may be the case in current compilers (i.e. I've not actually seen the above sequence get re-ordered), the GCC documentation states that:

Similarly, you can't expect a sequence of volatile asm instructions to remain perfectly consecutive. If you want consecutive output, use a single asm. Also, GCC performs some optimizations across a volatile asm instruction; GCC does not `forget everything' when it encounters a volatile asm instruction the way some other compilers do.

Quoted 3 lines

so I really think that the "memory" clobbers are needed to ensure strict ordering. This matches my understanding from discussions with the compiler engineers at ARM.

Quoted 8 lines

What it means is that the compiler may introduce additional instructions between your consecutive asm() statements. So there's no guarantee that the ISB will immediately follow the MSR instruction - there may be other instructions which the compiler may decide to schedule between the two.

For example, instructions to load the address of the variable(s) may be inserted between the assembly specified in the asm() statements which may involve loading from a literal pool.

That matches what Nicolas said and, to be honest, makes a lot of sense. I'm just slightly concerned that it doesn't match the explanation I received from some compiler guys, but I can chase that down separately.

Vinayak: sorry for leading you down the garden path on this. Please can you stick with your original patch, but adding something equivalent for arch/arm?

Cheers,

Will

#9 lore raw

PATCH v2 1/1 arm64: add DSB after icache flush in __flush_icache_all()

Quoted 32 lines · Will Deacon

Hi Nico, Russell,

Thanks for the replies.

Quoted 18 lines · Russell King - ARM Linux
Quoted 13 lines · Will Deacon
Quoted 2 lines · Nicolas Pitre

I don't think they would be reordered at all with the volatile qualifiers.

Whilst that may be the case in current compilers (i.e. I've not actually seen the above sequence get re-ordered), the GCC documentation states that:

Similarly, you can't expect a sequence of volatile asm instructions to remain perfectly consecutive. If you want consecutive output, use a single asm. Also, GCC performs some optimizations across a volatile asm instruction; GCC does not `forget everything' when it encounters a volatile asm instruction the way some other compilers do.

Quoted 3 lines

so I really think that the "memory" clobbers are needed to ensure strict ordering. This matches my understanding from discussions with the compiler engineers at ARM.

Quoted 8 lines

What it means is that the compiler may introduce additional instructions between your consecutive asm() statements. So there's no guarantee that the ISB will immediately follow the MSR instruction - there may be other instructions which the compiler may decide to schedule between the two.

For example, instructions to load the address of the variable(s) may be inserted between the assembly specified in the asm() statements which may involve loading from a literal pool.

Quoted 7 lines

That matches what Nicolas said and, to be honest, makes a lot of sense. I'm just slightly concerned that it doesn't match the explanation I received from some compiler guys, but I can chase that down separately.

Vinayak: sorry for leading you down the garden path on this. Please can you stick with your original patch, but adding something equivalent for arch/arm?

I have posted a patch for arch/arm, have tested it for ARM-v7.

Quoted 4 lines

Cheers,

Will