]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/mm/mincore.c | |
3 | * | |
2f77d107 | 4 | * Copyright (C) 1994-2006 Linus Torvalds |
1da177e4 LT |
5 | */ |
6 | ||
7 | /* | |
8 | * The mincore() system call. | |
9 | */ | |
10 | #include <linux/slab.h> | |
11 | #include <linux/pagemap.h> | |
12 | #include <linux/mm.h> | |
13 | #include <linux/mman.h> | |
14 | #include <linux/syscalls.h> | |
15 | ||
16 | #include <asm/uaccess.h> | |
17 | #include <asm/pgtable.h> | |
18 | ||
19 | /* | |
20 | * Later we can get more picky about what "in core" means precisely. | |
21 | * For now, simply check to see if the page is in the page cache, | |
22 | * 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. | |
24 | */ | |
25 | static unsigned char mincore_page(struct vm_area_struct * vma, | |
26 | unsigned long pgoff) | |
27 | { | |
28 | unsigned char present = 0; | |
29 | struct address_space * as = vma->vm_file->f_mapping; | |
30 | struct page * page; | |
31 | ||
32 | page = find_get_page(as, pgoff); | |
33 | if (page) { | |
34 | present = PageUptodate(page); | |
35 | page_cache_release(page); | |
36 | } | |
37 | ||
38 | return present; | |
39 | } | |
40 | ||
2f77d107 LT |
41 | /* |
42 | * Do a chunk of "sys_mincore()". We've already checked | |
43 | * all the arguments, we hold the mmap semaphore: we should | |
44 | * just return the amount of info we're asked for. | |
45 | */ | |
46 | static long do_mincore(unsigned long addr, unsigned char *vec, unsigned long pages) | |
1da177e4 | 47 | { |
2f77d107 LT |
48 | unsigned long i, nr, pgoff; |
49 | struct vm_area_struct *vma = find_vma(current->mm, addr); | |
1da177e4 | 50 | |
2f77d107 LT |
51 | /* |
52 | * find_vma() didn't find anything: the address | |
53 | * is above everything we have mapped. | |
54 | */ | |
55 | if (!vma) { | |
56 | memset(vec, 0, pages); | |
57 | return pages; | |
58 | } | |
1da177e4 | 59 | |
2f77d107 LT |
60 | /* |
61 | * find_vma() found something, but we might be | |
62 | * below it: check for that. | |
63 | */ | |
64 | if (addr < vma->vm_start) { | |
65 | unsigned long gap = (vma->vm_start - addr) >> PAGE_SHIFT; | |
66 | if (gap > pages) | |
67 | gap = pages; | |
68 | memset(vec, 0, gap); | |
69 | return gap; | |
70 | } | |
1da177e4 | 71 | |
2f77d107 LT |
72 | /* |
73 | * Ok, got it. But check whether it's a segment we support | |
74 | * mincore() on. Right now, we don't do any anonymous mappings. | |
75 | */ | |
76 | if (!vma->vm_file) | |
77 | return -ENOMEM; | |
1da177e4 | 78 | |
2f77d107 LT |
79 | /* |
80 | * Calculate how many pages there are left in the vma, and | |
81 | * what the pgoff is for our address. | |
82 | */ | |
83 | nr = (vma->vm_end - addr) >> PAGE_SHIFT; | |
84 | if (nr > pages) | |
85 | nr = pages; | |
1da177e4 | 86 | |
2f77d107 LT |
87 | pgoff = (addr - vma->vm_start) >> PAGE_SHIFT; |
88 | pgoff += vma->vm_pgoff; | |
1da177e4 | 89 | |
2f77d107 LT |
90 | /* And then we just fill the sucker in.. */ |
91 | for (i = 0 ; i < nr; i++, pgoff++) | |
92 | vec[i] = mincore_page(vma, pgoff); | |
1da177e4 | 93 | |
2f77d107 | 94 | return nr; |
1da177e4 LT |
95 | } |
96 | ||
97 | /* | |
98 | * The mincore(2) system call. | |
99 | * | |
100 | * mincore() returns the memory residency status of the pages in the | |
101 | * current process's address space specified by [addr, addr + len). | |
102 | * The status is returned in a vector of bytes. The least significant | |
103 | * bit of each byte is 1 if the referenced page is in memory, otherwise | |
104 | * it is zero. | |
105 | * | |
106 | * Because the status of a page can change after mincore() checks it | |
107 | * but before it returns to the application, the returned vector may | |
108 | * contain stale information. Only locked pages are guaranteed to | |
109 | * remain in memory. | |
110 | * | |
111 | * return values: | |
112 | * zero - success | |
113 | * -EFAULT - vec points to an illegal address | |
114 | * -EINVAL - addr is not a multiple of PAGE_CACHE_SIZE | |
115 | * -ENOMEM - Addresses in the range [addr, addr + len] are | |
116 | * invalid for the address space of this process, or | |
117 | * specify one or more pages which are not currently | |
118 | * mapped | |
119 | * -EAGAIN - A kernel resource was temporarily unavailable. | |
120 | */ | |
121 | asmlinkage long sys_mincore(unsigned long start, size_t len, | |
122 | unsigned char __user * vec) | |
123 | { | |
2f77d107 LT |
124 | long retval; |
125 | unsigned long pages; | |
126 | unsigned char *tmp; | |
1da177e4 | 127 | |
2f77d107 LT |
128 | /* Check the start address: needs to be page-aligned.. */ |
129 | if (start & ~PAGE_CACHE_MASK) | |
130 | return -EINVAL; | |
1da177e4 | 131 | |
2f77d107 LT |
132 | /* ..and we need to be passed a valid user-space range */ |
133 | if (!access_ok(VERIFY_READ, (void __user *) start, len)) | |
134 | return -ENOMEM; | |
1da177e4 | 135 | |
2f77d107 LT |
136 | /* This also avoids any overflows on PAGE_CACHE_ALIGN */ |
137 | pages = len >> PAGE_SHIFT; | |
138 | pages += (len & ~PAGE_MASK) != 0; | |
1da177e4 | 139 | |
2f77d107 LT |
140 | if (!access_ok(VERIFY_WRITE, vec, pages)) |
141 | return -EFAULT; | |
1da177e4 | 142 | |
2f77d107 LT |
143 | tmp = (void *) __get_free_page(GFP_USER); |
144 | if (!tmp) | |
145 | return -ENOMEM; | |
146 | ||
147 | retval = 0; | |
148 | while (pages) { | |
149 | /* | |
150 | * Do at most PAGE_SIZE entries per iteration, due to | |
151 | * the temporary buffer size. | |
152 | */ | |
153 | down_read(¤t->mm->mmap_sem); | |
154 | retval = do_mincore(start, tmp, max(pages, PAGE_SIZE)); | |
155 | up_read(¤t->mm->mmap_sem); | |
156 | ||
157 | if (retval <= 0) | |
158 | break; | |
159 | if (copy_to_user(vec, tmp, retval)) { | |
160 | retval = -EFAULT; | |
161 | break; | |
1da177e4 | 162 | } |
2f77d107 LT |
163 | pages -= retval; |
164 | vec += retval; | |
165 | start += retval << PAGE_SHIFT; | |
166 | retval = 0; | |
1da177e4 | 167 | } |
2f77d107 LT |
168 | free_page((unsigned long) tmp); |
169 | return retval; | |
1da177e4 | 170 | } |