aboutsummaryrefslogtreecommitdiffstats
path: root/mm/mincore.c
diff options
context:
space:
mode:
authorNick Piggin <npiggin@suse.de>2007-02-12 03:51:39 -0500
committerLinus Torvalds <torvalds@woody.linux-foundation.org>2007-02-12 12:48:27 -0500
commit42da9cbd3eedde33a42acc2cb06f454814cf5de0 (patch)
tree1f5adff6b09263326e6c47e96e1d30bc1438be63 /mm/mincore.c
parent22cd25ed31bbf849acaa06ab220dc4f526153f13 (diff)
[PATCH] mm: mincore anon
Make mincore work for anon mappings, nonlinear, and migration entries. Based on patch from Linus Torvalds <torvalds@linux-foundation.org>. Signed-off-by: Nick Piggin <npiggin@suse.de> Acked-by: Hugh Dickins <hugh@veritas.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'mm/mincore.c')
-rw-r--r--mm/mincore.c102
1 files changed, 76 insertions, 26 deletions
diff --git a/mm/mincore.c b/mm/mincore.c
index 8aca6f7167bb..95c5f49f0a1a 100644
--- a/mm/mincore.c
+++ b/mm/mincore.c
@@ -12,6 +12,8 @@
12#include <linux/mm.h> 12#include <linux/mm.h>
13#include <linux/mman.h> 13#include <linux/mman.h>
14#include <linux/syscalls.h> 14#include <linux/syscalls.h>
15#include <linux/swap.h>
16#include <linux/swapops.h>
15 17
16#include <asm/uaccess.h> 18#include <asm/uaccess.h>
17#include <asm/pgtable.h> 19#include <asm/pgtable.h>
@@ -22,14 +24,22 @@
22 * and is up to date; i.e. that no page-in operation would be required 24 * and is up to date; i.e. that no page-in operation would be required
23 * at this time if an application were to map and access this page. 25 * at this time if an application were to map and access this page.
24 */ 26 */
25static unsigned char mincore_page(struct vm_area_struct * vma, 27static unsigned char mincore_page(struct address_space *mapping, pgoff_t pgoff)
26 unsigned long pgoff)
27{ 28{
28 unsigned char present = 0; 29 unsigned char present = 0;
29 struct address_space * as = vma->vm_file->f_mapping; 30 struct page *page;
30 struct page * page;
31 31
32 page = find_get_page(as, pgoff); 32 /*
33 * When tmpfs swaps out a page from a file, any process mapping that
34 * file will not get a swp_entry_t in its pte, but rather it is like
35 * any other file mapping (ie. marked !present and faulted in with
36 * tmpfs's .nopage). So swapped out tmpfs mappings are tested here.
37 *
38 * However when tmpfs moves the page from pagecache and into swapcache,
39 * it is still in core, but the find_get_page below won't find it.
40 * No big deal, but make a note of it.
41 */
42 page = find_get_page(mapping, pgoff);
33 if (page) { 43 if (page) {
34 present = PageUptodate(page); 44 present = PageUptodate(page);
35 page_cache_release(page); 45 page_cache_release(page);
@@ -45,7 +55,14 @@ static unsigned char mincore_page(struct vm_area_struct * vma,
45 */ 55 */
46static long do_mincore(unsigned long addr, unsigned char *vec, unsigned long pages) 56static long do_mincore(unsigned long addr, unsigned char *vec, unsigned long pages)
47{ 57{
48 unsigned long i, nr, pgoff; 58 pgd_t *pgd;
59 pud_t *pud;
60 pmd_t *pmd;
61 pte_t *ptep;
62 spinlock_t *ptl;
63 unsigned long nr;
64 int i;
65 pgoff_t pgoff;
49 struct vm_area_struct *vma = find_vma(current->mm, addr); 66 struct vm_area_struct *vma = find_vma(current->mm, addr);
50 67
51 /* 68 /*
@@ -56,31 +73,64 @@ static long do_mincore(unsigned long addr, unsigned char *vec, unsigned long pag
56 return -ENOMEM; 73 return -ENOMEM;
57 74
58 /* 75 /*
59 * Ok, got it. But check whether it's a segment we support 76 * Calculate how many pages there are left in the last level of the
60 * mincore() on. Right now, we don't do any anonymous mappings. 77 * PTE array for our address.
61 *
62 * FIXME: This is just stupid. And returning ENOMEM is
63 * stupid too. We should just look at the page tables. But
64 * this is what we've traditionally done, so we'll just
65 * continue doing it.
66 */ 78 */
67 if (!vma->vm_file) 79 nr = PTRS_PER_PTE - ((addr >> PAGE_SHIFT) & (PTRS_PER_PTE-1));
68 return -ENOMEM;
69
70 /*
71 * Calculate how many pages there are left in the vma, and
72 * what the pgoff is for our address.
73 */
74 nr = (vma->vm_end - addr) >> PAGE_SHIFT;
75 if (nr > pages) 80 if (nr > pages)
76 nr = pages; 81 nr = pages;
77 82
78 pgoff = (addr - vma->vm_start) >> PAGE_SHIFT; 83 pgd = pgd_offset(vma->vm_mm, addr);
79 pgoff += vma->vm_pgoff; 84 if (pgd_none_or_clear_bad(pgd))
85 goto none_mapped;
86 pud = pud_offset(pgd, addr);
87 if (pud_none_or_clear_bad(pud))
88 goto none_mapped;
89 pmd = pmd_offset(pud, addr);
90 if (pmd_none_or_clear_bad(pmd))
91 goto none_mapped;
92
93 ptep = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
94 for (i = 0; i < nr; i++, ptep++, addr += PAGE_SIZE) {
95 unsigned char present;
96 pte_t pte = *ptep;
97
98 if (pte_present(pte)) {
99 present = 1;
100
101 } else if (pte_none(pte)) {
102 if (vma->vm_file) {
103 pgoff = linear_page_index(vma, addr);
104 present = mincore_page(vma->vm_file->f_mapping,
105 pgoff);
106 } else
107 present = 0;
108
109 } else if (pte_file(pte)) {
110 pgoff = pte_to_pgoff(pte);
111 present = mincore_page(vma->vm_file->f_mapping, pgoff);
112
113 } else { /* pte is a swap entry */
114 swp_entry_t entry = pte_to_swp_entry(pte);
115 if (is_migration_entry(entry)) {
116 /* migration entries are always uptodate */
117 present = 1;
118 } else {
119 pgoff = entry.val;
120 present = mincore_page(&swapper_space, pgoff);
121 }
122 }
123 }
124 pte_unmap_unlock(ptep-1, ptl);
125
126 return nr;
80 127
81 /* And then we just fill the sucker in.. */ 128none_mapped:
82 for (i = 0 ; i < nr; i++, pgoff++) 129 if (vma->vm_file) {
83 vec[i] = mincore_page(vma, pgoff); 130 pgoff = linear_page_index(vma, addr);
131 for (i = 0; i < nr; i++, pgoff++)
132 vec[i] = mincore_page(vma->vm_file->f_mapping, pgoff);
133 }
84 134
85 return nr; 135 return nr;
86} 136}