Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * IA-32 Huge TLB Page Support for Kernel. |
| 3 | * |
| 4 | * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com> |
| 5 | */ |
| 6 | |
| 7 | #include <linux/config.h> |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/fs.h> |
| 10 | #include <linux/mm.h> |
| 11 | #include <linux/hugetlb.h> |
| 12 | #include <linux/pagemap.h> |
| 13 | #include <linux/smp_lock.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/sysctl.h> |
| 17 | #include <asm/mman.h> |
| 18 | #include <asm/tlb.h> |
| 19 | #include <asm/tlbflush.h> |
| 20 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 21 | pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | { |
| 23 | pgd_t *pgd; |
| 24 | pud_t *pud; |
| 25 | pmd_t *pmd = NULL; |
| 26 | |
| 27 | pgd = pgd_offset(mm, addr); |
| 28 | pud = pud_alloc(mm, pgd, addr); |
| 29 | pmd = pmd_alloc(mm, pud, addr); |
| 30 | return (pte_t *) pmd; |
| 31 | } |
| 32 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 33 | pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
| 35 | pgd_t *pgd; |
| 36 | pud_t *pud; |
| 37 | pmd_t *pmd = NULL; |
| 38 | |
| 39 | pgd = pgd_offset(mm, addr); |
| 40 | pud = pud_offset(pgd, addr); |
| 41 | pmd = pmd_offset(pud, addr); |
| 42 | return (pte_t *) pmd; |
| 43 | } |
| 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | /* |
| 46 | * This function checks for proper alignment of input addr and len parameters. |
| 47 | */ |
| 48 | int is_aligned_hugepage_range(unsigned long addr, unsigned long len) |
| 49 | { |
| 50 | if (len & ~HPAGE_MASK) |
| 51 | return -EINVAL; |
| 52 | if (addr & ~HPAGE_MASK) |
| 53 | return -EINVAL; |
| 54 | return 0; |
| 55 | } |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #if 0 /* This is just for testing */ |
| 58 | struct page * |
| 59 | follow_huge_addr(struct mm_struct *mm, unsigned long address, int write) |
| 60 | { |
| 61 | unsigned long start = address; |
| 62 | int length = 1; |
| 63 | int nr; |
| 64 | struct page *page; |
| 65 | struct vm_area_struct *vma; |
| 66 | |
| 67 | vma = find_vma(mm, addr); |
| 68 | if (!vma || !is_vm_hugetlb_page(vma)) |
| 69 | return ERR_PTR(-EINVAL); |
| 70 | |
| 71 | pte = huge_pte_offset(mm, address); |
| 72 | |
| 73 | /* hugetlb should be locked, and hence, prefaulted */ |
| 74 | WARN_ON(!pte || pte_none(*pte)); |
| 75 | |
| 76 | page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)]; |
| 77 | |
| 78 | WARN_ON(!PageCompound(page)); |
| 79 | |
| 80 | return page; |
| 81 | } |
| 82 | |
| 83 | int pmd_huge(pmd_t pmd) |
| 84 | { |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | struct page * |
| 89 | follow_huge_pmd(struct mm_struct *mm, unsigned long address, |
| 90 | pmd_t *pmd, int write) |
| 91 | { |
| 92 | return NULL; |
| 93 | } |
| 94 | |
| 95 | #else |
| 96 | |
| 97 | struct page * |
| 98 | follow_huge_addr(struct mm_struct *mm, unsigned long address, int write) |
| 99 | { |
| 100 | return ERR_PTR(-EINVAL); |
| 101 | } |
| 102 | |
| 103 | int pmd_huge(pmd_t pmd) |
| 104 | { |
| 105 | return !!(pmd_val(pmd) & _PAGE_PSE); |
| 106 | } |
| 107 | |
| 108 | struct page * |
| 109 | follow_huge_pmd(struct mm_struct *mm, unsigned long address, |
| 110 | pmd_t *pmd, int write) |
| 111 | { |
| 112 | struct page *page; |
| 113 | |
| 114 | page = pte_page(*(pte_t *)pmd); |
| 115 | if (page) |
| 116 | page += ((address & ~HPAGE_MASK) >> PAGE_SHIFT); |
| 117 | return page; |
| 118 | } |
| 119 | #endif |
| 120 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 121 | void hugetlb_clean_stale_pgtable(pte_t *pte) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | { |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 123 | pmd_t *pmd = (pmd_t *) pte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | struct page *page; |
| 125 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 126 | page = pmd_page(*pmd); |
| 127 | pmd_clear(pmd); |
| 128 | dec_page_state(nr_page_table_pages); |
| 129 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /* x86_64 also uses this file */ |
| 133 | |
| 134 | #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA |
| 135 | static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file, |
| 136 | unsigned long addr, unsigned long len, |
| 137 | unsigned long pgoff, unsigned long flags) |
| 138 | { |
| 139 | struct mm_struct *mm = current->mm; |
| 140 | struct vm_area_struct *vma; |
| 141 | unsigned long start_addr; |
| 142 | |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 143 | if (len > mm->cached_hole_size) { |
| 144 | start_addr = mm->free_area_cache; |
| 145 | } else { |
| 146 | start_addr = TASK_UNMAPPED_BASE; |
| 147 | mm->cached_hole_size = 0; |
| 148 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
| 150 | full_search: |
| 151 | addr = ALIGN(start_addr, HPAGE_SIZE); |
| 152 | |
| 153 | for (vma = find_vma(mm, addr); ; vma = vma->vm_next) { |
| 154 | /* At this point: (!vma || addr < vma->vm_end). */ |
| 155 | if (TASK_SIZE - len < addr) { |
| 156 | /* |
| 157 | * Start a new search - just in case we missed |
| 158 | * some holes. |
| 159 | */ |
| 160 | if (start_addr != TASK_UNMAPPED_BASE) { |
| 161 | start_addr = TASK_UNMAPPED_BASE; |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 162 | mm->cached_hole_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | goto full_search; |
| 164 | } |
| 165 | return -ENOMEM; |
| 166 | } |
| 167 | if (!vma || addr + len <= vma->vm_start) { |
| 168 | mm->free_area_cache = addr + len; |
| 169 | return addr; |
| 170 | } |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 171 | if (addr + mm->cached_hole_size < vma->vm_start) |
| 172 | mm->cached_hole_size = vma->vm_start - addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | addr = ALIGN(vma->vm_end, HPAGE_SIZE); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file, |
| 178 | unsigned long addr0, unsigned long len, |
| 179 | unsigned long pgoff, unsigned long flags) |
| 180 | { |
| 181 | struct mm_struct *mm = current->mm; |
| 182 | struct vm_area_struct *vma, *prev_vma; |
| 183 | unsigned long base = mm->mmap_base, addr = addr0; |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 184 | unsigned long largest_hole = mm->cached_hole_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | int first_time = 1; |
| 186 | |
| 187 | /* don't allow allocations above current base */ |
| 188 | if (mm->free_area_cache > base) |
| 189 | mm->free_area_cache = base; |
| 190 | |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 191 | if (len <= largest_hole) { |
| 192 | largest_hole = 0; |
| 193 | mm->free_area_cache = base; |
| 194 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | try_again: |
| 196 | /* make sure it can fit in the remaining address space */ |
| 197 | if (mm->free_area_cache < len) |
| 198 | goto fail; |
| 199 | |
| 200 | /* either no address requested or cant fit in requested address hole */ |
| 201 | addr = (mm->free_area_cache - len) & HPAGE_MASK; |
| 202 | do { |
| 203 | /* |
| 204 | * Lookup failure means no vma is above this address, |
| 205 | * i.e. return with success: |
| 206 | */ |
| 207 | if (!(vma = find_vma_prev(mm, addr, &prev_vma))) |
| 208 | return addr; |
| 209 | |
| 210 | /* |
| 211 | * new region fits between prev_vma->vm_end and |
| 212 | * vma->vm_start, use it: |
| 213 | */ |
| 214 | if (addr + len <= vma->vm_start && |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 215 | (!prev_vma || (addr >= prev_vma->vm_end))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | /* remember the address as a hint for next time */ |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 217 | mm->cached_hole_size = largest_hole; |
| 218 | return (mm->free_area_cache = addr); |
| 219 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | /* pull free_area_cache down to the first hole */ |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 221 | if (mm->free_area_cache == vma->vm_end) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | mm->free_area_cache = vma->vm_start; |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 223 | mm->cached_hole_size = largest_hole; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /* remember the largest hole we saw so far */ |
| 228 | if (addr + largest_hole < vma->vm_start) |
| 229 | largest_hole = vma->vm_start - addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
| 231 | /* try just below the current vma->vm_start */ |
| 232 | addr = (vma->vm_start - len) & HPAGE_MASK; |
| 233 | } while (len <= vma->vm_start); |
| 234 | |
| 235 | fail: |
| 236 | /* |
| 237 | * if hint left us with no space for the requested |
| 238 | * mapping then try again: |
| 239 | */ |
| 240 | if (first_time) { |
| 241 | mm->free_area_cache = base; |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 242 | largest_hole = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | first_time = 0; |
| 244 | goto try_again; |
| 245 | } |
| 246 | /* |
| 247 | * A failed mmap() very likely causes application failure, |
| 248 | * so fall back to the bottom-up function here. This scenario |
| 249 | * can happen with large stack limits and large mmap() |
| 250 | * allocations. |
| 251 | */ |
| 252 | mm->free_area_cache = TASK_UNMAPPED_BASE; |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 253 | mm->cached_hole_size = ~0UL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | addr = hugetlb_get_unmapped_area_bottomup(file, addr0, |
| 255 | len, pgoff, flags); |
| 256 | |
| 257 | /* |
| 258 | * Restore the topdown base: |
| 259 | */ |
| 260 | mm->free_area_cache = base; |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 261 | mm->cached_hole_size = ~0UL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
| 263 | return addr; |
| 264 | } |
| 265 | |
| 266 | unsigned long |
| 267 | hugetlb_get_unmapped_area(struct file *file, unsigned long addr, |
| 268 | unsigned long len, unsigned long pgoff, unsigned long flags) |
| 269 | { |
| 270 | struct mm_struct *mm = current->mm; |
| 271 | struct vm_area_struct *vma; |
| 272 | |
| 273 | if (len & ~HPAGE_MASK) |
| 274 | return -EINVAL; |
| 275 | if (len > TASK_SIZE) |
| 276 | return -ENOMEM; |
| 277 | |
| 278 | if (addr) { |
| 279 | addr = ALIGN(addr, HPAGE_SIZE); |
| 280 | vma = find_vma(mm, addr); |
| 281 | if (TASK_SIZE - len >= addr && |
| 282 | (!vma || addr + len <= vma->vm_start)) |
| 283 | return addr; |
| 284 | } |
| 285 | if (mm->get_unmapped_area == arch_get_unmapped_area) |
| 286 | return hugetlb_get_unmapped_area_bottomup(file, addr, len, |
| 287 | pgoff, flags); |
| 288 | else |
| 289 | return hugetlb_get_unmapped_area_topdown(file, addr, len, |
| 290 | pgoff, flags); |
| 291 | } |
| 292 | |
| 293 | #endif /*HAVE_ARCH_HUGETLB_UNMAPPED_AREA*/ |
| 294 | |