migrate.c 49 KB

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