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