blob: aa30618ec6b2cd356c57896a2099a3e4b2a5e39c [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 pte_t pte_val;
Hugh Dickinsc74df322005-10-29 18:16:23 -070059 spinlock_t *ptl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvaldsc9cfcdd2005-11-29 14:03:14 -080061 pte = get_locked_pte(mm, addr, &ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 if (!pte)
Hugh Dickinsc74df322005-10-29 18:16:23 -070063 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 /*
66 * This page may have been truncated. Tell the
67 * caller about it.
68 */
69 err = -EINVAL;
70 inode = vma->vm_file->f_mapping->host;
71 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
72 if (!page->mapping || page->index >= size)
Hugh Dickinsc74df322005-10-29 18:16:23 -070073 goto unlock;
Hugh Dickinsf5154a92005-10-11 19:16:26 +010074 err = -ENOMEM;
75 if (page_mapcount(page) > INT_MAX/2)
Hugh Dickinsc74df322005-10-29 18:16:23 -070076 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Hugh Dickins861f2fb2005-10-29 18:16:17 -070078 if (pte_none(*pte) || !zap_pte(mm, vma, addr, pte))
79 inc_mm_counter(mm, file_rss);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 flush_icache_page(vma, page);
Peter Zijlstrae88dd6c2006-09-25 23:30:59 -070082 pte_val = mk_pte(page, prot);
83 set_pte_at(mm, addr, pte, pte_val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 page_add_file_rmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 update_mmu_cache(vma, addr, pte_val);
Hugh Dickins668e0d82006-06-23 02:03:45 -070086 lazy_mmu_prot_update(pte_val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 err = 0;
Hugh Dickinsc74df322005-10-29 18:16:23 -070088unlock:
89 pte_unmap_unlock(pte, ptl);
90out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return err;
92}
93EXPORT_SYMBOL(install_page);
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/*
96 * Install a file pte to a given virtual memory address, release any
97 * previously existing mapping.
98 */
99int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma,
100 unsigned long addr, unsigned long pgoff, pgprot_t prot)
101{
102 int err = -ENOMEM;
103 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 pte_t pte_val;
Hugh Dickinsc74df322005-10-29 18:16:23 -0700105 spinlock_t *ptl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Linus Torvaldsc9cfcdd2005-11-29 14:03:14 -0800107 pte = get_locked_pte(mm, addr, &ptl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 if (!pte)
Hugh Dickinsc74df322005-10-29 18:16:23 -0700109 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Hugh Dickins365e9c872005-10-29 18:16:18 -0700111 if (!pte_none(*pte) && zap_pte(mm, vma, addr, pte)) {
112 update_hiwater_rss(mm);
Hugh Dickins861f2fb2005-10-29 18:16:17 -0700113 dec_mm_counter(mm, file_rss);
Hugh Dickins365e9c872005-10-29 18:16:18 -0700114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 set_pte_at(mm, addr, pte, pgoff_to_pte(pgoff));
117 pte_val = *pte;
Hugh Dickins668e0d82006-06-23 02:03:45 -0700118 /*
119 * We don't need to run update_mmu_cache() here because the "file pte"
120 * being installed by install_file_pte() is not a real pte - it's a
121 * non-present entry (like a swap entry), noting what file offset should
122 * be mapped there when there's a fault (in a non-linear vma where
123 * that's not obvious).
124 */
Hugh Dickinsc74df322005-10-29 18:16:23 -0700125 pte_unmap_unlock(pte, ptl);
126 err = 0;
127out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return err;
129}
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/***
132 * sys_remap_file_pages - remap arbitrary pages of a shared backing store
133 * file within an existing vma.
134 * @start: start of the remapped virtual memory range
135 * @size: size of the remapped virtual memory range
136 * @prot: new protection bits of the range
137 * @pgoff: to be mapped page of the backing store file
138 * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO.
139 *
140 * this syscall works purely via pagetables, so it's the most efficient
141 * way to map the same (large) file into a given virtual window. Unlike
142 * mmap()/mremap() it does not create any new vmas. The new mappings are
143 * also safe across swapout.
144 *
145 * NOTE: the 'prot' parameter right now is ignored, and the vma's default
146 * protection is used. Arbitrary protections might be implemented in the
147 * future.
148 */
149asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
150 unsigned long __prot, unsigned long pgoff, unsigned long flags)
151{
152 struct mm_struct *mm = current->mm;
153 struct address_space *mapping;
154 unsigned long end = start + size;
155 struct vm_area_struct *vma;
156 int err = -EINVAL;
157 int has_write_lock = 0;
158
159 if (__prot)
160 return err;
161 /*
162 * Sanitize the syscall parameters:
163 */
164 start = start & PAGE_MASK;
165 size = size & PAGE_MASK;
166
167 /* Does the address range wrap, or is the span zero-sized? */
168 if (start + size <= start)
169 return err;
170
171 /* Can we represent this offset inside this architecture's pte's? */
172#if PTE_FILE_MAX_BITS < BITS_PER_LONG
173 if (pgoff + (size >> PAGE_SHIFT) >= (1UL << PTE_FILE_MAX_BITS))
174 return err;
175#endif
176
177 /* We need down_write() to change vma->vm_flags. */
178 down_read(&mm->mmap_sem);
179 retry:
180 vma = find_vma(mm, start);
181
182 /*
183 * Make sure the vma is shared, that it supports prefaulting,
184 * and that the remapped range is valid and fully within
185 * the single existing vma. vm_private_data is used as a
Hugh Dickins101d2be2005-11-21 21:32:16 -0800186 * swapout cursor in a VM_NONLINEAR vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 */
188 if (vma && (vma->vm_flags & VM_SHARED) &&
Hugh Dickins101d2be2005-11-21 21:32:16 -0800189 (!vma->vm_private_data || (vma->vm_flags & VM_NONLINEAR)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 vma->vm_ops && vma->vm_ops->populate &&
191 end > start && start >= vma->vm_start &&
192 end <= vma->vm_end) {
193
194 /* Must set VM_NONLINEAR before any pages are populated. */
195 if (pgoff != linear_page_index(vma, start) &&
196 !(vma->vm_flags & VM_NONLINEAR)) {
197 if (!has_write_lock) {
198 up_read(&mm->mmap_sem);
199 down_write(&mm->mmap_sem);
200 has_write_lock = 1;
201 goto retry;
202 }
203 mapping = vma->vm_file->f_mapping;
204 spin_lock(&mapping->i_mmap_lock);
205 flush_dcache_mmap_lock(mapping);
206 vma->vm_flags |= VM_NONLINEAR;
207 vma_prio_tree_remove(vma, &mapping->i_mmap);
208 vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
209 flush_dcache_mmap_unlock(mapping);
210 spin_unlock(&mapping->i_mmap_lock);
211 }
212
213 err = vma->vm_ops->populate(vma, start, size,
214 vma->vm_page_prot,
215 pgoff, flags & MAP_NONBLOCK);
216
217 /*
218 * We can't clear VM_NONLINEAR because we'd have to do
219 * it after ->populate completes, and that would prevent
220 * downgrading the lock. (Locks can't be upgraded).
221 */
222 }
223 if (likely(!has_write_lock))
224 up_read(&mm->mmap_sem);
225 else
226 up_write(&mm->mmap_sem);
227
228 return err;
229}
230