migrate.c 25 KB

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