Commit | Line | Data |
---|---|---|
1da177e4 LT |
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> | |
1da177e4 LT |
10 | #include <linux/sysctl.h> |
11 | #include <linux/highmem.h> | |
12 | #include <linux/nodemask.h> | |
63551ae0 | 13 | #include <linux/pagemap.h> |
5da7ca86 | 14 | #include <linux/mempolicy.h> |
aea47ff3 | 15 | #include <linux/cpuset.h> |
3935baa9 | 16 | #include <linux/mutex.h> |
5da7ca86 | 17 | |
63551ae0 DG |
18 | #include <asm/page.h> |
19 | #include <asm/pgtable.h> | |
20 | ||
21 | #include <linux/hugetlb.h> | |
7835e98b | 22 | #include "internal.h" |
1da177e4 LT |
23 | |
24 | const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL; | |
a43a8c39 | 25 | static unsigned long nr_huge_pages, free_huge_pages, resv_huge_pages; |
7893d1d5 | 26 | static unsigned long surplus_huge_pages; |
064d9efe | 27 | static unsigned long nr_overcommit_huge_pages; |
1da177e4 | 28 | unsigned long max_huge_pages; |
064d9efe | 29 | unsigned long sysctl_overcommit_huge_pages; |
1da177e4 LT |
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]; | |
7893d1d5 | 33 | static unsigned int surplus_huge_pages_node[MAX_NUMNODES]; |
396faf03 MG |
34 | static gfp_t htlb_alloc_mask = GFP_HIGHUSER; |
35 | unsigned long hugepages_treat_as_movable; | |
63b4613c | 36 | static int hugetlb_next_nid; |
396faf03 | 37 | |
3935baa9 DG |
38 | /* |
39 | * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages | |
40 | */ | |
41 | static DEFINE_SPINLOCK(hugetlb_lock); | |
0bd0f9fb | 42 | |
79ac6ba4 DG |
43 | static void clear_huge_page(struct page *page, unsigned long addr) |
44 | { | |
45 | int i; | |
46 | ||
47 | might_sleep(); | |
48 | for (i = 0; i < (HPAGE_SIZE/PAGE_SIZE); i++) { | |
49 | cond_resched(); | |
281e0e3b | 50 | clear_user_highpage(page + i, addr + i * PAGE_SIZE); |
79ac6ba4 DG |
51 | } |
52 | } | |
53 | ||
54 | static void copy_huge_page(struct page *dst, struct page *src, | |
9de455b2 | 55 | unsigned long addr, struct vm_area_struct *vma) |
79ac6ba4 DG |
56 | { |
57 | int i; | |
58 | ||
59 | might_sleep(); | |
60 | for (i = 0; i < HPAGE_SIZE/PAGE_SIZE; i++) { | |
61 | cond_resched(); | |
9de455b2 | 62 | copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma); |
79ac6ba4 DG |
63 | } |
64 | } | |
65 | ||
1da177e4 LT |
66 | static void enqueue_huge_page(struct page *page) |
67 | { | |
68 | int nid = page_to_nid(page); | |
69 | list_add(&page->lru, &hugepage_freelists[nid]); | |
70 | free_huge_pages++; | |
71 | free_huge_pages_node[nid]++; | |
72 | } | |
73 | ||
348e1e04 NA |
74 | static struct page *dequeue_huge_page(void) |
75 | { | |
76 | int nid; | |
77 | struct page *page = NULL; | |
78 | ||
79 | for (nid = 0; nid < MAX_NUMNODES; ++nid) { | |
80 | if (!list_empty(&hugepage_freelists[nid])) { | |
81 | page = list_entry(hugepage_freelists[nid].next, | |
82 | struct page, lru); | |
83 | list_del(&page->lru); | |
84 | free_huge_pages--; | |
85 | free_huge_pages_node[nid]--; | |
86 | break; | |
87 | } | |
88 | } | |
89 | return page; | |
90 | } | |
91 | ||
92 | static struct page *dequeue_huge_page_vma(struct vm_area_struct *vma, | |
5da7ca86 | 93 | unsigned long address) |
1da177e4 | 94 | { |
31a5c6e4 | 95 | int nid; |
1da177e4 | 96 | struct page *page = NULL; |
480eccf9 | 97 | struct mempolicy *mpol; |
396faf03 | 98 | struct zonelist *zonelist = huge_zonelist(vma, address, |
480eccf9 | 99 | htlb_alloc_mask, &mpol); |
96df9333 | 100 | struct zone **z; |
1da177e4 | 101 | |
96df9333 | 102 | for (z = zonelist->zones; *z; z++) { |
89fa3024 | 103 | nid = zone_to_nid(*z); |
396faf03 | 104 | if (cpuset_zone_allowed_softwall(*z, htlb_alloc_mask) && |
3abf7afd AM |
105 | !list_empty(&hugepage_freelists[nid])) { |
106 | page = list_entry(hugepage_freelists[nid].next, | |
107 | struct page, lru); | |
108 | list_del(&page->lru); | |
109 | free_huge_pages--; | |
110 | free_huge_pages_node[nid]--; | |
e4e574b7 AL |
111 | if (vma && vma->vm_flags & VM_MAYSHARE) |
112 | resv_huge_pages--; | |
5ab3ee7b | 113 | break; |
3abf7afd | 114 | } |
1da177e4 | 115 | } |
480eccf9 | 116 | mpol_free(mpol); /* unref if mpol !NULL */ |
1da177e4 LT |
117 | return page; |
118 | } | |
119 | ||
6af2acb6 AL |
120 | static void update_and_free_page(struct page *page) |
121 | { | |
122 | int i; | |
123 | nr_huge_pages--; | |
124 | nr_huge_pages_node[page_to_nid(page)]--; | |
125 | for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++) { | |
126 | page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced | | |
127 | 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved | | |
128 | 1 << PG_private | 1<< PG_writeback); | |
129 | } | |
130 | set_compound_page_dtor(page, NULL); | |
131 | set_page_refcounted(page); | |
132 | __free_pages(page, HUGETLB_PAGE_ORDER); | |
133 | } | |
134 | ||
27a85ef1 DG |
135 | static void free_huge_page(struct page *page) |
136 | { | |
7893d1d5 | 137 | int nid = page_to_nid(page); |
c79fb75e | 138 | struct address_space *mapping; |
27a85ef1 | 139 | |
c79fb75e | 140 | mapping = (struct address_space *) page_private(page); |
e5df70ab | 141 | set_page_private(page, 0); |
7893d1d5 | 142 | BUG_ON(page_count(page)); |
27a85ef1 DG |
143 | INIT_LIST_HEAD(&page->lru); |
144 | ||
145 | spin_lock(&hugetlb_lock); | |
7893d1d5 AL |
146 | if (surplus_huge_pages_node[nid]) { |
147 | update_and_free_page(page); | |
148 | surplus_huge_pages--; | |
149 | surplus_huge_pages_node[nid]--; | |
150 | } else { | |
151 | enqueue_huge_page(page); | |
152 | } | |
27a85ef1 | 153 | spin_unlock(&hugetlb_lock); |
c79fb75e | 154 | if (mapping) |
9a119c05 | 155 | hugetlb_put_quota(mapping, 1); |
27a85ef1 DG |
156 | } |
157 | ||
7893d1d5 AL |
158 | /* |
159 | * Increment or decrement surplus_huge_pages. Keep node-specific counters | |
160 | * balanced by operating on them in a round-robin fashion. | |
161 | * Returns 1 if an adjustment was made. | |
162 | */ | |
163 | static int adjust_pool_surplus(int delta) | |
164 | { | |
165 | static int prev_nid; | |
166 | int nid = prev_nid; | |
167 | int ret = 0; | |
168 | ||
169 | VM_BUG_ON(delta != -1 && delta != 1); | |
170 | do { | |
171 | nid = next_node(nid, node_online_map); | |
172 | if (nid == MAX_NUMNODES) | |
173 | nid = first_node(node_online_map); | |
174 | ||
175 | /* To shrink on this node, there must be a surplus page */ | |
176 | if (delta < 0 && !surplus_huge_pages_node[nid]) | |
177 | continue; | |
178 | /* Surplus cannot exceed the total number of pages */ | |
179 | if (delta > 0 && surplus_huge_pages_node[nid] >= | |
180 | nr_huge_pages_node[nid]) | |
181 | continue; | |
182 | ||
183 | surplus_huge_pages += delta; | |
184 | surplus_huge_pages_node[nid] += delta; | |
185 | ret = 1; | |
186 | break; | |
187 | } while (nid != prev_nid); | |
188 | ||
189 | prev_nid = nid; | |
190 | return ret; | |
191 | } | |
192 | ||
63b4613c | 193 | static struct page *alloc_fresh_huge_page_node(int nid) |
1da177e4 | 194 | { |
1da177e4 | 195 | struct page *page; |
f96efd58 | 196 | |
63b4613c NA |
197 | page = alloc_pages_node(nid, |
198 | htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|__GFP_NOWARN, | |
199 | HUGETLB_PAGE_ORDER); | |
1da177e4 | 200 | if (page) { |
33f2ef89 | 201 | set_compound_page_dtor(page, free_huge_page); |
0bd0f9fb | 202 | spin_lock(&hugetlb_lock); |
1da177e4 | 203 | nr_huge_pages++; |
63b4613c | 204 | nr_huge_pages_node[nid]++; |
0bd0f9fb | 205 | spin_unlock(&hugetlb_lock); |
a482289d | 206 | put_page(page); /* free it into the hugepage allocator */ |
1da177e4 | 207 | } |
63b4613c NA |
208 | |
209 | return page; | |
210 | } | |
211 | ||
212 | static int alloc_fresh_huge_page(void) | |
213 | { | |
214 | struct page *page; | |
215 | int start_nid; | |
216 | int next_nid; | |
217 | int ret = 0; | |
218 | ||
219 | start_nid = hugetlb_next_nid; | |
220 | ||
221 | do { | |
222 | page = alloc_fresh_huge_page_node(hugetlb_next_nid); | |
223 | if (page) | |
224 | ret = 1; | |
225 | /* | |
226 | * Use a helper variable to find the next node and then | |
227 | * copy it back to hugetlb_next_nid afterwards: | |
228 | * otherwise there's a window in which a racer might | |
229 | * pass invalid nid MAX_NUMNODES to alloc_pages_node. | |
230 | * But we don't need to use a spin_lock here: it really | |
231 | * doesn't matter if occasionally a racer chooses the | |
232 | * same nid as we do. Move nid forward in the mask even | |
233 | * if we just successfully allocated a hugepage so that | |
234 | * the next caller gets hugepages on the next node. | |
235 | */ | |
236 | next_nid = next_node(hugetlb_next_nid, node_online_map); | |
237 | if (next_nid == MAX_NUMNODES) | |
238 | next_nid = first_node(node_online_map); | |
239 | hugetlb_next_nid = next_nid; | |
240 | } while (!page && hugetlb_next_nid != start_nid); | |
241 | ||
242 | return ret; | |
1da177e4 LT |
243 | } |
244 | ||
7893d1d5 AL |
245 | static struct page *alloc_buddy_huge_page(struct vm_area_struct *vma, |
246 | unsigned long address) | |
247 | { | |
248 | struct page *page; | |
d1c3fb1f | 249 | unsigned int nid; |
7893d1d5 | 250 | |
d1c3fb1f NA |
251 | /* |
252 | * Assume we will successfully allocate the surplus page to | |
253 | * prevent racing processes from causing the surplus to exceed | |
254 | * overcommit | |
255 | * | |
256 | * This however introduces a different race, where a process B | |
257 | * tries to grow the static hugepage pool while alloc_pages() is | |
258 | * called by process A. B will only examine the per-node | |
259 | * counters in determining if surplus huge pages can be | |
260 | * converted to normal huge pages in adjust_pool_surplus(). A | |
261 | * won't be able to increment the per-node counter, until the | |
262 | * lock is dropped by B, but B doesn't drop hugetlb_lock until | |
263 | * no more huge pages can be converted from surplus to normal | |
264 | * state (and doesn't try to convert again). Thus, we have a | |
265 | * case where a surplus huge page exists, the pool is grown, and | |
266 | * the surplus huge page still exists after, even though it | |
267 | * should just have been converted to a normal huge page. This | |
268 | * does not leak memory, though, as the hugepage will be freed | |
269 | * once it is out of use. It also does not allow the counters to | |
270 | * go out of whack in adjust_pool_surplus() as we don't modify | |
271 | * the node values until we've gotten the hugepage and only the | |
272 | * per-node value is checked there. | |
273 | */ | |
274 | spin_lock(&hugetlb_lock); | |
275 | if (surplus_huge_pages >= nr_overcommit_huge_pages) { | |
276 | spin_unlock(&hugetlb_lock); | |
277 | return NULL; | |
278 | } else { | |
279 | nr_huge_pages++; | |
280 | surplus_huge_pages++; | |
281 | } | |
282 | spin_unlock(&hugetlb_lock); | |
283 | ||
7893d1d5 AL |
284 | page = alloc_pages(htlb_alloc_mask|__GFP_COMP|__GFP_NOWARN, |
285 | HUGETLB_PAGE_ORDER); | |
d1c3fb1f NA |
286 | |
287 | spin_lock(&hugetlb_lock); | |
7893d1d5 | 288 | if (page) { |
d1c3fb1f | 289 | nid = page_to_nid(page); |
7893d1d5 | 290 | set_compound_page_dtor(page, free_huge_page); |
d1c3fb1f NA |
291 | /* |
292 | * We incremented the global counters already | |
293 | */ | |
294 | nr_huge_pages_node[nid]++; | |
295 | surplus_huge_pages_node[nid]++; | |
296 | } else { | |
297 | nr_huge_pages--; | |
298 | surplus_huge_pages--; | |
7893d1d5 | 299 | } |
d1c3fb1f | 300 | spin_unlock(&hugetlb_lock); |
7893d1d5 AL |
301 | |
302 | return page; | |
303 | } | |
304 | ||
e4e574b7 AL |
305 | /* |
306 | * Increase the hugetlb pool such that it can accomodate a reservation | |
307 | * of size 'delta'. | |
308 | */ | |
309 | static int gather_surplus_pages(int delta) | |
310 | { | |
311 | struct list_head surplus_list; | |
312 | struct page *page, *tmp; | |
313 | int ret, i; | |
314 | int needed, allocated; | |
315 | ||
316 | needed = (resv_huge_pages + delta) - free_huge_pages; | |
ac09b3a1 AL |
317 | if (needed <= 0) { |
318 | resv_huge_pages += delta; | |
e4e574b7 | 319 | return 0; |
ac09b3a1 | 320 | } |
e4e574b7 AL |
321 | |
322 | allocated = 0; | |
323 | INIT_LIST_HEAD(&surplus_list); | |
324 | ||
325 | ret = -ENOMEM; | |
326 | retry: | |
327 | spin_unlock(&hugetlb_lock); | |
328 | for (i = 0; i < needed; i++) { | |
329 | page = alloc_buddy_huge_page(NULL, 0); | |
330 | if (!page) { | |
331 | /* | |
332 | * We were not able to allocate enough pages to | |
333 | * satisfy the entire reservation so we free what | |
334 | * we've allocated so far. | |
335 | */ | |
336 | spin_lock(&hugetlb_lock); | |
337 | needed = 0; | |
338 | goto free; | |
339 | } | |
340 | ||
341 | list_add(&page->lru, &surplus_list); | |
342 | } | |
343 | allocated += needed; | |
344 | ||
345 | /* | |
346 | * After retaking hugetlb_lock, we need to recalculate 'needed' | |
347 | * because either resv_huge_pages or free_huge_pages may have changed. | |
348 | */ | |
349 | spin_lock(&hugetlb_lock); | |
350 | needed = (resv_huge_pages + delta) - (free_huge_pages + allocated); | |
351 | if (needed > 0) | |
352 | goto retry; | |
353 | ||
354 | /* | |
355 | * The surplus_list now contains _at_least_ the number of extra pages | |
356 | * needed to accomodate the reservation. Add the appropriate number | |
357 | * of pages to the hugetlb pool and free the extras back to the buddy | |
ac09b3a1 AL |
358 | * allocator. Commit the entire reservation here to prevent another |
359 | * process from stealing the pages as they are added to the pool but | |
360 | * before they are reserved. | |
e4e574b7 AL |
361 | */ |
362 | needed += allocated; | |
ac09b3a1 | 363 | resv_huge_pages += delta; |
e4e574b7 AL |
364 | ret = 0; |
365 | free: | |
366 | list_for_each_entry_safe(page, tmp, &surplus_list, lru) { | |
367 | list_del(&page->lru); | |
368 | if ((--needed) >= 0) | |
369 | enqueue_huge_page(page); | |
af767cbd AL |
370 | else { |
371 | /* | |
372 | * Decrement the refcount and free the page using its | |
373 | * destructor. This must be done with hugetlb_lock | |
374 | * unlocked which is safe because free_huge_page takes | |
375 | * hugetlb_lock before deciding how to free the page. | |
376 | */ | |
377 | spin_unlock(&hugetlb_lock); | |
378 | put_page(page); | |
379 | spin_lock(&hugetlb_lock); | |
380 | } | |
e4e574b7 AL |
381 | } |
382 | ||
383 | return ret; | |
384 | } | |
385 | ||
386 | /* | |
387 | * When releasing a hugetlb pool reservation, any surplus pages that were | |
388 | * allocated to satisfy the reservation must be explicitly freed if they were | |
389 | * never used. | |
390 | */ | |
8cde045c | 391 | static void return_unused_surplus_pages(unsigned long unused_resv_pages) |
e4e574b7 AL |
392 | { |
393 | static int nid = -1; | |
394 | struct page *page; | |
395 | unsigned long nr_pages; | |
396 | ||
ac09b3a1 AL |
397 | /* Uncommit the reservation */ |
398 | resv_huge_pages -= unused_resv_pages; | |
399 | ||
e4e574b7 AL |
400 | nr_pages = min(unused_resv_pages, surplus_huge_pages); |
401 | ||
402 | while (nr_pages) { | |
403 | nid = next_node(nid, node_online_map); | |
404 | if (nid == MAX_NUMNODES) | |
405 | nid = first_node(node_online_map); | |
406 | ||
407 | if (!surplus_huge_pages_node[nid]) | |
408 | continue; | |
409 | ||
410 | if (!list_empty(&hugepage_freelists[nid])) { | |
411 | page = list_entry(hugepage_freelists[nid].next, | |
412 | struct page, lru); | |
413 | list_del(&page->lru); | |
414 | update_and_free_page(page); | |
415 | free_huge_pages--; | |
416 | free_huge_pages_node[nid]--; | |
417 | surplus_huge_pages--; | |
418 | surplus_huge_pages_node[nid]--; | |
419 | nr_pages--; | |
420 | } | |
421 | } | |
422 | } | |
423 | ||
348ea204 AL |
424 | |
425 | static struct page *alloc_huge_page_shared(struct vm_area_struct *vma, | |
426 | unsigned long addr) | |
1da177e4 | 427 | { |
348ea204 | 428 | struct page *page; |
1da177e4 LT |
429 | |
430 | spin_lock(&hugetlb_lock); | |
348e1e04 | 431 | page = dequeue_huge_page_vma(vma, addr); |
1da177e4 | 432 | spin_unlock(&hugetlb_lock); |
90d8b7e6 | 433 | return page ? page : ERR_PTR(-VM_FAULT_OOM); |
348ea204 | 434 | } |
b45b5bd6 | 435 | |
348ea204 AL |
436 | static struct page *alloc_huge_page_private(struct vm_area_struct *vma, |
437 | unsigned long addr) | |
438 | { | |
439 | struct page *page = NULL; | |
7893d1d5 | 440 | |
90d8b7e6 AL |
441 | if (hugetlb_get_quota(vma->vm_file->f_mapping, 1)) |
442 | return ERR_PTR(-VM_FAULT_SIGBUS); | |
443 | ||
348ea204 AL |
444 | spin_lock(&hugetlb_lock); |
445 | if (free_huge_pages > resv_huge_pages) | |
348e1e04 | 446 | page = dequeue_huge_page_vma(vma, addr); |
348ea204 | 447 | spin_unlock(&hugetlb_lock); |
68842c9b | 448 | if (!page) { |
7893d1d5 | 449 | page = alloc_buddy_huge_page(vma, addr); |
68842c9b KC |
450 | if (!page) { |
451 | hugetlb_put_quota(vma->vm_file->f_mapping, 1); | |
452 | return ERR_PTR(-VM_FAULT_OOM); | |
453 | } | |
454 | } | |
455 | return page; | |
348ea204 AL |
456 | } |
457 | ||
458 | static struct page *alloc_huge_page(struct vm_area_struct *vma, | |
459 | unsigned long addr) | |
460 | { | |
461 | struct page *page; | |
2fc39cec AL |
462 | struct address_space *mapping = vma->vm_file->f_mapping; |
463 | ||
348ea204 AL |
464 | if (vma->vm_flags & VM_MAYSHARE) |
465 | page = alloc_huge_page_shared(vma, addr); | |
466 | else | |
467 | page = alloc_huge_page_private(vma, addr); | |
90d8b7e6 AL |
468 | |
469 | if (!IS_ERR(page)) { | |
348ea204 | 470 | set_page_refcounted(page); |
2fc39cec | 471 | set_page_private(page, (unsigned long) mapping); |
90d8b7e6 AL |
472 | } |
473 | return page; | |
b45b5bd6 DG |
474 | } |
475 | ||
1da177e4 LT |
476 | static int __init hugetlb_init(void) |
477 | { | |
478 | unsigned long i; | |
1da177e4 | 479 | |
3c726f8d BH |
480 | if (HPAGE_SHIFT == 0) |
481 | return 0; | |
482 | ||
1da177e4 LT |
483 | for (i = 0; i < MAX_NUMNODES; ++i) |
484 | INIT_LIST_HEAD(&hugepage_freelists[i]); | |
485 | ||
63b4613c NA |
486 | hugetlb_next_nid = first_node(node_online_map); |
487 | ||
1da177e4 | 488 | for (i = 0; i < max_huge_pages; ++i) { |
a482289d | 489 | if (!alloc_fresh_huge_page()) |
1da177e4 | 490 | break; |
1da177e4 LT |
491 | } |
492 | max_huge_pages = free_huge_pages = nr_huge_pages = i; | |
493 | printk("Total HugeTLB memory allocated, %ld\n", free_huge_pages); | |
494 | return 0; | |
495 | } | |
496 | module_init(hugetlb_init); | |
497 | ||
498 | static int __init hugetlb_setup(char *s) | |
499 | { | |
500 | if (sscanf(s, "%lu", &max_huge_pages) <= 0) | |
501 | max_huge_pages = 0; | |
502 | return 1; | |
503 | } | |
504 | __setup("hugepages=", hugetlb_setup); | |
505 | ||
8a630112 KC |
506 | static unsigned int cpuset_mems_nr(unsigned int *array) |
507 | { | |
508 | int node; | |
509 | unsigned int nr = 0; | |
510 | ||
511 | for_each_node_mask(node, cpuset_current_mems_allowed) | |
512 | nr += array[node]; | |
513 | ||
514 | return nr; | |
515 | } | |
516 | ||
1da177e4 | 517 | #ifdef CONFIG_SYSCTL |
1da177e4 LT |
518 | #ifdef CONFIG_HIGHMEM |
519 | static void try_to_free_low(unsigned long count) | |
520 | { | |
4415cc8d CL |
521 | int i; |
522 | ||
1da177e4 LT |
523 | for (i = 0; i < MAX_NUMNODES; ++i) { |
524 | struct page *page, *next; | |
525 | list_for_each_entry_safe(page, next, &hugepage_freelists[i], lru) { | |
6b0c880d AL |
526 | if (count >= nr_huge_pages) |
527 | return; | |
1da177e4 LT |
528 | if (PageHighMem(page)) |
529 | continue; | |
530 | list_del(&page->lru); | |
531 | update_and_free_page(page); | |
1da177e4 | 532 | free_huge_pages--; |
4415cc8d | 533 | free_huge_pages_node[page_to_nid(page)]--; |
1da177e4 LT |
534 | } |
535 | } | |
536 | } | |
537 | #else | |
538 | static inline void try_to_free_low(unsigned long count) | |
539 | { | |
540 | } | |
541 | #endif | |
542 | ||
7893d1d5 | 543 | #define persistent_huge_pages (nr_huge_pages - surplus_huge_pages) |
1da177e4 LT |
544 | static unsigned long set_max_huge_pages(unsigned long count) |
545 | { | |
7893d1d5 | 546 | unsigned long min_count, ret; |
1da177e4 | 547 | |
7893d1d5 AL |
548 | /* |
549 | * Increase the pool size | |
550 | * First take pages out of surplus state. Then make up the | |
551 | * remaining difference by allocating fresh huge pages. | |
d1c3fb1f NA |
552 | * |
553 | * We might race with alloc_buddy_huge_page() here and be unable | |
554 | * to convert a surplus huge page to a normal huge page. That is | |
555 | * not critical, though, it just means the overall size of the | |
556 | * pool might be one hugepage larger than it needs to be, but | |
557 | * within all the constraints specified by the sysctls. | |
7893d1d5 | 558 | */ |
1da177e4 | 559 | spin_lock(&hugetlb_lock); |
7893d1d5 AL |
560 | while (surplus_huge_pages && count > persistent_huge_pages) { |
561 | if (!adjust_pool_surplus(-1)) | |
562 | break; | |
563 | } | |
564 | ||
565 | while (count > persistent_huge_pages) { | |
566 | int ret; | |
567 | /* | |
568 | * If this allocation races such that we no longer need the | |
569 | * page, free_huge_page will handle it by freeing the page | |
570 | * and reducing the surplus. | |
571 | */ | |
572 | spin_unlock(&hugetlb_lock); | |
573 | ret = alloc_fresh_huge_page(); | |
574 | spin_lock(&hugetlb_lock); | |
575 | if (!ret) | |
576 | goto out; | |
577 | ||
578 | } | |
7893d1d5 AL |
579 | |
580 | /* | |
581 | * Decrease the pool size | |
582 | * First return free pages to the buddy allocator (being careful | |
583 | * to keep enough around to satisfy reservations). Then place | |
584 | * pages into surplus state as needed so the pool will shrink | |
585 | * to the desired size as pages become free. | |
d1c3fb1f NA |
586 | * |
587 | * By placing pages into the surplus state independent of the | |
588 | * overcommit value, we are allowing the surplus pool size to | |
589 | * exceed overcommit. There are few sane options here. Since | |
590 | * alloc_buddy_huge_page() is checking the global counter, | |
591 | * though, we'll note that we're not allowed to exceed surplus | |
592 | * and won't grow the pool anywhere else. Not until one of the | |
593 | * sysctls are changed, or the surplus pages go out of use. | |
7893d1d5 | 594 | */ |
6b0c880d AL |
595 | min_count = resv_huge_pages + nr_huge_pages - free_huge_pages; |
596 | min_count = max(count, min_count); | |
7893d1d5 AL |
597 | try_to_free_low(min_count); |
598 | while (min_count < persistent_huge_pages) { | |
348e1e04 | 599 | struct page *page = dequeue_huge_page(); |
1da177e4 LT |
600 | if (!page) |
601 | break; | |
602 | update_and_free_page(page); | |
603 | } | |
7893d1d5 AL |
604 | while (count < persistent_huge_pages) { |
605 | if (!adjust_pool_surplus(1)) | |
606 | break; | |
607 | } | |
608 | out: | |
609 | ret = persistent_huge_pages; | |
1da177e4 | 610 | spin_unlock(&hugetlb_lock); |
7893d1d5 | 611 | return ret; |
1da177e4 LT |
612 | } |
613 | ||
614 | int hugetlb_sysctl_handler(struct ctl_table *table, int write, | |
615 | struct file *file, void __user *buffer, | |
616 | size_t *length, loff_t *ppos) | |
617 | { | |
618 | proc_doulongvec_minmax(table, write, file, buffer, length, ppos); | |
619 | max_huge_pages = set_max_huge_pages(max_huge_pages); | |
620 | return 0; | |
621 | } | |
396faf03 MG |
622 | |
623 | int hugetlb_treat_movable_handler(struct ctl_table *table, int write, | |
624 | struct file *file, void __user *buffer, | |
625 | size_t *length, loff_t *ppos) | |
626 | { | |
627 | proc_dointvec(table, write, file, buffer, length, ppos); | |
628 | if (hugepages_treat_as_movable) | |
629 | htlb_alloc_mask = GFP_HIGHUSER_MOVABLE; | |
630 | else | |
631 | htlb_alloc_mask = GFP_HIGHUSER; | |
632 | return 0; | |
633 | } | |
634 | ||
a3d0c6aa NA |
635 | int hugetlb_overcommit_handler(struct ctl_table *table, int write, |
636 | struct file *file, void __user *buffer, | |
637 | size_t *length, loff_t *ppos) | |
638 | { | |
a3d0c6aa | 639 | proc_doulongvec_minmax(table, write, file, buffer, length, ppos); |
064d9efe NA |
640 | spin_lock(&hugetlb_lock); |
641 | nr_overcommit_huge_pages = sysctl_overcommit_huge_pages; | |
a3d0c6aa NA |
642 | spin_unlock(&hugetlb_lock); |
643 | return 0; | |
644 | } | |
645 | ||
1da177e4 LT |
646 | #endif /* CONFIG_SYSCTL */ |
647 | ||
648 | int hugetlb_report_meminfo(char *buf) | |
649 | { | |
650 | return sprintf(buf, | |
651 | "HugePages_Total: %5lu\n" | |
652 | "HugePages_Free: %5lu\n" | |
a43a8c39 | 653 | "HugePages_Rsvd: %5lu\n" |
7893d1d5 | 654 | "HugePages_Surp: %5lu\n" |
1da177e4 LT |
655 | "Hugepagesize: %5lu kB\n", |
656 | nr_huge_pages, | |
657 | free_huge_pages, | |
a43a8c39 | 658 | resv_huge_pages, |
7893d1d5 | 659 | surplus_huge_pages, |
1da177e4 LT |
660 | HPAGE_SIZE/1024); |
661 | } | |
662 | ||
663 | int hugetlb_report_node_meminfo(int nid, char *buf) | |
664 | { | |
665 | return sprintf(buf, | |
666 | "Node %d HugePages_Total: %5u\n" | |
667 | "Node %d HugePages_Free: %5u\n", | |
668 | nid, nr_huge_pages_node[nid], | |
669 | nid, free_huge_pages_node[nid]); | |
670 | } | |
671 | ||
1da177e4 LT |
672 | /* Return the number pages of memory we physically have, in PAGE_SIZE units. */ |
673 | unsigned long hugetlb_total_pages(void) | |
674 | { | |
675 | return nr_huge_pages * (HPAGE_SIZE / PAGE_SIZE); | |
676 | } | |
1da177e4 LT |
677 | |
678 | /* | |
679 | * We cannot handle pagefaults against hugetlb pages at all. They cause | |
680 | * handle_mm_fault() to try to instantiate regular-sized pages in the | |
681 | * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get | |
682 | * this far. | |
683 | */ | |
d0217ac0 | 684 | static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
1da177e4 LT |
685 | { |
686 | BUG(); | |
d0217ac0 | 687 | return 0; |
1da177e4 LT |
688 | } |
689 | ||
690 | struct vm_operations_struct hugetlb_vm_ops = { | |
d0217ac0 | 691 | .fault = hugetlb_vm_op_fault, |
1da177e4 LT |
692 | }; |
693 | ||
1e8f889b DG |
694 | static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page, |
695 | int writable) | |
63551ae0 DG |
696 | { |
697 | pte_t entry; | |
698 | ||
1e8f889b | 699 | if (writable) { |
63551ae0 DG |
700 | entry = |
701 | pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot))); | |
702 | } else { | |
703 | entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot)); | |
704 | } | |
705 | entry = pte_mkyoung(entry); | |
706 | entry = pte_mkhuge(entry); | |
707 | ||
708 | return entry; | |
709 | } | |
710 | ||
1e8f889b DG |
711 | static void set_huge_ptep_writable(struct vm_area_struct *vma, |
712 | unsigned long address, pte_t *ptep) | |
713 | { | |
714 | pte_t entry; | |
715 | ||
716 | entry = pte_mkwrite(pte_mkdirty(*ptep)); | |
8dab5241 BH |
717 | if (ptep_set_access_flags(vma, address, ptep, entry, 1)) { |
718 | update_mmu_cache(vma, address, entry); | |
8dab5241 | 719 | } |
1e8f889b DG |
720 | } |
721 | ||
722 | ||
63551ae0 DG |
723 | int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, |
724 | struct vm_area_struct *vma) | |
725 | { | |
726 | pte_t *src_pte, *dst_pte, entry; | |
727 | struct page *ptepage; | |
1c59827d | 728 | unsigned long addr; |
1e8f889b DG |
729 | int cow; |
730 | ||
731 | cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; | |
63551ae0 | 732 | |
1c59827d | 733 | for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) { |
c74df32c HD |
734 | src_pte = huge_pte_offset(src, addr); |
735 | if (!src_pte) | |
736 | continue; | |
63551ae0 DG |
737 | dst_pte = huge_pte_alloc(dst, addr); |
738 | if (!dst_pte) | |
739 | goto nomem; | |
c5c99429 LW |
740 | |
741 | /* If the pagetables are shared don't copy or take references */ | |
742 | if (dst_pte == src_pte) | |
743 | continue; | |
744 | ||
c74df32c | 745 | spin_lock(&dst->page_table_lock); |
1c59827d | 746 | spin_lock(&src->page_table_lock); |
c74df32c | 747 | if (!pte_none(*src_pte)) { |
1e8f889b DG |
748 | if (cow) |
749 | ptep_set_wrprotect(src, addr, src_pte); | |
1c59827d HD |
750 | entry = *src_pte; |
751 | ptepage = pte_page(entry); | |
752 | get_page(ptepage); | |
1c59827d HD |
753 | set_huge_pte_at(dst, addr, dst_pte, entry); |
754 | } | |
755 | spin_unlock(&src->page_table_lock); | |
c74df32c | 756 | spin_unlock(&dst->page_table_lock); |
63551ae0 DG |
757 | } |
758 | return 0; | |
759 | ||
760 | nomem: | |
761 | return -ENOMEM; | |
762 | } | |
763 | ||
502717f4 CK |
764 | void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start, |
765 | unsigned long end) | |
63551ae0 DG |
766 | { |
767 | struct mm_struct *mm = vma->vm_mm; | |
768 | unsigned long address; | |
c7546f8f | 769 | pte_t *ptep; |
63551ae0 DG |
770 | pte_t pte; |
771 | struct page *page; | |
fe1668ae | 772 | struct page *tmp; |
c0a499c2 CK |
773 | /* |
774 | * A page gathering list, protected by per file i_mmap_lock. The | |
775 | * lock is used to avoid list corruption from multiple unmapping | |
776 | * of the same page since we are using page->lru. | |
777 | */ | |
fe1668ae | 778 | LIST_HEAD(page_list); |
63551ae0 DG |
779 | |
780 | WARN_ON(!is_vm_hugetlb_page(vma)); | |
781 | BUG_ON(start & ~HPAGE_MASK); | |
782 | BUG_ON(end & ~HPAGE_MASK); | |
783 | ||
508034a3 | 784 | spin_lock(&mm->page_table_lock); |
63551ae0 | 785 | for (address = start; address < end; address += HPAGE_SIZE) { |
c7546f8f | 786 | ptep = huge_pte_offset(mm, address); |
4c887265 | 787 | if (!ptep) |
c7546f8f DG |
788 | continue; |
789 | ||
39dde65c CK |
790 | if (huge_pmd_unshare(mm, &address, ptep)) |
791 | continue; | |
792 | ||
c7546f8f | 793 | pte = huge_ptep_get_and_clear(mm, address, ptep); |
63551ae0 DG |
794 | if (pte_none(pte)) |
795 | continue; | |
c7546f8f | 796 | |
63551ae0 | 797 | page = pte_page(pte); |
6649a386 KC |
798 | if (pte_dirty(pte)) |
799 | set_page_dirty(page); | |
fe1668ae | 800 | list_add(&page->lru, &page_list); |
63551ae0 | 801 | } |
1da177e4 | 802 | spin_unlock(&mm->page_table_lock); |
508034a3 | 803 | flush_tlb_range(vma, start, end); |
fe1668ae CK |
804 | list_for_each_entry_safe(page, tmp, &page_list, lru) { |
805 | list_del(&page->lru); | |
806 | put_page(page); | |
807 | } | |
1da177e4 | 808 | } |
63551ae0 | 809 | |
502717f4 CK |
810 | void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start, |
811 | unsigned long end) | |
812 | { | |
813 | /* | |
814 | * It is undesirable to test vma->vm_file as it should be non-null | |
815 | * for valid hugetlb area. However, vm_file will be NULL in the error | |
816 | * cleanup path of do_mmap_pgoff. When hugetlbfs ->mmap method fails, | |
817 | * do_mmap_pgoff() nullifies vma->vm_file before calling this function | |
818 | * to clean up. Since no pte has actually been setup, it is safe to | |
819 | * do nothing in this case. | |
820 | */ | |
821 | if (vma->vm_file) { | |
822 | spin_lock(&vma->vm_file->f_mapping->i_mmap_lock); | |
823 | __unmap_hugepage_range(vma, start, end); | |
824 | spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock); | |
825 | } | |
826 | } | |
827 | ||
1e8f889b DG |
828 | static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma, |
829 | unsigned long address, pte_t *ptep, pte_t pte) | |
830 | { | |
831 | struct page *old_page, *new_page; | |
79ac6ba4 | 832 | int avoidcopy; |
1e8f889b DG |
833 | |
834 | old_page = pte_page(pte); | |
835 | ||
836 | /* If no-one else is actually using this page, avoid the copy | |
837 | * and just make the page writable */ | |
838 | avoidcopy = (page_count(old_page) == 1); | |
839 | if (avoidcopy) { | |
840 | set_huge_ptep_writable(vma, address, ptep); | |
83c54070 | 841 | return 0; |
1e8f889b DG |
842 | } |
843 | ||
844 | page_cache_get(old_page); | |
5da7ca86 | 845 | new_page = alloc_huge_page(vma, address); |
1e8f889b | 846 | |
2fc39cec | 847 | if (IS_ERR(new_page)) { |
1e8f889b | 848 | page_cache_release(old_page); |
2fc39cec | 849 | return -PTR_ERR(new_page); |
1e8f889b DG |
850 | } |
851 | ||
852 | spin_unlock(&mm->page_table_lock); | |
9de455b2 | 853 | copy_huge_page(new_page, old_page, address, vma); |
0ed361de | 854 | __SetPageUptodate(new_page); |
1e8f889b DG |
855 | spin_lock(&mm->page_table_lock); |
856 | ||
857 | ptep = huge_pte_offset(mm, address & HPAGE_MASK); | |
858 | if (likely(pte_same(*ptep, pte))) { | |
859 | /* Break COW */ | |
860 | set_huge_pte_at(mm, address, ptep, | |
861 | make_huge_pte(vma, new_page, 1)); | |
862 | /* Make the old page be freed below */ | |
863 | new_page = old_page; | |
864 | } | |
865 | page_cache_release(new_page); | |
866 | page_cache_release(old_page); | |
83c54070 | 867 | return 0; |
1e8f889b DG |
868 | } |
869 | ||
a1ed3dda | 870 | static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma, |
1e8f889b | 871 | unsigned long address, pte_t *ptep, int write_access) |
ac9b9c66 HD |
872 | { |
873 | int ret = VM_FAULT_SIGBUS; | |
4c887265 AL |
874 | unsigned long idx; |
875 | unsigned long size; | |
4c887265 AL |
876 | struct page *page; |
877 | struct address_space *mapping; | |
1e8f889b | 878 | pte_t new_pte; |
4c887265 | 879 | |
4c887265 AL |
880 | mapping = vma->vm_file->f_mapping; |
881 | idx = ((address - vma->vm_start) >> HPAGE_SHIFT) | |
882 | + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT)); | |
883 | ||
884 | /* | |
885 | * Use page lock to guard against racing truncation | |
886 | * before we get page_table_lock. | |
887 | */ | |
6bda666a CL |
888 | retry: |
889 | page = find_lock_page(mapping, idx); | |
890 | if (!page) { | |
ebed4bfc HD |
891 | size = i_size_read(mapping->host) >> HPAGE_SHIFT; |
892 | if (idx >= size) | |
893 | goto out; | |
6bda666a | 894 | page = alloc_huge_page(vma, address); |
2fc39cec AL |
895 | if (IS_ERR(page)) { |
896 | ret = -PTR_ERR(page); | |
6bda666a CL |
897 | goto out; |
898 | } | |
79ac6ba4 | 899 | clear_huge_page(page, address); |
0ed361de | 900 | __SetPageUptodate(page); |
ac9b9c66 | 901 | |
6bda666a CL |
902 | if (vma->vm_flags & VM_SHARED) { |
903 | int err; | |
45c682a6 | 904 | struct inode *inode = mapping->host; |
6bda666a CL |
905 | |
906 | err = add_to_page_cache(page, mapping, idx, GFP_KERNEL); | |
907 | if (err) { | |
908 | put_page(page); | |
6bda666a CL |
909 | if (err == -EEXIST) |
910 | goto retry; | |
911 | goto out; | |
912 | } | |
45c682a6 KC |
913 | |
914 | spin_lock(&inode->i_lock); | |
915 | inode->i_blocks += BLOCKS_PER_HUGEPAGE; | |
916 | spin_unlock(&inode->i_lock); | |
6bda666a CL |
917 | } else |
918 | lock_page(page); | |
919 | } | |
1e8f889b | 920 | |
ac9b9c66 | 921 | spin_lock(&mm->page_table_lock); |
4c887265 AL |
922 | size = i_size_read(mapping->host) >> HPAGE_SHIFT; |
923 | if (idx >= size) | |
924 | goto backout; | |
925 | ||
83c54070 | 926 | ret = 0; |
86e5216f | 927 | if (!pte_none(*ptep)) |
4c887265 AL |
928 | goto backout; |
929 | ||
1e8f889b DG |
930 | new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE) |
931 | && (vma->vm_flags & VM_SHARED))); | |
932 | set_huge_pte_at(mm, address, ptep, new_pte); | |
933 | ||
934 | if (write_access && !(vma->vm_flags & VM_SHARED)) { | |
935 | /* Optimization, do the COW without a second fault */ | |
936 | ret = hugetlb_cow(mm, vma, address, ptep, new_pte); | |
937 | } | |
938 | ||
ac9b9c66 | 939 | spin_unlock(&mm->page_table_lock); |
4c887265 AL |
940 | unlock_page(page); |
941 | out: | |
ac9b9c66 | 942 | return ret; |
4c887265 AL |
943 | |
944 | backout: | |
945 | spin_unlock(&mm->page_table_lock); | |
4c887265 AL |
946 | unlock_page(page); |
947 | put_page(page); | |
948 | goto out; | |
ac9b9c66 HD |
949 | } |
950 | ||
86e5216f AL |
951 | int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, |
952 | unsigned long address, int write_access) | |
953 | { | |
954 | pte_t *ptep; | |
955 | pte_t entry; | |
1e8f889b | 956 | int ret; |
3935baa9 | 957 | static DEFINE_MUTEX(hugetlb_instantiation_mutex); |
86e5216f AL |
958 | |
959 | ptep = huge_pte_alloc(mm, address); | |
960 | if (!ptep) | |
961 | return VM_FAULT_OOM; | |
962 | ||
3935baa9 DG |
963 | /* |
964 | * Serialize hugepage allocation and instantiation, so that we don't | |
965 | * get spurious allocation failures if two CPUs race to instantiate | |
966 | * the same page in the page cache. | |
967 | */ | |
968 | mutex_lock(&hugetlb_instantiation_mutex); | |
86e5216f | 969 | entry = *ptep; |
3935baa9 DG |
970 | if (pte_none(entry)) { |
971 | ret = hugetlb_no_page(mm, vma, address, ptep, write_access); | |
972 | mutex_unlock(&hugetlb_instantiation_mutex); | |
973 | return ret; | |
974 | } | |
86e5216f | 975 | |
83c54070 | 976 | ret = 0; |
1e8f889b DG |
977 | |
978 | spin_lock(&mm->page_table_lock); | |
979 | /* Check for a racing update before calling hugetlb_cow */ | |
980 | if (likely(pte_same(entry, *ptep))) | |
981 | if (write_access && !pte_write(entry)) | |
982 | ret = hugetlb_cow(mm, vma, address, ptep, entry); | |
983 | spin_unlock(&mm->page_table_lock); | |
3935baa9 | 984 | mutex_unlock(&hugetlb_instantiation_mutex); |
1e8f889b DG |
985 | |
986 | return ret; | |
86e5216f AL |
987 | } |
988 | ||
63551ae0 DG |
989 | int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma, |
990 | struct page **pages, struct vm_area_struct **vmas, | |
5b23dbe8 AL |
991 | unsigned long *position, int *length, int i, |
992 | int write) | |
63551ae0 | 993 | { |
d5d4b0aa CK |
994 | unsigned long pfn_offset; |
995 | unsigned long vaddr = *position; | |
63551ae0 DG |
996 | int remainder = *length; |
997 | ||
1c59827d | 998 | spin_lock(&mm->page_table_lock); |
63551ae0 | 999 | while (vaddr < vma->vm_end && remainder) { |
4c887265 AL |
1000 | pte_t *pte; |
1001 | struct page *page; | |
63551ae0 | 1002 | |
4c887265 AL |
1003 | /* |
1004 | * Some archs (sparc64, sh*) have multiple pte_ts to | |
1005 | * each hugepage. We have to make * sure we get the | |
1006 | * first, for the page indexing below to work. | |
1007 | */ | |
1008 | pte = huge_pte_offset(mm, vaddr & HPAGE_MASK); | |
63551ae0 | 1009 | |
72fad713 | 1010 | if (!pte || pte_none(*pte) || (write && !pte_write(*pte))) { |
4c887265 | 1011 | int ret; |
63551ae0 | 1012 | |
4c887265 | 1013 | spin_unlock(&mm->page_table_lock); |
5b23dbe8 | 1014 | ret = hugetlb_fault(mm, vma, vaddr, write); |
4c887265 | 1015 | spin_lock(&mm->page_table_lock); |
a89182c7 | 1016 | if (!(ret & VM_FAULT_ERROR)) |
4c887265 | 1017 | continue; |
63551ae0 | 1018 | |
4c887265 AL |
1019 | remainder = 0; |
1020 | if (!i) | |
1021 | i = -EFAULT; | |
1022 | break; | |
1023 | } | |
1024 | ||
d5d4b0aa CK |
1025 | pfn_offset = (vaddr & ~HPAGE_MASK) >> PAGE_SHIFT; |
1026 | page = pte_page(*pte); | |
1027 | same_page: | |
d6692183 CK |
1028 | if (pages) { |
1029 | get_page(page); | |
d5d4b0aa | 1030 | pages[i] = page + pfn_offset; |
d6692183 | 1031 | } |
63551ae0 DG |
1032 | |
1033 | if (vmas) | |
1034 | vmas[i] = vma; | |
1035 | ||
1036 | vaddr += PAGE_SIZE; | |
d5d4b0aa | 1037 | ++pfn_offset; |
63551ae0 DG |
1038 | --remainder; |
1039 | ++i; | |
d5d4b0aa CK |
1040 | if (vaddr < vma->vm_end && remainder && |
1041 | pfn_offset < HPAGE_SIZE/PAGE_SIZE) { | |
1042 | /* | |
1043 | * We use pfn_offset to avoid touching the pageframes | |
1044 | * of this compound page. | |
1045 | */ | |
1046 | goto same_page; | |
1047 | } | |
63551ae0 | 1048 | } |
1c59827d | 1049 | spin_unlock(&mm->page_table_lock); |
63551ae0 DG |
1050 | *length = remainder; |
1051 | *position = vaddr; | |
1052 | ||
1053 | return i; | |
1054 | } | |
8f860591 ZY |
1055 | |
1056 | void hugetlb_change_protection(struct vm_area_struct *vma, | |
1057 | unsigned long address, unsigned long end, pgprot_t newprot) | |
1058 | { | |
1059 | struct mm_struct *mm = vma->vm_mm; | |
1060 | unsigned long start = address; | |
1061 | pte_t *ptep; | |
1062 | pte_t pte; | |
1063 | ||
1064 | BUG_ON(address >= end); | |
1065 | flush_cache_range(vma, address, end); | |
1066 | ||
39dde65c | 1067 | spin_lock(&vma->vm_file->f_mapping->i_mmap_lock); |
8f860591 ZY |
1068 | spin_lock(&mm->page_table_lock); |
1069 | for (; address < end; address += HPAGE_SIZE) { | |
1070 | ptep = huge_pte_offset(mm, address); | |
1071 | if (!ptep) | |
1072 | continue; | |
39dde65c CK |
1073 | if (huge_pmd_unshare(mm, &address, ptep)) |
1074 | continue; | |
8f860591 ZY |
1075 | if (!pte_none(*ptep)) { |
1076 | pte = huge_ptep_get_and_clear(mm, address, ptep); | |
1077 | pte = pte_mkhuge(pte_modify(pte, newprot)); | |
1078 | set_huge_pte_at(mm, address, ptep, pte); | |
8f860591 ZY |
1079 | } |
1080 | } | |
1081 | spin_unlock(&mm->page_table_lock); | |
39dde65c | 1082 | spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock); |
8f860591 ZY |
1083 | |
1084 | flush_tlb_range(vma, start, end); | |
1085 | } | |
1086 | ||
a43a8c39 CK |
1087 | struct file_region { |
1088 | struct list_head link; | |
1089 | long from; | |
1090 | long to; | |
1091 | }; | |
1092 | ||
1093 | static long region_add(struct list_head *head, long f, long t) | |
1094 | { | |
1095 | struct file_region *rg, *nrg, *trg; | |
1096 | ||
1097 | /* Locate the region we are either in or before. */ | |
1098 | list_for_each_entry(rg, head, link) | |
1099 | if (f <= rg->to) | |
1100 | break; | |
1101 | ||
1102 | /* Round our left edge to the current segment if it encloses us. */ | |
1103 | if (f > rg->from) | |
1104 | f = rg->from; | |
1105 | ||
1106 | /* Check for and consume any regions we now overlap with. */ | |
1107 | nrg = rg; | |
1108 | list_for_each_entry_safe(rg, trg, rg->link.prev, link) { | |
1109 | if (&rg->link == head) | |
1110 | break; | |
1111 | if (rg->from > t) | |
1112 | break; | |
1113 | ||
1114 | /* If this area reaches higher then extend our area to | |
1115 | * include it completely. If this is not the first area | |
1116 | * which we intend to reuse, free it. */ | |
1117 | if (rg->to > t) | |
1118 | t = rg->to; | |
1119 | if (rg != nrg) { | |
1120 | list_del(&rg->link); | |
1121 | kfree(rg); | |
1122 | } | |
1123 | } | |
1124 | nrg->from = f; | |
1125 | nrg->to = t; | |
1126 | return 0; | |
1127 | } | |
1128 | ||
1129 | static long region_chg(struct list_head *head, long f, long t) | |
1130 | { | |
1131 | struct file_region *rg, *nrg; | |
1132 | long chg = 0; | |
1133 | ||
1134 | /* Locate the region we are before or in. */ | |
1135 | list_for_each_entry(rg, head, link) | |
1136 | if (f <= rg->to) | |
1137 | break; | |
1138 | ||
1139 | /* If we are below the current region then a new region is required. | |
1140 | * Subtle, allocate a new region at the position but make it zero | |
183ff22b | 1141 | * size such that we can guarantee to record the reservation. */ |
a43a8c39 CK |
1142 | if (&rg->link == head || t < rg->from) { |
1143 | nrg = kmalloc(sizeof(*nrg), GFP_KERNEL); | |
c80544dc | 1144 | if (!nrg) |
a43a8c39 CK |
1145 | return -ENOMEM; |
1146 | nrg->from = f; | |
1147 | nrg->to = f; | |
1148 | INIT_LIST_HEAD(&nrg->link); | |
1149 | list_add(&nrg->link, rg->link.prev); | |
1150 | ||
1151 | return t - f; | |
1152 | } | |
1153 | ||
1154 | /* Round our left edge to the current segment if it encloses us. */ | |
1155 | if (f > rg->from) | |
1156 | f = rg->from; | |
1157 | chg = t - f; | |
1158 | ||
1159 | /* Check for and consume any regions we now overlap with. */ | |
1160 | list_for_each_entry(rg, rg->link.prev, link) { | |
1161 | if (&rg->link == head) | |
1162 | break; | |
1163 | if (rg->from > t) | |
1164 | return chg; | |
1165 | ||
1166 | /* We overlap with this area, if it extends futher than | |
1167 | * us then we must extend ourselves. Account for its | |
1168 | * existing reservation. */ | |
1169 | if (rg->to > t) { | |
1170 | chg += rg->to - t; | |
1171 | t = rg->to; | |
1172 | } | |
1173 | chg -= rg->to - rg->from; | |
1174 | } | |
1175 | return chg; | |
1176 | } | |
1177 | ||
1178 | static long region_truncate(struct list_head *head, long end) | |
1179 | { | |
1180 | struct file_region *rg, *trg; | |
1181 | long chg = 0; | |
1182 | ||
1183 | /* Locate the region we are either in or before. */ | |
1184 | list_for_each_entry(rg, head, link) | |
1185 | if (end <= rg->to) | |
1186 | break; | |
1187 | if (&rg->link == head) | |
1188 | return 0; | |
1189 | ||
1190 | /* If we are in the middle of a region then adjust it. */ | |
1191 | if (end > rg->from) { | |
1192 | chg = rg->to - end; | |
1193 | rg->to = end; | |
1194 | rg = list_entry(rg->link.next, typeof(*rg), link); | |
1195 | } | |
1196 | ||
1197 | /* Drop any remaining regions. */ | |
1198 | list_for_each_entry_safe(rg, trg, rg->link.prev, link) { | |
1199 | if (&rg->link == head) | |
1200 | break; | |
1201 | chg += rg->to - rg->from; | |
1202 | list_del(&rg->link); | |
1203 | kfree(rg); | |
1204 | } | |
1205 | return chg; | |
1206 | } | |
1207 | ||
1208 | static int hugetlb_acct_memory(long delta) | |
1209 | { | |
1210 | int ret = -ENOMEM; | |
1211 | ||
1212 | spin_lock(&hugetlb_lock); | |
8a630112 KC |
1213 | /* |
1214 | * When cpuset is configured, it breaks the strict hugetlb page | |
1215 | * reservation as the accounting is done on a global variable. Such | |
1216 | * reservation is completely rubbish in the presence of cpuset because | |
1217 | * the reservation is not checked against page availability for the | |
1218 | * current cpuset. Application can still potentially OOM'ed by kernel | |
1219 | * with lack of free htlb page in cpuset that the task is in. | |
1220 | * Attempt to enforce strict accounting with cpuset is almost | |
1221 | * impossible (or too ugly) because cpuset is too fluid that | |
1222 | * task or memory node can be dynamically moved between cpusets. | |
1223 | * | |
1224 | * The change of semantics for shared hugetlb mapping with cpuset is | |
1225 | * undesirable. However, in order to preserve some of the semantics, | |
1226 | * we fall back to check against current free page availability as | |
1227 | * a best attempt and hopefully to minimize the impact of changing | |
1228 | * semantics that cpuset has. | |
1229 | */ | |
e4e574b7 AL |
1230 | if (delta > 0) { |
1231 | if (gather_surplus_pages(delta) < 0) | |
1232 | goto out; | |
1233 | ||
ac09b3a1 AL |
1234 | if (delta > cpuset_mems_nr(free_huge_pages_node)) { |
1235 | return_unused_surplus_pages(delta); | |
e4e574b7 | 1236 | goto out; |
ac09b3a1 | 1237 | } |
e4e574b7 AL |
1238 | } |
1239 | ||
1240 | ret = 0; | |
e4e574b7 AL |
1241 | if (delta < 0) |
1242 | return_unused_surplus_pages((unsigned long) -delta); | |
1243 | ||
1244 | out: | |
1245 | spin_unlock(&hugetlb_lock); | |
1246 | return ret; | |
1247 | } | |
1248 | ||
1249 | int hugetlb_reserve_pages(struct inode *inode, long from, long to) | |
1250 | { | |
1251 | long ret, chg; | |
1252 | ||
1253 | chg = region_chg(&inode->i_mapping->private_list, from, to); | |
1254 | if (chg < 0) | |
1255 | return chg; | |
8a630112 | 1256 | |
90d8b7e6 AL |
1257 | if (hugetlb_get_quota(inode->i_mapping, chg)) |
1258 | return -ENOSPC; | |
a43a8c39 | 1259 | ret = hugetlb_acct_memory(chg); |
68842c9b KC |
1260 | if (ret < 0) { |
1261 | hugetlb_put_quota(inode->i_mapping, chg); | |
a43a8c39 | 1262 | return ret; |
68842c9b | 1263 | } |
a43a8c39 CK |
1264 | region_add(&inode->i_mapping->private_list, from, to); |
1265 | return 0; | |
1266 | } | |
1267 | ||
1268 | void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed) | |
1269 | { | |
1270 | long chg = region_truncate(&inode->i_mapping->private_list, offset); | |
45c682a6 KC |
1271 | |
1272 | spin_lock(&inode->i_lock); | |
1273 | inode->i_blocks -= BLOCKS_PER_HUGEPAGE * freed; | |
1274 | spin_unlock(&inode->i_lock); | |
1275 | ||
90d8b7e6 AL |
1276 | hugetlb_put_quota(inode->i_mapping, (chg - freed)); |
1277 | hugetlb_acct_memory(-(chg - freed)); | |
a43a8c39 | 1278 | } |