migrate.c 47 KB

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