ia64/xen-unstable

view xenolinux-2.4.21-pre4-sparse/mm/mremap.c @ 302:18f05ecf3fef

bitkeeper revision 1.124 (3e71f8a4QvveKwitZNAJi1H3BJpPEQ)

ac_timer.c:
rewrite of do timer/add_timer + perfcounters
apic.c:
added perfcounter and try to disable APIC when no timeout value is zero.
irq.c:
count interrupts and cycles spent in them
sched.h:
added fields for BVT
schedule.c:
BVT without warping
keyhandler.c:
added handler for dumping run queues
moved handler for ac_timers here
.del-dom0_ops.h~f77c7a14cfa618f8:
Delete: tools/domain_builder/dom0_ops.h
author rn@wyvis.research.intel-research.net
date Fri Mar 14 15:43:32 2003 +0000 (2003-03-14)
parents d7d0a23b2e07
children
line source
1 /*
2 * linux/mm/remap.c
3 *
4 * (C) Copyright 1996 Linus Torvalds
5 */
7 #include <linux/slab.h>
8 #include <linux/smp_lock.h>
9 #include <linux/shm.h>
10 #include <linux/mman.h>
11 #include <linux/swap.h>
13 #include <asm/uaccess.h>
14 #include <asm/pgalloc.h>
16 extern int vm_enough_memory(long pages);
18 static inline pte_t *get_one_pte(struct mm_struct *mm, unsigned long addr)
19 {
20 pgd_t * pgd;
21 pmd_t * pmd;
22 pte_t * pte = NULL;
24 pgd = pgd_offset(mm, addr);
25 if (pgd_none(*pgd))
26 goto end;
27 if (pgd_bad(*pgd)) {
28 pgd_ERROR(*pgd);
29 pgd_clear(pgd);
30 goto end;
31 }
33 pmd = pmd_offset(pgd, addr);
34 if (pmd_none(*pmd))
35 goto end;
36 if (pmd_bad(*pmd)) {
37 pmd_ERROR(*pmd);
38 pmd_clear(pmd);
39 goto end;
40 }
42 pte = pte_offset(pmd, addr);
43 if (pte_none(*pte))
44 pte = NULL;
45 end:
46 return pte;
47 }
49 static inline pte_t *alloc_one_pte(struct mm_struct *mm, unsigned long addr)
50 {
51 pmd_t * pmd;
52 pte_t * pte = NULL;
54 pmd = pmd_alloc(mm, pgd_offset(mm, addr), addr);
55 if (pmd)
56 pte = pte_alloc(mm, pmd, addr);
57 return pte;
58 }
60 static inline int copy_one_pte(struct mm_struct *mm, pte_t * src, pte_t * dst)
61 {
62 int error = 0;
63 pte_t pte;
65 if (!pte_none(*src)) {
66 pte = ptep_get_and_clear(src);
67 if (!dst) {
68 /* No dest? We must put it back. */
69 dst = src;
70 error++;
71 }
72 set_pte(dst, pte);
73 }
74 return error;
75 }
77 static int move_one_page(struct mm_struct *mm, unsigned long old_addr, unsigned long new_addr)
78 {
79 int error = 0;
80 pte_t * src;
82 spin_lock(&mm->page_table_lock);
83 src = get_one_pte(mm, old_addr);
84 if (src)
85 error = copy_one_pte(mm, src, alloc_one_pte(mm, new_addr));
86 spin_unlock(&mm->page_table_lock);
87 return error;
88 }
90 static int move_page_tables(struct mm_struct * mm,
91 unsigned long new_addr, unsigned long old_addr, unsigned long len)
92 {
93 unsigned long offset = len;
95 flush_cache_range(mm, old_addr, old_addr + len);
97 /*
98 * This is not the clever way to do this, but we're taking the
99 * easy way out on the assumption that most remappings will be
100 * only a few pages.. This also makes error recovery easier.
101 */
102 while (offset) {
103 offset -= PAGE_SIZE;
104 if (move_one_page(mm, old_addr + offset, new_addr + offset))
105 goto oops_we_failed;
106 }
107 flush_tlb_range(mm, old_addr, old_addr + len);
108 return 0;
110 /*
111 * Ok, the move failed because we didn't have enough pages for
112 * the new page table tree. This is unlikely, but we have to
113 * take the possibility into account. In that case we just move
114 * all the pages back (this will work, because we still have
115 * the old page tables)
116 */
117 oops_we_failed:
118 XENO_flush_page_update_queue();
119 flush_cache_range(mm, new_addr, new_addr + len);
120 while ((offset += PAGE_SIZE) < len)
121 move_one_page(mm, new_addr + offset, old_addr + offset);
122 XENO_flush_page_update_queue();
123 zap_page_range(mm, new_addr, len);
124 return -1;
125 }
127 static inline unsigned long move_vma(struct vm_area_struct * vma,
128 unsigned long addr, unsigned long old_len, unsigned long new_len,
129 unsigned long new_addr)
130 {
131 struct mm_struct * mm = vma->vm_mm;
132 struct vm_area_struct * new_vma, * next, * prev;
133 int allocated_vma;
135 new_vma = NULL;
136 next = find_vma_prev(mm, new_addr, &prev);
137 if (next) {
138 if (prev && prev->vm_end == new_addr &&
139 can_vma_merge(prev, vma->vm_flags) && !vma->vm_file && !(vma->vm_flags & VM_SHARED)) {
140 spin_lock(&mm->page_table_lock);
141 prev->vm_end = new_addr + new_len;
142 spin_unlock(&mm->page_table_lock);
143 new_vma = prev;
144 if (next != prev->vm_next)
145 BUG();
146 if (prev->vm_end == next->vm_start && can_vma_merge(next, prev->vm_flags)) {
147 spin_lock(&mm->page_table_lock);
148 prev->vm_end = next->vm_end;
149 __vma_unlink(mm, next, prev);
150 spin_unlock(&mm->page_table_lock);
152 mm->map_count--;
153 kmem_cache_free(vm_area_cachep, next);
154 }
155 } else if (next->vm_start == new_addr + new_len &&
156 can_vma_merge(next, vma->vm_flags) && !vma->vm_file && !(vma->vm_flags & VM_SHARED)) {
157 spin_lock(&mm->page_table_lock);
158 next->vm_start = new_addr;
159 spin_unlock(&mm->page_table_lock);
160 new_vma = next;
161 }
162 } else {
163 prev = find_vma(mm, new_addr-1);
164 if (prev && prev->vm_end == new_addr &&
165 can_vma_merge(prev, vma->vm_flags) && !vma->vm_file && !(vma->vm_flags & VM_SHARED)) {
166 spin_lock(&mm->page_table_lock);
167 prev->vm_end = new_addr + new_len;
168 spin_unlock(&mm->page_table_lock);
169 new_vma = prev;
170 }
171 }
173 allocated_vma = 0;
174 if (!new_vma) {
175 new_vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
176 if (!new_vma)
177 goto out;
178 allocated_vma = 1;
179 }
181 if (!move_page_tables(current->mm, new_addr, addr, old_len)) {
182 if (allocated_vma) {
183 *new_vma = *vma;
184 new_vma->vm_start = new_addr;
185 new_vma->vm_end = new_addr+new_len;
186 new_vma->vm_pgoff += (addr - vma->vm_start) >> PAGE_SHIFT;
187 new_vma->vm_raend = 0;
188 if (new_vma->vm_file)
189 get_file(new_vma->vm_file);
190 if (new_vma->vm_ops && new_vma->vm_ops->open)
191 new_vma->vm_ops->open(new_vma);
192 insert_vm_struct(current->mm, new_vma);
193 }
194 do_munmap(current->mm, addr, old_len);
195 current->mm->total_vm += new_len >> PAGE_SHIFT;
196 if (new_vma->vm_flags & VM_LOCKED) {
197 current->mm->locked_vm += new_len >> PAGE_SHIFT;
198 make_pages_present(new_vma->vm_start,
199 new_vma->vm_end);
200 }
201 return new_addr;
202 }
203 if (allocated_vma)
204 kmem_cache_free(vm_area_cachep, new_vma);
205 out:
206 return -ENOMEM;
207 }
209 /*
210 * Expand (or shrink) an existing mapping, potentially moving it at the
211 * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
212 *
213 * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
214 * This option implies MREMAP_MAYMOVE.
215 */
216 unsigned long do_mremap(unsigned long addr,
217 unsigned long old_len, unsigned long new_len,
218 unsigned long flags, unsigned long new_addr)
219 {
220 struct vm_area_struct *vma;
221 unsigned long ret = -EINVAL;
223 if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE))
224 goto out;
226 if (addr & ~PAGE_MASK)
227 goto out;
229 old_len = PAGE_ALIGN(old_len);
230 new_len = PAGE_ALIGN(new_len);
232 /* new_addr is only valid if MREMAP_FIXED is specified */
233 if (flags & MREMAP_FIXED) {
234 if (new_addr & ~PAGE_MASK)
235 goto out;
236 if (!(flags & MREMAP_MAYMOVE))
237 goto out;
239 if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len)
240 goto out;
242 /* Check if the location we're moving into overlaps the
243 * old location at all, and fail if it does.
244 */
245 if ((new_addr <= addr) && (new_addr+new_len) > addr)
246 goto out;
248 if ((addr <= new_addr) && (addr+old_len) > new_addr)
249 goto out;
251 do_munmap(current->mm, new_addr, new_len);
252 }
254 /*
255 * Always allow a shrinking remap: that just unmaps
256 * the unnecessary pages..
257 */
258 ret = addr;
259 if (old_len >= new_len) {
260 do_munmap(current->mm, addr+new_len, old_len - new_len);
261 if (!(flags & MREMAP_FIXED) || (new_addr == addr))
262 goto out;
263 }
265 /*
266 * Ok, we need to grow.. or relocate.
267 */
268 ret = -EFAULT;
269 vma = find_vma(current->mm, addr);
270 if (!vma || vma->vm_start > addr)
271 goto out;
272 /* We can't remap across vm area boundaries */
273 if (old_len > vma->vm_end - addr)
274 goto out;
275 if (vma->vm_flags & VM_DONTEXPAND) {
276 if (new_len > old_len)
277 goto out;
278 }
279 if (vma->vm_flags & VM_LOCKED) {
280 unsigned long locked = current->mm->locked_vm << PAGE_SHIFT;
281 locked += new_len - old_len;
282 ret = -EAGAIN;
283 if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
284 goto out;
285 }
286 ret = -ENOMEM;
287 if ((current->mm->total_vm << PAGE_SHIFT) + (new_len - old_len)
288 > current->rlim[RLIMIT_AS].rlim_cur)
289 goto out;
290 /* Private writable mapping? Check memory availability.. */
291 if ((vma->vm_flags & (VM_SHARED | VM_WRITE)) == VM_WRITE &&
292 !(flags & MAP_NORESERVE) &&
293 !vm_enough_memory((new_len - old_len) >> PAGE_SHIFT))
294 goto out;
296 /* old_len exactly to the end of the area..
297 * And we're not relocating the area.
298 */
299 if (old_len == vma->vm_end - addr &&
300 !((flags & MREMAP_FIXED) && (addr != new_addr)) &&
301 (old_len != new_len || !(flags & MREMAP_MAYMOVE))) {
302 unsigned long max_addr = TASK_SIZE;
303 if (vma->vm_next)
304 max_addr = vma->vm_next->vm_start;
305 /* can we just expand the current mapping? */
306 if (max_addr - addr >= new_len) {
307 int pages = (new_len - old_len) >> PAGE_SHIFT;
308 spin_lock(&vma->vm_mm->page_table_lock);
309 vma->vm_end = addr + new_len;
310 spin_unlock(&vma->vm_mm->page_table_lock);
311 current->mm->total_vm += pages;
312 if (vma->vm_flags & VM_LOCKED) {
313 current->mm->locked_vm += pages;
314 make_pages_present(addr + old_len,
315 addr + new_len);
316 }
317 ret = addr;
318 goto out;
319 }
320 }
322 /*
323 * We weren't able to just expand or shrink the area,
324 * we need to create a new one and move it..
325 */
326 ret = -ENOMEM;
327 if (flags & MREMAP_MAYMOVE) {
328 if (!(flags & MREMAP_FIXED)) {
329 unsigned long map_flags = 0;
330 if (vma->vm_flags & VM_SHARED)
331 map_flags |= MAP_SHARED;
333 new_addr = get_unmapped_area(vma->vm_file, 0, new_len, vma->vm_pgoff, map_flags);
334 ret = new_addr;
335 if (new_addr & ~PAGE_MASK)
336 goto out;
337 }
338 ret = move_vma(vma, addr, old_len, new_len, new_addr);
339 }
340 out:
341 return ret;
342 }
344 asmlinkage unsigned long sys_mremap(unsigned long addr,
345 unsigned long old_len, unsigned long new_len,
346 unsigned long flags, unsigned long new_addr)
347 {
348 unsigned long ret;
350 down_write(&current->mm->mmap_sem);
351 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
352 up_write(&current->mm->mmap_sem);
353 return ret;
354 }