Skip to contentSkip to conversation

ARM: use cache type functions for arch_get_unmapped_area

PATCH Superseded Rob Herring 3 participants 12 messages Nov 7, 2011 – Nov 16, 2011 Arm (32-bit)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 ARM: use cache type functions for arch_get_unmapped_area

From: Rob Herring <[email protected]>

There are already cache type decoding functions, so use those instead of custom decode code which only works for ARMv6.

This change also correctly enables cache colour alignment on Cortex-A9 whose I-cache is aliasing VIPT.

---
 arch/arm/mm/mmap.c |   23 ++++++-----------------
 1 files changed, 6 insertions(+), 17 deletions(-)
arch/arm/mm/mmap.c +6 -17
index 74be05f..80e70ef 100644
@@ -9,8 +9,7 @@
 #include <linux/io.h>
 #include <linux/personality.h>
 #include <linux/random.h>
-#include <asm/cputype.h>
-#include <asm/system.h>
+#include <asm/cachetype.h>
 
 #define COLOUR_ALIGN(addr,pgoff)		\
 	((((addr)+SHMLBA-1)&~(SHMLBA-1)) +	\
@@ -32,25 +31,15 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,
 	struct mm_struct *mm = current->mm;
 	struct vm_area_struct *vma;
 	unsigned long start_addr;
-#if defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V6K)
-	unsigned int cache_type;
-	int do_align = 0, aliasing = 0;
+	int do_align = 0;
+	int aliasing = cache_is_vipt_aliasing() || icache_is_vipt_aliasing();
 
 	/*
 	 * We only need to do colour alignment if either the I or D
-	 * caches alias.  This is indicated by bits 9 and 21 of the
-	 * cache type register.
+	 * caches alias.
 	 */
-	cache_type = read_cpuid_cachetype();
-	if (cache_type != read_cpuid_id()) {
-		aliasing = (cache_type | cache_type >> 12) & (1 << 11);
-		if (aliasing)
-			do_align = filp || flags & MAP_SHARED;
-	}
-#else
-#define do_align 0
-#define aliasing 0
-#endif
+	if (aliasing)
+		do_align = filp || (flags & MAP_SHARED);
 
 	/*
 	 * We enforce the MAP_FIXED case.
Signature · 1 line
1.7.5.4
#1 lore raw

PATCH 2/2 ARM: topdown mmap support

From: Rob Herring <[email protected]>

Similar to other architectures, this adds topdown mmap support in user process address space allocation policy. This allows mmap sizes greater than 2GB. This support is largely copied from MIPS and the generic implementations.

The address space randomization is moved into arch_pick_mmap_layout.

Tested on V-Express with ubuntu and a mmap test from here:
https://bugs.launchpad.net/bugs/861296

---
 arch/arm/include/asm/pgtable.h   |    1 +
 arch/arm/include/asm/processor.h |    2 +
 arch/arm/mm/mmap.c               |  173 ++++++++++++++++++++++++++++++++++++-
 3 files changed, 171 insertions(+), 5 deletions(-)
arch/arm/include/asm/pgtable.h +1 -0
index 9451dce..2f659e2 100644
@@ -336,6 +336,7 @@ static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
  * We provide our own arch_get_unmapped_area to cope with VIPT caches.
  */
 #define HAVE_ARCH_UNMAPPED_AREA
+#define HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
 
 /*
  * remap a physical page `pfn' of size `size' with page protection `prot'
arch/arm/include/asm/processor.h +2 -0
index b2d9df5..ce280b8 100644
@@ -123,6 +123,8 @@ static inline void prefetch(const void *ptr)
 
 #endif
 
+#define HAVE_ARCH_PICK_MMAP_LAYOUT
+
 #endif
 
 #endif /* __ASM_ARM_PROCESSOR_H */
arch/arm/mm/mmap.c +168 -5

197 lines not on this page · raw message

Signature · 1 line
1.7.5.4
#2 lore raw

PATCH 2/2 ARM: topdown mmap support

Quoted 8 lines · Rob Herring

From: Rob Herring <[email protected]>

Similar to other architectures, this adds topdown mmap support in user process address space allocation policy. This allows mmap sizes greater than 2GB. This support is largely copied from MIPS and the generic implementations.

The address space randomization is moved into arch_pick_mmap_layout.

This is a problem by effectively weakening the randomization greatly. Now you get a random starting point but all mmaps are otherwise fixed relative to each other, whereas you had random distances between each mmaps before.

Nicolas

#3 lore raw

PATCH 2/2 ARM: topdown mmap support

Quoted 9 lines · Nicolas Pitre
Quoted 7 lines · Rob Herring

From: Rob Herring <[email protected]>

Similar to other architectures, this adds topdown mmap support in user process address space allocation policy. This allows mmap sizes greater than 2GB. This support is largely copied from MIPS and the generic implementations.

Quoted 7 lines
Quoted 1 line

The address space randomization is moved into arch_pick_mmap_layout.

This is a problem by effectively weakening the randomization greatly. Now you get a random starting point but all mmaps are otherwise fixed relative to each other, whereas you had random distances between each mmaps before.

You mean within a single process the mmap's are not randomized? Couldn't that end up wasting a lot of virtual space if you have a lot of mmaps?

It is aligned with other arch's and the generic implementation. The generic implementation doesn't even do randomization for legacy layouts.

Rob

#4 lore raw

PATCH 2/2 ARM: topdown mmap support

Quoted 12 lines · Rob Herring
Quoted 11 lines · Nicolas Pitre
Quoted 8 lines · Rob Herring

From: Rob Herring <[email protected]>

Similar to other architectures, this adds topdown mmap support in user process address space allocation policy. This allows mmap sizes greater than 2GB. This support is largely copied from MIPS and the generic implementations.

The address space randomization is moved into arch_pick_mmap_layout.

Quoted 7 lines
Quoted 5 lines

This is a problem by effectively weakening the randomization greatly. Now you get a random starting point but all mmaps are otherwise fixed relative to each other, whereas you had random distances between each mmaps before.

You mean within a single process the mmap's are not randomized?

With regard to each other: not anymore with your patch.

Quoted 2 lines

Couldn't that end up wasting a lot of virtual space if you have a lot of mmaps?

That is what we have now without this patch. The Potential for wasted space is 8 bits on a page level, i.e. 1MB max or 512 KB on average, per mmap. I don't think this is that bad.

Quoted 2 lines

It is aligned with other arch's and the generic implementation. The generic implementation doesn't even do randomization for legacy layouts.

Only x86 does complete ASLR besides ARM.

Nicolas

#5 lore raw

PATCH 2/2 ARM: topdown mmap support

Nicolas,

Quoted 22 lines · Nicolas Pitre
Quoted 12 lines · Rob Herring
Quoted 11 lines · Nicolas Pitre
Quoted 8 lines · Rob Herring

From: Rob Herring <[email protected]>

Similar to other architectures, this adds topdown mmap support in user process address space allocation policy. This allows mmap sizes greater than 2GB. This support is largely copied from MIPS and the generic implementations.

The address space randomization is moved into arch_pick_mmap_layout.

Quoted 7 lines
Quoted 5 lines

This is a problem by effectively weakening the randomization greatly. Now you get a random starting point but all mmaps are otherwise fixed relative to each other, whereas you had random distances between each mmaps before.

You mean within a single process the mmap's are not randomized?

Quoted 9 lines

With regard to each other: not anymore with your patch.

Quoted 2 lines

Couldn't that end up wasting a lot of virtual space if you have a lot of mmaps?

That is what we have now without this patch. The Potential for wasted space is 8 bits on a page level, i.e. 1MB max or 512 KB on average, per mmap. I don't think this is that bad.

No one cared about or noticed this issue until recently. That is changing as we start to see ARM systems with more memory and I'm sure we'll see more issues like this. Someone may care that they get less memory than other 32-bit arches. Can you really trust that apps don't do a large number of mmaps.

Quoted 4 lines
Quoted 2 lines

It is aligned with other arch's and the generic implementation. The generic implementation doesn't even do randomization for legacy layouts.

Only x86 does complete ASLR besides ARM.

x86 does not do per mmap randomization. Here's the output of a test program which does 1 to 23 MB size mmaps in 1MB steps. The gap is the last address - current address. I'm not sure what's going on with the 2nd mmap gap though, but otherwise the gap matches the mmap size.

mmap at 0x7f74bd1b7000, gap = 42e49000
mmap at 0x7f74bcb1e000, gap = 699000
mmap at 0x7f74bc81e000, gap = 300000
mmap at 0x7f74bc41e000, gap = 400000
mmap at 0x7f74bbf1e000, gap = 500000
mmap at 0x7f74bb91e000, gap = 600000
mmap at 0x7f74bb21e000, gap = 700000
mmap at 0x7f74baa1e000, gap = 800000
mmap at 0x7f74ba11e000, gap = 900000
mmap at 0x7f74b971e000, gap = a00000
mmap at 0x7f74b8c1e000, gap = b00000
mmap at 0x7f74b801e000, gap = c00000
mmap at 0x7f74b731e000, gap = d00000
mmap at 0x7f74b651e000, gap = e00000
mmap at 0x7f74b561e000, gap = f00000
mmap at 0x7f74b461e000, gap = 1000000
mmap at 0x7f74b351e000, gap = 1100000
mmap at 0x7f74b231e000, gap = 1200000
mmap at 0x7f74b101e000, gap = 1300000
mmap at 0x7f74afc1e000, gap = 1400000
mmap at 0x7f74ae71e000, gap = 1500000
mmap at 0x7f74ad11e000, gap = 1600000

My patch exceeds 32-bit x86 functionality which does no randomization for legacy layouts. It matches 32-bit and 64-bit x86 for topdown layout.

I'm happy to revert the legacy part of my patch, but it's a bit mute point as legacy is not used by default.

Rob

#6 lore raw

PATCH 2/2 ARM: topdown mmap support

Quoted 44 lines · Rob Herring

Nicolas,

Quoted 22 lines · Nicolas Pitre
Quoted 12 lines · Rob Herring
Quoted 11 lines · Nicolas Pitre
Quoted 8 lines · Rob Herring

From: Rob Herring <[email protected]>

Similar to other architectures, this adds topdown mmap support in user process address space allocation policy. This allows mmap sizes greater than 2GB. This support is largely copied from MIPS and the generic implementations.

The address space randomization is moved into arch_pick_mmap_layout.

Quoted 7 lines
Quoted 5 lines

This is a problem by effectively weakening the randomization greatly. Now you get a random starting point but all mmaps are otherwise fixed relative to each other, whereas you had random distances between each mmaps before.

You mean within a single process the mmap's are not randomized?

Quoted 9 lines

With regard to each other: not anymore with your patch.

Quoted 2 lines

Couldn't that end up wasting a lot of virtual space if you have a lot of mmaps?

That is what we have now without this patch. The Potential for wasted space is 8 bits on a page level, i.e. 1MB max or 512 KB on average, per mmap. I don't think this is that bad.

No one cared about or noticed this issue until recently. That is changing as we start to see ARM systems with more memory and I'm sure we'll see more issues like this. Someone may care that they get less memory than other 32-bit arches. Can you really trust that apps don't do a large number of mmaps.

Quoted 3 lines
Quoted 2 lines

It is aligned with other arch's and the generic implementation. The generic implementation doesn't even do randomization for legacy layouts.

Quoted 6 lines
Quoted 1 line

Only x86 does complete ASLR besides ARM.

x86 does not do per mmap randomization. Here's the output of a test program which does 1 to 23 MB size mmaps in 1MB steps. The gap is the last address - current address. I'm not sure what's going on with the 2nd mmap gap though, but otherwise the gap matches the mmap size.

OK, after verification I agree.

Quoted 2 lines

My patch exceeds 32-bit x86 functionality which does no randomization for legacy layouts. It matches 32-bit and 64-bit x86 for topdown layout.

I don't think we should skip randomization for the legacy layout. Without your patch we have only one layout and it is already randomized by default. There are other knobs for inhibiting randomization already.

Quoted 2 lines

I'm happy to revert the legacy part of my patch, but it's a bit mute point as legacy is not used by default.

I'm actually wondering if we should carry a legacy layout at all on ARM...

Otherwise for the patch:

Nicolas

#7 lore raw

PATCH 1/2 ARM: use cache type functions for arch_get_unmapped_area

In reply to Rob Herring
Quoted 9 lines · Rob Herring

From: Rob Herring <[email protected]>

There are already cache type decoding functions, so use those instead of custom decode code which only works for ARMv6.

This change also correctly enables cache colour alignment on Cortex-A9 whose I-cache is aliasing VIPT.

Quoted 52 lines
---
 arch/arm/mm/mmap.c |   23 ++++++-----------------
 1 files changed, 6 insertions(+), 17 deletions(-)
arch/arm/mm/mmap.c +6 -17
index 74be05f..80e70ef 100644
@@ -9,8 +9,7 @@
 #include <linux/io.h>
 #include <linux/personality.h>
 #include <linux/random.h>
-#include <asm/cputype.h>
-#include <asm/system.h>
+#include <asm/cachetype.h>
 
 #define COLOUR_ALIGN(addr,pgoff)		\
 	((((addr)+SHMLBA-1)&~(SHMLBA-1)) +	\
@@ -32,25 +31,15 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,
 	struct mm_struct *mm = current->mm;
 	struct vm_area_struct *vma;
 	unsigned long start_addr;
-#if defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V6K)
-	unsigned int cache_type;
-	int do_align = 0, aliasing = 0;
+	int do_align = 0;
+	int aliasing = cache_is_vipt_aliasing() || icache_is_vipt_aliasing();
 
 	/*
 	 * We only need to do colour alignment if either the I or D
-	 * caches alias.  This is indicated by bits 9 and 21 of the
-	 * cache type register.
+	 * caches alias.
 	 */
-	cache_type = read_cpuid_cachetype();
-	if (cache_type != read_cpuid_id()) {
-		aliasing = (cache_type | cache_type >> 12) & (1 << 11);
-		if (aliasing)
-			do_align = filp || flags & MAP_SHARED;
-	}
-#else
-#define do_align 0
-#define aliasing 0
-#endif
+	if (aliasing)
+		do_align = filp || (flags & MAP_SHARED);
 
 	/*
 	 * We enforce the MAP_FIXED case.
Signature · 1 line
1.7.5.4
#8 lore raw

PATCH 1/2 ARM: use cache type functions for arch_get_unmapped_area

Hi Rob, Nico,

Quoted 9 lines · Nicolas Pitre
Quoted 7 lines · Rob Herring

From: Rob Herring <[email protected]>

There are already cache type decoding functions, so use those instead of custom decode code which only works for ARMv6.

This change also correctly enables cache colour alignment on Cortex-A9 whose I-cache is aliasing VIPT.

I appreciate that this is preserving the current behaviour, but why do we need to ensure colour alignment when only the I-side is aliasing?

In the case that there is a MAP_SHARED, PROT_WRITE|PROT_EXEC mapping then explicit cache maintenance will still be required by the task writing to the page and also the task executing the written data (not to mention the synchronisation requirements). Russell confirmed this recently:

http://lists.arm.linux.org.uk/lurker/message/20110923.193941.47decb13.en.html

no amount of colouring can avoid that, so why not only bother with it when we alias on the D-side? On a coherent system it's reasonable to expect that to work across tasks, so we definitely need the colouring correction there.

Will

#9 lore raw

PATCH 1/2 ARM: use cache type functions for arch_get_unmapped_area

Quoted 21 lines · Will Deacon

Hi Rob, Nico,

Quoted 9 lines · Nicolas Pitre
Quoted 7 lines · Rob Herring

From: Rob Herring <[email protected]>

There are already cache type decoding functions, so use those instead of custom decode code which only works for ARMv6.

This change also correctly enables cache colour alignment on Cortex-A9 whose I-cache is aliasing VIPT.

I appreciate that this is preserving the current behaviour, but why do we need to ensure colour alignment when only the I-side is aliasing?

In the case that there is a MAP_SHARED, PROT_WRITE|PROT_EXEC mapping then explicit cache maintenance will still be required by the task writing to the page and also the task executing the written data (not to mention the synchronisation requirements). Russell confirmed this recently:

Quoted 5 lines

http://lists.arm.linux.org.uk/lurker/message/20110923.193941.47decb13.en.html

no amount of colouring can avoid that, so why not only bother with it when we alias on the D-side? On a coherent system it's reasonable to expect that to work across tasks, so we definitely need the colouring correction there.

I wasn't too sure about that, so I'll drop the i-cache part.

Rob

#10 lore raw

PATCH 1/2 ARM: use cache type functions for arch_get_unmapped_area

Quoted 14 lines · Rob Herring
Quoted 13 lines · Will Deacon

Hi Rob, Nico,

Quoted 9 lines · Nicolas Pitre
Quoted 7 lines · Rob Herring

From: Rob Herring <[email protected]>

There are already cache type decoding functions, so use those instead of custom decode code which only works for ARMv6.

This change also correctly enables cache colour alignment on Cortex-A9 whose I-cache is aliasing VIPT.

Quoted 8 lines
Quoted 8 lines

I appreciate that this is preserving the current behaviour, but why do we need to ensure colour alignment when only the I-side is aliasing?

In the case that there is a MAP_SHARED, PROT_WRITE|PROT_EXEC mapping then explicit cache maintenance will still be required by the task writing to the page and also the task executing the written data (not to mention the synchronisation requirements). Russell confirmed this recently:

What if you have 2 tasks that are executing code from the same page? Perhaps it doesn't matter as the I caches are separate?

Quoted 8 lines
Quoted 5 lines

http://lists.arm.linux.org.uk/lurker/message/20110923.193941.47decb13.en.html

no amount of colouring can avoid that, so why not only bother with it when we alias on the D-side? On a coherent system it's reasonable to expect that to work across tasks, so we definitely need the colouring correction there.

I wasn't too sure about that, so I'll drop the i-cache part.

However, the original code and comments did check for aliasing I cache. So dropping I-cache check could change v6 behavior if there are any systems with only aliasing I cache.

Rob

#11 lore raw

PATCH 1/2 ARM: use cache type functions for arch_get_unmapped_area

Quoted 10 lines · Rob Herring
Quoted 6 lines · Rob Herring
Quoted 5 lines · Will Deacon

In the case that there is a MAP_SHARED, PROT_WRITE|PROT_EXEC mapping then explicit cache maintenance will still be required by the task writing to the page and also the task executing the written data (not to mention the synchronisation requirements). Russell confirmed this recently:

What if you have 2 tasks that are executing code from the same page? Perhaps it doesn't matter as the I caches are separate?

Indeed, in that case there's no problem. The only case where you might try to use this is when you have one task writing code and another one executing it. But in this case, the task doing the executing will need to take care of the I-cache invalidation anyway because it could be VIVT ASID-tagged.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

http://lists.arm.linux.org.uk/lurker/message/20110923.193941.47decb13.en.html

no amount of colouring can avoid that, so why not only bother with it when we alias on the D-side? On a coherent system it's reasonable to expect that to work across tasks, so we definitely need the colouring correction there.

Quoted 6 lines
Quoted 2 lines

I wasn't too sure about that, so I'll drop the i-cache part.

However, the original code and comments did check for aliasing I cache. So dropping I-cache check could change v6 behavior if there are any systems with only aliasing I cache.

It shouldn't affect correct code but I can always post this as a separate patch if you're not happy with it.

Will