2df75a119c831b92eeb8d2e5b49632ac7262f74b
[linux-2.6-microblaze.git] / mm / rmap.c
1 /*
2  * mm/rmap.c - physical to virtual reverse mappings
3  *
4  * Copyright 2001, Rik van Riel <riel@conectiva.com.br>
5  * Released under the General Public License (GPL).
6  *
7  * Simple, low overhead reverse mapping scheme.
8  * Please try to keep this thing as modular as possible.
9  *
10  * Provides methods for unmapping each kind of mapped page:
11  * the anon methods track anonymous pages, and
12  * the file methods track pages belonging to an inode.
13  *
14  * Original design by Rik van Riel <riel@conectiva.com.br> 2001
15  * File methods by Dave McCracken <dmccr@us.ibm.com> 2003, 2004
16  * Anonymous methods by Andrea Arcangeli <andrea@suse.de> 2004
17  * Contributions by Hugh Dickins 2003, 2004
18  */
19
20 /*
21  * Lock ordering in mm:
22  *
23  * inode->i_mutex       (while writing or truncating, not reading or faulting)
24  *   mm->mmap_sem
25  *     page->flags PG_locked (lock_page)   * (see huegtlbfs below)
26  *       hugetlbfs_i_mmap_rwsem_key (in huge_pmd_share)
27  *         mapping->i_mmap_rwsem
28  *           hugetlb_fault_mutex (hugetlbfs specific page fault mutex)
29  *           anon_vma->rwsem
30  *             mm->page_table_lock or pte_lock
31  *               pgdat->lru_lock (in mark_page_accessed, isolate_lru_page)
32  *               swap_lock (in swap_duplicate, swap_info_get)
33  *                 mmlist_lock (in mmput, drain_mmlist and others)
34  *                 mapping->private_lock (in __set_page_dirty_buffers)
35  *                   mem_cgroup_{begin,end}_page_stat (memcg->move_lock)
36  *                     i_pages lock (widely used)
37  *                 inode->i_lock (in set_page_dirty's __mark_inode_dirty)
38  *                 bdi.wb->list_lock (in set_page_dirty's __mark_inode_dirty)
39  *                   sb_lock (within inode_lock in fs/fs-writeback.c)
40  *                   i_pages lock (widely used, in set_page_dirty,
41  *                             in arch-dependent flush_dcache_mmap_lock,
42  *                             within bdi.wb->list_lock in __sync_single_inode)
43  *
44  * anon_vma->rwsem,mapping->i_mutex      (memory_failure, collect_procs_anon)
45  *   ->tasklist_lock
46  *     pte map lock
47  *
48  * * hugetlbfs PageHuge() pages take locks in this order:
49  *         mapping->i_mmap_rwsem
50  *           hugetlb_fault_mutex (hugetlbfs specific page fault mutex)
51  *             page->flags PG_locked (lock_page)
52  */
53
54 #include <linux/mm.h>
55 #include <linux/sched/mm.h>
56 #include <linux/sched/task.h>
57 #include <linux/pagemap.h>
58 #include <linux/swap.h>
59 #include <linux/swapops.h>
60 #include <linux/slab.h>
61 #include <linux/init.h>
62 #include <linux/ksm.h>
63 #include <linux/rmap.h>
64 #include <linux/rcupdate.h>
65 #include <linux/export.h>
66 #include <linux/memcontrol.h>
67 #include <linux/mmu_notifier.h>
68 #include <linux/migrate.h>
69 #include <linux/hugetlb.h>
70 #include <linux/huge_mm.h>
71 #include <linux/backing-dev.h>
72 #include <linux/page_idle.h>
73 #include <linux/memremap.h>
74 #include <linux/userfaultfd_k.h>
75
76 #include <asm/tlbflush.h>
77
78 #include <trace/events/tlb.h>
79
80 #include "internal.h"
81
82 static struct kmem_cache *anon_vma_cachep;
83 static struct kmem_cache *anon_vma_chain_cachep;
84
85 static inline struct anon_vma *anon_vma_alloc(void)
86 {
87         struct anon_vma *anon_vma;
88
89         anon_vma = kmem_cache_alloc(anon_vma_cachep, GFP_KERNEL);
90         if (anon_vma) {
91                 atomic_set(&anon_vma->refcount, 1);
92                 anon_vma->degree = 1;   /* Reference for first vma */
93                 anon_vma->parent = anon_vma;
94                 /*
95                  * Initialise the anon_vma root to point to itself. If called
96                  * from fork, the root will be reset to the parents anon_vma.
97                  */
98                 anon_vma->root = anon_vma;
99         }
100
101         return anon_vma;
102 }
103
104 static inline void anon_vma_free(struct anon_vma *anon_vma)
105 {
106         VM_BUG_ON(atomic_read(&anon_vma->refcount));
107
108         /*
109          * Synchronize against page_lock_anon_vma_read() such that
110          * we can safely hold the lock without the anon_vma getting
111          * freed.
112          *
113          * Relies on the full mb implied by the atomic_dec_and_test() from
114          * put_anon_vma() against the acquire barrier implied by
115          * down_read_trylock() from page_lock_anon_vma_read(). This orders:
116          *
117          * page_lock_anon_vma_read()    VS      put_anon_vma()
118          *   down_read_trylock()                  atomic_dec_and_test()
119          *   LOCK                                 MB
120          *   atomic_read()                        rwsem_is_locked()
121          *
122          * LOCK should suffice since the actual taking of the lock must
123          * happen _before_ what follows.
124          */
125         might_sleep();
126         if (rwsem_is_locked(&anon_vma->root->rwsem)) {
127                 anon_vma_lock_write(anon_vma);
128                 anon_vma_unlock_write(anon_vma);
129         }
130
131         kmem_cache_free(anon_vma_cachep, anon_vma);
132 }
133
134 static inline struct anon_vma_chain *anon_vma_chain_alloc(gfp_t gfp)
135 {
136         return kmem_cache_alloc(anon_vma_chain_cachep, gfp);
137 }
138
139 static void anon_vma_chain_free(struct anon_vma_chain *anon_vma_chain)
140 {
141         kmem_cache_free(anon_vma_chain_cachep, anon_vma_chain);
142 }
143
144 static void anon_vma_chain_link(struct vm_area_struct *vma,
145                                 struct anon_vma_chain *avc,
146                                 struct anon_vma *anon_vma)
147 {
148         avc->vma = vma;
149         avc->anon_vma = anon_vma;
150         list_add(&avc->same_vma, &vma->anon_vma_chain);
151         anon_vma_interval_tree_insert(avc, &anon_vma->rb_root);
152 }
153
154 /**
155  * __anon_vma_prepare - attach an anon_vma to a memory region
156  * @vma: the memory region in question
157  *
158  * This makes sure the memory mapping described by 'vma' has
159  * an 'anon_vma' attached to it, so that we can associate the
160  * anonymous pages mapped into it with that anon_vma.
161  *
162  * The common case will be that we already have one, which
163  * is handled inline by anon_vma_prepare(). But if
164  * not we either need to find an adjacent mapping that we
165  * can re-use the anon_vma from (very common when the only
166  * reason for splitting a vma has been mprotect()), or we
167  * allocate a new one.
168  *
169  * Anon-vma allocations are very subtle, because we may have
170  * optimistically looked up an anon_vma in page_lock_anon_vma_read()
171  * and that may actually touch the spinlock even in the newly
172  * allocated vma (it depends on RCU to make sure that the
173  * anon_vma isn't actually destroyed).
174  *
175  * As a result, we need to do proper anon_vma locking even
176  * for the new allocation. At the same time, we do not want
177  * to do any locking for the common case of already having
178  * an anon_vma.
179  *
180  * This must be called with the mmap_sem held for reading.
181  */
182 int __anon_vma_prepare(struct vm_area_struct *vma)
183 {
184         struct mm_struct *mm = vma->vm_mm;
185         struct anon_vma *anon_vma, *allocated;
186         struct anon_vma_chain *avc;
187
188         might_sleep();
189
190         avc = anon_vma_chain_alloc(GFP_KERNEL);
191         if (!avc)
192                 goto out_enomem;
193
194         anon_vma = find_mergeable_anon_vma(vma);
195         allocated = NULL;
196         if (!anon_vma) {
197                 anon_vma = anon_vma_alloc();
198                 if (unlikely(!anon_vma))
199                         goto out_enomem_free_avc;
200                 allocated = anon_vma;
201         }
202
203         anon_vma_lock_write(anon_vma);
204         /* page_table_lock to protect against threads */
205         spin_lock(&mm->page_table_lock);
206         if (likely(!vma->anon_vma)) {
207                 vma->anon_vma = anon_vma;
208                 anon_vma_chain_link(vma, avc, anon_vma);
209                 /* vma reference or self-parent link for new root */
210                 anon_vma->degree++;
211                 allocated = NULL;
212                 avc = NULL;
213         }
214         spin_unlock(&mm->page_table_lock);
215         anon_vma_unlock_write(anon_vma);
216
217         if (unlikely(allocated))
218                 put_anon_vma(allocated);
219         if (unlikely(avc))
220                 anon_vma_chain_free(avc);
221
222         return 0;
223
224  out_enomem_free_avc:
225         anon_vma_chain_free(avc);
226  out_enomem:
227         return -ENOMEM;
228 }
229
230 /*
231  * This is a useful helper function for locking the anon_vma root as
232  * we traverse the vma->anon_vma_chain, looping over anon_vma's that
233  * have the same vma.
234  *
235  * Such anon_vma's should have the same root, so you'd expect to see
236  * just a single mutex_lock for the whole traversal.
237  */
238 static inline struct anon_vma *lock_anon_vma_root(struct anon_vma *root, struct anon_vma *anon_vma)
239 {
240         struct anon_vma *new_root = anon_vma->root;
241         if (new_root != root) {
242                 if (WARN_ON_ONCE(root))
243                         up_write(&root->rwsem);
244                 root = new_root;
245                 down_write(&root->rwsem);
246         }
247         return root;
248 }
249
250 static inline void unlock_anon_vma_root(struct anon_vma *root)
251 {
252         if (root)
253                 up_write(&root->rwsem);
254 }
255
256 /*
257  * Attach the anon_vmas from src to dst.
258  * Returns 0 on success, -ENOMEM on failure.
259  *
260  * anon_vma_clone() is called by __vma_split(), __split_vma(), copy_vma() and
261  * anon_vma_fork(). The first three want an exact copy of src, while the last
262  * one, anon_vma_fork(), may try to reuse an existing anon_vma to prevent
263  * endless growth of anon_vma. Since dst->anon_vma is set to NULL before call,
264  * we can identify this case by checking (!dst->anon_vma && src->anon_vma).
265  *
266  * If (!dst->anon_vma && src->anon_vma) is true, this function tries to find
267  * and reuse existing anon_vma which has no vmas and only one child anon_vma.
268  * This prevents degradation of anon_vma hierarchy to endless linear chain in
269  * case of constantly forking task. On the other hand, an anon_vma with more
270  * than one child isn't reused even if there was no alive vma, thus rmap
271  * walker has a good chance of avoiding scanning the whole hierarchy when it
272  * searches where page is mapped.
273  */
274 int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src)
275 {
276         struct anon_vma_chain *avc, *pavc;
277         struct anon_vma *root = NULL;
278         struct vm_area_struct *prev = dst->vm_prev, *pprev = src->vm_prev;
279
280         /*
281          * If parent share anon_vma with its vm_prev, keep this sharing in in
282          * child.
283          *
284          * 1. Parent has vm_prev, which implies we have vm_prev.
285          * 2. Parent and its vm_prev have the same anon_vma.
286          */
287         if (!dst->anon_vma && src->anon_vma &&
288             pprev && pprev->anon_vma == src->anon_vma)
289                 dst->anon_vma = prev->anon_vma;
290
291
292         list_for_each_entry_reverse(pavc, &src->anon_vma_chain, same_vma) {
293                 struct anon_vma *anon_vma;
294
295                 avc = anon_vma_chain_alloc(GFP_NOWAIT | __GFP_NOWARN);
296                 if (unlikely(!avc)) {
297                         unlock_anon_vma_root(root);
298                         root = NULL;
299                         avc = anon_vma_chain_alloc(GFP_KERNEL);
300                         if (!avc)
301                                 goto enomem_failure;
302                 }
303                 anon_vma = pavc->anon_vma;
304                 root = lock_anon_vma_root(root, anon_vma);
305                 anon_vma_chain_link(dst, avc, anon_vma);
306
307                 /*
308                  * Reuse existing anon_vma if its degree lower than two,
309                  * that means it has no vma and only one anon_vma child.
310                  *
311                  * Do not chose parent anon_vma, otherwise first child
312                  * will always reuse it. Root anon_vma is never reused:
313                  * it has self-parent reference and at least one child.
314                  */
315                 if (!dst->anon_vma && src->anon_vma &&
316                     anon_vma != src->anon_vma && anon_vma->degree < 2)
317                         dst->anon_vma = anon_vma;
318         }
319         if (dst->anon_vma)
320                 dst->anon_vma->degree++;
321         unlock_anon_vma_root(root);
322         return 0;
323
324  enomem_failure:
325         /*
326          * dst->anon_vma is dropped here otherwise its degree can be incorrectly
327          * decremented in unlink_anon_vmas().
328          * We can safely do this because callers of anon_vma_clone() don't care
329          * about dst->anon_vma if anon_vma_clone() failed.
330          */
331         dst->anon_vma = NULL;
332         unlink_anon_vmas(dst);
333         return -ENOMEM;
334 }
335
336 /*
337  * Attach vma to its own anon_vma, as well as to the anon_vmas that
338  * the corresponding VMA in the parent process is attached to.
339  * Returns 0 on success, non-zero on failure.
340  */
341 int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma)
342 {
343         struct anon_vma_chain *avc;
344         struct anon_vma *anon_vma;
345         int error;
346
347         /* Don't bother if the parent process has no anon_vma here. */
348         if (!pvma->anon_vma)
349                 return 0;
350
351         /* Drop inherited anon_vma, we'll reuse existing or allocate new. */
352         vma->anon_vma = NULL;
353
354         /*
355          * First, attach the new VMA to the parent VMA's anon_vmas,
356          * so rmap can find non-COWed pages in child processes.
357          */
358         error = anon_vma_clone(vma, pvma);
359         if (error)
360                 return error;
361
362         /* An existing anon_vma has been reused, all done then. */
363         if (vma->anon_vma)
364                 return 0;
365
366         /* Then add our own anon_vma. */
367         anon_vma = anon_vma_alloc();
368         if (!anon_vma)
369                 goto out_error;
370         avc = anon_vma_chain_alloc(GFP_KERNEL);
371         if (!avc)
372                 goto out_error_free_anon_vma;
373
374         /*
375          * The root anon_vma's spinlock is the lock actually used when we
376          * lock any of the anon_vmas in this anon_vma tree.
377          */
378         anon_vma->root = pvma->anon_vma->root;
379         anon_vma->parent = pvma->anon_vma;
380         /*
381          * With refcounts, an anon_vma can stay around longer than the
382          * process it belongs to. The root anon_vma needs to be pinned until
383          * this anon_vma is freed, because the lock lives in the root.
384          */
385         get_anon_vma(anon_vma->root);
386         /* Mark this anon_vma as the one where our new (COWed) pages go. */
387         vma->anon_vma = anon_vma;
388         anon_vma_lock_write(anon_vma);
389         anon_vma_chain_link(vma, avc, anon_vma);
390         anon_vma->parent->degree++;
391         anon_vma_unlock_write(anon_vma);
392
393         return 0;
394
395  out_error_free_anon_vma:
396         put_anon_vma(anon_vma);
397  out_error:
398         unlink_anon_vmas(vma);
399         return -ENOMEM;
400 }
401
402 void unlink_anon_vmas(struct vm_area_struct *vma)
403 {
404         struct anon_vma_chain *avc, *next;
405         struct anon_vma *root = NULL;
406
407         /*
408          * Unlink each anon_vma chained to the VMA.  This list is ordered
409          * from newest to oldest, ensuring the root anon_vma gets freed last.
410          */
411         list_for_each_entry_safe(avc, next, &vma->anon_vma_chain, same_vma) {
412                 struct anon_vma *anon_vma = avc->anon_vma;
413
414                 root = lock_anon_vma_root(root, anon_vma);
415                 anon_vma_interval_tree_remove(avc, &anon_vma->rb_root);
416
417                 /*
418                  * Leave empty anon_vmas on the list - we'll need
419                  * to free them outside the lock.
420                  */
421                 if (RB_EMPTY_ROOT(&anon_vma->rb_root.rb_root)) {
422                         anon_vma->parent->degree--;
423                         continue;
424                 }
425
426                 list_del(&avc->same_vma);
427                 anon_vma_chain_free(avc);
428         }
429         if (vma->anon_vma)
430                 vma->anon_vma->degree--;
431         unlock_anon_vma_root(root);
432
433         /*
434          * Iterate the list once more, it now only contains empty and unlinked
435          * anon_vmas, destroy them. Could not do before due to __put_anon_vma()
436          * needing to write-acquire the anon_vma->root->rwsem.
437          */
438         list_for_each_entry_safe(avc, next, &vma->anon_vma_chain, same_vma) {
439                 struct anon_vma *anon_vma = avc->anon_vma;
440
441                 VM_WARN_ON(anon_vma->degree);
442                 put_anon_vma(anon_vma);
443
444                 list_del(&avc->same_vma);
445                 anon_vma_chain_free(avc);
446         }
447 }
448
449 static void anon_vma_ctor(void *data)
450 {
451         struct anon_vma *anon_vma = data;
452
453         init_rwsem(&anon_vma->rwsem);
454         atomic_set(&anon_vma->refcount, 0);
455         anon_vma->rb_root = RB_ROOT_CACHED;
456 }
457
458 void __init anon_vma_init(void)
459 {
460         anon_vma_cachep = kmem_cache_create("anon_vma", sizeof(struct anon_vma),
461                         0, SLAB_TYPESAFE_BY_RCU|SLAB_PANIC|SLAB_ACCOUNT,
462                         anon_vma_ctor);
463         anon_vma_chain_cachep = KMEM_CACHE(anon_vma_chain,
464                         SLAB_PANIC|SLAB_ACCOUNT);
465 }
466
467 /*
468  * Getting a lock on a stable anon_vma from a page off the LRU is tricky!
469  *
470  * Since there is no serialization what so ever against page_remove_rmap()
471  * the best this function can do is return a locked anon_vma that might
472  * have been relevant to this page.
473  *
474  * The page might have been remapped to a different anon_vma or the anon_vma
475  * returned may already be freed (and even reused).
476  *
477  * In case it was remapped to a different anon_vma, the new anon_vma will be a
478  * child of the old anon_vma, and the anon_vma lifetime rules will therefore
479  * ensure that any anon_vma obtained from the page will still be valid for as
480  * long as we observe page_mapped() [ hence all those page_mapped() tests ].
481  *
482  * All users of this function must be very careful when walking the anon_vma
483  * chain and verify that the page in question is indeed mapped in it
484  * [ something equivalent to page_mapped_in_vma() ].
485  *
486  * Since anon_vma's slab is SLAB_TYPESAFE_BY_RCU and we know from
487  * page_remove_rmap() that the anon_vma pointer from page->mapping is valid
488  * if there is a mapcount, we can dereference the anon_vma after observing
489  * those.
490  */
491 struct anon_vma *page_get_anon_vma(struct page *page)
492 {
493         struct anon_vma *anon_vma = NULL;
494         unsigned long anon_mapping;
495
496         rcu_read_lock();
497         anon_mapping = (unsigned long)READ_ONCE(page->mapping);
498         if ((anon_mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON)
499                 goto out;
500         if (!page_mapped(page))
501                 goto out;
502
503         anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON);
504         if (!atomic_inc_not_zero(&anon_vma->refcount)) {
505                 anon_vma = NULL;
506                 goto out;
507         }
508
509         /*
510          * If this page is still mapped, then its anon_vma cannot have been
511          * freed.  But if it has been unmapped, we have no security against the
512          * anon_vma structure being freed and reused (for another anon_vma:
513          * SLAB_TYPESAFE_BY_RCU guarantees that - so the atomic_inc_not_zero()
514          * above cannot corrupt).
515          */
516         if (!page_mapped(page)) {
517                 rcu_read_unlock();
518                 put_anon_vma(anon_vma);
519                 return NULL;
520         }
521 out:
522         rcu_read_unlock();
523
524         return anon_vma;
525 }
526
527 /*
528  * Similar to page_get_anon_vma() except it locks the anon_vma.
529  *
530  * Its a little more complex as it tries to keep the fast path to a single
531  * atomic op -- the trylock. If we fail the trylock, we fall back to getting a
532  * reference like with page_get_anon_vma() and then block on the mutex.
533  */
534 struct anon_vma *page_lock_anon_vma_read(struct page *page)
535 {
536         struct anon_vma *anon_vma = NULL;
537         struct anon_vma *root_anon_vma;
538         unsigned long anon_mapping;
539
540         rcu_read_lock();
541         anon_mapping = (unsigned long)READ_ONCE(page->mapping);
542         if ((anon_mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON)
543                 goto out;
544         if (!page_mapped(page))
545                 goto out;
546
547         anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON);
548         root_anon_vma = READ_ONCE(anon_vma->root);
549         if (down_read_trylock(&root_anon_vma->rwsem)) {
550                 /*
551                  * If the page is still mapped, then this anon_vma is still
552                  * its anon_vma, and holding the mutex ensures that it will
553                  * not go away, see anon_vma_free().
554                  */
555                 if (!page_mapped(page)) {
556                         up_read(&root_anon_vma->rwsem);
557                         anon_vma = NULL;
558                 }
559                 goto out;
560         }
561
562         /* trylock failed, we got to sleep */
563         if (!atomic_inc_not_zero(&anon_vma->refcount)) {
564                 anon_vma = NULL;
565                 goto out;
566         }
567
568         if (!page_mapped(page)) {
569                 rcu_read_unlock();
570                 put_anon_vma(anon_vma);
571                 return NULL;
572         }
573
574         /* we pinned the anon_vma, its safe to sleep */
575         rcu_read_unlock();
576         anon_vma_lock_read(anon_vma);
577
578         if (atomic_dec_and_test(&anon_vma->refcount)) {
579                 /*
580                  * Oops, we held the last refcount, release the lock
581                  * and bail -- can't simply use put_anon_vma() because
582                  * we'll deadlock on the anon_vma_lock_write() recursion.
583                  */
584                 anon_vma_unlock_read(anon_vma);
585                 __put_anon_vma(anon_vma);
586                 anon_vma = NULL;
587         }
588
589         return anon_vma;
590
591 out:
592         rcu_read_unlock();
593         return anon_vma;
594 }
595
596 void page_unlock_anon_vma_read(struct anon_vma *anon_vma)
597 {
598         anon_vma_unlock_read(anon_vma);
599 }
600
601 #ifdef CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH
602 /*
603  * Flush TLB entries for recently unmapped pages from remote CPUs. It is
604  * important if a PTE was dirty when it was unmapped that it's flushed
605  * before any IO is initiated on the page to prevent lost writes. Similarly,
606  * it must be flushed before freeing to prevent data leakage.
607  */
608 void try_to_unmap_flush(void)
609 {
610         struct tlbflush_unmap_batch *tlb_ubc = &current->tlb_ubc;
611
612         if (!tlb_ubc->flush_required)
613                 return;
614
615         arch_tlbbatch_flush(&tlb_ubc->arch);
616         tlb_ubc->flush_required = false;
617         tlb_ubc->writable = false;
618 }
619
620 /* Flush iff there are potentially writable TLB entries that can race with IO */
621 void try_to_unmap_flush_dirty(void)
622 {
623         struct tlbflush_unmap_batch *tlb_ubc = &current->tlb_ubc;
624
625         if (tlb_ubc->writable)
626                 try_to_unmap_flush();
627 }
628
629 static void set_tlb_ubc_flush_pending(struct mm_struct *mm, bool writable)
630 {
631         struct tlbflush_unmap_batch *tlb_ubc = &current->tlb_ubc;
632
633         arch_tlbbatch_add_mm(&tlb_ubc->arch, mm);
634         tlb_ubc->flush_required = true;
635
636         /*
637          * Ensure compiler does not re-order the setting of tlb_flush_batched
638          * before the PTE is cleared.
639          */
640         barrier();
641         mm->tlb_flush_batched = true;
642
643         /*
644          * If the PTE was dirty then it's best to assume it's writable. The
645          * caller must use try_to_unmap_flush_dirty() or try_to_unmap_flush()
646          * before the page is queued for IO.
647          */
648         if (writable)
649                 tlb_ubc->writable = true;
650 }
651
652 /*
653  * Returns true if the TLB flush should be deferred to the end of a batch of
654  * unmap operations to reduce IPIs.
655  */
656 static bool should_defer_flush(struct mm_struct *mm, enum ttu_flags flags)
657 {
658         bool should_defer = false;
659
660         if (!(flags & TTU_BATCH_FLUSH))
661                 return false;
662
663         /* If remote CPUs need to be flushed then defer batch the flush */
664         if (cpumask_any_but(mm_cpumask(mm), get_cpu()) < nr_cpu_ids)
665                 should_defer = true;
666         put_cpu();
667
668         return should_defer;
669 }
670
671 /*
672  * Reclaim unmaps pages under the PTL but do not flush the TLB prior to
673  * releasing the PTL if TLB flushes are batched. It's possible for a parallel
674  * operation such as mprotect or munmap to race between reclaim unmapping
675  * the page and flushing the page. If this race occurs, it potentially allows
676  * access to data via a stale TLB entry. Tracking all mm's that have TLB
677  * batching in flight would be expensive during reclaim so instead track
678  * whether TLB batching occurred in the past and if so then do a flush here
679  * if required. This will cost one additional flush per reclaim cycle paid
680  * by the first operation at risk such as mprotect and mumap.
681  *
682  * This must be called under the PTL so that an access to tlb_flush_batched
683  * that is potentially a "reclaim vs mprotect/munmap/etc" race will synchronise
684  * via the PTL.
685  */
686 void flush_tlb_batched_pending(struct mm_struct *mm)
687 {
688         if (mm->tlb_flush_batched) {
689                 flush_tlb_mm(mm);
690
691                 /*
692                  * Do not allow the compiler to re-order the clearing of
693                  * tlb_flush_batched before the tlb is flushed.
694                  */
695                 barrier();
696                 mm->tlb_flush_batched = false;
697         }
698 }
699 #else
700 static void set_tlb_ubc_flush_pending(struct mm_struct *mm, bool writable)
701 {
702 }
703
704 static bool should_defer_flush(struct mm_struct *mm, enum ttu_flags flags)
705 {
706         return false;
707 }
708 #endif /* CONFIG_ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH */
709
710 /*
711  * At what user virtual address is page expected in vma?
712  * Caller should check the page is actually part of the vma.
713  */
714 unsigned long page_address_in_vma(struct page *page, struct vm_area_struct *vma)
715 {
716         unsigned long address;
717         if (PageAnon(page)) {
718                 struct anon_vma *page__anon_vma = page_anon_vma(page);
719                 /*
720                  * Note: swapoff's unuse_vma() is more efficient with this
721                  * check, and needs it to match anon_vma when KSM is active.
722                  */
723                 if (!vma->anon_vma || !page__anon_vma ||
724                     vma->anon_vma->root != page__anon_vma->root)
725                         return -EFAULT;
726         } else if (page->mapping) {
727                 if (!vma->vm_file || vma->vm_file->f_mapping != page->mapping)
728                         return -EFAULT;
729         } else
730                 return -EFAULT;
731         address = __vma_address(page, vma);
732         if (unlikely(address < vma->vm_start || address >= vma->vm_end))
733                 return -EFAULT;
734         return address;
735 }
736
737 pmd_t *mm_find_pmd(struct mm_struct *mm, unsigned long address)
738 {
739         pgd_t *pgd;
740         p4d_t *p4d;
741         pud_t *pud;
742         pmd_t *pmd = NULL;
743         pmd_t pmde;
744
745         pgd = pgd_offset(mm, address);
746         if (!pgd_present(*pgd))
747                 goto out;
748
749         p4d = p4d_offset(pgd, address);
750         if (!p4d_present(*p4d))
751                 goto out;
752
753         pud = pud_offset(p4d, address);
754         if (!pud_present(*pud))
755                 goto out;
756
757         pmd = pmd_offset(pud, address);
758         /*
759          * Some THP functions use the sequence pmdp_huge_clear_flush(), set_pmd_at()
760          * without holding anon_vma lock for write.  So when looking for a
761          * genuine pmde (in which to find pte), test present and !THP together.
762          */
763         pmde = *pmd;
764         barrier();
765         if (!pmd_present(pmde) || pmd_trans_huge(pmde))
766                 pmd = NULL;
767 out:
768         return pmd;
769 }
770
771 struct page_referenced_arg {
772         int mapcount;
773         int referenced;
774         unsigned long vm_flags;
775         struct mem_cgroup *memcg;
776 };
777 /*
778  * arg: page_referenced_arg will be passed
779  */
780 static bool page_referenced_one(struct page *page, struct vm_area_struct *vma,
781                         unsigned long address, void *arg)
782 {
783         struct page_referenced_arg *pra = arg;
784         struct page_vma_mapped_walk pvmw = {
785                 .page = page,
786                 .vma = vma,
787                 .address = address,
788         };
789         int referenced = 0;
790
791         while (page_vma_mapped_walk(&pvmw)) {
792                 address = pvmw.address;
793
794                 if (vma->vm_flags & VM_LOCKED) {
795                         page_vma_mapped_walk_done(&pvmw);
796                         pra->vm_flags |= VM_LOCKED;
797                         return false; /* To break the loop */
798                 }
799
800                 if (pvmw.pte) {
801                         if (ptep_clear_flush_young_notify(vma, address,
802                                                 pvmw.pte)) {
803                                 /*
804                                  * Don't treat a reference through
805                                  * a sequentially read mapping as such.
806                                  * If the page has been used in another mapping,
807                                  * we will catch it; if this other mapping is
808                                  * already gone, the unmap path will have set
809                                  * PG_referenced or activated the page.
810                                  */
811                                 if (likely(!(vma->vm_flags & VM_SEQ_READ)))
812                                         referenced++;
813                         }
814                 } else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
815                         if (pmdp_clear_flush_young_notify(vma, address,
816                                                 pvmw.pmd))
817                                 referenced++;
818                 } else {
819                         /* unexpected pmd-mapped page? */
820                         WARN_ON_ONCE(1);
821                 }
822
823                 pra->mapcount--;
824         }
825
826         if (referenced)
827                 clear_page_idle(page);
828         if (test_and_clear_page_young(page))
829                 referenced++;
830
831         if (referenced) {
832                 pra->referenced++;
833                 pra->vm_flags |= vma->vm_flags;
834         }
835
836         if (!pra->mapcount)
837                 return false; /* To break the loop */
838
839         return true;
840 }
841
842 static bool invalid_page_referenced_vma(struct vm_area_struct *vma, void *arg)
843 {
844         struct page_referenced_arg *pra = arg;
845         struct mem_cgroup *memcg = pra->memcg;
846
847         if (!mm_match_cgroup(vma->vm_mm, memcg))
848                 return true;
849
850         return false;
851 }
852
853 /**
854  * page_referenced - test if the page was referenced
855  * @page: the page to test
856  * @is_locked: caller holds lock on the page
857  * @memcg: target memory cgroup
858  * @vm_flags: collect encountered vma->vm_flags who actually referenced the page
859  *
860  * Quick test_and_clear_referenced for all mappings to a page,
861  * returns the number of ptes which referenced the page.
862  */
863 int page_referenced(struct page *page,
864                     int is_locked,
865                     struct mem_cgroup *memcg,
866                     unsigned long *vm_flags)
867 {
868         int we_locked = 0;
869         struct page_referenced_arg pra = {
870                 .mapcount = total_mapcount(page),
871                 .memcg = memcg,
872         };
873         struct rmap_walk_control rwc = {
874                 .rmap_one = page_referenced_one,
875                 .arg = (void *)&pra,
876                 .anon_lock = page_lock_anon_vma_read,
877         };
878
879         *vm_flags = 0;
880         if (!pra.mapcount)
881                 return 0;
882
883         if (!page_rmapping(page))
884                 return 0;
885
886         if (!is_locked && (!PageAnon(page) || PageKsm(page))) {
887                 we_locked = trylock_page(page);
888                 if (!we_locked)
889                         return 1;
890         }
891
892         /*
893          * If we are reclaiming on behalf of a cgroup, skip
894          * counting on behalf of references from different
895          * cgroups
896          */
897         if (memcg) {
898                 rwc.invalid_vma = invalid_page_referenced_vma;
899         }
900
901         rmap_walk(page, &rwc);
902         *vm_flags = pra.vm_flags;
903
904         if (we_locked)
905                 unlock_page(page);
906
907         return pra.referenced;
908 }
909
910 static bool page_mkclean_one(struct page *page, struct vm_area_struct *vma,
911                             unsigned long address, void *arg)
912 {
913         struct page_vma_mapped_walk pvmw = {
914                 .page = page,
915                 .vma = vma,
916                 .address = address,
917                 .flags = PVMW_SYNC,
918         };
919         struct mmu_notifier_range range;
920         int *cleaned = arg;
921
922         /*
923          * We have to assume the worse case ie pmd for invalidation. Note that
924          * the page can not be free from this function.
925          */
926         mmu_notifier_range_init(&range, MMU_NOTIFY_PROTECTION_PAGE,
927                                 0, vma, vma->vm_mm, address,
928                                 min(vma->vm_end, address + page_size(page)));
929         mmu_notifier_invalidate_range_start(&range);
930
931         while (page_vma_mapped_walk(&pvmw)) {
932                 int ret = 0;
933
934                 address = pvmw.address;
935                 if (pvmw.pte) {
936                         pte_t entry;
937                         pte_t *pte = pvmw.pte;
938
939                         if (!pte_dirty(*pte) && !pte_write(*pte))
940                                 continue;
941
942                         flush_cache_page(vma, address, pte_pfn(*pte));
943                         entry = ptep_clear_flush(vma, address, pte);
944                         entry = pte_wrprotect(entry);
945                         entry = pte_mkclean(entry);
946                         set_pte_at(vma->vm_mm, address, pte, entry);
947                         ret = 1;
948                 } else {
949 #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
950                         pmd_t *pmd = pvmw.pmd;
951                         pmd_t entry;
952
953                         if (!pmd_dirty(*pmd) && !pmd_write(*pmd))
954                                 continue;
955
956                         flush_cache_page(vma, address, page_to_pfn(page));
957                         entry = pmdp_invalidate(vma, address, pmd);
958                         entry = pmd_wrprotect(entry);
959                         entry = pmd_mkclean(entry);
960                         set_pmd_at(vma->vm_mm, address, pmd, entry);
961                         ret = 1;
962 #else
963                         /* unexpected pmd-mapped page? */
964                         WARN_ON_ONCE(1);
965 #endif
966                 }
967
968                 /*
969                  * No need to call mmu_notifier_invalidate_range() as we are
970                  * downgrading page table protection not changing it to point
971                  * to a new page.
972                  *
973                  * See Documentation/vm/mmu_notifier.rst
974                  */
975                 if (ret)
976                         (*cleaned)++;
977         }
978
979         mmu_notifier_invalidate_range_end(&range);
980
981         return true;
982 }
983
984 static bool invalid_mkclean_vma(struct vm_area_struct *vma, void *arg)
985 {
986         if (vma->vm_flags & VM_SHARED)
987                 return false;
988
989         return true;
990 }
991
992 int page_mkclean(struct page *page)
993 {
994         int cleaned = 0;
995         struct address_space *mapping;
996         struct rmap_walk_control rwc = {
997                 .arg = (void *)&cleaned,
998                 .rmap_one = page_mkclean_one,
999                 .invalid_vma = invalid_mkclean_vma,
1000         };
1001
1002         BUG_ON(!PageLocked(page));
1003
1004         if (!page_mapped(page))
1005                 return 0;
1006
1007         mapping = page_mapping(page);
1008         if (!mapping)
1009                 return 0;
1010
1011         rmap_walk(page, &rwc);
1012
1013         return cleaned;
1014 }
1015 EXPORT_SYMBOL_GPL(page_mkclean);
1016
1017 /**
1018  * page_move_anon_rmap - move a page to our anon_vma
1019  * @page:       the page to move to our anon_vma
1020  * @vma:        the vma the page belongs to
1021  *
1022  * When a page belongs exclusively to one process after a COW event,
1023  * that page can be moved into the anon_vma that belongs to just that
1024  * process, so the rmap code will not search the parent or sibling
1025  * processes.
1026  */
1027 void page_move_anon_rmap(struct page *page, struct vm_area_struct *vma)
1028 {
1029         struct anon_vma *anon_vma = vma->anon_vma;
1030
1031         page = compound_head(page);
1032
1033         VM_BUG_ON_PAGE(!PageLocked(page), page);
1034         VM_BUG_ON_VMA(!anon_vma, vma);
1035
1036         anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
1037         /*
1038          * Ensure that anon_vma and the PAGE_MAPPING_ANON bit are written
1039          * simultaneously, so a concurrent reader (eg page_referenced()'s
1040          * PageAnon()) will not see one without the other.
1041          */
1042         WRITE_ONCE(page->mapping, (struct address_space *) anon_vma);
1043 }
1044
1045 /**
1046  * __page_set_anon_rmap - set up new anonymous rmap
1047  * @page:       Page or Hugepage to add to rmap
1048  * @vma:        VM area to add page to.
1049  * @address:    User virtual address of the mapping     
1050  * @exclusive:  the page is exclusively owned by the current process
1051  */
1052 static void __page_set_anon_rmap(struct page *page,
1053         struct vm_area_struct *vma, unsigned long address, int exclusive)
1054 {
1055         struct anon_vma *anon_vma = vma->anon_vma;
1056
1057         BUG_ON(!anon_vma);
1058
1059         if (PageAnon(page))
1060                 return;
1061
1062         /*
1063          * If the page isn't exclusively mapped into this vma,
1064          * we must use the _oldest_ possible anon_vma for the
1065          * page mapping!
1066          */
1067         if (!exclusive)
1068                 anon_vma = anon_vma->root;
1069
1070         anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
1071         page->mapping = (struct address_space *) anon_vma;
1072         page->index = linear_page_index(vma, address);
1073 }
1074
1075 /**
1076  * __page_check_anon_rmap - sanity check anonymous rmap addition
1077  * @page:       the page to add the mapping to
1078  * @vma:        the vm area in which the mapping is added
1079  * @address:    the user virtual address mapped
1080  */
1081 static void __page_check_anon_rmap(struct page *page,
1082         struct vm_area_struct *vma, unsigned long address)
1083 {
1084         /*
1085          * The page's anon-rmap details (mapping and index) are guaranteed to
1086          * be set up correctly at this point.
1087          *
1088          * We have exclusion against page_add_anon_rmap because the caller
1089          * always holds the page locked, except if called from page_dup_rmap,
1090          * in which case the page is already known to be setup.
1091          *
1092          * We have exclusion against page_add_new_anon_rmap because those pages
1093          * are initially only visible via the pagetables, and the pte is locked
1094          * over the call to page_add_new_anon_rmap.
1095          */
1096         VM_BUG_ON_PAGE(page_anon_vma(page)->root != vma->anon_vma->root, page);
1097         VM_BUG_ON_PAGE(page_to_pgoff(page) != linear_page_index(vma, address),
1098                        page);
1099 }
1100
1101 /**
1102  * page_add_anon_rmap - add pte mapping to an anonymous page
1103  * @page:       the page to add the mapping to
1104  * @vma:        the vm area in which the mapping is added
1105  * @address:    the user virtual address mapped
1106  * @compound:   charge the page as compound or small page
1107  *
1108  * The caller needs to hold the pte lock, and the page must be locked in
1109  * the anon_vma case: to serialize mapping,index checking after setting,
1110  * and to ensure that PageAnon is not being upgraded racily to PageKsm
1111  * (but PageKsm is never downgraded to PageAnon).
1112  */
1113 void page_add_anon_rmap(struct page *page,
1114         struct vm_area_struct *vma, unsigned long address, bool compound)
1115 {
1116         do_page_add_anon_rmap(page, vma, address, compound ? RMAP_COMPOUND : 0);
1117 }
1118
1119 /*
1120  * Special version of the above for do_swap_page, which often runs
1121  * into pages that are exclusively owned by the current process.
1122  * Everybody else should continue to use page_add_anon_rmap above.
1123  */
1124 void do_page_add_anon_rmap(struct page *page,
1125         struct vm_area_struct *vma, unsigned long address, int flags)
1126 {
1127         bool compound = flags & RMAP_COMPOUND;
1128         bool first;
1129
1130         if (compound) {
1131                 atomic_t *mapcount;
1132                 VM_BUG_ON_PAGE(!PageLocked(page), page);
1133                 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
1134                 mapcount = compound_mapcount_ptr(page);
1135                 first = atomic_inc_and_test(mapcount);
1136         } else {
1137                 first = atomic_inc_and_test(&page->_mapcount);
1138         }
1139
1140         if (first) {
1141                 int nr = compound ? hpage_nr_pages(page) : 1;
1142                 /*
1143                  * We use the irq-unsafe __{inc|mod}_zone_page_stat because
1144                  * these counters are not modified in interrupt context, and
1145                  * pte lock(a spinlock) is held, which implies preemption
1146                  * disabled.
1147                  */
1148                 if (compound)
1149                         __inc_node_page_state(page, NR_ANON_THPS);
1150                 __mod_node_page_state(page_pgdat(page), NR_ANON_MAPPED, nr);
1151         }
1152         if (unlikely(PageKsm(page)))
1153                 return;
1154
1155         VM_BUG_ON_PAGE(!PageLocked(page), page);
1156
1157         /* address might be in next vma when migration races vma_adjust */
1158         if (first)
1159                 __page_set_anon_rmap(page, vma, address,
1160                                 flags & RMAP_EXCLUSIVE);
1161         else
1162                 __page_check_anon_rmap(page, vma, address);
1163 }
1164
1165 /**
1166  * page_add_new_anon_rmap - add pte mapping to a new anonymous page
1167  * @page:       the page to add the mapping to
1168  * @vma:        the vm area in which the mapping is added
1169  * @address:    the user virtual address mapped
1170  * @compound:   charge the page as compound or small page
1171  *
1172  * Same as page_add_anon_rmap but must only be called on *new* pages.
1173  * This means the inc-and-test can be bypassed.
1174  * Page does not have to be locked.
1175  */
1176 void page_add_new_anon_rmap(struct page *page,
1177         struct vm_area_struct *vma, unsigned long address, bool compound)
1178 {
1179         int nr = compound ? hpage_nr_pages(page) : 1;
1180
1181         VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma);
1182         __SetPageSwapBacked(page);
1183         if (compound) {
1184                 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
1185                 /* increment count (starts at -1) */
1186                 atomic_set(compound_mapcount_ptr(page), 0);
1187                 if (hpage_pincount_available(page))
1188                         atomic_set(compound_pincount_ptr(page), 0);
1189
1190                 __inc_node_page_state(page, NR_ANON_THPS);
1191         } else {
1192                 /* Anon THP always mapped first with PMD */
1193                 VM_BUG_ON_PAGE(PageTransCompound(page), page);
1194                 /* increment count (starts at -1) */
1195                 atomic_set(&page->_mapcount, 0);
1196         }
1197         __mod_node_page_state(page_pgdat(page), NR_ANON_MAPPED, nr);
1198         __page_set_anon_rmap(page, vma, address, 1);
1199 }
1200
1201 /**
1202  * page_add_file_rmap - add pte mapping to a file page
1203  * @page: the page to add the mapping to
1204  * @compound: charge the page as compound or small page
1205  *
1206  * The caller needs to hold the pte lock.
1207  */
1208 void page_add_file_rmap(struct page *page, bool compound)
1209 {
1210         int i, nr = 1;
1211
1212         VM_BUG_ON_PAGE(compound && !PageTransHuge(page), page);
1213         lock_page_memcg(page);
1214         if (compound && PageTransHuge(page)) {
1215                 for (i = 0, nr = 0; i < HPAGE_PMD_NR; i++) {
1216                         if (atomic_inc_and_test(&page[i]._mapcount))
1217                                 nr++;
1218                 }
1219                 if (!atomic_inc_and_test(compound_mapcount_ptr(page)))
1220                         goto out;
1221                 if (PageSwapBacked(page))
1222                         __inc_node_page_state(page, NR_SHMEM_PMDMAPPED);
1223                 else
1224                         __inc_node_page_state(page, NR_FILE_PMDMAPPED);
1225         } else {
1226                 if (PageTransCompound(page) && page_mapping(page)) {
1227                         VM_WARN_ON_ONCE(!PageLocked(page));
1228
1229                         SetPageDoubleMap(compound_head(page));
1230                         if (PageMlocked(page))
1231                                 clear_page_mlock(compound_head(page));
1232                 }
1233                 if (!atomic_inc_and_test(&page->_mapcount))
1234                         goto out;
1235         }
1236         __mod_lruvec_page_state(page, NR_FILE_MAPPED, nr);
1237 out:
1238         unlock_page_memcg(page);
1239 }
1240
1241 static void page_remove_file_rmap(struct page *page, bool compound)
1242 {
1243         int i, nr = 1;
1244
1245         VM_BUG_ON_PAGE(compound && !PageHead(page), page);
1246         lock_page_memcg(page);
1247
1248         /* Hugepages are not counted in NR_FILE_MAPPED for now. */
1249         if (unlikely(PageHuge(page))) {
1250                 /* hugetlb pages are always mapped with pmds */
1251                 atomic_dec(compound_mapcount_ptr(page));
1252                 goto out;
1253         }
1254
1255         /* page still mapped by someone else? */
1256         if (compound && PageTransHuge(page)) {
1257                 for (i = 0, nr = 0; i < HPAGE_PMD_NR; i++) {
1258                         if (atomic_add_negative(-1, &page[i]._mapcount))
1259                                 nr++;
1260                 }
1261                 if (!atomic_add_negative(-1, compound_mapcount_ptr(page)))
1262                         goto out;
1263                 if (PageSwapBacked(page))
1264                         __dec_node_page_state(page, NR_SHMEM_PMDMAPPED);
1265                 else
1266                         __dec_node_page_state(page, NR_FILE_PMDMAPPED);
1267         } else {
1268                 if (!atomic_add_negative(-1, &page->_mapcount))
1269                         goto out;
1270         }
1271
1272         /*
1273          * We use the irq-unsafe __{inc|mod}_lruvec_page_state because
1274          * these counters are not modified in interrupt context, and
1275          * pte lock(a spinlock) is held, which implies preemption disabled.
1276          */
1277         __mod_lruvec_page_state(page, NR_FILE_MAPPED, -nr);
1278
1279         if (unlikely(PageMlocked(page)))
1280                 clear_page_mlock(page);
1281 out:
1282         unlock_page_memcg(page);
1283 }
1284
1285 static void page_remove_anon_compound_rmap(struct page *page)
1286 {
1287         int i, nr;
1288
1289         if (!atomic_add_negative(-1, compound_mapcount_ptr(page)))
1290                 return;
1291
1292         /* Hugepages are not counted in NR_ANON_PAGES for now. */
1293         if (unlikely(PageHuge(page)))
1294                 return;
1295
1296         if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
1297                 return;
1298
1299         __dec_node_page_state(page, NR_ANON_THPS);
1300
1301         if (TestClearPageDoubleMap(page)) {
1302                 /*
1303                  * Subpages can be mapped with PTEs too. Check how many of
1304                  * them are still mapped.
1305                  */
1306                 for (i = 0, nr = 0; i < HPAGE_PMD_NR; i++) {
1307                         if (atomic_add_negative(-1, &page[i]._mapcount))
1308                                 nr++;
1309                 }
1310
1311                 /*
1312                  * Queue the page for deferred split if at least one small
1313                  * page of the compound page is unmapped, but at least one
1314                  * small page is still mapped.
1315                  */
1316                 if (nr && nr < HPAGE_PMD_NR)
1317                         deferred_split_huge_page(page);
1318         } else {
1319                 nr = HPAGE_PMD_NR;
1320         }
1321
1322         if (unlikely(PageMlocked(page)))
1323                 clear_page_mlock(page);
1324
1325         if (nr)
1326                 __mod_node_page_state(page_pgdat(page), NR_ANON_MAPPED, -nr);
1327 }
1328
1329 /**
1330  * page_remove_rmap - take down pte mapping from a page
1331  * @page:       page to remove mapping from
1332  * @compound:   uncharge the page as compound or small page
1333  *
1334  * The caller needs to hold the pte lock.
1335  */
1336 void page_remove_rmap(struct page *page, bool compound)
1337 {
1338         if (!PageAnon(page))
1339                 return page_remove_file_rmap(page, compound);
1340
1341         if (compound)
1342                 return page_remove_anon_compound_rmap(page);
1343
1344         /* page still mapped by someone else? */
1345         if (!atomic_add_negative(-1, &page->_mapcount))
1346                 return;
1347
1348         /*
1349          * We use the irq-unsafe __{inc|mod}_zone_page_stat because
1350          * these counters are not modified in interrupt context, and
1351          * pte lock(a spinlock) is held, which implies preemption disabled.
1352          */
1353         __dec_node_page_state(page, NR_ANON_MAPPED);
1354
1355         if (unlikely(PageMlocked(page)))
1356                 clear_page_mlock(page);
1357
1358         if (PageTransCompound(page))
1359                 deferred_split_huge_page(compound_head(page));
1360
1361         /*
1362          * It would be tidy to reset the PageAnon mapping here,
1363          * but that might overwrite a racing page_add_anon_rmap
1364          * which increments mapcount after us but sets mapping
1365          * before us: so leave the reset to free_unref_page,
1366          * and remember that it's only reliable while mapped.
1367          * Leaving it set also helps swapoff to reinstate ptes
1368          * faster for those pages still in swapcache.
1369          */
1370 }
1371
1372 /*
1373  * @arg: enum ttu_flags will be passed to this argument
1374  */
1375 static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
1376                      unsigned long address, void *arg)
1377 {
1378         struct mm_struct *mm = vma->vm_mm;
1379         struct page_vma_mapped_walk pvmw = {
1380                 .page = page,
1381                 .vma = vma,
1382                 .address = address,
1383         };
1384         pte_t pteval;
1385         struct page *subpage;
1386         bool ret = true;
1387         struct mmu_notifier_range range;
1388         enum ttu_flags flags = (enum ttu_flags)arg;
1389
1390         /* munlock has nothing to gain from examining un-locked vmas */
1391         if ((flags & TTU_MUNLOCK) && !(vma->vm_flags & VM_LOCKED))
1392                 return true;
1393
1394         if (IS_ENABLED(CONFIG_MIGRATION) && (flags & TTU_MIGRATION) &&
1395             is_zone_device_page(page) && !is_device_private_page(page))
1396                 return true;
1397
1398         if (flags & TTU_SPLIT_HUGE_PMD) {
1399                 split_huge_pmd_address(vma, address,
1400                                 flags & TTU_SPLIT_FREEZE, page);
1401         }
1402
1403         /*
1404          * For THP, we have to assume the worse case ie pmd for invalidation.
1405          * For hugetlb, it could be much worse if we need to do pud
1406          * invalidation in the case of pmd sharing.
1407          *
1408          * Note that the page can not be free in this function as call of
1409          * try_to_unmap() must hold a reference on the page.
1410          */
1411         mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
1412                                 address,
1413                                 min(vma->vm_end, address + page_size(page)));
1414         if (PageHuge(page)) {
1415                 /*
1416                  * If sharing is possible, start and end will be adjusted
1417                  * accordingly.
1418                  *
1419                  * If called for a huge page, caller must hold i_mmap_rwsem
1420                  * in write mode as it is possible to call huge_pmd_unshare.
1421                  */
1422                 adjust_range_if_pmd_sharing_possible(vma, &range.start,
1423                                                      &range.end);
1424         }
1425         mmu_notifier_invalidate_range_start(&range);
1426
1427         while (page_vma_mapped_walk(&pvmw)) {
1428 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1429                 /* PMD-mapped THP migration entry */
1430                 if (!pvmw.pte && (flags & TTU_MIGRATION)) {
1431                         VM_BUG_ON_PAGE(PageHuge(page) || !PageTransCompound(page), page);
1432
1433                         set_pmd_migration_entry(&pvmw, page);
1434                         continue;
1435                 }
1436 #endif
1437
1438                 /*
1439                  * If the page is mlock()d, we cannot swap it out.
1440                  * If it's recently referenced (perhaps page_referenced
1441                  * skipped over this mm) then we should reactivate it.
1442                  */
1443                 if (!(flags & TTU_IGNORE_MLOCK)) {
1444                         if (vma->vm_flags & VM_LOCKED) {
1445                                 /* PTE-mapped THP are never mlocked */
1446                                 if (!PageTransCompound(page)) {
1447                                         /*
1448                                          * Holding pte lock, we do *not* need
1449                                          * mmap_sem here
1450                                          */
1451                                         mlock_vma_page(page);
1452                                 }
1453                                 ret = false;
1454                                 page_vma_mapped_walk_done(&pvmw);
1455                                 break;
1456                         }
1457                         if (flags & TTU_MUNLOCK)
1458                                 continue;
1459                 }
1460
1461                 /* Unexpected PMD-mapped THP? */
1462                 VM_BUG_ON_PAGE(!pvmw.pte, page);
1463
1464                 subpage = page - page_to_pfn(page) + pte_pfn(*pvmw.pte);
1465                 address = pvmw.address;
1466
1467                 if (PageHuge(page)) {
1468                         /*
1469                          * To call huge_pmd_unshare, i_mmap_rwsem must be
1470                          * held in write mode.  Caller needs to explicitly
1471                          * do this outside rmap routines.
1472                          */
1473                         VM_BUG_ON(!(flags & TTU_RMAP_LOCKED));
1474                         if (huge_pmd_unshare(mm, &address, pvmw.pte)) {
1475                                 /*
1476                                  * huge_pmd_unshare unmapped an entire PMD
1477                                  * page.  There is no way of knowing exactly
1478                                  * which PMDs may be cached for this mm, so
1479                                  * we must flush them all.  start/end were
1480                                  * already adjusted above to cover this range.
1481                                  */
1482                                 flush_cache_range(vma, range.start, range.end);
1483                                 flush_tlb_range(vma, range.start, range.end);
1484                                 mmu_notifier_invalidate_range(mm, range.start,
1485                                                               range.end);
1486
1487                                 /*
1488                                  * The ref count of the PMD page was dropped
1489                                  * which is part of the way map counting
1490                                  * is done for shared PMDs.  Return 'true'
1491                                  * here.  When there is no other sharing,
1492                                  * huge_pmd_unshare returns false and we will
1493                                  * unmap the actual page and drop map count
1494                                  * to zero.
1495                                  */
1496                                 page_vma_mapped_walk_done(&pvmw);
1497                                 break;
1498                         }
1499                 }
1500
1501                 if (IS_ENABLED(CONFIG_MIGRATION) &&
1502                     (flags & TTU_MIGRATION) &&
1503                     is_zone_device_page(page)) {
1504                         swp_entry_t entry;
1505                         pte_t swp_pte;
1506
1507                         pteval = ptep_get_and_clear(mm, pvmw.address, pvmw.pte);
1508
1509                         /*
1510                          * Store the pfn of the page in a special migration
1511                          * pte. do_swap_page() will wait until the migration
1512                          * pte is removed and then restart fault handling.
1513                          */
1514                         entry = make_migration_entry(page, 0);
1515                         swp_pte = swp_entry_to_pte(entry);
1516                         if (pte_soft_dirty(pteval))
1517                                 swp_pte = pte_swp_mksoft_dirty(swp_pte);
1518                         set_pte_at(mm, pvmw.address, pvmw.pte, swp_pte);
1519                         /*
1520                          * No need to invalidate here it will synchronize on
1521                          * against the special swap migration pte.
1522                          *
1523                          * The assignment to subpage above was computed from a
1524                          * swap PTE which results in an invalid pointer.
1525                          * Since only PAGE_SIZE pages can currently be
1526                          * migrated, just set it to page. This will need to be
1527                          * changed when hugepage migrations to device private
1528                          * memory are supported.
1529                          */
1530                         subpage = page;
1531                         goto discard;
1532                 }
1533
1534                 if (!(flags & TTU_IGNORE_ACCESS)) {
1535                         if (ptep_clear_flush_young_notify(vma, address,
1536                                                 pvmw.pte)) {
1537                                 ret = false;
1538                                 page_vma_mapped_walk_done(&pvmw);
1539                                 break;
1540                         }
1541                 }
1542
1543                 /* Nuke the page table entry. */
1544                 flush_cache_page(vma, address, pte_pfn(*pvmw.pte));
1545                 if (should_defer_flush(mm, flags)) {
1546                         /*
1547                          * We clear the PTE but do not flush so potentially
1548                          * a remote CPU could still be writing to the page.
1549                          * If the entry was previously clean then the
1550                          * architecture must guarantee that a clear->dirty
1551                          * transition on a cached TLB entry is written through
1552                          * and traps if the PTE is unmapped.
1553                          */
1554                         pteval = ptep_get_and_clear(mm, address, pvmw.pte);
1555
1556                         set_tlb_ubc_flush_pending(mm, pte_dirty(pteval));
1557                 } else {
1558                         pteval = ptep_clear_flush(vma, address, pvmw.pte);
1559                 }
1560
1561                 /* Move the dirty bit to the page. Now the pte is gone. */
1562                 if (pte_dirty(pteval))
1563                         set_page_dirty(page);
1564
1565                 /* Update high watermark before we lower rss */
1566                 update_hiwater_rss(mm);
1567
1568                 if (PageHWPoison(page) && !(flags & TTU_IGNORE_HWPOISON)) {
1569                         pteval = swp_entry_to_pte(make_hwpoison_entry(subpage));
1570                         if (PageHuge(page)) {
1571                                 hugetlb_count_sub(compound_nr(page), mm);
1572                                 set_huge_swap_pte_at(mm, address,
1573                                                      pvmw.pte, pteval,
1574                                                      vma_mmu_pagesize(vma));
1575                         } else {
1576                                 dec_mm_counter(mm, mm_counter(page));
1577                                 set_pte_at(mm, address, pvmw.pte, pteval);
1578                         }
1579
1580                 } else if (pte_unused(pteval) && !userfaultfd_armed(vma)) {
1581                         /*
1582                          * The guest indicated that the page content is of no
1583                          * interest anymore. Simply discard the pte, vmscan
1584                          * will take care of the rest.
1585                          * A future reference will then fault in a new zero
1586                          * page. When userfaultfd is active, we must not drop
1587                          * this page though, as its main user (postcopy
1588                          * migration) will not expect userfaults on already
1589                          * copied pages.
1590                          */
1591                         dec_mm_counter(mm, mm_counter(page));
1592                         /* We have to invalidate as we cleared the pte */
1593                         mmu_notifier_invalidate_range(mm, address,
1594                                                       address + PAGE_SIZE);
1595                 } else if (IS_ENABLED(CONFIG_MIGRATION) &&
1596                                 (flags & (TTU_MIGRATION|TTU_SPLIT_FREEZE))) {
1597                         swp_entry_t entry;
1598                         pte_t swp_pte;
1599
1600                         if (arch_unmap_one(mm, vma, address, pteval) < 0) {
1601                                 set_pte_at(mm, address, pvmw.pte, pteval);
1602                                 ret = false;
1603                                 page_vma_mapped_walk_done(&pvmw);
1604                                 break;
1605                         }
1606
1607                         /*
1608                          * Store the pfn of the page in a special migration
1609                          * pte. do_swap_page() will wait until the migration
1610                          * pte is removed and then restart fault handling.
1611                          */
1612                         entry = make_migration_entry(subpage,
1613                                         pte_write(pteval));
1614                         swp_pte = swp_entry_to_pte(entry);
1615                         if (pte_soft_dirty(pteval))
1616                                 swp_pte = pte_swp_mksoft_dirty(swp_pte);
1617                         set_pte_at(mm, address, pvmw.pte, swp_pte);
1618                         /*
1619                          * No need to invalidate here it will synchronize on
1620                          * against the special swap migration pte.
1621                          */
1622                 } else if (PageAnon(page)) {
1623                         swp_entry_t entry = { .val = page_private(subpage) };
1624                         pte_t swp_pte;
1625                         /*
1626                          * Store the swap location in the pte.
1627                          * See handle_pte_fault() ...
1628                          */
1629                         if (unlikely(PageSwapBacked(page) != PageSwapCache(page))) {
1630                                 WARN_ON_ONCE(1);
1631                                 ret = false;
1632                                 /* We have to invalidate as we cleared the pte */
1633                                 mmu_notifier_invalidate_range(mm, address,
1634                                                         address + PAGE_SIZE);
1635                                 page_vma_mapped_walk_done(&pvmw);
1636                                 break;
1637                         }
1638
1639                         /* MADV_FREE page check */
1640                         if (!PageSwapBacked(page)) {
1641                                 if (!PageDirty(page)) {
1642                                         /* Invalidate as we cleared the pte */
1643                                         mmu_notifier_invalidate_range(mm,
1644                                                 address, address + PAGE_SIZE);
1645                                         dec_mm_counter(mm, MM_ANONPAGES);
1646                                         goto discard;
1647                                 }
1648
1649                                 /*
1650                                  * If the page was redirtied, it cannot be
1651                                  * discarded. Remap the page to page table.
1652                                  */
1653                                 set_pte_at(mm, address, pvmw.pte, pteval);
1654                                 SetPageSwapBacked(page);
1655                                 ret = false;
1656                                 page_vma_mapped_walk_done(&pvmw);
1657                                 break;
1658                         }
1659
1660                         if (swap_duplicate(entry) < 0) {
1661                                 set_pte_at(mm, address, pvmw.pte, pteval);
1662                                 ret = false;
1663                                 page_vma_mapped_walk_done(&pvmw);
1664                                 break;
1665                         }
1666                         if (arch_unmap_one(mm, vma, address, pteval) < 0) {
1667                                 set_pte_at(mm, address, pvmw.pte, pteval);
1668                                 ret = false;
1669                                 page_vma_mapped_walk_done(&pvmw);
1670                                 break;
1671                         }
1672                         if (list_empty(&mm->mmlist)) {
1673                                 spin_lock(&mmlist_lock);
1674                                 if (list_empty(&mm->mmlist))
1675                                         list_add(&mm->mmlist, &init_mm.mmlist);
1676                                 spin_unlock(&mmlist_lock);
1677                         }
1678                         dec_mm_counter(mm, MM_ANONPAGES);
1679                         inc_mm_counter(mm, MM_SWAPENTS);
1680                         swp_pte = swp_entry_to_pte(entry);
1681                         if (pte_soft_dirty(pteval))
1682                                 swp_pte = pte_swp_mksoft_dirty(swp_pte);
1683                         set_pte_at(mm, address, pvmw.pte, swp_pte);
1684                         /* Invalidate as we cleared the pte */
1685                         mmu_notifier_invalidate_range(mm, address,
1686                                                       address + PAGE_SIZE);
1687                 } else {
1688                         /*
1689                          * This is a locked file-backed page, thus it cannot
1690                          * be removed from the page cache and replaced by a new
1691                          * page before mmu_notifier_invalidate_range_end, so no
1692                          * concurrent thread might update its page table to
1693                          * point at new page while a device still is using this
1694                          * page.
1695                          *
1696                          * See Documentation/vm/mmu_notifier.rst
1697                          */
1698                         dec_mm_counter(mm, mm_counter_file(page));
1699                 }
1700 discard:
1701                 /*
1702                  * No need to call mmu_notifier_invalidate_range() it has be
1703                  * done above for all cases requiring it to happen under page
1704                  * table lock before mmu_notifier_invalidate_range_end()
1705                  *
1706                  * See Documentation/vm/mmu_notifier.rst
1707                  */
1708                 page_remove_rmap(subpage, PageHuge(page));
1709                 put_page(page);
1710         }
1711
1712         mmu_notifier_invalidate_range_end(&range);
1713
1714         return ret;
1715 }
1716
1717 static bool invalid_migration_vma(struct vm_area_struct *vma, void *arg)
1718 {
1719         return vma_is_temporary_stack(vma);
1720 }
1721
1722 static int page_mapcount_is_zero(struct page *page)
1723 {
1724         return !total_mapcount(page);
1725 }
1726
1727 /**
1728  * try_to_unmap - try to remove all page table mappings to a page
1729  * @page: the page to get unmapped
1730  * @flags: action and flags
1731  *
1732  * Tries to remove all the page table entries which are mapping this
1733  * page, used in the pageout path.  Caller must hold the page lock.
1734  *
1735  * If unmap is successful, return true. Otherwise, false.
1736  */
1737 bool try_to_unmap(struct page *page, enum ttu_flags flags)
1738 {
1739         struct rmap_walk_control rwc = {
1740                 .rmap_one = try_to_unmap_one,
1741                 .arg = (void *)flags,
1742                 .done = page_mapcount_is_zero,
1743                 .anon_lock = page_lock_anon_vma_read,
1744         };
1745
1746         /*
1747          * During exec, a temporary VMA is setup and later moved.
1748          * The VMA is moved under the anon_vma lock but not the
1749          * page tables leading to a race where migration cannot
1750          * find the migration ptes. Rather than increasing the
1751          * locking requirements of exec(), migration skips
1752          * temporary VMAs until after exec() completes.
1753          */
1754         if ((flags & (TTU_MIGRATION|TTU_SPLIT_FREEZE))
1755             && !PageKsm(page) && PageAnon(page))
1756                 rwc.invalid_vma = invalid_migration_vma;
1757
1758         if (flags & TTU_RMAP_LOCKED)
1759                 rmap_walk_locked(page, &rwc);
1760         else
1761                 rmap_walk(page, &rwc);
1762
1763         return !page_mapcount(page) ? true : false;
1764 }
1765
1766 static int page_not_mapped(struct page *page)
1767 {
1768         return !page_mapped(page);
1769 };
1770
1771 /**
1772  * try_to_munlock - try to munlock a page
1773  * @page: the page to be munlocked
1774  *
1775  * Called from munlock code.  Checks all of the VMAs mapping the page
1776  * to make sure nobody else has this page mlocked. The page will be
1777  * returned with PG_mlocked cleared if no other vmas have it mlocked.
1778  */
1779
1780 void try_to_munlock(struct page *page)
1781 {
1782         struct rmap_walk_control rwc = {
1783                 .rmap_one = try_to_unmap_one,
1784                 .arg = (void *)TTU_MUNLOCK,
1785                 .done = page_not_mapped,
1786                 .anon_lock = page_lock_anon_vma_read,
1787
1788         };
1789
1790         VM_BUG_ON_PAGE(!PageLocked(page) || PageLRU(page), page);
1791         VM_BUG_ON_PAGE(PageCompound(page) && PageDoubleMap(page), page);
1792
1793         rmap_walk(page, &rwc);
1794 }
1795
1796 void __put_anon_vma(struct anon_vma *anon_vma)
1797 {
1798         struct anon_vma *root = anon_vma->root;
1799
1800         anon_vma_free(anon_vma);
1801         if (root != anon_vma && atomic_dec_and_test(&root->refcount))
1802                 anon_vma_free(root);
1803 }
1804
1805 static struct anon_vma *rmap_walk_anon_lock(struct page *page,
1806                                         struct rmap_walk_control *rwc)
1807 {
1808         struct anon_vma *anon_vma;
1809
1810         if (rwc->anon_lock)
1811                 return rwc->anon_lock(page);
1812
1813         /*
1814          * Note: remove_migration_ptes() cannot use page_lock_anon_vma_read()
1815          * because that depends on page_mapped(); but not all its usages
1816          * are holding mmap_sem. Users without mmap_sem are required to
1817          * take a reference count to prevent the anon_vma disappearing
1818          */
1819         anon_vma = page_anon_vma(page);
1820         if (!anon_vma)
1821                 return NULL;
1822
1823         anon_vma_lock_read(anon_vma);
1824         return anon_vma;
1825 }
1826
1827 /*
1828  * rmap_walk_anon - do something to anonymous page using the object-based
1829  * rmap method
1830  * @page: the page to be handled
1831  * @rwc: control variable according to each walk type
1832  *
1833  * Find all the mappings of a page using the mapping pointer and the vma chains
1834  * contained in the anon_vma struct it points to.
1835  *
1836  * When called from try_to_munlock(), the mmap_sem of the mm containing the vma
1837  * where the page was found will be held for write.  So, we won't recheck
1838  * vm_flags for that VMA.  That should be OK, because that vma shouldn't be
1839  * LOCKED.
1840  */
1841 static void rmap_walk_anon(struct page *page, struct rmap_walk_control *rwc,
1842                 bool locked)
1843 {
1844         struct anon_vma *anon_vma;
1845         pgoff_t pgoff_start, pgoff_end;
1846         struct anon_vma_chain *avc;
1847
1848         if (locked) {
1849                 anon_vma = page_anon_vma(page);
1850                 /* anon_vma disappear under us? */
1851                 VM_BUG_ON_PAGE(!anon_vma, page);
1852         } else {
1853                 anon_vma = rmap_walk_anon_lock(page, rwc);
1854         }
1855         if (!anon_vma)
1856                 return;
1857
1858         pgoff_start = page_to_pgoff(page);
1859         pgoff_end = pgoff_start + hpage_nr_pages(page) - 1;
1860         anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root,
1861                         pgoff_start, pgoff_end) {
1862                 struct vm_area_struct *vma = avc->vma;
1863                 unsigned long address = vma_address(page, vma);
1864
1865                 cond_resched();
1866
1867                 if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg))
1868                         continue;
1869
1870                 if (!rwc->rmap_one(page, vma, address, rwc->arg))
1871                         break;
1872                 if (rwc->done && rwc->done(page))
1873                         break;
1874         }
1875
1876         if (!locked)
1877                 anon_vma_unlock_read(anon_vma);
1878 }
1879
1880 /*
1881  * rmap_walk_file - do something to file page using the object-based rmap method
1882  * @page: the page to be handled
1883  * @rwc: control variable according to each walk type
1884  *
1885  * Find all the mappings of a page using the mapping pointer and the vma chains
1886  * contained in the address_space struct it points to.
1887  *
1888  * When called from try_to_munlock(), the mmap_sem of the mm containing the vma
1889  * where the page was found will be held for write.  So, we won't recheck
1890  * vm_flags for that VMA.  That should be OK, because that vma shouldn't be
1891  * LOCKED.
1892  */
1893 static void rmap_walk_file(struct page *page, struct rmap_walk_control *rwc,
1894                 bool locked)
1895 {
1896         struct address_space *mapping = page_mapping(page);
1897         pgoff_t pgoff_start, pgoff_end;
1898         struct vm_area_struct *vma;
1899
1900         /*
1901          * The page lock not only makes sure that page->mapping cannot
1902          * suddenly be NULLified by truncation, it makes sure that the
1903          * structure at mapping cannot be freed and reused yet,
1904          * so we can safely take mapping->i_mmap_rwsem.
1905          */
1906         VM_BUG_ON_PAGE(!PageLocked(page), page);
1907
1908         if (!mapping)
1909                 return;
1910
1911         pgoff_start = page_to_pgoff(page);
1912         pgoff_end = pgoff_start + hpage_nr_pages(page) - 1;
1913         if (!locked)
1914                 i_mmap_lock_read(mapping);
1915         vma_interval_tree_foreach(vma, &mapping->i_mmap,
1916                         pgoff_start, pgoff_end) {
1917                 unsigned long address = vma_address(page, vma);
1918
1919                 cond_resched();
1920
1921                 if (rwc->invalid_vma && rwc->invalid_vma(vma, rwc->arg))
1922                         continue;
1923
1924                 if (!rwc->rmap_one(page, vma, address, rwc->arg))
1925                         goto done;
1926                 if (rwc->done && rwc->done(page))
1927                         goto done;
1928         }
1929
1930 done:
1931         if (!locked)
1932                 i_mmap_unlock_read(mapping);
1933 }
1934
1935 void rmap_walk(struct page *page, struct rmap_walk_control *rwc)
1936 {
1937         if (unlikely(PageKsm(page)))
1938                 rmap_walk_ksm(page, rwc);
1939         else if (PageAnon(page))
1940                 rmap_walk_anon(page, rwc, false);
1941         else
1942                 rmap_walk_file(page, rwc, false);
1943 }
1944
1945 /* Like rmap_walk, but caller holds relevant rmap lock */
1946 void rmap_walk_locked(struct page *page, struct rmap_walk_control *rwc)
1947 {
1948         /* no ksm support for now */
1949         VM_BUG_ON_PAGE(PageKsm(page), page);
1950         if (PageAnon(page))
1951                 rmap_walk_anon(page, rwc, true);
1952         else
1953                 rmap_walk_file(page, rwc, true);
1954 }
1955
1956 #ifdef CONFIG_HUGETLB_PAGE
1957 /*
1958  * The following two functions are for anonymous (private mapped) hugepages.
1959  * Unlike common anonymous pages, anonymous hugepages have no accounting code
1960  * and no lru code, because we handle hugepages differently from common pages.
1961  */
1962 void hugepage_add_anon_rmap(struct page *page,
1963                             struct vm_area_struct *vma, unsigned long address)
1964 {
1965         struct anon_vma *anon_vma = vma->anon_vma;
1966         int first;
1967
1968         BUG_ON(!PageLocked(page));
1969         BUG_ON(!anon_vma);
1970         /* address might be in next vma when migration races vma_adjust */
1971         first = atomic_inc_and_test(compound_mapcount_ptr(page));
1972         if (first)
1973                 __page_set_anon_rmap(page, vma, address, 0);
1974 }
1975
1976 void hugepage_add_new_anon_rmap(struct page *page,
1977                         struct vm_area_struct *vma, unsigned long address)
1978 {
1979         BUG_ON(address < vma->vm_start || address >= vma->vm_end);
1980         atomic_set(compound_mapcount_ptr(page), 0);
1981         if (hpage_pincount_available(page))
1982                 atomic_set(compound_pincount_ptr(page), 0);
1983
1984         __page_set_anon_rmap(page, vma, address, 1);
1985 }
1986 #endif /* CONFIG_HUGETLB_PAGE */