migrate.c 22 KB

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