migrate.c 26 KB

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