rmap.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. /*
  2. * mm/rmap.c - physical to virtual reverse mappings
  3. *
  4. * Copyright 2001, Rik van Riel <riel@conectiva.com.br>
  5. * Released under the General Public License (GPL).
  6. *
  7. * Simple, low overhead reverse mapping scheme.
  8. * Please try to keep this thing as modular as possible.
  9. *
  10. * Provides methods for unmapping each kind of mapped page:
  11. * the anon methods track anonymous pages, and
  12. * the file methods track pages belonging to an inode.
  13. *
  14. * Original design by Rik van Riel <riel@conectiva.com.br> 2001
  15. * File methods by Dave McCracken <dmccr@us.ibm.com> 2003, 2004
  16. * Anonymous methods by Andrea Arcangeli <andrea@suse.de> 2004
  17. * Contributions by Hugh Dickins <hugh@veritas.com> 2003, 2004
  18. */
  19. /*
  20. * Lock ordering in mm:
  21. *
  22. * inode->i_mutex (while writing or truncating, not reading or faulting)
  23. * inode->i_alloc_sem (vmtruncate_range)
  24. * mm->mmap_sem
  25. * page->flags PG_locked (lock_page)
  26. * mapping->i_mmap_lock
  27. * anon_vma->lock
  28. * mm->page_table_lock or pte_lock
  29. * zone->lru_lock (in mark_page_accessed, isolate_lru_page)
  30. * swap_lock (in swap_duplicate, swap_info_get)
  31. * mmlist_lock (in mmput, drain_mmlist and others)
  32. * mapping->private_lock (in __set_page_dirty_buffers)
  33. * inode_lock (in set_page_dirty's __mark_inode_dirty)
  34. * sb_lock (within inode_lock in fs/fs-writeback.c)
  35. * mapping->tree_lock (widely used, in set_page_dirty,
  36. * in arch-dependent flush_dcache_mmap_lock,
  37. * within inode_lock in __sync_single_inode)
  38. */
  39. #include <linux/mm.h>
  40. #include <linux/pagemap.h>
  41. #include <linux/swap.h>
  42. #include <linux/swapops.h>
  43. #include <linux/slab.h>
  44. #include <linux/init.h>
  45. #include <linux/rmap.h>
  46. #include <linux/rcupdate.h>
  47. #include <linux/module.h>
  48. #include <asm/tlbflush.h>
  49. struct kmem_cache *anon_vma_cachep;
  50. static inline void validate_anon_vma(struct vm_area_struct *find_vma)
  51. {
  52. #ifdef CONFIG_DEBUG_VM
  53. struct anon_vma *anon_vma = find_vma->anon_vma;
  54. struct vm_area_struct *vma;
  55. unsigned int mapcount = 0;
  56. int found = 0;
  57. list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
  58. mapcount++;
  59. BUG_ON(mapcount > 100000);
  60. if (vma == find_vma)
  61. found = 1;
  62. }
  63. BUG_ON(!found);
  64. #endif
  65. }
  66. /* This must be called under the mmap_sem. */
  67. int anon_vma_prepare(struct vm_area_struct *vma)
  68. {
  69. struct anon_vma *anon_vma = vma->anon_vma;
  70. might_sleep();
  71. if (unlikely(!anon_vma)) {
  72. struct mm_struct *mm = vma->vm_mm;
  73. struct anon_vma *allocated, *locked;
  74. anon_vma = find_mergeable_anon_vma(vma);
  75. if (anon_vma) {
  76. allocated = NULL;
  77. locked = anon_vma;
  78. spin_lock(&locked->lock);
  79. } else {
  80. anon_vma = anon_vma_alloc();
  81. if (unlikely(!anon_vma))
  82. return -ENOMEM;
  83. allocated = anon_vma;
  84. locked = NULL;
  85. }
  86. /* page_table_lock to protect against threads */
  87. spin_lock(&mm->page_table_lock);
  88. if (likely(!vma->anon_vma)) {
  89. vma->anon_vma = anon_vma;
  90. list_add_tail(&vma->anon_vma_node, &anon_vma->head);
  91. allocated = NULL;
  92. }
  93. spin_unlock(&mm->page_table_lock);
  94. if (locked)
  95. spin_unlock(&locked->lock);
  96. if (unlikely(allocated))
  97. anon_vma_free(allocated);
  98. }
  99. return 0;
  100. }
  101. void __anon_vma_merge(struct vm_area_struct *vma, struct vm_area_struct *next)
  102. {
  103. BUG_ON(vma->anon_vma != next->anon_vma);
  104. list_del(&next->anon_vma_node);
  105. }
  106. void __anon_vma_link(struct vm_area_struct *vma)
  107. {
  108. struct anon_vma *anon_vma = vma->anon_vma;
  109. if (anon_vma) {
  110. list_add_tail(&vma->anon_vma_node, &anon_vma->head);
  111. validate_anon_vma(vma);
  112. }
  113. }
  114. void anon_vma_link(struct vm_area_struct *vma)
  115. {
  116. struct anon_vma *anon_vma = vma->anon_vma;
  117. if (anon_vma) {
  118. spin_lock(&anon_vma->lock);
  119. list_add_tail(&vma->anon_vma_node, &anon_vma->head);
  120. validate_anon_vma(vma);
  121. spin_unlock(&anon_vma->lock);
  122. }
  123. }
  124. void anon_vma_unlink(struct vm_area_struct *vma)
  125. {
  126. struct anon_vma *anon_vma = vma->anon_vma;
  127. int empty;
  128. if (!anon_vma)
  129. return;
  130. spin_lock(&anon_vma->lock);
  131. validate_anon_vma(vma);
  132. list_del(&vma->anon_vma_node);
  133. /* We must garbage collect the anon_vma if it's empty */
  134. empty = list_empty(&anon_vma->head);
  135. spin_unlock(&anon_vma->lock);
  136. if (empty)
  137. anon_vma_free(anon_vma);
  138. }
  139. static void anon_vma_ctor(void *data, struct kmem_cache *cachep,
  140. unsigned long flags)
  141. {
  142. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  143. SLAB_CTOR_CONSTRUCTOR) {
  144. struct anon_vma *anon_vma = data;
  145. spin_lock_init(&anon_vma->lock);
  146. INIT_LIST_HEAD(&anon_vma->head);
  147. }
  148. }
  149. void __init anon_vma_init(void)
  150. {
  151. anon_vma_cachep = kmem_cache_create("anon_vma", sizeof(struct anon_vma),
  152. 0, SLAB_DESTROY_BY_RCU|SLAB_PANIC, anon_vma_ctor, NULL);
  153. }
  154. /*
  155. * Getting a lock on a stable anon_vma from a page off the LRU is
  156. * tricky: page_lock_anon_vma rely on RCU to guard against the races.
  157. */
  158. static struct anon_vma *page_lock_anon_vma(struct page *page)
  159. {
  160. struct anon_vma *anon_vma = NULL;
  161. unsigned long anon_mapping;
  162. rcu_read_lock();
  163. anon_mapping = (unsigned long) page->mapping;
  164. if (!(anon_mapping & PAGE_MAPPING_ANON))
  165. goto out;
  166. if (!page_mapped(page))
  167. goto out;
  168. anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON);
  169. spin_lock(&anon_vma->lock);
  170. out:
  171. rcu_read_unlock();
  172. return anon_vma;
  173. }
  174. /*
  175. * At what user virtual address is page expected in vma?
  176. */
  177. static inline unsigned long
  178. vma_address(struct page *page, struct vm_area_struct *vma)
  179. {
  180. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  181. unsigned long address;
  182. address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
  183. if (unlikely(address < vma->vm_start || address >= vma->vm_end)) {
  184. /* page should be within any vma from prio_tree_next */
  185. BUG_ON(!PageAnon(page));
  186. return -EFAULT;
  187. }
  188. return address;
  189. }
  190. /*
  191. * At what user virtual address is page expected in vma? checking that the
  192. * page matches the vma: currently only used on anon pages, by unuse_vma;
  193. */
  194. unsigned long page_address_in_vma(struct page *page, struct vm_area_struct *vma)
  195. {
  196. if (PageAnon(page)) {
  197. if ((void *)vma->anon_vma !=
  198. (void *)page->mapping - PAGE_MAPPING_ANON)
  199. return -EFAULT;
  200. } else if (page->mapping && !(vma->vm_flags & VM_NONLINEAR)) {
  201. if (!vma->vm_file ||
  202. vma->vm_file->f_mapping != page->mapping)
  203. return -EFAULT;
  204. } else
  205. return -EFAULT;
  206. return vma_address(page, vma);
  207. }
  208. /*
  209. * Check that @page is mapped at @address into @mm.
  210. *
  211. * On success returns with pte mapped and locked.
  212. */
  213. pte_t *page_check_address(struct page *page, struct mm_struct *mm,
  214. unsigned long address, spinlock_t **ptlp)
  215. {
  216. pgd_t *pgd;
  217. pud_t *pud;
  218. pmd_t *pmd;
  219. pte_t *pte;
  220. spinlock_t *ptl;
  221. pgd = pgd_offset(mm, address);
  222. if (!pgd_present(*pgd))
  223. return NULL;
  224. pud = pud_offset(pgd, address);
  225. if (!pud_present(*pud))
  226. return NULL;
  227. pmd = pmd_offset(pud, address);
  228. if (!pmd_present(*pmd))
  229. return NULL;
  230. pte = pte_offset_map(pmd, address);
  231. /* Make a quick check before getting the lock */
  232. if (!pte_present(*pte)) {
  233. pte_unmap(pte);
  234. return NULL;
  235. }
  236. ptl = pte_lockptr(mm, pmd);
  237. spin_lock(ptl);
  238. if (pte_present(*pte) && page_to_pfn(page) == pte_pfn(*pte)) {
  239. *ptlp = ptl;
  240. return pte;
  241. }
  242. pte_unmap_unlock(pte, ptl);
  243. return NULL;
  244. }
  245. /*
  246. * Subfunctions of page_referenced: page_referenced_one called
  247. * repeatedly from either page_referenced_anon or page_referenced_file.
  248. */
  249. static int page_referenced_one(struct page *page,
  250. struct vm_area_struct *vma, unsigned int *mapcount)
  251. {
  252. struct mm_struct *mm = vma->vm_mm;
  253. unsigned long address;
  254. pte_t *pte;
  255. spinlock_t *ptl;
  256. int referenced = 0;
  257. address = vma_address(page, vma);
  258. if (address == -EFAULT)
  259. goto out;
  260. pte = page_check_address(page, mm, address, &ptl);
  261. if (!pte)
  262. goto out;
  263. if (ptep_clear_flush_young(vma, address, pte))
  264. referenced++;
  265. /* Pretend the page is referenced if the task has the
  266. swap token and is in the middle of a page fault. */
  267. if (mm != current->mm && has_swap_token(mm) &&
  268. rwsem_is_locked(&mm->mmap_sem))
  269. referenced++;
  270. (*mapcount)--;
  271. pte_unmap_unlock(pte, ptl);
  272. out:
  273. return referenced;
  274. }
  275. static int page_referenced_anon(struct page *page)
  276. {
  277. unsigned int mapcount;
  278. struct anon_vma *anon_vma;
  279. struct vm_area_struct *vma;
  280. int referenced = 0;
  281. anon_vma = page_lock_anon_vma(page);
  282. if (!anon_vma)
  283. return referenced;
  284. mapcount = page_mapcount(page);
  285. list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
  286. referenced += page_referenced_one(page, vma, &mapcount);
  287. if (!mapcount)
  288. break;
  289. }
  290. spin_unlock(&anon_vma->lock);
  291. return referenced;
  292. }
  293. /**
  294. * page_referenced_file - referenced check for object-based rmap
  295. * @page: the page we're checking references on.
  296. *
  297. * For an object-based mapped page, find all the places it is mapped and
  298. * check/clear the referenced flag. This is done by following the page->mapping
  299. * pointer, then walking the chain of vmas it holds. It returns the number
  300. * of references it found.
  301. *
  302. * This function is only called from page_referenced for object-based pages.
  303. */
  304. static int page_referenced_file(struct page *page)
  305. {
  306. unsigned int mapcount;
  307. struct address_space *mapping = page->mapping;
  308. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  309. struct vm_area_struct *vma;
  310. struct prio_tree_iter iter;
  311. int referenced = 0;
  312. /*
  313. * The caller's checks on page->mapping and !PageAnon have made
  314. * sure that this is a file page: the check for page->mapping
  315. * excludes the case just before it gets set on an anon page.
  316. */
  317. BUG_ON(PageAnon(page));
  318. /*
  319. * The page lock not only makes sure that page->mapping cannot
  320. * suddenly be NULLified by truncation, it makes sure that the
  321. * structure at mapping cannot be freed and reused yet,
  322. * so we can safely take mapping->i_mmap_lock.
  323. */
  324. BUG_ON(!PageLocked(page));
  325. spin_lock(&mapping->i_mmap_lock);
  326. /*
  327. * i_mmap_lock does not stabilize mapcount at all, but mapcount
  328. * is more likely to be accurate if we note it after spinning.
  329. */
  330. mapcount = page_mapcount(page);
  331. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  332. if ((vma->vm_flags & (VM_LOCKED|VM_MAYSHARE))
  333. == (VM_LOCKED|VM_MAYSHARE)) {
  334. referenced++;
  335. break;
  336. }
  337. referenced += page_referenced_one(page, vma, &mapcount);
  338. if (!mapcount)
  339. break;
  340. }
  341. spin_unlock(&mapping->i_mmap_lock);
  342. return referenced;
  343. }
  344. /**
  345. * page_referenced - test if the page was referenced
  346. * @page: the page to test
  347. * @is_locked: caller holds lock on the page
  348. *
  349. * Quick test_and_clear_referenced for all mappings to a page,
  350. * returns the number of ptes which referenced the page.
  351. */
  352. int page_referenced(struct page *page, int is_locked)
  353. {
  354. int referenced = 0;
  355. if (page_test_and_clear_young(page))
  356. referenced++;
  357. if (TestClearPageReferenced(page))
  358. referenced++;
  359. if (page_mapped(page) && page->mapping) {
  360. if (PageAnon(page))
  361. referenced += page_referenced_anon(page);
  362. else if (is_locked)
  363. referenced += page_referenced_file(page);
  364. else if (TestSetPageLocked(page))
  365. referenced++;
  366. else {
  367. if (page->mapping)
  368. referenced += page_referenced_file(page);
  369. unlock_page(page);
  370. }
  371. }
  372. return referenced;
  373. }
  374. static int page_mkclean_one(struct page *page, struct vm_area_struct *vma)
  375. {
  376. struct mm_struct *mm = vma->vm_mm;
  377. unsigned long address;
  378. pte_t *pte, entry;
  379. spinlock_t *ptl;
  380. int ret = 0;
  381. address = vma_address(page, vma);
  382. if (address == -EFAULT)
  383. goto out;
  384. pte = page_check_address(page, mm, address, &ptl);
  385. if (!pte)
  386. goto out;
  387. if (!pte_dirty(*pte) && !pte_write(*pte))
  388. goto unlock;
  389. entry = ptep_get_and_clear(mm, address, pte);
  390. entry = pte_mkclean(entry);
  391. entry = pte_wrprotect(entry);
  392. ptep_establish(vma, address, pte, entry);
  393. lazy_mmu_prot_update(entry);
  394. ret = 1;
  395. unlock:
  396. pte_unmap_unlock(pte, ptl);
  397. out:
  398. return ret;
  399. }
  400. static int page_mkclean_file(struct address_space *mapping, struct page *page)
  401. {
  402. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  403. struct vm_area_struct *vma;
  404. struct prio_tree_iter iter;
  405. int ret = 0;
  406. BUG_ON(PageAnon(page));
  407. spin_lock(&mapping->i_mmap_lock);
  408. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  409. if (vma->vm_flags & VM_SHARED)
  410. ret += page_mkclean_one(page, vma);
  411. }
  412. spin_unlock(&mapping->i_mmap_lock);
  413. return ret;
  414. }
  415. int page_mkclean(struct page *page)
  416. {
  417. int ret = 0;
  418. BUG_ON(!PageLocked(page));
  419. if (page_mapped(page)) {
  420. struct address_space *mapping = page_mapping(page);
  421. if (mapping)
  422. ret = page_mkclean_file(mapping, page);
  423. }
  424. return ret;
  425. }
  426. /**
  427. * page_set_anon_rmap - setup new anonymous rmap
  428. * @page: the page to add the mapping to
  429. * @vma: the vm area in which the mapping is added
  430. * @address: the user virtual address mapped
  431. */
  432. static void __page_set_anon_rmap(struct page *page,
  433. struct vm_area_struct *vma, unsigned long address)
  434. {
  435. struct anon_vma *anon_vma = vma->anon_vma;
  436. BUG_ON(!anon_vma);
  437. anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
  438. page->mapping = (struct address_space *) anon_vma;
  439. page->index = linear_page_index(vma, address);
  440. /*
  441. * nr_mapped state can be updated without turning off
  442. * interrupts because it is not modified via interrupt.
  443. */
  444. __inc_zone_page_state(page, NR_ANON_PAGES);
  445. }
  446. /**
  447. * page_add_anon_rmap - add pte mapping to an anonymous page
  448. * @page: the page to add the mapping to
  449. * @vma: the vm area in which the mapping is added
  450. * @address: the user virtual address mapped
  451. *
  452. * The caller needs to hold the pte lock.
  453. */
  454. void page_add_anon_rmap(struct page *page,
  455. struct vm_area_struct *vma, unsigned long address)
  456. {
  457. if (atomic_inc_and_test(&page->_mapcount))
  458. __page_set_anon_rmap(page, vma, address);
  459. /* else checking page index and mapping is racy */
  460. }
  461. /*
  462. * page_add_new_anon_rmap - add pte mapping to a new anonymous page
  463. * @page: the page to add the mapping to
  464. * @vma: the vm area in which the mapping is added
  465. * @address: the user virtual address mapped
  466. *
  467. * Same as page_add_anon_rmap but must only be called on *new* pages.
  468. * This means the inc-and-test can be bypassed.
  469. */
  470. void page_add_new_anon_rmap(struct page *page,
  471. struct vm_area_struct *vma, unsigned long address)
  472. {
  473. atomic_set(&page->_mapcount, 0); /* elevate count by 1 (starts at -1) */
  474. __page_set_anon_rmap(page, vma, address);
  475. }
  476. /**
  477. * page_add_file_rmap - add pte mapping to a file page
  478. * @page: the page to add the mapping to
  479. *
  480. * The caller needs to hold the pte lock.
  481. */
  482. void page_add_file_rmap(struct page *page)
  483. {
  484. if (atomic_inc_and_test(&page->_mapcount))
  485. __inc_zone_page_state(page, NR_FILE_MAPPED);
  486. }
  487. /**
  488. * page_remove_rmap - take down pte mapping from a page
  489. * @page: page to remove mapping from
  490. *
  491. * The caller needs to hold the pte lock.
  492. */
  493. void page_remove_rmap(struct page *page)
  494. {
  495. if (atomic_add_negative(-1, &page->_mapcount)) {
  496. if (unlikely(page_mapcount(page) < 0)) {
  497. printk (KERN_EMERG "Eeek! page_mapcount(page) went negative! (%d)\n", page_mapcount(page));
  498. printk (KERN_EMERG " page->flags = %lx\n", page->flags);
  499. printk (KERN_EMERG " page->count = %x\n", page_count(page));
  500. printk (KERN_EMERG " page->mapping = %p\n", page->mapping);
  501. BUG();
  502. }
  503. /*
  504. * It would be tidy to reset the PageAnon mapping here,
  505. * but that might overwrite a racing page_add_anon_rmap
  506. * which increments mapcount after us but sets mapping
  507. * before us: so leave the reset to free_hot_cold_page,
  508. * and remember that it's only reliable while mapped.
  509. * Leaving it set also helps swapoff to reinstate ptes
  510. * faster for those pages still in swapcache.
  511. */
  512. if (page_test_and_clear_dirty(page))
  513. set_page_dirty(page);
  514. __dec_zone_page_state(page,
  515. PageAnon(page) ? NR_ANON_PAGES : NR_FILE_MAPPED);
  516. }
  517. }
  518. /*
  519. * Subfunctions of try_to_unmap: try_to_unmap_one called
  520. * repeatedly from either try_to_unmap_anon or try_to_unmap_file.
  521. */
  522. static int try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
  523. int migration)
  524. {
  525. struct mm_struct *mm = vma->vm_mm;
  526. unsigned long address;
  527. pte_t *pte;
  528. pte_t pteval;
  529. spinlock_t *ptl;
  530. int ret = SWAP_AGAIN;
  531. address = vma_address(page, vma);
  532. if (address == -EFAULT)
  533. goto out;
  534. pte = page_check_address(page, mm, address, &ptl);
  535. if (!pte)
  536. goto out;
  537. /*
  538. * If the page is mlock()d, we cannot swap it out.
  539. * If it's recently referenced (perhaps page_referenced
  540. * skipped over this mm) then we should reactivate it.
  541. */
  542. if (!migration && ((vma->vm_flags & VM_LOCKED) ||
  543. (ptep_clear_flush_young(vma, address, pte)))) {
  544. ret = SWAP_FAIL;
  545. goto out_unmap;
  546. }
  547. /* Nuke the page table entry. */
  548. flush_cache_page(vma, address, page_to_pfn(page));
  549. pteval = ptep_clear_flush(vma, address, pte);
  550. /* Move the dirty bit to the physical page now the pte is gone. */
  551. if (pte_dirty(pteval))
  552. set_page_dirty(page);
  553. /* Update high watermark before we lower rss */
  554. update_hiwater_rss(mm);
  555. if (PageAnon(page)) {
  556. swp_entry_t entry = { .val = page_private(page) };
  557. if (PageSwapCache(page)) {
  558. /*
  559. * Store the swap location in the pte.
  560. * See handle_pte_fault() ...
  561. */
  562. swap_duplicate(entry);
  563. if (list_empty(&mm->mmlist)) {
  564. spin_lock(&mmlist_lock);
  565. if (list_empty(&mm->mmlist))
  566. list_add(&mm->mmlist, &init_mm.mmlist);
  567. spin_unlock(&mmlist_lock);
  568. }
  569. dec_mm_counter(mm, anon_rss);
  570. #ifdef CONFIG_MIGRATION
  571. } else {
  572. /*
  573. * Store the pfn of the page in a special migration
  574. * pte. do_swap_page() will wait until the migration
  575. * pte is removed and then restart fault handling.
  576. */
  577. BUG_ON(!migration);
  578. entry = make_migration_entry(page, pte_write(pteval));
  579. #endif
  580. }
  581. set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
  582. BUG_ON(pte_file(*pte));
  583. } else
  584. #ifdef CONFIG_MIGRATION
  585. if (migration) {
  586. /* Establish migration entry for a file page */
  587. swp_entry_t entry;
  588. entry = make_migration_entry(page, pte_write(pteval));
  589. set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
  590. } else
  591. #endif
  592. dec_mm_counter(mm, file_rss);
  593. page_remove_rmap(page);
  594. page_cache_release(page);
  595. out_unmap:
  596. pte_unmap_unlock(pte, ptl);
  597. out:
  598. return ret;
  599. }
  600. /*
  601. * objrmap doesn't work for nonlinear VMAs because the assumption that
  602. * offset-into-file correlates with offset-into-virtual-addresses does not hold.
  603. * Consequently, given a particular page and its ->index, we cannot locate the
  604. * ptes which are mapping that page without an exhaustive linear search.
  605. *
  606. * So what this code does is a mini "virtual scan" of each nonlinear VMA which
  607. * maps the file to which the target page belongs. The ->vm_private_data field
  608. * holds the current cursor into that scan. Successive searches will circulate
  609. * around the vma's virtual address space.
  610. *
  611. * So as more replacement pressure is applied to the pages in a nonlinear VMA,
  612. * more scanning pressure is placed against them as well. Eventually pages
  613. * will become fully unmapped and are eligible for eviction.
  614. *
  615. * For very sparsely populated VMAs this is a little inefficient - chances are
  616. * there there won't be many ptes located within the scan cluster. In this case
  617. * maybe we could scan further - to the end of the pte page, perhaps.
  618. */
  619. #define CLUSTER_SIZE min(32*PAGE_SIZE, PMD_SIZE)
  620. #define CLUSTER_MASK (~(CLUSTER_SIZE - 1))
  621. static void try_to_unmap_cluster(unsigned long cursor,
  622. unsigned int *mapcount, struct vm_area_struct *vma)
  623. {
  624. struct mm_struct *mm = vma->vm_mm;
  625. pgd_t *pgd;
  626. pud_t *pud;
  627. pmd_t *pmd;
  628. pte_t *pte;
  629. pte_t pteval;
  630. spinlock_t *ptl;
  631. struct page *page;
  632. unsigned long address;
  633. unsigned long end;
  634. address = (vma->vm_start + cursor) & CLUSTER_MASK;
  635. end = address + CLUSTER_SIZE;
  636. if (address < vma->vm_start)
  637. address = vma->vm_start;
  638. if (end > vma->vm_end)
  639. end = vma->vm_end;
  640. pgd = pgd_offset(mm, address);
  641. if (!pgd_present(*pgd))
  642. return;
  643. pud = pud_offset(pgd, address);
  644. if (!pud_present(*pud))
  645. return;
  646. pmd = pmd_offset(pud, address);
  647. if (!pmd_present(*pmd))
  648. return;
  649. pte = pte_offset_map_lock(mm, pmd, address, &ptl);
  650. /* Update high watermark before we lower rss */
  651. update_hiwater_rss(mm);
  652. for (; address < end; pte++, address += PAGE_SIZE) {
  653. if (!pte_present(*pte))
  654. continue;
  655. page = vm_normal_page(vma, address, *pte);
  656. BUG_ON(!page || PageAnon(page));
  657. if (ptep_clear_flush_young(vma, address, pte))
  658. continue;
  659. /* Nuke the page table entry. */
  660. flush_cache_page(vma, address, pte_pfn(*pte));
  661. pteval = ptep_clear_flush(vma, address, pte);
  662. /* If nonlinear, store the file page offset in the pte. */
  663. if (page->index != linear_page_index(vma, address))
  664. set_pte_at(mm, address, pte, pgoff_to_pte(page->index));
  665. /* Move the dirty bit to the physical page now the pte is gone. */
  666. if (pte_dirty(pteval))
  667. set_page_dirty(page);
  668. page_remove_rmap(page);
  669. page_cache_release(page);
  670. dec_mm_counter(mm, file_rss);
  671. (*mapcount)--;
  672. }
  673. pte_unmap_unlock(pte - 1, ptl);
  674. }
  675. static int try_to_unmap_anon(struct page *page, int migration)
  676. {
  677. struct anon_vma *anon_vma;
  678. struct vm_area_struct *vma;
  679. int ret = SWAP_AGAIN;
  680. anon_vma = page_lock_anon_vma(page);
  681. if (!anon_vma)
  682. return ret;
  683. list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
  684. ret = try_to_unmap_one(page, vma, migration);
  685. if (ret == SWAP_FAIL || !page_mapped(page))
  686. break;
  687. }
  688. spin_unlock(&anon_vma->lock);
  689. return ret;
  690. }
  691. /**
  692. * try_to_unmap_file - unmap file page using the object-based rmap method
  693. * @page: the page to unmap
  694. *
  695. * Find all the mappings of a page using the mapping pointer and the vma chains
  696. * contained in the address_space struct it points to.
  697. *
  698. * This function is only called from try_to_unmap for object-based pages.
  699. */
  700. static int try_to_unmap_file(struct page *page, int migration)
  701. {
  702. struct address_space *mapping = page->mapping;
  703. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  704. struct vm_area_struct *vma;
  705. struct prio_tree_iter iter;
  706. int ret = SWAP_AGAIN;
  707. unsigned long cursor;
  708. unsigned long max_nl_cursor = 0;
  709. unsigned long max_nl_size = 0;
  710. unsigned int mapcount;
  711. spin_lock(&mapping->i_mmap_lock);
  712. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  713. ret = try_to_unmap_one(page, vma, migration);
  714. if (ret == SWAP_FAIL || !page_mapped(page))
  715. goto out;
  716. }
  717. if (list_empty(&mapping->i_mmap_nonlinear))
  718. goto out;
  719. list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
  720. shared.vm_set.list) {
  721. if ((vma->vm_flags & VM_LOCKED) && !migration)
  722. continue;
  723. cursor = (unsigned long) vma->vm_private_data;
  724. if (cursor > max_nl_cursor)
  725. max_nl_cursor = cursor;
  726. cursor = vma->vm_end - vma->vm_start;
  727. if (cursor > max_nl_size)
  728. max_nl_size = cursor;
  729. }
  730. if (max_nl_size == 0) { /* any nonlinears locked or reserved */
  731. ret = SWAP_FAIL;
  732. goto out;
  733. }
  734. /*
  735. * We don't try to search for this page in the nonlinear vmas,
  736. * and page_referenced wouldn't have found it anyway. Instead
  737. * just walk the nonlinear vmas trying to age and unmap some.
  738. * The mapcount of the page we came in with is irrelevant,
  739. * but even so use it as a guide to how hard we should try?
  740. */
  741. mapcount = page_mapcount(page);
  742. if (!mapcount)
  743. goto out;
  744. cond_resched_lock(&mapping->i_mmap_lock);
  745. max_nl_size = (max_nl_size + CLUSTER_SIZE - 1) & CLUSTER_MASK;
  746. if (max_nl_cursor == 0)
  747. max_nl_cursor = CLUSTER_SIZE;
  748. do {
  749. list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
  750. shared.vm_set.list) {
  751. if ((vma->vm_flags & VM_LOCKED) && !migration)
  752. continue;
  753. cursor = (unsigned long) vma->vm_private_data;
  754. while ( cursor < max_nl_cursor &&
  755. cursor < vma->vm_end - vma->vm_start) {
  756. try_to_unmap_cluster(cursor, &mapcount, vma);
  757. cursor += CLUSTER_SIZE;
  758. vma->vm_private_data = (void *) cursor;
  759. if ((int)mapcount <= 0)
  760. goto out;
  761. }
  762. vma->vm_private_data = (void *) max_nl_cursor;
  763. }
  764. cond_resched_lock(&mapping->i_mmap_lock);
  765. max_nl_cursor += CLUSTER_SIZE;
  766. } while (max_nl_cursor <= max_nl_size);
  767. /*
  768. * Don't loop forever (perhaps all the remaining pages are
  769. * in locked vmas). Reset cursor on all unreserved nonlinear
  770. * vmas, now forgetting on which ones it had fallen behind.
  771. */
  772. list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
  773. vma->vm_private_data = NULL;
  774. out:
  775. spin_unlock(&mapping->i_mmap_lock);
  776. return ret;
  777. }
  778. /**
  779. * try_to_unmap - try to remove all page table mappings to a page
  780. * @page: the page to get unmapped
  781. *
  782. * Tries to remove all the page table entries which are mapping this
  783. * page, used in the pageout path. Caller must hold the page lock.
  784. * Return values are:
  785. *
  786. * SWAP_SUCCESS - we succeeded in removing all mappings
  787. * SWAP_AGAIN - we missed a mapping, try again later
  788. * SWAP_FAIL - the page is unswappable
  789. */
  790. int try_to_unmap(struct page *page, int migration)
  791. {
  792. int ret;
  793. BUG_ON(!PageLocked(page));
  794. if (PageAnon(page))
  795. ret = try_to_unmap_anon(page, migration);
  796. else
  797. ret = try_to_unmap_file(page, migration);
  798. if (!page_mapped(page))
  799. ret = SWAP_SUCCESS;
  800. return ret;
  801. }