migrate.c 25 KB

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