mincore: pass ranges as start,end address pairs
[safe/jmp/linux-2.6] / mm / mincore.c
1 /*
2  *      linux/mm/mincore.c
3  *
4  * Copyright (C) 1994-2006  Linus Torvalds
5  */
6
7 /*
8  * The mincore() system call.
9  */
10 #include <linux/pagemap.h>
11 #include <linux/gfp.h>
12 #include <linux/mm.h>
13 #include <linux/mman.h>
14 #include <linux/syscalls.h>
15 #include <linux/swap.h>
16 #include <linux/swapops.h>
17 #include <linux/hugetlb.h>
18
19 #include <asm/uaccess.h>
20 #include <asm/pgtable.h>
21
22 static void mincore_hugetlb_page_range(struct vm_area_struct *vma,
23                                 unsigned long addr, unsigned long end,
24                                 unsigned char *vec)
25 {
26 #ifdef CONFIG_HUGETLB_PAGE
27         struct hstate *h;
28
29         h = hstate_vma(vma);
30         while (1) {
31                 unsigned char present;
32                 pte_t *ptep;
33                 /*
34                  * Huge pages are always in RAM for now, but
35                  * theoretically it needs to be checked.
36                  */
37                 ptep = huge_pte_offset(current->mm,
38                                        addr & huge_page_mask(h));
39                 present = ptep && !huge_pte_none(huge_ptep_get(ptep));
40                 while (1) {
41                         *vec = present;
42                         vec++;
43                         addr += PAGE_SIZE;
44                         if (addr == end)
45                                 return;
46                         /* check hugepage border */
47                         if (!(addr & ~huge_page_mask(h)))
48                                 break;
49                 }
50         }
51 #else
52         BUG();
53 #endif
54 }
55
56 /*
57  * Later we can get more picky about what "in core" means precisely.
58  * For now, simply check to see if the page is in the page cache,
59  * and is up to date; i.e. that no page-in operation would be required
60  * at this time if an application were to map and access this page.
61  */
62 static unsigned char mincore_page(struct address_space *mapping, pgoff_t pgoff)
63 {
64         unsigned char present = 0;
65         struct page *page;
66
67         /*
68          * When tmpfs swaps out a page from a file, any process mapping that
69          * file will not get a swp_entry_t in its pte, but rather it is like
70          * any other file mapping (ie. marked !present and faulted in with
71          * tmpfs's .fault). So swapped out tmpfs mappings are tested here.
72          *
73          * However when tmpfs moves the page from pagecache and into swapcache,
74          * it is still in core, but the find_get_page below won't find it.
75          * No big deal, but make a note of it.
76          */
77         page = find_get_page(mapping, pgoff);
78         if (page) {
79                 present = PageUptodate(page);
80                 page_cache_release(page);
81         }
82
83         return present;
84 }
85
86 static void mincore_unmapped_range(struct vm_area_struct *vma,
87                                 unsigned long addr, unsigned long end,
88                                 unsigned char *vec)
89 {
90         unsigned long nr = (end - addr) >> PAGE_SHIFT;
91         int i;
92
93         if (vma->vm_file) {
94                 pgoff_t pgoff;
95
96                 pgoff = linear_page_index(vma, addr);
97                 for (i = 0; i < nr; i++, pgoff++)
98                         vec[i] = mincore_page(vma->vm_file->f_mapping, pgoff);
99         } else {
100                 for (i = 0; i < nr; i++)
101                         vec[i] = 0;
102         }
103 }
104
105 static void mincore_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
106                         unsigned long addr, unsigned long end,
107                         unsigned char *vec)
108 {
109         unsigned long next;
110         spinlock_t *ptl;
111         pte_t *ptep;
112
113         ptep = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
114         do {
115                 pte_t pte = *ptep;
116                 pgoff_t pgoff;
117
118                 next = addr + PAGE_SIZE;
119                 if (pte_none(pte))
120                         mincore_unmapped_range(vma, addr, next, vec);
121                 else if (pte_present(pte))
122                         *vec = 1;
123                 else if (pte_file(pte)) {
124                         pgoff = pte_to_pgoff(pte);
125                         *vec = mincore_page(vma->vm_file->f_mapping, pgoff);
126                 } else { /* pte is a swap entry */
127                         swp_entry_t entry = pte_to_swp_entry(pte);
128
129                         if (is_migration_entry(entry)) {
130                                 /* migration entries are always uptodate */
131                                 *vec = 1;
132                         } else {
133 #ifdef CONFIG_SWAP
134                                 pgoff = entry.val;
135                                 *vec = mincore_page(&swapper_space, pgoff);
136 #else
137                                 WARN_ON(1);
138                                 *vec = 1;
139 #endif
140                         }
141                 }
142                 vec++;
143         } while (ptep++, addr = next, addr != end);
144         pte_unmap_unlock(ptep - 1, ptl);
145 }
146
147 /*
148  * Do a chunk of "sys_mincore()". We've already checked
149  * all the arguments, we hold the mmap semaphore: we should
150  * just return the amount of info we're asked for.
151  */
152 static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *vec)
153 {
154         pgd_t *pgd;
155         pud_t *pud;
156         pmd_t *pmd;
157         struct vm_area_struct *vma;
158         unsigned long end;
159
160         vma = find_vma(current->mm, addr);
161         if (!vma || addr < vma->vm_start)
162                 return -ENOMEM;
163
164         end = min(vma->vm_end, addr + (pages << PAGE_SHIFT));
165
166         if (is_vm_hugetlb_page(vma)) {
167                 mincore_hugetlb_page_range(vma, addr, end, vec);
168                 return (end - addr) >> PAGE_SHIFT;
169         }
170
171         end = pmd_addr_end(addr, end);
172
173         pgd = pgd_offset(vma->vm_mm, addr);
174         if (pgd_none_or_clear_bad(pgd))
175                 goto none_mapped;
176         pud = pud_offset(pgd, addr);
177         if (pud_none_or_clear_bad(pud))
178                 goto none_mapped;
179         pmd = pmd_offset(pud, addr);
180         if (pmd_none_or_clear_bad(pmd))
181                 goto none_mapped;
182
183         mincore_pte_range(vma, pmd, addr, end, vec);
184         return (end - addr) >> PAGE_SHIFT;
185
186 none_mapped:
187         mincore_unmapped_range(vma, addr, end, vec);
188         return (end - addr) >> PAGE_SHIFT;
189 }
190
191 /*
192  * The mincore(2) system call.
193  *
194  * mincore() returns the memory residency status of the pages in the
195  * current process's address space specified by [addr, addr + len).
196  * The status is returned in a vector of bytes.  The least significant
197  * bit of each byte is 1 if the referenced page is in memory, otherwise
198  * it is zero.
199  *
200  * Because the status of a page can change after mincore() checks it
201  * but before it returns to the application, the returned vector may
202  * contain stale information.  Only locked pages are guaranteed to
203  * remain in memory.
204  *
205  * return values:
206  *  zero    - success
207  *  -EFAULT - vec points to an illegal address
208  *  -EINVAL - addr is not a multiple of PAGE_CACHE_SIZE
209  *  -ENOMEM - Addresses in the range [addr, addr + len] are
210  *              invalid for the address space of this process, or
211  *              specify one or more pages which are not currently
212  *              mapped
213  *  -EAGAIN - A kernel resource was temporarily unavailable.
214  */
215 SYSCALL_DEFINE3(mincore, unsigned long, start, size_t, len,
216                 unsigned char __user *, vec)
217 {
218         long retval;
219         unsigned long pages;
220         unsigned char *tmp;
221
222         /* Check the start address: needs to be page-aligned.. */
223         if (start & ~PAGE_CACHE_MASK)
224                 return -EINVAL;
225
226         /* ..and we need to be passed a valid user-space range */
227         if (!access_ok(VERIFY_READ, (void __user *) start, len))
228                 return -ENOMEM;
229
230         /* This also avoids any overflows on PAGE_CACHE_ALIGN */
231         pages = len >> PAGE_SHIFT;
232         pages += (len & ~PAGE_MASK) != 0;
233
234         if (!access_ok(VERIFY_WRITE, vec, pages))
235                 return -EFAULT;
236
237         tmp = (void *) __get_free_page(GFP_USER);
238         if (!tmp)
239                 return -EAGAIN;
240
241         retval = 0;
242         while (pages) {
243                 /*
244                  * Do at most PAGE_SIZE entries per iteration, due to
245                  * the temporary buffer size.
246                  */
247                 down_read(&current->mm->mmap_sem);
248                 retval = do_mincore(start, min(pages, PAGE_SIZE), tmp);
249                 up_read(&current->mm->mmap_sem);
250
251                 if (retval <= 0)
252                         break;
253                 if (copy_to_user(vec, tmp, retval)) {
254                         retval = -EFAULT;
255                         break;
256                 }
257                 pages -= retval;
258                 vec += retval;
259                 start += retval << PAGE_SHIFT;
260                 retval = 0;
261         }
262         free_page((unsigned long) tmp);
263         return retval;
264 }