blob: 7e60df99018e033fe87c316dde6ba4e592fc5e0f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * mm/rmap.c - physical to virtual reverse mappings
3 *
4 * Copyright 2001, Rik van Riel <riel@conectiva.com.br>
5 * Released under the General Public License (GPL).
6 *
7 * Simple, low overhead reverse mapping scheme.
8 * Please try to keep this thing as modular as possible.
9 *
10 * Provides methods for unmapping each kind of mapped page:
11 * the anon methods track anonymous pages, and
12 * the file methods track pages belonging to an inode.
13 *
14 * Original design by Rik van Riel <riel@conectiva.com.br> 2001
15 * File methods by Dave McCracken <dmccr@us.ibm.com> 2003, 2004
16 * Anonymous methods by Andrea Arcangeli <andrea@suse.de> 2004
17 * Contributions by Hugh Dickins <hugh@veritas.com> 2003, 2004
18 */
19
20/*
21 * Lock ordering in mm:
22 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080023 * inode->i_mutex (while writing or truncating, not reading or faulting)
Nick Piggin82591e62006-10-19 23:29:10 -070024 * inode->i_alloc_sem (vmtruncate_range)
25 * mm->mmap_sem
26 * page->flags PG_locked (lock_page)
27 * mapping->i_mmap_lock
28 * anon_vma->lock
29 * mm->page_table_lock or pte_lock
30 * zone->lru_lock (in mark_page_accessed, isolate_lru_page)
31 * swap_lock (in swap_duplicate, swap_info_get)
32 * mmlist_lock (in mmput, drain_mmlist and others)
33 * mapping->private_lock (in __set_page_dirty_buffers)
34 * inode_lock (in set_page_dirty's __mark_inode_dirty)
35 * sb_lock (within inode_lock in fs/fs-writeback.c)
36 * mapping->tree_lock (widely used, in set_page_dirty,
37 * in arch-dependent flush_dcache_mmap_lock,
38 * within inode_lock in __sync_single_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 */
40
41#include <linux/mm.h>
42#include <linux/pagemap.h>
43#include <linux/swap.h>
44#include <linux/swapops.h>
45#include <linux/slab.h>
46#include <linux/init.h>
47#include <linux/rmap.h>
48#include <linux/rcupdate.h>
Christoph Lametera48d07a2006-02-01 03:05:38 -080049#include <linux/module.h>
Nick Piggin7de6b802006-12-22 01:09:33 -080050#include <linux/kallsyms.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080051#include <linux/memcontrol.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070052#include <linux/mmu_notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#include <asm/tlbflush.h>
55
Nick Pigginb291f002008-10-18 20:26:44 -070056#include "internal.h"
57
Pekka Enbergfcc234f2006-03-22 00:08:13 -080058struct kmem_cache *anon_vma_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvaldsd9d332e2008-10-19 10:32:20 -070060/**
61 * anon_vma_prepare - attach an anon_vma to a memory region
62 * @vma: the memory region in question
63 *
64 * This makes sure the memory mapping described by 'vma' has
65 * an 'anon_vma' attached to it, so that we can associate the
66 * anonymous pages mapped into it with that anon_vma.
67 *
68 * The common case will be that we already have one, but if
69 * if not we either need to find an adjacent mapping that we
70 * can re-use the anon_vma from (very common when the only
71 * reason for splitting a vma has been mprotect()), or we
72 * allocate a new one.
73 *
74 * Anon-vma allocations are very subtle, because we may have
75 * optimistically looked up an anon_vma in page_lock_anon_vma()
76 * and that may actually touch the spinlock even in the newly
77 * allocated vma (it depends on RCU to make sure that the
78 * anon_vma isn't actually destroyed).
79 *
80 * As a result, we need to do proper anon_vma locking even
81 * for the new allocation. At the same time, we do not want
82 * to do any locking for the common case of already having
83 * an anon_vma.
84 *
85 * This must be called with the mmap_sem held for reading.
86 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087int anon_vma_prepare(struct vm_area_struct *vma)
88{
89 struct anon_vma *anon_vma = vma->anon_vma;
90
91 might_sleep();
92 if (unlikely(!anon_vma)) {
93 struct mm_struct *mm = vma->vm_mm;
Linus Torvaldsd9d332e2008-10-19 10:32:20 -070094 struct anon_vma *allocated;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 anon_vma = find_mergeable_anon_vma(vma);
Linus Torvaldsd9d332e2008-10-19 10:32:20 -070097 allocated = NULL;
98 if (!anon_vma) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 anon_vma = anon_vma_alloc();
100 if (unlikely(!anon_vma))
101 return -ENOMEM;
102 allocated = anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
Linus Torvaldsd9d332e2008-10-19 10:32:20 -0700104 spin_lock(&anon_vma->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 /* page_table_lock to protect against threads */
107 spin_lock(&mm->page_table_lock);
108 if (likely(!vma->anon_vma)) {
109 vma->anon_vma = anon_vma;
Christoph Lameter06972122006-06-23 02:03:35 -0700110 list_add_tail(&vma->anon_vma_node, &anon_vma->head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 allocated = NULL;
112 }
113 spin_unlock(&mm->page_table_lock);
114
Linus Torvaldsd9d332e2008-10-19 10:32:20 -0700115 spin_unlock(&anon_vma->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (unlikely(allocated))
117 anon_vma_free(allocated);
118 }
119 return 0;
120}
121
122void __anon_vma_merge(struct vm_area_struct *vma, struct vm_area_struct *next)
123{
124 BUG_ON(vma->anon_vma != next->anon_vma);
125 list_del(&next->anon_vma_node);
126}
127
128void __anon_vma_link(struct vm_area_struct *vma)
129{
130 struct anon_vma *anon_vma = vma->anon_vma;
131
Hugh Dickins30acbab2007-06-27 14:09:53 -0700132 if (anon_vma)
Christoph Lameter06972122006-06-23 02:03:35 -0700133 list_add_tail(&vma->anon_vma_node, &anon_vma->head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
136void anon_vma_link(struct vm_area_struct *vma)
137{
138 struct anon_vma *anon_vma = vma->anon_vma;
139
140 if (anon_vma) {
141 spin_lock(&anon_vma->lock);
Christoph Lameter06972122006-06-23 02:03:35 -0700142 list_add_tail(&vma->anon_vma_node, &anon_vma->head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 spin_unlock(&anon_vma->lock);
144 }
145}
146
147void anon_vma_unlink(struct vm_area_struct *vma)
148{
149 struct anon_vma *anon_vma = vma->anon_vma;
150 int empty;
151
152 if (!anon_vma)
153 return;
154
155 spin_lock(&anon_vma->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 list_del(&vma->anon_vma_node);
157
158 /* We must garbage collect the anon_vma if it's empty */
159 empty = list_empty(&anon_vma->head);
160 spin_unlock(&anon_vma->lock);
161
162 if (empty)
163 anon_vma_free(anon_vma);
164}
165
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700166static void anon_vma_ctor(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Christoph Lametera35afb82007-05-16 22:10:57 -0700168 struct anon_vma *anon_vma = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Christoph Lametera35afb82007-05-16 22:10:57 -0700170 spin_lock_init(&anon_vma->lock);
171 INIT_LIST_HEAD(&anon_vma->head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174void __init anon_vma_init(void)
175{
176 anon_vma_cachep = kmem_cache_create("anon_vma", sizeof(struct anon_vma),
Paul Mundt20c2df82007-07-20 10:11:58 +0900177 0, SLAB_DESTROY_BY_RCU|SLAB_PANIC, anon_vma_ctor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180/*
181 * Getting a lock on a stable anon_vma from a page off the LRU is
182 * tricky: page_lock_anon_vma rely on RCU to guard against the races.
183 */
184static struct anon_vma *page_lock_anon_vma(struct page *page)
185{
Oleg Nesterov34bbd702007-02-28 20:13:49 -0800186 struct anon_vma *anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 unsigned long anon_mapping;
188
189 rcu_read_lock();
190 anon_mapping = (unsigned long) page->mapping;
191 if (!(anon_mapping & PAGE_MAPPING_ANON))
192 goto out;
193 if (!page_mapped(page))
194 goto out;
195
196 anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON);
197 spin_lock(&anon_vma->lock);
Oleg Nesterov34bbd702007-02-28 20:13:49 -0800198 return anon_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199out:
200 rcu_read_unlock();
Oleg Nesterov34bbd702007-02-28 20:13:49 -0800201 return NULL;
202}
203
204static void page_unlock_anon_vma(struct anon_vma *anon_vma)
205{
206 spin_unlock(&anon_vma->lock);
207 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
210/*
Lee Schermerhorn3ad33b22007-11-14 16:59:10 -0800211 * At what user virtual address is page expected in @vma?
212 * Returns virtual address or -EFAULT if page's index/offset is not
213 * within the range mapped the @vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 */
215static inline unsigned long
216vma_address(struct page *page, struct vm_area_struct *vma)
217{
218 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
219 unsigned long address;
220
221 address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
222 if (unlikely(address < vma->vm_start || address >= vma->vm_end)) {
Lee Schermerhorn3ad33b22007-11-14 16:59:10 -0800223 /* page should be within @vma mapping range */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return -EFAULT;
225 }
226 return address;
227}
228
229/*
230 * At what user virtual address is page expected in vma? checking that the
Hugh Dickinsee498ed2005-11-21 21:32:18 -0800231 * page matches the vma: currently only used on anon pages, by unuse_vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 */
233unsigned long page_address_in_vma(struct page *page, struct vm_area_struct *vma)
234{
235 if (PageAnon(page)) {
236 if ((void *)vma->anon_vma !=
237 (void *)page->mapping - PAGE_MAPPING_ANON)
238 return -EFAULT;
239 } else if (page->mapping && !(vma->vm_flags & VM_NONLINEAR)) {
Hugh Dickinsee498ed2005-11-21 21:32:18 -0800240 if (!vma->vm_file ||
241 vma->vm_file->f_mapping != page->mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return -EFAULT;
243 } else
244 return -EFAULT;
245 return vma_address(page, vma);
246}
247
248/*
Nikita Danilov81b40822005-05-01 08:58:36 -0700249 * Check that @page is mapped at @address into @mm.
250 *
Nick Piggin479db0b2008-08-20 14:09:18 -0700251 * If @sync is false, page_check_address may perform a racy check to avoid
252 * the page table lock when the pte is not present (helpful when reclaiming
253 * highly shared pages).
254 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -0700255 * On success returns with pte mapped and locked.
Nikita Danilov81b40822005-05-01 08:58:36 -0700256 */
Carsten Otteceffc072005-06-23 22:05:25 -0700257pte_t *page_check_address(struct page *page, struct mm_struct *mm,
Nick Piggin479db0b2008-08-20 14:09:18 -0700258 unsigned long address, spinlock_t **ptlp, int sync)
Nikita Danilov81b40822005-05-01 08:58:36 -0700259{
260 pgd_t *pgd;
261 pud_t *pud;
262 pmd_t *pmd;
263 pte_t *pte;
Hugh Dickinsc0718802005-10-29 18:16:31 -0700264 spinlock_t *ptl;
Nikita Danilov81b40822005-05-01 08:58:36 -0700265
Nikita Danilov81b40822005-05-01 08:58:36 -0700266 pgd = pgd_offset(mm, address);
Hugh Dickinsc0718802005-10-29 18:16:31 -0700267 if (!pgd_present(*pgd))
268 return NULL;
269
270 pud = pud_offset(pgd, address);
271 if (!pud_present(*pud))
272 return NULL;
273
274 pmd = pmd_offset(pud, address);
275 if (!pmd_present(*pmd))
276 return NULL;
277
278 pte = pte_offset_map(pmd, address);
279 /* Make a quick check before getting the lock */
Nick Piggin479db0b2008-08-20 14:09:18 -0700280 if (!sync && !pte_present(*pte)) {
Hugh Dickinsc0718802005-10-29 18:16:31 -0700281 pte_unmap(pte);
282 return NULL;
Nikita Danilov81b40822005-05-01 08:58:36 -0700283 }
Hugh Dickinsc0718802005-10-29 18:16:31 -0700284
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700285 ptl = pte_lockptr(mm, pmd);
Hugh Dickinsc0718802005-10-29 18:16:31 -0700286 spin_lock(ptl);
287 if (pte_present(*pte) && page_to_pfn(page) == pte_pfn(*pte)) {
288 *ptlp = ptl;
289 return pte;
290 }
291 pte_unmap_unlock(pte, ptl);
292 return NULL;
Nikita Danilov81b40822005-05-01 08:58:36 -0700293}
294
Nick Pigginb291f002008-10-18 20:26:44 -0700295/**
296 * page_mapped_in_vma - check whether a page is really mapped in a VMA
297 * @page: the page to test
298 * @vma: the VMA to test
299 *
300 * Returns 1 if the page is mapped into the page tables of the VMA, 0
301 * if the page is not mapped into the page tables of this VMA. Only
302 * valid for normal file or anonymous VMAs.
303 */
304static int page_mapped_in_vma(struct page *page, struct vm_area_struct *vma)
305{
306 unsigned long address;
307 pte_t *pte;
308 spinlock_t *ptl;
309
310 address = vma_address(page, vma);
311 if (address == -EFAULT) /* out of vma range */
312 return 0;
313 pte = page_check_address(page, vma->vm_mm, address, &ptl, 1);
314 if (!pte) /* the page is not in this mm */
315 return 0;
316 pte_unmap_unlock(pte, ptl);
317
318 return 1;
319}
320
Nikita Danilov81b40822005-05-01 08:58:36 -0700321/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 * Subfunctions of page_referenced: page_referenced_one called
323 * repeatedly from either page_referenced_anon or page_referenced_file.
324 */
325static int page_referenced_one(struct page *page,
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800326 struct vm_area_struct *vma, unsigned int *mapcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 struct mm_struct *mm = vma->vm_mm;
329 unsigned long address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 pte_t *pte;
Hugh Dickinsc0718802005-10-29 18:16:31 -0700331 spinlock_t *ptl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 int referenced = 0;
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 address = vma_address(page, vma);
335 if (address == -EFAULT)
336 goto out;
337
Nick Piggin479db0b2008-08-20 14:09:18 -0700338 pte = page_check_address(page, mm, address, &ptl, 0);
Hugh Dickinsc0718802005-10-29 18:16:31 -0700339 if (!pte)
340 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Nick Pigginb291f002008-10-18 20:26:44 -0700342 /*
343 * Don't want to elevate referenced for mlocked page that gets this far,
344 * in order that it progresses to try_to_unmap and is moved to the
345 * unevictable list.
346 */
Hugh Dickins5a9bbdc2008-02-04 22:29:23 -0800347 if (vma->vm_flags & VM_LOCKED) {
Hugh Dickins5a9bbdc2008-02-04 22:29:23 -0800348 *mapcount = 1; /* break early from loop */
Nick Pigginb291f002008-10-18 20:26:44 -0700349 goto out_unmap;
350 }
351
352 if (ptep_clear_flush_young_notify(vma, address, pte))
Hugh Dickinsc0718802005-10-29 18:16:31 -0700353 referenced++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Hugh Dickinsc0718802005-10-29 18:16:31 -0700355 /* Pretend the page is referenced if the task has the
356 swap token and is in the middle of a page fault. */
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800357 if (mm != current->mm && has_swap_token(mm) &&
Hugh Dickinsc0718802005-10-29 18:16:31 -0700358 rwsem_is_locked(&mm->mmap_sem))
359 referenced++;
360
Nick Pigginb291f002008-10-18 20:26:44 -0700361out_unmap:
Hugh Dickinsc0718802005-10-29 18:16:31 -0700362 (*mapcount)--;
363 pte_unmap_unlock(pte, ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364out:
365 return referenced;
366}
367
Balbir Singhbed71612008-02-07 00:14:01 -0800368static int page_referenced_anon(struct page *page,
369 struct mem_cgroup *mem_cont)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 unsigned int mapcount;
372 struct anon_vma *anon_vma;
373 struct vm_area_struct *vma;
374 int referenced = 0;
375
376 anon_vma = page_lock_anon_vma(page);
377 if (!anon_vma)
378 return referenced;
379
380 mapcount = page_mapcount(page);
381 list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
Balbir Singhbed71612008-02-07 00:14:01 -0800382 /*
383 * If we are reclaiming on behalf of a cgroup, skip
384 * counting on behalf of references from different
385 * cgroups
386 */
Hugh Dickinsbd845e32008-03-04 14:29:01 -0800387 if (mem_cont && !mm_match_cgroup(vma->vm_mm, mem_cont))
Balbir Singhbed71612008-02-07 00:14:01 -0800388 continue;
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800389 referenced += page_referenced_one(page, vma, &mapcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (!mapcount)
391 break;
392 }
Oleg Nesterov34bbd702007-02-28 20:13:49 -0800393
394 page_unlock_anon_vma(anon_vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return referenced;
396}
397
398/**
399 * page_referenced_file - referenced check for object-based rmap
400 * @page: the page we're checking references on.
Randy Dunlap43d8eac2008-03-19 17:00:43 -0700401 * @mem_cont: target memory controller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 *
403 * For an object-based mapped page, find all the places it is mapped and
404 * check/clear the referenced flag. This is done by following the page->mapping
405 * pointer, then walking the chain of vmas it holds. It returns the number
406 * of references it found.
407 *
408 * This function is only called from page_referenced for object-based pages.
409 */
Balbir Singhbed71612008-02-07 00:14:01 -0800410static int page_referenced_file(struct page *page,
411 struct mem_cgroup *mem_cont)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 unsigned int mapcount;
414 struct address_space *mapping = page->mapping;
415 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
416 struct vm_area_struct *vma;
417 struct prio_tree_iter iter;
418 int referenced = 0;
419
420 /*
421 * The caller's checks on page->mapping and !PageAnon have made
422 * sure that this is a file page: the check for page->mapping
423 * excludes the case just before it gets set on an anon page.
424 */
425 BUG_ON(PageAnon(page));
426
427 /*
428 * The page lock not only makes sure that page->mapping cannot
429 * suddenly be NULLified by truncation, it makes sure that the
430 * structure at mapping cannot be freed and reused yet,
431 * so we can safely take mapping->i_mmap_lock.
432 */
433 BUG_ON(!PageLocked(page));
434
435 spin_lock(&mapping->i_mmap_lock);
436
437 /*
438 * i_mmap_lock does not stabilize mapcount at all, but mapcount
439 * is more likely to be accurate if we note it after spinning.
440 */
441 mapcount = page_mapcount(page);
442
443 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
Balbir Singhbed71612008-02-07 00:14:01 -0800444 /*
445 * If we are reclaiming on behalf of a cgroup, skip
446 * counting on behalf of references from different
447 * cgroups
448 */
Hugh Dickinsbd845e32008-03-04 14:29:01 -0800449 if (mem_cont && !mm_match_cgroup(vma->vm_mm, mem_cont))
Balbir Singhbed71612008-02-07 00:14:01 -0800450 continue;
Rik van Rielf7b7fd82005-11-28 13:44:07 -0800451 referenced += page_referenced_one(page, vma, &mapcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (!mapcount)
453 break;
454 }
455
456 spin_unlock(&mapping->i_mmap_lock);
457 return referenced;
458}
459
460/**
461 * page_referenced - test if the page was referenced
462 * @page: the page to test
463 * @is_locked: caller holds lock on the page
Randy Dunlap43d8eac2008-03-19 17:00:43 -0700464 * @mem_cont: target memory controller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 *
466 * Quick test_and_clear_referenced for all mappings to a page,
467 * returns the number of ptes which referenced the page.
468 */
Balbir Singhbed71612008-02-07 00:14:01 -0800469int page_referenced(struct page *page, int is_locked,
470 struct mem_cgroup *mem_cont)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 int referenced = 0;
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (TestClearPageReferenced(page))
475 referenced++;
476
477 if (page_mapped(page) && page->mapping) {
478 if (PageAnon(page))
Balbir Singhbed71612008-02-07 00:14:01 -0800479 referenced += page_referenced_anon(page, mem_cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 else if (is_locked)
Balbir Singhbed71612008-02-07 00:14:01 -0800481 referenced += page_referenced_file(page, mem_cont);
Nick Piggin529ae9a2008-08-02 12:01:03 +0200482 else if (!trylock_page(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 referenced++;
484 else {
485 if (page->mapping)
Balbir Singhbed71612008-02-07 00:14:01 -0800486 referenced +=
487 page_referenced_file(page, mem_cont);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 unlock_page(page);
489 }
490 }
Christian Borntraeger5b7baf02008-03-25 18:47:12 +0100491
492 if (page_test_and_clear_young(page))
493 referenced++;
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return referenced;
496}
497
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700498static int page_mkclean_one(struct page *page, struct vm_area_struct *vma)
499{
500 struct mm_struct *mm = vma->vm_mm;
501 unsigned long address;
Peter Zijlstrac2fda5f2006-12-22 14:25:52 +0100502 pte_t *pte;
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700503 spinlock_t *ptl;
504 int ret = 0;
505
506 address = vma_address(page, vma);
507 if (address == -EFAULT)
508 goto out;
509
Nick Piggin479db0b2008-08-20 14:09:18 -0700510 pte = page_check_address(page, mm, address, &ptl, 1);
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700511 if (!pte)
512 goto out;
513
Peter Zijlstrac2fda5f2006-12-22 14:25:52 +0100514 if (pte_dirty(*pte) || pte_write(*pte)) {
515 pte_t entry;
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700516
Peter Zijlstrac2fda5f2006-12-22 14:25:52 +0100517 flush_cache_page(vma, address, pte_pfn(*pte));
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700518 entry = ptep_clear_flush_notify(vma, address, pte);
Peter Zijlstrac2fda5f2006-12-22 14:25:52 +0100519 entry = pte_wrprotect(entry);
520 entry = pte_mkclean(entry);
Al Virod6e88e62006-12-29 16:48:35 -0800521 set_pte_at(mm, address, pte, entry);
Peter Zijlstrac2fda5f2006-12-22 14:25:52 +0100522 ret = 1;
523 }
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700524
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700525 pte_unmap_unlock(pte, ptl);
526out:
527 return ret;
528}
529
530static int page_mkclean_file(struct address_space *mapping, struct page *page)
531{
532 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
533 struct vm_area_struct *vma;
534 struct prio_tree_iter iter;
535 int ret = 0;
536
537 BUG_ON(PageAnon(page));
538
539 spin_lock(&mapping->i_mmap_lock);
540 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
541 if (vma->vm_flags & VM_SHARED)
542 ret += page_mkclean_one(page, vma);
543 }
544 spin_unlock(&mapping->i_mmap_lock);
545 return ret;
546}
547
548int page_mkclean(struct page *page)
549{
550 int ret = 0;
551
552 BUG_ON(!PageLocked(page));
553
554 if (page_mapped(page)) {
555 struct address_space *mapping = page_mapping(page);
Christian Borntraegerce7e9fa2007-11-20 11:13:36 +0100556 if (mapping) {
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700557 ret = page_mkclean_file(mapping, page);
Christian Borntraegerce7e9fa2007-11-20 11:13:36 +0100558 if (page_test_dirty(page)) {
559 page_clear_dirty(page);
560 ret = 1;
561 }
Martin Schwidefsky6c210482007-04-27 16:01:57 +0200562 }
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700563 }
564
565 return ret;
566}
Jaya Kumar60b59be2007-05-08 00:37:37 -0700567EXPORT_SYMBOL_GPL(page_mkclean);
Peter Zijlstrad08b3852006-09-25 23:30:57 -0700568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569/**
Randy Dunlap43d8eac2008-03-19 17:00:43 -0700570 * __page_set_anon_rmap - setup new anonymous rmap
Nick Piggin9617d952006-01-06 00:11:12 -0800571 * @page: the page to add the mapping to
572 * @vma: the vm area in which the mapping is added
573 * @address: the user virtual address mapped
574 */
575static void __page_set_anon_rmap(struct page *page,
576 struct vm_area_struct *vma, unsigned long address)
577{
578 struct anon_vma *anon_vma = vma->anon_vma;
579
580 BUG_ON(!anon_vma);
581 anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
582 page->mapping = (struct address_space *) anon_vma;
583
584 page->index = linear_page_index(vma, address);
585
Nick Piggina74609f2006-01-06 00:11:20 -0800586 /*
587 * nr_mapped state can be updated without turning off
588 * interrupts because it is not modified via interrupt.
589 */
Christoph Lameterf3dbd342006-06-30 01:55:36 -0700590 __inc_zone_page_state(page, NR_ANON_PAGES);
Nick Piggin9617d952006-01-06 00:11:12 -0800591}
592
593/**
Randy Dunlap43d8eac2008-03-19 17:00:43 -0700594 * __page_check_anon_rmap - sanity check anonymous rmap addition
Nick Pigginc97a9e12007-05-16 22:11:21 -0700595 * @page: the page to add the mapping to
596 * @vma: the vm area in which the mapping is added
597 * @address: the user virtual address mapped
598 */
599static void __page_check_anon_rmap(struct page *page,
600 struct vm_area_struct *vma, unsigned long address)
601{
602#ifdef CONFIG_DEBUG_VM
603 /*
604 * The page's anon-rmap details (mapping and index) are guaranteed to
605 * be set up correctly at this point.
606 *
607 * We have exclusion against page_add_anon_rmap because the caller
608 * always holds the page locked, except if called from page_dup_rmap,
609 * in which case the page is already known to be setup.
610 *
611 * We have exclusion against page_add_new_anon_rmap because those pages
612 * are initially only visible via the pagetables, and the pte is locked
613 * over the call to page_add_new_anon_rmap.
614 */
615 struct anon_vma *anon_vma = vma->anon_vma;
616 anon_vma = (void *) anon_vma + PAGE_MAPPING_ANON;
617 BUG_ON(page->mapping != (struct address_space *)anon_vma);
618 BUG_ON(page->index != linear_page_index(vma, address));
619#endif
620}
621
622/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 * page_add_anon_rmap - add pte mapping to an anonymous page
624 * @page: the page to add the mapping to
625 * @vma: the vm area in which the mapping is added
626 * @address: the user virtual address mapped
627 *
Nick Pigginc97a9e12007-05-16 22:11:21 -0700628 * The caller needs to hold the pte lock and the page must be locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 */
630void page_add_anon_rmap(struct page *page,
631 struct vm_area_struct *vma, unsigned long address)
632{
Nick Pigginc97a9e12007-05-16 22:11:21 -0700633 VM_BUG_ON(!PageLocked(page));
634 VM_BUG_ON(address < vma->vm_start || address >= vma->vm_end);
Nick Piggin9617d952006-01-06 00:11:12 -0800635 if (atomic_inc_and_test(&page->_mapcount))
636 __page_set_anon_rmap(page, vma, address);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700637 else
Nick Pigginc97a9e12007-05-16 22:11:21 -0700638 __page_check_anon_rmap(page, vma, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
Randy Dunlap43d8eac2008-03-19 17:00:43 -0700641/**
Nick Piggin9617d952006-01-06 00:11:12 -0800642 * page_add_new_anon_rmap - add pte mapping to a new anonymous page
643 * @page: the page to add the mapping to
644 * @vma: the vm area in which the mapping is added
645 * @address: the user virtual address mapped
646 *
647 * Same as page_add_anon_rmap but must only be called on *new* pages.
648 * This means the inc-and-test can be bypassed.
Nick Pigginc97a9e12007-05-16 22:11:21 -0700649 * Page does not have to be locked.
Nick Piggin9617d952006-01-06 00:11:12 -0800650 */
651void page_add_new_anon_rmap(struct page *page,
652 struct vm_area_struct *vma, unsigned long address)
653{
Nick Pigginc97a9e12007-05-16 22:11:21 -0700654 BUG_ON(address < vma->vm_start || address >= vma->vm_end);
Nick Piggin9617d952006-01-06 00:11:12 -0800655 atomic_set(&page->_mapcount, 0); /* elevate count by 1 (starts at -1) */
656 __page_set_anon_rmap(page, vma, address);
657}
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659/**
660 * page_add_file_rmap - add pte mapping to a file page
661 * @page: the page to add the mapping to
662 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -0700663 * The caller needs to hold the pte lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 */
665void page_add_file_rmap(struct page *page)
666{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (atomic_inc_and_test(&page->_mapcount))
Christoph Lameter65ba55f2006-06-30 01:55:34 -0700668 __inc_zone_page_state(page, NR_FILE_MAPPED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
Nick Pigginc97a9e12007-05-16 22:11:21 -0700671#ifdef CONFIG_DEBUG_VM
672/**
673 * page_dup_rmap - duplicate pte mapping to a page
674 * @page: the page to add the mapping to
Randy Dunlap43d8eac2008-03-19 17:00:43 -0700675 * @vma: the vm area being duplicated
676 * @address: the user virtual address mapped
Nick Pigginc97a9e12007-05-16 22:11:21 -0700677 *
678 * For copy_page_range only: minimal extract from page_add_file_rmap /
679 * page_add_anon_rmap, avoiding unnecessary tests (already checked) so it's
680 * quicker.
681 *
682 * The caller needs to hold the pte lock.
683 */
684void page_dup_rmap(struct page *page, struct vm_area_struct *vma, unsigned long address)
685{
686 BUG_ON(page_mapcount(page) == 0);
687 if (PageAnon(page))
688 __page_check_anon_rmap(page, vma, address);
689 atomic_inc(&page->_mapcount);
690}
691#endif
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693/**
694 * page_remove_rmap - take down pte mapping from a page
695 * @page: page to remove mapping from
Randy Dunlap43d8eac2008-03-19 17:00:43 -0700696 * @vma: the vm area in which the mapping is removed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 *
Hugh Dickinsb8072f02005-10-29 18:16:41 -0700698 * The caller needs to hold the pte lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 */
Nick Piggin7de6b802006-12-22 01:09:33 -0800700void page_remove_rmap(struct page *page, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (atomic_add_negative(-1, &page->_mapcount)) {
Nick Pigginb7ab7952006-03-22 00:08:42 -0800703 if (unlikely(page_mapcount(page) < 0)) {
Dave Jonesef2bf0d2006-01-08 01:01:00 -0800704 printk (KERN_EMERG "Eeek! page_mapcount(page) went negative! (%d)\n", page_mapcount(page));
Nick Piggin7de6b802006-12-22 01:09:33 -0800705 printk (KERN_EMERG " page pfn = %lx\n", page_to_pfn(page));
Dave Jonesef2bf0d2006-01-08 01:01:00 -0800706 printk (KERN_EMERG " page->flags = %lx\n", page->flags);
707 printk (KERN_EMERG " page->count = %x\n", page_count(page));
708 printk (KERN_EMERG " page->mapping = %p\n", page->mapping);
Nick Piggin7de6b802006-12-22 01:09:33 -0800709 print_symbol (KERN_EMERG " vma->vm_ops = %s\n", (unsigned long)vma->vm_ops);
Nick Piggin54cb8822007-07-19 01:46:59 -0700710 if (vma->vm_ops) {
Nick Piggin54cb8822007-07-19 01:46:59 -0700711 print_symbol (KERN_EMERG " vma->vm_ops->fault = %s\n", (unsigned long)vma->vm_ops->fault);
712 }
Nick Piggin7de6b802006-12-22 01:09:33 -0800713 if (vma->vm_file && vma->vm_file->f_op)
714 print_symbol (KERN_EMERG " vma->vm_file->f_op->mmap = %s\n", (unsigned long)vma->vm_file->f_op->mmap);
Dave Jonesb16bc642006-10-11 01:21:27 -0700715 BUG();
Dave Jonesef2bf0d2006-01-08 01:01:00 -0800716 }
Dave Jonesb16bc642006-10-11 01:21:27 -0700717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 /*
Hugh Dickins16f8c5b2008-08-20 14:09:04 -0700719 * Now that the last pte has gone, s390 must transfer dirty
720 * flag from storage key to struct page. We can usually skip
721 * this if the page is anon, so about to be freed; but perhaps
722 * not if it's in swapcache - there might be another pte slot
723 * containing the swap entry, but page not yet written to swap.
724 */
725 if ((!PageAnon(page) || PageSwapCache(page)) &&
726 page_test_dirty(page)) {
727 page_clear_dirty(page);
728 set_page_dirty(page);
729 }
730
731 mem_cgroup_uncharge_page(page);
732 __dec_zone_page_state(page,
733 PageAnon(page) ? NR_ANON_PAGES : NR_FILE_MAPPED);
734 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 * It would be tidy to reset the PageAnon mapping here,
736 * but that might overwrite a racing page_add_anon_rmap
737 * which increments mapcount after us but sets mapping
738 * before us: so leave the reset to free_hot_cold_page,
739 * and remember that it's only reliable while mapped.
740 * Leaving it set also helps swapoff to reinstate ptes
741 * faster for those pages still in swapcache.
742 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744}
745
746/*
747 * Subfunctions of try_to_unmap: try_to_unmap_one called
748 * repeatedly from either try_to_unmap_anon or try_to_unmap_file.
749 */
Christoph Lametera48d07a2006-02-01 03:05:38 -0800750static int try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
Christoph Lameter73523492006-06-23 02:03:27 -0700751 int migration)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 struct mm_struct *mm = vma->vm_mm;
754 unsigned long address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 pte_t *pte;
756 pte_t pteval;
Hugh Dickinsc0718802005-10-29 18:16:31 -0700757 spinlock_t *ptl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 int ret = SWAP_AGAIN;
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 address = vma_address(page, vma);
761 if (address == -EFAULT)
762 goto out;
763
Nick Piggin479db0b2008-08-20 14:09:18 -0700764 pte = page_check_address(page, mm, address, &ptl, 0);
Hugh Dickinsc0718802005-10-29 18:16:31 -0700765 if (!pte)
Nikita Danilov81b40822005-05-01 08:58:36 -0700766 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 /*
769 * If the page is mlock()d, we cannot swap it out.
770 * If it's recently referenced (perhaps page_referenced
771 * skipped over this mm) then we should reactivate it.
772 */
Nick Pigginb291f002008-10-18 20:26:44 -0700773 if (!migration) {
774 if (vma->vm_flags & VM_LOCKED) {
775 ret = SWAP_MLOCK;
776 goto out_unmap;
777 }
778 if (ptep_clear_flush_young_notify(vma, address, pte)) {
779 ret = SWAP_FAIL;
780 goto out_unmap;
781 }
782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 /* Nuke the page table entry. */
785 flush_cache_page(vma, address, page_to_pfn(page));
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700786 pteval = ptep_clear_flush_notify(vma, address, pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 /* Move the dirty bit to the physical page now the pte is gone. */
789 if (pte_dirty(pteval))
790 set_page_dirty(page);
791
Hugh Dickins365e9c872005-10-29 18:16:18 -0700792 /* Update high watermark before we lower rss */
793 update_hiwater_rss(mm);
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (PageAnon(page)) {
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700796 swp_entry_t entry = { .val = page_private(page) };
Christoph Lameter06972122006-06-23 02:03:35 -0700797
798 if (PageSwapCache(page)) {
799 /*
800 * Store the swap location in the pte.
801 * See handle_pte_fault() ...
802 */
803 swap_duplicate(entry);
804 if (list_empty(&mm->mmlist)) {
805 spin_lock(&mmlist_lock);
806 if (list_empty(&mm->mmlist))
807 list_add(&mm->mmlist, &init_mm.mmlist);
808 spin_unlock(&mmlist_lock);
809 }
Christoph Lameter442c9132006-06-23 02:03:38 -0700810 dec_mm_counter(mm, anon_rss);
Christoph Lameter04e62a22006-06-23 02:03:38 -0700811#ifdef CONFIG_MIGRATION
Christoph Lameter06972122006-06-23 02:03:35 -0700812 } else {
813 /*
814 * Store the pfn of the page in a special migration
815 * pte. do_swap_page() will wait until the migration
816 * pte is removed and then restart fault handling.
817 */
818 BUG_ON(!migration);
819 entry = make_migration_entry(page, pte_write(pteval));
Christoph Lameter04e62a22006-06-23 02:03:38 -0700820#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
822 set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
823 BUG_ON(pte_file(*pte));
Hugh Dickins42946212005-10-29 18:16:05 -0700824 } else
Christoph Lameter04e62a22006-06-23 02:03:38 -0700825#ifdef CONFIG_MIGRATION
826 if (migration) {
827 /* Establish migration entry for a file page */
828 swp_entry_t entry;
829 entry = make_migration_entry(page, pte_write(pteval));
830 set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
831 } else
832#endif
Hugh Dickins42946212005-10-29 18:16:05 -0700833 dec_mm_counter(mm, file_rss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Christoph Lameter04e62a22006-06-23 02:03:38 -0700835
Nick Piggin7de6b802006-12-22 01:09:33 -0800836 page_remove_rmap(page, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 page_cache_release(page);
838
839out_unmap:
Hugh Dickinsc0718802005-10-29 18:16:31 -0700840 pte_unmap_unlock(pte, ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841out:
842 return ret;
843}
844
845/*
846 * objrmap doesn't work for nonlinear VMAs because the assumption that
847 * offset-into-file correlates with offset-into-virtual-addresses does not hold.
848 * Consequently, given a particular page and its ->index, we cannot locate the
849 * ptes which are mapping that page without an exhaustive linear search.
850 *
851 * So what this code does is a mini "virtual scan" of each nonlinear VMA which
852 * maps the file to which the target page belongs. The ->vm_private_data field
853 * holds the current cursor into that scan. Successive searches will circulate
854 * around the vma's virtual address space.
855 *
856 * So as more replacement pressure is applied to the pages in a nonlinear VMA,
857 * more scanning pressure is placed against them as well. Eventually pages
858 * will become fully unmapped and are eligible for eviction.
859 *
860 * For very sparsely populated VMAs this is a little inefficient - chances are
861 * there there won't be many ptes located within the scan cluster. In this case
862 * maybe we could scan further - to the end of the pte page, perhaps.
Nick Pigginb291f002008-10-18 20:26:44 -0700863 *
864 * Mlocked pages: check VM_LOCKED under mmap_sem held for read, if we can
865 * acquire it without blocking. If vma locked, mlock the pages in the cluster,
866 * rather than unmapping them. If we encounter the "check_page" that vmscan is
867 * trying to unmap, return SWAP_MLOCK, else default SWAP_AGAIN.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 */
869#define CLUSTER_SIZE min(32*PAGE_SIZE, PMD_SIZE)
870#define CLUSTER_MASK (~(CLUSTER_SIZE - 1))
871
Nick Pigginb291f002008-10-18 20:26:44 -0700872static int try_to_unmap_cluster(unsigned long cursor, unsigned int *mapcount,
873 struct vm_area_struct *vma, struct page *check_page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
875 struct mm_struct *mm = vma->vm_mm;
876 pgd_t *pgd;
877 pud_t *pud;
878 pmd_t *pmd;
Hugh Dickinsc0718802005-10-29 18:16:31 -0700879 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 pte_t pteval;
Hugh Dickinsc0718802005-10-29 18:16:31 -0700881 spinlock_t *ptl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 struct page *page;
883 unsigned long address;
884 unsigned long end;
Nick Pigginb291f002008-10-18 20:26:44 -0700885 int ret = SWAP_AGAIN;
886 int locked_vma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 address = (vma->vm_start + cursor) & CLUSTER_MASK;
889 end = address + CLUSTER_SIZE;
890 if (address < vma->vm_start)
891 address = vma->vm_start;
892 if (end > vma->vm_end)
893 end = vma->vm_end;
894
895 pgd = pgd_offset(mm, address);
896 if (!pgd_present(*pgd))
Nick Pigginb291f002008-10-18 20:26:44 -0700897 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 pud = pud_offset(pgd, address);
900 if (!pud_present(*pud))
Nick Pigginb291f002008-10-18 20:26:44 -0700901 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 pmd = pmd_offset(pud, address);
904 if (!pmd_present(*pmd))
Nick Pigginb291f002008-10-18 20:26:44 -0700905 return ret;
906
907 /*
908 * MLOCK_PAGES => feature is configured.
909 * if we can acquire the mmap_sem for read, and vma is VM_LOCKED,
910 * keep the sem while scanning the cluster for mlocking pages.
911 */
912 if (MLOCK_PAGES && down_read_trylock(&vma->vm_mm->mmap_sem)) {
913 locked_vma = (vma->vm_flags & VM_LOCKED);
914 if (!locked_vma)
915 up_read(&vma->vm_mm->mmap_sem); /* don't need it */
916 }
Hugh Dickinsc0718802005-10-29 18:16:31 -0700917
918 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Hugh Dickins365e9c872005-10-29 18:16:18 -0700920 /* Update high watermark before we lower rss */
921 update_hiwater_rss(mm);
922
Hugh Dickinsc0718802005-10-29 18:16:31 -0700923 for (; address < end; pte++, address += PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (!pte_present(*pte))
925 continue;
Linus Torvalds6aab3412005-11-28 14:34:23 -0800926 page = vm_normal_page(vma, address, *pte);
927 BUG_ON(!page || PageAnon(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Nick Pigginb291f002008-10-18 20:26:44 -0700929 if (locked_vma) {
930 mlock_vma_page(page); /* no-op if already mlocked */
931 if (page == check_page)
932 ret = SWAP_MLOCK;
933 continue; /* don't unmap */
934 }
935
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700936 if (ptep_clear_flush_young_notify(vma, address, pte))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 continue;
938
939 /* Nuke the page table entry. */
Ben Collinseca35132005-11-29 11:45:26 -0800940 flush_cache_page(vma, address, pte_pfn(*pte));
Andrea Arcangelicddb8a52008-07-28 15:46:29 -0700941 pteval = ptep_clear_flush_notify(vma, address, pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 /* If nonlinear, store the file page offset in the pte. */
944 if (page->index != linear_page_index(vma, address))
945 set_pte_at(mm, address, pte, pgoff_to_pte(page->index));
946
947 /* Move the dirty bit to the physical page now the pte is gone. */
948 if (pte_dirty(pteval))
949 set_page_dirty(page);
950
Nick Piggin7de6b802006-12-22 01:09:33 -0800951 page_remove_rmap(page, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 page_cache_release(page);
Hugh Dickins42946212005-10-29 18:16:05 -0700953 dec_mm_counter(mm, file_rss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 (*mapcount)--;
955 }
Hugh Dickinsc0718802005-10-29 18:16:31 -0700956 pte_unmap_unlock(pte - 1, ptl);
Nick Pigginb291f002008-10-18 20:26:44 -0700957 if (locked_vma)
958 up_read(&vma->vm_mm->mmap_sem);
959 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
961
Nick Pigginb291f002008-10-18 20:26:44 -0700962/*
963 * common handling for pages mapped in VM_LOCKED vmas
964 */
965static int try_to_mlock_page(struct page *page, struct vm_area_struct *vma)
966{
967 int mlocked = 0;
968
969 if (down_read_trylock(&vma->vm_mm->mmap_sem)) {
970 if (vma->vm_flags & VM_LOCKED) {
971 mlock_vma_page(page);
972 mlocked++; /* really mlocked the page */
973 }
974 up_read(&vma->vm_mm->mmap_sem);
975 }
976 return mlocked;
977}
978
979/**
980 * try_to_unmap_anon - unmap or unlock anonymous page using the object-based
981 * rmap method
982 * @page: the page to unmap/unlock
983 * @unlock: request for unlock rather than unmap [unlikely]
984 * @migration: unmapping for migration - ignored if @unlock
985 *
986 * Find all the mappings of a page using the mapping pointer and the vma chains
987 * contained in the anon_vma struct it points to.
988 *
989 * This function is only called from try_to_unmap/try_to_munlock for
990 * anonymous pages.
991 * When called from try_to_munlock(), the mmap_sem of the mm containing the vma
992 * where the page was found will be held for write. So, we won't recheck
993 * vm_flags for that VMA. That should be OK, because that vma shouldn't be
994 * 'LOCKED.
995 */
996static int try_to_unmap_anon(struct page *page, int unlock, int migration)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998 struct anon_vma *anon_vma;
999 struct vm_area_struct *vma;
Nick Pigginb291f002008-10-18 20:26:44 -07001000 unsigned int mlocked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 int ret = SWAP_AGAIN;
1002
Nick Pigginb291f002008-10-18 20:26:44 -07001003 if (MLOCK_PAGES && unlikely(unlock))
1004 ret = SWAP_SUCCESS; /* default for try_to_munlock() */
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 anon_vma = page_lock_anon_vma(page);
1007 if (!anon_vma)
1008 return ret;
1009
1010 list_for_each_entry(vma, &anon_vma->head, anon_vma_node) {
Nick Pigginb291f002008-10-18 20:26:44 -07001011 if (MLOCK_PAGES && unlikely(unlock)) {
1012 if (!((vma->vm_flags & VM_LOCKED) &&
1013 page_mapped_in_vma(page, vma)))
1014 continue; /* must visit all unlocked vmas */
1015 ret = SWAP_MLOCK; /* saw at least one mlocked vma */
1016 } else {
1017 ret = try_to_unmap_one(page, vma, migration);
1018 if (ret == SWAP_FAIL || !page_mapped(page))
1019 break;
1020 }
1021 if (ret == SWAP_MLOCK) {
1022 mlocked = try_to_mlock_page(page, vma);
1023 if (mlocked)
1024 break; /* stop if actually mlocked page */
1025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
Oleg Nesterov34bbd702007-02-28 20:13:49 -08001027
1028 page_unlock_anon_vma(anon_vma);
Nick Pigginb291f002008-10-18 20:26:44 -07001029
1030 if (mlocked)
1031 ret = SWAP_MLOCK; /* actually mlocked the page */
1032 else if (ret == SWAP_MLOCK)
1033 ret = SWAP_AGAIN; /* saw VM_LOCKED vma */
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 return ret;
1036}
1037
1038/**
Nick Pigginb291f002008-10-18 20:26:44 -07001039 * try_to_unmap_file - unmap/unlock file page using the object-based rmap method
1040 * @page: the page to unmap/unlock
1041 * @unlock: request for unlock rather than unmap [unlikely]
1042 * @migration: unmapping for migration - ignored if @unlock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 *
1044 * Find all the mappings of a page using the mapping pointer and the vma chains
1045 * contained in the address_space struct it points to.
1046 *
Nick Pigginb291f002008-10-18 20:26:44 -07001047 * This function is only called from try_to_unmap/try_to_munlock for
1048 * object-based pages.
1049 * When called from try_to_munlock(), the mmap_sem of the mm containing the vma
1050 * where the page was found will be held for write. So, we won't recheck
1051 * vm_flags for that VMA. That should be OK, because that vma shouldn't be
1052 * 'LOCKED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 */
Nick Pigginb291f002008-10-18 20:26:44 -07001054static int try_to_unmap_file(struct page *page, int unlock, int migration)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
1056 struct address_space *mapping = page->mapping;
1057 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
1058 struct vm_area_struct *vma;
1059 struct prio_tree_iter iter;
1060 int ret = SWAP_AGAIN;
1061 unsigned long cursor;
1062 unsigned long max_nl_cursor = 0;
1063 unsigned long max_nl_size = 0;
1064 unsigned int mapcount;
Nick Pigginb291f002008-10-18 20:26:44 -07001065 unsigned int mlocked = 0;
1066
1067 if (MLOCK_PAGES && unlikely(unlock))
1068 ret = SWAP_SUCCESS; /* default for try_to_munlock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 spin_lock(&mapping->i_mmap_lock);
1071 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
Nick Pigginb291f002008-10-18 20:26:44 -07001072 if (MLOCK_PAGES && unlikely(unlock)) {
1073 if (!(vma->vm_flags & VM_LOCKED))
1074 continue; /* must visit all vmas */
1075 ret = SWAP_MLOCK;
1076 } else {
1077 ret = try_to_unmap_one(page, vma, migration);
1078 if (ret == SWAP_FAIL || !page_mapped(page))
1079 goto out;
1080 }
1081 if (ret == SWAP_MLOCK) {
1082 mlocked = try_to_mlock_page(page, vma);
1083 if (mlocked)
1084 break; /* stop if actually mlocked page */
1085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 }
1087
Nick Pigginb291f002008-10-18 20:26:44 -07001088 if (mlocked)
1089 goto out;
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (list_empty(&mapping->i_mmap_nonlinear))
1092 goto out;
1093
1094 list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
1095 shared.vm_set.list) {
Nick Pigginb291f002008-10-18 20:26:44 -07001096 if (MLOCK_PAGES && unlikely(unlock)) {
1097 if (!(vma->vm_flags & VM_LOCKED))
1098 continue; /* must visit all vmas */
1099 ret = SWAP_MLOCK; /* leave mlocked == 0 */
1100 goto out; /* no need to look further */
1101 }
1102 if (!MLOCK_PAGES && !migration && (vma->vm_flags & VM_LOCKED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 continue;
1104 cursor = (unsigned long) vma->vm_private_data;
1105 if (cursor > max_nl_cursor)
1106 max_nl_cursor = cursor;
1107 cursor = vma->vm_end - vma->vm_start;
1108 if (cursor > max_nl_size)
1109 max_nl_size = cursor;
1110 }
1111
Nick Pigginb291f002008-10-18 20:26:44 -07001112 if (max_nl_size == 0) { /* all nonlinears locked or reserved ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 ret = SWAP_FAIL;
1114 goto out;
1115 }
1116
1117 /*
1118 * We don't try to search for this page in the nonlinear vmas,
1119 * and page_referenced wouldn't have found it anyway. Instead
1120 * just walk the nonlinear vmas trying to age and unmap some.
1121 * The mapcount of the page we came in with is irrelevant,
1122 * but even so use it as a guide to how hard we should try?
1123 */
1124 mapcount = page_mapcount(page);
1125 if (!mapcount)
1126 goto out;
1127 cond_resched_lock(&mapping->i_mmap_lock);
1128
1129 max_nl_size = (max_nl_size + CLUSTER_SIZE - 1) & CLUSTER_MASK;
1130 if (max_nl_cursor == 0)
1131 max_nl_cursor = CLUSTER_SIZE;
1132
1133 do {
1134 list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
1135 shared.vm_set.list) {
Nick Pigginb291f002008-10-18 20:26:44 -07001136 if (!MLOCK_PAGES && !migration &&
1137 (vma->vm_flags & VM_LOCKED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 continue;
1139 cursor = (unsigned long) vma->vm_private_data;
Hugh Dickins839b9682005-09-03 15:54:43 -07001140 while ( cursor < max_nl_cursor &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 cursor < vma->vm_end - vma->vm_start) {
Nick Pigginb291f002008-10-18 20:26:44 -07001142 ret = try_to_unmap_cluster(cursor, &mapcount,
1143 vma, page);
1144 if (ret == SWAP_MLOCK)
1145 mlocked = 2; /* to return below */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 cursor += CLUSTER_SIZE;
1147 vma->vm_private_data = (void *) cursor;
1148 if ((int)mapcount <= 0)
1149 goto out;
1150 }
1151 vma->vm_private_data = (void *) max_nl_cursor;
1152 }
1153 cond_resched_lock(&mapping->i_mmap_lock);
1154 max_nl_cursor += CLUSTER_SIZE;
1155 } while (max_nl_cursor <= max_nl_size);
1156
1157 /*
1158 * Don't loop forever (perhaps all the remaining pages are
1159 * in locked vmas). Reset cursor on all unreserved nonlinear
1160 * vmas, now forgetting on which ones it had fallen behind.
1161 */
Hugh Dickins101d2be2005-11-21 21:32:16 -08001162 list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
1163 vma->vm_private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164out:
1165 spin_unlock(&mapping->i_mmap_lock);
Nick Pigginb291f002008-10-18 20:26:44 -07001166 if (mlocked)
1167 ret = SWAP_MLOCK; /* actually mlocked the page */
1168 else if (ret == SWAP_MLOCK)
1169 ret = SWAP_AGAIN; /* saw VM_LOCKED vma */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return ret;
1171}
1172
1173/**
1174 * try_to_unmap - try to remove all page table mappings to a page
1175 * @page: the page to get unmapped
Randy Dunlap43d8eac2008-03-19 17:00:43 -07001176 * @migration: migration flag
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 *
1178 * Tries to remove all the page table entries which are mapping this
1179 * page, used in the pageout path. Caller must hold the page lock.
1180 * Return values are:
1181 *
1182 * SWAP_SUCCESS - we succeeded in removing all mappings
1183 * SWAP_AGAIN - we missed a mapping, try again later
1184 * SWAP_FAIL - the page is unswappable
Nick Pigginb291f002008-10-18 20:26:44 -07001185 * SWAP_MLOCK - page is mlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 */
Christoph Lameter73523492006-06-23 02:03:27 -07001187int try_to_unmap(struct page *page, int migration)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
1189 int ret;
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 BUG_ON(!PageLocked(page));
1192
1193 if (PageAnon(page))
Nick Pigginb291f002008-10-18 20:26:44 -07001194 ret = try_to_unmap_anon(page, 0, migration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 else
Nick Pigginb291f002008-10-18 20:26:44 -07001196 ret = try_to_unmap_file(page, 0, migration);
1197 if (ret != SWAP_MLOCK && !page_mapped(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 ret = SWAP_SUCCESS;
1199 return ret;
1200}
Nikita Danilov81b40822005-05-01 08:58:36 -07001201
Nick Pigginb291f002008-10-18 20:26:44 -07001202#ifdef CONFIG_UNEVICTABLE_LRU
1203/**
1204 * try_to_munlock - try to munlock a page
1205 * @page: the page to be munlocked
1206 *
1207 * Called from munlock code. Checks all of the VMAs mapping the page
1208 * to make sure nobody else has this page mlocked. The page will be
1209 * returned with PG_mlocked cleared if no other vmas have it mlocked.
1210 *
1211 * Return values are:
1212 *
1213 * SWAP_SUCCESS - no vma's holding page mlocked.
1214 * SWAP_AGAIN - page mapped in mlocked vma -- couldn't acquire mmap sem
1215 * SWAP_MLOCK - page is now mlocked.
1216 */
1217int try_to_munlock(struct page *page)
1218{
1219 VM_BUG_ON(!PageLocked(page) || PageLRU(page));
1220
1221 if (PageAnon(page))
1222 return try_to_unmap_anon(page, 1, 0);
1223 else
1224 return try_to_unmap_file(page, 1, 0);
1225}
1226#endif