backported vs2.1.x fix to irq handling, which caused incorrect scheduler behavior
[linux-2.6.git] / mm / fremap.c
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 #include <linux/vs_base.h>
19 #include <linux/vs_memory.h>
20
21 #include <asm/mmu_context.h>
22 #include <asm/cacheflush.h>
23 #include <asm/tlbflush.h>
24
25 static int zap_pte(struct mm_struct *mm, struct vm_area_struct *vma,
26                         unsigned long addr, pte_t *ptep)
27 {
28         pte_t pte = *ptep;
29         struct page *page = NULL;
30
31         if (pte_present(pte)) {
32                 flush_cache_page(vma, addr, pte_pfn(pte));
33                 pte = ptep_clear_flush(vma, addr, ptep);
34                 page = vm_normal_page(vma, addr, pte);
35                 if (page) {
36                         if (pte_dirty(pte))
37                                 set_page_dirty(page);
38                         page_remove_rmap(page, vma);
39                         page_cache_release(page);
40                 }
41         } else {
42                 if (!pte_file(pte))
43                         free_swap_and_cache(pte_to_swp_entry(pte));
44                 pte_clear(mm, addr, ptep);
45         }
46         return !!page;
47 }
48
49 /*
50  * Install a file page to a given virtual memory address, release any
51  * previously existing mapping.
52  */
53 int install_page(struct mm_struct *mm, struct vm_area_struct *vma,
54                 unsigned long addr, struct page *page, pgprot_t prot)
55 {
56         struct inode *inode;
57         pgoff_t size;
58         int err = -ENOMEM;
59         pte_t *pte;
60         pte_t pte_val;
61         spinlock_t *ptl;
62
63         pte = get_locked_pte(mm, addr, &ptl);
64         if (!pte)
65                 goto out;
66
67         /*
68          * This page may have been truncated. Tell the
69          * caller about it.
70          */
71         err = -EINVAL;
72         if (vma->vm_file) {
73                 inode = vma->vm_file->f_mapping->host;
74                 size = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
75                 if (!page->mapping || page->index >= size)
76                         goto unlock;
77                 err = -ENOMEM;
78                 if (page_mapcount(page) > INT_MAX/2)
79                         goto unlock;
80         }
81         err = -ENOMEM;
82         if (!vx_rsspages_avail(mm, 1))
83                 goto unlock;
84
85         if (pte_none(*pte) || !zap_pte(mm, vma, addr, pte))
86                 inc_mm_counter(mm, file_rss);
87
88         flush_icache_page(vma, page);
89         pte_val = mk_pte(page, prot);
90         set_pte_at(mm, addr, pte, pte_val);
91         page_add_file_rmap(page);
92         update_mmu_cache(vma, addr, pte_val);
93         lazy_mmu_prot_update(pte_val);
94         err = 0;
95 unlock:
96         pte_unmap_unlock(pte, ptl);
97 out:
98         return err;
99 }
100 EXPORT_SYMBOL(install_page);
101
102 /*
103  * Install a file pte to a given virtual memory address, release any
104  * previously existing mapping.
105  */
106 int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma,
107                 unsigned long addr, unsigned long pgoff, pgprot_t prot)
108 {
109         int err = -ENOMEM;
110         pte_t *pte;
111         pte_t pte_val;
112         spinlock_t *ptl;
113
114         pte = get_locked_pte(mm, addr, &ptl);
115         if (!pte)
116                 goto out;
117
118         if (!pte_none(*pte) && zap_pte(mm, vma, addr, pte)) {
119                 update_hiwater_rss(mm);
120                 dec_mm_counter(mm, file_rss);
121         }
122
123         set_pte_at(mm, addr, pte, pgoff_to_pte(pgoff));
124         pte_val = *pte;
125         /*
126          * We don't need to run update_mmu_cache() here because the "file pte"
127          * being installed by install_file_pte() is not a real pte - it's a
128          * non-present entry (like a swap entry), noting what file offset should
129          * be mapped there when there's a fault (in a non-linear vma where
130          * that's not obvious).
131          */
132         pte_unmap_unlock(pte, ptl);
133         err = 0;
134 out:
135         return err;
136 }
137
138 /***
139  * sys_remap_file_pages - remap arbitrary pages of a shared backing store
140  *                        file within an existing vma.
141  * @start: start of the remapped virtual memory range
142  * @size: size of the remapped virtual memory range
143  * @prot: new protection bits of the range
144  * @pgoff: to be mapped page of the backing store file
145  * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO.
146  *
147  * this syscall works purely via pagetables, so it's the most efficient
148  * way to map the same (large) file into a given virtual window. Unlike
149  * mmap()/mremap() it does not create any new vmas. The new mappings are
150  * also safe across swapout.
151  *
152  * NOTE: the 'prot' parameter right now is ignored, and the vma's default
153  * protection is used. Arbitrary protections might be implemented in the
154  * future.
155  */
156 asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
157         unsigned long __prot, unsigned long pgoff, unsigned long flags)
158 {
159         struct mm_struct *mm = current->mm;
160         struct address_space *mapping;
161         unsigned long end = start + size;
162         struct vm_area_struct *vma;
163         int err = -EINVAL;
164         int has_write_lock = 0;
165
166         if (__prot)
167                 return err;
168         /*
169          * Sanitize the syscall parameters:
170          */
171         start = start & PAGE_MASK;
172         size = size & PAGE_MASK;
173
174         /* Does the address range wrap, or is the span zero-sized? */
175         if (start + size <= start)
176                 return err;
177
178         /* Can we represent this offset inside this architecture's pte's? */
179 #if PTE_FILE_MAX_BITS < BITS_PER_LONG
180         if (pgoff + (size >> PAGE_SHIFT) >= (1UL << PTE_FILE_MAX_BITS))
181                 return err;
182 #endif
183
184         /* We need down_write() to change vma->vm_flags. */
185         down_read(&mm->mmap_sem);
186  retry:
187         vma = find_vma(mm, start);
188
189         /*
190          * Make sure the vma is shared, that it supports prefaulting,
191          * and that the remapped range is valid and fully within
192          * the single existing vma.  vm_private_data is used as a
193          * swapout cursor in a VM_NONLINEAR vma.
194          */
195         if (vma && (vma->vm_flags & VM_SHARED) &&
196                 (!vma->vm_private_data || (vma->vm_flags & VM_NONLINEAR)) &&
197                 vma->vm_ops && vma->vm_ops->populate &&
198                         end > start && start >= vma->vm_start &&
199                                 end <= vma->vm_end) {
200
201                 /* Must set VM_NONLINEAR before any pages are populated. */
202                 if (pgoff != linear_page_index(vma, start) &&
203                     !(vma->vm_flags & VM_NONLINEAR)) {
204                         if (!has_write_lock) {
205                                 up_read(&mm->mmap_sem);
206                                 down_write(&mm->mmap_sem);
207                                 has_write_lock = 1;
208                                 goto retry;
209                         }
210                         mapping = vma->vm_file->f_mapping;
211                         spin_lock(&mapping->i_mmap_lock);
212                         flush_dcache_mmap_lock(mapping);
213                         vma->vm_flags |= VM_NONLINEAR;
214                         vma_prio_tree_remove(vma, &mapping->i_mmap);
215                         vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
216                         flush_dcache_mmap_unlock(mapping);
217                         spin_unlock(&mapping->i_mmap_lock);
218                 }
219
220                 err = vma->vm_ops->populate(vma, start, size,
221                                             vma->vm_page_prot,
222                                             pgoff, flags & MAP_NONBLOCK);
223
224                 /*
225                  * We can't clear VM_NONLINEAR because we'd have to do
226                  * it after ->populate completes, and that would prevent
227                  * downgrading the lock.  (Locks can't be upgraded).
228                  */
229         }
230         if (likely(!has_write_lock))
231                 up_read(&mm->mmap_sem);
232         else
233                 up_write(&mm->mmap_sem);
234
235         return err;
236 }
237