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

Linux内核最新的连续内存分配器(CMA)——避免预留大块内存

[日期:2012-03-03] 来源:Linux社区  作者:宋宝华 [字体:大 中 小]

其中的函数migrate_pages()会完成页面的迁移,迁移过程中通过传入的__alloc_contig_migrate_alloc()申请新的page,并将老的page付给新的page:

  1. int migrate_pages(struct list_head *from,  
  2.                 new_page_t get_new_page, unsigned long private, bool offlining,  
  3.                 bool sync)  
  4. {  
  5.         int retry = 1;   
  6.         int nr_failed = 0;   
  7.         int pass = 0;   
  8.         struct page *page;  
  9.         struct page *page2;  
  10.         int swapwrite = current->flags & PF_SWAPWRITE;  
  11.         int rc;  
  12.   
  13.         if (!swapwrite)  
  14.                 current->flags |= PF_SWAPWRITE;  
  15.   
  16.         for(pass = 0; pass < 10 && retry; pass++) {  
  17.                 retry = 0;   
  18.   
  19.                 list_for_each_entry_safe(page, page2, from, lru) {  
  20.                         cond_resched();  
  21.   
  22.                         rc = unmap_and_move(get_new_page, private,  
  23.                                                 page, pass > 2, offlining,  
  24.                                                 sync);  
  25.   
  26.                         switch(rc) {  
  27.                         case -ENOMEM:  
  28.                                 goto out;   
  29.                         case -EAGAIN:  
  30.                                 retry++;  
  31.                                 break;  
  32.                         case 0:  
  33.                                 break;  
  34.                         default:  
  35.                                 /* Permanent failure */  
  36.                                 nr_failed++;  
  37.                                 break;  
  38.                         }      
  39.                 }      
  40.         }      
  41.         rc = 0;  
  42. ...  
  43. }   
其中的unmap_and_move()函数较为关键,它定义在mm/migrate.c中
  1. /* 
  2.  * Obtain the lock on page, remove all ptes and migrate the page 
  3.  * to the newly allocated page in newpage. 
  4.  */  
  5. static int unmap_and_move(new_page_t get_new_page, unsigned long private,  
  6.             struct page *page, int force, bool offlining, bool sync)  
  7. {  
  8.     int rc = 0;  
  9.     int *result = NULL;  
  10.     struct page *newpage = get_new_page(page, private, &result);  
  11.     int remap_swapcache = 1;  
  12.     int charge = 0;  
  13.     struct mem_cgroup *mem = NULL;  
  14.     struct anon_vma *anon_vma = NULL;  
  15.   
  16.     ...  
  17.   
  18.     /* charge against new page */  
  19.     charge = mem_cgroup_prepare_migration(page, newpage, &mem);  
  20.     ...  
  21.   
  22.     if (PageWriteback(page)) {  
  23.         if (!force || !sync)  
  24.             goto uncharge;  
  25.         wait_on_page_writeback(page);  
  26.     }  
  27.     /* 
  28.      * By try_to_unmap(), page->mapcount goes down to 0 here. In this case, 
  29.      * we cannot notice that anon_vma is freed while we migrates a page. 
  30.      * This get_anon_vma() delays freeing anon_vma pointer until the end 
  31.      * of migration. File cache pages are no problem because of page_lock() 
  32.      * File Caches may use write_page() or lock_page() in migration, then, 
  33.      * just care Anon page here. 
  34.      */  
  35.     if (PageAnon(page)) {  
  36.         /* 
  37.          * Only page_lock_anon_vma() understands the subtleties of 
  38.          * getting a hold on an anon_vma from outside one of its mms. 
  39.          */  
  40.         anon_vma = page_lock_anon_vma(page);  
  41.         if (anon_vma) {  
  42.             /* 
  43.              * Take a reference count on the anon_vma if the 
  44.              * page is mapped so that it is guaranteed to 
  45.              * exist when the page is remapped later 
  46.              */  
  47.             get_anon_vma(anon_vma);  
  48.             page_unlock_anon_vma(anon_vma);  
  49.         } else if (PageSwapCache(page)) {  
  50.             /* 
  51.              * We cannot be sure that the anon_vma of an unmapped 
  52.              * swapcache page is safe to use because we don't 
  53.              * know in advance if the VMA that this page belonged 
  54.              * to still exists. If the VMA and others sharing the 
  55.              * data have been freed, then the anon_vma could 
  56.              * already be invalid. 
  57.              * 
  58.              * To avoid this possibility, swapcache pages get 
  59.              * migrated but are not remapped when migration 
  60.              * completes 
  61.              */  
  62.             remap_swapcache = 0;  
  63.         } else {  
  64.             goto uncharge;  
  65.         }  
  66.     }  
  67.   
  68.     ...  
  69.     /* Establish migration ptes or remove ptes */  
  70.     try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);  
  71.   
  72. skip_unmap:  
  73.     if (!page_mapped(page))  
  74.         rc = move_to_new_page(newpage, page, remap_swapcache);  
  75.   
  76.     if (rc && remap_swapcache)  
  77.         remove_migration_ptes(page, page);  
  78.   
  79.     /* Drop an anon_vma reference if we took one */  
  80.     if (anon_vma)  
  81.         drop_anon_vma(anon_vma);  
  82.   
  83. uncharge:  
  84.     if (!charge)  
  85.         mem_cgroup_end_migration(mem, page, newpage, rc == 0);  
  86. unlock:  
  87.     unlock_page(page);  
  88.   
  89. move_newpage:  
  90.     ...  
  91. }  
通过unmap_and_move(),老的page就被迁移过去新的page。

接下来要回收page,回收page的作用是,不至于因为拿了连续的内存后,系统变得内存饥饿:

->

  1. /* 
  2.  * Reclaim enough pages to make sure that contiguous allocation 
  3.  * will not starve the system. 
  4.  */  
  5. __reclaim_pages(zone, GFP_HIGHUSER_MOVABLE, end-start);  
->
  1. /* 
  2.  * Trigger memory pressure bump to reclaim some pages in order to be able to 
  3.  * allocate 'count' pages in single page units. Does similar work as 
  4.  *__alloc_pages_slowpath() function. 
  5.  */  
  6. static int __reclaim_pages(struct zone *zone, gfp_t gfp_mask, int count)  
  7. {  
  8.         enum zone_type high_zoneidx = gfp_zone(gfp_mask);  
  9.         struct zonelist *zonelist = node_zonelist(0, gfp_mask);  
  10.         int did_some_progress = 0;  
  11.         int order = 1;  
  12.         unsigned long watermark;  
  13.   
  14.         /* 
  15.          * Increase level of watermarks to force kswapd do his job 
  16.          * to stabilise at new watermark level. 
  17.          */  
  18.         __update_cma_watermarks(zone, count);  
  19.   
  20.         /* Obey watermarks as if the page was being allocated */  
  21.         watermark = low_wmark_pages(zone) + count;  
  22.         while (!zone_watermark_ok(zone, 0, watermark, 0, 0)) {  
  23.                 wake_all_kswapd(order, zonelist, high_zoneidx, zone_idx(zone));  
  24.   
  25.                 did_some_progress = __perform_reclaim(gfp_mask, order, zonelist,  
  26.                                                       NULL);  
  27.                 if (!did_some_progress) {  
  28.                         /* Exhausted what can be done so it's blamo time */  
  29.                         out_of_memory(zonelist, gfp_mask, order, NULL);  
  30.                 }  
  31.         }  
  32.   
  33.         /* Restore original watermark levels. */  
  34.         __update_cma_watermarks(zone, -count);  
  35.   
  36.         return count;  
  37. }  
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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