How to distribute Linux virtual address space

1 thought on “How to distribute Linux virtual address space”

  1. The virtual address space of a process is mainly described by two data. One is the highest level: MM_Struct, the other is a higher level: vm_area_structs. The highest -level MM_Struct structure describes the entire virtual address space of a process. Higher -level structure vm_area_truct describes a range of virtual address space (referred to as virtual zone).

    1. MM_Struct structure

    mm_strcut is used to describe the virtual address space of a process, describing in /include/linux/sched.h as follows:
    nstruct mm_Struct {

    struct vm_area_Struct*mmap;/*Direct to the virtual interval (VMA) linked list*/
    nrb_root_t mm_rb;/*point to the red_black tree*/
    nstruct vm_area_Struct* mmap_cache; /* points to the recently found virtual interval* /

    pgd_t* pgd; /* Page directory of the process* /
    natomic_t mm_users; How many users*/

    atomic_t mm_count;/*how many references to “struct mm_Struct” are referenced*/

    int map_count;/*Virtual interval*/

    struct rw_semaphore mmap_sem;

    SpinLock_t Page_table_Lock; /*Protective task page and mm-> RSS* /
    nstruct list_head mmlist; /*All Active (Active) mm链表*/rnrnunsigned long start_code, end_code, start_data, end_data;rnrnunsigned long start_brk, brk, start_stack;rnrnunsigned long arg_start, arg_end, env_start, env_end;

    unsigned long rss, topal_vm, locked_vm;

    unsigned long def_flags;

    unsigned long cpu_vm_mask;

    unsigned long swap_address; SPECIFIC MM Context */

    mm_Context_T Context;


    In the code, the variables pointing to this data structure are often MM.

    each process has only one mm_Struct structure. In the task_Struct structure of each process, there is a structure pointing to the process. It can be said that the MM_STRUCT structure is a description of the entire user space.

    The virtual space in a process of a process may (see the following description of vm_area_Struct). There are two types of organizations in these virtual intervals. From the MMAP pointer to this linked list, when the virtual interval is often used, the “Red_black
    Tree” structure is adopted, and MM_RB points to the tree. In the version before 2.4.10, the AVL tree is used because compared with the AVL tree, the operation of the red and black trees is more efficient.

    Because the address used in the program is often locality, the virtual interval used in the last time is likely to be used next time. In the high -speed cache, this virtual interval is directed by mmap_cache.

    The pointer PGT points to the page directory of the process (each process has its own page directory, pay attention to the difference between the kernel pages directory). When the scheduling program is scheduled to run a program, this address is transferred to this address. Incorporate a physical address and write the control register (CR3).

    It because the virtual space of the process and the virtual interval of its subordinates may be accessed in different contexts, and these access must be mutually exclusive, so in this structure Symptoms map_sem. In addition, Page_table_lock is set for similar purposes.

    . Although each process has only one virtual address space, this address space can be shared by other processes, such as the address space of the parent process sharing the parent process (that is, the MM_Struct structure). Therefore, count with mm_user and mm_count. Type Atomic_t is actually an integer, but the operation of this integer must be “atom”.

    It also describes the starting address and end address of the code segment, data segment, stack segment, parameter segment, and environment segment. The paragraph here is the logic of the program, which is different from the mechanism we described earlier.

    mm_context_t is a structure related to the platform, which is almost useless for i386.

    The further explanation of some domains in the analysis of the code.

    2. vm_area_Struct

    vm_area_struct description a virtual address interval in the process, in /include/linux/mm.h as follows:
    nstruct vm_area_struct

    struct mm_Struct* vm_mm; /* The address space where the virtual interval is located* /

    unsigned long vm_start; /* Start address in vm_mm* /
    UNSIGNED Long VM_END;/*The end address in vm_mm*/

    /*linked list of vm areas per task, sorted by address*/
    nstruct vm_area_Struct*vm_next; ; r n
    pgprot_t vm_page_prot; /* Exchange permissions to this virtual interval* /

    unsigned long vm_flags; /* virtual interval. ;


    * for areas with an address space and backing store,

    * One of the address_space-> i_mmap {, shared} lists,

    *for shm areas, the list of attaches, otherwise unused. n
    struct vm_area_ARUCT*vm_next_share;

    struct vm_area_struct ** vm_pprev_share;

    /*The function of the operation of this interval*/

    struct vm___ops;
    n/*About Our backing store: * /

    unsigned long vm_pgoff; / * offset (within vm_file) in page_siz E

    units, * not * page_cache_size */

    struct file * vm_file;/ * file we map to (can be null). */
    nunsigned long vm_rand; / * xxx: putfodahead info here. * /

    void * vm_private_data; / * WAS vm_Pte (Shared Mem) * /
    vm_flag is a sign to describe the operation of the virtual interval. document.

    vm_exec pages can be executed.

    vm_Page contains executable code.

    vm_growsdown this range can be extended to the low address.

    vm_growsup this range can be extended to high address.

    vm_io This interval maps the I/O address space of a device.

    vm_locked pages are locked and cannot be exchanged.

    vm_mayexec vm_exec logo can be set.

    vm_mayread vm_read logo can be set.

    vm_mayshare vm_share can be set.

    vm_maywrite vm_write can be set.

    vm_read pages are readable.

    VM_SHARED pages can be shared by multiple processes.

    vm_shm page is used for IPC sharing memory.
    vm_write pages are written.

    The higher -level structural vm_area_Structs is connected by a two -way linked list. They are arranged in the order of the false address. Each structure corresponds to a adjacent address space range. The reason why this is divided is because each virtual interval may have different sources, some may come from the executable image, some may come from the shared library, while others may be dynamicly allocated in memory areas. Therefore The processing operation described is different from the processing operation of its front and rear ranges. Therefore, Linux
    This to divide the virtual memory segmentation and use the virtual memory processing routine (VM_OPS) to abstract the processing method of virtual memory of different sources. The processing operations of different virtual intervals may be different. Linux uses object -oriented ideas here, that is, treat a virtual interval as an object, and use VM_AREA_STRUCTS to describe the attributes of this object. The VM_Operation structure describes the object on this object. Operation, its definition in /include/linux/mm.h:


    * These are the virtual mm functions -Opening of an area, closing and

    * unmapping it (needed to keep files on disk up-to-date etc), Pointer
    * to the function, .


    struct vm__struct {

    void (*open) (struct vm_area_Struct*Area);

    void (* Close) (Struct vm_area_Struct * Area);

    struct page * ( * not) (struct vm_area_Struct * Area, unsigned long address, int untens) N
    vm_ The structure contains function pointers; where of Open and Close for the opening and closing of the virtual interval, and the notage is used when the virtual pages are not caused by the “abnormal pages” caused by physical memory. The function that should be called.

    3. Red and black tree structure

    linux kernels starting from 2.4.10, and no AVL trees in the virtual zone tissue, but red and black trees, which is also for efficiency consideration, although AVL tree and red red red, red trees and red red reds, red trees and red red red, red The black tree is very similar, but in terms of insertion and deleting nodes, the performance of red and black trees is better. Below is briefly introduced to the red and black trees.
    A red and black tree is a binary tree with the following characteristics:
    E each node has color, or red, or black
    Red, then its sub -node must be black
    all paths from a node to the leaf nodes contain the same number of black nodes

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top