blob: 5f0857bf5300d8b33feedef0ea564466aa4d4a64 [file] [log] [blame]
Christoph Lameterb20a3502006-03-22 00:09:12 -08001/*
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>
Christoph Lametercde53532008-07-04 09:59:22 -070012 * Christoph Lameter
Christoph Lameterb20a3502006-03-22 00:09:12 -080013 */
14
15#include <linux/migrate.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040016#include <linux/export.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080017#include <linux/swap.h>
Christoph Lameter06972122006-06-23 02:03:35 -070018#include <linux/swapops.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080019#include <linux/pagemap.h>
Christoph Lametere23ca002006-04-10 22:52:57 -070020#include <linux/buffer_head.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080021#include <linux/mm_inline.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070022#include <linux/nsproxy.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080023#include <linux/pagevec.h>
Hugh Dickinse9995ef2009-12-14 17:59:31 -080024#include <linux/ksm.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080025#include <linux/rmap.h>
26#include <linux/topology.h>
27#include <linux/cpu.h>
28#include <linux/cpuset.h>
Christoph Lameter04e62a22006-06-23 02:03:38 -070029#include <linux/writeback.h>
Christoph Lameter742755a2006-06-23 02:03:55 -070030#include <linux/mempolicy.h>
31#include <linux/vmalloc.h>
David Quigley86c3a762006-06-23 02:04:02 -070032#include <linux/security.h>
Hugh Dickinse468c872016-07-15 15:08:19 -040033#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080034#include <linux/memcontrol.h>
Adrian Bunk4f5ca262008-07-23 21:27:02 -070035#include <linux/syscalls.h>
Naoya Horiguchi290408d2010-09-08 10:19:35 +090036#include <linux/hugetlb.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/gfp.h>
Liam Markcc2d4bd2013-01-16 10:14:40 -080038#include <trace/events/kmem.h>
Linus Torvalds4c497362017-08-20 13:26:27 -070039#include <linux/ptrace.h>
Michal Nazarewicz0d1836c2010-12-21 17:24:26 -080040#include <asm/tlbflush.h>
41
Christoph Lameterb20a3502006-03-22 00:09:12 -080042#include "internal.h"
43
Christoph Lameterb20a3502006-03-22 00:09:12 -080044/*
Christoph Lameter742755a2006-06-23 02:03:55 -070045 * migrate_prep() needs to be called before we start compiling a list of pages
Mel Gorman748446b2010-05-24 14:32:27 -070046 * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is
47 * undesirable, use migrate_prep_local()
Christoph Lameterb20a3502006-03-22 00:09:12 -080048 */
49int migrate_prep(void)
50{
Christoph Lameterb20a3502006-03-22 00:09:12 -080051 /*
52 * Clear the LRU lists so pages can be isolated.
53 * Note that pages may be moved off the LRU after we have
54 * drained them. Those pages will fail to migrate like other
55 * pages that may be busy.
56 */
57 lru_add_drain_all();
58
59 return 0;
60}
61
Mel Gorman748446b2010-05-24 14:32:27 -070062/* Do the necessary work of migrate_prep but not if it involves other CPUs */
63int migrate_prep_local(void)
64{
65 lru_add_drain();
66
67 return 0;
68}
69
Christoph Lameterb20a3502006-03-22 00:09:12 -080070/*
Lee Schermerhorn894bc312008-10-18 20:26:39 -070071 * Add isolated pages on the list back to the LRU under page lock
72 * to avoid leaking evictable pages back onto unevictable list.
Christoph Lameterb20a3502006-03-22 00:09:12 -080073 */
Minchan Kime13861d2010-05-24 14:31:59 -070074void putback_lru_pages(struct list_head *l)
Christoph Lameterb20a3502006-03-22 00:09:12 -080075{
76 struct page *page;
77 struct page *page2;
Christoph Lameterb20a3502006-03-22 00:09:12 -080078
79 list_for_each_entry_safe(page, page2, l, lru) {
Christoph Lametere24f0b82006-06-23 02:03:51 -070080 list_del(&page->lru);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -070081 dec_zone_page_state(page, NR_ISOLATED_ANON +
Johannes Weiner6c0b1352009-09-21 17:02:59 -070082 page_is_file_cache(page));
Lee Schermerhorn894bc312008-10-18 20:26:39 -070083 putback_lru_page(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -080084 }
Christoph Lameterb20a3502006-03-22 00:09:12 -080085}
86
Christoph Lameter06972122006-06-23 02:03:35 -070087/*
88 * Restore a potential migration pte to a working pte entry
89 */
Hugh Dickinse9995ef2009-12-14 17:59:31 -080090static int remove_migration_pte(struct page *new, struct vm_area_struct *vma,
91 unsigned long addr, void *old)
Christoph Lameter06972122006-06-23 02:03:35 -070092{
93 struct mm_struct *mm = vma->vm_mm;
94 swp_entry_t entry;
95 pgd_t *pgd;
96 pud_t *pud;
97 pmd_t *pmd;
98 pte_t *ptep, pte;
99 spinlock_t *ptl;
100
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900101 if (unlikely(PageHuge(new))) {
102 ptep = huge_pte_offset(mm, addr);
103 if (!ptep)
104 goto out;
105 ptl = &mm->page_table_lock;
106 } else {
107 pgd = pgd_offset(mm, addr);
108 if (!pgd_present(*pgd))
109 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700110
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900111 pud = pud_offset(pgd, addr);
112 if (!pud_present(*pud))
113 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700114
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900115 pmd = pmd_offset(pud, addr);
Andrea Arcangeli500d65d2011-01-13 15:46:55 -0800116 if (pmd_trans_huge(*pmd))
117 goto out;
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900118 if (!pmd_present(*pmd))
119 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700120
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900121 ptep = pte_offset_map(pmd, addr);
Christoph Lameter06972122006-06-23 02:03:35 -0700122
Hugh Dickins486cf462011-10-19 12:50:35 -0700123 /*
124 * Peek to check is_swap_pte() before taking ptlock? No, we
125 * can race mremap's move_ptes(), which skips anon_vma lock.
126 */
Christoph Lameter06972122006-06-23 02:03:35 -0700127
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900128 ptl = pte_lockptr(mm, pmd);
129 }
130
Christoph Lameter06972122006-06-23 02:03:35 -0700131 spin_lock(ptl);
132 pte = *ptep;
133 if (!is_swap_pte(pte))
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800134 goto unlock;
Christoph Lameter06972122006-06-23 02:03:35 -0700135
136 entry = pte_to_swp_entry(pte);
137
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800138 if (!is_migration_entry(entry) ||
139 migration_entry_to_page(entry) != old)
140 goto unlock;
Christoph Lameter06972122006-06-23 02:03:35 -0700141
Christoph Lameter06972122006-06-23 02:03:35 -0700142 get_page(new);
143 pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
144 if (is_write_migration_entry(entry))
145 pte = pte_mkwrite(pte);
Andi Kleen3ef8fd72010-10-11 16:03:21 +0200146#ifdef CONFIG_HUGETLB_PAGE
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900147 if (PageHuge(new))
148 pte = pte_mkhuge(pte);
Andi Kleen3ef8fd72010-10-11 16:03:21 +0200149#endif
Leonid Yegoshina0ca5172013-05-24 15:55:18 -0700150 flush_dcache_page(new);
Christoph Lameter06972122006-06-23 02:03:35 -0700151 set_pte_at(mm, addr, ptep, pte);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700152
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900153 if (PageHuge(new)) {
154 if (PageAnon(new))
155 hugepage_add_anon_rmap(new, vma, addr);
156 else
157 page_dup_rmap(new);
158 } else if (PageAnon(new))
Christoph Lameter04e62a22006-06-23 02:03:38 -0700159 page_add_anon_rmap(new, vma, addr);
160 else
161 page_add_file_rmap(new);
162
163 /* No need to invalidate - it was non-present before */
Russell King4b3073e2009-12-18 16:40:18 +0000164 update_mmu_cache(vma, addr, ptep);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800165unlock:
Christoph Lameter06972122006-06-23 02:03:35 -0700166 pte_unmap_unlock(ptep, ptl);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800167out:
168 return SWAP_AGAIN;
Christoph Lameter06972122006-06-23 02:03:35 -0700169}
170
171/*
Christoph Lameter04e62a22006-06-23 02:03:38 -0700172 * Get rid of all migration entries and replace them by
173 * references to the indicated page.
174 */
175static void remove_migration_ptes(struct page *old, struct page *new)
176{
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800177 rmap_walk(new, remove_migration_pte, old);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700178}
179
180/*
Christoph Lameter06972122006-06-23 02:03:35 -0700181 * Something used the pte of a page under migration. We need to
182 * get to the page and wait until migration is finished.
183 * When we return from this function the fault will be retried.
Christoph Lameter06972122006-06-23 02:03:35 -0700184 */
Naoya Horiguchi065d0e82013-06-12 14:05:04 -0700185static void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
186 spinlock_t *ptl)
Christoph Lameter06972122006-06-23 02:03:35 -0700187{
Naoya Horiguchi065d0e82013-06-12 14:05:04 -0700188 pte_t pte;
Christoph Lameter06972122006-06-23 02:03:35 -0700189 swp_entry_t entry;
190 struct page *page;
191
Naoya Horiguchi065d0e82013-06-12 14:05:04 -0700192 spin_lock(ptl);
Christoph Lameter06972122006-06-23 02:03:35 -0700193 pte = *ptep;
194 if (!is_swap_pte(pte))
195 goto out;
196
197 entry = pte_to_swp_entry(pte);
198 if (!is_migration_entry(entry))
199 goto out;
200
201 page = migration_entry_to_page(entry);
202
Nick Piggine2867812008-07-25 19:45:30 -0700203 /*
204 * Once radix-tree replacement of page migration started, page_count
205 * *must* be zero. And, we don't want to call wait_on_page_locked()
206 * against a page without get_page().
207 * So, we use get_page_unless_zero(), here. Even failed, page fault
208 * will occur again.
209 */
210 if (!get_page_unless_zero(page))
211 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700212 pte_unmap_unlock(ptep, ptl);
213 wait_on_page_locked(page);
214 put_page(page);
215 return;
216out:
217 pte_unmap_unlock(ptep, ptl);
218}
219
Naoya Horiguchi065d0e82013-06-12 14:05:04 -0700220void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
221 unsigned long address)
222{
223 spinlock_t *ptl = pte_lockptr(mm, pmd);
224 pte_t *ptep = pte_offset_map(pmd, address);
225 __migration_entry_wait(mm, ptep, ptl);
226}
227
228void migration_entry_wait_huge(struct mm_struct *mm, pte_t *pte)
229{
230 spinlock_t *ptl = &(mm)->page_table_lock;
231 __migration_entry_wait(mm, pte, ptl);
232}
233
Mel Gormanb969c4a2012-01-12 17:19:34 -0800234#ifdef CONFIG_BLOCK
235/* Returns true if all buffers are successfully locked */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800236static bool buffer_migrate_lock_buffers(struct buffer_head *head,
237 enum migrate_mode mode)
Mel Gormanb969c4a2012-01-12 17:19:34 -0800238{
239 struct buffer_head *bh = head;
240
241 /* Simple case, sync compaction */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800242 if (mode != MIGRATE_ASYNC) {
Mel Gormanb969c4a2012-01-12 17:19:34 -0800243 do {
244 get_bh(bh);
245 lock_buffer(bh);
246 bh = bh->b_this_page;
247
248 } while (bh != head);
249
250 return true;
251 }
252
253 /* async case, we cannot block on lock_buffer so use trylock_buffer */
254 do {
255 get_bh(bh);
256 if (!trylock_buffer(bh)) {
257 /*
258 * We failed to lock the buffer and cannot stall in
259 * async migration. Release the taken locks
260 */
261 struct buffer_head *failed_bh = bh;
262 put_bh(failed_bh);
263 bh = head;
264 while (bh != failed_bh) {
265 unlock_buffer(bh);
266 put_bh(bh);
267 bh = bh->b_this_page;
268 }
269 return false;
270 }
271
272 bh = bh->b_this_page;
273 } while (bh != head);
274 return true;
275}
276#else
277static inline bool buffer_migrate_lock_buffers(struct buffer_head *head,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800278 enum migrate_mode mode)
Mel Gormanb969c4a2012-01-12 17:19:34 -0800279{
280 return true;
281}
282#endif /* CONFIG_BLOCK */
283
Christoph Lameterb20a3502006-03-22 00:09:12 -0800284/*
Christoph Lameterc3fcf8a2006-06-23 02:03:32 -0700285 * Replace the page in the mapping.
Christoph Lameter5b5c7122006-06-23 02:03:29 -0700286 *
287 * The number of remaining references must be:
288 * 1 for anonymous pages without a mapping
289 * 2 for pages with a mapping
David Howells266cf652009-04-03 16:42:36 +0100290 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800291 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700292static int migrate_page_move_mapping(struct address_space *mapping,
Mel Gormanb969c4a2012-01-12 17:19:34 -0800293 struct page *newpage, struct page *page,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800294 struct buffer_head *head, enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800295{
Hugh Dickinse468c872016-07-15 15:08:19 -0400296 struct zone *oldzone, *newzone;
297 int dirty;
Nick Piggine2867812008-07-25 19:45:30 -0700298 int expected_count;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800299 void **pslot;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800300
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700301 if (!mapping) {
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700302 /* Anonymous page without mapping */
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700303 if (page_count(page) != 1)
304 return -EAGAIN;
305 return 0;
306 }
307
Hugh Dickinse468c872016-07-15 15:08:19 -0400308 oldzone = page_zone(page);
309 newzone = page_zone(newpage);
310
Nick Piggin19fd6232008-07-25 19:45:32 -0700311 spin_lock_irq(&mapping->tree_lock);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800312
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800313 pslot = radix_tree_lookup_slot(&mapping->page_tree,
314 page_index(page));
Christoph Lameterb20a3502006-03-22 00:09:12 -0800315
Johannes Weineredcf4742009-09-21 17:02:59 -0700316 expected_count = 2 + page_has_private(page);
Nick Piggine2867812008-07-25 19:45:30 -0700317 if (page_count(page) != expected_count ||
Mel Gorman29c1f672011-01-13 15:47:21 -0800318 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700319 spin_unlock_irq(&mapping->tree_lock);
Christoph Lametere23ca002006-04-10 22:52:57 -0700320 return -EAGAIN;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800321 }
322
Nick Piggine2867812008-07-25 19:45:30 -0700323 if (!page_freeze_refs(page, expected_count)) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700324 spin_unlock_irq(&mapping->tree_lock);
Nick Piggine2867812008-07-25 19:45:30 -0700325 return -EAGAIN;
326 }
327
Christoph Lameterb20a3502006-03-22 00:09:12 -0800328 /*
Mel Gormanb969c4a2012-01-12 17:19:34 -0800329 * In the async migration case of moving a page with buffers, lock the
330 * buffers using trylock before the mapping is moved. If the mapping
331 * was moved, we later failed to lock the buffers and could not move
332 * the mapping back due to an elevated page count, we would have to
333 * block waiting on other references to be dropped.
334 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800335 if (mode == MIGRATE_ASYNC && head &&
336 !buffer_migrate_lock_buffers(head, mode)) {
Mel Gormanb969c4a2012-01-12 17:19:34 -0800337 page_unfreeze_refs(page, expected_count);
338 spin_unlock_irq(&mapping->tree_lock);
339 return -EAGAIN;
340 }
341
342 /*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800343 * Now we know that no one else is looking at the page.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800344 */
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800345 get_page(newpage); /* add cache reference */
Christoph Lameterb20a3502006-03-22 00:09:12 -0800346 if (PageSwapCache(page)) {
347 SetPageSwapCache(newpage);
348 set_page_private(newpage, page_private(page));
349 }
350
Hugh Dickinse468c872016-07-15 15:08:19 -0400351 /* Move dirty while page refs frozen and newpage not yet exposed */
352 dirty = PageDirty(page);
353 if (dirty) {
354 ClearPageDirty(page);
355 SetPageDirty(newpage);
356 }
357
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800358 radix_tree_replace_slot(pslot, newpage);
359
360 /*
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800361 * Drop cache reference from old page by unfreezing
362 * to one less reference.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800363 * We know this isn't the last reference.
364 */
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800365 page_unfreeze_refs(page, expected_count - 1);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800366
Hugh Dickinse468c872016-07-15 15:08:19 -0400367 spin_unlock(&mapping->tree_lock);
368 /* Leave irq disabled to prevent preemption while updating stats */
369
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700370 /*
371 * If moved to a different zone then also account
372 * the page for that zone. Other VM counters will be
373 * taken care of when we establish references to the
374 * new page and drop references to the old page.
375 *
376 * Note that anonymous pages are accounted for
377 * via NR_FILE_PAGES and NR_ANON_PAGES if they
378 * are mapped to swap space.
379 */
Hugh Dickinse468c872016-07-15 15:08:19 -0400380 if (newzone != oldzone) {
381 __dec_zone_state(oldzone, NR_FILE_PAGES);
382 __inc_zone_state(newzone, NR_FILE_PAGES);
383 if (PageSwapBacked(page) && !PageSwapCache(page)) {
384 __dec_zone_state(oldzone, NR_SHMEM);
385 __inc_zone_state(newzone, NR_SHMEM);
386 }
387 if (dirty && mapping_cap_account_dirty(mapping)) {
388 __dec_zone_state(oldzone, NR_FILE_DIRTY);
389 __inc_zone_state(newzone, NR_FILE_DIRTY);
390 }
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700391 }
Hugh Dickinse468c872016-07-15 15:08:19 -0400392 local_irq_enable();
Christoph Lameterb20a3502006-03-22 00:09:12 -0800393
394 return 0;
395}
Christoph Lameterb20a3502006-03-22 00:09:12 -0800396
397/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900398 * The expected number of remaining references is the same as that
399 * of migrate_page_move_mapping().
400 */
401int migrate_huge_page_move_mapping(struct address_space *mapping,
402 struct page *newpage, struct page *page)
403{
404 int expected_count;
405 void **pslot;
406
407 if (!mapping) {
408 if (page_count(page) != 1)
409 return -EAGAIN;
410 return 0;
411 }
412
413 spin_lock_irq(&mapping->tree_lock);
414
415 pslot = radix_tree_lookup_slot(&mapping->page_tree,
416 page_index(page));
417
418 expected_count = 2 + page_has_private(page);
419 if (page_count(page) != expected_count ||
Mel Gorman29c1f672011-01-13 15:47:21 -0800420 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900421 spin_unlock_irq(&mapping->tree_lock);
422 return -EAGAIN;
423 }
424
425 if (!page_freeze_refs(page, expected_count)) {
426 spin_unlock_irq(&mapping->tree_lock);
427 return -EAGAIN;
428 }
429
430 get_page(newpage);
431
432 radix_tree_replace_slot(pslot, newpage);
433
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800434 page_unfreeze_refs(page, expected_count - 1);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900435
436 spin_unlock_irq(&mapping->tree_lock);
437 return 0;
438}
439
440/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800441 * Copy the page to its new location
442 */
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900443void migrate_page_copy(struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800444{
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900445 if (PageHuge(page))
446 copy_huge_page(newpage, page);
447 else
448 copy_highpage(newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800449
450 if (PageError(page))
451 SetPageError(newpage);
452 if (PageReferenced(page))
453 SetPageReferenced(newpage);
454 if (PageUptodate(page))
455 SetPageUptodate(newpage);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700456 if (TestClearPageActive(page)) {
457 VM_BUG_ON(PageUnevictable(page));
Christoph Lameterb20a3502006-03-22 00:09:12 -0800458 SetPageActive(newpage);
Lee Schermerhorn418b27e2009-12-14 17:59:54 -0800459 } else if (TestClearPageUnevictable(page))
460 SetPageUnevictable(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800461 if (PageChecked(page))
462 SetPageChecked(newpage);
463 if (PageMappedToDisk(page))
464 SetPageMappedToDisk(newpage);
465
Hugh Dickinse468c872016-07-15 15:08:19 -0400466 /* Move dirty on pages not done by migrate_page_move_mapping() */
467 if (PageDirty(page))
468 SetPageDirty(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800469
Nick Pigginb291f002008-10-18 20:26:44 -0700470 mlock_migrate_page(newpage, page);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800471 ksm_migrate_page(newpage, page);
Nick Pigginb291f002008-10-18 20:26:44 -0700472
Christoph Lameterb20a3502006-03-22 00:09:12 -0800473 ClearPageSwapCache(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800474 ClearPagePrivate(page);
475 set_page_private(page, 0);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800476
477 /*
478 * If any waiters have accumulated on the new page then
479 * wake them up.
480 */
481 if (PageWriteback(newpage))
482 end_page_writeback(newpage);
483}
Christoph Lameterb20a3502006-03-22 00:09:12 -0800484
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700485/************************************************************
486 * Migration functions
487 ***********************************************************/
488
489/* Always fail migration. Used for mappings that are not movable */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700490int fail_migrate_page(struct address_space *mapping,
491 struct page *newpage, struct page *page)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700492{
493 return -EIO;
494}
495EXPORT_SYMBOL(fail_migrate_page);
496
Christoph Lameterb20a3502006-03-22 00:09:12 -0800497/*
498 * Common logic to directly migrate a single page suitable for
David Howells266cf652009-04-03 16:42:36 +0100499 * pages that do not use PagePrivate/PagePrivate2.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800500 *
501 * Pages are locked upon entry and exit.
502 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700503int migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800504 struct page *newpage, struct page *page,
505 enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800506{
507 int rc;
508
509 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
510
Mel Gormana6bc32b2012-01-12 17:19:43 -0800511 rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800512
513 if (rc)
514 return rc;
515
516 migrate_page_copy(newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800517 return 0;
518}
519EXPORT_SYMBOL(migrate_page);
520
David Howells93614012006-09-30 20:45:40 +0200521#ifdef CONFIG_BLOCK
Christoph Lameterb20a3502006-03-22 00:09:12 -0800522/*
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700523 * Migration function for pages with buffers. This function can only be used
524 * if the underlying filesystem guarantees that no other references to "page"
525 * exist.
526 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700527int buffer_migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800528 struct page *newpage, struct page *page, enum migrate_mode mode)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700529{
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700530 struct buffer_head *bh, *head;
531 int rc;
532
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700533 if (!page_has_buffers(page))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800534 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700535
536 head = page_buffers(page);
537
Mel Gormana6bc32b2012-01-12 17:19:43 -0800538 rc = migrate_page_move_mapping(mapping, newpage, page, head, mode);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700539
540 if (rc)
541 return rc;
542
Mel Gormanb969c4a2012-01-12 17:19:34 -0800543 /*
544 * In the async case, migrate_page_move_mapping locked the buffers
545 * with an IRQ-safe spinlock held. In the sync case, the buffers
546 * need to be locked now
547 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800548 if (mode != MIGRATE_ASYNC)
549 BUG_ON(!buffer_migrate_lock_buffers(head, mode));
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700550
551 ClearPagePrivate(page);
552 set_page_private(newpage, page_private(page));
553 set_page_private(page, 0);
554 put_page(page);
555 get_page(newpage);
556
557 bh = head;
558 do {
559 set_bh_page(bh, newpage, bh_offset(bh));
560 bh = bh->b_this_page;
561
562 } while (bh != head);
563
564 SetPagePrivate(newpage);
565
566 migrate_page_copy(newpage, page);
567
568 bh = head;
569 do {
570 unlock_buffer(bh);
571 put_bh(bh);
572 bh = bh->b_this_page;
573
574 } while (bh != head);
575
576 return 0;
577}
578EXPORT_SYMBOL(buffer_migrate_page);
David Howells93614012006-09-30 20:45:40 +0200579#endif
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700580
Christoph Lameter04e62a22006-06-23 02:03:38 -0700581/*
582 * Writeback a page to clean the dirty state
583 */
584static int writeout(struct address_space *mapping, struct page *page)
585{
586 struct writeback_control wbc = {
587 .sync_mode = WB_SYNC_NONE,
588 .nr_to_write = 1,
589 .range_start = 0,
590 .range_end = LLONG_MAX,
Christoph Lameter04e62a22006-06-23 02:03:38 -0700591 .for_reclaim = 1
592 };
593 int rc;
594
595 if (!mapping->a_ops->writepage)
596 /* No write method for the address space */
597 return -EINVAL;
598
599 if (!clear_page_dirty_for_io(page))
600 /* Someone else already triggered a write */
601 return -EAGAIN;
602
603 /*
604 * A dirty page may imply that the underlying filesystem has
605 * the page on some queue. So the page must be clean for
606 * migration. Writeout may mean we loose the lock and the
607 * page state is no longer what we checked for earlier.
608 * At this point we know that the migration attempt cannot
609 * be successful.
610 */
611 remove_migration_ptes(page, page);
612
613 rc = mapping->a_ops->writepage(page, &wbc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700614
615 if (rc != AOP_WRITEPAGE_ACTIVATE)
616 /* unlocked. Relock */
617 lock_page(page);
618
Hugh Dickinsbda85502008-11-19 15:36:36 -0800619 return (rc < 0) ? -EIO : -EAGAIN;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700620}
621
622/*
623 * Default handling if a filesystem does not provide a migration function.
624 */
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700625static int fallback_migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800626 struct page *newpage, struct page *page, enum migrate_mode mode)
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700627{
Mel Gormanb969c4a2012-01-12 17:19:34 -0800628 if (PageDirty(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800629 /* Only writeback pages in full synchronous migration */
630 if (mode != MIGRATE_SYNC)
Mel Gormanb969c4a2012-01-12 17:19:34 -0800631 return -EBUSY;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700632 return writeout(mapping, page);
Mel Gormanb969c4a2012-01-12 17:19:34 -0800633 }
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700634
635 /*
636 * Buffers may be managed in a filesystem specific way.
637 * We must have no buffers or drop them.
638 */
David Howells266cf652009-04-03 16:42:36 +0100639 if (page_has_private(page) &&
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700640 !try_to_release_page(page, GFP_KERNEL))
641 return -EAGAIN;
642
Mel Gormana6bc32b2012-01-12 17:19:43 -0800643 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700644}
645
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700646/*
Christoph Lametere24f0b82006-06-23 02:03:51 -0700647 * Move a page to a newly allocated page
648 * The page is locked and all ptes have been successfully removed.
649 *
650 * The new page will have replaced the old page if this function
651 * is successful.
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700652 *
653 * Return value:
654 * < 0 - error code
655 * == 0 - success
Christoph Lametere24f0b82006-06-23 02:03:51 -0700656 */
Mel Gorman3fe20112010-05-24 14:32:20 -0700657static int move_to_new_page(struct page *newpage, struct page *page,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800658 int remap_swapcache, enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700659{
660 struct address_space *mapping;
661 int rc;
662
663 /*
664 * Block others from accessing the page when we get around to
665 * establishing additional references. We are the only one
666 * holding a reference to the new page at this point.
667 */
Nick Piggin529ae9a2008-08-02 12:01:03 +0200668 if (!trylock_page(newpage))
Christoph Lametere24f0b82006-06-23 02:03:51 -0700669 BUG();
670
671 /* Prepare mapping for the new page.*/
672 newpage->index = page->index;
673 newpage->mapping = page->mapping;
Rik van Rielb2e18532008-10-18 20:26:30 -0700674 if (PageSwapBacked(page))
675 SetPageSwapBacked(newpage);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700676
677 mapping = page_mapping(page);
678 if (!mapping)
Mel Gormana6bc32b2012-01-12 17:19:43 -0800679 rc = migrate_page(mapping, newpage, page, mode);
Mel Gormanb969c4a2012-01-12 17:19:34 -0800680 else if (mapping->a_ops->migratepage)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700681 /*
Mel Gormanb969c4a2012-01-12 17:19:34 -0800682 * Most pages have a mapping and most filesystems provide a
683 * migratepage callback. Anonymous pages are part of swap
684 * space which also has its own migratepage callback. This
685 * is the most common path for page migration.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700686 */
Mel Gormanb969c4a2012-01-12 17:19:34 -0800687 rc = mapping->a_ops->migratepage(mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800688 newpage, page, mode);
Mel Gormanb969c4a2012-01-12 17:19:34 -0800689 else
Mel Gormana6bc32b2012-01-12 17:19:43 -0800690 rc = fallback_migrate_page(mapping, newpage, page, mode);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700691
Mel Gorman3fe20112010-05-24 14:32:20 -0700692 if (rc) {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700693 newpage->mapping = NULL;
Mel Gorman3fe20112010-05-24 14:32:20 -0700694 } else {
695 if (remap_swapcache)
696 remove_migration_ptes(page, newpage);
Konstantin Khlebnikov35512ec2012-02-03 15:37:13 -0800697 page->mapping = NULL;
Mel Gorman3fe20112010-05-24 14:32:20 -0700698 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700699
700 unlock_page(newpage);
701
702 return rc;
703}
704
Minchan Kim0dabec92011-10-31 17:06:57 -0700705static int __unmap_and_move(struct page *page, struct page *newpage,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800706 int force, bool offlining, enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700707{
Minchan Kim0dabec92011-10-31 17:06:57 -0700708 int rc = -EAGAIN;
Mel Gorman3fe20112010-05-24 14:32:20 -0700709 int remap_swapcache = 1;
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800710 int charge = 0;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -0700711 struct mem_cgroup *mem;
Mel Gorman3f6c8272010-05-24 14:32:17 -0700712 struct anon_vma *anon_vma = NULL;
Christoph Lameter95a402c2006-06-23 02:03:53 -0700713
Nick Piggin529ae9a2008-08-02 12:01:03 +0200714 if (!trylock_page(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800715 if (!force || mode == MIGRATE_ASYNC)
Minchan Kim0dabec92011-10-31 17:06:57 -0700716 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -0800717
718 /*
719 * It's not safe for direct compaction to call lock_page.
720 * For example, during page readahead pages are added locked
721 * to the LRU. Later, when the IO completes the pages are
722 * marked uptodate and unlocked. However, the queueing
723 * could be merging multiple pages for one bio (e.g.
724 * mpage_readpages). If an allocation happens for the
725 * second or third page, the process can end up locking
726 * the same page twice and deadlocking. Rather than
727 * trying to be clever about what pages can be locked,
728 * avoid the use of lock_page for direct compaction
729 * altogether.
730 */
731 if (current->flags & PF_MEMALLOC)
Minchan Kim0dabec92011-10-31 17:06:57 -0700732 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -0800733
Christoph Lametere24f0b82006-06-23 02:03:51 -0700734 lock_page(page);
735 }
736
Hugh Dickins62b61f62009-12-14 17:59:33 -0800737 /*
738 * Only memory hotplug's offline_pages() caller has locked out KSM,
739 * and can safely migrate a KSM page. The other cases have skipped
740 * PageKsm along with PageReserved - but it is only now when we have
741 * the page lock that we can be certain it will not go KSM beneath us
742 * (KSM will not upgrade a page from PageAnon to PageKsm when it sees
743 * its pagecount raised, but only here do we take the page lock which
744 * serializes that).
745 */
746 if (PageKsm(page) && !offlining) {
747 rc = -EBUSY;
748 goto unlock;
749 }
750
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800751 /* charge against new page */
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700752 charge = mem_cgroup_prepare_migration(page, newpage, &mem, GFP_KERNEL);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800753 if (charge == -ENOMEM) {
754 rc = -ENOMEM;
755 goto unlock;
756 }
757 BUG_ON(charge);
758
Christoph Lametere24f0b82006-06-23 02:03:51 -0700759 if (PageWriteback(page)) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700760 /*
Mel Gormana6bc32b2012-01-12 17:19:43 -0800761 * Only in the case of a full syncronous migration is it
762 * necessary to wait for PageWriteback. In the async case,
763 * the retry loop is too short and in the sync-light case,
764 * the overhead of stalling is too much
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700765 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800766 if (mode != MIGRATE_SYNC) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700767 rc = -EBUSY;
768 goto uncharge;
769 }
770 if (!force)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800771 goto uncharge;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700772 wait_on_page_writeback(page);
773 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700774 /*
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700775 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
776 * we cannot notice that anon_vma is freed while we migrates a page.
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800777 * This get_anon_vma() delays freeing anon_vma pointer until the end
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700778 * of migration. File cache pages are no problem because of page_lock()
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700779 * File Caches may use write_page() or lock_page() in migration, then,
780 * just care Anon page here.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700781 */
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700782 if (PageAnon(page)) {
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800783 /*
784 * Only page_lock_anon_vma() understands the subtleties of
785 * getting a hold on an anon_vma from outside one of its mms.
786 */
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700787 anon_vma = page_get_anon_vma(page);
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800788 if (anon_vma) {
789 /*
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700790 * Anon page
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800791 */
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800792 } else if (PageSwapCache(page)) {
Mel Gorman3fe20112010-05-24 14:32:20 -0700793 /*
794 * We cannot be sure that the anon_vma of an unmapped
795 * swapcache page is safe to use because we don't
796 * know in advance if the VMA that this page belonged
797 * to still exists. If the VMA and others sharing the
798 * data have been freed, then the anon_vma could
799 * already be invalid.
800 *
801 * To avoid this possibility, swapcache pages get
802 * migrated but are not remapped when migration
803 * completes
804 */
805 remap_swapcache = 0;
806 } else {
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800807 goto uncharge;
Mel Gorman3fe20112010-05-24 14:32:20 -0700808 }
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700809 }
Shaohua Li62e1c552008-02-04 22:29:33 -0800810
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700811 /*
Shaohua Li62e1c552008-02-04 22:29:33 -0800812 * Corner case handling:
813 * 1. When a new swap-cache page is read into, it is added to the LRU
814 * and treated as swapcache but it has no rmap yet.
815 * Calling try_to_unmap() against a page->mapping==NULL page will
816 * trigger a BUG. So handle it here.
817 * 2. An orphaned page (see truncate_complete_page) might have
818 * fs-private metadata. The page can be picked up due to memory
819 * offlining. Everywhere else except page reclaim, the page is
820 * invisible to the vm, so the page can not be migrated. So try to
821 * free the metadata, so the page can be freed.
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700822 */
Shaohua Li62e1c552008-02-04 22:29:33 -0800823 if (!page->mapping) {
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800824 VM_BUG_ON(PageAnon(page));
825 if (page_has_private(page)) {
Shaohua Li62e1c552008-02-04 22:29:33 -0800826 try_to_free_buffers(page);
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800827 goto uncharge;
Shaohua Li62e1c552008-02-04 22:29:33 -0800828 }
Shaohua Liabfc3482009-09-21 17:01:19 -0700829 goto skip_unmap;
Shaohua Li62e1c552008-02-04 22:29:33 -0800830 }
831
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700832 /* Establish migration ptes or remove ptes */
Andi Kleen14fa31b2009-09-16 11:50:10 +0200833 try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700834
Shaohua Liabfc3482009-09-21 17:01:19 -0700835skip_unmap:
Christoph Lametere6a15302006-06-25 05:46:49 -0700836 if (!page_mapped(page))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800837 rc = move_to_new_page(newpage, page, remap_swapcache, mode);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700838
Mel Gorman3fe20112010-05-24 14:32:20 -0700839 if (rc && remap_swapcache)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700840 remove_migration_ptes(page, page);
Mel Gorman3f6c8272010-05-24 14:32:17 -0700841
842 /* Drop an anon_vma reference if we took one */
Rik van Riel76545062010-08-09 17:18:41 -0700843 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -0700844 put_anon_vma(anon_vma);
Mel Gorman3f6c8272010-05-24 14:32:17 -0700845
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800846uncharge:
847 if (!charge)
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -0800848 mem_cgroup_end_migration(mem, page, newpage, rc == 0);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700849unlock:
850 unlock_page(page);
Minchan Kim0dabec92011-10-31 17:06:57 -0700851out:
852 return rc;
853}
Christoph Lameter95a402c2006-06-23 02:03:53 -0700854
Minchan Kim0dabec92011-10-31 17:06:57 -0700855/*
856 * Obtain the lock on page, remove all ptes and migrate the page
857 * to the newly allocated page in newpage.
858 */
859static int unmap_and_move(new_page_t get_new_page, unsigned long private,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800860 struct page *page, int force, bool offlining,
861 enum migrate_mode mode)
Minchan Kim0dabec92011-10-31 17:06:57 -0700862{
863 int rc = 0;
864 int *result = NULL;
865 struct page *newpage = get_new_page(page, private, &result);
866
867 if (!newpage)
868 return -ENOMEM;
869
870 if (page_count(page) == 1) {
871 /* page was freed from under us. So we are done. */
872 goto out;
873 }
874
875 if (unlikely(PageTransHuge(page)))
876 if (unlikely(split_huge_page(page)))
877 goto out;
878
Mel Gormana6bc32b2012-01-12 17:19:43 -0800879 rc = __unmap_and_move(page, newpage, force, offlining, mode);
Minchan Kim0dabec92011-10-31 17:06:57 -0700880out:
Christoph Lametere24f0b82006-06-23 02:03:51 -0700881 if (rc != -EAGAIN) {
Minchan Kim0dabec92011-10-31 17:06:57 -0700882 /*
883 * A page that has been migrated has all references
884 * removed and will be freed. A page that has not been
885 * migrated will have kepts its references and be
886 * restored.
887 */
888 list_del(&page->lru);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -0700889 dec_zone_page_state(page, NR_ISOLATED_ANON +
Johannes Weiner6c0b1352009-09-21 17:02:59 -0700890 page_is_file_cache(page));
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700891 putback_lru_page(page);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700892 }
Christoph Lameter95a402c2006-06-23 02:03:53 -0700893 /*
894 * Move the new page to the LRU. If migration was not successful
895 * then this will free the page.
896 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700897 putback_lru_page(newpage);
Christoph Lameter742755a2006-06-23 02:03:55 -0700898 if (result) {
899 if (rc)
900 *result = rc;
901 else
902 *result = page_to_nid(newpage);
903 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700904 return rc;
905}
906
907/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900908 * Counterpart of unmap_and_move_page() for hugepage migration.
909 *
910 * This function doesn't wait the completion of hugepage I/O
911 * because there is no race between I/O and migration for hugepage.
912 * Note that currently hugepage I/O occurs only in direct I/O
913 * where no lock is held and PG_writeback is irrelevant,
914 * and writeback status of all subpages are counted in the reference
915 * count of the head page (i.e. if all subpages of a 2MB hugepage are
916 * under direct I/O, the reference of the head page is 512 and a bit more.)
917 * This means that when we try to migrate hugepage whose subpages are
918 * doing direct I/O, some references remain after try_to_unmap() and
919 * hugepage migration fails without data corruption.
920 *
921 * There is also no race when direct I/O is issued on the page under migration,
922 * because then pte is replaced with migration swap entry and direct I/O code
923 * will wait in the page fault for migration to complete.
924 */
925static int unmap_and_move_huge_page(new_page_t get_new_page,
926 unsigned long private, struct page *hpage,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800927 int force, bool offlining,
928 enum migrate_mode mode)
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900929{
930 int rc = 0;
931 int *result = NULL;
932 struct page *new_hpage = get_new_page(hpage, private, &result);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900933 struct anon_vma *anon_vma = NULL;
934
935 if (!new_hpage)
936 return -ENOMEM;
937
938 rc = -EAGAIN;
939
940 if (!trylock_page(hpage)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800941 if (!force || mode != MIGRATE_SYNC)
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900942 goto out;
943 lock_page(hpage);
944 }
945
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700946 if (PageAnon(hpage))
947 anon_vma = page_get_anon_vma(hpage);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900948
949 try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
950
951 if (!page_mapped(hpage))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800952 rc = move_to_new_page(new_hpage, hpage, 1, mode);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900953
954 if (rc)
955 remove_migration_ptes(hpage, hpage);
956
Hugh Dickinsfd4a4662011-01-13 15:47:31 -0800957 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -0700958 put_anon_vma(anon_vma);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900959 unlock_page(hpage);
960
Hillf Danton09761332011-12-08 14:34:20 -0800961out:
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900962 if (rc != -EAGAIN) {
963 list_del(&hpage->lru);
964 put_page(hpage);
965 }
966
967 put_page(new_hpage);
968
969 if (result) {
970 if (rc)
971 *result = rc;
972 else
973 *result = page_to_nid(new_hpage);
974 }
975 return rc;
976}
977
978/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800979 * migrate_pages
980 *
Christoph Lameter95a402c2006-06-23 02:03:53 -0700981 * The function takes one list of pages to migrate and a function
982 * that determines from the page to be migrated and the private data
983 * the target of the move and allocates the page.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800984 *
985 * The function returns after 10 attempts or if no pages
986 * are movable anymore because to has become empty
Minchan Kimcf608ac2010-10-26 14:21:29 -0700987 * or no retryable pages exist anymore.
988 * Caller should call putback_lru_pages to return pages to the LRU
Minchan Kim28bd6572011-01-25 15:07:26 -0800989 * or free list only if ret != 0.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800990 *
Christoph Lameter95a402c2006-06-23 02:03:53 -0700991 * Return: Number of pages not migrated or error code.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800992 */
Christoph Lameter95a402c2006-06-23 02:03:53 -0700993int migrate_pages(struct list_head *from,
Mel Gorman7f0f2492011-01-13 15:45:58 -0800994 new_page_t get_new_page, unsigned long private, bool offlining,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800995 enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800996{
Christoph Lametere24f0b82006-06-23 02:03:51 -0700997 int retry = 1;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800998 int nr_failed = 0;
Mel Gormancd9bb9b2012-10-19 10:46:20 +0100999 int nr_succeeded = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001000 int pass = 0;
1001 struct page *page;
1002 struct page *page2;
1003 int swapwrite = current->flags & PF_SWAPWRITE;
1004 int rc;
1005
Liam Markcc2d4bd2013-01-16 10:14:40 -08001006 trace_migrate_pages_start(mode);
Christoph Lameterb20a3502006-03-22 00:09:12 -08001007 if (!swapwrite)
1008 current->flags |= PF_SWAPWRITE;
1009
Christoph Lametere24f0b82006-06-23 02:03:51 -07001010 for(pass = 0; pass < 10 && retry; pass++) {
1011 retry = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001012
Christoph Lametere24f0b82006-06-23 02:03:51 -07001013 list_for_each_entry_safe(page, page2, from, lru) {
Christoph Lametere24f0b82006-06-23 02:03:51 -07001014 cond_resched();
Christoph Lameterb20a3502006-03-22 00:09:12 -08001015
Christoph Lameter95a402c2006-06-23 02:03:53 -07001016 rc = unmap_and_move(get_new_page, private,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001017 page, pass > 2, offlining,
Mel Gormana6bc32b2012-01-12 17:19:43 -08001018 mode);
Christoph Lameterb20a3502006-03-22 00:09:12 -08001019
Christoph Lametere24f0b82006-06-23 02:03:51 -07001020 switch(rc) {
Christoph Lameter95a402c2006-06-23 02:03:53 -07001021 case -ENOMEM:
1022 goto out;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001023 case -EAGAIN:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001024 retry++;
Liam Markcc2d4bd2013-01-16 10:14:40 -08001025 trace_migrate_retry(retry);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001026 break;
1027 case 0:
Mel Gormancd9bb9b2012-10-19 10:46:20 +01001028 nr_succeeded++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001029 break;
1030 default:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001031 /* Permanent failure */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001032 nr_failed++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001033 break;
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001034 }
Christoph Lameterb20a3502006-03-22 00:09:12 -08001035 }
1036 }
Christoph Lameter95a402c2006-06-23 02:03:53 -07001037 rc = 0;
1038out:
Mel Gormancd9bb9b2012-10-19 10:46:20 +01001039 if (nr_succeeded)
1040 count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
1041 if (nr_failed)
1042 count_vm_events(PGMIGRATE_FAIL, nr_failed);
Christoph Lameterb20a3502006-03-22 00:09:12 -08001043 if (!swapwrite)
1044 current->flags &= ~PF_SWAPWRITE;
1045
Liam Markcc2d4bd2013-01-16 10:14:40 -08001046 trace_migrate_pages_end(mode);
Christoph Lameter95a402c2006-06-23 02:03:53 -07001047 if (rc)
1048 return rc;
1049
Christoph Lameterb20a3502006-03-22 00:09:12 -08001050 return nr_failed + retry;
1051}
1052
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001053int migrate_huge_pages(struct list_head *from,
Mel Gorman7f0f2492011-01-13 15:45:58 -08001054 new_page_t get_new_page, unsigned long private, bool offlining,
Mel Gormana6bc32b2012-01-12 17:19:43 -08001055 enum migrate_mode mode)
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001056{
1057 int retry = 1;
1058 int nr_failed = 0;
1059 int pass = 0;
1060 struct page *page;
1061 struct page *page2;
1062 int rc;
1063
1064 for (pass = 0; pass < 10 && retry; pass++) {
1065 retry = 0;
1066
1067 list_for_each_entry_safe(page, page2, from, lru) {
1068 cond_resched();
1069
1070 rc = unmap_and_move_huge_page(get_new_page,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001071 private, page, pass > 2, offlining,
Mel Gormana6bc32b2012-01-12 17:19:43 -08001072 mode);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001073
1074 switch(rc) {
1075 case -ENOMEM:
1076 goto out;
1077 case -EAGAIN:
1078 retry++;
1079 break;
1080 case 0:
1081 break;
1082 default:
1083 /* Permanent failure */
1084 nr_failed++;
1085 break;
1086 }
1087 }
1088 }
1089 rc = 0;
1090out:
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001091 if (rc)
1092 return rc;
1093
1094 return nr_failed + retry;
1095}
1096
Christoph Lameter742755a2006-06-23 02:03:55 -07001097#ifdef CONFIG_NUMA
1098/*
1099 * Move a list of individual pages
1100 */
1101struct page_to_node {
1102 unsigned long addr;
1103 struct page *page;
1104 int node;
1105 int status;
1106};
1107
1108static struct page *new_page_node(struct page *p, unsigned long private,
1109 int **result)
1110{
1111 struct page_to_node *pm = (struct page_to_node *)private;
1112
1113 while (pm->node != MAX_NUMNODES && pm->page != p)
1114 pm++;
1115
1116 if (pm->node == MAX_NUMNODES)
1117 return NULL;
1118
1119 *result = &pm->status;
1120
Mel Gorman6484eb32009-06-16 15:31:54 -07001121 return alloc_pages_exact_node(pm->node,
Mel Gorman769848c2007-07-17 04:03:05 -07001122 GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
Christoph Lameter742755a2006-06-23 02:03:55 -07001123}
1124
1125/*
1126 * Move a set of pages as indicated in the pm array. The addr
1127 * field must be set to the virtual address of the page to be moved
1128 * and the node number must contain a valid target node.
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001129 * The pm array ends with node = MAX_NUMNODES.
Christoph Lameter742755a2006-06-23 02:03:55 -07001130 */
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001131static int do_move_page_to_node_array(struct mm_struct *mm,
1132 struct page_to_node *pm,
1133 int migrate_all)
Christoph Lameter742755a2006-06-23 02:03:55 -07001134{
1135 int err;
1136 struct page_to_node *pp;
1137 LIST_HEAD(pagelist);
1138
1139 down_read(&mm->mmap_sem);
1140
1141 /*
1142 * Build a list of pages to migrate
1143 */
Christoph Lameter742755a2006-06-23 02:03:55 -07001144 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
1145 struct vm_area_struct *vma;
1146 struct page *page;
1147
Christoph Lameter742755a2006-06-23 02:03:55 -07001148 err = -EFAULT;
1149 vma = find_vma(mm, pp->addr);
Gleb Natapov70384dc2010-10-26 14:22:07 -07001150 if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
Christoph Lameter742755a2006-06-23 02:03:55 -07001151 goto set_status;
1152
Andrea Arcangeli500d65d2011-01-13 15:46:55 -08001153 page = follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001154
1155 err = PTR_ERR(page);
1156 if (IS_ERR(page))
1157 goto set_status;
1158
Christoph Lameter742755a2006-06-23 02:03:55 -07001159 err = -ENOENT;
1160 if (!page)
1161 goto set_status;
1162
Hugh Dickins62b61f62009-12-14 17:59:33 -08001163 /* Use PageReserved to check for zero page */
1164 if (PageReserved(page) || PageKsm(page))
Christoph Lameter742755a2006-06-23 02:03:55 -07001165 goto put_and_set;
1166
1167 pp->page = page;
1168 err = page_to_nid(page);
1169
1170 if (err == pp->node)
1171 /*
1172 * Node already in the right place
1173 */
1174 goto put_and_set;
1175
1176 err = -EACCES;
1177 if (page_mapcount(page) > 1 &&
1178 !migrate_all)
1179 goto put_and_set;
1180
Nick Piggin62695a82008-10-18 20:26:09 -07001181 err = isolate_lru_page(page);
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001182 if (!err) {
Nick Piggin62695a82008-10-18 20:26:09 -07001183 list_add_tail(&page->lru, &pagelist);
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001184 inc_zone_page_state(page, NR_ISOLATED_ANON +
1185 page_is_file_cache(page));
1186 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001187put_and_set:
1188 /*
1189 * Either remove the duplicate refcount from
1190 * isolate_lru_page() or drop the page ref if it was
1191 * not isolated.
1192 */
1193 put_page(page);
1194set_status:
1195 pp->status = err;
1196 }
1197
Brice Gogline78bbfa2008-10-18 20:27:15 -07001198 err = 0;
Minchan Kimcf608ac2010-10-26 14:21:29 -07001199 if (!list_empty(&pagelist)) {
Christoph Lameter742755a2006-06-23 02:03:55 -07001200 err = migrate_pages(&pagelist, new_page_node,
Mel Gormana6bc32b2012-01-12 17:19:43 -08001201 (unsigned long)pm, 0, MIGRATE_SYNC);
Minchan Kimcf608ac2010-10-26 14:21:29 -07001202 if (err)
1203 putback_lru_pages(&pagelist);
1204 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001205
1206 up_read(&mm->mmap_sem);
1207 return err;
1208}
1209
1210/*
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001211 * Migrate an array of page address onto an array of nodes and fill
1212 * the corresponding array of status.
1213 */
Christoph Lameter3268c632012-03-21 16:34:06 -07001214static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001215 unsigned long nr_pages,
1216 const void __user * __user *pages,
1217 const int __user *nodes,
1218 int __user *status, int flags)
1219{
Brice Goglin3140a222009-01-06 14:38:57 -08001220 struct page_to_node *pm;
Brice Goglin3140a222009-01-06 14:38:57 -08001221 unsigned long chunk_nr_pages;
1222 unsigned long chunk_start;
1223 int err;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001224
Brice Goglin3140a222009-01-06 14:38:57 -08001225 err = -ENOMEM;
1226 pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
1227 if (!pm)
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001228 goto out;
Brice Goglin35282a22009-06-16 15:32:43 -07001229
1230 migrate_prep();
1231
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001232 /*
Brice Goglin3140a222009-01-06 14:38:57 -08001233 * Store a chunk of page_to_node array in a page,
1234 * but keep the last one as a marker
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001235 */
Brice Goglin3140a222009-01-06 14:38:57 -08001236 chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001237
Brice Goglin3140a222009-01-06 14:38:57 -08001238 for (chunk_start = 0;
1239 chunk_start < nr_pages;
1240 chunk_start += chunk_nr_pages) {
1241 int j;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001242
Brice Goglin3140a222009-01-06 14:38:57 -08001243 if (chunk_start + chunk_nr_pages > nr_pages)
1244 chunk_nr_pages = nr_pages - chunk_start;
1245
1246 /* fill the chunk pm with addrs and nodes from user-space */
1247 for (j = 0; j < chunk_nr_pages; j++) {
1248 const void __user *p;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001249 int node;
1250
Brice Goglin3140a222009-01-06 14:38:57 -08001251 err = -EFAULT;
1252 if (get_user(p, pages + j + chunk_start))
1253 goto out_pm;
1254 pm[j].addr = (unsigned long) p;
1255
1256 if (get_user(node, nodes + j + chunk_start))
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001257 goto out_pm;
1258
1259 err = -ENODEV;
Linus Torvalds6f5a55f2010-02-05 16:16:50 -08001260 if (node < 0 || node >= MAX_NUMNODES)
1261 goto out_pm;
1262
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001263 if (!node_state(node, N_HIGH_MEMORY))
1264 goto out_pm;
1265
1266 err = -EACCES;
1267 if (!node_isset(node, task_nodes))
1268 goto out_pm;
1269
Brice Goglin3140a222009-01-06 14:38:57 -08001270 pm[j].node = node;
1271 }
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001272
Brice Goglin3140a222009-01-06 14:38:57 -08001273 /* End marker for this chunk */
1274 pm[chunk_nr_pages].node = MAX_NUMNODES;
1275
1276 /* Migrate this chunk */
1277 err = do_move_page_to_node_array(mm, pm,
1278 flags & MPOL_MF_MOVE_ALL);
1279 if (err < 0)
1280 goto out_pm;
1281
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001282 /* Return status information */
Brice Goglin3140a222009-01-06 14:38:57 -08001283 for (j = 0; j < chunk_nr_pages; j++)
1284 if (put_user(pm[j].status, status + j + chunk_start)) {
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001285 err = -EFAULT;
Brice Goglin3140a222009-01-06 14:38:57 -08001286 goto out_pm;
1287 }
1288 }
1289 err = 0;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001290
1291out_pm:
Brice Goglin3140a222009-01-06 14:38:57 -08001292 free_page((unsigned long)pm);
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001293out:
1294 return err;
1295}
1296
1297/*
Brice Goglin2f007e72008-10-18 20:27:16 -07001298 * Determine the nodes of an array of pages and store it in an array of status.
Christoph Lameter742755a2006-06-23 02:03:55 -07001299 */
Brice Goglin80bba122008-12-09 13:14:23 -08001300static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
1301 const void __user **pages, int *status)
Christoph Lameter742755a2006-06-23 02:03:55 -07001302{
Brice Goglin2f007e72008-10-18 20:27:16 -07001303 unsigned long i;
Brice Goglin2f007e72008-10-18 20:27:16 -07001304
Christoph Lameter742755a2006-06-23 02:03:55 -07001305 down_read(&mm->mmap_sem);
1306
Brice Goglin2f007e72008-10-18 20:27:16 -07001307 for (i = 0; i < nr_pages; i++) {
Brice Goglin80bba122008-12-09 13:14:23 -08001308 unsigned long addr = (unsigned long)(*pages);
Christoph Lameter742755a2006-06-23 02:03:55 -07001309 struct vm_area_struct *vma;
1310 struct page *page;
KOSAKI Motohiroc095adb2008-12-16 16:06:43 +09001311 int err = -EFAULT;
Brice Goglin2f007e72008-10-18 20:27:16 -07001312
1313 vma = find_vma(mm, addr);
Gleb Natapov70384dc2010-10-26 14:22:07 -07001314 if (!vma || addr < vma->vm_start)
Christoph Lameter742755a2006-06-23 02:03:55 -07001315 goto set_status;
1316
Brice Goglin2f007e72008-10-18 20:27:16 -07001317 page = follow_page(vma, addr, 0);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001318
1319 err = PTR_ERR(page);
1320 if (IS_ERR(page))
1321 goto set_status;
1322
Christoph Lameter742755a2006-06-23 02:03:55 -07001323 err = -ENOENT;
1324 /* Use PageReserved to check for zero page */
Hugh Dickins62b61f62009-12-14 17:59:33 -08001325 if (!page || PageReserved(page) || PageKsm(page))
Christoph Lameter742755a2006-06-23 02:03:55 -07001326 goto set_status;
1327
1328 err = page_to_nid(page);
1329set_status:
Brice Goglin80bba122008-12-09 13:14:23 -08001330 *status = err;
1331
1332 pages++;
1333 status++;
1334 }
1335
1336 up_read(&mm->mmap_sem);
1337}
1338
1339/*
1340 * Determine the nodes of a user array of pages and store it in
1341 * a user array of status.
1342 */
1343static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1344 const void __user * __user *pages,
1345 int __user *status)
1346{
1347#define DO_PAGES_STAT_CHUNK_NR 16
1348 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1349 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
Brice Goglin80bba122008-12-09 13:14:23 -08001350
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001351 while (nr_pages) {
1352 unsigned long chunk_nr;
Brice Goglin80bba122008-12-09 13:14:23 -08001353
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001354 chunk_nr = nr_pages;
1355 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1356 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1357
1358 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
1359 break;
Brice Goglin80bba122008-12-09 13:14:23 -08001360
1361 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1362
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001363 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1364 break;
Christoph Lameter742755a2006-06-23 02:03:55 -07001365
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001366 pages += chunk_nr;
1367 status += chunk_nr;
1368 nr_pages -= chunk_nr;
1369 }
1370 return nr_pages ? -EFAULT : 0;
Christoph Lameter742755a2006-06-23 02:03:55 -07001371}
1372
1373/*
1374 * Move a list of pages in the address space of the currently executing
1375 * process.
1376 */
Heiko Carstens938bb9f2009-01-14 14:14:30 +01001377SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1378 const void __user * __user *, pages,
1379 const int __user *, nodes,
1380 int __user *, status, int, flags)
Christoph Lameter742755a2006-06-23 02:03:55 -07001381{
Christoph Lameter742755a2006-06-23 02:03:55 -07001382 struct task_struct *task;
Christoph Lameter742755a2006-06-23 02:03:55 -07001383 struct mm_struct *mm;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001384 int err;
Christoph Lameter3268c632012-03-21 16:34:06 -07001385 nodemask_t task_nodes;
Christoph Lameter742755a2006-06-23 02:03:55 -07001386
1387 /* Check flags */
1388 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1389 return -EINVAL;
1390
1391 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1392 return -EPERM;
1393
1394 /* Find the mm_struct */
Greg Thelena879bf52011-02-25 14:44:13 -08001395 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07001396 task = pid ? find_task_by_vpid(pid) : current;
Christoph Lameter742755a2006-06-23 02:03:55 -07001397 if (!task) {
Greg Thelena879bf52011-02-25 14:44:13 -08001398 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001399 return -ESRCH;
1400 }
Christoph Lameter3268c632012-03-21 16:34:06 -07001401 get_task_struct(task);
Christoph Lameter742755a2006-06-23 02:03:55 -07001402
1403 /*
1404 * Check if this process has the right to modify the specified
Linus Torvalds4c497362017-08-20 13:26:27 -07001405 * process. Use the regular "ptrace_may_access()" checks.
Christoph Lameter742755a2006-06-23 02:03:55 -07001406 */
Linus Torvalds4c497362017-08-20 13:26:27 -07001407 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001408 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001409 err = -EPERM;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001410 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001411 }
David Howellsc69e8d92008-11-14 10:39:19 +11001412 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001413
David Quigley86c3a762006-06-23 02:04:02 -07001414 err = security_task_movememory(task);
1415 if (err)
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001416 goto out;
David Quigley86c3a762006-06-23 02:04:02 -07001417
Christoph Lameter3268c632012-03-21 16:34:06 -07001418 task_nodes = cpuset_mems_allowed(task);
1419 mm = get_task_mm(task);
1420 put_task_struct(task);
1421
Sasha Levin6e8b09e2012-04-25 16:01:53 -07001422 if (!mm)
1423 return -EINVAL;
1424
1425 if (nodes)
1426 err = do_pages_move(mm, task_nodes, nr_pages, pages,
1427 nodes, status, flags);
1428 else
1429 err = do_pages_stat(mm, nr_pages, pages, status);
Christoph Lameter3268c632012-03-21 16:34:06 -07001430
1431 mmput(mm);
1432 return err;
David Quigley86c3a762006-06-23 02:04:02 -07001433
Christoph Lameter742755a2006-06-23 02:03:55 -07001434out:
Christoph Lameter3268c632012-03-21 16:34:06 -07001435 put_task_struct(task);
Christoph Lameter742755a2006-06-23 02:03:55 -07001436 return err;
1437}
Christoph Lameter742755a2006-06-23 02:03:55 -07001438
Christoph Lameter7b2259b2006-06-25 05:46:48 -07001439/*
1440 * Call migration functions in the vma_ops that may prepare
1441 * memory in a vm for migration. migration functions may perform
1442 * the migration for vmas that do not have an underlying page struct.
1443 */
1444int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
1445 const nodemask_t *from, unsigned long flags)
1446{
1447 struct vm_area_struct *vma;
1448 int err = 0;
1449
Daisuke Nishimura1001c9f2009-02-11 13:04:18 -08001450 for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
Christoph Lameter7b2259b2006-06-25 05:46:48 -07001451 if (vma->vm_ops && vma->vm_ops->migrate) {
1452 err = vma->vm_ops->migrate(vma, to, from, flags);
1453 if (err)
1454 break;
1455 }
1456 }
1457 return err;
1458}
Gerald Schaefer83d16742008-07-23 21:28:22 -07001459#endif