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> |
Christoph Lameter | 5da7ca8 | 2006-01-06 00:10:46 -0800 | [diff] [blame] | 17 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 18 | #include <asm/page.h> |
| 19 | #include <asm/pgtable.h> |
| 20 | |
| 21 | #include <linux/hugetlb.h> |
Nick Piggin | 7835e98 | 2006-03-22 00:08:40 -0800 | [diff] [blame] | 22 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL; |
Chen, Kenneth W | a43a8c3 | 2006-06-23 02:03:15 -0700 | [diff] [blame] | 25 | static unsigned long nr_huge_pages, free_huge_pages, resv_huge_pages; |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 26 | static unsigned long surplus_huge_pages; |
Nishanth Aravamudan | 064d9ef | 2008-02-13 15:03:19 -0800 | [diff] [blame] | 27 | static unsigned long nr_overcommit_huge_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | unsigned long max_huge_pages; |
Nishanth Aravamudan | 064d9ef | 2008-02-13 15:03:19 -0800 | [diff] [blame] | 29 | unsigned long sysctl_overcommit_huge_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | static struct list_head hugepage_freelists[MAX_NUMNODES]; |
| 31 | static unsigned int nr_huge_pages_node[MAX_NUMNODES]; |
| 32 | static unsigned int free_huge_pages_node[MAX_NUMNODES]; |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 33 | static unsigned int surplus_huge_pages_node[MAX_NUMNODES]; |
Mel Gorman | 396faf0 | 2007-07-17 04:03:13 -0700 | [diff] [blame] | 34 | static gfp_t htlb_alloc_mask = GFP_HIGHUSER; |
| 35 | unsigned long hugepages_treat_as_movable; |
Nishanth Aravamudan | 63b4613 | 2007-10-16 01:26:24 -0700 | [diff] [blame] | 36 | static int hugetlb_next_nid; |
Mel Gorman | 396faf0 | 2007-07-17 04:03:13 -0700 | [diff] [blame] | 37 | |
David Gibson | 3935baa | 2006-03-22 00:08:53 -0800 | [diff] [blame] | 38 | /* |
| 39 | * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages |
| 40 | */ |
| 41 | static DEFINE_SPINLOCK(hugetlb_lock); |
Eric Paris | 0bd0f9f | 2005-11-21 21:32:28 -0800 | [diff] [blame] | 42 | |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 43 | #define HPAGE_RESV_OWNER (1UL << (BITS_PER_LONG - 1)) |
| 44 | #define HPAGE_RESV_UNMAPPED (1UL << (BITS_PER_LONG - 2)) |
| 45 | #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED) |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 46 | /* |
| 47 | * These helpers are used to track how many pages are reserved for |
| 48 | * faults in a MAP_PRIVATE mapping. Only the process that called mmap() |
| 49 | * is guaranteed to have their future faults succeed. |
| 50 | * |
| 51 | * With the exception of reset_vma_resv_huge_pages() which is called at fork(), |
| 52 | * the reserve counters are updated with the hugetlb_lock held. It is safe |
| 53 | * to reset the VMA at fork() time as it is not in use yet and there is no |
| 54 | * chance of the global counters getting corrupted as a result of the values. |
| 55 | */ |
| 56 | static unsigned long vma_resv_huge_pages(struct vm_area_struct *vma) |
| 57 | { |
| 58 | VM_BUG_ON(!is_vm_hugetlb_page(vma)); |
| 59 | if (!(vma->vm_flags & VM_SHARED)) |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 60 | return (unsigned long)vma->vm_private_data & ~HPAGE_RESV_MASK; |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | static void set_vma_resv_huge_pages(struct vm_area_struct *vma, |
| 65 | unsigned long reserve) |
| 66 | { |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 67 | unsigned long flags; |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 68 | VM_BUG_ON(!is_vm_hugetlb_page(vma)); |
| 69 | VM_BUG_ON(vma->vm_flags & VM_SHARED); |
| 70 | |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 71 | flags = (unsigned long)vma->vm_private_data & HPAGE_RESV_MASK; |
| 72 | vma->vm_private_data = (void *)(reserve | flags); |
| 73 | } |
| 74 | |
| 75 | static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags) |
| 76 | { |
| 77 | unsigned long reserveflags = (unsigned long)vma->vm_private_data; |
| 78 | VM_BUG_ON(!is_vm_hugetlb_page(vma)); |
| 79 | vma->vm_private_data = (void *)(reserveflags | flags); |
| 80 | } |
| 81 | |
| 82 | static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag) |
| 83 | { |
| 84 | VM_BUG_ON(!is_vm_hugetlb_page(vma)); |
| 85 | return ((unsigned long)vma->vm_private_data & flag) != 0; |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /* Decrement the reserved pages in the hugepage pool by one */ |
| 89 | static void decrement_hugepage_resv_vma(struct vm_area_struct *vma) |
| 90 | { |
| 91 | if (vma->vm_flags & VM_SHARED) { |
| 92 | /* Shared mappings always use reserves */ |
| 93 | resv_huge_pages--; |
| 94 | } else { |
| 95 | /* |
| 96 | * Only the process that called mmap() has reserves for |
| 97 | * private mappings. |
| 98 | */ |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 99 | if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) { |
| 100 | unsigned long flags, reserve; |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 101 | resv_huge_pages--; |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 102 | flags = (unsigned long)vma->vm_private_data & |
| 103 | HPAGE_RESV_MASK; |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 104 | reserve = (unsigned long)vma->vm_private_data - 1; |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 105 | vma->vm_private_data = (void *)(reserve | flags); |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 110 | /* Reset counters to 0 and clear all HPAGE_RESV_* flags */ |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 111 | void reset_vma_resv_huge_pages(struct vm_area_struct *vma) |
| 112 | { |
| 113 | VM_BUG_ON(!is_vm_hugetlb_page(vma)); |
| 114 | if (!(vma->vm_flags & VM_SHARED)) |
| 115 | vma->vm_private_data = (void *)0; |
| 116 | } |
| 117 | |
| 118 | /* Returns true if the VMA has associated reserve pages */ |
| 119 | static int vma_has_private_reserves(struct vm_area_struct *vma) |
| 120 | { |
| 121 | if (vma->vm_flags & VM_SHARED) |
| 122 | return 0; |
| 123 | if (!vma_resv_huge_pages(vma)) |
| 124 | return 0; |
| 125 | return 1; |
| 126 | } |
| 127 | |
David Gibson | 79ac6ba | 2006-03-22 00:08:51 -0800 | [diff] [blame] | 128 | static void clear_huge_page(struct page *page, unsigned long addr) |
| 129 | { |
| 130 | int i; |
| 131 | |
| 132 | might_sleep(); |
| 133 | for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); i++) { |
| 134 | cond_resched(); |
Ralf Baechle | 281e0e3 | 2007-10-01 01:20:10 -0700 | [diff] [blame] | 135 | clear_user_highpage(page + i, addr + i * PAGE_SIZE); |
David Gibson | 79ac6ba | 2006-03-22 00:08:51 -0800 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | static void copy_huge_page(struct page *dst, struct page *src, |
Atsushi Nemoto | 9de455b | 2006-12-12 17:14:55 +0000 | [diff] [blame] | 140 | unsigned long addr, struct vm_area_struct *vma) |
David Gibson | 79ac6ba | 2006-03-22 00:08:51 -0800 | [diff] [blame] | 141 | { |
| 142 | int i; |
| 143 | |
| 144 | might_sleep(); |
| 145 | for (i = 0; i < HPAGE_SIZE/PAGE_SIZE; i++) { |
| 146 | cond_resched(); |
Atsushi Nemoto | 9de455b | 2006-12-12 17:14:55 +0000 | [diff] [blame] | 147 | copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma); |
David Gibson | 79ac6ba | 2006-03-22 00:08:51 -0800 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | static void enqueue_huge_page(struct page *page) |
| 152 | { |
| 153 | int nid = page_to_nid(page); |
| 154 | list_add(&page->lru, &hugepage_freelists[nid]); |
| 155 | free_huge_pages++; |
| 156 | free_huge_pages_node[nid]++; |
| 157 | } |
| 158 | |
Nishanth Aravamudan | 348e1e0 | 2008-03-04 14:29:42 -0800 | [diff] [blame] | 159 | static struct page *dequeue_huge_page(void) |
| 160 | { |
| 161 | int nid; |
| 162 | struct page *page = NULL; |
| 163 | |
| 164 | for (nid = 0; nid < MAX_NUMNODES; ++nid) { |
| 165 | if (!list_empty(&hugepage_freelists[nid])) { |
| 166 | page = list_entry(hugepage_freelists[nid].next, |
| 167 | struct page, lru); |
| 168 | list_del(&page->lru); |
| 169 | free_huge_pages--; |
| 170 | free_huge_pages_node[nid]--; |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | return page; |
| 175 | } |
| 176 | |
| 177 | static struct page *dequeue_huge_page_vma(struct vm_area_struct *vma, |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 178 | unsigned long address, int avoid_reserve) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | { |
Nishanth Aravamudan | 31a5c6e | 2007-07-15 23:38:02 -0700 | [diff] [blame] | 180 | int nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | struct page *page = NULL; |
Lee Schermerhorn | 480eccf | 2007-09-18 22:46:47 -0700 | [diff] [blame] | 182 | struct mempolicy *mpol; |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 183 | nodemask_t *nodemask; |
Mel Gorman | 396faf0 | 2007-07-17 04:03:13 -0700 | [diff] [blame] | 184 | struct zonelist *zonelist = huge_zonelist(vma, address, |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 185 | htlb_alloc_mask, &mpol, &nodemask); |
Mel Gorman | dd1a239 | 2008-04-28 02:12:17 -0700 | [diff] [blame] | 186 | struct zone *zone; |
| 187 | struct zoneref *z; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 189 | /* |
| 190 | * A child process with MAP_PRIVATE mappings created by their parent |
| 191 | * have no page reserves. This check ensures that reservations are |
| 192 | * not "stolen". The child may still get SIGKILLed |
| 193 | */ |
| 194 | if (!vma_has_private_reserves(vma) && |
| 195 | free_huge_pages - resv_huge_pages == 0) |
| 196 | return NULL; |
| 197 | |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 198 | /* If reserves cannot be used, ensure enough pages are in the pool */ |
| 199 | if (avoid_reserve && free_huge_pages - resv_huge_pages == 0) |
| 200 | return NULL; |
| 201 | |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 202 | for_each_zone_zonelist_nodemask(zone, z, zonelist, |
| 203 | MAX_NR_ZONES - 1, nodemask) { |
Mel Gorman | 54a6eb5 | 2008-04-28 02:12:16 -0700 | [diff] [blame] | 204 | nid = zone_to_nid(zone); |
| 205 | if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask) && |
Andrew Morton | 3abf7af | 2007-07-19 01:49:08 -0700 | [diff] [blame] | 206 | !list_empty(&hugepage_freelists[nid])) { |
| 207 | page = list_entry(hugepage_freelists[nid].next, |
| 208 | struct page, lru); |
| 209 | list_del(&page->lru); |
| 210 | free_huge_pages--; |
| 211 | free_huge_pages_node[nid]--; |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 212 | |
| 213 | if (!avoid_reserve) |
| 214 | decrement_hugepage_resv_vma(vma); |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 215 | |
Ken Chen | 5ab3ee7 | 2007-07-23 18:44:00 -0700 | [diff] [blame] | 216 | break; |
Andrew Morton | 3abf7af | 2007-07-19 01:49:08 -0700 | [diff] [blame] | 217 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } |
Lee Schermerhorn | 52cd3b0 | 2008-04-28 02:13:16 -0700 | [diff] [blame] | 219 | mpol_cond_put(mpol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | return page; |
| 221 | } |
| 222 | |
Adam Litke | 6af2acb | 2007-10-16 01:26:16 -0700 | [diff] [blame] | 223 | static void update_and_free_page(struct page *page) |
| 224 | { |
| 225 | int i; |
| 226 | nr_huge_pages--; |
| 227 | nr_huge_pages_node[page_to_nid(page)]--; |
| 228 | for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) { |
| 229 | page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced | |
| 230 | 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved | |
| 231 | 1 << PG_private | 1<< PG_writeback); |
| 232 | } |
| 233 | set_compound_page_dtor(page, NULL); |
| 234 | set_page_refcounted(page); |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 235 | arch_release_hugepage(page); |
Adam Litke | 6af2acb | 2007-10-16 01:26:16 -0700 | [diff] [blame] | 236 | __free_pages(page, HUGETLB_PAGE_ORDER); |
| 237 | } |
| 238 | |
David Gibson | 27a85ef | 2006-03-22 00:08:56 -0800 | [diff] [blame] | 239 | static void free_huge_page(struct page *page) |
| 240 | { |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 241 | int nid = page_to_nid(page); |
Adam Litke | c79fb75 | 2007-11-14 16:59:38 -0800 | [diff] [blame] | 242 | struct address_space *mapping; |
David Gibson | 27a85ef | 2006-03-22 00:08:56 -0800 | [diff] [blame] | 243 | |
Adam Litke | c79fb75 | 2007-11-14 16:59:38 -0800 | [diff] [blame] | 244 | mapping = (struct address_space *) page_private(page); |
Andy Whitcroft | e5df70a | 2008-02-23 15:23:32 -0800 | [diff] [blame] | 245 | set_page_private(page, 0); |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 246 | BUG_ON(page_count(page)); |
David Gibson | 27a85ef | 2006-03-22 00:08:56 -0800 | [diff] [blame] | 247 | INIT_LIST_HEAD(&page->lru); |
| 248 | |
| 249 | spin_lock(&hugetlb_lock); |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 250 | if (surplus_huge_pages_node[nid]) { |
| 251 | update_and_free_page(page); |
| 252 | surplus_huge_pages--; |
| 253 | surplus_huge_pages_node[nid]--; |
| 254 | } else { |
| 255 | enqueue_huge_page(page); |
| 256 | } |
David Gibson | 27a85ef | 2006-03-22 00:08:56 -0800 | [diff] [blame] | 257 | spin_unlock(&hugetlb_lock); |
Adam Litke | c79fb75 | 2007-11-14 16:59:38 -0800 | [diff] [blame] | 258 | if (mapping) |
Adam Litke | 9a119c0 | 2007-11-14 16:59:41 -0800 | [diff] [blame] | 259 | hugetlb_put_quota(mapping, 1); |
David Gibson | 27a85ef | 2006-03-22 00:08:56 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 262 | /* |
| 263 | * Increment or decrement surplus_huge_pages. Keep node-specific counters |
| 264 | * balanced by operating on them in a round-robin fashion. |
| 265 | * Returns 1 if an adjustment was made. |
| 266 | */ |
| 267 | static int adjust_pool_surplus(int delta) |
| 268 | { |
| 269 | static int prev_nid; |
| 270 | int nid = prev_nid; |
| 271 | int ret = 0; |
| 272 | |
| 273 | VM_BUG_ON(delta != -1 && delta != 1); |
| 274 | do { |
| 275 | nid = next_node(nid, node_online_map); |
| 276 | if (nid == MAX_NUMNODES) |
| 277 | nid = first_node(node_online_map); |
| 278 | |
| 279 | /* To shrink on this node, there must be a surplus page */ |
| 280 | if (delta < 0 && !surplus_huge_pages_node[nid]) |
| 281 | continue; |
| 282 | /* Surplus cannot exceed the total number of pages */ |
| 283 | if (delta > 0 && surplus_huge_pages_node[nid] >= |
| 284 | nr_huge_pages_node[nid]) |
| 285 | continue; |
| 286 | |
| 287 | surplus_huge_pages += delta; |
| 288 | surplus_huge_pages_node[nid] += delta; |
| 289 | ret = 1; |
| 290 | break; |
| 291 | } while (nid != prev_nid); |
| 292 | |
| 293 | prev_nid = nid; |
| 294 | return ret; |
| 295 | } |
| 296 | |
Nishanth Aravamudan | 63b4613 | 2007-10-16 01:26:24 -0700 | [diff] [blame] | 297 | static struct page *alloc_fresh_huge_page_node(int nid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | struct page *page; |
Joe Jin | f96efd5 | 2007-07-15 23:38:12 -0700 | [diff] [blame] | 300 | |
Nishanth Aravamudan | 63b4613 | 2007-10-16 01:26:24 -0700 | [diff] [blame] | 301 | page = alloc_pages_node(nid, |
Nishanth Aravamudan | 551883a | 2008-04-29 00:58:26 -0700 | [diff] [blame] | 302 | htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE| |
| 303 | __GFP_REPEAT|__GFP_NOWARN, |
Nishanth Aravamudan | 63b4613 | 2007-10-16 01:26:24 -0700 | [diff] [blame] | 304 | HUGETLB_PAGE_ORDER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | if (page) { |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 306 | if (arch_prepare_hugepage(page)) { |
| 307 | __free_pages(page, HUGETLB_PAGE_ORDER); |
Harvey Harrison | 7b8ee84 | 2008-04-28 14:13:19 -0700 | [diff] [blame] | 308 | return NULL; |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 309 | } |
Andy Whitcroft | 33f2ef8 | 2006-12-06 20:33:32 -0800 | [diff] [blame] | 310 | set_compound_page_dtor(page, free_huge_page); |
Eric Paris | 0bd0f9f | 2005-11-21 21:32:28 -0800 | [diff] [blame] | 311 | spin_lock(&hugetlb_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | nr_huge_pages++; |
Nishanth Aravamudan | 63b4613 | 2007-10-16 01:26:24 -0700 | [diff] [blame] | 313 | nr_huge_pages_node[nid]++; |
Eric Paris | 0bd0f9f | 2005-11-21 21:32:28 -0800 | [diff] [blame] | 314 | spin_unlock(&hugetlb_lock); |
Nick Piggin | a482289 | 2006-03-22 00:08:08 -0800 | [diff] [blame] | 315 | put_page(page); /* free it into the hugepage allocator */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | } |
Nishanth Aravamudan | 63b4613 | 2007-10-16 01:26:24 -0700 | [diff] [blame] | 317 | |
| 318 | return page; |
| 319 | } |
| 320 | |
| 321 | static int alloc_fresh_huge_page(void) |
| 322 | { |
| 323 | struct page *page; |
| 324 | int start_nid; |
| 325 | int next_nid; |
| 326 | int ret = 0; |
| 327 | |
| 328 | start_nid = hugetlb_next_nid; |
| 329 | |
| 330 | do { |
| 331 | page = alloc_fresh_huge_page_node(hugetlb_next_nid); |
| 332 | if (page) |
| 333 | ret = 1; |
| 334 | /* |
| 335 | * Use a helper variable to find the next node and then |
| 336 | * copy it back to hugetlb_next_nid afterwards: |
| 337 | * otherwise there's a window in which a racer might |
| 338 | * pass invalid nid MAX_NUMNODES to alloc_pages_node. |
| 339 | * But we don't need to use a spin_lock here: it really |
| 340 | * doesn't matter if occasionally a racer chooses the |
| 341 | * same nid as we do. Move nid forward in the mask even |
| 342 | * if we just successfully allocated a hugepage so that |
| 343 | * the next caller gets hugepages on the next node. |
| 344 | */ |
| 345 | next_nid = next_node(hugetlb_next_nid, node_online_map); |
| 346 | if (next_nid == MAX_NUMNODES) |
| 347 | next_nid = first_node(node_online_map); |
| 348 | hugetlb_next_nid = next_nid; |
| 349 | } while (!page && hugetlb_next_nid != start_nid); |
| 350 | |
Adam Litke | 3b11630 | 2008-04-28 02:13:06 -0700 | [diff] [blame] | 351 | if (ret) |
| 352 | count_vm_event(HTLB_BUDDY_PGALLOC); |
| 353 | else |
| 354 | count_vm_event(HTLB_BUDDY_PGALLOC_FAIL); |
| 355 | |
Nishanth Aravamudan | 63b4613 | 2007-10-16 01:26:24 -0700 | [diff] [blame] | 356 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 359 | static struct page *alloc_buddy_huge_page(struct vm_area_struct *vma, |
| 360 | unsigned long address) |
| 361 | { |
| 362 | struct page *page; |
Nishanth Aravamudan | d1c3fb1 | 2007-12-17 16:20:12 -0800 | [diff] [blame] | 363 | unsigned int nid; |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 364 | |
Nishanth Aravamudan | d1c3fb1 | 2007-12-17 16:20:12 -0800 | [diff] [blame] | 365 | /* |
| 366 | * Assume we will successfully allocate the surplus page to |
| 367 | * prevent racing processes from causing the surplus to exceed |
| 368 | * overcommit |
| 369 | * |
| 370 | * This however introduces a different race, where a process B |
| 371 | * tries to grow the static hugepage pool while alloc_pages() is |
| 372 | * called by process A. B will only examine the per-node |
| 373 | * counters in determining if surplus huge pages can be |
| 374 | * converted to normal huge pages in adjust_pool_surplus(). A |
| 375 | * won't be able to increment the per-node counter, until the |
| 376 | * lock is dropped by B, but B doesn't drop hugetlb_lock until |
| 377 | * no more huge pages can be converted from surplus to normal |
| 378 | * state (and doesn't try to convert again). Thus, we have a |
| 379 | * case where a surplus huge page exists, the pool is grown, and |
| 380 | * the surplus huge page still exists after, even though it |
| 381 | * should just have been converted to a normal huge page. This |
| 382 | * does not leak memory, though, as the hugepage will be freed |
| 383 | * once it is out of use. It also does not allow the counters to |
| 384 | * go out of whack in adjust_pool_surplus() as we don't modify |
| 385 | * the node values until we've gotten the hugepage and only the |
| 386 | * per-node value is checked there. |
| 387 | */ |
| 388 | spin_lock(&hugetlb_lock); |
| 389 | if (surplus_huge_pages >= nr_overcommit_huge_pages) { |
| 390 | spin_unlock(&hugetlb_lock); |
| 391 | return NULL; |
| 392 | } else { |
| 393 | nr_huge_pages++; |
| 394 | surplus_huge_pages++; |
| 395 | } |
| 396 | spin_unlock(&hugetlb_lock); |
| 397 | |
Nishanth Aravamudan | 551883a | 2008-04-29 00:58:26 -0700 | [diff] [blame] | 398 | page = alloc_pages(htlb_alloc_mask|__GFP_COMP| |
| 399 | __GFP_REPEAT|__GFP_NOWARN, |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 400 | HUGETLB_PAGE_ORDER); |
Nishanth Aravamudan | d1c3fb1 | 2007-12-17 16:20:12 -0800 | [diff] [blame] | 401 | |
| 402 | spin_lock(&hugetlb_lock); |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 403 | if (page) { |
Adam Litke | 2668db9 | 2008-03-10 11:43:50 -0700 | [diff] [blame] | 404 | /* |
| 405 | * This page is now managed by the hugetlb allocator and has |
| 406 | * no users -- drop the buddy allocator's reference. |
| 407 | */ |
| 408 | put_page_testzero(page); |
| 409 | VM_BUG_ON(page_count(page)); |
Nishanth Aravamudan | d1c3fb1 | 2007-12-17 16:20:12 -0800 | [diff] [blame] | 410 | nid = page_to_nid(page); |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 411 | set_compound_page_dtor(page, free_huge_page); |
Nishanth Aravamudan | d1c3fb1 | 2007-12-17 16:20:12 -0800 | [diff] [blame] | 412 | /* |
| 413 | * We incremented the global counters already |
| 414 | */ |
| 415 | nr_huge_pages_node[nid]++; |
| 416 | surplus_huge_pages_node[nid]++; |
Adam Litke | 3b11630 | 2008-04-28 02:13:06 -0700 | [diff] [blame] | 417 | __count_vm_event(HTLB_BUDDY_PGALLOC); |
Nishanth Aravamudan | d1c3fb1 | 2007-12-17 16:20:12 -0800 | [diff] [blame] | 418 | } else { |
| 419 | nr_huge_pages--; |
| 420 | surplus_huge_pages--; |
Adam Litke | 3b11630 | 2008-04-28 02:13:06 -0700 | [diff] [blame] | 421 | __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL); |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 422 | } |
Nishanth Aravamudan | d1c3fb1 | 2007-12-17 16:20:12 -0800 | [diff] [blame] | 423 | spin_unlock(&hugetlb_lock); |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 424 | |
| 425 | return page; |
| 426 | } |
| 427 | |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 428 | /* |
| 429 | * Increase the hugetlb pool such that it can accomodate a reservation |
| 430 | * of size 'delta'. |
| 431 | */ |
| 432 | static int gather_surplus_pages(int delta) |
| 433 | { |
| 434 | struct list_head surplus_list; |
| 435 | struct page *page, *tmp; |
| 436 | int ret, i; |
| 437 | int needed, allocated; |
| 438 | |
| 439 | needed = (resv_huge_pages + delta) - free_huge_pages; |
Adam Litke | ac09b3a | 2008-03-04 14:29:38 -0800 | [diff] [blame] | 440 | if (needed <= 0) { |
| 441 | resv_huge_pages += delta; |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 442 | return 0; |
Adam Litke | ac09b3a | 2008-03-04 14:29:38 -0800 | [diff] [blame] | 443 | } |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 444 | |
| 445 | allocated = 0; |
| 446 | INIT_LIST_HEAD(&surplus_list); |
| 447 | |
| 448 | ret = -ENOMEM; |
| 449 | retry: |
| 450 | spin_unlock(&hugetlb_lock); |
| 451 | for (i = 0; i < needed; i++) { |
| 452 | page = alloc_buddy_huge_page(NULL, 0); |
| 453 | if (!page) { |
| 454 | /* |
| 455 | * We were not able to allocate enough pages to |
| 456 | * satisfy the entire reservation so we free what |
| 457 | * we've allocated so far. |
| 458 | */ |
| 459 | spin_lock(&hugetlb_lock); |
| 460 | needed = 0; |
| 461 | goto free; |
| 462 | } |
| 463 | |
| 464 | list_add(&page->lru, &surplus_list); |
| 465 | } |
| 466 | allocated += needed; |
| 467 | |
| 468 | /* |
| 469 | * After retaking hugetlb_lock, we need to recalculate 'needed' |
| 470 | * because either resv_huge_pages or free_huge_pages may have changed. |
| 471 | */ |
| 472 | spin_lock(&hugetlb_lock); |
| 473 | needed = (resv_huge_pages + delta) - (free_huge_pages + allocated); |
| 474 | if (needed > 0) |
| 475 | goto retry; |
| 476 | |
| 477 | /* |
| 478 | * The surplus_list now contains _at_least_ the number of extra pages |
| 479 | * needed to accomodate the reservation. Add the appropriate number |
| 480 | * 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] | 481 | * allocator. Commit the entire reservation here to prevent another |
| 482 | * process from stealing the pages as they are added to the pool but |
| 483 | * before they are reserved. |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 484 | */ |
| 485 | needed += allocated; |
Adam Litke | ac09b3a | 2008-03-04 14:29:38 -0800 | [diff] [blame] | 486 | resv_huge_pages += delta; |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 487 | ret = 0; |
| 488 | free: |
Adam Litke | 19fc3f0 | 2008-04-28 02:12:20 -0700 | [diff] [blame] | 489 | /* Free the needed pages to the hugetlb pool */ |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 490 | list_for_each_entry_safe(page, tmp, &surplus_list, lru) { |
Adam Litke | 19fc3f0 | 2008-04-28 02:12:20 -0700 | [diff] [blame] | 491 | if ((--needed) < 0) |
| 492 | break; |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 493 | list_del(&page->lru); |
Adam Litke | 19fc3f0 | 2008-04-28 02:12:20 -0700 | [diff] [blame] | 494 | enqueue_huge_page(page); |
| 495 | } |
| 496 | |
| 497 | /* Free unnecessary surplus pages to the buddy allocator */ |
| 498 | if (!list_empty(&surplus_list)) { |
| 499 | spin_unlock(&hugetlb_lock); |
| 500 | list_for_each_entry_safe(page, tmp, &surplus_list, lru) { |
| 501 | list_del(&page->lru); |
Adam Litke | af767cb | 2007-10-16 01:26:25 -0700 | [diff] [blame] | 502 | /* |
Adam Litke | 2668db9 | 2008-03-10 11:43:50 -0700 | [diff] [blame] | 503 | * The page has a reference count of zero already, so |
| 504 | * call free_huge_page directly instead of using |
| 505 | * put_page. This must be done with hugetlb_lock |
Adam Litke | af767cb | 2007-10-16 01:26:25 -0700 | [diff] [blame] | 506 | * unlocked which is safe because free_huge_page takes |
| 507 | * hugetlb_lock before deciding how to free the page. |
| 508 | */ |
Adam Litke | 2668db9 | 2008-03-10 11:43:50 -0700 | [diff] [blame] | 509 | free_huge_page(page); |
Adam Litke | af767cb | 2007-10-16 01:26:25 -0700 | [diff] [blame] | 510 | } |
Adam Litke | 19fc3f0 | 2008-04-28 02:12:20 -0700 | [diff] [blame] | 511 | spin_lock(&hugetlb_lock); |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | return ret; |
| 515 | } |
| 516 | |
| 517 | /* |
| 518 | * When releasing a hugetlb pool reservation, any surplus pages that were |
| 519 | * allocated to satisfy the reservation must be explicitly freed if they were |
| 520 | * never used. |
| 521 | */ |
Adrian Bunk | 8cde045 | 2007-11-14 16:59:43 -0800 | [diff] [blame] | 522 | static void return_unused_surplus_pages(unsigned long unused_resv_pages) |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 523 | { |
| 524 | static int nid = -1; |
| 525 | struct page *page; |
| 526 | unsigned long nr_pages; |
| 527 | |
Nishanth Aravamudan | 11320d1 | 2008-03-26 14:40:20 -0700 | [diff] [blame] | 528 | /* |
| 529 | * We want to release as many surplus pages as possible, spread |
| 530 | * evenly across all nodes. Iterate across all nodes until we |
| 531 | * can no longer free unreserved surplus pages. This occurs when |
| 532 | * the nodes with surplus pages have no free pages. |
| 533 | */ |
| 534 | unsigned long remaining_iterations = num_online_nodes(); |
| 535 | |
Adam Litke | ac09b3a | 2008-03-04 14:29:38 -0800 | [diff] [blame] | 536 | /* Uncommit the reservation */ |
| 537 | resv_huge_pages -= unused_resv_pages; |
| 538 | |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 539 | nr_pages = min(unused_resv_pages, surplus_huge_pages); |
| 540 | |
Nishanth Aravamudan | 11320d1 | 2008-03-26 14:40:20 -0700 | [diff] [blame] | 541 | while (remaining_iterations-- && nr_pages) { |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 542 | nid = next_node(nid, node_online_map); |
| 543 | if (nid == MAX_NUMNODES) |
| 544 | nid = first_node(node_online_map); |
| 545 | |
| 546 | if (!surplus_huge_pages_node[nid]) |
| 547 | continue; |
| 548 | |
| 549 | if (!list_empty(&hugepage_freelists[nid])) { |
| 550 | page = list_entry(hugepage_freelists[nid].next, |
| 551 | struct page, lru); |
| 552 | list_del(&page->lru); |
| 553 | update_and_free_page(page); |
| 554 | free_huge_pages--; |
| 555 | free_huge_pages_node[nid]--; |
| 556 | surplus_huge_pages--; |
| 557 | surplus_huge_pages_node[nid]--; |
| 558 | nr_pages--; |
Nishanth Aravamudan | 11320d1 | 2008-03-26 14:40:20 -0700 | [diff] [blame] | 559 | remaining_iterations = num_online_nodes(); |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | } |
| 563 | |
David Gibson | 27a85ef | 2006-03-22 00:08:56 -0800 | [diff] [blame] | 564 | static struct page *alloc_huge_page(struct vm_area_struct *vma, |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 565 | unsigned long addr, int avoid_reserve) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | { |
Adam Litke | 348ea20 | 2007-11-14 16:59:37 -0800 | [diff] [blame] | 567 | struct page *page; |
Adam Litke | 2fc39ce | 2007-11-14 16:59:39 -0800 | [diff] [blame] | 568 | struct address_space *mapping = vma->vm_file->f_mapping; |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 569 | struct inode *inode = mapping->host; |
| 570 | unsigned int chg = 0; |
Adam Litke | 2fc39ce | 2007-11-14 16:59:39 -0800 | [diff] [blame] | 571 | |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 572 | /* |
| 573 | * Processes that did not create the mapping will have no reserves and |
| 574 | * will not have accounted against quota. Check that the quota can be |
| 575 | * made before satisfying the allocation |
| 576 | */ |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 577 | if (!(vma->vm_flags & VM_SHARED) && |
| 578 | !is_vma_resv_set(vma, HPAGE_RESV_OWNER)) { |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 579 | chg = 1; |
| 580 | if (hugetlb_get_quota(inode->i_mapping, chg)) |
| 581 | return ERR_PTR(-ENOSPC); |
Adam Litke | 90d8b7e | 2007-11-14 16:59:42 -0800 | [diff] [blame] | 582 | } |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 583 | |
| 584 | spin_lock(&hugetlb_lock); |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 585 | page = dequeue_huge_page_vma(vma, addr, avoid_reserve); |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 586 | spin_unlock(&hugetlb_lock); |
| 587 | |
| 588 | if (!page) { |
| 589 | page = alloc_buddy_huge_page(vma, addr); |
| 590 | if (!page) { |
| 591 | hugetlb_put_quota(inode->i_mapping, chg); |
| 592 | return ERR_PTR(-VM_FAULT_OOM); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | set_page_refcounted(page); |
| 597 | set_page_private(page, (unsigned long) mapping); |
| 598 | |
Adam Litke | 90d8b7e | 2007-11-14 16:59:42 -0800 | [diff] [blame] | 599 | return page; |
David Gibson | b45b5bd | 2006-03-22 00:08:55 -0800 | [diff] [blame] | 600 | } |
| 601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | static int __init hugetlb_init(void) |
| 603 | { |
| 604 | unsigned long i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 606 | if (HPAGE_SHIFT == 0) |
| 607 | return 0; |
| 608 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | for (i = 0; i < MAX_NUMNODES; ++i) |
| 610 | INIT_LIST_HEAD(&hugepage_freelists[i]); |
| 611 | |
Nishanth Aravamudan | 63b4613 | 2007-10-16 01:26:24 -0700 | [diff] [blame] | 612 | hugetlb_next_nid = first_node(node_online_map); |
| 613 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | for (i = 0; i < max_huge_pages; ++i) { |
Nick Piggin | a482289 | 2006-03-22 00:08:08 -0800 | [diff] [blame] | 615 | if (!alloc_fresh_huge_page()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | } |
| 618 | max_huge_pages = free_huge_pages = nr_huge_pages = i; |
| 619 | printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages); |
| 620 | return 0; |
| 621 | } |
| 622 | module_init(hugetlb_init); |
| 623 | |
| 624 | static int __init hugetlb_setup(char *s) |
| 625 | { |
| 626 | if (sscanf(s, "%lu", &max_huge_pages) <= 0) |
| 627 | max_huge_pages = 0; |
| 628 | return 1; |
| 629 | } |
| 630 | __setup("hugepages=", hugetlb_setup); |
| 631 | |
Ken Chen | 8a63011 | 2007-05-09 02:33:34 -0700 | [diff] [blame] | 632 | static unsigned int cpuset_mems_nr(unsigned int *array) |
| 633 | { |
| 634 | int node; |
| 635 | unsigned int nr = 0; |
| 636 | |
| 637 | for_each_node_mask(node, cpuset_current_mems_allowed) |
| 638 | nr += array[node]; |
| 639 | |
| 640 | return nr; |
| 641 | } |
| 642 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | #ifdef CONFIG_SYSCTL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | #ifdef CONFIG_HIGHMEM |
| 645 | static void try_to_free_low(unsigned long count) |
| 646 | { |
Christoph Lameter | 4415cc8 | 2006-09-25 23:31:55 -0700 | [diff] [blame] | 647 | int i; |
| 648 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | for (i = 0; i < MAX_NUMNODES; ++i) { |
| 650 | struct page *page, *next; |
| 651 | list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) { |
Adam Litke | 6b0c880 | 2007-10-16 01:26:23 -0700 | [diff] [blame] | 652 | if (count >= nr_huge_pages) |
| 653 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | if (PageHighMem(page)) |
| 655 | continue; |
| 656 | list_del(&page->lru); |
| 657 | update_and_free_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | free_huge_pages--; |
Christoph Lameter | 4415cc8 | 2006-09-25 23:31:55 -0700 | [diff] [blame] | 659 | free_huge_pages_node[page_to_nid(page)]--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | } |
| 663 | #else |
| 664 | static inline void try_to_free_low(unsigned long count) |
| 665 | { |
| 666 | } |
| 667 | #endif |
| 668 | |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 669 | #define persistent_huge_pages (nr_huge_pages - surplus_huge_pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | static unsigned long set_max_huge_pages(unsigned long count) |
| 671 | { |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 672 | unsigned long min_count, ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 674 | /* |
| 675 | * Increase the pool size |
| 676 | * First take pages out of surplus state. Then make up the |
| 677 | * remaining difference by allocating fresh huge pages. |
Nishanth Aravamudan | d1c3fb1 | 2007-12-17 16:20:12 -0800 | [diff] [blame] | 678 | * |
| 679 | * We might race with alloc_buddy_huge_page() here and be unable |
| 680 | * to convert a surplus huge page to a normal huge page. That is |
| 681 | * not critical, though, it just means the overall size of the |
| 682 | * pool might be one hugepage larger than it needs to be, but |
| 683 | * within all the constraints specified by the sysctls. |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 684 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | spin_lock(&hugetlb_lock); |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 686 | while (surplus_huge_pages && count > persistent_huge_pages) { |
| 687 | if (!adjust_pool_surplus(-1)) |
| 688 | break; |
| 689 | } |
| 690 | |
| 691 | while (count > persistent_huge_pages) { |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 692 | /* |
| 693 | * If this allocation races such that we no longer need the |
| 694 | * page, free_huge_page will handle it by freeing the page |
| 695 | * and reducing the surplus. |
| 696 | */ |
| 697 | spin_unlock(&hugetlb_lock); |
| 698 | ret = alloc_fresh_huge_page(); |
| 699 | spin_lock(&hugetlb_lock); |
| 700 | if (!ret) |
| 701 | goto out; |
| 702 | |
| 703 | } |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 704 | |
| 705 | /* |
| 706 | * Decrease the pool size |
| 707 | * First return free pages to the buddy allocator (being careful |
| 708 | * to keep enough around to satisfy reservations). Then place |
| 709 | * pages into surplus state as needed so the pool will shrink |
| 710 | * to the desired size as pages become free. |
Nishanth Aravamudan | d1c3fb1 | 2007-12-17 16:20:12 -0800 | [diff] [blame] | 711 | * |
| 712 | * By placing pages into the surplus state independent of the |
| 713 | * overcommit value, we are allowing the surplus pool size to |
| 714 | * exceed overcommit. There are few sane options here. Since |
| 715 | * alloc_buddy_huge_page() is checking the global counter, |
| 716 | * though, we'll note that we're not allowed to exceed surplus |
| 717 | * and won't grow the pool anywhere else. Not until one of the |
| 718 | * sysctls are changed, or the surplus pages go out of use. |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 719 | */ |
Adam Litke | 6b0c880 | 2007-10-16 01:26:23 -0700 | [diff] [blame] | 720 | min_count = resv_huge_pages + nr_huge_pages - free_huge_pages; |
| 721 | min_count = max(count, min_count); |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 722 | try_to_free_low(min_count); |
| 723 | while (min_count < persistent_huge_pages) { |
Nishanth Aravamudan | 348e1e0 | 2008-03-04 14:29:42 -0800 | [diff] [blame] | 724 | struct page *page = dequeue_huge_page(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | if (!page) |
| 726 | break; |
| 727 | update_and_free_page(page); |
| 728 | } |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 729 | while (count < persistent_huge_pages) { |
| 730 | if (!adjust_pool_surplus(1)) |
| 731 | break; |
| 732 | } |
| 733 | out: |
| 734 | ret = persistent_huge_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | spin_unlock(&hugetlb_lock); |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 736 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | int hugetlb_sysctl_handler(struct ctl_table *table, int write, |
| 740 | struct file *file, void __user *buffer, |
| 741 | size_t *length, loff_t *ppos) |
| 742 | { |
| 743 | proc_doulongvec_minmax(table, write, file, buffer, length, ppos); |
| 744 | max_huge_pages = set_max_huge_pages(max_huge_pages); |
| 745 | return 0; |
| 746 | } |
Mel Gorman | 396faf0 | 2007-07-17 04:03:13 -0700 | [diff] [blame] | 747 | |
| 748 | int hugetlb_treat_movable_handler(struct ctl_table *table, int write, |
| 749 | struct file *file, void __user *buffer, |
| 750 | size_t *length, loff_t *ppos) |
| 751 | { |
| 752 | proc_dointvec(table, write, file, buffer, length, ppos); |
| 753 | if (hugepages_treat_as_movable) |
| 754 | htlb_alloc_mask = GFP_HIGHUSER_MOVABLE; |
| 755 | else |
| 756 | htlb_alloc_mask = GFP_HIGHUSER; |
| 757 | return 0; |
| 758 | } |
| 759 | |
Nishanth Aravamudan | a3d0c6a | 2008-02-08 04:18:18 -0800 | [diff] [blame] | 760 | int hugetlb_overcommit_handler(struct ctl_table *table, int write, |
| 761 | struct file *file, void __user *buffer, |
| 762 | size_t *length, loff_t *ppos) |
| 763 | { |
Nishanth Aravamudan | a3d0c6a | 2008-02-08 04:18:18 -0800 | [diff] [blame] | 764 | proc_doulongvec_minmax(table, write, file, buffer, length, ppos); |
Nishanth Aravamudan | 064d9ef | 2008-02-13 15:03:19 -0800 | [diff] [blame] | 765 | spin_lock(&hugetlb_lock); |
| 766 | nr_overcommit_huge_pages = sysctl_overcommit_huge_pages; |
Nishanth Aravamudan | a3d0c6a | 2008-02-08 04:18:18 -0800 | [diff] [blame] | 767 | spin_unlock(&hugetlb_lock); |
| 768 | return 0; |
| 769 | } |
| 770 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | #endif /* CONFIG_SYSCTL */ |
| 772 | |
| 773 | int hugetlb_report_meminfo(char *buf) |
| 774 | { |
| 775 | return sprintf(buf, |
| 776 | "HugePages_Total: %5lu\n" |
| 777 | "HugePages_Free: %5lu\n" |
Chen, Kenneth W | a43a8c3 | 2006-06-23 02:03:15 -0700 | [diff] [blame] | 778 | "HugePages_Rsvd: %5lu\n" |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 779 | "HugePages_Surp: %5lu\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | "Hugepagesize: %5lu kB\n", |
| 781 | nr_huge_pages, |
| 782 | free_huge_pages, |
Chen, Kenneth W | a43a8c3 | 2006-06-23 02:03:15 -0700 | [diff] [blame] | 783 | resv_huge_pages, |
Adam Litke | 7893d1d | 2007-10-16 01:26:18 -0700 | [diff] [blame] | 784 | surplus_huge_pages, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | HPAGE_SIZE/1024); |
| 786 | } |
| 787 | |
| 788 | int hugetlb_report_node_meminfo(int nid, char *buf) |
| 789 | { |
| 790 | return sprintf(buf, |
| 791 | "Node %d HugePages_Total: %5u\n" |
Nishanth Aravamudan | a1de091 | 2008-03-26 14:37:53 -0700 | [diff] [blame] | 792 | "Node %d HugePages_Free: %5u\n" |
| 793 | "Node %d HugePages_Surp: %5u\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | nid, nr_huge_pages_node[nid], |
Nishanth Aravamudan | a1de091 | 2008-03-26 14:37:53 -0700 | [diff] [blame] | 795 | nid, free_huge_pages_node[nid], |
| 796 | nid, surplus_huge_pages_node[nid]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | } |
| 798 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | /* Return the number pages of memory we physically have, in PAGE_SIZE units. */ |
| 800 | unsigned long hugetlb_total_pages(void) |
| 801 | { |
| 802 | return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE); |
| 803 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | |
Mel Gorman | fc1b8a7 | 2008-07-23 21:27:22 -0700 | [diff] [blame] | 805 | static int hugetlb_acct_memory(long delta) |
| 806 | { |
| 807 | int ret = -ENOMEM; |
| 808 | |
| 809 | spin_lock(&hugetlb_lock); |
| 810 | /* |
| 811 | * When cpuset is configured, it breaks the strict hugetlb page |
| 812 | * reservation as the accounting is done on a global variable. Such |
| 813 | * reservation is completely rubbish in the presence of cpuset because |
| 814 | * the reservation is not checked against page availability for the |
| 815 | * current cpuset. Application can still potentially OOM'ed by kernel |
| 816 | * with lack of free htlb page in cpuset that the task is in. |
| 817 | * Attempt to enforce strict accounting with cpuset is almost |
| 818 | * impossible (or too ugly) because cpuset is too fluid that |
| 819 | * task or memory node can be dynamically moved between cpusets. |
| 820 | * |
| 821 | * The change of semantics for shared hugetlb mapping with cpuset is |
| 822 | * undesirable. However, in order to preserve some of the semantics, |
| 823 | * we fall back to check against current free page availability as |
| 824 | * a best attempt and hopefully to minimize the impact of changing |
| 825 | * semantics that cpuset has. |
| 826 | */ |
| 827 | if (delta > 0) { |
| 828 | if (gather_surplus_pages(delta) < 0) |
| 829 | goto out; |
| 830 | |
| 831 | if (delta > cpuset_mems_nr(free_huge_pages_node)) { |
| 832 | return_unused_surplus_pages(delta); |
| 833 | goto out; |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | ret = 0; |
| 838 | if (delta < 0) |
| 839 | return_unused_surplus_pages((unsigned long) -delta); |
| 840 | |
| 841 | out: |
| 842 | spin_unlock(&hugetlb_lock); |
| 843 | return ret; |
| 844 | } |
| 845 | |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 846 | static void hugetlb_vm_op_close(struct vm_area_struct *vma) |
| 847 | { |
| 848 | unsigned long reserve = vma_resv_huge_pages(vma); |
| 849 | if (reserve) |
| 850 | hugetlb_acct_memory(-reserve); |
| 851 | } |
| 852 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | /* |
| 854 | * We cannot handle pagefaults against hugetlb pages at all. They cause |
| 855 | * handle_mm_fault() to try to instantiate regular-sized pages in the |
| 856 | * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get |
| 857 | * this far. |
| 858 | */ |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 859 | 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] | 860 | { |
| 861 | BUG(); |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 862 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | struct vm_operations_struct hugetlb_vm_ops = { |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 866 | .fault = hugetlb_vm_op_fault, |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 867 | .close = hugetlb_vm_op_close, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | }; |
| 869 | |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 870 | static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page, |
| 871 | int writable) |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 872 | { |
| 873 | pte_t entry; |
| 874 | |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 875 | if (writable) { |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 876 | entry = |
| 877 | pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot))); |
| 878 | } else { |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 879 | entry = huge_pte_wrprotect(mk_pte(page, vma->vm_page_prot)); |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 880 | } |
| 881 | entry = pte_mkyoung(entry); |
| 882 | entry = pte_mkhuge(entry); |
| 883 | |
| 884 | return entry; |
| 885 | } |
| 886 | |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 887 | static void set_huge_ptep_writable(struct vm_area_struct *vma, |
| 888 | unsigned long address, pte_t *ptep) |
| 889 | { |
| 890 | pte_t entry; |
| 891 | |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 892 | entry = pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep))); |
| 893 | if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) { |
Benjamin Herrenschmidt | 8dab524 | 2007-06-16 10:16:12 -0700 | [diff] [blame] | 894 | update_mmu_cache(vma, address, entry); |
Benjamin Herrenschmidt | 8dab524 | 2007-06-16 10:16:12 -0700 | [diff] [blame] | 895 | } |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 899 | int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, |
| 900 | struct vm_area_struct *vma) |
| 901 | { |
| 902 | pte_t *src_pte, *dst_pte, entry; |
| 903 | struct page *ptepage; |
Hugh Dickins | 1c59827 | 2005-10-19 21:23:43 -0700 | [diff] [blame] | 904 | unsigned long addr; |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 905 | int cow; |
| 906 | |
| 907 | cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 908 | |
Hugh Dickins | 1c59827 | 2005-10-19 21:23:43 -0700 | [diff] [blame] | 909 | for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) { |
Hugh Dickins | c74df32 | 2005-10-29 18:16:23 -0700 | [diff] [blame] | 910 | src_pte = huge_pte_offset(src, addr); |
| 911 | if (!src_pte) |
| 912 | continue; |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 913 | dst_pte = huge_pte_alloc(dst, addr); |
| 914 | if (!dst_pte) |
| 915 | goto nomem; |
Larry Woodman | c5c9942 | 2008-01-24 05:49:25 -0800 | [diff] [blame] | 916 | |
| 917 | /* If the pagetables are shared don't copy or take references */ |
| 918 | if (dst_pte == src_pte) |
| 919 | continue; |
| 920 | |
Hugh Dickins | c74df32 | 2005-10-29 18:16:23 -0700 | [diff] [blame] | 921 | spin_lock(&dst->page_table_lock); |
Nick Piggin | 4647875 | 2008-06-05 22:45:57 -0700 | [diff] [blame] | 922 | spin_lock_nested(&src->page_table_lock, SINGLE_DEPTH_NESTING); |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 923 | if (!huge_pte_none(huge_ptep_get(src_pte))) { |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 924 | if (cow) |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 925 | huge_ptep_set_wrprotect(src, addr, src_pte); |
| 926 | entry = huge_ptep_get(src_pte); |
Hugh Dickins | 1c59827 | 2005-10-19 21:23:43 -0700 | [diff] [blame] | 927 | ptepage = pte_page(entry); |
| 928 | get_page(ptepage); |
Hugh Dickins | 1c59827 | 2005-10-19 21:23:43 -0700 | [diff] [blame] | 929 | set_huge_pte_at(dst, addr, dst_pte, entry); |
| 930 | } |
| 931 | spin_unlock(&src->page_table_lock); |
Hugh Dickins | c74df32 | 2005-10-29 18:16:23 -0700 | [diff] [blame] | 932 | spin_unlock(&dst->page_table_lock); |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 933 | } |
| 934 | return 0; |
| 935 | |
| 936 | nomem: |
| 937 | return -ENOMEM; |
| 938 | } |
| 939 | |
Chen, Kenneth W | 502717f | 2006-10-11 01:20:46 -0700 | [diff] [blame] | 940 | void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start, |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 941 | unsigned long end, struct page *ref_page) |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 942 | { |
| 943 | struct mm_struct *mm = vma->vm_mm; |
| 944 | unsigned long address; |
David Gibson | c7546f8 | 2005-08-05 11:59:35 -0700 | [diff] [blame] | 945 | pte_t *ptep; |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 946 | pte_t pte; |
| 947 | struct page *page; |
Chen, Kenneth W | fe1668a | 2006-10-04 02:15:24 -0700 | [diff] [blame] | 948 | struct page *tmp; |
Chen, Kenneth W | c0a499c | 2006-12-06 20:31:39 -0800 | [diff] [blame] | 949 | /* |
| 950 | * A page gathering list, protected by per file i_mmap_lock. The |
| 951 | * lock is used to avoid list corruption from multiple unmapping |
| 952 | * of the same page since we are using page->lru. |
| 953 | */ |
Chen, Kenneth W | fe1668a | 2006-10-04 02:15:24 -0700 | [diff] [blame] | 954 | LIST_HEAD(page_list); |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 955 | |
| 956 | WARN_ON(!is_vm_hugetlb_page(vma)); |
| 957 | BUG_ON(start & ~HPAGE_MASK); |
| 958 | BUG_ON(end & ~HPAGE_MASK); |
| 959 | |
Hugh Dickins | 508034a | 2005-10-29 18:16:30 -0700 | [diff] [blame] | 960 | spin_lock(&mm->page_table_lock); |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 961 | for (address = start; address < end; address += HPAGE_SIZE) { |
David Gibson | c7546f8 | 2005-08-05 11:59:35 -0700 | [diff] [blame] | 962 | ptep = huge_pte_offset(mm, address); |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 963 | if (!ptep) |
David Gibson | c7546f8 | 2005-08-05 11:59:35 -0700 | [diff] [blame] | 964 | continue; |
| 965 | |
Chen, Kenneth W | 39dde65 | 2006-12-06 20:32:03 -0800 | [diff] [blame] | 966 | if (huge_pmd_unshare(mm, &address, ptep)) |
| 967 | continue; |
| 968 | |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 969 | /* |
| 970 | * If a reference page is supplied, it is because a specific |
| 971 | * page is being unmapped, not a range. Ensure the page we |
| 972 | * are about to unmap is the actual page of interest. |
| 973 | */ |
| 974 | if (ref_page) { |
| 975 | pte = huge_ptep_get(ptep); |
| 976 | if (huge_pte_none(pte)) |
| 977 | continue; |
| 978 | page = pte_page(pte); |
| 979 | if (page != ref_page) |
| 980 | continue; |
| 981 | |
| 982 | /* |
| 983 | * Mark the VMA as having unmapped its page so that |
| 984 | * future faults in this VMA will fail rather than |
| 985 | * looking like data was lost |
| 986 | */ |
| 987 | set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED); |
| 988 | } |
| 989 | |
David Gibson | c7546f8 | 2005-08-05 11:59:35 -0700 | [diff] [blame] | 990 | pte = huge_ptep_get_and_clear(mm, address, ptep); |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 991 | if (huge_pte_none(pte)) |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 992 | continue; |
David Gibson | c7546f8 | 2005-08-05 11:59:35 -0700 | [diff] [blame] | 993 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 994 | page = pte_page(pte); |
Ken Chen | 6649a38 | 2007-02-08 14:20:27 -0800 | [diff] [blame] | 995 | if (pte_dirty(pte)) |
| 996 | set_page_dirty(page); |
Chen, Kenneth W | fe1668a | 2006-10-04 02:15:24 -0700 | [diff] [blame] | 997 | list_add(&page->lru, &page_list); |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 998 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | spin_unlock(&mm->page_table_lock); |
Hugh Dickins | 508034a | 2005-10-29 18:16:30 -0700 | [diff] [blame] | 1000 | flush_tlb_range(vma, start, end); |
Chen, Kenneth W | fe1668a | 2006-10-04 02:15:24 -0700 | [diff] [blame] | 1001 | list_for_each_entry_safe(page, tmp, &page_list, lru) { |
| 1002 | list_del(&page->lru); |
| 1003 | put_page(page); |
| 1004 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | } |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1006 | |
Chen, Kenneth W | 502717f | 2006-10-11 01:20:46 -0700 | [diff] [blame] | 1007 | void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start, |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 1008 | unsigned long end, struct page *ref_page) |
Chen, Kenneth W | 502717f | 2006-10-11 01:20:46 -0700 | [diff] [blame] | 1009 | { |
| 1010 | /* |
| 1011 | * It is undesirable to test vma->vm_file as it should be non-null |
| 1012 | * for valid hugetlb area. However, vm_file will be NULL in the error |
| 1013 | * cleanup path of do_mmap_pgoff. When hugetlbfs ->mmap method fails, |
| 1014 | * do_mmap_pgoff() nullifies vma->vm_file before calling this function |
| 1015 | * to clean up. Since no pte has actually been setup, it is safe to |
| 1016 | * do nothing in this case. |
| 1017 | */ |
| 1018 | if (vma->vm_file) { |
| 1019 | spin_lock(&vma->vm_file->f_mapping->i_mmap_lock); |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 1020 | __unmap_hugepage_range(vma, start, end, ref_page); |
Chen, Kenneth W | 502717f | 2006-10-11 01:20:46 -0700 | [diff] [blame] | 1021 | spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock); |
| 1022 | } |
| 1023 | } |
| 1024 | |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 1025 | /* |
| 1026 | * This is called when the original mapper is failing to COW a MAP_PRIVATE |
| 1027 | * mappping it owns the reserve page for. The intention is to unmap the page |
| 1028 | * from other VMAs and let the children be SIGKILLed if they are faulting the |
| 1029 | * same region. |
| 1030 | */ |
| 1031 | int unmap_ref_private(struct mm_struct *mm, |
| 1032 | struct vm_area_struct *vma, |
| 1033 | struct page *page, |
| 1034 | unsigned long address) |
| 1035 | { |
| 1036 | struct vm_area_struct *iter_vma; |
| 1037 | struct address_space *mapping; |
| 1038 | struct prio_tree_iter iter; |
| 1039 | pgoff_t pgoff; |
| 1040 | |
| 1041 | /* |
| 1042 | * vm_pgoff is in PAGE_SIZE units, hence the different calculation |
| 1043 | * from page cache lookup which is in HPAGE_SIZE units. |
| 1044 | */ |
| 1045 | address = address & huge_page_mask(hstate_vma(vma)); |
| 1046 | pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) |
| 1047 | + (vma->vm_pgoff >> PAGE_SHIFT); |
| 1048 | mapping = (struct address_space *)page_private(page); |
| 1049 | |
| 1050 | vma_prio_tree_foreach(iter_vma, &iter, &mapping->i_mmap, pgoff, pgoff) { |
| 1051 | /* Do not unmap the current VMA */ |
| 1052 | if (iter_vma == vma) |
| 1053 | continue; |
| 1054 | |
| 1055 | /* |
| 1056 | * Unmap the page from other VMAs without their own reserves. |
| 1057 | * They get marked to be SIGKILLed if they fault in these |
| 1058 | * areas. This is because a future no-page fault on this VMA |
| 1059 | * could insert a zeroed page instead of the data existing |
| 1060 | * from the time of fork. This would look like data corruption |
| 1061 | */ |
| 1062 | if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER)) |
| 1063 | unmap_hugepage_range(iter_vma, |
| 1064 | address, address + HPAGE_SIZE, |
| 1065 | page); |
| 1066 | } |
| 1067 | |
| 1068 | return 1; |
| 1069 | } |
| 1070 | |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1071 | 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^] | 1072 | unsigned long address, pte_t *ptep, pte_t pte, |
| 1073 | struct page *pagecache_page) |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1074 | { |
| 1075 | struct page *old_page, *new_page; |
David Gibson | 79ac6ba | 2006-03-22 00:08:51 -0800 | [diff] [blame] | 1076 | int avoidcopy; |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 1077 | int outside_reserve = 0; |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1078 | |
| 1079 | old_page = pte_page(pte); |
| 1080 | |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 1081 | retry_avoidcopy: |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1082 | /* If no-one else is actually using this page, avoid the copy |
| 1083 | * and just make the page writable */ |
| 1084 | avoidcopy = (page_count(old_page) == 1); |
| 1085 | if (avoidcopy) { |
| 1086 | set_huge_ptep_writable(vma, address, ptep); |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 1087 | return 0; |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1088 | } |
| 1089 | |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 1090 | /* |
| 1091 | * If the process that created a MAP_PRIVATE mapping is about to |
| 1092 | * perform a COW due to a shared page count, attempt to satisfy |
| 1093 | * the allocation without using the existing reserves. The pagecache |
| 1094 | * page is used to determine if the reserve at this address was |
| 1095 | * consumed or not. If reserves were used, a partial faulted mapping |
| 1096 | * at the time of fork() could consume its reserves on COW instead |
| 1097 | * of the full address range. |
| 1098 | */ |
| 1099 | if (!(vma->vm_flags & VM_SHARED) && |
| 1100 | is_vma_resv_set(vma, HPAGE_RESV_OWNER) && |
| 1101 | old_page != pagecache_page) |
| 1102 | outside_reserve = 1; |
| 1103 | |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1104 | page_cache_get(old_page); |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 1105 | new_page = alloc_huge_page(vma, address, outside_reserve); |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1106 | |
Adam Litke | 2fc39ce | 2007-11-14 16:59:39 -0800 | [diff] [blame] | 1107 | if (IS_ERR(new_page)) { |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1108 | page_cache_release(old_page); |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 1109 | |
| 1110 | /* |
| 1111 | * If a process owning a MAP_PRIVATE mapping fails to COW, |
| 1112 | * it is due to references held by a child and an insufficient |
| 1113 | * huge page pool. To guarantee the original mappers |
| 1114 | * reliability, unmap the page from child processes. The child |
| 1115 | * may get SIGKILLed if it later faults. |
| 1116 | */ |
| 1117 | if (outside_reserve) { |
| 1118 | BUG_ON(huge_pte_none(pte)); |
| 1119 | if (unmap_ref_private(mm, vma, old_page, address)) { |
| 1120 | BUG_ON(page_count(old_page) != 1); |
| 1121 | BUG_ON(huge_pte_none(pte)); |
| 1122 | goto retry_avoidcopy; |
| 1123 | } |
| 1124 | WARN_ON_ONCE(1); |
| 1125 | } |
| 1126 | |
Adam Litke | 2fc39ce | 2007-11-14 16:59:39 -0800 | [diff] [blame] | 1127 | return -PTR_ERR(new_page); |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | spin_unlock(&mm->page_table_lock); |
Atsushi Nemoto | 9de455b | 2006-12-12 17:14:55 +0000 | [diff] [blame] | 1131 | copy_huge_page(new_page, old_page, address, vma); |
Nick Piggin | 0ed361d | 2008-02-04 22:29:34 -0800 | [diff] [blame] | 1132 | __SetPageUptodate(new_page); |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1133 | spin_lock(&mm->page_table_lock); |
| 1134 | |
| 1135 | ptep = huge_pte_offset(mm, address & HPAGE_MASK); |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 1136 | if (likely(pte_same(huge_ptep_get(ptep), pte))) { |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1137 | /* Break COW */ |
Gerald Schaefer | 8fe627e | 2008-04-28 02:13:28 -0700 | [diff] [blame] | 1138 | huge_ptep_clear_flush(vma, address, ptep); |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1139 | set_huge_pte_at(mm, address, ptep, |
| 1140 | make_huge_pte(vma, new_page, 1)); |
| 1141 | /* Make the old page be freed below */ |
| 1142 | new_page = old_page; |
| 1143 | } |
| 1144 | page_cache_release(new_page); |
| 1145 | page_cache_release(old_page); |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 1146 | return 0; |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1147 | } |
| 1148 | |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 1149 | /* Return the pagecache page at a given address within a VMA */ |
| 1150 | static struct page *hugetlbfs_pagecache_page(struct vm_area_struct *vma, |
| 1151 | unsigned long address) |
| 1152 | { |
| 1153 | struct address_space *mapping; |
| 1154 | unsigned long idx; |
| 1155 | |
| 1156 | mapping = vma->vm_file->f_mapping; |
| 1157 | idx = ((address - vma->vm_start) >> HPAGE_SHIFT) |
| 1158 | + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT)); |
| 1159 | |
| 1160 | return find_lock_page(mapping, idx); |
| 1161 | } |
| 1162 | |
Robert P. J. Day | a1ed3dd | 2007-07-17 04:03:33 -0700 | [diff] [blame] | 1163 | 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] | 1164 | unsigned long address, pte_t *ptep, int write_access) |
Hugh Dickins | ac9b9c6 | 2005-10-20 16:24:28 +0100 | [diff] [blame] | 1165 | { |
| 1166 | int ret = VM_FAULT_SIGBUS; |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1167 | unsigned long idx; |
| 1168 | unsigned long size; |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1169 | struct page *page; |
| 1170 | struct address_space *mapping; |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1171 | pte_t new_pte; |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1172 | |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 1173 | /* |
| 1174 | * Currently, we are forced to kill the process in the event the |
| 1175 | * original mapper has unmapped pages from the child due to a failed |
| 1176 | * COW. Warn that such a situation has occured as it may not be obvious |
| 1177 | */ |
| 1178 | if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) { |
| 1179 | printk(KERN_WARNING |
| 1180 | "PID %d killed due to inadequate hugepage pool\n", |
| 1181 | current->pid); |
| 1182 | return ret; |
| 1183 | } |
| 1184 | |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1185 | mapping = vma->vm_file->f_mapping; |
| 1186 | idx = ((address - vma->vm_start) >> HPAGE_SHIFT) |
| 1187 | + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT)); |
| 1188 | |
| 1189 | /* |
| 1190 | * Use page lock to guard against racing truncation |
| 1191 | * before we get page_table_lock. |
| 1192 | */ |
Christoph Lameter | 6bda666 | 2006-01-06 00:10:49 -0800 | [diff] [blame] | 1193 | retry: |
| 1194 | page = find_lock_page(mapping, idx); |
| 1195 | if (!page) { |
Hugh Dickins | ebed4bf | 2006-10-28 10:38:43 -0700 | [diff] [blame] | 1196 | size = i_size_read(mapping->host) >> HPAGE_SHIFT; |
| 1197 | if (idx >= size) |
| 1198 | goto out; |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 1199 | page = alloc_huge_page(vma, address, 0); |
Adam Litke | 2fc39ce | 2007-11-14 16:59:39 -0800 | [diff] [blame] | 1200 | if (IS_ERR(page)) { |
| 1201 | ret = -PTR_ERR(page); |
Christoph Lameter | 6bda666 | 2006-01-06 00:10:49 -0800 | [diff] [blame] | 1202 | goto out; |
| 1203 | } |
David Gibson | 79ac6ba | 2006-03-22 00:08:51 -0800 | [diff] [blame] | 1204 | clear_huge_page(page, address); |
Nick Piggin | 0ed361d | 2008-02-04 22:29:34 -0800 | [diff] [blame] | 1205 | __SetPageUptodate(page); |
Hugh Dickins | ac9b9c6 | 2005-10-20 16:24:28 +0100 | [diff] [blame] | 1206 | |
Christoph Lameter | 6bda666 | 2006-01-06 00:10:49 -0800 | [diff] [blame] | 1207 | if (vma->vm_flags & VM_SHARED) { |
| 1208 | int err; |
Ken Chen | 45c682a | 2007-11-14 16:59:44 -0800 | [diff] [blame] | 1209 | struct inode *inode = mapping->host; |
Christoph Lameter | 6bda666 | 2006-01-06 00:10:49 -0800 | [diff] [blame] | 1210 | |
| 1211 | err = add_to_page_cache(page, mapping, idx, GFP_KERNEL); |
| 1212 | if (err) { |
| 1213 | put_page(page); |
Christoph Lameter | 6bda666 | 2006-01-06 00:10:49 -0800 | [diff] [blame] | 1214 | if (err == -EEXIST) |
| 1215 | goto retry; |
| 1216 | goto out; |
| 1217 | } |
Ken Chen | 45c682a | 2007-11-14 16:59:44 -0800 | [diff] [blame] | 1218 | |
| 1219 | spin_lock(&inode->i_lock); |
| 1220 | inode->i_blocks += BLOCKS_PER_HUGEPAGE; |
| 1221 | spin_unlock(&inode->i_lock); |
Christoph Lameter | 6bda666 | 2006-01-06 00:10:49 -0800 | [diff] [blame] | 1222 | } else |
| 1223 | lock_page(page); |
| 1224 | } |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1225 | |
Hugh Dickins | ac9b9c6 | 2005-10-20 16:24:28 +0100 | [diff] [blame] | 1226 | spin_lock(&mm->page_table_lock); |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1227 | size = i_size_read(mapping->host) >> HPAGE_SHIFT; |
| 1228 | if (idx >= size) |
| 1229 | goto backout; |
| 1230 | |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 1231 | ret = 0; |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 1232 | if (!huge_pte_none(huge_ptep_get(ptep))) |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1233 | goto backout; |
| 1234 | |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1235 | new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE) |
| 1236 | && (vma->vm_flags & VM_SHARED))); |
| 1237 | set_huge_pte_at(mm, address, ptep, new_pte); |
| 1238 | |
| 1239 | if (write_access && !(vma->vm_flags & VM_SHARED)) { |
| 1240 | /* Optimization, do the COW without a second fault */ |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 1241 | ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page); |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1242 | } |
| 1243 | |
Hugh Dickins | ac9b9c6 | 2005-10-20 16:24:28 +0100 | [diff] [blame] | 1244 | spin_unlock(&mm->page_table_lock); |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1245 | unlock_page(page); |
| 1246 | out: |
Hugh Dickins | ac9b9c6 | 2005-10-20 16:24:28 +0100 | [diff] [blame] | 1247 | return ret; |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1248 | |
| 1249 | backout: |
| 1250 | spin_unlock(&mm->page_table_lock); |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1251 | unlock_page(page); |
| 1252 | put_page(page); |
| 1253 | goto out; |
Hugh Dickins | ac9b9c6 | 2005-10-20 16:24:28 +0100 | [diff] [blame] | 1254 | } |
| 1255 | |
Adam Litke | 86e5216 | 2006-01-06 00:10:43 -0800 | [diff] [blame] | 1256 | int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, |
| 1257 | unsigned long address, int write_access) |
| 1258 | { |
| 1259 | pte_t *ptep; |
| 1260 | pte_t entry; |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1261 | int ret; |
David Gibson | 3935baa | 2006-03-22 00:08:53 -0800 | [diff] [blame] | 1262 | static DEFINE_MUTEX(hugetlb_instantiation_mutex); |
Adam Litke | 86e5216 | 2006-01-06 00:10:43 -0800 | [diff] [blame] | 1263 | |
| 1264 | ptep = huge_pte_alloc(mm, address); |
| 1265 | if (!ptep) |
| 1266 | return VM_FAULT_OOM; |
| 1267 | |
David Gibson | 3935baa | 2006-03-22 00:08:53 -0800 | [diff] [blame] | 1268 | /* |
| 1269 | * Serialize hugepage allocation and instantiation, so that we don't |
| 1270 | * get spurious allocation failures if two CPUs race to instantiate |
| 1271 | * the same page in the page cache. |
| 1272 | */ |
| 1273 | mutex_lock(&hugetlb_instantiation_mutex); |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 1274 | entry = huge_ptep_get(ptep); |
| 1275 | if (huge_pte_none(entry)) { |
David Gibson | 3935baa | 2006-03-22 00:08:53 -0800 | [diff] [blame] | 1276 | ret = hugetlb_no_page(mm, vma, address, ptep, write_access); |
| 1277 | mutex_unlock(&hugetlb_instantiation_mutex); |
| 1278 | return ret; |
| 1279 | } |
Adam Litke | 86e5216 | 2006-01-06 00:10:43 -0800 | [diff] [blame] | 1280 | |
Nick Piggin | 83c5407 | 2007-07-19 01:47:05 -0700 | [diff] [blame] | 1281 | ret = 0; |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1282 | |
| 1283 | spin_lock(&mm->page_table_lock); |
| 1284 | /* Check for a racing update before calling hugetlb_cow */ |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 1285 | if (likely(pte_same(entry, huge_ptep_get(ptep)))) |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 1286 | if (write_access && !pte_write(entry)) { |
| 1287 | struct page *page; |
| 1288 | page = hugetlbfs_pagecache_page(vma, address); |
| 1289 | ret = hugetlb_cow(mm, vma, address, ptep, entry, page); |
| 1290 | if (page) { |
| 1291 | unlock_page(page); |
| 1292 | put_page(page); |
| 1293 | } |
| 1294 | } |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1295 | spin_unlock(&mm->page_table_lock); |
David Gibson | 3935baa | 2006-03-22 00:08:53 -0800 | [diff] [blame] | 1296 | mutex_unlock(&hugetlb_instantiation_mutex); |
David Gibson | 1e8f889 | 2006-01-06 00:10:44 -0800 | [diff] [blame] | 1297 | |
| 1298 | return ret; |
Adam Litke | 86e5216 | 2006-01-06 00:10:43 -0800 | [diff] [blame] | 1299 | } |
| 1300 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1301 | int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, |
| 1302 | struct page **pages, struct vm_area_struct **vmas, |
Adam Litke | 5b23dbe | 2007-11-14 16:59:33 -0800 | [diff] [blame] | 1303 | unsigned long *position, int *length, int i, |
| 1304 | int write) |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1305 | { |
Chen, Kenneth W | d5d4b0a | 2006-03-22 00:09:03 -0800 | [diff] [blame] | 1306 | unsigned long pfn_offset; |
| 1307 | unsigned long vaddr = *position; |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1308 | int remainder = *length; |
| 1309 | |
Hugh Dickins | 1c59827 | 2005-10-19 21:23:43 -0700 | [diff] [blame] | 1310 | spin_lock(&mm->page_table_lock); |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1311 | while (vaddr < vma->vm_end && remainder) { |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1312 | pte_t *pte; |
| 1313 | struct page *page; |
| 1314 | |
| 1315 | /* |
| 1316 | * Some archs (sparc64, sh*) have multiple pte_ts to |
| 1317 | * each hugepage. We have to make * sure we get the |
| 1318 | * first, for the page indexing below to work. |
| 1319 | */ |
| 1320 | pte = huge_pte_offset(mm, vaddr & HPAGE_MASK); |
| 1321 | |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 1322 | if (!pte || huge_pte_none(huge_ptep_get(pte)) || |
| 1323 | (write && !pte_write(huge_ptep_get(pte)))) { |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1324 | int ret; |
| 1325 | |
| 1326 | spin_unlock(&mm->page_table_lock); |
Adam Litke | 5b23dbe | 2007-11-14 16:59:33 -0800 | [diff] [blame] | 1327 | ret = hugetlb_fault(mm, vma, vaddr, write); |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1328 | spin_lock(&mm->page_table_lock); |
Adam Litke | a89182c | 2007-08-22 14:01:51 -0700 | [diff] [blame] | 1329 | if (!(ret & VM_FAULT_ERROR)) |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 1330 | continue; |
| 1331 | |
| 1332 | remainder = 0; |
| 1333 | if (!i) |
| 1334 | i = -EFAULT; |
| 1335 | break; |
| 1336 | } |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1337 | |
Chen, Kenneth W | d5d4b0a | 2006-03-22 00:09:03 -0800 | [diff] [blame] | 1338 | pfn_offset = (vaddr & ~HPAGE_MASK) >> PAGE_SHIFT; |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 1339 | page = pte_page(huge_ptep_get(pte)); |
Chen, Kenneth W | d5d4b0a | 2006-03-22 00:09:03 -0800 | [diff] [blame] | 1340 | same_page: |
Chen, Kenneth W | d669218 | 2006-03-31 02:29:57 -0800 | [diff] [blame] | 1341 | if (pages) { |
| 1342 | get_page(page); |
Chen, Kenneth W | d5d4b0a | 2006-03-22 00:09:03 -0800 | [diff] [blame] | 1343 | pages[i] = page + pfn_offset; |
Chen, Kenneth W | d669218 | 2006-03-31 02:29:57 -0800 | [diff] [blame] | 1344 | } |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1345 | |
| 1346 | if (vmas) |
| 1347 | vmas[i] = vma; |
| 1348 | |
| 1349 | vaddr += PAGE_SIZE; |
Chen, Kenneth W | d5d4b0a | 2006-03-22 00:09:03 -0800 | [diff] [blame] | 1350 | ++pfn_offset; |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1351 | --remainder; |
| 1352 | ++i; |
Chen, Kenneth W | d5d4b0a | 2006-03-22 00:09:03 -0800 | [diff] [blame] | 1353 | if (vaddr < vma->vm_end && remainder && |
| 1354 | pfn_offset < HPAGE_SIZE/PAGE_SIZE) { |
| 1355 | /* |
| 1356 | * We use pfn_offset to avoid touching the pageframes |
| 1357 | * of this compound page. |
| 1358 | */ |
| 1359 | goto same_page; |
| 1360 | } |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1361 | } |
Hugh Dickins | 1c59827 | 2005-10-19 21:23:43 -0700 | [diff] [blame] | 1362 | spin_unlock(&mm->page_table_lock); |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 1363 | *length = remainder; |
| 1364 | *position = vaddr; |
| 1365 | |
| 1366 | return i; |
| 1367 | } |
Zhang, Yanmin | 8f86059 | 2006-03-22 00:08:50 -0800 | [diff] [blame] | 1368 | |
| 1369 | void hugetlb_change_protection(struct vm_area_struct *vma, |
| 1370 | unsigned long address, unsigned long end, pgprot_t newprot) |
| 1371 | { |
| 1372 | struct mm_struct *mm = vma->vm_mm; |
| 1373 | unsigned long start = address; |
| 1374 | pte_t *ptep; |
| 1375 | pte_t pte; |
| 1376 | |
| 1377 | BUG_ON(address >= end); |
| 1378 | flush_cache_range(vma, address, end); |
| 1379 | |
Chen, Kenneth W | 39dde65 | 2006-12-06 20:32:03 -0800 | [diff] [blame] | 1380 | spin_lock(&vma->vm_file->f_mapping->i_mmap_lock); |
Zhang, Yanmin | 8f86059 | 2006-03-22 00:08:50 -0800 | [diff] [blame] | 1381 | spin_lock(&mm->page_table_lock); |
| 1382 | for (; address < end; address += HPAGE_SIZE) { |
| 1383 | ptep = huge_pte_offset(mm, address); |
| 1384 | if (!ptep) |
| 1385 | continue; |
Chen, Kenneth W | 39dde65 | 2006-12-06 20:32:03 -0800 | [diff] [blame] | 1386 | if (huge_pmd_unshare(mm, &address, ptep)) |
| 1387 | continue; |
Gerald Schaefer | 7f2e952 | 2008-04-28 02:13:29 -0700 | [diff] [blame] | 1388 | if (!huge_pte_none(huge_ptep_get(ptep))) { |
Zhang, Yanmin | 8f86059 | 2006-03-22 00:08:50 -0800 | [diff] [blame] | 1389 | pte = huge_ptep_get_and_clear(mm, address, ptep); |
| 1390 | pte = pte_mkhuge(pte_modify(pte, newprot)); |
| 1391 | set_huge_pte_at(mm, address, ptep, pte); |
Zhang, Yanmin | 8f86059 | 2006-03-22 00:08:50 -0800 | [diff] [blame] | 1392 | } |
| 1393 | } |
| 1394 | spin_unlock(&mm->page_table_lock); |
Chen, Kenneth W | 39dde65 | 2006-12-06 20:32:03 -0800 | [diff] [blame] | 1395 | spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock); |
Zhang, Yanmin | 8f86059 | 2006-03-22 00:08:50 -0800 | [diff] [blame] | 1396 | |
| 1397 | flush_tlb_range(vma, start, end); |
| 1398 | } |
| 1399 | |
Chen, Kenneth W | a43a8c3 | 2006-06-23 02:03:15 -0700 | [diff] [blame] | 1400 | struct file_region { |
| 1401 | struct list_head link; |
| 1402 | long from; |
| 1403 | long to; |
| 1404 | }; |
| 1405 | |
| 1406 | static long region_add(struct list_head *head, long f, long t) |
| 1407 | { |
| 1408 | struct file_region *rg, *nrg, *trg; |
| 1409 | |
| 1410 | /* Locate the region we are either in or before. */ |
| 1411 | list_for_each_entry(rg, head, link) |
| 1412 | if (f <= rg->to) |
| 1413 | break; |
| 1414 | |
| 1415 | /* Round our left edge to the current segment if it encloses us. */ |
| 1416 | if (f > rg->from) |
| 1417 | f = rg->from; |
| 1418 | |
| 1419 | /* Check for and consume any regions we now overlap with. */ |
| 1420 | nrg = rg; |
| 1421 | list_for_each_entry_safe(rg, trg, rg->link.prev, link) { |
| 1422 | if (&rg->link == head) |
| 1423 | break; |
| 1424 | if (rg->from > t) |
| 1425 | break; |
| 1426 | |
| 1427 | /* If this area reaches higher then extend our area to |
| 1428 | * include it completely. If this is not the first area |
| 1429 | * which we intend to reuse, free it. */ |
| 1430 | if (rg->to > t) |
| 1431 | t = rg->to; |
| 1432 | if (rg != nrg) { |
| 1433 | list_del(&rg->link); |
| 1434 | kfree(rg); |
| 1435 | } |
| 1436 | } |
| 1437 | nrg->from = f; |
| 1438 | nrg->to = t; |
| 1439 | return 0; |
| 1440 | } |
| 1441 | |
| 1442 | static long region_chg(struct list_head *head, long f, long t) |
| 1443 | { |
| 1444 | struct file_region *rg, *nrg; |
| 1445 | long chg = 0; |
| 1446 | |
| 1447 | /* Locate the region we are before or in. */ |
| 1448 | list_for_each_entry(rg, head, link) |
| 1449 | if (f <= rg->to) |
| 1450 | break; |
| 1451 | |
| 1452 | /* If we are below the current region then a new region is required. |
| 1453 | * Subtle, allocate a new region at the position but make it zero |
Simon Arlott | 183ff22 | 2007-10-20 01:27:18 +0200 | [diff] [blame] | 1454 | * size such that we can guarantee to record the reservation. */ |
Chen, Kenneth W | a43a8c3 | 2006-06-23 02:03:15 -0700 | [diff] [blame] | 1455 | if (&rg->link == head || t < rg->from) { |
| 1456 | nrg = kmalloc(sizeof(*nrg), GFP_KERNEL); |
Stephen Hemminger | c80544d | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 1457 | if (!nrg) |
Chen, Kenneth W | a43a8c3 | 2006-06-23 02:03:15 -0700 | [diff] [blame] | 1458 | return -ENOMEM; |
| 1459 | nrg->from = f; |
| 1460 | nrg->to = f; |
| 1461 | INIT_LIST_HEAD(&nrg->link); |
| 1462 | list_add(&nrg->link, rg->link.prev); |
| 1463 | |
| 1464 | return t - f; |
| 1465 | } |
| 1466 | |
| 1467 | /* Round our left edge to the current segment if it encloses us. */ |
| 1468 | if (f > rg->from) |
| 1469 | f = rg->from; |
| 1470 | chg = t - f; |
| 1471 | |
| 1472 | /* Check for and consume any regions we now overlap with. */ |
| 1473 | list_for_each_entry(rg, rg->link.prev, link) { |
| 1474 | if (&rg->link == head) |
| 1475 | break; |
| 1476 | if (rg->from > t) |
| 1477 | return chg; |
| 1478 | |
| 1479 | /* We overlap with this area, if it extends futher than |
| 1480 | * us then we must extend ourselves. Account for its |
| 1481 | * existing reservation. */ |
| 1482 | if (rg->to > t) { |
| 1483 | chg += rg->to - t; |
| 1484 | t = rg->to; |
| 1485 | } |
| 1486 | chg -= rg->to - rg->from; |
| 1487 | } |
| 1488 | return chg; |
| 1489 | } |
| 1490 | |
| 1491 | static long region_truncate(struct list_head *head, long end) |
| 1492 | { |
| 1493 | struct file_region *rg, *trg; |
| 1494 | long chg = 0; |
| 1495 | |
| 1496 | /* Locate the region we are either in or before. */ |
| 1497 | list_for_each_entry(rg, head, link) |
| 1498 | if (end <= rg->to) |
| 1499 | break; |
| 1500 | if (&rg->link == head) |
| 1501 | return 0; |
| 1502 | |
| 1503 | /* If we are in the middle of a region then adjust it. */ |
| 1504 | if (end > rg->from) { |
| 1505 | chg = rg->to - end; |
| 1506 | rg->to = end; |
| 1507 | rg = list_entry(rg->link.next, typeof(*rg), link); |
| 1508 | } |
| 1509 | |
| 1510 | /* Drop any remaining regions. */ |
| 1511 | list_for_each_entry_safe(rg, trg, rg->link.prev, link) { |
| 1512 | if (&rg->link == head) |
| 1513 | break; |
| 1514 | chg += rg->to - rg->from; |
| 1515 | list_del(&rg->link); |
| 1516 | kfree(rg); |
| 1517 | } |
| 1518 | return chg; |
| 1519 | } |
| 1520 | |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 1521 | int hugetlb_reserve_pages(struct inode *inode, |
| 1522 | long from, long to, |
| 1523 | struct vm_area_struct *vma) |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 1524 | { |
| 1525 | long ret, chg; |
| 1526 | |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 1527 | /* |
| 1528 | * Shared mappings base their reservation on the number of pages that |
| 1529 | * are already allocated on behalf of the file. Private mappings need |
| 1530 | * to reserve the full area even if read-only as mprotect() may be |
| 1531 | * called to make the mapping read-write. Assume !vma is a shm mapping |
| 1532 | */ |
| 1533 | if (!vma || vma->vm_flags & VM_SHARED) |
| 1534 | chg = region_chg(&inode->i_mapping->private_list, from, to); |
| 1535 | else { |
| 1536 | chg = to - from; |
| 1537 | set_vma_resv_huge_pages(vma, chg); |
Mel Gorman | 04f2cbe | 2008-07-23 21:27:25 -0700 | [diff] [blame^] | 1538 | set_vma_resv_flags(vma, HPAGE_RESV_OWNER); |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 1539 | } |
| 1540 | |
Adam Litke | e4e574b | 2007-10-16 01:26:19 -0700 | [diff] [blame] | 1541 | if (chg < 0) |
| 1542 | return chg; |
Ken Chen | 8a63011 | 2007-05-09 02:33:34 -0700 | [diff] [blame] | 1543 | |
Adam Litke | 90d8b7e | 2007-11-14 16:59:42 -0800 | [diff] [blame] | 1544 | if (hugetlb_get_quota(inode->i_mapping, chg)) |
| 1545 | return -ENOSPC; |
Chen, Kenneth W | a43a8c3 | 2006-06-23 02:03:15 -0700 | [diff] [blame] | 1546 | ret = hugetlb_acct_memory(chg); |
Ken Chen | 68842c9 | 2008-01-14 00:55:19 -0800 | [diff] [blame] | 1547 | if (ret < 0) { |
| 1548 | hugetlb_put_quota(inode->i_mapping, chg); |
Chen, Kenneth W | a43a8c3 | 2006-06-23 02:03:15 -0700 | [diff] [blame] | 1549 | return ret; |
Ken Chen | 68842c9 | 2008-01-14 00:55:19 -0800 | [diff] [blame] | 1550 | } |
Mel Gorman | a1e7877 | 2008-07-23 21:27:23 -0700 | [diff] [blame] | 1551 | if (!vma || vma->vm_flags & VM_SHARED) |
| 1552 | region_add(&inode->i_mapping->private_list, from, to); |
Chen, Kenneth W | a43a8c3 | 2006-06-23 02:03:15 -0700 | [diff] [blame] | 1553 | return 0; |
| 1554 | } |
| 1555 | |
| 1556 | void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed) |
| 1557 | { |
| 1558 | long chg = region_truncate(&inode->i_mapping->private_list, offset); |
Ken Chen | 45c682a | 2007-11-14 16:59:44 -0800 | [diff] [blame] | 1559 | |
| 1560 | spin_lock(&inode->i_lock); |
| 1561 | inode->i_blocks -= BLOCKS_PER_HUGEPAGE * freed; |
| 1562 | spin_unlock(&inode->i_lock); |
| 1563 | |
Adam Litke | 90d8b7e | 2007-11-14 16:59:42 -0800 | [diff] [blame] | 1564 | hugetlb_put_quota(inode->i_mapping, (chg - freed)); |
| 1565 | hugetlb_acct_memory(-(chg - freed)); |
Chen, Kenneth W | a43a8c3 | 2006-06-23 02:03:15 -0700 | [diff] [blame] | 1566 | } |