migrate.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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. #ifdef CONFIG_SWAP
  258. if (PageSwapCache(page)) {
  259. SetPageSwapCache(newpage);
  260. set_page_private(newpage, page_private(page));
  261. }
  262. #endif
  263. radix_tree_replace_slot(pslot, newpage);
  264. page_unfreeze_refs(page, expected_count);
  265. /*
  266. * Drop cache reference from old page.
  267. * We know this isn't the last reference.
  268. */
  269. __put_page(page);
  270. /*
  271. * If moved to a different zone then also account
  272. * the page for that zone. Other VM counters will be
  273. * taken care of when we establish references to the
  274. * new page and drop references to the old page.
  275. *
  276. * Note that anonymous pages are accounted for
  277. * via NR_FILE_PAGES and NR_ANON_PAGES if they
  278. * are mapped to swap space.
  279. */
  280. __dec_zone_page_state(page, NR_FILE_PAGES);
  281. __inc_zone_page_state(newpage, NR_FILE_PAGES);
  282. spin_unlock_irq(&mapping->tree_lock);
  283. if (!PageSwapCache(newpage))
  284. mem_cgroup_uncharge_cache_page(page);
  285. return 0;
  286. }
  287. /*
  288. * Copy the page to its new location
  289. */
  290. static void migrate_page_copy(struct page *newpage, struct page *page)
  291. {
  292. copy_highpage(newpage, page);
  293. if (PageError(page))
  294. SetPageError(newpage);
  295. if (PageReferenced(page))
  296. SetPageReferenced(newpage);
  297. if (PageUptodate(page))
  298. SetPageUptodate(newpage);
  299. if (TestClearPageActive(page)) {
  300. VM_BUG_ON(PageUnevictable(page));
  301. SetPageActive(newpage);
  302. } else
  303. unevictable_migrate_page(newpage, page);
  304. if (PageChecked(page))
  305. SetPageChecked(newpage);
  306. if (PageMappedToDisk(page))
  307. SetPageMappedToDisk(newpage);
  308. if (PageDirty(page)) {
  309. clear_page_dirty_for_io(page);
  310. /*
  311. * Want to mark the page and the radix tree as dirty, and
  312. * redo the accounting that clear_page_dirty_for_io undid,
  313. * but we can't use set_page_dirty because that function
  314. * is actually a signal that all of the page has become dirty.
  315. * Wheras only part of our page may be dirty.
  316. */
  317. __set_page_dirty_nobuffers(newpage);
  318. }
  319. #ifdef CONFIG_SWAP
  320. ClearPageSwapCache(page);
  321. #endif
  322. ClearPagePrivate(page);
  323. set_page_private(page, 0);
  324. page->mapping = NULL;
  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 < 0)
  436. /* I/O Error writing */
  437. return -EIO;
  438. if (rc != AOP_WRITEPAGE_ACTIVATE)
  439. /* unlocked. Relock */
  440. lock_page(page);
  441. return -EAGAIN;
  442. }
  443. /*
  444. * Default handling if a filesystem does not provide a migration function.
  445. */
  446. static int fallback_migrate_page(struct address_space *mapping,
  447. struct page *newpage, struct page *page)
  448. {
  449. if (PageDirty(page))
  450. return writeout(mapping, page);
  451. /*
  452. * Buffers may be managed in a filesystem specific way.
  453. * We must have no buffers or drop them.
  454. */
  455. if (PagePrivate(page) &&
  456. !try_to_release_page(page, GFP_KERNEL))
  457. return -EAGAIN;
  458. return migrate_page(mapping, newpage, page);
  459. }
  460. /*
  461. * Move a page to a newly allocated page
  462. * The page is locked and all ptes have been successfully removed.
  463. *
  464. * The new page will have replaced the old page if this function
  465. * is successful.
  466. *
  467. * Return value:
  468. * < 0 - error code
  469. * == 0 - success
  470. */
  471. static int move_to_new_page(struct page *newpage, struct page *page)
  472. {
  473. struct address_space *mapping;
  474. int rc;
  475. /*
  476. * Block others from accessing the page when we get around to
  477. * establishing additional references. We are the only one
  478. * holding a reference to the new page at this point.
  479. */
  480. if (!trylock_page(newpage))
  481. BUG();
  482. /* Prepare mapping for the new page.*/
  483. newpage->index = page->index;
  484. newpage->mapping = page->mapping;
  485. if (PageSwapBacked(page))
  486. SetPageSwapBacked(newpage);
  487. mapping = page_mapping(page);
  488. if (!mapping)
  489. rc = migrate_page(mapping, newpage, page);
  490. else if (mapping->a_ops->migratepage)
  491. /*
  492. * Most pages have a mapping and most filesystems
  493. * should provide a migration function. Anonymous
  494. * pages are part of swap space which also has its
  495. * own migration function. This is the most common
  496. * path for page migration.
  497. */
  498. rc = mapping->a_ops->migratepage(mapping,
  499. newpage, page);
  500. else
  501. rc = fallback_migrate_page(mapping, newpage, page);
  502. if (!rc) {
  503. remove_migration_ptes(page, newpage);
  504. } else
  505. newpage->mapping = NULL;
  506. unlock_page(newpage);
  507. return rc;
  508. }
  509. /*
  510. * Obtain the lock on page, remove all ptes and migrate the page
  511. * to the newly allocated page in newpage.
  512. */
  513. static int unmap_and_move(new_page_t get_new_page, unsigned long private,
  514. struct page *page, int force)
  515. {
  516. int rc = 0;
  517. int *result = NULL;
  518. struct page *newpage = get_new_page(page, private, &result);
  519. int rcu_locked = 0;
  520. int charge = 0;
  521. if (!newpage)
  522. return -ENOMEM;
  523. if (page_count(page) == 1) {
  524. /* page was freed from under us. So we are done. */
  525. goto move_newpage;
  526. }
  527. charge = mem_cgroup_prepare_migration(page, newpage);
  528. if (charge == -ENOMEM) {
  529. rc = -ENOMEM;
  530. goto move_newpage;
  531. }
  532. /* prepare cgroup just returns 0 or -ENOMEM */
  533. BUG_ON(charge);
  534. rc = -EAGAIN;
  535. if (!trylock_page(page)) {
  536. if (!force)
  537. goto move_newpage;
  538. lock_page(page);
  539. }
  540. if (PageWriteback(page)) {
  541. if (!force)
  542. goto unlock;
  543. wait_on_page_writeback(page);
  544. }
  545. /*
  546. * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
  547. * we cannot notice that anon_vma is freed while we migrates a page.
  548. * This rcu_read_lock() delays freeing anon_vma pointer until the end
  549. * of migration. File cache pages are no problem because of page_lock()
  550. * File Caches may use write_page() or lock_page() in migration, then,
  551. * just care Anon page here.
  552. */
  553. if (PageAnon(page)) {
  554. rcu_read_lock();
  555. rcu_locked = 1;
  556. }
  557. /*
  558. * Corner case handling:
  559. * 1. When a new swap-cache page is read into, it is added to the LRU
  560. * and treated as swapcache but it has no rmap yet.
  561. * Calling try_to_unmap() against a page->mapping==NULL page will
  562. * trigger a BUG. So handle it here.
  563. * 2. An orphaned page (see truncate_complete_page) might have
  564. * fs-private metadata. The page can be picked up due to memory
  565. * offlining. Everywhere else except page reclaim, the page is
  566. * invisible to the vm, so the page can not be migrated. So try to
  567. * free the metadata, so the page can be freed.
  568. */
  569. if (!page->mapping) {
  570. if (!PageAnon(page) && PagePrivate(page)) {
  571. /*
  572. * Go direct to try_to_free_buffers() here because
  573. * a) that's what try_to_release_page() would do anyway
  574. * b) we may be under rcu_read_lock() here, so we can't
  575. * use GFP_KERNEL which is what try_to_release_page()
  576. * needs to be effective.
  577. */
  578. try_to_free_buffers(page);
  579. }
  580. goto rcu_unlock;
  581. }
  582. /* Establish migration ptes or remove ptes */
  583. try_to_unmap(page, 1);
  584. if (!page_mapped(page))
  585. rc = move_to_new_page(newpage, page);
  586. if (rc)
  587. remove_migration_ptes(page, page);
  588. rcu_unlock:
  589. if (rcu_locked)
  590. rcu_read_unlock();
  591. unlock:
  592. unlock_page(page);
  593. if (rc != -EAGAIN) {
  594. /*
  595. * A page that has been migrated has all references
  596. * removed and will be freed. A page that has not been
  597. * migrated will have kepts its references and be
  598. * restored.
  599. */
  600. list_del(&page->lru);
  601. putback_lru_page(page);
  602. }
  603. move_newpage:
  604. if (!charge)
  605. mem_cgroup_end_migration(newpage);
  606. /*
  607. * Move the new page to the LRU. If migration was not successful
  608. * then this will free the page.
  609. */
  610. putback_lru_page(newpage);
  611. if (result) {
  612. if (rc)
  613. *result = rc;
  614. else
  615. *result = page_to_nid(newpage);
  616. }
  617. return rc;
  618. }
  619. /*
  620. * migrate_pages
  621. *
  622. * The function takes one list of pages to migrate and a function
  623. * that determines from the page to be migrated and the private data
  624. * the target of the move and allocates the page.
  625. *
  626. * The function returns after 10 attempts or if no pages
  627. * are movable anymore because to has become empty
  628. * or no retryable pages exist anymore. All pages will be
  629. * returned to the LRU or freed.
  630. *
  631. * Return: Number of pages not migrated or error code.
  632. */
  633. int migrate_pages(struct list_head *from,
  634. new_page_t get_new_page, unsigned long private)
  635. {
  636. int retry = 1;
  637. int nr_failed = 0;
  638. int pass = 0;
  639. struct page *page;
  640. struct page *page2;
  641. int swapwrite = current->flags & PF_SWAPWRITE;
  642. int rc;
  643. if (!swapwrite)
  644. current->flags |= PF_SWAPWRITE;
  645. for(pass = 0; pass < 10 && retry; pass++) {
  646. retry = 0;
  647. list_for_each_entry_safe(page, page2, from, lru) {
  648. cond_resched();
  649. rc = unmap_and_move(get_new_page, private,
  650. page, pass > 2);
  651. switch(rc) {
  652. case -ENOMEM:
  653. goto out;
  654. case -EAGAIN:
  655. retry++;
  656. break;
  657. case 0:
  658. break;
  659. default:
  660. /* Permanent failure */
  661. nr_failed++;
  662. break;
  663. }
  664. }
  665. }
  666. rc = 0;
  667. out:
  668. if (!swapwrite)
  669. current->flags &= ~PF_SWAPWRITE;
  670. putback_lru_pages(from);
  671. if (rc)
  672. return rc;
  673. return nr_failed + retry;
  674. }
  675. #ifdef CONFIG_NUMA
  676. /*
  677. * Move a list of individual pages
  678. */
  679. struct page_to_node {
  680. unsigned long addr;
  681. struct page *page;
  682. int node;
  683. int status;
  684. };
  685. static struct page *new_page_node(struct page *p, unsigned long private,
  686. int **result)
  687. {
  688. struct page_to_node *pm = (struct page_to_node *)private;
  689. while (pm->node != MAX_NUMNODES && pm->page != p)
  690. pm++;
  691. if (pm->node == MAX_NUMNODES)
  692. return NULL;
  693. *result = &pm->status;
  694. return alloc_pages_node(pm->node,
  695. GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
  696. }
  697. /*
  698. * Move a set of pages as indicated in the pm array. The addr
  699. * field must be set to the virtual address of the page to be moved
  700. * and the node number must contain a valid target node.
  701. */
  702. static int do_move_pages(struct mm_struct *mm, struct page_to_node *pm,
  703. int migrate_all)
  704. {
  705. int err;
  706. struct page_to_node *pp;
  707. LIST_HEAD(pagelist);
  708. down_read(&mm->mmap_sem);
  709. /*
  710. * Build a list of pages to migrate
  711. */
  712. migrate_prep();
  713. for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
  714. struct vm_area_struct *vma;
  715. struct page *page;
  716. /*
  717. * A valid page pointer that will not match any of the
  718. * pages that will be moved.
  719. */
  720. pp->page = ZERO_PAGE(0);
  721. err = -EFAULT;
  722. vma = find_vma(mm, pp->addr);
  723. if (!vma || !vma_migratable(vma))
  724. goto set_status;
  725. page = follow_page(vma, pp->addr, FOLL_GET);
  726. err = PTR_ERR(page);
  727. if (IS_ERR(page))
  728. goto set_status;
  729. err = -ENOENT;
  730. if (!page)
  731. goto set_status;
  732. if (PageReserved(page)) /* Check for zero page */
  733. goto put_and_set;
  734. pp->page = page;
  735. err = page_to_nid(page);
  736. if (err == pp->node)
  737. /*
  738. * Node already in the right place
  739. */
  740. goto put_and_set;
  741. err = -EACCES;
  742. if (page_mapcount(page) > 1 &&
  743. !migrate_all)
  744. goto put_and_set;
  745. err = isolate_lru_page(page);
  746. if (!err)
  747. list_add_tail(&page->lru, &pagelist);
  748. put_and_set:
  749. /*
  750. * Either remove the duplicate refcount from
  751. * isolate_lru_page() or drop the page ref if it was
  752. * not isolated.
  753. */
  754. put_page(page);
  755. set_status:
  756. pp->status = err;
  757. }
  758. if (!list_empty(&pagelist))
  759. err = migrate_pages(&pagelist, new_page_node,
  760. (unsigned long)pm);
  761. else
  762. err = -ENOENT;
  763. up_read(&mm->mmap_sem);
  764. return err;
  765. }
  766. /*
  767. * Determine the nodes of a list of pages. The addr in the pm array
  768. * must have been set to the virtual address of which we want to determine
  769. * the node number.
  770. */
  771. static int do_pages_stat(struct mm_struct *mm, struct page_to_node *pm)
  772. {
  773. down_read(&mm->mmap_sem);
  774. for ( ; pm->node != MAX_NUMNODES; pm++) {
  775. struct vm_area_struct *vma;
  776. struct page *page;
  777. int err;
  778. err = -EFAULT;
  779. vma = find_vma(mm, pm->addr);
  780. if (!vma)
  781. goto set_status;
  782. page = follow_page(vma, pm->addr, 0);
  783. err = PTR_ERR(page);
  784. if (IS_ERR(page))
  785. goto set_status;
  786. err = -ENOENT;
  787. /* Use PageReserved to check for zero page */
  788. if (!page || PageReserved(page))
  789. goto set_status;
  790. err = page_to_nid(page);
  791. set_status:
  792. pm->status = err;
  793. }
  794. up_read(&mm->mmap_sem);
  795. return 0;
  796. }
  797. /*
  798. * Move a list of pages in the address space of the currently executing
  799. * process.
  800. */
  801. asmlinkage long sys_move_pages(pid_t pid, unsigned long nr_pages,
  802. const void __user * __user *pages,
  803. const int __user *nodes,
  804. int __user *status, int flags)
  805. {
  806. int err = 0;
  807. int i;
  808. struct task_struct *task;
  809. nodemask_t task_nodes;
  810. struct mm_struct *mm;
  811. struct page_to_node *pm = NULL;
  812. /* Check flags */
  813. if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
  814. return -EINVAL;
  815. if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
  816. return -EPERM;
  817. /* Find the mm_struct */
  818. read_lock(&tasklist_lock);
  819. task = pid ? find_task_by_vpid(pid) : current;
  820. if (!task) {
  821. read_unlock(&tasklist_lock);
  822. return -ESRCH;
  823. }
  824. mm = get_task_mm(task);
  825. read_unlock(&tasklist_lock);
  826. if (!mm)
  827. return -EINVAL;
  828. /*
  829. * Check if this process has the right to modify the specified
  830. * process. The right exists if the process has administrative
  831. * capabilities, superuser privileges or the same
  832. * userid as the target process.
  833. */
  834. if ((current->euid != task->suid) && (current->euid != task->uid) &&
  835. (current->uid != task->suid) && (current->uid != task->uid) &&
  836. !capable(CAP_SYS_NICE)) {
  837. err = -EPERM;
  838. goto out2;
  839. }
  840. err = security_task_movememory(task);
  841. if (err)
  842. goto out2;
  843. task_nodes = cpuset_mems_allowed(task);
  844. /* Limit nr_pages so that the multiplication may not overflow */
  845. if (nr_pages >= ULONG_MAX / sizeof(struct page_to_node) - 1) {
  846. err = -E2BIG;
  847. goto out2;
  848. }
  849. pm = vmalloc((nr_pages + 1) * sizeof(struct page_to_node));
  850. if (!pm) {
  851. err = -ENOMEM;
  852. goto out2;
  853. }
  854. /*
  855. * Get parameters from user space and initialize the pm
  856. * array. Return various errors if the user did something wrong.
  857. */
  858. for (i = 0; i < nr_pages; i++) {
  859. const void __user *p;
  860. err = -EFAULT;
  861. if (get_user(p, pages + i))
  862. goto out;
  863. pm[i].addr = (unsigned long)p;
  864. if (nodes) {
  865. int node;
  866. if (get_user(node, nodes + i))
  867. goto out;
  868. err = -ENODEV;
  869. if (!node_state(node, N_HIGH_MEMORY))
  870. goto out;
  871. err = -EACCES;
  872. if (!node_isset(node, task_nodes))
  873. goto out;
  874. pm[i].node = node;
  875. } else
  876. pm[i].node = 0; /* anything to not match MAX_NUMNODES */
  877. }
  878. /* End marker */
  879. pm[nr_pages].node = MAX_NUMNODES;
  880. if (nodes)
  881. err = do_move_pages(mm, pm, flags & MPOL_MF_MOVE_ALL);
  882. else
  883. err = do_pages_stat(mm, pm);
  884. if (err >= 0)
  885. /* Return status information */
  886. for (i = 0; i < nr_pages; i++)
  887. if (put_user(pm[i].status, status + i))
  888. err = -EFAULT;
  889. out:
  890. vfree(pm);
  891. out2:
  892. mmput(mm);
  893. return err;
  894. }
  895. /*
  896. * Call migration functions in the vma_ops that may prepare
  897. * memory in a vm for migration. migration functions may perform
  898. * the migration for vmas that do not have an underlying page struct.
  899. */
  900. int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
  901. const nodemask_t *from, unsigned long flags)
  902. {
  903. struct vm_area_struct *vma;
  904. int err = 0;
  905. for(vma = mm->mmap; vma->vm_next && !err; vma = vma->vm_next) {
  906. if (vma->vm_ops && vma->vm_ops->migrate) {
  907. err = vma->vm_ops->migrate(vma, to, from, flags);
  908. if (err)
  909. break;
  910. }
  911. }
  912. return err;
  913. }
  914. #endif