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