migrate.c 21 KB

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