zoukankan      html  css  js  c++  java
  • 第六章扩展——VMA

    由 user process角度来说明的话,VMA 是 user process 里一段 virtual address space 区域;virtual address space 是连续的内存空间,当然VMA 也会是连续的空间。VMA 对 Linux 的主要好处是,可以内存的使用更有效率,並且更容易管理 user process address space。

    从另一个观念来看,VMA 可以让 Linux kernel 以 process 的角度来管理 virtual address space。Process 的 VMA对应,可以由 /proc/<pid>/maps 来查看;例如 pid 1(init)的 VMA mapping为:

    $ cat /proc/1/maps

    08048000-0804e000 r-xp  00000000 08:01 12118 /sbin/init

    0804e000-08050000 rw-p 00005000 08:01 12118 /sbin/init

    08050000-08054000 rwxp 00000000 00:00 0

    40000000-40016000 r-xp  00000000 08:01 52297 /lib/ld-2.2.4.so

    40016000-40017000 rw-p 00015000 08:01 52297 /lib/ld-2.2.4.so

    40024000-40025000 rw-p 00000000 00:00 0

    40025000-40157000 r-xp  00000000 08:01 58241 /lib/i686/libc-2.2.4.so

    40157000-4015c000 rw-p 00131000 08:01 58241 /lib/i686/libc-2.2.4.so

    4015c000-40160000 rw-p 00000000 00:00 0

    bfffe000-c0000000   rwxp fffff000 00:00 0

    列表中的各列格式如下:

    start-end perm offset major:minor inode image

    Linux 以 struct vm_area_struct 数据结构来记录每一「区域」的 VMA 信息(include/linux/mm.h):

    struct vm_area_struct {

    struct mm_struct  * vm_mm;

    unsigned long vm_start;

    unsigned long vm_end;

    struct vm_area_struct *vm_next;

    pgprot_t vm_page_prot;

    unsigned long vm_flags;

    rb_node_t vm_rb;

    struct vm_area_struct *vm_next_share;

    struct vm_area_struct **vm_pprev_share;

    struct vm_operations_struct * vm_ops;

    unsigned long vm_pgoff;

    struct file * vm_file;

    unsigned long vm_raend;

    void * vm_private_data;

    };

    struct vm_area_struct 里有 3 域,用來來保存 VMA 数据信息:

    ˙ unsigned long vm_start:记录此 VMA 区域的开始位址(start address)。

    ˙ unsigned long vm_end:记录此 VMA区域的结束位址(end address)。

    ˙ struct vm_area_struct *vm_next:指向下一個 VMA 区域结构的指针(Linux 以 linked list 数据结构维护每一個 VMA 区域)。

    VMA 的实际作用主要是为了能更有效率地管理内存,并且是给予 paging 系統之上所发展出的;VMA 是比原始 paging 更高级的内存管理方法。

    图:Process与 VMA 关系

    Memory Descriptor

    Linux 的「Process Descriptor」数据结构 struct task_struct(include/linux/sched.h)。Process descriptor 里的 mm field 记录了 process 的 VMA 信息:

    struct task_struct {

    ...

    struct mm_struct *mm;

    ...

    }

    struct mm_struct 即是 Linux 提供的「Memory Descriptor」数据结构,以下是 struct mm_struct 的原型宣告:

    struct mm_struct {

    struct vm_area_struct * mmap;    /* list of VMAs */

    struct rb_root mm_rb;

    struct vm_area_struct * mmap_cache;    /* last find_vma result */

    unsigned long (*get_unmapped_area) (struct file *filp,

    unsigned long addr, unsigned long len,

    unsigned long pgoff, unsigned long flags);

    void (*unmap_area) (struct mm_struct *mm, unsigned long addr);

    unsigned long mmap_base;        /* base of mmap area */

    unsigned long task_size;        /* size of task vm space */

    unsigned long cached_hole_size; /* if non-zero, the largest hole below free_area_cache */

    unsigned long free_area_cache;    /* first hole of size cached_hole_size or larger */

    pgd_t * pgd;

    atomic_t mm_users;            /* How many users with user space? */

    atomic_t mm_count;            /* How many references to "struct mm_struct" (users count as 1) */

    int map_count;                /* number of VMAs */

    struct rw_semaphore mmap_sem;

    spinlock_t page_table_lock;        /* Protects page tables and some counters */

    struct list_head mmlist;        /* List of maybe swapped mm's. These are globally strung

    * together off init_mm.mmlist, and are protected

    * by mmlist_lock

    */

    /* Special counters, in some configurations protected by the

    * page_table_lock, in other configurations by being atomic.

    */

    mm_counter_t _file_rss;

    mm_counter_t _anon_rss;

    unsigned long hiwater_rss;    /* High-watermark of RSS usage */

    unsigned long hiwater_vm;    /* High-water virtual memory usage */

    unsigned long total_vm, locked_vm, shared_vm, exec_vm;

    unsigned long stack_vm, reserved_vm, def_flags, nr_ptes;

    unsigned long start_code, end_code, start_data, end_data;

    unsigned long start_brk, brk, start_stack;

    unsigned long arg_start, arg_end, env_start, env_end;

    unsigned long saved_auxv[AT_VECTOR_SIZE]; /* for /proc/PID/auxv */

    unsigned dumpable:2;

    cpumask_t cpu_vm_mask;

    /* Architecture-specific MM context */

    mm_context_t context;

    /* Token based thrashing protection. */

    unsigned long swap_token_time;

    char recent_pagein;

    /* coredumping support */

    int core_waiters;

    struct completion *core_startup_done, core_done;

    /* aio bits */

    rwlock_t        ioctx_list_lock;

    struct kioctx        *ioctx_list;

    };

    Memory descriptor 顾名思义,是用来描述 process 内存信息的数据结构。由 struct mm_struct 里可以看到一个名为 mmap 的 field,mmap 的 data type 为 struct vm_area_struct,这个数据结构即是我们在「Linux 的 Virtual Memory Areas(VMA):基本概念结介绍」所介绍的 VMA 数据结构。

    VMA 与 ELF Image 的对应关系

    在「Linux 的 Virtual Memory Areas(VMA):基本概念介绍」曾经介绍过,Process 的 VMA 对应,可以由 /proc/<pid>/maps 命令查询;例如 pid 1(init)的 VMA mapping 为:

    $ cat /proc/1/maps

    08048000-0804e000 r-xp 00000000 08:01 12118 /sbin/init

    0804e000-08050000 rw-p 00005000 08:01 12118 /sbin/init

    08050000-08054000 rwxp 00000000 00:00 0

    40000000-40016000 r-xp 00000000 08:01 52297 /lib/ld-2.2.4.so

    40016000-40017000 rw-p 00015000 08:01 52297 /lib/ld-2.2.4.so

    40024000-40025000 rw-p 00000000 00:00 0

    40025000-40157000 r-xp 00000000 08:01 58241 /lib/i686/libc-2.2.4.so

    40157000-4015c000 rw-p 00131000 08:01 58241 /lib/i686/libc-2.2.4.so

    4015c000-40160000 rw-p 00000000 00:00 0

    bfffe000-c0000000 rwxp fffff000 00:00 0

    列表结果便能用來说明 VMA 与 ELF image 之间的关系。搭配上图來說明列表结果的 VMA对应关系,如下:

    1. 第 1 列(row)是 ELF 可执行文件(/sbin/init)的 code section VMA mapping;

    2. 第 2 列是 ELF 可执行文件 data section VMA mapping;

    3. 第 3 列是 ELF 可执行文件 .bss section VMA mapping。

    4. 第 4 列是 dynamic loader(/lib/ld-2.2.4.so)的 code section VMA mapping;

    5. 第 5 列是 dynamic loader 的 data section VMA mapping;

    6. 第 6 列是 dynamic loader 的 .bss section VMA mapping。

    7. 第 7 列是 libc 的 code section VMA mapping;

    8. 第 8 列是 libc 的 data section VMA mapping;

    9. 第 9 列是 libc 的 .bss section VMA mapping。

    另外,要留意的是,在文中所指的 code section 與 data section 不见得就是 ELF 的 .text section 與 .data section;我们以 code section 来表示所有可执行的区块,以 data section 來表示包含结构的区块。

    在整個 VMA 的讨论程中,我們只针对 code section 与 data section 做讨论(如图)。

    源文档 <http://blog.csdn.net/pmpmp2006/article/details/4735722>

  • 相关阅读:
    SQL的join使用图解
    归并排序的JAVA实现
    java 快速排序 时间复杂度 空间复杂度 稳定性
    哈希表(HashMap)分析及实现(JAVA)
    外部排序
    海量数据面试题整理
    《CSS3秘籍》第6、7章
    《CSS3秘籍》第3-5章
    《CSS3秘籍》第1、2章
    《HTML5与CSS3基础教程》第11、14-16、18章
  • 原文地址:https://www.cnblogs.com/zhuyp1015/p/2486675.html
Copyright © 2011-2022 走看看