In the Linux kernelThe most basic management unit of virtual storage managementIt should be struct vm_area_struct, which describesSegment continuous virtual storage space with the same access attribute, the size of the virtual storage space is an integer multiple of the physical memory page.
The following is the definition of the struct vm_area_struct struct:
/*
* This struct defines a memory vmm memory area. There is color: black; Background-color: # a0ffff; "> vm_area_struct {
Struct mm_struct * vm_mm;/* VM area parameters */
Unsigned long vm_start;
Unsigned long vm_end;
/* Linked list of VM areas per task, sorted by address */
StructVm_area_struct* Vm_next;
Pgprot_t vm_page_prot;
Unsigned long vm_flags;
/* AVL Tree of VM areas per task, sorted by address */
Short vm_avl_height;
StructVm_area_struct* Vm_avl_left;
StructVm_area_struct* Vm_avl_right;
/* For areas with an address space and backing store,
* Font-size: 10px; ">Vm_area_struct* Vm_next_share;
StructVm_area_struct** Vm_pprev_share;
Struct vm_operations_struct * vm_ops;
Unsigned long vm_pgoff;/* offset in page_size units, * not * page_cache_size */
Struct file * vm_file;
Unsigned long vm_raend;
Void * vm_private_data;/* was vm_pte (shared MEm )*/
}; |
The virtual memory space described in the vm_area_struct structure is represented by vm_start and vm_end members. They respectively Save the first address and the first byte address after the last address, in bytes, therefore, the range of the VM space can be expressed by [vm_start, vm_end.
Generally,The virtual memory space used by the process is not consecutive, And the access attributes of each part of the virtual memory space may be different.. Therefore, the virtual storage space of a process needs to be described by multiple vm_area_struct structures. When the number of vm_area_struct structures is small, each vm_area_struct is sorted in ascending order and organizes data in the form of a single-chain table (points to the next vm_area_struct structure through the vm_next pointer ). However, when there is a large amount of data in the vm_area_struct structure, the chain table is still organized, which will inevitably affect the search speed. To solve this problem, vm_area_struct also adds vm_avl_hight (tree height), vm_avl_left (left sub-node), and vm_avl_right (right sub-node) to implement AVL Tree to improve the search speed of vm_area_struct.
If vm_area_struct describes a file-mapped virtual storage space, the vm_file Member points to the file structure of the mapped file. vm_pgoff is the file offset of the starting address of the virtual storage space in the vm_file file, the Unit is physical page.
One program can selectMap_shared or map_private Sharing ModeMap a part of the data of a file to its own virtual storage space. The difference between the two ing modes is that after map_shared ing, modifying the data in the virtual storage space in the memory will affect other processes that map the data in the same way, the modification will also be written back to the file, that is, these processes are actually sharing the data. After map_private ing, modifications to the data in the virtual storage space will not affect other processes or be written into files.
All vm_area_struct structures mapped to the same file from different processes are organized into two linked lists according to their sharing mode.The chain headers of the linked list are: vm_file-> f_dentry-> d_inode-> I _mapping-> I _mmap_shared, vm_file-> f_dentry-> d_inode-> I _mapping-> I _mmap. In the vm_area_struct structure, vm_next_share points to the next node in the linked list. vm_pprev_share is a pointer and its value is vm_next_share (I _mmap_shared or I _mmap) of the node (header node) structure in the linked list).
After the vm_area_struct structure is created, it indicates that the process can access the virtual storage space, but it may not allocate the corresponding physical page and establish PAGE ing.. In this case, if a command in the process execution needs to access the memory in the virtual memory space,Page missing exception. At this time, you need to read the file data corresponding to the address with a page exception through the vm_ops-> nopage function in the vm_area_struct structure.
Vm_flags mainly stores the access permissions of the process to the virtual storage space, and there are other attributes. Vm_page_prot is the default value of pgprot for the page table entry of the newly mapped physical page.