Blame view

mm/mincore.c 6.65 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
  /*
   *	linux/mm/mincore.c
   *
2f77d1070   Linus Torvalds   Fix incorrect use...
4
   * Copyright (C) 1994-2006  Linus Torvalds
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
5
6
7
8
9
   */
  
  /*
   * The mincore() system call.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
10
  #include <linux/pagemap.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
11
  #include <linux/gfp.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
12
13
14
  #include <linux/mm.h>
  #include <linux/mman.h>
  #include <linux/syscalls.h>
42da9cbd3   Nick Piggin   [PATCH] mm: minco...
15
16
  #include <linux/swap.h>
  #include <linux/swapops.h>
4f16fc107   Naoya Horiguchi   mm: hugetlb: fix ...
17
  #include <linux/hugetlb.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
19
20
  
  #include <asm/uaccess.h>
  #include <asm/pgtable.h>
1e25a271c   Naoya Horiguchi   mincore: apply pa...
21
22
  static int mincore_hugetlb(pte_t *pte, unsigned long hmask, unsigned long addr,
  			unsigned long end, struct mm_walk *walk)
f48840107   Johannes Weiner   mincore: break do...
23
24
  {
  #ifdef CONFIG_HUGETLB_PAGE
1e25a271c   Naoya Horiguchi   mincore: apply pa...
25
26
  	unsigned char present;
  	unsigned char *vec = walk->private;
f48840107   Johannes Weiner   mincore: break do...
27

1e25a271c   Naoya Horiguchi   mincore: apply pa...
28
29
30
31
32
33
34
35
  	/*
  	 * 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   Johannes Weiner   mincore: break do...
36
37
38
  #else
  	BUG();
  #endif
1e25a271c   Naoya Horiguchi   mincore: apply pa...
39
  	return 0;
f48840107   Johannes Weiner   mincore: break do...
40
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
42
43
44
45
46
  /*
   * 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.
   */
42da9cbd3   Nick Piggin   [PATCH] mm: minco...
47
  static unsigned char mincore_page(struct address_space *mapping, pgoff_t pgoff)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
48
49
  {
  	unsigned char present = 0;
42da9cbd3   Nick Piggin   [PATCH] mm: minco...
50
  	struct page *page;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51

42da9cbd3   Nick Piggin   [PATCH] mm: minco...
52
53
54
55
  	/*
  	 * 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
3c18ddd16   Nick Piggin   mm: remove nopage
56
  	 * tmpfs's .fault). So swapped out tmpfs mappings are tested here.
42da9cbd3   Nick Piggin   [PATCH] mm: minco...
57
  	 */
31475dd61   Hugh Dickins   mm: a few small u...
58
  #ifdef CONFIG_SWAP
0cd6144aa   Johannes Weiner   mm + fs: prepare ...
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  	if (shmem_mapping(mapping)) {
  		page = find_get_entry(mapping, pgoff);
  		/*
  		 * shmem/tmpfs may return swap: account for swapcache
  		 * page too.
  		 */
  		if (radix_tree_exceptional_entry(page)) {
  			swp_entry_t swp = radix_to_swp_entry(page);
  			page = find_get_page(swap_address_space(swp), swp.val);
  		}
  	} else
  		page = find_get_page(mapping, pgoff);
  #else
  	page = find_get_page(mapping, pgoff);
31475dd61   Hugh Dickins   mm: a few small u...
73
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
74
75
76
77
78
79
80
  	if (page) {
  		present = PageUptodate(page);
  		page_cache_release(page);
  	}
  
  	return present;
  }
1e25a271c   Naoya Horiguchi   mincore: apply pa...
81
82
  static int __mincore_unmapped_range(unsigned long addr, unsigned long end,
  				struct vm_area_struct *vma, unsigned char *vec)
f48840107   Johannes Weiner   mincore: break do...
83
  {
25ef0e50c   Johannes Weiner   mincore: pass ran...
84
  	unsigned long nr = (end - addr) >> PAGE_SHIFT;
f48840107   Johannes Weiner   mincore: break do...
85
86
87
88
89
90
91
92
93
94
95
96
  	int i;
  
  	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;
  	}
1e25a271c   Naoya Horiguchi   mincore: apply pa...
97
98
99
100
101
102
103
104
105
  	return nr;
  }
  
  static int mincore_unmapped_range(unsigned long addr, unsigned long end,
  				   struct mm_walk *walk)
  {
  	walk->private += __mincore_unmapped_range(addr, end,
  						  walk->vma, walk->private);
  	return 0;
f48840107   Johannes Weiner   mincore: break do...
106
  }
1e25a271c   Naoya Horiguchi   mincore: apply pa...
107
108
  static int mincore_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
  			struct mm_walk *walk)
f48840107   Johannes Weiner   mincore: break do...
109
110
  {
  	spinlock_t *ptl;
1e25a271c   Naoya Horiguchi   mincore: apply pa...
111
  	struct vm_area_struct *vma = walk->vma;
f48840107   Johannes Weiner   mincore: break do...
112
  	pte_t *ptep;
1e25a271c   Naoya Horiguchi   mincore: apply pa...
113
114
115
116
117
118
119
120
  	unsigned char *vec = walk->private;
  	int nr = (end - addr) >> PAGE_SHIFT;
  
  	if (pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
  		memset(vec, 1, nr);
  		spin_unlock(ptl);
  		goto out;
  	}
f48840107   Johannes Weiner   mincore: break do...
121

1e25a271c   Naoya Horiguchi   mincore: apply pa...
122
123
124
125
126
127
128
  	if (pmd_trans_unstable(pmd)) {
  		__mincore_unmapped_range(addr, end, vma, vec);
  		goto out;
  	}
  
  	ptep = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
  	for (; addr != end; ptep++, addr += PAGE_SIZE) {
f48840107   Johannes Weiner   mincore: break do...
129
  		pte_t pte = *ptep;
f48840107   Johannes Weiner   mincore: break do...
130
131
  
  		if (pte_none(pte))
1e25a271c   Naoya Horiguchi   mincore: apply pa...
132
133
  			__mincore_unmapped_range(addr, addr + PAGE_SIZE,
  						 vma, vec);
f48840107   Johannes Weiner   mincore: break do...
134
  		else if (pte_present(pte))
25ef0e50c   Johannes Weiner   mincore: pass ran...
135
  			*vec = 1;
0661a3361   Kirill A. Shutemov   mm: remove rest u...
136
  		else { /* pte is a swap entry */
f48840107   Johannes Weiner   mincore: break do...
137
  			swp_entry_t entry = pte_to_swp_entry(pte);
c313dc5de   Weijie Yang   mm: mincore: add ...
138
139
140
141
142
  			if (non_swap_entry(entry)) {
  				/*
  				 * migration or hwpoison entries are always
  				 * uptodate
  				 */
25ef0e50c   Johannes Weiner   mincore: pass ran...
143
  				*vec = 1;
f48840107   Johannes Weiner   mincore: break do...
144
145
  			} else {
  #ifdef CONFIG_SWAP
33806f06d   Shaohua Li   swap: make each s...
146
  				*vec = mincore_page(swap_address_space(entry),
0661a3361   Kirill A. Shutemov   mm: remove rest u...
147
  					entry.val);
f48840107   Johannes Weiner   mincore: break do...
148
149
  #else
  				WARN_ON(1);
25ef0e50c   Johannes Weiner   mincore: pass ran...
150
  				*vec = 1;
f48840107   Johannes Weiner   mincore: break do...
151
152
153
  #endif
  			}
  		}
25ef0e50c   Johannes Weiner   mincore: pass ran...
154
  		vec++;
1e25a271c   Naoya Horiguchi   mincore: apply pa...
155
  	}
f48840107   Johannes Weiner   mincore: break do...
156
  	pte_unmap_unlock(ptep - 1, ptl);
1e25a271c   Naoya Horiguchi   mincore: apply pa...
157
158
159
160
  out:
  	walk->private += nr;
  	cond_resched();
  	return 0;
e48293fd7   Johannes Weiner   mincore: do neste...
161
  }
2f77d1070   Linus Torvalds   Fix incorrect use...
162
163
164
165
166
  /*
   * 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   Johannes Weiner   mincore: cleanups
167
  static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *vec)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
168
  {
6a60f1b35   Johannes Weiner   mincore: cleanups
169
  	struct vm_area_struct *vma;
25ef0e50c   Johannes Weiner   mincore: pass ran...
170
  	unsigned long end;
1e25a271c   Naoya Horiguchi   mincore: apply pa...
171
172
173
174
175
176
177
  	int err;
  	struct mm_walk mincore_walk = {
  		.pmd_entry = mincore_pte_range,
  		.pte_hole = mincore_unmapped_range,
  		.hugetlb_entry = mincore_hugetlb,
  		.private = vec,
  	};
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
178

6a60f1b35   Johannes Weiner   mincore: cleanups
179
  	vma = find_vma(current->mm, addr);
4fb23e439   Linus Torvalds   Fix up mm/mincore...
180
181
  	if (!vma || addr < vma->vm_start)
  		return -ENOMEM;
1e25a271c   Naoya Horiguchi   mincore: apply pa...
182
  	mincore_walk.mm = vma->vm_mm;
25ef0e50c   Johannes Weiner   mincore: pass ran...
183
  	end = min(vma->vm_end, addr + (pages << PAGE_SHIFT));
1e25a271c   Naoya Horiguchi   mincore: apply pa...
184
185
186
  	err = walk_page_range(addr, end, &mincore_walk);
  	if (err < 0)
  		return err;
25ef0e50c   Johannes Weiner   mincore: pass ran...
187
  	return (end - addr) >> PAGE_SHIFT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
  }
  
  /*
   * 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
   *  -EINVAL - addr is not a multiple of PAGE_CACHE_SIZE
   *  -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   Heiko Carstens   [CVE-2009-0029] S...
214
215
  SYSCALL_DEFINE3(mincore, unsigned long, start, size_t, len,
  		unsigned char __user *, vec)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
216
  {
2f77d1070   Linus Torvalds   Fix incorrect use...
217
218
219
  	long retval;
  	unsigned long pages;
  	unsigned char *tmp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
220

2f77d1070   Linus Torvalds   Fix incorrect use...
221
222
223
  	/* Check the start address: needs to be page-aligned.. */
   	if (start & ~PAGE_CACHE_MASK)
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
224

2f77d1070   Linus Torvalds   Fix incorrect use...
225
226
227
  	/* ..and we need to be passed a valid user-space range */
  	if (!access_ok(VERIFY_READ, (void __user *) start, len))
  		return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
228

2f77d1070   Linus Torvalds   Fix incorrect use...
229
230
231
  	/* This also avoids any overflows on PAGE_CACHE_ALIGN */
  	pages = len >> PAGE_SHIFT;
  	pages += (len & ~PAGE_MASK) != 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
232

2f77d1070   Linus Torvalds   Fix incorrect use...
233
234
  	if (!access_ok(VERIFY_WRITE, vec, pages))
  		return -EFAULT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
235

2f77d1070   Linus Torvalds   Fix incorrect use...
236
237
  	tmp = (void *) __get_free_page(GFP_USER);
  	if (!tmp)
4fb23e439   Linus Torvalds   Fix up mm/mincore...
238
  		return -EAGAIN;
2f77d1070   Linus Torvalds   Fix incorrect use...
239
240
241
242
243
244
245
246
  
  	retval = 0;
  	while (pages) {
  		/*
  		 * Do at most PAGE_SIZE entries per iteration, due to
  		 * the temporary buffer size.
  		 */
  		down_read(&current->mm->mmap_sem);
6a60f1b35   Johannes Weiner   mincore: cleanups
247
  		retval = do_mincore(start, min(pages, PAGE_SIZE), tmp);
2f77d1070   Linus Torvalds   Fix incorrect use...
248
249
250
251
252
253
254
  		up_read(&current->mm->mmap_sem);
  
  		if (retval <= 0)
  			break;
  		if (copy_to_user(vec, tmp, retval)) {
  			retval = -EFAULT;
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
255
  		}
2f77d1070   Linus Torvalds   Fix incorrect use...
256
257
258
259
  		pages -= retval;
  		vec += retval;
  		start += retval << PAGE_SHIFT;
  		retval = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
260
  	}
2f77d1070   Linus Torvalds   Fix incorrect use...
261
262
  	free_page((unsigned long) tmp);
  	return retval;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
263
  }