migrate.c 26 KB

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