Re: mprotect() failed: Cannot allocate memory

Previous thread: perf top broken on ppc64 by Alexander Graf on Tuesday, April 20, 2010 - 4:21 pm. (5 messages)

Next thread: [PATCH] perf: Fix perf probe build error by Frederic Weisbecker on Tuesday, April 20, 2010 - 5:01 pm. (2 messages)
From: =?utf-8?q?Pawe=C5=82_Sikora?=
Date: Tuesday, April 20, 2010 - 4:05 pm

hi,

i'm trying to debug an ugly application with ElectricFence.
in fact, on x86-64 box with 8GB ram and 16GB swap i'm getting following error:

"ElectricFence Exiting: mprotect() failed: Cannot allocate memory"

the program has been compiled with gcc-4.5, glibc-2.11.1, kernel-2.6.32.
did you ever come across such (kernel/glibc) limitations?

here's a simple testcase which triggs -ENOMEM in mprotect().

#define N 100
#include<stdlib.h>

double **bm;
int main(){
    int i;
    long NN = 4*N*N;
    int kmax=100;

        bm = (double **)malloc((time_t)NN*sizeof(double *));
        for(i=0; i<NN; ++i){
                bm[i] = (double*)malloc((time_t)kmax*sizeof(double));
        }

        for(i=0; i<NN; ++i){
                free(bm[i]);
        }
        free(bm);
        return 0;
}

thanks for any hint,

BR,
Pawel.
--

From: Peter Zijlstra
Date: Wednesday, April 21, 2010 - 3:16 am

You probably depleted the max map count, see:
  /proc/sys/vm/max_map_count

We have a limit on the number of maps you can have, those mprotect()
calls split you maps like crazy, see also /proc/$pid/maps.

eg. change your second test program to include something like:

  char buf[128];
  snprintf(buf, sizeof(buf), "cat /proc/%d/maps", (int)getpid());
  system(buf);

at the end after lowering your NN count to fit, and observe the result
of those mprotect() calls.

--

From: Paweł Sikora
Date: Wednesday, April 21, 2010 - 2:42 am

Dnia 21-04-2010 o 13:16:50 Peter Zijlstra <peterz@infradead.org>  

yes, that is the clue :)

the limit in /proc/sys/vm/max_map_count was set to 65530.
with `echo 128000 > /proc/sys/vm/max_map_count` the testcase passes.

thanks for hint.
--

Previous thread: perf top broken on ppc64 by Alexander Graf on Tuesday, April 20, 2010 - 4:21 pm. (5 messages)

Next thread: [PATCH] perf: Fix perf probe build error by Frederic Weisbecker on Tuesday, April 20, 2010 - 5:01 pm. (2 messages)