rmap.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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 <linux/kallsyms.h>
  49. #include <linux/memcontrol.h>
  50. #include <linux/mmu_notifier.h>
  51. #include <asm/tlbflush.h>
  52. struct kmem_cache *anon_vma_cachep;
  53. /**
  54. * anon_vma_prepare - attach an anon_vma to a memory region
  55. * @vma: the memory region in question
  56. *
  57. * This makes sure the memory mapping described by 'vma' has
  58. * an 'anon_vma' attached to it, so that we can associate the
  59. * anonymous pages mapped into it with that anon_vma.
  60. *
  61. * The common case will be that we already have one, but if
  62. * if not we either need to find an adjacent mapping that we
  63. * can re-use the anon_vma from (very common when the only
  64. * reason for splitting a vma has been mprotect()), or we
  65. * allocate a new one.
  66. *
  67. * Anon-vma allocations are very subtle, because we may have
  68. * optimistically looked up an anon_vma in page_lock_anon_vma()
  69. * and that may actually touch the spinlock even in the newly
  70. * allocated vma (it depends on RCU to make sure that the
  71. * anon_vma isn't actually destroyed).
  72. *
  73. * As a result, we need to do proper anon_vma locking even
  74. * for the new allocation. At the same time, we do not want
  75. * to do any locking for the common case of already having
  76. * an anon_vma.
  77. *
  78. * This must be called with the mmap_sem held for reading.
  79. */
  80. int anon_vma_prepare(struct vm_area_struct *vma)
  81. {
  82. struct anon_vma *anon_vma = vma->anon_vma;
  83. might_sleep();
  84. if (unlikely(!anon_vma)) {
  85. struct mm_struct *mm = vma->vm_mm;
  86. struct anon_vma *allocated;
  87. anon_vma = find_mergeable_anon_vma(vma);
  88. allocated = NULL;
  89. if (!anon_vma) {
  90. anon_vma = anon_vma_alloc();
  91. if (unlikely(!anon_vma))
  92. return -ENOMEM;
  93. allocated = anon_vma;
  94. }
  95. spin_lock(&anon_vma->lock);
  96. /* page_table_lock to protect against threads */
  97. spin_lock(&mm->page_table_lock);
  98. if (likely(!vma->anon_vma)) {
  99. vma->anon_vma = anon_vma;
  100. list_add_tail(&vma->anon_vma_node, &anon_vma->head);
  101. allocated = NULL;
  102. }
  103. spin_unlock(&mm->page_table_lock);
  104. spin_unlock(&anon_vma->lock);
  105. if (unlikely(allocated))
  106. anon_vma_free(allocated);
  107. }
  108. return 0;
  109. }
  110. void __anon_vma_merge(struct vm_area_struct *vma, struct vm_area_struct *next)
  111. {
  112. BUG_ON(vma->anon_vma != next->anon_vma);
  113. list_del(&next->anon_vma_node);
  114. }
  115. void __anon_vma_link(struct vm_area_struct *vma)
  116. {
  117. struct anon_vma *anon_vma = vma->anon_vma;
  118. if (anon_vma)
  119. list_add_tail(&vma->anon_vma_node, &anon_vma->head);
  120. }
  121. void anon_vma_link(struct vm_area_struct *vma)
  122. {
  123. struct anon_vma *anon_vma = vma->anon_vma;
  124. if (anon_vma) {
  125. spin_lock(&anon_vma->lock);
  126. list_add_tail(&vma->anon_vma_node, &anon_vma->head);
  127. spin_unlock(&anon_vma->lock);
  128. }
  129. }
  130. void anon_vma_unlink(struct vm_area_struct *vma)
  131. {
  132. struct anon_vma *anon_vma = vma->anon_vma;
  133. int empty;
  134. if (!anon_vma)
  135. return;
  136. spin_lock(&anon_vma->lock);
  137. list_del(&vma->anon_vma_node);
  138. /* We must garbage collect the anon_vma if it's empty */
  139. empty = list_empty(&anon_vma->head);
  140. spin_unlock(&anon_vma->lock);
  141. if (empty)
  142. anon_vma_free(anon_vma);
  143. }
  144. static void anon_vma_ctor(void *data)
  145. {
  146. struct anon_vma *anon_vma = data;
  147. spin_lock_init(&anon_vma->lock);
  148. INIT_LIST_HEAD(&anon_vma->head);
  149. }
  150. void __init anon_vma_init(void)
  151. {
  152. anon_vma_cachep = kmem_cache_create("anon_vma", sizeof(struct anon_vma),
  153. 0, SLAB_DESTROY_BY_RCU|SLAB_PANIC, anon_vma_ctor);
  154. }
  155. /*
  156. * Getting a lock on a stable anon_vma from a page off the LRU is
  157. * tricky: page_lock_anon_vma rely on RCU to guard against the races.
  158. */
  159. static struct anon_vma *page_lock_anon_vma(struct page *page)
  160. {
  161. struct anon_vma *anon_vma;
  162. unsigned long anon_mapping;
  163. rcu_read_lock();
  164. anon_mapping = (unsigned long) page->mapping;
  165. if (!(anon_mapping & PAGE_MAPPING_ANON))
  166. goto out;
  167. if (!page_mapped(page))
  168. goto out;
  169. anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON);
  170. spin_lock(&anon_vma->lock);
  171. return anon_vma;
  172. out:
  173. rcu_read_unlock();
  174. return NULL;
  175. }
  176. static void page_unlock_anon_vma(struct anon_vma *anon_vma)
  177. {
  178. spin_unlock(&anon_vma->lock);
  179. rcu_read_unlock();
  180. }
  181. /*
  182. * At what user virtual address is page expected in @vma?
  183. * Returns virtual address or -EFAULT if page's index/offset is not
  184. * within the range mapped the @vma.
  185. */
  186. static inline unsigned long
  187. vma_address(struct page *page, struct vm_area_struct *vma)
  188. {
  189. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  190. unsigned long address;
  191. address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
  192. if (unlikely(address < vma->vm_start || address >= vma->vm_end)) {
  193. /* page should be within @vma mapping range */
  194. return -EFAULT;
  195. }
  196. return address;
  197. }
  198. /*
  199. * At what user virtual address is page expected in vma? checking that the
  200. * page matches the vma: currently only used on anon pages, by unuse_vma;
  201. */
  202. unsigned long page_address_in_vma(struct page *page, struct vm_area_struct *vma)
  203. {
  204. if (PageAnon(page)) {
  205. if ((void *)vma->anon_vma !=
  206. (void *)page->mapping - PAGE_MAPPING_ANON)
  207. return -EFAULT;
  208. } else if (page->mapping && !(vma->vm_flags & VM_NONLINEAR)) {
  209. if (!vma->vm_file ||
  210. vma->vm_file->f_mapping != page->mapping)
  211. return -EFAULT;
  212. } else
  213. return -EFAULT;
  214. return vma_address(page, vma);
  215. }
  216. /*
  217. * Check that @page is mapped at @address into @mm.
  218. *
  219. * If @sync is false, page_check_address may perform a racy check to avoid
  220. * the page table lock when the pte is not present (helpful when reclaiming
  221. * highly shared pages).
  222. *
  223. * On success returns with pte mapped and locked.
  224. */
  225. pte_t *page_check_address(struct page *page, struct mm_struct *mm,
  226. unsigned long address, spinlock_t **ptlp, int sync)
  227. {
  228. pgd_t *pgd;
  229. pud_t *pud;
  230. pmd_t *pmd;
  231. pte_t *pte;
  232. spinlock_t *ptl;
  233. pgd = pgd_offset(mm, address);
  234. if (!pgd_present(*pgd))
  235. return NULL;
  236. pud = pud_offset(pgd, address);
  237. if (!pud_present(*pud))
  238. return NULL;
  239. pmd = pmd_offset(pud, address);
  240. if (!pmd_present(*pmd))
  241. return NULL;
  242. pte = pte_offset_map(pmd, address);
  243. /* Make a quick check before getting the lock */
  244. if (!sync && !pte_present(*pte)) {
  245. pte_unmap(pte);
  246. return NULL;
  247. }
  248. ptl = pte_lockptr(mm, pmd);
  249. spin_lock(ptl);
  250. if (pte_present(*pte) && page_to_pfn(page) == pte_pfn(*pte)) {
  251. *ptlp = ptl;
  252. return pte;
  253. }
  254. pte_unmap_unlock(pte, ptl);
  255. return NULL;
  256. }
  257. /*
  258. * Subfunctions of page_referenced: page_referenced_one called
  259. * repeatedly from either page_referenced_anon or page_referenced_file.
  260. */
  261. static int page_referenced_one(struct page *page,
  262. struct vm_area_struct *vma, unsigned int *mapcount)
  263. {
  264. struct mm_struct *mm = vma->vm_mm;
  265. unsigned long address;
  266. pte_t *pte;
  267. spinlock_t *ptl;
  268. int referenced = 0;
  269. address = vma_address(page, vma);
  270. if (address == -EFAULT)
  271. goto out;
  272. pte = page_check_address(page, mm, address, &ptl, 0);
  273. if (!pte)
  274. goto out;
  275. if (vma->vm_flags & VM_LOCKED) {
  276. referenced++;
  277. *mapcount = 1; /* break early from loop */
  278. } else if (ptep_clear_flush_young_notify(vma, address, pte))
  279. referenced++;
  280. /* Pretend the page is referenced if the task has the
  281. swap token and is in the middle of a page fault. */
  282. if (mm != current->mm && has_swap_token(mm) &&
  283. rwsem_is_locked(&mm->mmap_sem))
  284. referenced++;
  285. (*mapcount)--;
  286. pte_unmap_unlock(pte, ptl);
  287. out:
  288. return referenced;
  289. }
  290. static int page_referenced_anon(struct page *page,
  291. struct mem_cgroup *mem_cont)
  292. {
  293. unsigned int mapcount;
  294. struct anon_vma *anon_vma;
  295. struct vm_area_struct *vma;
  296. int referenced = 0;
  297. anon_vma = page_lock_anon_vma(page);
  298. if (!anon_vma)
  299. return referenced;
  300. mapcount = page_mapcount(page);
  301. list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
  302. /*
  303. * If we are reclaiming on behalf of a cgroup, skip
  304. * counting on behalf of references from different
  305. * cgroups
  306. */
  307. if (mem_cont && !mm_match_cgroup(vma->vm_mm, mem_cont))
  308. continue;
  309. referenced += page_referenced_one(page, vma, &mapcount);
  310. if (!mapcount)
  311. break;
  312. }
  313. page_unlock_anon_vma(anon_vma);
  314. return referenced;
  315. }
  316. /**
  317. * page_referenced_file - referenced check for object-based rmap
  318. * @page: the page we're checking references on.
  319. * @mem_cont: target memory controller
  320. *
  321. * For an object-based mapped page, find all the places it is mapped and
  322. * check/clear the referenced flag. This is done by following the page->mapping
  323. * pointer, then walking the chain of vmas it holds. It returns the number
  324. * of references it found.
  325. *
  326. * This function is only called from page_referenced for object-based pages.
  327. */
  328. static int page_referenced_file(struct page *page,
  329. struct mem_cgroup *mem_cont)
  330. {
  331. unsigned int mapcount;
  332. struct address_space *mapping = page->mapping;
  333. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  334. struct vm_area_struct *vma;
  335. struct prio_tree_iter iter;
  336. int referenced = 0;
  337. /*
  338. * The caller's checks on page->mapping and !PageAnon have made
  339. * sure that this is a file page: the check for page->mapping
  340. * excludes the case just before it gets set on an anon page.
  341. */
  342. BUG_ON(PageAnon(page));
  343. /*
  344. * The page lock not only makes sure that page->mapping cannot
  345. * suddenly be NULLified by truncation, it makes sure that the
  346. * structure at mapping cannot be freed and reused yet,
  347. * so we can safely take mapping->i_mmap_lock.
  348. */
  349. BUG_ON(!PageLocked(page));
  350. spin_lock(&mapping->i_mmap_lock);
  351. /*
  352. * i_mmap_lock does not stabilize mapcount at all, but mapcount
  353. * is more likely to be accurate if we note it after spinning.
  354. */
  355. mapcount = page_mapcount(page);
  356. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  357. /*
  358. * If we are reclaiming on behalf of a cgroup, skip
  359. * counting on behalf of references from different
  360. * cgroups
  361. */
  362. if (mem_cont && !mm_match_cgroup(vma->vm_mm, mem_cont))
  363. continue;
  364. if ((vma->vm_flags & (VM_LOCKED|VM_MAYSHARE))
  365. == (VM_LOCKED|VM_MAYSHARE)) {
  366. referenced++;
  367. break;
  368. }
  369. referenced += page_referenced_one(page, vma, &mapcount);
  370. if (!mapcount)
  371. break;
  372. }
  373. spin_unlock(&mapping->i_mmap_lock);
  374. return referenced;
  375. }
  376. /**
  377. * page_referenced - test if the page was referenced
  378. * @page: the page to test
  379. * @is_locked: caller holds lock on the page
  380. * @mem_cont: target memory controller
  381. *
  382. * Quick test_and_clear_referenced for all mappings to a page,
  383. * returns the number of ptes which referenced the page.
  384. */
  385. int page_referenced(struct page *page, int is_locked,
  386. struct mem_cgroup *mem_cont)
  387. {
  388. int referenced = 0;
  389. if (TestClearPageReferenced(page))
  390. referenced++;
  391. if (page_mapped(page) && page->mapping) {
  392. if (PageAnon(page))
  393. referenced += page_referenced_anon(page, mem_cont);
  394. else if (is_locked)
  395. referenced += page_referenced_file(page, mem_cont);
  396. else if (!trylock_page(page))
  397. referenced++;
  398. else {
  399. if (page->mapping)
  400. referenced +=
  401. page_referenced_file(page, mem_cont);
  402. unlock_page(page);
  403. }
  404. }
  405. if (page_test_and_clear_young(page))
  406. referenced++;
  407. return referenced;
  408. }
  409. static int page_mkclean_one(struct page *page, struct vm_area_struct *vma)
  410. {
  411. struct mm_struct *mm = vma->vm_mm;
  412. unsigned long address;
  413. pte_t *pte;
  414. spinlock_t *ptl;
  415. int ret = 0;
  416. address = vma_address(page, vma);
  417. if (address == -EFAULT)
  418. goto out;
  419. pte = page_check_address(page, mm, address, &ptl, 1);
  420. if (!pte)
  421. goto out;
  422. if (pte_dirty(*pte) || pte_write(*pte)) {
  423. pte_t entry;
  424. flush_cache_page(vma, address, pte_pfn(*pte));
  425. entry = ptep_clear_flush_notify(vma, address, pte);
  426. entry = pte_wrprotect(entry);
  427. entry = pte_mkclean(entry);
  428. set_pte_at(mm, address, pte, entry);
  429. ret = 1;
  430. }
  431. pte_unmap_unlock(pte, ptl);
  432. out:
  433. return ret;
  434. }
  435. static int page_mkclean_file(struct address_space *mapping, struct page *page)
  436. {
  437. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  438. struct vm_area_struct *vma;
  439. struct prio_tree_iter iter;
  440. int ret = 0;
  441. BUG_ON(PageAnon(page));
  442. spin_lock(&mapping->i_mmap_lock);
  443. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  444. if (vma->vm_flags & VM_SHARED)
  445. ret += page_mkclean_one(page, vma);
  446. }
  447. spin_unlock(&mapping->i_mmap_lock);
  448. return ret;
  449. }
  450. int page_mkclean(struct page *page)
  451. {
  452. int ret = 0;
  453. BUG_ON(!PageLocked(page));
  454. if (page_mapped(page)) {
  455. struct address_space *mapping = page_mapping(page);
  456. if (mapping) {
  457. ret = page_mkclean_file(mapping, page);
  458. if (page_test_dirty(page)) {
  459. page_clear_dirty(page);
  460. ret = 1;
  461. }
  462. }
  463. }
  464. return ret;
  465. }
  466. EXPORT_SYMBOL_GPL(page_mkclean);
  467. /**
  468. * __page_set_anon_rmap - setup new anonymous rmap
  469. * @page: the page to add the mapping to
  470. * @vma: the vm area in which the mapping is added
  471. * @address: the user virtual address mapped
  472. */
  473. static void __page_set_anon_rmap(struct page *page,
  474. struct vm_area_struct *vma, unsigned long address)
  475. {
  476. struct anon_vma *anon_vma = vma->anon_vma;
  477. BUG_ON(!anon_vma);
  478. anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
  479. page->mapping = (struct address_space *) anon_vma;
  480. page->index = linear_page_index(vma, address);
  481. /*
  482. * nr_mapped state can be updated without turning off
  483. * interrupts because it is not modified via interrupt.
  484. */
  485. __inc_zone_page_state(page, NR_ANON_PAGES);
  486. }
  487. /**
  488. * __page_check_anon_rmap - sanity check anonymous rmap addition
  489. * @page: the page to add the mapping to
  490. * @vma: the vm area in which the mapping is added
  491. * @address: the user virtual address mapped
  492. */
  493. static void __page_check_anon_rmap(struct page *page,
  494. struct vm_area_struct *vma, unsigned long address)
  495. {
  496. #ifdef CONFIG_DEBUG_VM
  497. /*
  498. * The page's anon-rmap details (mapping and index) are guaranteed to
  499. * be set up correctly at this point.
  500. *
  501. * We have exclusion against page_add_anon_rmap because the caller
  502. * always holds the page locked, except if called from page_dup_rmap,
  503. * in which case the page is already known to be setup.
  504. *
  505. * We have exclusion against page_add_new_anon_rmap because those pages
  506. * are initially only visible via the pagetables, and the pte is locked
  507. * over the call to page_add_new_anon_rmap.
  508. */
  509. struct anon_vma *anon_vma = vma->anon_vma;
  510. anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
  511. BUG_ON(page->mapping != (struct address_space *)anon_vma);
  512. BUG_ON(page->index != linear_page_index(vma, address));
  513. #endif
  514. }
  515. /**
  516. * page_add_anon_rmap - add pte mapping to an anonymous page
  517. * @page: the page to add the mapping to
  518. * @vma: the vm area in which the mapping is added
  519. * @address: the user virtual address mapped
  520. *
  521. * The caller needs to hold the pte lock and the page must be locked.
  522. */
  523. void page_add_anon_rmap(struct page *page,
  524. struct vm_area_struct *vma, unsigned long address)
  525. {
  526. VM_BUG_ON(!PageLocked(page));
  527. VM_BUG_ON(address < vma->vm_start || address >= vma->vm_end);
  528. if (atomic_inc_and_test(&page->_mapcount))
  529. __page_set_anon_rmap(page, vma, address);
  530. else
  531. __page_check_anon_rmap(page, vma, address);
  532. }
  533. /**
  534. * page_add_new_anon_rmap - add pte mapping to a new anonymous page
  535. * @page: the page to add the mapping to
  536. * @vma: the vm area in which the mapping is added
  537. * @address: the user virtual address mapped
  538. *
  539. * Same as page_add_anon_rmap but must only be called on *new* pages.
  540. * This means the inc-and-test can be bypassed.
  541. * Page does not have to be locked.
  542. */
  543. void page_add_new_anon_rmap(struct page *page,
  544. struct vm_area_struct *vma, unsigned long address)
  545. {
  546. BUG_ON(address < vma->vm_start || address >= vma->vm_end);
  547. atomic_set(&page->_mapcount, 0); /* elevate count by 1 (starts at -1) */
  548. __page_set_anon_rmap(page, vma, address);
  549. }
  550. /**
  551. * page_add_file_rmap - add pte mapping to a file page
  552. * @page: the page to add the mapping to
  553. *
  554. * The caller needs to hold the pte lock.
  555. */
  556. void page_add_file_rmap(struct page *page)
  557. {
  558. if (atomic_inc_and_test(&page->_mapcount))
  559. __inc_zone_page_state(page, NR_FILE_MAPPED);
  560. }
  561. #ifdef CONFIG_DEBUG_VM
  562. /**
  563. * page_dup_rmap - duplicate pte mapping to a page
  564. * @page: the page to add the mapping to
  565. * @vma: the vm area being duplicated
  566. * @address: the user virtual address mapped
  567. *
  568. * For copy_page_range only: minimal extract from page_add_file_rmap /
  569. * page_add_anon_rmap, avoiding unnecessary tests (already checked) so it's
  570. * quicker.
  571. *
  572. * The caller needs to hold the pte lock.
  573. */
  574. void page_dup_rmap(struct page *page, struct vm_area_struct *vma, unsigned long address)
  575. {
  576. BUG_ON(page_mapcount(page) == 0);
  577. if (PageAnon(page))
  578. __page_check_anon_rmap(page, vma, address);
  579. atomic_inc(&page->_mapcount);
  580. }
  581. #endif
  582. /**
  583. * page_remove_rmap - take down pte mapping from a page
  584. * @page: page to remove mapping from
  585. * @vma: the vm area in which the mapping is removed
  586. *
  587. * The caller needs to hold the pte lock.
  588. */
  589. void page_remove_rmap(struct page *page, struct vm_area_struct *vma)
  590. {
  591. if (atomic_add_negative(-1, &page->_mapcount)) {
  592. if (unlikely(page_mapcount(page) < 0)) {
  593. printk (KERN_EMERG "Eeek! page_mapcount(page) went negative! (%d)\n", page_mapcount(page));
  594. printk (KERN_EMERG " page pfn = %lx\n", page_to_pfn(page));
  595. printk (KERN_EMERG " page->flags = %lx\n", page->flags);
  596. printk (KERN_EMERG " page->count = %x\n", page_count(page));
  597. printk (KERN_EMERG " page->mapping = %p\n", page->mapping);
  598. print_symbol (KERN_EMERG " vma->vm_ops = %s\n", (unsigned long)vma->vm_ops);
  599. if (vma->vm_ops) {
  600. print_symbol (KERN_EMERG " vma->vm_ops->fault = %s\n", (unsigned long)vma->vm_ops->fault);
  601. }
  602. if (vma->vm_file && vma->vm_file->f_op)
  603. print_symbol (KERN_EMERG " vma->vm_file->f_op->mmap = %s\n", (unsigned long)vma->vm_file->f_op->mmap);
  604. BUG();
  605. }
  606. /*
  607. * Now that the last pte has gone, s390 must transfer dirty
  608. * flag from storage key to struct page. We can usually skip
  609. * this if the page is anon, so about to be freed; but perhaps
  610. * not if it's in swapcache - there might be another pte slot
  611. * containing the swap entry, but page not yet written to swap.
  612. */
  613. if ((!PageAnon(page) || PageSwapCache(page)) &&
  614. page_test_dirty(page)) {
  615. page_clear_dirty(page);
  616. set_page_dirty(page);
  617. }
  618. mem_cgroup_uncharge_page(page);
  619. __dec_zone_page_state(page,
  620. PageAnon(page) ? NR_ANON_PAGES : NR_FILE_MAPPED);
  621. /*
  622. * It would be tidy to reset the PageAnon mapping here,
  623. * but that might overwrite a racing page_add_anon_rmap
  624. * which increments mapcount after us but sets mapping
  625. * before us: so leave the reset to free_hot_cold_page,
  626. * and remember that it's only reliable while mapped.
  627. * Leaving it set also helps swapoff to reinstate ptes
  628. * faster for those pages still in swapcache.
  629. */
  630. }
  631. }
  632. /*
  633. * Subfunctions of try_to_unmap: try_to_unmap_one called
  634. * repeatedly from either try_to_unmap_anon or try_to_unmap_file.
  635. */
  636. static int try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
  637. int migration)
  638. {
  639. struct mm_struct *mm = vma->vm_mm;
  640. unsigned long address;
  641. pte_t *pte;
  642. pte_t pteval;
  643. spinlock_t *ptl;
  644. int ret = SWAP_AGAIN;
  645. address = vma_address(page, vma);
  646. if (address == -EFAULT)
  647. goto out;
  648. pte = page_check_address(page, mm, address, &ptl, 0);
  649. if (!pte)
  650. goto out;
  651. /*
  652. * If the page is mlock()d, we cannot swap it out.
  653. * If it's recently referenced (perhaps page_referenced
  654. * skipped over this mm) then we should reactivate it.
  655. */
  656. if (!migration && ((vma->vm_flags & VM_LOCKED) ||
  657. (ptep_clear_flush_young_notify(vma, address, pte)))) {
  658. ret = SWAP_FAIL;
  659. goto out_unmap;
  660. }
  661. /* Nuke the page table entry. */
  662. flush_cache_page(vma, address, page_to_pfn(page));
  663. pteval = ptep_clear_flush_notify(vma, address, pte);
  664. /* Move the dirty bit to the physical page now the pte is gone. */
  665. if (pte_dirty(pteval))
  666. set_page_dirty(page);
  667. /* Update high watermark before we lower rss */
  668. update_hiwater_rss(mm);
  669. if (PageAnon(page)) {
  670. swp_entry_t entry = { .val = page_private(page) };
  671. if (PageSwapCache(page)) {
  672. /*
  673. * Store the swap location in the pte.
  674. * See handle_pte_fault() ...
  675. */
  676. swap_duplicate(entry);
  677. if (list_empty(&mm->mmlist)) {
  678. spin_lock(&mmlist_lock);
  679. if (list_empty(&mm->mmlist))
  680. list_add(&mm->mmlist, &init_mm.mmlist);
  681. spin_unlock(&mmlist_lock);
  682. }
  683. dec_mm_counter(mm, anon_rss);
  684. #ifdef CONFIG_MIGRATION
  685. } else {
  686. /*
  687. * Store the pfn of the page in a special migration
  688. * pte. do_swap_page() will wait until the migration
  689. * pte is removed and then restart fault handling.
  690. */
  691. BUG_ON(!migration);
  692. entry = make_migration_entry(page, pte_write(pteval));
  693. #endif
  694. }
  695. set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
  696. BUG_ON(pte_file(*pte));
  697. } else
  698. #ifdef CONFIG_MIGRATION
  699. if (migration) {
  700. /* Establish migration entry for a file page */
  701. swp_entry_t entry;
  702. entry = make_migration_entry(page, pte_write(pteval));
  703. set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
  704. } else
  705. #endif
  706. dec_mm_counter(mm, file_rss);
  707. page_remove_rmap(page, vma);
  708. page_cache_release(page);
  709. out_unmap:
  710. pte_unmap_unlock(pte, ptl);
  711. out:
  712. return ret;
  713. }
  714. /*
  715. * objrmap doesn't work for nonlinear VMAs because the assumption that
  716. * offset-into-file correlates with offset-into-virtual-addresses does not hold.
  717. * Consequently, given a particular page and its ->index, we cannot locate the
  718. * ptes which are mapping that page without an exhaustive linear search.
  719. *
  720. * So what this code does is a mini "virtual scan" of each nonlinear VMA which
  721. * maps the file to which the target page belongs. The ->vm_private_data field
  722. * holds the current cursor into that scan. Successive searches will circulate
  723. * around the vma's virtual address space.
  724. *
  725. * So as more replacement pressure is applied to the pages in a nonlinear VMA,
  726. * more scanning pressure is placed against them as well. Eventually pages
  727. * will become fully unmapped and are eligible for eviction.
  728. *
  729. * For very sparsely populated VMAs this is a little inefficient - chances are
  730. * there there won't be many ptes located within the scan cluster. In this case
  731. * maybe we could scan further - to the end of the pte page, perhaps.
  732. */
  733. #define CLUSTER_SIZE min(32*PAGE_SIZE, PMD_SIZE)
  734. #define CLUSTER_MASK (~(CLUSTER_SIZE - 1))
  735. static void try_to_unmap_cluster(unsigned long cursor,
  736. unsigned int *mapcount, struct vm_area_struct *vma)
  737. {
  738. struct mm_struct *mm = vma->vm_mm;
  739. pgd_t *pgd;
  740. pud_t *pud;
  741. pmd_t *pmd;
  742. pte_t *pte;
  743. pte_t pteval;
  744. spinlock_t *ptl;
  745. struct page *page;
  746. unsigned long address;
  747. unsigned long end;
  748. address = (vma->vm_start + cursor) & CLUSTER_MASK;
  749. end = address + CLUSTER_SIZE;
  750. if (address < vma->vm_start)
  751. address = vma->vm_start;
  752. if (end > vma->vm_end)
  753. end = vma->vm_end;
  754. pgd = pgd_offset(mm, address);
  755. if (!pgd_present(*pgd))
  756. return;
  757. pud = pud_offset(pgd, address);
  758. if (!pud_present(*pud))
  759. return;
  760. pmd = pmd_offset(pud, address);
  761. if (!pmd_present(*pmd))
  762. return;
  763. pte = pte_offset_map_lock(mm, pmd, address, &ptl);
  764. /* Update high watermark before we lower rss */
  765. update_hiwater_rss(mm);
  766. for (; address < end; pte++, address += PAGE_SIZE) {
  767. if (!pte_present(*pte))
  768. continue;
  769. page = vm_normal_page(vma, address, *pte);
  770. BUG_ON(!page || PageAnon(page));
  771. if (ptep_clear_flush_young_notify(vma, address, pte))
  772. continue;
  773. /* Nuke the page table entry. */
  774. flush_cache_page(vma, address, pte_pfn(*pte));
  775. pteval = ptep_clear_flush_notify(vma, address, pte);
  776. /* If nonlinear, store the file page offset in the pte. */
  777. if (page->index != linear_page_index(vma, address))
  778. set_pte_at(mm, address, pte, pgoff_to_pte(page->index));
  779. /* Move the dirty bit to the physical page now the pte is gone. */
  780. if (pte_dirty(pteval))
  781. set_page_dirty(page);
  782. page_remove_rmap(page, vma);
  783. page_cache_release(page);
  784. dec_mm_counter(mm, file_rss);
  785. (*mapcount)--;
  786. }
  787. pte_unmap_unlock(pte - 1, ptl);
  788. }
  789. static int try_to_unmap_anon(struct page *page, int migration)
  790. {
  791. struct anon_vma *anon_vma;
  792. struct vm_area_struct *vma;
  793. int ret = SWAP_AGAIN;
  794. anon_vma = page_lock_anon_vma(page);
  795. if (!anon_vma)
  796. return ret;
  797. list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
  798. ret = try_to_unmap_one(page, vma, migration);
  799. if (ret == SWAP_FAIL || !page_mapped(page))
  800. break;
  801. }
  802. page_unlock_anon_vma(anon_vma);
  803. return ret;
  804. }
  805. /**
  806. * try_to_unmap_file - unmap file page using the object-based rmap method
  807. * @page: the page to unmap
  808. * @migration: migration flag
  809. *
  810. * Find all the mappings of a page using the mapping pointer and the vma chains
  811. * contained in the address_space struct it points to.
  812. *
  813. * This function is only called from try_to_unmap for object-based pages.
  814. */
  815. static int try_to_unmap_file(struct page *page, int migration)
  816. {
  817. struct address_space *mapping = page->mapping;
  818. pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  819. struct vm_area_struct *vma;
  820. struct prio_tree_iter iter;
  821. int ret = SWAP_AGAIN;
  822. unsigned long cursor;
  823. unsigned long max_nl_cursor = 0;
  824. unsigned long max_nl_size = 0;
  825. unsigned int mapcount;
  826. spin_lock(&mapping->i_mmap_lock);
  827. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  828. ret = try_to_unmap_one(page, vma, migration);
  829. if (ret == SWAP_FAIL || !page_mapped(page))
  830. goto out;
  831. }
  832. if (list_empty(&mapping->i_mmap_nonlinear))
  833. goto out;
  834. list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
  835. shared.vm_set.list) {
  836. if ((vma->vm_flags & VM_LOCKED) && !migration)
  837. continue;
  838. cursor = (unsigned long) vma->vm_private_data;
  839. if (cursor > max_nl_cursor)
  840. max_nl_cursor = cursor;
  841. cursor = vma->vm_end - vma->vm_start;
  842. if (cursor > max_nl_size)
  843. max_nl_size = cursor;
  844. }
  845. if (max_nl_size == 0) { /* any nonlinears locked or reserved */
  846. ret = SWAP_FAIL;
  847. goto out;
  848. }
  849. /*
  850. * We don't try to search for this page in the nonlinear vmas,
  851. * and page_referenced wouldn't have found it anyway. Instead
  852. * just walk the nonlinear vmas trying to age and unmap some.
  853. * The mapcount of the page we came in with is irrelevant,
  854. * but even so use it as a guide to how hard we should try?
  855. */
  856. mapcount = page_mapcount(page);
  857. if (!mapcount)
  858. goto out;
  859. cond_resched_lock(&mapping->i_mmap_lock);
  860. max_nl_size = (max_nl_size + CLUSTER_SIZE - 1) & CLUSTER_MASK;
  861. if (max_nl_cursor == 0)
  862. max_nl_cursor = CLUSTER_SIZE;
  863. do {
  864. list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
  865. shared.vm_set.list) {
  866. if ((vma->vm_flags & VM_LOCKED) && !migration)
  867. continue;
  868. cursor = (unsigned long) vma->vm_private_data;
  869. while ( cursor < max_nl_cursor &&
  870. cursor < vma->vm_end - vma->vm_start) {
  871. try_to_unmap_cluster(cursor, &mapcount, vma);
  872. cursor += CLUSTER_SIZE;
  873. vma->vm_private_data = (void *) cursor;
  874. if ((int)mapcount <= 0)
  875. goto out;
  876. }
  877. vma->vm_private_data = (void *) max_nl_cursor;
  878. }
  879. cond_resched_lock(&mapping->i_mmap_lock);
  880. max_nl_cursor += CLUSTER_SIZE;
  881. } while (max_nl_cursor <= max_nl_size);
  882. /*
  883. * Don't loop forever (perhaps all the remaining pages are
  884. * in locked vmas). Reset cursor on all unreserved nonlinear
  885. * vmas, now forgetting on which ones it had fallen behind.
  886. */
  887. list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
  888. vma->vm_private_data = NULL;
  889. out:
  890. spin_unlock(&mapping->i_mmap_lock);
  891. return ret;
  892. }
  893. /**
  894. * try_to_unmap - try to remove all page table mappings to a page
  895. * @page: the page to get unmapped
  896. * @migration: migration flag
  897. *
  898. * Tries to remove all the page table entries which are mapping this
  899. * page, used in the pageout path. Caller must hold the page lock.
  900. * Return values are:
  901. *
  902. * SWAP_SUCCESS - we succeeded in removing all mappings
  903. * SWAP_AGAIN - we missed a mapping, try again later
  904. * SWAP_FAIL - the page is unswappable
  905. */
  906. int try_to_unmap(struct page *page, int migration)
  907. {
  908. int ret;
  909. BUG_ON(!PageLocked(page));
  910. if (PageAnon(page))
  911. ret = try_to_unmap_anon(page, migration);
  912. else
  913. ret = try_to_unmap_file(page, migration);
  914. if (!page_mapped(page))
  915. ret = SWAP_SUCCESS;
  916. return ret;
  917. }