diff -urN 2.4.17pre6/arch/alpha/mm/fault.c silent/arch/alpha/mm/fault.c --- 2.4.17pre6/arch/alpha/mm/fault.c Sun Sep 23 21:11:28 2001 +++ silent/arch/alpha/mm/fault.c Sat Dec 8 13:09:14 2001 @@ -121,7 +121,7 @@ goto good_area; if (!(vma->vm_flags & VM_GROWSDOWN)) goto bad_area; - if (expand_stack(vma, address)) + if (expand_stack(vma, address, NULL)) goto bad_area; /* * Ok, we have a good vm_area for this memory access, so diff -urN 2.4.17pre6/arch/arm/mm/fault-common.c silent/arch/arm/mm/fault-common.c --- 2.4.17pre6/arch/arm/mm/fault-common.c Wed Oct 24 08:04:08 2001 +++ silent/arch/arm/mm/fault-common.c Sat Dec 8 13:09:14 2001 @@ -229,7 +229,7 @@ goto survive; check_stack: - if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr)) + if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr, NULL)) goto good_area; out: return fault; diff -urN 2.4.17pre6/arch/cris/mm/fault.c silent/arch/cris/mm/fault.c --- 2.4.17pre6/arch/cris/mm/fault.c Sat Aug 11 08:03:54 2001 +++ silent/arch/cris/mm/fault.c Sat Dec 8 13:09:14 2001 @@ -284,7 +284,7 @@ if (address + PAGE_SIZE < rdusp()) goto bad_area; } - if (expand_stack(vma, address)) + if (expand_stack(vma, address, NULL)) goto bad_area; /* diff -urN 2.4.17pre6/arch/i386/mm/fault.c silent/arch/i386/mm/fault.c --- 2.4.17pre6/arch/i386/mm/fault.c Wed Oct 10 02:14:56 2001 +++ silent/arch/i386/mm/fault.c Sat Dec 8 13:09:14 2001 @@ -34,7 +34,7 @@ */ int __verify_write(const void * addr, unsigned long size) { - struct vm_area_struct * vma; + struct vm_area_struct * vma, * prev_vma; unsigned long start = (unsigned long) addr; if (!size) @@ -80,7 +80,8 @@ check_stack: if (!(vma->vm_flags & VM_GROWSDOWN)) goto bad_area; - if (expand_stack(vma, start) == 0) + find_vma_prev(current->mm, start, &prev_vma); + if (expand_stack(vma, start, prev_vma) == 0) goto good_area; bad_area: @@ -150,7 +151,7 @@ { struct task_struct *tsk; struct mm_struct *mm; - struct vm_area_struct * vma; + struct vm_area_struct * vma, * prev_vma; unsigned long address; unsigned long page; unsigned long fixup; @@ -211,7 +212,8 @@ if (address + 32 < regs->esp) goto bad_area; } - if (expand_stack(vma, address)) + find_vma_prev(mm, address, &prev_vma); + if (expand_stack(vma, address, prev_vma)) goto bad_area; /* * Ok, we have a good vm_area for this memory access, so diff -urN 2.4.17pre6/arch/ia64/mm/fault.c silent/arch/ia64/mm/fault.c --- 2.4.17pre6/arch/ia64/mm/fault.c Fri Nov 23 08:20:51 2001 +++ silent/arch/ia64/mm/fault.c Sat Dec 8 13:09:14 2001 @@ -124,7 +124,7 @@ if (rgn_index(address) != rgn_index(vma->vm_start) || rgn_offset(address) >= RGN_MAP_LIMIT) goto bad_area; - if (expand_stack(vma, address)) + if (expand_stack(vma, address, NULL)) goto bad_area; } else { vma = prev_vma; diff -urN 2.4.17pre6/arch/m68k/mm/fault.c silent/arch/m68k/mm/fault.c --- 2.4.17pre6/arch/m68k/mm/fault.c Sun Apr 1 01:17:08 2001 +++ silent/arch/m68k/mm/fault.c Sat Dec 8 13:09:14 2001 @@ -120,7 +120,7 @@ if (address + 256 < rdusp()) goto map_err; } - if (expand_stack(vma, address)) + if (expand_stack(vma, address, NULL)) goto map_err; /* diff -urN 2.4.17pre6/arch/mips/mm/fault.c silent/arch/mips/mm/fault.c --- 2.4.17pre6/arch/mips/mm/fault.c Sat Jul 21 00:04:05 2001 +++ silent/arch/mips/mm/fault.c Sat Dec 8 13:09:14 2001 @@ -80,7 +80,7 @@ goto good_area; if (!(vma->vm_flags & VM_GROWSDOWN)) goto bad_area; - if (expand_stack(vma, address)) + if (expand_stack(vma, address, NULL)) goto bad_area; /* * Ok, we have a good vm_area for this memory access, so diff -urN 2.4.17pre6/arch/mips64/mm/fault.c silent/arch/mips64/mm/fault.c --- 2.4.17pre6/arch/mips64/mm/fault.c Sun Sep 23 21:11:29 2001 +++ silent/arch/mips64/mm/fault.c Sat Dec 8 13:09:14 2001 @@ -132,7 +132,7 @@ goto good_area; if (!(vma->vm_flags & VM_GROWSDOWN)) goto bad_area; - if (expand_stack(vma, address)) + if (expand_stack(vma, address, NULL)) goto bad_area; /* * Ok, we have a good vm_area for this memory access, so diff -urN 2.4.17pre6/arch/ppc/mm/fault.c silent/arch/ppc/mm/fault.c --- 2.4.17pre6/arch/ppc/mm/fault.c Wed Oct 10 02:14:58 2001 +++ silent/arch/ppc/mm/fault.c Sat Dec 8 13:09:14 2001 @@ -64,7 +64,7 @@ void do_page_fault(struct pt_regs *regs, unsigned long address, unsigned long error_code) { - struct vm_area_struct * vma; + struct vm_area_struct * vma, * prev_vma; struct mm_struct *mm = current->mm; siginfo_t info; int code = SEGV_MAPERR; @@ -111,7 +111,8 @@ goto good_area; if (!(vma->vm_flags & VM_GROWSDOWN)) goto bad_area; - if (expand_stack(vma, address)) + vma = find_vma_prev(mm, address, &prev_vma); + if (expand_stack(vma, address, prev_vma)) goto bad_area; good_area: diff -urN 2.4.17pre6/arch/s390/mm/fault.c silent/arch/s390/mm/fault.c --- 2.4.17pre6/arch/s390/mm/fault.c Fri Nov 23 08:20:52 2001 +++ silent/arch/s390/mm/fault.c Sat Dec 8 13:09:14 2001 @@ -179,7 +179,7 @@ goto good_area; if (!(vma->vm_flags & VM_GROWSDOWN)) goto bad_area; - if (expand_stack(vma, address)) + if (expand_stack(vma, address, NULL)) goto bad_area; /* * Ok, we have a good vm_area for this memory access, so diff -urN 2.4.17pre6/arch/s390x/mm/fault.c silent/arch/s390x/mm/fault.c --- 2.4.17pre6/arch/s390x/mm/fault.c Fri Nov 23 08:20:52 2001 +++ silent/arch/s390x/mm/fault.c Sat Dec 8 13:09:14 2001 @@ -179,7 +179,7 @@ goto good_area; if (!(vma->vm_flags & VM_GROWSDOWN)) goto bad_area; - if (expand_stack(vma, address)) + if (expand_stack(vma, address, NULL)) goto bad_area; /* * Ok, we have a good vm_area for this memory access, so diff -urN 2.4.17pre6/arch/sh/mm/fault.c silent/arch/sh/mm/fault.c --- 2.4.17pre6/arch/sh/mm/fault.c Wed Oct 24 08:04:10 2001 +++ silent/arch/sh/mm/fault.c Sat Dec 8 13:09:14 2001 @@ -74,7 +74,7 @@ check_stack: if (!(vma->vm_flags & VM_GROWSDOWN)) goto bad_area; - if (expand_stack(vma, start) == 0) + if (expand_stack(vma, start, NULL) == 0) goto good_area; bad_area: @@ -114,7 +114,7 @@ goto good_area; if (!(vma->vm_flags & VM_GROWSDOWN)) goto bad_area; - if (expand_stack(vma, address)) + if (expand_stack(vma, address, NULL)) goto bad_area; /* * Ok, we have a good vm_area for this memory access, so diff -urN 2.4.17pre6/arch/sparc/mm/fault.c silent/arch/sparc/mm/fault.c --- 2.4.17pre6/arch/sparc/mm/fault.c Thu Dec 6 20:07:27 2001 +++ silent/arch/sparc/mm/fault.c Sat Dec 8 13:09:14 2001 @@ -251,7 +251,7 @@ goto good_area; if(!(vma->vm_flags & VM_GROWSDOWN)) goto bad_area; - if(expand_stack(vma, address)) + if(expand_stack(vma, address, NULL)) goto bad_area; /* * Ok, we have a good vm_area for this memory access, so @@ -498,7 +498,7 @@ goto good_area; if(!(vma->vm_flags & VM_GROWSDOWN)) goto bad_area; - if(expand_stack(vma, address)) + if(expand_stack(vma, address, NULL)) goto bad_area; good_area: info.si_code = SEGV_ACCERR; diff -urN 2.4.17pre6/arch/sparc64/mm/fault.c silent/arch/sparc64/mm/fault.c --- 2.4.17pre6/arch/sparc64/mm/fault.c Sun Sep 23 21:11:30 2001 +++ silent/arch/sparc64/mm/fault.c Sat Dec 8 13:09:14 2001 @@ -340,7 +340,7 @@ goto good_area; if (!(vma->vm_flags & VM_GROWSDOWN)) goto bad_area; - if (expand_stack(vma, address)) + if (expand_stack(vma, address, NULL)) goto bad_area; /* * Ok, we have a good vm_area for this memory access, so diff -urN 2.4.17pre6/include/linux/mm.h silent/include/linux/mm.h --- 2.4.17pre6/include/linux/mm.h Sat Dec 8 12:34:34 2001 +++ silent/include/linux/mm.h Sat Dec 8 13:09:14 2001 @@ -548,11 +548,24 @@ #define GFP_DMA __GFP_DMA -/* vma is the first one with address < vma->vm_end, - * and even address < vma->vm_start. Have to extend vma. */ -static inline int expand_stack(struct vm_area_struct * vma, unsigned long address) +extern int heap_stack_gap; + +/* + * vma is the first one with address < vma->vm_end, + * and even address < vma->vm_start. Have to extend vma. + * + * Locking: vm_start can decrease under you if you only hold + * the read semaphore, you either need the write semaphore + * or both the read semaphore and the page_table_lock acquired + * if you want vm_start consistent. vm_end and the vma layout + * are just consistent with only the read semaphore acquired + * instead. + */ +static inline int expand_stack(struct vm_area_struct * vma, unsigned long address, + struct vm_area_struct * prev_vma) { unsigned long grow; + int err = -ENOMEM; /* * vma->vm_start/vm_end cannot change under us because the caller is required @@ -560,20 +573,23 @@ * before relocating the vma range ourself. */ address &= PAGE_MASK; - spin_lock(&vma->vm_mm->page_table_lock); + if (prev_vma && prev_vma->vm_end + (heap_stack_gap << PAGE_SHIFT) > address) + goto out; + spin_lock(&vma->vm_mm->page_table_lock); grow = (vma->vm_start - address) >> PAGE_SHIFT; if (vma->vm_end - address > current->rlim[RLIMIT_STACK].rlim_cur || - ((vma->vm_mm->total_vm + grow) << PAGE_SHIFT) > current->rlim[RLIMIT_AS].rlim_cur) { - spin_unlock(&vma->vm_mm->page_table_lock); - return -ENOMEM; - } + ((vma->vm_mm->total_vm + grow) << PAGE_SHIFT) > current->rlim[RLIMIT_AS].rlim_cur) + goto out_unlock; vma->vm_start = address; vma->vm_pgoff -= grow; vma->vm_mm->total_vm += grow; if (vma->vm_flags & VM_LOCKED) vma->vm_mm->locked_vm += grow; + err = 0; + out_unlock: spin_unlock(&vma->vm_mm->page_table_lock); - return 0; + out: + return err; } /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */ diff -urN 2.4.17pre6/include/linux/sysctl.h silent/include/linux/sysctl.h --- 2.4.17pre6/include/linux/sysctl.h Mon Nov 26 16:04:31 2001 +++ silent/include/linux/sysctl.h Sat Dec 8 13:09:14 2001 @@ -140,6 +140,7 @@ VM_PAGERDAEMON=8, /* struct: Control kswapd behaviour */ VM_PGT_CACHE=9, /* struct: Set page table cache parameters */ VM_PAGE_CLUSTER=10, /* int: set number of pages to swap together */ + VM_HEAP_STACK_GAP=11, /* int: page gap between heap and stack */ VM_MIN_READAHEAD=12, /* Min file readahead */ VM_MAX_READAHEAD=13 /* Max file readahead */ }; diff -urN 2.4.17pre6/kernel/sysctl.c silent/kernel/sysctl.c --- 2.4.17pre6/kernel/sysctl.c Mon Nov 26 16:04:31 2001 +++ silent/kernel/sysctl.c Sat Dec 8 13:09:14 2001 @@ -270,6 +270,8 @@ &pgt_cache_water, 2*sizeof(int), 0644, NULL, &proc_dointvec}, {VM_PAGE_CLUSTER, "page-cluster", &page_cluster, sizeof(int), 0644, NULL, &proc_dointvec}, + {VM_HEAP_STACK_GAP, "heap-stack-gap", + &heap_stack_gap, sizeof(int), 0644, NULL, &proc_dointvec}, {VM_MIN_READAHEAD, "min-readahead", &vm_min_readahead,sizeof(int), 0644, NULL, &proc_dointvec}, {VM_MAX_READAHEAD, "max-readahead", diff -urN 2.4.17pre6/mm/mmap.c silent/mm/mmap.c --- 2.4.17pre6/mm/mmap.c Tue Nov 6 02:04:54 2001 +++ silent/mm/mmap.c Sat Dec 8 13:16:16 2001 @@ -45,6 +45,7 @@ }; int sysctl_overcommit_memory; +int heap_stack_gap = 1; /* Check that a process has enough memory to allocate a * new virtual mapping. @@ -605,9 +606,15 @@ for (vma = find_vma(current->mm, addr); ; vma = vma->vm_next) { /* At this point: (!vma || addr < vma->vm_end). */ + unsigned long __heap_stack_gap; if (TASK_SIZE - len < addr) return -ENOMEM; - if (!vma || addr + len <= vma->vm_start) + if (!vma) + return addr; + __heap_stack_gap = 0; + if (vma->vm_flags & VM_GROWSDOWN) + __heap_stack_gap = heap_stack_gap << PAGE_SHIFT; + if (addr + len + __heap_stack_gap <= vma->vm_start) return addr; addr = vma->vm_end; } @@ -716,7 +723,7 @@ struct vm_area_struct * find_extend_vma(struct mm_struct * mm, unsigned long addr) { - struct vm_area_struct * vma; + struct vm_area_struct * vma, * prev_vma; unsigned long start; addr &= PAGE_MASK; @@ -728,7 +735,8 @@ if (!(vma->vm_flags & VM_GROWSDOWN)) return NULL; start = vma->vm_start; - if (expand_stack(vma, addr)) + find_vma_prev(mm, addr, &prev_vma); + if (expand_stack(vma, addr, prev_vma)) return NULL; if (vma->vm_flags & VM_LOCKED) { make_pages_present(addr, start);