Analyze mmap: what is it, why and how to use it

mmap basic concepts

mmap is a method of memory mapping files, that is, a file or other object is mapped to the address space of the process to realize the one-to-one mapping relationship between the disk address of the file and a virtual address in the virtual address space of the process. After implementing such a mapping relationship, the process can use pointers to read and write operations on this memory, and the system will automatically write back dirty pages to the corresponding file disk, that is, the file operation is completed without calling read, write Wait for the system to call the function. On the contrary, the modification of this area in the kernel space also directly reflects the user space, so that file sharing between different processes can be realized. As shown in the figure below:
Insert picture description here
As can be seen from the figure above, the virtual address space of a process consists of multiple virtual memory areas. The virtual memory area is a homogeneous range in the virtual address space of a process, that is, a continuous address range with the same characteristics. The text data segment (code segment), initial data segment, BSS data segment, heap, stack, and memory map shown in the above figure are all an independent virtual memory area. The address space for memory mapping is in the spare part between the stacks.

The Linux kernel uses the vm_area_struct structure to represent an independent virtual memory area. Since each virtual memory area of ​​different qualities has different functions and internal mechanisms, a process uses multiple vm_area_struct structures to represent different types of virtual memory areas. Each vm_area_struct structure is linked by a linked list or tree structure to facilitate rapid process access, as shown in the following figure: The
Insert picture description here
vm_area_struct structure contains the start and end addresses of the area and other related information, and also contains a vm_ops pointer, which can lead to all of this The system call function that the area can use. In this way, the information needed by the process for any operation of a certain virtual memory area can be obtained from vm_area_struct. The mmap function is to create a new vm_area_struct structure and connect it to the physical disk address of the file. Please see the next section for specific steps.

mmap memory mapping principle

The implementation process of mmap memory mapping can be divided into three stages in general:

(1) The process starts the mapping process and creates a virtual mapping area for the mapping in the virtual address space

1. The process calls the library function mmap in user space, prototype: void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);

2. In the virtual address space of the current process, look for a free continuous virtual address that meets the requirements

3. Allocate a vm_area_struct structure for this virtual area, and then initialize each field of this structure

4. Insert the newly created virtual area structure (vm_area_struct) into the virtual address area linked list or tree of the process

(2) Call the kernel space system call function mmap (different from the user space function) to realize the one-to-one mapping relationship between the physical address of the file and the virtual address of the process

5. After allocating a new virtual address area for the mapping, find the corresponding file descriptor in the file descriptor table through the file pointer to be mapped, and link to the file in the kernel "open file set" through the file descriptor The file structure (struct file), each file structure maintains various information related to the opened file.

6. Through the file structure of the file, link to the file_operations module and call the kernel function mmap. Its prototype is: int mmap(struct file *filp, struct vm_area_struct *vma), which is different from user space library functions.

7. The kernel mmap function locates the physical address of the file disk through the inode module of the virtual file system.

8. The page table is established through the remap_pfn_range function, which realizes the mapping relationship between the file address and the virtual address area. At this time, this virtual address does not have any data associated with the main memory.

(3) The process initiates an access to this mapping space, causing a page fault exception, and realizing the copy of the file content to the physical memory (main memory)

Note: The first two stages are only to create a virtual interval and complete the address mapping, but do not copy any file data to the main memory. The real file read is when the process initiates a read or write operation.

9. The read or write operation of the process accesses the mapped address of the virtual address space. By querying the page table, it is found that this address is not on the physical page. Because only the address mapping has been established at present, the real hard disk data has not been copied to the memory, so a page fault exception is caused.

10. After a series of judgments are made for abnormal page faults, the kernel initiates a request paging process after confirming that there is no illegal operation.

11. The paging process first searches for the memory page that needs to be accessed in the swap cache. If there is no page, the nopage function is called to load the missing page from the disk into the main memory.

12. After that, the process can read or write this piece of main memory. If the write operation changes its content, the system will automatically write back dirty pages to the corresponding disk address after a certain period of time, that is, the completion of writing to the file process.

Note: The modified dirty page will not be updated back to the file immediately, but there will be a period of delay. You can call msync() to force the synchronization, so that the written content can be saved to the file immediately.

The difference between mmap and regular file operations

The calling process of the function:

1. The process initiates a file read request.

2. The kernel finds the inode of the file by looking up the process file symbol table and locates the file information on the file set that the kernel has opened.

3. The inode searches the address_space to find whether the requested file page has been cached in the page cache. If it exists, it will directly return to the content of this file page.

4. If it does not exist, locate the file disk address through the inode, and copy the data from the disk to the page cache. After that, the page reading process is initiated again, and the data in the page cache is sent to the user process.

In summary, conventional file operations use a page cache mechanism in order to improve read and write efficiency and protect disks. This causes the file page to be copied from the disk to the page cache when reading the file. Since the page cache is in the kernel space and cannot be directly addressed by the user process, it is necessary to copy the data page in the page cache to the corresponding user in the memory again. In space. In this way, after two data copy processes, the process of acquiring the file content can be completed. The same is true for write operations. The buffer to be written cannot be directly accessed in the kernel space. It must first be copied to the main memory corresponding to the kernel space and then written back to the disk (delayed write back), which also requires two data copies.

When using mmap to manipulate files, there is no file copy operation in the two steps of creating a new virtual memory area and establishing the file disk address and virtual memory area mapping. When accessing data later, it is found that there is no data in the memory and initiated the abnormal page fault process. Through the established mapping relationship, only one data copy is used, and the data is transferred from the disk to the user space of the memory for the process use.

==In short, regular file operations require two copies of data from disk to page cache and then to user main memory. While mmap manipulates files, only one data copy process from the disk to the user's main memory is required. == To put it bluntly, the key point of mmap is to realize the direct interaction of data between user space and kernel space and save the tedious process of different data in different spaces. Therefore mmap is more efficient.

Summary of mmap advantages

From the above discussion, we can see that mmap has the following advantages:

1. The file read operation has crossed the page cache, reducing the number of data copies, replacing I/O reading and writing with memory reading and writing, and improving the efficiency of file reading.

2. Realize the efficient interaction between user space and kernel space. The respective modification operations of the two spaces can be directly reflected in the mapped area, so that they can be captured in time by the other space.

3. Provide a way to share memory and communicate with each other between processes. Whether it is a parent-child process or an unrelated process, it can map its user space to the same file or anonymously to the same area. In this way, the purpose of inter-process communication and inter-process sharing can be achieved through respective changes to the mapping area.
At the same time, if process A and process B both map area C, when A reads C for the first time, the file page is copied from the disk to the memory through a page fault; but when B reads the same page of C, it will also produce The page fault is abnormal, but it is no longer necessary to copy the file from the disk, and the file data that has been saved in the memory can be used directly.

4. It can be used to realize efficient large-scale data transmission. Insufficient memory space is an aspect that restricts the operation of big data. The solution is often to use hard disk space to assist the operation to supplement the lack of memory. But further it will cause a large number of file I/O operations, which greatly affects efficiency. This problem can be solved by mmap mapping. In other words, mmap can play its role whenever you need to use disk space instead of memory.

Guess you like

Origin blog.csdn.net/qq_32727095/article/details/113848377