migrate.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. /*
  2. * Memory Migration functionality - linux/mm/migration.c
  3. *
  4. * Copyright (C) 2006 Silicon Graphics, Inc., Christoph Lameter
  5. *
  6. * Page migration was first developed in the context of the memory hotplug
  7. * project. The main authors of the migration code are:
  8. *
  9. * IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
  10. * Hirokazu Takahashi <taka@valinux.co.jp>
  11. * Dave Hansen <haveblue@us.ibm.com>
  12. * Christoph Lameter
  13. */
  14. #include <linux/migrate.h>
  15. #include <linux/module.h>
  16. #include <linux/swap.h>
  17. #include <linux/swapops.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/buffer_head.h>
  20. #include <linux/mm_inline.h>
  21. #include <linux/nsproxy.h>
  22. #include <linux/pagevec.h>
  23. #include <linux/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. return 0;
  284. }
  285. /*
  286. * Copy the page to its new location
  287. */
  288. static void migrate_page_copy(struct page *newpage, struct page *page)
  289. {
  290. int anon;
  291. copy_highpage(newpage, page);
  292. if (PageError(page))
  293. SetPageError(newpage);
  294. if (PageReferenced(page))
  295. SetPageReferenced(newpage);
  296. if (PageUptodate(page))
  297. SetPageUptodate(newpage);
  298. if (TestClearPageActive(page)) {
  299. VM_BUG_ON(PageUnevictable(page));
  300. SetPageActive(newpage);
  301. } else
  302. unevictable_migrate_page(newpage, page);
  303. if (PageChecked(page))
  304. SetPageChecked(newpage);
  305. if (PageMappedToDisk(page))
  306. SetPageMappedToDisk(newpage);
  307. if (PageDirty(page)) {
  308. clear_page_dirty_for_io(page);
  309. /*
  310. * Want to mark the page and the radix tree as dirty, and
  311. * redo the accounting that clear_page_dirty_for_io undid,
  312. * but we can't use set_page_dirty because that function
  313. * is actually a signal that all of the page has become dirty.
  314. * Wheras only part of our page may be dirty.
  315. */
  316. __set_page_dirty_nobuffers(newpage);
  317. }
  318. mlock_migrate_page(newpage, page);
  319. #ifdef CONFIG_SWAP
  320. ClearPageSwapCache(page);
  321. #endif
  322. ClearPagePrivate(page);
  323. set_page_private(page, 0);
  324. /* page->mapping contains a flag for PageAnon() */
  325. anon = PageAnon(page);
  326. page->mapping = NULL;
  327. if (!anon) /* This page was removed from radix-tree. */
  328. mem_cgroup_uncharge_cache_page(page);
  329. /*
  330. * If any waiters have accumulated on the new page then
  331. * wake them up.
  332. */
  333. if (PageWriteback(newpage))
  334. end_page_writeback(newpage);
  335. }
  336. /************************************************************
  337. * Migration functions
  338. ***********************************************************/
  339. /* Always fail migration. Used for mappings that are not movable */
  340. int fail_migrate_page(struct address_space *mapping,
  341. struct page *newpage, struct page *page)
  342. {
  343. return -EIO;
  344. }
  345. EXPORT_SYMBOL(fail_migrate_page);
  346. /*
  347. * Common logic to directly migrate a single page suitable for
  348. * pages that do not use PagePrivate.
  349. *
  350. * Pages are locked upon entry and exit.
  351. */
  352. int migrate_page(struct address_space *mapping,
  353. struct page *newpage, struct page *page)
  354. {
  355. int rc;
  356. BUG_ON(PageWriteback(page)); /* Writeback must be complete */
  357. rc = migrate_page_move_mapping(mapping, newpage, page);
  358. if (rc)
  359. return rc;
  360. migrate_page_copy(newpage, page);
  361. return 0;
  362. }
  363. EXPORT_SYMBOL(migrate_page);
  364. #ifdef CONFIG_BLOCK
  365. /*
  366. * Migration function for pages with buffers. This function can only be used
  367. * if the underlying filesystem guarantees that no other references to "page"
  368. * exist.
  369. */
  370. int buffer_migrate_page(struct address_space *mapping,
  371. struct page *newpage, struct page *page)
  372. {
  373. struct buffer_head *bh, *head;
  374. int rc;
  375. if (!page_has_buffers(page))
  376. return migrate_page(mapping, newpage, page);
  377. head = page_buffers(page);
  378. rc = migrate_page_move_mapping(mapping, newpage, page);
  379. if (rc)
  380. return rc;
  381. bh = head;
  382. do {
  383. get_bh(bh);
  384. lock_buffer(bh);
  385. bh = bh->b_this_page;
  386. } while (bh != head);
  387. ClearPagePrivate(page);
  388. set_page_private(newpage, page_private(page));
  389. set_page_private(page, 0);
  390. put_page(page);
  391. get_page(newpage);
  392. bh = head;
  393. do {
  394. set_bh_page(bh, newpage, bh_offset(bh));
  395. bh = bh->b_this_page;
  396. } while (bh != head);
  397. SetPagePrivate(newpage);
  398. migrate_page_copy(newpage, page);
  399. bh = head;
  400. do {
  401. unlock_buffer(bh);
  402. put_bh(bh);
  403. bh = bh->b_this_page;
  404. } while (bh != head);
  405. return 0;
  406. }
  407. EXPORT_SYMBOL(buffer_migrate_page);
  408. #endif
  409. /*
  410. * Writeback a page to clean the dirty state
  411. */
  412. static int writeout(struct address_space *mapping, struct page *page)
  413. {
  414. struct writeback_control wbc = {
  415. .sync_mode = WB_SYNC_NONE,
  416. .nr_to_write = 1,
  417. .range_start = 0,
  418. .range_end = LLONG_MAX,
  419. .nonblocking = 1,
  420. .for_reclaim = 1
  421. };
  422. int rc;
  423. if (!mapping->a_ops->writepage)
  424. /* No write method for the address space */
  425. return -EINVAL;
  426. if (!clear_page_dirty_for_io(page))
  427. /* Someone else already triggered a write */
  428. return -EAGAIN;
  429. /*
  430. * A dirty page may imply that the underlying filesystem has
  431. * the page on some queue. So the page must be clean for
  432. * migration. Writeout may mean we loose the lock and the
  433. * page state is no longer what we checked for earlier.
  434. * At this point we know that the migration attempt cannot
  435. * be successful.
  436. */
  437. remove_migration_ptes(page, page);
  438. rc = mapping->a_ops->writepage(page, &wbc);
  439. if (rc != AOP_WRITEPAGE_ACTIVATE)
  440. /* unlocked. Relock */
  441. lock_page(page);
  442. return (rc < 0) ? -EIO : -EAGAIN;
  443. }
  444. /*
  445. * Default handling if a filesystem does not provide a migration function.
  446. */
  447. static int fallback_migrate_page(struct address_space *mapping,
  448. struct page *newpage, struct page *page)
  449. {
  450. if (PageDirty(page))
  451. return writeout(mapping, page);
  452. /*
  453. * Buffers may be managed in a filesystem specific way.
  454. * We must have no buffers or drop them.
  455. */
  456. if (PagePrivate(page) &&
  457. !try_to_release_page(page, GFP_KERNEL))
  458. return -EAGAIN;
  459. return migrate_page(mapping, newpage, page);
  460. }
  461. /*
  462. * Move a page to a newly allocated page
  463. * The page is locked and all ptes have been successfully removed.
  464. *
  465. * The new page will have replaced the old page if this function
  466. * is successful.
  467. *
  468. * Return value:
  469. * < 0 - error code
  470. * == 0 - success
  471. */
  472. static int move_to_new_page(struct page *newpage, struct page *page)
  473. {
  474. struct address_space *mapping;
  475. int rc;
  476. /*
  477. * Block others from accessing the page when we get around to
  478. * establishing additional references. We are the only one
  479. * holding a reference to the new page at this point.
  480. */
  481. if (!trylock_page(newpage))
  482. BUG();
  483. /* Prepare mapping for the new page.*/
  484. newpage->index = page->index;
  485. newpage->mapping = page->mapping;
  486. if (PageSwapBacked(page))
  487. SetPageSwapBacked(newpage);
  488. mapping = page_mapping(page);
  489. if (!mapping)
  490. rc = migrate_page(mapping, newpage, page);
  491. else if (mapping->a_ops->migratepage)
  492. /*
  493. * Most pages have a mapping and most filesystems
  494. * should provide a migration function. Anonymous
  495. * pages are part of swap space which also has its
  496. * own migration function. This is the most common
  497. * path for page migration.
  498. */
  499. rc = mapping->a_ops->migratepage(mapping,
  500. newpage, page);
  501. else
  502. rc = fallback_migrate_page(mapping, newpage, page);
  503. if (!rc) {
  504. remove_migration_ptes(page, newpage);
  505. } else
  506. newpage->mapping = NULL;
  507. unlock_page(newpage);
  508. return rc;
  509. }
  510. /*
  511. * Obtain the lock on page, remove all ptes and migrate the page
  512. * to the newly allocated page in newpage.
  513. */
  514. static int unmap_and_move(new_page_t get_new_page, unsigned long private,
  515. struct page *page, int force)
  516. {
  517. int rc = 0;
  518. int *result = NULL;
  519. struct page *newpage = get_new_page(page, private, &result);
  520. int rcu_locked = 0;
  521. int charge = 0;
  522. if (!newpage)
  523. return -ENOMEM;
  524. if (page_count(page) == 1) {
  525. /* page was freed from under us. So we are done. */
  526. goto move_newpage;
  527. }
  528. charge = mem_cgroup_prepare_migration(page, newpage);
  529. if (charge == -ENOMEM) {
  530. rc = -ENOMEM;
  531. goto move_newpage;
  532. }
  533. /* prepare cgroup just returns 0 or -ENOMEM */
  534. BUG_ON(charge);
  535. rc = -EAGAIN;
  536. if (!trylock_page(page)) {
  537. if (!force)
  538. goto move_newpage;
  539. lock_page(page);
  540. }
  541. if (PageWriteback(page)) {
  542. if (!force)
  543. goto unlock;
  544. wait_on_page_writeback(page);
  545. }
  546. /*
  547. * By try_to_unmap(), page->mapcount goes down to 0 here. In this case,
  548. * we cannot notice that anon_vma is freed while we migrates a page.
  549. * This rcu_read_lock() delays freeing anon_vma pointer until the end
  550. * of migration. File cache pages are no problem because of page_lock()
  551. * File Caches may use write_page() or lock_page() in migration, then,
  552. * just care Anon page here.
  553. */
  554. if (PageAnon(page)) {
  555. rcu_read_lock();
  556. rcu_locked = 1;
  557. }
  558. /*
  559. * Corner case handling:
  560. * 1. When a new swap-cache page is read into, it is added to the LRU
  561. * and treated as swapcache but it has no rmap yet.
  562. * Calling try_to_unmap() against a page->mapping==NULL page will
  563. * trigger a BUG. So handle it here.
  564. * 2. An orphaned page (see truncate_complete_page) might have
  565. * fs-private metadata. The page can be picked up due to memory
  566. * offlining. Everywhere else except page reclaim, the page is
  567. * invisible to the vm, so the page can not be migrated. So try to
  568. * free the metadata, so the page can be freed.
  569. */
  570. if (!page->mapping) {
  571. if (!PageAnon(page) && PagePrivate(page)) {
  572. /*
  573. * Go direct to try_to_free_buffers() here because
  574. * a) that's what try_to_release_page() would do anyway
  575. * b) we may be under rcu_read_lock() here, so we can't
  576. * use GFP_KERNEL which is what try_to_release_page()
  577. * needs to be effective.
  578. */
  579. try_to_free_buffers(page);
  580. }
  581. goto rcu_unlock;
  582. }
  583. /* Establish migration ptes or remove ptes */
  584. try_to_unmap(page, 1);
  585. if (!page_mapped(page))
  586. rc = move_to_new_page(newpage, page);
  587. if (rc)
  588. remove_migration_ptes(page, page);
  589. rcu_unlock:
  590. if (rcu_locked)
  591. rcu_read_unlock();
  592. unlock:
  593. unlock_page(page);
  594. if (rc != -EAGAIN) {
  595. /*
  596. * A page that has been migrated has all references
  597. * removed and will be freed. A page that has not been
  598. * migrated will have kepts its references and be
  599. * restored.
  600. */
  601. list_del(&page->lru);
  602. putback_lru_page(page);
  603. }
  604. move_newpage:
  605. if (!charge)
  606. mem_cgroup_end_migration(newpage);
  607. /*
  608. * Move the new page to the LRU. If migration was not successful
  609. * then this will free the page.
  610. */
  611. putback_lru_page(newpage);
  612. if (result) {
  613. if (rc)
  614. *result = rc;
  615. else
  616. *result = page_to_nid(newpage);
  617. }
  618. return rc;
  619. }
  620. /*
  621. * migrate_pages
  622. *
  623. * The function takes one list of pages to migrate and a function
  624. * that determines from the page to be migrated and the private data
  625. * the target of the move and allocates the page.
  626. *
  627. * The function returns after 10 attempts or if no pages
  628. * are movable anymore because to has become empty
  629. * or no retryable pages exist anymore. All pages will be
  630. * returned to the LRU or freed.
  631. *
  632. * Return: Number of pages not migrated or error code.
  633. */
  634. int migrate_pages(struct list_head *from,
  635. new_page_t get_new_page, unsigned long private)
  636. {
  637. int retry = 1;
  638. int nr_failed = 0;
  639. int pass = 0;
  640. struct page *page;
  641. struct page *page2;
  642. int swapwrite = current->flags & PF_SWAPWRITE;
  643. int rc;
  644. if (!swapwrite)
  645. current->flags |= PF_SWAPWRITE;
  646. for(pass = 0; pass < 10 && retry; pass++) {
  647. retry = 0;
  648. list_for_each_entry_safe(page, page2, from, lru) {
  649. cond_resched();
  650. rc = unmap_and_move(get_new_page, private,
  651. page, pass > 2);
  652. switch(rc) {
  653. case -ENOMEM:
  654. goto out;
  655. case -EAGAIN:
  656. retry++;
  657. break;
  658. case 0:
  659. break;
  660. default:
  661. /* Permanent failure */
  662. nr_failed++;
  663. break;
  664. }
  665. }
  666. }
  667. rc = 0;
  668. out:
  669. if (!swapwrite)
  670. current->flags &= ~PF_SWAPWRITE;
  671. putback_lru_pages(from);
  672. if (rc)
  673. return rc;
  674. return nr_failed + retry;
  675. }
  676. #ifdef CONFIG_NUMA
  677. /*
  678. * Move a list of individual pages
  679. */
  680. struct page_to_node {
  681. unsigned long addr;
  682. struct page *page;
  683. int node;
  684. int status;
  685. };
  686. static struct page *new_page_node(struct page *p, unsigned long private,
  687. int **result)
  688. {
  689. struct page_to_node *pm = (struct page_to_node *)private;
  690. while (pm->node != MAX_NUMNODES && pm->page != p)
  691. pm++;
  692. if (pm->node == MAX_NUMNODES)
  693. return NULL;
  694. *result = &pm->status;
  695. return alloc_pages_node(pm->node,
  696. GFP_HIGHUSER_MOVABLE | GFP_THISNODE, 0);
  697. }
  698. /*
  699. * Move a set of pages as indicated in the pm array. The addr
  700. * field must be set to the virtual address of the page to be moved
  701. * and the node number must contain a valid target node.
  702. * The pm array ends with node = MAX_NUMNODES.
  703. */
  704. static int do_move_page_to_node_array(struct mm_struct *mm,
  705. struct page_to_node *pm,
  706. int migrate_all)
  707. {
  708. int err;
  709. struct page_to_node *pp;
  710. LIST_HEAD(pagelist);
  711. migrate_prep();
  712. down_read(&mm->mmap_sem);
  713. /*
  714. * Build a list of pages to migrate
  715. */
  716. for (pp = pm; pp->node != MAX_NUMNODES; pp++) {
  717. struct vm_area_struct *vma;
  718. struct page *page;
  719. /*
  720. * A valid page pointer that will not match any of the
  721. * pages that will be moved.
  722. */
  723. pp->page = ZERO_PAGE(0);
  724. err = -EFAULT;
  725. vma = find_vma(mm, pp->addr);
  726. if (!vma || !vma_migratable(vma))
  727. goto set_status;
  728. page = follow_page(vma, pp->addr, FOLL_GET);
  729. err = PTR_ERR(page);
  730. if (IS_ERR(page))
  731. goto set_status;
  732. err = -ENOENT;
  733. if (!page)
  734. goto set_status;
  735. if (PageReserved(page)) /* Check for zero page */
  736. goto put_and_set;
  737. pp->page = page;
  738. err = page_to_nid(page);
  739. if (err == pp->node)
  740. /*
  741. * Node already in the right place
  742. */
  743. goto put_and_set;
  744. err = -EACCES;
  745. if (page_mapcount(page) > 1 &&
  746. !migrate_all)
  747. goto put_and_set;
  748. err = isolate_lru_page(page);
  749. if (!err)
  750. list_add_tail(&page->lru, &pagelist);
  751. put_and_set:
  752. /*
  753. * Either remove the duplicate refcount from
  754. * isolate_lru_page() or drop the page ref if it was
  755. * not isolated.
  756. */
  757. put_page(page);
  758. set_status:
  759. pp->status = err;
  760. }
  761. err = 0;
  762. if (!list_empty(&pagelist))
  763. err = migrate_pages(&pagelist, new_page_node,
  764. (unsigned long)pm);
  765. up_read(&mm->mmap_sem);
  766. return err;
  767. }
  768. /*
  769. * Migrate an array of page address onto an array of nodes and fill
  770. * the corresponding array of status.
  771. */
  772. static int do_pages_move(struct mm_struct *mm, struct task_struct *task,
  773. unsigned long nr_pages,
  774. const void __user * __user *pages,
  775. const int __user *nodes,
  776. int __user *status, int flags)
  777. {
  778. struct page_to_node *pm = NULL;
  779. nodemask_t task_nodes;
  780. int err = 0;
  781. int i;
  782. task_nodes = cpuset_mems_allowed(task);
  783. /* Limit nr_pages so that the multiplication may not overflow */
  784. if (nr_pages >= ULONG_MAX / sizeof(struct page_to_node) - 1) {
  785. err = -E2BIG;
  786. goto out;
  787. }
  788. pm = vmalloc((nr_pages + 1) * sizeof(struct page_to_node));
  789. if (!pm) {
  790. err = -ENOMEM;
  791. goto out;
  792. }
  793. /*
  794. * Get parameters from user space and initialize the pm
  795. * array. Return various errors if the user did something wrong.
  796. */
  797. for (i = 0; i < nr_pages; i++) {
  798. const void __user *p;
  799. err = -EFAULT;
  800. if (get_user(p, pages + i))
  801. goto out_pm;
  802. pm[i].addr = (unsigned long)p;
  803. if (nodes) {
  804. int node;
  805. if (get_user(node, nodes + i))
  806. goto out_pm;
  807. err = -ENODEV;
  808. if (!node_state(node, N_HIGH_MEMORY))
  809. goto out_pm;
  810. err = -EACCES;
  811. if (!node_isset(node, task_nodes))
  812. goto out_pm;
  813. pm[i].node = node;
  814. } else
  815. pm[i].node = 0; /* anything to not match MAX_NUMNODES */
  816. }
  817. /* End marker */
  818. pm[nr_pages].node = MAX_NUMNODES;
  819. err = do_move_page_to_node_array(mm, pm, flags & MPOL_MF_MOVE_ALL);
  820. if (err >= 0)
  821. /* Return status information */
  822. for (i = 0; i < nr_pages; i++)
  823. if (put_user(pm[i].status, status + i))
  824. err = -EFAULT;
  825. out_pm:
  826. vfree(pm);
  827. out:
  828. return err;
  829. }
  830. /*
  831. * Determine the nodes of an array of pages and store it in an array of status.
  832. */
  833. static int do_pages_stat(struct mm_struct *mm, unsigned long nr_pages,
  834. const void __user * __user *pages,
  835. int __user *status)
  836. {
  837. unsigned long i;
  838. int err;
  839. down_read(&mm->mmap_sem);
  840. for (i = 0; i < nr_pages; i++) {
  841. const void __user *p;
  842. unsigned long addr;
  843. struct vm_area_struct *vma;
  844. struct page *page;
  845. err = -EFAULT;
  846. if (get_user(p, pages+i))
  847. goto out;
  848. addr = (unsigned long) p;
  849. vma = find_vma(mm, addr);
  850. if (!vma)
  851. goto set_status;
  852. page = follow_page(vma, addr, 0);
  853. err = PTR_ERR(page);
  854. if (IS_ERR(page))
  855. goto set_status;
  856. err = -ENOENT;
  857. /* Use PageReserved to check for zero page */
  858. if (!page || PageReserved(page))
  859. goto set_status;
  860. err = page_to_nid(page);
  861. set_status:
  862. put_user(err, status+i);
  863. }
  864. err = 0;
  865. out:
  866. up_read(&mm->mmap_sem);
  867. return err;
  868. }
  869. /*
  870. * Move a list of pages in the address space of the currently executing
  871. * process.
  872. */
  873. asmlinkage long sys_move_pages(pid_t pid, unsigned long nr_pages,
  874. const void __user * __user *pages,
  875. const int __user *nodes,
  876. int __user *status, int flags)
  877. {
  878. struct task_struct *task;
  879. struct mm_struct *mm;
  880. int err;
  881. /* Check flags */
  882. if (flags & ~(MPOL_MF_MOVE|MPOL_MF_MOVE_ALL))
  883. return -EINVAL;
  884. if ((flags & MPOL_MF_MOVE_ALL) && !capable(CAP_SYS_NICE))
  885. return -EPERM;
  886. /* Find the mm_struct */
  887. read_lock(&tasklist_lock);
  888. task = pid ? find_task_by_vpid(pid) : current;
  889. if (!task) {
  890. read_unlock(&tasklist_lock);
  891. return -ESRCH;
  892. }
  893. mm = get_task_mm(task);
  894. read_unlock(&tasklist_lock);
  895. if (!mm)
  896. return -EINVAL;
  897. /*
  898. * Check if this process has the right to modify the specified
  899. * process. The right exists if the process has administrative
  900. * capabilities, superuser privileges or the same
  901. * userid as the target process.
  902. */
  903. if ((current->euid != task->suid) && (current->euid != task->uid) &&
  904. (current->uid != task->suid) && (current->uid != task->uid) &&
  905. !capable(CAP_SYS_NICE)) {
  906. err = -EPERM;
  907. goto out;
  908. }
  909. err = security_task_movememory(task);
  910. if (err)
  911. goto out;
  912. if (nodes) {
  913. err = do_pages_move(mm, task, nr_pages, pages, nodes, status,
  914. flags);
  915. } else {
  916. err = do_pages_stat(mm, nr_pages, pages, status);
  917. }
  918. out:
  919. mmput(mm);
  920. return err;
  921. }
  922. /*
  923. * Call migration functions in the vma_ops that may prepare
  924. * memory in a vm for migration. migration functions may perform
  925. * the migration for vmas that do not have an underlying page struct.
  926. */
  927. int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
  928. const nodemask_t *from, unsigned long flags)
  929. {
  930. struct vm_area_struct *vma;
  931. int err = 0;
  932. for(vma = mm->mmap; vma->vm_next && !err; vma = vma->vm_next) {
  933. if (vma->vm_ops && vma->vm_ops->migrate) {
  934. err = vma->vm_ops->migrate(vma, to, from, flags);
  935. if (err)
  936. break;
  937. }
  938. }
  939. return err;
  940. }
  941. #endif