mincore.c 6.72 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
Linus Torvalds's avatar
Linus Torvalds committed
2 3 4
/*
 *	linux/mm/mincore.c
 *
5
 * Copyright (C) 1994-2006  Linus Torvalds
Linus Torvalds's avatar
Linus Torvalds committed
6 7 8 9 10 11
 */

/*
 * The mincore() system call.
 */
#include <linux/pagemap.h>
12
#include <linux/gfp.h>
Linus Torvalds's avatar
Linus Torvalds committed
13 14 15
#include <linux/mm.h>
#include <linux/mman.h>
#include <linux/syscalls.h>
Nick Piggin's avatar
Nick Piggin committed
16 17
#include <linux/swap.h>
#include <linux/swapops.h>
18
#include <linux/shmem_fs.h>
19
#include <linux/hugetlb.h>
Linus Torvalds's avatar
Linus Torvalds committed
20

21
#include <linux/uaccess.h>
Linus Torvalds's avatar
Linus Torvalds committed
22 23
#include <asm/pgtable.h>

24 25
static int mincore_hugetlb(pte_t *pte, unsigned long hmask, unsigned long addr,
			unsigned long end, struct mm_walk *walk)
26 27
{
#ifdef CONFIG_HUGETLB_PAGE
28 29
	unsigned char present;
	unsigned char *vec = walk->private;
30

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;
39 40 41
#else
	BUG();
#endif
42
	return 0;
43 44
}

Linus Torvalds's avatar
Linus Torvalds committed
45 46 47 48 49 50
/*
 * 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.
 */
Nick Piggin's avatar
Nick Piggin committed
51
static unsigned char mincore_page(struct address_space *mapping, pgoff_t pgoff)
Linus Torvalds's avatar
Linus Torvalds committed
52 53
{
	unsigned char present = 0;
Nick Piggin's avatar
Nick Piggin committed
54
	struct page *page;
Linus Torvalds's avatar
Linus Torvalds committed
55

Nick Piggin's avatar
Nick Piggin committed
56 57 58 59
	/*
	 * 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
Nick Piggin's avatar
Nick Piggin committed
60
	 * tmpfs's .fault). So swapped out tmpfs mappings are tested here.
Nick Piggin's avatar
Nick Piggin committed
61
	 */
62
#ifdef CONFIG_SWAP
63 64 65 66 67 68 69 70
	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);
71 72
			page = find_get_page(swap_address_space(swp),
					     swp_offset(swp));
73 74 75 76 77
		}
	} else
		page = find_get_page(mapping, pgoff);
#else
	page = find_get_page(mapping, pgoff);
78
#endif
Linus Torvalds's avatar
Linus Torvalds committed
79 80
	if (page) {
		present = PageUptodate(page);
81
		put_page(page);
Linus Torvalds's avatar
Linus Torvalds committed
82 83 84 85 86
	}

	return present;
}

87 88
static int __mincore_unmapped_range(unsigned long addr, unsigned long end,
				struct vm_area_struct *vma, unsigned char *vec)
89
{
90
	unsigned long nr = (end - addr) >> PAGE_SHIFT;
91 92 93 94 95 96 97 98 99 100 101 102
	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;
	}
103 104 105 106 107 108 109 110 111
	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;
112 113
}

114 115
static int mincore_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end,
			struct mm_walk *walk)
116 117
{
	spinlock_t *ptl;
118
	struct vm_area_struct *vma = walk->vma;
119
	pte_t *ptep;
120 121 122
	unsigned char *vec = walk->private;
	int nr = (end - addr) >> PAGE_SHIFT;

123 124
	ptl = pmd_trans_huge_lock(pmd, vma);
	if (ptl) {
125 126 127 128
		memset(vec, 1, nr);
		spin_unlock(ptl);
		goto out;
	}
129

130 131 132 133 134 135 136
	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) {
137 138 139
		pte_t pte = *ptep;

		if (pte_none(pte))
140 141
			__mincore_unmapped_range(addr, addr + PAGE_SIZE,
						 vma, vec);
142
		else if (pte_present(pte))
143
			*vec = 1;
144
		else { /* pte is a swap entry */
145 146
			swp_entry_t entry = pte_to_swp_entry(pte);

147 148 149 150 151
			if (non_swap_entry(entry)) {
				/*
				 * migration or hwpoison entries are always
				 * uptodate
				 */
152
				*vec = 1;
153 154
			} else {
#ifdef CONFIG_SWAP
155
				*vec = mincore_page(swap_address_space(entry),
156
						    swp_offset(entry));
157 158
#else
				WARN_ON(1);
159
				*vec = 1;
160 161 162
#endif
			}
		}
163
		vec++;
164
	}
165
	pte_unmap_unlock(ptep - 1, ptl);
166 167 168 169
out:
	walk->private += nr;
	cond_resched();
	return 0;
170 171
}

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.
 */
Johannes Weiner's avatar
Johannes Weiner committed
177
static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *vec)
Linus Torvalds's avatar
Linus Torvalds committed
178
{
Johannes Weiner's avatar
Johannes Weiner committed
179
	struct vm_area_struct *vma;
180
	unsigned long end;
181 182 183 184 185 186 187
	int err;
	struct mm_walk mincore_walk = {
		.pmd_entry = mincore_pte_range,
		.pte_hole = mincore_unmapped_range,
		.hugetlb_entry = mincore_hugetlb,
		.private = vec,
	};
Linus Torvalds's avatar
Linus Torvalds committed
188

Johannes Weiner's avatar
Johannes Weiner committed
189
	vma = find_vma(current->mm, addr);
190 191
	if (!vma || addr < vma->vm_start)
		return -ENOMEM;
192
	mincore_walk.mm = vma->vm_mm;
193
	end = min(vma->vm_end, addr + (pages << PAGE_SHIFT));
194 195 196
	err = walk_page_range(addr, end, &mincore_walk);
	if (err < 0)
		return err;
197
	return (end - addr) >> PAGE_SHIFT;
Linus Torvalds's avatar
Linus Torvalds committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
}

/*
 * 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
217
 *  -EINVAL - addr is not a multiple of PAGE_SIZE
Linus Torvalds's avatar
Linus Torvalds committed
218 219 220 221 222 223
 *  -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.
 */
224 225
SYSCALL_DEFINE3(mincore, unsigned long, start, size_t, len,
		unsigned char __user *, vec)
Linus Torvalds's avatar
Linus Torvalds committed
226
{
227 228 229
	long retval;
	unsigned long pages;
	unsigned char *tmp;
Linus Torvalds's avatar
Linus Torvalds committed
230

231
	/* Check the start address: needs to be page-aligned.. */
232
	if (start & ~PAGE_MASK)
233
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
234

235 236 237
	/* ..and we need to be passed a valid user-space range */
	if (!access_ok(VERIFY_READ, (void __user *) start, len))
		return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
238

239
	/* This also avoids any overflows on PAGE_ALIGN */
240
	pages = len >> PAGE_SHIFT;
241
	pages += (offset_in_page(len)) != 0;
Linus Torvalds's avatar
Linus Torvalds committed
242

243 244
	if (!access_ok(VERIFY_WRITE, vec, pages))
		return -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
245

246 247
	tmp = (void *) __get_free_page(GFP_USER);
	if (!tmp)
248
		return -EAGAIN;
249 250 251 252 253 254 255 256

	retval = 0;
	while (pages) {
		/*
		 * Do at most PAGE_SIZE entries per iteration, due to
		 * the temporary buffer size.
		 */
		down_read(&current->mm->mmap_sem);
Johannes Weiner's avatar
Johannes Weiner committed
257
		retval = do_mincore(start, min(pages, PAGE_SIZE), tmp);
258 259 260 261 262 263 264
		up_read(&current->mm->mmap_sem);

		if (retval <= 0)
			break;
		if (copy_to_user(vec, tmp, retval)) {
			retval = -EFAULT;
			break;
Linus Torvalds's avatar
Linus Torvalds committed
265
		}
266 267 268 269
		pages -= retval;
		vec += retval;
		start += retval << PAGE_SHIFT;
		retval = 0;
Linus Torvalds's avatar
Linus Torvalds committed
270
	}
271 272
	free_page((unsigned long) tmp);
	return retval;
Linus Torvalds's avatar
Linus Torvalds committed
273
}