migrate.c 25 KB

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