Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Generic hugetlb support. |
| 3 | * (C) William Irwin, April 2004 |
| 4 | */ |
| 5 | #include <linux/gfp.h> |
| 6 | #include <linux/list.h> |
| 7 | #include <linux/init.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/sysctl.h> |
| 11 | #include <linux/highmem.h> |
| 12 | #include <linux/nodemask.h> |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 13 | #include <linux/pagemap.h> |
Christoph Lameter | 5da7ca8 | 2006-01-06 00:10:46 -0800 | [diff] [blame^] | 14 | #include <linux/mempolicy.h> |
| 15 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 16 | #include <asm/page.h> |
| 17 | #include <asm/pgtable.h> |
| 18 | |
| 19 | #include <linux/hugetlb.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL; |
| 22 | static unsigned long nr_huge_pages, free_huge_pages; |
| 23 | unsigned long max_huge_pages; |
| 24 | static struct list_head hugepage_freelists[MAX_NUMNODES]; |
| 25 | static unsigned int nr_huge_pages_node[MAX_NUMNODES]; |
| 26 | static unsigned int free_huge_pages_node[MAX_NUMNODES]; |
Eric Paris | 0bd0f9f | 2005-11-21 21:32:28 -0800 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages |
| 30 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | static DEFINE_SPINLOCK(hugetlb_lock); |
| 32 | |
| 33 | static void enqueue_huge_page(struct page *page) |
| 34 | { |
| 35 | int nid = page_to_nid(page); |
| 36 | list_add(&page->lru, &hugepage_freelists[nid]); |
| 37 | free_huge_pages++; |
| 38 | free_huge_pages_node[nid]++; |
| 39 | } |
| 40 | |
Christoph Lameter | 5da7ca8 | 2006-01-06 00:10:46 -0800 | [diff] [blame^] | 41 | static struct page *dequeue_huge_page(struct vm_area_struct *vma, |
| 42 | unsigned long address) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | { |
| 44 | int nid = numa_node_id(); |
| 45 | struct page *page = NULL; |
Christoph Lameter | 5da7ca8 | 2006-01-06 00:10:46 -0800 | [diff] [blame^] | 46 | struct zonelist *zonelist = huge_zonelist(vma, address); |
Christoph Lameter | 96df933 | 2006-01-06 00:10:45 -0800 | [diff] [blame] | 47 | struct zone **z; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Christoph Lameter | 96df933 | 2006-01-06 00:10:45 -0800 | [diff] [blame] | 49 | for (z = zonelist->zones; *z; z++) { |
| 50 | nid = (*z)->zone_pgdat->node_id; |
| 51 | if (!list_empty(&hugepage_freelists[nid])) |
| 52 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
Christoph Lameter | 96df933 | 2006-01-06 00:10:45 -0800 | [diff] [blame] | 54 | |
| 55 | if (*z) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | page = list_entry(hugepage_freelists[nid].next, |
| 57 | struct page, lru); |
| 58 | list_del(&page->lru); |
| 59 | free_huge_pages--; |
| 60 | free_huge_pages_node[nid]--; |
| 61 | } |
| 62 | return page; |
| 63 | } |
| 64 | |
| 65 | static struct page *alloc_fresh_huge_page(void) |
| 66 | { |
| 67 | static int nid = 0; |
| 68 | struct page *page; |
| 69 | page = alloc_pages_node(nid, GFP_HIGHUSER|__GFP_COMP|__GFP_NOWARN, |
| 70 | HUGETLB_PAGE_ORDER); |
| 71 | nid = (nid + 1) % num_online_nodes(); |
| 72 | if (page) { |
Eric Paris | 0bd0f9f | 2005-11-21 21:32:28 -0800 | [diff] [blame] | 73 | spin_lock(&hugetlb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | nr_huge_pages++; |
| 75 | nr_huge_pages_node[page_to_nid(page)]++; |
Eric Paris | 0bd0f9f | 2005-11-21 21:32:28 -0800 | [diff] [blame] | 76 | spin_unlock(&hugetlb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
| 78 | return page; |
| 79 | } |
| 80 | |
| 81 | void free_huge_page(struct page *page) |
| 82 | { |
| 83 | BUG_ON(page_count(page)); |
| 84 | |
| 85 | INIT_LIST_HEAD(&page->lru); |
| 86 | page[1].mapping = NULL; |
| 87 | |
| 88 | spin_lock(&hugetlb_lock); |
| 89 | enqueue_huge_page(page); |
| 90 | spin_unlock(&hugetlb_lock); |
| 91 | } |
| 92 | |
Christoph Lameter | 5da7ca8 | 2006-01-06 00:10:46 -0800 | [diff] [blame^] | 93 | struct page *alloc_huge_page(struct vm_area_struct *vma, unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { |
| 95 | struct page *page; |
| 96 | int i; |
| 97 | |
| 98 | spin_lock(&hugetlb_lock); |
Christoph Lameter | 5da7ca8 | 2006-01-06 00:10:46 -0800 | [diff] [blame^] | 99 | page = dequeue_huge_page(vma, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | if (!page) { |
| 101 | spin_unlock(&hugetlb_lock); |
| 102 | return NULL; |
| 103 | } |
| 104 | spin_unlock(&hugetlb_lock); |
| 105 | set_page_count(page, 1); |
| 106 | page[1].mapping = (void *)free_huge_page; |
| 107 | for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); ++i) |
| 108 | clear_highpage(&page[i]); |
| 109 | return page; |
| 110 | } |
| 111 | |
| 112 | static int __init hugetlb_init(void) |
| 113 | { |
| 114 | unsigned long i; |
| 115 | struct page *page; |
| 116 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 117 | if (HPAGE_SHIFT == 0) |
| 118 | return 0; |
| 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | for (i = 0; i < MAX_NUMNODES; ++i) |
| 121 | INIT_LIST_HEAD(&hugepage_freelists[i]); |
| 122 | |
| 123 | for (i = 0; i < max_huge_pages; ++i) { |
| 124 | page = alloc_fresh_huge_page(); |
| 125 | if (!page) |
| 126 | break; |
| 127 | spin_lock(&hugetlb_lock); |
| 128 | enqueue_huge_page(page); |
| 129 | spin_unlock(&hugetlb_lock); |
| 130 | } |
| 131 | max_huge_pages = free_huge_pages = nr_huge_pages = i; |
| 132 | printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages); |
| 133 | return 0; |
| 134 | } |
| 135 | module_init(hugetlb_init); |
| 136 | |
| 137 | static int __init hugetlb_setup(char *s) |
| 138 | { |
| 139 | if (sscanf(s, "%lu", &max_huge_pages) <= 0) |
| 140 | max_huge_pages = 0; |
| 141 | return 1; |
| 142 | } |
| 143 | __setup("hugepages=", hugetlb_setup); |
| 144 | |
| 145 | #ifdef CONFIG_SYSCTL |
| 146 | static void update_and_free_page(struct page *page) |
| 147 | { |
| 148 | int i; |
| 149 | nr_huge_pages--; |
| 150 | nr_huge_pages_node[page_zone(page)->zone_pgdat->node_id]--; |
| 151 | for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) { |
| 152 | page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced | |
| 153 | 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved | |
| 154 | 1 << PG_private | 1<< PG_writeback); |
| 155 | set_page_count(&page[i], 0); |
| 156 | } |
| 157 | set_page_count(page, 1); |
| 158 | __free_pages(page, HUGETLB_PAGE_ORDER); |
| 159 | } |
| 160 | |
| 161 | #ifdef CONFIG_HIGHMEM |
| 162 | static void try_to_free_low(unsigned long count) |
| 163 | { |
| 164 | int i, nid; |
| 165 | for (i = 0; i < MAX_NUMNODES; ++i) { |
| 166 | struct page *page, *next; |
| 167 | list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) { |
| 168 | if (PageHighMem(page)) |
| 169 | continue; |
| 170 | list_del(&page->lru); |
| 171 | update_and_free_page(page); |
| 172 | nid = page_zone(page)->zone_pgdat->node_id; |
| 173 | free_huge_pages--; |
| 174 | free_huge_pages_node[nid]--; |
| 175 | if (count >= nr_huge_pages) |
| 176 | return; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | #else |
| 181 | static inline void try_to_free_low(unsigned long count) |
| 182 | { |
| 183 | } |
| 184 | #endif |
| 185 | |
| 186 | static unsigned long set_max_huge_pages(unsigned long count) |
| 187 | { |
| 188 | while (count > nr_huge_pages) { |
| 189 | struct page *page = alloc_fresh_huge_page(); |
| 190 | if (!page) |
| 191 | return nr_huge_pages; |
| 192 | spin_lock(&hugetlb_lock); |
| 193 | enqueue_huge_page(page); |
| 194 | spin_unlock(&hugetlb_lock); |
| 195 | } |
| 196 | if (count >= nr_huge_pages) |
| 197 | return nr_huge_pages; |
| 198 | |
| 199 | spin_lock(&hugetlb_lock); |
| 200 | try_to_free_low(count); |
| 201 | while (count < nr_huge_pages) { |
Christoph Lameter | 5da7ca8 | 2006-01-06 00:10:46 -0800 | [diff] [blame^] | 202 | struct page *page = dequeue_huge_page(NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | if (!page) |
| 204 | break; |
| 205 | update_and_free_page(page); |
| 206 | } |
| 207 | spin_unlock(&hugetlb_lock); |
| 208 | return nr_huge_pages; |
| 209 | } |
| 210 | |
| 211 | int hugetlb_sysctl_handler(struct ctl_table *table, int write, |
| 212 | struct file *file, void __user *buffer, |
| 213 | size_t *length, loff_t *ppos) |
| 214 | { |
| 215 | proc_doulongvec_minmax(table, write, file, buffer, length, ppos); |
| 216 | max_huge_pages = set_max_huge_pages(max_huge_pages); |
| 217 | return 0; |
| 218 | } |
| 219 | #endif /* CONFIG_SYSCTL */ |
| 220 | |
| 221 | int hugetlb_report_meminfo(char *buf) |
| 222 | { |
| 223 | return sprintf(buf, |
| 224 | "HugePages_Total: %5lu\n" |
| 225 | "HugePages_Free: %5lu\n" |
| 226 | "Hugepagesize: %5lu kB\n", |
| 227 | nr_huge_pages, |
| 228 | free_huge_pages, |
| 229 | HPAGE_SIZE/1024); |
| 230 | } |
| 231 | |
| 232 | int hugetlb_report_node_meminfo(int nid, char *buf) |
| 233 | { |
| 234 | return sprintf(buf, |
| 235 | "Node %d HugePages_Total: %5u\n" |
| 236 | "Node %d HugePages_Free: %5u\n", |
| 237 | nid, nr_huge_pages_node[nid], |
| 238 | nid, free_huge_pages_node[nid]); |
| 239 | } |
| 240 | |
| 241 | int is_hugepage_mem_enough(size_t size) |
| 242 | { |
| 243 | return (size + ~HPAGE_MASK)/HPAGE_SIZE <= free_huge_pages; |
| 244 | } |
| 245 | |
| 246 | /* Return the number pages of memory we physically have, in PAGE_SIZE units. */ |
| 247 | unsigned long hugetlb_total_pages(void) |
| 248 | { |
| 249 | return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE); |
| 250 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
| 252 | /* |
| 253 | * We cannot handle pagefaults against hugetlb pages at all. They cause |
| 254 | * handle_mm_fault() to try to instantiate regular-sized pages in the |
| 255 | * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get |
| 256 | * this far. |
| 257 | */ |
| 258 | static struct page *hugetlb_nopage(struct vm_area_struct *vma, |
| 259 | unsigned long address, int *unused) |
| 260 | { |
| 261 | BUG(); |
| 262 | return NULL; |
| 263 | } |
| 264 | |
| 265 | struct vm_operations_struct hugetlb_vm_ops = { |
| 266 | .nopage = hugetlb_nopage, |
| 267 | }; |
| 268 | |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 269 | static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page, |
| 270 | int writable) |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 271 | { |
| 272 | pte_t entry; |
| 273 | |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 274 | if (writable) { |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 275 | entry = |
| 276 | pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot))); |
| 277 | } else { |
| 278 | entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot)); |
| 279 | } |
| 280 | entry = pte_mkyoung(entry); |
| 281 | entry = pte_mkhuge(entry); |
| 282 | |
| 283 | return entry; |
| 284 | } |
| 285 | |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 286 | static void set_huge_ptep_writable(struct vm_area_struct *vma, |
| 287 | unsigned long address, pte_t *ptep) |
| 288 | { |
| 289 | pte_t entry; |
| 290 | |
| 291 | entry = pte_mkwrite(pte_mkdirty(*ptep)); |
| 292 | ptep_set_access_flags(vma, address, ptep, entry, 1); |
| 293 | update_mmu_cache(vma, address, entry); |
| 294 | lazy_mmu_prot_update(entry); |
| 295 | } |
| 296 | |
| 297 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 298 | int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, |
| 299 | struct vm_area_struct *vma) |
| 300 | { |
| 301 | pte_t *src_pte, *dst_pte, entry; |
| 302 | struct page *ptepage; |
Hugh Dickins | 1c59827 | 2005-10-19 21:23:43 -0700 | [diff] [blame] | 303 | unsigned long addr; |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 304 | int cow; |
| 305 | |
| 306 | cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 307 | |
Hugh Dickins | 1c59827 | 2005-10-19 21:23:43 -0700 | [diff] [blame] | 308 | for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) { |
Hugh Dickins | c74df32 | 2005-10-29 18:16:23 -0700 | [diff] [blame] | 309 | src_pte = huge_pte_offset(src, addr); |
| 310 | if (!src_pte) |
| 311 | continue; |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 312 | dst_pte = huge_pte_alloc(dst, addr); |
| 313 | if (!dst_pte) |
| 314 | goto nomem; |
Hugh Dickins | c74df32 | 2005-10-29 18:16:23 -0700 | [diff] [blame] | 315 | spin_lock(&dst->page_table_lock); |
Hugh Dickins | 1c59827 | 2005-10-19 21:23:43 -0700 | [diff] [blame] | 316 | spin_lock(&src->page_table_lock); |
Hugh Dickins | c74df32 | 2005-10-29 18:16:23 -0700 | [diff] [blame] | 317 | if (!pte_none(*src_pte)) { |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 318 | if (cow) |
| 319 | ptep_set_wrprotect(src, addr, src_pte); |
Hugh Dickins | 1c59827 | 2005-10-19 21:23:43 -0700 | [diff] [blame] | 320 | entry = *src_pte; |
| 321 | ptepage = pte_page(entry); |
| 322 | get_page(ptepage); |
Hugh Dickins | 4294621 | 2005-10-29 18:16:05 -0700 | [diff] [blame] | 323 | add_mm_counter(dst, file_rss, HPAGE_SIZE / PAGE_SIZE); |
Hugh Dickins | 1c59827 | 2005-10-19 21:23:43 -0700 | [diff] [blame] | 324 | set_huge_pte_at(dst, addr, dst_pte, entry); |
| 325 | } |
| 326 | spin_unlock(&src->page_table_lock); |
Hugh Dickins | c74df32 | 2005-10-29 18:16:23 -0700 | [diff] [blame] | 327 | spin_unlock(&dst->page_table_lock); |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 328 | } |
| 329 | return 0; |
| 330 | |
| 331 | nomem: |
| 332 | return -ENOMEM; |
| 333 | } |
| 334 | |
| 335 | void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start, |
| 336 | unsigned long end) |
| 337 | { |
| 338 | struct mm_struct *mm = vma->vm_mm; |
| 339 | unsigned long address; |
David Gibson | c7546f8 | 2005-08-05 11:59:35 -0700 | [diff] [blame] | 340 | pte_t *ptep; |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 341 | pte_t pte; |
| 342 | struct page *page; |
| 343 | |
| 344 | WARN_ON(!is_vm_hugetlb_page(vma)); |
| 345 | BUG_ON(start & ~HPAGE_MASK); |
| 346 | BUG_ON(end & ~HPAGE_MASK); |
| 347 | |
Hugh Dickins | 508034a | 2005-10-29 18:16:30 -0700 | [diff] [blame] | 348 | spin_lock(&mm->page_table_lock); |
| 349 | |
Hugh Dickins | 365e9c87 | 2005-10-29 18:16:18 -0700 | [diff] [blame] | 350 | /* Update high watermark before we lower rss */ |
| 351 | update_hiwater_rss(mm); |
| 352 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 353 | for (address = start; address < end; address += HPAGE_SIZE) { |
David Gibson | c7546f8 | 2005-08-05 11:59:35 -0700 | [diff] [blame] | 354 | ptep = huge_pte_offset(mm, address); |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 355 | if (!ptep) |
David Gibson | c7546f8 | 2005-08-05 11:59:35 -0700 | [diff] [blame] | 356 | continue; |
| 357 | |
| 358 | pte = huge_ptep_get_and_clear(mm, address, ptep); |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 359 | if (pte_none(pte)) |
| 360 | continue; |
David Gibson | c7546f8 | 2005-08-05 11:59:35 -0700 | [diff] [blame] | 361 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 362 | page = pte_page(pte); |
| 363 | put_page(page); |
Hugh Dickins | 4294621 | 2005-10-29 18:16:05 -0700 | [diff] [blame] | 364 | add_mm_counter(mm, file_rss, (int) -(HPAGE_SIZE / PAGE_SIZE)); |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 365 | } |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | spin_unlock(&mm->page_table_lock); |
Hugh Dickins | 508034a | 2005-10-29 18:16:30 -0700 | [diff] [blame] | 368 | flush_tlb_range(vma, start, end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | } |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 370 | |
Christoph Lameter | 5da7ca8 | 2006-01-06 00:10:46 -0800 | [diff] [blame^] | 371 | static struct page *find_or_alloc_huge_page(struct vm_area_struct *vma, |
| 372 | unsigned long addr, struct address_space *mapping, |
| 373 | unsigned long idx, int shared) |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 374 | { |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 375 | struct page *page; |
| 376 | int err; |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 377 | |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 378 | retry: |
| 379 | page = find_lock_page(mapping, idx); |
| 380 | if (page) |
| 381 | goto out; |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 382 | |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 383 | if (hugetlb_get_quota(mapping)) |
| 384 | goto out; |
Christoph Lameter | 5da7ca8 | 2006-01-06 00:10:46 -0800 | [diff] [blame^] | 385 | page = alloc_huge_page(vma, addr); |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 386 | if (!page) { |
| 387 | hugetlb_put_quota(mapping); |
| 388 | goto out; |
| 389 | } |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 390 | |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 391 | if (shared) { |
| 392 | err = add_to_page_cache(page, mapping, idx, GFP_KERNEL); |
| 393 | if (err) { |
| 394 | put_page(page); |
| 395 | hugetlb_put_quota(mapping); |
| 396 | if (err == -EEXIST) |
| 397 | goto retry; |
| 398 | page = NULL; |
| 399 | } |
| 400 | } else { |
| 401 | /* Caller expects a locked page */ |
| 402 | lock_page(page); |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 403 | } |
| 404 | out: |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 405 | return page; |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 406 | } |
| 407 | |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 408 | static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma, |
| 409 | unsigned long address, pte_t *ptep, pte_t pte) |
| 410 | { |
| 411 | struct page *old_page, *new_page; |
| 412 | int i, avoidcopy; |
| 413 | |
| 414 | old_page = pte_page(pte); |
| 415 | |
| 416 | /* If no-one else is actually using this page, avoid the copy |
| 417 | * and just make the page writable */ |
| 418 | avoidcopy = (page_count(old_page) == 1); |
| 419 | if (avoidcopy) { |
| 420 | set_huge_ptep_writable(vma, address, ptep); |
| 421 | return VM_FAULT_MINOR; |
| 422 | } |
| 423 | |
| 424 | page_cache_get(old_page); |
Christoph Lameter | 5da7ca8 | 2006-01-06 00:10:46 -0800 | [diff] [blame^] | 425 | new_page = alloc_huge_page(vma, address); |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 426 | |
| 427 | if (!new_page) { |
| 428 | page_cache_release(old_page); |
| 429 | |
| 430 | /* Logically this is OOM, not a SIGBUS, but an OOM |
| 431 | * could cause the kernel to go killing other |
| 432 | * processes which won't help the hugepage situation |
| 433 | * at all (?) */ |
| 434 | return VM_FAULT_SIGBUS; |
| 435 | } |
| 436 | |
| 437 | spin_unlock(&mm->page_table_lock); |
| 438 | for (i = 0; i < HPAGE_SIZE/PAGE_SIZE; i++) |
| 439 | copy_user_highpage(new_page + i, old_page + i, |
| 440 | address + i*PAGE_SIZE); |
| 441 | spin_lock(&mm->page_table_lock); |
| 442 | |
| 443 | ptep = huge_pte_offset(mm, address & HPAGE_MASK); |
| 444 | if (likely(pte_same(*ptep, pte))) { |
| 445 | /* Break COW */ |
| 446 | set_huge_pte_at(mm, address, ptep, |
| 447 | make_huge_pte(vma, new_page, 1)); |
| 448 | /* Make the old page be freed below */ |
| 449 | new_page = old_page; |
| 450 | } |
| 451 | page_cache_release(new_page); |
| 452 | page_cache_release(old_page); |
| 453 | return VM_FAULT_MINOR; |
| 454 | } |
| 455 | |
Adam Litke | 86e5216 | 2006-01-06 00:10:43 -0800 | [diff] [blame] | 456 | int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma, |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 457 | unsigned long address, pte_t *ptep, int write_access) |
Hugh Dickins | ac9b9c6 | 2005-10-20 16:24:28 +0100 | [diff] [blame] | 458 | { |
| 459 | int ret = VM_FAULT_SIGBUS; |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 460 | unsigned long idx; |
| 461 | unsigned long size; |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 462 | struct page *page; |
| 463 | struct address_space *mapping; |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 464 | pte_t new_pte; |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 465 | |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 466 | mapping = vma->vm_file->f_mapping; |
| 467 | idx = ((address - vma->vm_start) >> HPAGE_SHIFT) |
| 468 | + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT)); |
| 469 | |
| 470 | /* |
| 471 | * Use page lock to guard against racing truncation |
| 472 | * before we get page_table_lock. |
| 473 | */ |
Christoph Lameter | 5da7ca8 | 2006-01-06 00:10:46 -0800 | [diff] [blame^] | 474 | page = find_or_alloc_huge_page(vma, address, mapping, idx, |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 475 | vma->vm_flags & VM_SHARED); |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 476 | if (!page) |
| 477 | goto out; |
Hugh Dickins | ac9b9c6 | 2005-10-20 16:24:28 +0100 | [diff] [blame] | 478 | |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 479 | BUG_ON(!PageLocked(page)); |
| 480 | |
Hugh Dickins | ac9b9c6 | 2005-10-20 16:24:28 +0100 | [diff] [blame] | 481 | spin_lock(&mm->page_table_lock); |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 482 | size = i_size_read(mapping->host) >> HPAGE_SHIFT; |
| 483 | if (idx >= size) |
| 484 | goto backout; |
| 485 | |
| 486 | ret = VM_FAULT_MINOR; |
Adam Litke | 86e5216 | 2006-01-06 00:10:43 -0800 | [diff] [blame] | 487 | if (!pte_none(*ptep)) |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 488 | goto backout; |
| 489 | |
| 490 | add_mm_counter(mm, file_rss, HPAGE_SIZE / PAGE_SIZE); |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 491 | new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE) |
| 492 | && (vma->vm_flags & VM_SHARED))); |
| 493 | set_huge_pte_at(mm, address, ptep, new_pte); |
| 494 | |
| 495 | if (write_access && !(vma->vm_flags & VM_SHARED)) { |
| 496 | /* Optimization, do the COW without a second fault */ |
| 497 | ret = hugetlb_cow(mm, vma, address, ptep, new_pte); |
| 498 | } |
| 499 | |
Hugh Dickins | ac9b9c6 | 2005-10-20 16:24:28 +0100 | [diff] [blame] | 500 | spin_unlock(&mm->page_table_lock); |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 501 | unlock_page(page); |
| 502 | out: |
Hugh Dickins | ac9b9c6 | 2005-10-20 16:24:28 +0100 | [diff] [blame] | 503 | return ret; |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 504 | |
| 505 | backout: |
| 506 | spin_unlock(&mm->page_table_lock); |
| 507 | hugetlb_put_quota(mapping); |
| 508 | unlock_page(page); |
| 509 | put_page(page); |
| 510 | goto out; |
Hugh Dickins | ac9b9c6 | 2005-10-20 16:24:28 +0100 | [diff] [blame] | 511 | } |
| 512 | |
Adam Litke | 86e5216 | 2006-01-06 00:10:43 -0800 | [diff] [blame] | 513 | int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, |
| 514 | unsigned long address, int write_access) |
| 515 | { |
| 516 | pte_t *ptep; |
| 517 | pte_t entry; |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 518 | int ret; |
Adam Litke | 86e5216 | 2006-01-06 00:10:43 -0800 | [diff] [blame] | 519 | |
| 520 | ptep = huge_pte_alloc(mm, address); |
| 521 | if (!ptep) |
| 522 | return VM_FAULT_OOM; |
| 523 | |
| 524 | entry = *ptep; |
| 525 | if (pte_none(entry)) |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 526 | return hugetlb_no_page(mm, vma, address, ptep, write_access); |
Adam Litke | 86e5216 | 2006-01-06 00:10:43 -0800 | [diff] [blame] | 527 | |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 528 | ret = VM_FAULT_MINOR; |
| 529 | |
| 530 | spin_lock(&mm->page_table_lock); |
| 531 | /* Check for a racing update before calling hugetlb_cow */ |
| 532 | if (likely(pte_same(entry, *ptep))) |
| 533 | if (write_access && !pte_write(entry)) |
| 534 | ret = hugetlb_cow(mm, vma, address, ptep, entry); |
| 535 | spin_unlock(&mm->page_table_lock); |
| 536 | |
| 537 | return ret; |
Adam Litke | 86e5216 | 2006-01-06 00:10:43 -0800 | [diff] [blame] | 538 | } |
| 539 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 540 | int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, |
| 541 | struct page **pages, struct vm_area_struct **vmas, |
| 542 | unsigned long *position, int *length, int i) |
| 543 | { |
| 544 | unsigned long vpfn, vaddr = *position; |
| 545 | int remainder = *length; |
| 546 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 547 | vpfn = vaddr/PAGE_SIZE; |
Hugh Dickins | 1c59827 | 2005-10-19 21:23:43 -0700 | [diff] [blame] | 548 | spin_lock(&mm->page_table_lock); |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 549 | while (vaddr < vma->vm_end && remainder) { |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 550 | pte_t *pte; |
| 551 | struct page *page; |
| 552 | |
| 553 | /* |
| 554 | * Some archs (sparc64, sh*) have multiple pte_ts to |
| 555 | * each hugepage. We have to make * sure we get the |
| 556 | * first, for the page indexing below to work. |
| 557 | */ |
| 558 | pte = huge_pte_offset(mm, vaddr & HPAGE_MASK); |
| 559 | |
| 560 | if (!pte || pte_none(*pte)) { |
| 561 | int ret; |
| 562 | |
| 563 | spin_unlock(&mm->page_table_lock); |
| 564 | ret = hugetlb_fault(mm, vma, vaddr, 0); |
| 565 | spin_lock(&mm->page_table_lock); |
| 566 | if (ret == VM_FAULT_MINOR) |
| 567 | continue; |
| 568 | |
| 569 | remainder = 0; |
| 570 | if (!i) |
| 571 | i = -EFAULT; |
| 572 | break; |
| 573 | } |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 574 | |
| 575 | if (pages) { |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 576 | page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)]; |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 577 | get_page(page); |
| 578 | pages[i] = page; |
| 579 | } |
| 580 | |
| 581 | if (vmas) |
| 582 | vmas[i] = vma; |
| 583 | |
| 584 | vaddr += PAGE_SIZE; |
| 585 | ++vpfn; |
| 586 | --remainder; |
| 587 | ++i; |
| 588 | } |
Hugh Dickins | 1c59827 | 2005-10-19 21:23:43 -0700 | [diff] [blame] | 589 | spin_unlock(&mm->page_table_lock); |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 590 | *length = remainder; |
| 591 | *position = vaddr; |
| 592 | |
| 593 | return i; |
| 594 | } |