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)
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)
39 *
40 * anon_vma->mutex,mapping->i_mutex (memory_failure, collect_procs_anon)
41 * ->tasklist_lock
42 * pte map lock
43 */
44
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>
60
61 #include <asm/tlbflush.h>
62
63 #include "internal.h"
64
65 static struct kmem_cache *anon_vma_cachep;
66 static struct kmem_cache *anon_vma_chain_cachep;
67
anon_vma_alloc(void)68 static inline struct anon_vma *anon_vma_alloc(void)
69 {
70 struct anon_vma *anon_vma;
71
72 anon_vma = kmem_cache_alloc(anon_vma_cachep, GFP_KERNEL);
73 if (anon_vma) {
74 atomic_set(&anon_vma->refcount, 1);
75 /*
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.
78 */
79 anon_vma->root = anon_vma;
80 }
81
82 return anon_vma;
83 }
84
anon_vma_free(struct anon_vma * anon_vma)85 static inline void anon_vma_free(struct anon_vma *anon_vma)
86 {
87 VM_BUG_ON(atomic_read(&anon_vma->refcount));
88
89 /*
90 * Synchronize against page_lock_anon_vma() such that
91 * we can safely hold the lock without the anon_vma getting
92 * freed.
93 *
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:
97 *
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()
102 *
103 * LOCK should suffice since the actual taking of the lock must
104 * happen _before_ what follows.
105 */
106 might_sleep();
107 if (mutex_is_locked(&anon_vma->root->mutex)) {
108 anon_vma_lock(anon_vma);
109 anon_vma_unlock(anon_vma);
110 }
111
112 kmem_cache_free(anon_vma_cachep, anon_vma);
113 }
114
anon_vma_chain_alloc(gfp_t gfp)115 static inline struct anon_vma_chain *anon_vma_chain_alloc(gfp_t gfp)
116 {
117 return kmem_cache_alloc(anon_vma_chain_cachep, gfp);
118 }
119
anon_vma_chain_free(struct anon_vma_chain * anon_vma_chain)120 static void anon_vma_chain_free(struct anon_vma_chain *anon_vma_chain)
121 {
122 kmem_cache_free(anon_vma_chain_cachep, anon_vma_chain);
123 }
124
anon_vma_chain_link(struct vm_area_struct * vma,struct anon_vma_chain * avc,struct anon_vma * anon_vma)125 static void anon_vma_chain_link(struct vm_area_struct *vma,
126 struct anon_vma_chain *avc,
127 struct anon_vma *anon_vma)
128 {
129 avc->vma = vma;
130 avc->anon_vma = anon_vma;
131 list_add(&avc->same_vma, &vma->anon_vma_chain);
132
133 /*
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().
136 */
137 list_add_tail(&avc->same_anon_vma, &anon_vma->head);
138 }
139
140 /**
141 * anon_vma_prepare - attach an anon_vma to a memory region
142 * @vma: the memory region in question
143 *
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.
147 *
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.
153 *
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).
159 *
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.
164 *
165 * This must be called with the mmap_sem held for reading.
166 */
anon_vma_prepare(struct vm_area_struct * vma)167 int anon_vma_prepare(struct vm_area_struct *vma)
168 {
169 struct anon_vma *anon_vma = vma->anon_vma;
170 struct anon_vma_chain *avc;
171
172 might_sleep();
173 if (unlikely(!anon_vma)) {
174 struct mm_struct *mm = vma->vm_mm;
175 struct anon_vma *allocated;
176
177 avc = anon_vma_chain_alloc(GFP_KERNEL);
178 if (!avc)
179 goto out_enomem;
180
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;
188 }
189
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;
198 }
199 spin_unlock(&mm->page_table_lock);
200 anon_vma_unlock(anon_vma);
201
202 if (unlikely(allocated))
203 put_anon_vma(allocated);
204 if (unlikely(avc))
205 anon_vma_chain_free(avc);
206 }
207 return 0;
208
209 out_enomem_free_avc:
210 anon_vma_chain_free(avc);
211 out_enomem:
212 return -ENOMEM;
213 }
214
215 /*
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.
219 *
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.
222 */
lock_anon_vma_root(struct anon_vma * root,struct anon_vma * anon_vma)223 static inline struct anon_vma *lock_anon_vma_root(struct anon_vma *root, struct anon_vma *anon_vma)
224 {
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);
231 }
232 return root;
233 }
234
unlock_anon_vma_root(struct anon_vma * root)235 static inline void unlock_anon_vma_root(struct anon_vma *root)
236 {
237 if (root)
238 mutex_unlock(&root->mutex);
239 }
240
241 /*
242 * Attach the anon_vmas from src to dst.
243 * Returns 0 on success, -ENOMEM on failure.
244 */
anon_vma_clone(struct vm_area_struct * dst,struct vm_area_struct * src)245 int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src)
246 {
247 struct anon_vma_chain *avc, *pavc;
248 struct anon_vma *root = NULL;
249
250 list_for_each_entry_reverse(pavc, &src->anon_vma_chain, same_vma) {
251 struct anon_vma *anon_vma;
252
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;
260 }
261 anon_vma = pavc->anon_vma;
262 root = lock_anon_vma_root(root, anon_vma);
263 anon_vma_chain_link(dst, avc, anon_vma);
264 }
265 unlock_anon_vma_root(root);
266 return 0;
267
268 enomem_failure:
269 unlink_anon_vmas(dst);
270 return -ENOMEM;
271 }
272
273 /*
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.
284 *
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.
302 */
anon_vma_moveto_tail(struct vm_area_struct * dst)303 void anon_vma_moveto_tail(struct vm_area_struct *dst)
304 {
305 struct anon_vma_chain *pavc;
306 struct anon_vma *root = NULL;
307
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);
314 }
315 unlock_anon_vma_root(root);
316 }
317
318 /*
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.
322 */
anon_vma_fork(struct vm_area_struct * vma,struct vm_area_struct * pvma)323 int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma)
324 {
325 struct anon_vma_chain *avc;
326 struct anon_vma *anon_vma;
327
328 /* Don't bother if the parent process has no anon_vma here. */
329 if (!pvma->anon_vma)
330 return 0;
331
332 /*
333 * First, attach the new VMA to the parent VMA's anon_vmas,
334 * so rmap can find non-COWed pages in child processes.
335 */
336 if (anon_vma_clone(vma, pvma))
337 return -ENOMEM;
338
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;
346
347 /*
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.
350 */
351 anon_vma->root = pvma->anon_vma->root;
352 /*
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.
356 */
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);
363
364 return 0;
365
366 out_error_free_anon_vma:
367 put_anon_vma(anon_vma);
368 out_error:
369 unlink_anon_vmas(vma);
370 return -ENOMEM;
371 }
372
unlink_anon_vmas(struct vm_area_struct * vma)373 void unlink_anon_vmas(struct vm_area_struct *vma)
374 {
375 struct anon_vma_chain *avc, *next;
376 struct anon_vma *root = NULL;
377
378 /*
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.
381 */
382 list_for_each_entry_safe(avc, next, &vma->anon_vma_chain, same_vma) {
383 struct anon_vma *anon_vma = avc->anon_vma;
384
385 root = lock_anon_vma_root(root, anon_vma);
386 list_del(&avc->same_anon_vma);
387
388 /*
389 * Leave empty anon_vmas on the list - we'll need
390 * to free them outside the lock.
391 */
392 if (list_empty(&anon_vma->head))
393 continue;
394
395 list_del(&avc->same_vma);
396 anon_vma_chain_free(avc);
397 }
398 unlock_anon_vma_root(root);
399
400 /*
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.
404 */
405 list_for_each_entry_safe(avc, next, &vma->anon_vma_chain, same_vma) {
406 struct anon_vma *anon_vma = avc->anon_vma;
407
408 put_anon_vma(anon_vma);
409
410 list_del(&avc->same_vma);
411 anon_vma_chain_free(avc);
412 }
413 }
414
anon_vma_ctor(void * data)415 static void anon_vma_ctor(void *data)
416 {
417 struct anon_vma *anon_vma = data;
418
419 mutex_init(&anon_vma->mutex);
420 atomic_set(&anon_vma->refcount, 0);
421 INIT_LIST_HEAD(&anon_vma->head);
422 }
423
anon_vma_init(void)424 void __init anon_vma_init(void)
425 {
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);
429 }
430
431 /*
432 * Getting a lock on a stable anon_vma from a page off the LRU is tricky!
433 *
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.
437 *
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).
440 *
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 ].
445 *
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() ].
449 *
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.
453 */
page_get_anon_vma(struct page * page)454 struct anon_vma *page_get_anon_vma(struct page *page)
455 {
456 struct anon_vma *anon_vma = NULL;
457 unsigned long anon_mapping;
458
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;
465
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;
470 }
471
472 /*
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).
478 */
479 if (!page_mapped(page)) {
480 rcu_read_unlock();
481 put_anon_vma(anon_vma);
482 return NULL;
483 }
484 out:
485 rcu_read_unlock();
486
487 return anon_vma;
488 }
489
490 /*
491 * Similar to page_get_anon_vma() except it locks the anon_vma.
492 *
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.
496 */
page_lock_anon_vma(struct page * page)497 struct anon_vma *page_lock_anon_vma(struct page *page)
498 {
499 struct anon_vma *anon_vma = NULL;
500 struct anon_vma *root_anon_vma;
501 unsigned long anon_mapping;
502
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;
509
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)) {
513 /*
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().
517 */
518 if (!page_mapped(page)) {
519 mutex_unlock(&root_anon_vma->mutex);
520 anon_vma = NULL;
521 }
522 goto out;
523 }
524
525 /* trylock failed, we got to sleep */
526 if (!atomic_inc_not_zero(&anon_vma->refcount)) {
527 anon_vma = NULL;
528 goto out;
529 }
530
531 if (!page_mapped(page)) {
532 rcu_read_unlock();
533 put_anon_vma(anon_vma);
534 return NULL;
535 }
536
537 /* we pinned the anon_vma, its safe to sleep */
538 rcu_read_unlock();
539 anon_vma_lock(anon_vma);
540
541 if (atomic_dec_and_test(&anon_vma->refcount)) {
542 /*
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.
546 */
547 anon_vma_unlock(anon_vma);
548 __put_anon_vma(anon_vma);
549 anon_vma = NULL;
550 }
551
552 return anon_vma;
553
554 out:
555 rcu_read_unlock();
556 return anon_vma;
557 }
558
page_unlock_anon_vma(struct anon_vma * anon_vma)559 void page_unlock_anon_vma(struct anon_vma *anon_vma)
560 {
561 anon_vma_unlock(anon_vma);
562 }
563
564 /*
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.
568 */
569 inline unsigned long
vma_address(struct page * page,struct vm_area_struct * vma)570 vma_address(struct page *page, struct vm_area_struct *vma)
571 {
572 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
573 unsigned long address;
574
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;
581 }
582 return address;
583 }
584
585 /*
586 * At what user virtual address is page expected in vma?
587 * Caller should check the page is actually part of the vma.
588 */
page_address_in_vma(struct page * page,struct vm_area_struct * vma)589 unsigned long page_address_in_vma(struct page *page, struct vm_area_struct *vma)
590 {
591 if (PageAnon(page)) {
592 struct anon_vma *page__anon_vma = page_anon_vma(page);
593 /*
594 * Note: swapoff's unuse_vma() is more efficient with this
595 * check, and needs it to match anon_vma when KSM is active.
596 */
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);
607 }
608
609 /*
610 * Check that @page is mapped at @address into @mm.
611 *
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).
615 *
616 * On success returns with pte mapped and locked.
617 */
__page_check_address(struct page * page,struct mm_struct * mm,unsigned long address,spinlock_t ** ptlp,int sync)618 pte_t *__page_check_address(struct page *page, struct mm_struct *mm,
619 unsigned long address, spinlock_t **ptlp, int sync)
620 {
621 pgd_t *pgd;
622 pud_t *pud;
623 pmd_t *pmd;
624 pte_t *pte;
625 spinlock_t *ptl;
626
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;
632
633 ptl = &mm->page_table_lock;
634 goto check;
635 }
636
637 pgd = pgd_offset(mm, address);
638 if (!pgd_present(*pgd))
639 return NULL;
640
641 pud = pud_offset(pgd, address);
642 if (!pud_present(*pud))
643 return NULL;
644
645 pmd = pmd_offset(pud, address);
646 if (!pmd_present(*pmd))
647 return NULL;
648 if (pmd_trans_huge(*pmd))
649 return NULL;
650
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;
656 }
657
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;
664 }
665 pte_unmap_unlock(pte, ptl);
666 return NULL;
667 }
668
669 /**
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
673 *
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.
677 */
page_mapped_in_vma(struct page * page,struct vm_area_struct * vma)678 int page_mapped_in_vma(struct page *page, struct vm_area_struct *vma)
679 {
680 unsigned long address;
681 pte_t *pte;
682 spinlock_t *ptl;
683
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);
691
692 return 1;
693 }
694
695 /*
696 * Subfunctions of page_referenced: page_referenced_one called
697 * repeatedly from either page_referenced_anon or page_referenced_file.
698 */
page_referenced_one(struct page * page,struct vm_area_struct * vma,unsigned long address,unsigned int * mapcount,unsigned long * vm_flags)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)
702 {
703 struct mm_struct *mm = vma->vm_mm;
704 int referenced = 0;
705
706 if (unlikely(PageTransHuge(page))) {
707 pmd_t *pmd;
708
709 spin_lock(&mm->page_table_lock);
710 /*
711 * rmap might return false positives; we must filter
712 * these out using page_check_address_pmd().
713 */
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;
719 }
720
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;
726 }
727
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;
735
736 /*
737 * rmap might return false positives; we must filter
738 * these out using page_check_address().
739 */
740 pte = page_check_address(page, mm, address, &ptl, 0);
741 if (!pte)
742 goto out;
743
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;
749 }
750
751 if (ptep_clear_flush_young_notify(vma, address, pte)) {
752 /*
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.
758 */
759 if (likely(!VM_SequentialReadHint(vma)))
760 referenced++;
761 }
762 pte_unmap_unlock(pte, ptl);
763 }
764
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++;
770
771 (*mapcount)--;
772
773 if (referenced)
774 *vm_flags |= vma->vm_flags;
775 out:
776 return referenced;
777 }
778
page_referenced_anon(struct page * page,struct mem_cgroup * memcg,unsigned long * vm_flags)779 static int page_referenced_anon(struct page *page,
780 struct mem_cgroup *memcg,
781 unsigned long *vm_flags)
782 {
783 unsigned int mapcount;
784 struct anon_vma *anon_vma;
785 struct anon_vma_chain *avc;
786 int referenced = 0;
787
788 anon_vma = page_lock_anon_vma(page);
789 if (!anon_vma)
790 return referenced;
791
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;
798 /*
799 * If we are reclaiming on behalf of a cgroup, skip
800 * counting on behalf of references from different
801 * cgroups
802 */
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;
809 }
810
811 page_unlock_anon_vma(anon_vma);
812 return referenced;
813 }
814
815 /**
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
820 *
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.
825 *
826 * This function is only called from page_referenced for object-based pages.
827 */
page_referenced_file(struct page * page,struct mem_cgroup * memcg,unsigned long * vm_flags)828 static int page_referenced_file(struct page *page,
829 struct mem_cgroup *memcg,
830 unsigned long *vm_flags)
831 {
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;
838
839 /*
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.
843 */
844 BUG_ON(PageAnon(page));
845
846 /*
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.
851 */
852 BUG_ON(!PageLocked(page));
853
854 mutex_lock(&mapping->i_mmap_mutex);
855
856 /*
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.
859 */
860 mapcount = page_mapcount(page);
861
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;
866 /*
867 * If we are reclaiming on behalf of a cgroup, skip
868 * counting on behalf of references from different
869 * cgroups
870 */
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;
877 }
878
879 mutex_unlock(&mapping->i_mmap_mutex);
880 return referenced;
881 }
882
883 /**
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
889 *
890 * Quick test_and_clear_referenced for all mappings to a page,
891 * returns the number of ptes which referenced the page.
892 */
page_referenced(struct page * page,int is_locked,struct mem_cgroup * memcg,unsigned long * vm_flags)893 int page_referenced(struct page *page,
894 int is_locked,
895 struct mem_cgroup *memcg,
896 unsigned long *vm_flags)
897 {
898 int referenced = 0;
899 int we_locked = 0;
900
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;
908 }
909 }
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);
921
922 if (page_test_and_clear_young(page_to_pfn(page)))
923 referenced++;
924 }
925 out:
926 return referenced;
927 }
928
page_mkclean_one(struct page * page,struct vm_area_struct * vma,unsigned long address)929 static int page_mkclean_one(struct page *page, struct vm_area_struct *vma,
930 unsigned long address)
931 {
932 struct mm_struct *mm = vma->vm_mm;
933 pte_t *pte;
934 spinlock_t *ptl;
935 int ret = 0;
936
937 pte = page_check_address(page, mm, address, &ptl, 1);
938 if (!pte)
939 goto out;
940
941 if (pte_dirty(*pte) || pte_write(*pte)) {
942 pte_t entry;
943
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;
950 }
951
952 pte_unmap_unlock(pte, ptl);
953 out:
954 return ret;
955 }
956
page_mkclean_file(struct address_space * mapping,struct page * page)957 static int page_mkclean_file(struct address_space *mapping, struct page *page)
958 {
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;
963
964 BUG_ON(PageAnon(page));
965
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);
973 }
974 }
975 mutex_unlock(&mapping->i_mmap_mutex);
976 return ret;
977 }
978
page_mkclean(struct page * page)979 int page_mkclean(struct page *page)
980 {
981 int ret = 0;
982
983 BUG_ON(!PageLocked(page));
984
985 if (page_mapped(page)) {
986 struct address_space *mapping = page_mapping(page);
987 if (mapping)
988 ret = page_mkclean_file(mapping, page);
989 }
990
991 return ret;
992 }
993 EXPORT_SYMBOL_GPL(page_mkclean);
994
995 /**
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
1000 *
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.
1005 */
page_move_anon_rmap(struct page * page,struct vm_area_struct * vma,unsigned long address)1006 void page_move_anon_rmap(struct page *page,
1007 struct vm_area_struct *vma, unsigned long address)
1008 {
1009 struct anon_vma *anon_vma = vma->anon_vma;
1010
1011 VM_BUG_ON(!PageLocked(page));
1012 VM_BUG_ON(!anon_vma);
1013 VM_BUG_ON(page->index != linear_page_index(vma, address));
1014
1015 anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
1016 page->mapping = (struct address_space *) anon_vma;
1017 }
1018
1019 /**
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
1025 */
__page_set_anon_rmap(struct page * page,struct vm_area_struct * vma,unsigned long address,int exclusive)1026 static void __page_set_anon_rmap(struct page *page,
1027 struct vm_area_struct *vma, unsigned long address, int exclusive)
1028 {
1029 struct anon_vma *anon_vma = vma->anon_vma;
1030
1031 BUG_ON(!anon_vma);
1032
1033 if (PageAnon(page))
1034 return;
1035
1036 /*
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!
1040 */
1041 if (!exclusive)
1042 anon_vma = anon_vma->root;
1043
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);
1047 }
1048
1049 /**
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
1054 */
__page_check_anon_rmap(struct page * page,struct vm_area_struct * vma,unsigned long address)1055 static void __page_check_anon_rmap(struct page *page,
1056 struct vm_area_struct *vma, unsigned long address)
1057 {
1058 #ifdef CONFIG_DEBUG_VM
1059 /*
1060 * The page's anon-rmap details (mapping and index) are guaranteed to
1061 * be set up correctly at this point.
1062 *
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.
1066 *
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.
1070 */
1071 BUG_ON(page_anon_vma(page)->root != vma->anon_vma->root);
1072 BUG_ON(page->index != linear_page_index(vma, address));
1073 #endif
1074 }
1075
1076 /**
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
1081 *
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).
1086 */
page_add_anon_rmap(struct page * page,struct vm_area_struct * vma,unsigned long address)1087 void page_add_anon_rmap(struct page *page,
1088 struct vm_area_struct *vma, unsigned long address)
1089 {
1090 do_page_add_anon_rmap(page, vma, address, 0);
1091 }
1092
1093 /*
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.
1097 */
do_page_add_anon_rmap(struct page * page,struct vm_area_struct * vma,unsigned long address,int exclusive)1098 void do_page_add_anon_rmap(struct page *page,
1099 struct vm_area_struct *vma, unsigned long address, int exclusive)
1100 {
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);
1108 }
1109 if (unlikely(PageKsm(page)))
1110 return;
1111
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);
1118 }
1119
1120 /**
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
1125 *
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.
1129 */
page_add_new_anon_rmap(struct page * page,struct vm_area_struct * vma,unsigned long address)1130 void page_add_new_anon_rmap(struct page *page,
1131 struct vm_area_struct *vma, unsigned long address)
1132 {
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);
1145 }
1146
1147 /**
1148 * page_add_file_rmap - add pte mapping to a file page
1149 * @page: the page to add the mapping to
1150 *
1151 * The caller needs to hold the pte lock.
1152 */
page_add_file_rmap(struct page * page)1153 void page_add_file_rmap(struct page *page)
1154 {
1155 bool locked;
1156 unsigned long flags;
1157
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);
1162 }
1163 mem_cgroup_end_update_page_stat(page, &locked, &flags);
1164 }
1165
1166 /**
1167 * page_remove_rmap - take down pte mapping from a page
1168 * @page: page to remove mapping from
1169 *
1170 * The caller needs to hold the pte lock.
1171 */
page_remove_rmap(struct page * page)1172 void page_remove_rmap(struct page *page)
1173 {
1174 struct address_space *mapping = page_mapping(page);
1175 bool anon = PageAnon(page);
1176 bool locked;
1177 unsigned long flags;
1178
1179 /*
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.
1183 */
1184 if (!anon)
1185 mem_cgroup_begin_update_page_stat(page, &locked, &flags);
1186
1187 /* page still mapped by someone else? */
1188 if (!atomic_add_negative(-1, &page->_mapcount))
1189 goto out;
1190
1191 /*
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.
1197 *
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.
1202 *
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).
1208 */
1209 if (mapping && !mapping_cap_account_dirty(mapping) &&
1210 page_test_and_clear_dirty(page_to_pfn(page), 1))
1211 set_page_dirty(page);
1212 /*
1213 * Hugepages are not counted in NR_ANON_PAGES nor NR_FILE_MAPPED
1214 * and not charged by memcg for now.
1215 */
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);
1228 }
1229 /*
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.
1237 */
1238 out:
1239 if (!anon)
1240 mem_cgroup_end_update_page_stat(page, &locked, &flags);
1241 }
1242
1243 /*
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.
1246 */
try_to_unmap_one(struct page * page,struct vm_area_struct * vma,unsigned long address,enum ttu_flags flags)1247 int try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
1248 unsigned long address, enum ttu_flags flags)
1249 {
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;
1255
1256 pte = page_check_address(page, mm, address, &ptl, 0);
1257 if (!pte)
1258 goto out;
1259
1260 /*
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.
1264 */
1265 if (!(flags & TTU_IGNORE_MLOCK)) {
1266 if (vma->vm_flags & VM_LOCKED)
1267 goto out_mlock;
1268
1269 if (TTU_ACTION(flags) == TTU_MUNLOCK)
1270 goto out_unmap;
1271 }
1272 if (!(flags & TTU_IGNORE_ACCESS)) {
1273 if (ptep_clear_flush_young_notify(vma, address, pte)) {
1274 ret = SWAP_FAIL;
1275 goto out_unmap;
1276 }
1277 }
1278
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);
1282
1283 /* Move the dirty bit to the physical page now the pte is gone. */
1284 if (pte_dirty(pteval))
1285 set_page_dirty(page);
1286
1287 /* Update high watermark before we lower rss */
1288 update_hiwater_rss(mm);
1289
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) };
1299
1300 if (PageSwapCache(page)) {
1301 /*
1302 * Store the swap location in the pte.
1303 * See handle_pte_fault() ...
1304 */
1305 if (swap_duplicate(entry) < 0) {
1306 set_pte_at(mm, address, pte, pteval);
1307 ret = SWAP_FAIL;
1308 goto out_unmap;
1309 }
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);
1315 }
1316 dec_mm_counter(mm, MM_ANONPAGES);
1317 inc_mm_counter(mm, MM_SWAPENTS);
1318 } else if (IS_ENABLED(CONFIG_MIGRATION)) {
1319 /*
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.
1323 */
1324 BUG_ON(TTU_ACTION(flags) != TTU_MIGRATION);
1325 entry = make_migration_entry(page, pte_write(pteval));
1326 }
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);
1337
1338 page_remove_rmap(page);
1339 page_cache_release(page);
1340
1341 out_unmap:
1342 pte_unmap_unlock(pte, ptl);
1343 out:
1344 return ret;
1345
1346 out_mlock:
1347 pte_unmap_unlock(pte, ptl);
1348
1349
1350 /*
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.
1357 */
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;
1362 }
1363 up_read(&vma->vm_mm->mmap_sem);
1364 }
1365 return ret;
1366 }
1367
1368 /*
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.
1373 *
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.
1378 *
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.
1382 *
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.
1386 *
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.
1391 */
1392 #define CLUSTER_SIZE min(32*PAGE_SIZE, PMD_SIZE)
1393 #define CLUSTER_MASK (~(CLUSTER_SIZE - 1))
1394
try_to_unmap_cluster(unsigned long cursor,unsigned int * mapcount,struct vm_area_struct * vma,struct page * check_page)1395 static int try_to_unmap_cluster(unsigned long cursor, unsigned int *mapcount,
1396 struct vm_area_struct *vma, struct page *check_page)
1397 {
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;
1410
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;
1417
1418 pgd = pgd_offset(mm, address);
1419 if (!pgd_present(*pgd))
1420 return ret;
1421
1422 pud = pud_offset(pgd, address);
1423 if (!pud_present(*pud))
1424 return ret;
1425
1426 pmd = pmd_offset(pud, address);
1427 if (!pmd_present(*pmd))
1428 return ret;
1429
1430 /*
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.
1433 */
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 */
1438 }
1439
1440 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
1441
1442 /* Update high watermark before we lower rss */
1443 update_hiwater_rss(mm);
1444
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));
1450
1451 if (locked_vma) {
1452 mlock_vma_page(page); /* no-op if already mlocked */
1453 if (page == check_page)
1454 ret = SWAP_MLOCK;
1455 continue; /* don't unmap */
1456 }
1457
1458 if (ptep_clear_flush_young_notify(vma, address, pte))
1459 continue;
1460
1461 /* Nuke the page table entry. */
1462 flush_cache_page(vma, address, pte_pfn(*pte));
1463 pteval = ptep_clear_flush_notify(vma, address, pte);
1464
1465 /* If nonlinear, store the file page offset in the pte. */
1466 if (page->index != linear_page_index(vma, address))
1467 set_pte_at(mm, address, pte, pgoff_to_pte(page->index));
1468
1469 /* Move the dirty bit to the physical page now the pte is gone. */
1470 if (pte_dirty(pteval))
1471 set_page_dirty(page);
1472
1473 page_remove_rmap(page);
1474 page_cache_release(page);
1475 dec_mm_counter(mm, MM_FILEPAGES);
1476 (*mapcount)--;
1477 }
1478 pte_unmap_unlock(pte - 1, ptl);
1479 if (locked_vma)
1480 up_read(&vma->vm_mm->mmap_sem);
1481 return ret;
1482 }
1483
is_vma_temporary_stack(struct vm_area_struct * vma)1484 bool is_vma_temporary_stack(struct vm_area_struct *vma)
1485 {
1486 int maybe_stack = vma->vm_flags & (VM_GROWSDOWN | VM_GROWSUP);
1487
1488 if (!maybe_stack)
1489 return false;
1490
1491 if ((vma->vm_flags & VM_STACK_INCOMPLETE_SETUP) ==
1492 VM_STACK_INCOMPLETE_SETUP)
1493 return true;
1494
1495 return false;
1496 }
1497
1498 /**
1499 * try_to_unmap_anon - unmap or unlock anonymous page using the object-based
1500 * rmap method
1501 * @page: the page to unmap/unlock
1502 * @flags: action and flags
1503 *
1504 * Find all the mappings of a page using the mapping pointer and the vma chains
1505 * contained in the anon_vma struct it points to.
1506 *
1507 * This function is only called from try_to_unmap/try_to_munlock for
1508 * anonymous pages.
1509 * When called from try_to_munlock(), the mmap_sem of the mm containing the vma
1510 * where the page was found will be held for write. So, we won't recheck
1511 * vm_flags for that VMA. That should be OK, because that vma shouldn't be
1512 * 'LOCKED.
1513 */
try_to_unmap_anon(struct page * page,enum ttu_flags flags)1514 static int try_to_unmap_anon(struct page *page, enum ttu_flags flags)
1515 {
1516 struct anon_vma *anon_vma;
1517 struct anon_vma_chain *avc;
1518 int ret = SWAP_AGAIN;
1519
1520 anon_vma = page_lock_anon_vma(page);
1521 if (!anon_vma)
1522 return ret;
1523
1524 list_for_each_entry(avc, &anon_vma->head, same_anon_vma) {
1525 struct vm_area_struct *vma = avc->vma;
1526 unsigned long address;
1527
1528 /*
1529 * During exec, a temporary VMA is setup and later moved.
1530 * The VMA is moved under the anon_vma lock but not the
1531 * page tables leading to a race where migration cannot
1532 * find the migration ptes. Rather than increasing the
1533 * locking requirements of exec(), migration skips
1534 * temporary VMAs until after exec() completes.
1535 */
1536 if (IS_ENABLED(CONFIG_MIGRATION) && (flags & TTU_MIGRATION) &&
1537 is_vma_temporary_stack(vma))
1538 continue;
1539
1540 address = vma_address(page, vma);
1541 if (address == -EFAULT)
1542 continue;
1543 ret = try_to_unmap_one(page, vma, address, flags);
1544 if (ret != SWAP_AGAIN || !page_mapped(page))
1545 break;
1546 }
1547
1548 page_unlock_anon_vma(anon_vma);
1549 return ret;
1550 }
1551
1552 /**
1553 * try_to_unmap_file - unmap/unlock file page using the object-based rmap method
1554 * @page: the page to unmap/unlock
1555 * @flags: action and flags
1556 *
1557 * Find all the mappings of a page using the mapping pointer and the vma chains
1558 * contained in the address_space struct it points to.
1559 *
1560 * This function is only called from try_to_unmap/try_to_munlock for
1561 * object-based pages.
1562 * When called from try_to_munlock(), the mmap_sem of the mm containing the vma
1563 * where the page was found will be held for write. So, we won't recheck
1564 * vm_flags for that VMA. That should be OK, because that vma shouldn't be
1565 * 'LOCKED.
1566 */
try_to_unmap_file(struct page * page,enum ttu_flags flags)1567 static int try_to_unmap_file(struct page *page, enum ttu_flags flags)
1568 {
1569 struct address_space *mapping = page->mapping;
1570 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
1571 struct vm_area_struct *vma;
1572 struct prio_tree_iter iter;
1573 int ret = SWAP_AGAIN;
1574 unsigned long cursor;
1575 unsigned long max_nl_cursor = 0;
1576 unsigned long max_nl_size = 0;
1577 unsigned int mapcount;
1578
1579 mutex_lock(&mapping->i_mmap_mutex);
1580 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
1581 unsigned long address = vma_address(page, vma);
1582 if (address == -EFAULT)
1583 continue;
1584 ret = try_to_unmap_one(page, vma, address, flags);
1585 if (ret != SWAP_AGAIN || !page_mapped(page))
1586 goto out;
1587 }
1588
1589 if (list_empty(&mapping->i_mmap_nonlinear))
1590 goto out;
1591
1592 /*
1593 * We don't bother to try to find the munlocked page in nonlinears.
1594 * It's costly. Instead, later, page reclaim logic may call
1595 * try_to_unmap(TTU_MUNLOCK) and recover PG_mlocked lazily.
1596 */
1597 if (TTU_ACTION(flags) == TTU_MUNLOCK)
1598 goto out;
1599
1600 list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
1601 shared.vm_set.list) {
1602 cursor = (unsigned long) vma->vm_private_data;
1603 if (cursor > max_nl_cursor)
1604 max_nl_cursor = cursor;
1605 cursor = vma->vm_end - vma->vm_start;
1606 if (cursor > max_nl_size)
1607 max_nl_size = cursor;
1608 }
1609
1610 if (max_nl_size == 0) { /* all nonlinears locked or reserved ? */
1611 ret = SWAP_FAIL;
1612 goto out;
1613 }
1614
1615 /*
1616 * We don't try to search for this page in the nonlinear vmas,
1617 * and page_referenced wouldn't have found it anyway. Instead
1618 * just walk the nonlinear vmas trying to age and unmap some.
1619 * The mapcount of the page we came in with is irrelevant,
1620 * but even so use it as a guide to how hard we should try?
1621 */
1622 mapcount = page_mapcount(page);
1623 if (!mapcount)
1624 goto out;
1625 cond_resched();
1626
1627 max_nl_size = (max_nl_size + CLUSTER_SIZE - 1) & CLUSTER_MASK;
1628 if (max_nl_cursor == 0)
1629 max_nl_cursor = CLUSTER_SIZE;
1630
1631 do {
1632 list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
1633 shared.vm_set.list) {
1634 cursor = (unsigned long) vma->vm_private_data;
1635 while ( cursor < max_nl_cursor &&
1636 cursor < vma->vm_end - vma->vm_start) {
1637 if (try_to_unmap_cluster(cursor, &mapcount,
1638 vma, page) == SWAP_MLOCK)
1639 ret = SWAP_MLOCK;
1640 cursor += CLUSTER_SIZE;
1641 vma->vm_private_data = (void *) cursor;
1642 if ((int)mapcount <= 0)
1643 goto out;
1644 }
1645 vma->vm_private_data = (void *) max_nl_cursor;
1646 }
1647 cond_resched();
1648 max_nl_cursor += CLUSTER_SIZE;
1649 } while (max_nl_cursor <= max_nl_size);
1650
1651 /*
1652 * Don't loop forever (perhaps all the remaining pages are
1653 * in locked vmas). Reset cursor on all unreserved nonlinear
1654 * vmas, now forgetting on which ones it had fallen behind.
1655 */
1656 list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
1657 vma->vm_private_data = NULL;
1658 out:
1659 mutex_unlock(&mapping->i_mmap_mutex);
1660 return ret;
1661 }
1662
1663 /**
1664 * try_to_unmap - try to remove all page table mappings to a page
1665 * @page: the page to get unmapped
1666 * @flags: action and flags
1667 *
1668 * Tries to remove all the page table entries which are mapping this
1669 * page, used in the pageout path. Caller must hold the page lock.
1670 * Return values are:
1671 *
1672 * SWAP_SUCCESS - we succeeded in removing all mappings
1673 * SWAP_AGAIN - we missed a mapping, try again later
1674 * SWAP_FAIL - the page is unswappable
1675 * SWAP_MLOCK - page is mlocked.
1676 */
try_to_unmap(struct page * page,enum ttu_flags flags)1677 int try_to_unmap(struct page *page, enum ttu_flags flags)
1678 {
1679 int ret;
1680
1681 BUG_ON(!PageLocked(page));
1682 VM_BUG_ON(!PageHuge(page) && PageTransHuge(page));
1683
1684 if (unlikely(PageKsm(page)))
1685 ret = try_to_unmap_ksm(page, flags);
1686 else if (PageAnon(page))
1687 ret = try_to_unmap_anon(page, flags);
1688 else
1689 ret = try_to_unmap_file(page, flags);
1690 if (ret != SWAP_MLOCK && !page_mapped(page))
1691 ret = SWAP_SUCCESS;
1692 return ret;
1693 }
1694
1695 /**
1696 * try_to_munlock - try to munlock a page
1697 * @page: the page to be munlocked
1698 *
1699 * Called from munlock code. Checks all of the VMAs mapping the page
1700 * to make sure nobody else has this page mlocked. The page will be
1701 * returned with PG_mlocked cleared if no other vmas have it mlocked.
1702 *
1703 * Return values are:
1704 *
1705 * SWAP_AGAIN - no vma is holding page mlocked, or,
1706 * SWAP_AGAIN - page mapped in mlocked vma -- couldn't acquire mmap sem
1707 * SWAP_FAIL - page cannot be located at present
1708 * SWAP_MLOCK - page is now mlocked.
1709 */
try_to_munlock(struct page * page)1710 int try_to_munlock(struct page *page)
1711 {
1712 VM_BUG_ON(!PageLocked(page) || PageLRU(page));
1713
1714 if (unlikely(PageKsm(page)))
1715 return try_to_unmap_ksm(page, TTU_MUNLOCK);
1716 else if (PageAnon(page))
1717 return try_to_unmap_anon(page, TTU_MUNLOCK);
1718 else
1719 return try_to_unmap_file(page, TTU_MUNLOCK);
1720 }
1721
__put_anon_vma(struct anon_vma * anon_vma)1722 void __put_anon_vma(struct anon_vma *anon_vma)
1723 {
1724 struct anon_vma *root = anon_vma->root;
1725
1726 anon_vma_free(anon_vma);
1727 if (root != anon_vma && atomic_dec_and_test(&root->refcount))
1728 anon_vma_free(root);
1729 }
1730
1731 #ifdef CONFIG_MIGRATION
1732 /*
1733 * rmap_walk() and its helpers rmap_walk_anon() and rmap_walk_file():
1734 * Called by migrate.c to remove migration ptes, but might be used more later.
1735 */
rmap_walk_anon(struct page * page,int (* rmap_one)(struct page *,struct vm_area_struct *,unsigned long,void *),void * arg)1736 static int rmap_walk_anon(struct page *page, int (*rmap_one)(struct page *,
1737 struct vm_area_struct *, unsigned long, void *), void *arg)
1738 {
1739 struct anon_vma *anon_vma;
1740 struct anon_vma_chain *avc;
1741 int ret = SWAP_AGAIN;
1742
1743 /*
1744 * Note: remove_migration_ptes() cannot use page_lock_anon_vma()
1745 * because that depends on page_mapped(); but not all its usages
1746 * are holding mmap_sem. Users without mmap_sem are required to
1747 * take a reference count to prevent the anon_vma disappearing
1748 */
1749 anon_vma = page_anon_vma(page);
1750 if (!anon_vma)
1751 return ret;
1752 anon_vma_lock(anon_vma);
1753 list_for_each_entry(avc, &anon_vma->head, same_anon_vma) {
1754 struct vm_area_struct *vma = avc->vma;
1755 unsigned long address = vma_address(page, vma);
1756 if (address == -EFAULT)
1757 continue;
1758 ret = rmap_one(page, vma, address, arg);
1759 if (ret != SWAP_AGAIN)
1760 break;
1761 }
1762 anon_vma_unlock(anon_vma);
1763 return ret;
1764 }
1765
rmap_walk_file(struct page * page,int (* rmap_one)(struct page *,struct vm_area_struct *,unsigned long,void *),void * arg)1766 static int rmap_walk_file(struct page *page, int (*rmap_one)(struct page *,
1767 struct vm_area_struct *, unsigned long, void *), void *arg)
1768 {
1769 struct address_space *mapping = page->mapping;
1770 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
1771 struct vm_area_struct *vma;
1772 struct prio_tree_iter iter;
1773 int ret = SWAP_AGAIN;
1774
1775 if (!mapping)
1776 return ret;
1777 mutex_lock(&mapping->i_mmap_mutex);
1778 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
1779 unsigned long address = vma_address(page, vma);
1780 if (address == -EFAULT)
1781 continue;
1782 ret = rmap_one(page, vma, address, arg);
1783 if (ret != SWAP_AGAIN)
1784 break;
1785 }
1786 /*
1787 * No nonlinear handling: being always shared, nonlinear vmas
1788 * never contain migration ptes. Decide what to do about this
1789 * limitation to linear when we need rmap_walk() on nonlinear.
1790 */
1791 mutex_unlock(&mapping->i_mmap_mutex);
1792 return ret;
1793 }
1794
rmap_walk(struct page * page,int (* rmap_one)(struct page *,struct vm_area_struct *,unsigned long,void *),void * arg)1795 int rmap_walk(struct page *page, int (*rmap_one)(struct page *,
1796 struct vm_area_struct *, unsigned long, void *), void *arg)
1797 {
1798 VM_BUG_ON(!PageLocked(page));
1799
1800 if (unlikely(PageKsm(page)))
1801 return rmap_walk_ksm(page, rmap_one, arg);
1802 else if (PageAnon(page))
1803 return rmap_walk_anon(page, rmap_one, arg);
1804 else
1805 return rmap_walk_file(page, rmap_one, arg);
1806 }
1807 #endif /* CONFIG_MIGRATION */
1808
1809 #ifdef CONFIG_HUGETLB_PAGE
1810 /*
1811 * The following three functions are for anonymous (private mapped) hugepages.
1812 * Unlike common anonymous pages, anonymous hugepages have no accounting code
1813 * and no lru code, because we handle hugepages differently from common pages.
1814 */
__hugepage_set_anon_rmap(struct page * page,struct vm_area_struct * vma,unsigned long address,int exclusive)1815 static void __hugepage_set_anon_rmap(struct page *page,
1816 struct vm_area_struct *vma, unsigned long address, int exclusive)
1817 {
1818 struct anon_vma *anon_vma = vma->anon_vma;
1819
1820 BUG_ON(!anon_vma);
1821
1822 if (PageAnon(page))
1823 return;
1824 if (!exclusive)
1825 anon_vma = anon_vma->root;
1826
1827 anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
1828 page->mapping = (struct address_space *) anon_vma;
1829 page->index = linear_page_index(vma, address);
1830 }
1831
hugepage_add_anon_rmap(struct page * page,struct vm_area_struct * vma,unsigned long address)1832 void hugepage_add_anon_rmap(struct page *page,
1833 struct vm_area_struct *vma, unsigned long address)
1834 {
1835 struct anon_vma *anon_vma = vma->anon_vma;
1836 int first;
1837
1838 BUG_ON(!PageLocked(page));
1839 BUG_ON(!anon_vma);
1840 /* address might be in next vma when migration races vma_adjust */
1841 first = atomic_inc_and_test(&page->_mapcount);
1842 if (first)
1843 __hugepage_set_anon_rmap(page, vma, address, 0);
1844 }
1845
hugepage_add_new_anon_rmap(struct page * page,struct vm_area_struct * vma,unsigned long address)1846 void hugepage_add_new_anon_rmap(struct page *page,
1847 struct vm_area_struct *vma, unsigned long address)
1848 {
1849 BUG_ON(address < vma->vm_start || address >= vma->vm_end);
1850 atomic_set(&page->_mapcount, 0);
1851 __hugepage_set_anon_rmap(page, vma, address, 1);
1852 }
1853 #endif /* CONFIG_HUGETLB_PAGE */
1854