LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   mmap is failing. Is there a setting to correct this. (https://www.linuxquestions.org/questions/linux-newbie-8/mmap-is-failing-is-there-a-setting-to-correct-this-4175505761/)

ddsglq 05-22-2014 07:21 AM

mmap is failing. Is there a setting to correct this.
 
I have a new linux mint system with 32 GB of ram.

The following program fails with message
commit failed i=32758
However if I try to allocate 16 GB directly it succeeds. Is there a limit on the number of mmapped pages and where can I increase this limit.
I am using something like below to allocate a separate page for each pointer in my program followed by a non committed page so that I can track down a buffer overflow in my program. So I need to do separate commits and not allocate a single 16 GB block.

Code:

#include <stdio.h>
#include <sys/mman.h>

int main()
{
        char* pt = (char*)mmap( 0, 2*2*1024LU*1024LU*4096LU, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0 );
        for( int i=0;i<2*1024*1024;i++ ){
                if( mmap( pt, 10, PROT_READ|PROT_WRITE, MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS, 0, 0 )==(void*)-1 ){
                        printf( "commit failed i=%u\n", i );
                        return 0;
                }
                *pt = 1;
                pt += 4096*2;
        }
        return 0;
}


jpollard 05-22-2014 08:36 AM

I ran across this:

http://www.enchantedage.com/node/235

This indicates the limit is in /proc/sys/vm/max_map_count

On my system (I have only 8GB ram), the default value is 65530.

You should be able to change the limit permanently in sysctl.conf (parameter would be vm.max_map_count)

BTW, just a nit, but index "i" is declared to be an int, yet in the format you used it is treated as unsigned int.

ddsglq 05-22-2014 08:56 AM

It worked.
Thank you jpollard. You are such a great help.
Thanks for the nit also.


All times are GMT -5. The time now is 06:01 PM.