rmap.c 28 KB

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