migrate.c 25 KB

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