migrate.c 47 KB

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