blob: f851775e09c2a27f66763ef9e239c025f2ae8c6a [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)) {
Linus Torvalds6aab3412005-11-28 14:34:23 -080030 flush_cache_page(vma, addr, pte_pfn(pte));
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 pte = ptep_clear_flush(vma, addr, ptep);
Linus Torvalds6aab3412005-11-28 14:34:23 -080032 page = vm_normal_page(vma, addr, pte);
33 if (page) {
34 if (pte_dirty(pte))
35 set_page_dirty(page);
36 page_remove_rmap(page);
37 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 }
39 } else {
40 if (!pte_file(pte))
41 free_swap_and_cache(pte_to_swp_entry(pte));
42 pte_clear(mm, addr, ptep);
43 }
Hugh Dickins861f2fb2005-10-29 18:16:17 -070044 return !!page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
46
47/*
48 * Install a file page to a given virtual memory address, release any
49 * previously existing mapping.
50 */
51int install_page(struct mm_struct *mm, struct vm_area_struct *vma,
52 unsigned long addr, struct page *page, pgprot_t prot)
53{
54 struct inode *inode;
55 pgoff_t size;
56 int err = -ENOMEM;
57 pte_t *pte;
58 pmd_t *pmd;
59 pud_t *pud;
60 pgd_t *pgd;
61 pte_t pte_val;
Hugh Dickinsc74df322005-10-29 18:16:23 -070062 spinlock_t *ptl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 pgd = pgd_offset(mm, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 pud = pud_alloc(mm, pgd, addr);
66 if (!pud)
Hugh Dickinsc74df322005-10-29 18:16:23 -070067 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 pmd = pmd_alloc(mm, pud, addr);
69 if (!pmd)
Hugh Dickinsc74df322005-10-29 18:16:23 -070070 goto out;
71 pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (!pte)
Hugh Dickinsc74df322005-10-29 18:16:23 -070073 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 /*
76 * This page may have been truncated. Tell the
77 * caller about it.
78 */
79 err = -EINVAL;
80 inode = vma->vm_file->f_mapping->host;
81 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
82 if (!page->mapping || page->index >= size)
Hugh Dickinsc74df322005-10-29 18:16:23 -070083 goto unlock;
Hugh Dickinsf5154a92005-10-11 19:16:26 +010084 err = -ENOMEM;
85 if (page_mapcount(page) > INT_MAX/2)
Hugh Dickinsc74df322005-10-29 18:16:23 -070086 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Hugh Dickins861f2fb2005-10-29 18:16:17 -070088 if (pte_none(*pte) || !zap_pte(mm, vma, addr, pte))
89 inc_mm_counter(mm, file_rss);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 flush_icache_page(vma, page);
92 set_pte_at(mm, addr, pte, mk_pte(page, prot));
93 page_add_file_rmap(page);
94 pte_val = *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 update_mmu_cache(vma, addr, pte_val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 err = 0;
Hugh Dickinsc74df322005-10-29 18:16:23 -070097unlock:
98 pte_unmap_unlock(pte, ptl);
99out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return err;
101}
102EXPORT_SYMBOL(install_page);
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/*
105 * Install a file pte to a given virtual memory address, release any
106 * previously existing mapping.
107 */
108int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma,
109 unsigned long addr, unsigned long pgoff, pgprot_t prot)
110{
111 int err = -ENOMEM;
112 pte_t *pte;
113 pmd_t *pmd;
114 pud_t *pud;
115 pgd_t *pgd;
116 pte_t pte_val;
Hugh Dickinsc74df322005-10-29 18:16:23 -0700117 spinlock_t *ptl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 pgd = pgd_offset(mm, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 pud = pud_alloc(mm, pgd, addr);
121 if (!pud)
Hugh Dickinsc74df322005-10-29 18:16:23 -0700122 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 pmd = pmd_alloc(mm, pud, addr);
124 if (!pmd)
Hugh Dickinsc74df322005-10-29 18:16:23 -0700125 goto out;
126 pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (!pte)
Hugh Dickinsc74df322005-10-29 18:16:23 -0700128 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Hugh Dickins365e9c872005-10-29 18:16:18 -0700130 if (!pte_none(*pte) && zap_pte(mm, vma, addr, pte)) {
131 update_hiwater_rss(mm);
Hugh Dickins861f2fb2005-10-29 18:16:17 -0700132 dec_mm_counter(mm, file_rss);
Hugh Dickins365e9c872005-10-29 18:16:18 -0700133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 set_pte_at(mm, addr, pte, pgoff_to_pte(pgoff));
136 pte_val = *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 update_mmu_cache(vma, addr, pte_val);
Hugh Dickinsc74df322005-10-29 18:16:23 -0700138 pte_unmap_unlock(pte, ptl);
139 err = 0;
140out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return err;
142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/***
145 * sys_remap_file_pages - remap arbitrary pages of a shared backing store
146 * file within an existing vma.
147 * @start: start of the remapped virtual memory range
148 * @size: size of the remapped virtual memory range
149 * @prot: new protection bits of the range
150 * @pgoff: to be mapped page of the backing store file
151 * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO.
152 *
153 * this syscall works purely via pagetables, so it's the most efficient
154 * way to map the same (large) file into a given virtual window. Unlike
155 * mmap()/mremap() it does not create any new vmas. The new mappings are
156 * also safe across swapout.
157 *
158 * NOTE: the 'prot' parameter right now is ignored, and the vma's default
159 * protection is used. Arbitrary protections might be implemented in the
160 * future.
161 */
162asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
163 unsigned long __prot, unsigned long pgoff, unsigned long flags)
164{
165 struct mm_struct *mm = current->mm;
166 struct address_space *mapping;
167 unsigned long end = start + size;
168 struct vm_area_struct *vma;
169 int err = -EINVAL;
170 int has_write_lock = 0;
171
172 if (__prot)
173 return err;
174 /*
175 * Sanitize the syscall parameters:
176 */
177 start = start & PAGE_MASK;
178 size = size & PAGE_MASK;
179
180 /* Does the address range wrap, or is the span zero-sized? */
181 if (start + size <= start)
182 return err;
183
184 /* Can we represent this offset inside this architecture's pte's? */
185#if PTE_FILE_MAX_BITS < BITS_PER_LONG
186 if (pgoff + (size >> PAGE_SHIFT) >= (1UL << PTE_FILE_MAX_BITS))
187 return err;
188#endif
189
190 /* We need down_write() to change vma->vm_flags. */
191 down_read(&mm->mmap_sem);
192 retry:
193 vma = find_vma(mm, start);
194
195 /*
196 * Make sure the vma is shared, that it supports prefaulting,
197 * and that the remapped range is valid and fully within
198 * the single existing vma. vm_private_data is used as a
Hugh Dickins101d2be2005-11-21 21:32:16 -0800199 * swapout cursor in a VM_NONLINEAR vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 */
201 if (vma && (vma->vm_flags & VM_SHARED) &&
Hugh Dickins101d2be2005-11-21 21:32:16 -0800202 (!vma->vm_private_data || (vma->vm_flags & VM_NONLINEAR)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 vma->vm_ops && vma->vm_ops->populate &&
204 end > start && start >= vma->vm_start &&
205 end <= vma->vm_end) {
206
207 /* Must set VM_NONLINEAR before any pages are populated. */
208 if (pgoff != linear_page_index(vma, start) &&
209 !(vma->vm_flags & VM_NONLINEAR)) {
210 if (!has_write_lock) {
211 up_read(&mm->mmap_sem);
212 down_write(&mm->mmap_sem);
213 has_write_lock = 1;
214 goto retry;
215 }
216 mapping = vma->vm_file->f_mapping;
217 spin_lock(&mapping->i_mmap_lock);
218 flush_dcache_mmap_lock(mapping);
219 vma->vm_flags |= VM_NONLINEAR;
220 vma_prio_tree_remove(vma, &mapping->i_mmap);
221 vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
222 flush_dcache_mmap_unlock(mapping);
223 spin_unlock(&mapping->i_mmap_lock);
224 }
225
226 err = vma->vm_ops->populate(vma, start, size,
227 vma->vm_page_prot,
228 pgoff, flags & MAP_NONBLOCK);
229
230 /*
231 * We can't clear VM_NONLINEAR because we'd have to do
232 * it after ->populate completes, and that would prevent
233 * downgrading the lock. (Locks can't be upgraded).
234 */
235 }
236 if (likely(!has_write_lock))
237 up_read(&mm->mmap_sem);
238 else
239 up_write(&mm->mmap_sem);
240
241 return err;
242}
243