当一个新区域被加到进程的地址空间时,内核会检查它是否可以与一个或多个现存区域合并,vma_merge()函数在可能的情况下,将一个新区域与周边区域进行合并。参数:
mm:新区域所属的进程地址空间
prev:在地址上紧接着新区域的前面一个vma
addr:新区域的起始地址
end:新区域的结束地址
vm_flags:新区域的标识集
anon_vma:新区域所属的匿名映射
file:新区域映射的文件
pgoff:新区域映射文件的偏移
policy:和NUMA相关
- struct vm_area_struct *vma_merge(struct mm_struct *mm,
- struct vm_area_struct *prev, unsigned long addr,
- unsigned long end, unsigned long vm_flags,
- struct anon_vma *anon_vma, struct file *file,
- pgoff_t pgoff, struct mempolicy *policy)
- {
- pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
- struct vm_area_struct *area, *next;
- /*
- * We later require that vma->vm_flags == vm_flags,
- * so this tests vma->vm_flags & VM_SPECIAL, too.
- */
- if (vm_flags & VM_SPECIAL)
- return NULL;
- if (prev)//指定了先驱vma,则获取先驱vma的后驱vma
- next = prev->vm_next;
- else //否则指定mm的vma链表中的第一个元素为后驱vma
- next = mm->mmap;
- area = next;
- /*后驱节点存在,并且后驱vma的结束地址和给定区域的结束地址相同,
- 也就是说两者有重叠,那么调整后驱vma*/
- if (next && next->vm_end == end) /* cases 6, 7, 8 */
- next = next->vm_next;
- /*
- * 先判断给定的区域能否和前驱vma进行合并,需要判断如下的几个方面:
- 1.前驱vma必须存在
- 2.前驱vma的结束地址正好等于给定区域的起始地址
- 3.两者的struct mempolicy中的相关属性要相同,这项检查只对NUMA架构有意义
- 4.其他相关项必须匹配,包括两者的vm_flags,是否映射同一个文件等等
- */
- if (prev && prev->vm_end == addr &&
- mpol_equal(vma_policy(prev), policy) &&
- can_vma_merge_after(prev, vm_flags,
- anon_vma, file, pgoff)) {
- /*
- *确定可以和前驱vma合并后再判断是否能和后驱vma合并,判断方式和前面一样,
- 不过这里多了一项检查,在给定区域能和前驱、后驱vma合并的情况下还要检查
- 前驱、后驱vma的匿名映射可以合并
- */
- if (next && end == next->vm_start &&
- mpol_equal(policy, vma_policy(next)) &&
- can_vma_merge_before(next, vm_flags,
- anon_vma, file, pgoff+pglen) &&
- is_mergeable_anon_vma(prev->anon_vma,
- next->anon_vma)) {
- /* cases 1, 6 */
- vma_adjust(prev, prev->vm_start,
- next->vm_end, prev->vm_pgoff, NULL);
- } else /* cases 2, 5, 7 */
- vma_adjust(prev, prev->vm_start,
- end, prev->vm_pgoff, NULL);
- return prev;
- }
- /*
- * Can this new request be merged in front of next?
- */
- /*如果前面的步骤失败,那么则从后驱vma开始进行和上面类似的步骤*/
- if (next && end == next->vm_start &&
- mpol_equal(policy, vma_policy(next)) &&
- can_vma_merge_before(next, vm_flags,
- anon_vma, file, pgoff+pglen)) {
- if (prev && addr < prev->vm_end) /* case 4 */
- vma_adjust(prev, prev->vm_start,
- addr, prev->vm_pgoff, NULL);
- else /* cases 3, 8 */
- vma_adjust(area, addr, next->vm_end,
- next->vm_pgoff - pglen, NULL);
- return area;
- }
- return NULL;
- }
vma_adjust会执行具体的合并调整操作
- void vma_adjust(struct vm_area_struct *vma, unsigned long start,
- unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
- {
- struct mm_struct *mm = vma->vm_mm;
- struct vm_area_struct *next = vma->vm_next;
- struct vm_area_struct *importer = NULL;
- struct address_space *mapping = NULL;
- struct prio_tree_root *root = NULL;
- struct file *file = vma->vm_file;
- struct anon_vma *anon_vma = NULL;
- long adjust_next = 0;
- int remove_next = 0;
- if (next && !insert) {
- /*指定的范围已经跨越了整个后驱vma,并且有可能超过后驱vma*/
- if (end >= next->vm_end) {
- /*
- * vma expands, overlapping all the next, and
- * perhaps the one after too (mprotect case 6).
- */
- again: remove_next = 1 + (end > next->vm_end);//确定是否超过了后驱vma
- end = next->vm_end;
- anon_vma = next->anon_vma;
- importer = vma;
- } else if (end > next->vm_start) {/*指定的区域和后驱vma部分重合*/
- /*
- * vma expands, overlapping part of the next:
- * mprotect case 5 shifting the boundary up.
- */
- adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
- anon_vma = next->anon_vma;
- importer = vma;
- } else if (end < vma->vm_end) {/*指定的区域没到达后驱vma的结束处*/
- /*
- * vma shrinks, and !insert tells it's not
- * split_vma inserting another: so it must be
- * mprotect case 4 shifting the boundary down.
- */
- adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT);
- anon_vma = next->anon_vma;
- importer = next;
- }
- }
- if (file) {//如果有映射文件
- mapping = file->f_mapping;//获取文件对应的address_space
- if (!(vma->vm_flags & VM_NONLINEAR))
- root = &mapping->i_mmap;
- spin_lock(&mapping->i_mmap_lock);
- if (importer &&
- vma->vm_truncate_count != next->vm_truncate_count) {
- /*
- * unmap_mapping_range might be in progress:
- * ensure that the expanding vma is rescanned.
- */
- importer->vm_truncate_count = 0;
- }
- /*如果指定了待插入的vma,则根据vma是否以非线性的方式映射文件来选择是将
- vma插入file对应的address_space的优先树(对应线性映射)还是双向链表(非线性映射)*/
- if (insert) {
- insert->vm_truncate_count = vma->vm_truncate_count;
- /*
- * Put into prio_tree now, so instantiated pages
- * are visible to arm/parisc __flush_dcache_page
- * throughout; but we cannot insert into address
- * space until vma start or end is updated.
- */
- __vma_link_file(insert);
- }
- }
- /*
- * When changing only vma->vm_end, we don't really need
- * anon_vma lock.
- */
- if (vma->anon_vma && (insert || importer || start != vma->vm_start))
- anon_vma = vma->anon_vma;
- if (anon_vma) {
- spin_lock(&anon_vma->lock);
- /*
- * Easily overlooked: when mprotect shifts the boundary,
- * make sure the expanding vma has anon_vma set if the
- * shrinking vma had, to cover any anon pages imported.
- */
- if (importer && !importer->anon_vma) {
- importer->anon_vma = anon_vma;
- __anon_vma_link(importer);//将importer插入importer的anon_vma匿名映射链表中
- }
- }
- if (root) {
- flush_dcache_mmap_lock(mapping);
- vma_prio_tree_remove(vma, root);
- if (adjust_next)
- vma_prio_tree_remove(next, root);
- }
- /*调整vma的相关量*/
- vma->vm_start = start;
- vma->vm_end = end;
- vma->vm_pgoff = pgoff;
- if (adjust_next) {//调整后驱vma的相关量
- next->vm_start += adjust_next << PAGE_SHIFT;
- next->vm_pgoff += adjust_next;
- }
- if (root) {
- if (adjust_next)//如果后驱vma被调整了,则重新插入到优先树中
- vma_prio_tree_insert(next, root);
- vma_prio_tree_insert(vma, root);//将vma插入到优先树中
- flush_dcache_mmap_unlock(mapping);
- }
- if (remove_next) {//给定区域与后驱vma有重合
- /*
- * vma_merge has merged next into vma, and needs
- * us to remove next before dropping the locks.
- */
- __vma_unlink(mm, next, vma);//将后驱vma从红黑树中删除
- if (file)//将后驱vma从文件对应的address space中删除
- __remove_shared_vm_struct(next, file, mapping);
- if (next->anon_vma)//将后驱vma从匿名映射链表中删除
- __anon_vma_merge(vma, next);
- } else if (insert) {
- /*
- * split_vma has split insert from vma, and needs
- * us to insert it before dropping the locks
- * (it may either follow vma or precede it).
- */
- __insert_vm_struct(mm, insert);//将待插入的vma插入mm的红黑树,双向链表以及
- //匿名映射链表
- }
- if (anon_vma)
- spin_unlock(&anon_vma->lock);
- if (mapping)
- spin_unlock(&mapping->i_mmap_lock);
- if (remove_next) {
- if (file) {
- fput(file);
- if (next->vm_flags & VM_EXECUTABLE)
- removed_exe_file_vma(mm);
- }
- mm->map_count--;
- mpol_put(vma_policy(next));
- kmem_cache_free(vm_area_cachep, next);
- /*
- * In mprotect's case 6 (see comments on vma_merge),
- * we must remove another next too. It would clutter
- * up the code too much to do both in one go.
- */
- if (remove_next == 2) {//还有待删除的区域
- next = vma->vm_next;
- goto again;
- }
- }
- validate_mm(mm);
- }
insert_vm_struct()函数用于插入一块新区域
- int insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
- {
- struct vm_area_struct * __vma, * prev;
- struct rb_node ** rb_link, * rb_parent;
- /*
- * The vm_pgoff of a purely anonymous vma should be irrelevant
- * until its first write fault, when page's anon_vma and index
- * are set. But now set the vm_pgoff it will almost certainly
- * end up with (unless mremap moves it elsewhere before that
- * first wfault), so /proc/pid/maps tells a consistent story.
- *
- * By setting it to reflect the virtual start address of the
- * vma, merges and splits can happen in a seamless way, just
- * using the existing file pgoff checks and manipulations.
- * Similarly in do_mmap_pgoff and in do_brk.
- */
- if (!vma->vm_file) {
- BUG_ON(vma->anon_vma);
- vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
- }
- /*__vma用来保存和vma->start对应的vma(与find_vma()一样),同时获取以下信息:
- 1.prev用来保存对应的前驱vma
- 2.rb_link保存该vma区域插入对应的红黑树节点
- 3.rb_parent保存该vma区域对应的父节点*/
- __vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent);
- if (__vma && __vma->vm_start < vma->vm_end)
- return -ENOMEM;
- if ((vma->vm_flags & VM_ACCOUNT) &&
- security_vm_enough_memory_mm(mm, vma_pages(vma)))
- return -ENOMEM;
- vma_link(mm, vma, prev, rb_link, rb_parent);//将vma关联到所有的数据结构中
- return 0;
- }
- static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
- struct vm_area_struct *prev, struct rb_node **rb_link,
- struct rb_node *rb_parent)
- {
- struct address_space *mapping = NULL;
- if (vma->vm_file)//如果存在文件映射则获取文件对应的地址空间
- mapping = vma->vm_file->f_mapping;
- if (mapping) {
- spin_lock(&mapping->i_mmap_lock);
- vma->vm_truncate_count = mapping->truncate_count;
- }
- anon_vma_lock(vma);
- /*将vma插入到相应的数据结构中--双向链表,红黑树和匿名映射链表*/
- __vma_link(mm, vma, prev, rb_link, rb_parent);
- __vma_link_file(vma);//将vma插入到文件地址空间的相应数据结构中
- anon_vma_unlock(vma);
- if (mapping)
- spin_unlock(&mapping->i_mmap_lock);
- mm->map_count++;
- validate_mm(mm);
- }
