fremap.c 6.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7
/*
 *   linux/mm/fremap.c
 * 
 * Explicit pagetable population and nonlinear (random) mappings support.
 *
 * started by Ingo Molnar, Copyright (C) 2002, 2003
 */
8
#include <linux/export.h>
A
Alexey Dobriyan 已提交
9
#include <linux/backing-dev.h>
L
Linus Torvalds 已提交
10 11 12 13 14 15 16 17
#include <linux/mm.h>
#include <linux/swap.h>
#include <linux/file.h>
#include <linux/mman.h>
#include <linux/pagemap.h>
#include <linux/swapops.h>
#include <linux/rmap.h>
#include <linux/syscalls.h>
A
Andrea Arcangeli 已提交
18
#include <linux/mmu_notifier.h>
L
Linus Torvalds 已提交
19 20 21 22 23

#include <asm/mmu_context.h>
#include <asm/cacheflush.h>
#include <asm/tlbflush.h>

24 25
#include "internal.h"

N
Nick Piggin 已提交
26
static void zap_pte(struct mm_struct *mm, struct vm_area_struct *vma,
L
Linus Torvalds 已提交
27 28 29 30 31
			unsigned long addr, pte_t *ptep)
{
	pte_t pte = *ptep;

	if (pte_present(pte)) {
N
Nick Piggin 已提交
32 33
		struct page *page;

34
		flush_cache_page(vma, addr, pte_pfn(pte));
L
Linus Torvalds 已提交
35
		pte = ptep_clear_flush(vma, addr, ptep);
36 37 38 39
		page = vm_normal_page(vma, addr, pte);
		if (page) {
			if (pte_dirty(pte))
				set_page_dirty(page);
40
			page_remove_rmap(page);
41
			page_cache_release(page);
N
Nick Piggin 已提交
42
			update_hiwater_rss(mm);
K
KAMEZAWA Hiroyuki 已提交
43
			dec_mm_counter(mm, MM_FILEPAGES);
L
Linus Torvalds 已提交
44 45 46 47
		}
	} else {
		if (!pte_file(pte))
			free_swap_and_cache(pte_to_swp_entry(pte));
48
		pte_clear_not_present_full(mm, addr, ptep, 0);
L
Linus Torvalds 已提交
49 50 51 52 53 54 55
	}
}

/*
 * Install a file pte to a given virtual memory address, release any
 * previously existing mapping.
 */
N
Nick Piggin 已提交
56
static int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma,
L
Linus Torvalds 已提交
57 58 59 60
		unsigned long addr, unsigned long pgoff, pgprot_t prot)
{
	int err = -ENOMEM;
	pte_t *pte;
H
Hugh Dickins 已提交
61
	spinlock_t *ptl;
L
Linus Torvalds 已提交
62

63
	pte = get_locked_pte(mm, addr, &ptl);
L
Linus Torvalds 已提交
64
	if (!pte)
H
Hugh Dickins 已提交
65
		goto out;
L
Linus Torvalds 已提交
66

N
Nick Piggin 已提交
67 68
	if (!pte_none(*pte))
		zap_pte(mm, vma, addr, pte);
L
Linus Torvalds 已提交
69 70

	set_pte_at(mm, addr, pte, pgoff_to_pte(pgoff));
71 72 73 74 75 76 77
	/*
	 * We don't need to run update_mmu_cache() here because the "file pte"
	 * being installed by install_file_pte() is not a real pte - it's a
	 * non-present entry (like a swap entry), noting what file offset should
	 * be mapped there when there's a fault (in a non-linear vma where
	 * that's not obvious).
	 */
H
Hugh Dickins 已提交
78 79 80
	pte_unmap_unlock(pte, ptl);
	err = 0;
out:
L
Linus Torvalds 已提交
81 82 83
	return err;
}

84 85
int generic_file_remap_pages(struct vm_area_struct *vma, unsigned long addr,
			     unsigned long size, pgoff_t pgoff)
86
{
87
	struct mm_struct *mm = vma->vm_mm;
88 89 90 91 92 93 94 95 96 97 98 99
	int err;

	do {
		err = install_file_pte(mm, vma, addr, pgoff, vma->vm_page_prot);
		if (err)
			return err;

		size -= PAGE_SIZE;
		addr += PAGE_SIZE;
		pgoff++;
	} while (size);

100
	return 0;
101
}
102
EXPORT_SYMBOL(generic_file_remap_pages);
103

104 105
/**
 * sys_remap_file_pages - remap arbitrary pages of an existing VM_SHARED vma
L
Linus Torvalds 已提交
106 107
 * @start: start of the remapped virtual memory range
 * @size: size of the remapped virtual memory range
108 109
 * @prot: new protection bits of the range (see NOTE)
 * @pgoff: to-be-mapped page of the backing store file
L
Linus Torvalds 已提交
110 111
 * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO.
 *
112 113 114 115
 * sys_remap_file_pages remaps arbitrary pages of an existing VM_SHARED vma
 * (shared backing store file).
 *
 * This syscall works purely via pagetables, so it's the most efficient
L
Linus Torvalds 已提交
116 117 118 119
 * way to map the same (large) file into a given virtual window. Unlike
 * mmap()/mremap() it does not create any new vmas. The new mappings are
 * also safe across swapout.
 *
120
 * NOTE: the @prot parameter right now is ignored (but must be zero),
121 122
 * and the vma's default protection is used. Arbitrary protections
 * might be implemented in the future.
L
Linus Torvalds 已提交
123
 */
124 125
SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
		unsigned long, prot, unsigned long, pgoff, unsigned long, flags)
L
Linus Torvalds 已提交
126 127 128 129 130 131
{
	struct mm_struct *mm = current->mm;
	struct address_space *mapping;
	struct vm_area_struct *vma;
	int err = -EINVAL;
	int has_write_lock = 0;
132
	vm_flags_t vm_flags;
L
Linus Torvalds 已提交
133

134
	if (prot)
L
Linus Torvalds 已提交
135 136 137 138 139 140 141 142 143 144 145
		return err;
	/*
	 * Sanitize the syscall parameters:
	 */
	start = start & PAGE_MASK;
	size = size & PAGE_MASK;

	/* Does the address range wrap, or is the span zero-sized? */
	if (start + size <= start)
		return err;

146 147 148 149
	/* Does pgoff wrap? */
	if (pgoff + (size >> PAGE_SHIFT) < pgoff)
		return err;

L
Linus Torvalds 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163
	/* Can we represent this offset inside this architecture's pte's? */
#if PTE_FILE_MAX_BITS < BITS_PER_LONG
	if (pgoff + (size >> PAGE_SHIFT) >= (1UL << PTE_FILE_MAX_BITS))
		return err;
#endif

	/* We need down_write() to change vma->vm_flags. */
	down_read(&mm->mmap_sem);
 retry:
	vma = find_vma(mm, start);

	/*
	 * Make sure the vma is shared, that it supports prefaulting,
	 * and that the remapped range is valid and fully within
M
Michel Lespinasse 已提交
164
	 * the single existing vma.
L
Linus Torvalds 已提交
165
	 */
166 167 168
	if (!vma || !(vma->vm_flags & VM_SHARED))
		goto out;

169
	if (!vma->vm_ops || !vma->vm_ops->remap_pages)
170 171
		goto out;

172
	if (start < vma->vm_start || start + size > vma->vm_end)
173 174 175 176
		goto out;

	/* Must set VM_NONLINEAR before any pages are populated. */
	if (!(vma->vm_flags & VM_NONLINEAR)) {
M
Michel Lespinasse 已提交
177 178 179 180 181 182 183
		/*
		 * vm_private_data is used as a swapout cursor
		 * in a VM_NONLINEAR vma.
		 */
		if (vma->vm_private_data)
			goto out;

184 185 186 187 188 189 190
		/* Don't need a nonlinear mapping, exit success */
		if (pgoff == linear_page_index(vma, start)) {
			err = 0;
			goto out;
		}

		if (!has_write_lock) {
M
Michel Lespinasse 已提交
191
get_write_lock:
192 193 194 195 196 197
			up_read(&mm->mmap_sem);
			down_write(&mm->mmap_sem);
			has_write_lock = 1;
			goto retry;
		}
		mapping = vma->vm_file->f_mapping;
198 199 200 201 202 203 204
		/*
		 * page_mkclean doesn't work on nonlinear vmas, so if
		 * dirty pages need to be accounted, emulate with linear
		 * vmas.
		 */
		if (mapping_cap_account_dirty(mapping)) {
			unsigned long addr;
A
Al Viro 已提交
205
			struct file *file = get_file(vma->vm_file);
206

207
			addr = mmap_region(file, start, size,
208
					vma->vm_flags, pgoff);
209
			fput(file);
210 211 212 213 214 215 216 217
			if (IS_ERR_VALUE(addr)) {
				err = addr;
			} else {
				BUG_ON(addr != start);
				err = 0;
			}
			goto out;
		}
218
		mutex_lock(&mapping->i_mmap_mutex);
219 220
		flush_dcache_mmap_lock(mapping);
		vma->vm_flags |= VM_NONLINEAR;
221
		vma_interval_tree_remove(vma, &mapping->i_mmap);
222 223
		vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
		flush_dcache_mmap_unlock(mapping);
224
		mutex_unlock(&mapping->i_mmap_mutex);
225 226
	}

227 228 229 230
	if (vma->vm_flags & VM_LOCKED) {
		/*
		 * drop PG_Mlocked flag for over-mapped range
		 */
M
Michel Lespinasse 已提交
231 232
		if (!has_write_lock)
			goto get_write_lock;
233
		vm_flags = vma->vm_flags;
234
		munlock_vma_pages_range(vma, start, start + size);
235
		vma->vm_flags = vm_flags;
236 237
	}

A
Andrea Arcangeli 已提交
238
	mmu_notifier_invalidate_range_start(mm, start, start + size);
239
	err = vma->vm_ops->remap_pages(vma, start, size, pgoff);
A
Andrea Arcangeli 已提交
240
	mmu_notifier_invalidate_range_end(mm, start, start + size);
L
Linus Torvalds 已提交
241

242 243 244 245 246
	/*
	 * We can't clear VM_NONLINEAR because we'd have to do
	 * it after ->populate completes, and that would prevent
	 * downgrading the lock.  (Locks can't be upgraded).
	 */
L
Linus Torvalds 已提交
247

248
out:
249
	vm_flags = vma->vm_flags;
L
Linus Torvalds 已提交
250 251 252 253
	if (likely(!has_write_lock))
		up_read(&mm->mmap_sem);
	else
		up_write(&mm->mmap_sem);
254 255
	if (!err && ((vm_flags & VM_LOCKED) || !(flags & MAP_NONBLOCK)))
		mm_populate(start, size);
L
Linus Torvalds 已提交
256 257 258

	return err;
}