Mmap principle and process (Kernel 4.1 version)

Source: Internet
Author: User
background

Recently in the analysis of problems, encountered MMAP FB equipment failure problem, by the way to see the next 4.1 version of the kernel of the mmap related process, previously seen the old version, some forget, here right when records for follow-up reference. what Mmap is.

Believe that a brother who has done Linux development more or less used, or heard of mmap, but may not fully understand the role of mmap.

Mmap is literally a memory-mapped meaning, sounds more abstract, the use of a lot, but summed up, mainly the following two purposes: to map the contents of the file to the process user state of the virtual address space, so that the process can read and write the corresponding virtual address space content, and directly read and write the contents of the corresponding file. The biggest advantage of this mapping is that processes can access the data in the file directly from the user state without requiring a copy of the memory between the user state and the kernel state (under normal process, if you want to write data to the file, you need to copy the data from the user state to the kernel state, and then write the file from the kernel state), Equivalent to one less memory copy operation, which is one of the 0 copy techniques that people often say. Of course, the file here is not limited to ordinary files, UNIX environment, everything is a file, the file here may be a special file, such as equipment files, the corresponding mmap operation needs to be a separate drive to achieve. Allocates memory. When the incoming FD in mmap is empty, its function is to allocate memory, similar to malloc (in fact, malloc GLIBC implementation of the use of mmap to allocate memory), commonly known as "anonymous mapping", the meaning of anonymity is fd empty, the name is very abstract, The essence is not complex: in the process of virtual address space to allocate a section of virtual memory (with VMA), the physical presence of a fault in the distribution of anomalies, and modify the corresponding page table. Mmap Fundamentals

As mentioned earlier, there are two main uses for Mmap, the first of which is in two cases (ordinary files and special files), where the principles are described separately:

1. The basic principle of the ordinary document is: each file (mmap) defines the corresponding file operation data structure (file_operations), which defines the mmap operation, such as ext3 file system files corresponding to the file operation: Ext3_file_ Operations, the corresponding MMAP operation interface is: Generic_file_mmap. Generic_file_mmap is to create (or find the use of existing) VMA, and then set the appropriate members, including the corresponding processing hooks page faults, and finally return the corresponding virtual address. When the process access (write) the corresponding virtual address, the hardware will trigger a page fault exception (because the corresponding table entries have not yet been created), it will enter the error process (Do_page_fault), and then go into the previous set of pages of the exception hook, the hook will trigger the file system write operation, The data is eventually written to the file.

2. Special documents (for example, in the case of equipment files) The basic principle of mmap is similar to the above common file, the main difference is: the definition of the file operation is different, the corresponding interface is different, its implementation depends on the specific driver, process and ordinary file implementation can be completely different, not detailed here

3. The basic principle of anonymous mapping: Because there is no specific FD, there is no corresponding file, the anonymous mapping does not have the corresponding file operation, the process is more direct, mainly to create (or find the use of existing) VMA, and then set the appropriate members, return the corresponding virtual address. When the process accesses (writes) the corresponding virtual address, the hardware will trigger a page fault exception (because the corresponding pages table entry has not yet been created), it will enter the fault process (Do_page_fault), and then enter the process of anonymous mapping, mainly for the virtual address range to create the corresponding page table, in essence, is to allocate the corresponding physical memory. Code Flow mmap Process

Mmap has the corresponding system call interface, starting from the system call the approximate flow is as follows (the code is not very easy to find, need to look carefully):

Syscall_define6 (Mmap_pgoff, ...
  ) Sys_mmap_pgoff	
    syscall_define6 (Mmap_pgoff, ...
	  Vm_mmap_pgoff
	    do_mmap_pgoff
		  mmap_region
			file->f_op->mmap ()//(drive) a mmap hook of its own definition, such as the Ext3 file system          the EC corresponds to ' Generic_file_mmap '
File page missing exception process
Do_page_fault
  __do_page_fault
	handle_pte_fault
  	  __handle_mm_fault
		handle_pte_fault
		  do_ Fault
			do_shared_fault
			  __do_fault
				vma->vm_ops->fault ()  //File system or driver registration of the missing exception hook, If the Ext4 file corresponds to the Filemap_fault
anonymous Map page fault exception flow
Do_page_fault
  __do_page_fault
	handle_pte_fault
  	  __handle_mm_fault
		handle_pte_fault
		  do_ Anonymous_page
		    mk_pte//Create page table entries
			set_pte_at


Original address: http://happyseeker.github.io/kernel/2016/06/15/mmap-flow.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.