migrate.c 48 KB

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