migrate.c 26 KB

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