blob: 1088cef6ef8be0f0cd90ce476527abbaa631d9ab [file] [log] [blame]
Christoph Lameterb20a3502006-03-22 00:09:12 -08001/*
Hugh Dickins14e0f9b2015-11-05 18:49:43 -08002 * Memory Migration functionality - linux/mm/migrate.c
Christoph Lameterb20a3502006-03-22 00:09:12 -08003 *
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 Dickins42cb14b2015-11-05 18:50:05 -080033#include <linux/backing-dev.h>
Minchan Kimbda807d2016-07-26 15:23:05 -070034#include <linux/compaction.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>
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -070037#include <linux/hugetlb_cgroup.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/gfp.h>
Rafael Aquinibf6bddf12012-12-11 16:02:42 -080039#include <linux/balloon_compaction.h>
Mel Gormanf714f4f2013-12-18 17:08:33 -080040#include <linux/mmu_notifier.h>
Vladimir Davydov33c3fc72015-09-09 15:35:45 -070041#include <linux/page_idle.h>
Vlastimil Babkad435edc2016-03-15 14:56:15 -070042#include <linux/page_owner.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010043#include <linux/sched/mm.h>
Linus Torvalds197e7e52017-08-20 13:26:27 -070044#include <linux/ptrace.h>
Christoph Lameterb20a3502006-03-22 00:09:12 -080045
Michal Nazarewicz0d1836c2010-12-21 17:24:26 -080046#include <asm/tlbflush.h>
47
Mel Gorman7b2a2d42012-10-19 14:07:31 +010048#define CREATE_TRACE_POINTS
49#include <trace/events/migrate.h>
50
Christoph Lameterb20a3502006-03-22 00:09:12 -080051#include "internal.h"
52
Christoph Lameterb20a3502006-03-22 00:09:12 -080053/*
Christoph Lameter742755a2006-06-23 02:03:55 -070054 * migrate_prep() needs to be called before we start compiling a list of pages
Mel Gorman748446b2010-05-24 14:32:27 -070055 * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is
56 * undesirable, use migrate_prep_local()
Christoph Lameterb20a3502006-03-22 00:09:12 -080057 */
58int migrate_prep(void)
59{
Christoph Lameterb20a3502006-03-22 00:09:12 -080060 /*
61 * Clear the LRU lists so pages can be isolated.
62 * Note that pages may be moved off the LRU after we have
63 * drained them. Those pages will fail to migrate like other
64 * pages that may be busy.
65 */
66 lru_add_drain_all();
67
68 return 0;
69}
70
Mel Gorman748446b2010-05-24 14:32:27 -070071/* Do the necessary work of migrate_prep but not if it involves other CPUs */
72int migrate_prep_local(void)
73{
74 lru_add_drain();
75
76 return 0;
77}
78
Yisheng Xie9e5bcd62017-02-24 14:57:29 -080079int isolate_movable_page(struct page *page, isolate_mode_t mode)
Minchan Kimbda807d2016-07-26 15:23:05 -070080{
81 struct address_space *mapping;
82
83 /*
84 * Avoid burning cycles with pages that are yet under __free_pages(),
85 * or just got freed under us.
86 *
87 * In case we 'win' a race for a movable page being freed under us and
88 * raise its refcount preventing __free_pages() from doing its job
89 * the put_page() at the end of this block will take care of
90 * release this page, thus avoiding a nasty leakage.
91 */
92 if (unlikely(!get_page_unless_zero(page)))
93 goto out;
94
95 /*
96 * Check PageMovable before holding a PG_lock because page's owner
97 * assumes anybody doesn't touch PG_lock of newly allocated page
98 * so unconditionally grapping the lock ruins page's owner side.
99 */
100 if (unlikely(!__PageMovable(page)))
101 goto out_putpage;
102 /*
103 * As movable pages are not isolated from LRU lists, concurrent
104 * compaction threads can race against page migration functions
105 * as well as race against the releasing a page.
106 *
107 * In order to avoid having an already isolated movable page
108 * being (wrongly) re-isolated while it is under migration,
109 * or to avoid attempting to isolate pages being released,
110 * lets be sure we have the page lock
111 * before proceeding with the movable page isolation steps.
112 */
113 if (unlikely(!trylock_page(page)))
114 goto out_putpage;
115
116 if (!PageMovable(page) || PageIsolated(page))
117 goto out_no_isolated;
118
119 mapping = page_mapping(page);
120 VM_BUG_ON_PAGE(!mapping, page);
121
122 if (!mapping->a_ops->isolate_page(page, mode))
123 goto out_no_isolated;
124
125 /* Driver shouldn't use PG_isolated bit of page->flags */
126 WARN_ON_ONCE(PageIsolated(page));
127 __SetPageIsolated(page);
128 unlock_page(page);
129
Yisheng Xie9e5bcd62017-02-24 14:57:29 -0800130 return 0;
Minchan Kimbda807d2016-07-26 15:23:05 -0700131
132out_no_isolated:
133 unlock_page(page);
134out_putpage:
135 put_page(page);
136out:
Yisheng Xie9e5bcd62017-02-24 14:57:29 -0800137 return -EBUSY;
Minchan Kimbda807d2016-07-26 15:23:05 -0700138}
139
140/* It should be called on page which is PG_movable */
141void putback_movable_page(struct page *page)
142{
143 struct address_space *mapping;
144
145 VM_BUG_ON_PAGE(!PageLocked(page), page);
146 VM_BUG_ON_PAGE(!PageMovable(page), page);
147 VM_BUG_ON_PAGE(!PageIsolated(page), page);
148
149 mapping = page_mapping(page);
150 mapping->a_ops->putback_page(page);
151 __ClearPageIsolated(page);
152}
153
Christoph Lameterb20a3502006-03-22 00:09:12 -0800154/*
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800155 * Put previously isolated pages back onto the appropriate lists
156 * from where they were once taken off for compaction/migration.
157 *
Joonsoo Kim59c82b72014-01-21 15:51:17 -0800158 * This function shall be used whenever the isolated pageset has been
159 * built from lru, balloon, hugetlbfs page. See isolate_migratepages_range()
160 * and isolate_huge_page().
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800161 */
162void putback_movable_pages(struct list_head *l)
163{
164 struct page *page;
165 struct page *page2;
166
167 list_for_each_entry_safe(page, page2, l, lru) {
Naoya Horiguchi31caf662013-09-11 14:21:59 -0700168 if (unlikely(PageHuge(page))) {
169 putback_active_hugepage(page);
170 continue;
171 }
Rafael Aquini5733c7d2012-12-11 16:02:47 -0800172 list_del(&page->lru);
Minchan Kimbda807d2016-07-26 15:23:05 -0700173 /*
174 * We isolated non-lru movable page so here we can use
175 * __PageMovable because LRU page's mapping cannot have
176 * PAGE_MAPPING_MOVABLE.
177 */
Minchan Kimb1123ea62016-07-26 15:23:09 -0700178 if (unlikely(__PageMovable(page))) {
Minchan Kimbda807d2016-07-26 15:23:05 -0700179 VM_BUG_ON_PAGE(!PageIsolated(page), page);
180 lock_page(page);
181 if (PageMovable(page))
182 putback_movable_page(page);
183 else
184 __ClearPageIsolated(page);
185 unlock_page(page);
186 put_page(page);
187 } else {
Naoya Horiguchie8db67e2017-09-08 16:11:12 -0700188 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
189 page_is_file_cache(page), -hpage_nr_pages(page));
Rabin Vincentfc280fe2017-04-20 14:37:46 -0700190 putback_lru_page(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700191 }
Christoph Lameterb20a3502006-03-22 00:09:12 -0800192 }
Christoph Lameterb20a3502006-03-22 00:09:12 -0800193}
194
Christoph Lameter06972122006-06-23 02:03:35 -0700195/*
196 * Restore a potential migration pte to a working pte entry
197 */
Minchan Kime4b82222017-05-03 14:54:27 -0700198static bool remove_migration_pte(struct page *page, struct vm_area_struct *vma,
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800199 unsigned long addr, void *old)
Christoph Lameter06972122006-06-23 02:03:35 -0700200{
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800201 struct page_vma_mapped_walk pvmw = {
202 .page = old,
203 .vma = vma,
204 .address = addr,
205 .flags = PVMW_SYNC | PVMW_MIGRATION,
206 };
207 struct page *new;
208 pte_t pte;
Christoph Lameter06972122006-06-23 02:03:35 -0700209 swp_entry_t entry;
Christoph Lameter06972122006-06-23 02:03:35 -0700210
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800211 VM_BUG_ON_PAGE(PageTail(page), page);
212 while (page_vma_mapped_walk(&pvmw)) {
Naoya Horiguchi4b0ece62017-03-31 15:11:44 -0700213 if (PageKsm(page))
214 new = page;
215 else
216 new = page - pvmw.page->index +
217 linear_page_index(vma, pvmw.address);
Christoph Lameter06972122006-06-23 02:03:35 -0700218
Zi Yan616b8372017-09-08 16:10:57 -0700219#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
220 /* PMD-mapped THP migration entry */
221 if (!pvmw.pte) {
222 VM_BUG_ON_PAGE(PageHuge(page) || !PageTransCompound(page), page);
223 remove_migration_pmd(&pvmw, new);
224 continue;
225 }
226#endif
227
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800228 get_page(new);
229 pte = pte_mkold(mk_pte(new, READ_ONCE(vma->vm_page_prot)));
230 if (pte_swp_soft_dirty(*pvmw.pte))
231 pte = pte_mksoft_dirty(pte);
Christoph Lameter06972122006-06-23 02:03:35 -0700232
Hugh Dickins486cf462011-10-19 12:50:35 -0700233 /*
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800234 * Recheck VMA as permissions can change since migration started
Hugh Dickins486cf462011-10-19 12:50:35 -0700235 */
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800236 entry = pte_to_swp_entry(*pvmw.pte);
237 if (is_write_migration_entry(entry))
238 pte = maybe_mkwrite(pte, vma);
Mel Gormand3cb8bf2014-10-02 19:47:41 +0100239
Aneesh Kumar K.V383321a2017-07-06 15:38:41 -0700240 flush_dcache_page(new);
Andi Kleen3ef8fd72010-10-11 16:03:21 +0200241#ifdef CONFIG_HUGETLB_PAGE
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800242 if (PageHuge(new)) {
243 pte = pte_mkhuge(pte);
244 pte = arch_make_huge_pte(pte, vma, new, 0);
Aneesh Kumar K.V383321a2017-07-06 15:38:41 -0700245 set_huge_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte);
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800246 if (PageAnon(new))
247 hugepage_add_anon_rmap(new, vma, pvmw.address);
248 else
249 page_dup_rmap(new, true);
Aneesh Kumar K.V383321a2017-07-06 15:38:41 -0700250 } else
251#endif
252 {
253 set_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700254
Aneesh Kumar K.V383321a2017-07-06 15:38:41 -0700255 if (PageAnon(new))
256 page_add_anon_rmap(new, vma, pvmw.address, false);
257 else
258 page_add_file_rmap(new, false);
259 }
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800260 if (vma->vm_flags & VM_LOCKED && !PageTransCompound(new))
261 mlock_vma_page(new);
Hugh Dickins51afb122015-11-05 18:49:37 -0800262
Kirill A. Shutemov3fe87962017-02-24 14:58:16 -0800263 /* No need to invalidate - it was non-present before */
264 update_mmu_cache(vma, pvmw.address, pvmw.pte);
265 }
266
Minchan Kime4b82222017-05-03 14:54:27 -0700267 return true;
Christoph Lameter06972122006-06-23 02:03:35 -0700268}
269
270/*
Christoph Lameter04e62a22006-06-23 02:03:38 -0700271 * Get rid of all migration entries and replace them by
272 * references to the indicated page.
273 */
Kirill A. Shutemove3884662016-03-17 14:20:07 -0700274void remove_migration_ptes(struct page *old, struct page *new, bool locked)
Christoph Lameter04e62a22006-06-23 02:03:38 -0700275{
Joonsoo Kim051ac832014-01-21 15:49:48 -0800276 struct rmap_walk_control rwc = {
277 .rmap_one = remove_migration_pte,
278 .arg = old,
279 };
280
Kirill A. Shutemove3884662016-03-17 14:20:07 -0700281 if (locked)
282 rmap_walk_locked(new, &rwc);
283 else
284 rmap_walk(new, &rwc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700285}
286
287/*
Christoph Lameter06972122006-06-23 02:03:35 -0700288 * Something used the pte of a page under migration. We need to
289 * get to the page and wait until migration is finished.
290 * When we return from this function the fault will be retried.
Christoph Lameter06972122006-06-23 02:03:35 -0700291 */
Naoya Horiguchie66f17f2015-02-11 15:25:22 -0800292void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700293 spinlock_t *ptl)
Christoph Lameter06972122006-06-23 02:03:35 -0700294{
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700295 pte_t pte;
Christoph Lameter06972122006-06-23 02:03:35 -0700296 swp_entry_t entry;
297 struct page *page;
298
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700299 spin_lock(ptl);
Christoph Lameter06972122006-06-23 02:03:35 -0700300 pte = *ptep;
301 if (!is_swap_pte(pte))
302 goto out;
303
304 entry = pte_to_swp_entry(pte);
305 if (!is_migration_entry(entry))
306 goto out;
307
308 page = migration_entry_to_page(entry);
309
Nick Piggine2867812008-07-25 19:45:30 -0700310 /*
311 * Once radix-tree replacement of page migration started, page_count
312 * *must* be zero. And, we don't want to call wait_on_page_locked()
313 * against a page without get_page().
314 * So, we use get_page_unless_zero(), here. Even failed, page fault
315 * will occur again.
316 */
317 if (!get_page_unless_zero(page))
318 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -0700319 pte_unmap_unlock(ptep, ptl);
320 wait_on_page_locked(page);
321 put_page(page);
322 return;
323out:
324 pte_unmap_unlock(ptep, ptl);
325}
326
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700327void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
328 unsigned long address)
329{
330 spinlock_t *ptl = pte_lockptr(mm, pmd);
331 pte_t *ptep = pte_offset_map(pmd, address);
332 __migration_entry_wait(mm, ptep, ptl);
333}
334
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -0800335void migration_entry_wait_huge(struct vm_area_struct *vma,
336 struct mm_struct *mm, pte_t *pte)
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700337{
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -0800338 spinlock_t *ptl = huge_pte_lockptr(hstate_vma(vma), mm, pte);
Naoya Horiguchi30dad302013-06-12 14:05:04 -0700339 __migration_entry_wait(mm, pte, ptl);
340}
341
Zi Yan616b8372017-09-08 16:10:57 -0700342#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
343void pmd_migration_entry_wait(struct mm_struct *mm, pmd_t *pmd)
344{
345 spinlock_t *ptl;
346 struct page *page;
347
348 ptl = pmd_lock(mm, pmd);
349 if (!is_pmd_migration_entry(*pmd))
350 goto unlock;
351 page = migration_entry_to_page(pmd_to_swp_entry(*pmd));
352 if (!get_page_unless_zero(page))
353 goto unlock;
354 spin_unlock(ptl);
355 wait_on_page_locked(page);
356 put_page(page);
357 return;
358unlock:
359 spin_unlock(ptl);
360}
361#endif
362
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800363#ifdef CONFIG_BLOCK
364/* Returns true if all buffers are successfully locked */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800365static bool buffer_migrate_lock_buffers(struct buffer_head *head,
366 enum migrate_mode mode)
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800367{
368 struct buffer_head *bh = head;
369
370 /* Simple case, sync compaction */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800371 if (mode != MIGRATE_ASYNC) {
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800372 do {
373 get_bh(bh);
374 lock_buffer(bh);
375 bh = bh->b_this_page;
376
377 } while (bh != head);
378
379 return true;
380 }
381
382 /* async case, we cannot block on lock_buffer so use trylock_buffer */
383 do {
384 get_bh(bh);
385 if (!trylock_buffer(bh)) {
386 /*
387 * We failed to lock the buffer and cannot stall in
388 * async migration. Release the taken locks
389 */
390 struct buffer_head *failed_bh = bh;
391 put_bh(failed_bh);
392 bh = head;
393 while (bh != failed_bh) {
394 unlock_buffer(bh);
395 put_bh(bh);
396 bh = bh->b_this_page;
397 }
398 return false;
399 }
400
401 bh = bh->b_this_page;
402 } while (bh != head);
403 return true;
404}
405#else
406static inline bool buffer_migrate_lock_buffers(struct buffer_head *head,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800407 enum migrate_mode mode)
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800408{
409 return true;
410}
411#endif /* CONFIG_BLOCK */
412
Christoph Lameterb20a3502006-03-22 00:09:12 -0800413/*
Christoph Lameterc3fcf8a2006-06-23 02:03:32 -0700414 * Replace the page in the mapping.
Christoph Lameter5b5c7122006-06-23 02:03:29 -0700415 *
416 * The number of remaining references must be:
417 * 1 for anonymous pages without a mapping
418 * 2 for pages with a mapping
David Howells266cf652009-04-03 16:42:36 +0100419 * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800420 */
Gu Zheng36bc08c2013-07-16 17:56:16 +0800421int migrate_page_move_mapping(struct address_space *mapping,
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800422 struct page *newpage, struct page *page,
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500423 struct buffer_head *head, enum migrate_mode mode,
424 int extra_count)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800425{
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800426 struct zone *oldzone, *newzone;
427 int dirty;
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500428 int expected_count = 1 + extra_count;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800429 void **pslot;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800430
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700431 if (!mapping) {
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700432 /* Anonymous page without mapping */
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500433 if (page_count(page) != expected_count)
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700434 return -EAGAIN;
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800435
436 /* No turning back from here */
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800437 newpage->index = page->index;
438 newpage->mapping = page->mapping;
439 if (PageSwapBacked(page))
Hugh Dickinsfa9949d2016-05-19 17:12:41 -0700440 __SetPageSwapBacked(newpage);
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800441
Rafael Aquini78bd5202012-12-11 16:02:31 -0800442 return MIGRATEPAGE_SUCCESS;
Christoph Lameter6c5240a2006-06-23 02:03:37 -0700443 }
444
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800445 oldzone = page_zone(page);
446 newzone = page_zone(newpage);
447
Nick Piggin19fd6232008-07-25 19:45:32 -0700448 spin_lock_irq(&mapping->tree_lock);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800449
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800450 pslot = radix_tree_lookup_slot(&mapping->page_tree,
451 page_index(page));
Christoph Lameterb20a3502006-03-22 00:09:12 -0800452
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500453 expected_count += 1 + page_has_private(page);
Nick Piggine2867812008-07-25 19:45:30 -0700454 if (page_count(page) != expected_count ||
Mel Gorman29c1f672011-01-13 15:47:21 -0800455 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700456 spin_unlock_irq(&mapping->tree_lock);
Christoph Lametere23ca002006-04-10 22:52:57 -0700457 return -EAGAIN;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800458 }
459
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700460 if (!page_ref_freeze(page, expected_count)) {
Nick Piggin19fd6232008-07-25 19:45:32 -0700461 spin_unlock_irq(&mapping->tree_lock);
Nick Piggine2867812008-07-25 19:45:30 -0700462 return -EAGAIN;
463 }
464
Christoph Lameterb20a3502006-03-22 00:09:12 -0800465 /*
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800466 * In the async migration case of moving a page with buffers, lock the
467 * buffers using trylock before the mapping is moved. If the mapping
468 * was moved, we later failed to lock the buffers and could not move
469 * the mapping back due to an elevated page count, we would have to
470 * block waiting on other references to be dropped.
471 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800472 if (mode == MIGRATE_ASYNC && head &&
473 !buffer_migrate_lock_buffers(head, mode)) {
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700474 page_ref_unfreeze(page, expected_count);
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800475 spin_unlock_irq(&mapping->tree_lock);
476 return -EAGAIN;
477 }
478
479 /*
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800480 * Now we know that no one else is looking at the page:
481 * no turning back from here.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800482 */
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800483 newpage->index = page->index;
484 newpage->mapping = page->mapping;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800485 get_page(newpage); /* add cache reference */
Nicholas Piggin6326fec2016-12-25 13:00:29 +1000486 if (PageSwapBacked(page)) {
487 __SetPageSwapBacked(newpage);
488 if (PageSwapCache(page)) {
489 SetPageSwapCache(newpage);
490 set_page_private(newpage, page_private(page));
491 }
492 } else {
493 VM_BUG_ON_PAGE(PageSwapCache(page), page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800494 }
495
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800496 /* Move dirty while page refs frozen and newpage not yet exposed */
497 dirty = PageDirty(page);
498 if (dirty) {
499 ClearPageDirty(page);
500 SetPageDirty(newpage);
501 }
502
Johannes Weiner6d75f362016-12-12 16:43:43 -0800503 radix_tree_replace_slot(&mapping->page_tree, pslot, newpage);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800504
505 /*
Jacobo Giralt937a94c2012-01-10 15:07:11 -0800506 * Drop cache reference from old page by unfreezing
507 * to one less reference.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800508 * We know this isn't the last reference.
509 */
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700510 page_ref_unfreeze(page, expected_count - 1);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800511
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800512 spin_unlock(&mapping->tree_lock);
513 /* Leave irq disabled to prevent preemption while updating stats */
514
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700515 /*
516 * If moved to a different zone then also account
517 * the page for that zone. Other VM counters will be
518 * taken care of when we establish references to the
519 * new page and drop references to the old page.
520 *
521 * Note that anonymous pages are accounted for
Mel Gorman4b9d0fa2016-07-28 15:46:17 -0700522 * via NR_FILE_PAGES and NR_ANON_MAPPED if they
Christoph Lameter0e8c7d02007-04-23 14:41:09 -0700523 * are mapped to swap space.
524 */
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800525 if (newzone != oldzone) {
Mel Gorman11fb9982016-07-28 15:46:20 -0700526 __dec_node_state(oldzone->zone_pgdat, NR_FILE_PAGES);
527 __inc_node_state(newzone->zone_pgdat, NR_FILE_PAGES);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800528 if (PageSwapBacked(page) && !PageSwapCache(page)) {
Mel Gorman11fb9982016-07-28 15:46:20 -0700529 __dec_node_state(oldzone->zone_pgdat, NR_SHMEM);
530 __inc_node_state(newzone->zone_pgdat, NR_SHMEM);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800531 }
532 if (dirty && mapping_cap_account_dirty(mapping)) {
Mel Gorman11fb9982016-07-28 15:46:20 -0700533 __dec_node_state(oldzone->zone_pgdat, NR_FILE_DIRTY);
Mel Gorman5a1c84b2016-07-28 15:47:31 -0700534 __dec_zone_state(oldzone, NR_ZONE_WRITE_PENDING);
Mel Gorman11fb9982016-07-28 15:46:20 -0700535 __inc_node_state(newzone->zone_pgdat, NR_FILE_DIRTY);
Mel Gorman5a1c84b2016-07-28 15:47:31 -0700536 __inc_zone_state(newzone, NR_ZONE_WRITE_PENDING);
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800537 }
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700538 }
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800539 local_irq_enable();
Christoph Lameterb20a3502006-03-22 00:09:12 -0800540
Rafael Aquini78bd5202012-12-11 16:02:31 -0800541 return MIGRATEPAGE_SUCCESS;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800542}
Richard Weinberger1118dce2016-06-16 23:26:14 +0200543EXPORT_SYMBOL(migrate_page_move_mapping);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800544
545/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900546 * The expected number of remaining references is the same as that
547 * of migrate_page_move_mapping().
548 */
549int migrate_huge_page_move_mapping(struct address_space *mapping,
550 struct page *newpage, struct page *page)
551{
552 int expected_count;
553 void **pslot;
554
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900555 spin_lock_irq(&mapping->tree_lock);
556
557 pslot = radix_tree_lookup_slot(&mapping->page_tree,
558 page_index(page));
559
560 expected_count = 2 + page_has_private(page);
561 if (page_count(page) != expected_count ||
Mel Gorman29c1f672011-01-13 15:47:21 -0800562 radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900563 spin_unlock_irq(&mapping->tree_lock);
564 return -EAGAIN;
565 }
566
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700567 if (!page_ref_freeze(page, expected_count)) {
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900568 spin_unlock_irq(&mapping->tree_lock);
569 return -EAGAIN;
570 }
571
Hugh Dickinscf4b7692015-11-05 18:50:02 -0800572 newpage->index = page->index;
573 newpage->mapping = page->mapping;
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700574
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900575 get_page(newpage);
576
Johannes Weiner6d75f362016-12-12 16:43:43 -0800577 radix_tree_replace_slot(&mapping->page_tree, pslot, newpage);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900578
Joonsoo Kimfe896d12016-03-17 14:19:26 -0700579 page_ref_unfreeze(page, expected_count - 1);
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900580
581 spin_unlock_irq(&mapping->tree_lock);
Johannes Weiner6a93ca82016-03-15 14:57:19 -0700582
Rafael Aquini78bd5202012-12-11 16:02:31 -0800583 return MIGRATEPAGE_SUCCESS;
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900584}
585
586/*
Dave Hansen30b0a102013-11-21 14:31:58 -0800587 * Gigantic pages are so large that we do not guarantee that page++ pointer
588 * arithmetic will work across the entire page. We need something more
589 * specialized.
590 */
591static void __copy_gigantic_page(struct page *dst, struct page *src,
592 int nr_pages)
593{
594 int i;
595 struct page *dst_base = dst;
596 struct page *src_base = src;
597
598 for (i = 0; i < nr_pages; ) {
599 cond_resched();
600 copy_highpage(dst, src);
601
602 i++;
603 dst = mem_map_next(dst, dst_base, i);
604 src = mem_map_next(src, src_base, i);
605 }
606}
607
608static void copy_huge_page(struct page *dst, struct page *src)
609{
610 int i;
611 int nr_pages;
612
613 if (PageHuge(src)) {
614 /* hugetlbfs page */
615 struct hstate *h = page_hstate(src);
616 nr_pages = pages_per_huge_page(h);
617
618 if (unlikely(nr_pages > MAX_ORDER_NR_PAGES)) {
619 __copy_gigantic_page(dst, src, nr_pages);
620 return;
621 }
622 } else {
623 /* thp page */
624 BUG_ON(!PageTransHuge(src));
625 nr_pages = hpage_nr_pages(src);
626 }
627
628 for (i = 0; i < nr_pages; i++) {
629 cond_resched();
630 copy_highpage(dst + i, src + i);
631 }
632}
633
634/*
Christoph Lameterb20a3502006-03-22 00:09:12 -0800635 * Copy the page to its new location
636 */
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900637void migrate_page_copy(struct page *newpage, struct page *page)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800638{
Rik van Riel7851a452013-10-07 11:29:23 +0100639 int cpupid;
640
Mel Gormanb32967f2012-11-19 12:35:47 +0000641 if (PageHuge(page) || PageTransHuge(page))
Naoya Horiguchi290408d2010-09-08 10:19:35 +0900642 copy_huge_page(newpage, page);
643 else
644 copy_highpage(newpage, page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800645
646 if (PageError(page))
647 SetPageError(newpage);
648 if (PageReferenced(page))
649 SetPageReferenced(newpage);
650 if (PageUptodate(page))
651 SetPageUptodate(newpage);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700652 if (TestClearPageActive(page)) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800653 VM_BUG_ON_PAGE(PageUnevictable(page), page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800654 SetPageActive(newpage);
Lee Schermerhorn418b27e2009-12-14 17:59:54 -0800655 } else if (TestClearPageUnevictable(page))
656 SetPageUnevictable(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800657 if (PageChecked(page))
658 SetPageChecked(newpage);
659 if (PageMappedToDisk(page))
660 SetPageMappedToDisk(newpage);
661
Hugh Dickins42cb14b2015-11-05 18:50:05 -0800662 /* Move dirty on pages not done by migrate_page_move_mapping() */
663 if (PageDirty(page))
664 SetPageDirty(newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800665
Vladimir Davydov33c3fc72015-09-09 15:35:45 -0700666 if (page_is_young(page))
667 set_page_young(newpage);
668 if (page_is_idle(page))
669 set_page_idle(newpage);
670
Rik van Riel7851a452013-10-07 11:29:23 +0100671 /*
672 * Copy NUMA information to the new page, to prevent over-eager
673 * future migrations of this same page.
674 */
675 cpupid = page_cpupid_xchg_last(page, -1);
676 page_cpupid_xchg_last(newpage, cpupid);
677
Hugh Dickinse9995ef2009-12-14 17:59:31 -0800678 ksm_migrate_page(newpage, page);
Hugh Dickinsc8d65532013-02-22 16:35:10 -0800679 /*
680 * Please do not reorder this without considering how mm/ksm.c's
681 * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache().
682 */
Naoya Horiguchib3b3a992015-04-15 16:13:15 -0700683 if (PageSwapCache(page))
684 ClearPageSwapCache(page);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800685 ClearPagePrivate(page);
686 set_page_private(page, 0);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800687
688 /*
689 * If any waiters have accumulated on the new page then
690 * wake them up.
691 */
692 if (PageWriteback(newpage))
693 end_page_writeback(newpage);
Vlastimil Babkad435edc2016-03-15 14:56:15 -0700694
695 copy_page_owner(page, newpage);
Johannes Weiner74485cf2016-03-15 14:57:54 -0700696
697 mem_cgroup_migrate(page, newpage);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800698}
Richard Weinberger1118dce2016-06-16 23:26:14 +0200699EXPORT_SYMBOL(migrate_page_copy);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800700
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700701/************************************************************
702 * Migration functions
703 ***********************************************************/
704
Christoph Lameterb20a3502006-03-22 00:09:12 -0800705/*
Minchan Kimbda807d2016-07-26 15:23:05 -0700706 * Common logic to directly migrate a single LRU page suitable for
David Howells266cf652009-04-03 16:42:36 +0100707 * pages that do not use PagePrivate/PagePrivate2.
Christoph Lameterb20a3502006-03-22 00:09:12 -0800708 *
709 * Pages are locked upon entry and exit.
710 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700711int migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800712 struct page *newpage, struct page *page,
713 enum migrate_mode mode)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800714{
715 int rc;
716
717 BUG_ON(PageWriteback(page)); /* Writeback must be complete */
718
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500719 rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode, 0);
Christoph Lameterb20a3502006-03-22 00:09:12 -0800720
Rafael Aquini78bd5202012-12-11 16:02:31 -0800721 if (rc != MIGRATEPAGE_SUCCESS)
Christoph Lameterb20a3502006-03-22 00:09:12 -0800722 return rc;
723
724 migrate_page_copy(newpage, page);
Rafael Aquini78bd5202012-12-11 16:02:31 -0800725 return MIGRATEPAGE_SUCCESS;
Christoph Lameterb20a3502006-03-22 00:09:12 -0800726}
727EXPORT_SYMBOL(migrate_page);
728
David Howells93614012006-09-30 20:45:40 +0200729#ifdef CONFIG_BLOCK
Christoph Lameterb20a3502006-03-22 00:09:12 -0800730/*
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700731 * Migration function for pages with buffers. This function can only be used
732 * if the underlying filesystem guarantees that no other references to "page"
733 * exist.
734 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -0700735int buffer_migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800736 struct page *newpage, struct page *page, enum migrate_mode mode)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700737{
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700738 struct buffer_head *bh, *head;
739 int rc;
740
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700741 if (!page_has_buffers(page))
Mel Gormana6bc32b2012-01-12 17:19:43 -0800742 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700743
744 head = page_buffers(page);
745
Benjamin LaHaise8e321fe2013-12-21 17:56:08 -0500746 rc = migrate_page_move_mapping(mapping, newpage, page, head, mode, 0);
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700747
Rafael Aquini78bd5202012-12-11 16:02:31 -0800748 if (rc != MIGRATEPAGE_SUCCESS)
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700749 return rc;
750
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800751 /*
752 * In the async case, migrate_page_move_mapping locked the buffers
753 * with an IRQ-safe spinlock held. In the sync case, the buffers
754 * need to be locked now
755 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800756 if (mode != MIGRATE_ASYNC)
757 BUG_ON(!buffer_migrate_lock_buffers(head, mode));
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700758
759 ClearPagePrivate(page);
760 set_page_private(newpage, page_private(page));
761 set_page_private(page, 0);
762 put_page(page);
763 get_page(newpage);
764
765 bh = head;
766 do {
767 set_bh_page(bh, newpage, bh_offset(bh));
768 bh = bh->b_this_page;
769
770 } while (bh != head);
771
772 SetPagePrivate(newpage);
773
774 migrate_page_copy(newpage, page);
775
776 bh = head;
777 do {
778 unlock_buffer(bh);
779 put_bh(bh);
780 bh = bh->b_this_page;
781
782 } while (bh != head);
783
Rafael Aquini78bd5202012-12-11 16:02:31 -0800784 return MIGRATEPAGE_SUCCESS;
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700785}
786EXPORT_SYMBOL(buffer_migrate_page);
David Howells93614012006-09-30 20:45:40 +0200787#endif
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700788
Christoph Lameter04e62a22006-06-23 02:03:38 -0700789/*
790 * Writeback a page to clean the dirty state
791 */
792static int writeout(struct address_space *mapping, struct page *page)
793{
794 struct writeback_control wbc = {
795 .sync_mode = WB_SYNC_NONE,
796 .nr_to_write = 1,
797 .range_start = 0,
798 .range_end = LLONG_MAX,
Christoph Lameter04e62a22006-06-23 02:03:38 -0700799 .for_reclaim = 1
800 };
801 int rc;
802
803 if (!mapping->a_ops->writepage)
804 /* No write method for the address space */
805 return -EINVAL;
806
807 if (!clear_page_dirty_for_io(page))
808 /* Someone else already triggered a write */
809 return -EAGAIN;
810
811 /*
812 * A dirty page may imply that the underlying filesystem has
813 * the page on some queue. So the page must be clean for
814 * migration. Writeout may mean we loose the lock and the
815 * page state is no longer what we checked for earlier.
816 * At this point we know that the migration attempt cannot
817 * be successful.
818 */
Kirill A. Shutemove3884662016-03-17 14:20:07 -0700819 remove_migration_ptes(page, page, false);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700820
821 rc = mapping->a_ops->writepage(page, &wbc);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700822
823 if (rc != AOP_WRITEPAGE_ACTIVATE)
824 /* unlocked. Relock */
825 lock_page(page);
826
Hugh Dickinsbda85502008-11-19 15:36:36 -0800827 return (rc < 0) ? -EIO : -EAGAIN;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700828}
829
830/*
831 * Default handling if a filesystem does not provide a migration function.
832 */
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700833static int fallback_migrate_page(struct address_space *mapping,
Mel Gormana6bc32b2012-01-12 17:19:43 -0800834 struct page *newpage, struct page *page, enum migrate_mode mode)
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700835{
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800836 if (PageDirty(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800837 /* Only writeback pages in full synchronous migration */
838 if (mode != MIGRATE_SYNC)
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800839 return -EBUSY;
Christoph Lameter04e62a22006-06-23 02:03:38 -0700840 return writeout(mapping, page);
Mel Gormanb969c4ab2012-01-12 17:19:34 -0800841 }
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700842
843 /*
844 * Buffers may be managed in a filesystem specific way.
845 * We must have no buffers or drop them.
846 */
David Howells266cf652009-04-03 16:42:36 +0100847 if (page_has_private(page) &&
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700848 !try_to_release_page(page, GFP_KERNEL))
849 return -EAGAIN;
850
Mel Gormana6bc32b2012-01-12 17:19:43 -0800851 return migrate_page(mapping, newpage, page, mode);
Christoph Lameter8351a6e2006-06-23 02:03:33 -0700852}
853
Christoph Lameter1d8b85c2006-06-23 02:03:28 -0700854/*
Christoph Lametere24f0b82006-06-23 02:03:51 -0700855 * Move a page to a newly allocated page
856 * The page is locked and all ptes have been successfully removed.
857 *
858 * The new page will have replaced the old page if this function
859 * is successful.
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700860 *
861 * Return value:
862 * < 0 - error code
Rafael Aquini78bd5202012-12-11 16:02:31 -0800863 * MIGRATEPAGE_SUCCESS - success
Christoph Lametere24f0b82006-06-23 02:03:51 -0700864 */
Mel Gorman3fe20112010-05-24 14:32:20 -0700865static int move_to_new_page(struct page *newpage, struct page *page,
Hugh Dickins5c3f9a62015-11-05 18:49:53 -0800866 enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700867{
868 struct address_space *mapping;
Minchan Kimbda807d2016-07-26 15:23:05 -0700869 int rc = -EAGAIN;
870 bool is_lru = !__PageMovable(page);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700871
Hugh Dickins7db76712015-11-05 18:49:49 -0800872 VM_BUG_ON_PAGE(!PageLocked(page), page);
873 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
Christoph Lametere24f0b82006-06-23 02:03:51 -0700874
Christoph Lametere24f0b82006-06-23 02:03:51 -0700875 mapping = page_mapping(page);
Minchan Kimbda807d2016-07-26 15:23:05 -0700876
877 if (likely(is_lru)) {
878 if (!mapping)
879 rc = migrate_page(mapping, newpage, page, mode);
880 else if (mapping->a_ops->migratepage)
881 /*
882 * Most pages have a mapping and most filesystems
883 * provide a migratepage callback. Anonymous pages
884 * are part of swap space which also has its own
885 * migratepage callback. This is the most common path
886 * for page migration.
887 */
888 rc = mapping->a_ops->migratepage(mapping, newpage,
889 page, mode);
890 else
891 rc = fallback_migrate_page(mapping, newpage,
892 page, mode);
893 } else {
Christoph Lametere24f0b82006-06-23 02:03:51 -0700894 /*
Minchan Kimbda807d2016-07-26 15:23:05 -0700895 * In case of non-lru page, it could be released after
896 * isolation step. In that case, we shouldn't try migration.
Christoph Lametere24f0b82006-06-23 02:03:51 -0700897 */
Minchan Kimbda807d2016-07-26 15:23:05 -0700898 VM_BUG_ON_PAGE(!PageIsolated(page), page);
899 if (!PageMovable(page)) {
900 rc = MIGRATEPAGE_SUCCESS;
901 __ClearPageIsolated(page);
902 goto out;
903 }
904
905 rc = mapping->a_ops->migratepage(mapping, newpage,
906 page, mode);
907 WARN_ON_ONCE(rc == MIGRATEPAGE_SUCCESS &&
908 !PageIsolated(page));
909 }
Christoph Lametere24f0b82006-06-23 02:03:51 -0700910
Hugh Dickins5c3f9a62015-11-05 18:49:53 -0800911 /*
912 * When successful, old pagecache page->mapping must be cleared before
913 * page is freed; but stats require that PageAnon be left as PageAnon.
914 */
915 if (rc == MIGRATEPAGE_SUCCESS) {
Minchan Kimbda807d2016-07-26 15:23:05 -0700916 if (__PageMovable(page)) {
917 VM_BUG_ON_PAGE(!PageIsolated(page), page);
918
919 /*
920 * We clear PG_movable under page_lock so any compactor
921 * cannot try to migrate this page.
922 */
923 __ClearPageIsolated(page);
924 }
925
926 /*
927 * Anonymous and movable page->mapping will be cleard by
928 * free_pages_prepare so don't reset it here for keeping
929 * the type to work PageAnon, for example.
930 */
931 if (!PageMappingFlags(page))
Hugh Dickins5c3f9a62015-11-05 18:49:53 -0800932 page->mapping = NULL;
Mel Gorman3fe20112010-05-24 14:32:20 -0700933 }
Minchan Kimbda807d2016-07-26 15:23:05 -0700934out:
Christoph Lametere24f0b82006-06-23 02:03:51 -0700935 return rc;
936}
937
Minchan Kim0dabec92011-10-31 17:06:57 -0700938static int __unmap_and_move(struct page *page, struct page *newpage,
Hugh Dickins9c620e22013-02-22 16:35:14 -0800939 int force, enum migrate_mode mode)
Christoph Lametere24f0b82006-06-23 02:03:51 -0700940{
Minchan Kim0dabec92011-10-31 17:06:57 -0700941 int rc = -EAGAIN;
Hugh Dickins2ebba6b2014-12-12 16:56:19 -0800942 int page_was_mapped = 0;
Mel Gorman3f6c8272010-05-24 14:32:17 -0700943 struct anon_vma *anon_vma = NULL;
Minchan Kimbda807d2016-07-26 15:23:05 -0700944 bool is_lru = !__PageMovable(page);
Christoph Lameter95a402c2006-06-23 02:03:53 -0700945
Nick Piggin529ae9a2008-08-02 12:01:03 +0200946 if (!trylock_page(page)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -0800947 if (!force || mode == MIGRATE_ASYNC)
Minchan Kim0dabec92011-10-31 17:06:57 -0700948 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -0800949
950 /*
951 * It's not safe for direct compaction to call lock_page.
952 * For example, during page readahead pages are added locked
953 * to the LRU. Later, when the IO completes the pages are
954 * marked uptodate and unlocked. However, the queueing
955 * could be merging multiple pages for one bio (e.g.
956 * mpage_readpages). If an allocation happens for the
957 * second or third page, the process can end up locking
958 * the same page twice and deadlocking. Rather than
959 * trying to be clever about what pages can be locked,
960 * avoid the use of lock_page for direct compaction
961 * altogether.
962 */
963 if (current->flags & PF_MEMALLOC)
Minchan Kim0dabec92011-10-31 17:06:57 -0700964 goto out;
Mel Gorman3e7d3442011-01-13 15:45:56 -0800965
Christoph Lametere24f0b82006-06-23 02:03:51 -0700966 lock_page(page);
967 }
968
969 if (PageWriteback(page)) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700970 /*
Jianguo Wufed5b642013-04-29 15:07:58 -0700971 * Only in the case of a full synchronous migration is it
Mel Gormana6bc32b2012-01-12 17:19:43 -0800972 * necessary to wait for PageWriteback. In the async case,
973 * the retry loop is too short and in the sync-light case,
974 * the overhead of stalling is too much
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700975 */
Mel Gormana6bc32b2012-01-12 17:19:43 -0800976 if (mode != MIGRATE_SYNC) {
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700977 rc = -EBUSY;
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700978 goto out_unlock;
Andrea Arcangeli11bc82d2011-03-22 16:33:11 -0700979 }
980 if (!force)
Johannes Weiner0a31bc92014-08-08 14:19:22 -0700981 goto out_unlock;
Christoph Lametere24f0b82006-06-23 02:03:51 -0700982 wait_on_page_writeback(page);
983 }
Hugh Dickins03f15c82015-11-05 18:49:56 -0800984
Christoph Lametere24f0b82006-06-23 02:03:51 -0700985 /*
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700986 * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
987 * we cannot notice that anon_vma is freed while we migrates a page.
Hugh Dickins1ce82b62011-01-13 15:47:30 -0800988 * This get_anon_vma() delays freeing anon_vma pointer until the end
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -0700989 * of migration. File cache pages are no problem because of page_lock()
KAMEZAWA Hiroyuki989f89c2007-08-30 23:56:21 -0700990 * File Caches may use write_page() or lock_page() in migration, then,
991 * just care Anon page here.
Hugh Dickins03f15c82015-11-05 18:49:56 -0800992 *
993 * Only page_get_anon_vma() understands the subtleties of
994 * getting a hold on an anon_vma from outside one of its mms.
995 * But if we cannot get anon_vma, then we won't need it anyway,
996 * because that implies that the anon page is no longer mapped
997 * (and cannot be remapped so long as we hold the page lock).
Christoph Lametere24f0b82006-06-23 02:03:51 -0700998 */
Hugh Dickins03f15c82015-11-05 18:49:56 -0800999 if (PageAnon(page) && !PageKsm(page))
Peter Zijlstra746b18d2011-05-24 17:12:10 -07001000 anon_vma = page_get_anon_vma(page);
Shaohua Li62e1c552008-02-04 22:29:33 -08001001
Hugh Dickins7db76712015-11-05 18:49:49 -08001002 /*
1003 * Block others from accessing the new page when we get around to
1004 * establishing additional references. We are usually the only one
1005 * holding a reference to newpage at this point. We used to have a BUG
1006 * here if trylock_page(newpage) fails, but would like to allow for
1007 * cases where there might be a race with the previous use of newpage.
1008 * This is much like races on refcount of oldpage: just don't BUG().
1009 */
1010 if (unlikely(!trylock_page(newpage)))
1011 goto out_unlock;
1012
Minchan Kimbda807d2016-07-26 15:23:05 -07001013 if (unlikely(!is_lru)) {
1014 rc = move_to_new_page(newpage, page, mode);
1015 goto out_unlock_both;
1016 }
1017
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001018 /*
Shaohua Li62e1c552008-02-04 22:29:33 -08001019 * Corner case handling:
1020 * 1. When a new swap-cache page is read into, it is added to the LRU
1021 * and treated as swapcache but it has no rmap yet.
1022 * Calling try_to_unmap() against a page->mapping==NULL page will
1023 * trigger a BUG. So handle it here.
1024 * 2. An orphaned page (see truncate_complete_page) might have
1025 * fs-private metadata. The page can be picked up due to memory
1026 * offlining. Everywhere else except page reclaim, the page is
1027 * invisible to the vm, so the page can not be migrated. So try to
1028 * free the metadata, so the page can be freed.
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001029 */
Shaohua Li62e1c552008-02-04 22:29:33 -08001030 if (!page->mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -08001031 VM_BUG_ON_PAGE(PageAnon(page), page);
Hugh Dickins1ce82b62011-01-13 15:47:30 -08001032 if (page_has_private(page)) {
Shaohua Li62e1c552008-02-04 22:29:33 -08001033 try_to_free_buffers(page);
Hugh Dickins7db76712015-11-05 18:49:49 -08001034 goto out_unlock_both;
Shaohua Li62e1c552008-02-04 22:29:33 -08001035 }
Hugh Dickins7db76712015-11-05 18:49:49 -08001036 } else if (page_mapped(page)) {
1037 /* Establish migration ptes */
Hugh Dickins03f15c82015-11-05 18:49:56 -08001038 VM_BUG_ON_PAGE(PageAnon(page) && !PageKsm(page) && !anon_vma,
1039 page);
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001040 try_to_unmap(page,
Wanpeng Lida1b13c2015-09-08 15:03:27 -07001041 TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001042 page_was_mapped = 1;
1043 }
KAMEZAWA Hiroyukidc386d42007-07-26 10:41:07 -07001044
Christoph Lametere6a15302006-06-25 05:46:49 -07001045 if (!page_mapped(page))
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001046 rc = move_to_new_page(newpage, page, mode);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001047
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001048 if (page_was_mapped)
1049 remove_migration_ptes(page,
Kirill A. Shutemove3884662016-03-17 14:20:07 -07001050 rc == MIGRATEPAGE_SUCCESS ? newpage : page, false);
Mel Gorman3f6c8272010-05-24 14:32:17 -07001051
Hugh Dickins7db76712015-11-05 18:49:49 -08001052out_unlock_both:
1053 unlock_page(newpage);
1054out_unlock:
Mel Gorman3f6c8272010-05-24 14:32:17 -07001055 /* Drop an anon_vma reference if we took one */
Rik van Riel76545062010-08-09 17:18:41 -07001056 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -07001057 put_anon_vma(anon_vma);
Christoph Lametere24f0b82006-06-23 02:03:51 -07001058 unlock_page(page);
Minchan Kim0dabec92011-10-31 17:06:57 -07001059out:
Minchan Kimc6c919e2016-07-26 15:23:02 -07001060 /*
1061 * If migration is successful, decrease refcount of the newpage
1062 * which will not free the page because new page owner increased
1063 * refcounter. As well, if it is LRU page, add the page to LRU
1064 * list in here.
1065 */
1066 if (rc == MIGRATEPAGE_SUCCESS) {
Minchan Kimb1123ea62016-07-26 15:23:09 -07001067 if (unlikely(__PageMovable(newpage)))
Minchan Kimc6c919e2016-07-26 15:23:02 -07001068 put_page(newpage);
1069 else
1070 putback_lru_page(newpage);
1071 }
1072
Minchan Kim0dabec92011-10-31 17:06:57 -07001073 return rc;
1074}
Christoph Lameter95a402c2006-06-23 02:03:53 -07001075
Minchan Kim0dabec92011-10-31 17:06:57 -07001076/*
Geert Uytterhoevenef2a5152015-04-14 15:44:22 -07001077 * gcc 4.7 and 4.8 on arm get an ICEs when inlining unmap_and_move(). Work
1078 * around it.
1079 */
1080#if (GCC_VERSION >= 40700 && GCC_VERSION < 40900) && defined(CONFIG_ARM)
1081#define ICE_noinline noinline
1082#else
1083#define ICE_noinline
1084#endif
1085
1086/*
Minchan Kim0dabec92011-10-31 17:06:57 -07001087 * Obtain the lock on page, remove all ptes and migrate the page
1088 * to the newly allocated page in newpage.
1089 */
Geert Uytterhoevenef2a5152015-04-14 15:44:22 -07001090static ICE_noinline int unmap_and_move(new_page_t get_new_page,
1091 free_page_t put_new_page,
1092 unsigned long private, struct page *page,
Naoya Horiguchiadd05ce2015-06-24 16:56:50 -07001093 int force, enum migrate_mode mode,
1094 enum migrate_reason reason)
Minchan Kim0dabec92011-10-31 17:06:57 -07001095{
Hugh Dickins2def7422015-11-05 18:49:46 -08001096 int rc = MIGRATEPAGE_SUCCESS;
Minchan Kim0dabec92011-10-31 17:06:57 -07001097 int *result = NULL;
Hugh Dickins2def7422015-11-05 18:49:46 -08001098 struct page *newpage;
Minchan Kim0dabec92011-10-31 17:06:57 -07001099
Hugh Dickins2def7422015-11-05 18:49:46 -08001100 newpage = get_new_page(page, private, &result);
Minchan Kim0dabec92011-10-31 17:06:57 -07001101 if (!newpage)
1102 return -ENOMEM;
1103
1104 if (page_count(page) == 1) {
1105 /* page was freed from under us. So we are done. */
Minchan Kimc6c919e2016-07-26 15:23:02 -07001106 ClearPageActive(page);
1107 ClearPageUnevictable(page);
Minchan Kimbda807d2016-07-26 15:23:05 -07001108 if (unlikely(__PageMovable(page))) {
1109 lock_page(page);
1110 if (!PageMovable(page))
1111 __ClearPageIsolated(page);
1112 unlock_page(page);
1113 }
Minchan Kimc6c919e2016-07-26 15:23:02 -07001114 if (put_new_page)
1115 put_new_page(newpage, private);
1116 else
1117 put_page(newpage);
Minchan Kim0dabec92011-10-31 17:06:57 -07001118 goto out;
1119 }
1120
Zi Yan616b8372017-09-08 16:10:57 -07001121 if (unlikely(PageTransHuge(page) && !PageTransHuge(newpage))) {
Kirill A. Shutemov4d2fa962016-01-15 16:54:00 -08001122 lock_page(page);
1123 rc = split_huge_page(page);
1124 unlock_page(page);
1125 if (rc)
Minchan Kim0dabec92011-10-31 17:06:57 -07001126 goto out;
Kirill A. Shutemov4d2fa962016-01-15 16:54:00 -08001127 }
Minchan Kim0dabec92011-10-31 17:06:57 -07001128
Hugh Dickins9c620e22013-02-22 16:35:14 -08001129 rc = __unmap_and_move(page, newpage, force, mode);
Minchan Kimc6c919e2016-07-26 15:23:02 -07001130 if (rc == MIGRATEPAGE_SUCCESS)
Vlastimil Babka7cd12b42016-03-15 14:56:18 -07001131 set_page_owner_migrate_reason(newpage, reason);
Rafael Aquinibf6bddf12012-12-11 16:02:42 -08001132
Minchan Kim0dabec92011-10-31 17:06:57 -07001133out:
Christoph Lametere24f0b82006-06-23 02:03:51 -07001134 if (rc != -EAGAIN) {
Minchan Kim0dabec92011-10-31 17:06:57 -07001135 /*
1136 * A page that has been migrated has all references
1137 * removed and will be freed. A page that has not been
1138 * migrated will have kepts its references and be
1139 * restored.
1140 */
1141 list_del(&page->lru);
Ming Ling6afcf8e2016-12-12 16:42:26 -08001142
1143 /*
1144 * Compaction can migrate also non-LRU pages which are
1145 * not accounted to NR_ISOLATED_*. They can be recognized
1146 * as __PageMovable
1147 */
1148 if (likely(!__PageMovable(page)))
Naoya Horiguchie8db67e2017-09-08 16:11:12 -07001149 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON +
1150 page_is_file_cache(page), -hpage_nr_pages(page));
Christoph Lametere24f0b82006-06-23 02:03:51 -07001151 }
David Rientjes68711a72014-06-04 16:08:25 -07001152
Christoph Lameter95a402c2006-06-23 02:03:53 -07001153 /*
Minchan Kimc6c919e2016-07-26 15:23:02 -07001154 * If migration is successful, releases reference grabbed during
1155 * isolation. Otherwise, restore the page to right list unless
1156 * we want to retry.
Christoph Lameter95a402c2006-06-23 02:03:53 -07001157 */
Minchan Kimc6c919e2016-07-26 15:23:02 -07001158 if (rc == MIGRATEPAGE_SUCCESS) {
1159 put_page(page);
1160 if (reason == MR_MEMORY_FAILURE) {
1161 /*
1162 * Set PG_HWPoison on just freed page
1163 * intentionally. Although it's rather weird,
1164 * it's how HWPoison flag works at the moment.
1165 */
1166 if (!test_set_page_hwpoison(page))
1167 num_poisoned_pages_inc();
1168 }
1169 } else {
Minchan Kimbda807d2016-07-26 15:23:05 -07001170 if (rc != -EAGAIN) {
1171 if (likely(!__PageMovable(page))) {
1172 putback_lru_page(page);
1173 goto put_new;
1174 }
1175
1176 lock_page(page);
1177 if (PageMovable(page))
1178 putback_movable_page(page);
1179 else
1180 __ClearPageIsolated(page);
1181 unlock_page(page);
1182 put_page(page);
1183 }
1184put_new:
Minchan Kimc6c919e2016-07-26 15:23:02 -07001185 if (put_new_page)
1186 put_new_page(newpage, private);
1187 else
1188 put_page(newpage);
1189 }
David Rientjes68711a72014-06-04 16:08:25 -07001190
Christoph Lameter742755a2006-06-23 02:03:55 -07001191 if (result) {
1192 if (rc)
1193 *result = rc;
1194 else
1195 *result = page_to_nid(newpage);
1196 }
Christoph Lametere24f0b82006-06-23 02:03:51 -07001197 return rc;
1198}
1199
1200/*
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001201 * Counterpart of unmap_and_move_page() for hugepage migration.
1202 *
1203 * This function doesn't wait the completion of hugepage I/O
1204 * because there is no race between I/O and migration for hugepage.
1205 * Note that currently hugepage I/O occurs only in direct I/O
1206 * where no lock is held and PG_writeback is irrelevant,
1207 * and writeback status of all subpages are counted in the reference
1208 * count of the head page (i.e. if all subpages of a 2MB hugepage are
1209 * under direct I/O, the reference of the head page is 512 and a bit more.)
1210 * This means that when we try to migrate hugepage whose subpages are
1211 * doing direct I/O, some references remain after try_to_unmap() and
1212 * hugepage migration fails without data corruption.
1213 *
1214 * There is also no race when direct I/O is issued on the page under migration,
1215 * because then pte is replaced with migration swap entry and direct I/O code
1216 * will wait in the page fault for migration to complete.
1217 */
1218static int unmap_and_move_huge_page(new_page_t get_new_page,
David Rientjes68711a72014-06-04 16:08:25 -07001219 free_page_t put_new_page, unsigned long private,
1220 struct page *hpage, int force,
Vlastimil Babka7cd12b42016-03-15 14:56:18 -07001221 enum migrate_mode mode, int reason)
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001222{
Hugh Dickins2def7422015-11-05 18:49:46 -08001223 int rc = -EAGAIN;
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001224 int *result = NULL;
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001225 int page_was_mapped = 0;
Joonsoo Kim32665f22014-01-21 15:51:15 -08001226 struct page *new_hpage;
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001227 struct anon_vma *anon_vma = NULL;
1228
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001229 /*
1230 * Movability of hugepages depends on architectures and hugepage size.
1231 * This check is necessary because some callers of hugepage migration
1232 * like soft offline and memory hotremove don't walk through page
1233 * tables or check whether the hugepage is pmd-based or not before
1234 * kicking migration.
1235 */
Naoya Horiguchi100873d2014-06-04 16:10:56 -07001236 if (!hugepage_migration_supported(page_hstate(hpage))) {
Joonsoo Kim32665f22014-01-21 15:51:15 -08001237 putback_active_hugepage(hpage);
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001238 return -ENOSYS;
Joonsoo Kim32665f22014-01-21 15:51:15 -08001239 }
Naoya Horiguchi83467ef2013-09-11 14:22:11 -07001240
Joonsoo Kim32665f22014-01-21 15:51:15 -08001241 new_hpage = get_new_page(hpage, private, &result);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001242 if (!new_hpage)
1243 return -ENOMEM;
1244
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001245 if (!trylock_page(hpage)) {
Mel Gormana6bc32b2012-01-12 17:19:43 -08001246 if (!force || mode != MIGRATE_SYNC)
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001247 goto out;
1248 lock_page(hpage);
1249 }
1250
Peter Zijlstra746b18d2011-05-24 17:12:10 -07001251 if (PageAnon(hpage))
1252 anon_vma = page_get_anon_vma(hpage);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001253
Hugh Dickins7db76712015-11-05 18:49:49 -08001254 if (unlikely(!trylock_page(new_hpage)))
1255 goto put_anon;
1256
Hugh Dickins2ebba6b2014-12-12 16:56:19 -08001257 if (page_mapped(hpage)) {
1258 try_to_unmap(hpage,
1259 TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
1260 page_was_mapped = 1;
1261 }
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001262
1263 if (!page_mapped(hpage))
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001264 rc = move_to_new_page(new_hpage, hpage, mode);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001265
Hugh Dickins5c3f9a62015-11-05 18:49:53 -08001266 if (page_was_mapped)
1267 remove_migration_ptes(hpage,
Kirill A. Shutemove3884662016-03-17 14:20:07 -07001268 rc == MIGRATEPAGE_SUCCESS ? new_hpage : hpage, false);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001269
Hugh Dickins7db76712015-11-05 18:49:49 -08001270 unlock_page(new_hpage);
1271
1272put_anon:
Hugh Dickinsfd4a4662011-01-13 15:47:31 -08001273 if (anon_vma)
Peter Zijlstra9e601092011-03-22 16:32:46 -07001274 put_anon_vma(anon_vma);
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -07001275
Hugh Dickins2def7422015-11-05 18:49:46 -08001276 if (rc == MIGRATEPAGE_SUCCESS) {
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -07001277 hugetlb_cgroup_migrate(hpage, new_hpage);
Hugh Dickins2def7422015-11-05 18:49:46 -08001278 put_new_page = NULL;
Vlastimil Babka7cd12b42016-03-15 14:56:18 -07001279 set_page_owner_migrate_reason(new_hpage, reason);
Hugh Dickins2def7422015-11-05 18:49:46 -08001280 }
Aneesh Kumar K.V8e6ac7f2012-07-31 16:42:27 -07001281
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001282 unlock_page(hpage);
Hillf Danton09761332011-12-08 14:34:20 -08001283out:
Naoya Horiguchib8ec1ce2013-09-11 14:22:01 -07001284 if (rc != -EAGAIN)
1285 putback_active_hugepage(hpage);
Anshuman Khandualc3114a82017-07-10 15:47:41 -07001286 if (reason == MR_MEMORY_FAILURE && !test_set_page_hwpoison(hpage))
1287 num_poisoned_pages_inc();
David Rientjes68711a72014-06-04 16:08:25 -07001288
1289 /*
1290 * If migration was not successful and there's a freeing callback, use
1291 * it. Otherwise, put_page() will drop the reference grabbed during
1292 * isolation.
1293 */
Hugh Dickins2def7422015-11-05 18:49:46 -08001294 if (put_new_page)
David Rientjes68711a72014-06-04 16:08:25 -07001295 put_new_page(new_hpage, private);
1296 else
Naoya Horiguchi3aaa76e2015-09-22 14:59:14 -07001297 putback_active_hugepage(new_hpage);
David Rientjes68711a72014-06-04 16:08:25 -07001298
Naoya Horiguchi290408d2010-09-08 10:19:35 +09001299 if (result) {
1300 if (rc)
1301 *result = rc;
1302 else
1303 *result = page_to_nid(new_hpage);
1304 }
1305 return rc;
1306}
1307
1308/*
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001309 * migrate_pages - migrate the pages specified in a list, to the free pages
1310 * supplied as the target for the page migration
Christoph Lameterb20a3502006-03-22 00:09:12 -08001311 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001312 * @from: The list of pages to be migrated.
1313 * @get_new_page: The function used to allocate free pages to be used
1314 * as the target of the page migration.
David Rientjes68711a72014-06-04 16:08:25 -07001315 * @put_new_page: The function used to free target pages if migration
1316 * fails, or NULL if no special handling is necessary.
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001317 * @private: Private data to be passed on to get_new_page()
1318 * @mode: The migration mode that specifies the constraints for
1319 * page migration, if any.
1320 * @reason: The reason for page migration.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001321 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001322 * The function returns after 10 attempts or if no pages are movable any more
1323 * because the list has become empty or no retryable pages exist any more.
Hugh Dickins14e0f9b2015-11-05 18:49:43 -08001324 * The caller should call putback_movable_pages() to return pages to the LRU
Minchan Kim28bd6572011-01-25 15:07:26 -08001325 * or free list only if ret != 0.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001326 *
Srivatsa S. Bhatc73e5c92013-04-29 15:08:16 -07001327 * Returns the number of pages that were not migrated, or an error code.
Christoph Lameterb20a3502006-03-22 00:09:12 -08001328 */
Hugh Dickins9c620e22013-02-22 16:35:14 -08001329int migrate_pages(struct list_head *from, new_page_t get_new_page,
David Rientjes68711a72014-06-04 16:08:25 -07001330 free_page_t put_new_page, unsigned long private,
1331 enum migrate_mode mode, int reason)
Christoph Lameterb20a3502006-03-22 00:09:12 -08001332{
Christoph Lametere24f0b82006-06-23 02:03:51 -07001333 int retry = 1;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001334 int nr_failed = 0;
Mel Gorman5647bc22012-10-19 10:46:20 +01001335 int nr_succeeded = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001336 int pass = 0;
1337 struct page *page;
1338 struct page *page2;
1339 int swapwrite = current->flags & PF_SWAPWRITE;
1340 int rc;
1341
1342 if (!swapwrite)
1343 current->flags |= PF_SWAPWRITE;
1344
Christoph Lametere24f0b82006-06-23 02:03:51 -07001345 for(pass = 0; pass < 10 && retry; pass++) {
1346 retry = 0;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001347
Christoph Lametere24f0b82006-06-23 02:03:51 -07001348 list_for_each_entry_safe(page, page2, from, lru) {
Christoph Lametere24f0b82006-06-23 02:03:51 -07001349 cond_resched();
Christoph Lameterb20a3502006-03-22 00:09:12 -08001350
Naoya Horiguchi31caf662013-09-11 14:21:59 -07001351 if (PageHuge(page))
1352 rc = unmap_and_move_huge_page(get_new_page,
David Rientjes68711a72014-06-04 16:08:25 -07001353 put_new_page, private, page,
Vlastimil Babka7cd12b42016-03-15 14:56:18 -07001354 pass > 2, mode, reason);
Naoya Horiguchi31caf662013-09-11 14:21:59 -07001355 else
David Rientjes68711a72014-06-04 16:08:25 -07001356 rc = unmap_and_move(get_new_page, put_new_page,
Naoya Horiguchiadd05ce2015-06-24 16:56:50 -07001357 private, page, pass > 2, mode,
1358 reason);
Christoph Lameterb20a3502006-03-22 00:09:12 -08001359
Christoph Lametere24f0b82006-06-23 02:03:51 -07001360 switch(rc) {
Christoph Lameter95a402c2006-06-23 02:03:53 -07001361 case -ENOMEM:
David Rientjesdfef2ef2016-05-20 16:59:05 -07001362 nr_failed++;
Christoph Lameter95a402c2006-06-23 02:03:53 -07001363 goto out;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001364 case -EAGAIN:
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001365 retry++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001366 break;
Rafael Aquini78bd5202012-12-11 16:02:31 -08001367 case MIGRATEPAGE_SUCCESS:
Mel Gorman5647bc22012-10-19 10:46:20 +01001368 nr_succeeded++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001369 break;
1370 default:
Naoya Horiguchi354a3362014-01-21 15:51:14 -08001371 /*
1372 * Permanent failure (-EBUSY, -ENOSYS, etc.):
1373 * unlike -EAGAIN case, the failed page is
1374 * removed from migration page list and not
1375 * retried in the next outer loop.
1376 */
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001377 nr_failed++;
Christoph Lametere24f0b82006-06-23 02:03:51 -07001378 break;
Christoph Lameter2d1db3b2006-06-23 02:03:33 -07001379 }
Christoph Lameterb20a3502006-03-22 00:09:12 -08001380 }
1381 }
Vlastimil Babkaf2f81fb2015-11-05 18:47:03 -08001382 nr_failed += retry;
1383 rc = nr_failed;
Christoph Lameter95a402c2006-06-23 02:03:53 -07001384out:
Mel Gorman5647bc22012-10-19 10:46:20 +01001385 if (nr_succeeded)
1386 count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
1387 if (nr_failed)
1388 count_vm_events(PGMIGRATE_FAIL, nr_failed);
Mel Gorman7b2a2d42012-10-19 14:07:31 +01001389 trace_mm_migrate_pages(nr_succeeded, nr_failed, mode, reason);
1390
Christoph Lameterb20a3502006-03-22 00:09:12 -08001391 if (!swapwrite)
1392 current->flags &= ~PF_SWAPWRITE;
1393
Rafael Aquini78bd5202012-12-11 16:02:31 -08001394 return rc;
Christoph Lameterb20a3502006-03-22 00:09:12 -08001395}
1396
Christoph Lameter742755a2006-06-23 02:03:55 -07001397#ifdef CONFIG_NUMA
1398/*
1399 * Move a list of individual pages
1400 */
1401struct page_to_node {
1402 unsigned long addr;
1403 struct page *page;
1404 int node;
1405 int status;
1406};
1407
1408static struct page *new_page_node(struct page *p, unsigned long private,
1409 int **result)
1410{
1411 struct page_to_node *pm = (struct page_to_node *)private;
1412
1413 while (pm->node != MAX_NUMNODES && pm->page != p)
1414 pm++;
1415
1416 if (pm->node == MAX_NUMNODES)
1417 return NULL;
1418
1419 *result = &pm->status;
1420
Naoya Horiguchie632a932013-09-11 14:22:04 -07001421 if (PageHuge(p))
1422 return alloc_huge_page_node(page_hstate(compound_head(p)),
1423 pm->node);
Naoya Horiguchie8db67e2017-09-08 16:11:12 -07001424 else if (thp_migration_supported() && PageTransHuge(p)) {
1425 struct page *thp;
1426
1427 thp = alloc_pages_node(pm->node,
1428 (GFP_TRANSHUGE | __GFP_THISNODE) & ~__GFP_RECLAIM,
1429 HPAGE_PMD_ORDER);
1430 if (!thp)
1431 return NULL;
1432 prep_transhuge_page(thp);
1433 return thp;
1434 } else
Vlastimil Babka96db8002015-09-08 15:03:50 -07001435 return __alloc_pages_node(pm->node,
Johannes Weinere97ca8e52014-03-10 15:49:43 -07001436 GFP_HIGHUSER_MOVABLE | __GFP_THISNODE, 0);
Christoph Lameter742755a2006-06-23 02:03:55 -07001437}
1438
1439/*
1440 * Move a set of pages as indicated in the pm array. The addr
1441 * field must be set to the virtual address of the page to be moved
1442 * and the node number must contain a valid target node.
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001443 * The pm array ends with node = MAX_NUMNODES.
Christoph Lameter742755a2006-06-23 02:03:55 -07001444 */
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001445static int do_move_page_to_node_array(struct mm_struct *mm,
1446 struct page_to_node *pm,
1447 int migrate_all)
Christoph Lameter742755a2006-06-23 02:03:55 -07001448{
1449 int err;
1450 struct page_to_node *pp;
1451 LIST_HEAD(pagelist);
1452
1453 down_read(&mm->mmap_sem);
1454
1455 /*
1456 * Build a list of pages to migrate
1457 */
Christoph Lameter742755a2006-06-23 02:03:55 -07001458 for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
1459 struct vm_area_struct *vma;
1460 struct page *page;
Naoya Horiguchie8db67e2017-09-08 16:11:12 -07001461 struct page *head;
1462 unsigned int follflags;
Christoph Lameter742755a2006-06-23 02:03:55 -07001463
Christoph Lameter742755a2006-06-23 02:03:55 -07001464 err = -EFAULT;
1465 vma = find_vma(mm, pp->addr);
Gleb Natapov70384dc2010-10-26 14:22:07 -07001466 if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
Christoph Lameter742755a2006-06-23 02:03:55 -07001467 goto set_status;
1468
Kirill A. Shutemovd8998442015-09-04 15:47:53 -07001469 /* FOLL_DUMP to ignore special (like zero) pages */
Naoya Horiguchie8db67e2017-09-08 16:11:12 -07001470 follflags = FOLL_GET | FOLL_DUMP;
1471 if (!thp_migration_supported())
1472 follflags |= FOLL_SPLIT;
1473 page = follow_page(vma, pp->addr, follflags);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001474
1475 err = PTR_ERR(page);
1476 if (IS_ERR(page))
1477 goto set_status;
1478
Christoph Lameter742755a2006-06-23 02:03:55 -07001479 err = -ENOENT;
1480 if (!page)
1481 goto set_status;
1482
Christoph Lameter742755a2006-06-23 02:03:55 -07001483 err = page_to_nid(page);
1484
1485 if (err == pp->node)
1486 /*
1487 * Node already in the right place
1488 */
1489 goto put_and_set;
1490
1491 err = -EACCES;
1492 if (page_mapcount(page) > 1 &&
1493 !migrate_all)
1494 goto put_and_set;
1495
Naoya Horiguchie632a932013-09-11 14:22:04 -07001496 if (PageHuge(page)) {
Naoya Horiguchie8db67e2017-09-08 16:11:12 -07001497 if (PageHead(page)) {
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08001498 isolate_huge_page(page, &pagelist);
Naoya Horiguchie8db67e2017-09-08 16:11:12 -07001499 err = 0;
1500 pp->page = page;
1501 }
Naoya Horiguchie632a932013-09-11 14:22:04 -07001502 goto put_and_set;
1503 }
1504
Naoya Horiguchie8db67e2017-09-08 16:11:12 -07001505 pp->page = compound_head(page);
1506 head = compound_head(page);
1507 err = isolate_lru_page(head);
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001508 if (!err) {
Naoya Horiguchie8db67e2017-09-08 16:11:12 -07001509 list_add_tail(&head->lru, &pagelist);
1510 mod_node_page_state(page_pgdat(head),
1511 NR_ISOLATED_ANON + page_is_file_cache(head),
1512 hpage_nr_pages(head));
KOSAKI Motohiro6d9c2852009-12-14 17:58:11 -08001513 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001514put_and_set:
1515 /*
1516 * Either remove the duplicate refcount from
1517 * isolate_lru_page() or drop the page ref if it was
1518 * not isolated.
1519 */
1520 put_page(page);
1521set_status:
1522 pp->status = err;
1523 }
1524
Brice Gogline78bbfa2008-10-18 20:27:15 -07001525 err = 0;
Minchan Kimcf608ac2010-10-26 14:21:29 -07001526 if (!list_empty(&pagelist)) {
David Rientjes68711a72014-06-04 16:08:25 -07001527 err = migrate_pages(&pagelist, new_page_node, NULL,
Hugh Dickins9c620e22013-02-22 16:35:14 -08001528 (unsigned long)pm, MIGRATE_SYNC, MR_SYSCALL);
Minchan Kimcf608ac2010-10-26 14:21:29 -07001529 if (err)
Naoya Horiguchie632a932013-09-11 14:22:04 -07001530 putback_movable_pages(&pagelist);
Minchan Kimcf608ac2010-10-26 14:21:29 -07001531 }
Christoph Lameter742755a2006-06-23 02:03:55 -07001532
1533 up_read(&mm->mmap_sem);
1534 return err;
1535}
1536
1537/*
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001538 * Migrate an array of page address onto an array of nodes and fill
1539 * the corresponding array of status.
1540 */
Christoph Lameter3268c632012-03-21 16:34:06 -07001541static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001542 unsigned long nr_pages,
1543 const void __user * __user *pages,
1544 const int __user *nodes,
1545 int __user *status, int flags)
1546{
Brice Goglin3140a222009-01-06 14:38:57 -08001547 struct page_to_node *pm;
Brice Goglin3140a222009-01-06 14:38:57 -08001548 unsigned long chunk_nr_pages;
1549 unsigned long chunk_start;
1550 int err;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001551
Brice Goglin3140a222009-01-06 14:38:57 -08001552 err = -ENOMEM;
1553 pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
1554 if (!pm)
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001555 goto out;
Brice Goglin35282a22009-06-16 15:32:43 -07001556
1557 migrate_prep();
1558
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001559 /*
Brice Goglin3140a222009-01-06 14:38:57 -08001560 * Store a chunk of page_to_node array in a page,
1561 * but keep the last one as a marker
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001562 */
Brice Goglin3140a222009-01-06 14:38:57 -08001563 chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001564
Brice Goglin3140a222009-01-06 14:38:57 -08001565 for (chunk_start = 0;
1566 chunk_start < nr_pages;
1567 chunk_start += chunk_nr_pages) {
1568 int j;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001569
Brice Goglin3140a222009-01-06 14:38:57 -08001570 if (chunk_start + chunk_nr_pages > nr_pages)
1571 chunk_nr_pages = nr_pages - chunk_start;
1572
1573 /* fill the chunk pm with addrs and nodes from user-space */
1574 for (j = 0; j < chunk_nr_pages; j++) {
1575 const void __user *p;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001576 int node;
1577
Brice Goglin3140a222009-01-06 14:38:57 -08001578 err = -EFAULT;
1579 if (get_user(p, pages + j + chunk_start))
1580 goto out_pm;
1581 pm[j].addr = (unsigned long) p;
1582
1583 if (get_user(node, nodes + j + chunk_start))
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001584 goto out_pm;
1585
1586 err = -ENODEV;
Linus Torvalds6f5a55f2010-02-05 16:16:50 -08001587 if (node < 0 || node >= MAX_NUMNODES)
1588 goto out_pm;
1589
Lai Jiangshan389162c2012-12-12 13:51:30 -08001590 if (!node_state(node, N_MEMORY))
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001591 goto out_pm;
1592
1593 err = -EACCES;
1594 if (!node_isset(node, task_nodes))
1595 goto out_pm;
1596
Brice Goglin3140a222009-01-06 14:38:57 -08001597 pm[j].node = node;
1598 }
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001599
Brice Goglin3140a222009-01-06 14:38:57 -08001600 /* End marker for this chunk */
1601 pm[chunk_nr_pages].node = MAX_NUMNODES;
1602
1603 /* Migrate this chunk */
1604 err = do_move_page_to_node_array(mm, pm,
1605 flags & MPOL_MF_MOVE_ALL);
1606 if (err < 0)
1607 goto out_pm;
1608
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001609 /* Return status information */
Brice Goglin3140a222009-01-06 14:38:57 -08001610 for (j = 0; j < chunk_nr_pages; j++)
1611 if (put_user(pm[j].status, status + j + chunk_start)) {
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001612 err = -EFAULT;
Brice Goglin3140a222009-01-06 14:38:57 -08001613 goto out_pm;
1614 }
1615 }
1616 err = 0;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001617
1618out_pm:
Brice Goglin3140a222009-01-06 14:38:57 -08001619 free_page((unsigned long)pm);
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001620out:
1621 return err;
1622}
1623
1624/*
Brice Goglin2f007e72008-10-18 20:27:16 -07001625 * Determine the nodes of an array of pages and store it in an array of status.
Christoph Lameter742755a2006-06-23 02:03:55 -07001626 */
Brice Goglin80bba122008-12-09 13:14:23 -08001627static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
1628 const void __user **pages, int *status)
Christoph Lameter742755a2006-06-23 02:03:55 -07001629{
Brice Goglin2f007e72008-10-18 20:27:16 -07001630 unsigned long i;
Brice Goglin2f007e72008-10-18 20:27:16 -07001631
Christoph Lameter742755a2006-06-23 02:03:55 -07001632 down_read(&mm->mmap_sem);
1633
Brice Goglin2f007e72008-10-18 20:27:16 -07001634 for (i = 0; i < nr_pages; i++) {
Brice Goglin80bba122008-12-09 13:14:23 -08001635 unsigned long addr = (unsigned long)(*pages);
Christoph Lameter742755a2006-06-23 02:03:55 -07001636 struct vm_area_struct *vma;
1637 struct page *page;
KOSAKI Motohiroc095adb2008-12-16 16:06:43 +09001638 int err = -EFAULT;
Brice Goglin2f007e72008-10-18 20:27:16 -07001639
1640 vma = find_vma(mm, addr);
Gleb Natapov70384dc2010-10-26 14:22:07 -07001641 if (!vma || addr < vma->vm_start)
Christoph Lameter742755a2006-06-23 02:03:55 -07001642 goto set_status;
1643
Kirill A. Shutemovd8998442015-09-04 15:47:53 -07001644 /* FOLL_DUMP to ignore special (like zero) pages */
1645 page = follow_page(vma, addr, FOLL_DUMP);
Linus Torvalds89f5b7d2008-06-20 11:18:25 -07001646
1647 err = PTR_ERR(page);
1648 if (IS_ERR(page))
1649 goto set_status;
1650
Kirill A. Shutemovd8998442015-09-04 15:47:53 -07001651 err = page ? page_to_nid(page) : -ENOENT;
Christoph Lameter742755a2006-06-23 02:03:55 -07001652set_status:
Brice Goglin80bba122008-12-09 13:14:23 -08001653 *status = err;
1654
1655 pages++;
1656 status++;
1657 }
1658
1659 up_read(&mm->mmap_sem);
1660}
1661
1662/*
1663 * Determine the nodes of a user array of pages and store it in
1664 * a user array of status.
1665 */
1666static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
1667 const void __user * __user *pages,
1668 int __user *status)
1669{
1670#define DO_PAGES_STAT_CHUNK_NR 16
1671 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1672 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
Brice Goglin80bba122008-12-09 13:14:23 -08001673
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001674 while (nr_pages) {
1675 unsigned long chunk_nr;
Brice Goglin80bba122008-12-09 13:14:23 -08001676
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001677 chunk_nr = nr_pages;
1678 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1679 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1680
1681 if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
1682 break;
Brice Goglin80bba122008-12-09 13:14:23 -08001683
1684 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1685
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001686 if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
1687 break;
Christoph Lameter742755a2006-06-23 02:03:55 -07001688
H. Peter Anvin87b8d1a2010-02-18 16:13:40 -08001689 pages += chunk_nr;
1690 status += chunk_nr;
1691 nr_pages -= chunk_nr;
1692 }
1693 return nr_pages ? -EFAULT : 0;
Christoph Lameter742755a2006-06-23 02:03:55 -07001694}
1695
1696/*
1697 * Move a list of pages in the address space of the currently executing
1698 * process.
1699 */
Heiko Carstens938bb9f2009-01-14 14:14:30 +01001700SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
1701 const void __user * __user *, pages,
1702 const int __user *, nodes,
1703 int __user *, status, int, flags)
Christoph Lameter742755a2006-06-23 02:03:55 -07001704{
Christoph Lameter742755a2006-06-23 02:03:55 -07001705 struct task_struct *task;
Christoph Lameter742755a2006-06-23 02:03:55 -07001706 struct mm_struct *mm;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001707 int err;
Christoph Lameter3268c632012-03-21 16:34:06 -07001708 nodemask_t task_nodes;
Christoph Lameter742755a2006-06-23 02:03:55 -07001709
1710 /* Check flags */
1711 if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
1712 return -EINVAL;
1713
1714 if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
1715 return -EPERM;
1716
1717 /* Find the mm_struct */
Greg Thelena879bf52011-02-25 14:44:13 -08001718 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07001719 task = pid ? find_task_by_vpid(pid) : current;
Christoph Lameter742755a2006-06-23 02:03:55 -07001720 if (!task) {
Greg Thelena879bf52011-02-25 14:44:13 -08001721 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001722 return -ESRCH;
1723 }
Christoph Lameter3268c632012-03-21 16:34:06 -07001724 get_task_struct(task);
Christoph Lameter742755a2006-06-23 02:03:55 -07001725
1726 /*
1727 * Check if this process has the right to modify the specified
Linus Torvalds197e7e52017-08-20 13:26:27 -07001728 * process. Use the regular "ptrace_may_access()" checks.
Christoph Lameter742755a2006-06-23 02:03:55 -07001729 */
Linus Torvalds197e7e52017-08-20 13:26:27 -07001730 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001731 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001732 err = -EPERM;
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001733 goto out;
Christoph Lameter742755a2006-06-23 02:03:55 -07001734 }
David Howellsc69e8d92008-11-14 10:39:19 +11001735 rcu_read_unlock();
Christoph Lameter742755a2006-06-23 02:03:55 -07001736
David Quigley86c3a762006-06-23 02:04:02 -07001737 err = security_task_movememory(task);
1738 if (err)
Brice Goglin5e9a0f02008-10-18 20:27:17 -07001739 goto out;
David Quigley86c3a762006-06-23 02:04:02 -07001740
Christoph Lameter3268c632012-03-21 16:34:06 -07001741 task_nodes = cpuset_mems_allowed(task);
1742 mm = get_task_mm(task);
1743 put_task_struct(task);
1744
Sasha Levin6e8b09e2012-04-25 16:01:53 -07001745 if (!mm)
1746 return -EINVAL;
1747
1748 if (nodes)
1749 err = do_pages_move(mm, task_nodes, nr_pages, pages,
1750 nodes, status, flags);
1751 else
1752 err = do_pages_stat(mm, nr_pages, pages, status);
Christoph Lameter3268c632012-03-21 16:34:06 -07001753
1754 mmput(mm);
1755 return err;
David Quigley86c3a762006-06-23 02:04:02 -07001756
Christoph Lameter742755a2006-06-23 02:03:55 -07001757out:
Christoph Lameter3268c632012-03-21 16:34:06 -07001758 put_task_struct(task);
Christoph Lameter742755a2006-06-23 02:03:55 -07001759 return err;
1760}
Christoph Lameter742755a2006-06-23 02:03:55 -07001761
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001762#ifdef CONFIG_NUMA_BALANCING
1763/*
1764 * Returns true if this is a safe migration target node for misplaced NUMA
1765 * pages. Currently it only checks the watermarks which crude
1766 */
1767static bool migrate_balanced_pgdat(struct pglist_data *pgdat,
Mel Gorman3abef4e2013-02-22 16:34:27 -08001768 unsigned long nr_migrate_pages)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001769{
1770 int z;
Mel Gorman599d0c92016-07-28 15:45:31 -07001771
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001772 for (z = pgdat->nr_zones - 1; z >= 0; z--) {
1773 struct zone *zone = pgdat->node_zones + z;
1774
1775 if (!populated_zone(zone))
1776 continue;
1777
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001778 /* Avoid waking kswapd by allocating pages_to_migrate pages. */
1779 if (!zone_watermark_ok(zone, 0,
1780 high_wmark_pages(zone) +
1781 nr_migrate_pages,
1782 0, 0))
1783 continue;
1784 return true;
1785 }
1786 return false;
1787}
1788
1789static struct page *alloc_misplaced_dst_page(struct page *page,
1790 unsigned long data,
1791 int **result)
1792{
1793 int nid = (int) data;
1794 struct page *newpage;
1795
Vlastimil Babka96db8002015-09-08 15:03:50 -07001796 newpage = __alloc_pages_node(nid,
Johannes Weinere97ca8e52014-03-10 15:49:43 -07001797 (GFP_HIGHUSER_MOVABLE |
1798 __GFP_THISNODE | __GFP_NOMEMALLOC |
1799 __GFP_NORETRY | __GFP_NOWARN) &
Mel Gorman8479eba2016-02-26 15:19:31 -08001800 ~__GFP_RECLAIM, 0);
Hillf Dantonbac03822012-11-27 14:46:24 +00001801
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001802 return newpage;
1803}
1804
1805/*
Mel Gormana8f60772012-11-14 21:41:46 +00001806 * page migration rate limiting control.
1807 * Do not migrate more than @pages_to_migrate in a @migrate_interval_millisecs
1808 * window of time. Default here says do not migrate more than 1280M per second.
1809 */
1810static unsigned int migrate_interval_millisecs __read_mostly = 100;
1811static unsigned int ratelimit_pages __read_mostly = 128 << (20 - PAGE_SHIFT);
1812
Mel Gormanb32967f2012-11-19 12:35:47 +00001813/* Returns true if the node is migrate rate-limited after the update */
Mel Gorman1c30e012014-01-21 15:50:58 -08001814static bool numamigrate_update_ratelimit(pg_data_t *pgdat,
1815 unsigned long nr_pages)
Mel Gormanb32967f2012-11-19 12:35:47 +00001816{
Mel Gormanb32967f2012-11-19 12:35:47 +00001817 /*
1818 * Rate-limit the amount of data that is being migrated to a node.
1819 * Optimal placement is no good if the memory bus is saturated and
1820 * all the time is being spent migrating!
1821 */
Mel Gormanb32967f2012-11-19 12:35:47 +00001822 if (time_after(jiffies, pgdat->numabalancing_migrate_next_window)) {
Mel Gorman1c5e9c22014-01-21 15:50:59 -08001823 spin_lock(&pgdat->numabalancing_migrate_lock);
Mel Gormanb32967f2012-11-19 12:35:47 +00001824 pgdat->numabalancing_migrate_nr_pages = 0;
1825 pgdat->numabalancing_migrate_next_window = jiffies +
1826 msecs_to_jiffies(migrate_interval_millisecs);
Mel Gorman1c5e9c22014-01-21 15:50:59 -08001827 spin_unlock(&pgdat->numabalancing_migrate_lock);
Mel Gormanb32967f2012-11-19 12:35:47 +00001828 }
Mel Gormanaf1839d2014-01-21 15:51:01 -08001829 if (pgdat->numabalancing_migrate_nr_pages > ratelimit_pages) {
1830 trace_mm_numa_migrate_ratelimit(current, pgdat->node_id,
1831 nr_pages);
Mel Gorman1c5e9c22014-01-21 15:50:59 -08001832 return true;
Mel Gormanaf1839d2014-01-21 15:51:01 -08001833 }
Mel Gorman1c5e9c22014-01-21 15:50:59 -08001834
1835 /*
1836 * This is an unlocked non-atomic update so errors are possible.
1837 * The consequences are failing to migrate when we potentiall should
1838 * have which is not severe enough to warrant locking. If it is ever
1839 * a problem, it can be converted to a per-cpu counter.
1840 */
1841 pgdat->numabalancing_migrate_nr_pages += nr_pages;
1842 return false;
Mel Gormanb32967f2012-11-19 12:35:47 +00001843}
1844
Mel Gorman1c30e012014-01-21 15:50:58 -08001845static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
Mel Gormanb32967f2012-11-19 12:35:47 +00001846{
Hugh Dickins340ef392013-02-22 16:34:33 -08001847 int page_lru;
Mel Gormanb32967f2012-11-19 12:35:47 +00001848
Sasha Levin309381fea2014-01-23 15:52:54 -08001849 VM_BUG_ON_PAGE(compound_order(page) && !PageTransHuge(page), page);
Mel Gorman3abef4e2013-02-22 16:34:27 -08001850
Mel Gormanb32967f2012-11-19 12:35:47 +00001851 /* Avoid migrating to a node that is nearly full */
Hugh Dickins340ef392013-02-22 16:34:33 -08001852 if (!migrate_balanced_pgdat(pgdat, 1UL << compound_order(page)))
1853 return 0;
Mel Gormanb32967f2012-11-19 12:35:47 +00001854
Hugh Dickins340ef392013-02-22 16:34:33 -08001855 if (isolate_lru_page(page))
1856 return 0;
Mel Gormanb32967f2012-11-19 12:35:47 +00001857
1858 /*
Hugh Dickins340ef392013-02-22 16:34:33 -08001859 * migrate_misplaced_transhuge_page() skips page migration's usual
1860 * check on page_count(), so we must do it here, now that the page
1861 * has been isolated: a GUP pin, or any other pin, prevents migration.
1862 * The expected page count is 3: 1 for page's mapcount and 1 for the
1863 * caller's pin and 1 for the reference taken by isolate_lru_page().
1864 */
1865 if (PageTransHuge(page) && page_count(page) != 3) {
1866 putback_lru_page(page);
1867 return 0;
1868 }
1869
1870 page_lru = page_is_file_cache(page);
Mel Gorman599d0c92016-07-28 15:45:31 -07001871 mod_node_page_state(page_pgdat(page), NR_ISOLATED_ANON + page_lru,
Hugh Dickins340ef392013-02-22 16:34:33 -08001872 hpage_nr_pages(page));
1873
1874 /*
1875 * Isolating the page has taken another reference, so the
1876 * caller's reference can be safely dropped without the page
1877 * disappearing underneath us during migration.
Mel Gormanb32967f2012-11-19 12:35:47 +00001878 */
1879 put_page(page);
Hugh Dickins340ef392013-02-22 16:34:33 -08001880 return 1;
Mel Gormanb32967f2012-11-19 12:35:47 +00001881}
1882
Mel Gormande466bd2013-12-18 17:08:42 -08001883bool pmd_trans_migrating(pmd_t pmd)
1884{
1885 struct page *page = pmd_page(pmd);
1886 return PageLocked(page);
1887}
1888
Mel Gormana8f60772012-11-14 21:41:46 +00001889/*
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001890 * Attempt to migrate a misplaced page to the specified destination
1891 * node. Caller is expected to have an elevated reference count on
1892 * the page that will be dropped by this function before returning.
1893 */
Mel Gorman1bc115d2013-10-07 11:29:05 +01001894int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma,
1895 int node)
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001896{
Mel Gormana8f60772012-11-14 21:41:46 +00001897 pg_data_t *pgdat = NODE_DATA(node);
Hugh Dickins340ef392013-02-22 16:34:33 -08001898 int isolated;
Mel Gormanb32967f2012-11-19 12:35:47 +00001899 int nr_remaining;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001900 LIST_HEAD(migratepages);
1901
1902 /*
Mel Gorman1bc115d2013-10-07 11:29:05 +01001903 * Don't migrate file pages that are mapped in multiple processes
1904 * with execute permissions as they are probably shared libraries.
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001905 */
Mel Gorman1bc115d2013-10-07 11:29:05 +01001906 if (page_mapcount(page) != 1 && page_is_file_cache(page) &&
1907 (vma->vm_flags & VM_EXEC))
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001908 goto out;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001909
Mel Gormana8f60772012-11-14 21:41:46 +00001910 /*
1911 * Rate-limit the amount of data that is being migrated to a node.
1912 * Optimal placement is no good if the memory bus is saturated and
1913 * all the time is being spent migrating!
1914 */
Hugh Dickins340ef392013-02-22 16:34:33 -08001915 if (numamigrate_update_ratelimit(pgdat, 1))
Mel Gormana8f60772012-11-14 21:41:46 +00001916 goto out;
Mel Gormana8f60772012-11-14 21:41:46 +00001917
Mel Gormanb32967f2012-11-19 12:35:47 +00001918 isolated = numamigrate_isolate_page(pgdat, page);
1919 if (!isolated)
1920 goto out;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001921
Mel Gormanb32967f2012-11-19 12:35:47 +00001922 list_add(&page->lru, &migratepages);
Hugh Dickins9c620e22013-02-22 16:35:14 -08001923 nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_page,
David Rientjes68711a72014-06-04 16:08:25 -07001924 NULL, node, MIGRATE_ASYNC,
1925 MR_NUMA_MISPLACED);
Mel Gormanb32967f2012-11-19 12:35:47 +00001926 if (nr_remaining) {
Joonsoo Kim59c82b72014-01-21 15:51:17 -08001927 if (!list_empty(&migratepages)) {
1928 list_del(&page->lru);
Mel Gorman599d0c92016-07-28 15:45:31 -07001929 dec_node_page_state(page, NR_ISOLATED_ANON +
Joonsoo Kim59c82b72014-01-21 15:51:17 -08001930 page_is_file_cache(page));
1931 putback_lru_page(page);
1932 }
Mel Gormanb32967f2012-11-19 12:35:47 +00001933 isolated = 0;
1934 } else
1935 count_vm_numa_event(NUMA_PAGE_MIGRATE);
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001936 BUG_ON(!list_empty(&migratepages));
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001937 return isolated;
Hugh Dickins340ef392013-02-22 16:34:33 -08001938
1939out:
1940 put_page(page);
1941 return 0;
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02001942}
Mel Gorman220018d2012-12-05 09:32:56 +00001943#endif /* CONFIG_NUMA_BALANCING */
Mel Gormanb32967f2012-11-19 12:35:47 +00001944
Mel Gorman220018d2012-12-05 09:32:56 +00001945#if defined(CONFIG_NUMA_BALANCING) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
Hugh Dickins340ef392013-02-22 16:34:33 -08001946/*
1947 * Migrates a THP to a given target node. page must be locked and is unlocked
1948 * before returning.
1949 */
Mel Gormanb32967f2012-11-19 12:35:47 +00001950int migrate_misplaced_transhuge_page(struct mm_struct *mm,
1951 struct vm_area_struct *vma,
1952 pmd_t *pmd, pmd_t entry,
1953 unsigned long address,
1954 struct page *page, int node)
1955{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001956 spinlock_t *ptl;
Mel Gormanb32967f2012-11-19 12:35:47 +00001957 pg_data_t *pgdat = NODE_DATA(node);
1958 int isolated = 0;
1959 struct page *new_page = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001960 int page_lru = page_is_file_cache(page);
Mel Gormanf714f4f2013-12-18 17:08:33 -08001961 unsigned long mmun_start = address & HPAGE_PMD_MASK;
1962 unsigned long mmun_end = mmun_start + HPAGE_PMD_SIZE;
Mel Gormanb32967f2012-11-19 12:35:47 +00001963
1964 /*
Mel Gormanb32967f2012-11-19 12:35:47 +00001965 * Rate-limit the amount of data that is being migrated to a node.
1966 * Optimal placement is no good if the memory bus is saturated and
1967 * all the time is being spent migrating!
1968 */
Mel Gormand28d43352012-11-29 09:24:36 +00001969 if (numamigrate_update_ratelimit(pgdat, HPAGE_PMD_NR))
Mel Gormanb32967f2012-11-19 12:35:47 +00001970 goto out_dropref;
1971
1972 new_page = alloc_pages_node(node,
Vlastimil Babka25160352016-07-28 15:49:25 -07001973 (GFP_TRANSHUGE_LIGHT | __GFP_THISNODE),
Johannes Weinere97ca8e52014-03-10 15:49:43 -07001974 HPAGE_PMD_ORDER);
Hugh Dickins340ef392013-02-22 16:34:33 -08001975 if (!new_page)
1976 goto out_fail;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08001977 prep_transhuge_page(new_page);
Hugh Dickins340ef392013-02-22 16:34:33 -08001978
Mel Gormanb32967f2012-11-19 12:35:47 +00001979 isolated = numamigrate_isolate_page(pgdat, page);
Hugh Dickins340ef392013-02-22 16:34:33 -08001980 if (!isolated) {
Mel Gormanb32967f2012-11-19 12:35:47 +00001981 put_page(new_page);
Hugh Dickins340ef392013-02-22 16:34:33 -08001982 goto out_fail;
Mel Gormanb32967f2012-11-19 12:35:47 +00001983 }
Mel Gormanb0943d62013-12-18 17:08:46 -08001984
Mel Gormanb32967f2012-11-19 12:35:47 +00001985 /* Prepare a page as a migration target */
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -08001986 __SetPageLocked(new_page);
Shaohua Lid44d3632017-05-03 14:52:26 -07001987 if (PageSwapBacked(page))
1988 __SetPageSwapBacked(new_page);
Mel Gormanb32967f2012-11-19 12:35:47 +00001989
1990 /* anon mapping, we can simply copy page->mapping to the new page: */
1991 new_page->mapping = page->mapping;
1992 new_page->index = page->index;
1993 migrate_page_copy(new_page, page);
1994 WARN_ON(PageLRU(new_page));
1995
1996 /* Recheck the target PMD */
Mel Gormanf714f4f2013-12-18 17:08:33 -08001997 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001998 ptl = pmd_lock(mm, pmd);
Will Deaconf4e177d2017-07-10 15:48:31 -07001999 if (unlikely(!pmd_same(*pmd, entry) || !page_ref_freeze(page, 2))) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002000 spin_unlock(ptl);
Mel Gormanf714f4f2013-12-18 17:08:33 -08002001 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Mel Gormanb32967f2012-11-19 12:35:47 +00002002
2003 /* Reverse changes made by migrate_page_copy() */
2004 if (TestClearPageActive(new_page))
2005 SetPageActive(page);
2006 if (TestClearPageUnevictable(new_page))
2007 SetPageUnevictable(page);
Mel Gormanb32967f2012-11-19 12:35:47 +00002008
2009 unlock_page(new_page);
2010 put_page(new_page); /* Free it */
2011
Mel Gormana54a4072013-10-07 11:28:46 +01002012 /* Retake the callers reference and putback on LRU */
2013 get_page(page);
Mel Gormanb32967f2012-11-19 12:35:47 +00002014 putback_lru_page(page);
Mel Gorman599d0c92016-07-28 15:45:31 -07002015 mod_node_page_state(page_pgdat(page),
Mel Gormana54a4072013-10-07 11:28:46 +01002016 NR_ISOLATED_ANON + page_lru, -HPAGE_PMD_NR);
Mel Gormaneb4489f62013-12-18 17:08:39 -08002017
2018 goto out_unlock;
Mel Gormanb32967f2012-11-19 12:35:47 +00002019 }
2020
Kirill A. Shutemov10102452016-07-26 15:25:29 -07002021 entry = mk_huge_pmd(new_page, vma->vm_page_prot);
Mel Gorman2b4847e2013-12-18 17:08:32 -08002022 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Mel Gormanb32967f2012-11-19 12:35:47 +00002023
Mel Gorman2b4847e2013-12-18 17:08:32 -08002024 /*
2025 * Clear the old entry under pagetable lock and establish the new PTE.
2026 * Any parallel GUP will either observe the old page blocking on the
2027 * page lock, block on the page table lock or observe the new page.
2028 * The SetPageUptodate on the new page and page_add_new_anon_rmap
2029 * guarantee the copy is visible before the pagetable update.
2030 */
Mel Gormanf714f4f2013-12-18 17:08:33 -08002031 flush_cache_range(vma, mmun_start, mmun_end);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002032 page_add_anon_rmap(new_page, vma, mmun_start, true);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07002033 pmdp_huge_clear_flush_notify(vma, mmun_start, pmd);
Mel Gormanf714f4f2013-12-18 17:08:33 -08002034 set_pmd_at(mm, mmun_start, pmd, entry);
Stephen Rothwellce4a9cc2012-12-10 19:50:57 +11002035 update_mmu_cache_pmd(vma, address, &entry);
Mel Gorman2b4847e2013-12-18 17:08:32 -08002036
Will Deaconf4e177d2017-07-10 15:48:31 -07002037 page_ref_unfreeze(page, 2);
Hugh Dickins51afb122015-11-05 18:49:37 -08002038 mlock_migrate_page(new_page, page);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002039 page_remove_rmap(page, true);
Vlastimil Babka7cd12b42016-03-15 14:56:18 -07002040 set_page_owner_migrate_reason(new_page, MR_NUMA_MISPLACED);
Mel Gorman2b4847e2013-12-18 17:08:32 -08002041
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002042 spin_unlock(ptl);
Mel Gormanf714f4f2013-12-18 17:08:33 -08002043 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Mel Gormanb32967f2012-11-19 12:35:47 +00002044
Mel Gorman11de9922014-06-04 16:07:41 -07002045 /* Take an "isolate" reference and put new page on the LRU. */
2046 get_page(new_page);
2047 putback_lru_page(new_page);
2048
Mel Gormanb32967f2012-11-19 12:35:47 +00002049 unlock_page(new_page);
2050 unlock_page(page);
2051 put_page(page); /* Drop the rmap reference */
2052 put_page(page); /* Drop the LRU isolation reference */
2053
2054 count_vm_events(PGMIGRATE_SUCCESS, HPAGE_PMD_NR);
2055 count_vm_numa_events(NUMA_PAGE_MIGRATE, HPAGE_PMD_NR);
2056
Mel Gorman599d0c92016-07-28 15:45:31 -07002057 mod_node_page_state(page_pgdat(page),
Mel Gormanb32967f2012-11-19 12:35:47 +00002058 NR_ISOLATED_ANON + page_lru,
2059 -HPAGE_PMD_NR);
2060 return isolated;
2061
Hugh Dickins340ef392013-02-22 16:34:33 -08002062out_fail:
2063 count_vm_events(PGMIGRATE_FAIL, HPAGE_PMD_NR);
Mel Gormanb32967f2012-11-19 12:35:47 +00002064out_dropref:
Mel Gorman2b4847e2013-12-18 17:08:32 -08002065 ptl = pmd_lock(mm, pmd);
2066 if (pmd_same(*pmd, entry)) {
Mel Gorman4d942462015-02-12 14:58:28 -08002067 entry = pmd_modify(entry, vma->vm_page_prot);
Mel Gormanf714f4f2013-12-18 17:08:33 -08002068 set_pmd_at(mm, mmun_start, pmd, entry);
Mel Gorman2b4847e2013-12-18 17:08:32 -08002069 update_mmu_cache_pmd(vma, address, &entry);
2070 }
2071 spin_unlock(ptl);
Mel Gormana54a4072013-10-07 11:28:46 +01002072
Mel Gormaneb4489f62013-12-18 17:08:39 -08002073out_unlock:
Hugh Dickins340ef392013-02-22 16:34:33 -08002074 unlock_page(page);
Mel Gormanb32967f2012-11-19 12:35:47 +00002075 put_page(page);
Mel Gormanb32967f2012-11-19 12:35:47 +00002076 return 0;
2077}
Peter Zijlstra7039e1d2012-10-25 14:16:34 +02002078#endif /* CONFIG_NUMA_BALANCING */
2079
2080#endif /* CONFIG_NUMA */