migrate.c 25 KB

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