rmap.c 34 KB

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