migrate.c 49 KB

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