hugetlb.c 75.9 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
2
3
4
5
6
7
8
/*
 * Generic hugetlb support.
 * (C) William Irwin, April 2004
 */
#include <linux/list.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/mm.h>
9
#include <linux/seq_file.h>
Linus Torvalds's avatar
Linus Torvalds committed
10
11
#include <linux/sysctl.h>
#include <linux/highmem.h>
Andrea Arcangeli's avatar
Andrea Arcangeli committed
12
#include <linux/mmu_notifier.h>
Linus Torvalds's avatar
Linus Torvalds committed
13
#include <linux/nodemask.h>
David Gibson's avatar
David Gibson committed
14
#include <linux/pagemap.h>
15
#include <linux/mempolicy.h>
16
#include <linux/cpuset.h>
17
#include <linux/mutex.h>
18
#include <linux/bootmem.h>
19
#include <linux/sysfs.h>
20
#include <linux/slab.h>
21
#include <linux/rmap.h>
22
23
#include <linux/swap.h>
#include <linux/swapops.h>
24

David Gibson's avatar
David Gibson committed
25
26
#include <asm/page.h>
#include <asm/pgtable.h>
27
#include <asm/io.h>
David Gibson's avatar
David Gibson committed
28
29

#include <linux/hugetlb.h>
30
#include <linux/node.h>
31
#include "internal.h"
Linus Torvalds's avatar
Linus Torvalds committed
32
33

const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
34
35
static gfp_t htlb_alloc_mask = GFP_HIGHUSER;
unsigned long hugepages_treat_as_movable;
36

37
38
39
40
static int max_hstate;
unsigned int default_hstate_idx;
struct hstate hstates[HUGE_MAX_HSTATE];

41
42
__initdata LIST_HEAD(huge_boot_pages);

43
44
45
/* for command line parsing */
static struct hstate * __initdata parsed_hstate;
static unsigned long __initdata default_hstate_max_huge_pages;
46
static unsigned long __initdata default_hstate_size;
47
48
49

#define for_each_hstate(h) \
	for ((h) = hstates; (h) < &hstates[max_hstate]; (h)++)
50

51
52
53
54
/*
 * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
 */
static DEFINE_SPINLOCK(hugetlb_lock);
55

56
57
58
/*
 * Region tracking -- allows tracking of reservations and instantiated pages
 *                    across the pages in a mapping.
59
60
61
62
63
64
65
66
67
68
 *
 * The region data structures are protected by a combination of the mmap_sem
 * and the hugetlb_instantion_mutex.  To access or modify a region the caller
 * must either hold the mmap_sem for write, or the mmap_sem for read and
 * the hugetlb_instantiation mutex:
 *
 * 	down_write(&mm->mmap_sem);
 * or
 * 	down_read(&mm->mmap_sem);
 * 	mutex_lock(&hugetlb_instantiation_mutex);
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
 */
struct file_region {
	struct list_head link;
	long from;
	long to;
};

static long region_add(struct list_head *head, long f, long t)
{
	struct file_region *rg, *nrg, *trg;

	/* Locate the region we are either in or before. */
	list_for_each_entry(rg, head, link)
		if (f <= rg->to)
			break;

	/* Round our left edge to the current segment if it encloses us. */
	if (f > rg->from)
		f = rg->from;

	/* Check for and consume any regions we now overlap with. */
	nrg = rg;
	list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
		if (&rg->link == head)
			break;
		if (rg->from > t)
			break;

		/* If this area reaches higher then extend our area to
		 * include it completely.  If this is not the first area
		 * which we intend to reuse, free it. */
		if (rg->to > t)
			t = rg->to;
		if (rg != nrg) {
			list_del(&rg->link);
			kfree(rg);
		}
	}
	nrg->from = f;
	nrg->to = t;
	return 0;
}

static long region_chg(struct list_head *head, long f, long t)
{
	struct file_region *rg, *nrg;
	long chg = 0;

	/* Locate the region we are before or in. */
	list_for_each_entry(rg, head, link)
		if (f <= rg->to)
			break;

	/* If we are below the current region then a new region is required.
	 * Subtle, allocate a new region at the position but make it zero
	 * size such that we can guarantee to record the reservation. */
	if (&rg->link == head || t < rg->from) {
		nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
		if (!nrg)
			return -ENOMEM;
		nrg->from = f;
		nrg->to   = f;
		INIT_LIST_HEAD(&nrg->link);
		list_add(&nrg->link, rg->link.prev);

		return t - f;
	}

	/* Round our left edge to the current segment if it encloses us. */
	if (f > rg->from)
		f = rg->from;
	chg = t - f;

	/* Check for and consume any regions we now overlap with. */
	list_for_each_entry(rg, rg->link.prev, link) {
		if (&rg->link == head)
			break;
		if (rg->from > t)
			return chg;

Lucas De Marchi's avatar
Lucas De Marchi committed
149
		/* We overlap with this area, if it extends further than
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
		 * us then we must extend ourselves.  Account for its
		 * existing reservation. */
		if (rg->to > t) {
			chg += rg->to - t;
			t = rg->to;
		}
		chg -= rg->to - rg->from;
	}
	return chg;
}

static long region_truncate(struct list_head *head, long end)
{
	struct file_region *rg, *trg;
	long chg = 0;

	/* Locate the region we are either in or before. */
	list_for_each_entry(rg, head, link)
		if (end <= rg->to)
			break;
	if (&rg->link == head)
		return 0;

	/* If we are in the middle of a region then adjust it. */
	if (end > rg->from) {
		chg = rg->to - end;
		rg->to = end;
		rg = list_entry(rg->link.next, typeof(*rg), link);
	}

	/* Drop any remaining regions. */
	list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
		if (&rg->link == head)
			break;
		chg += rg->to - rg->from;
		list_del(&rg->link);
		kfree(rg);
	}
	return chg;
}

191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
static long region_count(struct list_head *head, long f, long t)
{
	struct file_region *rg;
	long chg = 0;

	/* Locate each segment we overlap with, and count that overlap. */
	list_for_each_entry(rg, head, link) {
		int seg_from;
		int seg_to;

		if (rg->to <= f)
			continue;
		if (rg->from >= t)
			break;

		seg_from = max(rg->from, f);
		seg_to = min(rg->to, t);

		chg += seg_to - seg_from;
	}

	return chg;
}

215
216
217
218
/*
 * Convert the address within this vma to the page offset within
 * the mapping, in pagecache page units; huge pages here.
 */
219
220
static pgoff_t vma_hugecache_offset(struct hstate *h,
			struct vm_area_struct *vma, unsigned long address)
221
{
222
223
	return ((address - vma->vm_start) >> huge_page_shift(h)) +
			(vma->vm_pgoff >> huge_page_order(h));
224
225
}

226
227
228
229
230
231
pgoff_t linear_hugepage_index(struct vm_area_struct *vma,
				     unsigned long address)
{
	return vma_hugecache_offset(hstate_vma(vma), vma, address);
}

232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
 * Return the size of the pages allocated when backing a VMA. In the majority
 * cases this will be same size as used by the page table entries.
 */
unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
{
	struct hstate *hstate;

	if (!is_vm_hugetlb_page(vma))
		return PAGE_SIZE;

	hstate = hstate_vma(vma);

	return 1UL << (hstate->order + PAGE_SHIFT);
}
247
EXPORT_SYMBOL_GPL(vma_kernel_pagesize);
248

249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * Return the page size being used by the MMU to back a VMA. In the majority
 * of cases, the page size used by the kernel matches the MMU size. On
 * architectures where it differs, an architecture-specific version of this
 * function is required.
 */
#ifndef vma_mmu_pagesize
unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
{
	return vma_kernel_pagesize(vma);
}
#endif

262
263
264
265
266
267
268
/*
 * Flags for MAP_PRIVATE reservations.  These are stored in the bottom
 * bits of the reservation map pointer, which are always clear due to
 * alignment.
 */
#define HPAGE_RESV_OWNER    (1UL << 0)
#define HPAGE_RESV_UNMAPPED (1UL << 1)
269
#define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
270

271
272
273
274
275
276
277
278
279
/*
 * These helpers are used to track how many pages are reserved for
 * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
 * is guaranteed to have their future faults succeed.
 *
 * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
 * the reserve counters are updated with the hugetlb_lock held. It is safe
 * to reset the VMA at fork() time as it is not in use yet and there is no
 * chance of the global counters getting corrupted as a result of the values.
280
281
282
283
284
285
286
287
288
 *
 * The private mapping reservation is represented in a subtly different
 * manner to a shared mapping.  A shared mapping has a region map associated
 * with the underlying file, this region map represents the backing file
 * pages which have ever had a reservation assigned which this persists even
 * after the page is instantiated.  A private mapping has a region map
 * associated with the original mmap which is attached to all VMAs which
 * reference it, this region map represents those offsets which have consumed
 * reservation ie. where pages have been instantiated.
289
 */
290
291
292
293
294
295
296
297
298
299
300
static unsigned long get_vma_private_data(struct vm_area_struct *vma)
{
	return (unsigned long)vma->vm_private_data;
}

static void set_vma_private_data(struct vm_area_struct *vma,
							unsigned long value)
{
	vma->vm_private_data = (void *)value;
}

301
302
303
304
305
struct resv_map {
	struct kref refs;
	struct list_head regions;
};

306
static struct resv_map *resv_map_alloc(void)
307
308
309
310
311
312
313
314
315
316
317
{
	struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);
	if (!resv_map)
		return NULL;

	kref_init(&resv_map->refs);
	INIT_LIST_HEAD(&resv_map->regions);

	return resv_map;
}

318
static void resv_map_release(struct kref *ref)
319
320
321
322
323
324
325
326
327
{
	struct resv_map *resv_map = container_of(ref, struct resv_map, refs);

	/* Clear out any active regions before we release the map. */
	region_truncate(&resv_map->regions, 0);
	kfree(resv_map);
}

static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
328
329
{
	VM_BUG_ON(!is_vm_hugetlb_page(vma));
330
	if (!(vma->vm_flags & VM_MAYSHARE))
331
332
		return (struct resv_map *)(get_vma_private_data(vma) &
							~HPAGE_RESV_MASK);
333
	return NULL;
334
335
}

336
static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)
337
338
{
	VM_BUG_ON(!is_vm_hugetlb_page(vma));
339
	VM_BUG_ON(vma->vm_flags & VM_MAYSHARE);
340

341
342
	set_vma_private_data(vma, (get_vma_private_data(vma) &
				HPAGE_RESV_MASK) | (unsigned long)map);
343
344
345
346
347
}

static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
{
	VM_BUG_ON(!is_vm_hugetlb_page(vma));
348
	VM_BUG_ON(vma->vm_flags & VM_MAYSHARE);
349
350

	set_vma_private_data(vma, get_vma_private_data(vma) | flags);
351
352
353
354
355
}

static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
{
	VM_BUG_ON(!is_vm_hugetlb_page(vma));
356
357

	return (get_vma_private_data(vma) & flag) != 0;
358
359
360
}

/* Decrement the reserved pages in the hugepage pool by one */
361
362
static void decrement_hugepage_resv_vma(struct hstate *h,
			struct vm_area_struct *vma)
363
{
364
365
366
	if (vma->vm_flags & VM_NORESERVE)
		return;

367
	if (vma->vm_flags & VM_MAYSHARE) {
368
		/* Shared mappings always use reserves */
369
		h->resv_huge_pages--;
370
	} else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
371
372
373
374
		/*
		 * Only the process that called mmap() has reserves for
		 * private mappings.
		 */
375
		h->resv_huge_pages--;
376
377
378
	}
}

379
/* Reset counters to 0 and clear all HPAGE_RESV_* flags */
380
381
382
void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
{
	VM_BUG_ON(!is_vm_hugetlb_page(vma));
383
	if (!(vma->vm_flags & VM_MAYSHARE))
384
385
386
387
		vma->vm_private_data = (void *)0;
}

/* Returns true if the VMA has associated reserve pages */
388
static int vma_has_reserves(struct vm_area_struct *vma)
389
{
390
	if (vma->vm_flags & VM_MAYSHARE)
391
392
393
394
		return 1;
	if (is_vma_resv_set(vma, HPAGE_RESV_OWNER))
		return 1;
	return 0;
395
396
}

397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
static void copy_gigantic_page(struct page *dst, struct page *src)
{
	int i;
	struct hstate *h = page_hstate(src);
	struct page *dst_base = dst;
	struct page *src_base = src;

	for (i = 0; i < pages_per_huge_page(h); ) {
		cond_resched();
		copy_highpage(dst, src);

		i++;
		dst = mem_map_next(dst, dst_base, i);
		src = mem_map_next(src, src_base, i);
	}
}

void copy_huge_page(struct page *dst, struct page *src)
{
	int i;
	struct hstate *h = page_hstate(src);

	if (unlikely(pages_per_huge_page(h) > MAX_ORDER_NR_PAGES)) {
		copy_gigantic_page(dst, src);
		return;
	}

	might_sleep();
	for (i = 0; i < pages_per_huge_page(h); i++) {
		cond_resched();
		copy_highpage(dst + i, src + i);
	}
}

431
static void enqueue_huge_page(struct hstate *h, struct page *page)
Linus Torvalds's avatar
Linus Torvalds committed
432
433
{
	int nid = page_to_nid(page);
434
435
436
	list_add(&page->lru, &h->hugepage_freelists[nid]);
	h->free_huge_pages++;
	h->free_huge_pages_node[nid]++;
Linus Torvalds's avatar
Linus Torvalds committed
437
438
}

439
440
441
442
443
444
445
446
static struct page *dequeue_huge_page_node(struct hstate *h, int nid)
{
	struct page *page;

	if (list_empty(&h->hugepage_freelists[nid]))
		return NULL;
	page = list_entry(h->hugepage_freelists[nid].next, struct page, lru);
	list_del(&page->lru);
447
	set_page_refcounted(page);
448
449
450
451
452
	h->free_huge_pages--;
	h->free_huge_pages_node[nid]--;
	return page;
}

453
454
static struct page *dequeue_huge_page_vma(struct hstate *h,
				struct vm_area_struct *vma,
455
				unsigned long address, int avoid_reserve)
Linus Torvalds's avatar
Linus Torvalds committed
456
457
{
	struct page *page = NULL;
458
	struct mempolicy *mpol;
459
	nodemask_t *nodemask;
460
	struct zonelist *zonelist;
461
462
	struct zone *zone;
	struct zoneref *z;
Linus Torvalds's avatar
Linus Torvalds committed
463

464
465
466
	get_mems_allowed();
	zonelist = huge_zonelist(vma, address,
					htlb_alloc_mask, &mpol, &nodemask);
467
468
469
470
471
	/*
	 * A child process with MAP_PRIVATE mappings created by their parent
	 * have no page reserves. This check ensures that reservations are
	 * not "stolen". The child may still get SIGKILLed
	 */
472
	if (!vma_has_reserves(vma) &&
473
			h->free_huge_pages - h->resv_huge_pages == 0)
474
		goto err;
475

476
	/* If reserves cannot be used, ensure enough pages are in the pool */
477
	if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0)
478
		goto err;
479

480
481
	for_each_zone_zonelist_nodemask(zone, z, zonelist,
						MAX_NR_ZONES - 1, nodemask) {
482
483
484
485
486
487
488
		if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask)) {
			page = dequeue_huge_page_node(h, zone_to_nid(zone));
			if (page) {
				if (!avoid_reserve)
					decrement_hugepage_resv_vma(h, vma);
				break;
			}
489
		}
Linus Torvalds's avatar
Linus Torvalds committed
490
	}
491
err:
492
	mpol_cond_put(mpol);
493
	put_mems_allowed();
Linus Torvalds's avatar
Linus Torvalds committed
494
495
496
	return page;
}

497
static void update_and_free_page(struct hstate *h, struct page *page)
498
499
{
	int i;
500

501
502
	VM_BUG_ON(h->order >= MAX_ORDER);

503
504
505
	h->nr_huge_pages--;
	h->nr_huge_pages_node[page_to_nid(page)]--;
	for (i = 0; i < pages_per_huge_page(h); i++) {
506
507
508
509
510
511
		page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
				1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
				1 << PG_private | 1<< PG_writeback);
	}
	set_compound_page_dtor(page, NULL);
	set_page_refcounted(page);
512
	arch_release_hugepage(page);
513
	__free_pages(page, huge_page_order(h));
514
515
}

516
517
518
519
520
521
522
523
524
525
526
struct hstate *size_to_hstate(unsigned long size)
{
	struct hstate *h;

	for_each_hstate(h) {
		if (huge_page_size(h) == size)
			return h;
	}
	return NULL;
}

527
528
static void free_huge_page(struct page *page)
{
529
530
531
532
	/*
	 * Can't pass hstate in here because it is called from the
	 * compound page destructor.
	 */
533
	struct hstate *h = page_hstate(page);
534
	int nid = page_to_nid(page);
535
	struct address_space *mapping;
536

537
	mapping = (struct address_space *) page_private(page);
538
	set_page_private(page, 0);
539
	page->mapping = NULL;
540
	BUG_ON(page_count(page));
541
	BUG_ON(page_mapcount(page));
542
543
544
	INIT_LIST_HEAD(&page->lru);

	spin_lock(&hugetlb_lock);
545
	if (h->surplus_huge_pages_node[nid] && huge_page_order(h) < MAX_ORDER) {
546
547
548
		update_and_free_page(h, page);
		h->surplus_huge_pages--;
		h->surplus_huge_pages_node[nid]--;
549
	} else {
550
		enqueue_huge_page(h, page);
551
	}
552
	spin_unlock(&hugetlb_lock);
553
	if (mapping)
554
		hugetlb_put_quota(mapping, 1);
555
556
}

557
static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
558
559
560
{
	set_compound_page_dtor(page, free_huge_page);
	spin_lock(&hugetlb_lock);
561
562
	h->nr_huge_pages++;
	h->nr_huge_pages_node[nid]++;
563
564
565
566
	spin_unlock(&hugetlb_lock);
	put_page(page); /* free it into the hugepage allocator */
}

567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
static void prep_compound_gigantic_page(struct page *page, unsigned long order)
{
	int i;
	int nr_pages = 1 << order;
	struct page *p = page + 1;

	/* we rely on prep_new_huge_page to set the destructor */
	set_compound_order(page, order);
	__SetPageHead(page);
	for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
		__SetPageTail(p);
		p->first_page = page;
	}
}

int PageHuge(struct page *page)
{
	compound_page_dtor *dtor;

	if (!PageCompound(page))
		return 0;

	page = compound_head(page);
	dtor = get_compound_page_dtor(page);

	return dtor == free_huge_page;
}

595
596
EXPORT_SYMBOL_GPL(PageHuge);

597
static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid)
Linus Torvalds's avatar
Linus Torvalds committed
598
599
{
	struct page *page;
600

601
602
603
	if (h->order >= MAX_ORDER)
		return NULL;

604
	page = alloc_pages_exact_node(nid,
605
606
		htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
						__GFP_REPEAT|__GFP_NOWARN,
607
		huge_page_order(h));
Linus Torvalds's avatar
Linus Torvalds committed
608
	if (page) {
609
		if (arch_prepare_hugepage(page)) {
610
			__free_pages(page, huge_page_order(h));
611
			return NULL;
612
		}
613
		prep_new_huge_page(h, page, nid);
Linus Torvalds's avatar
Linus Torvalds committed
614
	}
615
616
617
618

	return page;
}

619
/*
620
621
622
623
624
 * common helper functions for hstate_next_node_to_{alloc|free}.
 * We may have allocated or freed a huge page based on a different
 * nodes_allowed previously, so h->next_node_to_{alloc|free} might
 * be outside of *nodes_allowed.  Ensure that we use an allowed
 * node for alloc or free.
625
 */
626
static int next_node_allowed(int nid, nodemask_t *nodes_allowed)
627
{
628
	nid = next_node(nid, *nodes_allowed);
629
	if (nid == MAX_NUMNODES)
630
		nid = first_node(*nodes_allowed);
631
632
633
634
635
	VM_BUG_ON(nid >= MAX_NUMNODES);

	return nid;
}

636
637
638
639
640
641
642
static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed)
{
	if (!node_isset(nid, *nodes_allowed))
		nid = next_node_allowed(nid, nodes_allowed);
	return nid;
}

643
/*
644
645
646
647
 * returns the previously saved node ["this node"] from which to
 * allocate a persistent huge page for the pool and advance the
 * next node from which to allocate, handling wrap at end of node
 * mask.
648
 */
649
650
static int hstate_next_node_to_alloc(struct hstate *h,
					nodemask_t *nodes_allowed)
651
{
652
653
654
655
656
657
	int nid;

	VM_BUG_ON(!nodes_allowed);

	nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed);
	h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed);
658
659

	return nid;
660
661
}

662
static int alloc_fresh_huge_page(struct hstate *h, nodemask_t *nodes_allowed)
663
664
665
666
667
668
{
	struct page *page;
	int start_nid;
	int next_nid;
	int ret = 0;

669
	start_nid = hstate_next_node_to_alloc(h, nodes_allowed);
670
	next_nid = start_nid;
671
672

	do {
673
		page = alloc_fresh_huge_page_node(h, next_nid);
674
		if (page) {
675
			ret = 1;
676
677
			break;
		}
678
		next_nid = hstate_next_node_to_alloc(h, nodes_allowed);
679
	} while (next_nid != start_nid);
680

681
682
683
684
685
	if (ret)
		count_vm_event(HTLB_BUDDY_PGALLOC);
	else
		count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);

686
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
687
688
}

689
/*
690
691
692
693
 * helper for free_pool_huge_page() - return the previously saved
 * node ["this node"] from which to free a huge page.  Advance the
 * next node id whether or not we find a free huge page to free so
 * that the next attempt to free addresses the next node.
694
 */
695
static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed)
696
{
697
698
699
700
701
702
	int nid;

	VM_BUG_ON(!nodes_allowed);

	nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed);
	h->next_nid_to_free = next_node_allowed(nid, nodes_allowed);
703
704

	return nid;
705
706
707
708
709
710
711
712
}

/*
 * Free huge page from pool from next node to free.
 * Attempt to keep persistent huge pages more or less
 * balanced over allowed nodes.
 * Called with hugetlb_lock locked.
 */
713
714
static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
							 bool acct_surplus)
715
716
717
718
719
{
	int start_nid;
	int next_nid;
	int ret = 0;

720
	start_nid = hstate_next_node_to_free(h, nodes_allowed);
721
722
723
	next_nid = start_nid;

	do {
724
725
726
727
728
729
		/*
		 * If we're returning unused surplus pages, only examine
		 * nodes with surplus pages.
		 */
		if ((!acct_surplus || h->surplus_huge_pages_node[next_nid]) &&
		    !list_empty(&h->hugepage_freelists[next_nid])) {
730
731
732
733
734
735
			struct page *page =
				list_entry(h->hugepage_freelists[next_nid].next,
					  struct page, lru);
			list_del(&page->lru);
			h->free_huge_pages--;
			h->free_huge_pages_node[next_nid]--;
736
737
738
739
			if (acct_surplus) {
				h->surplus_huge_pages--;
				h->surplus_huge_pages_node[next_nid]--;
			}
740
741
			update_and_free_page(h, page);
			ret = 1;
742
			break;
743
		}
744
		next_nid = hstate_next_node_to_free(h, nodes_allowed);
745
	} while (next_nid != start_nid);
746
747
748
749

	return ret;
}

750
static struct page *alloc_buddy_huge_page(struct hstate *h, int nid)
751
752
{
	struct page *page;
753
	unsigned int r_nid;
754

755
756
757
	if (h->order >= MAX_ORDER)
		return NULL;

758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
	/*
	 * Assume we will successfully allocate the surplus page to
	 * prevent racing processes from causing the surplus to exceed
	 * overcommit
	 *
	 * This however introduces a different race, where a process B
	 * tries to grow the static hugepage pool while alloc_pages() is
	 * called by process A. B will only examine the per-node
	 * counters in determining if surplus huge pages can be
	 * converted to normal huge pages in adjust_pool_surplus(). A
	 * won't be able to increment the per-node counter, until the
	 * lock is dropped by B, but B doesn't drop hugetlb_lock until
	 * no more huge pages can be converted from surplus to normal
	 * state (and doesn't try to convert again). Thus, we have a
	 * case where a surplus huge page exists, the pool is grown, and
	 * the surplus huge page still exists after, even though it
	 * should just have been converted to a normal huge page. This
	 * does not leak memory, though, as the hugepage will be freed
	 * once it is out of use. It also does not allow the counters to
	 * go out of whack in adjust_pool_surplus() as we don't modify
	 * the node values until we've gotten the hugepage and only the
	 * per-node value is checked there.
	 */
	spin_lock(&hugetlb_lock);
782
	if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
783
784
785
		spin_unlock(&hugetlb_lock);
		return NULL;
	} else {
786
787
		h->nr_huge_pages++;
		h->surplus_huge_pages++;
788
789
790
	}
	spin_unlock(&hugetlb_lock);

791
792
793
794
795
796
797
798
	if (nid == NUMA_NO_NODE)
		page = alloc_pages(htlb_alloc_mask|__GFP_COMP|
				   __GFP_REPEAT|__GFP_NOWARN,
				   huge_page_order(h));
	else
		page = alloc_pages_exact_node(nid,
			htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
			__GFP_REPEAT|__GFP_NOWARN, huge_page_order(h));
799

800
801
802
803
804
	if (page && arch_prepare_hugepage(page)) {
		__free_pages(page, huge_page_order(h));
		return NULL;
	}

805
	spin_lock(&hugetlb_lock);
806
	if (page) {
807
		r_nid = page_to_nid(page);
808
		set_compound_page_dtor(page, free_huge_page);
809
810
811
		/*
		 * We incremented the global counters already
		 */
812
813
		h->nr_huge_pages_node[r_nid]++;
		h->surplus_huge_pages_node[r_nid]++;
814
		__count_vm_event(HTLB_BUDDY_PGALLOC);
815
	} else {
816
817
		h->nr_huge_pages--;
		h->surplus_huge_pages--;
818
		__count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
819
	}
820
	spin_unlock(&hugetlb_lock);
821
822
823
824

	return page;
}

825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
/*
 * This allocation function is useful in the context where vma is irrelevant.
 * E.g. soft-offlining uses this function because it only cares physical
 * address of error page.
 */
struct page *alloc_huge_page_node(struct hstate *h, int nid)
{
	struct page *page;

	spin_lock(&hugetlb_lock);
	page = dequeue_huge_page_node(h, nid);
	spin_unlock(&hugetlb_lock);

	if (!page)
		page = alloc_buddy_huge_page(h, nid);

	return page;
}

844
/*
Lucas De Marchi's avatar
Lucas De Marchi committed
845
 * Increase the hugetlb pool such that it can accommodate a reservation
846
847
 * of size 'delta'.
 */
848
static int gather_surplus_pages(struct hstate *h, int delta)
849
850
851
852
853
854
{
	struct list_head surplus_list;
	struct page *page, *tmp;
	int ret, i;
	int needed, allocated;

855
	needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
856
	if (needed <= 0) {
857
		h->resv_huge_pages += delta;
858
		return 0;
859
	}
860
861
862
863
864
865
866
867

	allocated = 0;
	INIT_LIST_HEAD(&surplus_list);

	ret = -ENOMEM;
retry:
	spin_unlock(&hugetlb_lock);
	for (i = 0; i < needed; i++) {
868
		page = alloc_buddy_huge_page(h, NUMA_NO_NODE);
869
		if (!page)
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
			/*
			 * We were not able to allocate enough pages to
			 * satisfy the entire reservation so we free what
			 * we've allocated so far.
			 */
			goto free;

		list_add(&page->lru, &surplus_list);
	}
	allocated += needed;

	/*
	 * After retaking hugetlb_lock, we need to recalculate 'needed'
	 * because either resv_huge_pages or free_huge_pages may have changed.
	 */
	spin_lock(&hugetlb_lock);
886
887
	needed = (h->resv_huge_pages + delta) -
			(h->free_huge_pages + allocated);
888
889
890
891
892
	if (needed > 0)
		goto retry;

	/*
	 * The surplus_list now contains _at_least_ the number of extra pages
Lucas De Marchi's avatar
Lucas De Marchi committed
893
	 * needed to accommodate the reservation.  Add the appropriate number
894
	 * of pages to the hugetlb pool and free the extras back to the buddy
895
896
897
	 * allocator.  Commit the entire reservation here to prevent another
	 * process from stealing the pages as they are added to the pool but
	 * before they are reserved.
898
899
	 */
	needed += allocated;
900
	h->resv_huge_pages += delta;
901
	ret = 0;
902
903

	spin_unlock(&hugetlb_lock);
904
	/* Free the needed pages to the hugetlb pool */
905
	list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
906
907
		if ((--needed) < 0)
			break;
908
		list_del(&page->lru);
909
910
911
912
913
914
		/*
		 * This page is now managed by the hugetlb allocator and has
		 * no users -- drop the buddy allocator's reference.
		 */
		put_page_testzero(page);
		VM_BUG_ON(page_count(page));
915
		enqueue_huge_page(h, page);
916
917
918
	}

	/* Free unnecessary surplus pages to the buddy allocator */
919
free:
920
921
922
	if (!list_empty(&surplus_list)) {
		list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
			list_del(&page->lru);
923
			put_page(page);
924
		}
925
	}
926
	spin_lock(&hugetlb_lock);
927
928
929
930
931
932
933
934

	return ret;
}

/*
 * When releasing a hugetlb pool reservation, any surplus pages that were
 * allocated to satisfy the reservation must be explicitly freed if they were
 * never used.
935
 * Called with hugetlb_lock held.
936
 */
937
938
static void return_unused_surplus_pages(struct hstate *h,
					unsigned long unused_resv_pages)
939
940
941
{
	unsigned long nr_pages;

942
	/* Uncommit the reservation */
943
	h->resv_huge_pages -= unused_resv_pages;
944

945
946
947
948
	/* Cannot return gigantic pages currently */
	if (h->order >= MAX_ORDER)
		return;

949
	nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
950

951
952
	/*
	 * We want to release as many surplus pages as possible, spread
953
954
955
956
957
	 * evenly across all nodes with memory. Iterate across these nodes
	 * until we can no longer free unreserved surplus pages. This occurs
	 * when the nodes with surplus pages have no free pages.
	 * free_pool_huge_page() will balance the the freed pages across the
	 * on-line nodes with memory and will handle the hstate accounting.
958
959
	 */
	while (nr_pages--) {
960
		if (!free_pool_huge_page(h, &node_states[N_HIGH_MEMORY], 1))
961
			break;
962
963
964
	}
}

965
966
967
968
969
970
971
972
973
/*
 * Determine if the huge page at addr within the vma has an associated
 * reservation.  Where it does not we will need to logically increase
 * reservation and actually increase quota before an allocation can occur.
 * Where any new reservation would be required the reservation change is
 * prepared, but not committed.  Once the page has been quota'd allocated
 * an instantiated the change should be committed via vma_commit_reservation.
 * No action is required on failure.
 */
974
static long vma_needs_reservation(struct hstate *h,
975
			struct vm_area_struct *vma, unsigned long addr)
976
977
978
979
{
	struct address_space *mapping = vma->vm_file->f_mapping;
	struct inode *inode = mapping->host;

980
	if (vma->vm_flags & VM_MAYSHARE) {
981
		pgoff_t idx = vma_hugecache_offset(h, vma, addr);
982
983
984
		return region_chg(&inode->i_mapping->private_list,
							idx, idx + 1);

985
986
	} else if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
		return 1;
987

988
	} else  {
989
		long err;
990
		pgoff_t idx = vma_hugecache_offset(h, vma, addr);
991
992
993
994
995
996
997
		struct resv_map *reservations = vma_resv_map(vma);

		err = region_chg(&reservations->regions, idx, idx + 1);
		if (err < 0)
			return err;
		return 0;
	}
998
}
999
1000
static void vma_commit_reservation(struct hstate *h,
			struct vm_area_struct *vma, unsigned long addr)
For faster browsing, not all history is shown. View entire blame