migrate.c 42 KB

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