rmap.c 35 KB

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