rmap.c 35 KB

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