手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux内存管理之页面回收

[日期:2012-03-02] 来源:Linux社区  作者:bullbat [字体:大 中 小]
与匿名页面的双向链表相关的字段是 anon_vma_node 和 anon_vma。union shared 则与文件映射页面使用的优先级搜索树相关。字段 anon_vma 指向 anon_vma 表;字段 anon_vma_node 将映射该页面的所有虚拟内存区域链接起来;union shared 中的 prio_tree_node 结构用于表示优先级搜索树的一个节点;在某些情况下,比如不同的进程的内存区域可能映射到了同一个文件的相同部分,也就是说这些内存区域具有相同的(radix,size,heap)值,这个时候 Linux 就会在树上相应的节点(树上原来那个具有相同(radix,size,heap) 值的内存区域)上接一个双向链表用来存放这些内存区域,这个链表用 vm_set.list 来表示;树上那个节点指向的链表中的第一个节点是表头,用 vm_set.head 表示;vm_set.parent 用于表示是否是树结点。下边给出一个小图示简单说明一下 vm_set.list 和 vm_set.head。

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() 函数中,更新引用特定物理页面的所有页表项的操作都是在这个函数中实现的。

代码如下,对关键部分做了注释:

  1. static int try_to_unmap_anon(struct page *page, enum ttu_flags flags)  
  2. {  
  3.     struct anon_vma *anon_vma;  
  4.     struct vm_area_struct *vma;  
  5.     unsigned int mlocked = 0;  
  6.     int ret = SWAP_AGAIN;  
  7.     int unlock = TTU_ACTION(flags) == TTU_MUNLOCK;  
  8.   
  9.     if (MLOCK_PAGES && unlikely(unlock))  
  10.         ret = SWAP_SUCCESS; /* default for try_to_munlock() */  
  11.     /*如果该页面为匿名映射,返回该页面对应的匿名结构*/  
  12.     anon_vma = page_lock_anon_vma(page);  
  13.     if (!anon_vma)  
  14.         return ret;  
  15.     /*这里可以看出,vma的anon_vma_node字段链接到 
  16.     anon_vma的head字段*/  
  17.     /*扫描线性区描述符的anon_vma链表*/  
  18.     list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {  
  19.         if (MLOCK_PAGES && unlikely(unlock)) {  
  20.             if (!((vma->vm_flags & VM_LOCKED) &&  
  21.                   page_mapped_in_vma(page, vma)))  
  22.                 continue;  /* must visit all unlocked vmas */  
  23.             ret = SWAP_MLOCK;  /* saw at least one mlocked vma */  
  24.         } else {  
  25.             /*对anon_vma链表中的每一个vma线性区描述符 
  26.             调用该函数*/  
  27.             ret = try_to_unmap_one(page, vma, flags);  
  28.             if (ret == SWAP_FAIL || !page_mapped(page))  
  29.                 break;  
  30.         }  
  31.         if (ret == SWAP_MLOCK) {  
  32.             mlocked = try_to_mlock_page(page, vma);  
  33.             if (mlocked)  
  34.                 break;  /* stop if actually mlocked page */  
  35.         }  
  36.     }  
  37.   
  38.     page_unlock_anon_vma(anon_vma);  
  39.   
  40.     if (mlocked)  
  41.         ret = SWAP_MLOCK;   /* actually mlocked the page */  
  42.     else if (ret == SWAP_MLOCK)  
  43.         ret = SWAP_AGAIN;   /* saw VM_LOCKED vma */  
  44.   
  45.     return ret;  
  46. }  
 
  1. /* 
  2.  * Subfunctions of try_to_unmap: try_to_unmap_one called 
  3.  * repeatedly from either try_to_unmap_anon or try_to_unmap_file. 
  4.  */  
  5.  /** 
  6.     *page是一个指向目标页描述符的指针; 
  7.     *vma是指向线性区描述符的指针 
  8.     */  
  9. static int try_to_unmap_one(struct page *page, struct vm_area_struct *vma,  
  10.                 enum ttu_flags flags)  
  11. {  
  12.     struct mm_struct *mm = vma->vm_mm;  
  13.     unsigned long address;  
  14.     pte_t *pte;  
  15.     pte_t pteval;  
  16.     spinlock_t *ptl;  
  17.     int ret = SWAP_AGAIN;  
  18.     /*计算出待回收页的线性地址*/  
  19.     address = vma_address(page, vma);  
  20.     if (address == -EFAULT)  
  21.         goto out;  
  22.     /*获取线性地址对应的页表项地址*/  
  23.     pte = page_check_address(page, mm, address, &ptl, 0);  
  24.     if (!pte)  
  25.         goto out;  
  26.   
  27.     /* 
  28.      * If the page is mlock()d, we cannot swap it out. 
  29.      * If it's recently referenced (perhaps page_referenced 
  30.      * skipped over this mm) then we should reactivate it. 
  31.      */  
  32.      /*下面为判断是否可以被回收*/  
  33.     if (!(flags & TTU_IGNORE_MLOCK)) {  
  34.         if (vma->vm_flags & VM_LOCKED) {  
  35.             ret = SWAP_MLOCK;  
  36.             goto out_unmap;  
  37.         }  
  38.     }  
  39.     if (!(flags & TTU_IGNORE_ACCESS)) {  
  40.         if (ptep_clear_flush_young_notify(vma, address, pte)) {  
  41.             ret = SWAP_FAIL;  
  42.             goto out_unmap;  
  43.         }  
  44.     }  
  45.   
  46.     /* Nuke the page table entry. */  
  47.     flush_cache_page(vma, address, page_to_pfn(page));  
  48.     /*更新页表项并冲刷相应的TLB*/  
  49.     pteval = ptep_clear_flush_notify(vma, address, pte);  
  50.   
  51.     /* Move the dirty bit to the physical page now the pte is gone. */  
  52.     if (pte_dirty(pteval))/*如果是脏页面,置位PG_dirty*/  
  53.         set_page_dirty(page);  
  54.   
  55.     /* Update high watermark before we lower rss */  
  56.     /*更新mm的hiwater_rss*/  
  57.     update_hiwater_rss(mm);  
  58.   
  59.     if (PageHWPoison(page) && !(flags & TTU_IGNORE_HWPOISON)) {  
  60.         if (PageAnon(page))  
  61.             dec_mm_counter(mm, anon_rss);  
  62.         else  
  63.             dec_mm_counter(mm, file_rss);  
  64.         set_pte_at(mm, address, pte,  
  65.                 swp_entry_to_pte(make_hwpoison_entry(page)));  
  66.     } else if (PageAnon(page)) {/*如果是匿名页*/  
  67.         swp_entry_t entry = { .val = page_private(page) };  
  68.   
  69.         if (PageSwapCache(page)) {  
  70.             /* 
  71.              * Store the swap location in the pte. 
  72.              * See handle_pte_fault() ... 
  73.              */  
  74.              /*保存换出位置*/  
  75.             swap_duplicate(entry);  
  76.             if (list_empty(&mm->mmlist)) {  
  77.                 spin_lock(&mmlist_lock);  
  78.                 if (list_empty(&mm->mmlist))  
  79.                     /*添加到init_mm的相应链表,从这里可以 
  80.                     看出mm->mmlist为交换用的链表*/  
  81.                     list_add(&mm->mmlist, &init_mm.mmlist);  
  82.                 spin_unlock(&mmlist_lock);  
  83.             }  
  84.             dec_mm_counter(mm, anon_rss);  
  85.         } else if (PAGE_MIGRATION) {  
  86.             /* 
  87.              * Store the pfn of the page in a special migration 
  88.              * pte. do_swap_page() will wait until the migration 
  89.              * pte is removed and then restart fault handling. 
  90.              */  
  91.             BUG_ON(TTU_ACTION(flags) != TTU_MIGRATION);  
  92.             entry = make_migration_entry(page, pte_write(pteval));  
  93.         }  
  94.         set_pte_at(mm, address, pte, swp_entry_to_pte(entry));  
  95.         BUG_ON(pte_file(*pte));  
  96.     } else if (PAGE_MIGRATION && (TTU_ACTION(flags) == TTU_MIGRATION)) {  
  97.         /* Establish migration entry for a file page */  
  98.         swp_entry_t entry;  
  99.         entry = make_migration_entry(page, pte_write(pteval));  
  100.         set_pte_at(mm, address, pte, swp_entry_to_pte(entry));  
  101.     } else  
  102.         dec_mm_counter(mm, file_rss);  
  103.   
  104.     /*断开页表项和物理页面的关系*/  
  105.     page_remove_rmap(page);  
  106.     /*释放所分配的缓存*/  
  107.     page_cache_release(page);  
  108.   
  109. out_unmap:  
  110.     pte_unmap_unlock(pte, ptl);  
  111. out:  
  112.     return ret;  
  113. }  

        对于给定的物理页面来说,该函数会根据计算出来的线性地址找到对应的页表项地址,并更新页表项。对于匿名页面来说,换出的位置必须要被保存下来,以便于该页面下次被访问的时候可以被换进来。并非所有的页面都是可以被回收的,比如被 mlock() 函数设置过的内存页,或者最近刚被访问过的页面,等等,都是不可以被回收的。一旦遇上这样的页面,该函数会直接跳出执行并返回错误代码。如果涉及到页缓存中的数据,需要设置页缓存中的数据无效,必要的时候还要置位页面标识符以进行数据回写。该函数还会更新相应的一些页面使用计数器,比如前边提到的 _mapcount 字段,还会相应地更新进程拥有的物理页面数目等。

linux
相关资讯       Linux教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款