blob: 79778dd84cd4329ead1d73613db55094a31e286c [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
KAMEZAWA Hiroyuki97ee0522007-10-16 01:25:43 -0700150 flush_cache_page(vma, addr, pte_pfn(pte));
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 */
185void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
186 unsigned long address)
187{
188 pte_t *ptep, pte;
189 spinlock_t *ptl;
190 swp_entry_t entry;
191 struct page *page;
192
193 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
194 pte = *ptep;
195 if (!is_swap_pte(pte))
196 goto out;
197
198 entry = pte_to_swp_entry(pte);
199 if (!is_migration_entry(entry))
200 goto out;
201
202 page = migration_entry_to_page(entry);
203
Nick Piggine2867812008-07-25 19:45:30 -0700204 /*
205 * Once radix-tree replacement of page migration started, page_count
206 * *must* be zero. And, we don't want to call wait_on_page_locked()
207 * against a page without get_page().
208 * So, we use get_page_unless_zero(), here. Even failed, page fault
209 * will occur again.
210 */
211 if (!get_page_unless_zero(page))
212 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700213 pte_unmap_unlock(ptep, ptl);
214 wait_on_page_locked(page);
215 put_page(page);
216 return;
217out:
218 pte_unmap_unlock(ptep, ptl);
219}
220
Mel Gormanb969c4a2012-01-12 17:19:34 -0800221#ifdef CONFIG_BLOCK
222/* Returns true if all buffers are successfully locked */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800223static bool buffer_migrate_lock_buffers(struct buffer_head *head,
224 enum migrate_mode mode)
Mel Gormanb969c4a2012-01-12 17:19:34 -0800225{
226 struct buffer_head *bh = head;
227
228 /* Simple case, sync compaction */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800229 if (mode != MIGRATE_ASYNC) {
Mel Gormanb969c4a2012-01-12 17:19:34 -0800230 do {
231 get_bh(bh);
232 lock_buffer(bh);
233 bh = bh->b_this_page;
234
235 } while (bh != head);
236
237 return true;
238 }
239
240 /* async case, we cannot block on lock_buffer so use trylock_buffer */
241 do {
242 get_bh(bh);
243 if (!trylock_buffer(bh)) {
244 /*
245 * We failed to lock the buffer and cannot stall in
246 * async migration. Release the taken locks
247 */
248 struct buffer_head *failed_bh = bh;
249 put_bh(failed_bh);
250 bh = head;
251 while (bh != failed_bh) {
252 unlock_buffer(bh);
253 put_bh(bh);
254 bh = bh->b_this_page;
255 }
256 return false;
257 }
258
259 bh = bh->b_this_page;
260 } while (bh != head);
261 return true;
262}
263#else
264static inline bool buffer_migrate_lock_buffers(struct buffer_head *head,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800265 enum migrate_mode mode)
Mel Gormanb969c4a2012-01-12 17:19:34 -0800266{
267 return true;
268}
269#endif /* CONFIG_BLOCK */
270
Christoph Lameterb20a3502006-03-22 00:09:12 -0800271/*
Christoph Lameterc3fcf8a2006-06-23 02:03:32 -0700272 * Replace the page in the mapping.
Christoph Lameter5b5c7122006-06-23 02:03:29 -0700273 *
274 * The number of remaining references must be:
275 * 1 for anonymous pages without a mapping
276 * 2 for pages with a mapping
David Howells266cf652009-04-03 16:42:36 +0100277 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800278 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700279static int migrate_page_move_mapping(struct address_space *mapping,
Mel Gormanb969c4a2012-01-12 17:19:34 -0800280 struct page *newpage, struct page *page,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800281 struct buffer_head *head, enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800282{
Hugh Dickinse468c872016-07-15 15:08:19 -0400283 struct zone *oldzone, *newzone;
284 int dirty;
Nick Piggine2867812008-07-25 19:45:30 -0700285 int expected_count;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800286 void **pslot;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800287
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700288 if (!mapping) {
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700289 /* Anonymous page without mapping */
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700290 if (page_count(page) != 1)
291 return -EAGAIN;
292 return 0;
293 }
294
Hugh Dickinse468c872016-07-15 15:08:19 -0400295 oldzone = page_zone(page);
296 newzone = page_zone(newpage);
297
Nick Piggin19fd6232008-07-25 19:45:32 -0700298 spin_lock_irq(&mapping->tree_lock);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800299
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800300 pslot = radix_tree_lookup_slot(&mapping->page_tree,
301 page_index(page));
Christoph Lameterb20a3502006-03-22 00:09:12 -0800302
Johannes Weineredcf4742009-09-21 17:02:59 -0700303 expected_count = 2 + page_has_private(page);
Nick Piggine2867812008-07-25 19:45:30 -0700304 if (page_count(page) != expected_count ||
Mel Gorman29c1f672011-01-13 15:47:21 -0800305 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700306 spin_unlock_irq(&mapping->tree_lock);
Christoph Lametere23ca002006-04-10 22:52:57 -0700307 return -EAGAIN;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800308 }
309
Nick Piggine2867812008-07-25 19:45:30 -0700310 if (!page_freeze_refs(page, expected_count)) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700311 spin_unlock_irq(&mapping->tree_lock);
Nick Piggine2867812008-07-25 19:45:30 -0700312 return -EAGAIN;
313 }
314
Christoph Lameterb20a3502006-03-22 00:09:12 -0800315 /*
Mel Gormanb969c4a2012-01-12 17:19:34 -0800316 * In the async migration case of moving a page with buffers, lock the
317 * buffers using trylock before the mapping is moved. If the mapping
318 * was moved, we later failed to lock the buffers and could not move
319 * the mapping back due to an elevated page count, we would have to
320 * block waiting on other references to be dropped.
321 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800322 if (mode == MIGRATE_ASYNC && head &&
323 !buffer_migrate_lock_buffers(head, mode)) {
Mel Gormanb969c4a2012-01-12 17:19:34 -0800324 page_unfreeze_refs(page, expected_count);
325 spin_unlock_irq(&mapping->tree_lock);
326 return -EAGAIN;
327 }
328
329 /*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800330 * Now we know that no one else is looking at the page.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800331 */
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800332 get_page(newpage); /* add cache reference */
Christoph Lameterb20a3502006-03-22 00:09:12 -0800333 if (PageSwapCache(page)) {
334 SetPageSwapCache(newpage);
335 set_page_private(newpage, page_private(page));
336 }
337
Hugh Dickinse468c872016-07-15 15:08:19 -0400338 /* Move dirty while page refs frozen and newpage not yet exposed */
339 dirty = PageDirty(page);
340 if (dirty) {
341 ClearPageDirty(page);
342 SetPageDirty(newpage);
343 }
344
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800345 radix_tree_replace_slot(pslot, newpage);
346
347 /*
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800348 * Drop cache reference from old page by unfreezing
349 * to one less reference.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800350 * We know this isn't the last reference.
351 */
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800352 page_unfreeze_refs(page, expected_count - 1);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800353
Hugh Dickinse468c872016-07-15 15:08:19 -0400354 spin_unlock(&mapping->tree_lock);
355 /* Leave irq disabled to prevent preemption while updating stats */
356
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700357 /*
358 * If moved to a different zone then also account
359 * the page for that zone. Other VM counters will be
360 * taken care of when we establish references to the
361 * new page and drop references to the old page.
362 *
363 * Note that anonymous pages are accounted for
364 * via NR_FILE_PAGES and NR_ANON_PAGES if they
365 * are mapped to swap space.
366 */
Hugh Dickinse468c872016-07-15 15:08:19 -0400367 if (newzone != oldzone) {
368 __dec_zone_state(oldzone, NR_FILE_PAGES);
369 __inc_zone_state(newzone, NR_FILE_PAGES);
370 if (PageSwapBacked(page) && !PageSwapCache(page)) {
371 __dec_zone_state(oldzone, NR_SHMEM);
372 __inc_zone_state(newzone, NR_SHMEM);
373 }
374 if (dirty && mapping_cap_account_dirty(mapping)) {
375 __dec_zone_state(oldzone, NR_FILE_DIRTY);
376 __inc_zone_state(newzone, NR_FILE_DIRTY);
377 }
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700378 }
Hugh Dickinse468c872016-07-15 15:08:19 -0400379 local_irq_enable();
Christoph Lameterb20a3502006-03-22 00:09:12 -0800380
381 return 0;
382}
Christoph Lameterb20a3502006-03-22 00:09:12 -0800383
384/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900385 * The expected number of remaining references is the same as that
386 * of migrate_page_move_mapping().
387 */
388int migrate_huge_page_move_mapping(struct address_space *mapping,
389 struct page *newpage, struct page *page)
390{
391 int expected_count;
392 void **pslot;
393
394 if (!mapping) {
395 if (page_count(page) != 1)
396 return -EAGAIN;
397 return 0;
398 }
399
400 spin_lock_irq(&mapping->tree_lock);
401
402 pslot = radix_tree_lookup_slot(&mapping->page_tree,
403 page_index(page));
404
405 expected_count = 2 + page_has_private(page);
406 if (page_count(page) != expected_count ||
Mel Gorman29c1f672011-01-13 15:47:21 -0800407 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900408 spin_unlock_irq(&mapping->tree_lock);
409 return -EAGAIN;
410 }
411
412 if (!page_freeze_refs(page, expected_count)) {
413 spin_unlock_irq(&mapping->tree_lock);
414 return -EAGAIN;
415 }
416
417 get_page(newpage);
418
419 radix_tree_replace_slot(pslot, newpage);
420
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800421 page_unfreeze_refs(page, expected_count - 1);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900422
423 spin_unlock_irq(&mapping->tree_lock);
424 return 0;
425}
426
427/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800428 * Copy the page to its new location
429 */
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900430void migrate_page_copy(struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800431{
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900432 if (PageHuge(page))
433 copy_huge_page(newpage, page);
434 else
435 copy_highpage(newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800436
437 if (PageError(page))
438 SetPageError(newpage);
439 if (PageReferenced(page))
440 SetPageReferenced(newpage);
441 if (PageUptodate(page))
442 SetPageUptodate(newpage);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700443 if (TestClearPageActive(page)) {
444 VM_BUG_ON(PageUnevictable(page));
Christoph Lameterb20a3502006-03-22 00:09:12 -0800445 SetPageActive(newpage);
Lee Schermerhorn418b27e2009-12-14 17:59:54 -0800446 } else if (TestClearPageUnevictable(page))
447 SetPageUnevictable(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800448 if (PageChecked(page))
449 SetPageChecked(newpage);
450 if (PageMappedToDisk(page))
451 SetPageMappedToDisk(newpage);
452
Hugh Dickinse468c872016-07-15 15:08:19 -0400453 /* Move dirty on pages not done by migrate_page_move_mapping() */
454 if (PageDirty(page))
455 SetPageDirty(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800456
Nick Pigginb291f002008-10-18 20:26:44 -0700457 mlock_migrate_page(newpage, page);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800458 ksm_migrate_page(newpage, page);
Nick Pigginb291f002008-10-18 20:26:44 -0700459
Christoph Lameterb20a3502006-03-22 00:09:12 -0800460 ClearPageSwapCache(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800461 ClearPagePrivate(page);
462 set_page_private(page, 0);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800463
464 /*
465 * If any waiters have accumulated on the new page then
466 * wake them up.
467 */
468 if (PageWriteback(newpage))
469 end_page_writeback(newpage);
470}
Christoph Lameterb20a3502006-03-22 00:09:12 -0800471
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700472/************************************************************
473 * Migration functions
474 ***********************************************************/
475
476/* Always fail migration. Used for mappings that are not movable */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700477int fail_migrate_page(struct address_space *mapping,
478 struct page *newpage, struct page *page)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700479{
480 return -EIO;
481}
482EXPORT_SYMBOL(fail_migrate_page);
483
Christoph Lameterb20a3502006-03-22 00:09:12 -0800484/*
485 * Common logic to directly migrate a single page suitable for
David Howells266cf652009-04-03 16:42:36 +0100486 * pages that do not use PagePrivate/PagePrivate2.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800487 *
488 * Pages are locked upon entry and exit.
489 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700490int migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800491 struct page *newpage, struct page *page,
492 enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800493{
494 int rc;
495
496 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
497
Mel Gormana6bc32b2012-01-12 17:19:43 -0800498 rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800499
500 if (rc)
501 return rc;
502
503 migrate_page_copy(newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800504 return 0;
505}
506EXPORT_SYMBOL(migrate_page);
507
David Howells93614012006-09-30 20:45:40 +0200508#ifdef CONFIG_BLOCK
Christoph Lameterb20a3502006-03-22 00:09:12 -0800509/*
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700510 * Migration function for pages with buffers. This function can only be used
511 * if the underlying filesystem guarantees that no other references to "page"
512 * exist.
513 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700514int buffer_migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800515 struct page *newpage, struct page *page, enum migrate_mode mode)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700516{
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700517 struct buffer_head *bh, *head;
518 int rc;
519
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700520 if (!page_has_buffers(page))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800521 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700522
523 head = page_buffers(page);
524
Mel Gormana6bc32b2012-01-12 17:19:43 -0800525 rc = migrate_page_move_mapping(mapping, newpage, page, head, mode);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700526
527 if (rc)
528 return rc;
529
Mel Gormanb969c4a2012-01-12 17:19:34 -0800530 /*
531 * In the async case, migrate_page_move_mapping locked the buffers
532 * with an IRQ-safe spinlock held. In the sync case, the buffers
533 * need to be locked now
534 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800535 if (mode != MIGRATE_ASYNC)
536 BUG_ON(!buffer_migrate_lock_buffers(head, mode));
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700537
538 ClearPagePrivate(page);
539 set_page_private(newpage, page_private(page));
540 set_page_private(page, 0);
541 put_page(page);
542 get_page(newpage);
543
544 bh = head;
545 do {
546 set_bh_page(bh, newpage, bh_offset(bh));
547 bh = bh->b_this_page;
548
549 } while (bh != head);
550
551 SetPagePrivate(newpage);
552
553 migrate_page_copy(newpage, page);
554
555 bh = head;
556 do {
557 unlock_buffer(bh);
558 put_bh(bh);
559 bh = bh->b_this_page;
560
561 } while (bh != head);
562
563 return 0;
564}
565EXPORT_SYMBOL(buffer_migrate_page);
David Howells93614012006-09-30 20:45:40 +0200566#endif
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700567
Christoph Lameter04e62a22006-06-23 02:03:38 -0700568/*
569 * Writeback a page to clean the dirty state
570 */
571static int writeout(struct address_space *mapping, struct page *page)
572{
573 struct writeback_control wbc = {
574 .sync_mode = WB_SYNC_NONE,
575 .nr_to_write = 1,
576 .range_start = 0,
577 .range_end = LLONG_MAX,
Christoph Lameter04e62a22006-06-23 02:03:38 -0700578 .for_reclaim = 1
579 };
580 int rc;
581
582 if (!mapping->a_ops->writepage)
583 /* No write method for the address space */
584 return -EINVAL;
585
586 if (!clear_page_dirty_for_io(page))
587 /* Someone else already triggered a write */
588 return -EAGAIN;
589
590 /*
591 * A dirty page may imply that the underlying filesystem has
592 * the page on some queue. So the page must be clean for
593 * migration. Writeout may mean we loose the lock and the
594 * page state is no longer what we checked for earlier.
595 * At this point we know that the migration attempt cannot
596 * be successful.
597 */
598 remove_migration_ptes(page, page);
599
600 rc = mapping->a_ops->writepage(page, &wbc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700601
602 if (rc != AOP_WRITEPAGE_ACTIVATE)
603 /* unlocked. Relock */
604 lock_page(page);
605
Hugh Dickinsbda85502008-11-19 15:36:36 -0800606 return (rc < 0) ? -EIO : -EAGAIN;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700607}
608
609/*
610 * Default handling if a filesystem does not provide a migration function.
611 */
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700612static int fallback_migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800613 struct page *newpage, struct page *page, enum migrate_mode mode)
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700614{
Mel Gormanb969c4a2012-01-12 17:19:34 -0800615 if (PageDirty(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800616 /* Only writeback pages in full synchronous migration */
617 if (mode != MIGRATE_SYNC)
Mel Gormanb969c4a2012-01-12 17:19:34 -0800618 return -EBUSY;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700619 return writeout(mapping, page);
Mel Gormanb969c4a2012-01-12 17:19:34 -0800620 }
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700621
622 /*
623 * Buffers may be managed in a filesystem specific way.
624 * We must have no buffers or drop them.
625 */
David Howells266cf652009-04-03 16:42:36 +0100626 if (page_has_private(page) &&
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700627 !try_to_release_page(page, GFP_KERNEL))
628 return -EAGAIN;
629
Mel Gormana6bc32b2012-01-12 17:19:43 -0800630 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700631}
632
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700633/*
Christoph Lametere24f0b82006-06-23 02:03:51 -0700634 * Move a page to a newly allocated page
635 * The page is locked and all ptes have been successfully removed.
636 *
637 * The new page will have replaced the old page if this function
638 * is successful.
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700639 *
640 * Return value:
641 * < 0 - error code
642 * == 0 - success
Christoph Lametere24f0b82006-06-23 02:03:51 -0700643 */
Mel Gorman3fe20112010-05-24 14:32:20 -0700644static int move_to_new_page(struct page *newpage, struct page *page,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800645 int remap_swapcache, enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700646{
647 struct address_space *mapping;
648 int rc;
649
650 /*
651 * Block others from accessing the page when we get around to
652 * establishing additional references. We are the only one
653 * holding a reference to the new page at this point.
654 */
Nick Piggin529ae9a2008-08-02 12:01:03 +0200655 if (!trylock_page(newpage))
Christoph Lametere24f0b82006-06-23 02:03:51 -0700656 BUG();
657
658 /* Prepare mapping for the new page.*/
659 newpage->index = page->index;
660 newpage->mapping = page->mapping;
Rik van Rielb2e18532008-10-18 20:26:30 -0700661 if (PageSwapBacked(page))
662 SetPageSwapBacked(newpage);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700663
664 mapping = page_mapping(page);
665 if (!mapping)
Mel Gormana6bc32b2012-01-12 17:19:43 -0800666 rc = migrate_page(mapping, newpage, page, mode);
Mel Gormanb969c4a2012-01-12 17:19:34 -0800667 else if (mapping->a_ops->migratepage)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700668 /*
Mel Gormanb969c4a2012-01-12 17:19:34 -0800669 * Most pages have a mapping and most filesystems provide a
670 * migratepage callback. Anonymous pages are part of swap
671 * space which also has its own migratepage callback. This
672 * is the most common path for page migration.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700673 */
Mel Gormanb969c4a2012-01-12 17:19:34 -0800674 rc = mapping->a_ops->migratepage(mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800675 newpage, page, mode);
Mel Gormanb969c4a2012-01-12 17:19:34 -0800676 else
Mel Gormana6bc32b2012-01-12 17:19:43 -0800677 rc = fallback_migrate_page(mapping, newpage, page, mode);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700678
Mel Gorman3fe20112010-05-24 14:32:20 -0700679 if (rc) {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700680 newpage->mapping = NULL;
Mel Gorman3fe20112010-05-24 14:32:20 -0700681 } else {
682 if (remap_swapcache)
683 remove_migration_ptes(page, newpage);
Konstantin Khlebnikov35512ec2012-02-03 15:37:13 -0800684 page->mapping = NULL;
Mel Gorman3fe20112010-05-24 14:32:20 -0700685 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700686
687 unlock_page(newpage);
688
689 return rc;
690}
691
Minchan Kim0dabec92011-10-31 17:06:57 -0700692static int __unmap_and_move(struct page *page, struct page *newpage,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800693 int force, bool offlining, enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700694{
Minchan Kim0dabec92011-10-31 17:06:57 -0700695 int rc = -EAGAIN;
Mel Gorman3fe20112010-05-24 14:32:20 -0700696 int remap_swapcache = 1;
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800697 int charge = 0;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -0700698 struct mem_cgroup *mem;
Mel Gorman3f6c8272010-05-24 14:32:17 -0700699 struct anon_vma *anon_vma = NULL;
Christoph Lameter95a402c2006-06-23 02:03:53 -0700700
Nick Piggin529ae9a2008-08-02 12:01:03 +0200701 if (!trylock_page(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800702 if (!force || mode == MIGRATE_ASYNC)
Minchan Kim0dabec92011-10-31 17:06:57 -0700703 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -0800704
705 /*
706 * It's not safe for direct compaction to call lock_page.
707 * For example, during page readahead pages are added locked
708 * to the LRU. Later, when the IO completes the pages are
709 * marked uptodate and unlocked. However, the queueing
710 * could be merging multiple pages for one bio (e.g.
711 * mpage_readpages). If an allocation happens for the
712 * second or third page, the process can end up locking
713 * the same page twice and deadlocking. Rather than
714 * trying to be clever about what pages can be locked,
715 * avoid the use of lock_page for direct compaction
716 * altogether.
717 */
718 if (current->flags & PF_MEMALLOC)
Minchan Kim0dabec92011-10-31 17:06:57 -0700719 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -0800720
Christoph Lametere24f0b82006-06-23 02:03:51 -0700721 lock_page(page);
722 }
723
Hugh Dickins62b61f62009-12-14 17:59:33 -0800724 /*
725 * Only memory hotplug's offline_pages() caller has locked out KSM,
726 * and can safely migrate a KSM page. The other cases have skipped
727 * PageKsm along with PageReserved - but it is only now when we have
728 * the page lock that we can be certain it will not go KSM beneath us
729 * (KSM will not upgrade a page from PageAnon to PageKsm when it sees
730 * its pagecount raised, but only here do we take the page lock which
731 * serializes that).
732 */
733 if (PageKsm(page) && !offlining) {
734 rc = -EBUSY;
735 goto unlock;
736 }
737
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800738 /* charge against new page */
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700739 charge = mem_cgroup_prepare_migration(page, newpage, &mem, GFP_KERNEL);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800740 if (charge == -ENOMEM) {
741 rc = -ENOMEM;
742 goto unlock;
743 }
744 BUG_ON(charge);
745
Christoph Lametere24f0b82006-06-23 02:03:51 -0700746 if (PageWriteback(page)) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700747 /*
Mel Gormana6bc32b2012-01-12 17:19:43 -0800748 * Only in the case of a full syncronous migration is it
749 * necessary to wait for PageWriteback. In the async case,
750 * the retry loop is too short and in the sync-light case,
751 * the overhead of stalling is too much
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700752 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800753 if (mode != MIGRATE_SYNC) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700754 rc = -EBUSY;
755 goto uncharge;
756 }
757 if (!force)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800758 goto uncharge;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700759 wait_on_page_writeback(page);
760 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700761 /*
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700762 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
763 * we cannot notice that anon_vma is freed while we migrates a page.
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800764 * This get_anon_vma() delays freeing anon_vma pointer until the end
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700765 * of migration. File cache pages are no problem because of page_lock()
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700766 * File Caches may use write_page() or lock_page() in migration, then,
767 * just care Anon page here.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700768 */
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700769 if (PageAnon(page)) {
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800770 /*
771 * Only page_lock_anon_vma() understands the subtleties of
772 * getting a hold on an anon_vma from outside one of its mms.
773 */
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700774 anon_vma = page_get_anon_vma(page);
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800775 if (anon_vma) {
776 /*
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700777 * Anon page
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800778 */
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800779 } else if (PageSwapCache(page)) {
Mel Gorman3fe20112010-05-24 14:32:20 -0700780 /*
781 * We cannot be sure that the anon_vma of an unmapped
782 * swapcache page is safe to use because we don't
783 * know in advance if the VMA that this page belonged
784 * to still exists. If the VMA and others sharing the
785 * data have been freed, then the anon_vma could
786 * already be invalid.
787 *
788 * To avoid this possibility, swapcache pages get
789 * migrated but are not remapped when migration
790 * completes
791 */
792 remap_swapcache = 0;
793 } else {
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800794 goto uncharge;
Mel Gorman3fe20112010-05-24 14:32:20 -0700795 }
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700796 }
Shaohua Li62e1c552008-02-04 22:29:33 -0800797
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700798 /*
Shaohua Li62e1c552008-02-04 22:29:33 -0800799 * Corner case handling:
800 * 1. When a new swap-cache page is read into, it is added to the LRU
801 * and treated as swapcache but it has no rmap yet.
802 * Calling try_to_unmap() against a page->mapping==NULL page will
803 * trigger a BUG. So handle it here.
804 * 2. An orphaned page (see truncate_complete_page) might have
805 * fs-private metadata. The page can be picked up due to memory
806 * offlining. Everywhere else except page reclaim, the page is
807 * invisible to the vm, so the page can not be migrated. So try to
808 * free the metadata, so the page can be freed.
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700809 */
Shaohua Li62e1c552008-02-04 22:29:33 -0800810 if (!page->mapping) {
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800811 VM_BUG_ON(PageAnon(page));
812 if (page_has_private(page)) {
Shaohua Li62e1c552008-02-04 22:29:33 -0800813 try_to_free_buffers(page);
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800814 goto uncharge;
Shaohua Li62e1c552008-02-04 22:29:33 -0800815 }
Shaohua Liabfc3482009-09-21 17:01:19 -0700816 goto skip_unmap;
Shaohua Li62e1c552008-02-04 22:29:33 -0800817 }
818
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700819 /* Establish migration ptes or remove ptes */
Andi Kleen14fa31b2009-09-16 11:50:10 +0200820 try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700821
Shaohua Liabfc3482009-09-21 17:01:19 -0700822skip_unmap:
Christoph Lametere6a15302006-06-25 05:46:49 -0700823 if (!page_mapped(page))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800824 rc = move_to_new_page(newpage, page, remap_swapcache, mode);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700825
Mel Gorman3fe20112010-05-24 14:32:20 -0700826 if (rc && remap_swapcache)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700827 remove_migration_ptes(page, page);
Mel Gorman3f6c8272010-05-24 14:32:17 -0700828
829 /* Drop an anon_vma reference if we took one */
Rik van Riel76545062010-08-09 17:18:41 -0700830 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -0700831 put_anon_vma(anon_vma);
Mel Gorman3f6c8272010-05-24 14:32:17 -0700832
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800833uncharge:
834 if (!charge)
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -0800835 mem_cgroup_end_migration(mem, page, newpage, rc == 0);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700836unlock:
837 unlock_page(page);
Minchan Kim0dabec92011-10-31 17:06:57 -0700838out:
839 return rc;
840}
Christoph Lameter95a402c2006-06-23 02:03:53 -0700841
Minchan Kim0dabec92011-10-31 17:06:57 -0700842/*
843 * Obtain the lock on page, remove all ptes and migrate the page
844 * to the newly allocated page in newpage.
845 */
846static int unmap_and_move(new_page_t get_new_page, unsigned long private,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800847 struct page *page, int force, bool offlining,
848 enum migrate_mode mode)
Minchan Kim0dabec92011-10-31 17:06:57 -0700849{
850 int rc = 0;
851 int *result = NULL;
852 struct page *newpage = get_new_page(page, private, &result);
853
854 if (!newpage)
855 return -ENOMEM;
856
857 if (page_count(page) == 1) {
858 /* page was freed from under us. So we are done. */
859 goto out;
860 }
861
862 if (unlikely(PageTransHuge(page)))
863 if (unlikely(split_huge_page(page)))
864 goto out;
865
Mel Gormana6bc32b2012-01-12 17:19:43 -0800866 rc = __unmap_and_move(page, newpage, force, offlining, mode);
Minchan Kim0dabec92011-10-31 17:06:57 -0700867out:
Christoph Lametere24f0b82006-06-23 02:03:51 -0700868 if (rc != -EAGAIN) {
Minchan Kim0dabec92011-10-31 17:06:57 -0700869 /*
870 * A page that has been migrated has all references
871 * removed and will be freed. A page that has not been
872 * migrated will have kepts its references and be
873 * restored.
874 */
875 list_del(&page->lru);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -0700876 dec_zone_page_state(page, NR_ISOLATED_ANON +
Johannes Weiner6c0b1352009-09-21 17:02:59 -0700877 page_is_file_cache(page));
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700878 putback_lru_page(page);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700879 }
Christoph Lameter95a402c2006-06-23 02:03:53 -0700880 /*
881 * Move the new page to the LRU. If migration was not successful
882 * then this will free the page.
883 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700884 putback_lru_page(newpage);
Christoph Lameter742755a2006-06-23 02:03:55 -0700885 if (result) {
886 if (rc)
887 *result = rc;
888 else
889 *result = page_to_nid(newpage);
890 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700891 return rc;
892}
893
894/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900895 * Counterpart of unmap_and_move_page() for hugepage migration.
896 *
897 * This function doesn't wait the completion of hugepage I/O
898 * because there is no race between I/O and migration for hugepage.
899 * Note that currently hugepage I/O occurs only in direct I/O
900 * where no lock is held and PG_writeback is irrelevant,
901 * and writeback status of all subpages are counted in the reference
902 * count of the head page (i.e. if all subpages of a 2MB hugepage are
903 * under direct I/O, the reference of the head page is 512 and a bit more.)
904 * This means that when we try to migrate hugepage whose subpages are
905 * doing direct I/O, some references remain after try_to_unmap() and
906 * hugepage migration fails without data corruption.
907 *
908 * There is also no race when direct I/O is issued on the page under migration,
909 * because then pte is replaced with migration swap entry and direct I/O code
910 * will wait in the page fault for migration to complete.
911 */
912static int unmap_and_move_huge_page(new_page_t get_new_page,
913 unsigned long private, struct page *hpage,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800914 int force, bool offlining,
915 enum migrate_mode mode)
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900916{
917 int rc = 0;
918 int *result = NULL;
919 struct page *new_hpage = get_new_page(hpage, private, &result);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900920 struct anon_vma *anon_vma = NULL;
921
922 if (!new_hpage)
923 return -ENOMEM;
924
925 rc = -EAGAIN;
926
927 if (!trylock_page(hpage)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800928 if (!force || mode != MIGRATE_SYNC)
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900929 goto out;
930 lock_page(hpage);
931 }
932
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700933 if (PageAnon(hpage))
934 anon_vma = page_get_anon_vma(hpage);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900935
936 try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
937
938 if (!page_mapped(hpage))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800939 rc = move_to_new_page(new_hpage, hpage, 1, mode);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900940
941 if (rc)
942 remove_migration_ptes(hpage, hpage);
943
Hugh Dickinsfd4a4662011-01-13 15:47:31 -0800944 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -0700945 put_anon_vma(anon_vma);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900946 unlock_page(hpage);
947
Hillf Danton09761332011-12-08 14:34:20 -0800948out:
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900949 if (rc != -EAGAIN) {
950 list_del(&hpage->lru);
951 put_page(hpage);
952 }
953
954 put_page(new_hpage);
955
956 if (result) {
957 if (rc)
958 *result = rc;
959 else
960 *result = page_to_nid(new_hpage);
961 }
962 return rc;
963}
964
965/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800966 * migrate_pages
967 *
Christoph Lameter95a402c2006-06-23 02:03:53 -0700968 * The function takes one list of pages to migrate and a function
969 * that determines from the page to be migrated and the private data
970 * the target of the move and allocates the page.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800971 *
972 * The function returns after 10 attempts or if no pages
973 * are movable anymore because to has become empty
Minchan Kimcf608ac2010-10-26 14:21:29 -0700974 * or no retryable pages exist anymore.
975 * Caller should call putback_lru_pages to return pages to the LRU
Minchan Kim28bd6572011-01-25 15:07:26 -0800976 * or free list only if ret != 0.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800977 *
Christoph Lameter95a402c2006-06-23 02:03:53 -0700978 * Return: Number of pages not migrated or error code.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800979 */
Christoph Lameter95a402c2006-06-23 02:03:53 -0700980int migrate_pages(struct list_head *from,
Mel Gorman7f0f2492011-01-13 15:45:58 -0800981 new_page_t get_new_page, unsigned long private, bool offlining,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800982 enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800983{
Christoph Lametere24f0b82006-06-23 02:03:51 -0700984 int retry = 1;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800985 int nr_failed = 0;
Mel Gormancd9bb9b2012-10-19 10:46:20 +0100986 int nr_succeeded = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800987 int pass = 0;
988 struct page *page;
989 struct page *page2;
990 int swapwrite = current->flags & PF_SWAPWRITE;
991 int rc;
992
Liam Markcc2d4bd2013-01-16 10:14:40 -0800993 trace_migrate_pages_start(mode);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800994 if (!swapwrite)
995 current->flags |= PF_SWAPWRITE;
996
Christoph Lametere24f0b82006-06-23 02:03:51 -0700997 for(pass = 0; pass < 10 && retry; pass++) {
998 retry = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800999
Christoph Lametere24f0b82006-06-23 02:03:51 -07001000 list_for_each_entry_safe(page, page2, from, lru) {
Christoph Lametere24f0b82006-06-23 02:03:51 -07001001 cond_resched();
Christoph Lameterb20a3502006-03-22 00:09:12 -08001002
Christoph Lameter95a402c2006-06-23 02:03:53 -07001003 rc = unmap_and_move(get_new_page, private,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001004 page, pass > 2, offlining,
Mel Gormana6bc32b2012-01-12 17:19:43 -08001005 mode);
Christoph Lameterb20a3502006-03-22 00:09:12 -08001006
Christoph Lametere24f0b82006-06-23 02:03:51 -07001007 switch(rc) {
Christoph Lameter95a402c2006-06-23 02:03:53 -07001008 case -ENOMEM:
1009 goto out;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001010 case -EAGAIN:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001011 retry++;
Liam Markcc2d4bd2013-01-16 10:14:40 -08001012 trace_migrate_retry(retry);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001013 break;
1014 case 0:
Mel Gormancd9bb9b2012-10-19 10:46:20 +01001015 nr_succeeded++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001016 break;
1017 default:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001018 /* Permanent failure */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001019 nr_failed++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001020 break;
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001021 }
Christoph Lameterb20a3502006-03-22 00:09:12 -08001022 }
1023 }
Christoph Lameter95a402c2006-06-23 02:03:53 -07001024 rc = 0;
1025out:
Mel Gormancd9bb9b2012-10-19 10:46:20 +01001026 if (nr_succeeded)
1027 count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
1028 if (nr_failed)
1029 count_vm_events(PGMIGRATE_FAIL, nr_failed);
Christoph Lameterb20a3502006-03-22 00:09:12 -08001030 if (!swapwrite)
1031 current->flags &= ~PF_SWAPWRITE;
1032
Liam Markcc2d4bd2013-01-16 10:14:40 -08001033 trace_migrate_pages_end(mode);
Christoph Lameter95a402c2006-06-23 02:03:53 -07001034 if (rc)
1035 return rc;
1036
Christoph Lameterb20a3502006-03-22 00:09:12 -08001037 return nr_failed + retry;
1038}
1039
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001040int migrate_huge_pages(struct list_head *from,
Mel Gorman7f0f2492011-01-13 15:45:58 -08001041 new_page_t get_new_page, unsigned long private, bool offlining,
Mel Gormana6bc32b2012-01-12 17:19:43 -08001042 enum migrate_mode mode)
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001043{
1044 int retry = 1;
1045 int nr_failed = 0;
1046 int pass = 0;
1047 struct page *page;
1048 struct page *page2;
1049 int rc;
1050
1051 for (pass = 0; pass < 10 && retry; pass++) {
1052 retry = 0;
1053
1054 list_for_each_entry_safe(page, page2, from, lru) {
1055 cond_resched();
1056
1057 rc = unmap_and_move_huge_page(get_new_page,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001058 private, page, pass > 2, offlining,
Mel Gormana6bc32b2012-01-12 17:19:43 -08001059 mode);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001060
1061 switch(rc) {
1062 case -ENOMEM:
1063 goto out;
1064 case -EAGAIN:
1065 retry++;
1066 break;
1067 case 0:
1068 break;
1069 default:
1070 /* Permanent failure */
1071 nr_failed++;
1072 break;
1073 }
1074 }
1075 }
1076 rc = 0;
1077out:
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001078 if (rc)
1079 return rc;
1080
1081 return nr_failed + retry;
1082}
1083
Christoph Lameter742755a2006-06-23 02:03:55 -07001084#ifdef CONFIG_NUMA
1085/*
1086 * Move a list of individual pages
1087 */
1088struct page_to_node {
1089 unsigned long addr;
1090 struct page *page;
1091 int node;
1092 int status;
1093};
1094
1095static struct page *new_page_node(struct page *p, unsigned long private,
1096 int **result)
1097{
1098 struct page_to_node *pm = (struct page_to_node *)private;
1099
1100 while (pm->node != MAX_NUMNODES && pm->page != p)
1101 pm++;
1102
1103 if (pm->node == MAX_NUMNODES)
1104 return NULL;
1105
1106 *result = &pm->status;
1107
Mel Gorman6484eb32009-06-16 15:31:54 -07001108 return alloc_pages_exact_node(pm->node,
Mel Gorman769848c2007-07-17 04:03:05 -07001109 GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
Christoph Lameter742755a2006-06-23 02:03:55 -07001110}
1111
1112/*
1113 * Move a set of pages as indicated in the pm array. The addr
1114 * field must be set to the virtual address of the page to be moved
1115 * and the node number must contain a valid target node.
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001116 * The pm array ends with node = MAX_NUMNODES.
Christoph Lameter742755a2006-06-23 02:03:55 -07001117 */
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001118static int do_move_page_to_node_array(struct mm_struct *mm,
1119 struct page_to_node *pm,
1120 int migrate_all)
Christoph Lameter742755a2006-06-23 02:03:55 -07001121{
1122 int err;
1123 struct page_to_node *pp;
1124 LIST_HEAD(pagelist);
1125
1126 down_read(&mm->mmap_sem);
1127
1128 /*
1129 * Build a list of pages to migrate
1130 */
Christoph Lameter742755a2006-06-23 02:03:55 -07001131 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
1132 struct vm_area_struct *vma;
1133 struct page *page;
1134
Christoph Lameter742755a2006-06-23 02:03:55 -07001135 err = -EFAULT;
1136 vma = find_vma(mm, pp->addr);
Gleb Natapov70384dc2010-10-26 14:22:07 -07001137 if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
Christoph Lameter742755a2006-06-23 02:03:55 -07001138 goto set_status;
1139
Andrea Arcangeli500d65d2011-01-13 15:46:55 -08001140 page = follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001141
1142 err = PTR_ERR(page);
1143 if (IS_ERR(page))
1144 goto set_status;
1145
Christoph Lameter742755a2006-06-23 02:03:55 -07001146 err = -ENOENT;
1147 if (!page)
1148 goto set_status;
1149
Hugh Dickins62b61f62009-12-14 17:59:33 -08001150 /* Use PageReserved to check for zero page */
1151 if (PageReserved(page) || PageKsm(page))
Christoph Lameter742755a2006-06-23 02:03:55 -07001152 goto put_and_set;
1153
1154 pp->page = page;
1155 err = page_to_nid(page);
1156
1157 if (err == pp->node)
1158 /*
1159 * Node already in the right place
1160 */
1161 goto put_and_set;
1162
1163 err = -EACCES;
1164 if (page_mapcount(page) > 1 &&
1165 !migrate_all)
1166 goto put_and_set;
1167
Nick Piggin62695a82008-10-18 20:26:09 -07001168 err = isolate_lru_page(page);
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001169 if (!err) {
Nick Piggin62695a82008-10-18 20:26:09 -07001170 list_add_tail(&page->lru, &pagelist);
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001171 inc_zone_page_state(page, NR_ISOLATED_ANON +
1172 page_is_file_cache(page));
1173 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001174put_and_set:
1175 /*
1176 * Either remove the duplicate refcount from
1177 * isolate_lru_page() or drop the page ref if it was
1178 * not isolated.
1179 */
1180 put_page(page);
1181set_status:
1182 pp->status = err;
1183 }
1184
Brice Gogline78bbfa2008-10-18 20:27:15 -07001185 err = 0;
Minchan Kimcf608ac2010-10-26 14:21:29 -07001186 if (!list_empty(&pagelist)) {
Christoph Lameter742755a2006-06-23 02:03:55 -07001187 err = migrate_pages(&pagelist, new_page_node,
Mel Gormana6bc32b2012-01-12 17:19:43 -08001188 (unsigned long)pm, 0, MIGRATE_SYNC);
Minchan Kimcf608ac2010-10-26 14:21:29 -07001189 if (err)
1190 putback_lru_pages(&pagelist);
1191 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001192
1193 up_read(&mm->mmap_sem);
1194 return err;
1195}
1196
1197/*
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001198 * Migrate an array of page address onto an array of nodes and fill
1199 * the corresponding array of status.
1200 */
Christoph Lameter3268c632012-03-21 16:34:06 -07001201static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001202 unsigned long nr_pages,
1203 const void __user * __user *pages,
1204 const int __user *nodes,
1205 int __user *status, int flags)
1206{
Brice Goglin3140a222009-01-06 14:38:57 -08001207 struct page_to_node *pm;
Brice Goglin3140a222009-01-06 14:38:57 -08001208 unsigned long chunk_nr_pages;
1209 unsigned long chunk_start;
1210 int err;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001211
Brice Goglin3140a222009-01-06 14:38:57 -08001212 err = -ENOMEM;
1213 pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
1214 if (!pm)
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001215 goto out;
Brice Goglin35282a22009-06-16 15:32:43 -07001216
1217 migrate_prep();
1218
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001219 /*
Brice Goglin3140a222009-01-06 14:38:57 -08001220 * Store a chunk of page_to_node array in a page,
1221 * but keep the last one as a marker
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001222 */
Brice Goglin3140a222009-01-06 14:38:57 -08001223 chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001224
Brice Goglin3140a222009-01-06 14:38:57 -08001225 for (chunk_start = 0;
1226 chunk_start < nr_pages;
1227 chunk_start += chunk_nr_pages) {
1228 int j;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001229
Brice Goglin3140a222009-01-06 14:38:57 -08001230 if (chunk_start + chunk_nr_pages > nr_pages)
1231 chunk_nr_pages = nr_pages - chunk_start;
1232
1233 /* fill the chunk pm with addrs and nodes from user-space */
1234 for (j = 0; j < chunk_nr_pages; j++) {
1235 const void __user *p;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001236 int node;
1237
Brice Goglin3140a222009-01-06 14:38:57 -08001238 err = -EFAULT;
1239 if (get_user(p, pages + j + chunk_start))
1240 goto out_pm;
1241 pm[j].addr = (unsigned long) p;
1242
1243 if (get_user(node, nodes + j + chunk_start))
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001244 goto out_pm;
1245
1246 err = -ENODEV;
Linus Torvalds6f5a55f2010-02-05 16:16:50 -08001247 if (node < 0 || node >= MAX_NUMNODES)
1248 goto out_pm;
1249
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001250 if (!node_state(node, N_HIGH_MEMORY))
1251 goto out_pm;
1252
1253 err = -EACCES;
1254 if (!node_isset(node, task_nodes))
1255 goto out_pm;
1256
Brice Goglin3140a222009-01-06 14:38:57 -08001257 pm[j].node = node;
1258 }
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001259
Brice Goglin3140a222009-01-06 14:38:57 -08001260 /* End marker for this chunk */
1261 pm[chunk_nr_pages].node = MAX_NUMNODES;
1262
1263 /* Migrate this chunk */
1264 err = do_move_page_to_node_array(mm, pm,
1265 flags & MPOL_MF_MOVE_ALL);
1266 if (err < 0)
1267 goto out_pm;
1268
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001269 /* Return status information */
Brice Goglin3140a222009-01-06 14:38:57 -08001270 for (j = 0; j < chunk_nr_pages; j++)
1271 if (put_user(pm[j].status, status + j + chunk_start)) {
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001272 err = -EFAULT;
Brice Goglin3140a222009-01-06 14:38:57 -08001273 goto out_pm;
1274 }
1275 }
1276 err = 0;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001277
1278out_pm:
Brice Goglin3140a222009-01-06 14:38:57 -08001279 free_page((unsigned long)pm);
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001280out:
1281 return err;
1282}
1283
1284/*
Brice Goglin2f007e72008-10-18 20:27:16 -07001285 * Determine the nodes of an array of pages and store it in an array of status.
Christoph Lameter742755a2006-06-23 02:03:55 -07001286 */
Brice Goglin80bba122008-12-09 13:14:23 -08001287static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
1288 const void __user **pages, int *status)
Christoph Lameter742755a2006-06-23 02:03:55 -07001289{
Brice Goglin2f007e72008-10-18 20:27:16 -07001290 unsigned long i;
Brice Goglin2f007e72008-10-18 20:27:16 -07001291
Christoph Lameter742755a2006-06-23 02:03:55 -07001292 down_read(&mm->mmap_sem);
1293
Brice Goglin2f007e72008-10-18 20:27:16 -07001294 for (i = 0; i < nr_pages; i++) {
Brice Goglin80bba122008-12-09 13:14:23 -08001295 unsigned long addr = (unsigned long)(*pages);
Christoph Lameter742755a2006-06-23 02:03:55 -07001296 struct vm_area_struct *vma;
1297 struct page *page;
KOSAKI Motohiroc095adb2008-12-16 16:06:43 +09001298 int err = -EFAULT;
Brice Goglin2f007e72008-10-18 20:27:16 -07001299
1300 vma = find_vma(mm, addr);
Gleb Natapov70384dc2010-10-26 14:22:07 -07001301 if (!vma || addr < vma->vm_start)
Christoph Lameter742755a2006-06-23 02:03:55 -07001302 goto set_status;
1303
Brice Goglin2f007e72008-10-18 20:27:16 -07001304 page = follow_page(vma, addr, 0);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001305
1306 err = PTR_ERR(page);
1307 if (IS_ERR(page))
1308 goto set_status;
1309
Christoph Lameter742755a2006-06-23 02:03:55 -07001310 err = -ENOENT;
1311 /* Use PageReserved to check for zero page */
Hugh Dickins62b61f62009-12-14 17:59:33 -08001312 if (!page || PageReserved(page) || PageKsm(page))
Christoph Lameter742755a2006-06-23 02:03:55 -07001313 goto set_status;
1314
1315 err = page_to_nid(page);
1316set_status:
Brice Goglin80bba122008-12-09 13:14:23 -08001317 *status = err;
1318
1319 pages++;
1320 status++;
1321 }
1322
1323 up_read(&mm->mmap_sem);
1324}
1325
1326/*
1327 * Determine the nodes of a user array of pages and store it in
1328 * a user array of status.
1329 */
1330static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1331 const void __user * __user *pages,
1332 int __user *status)
1333{
1334#define DO_PAGES_STAT_CHUNK_NR 16
1335 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1336 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
Brice Goglin80bba122008-12-09 13:14:23 -08001337
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001338 while (nr_pages) {
1339 unsigned long chunk_nr;
Brice Goglin80bba122008-12-09 13:14:23 -08001340
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001341 chunk_nr = nr_pages;
1342 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1343 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1344
1345 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
1346 break;
Brice Goglin80bba122008-12-09 13:14:23 -08001347
1348 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1349
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001350 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1351 break;
Christoph Lameter742755a2006-06-23 02:03:55 -07001352
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001353 pages += chunk_nr;
1354 status += chunk_nr;
1355 nr_pages -= chunk_nr;
1356 }
1357 return nr_pages ? -EFAULT : 0;
Christoph Lameter742755a2006-06-23 02:03:55 -07001358}
1359
1360/*
1361 * Move a list of pages in the address space of the currently executing
1362 * process.
1363 */
Heiko Carstens938bb9f2009-01-14 14:14:30 +01001364SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1365 const void __user * __user *, pages,
1366 const int __user *, nodes,
1367 int __user *, status, int, flags)
Christoph Lameter742755a2006-06-23 02:03:55 -07001368{
Christoph Lameter742755a2006-06-23 02:03:55 -07001369 struct task_struct *task;
Christoph Lameter742755a2006-06-23 02:03:55 -07001370 struct mm_struct *mm;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001371 int err;
Christoph Lameter3268c632012-03-21 16:34:06 -07001372 nodemask_t task_nodes;
Christoph Lameter742755a2006-06-23 02:03:55 -07001373
1374 /* Check flags */
1375 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1376 return -EINVAL;
1377
1378 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1379 return -EPERM;
1380
1381 /* Find the mm_struct */
Greg Thelena879bf52011-02-25 14:44:13 -08001382 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07001383 task = pid ? find_task_by_vpid(pid) : current;
Christoph Lameter742755a2006-06-23 02:03:55 -07001384 if (!task) {
Greg Thelena879bf52011-02-25 14:44:13 -08001385 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001386 return -ESRCH;
1387 }
Christoph Lameter3268c632012-03-21 16:34:06 -07001388 get_task_struct(task);
Christoph Lameter742755a2006-06-23 02:03:55 -07001389
1390 /*
1391 * Check if this process has the right to modify the specified
Linus Torvalds4c497362017-08-20 13:26:27 -07001392 * process. Use the regular "ptrace_may_access()" checks.
Christoph Lameter742755a2006-06-23 02:03:55 -07001393 */
Linus Torvalds4c497362017-08-20 13:26:27 -07001394 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001395 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001396 err = -EPERM;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001397 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001398 }
David Howellsc69e8d92008-11-14 10:39:19 +11001399 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001400
David Quigley86c3a762006-06-23 02:04:02 -07001401 err = security_task_movememory(task);
1402 if (err)
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001403 goto out;
David Quigley86c3a762006-06-23 02:04:02 -07001404
Christoph Lameter3268c632012-03-21 16:34:06 -07001405 task_nodes = cpuset_mems_allowed(task);
1406 mm = get_task_mm(task);
1407 put_task_struct(task);
1408
Sasha Levin6e8b09e2012-04-25 16:01:53 -07001409 if (!mm)
1410 return -EINVAL;
1411
1412 if (nodes)
1413 err = do_pages_move(mm, task_nodes, nr_pages, pages,
1414 nodes, status, flags);
1415 else
1416 err = do_pages_stat(mm, nr_pages, pages, status);
Christoph Lameter3268c632012-03-21 16:34:06 -07001417
1418 mmput(mm);
1419 return err;
David Quigley86c3a762006-06-23 02:04:02 -07001420
Christoph Lameter742755a2006-06-23 02:03:55 -07001421out:
Christoph Lameter3268c632012-03-21 16:34:06 -07001422 put_task_struct(task);
Christoph Lameter742755a2006-06-23 02:03:55 -07001423 return err;
1424}
Christoph Lameter742755a2006-06-23 02:03:55 -07001425
Christoph Lameter7b2259b2006-06-25 05:46:48 -07001426/*
1427 * Call migration functions in the vma_ops that may prepare
1428 * memory in a vm for migration. migration functions may perform
1429 * the migration for vmas that do not have an underlying page struct.
1430 */
1431int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
1432 const nodemask_t *from, unsigned long flags)
1433{
1434 struct vm_area_struct *vma;
1435 int err = 0;
1436
Daisuke Nishimura1001c9f2009-02-11 13:04:18 -08001437 for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
Christoph Lameter7b2259b2006-06-25 05:46:48 -07001438 if (vma->vm_ops && vma->vm_ops->migrate) {
1439 err = vma->vm_ops->migrate(vma, to, from, flags);
1440 if (err)
1441 break;
1442 }
1443 }
1444 return err;
1445}
Gerald Schaefer83d16742008-07-23 21:28:22 -07001446#endif