rmap.c 35 KB

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