]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | ||
861f2fb8 | 23 | static int zap_pte(struct mm_struct *mm, struct vm_area_struct *vma, |
1da177e4 LT |
24 | unsigned long addr, pte_t *ptep) |
25 | { | |
26 | pte_t pte = *ptep; | |
861f2fb8 | 27 | struct page *page = NULL; |
1da177e4 | 28 | |
1da177e4 | 29 | if (pte_present(pte)) { |
6aab341e | 30 | flush_cache_page(vma, addr, pte_pfn(pte)); |
1da177e4 | 31 | pte = ptep_clear_flush(vma, addr, ptep); |
6aab341e LT |
32 | 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); | |
1da177e4 LT |
38 | } |
39 | } else { | |
40 | if (!pte_file(pte)) | |
41 | free_swap_and_cache(pte_to_swp_entry(pte)); | |
42 | pte_clear(mm, addr, ptep); | |
43 | } | |
861f2fb8 | 44 | return !!page; |
1da177e4 LT |
45 | } |
46 | ||
47 | /* | |
48 | * Install a file page to a given virtual memory address, release any | |
49 | * previously existing mapping. | |
50 | */ | |
51 | int 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; | |
1da177e4 | 58 | pte_t pte_val; |
c74df32c | 59 | spinlock_t *ptl; |
1da177e4 | 60 | |
c9cfcddf | 61 | pte = get_locked_pte(mm, addr, &ptl); |
1da177e4 | 62 | if (!pte) |
c74df32c | 63 | goto out; |
1da177e4 LT |
64 | |
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) | |
c74df32c | 73 | goto unlock; |
f5154a98 HD |
74 | err = -ENOMEM; |
75 | if (page_mapcount(page) > INT_MAX/2) | |
c74df32c | 76 | goto unlock; |
1da177e4 | 77 | |
861f2fb8 HD |
78 | if (pte_none(*pte) || !zap_pte(mm, vma, addr, pte)) |
79 | inc_mm_counter(mm, file_rss); | |
1da177e4 | 80 | |
1da177e4 LT |
81 | flush_icache_page(vma, page); |
82 | set_pte_at(mm, addr, pte, mk_pte(page, prot)); | |
83 | page_add_file_rmap(page); | |
84 | pte_val = *pte; | |
1da177e4 | 85 | update_mmu_cache(vma, addr, pte_val); |
1da177e4 | 86 | err = 0; |
c74df32c HD |
87 | unlock: |
88 | pte_unmap_unlock(pte, ptl); | |
89 | out: | |
1da177e4 LT |
90 | return err; |
91 | } | |
92 | EXPORT_SYMBOL(install_page); | |
93 | ||
1da177e4 LT |
94 | /* |
95 | * Install a file pte to a given virtual memory address, release any | |
96 | * previously existing mapping. | |
97 | */ | |
98 | int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma, | |
99 | unsigned long addr, unsigned long pgoff, pgprot_t prot) | |
100 | { | |
101 | int err = -ENOMEM; | |
102 | pte_t *pte; | |
1da177e4 | 103 | pte_t pte_val; |
c74df32c | 104 | spinlock_t *ptl; |
1da177e4 | 105 | |
c9cfcddf | 106 | pte = get_locked_pte(mm, addr, &ptl); |
1da177e4 | 107 | if (!pte) |
c74df32c | 108 | goto out; |
1da177e4 | 109 | |
365e9c87 HD |
110 | if (!pte_none(*pte) && zap_pte(mm, vma, addr, pte)) { |
111 | update_hiwater_rss(mm); | |
861f2fb8 | 112 | dec_mm_counter(mm, file_rss); |
365e9c87 | 113 | } |
1da177e4 LT |
114 | |
115 | set_pte_at(mm, addr, pte, pgoff_to_pte(pgoff)); | |
116 | pte_val = *pte; | |
1da177e4 | 117 | update_mmu_cache(vma, addr, pte_val); |
c74df32c HD |
118 | pte_unmap_unlock(pte, ptl); |
119 | err = 0; | |
120 | out: | |
1da177e4 LT |
121 | return err; |
122 | } | |
123 | ||
1da177e4 LT |
124 | /*** |
125 | * sys_remap_file_pages - remap arbitrary pages of a shared backing store | |
126 | * file within an existing vma. | |
127 | * @start: start of the remapped virtual memory range | |
128 | * @size: size of the remapped virtual memory range | |
129 | * @prot: new protection bits of the range | |
130 | * @pgoff: to be mapped page of the backing store file | |
131 | * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO. | |
132 | * | |
133 | * this syscall works purely via pagetables, so it's the most efficient | |
134 | * way to map the same (large) file into a given virtual window. Unlike | |
135 | * mmap()/mremap() it does not create any new vmas. The new mappings are | |
136 | * also safe across swapout. | |
137 | * | |
138 | * NOTE: the 'prot' parameter right now is ignored, and the vma's default | |
139 | * protection is used. Arbitrary protections might be implemented in the | |
140 | * future. | |
141 | */ | |
142 | asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size, | |
143 | unsigned long __prot, unsigned long pgoff, unsigned long flags) | |
144 | { | |
145 | struct mm_struct *mm = current->mm; | |
146 | struct address_space *mapping; | |
147 | unsigned long end = start + size; | |
148 | struct vm_area_struct *vma; | |
149 | int err = -EINVAL; | |
150 | int has_write_lock = 0; | |
151 | ||
152 | if (__prot) | |
153 | return err; | |
154 | /* | |
155 | * Sanitize the syscall parameters: | |
156 | */ | |
157 | start = start & PAGE_MASK; | |
158 | size = size & PAGE_MASK; | |
159 | ||
160 | /* Does the address range wrap, or is the span zero-sized? */ | |
161 | if (start + size <= start) | |
162 | return err; | |
163 | ||
164 | /* Can we represent this offset inside this architecture's pte's? */ | |
165 | #if PTE_FILE_MAX_BITS < BITS_PER_LONG | |
166 | if (pgoff + (size >> PAGE_SHIFT) >= (1UL << PTE_FILE_MAX_BITS)) | |
167 | return err; | |
168 | #endif | |
169 | ||
170 | /* We need down_write() to change vma->vm_flags. */ | |
171 | down_read(&mm->mmap_sem); | |
172 | retry: | |
173 | vma = find_vma(mm, start); | |
174 | ||
175 | /* | |
176 | * Make sure the vma is shared, that it supports prefaulting, | |
177 | * and that the remapped range is valid and fully within | |
178 | * the single existing vma. vm_private_data is used as a | |
101d2be7 | 179 | * swapout cursor in a VM_NONLINEAR vma. |
1da177e4 LT |
180 | */ |
181 | if (vma && (vma->vm_flags & VM_SHARED) && | |
101d2be7 | 182 | (!vma->vm_private_data || (vma->vm_flags & VM_NONLINEAR)) && |
1da177e4 LT |
183 | vma->vm_ops && vma->vm_ops->populate && |
184 | end > start && start >= vma->vm_start && | |
185 | end <= vma->vm_end) { | |
186 | ||
187 | /* Must set VM_NONLINEAR before any pages are populated. */ | |
188 | if (pgoff != linear_page_index(vma, start) && | |
189 | !(vma->vm_flags & VM_NONLINEAR)) { | |
190 | if (!has_write_lock) { | |
191 | up_read(&mm->mmap_sem); | |
192 | down_write(&mm->mmap_sem); | |
193 | has_write_lock = 1; | |
194 | goto retry; | |
195 | } | |
196 | mapping = vma->vm_file->f_mapping; | |
197 | spin_lock(&mapping->i_mmap_lock); | |
198 | flush_dcache_mmap_lock(mapping); | |
199 | vma->vm_flags |= VM_NONLINEAR; | |
200 | vma_prio_tree_remove(vma, &mapping->i_mmap); | |
201 | vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear); | |
202 | flush_dcache_mmap_unlock(mapping); | |
203 | spin_unlock(&mapping->i_mmap_lock); | |
204 | } | |
205 | ||
206 | err = vma->vm_ops->populate(vma, start, size, | |
207 | vma->vm_page_prot, | |
208 | pgoff, flags & MAP_NONBLOCK); | |
209 | ||
210 | /* | |
211 | * We can't clear VM_NONLINEAR because we'd have to do | |
212 | * it after ->populate completes, and that would prevent | |
213 | * downgrading the lock. (Locks can't be upgraded). | |
214 | */ | |
215 | } | |
216 | if (likely(!has_write_lock)) | |
217 | up_read(&mm->mmap_sem); | |
218 | else | |
219 | up_write(&mm->mmap_sem); | |
220 | ||
221 | return err; | |
222 | } | |
223 |