Blame view
mm/mincore.c
7 KB
b24413180 License cleanup: ... |
1 |
// SPDX-License-Identifier: GPL-2.0 |
1da177e4c Linux-2.6.12-rc2 |
2 3 4 |
/* * linux/mm/mincore.c * |
2f77d1070 Fix incorrect use... |
5 |
* Copyright (C) 1994-2006 Linus Torvalds |
1da177e4c Linux-2.6.12-rc2 |
6 7 8 9 10 |
*/ /* * The mincore() system call. */ |
1da177e4c Linux-2.6.12-rc2 |
11 |
#include <linux/pagemap.h> |
5a0e3ad6a include cleanup: ... |
12 |
#include <linux/gfp.h> |
a520110e4 mm: split out a n... |
13 |
#include <linux/pagewalk.h> |
1da177e4c Linux-2.6.12-rc2 |
14 15 |
#include <linux/mman.h> #include <linux/syscalls.h> |
42da9cbd3 [PATCH] mm: minco... |
16 17 |
#include <linux/swap.h> #include <linux/swapops.h> |
3a4f8a0b3 mm: remove shmem_... |
18 |
#include <linux/shmem_fs.h> |
4f16fc107 mm: hugetlb: fix ... |
19 |
#include <linux/hugetlb.h> |
65fddcfca mm: reorder inclu... |
20 |
#include <linux/pgtable.h> |
1da177e4c Linux-2.6.12-rc2 |
21 |
|
7c0f6ba68 Replace <asm/uacc... |
22 |
#include <linux/uaccess.h> |
1da177e4c Linux-2.6.12-rc2 |
23 |
|
1e25a271c mincore: apply pa... |
24 25 |
static int mincore_hugetlb(pte_t *pte, unsigned long hmask, unsigned long addr, unsigned long end, struct mm_walk *walk) |
f48840107 mincore: break do... |
26 27 |
{ #ifdef CONFIG_HUGETLB_PAGE |
1e25a271c mincore: apply pa... |
28 29 |
unsigned char present; unsigned char *vec = walk->private; |
f48840107 mincore: break do... |
30 |
|
1e25a271c mincore: apply pa... |
31 32 33 34 35 36 37 38 |
/* * Hugepages under user process are always in RAM and never * swapped out, but theoretically it needs to be checked. */ present = pte && !huge_pte_none(huge_ptep_get(pte)); for (; addr != end; vec++, addr += PAGE_SIZE) *vec = present; walk->private = vec; |
f48840107 mincore: break do... |
39 40 41 |
#else BUG(); #endif |
1e25a271c mincore: apply pa... |
42 |
return 0; |
f48840107 mincore: break do... |
43 |
} |
30bac164a Revert "Change mi... |
44 45 46 47 48 49 |
/* * Later we can get more picky about what "in core" means precisely. * For now, simply check to see if the page is in the page cache, * and is up to date; i.e. that no page-in operation would be required * at this time if an application were to map and access this page. */ |
61ef18655 mm: factor find_g... |
50 |
static unsigned char mincore_page(struct address_space *mapping, pgoff_t index) |
30bac164a Revert "Change mi... |
51 52 53 54 55 56 57 58 59 60 |
{ unsigned char present = 0; struct page *page; /* * When tmpfs swaps out a page from a file, any process mapping that * file will not get a swp_entry_t in its pte, but rather it is like * any other file mapping (ie. marked !present and faulted in with * tmpfs's .fault). So swapped out tmpfs mappings are tested here. */ |
61ef18655 mm: factor find_g... |
61 |
page = find_get_incore_page(mapping, index); |
30bac164a Revert "Change mi... |
62 63 64 65 66 67 68 69 70 71 |
if (page) { present = PageUptodate(page); put_page(page); } return present; } static int __mincore_unmapped_range(unsigned long addr, unsigned long end, struct vm_area_struct *vma, unsigned char *vec) |
1e25a271c mincore: apply pa... |
72 |
{ |
574823bfa Change mincore() ... |
73 |
unsigned long nr = (end - addr) >> PAGE_SHIFT; |
30bac164a Revert "Change mi... |
74 |
int i; |
574823bfa Change mincore() ... |
75 |
|
30bac164a Revert "Change mi... |
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
if (vma->vm_file) { pgoff_t pgoff; pgoff = linear_page_index(vma, addr); for (i = 0; i < nr; i++, pgoff++) vec[i] = mincore_page(vma->vm_file->f_mapping, pgoff); } else { for (i = 0; i < nr; i++) vec[i] = 0; } return nr; } static int mincore_unmapped_range(unsigned long addr, unsigned long end, |
b7a16c7ad mm: pagewalk: add... |
90 |
__always_unused int depth, |
30bac164a Revert "Change mi... |
91 92 93 94 |
struct mm_walk *walk) { walk->private += __mincore_unmapped_range(addr, end, walk->vma, walk->private); |
1e25a271c mincore: apply pa... |
95 |
return 0; |
f48840107 mincore: break do... |
96 |
} |
1e25a271c mincore: apply pa... |
97 98 |
static int mincore_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, struct mm_walk *walk) |
f48840107 mincore: break do... |
99 100 |
{ spinlock_t *ptl; |
1e25a271c mincore: apply pa... |
101 |
struct vm_area_struct *vma = walk->vma; |
f48840107 mincore: break do... |
102 |
pte_t *ptep; |
1e25a271c mincore: apply pa... |
103 104 |
unsigned char *vec = walk->private; int nr = (end - addr) >> PAGE_SHIFT; |
b6ec57f4b thp: change pmd_t... |
105 106 |
ptl = pmd_trans_huge_lock(pmd, vma); if (ptl) { |
1e25a271c mincore: apply pa... |
107 108 109 110 |
memset(vec, 1, nr); spin_unlock(ptl); goto out; } |
f48840107 mincore: break do... |
111 |
|
1e25a271c mincore: apply pa... |
112 |
if (pmd_trans_unstable(pmd)) { |
30bac164a Revert "Change mi... |
113 |
__mincore_unmapped_range(addr, end, vma, vec); |
1e25a271c mincore: apply pa... |
114 115 116 117 118 |
goto out; } ptep = pte_offset_map_lock(walk->mm, pmd, addr, &ptl); for (; addr != end; ptep++, addr += PAGE_SIZE) { |
f48840107 mincore: break do... |
119 |
pte_t pte = *ptep; |
f48840107 mincore: break do... |
120 121 |
if (pte_none(pte)) |
30bac164a Revert "Change mi... |
122 123 |
__mincore_unmapped_range(addr, addr + PAGE_SIZE, vma, vec); |
f48840107 mincore: break do... |
124 |
else if (pte_present(pte)) |
25ef0e50c mincore: pass ran... |
125 |
*vec = 1; |
0661a3361 mm: remove rest u... |
126 |
else { /* pte is a swap entry */ |
f48840107 mincore: break do... |
127 |
swp_entry_t entry = pte_to_swp_entry(pte); |
30bac164a Revert "Change mi... |
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
if (non_swap_entry(entry)) { /* * migration or hwpoison entries are always * uptodate */ *vec = 1; } else { #ifdef CONFIG_SWAP *vec = mincore_page(swap_address_space(entry), swp_offset(entry)); #else WARN_ON(1); *vec = 1; #endif } |
f48840107 mincore: break do... |
143 |
} |
25ef0e50c mincore: pass ran... |
144 |
vec++; |
1e25a271c mincore: apply pa... |
145 |
} |
f48840107 mincore: break do... |
146 |
pte_unmap_unlock(ptep - 1, ptl); |
1e25a271c mincore: apply pa... |
147 148 149 150 |
out: walk->private += nr; cond_resched(); return 0; |
e48293fd7 mincore: do neste... |
151 |
} |
134fca906 mm/mincore.c: mak... |
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
static inline bool can_do_mincore(struct vm_area_struct *vma) { if (vma_is_anonymous(vma)) return true; if (!vma->vm_file) return false; /* * Reveal pagecache information only for non-anonymous mappings that * correspond to the files the calling process could (if tried) open * for writing; otherwise we'd be including shared non-exclusive * mappings, which opens a side channel. */ return inode_owner_or_capable(file_inode(vma->vm_file)) || inode_permission(file_inode(vma->vm_file), MAY_WRITE) == 0; } |
7b86ac337 pagewalk: separat... |
167 168 169 170 171 |
static const struct mm_walk_ops mincore_walk_ops = { .pmd_entry = mincore_pte_range, .pte_hole = mincore_unmapped_range, .hugetlb_entry = mincore_hugetlb, }; |
2f77d1070 Fix incorrect use... |
172 173 174 175 176 |
/* * Do a chunk of "sys_mincore()". We've already checked * all the arguments, we hold the mmap semaphore: we should * just return the amount of info we're asked for. */ |
6a60f1b35 mincore: cleanups |
177 |
static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *vec) |
1da177e4c Linux-2.6.12-rc2 |
178 |
{ |
6a60f1b35 mincore: cleanups |
179 |
struct vm_area_struct *vma; |
25ef0e50c mincore: pass ran... |
180 |
unsigned long end; |
1e25a271c mincore: apply pa... |
181 |
int err; |
1da177e4c Linux-2.6.12-rc2 |
182 |
|
6a60f1b35 mincore: cleanups |
183 |
vma = find_vma(current->mm, addr); |
4fb23e439 Fix up mm/mincore... |
184 185 |
if (!vma || addr < vma->vm_start) return -ENOMEM; |
25ef0e50c mincore: pass ran... |
186 |
end = min(vma->vm_end, addr + (pages << PAGE_SHIFT)); |
134fca906 mm/mincore.c: mak... |
187 188 189 190 191 |
if (!can_do_mincore(vma)) { unsigned long pages = DIV_ROUND_UP(end - addr, PAGE_SIZE); memset(vec, 1, pages); return pages; } |
7b86ac337 pagewalk: separat... |
192 |
err = walk_page_range(vma->vm_mm, addr, end, &mincore_walk_ops, vec); |
1e25a271c mincore: apply pa... |
193 194 |
if (err < 0) return err; |
25ef0e50c mincore: pass ran... |
195 |
return (end - addr) >> PAGE_SHIFT; |
1da177e4c Linux-2.6.12-rc2 |
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
} /* * The mincore(2) system call. * * mincore() returns the memory residency status of the pages in the * current process's address space specified by [addr, addr + len). * The status is returned in a vector of bytes. The least significant * bit of each byte is 1 if the referenced page is in memory, otherwise * it is zero. * * Because the status of a page can change after mincore() checks it * but before it returns to the application, the returned vector may * contain stale information. Only locked pages are guaranteed to * remain in memory. * * return values: * zero - success * -EFAULT - vec points to an illegal address |
ea1754a08 mm, fs: remove re... |
215 |
* -EINVAL - addr is not a multiple of PAGE_SIZE |
1da177e4c Linux-2.6.12-rc2 |
216 217 218 219 220 221 |
* -ENOMEM - Addresses in the range [addr, addr + len] are * invalid for the address space of this process, or * specify one or more pages which are not currently * mapped * -EAGAIN - A kernel resource was temporarily unavailable. */ |
3480b2574 [CVE-2009-0029] S... |
222 223 |
SYSCALL_DEFINE3(mincore, unsigned long, start, size_t, len, unsigned char __user *, vec) |
1da177e4c Linux-2.6.12-rc2 |
224 |
{ |
2f77d1070 Fix incorrect use... |
225 226 227 |
long retval; unsigned long pages; unsigned char *tmp; |
1da177e4c Linux-2.6.12-rc2 |
228 |
|
057d33891 mm: untag user po... |
229 |
start = untagged_addr(start); |
2f77d1070 Fix incorrect use... |
230 |
/* Check the start address: needs to be page-aligned.. */ |
09cbfeaf1 mm, fs: get rid o... |
231 |
if (start & ~PAGE_MASK) |
2f77d1070 Fix incorrect use... |
232 |
return -EINVAL; |
1da177e4c Linux-2.6.12-rc2 |
233 |
|
2f77d1070 Fix incorrect use... |
234 |
/* ..and we need to be passed a valid user-space range */ |
96d4f267e Remove 'type' arg... |
235 |
if (!access_ok((void __user *) start, len)) |
2f77d1070 Fix incorrect use... |
236 |
return -ENOMEM; |
1da177e4c Linux-2.6.12-rc2 |
237 |
|
ea1754a08 mm, fs: remove re... |
238 |
/* This also avoids any overflows on PAGE_ALIGN */ |
2f77d1070 Fix incorrect use... |
239 |
pages = len >> PAGE_SHIFT; |
e7bbdd071 mm/mincore: use o... |
240 |
pages += (offset_in_page(len)) != 0; |
1da177e4c Linux-2.6.12-rc2 |
241 |
|
96d4f267e Remove 'type' arg... |
242 |
if (!access_ok(vec, pages)) |
2f77d1070 Fix incorrect use... |
243 |
return -EFAULT; |
1da177e4c Linux-2.6.12-rc2 |
244 |
|
2f77d1070 Fix incorrect use... |
245 246 |
tmp = (void *) __get_free_page(GFP_USER); if (!tmp) |
4fb23e439 Fix up mm/mincore... |
247 |
return -EAGAIN; |
2f77d1070 Fix incorrect use... |
248 249 250 251 252 253 254 |
retval = 0; while (pages) { /* * Do at most PAGE_SIZE entries per iteration, due to * the temporary buffer size. */ |
d8ed45c5d mmap locking API:... |
255 |
mmap_read_lock(current->mm); |
6a60f1b35 mincore: cleanups |
256 |
retval = do_mincore(start, min(pages, PAGE_SIZE), tmp); |
d8ed45c5d mmap locking API:... |
257 |
mmap_read_unlock(current->mm); |
2f77d1070 Fix incorrect use... |
258 259 260 261 262 263 |
if (retval <= 0) break; if (copy_to_user(vec, tmp, retval)) { retval = -EFAULT; break; |
1da177e4c Linux-2.6.12-rc2 |
264 |
} |
2f77d1070 Fix incorrect use... |
265 266 267 268 |
pages -= retval; vec += retval; start += retval << PAGE_SHIFT; retval = 0; |
1da177e4c Linux-2.6.12-rc2 |
269 |
} |
2f77d1070 Fix incorrect use... |
270 271 |
free_page((unsigned long) tmp); return retval; |
1da177e4c Linux-2.6.12-rc2 |
272 |
} |