rmap.c 27 KB

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