ipv6: reallocate addrconf router for ipv6 address when lo device up
[linux/fpc-iii.git] / mm / rmap.c
blob695eaff55d77c3a64e594fe273939c0ad11a78f5
1 /*
2 * mm/rmap.c - physical to virtual reverse mappings
4 * Copyright 2001, Rik van Riel <riel@conectiva.com.br>
5 * Released under the General Public License (GPL).
7 * Simple, low overhead reverse mapping scheme.
8 * Please try to keep this thing as modular as possible.
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.
14 * Original design by Rik van Riel <riel@conectiva.com.br> 2001
15 * File methods by Dave McCracken <dmccr@us.ibm.com> 2003, 2004
16 * Anonymous methods by Andrea Arcangeli <andrea@suse.de> 2004
17 * Contributions by Hugh Dickins 2003, 2004
21 * Lock ordering in mm:
23 * inode->i_mutex (while writing or truncating, not reading or faulting)
24 * mm->mmap_sem
25 * page->flags PG_locked (lock_page)
26 * mapping->i_mmap_mutex
27 * anon_vma->mutex
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->i_lock (in set_page_dirty's __mark_inode_dirty)
34 * bdi.wb->list_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 bdi.wb->list_lock in __sync_single_inode)
40 * anon_vma->mutex,mapping->i_mutex (memory_failure, collect_procs_anon)
41 * ->tasklist_lock
42 * pte map lock
45 #include <linux/mm.h>
46 #include <linux/pagemap.h>
47 #include <linux/swap.h>
48 #include <linux/swapops.h>
49 #include <linux/slab.h>
50 #include <linux/init.h>
51 #include <linux/ksm.h>
52 #include <linux/rmap.h>
53 #include <linux/rcupdate.h>
54 #include <linux/export.h>
55 #include <linux/memcontrol.h>
56 #include <linux/mmu_notifier.h>
57 #include <linux/migrate.h>
58 #include <linux/hugetlb.h>
59 #include <linux/backing-dev.h>
61 #include <asm/tlbflush.h>
63 #include "internal.h"
65 static struct kmem_cache *anon_vma_cachep;
66 static struct kmem_cache *anon_vma_chain_cachep;
68 static inline struct anon_vma *anon_vma_alloc(void)
70 struct anon_vma *anon_vma;
72 anon_vma = kmem_cache_alloc(anon_vma_cachep, GFP_KERNEL);
73 if (anon_vma) {
74 atomic_set(&anon_vma->refcount, 1);
76 * Initialise the anon_vma root to point to itself. If called
77 * from fork, the root will be reset to the parents anon_vma.
79 anon_vma->root = anon_vma;
82 return anon_vma;
85 static inline void anon_vma_free(struct anon_vma *anon_vma)
87 VM_BUG_ON(atomic_read(&anon_vma->refcount));
90 * Synchronize against page_lock_anon_vma() such that
91 * we can safely hold the lock without the anon_vma getting
92 * freed.
94 * Relies on the full mb implied by the atomic_dec_and_test() from
95 * put_anon_vma() against the acquire barrier implied by
96 * mutex_trylock() from page_lock_anon_vma(). This orders:
98 * page_lock_anon_vma() VS put_anon_vma()
99 * mutex_trylock() atomic_dec_and_test()
100 * LOCK MB
101 * atomic_read() mutex_is_locked()
103 * LOCK should suffice since the actual taking of the lock must
104 * happen _before_ what follows.
106 might_sleep();
107 if (mutex_is_locked(&anon_vma->root->mutex)) {
108 anon_vma_lock(anon_vma);
109 anon_vma_unlock(anon_vma);
112 kmem_cache_free(anon_vma_cachep, anon_vma);
115 static inline struct anon_vma_chain *anon_vma_chain_alloc(gfp_t gfp)
117 return kmem_cache_alloc(anon_vma_chain_cachep, gfp);
120 static void anon_vma_chain_free(struct anon_vma_chain *anon_vma_chain)
122 kmem_cache_free(anon_vma_chain_cachep, anon_vma_chain);
125 static void anon_vma_chain_link(struct vm_area_struct *vma,
126 struct anon_vma_chain *avc,
127 struct anon_vma *anon_vma)
129 avc->vma = vma;
130 avc->anon_vma = anon_vma;
131 list_add(&avc->same_vma, &vma->anon_vma_chain);
134 * It's critical to add new vmas to the tail of the anon_vma,
135 * see comment in huge_memory.c:__split_huge_page().
137 list_add_tail(&avc->same_anon_vma, &anon_vma->head);
141 * anon_vma_prepare - attach an anon_vma to a memory region
142 * @vma: the memory region in question
144 * This makes sure the memory mapping described by 'vma' has
145 * an 'anon_vma' attached to it, so that we can associate the
146 * anonymous pages mapped into it with that anon_vma.
148 * The common case will be that we already have one, but if
149 * not we either need to find an adjacent mapping that we
150 * can re-use the anon_vma from (very common when the only
151 * reason for splitting a vma has been mprotect()), or we
152 * allocate a new one.
154 * Anon-vma allocations are very subtle, because we may have
155 * optimistically looked up an anon_vma in page_lock_anon_vma()
156 * and that may actually touch the spinlock even in the newly
157 * allocated vma (it depends on RCU to make sure that the
158 * anon_vma isn't actually destroyed).
160 * As a result, we need to do proper anon_vma locking even
161 * for the new allocation. At the same time, we do not want
162 * to do any locking for the common case of already having
163 * an anon_vma.
165 * This must be called with the mmap_sem held for reading.
167 int anon_vma_prepare(struct vm_area_struct *vma)
169 struct anon_vma *anon_vma = vma->anon_vma;
170 struct anon_vma_chain *avc;
172 might_sleep();
173 if (unlikely(!anon_vma)) {
174 struct mm_struct *mm = vma->vm_mm;
175 struct anon_vma *allocated;
177 avc = anon_vma_chain_alloc(GFP_KERNEL);
178 if (!avc)
179 goto out_enomem;
181 anon_vma = find_mergeable_anon_vma(vma);
182 allocated = NULL;
183 if (!anon_vma) {
184 anon_vma = anon_vma_alloc();
185 if (unlikely(!anon_vma))
186 goto out_enomem_free_avc;
187 allocated = anon_vma;
190 anon_vma_lock(anon_vma);
191 /* page_table_lock to protect against threads */
192 spin_lock(&mm->page_table_lock);
193 if (likely(!vma->anon_vma)) {
194 vma->anon_vma = anon_vma;
195 anon_vma_chain_link(vma, avc, anon_vma);
196 allocated = NULL;
197 avc = NULL;
199 spin_unlock(&mm->page_table_lock);
200 anon_vma_unlock(anon_vma);
202 if (unlikely(allocated))
203 put_anon_vma(allocated);
204 if (unlikely(avc))
205 anon_vma_chain_free(avc);
207 return 0;
209 out_enomem_free_avc:
210 anon_vma_chain_free(avc);
211 out_enomem:
212 return -ENOMEM;
216 * This is a useful helper function for locking the anon_vma root as
217 * we traverse the vma->anon_vma_chain, looping over anon_vma's that
218 * have the same vma.
220 * Such anon_vma's should have the same root, so you'd expect to see
221 * just a single mutex_lock for the whole traversal.
223 static inline struct anon_vma *lock_anon_vma_root(struct anon_vma *root, struct anon_vma *anon_vma)
225 struct anon_vma *new_root = anon_vma->root;
226 if (new_root != root) {
227 if (WARN_ON_ONCE(root))
228 mutex_unlock(&root->mutex);
229 root = new_root;
230 mutex_lock(&root->mutex);
232 return root;
235 static inline void unlock_anon_vma_root(struct anon_vma *root)
237 if (root)
238 mutex_unlock(&root->mutex);
242 * Attach the anon_vmas from src to dst.
243 * Returns 0 on success, -ENOMEM on failure.
245 int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src)
247 struct anon_vma_chain *avc, *pavc;
248 struct anon_vma *root = NULL;
250 list_for_each_entry_reverse(pavc, &src->anon_vma_chain, same_vma) {
251 struct anon_vma *anon_vma;
253 avc = anon_vma_chain_alloc(GFP_NOWAIT | __GFP_NOWARN);
254 if (unlikely(!avc)) {
255 unlock_anon_vma_root(root);
256 root = NULL;
257 avc = anon_vma_chain_alloc(GFP_KERNEL);
258 if (!avc)
259 goto enomem_failure;
261 anon_vma = pavc->anon_vma;
262 root = lock_anon_vma_root(root, anon_vma);
263 anon_vma_chain_link(dst, avc, anon_vma);
265 unlock_anon_vma_root(root);
266 return 0;
268 enomem_failure:
269 unlink_anon_vmas(dst);
270 return -ENOMEM;
274 * Some rmap walk that needs to find all ptes/hugepmds without false
275 * negatives (like migrate and split_huge_page) running concurrent
276 * with operations that copy or move pagetables (like mremap() and
277 * fork()) to be safe. They depend on the anon_vma "same_anon_vma"
278 * list to be in a certain order: the dst_vma must be placed after the
279 * src_vma in the list. This is always guaranteed by fork() but
280 * mremap() needs to call this function to enforce it in case the
281 * dst_vma isn't newly allocated and chained with the anon_vma_clone()
282 * function but just an extension of a pre-existing vma through
283 * vma_merge.
285 * NOTE: the same_anon_vma list can still be changed by other
286 * processes while mremap runs because mremap doesn't hold the
287 * anon_vma mutex to prevent modifications to the list while it
288 * runs. All we need to enforce is that the relative order of this
289 * process vmas isn't changing (we don't care about other vmas
290 * order). Each vma corresponds to an anon_vma_chain structure so
291 * there's no risk that other processes calling anon_vma_moveto_tail()
292 * and changing the same_anon_vma list under mremap() will screw with
293 * the relative order of this process vmas in the list, because we
294 * they can't alter the order of any vma that belongs to this
295 * process. And there can't be another anon_vma_moveto_tail() running
296 * concurrently with mremap() coming from this process because we hold
297 * the mmap_sem for the whole mremap(). fork() ordering dependency
298 * also shouldn't be affected because fork() only cares that the
299 * parent vmas are placed in the list before the child vmas and
300 * anon_vma_moveto_tail() won't reorder vmas from either the fork()
301 * parent or child.
303 void anon_vma_moveto_tail(struct vm_area_struct *dst)
305 struct anon_vma_chain *pavc;
306 struct anon_vma *root = NULL;
308 list_for_each_entry_reverse(pavc, &dst->anon_vma_chain, same_vma) {
309 struct anon_vma *anon_vma = pavc->anon_vma;
310 VM_BUG_ON(pavc->vma != dst);
311 root = lock_anon_vma_root(root, anon_vma);
312 list_del(&pavc->same_anon_vma);
313 list_add_tail(&pavc->same_anon_vma, &anon_vma->head);
315 unlock_anon_vma_root(root);
319 * Attach vma to its own anon_vma, as well as to the anon_vmas that
320 * the corresponding VMA in the parent process is attached to.
321 * Returns 0 on success, non-zero on failure.
323 int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma)
325 struct anon_vma_chain *avc;
326 struct anon_vma *anon_vma;
328 /* Don't bother if the parent process has no anon_vma here. */
329 if (!pvma->anon_vma)
330 return 0;
333 * First, attach the new VMA to the parent VMA's anon_vmas,
334 * so rmap can find non-COWed pages in child processes.
336 if (anon_vma_clone(vma, pvma))
337 return -ENOMEM;
339 /* Then add our own anon_vma. */
340 anon_vma = anon_vma_alloc();
341 if (!anon_vma)
342 goto out_error;
343 avc = anon_vma_chain_alloc(GFP_KERNEL);
344 if (!avc)
345 goto out_error_free_anon_vma;
348 * The root anon_vma's spinlock is the lock actually used when we
349 * lock any of the anon_vmas in this anon_vma tree.
351 anon_vma->root = pvma->anon_vma->root;
353 * With refcounts, an anon_vma can stay around longer than the
354 * process it belongs to. The root anon_vma needs to be pinned until
355 * this anon_vma is freed, because the lock lives in the root.
357 get_anon_vma(anon_vma->root);
358 /* Mark this anon_vma as the one where our new (COWed) pages go. */
359 vma->anon_vma = anon_vma;
360 anon_vma_lock(anon_vma);
361 anon_vma_chain_link(vma, avc, anon_vma);
362 anon_vma_unlock(anon_vma);
364 return 0;
366 out_error_free_anon_vma:
367 put_anon_vma(anon_vma);
368 out_error:
369 unlink_anon_vmas(vma);
370 return -ENOMEM;
373 void unlink_anon_vmas(struct vm_area_struct *vma)
375 struct anon_vma_chain *avc, *next;
376 struct anon_vma *root = NULL;
379 * Unlink each anon_vma chained to the VMA. This list is ordered
380 * from newest to oldest, ensuring the root anon_vma gets freed last.
382 list_for_each_entry_safe(avc, next, &vma->anon_vma_chain, same_vma) {
383 struct anon_vma *anon_vma = avc->anon_vma;
385 root = lock_anon_vma_root(root, anon_vma);
386 list_del(&avc->same_anon_vma);
389 * Leave empty anon_vmas on the list - we'll need
390 * to free them outside the lock.
392 if (list_empty(&anon_vma->head))
393 continue;
395 list_del(&avc->same_vma);
396 anon_vma_chain_free(avc);
398 unlock_anon_vma_root(root);
401 * Iterate the list once more, it now only contains empty and unlinked
402 * anon_vmas, destroy them. Could not do before due to __put_anon_vma()
403 * needing to acquire the anon_vma->root->mutex.
405 list_for_each_entry_safe(avc, next, &vma->anon_vma_chain, same_vma) {
406 struct anon_vma *anon_vma = avc->anon_vma;
408 put_anon_vma(anon_vma);
410 list_del(&avc->same_vma);
411 anon_vma_chain_free(avc);
415 static void anon_vma_ctor(void *data)
417 struct anon_vma *anon_vma = data;
419 mutex_init(&anon_vma->mutex);
420 atomic_set(&anon_vma->refcount, 0);
421 INIT_LIST_HEAD(&anon_vma->head);
424 void __init anon_vma_init(void)
426 anon_vma_cachep = kmem_cache_create("anon_vma", sizeof(struct anon_vma),
427 0, SLAB_DESTROY_BY_RCU|SLAB_PANIC, anon_vma_ctor);
428 anon_vma_chain_cachep = KMEM_CACHE(anon_vma_chain, SLAB_PANIC);
432 * Getting a lock on a stable anon_vma from a page off the LRU is tricky!
434 * Since there is no serialization what so ever against page_remove_rmap()
435 * the best this function can do is return a locked anon_vma that might
436 * have been relevant to this page.
438 * The page might have been remapped to a different anon_vma or the anon_vma
439 * returned may already be freed (and even reused).
441 * In case it was remapped to a different anon_vma, the new anon_vma will be a
442 * child of the old anon_vma, and the anon_vma lifetime rules will therefore
443 * ensure that any anon_vma obtained from the page will still be valid for as
444 * long as we observe page_mapped() [ hence all those page_mapped() tests ].
446 * All users of this function must be very careful when walking the anon_vma
447 * chain and verify that the page in question is indeed mapped in it
448 * [ something equivalent to page_mapped_in_vma() ].
450 * Since anon_vma's slab is DESTROY_BY_RCU and we know from page_remove_rmap()
451 * that the anon_vma pointer from page->mapping is valid if there is a
452 * mapcount, we can dereference the anon_vma after observing those.
454 struct anon_vma *page_get_anon_vma(struct page *page)
456 struct anon_vma *anon_vma = NULL;
457 unsigned long anon_mapping;
459 rcu_read_lock();
460 anon_mapping = (unsigned long) ACCESS_ONCE(page->mapping);
461 if ((anon_mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON)
462 goto out;
463 if (!page_mapped(page))
464 goto out;
466 anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON);
467 if (!atomic_inc_not_zero(&anon_vma->refcount)) {
468 anon_vma = NULL;
469 goto out;
473 * If this page is still mapped, then its anon_vma cannot have been
474 * freed. But if it has been unmapped, we have no security against the
475 * anon_vma structure being freed and reused (for another anon_vma:
476 * SLAB_DESTROY_BY_RCU guarantees that - so the atomic_inc_not_zero()
477 * above cannot corrupt).
479 if (!page_mapped(page)) {
480 rcu_read_unlock();
481 put_anon_vma(anon_vma);
482 return NULL;
484 out:
485 rcu_read_unlock();
487 return anon_vma;
491 * Similar to page_get_anon_vma() except it locks the anon_vma.
493 * Its a little more complex as it tries to keep the fast path to a single
494 * atomic op -- the trylock. If we fail the trylock, we fall back to getting a
495 * reference like with page_get_anon_vma() and then block on the mutex.
497 struct anon_vma *page_lock_anon_vma(struct page *page)
499 struct anon_vma *anon_vma = NULL;
500 struct anon_vma *root_anon_vma;
501 unsigned long anon_mapping;
503 rcu_read_lock();
504 anon_mapping = (unsigned long) ACCESS_ONCE(page->mapping);
505 if ((anon_mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON)
506 goto out;
507 if (!page_mapped(page))
508 goto out;
510 anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON);
511 root_anon_vma = ACCESS_ONCE(anon_vma->root);
512 if (mutex_trylock(&root_anon_vma->mutex)) {
514 * If the page is still mapped, then this anon_vma is still
515 * its anon_vma, and holding the mutex ensures that it will
516 * not go away, see anon_vma_free().
518 if (!page_mapped(page)) {
519 mutex_unlock(&root_anon_vma->mutex);
520 anon_vma = NULL;
522 goto out;
525 /* trylock failed, we got to sleep */
526 if (!atomic_inc_not_zero(&anon_vma->refcount)) {
527 anon_vma = NULL;
528 goto out;
531 if (!page_mapped(page)) {
532 rcu_read_unlock();
533 put_anon_vma(anon_vma);
534 return NULL;
537 /* we pinned the anon_vma, its safe to sleep */
538 rcu_read_unlock();
539 anon_vma_lock(anon_vma);
541 if (atomic_dec_and_test(&anon_vma->refcount)) {
543 * Oops, we held the last refcount, release the lock
544 * and bail -- can't simply use put_anon_vma() because
545 * we'll deadlock on the anon_vma_lock() recursion.
547 anon_vma_unlock(anon_vma);
548 __put_anon_vma(anon_vma);
549 anon_vma = NULL;
552 return anon_vma;
554 out:
555 rcu_read_unlock();
556 return anon_vma;
559 void page_unlock_anon_vma(struct anon_vma *anon_vma)
561 anon_vma_unlock(anon_vma);
565 * At what user virtual address is page expected in @vma?
566 * Returns virtual address or -EFAULT if page's index/offset is not
567 * within the range mapped the @vma.
569 inline unsigned long
570 vma_address(struct page *page, struct vm_area_struct *vma)
572 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
573 unsigned long address;
575 if (unlikely(is_vm_hugetlb_page(vma)))
576 pgoff = page->index << huge_page_order(page_hstate(page));
577 address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
578 if (unlikely(address < vma->vm_start || address >= vma->vm_end)) {
579 /* page should be within @vma mapping range */
580 return -EFAULT;
582 return address;
586 * At what user virtual address is page expected in vma?
587 * Caller should check the page is actually part of the vma.
589 unsigned long page_address_in_vma(struct page *page, struct vm_area_struct *vma)
591 if (PageAnon(page)) {
592 struct anon_vma *page__anon_vma = page_anon_vma(page);
594 * Note: swapoff's unuse_vma() is more efficient with this
595 * check, and needs it to match anon_vma when KSM is active.
597 if (!vma->anon_vma || !page__anon_vma ||
598 vma->anon_vma->root != page__anon_vma->root)
599 return -EFAULT;
600 } else if (page->mapping && !(vma->vm_flags & VM_NONLINEAR)) {
601 if (!vma->vm_file ||
602 vma->vm_file->f_mapping != page->mapping)
603 return -EFAULT;
604 } else
605 return -EFAULT;
606 return vma_address(page, vma);
610 * Check that @page is mapped at @address into @mm.
612 * If @sync is false, page_check_address may perform a racy check to avoid
613 * the page table lock when the pte is not present (helpful when reclaiming
614 * highly shared pages).
616 * On success returns with pte mapped and locked.
618 pte_t *__page_check_address(struct page *page, struct mm_struct *mm,
619 unsigned long address, spinlock_t **ptlp, int sync)
621 pgd_t *pgd;
622 pud_t *pud;
623 pmd_t *pmd;
624 pte_t *pte;
625 spinlock_t *ptl;
627 if (unlikely(PageHuge(page))) {
628 /* when pud is not present, pte will be NULL */
629 pte = huge_pte_offset(mm, address);
630 if (!pte)
631 return NULL;
633 ptl = &mm->page_table_lock;
634 goto check;
637 pgd = pgd_offset(mm, address);
638 if (!pgd_present(*pgd))
639 return NULL;
641 pud = pud_offset(pgd, address);
642 if (!pud_present(*pud))
643 return NULL;
645 pmd = pmd_offset(pud, address);
646 if (!pmd_present(*pmd))
647 return NULL;
648 if (pmd_trans_huge(*pmd))
649 return NULL;
651 pte = pte_offset_map(pmd, address);
652 /* Make a quick check before getting the lock */
653 if (!sync && !pte_present(*pte)) {
654 pte_unmap(pte);
655 return NULL;
658 ptl = pte_lockptr(mm, pmd);
659 check:
660 spin_lock(ptl);
661 if (pte_present(*pte) && page_to_pfn(page) == pte_pfn(*pte)) {
662 *ptlp = ptl;
663 return pte;
665 pte_unmap_unlock(pte, ptl);
666 return NULL;
670 * page_mapped_in_vma - check whether a page is really mapped in a VMA
671 * @page: the page to test
672 * @vma: the VMA to test
674 * Returns 1 if the page is mapped into the page tables of the VMA, 0
675 * if the page is not mapped into the page tables of this VMA. Only
676 * valid for normal file or anonymous VMAs.
678 int page_mapped_in_vma(struct page *page, struct vm_area_struct *vma)
680 unsigned long address;
681 pte_t *pte;
682 spinlock_t *ptl;
684 address = vma_address(page, vma);
685 if (address == -EFAULT) /* out of vma range */
686 return 0;
687 pte = page_check_address(page, vma->vm_mm, address, &ptl, 1);
688 if (!pte) /* the page is not in this mm */
689 return 0;
690 pte_unmap_unlock(pte, ptl);
692 return 1;
696 * Subfunctions of page_referenced: page_referenced_one called
697 * repeatedly from either page_referenced_anon or page_referenced_file.
699 int page_referenced_one(struct page *page, struct vm_area_struct *vma,
700 unsigned long address, unsigned int *mapcount,
701 unsigned long *vm_flags)
703 struct mm_struct *mm = vma->vm_mm;
704 int referenced = 0;
706 if (unlikely(PageTransHuge(page))) {
707 pmd_t *pmd;
709 spin_lock(&mm->page_table_lock);
711 * rmap might return false positives; we must filter
712 * these out using page_check_address_pmd().
714 pmd = page_check_address_pmd(page, mm, address,
715 PAGE_CHECK_ADDRESS_PMD_FLAG);
716 if (!pmd) {
717 spin_unlock(&mm->page_table_lock);
718 goto out;
721 if (vma->vm_flags & VM_LOCKED) {
722 spin_unlock(&mm->page_table_lock);
723 *mapcount = 0; /* break early from loop */
724 *vm_flags |= VM_LOCKED;
725 goto out;
728 /* go ahead even if the pmd is pmd_trans_splitting() */
729 if (pmdp_clear_flush_young_notify(vma, address, pmd))
730 referenced++;
731 spin_unlock(&mm->page_table_lock);
732 } else {
733 pte_t *pte;
734 spinlock_t *ptl;
737 * rmap might return false positives; we must filter
738 * these out using page_check_address().
740 pte = page_check_address(page, mm, address, &ptl, 0);
741 if (!pte)
742 goto out;
744 if (vma->vm_flags & VM_LOCKED) {
745 pte_unmap_unlock(pte, ptl);
746 *mapcount = 0; /* break early from loop */
747 *vm_flags |= VM_LOCKED;
748 goto out;
751 if (ptep_clear_flush_young_notify(vma, address, pte)) {
753 * Don't treat a reference through a sequentially read
754 * mapping as such. If the page has been used in
755 * another mapping, we will catch it; if this other
756 * mapping is already gone, the unmap path will have
757 * set PG_referenced or activated the page.
759 if (likely(!VM_SequentialReadHint(vma)))
760 referenced++;
762 pte_unmap_unlock(pte, ptl);
765 /* Pretend the page is referenced if the task has the
766 swap token and is in the middle of a page fault. */
767 if (mm != current->mm && has_swap_token(mm) &&
768 rwsem_is_locked(&mm->mmap_sem))
769 referenced++;
771 (*mapcount)--;
773 if (referenced)
774 *vm_flags |= vma->vm_flags;
775 out:
776 return referenced;
779 static int page_referenced_anon(struct page *page,
780 struct mem_cgroup *memcg,
781 unsigned long *vm_flags)
783 unsigned int mapcount;
784 struct anon_vma *anon_vma;
785 struct anon_vma_chain *avc;
786 int referenced = 0;
788 anon_vma = page_lock_anon_vma(page);
789 if (!anon_vma)
790 return referenced;
792 mapcount = page_mapcount(page);
793 list_for_each_entry(avc, &anon_vma->head, same_anon_vma) {
794 struct vm_area_struct *vma = avc->vma;
795 unsigned long address = vma_address(page, vma);
796 if (address == -EFAULT)
797 continue;
799 * If we are reclaiming on behalf of a cgroup, skip
800 * counting on behalf of references from different
801 * cgroups
803 if (memcg && !mm_match_cgroup(vma->vm_mm, memcg))
804 continue;
805 referenced += page_referenced_one(page, vma, address,
806 &mapcount, vm_flags);
807 if (!mapcount)
808 break;
811 page_unlock_anon_vma(anon_vma);
812 return referenced;
816 * page_referenced_file - referenced check for object-based rmap
817 * @page: the page we're checking references on.
818 * @memcg: target memory control group
819 * @vm_flags: collect encountered vma->vm_flags who actually referenced the page
821 * For an object-based mapped page, find all the places it is mapped and
822 * check/clear the referenced flag. This is done by following the page->mapping
823 * pointer, then walking the chain of vmas it holds. It returns the number
824 * of references it found.
826 * This function is only called from page_referenced for object-based pages.
828 static int page_referenced_file(struct page *page,
829 struct mem_cgroup *memcg,
830 unsigned long *vm_flags)
832 unsigned int mapcount;
833 struct address_space *mapping = page->mapping;
834 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
835 struct vm_area_struct *vma;
836 struct prio_tree_iter iter;
837 int referenced = 0;
840 * The caller's checks on page->mapping and !PageAnon have made
841 * sure that this is a file page: the check for page->mapping
842 * excludes the case just before it gets set on an anon page.
844 BUG_ON(PageAnon(page));
847 * The page lock not only makes sure that page->mapping cannot
848 * suddenly be NULLified by truncation, it makes sure that the
849 * structure at mapping cannot be freed and reused yet,
850 * so we can safely take mapping->i_mmap_mutex.
852 BUG_ON(!PageLocked(page));
854 mutex_lock(&mapping->i_mmap_mutex);
857 * i_mmap_mutex does not stabilize mapcount at all, but mapcount
858 * is more likely to be accurate if we note it after spinning.
860 mapcount = page_mapcount(page);
862 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
863 unsigned long address = vma_address(page, vma);
864 if (address == -EFAULT)
865 continue;
867 * If we are reclaiming on behalf of a cgroup, skip
868 * counting on behalf of references from different
869 * cgroups
871 if (memcg && !mm_match_cgroup(vma->vm_mm, memcg))
872 continue;
873 referenced += page_referenced_one(page, vma, address,
874 &mapcount, vm_flags);
875 if (!mapcount)
876 break;
879 mutex_unlock(&mapping->i_mmap_mutex);
880 return referenced;
884 * page_referenced - test if the page was referenced
885 * @page: the page to test
886 * @is_locked: caller holds lock on the page
887 * @memcg: target memory cgroup
888 * @vm_flags: collect encountered vma->vm_flags who actually referenced the page
890 * Quick test_and_clear_referenced for all mappings to a page,
891 * returns the number of ptes which referenced the page.
893 int page_referenced(struct page *page,
894 int is_locked,
895 struct mem_cgroup *memcg,
896 unsigned long *vm_flags)
898 int referenced = 0;
899 int we_locked = 0;
901 *vm_flags = 0;
902 if (page_mapped(page) && page_rmapping(page)) {
903 if (!is_locked && (!PageAnon(page) || PageKsm(page))) {
904 we_locked = trylock_page(page);
905 if (!we_locked) {
906 referenced++;
907 goto out;
910 if (unlikely(PageKsm(page)))
911 referenced += page_referenced_ksm(page, memcg,
912 vm_flags);
913 else if (PageAnon(page))
914 referenced += page_referenced_anon(page, memcg,
915 vm_flags);
916 else if (page->mapping)
917 referenced += page_referenced_file(page, memcg,
918 vm_flags);
919 if (we_locked)
920 unlock_page(page);
922 if (page_test_and_clear_young(page_to_pfn(page)))
923 referenced++;
925 out:
926 return referenced;
929 static int page_mkclean_one(struct page *page, struct vm_area_struct *vma,
930 unsigned long address)
932 struct mm_struct *mm = vma->vm_mm;
933 pte_t *pte;
934 spinlock_t *ptl;
935 int ret = 0;
937 pte = page_check_address(page, mm, address, &ptl, 1);
938 if (!pte)
939 goto out;
941 if (pte_dirty(*pte) || pte_write(*pte)) {
942 pte_t entry;
944 flush_cache_page(vma, address, pte_pfn(*pte));
945 entry = ptep_clear_flush_notify(vma, address, pte);
946 entry = pte_wrprotect(entry);
947 entry = pte_mkclean(entry);
948 set_pte_at(mm, address, pte, entry);
949 ret = 1;
952 pte_unmap_unlock(pte, ptl);
953 out:
954 return ret;
957 static int page_mkclean_file(struct address_space *mapping, struct page *page)
959 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
960 struct vm_area_struct *vma;
961 struct prio_tree_iter iter;
962 int ret = 0;
964 BUG_ON(PageAnon(page));
966 mutex_lock(&mapping->i_mmap_mutex);
967 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
968 if (vma->vm_flags & VM_SHARED) {
969 unsigned long address = vma_address(page, vma);
970 if (address == -EFAULT)
971 continue;
972 ret += page_mkclean_one(page, vma, address);
975 mutex_unlock(&mapping->i_mmap_mutex);
976 return ret;
979 int page_mkclean(struct page *page)
981 int ret = 0;
983 BUG_ON(!PageLocked(page));
985 if (page_mapped(page)) {
986 struct address_space *mapping = page_mapping(page);
987 if (mapping)
988 ret = page_mkclean_file(mapping, page);
991 return ret;
993 EXPORT_SYMBOL_GPL(page_mkclean);
996 * page_move_anon_rmap - move a page to our anon_vma
997 * @page: the page to move to our anon_vma
998 * @vma: the vma the page belongs to
999 * @address: the user virtual address mapped
1001 * When a page belongs exclusively to one process after a COW event,
1002 * that page can be moved into the anon_vma that belongs to just that
1003 * process, so the rmap code will not search the parent or sibling
1004 * processes.
1006 void page_move_anon_rmap(struct page *page,
1007 struct vm_area_struct *vma, unsigned long address)
1009 struct anon_vma *anon_vma = vma->anon_vma;
1011 VM_BUG_ON(!PageLocked(page));
1012 VM_BUG_ON(!anon_vma);
1013 VM_BUG_ON(page->index != linear_page_index(vma, address));
1015 anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
1016 page->mapping = (struct address_space *) anon_vma;
1020 * __page_set_anon_rmap - set up new anonymous rmap
1021 * @page: Page to add to rmap
1022 * @vma: VM area to add page to.
1023 * @address: User virtual address of the mapping
1024 * @exclusive: the page is exclusively owned by the current process
1026 static void __page_set_anon_rmap(struct page *page,
1027 struct vm_area_struct *vma, unsigned long address, int exclusive)
1029 struct anon_vma *anon_vma = vma->anon_vma;
1031 BUG_ON(!anon_vma);
1033 if (PageAnon(page))
1034 return;
1037 * If the page isn't exclusively mapped into this vma,
1038 * we must use the _oldest_ possible anon_vma for the
1039 * page mapping!
1041 if (!exclusive)
1042 anon_vma = anon_vma->root;
1044 anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
1045 page->mapping = (struct address_space *) anon_vma;
1046 page->index = linear_page_index(vma, address);
1050 * __page_check_anon_rmap - sanity check anonymous rmap addition
1051 * @page: the page to add the mapping to
1052 * @vma: the vm area in which the mapping is added
1053 * @address: the user virtual address mapped
1055 static void __page_check_anon_rmap(struct page *page,
1056 struct vm_area_struct *vma, unsigned long address)
1058 #ifdef CONFIG_DEBUG_VM
1060 * The page's anon-rmap details (mapping and index) are guaranteed to
1061 * be set up correctly at this point.
1063 * We have exclusion against page_add_anon_rmap because the caller
1064 * always holds the page locked, except if called from page_dup_rmap,
1065 * in which case the page is already known to be setup.
1067 * We have exclusion against page_add_new_anon_rmap because those pages
1068 * are initially only visible via the pagetables, and the pte is locked
1069 * over the call to page_add_new_anon_rmap.
1071 BUG_ON(page_anon_vma(page)->root != vma->anon_vma->root);
1072 BUG_ON(page->index != linear_page_index(vma, address));
1073 #endif
1077 * page_add_anon_rmap - add pte mapping to an anonymous page
1078 * @page: the page to add the mapping to
1079 * @vma: the vm area in which the mapping is added
1080 * @address: the user virtual address mapped
1082 * The caller needs to hold the pte lock, and the page must be locked in
1083 * the anon_vma case: to serialize mapping,index checking after setting,
1084 * and to ensure that PageAnon is not being upgraded racily to PageKsm
1085 * (but PageKsm is never downgraded to PageAnon).
1087 void page_add_anon_rmap(struct page *page,
1088 struct vm_area_struct *vma, unsigned long address)
1090 do_page_add_anon_rmap(page, vma, address, 0);
1094 * Special version of the above for do_swap_page, which often runs
1095 * into pages that are exclusively owned by the current process.
1096 * Everybody else should continue to use page_add_anon_rmap above.
1098 void do_page_add_anon_rmap(struct page *page,
1099 struct vm_area_struct *vma, unsigned long address, int exclusive)
1101 int first = atomic_inc_and_test(&page->_mapcount);
1102 if (first) {
1103 if (!PageTransHuge(page))
1104 __inc_zone_page_state(page, NR_ANON_PAGES);
1105 else
1106 __inc_zone_page_state(page,
1107 NR_ANON_TRANSPARENT_HUGEPAGES);
1109 if (unlikely(PageKsm(page)))
1110 return;
1112 VM_BUG_ON(!PageLocked(page));
1113 /* address might be in next vma when migration races vma_adjust */
1114 if (first)
1115 __page_set_anon_rmap(page, vma, address, exclusive);
1116 else
1117 __page_check_anon_rmap(page, vma, address);
1121 * page_add_new_anon_rmap - add pte mapping to a new anonymous page
1122 * @page: the page to add the mapping to
1123 * @vma: the vm area in which the mapping is added
1124 * @address: the user virtual address mapped
1126 * Same as page_add_anon_rmap but must only be called on *new* pages.
1127 * This means the inc-and-test can be bypassed.
1128 * Page does not have to be locked.
1130 void page_add_new_anon_rmap(struct page *page,
1131 struct vm_area_struct *vma, unsigned long address)
1133 VM_BUG_ON(address < vma->vm_start || address >= vma->vm_end);
1134 SetPageSwapBacked(page);
1135 atomic_set(&page->_mapcount, 0); /* increment count (starts at -1) */
1136 if (!PageTransHuge(page))
1137 __inc_zone_page_state(page, NR_ANON_PAGES);
1138 else
1139 __inc_zone_page_state(page, NR_ANON_TRANSPARENT_HUGEPAGES);
1140 __page_set_anon_rmap(page, vma, address, 1);
1141 if (page_evictable(page, vma))
1142 lru_cache_add_lru(page, LRU_ACTIVE_ANON);
1143 else
1144 add_page_to_unevictable_list(page);
1148 * page_add_file_rmap - add pte mapping to a file page
1149 * @page: the page to add the mapping to
1151 * The caller needs to hold the pte lock.
1153 void page_add_file_rmap(struct page *page)
1155 bool locked;
1156 unsigned long flags;
1158 mem_cgroup_begin_update_page_stat(page, &locked, &flags);
1159 if (atomic_inc_and_test(&page->_mapcount)) {
1160 __inc_zone_page_state(page, NR_FILE_MAPPED);
1161 mem_cgroup_inc_page_stat(page, MEMCG_NR_FILE_MAPPED);
1163 mem_cgroup_end_update_page_stat(page, &locked, &flags);
1167 * page_remove_rmap - take down pte mapping from a page
1168 * @page: page to remove mapping from
1170 * The caller needs to hold the pte lock.
1172 void page_remove_rmap(struct page *page)
1174 struct address_space *mapping = page_mapping(page);
1175 bool anon = PageAnon(page);
1176 bool locked;
1177 unsigned long flags;
1180 * The anon case has no mem_cgroup page_stat to update; but may
1181 * uncharge_page() below, where the lock ordering can deadlock if
1182 * we hold the lock against page_stat move: so avoid it on anon.
1184 if (!anon)
1185 mem_cgroup_begin_update_page_stat(page, &locked, &flags);
1187 /* page still mapped by someone else? */
1188 if (!atomic_add_negative(-1, &page->_mapcount))
1189 goto out;
1192 * Now that the last pte has gone, s390 must transfer dirty
1193 * flag from storage key to struct page. We can usually skip
1194 * this if the page is anon, so about to be freed; but perhaps
1195 * not if it's in swapcache - there might be another pte slot
1196 * containing the swap entry, but page not yet written to swap.
1198 * And we can skip it on file pages, so long as the filesystem
1199 * participates in dirty tracking; but need to catch shm and tmpfs
1200 * and ramfs pages which have been modified since creation by read
1201 * fault.
1203 * Note that mapping must be decided above, before decrementing
1204 * mapcount (which luckily provides a barrier): once page is unmapped,
1205 * it could be truncated and page->mapping reset to NULL at any moment.
1206 * Note also that we are relying on page_mapping(page) to set mapping
1207 * to &swapper_space when PageSwapCache(page).
1209 if (mapping && !mapping_cap_account_dirty(mapping) &&
1210 page_test_and_clear_dirty(page_to_pfn(page), 1))
1211 set_page_dirty(page);
1213 * Hugepages are not counted in NR_ANON_PAGES nor NR_FILE_MAPPED
1214 * and not charged by memcg for now.
1216 if (unlikely(PageHuge(page)))
1217 goto out;
1218 if (anon) {
1219 mem_cgroup_uncharge_page(page);
1220 if (!PageTransHuge(page))
1221 __dec_zone_page_state(page, NR_ANON_PAGES);
1222 else
1223 __dec_zone_page_state(page,
1224 NR_ANON_TRANSPARENT_HUGEPAGES);
1225 } else {
1226 __dec_zone_page_state(page, NR_FILE_MAPPED);
1227 mem_cgroup_dec_page_stat(page, MEMCG_NR_FILE_MAPPED);
1230 * It would be tidy to reset the PageAnon mapping here,
1231 * but that might overwrite a racing page_add_anon_rmap
1232 * which increments mapcount after us but sets mapping
1233 * before us: so leave the reset to free_hot_cold_page,
1234 * and remember that it's only reliable while mapped.
1235 * Leaving it set also helps swapoff to reinstate ptes
1236 * faster for those pages still in swapcache.
1238 out:
1239 if (!anon)
1240 mem_cgroup_end_update_page_stat(page, &locked, &flags);
1244 * Subfunctions of try_to_unmap: try_to_unmap_one called
1245 * repeatedly from try_to_unmap_ksm, try_to_unmap_anon or try_to_unmap_file.
1247 int try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
1248 unsigned long address, enum ttu_flags flags)
1250 struct mm_struct *mm = vma->vm_mm;
1251 pte_t *pte;
1252 pte_t pteval;
1253 spinlock_t *ptl;
1254 int ret = SWAP_AGAIN;
1256 pte = page_check_address(page, mm, address, &ptl, 0);
1257 if (!pte)
1258 goto out;
1261 * If the page is mlock()d, we cannot swap it out.
1262 * If it's recently referenced (perhaps page_referenced
1263 * skipped over this mm) then we should reactivate it.
1265 if (!(flags & TTU_IGNORE_MLOCK)) {
1266 if (vma->vm_flags & VM_LOCKED)
1267 goto out_mlock;
1269 if (TTU_ACTION(flags) == TTU_MUNLOCK)
1270 goto out_unmap;
1272 if (!(flags & TTU_IGNORE_ACCESS)) {
1273 if (ptep_clear_flush_young_notify(vma, address, pte)) {
1274 ret = SWAP_FAIL;
1275 goto out_unmap;
1279 /* Nuke the page table entry. */
1280 flush_cache_page(vma, address, page_to_pfn(page));
1281 pteval = ptep_clear_flush_notify(vma, address, pte);
1283 /* Move the dirty bit to the physical page now the pte is gone. */
1284 if (pte_dirty(pteval))
1285 set_page_dirty(page);
1287 /* Update high watermark before we lower rss */
1288 update_hiwater_rss(mm);
1290 if (PageHWPoison(page) && !(flags & TTU_IGNORE_HWPOISON)) {
1291 if (PageAnon(page))
1292 dec_mm_counter(mm, MM_ANONPAGES);
1293 else
1294 dec_mm_counter(mm, MM_FILEPAGES);
1295 set_pte_at(mm, address, pte,
1296 swp_entry_to_pte(make_hwpoison_entry(page)));
1297 } else if (PageAnon(page)) {
1298 swp_entry_t entry = { .val = page_private(page) };
1300 if (PageSwapCache(page)) {
1302 * Store the swap location in the pte.
1303 * See handle_pte_fault() ...
1305 if (swap_duplicate(entry) < 0) {
1306 set_pte_at(mm, address, pte, pteval);
1307 ret = SWAP_FAIL;
1308 goto out_unmap;
1310 if (list_empty(&mm->mmlist)) {
1311 spin_lock(&mmlist_lock);
1312 if (list_empty(&mm->mmlist))
1313 list_add(&mm->mmlist, &init_mm.mmlist);
1314 spin_unlock(&mmlist_lock);
1316 dec_mm_counter(mm, MM_ANONPAGES);
1317 inc_mm_counter(mm, MM_SWAPENTS);
1318 } else if (IS_ENABLED(CONFIG_MIGRATION)) {
1320 * Store the pfn of the page in a special migration
1321 * pte. do_swap_page() will wait until the migration
1322 * pte is removed and then restart fault handling.
1324 BUG_ON(TTU_ACTION(flags) != TTU_MIGRATION);
1325 entry = make_migration_entry(page, pte_write(pteval));
1327 set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
1328 BUG_ON(pte_file(*pte));
1329 } else if (IS_ENABLED(CONFIG_MIGRATION) &&
1330 (TTU_ACTION(flags) == TTU_MIGRATION)) {
1331 /* Establish migration entry for a file page */
1332 swp_entry_t entry;
1333 entry = make_migration_entry(page, pte_write(pteval));
1334 set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
1335 } else
1336 dec_mm_counter(mm, MM_FILEPAGES);
1338 page_remove_rmap(page);
1339 page_cache_release(page);
1341 out_unmap:
1342 pte_unmap_unlock(pte, ptl);
1343 out:
1344 return ret;
1346 out_mlock:
1347 pte_unmap_unlock(pte, ptl);
1351 * We need mmap_sem locking, Otherwise VM_LOCKED check makes
1352 * unstable result and race. Plus, We can't wait here because
1353 * we now hold anon_vma->mutex or mapping->i_mmap_mutex.
1354 * if trylock failed, the page remain in evictable lru and later
1355 * vmscan could retry to move the page to unevictable lru if the
1356 * page is actually mlocked.
1358 if (down_read_trylock(&vma->vm_mm->mmap_sem)) {
1359 if (vma->vm_flags & VM_LOCKED) {
1360 mlock_vma_page(page);
1361 ret = SWAP_MLOCK;
1363 up_read(&vma->vm_mm->mmap_sem);
1365 return ret;
1369 * objrmap doesn't work for nonlinear VMAs because the assumption that
1370 * offset-into-file correlates with offset-into-virtual-addresses does not hold.
1371 * Consequently, given a particular page and its ->index, we cannot locate the
1372 * ptes which are mapping that page without an exhaustive linear search.
1374 * So what this code does is a mini "virtual scan" of each nonlinear VMA which
1375 * maps the file to which the target page belongs. The ->vm_private_data field
1376 * holds the current cursor into that scan. Successive searches will circulate
1377 * around the vma's virtual address space.
1379 * So as more replacement pressure is applied to the pages in a nonlinear VMA,
1380 * more scanning pressure is placed against them as well. Eventually pages
1381 * will become fully unmapped and are eligible for eviction.
1383 * For very sparsely populated VMAs this is a little inefficient - chances are
1384 * there there won't be many ptes located within the scan cluster. In this case
1385 * maybe we could scan further - to the end of the pte page, perhaps.
1387 * Mlocked pages: check VM_LOCKED under mmap_sem held for read, if we can
1388 * acquire it without blocking. If vma locked, mlock the pages in the cluster,
1389 * rather than unmapping them. If we encounter the "check_page" that vmscan is
1390 * trying to unmap, return SWAP_MLOCK, else default SWAP_AGAIN.
1392 #define CLUSTER_SIZE min(32*PAGE_SIZE, PMD_SIZE)
1393 #define CLUSTER_MASK (~(CLUSTER_SIZE - 1))
1395 static int try_to_unmap_cluster(unsigned long cursor, unsigned int *mapcount,
1396 struct vm_area_struct *vma, struct page *check_page)
1398 struct mm_struct *mm = vma->vm_mm;
1399 pgd_t *pgd;
1400 pud_t *pud;
1401 pmd_t *pmd;
1402 pte_t *pte;
1403 pte_t pteval;
1404 spinlock_t *ptl;
1405 struct page *page;
1406 unsigned long address;
1407 unsigned long end;
1408 int ret = SWAP_AGAIN;
1409 int locked_vma = 0;
1411 address = (vma->vm_start + cursor) & CLUSTER_MASK;
1412 end = address + CLUSTER_SIZE;
1413 if (address < vma->vm_start)
1414 address = vma->vm_start;
1415 if (end > vma->vm_end)
1416 end = vma->vm_end;
1418 pgd = pgd_offset(mm, address);
1419 if (!pgd_present(*pgd))
1420 return ret;
1422 pud = pud_offset(pgd, address);
1423 if (!pud_present(*pud))
1424 return ret;
1426 pmd = pmd_offset(pud, address);
1427 if (!pmd_present(*pmd))
1428 return ret;
1431 * If we can acquire the mmap_sem for read, and vma is VM_LOCKED,
1432 * keep the sem while scanning the cluster for mlocking pages.
1434 if (down_read_trylock(&vma->vm_mm->mmap_sem)) {
1435 locked_vma = (vma->vm_flags & VM_LOCKED);
1436 if (!locked_vma)
1437 up_read(&vma->vm_mm->mmap_sem); /* don't need it */
1440 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
1442 /* Update high watermark before we lower rss */
1443 update_hiwater_rss(mm);
1445 for (; address < end; pte++, address += PAGE_SIZE) {
1446 if (!pte_present(*pte))
1447 continue;
1448 page = vm_normal_page(vma, address, *pte);
1449 BUG_ON(!page || PageAnon(page));
1451 if (locked_vma) {
1452 if (page == check_page) {
1453 /* we know we have check_page locked */
1454 mlock_vma_page(page);
1455 ret = SWAP_MLOCK;
1456 } else if (trylock_page(page)) {
1458 * If we can lock the page, perform mlock.
1459 * Otherwise leave the page alone, it will be
1460 * eventually encountered again later.
1462 mlock_vma_page(page);
1463 unlock_page(page);
1465 continue; /* don't unmap */
1468 if (ptep_clear_flush_young_notify(vma, address, pte))
1469 continue;
1471 /* Nuke the page table entry. */
1472 flush_cache_page(vma, address, pte_pfn(*pte));
1473 pteval = ptep_clear_flush_notify(vma, address, pte);
1475 /* If nonlinear, store the file page offset in the pte. */
1476 if (page->index != linear_page_index(vma, address))
1477 set_pte_at(mm, address, pte, pgoff_to_pte(page->index));
1479 /* Move the dirty bit to the physical page now the pte is gone. */
1480 if (pte_dirty(pteval))
1481 set_page_dirty(page);
1483 page_remove_rmap(page);
1484 page_cache_release(page);
1485 dec_mm_counter(mm, MM_FILEPAGES);
1486 (*mapcount)--;
1488 pte_unmap_unlock(pte - 1, ptl);
1489 if (locked_vma)
1490 up_read(&vma->vm_mm->mmap_sem);
1491 return ret;
1494 bool is_vma_temporary_stack(struct vm_area_struct *vma)
1496 int maybe_stack = vma->vm_flags & (VM_GROWSDOWN | VM_GROWSUP);
1498 if (!maybe_stack)
1499 return false;
1501 if ((vma->vm_flags & VM_STACK_INCOMPLETE_SETUP) ==
1502 VM_STACK_INCOMPLETE_SETUP)
1503 return true;
1505 return false;
1509 * try_to_unmap_anon - unmap or unlock anonymous page using the object-based
1510 * rmap method
1511 * @page: the page to unmap/unlock
1512 * @flags: action and flags
1514 * Find all the mappings of a page using the mapping pointer and the vma chains
1515 * contained in the anon_vma struct it points to.
1517 * This function is only called from try_to_unmap/try_to_munlock for
1518 * anonymous pages.
1519 * When called from try_to_munlock(), the mmap_sem of the mm containing the vma
1520 * where the page was found will be held for write. So, we won't recheck
1521 * vm_flags for that VMA. That should be OK, because that vma shouldn't be
1522 * 'LOCKED.
1524 static int try_to_unmap_anon(struct page *page, enum ttu_flags flags)
1526 struct anon_vma *anon_vma;
1527 struct anon_vma_chain *avc;
1528 int ret = SWAP_AGAIN;
1530 anon_vma = page_lock_anon_vma(page);
1531 if (!anon_vma)
1532 return ret;
1534 list_for_each_entry(avc, &anon_vma->head, same_anon_vma) {
1535 struct vm_area_struct *vma = avc->vma;
1536 unsigned long address;
1539 * During exec, a temporary VMA is setup and later moved.
1540 * The VMA is moved under the anon_vma lock but not the
1541 * page tables leading to a race where migration cannot
1542 * find the migration ptes. Rather than increasing the
1543 * locking requirements of exec(), migration skips
1544 * temporary VMAs until after exec() completes.
1546 if (IS_ENABLED(CONFIG_MIGRATION) && (flags & TTU_MIGRATION) &&
1547 is_vma_temporary_stack(vma))
1548 continue;
1550 address = vma_address(page, vma);
1551 if (address == -EFAULT)
1552 continue;
1553 ret = try_to_unmap_one(page, vma, address, flags);
1554 if (ret != SWAP_AGAIN || !page_mapped(page))
1555 break;
1558 page_unlock_anon_vma(anon_vma);
1559 return ret;
1563 * try_to_unmap_file - unmap/unlock file page using the object-based rmap method
1564 * @page: the page to unmap/unlock
1565 * @flags: action and flags
1567 * Find all the mappings of a page using the mapping pointer and the vma chains
1568 * contained in the address_space struct it points to.
1570 * This function is only called from try_to_unmap/try_to_munlock for
1571 * object-based pages.
1572 * When called from try_to_munlock(), the mmap_sem of the mm containing the vma
1573 * where the page was found will be held for write. So, we won't recheck
1574 * vm_flags for that VMA. That should be OK, because that vma shouldn't be
1575 * 'LOCKED.
1577 static int try_to_unmap_file(struct page *page, enum ttu_flags flags)
1579 struct address_space *mapping = page->mapping;
1580 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
1581 struct vm_area_struct *vma;
1582 struct prio_tree_iter iter;
1583 int ret = SWAP_AGAIN;
1584 unsigned long cursor;
1585 unsigned long max_nl_cursor = 0;
1586 unsigned long max_nl_size = 0;
1587 unsigned int mapcount;
1589 mutex_lock(&mapping->i_mmap_mutex);
1590 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
1591 unsigned long address = vma_address(page, vma);
1592 if (address == -EFAULT)
1593 continue;
1594 ret = try_to_unmap_one(page, vma, address, flags);
1595 if (ret != SWAP_AGAIN || !page_mapped(page))
1596 goto out;
1599 if (list_empty(&mapping->i_mmap_nonlinear))
1600 goto out;
1603 * We don't bother to try to find the munlocked page in nonlinears.
1604 * It's costly. Instead, later, page reclaim logic may call
1605 * try_to_unmap(TTU_MUNLOCK) and recover PG_mlocked lazily.
1607 if (TTU_ACTION(flags) == TTU_MUNLOCK)
1608 goto out;
1610 list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
1611 shared.vm_set.list) {
1612 cursor = (unsigned long) vma->vm_private_data;
1613 if (cursor > max_nl_cursor)
1614 max_nl_cursor = cursor;
1615 cursor = vma->vm_end - vma->vm_start;
1616 if (cursor > max_nl_size)
1617 max_nl_size = cursor;
1620 if (max_nl_size == 0) { /* all nonlinears locked or reserved ? */
1621 ret = SWAP_FAIL;
1622 goto out;
1626 * We don't try to search for this page in the nonlinear vmas,
1627 * and page_referenced wouldn't have found it anyway. Instead
1628 * just walk the nonlinear vmas trying to age and unmap some.
1629 * The mapcount of the page we came in with is irrelevant,
1630 * but even so use it as a guide to how hard we should try?
1632 mapcount = page_mapcount(page);
1633 if (!mapcount)
1634 goto out;
1635 cond_resched();
1637 max_nl_size = (max_nl_size + CLUSTER_SIZE - 1) & CLUSTER_MASK;
1638 if (max_nl_cursor == 0)
1639 max_nl_cursor = CLUSTER_SIZE;
1641 do {
1642 list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
1643 shared.vm_set.list) {
1644 cursor = (unsigned long) vma->vm_private_data;
1645 while ( cursor < max_nl_cursor &&
1646 cursor < vma->vm_end - vma->vm_start) {
1647 if (try_to_unmap_cluster(cursor, &mapcount,
1648 vma, page) == SWAP_MLOCK)
1649 ret = SWAP_MLOCK;
1650 cursor += CLUSTER_SIZE;
1651 vma->vm_private_data = (void *) cursor;
1652 if ((int)mapcount <= 0)
1653 goto out;
1655 vma->vm_private_data = (void *) max_nl_cursor;
1657 cond_resched();
1658 max_nl_cursor += CLUSTER_SIZE;
1659 } while (max_nl_cursor <= max_nl_size);
1662 * Don't loop forever (perhaps all the remaining pages are
1663 * in locked vmas). Reset cursor on all unreserved nonlinear
1664 * vmas, now forgetting on which ones it had fallen behind.
1666 list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
1667 vma->vm_private_data = NULL;
1668 out:
1669 mutex_unlock(&mapping->i_mmap_mutex);
1670 return ret;
1674 * try_to_unmap - try to remove all page table mappings to a page
1675 * @page: the page to get unmapped
1676 * @flags: action and flags
1678 * Tries to remove all the page table entries which are mapping this
1679 * page, used in the pageout path. Caller must hold the page lock.
1680 * Return values are:
1682 * SWAP_SUCCESS - we succeeded in removing all mappings
1683 * SWAP_AGAIN - we missed a mapping, try again later
1684 * SWAP_FAIL - the page is unswappable
1685 * SWAP_MLOCK - page is mlocked.
1687 int try_to_unmap(struct page *page, enum ttu_flags flags)
1689 int ret;
1691 BUG_ON(!PageLocked(page));
1692 VM_BUG_ON(!PageHuge(page) && PageTransHuge(page));
1694 if (unlikely(PageKsm(page)))
1695 ret = try_to_unmap_ksm(page, flags);
1696 else if (PageAnon(page))
1697 ret = try_to_unmap_anon(page, flags);
1698 else
1699 ret = try_to_unmap_file(page, flags);
1700 if (ret != SWAP_MLOCK && !page_mapped(page))
1701 ret = SWAP_SUCCESS;
1702 return ret;
1706 * try_to_munlock - try to munlock a page
1707 * @page: the page to be munlocked
1709 * Called from munlock code. Checks all of the VMAs mapping the page
1710 * to make sure nobody else has this page mlocked. The page will be
1711 * returned with PG_mlocked cleared if no other vmas have it mlocked.
1713 * Return values are:
1715 * SWAP_AGAIN - no vma is holding page mlocked, or,
1716 * SWAP_AGAIN - page mapped in mlocked vma -- couldn't acquire mmap sem
1717 * SWAP_FAIL - page cannot be located at present
1718 * SWAP_MLOCK - page is now mlocked.
1720 int try_to_munlock(struct page *page)
1722 VM_BUG_ON(!PageLocked(page) || PageLRU(page));
1724 if (unlikely(PageKsm(page)))
1725 return try_to_unmap_ksm(page, TTU_MUNLOCK);
1726 else if (PageAnon(page))
1727 return try_to_unmap_anon(page, TTU_MUNLOCK);
1728 else
1729 return try_to_unmap_file(page, TTU_MUNLOCK);
1732 void __put_anon_vma(struct anon_vma *anon_vma)
1734 struct anon_vma *root = anon_vma->root;
1736 anon_vma_free(anon_vma);
1737 if (root != anon_vma && atomic_dec_and_test(&root->refcount))
1738 anon_vma_free(root);
1741 #ifdef CONFIG_MIGRATION
1743 * rmap_walk() and its helpers rmap_walk_anon() and rmap_walk_file():
1744 * Called by migrate.c to remove migration ptes, but might be used more later.
1746 static int rmap_walk_anon(struct page *page, int (*rmap_one)(struct page *,
1747 struct vm_area_struct *, unsigned long, void *), void *arg)
1749 struct anon_vma *anon_vma;
1750 struct anon_vma_chain *avc;
1751 int ret = SWAP_AGAIN;
1754 * Note: remove_migration_ptes() cannot use page_lock_anon_vma()
1755 * because that depends on page_mapped(); but not all its usages
1756 * are holding mmap_sem. Users without mmap_sem are required to
1757 * take a reference count to prevent the anon_vma disappearing
1759 anon_vma = page_anon_vma(page);
1760 if (!anon_vma)
1761 return ret;
1762 anon_vma_lock(anon_vma);
1763 list_for_each_entry(avc, &anon_vma->head, same_anon_vma) {
1764 struct vm_area_struct *vma = avc->vma;
1765 unsigned long address = vma_address(page, vma);
1766 if (address == -EFAULT)
1767 continue;
1768 ret = rmap_one(page, vma, address, arg);
1769 if (ret != SWAP_AGAIN)
1770 break;
1772 anon_vma_unlock(anon_vma);
1773 return ret;
1776 static int rmap_walk_file(struct page *page, int (*rmap_one)(struct page *,
1777 struct vm_area_struct *, unsigned long, void *), void *arg)
1779 struct address_space *mapping = page->mapping;
1780 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
1781 struct vm_area_struct *vma;
1782 struct prio_tree_iter iter;
1783 int ret = SWAP_AGAIN;
1785 if (!mapping)
1786 return ret;
1787 mutex_lock(&mapping->i_mmap_mutex);
1788 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
1789 unsigned long address = vma_address(page, vma);
1790 if (address == -EFAULT)
1791 continue;
1792 ret = rmap_one(page, vma, address, arg);
1793 if (ret != SWAP_AGAIN)
1794 break;
1797 * No nonlinear handling: being always shared, nonlinear vmas
1798 * never contain migration ptes. Decide what to do about this
1799 * limitation to linear when we need rmap_walk() on nonlinear.
1801 mutex_unlock(&mapping->i_mmap_mutex);
1802 return ret;
1805 int rmap_walk(struct page *page, int (*rmap_one)(struct page *,
1806 struct vm_area_struct *, unsigned long, void *), void *arg)
1808 VM_BUG_ON(!PageLocked(page));
1810 if (unlikely(PageKsm(page)))
1811 return rmap_walk_ksm(page, rmap_one, arg);
1812 else if (PageAnon(page))
1813 return rmap_walk_anon(page, rmap_one, arg);
1814 else
1815 return rmap_walk_file(page, rmap_one, arg);
1817 #endif /* CONFIG_MIGRATION */
1819 #ifdef CONFIG_HUGETLB_PAGE
1821 * The following three functions are for anonymous (private mapped) hugepages.
1822 * Unlike common anonymous pages, anonymous hugepages have no accounting code
1823 * and no lru code, because we handle hugepages differently from common pages.
1825 static void __hugepage_set_anon_rmap(struct page *page,
1826 struct vm_area_struct *vma, unsigned long address, int exclusive)
1828 struct anon_vma *anon_vma = vma->anon_vma;
1830 BUG_ON(!anon_vma);
1832 if (PageAnon(page))
1833 return;
1834 if (!exclusive)
1835 anon_vma = anon_vma->root;
1837 anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
1838 page->mapping = (struct address_space *) anon_vma;
1839 page->index = linear_page_index(vma, address);
1842 void hugepage_add_anon_rmap(struct page *page,
1843 struct vm_area_struct *vma, unsigned long address)
1845 struct anon_vma *anon_vma = vma->anon_vma;
1846 int first;
1848 BUG_ON(!PageLocked(page));
1849 BUG_ON(!anon_vma);
1850 /* address might be in next vma when migration races vma_adjust */
1851 first = atomic_inc_and_test(&page->_mapcount);
1852 if (first)
1853 __hugepage_set_anon_rmap(page, vma, address, 0);
1856 void hugepage_add_new_anon_rmap(struct page *page,
1857 struct vm_area_struct *vma, unsigned long address)
1859 BUG_ON(address < vma->vm_start || address >= vma->vm_end);
1860 atomic_set(&page->_mapcount, 0);
1861 __hugepage_set_anon_rmap(page, vma, address, 1);
1863 #endif /* CONFIG_HUGETLB_PAGE */