Re: 2.6.23-rc3-mm1 - memory layout change? - lost support for MAP_32BIT? - mono crashes

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Jiri Kosina
Date: Thursday, August 23, 2007 - 2:28 am

On Wed, 22 Aug 2007, Zan Lynx wrote:

[...]

Hi Zan,

thanks for an excellent bugreport. Rather than throwing the whole 
pie-randomization and flexmmap support away, could you please test the 
patch below and let me know whether it fixes all your issues? Thanks.


From: Jiri Kosina <jkosina@suse.cz>

Handle MAP_32BIT flags properly in x86_64 flexmmap

We need to handle MAP_32BIT flags of mmap() properly for 64bit 
applications with filexible mmap layout.

This patch introduces x86_64-specific version of 
arch_get_unmapped_area_topdown() which differs from the generic one in 
handling of the MAP_32BIT flag -- when this flag is passed to mmap(), we 
stick back to the legacy layout for this particular mmap, which gives 
proper 32bit range.

Signed-off-by: Jiri Kosina <jkosina@suse.cz>

diff --git a/arch/x86_64/kernel/sys_x86_64.c b/arch/x86_64/kernel/sys_x86_64.c
index 4770b7a..0e44d08 100644
--- a/arch/x86_64/kernel/sys_x86_64.c
+++ b/arch/x86_64/kernel/sys_x86_64.c
@@ -16,6 +16,7 @@
 #include <linux/file.h>
 #include <linux/utsname.h>
 #include <linux/personality.h>
+#include <linux/random.h>
 
 #include <asm/uaccess.h>
 #include <asm/ia32.h>
@@ -69,6 +70,7 @@ static void find_start_end(unsigned long flags, unsigned long *begin,
 			   unsigned long *end)
 {
 	if (!test_thread_flag(TIF_IA32) && (flags & MAP_32BIT)) {
+		unsigned long new_begin;
 		/* This is usually used needed to map code in small
 		   model, so it needs to be in the first 31bit. Limit
 		   it to that.  This means we need to move the
@@ -78,6 +80,11 @@ static void find_start_end(unsigned long flags, unsigned long *begin,
 		   of playground for now. -AK */ 
 		*begin = 0x40000000; 
 		*end = 0x80000000;		
+		if (current->flags & PF_RANDOMIZE) {
+			new_begin = randomize_range(*begin, *begin + 0x02000000, 0);
+			if (new_begin)
+				*begin = new_begin;
+		}
 	} else {
 		*begin = TASK_UNMAPPED_BASE;
 		*end = TASK_SIZE; 
@@ -147,6 +154,97 @@ full_search:
 	}
 }
 
+
+unsigned long
+arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
+			  const unsigned long len, const unsigned long pgoff,
+			  const unsigned long flags)
+{
+	struct vm_area_struct *vma;
+	struct mm_struct *mm = current->mm;
+	unsigned long addr = addr0;
+
+	/* requested length too big for entire address space */
+	if (len > TASK_SIZE)
+		return -ENOMEM;
+
+	if (flags & MAP_FIXED)
+		return addr;
+
+	/* for MAP_32BIT mappings we force the legact mmap base */
+	if (!test_thread_flag(TIF_IA32) && (flags & MAP_32BIT))
+		goto bottomup;
+
+	/* requesting a specific address */
+	if (addr) {
+		addr = PAGE_ALIGN(addr);
+		vma = find_vma(mm, addr);
+		if (TASK_SIZE - len >= addr &&
+				(!vma || addr + len <= vma->vm_start))
+			return addr;
+	}
+
+	/* check if free_area_cache is useful for us */
+	if (len <= mm->cached_hole_size) {
+ 	        mm->cached_hole_size = 0;
+ 		mm->free_area_cache = mm->mmap_base;
+ 	}
+
+	/* either no address requested or can't fit in requested address hole */
+	addr = mm->free_area_cache;
+
+	/* make sure it can fit in the remaining address space */
+	if (addr > len) {
+		vma = find_vma(mm, addr-len);
+		if (!vma || addr <= vma->vm_start)
+			/* remember the address as a hint for next time */
+			return (mm->free_area_cache = addr-len);
+	}
+
+	if (mm->mmap_base < len)
+		goto bottomup;
+
+	addr = mm->mmap_base-len;
+
+	do {
+		/*
+		 * Lookup failure means no vma is above this address,
+		 * else if new region fits below vma->vm_start,
+		 * return with success:
+		 */
+		vma = find_vma(mm, addr);
+		if (!vma || addr+len <= vma->vm_start)
+			/* remember the address as a hint for next time */
+			return (mm->free_area_cache = addr);
+
+ 		/* remember the largest hole we saw so far */
+ 		if (addr + mm->cached_hole_size < vma->vm_start)
+ 		        mm->cached_hole_size = vma->vm_start - addr;
+
+		/* try just below the current vma->vm_start */
+		addr = vma->vm_start-len;
+	} while (len < vma->vm_start);
+
+bottomup:
+	/*
+	 * A failed mmap() very likely causes application failure,
+	 * so fall back to the bottom-up function here. This scenario
+	 * can happen with large stack limits and large mmap()
+	 * allocations.
+	 */
+	mm->cached_hole_size = ~0UL;
+  	mm->free_area_cache = TASK_UNMAPPED_BASE;
+	addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
+	/*
+	 * Restore the topdown base:
+	 */
+	mm->free_area_cache = mm->mmap_base;
+	mm->cached_hole_size = ~0UL;
+
+	return addr;
+}
+
+
 asmlinkage long sys_uname(struct new_utsname __user * name)
 {
 	int err;
diff --git a/include/asm-x86_64/pgtable.h b/include/asm-x86_64/pgtable.h
index c9d8764..8863d04 100644
--- a/include/asm-x86_64/pgtable.h
+++ b/include/asm-x86_64/pgtable.h
@@ -409,6 +409,7 @@ pte_t *lookup_address(unsigned long addr);
 		remap_pfn_range(vma, vaddr, pfn, size, prot)
 
 #define HAVE_ARCH_UNMAPPED_AREA
+#define HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
 
 #define pgtable_cache_init()   do { } while (0)
 
-
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
2.6.23-rc3-mm1, Andrew Morton, (Wed Aug 22, 2:06 am)
[BUG] fs/xfs/xfs_bmap_btree.c:2312: error: 'b' undeclared ..., Michal Piotrowski, (Wed Aug 22, 3:11 am)
Re: [BUG] fs/xfs/xfs_bmap_btree.c:2312: error: 'b' undecla ..., Michal Piotrowski, (Wed Aug 22, 3:27 am)
[BUG] 2.6.23-rc3-mm1 - kernel BUG at net/core/skbuff.c:95!, Kamalesh Babulal, (Wed Aug 22, 6:02 am)
Re: 2.6.23-rc3-mm1, Gabriel C, (Wed Aug 22, 6:33 am)
Re: 2.6.23-rc3-mm1, Michal Piotrowski, (Wed Aug 22, 7:19 am)
Re: 2.6.23-rc3-mm1, Andrew Morton, (Wed Aug 22, 9:09 am)
Re: 2.6.23-rc3-mm1, Andrew Morton, (Wed Aug 22, 9:17 am)
Re: 2.6.23-rc3-mm1: fix b43 compilation, Rafael J. Wysocki, (Wed Aug 22, 9:33 am)
Re: 2.6.23-rc3-mm1, Gabriel C, (Wed Aug 22, 10:01 am)
Re: net/ipv4/fib_trie.c - compile error (Re: 2.6.23-rc3-mm1), Paul E. McKenney, (Wed Aug 22, 10:03 am)
Re: 2.6.23-rc3-mm1, Mel Gorman, (Wed Aug 22, 10:17 am)
Re: 2.6.23-rc3-mm1, Torsten Kaiser, (Wed Aug 22, 10:24 am)
Re: 2.6.23-rc3-mm1: locking boot-time self-test failure, Mariusz Kozlowski, (Wed Aug 22, 10:26 am)
Re: 2.6.23-rc3-mm1: WARNING: during resume from suspend on ..., Rafael J. Wysocki, (Wed Aug 22, 10:30 am)
Re: 2.6.23-rc3-mm1, Randy Dunlap, (Wed Aug 22, 11:03 am)
Re: 2.6.23-rc3-mm1, Andrew Morton, (Wed Aug 22, 11:10 am)
Re: 2.6.23-rc3-mm1, Andrew Morton, (Wed Aug 22, 11:14 am)
Re: 2.6.23-rc3-mm1, Andrew Morton, (Wed Aug 22, 11:32 am)
Re: 2.6.23-rc3-mm1: kgdb build failure on powerpc, Mariusz Kozlowski, (Wed Aug 22, 12:04 pm)
Re: 2.6.23-rc3-mm1: net/wireless/rt2x00mac.c build failure, Mariusz Kozlowski, (Wed Aug 22, 12:16 pm)
Re: 2.6.23-rc3-mm1, Randy Dunlap, (Wed Aug 22, 12:17 pm)
Re: 2.6.23-rc3-mm1: net/wireless/rt2x00mac.c build failure, Ivo van Doorn, (Wed Aug 22, 12:31 pm)
Re: 2.6.23-rc3-mm1, Andi Kleen, (Wed Aug 22, 12:38 pm)
Re: 2.6.23-rc3-mm1: kgdb build failure on powerpc, Andrew Morton, (Wed Aug 22, 12:47 pm)
Re: 2.6.23-rc3-mm1: net/wireless/rt2x00mac.c build failure, Mariusz Kozlowski, (Wed Aug 22, 12:54 pm)
Re: 2.6.23-rc3-mm1: net/wireless/rt2x00mac.c build failure, John W. Linville, (Wed Aug 22, 12:58 pm)
Re: 2.6.23-rc3-mm1: net/wireless/rt2x00mac.c build failure, Rafael J. Wysocki, (Wed Aug 22, 1:22 pm)
Re: 2.6.23-rc3-mm1: inlining failures in sound/pci/hda/hda ..., Mariusz Kozlowski, (Wed Aug 22, 1:23 pm)
[-mm patch] enforce noreplace-smp in alternative_instructi ..., Frederik Deweerdt, (Wed Aug 22, 1:25 pm)
Re: 2.6.23-rc3-mm1, Andi Kleen, (Wed Aug 22, 1:53 pm)
Re: 2.6.23-rc3-mm1: inlining failures in sound/pci/hda/hda ..., Mariusz Kozlowski, (Wed Aug 22, 2:18 pm)
Re: 2.6.23-rc3-mm1: locking boot-time self-test failure, Frederik Deweerdt, (Wed Aug 22, 2:27 pm)
Re: 2.6.23-rc3-mm1: fix b43 compilation, Michael Buesch, (Wed Aug 22, 2:56 pm)
Re: 2.6.23-rc3-mm1: fix b43 compilation, John W. Linville, (Wed Aug 22, 7:56 pm)
Re: 2.6.23-rc3-mm1: fix b43 compilation, Andrew Morton, (Thu Aug 23, 12:07 am)
Re: 2.6.23-rc3-mm1 - memory layout change? - lost support ..., Jiri Kosina, (Thu Aug 23, 2:28 am)
Re: 2.6.23-rc3-mm1, Mel Gorman, (Thu Aug 23, 4:39 am)
Re: 2.6.23-rc3-mm1, Andy Whitcroft, (Thu Aug 23, 5:03 am)
Re: 2.6.23-rc3-mm1, Andi Kleen, (Thu Aug 23, 5:07 am)
Re: 2.6.23-rc3-mm1, Andi Kleen, (Thu Aug 23, 5:22 am)
Re: 2.6.23-rc3-mm1, Sam Ravnborg, (Thu Aug 23, 5:28 am)
Re: 2.6.23-rc3-mm1, Andy Whitcroft, (Thu Aug 23, 5:34 am)
2.6.23-rc3-mm1 - irda goes belly up, Valdis.Kletnieks, (Thu Aug 23, 6:33 am)
Re: 2.6.23-rc3-mm1, Sam Ravnborg, (Thu Aug 23, 7:24 am)
Re: 2.6.23-rc3-mm1, Mel Gorman, (Thu Aug 23, 9:25 am)
Re: 2.6.23-rc3-mm1 - irda goes belly up, Alexey Dobriyan, (Thu Aug 23, 10:37 am)
Re: 2.6.23-rc3-mm1 - irda goes belly up, Valdis.Kletnieks, (Thu Aug 23, 11:45 am)
Re: 2.6.23-rc3-mm1 - irda goes belly up, Andrew Morton, (Thu Aug 23, 2:16 pm)
Re: [-mm patch] enforce noreplace-smp in alternative_instr ..., Jeremy Fitzhardinge, (Thu Aug 23, 4:16 pm)
Re: 2.6.23-rc3-mm1 - irda goes belly up, Eric W. Biederman, (Thu Aug 23, 8:11 pm)
Re: 2.6.23-rc3-mm1 - irda goes belly up, Eric W. Biederman, (Thu Aug 23, 8:46 pm)
[PATCH 1/2] sysctl: Properly register the irda binary sysc ..., Eric W. Biederman, (Thu Aug 23, 8:53 pm)
[PATCH 2/2] sysctl: For irda update sysctl_checks list of ..., Eric W. Biederman, (Thu Aug 23, 8:55 pm)
Re: [-mm patch] enforce noreplace-smp in alternative_instr ..., Frederik Deweerdt, (Thu Aug 23, 11:04 pm)
Re: [-mm patch] enforce noreplace-smp in alternative_instr ..., Frederik Deweerdt, (Thu Aug 23, 11:06 pm)
Re: [-mm patch] enforce noreplace-smp in alternative_instr ..., Jeremy Fitzhardinge, (Thu Aug 23, 11:46 pm)
Re: [-mm patch] enforce noreplace-smp in alternative_instr ..., Frederik Deweerdt, (Fri Aug 24, 1:22 am)
Re: 2.6.23-rc3-mm1 - memory layout change? - lost support ..., Arjan van de Ven, (Fri Aug 24, 9:17 am)
Re: 2.6.23-rc3-mm1, Tilman Schmidt, (Fri Aug 24, 4:27 pm)
Re: 2.6.23-rc3-mm1, Andrew Morton, (Fri Aug 24, 5:07 pm)
RE: 2.6.23-rc3-mm1, Pallipadi, Venkatesh, (Fri Aug 24, 5:13 pm)
Re: 2.6.23-rc3-mm1, Dave Jones, (Fri Aug 24, 5:14 pm)
Re: 2.6.23-rc3-mm1, john stultz, (Fri Aug 24, 5:21 pm)
RE: 2.6.23-rc3-mm1, Pallipadi, Venkatesh, (Fri Aug 24, 5:38 pm)
Re: 2.6.23-rc3-mm1, Tilman Schmidt, (Fri Aug 24, 5:47 pm)
Re: 2.6.23-rc3-mm1, Andrew Morton, (Fri Aug 24, 8:30 pm)
Re: 2.6.23-rc3-mm1, Dave Jones, (Fri Aug 24, 9:28 pm)
Re: 2.6.23-rc3-mm1, Paul Rolland, (Sat Aug 25, 12:55 am)
Re: [PATCH 1/2] sysctl: Properly register the irda binary ..., Valdis.Kletnieks, (Sat Aug 25, 1:29 am)
Re: [-mm patch] enforce noreplace-smp in alternative_instr ..., Frederik Deweerdt, (Sat Aug 25, 5:23 am)
Re: [PATCH 1/2] sysctl: Properly register the irda binary ..., Eric W. Biederman, (Sat Aug 25, 5:57 am)
Re: [PATCH 1/2] sysctl: Properly register the irda binary ..., Valdis.Kletnieks, (Sat Aug 25, 7:07 am)
Re: [PATCH 1/2] sysctl: Properly register the irda binary ..., Eric W. Biederman, (Sat Aug 25, 10:59 am)
[PATCH] sysctl: Update sysctl_check to handle compiled out ..., Eric W. Biederman, (Sat Aug 25, 11:03 am)
Re: [-mm patch] enforce noreplace-smp in alternative_instr ..., Frederik Deweerdt, (Sat Aug 25, 2:14 pm)
Re: 2.6.23-rc3-mm1, Tilman Schmidt, (Sat Aug 25, 3:39 pm)
Re: 2.6.23-rc3-mm1, Tilman Schmidt, (Sat Aug 25, 4:26 pm)
Re: 2.6.23-rc3-mm1, Tilman Schmidt, (Sat Aug 25, 4:37 pm)
Re: 2.6.23-rc3-mm1, Randy Dunlap, (Sat Aug 25, 4:57 pm)
X freezes kernel during exit [Re: 2.6.23-rc3-mm1], Jiri Slaby, (Sun Aug 26, 6:04 am)
Re: net/ipv4/fib_trie.c - compile error (Re: 2.6.23-rc3-mm1), Jarek Poplawski, (Sun Aug 26, 11:36 pm)
Re: 2.6.23-rc3-mm1, Tilman Schmidt, (Mon Aug 27, 6:35 am)
Re: net/ipv4/fib_trie.c - compile error (Re: 2.6.23-rc3-mm1), Paul E. McKenney, (Mon Aug 27, 9:23 am)
[-mm patch] remove parport_device_num(), Adrian Bunk, (Mon Aug 27, 2:27 pm)
[-mm patch] make do_restart_poll() static, Adrian Bunk, (Mon Aug 27, 2:27 pm)
[-mm patch] unexport snd_ctl_elem_{read,write}, Adrian Bunk, (Mon Aug 27, 2:27 pm)
[-mm patch] unexport sys_{open,read}, Adrian Bunk, (Mon Aug 27, 2:27 pm)
[-mm patch] make types.h usable for non-gcc C parsers, Adrian Bunk, (Mon Aug 27, 2:27 pm)
2.6.23-rc3-mm1: m32r defconfig compile error, Adrian Bunk, (Mon Aug 27, 2:27 pm)
[-mm patch] remove unwind exports, Adrian Bunk, (Mon Aug 27, 2:27 pm)
[-mm patch] unexport noautodma, Adrian Bunk, (Mon Aug 27, 2:28 pm)
[-mm patch] mousedev.c:mixdev_open_devices() bugfix, Adrian Bunk, (Mon Aug 27, 2:28 pm)
[-mm patch] ivtv-fb.c bugfix, Adrian Bunk, (Mon Aug 27, 2:29 pm)
[-mm patch] iwl-base.c bugfixes, Adrian Bunk, (Mon Aug 27, 2:29 pm)
Re: [-mm patch] make types.h usable for non-gcc C parsers, Mike Frysinger, (Mon Aug 27, 2:34 pm)
Re: [-mm patch] make types.h usable for non-gcc C parsers, Mike Frysinger, (Mon Aug 27, 2:42 pm)
Re: [-mm patch] iwl-base.c bugfixes, Tomas Winkler, (Mon Aug 27, 3:34 pm)
Re: [-mm patch] unexport sys_{open,read}, Arjan van de Ven, (Mon Aug 27, 3:53 pm)
Re: [-mm patch] unexport sys_{open,read}, Adrian Bunk, (Mon Aug 27, 4:17 pm)
Re: 2.6.23-rc3-mm1: m32r defconfig compile error, Hirokazu Takata, (Mon Aug 27, 8:50 pm)
Re: [v4l-dvb-maintainer] [-mm patch] ivtv-fb.c bugfix, Hans Verkuil, (Mon Aug 27, 11:30 pm)
Re: [-mm patch] make types.h usable for non-gcc C parsers, Andrew Morton, (Tue Aug 28, 12:37 am)
oops at sr_block_release [Re: 2.6.23-rc3-mm1], Jiri Slaby, (Tue Aug 28, 4:32 am)
Re: oops at sr_block_release [Re: 2.6.23-rc3-mm1], Satyam Sharma, (Tue Aug 28, 8:08 am)
Re: oops at sr_block_release [Re: 2.6.23-rc3-mm1], Jiri Slaby, (Tue Aug 28, 8:21 am)
Re: [-mm patch] make types.h usable for non-gcc C parsers, Sam Ravnborg, (Tue Aug 28, 10:06 am)
Re: [-mm patch] make types.h usable for non-gcc C parsers, Mike Frysinger, (Tue Aug 28, 10:42 am)
Re: [-mm patch] make types.h usable for non-gcc C parsers, Sam Ravnborg, (Tue Aug 28, 11:37 am)
Re: [PATCH 1/2] sysctl: Properly register the irda binary ..., Valdis.Kletnieks, (Tue Aug 28, 11:40 am)
Re: [PATCH] sysctl: Update sysctl_check to handle compiled ..., Valdis.Kletnieks, (Tue Aug 28, 11:44 am)
Re: [PATCH 1/2] sysctl: Properly register the irda binary ..., Eric W. Biederman, (Tue Aug 28, 2:06 pm)
Re: oops at sr_block_release [Re: 2.6.23-rc3-mm1], Andrew Morton, (Tue Aug 28, 7:58 pm)
Re: 2.6.23-rc3-mm1, Valdis.Kletnieks, (Wed Aug 29, 7:04 am)
Re: 2.6.23-rc3-mm1 - vdso and gettimeofday issues with glibc, Valdis.Kletnieks, (Wed Aug 29, 10:37 am)
Re: [Kgdb-bugreport] 2.6.23-rc3-mm1: kgdb build failure on ..., Pete/Piet Delaney, (Wed Aug 29, 4:43 pm)
Re: [Kgdb-bugreport] 2.6.23-rc3-mm1: kgdb build failure on ..., Pete/Piet Delaney, (Wed Aug 29, 5:05 pm)
Re: [Kgdb-bugreport] 2.6.23-rc3-mm1: kgdb build failure on ..., Pete/Piet Delaney, (Wed Aug 29, 6:19 pm)
Re: 2.6.23-rc3-mm1 - vdso and gettimeofday issues with glibc, Valdis.Kletnieks, (Thu Aug 30, 7:08 am)
[PATCH] Fix out-by-one error in traps.c, Rusty Russell, (Thu Aug 30, 3:14 pm)
Re: [PATCH] Fix out-by-one error in traps.c, Linus Torvalds, (Thu Aug 30, 9:44 pm)
Re: [PATCH] Fix out-by-one error in traps.c, Rusty Russell, (Thu Aug 30, 11:03 pm)
Re: [PATCH] Fix out-by-one error in traps.c, Linus Torvalds, (Fri Aug 31, 12:51 am)
Re: [PATCH] Fix out-by-one error in traps.c, Rusty Russell, (Fri Aug 31, 10:37 am)
Re: [PATCH] Fix out-by-one error in traps.c, Linus Torvalds, (Fri Aug 31, 11:24 am)
Re: [PATCH] Fix out-by-one error in traps.c, Rusty Russell, (Tue Sep 4, 11:18 am)
Re: 2.6.23-rc3-mm1 - vdso and gettimeofday issues with glibc, Valdis.Kletnieks, (Sat Sep 8, 5:24 pm)
Re: 2.6.23-rc3-mm1 - vdso and gettimeofday issues with glibc, Valdis.Kletnieks, (Sat Sep 8, 8:20 pm)
Re: X freezes kernel during exit [Re: 2.6.23-rc3-mm1], Andrew Morton, (Sun Sep 9, 5:47 am)
Re: 2.6.23-rc3-mm1 - vdso and gettimeofday issues with glibc, Valdis.Kletnieks, (Mon Sep 10, 12:07 pm)
Re: X freezes kernel during exit [Re: 2.6.23-rc3-mm1], Dave Airlie, (Tue Sep 11, 8:18 am)