migrate.c 48 KB

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