Hope this is the most appropriate forum for my question....
munmap, in anonymous mapping mode, fails with invalid argument (22). The possible causes documented for this (ref: Interprocess communications in linux) are listed as:
As start and length arguments look OK (correspond to earlier successful mmap operation) I believe it is failing because 'start' isn't a multiple of page size (using huge page sizes). HugePageSize 2048KB, vm.nr_hugepages=117187 (to use 240GB of 250GB available).
When mmap is called 'addr' is specified as 0. Documentation says "...the kernel takes '0' as a hint about where to place the mapping; on linux, the mapping will be created at a nearby page boundary."
void * data = mmap( 0, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS |MAP_HUGETLB | MAP_POPULATE, 0, 0 );
But when munmap fails, the returned memory address from the earlier mmap call doesn't appear to be aligned on page boundary. That is, when I divide data's address by the value returned from sysconf(_SC_PAGE_SIZE) modulus is non-zero.
At other times when it is divisble I note that munmap is successful.
Machine, Centos 7, has 256GB ram, 96 cores though running on one core for this. icpc (ICC) 14.0.0 20130728
Any ideas as to why the returned address isn't aligned, or indeed if this is the likely cause of munmap failing would be much appreciated,
Just to report I resolved this in case of use to anyone in future. My test for alignment was faulty (divided by 2048 rather than 1<<21 for page size). So address parameter was in fact aligned. However, I came across more comprehensive documentation on mmap/munmap (from man7) which indicated that the length parameter also has to be aligned. When I aligned length to page size map and munmap worked!
True, the best way to handle mmap is to have "everything" aligned to the page size.
For instance, in a device driver, the other side of mmap, memory allocation must be a unit of a page to succeed with ()
Example in function IntelFreq_mmap