migrate.c 25 KB

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