其中的函数migrate_pages()会完成页面的迁移,迁移过程中通过传入的__alloc_contig_migrate_alloc()申请新的page,并将老的page付给新的page:
- int migrate_pages(struct list_head *from,
- new_page_t get_new_page, unsigned long private, bool offlining,
- bool sync)
- {
- int retry = 1;
- int nr_failed = 0;
- int pass = 0;
- struct page *page;
- struct page *page2;
- int swapwrite = current->flags & PF_SWAPWRITE;
- int rc;
- if (!swapwrite)
- current->flags |= PF_SWAPWRITE;
- for(pass = 0; pass < 10 && retry; pass++) {
- retry = 0;
- list_for_each_entry_safe(page, page2, from, lru) {
- cond_resched();
- rc = unmap_and_move(get_new_page, private,
- page, pass > 2, offlining,
- sync);
- switch(rc) {
- case -ENOMEM:
- goto out;
- case -EAGAIN:
- retry++;
- break;
- case 0:
- break;
- default:
- /* Permanent failure */
- nr_failed++;
- break;
- }
- }
- }
- rc = 0;
- ...
- }
- /*
- * Obtain the lock on page, remove all ptes and migrate the page
- * to the newly allocated page in newpage.
- */
- static int unmap_and_move(new_page_t get_new_page, unsigned long private,
- struct page *page, int force, bool offlining, bool sync)
- {
- int rc = 0;
- int *result = NULL;
- struct page *newpage = get_new_page(page, private, &result);
- int remap_swapcache = 1;
- int charge = 0;
- struct mem_cgroup *mem = NULL;
- struct anon_vma *anon_vma = NULL;
- ...
- /* charge against new page */
- charge = mem_cgroup_prepare_migration(page, newpage, &mem);
- ...
- if (PageWriteback(page)) {
- if (!force || !sync)
- goto uncharge;
- wait_on_page_writeback(page);
- }
- /*
- * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
- * we cannot notice that anon_vma is freed while we migrates a page.
- * This get_anon_vma() delays freeing anon_vma pointer until the end
- * of migration. File cache pages are no problem because of page_lock()
- * File Caches may use write_page() or lock_page() in migration, then,
- * just care Anon page here.
- */
- if (PageAnon(page)) {
- /*
- * Only page_lock_anon_vma() understands the subtleties of
- * getting a hold on an anon_vma from outside one of its mms.
- */
- anon_vma = page_lock_anon_vma(page);
- if (anon_vma) {
- /*
- * Take a reference count on the anon_vma if the
- * page is mapped so that it is guaranteed to
- * exist when the page is remapped later
- */
- get_anon_vma(anon_vma);
- page_unlock_anon_vma(anon_vma);
- } else if (PageSwapCache(page)) {
- /*
- * We cannot be sure that the anon_vma of an unmapped
- * swapcache page is safe to use because we don't
- * know in advance if the VMA that this page belonged
- * to still exists. If the VMA and others sharing the
- * data have been freed, then the anon_vma could
- * already be invalid.
- *
- * To avoid this possibility, swapcache pages get
- * migrated but are not remapped when migration
- * completes
- */
- remap_swapcache = 0;
- } else {
- goto uncharge;
- }
- }
- ...
- /* Establish migration ptes or remove ptes */
- try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
- skip_unmap:
- if (!page_mapped(page))
- rc = move_to_new_page(newpage, page, remap_swapcache);
- if (rc && remap_swapcache)
- remove_migration_ptes(page, page);
- /* Drop an anon_vma reference if we took one */
- if (anon_vma)
- drop_anon_vma(anon_vma);
- uncharge:
- if (!charge)
- mem_cgroup_end_migration(mem, page, newpage, rc == 0);
- unlock:
- unlock_page(page);
- move_newpage:
- ...
- }
接下来要回收page,回收page的作用是,不至于因为拿了连续的内存后,系统变得内存饥饿:
->
- /*
- * Reclaim enough pages to make sure that contiguous allocation
- * will not starve the system.
- */
- __reclaim_pages(zone, GFP_HIGHUSER_MOVABLE, end-start);
- /*
- * Trigger memory pressure bump to reclaim some pages in order to be able to
- * allocate 'count' pages in single page units. Does similar work as
- *__alloc_pages_slowpath() function.
- */
- static int __reclaim_pages(struct zone *zone, gfp_t gfp_mask, int count)
- {
- enum zone_type high_zoneidx = gfp_zone(gfp_mask);
- struct zonelist *zonelist = node_zonelist(0, gfp_mask);
- int did_some_progress = 0;
- int order = 1;
- unsigned long watermark;
- /*
- * Increase level of watermarks to force kswapd do his job
- * to stabilise at new watermark level.
- */
- __update_cma_watermarks(zone, count);
- /* Obey watermarks as if the page was being allocated */
- watermark = low_wmark_pages(zone) + count;
- while (!zone_watermark_ok(zone, 0, watermark, 0, 0)) {
- wake_all_kswapd(order, zonelist, high_zoneidx, zone_idx(zone));
- did_some_progress = __perform_reclaim(gfp_mask, order, zonelist,
- NULL);
- if (!did_some_progress) {
- /* Exhausted what can be done so it's blamo time */
- out_of_memory(zonelist, gfp_mask, order, NULL);
- }
- }
- /* Restore original watermark levels. */
- __update_cma_watermarks(zone, -count);
- return count;
- }
