Software Tuning, Performance Optimization & Platform Monitoring
Discussion regarding monitoring and software tuning methodologies, Performance Monitoring Unit (PMU) of Intel microprocessors, and platform updating.

mmap doesn't return page size aligned address when mmap 1st param is zero

Karen_M_
Beginner
3,386 Views

Hi,

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:

  • Argument length is less that 1
  • Argument start is not a multiple of the page size.
  • Argument start or start + 1 is outside the processes address space.

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,

Many Thanks

Karen


 


 

 

0 Kudos
2 Replies
Karen_M_
Beginner
3,386 Views

 

Hi,

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!

http://man7.org/linux/man-pages/man2/mmap.2.html

Many Thanks

Karen

0 Kudos
CyrIng
Novice
3,386 Views

Hello,

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 remap_pfn_range()

Example in function IntelFreq_mmap

 

0 Kudos
Reply