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> |
David Gibson | 3935baa | 2006-03-22 00:08:53 -0800 | [diff] [blame] | 16 | #include <linux/mutex.h> |
Nishanth Aravamudan | a343787 | 2008-07-23 21:27:44 -0700 | [diff] [blame] | 17 | #include <linux/sysfs.h> |
Christoph Lameter | 5da7ca8 | 2006-01-06 00:10:46 -0800 | [diff] [blame] | 18 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 19 | #include <asm/page.h> |
| 20 | #include <asm/pgtable.h> |
| 21 | |
| 22 | #include <linux/hugetlb.h> |
Nick Piggin | 7835e98 | 2006-03-22 00:08:40 -0800 | [diff] [blame] | 23 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL; |
Mel Gorman | 396faf0 | 2007-07-17 04:03:13 -0700 | [diff] [blame] | 26 | static gfp_t htlb_alloc_mask = GFP_HIGHUSER; |
| 27 | unsigned long hugepages_treat_as_movable; |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 28 | |
Andi Kleen | e5ff215 | 2008-07-23 21:27:42 -0700 | [diff] [blame] | 29 | static int max_hstate; |
| 30 | unsigned int default_hstate_idx; |
| 31 | struct hstate hstates[HUGE_MAX_HSTATE]; |
| 32 | |
| 33 | /* for command line parsing */ |
| 34 | static struct hstate * __initdata parsed_hstate; |
| 35 | static unsigned long __initdata default_hstate_max_huge_pages; |
| 36 | |
| 37 | #define for_each_hstate(h) \ |
| 38 | for ((h) = hstates; (h) < &hstates[max_hstate]; (h)++) |
Mel Gorman | 396faf0 | 2007-07-17 04:03:13 -0700 | [diff] [blame] | 39 | |
David Gibson | 3935baa | 2006-03-22 00:08:53 -0800 | [diff] [blame] | 40 | /* |
| 41 | * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages |
| 42 | */ |
| 43 | static DEFINE_SPINLOCK(hugetlb_lock); |
Eric Paris | 0bd0f9f | 2005-11-21 21:32:28 -0800 | [diff] [blame] | 44 | |
Andy Whitcroft | e7c4b0b | 2008-07-23 21:27:26 -0700 | [diff] [blame] | 45 | /* |
Andy Whitcroft | 9682290 | 2008-07-23 21:27:29 -0700 | [diff] [blame] | 46 | * Region tracking -- allows tracking of reservations and instantiated pages |
| 47 | * across the pages in a mapping. |
Andy Whitcroft | 84afd99 | 2008-07-23 21:27:32 -0700 | [diff] [blame] | 48 | * |
| 49 | * The region data structures are protected by a combination of the mmap_sem |
| 50 | * and the hugetlb_instantion_mutex. To access or modify a region the caller |
| 51 | * must either hold the mmap_sem for write, or the mmap_sem for read and |
| 52 | * the hugetlb_instantiation mutex: |
| 53 | * |
| 54 | * down_write(&mm->mmap_sem); |
| 55 | * or |
| 56 | * down_read(&mm->mmap_sem); |
| 57 | * mutex_lock(&hugetlb_instantiation_mutex); |
Andy Whitcroft | 9682290 | 2008-07-23 21:27:29 -0700 | [diff] [blame] | 58 | */ |
| 59 | struct file_region { |
| 60 | struct list_head link; |
| 61 | long from; |
| 62 | long to; |
| 63 | }; |
| 64 | |
| 65 | static long region_add(struct list_head *head, long f, long t) |
| 66 | { |
| 67 | struct file_region *rg, *nrg, *trg; |
| 68 | |
| 69 | /* Locate the region we are either in or before. */ |
| 70 | list_for_each_entry(rg, head, link) |
| 71 | if (f <= rg->to) |
| 72 | break; |
| 73 | |
| 74 | /* Round our left edge to the current segment if it encloses us. */ |
| 75 | if (f > rg->from) |
| 76 | f = rg->from; |
| 77 | |
| 78 | /* Check for and consume any regions we now overlap with. */ |
| 79 | nrg = rg; |
| 80 | list_for_each_entry_safe(rg, trg, rg->link.prev, link) { |
| 81 | if (&rg->link == head) |
| 82 | break; |
| 83 | if (rg->from > t) |
| 84 | break; |
| 85 | |
| 86 | /* If this area reaches higher then extend our area to |
| 87 | * include it completely. If this is not the first area |
| 88 | * which we intend to reuse, free it. */ |
| 89 | if (rg->to > t) |
| 90 | t = rg->to; |
| 91 | if (rg != nrg) { |
| 92 | list_del(&rg->link); |
| 93 | kfree(rg); |
| 94 | } |
| 95 | } |
| 96 | nrg->from = f; |
| 97 | nrg->to = t; |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static long region_chg(struct list_head *head, long f, long t) |
| 102 | { |
| 103 | struct file_region *rg, *nrg; |
| 104 | long chg = 0; |
| 105 | |
| 106 | /* Locate the region we are before or in. */ |
| 107 | list_for_each_entry(rg, head, link) |
| 108 | if (f <= rg->to) |
| 109 | break; |
| 110 | |
| 111 | /* If we are below the current region then a new region is required. |
| 112 | * Subtle, allocate a new region at the position but make it zero |
| 113 | * size such that we can guarantee to record the reservation. */ |
| 114 | if (&rg->link == head || t < rg->from) { |
| 115 | nrg = kmalloc(sizeof(*nrg), GFP_KERNEL); |
| 116 | if (!nrg) |
| 117 | return -ENOMEM; |
| 118 | nrg->from = f; |
| 119 | nrg->to = f; |
| 120 | INIT_LIST_HEAD(&nrg->link); |
| 121 | list_add(&nrg->link, rg->link.prev); |
| 122 | |
| 123 | return t - f; |
| 124 | } |
| 125 | |
| 126 | /* Round our left edge to the current segment if it encloses us. */ |
| 127 | if (f > rg->from) |
| 128 | f = rg->from; |
| 129 | chg = t - f; |
| 130 | |
| 131 | /* Check for and consume any regions we now overlap with. */ |
| 132 | list_for_each_entry(rg, rg->link.prev, link) { |
| 133 | if (&rg->link == head) |
| 134 | break; |
| 135 | if (rg->from > t) |
| 136 | return chg; |
| 137 | |
| 138 | /* We overlap with this area, if it extends futher than |
| 139 | * us then we must extend ourselves. Account for its |
| 140 | * existing reservation. */ |
| 141 | if (rg->to > t) { |
| 142 | chg += rg->to - t; |
| 143 | t = rg->to; |
| 144 | } |
| 145 | chg -= rg->to - rg->from; |
| 146 | } |
| 147 | return chg; |
| 148 | } |
| 149 | |
| 150 | static long region_truncate(struct list_head *head, long end) |
| 151 | { |
| 152 | struct file_region *rg, *trg; |
| 153 | long chg = 0; |
| 154 | |
| 155 | /* Locate the region we are either in or before. */ |
| 156 | list_for_each_entry(rg, head, link) |
| 157 | if (end <= rg->to) |
| 158 | break; |
| 159 | if (&rg->link == head) |
| 160 | return 0; |
| 161 | |
| 162 | /* If we are in the middle of a region then adjust it. */ |
| 163 | if (end > rg->from) { |
| 164 | chg = rg->to - end; |
| 165 | rg->to = end; |
| 166 | rg = list_entry(rg->link.next, typeof(*rg), link); |
| 167 | } |
| 168 | |
| 169 | /* Drop any remaining regions. */ |
| 170 | list_for_each_entry_safe(rg, trg, rg->link.prev, link) { |
| 171 | if (&rg->link == head) |
| 172 | break; |
| 173 | chg += rg->to - rg->from; |
| 174 | list_del(&rg->link); |
| 175 | kfree(rg); |
| 176 | } |
| 177 | return chg; |
| 178 | } |
| 179 | |
Andy Whitcroft | 84afd99 | 2008-07-23 21:27:32 -0700 | [diff] [blame] | 180 | static long region_count(struct list_head *head, long f, long t) |
| 181 | { |
| 182 | struct file_region *rg; |
| 183 | long chg = 0; |
| 184 | |
| 185 | /* Locate each segment we overlap with, and count that overlap. */ |
| 186 | list_for_each_entry(rg, head, link) { |
| 187 | int seg_from; |
| 188 | int seg_to; |
| 189 | |
| 190 | if (rg->to <= f) |
| 191 | continue; |
| 192 | if (rg->from >= t) |
| 193 | break; |
| 194 | |
| 195 | seg_from = max(rg->from, f); |
| 196 | seg_to = min(rg->to, t); |
| 197 | |
| 198 | chg += seg_to - seg_from; |
| 199 | } |
| 200 | |
| 201 | return chg; |
| 202 | } |
| 203 | |
Andy Whitcroft | 9682290 | 2008-07-23 21:27:29 -0700 | [diff] [blame] | 204 | /* |
Andy Whitcroft | e7c4b0b | 2008-07-23 21:27:26 -0700 | [diff] [blame] | 205 | * Convert the address within this vma to the page offset within |
Andy Whitcroft | e7c4b0b | 2008-07-23 21:27:26 -0700 | [diff] [blame] | 206 | * the mapping, in pagecache page units; huge pages here. |
| 207 | */ |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 208 | static pgoff_t vma_hugecache_offset(struct hstate *h, |
| 209 | struct vm_area_struct *vma, unsigned long address) |
Andy Whitcroft | e7c4b0b | 2008-07-23 21:27:26 -0700 | [diff] [blame] | 210 | { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 211 | return ((address - vma->vm_start) >> huge_page_shift(h)) + |
| 212 | (vma->vm_pgoff >> huge_page_order(h)); |
Andy Whitcroft | e7c4b0b | 2008-07-23 21:27:26 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Andy Whitcroft | 84afd99 | 2008-07-23 21:27:32 -0700 | [diff] [blame] | 215 | /* |
| 216 | * Flags for MAP_PRIVATE reservations. These are stored in the bottom |
| 217 | * bits of the reservation map pointer, which are always clear due to |
| 218 | * alignment. |
| 219 | */ |
| 220 | #define HPAGE_RESV_OWNER (1UL << 0) |
| 221 | #define HPAGE_RESV_UNMAPPED (1UL << 1) |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 222 | #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED) |
Andy Whitcroft | 84afd99 | 2008-07-23 21:27:32 -0700 | [diff] [blame] | 223 | |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 224 | /* |
| 225 | * These helpers are used to track how many pages are reserved for |
| 226 | * faults in a MAP_PRIVATE mapping. Only the process that called mmap() |
| 227 | * is guaranteed to have their future faults succeed. |
| 228 | * |
| 229 | * With the exception of reset_vma_resv_huge_pages() which is called at fork(), |
| 230 | * the reserve counters are updated with the hugetlb_lock held. It is safe |
| 231 | * to reset the VMA at fork() time as it is not in use yet and there is no |
| 232 | * chance of the global counters getting corrupted as a result of the values. |
Andy Whitcroft | 84afd99 | 2008-07-23 21:27:32 -0700 | [diff] [blame] | 233 | * |
| 234 | * The private mapping reservation is represented in a subtly different |
| 235 | * manner to a shared mapping. A shared mapping has a region map associated |
| 236 | * with the underlying file, this region map represents the backing file |
| 237 | * pages which have ever had a reservation assigned which this persists even |
| 238 | * after the page is instantiated. A private mapping has a region map |
| 239 | * associated with the original mmap which is attached to all VMAs which |
| 240 | * reference it, this region map represents those offsets which have consumed |
| 241 | * reservation ie. where pages have been instantiated. |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 242 | */ |
Andy Whitcroft | e7c4b0b | 2008-07-23 21:27:26 -0700 | [diff] [blame] | 243 | static unsigned long get_vma_private_data(struct vm_area_struct *vma) |
| 244 | { |
| 245 | return (unsigned long)vma->vm_private_data; |
| 246 | } |
| 247 | |
| 248 | static void set_vma_private_data(struct vm_area_struct *vma, |
| 249 | unsigned long value) |
| 250 | { |
| 251 | vma->vm_private_data = (void *)value; |
| 252 | } |
| 253 | |
Andy Whitcroft | 84afd99 | 2008-07-23 21:27:32 -0700 | [diff] [blame] | 254 | struct resv_map { |
| 255 | struct kref refs; |
| 256 | struct list_head regions; |
| 257 | }; |
| 258 | |
| 259 | struct resv_map *resv_map_alloc(void) |
| 260 | { |
| 261 | struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL); |
| 262 | if (!resv_map) |
| 263 | return NULL; |
| 264 | |
| 265 | kref_init(&resv_map->refs); |
| 266 | INIT_LIST_HEAD(&resv_map->regions); |
| 267 | |
| 268 | return resv_map; |
| 269 | } |
| 270 | |
| 271 | void resv_map_release(struct kref *ref) |
| 272 | { |
| 273 | struct resv_map *resv_map = container_of(ref, struct resv_map, refs); |
| 274 | |
| 275 | /* Clear out any active regions before we release the map. */ |
| 276 | region_truncate(&resv_map->regions, 0); |
| 277 | kfree(resv_map); |
| 278 | } |
| 279 | |
| 280 | static struct resv_map *vma_resv_map(struct vm_area_struct *vma) |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 281 | { |
| 282 | VM_BUG_ON(!is_vm_hugetlb_page(vma)); |
| 283 | if (!(vma->vm_flags & VM_SHARED)) |
Andy Whitcroft | 84afd99 | 2008-07-23 21:27:32 -0700 | [diff] [blame] | 284 | return (struct resv_map *)(get_vma_private_data(vma) & |
| 285 | ~HPAGE_RESV_MASK); |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 286 | return 0; |
| 287 | } |
| 288 | |
Andy Whitcroft | 84afd99 | 2008-07-23 21:27:32 -0700 | [diff] [blame] | 289 | static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map) |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 290 | { |
| 291 | VM_BUG_ON(!is_vm_hugetlb_page(vma)); |
| 292 | VM_BUG_ON(vma->vm_flags & VM_SHARED); |
| 293 | |
Andy Whitcroft | 84afd99 | 2008-07-23 21:27:32 -0700 | [diff] [blame] | 294 | set_vma_private_data(vma, (get_vma_private_data(vma) & |
| 295 | HPAGE_RESV_MASK) | (unsigned long)map); |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags) |
| 299 | { |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 300 | VM_BUG_ON(!is_vm_hugetlb_page(vma)); |
Andy Whitcroft | e7c4b0b | 2008-07-23 21:27:26 -0700 | [diff] [blame] | 301 | VM_BUG_ON(vma->vm_flags & VM_SHARED); |
| 302 | |
| 303 | set_vma_private_data(vma, get_vma_private_data(vma) | flags); |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag) |
| 307 | { |
| 308 | VM_BUG_ON(!is_vm_hugetlb_page(vma)); |
Andy Whitcroft | e7c4b0b | 2008-07-23 21:27:26 -0700 | [diff] [blame] | 309 | |
| 310 | return (get_vma_private_data(vma) & flag) != 0; |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /* Decrement the reserved pages in the hugepage pool by one */ |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 314 | static void decrement_hugepage_resv_vma(struct hstate *h, |
| 315 | struct vm_area_struct *vma) |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 316 | { |
Andy Whitcroft | c37f9fb | 2008-07-23 21:27:30 -0700 | [diff] [blame] | 317 | if (vma->vm_flags & VM_NORESERVE) |
| 318 | return; |
| 319 | |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 320 | if (vma->vm_flags & VM_SHARED) { |
| 321 | /* Shared mappings always use reserves */ |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 322 | h->resv_huge_pages--; |
Andy Whitcroft | 84afd99 | 2008-07-23 21:27:32 -0700 | [diff] [blame] | 323 | } else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) { |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 324 | /* |
| 325 | * Only the process that called mmap() has reserves for |
| 326 | * private mappings. |
| 327 | */ |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 328 | h->resv_huge_pages--; |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 332 | /* Reset counters to 0 and clear all HPAGE_RESV_* flags */ |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 333 | void reset_vma_resv_huge_pages(struct vm_area_struct *vma) |
| 334 | { |
| 335 | VM_BUG_ON(!is_vm_hugetlb_page(vma)); |
| 336 | if (!(vma->vm_flags & VM_SHARED)) |
| 337 | vma->vm_private_data = (void *)0; |
| 338 | } |
| 339 | |
| 340 | /* Returns true if the VMA has associated reserve pages */ |
| 341 | static int vma_has_private_reserves(struct vm_area_struct *vma) |
| 342 | { |
| 343 | if (vma->vm_flags & VM_SHARED) |
| 344 | return 0; |
Andy Whitcroft | 84afd99 | 2008-07-23 21:27:32 -0700 | [diff] [blame] | 345 | if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER)) |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 346 | return 0; |
| 347 | return 1; |
| 348 | } |
| 349 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 350 | static void clear_huge_page(struct page *page, |
| 351 | unsigned long addr, unsigned long sz) |
David Gibson | 79ac6ba | 2006-03-22 00:08:51 -0800 | [diff] [blame] | 352 | { |
| 353 | int i; |
| 354 | |
| 355 | might_sleep(); |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 356 | for (i = 0; i < sz/PAGE_SIZE; i++) { |
David Gibson | 79ac6ba | 2006-03-22 00:08:51 -0800 | [diff] [blame] | 357 | cond_resched(); |
Ralf Baechle | 281e0e3 | 2007-10-01 01:20:10 -0700 | [diff] [blame] | 358 | clear_user_highpage(page + i, addr + i * PAGE_SIZE); |
David Gibson | 79ac6ba | 2006-03-22 00:08:51 -0800 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
| 362 | static void copy_huge_page(struct page *dst, struct page *src, |
Atsushi Nemoto | 9de455b | 2006-12-12 17:14:55 +0000 | [diff] [blame] | 363 | unsigned long addr, struct vm_area_struct *vma) |
David Gibson | 79ac6ba | 2006-03-22 00:08:51 -0800 | [diff] [blame] | 364 | { |
| 365 | int i; |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 366 | struct hstate *h = hstate_vma(vma); |
David Gibson | 79ac6ba | 2006-03-22 00:08:51 -0800 | [diff] [blame] | 367 | |
| 368 | might_sleep(); |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 369 | for (i = 0; i < pages_per_huge_page(h); i++) { |
David Gibson | 79ac6ba | 2006-03-22 00:08:51 -0800 | [diff] [blame] | 370 | cond_resched(); |
Atsushi Nemoto | 9de455b | 2006-12-12 17:14:55 +0000 | [diff] [blame] | 371 | copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma); |
David Gibson | 79ac6ba | 2006-03-22 00:08:51 -0800 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 375 | static void enqueue_huge_page(struct hstate *h, struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | { |
| 377 | int nid = page_to_nid(page); |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 378 | list_add(&page->lru, &h->hugepage_freelists[nid]); |
| 379 | h->free_huge_pages++; |
| 380 | h->free_huge_pages_node[nid]++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 383 | static struct page *dequeue_huge_page(struct hstate *h) |
Nishanth Aravamudan | 348e1e0 | 2008-03-04 14:29:42 -0800 | [diff] [blame] | 384 | { |
| 385 | int nid; |
| 386 | struct page *page = NULL; |
| 387 | |
| 388 | for (nid = 0; nid < MAX_NUMNODES; ++nid) { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 389 | if (!list_empty(&h->hugepage_freelists[nid])) { |
| 390 | page = list_entry(h->hugepage_freelists[nid].next, |
Nishanth Aravamudan | 348e1e0 | 2008-03-04 14:29:42 -0800 | [diff] [blame] | 391 | struct page, lru); |
| 392 | list_del(&page->lru); |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 393 | h->free_huge_pages--; |
| 394 | h->free_huge_pages_node[nid]--; |
Nishanth Aravamudan | 348e1e0 | 2008-03-04 14:29:42 -0800 | [diff] [blame] | 395 | break; |
| 396 | } |
| 397 | } |
| 398 | return page; |
| 399 | } |
| 400 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 401 | static struct page *dequeue_huge_page_vma(struct hstate *h, |
| 402 | struct vm_area_struct *vma, |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 403 | unsigned long address, int avoid_reserve) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | { |
Nishanth Aravamudan | 31a5c6e | 2007-07-15 23:38:02 -0700 | [diff] [blame] | 405 | int nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | struct page *page = NULL; |
Lee Schermerhorn | 480eccf | 2007-09-18 22:46:47 -0700 | [diff] [blame] | 407 | struct mempolicy *mpol; |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 408 | nodemask_t *nodemask; |
Mel Gorman | 396faf0 | 2007-07-17 04:03:13 -0700 | [diff] [blame] | 409 | struct zonelist *zonelist = huge_zonelist(vma, address, |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 410 | htlb_alloc_mask, &mpol, &nodemask); |
Mel Gorman | dd1a239 | 2008-04-28 02:12:17 -0700 | [diff] [blame] | 411 | struct zone *zone; |
| 412 | struct zoneref *z; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 414 | /* |
| 415 | * A child process with MAP_PRIVATE mappings created by their parent |
| 416 | * have no page reserves. This check ensures that reservations are |
| 417 | * not "stolen". The child may still get SIGKILLed |
| 418 | */ |
| 419 | if (!vma_has_private_reserves(vma) && |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 420 | h->free_huge_pages - h->resv_huge_pages == 0) |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 421 | return NULL; |
| 422 | |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 423 | /* If reserves cannot be used, ensure enough pages are in the pool */ |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 424 | if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0) |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 425 | return NULL; |
| 426 | |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 427 | for_each_zone_zonelist_nodemask(zone, z, zonelist, |
| 428 | MAX_NR_ZONES - 1, nodemask) { |
Mel Gorman | 54a6eb5 | 2008-04-28 02:12:16 -0700 | [diff] [blame] | 429 | nid = zone_to_nid(zone); |
| 430 | if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask) && |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 431 | !list_empty(&h->hugepage_freelists[nid])) { |
| 432 | page = list_entry(h->hugepage_freelists[nid].next, |
Andrew Morton | 3abf7af | 2007-07-19 01:49:08 -0700 | [diff] [blame] | 433 | struct page, lru); |
| 434 | list_del(&page->lru); |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 435 | h->free_huge_pages--; |
| 436 | h->free_huge_pages_node[nid]--; |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 437 | |
| 438 | if (!avoid_reserve) |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 439 | decrement_hugepage_resv_vma(h, vma); |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 440 | |
Ken Chen | 5ab3ee7 | 2007-07-23 18:44:00 -0700 | [diff] [blame] | 441 | break; |
Andrew Morton | 3abf7af | 2007-07-19 01:49:08 -0700 | [diff] [blame] | 442 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | } |
Lee Schermerhorn | 52cd3b0 | 2008-04-28 02:13:16 -0700 | [diff] [blame] | 444 | mpol_cond_put(mpol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | return page; |
| 446 | } |
| 447 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 448 | static void update_and_free_page(struct hstate *h, struct page *page) |
Adam Litke | 6af2acb | 2007-10-16 01:26:16 -0700 | [diff] [blame] | 449 | { |
| 450 | int i; |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 451 | |
| 452 | h->nr_huge_pages--; |
| 453 | h->nr_huge_pages_node[page_to_nid(page)]--; |
| 454 | for (i = 0; i < pages_per_huge_page(h); i++) { |
Adam Litke | 6af2acb | 2007-10-16 01:26:16 -0700 | [diff] [blame] | 455 | page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced | |
| 456 | 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved | |
| 457 | 1 << PG_private | 1<< PG_writeback); |
| 458 | } |
| 459 | set_compound_page_dtor(page, NULL); |
| 460 | set_page_refcounted(page); |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 461 | arch_release_hugepage(page); |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 462 | __free_pages(page, huge_page_order(h)); |
Adam Litke | 6af2acb | 2007-10-16 01:26:16 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Andi Kleen | e5ff215 | 2008-07-23 21:27:42 -0700 | [diff] [blame] | 465 | struct hstate *size_to_hstate(unsigned long size) |
| 466 | { |
| 467 | struct hstate *h; |
| 468 | |
| 469 | for_each_hstate(h) { |
| 470 | if (huge_page_size(h) == size) |
| 471 | return h; |
| 472 | } |
| 473 | return NULL; |
| 474 | } |
| 475 | |
David Gibson | 27a85ef | 2006-03-22 00:08:56 -0800 | [diff] [blame] | 476 | static void free_huge_page(struct page *page) |
| 477 | { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 478 | /* |
| 479 | * Can't pass hstate in here because it is called from the |
| 480 | * compound page destructor. |
| 481 | */ |
Andi Kleen | e5ff215 | 2008-07-23 21:27:42 -0700 | [diff] [blame] | 482 | struct hstate *h = page_hstate(page); |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 483 | int nid = page_to_nid(page); |
Adam Litke | c79fb75 | 2007-11-14 16:59:38 -0800 | [diff] [blame] | 484 | struct address_space *mapping; |
David Gibson | 27a85ef | 2006-03-22 00:08:56 -0800 | [diff] [blame] | 485 | |
Adam Litke | c79fb75 | 2007-11-14 16:59:38 -0800 | [diff] [blame] | 486 | mapping = (struct address_space *) page_private(page); |
Andy Whitcroft | e5df70a | 2008-02-23 15:23:32 -0800 | [diff] [blame] | 487 | set_page_private(page, 0); |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 488 | BUG_ON(page_count(page)); |
David Gibson | 27a85ef | 2006-03-22 00:08:56 -0800 | [diff] [blame] | 489 | INIT_LIST_HEAD(&page->lru); |
| 490 | |
| 491 | spin_lock(&hugetlb_lock); |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 492 | if (h->surplus_huge_pages_node[nid]) { |
| 493 | update_and_free_page(h, page); |
| 494 | h->surplus_huge_pages--; |
| 495 | h->surplus_huge_pages_node[nid]--; |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 496 | } else { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 497 | enqueue_huge_page(h, page); |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 498 | } |
David Gibson | 27a85ef | 2006-03-22 00:08:56 -0800 | [diff] [blame] | 499 | spin_unlock(&hugetlb_lock); |
Adam Litke | c79fb75 | 2007-11-14 16:59:38 -0800 | [diff] [blame] | 500 | if (mapping) |
Adam Litke | 9a119c0 | 2007-11-14 16:59:41 -0800 | [diff] [blame] | 501 | hugetlb_put_quota(mapping, 1); |
David Gibson | 27a85ef | 2006-03-22 00:08:56 -0800 | [diff] [blame] | 502 | } |
| 503 | |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 504 | /* |
| 505 | * Increment or decrement surplus_huge_pages. Keep node-specific counters |
| 506 | * balanced by operating on them in a round-robin fashion. |
| 507 | * Returns 1 if an adjustment was made. |
| 508 | */ |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 509 | static int adjust_pool_surplus(struct hstate *h, int delta) |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 510 | { |
| 511 | static int prev_nid; |
| 512 | int nid = prev_nid; |
| 513 | int ret = 0; |
| 514 | |
| 515 | VM_BUG_ON(delta != -1 && delta != 1); |
| 516 | do { |
| 517 | nid = next_node(nid, node_online_map); |
| 518 | if (nid == MAX_NUMNODES) |
| 519 | nid = first_node(node_online_map); |
| 520 | |
| 521 | /* To shrink on this node, there must be a surplus page */ |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 522 | if (delta < 0 && !h->surplus_huge_pages_node[nid]) |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 523 | continue; |
| 524 | /* Surplus cannot exceed the total number of pages */ |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 525 | if (delta > 0 && h->surplus_huge_pages_node[nid] >= |
| 526 | h->nr_huge_pages_node[nid]) |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 527 | continue; |
| 528 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 529 | h->surplus_huge_pages += delta; |
| 530 | h->surplus_huge_pages_node[nid] += delta; |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 531 | ret = 1; |
| 532 | break; |
| 533 | } while (nid != prev_nid); |
| 534 | |
| 535 | prev_nid = nid; |
| 536 | return ret; |
| 537 | } |
| 538 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 539 | static void prep_new_huge_page(struct hstate *h, struct page *page, int nid) |
Andi Kleen | b7ba30c | 2008-07-23 21:27:40 -0700 | [diff] [blame] | 540 | { |
| 541 | set_compound_page_dtor(page, free_huge_page); |
| 542 | spin_lock(&hugetlb_lock); |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 543 | h->nr_huge_pages++; |
| 544 | h->nr_huge_pages_node[nid]++; |
Andi Kleen | b7ba30c | 2008-07-23 21:27:40 -0700 | [diff] [blame] | 545 | spin_unlock(&hugetlb_lock); |
| 546 | put_page(page); /* free it into the hugepage allocator */ |
| 547 | } |
| 548 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 549 | static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | struct page *page; |
Joe Jin | f96efd5 | 2007-07-15 23:38:12 -0700 | [diff] [blame] | 552 | |
Nishanth Aravamudan | 63b4613 | 2007-10-16 01:26:24 -0700 | [diff] [blame] | 553 | page = alloc_pages_node(nid, |
Nishanth Aravamudan | 551883a | 2008-04-29 00:58:26 -0700 | [diff] [blame] | 554 | htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE| |
| 555 | __GFP_REPEAT|__GFP_NOWARN, |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 556 | huge_page_order(h)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | if (page) { |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 558 | if (arch_prepare_hugepage(page)) { |
| 559 | __free_pages(page, HUGETLB_PAGE_ORDER); |
Harvey Harrison | 7b8ee84 | 2008-04-28 14:13:19 -0700 | [diff] [blame] | 560 | return NULL; |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 561 | } |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 562 | prep_new_huge_page(h, page, nid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | } |
Nishanth Aravamudan | 63b4613 | 2007-10-16 01:26:24 -0700 | [diff] [blame] | 564 | |
| 565 | return page; |
| 566 | } |
| 567 | |
Andi Kleen | 5ced66c | 2008-07-23 21:27:45 -0700 | [diff] [blame] | 568 | /* |
| 569 | * Use a helper variable to find the next node and then |
| 570 | * copy it back to hugetlb_next_nid afterwards: |
| 571 | * otherwise there's a window in which a racer might |
| 572 | * pass invalid nid MAX_NUMNODES to alloc_pages_node. |
| 573 | * But we don't need to use a spin_lock here: it really |
| 574 | * doesn't matter if occasionally a racer chooses the |
| 575 | * same nid as we do. Move nid forward in the mask even |
| 576 | * if we just successfully allocated a hugepage so that |
| 577 | * the next caller gets hugepages on the next node. |
| 578 | */ |
| 579 | static int hstate_next_node(struct hstate *h) |
| 580 | { |
| 581 | int next_nid; |
| 582 | next_nid = next_node(h->hugetlb_next_nid, node_online_map); |
| 583 | if (next_nid == MAX_NUMNODES) |
| 584 | next_nid = first_node(node_online_map); |
| 585 | h->hugetlb_next_nid = next_nid; |
| 586 | return next_nid; |
| 587 | } |
| 588 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 589 | static int alloc_fresh_huge_page(struct hstate *h) |
Nishanth Aravamudan | 63b4613 | 2007-10-16 01:26:24 -0700 | [diff] [blame] | 590 | { |
| 591 | struct page *page; |
| 592 | int start_nid; |
| 593 | int next_nid; |
| 594 | int ret = 0; |
| 595 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 596 | start_nid = h->hugetlb_next_nid; |
Nishanth Aravamudan | 63b4613 | 2007-10-16 01:26:24 -0700 | [diff] [blame] | 597 | |
| 598 | do { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 599 | page = alloc_fresh_huge_page_node(h, h->hugetlb_next_nid); |
Nishanth Aravamudan | 63b4613 | 2007-10-16 01:26:24 -0700 | [diff] [blame] | 600 | if (page) |
| 601 | ret = 1; |
Andi Kleen | 5ced66c | 2008-07-23 21:27:45 -0700 | [diff] [blame] | 602 | next_nid = hstate_next_node(h); |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 603 | } while (!page && h->hugetlb_next_nid != start_nid); |
Nishanth Aravamudan | 63b4613 | 2007-10-16 01:26:24 -0700 | [diff] [blame] | 604 | |
Adam Litke | 3b11630 | 2008-04-28 02:13:06 -0700 | [diff] [blame] | 605 | if (ret) |
| 606 | count_vm_event(HTLB_BUDDY_PGALLOC); |
| 607 | else |
| 608 | count_vm_event(HTLB_BUDDY_PGALLOC_FAIL); |
| 609 | |
Nishanth Aravamudan | 63b4613 | 2007-10-16 01:26:24 -0700 | [diff] [blame] | 610 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 613 | static struct page *alloc_buddy_huge_page(struct hstate *h, |
| 614 | struct vm_area_struct *vma, unsigned long address) |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 615 | { |
| 616 | struct page *page; |
Nishanth Aravamudan | d1c3fb1 | 2007-12-17 16:20:12 -0800 | [diff] [blame] | 617 | unsigned int nid; |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 618 | |
Nishanth Aravamudan | d1c3fb1 | 2007-12-17 16:20:12 -0800 | [diff] [blame] | 619 | /* |
| 620 | * Assume we will successfully allocate the surplus page to |
| 621 | * prevent racing processes from causing the surplus to exceed |
| 622 | * overcommit |
| 623 | * |
| 624 | * This however introduces a different race, where a process B |
| 625 | * tries to grow the static hugepage pool while alloc_pages() is |
| 626 | * called by process A. B will only examine the per-node |
| 627 | * counters in determining if surplus huge pages can be |
| 628 | * converted to normal huge pages in adjust_pool_surplus(). A |
| 629 | * won't be able to increment the per-node counter, until the |
| 630 | * lock is dropped by B, but B doesn't drop hugetlb_lock until |
| 631 | * no more huge pages can be converted from surplus to normal |
| 632 | * state (and doesn't try to convert again). Thus, we have a |
| 633 | * case where a surplus huge page exists, the pool is grown, and |
| 634 | * the surplus huge page still exists after, even though it |
| 635 | * should just have been converted to a normal huge page. This |
| 636 | * does not leak memory, though, as the hugepage will be freed |
| 637 | * once it is out of use. It also does not allow the counters to |
| 638 | * go out of whack in adjust_pool_surplus() as we don't modify |
| 639 | * the node values until we've gotten the hugepage and only the |
| 640 | * per-node value is checked there. |
| 641 | */ |
| 642 | spin_lock(&hugetlb_lock); |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 643 | if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) { |
Nishanth Aravamudan | d1c3fb1 | 2007-12-17 16:20:12 -0800 | [diff] [blame] | 644 | spin_unlock(&hugetlb_lock); |
| 645 | return NULL; |
| 646 | } else { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 647 | h->nr_huge_pages++; |
| 648 | h->surplus_huge_pages++; |
Nishanth Aravamudan | d1c3fb1 | 2007-12-17 16:20:12 -0800 | [diff] [blame] | 649 | } |
| 650 | spin_unlock(&hugetlb_lock); |
| 651 | |
Nishanth Aravamudan | 551883a | 2008-04-29 00:58:26 -0700 | [diff] [blame] | 652 | page = alloc_pages(htlb_alloc_mask|__GFP_COMP| |
| 653 | __GFP_REPEAT|__GFP_NOWARN, |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 654 | huge_page_order(h)); |
Nishanth Aravamudan | d1c3fb1 | 2007-12-17 16:20:12 -0800 | [diff] [blame] | 655 | |
| 656 | spin_lock(&hugetlb_lock); |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 657 | if (page) { |
Adam Litke | 2668db9 | 2008-03-10 11:43:50 -0700 | [diff] [blame] | 658 | /* |
| 659 | * This page is now managed by the hugetlb allocator and has |
| 660 | * no users -- drop the buddy allocator's reference. |
| 661 | */ |
| 662 | put_page_testzero(page); |
| 663 | VM_BUG_ON(page_count(page)); |
Nishanth Aravamudan | d1c3fb1 | 2007-12-17 16:20:12 -0800 | [diff] [blame] | 664 | nid = page_to_nid(page); |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 665 | set_compound_page_dtor(page, free_huge_page); |
Nishanth Aravamudan | d1c3fb1 | 2007-12-17 16:20:12 -0800 | [diff] [blame] | 666 | /* |
| 667 | * We incremented the global counters already |
| 668 | */ |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 669 | h->nr_huge_pages_node[nid]++; |
| 670 | h->surplus_huge_pages_node[nid]++; |
Adam Litke | 3b11630 | 2008-04-28 02:13:06 -0700 | [diff] [blame] | 671 | __count_vm_event(HTLB_BUDDY_PGALLOC); |
Nishanth Aravamudan | d1c3fb1 | 2007-12-17 16:20:12 -0800 | [diff] [blame] | 672 | } else { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 673 | h->nr_huge_pages--; |
| 674 | h->surplus_huge_pages--; |
Adam Litke | 3b11630 | 2008-04-28 02:13:06 -0700 | [diff] [blame] | 675 | __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL); |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 676 | } |
Nishanth Aravamudan | d1c3fb1 | 2007-12-17 16:20:12 -0800 | [diff] [blame] | 677 | spin_unlock(&hugetlb_lock); |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 678 | |
| 679 | return page; |
| 680 | } |
| 681 | |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 682 | /* |
| 683 | * Increase the hugetlb pool such that it can accomodate a reservation |
| 684 | * of size 'delta'. |
| 685 | */ |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 686 | static int gather_surplus_pages(struct hstate *h, int delta) |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 687 | { |
| 688 | struct list_head surplus_list; |
| 689 | struct page *page, *tmp; |
| 690 | int ret, i; |
| 691 | int needed, allocated; |
| 692 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 693 | needed = (h->resv_huge_pages + delta) - h->free_huge_pages; |
Adam Litke | ac09b3a | 2008-03-04 14:29:38 -0800 | [diff] [blame] | 694 | if (needed <= 0) { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 695 | h->resv_huge_pages += delta; |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 696 | return 0; |
Adam Litke | ac09b3a | 2008-03-04 14:29:38 -0800 | [diff] [blame] | 697 | } |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 698 | |
| 699 | allocated = 0; |
| 700 | INIT_LIST_HEAD(&surplus_list); |
| 701 | |
| 702 | ret = -ENOMEM; |
| 703 | retry: |
| 704 | spin_unlock(&hugetlb_lock); |
| 705 | for (i = 0; i < needed; i++) { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 706 | page = alloc_buddy_huge_page(h, NULL, 0); |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 707 | if (!page) { |
| 708 | /* |
| 709 | * We were not able to allocate enough pages to |
| 710 | * satisfy the entire reservation so we free what |
| 711 | * we've allocated so far. |
| 712 | */ |
| 713 | spin_lock(&hugetlb_lock); |
| 714 | needed = 0; |
| 715 | goto free; |
| 716 | } |
| 717 | |
| 718 | list_add(&page->lru, &surplus_list); |
| 719 | } |
| 720 | allocated += needed; |
| 721 | |
| 722 | /* |
| 723 | * After retaking hugetlb_lock, we need to recalculate 'needed' |
| 724 | * because either resv_huge_pages or free_huge_pages may have changed. |
| 725 | */ |
| 726 | spin_lock(&hugetlb_lock); |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 727 | needed = (h->resv_huge_pages + delta) - |
| 728 | (h->free_huge_pages + allocated); |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 729 | if (needed > 0) |
| 730 | goto retry; |
| 731 | |
| 732 | /* |
| 733 | * The surplus_list now contains _at_least_ the number of extra pages |
| 734 | * needed to accomodate the reservation. Add the appropriate number |
| 735 | * of pages to the hugetlb pool and free the extras back to the buddy |
Adam Litke | ac09b3a | 2008-03-04 14:29:38 -0800 | [diff] [blame] | 736 | * allocator. Commit the entire reservation here to prevent another |
| 737 | * process from stealing the pages as they are added to the pool but |
| 738 | * before they are reserved. |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 739 | */ |
| 740 | needed += allocated; |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 741 | h->resv_huge_pages += delta; |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 742 | ret = 0; |
| 743 | free: |
Adam Litke | 19fc3f0 | 2008-04-28 02:12:20 -0700 | [diff] [blame] | 744 | /* Free the needed pages to the hugetlb pool */ |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 745 | list_for_each_entry_safe(page, tmp, &surplus_list, lru) { |
Adam Litke | 19fc3f0 | 2008-04-28 02:12:20 -0700 | [diff] [blame] | 746 | if ((--needed) < 0) |
| 747 | break; |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 748 | list_del(&page->lru); |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 749 | enqueue_huge_page(h, page); |
Adam Litke | 19fc3f0 | 2008-04-28 02:12:20 -0700 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | /* Free unnecessary surplus pages to the buddy allocator */ |
| 753 | if (!list_empty(&surplus_list)) { |
| 754 | spin_unlock(&hugetlb_lock); |
| 755 | list_for_each_entry_safe(page, tmp, &surplus_list, lru) { |
| 756 | list_del(&page->lru); |
Adam Litke | af767cb | 2007-10-16 01:26:25 -0700 | [diff] [blame] | 757 | /* |
Adam Litke | 2668db9 | 2008-03-10 11:43:50 -0700 | [diff] [blame] | 758 | * The page has a reference count of zero already, so |
| 759 | * call free_huge_page directly instead of using |
| 760 | * put_page. This must be done with hugetlb_lock |
Adam Litke | af767cb | 2007-10-16 01:26:25 -0700 | [diff] [blame] | 761 | * unlocked which is safe because free_huge_page takes |
| 762 | * hugetlb_lock before deciding how to free the page. |
| 763 | */ |
Adam Litke | 2668db9 | 2008-03-10 11:43:50 -0700 | [diff] [blame] | 764 | free_huge_page(page); |
Adam Litke | af767cb | 2007-10-16 01:26:25 -0700 | [diff] [blame] | 765 | } |
Adam Litke | 19fc3f0 | 2008-04-28 02:12:20 -0700 | [diff] [blame] | 766 | spin_lock(&hugetlb_lock); |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | return ret; |
| 770 | } |
| 771 | |
| 772 | /* |
| 773 | * When releasing a hugetlb pool reservation, any surplus pages that were |
| 774 | * allocated to satisfy the reservation must be explicitly freed if they were |
| 775 | * never used. |
| 776 | */ |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 777 | static void return_unused_surplus_pages(struct hstate *h, |
| 778 | unsigned long unused_resv_pages) |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 779 | { |
| 780 | static int nid = -1; |
| 781 | struct page *page; |
| 782 | unsigned long nr_pages; |
| 783 | |
Nishanth Aravamudan | 11320d1 | 2008-03-26 14:40:20 -0700 | [diff] [blame] | 784 | /* |
| 785 | * We want to release as many surplus pages as possible, spread |
| 786 | * evenly across all nodes. Iterate across all nodes until we |
| 787 | * can no longer free unreserved surplus pages. This occurs when |
| 788 | * the nodes with surplus pages have no free pages. |
| 789 | */ |
| 790 | unsigned long remaining_iterations = num_online_nodes(); |
| 791 | |
Adam Litke | ac09b3a | 2008-03-04 14:29:38 -0800 | [diff] [blame] | 792 | /* Uncommit the reservation */ |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 793 | h->resv_huge_pages -= unused_resv_pages; |
Adam Litke | ac09b3a | 2008-03-04 14:29:38 -0800 | [diff] [blame] | 794 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 795 | nr_pages = min(unused_resv_pages, h->surplus_huge_pages); |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 796 | |
Nishanth Aravamudan | 11320d1 | 2008-03-26 14:40:20 -0700 | [diff] [blame] | 797 | while (remaining_iterations-- && nr_pages) { |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 798 | nid = next_node(nid, node_online_map); |
| 799 | if (nid == MAX_NUMNODES) |
| 800 | nid = first_node(node_online_map); |
| 801 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 802 | if (!h->surplus_huge_pages_node[nid]) |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 803 | continue; |
| 804 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 805 | if (!list_empty(&h->hugepage_freelists[nid])) { |
| 806 | page = list_entry(h->hugepage_freelists[nid].next, |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 807 | struct page, lru); |
| 808 | list_del(&page->lru); |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 809 | update_and_free_page(h, page); |
| 810 | h->free_huge_pages--; |
| 811 | h->free_huge_pages_node[nid]--; |
| 812 | h->surplus_huge_pages--; |
| 813 | h->surplus_huge_pages_node[nid]--; |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 814 | nr_pages--; |
Nishanth Aravamudan | 11320d1 | 2008-03-26 14:40:20 -0700 | [diff] [blame] | 815 | remaining_iterations = num_online_nodes(); |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | } |
| 819 | |
Andy Whitcroft | c37f9fb | 2008-07-23 21:27:30 -0700 | [diff] [blame] | 820 | /* |
| 821 | * Determine if the huge page at addr within the vma has an associated |
| 822 | * reservation. Where it does not we will need to logically increase |
| 823 | * reservation and actually increase quota before an allocation can occur. |
| 824 | * Where any new reservation would be required the reservation change is |
| 825 | * prepared, but not committed. Once the page has been quota'd allocated |
| 826 | * an instantiated the change should be committed via vma_commit_reservation. |
| 827 | * No action is required on failure. |
| 828 | */ |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 829 | static int vma_needs_reservation(struct hstate *h, |
| 830 | struct vm_area_struct *vma, unsigned long addr) |
Andy Whitcroft | c37f9fb | 2008-07-23 21:27:30 -0700 | [diff] [blame] | 831 | { |
| 832 | struct address_space *mapping = vma->vm_file->f_mapping; |
| 833 | struct inode *inode = mapping->host; |
| 834 | |
| 835 | if (vma->vm_flags & VM_SHARED) { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 836 | pgoff_t idx = vma_hugecache_offset(h, vma, addr); |
Andy Whitcroft | c37f9fb | 2008-07-23 21:27:30 -0700 | [diff] [blame] | 837 | return region_chg(&inode->i_mapping->private_list, |
| 838 | idx, idx + 1); |
| 839 | |
Andy Whitcroft | 84afd99 | 2008-07-23 21:27:32 -0700 | [diff] [blame] | 840 | } else if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER)) { |
| 841 | return 1; |
Andy Whitcroft | c37f9fb | 2008-07-23 21:27:30 -0700 | [diff] [blame] | 842 | |
Andy Whitcroft | 84afd99 | 2008-07-23 21:27:32 -0700 | [diff] [blame] | 843 | } else { |
| 844 | int err; |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 845 | pgoff_t idx = vma_hugecache_offset(h, vma, addr); |
Andy Whitcroft | 84afd99 | 2008-07-23 21:27:32 -0700 | [diff] [blame] | 846 | struct resv_map *reservations = vma_resv_map(vma); |
| 847 | |
| 848 | err = region_chg(&reservations->regions, idx, idx + 1); |
| 849 | if (err < 0) |
| 850 | return err; |
| 851 | return 0; |
| 852 | } |
Andy Whitcroft | c37f9fb | 2008-07-23 21:27:30 -0700 | [diff] [blame] | 853 | } |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 854 | static void vma_commit_reservation(struct hstate *h, |
| 855 | struct vm_area_struct *vma, unsigned long addr) |
Andy Whitcroft | c37f9fb | 2008-07-23 21:27:30 -0700 | [diff] [blame] | 856 | { |
| 857 | struct address_space *mapping = vma->vm_file->f_mapping; |
| 858 | struct inode *inode = mapping->host; |
| 859 | |
| 860 | if (vma->vm_flags & VM_SHARED) { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 861 | pgoff_t idx = vma_hugecache_offset(h, vma, addr); |
Andy Whitcroft | c37f9fb | 2008-07-23 21:27:30 -0700 | [diff] [blame] | 862 | region_add(&inode->i_mapping->private_list, idx, idx + 1); |
Andy Whitcroft | 84afd99 | 2008-07-23 21:27:32 -0700 | [diff] [blame] | 863 | |
| 864 | } else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 865 | pgoff_t idx = vma_hugecache_offset(h, vma, addr); |
Andy Whitcroft | 84afd99 | 2008-07-23 21:27:32 -0700 | [diff] [blame] | 866 | struct resv_map *reservations = vma_resv_map(vma); |
| 867 | |
| 868 | /* Mark this page used in the map. */ |
| 869 | region_add(&reservations->regions, idx, idx + 1); |
Andy Whitcroft | c37f9fb | 2008-07-23 21:27:30 -0700 | [diff] [blame] | 870 | } |
| 871 | } |
| 872 | |
David Gibson | 27a85ef | 2006-03-22 00:08:56 -0800 | [diff] [blame] | 873 | static struct page *alloc_huge_page(struct vm_area_struct *vma, |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 874 | unsigned long addr, int avoid_reserve) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 876 | struct hstate *h = hstate_vma(vma); |
Adam Litke | 348ea20 | 2007-11-14 16:59:37 -0800 | [diff] [blame] | 877 | struct page *page; |
Adam Litke | 2fc39ce | 2007-11-14 16:59:39 -0800 | [diff] [blame] | 878 | struct address_space *mapping = vma->vm_file->f_mapping; |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 879 | struct inode *inode = mapping->host; |
Andy Whitcroft | c37f9fb | 2008-07-23 21:27:30 -0700 | [diff] [blame] | 880 | unsigned int chg; |
Adam Litke | 2fc39ce | 2007-11-14 16:59:39 -0800 | [diff] [blame] | 881 | |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 882 | /* |
| 883 | * Processes that did not create the mapping will have no reserves and |
| 884 | * will not have accounted against quota. Check that the quota can be |
| 885 | * made before satisfying the allocation |
Andy Whitcroft | c37f9fb | 2008-07-23 21:27:30 -0700 | [diff] [blame] | 886 | * MAP_NORESERVE mappings may also need pages and quota allocated |
| 887 | * if no reserve mapping overlaps. |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 888 | */ |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 889 | chg = vma_needs_reservation(h, vma, addr); |
Andy Whitcroft | c37f9fb | 2008-07-23 21:27:30 -0700 | [diff] [blame] | 890 | if (chg < 0) |
| 891 | return ERR_PTR(chg); |
| 892 | if (chg) |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 893 | if (hugetlb_get_quota(inode->i_mapping, chg)) |
| 894 | return ERR_PTR(-ENOSPC); |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 895 | |
| 896 | spin_lock(&hugetlb_lock); |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 897 | page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve); |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 898 | spin_unlock(&hugetlb_lock); |
| 899 | |
| 900 | if (!page) { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 901 | page = alloc_buddy_huge_page(h, vma, addr); |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 902 | if (!page) { |
| 903 | hugetlb_put_quota(inode->i_mapping, chg); |
| 904 | return ERR_PTR(-VM_FAULT_OOM); |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | set_page_refcounted(page); |
| 909 | set_page_private(page, (unsigned long) mapping); |
| 910 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 911 | vma_commit_reservation(h, vma, addr); |
Andy Whitcroft | c37f9fb | 2008-07-23 21:27:30 -0700 | [diff] [blame] | 912 | |
Adam Litke | 90d8b7e | 2007-11-14 16:59:42 -0800 | [diff] [blame] | 913 | return page; |
David Gibson | b45b5bd | 2006-03-22 00:08:55 -0800 | [diff] [blame] | 914 | } |
| 915 | |
Andi Kleen | e5ff215 | 2008-07-23 21:27:42 -0700 | [diff] [blame] | 916 | static void __init hugetlb_init_one_hstate(struct hstate *h) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | { |
| 918 | unsigned long i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 920 | for (i = 0; i < MAX_NUMNODES; ++i) |
| 921 | INIT_LIST_HEAD(&h->hugepage_freelists[i]); |
| 922 | |
| 923 | h->hugetlb_next_nid = first_node(node_online_map); |
Nishanth Aravamudan | 63b4613 | 2007-10-16 01:26:24 -0700 | [diff] [blame] | 924 | |
Andi Kleen | e5ff215 | 2008-07-23 21:27:42 -0700 | [diff] [blame] | 925 | for (i = 0; i < h->max_huge_pages; ++i) { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 926 | if (!alloc_fresh_huge_page(h)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | } |
Andi Kleen | e5ff215 | 2008-07-23 21:27:42 -0700 | [diff] [blame] | 929 | h->max_huge_pages = h->free_huge_pages = h->nr_huge_pages = i; |
| 930 | } |
| 931 | |
| 932 | static void __init hugetlb_init_hstates(void) |
| 933 | { |
| 934 | struct hstate *h; |
| 935 | |
| 936 | for_each_hstate(h) { |
| 937 | hugetlb_init_one_hstate(h); |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | static void __init report_hugepages(void) |
| 942 | { |
| 943 | struct hstate *h; |
| 944 | |
| 945 | for_each_hstate(h) { |
| 946 | printk(KERN_INFO "Total HugeTLB memory allocated, " |
| 947 | "%ld %dMB pages\n", |
| 948 | h->free_huge_pages, |
| 949 | 1 << (h->order + PAGE_SHIFT - 20)); |
| 950 | } |
| 951 | } |
| 952 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | #ifdef CONFIG_SYSCTL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | #ifdef CONFIG_HIGHMEM |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 955 | static void try_to_free_low(struct hstate *h, unsigned long count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | { |
Christoph Lameter | 4415cc8 | 2006-09-25 23:31:55 -0700 | [diff] [blame] | 957 | int i; |
| 958 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | for (i = 0; i < MAX_NUMNODES; ++i) { |
| 960 | struct page *page, *next; |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 961 | struct list_head *freel = &h->hugepage_freelists[i]; |
| 962 | list_for_each_entry_safe(page, next, freel, lru) { |
| 963 | if (count >= h->nr_huge_pages) |
Adam Litke | 6b0c880 | 2007-10-16 01:26:23 -0700 | [diff] [blame] | 964 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | if (PageHighMem(page)) |
| 966 | continue; |
| 967 | list_del(&page->lru); |
Andi Kleen | e5ff215 | 2008-07-23 21:27:42 -0700 | [diff] [blame] | 968 | update_and_free_page(h, page); |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 969 | h->free_huge_pages--; |
| 970 | h->free_huge_pages_node[page_to_nid(page)]--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | } |
| 974 | #else |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 975 | static inline void try_to_free_low(struct hstate *h, unsigned long count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | { |
| 977 | } |
| 978 | #endif |
| 979 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 980 | #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages) |
Andi Kleen | e5ff215 | 2008-07-23 21:27:42 -0700 | [diff] [blame] | 981 | static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | { |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 983 | unsigned long min_count, ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 985 | /* |
| 986 | * Increase the pool size |
| 987 | * First take pages out of surplus state. Then make up the |
| 988 | * remaining difference by allocating fresh huge pages. |
Nishanth Aravamudan | d1c3fb1 | 2007-12-17 16:20:12 -0800 | [diff] [blame] | 989 | * |
| 990 | * We might race with alloc_buddy_huge_page() here and be unable |
| 991 | * to convert a surplus huge page to a normal huge page. That is |
| 992 | * not critical, though, it just means the overall size of the |
| 993 | * pool might be one hugepage larger than it needs to be, but |
| 994 | * within all the constraints specified by the sysctls. |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 995 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | spin_lock(&hugetlb_lock); |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 997 | while (h->surplus_huge_pages && count > persistent_huge_pages(h)) { |
| 998 | if (!adjust_pool_surplus(h, -1)) |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 999 | break; |
| 1000 | } |
| 1001 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1002 | while (count > persistent_huge_pages(h)) { |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 1003 | /* |
| 1004 | * If this allocation races such that we no longer need the |
| 1005 | * page, free_huge_page will handle it by freeing the page |
| 1006 | * and reducing the surplus. |
| 1007 | */ |
| 1008 | spin_unlock(&hugetlb_lock); |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1009 | ret = alloc_fresh_huge_page(h); |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 1010 | spin_lock(&hugetlb_lock); |
| 1011 | if (!ret) |
| 1012 | goto out; |
| 1013 | |
| 1014 | } |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 1015 | |
| 1016 | /* |
| 1017 | * Decrease the pool size |
| 1018 | * First return free pages to the buddy allocator (being careful |
| 1019 | * to keep enough around to satisfy reservations). Then place |
| 1020 | * pages into surplus state as needed so the pool will shrink |
| 1021 | * to the desired size as pages become free. |
Nishanth Aravamudan | d1c3fb1 | 2007-12-17 16:20:12 -0800 | [diff] [blame] | 1022 | * |
| 1023 | * By placing pages into the surplus state independent of the |
| 1024 | * overcommit value, we are allowing the surplus pool size to |
| 1025 | * exceed overcommit. There are few sane options here. Since |
| 1026 | * alloc_buddy_huge_page() is checking the global counter, |
| 1027 | * though, we'll note that we're not allowed to exceed surplus |
| 1028 | * and won't grow the pool anywhere else. Not until one of the |
| 1029 | * sysctls are changed, or the surplus pages go out of use. |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 1030 | */ |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1031 | min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages; |
Adam Litke | 6b0c880 | 2007-10-16 01:26:23 -0700 | [diff] [blame] | 1032 | min_count = max(count, min_count); |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1033 | try_to_free_low(h, min_count); |
| 1034 | while (min_count < persistent_huge_pages(h)) { |
| 1035 | struct page *page = dequeue_huge_page(h); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | if (!page) |
| 1037 | break; |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1038 | update_and_free_page(h, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | } |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1040 | while (count < persistent_huge_pages(h)) { |
| 1041 | if (!adjust_pool_surplus(h, 1)) |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 1042 | break; |
| 1043 | } |
| 1044 | out: |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1045 | ret = persistent_huge_pages(h); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | spin_unlock(&hugetlb_lock); |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 1047 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
Nishanth Aravamudan | a343787 | 2008-07-23 21:27:44 -0700 | [diff] [blame] | 1050 | #define HSTATE_ATTR_RO(_name) \ |
| 1051 | static struct kobj_attribute _name##_attr = __ATTR_RO(_name) |
| 1052 | |
| 1053 | #define HSTATE_ATTR(_name) \ |
| 1054 | static struct kobj_attribute _name##_attr = \ |
| 1055 | __ATTR(_name, 0644, _name##_show, _name##_store) |
| 1056 | |
| 1057 | static struct kobject *hugepages_kobj; |
| 1058 | static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE]; |
| 1059 | |
| 1060 | static struct hstate *kobj_to_hstate(struct kobject *kobj) |
| 1061 | { |
| 1062 | int i; |
| 1063 | for (i = 0; i < HUGE_MAX_HSTATE; i++) |
| 1064 | if (hstate_kobjs[i] == kobj) |
| 1065 | return &hstates[i]; |
| 1066 | BUG(); |
| 1067 | return NULL; |
| 1068 | } |
| 1069 | |
| 1070 | static ssize_t nr_hugepages_show(struct kobject *kobj, |
| 1071 | struct kobj_attribute *attr, char *buf) |
| 1072 | { |
| 1073 | struct hstate *h = kobj_to_hstate(kobj); |
| 1074 | return sprintf(buf, "%lu\n", h->nr_huge_pages); |
| 1075 | } |
| 1076 | static ssize_t nr_hugepages_store(struct kobject *kobj, |
| 1077 | struct kobj_attribute *attr, const char *buf, size_t count) |
| 1078 | { |
| 1079 | int err; |
| 1080 | unsigned long input; |
| 1081 | struct hstate *h = kobj_to_hstate(kobj); |
| 1082 | |
| 1083 | err = strict_strtoul(buf, 10, &input); |
| 1084 | if (err) |
| 1085 | return 0; |
| 1086 | |
| 1087 | h->max_huge_pages = set_max_huge_pages(h, input); |
| 1088 | |
| 1089 | return count; |
| 1090 | } |
| 1091 | HSTATE_ATTR(nr_hugepages); |
| 1092 | |
| 1093 | static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj, |
| 1094 | struct kobj_attribute *attr, char *buf) |
| 1095 | { |
| 1096 | struct hstate *h = kobj_to_hstate(kobj); |
| 1097 | return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages); |
| 1098 | } |
| 1099 | static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj, |
| 1100 | struct kobj_attribute *attr, const char *buf, size_t count) |
| 1101 | { |
| 1102 | int err; |
| 1103 | unsigned long input; |
| 1104 | struct hstate *h = kobj_to_hstate(kobj); |
| 1105 | |
| 1106 | err = strict_strtoul(buf, 10, &input); |
| 1107 | if (err) |
| 1108 | return 0; |
| 1109 | |
| 1110 | spin_lock(&hugetlb_lock); |
| 1111 | h->nr_overcommit_huge_pages = input; |
| 1112 | spin_unlock(&hugetlb_lock); |
| 1113 | |
| 1114 | return count; |
| 1115 | } |
| 1116 | HSTATE_ATTR(nr_overcommit_hugepages); |
| 1117 | |
| 1118 | static ssize_t free_hugepages_show(struct kobject *kobj, |
| 1119 | struct kobj_attribute *attr, char *buf) |
| 1120 | { |
| 1121 | struct hstate *h = kobj_to_hstate(kobj); |
| 1122 | return sprintf(buf, "%lu\n", h->free_huge_pages); |
| 1123 | } |
| 1124 | HSTATE_ATTR_RO(free_hugepages); |
| 1125 | |
| 1126 | static ssize_t resv_hugepages_show(struct kobject *kobj, |
| 1127 | struct kobj_attribute *attr, char *buf) |
| 1128 | { |
| 1129 | struct hstate *h = kobj_to_hstate(kobj); |
| 1130 | return sprintf(buf, "%lu\n", h->resv_huge_pages); |
| 1131 | } |
| 1132 | HSTATE_ATTR_RO(resv_hugepages); |
| 1133 | |
| 1134 | static ssize_t surplus_hugepages_show(struct kobject *kobj, |
| 1135 | struct kobj_attribute *attr, char *buf) |
| 1136 | { |
| 1137 | struct hstate *h = kobj_to_hstate(kobj); |
| 1138 | return sprintf(buf, "%lu\n", h->surplus_huge_pages); |
| 1139 | } |
| 1140 | HSTATE_ATTR_RO(surplus_hugepages); |
| 1141 | |
| 1142 | static struct attribute *hstate_attrs[] = { |
| 1143 | &nr_hugepages_attr.attr, |
| 1144 | &nr_overcommit_hugepages_attr.attr, |
| 1145 | &free_hugepages_attr.attr, |
| 1146 | &resv_hugepages_attr.attr, |
| 1147 | &surplus_hugepages_attr.attr, |
| 1148 | NULL, |
| 1149 | }; |
| 1150 | |
| 1151 | static struct attribute_group hstate_attr_group = { |
| 1152 | .attrs = hstate_attrs, |
| 1153 | }; |
| 1154 | |
| 1155 | static int __init hugetlb_sysfs_add_hstate(struct hstate *h) |
| 1156 | { |
| 1157 | int retval; |
| 1158 | |
| 1159 | hstate_kobjs[h - hstates] = kobject_create_and_add(h->name, |
| 1160 | hugepages_kobj); |
| 1161 | if (!hstate_kobjs[h - hstates]) |
| 1162 | return -ENOMEM; |
| 1163 | |
| 1164 | retval = sysfs_create_group(hstate_kobjs[h - hstates], |
| 1165 | &hstate_attr_group); |
| 1166 | if (retval) |
| 1167 | kobject_put(hstate_kobjs[h - hstates]); |
| 1168 | |
| 1169 | return retval; |
| 1170 | } |
| 1171 | |
| 1172 | static void __init hugetlb_sysfs_init(void) |
| 1173 | { |
| 1174 | struct hstate *h; |
| 1175 | int err; |
| 1176 | |
| 1177 | hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj); |
| 1178 | if (!hugepages_kobj) |
| 1179 | return; |
| 1180 | |
| 1181 | for_each_hstate(h) { |
| 1182 | err = hugetlb_sysfs_add_hstate(h); |
| 1183 | if (err) |
| 1184 | printk(KERN_ERR "Hugetlb: Unable to add hstate %s", |
| 1185 | h->name); |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | static void __exit hugetlb_exit(void) |
| 1190 | { |
| 1191 | struct hstate *h; |
| 1192 | |
| 1193 | for_each_hstate(h) { |
| 1194 | kobject_put(hstate_kobjs[h - hstates]); |
| 1195 | } |
| 1196 | |
| 1197 | kobject_put(hugepages_kobj); |
| 1198 | } |
| 1199 | module_exit(hugetlb_exit); |
| 1200 | |
| 1201 | static int __init hugetlb_init(void) |
| 1202 | { |
| 1203 | BUILD_BUG_ON(HPAGE_SHIFT == 0); |
| 1204 | |
| 1205 | if (!size_to_hstate(HPAGE_SIZE)) { |
| 1206 | hugetlb_add_hstate(HUGETLB_PAGE_ORDER); |
| 1207 | parsed_hstate->max_huge_pages = default_hstate_max_huge_pages; |
| 1208 | } |
| 1209 | default_hstate_idx = size_to_hstate(HPAGE_SIZE) - hstates; |
| 1210 | |
| 1211 | hugetlb_init_hstates(); |
| 1212 | |
| 1213 | report_hugepages(); |
| 1214 | |
| 1215 | hugetlb_sysfs_init(); |
| 1216 | |
| 1217 | return 0; |
| 1218 | } |
| 1219 | module_init(hugetlb_init); |
| 1220 | |
| 1221 | /* Should be called on processing a hugepagesz=... option */ |
| 1222 | void __init hugetlb_add_hstate(unsigned order) |
| 1223 | { |
| 1224 | struct hstate *h; |
| 1225 | if (size_to_hstate(PAGE_SIZE << order)) { |
| 1226 | printk(KERN_WARNING "hugepagesz= specified twice, ignoring\n"); |
| 1227 | return; |
| 1228 | } |
| 1229 | BUG_ON(max_hstate >= HUGE_MAX_HSTATE); |
| 1230 | BUG_ON(order == 0); |
| 1231 | h = &hstates[max_hstate++]; |
| 1232 | h->order = order; |
| 1233 | h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1); |
| 1234 | snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB", |
| 1235 | huge_page_size(h)/1024); |
| 1236 | hugetlb_init_one_hstate(h); |
| 1237 | parsed_hstate = h; |
| 1238 | } |
| 1239 | |
| 1240 | static int __init hugetlb_setup(char *s) |
| 1241 | { |
| 1242 | unsigned long *mhp; |
| 1243 | |
| 1244 | /* |
| 1245 | * !max_hstate means we haven't parsed a hugepagesz= parameter yet, |
| 1246 | * so this hugepages= parameter goes to the "default hstate". |
| 1247 | */ |
| 1248 | if (!max_hstate) |
| 1249 | mhp = &default_hstate_max_huge_pages; |
| 1250 | else |
| 1251 | mhp = &parsed_hstate->max_huge_pages; |
| 1252 | |
| 1253 | if (sscanf(s, "%lu", mhp) <= 0) |
| 1254 | *mhp = 0; |
| 1255 | |
| 1256 | return 1; |
| 1257 | } |
| 1258 | __setup("hugepages=", hugetlb_setup); |
| 1259 | |
| 1260 | static unsigned int cpuset_mems_nr(unsigned int *array) |
| 1261 | { |
| 1262 | int node; |
| 1263 | unsigned int nr = 0; |
| 1264 | |
| 1265 | for_each_node_mask(node, cpuset_current_mems_allowed) |
| 1266 | nr += array[node]; |
| 1267 | |
| 1268 | return nr; |
| 1269 | } |
| 1270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | int hugetlb_sysctl_handler(struct ctl_table *table, int write, |
| 1272 | struct file *file, void __user *buffer, |
| 1273 | size_t *length, loff_t *ppos) |
| 1274 | { |
Andi Kleen | e5ff215 | 2008-07-23 21:27:42 -0700 | [diff] [blame] | 1275 | struct hstate *h = &default_hstate; |
| 1276 | unsigned long tmp; |
| 1277 | |
| 1278 | if (!write) |
| 1279 | tmp = h->max_huge_pages; |
| 1280 | |
| 1281 | table->data = &tmp; |
| 1282 | table->maxlen = sizeof(unsigned long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | proc_doulongvec_minmax(table, write, file, buffer, length, ppos); |
Andi Kleen | e5ff215 | 2008-07-23 21:27:42 -0700 | [diff] [blame] | 1284 | |
| 1285 | if (write) |
| 1286 | h->max_huge_pages = set_max_huge_pages(h, tmp); |
| 1287 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | return 0; |
| 1289 | } |
Mel Gorman | 396faf0 | 2007-07-17 04:03:13 -0700 | [diff] [blame] | 1290 | |
| 1291 | int hugetlb_treat_movable_handler(struct ctl_table *table, int write, |
| 1292 | struct file *file, void __user *buffer, |
| 1293 | size_t *length, loff_t *ppos) |
| 1294 | { |
| 1295 | proc_dointvec(table, write, file, buffer, length, ppos); |
| 1296 | if (hugepages_treat_as_movable) |
| 1297 | htlb_alloc_mask = GFP_HIGHUSER_MOVABLE; |
| 1298 | else |
| 1299 | htlb_alloc_mask = GFP_HIGHUSER; |
| 1300 | return 0; |
| 1301 | } |
| 1302 | |
Nishanth Aravamudan | a3d0c6a | 2008-02-08 04:18:18 -0800 | [diff] [blame] | 1303 | int hugetlb_overcommit_handler(struct ctl_table *table, int write, |
| 1304 | struct file *file, void __user *buffer, |
| 1305 | size_t *length, loff_t *ppos) |
| 1306 | { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1307 | struct hstate *h = &default_hstate; |
Andi Kleen | e5ff215 | 2008-07-23 21:27:42 -0700 | [diff] [blame] | 1308 | unsigned long tmp; |
| 1309 | |
| 1310 | if (!write) |
| 1311 | tmp = h->nr_overcommit_huge_pages; |
| 1312 | |
| 1313 | table->data = &tmp; |
| 1314 | table->maxlen = sizeof(unsigned long); |
Nishanth Aravamudan | a3d0c6a | 2008-02-08 04:18:18 -0800 | [diff] [blame] | 1315 | proc_doulongvec_minmax(table, write, file, buffer, length, ppos); |
Andi Kleen | e5ff215 | 2008-07-23 21:27:42 -0700 | [diff] [blame] | 1316 | |
| 1317 | if (write) { |
| 1318 | spin_lock(&hugetlb_lock); |
| 1319 | h->nr_overcommit_huge_pages = tmp; |
| 1320 | spin_unlock(&hugetlb_lock); |
| 1321 | } |
| 1322 | |
Nishanth Aravamudan | a3d0c6a | 2008-02-08 04:18:18 -0800 | [diff] [blame] | 1323 | return 0; |
| 1324 | } |
| 1325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | #endif /* CONFIG_SYSCTL */ |
| 1327 | |
| 1328 | int hugetlb_report_meminfo(char *buf) |
| 1329 | { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1330 | struct hstate *h = &default_hstate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | return sprintf(buf, |
| 1332 | "HugePages_Total: %5lu\n" |
| 1333 | "HugePages_Free: %5lu\n" |
Chen, Kenneth W | a43a8c3 | 2006-06-23 02:03:15 -0700 | [diff] [blame] | 1334 | "HugePages_Rsvd: %5lu\n" |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 1335 | "HugePages_Surp: %5lu\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | "Hugepagesize: %5lu kB\n", |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1337 | h->nr_huge_pages, |
| 1338 | h->free_huge_pages, |
| 1339 | h->resv_huge_pages, |
| 1340 | h->surplus_huge_pages, |
| 1341 | 1UL << (huge_page_order(h) + PAGE_SHIFT - 10)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | int hugetlb_report_node_meminfo(int nid, char *buf) |
| 1345 | { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1346 | struct hstate *h = &default_hstate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | return sprintf(buf, |
| 1348 | "Node %d HugePages_Total: %5u\n" |
Nishanth Aravamudan | a1de091 | 2008-03-26 14:37:53 -0700 | [diff] [blame] | 1349 | "Node %d HugePages_Free: %5u\n" |
| 1350 | "Node %d HugePages_Surp: %5u\n", |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1351 | nid, h->nr_huge_pages_node[nid], |
| 1352 | nid, h->free_huge_pages_node[nid], |
| 1353 | nid, h->surplus_huge_pages_node[nid]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | } |
| 1355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | /* Return the number pages of memory we physically have, in PAGE_SIZE units. */ |
| 1357 | unsigned long hugetlb_total_pages(void) |
| 1358 | { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1359 | struct hstate *h = &default_hstate; |
| 1360 | return h->nr_huge_pages * pages_per_huge_page(h); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1363 | static int hugetlb_acct_memory(struct hstate *h, long delta) |
Mel Gorman | fc1b8a7 | 2008-07-23 21:27:22 -0700 | [diff] [blame] | 1364 | { |
| 1365 | int ret = -ENOMEM; |
| 1366 | |
| 1367 | spin_lock(&hugetlb_lock); |
| 1368 | /* |
| 1369 | * When cpuset is configured, it breaks the strict hugetlb page |
| 1370 | * reservation as the accounting is done on a global variable. Such |
| 1371 | * reservation is completely rubbish in the presence of cpuset because |
| 1372 | * the reservation is not checked against page availability for the |
| 1373 | * current cpuset. Application can still potentially OOM'ed by kernel |
| 1374 | * with lack of free htlb page in cpuset that the task is in. |
| 1375 | * Attempt to enforce strict accounting with cpuset is almost |
| 1376 | * impossible (or too ugly) because cpuset is too fluid that |
| 1377 | * task or memory node can be dynamically moved between cpusets. |
| 1378 | * |
| 1379 | * The change of semantics for shared hugetlb mapping with cpuset is |
| 1380 | * undesirable. However, in order to preserve some of the semantics, |
| 1381 | * we fall back to check against current free page availability as |
| 1382 | * a best attempt and hopefully to minimize the impact of changing |
| 1383 | * semantics that cpuset has. |
| 1384 | */ |
| 1385 | if (delta > 0) { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1386 | if (gather_surplus_pages(h, delta) < 0) |
Mel Gorman | fc1b8a7 | 2008-07-23 21:27:22 -0700 | [diff] [blame] | 1387 | goto out; |
| 1388 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1389 | if (delta > cpuset_mems_nr(h->free_huge_pages_node)) { |
| 1390 | return_unused_surplus_pages(h, delta); |
Mel Gorman | fc1b8a7 | 2008-07-23 21:27:22 -0700 | [diff] [blame] | 1391 | goto out; |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | ret = 0; |
| 1396 | if (delta < 0) |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1397 | return_unused_surplus_pages(h, (unsigned long) -delta); |
Mel Gorman | fc1b8a7 | 2008-07-23 21:27:22 -0700 | [diff] [blame] | 1398 | |
| 1399 | out: |
| 1400 | spin_unlock(&hugetlb_lock); |
| 1401 | return ret; |
| 1402 | } |
| 1403 | |
Andy Whitcroft | 84afd99 | 2008-07-23 21:27:32 -0700 | [diff] [blame] | 1404 | static void hugetlb_vm_op_open(struct vm_area_struct *vma) |
| 1405 | { |
| 1406 | struct resv_map *reservations = vma_resv_map(vma); |
| 1407 | |
| 1408 | /* |
| 1409 | * This new VMA should share its siblings reservation map if present. |
| 1410 | * The VMA will only ever have a valid reservation map pointer where |
| 1411 | * it is being copied for another still existing VMA. As that VMA |
| 1412 | * has a reference to the reservation map it cannot dissappear until |
| 1413 | * after this open call completes. It is therefore safe to take a |
| 1414 | * new reference here without additional locking. |
| 1415 | */ |
| 1416 | if (reservations) |
| 1417 | kref_get(&reservations->refs); |
| 1418 | } |
| 1419 | |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 1420 | static void hugetlb_vm_op_close(struct vm_area_struct *vma) |
| 1421 | { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1422 | struct hstate *h = hstate_vma(vma); |
Andy Whitcroft | 84afd99 | 2008-07-23 21:27:32 -0700 | [diff] [blame] | 1423 | struct resv_map *reservations = vma_resv_map(vma); |
| 1424 | unsigned long reserve; |
| 1425 | unsigned long start; |
| 1426 | unsigned long end; |
| 1427 | |
| 1428 | if (reservations) { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1429 | start = vma_hugecache_offset(h, vma, vma->vm_start); |
| 1430 | end = vma_hugecache_offset(h, vma, vma->vm_end); |
Andy Whitcroft | 84afd99 | 2008-07-23 21:27:32 -0700 | [diff] [blame] | 1431 | |
| 1432 | reserve = (end - start) - |
| 1433 | region_count(&reservations->regions, start, end); |
| 1434 | |
| 1435 | kref_put(&reservations->refs, resv_map_release); |
| 1436 | |
| 1437 | if (reserve) |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1438 | hugetlb_acct_memory(h, -reserve); |
Andy Whitcroft | 84afd99 | 2008-07-23 21:27:32 -0700 | [diff] [blame] | 1439 | } |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 1440 | } |
| 1441 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | /* |
| 1443 | * We cannot handle pagefaults against hugetlb pages at all. They cause |
| 1444 | * handle_mm_fault() to try to instantiate regular-sized pages in the |
| 1445 | * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get |
| 1446 | * this far. |
| 1447 | */ |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1448 | static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | { |
| 1450 | BUG(); |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1451 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | struct vm_operations_struct hugetlb_vm_ops = { |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 1455 | .fault = hugetlb_vm_op_fault, |
Andy Whitcroft | 84afd99 | 2008-07-23 21:27:32 -0700 | [diff] [blame] | 1456 | .open = hugetlb_vm_op_open, |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 1457 | .close = hugetlb_vm_op_close, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | }; |
| 1459 | |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1460 | static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page, |
| 1461 | int writable) |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1462 | { |
| 1463 | pte_t entry; |
| 1464 | |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1465 | if (writable) { |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1466 | entry = |
| 1467 | pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot))); |
| 1468 | } else { |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 1469 | entry = huge_pte_wrprotect(mk_pte(page, vma->vm_page_prot)); |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1470 | } |
| 1471 | entry = pte_mkyoung(entry); |
| 1472 | entry = pte_mkhuge(entry); |
| 1473 | |
| 1474 | return entry; |
| 1475 | } |
| 1476 | |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1477 | static void set_huge_ptep_writable(struct vm_area_struct *vma, |
| 1478 | unsigned long address, pte_t *ptep) |
| 1479 | { |
| 1480 | pte_t entry; |
| 1481 | |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 1482 | entry = pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep))); |
| 1483 | if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) { |
Benjamin Herrenschmidt | 8dab524 | 2007-06-16 10:16:12 -0700 | [diff] [blame] | 1484 | update_mmu_cache(vma, address, entry); |
Benjamin Herrenschmidt | 8dab524 | 2007-06-16 10:16:12 -0700 | [diff] [blame] | 1485 | } |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1486 | } |
| 1487 | |
| 1488 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1489 | int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, |
| 1490 | struct vm_area_struct *vma) |
| 1491 | { |
| 1492 | pte_t *src_pte, *dst_pte, entry; |
| 1493 | struct page *ptepage; |
Hugh Dickins | 1c59827 | 2005-10-19 21:23:43 -0700 | [diff] [blame] | 1494 | unsigned long addr; |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1495 | int cow; |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1496 | struct hstate *h = hstate_vma(vma); |
| 1497 | unsigned long sz = huge_page_size(h); |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1498 | |
| 1499 | cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1500 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1501 | for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) { |
Hugh Dickins | c74df32 | 2005-10-29 18:16:23 -0700 | [diff] [blame] | 1502 | src_pte = huge_pte_offset(src, addr); |
| 1503 | if (!src_pte) |
| 1504 | continue; |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1505 | dst_pte = huge_pte_alloc(dst, addr, sz); |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1506 | if (!dst_pte) |
| 1507 | goto nomem; |
Larry Woodman | c5c9942 | 2008-01-24 05:49:25 -0800 | [diff] [blame] | 1508 | |
| 1509 | /* If the pagetables are shared don't copy or take references */ |
| 1510 | if (dst_pte == src_pte) |
| 1511 | continue; |
| 1512 | |
Hugh Dickins | c74df32 | 2005-10-29 18:16:23 -0700 | [diff] [blame] | 1513 | spin_lock(&dst->page_table_lock); |
Nick Piggin | 4647875 | 2008-06-05 22:45:57 -0700 | [diff] [blame] | 1514 | spin_lock_nested(&src->page_table_lock, SINGLE_DEPTH_NESTING); |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 1515 | if (!huge_pte_none(huge_ptep_get(src_pte))) { |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1516 | if (cow) |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 1517 | huge_ptep_set_wrprotect(src, addr, src_pte); |
| 1518 | entry = huge_ptep_get(src_pte); |
Hugh Dickins | 1c59827 | 2005-10-19 21:23:43 -0700 | [diff] [blame] | 1519 | ptepage = pte_page(entry); |
| 1520 | get_page(ptepage); |
Hugh Dickins | 1c59827 | 2005-10-19 21:23:43 -0700 | [diff] [blame] | 1521 | set_huge_pte_at(dst, addr, dst_pte, entry); |
| 1522 | } |
| 1523 | spin_unlock(&src->page_table_lock); |
Hugh Dickins | c74df32 | 2005-10-29 18:16:23 -0700 | [diff] [blame] | 1524 | spin_unlock(&dst->page_table_lock); |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1525 | } |
| 1526 | return 0; |
| 1527 | |
| 1528 | nomem: |
| 1529 | return -ENOMEM; |
| 1530 | } |
| 1531 | |
Chen, Kenneth W | 502717f | 2006-10-11 01:20:46 -0700 | [diff] [blame] | 1532 | void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start, |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 1533 | unsigned long end, struct page *ref_page) |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1534 | { |
| 1535 | struct mm_struct *mm = vma->vm_mm; |
| 1536 | unsigned long address; |
David Gibson | c7546f8 | 2005-08-05 11:59:35 -0700 | [diff] [blame] | 1537 | pte_t *ptep; |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1538 | pte_t pte; |
| 1539 | struct page *page; |
Chen, Kenneth W | fe1668a | 2006-10-04 02:15:24 -0700 | [diff] [blame] | 1540 | struct page *tmp; |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1541 | struct hstate *h = hstate_vma(vma); |
| 1542 | unsigned long sz = huge_page_size(h); |
| 1543 | |
Chen, Kenneth W | c0a499c | 2006-12-06 20:31:39 -0800 | [diff] [blame] | 1544 | /* |
| 1545 | * A page gathering list, protected by per file i_mmap_lock. The |
| 1546 | * lock is used to avoid list corruption from multiple unmapping |
| 1547 | * of the same page since we are using page->lru. |
| 1548 | */ |
Chen, Kenneth W | fe1668a | 2006-10-04 02:15:24 -0700 | [diff] [blame] | 1549 | LIST_HEAD(page_list); |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1550 | |
| 1551 | WARN_ON(!is_vm_hugetlb_page(vma)); |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1552 | BUG_ON(start & ~huge_page_mask(h)); |
| 1553 | BUG_ON(end & ~huge_page_mask(h)); |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1554 | |
Hugh Dickins | 508034a | 2005-10-29 18:16:30 -0700 | [diff] [blame] | 1555 | spin_lock(&mm->page_table_lock); |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1556 | for (address = start; address < end; address += sz) { |
David Gibson | c7546f8 | 2005-08-05 11:59:35 -0700 | [diff] [blame] | 1557 | ptep = huge_pte_offset(mm, address); |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1558 | if (!ptep) |
David Gibson | c7546f8 | 2005-08-05 11:59:35 -0700 | [diff] [blame] | 1559 | continue; |
| 1560 | |
Chen, Kenneth W | 39dde65 | 2006-12-06 20:32:03 -0800 | [diff] [blame] | 1561 | if (huge_pmd_unshare(mm, &address, ptep)) |
| 1562 | continue; |
| 1563 | |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 1564 | /* |
| 1565 | * If a reference page is supplied, it is because a specific |
| 1566 | * page is being unmapped, not a range. Ensure the page we |
| 1567 | * are about to unmap is the actual page of interest. |
| 1568 | */ |
| 1569 | if (ref_page) { |
| 1570 | pte = huge_ptep_get(ptep); |
| 1571 | if (huge_pte_none(pte)) |
| 1572 | continue; |
| 1573 | page = pte_page(pte); |
| 1574 | if (page != ref_page) |
| 1575 | continue; |
| 1576 | |
| 1577 | /* |
| 1578 | * Mark the VMA as having unmapped its page so that |
| 1579 | * future faults in this VMA will fail rather than |
| 1580 | * looking like data was lost |
| 1581 | */ |
| 1582 | set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED); |
| 1583 | } |
| 1584 | |
David Gibson | c7546f8 | 2005-08-05 11:59:35 -0700 | [diff] [blame] | 1585 | pte = huge_ptep_get_and_clear(mm, address, ptep); |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 1586 | if (huge_pte_none(pte)) |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1587 | continue; |
David Gibson | c7546f8 | 2005-08-05 11:59:35 -0700 | [diff] [blame] | 1588 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1589 | page = pte_page(pte); |
Ken Chen | 6649a38 | 2007-02-08 14:20:27 -0800 | [diff] [blame] | 1590 | if (pte_dirty(pte)) |
| 1591 | set_page_dirty(page); |
Chen, Kenneth W | fe1668a | 2006-10-04 02:15:24 -0700 | [diff] [blame] | 1592 | list_add(&page->lru, &page_list); |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1593 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | spin_unlock(&mm->page_table_lock); |
Hugh Dickins | 508034a | 2005-10-29 18:16:30 -0700 | [diff] [blame] | 1595 | flush_tlb_range(vma, start, end); |
Chen, Kenneth W | fe1668a | 2006-10-04 02:15:24 -0700 | [diff] [blame] | 1596 | list_for_each_entry_safe(page, tmp, &page_list, lru) { |
| 1597 | list_del(&page->lru); |
| 1598 | put_page(page); |
| 1599 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | } |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1601 | |
Chen, Kenneth W | 502717f | 2006-10-11 01:20:46 -0700 | [diff] [blame] | 1602 | void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start, |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 1603 | unsigned long end, struct page *ref_page) |
Chen, Kenneth W | 502717f | 2006-10-11 01:20:46 -0700 | [diff] [blame] | 1604 | { |
Andi Kleen | a137e1c | 2008-07-23 21:27:43 -0700 | [diff] [blame] | 1605 | spin_lock(&vma->vm_file->f_mapping->i_mmap_lock); |
| 1606 | __unmap_hugepage_range(vma, start, end, ref_page); |
| 1607 | spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock); |
Chen, Kenneth W | 502717f | 2006-10-11 01:20:46 -0700 | [diff] [blame] | 1608 | } |
| 1609 | |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 1610 | /* |
| 1611 | * This is called when the original mapper is failing to COW a MAP_PRIVATE |
| 1612 | * mappping it owns the reserve page for. The intention is to unmap the page |
| 1613 | * from other VMAs and let the children be SIGKILLed if they are faulting the |
| 1614 | * same region. |
| 1615 | */ |
| 1616 | int unmap_ref_private(struct mm_struct *mm, |
| 1617 | struct vm_area_struct *vma, |
| 1618 | struct page *page, |
| 1619 | unsigned long address) |
| 1620 | { |
| 1621 | struct vm_area_struct *iter_vma; |
| 1622 | struct address_space *mapping; |
| 1623 | struct prio_tree_iter iter; |
| 1624 | pgoff_t pgoff; |
| 1625 | |
| 1626 | /* |
| 1627 | * vm_pgoff is in PAGE_SIZE units, hence the different calculation |
| 1628 | * from page cache lookup which is in HPAGE_SIZE units. |
| 1629 | */ |
| 1630 | address = address & huge_page_mask(hstate_vma(vma)); |
| 1631 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) |
| 1632 | + (vma->vm_pgoff >> PAGE_SHIFT); |
| 1633 | mapping = (struct address_space *)page_private(page); |
| 1634 | |
| 1635 | vma_prio_tree_foreach(iter_vma, &iter, &mapping->i_mmap, pgoff, pgoff) { |
| 1636 | /* Do not unmap the current VMA */ |
| 1637 | if (iter_vma == vma) |
| 1638 | continue; |
| 1639 | |
| 1640 | /* |
| 1641 | * Unmap the page from other VMAs without their own reserves. |
| 1642 | * They get marked to be SIGKILLed if they fault in these |
| 1643 | * areas. This is because a future no-page fault on this VMA |
| 1644 | * could insert a zeroed page instead of the data existing |
| 1645 | * from the time of fork. This would look like data corruption |
| 1646 | */ |
| 1647 | if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER)) |
| 1648 | unmap_hugepage_range(iter_vma, |
| 1649 | address, address + HPAGE_SIZE, |
| 1650 | page); |
| 1651 | } |
| 1652 | |
| 1653 | return 1; |
| 1654 | } |
| 1655 | |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1656 | static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma, |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 1657 | unsigned long address, pte_t *ptep, pte_t pte, |
| 1658 | struct page *pagecache_page) |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1659 | { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1660 | struct hstate *h = hstate_vma(vma); |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1661 | struct page *old_page, *new_page; |
David Gibson | 79ac6ba | 2006-03-22 00:08:51 -0800 | [diff] [blame] | 1662 | int avoidcopy; |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 1663 | int outside_reserve = 0; |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1664 | |
| 1665 | old_page = pte_page(pte); |
| 1666 | |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 1667 | retry_avoidcopy: |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1668 | /* If no-one else is actually using this page, avoid the copy |
| 1669 | * and just make the page writable */ |
| 1670 | avoidcopy = (page_count(old_page) == 1); |
| 1671 | if (avoidcopy) { |
| 1672 | set_huge_ptep_writable(vma, address, ptep); |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 1673 | return 0; |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1674 | } |
| 1675 | |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 1676 | /* |
| 1677 | * If the process that created a MAP_PRIVATE mapping is about to |
| 1678 | * perform a COW due to a shared page count, attempt to satisfy |
| 1679 | * the allocation without using the existing reserves. The pagecache |
| 1680 | * page is used to determine if the reserve at this address was |
| 1681 | * consumed or not. If reserves were used, a partial faulted mapping |
| 1682 | * at the time of fork() could consume its reserves on COW instead |
| 1683 | * of the full address range. |
| 1684 | */ |
| 1685 | if (!(vma->vm_flags & VM_SHARED) && |
| 1686 | is_vma_resv_set(vma, HPAGE_RESV_OWNER) && |
| 1687 | old_page != pagecache_page) |
| 1688 | outside_reserve = 1; |
| 1689 | |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1690 | page_cache_get(old_page); |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 1691 | new_page = alloc_huge_page(vma, address, outside_reserve); |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1692 | |
Adam Litke | 2fc39ce | 2007-11-14 16:59:39 -0800 | [diff] [blame] | 1693 | if (IS_ERR(new_page)) { |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1694 | page_cache_release(old_page); |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 1695 | |
| 1696 | /* |
| 1697 | * If a process owning a MAP_PRIVATE mapping fails to COW, |
| 1698 | * it is due to references held by a child and an insufficient |
| 1699 | * huge page pool. To guarantee the original mappers |
| 1700 | * reliability, unmap the page from child processes. The child |
| 1701 | * may get SIGKILLed if it later faults. |
| 1702 | */ |
| 1703 | if (outside_reserve) { |
| 1704 | BUG_ON(huge_pte_none(pte)); |
| 1705 | if (unmap_ref_private(mm, vma, old_page, address)) { |
| 1706 | BUG_ON(page_count(old_page) != 1); |
| 1707 | BUG_ON(huge_pte_none(pte)); |
| 1708 | goto retry_avoidcopy; |
| 1709 | } |
| 1710 | WARN_ON_ONCE(1); |
| 1711 | } |
| 1712 | |
Adam Litke | 2fc39ce | 2007-11-14 16:59:39 -0800 | [diff] [blame] | 1713 | return -PTR_ERR(new_page); |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | spin_unlock(&mm->page_table_lock); |
Atsushi Nemoto | 9de455b | 2006-12-12 17:14:55 +0000 | [diff] [blame] | 1717 | copy_huge_page(new_page, old_page, address, vma); |
Nick Piggin | 0ed361d | 2008-02-04 22:29:34 -0800 | [diff] [blame] | 1718 | __SetPageUptodate(new_page); |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1719 | spin_lock(&mm->page_table_lock); |
| 1720 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1721 | ptep = huge_pte_offset(mm, address & huge_page_mask(h)); |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 1722 | if (likely(pte_same(huge_ptep_get(ptep), pte))) { |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1723 | /* Break COW */ |
Gerald Schaefer | 8fe627e | 2008-04-28 02:13:28 -0700 | [diff] [blame] | 1724 | huge_ptep_clear_flush(vma, address, ptep); |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1725 | set_huge_pte_at(mm, address, ptep, |
| 1726 | make_huge_pte(vma, new_page, 1)); |
| 1727 | /* Make the old page be freed below */ |
| 1728 | new_page = old_page; |
| 1729 | } |
| 1730 | page_cache_release(new_page); |
| 1731 | page_cache_release(old_page); |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 1732 | return 0; |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1733 | } |
| 1734 | |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 1735 | /* Return the pagecache page at a given address within a VMA */ |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1736 | static struct page *hugetlbfs_pagecache_page(struct hstate *h, |
| 1737 | struct vm_area_struct *vma, unsigned long address) |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 1738 | { |
| 1739 | struct address_space *mapping; |
Andy Whitcroft | e7c4b0b | 2008-07-23 21:27:26 -0700 | [diff] [blame] | 1740 | pgoff_t idx; |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 1741 | |
| 1742 | mapping = vma->vm_file->f_mapping; |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1743 | idx = vma_hugecache_offset(h, vma, address); |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 1744 | |
| 1745 | return find_lock_page(mapping, idx); |
| 1746 | } |
| 1747 | |
Robert P. J. Day | a1ed3dd | 2007-07-17 04:03:33 -0700 | [diff] [blame] | 1748 | static 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] | 1749 | unsigned long address, pte_t *ptep, int write_access) |
Hugh Dickins | ac9b9c6 | 2005-10-20 16:24:28 +0100 | [diff] [blame] | 1750 | { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1751 | struct hstate *h = hstate_vma(vma); |
Hugh Dickins | ac9b9c6 | 2005-10-20 16:24:28 +0100 | [diff] [blame] | 1752 | int ret = VM_FAULT_SIGBUS; |
Andy Whitcroft | e7c4b0b | 2008-07-23 21:27:26 -0700 | [diff] [blame] | 1753 | pgoff_t idx; |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1754 | unsigned long size; |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1755 | struct page *page; |
| 1756 | struct address_space *mapping; |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1757 | pte_t new_pte; |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1758 | |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 1759 | /* |
| 1760 | * Currently, we are forced to kill the process in the event the |
| 1761 | * original mapper has unmapped pages from the child due to a failed |
| 1762 | * COW. Warn that such a situation has occured as it may not be obvious |
| 1763 | */ |
| 1764 | if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) { |
| 1765 | printk(KERN_WARNING |
| 1766 | "PID %d killed due to inadequate hugepage pool\n", |
| 1767 | current->pid); |
| 1768 | return ret; |
| 1769 | } |
| 1770 | |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1771 | mapping = vma->vm_file->f_mapping; |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1772 | idx = vma_hugecache_offset(h, vma, address); |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1773 | |
| 1774 | /* |
| 1775 | * Use page lock to guard against racing truncation |
| 1776 | * before we get page_table_lock. |
| 1777 | */ |
Christoph Lameter | 6bda666 | 2006-01-06 00:10:49 -0800 | [diff] [blame] | 1778 | retry: |
| 1779 | page = find_lock_page(mapping, idx); |
| 1780 | if (!page) { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1781 | size = i_size_read(mapping->host) >> huge_page_shift(h); |
Hugh Dickins | ebed4bf | 2006-10-28 10:38:43 -0700 | [diff] [blame] | 1782 | if (idx >= size) |
| 1783 | goto out; |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 1784 | page = alloc_huge_page(vma, address, 0); |
Adam Litke | 2fc39ce | 2007-11-14 16:59:39 -0800 | [diff] [blame] | 1785 | if (IS_ERR(page)) { |
| 1786 | ret = -PTR_ERR(page); |
Christoph Lameter | 6bda666 | 2006-01-06 00:10:49 -0800 | [diff] [blame] | 1787 | goto out; |
| 1788 | } |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1789 | clear_huge_page(page, address, huge_page_size(h)); |
Nick Piggin | 0ed361d | 2008-02-04 22:29:34 -0800 | [diff] [blame] | 1790 | __SetPageUptodate(page); |
Hugh Dickins | ac9b9c6 | 2005-10-20 16:24:28 +0100 | [diff] [blame] | 1791 | |
Christoph Lameter | 6bda666 | 2006-01-06 00:10:49 -0800 | [diff] [blame] | 1792 | if (vma->vm_flags & VM_SHARED) { |
| 1793 | int err; |
Ken Chen | 45c682a | 2007-11-14 16:59:44 -0800 | [diff] [blame] | 1794 | struct inode *inode = mapping->host; |
Christoph Lameter | 6bda666 | 2006-01-06 00:10:49 -0800 | [diff] [blame] | 1795 | |
| 1796 | err = add_to_page_cache(page, mapping, idx, GFP_KERNEL); |
| 1797 | if (err) { |
| 1798 | put_page(page); |
Christoph Lameter | 6bda666 | 2006-01-06 00:10:49 -0800 | [diff] [blame] | 1799 | if (err == -EEXIST) |
| 1800 | goto retry; |
| 1801 | goto out; |
| 1802 | } |
Ken Chen | 45c682a | 2007-11-14 16:59:44 -0800 | [diff] [blame] | 1803 | |
| 1804 | spin_lock(&inode->i_lock); |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1805 | inode->i_blocks += blocks_per_huge_page(h); |
Ken Chen | 45c682a | 2007-11-14 16:59:44 -0800 | [diff] [blame] | 1806 | spin_unlock(&inode->i_lock); |
Christoph Lameter | 6bda666 | 2006-01-06 00:10:49 -0800 | [diff] [blame] | 1807 | } else |
| 1808 | lock_page(page); |
| 1809 | } |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1810 | |
Hugh Dickins | ac9b9c6 | 2005-10-20 16:24:28 +0100 | [diff] [blame] | 1811 | spin_lock(&mm->page_table_lock); |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1812 | size = i_size_read(mapping->host) >> huge_page_shift(h); |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1813 | if (idx >= size) |
| 1814 | goto backout; |
| 1815 | |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 1816 | ret = 0; |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 1817 | if (!huge_pte_none(huge_ptep_get(ptep))) |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1818 | goto backout; |
| 1819 | |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1820 | new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE) |
| 1821 | && (vma->vm_flags & VM_SHARED))); |
| 1822 | set_huge_pte_at(mm, address, ptep, new_pte); |
| 1823 | |
| 1824 | if (write_access && !(vma->vm_flags & VM_SHARED)) { |
| 1825 | /* Optimization, do the COW without a second fault */ |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 1826 | ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page); |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1827 | } |
| 1828 | |
Hugh Dickins | ac9b9c6 | 2005-10-20 16:24:28 +0100 | [diff] [blame] | 1829 | spin_unlock(&mm->page_table_lock); |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1830 | unlock_page(page); |
| 1831 | out: |
Hugh Dickins | ac9b9c6 | 2005-10-20 16:24:28 +0100 | [diff] [blame] | 1832 | return ret; |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1833 | |
| 1834 | backout: |
| 1835 | spin_unlock(&mm->page_table_lock); |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1836 | unlock_page(page); |
| 1837 | put_page(page); |
| 1838 | goto out; |
Hugh Dickins | ac9b9c6 | 2005-10-20 16:24:28 +0100 | [diff] [blame] | 1839 | } |
| 1840 | |
Adam Litke | 86e5216 | 2006-01-06 00:10:43 -0800 | [diff] [blame] | 1841 | int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, |
| 1842 | unsigned long address, int write_access) |
| 1843 | { |
| 1844 | pte_t *ptep; |
| 1845 | pte_t entry; |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1846 | int ret; |
David Gibson | 3935baa | 2006-03-22 00:08:53 -0800 | [diff] [blame] | 1847 | static DEFINE_MUTEX(hugetlb_instantiation_mutex); |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1848 | struct hstate *h = hstate_vma(vma); |
Adam Litke | 86e5216 | 2006-01-06 00:10:43 -0800 | [diff] [blame] | 1849 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1850 | ptep = huge_pte_alloc(mm, address, huge_page_size(h)); |
Adam Litke | 86e5216 | 2006-01-06 00:10:43 -0800 | [diff] [blame] | 1851 | if (!ptep) |
| 1852 | return VM_FAULT_OOM; |
| 1853 | |
David Gibson | 3935baa | 2006-03-22 00:08:53 -0800 | [diff] [blame] | 1854 | /* |
| 1855 | * Serialize hugepage allocation and instantiation, so that we don't |
| 1856 | * get spurious allocation failures if two CPUs race to instantiate |
| 1857 | * the same page in the page cache. |
| 1858 | */ |
| 1859 | mutex_lock(&hugetlb_instantiation_mutex); |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 1860 | entry = huge_ptep_get(ptep); |
| 1861 | if (huge_pte_none(entry)) { |
David Gibson | 3935baa | 2006-03-22 00:08:53 -0800 | [diff] [blame] | 1862 | ret = hugetlb_no_page(mm, vma, address, ptep, write_access); |
| 1863 | mutex_unlock(&hugetlb_instantiation_mutex); |
| 1864 | return ret; |
| 1865 | } |
Adam Litke | 86e5216 | 2006-01-06 00:10:43 -0800 | [diff] [blame] | 1866 | |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 1867 | ret = 0; |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1868 | |
| 1869 | spin_lock(&mm->page_table_lock); |
| 1870 | /* Check for a racing update before calling hugetlb_cow */ |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 1871 | if (likely(pte_same(entry, huge_ptep_get(ptep)))) |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 1872 | if (write_access && !pte_write(entry)) { |
| 1873 | struct page *page; |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1874 | page = hugetlbfs_pagecache_page(h, vma, address); |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 1875 | ret = hugetlb_cow(mm, vma, address, ptep, entry, page); |
| 1876 | if (page) { |
| 1877 | unlock_page(page); |
| 1878 | put_page(page); |
| 1879 | } |
| 1880 | } |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1881 | spin_unlock(&mm->page_table_lock); |
David Gibson | 3935baa | 2006-03-22 00:08:53 -0800 | [diff] [blame] | 1882 | mutex_unlock(&hugetlb_instantiation_mutex); |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1883 | |
| 1884 | return ret; |
Adam Litke | 86e5216 | 2006-01-06 00:10:43 -0800 | [diff] [blame] | 1885 | } |
| 1886 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1887 | int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, |
| 1888 | struct page **pages, struct vm_area_struct **vmas, |
Adam Litke | 5b23dbe | 2007-11-14 16:59:33 -0800 | [diff] [blame] | 1889 | unsigned long *position, int *length, int i, |
| 1890 | int write) |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1891 | { |
Chen, Kenneth W | d5d4b0a | 2006-03-22 00:09:03 -0800 | [diff] [blame] | 1892 | unsigned long pfn_offset; |
| 1893 | unsigned long vaddr = *position; |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1894 | int remainder = *length; |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1895 | struct hstate *h = hstate_vma(vma); |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1896 | |
Hugh Dickins | 1c59827 | 2005-10-19 21:23:43 -0700 | [diff] [blame] | 1897 | spin_lock(&mm->page_table_lock); |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1898 | while (vaddr < vma->vm_end && remainder) { |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1899 | pte_t *pte; |
| 1900 | struct page *page; |
| 1901 | |
| 1902 | /* |
| 1903 | * Some archs (sparc64, sh*) have multiple pte_ts to |
| 1904 | * each hugepage. We have to make * sure we get the |
| 1905 | * first, for the page indexing below to work. |
| 1906 | */ |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1907 | pte = huge_pte_offset(mm, vaddr & huge_page_mask(h)); |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1908 | |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 1909 | if (!pte || huge_pte_none(huge_ptep_get(pte)) || |
| 1910 | (write && !pte_write(huge_ptep_get(pte)))) { |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1911 | int ret; |
| 1912 | |
| 1913 | spin_unlock(&mm->page_table_lock); |
Adam Litke | 5b23dbe | 2007-11-14 16:59:33 -0800 | [diff] [blame] | 1914 | ret = hugetlb_fault(mm, vma, vaddr, write); |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1915 | spin_lock(&mm->page_table_lock); |
Adam Litke | a89182c | 2007-08-22 14:01:51 -0700 | [diff] [blame] | 1916 | if (!(ret & VM_FAULT_ERROR)) |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1917 | continue; |
| 1918 | |
| 1919 | remainder = 0; |
| 1920 | if (!i) |
| 1921 | i = -EFAULT; |
| 1922 | break; |
| 1923 | } |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1924 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1925 | pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT; |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 1926 | page = pte_page(huge_ptep_get(pte)); |
Chen, Kenneth W | d5d4b0a | 2006-03-22 00:09:03 -0800 | [diff] [blame] | 1927 | same_page: |
Chen, Kenneth W | d669218 | 2006-03-31 02:29:57 -0800 | [diff] [blame] | 1928 | if (pages) { |
| 1929 | get_page(page); |
Chen, Kenneth W | d5d4b0a | 2006-03-22 00:09:03 -0800 | [diff] [blame] | 1930 | pages[i] = page + pfn_offset; |
Chen, Kenneth W | d669218 | 2006-03-31 02:29:57 -0800 | [diff] [blame] | 1931 | } |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1932 | |
| 1933 | if (vmas) |
| 1934 | vmas[i] = vma; |
| 1935 | |
| 1936 | vaddr += PAGE_SIZE; |
Chen, Kenneth W | d5d4b0a | 2006-03-22 00:09:03 -0800 | [diff] [blame] | 1937 | ++pfn_offset; |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1938 | --remainder; |
| 1939 | ++i; |
Chen, Kenneth W | d5d4b0a | 2006-03-22 00:09:03 -0800 | [diff] [blame] | 1940 | if (vaddr < vma->vm_end && remainder && |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1941 | pfn_offset < pages_per_huge_page(h)) { |
Chen, Kenneth W | d5d4b0a | 2006-03-22 00:09:03 -0800 | [diff] [blame] | 1942 | /* |
| 1943 | * We use pfn_offset to avoid touching the pageframes |
| 1944 | * of this compound page. |
| 1945 | */ |
| 1946 | goto same_page; |
| 1947 | } |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1948 | } |
Hugh Dickins | 1c59827 | 2005-10-19 21:23:43 -0700 | [diff] [blame] | 1949 | spin_unlock(&mm->page_table_lock); |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1950 | *length = remainder; |
| 1951 | *position = vaddr; |
| 1952 | |
| 1953 | return i; |
| 1954 | } |
Zhang, Yanmin | 8f86059 | 2006-03-22 00:08:50 -0800 | [diff] [blame] | 1955 | |
| 1956 | void hugetlb_change_protection(struct vm_area_struct *vma, |
| 1957 | unsigned long address, unsigned long end, pgprot_t newprot) |
| 1958 | { |
| 1959 | struct mm_struct *mm = vma->vm_mm; |
| 1960 | unsigned long start = address; |
| 1961 | pte_t *ptep; |
| 1962 | pte_t pte; |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1963 | struct hstate *h = hstate_vma(vma); |
Zhang, Yanmin | 8f86059 | 2006-03-22 00:08:50 -0800 | [diff] [blame] | 1964 | |
| 1965 | BUG_ON(address >= end); |
| 1966 | flush_cache_range(vma, address, end); |
| 1967 | |
Chen, Kenneth W | 39dde65 | 2006-12-06 20:32:03 -0800 | [diff] [blame] | 1968 | spin_lock(&vma->vm_file->f_mapping->i_mmap_lock); |
Zhang, Yanmin | 8f86059 | 2006-03-22 00:08:50 -0800 | [diff] [blame] | 1969 | spin_lock(&mm->page_table_lock); |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1970 | for (; address < end; address += huge_page_size(h)) { |
Zhang, Yanmin | 8f86059 | 2006-03-22 00:08:50 -0800 | [diff] [blame] | 1971 | ptep = huge_pte_offset(mm, address); |
| 1972 | if (!ptep) |
| 1973 | continue; |
Chen, Kenneth W | 39dde65 | 2006-12-06 20:32:03 -0800 | [diff] [blame] | 1974 | if (huge_pmd_unshare(mm, &address, ptep)) |
| 1975 | continue; |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 1976 | if (!huge_pte_none(huge_ptep_get(ptep))) { |
Zhang, Yanmin | 8f86059 | 2006-03-22 00:08:50 -0800 | [diff] [blame] | 1977 | pte = huge_ptep_get_and_clear(mm, address, ptep); |
| 1978 | pte = pte_mkhuge(pte_modify(pte, newprot)); |
| 1979 | set_huge_pte_at(mm, address, ptep, pte); |
Zhang, Yanmin | 8f86059 | 2006-03-22 00:08:50 -0800 | [diff] [blame] | 1980 | } |
| 1981 | } |
| 1982 | spin_unlock(&mm->page_table_lock); |
Chen, Kenneth W | 39dde65 | 2006-12-06 20:32:03 -0800 | [diff] [blame] | 1983 | spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock); |
Zhang, Yanmin | 8f86059 | 2006-03-22 00:08:50 -0800 | [diff] [blame] | 1984 | |
| 1985 | flush_tlb_range(vma, start, end); |
| 1986 | } |
| 1987 | |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 1988 | int hugetlb_reserve_pages(struct inode *inode, |
| 1989 | long from, long to, |
| 1990 | struct vm_area_struct *vma) |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 1991 | { |
| 1992 | long ret, chg; |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 1993 | struct hstate *h = hstate_inode(inode); |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 1994 | |
Andy Whitcroft | c37f9fb | 2008-07-23 21:27:30 -0700 | [diff] [blame] | 1995 | if (vma && vma->vm_flags & VM_NORESERVE) |
| 1996 | return 0; |
| 1997 | |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 1998 | /* |
| 1999 | * Shared mappings base their reservation on the number of pages that |
| 2000 | * are already allocated on behalf of the file. Private mappings need |
| 2001 | * to reserve the full area even if read-only as mprotect() may be |
| 2002 | * called to make the mapping read-write. Assume !vma is a shm mapping |
| 2003 | */ |
| 2004 | if (!vma || vma->vm_flags & VM_SHARED) |
| 2005 | chg = region_chg(&inode->i_mapping->private_list, from, to); |
| 2006 | else { |
Andy Whitcroft | 84afd99 | 2008-07-23 21:27:32 -0700 | [diff] [blame] | 2007 | struct resv_map *resv_map = resv_map_alloc(); |
| 2008 | if (!resv_map) |
| 2009 | return -ENOMEM; |
| 2010 | |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 2011 | chg = to - from; |
Andy Whitcroft | 84afd99 | 2008-07-23 21:27:32 -0700 | [diff] [blame] | 2012 | |
| 2013 | set_vma_resv_map(vma, resv_map); |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame] | 2014 | set_vma_resv_flags(vma, HPAGE_RESV_OWNER); |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 2015 | } |
| 2016 | |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 2017 | if (chg < 0) |
| 2018 | return chg; |
Ken Chen | 8a63011 | 2007-05-09 02:33:34 -0700 | [diff] [blame] | 2019 | |
Adam Litke | 90d8b7e | 2007-11-14 16:59:42 -0800 | [diff] [blame] | 2020 | if (hugetlb_get_quota(inode->i_mapping, chg)) |
| 2021 | return -ENOSPC; |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 2022 | ret = hugetlb_acct_memory(h, chg); |
Ken Chen | 68842c9 | 2008-01-14 00:55:19 -0800 | [diff] [blame] | 2023 | if (ret < 0) { |
| 2024 | hugetlb_put_quota(inode->i_mapping, chg); |
Chen, Kenneth W | a43a8c3 | 2006-06-23 02:03:15 -0700 | [diff] [blame] | 2025 | return ret; |
Ken Chen | 68842c9 | 2008-01-14 00:55:19 -0800 | [diff] [blame] | 2026 | } |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 2027 | if (!vma || vma->vm_flags & VM_SHARED) |
| 2028 | region_add(&inode->i_mapping->private_list, from, to); |
Chen, Kenneth W | a43a8c3 | 2006-06-23 02:03:15 -0700 | [diff] [blame] | 2029 | return 0; |
| 2030 | } |
| 2031 | |
| 2032 | void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed) |
| 2033 | { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 2034 | struct hstate *h = hstate_inode(inode); |
Chen, Kenneth W | a43a8c3 | 2006-06-23 02:03:15 -0700 | [diff] [blame] | 2035 | long chg = region_truncate(&inode->i_mapping->private_list, offset); |
Ken Chen | 45c682a | 2007-11-14 16:59:44 -0800 | [diff] [blame] | 2036 | |
| 2037 | spin_lock(&inode->i_lock); |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 2038 | inode->i_blocks -= blocks_per_huge_page(h); |
Ken Chen | 45c682a | 2007-11-14 16:59:44 -0800 | [diff] [blame] | 2039 | spin_unlock(&inode->i_lock); |
| 2040 | |
Adam Litke | 90d8b7e | 2007-11-14 16:59:42 -0800 | [diff] [blame] | 2041 | hugetlb_put_quota(inode->i_mapping, (chg - freed)); |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 2042 | hugetlb_acct_memory(h, -(chg - freed)); |
Chen, Kenneth W | a43a8c3 | 2006-06-23 02:03:15 -0700 | [diff] [blame] | 2043 | } |