migrate.c 23 KB

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