migrate.c 44 KB

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