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