Linux内核分析之进程地址空间


本文主要介绍linux内核中进程地址空间的数据结构描述,包括mm_struct/vm_area_struct。进程线性地址区间的分配流程,并对相应的源代码做了注释。 

内核中的函数以相当直接了当的方式获得动态内存。当给用户态进程分配内存时,情况完全不同了。进程对动态内存的请求被认为是不紧迫的,一般来说,内核总是尽量推迟给用户态进程分配内存。由于用户进程时不可信任的,因此,内核必须能随时准备捕获用户态进程引起的所有寻址错误。当用户态进程请求动态内存时,并没有获得请求的页框,而仅仅获得对一个新的线性地址区间的使用权,而这一线性地址区间就成为进程地址空间的一部分。

进程地址空间由允许进程使用的全部线性地址组成。内核可以通过增加或删除某些线程地址区间来动态地修改进程的地址空间。内核通过所谓线性去得资源来标示线性地址区间,线性区是由起始线性地址、长度和一些访问权限来描述的。进程获得新线性区的一些典型情况:

1.但用户在控制台输入一条命令时,shell进程创建一个新的进程去执行这个命令。结果是,一个全新的地址空间(也就是一组线性区)分配给新进程。

2.正在运行的进程有可能决定装入一个完全不同的程序。这时,进程描述符不变,可是在装入这个程序以前所有的线性区却被释放,并有一组新的线性区被分配给这个进程。

3.正在运行的进程可能对一个文件执行内存映像。

4.进程可能持续向他的用户态堆栈增加数据,知道映像这个堆栈的线性区用完为止,此时,内核也许会决定扩展这个线性区的大小。

5.进程可能创建一个IPC共享线性区来与其他合作进程共享数据。此时,内核给这个进程分配一个新的线性区以实现这个方案。

6.进程可能通过调用类似malloc这样的函数扩展自己的动态堆。结果是,内核可能决定扩展给这个堆所分配的线性区。

数据结构描述

进程描述符task_struct中的mm字段描述了进程地址空间

  1. struct mm_struct {  
  2.     struct vm_area_struct * mmap;       /* list of VMAs */  
  3.     struct rb_root mm_rb;  
  4.     struct vm_area_struct * mmap_cache; /* last find_vma result */  
  5.     unsigned long (*get_unmapped_area) (struct file *filp,  
  6.                 unsigned long addr, unsigned long len,  
  7.                 unsigned long pgoff, unsigned long flags);  
  8.     void (*unmap_area) (struct mm_struct *mm, unsigned long addr);  
  9.     unsigned long mmap_base;        /* base of mmap area */  
  10.     unsigned long task_size;        /* size of task vm space */  
  11.     unsigned long cached_hole_size;     /* if non-zero, the largest hole below free_area_cache */  
  12.     unsigned long free_area_cache;      /* first hole of size cached_hole_size or larger */  
  13.     pgd_t * pgd;  
  14.     atomic_t mm_users;          /* How many users with user space? */  
  15.     atomic_t mm_count;          /* How many references to "struct mm_struct" (users count as 1) */  
  16.     int map_count;              /* number of VMAs */  
  17.     struct rw_semaphore mmap_sem;  
  18.     spinlock_t page_table_lock;     /* Protects page tables and some counters */  
  19.   
  20.     struct list_head mmlist;        /* List of maybe swapped mm's.  These are globally strung 
  21.                          * together off init_mm.mmlist, and are protected 
  22.                          * by mmlist_lock 
  23.                          */  
  24.   
  25.     /* Special counters, in some configurations protected by the 
  26.      * page_table_lock, in other configurations by being atomic. 
  27.      */  
  28.     mm_counter_t _file_rss;  
  29.     mm_counter_t _anon_rss;  
  30.   
  31.     unsigned long hiwater_rss;  /* High-watermark of RSS usage */  
  32.     unsigned long hiwater_vm;   /* High-water virtual memory usage */  
  33.   
  34.     unsigned long total_vm, locked_vm, shared_vm, exec_vm;  
  35.     unsigned long stack_vm, reserved_vm, def_flags, nr_ptes;  
  36.     unsigned long start_code, end_code, start_data, end_data;  
  37.     unsigned long start_brk, brk, start_stack;  
  38.     unsigned long arg_start, arg_end, env_start, env_end;  
  39.   
  40.     unsigned long saved_auxv[AT_VECTOR_SIZE]; /* for /proc/PID/auxv */  
  41.   
  42.     struct linux_binfmt *binfmt;  
  43.   
  44.     cpumask_t cpu_vm_mask;/*用于懒惰TLB交换的位掩码*/  
  45.   
  46.     /* Architecture-specific MM context */  
  47.     mm_context_t context;  
  48.   
  49.     /* Swap token stuff */  
  50.     /* 
  51.      * Last value of global fault stamp as seen by this process. 
  52.      * In other words, this value gives an indication of how long 
  53.      * it has been since this task got the token. 
  54.      * Look at mm/thrash.c 
  55.      */  
  56.     unsigned int faultstamp;  
  57.     unsigned int token_priority;  
  58.     unsigned int last_interval;  
  59.   
  60.     unsigned long flags; /* Must use atomic bitops to access the bits */  
  61.   
  62.     struct core_state *core_state; /* coredumping support */  
  63. #ifdef CONFIG_AIO   
  64.     spinlock_t      ioctx_lock;  
  65.     struct hlist_head   ioctx_list;/*一步IO上下文链表*/  
  66. #endif   
  67. #ifdef CONFIG_MM_OWNER   
  68.     /* 
  69.      * "owner" points to a task that is regarded as the canonical 
  70.      * user/owner of this mm. All of the following must be true in 
  71.      * order for it to be changed: 
  72.      * 
  73.      * current == mm->owner 
  74.      * current->mm != mm 
  75.      * new_owner->mm == mm 
  76.      * new_owner->alloc_lock is held 
  77.      */  
  78.     struct task_struct *owner;  
  79. #endif   
  80.   
  81. #ifdef CONFIG_PROC_FS   
  82.     /* store ref to file /proc/<pid>/exe symlink points to */  
  83.     struct file *exe_file;  
  84.     unsigned long num_exe_file_vmas;  
  85. #endif   
  86. #ifdef CONFIG_MMU_NOTIFIER   
  87.     struct mmu_notifier_mm *mmu_notifier_mm;  
  88. #endif   
  89. };  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 下一页

相关内容