migrate.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. /*
  2. * Memory Migration functionality - linux/mm/migration.c
  3. *
  4. * Copyright (C) 2006 Silicon Graphics, Inc., Christoph Lameter
  5. *
  6. * Page migration was first developed in the context of the memory hotplug
  7. * project. The main authors of the migration code are:
  8. *
  9. * IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
  10. * Hirokazu Takahashi <taka@valinux.co.jp>
  11. * Dave Hansen <haveblue@us.ibm.com>
  12. * Christoph Lameter
  13. */
  14. #include <linux/migrate.h>
  15. #include <linux/export.h>
  16. #include <linux/swap.h>
  17. #include <linux/swapops.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/buffer_head.h>
  20. #include <linux/mm_inline.h>
  21. #include <linux/nsproxy.h>
  22. #include <linux/pagevec.h>
  23. #include <linux/ksm.h>
  24. #include <linux/rmap.h>
  25. #include <linux/topology.h>
  26. #include <linux/cpu.h>
  27. #include <linux/cpuset.h>
  28. #include <linux/writeback.h>
  29. #include <linux/mempolicy.h>
  30. #include <linux/vmalloc.h>
  31. #include <linux/security.h>
  32. #include <linux/memcontrol.h>
  33. #include <linux/syscalls.h>
  34. #include <linux/hugetlb.h>
  35. #include <linux/gfp.h>
  36. #include <asm/tlbflush.h>
  37. #include "internal.h"
  38. /*
  39. * migrate_prep() needs to be called before we start compiling a list of pages
  40. * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is
  41. * undesirable, use migrate_prep_local()
  42. */
  43. int migrate_prep(void)
  44. {
  45. /*
  46. * Clear the LRU lists so pages can be isolated.
  47. * Note that pages may be moved off the LRU after we have
  48. * drained them. Those pages will fail to migrate like other
  49. * pages that may be busy.
  50. */
  51. lru_add_drain_all();
  52. return 0;
  53. }
  54. /* Do the necessary work of migrate_prep but not if it involves other CPUs */
  55. int migrate_prep_local(void)
  56. {
  57. lru_add_drain();
  58. return 0;
  59. }
  60. /*
  61. * Add isolated pages on the list back to the LRU under page lock
  62. * to avoid leaking evictable pages back onto unevictable list.
  63. */
  64. void putback_lru_pages(struct list_head *l)
  65. {
  66. struct page *page;
  67. struct page *page2;
  68. list_for_each_entry_safe(page, page2, l, lru) {
  69. list_del(&page->lru);
  70. dec_zone_page_state(page, NR_ISOLATED_ANON +
  71. page_is_file_cache(page));
  72. putback_lru_page(page);
  73. }
  74. }
  75. /*
  76. * Restore a potential migration pte to a working pte entry
  77. */
  78. static int remove_migration_pte(struct page *new, struct vm_area_struct *vma,
  79. unsigned long addr, void *old)
  80. {
  81. struct mm_struct *mm = vma->vm_mm;
  82. swp_entry_t entry;
  83. pgd_t *pgd;
  84. pud_t *pud;
  85. pmd_t *pmd;
  86. pte_t *ptep, pte;
  87. spinlock_t *ptl;
  88. if (unlikely(PageHuge(new))) {
  89. ptep = huge_pte_offset(mm, addr);
  90. if (!ptep)
  91. goto out;
  92. ptl = &mm->page_table_lock;
  93. } else {
  94. pgd = pgd_offset(mm, addr);
  95. if (!pgd_present(*pgd))
  96. goto out;
  97. pud = pud_offset(pgd, addr);
  98. if (!pud_present(*pud))
  99. goto out;
  100. pmd = pmd_offset(pud, addr);
  101. if (pmd_trans_huge(*pmd))
  102. goto out;
  103. if (!pmd_present(*pmd))
  104. goto out;
  105. ptep = pte_offset_map(pmd, addr);
  106. /*
  107. * Peek to check is_swap_pte() before taking ptlock? No, we
  108. * can race mremap's move_ptes(), which skips anon_vma lock.
  109. */
  110. ptl = pte_lockptr(mm, pmd);
  111. }
  112. spin_lock(ptl);
  113. pte = *ptep;
  114. if (!is_swap_pte(pte))
  115. goto unlock;
  116. entry = pte_to_swp_entry(pte);
  117. if (!is_migration_entry(entry) ||
  118. migration_entry_to_page(entry) != old)
  119. goto unlock;
  120. get_page(new);
  121. pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
  122. if (is_write_migration_entry(entry))
  123. pte = pte_mkwrite(pte);
  124. #ifdef CONFIG_HUGETLB_PAGE
  125. if (PageHuge(new))
  126. pte = pte_mkhuge(pte);
  127. #endif
  128. flush_cache_page(vma, addr, pte_pfn(pte));
  129. set_pte_at(mm, addr, ptep, pte);
  130. if (PageHuge(new)) {
  131. if (PageAnon(new))
  132. hugepage_add_anon_rmap(new, vma, addr);
  133. else
  134. page_dup_rmap(new);
  135. } else if (PageAnon(new))
  136. page_add_anon_rmap(new, vma, addr);
  137. else
  138. page_add_file_rmap(new);
  139. /* No need to invalidate - it was non-present before */
  140. update_mmu_cache(vma, addr, ptep);
  141. unlock:
  142. pte_unmap_unlock(ptep, ptl);
  143. out:
  144. return SWAP_AGAIN;
  145. }
  146. /*
  147. * Get rid of all migration entries and replace them by
  148. * references to the indicated page.
  149. */
  150. static void remove_migration_ptes(struct page *old, struct page *new)
  151. {
  152. rmap_walk(new, remove_migration_pte, old);
  153. }
  154. /*
  155. * Something used the pte of a page under migration. We need to
  156. * get to the page and wait until migration is finished.
  157. * When we return from this function the fault will be retried.
  158. */
  159. void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
  160. unsigned long address)
  161. {
  162. pte_t *ptep, pte;
  163. spinlock_t *ptl;
  164. swp_entry_t entry;
  165. struct page *page;
  166. ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
  167. pte = *ptep;
  168. if (!is_swap_pte(pte))
  169. goto out;
  170. entry = pte_to_swp_entry(pte);
  171. if (!is_migration_entry(entry))
  172. goto out;
  173. page = migration_entry_to_page(entry);
  174. /*
  175. * Once radix-tree replacement of page migration started, page_count
  176. * *must* be zero. And, we don't want to call wait_on_page_locked()
  177. * against a page without get_page().
  178. * So, we use get_page_unless_zero(), here. Even failed, page fault
  179. * will occur again.
  180. */
  181. if (!get_page_unless_zero(page))
  182. goto out;
  183. pte_unmap_unlock(ptep, ptl);
  184. wait_on_page_locked(page);
  185. put_page(page);
  186. return;
  187. out:
  188. pte_unmap_unlock(ptep, ptl);
  189. }
  190. /*
  191. * Replace the page in the mapping.
  192. *
  193. * The number of remaining references must be:
  194. * 1 for anonymous pages without a mapping
  195. * 2 for pages with a mapping
  196. * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
  197. */
  198. static int migrate_page_move_mapping(struct address_space *mapping,
  199. struct page *newpage, struct page *page)
  200. {
  201. int expected_count;
  202. void **pslot;
  203. if (!mapping) {
  204. /* Anonymous page without mapping */
  205. if (page_count(page) != 1)
  206. return -EAGAIN;
  207. return 0;
  208. }
  209. spin_lock_irq(&mapping->tree_lock);
  210. pslot = radix_tree_lookup_slot(&mapping->page_tree,
  211. page_index(page));
  212. expected_count = 2 + page_has_private(page);
  213. if (page_count(page) != expected_count ||
  214. radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
  215. spin_unlock_irq(&mapping->tree_lock);
  216. return -EAGAIN;
  217. }
  218. if (!page_freeze_refs(page, expected_count)) {
  219. spin_unlock_irq(&mapping->tree_lock);
  220. return -EAGAIN;
  221. }
  222. /*
  223. * Now we know that no one else is looking at the page.
  224. */
  225. get_page(newpage); /* add cache reference */
  226. if (PageSwapCache(page)) {
  227. SetPageSwapCache(newpage);
  228. set_page_private(newpage, page_private(page));
  229. }
  230. radix_tree_replace_slot(pslot, newpage);
  231. /*
  232. * Drop cache reference from old page by unfreezing
  233. * to one less reference.
  234. * We know this isn't the last reference.
  235. */
  236. page_unfreeze_refs(page, expected_count - 1);
  237. /*
  238. * If moved to a different zone then also account
  239. * the page for that zone. Other VM counters will be
  240. * taken care of when we establish references to the
  241. * new page and drop references to the old page.
  242. *
  243. * Note that anonymous pages are accounted for
  244. * via NR_FILE_PAGES and NR_ANON_PAGES if they
  245. * are mapped to swap space.
  246. */
  247. __dec_zone_page_state(page, NR_FILE_PAGES);
  248. __inc_zone_page_state(newpage, NR_FILE_PAGES);
  249. if (!PageSwapCache(page) && PageSwapBacked(page)) {
  250. __dec_zone_page_state(page, NR_SHMEM);
  251. __inc_zone_page_state(newpage, NR_SHMEM);
  252. }
  253. spin_unlock_irq(&mapping->tree_lock);
  254. return 0;
  255. }
  256. /*
  257. * The expected number of remaining references is the same as that
  258. * of migrate_page_move_mapping().
  259. */
  260. int migrate_huge_page_move_mapping(struct address_space *mapping,
  261. struct page *newpage, struct page *page)
  262. {
  263. int expected_count;
  264. void **pslot;
  265. if (!mapping) {
  266. if (page_count(page) != 1)
  267. return -EAGAIN;
  268. return 0;
  269. }
  270. spin_lock_irq(&mapping->tree_lock);
  271. pslot = radix_tree_lookup_slot(&mapping->page_tree,
  272. page_index(page));
  273. expected_count = 2 + page_has_private(page);
  274. if (page_count(page) != expected_count ||
  275. radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
  276. spin_unlock_irq(&mapping->tree_lock);
  277. return -EAGAIN;
  278. }
  279. if (!page_freeze_refs(page, expected_count)) {
  280. spin_unlock_irq(&mapping->tree_lock);
  281. return -EAGAIN;
  282. }
  283. get_page(newpage);
  284. radix_tree_replace_slot(pslot, newpage);
  285. page_unfreeze_refs(page, expected_count - 1);
  286. spin_unlock_irq(&mapping->tree_lock);
  287. return 0;
  288. }
  289. /*
  290. * Copy the page to its new location
  291. */
  292. void migrate_page_copy(struct page *newpage, struct page *page)
  293. {
  294. if (PageHuge(page))
  295. copy_huge_page(newpage, page);
  296. else
  297. copy_highpage(newpage, page);
  298. if (PageError(page))
  299. SetPageError(newpage);
  300. if (PageReferenced(page))
  301. SetPageReferenced(newpage);
  302. if (PageUptodate(page))
  303. SetPageUptodate(newpage);
  304. if (TestClearPageActive(page)) {
  305. VM_BUG_ON(PageUnevictable(page));
  306. SetPageActive(newpage);
  307. } else if (TestClearPageUnevictable(page))
  308. SetPageUnevictable(newpage);
  309. if (PageChecked(page))
  310. SetPageChecked(newpage);
  311. if (PageMappedToDisk(page))
  312. SetPageMappedToDisk(newpage);
  313. if (PageDirty(page)) {
  314. clear_page_dirty_for_io(page);
  315. /*
  316. * Want to mark the page and the radix tree as dirty, and
  317. * redo the accounting that clear_page_dirty_for_io undid,
  318. * but we can't use set_page_dirty because that function
  319. * is actually a signal that all of the page has become dirty.
  320. * Whereas only part of our page may be dirty.
  321. */
  322. __set_page_dirty_nobuffers(newpage);
  323. }
  324. mlock_migrate_page(newpage, page);
  325. ksm_migrate_page(newpage, page);
  326. ClearPageSwapCache(page);
  327. ClearPagePrivate(page);
  328. set_page_private(page, 0);
  329. page->mapping = NULL;
  330. /*
  331. * If any waiters have accumulated on the new page then
  332. * wake them up.
  333. */
  334. if (PageWriteback(newpage))
  335. end_page_writeback(newpage);
  336. }
  337. /************************************************************
  338. * Migration functions
  339. ***********************************************************/
  340. /* Always fail migration. Used for mappings that are not movable */
  341. int fail_migrate_page(struct address_space *mapping,
  342. struct page *newpage, struct page *page)
  343. {
  344. return -EIO;
  345. }
  346. EXPORT_SYMBOL(fail_migrate_page);
  347. /*
  348. * Common logic to directly migrate a single page suitable for
  349. * pages that do not use PagePrivate/PagePrivate2.
  350. *
  351. * Pages are locked upon entry and exit.
  352. */
  353. int migrate_page(struct address_space *mapping,
  354. struct page *newpage, struct page *page)
  355. {
  356. int rc;
  357. BUG_ON(PageWriteback(page)); /* Writeback must be complete */
  358. rc = migrate_page_move_mapping(mapping, newpage, page);
  359. if (rc)
  360. return rc;
  361. migrate_page_copy(newpage, page);
  362. return 0;
  363. }
  364. EXPORT_SYMBOL(migrate_page);
  365. #ifdef CONFIG_BLOCK
  366. /*
  367. * Migration function for pages with buffers. This function can only be used
  368. * if the underlying filesystem guarantees that no other references to "page"
  369. * exist.
  370. */
  371. int buffer_migrate_page(struct address_space *mapping,
  372. struct page *newpage, struct page *page)
  373. {
  374. struct buffer_head *bh, *head;
  375. int rc;
  376. if (!page_has_buffers(page))
  377. return migrate_page(mapping, newpage, page);
  378. head = page_buffers(page);
  379. rc = migrate_page_move_mapping(mapping, newpage, page);
  380. if (rc)
  381. return rc;
  382. bh = head;
  383. do {
  384. get_bh(bh);
  385. lock_buffer(bh);
  386. bh = bh->b_this_page;
  387. } while (bh != head);
  388. ClearPagePrivate(page);
  389. set_page_private(newpage, page_private(page));
  390. set_page_private(page, 0);
  391. put_page(page);
  392. get_page(newpage);
  393. bh = head;
  394. do {
  395. set_bh_page(bh, newpage, bh_offset(bh));
  396. bh = bh->b_this_page;
  397. } while (bh != head);
  398. SetPagePrivate(newpage);
  399. migrate_page_copy(newpage, page);
  400. bh = head;
  401. do {
  402. unlock_buffer(bh);
  403. put_bh(bh);
  404. bh = bh->b_this_page;
  405. } while (bh != head);
  406. return 0;
  407. }
  408. EXPORT_SYMBOL(buffer_migrate_page);
  409. #endif
  410. /*
  411. * Writeback a page to clean the dirty state
  412. */
  413. static int writeout(struct address_space *mapping, struct page *page)
  414. {
  415. struct writeback_control wbc = {
  416. .sync_mode = WB_SYNC_NONE,
  417. .nr_to_write = 1,
  418. .range_start = 0,
  419. .range_end = LLONG_MAX,
  420. .for_reclaim = 1
  421. };
  422. int rc;
  423. if (!mapping->a_ops->writepage)
  424. /* No write method for the address space */
  425. return -EINVAL;
  426. if (!clear_page_dirty_for_io(page))
  427. /* Someone else already triggered a write */
  428. return -EAGAIN;
  429. /*
  430. * A dirty page may imply that the underlying filesystem has
  431. * the page on some queue. So the page must be clean for
  432. * migration. Writeout may mean we loose the lock and the
  433. * page state is no longer what we checked for earlier.
  434. * At this point we know that the migration attempt cannot
  435. * be successful.
  436. */
  437. remove_migration_ptes(page, page);
  438. rc = mapping->a_ops->writepage(page, &wbc);
  439. if (rc != AOP_WRITEPAGE_ACTIVATE)
  440. /* unlocked. Relock */
  441. lock_page(page);
  442. return (rc < 0) ? -EIO : -EAGAIN;
  443. }
  444. /*
  445. * Default handling if a filesystem does not provide a migration function.
  446. */
  447. static int fallback_migrate_page(struct address_space *mapping,
  448. struct page *newpage, struct page *page)
  449. {
  450. if (PageDirty(page))
  451. return writeout(mapping, page);
  452. /*
  453. * Buffers may be managed in a filesystem specific way.
  454. * We must have no buffers or drop them.
  455. */
  456. if (page_has_private(page) &&
  457. !try_to_release_page(page, GFP_KERNEL))
  458. return -EAGAIN;
  459. return migrate_page(mapping, newpage, page);
  460. }
  461. /*
  462. * Move a page to a newly allocated page
  463. * The page is locked and all ptes have been successfully removed.
  464. *
  465. * The new page will have replaced the old page if this function
  466. * is successful.
  467. *
  468. * Return value:
  469. * < 0 - error code
  470. * == 0 - success
  471. */
  472. static int move_to_new_page(struct page *newpage, struct page *page,
  473. int remap_swapcache, bool sync)
  474. {
  475. struct address_space *mapping;
  476. int rc;
  477. /*
  478. * Block others from accessing the page when we get around to
  479. * establishing additional references. We are the only one
  480. * holding a reference to the new page at this point.
  481. */
  482. if (!trylock_page(newpage))
  483. BUG();
  484. /* Prepare mapping for the new page.*/
  485. newpage->index = page->index;
  486. newpage->mapping = page->mapping;
  487. if (PageSwapBacked(page))
  488. SetPageSwapBacked(newpage);
  489. mapping = page_mapping(page);
  490. if (!mapping)
  491. rc = migrate_page(mapping, newpage, page);
  492. else {
  493. /*
  494. * Do not writeback pages if !sync and migratepage is
  495. * not pointing to migrate_page() which is nonblocking
  496. * (swapcache/tmpfs uses migratepage = migrate_page).
  497. */
  498. if (PageDirty(page) && !sync &&
  499. mapping->a_ops->migratepage != migrate_page)
  500. rc = -EBUSY;
  501. else if (mapping->a_ops->migratepage)
  502. /*
  503. * Most pages have a mapping and most filesystems
  504. * should provide a migration function. Anonymous
  505. * pages are part of swap space which also has its
  506. * own migration function. This is the most common
  507. * path for page migration.
  508. */
  509. rc = mapping->a_ops->migratepage(mapping,
  510. newpage, page);
  511. else
  512. rc = fallback_migrate_page(mapping, newpage, page);
  513. }
  514. if (rc) {
  515. newpage->mapping = NULL;
  516. } else {
  517. if (remap_swapcache)
  518. remove_migration_ptes(page, newpage);
  519. }
  520. unlock_page(newpage);
  521. return rc;
  522. }
  523. static int __unmap_and_move(struct page *page, struct page *newpage,
  524. int force, bool offlining, bool sync)
  525. {
  526. int rc = -EAGAIN;
  527. int remap_swapcache = 1;
  528. int charge = 0;
  529. struct mem_cgroup *mem;
  530. struct anon_vma *anon_vma = NULL;
  531. if (!trylock_page(page)) {
  532. if (!force || !sync)
  533. goto out;
  534. /*
  535. * It's not safe for direct compaction to call lock_page.
  536. * For example, during page readahead pages are added locked
  537. * to the LRU. Later, when the IO completes the pages are
  538. * marked uptodate and unlocked. However, the queueing
  539. * could be merging multiple pages for one bio (e.g.
  540. * mpage_readpages). If an allocation happens for the
  541. * second or third page, the process can end up locking
  542. * the same page twice and deadlocking. Rather than
  543. * trying to be clever about what pages can be locked,
  544. * avoid the use of lock_page for direct compaction
  545. * altogether.
  546. */
  547. if (current->flags & PF_MEMALLOC)
  548. goto out;
  549. lock_page(page);
  550. }
  551. /*
  552. * Only memory hotplug's offline_pages() caller has locked out KSM,
  553. * and can safely migrate a KSM page. The other cases have skipped
  554. * PageKsm along with PageReserved - but it is only now when we have
  555. * the page lock that we can be certain it will not go KSM beneath us
  556. * (KSM will not upgrade a page from PageAnon to PageKsm when it sees
  557. * its pagecount raised, but only here do we take the page lock which
  558. * serializes that).
  559. */
  560. if (PageKsm(page) && !offlining) {
  561. rc = -EBUSY;
  562. goto unlock;
  563. }
  564. /* charge against new page */
  565. charge = mem_cgroup_prepare_migration(page, newpage, &mem, GFP_KERNEL);
  566. if (charge == -ENOMEM) {
  567. rc = -ENOMEM;
  568. goto unlock;
  569. }
  570. BUG_ON(charge);
  571. if (PageWriteback(page)) {
  572. /*
  573. * For !sync, there is no point retrying as the retry loop
  574. * is expected to be too short for PageWriteback to be cleared
  575. */
  576. if (!sync) {
  577. rc = -EBUSY;
  578. goto uncharge;
  579. }
  580. if (!force)
  581. goto uncharge;
  582. wait_on_page_writeback(page);
  583. }
  584. /*
  585. * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
  586. * we cannot notice that anon_vma is freed while we migrates a page.
  587. * This get_anon_vma() delays freeing anon_vma pointer until the end
  588. * of migration. File cache pages are no problem because of page_lock()
  589. * File Caches may use write_page() or lock_page() in migration, then,
  590. * just care Anon page here.
  591. */
  592. if (PageAnon(page)) {
  593. /*
  594. * Only page_lock_anon_vma() understands the subtleties of
  595. * getting a hold on an anon_vma from outside one of its mms.
  596. */
  597. anon_vma = page_get_anon_vma(page);
  598. if (anon_vma) {
  599. /*
  600. * Anon page
  601. */
  602. } else if (PageSwapCache(page)) {
  603. /*
  604. * We cannot be sure that the anon_vma of an unmapped
  605. * swapcache page is safe to use because we don't
  606. * know in advance if the VMA that this page belonged
  607. * to still exists. If the VMA and others sharing the
  608. * data have been freed, then the anon_vma could
  609. * already be invalid.
  610. *
  611. * To avoid this possibility, swapcache pages get
  612. * migrated but are not remapped when migration
  613. * completes
  614. */
  615. remap_swapcache = 0;
  616. } else {
  617. goto uncharge;
  618. }
  619. }
  620. /*
  621. * Corner case handling:
  622. * 1. When a new swap-cache page is read into, it is added to the LRU
  623. * and treated as swapcache but it has no rmap yet.
  624. * Calling try_to_unmap() against a page->mapping==NULL page will
  625. * trigger a BUG. So handle it here.
  626. * 2. An orphaned page (see truncate_complete_page) might have
  627. * fs-private metadata. The page can be picked up due to memory
  628. * offlining. Everywhere else except page reclaim, the page is
  629. * invisible to the vm, so the page can not be migrated. So try to
  630. * free the metadata, so the page can be freed.
  631. */
  632. if (!page->mapping) {
  633. VM_BUG_ON(PageAnon(page));
  634. if (page_has_private(page)) {
  635. try_to_free_buffers(page);
  636. goto uncharge;
  637. }
  638. goto skip_unmap;
  639. }
  640. /* Establish migration ptes or remove ptes */
  641. try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
  642. skip_unmap:
  643. if (!page_mapped(page))
  644. rc = move_to_new_page(newpage, page, remap_swapcache, sync);
  645. if (rc && remap_swapcache)
  646. remove_migration_ptes(page, page);
  647. /* Drop an anon_vma reference if we took one */
  648. if (anon_vma)
  649. put_anon_vma(anon_vma);
  650. uncharge:
  651. if (!charge)
  652. mem_cgroup_end_migration(mem, page, newpage, rc == 0);
  653. unlock:
  654. unlock_page(page);
  655. out:
  656. return rc;
  657. }
  658. /*
  659. * Obtain the lock on page, remove all ptes and migrate the page
  660. * to the newly allocated page in newpage.
  661. */
  662. static int unmap_and_move(new_page_t get_new_page, unsigned long private,
  663. struct page *page, int force, bool offlining, bool sync)
  664. {
  665. int rc = 0;
  666. int *result = NULL;
  667. struct page *newpage = get_new_page(page, private, &result);
  668. if (!newpage)
  669. return -ENOMEM;
  670. if (page_count(page) == 1) {
  671. /* page was freed from under us. So we are done. */
  672. goto out;
  673. }
  674. if (unlikely(PageTransHuge(page)))
  675. if (unlikely(split_huge_page(page)))
  676. goto out;
  677. rc = __unmap_and_move(page, newpage, force, offlining, sync);
  678. out:
  679. if (rc != -EAGAIN) {
  680. /*
  681. * A page that has been migrated has all references
  682. * removed and will be freed. A page that has not been
  683. * migrated will have kepts its references and be
  684. * restored.
  685. */
  686. list_del(&page->lru);
  687. dec_zone_page_state(page, NR_ISOLATED_ANON +
  688. page_is_file_cache(page));
  689. putback_lru_page(page);
  690. }
  691. /*
  692. * Move the new page to the LRU. If migration was not successful
  693. * then this will free the page.
  694. */
  695. putback_lru_page(newpage);
  696. if (result) {
  697. if (rc)
  698. *result = rc;
  699. else
  700. *result = page_to_nid(newpage);
  701. }
  702. return rc;
  703. }
  704. /*
  705. * Counterpart of unmap_and_move_page() for hugepage migration.
  706. *
  707. * This function doesn't wait the completion of hugepage I/O
  708. * because there is no race between I/O and migration for hugepage.
  709. * Note that currently hugepage I/O occurs only in direct I/O
  710. * where no lock is held and PG_writeback is irrelevant,
  711. * and writeback status of all subpages are counted in the reference
  712. * count of the head page (i.e. if all subpages of a 2MB hugepage are
  713. * under direct I/O, the reference of the head page is 512 and a bit more.)
  714. * This means that when we try to migrate hugepage whose subpages are
  715. * doing direct I/O, some references remain after try_to_unmap() and
  716. * hugepage migration fails without data corruption.
  717. *
  718. * There is also no race when direct I/O is issued on the page under migration,
  719. * because then pte is replaced with migration swap entry and direct I/O code
  720. * will wait in the page fault for migration to complete.
  721. */
  722. static int unmap_and_move_huge_page(new_page_t get_new_page,
  723. unsigned long private, struct page *hpage,
  724. int force, bool offlining, bool sync)
  725. {
  726. int rc = 0;
  727. int *result = NULL;
  728. struct page *new_hpage = get_new_page(hpage, private, &result);
  729. struct anon_vma *anon_vma = NULL;
  730. if (!new_hpage)
  731. return -ENOMEM;
  732. rc = -EAGAIN;
  733. if (!trylock_page(hpage)) {
  734. if (!force || !sync)
  735. goto out;
  736. lock_page(hpage);
  737. }
  738. if (PageAnon(hpage))
  739. anon_vma = page_get_anon_vma(hpage);
  740. try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
  741. if (!page_mapped(hpage))
  742. rc = move_to_new_page(new_hpage, hpage, 1, sync);
  743. if (rc)
  744. remove_migration_ptes(hpage, hpage);
  745. if (anon_vma)
  746. put_anon_vma(anon_vma);
  747. unlock_page(hpage);
  748. out:
  749. if (rc != -EAGAIN) {
  750. list_del(&hpage->lru);
  751. put_page(hpage);
  752. }
  753. put_page(new_hpage);
  754. if (result) {
  755. if (rc)
  756. *result = rc;
  757. else
  758. *result = page_to_nid(new_hpage);
  759. }
  760. return rc;
  761. }
  762. /*
  763. * migrate_pages
  764. *
  765. * The function takes one list of pages to migrate and a function
  766. * that determines from the page to be migrated and the private data
  767. * the target of the move and allocates the page.
  768. *
  769. * The function returns after 10 attempts or if no pages
  770. * are movable anymore because to has become empty
  771. * or no retryable pages exist anymore.
  772. * Caller should call putback_lru_pages to return pages to the LRU
  773. * or free list only if ret != 0.
  774. *
  775. * Return: Number of pages not migrated or error code.
  776. */
  777. int migrate_pages(struct list_head *from,
  778. new_page_t get_new_page, unsigned long private, bool offlining,
  779. bool sync)
  780. {
  781. int retry = 1;
  782. int nr_failed = 0;
  783. int pass = 0;
  784. struct page *page;
  785. struct page *page2;
  786. int swapwrite = current->flags & PF_SWAPWRITE;
  787. int rc;
  788. if (!swapwrite)
  789. current->flags |= PF_SWAPWRITE;
  790. for(pass = 0; pass < 10 && retry; pass++) {
  791. retry = 0;
  792. list_for_each_entry_safe(page, page2, from, lru) {
  793. cond_resched();
  794. rc = unmap_and_move(get_new_page, private,
  795. page, pass > 2, offlining,
  796. sync);
  797. switch(rc) {
  798. case -ENOMEM:
  799. goto out;
  800. case -EAGAIN:
  801. retry++;
  802. break;
  803. case 0:
  804. break;
  805. default:
  806. /* Permanent failure */
  807. nr_failed++;
  808. break;
  809. }
  810. }
  811. }
  812. rc = 0;
  813. out:
  814. if (!swapwrite)
  815. current->flags &= ~PF_SWAPWRITE;
  816. if (rc)
  817. return rc;
  818. return nr_failed + retry;
  819. }
  820. int migrate_huge_pages(struct list_head *from,
  821. new_page_t get_new_page, unsigned long private, bool offlining,
  822. bool sync)
  823. {
  824. int retry = 1;
  825. int nr_failed = 0;
  826. int pass = 0;
  827. struct page *page;
  828. struct page *page2;
  829. int rc;
  830. for (pass = 0; pass < 10 && retry; pass++) {
  831. retry = 0;
  832. list_for_each_entry_safe(page, page2, from, lru) {
  833. cond_resched();
  834. rc = unmap_and_move_huge_page(get_new_page,
  835. private, page, pass > 2, offlining,
  836. sync);
  837. switch(rc) {
  838. case -ENOMEM:
  839. goto out;
  840. case -EAGAIN:
  841. retry++;
  842. break;
  843. case 0:
  844. break;
  845. default:
  846. /* Permanent failure */
  847. nr_failed++;
  848. break;
  849. }
  850. }
  851. }
  852. rc = 0;
  853. out:
  854. if (rc)
  855. return rc;
  856. return nr_failed + retry;
  857. }
  858. #ifdef CONFIG_NUMA
  859. /*
  860. * Move a list of individual pages
  861. */
  862. struct page_to_node {
  863. unsigned long addr;
  864. struct page *page;
  865. int node;
  866. int status;
  867. };
  868. static struct page *new_page_node(struct page *p, unsigned long private,
  869. int **result)
  870. {
  871. struct page_to_node *pm = (struct page_to_node *)private;
  872. while (pm->node != MAX_NUMNODES && pm->page != p)
  873. pm++;
  874. if (pm->node == MAX_NUMNODES)
  875. return NULL;
  876. *result = &pm->status;
  877. return alloc_pages_exact_node(pm->node,
  878. GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
  879. }
  880. /*
  881. * Move a set of pages as indicated in the pm array. The addr
  882. * field must be set to the virtual address of the page to be moved
  883. * and the node number must contain a valid target node.
  884. * The pm array ends with node = MAX_NUMNODES.
  885. */
  886. static int do_move_page_to_node_array(struct mm_struct *mm,
  887. struct page_to_node *pm,
  888. int migrate_all)
  889. {
  890. int err;
  891. struct page_to_node *pp;
  892. LIST_HEAD(pagelist);
  893. down_read(&mm->mmap_sem);
  894. /*
  895. * Build a list of pages to migrate
  896. */
  897. for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
  898. struct vm_area_struct *vma;
  899. struct page *page;
  900. err = -EFAULT;
  901. vma = find_vma(mm, pp->addr);
  902. if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
  903. goto set_status;
  904. page = follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT);
  905. err = PTR_ERR(page);
  906. if (IS_ERR(page))
  907. goto set_status;
  908. err = -ENOENT;
  909. if (!page)
  910. goto set_status;
  911. /* Use PageReserved to check for zero page */
  912. if (PageReserved(page) || PageKsm(page))
  913. goto put_and_set;
  914. pp->page = page;
  915. err = page_to_nid(page);
  916. if (err == pp->node)
  917. /*
  918. * Node already in the right place
  919. */
  920. goto put_and_set;
  921. err = -EACCES;
  922. if (page_mapcount(page) > 1 &&
  923. !migrate_all)
  924. goto put_and_set;
  925. err = isolate_lru_page(page);
  926. if (!err) {
  927. list_add_tail(&page->lru, &pagelist);
  928. inc_zone_page_state(page, NR_ISOLATED_ANON +
  929. page_is_file_cache(page));
  930. }
  931. put_and_set:
  932. /*
  933. * Either remove the duplicate refcount from
  934. * isolate_lru_page() or drop the page ref if it was
  935. * not isolated.
  936. */
  937. put_page(page);
  938. set_status:
  939. pp->status = err;
  940. }
  941. err = 0;
  942. if (!list_empty(&pagelist)) {
  943. err = migrate_pages(&pagelist, new_page_node,
  944. (unsigned long)pm, 0, true);
  945. if (err)
  946. putback_lru_pages(&pagelist);
  947. }
  948. up_read(&mm->mmap_sem);
  949. return err;
  950. }
  951. /*
  952. * Migrate an array of page address onto an array of nodes and fill
  953. * the corresponding array of status.
  954. */
  955. static int do_pages_move(struct mm_struct *mm, struct task_struct *task,
  956. unsigned long nr_pages,
  957. const void __user * __user *pages,
  958. const int __user *nodes,
  959. int __user *status, int flags)
  960. {
  961. struct page_to_node *pm;
  962. nodemask_t task_nodes;
  963. unsigned long chunk_nr_pages;
  964. unsigned long chunk_start;
  965. int err;
  966. task_nodes = cpuset_mems_allowed(task);
  967. err = -ENOMEM;
  968. pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
  969. if (!pm)
  970. goto out;
  971. migrate_prep();
  972. /*
  973. * Store a chunk of page_to_node array in a page,
  974. * but keep the last one as a marker
  975. */
  976. chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
  977. for (chunk_start = 0;
  978. chunk_start < nr_pages;
  979. chunk_start += chunk_nr_pages) {
  980. int j;
  981. if (chunk_start + chunk_nr_pages > nr_pages)
  982. chunk_nr_pages = nr_pages - chunk_start;
  983. /* fill the chunk pm with addrs and nodes from user-space */
  984. for (j = 0; j < chunk_nr_pages; j++) {
  985. const void __user *p;
  986. int node;
  987. err = -EFAULT;
  988. if (get_user(p, pages + j + chunk_start))
  989. goto out_pm;
  990. pm[j].addr = (unsigned long) p;
  991. if (get_user(node, nodes + j + chunk_start))
  992. goto out_pm;
  993. err = -ENODEV;
  994. if (node < 0 || node >= MAX_NUMNODES)
  995. goto out_pm;
  996. if (!node_state(node, N_HIGH_MEMORY))
  997. goto out_pm;
  998. err = -EACCES;
  999. if (!node_isset(node, task_nodes))
  1000. goto out_pm;
  1001. pm[j].node = node;
  1002. }
  1003. /* End marker for this chunk */
  1004. pm[chunk_nr_pages].node = MAX_NUMNODES;
  1005. /* Migrate this chunk */
  1006. err = do_move_page_to_node_array(mm, pm,
  1007. flags & MPOL_MF_MOVE_ALL);
  1008. if (err < 0)
  1009. goto out_pm;
  1010. /* Return status information */
  1011. for (j = 0; j < chunk_nr_pages; j++)
  1012. if (put_user(pm[j].status, status + j + chunk_start)) {
  1013. err = -EFAULT;
  1014. goto out_pm;
  1015. }
  1016. }
  1017. err = 0;
  1018. out_pm:
  1019. free_page((unsigned long)pm);
  1020. out:
  1021. return err;
  1022. }
  1023. /*
  1024. * Determine the nodes of an array of pages and store it in an array of status.
  1025. */
  1026. static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
  1027. const void __user **pages, int *status)
  1028. {
  1029. unsigned long i;
  1030. down_read(&mm->mmap_sem);
  1031. for (i = 0; i < nr_pages; i++) {
  1032. unsigned long addr = (unsigned long)(*pages);
  1033. struct vm_area_struct *vma;
  1034. struct page *page;
  1035. int err = -EFAULT;
  1036. vma = find_vma(mm, addr);
  1037. if (!vma || addr < vma->vm_start)
  1038. goto set_status;
  1039. page = follow_page(vma, addr, 0);
  1040. err = PTR_ERR(page);
  1041. if (IS_ERR(page))
  1042. goto set_status;
  1043. err = -ENOENT;
  1044. /* Use PageReserved to check for zero page */
  1045. if (!page || PageReserved(page) || PageKsm(page))
  1046. goto set_status;
  1047. err = page_to_nid(page);
  1048. set_status:
  1049. *status = err;
  1050. pages++;
  1051. status++;
  1052. }
  1053. up_read(&mm->mmap_sem);
  1054. }
  1055. /*
  1056. * Determine the nodes of a user array of pages and store it in
  1057. * a user array of status.
  1058. */
  1059. static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
  1060. const void __user * __user *pages,
  1061. int __user *status)
  1062. {
  1063. #define DO_PAGES_STAT_CHUNK_NR 16
  1064. const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
  1065. int chunk_status[DO_PAGES_STAT_CHUNK_NR];
  1066. while (nr_pages) {
  1067. unsigned long chunk_nr;
  1068. chunk_nr = nr_pages;
  1069. if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
  1070. chunk_nr = DO_PAGES_STAT_CHUNK_NR;
  1071. if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
  1072. break;
  1073. do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
  1074. if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
  1075. break;
  1076. pages += chunk_nr;
  1077. status += chunk_nr;
  1078. nr_pages -= chunk_nr;
  1079. }
  1080. return nr_pages ? -EFAULT : 0;
  1081. }
  1082. /*
  1083. * Move a list of pages in the address space of the currently executing
  1084. * process.
  1085. */
  1086. SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
  1087. const void __user * __user *, pages,
  1088. const int __user *, nodes,
  1089. int __user *, status, int, flags)
  1090. {
  1091. const struct cred *cred = current_cred(), *tcred;
  1092. struct task_struct *task;
  1093. struct mm_struct *mm;
  1094. int err;
  1095. /* Check flags */
  1096. if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
  1097. return -EINVAL;
  1098. if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
  1099. return -EPERM;
  1100. /* Find the mm_struct */
  1101. rcu_read_lock();
  1102. task = pid ? find_task_by_vpid(pid) : current;
  1103. if (!task) {
  1104. rcu_read_unlock();
  1105. return -ESRCH;
  1106. }
  1107. mm = get_task_mm(task);
  1108. rcu_read_unlock();
  1109. if (!mm)
  1110. return -EINVAL;
  1111. /*
  1112. * Check if this process has the right to modify the specified
  1113. * process. The right exists if the process has administrative
  1114. * capabilities, superuser privileges or the same
  1115. * userid as the target process.
  1116. */
  1117. rcu_read_lock();
  1118. tcred = __task_cred(task);
  1119. if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
  1120. cred->uid != tcred->suid && cred->uid != tcred->uid &&
  1121. !capable(CAP_SYS_NICE)) {
  1122. rcu_read_unlock();
  1123. err = -EPERM;
  1124. goto out;
  1125. }
  1126. rcu_read_unlock();
  1127. err = security_task_movememory(task);
  1128. if (err)
  1129. goto out;
  1130. if (nodes) {
  1131. err = do_pages_move(mm, task, nr_pages, pages, nodes, status,
  1132. flags);
  1133. } else {
  1134. err = do_pages_stat(mm, nr_pages, pages, status);
  1135. }
  1136. out:
  1137. mmput(mm);
  1138. return err;
  1139. }
  1140. /*
  1141. * Call migration functions in the vma_ops that may prepare
  1142. * memory in a vm for migration. migration functions may perform
  1143. * the migration for vmas that do not have an underlying page struct.
  1144. */
  1145. int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
  1146. const nodemask_t *from, unsigned long flags)
  1147. {
  1148. struct vm_area_struct *vma;
  1149. int err = 0;
  1150. for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
  1151. if (vma->vm_ops && vma->vm_ops->migrate) {
  1152. err = vma->vm_ops->migrate(vma, to, from, flags);
  1153. if (err)
  1154. break;
  1155. }
  1156. }
  1157. return err;
  1158. }
  1159. #endif