migrate.c 23 KB

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