rmap.c 27 KB

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