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