migrate.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  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/module.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/rmap.h>
  24. #include <linux/topology.h>
  25. #include <linux/cpu.h>
  26. #include <linux/cpuset.h>
  27. #include <linux/writeback.h>
  28. #include <linux/mempolicy.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/security.h>
  31. #include <linux/memcontrol.h>
  32. #include <linux/syscalls.h>
  33. #include "internal.h"
  34. #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
  35. /*
  36. * migrate_prep() needs to be called before we start compiling a list of pages
  37. * to be migrated using isolate_lru_page().
  38. */
  39. int migrate_prep(void)
  40. {
  41. /*
  42. * Clear the LRU lists so pages can be isolated.
  43. * Note that pages may be moved off the LRU after we have
  44. * drained them. Those pages will fail to migrate like other
  45. * pages that may be busy.
  46. */
  47. lru_add_drain_all();
  48. return 0;
  49. }
  50. static inline void move_to_lru(struct page *page)
  51. {
  52. if (PageActive(page)) {
  53. /*
  54. * lru_cache_add_active checks that
  55. * the PG_active bit is off.
  56. */
  57. ClearPageActive(page);
  58. lru_cache_add_active(page);
  59. } else {
  60. lru_cache_add(page);
  61. }
  62. put_page(page);
  63. }
  64. /*
  65. * Add isolated pages on the list back to the LRU.
  66. *
  67. * returns the number of pages put back.
  68. */
  69. int putback_lru_pages(struct list_head *l)
  70. {
  71. struct page *page;
  72. struct page *page2;
  73. int count = 0;
  74. list_for_each_entry_safe(page, page2, l, lru) {
  75. list_del(&page->lru);
  76. move_to_lru(page);
  77. count++;
  78. }
  79. return count;
  80. }
  81. /*
  82. * Restore a potential migration pte to a working pte entry
  83. */
  84. static void remove_migration_pte(struct vm_area_struct *vma,
  85. struct page *old, struct page *new)
  86. {
  87. struct mm_struct *mm = vma->vm_mm;
  88. swp_entry_t entry;
  89. pgd_t *pgd;
  90. pud_t *pud;
  91. pmd_t *pmd;
  92. pte_t *ptep, pte;
  93. spinlock_t *ptl;
  94. unsigned long addr = page_address_in_vma(new, vma);
  95. if (addr == -EFAULT)
  96. return;
  97. pgd = pgd_offset(mm, addr);
  98. if (!pgd_present(*pgd))
  99. return;
  100. pud = pud_offset(pgd, addr);
  101. if (!pud_present(*pud))
  102. return;
  103. pmd = pmd_offset(pud, addr);
  104. if (!pmd_present(*pmd))
  105. return;
  106. ptep = pte_offset_map(pmd, addr);
  107. if (!is_swap_pte(*ptep)) {
  108. pte_unmap(ptep);
  109. return;
  110. }
  111. ptl = pte_lockptr(mm, pmd);
  112. spin_lock(ptl);
  113. pte = *ptep;
  114. if (!is_swap_pte(pte))
  115. goto out;
  116. entry = pte_to_swp_entry(pte);
  117. if (!is_migration_entry(entry) || migration_entry_to_page(entry) != old)
  118. goto out;
  119. /*
  120. * Yes, ignore the return value from a GFP_ATOMIC mem_cgroup_charge.
  121. * Failure is not an option here: we're now expected to remove every
  122. * migration pte, and will cause crashes otherwise. Normally this
  123. * is not an issue: mem_cgroup_prepare_migration bumped up the old
  124. * page_cgroup count for safety, that's now attached to the new page,
  125. * so this charge should just be another incrementation of the count,
  126. * to keep in balance with rmap.c's mem_cgroup_uncharging. But if
  127. * there's been a force_empty, those reference counts may no longer
  128. * be reliable, and this charge can actually fail: oh well, we don't
  129. * make the situation any worse by proceeding as if it had succeeded.
  130. */
  131. mem_cgroup_charge(new, mm, GFP_ATOMIC);
  132. get_page(new);
  133. pte = pte_mkold(mk_pte(new, vma->vm_page_prot));
  134. if (is_write_migration_entry(entry))
  135. pte = pte_mkwrite(pte);
  136. flush_cache_page(vma, addr, pte_pfn(pte));
  137. set_pte_at(mm, addr, ptep, pte);
  138. if (PageAnon(new))
  139. page_add_anon_rmap(new, vma, addr);
  140. else
  141. page_add_file_rmap(new);
  142. /* No need to invalidate - it was non-present before */
  143. update_mmu_cache(vma, addr, pte);
  144. out:
  145. pte_unmap_unlock(ptep, ptl);
  146. }
  147. /*
  148. * Note that remove_file_migration_ptes will only work on regular mappings,
  149. * Nonlinear mappings do not use migration entries.
  150. */
  151. static void remove_file_migration_ptes(struct page *old, struct page *new)
  152. {
  153. struct vm_area_struct *vma;
  154. struct address_space *mapping = page_mapping(new);
  155. struct prio_tree_iter iter;
  156. pgoff_t pgoff = new->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  157. if (!mapping)
  158. return;
  159. spin_lock(&mapping->i_mmap_lock);
  160. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff)
  161. remove_migration_pte(vma, old, new);
  162. spin_unlock(&mapping->i_mmap_lock);
  163. }
  164. /*
  165. * Must hold mmap_sem lock on at least one of the vmas containing
  166. * the page so that the anon_vma cannot vanish.
  167. */
  168. static void remove_anon_migration_ptes(struct page *old, struct page *new)
  169. {
  170. struct anon_vma *anon_vma;
  171. struct vm_area_struct *vma;
  172. unsigned long mapping;
  173. mapping = (unsigned long)new->mapping;
  174. if (!mapping || (mapping & PAGE_MAPPING_ANON) == 0)
  175. return;
  176. /*
  177. * We hold the mmap_sem lock. So no need to call page_lock_anon_vma.
  178. */
  179. anon_vma = (struct anon_vma *) (mapping - PAGE_MAPPING_ANON);
  180. spin_lock(&anon_vma->lock);
  181. list_for_each_entry(vma, &anon_vma->head, anon_vma_node)
  182. remove_migration_pte(vma, old, new);
  183. spin_unlock(&anon_vma->lock);
  184. }
  185. /*
  186. * Get rid of all migration entries and replace them by
  187. * references to the indicated page.
  188. */
  189. static void remove_migration_ptes(struct page *old, struct page *new)
  190. {
  191. if (PageAnon(new))
  192. remove_anon_migration_ptes(old, new);
  193. else
  194. remove_file_migration_ptes(old, new);
  195. }
  196. /*
  197. * Something used the pte of a page under migration. We need to
  198. * get to the page and wait until migration is finished.
  199. * When we return from this function the fault will be retried.
  200. *
  201. * This function is called from do_swap_page().
  202. */
  203. void migration_entry_wait(struct mm_struct *mm, pmd_t *pmd,
  204. unsigned long address)
  205. {
  206. pte_t *ptep, pte;
  207. spinlock_t *ptl;
  208. swp_entry_t entry;
  209. struct page *page;
  210. ptep = pte_offset_map_lock(mm, pmd, address, &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. /*
  235. * Replace the page in the mapping.
  236. *
  237. * The number of remaining references must be:
  238. * 1 for anonymous pages without a mapping
  239. * 2 for pages with a mapping
  240. * 3 for pages with a mapping and PagePrivate set.
  241. */
  242. static int migrate_page_move_mapping(struct address_space *mapping,
  243. struct page *newpage, struct page *page)
  244. {
  245. int expected_count;
  246. void **pslot;
  247. if (!mapping) {
  248. /* Anonymous page without mapping */
  249. if (page_count(page) != 1)
  250. return -EAGAIN;
  251. return 0;
  252. }
  253. spin_lock_irq(&mapping->tree_lock);
  254. pslot = radix_tree_lookup_slot(&mapping->page_tree,
  255. page_index(page));
  256. expected_count = 2 + !!PagePrivate(page);
  257. if (page_count(page) != expected_count ||
  258. (struct page *)radix_tree_deref_slot(pslot) != page) {
  259. spin_unlock_irq(&mapping->tree_lock);
  260. return -EAGAIN;
  261. }
  262. if (!page_freeze_refs(page, expected_count)) {
  263. spin_unlock_irq(&mapping->tree_lock);
  264. return -EAGAIN;
  265. }
  266. /*
  267. * Now we know that no one else is looking at the page.
  268. */
  269. get_page(newpage); /* add cache reference */
  270. #ifdef CONFIG_SWAP
  271. if (PageSwapCache(page)) {
  272. SetPageSwapCache(newpage);
  273. set_page_private(newpage, page_private(page));
  274. }
  275. #endif
  276. radix_tree_replace_slot(pslot, newpage);
  277. page_unfreeze_refs(page, expected_count);
  278. /*
  279. * Drop cache reference from old page.
  280. * We know this isn't the last reference.
  281. */
  282. __put_page(page);
  283. /*
  284. * If moved to a different zone then also account
  285. * the page for that zone. Other VM counters will be
  286. * taken care of when we establish references to the
  287. * new page and drop references to the old page.
  288. *
  289. * Note that anonymous pages are accounted for
  290. * via NR_FILE_PAGES and NR_ANON_PAGES if they
  291. * are mapped to swap space.
  292. */
  293. __dec_zone_page_state(page, NR_FILE_PAGES);
  294. __inc_zone_page_state(newpage, NR_FILE_PAGES);
  295. spin_unlock_irq(&mapping->tree_lock);
  296. if (!PageSwapCache(newpage))
  297. mem_cgroup_uncharge_cache_page(page);
  298. return 0;
  299. }
  300. /*
  301. * Copy the page to its new location
  302. */
  303. static void migrate_page_copy(struct page *newpage, struct page *page)
  304. {
  305. copy_highpage(newpage, page);
  306. if (PageError(page))
  307. SetPageError(newpage);
  308. if (PageReferenced(page))
  309. SetPageReferenced(newpage);
  310. if (PageUptodate(page))
  311. SetPageUptodate(newpage);
  312. if (PageActive(page))
  313. SetPageActive(newpage);
  314. if (PageChecked(page))
  315. SetPageChecked(newpage);
  316. if (PageMappedToDisk(page))
  317. SetPageMappedToDisk(newpage);
  318. if (PageDirty(page)) {
  319. clear_page_dirty_for_io(page);
  320. /*
  321. * Want to mark the page and the radix tree as dirty, and
  322. * redo the accounting that clear_page_dirty_for_io undid,
  323. * but we can't use set_page_dirty because that function
  324. * is actually a signal that all of the page has become dirty.
  325. * Wheras only part of our page may be dirty.
  326. */
  327. __set_page_dirty_nobuffers(newpage);
  328. }
  329. #ifdef CONFIG_SWAP
  330. ClearPageSwapCache(page);
  331. #endif
  332. ClearPageActive(page);
  333. ClearPagePrivate(page);
  334. set_page_private(page, 0);
  335. page->mapping = NULL;
  336. /*
  337. * If any waiters have accumulated on the new page then
  338. * wake them up.
  339. */
  340. if (PageWriteback(newpage))
  341. end_page_writeback(newpage);
  342. }
  343. /************************************************************
  344. * Migration functions
  345. ***********************************************************/
  346. /* Always fail migration. Used for mappings that are not movable */
  347. int fail_migrate_page(struct address_space *mapping,
  348. struct page *newpage, struct page *page)
  349. {
  350. return -EIO;
  351. }
  352. EXPORT_SYMBOL(fail_migrate_page);
  353. /*
  354. * Common logic to directly migrate a single page suitable for
  355. * pages that do not use PagePrivate.
  356. *
  357. * Pages are locked upon entry and exit.
  358. */
  359. int migrate_page(struct address_space *mapping,
  360. struct page *newpage, struct page *page)
  361. {
  362. int rc;
  363. BUG_ON(PageWriteback(page)); /* Writeback must be complete */
  364. rc = migrate_page_move_mapping(mapping, newpage, page);
  365. if (rc)
  366. return rc;
  367. migrate_page_copy(newpage, page);
  368. return 0;
  369. }
  370. EXPORT_SYMBOL(migrate_page);
  371. #ifdef CONFIG_BLOCK
  372. /*
  373. * Migration function for pages with buffers. This function can only be used
  374. * if the underlying filesystem guarantees that no other references to "page"
  375. * exist.
  376. */
  377. int buffer_migrate_page(struct address_space *mapping,
  378. struct page *newpage, struct page *page)
  379. {
  380. struct buffer_head *bh, *head;
  381. int rc;
  382. if (!page_has_buffers(page))
  383. return migrate_page(mapping, newpage, page);
  384. head = page_buffers(page);
  385. rc = migrate_page_move_mapping(mapping, newpage, page);
  386. if (rc)
  387. return rc;
  388. bh = head;
  389. do {
  390. get_bh(bh);
  391. lock_buffer(bh);
  392. bh = bh->b_this_page;
  393. } while (bh != head);
  394. ClearPagePrivate(page);
  395. set_page_private(newpage, page_private(page));
  396. set_page_private(page, 0);
  397. put_page(page);
  398. get_page(newpage);
  399. bh = head;
  400. do {
  401. set_bh_page(bh, newpage, bh_offset(bh));
  402. bh = bh->b_this_page;
  403. } while (bh != head);
  404. SetPagePrivate(newpage);
  405. migrate_page_copy(newpage, page);
  406. bh = head;
  407. do {
  408. unlock_buffer(bh);
  409. put_bh(bh);
  410. bh = bh->b_this_page;
  411. } while (bh != head);
  412. return 0;
  413. }
  414. EXPORT_SYMBOL(buffer_migrate_page);
  415. #endif
  416. /*
  417. * Writeback a page to clean the dirty state
  418. */
  419. static int writeout(struct address_space *mapping, struct page *page)
  420. {
  421. struct writeback_control wbc = {
  422. .sync_mode = WB_SYNC_NONE,
  423. .nr_to_write = 1,
  424. .range_start = 0,
  425. .range_end = LLONG_MAX,
  426. .nonblocking = 1,
  427. .for_reclaim = 1
  428. };
  429. int rc;
  430. if (!mapping->a_ops->writepage)
  431. /* No write method for the address space */
  432. return -EINVAL;
  433. if (!clear_page_dirty_for_io(page))
  434. /* Someone else already triggered a write */
  435. return -EAGAIN;
  436. /*
  437. * A dirty page may imply that the underlying filesystem has
  438. * the page on some queue. So the page must be clean for
  439. * migration. Writeout may mean we loose the lock and the
  440. * page state is no longer what we checked for earlier.
  441. * At this point we know that the migration attempt cannot
  442. * be successful.
  443. */
  444. remove_migration_ptes(page, page);
  445. rc = mapping->a_ops->writepage(page, &wbc);
  446. if (rc < 0)
  447. /* I/O Error writing */
  448. return -EIO;
  449. if (rc != AOP_WRITEPAGE_ACTIVATE)
  450. /* unlocked. Relock */
  451. lock_page(page);
  452. return -EAGAIN;
  453. }
  454. /*
  455. * Default handling if a filesystem does not provide a migration function.
  456. */
  457. static int fallback_migrate_page(struct address_space *mapping,
  458. struct page *newpage, struct page *page)
  459. {
  460. if (PageDirty(page))
  461. return writeout(mapping, page);
  462. /*
  463. * Buffers may be managed in a filesystem specific way.
  464. * We must have no buffers or drop them.
  465. */
  466. if (PagePrivate(page) &&
  467. !try_to_release_page(page, GFP_KERNEL))
  468. return -EAGAIN;
  469. return migrate_page(mapping, newpage, page);
  470. }
  471. /*
  472. * Move a page to a newly allocated page
  473. * The page is locked and all ptes have been successfully removed.
  474. *
  475. * The new page will have replaced the old page if this function
  476. * is successful.
  477. */
  478. static int move_to_new_page(struct page *newpage, struct page *page)
  479. {
  480. struct address_space *mapping;
  481. int rc;
  482. /*
  483. * Block others from accessing the page when we get around to
  484. * establishing additional references. We are the only one
  485. * holding a reference to the new page at this point.
  486. */
  487. if (!trylock_page(newpage))
  488. BUG();
  489. /* Prepare mapping for the new page.*/
  490. newpage->index = page->index;
  491. newpage->mapping = page->mapping;
  492. mapping = page_mapping(page);
  493. if (!mapping)
  494. rc = migrate_page(mapping, newpage, page);
  495. else if (mapping->a_ops->migratepage)
  496. /*
  497. * Most pages have a mapping and most filesystems
  498. * should provide a migration function. Anonymous
  499. * pages are part of swap space which also has its
  500. * own migration function. This is the most common
  501. * path for page migration.
  502. */
  503. rc = mapping->a_ops->migratepage(mapping,
  504. newpage, page);
  505. else
  506. rc = fallback_migrate_page(mapping, newpage, page);
  507. if (!rc) {
  508. remove_migration_ptes(page, newpage);
  509. } else
  510. newpage->mapping = NULL;
  511. unlock_page(newpage);
  512. return rc;
  513. }
  514. /*
  515. * Obtain the lock on page, remove all ptes and migrate the page
  516. * to the newly allocated page in newpage.
  517. */
  518. static int unmap_and_move(new_page_t get_new_page, unsigned long private,
  519. struct page *page, int force)
  520. {
  521. int rc = 0;
  522. int *result = NULL;
  523. struct page *newpage = get_new_page(page, private, &result);
  524. int rcu_locked = 0;
  525. int charge = 0;
  526. if (!newpage)
  527. return -ENOMEM;
  528. if (page_count(page) == 1)
  529. /* page was freed from under us. So we are done. */
  530. goto move_newpage;
  531. charge = mem_cgroup_prepare_migration(page, newpage);
  532. if (charge == -ENOMEM) {
  533. rc = -ENOMEM;
  534. goto move_newpage;
  535. }
  536. /* prepare cgroup just returns 0 or -ENOMEM */
  537. BUG_ON(charge);
  538. rc = -EAGAIN;
  539. if (!trylock_page(page)) {
  540. if (!force)
  541. goto move_newpage;
  542. lock_page(page);
  543. }
  544. if (PageWriteback(page)) {
  545. if (!force)
  546. goto unlock;
  547. wait_on_page_writeback(page);
  548. }
  549. /*
  550. * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
  551. * we cannot notice that anon_vma is freed while we migrates a page.
  552. * This rcu_read_lock() delays freeing anon_vma pointer until the end
  553. * of migration. File cache pages are no problem because of page_lock()
  554. * File Caches may use write_page() or lock_page() in migration, then,
  555. * just care Anon page here.
  556. */
  557. if (PageAnon(page)) {
  558. rcu_read_lock();
  559. rcu_locked = 1;
  560. }
  561. /*
  562. * Corner case handling:
  563. * 1. When a new swap-cache page is read into, it is added to the LRU
  564. * and treated as swapcache but it has no rmap yet.
  565. * Calling try_to_unmap() against a page->mapping==NULL page will
  566. * trigger a BUG. So handle it here.
  567. * 2. An orphaned page (see truncate_complete_page) might have
  568. * fs-private metadata. The page can be picked up due to memory
  569. * offlining. Everywhere else except page reclaim, the page is
  570. * invisible to the vm, so the page can not be migrated. So try to
  571. * free the metadata, so the page can be freed.
  572. */
  573. if (!page->mapping) {
  574. if (!PageAnon(page) && PagePrivate(page)) {
  575. /*
  576. * Go direct to try_to_free_buffers() here because
  577. * a) that's what try_to_release_page() would do anyway
  578. * b) we may be under rcu_read_lock() here, so we can't
  579. * use GFP_KERNEL which is what try_to_release_page()
  580. * needs to be effective.
  581. */
  582. try_to_free_buffers(page);
  583. }
  584. goto rcu_unlock;
  585. }
  586. /* Establish migration ptes or remove ptes */
  587. try_to_unmap(page, 1);
  588. if (!page_mapped(page))
  589. rc = move_to_new_page(newpage, page);
  590. if (rc)
  591. remove_migration_ptes(page, page);
  592. rcu_unlock:
  593. if (rcu_locked)
  594. rcu_read_unlock();
  595. unlock:
  596. unlock_page(page);
  597. if (rc != -EAGAIN) {
  598. /*
  599. * A page that has been migrated has all references
  600. * removed and will be freed. A page that has not been
  601. * migrated will have kepts its references and be
  602. * restored.
  603. */
  604. list_del(&page->lru);
  605. move_to_lru(page);
  606. }
  607. move_newpage:
  608. if (!charge)
  609. mem_cgroup_end_migration(newpage);
  610. /*
  611. * Move the new page to the LRU. If migration was not successful
  612. * then this will free the page.
  613. */
  614. move_to_lru(newpage);
  615. if (result) {
  616. if (rc)
  617. *result = rc;
  618. else
  619. *result = page_to_nid(newpage);
  620. }
  621. return rc;
  622. }
  623. /*
  624. * migrate_pages
  625. *
  626. * The function takes one list of pages to migrate and a function
  627. * that determines from the page to be migrated and the private data
  628. * the target of the move and allocates the page.
  629. *
  630. * The function returns after 10 attempts or if no pages
  631. * are movable anymore because to has become empty
  632. * or no retryable pages exist anymore. All pages will be
  633. * returned to the LRU or freed.
  634. *
  635. * Return: Number of pages not migrated or error code.
  636. */
  637. int migrate_pages(struct list_head *from,
  638. new_page_t get_new_page, unsigned long private)
  639. {
  640. int retry = 1;
  641. int nr_failed = 0;
  642. int pass = 0;
  643. struct page *page;
  644. struct page *page2;
  645. int swapwrite = current->flags & PF_SWAPWRITE;
  646. int rc;
  647. if (!swapwrite)
  648. current->flags |= PF_SWAPWRITE;
  649. for(pass = 0; pass < 10 && retry; pass++) {
  650. retry = 0;
  651. list_for_each_entry_safe(page, page2, from, lru) {
  652. cond_resched();
  653. rc = unmap_and_move(get_new_page, private,
  654. page, pass > 2);
  655. switch(rc) {
  656. case -ENOMEM:
  657. goto out;
  658. case -EAGAIN:
  659. retry++;
  660. break;
  661. case 0:
  662. break;
  663. default:
  664. /* Permanent failure */
  665. nr_failed++;
  666. break;
  667. }
  668. }
  669. }
  670. rc = 0;
  671. out:
  672. if (!swapwrite)
  673. current->flags &= ~PF_SWAPWRITE;
  674. putback_lru_pages(from);
  675. if (rc)
  676. return rc;
  677. return nr_failed + retry;
  678. }
  679. #ifdef CONFIG_NUMA
  680. /*
  681. * Move a list of individual pages
  682. */
  683. struct page_to_node {
  684. unsigned long addr;
  685. struct page *page;
  686. int node;
  687. int status;
  688. };
  689. static struct page *new_page_node(struct page *p, unsigned long private,
  690. int **result)
  691. {
  692. struct page_to_node *pm = (struct page_to_node *)private;
  693. while (pm->node != MAX_NUMNODES && pm->page != p)
  694. pm++;
  695. if (pm->node == MAX_NUMNODES)
  696. return NULL;
  697. *result = &pm->status;
  698. return alloc_pages_node(pm->node,
  699. GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
  700. }
  701. /*
  702. * Move a set of pages as indicated in the pm array. The addr
  703. * field must be set to the virtual address of the page to be moved
  704. * and the node number must contain a valid target node.
  705. */
  706. static int do_move_pages(struct mm_struct *mm, struct page_to_node *pm,
  707. int migrate_all)
  708. {
  709. int err;
  710. struct page_to_node *pp;
  711. LIST_HEAD(pagelist);
  712. down_read(&mm->mmap_sem);
  713. /*
  714. * Build a list of pages to migrate
  715. */
  716. migrate_prep();
  717. for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
  718. struct vm_area_struct *vma;
  719. struct page *page;
  720. /*
  721. * A valid page pointer that will not match any of the
  722. * pages that will be moved.
  723. */
  724. pp->page = ZERO_PAGE(0);
  725. err = -EFAULT;
  726. vma = find_vma(mm, pp->addr);
  727. if (!vma || !vma_migratable(vma))
  728. goto set_status;
  729. page = follow_page(vma, pp->addr, FOLL_GET);
  730. err = PTR_ERR(page);
  731. if (IS_ERR(page))
  732. goto set_status;
  733. err = -ENOENT;
  734. if (!page)
  735. goto set_status;
  736. if (PageReserved(page)) /* Check for zero page */
  737. goto put_and_set;
  738. pp->page = page;
  739. err = page_to_nid(page);
  740. if (err == pp->node)
  741. /*
  742. * Node already in the right place
  743. */
  744. goto put_and_set;
  745. err = -EACCES;
  746. if (page_mapcount(page) > 1 &&
  747. !migrate_all)
  748. goto put_and_set;
  749. err = isolate_lru_page(page);
  750. if (!err)
  751. list_add_tail(&page->lru, &pagelist);
  752. put_and_set:
  753. /*
  754. * Either remove the duplicate refcount from
  755. * isolate_lru_page() or drop the page ref if it was
  756. * not isolated.
  757. */
  758. put_page(page);
  759. set_status:
  760. pp->status = err;
  761. }
  762. if (!list_empty(&pagelist))
  763. err = migrate_pages(&pagelist, new_page_node,
  764. (unsigned long)pm);
  765. else
  766. err = -ENOENT;
  767. up_read(&mm->mmap_sem);
  768. return err;
  769. }
  770. /*
  771. * Determine the nodes of a list of pages. The addr in the pm array
  772. * must have been set to the virtual address of which we want to determine
  773. * the node number.
  774. */
  775. static int do_pages_stat(struct mm_struct *mm, struct page_to_node *pm)
  776. {
  777. down_read(&mm->mmap_sem);
  778. for ( ; pm->node != MAX_NUMNODES; pm++) {
  779. struct vm_area_struct *vma;
  780. struct page *page;
  781. int err;
  782. err = -EFAULT;
  783. vma = find_vma(mm, pm->addr);
  784. if (!vma)
  785. goto set_status;
  786. page = follow_page(vma, pm->addr, 0);
  787. err = PTR_ERR(page);
  788. if (IS_ERR(page))
  789. goto set_status;
  790. err = -ENOENT;
  791. /* Use PageReserved to check for zero page */
  792. if (!page || PageReserved(page))
  793. goto set_status;
  794. err = page_to_nid(page);
  795. set_status:
  796. pm->status = err;
  797. }
  798. up_read(&mm->mmap_sem);
  799. return 0;
  800. }
  801. /*
  802. * Move a list of pages in the address space of the currently executing
  803. * process.
  804. */
  805. asmlinkage long sys_move_pages(pid_t pid, unsigned long nr_pages,
  806. const void __user * __user *pages,
  807. const int __user *nodes,
  808. int __user *status, int flags)
  809. {
  810. int err = 0;
  811. int i;
  812. struct task_struct *task;
  813. nodemask_t task_nodes;
  814. struct mm_struct *mm;
  815. struct page_to_node *pm = NULL;
  816. /* Check flags */
  817. if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
  818. return -EINVAL;
  819. if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
  820. return -EPERM;
  821. /* Find the mm_struct */
  822. read_lock(&tasklist_lock);
  823. task = pid ? find_task_by_vpid(pid) : current;
  824. if (!task) {
  825. read_unlock(&tasklist_lock);
  826. return -ESRCH;
  827. }
  828. mm = get_task_mm(task);
  829. read_unlock(&tasklist_lock);
  830. if (!mm)
  831. return -EINVAL;
  832. /*
  833. * Check if this process has the right to modify the specified
  834. * process. The right exists if the process has administrative
  835. * capabilities, superuser privileges or the same
  836. * userid as the target process.
  837. */
  838. if ((current->euid != task->suid) && (current->euid != task->uid) &&
  839. (current->uid != task->suid) && (current->uid != task->uid) &&
  840. !capable(CAP_SYS_NICE)) {
  841. err = -EPERM;
  842. goto out2;
  843. }
  844. err = security_task_movememory(task);
  845. if (err)
  846. goto out2;
  847. task_nodes = cpuset_mems_allowed(task);
  848. /* Limit nr_pages so that the multiplication may not overflow */
  849. if (nr_pages >= ULONG_MAX / sizeof(struct page_to_node) - 1) {
  850. err = -E2BIG;
  851. goto out2;
  852. }
  853. pm = vmalloc((nr_pages + 1) * sizeof(struct page_to_node));
  854. if (!pm) {
  855. err = -ENOMEM;
  856. goto out2;
  857. }
  858. /*
  859. * Get parameters from user space and initialize the pm
  860. * array. Return various errors if the user did something wrong.
  861. */
  862. for (i = 0; i < nr_pages; i++) {
  863. const void __user *p;
  864. err = -EFAULT;
  865. if (get_user(p, pages + i))
  866. goto out;
  867. pm[i].addr = (unsigned long)p;
  868. if (nodes) {
  869. int node;
  870. if (get_user(node, nodes + i))
  871. goto out;
  872. err = -ENODEV;
  873. if (!node_state(node, N_HIGH_MEMORY))
  874. goto out;
  875. err = -EACCES;
  876. if (!node_isset(node, task_nodes))
  877. goto out;
  878. pm[i].node = node;
  879. } else
  880. pm[i].node = 0; /* anything to not match MAX_NUMNODES */
  881. }
  882. /* End marker */
  883. pm[nr_pages].node = MAX_NUMNODES;
  884. if (nodes)
  885. err = do_move_pages(mm, pm, flags & MPOL_MF_MOVE_ALL);
  886. else
  887. err = do_pages_stat(mm, pm);
  888. if (err >= 0)
  889. /* Return status information */
  890. for (i = 0; i < nr_pages; i++)
  891. if (put_user(pm[i].status, status + i))
  892. err = -EFAULT;
  893. out:
  894. vfree(pm);
  895. out2:
  896. mmput(mm);
  897. return err;
  898. }
  899. /*
  900. * Call migration functions in the vma_ops that may prepare
  901. * memory in a vm for migration. migration functions may perform
  902. * the migration for vmas that do not have an underlying page struct.
  903. */
  904. int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
  905. const nodemask_t *from, unsigned long flags)
  906. {
  907. struct vm_area_struct *vma;
  908. int err = 0;
  909. for(vma = mm->mmap; vma->vm_next && !err; vma = vma->vm_next) {
  910. if (vma->vm_ops && vma->vm_ops->migrate) {
  911. err = vma->vm_ops->migrate(vma, to, from, flags);
  912. if (err)
  913. break;
  914. }
  915. }
  916. return err;
  917. }
  918. #endif