New: /proc/*/maps fragments too quickly compared to 2.4
Subsystems (9)
Guys, Wolfgang has found what appears to be a serious mmap fragmentation problem with the mm_struct.free_area_cache.
Begin forwarded message:
Date: Tue, 19 Apr 2005 11:55:44 -0700
From: [email protected]
To: [email protected]
Subject: [Bug 4520] New: /proc/*/maps fragments too quickly compared to 2.4
http://bugme.osdl.org/show_bug.cgi?id=4520
Summary: /proc/*/maps fragments too quickly compared to 2.4
Kernel Version: 2.6.11.4
Status: NEW
Severity: normal
Owner: [email protected]
Submitter: [email protected]
Distribution: Suse 9.2
Hardware Environment: Dual AMD64 / 8GB memory
Software Environment: 64 bit kernel 2.6.11.2 or .4 running 32 bit application Problem Description:
The appended c program, compiled in 32 bit mode, runs on our 2.6.11.4 (64bit kernel) out of memory after a short while.
Once this happens the programs copies /proc/self/maps to stdout which is large and very fragmented.
The same program runs 'forever' and after that ;-) /proc/self/maps only contains a few entries of very large mmapped regions.
Steps to reproduce:
Compile the program below in 32 bit mode, run on 2.4 and 2.6 kernels.
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#define numMaps 600
#define largeArea 9500000
#define forEver 1000000
#define oneMeg 0x100000
voidaLLocator()
{
char* bvec[numMaps];
unsigned int i;
memset( bvec,0,sizeof(bvec));
for( i = 0; i < forEver ; ++i ) {
unsigned oidx;
unsigned kidx;
int len;
/* munmap old entries */
oidx = (i+numMaps/10) % numMaps;
len = (oidx & 7) ? ((oidx&7)* oneMeg) : largeArea; /* map size */
if( bvec[oidx] ) { munmap( bvec[oidx], len ); bvec[oidx] = 0; } /* mmap new ones */
kidx = i % numMaps;
len = (kidx & 7) ? ((kidx&7)* oneMeg) : largeArea; /* map size */
bvec[kidx] = (char*)(mmap(0, len, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0));
if( bvec[kidx] == (char*)(-1) ) {
printf("Failed after %d rounds\n", i);
break;
} }
}
int main() {
FILE *f;
int c;
aLLocator();
f = fopen( "/proc/self/maps", "r" );
while( (c = fgetc(f)) != EOF )
putchar(c);
fclose(f); return 0;
}
------- You are receiving this mail because: ------- You are the assignee for the bug, or are watching the assignee. --
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
On Sat, Apr 23, 2005 at 09:18:19PM -0700, Andrew Morton wrote:
Ingo, do you have time to look? I have "family duty" today and am travelling the next few days; the earliest I can look is thursday realistically.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 4 lines · Andrew Morton
Guys, Wolfgang has found what appears to be a serious mmap fragmentation problem with the mm_struct.free_area_cache.
Andrew asked me to send the appended patch also to the list for comments:
------
In addtion to the the free_area_cache I've added another member
called cached_hole_size which contains the largest hole we have found up to the position of free_area_cache. Thus if we come in with a new request we know that we better start from scratch if the requested length is less or equal to the cached_hole_size.
I've tried to patch all available architectures but of course have
not even tried to compile it. So far only i32 and x86_64 is tested.
It avoids fragmentation (as 2.4 did) and should be still faster
than the uncached version I hacked earlier. And yes - check how I implemented the biggest unsigned long (~0UL), I'm not sure if that is ok with your standards...
Wolfgang
arch/arm/mm/mmap.c +10 -2
@@ -73,8 +73,13 @@ (!vma || addr + len <= vma->vm_start))return addr; } - start_addr = addr = mm->free_area_cache; - +if ( len > mm->cached_hole_size ) + start_addr = addr = mm->free_area_cache; +else { + start_addr =TASK_UNMAPPED_BASE ; + mm->cached_hole_size =0 ; + } + full_search:if (do_align) addr =COLOUR_ALIGN (addr, pgoff);
@@ -90,6 +95,7 @@*/ if (start_addr !=TASK_UNMAPPED_BASE ) { start_addr = addr =TASK_UNMAPPED_BASE ; + mm->cached_hole_size =0 ; goto full_search; }return -ENOMEM ;
@@ -101,6 +107,8 @@ mm->free_area_cache = addr + len;return addr; } +if ( addr + mm->cached_hole_size < vma->vm_start ) + mm->cached_hole_size = vma->vm_start - addr; addr = vma->vm_end;if (do_align) addr =COLOUR_ALIGN (addr, pgoff);
arch/i386/mm/hugetlbpage.c +29 -6
@@ -298,7 +298,12 @@struct vm_area_struct *vma;unsigned long start_addr; - start_addr = mm->free_area_cache; +if ( len > mm->cached_hole_size ) + start_addr = mm->free_area_cache; +else { + start_addr =TASK_UNMAPPED_BASE ; + mm->cached_hole_size =0 ; + } full_search: addr =ALIGN (start_addr,HPAGE_SIZE );
@@ -312,6 +317,7 @@*/ if (start_addr !=TASK_UNMAPPED_BASE ) { start_addr =TASK_UNMAPPED_BASE ; + mm->cached_hole_size =0 ; goto full_search; }return -ENOMEM ;
@@ -320,6 +326,8 @@ mm->free_area_cache = addr + len;return addr; } +if ( addr + mm->cached_hole_size < vma->vm_start ) + mm->cached_hole_size = vma->vm_start - addr; addr =ALIGN (vma->vm_end,HPAGE_SIZE ); } }
@@ -331,12 +339,17 @@struct mm_struct *mm = current->mm;struct vm_area_struct *vma, *prev_vma;unsigned long base = mm->mmap_base, addr = addr0; +unsigned long largest_hole = mm->cached_hole_size;int first_time =1 ;/* don't allow allocations above current base */ if (mm->free_area_cache > base) mm->free_area_cache = base; +if ( len <= largest_hole ) { + largest_hole =0 ; + mm->free_area_cache = base; + } try_again:/* make sure it can fit in the remaining address space */ if (mm->free_area_cache < len)
@@ -357,13 +370,20 @@* vma->vm_start, use it: */ if (addr + len <= vma->vm_start && - (!prev_vma || (addr >= prev_vma->vm_end))) + (!prev_vma || (addr >= prev_vma->vm_end))) {/* remember the address as a hint for next time */ -return (mm->free_area_cache = addr); -else + mm->cached_hole_size = largest_hole; +return (mm->free_area_cache = addr); + }else /* pull free_area_cache down to the first hole */ -if (mm->free_area_cache == vma->vm_end) +if (mm->free_area_cache == vma->vm_end) { mm->free_area_cache = vma->vm_start; + mm->cached_hole_size = largest_hole; + } + +/* remember the largest hole we saw so far */ +if ( addr + largest_hole < vma->vm_start ) + largest_hole = vma->vm_start - addr;/* try just below the current vma->vm_start */ addr = (vma->vm_start - len) &HPAGE_MASK ;
@@ -376,6 +396,7 @@*/ if (first_time) { mm->free_area_cache = base; + largest_hole =0 ; first_time =0 ; goto try_again; }
@@ -386,6 +407,7 @@* allocations. */ mm->free_area_cache =TASK_UNMAPPED_BASE ; + mm->cached_hole_size = ~0UL ; addr =hugetlb_get_unmapped_area_bottomup (file, addr0, len, pgoff, flags);
@@ -393,7 +415,8 @@* Restore the topdown base: */ mm->free_area_cache = base; - + mm->cached_hole_size = ~0UL ; +return addr; }
arch/ia64/kernel/sys_ia64.c +12 -3
@@ -38,9 +38,15 @@if (REGION_NUMBER (addr) ==REGION_HPAGE ) addr =0 ;#endif -if (!addr) - addr = mm->free_area_cache; - +if (!addr) { +if ( len > mm->cached_hole_size ) + addr = mm->free_area_cache; +else { + addr =TASK_UNMAPPED_BASE ; + mm->cached_hole_size =0 ; + } + } +if (map_shared && (TASK_SIZE >0xfffffffful ))/* * For 64-bit tasks, align shared segments to 1MB to avoid potential
@@ -59,6 +65,7 @@if (start_addr !=TASK_UNMAPPED_BASE ) {/* Start a new search --- just in case we missed some holes. */ addr =TASK_UNMAPPED_BASE ; + mm->cached_hole_size =0 ; goto full_search; }return -ENOMEM ;
@@ -68,6 +75,8 @@ mm->free_area_cache = addr + len;return addr; } +if ( addr + mm->cached_hole_size < vma->vm_start ) + mm->cached_hole_size = vma->vm_start - addr; addr = (vma->vm_end + align_mask) & ~align_mask; } }
arch/ppc64/mm/hugetlbpage.c +29 -5
@@ -515,7 +515,12 @@ && !is_hugepage_only_range(addr,len))return addr; } - start_addr = addr = mm->free_area_cache; +if ( len > mm->cached_hole_size ) + start_addr = addr = mm->free_area_cache; +else { + start_addr = addr =TASK_UNMAPPED_BASE ; + mm->cached_hole_size =0 ; + } full_search: vma =find_vma (mm, addr);
@@ -539,6 +544,8 @@ mm->free_area_cache = addr + len;return addr; } +if ( addr + mm->cached_hole_size < vma->vm_start ) + mm->cached_hole_size = vma->vm_start - addr; addr = vma->vm_end; vma = vma->vm_next; }
@@ -546,6 +553,7 @@/* Make sure we didn't miss any holes */ if (start_addr !=TASK_UNMAPPED_BASE ) { start_addr = addr =TASK_UNMAPPED_BASE ; + mm->cached_hole_size =0 ; goto full_search; }return -ENOMEM ;
@@ -567,6 +575,7 @@struct vm_area_struct *vma, *prev_vma;struct mm_struct *mm = current->mm;unsigned long base = mm->mmap_base, addr = addr0; +unsigned long largest_hole = mm->cached_hole_size;int first_time =1 ;/* requested length too big for entire address space */
@@ -587,6 +596,10 @@return addr; } +if ( len <= largest_hole ) { + largest_hole =0 ; + mm->free_area_cache = base; + } try_again:/* make sure it can fit in the remaining address space */ if (mm->free_area_cache < len)
@@ -615,13 +628,21 @@* vma->vm_start, use it: */ if (addr+len <= vma->vm_start && - (!prev_vma || (addr >= prev_vma->vm_end))) + (!prev_vma || (addr >= prev_vma->vm_end))) {/* remember the address as a hint for next time */ -return (mm->free_area_cache = addr); + mm->cached_hole_size = largest_hole; +return (mm->free_area_cache = addr); + }else /* pull free_area_cache down to the first hole */ -if (mm->free_area_cache == vma->vm_end) +if (mm->free_area_cache == vma->vm_end) { mm->free_area_cache = vma->vm_start; + mm->cached_hole_size = largest_hole; + } + +/* remember the largest hole we saw so far */ +if ( addr + largest_hole < vma->vm_start ) + largest_hole = vma->vm_start - addr;/* try just below the current vma->vm_start */ addr = vma->vm_start-len;
@@ -634,6 +655,7 @@*/ if (first_time) { mm->free_area_cache = base; + largest_hole =0 ; first_time =0 ; goto try_again; }
@@ -644,12 +666,14 @@* allocations. */ mm->free_area_cache =TASK_UNMAPPED_BASE ; + mm->cached_hole_size = ~0UL ; addr =arch_get_unmapped_area (filp, addr0, len, pgoff, flags);/* * Restore the topdown base: */ mm->free_area_cache = base; - + mm->cached_hole_size = ~0UL ; +return addr; }
arch/sh/kernel/sys_sh.c +8 -0
@@ -79,6 +79,10 @@ (!vma || addr + len <= vma->vm_start))return addr; } +if ( len <= mm->cached_hole_size ) { + mm->cached_hole_size =0 ; + mm->free_area_cache =TASK_UNMAPPED_BASE ; + }if (flags &MAP_PRIVATE ) addr =PAGE_ALIGN (mm->free_area_cache);else
@@ -95,6 +99,7 @@*/ if (start_addr !=TASK_UNMAPPED_BASE ) { start_addr = addr =TASK_UNMAPPED_BASE ; + mm->cached_hole_size =0 ; goto full_search; }return -ENOMEM ;
@@ -106,6 +111,9 @@ mm->free_area_cache = addr + len;return addr; } +if ( addr + mm->cached_hole_size < vma->vm_start ) + mm->cached_hole_size = vma->vm_start - addr; + addr = vma->vm_end;if (!(flags &MAP_PRIVATE )) addr =COLOUR_ALIGN (addr);
arch/sparc64/kernel/sys_sparc.c +8 -0
@@ -84,6 +84,10 @@return addr; } +if ( len <= mm->cached_hole_size ) { + mm->cached_hole_size =0 ; + mm->free_area_cache =TASK_UNMAPPED_BASE ; + } start_addr = addr = mm->free_area_cache; task_size -= len;
@@ -103,6 +107,7 @@if (task_size < addr) {if (start_addr !=TASK_UNMAPPED_BASE ) { start_addr = addr =TASK_UNMAPPED_BASE ; + mm->cached_hole_size =0 ; goto full_search; }return -ENOMEM ;
@@ -114,6 +119,9 @@ mm->free_area_cache = addr + len;return addr; } +if ( addr + mm->cached_hole_size < vma->vm_start ) + mm->cached_hole_size = vma->vm_start - addr; + addr = vma->vm_end;if (do_color_align) addr =COLOUR_ALIGN (addr, pgoff);
arch/x86_64/ia32/ia32_aout.c +1 -0
@@ -312,6 +312,7 @@ current->mm->brk = ex.a_bss + (current->mm->start_brk =N_BSSADDR (ex)); current->mm->free_area_cache =TASK_UNMAPPED_BASE ; + current->mm->cached_hole_size =0 ; current->mm->rss =0 ; current->mm->mmap =NULL ;
arch/x86_64/kernel/sys_x86_64.c +8 -0
@@ -112,6 +112,10 @@ (!vma || addr + len <= vma->vm_start))return addr; } +if ( len <= mm->cached_hole_size ) { + mm->cached_hole_size =0 ; + mm->free_area_cache = begin; + } addr = mm->free_area_cache;if (addr < begin) addr = begin;
@@ -127,6 +131,7 @@*/ if (start_addr != begin) { start_addr = addr = begin; + mm->cached_hole_size =0 ; goto full_search; }return -ENOMEM ;
@@ -138,6 +143,9 @@ mm->free_area_cache = addr + len;return addr; } +if ( addr + mm->cached_hole_size < vma->vm_start ) + mm->cached_hole_size = vma->vm_start - addr; + addr = vma->vm_end; } }
fs/binfmt_aout.c +1 -0
@@ -316,6 +316,7 @@ current->mm->brk = ex.a_bss + (current->mm->start_brk =N_BSSADDR (ex)); current->mm->free_area_cache = current->mm->mmap_base; + current->mm->cached_hole_size = current->mm->cached_hole_size; current->mm->rss =0 ; current->mm->mmap =NULL ;
fs/binfmt_elf.c +2 -0
@@ -766,6 +766,8 @@change some of these later */ current->mm->rss =0 ; current->mm->free_area_cache = current->mm->mmap_base; + current->mm->cached_hole_size = current->mm->cached_hole_size; + retval =setup_arg_pages (bprm,STACK_TOP , executable_stack);if (retval <0 ) {send_sig (SIGKILL , current,0 );
fs/hugetlbfs/inode.c +5 -0
@@ -122,6 +122,11 @@ start_addr = mm->free_area_cache; +if (len <= mm->cached_hole_size ) + start_addr =TASK_UNMAPPED_BASE ; + + + full_search: addr =ALIGN (start_addr,HPAGE_SIZE );
include/linux/sched.h +3 -2
@@ -212,8 +212,9 @@ unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags);void (*unmap_area) (struct vm_area_struct *area); -unsigned long mmap_base;/* base of mmap area */ -unsigned long free_area_cache;/* first hole */ +unsigned long mmap_base;/* base of mmap area */ +unsigned long cached_hole_size;/* if non-zero, the largest hole below free_area_cache */ +unsigned long free_area_cache;/* first hole of size cached_hole_size or larger */ pgd_t * pgd;atomic_t mm_users;/* How many users with user space? */ atomic_t mm_count;/* How many references to "struct mm_struct" (users count as 1) */
kernel/fork.c +3 -1
@@ -173,6 +173,7 @@ mm->mmap =NULL ; mm->mmap_cache =NULL ; mm->free_area_cache = oldmm->mmap_base; + mm->cached_hole_size = ~0UL ; mm->map_count =0 ; mm->rss =0 ; mm->anon_rss =0 ;
@@ -301,7 +302,8 @@ mm->ioctx_list =NULL ; mm->default_kioctx = (struct kioctx )INIT_KIOCTX (mm->default_kioctx, *mm); mm->free_area_cache =TASK_UNMAPPED_BASE ; - + mm->cached_hole_size = ~0UL ; +if (likely (!mm_alloc_pgd (mm))) { mm->def_flags =0 ;return mm;
mm/mmap.c +44 -11
123 lines not on this page · raw message
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>