blob: 95bcb5641c722b6f4d7eb49801320e0a266f7c83 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/fremap.c
3 *
4 * Explicit pagetable population and nonlinear (random) mappings support.
5 *
6 * started by Ingo Molnar, Copyright (C) 2002, 2003
7 */
8
9#include <linux/mm.h>
10#include <linux/swap.h>
11#include <linux/file.h>
12#include <linux/mman.h>
13#include <linux/pagemap.h>
14#include <linux/swapops.h>
15#include <linux/rmap.h>
16#include <linux/module.h>
17#include <linux/syscalls.h>
18
19#include <asm/mmu_context.h>
20#include <asm/cacheflush.h>
21#include <asm/tlbflush.h>
22
Nick Piggind0217ac2007-07-19 01:47:03 -070023static void zap_pte(struct mm_struct *mm, struct vm_area_struct *vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 unsigned long addr, pte_t *ptep)
25{
26 pte_t pte = *ptep;
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 if (pte_present(pte)) {
Nick Piggind0217ac2007-07-19 01:47:03 -070029 struct page *page;
30
Linus Torvalds6aab3412005-11-28 14:34:23 -080031 flush_cache_page(vma, addr, pte_pfn(pte));
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 pte = ptep_clear_flush(vma, addr, ptep);
Linus Torvalds6aab3412005-11-28 14:34:23 -080033 page = vm_normal_page(vma, addr, pte);
34 if (page) {
35 if (pte_dirty(pte))
36 set_page_dirty(page);
Nick Piggin7de6b802006-12-22 01:09:33 -080037 page_remove_rmap(page, vma);
Linus Torvalds6aab3412005-11-28 14:34:23 -080038 page_cache_release(page);
Nick Piggind0217ac2007-07-19 01:47:03 -070039 update_hiwater_rss(mm);
40 dec_mm_counter(mm, file_rss);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 }
42 } else {
43 if (!pte_file(pte))
44 free_swap_and_cache(pte_to_swp_entry(pte));
Zachary Amsden9888a1c2006-09-30 23:29:31 -070045 pte_clear_not_present_full(mm, addr, ptep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 }
47}
48
49/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * Install a file pte to a given virtual memory address, release any
51 * previously existing mapping.
52 */
Nick Piggind0217ac2007-07-19 01:47:03 -070053static int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma,
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 unsigned long addr, unsigned long pgoff, pgprot_t prot)
55{
56 int err = -ENOMEM;
57 pte_t *pte;
Hugh Dickinsc74df322005-10-29 18:16:23 -070058 spinlock_t *ptl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvaldsc9cfcdd2005-11-29 14:03:14 -080060 pte = get_locked_pte(mm, addr, &ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 if (!pte)
Hugh Dickinsc74df322005-10-29 18:16:23 -070062 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Nick Piggind0217ac2007-07-19 01:47:03 -070064 if (!pte_none(*pte))
65 zap_pte(mm, vma, addr, pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 set_pte_at(mm, addr, pte, pgoff_to_pte(pgoff));
Hugh Dickins668e0d82006-06-23 02:03:45 -070068 /*
69 * We don't need to run update_mmu_cache() here because the "file pte"
70 * being installed by install_file_pte() is not a real pte - it's a
71 * non-present entry (like a swap entry), noting what file offset should
72 * be mapped there when there's a fault (in a non-linear vma where
73 * that's not obvious).
74 */
Hugh Dickinsc74df322005-10-29 18:16:23 -070075 pte_unmap_unlock(pte, ptl);
76 err = 0;
77out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return err;
79}
80
Nick Piggin54cb8822007-07-19 01:46:59 -070081static int populate_range(struct mm_struct *mm, struct vm_area_struct *vma,
82 unsigned long addr, unsigned long size, pgoff_t pgoff)
83{
84 int err;
85
86 do {
87 err = install_file_pte(mm, vma, addr, pgoff, vma->vm_page_prot);
88 if (err)
89 return err;
90
91 size -= PAGE_SIZE;
92 addr += PAGE_SIZE;
93 pgoff++;
94 } while (size);
95
96 return 0;
97
98}
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/***
101 * sys_remap_file_pages - remap arbitrary pages of a shared backing store
102 * file within an existing vma.
103 * @start: start of the remapped virtual memory range
104 * @size: size of the remapped virtual memory range
105 * @prot: new protection bits of the range
106 * @pgoff: to be mapped page of the backing store file
107 * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO.
108 *
109 * this syscall works purely via pagetables, so it's the most efficient
110 * way to map the same (large) file into a given virtual window. Unlike
111 * mmap()/mremap() it does not create any new vmas. The new mappings are
112 * also safe across swapout.
113 *
114 * NOTE: the 'prot' parameter right now is ignored, and the vma's default
115 * protection is used. Arbitrary protections might be implemented in the
116 * future.
117 */
118asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
119 unsigned long __prot, unsigned long pgoff, unsigned long flags)
120{
121 struct mm_struct *mm = current->mm;
122 struct address_space *mapping;
123 unsigned long end = start + size;
124 struct vm_area_struct *vma;
125 int err = -EINVAL;
126 int has_write_lock = 0;
127
128 if (__prot)
129 return err;
130 /*
131 * Sanitize the syscall parameters:
132 */
133 start = start & PAGE_MASK;
134 size = size & PAGE_MASK;
135
136 /* Does the address range wrap, or is the span zero-sized? */
137 if (start + size <= start)
138 return err;
139
140 /* Can we represent this offset inside this architecture's pte's? */
141#if PTE_FILE_MAX_BITS < BITS_PER_LONG
142 if (pgoff + (size >> PAGE_SHIFT) >= (1UL << PTE_FILE_MAX_BITS))
143 return err;
144#endif
145
146 /* We need down_write() to change vma->vm_flags. */
147 down_read(&mm->mmap_sem);
148 retry:
149 vma = find_vma(mm, start);
150
151 /*
152 * Make sure the vma is shared, that it supports prefaulting,
153 * and that the remapped range is valid and fully within
154 * the single existing vma. vm_private_data is used as a
Hugh Dickins101d2be2005-11-21 21:32:16 -0800155 * swapout cursor in a VM_NONLINEAR vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 */
Nick Piggin54cb8822007-07-19 01:46:59 -0700157 if (!vma || !(vma->vm_flags & VM_SHARED))
158 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Nick Piggin54cb8822007-07-19 01:46:59 -0700160 if (vma->vm_private_data && !(vma->vm_flags & VM_NONLINEAR))
161 goto out;
162
Yan Zhengdd204d62007-10-08 10:05:48 -0700163 if (!(vma->vm_flags & VM_CAN_NONLINEAR))
Nick Piggin54cb8822007-07-19 01:46:59 -0700164 goto out;
165
166 if (end <= start || start < vma->vm_start || end > vma->vm_end)
167 goto out;
168
169 /* Must set VM_NONLINEAR before any pages are populated. */
170 if (!(vma->vm_flags & VM_NONLINEAR)) {
171 /* Don't need a nonlinear mapping, exit success */
172 if (pgoff == linear_page_index(vma, start)) {
173 err = 0;
174 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176
Nick Piggin54cb8822007-07-19 01:46:59 -0700177 if (!has_write_lock) {
178 up_read(&mm->mmap_sem);
179 down_write(&mm->mmap_sem);
180 has_write_lock = 1;
181 goto retry;
182 }
183 mapping = vma->vm_file->f_mapping;
Miklos Szeredi3ee6daf2007-07-19 01:47:24 -0700184 /*
185 * page_mkclean doesn't work on nonlinear vmas, so if
186 * dirty pages need to be accounted, emulate with linear
187 * vmas.
188 */
189 if (mapping_cap_account_dirty(mapping)) {
190 unsigned long addr;
191
192 flags &= MAP_NONBLOCK;
193 addr = mmap_region(vma->vm_file, start, size,
194 flags, vma->vm_flags, pgoff, 1);
195 if (IS_ERR_VALUE(addr)) {
196 err = addr;
197 } else {
198 BUG_ON(addr != start);
199 err = 0;
200 }
201 goto out;
202 }
Nick Piggin54cb8822007-07-19 01:46:59 -0700203 spin_lock(&mapping->i_mmap_lock);
204 flush_dcache_mmap_lock(mapping);
205 vma->vm_flags |= VM_NONLINEAR;
206 vma_prio_tree_remove(vma, &mapping->i_mmap);
207 vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
208 flush_dcache_mmap_unlock(mapping);
209 spin_unlock(&mapping->i_mmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
Nick Piggin54cb8822007-07-19 01:46:59 -0700211
Nick Piggind0217ac2007-07-19 01:47:03 -0700212 err = populate_range(mm, vma, start, size, pgoff);
213 if (!err && !(flags & MAP_NONBLOCK)) {
214 if (unlikely(has_write_lock)) {
215 downgrade_write(&mm->mmap_sem);
216 has_write_lock = 0;
Nick Piggin54cb8822007-07-19 01:46:59 -0700217 }
Nick Piggind0217ac2007-07-19 01:47:03 -0700218 make_pages_present(start, start+size);
219 }
Nick Piggin54cb8822007-07-19 01:46:59 -0700220
221 /*
222 * We can't clear VM_NONLINEAR because we'd have to do
223 * it after ->populate completes, and that would prevent
224 * downgrading the lock. (Locks can't be upgraded).
225 */
226
227out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (likely(!has_write_lock))
229 up_read(&mm->mmap_sem);
230 else
231 up_write(&mm->mmap_sem);
232
233 return err;
234}
235