migrate.c 21 KB

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