migrate.c 33 KB

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