migrate.c 48 KB

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