進程地址空間也就是每一個進程所使用的內存,內核對進程地址空間的管理,也就是對用戶態程序的內存管理。node
主要內容:linux
地址空間就是每一個進程所能訪問的內存地址範圍。數組
這個地址範圍不是真實的,是虛擬地址的範圍,有時甚至會超過實際物理內存的大小。緩存
現代的操做系統中進程都是在保護模式下運行的,地址空間實際上是操做系統給進程用的一段連續的虛擬內存空間。安全
地址空間最終會經過頁表映射到物理內存上,由於內核操做的是物理內存。數據結構
雖然地址空間的範圍很大,可是進程也不必定有權限訪問所有的地址空間(通常都是隻能訪問地址空間中的一些地址區間),app
進程可以訪問的那些地址區間也稱爲 內存區域。ide
進程若是訪問了有效內存區域之外的內容就會報 「段錯誤」 信息。函數
內存區域中主要包含如下信息:this
注:bss是 block started by symbol 的縮寫。
linux中內存相關的概念稍微整理了一下,供參考:
英文 |
含義 |
SIZE | 進程映射的內存大小,這不是進程實際使用的內存大小 |
RSS(Resident set size) | 實際駐留在「內存」中的內存大小,不包含已經交換出去的內存 |
SHARE | RSS中與其餘進程共享的內存大小 |
VMSIZE | 進程佔用的總地址空間,包含沒有映射到內存中的頁 |
Private RSS | 僅由進程單獨佔用的RSS,也就是進程實際佔用的內存 |
linux中的地址空間是用 mm_struct 來表示的。
下面對其中一些關鍵的屬性進行了註釋,有些屬性我也不是很瞭解......
struct mm_struct { struct vm_area_struct * mmap; /* [內存區域]鏈表 */ struct rb_root mm_rb; /* [內存區域]紅黑樹 */ struct vm_area_struct * mmap_cache; /* 最近一次訪問的[內存區域] */ 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); /* 取消地址 addr 的映射 */ unsigned long mmap_base; /* 地址空間中能夠用來映射的首地址 */ unsigned long task_size; /* 進程的虛擬地址空間大小 */ unsigned long cached_hole_size; /* 若是不空的話,就是 free_area_cache 後最大的空洞 */ unsigned long free_area_cache; /* 地址空間的第一個空洞 */ pgd_t * pgd; /* 頁全局目錄 */ atomic_t mm_users; /* 使用地址空間的用戶數 */ atomic_t mm_count; /* 實際使用地址空間的計數, (users count as 1) */ int map_count; /* [內存區域]個數 */ struct rw_semaphore mmap_sem; /* 內存區域信號量 */ spinlock_t page_table_lock; /* 頁表鎖 */ struct list_head mmlist; /* 全部地址空間造成的鏈表 */ /* 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 */ struct linux_binfmt *binfmt; cpumask_t cpu_vm_mask; /* Architecture-specific MM context */ mm_context_t context; /* Swap token stuff */ /* * Last value of global fault stamp as seen by this process. * In other words, this value gives an indication of how long * it has been since this task got the token. * Look at mm/thrash.c */ unsigned int faultstamp; unsigned int token_priority; unsigned int last_interval; unsigned long flags; /* Must use atomic bitops to access the bits */ struct core_state *core_state; /* coredumping support */ #ifdef CONFIG_AIO spinlock_t ioctx_lock; struct hlist_head ioctx_list; #endif #ifdef CONFIG_MM_OWNER /* * "owner" points to a task that is regarded as the canonical * user/owner of this mm. All of the following must be true in * order for it to be changed: * * current == mm->owner * current->mm != mm * new_owner->mm == mm * new_owner->alloc_lock is held */ struct task_struct *owner; #endif #ifdef CONFIG_PROC_FS /* store ref to file /proc/<pid>/exe symlink points to */ struct file *exe_file; unsigned long num_exe_file_vmas; #endif #ifdef CONFIG_MMU_NOTIFIER struct mmu_notifier_mm *mmu_notifier_mm; #endif };
補充說明1: 上面的屬性中,mm_users 和 mm_count 很容易混淆,這裏特別說明一下:(下面的內容有網上查找的,也有我本身理解的)
mm_users 比較好理解,就是 mm_struct 被用戶空間進程(線程)引用的次數。
若是進程A中建立了3個新線程,那麼 進程A(這時候叫線程A也能夠)對應的 mm_struct 中的 mm_users = 4
補充一點,linux中進程和線程幾乎沒有什麼區別,就是看它是否共享進程地址空間,共享進程地址空間就是線程,反之就是進程。
因此,若是子進程和父進程共享了進程地址空間,那麼父子進程均可以看作線程。若是父子進程沒有共享進程地址空間,就是2個進程
mm_count 則稍微有點繞人,其實它記錄就是 mm_struct 實際的引用計數。
簡單點說,當 mm_users=0 時,並不必定能釋放此 mm_struct,只有當 mm_count=0 時,才能夠肯定釋放此 mm_struct
從上面的解釋能夠看出,可能引用 mm_struct 的並不僅是用戶空間的進程(線程)
當 mm_users>0 時, mm_count 會增長1, 表示有用戶空間進程(線程)在使用 mm_struct。無論使用 mm_struct 的用戶進程(線程)有幾個, mm_count 都只是增長1。
也就是說,若是隻有1個進程使用 mm_struct,那麼 mm_users=1,mm_count也是 1。
若是有9個線程在使用 mm_struct,那麼 mm_users=9,而 mm_count 仍然爲 1。
那麼 mm_count 什麼狀況下會大於 1呢?
當有內核線程使用 mm_struct 時,mm_count 纔會再增長 1。
內核線程爲什麼會使用用戶空間的 mm_struct 是有其餘緣由的,這個後面再闡述。這裏先知道內核線程使用 mm_struct 時也會致使 mm_count 增長 1。
在下面這種狀況下,mm_count 就頗有必要了:
補充說明2:爲什麼內核線程會使用用戶空間的 mm_struct?
對Linux來講,用戶進程和內核線程都是task_struct的實例,
惟一的區別是內核線程是沒有進程地址空間的(內核線程使用的內核地址空間),內核線程的mm描述符是NULL,即內核線程的tsk->mm域是空(NULL)。
內核調度程序在進程上下文的時候,會根據tsk->mm判斷即將調度的進程是用戶進程仍是內核線程。
可是雖然內核線程不用訪問用戶進程地址空間,可是仍然須要頁表來訪問內核本身的空間。
而任何用戶進程來講,他們的內核空間都是100%相同的,因此內核會借用上一個被調用的用戶進程的mm_struct中的頁表來訪問內核地址,這個mm_struct就記錄在active_mm。
簡而言之就是,對於內核線程,tsk->mm == NULL表示本身內核線程的身份,而tsk->active_mm是借用上一個用戶進程的mm_struct,用mm_struct的頁表來訪問內核空間。
對於用戶進程,tsk->mm == tsk->active_mm。
補充說明3:除了 mm_users 和 mm_count 以外,還有 mmap 和 mm_rb 須要說明如下:
其實 mmap 和 mm_rb 都是保存此 進程地址空間中全部的內存區域(VMA)的,前者是以鏈表形式存放,後者以紅黑樹形式存放。
用2種數據結構組織同一種數據是爲了便於對VMA進行高效的操做。
1. 分配進程地址空間
參考 kernel/fork.c 中的宏 allocate_mm
#define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL)) #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm)))
其實分配進程地址空間時,都是從slab高速緩存中分配的,能夠經過 /proc/slabinfo 查看 mm_struct 的高速緩存
# cat /proc/slabinfo | grep mm_struct mm_struct 35 45 1408 5 2 : tunables 24 12 8 : slabdata 9 9 0
2. 撤銷進程地址空間
參考 kernel/exit.c 中的 exit_mm() 函數
該函數會調用 mmput() 函數減小 mm_users 的值,
當 mm_users=0 時,調用 mmdropo() 函數, 減小 mm_count 的值,
若是 mm_count=0,那麼調用 free_mm 宏,將 mm_struct 還給 slab高速緩存
3. 查看進程佔用的內存:
cat /proc/<PID>/maps 或者 pmap PID
內存區域在linux中也被稱爲虛擬內存區域(VMA),它其實就是進程地址空間上一段連續的內存範圍。
VMA的定義也在 <linux/mm_types.h> 中
struct vm_area_struct { struct mm_struct * vm_mm; /* 相關的 mm_struct 結構體 */ unsigned long vm_start; /* 內存區域首地址 */ unsigned long vm_end; /* 內存區域尾地址 */ /* linked list of VM areas per task, sorted by address */ struct vm_area_struct *vm_next, *vm_prev; /* VMA鏈表 */ pgprot_t vm_page_prot; /* 訪問控制權限 */ unsigned long vm_flags; /* 標誌 */ struct rb_node vm_rb; /* 樹上的VMA節點 */ /* * For areas with an address space and backing store, * linkage into the address_space->i_mmap prio tree, or * linkage to the list of like vmas hanging off its node, or * linkage of vma in the address_space->i_mmap_nonlinear list. */ union { struct { struct list_head list; void *parent; /* aligns with prio_tree_node parent */ struct vm_area_struct *head; } vm_set; struct raw_prio_tree_node prio_tree_node; } shared; /* * A file's MAP_PRIVATE vma can be in both i_mmap tree and anon_vma * list, after a COW of one of the file pages. A MAP_SHARED vma * can only be in the i_mmap tree. An anonymous MAP_PRIVATE, stack * or brk vma (with NULL file) can only be in an anon_vma list. */ struct list_head anon_vma_node; /* Serialized by anon_vma->lock */ struct anon_vma *anon_vma; /* Serialized by page_table_lock */ /* Function pointers to deal with this struct. */ const struct vm_operations_struct *vm_ops; /* Information about our backing store: */ unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE units, *not* PAGE_CACHE_SIZE */ struct file * vm_file; /* File we map to (can be NULL). */ void * vm_private_data; /* was vm_pte (shared mem) */ unsigned long vm_truncate_count;/* truncate_count or restart_addr */ #ifndef CONFIG_MMU struct vm_region *vm_region; /* NOMMU mapping region */ #endif #ifdef CONFIG_NUMA struct mempolicy *vm_policy; /* NUMA policy for the VMA */ #endif };
這個結構體各個字段的英文註釋都比較詳細,就不一一翻譯了。
上述屬性中的 vm_flags 標識了此VM 對 VMA和頁面的影響:
vm_flags 的宏定義參見 <linux/mm.h>
標誌 |
對VMA及其頁面的影響 |
VM_READ | 頁面可讀取 |
VM_WRITE | 頁面可寫 |
VM_EXEC | 頁面可執行 |
VM_SHARED | 頁面可共享 |
VM_MAYREAD | VM_READ 標誌可被設置 |
VM_MAYWRITER | VM_WRITE 標誌可被設置 |
VM_MAYEXEC | VM_EXEC 標誌可被設置 |
VM_MAYSHARE | VM_SHARE 標誌可被設置 |
VM_GROWSDOWN | 區域可向下增加 |
VM_GROWSUP | 區域可向上增加 |
VM_SHM | 區域可用做共享內存 |
VM_DENYWRITE | 區域映射一個不可寫文件 |
VM_EXECUTABLE | 區域映射一個可執行文件 |
VM_LOCKED | 區域中的頁面被鎖定 |
VM_IO | 區域映射設備I/O空間 |
VM_SEQ_READ | 頁面可能會被連續訪問 |
VM_RAND_READ | 頁面可能會被隨機訪問 |
VM_DONTCOPY | 區域不能在 fork() 時被拷貝 |
VM_DONTEXPAND | 區域不能經過 mremap() 增長 |
VM_RESERVED | 區域不能被換出 |
VM_ACCOUNT | 該區域時一個記帳 VM 對象 |
VM_HUGETLB | 區域使用了 hugetlb 頁面 |
VM_NONLINEAR | 該區域是非線性映射的 |
vm_area_struct 結構體定義中有個 vm_ops 屬性,其中定義了內核操做 VMA 的方法
/* * 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 functions called when a no-page or a wp-page exception occurs. */ struct vm_operations_struct { void (*open)(struct vm_area_struct * area); /* 指定內存區域加入到一個地址空間時,該函數被調用 */ void (*close)(struct vm_area_struct * area); /* 指定內存區域從一個地址空間刪除時,該函數被調用 */ int (*fault)(struct vm_area_struct *vma, struct vm_fault *vmf); /* 當沒有出如今物理頁面中的內存被訪問時,該函數被調用 */ /* 當一個以前只讀的頁面變爲可寫時,該函數被調用, * 若是此函數出錯,將致使一個 SIGBUS 信號 */ int (*page_mkwrite)(struct vm_area_struct *vma, struct vm_fault *vmf); /* 當 get_user_pages() 調用失敗時, 該函數被 access_process_vm() 函數調用 */ int (*access)(struct vm_area_struct *vma, unsigned long addr, void *buf, int len, int write); #ifdef CONFIG_NUMA /* * set_policy() op must add a reference to any non-NULL @new mempolicy * to hold the policy upon return. Caller should pass NULL @new to * remove a policy and fall back to surrounding context--i.e. do not * install a MPOL_DEFAULT policy, nor the task or system default * mempolicy. */ int (*set_policy)(struct vm_area_struct *vma, struct mempolicy *new); /* * get_policy() op must add reference [mpol_get()] to any policy at * (vma,addr) marked as MPOL_SHARED. The shared policy infrastructure * in mm/mempolicy.c will do this automatically. * get_policy() must NOT add a ref if the policy at (vma,addr) is not * marked as MPOL_SHARED. vma policies are protected by the mmap_sem. * If no [shared/vma] mempolicy exists at the addr, get_policy() op * must return NULL--i.e., do not "fallback" to task or system default * policy. */ struct mempolicy *(*get_policy)(struct vm_area_struct *vma, unsigned long addr); int (*migrate)(struct vm_area_struct *vma, const nodemask_t *from, const nodemask_t *to, unsigned long flags); #endif };
除了以上的操做以外,還有一些輔助函數來方便內核操做內存區域。
這些輔助函數均可以在 <linux/mm.h> 中找到
1. 查找地址空間
/* Look up the first VMA which satisfies addr < vm_end, NULL if none. */ extern struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr); extern struct vm_area_struct * find_vma_prev(struct mm_struct * mm, unsigned long addr, struct vm_area_struct **pprev); /* Look up the first VMA which intersects the interval start_addr..end_addr-1, NULL if none. Assume start_addr < end_addr. */ static inline struct vm_area_struct * find_vma_intersection(struct mm_struct * mm, unsigned long start_addr, unsigned long end_addr) { struct vm_area_struct * vma = find_vma(mm,start_addr); if (vma && end_addr <= vma->vm_start) vma = NULL; return vma; }
2. 建立地址區間
static inline unsigned long do_mmap(struct file *file, unsigned long addr, unsigned long len, unsigned long prot, unsigned long flag, unsigned long offset) { unsigned long ret = -EINVAL; if ((offset + PAGE_ALIGN(len)) < offset) goto out; if (!(offset & ~PAGE_MASK)) ret = do_mmap_pgoff(file, addr, len, prot, flag, offset >> PAGE_SHIFT); out: return ret; }
3. 刪除地址區間
extern int do_munmap(struct mm_struct *, unsigned long, size_t);
地址空間中的地址都是虛擬內存中的地址,而CPU須要操做的是物理內存,因此須要一個將虛擬地址映射到物理地址的機制。
這個機制就是頁表,linux中使用3級頁面來完成虛擬地址到物理地址的轉換。
1. PGD - 全局頁目錄,包含一個 pgd_t 類型數組,多數體系結構中 pgd_t 類型就是一個無符號長整型
2. PMD - 中間頁目錄,它是個 pmd_t 類型數組
3. PTE - 簡稱頁表,包含一個 pte_t 類型的頁表項,該頁表項指向物理頁面
虛擬地址 - 頁表 - 物理地址的關係以下圖: