migrate.c 25 KB

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