migrate.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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. * This is a corner case handling.
  543. * When a new swap-cache is read into, it is linked to LRU
  544. * and treated as swapcache but has no rmap yet.
  545. * Calling try_to_unmap() against a page->mapping==NULL page is
  546. * BUG. So handle it here.
  547. */
  548. if (!page->mapping)
  549. goto rcu_unlock;
  550. /* Establish migration ptes or remove ptes */
  551. try_to_unmap(page, 1);
  552. if (!page_mapped(page))
  553. rc = move_to_new_page(newpage, page);
  554. if (rc)
  555. remove_migration_ptes(page, page);
  556. rcu_unlock:
  557. if (rcu_locked)
  558. rcu_read_unlock();
  559. unlock:
  560. unlock_page(page);
  561. if (rc != -EAGAIN) {
  562. /*
  563. * A page that has been migrated has all references
  564. * removed and will be freed. A page that has not been
  565. * migrated will have kepts its references and be
  566. * restored.
  567. */
  568. list_del(&page->lru);
  569. move_to_lru(page);
  570. }
  571. move_newpage:
  572. /*
  573. * Move the new page to the LRU. If migration was not successful
  574. * then this will free the page.
  575. */
  576. move_to_lru(newpage);
  577. if (result) {
  578. if (rc)
  579. *result = rc;
  580. else
  581. *result = page_to_nid(newpage);
  582. }
  583. return rc;
  584. }
  585. /*
  586. * migrate_pages
  587. *
  588. * The function takes one list of pages to migrate and a function
  589. * that determines from the page to be migrated and the private data
  590. * the target of the move and allocates the page.
  591. *
  592. * The function returns after 10 attempts or if no pages
  593. * are movable anymore because to has become empty
  594. * or no retryable pages exist anymore. All pages will be
  595. * returned to the LRU or freed.
  596. *
  597. * Return: Number of pages not migrated or error code.
  598. */
  599. int migrate_pages(struct list_head *from,
  600. new_page_t get_new_page, unsigned long private)
  601. {
  602. int retry = 1;
  603. int nr_failed = 0;
  604. int pass = 0;
  605. struct page *page;
  606. struct page *page2;
  607. int swapwrite = current->flags & PF_SWAPWRITE;
  608. int rc;
  609. if (!swapwrite)
  610. current->flags |= PF_SWAPWRITE;
  611. for(pass = 0; pass < 10 && retry; pass++) {
  612. retry = 0;
  613. list_for_each_entry_safe(page, page2, from, lru) {
  614. cond_resched();
  615. rc = unmap_and_move(get_new_page, private,
  616. page, pass > 2);
  617. switch(rc) {
  618. case -ENOMEM:
  619. goto out;
  620. case -EAGAIN:
  621. retry++;
  622. break;
  623. case 0:
  624. break;
  625. default:
  626. /* Permanent failure */
  627. nr_failed++;
  628. break;
  629. }
  630. }
  631. }
  632. rc = 0;
  633. out:
  634. if (!swapwrite)
  635. current->flags &= ~PF_SWAPWRITE;
  636. putback_lru_pages(from);
  637. if (rc)
  638. return rc;
  639. return nr_failed + retry;
  640. }
  641. #ifdef CONFIG_NUMA
  642. /*
  643. * Move a list of individual pages
  644. */
  645. struct page_to_node {
  646. unsigned long addr;
  647. struct page *page;
  648. int node;
  649. int status;
  650. };
  651. static struct page *new_page_node(struct page *p, unsigned long private,
  652. int **result)
  653. {
  654. struct page_to_node *pm = (struct page_to_node *)private;
  655. while (pm->node != MAX_NUMNODES && pm->page != p)
  656. pm++;
  657. if (pm->node == MAX_NUMNODES)
  658. return NULL;
  659. *result = &pm->status;
  660. return alloc_pages_node(pm->node,
  661. GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
  662. }
  663. /*
  664. * Move a set of pages as indicated in the pm array. The addr
  665. * field must be set to the virtual address of the page to be moved
  666. * and the node number must contain a valid target node.
  667. */
  668. static int do_move_pages(struct mm_struct *mm, struct page_to_node *pm,
  669. int migrate_all)
  670. {
  671. int err;
  672. struct page_to_node *pp;
  673. LIST_HEAD(pagelist);
  674. down_read(&mm->mmap_sem);
  675. /*
  676. * Build a list of pages to migrate
  677. */
  678. migrate_prep();
  679. for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
  680. struct vm_area_struct *vma;
  681. struct page *page;
  682. /*
  683. * A valid page pointer that will not match any of the
  684. * pages that will be moved.
  685. */
  686. pp->page = ZERO_PAGE(0);
  687. err = -EFAULT;
  688. vma = find_vma(mm, pp->addr);
  689. if (!vma || !vma_migratable(vma))
  690. goto set_status;
  691. page = follow_page(vma, pp->addr, FOLL_GET);
  692. err = -ENOENT;
  693. if (!page)
  694. goto set_status;
  695. if (PageReserved(page)) /* Check for zero page */
  696. goto put_and_set;
  697. pp->page = page;
  698. err = page_to_nid(page);
  699. if (err == pp->node)
  700. /*
  701. * Node already in the right place
  702. */
  703. goto put_and_set;
  704. err = -EACCES;
  705. if (page_mapcount(page) > 1 &&
  706. !migrate_all)
  707. goto put_and_set;
  708. err = isolate_lru_page(page, &pagelist);
  709. put_and_set:
  710. /*
  711. * Either remove the duplicate refcount from
  712. * isolate_lru_page() or drop the page ref if it was
  713. * not isolated.
  714. */
  715. put_page(page);
  716. set_status:
  717. pp->status = err;
  718. }
  719. if (!list_empty(&pagelist))
  720. err = migrate_pages(&pagelist, new_page_node,
  721. (unsigned long)pm);
  722. else
  723. err = -ENOENT;
  724. up_read(&mm->mmap_sem);
  725. return err;
  726. }
  727. /*
  728. * Determine the nodes of a list of pages. The addr in the pm array
  729. * must have been set to the virtual address of which we want to determine
  730. * the node number.
  731. */
  732. static int do_pages_stat(struct mm_struct *mm, struct page_to_node *pm)
  733. {
  734. down_read(&mm->mmap_sem);
  735. for ( ; pm->node != MAX_NUMNODES; pm++) {
  736. struct vm_area_struct *vma;
  737. struct page *page;
  738. int err;
  739. err = -EFAULT;
  740. vma = find_vma(mm, pm->addr);
  741. if (!vma)
  742. goto set_status;
  743. page = follow_page(vma, pm->addr, 0);
  744. err = -ENOENT;
  745. /* Use PageReserved to check for zero page */
  746. if (!page || PageReserved(page))
  747. goto set_status;
  748. err = page_to_nid(page);
  749. set_status:
  750. pm->status = err;
  751. }
  752. up_read(&mm->mmap_sem);
  753. return 0;
  754. }
  755. /*
  756. * Move a list of pages in the address space of the currently executing
  757. * process.
  758. */
  759. asmlinkage long sys_move_pages(pid_t pid, unsigned long nr_pages,
  760. const void __user * __user *pages,
  761. const int __user *nodes,
  762. int __user *status, int flags)
  763. {
  764. int err = 0;
  765. int i;
  766. struct task_struct *task;
  767. nodemask_t task_nodes;
  768. struct mm_struct *mm;
  769. struct page_to_node *pm = NULL;
  770. /* Check flags */
  771. if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
  772. return -EINVAL;
  773. if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
  774. return -EPERM;
  775. /* Find the mm_struct */
  776. read_lock(&tasklist_lock);
  777. task = pid ? find_task_by_vpid(pid) : current;
  778. if (!task) {
  779. read_unlock(&tasklist_lock);
  780. return -ESRCH;
  781. }
  782. mm = get_task_mm(task);
  783. read_unlock(&tasklist_lock);
  784. if (!mm)
  785. return -EINVAL;
  786. /*
  787. * Check if this process has the right to modify the specified
  788. * process. The right exists if the process has administrative
  789. * capabilities, superuser privileges or the same
  790. * userid as the target process.
  791. */
  792. if ((current->euid != task->suid) && (current->euid != task->uid) &&
  793. (current->uid != task->suid) && (current->uid != task->uid) &&
  794. !capable(CAP_SYS_NICE)) {
  795. err = -EPERM;
  796. goto out2;
  797. }
  798. err = security_task_movememory(task);
  799. if (err)
  800. goto out2;
  801. task_nodes = cpuset_mems_allowed(task);
  802. /* Limit nr_pages so that the multiplication may not overflow */
  803. if (nr_pages >= ULONG_MAX / sizeof(struct page_to_node) - 1) {
  804. err = -E2BIG;
  805. goto out2;
  806. }
  807. pm = vmalloc((nr_pages + 1) * sizeof(struct page_to_node));
  808. if (!pm) {
  809. err = -ENOMEM;
  810. goto out2;
  811. }
  812. /*
  813. * Get parameters from user space and initialize the pm
  814. * array. Return various errors if the user did something wrong.
  815. */
  816. for (i = 0; i < nr_pages; i++) {
  817. const void __user *p;
  818. err = -EFAULT;
  819. if (get_user(p, pages + i))
  820. goto out;
  821. pm[i].addr = (unsigned long)p;
  822. if (nodes) {
  823. int node;
  824. if (get_user(node, nodes + i))
  825. goto out;
  826. err = -ENODEV;
  827. if (!node_state(node, N_HIGH_MEMORY))
  828. goto out;
  829. err = -EACCES;
  830. if (!node_isset(node, task_nodes))
  831. goto out;
  832. pm[i].node = node;
  833. } else
  834. pm[i].node = 0; /* anything to not match MAX_NUMNODES */
  835. }
  836. /* End marker */
  837. pm[nr_pages].node = MAX_NUMNODES;
  838. if (nodes)
  839. err = do_move_pages(mm, pm, flags & MPOL_MF_MOVE_ALL);
  840. else
  841. err = do_pages_stat(mm, pm);
  842. if (err >= 0)
  843. /* Return status information */
  844. for (i = 0; i < nr_pages; i++)
  845. if (put_user(pm[i].status, status + i))
  846. err = -EFAULT;
  847. out:
  848. vfree(pm);
  849. out2:
  850. mmput(mm);
  851. return err;
  852. }
  853. #endif
  854. /*
  855. * Call migration functions in the vma_ops that may prepare
  856. * memory in a vm for migration. migration functions may perform
  857. * the migration for vmas that do not have an underlying page struct.
  858. */
  859. int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
  860. const nodemask_t *from, unsigned long flags)
  861. {
  862. struct vm_area_struct *vma;
  863. int err = 0;
  864. for(vma = mm->mmap; vma->vm_next && !err; vma = vma->vm_next) {
  865. if (vma->vm_ops && vma->vm_ops->migrate) {
  866. err = vma->vm_ops->migrate(vma, to, from, flags);
  867. if (err)
  868. break;
  869. }
  870. }
  871. return err;
  872. }