第六章扩展——VMA
由 user process角度來(lái)說(shuō)明的話,VMA 是 user process 里一段 virtual address space 區(qū)域;virtual address space 是連續(xù)的內(nèi)存空間,當(dāng)然VMA 也會(huì)是連續(xù)的空間。VMA?對(duì) Linux 的主要好處是,可以內(nèi)存的使用更有效率,並且更容易管理 user process address space。
從另一個(gè)觀念來(lái)看,VMA 可以讓 Linux kernel 以 process 的角度來(lái)管理 virtual address space。Process 的 VMA對(duì)應(yīng),可以由 /proc/<pid>/maps 來(lái)查看;例如 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 數(shù)據(jù)結(jié)構(gòu)來(lái)記錄每一「區(qū)域」的 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 域,用來(lái)來(lái)保存 VMA 數(shù)據(jù)信息:
˙ unsigned long vm_start:記錄此 VMA 區(qū)域的開始位址(start address)。
˙ unsigned long vm_end:記錄此 VMA區(qū)域的結(jié)束位址(end address)。
˙ struct vm_area_struct *vm_next:指向下一個(gè) VMA 區(qū)域結(jié)構(gòu)的指針(Linux 以 linked list 數(shù)據(jù)結(jié)構(gòu)維護(hù)每一個(gè) VMA 區(qū)域)。
VMA 的實(shí)際作用主要是為了能更有效率地管理內(nèi)存,并且是給予 paging 系統(tǒng)之上所發(fā)展出的;VMA 是比原始 paging 更高級(jí)的內(nèi)存管理方法。
?
圖:Process與 VMA 關(guān)系
Memory Descriptor
Linux 的「Process Descriptor」數(shù)據(jù)結(jié)構(gòu) 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」數(shù)據(jù)結(jié)構(gòu),以下是 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 顧名思義,是用來(lái)描述 process 內(nèi)存信息的數(shù)據(jù)結(jié)構(gòu)。由 struct mm_struct 里可以看到一個(gè)名為 mmap 的 field,mmap 的 data type?為 struct vm_area_struct,這個(gè)數(shù)據(jù)結(jié)構(gòu)即是我們?cè)凇窵inux 的 Virtual Memory Areas(VMA):基本概念結(jié)介紹」所介紹的 VMA 數(shù)據(jù)結(jié)構(gòu)。
VMA?與 ELF Image 的對(duì)應(yīng)關(guān)系
在「Linux 的 Virtual Memory Areas(VMA):基本概念介紹」曾經(jīng)介紹過,Process 的 VMA 對(duì)應(yīng),可以由 /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
列表結(jié)果便能用來(lái)說(shuō)明 VMA?與 ELF image 之間的關(guān)系。搭配上圖來(lái)說(shuō)明列表結(jié)果的 VMA對(duì)應(yīng)關(guān)系,如下:
1. 第 1 列(row)是 ELF 可執(zhí)行文件(/sbin/init)的 code section VMA mapping;
2. 第 2 列是 ELF?可執(zhí)行文件 data section VMA mapping;
3. 第 3 列是 ELF?可執(zhí)行文件 .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 來(lái)表示所有可執(zhí)行的區(qū)塊,以 data section 來(lái)表示包含結(jié)構(gòu)的區(qū)塊。
在整個(gè) VMA 的討論程中,我們只針對(duì) code section?與 data section 做討論(如圖)。
?
源文檔 <http://blog.csdn.net/pmpmp2006/article/details/4735722>
總結(jié)
以上是生活随笔為你收集整理的第六章扩展——VMA的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用VMware Infrastruct
- 下一篇: Android上实现柱状图表