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