8eefa7a6bab364bd6a50f9f20a1485d65f7baec6
[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);
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 file 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
64         pgd = pgd_offset(mm, addr);
65         spin_lock(&mm->page_table_lock);
66
67         pmd = pmd_alloc(mm, pgd, addr);
68         if (!pmd)
69                 goto err_unlock;
70
71         pte = pte_alloc_map(mm, pmd, addr);
72         if (!pte)
73                 goto err_unlock;
74
75         zap_pte(mm, vma, addr, pte);
76
77         mm->rss++;
78         flush_icache_page(vma, page);
79         set_pte(pte, mk_pte(page, prot));
80         page_add_file_rmap(page);
81         pte_val = *pte;
82         pte_unmap(pte);
83         update_mmu_cache(vma, addr, pte_val);
84
85         err = 0;
86 err_unlock:
87         spin_unlock(&mm->page_table_lock);
88         return err;
89 }
90 EXPORT_SYMBOL(install_page);
91
92
93 /*
94  * Install a file pte to a given virtual memory address, release any
95  * previously existing mapping.
96  */
97 int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma,
98                 unsigned long addr, unsigned long pgoff, pgprot_t prot)
99 {
100         int err = -ENOMEM;
101         pte_t *pte;
102         pgd_t *pgd;
103         pmd_t *pmd;
104         pte_t pte_val;
105
106         pgd = pgd_offset(mm, addr);
107         spin_lock(&mm->page_table_lock);
108
109         pmd = pmd_alloc(mm, pgd, addr);
110         if (!pmd)
111                 goto err_unlock;
112
113         pte = pte_alloc_map(mm, pmd, addr);
114         if (!pte)
115                 goto err_unlock;
116
117         zap_pte(mm, vma, addr, pte);
118
119         set_pte(pte, pgoff_to_pte(pgoff));
120         pte_val = *pte;
121         pte_unmap(pte);
122         update_mmu_cache(vma, addr, pte_val);
123         spin_unlock(&mm->page_table_lock);
124         return 0;
125
126 err_unlock:
127         spin_unlock(&mm->page_table_lock);
128         return err;
129 }
130
131
132 /***
133  * sys_remap_file_pages - remap arbitrary pages of a shared backing store
134  *                        file within an existing vma.
135  * @start: start of the remapped virtual memory range
136  * @size: size of the remapped virtual memory range
137  * @prot: new protection bits of the range
138  * @pgoff: to be mapped page of the backing store file
139  * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO.
140  *
141  * this syscall works purely via pagetables, so it's the most efficient
142  * way to map the same (large) file into a given virtual window. Unlike
143  * mmap()/mremap() it does not create any new vmas. The new mappings are
144  * also safe across swapout.
145  *
146  * NOTE: the 'prot' parameter right now is ignored, and the vma's default
147  * protection is used. Arbitrary protections might be implemented in the
148  * future.
149  */
150 asmlinkage long sys_remap_file_pages(unsigned long start, unsigned long size,
151         unsigned long __prot, unsigned long pgoff, unsigned long flags)
152 {
153         struct mm_struct *mm = current->mm;
154         struct address_space *mapping;
155         unsigned long end = start + size;
156         struct vm_area_struct *vma;
157         int err = -EINVAL;
158
159         if (__prot)
160                 return err;
161         /*
162          * Sanitize the syscall parameters:
163          */
164         start = start & PAGE_MASK;
165         size = size & PAGE_MASK;
166
167         /* Does the address range wrap, or is the span zero-sized? */
168         if (start + size <= start)
169                 return err;
170
171         /* Can we represent this offset inside this architecture's pte's? */
172 #if PTE_FILE_MAX_BITS < BITS_PER_LONG
173         if (pgoff + (size >> PAGE_SHIFT) >= (1UL << PTE_FILE_MAX_BITS))
174                 return err;
175 #endif
176
177         /* We need down_write() to change vma->vm_flags. */
178         down_write(&mm->mmap_sem);
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 (unless VM_RESERVED
186          * or VM_LOCKED, but VM_LOCKED could be revoked later on).
187          */
188         if (vma && (vma->vm_flags & VM_SHARED) &&
189                 (!vma->vm_private_data || (vma->vm_flags & VM_RESERVED)) &&
190                 vma->vm_ops && vma->vm_ops->populate &&
191                         end > start && start >= vma->vm_start &&
192                                 end <= vma->vm_end) {
193
194                 /* Must set VM_NONLINEAR before any pages are populated. */
195                 if (pgoff != linear_page_index(vma, start) &&
196                     !(vma->vm_flags & VM_NONLINEAR)) {
197                         mapping = vma->vm_file->f_mapping;
198                         spin_lock(&mapping->i_mmap_lock);
199                         flush_dcache_mmap_lock(mapping);
200                         vma->vm_flags |= VM_NONLINEAR;
201                         vma_prio_tree_remove(vma, &mapping->i_mmap);
202                         vma_prio_tree_init(vma);
203                         list_add_tail(&vma->shared.vm_set.list,
204                                         &mapping->i_mmap_nonlinear);
205                         flush_dcache_mmap_unlock(mapping);
206                         spin_unlock(&mapping->i_mmap_lock);
207                 }
208
209                 /* ->populate can take a long time, so downgrade the lock. */
210                 downgrade_write(&mm->mmap_sem);
211                 err = vma->vm_ops->populate(vma, start, size,
212                                             vma->vm_page_prot,
213                                             pgoff, flags & MAP_NONBLOCK);
214
215                 /*
216                  * We can't clear VM_NONLINEAR because we'd have to do
217                  * it after ->populate completes, and that would prevent
218                  * downgrading the lock.  (Locks can't be upgraded).
219                  */
220                 up_read(&mm->mmap_sem);
221         } else {
222                 up_write(&mm->mmap_sem);
223         }
224
225         return err;
226 }
227