migrate.c 50 KB

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