Merge bk://kernel.bkbits.net/davem/net-2.5
[linux-2.6/history.git] / mm / fremap.c
blobf886ba1b4f7febe3ec0e0552b5dde2fc170b9d33
1 /*
2 * linux/mm/fremap.c
3 *
4 * Explicit pagetable population and nonlinear (random) mappings support.
6 * started by Ingo Molnar, Copyright (C) 2002, 2003
7 */
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-locking.h>
16 #include <linux/module.h>
18 #include <asm/mmu_context.h>
19 #include <asm/cacheflush.h>
20 #include <asm/tlbflush.h>
22 static inline int zap_pte(struct mm_struct *mm, struct vm_area_struct *vma,
23 unsigned long addr, pte_t *ptep)
25 pte_t pte = *ptep;
27 if (pte_none(pte))
28 return 0;
29 if (pte_present(pte)) {
30 unsigned long pfn = pte_pfn(pte);
32 flush_cache_page(vma, addr);
33 pte = ptep_get_and_clear(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--;
44 return 1;
45 } else {
46 if (!pte_file(pte))
47 free_swap_and_cache(pte_to_swp_entry(pte));
48 pte_clear(ptep);
49 return 0;
54 * Install a page to a given virtual memory address, release any
55 * previously existing mapping.
57 int install_page(struct mm_struct *mm, struct vm_area_struct *vma,
58 unsigned long addr, struct page *page, pgprot_t prot)
60 int err = -ENOMEM, flush;
61 pte_t *pte;
62 pgd_t *pgd;
63 pmd_t *pmd;
64 pte_t pte_val;
65 struct pte_chain *pte_chain;
67 pte_chain = pte_chain_alloc(GFP_KERNEL);
68 if (!pte_chain)
69 goto err;
70 pgd = pgd_offset(mm, addr);
71 spin_lock(&mm->page_table_lock);
73 pmd = pmd_alloc(mm, pgd, addr);
74 if (!pmd)
75 goto err_unlock;
77 pte = pte_alloc_map(mm, pmd, addr);
78 if (!pte)
79 goto err_unlock;
81 flush = zap_pte(mm, vma, addr, pte);
83 mm->rss++;
84 flush_icache_page(vma, page);
85 set_pte(pte, mk_pte(page, prot));
86 pte_chain = page_add_rmap(page, pte, pte_chain);
87 pte_val = *pte;
88 pte_unmap(pte);
89 if (flush)
90 flush_tlb_page(vma, addr);
91 update_mmu_cache(vma, addr, pte_val);
92 spin_unlock(&mm->page_table_lock);
93 pte_chain_free(pte_chain);
94 return 0;
96 err_unlock:
97 spin_unlock(&mm->page_table_lock);
98 pte_chain_free(pte_chain);
99 err:
100 return err;
102 EXPORT_SYMBOL(install_page);
106 * Install a file pte to a given virtual memory address, release any
107 * previously existing mapping.
109 int install_file_pte(struct mm_struct *mm, struct vm_area_struct *vma,
110 unsigned long addr, unsigned long pgoff, pgprot_t prot)
112 int err = -ENOMEM, flush;
113 pte_t *pte;
114 pgd_t *pgd;
115 pmd_t *pmd;
116 pte_t pte_val;
118 pgd = pgd_offset(mm, addr);
119 spin_lock(&mm->page_table_lock);
121 pmd = pmd_alloc(mm, pgd, addr);
122 if (!pmd)
123 goto err_unlock;
125 pte = pte_alloc_map(mm, pmd, addr);
126 if (!pte)
127 goto err_unlock;
129 flush = zap_pte(mm, vma, addr, pte);
131 set_pte(pte, pgoff_to_pte(pgoff));
132 pte_val = *pte;
133 pte_unmap(pte);
134 if (flush)
135 flush_tlb_page(vma, addr);
136 update_mmu_cache(vma, addr, pte_val);
137 spin_unlock(&mm->page_table_lock);
138 return 0;
140 err_unlock:
141 spin_unlock(&mm->page_table_lock);
142 return err;
146 /***
147 * sys_remap_file_pages - remap arbitrary pages of a shared backing store
148 * file within an existing vma.
149 * @start: start of the remapped virtual memory range
150 * @size: size of the remapped virtual memory range
151 * @prot: new protection bits of the range
152 * @pgoff: to be mapped page of the backing store file
153 * @flags: 0 or MAP_NONBLOCKED - the later will cause no IO.
155 * this syscall works purely via pagetables, so it's the most efficient
156 * way to map the same (large) file into a given virtual window. Unlike
157 * mmap()/mremap() it does not create any new vmas. The new mappings are
158 * also safe across swapout.
160 * NOTE: the 'prot' parameter right now is ignored, and the vma's default
161 * protection is used. Arbitrary protections might be implemented in the
162 * future.
164 long sys_remap_file_pages(unsigned long start, unsigned long size,
165 unsigned long __prot, unsigned long pgoff, unsigned long flags)
167 struct mm_struct *mm = current->mm;
168 unsigned long end = start + size;
169 struct vm_area_struct *vma;
170 int err = -EINVAL;
172 if (__prot)
173 return err;
175 * Sanitize the syscall parameters:
177 start = start & PAGE_MASK;
178 size = size & PAGE_MASK;
180 /* Does the address range wrap, or is the span zero-sized? */
181 if (start + size <= start)
182 return err;
184 /* Can we represent this offset inside this architecture's pte's? */
185 #if PTE_FILE_MAX_BITS < BITS_PER_LONG
186 if (pgoff + (size >> PAGE_SHIFT) >= (1UL << PTE_FILE_MAX_BITS))
187 return err;
188 #endif
190 /* We need down_write() to change vma->vm_flags. */
191 down_write(&mm->mmap_sem);
192 vma = find_vma(mm, start);
195 * Make sure the vma is shared, that it supports prefaulting,
196 * and that the remapped range is valid and fully within
197 * the single existing vma:
199 if (vma && (vma->vm_flags & VM_SHARED) &&
200 vma->vm_ops && vma->vm_ops->populate &&
201 end > start && start >= vma->vm_start &&
202 end <= vma->vm_end) {
204 /* Must set VM_NONLINEAR before any pages are populated. */
205 if (pgoff != ((start - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff)
206 vma->vm_flags |= VM_NONLINEAR;
208 /* ->populate can take a long time, so downgrade the lock. */
209 downgrade_write(&mm->mmap_sem);
210 err = vma->vm_ops->populate(vma, start, size,
211 vma->vm_page_prot,
212 pgoff, flags & MAP_NONBLOCK);
215 * We can't clear VM_NONLINEAR because we'd have to do
216 * it after ->populate completes, and that would prevent
217 * downgrading the lock. (Locks can't be upgraded).
219 up_read(&mm->mmap_sem);
220 } else {
221 up_write(&mm->mmap_sem);
224 return err;