xref: /openbmc/linux/mm/migrate.c (revision e286781d5f2e9c846e012a39653a166e9d31777d)
1 /*
2  * Memory Migration functionality - linux/mm/migration.c
3  *
4  * Copyright (C) 2006 Silicon Graphics, Inc., Christoph Lameter
5  *
6  * Page migration was first developed in the context of the memory hotplug
7  * project. The main authors of the migration code are:
8  *
9  * IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
10  * Hirokazu Takahashi <taka@valinux.co.jp>
11  * Dave Hansen <haveblue@us.ibm.com>
12  * Christoph Lameter
13  */
14 
15 #include <linux/migrate.h>
16 #include <linux/module.h>
17 #include <linux/swap.h>
18 #include <linux/swapops.h>
19 #include <linux/pagemap.h>
20 #include <linux/buffer_head.h>
21 #include <linux/mm_inline.h>
22 #include <linux/nsproxy.h>
23 #include <linux/pagevec.h>
24 #include <linux/rmap.h>
25 #include <linux/topology.h>
26 #include <linux/cpu.h>
27 #include <linux/cpuset.h>
28 #include <linux/writeback.h>
29 #include <linux/mempolicy.h>
30 #include <linux/vmalloc.h>
31 #include <linux/security.h>
32 #include <linux/memcontrol.h>
33 #include <linux/syscalls.h>
34 
35 #include "internal.h"
36 
37 #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
38 
39 /*
40  * Isolate one page from the LRU lists. If successful put it onto
41  * the indicated list with elevated page count.
42  *
43  * Result:
44  *  -EBUSY: page not on LRU list
45  *  0: page removed from LRU list and added to the specified list.
46  */
47 int isolate_lru_page(struct page *page, struct list_head *pagelist)
48 {
49 	int ret = -EBUSY;
50 
51 	if (PageLRU(page)) {
52 		struct zone *zone = page_zone(page);
53 
54 		spin_lock_irq(&zone->lru_lock);
55 		if (PageLRU(page) && get_page_unless_zero(page)) {
56 			ret = 0;
57 			ClearPageLRU(page);
58 			if (PageActive(page))
59 				del_page_from_active_list(zone, page);
60 			else
61 				del_page_from_inactive_list(zone, page);
62 			list_add_tail(&page->lru, pagelist);
63 		}
64 		spin_unlock_irq(&zone->lru_lock);
65 	}
66 	return ret;
67 }
68 
69 /*
70  * migrate_prep() needs to be called before we start compiling a list of pages
71  * to be migrated using isolate_lru_page().
72  */
73 int migrate_prep(void)
74 {
75 	/*
76 	 * Clear the LRU lists so pages can be isolated.
77 	 * Note that pages may be moved off the LRU after we have
78 	 * drained them. Those pages will fail to migrate like other
79 	 * pages that may be busy.
80 	 */
81 	lru_add_drain_all();
82 
83 	return 0;
84 }
85 
86 static inline void move_to_lru(struct page *page)
87 {
88 	if (PageActive(page)) {
89 		/*
90 		 * lru_cache_add_active checks that
91 		 * the PG_active bit is off.
92 		 */
93 		ClearPageActive(page);
94 		lru_cache_add_active(page);
95 	} else {
96 		lru_cache_add(page);
97 	}
98 	put_page(page);
99 }
100 
101 /*
102  * Add isolated pages on the list back to the LRU.
103  *
104  * returns the number of pages put back.
105  */
106 int putback_lru_pages(struct list_head *l)
107 {
108 	struct page *page;
109 	struct page *page2;
110 	int count = 0;
111 
112 	list_for_each_entry_safe(page, page2, l, lru) {
113 		list_del(&page->lru);
114 		move_to_lru(page);
115 		count++;
116 	}
117 	return count;
118 }
119 
120 /*
121  * Restore a potential migration pte to a working pte entry
122  */
123 static void remove_migration_pte(struct vm_area_struct *vma,
124 		struct page *old, struct page *new)
125 {
126 	struct mm_struct *mm = vma->vm_mm;
127 	swp_entry_t entry;
128  	pgd_t *pgd;
129  	pud_t *pud;
130  	pmd_t *pmd;
131 	pte_t *ptep, pte;
132  	spinlock_t *ptl;
133 	unsigned long addr = page_address_in_vma(new, vma);
134 
135 	if (addr == -EFAULT)
136 		return;
137 
138  	pgd = pgd_offset(mm, addr);
139 	if (!pgd_present(*pgd))
140                 return;
141 
142 	pud = pud_offset(pgd, addr);
143 	if (!pud_present(*pud))
144                 return;
145 
146 	pmd = pmd_offset(pud, addr);
147 	if (!pmd_present(*pmd))
148 		return;
149 
150 	ptep = pte_offset_map(pmd, addr);
151 
152 	if (!is_swap_pte(*ptep)) {
153 		pte_unmap(ptep);
154  		return;
155  	}
156 
157  	ptl = pte_lockptr(mm, pmd);
158  	spin_lock(ptl);
159 	pte = *ptep;
160 	if (!is_swap_pte(pte))
161 		goto out;
162 
163 	entry = pte_to_swp_entry(pte);
164 
165 	if (!is_migration_entry(entry) || migration_entry_to_page(entry) != old)
166 		goto out;
167 
168 	/*
169 	 * Yes, ignore the return value from a GFP_ATOMIC mem_cgroup_charge.
170 	 * Failure is not an option here: we're now expected to remove every
171 	 * migration pte, and will cause crashes otherwise.  Normally this
172 	 * is not an issue: mem_cgroup_prepare_migration bumped up the old
173 	 * page_cgroup count for safety, that's now attached to the new page,
174 	 * so this charge should just be another incrementation of the count,
175 	 * to keep in balance with rmap.c's mem_cgroup_uncharging.  But if
176 	 * there's been a force_empty, those reference counts may no longer
177 	 * be reliable, and this charge can actually fail: oh well, we don't
178 	 * make the situation any worse by proceeding as if it had succeeded.
179 	 */
180 	mem_cgroup_charge(new, mm, GFP_ATOMIC);
181 
182 	get_page(new);
183 	pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
184 	if (is_write_migration_entry(entry))
185 		pte = pte_mkwrite(pte);
186 	flush_cache_page(vma, addr, pte_pfn(pte));
187 	set_pte_at(mm, addr, ptep, pte);
188 
189 	if (PageAnon(new))
190 		page_add_anon_rmap(new, vma, addr);
191 	else
192 		page_add_file_rmap(new);
193 
194 	/* No need to invalidate - it was non-present before */
195 	update_mmu_cache(vma, addr, pte);
196 
197 out:
198 	pte_unmap_unlock(ptep, ptl);
199 }
200 
201 /*
202  * Note that remove_file_migration_ptes will only work on regular mappings,
203  * Nonlinear mappings do not use migration entries.
204  */
205 static void remove_file_migration_ptes(struct page *old, struct page *new)
206 {
207 	struct vm_area_struct *vma;
208 	struct address_space *mapping = page_mapping(new);
209 	struct prio_tree_iter iter;
210 	pgoff_t pgoff = new->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
211 
212 	if (!mapping)
213 		return;
214 
215 	spin_lock(&mapping->i_mmap_lock);
216 
217 	vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff)
218 		remove_migration_pte(vma, old, new);
219 
220 	spin_unlock(&mapping->i_mmap_lock);
221 }
222 
223 /*
224  * Must hold mmap_sem lock on at least one of the vmas containing
225  * the page so that the anon_vma cannot vanish.
226  */
227 static void remove_anon_migration_ptes(struct page *old, struct page *new)
228 {
229 	struct anon_vma *anon_vma;
230 	struct vm_area_struct *vma;
231 	unsigned long mapping;
232 
233 	mapping = (unsigned long)new->mapping;
234 
235 	if (!mapping || (mapping & PAGE_MAPPING_ANON) == 0)
236 		return;
237 
238 	/*
239 	 * We hold the mmap_sem lock. So no need to call page_lock_anon_vma.
240 	 */
241 	anon_vma = (struct anon_vma *) (mapping - PAGE_MAPPING_ANON);
242 	spin_lock(&anon_vma->lock);
243 
244 	list_for_each_entry(vma, &anon_vma->head, anon_vma_node)
245 		remove_migration_pte(vma, old, new);
246 
247 	spin_unlock(&anon_vma->lock);
248 }
249 
250 /*
251  * Get rid of all migration entries and replace them by
252  * references to the indicated page.
253  */
254 static void remove_migration_ptes(struct page *old, struct page *new)
255 {
256 	if (PageAnon(new))
257 		remove_anon_migration_ptes(old, new);
258 	else
259 		remove_file_migration_ptes(old, new);
260 }
261 
262 /*
263  * Something used the pte of a page under migration. We need to
264  * get to the page and wait until migration is finished.
265  * When we return from this function the fault will be retried.
266  *
267  * This function is called from do_swap_page().
268  */
269 void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
270 				unsigned long address)
271 {
272 	pte_t *ptep, pte;
273 	spinlock_t *ptl;
274 	swp_entry_t entry;
275 	struct page *page;
276 
277 	ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
278 	pte = *ptep;
279 	if (!is_swap_pte(pte))
280 		goto out;
281 
282 	entry = pte_to_swp_entry(pte);
283 	if (!is_migration_entry(entry))
284 		goto out;
285 
286 	page = migration_entry_to_page(entry);
287 
288 	/*
289 	 * Once radix-tree replacement of page migration started, page_count
290 	 * *must* be zero. And, we don't want to call wait_on_page_locked()
291 	 * against a page without get_page().
292 	 * So, we use get_page_unless_zero(), here. Even failed, page fault
293 	 * will occur again.
294 	 */
295 	if (!get_page_unless_zero(page))
296 		goto out;
297 	pte_unmap_unlock(ptep, ptl);
298 	wait_on_page_locked(page);
299 	put_page(page);
300 	return;
301 out:
302 	pte_unmap_unlock(ptep, ptl);
303 }
304 
305 /*
306  * Replace the page in the mapping.
307  *
308  * The number of remaining references must be:
309  * 1 for anonymous pages without a mapping
310  * 2 for pages with a mapping
311  * 3 for pages with a mapping and PagePrivate set.
312  */
313 static int migrate_page_move_mapping(struct address_space *mapping,
314 		struct page *newpage, struct page *page)
315 {
316 	int expected_count;
317 	void **pslot;
318 
319 	if (!mapping) {
320 		/* Anonymous page without mapping */
321 		if (page_count(page) != 1)
322 			return -EAGAIN;
323 		return 0;
324 	}
325 
326 	write_lock_irq(&mapping->tree_lock);
327 
328 	pslot = radix_tree_lookup_slot(&mapping->page_tree,
329  					page_index(page));
330 
331 	expected_count = 2 + !!PagePrivate(page);
332 	if (page_count(page) != expected_count ||
333 			(struct page *)radix_tree_deref_slot(pslot) != page) {
334 		write_unlock_irq(&mapping->tree_lock);
335 		return -EAGAIN;
336 	}
337 
338 	if (!page_freeze_refs(page, expected_count)) {
339 		write_unlock_irq(&mapping->tree_lock);
340 		return -EAGAIN;
341 	}
342 
343 	/*
344 	 * Now we know that no one else is looking at the page.
345 	 */
346 	get_page(newpage);	/* add cache reference */
347 #ifdef CONFIG_SWAP
348 	if (PageSwapCache(page)) {
349 		SetPageSwapCache(newpage);
350 		set_page_private(newpage, page_private(page));
351 	}
352 #endif
353 
354 	radix_tree_replace_slot(pslot, newpage);
355 
356 	page_unfreeze_refs(page, expected_count);
357 	/*
358 	 * Drop cache reference from old page.
359 	 * We know this isn't the last reference.
360 	 */
361 	__put_page(page);
362 
363 	/*
364 	 * If moved to a different zone then also account
365 	 * the page for that zone. Other VM counters will be
366 	 * taken care of when we establish references to the
367 	 * new page and drop references to the old page.
368 	 *
369 	 * Note that anonymous pages are accounted for
370 	 * via NR_FILE_PAGES and NR_ANON_PAGES if they
371 	 * are mapped to swap space.
372 	 */
373 	__dec_zone_page_state(page, NR_FILE_PAGES);
374 	__inc_zone_page_state(newpage, NR_FILE_PAGES);
375 
376 	write_unlock_irq(&mapping->tree_lock);
377 	if (!PageSwapCache(newpage)) {
378 		mem_cgroup_uncharge_cache_page(page);
379 	}
380 
381 	return 0;
382 }
383 
384 /*
385  * Copy the page to its new location
386  */
387 static void migrate_page_copy(struct page *newpage, struct page *page)
388 {
389 	copy_highpage(newpage, page);
390 
391 	if (PageError(page))
392 		SetPageError(newpage);
393 	if (PageReferenced(page))
394 		SetPageReferenced(newpage);
395 	if (PageUptodate(page))
396 		SetPageUptodate(newpage);
397 	if (PageActive(page))
398 		SetPageActive(newpage);
399 	if (PageChecked(page))
400 		SetPageChecked(newpage);
401 	if (PageMappedToDisk(page))
402 		SetPageMappedToDisk(newpage);
403 
404 	if (PageDirty(page)) {
405 		clear_page_dirty_for_io(page);
406 		/*
407 		 * Want to mark the page and the radix tree as dirty, and
408 		 * redo the accounting that clear_page_dirty_for_io undid,
409 		 * but we can't use set_page_dirty because that function
410 		 * is actually a signal that all of the page has become dirty.
411 		 * Wheras only part of our page may be dirty.
412 		 */
413 		__set_page_dirty_nobuffers(newpage);
414  	}
415 
416 #ifdef CONFIG_SWAP
417 	ClearPageSwapCache(page);
418 #endif
419 	ClearPageActive(page);
420 	ClearPagePrivate(page);
421 	set_page_private(page, 0);
422 	page->mapping = NULL;
423 
424 	/*
425 	 * If any waiters have accumulated on the new page then
426 	 * wake them up.
427 	 */
428 	if (PageWriteback(newpage))
429 		end_page_writeback(newpage);
430 }
431 
432 /************************************************************
433  *                    Migration functions
434  ***********************************************************/
435 
436 /* Always fail migration. Used for mappings that are not movable */
437 int fail_migrate_page(struct address_space *mapping,
438 			struct page *newpage, struct page *page)
439 {
440 	return -EIO;
441 }
442 EXPORT_SYMBOL(fail_migrate_page);
443 
444 /*
445  * Common logic to directly migrate a single page suitable for
446  * pages that do not use PagePrivate.
447  *
448  * Pages are locked upon entry and exit.
449  */
450 int migrate_page(struct address_space *mapping,
451 		struct page *newpage, struct page *page)
452 {
453 	int rc;
454 
455 	BUG_ON(PageWriteback(page));	/* Writeback must be complete */
456 
457 	rc = migrate_page_move_mapping(mapping, newpage, page);
458 
459 	if (rc)
460 		return rc;
461 
462 	migrate_page_copy(newpage, page);
463 	return 0;
464 }
465 EXPORT_SYMBOL(migrate_page);
466 
467 #ifdef CONFIG_BLOCK
468 /*
469  * Migration function for pages with buffers. This function can only be used
470  * if the underlying filesystem guarantees that no other references to "page"
471  * exist.
472  */
473 int buffer_migrate_page(struct address_space *mapping,
474 		struct page *newpage, struct page *page)
475 {
476 	struct buffer_head *bh, *head;
477 	int rc;
478 
479 	if (!page_has_buffers(page))
480 		return migrate_page(mapping, newpage, page);
481 
482 	head = page_buffers(page);
483 
484 	rc = migrate_page_move_mapping(mapping, newpage, page);
485 
486 	if (rc)
487 		return rc;
488 
489 	bh = head;
490 	do {
491 		get_bh(bh);
492 		lock_buffer(bh);
493 		bh = bh->b_this_page;
494 
495 	} while (bh != head);
496 
497 	ClearPagePrivate(page);
498 	set_page_private(newpage, page_private(page));
499 	set_page_private(page, 0);
500 	put_page(page);
501 	get_page(newpage);
502 
503 	bh = head;
504 	do {
505 		set_bh_page(bh, newpage, bh_offset(bh));
506 		bh = bh->b_this_page;
507 
508 	} while (bh != head);
509 
510 	SetPagePrivate(newpage);
511 
512 	migrate_page_copy(newpage, page);
513 
514 	bh = head;
515 	do {
516 		unlock_buffer(bh);
517  		put_bh(bh);
518 		bh = bh->b_this_page;
519 
520 	} while (bh != head);
521 
522 	return 0;
523 }
524 EXPORT_SYMBOL(buffer_migrate_page);
525 #endif
526 
527 /*
528  * Writeback a page to clean the dirty state
529  */
530 static int writeout(struct address_space *mapping, struct page *page)
531 {
532 	struct writeback_control wbc = {
533 		.sync_mode = WB_SYNC_NONE,
534 		.nr_to_write = 1,
535 		.range_start = 0,
536 		.range_end = LLONG_MAX,
537 		.nonblocking = 1,
538 		.for_reclaim = 1
539 	};
540 	int rc;
541 
542 	if (!mapping->a_ops->writepage)
543 		/* No write method for the address space */
544 		return -EINVAL;
545 
546 	if (!clear_page_dirty_for_io(page))
547 		/* Someone else already triggered a write */
548 		return -EAGAIN;
549 
550 	/*
551 	 * A dirty page may imply that the underlying filesystem has
552 	 * the page on some queue. So the page must be clean for
553 	 * migration. Writeout may mean we loose the lock and the
554 	 * page state is no longer what we checked for earlier.
555 	 * At this point we know that the migration attempt cannot
556 	 * be successful.
557 	 */
558 	remove_migration_ptes(page, page);
559 
560 	rc = mapping->a_ops->writepage(page, &wbc);
561 	if (rc < 0)
562 		/* I/O Error writing */
563 		return -EIO;
564 
565 	if (rc != AOP_WRITEPAGE_ACTIVATE)
566 		/* unlocked. Relock */
567 		lock_page(page);
568 
569 	return -EAGAIN;
570 }
571 
572 /*
573  * Default handling if a filesystem does not provide a migration function.
574  */
575 static int fallback_migrate_page(struct address_space *mapping,
576 	struct page *newpage, struct page *page)
577 {
578 	if (PageDirty(page))
579 		return writeout(mapping, page);
580 
581 	/*
582 	 * Buffers may be managed in a filesystem specific way.
583 	 * We must have no buffers or drop them.
584 	 */
585 	if (PagePrivate(page) &&
586 	    !try_to_release_page(page, GFP_KERNEL))
587 		return -EAGAIN;
588 
589 	return migrate_page(mapping, newpage, page);
590 }
591 
592 /*
593  * Move a page to a newly allocated page
594  * The page is locked and all ptes have been successfully removed.
595  *
596  * The new page will have replaced the old page if this function
597  * is successful.
598  */
599 static int move_to_new_page(struct page *newpage, struct page *page)
600 {
601 	struct address_space *mapping;
602 	int rc;
603 
604 	/*
605 	 * Block others from accessing the page when we get around to
606 	 * establishing additional references. We are the only one
607 	 * holding a reference to the new page at this point.
608 	 */
609 	if (TestSetPageLocked(newpage))
610 		BUG();
611 
612 	/* Prepare mapping for the new page.*/
613 	newpage->index = page->index;
614 	newpage->mapping = page->mapping;
615 
616 	mapping = page_mapping(page);
617 	if (!mapping)
618 		rc = migrate_page(mapping, newpage, page);
619 	else if (mapping->a_ops->migratepage)
620 		/*
621 		 * Most pages have a mapping and most filesystems
622 		 * should provide a migration function. Anonymous
623 		 * pages are part of swap space which also has its
624 		 * own migration function. This is the most common
625 		 * path for page migration.
626 		 */
627 		rc = mapping->a_ops->migratepage(mapping,
628 						newpage, page);
629 	else
630 		rc = fallback_migrate_page(mapping, newpage, page);
631 
632 	if (!rc) {
633 		remove_migration_ptes(page, newpage);
634 	} else
635 		newpage->mapping = NULL;
636 
637 	unlock_page(newpage);
638 
639 	return rc;
640 }
641 
642 /*
643  * Obtain the lock on page, remove all ptes and migrate the page
644  * to the newly allocated page in newpage.
645  */
646 static int unmap_and_move(new_page_t get_new_page, unsigned long private,
647 			struct page *page, int force)
648 {
649 	int rc = 0;
650 	int *result = NULL;
651 	struct page *newpage = get_new_page(page, private, &result);
652 	int rcu_locked = 0;
653 	int charge = 0;
654 
655 	if (!newpage)
656 		return -ENOMEM;
657 
658 	if (page_count(page) == 1)
659 		/* page was freed from under us. So we are done. */
660 		goto move_newpage;
661 
662 	charge = mem_cgroup_prepare_migration(page, newpage);
663 	if (charge == -ENOMEM) {
664 		rc = -ENOMEM;
665 		goto move_newpage;
666 	}
667 	/* prepare cgroup just returns 0 or -ENOMEM */
668 	BUG_ON(charge);
669 
670 	rc = -EAGAIN;
671 	if (TestSetPageLocked(page)) {
672 		if (!force)
673 			goto move_newpage;
674 		lock_page(page);
675 	}
676 
677 	if (PageWriteback(page)) {
678 		if (!force)
679 			goto unlock;
680 		wait_on_page_writeback(page);
681 	}
682 	/*
683 	 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
684 	 * we cannot notice that anon_vma is freed while we migrates a page.
685 	 * This rcu_read_lock() delays freeing anon_vma pointer until the end
686 	 * of migration. File cache pages are no problem because of page_lock()
687 	 * File Caches may use write_page() or lock_page() in migration, then,
688 	 * just care Anon page here.
689 	 */
690 	if (PageAnon(page)) {
691 		rcu_read_lock();
692 		rcu_locked = 1;
693 	}
694 
695 	/*
696 	 * Corner case handling:
697 	 * 1. When a new swap-cache page is read into, it is added to the LRU
698 	 * and treated as swapcache but it has no rmap yet.
699 	 * Calling try_to_unmap() against a page->mapping==NULL page will
700 	 * trigger a BUG.  So handle it here.
701 	 * 2. An orphaned page (see truncate_complete_page) might have
702 	 * fs-private metadata. The page can be picked up due to memory
703 	 * offlining.  Everywhere else except page reclaim, the page is
704 	 * invisible to the vm, so the page can not be migrated.  So try to
705 	 * free the metadata, so the page can be freed.
706 	 */
707 	if (!page->mapping) {
708 		if (!PageAnon(page) && PagePrivate(page)) {
709 			/*
710 			 * Go direct to try_to_free_buffers() here because
711 			 * a) that's what try_to_release_page() would do anyway
712 			 * b) we may be under rcu_read_lock() here, so we can't
713 			 *    use GFP_KERNEL which is what try_to_release_page()
714 			 *    needs to be effective.
715 			 */
716 			try_to_free_buffers(page);
717 		}
718 		goto rcu_unlock;
719 	}
720 
721 	/* Establish migration ptes or remove ptes */
722 	try_to_unmap(page, 1);
723 
724 	if (!page_mapped(page))
725 		rc = move_to_new_page(newpage, page);
726 
727 	if (rc)
728 		remove_migration_ptes(page, page);
729 rcu_unlock:
730 	if (rcu_locked)
731 		rcu_read_unlock();
732 
733 unlock:
734 
735 	unlock_page(page);
736 
737 	if (rc != -EAGAIN) {
738  		/*
739  		 * A page that has been migrated has all references
740  		 * removed and will be freed. A page that has not been
741  		 * migrated will have kepts its references and be
742  		 * restored.
743  		 */
744  		list_del(&page->lru);
745  		move_to_lru(page);
746 	}
747 
748 move_newpage:
749 	if (!charge)
750 		mem_cgroup_end_migration(newpage);
751 	/*
752 	 * Move the new page to the LRU. If migration was not successful
753 	 * then this will free the page.
754 	 */
755 	move_to_lru(newpage);
756 	if (result) {
757 		if (rc)
758 			*result = rc;
759 		else
760 			*result = page_to_nid(newpage);
761 	}
762 	return rc;
763 }
764 
765 /*
766  * migrate_pages
767  *
768  * The function takes one list of pages to migrate and a function
769  * that determines from the page to be migrated and the private data
770  * the target of the move and allocates the page.
771  *
772  * The function returns after 10 attempts or if no pages
773  * are movable anymore because to has become empty
774  * or no retryable pages exist anymore. All pages will be
775  * returned to the LRU or freed.
776  *
777  * Return: Number of pages not migrated or error code.
778  */
779 int migrate_pages(struct list_head *from,
780 		new_page_t get_new_page, unsigned long private)
781 {
782 	int retry = 1;
783 	int nr_failed = 0;
784 	int pass = 0;
785 	struct page *page;
786 	struct page *page2;
787 	int swapwrite = current->flags & PF_SWAPWRITE;
788 	int rc;
789 
790 	if (!swapwrite)
791 		current->flags |= PF_SWAPWRITE;
792 
793 	for(pass = 0; pass < 10 && retry; pass++) {
794 		retry = 0;
795 
796 		list_for_each_entry_safe(page, page2, from, lru) {
797 			cond_resched();
798 
799 			rc = unmap_and_move(get_new_page, private,
800 						page, pass > 2);
801 
802 			switch(rc) {
803 			case -ENOMEM:
804 				goto out;
805 			case -EAGAIN:
806 				retry++;
807 				break;
808 			case 0:
809 				break;
810 			default:
811 				/* Permanent failure */
812 				nr_failed++;
813 				break;
814 			}
815 		}
816 	}
817 	rc = 0;
818 out:
819 	if (!swapwrite)
820 		current->flags &= ~PF_SWAPWRITE;
821 
822 	putback_lru_pages(from);
823 
824 	if (rc)
825 		return rc;
826 
827 	return nr_failed + retry;
828 }
829 
830 #ifdef CONFIG_NUMA
831 /*
832  * Move a list of individual pages
833  */
834 struct page_to_node {
835 	unsigned long addr;
836 	struct page *page;
837 	int node;
838 	int status;
839 };
840 
841 static struct page *new_page_node(struct page *p, unsigned long private,
842 		int **result)
843 {
844 	struct page_to_node *pm = (struct page_to_node *)private;
845 
846 	while (pm->node != MAX_NUMNODES && pm->page != p)
847 		pm++;
848 
849 	if (pm->node == MAX_NUMNODES)
850 		return NULL;
851 
852 	*result = &pm->status;
853 
854 	return alloc_pages_node(pm->node,
855 				GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
856 }
857 
858 /*
859  * Move a set of pages as indicated in the pm array. The addr
860  * field must be set to the virtual address of the page to be moved
861  * and the node number must contain a valid target node.
862  */
863 static int do_move_pages(struct mm_struct *mm, struct page_to_node *pm,
864 				int migrate_all)
865 {
866 	int err;
867 	struct page_to_node *pp;
868 	LIST_HEAD(pagelist);
869 
870 	down_read(&mm->mmap_sem);
871 
872 	/*
873 	 * Build a list of pages to migrate
874 	 */
875 	migrate_prep();
876 	for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
877 		struct vm_area_struct *vma;
878 		struct page *page;
879 
880 		/*
881 		 * A valid page pointer that will not match any of the
882 		 * pages that will be moved.
883 		 */
884 		pp->page = ZERO_PAGE(0);
885 
886 		err = -EFAULT;
887 		vma = find_vma(mm, pp->addr);
888 		if (!vma || !vma_migratable(vma))
889 			goto set_status;
890 
891 		page = follow_page(vma, pp->addr, FOLL_GET);
892 
893 		err = PTR_ERR(page);
894 		if (IS_ERR(page))
895 			goto set_status;
896 
897 		err = -ENOENT;
898 		if (!page)
899 			goto set_status;
900 
901 		if (PageReserved(page))		/* Check for zero page */
902 			goto put_and_set;
903 
904 		pp->page = page;
905 		err = page_to_nid(page);
906 
907 		if (err == pp->node)
908 			/*
909 			 * Node already in the right place
910 			 */
911 			goto put_and_set;
912 
913 		err = -EACCES;
914 		if (page_mapcount(page) > 1 &&
915 				!migrate_all)
916 			goto put_and_set;
917 
918 		err = isolate_lru_page(page, &pagelist);
919 put_and_set:
920 		/*
921 		 * Either remove the duplicate refcount from
922 		 * isolate_lru_page() or drop the page ref if it was
923 		 * not isolated.
924 		 */
925 		put_page(page);
926 set_status:
927 		pp->status = err;
928 	}
929 
930 	if (!list_empty(&pagelist))
931 		err = migrate_pages(&pagelist, new_page_node,
932 				(unsigned long)pm);
933 	else
934 		err = -ENOENT;
935 
936 	up_read(&mm->mmap_sem);
937 	return err;
938 }
939 
940 /*
941  * Determine the nodes of a list of pages. The addr in the pm array
942  * must have been set to the virtual address of which we want to determine
943  * the node number.
944  */
945 static int do_pages_stat(struct mm_struct *mm, struct page_to_node *pm)
946 {
947 	down_read(&mm->mmap_sem);
948 
949 	for ( ; pm->node != MAX_NUMNODES; pm++) {
950 		struct vm_area_struct *vma;
951 		struct page *page;
952 		int err;
953 
954 		err = -EFAULT;
955 		vma = find_vma(mm, pm->addr);
956 		if (!vma)
957 			goto set_status;
958 
959 		page = follow_page(vma, pm->addr, 0);
960 
961 		err = PTR_ERR(page);
962 		if (IS_ERR(page))
963 			goto set_status;
964 
965 		err = -ENOENT;
966 		/* Use PageReserved to check for zero page */
967 		if (!page || PageReserved(page))
968 			goto set_status;
969 
970 		err = page_to_nid(page);
971 set_status:
972 		pm->status = err;
973 	}
974 
975 	up_read(&mm->mmap_sem);
976 	return 0;
977 }
978 
979 /*
980  * Move a list of pages in the address space of the currently executing
981  * process.
982  */
983 asmlinkage long sys_move_pages(pid_t pid, unsigned long nr_pages,
984 			const void __user * __user *pages,
985 			const int __user *nodes,
986 			int __user *status, int flags)
987 {
988 	int err = 0;
989 	int i;
990 	struct task_struct *task;
991 	nodemask_t task_nodes;
992 	struct mm_struct *mm;
993 	struct page_to_node *pm = NULL;
994 
995 	/* Check flags */
996 	if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
997 		return -EINVAL;
998 
999 	if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1000 		return -EPERM;
1001 
1002 	/* Find the mm_struct */
1003 	read_lock(&tasklist_lock);
1004 	task = pid ? find_task_by_vpid(pid) : current;
1005 	if (!task) {
1006 		read_unlock(&tasklist_lock);
1007 		return -ESRCH;
1008 	}
1009 	mm = get_task_mm(task);
1010 	read_unlock(&tasklist_lock);
1011 
1012 	if (!mm)
1013 		return -EINVAL;
1014 
1015 	/*
1016 	 * Check if this process has the right to modify the specified
1017 	 * process. The right exists if the process has administrative
1018 	 * capabilities, superuser privileges or the same
1019 	 * userid as the target process.
1020 	 */
1021 	if ((current->euid != task->suid) && (current->euid != task->uid) &&
1022 	    (current->uid != task->suid) && (current->uid != task->uid) &&
1023 	    !capable(CAP_SYS_NICE)) {
1024 		err = -EPERM;
1025 		goto out2;
1026 	}
1027 
1028  	err = security_task_movememory(task);
1029  	if (err)
1030  		goto out2;
1031 
1032 
1033 	task_nodes = cpuset_mems_allowed(task);
1034 
1035 	/* Limit nr_pages so that the multiplication may not overflow */
1036 	if (nr_pages >= ULONG_MAX / sizeof(struct page_to_node) - 1) {
1037 		err = -E2BIG;
1038 		goto out2;
1039 	}
1040 
1041 	pm = vmalloc((nr_pages + 1) * sizeof(struct page_to_node));
1042 	if (!pm) {
1043 		err = -ENOMEM;
1044 		goto out2;
1045 	}
1046 
1047 	/*
1048 	 * Get parameters from user space and initialize the pm
1049 	 * array. Return various errors if the user did something wrong.
1050 	 */
1051 	for (i = 0; i < nr_pages; i++) {
1052 		const void __user *p;
1053 
1054 		err = -EFAULT;
1055 		if (get_user(p, pages + i))
1056 			goto out;
1057 
1058 		pm[i].addr = (unsigned long)p;
1059 		if (nodes) {
1060 			int node;
1061 
1062 			if (get_user(node, nodes + i))
1063 				goto out;
1064 
1065 			err = -ENODEV;
1066 			if (!node_state(node, N_HIGH_MEMORY))
1067 				goto out;
1068 
1069 			err = -EACCES;
1070 			if (!node_isset(node, task_nodes))
1071 				goto out;
1072 
1073 			pm[i].node = node;
1074 		} else
1075 			pm[i].node = 0;	/* anything to not match MAX_NUMNODES */
1076 	}
1077 	/* End marker */
1078 	pm[nr_pages].node = MAX_NUMNODES;
1079 
1080 	if (nodes)
1081 		err = do_move_pages(mm, pm, flags & MPOL_MF_MOVE_ALL);
1082 	else
1083 		err = do_pages_stat(mm, pm);
1084 
1085 	if (err >= 0)
1086 		/* Return status information */
1087 		for (i = 0; i < nr_pages; i++)
1088 			if (put_user(pm[i].status, status + i))
1089 				err = -EFAULT;
1090 
1091 out:
1092 	vfree(pm);
1093 out2:
1094 	mmput(mm);
1095 	return err;
1096 }
1097 
1098 /*
1099  * Call migration functions in the vma_ops that may prepare
1100  * memory in a vm for migration. migration functions may perform
1101  * the migration for vmas that do not have an underlying page struct.
1102  */
1103 int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
1104 	const nodemask_t *from, unsigned long flags)
1105 {
1106  	struct vm_area_struct *vma;
1107  	int err = 0;
1108 
1109  	for(vma = mm->mmap; vma->vm_next && !err; vma = vma->vm_next) {
1110  		if (vma->vm_ops && vma->vm_ops->migrate) {
1111  			err = vma->vm_ops->migrate(vma, to, from, flags);
1112  			if (err)
1113  				break;
1114  		}
1115  	}
1116  	return err;
1117 }
1118 #endif
1119