进程地址空间[2]

    内存区域由vm_area_struct结构体描述,内存区域在内核中也被称作虚拟内存区域或VMA。vm_area_struct结构体描述了指定地址空间内连续区间上的一个独立内存范围。内核将每个内存区域作为一个单独的内存对象管理,每个内存区域都有一致的属性和操作:

 

  1. 在Mm.h中
  2. /*
  3.  * Linux kernel virtual memory manager primitives.
  4.  * The idea being to have a "virtual" mm in the same way
  5.  * we have a virtual fs - giving a cleaner interface to the
  6.  * mm details, and allowing different kinds of memory mappings
  7.  * (from shared memory to executable loading to arbitrary
  8.  * mmap() functions).
  9.  */
  10. /*
  11.  * This struct defines a memory VMM memory area. There is one of these
  12.  * per VM-area/task.  A VM area is any part of the process virtual memory
  13.  * space that has a special rule for the page-fault handlers (ie a shared
  14.  * library, the executable area etc).
  15.  */
  16. struct vm_area_struct {
  17.     struct mm_struct * vm_mm;   /* The address space we belong to. */
  18.     unsigned long vm_start;     /* Our start address within vm_mm. */
  19.     unsigned long vm_end;       /* The first byte after our end address
  20.                        within vm_mm. */
  21.     /* linked list of VM areas per task, sorted by address */
  22.     struct vm_area_struct *vm_next;
  23.     pgprot_t vm_page_prot;      /* Access permissions of this VMA. */
  24.     unsigned long vm_flags;     /* Flags, listed below. */
  25.     struct rb_node vm_rb;
  26.     /*
  27.      * For areas with an address space and backing store,
  28.      * linkage into the address_space->i_mmap prio tree, or
  29.      * linkage to the list of like vmas hanging off its node, or
  30.      * linkage of vma in the address_space->i_mmap_nonlinear list.
  31.      */
  32.     union {
  33.         struct {
  34.             struct list_head list;
  35.             void *parent;   /* aligns with prio_tree_node parent */
  36.             struct vm_area_struct *head;
  37.         } vm_set;
  38.         struct raw_prio_tree_node prio_tree_node;
  39.     } shared;
  40.     /*
  41.      * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma
  42.      * list, after a COW of one of the file pages.  A MAP_SHARED vma
  43.      * can only be in the i_mmap tree.  An anonymous MAP_PRIVATE, stack
  44.      * or brk vma (with NULL file) can only be in an anon_vma list.
  45.      */
  46.     struct list_head anon_vma_node; /* Serialized by anon_vma->lock */
  47.     struct anon_vma *anon_vma;  /* Serialized by page_table_lock */
  48.     /* Function pointers to deal with this struct. */
  49.     struct vm_operations_struct * vm_ops;
  50.     /* Information about our backing store: */
  51.     unsigned long vm_pgoff;     /* Offset (within vm_file) in PAGE_SIZE
  52.                        units, *not* PAGE_CACHE_SIZE */
  53.     struct file * vm_file;      /* File we map to (can be NULL). */
  54.     void * vm_private_data;     /* was vm_pte (shared mem) */
  55.     unsigned long vm_truncate_count;/* truncate_count or restart_addr */
  56. #ifndef CONFIG_MMU
  57.     atomic_t vm_usage;      /* refcount (VMAs shared if !MMU) */
  58. #endif
  59. #ifdef CONFIG_NUMA
  60.     struct mempolicy *vm_policy;    /* NUMA policy for the VMA */
  61. #endif
  62. };

  每个内存描述符都对应于进程地址空间中的惟一区间。内存区域的位置就在[vm_start,vm_end)之中。注意,在同一个地址空间内的不同内存区间不能重叠。

  mv_mm指向与VMA相关的mm_struct结构体。每个VMA对其相关的mm_struct结构体来说都是惟一的,所以即使两个独立的进程将同一个文件映射到各自的地址空间,它们都分别有一个vm_area_struct结构体来标志自己的内存区域;但是如果两个进程共享一个地址空间,那么它们也同时共享其中的所有vm_area_struct结构体。

VMA标志

   VMA标志是一种位标志,标志了内存区域所包含的页面的行为和信息。和物理页的访问权限不同,VMA标志反映了内核处理页面所需要遵循的行为准则,而不是硬件要求。该标志同时也包含了内存区域中页面的信息,或内存区域的整体信息:

  1. 在mm.h中
  2. /*
  3.  * vm_flags..
  4.  */
  5. #define VM_READ     0x00000001  /* currently active flags */
  6. #define VM_WRITE    0x00000002
  7. #define VM_EXEC     0x00000004
  8. #define VM_SHARED   0x00000008
  9. /* mprotect() hardcodes VM_MAYREAD >> 4 == VM_READ, and so for r/w/x bits. */
  10. #define VM_MAYREAD  0x00000010  /* limits for mprotect() etc */
  11. #define VM_MAYWRITE 0x00000020
  12. #define VM_MAYEXEC  0x00000040
  13. #define VM_MAYSHARE 0x00000080
  14. #define VM_GROWSDOWN    0x00000100  /* general info on the segment */
  15. #define VM_GROWSUP  0x00000200
  16. #define VM_PFNMAP   0x00000400  /* Page-ranges managed without "struct page", just pure PFN */
  17. #define VM_DENYWRITE    0x00000800  /* ETXTBSY on write attempts.. */
  18. #define VM_EXECUTABLE   0x00001000
  19. #define VM_LOCKED   0x00002000
  20. #define VM_IO           0x00004000  /* Memory mapped I/O or similar */
  21.                     /* Used by sys_madvise() */
  22. #define VM_SEQ_READ 0x00008000  /* App will access data sequentially */
  23. #define VM_RAND_READ    0x00010000  /* App will not benefit from clustered reads */
  24. #define VM_DONTCOPY 0x00020000      /* Do not copy this vma on fork */
  25. #define VM_DONTEXPAND   0x00040000  /* Cannot expand with mremap() */
  26. #define VM_RESERVED 0x00080000  /* Count as reserved_vm like IO */
  27. #define VM_ACCOUNT  0x00100000  /* Is a VM accounted object */
  28. #define VM_HUGETLB  0x00400000  /* Huge TLB Page VM */
  29. #define VM_NONLINEAR    0x00800000  /* Is non-linear (remap_file_pages) */
  30. #define VM_MAPPED_COPY  0x01000000  /* T if mapped copy of data (nommu mmap) */
  31. #define VM_INSERTPAGE   0x02000000  /* The vma has had "vm_insert_page()" done on it */
  32. #define VM_ALWAYSDUMP   0x04000000  /* Always include in core dumps */
  • VMA操作

   vm_area_struct结构体中的vm_ops域指向与指定内存域相关的操作函数表,内核使用表中的方法操作VMA。vm_area_struct为通用对象代表了任何类型的内存区域,而操作表描述针对特定的对象实例的特定方法。

 

  1. 在mm.h中
  2. /*
  3.  * These are the virtual MM functions - opening of an area, closing and
  4.  * unmapping it (needed to keep files on disk up-to-date etc), pointer
  5.  * to the functions called when a no-page or a wp-page exception occurs. 
  6.  */
  7. struct vm_operations_struct {
  8. /*当指定的内存区域被加入到一个地址空间时该函数被调用*/
  9.     void (*open)(struct vm_area_struct * area);
  10. /*当指定的内存区域从地址空间删除时,该函数被调用*/
  11.     void (*close)(struct vm_area_struct * area);
  12. /*当要访问的页不在物理内存中时,该函数被页错误处理程序调用*/
  13.     struct page * (*nopage)(struct vm_area_struct * area, unsigned long address, int *type);
  14.     unsigned long (*nopfn)(struct vm_area_struct * area, unsigned long address);
  15. /*该函数被系统调用remap_pages()调用来为将要发生的缺页中断预映射一个新映射*/
  16.     int (*populate)(struct vm_area_struct * area, unsigned long address, unsigned long len, pgprot_t prot, unsigned long pgoff, int nonblock);
  17.     /* notification that a previously read-only page is about to become
  18.      * writable, if an error is returned it will cause a SIGBUS */
  19.     int (*page_mkwrite)(struct vm_area_struct *vma, struct page *page);
  20. #ifdef CONFIG_NUMA
  21.     int (*set_policy)(struct vm_area_struct *vma, struct mempolicy *new);
  22.     struct mempolicy *(*get_policy)(struct vm_area_struct *vma,
  23.                     unsigned long addr);
  24.     int (*migrate)(struct vm_area_struct *vma, const nodemask_t *from,
  25.         const nodemask_t *to, unsigned long flags);
  26. #endif
  27. };
  • 内存区域的树形结构和内存区域的链表结构

    可以通过内存描述中的mmap和mm_rb域之一访问内存区域,这两个域各自独立地指向域内存描述符相关的全体内存区域对象。其实,它们包含完全相同的vm_area_struct结构体指针,仅仅是组织方法不同。

   mmap使用单独的链表连接所有的内存区域对象。每一个vm_area_struct结构体通过自身的vm_next联入链表,所有的区域按地址增长的方向排序,mmap域指向链表中第一个内存区域,链表中最后一个VMA结构体指针指向空。

   mm_rb使用红黑树连接所欲哦的内存区域对象。mm_rb指向红黑树的根节点,地址空间中的每一个vm_area_struct结构体通过自身的vm_rb连接到树中。

   红黑树中的所有节点都遵从:左边节点值小于右边节点值;每个节点都被配以红色或黑色。分配规则为:红色节点的子结点为黑色,并且数中的任何一条从节点到叶子的路径必须包含同样数目的黑色节点。根节点总为红色。红黑树的搜索、插入、删除等操作的复杂度都为O(log(n))。

   链表用于需要遍历全部节点的时候,红黑树适用于在地址空间中定位特定内存区域的时候。内核为了内存区域是上的各种不同操作都能获得高性能,所以使用了这两种数据结构。

  • 实际使用中的内存区域

   使用/proc文件系统和pmap(1)工具可以查看给定进程的内存空间和其中所含的内存区域。

  相关内容请参看

Linux 的 Virtual Memory Areas(VMA):基本概念介紹http://lixiang.cn/?q=node/105

 

   最后给出mm_struct和VMA的关系图:

a

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值