vm_set.list 和 vm_set.head


通过结构 vm_area_struct 中的 vm_mm 字段可以找到对应的 mm_struct 结构,在该结构中找到页全局目录,从而定位所有相关的页表项。
反向映射实现
在进行页面回收的时候,Linux的 shrink_page_list() 函数中调用 try_to_unmap() 函数去更新所有引用了回收页面的页表项。其代码流程如下所示:
实现函数 try_to_unmap() 的关键代码流程图

函数 try_to_unmap() 分别调用了两个函数 try_to_unmap_anon() 和 try_to_unmap_file(),其目的都是检查并确定都有哪些页表项引用了同一个物理页面,但是,由于匿名页面和文件映射页面分别采用了不同的数据结构,所以二者采用了不同的方法。
函数 try_to_unmap_anon() 用于匿名页面,该函数扫描相应的 anon_vma 表中包含的所有内存区域,并对这些内存区域分别调用 try_to_unmap_one() 函数。
函数 try_to_unmap_file() 用于文件映射页面,该函数会在优先级搜索树中进行搜索,并为每一个搜索到的内存区域调用 try_to_unmap_one() 函数。
两条代码路径最终汇合到 try_to_unmap_one() 函数中,更新引用特定物理页面的所有页表项的操作都是在这个函数中实现的。
代码如下,对关键部分做了注释:
- static int try_to_unmap_anon(struct page *page, enum ttu_flags flags)
- {
- struct anon_vma *anon_vma;
- struct vm_area_struct *vma;
- unsigned int mlocked = 0;
- int ret = SWAP_AGAIN;
- int unlock = TTU_ACTION(flags) == TTU_MUNLOCK;
- if (MLOCK_PAGES && unlikely(unlock))
- ret = SWAP_SUCCESS; /* default for try_to_munlock() */
- /*如果该页面为匿名映射,返回该页面对应的匿名结构*/
- anon_vma = page_lock_anon_vma(page);
- if (!anon_vma)
- return ret;
- /*这里可以看出,vma的anon_vma_node字段链接到
- anon_vma的head字段*/
- /*扫描线性区描述符的anon_vma链表*/
- list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
- if (MLOCK_PAGES && unlikely(unlock)) {
- if (!((vma->vm_flags & VM_LOCKED) &&
- page_mapped_in_vma(page, vma)))
- continue; /* must visit all unlocked vmas */
- ret = SWAP_MLOCK; /* saw at least one mlocked vma */
- } else {
- /*对anon_vma链表中的每一个vma线性区描述符
- 调用该函数*/
- ret = try_to_unmap_one(page, vma, flags);
- if (ret == SWAP_FAIL || !page_mapped(page))
- break;
- }
- if (ret == SWAP_MLOCK) {
- mlocked = try_to_mlock_page(page, vma);
- if (mlocked)
- break; /* stop if actually mlocked page */
- }
- }
- page_unlock_anon_vma(anon_vma);
- if (mlocked)
- ret = SWAP_MLOCK; /* actually mlocked the page */
- else if (ret == SWAP_MLOCK)
- ret = SWAP_AGAIN; /* saw VM_LOCKED vma */
- return ret;
- }
- /*
- * Subfunctions of try_to_unmap: try_to_unmap_one called
- * repeatedly from either try_to_unmap_anon or try_to_unmap_file.
- */
- /**
- *page是一个指向目标页描述符的指针;
- *vma是指向线性区描述符的指针
- */
- static int try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
- enum ttu_flags flags)
- {
- struct mm_struct *mm = vma->vm_mm;
- unsigned long address;
- pte_t *pte;
- pte_t pteval;
- spinlock_t *ptl;
- int ret = SWAP_AGAIN;
- /*计算出待回收页的线性地址*/
- address = vma_address(page, vma);
- if (address == -EFAULT)
- goto out;
- /*获取线性地址对应的页表项地址*/
- pte = page_check_address(page, mm, address, &ptl, 0);
- if (!pte)
- goto out;
- /*
- * If the page is mlock()d, we cannot swap it out.
- * If it's recently referenced (perhaps page_referenced
- * skipped over this mm) then we should reactivate it.
- */
- /*下面为判断是否可以被回收*/
- if (!(flags & TTU_IGNORE_MLOCK)) {
- if (vma->vm_flags & VM_LOCKED) {
- ret = SWAP_MLOCK;
- goto out_unmap;
- }
- }
- if (!(flags & TTU_IGNORE_ACCESS)) {
- if (ptep_clear_flush_young_notify(vma, address, pte)) {
- ret = SWAP_FAIL;
- goto out_unmap;
- }
- }
- /* Nuke the page table entry. */
- flush_cache_page(vma, address, page_to_pfn(page));
- /*更新页表项并冲刷相应的TLB*/
- pteval = ptep_clear_flush_notify(vma, address, pte);
- /* Move the dirty bit to the physical page now the pte is gone. */
- if (pte_dirty(pteval))/*如果是脏页面,置位PG_dirty*/
- set_page_dirty(page);
- /* Update high watermark before we lower rss */
- /*更新mm的hiwater_rss*/
- update_hiwater_rss(mm);
- if (PageHWPoison(page) && !(flags & TTU_IGNORE_HWPOISON)) {
- if (PageAnon(page))
- dec_mm_counter(mm, anon_rss);
- else
- dec_mm_counter(mm, file_rss);
- set_pte_at(mm, address, pte,
- swp_entry_to_pte(make_hwpoison_entry(page)));
- } else if (PageAnon(page)) {/*如果是匿名页*/
- swp_entry_t entry = { .val = page_private(page) };
- if (PageSwapCache(page)) {
- /*
- * Store the swap location in the pte.
- * See handle_pte_fault() ...
- */
- /*保存换出位置*/
- swap_duplicate(entry);
- if (list_empty(&mm->mmlist)) {
- spin_lock(&mmlist_lock);
- if (list_empty(&mm->mmlist))
- /*添加到init_mm的相应链表,从这里可以
- 看出mm->mmlist为交换用的链表*/
- list_add(&mm->mmlist, &init_mm.mmlist);
- spin_unlock(&mmlist_lock);
- }
- dec_mm_counter(mm, anon_rss);
- } else if (PAGE_MIGRATION) {
- /*
- * Store the pfn of the page in a special migration
- * pte. do_swap_page() will wait until the migration
- * pte is removed and then restart fault handling.
- */
- BUG_ON(TTU_ACTION(flags) != TTU_MIGRATION);
- entry = make_migration_entry(page, pte_write(pteval));
- }
- set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
- BUG_ON(pte_file(*pte));
- } else if (PAGE_MIGRATION && (TTU_ACTION(flags) == TTU_MIGRATION)) {
- /* Establish migration entry for a file page */
- swp_entry_t entry;
- entry = make_migration_entry(page, pte_write(pteval));
- set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
- } else
- dec_mm_counter(mm, file_rss);
- /*断开页表项和物理页面的关系*/
- page_remove_rmap(page);
- /*释放所分配的缓存*/
- page_cache_release(page);
- out_unmap:
- pte_unmap_unlock(pte, ptl);
- out:
- return ret;
- }
对于给定的物理页面来说,该函数会根据计算出来的线性地址找到对应的页表项地址,并更新页表项。对于匿名页面来说,换出的位置必须要被保存下来,以便于该页面下次被访问的时候可以被换进来。并非所有的页面都是可以被回收的,比如被 mlock() 函数设置过的内存页,或者最近刚被访问过的页面,等等,都是不可以被回收的。一旦遇上这样的页面,该函数会直接跳出执行并返回错误代码。如果涉及到页缓存中的数据,需要设置页缓存中的数据无效,必要的时候还要置位页面标识符以进行数据回写。该函数还会更新相应的一些页面使用计数器,比如前边提到的 _mapcount 字段,还会相应地更新进程拥有的物理页面数目等。
