migrate.c 47 KB

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