migrate.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911
  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 <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. if (pmd_trans_huge(*pmd))
  112. goto out;
  113. ptep = pte_offset_map(pmd, addr);
  114. /*
  115. * Peek to check is_swap_pte() before taking ptlock? No, we
  116. * can race mremap's move_ptes(), which skips anon_vma lock.
  117. */
  118. ptl = pte_lockptr(mm, pmd);
  119. }
  120. spin_lock(ptl);
  121. pte = *ptep;
  122. if (!is_swap_pte(pte))
  123. goto unlock;
  124. entry = pte_to_swp_entry(pte);
  125. if (!is_migration_entry(entry) ||
  126. migration_entry_to_page(entry) != old)
  127. goto unlock;
  128. get_page(new);
  129. pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
  130. if (pte_swp_soft_dirty(*ptep))
  131. pte = pte_mksoft_dirty(pte);
  132. if (is_write_migration_entry(entry))
  133. pte = pte_mkwrite(pte);
  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. static 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. if (remap_swapcache)
  676. remove_migration_ptes(page, newpage);
  677. page->mapping = NULL;
  678. }
  679. unlock_page(newpage);
  680. return rc;
  681. }
  682. static int __unmap_and_move(struct page *page, struct page *newpage,
  683. int force, enum migrate_mode mode)
  684. {
  685. int rc = -EAGAIN;
  686. int remap_swapcache = 1;
  687. struct mem_cgroup *mem;
  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. /* charge against new page */
  710. mem_cgroup_prepare_migration(page, newpage, &mem);
  711. if (PageWriteback(page)) {
  712. /*
  713. * Only in the case of a full synchronous migration is it
  714. * necessary to wait for PageWriteback. In the async case,
  715. * the retry loop is too short and in the sync-light case,
  716. * the overhead of stalling is too much
  717. */
  718. if (mode != MIGRATE_SYNC) {
  719. rc = -EBUSY;
  720. goto uncharge;
  721. }
  722. if (!force)
  723. goto uncharge;
  724. wait_on_page_writeback(page);
  725. }
  726. /*
  727. * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
  728. * we cannot notice that anon_vma is freed while we migrates a page.
  729. * This get_anon_vma() delays freeing anon_vma pointer until the end
  730. * of migration. File cache pages are no problem because of page_lock()
  731. * File Caches may use write_page() or lock_page() in migration, then,
  732. * just care Anon page here.
  733. */
  734. if (PageAnon(page) && !PageKsm(page)) {
  735. /*
  736. * Only page_lock_anon_vma_read() understands the subtleties of
  737. * getting a hold on an anon_vma from outside one of its mms.
  738. */
  739. anon_vma = page_get_anon_vma(page);
  740. if (anon_vma) {
  741. /*
  742. * Anon page
  743. */
  744. } else if (PageSwapCache(page)) {
  745. /*
  746. * We cannot be sure that the anon_vma of an unmapped
  747. * swapcache page is safe to use because we don't
  748. * know in advance if the VMA that this page belonged
  749. * to still exists. If the VMA and others sharing the
  750. * data have been freed, then the anon_vma could
  751. * already be invalid.
  752. *
  753. * To avoid this possibility, swapcache pages get
  754. * migrated but are not remapped when migration
  755. * completes
  756. */
  757. remap_swapcache = 0;
  758. } else {
  759. goto uncharge;
  760. }
  761. }
  762. if (unlikely(balloon_page_movable(page))) {
  763. /*
  764. * A ballooned page does not need any special attention from
  765. * physical to virtual reverse mapping procedures.
  766. * Skip any attempt to unmap PTEs or to remap swap cache,
  767. * in order to avoid burning cycles at rmap level, and perform
  768. * the page migration right away (proteced by page lock).
  769. */
  770. rc = balloon_page_migrate(newpage, page, mode);
  771. goto uncharge;
  772. }
  773. /*
  774. * Corner case handling:
  775. * 1. When a new swap-cache page is read into, it is added to the LRU
  776. * and treated as swapcache but it has no rmap yet.
  777. * Calling try_to_unmap() against a page->mapping==NULL page will
  778. * trigger a BUG. So handle it here.
  779. * 2. An orphaned page (see truncate_complete_page) might have
  780. * fs-private metadata. The page can be picked up due to memory
  781. * offlining. Everywhere else except page reclaim, the page is
  782. * invisible to the vm, so the page can not be migrated. So try to
  783. * free the metadata, so the page can be freed.
  784. */
  785. if (!page->mapping) {
  786. VM_BUG_ON_PAGE(PageAnon(page), page);
  787. if (page_has_private(page)) {
  788. try_to_free_buffers(page);
  789. goto uncharge;
  790. }
  791. goto skip_unmap;
  792. }
  793. /* Establish migration ptes or remove ptes */
  794. try_to_unmap(page, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
  795. skip_unmap:
  796. if (!page_mapped(page))
  797. rc = move_to_new_page(newpage, page, remap_swapcache, mode);
  798. if (rc && remap_swapcache)
  799. remove_migration_ptes(page, page);
  800. /* Drop an anon_vma reference if we took one */
  801. if (anon_vma)
  802. put_anon_vma(anon_vma);
  803. uncharge:
  804. mem_cgroup_end_migration(mem, page, newpage,
  805. (rc == MIGRATEPAGE_SUCCESS ||
  806. rc == MIGRATEPAGE_BALLOON_SUCCESS));
  807. unlock_page(page);
  808. out:
  809. return rc;
  810. }
  811. /*
  812. * Obtain the lock on page, remove all ptes and migrate the page
  813. * to the newly allocated page in newpage.
  814. */
  815. static int unmap_and_move(new_page_t get_new_page, unsigned long private,
  816. struct page *page, int force, enum migrate_mode mode)
  817. {
  818. int rc = 0;
  819. int *result = NULL;
  820. struct page *newpage = get_new_page(page, private, &result);
  821. if (!newpage)
  822. return -ENOMEM;
  823. if (page_count(page) == 1) {
  824. /* page was freed from under us. So we are done. */
  825. goto out;
  826. }
  827. if (unlikely(PageTransHuge(page)))
  828. if (unlikely(split_huge_page(page)))
  829. goto out;
  830. rc = __unmap_and_move(page, newpage, force, mode);
  831. if (unlikely(rc == MIGRATEPAGE_BALLOON_SUCCESS)) {
  832. /*
  833. * A ballooned page has been migrated already.
  834. * Now, it's the time to wrap-up counters,
  835. * handle the page back to Buddy and return.
  836. */
  837. dec_zone_page_state(page, NR_ISOLATED_ANON +
  838. page_is_file_cache(page));
  839. balloon_page_free(page);
  840. return MIGRATEPAGE_SUCCESS;
  841. }
  842. out:
  843. if (rc != -EAGAIN) {
  844. /*
  845. * A page that has been migrated has all references
  846. * removed and will be freed. A page that has not been
  847. * migrated will have kepts its references and be
  848. * restored.
  849. */
  850. list_del(&page->lru);
  851. dec_zone_page_state(page, NR_ISOLATED_ANON +
  852. page_is_file_cache(page));
  853. putback_lru_page(page);
  854. }
  855. /*
  856. * Move the new page to the LRU. If migration was not successful
  857. * then this will free the page.
  858. */
  859. putback_lru_page(newpage);
  860. if (result) {
  861. if (rc)
  862. *result = rc;
  863. else
  864. *result = page_to_nid(newpage);
  865. }
  866. return rc;
  867. }
  868. /*
  869. * Counterpart of unmap_and_move_page() for hugepage migration.
  870. *
  871. * This function doesn't wait the completion of hugepage I/O
  872. * because there is no race between I/O and migration for hugepage.
  873. * Note that currently hugepage I/O occurs only in direct I/O
  874. * where no lock is held and PG_writeback is irrelevant,
  875. * and writeback status of all subpages are counted in the reference
  876. * count of the head page (i.e. if all subpages of a 2MB hugepage are
  877. * under direct I/O, the reference of the head page is 512 and a bit more.)
  878. * This means that when we try to migrate hugepage whose subpages are
  879. * doing direct I/O, some references remain after try_to_unmap() and
  880. * hugepage migration fails without data corruption.
  881. *
  882. * There is also no race when direct I/O is issued on the page under migration,
  883. * because then pte is replaced with migration swap entry and direct I/O code
  884. * will wait in the page fault for migration to complete.
  885. */
  886. static int unmap_and_move_huge_page(new_page_t get_new_page,
  887. unsigned long private, struct page *hpage,
  888. int force, enum migrate_mode mode)
  889. {
  890. int rc = 0;
  891. int *result = NULL;
  892. struct page *new_hpage;
  893. struct anon_vma *anon_vma = NULL;
  894. /*
  895. * Movability of hugepages depends on architectures and hugepage size.
  896. * This check is necessary because some callers of hugepage migration
  897. * like soft offline and memory hotremove don't walk through page
  898. * tables or check whether the hugepage is pmd-based or not before
  899. * kicking migration.
  900. */
  901. if (!hugepage_migration_support(page_hstate(hpage))) {
  902. putback_active_hugepage(hpage);
  903. return -ENOSYS;
  904. }
  905. new_hpage = get_new_page(hpage, private, &result);
  906. if (!new_hpage)
  907. return -ENOMEM;
  908. rc = -EAGAIN;
  909. if (!trylock_page(hpage)) {
  910. if (!force || mode != MIGRATE_SYNC)
  911. goto out;
  912. lock_page(hpage);
  913. }
  914. if (PageAnon(hpage))
  915. anon_vma = page_get_anon_vma(hpage);
  916. try_to_unmap(hpage, TTU_MIGRATION|TTU_IGNORE_MLOCK|TTU_IGNORE_ACCESS);
  917. if (!page_mapped(hpage))
  918. rc = move_to_new_page(new_hpage, hpage, 1, mode);
  919. if (rc)
  920. remove_migration_ptes(hpage, hpage);
  921. if (anon_vma)
  922. put_anon_vma(anon_vma);
  923. if (!rc)
  924. hugetlb_cgroup_migrate(hpage, new_hpage);
  925. unlock_page(hpage);
  926. out:
  927. if (rc != -EAGAIN)
  928. putback_active_hugepage(hpage);
  929. put_page(new_hpage);
  930. if (result) {
  931. if (rc)
  932. *result = rc;
  933. else
  934. *result = page_to_nid(new_hpage);
  935. }
  936. return rc;
  937. }
  938. /*
  939. * migrate_pages - migrate the pages specified in a list, to the free pages
  940. * supplied as the target for the page migration
  941. *
  942. * @from: The list of pages to be migrated.
  943. * @get_new_page: The function used to allocate free pages to be used
  944. * as the target of the page migration.
  945. * @private: Private data to be passed on to get_new_page()
  946. * @mode: The migration mode that specifies the constraints for
  947. * page migration, if any.
  948. * @reason: The reason for page migration.
  949. *
  950. * The function returns after 10 attempts or if no pages are movable any more
  951. * because the list has become empty or no retryable pages exist any more.
  952. * The caller should call putback_lru_pages() to return pages to the LRU
  953. * or free list only if ret != 0.
  954. *
  955. * Returns the number of pages that were not migrated, or an error code.
  956. */
  957. int migrate_pages(struct list_head *from, new_page_t get_new_page,
  958. unsigned long private, enum migrate_mode mode, int reason)
  959. {
  960. int retry = 1;
  961. int nr_failed = 0;
  962. int nr_succeeded = 0;
  963. int pass = 0;
  964. struct page *page;
  965. struct page *page2;
  966. int swapwrite = current->flags & PF_SWAPWRITE;
  967. int rc;
  968. if (!swapwrite)
  969. current->flags |= PF_SWAPWRITE;
  970. for(pass = 0; pass < 10 && retry; pass++) {
  971. retry = 0;
  972. list_for_each_entry_safe(page, page2, from, lru) {
  973. cond_resched();
  974. if (PageHuge(page))
  975. rc = unmap_and_move_huge_page(get_new_page,
  976. private, page, pass > 2, mode);
  977. else
  978. rc = unmap_and_move(get_new_page, private,
  979. page, pass > 2, mode);
  980. switch(rc) {
  981. case -ENOMEM:
  982. goto out;
  983. case -EAGAIN:
  984. retry++;
  985. break;
  986. case MIGRATEPAGE_SUCCESS:
  987. nr_succeeded++;
  988. break;
  989. default:
  990. /*
  991. * Permanent failure (-EBUSY, -ENOSYS, etc.):
  992. * unlike -EAGAIN case, the failed page is
  993. * removed from migration page list and not
  994. * retried in the next outer loop.
  995. */
  996. nr_failed++;
  997. break;
  998. }
  999. }
  1000. }
  1001. rc = nr_failed + retry;
  1002. out:
  1003. if (nr_succeeded)
  1004. count_vm_events(PGMIGRATE_SUCCESS, nr_succeeded);
  1005. if (nr_failed)
  1006. count_vm_events(PGMIGRATE_FAIL, nr_failed);
  1007. trace_mm_migrate_pages(nr_succeeded, nr_failed, mode, reason);
  1008. if (!swapwrite)
  1009. current->flags &= ~PF_SWAPWRITE;
  1010. return rc;
  1011. }
  1012. #ifdef CONFIG_NUMA
  1013. /*
  1014. * Move a list of individual pages
  1015. */
  1016. struct page_to_node {
  1017. unsigned long addr;
  1018. struct page *page;
  1019. int node;
  1020. int status;
  1021. };
  1022. static struct page *new_page_node(struct page *p, unsigned long private,
  1023. int **result)
  1024. {
  1025. struct page_to_node *pm = (struct page_to_node *)private;
  1026. while (pm->node != MAX_NUMNODES && pm->page != p)
  1027. pm++;
  1028. if (pm->node == MAX_NUMNODES)
  1029. return NULL;
  1030. *result = &pm->status;
  1031. if (PageHuge(p))
  1032. return alloc_huge_page_node(page_hstate(compound_head(p)),
  1033. pm->node);
  1034. else
  1035. return alloc_pages_exact_node(pm->node,
  1036. GFP_HIGHUSER_MOVABLE | __GFP_THISNODE, 0);
  1037. }
  1038. /*
  1039. * Move a set of pages as indicated in the pm array. The addr
  1040. * field must be set to the virtual address of the page to be moved
  1041. * and the node number must contain a valid target node.
  1042. * The pm array ends with node = MAX_NUMNODES.
  1043. */
  1044. static int do_move_page_to_node_array(struct mm_struct *mm,
  1045. struct page_to_node *pm,
  1046. int migrate_all)
  1047. {
  1048. int err;
  1049. struct page_to_node *pp;
  1050. LIST_HEAD(pagelist);
  1051. down_read(&mm->mmap_sem);
  1052. /*
  1053. * Build a list of pages to migrate
  1054. */
  1055. for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
  1056. struct vm_area_struct *vma;
  1057. struct page *page;
  1058. err = -EFAULT;
  1059. vma = find_vma(mm, pp->addr);
  1060. if (!vma || pp->addr < vma->vm_start || !vma_migratable(vma))
  1061. goto set_status;
  1062. page = follow_page(vma, pp->addr, FOLL_GET|FOLL_SPLIT);
  1063. err = PTR_ERR(page);
  1064. if (IS_ERR(page))
  1065. goto set_status;
  1066. err = -ENOENT;
  1067. if (!page)
  1068. goto set_status;
  1069. /* Use PageReserved to check for zero page */
  1070. if (PageReserved(page))
  1071. goto put_and_set;
  1072. pp->page = page;
  1073. err = page_to_nid(page);
  1074. if (err == pp->node)
  1075. /*
  1076. * Node already in the right place
  1077. */
  1078. goto put_and_set;
  1079. err = -EACCES;
  1080. if (page_mapcount(page) > 1 &&
  1081. !migrate_all)
  1082. goto put_and_set;
  1083. if (PageHuge(page)) {
  1084. isolate_huge_page(page, &pagelist);
  1085. goto put_and_set;
  1086. }
  1087. err = isolate_lru_page(page);
  1088. if (!err) {
  1089. list_add_tail(&page->lru, &pagelist);
  1090. inc_zone_page_state(page, NR_ISOLATED_ANON +
  1091. page_is_file_cache(page));
  1092. }
  1093. put_and_set:
  1094. /*
  1095. * Either remove the duplicate refcount from
  1096. * isolate_lru_page() or drop the page ref if it was
  1097. * not isolated.
  1098. */
  1099. put_page(page);
  1100. set_status:
  1101. pp->status = err;
  1102. }
  1103. err = 0;
  1104. if (!list_empty(&pagelist)) {
  1105. err = migrate_pages(&pagelist, new_page_node,
  1106. (unsigned long)pm, MIGRATE_SYNC, MR_SYSCALL);
  1107. if (err)
  1108. putback_movable_pages(&pagelist);
  1109. }
  1110. up_read(&mm->mmap_sem);
  1111. return err;
  1112. }
  1113. /*
  1114. * Migrate an array of page address onto an array of nodes and fill
  1115. * the corresponding array of status.
  1116. */
  1117. static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
  1118. unsigned long nr_pages,
  1119. const void __user * __user *pages,
  1120. const int __user *nodes,
  1121. int __user *status, int flags)
  1122. {
  1123. struct page_to_node *pm;
  1124. unsigned long chunk_nr_pages;
  1125. unsigned long chunk_start;
  1126. int err;
  1127. err = -ENOMEM;
  1128. pm = (struct page_to_node *)__get_free_page(GFP_KERNEL);
  1129. if (!pm)
  1130. goto out;
  1131. migrate_prep();
  1132. /*
  1133. * Store a chunk of page_to_node array in a page,
  1134. * but keep the last one as a marker
  1135. */
  1136. chunk_nr_pages = (PAGE_SIZE / sizeof(struct page_to_node)) - 1;
  1137. for (chunk_start = 0;
  1138. chunk_start < nr_pages;
  1139. chunk_start += chunk_nr_pages) {
  1140. int j;
  1141. if (chunk_start + chunk_nr_pages > nr_pages)
  1142. chunk_nr_pages = nr_pages - chunk_start;
  1143. /* fill the chunk pm with addrs and nodes from user-space */
  1144. for (j = 0; j < chunk_nr_pages; j++) {
  1145. const void __user *p;
  1146. int node;
  1147. err = -EFAULT;
  1148. if (get_user(p, pages + j + chunk_start))
  1149. goto out_pm;
  1150. pm[j].addr = (unsigned long) p;
  1151. if (get_user(node, nodes + j + chunk_start))
  1152. goto out_pm;
  1153. err = -ENODEV;
  1154. if (node < 0 || node >= MAX_NUMNODES)
  1155. goto out_pm;
  1156. if (!node_state(node, N_MEMORY))
  1157. goto out_pm;
  1158. err = -EACCES;
  1159. if (!node_isset(node, task_nodes))
  1160. goto out_pm;
  1161. pm[j].node = node;
  1162. }
  1163. /* End marker for this chunk */
  1164. pm[chunk_nr_pages].node = MAX_NUMNODES;
  1165. /* Migrate this chunk */
  1166. err = do_move_page_to_node_array(mm, pm,
  1167. flags & MPOL_MF_MOVE_ALL);
  1168. if (err < 0)
  1169. goto out_pm;
  1170. /* Return status information */
  1171. for (j = 0; j < chunk_nr_pages; j++)
  1172. if (put_user(pm[j].status, status + j + chunk_start)) {
  1173. err = -EFAULT;
  1174. goto out_pm;
  1175. }
  1176. }
  1177. err = 0;
  1178. out_pm:
  1179. free_page((unsigned long)pm);
  1180. out:
  1181. return err;
  1182. }
  1183. /*
  1184. * Determine the nodes of an array of pages and store it in an array of status.
  1185. */
  1186. static void do_pages_stat_array(struct mm_struct *mm, unsigned long nr_pages,
  1187. const void __user **pages, int *status)
  1188. {
  1189. unsigned long i;
  1190. down_read(&mm->mmap_sem);
  1191. for (i = 0; i < nr_pages; i++) {
  1192. unsigned long addr = (unsigned long)(*pages);
  1193. struct vm_area_struct *vma;
  1194. struct page *page;
  1195. int err = -EFAULT;
  1196. vma = find_vma(mm, addr);
  1197. if (!vma || addr < vma->vm_start)
  1198. goto set_status;
  1199. page = follow_page(vma, addr, 0);
  1200. err = PTR_ERR(page);
  1201. if (IS_ERR(page))
  1202. goto set_status;
  1203. err = -ENOENT;
  1204. /* Use PageReserved to check for zero page */
  1205. if (!page || PageReserved(page))
  1206. goto set_status;
  1207. err = page_to_nid(page);
  1208. set_status:
  1209. *status = err;
  1210. pages++;
  1211. status++;
  1212. }
  1213. up_read(&mm->mmap_sem);
  1214. }
  1215. /*
  1216. * Determine the nodes of a user array of pages and store it in
  1217. * a user array of status.
  1218. */
  1219. static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
  1220. const void __user * __user *pages,
  1221. int __user *status)
  1222. {
  1223. #define DO_PAGES_STAT_CHUNK_NR 16
  1224. const void __user *chunk_pages[DO_PAGES_STAT_CHUNK_NR];
  1225. int chunk_status[DO_PAGES_STAT_CHUNK_NR];
  1226. while (nr_pages) {
  1227. unsigned long chunk_nr;
  1228. chunk_nr = nr_pages;
  1229. if (chunk_nr > DO_PAGES_STAT_CHUNK_NR)
  1230. chunk_nr = DO_PAGES_STAT_CHUNK_NR;
  1231. if (copy_from_user(chunk_pages, pages, chunk_nr * sizeof(*chunk_pages)))
  1232. break;
  1233. do_pages_stat_array(mm, chunk_nr, chunk_pages, chunk_status);
  1234. if (copy_to_user(status, chunk_status, chunk_nr * sizeof(*status)))
  1235. break;
  1236. pages += chunk_nr;
  1237. status += chunk_nr;
  1238. nr_pages -= chunk_nr;
  1239. }
  1240. return nr_pages ? -EFAULT : 0;
  1241. }
  1242. /*
  1243. * Move a list of pages in the address space of the currently executing
  1244. * process.
  1245. */
  1246. SYSCALL_DEFINE6(move_pages, pid_t, pid, unsigned long, nr_pages,
  1247. const void __user * __user *, pages,
  1248. const int __user *, nodes,
  1249. int __user *, status, int, flags)
  1250. {
  1251. const struct cred *cred = current_cred(), *tcred;
  1252. struct task_struct *task;
  1253. struct mm_struct *mm;
  1254. int err;
  1255. nodemask_t task_nodes;
  1256. /* Check flags */
  1257. if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
  1258. return -EINVAL;
  1259. if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
  1260. return -EPERM;
  1261. /* Find the mm_struct */
  1262. rcu_read_lock();
  1263. task = pid ? find_task_by_vpid(pid) : current;
  1264. if (!task) {
  1265. rcu_read_unlock();
  1266. return -ESRCH;
  1267. }
  1268. get_task_struct(task);
  1269. /*
  1270. * Check if this process has the right to modify the specified
  1271. * process. The right exists if the process has administrative
  1272. * capabilities, superuser privileges or the same
  1273. * userid as the target process.
  1274. */
  1275. tcred = __task_cred(task);
  1276. if (!uid_eq(cred->euid, tcred->suid) && !uid_eq(cred->euid, tcred->uid) &&
  1277. !uid_eq(cred->uid, tcred->suid) && !uid_eq(cred->uid, tcred->uid) &&
  1278. !capable(CAP_SYS_NICE)) {
  1279. rcu_read_unlock();
  1280. err = -EPERM;
  1281. goto out;
  1282. }
  1283. rcu_read_unlock();
  1284. err = security_task_movememory(task);
  1285. if (err)
  1286. goto out;
  1287. task_nodes = cpuset_mems_allowed(task);
  1288. mm = get_task_mm(task);
  1289. put_task_struct(task);
  1290. if (!mm)
  1291. return -EINVAL;
  1292. if (nodes)
  1293. err = do_pages_move(mm, task_nodes, nr_pages, pages,
  1294. nodes, status, flags);
  1295. else
  1296. err = do_pages_stat(mm, nr_pages, pages, status);
  1297. mmput(mm);
  1298. return err;
  1299. out:
  1300. put_task_struct(task);
  1301. return err;
  1302. }
  1303. /*
  1304. * Call migration functions in the vma_ops that may prepare
  1305. * memory in a vm for migration. migration functions may perform
  1306. * the migration for vmas that do not have an underlying page struct.
  1307. */
  1308. int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
  1309. const nodemask_t *from, unsigned long flags)
  1310. {
  1311. struct vm_area_struct *vma;
  1312. int err = 0;
  1313. for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
  1314. if (vma->vm_ops && vma->vm_ops->migrate) {
  1315. err = vma->vm_ops->migrate(vma, to, from, flags);
  1316. if (err)
  1317. break;
  1318. }
  1319. }
  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_exact_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. * If a node is rate-limited then PTE NUMA updates are also rate-limited. However
  1365. * as it is faults that reset the window, pte updates will happen unconditionally
  1366. * if there has not been a fault since @pteupdate_interval_millisecs after the
  1367. * throttle window closed.
  1368. */
  1369. static unsigned int migrate_interval_millisecs __read_mostly = 100;
  1370. static unsigned int pteupdate_interval_millisecs __read_mostly = 1000;
  1371. static unsigned int ratelimit_pages __read_mostly = 128 << (20 - PAGE_SHIFT);
  1372. /* Returns true if NUMA migration is currently rate limited */
  1373. bool migrate_ratelimited(int node)
  1374. {
  1375. pg_data_t *pgdat = NODE_DATA(node);
  1376. if (time_after(jiffies, pgdat->numabalancing_migrate_next_window +
  1377. msecs_to_jiffies(pteupdate_interval_millisecs)))
  1378. return false;
  1379. if (pgdat->numabalancing_migrate_nr_pages < ratelimit_pages)
  1380. return false;
  1381. return true;
  1382. }
  1383. /* Returns true if the node is migrate rate-limited after the update */
  1384. static bool numamigrate_update_ratelimit(pg_data_t *pgdat,
  1385. unsigned long nr_pages)
  1386. {
  1387. /*
  1388. * Rate-limit the amount of data that is being migrated to a node.
  1389. * Optimal placement is no good if the memory bus is saturated and
  1390. * all the time is being spent migrating!
  1391. */
  1392. if (time_after(jiffies, pgdat->numabalancing_migrate_next_window)) {
  1393. spin_lock(&pgdat->numabalancing_migrate_lock);
  1394. pgdat->numabalancing_migrate_nr_pages = 0;
  1395. pgdat->numabalancing_migrate_next_window = jiffies +
  1396. msecs_to_jiffies(migrate_interval_millisecs);
  1397. spin_unlock(&pgdat->numabalancing_migrate_lock);
  1398. }
  1399. if (pgdat->numabalancing_migrate_nr_pages > ratelimit_pages) {
  1400. trace_mm_numa_migrate_ratelimit(current, pgdat->node_id,
  1401. nr_pages);
  1402. return true;
  1403. }
  1404. /*
  1405. * This is an unlocked non-atomic update so errors are possible.
  1406. * The consequences are failing to migrate when we potentiall should
  1407. * have which is not severe enough to warrant locking. If it is ever
  1408. * a problem, it can be converted to a per-cpu counter.
  1409. */
  1410. pgdat->numabalancing_migrate_nr_pages += nr_pages;
  1411. return false;
  1412. }
  1413. static int numamigrate_isolate_page(pg_data_t *pgdat, struct page *page)
  1414. {
  1415. int page_lru;
  1416. VM_BUG_ON_PAGE(compound_order(page) && !PageTransHuge(page), page);
  1417. /* Avoid migrating to a node that is nearly full */
  1418. if (!migrate_balanced_pgdat(pgdat, 1UL << compound_order(page)))
  1419. return 0;
  1420. if (isolate_lru_page(page))
  1421. return 0;
  1422. /*
  1423. * migrate_misplaced_transhuge_page() skips page migration's usual
  1424. * check on page_count(), so we must do it here, now that the page
  1425. * has been isolated: a GUP pin, or any other pin, prevents migration.
  1426. * The expected page count is 3: 1 for page's mapcount and 1 for the
  1427. * caller's pin and 1 for the reference taken by isolate_lru_page().
  1428. */
  1429. if (PageTransHuge(page) && page_count(page) != 3) {
  1430. putback_lru_page(page);
  1431. return 0;
  1432. }
  1433. page_lru = page_is_file_cache(page);
  1434. mod_zone_page_state(page_zone(page), NR_ISOLATED_ANON + page_lru,
  1435. hpage_nr_pages(page));
  1436. /*
  1437. * Isolating the page has taken another reference, so the
  1438. * caller's reference can be safely dropped without the page
  1439. * disappearing underneath us during migration.
  1440. */
  1441. put_page(page);
  1442. return 1;
  1443. }
  1444. bool pmd_trans_migrating(pmd_t pmd)
  1445. {
  1446. struct page *page = pmd_page(pmd);
  1447. return PageLocked(page);
  1448. }
  1449. void wait_migrate_huge_page(struct anon_vma *anon_vma, pmd_t *pmd)
  1450. {
  1451. struct page *page = pmd_page(*pmd);
  1452. wait_on_page_locked(page);
  1453. }
  1454. /*
  1455. * Attempt to migrate a misplaced page to the specified destination
  1456. * node. Caller is expected to have an elevated reference count on
  1457. * the page that will be dropped by this function before returning.
  1458. */
  1459. int migrate_misplaced_page(struct page *page, struct vm_area_struct *vma,
  1460. int node)
  1461. {
  1462. pg_data_t *pgdat = NODE_DATA(node);
  1463. int isolated;
  1464. int nr_remaining;
  1465. LIST_HEAD(migratepages);
  1466. /*
  1467. * Don't migrate file pages that are mapped in multiple processes
  1468. * with execute permissions as they are probably shared libraries.
  1469. */
  1470. if (page_mapcount(page) != 1 && page_is_file_cache(page) &&
  1471. (vma->vm_flags & VM_EXEC))
  1472. goto out;
  1473. /*
  1474. * Rate-limit the amount of data that is being migrated to a node.
  1475. * Optimal placement is no good if the memory bus is saturated and
  1476. * all the time is being spent migrating!
  1477. */
  1478. if (numamigrate_update_ratelimit(pgdat, 1))
  1479. goto out;
  1480. isolated = numamigrate_isolate_page(pgdat, page);
  1481. if (!isolated)
  1482. goto out;
  1483. list_add(&page->lru, &migratepages);
  1484. nr_remaining = migrate_pages(&migratepages, alloc_misplaced_dst_page,
  1485. node, MIGRATE_ASYNC, MR_NUMA_MISPLACED);
  1486. if (nr_remaining) {
  1487. if (!list_empty(&migratepages)) {
  1488. list_del(&page->lru);
  1489. dec_zone_page_state(page, NR_ISOLATED_ANON +
  1490. page_is_file_cache(page));
  1491. putback_lru_page(page);
  1492. }
  1493. isolated = 0;
  1494. } else
  1495. count_vm_numa_event(NUMA_PAGE_MIGRATE);
  1496. BUG_ON(!list_empty(&migratepages));
  1497. return isolated;
  1498. out:
  1499. put_page(page);
  1500. return 0;
  1501. }
  1502. #endif /* CONFIG_NUMA_BALANCING */
  1503. #if defined(CONFIG_NUMA_BALANCING) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
  1504. /*
  1505. * Migrates a THP to a given target node. page must be locked and is unlocked
  1506. * before returning.
  1507. */
  1508. int migrate_misplaced_transhuge_page(struct mm_struct *mm,
  1509. struct vm_area_struct *vma,
  1510. pmd_t *pmd, pmd_t entry,
  1511. unsigned long address,
  1512. struct page *page, int node)
  1513. {
  1514. spinlock_t *ptl;
  1515. pg_data_t *pgdat = NODE_DATA(node);
  1516. int isolated = 0;
  1517. struct page *new_page = NULL;
  1518. struct mem_cgroup *memcg = NULL;
  1519. int page_lru = page_is_file_cache(page);
  1520. unsigned long mmun_start = address & HPAGE_PMD_MASK;
  1521. unsigned long mmun_end = mmun_start + HPAGE_PMD_SIZE;
  1522. pmd_t orig_entry;
  1523. /*
  1524. * Rate-limit the amount of data that is being migrated to a node.
  1525. * Optimal placement is no good if the memory bus is saturated and
  1526. * all the time is being spent migrating!
  1527. */
  1528. if (numamigrate_update_ratelimit(pgdat, HPAGE_PMD_NR))
  1529. goto out_dropref;
  1530. new_page = alloc_pages_node(node,
  1531. (GFP_TRANSHUGE | __GFP_THISNODE) & ~__GFP_WAIT,
  1532. HPAGE_PMD_ORDER);
  1533. if (!new_page)
  1534. goto out_fail;
  1535. isolated = numamigrate_isolate_page(pgdat, page);
  1536. if (!isolated) {
  1537. put_page(new_page);
  1538. goto out_fail;
  1539. }
  1540. if (mm_tlb_flush_pending(mm))
  1541. flush_tlb_range(vma, mmun_start, mmun_end);
  1542. /* Prepare a page as a migration target */
  1543. __set_page_locked(new_page);
  1544. SetPageSwapBacked(new_page);
  1545. /* anon mapping, we can simply copy page->mapping to the new page: */
  1546. new_page->mapping = page->mapping;
  1547. new_page->index = page->index;
  1548. migrate_page_copy(new_page, page);
  1549. WARN_ON(PageLRU(new_page));
  1550. /* Recheck the target PMD */
  1551. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  1552. ptl = pmd_lock(mm, pmd);
  1553. if (unlikely(!pmd_same(*pmd, entry) || page_count(page) != 2)) {
  1554. fail_putback:
  1555. spin_unlock(ptl);
  1556. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  1557. /* Reverse changes made by migrate_page_copy() */
  1558. if (TestClearPageActive(new_page))
  1559. SetPageActive(page);
  1560. if (TestClearPageUnevictable(new_page))
  1561. SetPageUnevictable(page);
  1562. mlock_migrate_page(page, new_page);
  1563. unlock_page(new_page);
  1564. put_page(new_page); /* Free it */
  1565. /* Retake the callers reference and putback on LRU */
  1566. get_page(page);
  1567. putback_lru_page(page);
  1568. mod_zone_page_state(page_zone(page),
  1569. NR_ISOLATED_ANON + page_lru, -HPAGE_PMD_NR);
  1570. goto out_unlock;
  1571. }
  1572. /*
  1573. * Traditional migration needs to prepare the memcg charge
  1574. * transaction early to prevent the old page from being
  1575. * uncharged when installing migration entries. Here we can
  1576. * save the potential rollback and start the charge transfer
  1577. * only when migration is already known to end successfully.
  1578. */
  1579. mem_cgroup_prepare_migration(page, new_page, &memcg);
  1580. orig_entry = *pmd;
  1581. entry = mk_pmd(new_page, vma->vm_page_prot);
  1582. entry = pmd_mkhuge(entry);
  1583. entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
  1584. /*
  1585. * Clear the old entry under pagetable lock and establish the new PTE.
  1586. * Any parallel GUP will either observe the old page blocking on the
  1587. * page lock, block on the page table lock or observe the new page.
  1588. * The SetPageUptodate on the new page and page_add_new_anon_rmap
  1589. * guarantee the copy is visible before the pagetable update.
  1590. */
  1591. flush_cache_range(vma, mmun_start, mmun_end);
  1592. page_add_new_anon_rmap(new_page, vma, mmun_start);
  1593. pmdp_clear_flush(vma, mmun_start, pmd);
  1594. set_pmd_at(mm, mmun_start, pmd, entry);
  1595. flush_tlb_range(vma, mmun_start, mmun_end);
  1596. update_mmu_cache_pmd(vma, address, &entry);
  1597. if (page_count(page) != 2) {
  1598. set_pmd_at(mm, mmun_start, pmd, orig_entry);
  1599. flush_tlb_range(vma, mmun_start, mmun_end);
  1600. update_mmu_cache_pmd(vma, address, &entry);
  1601. page_remove_rmap(new_page);
  1602. goto fail_putback;
  1603. }
  1604. page_remove_rmap(page);
  1605. /*
  1606. * Finish the charge transaction under the page table lock to
  1607. * prevent split_huge_page() from dividing up the charge
  1608. * before it's fully transferred to the new page.
  1609. */
  1610. mem_cgroup_end_migration(memcg, page, new_page, true);
  1611. spin_unlock(ptl);
  1612. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  1613. unlock_page(new_page);
  1614. unlock_page(page);
  1615. put_page(page); /* Drop the rmap reference */
  1616. put_page(page); /* Drop the LRU isolation reference */
  1617. count_vm_events(PGMIGRATE_SUCCESS, HPAGE_PMD_NR);
  1618. count_vm_numa_events(NUMA_PAGE_MIGRATE, HPAGE_PMD_NR);
  1619. mod_zone_page_state(page_zone(page),
  1620. NR_ISOLATED_ANON + page_lru,
  1621. -HPAGE_PMD_NR);
  1622. return isolated;
  1623. out_fail:
  1624. count_vm_events(PGMIGRATE_FAIL, HPAGE_PMD_NR);
  1625. out_dropref:
  1626. ptl = pmd_lock(mm, pmd);
  1627. if (pmd_same(*pmd, entry)) {
  1628. entry = pmd_mknonnuma(entry);
  1629. set_pmd_at(mm, mmun_start, pmd, entry);
  1630. update_mmu_cache_pmd(vma, address, &entry);
  1631. }
  1632. spin_unlock(ptl);
  1633. out_unlock:
  1634. unlock_page(page);
  1635. put_page(page);
  1636. return 0;
  1637. }
  1638. #endif /* CONFIG_NUMA_BALANCING */
  1639. #endif /* CONFIG_NUMA */