migrate.c 26 KB

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