migrate.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771
  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 <linux/balloon_compaction.h>
  38. #include <asm/tlbflush.h>
  39. #define CREATE_TRACE_POINTS
  40. #include <trace/events/migrate.h>
  41. #include "internal.h"
  42. /*
  43. * migrate_prep() needs to be called before we start compiling a list of pages
  44. * to be migrated using isolate_lru_page(). If scheduling work on other CPUs is
  45. * undesirable, use migrate_prep_local()
  46. */
  47. int migrate_prep(void)
  48. {
  49. /*
  50. * Clear the LRU lists so pages can be isolated.
  51. * Note that pages may be moved off the LRU after we have
  52. * drained them. Those pages will fail to migrate like other
  53. * pages that may be busy.
  54. */
  55. lru_add_drain_all();
  56. return 0;
  57. }
  58. /* Do the necessary work of migrate_prep but not if it involves other CPUs */
  59. int migrate_prep_local(void)
  60. {
  61. lru_add_drain();
  62. return 0;
  63. }
  64. /*
  65. * Add isolated pages on the list back to the LRU under page lock
  66. * to avoid leaking evictable pages back onto unevictable list.
  67. */
  68. void putback_lru_pages(struct list_head *l)
  69. {
  70. struct page *page;
  71. struct page *page2;
  72. list_for_each_entry_safe(page, page2, l, lru) {
  73. list_del(&page->lru);
  74. dec_zone_page_state(page, NR_ISOLATED_ANON +
  75. page_is_file_cache(page));
  76. putback_lru_page(page);
  77. }
  78. }
  79. /*
  80. * Put previously isolated pages back onto the appropriate lists
  81. * from where they were once taken off for compaction/migration.
  82. *
  83. * This function shall be used instead of putback_lru_pages(),
  84. * whenever the isolated pageset has been built by isolate_migratepages_range()
  85. */
  86. void putback_movable_pages(struct list_head *l)
  87. {
  88. struct page *page;
  89. struct page *page2;
  90. list_for_each_entry_safe(page, page2, l, lru) {
  91. list_del(&page->lru);
  92. dec_zone_page_state(page, NR_ISOLATED_ANON +
  93. page_is_file_cache(page));
  94. if (unlikely(balloon_page_movable(page)))
  95. balloon_page_putback(page);
  96. else
  97. putback_lru_page(page);
  98. }
  99. }
  100. /*
  101. * Restore a potential migration pte to a working pte entry
  102. */
  103. static int remove_migration_pte(struct page *new, struct vm_area_struct *vma,
  104. unsigned long addr, void *old)
  105. {
  106. struct mm_struct *mm = vma->vm_mm;
  107. swp_entry_t entry;
  108. pmd_t *pmd;
  109. pte_t *ptep, pte;
  110. spinlock_t *ptl;
  111. if (unlikely(PageHuge(new))) {
  112. ptep = huge_pte_offset(mm, addr);
  113. if (!ptep)
  114. goto out;
  115. ptl = &mm->page_table_lock;
  116. } else {
  117. pmd = mm_find_pmd(mm, addr);
  118. if (!pmd)
  119. goto out;
  120. if (pmd_trans_huge(*pmd))
  121. goto out;
  122. ptep = pte_offset_map(pmd, addr);
  123. /*
  124. * Peek to check is_swap_pte() before taking ptlock? No, we
  125. * can race mremap's move_ptes(), which skips anon_vma lock.
  126. */
  127. ptl = pte_lockptr(mm, pmd);
  128. }
  129. spin_lock(ptl);
  130. pte = *ptep;
  131. if (!is_swap_pte(pte))
  132. goto unlock;
  133. entry = pte_to_swp_entry(pte);
  134. if (!is_migration_entry(entry) ||
  135. migration_entry_to_page(entry) != old)
  136. goto unlock;
  137. get_page(new);
  138. pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
  139. if (is_write_migration_entry(entry))
  140. pte = pte_mkwrite(pte);
  141. #ifdef CONFIG_HUGETLB_PAGE
  142. if (PageHuge(new)) {
  143. pte = pte_mkhuge(pte);
  144. pte = arch_make_huge_pte(pte, vma, new, 0);
  145. }
  146. #endif
  147. flush_dcache_page(new);
  148. set_pte_at(mm, addr, ptep, pte);
  149. if (PageHuge(new)) {
  150. if (PageAnon(new))
  151. hugepage_add_anon_rmap(new, vma, addr);
  152. else
  153. page_dup_rmap(new);
  154. } else if (PageAnon(new))
  155. page_add_anon_rmap(new, vma, addr);
  156. else
  157. page_add_file_rmap(new);
  158. /* No need to invalidate - it was non-present before */
  159. update_mmu_cache(vma, addr, ptep);
  160. unlock:
  161. pte_unmap_unlock(ptep, ptl);
  162. out:
  163. return SWAP_AGAIN;
  164. }
  165. /*
  166. * Get rid of all migration entries and replace them by
  167. * references to the indicated page.
  168. */
  169. static void remove_migration_ptes(struct page *old, struct page *new)
  170. {
  171. rmap_walk(new, remove_migration_pte, old);
  172. }
  173. /*
  174. * Something used the pte of a page under migration. We need to
  175. * get to the page and wait until migration is finished.
  176. * When we return from this function the fault will be retried.
  177. */
  178. static void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep,
  179. spinlock_t *ptl)
  180. {
  181. pte_t pte;
  182. swp_entry_t entry;
  183. struct page *page;
  184. spin_lock(ptl);
  185. pte = *ptep;
  186. if (!is_swap_pte(pte))
  187. goto out;
  188. entry = pte_to_swp_entry(pte);
  189. if (!is_migration_entry(entry))
  190. goto out;
  191. page = migration_entry_to_page(entry);
  192. /*
  193. * Once radix-tree replacement of page migration started, page_count
  194. * *must* be zero. And, we don't want to call wait_on_page_locked()
  195. * against a page without get_page().
  196. * So, we use get_page_unless_zero(), here. Even failed, page fault
  197. * will occur again.
  198. */
  199. if (!get_page_unless_zero(page))
  200. goto out;
  201. pte_unmap_unlock(ptep, ptl);
  202. wait_on_page_locked(page);
  203. put_page(page);
  204. return;
  205. out:
  206. pte_unmap_unlock(ptep, ptl);
  207. }
  208. void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
  209. unsigned long address)
  210. {
  211. spinlock_t *ptl = pte_lockptr(mm, pmd);
  212. pte_t *ptep = pte_offset_map(pmd, address);
  213. __migration_entry_wait(mm, ptep, ptl);
  214. }
  215. void migration_entry_wait_huge(struct mm_struct *mm, pte_t *pte)
  216. {
  217. spinlock_t *ptl = &(mm)->page_table_lock;
  218. __migration_entry_wait(mm, pte, ptl);
  219. }
  220. #ifdef CONFIG_BLOCK
  221. /* Returns true if all buffers are successfully locked */
  222. static bool buffer_migrate_lock_buffers(struct buffer_head *head,
  223. enum migrate_mode mode)
  224. {
  225. struct buffer_head *bh = head;
  226. /* Simple case, sync compaction */
  227. if (mode != MIGRATE_ASYNC) {
  228. do {
  229. get_bh(bh);
  230. lock_buffer(bh);
  231. bh = bh->b_this_page;
  232. } while (bh != head);
  233. return true;
  234. }
  235. /* async case, we cannot block on lock_buffer so use trylock_buffer */
  236. do {
  237. get_bh(bh);
  238. if (!trylock_buffer(bh)) {
  239. /*
  240. * We failed to lock the buffer and cannot stall in
  241. * async migration. Release the taken locks
  242. */
  243. struct buffer_head *failed_bh = bh;
  244. put_bh(failed_bh);
  245. bh = head;
  246. while (bh != failed_bh) {
  247. unlock_buffer(bh);
  248. put_bh(bh);
  249. bh = bh->b_this_page;
  250. }
  251. return false;
  252. }
  253. bh = bh->b_this_page;
  254. } while (bh != head);
  255. return true;
  256. }
  257. #else
  258. static inline bool buffer_migrate_lock_buffers(struct buffer_head *head,
  259. enum migrate_mode mode)
  260. {
  261. return true;
  262. }
  263. #endif /* CONFIG_BLOCK */
  264. /*
  265. * Replace the page in the mapping.
  266. *
  267. * The number of remaining references must be:
  268. * 1 for anonymous pages without a mapping
  269. * 2 for pages with a mapping
  270. * 3 for pages with a mapping and PagePrivate/PagePrivate2 set.
  271. */
  272. static int migrate_page_move_mapping(struct address_space *mapping,
  273. struct page *newpage, struct page *page,
  274. struct buffer_head *head, enum migrate_mode mode)
  275. {
  276. int expected_count = 0;
  277. void **pslot;
  278. if (!mapping) {
  279. /* Anonymous page without mapping */
  280. if (page_count(page) != 1)
  281. return -EAGAIN;
  282. return MIGRATEPAGE_SUCCESS;
  283. }
  284. spin_lock_irq(&mapping->tree_lock);
  285. pslot = radix_tree_lookup_slot(&mapping->page_tree,
  286. page_index(page));
  287. expected_count = 2 + page_has_private(page);
  288. if (page_count(page) != expected_count ||
  289. radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
  290. spin_unlock_irq(&mapping->tree_lock);
  291. return -EAGAIN;
  292. }
  293. if (!page_freeze_refs(page, expected_count)) {
  294. spin_unlock_irq(&mapping->tree_lock);
  295. return -EAGAIN;
  296. }
  297. /*
  298. * In the async migration case of moving a page with buffers, lock the
  299. * buffers using trylock before the mapping is moved. If the mapping
  300. * was moved, we later failed to lock the buffers and could not move
  301. * the mapping back due to an elevated page count, we would have to
  302. * block waiting on other references to be dropped.
  303. */
  304. if (mode == MIGRATE_ASYNC && head &&
  305. !buffer_migrate_lock_buffers(head, mode)) {
  306. page_unfreeze_refs(page, expected_count);
  307. spin_unlock_irq(&mapping->tree_lock);
  308. return -EAGAIN;
  309. }
  310. /*
  311. * Now we know that no one else is looking at the page.
  312. */
  313. get_page(newpage); /* add cache reference */
  314. if (PageSwapCache(page)) {
  315. SetPageSwapCache(newpage);
  316. set_page_private(newpage, page_private(page));
  317. }
  318. radix_tree_replace_slot(pslot, newpage);
  319. /*
  320. * Drop cache reference from old page by unfreezing
  321. * to one less reference.
  322. * We know this isn't the last reference.
  323. */
  324. page_unfreeze_refs(page, expected_count - 1);
  325. /*
  326. * If moved to a different zone then also account
  327. * the page for that zone. Other VM counters will be
  328. * taken care of when we establish references to the
  329. * new page and drop references to the old page.
  330. *
  331. * Note that anonymous pages are accounted for
  332. * via NR_FILE_PAGES and NR_ANON_PAGES if they
  333. * are mapped to swap space.
  334. */
  335. __dec_zone_page_state(page, NR_FILE_PAGES);
  336. __inc_zone_page_state(newpage, NR_FILE_PAGES);
  337. if (!PageSwapCache(page) && PageSwapBacked(page)) {
  338. __dec_zone_page_state(page, NR_SHMEM);
  339. __inc_zone_page_state(newpage, NR_SHMEM);
  340. }
  341. spin_unlock_irq(&mapping->tree_lock);
  342. return MIGRATEPAGE_SUCCESS;
  343. }
  344. /*
  345. * The expected number of remaining references is the same as that
  346. * of migrate_page_move_mapping().
  347. */
  348. int migrate_huge_page_move_mapping(struct address_space *mapping,
  349. struct page *newpage, struct page *page)
  350. {
  351. int expected_count;
  352. void **pslot;
  353. if (!mapping) {
  354. if (page_count(page) != 1)
  355. return -EAGAIN;
  356. return MIGRATEPAGE_SUCCESS;
  357. }
  358. spin_lock_irq(&mapping->tree_lock);
  359. pslot = radix_tree_lookup_slot(&mapping->page_tree,
  360. page_index(page));
  361. expected_count = 2 + page_has_private(page);
  362. if (page_count(page) != expected_count ||
  363. radix_tree_deref_slot_protected(pslot, &mapping->tree_lock) != page) {
  364. spin_unlock_irq(&mapping->tree_lock);
  365. return -EAGAIN;
  366. }
  367. if (!page_freeze_refs(page, expected_count)) {
  368. spin_unlock_irq(&mapping->tree_lock);
  369. return -EAGAIN;
  370. }
  371. get_page(newpage);
  372. radix_tree_replace_slot(pslot, newpage);
  373. page_unfreeze_refs(page, expected_count - 1);
  374. spin_unlock_irq(&mapping->tree_lock);
  375. return MIGRATEPAGE_SUCCESS;
  376. }
  377. /*
  378. * Copy the page to its new location
  379. */
  380. void migrate_page_copy(struct page *newpage, struct page *page)
  381. {
  382. if (PageHuge(page) || PageTransHuge(page))
  383. copy_huge_page(newpage, page);
  384. else
  385. copy_highpage(newpage, page);
  386. if (PageError(page))
  387. SetPageError(newpage);
  388. if (PageReferenced(page))
  389. SetPageReferenced(newpage);
  390. if (PageUptodate(page))
  391. SetPageUptodate(newpage);
  392. if (TestClearPageActive(page)) {
  393. VM_BUG_ON(PageUnevictable(page));
  394. SetPageActive(newpage);
  395. } else if (TestClearPageUnevictable(page))
  396. SetPageUnevictable(newpage);
  397. if (PageChecked(page))
  398. SetPageChecked(newpage);
  399. if (PageMappedToDisk(page))
  400. SetPageMappedToDisk(newpage);
  401. if (PageDirty(page)) {
  402. clear_page_dirty_for_io(page);
  403. /*
  404. * Want to mark the page and the radix tree as dirty, and
  405. * redo the accounting that clear_page_dirty_for_io undid,
  406. * but we can't use set_page_dirty because that function
  407. * is actually a signal that all of the page has become dirty.
  408. * Whereas only part of our page may be dirty.
  409. */
  410. if (PageSwapBacked(page))
  411. SetPageDirty(newpage);
  412. else
  413. __set_page_dirty_nobuffers(newpage);
  414. }
  415. mlock_migrate_page(newpage, page);
  416. ksm_migrate_page(newpage, page);
  417. /*
  418. * Please do not reorder this without considering how mm/ksm.c's
  419. * get_ksm_page() depends upon ksm_migrate_page() and PageSwapCache().
  420. */
  421. ClearPageSwapCache(page);
  422. ClearPagePrivate(page);
  423. set_page_private(page, 0);
  424. /*
  425. * If any waiters have accumulated on the new page then
  426. * wake them up.
  427. */
  428. if (PageWriteback(newpage))
  429. end_page_writeback(newpage);
  430. }
  431. /************************************************************
  432. * Migration functions
  433. ***********************************************************/
  434. /* Always fail migration. Used for mappings that are not movable */
  435. int fail_migrate_page(struct address_space *mapping,
  436. struct page *newpage, struct page *page)
  437. {
  438. return -EIO;
  439. }
  440. EXPORT_SYMBOL(fail_migrate_page);
  441. /*
  442. * Common logic to directly migrate a single page suitable for
  443. * pages that do not use PagePrivate/PagePrivate2.
  444. *
  445. * Pages are locked upon entry and exit.
  446. */
  447. int migrate_page(struct address_space *mapping,
  448. struct page *newpage, struct page *page,
  449. enum migrate_mode mode)
  450. {
  451. int rc;
  452. BUG_ON(PageWriteback(page)); /* Writeback must be complete */
  453. rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode);
  454. if (rc != MIGRATEPAGE_SUCCESS)
  455. return rc;
  456. migrate_page_copy(newpage, page);
  457. return MIGRATEPAGE_SUCCESS;
  458. }
  459. EXPORT_SYMBOL(migrate_page);
  460. #ifdef CONFIG_BLOCK
  461. /*
  462. * Migration function for pages with buffers. This function can only be used
  463. * if the underlying filesystem guarantees that no other references to "page"
  464. * exist.
  465. */
  466. int buffer_migrate_page(struct address_space *mapping,
  467. struct page *newpage, struct page *page, enum migrate_mode mode)
  468. {
  469. struct buffer_head *bh, *head;
  470. int rc;
  471. if (!page_has_buffers(page))
  472. return migrate_page(mapping, newpage, page, mode);
  473. head = page_buffers(page);
  474. rc = migrate_page_move_mapping(mapping, newpage, page, head, mode);
  475. if (rc != MIGRATEPAGE_SUCCESS)
  476. return rc;
  477. /*
  478. * In the async case, migrate_page_move_mapping locked the buffers
  479. * with an IRQ-safe spinlock held. In the sync case, the buffers
  480. * need to be locked now
  481. */
  482. if (mode != MIGRATE_ASYNC)
  483. BUG_ON(!buffer_migrate_lock_buffers(head, mode));
  484. ClearPagePrivate(page);
  485. set_page_private(newpage, page_private(page));
  486. set_page_private(page, 0);
  487. put_page(page);
  488. get_page(newpage);
  489. bh = head;
  490. do {
  491. set_bh_page(bh, newpage, bh_offset(bh));
  492. bh = bh->b_this_page;
  493. } while (bh != head);
  494. SetPagePrivate(newpage);
  495. migrate_page_copy(newpage, page);
  496. bh = head;
  497. do {
  498. unlock_buffer(bh);
  499. put_bh(bh);
  500. bh = bh->b_this_page;
  501. } while (bh != head);
  502. return MIGRATEPAGE_SUCCESS;
  503. }
  504. EXPORT_SYMBOL(buffer_migrate_page);
  505. #endif
  506. /*
  507. * Writeback a page to clean the dirty state
  508. */
  509. static int writeout(struct address_space *mapping, struct page *page)
  510. {
  511. struct writeback_control wbc = {
  512. .sync_mode = WB_SYNC_NONE,
  513. .nr_to_write = 1,
  514. .range_start = 0,
  515. .range_end = LLONG_MAX,
  516. .for_reclaim = 1
  517. };
  518. int rc;
  519. if (!mapping->a_ops->writepage)
  520. /* No write method for the address space */
  521. return -EINVAL;
  522. if (!clear_page_dirty_for_io(page))
  523. /* Someone else already triggered a write */
  524. return -EAGAIN;
  525. /*
  526. * A dirty page may imply that the underlying filesystem has
  527. * the page on some queue. So the page must be clean for
  528. * migration. Writeout may mean we loose the lock and the
  529. * page state is no longer what we checked for earlier.
  530. * At this point we know that the migration attempt cannot
  531. * be successful.
  532. */
  533. remove_migration_ptes(page, page);
  534. rc = mapping->a_ops->writepage(page, &wbc);
  535. if (rc != AOP_WRITEPAGE_ACTIVATE)
  536. /* unlocked. Relock */
  537. lock_page(page);
  538. return (rc < 0) ? -EIO : -EAGAIN;
  539. }
  540. /*
  541. * Default handling if a filesystem does not provide a migration function.
  542. */
  543. static int fallback_migrate_page(struct address_space *mapping,
  544. struct page *newpage, struct page *page, enum migrate_mode mode)
  545. {
  546. if (PageDirty(page)) {
  547. /* Only writeback pages in full synchronous migration */
  548. if (mode != MIGRATE_SYNC)
  549. return -EBUSY;
  550. return writeout(mapping, page);
  551. }
  552. /*
  553. * Buffers may be managed in a filesystem specific way.
  554. * We must have no buffers or drop them.
  555. */
  556. if (page_has_private(page) &&
  557. !try_to_release_page(page, GFP_KERNEL))
  558. return -EAGAIN;
  559. return migrate_page(mapping, newpage, page, mode);
  560. }
  561. /*
  562. * Move a page to a newly allocated page
  563. * The page is locked and all ptes have been successfully removed.
  564. *
  565. * The new page will have replaced the old page if this function
  566. * is successful.
  567. *
  568. * Return value:
  569. * < 0 - error code
  570. * MIGRATEPAGE_SUCCESS - success
  571. */
  572. static int move_to_new_page(struct page *newpage, struct page *page,
  573. int remap_swapcache, enum migrate_mode mode)
  574. {
  575. struct address_space *mapping;
  576. int rc;
  577. /*
  578. * Block others from accessing the page when we get around to
  579. * establishing additional references. We are the only one
  580. * holding a reference to the new page at this point.
  581. */
  582. if (!trylock_page(newpage))
  583. BUG();
  584. /* Prepare mapping for the new page.*/
  585. newpage->index = page->index;
  586. newpage->mapping = page->mapping;
  587. if (PageSwapBacked(page))
  588. SetPageSwapBacked(newpage);
  589. mapping = page_mapping(page);
  590. if (!mapping)
  591. rc = migrate_page(mapping, newpage, page, mode);
  592. else if (mapping->a_ops->migratepage)
  593. /*
  594. * Most pages have a mapping and most filesystems provide a
  595. * migratepage callback. Anonymous pages are part of swap
  596. * space which also has its own migratepage callback. This
  597. * is the most common path for page migration.
  598. */
  599. rc = mapping->a_ops->migratepage(mapping,
  600. newpage, page, mode);
  601. else
  602. rc = fallback_migrate_page(mapping, newpage, page, mode);
  603. if (rc != MIGRATEPAGE_SUCCESS) {
  604. newpage->mapping = NULL;
  605. } else {
  606. if (remap_swapcache)
  607. remove_migration_ptes(page, newpage);
  608. page->mapping = NULL;
  609. }
  610. unlock_page(newpage);
  611. return rc;
  612. }
  613. static int __unmap_and_move(struct page *page, struct page *newpage,
  614. int force, enum migrate_mode mode)
  615. {
  616. int rc = -EAGAIN;
  617. int remap_swapcache = 1;
  618. struct mem_cgroup *mem;
  619. struct anon_vma *anon_vma = NULL;
  620. if (!trylock_page(page)) {
  621. if (!force || mode == MIGRATE_ASYNC)
  622. goto out;
  623. /*
  624. * It's not safe for direct compaction to call lock_page.
  625. * For example, during page readahead pages are added locked
  626. * to the LRU. Later, when the IO completes the pages are
  627. * marked uptodate and unlocked. However, the queueing
  628. * could be merging multiple pages for one bio (e.g.
  629. * mpage_readpages). If an allocation happens for the
  630. * second or third page, the process can end up locking
  631. * the same page twice and deadlocking. Rather than
  632. * trying to be clever about what pages can be locked,
  633. * avoid the use of lock_page for direct compaction
  634. * altogether.
  635. */
  636. if (current->flags & PF_MEMALLOC)
  637. goto out;
  638. lock_page(page);
  639. }
  640. /* charge against new page */
  641. mem_cgroup_prepare_migration(page, newpage, &mem);
  642. if (PageWriteback(page)) {
  643. /*
  644. * Only in the case of a full synchronous migration is it
  645. * necessary to wait for PageWriteback. In the async case,
  646. * the retry loop is too short and in the sync-light case,
  647. * the overhead of stalling is too much
  648. */
  649. if (mode != MIGRATE_SYNC) {
  650. rc = -EBUSY;
  651. goto uncharge;
  652. }
  653. if (!force)
  654. goto uncharge;
  655. wait_on_page_writeback(page);
  656. }
  657. /*
  658. * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
  659. * we cannot notice that anon_vma is freed while we migrates a page.
  660. * This get_anon_vma() delays freeing anon_vma pointer until the end
  661. * of migration. File cache pages are no problem because of page_lock()
  662. * File Caches may use write_page() or lock_page() in migration, then,
  663. * just care Anon page here.
  664. */
  665. if (PageAnon(page) && !PageKsm(page)) {
  666. /*
  667. * Only page_lock_anon_vma_read() understands the subtleties of
  668. * getting a hold on an anon_vma from outside one of its mms.
  669. */
  670. anon_vma = page_get_anon_vma(page);
  671. if (anon_vma) {
  672. /*
  673. * Anon page
  674. */
  675. } else if (PageSwapCache(page)) {
  676. /*
  677. * We cannot be sure that the anon_vma of an unmapped
  678. * swapcache page is safe to use because we don't
  679. * know in advance if the VMA that this page belonged
  680. * to still exists. If the VMA and others sharing the
  681. * data have been freed, then the anon_vma could
  682. * already be invalid.
  683. *
  684. * To avoid this possibility, swapcache pages get
  685. * migrated but are not remapped when migration
  686. * completes
  687. */
  688. remap_swapcache = 0;
  689. } else {
  690. goto uncharge;
  691. }
  692. }
  693. if (unlikely(balloon_page_movable(page))) {
  694. /*
  695. * A ballooned page does not need any special attention from
  696. * physical to virtual reverse mapping procedures.
  697. * Skip any attempt to unmap PTEs or to remap swap cache,
  698. * in order to avoid burning cycles at rmap level, and perform
  699. * the page migration right away (proteced by page lock).
  700. */
  701. rc = balloon_page_migrate(newpage, page, mode);
  702. goto uncharge;
  703. }
  704. /*
  705. * Corner case handling:
  706. * 1. When a new swap-cache page is read into, it is added to the LRU
  707. * and treated as swapcache but it has no rmap yet.
  708. * Calling try_to_unmap() against a page->mapping==NULL page will
  709. * trigger a BUG. So handle it here.
  710. * 2. An orphaned page (see truncate_complete_page) might have
  711. * fs-private metadata. The page can be picked up due to memory
  712. * offlining. Everywhere else except page reclaim, the page is
  713. * invisible to the vm, so the page can not be migrated. So try to
  714. * free the metadata, so the page can be freed.
  715. */
  716. if (!page->mapping) {
  717. VM_BUG_ON(PageAnon(page));
  718. if (page_has_private(page)) {
  719. try_to_free_buffers(page);
  720. goto uncharge;
  721. }
  722. goto skip_unmap;
  723. }
  724. /* Establish migration ptes or remove ptes */
  725. try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
  726. skip_unmap:
  727. if (!page_mapped(page))
  728. rc = move_to_new_page(newpage, page, remap_swapcache, mode);
  729. if (rc && remap_swapcache)
  730. remove_migration_ptes(page, page);
  731. /* Drop an anon_vma reference if we took one */
  732. if (anon_vma)
  733. put_anon_vma(anon_vma);
  734. uncharge:
  735. mem_cgroup_end_migration(mem, page, newpage,
  736. (rc == MIGRATEPAGE_SUCCESS ||
  737. rc == MIGRATEPAGE_BALLOON_SUCCESS));
  738. unlock_page(page);
  739. out:
  740. return rc;
  741. }
  742. /*
  743. * Obtain the lock on page, remove all ptes and migrate the page
  744. * to the newly allocated page in newpage.
  745. */
  746. static int unmap_and_move(new_page_t get_new_page, unsigned long private,
  747. struct page *page, int force, enum migrate_mode mode)
  748. {
  749. int rc = 0;
  750. int *result = NULL;
  751. struct page *newpage = get_new_page(page, private, &result);
  752. if (!newpage)
  753. return -ENOMEM;
  754. if (page_count(page) == 1) {
  755. /* page was freed from under us. So we are done. */
  756. goto out;
  757. }
  758. if (unlikely(PageTransHuge(page)))
  759. if (unlikely(split_huge_page(page)))
  760. goto out;
  761. rc = __unmap_and_move(page, newpage, force, mode);
  762. if (unlikely(rc == MIGRATEPAGE_BALLOON_SUCCESS)) {
  763. /*
  764. * A ballooned page has been migrated already.
  765. * Now, it's the time to wrap-up counters,
  766. * handle the page back to Buddy and return.
  767. */
  768. dec_zone_page_state(page, NR_ISOLATED_ANON +
  769. page_is_file_cache(page));
  770. balloon_page_free(page);
  771. return MIGRATEPAGE_SUCCESS;
  772. }
  773. out:
  774. if (rc != -EAGAIN) {
  775. /*
  776. * A page that has been migrated has all references
  777. * removed and will be freed. A page that has not been
  778. * migrated will have kepts its references and be
  779. * restored.
  780. */
  781. list_del(&page->lru);
  782. dec_zone_page_state(page, NR_ISOLATED_ANON +
  783. page_is_file_cache(page));
  784. putback_lru_page(page);
  785. }
  786. /*
  787. * Move the new page to the LRU. If migration was not successful
  788. * then this will free the page.
  789. */
  790. putback_lru_page(newpage);
  791. if (result) {
  792. if (rc)
  793. *result = rc;
  794. else
  795. *result = page_to_nid(newpage);
  796. }
  797. return rc;
  798. }
  799. /*
  800. * Counterpart of unmap_and_move_page() for hugepage migration.
  801. *
  802. * This function doesn't wait the completion of hugepage I/O
  803. * because there is no race between I/O and migration for hugepage.
  804. * Note that currently hugepage I/O occurs only in direct I/O
  805. * where no lock is held and PG_writeback is irrelevant,
  806. * and writeback status of all subpages are counted in the reference
  807. * count of the head page (i.e. if all subpages of a 2MB hugepage are
  808. * under direct I/O, the reference of the head page is 512 and a bit more.)
  809. * This means that when we try to migrate hugepage whose subpages are
  810. * doing direct I/O, some references remain after try_to_unmap() and
  811. * hugepage migration fails without data corruption.
  812. *
  813. * There is also no race when direct I/O is issued on the page under migration,
  814. * because then pte is replaced with migration swap entry and direct I/O code
  815. * will wait in the page fault for migration to complete.
  816. */
  817. static int unmap_and_move_huge_page(new_page_t get_new_page,
  818. unsigned long private, struct page *hpage,
  819. int force, enum migrate_mode mode)
  820. {
  821. int rc = 0;
  822. int *result = NULL;
  823. struct page *new_hpage = get_new_page(hpage, private, &result);
  824. struct anon_vma *anon_vma = NULL;
  825. if (!new_hpage)
  826. return -ENOMEM;
  827. rc = -EAGAIN;
  828. if (!trylock_page(hpage)) {
  829. if (!force || mode != MIGRATE_SYNC)
  830. goto out;
  831. lock_page(hpage);
  832. }
  833. if (PageAnon(hpage))
  834. anon_vma = page_get_anon_vma(hpage);
  835. try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
  836. if (!page_mapped(hpage))
  837. rc = move_to_new_page(new_hpage, hpage, 1, mode);
  838. if (rc)
  839. remove_migration_ptes(hpage, hpage);
  840. if (anon_vma)
  841. put_anon_vma(anon_vma);
  842. if (!rc)
  843. hugetlb_cgroup_migrate(hpage, new_hpage);
  844. unlock_page(hpage);
  845. out:
  846. put_page(new_hpage);
  847. if (result) {
  848. if (rc)
  849. *result = rc;
  850. else
  851. *result = page_to_nid(new_hpage);
  852. }
  853. return rc;
  854. }
  855. /*
  856. * migrate_pages - migrate the pages specified in a list, to the free pages
  857. * supplied as the target for the page migration
  858. *
  859. * @from: The list of pages to be migrated.
  860. * @get_new_page: The function used to allocate free pages to be used
  861. * as the target of the page migration.
  862. * @private: Private data to be passed on to get_new_page()
  863. * @mode: The migration mode that specifies the constraints for
  864. * page migration, if any.
  865. * @reason: The reason for page migration.
  866. *
  867. * The function returns after 10 attempts or if no pages are movable any more
  868. * because the list has become empty or no retryable pages exist any more.
  869. * The caller should call putback_lru_pages() to return pages to the LRU
  870. * or free list only if ret != 0.
  871. *
  872. * Returns the number of pages that were not migrated, or an error code.
  873. */
  874. int migrate_pages(struct list_head *from, new_page_t get_new_page,
  875. unsigned long private, enum migrate_mode mode, int reason)
  876. {
  877. int retry = 1;
  878. int nr_failed = 0;
  879. int nr_succeeded = 0;
  880. int pass = 0;
  881. struct page *page;
  882. struct page *page2;
  883. int swapwrite = current->flags & PF_SWAPWRITE;
  884. int rc;
  885. if (!swapwrite)
  886. current->flags |= PF_SWAPWRITE;
  887. for(pass = 0; pass < 10 && retry; pass++) {
  888. retry = 0;
  889. list_for_each_entry_safe(page, page2, from, lru) {
  890. cond_resched();
  891. rc = unmap_and_move(get_new_page, private,
  892. page, pass > 2, mode);
  893. switch(rc) {
  894. case -ENOMEM:
  895. goto out;
  896. case -EAGAIN:
  897. retry++;
  898. break;
  899. case MIGRATEPAGE_SUCCESS:
  900. nr_succeeded++;
  901. break;
  902. default:
  903. /* Permanent failure */
  904. nr_failed++;
  905. break;
  906. }
  907. }
  908. }
  909. rc = nr_failed + retry;
  910. out:
  911. if (nr_succeeded)
  912. count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
  913. if (nr_failed)
  914. count_vm_events(PGMIGRATE_FAIL, nr_failed);
  915. trace_mm_migrate_pages(nr_succeeded, nr_failed, mode, reason);
  916. if (!swapwrite)
  917. current->flags &= ~PF_SWAPWRITE;
  918. return rc;
  919. }
  920. int migrate_huge_page(struct page *hpage, new_page_t get_new_page,
  921. unsigned long private, enum migrate_mode mode)
  922. {
  923. int pass, rc;
  924. for (pass = 0; pass < 10; pass++) {
  925. rc = unmap_and_move_huge_page(get_new_page, private,
  926. hpage, pass > 2, mode);
  927. switch (rc) {
  928. case -ENOMEM:
  929. goto out;
  930. case -EAGAIN:
  931. /* try again */
  932. cond_resched();
  933. break;
  934. case MIGRATEPAGE_SUCCESS:
  935. goto out;
  936. default:
  937. rc = -EIO;
  938. goto out;
  939. }
  940. }
  941. out:
  942. return rc;
  943. }
  944. #ifdef CONFIG_NUMA
  945. /*
  946. * Move a list of individual pages
  947. */
  948. struct page_to_node {
  949. unsigned long addr;
  950. struct page *page;
  951. int node;
  952. int status;
  953. };
  954. static struct page *new_page_node(struct page *p, unsigned long private,
  955. int **result)
  956. {
  957. struct page_to_node *pm = (struct page_to_node *)private;
  958. while (pm->node != MAX_NUMNODES && pm->page != p)
  959. pm++;
  960. if (pm->node == MAX_NUMNODES)
  961. return NULL;
  962. *result = &pm->status;
  963. return alloc_pages_exact_node(pm->node,
  964. GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
  965. }
  966. /*
  967. * Move a set of pages as indicated in the pm array. The addr
  968. * field must be set to the virtual address of the page to be moved
  969. * and the node number must contain a valid target node.
  970. * The pm array ends with node = MAX_NUMNODES.
  971. */
  972. static int do_move_page_to_node_array(struct mm_struct *mm,
  973. struct page_to_node *pm,
  974. int migrate_all)
  975. {
  976. int err;
  977. struct page_to_node *pp;
  978. LIST_HEAD(pagelist);
  979. down_read(&mm->mmap_sem);
  980. /*
  981. * Build a list of pages to migrate
  982. */
  983. for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
  984. struct vm_area_struct *vma;
  985. struct page *page;
  986. err = -EFAULT;
  987. vma = find_vma(mm, pp->addr);
  988. if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
  989. goto set_status;
  990. page = follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT);
  991. err = PTR_ERR(page);
  992. if (IS_ERR(page))
  993. goto set_status;
  994. err = -ENOENT;
  995. if (!page)
  996. goto set_status;
  997. /* Use PageReserved to check for zero page */
  998. if (PageReserved(page))
  999. goto put_and_set;
  1000. pp->page = page;
  1001. err = page_to_nid(page);
  1002. if (err == pp->node)
  1003. /*
  1004. * Node already in the right place
  1005. */
  1006. goto put_and_set;
  1007. err = -EACCES;
  1008. if (page_mapcount(page) > 1 &&
  1009. !migrate_all)
  1010. goto put_and_set;
  1011. err = isolate_lru_page(page);
  1012. if (!err) {
  1013. list_add_tail(&page->lru, &pagelist);
  1014. inc_zone_page_state(page, NR_ISOLATED_ANON +
  1015. page_is_file_cache(page));
  1016. }
  1017. put_and_set:
  1018. /*
  1019. * Either remove the duplicate refcount from
  1020. * isolate_lru_page() or drop the page ref if it was
  1021. * not isolated.
  1022. */
  1023. put_page(page);
  1024. set_status:
  1025. pp->status = err;
  1026. }
  1027. err = 0;
  1028. if (!list_empty(&pagelist)) {
  1029. err = migrate_pages(&pagelist, new_page_node,
  1030. (unsigned long)pm, MIGRATE_SYNC, MR_SYSCALL);
  1031. if (err)
  1032. putback_lru_pages(&pagelist);
  1033. }
  1034. up_read(&mm->mmap_sem);
  1035. return err;
  1036. }
  1037. /*
  1038. * Migrate an array of page address onto an array of nodes and fill
  1039. * the corresponding array of status.
  1040. */
  1041. static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
  1042. unsigned long nr_pages,
  1043. const void __user * __user *pages,
  1044. const int __user *nodes,
  1045. int __user *status, int flags)
  1046. {
  1047. struct page_to_node *pm;
  1048. unsigned long chunk_nr_pages;
  1049. unsigned long chunk_start;
  1050. int err;
  1051. err = -ENOMEM;
  1052. pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
  1053. if (!pm)
  1054. goto out;
  1055. migrate_prep();
  1056. /*
  1057. * Store a chunk of page_to_node array in a page,
  1058. * but keep the last one as a marker
  1059. */
  1060. chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
  1061. for (chunk_start = 0;
  1062. chunk_start < nr_pages;
  1063. chunk_start += chunk_nr_pages) {
  1064. int j;
  1065. if (chunk_start + chunk_nr_pages > nr_pages)
  1066. chunk_nr_pages = nr_pages - chunk_start;
  1067. /* fill the chunk pm with addrs and nodes from user-space */
  1068. for (j = 0; j < chunk_nr_pages; j++) {
  1069. const void __user *p;
  1070. int node;
  1071. err = -EFAULT;
  1072. if (get_user(p, pages + j + chunk_start))
  1073. goto out_pm;
  1074. pm[j].addr = (unsigned long) p;
  1075. if (get_user(node, nodes + j + chunk_start))
  1076. goto out_pm;
  1077. err = -ENODEV;
  1078. if (node < 0 || node >= MAX_NUMNODES)
  1079. goto out_pm;
  1080. if (!node_state(node, N_MEMORY))
  1081. goto out_pm;
  1082. err = -EACCES;
  1083. if (!node_isset(node, task_nodes))
  1084. goto out_pm;
  1085. pm[j].node = node;
  1086. }
  1087. /* End marker for this chunk */
  1088. pm[chunk_nr_pages].node = MAX_NUMNODES;
  1089. /* Migrate this chunk */
  1090. err = do_move_page_to_node_array(mm, pm,
  1091. flags & MPOL_MF_MOVE_ALL);
  1092. if (err < 0)
  1093. goto out_pm;
  1094. /* Return status information */
  1095. for (j = 0; j < chunk_nr_pages; j++)
  1096. if (put_user(pm[j].status, status + j + chunk_start)) {
  1097. err = -EFAULT;
  1098. goto out_pm;
  1099. }
  1100. }
  1101. err = 0;
  1102. out_pm:
  1103. free_page((unsigned long)pm);
  1104. out:
  1105. return err;
  1106. }
  1107. /*
  1108. * Determine the nodes of an array of pages and store it in an array of status.
  1109. */
  1110. static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
  1111. const void __user **pages, int *status)
  1112. {
  1113. unsigned long i;
  1114. down_read(&mm->mmap_sem);
  1115. for (i = 0; i < nr_pages; i++) {
  1116. unsigned long addr = (unsigned long)(*pages);
  1117. struct vm_area_struct *vma;
  1118. struct page *page;
  1119. int err = -EFAULT;
  1120. vma = find_vma(mm, addr);
  1121. if (!vma || addr < vma->vm_start)
  1122. goto set_status;
  1123. page = follow_page(vma, addr, 0);
  1124. err = PTR_ERR(page);
  1125. if (IS_ERR(page))
  1126. goto set_status;
  1127. err = -ENOENT;
  1128. /* Use PageReserved to check for zero page */
  1129. if (!page || PageReserved(page))
  1130. goto set_status;
  1131. err = page_to_nid(page);
  1132. set_status:
  1133. *status = err;
  1134. pages++;
  1135. status++;
  1136. }
  1137. up_read(&mm->mmap_sem);
  1138. }
  1139. /*
  1140. * Determine the nodes of a user array of pages and store it in
  1141. * a user array of status.
  1142. */
  1143. static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
  1144. const void __user * __user *pages,
  1145. int __user *status)
  1146. {
  1147. #define DO_PAGES_STAT_CHUNK_NR 16
  1148. const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
  1149. int chunk_status[DO_PAGES_STAT_CHUNK_NR];
  1150. while (nr_pages) {
  1151. unsigned long chunk_nr;
  1152. chunk_nr = nr_pages;
  1153. if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
  1154. chunk_nr = DO_PAGES_STAT_CHUNK_NR;
  1155. if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
  1156. break;
  1157. do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
  1158. if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
  1159. break;
  1160. pages += chunk_nr;
  1161. status += chunk_nr;
  1162. nr_pages -= chunk_nr;
  1163. }
  1164. return nr_pages ? -EFAULT : 0;
  1165. }
  1166. /*
  1167. * Move a list of pages in the address space of the currently executing
  1168. * process.
  1169. */
  1170. SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
  1171. const void __user * __user *, pages,
  1172. const int __user *, nodes,
  1173. int __user *, status, int, flags)
  1174. {
  1175. const struct cred *cred = current_cred(), *tcred;
  1176. struct task_struct *task;
  1177. struct mm_struct *mm;
  1178. int err;
  1179. nodemask_t task_nodes;
  1180. /* Check flags */
  1181. if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
  1182. return -EINVAL;
  1183. if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
  1184. return -EPERM;
  1185. /* Find the mm_struct */
  1186. rcu_read_lock();
  1187. task = pid ? find_task_by_vpid(pid) : current;
  1188. if (!task) {
  1189. rcu_read_unlock();
  1190. return -ESRCH;
  1191. }
  1192. get_task_struct(task);
  1193. /*
  1194. * Check if this process has the right to modify the specified
  1195. * process. The right exists if the process has administrative
  1196. * capabilities, superuser privileges or the same
  1197. * userid as the target process.
  1198. */
  1199. tcred = __task_cred(task);
  1200. if (!uid_eq(cred->euid, tcred->suid) && !uid_eq(cred->euid, tcred->uid) &&
  1201. !uid_eq(cred->uid, tcred->suid) && !uid_eq(cred->uid, tcred->uid) &&
  1202. !capable(CAP_SYS_NICE)) {
  1203. rcu_read_unlock();
  1204. err = -EPERM;
  1205. goto out;
  1206. }
  1207. rcu_read_unlock();
  1208. err = security_task_movememory(task);
  1209. if (err)
  1210. goto out;
  1211. task_nodes = cpuset_mems_allowed(task);
  1212. mm = get_task_mm(task);
  1213. put_task_struct(task);
  1214. if (!mm)
  1215. return -EINVAL;
  1216. if (nodes)
  1217. err = do_pages_move(mm, task_nodes, nr_pages, pages,
  1218. nodes, status, flags);
  1219. else
  1220. err = do_pages_stat(mm, nr_pages, pages, status);
  1221. mmput(mm);
  1222. return err;
  1223. out:
  1224. put_task_struct(task);
  1225. return err;
  1226. }
  1227. /*
  1228. * Call migration functions in the vma_ops that may prepare
  1229. * memory in a vm for migration. migration functions may perform
  1230. * the migration for vmas that do not have an underlying page struct.
  1231. */
  1232. int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
  1233. const nodemask_t *from, unsigned long flags)
  1234. {
  1235. struct vm_area_struct *vma;
  1236. int err = 0;
  1237. for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
  1238. if (vma->vm_ops && vma->vm_ops->migrate) {
  1239. err = vma->vm_ops->migrate(vma, to, from, flags);
  1240. if (err)
  1241. break;
  1242. }
  1243. }
  1244. return err;
  1245. }
  1246. #ifdef CONFIG_NUMA_BALANCING
  1247. /*
  1248. * Returns true if this is a safe migration target node for misplaced NUMA
  1249. * pages. Currently it only checks the watermarks which crude
  1250. */
  1251. static bool migrate_balanced_pgdat(struct pglist_data *pgdat,
  1252. unsigned long nr_migrate_pages)
  1253. {
  1254. int z;
  1255. for (z = pgdat->nr_zones - 1; z >= 0; z--) {
  1256. struct zone *zone = pgdat->node_zones + z;
  1257. if (!populated_zone(zone))
  1258. continue;
  1259. if (zone->all_unreclaimable)
  1260. continue;
  1261. /* Avoid waking kswapd by allocating pages_to_migrate pages. */
  1262. if (!zone_watermark_ok(zone, 0,
  1263. high_wmark_pages(zone) +
  1264. nr_migrate_pages,
  1265. 0, 0))
  1266. continue;
  1267. return true;
  1268. }
  1269. return false;
  1270. }
  1271. static struct page *alloc_misplaced_dst_page(struct page *page,
  1272. unsigned long data,
  1273. int **result)
  1274. {
  1275. int nid = (int) data;
  1276. struct page *newpage;
  1277. newpage = alloc_pages_exact_node(nid,
  1278. (GFP_HIGHUSER_MOVABLE | GFP_THISNODE |
  1279. __GFP_NOMEMALLOC | __GFP_NORETRY |
  1280. __GFP_NOWARN) &
  1281. ~GFP_IOFS, 0);
  1282. if (newpage)
  1283. page_nid_xchg_last(newpage, page_nid_last(page));
  1284. return newpage;
  1285. }
  1286. /*
  1287. * page migration rate limiting control.
  1288. * Do not migrate more than @pages_to_migrate in a @migrate_interval_millisecs
  1289. * window of time. Default here says do not migrate more than 1280M per second.
  1290. * If a node is rate-limited then PTE NUMA updates are also rate-limited. However
  1291. * as it is faults that reset the window, pte updates will happen unconditionally
  1292. * if there has not been a fault since @pteupdate_interval_millisecs after the
  1293. * throttle window closed.
  1294. */
  1295. static unsigned int migrate_interval_millisecs __read_mostly = 100;
  1296. static unsigned int pteupdate_interval_millisecs __read_mostly = 1000;
  1297. static unsigned int ratelimit_pages __read_mostly = 128 << (20 - PAGE_SHIFT);
  1298. /* Returns true if NUMA migration is currently rate limited */
  1299. bool migrate_ratelimited(int node)
  1300. {
  1301. pg_data_t *pgdat = NODE_DATA(node);
  1302. if (time_after(jiffies, pgdat->numabalancing_migrate_next_window +
  1303. msecs_to_jiffies(pteupdate_interval_millisecs)))
  1304. return false;
  1305. if (pgdat->numabalancing_migrate_nr_pages < ratelimit_pages)
  1306. return false;
  1307. return true;
  1308. }
  1309. /* Returns true if the node is migrate rate-limited after the update */
  1310. bool numamigrate_update_ratelimit(pg_data_t *pgdat, unsigned long nr_pages)
  1311. {
  1312. bool rate_limited = false;
  1313. /*
  1314. * Rate-limit the amount of data that is being migrated to a node.
  1315. * Optimal placement is no good if the memory bus is saturated and
  1316. * all the time is being spent migrating!
  1317. */
  1318. spin_lock(&pgdat->numabalancing_migrate_lock);
  1319. if (time_after(jiffies, pgdat->numabalancing_migrate_next_window)) {
  1320. pgdat->numabalancing_migrate_nr_pages = 0;
  1321. pgdat->numabalancing_migrate_next_window = jiffies +
  1322. msecs_to_jiffies(migrate_interval_millisecs);
  1323. }
  1324. if (pgdat->numabalancing_migrate_nr_pages > ratelimit_pages)
  1325. rate_limited = true;
  1326. else
  1327. pgdat->numabalancing_migrate_nr_pages += nr_pages;
  1328. spin_unlock(&pgdat->numabalancing_migrate_lock);
  1329. return rate_limited;
  1330. }
  1331. int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
  1332. {
  1333. int page_lru;
  1334. VM_BUG_ON(compound_order(page) && !PageTransHuge(page));
  1335. /* Avoid migrating to a node that is nearly full */
  1336. if (!migrate_balanced_pgdat(pgdat, 1UL << compound_order(page)))
  1337. return 0;
  1338. if (isolate_lru_page(page))
  1339. return 0;
  1340. /*
  1341. * migrate_misplaced_transhuge_page() skips page migration's usual
  1342. * check on page_count(), so we must do it here, now that the page
  1343. * has been isolated: a GUP pin, or any other pin, prevents migration.
  1344. * The expected page count is 3: 1 for page's mapcount and 1 for the
  1345. * caller's pin and 1 for the reference taken by isolate_lru_page().
  1346. */
  1347. if (PageTransHuge(page) && page_count(page) != 3) {
  1348. putback_lru_page(page);
  1349. return 0;
  1350. }
  1351. page_lru = page_is_file_cache(page);
  1352. mod_zone_page_state(page_zone(page), NR_ISOLATED_ANON + page_lru,
  1353. hpage_nr_pages(page));
  1354. /*
  1355. * Isolating the page has taken another reference, so the
  1356. * caller's reference can be safely dropped without the page
  1357. * disappearing underneath us during migration.
  1358. */
  1359. put_page(page);
  1360. return 1;
  1361. }
  1362. /*
  1363. * Attempt to migrate a misplaced page to the specified destination
  1364. * node. Caller is expected to have an elevated reference count on
  1365. * the page that will be dropped by this function before returning.
  1366. */
  1367. int migrate_misplaced_page(struct page *page, int node)
  1368. {
  1369. pg_data_t *pgdat = NODE_DATA(node);
  1370. int isolated;
  1371. int nr_remaining;
  1372. LIST_HEAD(migratepages);
  1373. /*
  1374. * Don't migrate pages that are mapped in multiple processes.
  1375. * TODO: Handle false sharing detection instead of this hammer
  1376. */
  1377. if (page_mapcount(page) != 1)
  1378. goto out;
  1379. /*
  1380. * Rate-limit the amount of data that is being migrated to a node.
  1381. * Optimal placement is no good if the memory bus is saturated and
  1382. * all the time is being spent migrating!
  1383. */
  1384. if (numamigrate_update_ratelimit(pgdat, 1))
  1385. goto out;
  1386. isolated = numamigrate_isolate_page(pgdat, page);
  1387. if (!isolated)
  1388. goto out;
  1389. list_add(&page->lru, &migratepages);
  1390. nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_page,
  1391. node, MIGRATE_ASYNC, MR_NUMA_MISPLACED);
  1392. if (nr_remaining) {
  1393. putback_lru_pages(&migratepages);
  1394. isolated = 0;
  1395. } else
  1396. count_vm_numa_event(NUMA_PAGE_MIGRATE);
  1397. BUG_ON(!list_empty(&migratepages));
  1398. return isolated;
  1399. out:
  1400. put_page(page);
  1401. return 0;
  1402. }
  1403. #endif /* CONFIG_NUMA_BALANCING */
  1404. #if defined(CONFIG_NUMA_BALANCING) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
  1405. /*
  1406. * Migrates a THP to a given target node. page must be locked and is unlocked
  1407. * before returning.
  1408. */
  1409. int migrate_misplaced_transhuge_page(struct mm_struct *mm,
  1410. struct vm_area_struct *vma,
  1411. pmd_t *pmd, pmd_t entry,
  1412. unsigned long address,
  1413. struct page *page, int node)
  1414. {
  1415. unsigned long haddr = address & HPAGE_PMD_MASK;
  1416. pg_data_t *pgdat = NODE_DATA(node);
  1417. int isolated = 0;
  1418. struct page *new_page = NULL;
  1419. struct mem_cgroup *memcg = NULL;
  1420. int page_lru = page_is_file_cache(page);
  1421. /*
  1422. * Don't migrate pages that are mapped in multiple processes.
  1423. * TODO: Handle false sharing detection instead of this hammer
  1424. */
  1425. if (page_mapcount(page) != 1)
  1426. goto out_dropref;
  1427. /*
  1428. * Rate-limit the amount of data that is being migrated to a node.
  1429. * Optimal placement is no good if the memory bus is saturated and
  1430. * all the time is being spent migrating!
  1431. */
  1432. if (numamigrate_update_ratelimit(pgdat, HPAGE_PMD_NR))
  1433. goto out_dropref;
  1434. new_page = alloc_pages_node(node,
  1435. (GFP_TRANSHUGE | GFP_THISNODE) & ~__GFP_WAIT, HPAGE_PMD_ORDER);
  1436. if (!new_page)
  1437. goto out_fail;
  1438. page_nid_xchg_last(new_page, page_nid_last(page));
  1439. isolated = numamigrate_isolate_page(pgdat, page);
  1440. if (!isolated) {
  1441. put_page(new_page);
  1442. goto out_fail;
  1443. }
  1444. /* Prepare a page as a migration target */
  1445. __set_page_locked(new_page);
  1446. SetPageSwapBacked(new_page);
  1447. /* anon mapping, we can simply copy page->mapping to the new page: */
  1448. new_page->mapping = page->mapping;
  1449. new_page->index = page->index;
  1450. migrate_page_copy(new_page, page);
  1451. WARN_ON(PageLRU(new_page));
  1452. /* Recheck the target PMD */
  1453. spin_lock(&mm->page_table_lock);
  1454. if (unlikely(!pmd_same(*pmd, entry))) {
  1455. spin_unlock(&mm->page_table_lock);
  1456. /* Reverse changes made by migrate_page_copy() */
  1457. if (TestClearPageActive(new_page))
  1458. SetPageActive(page);
  1459. if (TestClearPageUnevictable(new_page))
  1460. SetPageUnevictable(page);
  1461. mlock_migrate_page(page, new_page);
  1462. unlock_page(new_page);
  1463. put_page(new_page); /* Free it */
  1464. unlock_page(page);
  1465. putback_lru_page(page);
  1466. count_vm_events(PGMIGRATE_FAIL, HPAGE_PMD_NR);
  1467. isolated = 0;
  1468. goto out;
  1469. }
  1470. /*
  1471. * Traditional migration needs to prepare the memcg charge
  1472. * transaction early to prevent the old page from being
  1473. * uncharged when installing migration entries. Here we can
  1474. * save the potential rollback and start the charge transfer
  1475. * only when migration is already known to end successfully.
  1476. */
  1477. mem_cgroup_prepare_migration(page, new_page, &memcg);
  1478. entry = mk_pmd(new_page, vma->vm_page_prot);
  1479. entry = pmd_mknonnuma(entry);
  1480. entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
  1481. entry = pmd_mkhuge(entry);
  1482. page_add_new_anon_rmap(new_page, vma, haddr);
  1483. set_pmd_at(mm, haddr, pmd, entry);
  1484. update_mmu_cache_pmd(vma, address, &entry);
  1485. page_remove_rmap(page);
  1486. /*
  1487. * Finish the charge transaction under the page table lock to
  1488. * prevent split_huge_page() from dividing up the charge
  1489. * before it's fully transferred to the new page.
  1490. */
  1491. mem_cgroup_end_migration(memcg, page, new_page, true);
  1492. spin_unlock(&mm->page_table_lock);
  1493. unlock_page(new_page);
  1494. unlock_page(page);
  1495. put_page(page); /* Drop the rmap reference */
  1496. put_page(page); /* Drop the LRU isolation reference */
  1497. count_vm_events(PGMIGRATE_SUCCESS, HPAGE_PMD_NR);
  1498. count_vm_numa_events(NUMA_PAGE_MIGRATE, HPAGE_PMD_NR);
  1499. out:
  1500. mod_zone_page_state(page_zone(page),
  1501. NR_ISOLATED_ANON + page_lru,
  1502. -HPAGE_PMD_NR);
  1503. return isolated;
  1504. out_fail:
  1505. count_vm_events(PGMIGRATE_FAIL, HPAGE_PMD_NR);
  1506. out_dropref:
  1507. unlock_page(page);
  1508. put_page(page);
  1509. return 0;
  1510. }
  1511. #endif /* CONFIG_NUMA_BALANCING */
  1512. #endif /* CONFIG_NUMA */