migrate.c 26 KB

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