2c8abe6d1f5a383c5ebb6b8ecea19eded27cd5cc
[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
18 #include <asm/mmu_context.h>
19 #include <asm/cacheflush.h>
20 #include <asm/tlbflush.h>
21
22 static inline void zap_pte(struct mm_struct *mm, struct vm_area_struct *vma,
23                         unsigned long addr, pte_t *ptep)
24 {
25         pte_t pte = *ptep;
26
27         if (pte_none(pte))
28                 return;
29         if (pte_present(pte)) {
30                 unsigned long pfn = pte_pfn(pte);
31
32                 flush_cache_page(vma, addr);
33                 pte = ptep_clear_flush(vma, addr, ptep);
34                 if (pfn_valid(pfn)) {
35                         struct page *page = pfn_to_page(pfn);
36                         if (!PageReserved(page)) {
37                                 if (pte_dirty(pte))
38                                         set_page_dirty(page);
39                                 page_remove_rmap(page, ptep);
40                                 page_cache_release(page);
41                                 mm->rss--;
42                         }
43                 }
44         } else {
45                 if (!pte_file(pte))
46                         free_swap_and_cache(pte_to_swp_entry(pte));
47                 pte_clear(ptep);
48         }
49 }
50
51 /*
52  * Install a page to a given virtual memory address, release any
53  * previously existing mapping.
54  */
55 int install_page(struct mm_struct *mm, struct vm_area_struct *vma,
56                 unsigned long addr, struct page *page, pgprot_t prot)
57 {
58         int err = -ENOMEM;
59         pte_t *pte;
60         pgd_t *pgd;
61         pmd_t *pmd;
62         pte_t pte_val;
63         struct pte_chain *pte_chain;
64
65         pte_chain = pte_chain_alloc(GFP_KERNEL);
66         if (!pte_chain)
67                 goto err;
68         pgd = pgd_offset(mm, addr);
69         spin_lock(&mm->page_table_lock);
70
71         pmd = pmd_alloc(mm, pgd, addr);
72         if (!pmd)
73                 goto err_unlock;
74
75         pte = pte_alloc_map(mm, pmd, addr);
76         if (!pte)
77                 goto err_unlock;
78
79         zap_pte(mm, vma, addr, pte);
80
81         mm->rss++;
82         flush_icache_page(vma, page);
83         set_pte(pte, mk_pte(page, prot));
84         pte_chain = page_add_rmap(page, pte, pte_chain);
85         pte_val = *pte;
86         pte_unmap(pte);
87         update_mmu_cache(vma, addr, pte_val);
88         spin_unlock(&mm->page_table_lock);
89         pte_chain_free(pte_chain);
90         return 0;
91
92 err_unlock:
93         spin_unlock(&mm->page_table_lock);
94         pte_chain_free(pte_chain);
95 err:
96         return err;
97 }
98 EXPORT_SYMBOL(install_page);
99
100
101 /*
102  * Install a file pte to a given virtual memory address, release any
103  * previously existing mapping.
104  */
105 int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma,
106                 unsigned long addr, unsigned long pgoff, pgprot_t prot)
107 {
108         int err = -ENOMEM;
109         pte_t *pte;
110         pgd_t *pgd;
111         pmd_t *pmd;
112         pte_t pte_val;
113
114         pgd = pgd_offset(mm, addr);
115         spin_lock(&mm->page_table_lock);
116
117         pmd = pmd_alloc(mm, pgd, addr);
118         if (!pmd)
119                 goto err_unlock;
120
121         pte = pte_alloc_map(mm, pmd, addr);
122         if (!pte)
123                 goto err_unlock;
124
125         zap_pte(mm, vma, addr, pte);
126
127         set_pte(pte, pgoff_to_pte(pgoff));
128         pte_val = *pte;
129         pte_unmap(pte);
130         update_mmu_cache(vma, addr, pte_val);
131         spin_unlock(&mm->page_table_lock);
132         return 0;
133
134 err_unlock:
135         spin_unlock(&mm->page_table_lock);
136         return err;
137 }
138
139
140 /***
141  * sys_remap_file_pages - remap arbitrary pages of a shared backing store
142  *                        file within an existing vma.
143  * @start: start of the remapped virtual memory range
144  * @size: size of the remapped virtual memory range
145  * @prot: new protection bits of the range
146  * @pgoff: to be mapped page of the backing store file
147  * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO.
148  *
149  * this syscall works purely via pagetables, so it's the most efficient
150  * way to map the same (large) file into a given virtual window. Unlike
151  * mmap()/mremap() it does not create any new vmas. The new mappings are
152  * also safe across swapout.
153  *
154  * NOTE: the 'prot' parameter right now is ignored, and the vma's default
155  * protection is used. Arbitrary protections might be implemented in the
156  * future.
157  */
158 asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
159         unsigned long __prot, unsigned long pgoff, unsigned long flags)
160 {
161         struct mm_struct *mm = current->mm;
162         unsigned long end = start + size;
163         struct vm_area_struct *vma;
164         int err = -EINVAL;
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_write(&mm->mmap_sem);
186         vma = find_vma(mm, start);
187
188         /*
189          * Make sure the vma is shared, that it supports prefaulting,
190          * and that the remapped range is valid and fully within
191          * the single existing vma:
192          */
193         if (vma && (vma->vm_flags & VM_SHARED) &&
194                 vma->vm_ops && vma->vm_ops->populate &&
195                         end > start && start >= vma->vm_start &&
196                                 end <= vma->vm_end) {
197
198                 /* Must set VM_NONLINEAR before any pages are populated. */
199                 if (pgoff != ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff)
200                         vma->vm_flags |= VM_NONLINEAR;
201
202                 /* ->populate can take a long time, so downgrade the lock. */
203                 downgrade_write(&mm->mmap_sem);
204                 err = vma->vm_ops->populate(vma, start, size,
205                                             vma->vm_page_prot,
206                                             pgoff, flags & MAP_NONBLOCK);
207
208                 /*
209                  * We can't clear VM_NONLINEAR because we'd have to do
210                  * it after ->populate completes, and that would prevent
211                  * downgrading the lock.  (Locks can't be upgraded).
212                  */
213                 up_read(&mm->mmap_sem);
214         } else {
215                 up_write(&mm->mmap_sem);
216         }
217
218         return err;
219 }
220