migrate.c 25 KB

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