Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why file starting offset in mmap() must be multiple of the page size

In mmap() manpage:

Its prototype is:

void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);

and description:

The mmap() function asks to map 'length' bytes starting at offset 'offset' 
from the file (or other object) specified by the file descriptor fd into 
memory, preferably at address 'start'.

Sepcifically, for the last argument:

'offset' should be a multiple of the page size as returned by getpagesize(2).

From what I practised, offset MUST be a multiple of the page size, for example, 4096 on my Linux, otherwise, mmap() would return Invalid argument, offset is for file offset, why it must be multiple of virtual memory system's page size?

Thanks,

like image 915
password636 Avatar asked Nov 20 '13 10:11

password636


People also ask

What is offset mmap?

The offset argument specifies the first byte of the file to be mapped. The offset must be an exact multiple of the system page size (4096 bytes). Note: See the IBM UNIX System Services Assembler Callable Services manual for additional information about the behavior of mmap and the conditions under which it can be used.

What is mmap () used for?

The mmap() function can be used to map a region of memory that is larger than the current size of the object. Memory access within the mapping but beyond the current end of the underlying objects may result in SIGBUS signals being sent to the process.

Does mmap load file into memory?

Yes, mmap creates a mapping. It does not normally read the entire content of whatever you have mapped into memory. If you wish to do that you can use the mlock/mlockall system call to force the kernel to read into RAM the content of the mapping, if applicable.

Can you mmap twice?

You can and should map the entire file unless it is larger than the available address space. On a 64-bit computer, that's rather unlikely. On 32-bits, it's not impossible. In any case, you can make as many mappings as you want of each single file.


1 Answers

The simple answer: to make it fast. The more complex answer: whenever you access the memory at a location within the mapped memory, the OS must make sure that this location is filled with the contents of the file. But the OS can only detect whether you access a memory page - not a single location. What it does is, it creates a simple relation between offsets in the file and memory pages - and whenever you access a memory page, that part of the file is loaded. To make these calculations fast, it restricts you to start at certain offsets.

like image 163
Tobias Langner Avatar answered Sep 29 '22 11:09

Tobias Langner