migrate.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  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. #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. /*
  108. * Peek to check is_swap_pte() before taking ptlock? No, we
  109. * can race mremap's move_ptes(), which skips anon_vma lock.
  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 (!PageSwapCache(page) && 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. * Whereas 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, bool sync)
  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 {
  497. /*
  498. * Do not writeback pages if !sync and migratepage is
  499. * not pointing to migrate_page() which is nonblocking
  500. * (swapcache/tmpfs uses migratepage = migrate_page).
  501. */
  502. if (PageDirty(page) && !sync &&
  503. mapping->a_ops->migratepage != migrate_page)
  504. rc = -EBUSY;
  505. else if (mapping->a_ops->migratepage)
  506. /*
  507. * Most pages have a mapping and most filesystems
  508. * should provide a migration function. Anonymous
  509. * pages are part of swap space which also has its
  510. * own migration function. This is the most common
  511. * path for page migration.
  512. */
  513. rc = mapping->a_ops->migratepage(mapping,
  514. newpage, page);
  515. else
  516. rc = fallback_migrate_page(mapping, newpage, page);
  517. }
  518. if (rc) {
  519. newpage->mapping = NULL;
  520. } else {
  521. if (remap_swapcache)
  522. remove_migration_ptes(page, newpage);
  523. }
  524. unlock_page(newpage);
  525. return rc;
  526. }
  527. static int __unmap_and_move(struct page *page, struct page *newpage,
  528. int force, bool offlining, bool sync)
  529. {
  530. int rc = -EAGAIN;
  531. int remap_swapcache = 1;
  532. int charge = 0;
  533. struct mem_cgroup *mem;
  534. struct anon_vma *anon_vma = NULL;
  535. if (!trylock_page(page)) {
  536. if (!force || !sync)
  537. goto out;
  538. /*
  539. * It's not safe for direct compaction to call lock_page.
  540. * For example, during page readahead pages are added locked
  541. * to the LRU. Later, when the IO completes the pages are
  542. * marked uptodate and unlocked. However, the queueing
  543. * could be merging multiple pages for one bio (e.g.
  544. * mpage_readpages). If an allocation happens for the
  545. * second or third page, the process can end up locking
  546. * the same page twice and deadlocking. Rather than
  547. * trying to be clever about what pages can be locked,
  548. * avoid the use of lock_page for direct compaction
  549. * altogether.
  550. */
  551. if (current->flags & PF_MEMALLOC)
  552. goto out;
  553. lock_page(page);
  554. }
  555. /*
  556. * Only memory hotplug's offline_pages() caller has locked out KSM,
  557. * and can safely migrate a KSM page. The other cases have skipped
  558. * PageKsm along with PageReserved - but it is only now when we have
  559. * the page lock that we can be certain it will not go KSM beneath us
  560. * (KSM will not upgrade a page from PageAnon to PageKsm when it sees
  561. * its pagecount raised, but only here do we take the page lock which
  562. * serializes that).
  563. */
  564. if (PageKsm(page) && !offlining) {
  565. rc = -EBUSY;
  566. goto unlock;
  567. }
  568. /* charge against new page */
  569. charge = mem_cgroup_prepare_migration(page, newpage, &mem, GFP_KERNEL);
  570. if (charge == -ENOMEM) {
  571. rc = -ENOMEM;
  572. goto unlock;
  573. }
  574. BUG_ON(charge);
  575. if (PageWriteback(page)) {
  576. /*
  577. * For !sync, there is no point retrying as the retry loop
  578. * is expected to be too short for PageWriteback to be cleared
  579. */
  580. if (!sync) {
  581. rc = -EBUSY;
  582. goto uncharge;
  583. }
  584. if (!force)
  585. goto uncharge;
  586. wait_on_page_writeback(page);
  587. }
  588. /*
  589. * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
  590. * we cannot notice that anon_vma is freed while we migrates a page.
  591. * This get_anon_vma() delays freeing anon_vma pointer until the end
  592. * of migration. File cache pages are no problem because of page_lock()
  593. * File Caches may use write_page() or lock_page() in migration, then,
  594. * just care Anon page here.
  595. */
  596. if (PageAnon(page)) {
  597. /*
  598. * Only page_lock_anon_vma() understands the subtleties of
  599. * getting a hold on an anon_vma from outside one of its mms.
  600. */
  601. anon_vma = page_get_anon_vma(page);
  602. if (anon_vma) {
  603. /*
  604. * Anon page
  605. */
  606. } else if (PageSwapCache(page)) {
  607. /*
  608. * We cannot be sure that the anon_vma of an unmapped
  609. * swapcache page is safe to use because we don't
  610. * know in advance if the VMA that this page belonged
  611. * to still exists. If the VMA and others sharing the
  612. * data have been freed, then the anon_vma could
  613. * already be invalid.
  614. *
  615. * To avoid this possibility, swapcache pages get
  616. * migrated but are not remapped when migration
  617. * completes
  618. */
  619. remap_swapcache = 0;
  620. } else {
  621. goto uncharge;
  622. }
  623. }
  624. /*
  625. * Corner case handling:
  626. * 1. When a new swap-cache page is read into, it is added to the LRU
  627. * and treated as swapcache but it has no rmap yet.
  628. * Calling try_to_unmap() against a page->mapping==NULL page will
  629. * trigger a BUG. So handle it here.
  630. * 2. An orphaned page (see truncate_complete_page) might have
  631. * fs-private metadata. The page can be picked up due to memory
  632. * offlining. Everywhere else except page reclaim, the page is
  633. * invisible to the vm, so the page can not be migrated. So try to
  634. * free the metadata, so the page can be freed.
  635. */
  636. if (!page->mapping) {
  637. VM_BUG_ON(PageAnon(page));
  638. if (page_has_private(page)) {
  639. try_to_free_buffers(page);
  640. goto uncharge;
  641. }
  642. goto skip_unmap;
  643. }
  644. /* Establish migration ptes or remove ptes */
  645. try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
  646. skip_unmap:
  647. if (!page_mapped(page))
  648. rc = move_to_new_page(newpage, page, remap_swapcache, sync);
  649. if (rc && remap_swapcache)
  650. remove_migration_ptes(page, page);
  651. /* Drop an anon_vma reference if we took one */
  652. if (anon_vma)
  653. put_anon_vma(anon_vma);
  654. uncharge:
  655. if (!charge)
  656. mem_cgroup_end_migration(mem, page, newpage, rc == 0);
  657. unlock:
  658. unlock_page(page);
  659. out:
  660. return rc;
  661. }
  662. /*
  663. * Obtain the lock on page, remove all ptes and migrate the page
  664. * to the newly allocated page in newpage.
  665. */
  666. static int unmap_and_move(new_page_t get_new_page, unsigned long private,
  667. struct page *page, int force, bool offlining, bool sync)
  668. {
  669. int rc = 0;
  670. int *result = NULL;
  671. struct page *newpage = get_new_page(page, private, &result);
  672. if (!newpage)
  673. return -ENOMEM;
  674. if (page_count(page) == 1) {
  675. /* page was freed from under us. So we are done. */
  676. goto out;
  677. }
  678. if (unlikely(PageTransHuge(page)))
  679. if (unlikely(split_huge_page(page)))
  680. goto out;
  681. rc = __unmap_and_move(page, newpage, force, offlining, sync);
  682. out:
  683. if (rc != -EAGAIN) {
  684. /*
  685. * A page that has been migrated has all references
  686. * removed and will be freed. A page that has not been
  687. * migrated will have kepts its references and be
  688. * restored.
  689. */
  690. list_del(&page->lru);
  691. dec_zone_page_state(page, NR_ISOLATED_ANON +
  692. page_is_file_cache(page));
  693. putback_lru_page(page);
  694. }
  695. /*
  696. * Move the new page to the LRU. If migration was not successful
  697. * then this will free the page.
  698. */
  699. putback_lru_page(newpage);
  700. if (result) {
  701. if (rc)
  702. *result = rc;
  703. else
  704. *result = page_to_nid(newpage);
  705. }
  706. return rc;
  707. }
  708. /*
  709. * Counterpart of unmap_and_move_page() for hugepage migration.
  710. *
  711. * This function doesn't wait the completion of hugepage I/O
  712. * because there is no race between I/O and migration for hugepage.
  713. * Note that currently hugepage I/O occurs only in direct I/O
  714. * where no lock is held and PG_writeback is irrelevant,
  715. * and writeback status of all subpages are counted in the reference
  716. * count of the head page (i.e. if all subpages of a 2MB hugepage are
  717. * under direct I/O, the reference of the head page is 512 and a bit more.)
  718. * This means that when we try to migrate hugepage whose subpages are
  719. * doing direct I/O, some references remain after try_to_unmap() and
  720. * hugepage migration fails without data corruption.
  721. *
  722. * There is also no race when direct I/O is issued on the page under migration,
  723. * because then pte is replaced with migration swap entry and direct I/O code
  724. * will wait in the page fault for migration to complete.
  725. */
  726. static int unmap_and_move_huge_page(new_page_t get_new_page,
  727. unsigned long private, struct page *hpage,
  728. int force, bool offlining, bool sync)
  729. {
  730. int rc = 0;
  731. int *result = NULL;
  732. struct page *new_hpage = get_new_page(hpage, private, &result);
  733. struct anon_vma *anon_vma = NULL;
  734. if (!new_hpage)
  735. return -ENOMEM;
  736. rc = -EAGAIN;
  737. if (!trylock_page(hpage)) {
  738. if (!force || !sync)
  739. goto out;
  740. lock_page(hpage);
  741. }
  742. if (PageAnon(hpage))
  743. anon_vma = page_get_anon_vma(hpage);
  744. try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
  745. if (!page_mapped(hpage))
  746. rc = move_to_new_page(new_hpage, hpage, 1, sync);
  747. if (rc)
  748. remove_migration_ptes(hpage, hpage);
  749. if (anon_vma)
  750. put_anon_vma(anon_vma);
  751. out:
  752. unlock_page(hpage);
  753. if (rc != -EAGAIN) {
  754. list_del(&hpage->lru);
  755. put_page(hpage);
  756. }
  757. put_page(new_hpage);
  758. if (result) {
  759. if (rc)
  760. *result = rc;
  761. else
  762. *result = page_to_nid(new_hpage);
  763. }
  764. return rc;
  765. }
  766. /*
  767. * migrate_pages
  768. *
  769. * The function takes one list of pages to migrate and a function
  770. * that determines from the page to be migrated and the private data
  771. * the target of the move and allocates the page.
  772. *
  773. * The function returns after 10 attempts or if no pages
  774. * are movable anymore because to has become empty
  775. * or no retryable pages exist anymore.
  776. * Caller should call putback_lru_pages to return pages to the LRU
  777. * or free list only if ret != 0.
  778. *
  779. * Return: Number of pages not migrated or error code.
  780. */
  781. int migrate_pages(struct list_head *from,
  782. new_page_t get_new_page, unsigned long private, bool offlining,
  783. bool sync)
  784. {
  785. int retry = 1;
  786. int nr_failed = 0;
  787. int pass = 0;
  788. struct page *page;
  789. struct page *page2;
  790. int swapwrite = current->flags & PF_SWAPWRITE;
  791. int rc;
  792. if (!swapwrite)
  793. current->flags |= PF_SWAPWRITE;
  794. for(pass = 0; pass < 10 && retry; pass++) {
  795. retry = 0;
  796. list_for_each_entry_safe(page, page2, from, lru) {
  797. cond_resched();
  798. rc = unmap_and_move(get_new_page, private,
  799. page, pass > 2, offlining,
  800. sync);
  801. switch(rc) {
  802. case -ENOMEM:
  803. goto out;
  804. case -EAGAIN:
  805. retry++;
  806. break;
  807. case 0:
  808. break;
  809. default:
  810. /* Permanent failure */
  811. nr_failed++;
  812. break;
  813. }
  814. }
  815. }
  816. rc = 0;
  817. out:
  818. if (!swapwrite)
  819. current->flags &= ~PF_SWAPWRITE;
  820. if (rc)
  821. return rc;
  822. return nr_failed + retry;
  823. }
  824. int migrate_huge_pages(struct list_head *from,
  825. new_page_t get_new_page, unsigned long private, bool offlining,
  826. bool sync)
  827. {
  828. int retry = 1;
  829. int nr_failed = 0;
  830. int pass = 0;
  831. struct page *page;
  832. struct page *page2;
  833. int rc;
  834. for (pass = 0; pass < 10 && retry; pass++) {
  835. retry = 0;
  836. list_for_each_entry_safe(page, page2, from, lru) {
  837. cond_resched();
  838. rc = unmap_and_move_huge_page(get_new_page,
  839. private, page, pass > 2, offlining,
  840. sync);
  841. switch(rc) {
  842. case -ENOMEM:
  843. goto out;
  844. case -EAGAIN:
  845. retry++;
  846. break;
  847. case 0:
  848. break;
  849. default:
  850. /* Permanent failure */
  851. nr_failed++;
  852. break;
  853. }
  854. }
  855. }
  856. rc = 0;
  857. out:
  858. if (rc)
  859. return rc;
  860. return nr_failed + retry;
  861. }
  862. #ifdef CONFIG_NUMA
  863. /*
  864. * Move a list of individual pages
  865. */
  866. struct page_to_node {
  867. unsigned long addr;
  868. struct page *page;
  869. int node;
  870. int status;
  871. };
  872. static struct page *new_page_node(struct page *p, unsigned long private,
  873. int **result)
  874. {
  875. struct page_to_node *pm = (struct page_to_node *)private;
  876. while (pm->node != MAX_NUMNODES && pm->page != p)
  877. pm++;
  878. if (pm->node == MAX_NUMNODES)
  879. return NULL;
  880. *result = &pm->status;
  881. return alloc_pages_exact_node(pm->node,
  882. GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
  883. }
  884. /*
  885. * Move a set of pages as indicated in the pm array. The addr
  886. * field must be set to the virtual address of the page to be moved
  887. * and the node number must contain a valid target node.
  888. * The pm array ends with node = MAX_NUMNODES.
  889. */
  890. static int do_move_page_to_node_array(struct mm_struct *mm,
  891. struct page_to_node *pm,
  892. int migrate_all)
  893. {
  894. int err;
  895. struct page_to_node *pp;
  896. LIST_HEAD(pagelist);
  897. down_read(&mm->mmap_sem);
  898. /*
  899. * Build a list of pages to migrate
  900. */
  901. for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
  902. struct vm_area_struct *vma;
  903. struct page *page;
  904. err = -EFAULT;
  905. vma = find_vma(mm, pp->addr);
  906. if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
  907. goto set_status;
  908. page = follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT);
  909. err = PTR_ERR(page);
  910. if (IS_ERR(page))
  911. goto set_status;
  912. err = -ENOENT;
  913. if (!page)
  914. goto set_status;
  915. /* Use PageReserved to check for zero page */
  916. if (PageReserved(page) || PageKsm(page))
  917. goto put_and_set;
  918. pp->page = page;
  919. err = page_to_nid(page);
  920. if (err == pp->node)
  921. /*
  922. * Node already in the right place
  923. */
  924. goto put_and_set;
  925. err = -EACCES;
  926. if (page_mapcount(page) > 1 &&
  927. !migrate_all)
  928. goto put_and_set;
  929. err = isolate_lru_page(page);
  930. if (!err) {
  931. list_add_tail(&page->lru, &pagelist);
  932. inc_zone_page_state(page, NR_ISOLATED_ANON +
  933. page_is_file_cache(page));
  934. }
  935. put_and_set:
  936. /*
  937. * Either remove the duplicate refcount from
  938. * isolate_lru_page() or drop the page ref if it was
  939. * not isolated.
  940. */
  941. put_page(page);
  942. set_status:
  943. pp->status = err;
  944. }
  945. err = 0;
  946. if (!list_empty(&pagelist)) {
  947. err = migrate_pages(&pagelist, new_page_node,
  948. (unsigned long)pm, 0, true);
  949. if (err)
  950. putback_lru_pages(&pagelist);
  951. }
  952. up_read(&mm->mmap_sem);
  953. return err;
  954. }
  955. /*
  956. * Migrate an array of page address onto an array of nodes and fill
  957. * the corresponding array of status.
  958. */
  959. static int do_pages_move(struct mm_struct *mm, struct task_struct *task,
  960. unsigned long nr_pages,
  961. const void __user * __user *pages,
  962. const int __user *nodes,
  963. int __user *status, int flags)
  964. {
  965. struct page_to_node *pm;
  966. nodemask_t task_nodes;
  967. unsigned long chunk_nr_pages;
  968. unsigned long chunk_start;
  969. int err;
  970. task_nodes = cpuset_mems_allowed(task);
  971. err = -ENOMEM;
  972. pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
  973. if (!pm)
  974. goto out;
  975. migrate_prep();
  976. /*
  977. * Store a chunk of page_to_node array in a page,
  978. * but keep the last one as a marker
  979. */
  980. chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
  981. for (chunk_start = 0;
  982. chunk_start < nr_pages;
  983. chunk_start += chunk_nr_pages) {
  984. int j;
  985. if (chunk_start + chunk_nr_pages > nr_pages)
  986. chunk_nr_pages = nr_pages - chunk_start;
  987. /* fill the chunk pm with addrs and nodes from user-space */
  988. for (j = 0; j < chunk_nr_pages; j++) {
  989. const void __user *p;
  990. int node;
  991. err = -EFAULT;
  992. if (get_user(p, pages + j + chunk_start))
  993. goto out_pm;
  994. pm[j].addr = (unsigned long) p;
  995. if (get_user(node, nodes + j + chunk_start))
  996. goto out_pm;
  997. err = -ENODEV;
  998. if (node < 0 || node >= MAX_NUMNODES)
  999. goto out_pm;
  1000. if (!node_state(node, N_HIGH_MEMORY))
  1001. goto out_pm;
  1002. err = -EACCES;
  1003. if (!node_isset(node, task_nodes))
  1004. goto out_pm;
  1005. pm[j].node = node;
  1006. }
  1007. /* End marker for this chunk */
  1008. pm[chunk_nr_pages].node = MAX_NUMNODES;
  1009. /* Migrate this chunk */
  1010. err = do_move_page_to_node_array(mm, pm,
  1011. flags & MPOL_MF_MOVE_ALL);
  1012. if (err < 0)
  1013. goto out_pm;
  1014. /* Return status information */
  1015. for (j = 0; j < chunk_nr_pages; j++)
  1016. if (put_user(pm[j].status, status + j + chunk_start)) {
  1017. err = -EFAULT;
  1018. goto out_pm;
  1019. }
  1020. }
  1021. err = 0;
  1022. out_pm:
  1023. free_page((unsigned long)pm);
  1024. out:
  1025. return err;
  1026. }
  1027. /*
  1028. * Determine the nodes of an array of pages and store it in an array of status.
  1029. */
  1030. static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
  1031. const void __user **pages, int *status)
  1032. {
  1033. unsigned long i;
  1034. down_read(&mm->mmap_sem);
  1035. for (i = 0; i < nr_pages; i++) {
  1036. unsigned long addr = (unsigned long)(*pages);
  1037. struct vm_area_struct *vma;
  1038. struct page *page;
  1039. int err = -EFAULT;
  1040. vma = find_vma(mm, addr);
  1041. if (!vma || addr < vma->vm_start)
  1042. goto set_status;
  1043. page = follow_page(vma, addr, 0);
  1044. err = PTR_ERR(page);
  1045. if (IS_ERR(page))
  1046. goto set_status;
  1047. err = -ENOENT;
  1048. /* Use PageReserved to check for zero page */
  1049. if (!page || PageReserved(page) || PageKsm(page))
  1050. goto set_status;
  1051. err = page_to_nid(page);
  1052. set_status:
  1053. *status = err;
  1054. pages++;
  1055. status++;
  1056. }
  1057. up_read(&mm->mmap_sem);
  1058. }
  1059. /*
  1060. * Determine the nodes of a user array of pages and store it in
  1061. * a user array of status.
  1062. */
  1063. static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
  1064. const void __user * __user *pages,
  1065. int __user *status)
  1066. {
  1067. #define DO_PAGES_STAT_CHUNK_NR 16
  1068. const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
  1069. int chunk_status[DO_PAGES_STAT_CHUNK_NR];
  1070. while (nr_pages) {
  1071. unsigned long chunk_nr;
  1072. chunk_nr = nr_pages;
  1073. if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
  1074. chunk_nr = DO_PAGES_STAT_CHUNK_NR;
  1075. if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
  1076. break;
  1077. do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
  1078. if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
  1079. break;
  1080. pages += chunk_nr;
  1081. status += chunk_nr;
  1082. nr_pages -= chunk_nr;
  1083. }
  1084. return nr_pages ? -EFAULT : 0;
  1085. }
  1086. /*
  1087. * Move a list of pages in the address space of the currently executing
  1088. * process.
  1089. */
  1090. SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
  1091. const void __user * __user *, pages,
  1092. const int __user *, nodes,
  1093. int __user *, status, int, flags)
  1094. {
  1095. const struct cred *cred = current_cred(), *tcred;
  1096. struct task_struct *task;
  1097. struct mm_struct *mm;
  1098. int err;
  1099. /* Check flags */
  1100. if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
  1101. return -EINVAL;
  1102. if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
  1103. return -EPERM;
  1104. /* Find the mm_struct */
  1105. rcu_read_lock();
  1106. task = pid ? find_task_by_vpid(pid) : current;
  1107. if (!task) {
  1108. rcu_read_unlock();
  1109. return -ESRCH;
  1110. }
  1111. mm = get_task_mm(task);
  1112. rcu_read_unlock();
  1113. if (!mm)
  1114. return -EINVAL;
  1115. /*
  1116. * Check if this process has the right to modify the specified
  1117. * process. The right exists if the process has administrative
  1118. * capabilities, superuser privileges or the same
  1119. * userid as the target process.
  1120. */
  1121. rcu_read_lock();
  1122. tcred = __task_cred(task);
  1123. if (cred->euid != tcred->suid && cred->euid != tcred->uid &&
  1124. cred->uid != tcred->suid && cred->uid != tcred->uid &&
  1125. !capable(CAP_SYS_NICE)) {
  1126. rcu_read_unlock();
  1127. err = -EPERM;
  1128. goto out;
  1129. }
  1130. rcu_read_unlock();
  1131. err = security_task_movememory(task);
  1132. if (err)
  1133. goto out;
  1134. if (nodes) {
  1135. err = do_pages_move(mm, task, nr_pages, pages, nodes, status,
  1136. flags);
  1137. } else {
  1138. err = do_pages_stat(mm, nr_pages, pages, status);
  1139. }
  1140. out:
  1141. mmput(mm);
  1142. return err;
  1143. }
  1144. /*
  1145. * Call migration functions in the vma_ops that may prepare
  1146. * memory in a vm for migration. migration functions may perform
  1147. * the migration for vmas that do not have an underlying page struct.
  1148. */
  1149. int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
  1150. const nodemask_t *from, unsigned long flags)
  1151. {
  1152. struct vm_area_struct *vma;
  1153. int err = 0;
  1154. for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
  1155. if (vma->vm_ops && vma->vm_ops->migrate) {
  1156. err = vma->vm_ops->migrate(vma, to, from, flags);
  1157. if (err)
  1158. break;
  1159. }
  1160. }
  1161. return err;
  1162. }
  1163. #endif