migrate.c 26 KB

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