blob: 5fb52678e179709c9e108beb4a4f2bf6b9981a75 [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));
Mel Gorman9c7beff2014-10-02 19:47:41 +0100144
145 /* Recheck VMA as permissions can change since migration started */
Christoph Lameter06972122006-06-23 02:03:35 -0700146 if (is_write_migration_entry(entry))
Mel Gorman9c7beff2014-10-02 19:47:41 +0100147 pte = maybe_mkwrite(pte, vma);
148
Andi Kleen3ef8fd72010-10-11 16:03:21 +0200149#ifdef CONFIG_HUGETLB_PAGE
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900150 if (PageHuge(new))
151 pte = pte_mkhuge(pte);
Andi Kleen3ef8fd72010-10-11 16:03:21 +0200152#endif
Leonid Yegoshina0ca5172013-05-24 15:55:18 -0700153 flush_dcache_page(new);
Christoph Lameter06972122006-06-23 02:03:35 -0700154 set_pte_at(mm, addr, ptep, pte);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700155
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900156 if (PageHuge(new)) {
157 if (PageAnon(new))
158 hugepage_add_anon_rmap(new, vma, addr);
159 else
160 page_dup_rmap(new);
161 } else if (PageAnon(new))
Christoph Lameter04e62a22006-06-23 02:03:38 -0700162 page_add_anon_rmap(new, vma, addr);
163 else
164 page_add_file_rmap(new);
165
166 /* No need to invalidate - it was non-present before */
Russell King4b3073e2009-12-18 16:40:18 +0000167 update_mmu_cache(vma, addr, ptep);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800168unlock:
Christoph Lameter06972122006-06-23 02:03:35 -0700169 pte_unmap_unlock(ptep, ptl);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800170out:
171 return SWAP_AGAIN;
Christoph Lameter06972122006-06-23 02:03:35 -0700172}
173
174/*
Christoph Lameter04e62a22006-06-23 02:03:38 -0700175 * Get rid of all migration entries and replace them by
176 * references to the indicated page.
177 */
178static void remove_migration_ptes(struct page *old, struct page *new)
179{
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800180 rmap_walk(new, remove_migration_pte, old);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700181}
182
183/*
Christoph Lameter06972122006-06-23 02:03:35 -0700184 * Something used the pte of a page under migration. We need to
185 * get to the page and wait until migration is finished.
186 * When we return from this function the fault will be retried.
Christoph Lameter06972122006-06-23 02:03:35 -0700187 */
Naoya Horiguchi065d0e82013-06-12 14:05:04 -0700188static void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
189 spinlock_t *ptl)
Christoph Lameter06972122006-06-23 02:03:35 -0700190{
Naoya Horiguchi065d0e82013-06-12 14:05:04 -0700191 pte_t pte;
Christoph Lameter06972122006-06-23 02:03:35 -0700192 swp_entry_t entry;
193 struct page *page;
194
Naoya Horiguchi065d0e82013-06-12 14:05:04 -0700195 spin_lock(ptl);
Christoph Lameter06972122006-06-23 02:03:35 -0700196 pte = *ptep;
197 if (!is_swap_pte(pte))
198 goto out;
199
200 entry = pte_to_swp_entry(pte);
201 if (!is_migration_entry(entry))
202 goto out;
203
204 page = migration_entry_to_page(entry);
205
Nick Piggine2867812008-07-25 19:45:30 -0700206 /*
207 * Once radix-tree replacement of page migration started, page_count
208 * *must* be zero. And, we don't want to call wait_on_page_locked()
209 * against a page without get_page().
210 * So, we use get_page_unless_zero(), here. Even failed, page fault
211 * will occur again.
212 */
213 if (!get_page_unless_zero(page))
214 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700215 pte_unmap_unlock(ptep, ptl);
216 wait_on_page_locked(page);
217 put_page(page);
218 return;
219out:
220 pte_unmap_unlock(ptep, ptl);
221}
222
Naoya Horiguchi065d0e82013-06-12 14:05:04 -0700223void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
224 unsigned long address)
225{
226 spinlock_t *ptl = pte_lockptr(mm, pmd);
227 pte_t *ptep = pte_offset_map(pmd, address);
228 __migration_entry_wait(mm, ptep, ptl);
229}
230
231void migration_entry_wait_huge(struct mm_struct *mm, pte_t *pte)
232{
233 spinlock_t *ptl = &(mm)->page_table_lock;
234 __migration_entry_wait(mm, pte, ptl);
235}
236
Mel Gormanb969c4a2012-01-12 17:19:34 -0800237#ifdef CONFIG_BLOCK
238/* Returns true if all buffers are successfully locked */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800239static bool buffer_migrate_lock_buffers(struct buffer_head *head,
240 enum migrate_mode mode)
Mel Gormanb969c4a2012-01-12 17:19:34 -0800241{
242 struct buffer_head *bh = head;
243
244 /* Simple case, sync compaction */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800245 if (mode != MIGRATE_ASYNC) {
Mel Gormanb969c4a2012-01-12 17:19:34 -0800246 do {
247 get_bh(bh);
248 lock_buffer(bh);
249 bh = bh->b_this_page;
250
251 } while (bh != head);
252
253 return true;
254 }
255
256 /* async case, we cannot block on lock_buffer so use trylock_buffer */
257 do {
258 get_bh(bh);
259 if (!trylock_buffer(bh)) {
260 /*
261 * We failed to lock the buffer and cannot stall in
262 * async migration. Release the taken locks
263 */
264 struct buffer_head *failed_bh = bh;
265 put_bh(failed_bh);
266 bh = head;
267 while (bh != failed_bh) {
268 unlock_buffer(bh);
269 put_bh(bh);
270 bh = bh->b_this_page;
271 }
272 return false;
273 }
274
275 bh = bh->b_this_page;
276 } while (bh != head);
277 return true;
278}
279#else
280static inline bool buffer_migrate_lock_buffers(struct buffer_head *head,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800281 enum migrate_mode mode)
Mel Gormanb969c4a2012-01-12 17:19:34 -0800282{
283 return true;
284}
285#endif /* CONFIG_BLOCK */
286
Christoph Lameterb20a3502006-03-22 00:09:12 -0800287/*
Christoph Lameterc3fcf8a2006-06-23 02:03:32 -0700288 * Replace the page in the mapping.
Christoph Lameter5b5c7122006-06-23 02:03:29 -0700289 *
290 * The number of remaining references must be:
291 * 1 for anonymous pages without a mapping
292 * 2 for pages with a mapping
David Howells266cf652009-04-03 16:42:36 +0100293 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800294 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700295static int migrate_page_move_mapping(struct address_space *mapping,
Mel Gormanb969c4a2012-01-12 17:19:34 -0800296 struct page *newpage, struct page *page,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800297 struct buffer_head *head, enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800298{
Hugh Dickinse468c872016-07-15 15:08:19 -0400299 struct zone *oldzone, *newzone;
300 int dirty;
Nick Piggine2867812008-07-25 19:45:30 -0700301 int expected_count;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800302 void **pslot;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800303
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700304 if (!mapping) {
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700305 /* Anonymous page without mapping */
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700306 if (page_count(page) != 1)
307 return -EAGAIN;
308 return 0;
309 }
310
Hugh Dickinse468c872016-07-15 15:08:19 -0400311 oldzone = page_zone(page);
312 newzone = page_zone(newpage);
313
Nick Piggin19fd6232008-07-25 19:45:32 -0700314 spin_lock_irq(&mapping->tree_lock);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800315
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800316 pslot = radix_tree_lookup_slot(&mapping->page_tree,
317 page_index(page));
Christoph Lameterb20a3502006-03-22 00:09:12 -0800318
Johannes Weineredcf4742009-09-21 17:02:59 -0700319 expected_count = 2 + page_has_private(page);
Nick Piggine2867812008-07-25 19:45:30 -0700320 if (page_count(page) != expected_count ||
Mel Gorman29c1f672011-01-13 15:47:21 -0800321 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700322 spin_unlock_irq(&mapping->tree_lock);
Christoph Lametere23ca002006-04-10 22:52:57 -0700323 return -EAGAIN;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800324 }
325
Nick Piggine2867812008-07-25 19:45:30 -0700326 if (!page_freeze_refs(page, expected_count)) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700327 spin_unlock_irq(&mapping->tree_lock);
Nick Piggine2867812008-07-25 19:45:30 -0700328 return -EAGAIN;
329 }
330
Christoph Lameterb20a3502006-03-22 00:09:12 -0800331 /*
Mel Gormanb969c4a2012-01-12 17:19:34 -0800332 * In the async migration case of moving a page with buffers, lock the
333 * buffers using trylock before the mapping is moved. If the mapping
334 * was moved, we later failed to lock the buffers and could not move
335 * the mapping back due to an elevated page count, we would have to
336 * block waiting on other references to be dropped.
337 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800338 if (mode == MIGRATE_ASYNC && head &&
339 !buffer_migrate_lock_buffers(head, mode)) {
Mel Gormanb969c4a2012-01-12 17:19:34 -0800340 page_unfreeze_refs(page, expected_count);
341 spin_unlock_irq(&mapping->tree_lock);
342 return -EAGAIN;
343 }
344
345 /*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800346 * Now we know that no one else is looking at the page.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800347 */
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800348 get_page(newpage); /* add cache reference */
Christoph Lameterb20a3502006-03-22 00:09:12 -0800349 if (PageSwapCache(page)) {
350 SetPageSwapCache(newpage);
351 set_page_private(newpage, page_private(page));
352 }
353
Hugh Dickinse468c872016-07-15 15:08:19 -0400354 /* Move dirty while page refs frozen and newpage not yet exposed */
355 dirty = PageDirty(page);
356 if (dirty) {
357 ClearPageDirty(page);
358 SetPageDirty(newpage);
359 }
360
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800361 radix_tree_replace_slot(pslot, newpage);
362
363 /*
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800364 * Drop cache reference from old page by unfreezing
365 * to one less reference.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800366 * We know this isn't the last reference.
367 */
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800368 page_unfreeze_refs(page, expected_count - 1);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800369
Hugh Dickinse468c872016-07-15 15:08:19 -0400370 spin_unlock(&mapping->tree_lock);
371 /* Leave irq disabled to prevent preemption while updating stats */
372
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700373 /*
374 * If moved to a different zone then also account
375 * the page for that zone. Other VM counters will be
376 * taken care of when we establish references to the
377 * new page and drop references to the old page.
378 *
379 * Note that anonymous pages are accounted for
380 * via NR_FILE_PAGES and NR_ANON_PAGES if they
381 * are mapped to swap space.
382 */
Hugh Dickinse468c872016-07-15 15:08:19 -0400383 if (newzone != oldzone) {
384 __dec_zone_state(oldzone, NR_FILE_PAGES);
385 __inc_zone_state(newzone, NR_FILE_PAGES);
386 if (PageSwapBacked(page) && !PageSwapCache(page)) {
387 __dec_zone_state(oldzone, NR_SHMEM);
388 __inc_zone_state(newzone, NR_SHMEM);
389 }
390 if (dirty && mapping_cap_account_dirty(mapping)) {
391 __dec_zone_state(oldzone, NR_FILE_DIRTY);
392 __inc_zone_state(newzone, NR_FILE_DIRTY);
393 }
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700394 }
Hugh Dickinse468c872016-07-15 15:08:19 -0400395 local_irq_enable();
Christoph Lameterb20a3502006-03-22 00:09:12 -0800396
397 return 0;
398}
Christoph Lameterb20a3502006-03-22 00:09:12 -0800399
400/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900401 * The expected number of remaining references is the same as that
402 * of migrate_page_move_mapping().
403 */
404int migrate_huge_page_move_mapping(struct address_space *mapping,
405 struct page *newpage, struct page *page)
406{
407 int expected_count;
408 void **pslot;
409
410 if (!mapping) {
411 if (page_count(page) != 1)
412 return -EAGAIN;
413 return 0;
414 }
415
416 spin_lock_irq(&mapping->tree_lock);
417
418 pslot = radix_tree_lookup_slot(&mapping->page_tree,
419 page_index(page));
420
421 expected_count = 2 + page_has_private(page);
422 if (page_count(page) != expected_count ||
Mel Gorman29c1f672011-01-13 15:47:21 -0800423 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900424 spin_unlock_irq(&mapping->tree_lock);
425 return -EAGAIN;
426 }
427
428 if (!page_freeze_refs(page, expected_count)) {
429 spin_unlock_irq(&mapping->tree_lock);
430 return -EAGAIN;
431 }
432
433 get_page(newpage);
434
435 radix_tree_replace_slot(pslot, newpage);
436
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800437 page_unfreeze_refs(page, expected_count - 1);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900438
439 spin_unlock_irq(&mapping->tree_lock);
440 return 0;
441}
442
443/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800444 * Copy the page to its new location
445 */
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900446void migrate_page_copy(struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800447{
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900448 if (PageHuge(page))
449 copy_huge_page(newpage, page);
450 else
451 copy_highpage(newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800452
453 if (PageError(page))
454 SetPageError(newpage);
455 if (PageReferenced(page))
456 SetPageReferenced(newpage);
457 if (PageUptodate(page))
458 SetPageUptodate(newpage);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700459 if (TestClearPageActive(page)) {
460 VM_BUG_ON(PageUnevictable(page));
Christoph Lameterb20a3502006-03-22 00:09:12 -0800461 SetPageActive(newpage);
Lee Schermerhorn418b27e2009-12-14 17:59:54 -0800462 } else if (TestClearPageUnevictable(page))
463 SetPageUnevictable(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800464 if (PageChecked(page))
465 SetPageChecked(newpage);
466 if (PageMappedToDisk(page))
467 SetPageMappedToDisk(newpage);
468
Hugh Dickinse468c872016-07-15 15:08:19 -0400469 /* Move dirty on pages not done by migrate_page_move_mapping() */
470 if (PageDirty(page))
471 SetPageDirty(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800472
Nick Pigginb291f002008-10-18 20:26:44 -0700473 mlock_migrate_page(newpage, page);
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800474 ksm_migrate_page(newpage, page);
Nick Pigginb291f002008-10-18 20:26:44 -0700475
Christoph Lameterb20a3502006-03-22 00:09:12 -0800476 ClearPageSwapCache(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800477 ClearPagePrivate(page);
478 set_page_private(page, 0);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800479
480 /*
481 * If any waiters have accumulated on the new page then
482 * wake them up.
483 */
484 if (PageWriteback(newpage))
485 end_page_writeback(newpage);
486}
Christoph Lameterb20a3502006-03-22 00:09:12 -0800487
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700488/************************************************************
489 * Migration functions
490 ***********************************************************/
491
492/* Always fail migration. Used for mappings that are not movable */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700493int fail_migrate_page(struct address_space *mapping,
494 struct page *newpage, struct page *page)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700495{
496 return -EIO;
497}
498EXPORT_SYMBOL(fail_migrate_page);
499
Christoph Lameterb20a3502006-03-22 00:09:12 -0800500/*
501 * Common logic to directly migrate a single page suitable for
David Howells266cf652009-04-03 16:42:36 +0100502 * pages that do not use PagePrivate/PagePrivate2.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800503 *
504 * Pages are locked upon entry and exit.
505 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700506int migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800507 struct page *newpage, struct page *page,
508 enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800509{
510 int rc;
511
512 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
513
Mel Gormana6bc32b2012-01-12 17:19:43 -0800514 rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800515
516 if (rc)
517 return rc;
518
519 migrate_page_copy(newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800520 return 0;
521}
522EXPORT_SYMBOL(migrate_page);
523
David Howells93614012006-09-30 20:45:40 +0200524#ifdef CONFIG_BLOCK
Christoph Lameterb20a3502006-03-22 00:09:12 -0800525/*
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700526 * Migration function for pages with buffers. This function can only be used
527 * if the underlying filesystem guarantees that no other references to "page"
528 * exist.
529 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700530int buffer_migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800531 struct page *newpage, struct page *page, enum migrate_mode mode)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700532{
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700533 struct buffer_head *bh, *head;
534 int rc;
535
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700536 if (!page_has_buffers(page))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800537 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700538
539 head = page_buffers(page);
540
Mel Gormana6bc32b2012-01-12 17:19:43 -0800541 rc = migrate_page_move_mapping(mapping, newpage, page, head, mode);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700542
543 if (rc)
544 return rc;
545
Mel Gormanb969c4a2012-01-12 17:19:34 -0800546 /*
547 * In the async case, migrate_page_move_mapping locked the buffers
548 * with an IRQ-safe spinlock held. In the sync case, the buffers
549 * need to be locked now
550 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800551 if (mode != MIGRATE_ASYNC)
552 BUG_ON(!buffer_migrate_lock_buffers(head, mode));
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700553
554 ClearPagePrivate(page);
555 set_page_private(newpage, page_private(page));
556 set_page_private(page, 0);
557 put_page(page);
558 get_page(newpage);
559
560 bh = head;
561 do {
562 set_bh_page(bh, newpage, bh_offset(bh));
563 bh = bh->b_this_page;
564
565 } while (bh != head);
566
567 SetPagePrivate(newpage);
568
569 migrate_page_copy(newpage, page);
570
571 bh = head;
572 do {
573 unlock_buffer(bh);
574 put_bh(bh);
575 bh = bh->b_this_page;
576
577 } while (bh != head);
578
579 return 0;
580}
581EXPORT_SYMBOL(buffer_migrate_page);
David Howells93614012006-09-30 20:45:40 +0200582#endif
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700583
Christoph Lameter04e62a22006-06-23 02:03:38 -0700584/*
585 * Writeback a page to clean the dirty state
586 */
587static int writeout(struct address_space *mapping, struct page *page)
588{
589 struct writeback_control wbc = {
590 .sync_mode = WB_SYNC_NONE,
591 .nr_to_write = 1,
592 .range_start = 0,
593 .range_end = LLONG_MAX,
Christoph Lameter04e62a22006-06-23 02:03:38 -0700594 .for_reclaim = 1
595 };
596 int rc;
597
598 if (!mapping->a_ops->writepage)
599 /* No write method for the address space */
600 return -EINVAL;
601
602 if (!clear_page_dirty_for_io(page))
603 /* Someone else already triggered a write */
604 return -EAGAIN;
605
606 /*
607 * A dirty page may imply that the underlying filesystem has
608 * the page on some queue. So the page must be clean for
609 * migration. Writeout may mean we loose the lock and the
610 * page state is no longer what we checked for earlier.
611 * At this point we know that the migration attempt cannot
612 * be successful.
613 */
614 remove_migration_ptes(page, page);
615
616 rc = mapping->a_ops->writepage(page, &wbc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700617
618 if (rc != AOP_WRITEPAGE_ACTIVATE)
619 /* unlocked. Relock */
620 lock_page(page);
621
Hugh Dickinsbda85502008-11-19 15:36:36 -0800622 return (rc < 0) ? -EIO : -EAGAIN;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700623}
624
625/*
626 * Default handling if a filesystem does not provide a migration function.
627 */
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700628static int fallback_migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800629 struct page *newpage, struct page *page, enum migrate_mode mode)
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700630{
Mel Gormanb969c4a2012-01-12 17:19:34 -0800631 if (PageDirty(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800632 /* Only writeback pages in full synchronous migration */
633 if (mode != MIGRATE_SYNC)
Mel Gormanb969c4a2012-01-12 17:19:34 -0800634 return -EBUSY;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700635 return writeout(mapping, page);
Mel Gormanb969c4a2012-01-12 17:19:34 -0800636 }
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700637
638 /*
639 * Buffers may be managed in a filesystem specific way.
640 * We must have no buffers or drop them.
641 */
David Howells266cf652009-04-03 16:42:36 +0100642 if (page_has_private(page) &&
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700643 !try_to_release_page(page, GFP_KERNEL))
644 return -EAGAIN;
645
Mel Gormana6bc32b2012-01-12 17:19:43 -0800646 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700647}
648
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700649/*
Christoph Lametere24f0b82006-06-23 02:03:51 -0700650 * Move a page to a newly allocated page
651 * The page is locked and all ptes have been successfully removed.
652 *
653 * The new page will have replaced the old page if this function
654 * is successful.
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700655 *
656 * Return value:
657 * < 0 - error code
658 * == 0 - success
Christoph Lametere24f0b82006-06-23 02:03:51 -0700659 */
Mel Gorman3fe20112010-05-24 14:32:20 -0700660static int move_to_new_page(struct page *newpage, struct page *page,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800661 int remap_swapcache, enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700662{
663 struct address_space *mapping;
664 int rc;
665
666 /*
667 * Block others from accessing the page when we get around to
668 * establishing additional references. We are the only one
669 * holding a reference to the new page at this point.
670 */
Nick Piggin529ae9a2008-08-02 12:01:03 +0200671 if (!trylock_page(newpage))
Christoph Lametere24f0b82006-06-23 02:03:51 -0700672 BUG();
673
674 /* Prepare mapping for the new page.*/
675 newpage->index = page->index;
676 newpage->mapping = page->mapping;
Rik van Rielb2e18532008-10-18 20:26:30 -0700677 if (PageSwapBacked(page))
678 SetPageSwapBacked(newpage);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700679
680 mapping = page_mapping(page);
681 if (!mapping)
Mel Gormana6bc32b2012-01-12 17:19:43 -0800682 rc = migrate_page(mapping, newpage, page, mode);
Mel Gormanb969c4a2012-01-12 17:19:34 -0800683 else if (mapping->a_ops->migratepage)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700684 /*
Mel Gormanb969c4a2012-01-12 17:19:34 -0800685 * Most pages have a mapping and most filesystems provide a
686 * migratepage callback. Anonymous pages are part of swap
687 * space which also has its own migratepage callback. This
688 * is the most common path for page migration.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700689 */
Mel Gormanb969c4a2012-01-12 17:19:34 -0800690 rc = mapping->a_ops->migratepage(mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800691 newpage, page, mode);
Mel Gormanb969c4a2012-01-12 17:19:34 -0800692 else
Mel Gormana6bc32b2012-01-12 17:19:43 -0800693 rc = fallback_migrate_page(mapping, newpage, page, mode);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700694
Mel Gorman3fe20112010-05-24 14:32:20 -0700695 if (rc) {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700696 newpage->mapping = NULL;
Mel Gorman3fe20112010-05-24 14:32:20 -0700697 } else {
698 if (remap_swapcache)
699 remove_migration_ptes(page, newpage);
Konstantin Khlebnikov35512ec2012-02-03 15:37:13 -0800700 page->mapping = NULL;
Mel Gorman3fe20112010-05-24 14:32:20 -0700701 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700702
703 unlock_page(newpage);
704
705 return rc;
706}
707
Minchan Kim0dabec92011-10-31 17:06:57 -0700708static int __unmap_and_move(struct page *page, struct page *newpage,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800709 int force, bool offlining, enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700710{
Minchan Kim0dabec92011-10-31 17:06:57 -0700711 int rc = -EAGAIN;
Mel Gorman3fe20112010-05-24 14:32:20 -0700712 int remap_swapcache = 1;
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800713 int charge = 0;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -0700714 struct mem_cgroup *mem;
Mel Gorman3f6c8272010-05-24 14:32:17 -0700715 struct anon_vma *anon_vma = NULL;
Christoph Lameter95a402c2006-06-23 02:03:53 -0700716
Nick Piggin529ae9a2008-08-02 12:01:03 +0200717 if (!trylock_page(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800718 if (!force || mode == MIGRATE_ASYNC)
Minchan Kim0dabec92011-10-31 17:06:57 -0700719 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -0800720
721 /*
722 * It's not safe for direct compaction to call lock_page.
723 * For example, during page readahead pages are added locked
724 * to the LRU. Later, when the IO completes the pages are
725 * marked uptodate and unlocked. However, the queueing
726 * could be merging multiple pages for one bio (e.g.
727 * mpage_readpages). If an allocation happens for the
728 * second or third page, the process can end up locking
729 * the same page twice and deadlocking. Rather than
730 * trying to be clever about what pages can be locked,
731 * avoid the use of lock_page for direct compaction
732 * altogether.
733 */
734 if (current->flags & PF_MEMALLOC)
Minchan Kim0dabec92011-10-31 17:06:57 -0700735 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -0800736
Christoph Lametere24f0b82006-06-23 02:03:51 -0700737 lock_page(page);
738 }
739
Hugh Dickins62b61f62009-12-14 17:59:33 -0800740 /*
741 * Only memory hotplug's offline_pages() caller has locked out KSM,
742 * and can safely migrate a KSM page. The other cases have skipped
743 * PageKsm along with PageReserved - but it is only now when we have
744 * the page lock that we can be certain it will not go KSM beneath us
745 * (KSM will not upgrade a page from PageAnon to PageKsm when it sees
746 * its pagecount raised, but only here do we take the page lock which
747 * serializes that).
748 */
749 if (PageKsm(page) && !offlining) {
750 rc = -EBUSY;
751 goto unlock;
752 }
753
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800754 /* charge against new page */
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700755 charge = mem_cgroup_prepare_migration(page, newpage, &mem, GFP_KERNEL);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800756 if (charge == -ENOMEM) {
757 rc = -ENOMEM;
758 goto unlock;
759 }
760 BUG_ON(charge);
761
Christoph Lametere24f0b82006-06-23 02:03:51 -0700762 if (PageWriteback(page)) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700763 /*
Mel Gormana6bc32b2012-01-12 17:19:43 -0800764 * Only in the case of a full syncronous migration is it
765 * necessary to wait for PageWriteback. In the async case,
766 * the retry loop is too short and in the sync-light case,
767 * the overhead of stalling is too much
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700768 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800769 if (mode != MIGRATE_SYNC) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700770 rc = -EBUSY;
771 goto uncharge;
772 }
773 if (!force)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800774 goto uncharge;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700775 wait_on_page_writeback(page);
776 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700777 /*
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700778 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
779 * we cannot notice that anon_vma is freed while we migrates a page.
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800780 * This get_anon_vma() delays freeing anon_vma pointer until the end
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700781 * of migration. File cache pages are no problem because of page_lock()
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700782 * File Caches may use write_page() or lock_page() in migration, then,
783 * just care Anon page here.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700784 */
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700785 if (PageAnon(page)) {
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800786 /*
787 * Only page_lock_anon_vma() understands the subtleties of
788 * getting a hold on an anon_vma from outside one of its mms.
789 */
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700790 anon_vma = page_get_anon_vma(page);
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800791 if (anon_vma) {
792 /*
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700793 * Anon page
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800794 */
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800795 } else if (PageSwapCache(page)) {
Mel Gorman3fe20112010-05-24 14:32:20 -0700796 /*
797 * We cannot be sure that the anon_vma of an unmapped
798 * swapcache page is safe to use because we don't
799 * know in advance if the VMA that this page belonged
800 * to still exists. If the VMA and others sharing the
801 * data have been freed, then the anon_vma could
802 * already be invalid.
803 *
804 * To avoid this possibility, swapcache pages get
805 * migrated but are not remapped when migration
806 * completes
807 */
808 remap_swapcache = 0;
809 } else {
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800810 goto uncharge;
Mel Gorman3fe20112010-05-24 14:32:20 -0700811 }
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700812 }
Shaohua Li62e1c552008-02-04 22:29:33 -0800813
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700814 /*
Shaohua Li62e1c552008-02-04 22:29:33 -0800815 * Corner case handling:
816 * 1. When a new swap-cache page is read into, it is added to the LRU
817 * and treated as swapcache but it has no rmap yet.
818 * Calling try_to_unmap() against a page->mapping==NULL page will
819 * trigger a BUG. So handle it here.
820 * 2. An orphaned page (see truncate_complete_page) might have
821 * fs-private metadata. The page can be picked up due to memory
822 * offlining. Everywhere else except page reclaim, the page is
823 * invisible to the vm, so the page can not be migrated. So try to
824 * free the metadata, so the page can be freed.
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700825 */
Shaohua Li62e1c552008-02-04 22:29:33 -0800826 if (!page->mapping) {
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800827 VM_BUG_ON(PageAnon(page));
828 if (page_has_private(page)) {
Shaohua Li62e1c552008-02-04 22:29:33 -0800829 try_to_free_buffers(page);
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800830 goto uncharge;
Shaohua Li62e1c552008-02-04 22:29:33 -0800831 }
Shaohua Liabfc3482009-09-21 17:01:19 -0700832 goto skip_unmap;
Shaohua Li62e1c552008-02-04 22:29:33 -0800833 }
834
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700835 /* Establish migration ptes or remove ptes */
Andi Kleen14fa31b2009-09-16 11:50:10 +0200836 try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700837
Shaohua Liabfc3482009-09-21 17:01:19 -0700838skip_unmap:
Christoph Lametere6a15302006-06-25 05:46:49 -0700839 if (!page_mapped(page))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800840 rc = move_to_new_page(newpage, page, remap_swapcache, mode);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700841
Mel Gorman3fe20112010-05-24 14:32:20 -0700842 if (rc && remap_swapcache)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700843 remove_migration_ptes(page, page);
Mel Gorman3f6c8272010-05-24 14:32:17 -0700844
845 /* Drop an anon_vma reference if we took one */
Rik van Riel76545062010-08-09 17:18:41 -0700846 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -0700847 put_anon_vma(anon_vma);
Mel Gorman3f6c8272010-05-24 14:32:17 -0700848
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800849uncharge:
850 if (!charge)
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -0800851 mem_cgroup_end_migration(mem, page, newpage, rc == 0);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700852unlock:
853 unlock_page(page);
Minchan Kim0dabec92011-10-31 17:06:57 -0700854out:
855 return rc;
856}
Christoph Lameter95a402c2006-06-23 02:03:53 -0700857
Minchan Kim0dabec92011-10-31 17:06:57 -0700858/*
859 * Obtain the lock on page, remove all ptes and migrate the page
860 * to the newly allocated page in newpage.
861 */
862static int unmap_and_move(new_page_t get_new_page, unsigned long private,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800863 struct page *page, int force, bool offlining,
864 enum migrate_mode mode)
Minchan Kim0dabec92011-10-31 17:06:57 -0700865{
866 int rc = 0;
867 int *result = NULL;
868 struct page *newpage = get_new_page(page, private, &result);
869
870 if (!newpage)
871 return -ENOMEM;
872
873 if (page_count(page) == 1) {
874 /* page was freed from under us. So we are done. */
875 goto out;
876 }
877
878 if (unlikely(PageTransHuge(page)))
879 if (unlikely(split_huge_page(page)))
880 goto out;
881
Mel Gormana6bc32b2012-01-12 17:19:43 -0800882 rc = __unmap_and_move(page, newpage, force, offlining, mode);
Minchan Kim0dabec92011-10-31 17:06:57 -0700883out:
Christoph Lametere24f0b82006-06-23 02:03:51 -0700884 if (rc != -EAGAIN) {
Minchan Kim0dabec92011-10-31 17:06:57 -0700885 /*
886 * A page that has been migrated has all references
887 * removed and will be freed. A page that has not been
888 * migrated will have kepts its references and be
889 * restored.
890 */
891 list_del(&page->lru);
KOSAKI Motohiroa7312862009-09-21 17:01:37 -0700892 dec_zone_page_state(page, NR_ISOLATED_ANON +
Johannes Weiner6c0b1352009-09-21 17:02:59 -0700893 page_is_file_cache(page));
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700894 putback_lru_page(page);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700895 }
Christoph Lameter95a402c2006-06-23 02:03:53 -0700896 /*
897 * Move the new page to the LRU. If migration was not successful
898 * then this will free the page.
899 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700900 putback_lru_page(newpage);
Christoph Lameter742755a2006-06-23 02:03:55 -0700901 if (result) {
902 if (rc)
903 *result = rc;
904 else
905 *result = page_to_nid(newpage);
906 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700907 return rc;
908}
909
910/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900911 * Counterpart of unmap_and_move_page() for hugepage migration.
912 *
913 * This function doesn't wait the completion of hugepage I/O
914 * because there is no race between I/O and migration for hugepage.
915 * Note that currently hugepage I/O occurs only in direct I/O
916 * where no lock is held and PG_writeback is irrelevant,
917 * and writeback status of all subpages are counted in the reference
918 * count of the head page (i.e. if all subpages of a 2MB hugepage are
919 * under direct I/O, the reference of the head page is 512 and a bit more.)
920 * This means that when we try to migrate hugepage whose subpages are
921 * doing direct I/O, some references remain after try_to_unmap() and
922 * hugepage migration fails without data corruption.
923 *
924 * There is also no race when direct I/O is issued on the page under migration,
925 * because then pte is replaced with migration swap entry and direct I/O code
926 * will wait in the page fault for migration to complete.
927 */
928static int unmap_and_move_huge_page(new_page_t get_new_page,
929 unsigned long private, struct page *hpage,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800930 int force, bool offlining,
931 enum migrate_mode mode)
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900932{
933 int rc = 0;
934 int *result = NULL;
935 struct page *new_hpage = get_new_page(hpage, private, &result);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900936 struct anon_vma *anon_vma = NULL;
937
938 if (!new_hpage)
939 return -ENOMEM;
940
941 rc = -EAGAIN;
942
943 if (!trylock_page(hpage)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800944 if (!force || mode != MIGRATE_SYNC)
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900945 goto out;
946 lock_page(hpage);
947 }
948
Peter Zijlstra746b18d2011-05-24 17:12:10 -0700949 if (PageAnon(hpage))
950 anon_vma = page_get_anon_vma(hpage);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900951
952 try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
953
954 if (!page_mapped(hpage))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800955 rc = move_to_new_page(new_hpage, hpage, 1, mode);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900956
957 if (rc)
958 remove_migration_ptes(hpage, hpage);
959
Hugh Dickinsfd4a4662011-01-13 15:47:31 -0800960 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -0700961 put_anon_vma(anon_vma);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900962 unlock_page(hpage);
963
Hillf Danton09761332011-12-08 14:34:20 -0800964out:
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900965 if (rc != -EAGAIN) {
966 list_del(&hpage->lru);
967 put_page(hpage);
968 }
969
970 put_page(new_hpage);
971
972 if (result) {
973 if (rc)
974 *result = rc;
975 else
976 *result = page_to_nid(new_hpage);
977 }
978 return rc;
979}
980
981/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800982 * migrate_pages
983 *
Christoph Lameter95a402c2006-06-23 02:03:53 -0700984 * The function takes one list of pages to migrate and a function
985 * that determines from the page to be migrated and the private data
986 * the target of the move and allocates the page.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800987 *
988 * The function returns after 10 attempts or if no pages
989 * are movable anymore because to has become empty
Minchan Kimcf608ac2010-10-26 14:21:29 -0700990 * or no retryable pages exist anymore.
991 * Caller should call putback_lru_pages to return pages to the LRU
Minchan Kim28bd6572011-01-25 15:07:26 -0800992 * or free list only if ret != 0.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800993 *
Christoph Lameter95a402c2006-06-23 02:03:53 -0700994 * Return: Number of pages not migrated or error code.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800995 */
Christoph Lameter95a402c2006-06-23 02:03:53 -0700996int migrate_pages(struct list_head *from,
Mel Gorman7f0f2492011-01-13 15:45:58 -0800997 new_page_t get_new_page, unsigned long private, bool offlining,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800998 enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800999{
Christoph Lametere24f0b82006-06-23 02:03:51 -07001000 int retry = 1;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001001 int nr_failed = 0;
Mel Gormancd9bb9b2012-10-19 10:46:20 +01001002 int nr_succeeded = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001003 int pass = 0;
1004 struct page *page;
1005 struct page *page2;
1006 int swapwrite = current->flags & PF_SWAPWRITE;
1007 int rc;
1008
Liam Markcc2d4bd2013-01-16 10:14:40 -08001009 trace_migrate_pages_start(mode);
Christoph Lameterb20a3502006-03-22 00:09:12 -08001010 if (!swapwrite)
1011 current->flags |= PF_SWAPWRITE;
1012
Christoph Lametere24f0b82006-06-23 02:03:51 -07001013 for(pass = 0; pass < 10 && retry; pass++) {
1014 retry = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001015
Christoph Lametere24f0b82006-06-23 02:03:51 -07001016 list_for_each_entry_safe(page, page2, from, lru) {
Christoph Lametere24f0b82006-06-23 02:03:51 -07001017 cond_resched();
Christoph Lameterb20a3502006-03-22 00:09:12 -08001018
Christoph Lameter95a402c2006-06-23 02:03:53 -07001019 rc = unmap_and_move(get_new_page, private,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001020 page, pass > 2, offlining,
Mel Gormana6bc32b2012-01-12 17:19:43 -08001021 mode);
Christoph Lameterb20a3502006-03-22 00:09:12 -08001022
Christoph Lametere24f0b82006-06-23 02:03:51 -07001023 switch(rc) {
Christoph Lameter95a402c2006-06-23 02:03:53 -07001024 case -ENOMEM:
1025 goto out;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001026 case -EAGAIN:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001027 retry++;
Liam Markcc2d4bd2013-01-16 10:14:40 -08001028 trace_migrate_retry(retry);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001029 break;
1030 case 0:
Mel Gormancd9bb9b2012-10-19 10:46:20 +01001031 nr_succeeded++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001032 break;
1033 default:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001034 /* Permanent failure */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001035 nr_failed++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001036 break;
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001037 }
Christoph Lameterb20a3502006-03-22 00:09:12 -08001038 }
1039 }
Christoph Lameter95a402c2006-06-23 02:03:53 -07001040 rc = 0;
1041out:
Mel Gormancd9bb9b2012-10-19 10:46:20 +01001042 if (nr_succeeded)
1043 count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
1044 if (nr_failed)
1045 count_vm_events(PGMIGRATE_FAIL, nr_failed);
Christoph Lameterb20a3502006-03-22 00:09:12 -08001046 if (!swapwrite)
1047 current->flags &= ~PF_SWAPWRITE;
1048
Liam Markcc2d4bd2013-01-16 10:14:40 -08001049 trace_migrate_pages_end(mode);
Christoph Lameter95a402c2006-06-23 02:03:53 -07001050 if (rc)
1051 return rc;
1052
Christoph Lameterb20a3502006-03-22 00:09:12 -08001053 return nr_failed + retry;
1054}
1055
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001056int migrate_huge_pages(struct list_head *from,
Mel Gorman7f0f2492011-01-13 15:45:58 -08001057 new_page_t get_new_page, unsigned long private, bool offlining,
Mel Gormana6bc32b2012-01-12 17:19:43 -08001058 enum migrate_mode mode)
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001059{
1060 int retry = 1;
1061 int nr_failed = 0;
1062 int pass = 0;
1063 struct page *page;
1064 struct page *page2;
1065 int rc;
1066
1067 for (pass = 0; pass < 10 && retry; pass++) {
1068 retry = 0;
1069
1070 list_for_each_entry_safe(page, page2, from, lru) {
1071 cond_resched();
1072
1073 rc = unmap_and_move_huge_page(get_new_page,
Mel Gorman77f1fe62011-01-13 15:45:57 -08001074 private, page, pass > 2, offlining,
Mel Gormana6bc32b2012-01-12 17:19:43 -08001075 mode);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001076
1077 switch(rc) {
1078 case -ENOMEM:
1079 goto out;
1080 case -EAGAIN:
1081 retry++;
1082 break;
1083 case 0:
1084 break;
1085 default:
1086 /* Permanent failure */
1087 nr_failed++;
1088 break;
1089 }
1090 }
1091 }
1092 rc = 0;
1093out:
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001094 if (rc)
1095 return rc;
1096
1097 return nr_failed + retry;
1098}
1099
Christoph Lameter742755a2006-06-23 02:03:55 -07001100#ifdef CONFIG_NUMA
1101/*
1102 * Move a list of individual pages
1103 */
1104struct page_to_node {
1105 unsigned long addr;
1106 struct page *page;
1107 int node;
1108 int status;
1109};
1110
1111static struct page *new_page_node(struct page *p, unsigned long private,
1112 int **result)
1113{
1114 struct page_to_node *pm = (struct page_to_node *)private;
1115
1116 while (pm->node != MAX_NUMNODES && pm->page != p)
1117 pm++;
1118
1119 if (pm->node == MAX_NUMNODES)
1120 return NULL;
1121
1122 *result = &pm->status;
1123
Mel Gorman6484eb32009-06-16 15:31:54 -07001124 return alloc_pages_exact_node(pm->node,
Mel Gorman769848c2007-07-17 04:03:05 -07001125 GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
Christoph Lameter742755a2006-06-23 02:03:55 -07001126}
1127
1128/*
1129 * Move a set of pages as indicated in the pm array. The addr
1130 * field must be set to the virtual address of the page to be moved
1131 * and the node number must contain a valid target node.
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001132 * The pm array ends with node = MAX_NUMNODES.
Christoph Lameter742755a2006-06-23 02:03:55 -07001133 */
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001134static int do_move_page_to_node_array(struct mm_struct *mm,
1135 struct page_to_node *pm,
1136 int migrate_all)
Christoph Lameter742755a2006-06-23 02:03:55 -07001137{
1138 int err;
1139 struct page_to_node *pp;
1140 LIST_HEAD(pagelist);
1141
1142 down_read(&mm->mmap_sem);
1143
1144 /*
1145 * Build a list of pages to migrate
1146 */
Christoph Lameter742755a2006-06-23 02:03:55 -07001147 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
1148 struct vm_area_struct *vma;
1149 struct page *page;
1150
Christoph Lameter742755a2006-06-23 02:03:55 -07001151 err = -EFAULT;
1152 vma = find_vma(mm, pp->addr);
Gleb Natapov70384dc2010-10-26 14:22:07 -07001153 if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
Christoph Lameter742755a2006-06-23 02:03:55 -07001154 goto set_status;
1155
Andrea Arcangeli500d65d2011-01-13 15:46:55 -08001156 page = follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001157
1158 err = PTR_ERR(page);
1159 if (IS_ERR(page))
1160 goto set_status;
1161
Christoph Lameter742755a2006-06-23 02:03:55 -07001162 err = -ENOENT;
1163 if (!page)
1164 goto set_status;
1165
Hugh Dickins62b61f62009-12-14 17:59:33 -08001166 /* Use PageReserved to check for zero page */
1167 if (PageReserved(page) || PageKsm(page))
Christoph Lameter742755a2006-06-23 02:03:55 -07001168 goto put_and_set;
1169
1170 pp->page = page;
1171 err = page_to_nid(page);
1172
1173 if (err == pp->node)
1174 /*
1175 * Node already in the right place
1176 */
1177 goto put_and_set;
1178
1179 err = -EACCES;
1180 if (page_mapcount(page) > 1 &&
1181 !migrate_all)
1182 goto put_and_set;
1183
Nick Piggin62695a82008-10-18 20:26:09 -07001184 err = isolate_lru_page(page);
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001185 if (!err) {
Nick Piggin62695a82008-10-18 20:26:09 -07001186 list_add_tail(&page->lru, &pagelist);
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001187 inc_zone_page_state(page, NR_ISOLATED_ANON +
1188 page_is_file_cache(page));
1189 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001190put_and_set:
1191 /*
1192 * Either remove the duplicate refcount from
1193 * isolate_lru_page() or drop the page ref if it was
1194 * not isolated.
1195 */
1196 put_page(page);
1197set_status:
1198 pp->status = err;
1199 }
1200
Brice Gogline78bbfa2008-10-18 20:27:15 -07001201 err = 0;
Minchan Kimcf608ac2010-10-26 14:21:29 -07001202 if (!list_empty(&pagelist)) {
Christoph Lameter742755a2006-06-23 02:03:55 -07001203 err = migrate_pages(&pagelist, new_page_node,
Mel Gormana6bc32b2012-01-12 17:19:43 -08001204 (unsigned long)pm, 0, MIGRATE_SYNC);
Minchan Kimcf608ac2010-10-26 14:21:29 -07001205 if (err)
1206 putback_lru_pages(&pagelist);
1207 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001208
1209 up_read(&mm->mmap_sem);
1210 return err;
1211}
1212
1213/*
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001214 * Migrate an array of page address onto an array of nodes and fill
1215 * the corresponding array of status.
1216 */
Christoph Lameter3268c632012-03-21 16:34:06 -07001217static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001218 unsigned long nr_pages,
1219 const void __user * __user *pages,
1220 const int __user *nodes,
1221 int __user *status, int flags)
1222{
Brice Goglin3140a222009-01-06 14:38:57 -08001223 struct page_to_node *pm;
Brice Goglin3140a222009-01-06 14:38:57 -08001224 unsigned long chunk_nr_pages;
1225 unsigned long chunk_start;
1226 int err;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001227
Brice Goglin3140a222009-01-06 14:38:57 -08001228 err = -ENOMEM;
1229 pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
1230 if (!pm)
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001231 goto out;
Brice Goglin35282a22009-06-16 15:32:43 -07001232
1233 migrate_prep();
1234
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001235 /*
Brice Goglin3140a222009-01-06 14:38:57 -08001236 * Store a chunk of page_to_node array in a page,
1237 * but keep the last one as a marker
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001238 */
Brice Goglin3140a222009-01-06 14:38:57 -08001239 chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001240
Brice Goglin3140a222009-01-06 14:38:57 -08001241 for (chunk_start = 0;
1242 chunk_start < nr_pages;
1243 chunk_start += chunk_nr_pages) {
1244 int j;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001245
Brice Goglin3140a222009-01-06 14:38:57 -08001246 if (chunk_start + chunk_nr_pages > nr_pages)
1247 chunk_nr_pages = nr_pages - chunk_start;
1248
1249 /* fill the chunk pm with addrs and nodes from user-space */
1250 for (j = 0; j < chunk_nr_pages; j++) {
1251 const void __user *p;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001252 int node;
1253
Brice Goglin3140a222009-01-06 14:38:57 -08001254 err = -EFAULT;
1255 if (get_user(p, pages + j + chunk_start))
1256 goto out_pm;
1257 pm[j].addr = (unsigned long) p;
1258
1259 if (get_user(node, nodes + j + chunk_start))
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001260 goto out_pm;
1261
1262 err = -ENODEV;
Linus Torvalds6f5a55f2010-02-05 16:16:50 -08001263 if (node < 0 || node >= MAX_NUMNODES)
1264 goto out_pm;
1265
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001266 if (!node_state(node, N_HIGH_MEMORY))
1267 goto out_pm;
1268
1269 err = -EACCES;
1270 if (!node_isset(node, task_nodes))
1271 goto out_pm;
1272
Brice Goglin3140a222009-01-06 14:38:57 -08001273 pm[j].node = node;
1274 }
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001275
Brice Goglin3140a222009-01-06 14:38:57 -08001276 /* End marker for this chunk */
1277 pm[chunk_nr_pages].node = MAX_NUMNODES;
1278
1279 /* Migrate this chunk */
1280 err = do_move_page_to_node_array(mm, pm,
1281 flags & MPOL_MF_MOVE_ALL);
1282 if (err < 0)
1283 goto out_pm;
1284
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001285 /* Return status information */
Brice Goglin3140a222009-01-06 14:38:57 -08001286 for (j = 0; j < chunk_nr_pages; j++)
1287 if (put_user(pm[j].status, status + j + chunk_start)) {
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001288 err = -EFAULT;
Brice Goglin3140a222009-01-06 14:38:57 -08001289 goto out_pm;
1290 }
1291 }
1292 err = 0;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001293
1294out_pm:
Brice Goglin3140a222009-01-06 14:38:57 -08001295 free_page((unsigned long)pm);
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001296out:
1297 return err;
1298}
1299
1300/*
Brice Goglin2f007e72008-10-18 20:27:16 -07001301 * Determine the nodes of an array of pages and store it in an array of status.
Christoph Lameter742755a2006-06-23 02:03:55 -07001302 */
Brice Goglin80bba122008-12-09 13:14:23 -08001303static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
1304 const void __user **pages, int *status)
Christoph Lameter742755a2006-06-23 02:03:55 -07001305{
Brice Goglin2f007e72008-10-18 20:27:16 -07001306 unsigned long i;
Brice Goglin2f007e72008-10-18 20:27:16 -07001307
Christoph Lameter742755a2006-06-23 02:03:55 -07001308 down_read(&mm->mmap_sem);
1309
Brice Goglin2f007e72008-10-18 20:27:16 -07001310 for (i = 0; i < nr_pages; i++) {
Brice Goglin80bba122008-12-09 13:14:23 -08001311 unsigned long addr = (unsigned long)(*pages);
Christoph Lameter742755a2006-06-23 02:03:55 -07001312 struct vm_area_struct *vma;
1313 struct page *page;
KOSAKI Motohiroc095adb2008-12-16 16:06:43 +09001314 int err = -EFAULT;
Brice Goglin2f007e72008-10-18 20:27:16 -07001315
1316 vma = find_vma(mm, addr);
Gleb Natapov70384dc2010-10-26 14:22:07 -07001317 if (!vma || addr < vma->vm_start)
Christoph Lameter742755a2006-06-23 02:03:55 -07001318 goto set_status;
1319
Brice Goglin2f007e72008-10-18 20:27:16 -07001320 page = follow_page(vma, addr, 0);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001321
1322 err = PTR_ERR(page);
1323 if (IS_ERR(page))
1324 goto set_status;
1325
Christoph Lameter742755a2006-06-23 02:03:55 -07001326 err = -ENOENT;
1327 /* Use PageReserved to check for zero page */
Hugh Dickins62b61f62009-12-14 17:59:33 -08001328 if (!page || PageReserved(page) || PageKsm(page))
Christoph Lameter742755a2006-06-23 02:03:55 -07001329 goto set_status;
1330
1331 err = page_to_nid(page);
1332set_status:
Brice Goglin80bba122008-12-09 13:14:23 -08001333 *status = err;
1334
1335 pages++;
1336 status++;
1337 }
1338
1339 up_read(&mm->mmap_sem);
1340}
1341
1342/*
1343 * Determine the nodes of a user array of pages and store it in
1344 * a user array of status.
1345 */
1346static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1347 const void __user * __user *pages,
1348 int __user *status)
1349{
1350#define DO_PAGES_STAT_CHUNK_NR 16
1351 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1352 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
Brice Goglin80bba122008-12-09 13:14:23 -08001353
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001354 while (nr_pages) {
1355 unsigned long chunk_nr;
Brice Goglin80bba122008-12-09 13:14:23 -08001356
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001357 chunk_nr = nr_pages;
1358 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1359 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1360
1361 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
1362 break;
Brice Goglin80bba122008-12-09 13:14:23 -08001363
1364 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1365
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001366 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1367 break;
Christoph Lameter742755a2006-06-23 02:03:55 -07001368
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001369 pages += chunk_nr;
1370 status += chunk_nr;
1371 nr_pages -= chunk_nr;
1372 }
1373 return nr_pages ? -EFAULT : 0;
Christoph Lameter742755a2006-06-23 02:03:55 -07001374}
1375
1376/*
1377 * Move a list of pages in the address space of the currently executing
1378 * process.
1379 */
Heiko Carstens938bb9f2009-01-14 14:14:30 +01001380SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1381 const void __user * __user *, pages,
1382 const int __user *, nodes,
1383 int __user *, status, int, flags)
Christoph Lameter742755a2006-06-23 02:03:55 -07001384{
Christoph Lameter742755a2006-06-23 02:03:55 -07001385 struct task_struct *task;
Christoph Lameter742755a2006-06-23 02:03:55 -07001386 struct mm_struct *mm;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001387 int err;
Christoph Lameter3268c632012-03-21 16:34:06 -07001388 nodemask_t task_nodes;
Christoph Lameter742755a2006-06-23 02:03:55 -07001389
1390 /* Check flags */
1391 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1392 return -EINVAL;
1393
1394 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1395 return -EPERM;
1396
1397 /* Find the mm_struct */
Greg Thelena879bf52011-02-25 14:44:13 -08001398 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07001399 task = pid ? find_task_by_vpid(pid) : current;
Christoph Lameter742755a2006-06-23 02:03:55 -07001400 if (!task) {
Greg Thelena879bf52011-02-25 14:44:13 -08001401 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001402 return -ESRCH;
1403 }
Christoph Lameter3268c632012-03-21 16:34:06 -07001404 get_task_struct(task);
Christoph Lameter742755a2006-06-23 02:03:55 -07001405
1406 /*
1407 * Check if this process has the right to modify the specified
Linus Torvalds4c497362017-08-20 13:26:27 -07001408 * process. Use the regular "ptrace_may_access()" checks.
Christoph Lameter742755a2006-06-23 02:03:55 -07001409 */
Linus Torvalds4c497362017-08-20 13:26:27 -07001410 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001411 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001412 err = -EPERM;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001413 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001414 }
David Howellsc69e8d92008-11-14 10:39:19 +11001415 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001416
David Quigley86c3a762006-06-23 02:04:02 -07001417 err = security_task_movememory(task);
1418 if (err)
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001419 goto out;
David Quigley86c3a762006-06-23 02:04:02 -07001420
Christoph Lameter3268c632012-03-21 16:34:06 -07001421 task_nodes = cpuset_mems_allowed(task);
1422 mm = get_task_mm(task);
1423 put_task_struct(task);
1424
Sasha Levin6e8b09e2012-04-25 16:01:53 -07001425 if (!mm)
1426 return -EINVAL;
1427
1428 if (nodes)
1429 err = do_pages_move(mm, task_nodes, nr_pages, pages,
1430 nodes, status, flags);
1431 else
1432 err = do_pages_stat(mm, nr_pages, pages, status);
Christoph Lameter3268c632012-03-21 16:34:06 -07001433
1434 mmput(mm);
1435 return err;
David Quigley86c3a762006-06-23 02:04:02 -07001436
Christoph Lameter742755a2006-06-23 02:03:55 -07001437out:
Christoph Lameter3268c632012-03-21 16:34:06 -07001438 put_task_struct(task);
Christoph Lameter742755a2006-06-23 02:03:55 -07001439 return err;
1440}
Christoph Lameter742755a2006-06-23 02:03:55 -07001441
Christoph Lameter7b2259b2006-06-25 05:46:48 -07001442/*
1443 * Call migration functions in the vma_ops that may prepare
1444 * memory in a vm for migration. migration functions may perform
1445 * the migration for vmas that do not have an underlying page struct.
1446 */
1447int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
1448 const nodemask_t *from, unsigned long flags)
1449{
1450 struct vm_area_struct *vma;
1451 int err = 0;
1452
Daisuke Nishimura1001c9f2009-02-11 13:04:18 -08001453 for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
Christoph Lameter7b2259b2006-06-25 05:46:48 -07001454 if (vma->vm_ops && vma->vm_ops->migrate) {
1455 err = vma->vm_ops->migrate(vma, to, from, flags);
1456 if (err)
1457 break;
1458 }
1459 }
1460 return err;
1461}
Gerald Schaefer83d16742008-07-23 21:28:22 -07001462#endif