rmap.c 35 KB

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