blob: 7f08d10ceaff4d037f5a37a5b629d89452a2dab0 [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
Hugh Dickins861f2fb2005-10-29 18:16:17 -070023static int 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;
Hugh Dickins861f2fb2005-10-29 18:16:17 -070027 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 if (pte_present(pte)) {
30 unsigned long pfn = pte_pfn(pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 flush_cache_page(vma, addr, pfn);
32 pte = ptep_clear_flush(vma, addr, ptep);
Nick Pigginb5810032005-10-29 18:16:12 -070033 if (unlikely(!pfn_valid(pfn))) {
34 print_bad_pte(vma, pte, addr);
Hugh Dickins861f2fb2005-10-29 18:16:17 -070035 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 }
Nick Pigginb5810032005-10-29 18:16:12 -070037 page = pfn_to_page(pfn);
38 if (pte_dirty(pte))
39 set_page_dirty(page);
40 page_remove_rmap(page);
41 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 } else {
43 if (!pte_file(pte))
44 free_swap_and_cache(pte_to_swp_entry(pte));
45 pte_clear(mm, addr, ptep);
46 }
Hugh Dickins861f2fb2005-10-29 18:16:17 -070047out:
48 return !!page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
51/*
52 * Install a file page to a given virtual memory address, release any
53 * previously existing mapping.
54 */
55int install_page(struct mm_struct *mm, struct vm_area_struct *vma,
56 unsigned long addr, struct page *page, pgprot_t prot)
57{
58 struct inode *inode;
59 pgoff_t size;
60 int err = -ENOMEM;
61 pte_t *pte;
62 pmd_t *pmd;
63 pud_t *pud;
64 pgd_t *pgd;
65 pte_t pte_val;
66
Nick Pigginb5810032005-10-29 18:16:12 -070067 BUG_ON(vma->vm_flags & VM_RESERVED);
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 pgd = pgd_offset(mm, addr);
70 spin_lock(&mm->page_table_lock);
71
72 pud = pud_alloc(mm, pgd, addr);
73 if (!pud)
74 goto err_unlock;
75
76 pmd = pmd_alloc(mm, pud, addr);
77 if (!pmd)
78 goto err_unlock;
79
80 pte = pte_alloc_map(mm, pmd, addr);
81 if (!pte)
82 goto err_unlock;
83
84 /*
85 * This page may have been truncated. Tell the
86 * caller about it.
87 */
88 err = -EINVAL;
89 inode = vma->vm_file->f_mapping->host;
90 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
91 if (!page->mapping || page->index >= size)
92 goto err_unlock;
Hugh Dickinsf5154a92005-10-11 19:16:26 +010093 err = -ENOMEM;
94 if (page_mapcount(page) > INT_MAX/2)
95 goto err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Hugh Dickins861f2fb2005-10-29 18:16:17 -070097 if (pte_none(*pte) || !zap_pte(mm, vma, addr, pte))
98 inc_mm_counter(mm, file_rss);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 flush_icache_page(vma, page);
101 set_pte_at(mm, addr, pte, mk_pte(page, prot));
102 page_add_file_rmap(page);
103 pte_val = *pte;
104 pte_unmap(pte);
105 update_mmu_cache(vma, addr, pte_val);
106
107 err = 0;
108err_unlock:
109 spin_unlock(&mm->page_table_lock);
110 return err;
111}
112EXPORT_SYMBOL(install_page);
113
114
115/*
116 * Install a file pte to a given virtual memory address, release any
117 * previously existing mapping.
118 */
119int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma,
120 unsigned long addr, unsigned long pgoff, pgprot_t prot)
121{
122 int err = -ENOMEM;
123 pte_t *pte;
124 pmd_t *pmd;
125 pud_t *pud;
126 pgd_t *pgd;
127 pte_t pte_val;
128
Nick Pigginb5810032005-10-29 18:16:12 -0700129 BUG_ON(vma->vm_flags & VM_RESERVED);
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 pgd = pgd_offset(mm, addr);
132 spin_lock(&mm->page_table_lock);
133
134 pud = pud_alloc(mm, pgd, addr);
135 if (!pud)
136 goto err_unlock;
137
138 pmd = pmd_alloc(mm, pud, addr);
139 if (!pmd)
140 goto err_unlock;
141
142 pte = pte_alloc_map(mm, pmd, addr);
143 if (!pte)
144 goto err_unlock;
145
Hugh Dickins861f2fb2005-10-29 18:16:17 -0700146 if (!pte_none(*pte) && zap_pte(mm, vma, addr, pte))
147 dec_mm_counter(mm, file_rss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 set_pte_at(mm, addr, pte, pgoff_to_pte(pgoff));
150 pte_val = *pte;
151 pte_unmap(pte);
152 update_mmu_cache(vma, addr, pte_val);
153 spin_unlock(&mm->page_table_lock);
154 return 0;
155
156err_unlock:
157 spin_unlock(&mm->page_table_lock);
158 return err;
159}
160
161
162/***
163 * sys_remap_file_pages - remap arbitrary pages of a shared backing store
164 * file within an existing vma.
165 * @start: start of the remapped virtual memory range
166 * @size: size of the remapped virtual memory range
167 * @prot: new protection bits of the range
168 * @pgoff: to be mapped page of the backing store file
169 * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO.
170 *
171 * this syscall works purely via pagetables, so it's the most efficient
172 * way to map the same (large) file into a given virtual window. Unlike
173 * mmap()/mremap() it does not create any new vmas. The new mappings are
174 * also safe across swapout.
175 *
176 * NOTE: the 'prot' parameter right now is ignored, and the vma's default
177 * protection is used. Arbitrary protections might be implemented in the
178 * future.
179 */
180asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
181 unsigned long __prot, unsigned long pgoff, unsigned long flags)
182{
183 struct mm_struct *mm = current->mm;
184 struct address_space *mapping;
185 unsigned long end = start + size;
186 struct vm_area_struct *vma;
187 int err = -EINVAL;
188 int has_write_lock = 0;
189
190 if (__prot)
191 return err;
192 /*
193 * Sanitize the syscall parameters:
194 */
195 start = start & PAGE_MASK;
196 size = size & PAGE_MASK;
197
198 /* Does the address range wrap, or is the span zero-sized? */
199 if (start + size <= start)
200 return err;
201
202 /* Can we represent this offset inside this architecture's pte's? */
203#if PTE_FILE_MAX_BITS < BITS_PER_LONG
204 if (pgoff + (size >> PAGE_SHIFT) >= (1UL << PTE_FILE_MAX_BITS))
205 return err;
206#endif
207
208 /* We need down_write() to change vma->vm_flags. */
209 down_read(&mm->mmap_sem);
210 retry:
211 vma = find_vma(mm, start);
212
213 /*
214 * Make sure the vma is shared, that it supports prefaulting,
215 * and that the remapped range is valid and fully within
216 * the single existing vma. vm_private_data is used as a
217 * swapout cursor in a VM_NONLINEAR vma (unless VM_RESERVED
218 * or VM_LOCKED, but VM_LOCKED could be revoked later on).
219 */
220 if (vma && (vma->vm_flags & VM_SHARED) &&
221 (!vma->vm_private_data ||
222 (vma->vm_flags & (VM_NONLINEAR|VM_RESERVED))) &&
223 vma->vm_ops && vma->vm_ops->populate &&
224 end > start && start >= vma->vm_start &&
225 end <= vma->vm_end) {
226
227 /* Must set VM_NONLINEAR before any pages are populated. */
228 if (pgoff != linear_page_index(vma, start) &&
229 !(vma->vm_flags & VM_NONLINEAR)) {
230 if (!has_write_lock) {
231 up_read(&mm->mmap_sem);
232 down_write(&mm->mmap_sem);
233 has_write_lock = 1;
234 goto retry;
235 }
236 mapping = vma->vm_file->f_mapping;
237 spin_lock(&mapping->i_mmap_lock);
238 flush_dcache_mmap_lock(mapping);
239 vma->vm_flags |= VM_NONLINEAR;
240 vma_prio_tree_remove(vma, &mapping->i_mmap);
241 vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
242 flush_dcache_mmap_unlock(mapping);
243 spin_unlock(&mapping->i_mmap_lock);
244 }
245
246 err = vma->vm_ops->populate(vma, start, size,
247 vma->vm_page_prot,
248 pgoff, flags & MAP_NONBLOCK);
249
250 /*
251 * We can't clear VM_NONLINEAR because we'd have to do
252 * it after ->populate completes, and that would prevent
253 * downgrading the lock. (Locks can't be upgraded).
254 */
255 }
256 if (likely(!has_write_lock))
257 up_read(&mm->mmap_sem);
258 else
259 up_write(&mm->mmap_sem);
260
261 return err;
262}
263