16 #include <linux/export.h>
30 #include <linux/mempolicy.h>
39 #include <asm/tlbflush.h>
81 page_is_file_cache(page));
90 unsigned long addr,
void *old)
115 if (pmd_trans_huge(*pmd))
127 ptl = pte_lockptr(mm, pmd);
132 if (!is_swap_pte(pte))
135 entry = pte_to_swp_entry(pte);
137 if (!is_migration_entry(entry) ||
143 if (is_write_migration_entry(entry))
145 #ifdef CONFIG_HUGETLB_PAGE
154 hugepage_add_anon_rmap(
new, vma, addr);
157 }
else if (PageAnon(
new))
165 pte_unmap_unlock(ptep, ptl);
174 static void remove_migration_ptes(
struct page *old,
struct page *
new)
176 rmap_walk(
new, remove_migration_pte, old);
192 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
194 if (!is_swap_pte(pte))
197 entry = pte_to_swp_entry(pte);
198 if (!is_migration_entry(entry))
210 if (!get_page_unless_zero(page))
212 pte_unmap_unlock(ptep, ptl);
213 wait_on_page_locked(page);
217 pte_unmap_unlock(ptep, ptl);
222 static bool buffer_migrate_lock_buffers(
struct buffer_head *
head,
225 struct buffer_head *bh =
head;
232 bh = bh->b_this_page;
234 }
while (bh != head);
242 if (!trylock_buffer(bh)) {
247 struct buffer_head *failed_bh = bh;
250 while (bh != failed_bh) {
253 bh = bh->b_this_page;
258 bh = bh->b_this_page;
259 }
while (bh != head);
263 static inline bool buffer_migrate_lock_buffers(
struct buffer_head *head,
287 if (page_count(page) != 1)
297 expected_count = 2 + page_has_private(page);
298 if (page_count(page) != expected_count ||
299 radix_tree_deref_slot_protected(pslot, &mapping->
tree_lock) != page) {
304 if (!page_freeze_refs(page, expected_count)) {
317 !buffer_migrate_lock_buffers(head, mode)) {
318 page_unfreeze_refs(page, expected_count);
327 if (PageSwapCache(page)) {
328 SetPageSwapCache(newpage);
329 set_page_private(newpage, page_private(page));
332 radix_tree_replace_slot(pslot, newpage);
339 page_unfreeze_refs(page, expected_count - 1);
353 if (!PageSwapCache(page) && PageSwapBacked(page)) {
354 __dec_zone_page_state(page,
NR_SHMEM);
355 __inc_zone_page_state(newpage,
NR_SHMEM);
367 struct page *newpage,
struct page *page)
373 if (page_count(page) != 1)
383 expected_count = 2 + page_has_private(page);
384 if (page_count(page) != expected_count ||
385 radix_tree_deref_slot_protected(pslot, &mapping->
tree_lock) != page) {
390 if (!page_freeze_refs(page, expected_count)) {
397 radix_tree_replace_slot(pslot, newpage);
399 page_unfreeze_refs(page, expected_count - 1);
413 copy_highpage(newpage, page);
416 SetPageError(newpage);
417 if (PageReferenced(page))
418 SetPageReferenced(newpage);
419 if (PageUptodate(page))
420 SetPageUptodate(newpage);
421 if (TestClearPageActive(page)) {
423 SetPageActive(newpage);
424 }
else if (TestClearPageUnevictable(page))
425 SetPageUnevictable(newpage);
426 if (PageChecked(page))
427 SetPageChecked(newpage);
428 if (PageMappedToDisk(page))
429 SetPageMappedToDisk(newpage);
431 if (PageDirty(page)) {
440 if (PageSwapBacked(page))
441 SetPageDirty(newpage);
446 mlock_migrate_page(newpage, page);
447 ksm_migrate_page(newpage, page);
449 ClearPageSwapCache(page);
450 ClearPagePrivate(page);
451 set_page_private(page, 0);
457 if (PageWriteback(newpage))
467 struct page *newpage,
struct page *page)
480 struct page *newpage,
struct page *page,
485 BUG_ON(PageWriteback(page));
487 rc = migrate_page_move_mapping(mapping, newpage, page,
NULL, mode);
504 struct page *newpage,
struct page *page,
enum migrate_mode mode)
506 struct buffer_head *bh, *
head;
509 if (!page_has_buffers(page))
512 head = page_buffers(page);
514 rc = migrate_page_move_mapping(mapping, newpage, page, head, mode);
525 BUG_ON(!buffer_migrate_lock_buffers(head, mode));
527 ClearPagePrivate(page);
528 set_page_private(newpage, page_private(page));
529 set_page_private(page, 0);
536 bh = bh->b_this_page;
538 }
while (bh != head);
540 SetPagePrivate(newpage);
548 bh = bh->b_this_page;
550 }
while (bh != head);
560 static int writeout(
struct address_space *mapping,
struct page *page)
571 if (!mapping->
a_ops->writepage)
587 remove_migration_ptes(page, page);
589 rc = mapping->
a_ops->writepage(page, &wbc);
601 static int fallback_migrate_page(
struct address_space *mapping,
602 struct page *newpage,
struct page *page,
enum migrate_mode mode)
604 if (PageDirty(page)) {
608 return writeout(mapping, page);
615 if (page_has_private(page) &&
633 static int move_to_new_page(
struct page *newpage,
struct page *page,
644 if (!trylock_page(newpage))
650 if (PageSwapBacked(page))
651 SetPageSwapBacked(newpage);
653 mapping = page_mapping(page);
656 else if (mapping->
a_ops->migratepage)
663 rc = mapping->
a_ops->migratepage(mapping,
664 newpage, page, mode);
666 rc = fallback_migrate_page(mapping, newpage, page, mode);
672 remove_migration_ptes(page, newpage);
681 static int __unmap_and_move(
struct page *page,
struct page *newpage,
685 int remap_swapcache = 1;
689 if (!trylock_page(page)) {
721 if (PageKsm(page) && !offlining) {
729 if (PageWriteback(page)) {
742 wait_on_page_writeback(page);
752 if (PageAnon(page)) {
762 }
else if (PageSwapCache(page)) {
795 if (page_has_private(page)) {
806 if (!page_mapped(page))
807 rc = move_to_new_page(newpage, page, remap_swapcache, mode);
809 if (rc && remap_swapcache)
810 remove_migration_ptes(page, page);
814 put_anon_vma(anon_vma);
828 static int unmap_and_move(
new_page_t get_new_page,
unsigned long private,
829 struct page *page,
int force,
bool offlining,
834 struct page *newpage = get_new_page(page,
private, &result);
839 if (page_count(page) == 1) {
848 rc = __unmap_and_move(page, newpage, force, offlining, mode);
859 page_is_file_cache(page));
871 *result = page_to_nid(newpage);
894 static int unmap_and_move_huge_page(
new_page_t get_new_page,
895 unsigned long private,
struct page *hpage,
896 int force,
bool offlining,
901 struct page *new_hpage = get_new_page(hpage,
private, &result);
902 struct anon_vma *anon_vma =
NULL;
909 if (!trylock_page(hpage)) {
920 if (!page_mapped(hpage))
921 rc = move_to_new_page(new_hpage, hpage, 1, mode);
924 remove_migration_ptes(hpage, hpage);
927 put_anon_vma(anon_vma);
939 *result = page_to_nid(new_hpage);
960 new_page_t get_new_page,
unsigned long private,
bool offlining,
974 for(pass = 0; pass < 10 &&
retry; pass++) {
980 rc = unmap_and_move(get_new_page,
private,
981 page, pass > 2, offlining,
1007 return nr_failed +
retry;
1011 unsigned long private,
bool offlining,
1016 for (pass = 0; pass < 10; pass++) {
1017 rc = unmap_and_move_huge_page(get_new_page,
1018 private, hpage, pass > 2, offlining,
1042 struct page_to_node {
1049 static struct page *new_page_node(
struct page *
p,
unsigned long private,
1052 struct page_to_node *
pm = (
struct page_to_node *)
private;
1060 *result = &pm->status;
1062 return alloc_pages_exact_node(pm->node,
1072 static int do_move_page_to_node_array(
struct mm_struct *mm,
1073 struct page_to_node *pm,
1077 struct page_to_node *
pp;
1091 if (!vma || pp->addr < vma->
vm_start || !vma_migratable(vma))
1094 page =
follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT);
1096 err = PTR_ERR(page);
1105 if (PageReserved(page) || PageKsm(page))
1109 err = page_to_nid(page);
1111 if (err == pp->node)
1118 if (page_mapcount(page) > 1 &&
1126 page_is_file_cache(page));
1140 if (!list_empty(&pagelist)) {
1156 unsigned long nr_pages,
1157 const void __user * __user *
pages,
1158 const int __user *nodes,
1161 struct page_to_node *pm;
1162 unsigned long chunk_nr_pages;
1163 unsigned long chunk_start;
1177 chunk_nr_pages = (
PAGE_SIZE /
sizeof(
struct page_to_node)) - 1;
1179 for (chunk_start = 0;
1180 chunk_start < nr_pages;
1181 chunk_start += chunk_nr_pages) {
1184 if (chunk_start + chunk_nr_pages > nr_pages)
1185 chunk_nr_pages = nr_pages - chunk_start;
1188 for (j = 0; j < chunk_nr_pages; j++) {
1193 if (
get_user(p, pages + j + chunk_start))
1195 pm[
j].addr = (
unsigned long) p;
1197 if (
get_user(node, nodes + j + chunk_start))
1218 err = do_move_page_to_node_array(mm, pm,
1224 for (j = 0; j < chunk_nr_pages; j++)
1225 if (
put_user(pm[j].status, status + j + chunk_start)) {
1241 static void do_pages_stat_array(
struct mm_struct *mm,
unsigned long nr_pages,
1242 const void __user **pages,
int *status)
1248 for (i = 0; i < nr_pages; i++) {
1249 unsigned long addr = (
unsigned long)(*pages);
1255 if (!vma || addr < vma->vm_start)
1260 err = PTR_ERR(page);
1266 if (!page || PageReserved(page) || PageKsm(page))
1269 err = page_to_nid(page);
1284 static int do_pages_stat(
struct mm_struct *mm,
unsigned long nr_pages,
1285 const void __user * __user *pages,
1288 #define DO_PAGES_STAT_CHUNK_NR 16
1289 const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
1290 int chunk_status[DO_PAGES_STAT_CHUNK_NR];
1293 unsigned long chunk_nr;
1295 chunk_nr = nr_pages;
1296 if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
1297 chunk_nr = DO_PAGES_STAT_CHUNK_NR;
1299 if (
copy_from_user(chunk_pages, pages, chunk_nr *
sizeof(*chunk_pages)))
1302 do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
1304 if (
copy_to_user(status, chunk_status, chunk_nr *
sizeof(*status)))
1309 nr_pages -= chunk_nr;
1311 return nr_pages ? -
EFAULT : 0;
1319 const void __user * __user *, pages,
1320 const int __user *, nodes,
1321 int __user *, status,
int, flags)
1352 if (!uid_eq(cred->
euid, tcred->suid) && !uid_eq(cred->
euid, tcred->uid) &&
1353 !uid_eq(cred->
uid, tcred->suid) && !uid_eq(cred->
uid, tcred->uid) &&
1367 put_task_struct(task);
1373 err = do_pages_move(mm, task_nodes, nr_pages, pages,
1374 nodes, status, flags);
1376 err = do_pages_stat(mm, nr_pages, pages, status);
1382 put_task_struct(task);
1397 for (vma = mm->
mmap; vma && !err; vma = vma->
vm_next) {
1399 err = vma->
vm_ops->migrate(vma, to, from, flags);