migrate.c 31 KB

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