migrate.c 23 KB

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