mmap: don't return ENOMEM when mapcount is temporarily exceeded in munmap()
authorKOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
Tue, 15 Dec 2009 01:57:56 +0000 (17:57 -0800)
committerLinus Torvalds <torvalds@linux-foundation.org>
Tue, 15 Dec 2009 16:53:11 +0000 (08:53 -0800)
commit659ace584e7a9fdda872eab4d6d7be1e0afb6cae
treee9d8ca3d7429bfa48e823a6f105d76254e34daf0
parentbb86a7338b7a864c03e1736a8d370b10254b0300
mmap: don't return ENOMEM when mapcount is temporarily exceeded in munmap()

On ia64, the following test program exit abnormally, because glibc thread
library called abort().

 ========================================================
 (gdb) bt
 #0  0xa000000000010620 in __kernel_syscall_via_break ()
 #1  0x20000000003208e0 in raise () from /lib/libc.so.6.1
 #2  0x2000000000324090 in abort () from /lib/libc.so.6.1
 #3  0x200000000027c3e0 in __deallocate_stack () from /lib/libpthread.so.0
 #4  0x200000000027f7c0 in start_thread () from /lib/libpthread.so.0
 #5  0x200000000047ef60 in __clone2 () from /lib/libc.so.6.1
 ========================================================

The fact is, glibc call munmap() when thread exitng time for freeing
stack, and it assume munlock() never fail.  However, munmap() often make
vma splitting and it with many mapcount make -ENOMEM.

Oh well, that's crazy, because stack unmapping never increase mapcount.
The maxcount exceeding is only temporary.  internal temporary exceeding
shouldn't make ENOMEM.

This patch does it.

 test_max_mapcount.c
 ==================================================================
  #include<stdio.h>
  #include<stdlib.h>
  #include<string.h>
  #include<pthread.h>
  #include<errno.h>
  #include<unistd.h>

  #define THREAD_NUM 30000
  #define MAL_SIZE (8*1024*1024)

 void *wait_thread(void *args)
 {
  void *addr;

  addr = malloc(MAL_SIZE);
  sleep(10);

  return NULL;
 }

 void *wait_thread2(void *args)
 {
  sleep(60);

  return NULL;
 }

 int main(int argc, char *argv[])
 {
  int i;
  pthread_t thread[THREAD_NUM], th;
  int ret, count = 0;
  pthread_attr_t attr;

  ret = pthread_attr_init(&attr);
  if(ret) {
  perror("pthread_attr_init");
  }

  ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  if(ret) {
  perror("pthread_attr_setdetachstate");
  }

  for (i = 0; i < THREAD_NUM; i++) {
  ret = pthread_create(&th, &attr, wait_thread, NULL);
  if(ret) {
  fprintf(stderr, "[%d] ", count);
  perror("pthread_create");
  } else {
  printf("[%d] create OK.\n", count);
  }
  count++;

  ret = pthread_create(&thread[i], &attr, wait_thread2, NULL);
  if(ret) {
  fprintf(stderr, "[%d] ", count);
  perror("pthread_create");
  } else {
  printf("[%d] create OK.\n", count);
  }
  count++;
  }

  sleep(3600);
  return 0;
 }
 ==================================================================

[akpm@linux-foundation.org: coding-style fixes]
Signed-off-by: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
Signed-off-by: Hugh Dickins <hugh.dickins@tiscali.co.uk>
Cc: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
mm/mmap.c