Skip to contentSkip to conversation

ARM: mm: avoid attempting to flush the gate_vma with VIVT caches

PATCH New Will Deacon 3 participants 14 messages Jul 16, 2012 – Jul 22, 2012 Arm (32-bit)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ARM: mm: avoid attempting to flush the gate_vma with VIVT caches

The vivt_flush_cache_{range,page} functions check that the mm_struct of the VMA being flushed has been active on the current CPU before performing the cache maintenance.

The gate_vma has a NULL mm_struct pointer and, as such, will cause a kernel fault if we try to flush it with the above operations. This happens during ELF core dumps, which include the gate_vma as it may be useful for debugging purposes.

This patch adds checks to the VIVT cache flushing functions so that VMAs with a NULL mm_struct are ignored.

---
 arch/arm/include/asm/cacheflush.h |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
arch/arm/include/asm/cacheflush.h +6 -2
index 004c1bc..8cf828e 100644
@@ -215,7 +215,9 @@ static inline void vivt_flush_cache_mm(struct mm_struct *mm)
 static inline void
 vivt_flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
 {
-	if (cpumask_test_cpu(smp_processor_id(), mm_cpumask(vma->vm_mm)))
+	struct mm_struct *mm = vma->vm_mm;
+
+	if (mm && cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm)))
 		__cpuc_flush_user_range(start & PAGE_MASK, PAGE_ALIGN(end),
 					vma->vm_flags);
 }
@@ -223,7 +225,9 @@ vivt_flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned
 static inline void
 vivt_flush_cache_page(struct vm_area_struct *vma, unsigned long user_addr, unsigned long pfn)
 {
-	if (cpumask_test_cpu(smp_processor_id(), mm_cpumask(vma->vm_mm))) {
+	struct mm_struct *mm = vma->vm_mm;
+
+	if (mm && cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm))) {
 		unsigned long addr = user_addr & PAGE_MASK;
 		__cpuc_flush_user_range(addr, addr + PAGE_SIZE, vma->vm_flags);
 	}
Signature · 1 line
1.7.4.1
#1 lore raw

PATCH 1/1 ARM: mm: avoid attempting to flush the gate_vma with VIVT caches

Gilles, Uros,

Quoted 11 lines · Will Deacon

The vivt_flush_cache_{range,page} functions check that the mm_struct of the VMA being flushed has been active on the current CPU before performing the cache maintenance.

The gate_vma has a NULL mm_struct pointer and, as such, will cause a kernel fault if we try to flush it with the above operations. This happens during ELF core dumps, which include the gate_vma as it may be useful for debugging purposes.

This patch adds checks to the VIVT cache flushing functions so that VMAs with a NULL mm_struct are ignored.

Would one of you be able to test this patch please? I've not managed to trigger the bug you reported on my boards, so it would be useful to know whether or not this patch solves the problem for you.

Thanks,

Will

Quoted 29 lines
arch/arm/include/asm/cacheflush.h +6 -2
index 004c1bc..8cf828e 100644
@@ -215,7 +215,9 @@ static inline void vivt_flush_cache_mm(struct mm_struct *mm)
 static inline void
 vivt_flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
 {
-	if (cpumask_test_cpu(smp_processor_id(), mm_cpumask(vma->vm_mm)))
+	struct mm_struct *mm = vma->vm_mm;
+
+	if (mm && cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm)))
 		__cpuc_flush_user_range(start & PAGE_MASK, PAGE_ALIGN(end),
 					vma->vm_flags);
 }
@@ -223,7 +225,9 @@ vivt_flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned
 static inline void
 vivt_flush_cache_page(struct vm_area_struct *vma, unsigned long user_addr, unsigned long pfn)
 {
-	if (cpumask_test_cpu(smp_processor_id(), mm_cpumask(vma->vm_mm))) {
+	struct mm_struct *mm = vma->vm_mm;
+
+	if (mm && cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm))) {
 		unsigned long addr = user_addr & PAGE_MASK;
 		__cpuc_flush_user_range(addr, addr + PAGE_SIZE, vma->vm_flags);
 	}
Signature · 1 line
1.7.4.1
#2 lore raw

PATCH 1/1 ARM: mm: avoid attempting to flush the gate_vma with VIVT caches

Quoted 10 lines · Will Deacon
Quoted 9 lines · Will Deacon

The vivt_flush_cache_{range,page} functions check that the mm_struct of the VMA being flushed has been active on the current CPU before performing the cache maintenance.

The gate_vma has a NULL mm_struct pointer and, as such, will cause a kernel fault if we try to flush it with the above operations. This happens during ELF core dumps, which include the gate_vma as it may be useful for debugging purposes.

Quoted 6 lines
Quoted 2 lines

This patch adds checks to the VIVT cache flushing functions so that VMAs with a NULL mm_struct are ignored.

Would one of you be able to test this patch please? I've not managed to trigger the bug you reported on my boards, so it would be useful to know whether or not this patch solves the problem for you.

Patched kernel works as expected. Also, backport to 3.4 (where original problem was triggered) works OK, so I'd suggest to backport this patch to 3.4.

Thanks,
Uros.

#3 lore raw

PATCH 1/1 ARM: mm: avoid attempting to flush the gate_vma with VIVT caches

Quoted 19 lines · Uros Bizjak
Quoted 10 lines · Will Deacon
Quoted 9 lines · Will Deacon

The vivt_flush_cache_{range,page} functions check that the mm_struct of the VMA being flushed has been active on the current CPU before performing the cache maintenance.

The gate_vma has a NULL mm_struct pointer and, as such, will cause a kernel fault if we try to flush it with the above operations. This happens during ELF core dumps, which include the gate_vma as it may be useful for debugging purposes.

Quoted 6 lines
Quoted 2 lines

This patch adds checks to the VIVT cache flushing functions so that VMAs with a NULL mm_struct are ignored.

Would one of you be able to test this patch please? I've not managed to trigger the bug you reported on my boards, so it would be useful to know whether or not this patch solves the problem for you.

Quoted 5 lines

Patched kernel works as expected. Also, backport to 3.4 (where original problem was triggered) works OK, so I'd suggest to backport this patch to 3.4.

Thanks Uros, I'll add that and CC stable on the patch too.

Will

#4 lore raw

PATCH 1/1 ARM: mm: avoid attempting to flush the gate_vma with VIVT caches

In reply to Will Deacon
Quoted 12 lines · Will Deacon

Gilles, Uros,

Quoted 9 lines · Will Deacon

The vivt_flush_cache_{range,page} functions check that the mm_struct of the VMA being flushed has been active on the current CPU before performing the cache maintenance.

The gate_vma has a NULL mm_struct pointer and, as such, will cause a kernel fault if we try to flush it with the above operations. This happens during ELF core dumps, which include the gate_vma as it may be useful for debugging purposes.

Quoted 4 lines
Quoted 2 lines

This patch adds checks to the VIVT cache flushing functions so that VMAs with a NULL mm_struct are ignored.

Would one of you be able to test this patch please?

Sorry for the delay, I am getting this mail just now.

Quoted 2 lines

I've not managed to
trigger the bug you reported on my boards,

I found this bug with the LTP testsuite, more precisely the test named "abort01".

Quoted 2 lines

so it would be useful to know
whether or not this patch solves the problem for you.

Fixes linux 3.4 bug for me. But... are you sure this is the right fix? I mean you are adding an almost always useless test to many cache flushes, except in one corner case. Would not it make more sense to make the fix local, and fix gate_vma to have a static mm struct with a valid cpumask? Being 0 or 1 whether we want to flush the vector page (I believe we do not want to flush it, but am not sure).

Signature · 1 line
                                                                Gilles.
#5 lore raw

PATCH 1/1 ARM: mm: avoid attempting to flush the gate_vma with VIVT caches

Quoted 2 lines · Gilles Chanteperdrix

Being 0 or 1 whether we want to flush the vector page (I believe we do not want to flush it, but am not sure).

Actually, I believe we want to flush the vector page, at least on systems with VIVT cache: on systems with VIVT cache, the vector page is writeable in kernel mode, so may have been modified, and the address used by elf_core_dump is not the vectors address, but the address in the kernel direct-mapped RAM region where the vector page was allocated, so there is a cache aliasing issue.

Signature · 1 line
                                                                Gilles.
#6 lore raw

PATCH 1/1 ARM: mm: avoid attempting to flush the gate_vma with VIVT caches

Hi Gilles,

Quoted 10 lines · Gilles Chanteperdrix
Quoted 2 lines · Gilles Chanteperdrix

Being 0 or 1 whether we want to flush the vector page (I believe we do not want to flush it, but am not sure).

Actually, I believe we want to flush the vector page, at least on systems with VIVT cache: on systems with VIVT cache, the vector page is writeable in kernel mode, so may have been modified, and the address used by elf_core_dump is not the vectors address, but the address in the kernel direct-mapped RAM region where the vector page was allocated, so there is a cache aliasing issue.

It may be writable, but we never actually write to it after it has been initialised so there's no need to worry about caching issues (the cache is flushed in devicemaps_init).

As for the NULL check, it's likely to be a single additional cycle before a cacheflush. I really consider it to be insignificant.

Will

#7 lore raw

PATCH 1/1 ARM: mm: avoid attempting to flush the gate_vma with VIVT caches

Quoted 14 lines · Will Deacon

Hi Gilles,

Quoted 10 lines · Gilles Chanteperdrix
Quoted 2 lines · Gilles Chanteperdrix

Being 0 or 1 whether we want to flush the vector page (I believe we do not want to flush it, but am not sure).

Actually, I believe we want to flush the vector page, at least on systems with VIVT cache: on systems with VIVT cache, the vector page is writeable in kernel mode, so may have been modified, and the address used by elf_core_dump is not the vectors address, but the address in the kernel direct-mapped RAM region where the vector page was allocated, so there is a cache aliasing issue.

Quoted 3 lines

It may be writable, but we never actually write to it after it has been initialised so there's no need to worry about caching issues (the cache is flushed in devicemaps_init).

Except if CONFIG_TLS_REG_EMUL is enabled, or if some faulty code wrote by accident to the vector page, which caused the application to crash. What is the reason to include the vector page in the core dump if not to help debugging?

Signature · 1 line
                                                                Gilles.
#8 lore raw

PATCH 1/1 ARM: mm: avoid attempting to flush the gate_vma with VIVT caches

Quoted 15 lines · Gilles Chanteperdrix
Quoted 14 lines · Will Deacon

Hi Gilles,

Quoted 10 lines · Gilles Chanteperdrix
Quoted 2 lines · Gilles Chanteperdrix

Being 0 or 1 whether we want to flush the vector page (I believe we do not want to flush it, but am not sure).

Actually, I believe we want to flush the vector page, at least on systems with VIVT cache: on systems with VIVT cache, the vector page is writeable in kernel mode, so may have been modified, and the address used by elf_core_dump is not the vectors address, but the address in the kernel direct-mapped RAM region where the vector page was allocated, so there is a cache aliasing issue.

Quoted 5 lines
Quoted 3 lines

It may be writable, but we never actually write to it after it has been initialised so there's no need to worry about caching issues (the cache is flushed in devicemaps_init).

Except if CONFIG_TLS_REG_EMUL is enabled

is disabled I mean.

Signature · 1 line
                                                                Gilles.
#9 lore raw

PATCH 1/1 ARM: mm: avoid attempting to flush the gate_vma with VIVT caches

Quoted 16 lines · Gilles Chanteperdrix
Quoted 15 lines · Gilles Chanteperdrix
Quoted 14 lines · Will Deacon

Hi Gilles,

Quoted 10 lines · Gilles Chanteperdrix
Quoted 2 lines · Gilles Chanteperdrix

Being 0 or 1 whether we want to flush the vector page (I believe we do not want to flush it, but am not sure).

Actually, I believe we want to flush the vector page, at least on systems with VIVT cache: on systems with VIVT cache, the vector page is writeable in kernel mode, so may have been modified, and the address used by elf_core_dump is not the vectors address, but the address in the kernel direct-mapped RAM region where the vector page was allocated, so there is a cache aliasing issue.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

It may be writable, but we never actually write to it after it has been initialised so there's no need to worry about caching issues (the cache is flushed in devicemaps_init).

Except if CONFIG_TLS_REG_EMUL is enabled

is disabled I mean.

Well spotted! I disagree about the address being flushed though -- it looks to me like we flush from 0xffff0000 - 0xffff1000, which is what we want. Why do you think we're flushing from the linear mapping?

Anyway, the TLS issue can easily be resolved by changing my previous patch so that we flush unconditionally when there's no mm (see below).

In the meantime, I'll remove the old patch from the patch system while we address your remaining concerns.

Cheers,

Will

---8<---

arch/arm/include/asm/cacheflush.h +2 -2
index 8cf828e..e4448e1 100644
@@ -217,7 +217,7 @@ vivt_flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned
 {
        struct mm_struct *mm = vma->vm_mm;
 
-       if (mm && cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm)))
+       if (!mm || cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm)))
                __cpuc_flush_user_range(start & PAGE_MASK, PAGE_ALIGN(end),
                                        vma->vm_flags);
 }
@@ -227,7 +227,7 @@ vivt_flush_cache_page(struct vm_area_struct *vma, unsigned long user_addr, unsig
 {
        struct mm_struct *mm = vma->vm_mm;
 
-       if (mm && cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm))) {
+       if (!mm || cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm))) {
                unsigned long addr = user_addr & PAGE_MASK;
                __cpuc_flush_user_range(addr, addr + PAGE_SIZE, vma->vm_flags);
        }
#10 lore raw

PATCH 1/1 ARM: mm: avoid attempting to flush the gate_vma with VIVT caches

Quoted 21 lines · Will Deacon
Quoted 20 lines · Gilles Chanteperdrix
Quoted 19 lines · Gilles Chanteperdrix
Quoted 17 lines · Will Deacon

Hi Gilles,

Quoted 10 lines · Gilles Chanteperdrix
Quoted 2 lines · Gilles Chanteperdrix

Being 0 or 1 whether we want to flush the vector page (I believe we do not want to flush it, but am not sure).

Actually, I believe we want to flush the vector page, at least on systems with VIVT cache: on systems with VIVT cache, the vector page is writeable in kernel mode, so may have been modified, and the address used by elf_core_dump is not the vectors address, but the address in the kernel direct-mapped RAM region where the vector page was allocated, so there is a cache aliasing issue.

It may be writable, but we never actually write to it after it has been initialised so there's no need to worry about caching issues (the cache is flushed in devicemaps_init).

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Except if CONFIG_TLS_REG_EMUL is enabled

is disabled I mean.

Well spotted! I disagree about the address being flushed though -- it looks to me like we flush from 0xffff0000 - 0xffff1000, which is what we want. Why do you think we're flushing from the linear mapping?

I do not think we're flushing from the linear mapping, I believe the address used by the elf_core_dump function (elf_core_dump -> kmap -> page_address), to copy the page data to the core is the linear mapping address, which is the reason why we need the flush at all.

Signature · 1 line
                                                                Gilles.
#11 lore raw

PATCH 1/1 ARM: mm: avoid attempting to flush the gate_vma with VIVT caches

Quoted 12 lines · Gilles Chanteperdrix
Quoted 11 lines · Will Deacon
Quoted 9 lines · Gilles Chanteperdrix
Quoted 6 lines · Gilles Chanteperdrix
Quoted 3 lines · Will Deacon

It may be writable, but we never actually write to it after it has been initialised so there's no need to worry about caching issues (the cache is flushed in devicemaps_init).

Except if CONFIG_TLS_REG_EMUL is enabled

is disabled I mean.

Quoted 8 lines
Quoted 3 lines

Well spotted! I disagree about the address being flushed though -- it looks to me like we flush from 0xffff0000 - 0xffff1000, which is what we want. Why do you think we're flushing from the linear mapping?

I do not think we're flushing from the linear mapping, I believe the address used by the elf_core_dump function (elf_core_dump -> kmap -> page_address), to copy the page data to the core is the linear mapping address, which is the reason why we need the flush at all.

Ok, good, sounds like we're singing the same tune at last. If you're happy with my proposed change to the original patch and Uros could re-test, then I think we're in business again.

Cheers,

Will

#12 lore raw

PATCH 1/1 ARM: mm: avoid attempting to flush the gate_vma with VIVT caches

Quoted 17 lines · Will Deacon
Quoted 16 lines · Gilles Chanteperdrix
Quoted 14 lines · Will Deacon
Quoted 9 lines · Gilles Chanteperdrix
Quoted 6 lines · Gilles Chanteperdrix
Quoted 3 lines · Will Deacon

It may be writable, but we never actually write to it after it has been initialised so there's no need to worry about caching issues (the cache is flushed in devicemaps_init).

Except if CONFIG_TLS_REG_EMUL is enabled

is disabled I mean.

Well spotted! I disagree about the address being flushed though -- it looks to me like we flush from 0xffff0000 - 0xffff1000, which is what we want. Why do you think we're flushing from the linear mapping?

Quoted 8 lines
Quoted 4 lines

I do not think we're flushing from the linear mapping, I believe the address used by the elf_core_dump function (elf_core_dump -> kmap -> page_address), to copy the page data to the core is the linear mapping address, which is the reason why we need the flush at all.

Ok, good, sounds like we're singing the same tune at last. If you're happy with my proposed change to the original patch and Uros could re-test, then I think we're in business again.

It is OK for me.

Signature · 1 line
                                                                Gilles.
#13 lore raw

PATCH 1/1 ARM: mm: avoid attempting to flush the gate_vma with VIVT caches

In reply to Will Deacon
Quoted 12 lines · Will Deacon
Quoted 12 lines
Quoted 11 lines
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

It may be writable, but we never actually write to it after it has been initialised so there's no need to worry about caching issues (the cache is flushed in devicemaps_init).

Except if CONFIG_TLS_REG_EMUL is enabled

is disabled I mean.

Well spotted! I disagree about the address being flushed though -- it looks to me like we flush from 0xffff0000 - 0xffff1000, which is what we want. Why do you think we're flushing from the linear mapping?

Quoted 8 lines
Quoted 4 lines

I do not think we're flushing from the linear mapping, I believe the address used by the elf_core_dump function (elf_core_dump -> kmap -> page_address), to copy the page data to the core is the linear mapping address, which is the reason why we need the flush at all.

Ok, good, sounds like we're singing the same tune at last. If you're happy with my proposed change to the original patch and Uros could re-test, then I think we're in business again.

The updated patch is effectively the same as the patch at [1], which works for our target as well. This patch was also tested on 3.4 branch, where it fixes the same problem.

For the patch, I can add:

[1] http://lists.infradead.org/pipermail/linux-arm-kernel/2012-June/105047.html

Thanks,
Uros.