migrate.c 49 KB

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