Ubuntu 10.04, error when using MAP_HUGETLB with MAP_SHARED

Below is the code that I use for mmaping file in ubuntu with huge pages, but this call fails with the error "invalid argument". However, when I passed the MAP_ANON flag without the file descriptor parameter in mmap, then it works. I can not understand the possible reason for this.

Secondly, I cannot understand why the mmaping file is resolved using MAP_PRIVATE, when this flag itself means that no changes will be written back to the file. Can this always be accomplished with MAP_ANON, or is there something I can't see?

Can someone help me with this?

int32_t main(int32_t argc, char** argv) { int32_t map_length = 16*1024*1024; // 16 MB , huge page size is 2 MB int32_t protection = PROT_READ | PROT_WRITE; int32_t flags = MAP_SHARED | MAP_HUGETLB; int32_t file__ = open("test",O_RDWR|O_CREAT | O_LARGEFILE,s_IRWXU | S_IRGRP | S_IROTH); if(file__ < 0 ) { std::cerr << "Unable to open file\n"; return -1; } if (ftruncate(file__, map_length) < 0) { std::cerr << "main :: unable to truncate the file\n" << "main :: " << strerror(errno) << "\n" << "main :: error number is " << errno << "\n"; return -1; } void *addr= mmap(NULL, map_length, protection, flags, file__, 0); if (addr == MAP_FAILED) { perror("mmap"); return -1; } const char* msg = "Hello World\n"; int32_t len = strlen(msg); memcpy(addr,msg,len); munmap(addr, map_length); close(file__); return 0; } 
+6
source share
2 answers

Both questions come down to the same point: with mmap () you can get two kinds of mappings: anonymous memory and files.

Anonymous memory (as indicated on the manual page) not supported by any file on the file system. Instead, the memory that you return from the MAP_ANON call in mmap () is regular system memory. The main user of this interface is the C library, which uses it to obtain backup storage for malloc / free. Thus, using MAP_ANON clearly indicates that you do not want to display the file.

Memory with file support . Mixes in a file (or its parts) to the application address space. In this case, the contents of the memory are actually supported by the contents of the file. Think of the MAP_PRIVATE flag as the first memory allocation for a file, and then copy the contents to this memory. In truth, this will not be what the kernel does, but just pretend.

HUGE_TLB is a function provided by the kernel for anonymous memory (see Documentation / vm / hugetlb-page.txt, as indicated on the mmap () page). This should be the reason your mmap () call fails when using HUGETLB for a file. * Edit: not quite right. There is a RAM file system (hugetlbfs) that supports huge pages. However, huge_tlb comparisons will not work on arbitrary files, as I understand the docs. *

For more information on how to use HUGE_TLB and the corresponding in-memory file system (hugetlbfs), you can consider the following articles in LWN:

+4
source

Adding MAP_PRIVATE to flags is fixed for me.

0
source

Source: https://habr.com/ru/post/891976/


All Articles