migrate.c 45 KB

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