migrate.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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. /*
  349. * Migration function for pages with buffers. This function can only be used
  350. * if the underlying filesystem guarantees that no other references to "page"
  351. * exist.
  352. */
  353. int buffer_migrate_page(struct address_space *mapping,
  354. struct page *newpage, struct page *page)
  355. {
  356. struct buffer_head *bh, *head;
  357. int rc;
  358. if (!page_has_buffers(page))
  359. return migrate_page(mapping, newpage, page);
  360. head = page_buffers(page);
  361. rc = migrate_page_move_mapping(mapping, newpage, page);
  362. if (rc)
  363. return rc;
  364. bh = head;
  365. do {
  366. get_bh(bh);
  367. lock_buffer(bh);
  368. bh = bh->b_this_page;
  369. } while (bh != head);
  370. ClearPagePrivate(page);
  371. set_page_private(newpage, page_private(page));
  372. set_page_private(page, 0);
  373. put_page(page);
  374. get_page(newpage);
  375. bh = head;
  376. do {
  377. set_bh_page(bh, newpage, bh_offset(bh));
  378. bh = bh->b_this_page;
  379. } while (bh != head);
  380. SetPagePrivate(newpage);
  381. migrate_page_copy(newpage, page);
  382. bh = head;
  383. do {
  384. unlock_buffer(bh);
  385. put_bh(bh);
  386. bh = bh->b_this_page;
  387. } while (bh != head);
  388. return 0;
  389. }
  390. EXPORT_SYMBOL(buffer_migrate_page);
  391. /*
  392. * Writeback a page to clean the dirty state
  393. */
  394. static int writeout(struct address_space *mapping, struct page *page)
  395. {
  396. struct writeback_control wbc = {
  397. .sync_mode = WB_SYNC_NONE,
  398. .nr_to_write = 1,
  399. .range_start = 0,
  400. .range_end = LLONG_MAX,
  401. .nonblocking = 1,
  402. .for_reclaim = 1
  403. };
  404. int rc;
  405. if (!mapping->a_ops->writepage)
  406. /* No write method for the address space */
  407. return -EINVAL;
  408. if (!clear_page_dirty_for_io(page))
  409. /* Someone else already triggered a write */
  410. return -EAGAIN;
  411. /*
  412. * A dirty page may imply that the underlying filesystem has
  413. * the page on some queue. So the page must be clean for
  414. * migration. Writeout may mean we loose the lock and the
  415. * page state is no longer what we checked for earlier.
  416. * At this point we know that the migration attempt cannot
  417. * be successful.
  418. */
  419. remove_migration_ptes(page, page);
  420. rc = mapping->a_ops->writepage(page, &wbc);
  421. if (rc < 0)
  422. /* I/O Error writing */
  423. return -EIO;
  424. if (rc != AOP_WRITEPAGE_ACTIVATE)
  425. /* unlocked. Relock */
  426. lock_page(page);
  427. return -EAGAIN;
  428. }
  429. /*
  430. * Default handling if a filesystem does not provide a migration function.
  431. */
  432. static int fallback_migrate_page(struct address_space *mapping,
  433. struct page *newpage, struct page *page)
  434. {
  435. if (PageDirty(page))
  436. return writeout(mapping, page);
  437. /*
  438. * Buffers may be managed in a filesystem specific way.
  439. * We must have no buffers or drop them.
  440. */
  441. if (page_has_buffers(page) &&
  442. !try_to_release_page(page, GFP_KERNEL))
  443. return -EAGAIN;
  444. return migrate_page(mapping, newpage, page);
  445. }
  446. /*
  447. * Move a page to a newly allocated page
  448. * The page is locked and all ptes have been successfully removed.
  449. *
  450. * The new page will have replaced the old page if this function
  451. * is successful.
  452. */
  453. static int move_to_new_page(struct page *newpage, struct page *page)
  454. {
  455. struct address_space *mapping;
  456. int rc;
  457. /*
  458. * Block others from accessing the page when we get around to
  459. * establishing additional references. We are the only one
  460. * holding a reference to the new page at this point.
  461. */
  462. if (TestSetPageLocked(newpage))
  463. BUG();
  464. /* Prepare mapping for the new page.*/
  465. newpage->index = page->index;
  466. newpage->mapping = page->mapping;
  467. mapping = page_mapping(page);
  468. if (!mapping)
  469. rc = migrate_page(mapping, newpage, page);
  470. else if (mapping->a_ops->migratepage)
  471. /*
  472. * Most pages have a mapping and most filesystems
  473. * should provide a migration function. Anonymous
  474. * pages are part of swap space which also has its
  475. * own migration function. This is the most common
  476. * path for page migration.
  477. */
  478. rc = mapping->a_ops->migratepage(mapping,
  479. newpage, page);
  480. else
  481. rc = fallback_migrate_page(mapping, newpage, page);
  482. if (!rc)
  483. remove_migration_ptes(page, newpage);
  484. else
  485. newpage->mapping = NULL;
  486. unlock_page(newpage);
  487. return rc;
  488. }
  489. /*
  490. * Obtain the lock on page, remove all ptes and migrate the page
  491. * to the newly allocated page in newpage.
  492. */
  493. static int unmap_and_move(new_page_t get_new_page, unsigned long private,
  494. struct page *page, int force)
  495. {
  496. int rc = 0;
  497. int *result = NULL;
  498. struct page *newpage = get_new_page(page, private, &result);
  499. if (!newpage)
  500. return -ENOMEM;
  501. if (page_count(page) == 1)
  502. /* page was freed from under us. So we are done. */
  503. goto move_newpage;
  504. rc = -EAGAIN;
  505. if (TestSetPageLocked(page)) {
  506. if (!force)
  507. goto move_newpage;
  508. lock_page(page);
  509. }
  510. if (PageWriteback(page)) {
  511. if (!force)
  512. goto unlock;
  513. wait_on_page_writeback(page);
  514. }
  515. /*
  516. * Establish migration ptes or remove ptes
  517. */
  518. try_to_unmap(page, 1);
  519. if (!page_mapped(page))
  520. rc = move_to_new_page(newpage, page);
  521. if (rc)
  522. remove_migration_ptes(page, page);
  523. unlock:
  524. unlock_page(page);
  525. if (rc != -EAGAIN) {
  526. /*
  527. * A page that has been migrated has all references
  528. * removed and will be freed. A page that has not been
  529. * migrated will have kepts its references and be
  530. * restored.
  531. */
  532. list_del(&page->lru);
  533. move_to_lru(page);
  534. }
  535. move_newpage:
  536. /*
  537. * Move the new page to the LRU. If migration was not successful
  538. * then this will free the page.
  539. */
  540. move_to_lru(newpage);
  541. if (result) {
  542. if (rc)
  543. *result = rc;
  544. else
  545. *result = page_to_nid(newpage);
  546. }
  547. return rc;
  548. }
  549. /*
  550. * migrate_pages
  551. *
  552. * The function takes one list of pages to migrate and a function
  553. * that determines from the page to be migrated and the private data
  554. * the target of the move and allocates the page.
  555. *
  556. * The function returns after 10 attempts or if no pages
  557. * are movable anymore because to has become empty
  558. * or no retryable pages exist anymore. All pages will be
  559. * retruned to the LRU or freed.
  560. *
  561. * Return: Number of pages not migrated or error code.
  562. */
  563. int migrate_pages(struct list_head *from,
  564. new_page_t get_new_page, unsigned long private)
  565. {
  566. int retry = 1;
  567. int nr_failed = 0;
  568. int pass = 0;
  569. struct page *page;
  570. struct page *page2;
  571. int swapwrite = current->flags & PF_SWAPWRITE;
  572. int rc;
  573. if (!swapwrite)
  574. current->flags |= PF_SWAPWRITE;
  575. for(pass = 0; pass < 10 && retry; pass++) {
  576. retry = 0;
  577. list_for_each_entry_safe(page, page2, from, lru) {
  578. cond_resched();
  579. rc = unmap_and_move(get_new_page, private,
  580. page, pass > 2);
  581. switch(rc) {
  582. case -ENOMEM:
  583. goto out;
  584. case -EAGAIN:
  585. retry++;
  586. break;
  587. case 0:
  588. break;
  589. default:
  590. /* Permanent failure */
  591. nr_failed++;
  592. break;
  593. }
  594. }
  595. }
  596. rc = 0;
  597. out:
  598. if (!swapwrite)
  599. current->flags &= ~PF_SWAPWRITE;
  600. putback_lru_pages(from);
  601. if (rc)
  602. return rc;
  603. return nr_failed + retry;
  604. }
  605. #ifdef CONFIG_NUMA
  606. /*
  607. * Move a list of individual pages
  608. */
  609. struct page_to_node {
  610. unsigned long addr;
  611. struct page *page;
  612. int node;
  613. int status;
  614. };
  615. static struct page *new_page_node(struct page *p, unsigned long private,
  616. int **result)
  617. {
  618. struct page_to_node *pm = (struct page_to_node *)private;
  619. while (pm->node != MAX_NUMNODES && pm->page != p)
  620. pm++;
  621. if (pm->node == MAX_NUMNODES)
  622. return NULL;
  623. *result = &pm->status;
  624. return alloc_pages_node(pm->node, GFP_HIGHUSER | GFP_THISNODE, 0);
  625. }
  626. /*
  627. * Move a set of pages as indicated in the pm array. The addr
  628. * field must be set to the virtual address of the page to be moved
  629. * and the node number must contain a valid target node.
  630. */
  631. static int do_move_pages(struct mm_struct *mm, struct page_to_node *pm,
  632. int migrate_all)
  633. {
  634. int err;
  635. struct page_to_node *pp;
  636. LIST_HEAD(pagelist);
  637. down_read(&mm->mmap_sem);
  638. /*
  639. * Build a list of pages to migrate
  640. */
  641. migrate_prep();
  642. for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
  643. struct vm_area_struct *vma;
  644. struct page *page;
  645. /*
  646. * A valid page pointer that will not match any of the
  647. * pages that will be moved.
  648. */
  649. pp->page = ZERO_PAGE(0);
  650. err = -EFAULT;
  651. vma = find_vma(mm, pp->addr);
  652. if (!vma)
  653. goto set_status;
  654. page = follow_page(vma, pp->addr, FOLL_GET);
  655. err = -ENOENT;
  656. if (!page)
  657. goto set_status;
  658. if (PageReserved(page)) /* Check for zero page */
  659. goto put_and_set;
  660. pp->page = page;
  661. err = page_to_nid(page);
  662. if (err == pp->node)
  663. /*
  664. * Node already in the right place
  665. */
  666. goto put_and_set;
  667. err = -EACCES;
  668. if (page_mapcount(page) > 1 &&
  669. !migrate_all)
  670. goto put_and_set;
  671. err = isolate_lru_page(page, &pagelist);
  672. put_and_set:
  673. /*
  674. * Either remove the duplicate refcount from
  675. * isolate_lru_page() or drop the page ref if it was
  676. * not isolated.
  677. */
  678. put_page(page);
  679. set_status:
  680. pp->status = err;
  681. }
  682. if (!list_empty(&pagelist))
  683. err = migrate_pages(&pagelist, new_page_node,
  684. (unsigned long)pm);
  685. else
  686. err = -ENOENT;
  687. up_read(&mm->mmap_sem);
  688. return err;
  689. }
  690. /*
  691. * Determine the nodes of a list of pages. The addr in the pm array
  692. * must have been set to the virtual address of which we want to determine
  693. * the node number.
  694. */
  695. static int do_pages_stat(struct mm_struct *mm, struct page_to_node *pm)
  696. {
  697. down_read(&mm->mmap_sem);
  698. for ( ; pm->node != MAX_NUMNODES; pm++) {
  699. struct vm_area_struct *vma;
  700. struct page *page;
  701. int err;
  702. err = -EFAULT;
  703. vma = find_vma(mm, pm->addr);
  704. if (!vma)
  705. goto set_status;
  706. page = follow_page(vma, pm->addr, 0);
  707. err = -ENOENT;
  708. /* Use PageReserved to check for zero page */
  709. if (!page || PageReserved(page))
  710. goto set_status;
  711. err = page_to_nid(page);
  712. set_status:
  713. pm->status = err;
  714. }
  715. up_read(&mm->mmap_sem);
  716. return 0;
  717. }
  718. /*
  719. * Move a list of pages in the address space of the currently executing
  720. * process.
  721. */
  722. asmlinkage long sys_move_pages(pid_t pid, unsigned long nr_pages,
  723. const void __user * __user *pages,
  724. const int __user *nodes,
  725. int __user *status, int flags)
  726. {
  727. int err = 0;
  728. int i;
  729. struct task_struct *task;
  730. nodemask_t task_nodes;
  731. struct mm_struct *mm;
  732. struct page_to_node *pm = NULL;
  733. /* Check flags */
  734. if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
  735. return -EINVAL;
  736. if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
  737. return -EPERM;
  738. /* Find the mm_struct */
  739. read_lock(&tasklist_lock);
  740. task = pid ? find_task_by_pid(pid) : current;
  741. if (!task) {
  742. read_unlock(&tasklist_lock);
  743. return -ESRCH;
  744. }
  745. mm = get_task_mm(task);
  746. read_unlock(&tasklist_lock);
  747. if (!mm)
  748. return -EINVAL;
  749. /*
  750. * Check if this process has the right to modify the specified
  751. * process. The right exists if the process has administrative
  752. * capabilities, superuser privileges or the same
  753. * userid as the target process.
  754. */
  755. if ((current->euid != task->suid) && (current->euid != task->uid) &&
  756. (current->uid != task->suid) && (current->uid != task->uid) &&
  757. !capable(CAP_SYS_NICE)) {
  758. err = -EPERM;
  759. goto out2;
  760. }
  761. err = security_task_movememory(task);
  762. if (err)
  763. goto out2;
  764. task_nodes = cpuset_mems_allowed(task);
  765. /* Limit nr_pages so that the multiplication may not overflow */
  766. if (nr_pages >= ULONG_MAX / sizeof(struct page_to_node) - 1) {
  767. err = -E2BIG;
  768. goto out2;
  769. }
  770. pm = vmalloc((nr_pages + 1) * sizeof(struct page_to_node));
  771. if (!pm) {
  772. err = -ENOMEM;
  773. goto out2;
  774. }
  775. /*
  776. * Get parameters from user space and initialize the pm
  777. * array. Return various errors if the user did something wrong.
  778. */
  779. for (i = 0; i < nr_pages; i++) {
  780. const void *p;
  781. err = -EFAULT;
  782. if (get_user(p, pages + i))
  783. goto out;
  784. pm[i].addr = (unsigned long)p;
  785. if (nodes) {
  786. int node;
  787. if (get_user(node, nodes + i))
  788. goto out;
  789. err = -ENODEV;
  790. if (!node_online(node))
  791. goto out;
  792. err = -EACCES;
  793. if (!node_isset(node, task_nodes))
  794. goto out;
  795. pm[i].node = node;
  796. }
  797. }
  798. /* End marker */
  799. pm[nr_pages].node = MAX_NUMNODES;
  800. if (nodes)
  801. err = do_move_pages(mm, pm, flags & MPOL_MF_MOVE_ALL);
  802. else
  803. err = do_pages_stat(mm, pm);
  804. if (err >= 0)
  805. /* Return status information */
  806. for (i = 0; i < nr_pages; i++)
  807. if (put_user(pm[i].status, status + i))
  808. err = -EFAULT;
  809. out:
  810. vfree(pm);
  811. out2:
  812. mmput(mm);
  813. return err;
  814. }
  815. #endif
  816. /*
  817. * Call migration functions in the vma_ops that may prepare
  818. * memory in a vm for migration. migration functions may perform
  819. * the migration for vmas that do not have an underlying page struct.
  820. */
  821. int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
  822. const nodemask_t *from, unsigned long flags)
  823. {
  824. struct vm_area_struct *vma;
  825. int err = 0;
  826. for(vma = mm->mmap; vma->vm_next && !err; vma = vma->vm_next) {
  827. if (vma->vm_ops && vma->vm_ops->migrate) {
  828. err = vma->vm_ops->migrate(vma, to, from, flags);
  829. if (err)
  830. break;
  831. }
  832. }
  833. return err;
  834. }