migrate.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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. /*
  246. * Once radix-tree replacement of page migration started, page_count
  247. * *must* be zero. And, we don't want to call wait_on_page_locked()
  248. * against a page without get_page().
  249. * So, we use get_page_unless_zero(), here. Even failed, page fault
  250. * will occur again.
  251. */
  252. if (!get_page_unless_zero(page))
  253. goto out;
  254. pte_unmap_unlock(ptep, ptl);
  255. wait_on_page_locked(page);
  256. put_page(page);
  257. return;
  258. out:
  259. pte_unmap_unlock(ptep, ptl);
  260. }
  261. /*
  262. * Replace the page in the mapping.
  263. *
  264. * The number of remaining references must be:
  265. * 1 for anonymous pages without a mapping
  266. * 2 for pages with a mapping
  267. * 3 for pages with a mapping and PagePrivate set.
  268. */
  269. static int migrate_page_move_mapping(struct address_space *mapping,
  270. struct page *newpage, struct page *page)
  271. {
  272. int expected_count;
  273. void **pslot;
  274. if (!mapping) {
  275. /* Anonymous page without mapping */
  276. if (page_count(page) != 1)
  277. return -EAGAIN;
  278. return 0;
  279. }
  280. spin_lock_irq(&mapping->tree_lock);
  281. pslot = radix_tree_lookup_slot(&mapping->page_tree,
  282. page_index(page));
  283. expected_count = 2 + !!PagePrivate(page);
  284. if (page_count(page) != expected_count ||
  285. (struct page *)radix_tree_deref_slot(pslot) != page) {
  286. spin_unlock_irq(&mapping->tree_lock);
  287. return -EAGAIN;
  288. }
  289. if (!page_freeze_refs(page, expected_count)) {
  290. spin_unlock_irq(&mapping->tree_lock);
  291. return -EAGAIN;
  292. }
  293. /*
  294. * Now we know that no one else is looking at the page.
  295. */
  296. get_page(newpage); /* add cache reference */
  297. #ifdef CONFIG_SWAP
  298. if (PageSwapCache(page)) {
  299. SetPageSwapCache(newpage);
  300. set_page_private(newpage, page_private(page));
  301. }
  302. #endif
  303. radix_tree_replace_slot(pslot, newpage);
  304. page_unfreeze_refs(page, expected_count);
  305. /*
  306. * Drop cache reference from old page.
  307. * We know this isn't the last reference.
  308. */
  309. __put_page(page);
  310. /*
  311. * If moved to a different zone then also account
  312. * the page for that zone. Other VM counters will be
  313. * taken care of when we establish references to the
  314. * new page and drop references to the old page.
  315. *
  316. * Note that anonymous pages are accounted for
  317. * via NR_FILE_PAGES and NR_ANON_PAGES if they
  318. * are mapped to swap space.
  319. */
  320. __dec_zone_page_state(page, NR_FILE_PAGES);
  321. __inc_zone_page_state(newpage, NR_FILE_PAGES);
  322. spin_unlock_irq(&mapping->tree_lock);
  323. if (!PageSwapCache(newpage))
  324. mem_cgroup_uncharge_cache_page(page);
  325. return 0;
  326. }
  327. /*
  328. * Copy the page to its new location
  329. */
  330. static void migrate_page_copy(struct page *newpage, struct page *page)
  331. {
  332. copy_highpage(newpage, page);
  333. if (PageError(page))
  334. SetPageError(newpage);
  335. if (PageReferenced(page))
  336. SetPageReferenced(newpage);
  337. if (PageUptodate(page))
  338. SetPageUptodate(newpage);
  339. if (PageActive(page))
  340. SetPageActive(newpage);
  341. if (PageChecked(page))
  342. SetPageChecked(newpage);
  343. if (PageMappedToDisk(page))
  344. SetPageMappedToDisk(newpage);
  345. if (PageDirty(page)) {
  346. clear_page_dirty_for_io(page);
  347. /*
  348. * Want to mark the page and the radix tree as dirty, and
  349. * redo the accounting that clear_page_dirty_for_io undid,
  350. * but we can't use set_page_dirty because that function
  351. * is actually a signal that all of the page has become dirty.
  352. * Wheras only part of our page may be dirty.
  353. */
  354. __set_page_dirty_nobuffers(newpage);
  355. }
  356. #ifdef CONFIG_SWAP
  357. ClearPageSwapCache(page);
  358. #endif
  359. ClearPageActive(page);
  360. ClearPagePrivate(page);
  361. set_page_private(page, 0);
  362. page->mapping = NULL;
  363. /*
  364. * If any waiters have accumulated on the new page then
  365. * wake them up.
  366. */
  367. if (PageWriteback(newpage))
  368. end_page_writeback(newpage);
  369. }
  370. /************************************************************
  371. * Migration functions
  372. ***********************************************************/
  373. /* Always fail migration. Used for mappings that are not movable */
  374. int fail_migrate_page(struct address_space *mapping,
  375. struct page *newpage, struct page *page)
  376. {
  377. return -EIO;
  378. }
  379. EXPORT_SYMBOL(fail_migrate_page);
  380. /*
  381. * Common logic to directly migrate a single page suitable for
  382. * pages that do not use PagePrivate.
  383. *
  384. * Pages are locked upon entry and exit.
  385. */
  386. int migrate_page(struct address_space *mapping,
  387. struct page *newpage, struct page *page)
  388. {
  389. int rc;
  390. BUG_ON(PageWriteback(page)); /* Writeback must be complete */
  391. rc = migrate_page_move_mapping(mapping, newpage, page);
  392. if (rc)
  393. return rc;
  394. migrate_page_copy(newpage, page);
  395. return 0;
  396. }
  397. EXPORT_SYMBOL(migrate_page);
  398. #ifdef CONFIG_BLOCK
  399. /*
  400. * Migration function for pages with buffers. This function can only be used
  401. * if the underlying filesystem guarantees that no other references to "page"
  402. * exist.
  403. */
  404. int buffer_migrate_page(struct address_space *mapping,
  405. struct page *newpage, struct page *page)
  406. {
  407. struct buffer_head *bh, *head;
  408. int rc;
  409. if (!page_has_buffers(page))
  410. return migrate_page(mapping, newpage, page);
  411. head = page_buffers(page);
  412. rc = migrate_page_move_mapping(mapping, newpage, page);
  413. if (rc)
  414. return rc;
  415. bh = head;
  416. do {
  417. get_bh(bh);
  418. lock_buffer(bh);
  419. bh = bh->b_this_page;
  420. } while (bh != head);
  421. ClearPagePrivate(page);
  422. set_page_private(newpage, page_private(page));
  423. set_page_private(page, 0);
  424. put_page(page);
  425. get_page(newpage);
  426. bh = head;
  427. do {
  428. set_bh_page(bh, newpage, bh_offset(bh));
  429. bh = bh->b_this_page;
  430. } while (bh != head);
  431. SetPagePrivate(newpage);
  432. migrate_page_copy(newpage, page);
  433. bh = head;
  434. do {
  435. unlock_buffer(bh);
  436. put_bh(bh);
  437. bh = bh->b_this_page;
  438. } while (bh != head);
  439. return 0;
  440. }
  441. EXPORT_SYMBOL(buffer_migrate_page);
  442. #endif
  443. /*
  444. * Writeback a page to clean the dirty state
  445. */
  446. static int writeout(struct address_space *mapping, struct page *page)
  447. {
  448. struct writeback_control wbc = {
  449. .sync_mode = WB_SYNC_NONE,
  450. .nr_to_write = 1,
  451. .range_start = 0,
  452. .range_end = LLONG_MAX,
  453. .nonblocking = 1,
  454. .for_reclaim = 1
  455. };
  456. int rc;
  457. if (!mapping->a_ops->writepage)
  458. /* No write method for the address space */
  459. return -EINVAL;
  460. if (!clear_page_dirty_for_io(page))
  461. /* Someone else already triggered a write */
  462. return -EAGAIN;
  463. /*
  464. * A dirty page may imply that the underlying filesystem has
  465. * the page on some queue. So the page must be clean for
  466. * migration. Writeout may mean we loose the lock and the
  467. * page state is no longer what we checked for earlier.
  468. * At this point we know that the migration attempt cannot
  469. * be successful.
  470. */
  471. remove_migration_ptes(page, page);
  472. rc = mapping->a_ops->writepage(page, &wbc);
  473. if (rc < 0)
  474. /* I/O Error writing */
  475. return -EIO;
  476. if (rc != AOP_WRITEPAGE_ACTIVATE)
  477. /* unlocked. Relock */
  478. lock_page(page);
  479. return -EAGAIN;
  480. }
  481. /*
  482. * Default handling if a filesystem does not provide a migration function.
  483. */
  484. static int fallback_migrate_page(struct address_space *mapping,
  485. struct page *newpage, struct page *page)
  486. {
  487. if (PageDirty(page))
  488. return writeout(mapping, page);
  489. /*
  490. * Buffers may be managed in a filesystem specific way.
  491. * We must have no buffers or drop them.
  492. */
  493. if (PagePrivate(page) &&
  494. !try_to_release_page(page, GFP_KERNEL))
  495. return -EAGAIN;
  496. return migrate_page(mapping, newpage, page);
  497. }
  498. /*
  499. * Move a page to a newly allocated page
  500. * The page is locked and all ptes have been successfully removed.
  501. *
  502. * The new page will have replaced the old page if this function
  503. * is successful.
  504. */
  505. static int move_to_new_page(struct page *newpage, struct page *page)
  506. {
  507. struct address_space *mapping;
  508. int rc;
  509. /*
  510. * Block others from accessing the page when we get around to
  511. * establishing additional references. We are the only one
  512. * holding a reference to the new page at this point.
  513. */
  514. if (!trylock_page(newpage))
  515. BUG();
  516. /* Prepare mapping for the new page.*/
  517. newpage->index = page->index;
  518. newpage->mapping = page->mapping;
  519. mapping = page_mapping(page);
  520. if (!mapping)
  521. rc = migrate_page(mapping, newpage, page);
  522. else if (mapping->a_ops->migratepage)
  523. /*
  524. * Most pages have a mapping and most filesystems
  525. * should provide a migration function. Anonymous
  526. * pages are part of swap space which also has its
  527. * own migration function. This is the most common
  528. * path for page migration.
  529. */
  530. rc = mapping->a_ops->migratepage(mapping,
  531. newpage, page);
  532. else
  533. rc = fallback_migrate_page(mapping, newpage, page);
  534. if (!rc) {
  535. remove_migration_ptes(page, newpage);
  536. } else
  537. newpage->mapping = NULL;
  538. unlock_page(newpage);
  539. return rc;
  540. }
  541. /*
  542. * Obtain the lock on page, remove all ptes and migrate the page
  543. * to the newly allocated page in newpage.
  544. */
  545. static int unmap_and_move(new_page_t get_new_page, unsigned long private,
  546. struct page *page, int force)
  547. {
  548. int rc = 0;
  549. int *result = NULL;
  550. struct page *newpage = get_new_page(page, private, &result);
  551. int rcu_locked = 0;
  552. int charge = 0;
  553. if (!newpage)
  554. return -ENOMEM;
  555. if (page_count(page) == 1)
  556. /* page was freed from under us. So we are done. */
  557. goto move_newpage;
  558. charge = mem_cgroup_prepare_migration(page, newpage);
  559. if (charge == -ENOMEM) {
  560. rc = -ENOMEM;
  561. goto move_newpage;
  562. }
  563. /* prepare cgroup just returns 0 or -ENOMEM */
  564. BUG_ON(charge);
  565. rc = -EAGAIN;
  566. if (!trylock_page(page)) {
  567. if (!force)
  568. goto move_newpage;
  569. lock_page(page);
  570. }
  571. if (PageWriteback(page)) {
  572. if (!force)
  573. goto unlock;
  574. wait_on_page_writeback(page);
  575. }
  576. /*
  577. * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
  578. * we cannot notice that anon_vma is freed while we migrates a page.
  579. * This rcu_read_lock() delays freeing anon_vma pointer until the end
  580. * of migration. File cache pages are no problem because of page_lock()
  581. * File Caches may use write_page() or lock_page() in migration, then,
  582. * just care Anon page here.
  583. */
  584. if (PageAnon(page)) {
  585. rcu_read_lock();
  586. rcu_locked = 1;
  587. }
  588. /*
  589. * Corner case handling:
  590. * 1. When a new swap-cache page is read into, it is added to the LRU
  591. * and treated as swapcache but it has no rmap yet.
  592. * Calling try_to_unmap() against a page->mapping==NULL page will
  593. * trigger a BUG. So handle it here.
  594. * 2. An orphaned page (see truncate_complete_page) might have
  595. * fs-private metadata. The page can be picked up due to memory
  596. * offlining. Everywhere else except page reclaim, the page is
  597. * invisible to the vm, so the page can not be migrated. So try to
  598. * free the metadata, so the page can be freed.
  599. */
  600. if (!page->mapping) {
  601. if (!PageAnon(page) && PagePrivate(page)) {
  602. /*
  603. * Go direct to try_to_free_buffers() here because
  604. * a) that's what try_to_release_page() would do anyway
  605. * b) we may be under rcu_read_lock() here, so we can't
  606. * use GFP_KERNEL which is what try_to_release_page()
  607. * needs to be effective.
  608. */
  609. try_to_free_buffers(page);
  610. }
  611. goto rcu_unlock;
  612. }
  613. /* Establish migration ptes or remove ptes */
  614. try_to_unmap(page, 1);
  615. if (!page_mapped(page))
  616. rc = move_to_new_page(newpage, page);
  617. if (rc)
  618. remove_migration_ptes(page, page);
  619. rcu_unlock:
  620. if (rcu_locked)
  621. rcu_read_unlock();
  622. unlock:
  623. unlock_page(page);
  624. if (rc != -EAGAIN) {
  625. /*
  626. * A page that has been migrated has all references
  627. * removed and will be freed. A page that has not been
  628. * migrated will have kepts its references and be
  629. * restored.
  630. */
  631. list_del(&page->lru);
  632. move_to_lru(page);
  633. }
  634. move_newpage:
  635. if (!charge)
  636. mem_cgroup_end_migration(newpage);
  637. /*
  638. * Move the new page to the LRU. If migration was not successful
  639. * then this will free the page.
  640. */
  641. move_to_lru(newpage);
  642. if (result) {
  643. if (rc)
  644. *result = rc;
  645. else
  646. *result = page_to_nid(newpage);
  647. }
  648. return rc;
  649. }
  650. /*
  651. * migrate_pages
  652. *
  653. * The function takes one list of pages to migrate and a function
  654. * that determines from the page to be migrated and the private data
  655. * the target of the move and allocates the page.
  656. *
  657. * The function returns after 10 attempts or if no pages
  658. * are movable anymore because to has become empty
  659. * or no retryable pages exist anymore. All pages will be
  660. * returned to the LRU or freed.
  661. *
  662. * Return: Number of pages not migrated or error code.
  663. */
  664. int migrate_pages(struct list_head *from,
  665. new_page_t get_new_page, unsigned long private)
  666. {
  667. int retry = 1;
  668. int nr_failed = 0;
  669. int pass = 0;
  670. struct page *page;
  671. struct page *page2;
  672. int swapwrite = current->flags & PF_SWAPWRITE;
  673. int rc;
  674. if (!swapwrite)
  675. current->flags |= PF_SWAPWRITE;
  676. for(pass = 0; pass < 10 && retry; pass++) {
  677. retry = 0;
  678. list_for_each_entry_safe(page, page2, from, lru) {
  679. cond_resched();
  680. rc = unmap_and_move(get_new_page, private,
  681. page, pass > 2);
  682. switch(rc) {
  683. case -ENOMEM:
  684. goto out;
  685. case -EAGAIN:
  686. retry++;
  687. break;
  688. case 0:
  689. break;
  690. default:
  691. /* Permanent failure */
  692. nr_failed++;
  693. break;
  694. }
  695. }
  696. }
  697. rc = 0;
  698. out:
  699. if (!swapwrite)
  700. current->flags &= ~PF_SWAPWRITE;
  701. putback_lru_pages(from);
  702. if (rc)
  703. return rc;
  704. return nr_failed + retry;
  705. }
  706. #ifdef CONFIG_NUMA
  707. /*
  708. * Move a list of individual pages
  709. */
  710. struct page_to_node {
  711. unsigned long addr;
  712. struct page *page;
  713. int node;
  714. int status;
  715. };
  716. static struct page *new_page_node(struct page *p, unsigned long private,
  717. int **result)
  718. {
  719. struct page_to_node *pm = (struct page_to_node *)private;
  720. while (pm->node != MAX_NUMNODES && pm->page != p)
  721. pm++;
  722. if (pm->node == MAX_NUMNODES)
  723. return NULL;
  724. *result = &pm->status;
  725. return alloc_pages_node(pm->node,
  726. GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
  727. }
  728. /*
  729. * Move a set of pages as indicated in the pm array. The addr
  730. * field must be set to the virtual address of the page to be moved
  731. * and the node number must contain a valid target node.
  732. */
  733. static int do_move_pages(struct mm_struct *mm, struct page_to_node *pm,
  734. int migrate_all)
  735. {
  736. int err;
  737. struct page_to_node *pp;
  738. LIST_HEAD(pagelist);
  739. down_read(&mm->mmap_sem);
  740. /*
  741. * Build a list of pages to migrate
  742. */
  743. migrate_prep();
  744. for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
  745. struct vm_area_struct *vma;
  746. struct page *page;
  747. /*
  748. * A valid page pointer that will not match any of the
  749. * pages that will be moved.
  750. */
  751. pp->page = ZERO_PAGE(0);
  752. err = -EFAULT;
  753. vma = find_vma(mm, pp->addr);
  754. if (!vma || !vma_migratable(vma))
  755. goto set_status;
  756. page = follow_page(vma, pp->addr, FOLL_GET);
  757. err = PTR_ERR(page);
  758. if (IS_ERR(page))
  759. goto set_status;
  760. err = -ENOENT;
  761. if (!page)
  762. goto set_status;
  763. if (PageReserved(page)) /* Check for zero page */
  764. goto put_and_set;
  765. pp->page = page;
  766. err = page_to_nid(page);
  767. if (err == pp->node)
  768. /*
  769. * Node already in the right place
  770. */
  771. goto put_and_set;
  772. err = -EACCES;
  773. if (page_mapcount(page) > 1 &&
  774. !migrate_all)
  775. goto put_and_set;
  776. err = isolate_lru_page(page, &pagelist);
  777. put_and_set:
  778. /*
  779. * Either remove the duplicate refcount from
  780. * isolate_lru_page() or drop the page ref if it was
  781. * not isolated.
  782. */
  783. put_page(page);
  784. set_status:
  785. pp->status = err;
  786. }
  787. if (!list_empty(&pagelist))
  788. err = migrate_pages(&pagelist, new_page_node,
  789. (unsigned long)pm);
  790. else
  791. err = -ENOENT;
  792. up_read(&mm->mmap_sem);
  793. return err;
  794. }
  795. /*
  796. * Determine the nodes of a list of pages. The addr in the pm array
  797. * must have been set to the virtual address of which we want to determine
  798. * the node number.
  799. */
  800. static int do_pages_stat(struct mm_struct *mm, struct page_to_node *pm)
  801. {
  802. down_read(&mm->mmap_sem);
  803. for ( ; pm->node != MAX_NUMNODES; pm++) {
  804. struct vm_area_struct *vma;
  805. struct page *page;
  806. int err;
  807. err = -EFAULT;
  808. vma = find_vma(mm, pm->addr);
  809. if (!vma)
  810. goto set_status;
  811. page = follow_page(vma, pm->addr, 0);
  812. err = PTR_ERR(page);
  813. if (IS_ERR(page))
  814. goto set_status;
  815. err = -ENOENT;
  816. /* Use PageReserved to check for zero page */
  817. if (!page || PageReserved(page))
  818. goto set_status;
  819. err = page_to_nid(page);
  820. set_status:
  821. pm->status = err;
  822. }
  823. up_read(&mm->mmap_sem);
  824. return 0;
  825. }
  826. /*
  827. * Move a list of pages in the address space of the currently executing
  828. * process.
  829. */
  830. asmlinkage long sys_move_pages(pid_t pid, unsigned long nr_pages,
  831. const void __user * __user *pages,
  832. const int __user *nodes,
  833. int __user *status, int flags)
  834. {
  835. int err = 0;
  836. int i;
  837. struct task_struct *task;
  838. nodemask_t task_nodes;
  839. struct mm_struct *mm;
  840. struct page_to_node *pm = NULL;
  841. /* Check flags */
  842. if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
  843. return -EINVAL;
  844. if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
  845. return -EPERM;
  846. /* Find the mm_struct */
  847. read_lock(&tasklist_lock);
  848. task = pid ? find_task_by_vpid(pid) : current;
  849. if (!task) {
  850. read_unlock(&tasklist_lock);
  851. return -ESRCH;
  852. }
  853. mm = get_task_mm(task);
  854. read_unlock(&tasklist_lock);
  855. if (!mm)
  856. return -EINVAL;
  857. /*
  858. * Check if this process has the right to modify the specified
  859. * process. The right exists if the process has administrative
  860. * capabilities, superuser privileges or the same
  861. * userid as the target process.
  862. */
  863. if ((current->euid != task->suid) && (current->euid != task->uid) &&
  864. (current->uid != task->suid) && (current->uid != task->uid) &&
  865. !capable(CAP_SYS_NICE)) {
  866. err = -EPERM;
  867. goto out2;
  868. }
  869. err = security_task_movememory(task);
  870. if (err)
  871. goto out2;
  872. task_nodes = cpuset_mems_allowed(task);
  873. /* Limit nr_pages so that the multiplication may not overflow */
  874. if (nr_pages >= ULONG_MAX / sizeof(struct page_to_node) - 1) {
  875. err = -E2BIG;
  876. goto out2;
  877. }
  878. pm = vmalloc((nr_pages + 1) * sizeof(struct page_to_node));
  879. if (!pm) {
  880. err = -ENOMEM;
  881. goto out2;
  882. }
  883. /*
  884. * Get parameters from user space and initialize the pm
  885. * array. Return various errors if the user did something wrong.
  886. */
  887. for (i = 0; i < nr_pages; i++) {
  888. const void __user *p;
  889. err = -EFAULT;
  890. if (get_user(p, pages + i))
  891. goto out;
  892. pm[i].addr = (unsigned long)p;
  893. if (nodes) {
  894. int node;
  895. if (get_user(node, nodes + i))
  896. goto out;
  897. err = -ENODEV;
  898. if (!node_state(node, N_HIGH_MEMORY))
  899. goto out;
  900. err = -EACCES;
  901. if (!node_isset(node, task_nodes))
  902. goto out;
  903. pm[i].node = node;
  904. } else
  905. pm[i].node = 0; /* anything to not match MAX_NUMNODES */
  906. }
  907. /* End marker */
  908. pm[nr_pages].node = MAX_NUMNODES;
  909. if (nodes)
  910. err = do_move_pages(mm, pm, flags & MPOL_MF_MOVE_ALL);
  911. else
  912. err = do_pages_stat(mm, pm);
  913. if (err >= 0)
  914. /* Return status information */
  915. for (i = 0; i < nr_pages; i++)
  916. if (put_user(pm[i].status, status + i))
  917. err = -EFAULT;
  918. out:
  919. vfree(pm);
  920. out2:
  921. mmput(mm);
  922. return err;
  923. }
  924. /*
  925. * Call migration functions in the vma_ops that may prepare
  926. * memory in a vm for migration. migration functions may perform
  927. * the migration for vmas that do not have an underlying page struct.
  928. */
  929. int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
  930. const nodemask_t *from, unsigned long flags)
  931. {
  932. struct vm_area_struct *vma;
  933. int err = 0;
  934. for(vma = mm->mmap; vma->vm_next && !err; vma = vma->vm_next) {
  935. if (vma->vm_ops && vma->vm_ops->migrate) {
  936. err = vma->vm_ops->migrate(vma, to, from, flags);
  937. if (err)
  938. break;
  939. }
  940. }
  941. return err;
  942. }
  943. #endif