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