migrate.c 45 KB

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