migrate.c 48 KB

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