blob: 499cb72c74b1d4c70dfb05610c0fe1248a15f166 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Generic hugetlb support.
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +01003 * (C) Nadia Yvette Chambers, April 2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/list.h>
6#include <linux/init.h>
7#include <linux/module.h>
8#include <linux/mm.h>
Alexey Dobriyane1759c22008-10-15 23:50:22 +04009#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/sysctl.h>
11#include <linux/highmem.h>
Andrea Arcangelicddb8a52008-07-28 15:46:29 -070012#include <linux/mmu_notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/nodemask.h>
David Gibson63551ae2005-06-21 17:14:44 -070014#include <linux/pagemap.h>
Christoph Lameter5da7ca82006-01-06 00:10:46 -080015#include <linux/mempolicy.h>
Gideon Israel Dsouza3b321232014-04-07 15:37:26 -070016#include <linux/compiler.h>
Christoph Lameteraea47ff2006-01-08 01:00:57 -080017#include <linux/cpuset.h>
David Gibson3935baa2006-03-22 00:08:53 -080018#include <linux/mutex.h>
Andi Kleenaa888a72008-07-23 21:27:47 -070019#include <linux/bootmem.h>
Nishanth Aravamudana3437872008-07-23 21:27:44 -070020#include <linux/sysfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +090022#include <linux/rmap.h>
Naoya Horiguchifd6a03ed2010-05-28 09:29:21 +090023#include <linux/swap.h>
24#include <linux/swapops.h>
Naoya Horiguchic8721bb2013-09-11 14:22:09 -070025#include <linux/page-isolation.h>
Davidlohr Bueso8382d912014-04-03 14:47:31 -070026#include <linux/jhash.h>
Linus Torvaldsd6606682008-08-06 12:04:54 -070027
David Gibson63551ae2005-06-21 17:14:44 -070028#include <asm/page.h>
29#include <asm/pgtable.h>
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -070030#include <asm/tlb.h>
David Gibson63551ae2005-06-21 17:14:44 -070031
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -070032#include <linux/io.h>
David Gibson63551ae2005-06-21 17:14:44 -070033#include <linux/hugetlb.h>
Aneesh Kumar K.V9dd540e2012-07-31 16:42:15 -070034#include <linux/hugetlb_cgroup.h>
Lee Schermerhorn9a305232009-12-14 17:58:25 -080035#include <linux/node.h>
Nick Piggin7835e982006-03-22 00:08:40 -080036#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Andrey Ryabinin753162c2015-02-10 14:11:36 -080038int hugepages_treat_as_movable;
Andi Kleena5516432008-07-23 21:27:41 -070039
Aneesh Kumar K.Vc3f38a32012-07-31 16:42:10 -070040int hugetlb_max_hstate __read_mostly;
Andi Kleene5ff2152008-07-23 21:27:42 -070041unsigned int default_hstate_idx;
42struct hstate hstates[HUGE_MAX_HSTATE];
43
Jon Tollefson53ba51d2008-07-23 21:27:52 -070044__initdata LIST_HEAD(huge_boot_pages);
45
Andi Kleene5ff2152008-07-23 21:27:42 -070046/* for command line parsing */
47static struct hstate * __initdata parsed_hstate;
48static unsigned long __initdata default_hstate_max_huge_pages;
Nick Piggine11bfbf2008-07-23 21:27:52 -070049static unsigned long __initdata default_hstate_size;
Andi Kleene5ff2152008-07-23 21:27:42 -070050
David Gibson3935baa2006-03-22 00:08:53 -080051/*
Naoya Horiguchi31caf662013-09-11 14:21:59 -070052 * Protects updates to hugepage_freelists, hugepage_activelist, nr_huge_pages,
53 * free_huge_pages, and surplus_huge_pages.
David Gibson3935baa2006-03-22 00:08:53 -080054 */
Aneesh Kumar K.Vc3f38a32012-07-31 16:42:10 -070055DEFINE_SPINLOCK(hugetlb_lock);
Eric Paris0bd0f9f2005-11-21 21:32:28 -080056
Davidlohr Bueso8382d912014-04-03 14:47:31 -070057/*
58 * Serializes faults on the same logical page. This is used to
59 * prevent spurious OOMs when the hugepage pool is fully utilized.
60 */
61static int num_fault_mutexes;
62static struct mutex *htlb_fault_mutex_table ____cacheline_aligned_in_smp;
63
David Gibson90481622012-03-21 16:34:12 -070064static inline void unlock_or_release_subpool(struct hugepage_subpool *spool)
65{
66 bool free = (spool->count == 0) && (spool->used_hpages == 0);
67
68 spin_unlock(&spool->lock);
69
70 /* If no pages are used, and no other handles to the subpool
71 * remain, free the subpool the subpool remain */
72 if (free)
73 kfree(spool);
74}
75
76struct hugepage_subpool *hugepage_new_subpool(long nr_blocks)
77{
78 struct hugepage_subpool *spool;
79
Mike Kravetzc6a91822015-04-15 16:13:36 -070080 spool = kzalloc(sizeof(*spool), GFP_KERNEL);
David Gibson90481622012-03-21 16:34:12 -070081 if (!spool)
82 return NULL;
83
84 spin_lock_init(&spool->lock);
85 spool->count = 1;
86 spool->max_hpages = nr_blocks;
David Gibson90481622012-03-21 16:34:12 -070087
88 return spool;
89}
90
91void hugepage_put_subpool(struct hugepage_subpool *spool)
92{
93 spin_lock(&spool->lock);
94 BUG_ON(!spool->count);
95 spool->count--;
96 unlock_or_release_subpool(spool);
97}
98
Mike Kravetz1c5ecae2015-04-15 16:13:39 -070099/*
100 * Subpool accounting for allocating and reserving pages.
101 * Return -ENOMEM if there are not enough resources to satisfy the
102 * the request. Otherwise, return the number of pages by which the
103 * global pools must be adjusted (upward). The returned value may
104 * only be different than the passed value (delta) in the case where
105 * a subpool minimum size must be manitained.
106 */
107static long hugepage_subpool_get_pages(struct hugepage_subpool *spool,
David Gibson90481622012-03-21 16:34:12 -0700108 long delta)
109{
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700110 long ret = delta;
David Gibson90481622012-03-21 16:34:12 -0700111
112 if (!spool)
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700113 return ret;
David Gibson90481622012-03-21 16:34:12 -0700114
115 spin_lock(&spool->lock);
David Gibson90481622012-03-21 16:34:12 -0700116
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700117 if (spool->max_hpages != -1) { /* maximum size accounting */
118 if ((spool->used_hpages + delta) <= spool->max_hpages)
119 spool->used_hpages += delta;
120 else {
121 ret = -ENOMEM;
122 goto unlock_ret;
123 }
124 }
125
126 if (spool->min_hpages != -1) { /* minimum size accounting */
127 if (delta > spool->rsv_hpages) {
128 /*
129 * Asking for more reserves than those already taken on
130 * behalf of subpool. Return difference.
131 */
132 ret = delta - spool->rsv_hpages;
133 spool->rsv_hpages = 0;
134 } else {
135 ret = 0; /* reserves already accounted for */
136 spool->rsv_hpages -= delta;
137 }
138 }
139
140unlock_ret:
141 spin_unlock(&spool->lock);
David Gibson90481622012-03-21 16:34:12 -0700142 return ret;
143}
144
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700145/*
146 * Subpool accounting for freeing and unreserving pages.
147 * Return the number of global page reservations that must be dropped.
148 * The return value may only be different than the passed value (delta)
149 * in the case where a subpool minimum size must be maintained.
150 */
151static long hugepage_subpool_put_pages(struct hugepage_subpool *spool,
David Gibson90481622012-03-21 16:34:12 -0700152 long delta)
153{
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700154 long ret = delta;
155
David Gibson90481622012-03-21 16:34:12 -0700156 if (!spool)
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700157 return delta;
David Gibson90481622012-03-21 16:34:12 -0700158
159 spin_lock(&spool->lock);
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700160
161 if (spool->max_hpages != -1) /* maximum size accounting */
162 spool->used_hpages -= delta;
163
164 if (spool->min_hpages != -1) { /* minimum size accounting */
165 if (spool->rsv_hpages + delta <= spool->min_hpages)
166 ret = 0;
167 else
168 ret = spool->rsv_hpages + delta - spool->min_hpages;
169
170 spool->rsv_hpages += delta;
171 if (spool->rsv_hpages > spool->min_hpages)
172 spool->rsv_hpages = spool->min_hpages;
173 }
174
175 /*
176 * If hugetlbfs_put_super couldn't free spool due to an outstanding
177 * quota reference, free it now.
178 */
David Gibson90481622012-03-21 16:34:12 -0700179 unlock_or_release_subpool(spool);
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700180
181 return ret;
David Gibson90481622012-03-21 16:34:12 -0700182}
183
184static inline struct hugepage_subpool *subpool_inode(struct inode *inode)
185{
186 return HUGETLBFS_SB(inode->i_sb)->spool;
187}
188
189static inline struct hugepage_subpool *subpool_vma(struct vm_area_struct *vma)
190{
Al Viro496ad9a2013-01-23 17:07:38 -0500191 return subpool_inode(file_inode(vma->vm_file));
David Gibson90481622012-03-21 16:34:12 -0700192}
193
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700194/*
Andy Whitcroft96822902008-07-23 21:27:29 -0700195 * Region tracking -- allows tracking of reservations and instantiated pages
196 * across the pages in a mapping.
Andy Whitcroft84afd992008-07-23 21:27:32 -0700197 *
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700198 * The region data structures are embedded into a resv_map and
199 * protected by a resv_map's lock
Andy Whitcroft96822902008-07-23 21:27:29 -0700200 */
201struct file_region {
202 struct list_head link;
203 long from;
204 long to;
205};
206
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700207static long region_add(struct resv_map *resv, long f, long t)
Andy Whitcroft96822902008-07-23 21:27:29 -0700208{
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700209 struct list_head *head = &resv->regions;
Andy Whitcroft96822902008-07-23 21:27:29 -0700210 struct file_region *rg, *nrg, *trg;
211
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700212 spin_lock(&resv->lock);
Andy Whitcroft96822902008-07-23 21:27:29 -0700213 /* Locate the region we are either in or before. */
214 list_for_each_entry(rg, head, link)
215 if (f <= rg->to)
216 break;
217
218 /* Round our left edge to the current segment if it encloses us. */
219 if (f > rg->from)
220 f = rg->from;
221
222 /* Check for and consume any regions we now overlap with. */
223 nrg = rg;
224 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
225 if (&rg->link == head)
226 break;
227 if (rg->from > t)
228 break;
229
230 /* If this area reaches higher then extend our area to
231 * include it completely. If this is not the first area
232 * which we intend to reuse, free it. */
233 if (rg->to > t)
234 t = rg->to;
235 if (rg != nrg) {
236 list_del(&rg->link);
237 kfree(rg);
238 }
239 }
240 nrg->from = f;
241 nrg->to = t;
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700242 spin_unlock(&resv->lock);
Andy Whitcroft96822902008-07-23 21:27:29 -0700243 return 0;
244}
245
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700246static long region_chg(struct resv_map *resv, long f, long t)
Andy Whitcroft96822902008-07-23 21:27:29 -0700247{
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700248 struct list_head *head = &resv->regions;
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700249 struct file_region *rg, *nrg = NULL;
Andy Whitcroft96822902008-07-23 21:27:29 -0700250 long chg = 0;
251
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700252retry:
253 spin_lock(&resv->lock);
Andy Whitcroft96822902008-07-23 21:27:29 -0700254 /* Locate the region we are before or in. */
255 list_for_each_entry(rg, head, link)
256 if (f <= rg->to)
257 break;
258
259 /* If we are below the current region then a new region is required.
260 * Subtle, allocate a new region at the position but make it zero
261 * size such that we can guarantee to record the reservation. */
262 if (&rg->link == head || t < rg->from) {
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700263 if (!nrg) {
264 spin_unlock(&resv->lock);
265 nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
266 if (!nrg)
267 return -ENOMEM;
Andy Whitcroft96822902008-07-23 21:27:29 -0700268
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700269 nrg->from = f;
270 nrg->to = f;
271 INIT_LIST_HEAD(&nrg->link);
272 goto retry;
273 }
274
275 list_add(&nrg->link, rg->link.prev);
276 chg = t - f;
277 goto out_nrg;
Andy Whitcroft96822902008-07-23 21:27:29 -0700278 }
279
280 /* Round our left edge to the current segment if it encloses us. */
281 if (f > rg->from)
282 f = rg->from;
283 chg = t - f;
284
285 /* Check for and consume any regions we now overlap with. */
286 list_for_each_entry(rg, rg->link.prev, link) {
287 if (&rg->link == head)
288 break;
289 if (rg->from > t)
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700290 goto out;
Andy Whitcroft96822902008-07-23 21:27:29 -0700291
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300292 /* We overlap with this area, if it extends further than
Andy Whitcroft96822902008-07-23 21:27:29 -0700293 * us then we must extend ourselves. Account for its
294 * existing reservation. */
295 if (rg->to > t) {
296 chg += rg->to - t;
297 t = rg->to;
298 }
299 chg -= rg->to - rg->from;
300 }
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700301
302out:
303 spin_unlock(&resv->lock);
304 /* We already know we raced and no longer need the new region */
305 kfree(nrg);
306 return chg;
307out_nrg:
308 spin_unlock(&resv->lock);
Andy Whitcroft96822902008-07-23 21:27:29 -0700309 return chg;
310}
311
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700312static long region_truncate(struct resv_map *resv, long end)
Andy Whitcroft96822902008-07-23 21:27:29 -0700313{
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700314 struct list_head *head = &resv->regions;
Andy Whitcroft96822902008-07-23 21:27:29 -0700315 struct file_region *rg, *trg;
316 long chg = 0;
317
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700318 spin_lock(&resv->lock);
Andy Whitcroft96822902008-07-23 21:27:29 -0700319 /* Locate the region we are either in or before. */
320 list_for_each_entry(rg, head, link)
321 if (end <= rg->to)
322 break;
323 if (&rg->link == head)
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700324 goto out;
Andy Whitcroft96822902008-07-23 21:27:29 -0700325
326 /* If we are in the middle of a region then adjust it. */
327 if (end > rg->from) {
328 chg = rg->to - end;
329 rg->to = end;
330 rg = list_entry(rg->link.next, typeof(*rg), link);
331 }
332
333 /* Drop any remaining regions. */
334 list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
335 if (&rg->link == head)
336 break;
337 chg += rg->to - rg->from;
338 list_del(&rg->link);
339 kfree(rg);
340 }
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700341
342out:
343 spin_unlock(&resv->lock);
Andy Whitcroft96822902008-07-23 21:27:29 -0700344 return chg;
345}
346
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700347static long region_count(struct resv_map *resv, long f, long t)
Andy Whitcroft84afd992008-07-23 21:27:32 -0700348{
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700349 struct list_head *head = &resv->regions;
Andy Whitcroft84afd992008-07-23 21:27:32 -0700350 struct file_region *rg;
351 long chg = 0;
352
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700353 spin_lock(&resv->lock);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700354 /* Locate each segment we overlap with, and count that overlap. */
355 list_for_each_entry(rg, head, link) {
Wang Sheng-Huif2135a42012-05-29 15:06:17 -0700356 long seg_from;
357 long seg_to;
Andy Whitcroft84afd992008-07-23 21:27:32 -0700358
359 if (rg->to <= f)
360 continue;
361 if (rg->from >= t)
362 break;
363
364 seg_from = max(rg->from, f);
365 seg_to = min(rg->to, t);
366
367 chg += seg_to - seg_from;
368 }
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700369 spin_unlock(&resv->lock);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700370
371 return chg;
372}
373
Andy Whitcroft96822902008-07-23 21:27:29 -0700374/*
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700375 * Convert the address within this vma to the page offset within
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700376 * the mapping, in pagecache page units; huge pages here.
377 */
Andi Kleena5516432008-07-23 21:27:41 -0700378static pgoff_t vma_hugecache_offset(struct hstate *h,
379 struct vm_area_struct *vma, unsigned long address)
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700380{
Andi Kleena5516432008-07-23 21:27:41 -0700381 return ((address - vma->vm_start) >> huge_page_shift(h)) +
382 (vma->vm_pgoff >> huge_page_order(h));
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700383}
384
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +0900385pgoff_t linear_hugepage_index(struct vm_area_struct *vma,
386 unsigned long address)
387{
388 return vma_hugecache_offset(hstate_vma(vma), vma, address);
389}
390
Andy Whitcroft84afd992008-07-23 21:27:32 -0700391/*
Mel Gorman08fba692009-01-06 14:38:53 -0800392 * Return the size of the pages allocated when backing a VMA. In the majority
393 * cases this will be same size as used by the page table entries.
394 */
395unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
396{
397 struct hstate *hstate;
398
399 if (!is_vm_hugetlb_page(vma))
400 return PAGE_SIZE;
401
402 hstate = hstate_vma(vma);
403
Wanpeng Li2415cf12013-07-03 15:02:43 -0700404 return 1UL << huge_page_shift(hstate);
Mel Gorman08fba692009-01-06 14:38:53 -0800405}
Joerg Roedelf340ca02009-06-19 15:16:22 +0200406EXPORT_SYMBOL_GPL(vma_kernel_pagesize);
Mel Gorman08fba692009-01-06 14:38:53 -0800407
408/*
Mel Gorman33402892009-01-06 14:38:54 -0800409 * Return the page size being used by the MMU to back a VMA. In the majority
410 * of cases, the page size used by the kernel matches the MMU size. On
411 * architectures where it differs, an architecture-specific version of this
412 * function is required.
413 */
414#ifndef vma_mmu_pagesize
415unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
416{
417 return vma_kernel_pagesize(vma);
418}
419#endif
420
421/*
Andy Whitcroft84afd992008-07-23 21:27:32 -0700422 * Flags for MAP_PRIVATE reservations. These are stored in the bottom
423 * bits of the reservation map pointer, which are always clear due to
424 * alignment.
425 */
426#define HPAGE_RESV_OWNER (1UL << 0)
427#define HPAGE_RESV_UNMAPPED (1UL << 1)
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700428#define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
Andy Whitcroft84afd992008-07-23 21:27:32 -0700429
Mel Gormana1e78772008-07-23 21:27:23 -0700430/*
431 * These helpers are used to track how many pages are reserved for
432 * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
433 * is guaranteed to have their future faults succeed.
434 *
435 * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
436 * the reserve counters are updated with the hugetlb_lock held. It is safe
437 * to reset the VMA at fork() time as it is not in use yet and there is no
438 * chance of the global counters getting corrupted as a result of the values.
Andy Whitcroft84afd992008-07-23 21:27:32 -0700439 *
440 * The private mapping reservation is represented in a subtly different
441 * manner to a shared mapping. A shared mapping has a region map associated
442 * with the underlying file, this region map represents the backing file
443 * pages which have ever had a reservation assigned which this persists even
444 * after the page is instantiated. A private mapping has a region map
445 * associated with the original mmap which is attached to all VMAs which
446 * reference it, this region map represents those offsets which have consumed
447 * reservation ie. where pages have been instantiated.
Mel Gormana1e78772008-07-23 21:27:23 -0700448 */
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700449static unsigned long get_vma_private_data(struct vm_area_struct *vma)
450{
451 return (unsigned long)vma->vm_private_data;
452}
453
454static void set_vma_private_data(struct vm_area_struct *vma,
455 unsigned long value)
456{
457 vma->vm_private_data = (void *)value;
458}
459
Joonsoo Kim9119a412014-04-03 14:47:25 -0700460struct resv_map *resv_map_alloc(void)
Andy Whitcroft84afd992008-07-23 21:27:32 -0700461{
462 struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);
463 if (!resv_map)
464 return NULL;
465
466 kref_init(&resv_map->refs);
Davidlohr Bueso7b24d862014-04-03 14:47:27 -0700467 spin_lock_init(&resv_map->lock);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700468 INIT_LIST_HEAD(&resv_map->regions);
469
470 return resv_map;
471}
472
Joonsoo Kim9119a412014-04-03 14:47:25 -0700473void resv_map_release(struct kref *ref)
Andy Whitcroft84afd992008-07-23 21:27:32 -0700474{
475 struct resv_map *resv_map = container_of(ref, struct resv_map, refs);
476
477 /* Clear out any active regions before we release the map. */
Joonsoo Kim1406ec92014-04-03 14:47:26 -0700478 region_truncate(resv_map, 0);
Andy Whitcroft84afd992008-07-23 21:27:32 -0700479 kfree(resv_map);
480}
481
Joonsoo Kim4e35f482014-04-03 14:47:30 -0700482static inline struct resv_map *inode_resv_map(struct inode *inode)
483{
484 return inode->i_mapping->private_data;
485}
486
Andy Whitcroft84afd992008-07-23 21:27:32 -0700487static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
Mel Gormana1e78772008-07-23 21:27:23 -0700488{
Sasha Levin81d1b092014-10-09 15:28:10 -0700489 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
Joonsoo Kim4e35f482014-04-03 14:47:30 -0700490 if (vma->vm_flags & VM_MAYSHARE) {
491 struct address_space *mapping = vma->vm_file->f_mapping;
492 struct inode *inode = mapping->host;
493
494 return inode_resv_map(inode);
495
496 } else {
Andy Whitcroft84afd992008-07-23 21:27:32 -0700497 return (struct resv_map *)(get_vma_private_data(vma) &
498 ~HPAGE_RESV_MASK);
Joonsoo Kim4e35f482014-04-03 14:47:30 -0700499 }
Mel Gormana1e78772008-07-23 21:27:23 -0700500}
501
Andy Whitcroft84afd992008-07-23 21:27:32 -0700502static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)
Mel Gormana1e78772008-07-23 21:27:23 -0700503{
Sasha Levin81d1b092014-10-09 15:28:10 -0700504 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
505 VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma);
Mel Gormana1e78772008-07-23 21:27:23 -0700506
Andy Whitcroft84afd992008-07-23 21:27:32 -0700507 set_vma_private_data(vma, (get_vma_private_data(vma) &
508 HPAGE_RESV_MASK) | (unsigned long)map);
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700509}
510
511static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
512{
Sasha Levin81d1b092014-10-09 15:28:10 -0700513 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
514 VM_BUG_ON_VMA(vma->vm_flags & VM_MAYSHARE, vma);
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700515
516 set_vma_private_data(vma, get_vma_private_data(vma) | flags);
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700517}
518
519static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
520{
Sasha Levin81d1b092014-10-09 15:28:10 -0700521 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -0700522
523 return (get_vma_private_data(vma) & flag) != 0;
Mel Gormana1e78772008-07-23 21:27:23 -0700524}
525
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700526/* Reset counters to 0 and clear all HPAGE_RESV_* flags */
Mel Gormana1e78772008-07-23 21:27:23 -0700527void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
528{
Sasha Levin81d1b092014-10-09 15:28:10 -0700529 VM_BUG_ON_VMA(!is_vm_hugetlb_page(vma), vma);
Mel Gormanf83a2752009-05-28 14:34:40 -0700530 if (!(vma->vm_flags & VM_MAYSHARE))
Mel Gormana1e78772008-07-23 21:27:23 -0700531 vma->vm_private_data = (void *)0;
532}
533
534/* Returns true if the VMA has associated reserve pages */
Joonsoo Kimaf0ed732013-09-11 14:21:18 -0700535static int vma_has_reserves(struct vm_area_struct *vma, long chg)
Mel Gormana1e78772008-07-23 21:27:23 -0700536{
Joonsoo Kimaf0ed732013-09-11 14:21:18 -0700537 if (vma->vm_flags & VM_NORESERVE) {
538 /*
539 * This address is already reserved by other process(chg == 0),
540 * so, we should decrement reserved count. Without decrementing,
541 * reserve count remains after releasing inode, because this
542 * allocated page will go into page cache and is regarded as
543 * coming from reserved pool in releasing step. Currently, we
544 * don't have any other solution to deal with this situation
545 * properly, so add work-around here.
546 */
547 if (vma->vm_flags & VM_MAYSHARE && chg == 0)
548 return 1;
549 else
550 return 0;
551 }
Joonsoo Kima63884e2013-09-11 14:21:07 -0700552
553 /* Shared mappings always use reserves */
Mel Gormanf83a2752009-05-28 14:34:40 -0700554 if (vma->vm_flags & VM_MAYSHARE)
Mel Gorman7f09ca52008-07-23 21:27:58 -0700555 return 1;
Joonsoo Kima63884e2013-09-11 14:21:07 -0700556
557 /*
558 * Only the process that called mmap() has reserves for
559 * private mappings.
560 */
Mel Gorman7f09ca52008-07-23 21:27:58 -0700561 if (is_vma_resv_set(vma, HPAGE_RESV_OWNER))
562 return 1;
Joonsoo Kima63884e2013-09-11 14:21:07 -0700563
Mel Gorman7f09ca52008-07-23 21:27:58 -0700564 return 0;
Mel Gormana1e78772008-07-23 21:27:23 -0700565}
566
Andi Kleena5516432008-07-23 21:27:41 -0700567static void enqueue_huge_page(struct hstate *h, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 int nid = page_to_nid(page);
Aneesh Kumar K.V0edaecf2012-07-31 16:42:07 -0700570 list_move(&page->lru, &h->hugepage_freelists[nid]);
Andi Kleena5516432008-07-23 21:27:41 -0700571 h->free_huge_pages++;
572 h->free_huge_pages_node[nid]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
Naoya Horiguchibf50bab2010-09-08 10:19:33 +0900575static struct page *dequeue_huge_page_node(struct hstate *h, int nid)
576{
577 struct page *page;
578
Naoya Horiguchic8721bb2013-09-11 14:22:09 -0700579 list_for_each_entry(page, &h->hugepage_freelists[nid], lru)
580 if (!is_migrate_isolate_page(page))
581 break;
582 /*
583 * if 'non-isolated free hugepage' not found on the list,
584 * the allocation fails.
585 */
586 if (&h->hugepage_freelists[nid] == &page->lru)
Naoya Horiguchibf50bab2010-09-08 10:19:33 +0900587 return NULL;
Aneesh Kumar K.V0edaecf2012-07-31 16:42:07 -0700588 list_move(&page->lru, &h->hugepage_activelist);
Naoya Horiguchia9869b82010-09-08 10:19:37 +0900589 set_page_refcounted(page);
Naoya Horiguchibf50bab2010-09-08 10:19:33 +0900590 h->free_huge_pages--;
591 h->free_huge_pages_node[nid]--;
592 return page;
593}
594
Naoya Horiguchi86cdb462013-09-11 14:22:13 -0700595/* Movability of hugepages depends on migration support. */
596static inline gfp_t htlb_alloc_mask(struct hstate *h)
597{
Naoya Horiguchi100873d2014-06-04 16:10:56 -0700598 if (hugepages_treat_as_movable || hugepage_migration_supported(h))
Naoya Horiguchi86cdb462013-09-11 14:22:13 -0700599 return GFP_HIGHUSER_MOVABLE;
600 else
601 return GFP_HIGHUSER;
602}
603
Andi Kleena5516432008-07-23 21:27:41 -0700604static struct page *dequeue_huge_page_vma(struct hstate *h,
605 struct vm_area_struct *vma,
Joonsoo Kimaf0ed732013-09-11 14:21:18 -0700606 unsigned long address, int avoid_reserve,
607 long chg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Konstantin Khlebnikovb1c12cb2012-04-25 16:01:46 -0700609 struct page *page = NULL;
Lee Schermerhorn480eccf2007-09-18 22:46:47 -0700610 struct mempolicy *mpol;
Mel Gorman19770b32008-04-28 02:12:18 -0700611 nodemask_t *nodemask;
Miao Xiec0ff7452010-05-24 14:32:08 -0700612 struct zonelist *zonelist;
Mel Gormandd1a2392008-04-28 02:12:17 -0700613 struct zone *zone;
614 struct zoneref *z;
Mel Gormancc9a6c82012-03-21 16:34:11 -0700615 unsigned int cpuset_mems_cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Mel Gormana1e78772008-07-23 21:27:23 -0700617 /*
618 * A child process with MAP_PRIVATE mappings created by their parent
619 * have no page reserves. This check ensures that reservations are
620 * not "stolen". The child may still get SIGKILLed
621 */
Joonsoo Kimaf0ed732013-09-11 14:21:18 -0700622 if (!vma_has_reserves(vma, chg) &&
Andi Kleena5516432008-07-23 21:27:41 -0700623 h->free_huge_pages - h->resv_huge_pages == 0)
Miao Xiec0ff7452010-05-24 14:32:08 -0700624 goto err;
Mel Gormana1e78772008-07-23 21:27:23 -0700625
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700626 /* If reserves cannot be used, ensure enough pages are in the pool */
Andi Kleena5516432008-07-23 21:27:41 -0700627 if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0)
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700628 goto err;
Mel Gorman04f2cbe2008-07-23 21:27:25 -0700629
Joonsoo Kim9966c4b2013-09-11 14:20:50 -0700630retry_cpuset:
Mel Gormand26914d2014-04-03 14:47:24 -0700631 cpuset_mems_cookie = read_mems_allowed_begin();
Joonsoo Kim9966c4b2013-09-11 14:20:50 -0700632 zonelist = huge_zonelist(vma, address,
Naoya Horiguchi86cdb462013-09-11 14:22:13 -0700633 htlb_alloc_mask(h), &mpol, &nodemask);
Joonsoo Kim9966c4b2013-09-11 14:20:50 -0700634
Mel Gorman19770b32008-04-28 02:12:18 -0700635 for_each_zone_zonelist_nodemask(zone, z, zonelist,
636 MAX_NR_ZONES - 1, nodemask) {
Vladimir Davydov344736f2014-10-20 15:50:30 +0400637 if (cpuset_zone_allowed(zone, htlb_alloc_mask(h))) {
Naoya Horiguchibf50bab2010-09-08 10:19:33 +0900638 page = dequeue_huge_page_node(h, zone_to_nid(zone));
639 if (page) {
Joonsoo Kimaf0ed732013-09-11 14:21:18 -0700640 if (avoid_reserve)
641 break;
642 if (!vma_has_reserves(vma, chg))
643 break;
644
Joonsoo Kim07443a82013-09-11 14:21:58 -0700645 SetPagePrivate(page);
Joonsoo Kimaf0ed732013-09-11 14:21:18 -0700646 h->resv_huge_pages--;
Naoya Horiguchibf50bab2010-09-08 10:19:33 +0900647 break;
648 }
Andrew Morton3abf7af2007-07-19 01:49:08 -0700649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
Mel Gormancc9a6c82012-03-21 16:34:11 -0700651
652 mpol_cond_put(mpol);
Mel Gormand26914d2014-04-03 14:47:24 -0700653 if (unlikely(!page && read_mems_allowed_retry(cpuset_mems_cookie)))
Mel Gormancc9a6c82012-03-21 16:34:11 -0700654 goto retry_cpuset;
655 return page;
656
Miao Xiec0ff7452010-05-24 14:32:08 -0700657err:
Mel Gormancc9a6c82012-03-21 16:34:11 -0700658 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
Luiz Capitulino1cac6f22014-06-04 16:07:11 -0700661/*
662 * common helper functions for hstate_next_node_to_{alloc|free}.
663 * We may have allocated or freed a huge page based on a different
664 * nodes_allowed previously, so h->next_node_to_{alloc|free} might
665 * be outside of *nodes_allowed. Ensure that we use an allowed
666 * node for alloc or free.
667 */
668static int next_node_allowed(int nid, nodemask_t *nodes_allowed)
669{
670 nid = next_node(nid, *nodes_allowed);
671 if (nid == MAX_NUMNODES)
672 nid = first_node(*nodes_allowed);
673 VM_BUG_ON(nid >= MAX_NUMNODES);
674
675 return nid;
676}
677
678static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed)
679{
680 if (!node_isset(nid, *nodes_allowed))
681 nid = next_node_allowed(nid, nodes_allowed);
682 return nid;
683}
684
685/*
686 * returns the previously saved node ["this node"] from which to
687 * allocate a persistent huge page for the pool and advance the
688 * next node from which to allocate, handling wrap at end of node
689 * mask.
690 */
691static int hstate_next_node_to_alloc(struct hstate *h,
692 nodemask_t *nodes_allowed)
693{
694 int nid;
695
696 VM_BUG_ON(!nodes_allowed);
697
698 nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed);
699 h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed);
700
701 return nid;
702}
703
704/*
705 * helper for free_pool_huge_page() - return the previously saved
706 * node ["this node"] from which to free a huge page. Advance the
707 * next node id whether or not we find a free huge page to free so
708 * that the next attempt to free addresses the next node.
709 */
710static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed)
711{
712 int nid;
713
714 VM_BUG_ON(!nodes_allowed);
715
716 nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed);
717 h->next_nid_to_free = next_node_allowed(nid, nodes_allowed);
718
719 return nid;
720}
721
722#define for_each_node_mask_to_alloc(hs, nr_nodes, node, mask) \
723 for (nr_nodes = nodes_weight(*mask); \
724 nr_nodes > 0 && \
725 ((node = hstate_next_node_to_alloc(hs, mask)) || 1); \
726 nr_nodes--)
727
728#define for_each_node_mask_to_free(hs, nr_nodes, node, mask) \
729 for (nr_nodes = nodes_weight(*mask); \
730 nr_nodes > 0 && \
731 ((node = hstate_next_node_to_free(hs, mask)) || 1); \
732 nr_nodes--)
733
Luiz Capitulino944d9fe2014-06-04 16:07:13 -0700734#if defined(CONFIG_CMA) && defined(CONFIG_X86_64)
735static void destroy_compound_gigantic_page(struct page *page,
736 unsigned long order)
737{
738 int i;
739 int nr_pages = 1 << order;
740 struct page *p = page + 1;
741
742 for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
743 __ClearPageTail(p);
744 set_page_refcounted(p);
745 p->first_page = NULL;
746 }
747
748 set_compound_order(page, 0);
749 __ClearPageHead(page);
750}
751
752static void free_gigantic_page(struct page *page, unsigned order)
753{
754 free_contig_range(page_to_pfn(page), 1 << order);
755}
756
757static int __alloc_gigantic_page(unsigned long start_pfn,
758 unsigned long nr_pages)
759{
760 unsigned long end_pfn = start_pfn + nr_pages;
761 return alloc_contig_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
762}
763
764static bool pfn_range_valid_gigantic(unsigned long start_pfn,
765 unsigned long nr_pages)
766{
767 unsigned long i, end_pfn = start_pfn + nr_pages;
768 struct page *page;
769
770 for (i = start_pfn; i < end_pfn; i++) {
771 if (!pfn_valid(i))
772 return false;
773
774 page = pfn_to_page(i);
775
776 if (PageReserved(page))
777 return false;
778
779 if (page_count(page) > 0)
780 return false;
781
782 if (PageHuge(page))
783 return false;
784 }
785
786 return true;
787}
788
789static bool zone_spans_last_pfn(const struct zone *zone,
790 unsigned long start_pfn, unsigned long nr_pages)
791{
792 unsigned long last_pfn = start_pfn + nr_pages - 1;
793 return zone_spans_pfn(zone, last_pfn);
794}
795
796static struct page *alloc_gigantic_page(int nid, unsigned order)
797{
798 unsigned long nr_pages = 1 << order;
799 unsigned long ret, pfn, flags;
800 struct zone *z;
801
802 z = NODE_DATA(nid)->node_zones;
803 for (; z - NODE_DATA(nid)->node_zones < MAX_NR_ZONES; z++) {
804 spin_lock_irqsave(&z->lock, flags);
805
806 pfn = ALIGN(z->zone_start_pfn, nr_pages);
807 while (zone_spans_last_pfn(z, pfn, nr_pages)) {
808 if (pfn_range_valid_gigantic(pfn, nr_pages)) {
809 /*
810 * We release the zone lock here because
811 * alloc_contig_range() will also lock the zone
812 * at some point. If there's an allocation
813 * spinning on this lock, it may win the race
814 * and cause alloc_contig_range() to fail...
815 */
816 spin_unlock_irqrestore(&z->lock, flags);
817 ret = __alloc_gigantic_page(pfn, nr_pages);
818 if (!ret)
819 return pfn_to_page(pfn);
820 spin_lock_irqsave(&z->lock, flags);
821 }
822 pfn += nr_pages;
823 }
824
825 spin_unlock_irqrestore(&z->lock, flags);
826 }
827
828 return NULL;
829}
830
831static void prep_new_huge_page(struct hstate *h, struct page *page, int nid);
832static void prep_compound_gigantic_page(struct page *page, unsigned long order);
833
834static struct page *alloc_fresh_gigantic_page_node(struct hstate *h, int nid)
835{
836 struct page *page;
837
838 page = alloc_gigantic_page(nid, huge_page_order(h));
839 if (page) {
840 prep_compound_gigantic_page(page, huge_page_order(h));
841 prep_new_huge_page(h, page, nid);
842 }
843
844 return page;
845}
846
847static int alloc_fresh_gigantic_page(struct hstate *h,
848 nodemask_t *nodes_allowed)
849{
850 struct page *page = NULL;
851 int nr_nodes, node;
852
853 for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
854 page = alloc_fresh_gigantic_page_node(h, node);
855 if (page)
856 return 1;
857 }
858
859 return 0;
860}
861
862static inline bool gigantic_page_supported(void) { return true; }
863#else
864static inline bool gigantic_page_supported(void) { return false; }
865static inline void free_gigantic_page(struct page *page, unsigned order) { }
866static inline void destroy_compound_gigantic_page(struct page *page,
867 unsigned long order) { }
868static inline int alloc_fresh_gigantic_page(struct hstate *h,
869 nodemask_t *nodes_allowed) { return 0; }
870#endif
871
Andi Kleena5516432008-07-23 21:27:41 -0700872static void update_and_free_page(struct hstate *h, struct page *page)
Adam Litke6af2acb2007-10-16 01:26:16 -0700873{
874 int i;
Andi Kleena5516432008-07-23 21:27:41 -0700875
Luiz Capitulino944d9fe2014-06-04 16:07:13 -0700876 if (hstate_is_gigantic(h) && !gigantic_page_supported())
877 return;
Andy Whitcroft18229df2008-11-06 12:53:27 -0800878
Andi Kleena5516432008-07-23 21:27:41 -0700879 h->nr_huge_pages--;
880 h->nr_huge_pages_node[page_to_nid(page)]--;
881 for (i = 0; i < pages_per_huge_page(h); i++) {
Chris Forbes32f84522011-07-25 17:12:14 -0700882 page[i].flags &= ~(1 << PG_locked | 1 << PG_error |
883 1 << PG_referenced | 1 << PG_dirty |
Luiz Capitulinoa7407a22014-06-04 16:07:09 -0700884 1 << PG_active | 1 << PG_private |
885 1 << PG_writeback);
Adam Litke6af2acb2007-10-16 01:26:16 -0700886 }
Sasha Levin309381fea2014-01-23 15:52:54 -0800887 VM_BUG_ON_PAGE(hugetlb_cgroup_from_page(page), page);
Adam Litke6af2acb2007-10-16 01:26:16 -0700888 set_compound_page_dtor(page, NULL);
889 set_page_refcounted(page);
Luiz Capitulino944d9fe2014-06-04 16:07:13 -0700890 if (hstate_is_gigantic(h)) {
891 destroy_compound_gigantic_page(page, huge_page_order(h));
892 free_gigantic_page(page, huge_page_order(h));
893 } else {
894 arch_release_hugepage(page);
895 __free_pages(page, huge_page_order(h));
896 }
Adam Litke6af2acb2007-10-16 01:26:16 -0700897}
898
Andi Kleene5ff2152008-07-23 21:27:42 -0700899struct hstate *size_to_hstate(unsigned long size)
900{
901 struct hstate *h;
902
903 for_each_hstate(h) {
904 if (huge_page_size(h) == size)
905 return h;
906 }
907 return NULL;
908}
909
Atsushi Kumagai8f1d26d2014-07-30 16:08:39 -0700910void free_huge_page(struct page *page)
David Gibson27a85ef2006-03-22 00:08:56 -0800911{
Andi Kleena5516432008-07-23 21:27:41 -0700912 /*
913 * Can't pass hstate in here because it is called from the
914 * compound page destructor.
915 */
Andi Kleene5ff2152008-07-23 21:27:42 -0700916 struct hstate *h = page_hstate(page);
Adam Litke7893d1d2007-10-16 01:26:18 -0700917 int nid = page_to_nid(page);
David Gibson90481622012-03-21 16:34:12 -0700918 struct hugepage_subpool *spool =
919 (struct hugepage_subpool *)page_private(page);
Joonsoo Kim07443a82013-09-11 14:21:58 -0700920 bool restore_reserve;
David Gibson27a85ef2006-03-22 00:08:56 -0800921
Andy Whitcrofte5df70a2008-02-23 15:23:32 -0800922 set_page_private(page, 0);
Mel Gorman23be7462010-04-23 13:17:56 -0400923 page->mapping = NULL;
Adam Litke7893d1d2007-10-16 01:26:18 -0700924 BUG_ON(page_count(page));
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +0900925 BUG_ON(page_mapcount(page));
Joonsoo Kim07443a82013-09-11 14:21:58 -0700926 restore_reserve = PagePrivate(page);
Joonsoo Kim16c794b2013-10-16 13:46:48 -0700927 ClearPagePrivate(page);
David Gibson27a85ef2006-03-22 00:08:56 -0800928
Mike Kravetz1c5ecae2015-04-15 16:13:39 -0700929 /*
930 * A return code of zero implies that the subpool will be under its
931 * minimum size if the reservation is not restored after page is free.
932 * Therefore, force restore_reserve operation.
933 */
934 if (hugepage_subpool_put_pages(spool, 1) == 0)
935 restore_reserve = true;
936
David Gibson27a85ef2006-03-22 00:08:56 -0800937 spin_lock(&hugetlb_lock);
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -0700938 hugetlb_cgroup_uncharge_page(hstate_index(h),
939 pages_per_huge_page(h), page);
Joonsoo Kim07443a82013-09-11 14:21:58 -0700940 if (restore_reserve)
941 h->resv_huge_pages++;
942
Luiz Capitulino944d9fe2014-06-04 16:07:13 -0700943 if (h->surplus_huge_pages_node[nid]) {
Aneesh Kumar K.V0edaecf2012-07-31 16:42:07 -0700944 /* remove the page from active list */
945 list_del(&page->lru);
Andi Kleena5516432008-07-23 21:27:41 -0700946 update_and_free_page(h, page);
947 h->surplus_huge_pages--;
948 h->surplus_huge_pages_node[nid]--;
Adam Litke7893d1d2007-10-16 01:26:18 -0700949 } else {
Will Deacon5d3a5512012-10-08 16:29:32 -0700950 arch_clear_hugepage_flags(page);
Andi Kleena5516432008-07-23 21:27:41 -0700951 enqueue_huge_page(h, page);
Adam Litke7893d1d2007-10-16 01:26:18 -0700952 }
David Gibson27a85ef2006-03-22 00:08:56 -0800953 spin_unlock(&hugetlb_lock);
954}
955
Andi Kleena5516432008-07-23 21:27:41 -0700956static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
Andi Kleenb7ba30c2008-07-23 21:27:40 -0700957{
Aneesh Kumar K.V0edaecf2012-07-31 16:42:07 -0700958 INIT_LIST_HEAD(&page->lru);
Andi Kleenb7ba30c2008-07-23 21:27:40 -0700959 set_compound_page_dtor(page, free_huge_page);
960 spin_lock(&hugetlb_lock);
Aneesh Kumar K.V9dd540e2012-07-31 16:42:15 -0700961 set_hugetlb_cgroup(page, NULL);
Andi Kleena5516432008-07-23 21:27:41 -0700962 h->nr_huge_pages++;
963 h->nr_huge_pages_node[nid]++;
Andi Kleenb7ba30c2008-07-23 21:27:40 -0700964 spin_unlock(&hugetlb_lock);
965 put_page(page); /* free it into the hugepage allocator */
966}
967
Luiz Capitulino2906dd52014-06-04 16:07:06 -0700968static void prep_compound_gigantic_page(struct page *page, unsigned long order)
Wu Fengguang20a03072009-06-16 15:32:22 -0700969{
970 int i;
971 int nr_pages = 1 << order;
972 struct page *p = page + 1;
973
974 /* we rely on prep_new_huge_page to set the destructor */
975 set_compound_order(page, order);
976 __SetPageHead(page);
Andrea Arcangelief5a22b2013-10-16 13:46:56 -0700977 __ClearPageReserved(page);
Wu Fengguang20a03072009-06-16 15:32:22 -0700978 for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
Andrea Arcangelief5a22b2013-10-16 13:46:56 -0700979 /*
980 * For gigantic hugepages allocated through bootmem at
981 * boot, it's safer to be consistent with the not-gigantic
982 * hugepages and clear the PG_reserved bit from all tail pages
983 * too. Otherwse drivers using get_user_pages() to access tail
984 * pages may get the reference counting wrong if they see
985 * PG_reserved set on a tail page (despite the head page not
986 * having PG_reserved set). Enforcing this consistency between
987 * head and tail pages allows drivers to optimize away a check
988 * on the head page when they need know if put_page() is needed
989 * after get_user_pages().
990 */
991 __ClearPageReserved(p);
Youquan Song58a84aa2011-12-08 14:34:18 -0800992 set_page_count(p, 0);
Wu Fengguang20a03072009-06-16 15:32:22 -0700993 p->first_page = page;
David Rientjes44fc8052015-03-12 16:25:54 -0700994 /* Make sure p->first_page is always valid for PageTail() */
995 smp_wmb();
996 __SetPageTail(p);
Wu Fengguang20a03072009-06-16 15:32:22 -0700997 }
998}
999
Andrew Morton77959122012-10-08 16:34:11 -07001000/*
1001 * PageHuge() only returns true for hugetlbfs pages, but not for normal or
1002 * transparent huge pages. See the PageTransHuge() documentation for more
1003 * details.
1004 */
Wu Fengguang20a03072009-06-16 15:32:22 -07001005int PageHuge(struct page *page)
1006{
Wu Fengguang20a03072009-06-16 15:32:22 -07001007 if (!PageCompound(page))
1008 return 0;
1009
1010 page = compound_head(page);
Andrew Morton758f66a2014-01-21 15:48:57 -08001011 return get_compound_page_dtor(page) == free_huge_page;
Wu Fengguang20a03072009-06-16 15:32:22 -07001012}
Naoya Horiguchi43131e12010-05-28 09:29:22 +09001013EXPORT_SYMBOL_GPL(PageHuge);
1014
Andrea Arcangeli27c73ae2013-11-21 14:32:02 -08001015/*
1016 * PageHeadHuge() only returns true for hugetlbfs head page, but not for
1017 * normal or transparent huge pages.
1018 */
1019int PageHeadHuge(struct page *page_head)
1020{
Andrea Arcangeli27c73ae2013-11-21 14:32:02 -08001021 if (!PageHead(page_head))
1022 return 0;
1023
Andrew Morton758f66a2014-01-21 15:48:57 -08001024 return get_compound_page_dtor(page_head) == free_huge_page;
Andrea Arcangeli27c73ae2013-11-21 14:32:02 -08001025}
Andrea Arcangeli27c73ae2013-11-21 14:32:02 -08001026
Zhang Yi13d60f42013-06-25 21:19:31 +08001027pgoff_t __basepage_index(struct page *page)
1028{
1029 struct page *page_head = compound_head(page);
1030 pgoff_t index = page_index(page_head);
1031 unsigned long compound_idx;
1032
1033 if (!PageHuge(page_head))
1034 return page_index(page);
1035
1036 if (compound_order(page_head) >= MAX_ORDER)
1037 compound_idx = page_to_pfn(page) - page_to_pfn(page_head);
1038 else
1039 compound_idx = page - page_head;
1040
1041 return (index << compound_order(page_head)) + compound_idx;
1042}
1043
Andi Kleena5516432008-07-23 21:27:41 -07001044static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 struct page *page;
Joe Jinf96efd52007-07-15 23:38:12 -07001047
Mel Gorman6484eb32009-06-16 15:31:54 -07001048 page = alloc_pages_exact_node(nid,
Naoya Horiguchi86cdb462013-09-11 14:22:13 -07001049 htlb_alloc_mask(h)|__GFP_COMP|__GFP_THISNODE|
Nishanth Aravamudan551883a2008-04-29 00:58:26 -07001050 __GFP_REPEAT|__GFP_NOWARN,
Andi Kleena5516432008-07-23 21:27:41 -07001051 huge_page_order(h));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (page) {
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001053 if (arch_prepare_hugepage(page)) {
Gerald Schaefercaff3a22008-08-12 15:08:38 -07001054 __free_pages(page, huge_page_order(h));
Harvey Harrison7b8ee842008-04-28 14:13:19 -07001055 return NULL;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07001056 }
Andi Kleena5516432008-07-23 21:27:41 -07001057 prep_new_huge_page(h, page, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
Nishanth Aravamudan63b46132007-10-16 01:26:24 -07001059
1060 return page;
1061}
1062
Joonsoo Kimb2261022013-09-11 14:21:00 -07001063static int alloc_fresh_huge_page(struct hstate *h, nodemask_t *nodes_allowed)
1064{
1065 struct page *page;
1066 int nr_nodes, node;
1067 int ret = 0;
1068
1069 for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
1070 page = alloc_fresh_huge_page_node(h, node);
1071 if (page) {
1072 ret = 1;
1073 break;
1074 }
1075 }
1076
1077 if (ret)
1078 count_vm_event(HTLB_BUDDY_PGALLOC);
1079 else
1080 count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
1081
1082 return ret;
1083}
1084
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001085/*
1086 * Free huge page from pool from next node to free.
1087 * Attempt to keep persistent huge pages more or less
1088 * balanced over allowed nodes.
1089 * Called with hugetlb_lock locked.
1090 */
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08001091static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
1092 bool acct_surplus)
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001093{
Joonsoo Kimb2261022013-09-11 14:21:00 -07001094 int nr_nodes, node;
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001095 int ret = 0;
1096
Joonsoo Kimb2261022013-09-11 14:21:00 -07001097 for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
Lee Schermerhorn685f3452009-09-21 17:01:23 -07001098 /*
1099 * If we're returning unused surplus pages, only examine
1100 * nodes with surplus pages.
1101 */
Joonsoo Kimb2261022013-09-11 14:21:00 -07001102 if ((!acct_surplus || h->surplus_huge_pages_node[node]) &&
1103 !list_empty(&h->hugepage_freelists[node])) {
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001104 struct page *page =
Joonsoo Kimb2261022013-09-11 14:21:00 -07001105 list_entry(h->hugepage_freelists[node].next,
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001106 struct page, lru);
1107 list_del(&page->lru);
1108 h->free_huge_pages--;
Joonsoo Kimb2261022013-09-11 14:21:00 -07001109 h->free_huge_pages_node[node]--;
Lee Schermerhorn685f3452009-09-21 17:01:23 -07001110 if (acct_surplus) {
1111 h->surplus_huge_pages--;
Joonsoo Kimb2261022013-09-11 14:21:00 -07001112 h->surplus_huge_pages_node[node]--;
Lee Schermerhorn685f3452009-09-21 17:01:23 -07001113 }
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001114 update_and_free_page(h, page);
1115 ret = 1;
Lee Schermerhorn9a76db02009-12-14 17:58:15 -08001116 break;
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001117 }
Joonsoo Kimb2261022013-09-11 14:21:00 -07001118 }
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001119
1120 return ret;
1121}
1122
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001123/*
1124 * Dissolve a given free hugepage into free buddy pages. This function does
1125 * nothing for in-use (including surplus) hugepages.
1126 */
1127static void dissolve_free_huge_page(struct page *page)
1128{
1129 spin_lock(&hugetlb_lock);
1130 if (PageHuge(page) && !page_count(page)) {
1131 struct hstate *h = page_hstate(page);
1132 int nid = page_to_nid(page);
1133 list_del(&page->lru);
1134 h->free_huge_pages--;
1135 h->free_huge_pages_node[nid]--;
1136 update_and_free_page(h, page);
1137 }
1138 spin_unlock(&hugetlb_lock);
1139}
1140
1141/*
1142 * Dissolve free hugepages in a given pfn range. Used by memory hotplug to
1143 * make specified memory blocks removable from the system.
1144 * Note that start_pfn should aligned with (minimum) hugepage size.
1145 */
1146void dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn)
1147{
1148 unsigned int order = 8 * sizeof(void *);
1149 unsigned long pfn;
1150 struct hstate *h;
1151
Li Zhongd0177632014-08-06 16:07:56 -07001152 if (!hugepages_supported())
1153 return;
1154
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07001155 /* Set scan step to minimum hugepage size */
1156 for_each_hstate(h)
1157 if (order > huge_page_order(h))
1158 order = huge_page_order(h);
1159 VM_BUG_ON(!IS_ALIGNED(start_pfn, 1 << order));
1160 for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << order)
1161 dissolve_free_huge_page(pfn_to_page(pfn));
1162}
1163
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001164static struct page *alloc_buddy_huge_page(struct hstate *h, int nid)
Adam Litke7893d1d2007-10-16 01:26:18 -07001165{
1166 struct page *page;
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001167 unsigned int r_nid;
Adam Litke7893d1d2007-10-16 01:26:18 -07001168
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07001169 if (hstate_is_gigantic(h))
Andi Kleenaa888a72008-07-23 21:27:47 -07001170 return NULL;
1171
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001172 /*
1173 * Assume we will successfully allocate the surplus page to
1174 * prevent racing processes from causing the surplus to exceed
1175 * overcommit
1176 *
1177 * This however introduces a different race, where a process B
1178 * tries to grow the static hugepage pool while alloc_pages() is
1179 * called by process A. B will only examine the per-node
1180 * counters in determining if surplus huge pages can be
1181 * converted to normal huge pages in adjust_pool_surplus(). A
1182 * won't be able to increment the per-node counter, until the
1183 * lock is dropped by B, but B doesn't drop hugetlb_lock until
1184 * no more huge pages can be converted from surplus to normal
1185 * state (and doesn't try to convert again). Thus, we have a
1186 * case where a surplus huge page exists, the pool is grown, and
1187 * the surplus huge page still exists after, even though it
1188 * should just have been converted to a normal huge page. This
1189 * does not leak memory, though, as the hugepage will be freed
1190 * once it is out of use. It also does not allow the counters to
1191 * go out of whack in adjust_pool_surplus() as we don't modify
1192 * the node values until we've gotten the hugepage and only the
1193 * per-node value is checked there.
1194 */
1195 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -07001196 if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001197 spin_unlock(&hugetlb_lock);
1198 return NULL;
1199 } else {
Andi Kleena5516432008-07-23 21:27:41 -07001200 h->nr_huge_pages++;
1201 h->surplus_huge_pages++;
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001202 }
1203 spin_unlock(&hugetlb_lock);
1204
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001205 if (nid == NUMA_NO_NODE)
Naoya Horiguchi86cdb462013-09-11 14:22:13 -07001206 page = alloc_pages(htlb_alloc_mask(h)|__GFP_COMP|
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001207 __GFP_REPEAT|__GFP_NOWARN,
1208 huge_page_order(h));
1209 else
1210 page = alloc_pages_exact_node(nid,
Naoya Horiguchi86cdb462013-09-11 14:22:13 -07001211 htlb_alloc_mask(h)|__GFP_COMP|__GFP_THISNODE|
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001212 __GFP_REPEAT|__GFP_NOWARN, huge_page_order(h));
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001213
Gerald Schaefercaff3a22008-08-12 15:08:38 -07001214 if (page && arch_prepare_hugepage(page)) {
1215 __free_pages(page, huge_page_order(h));
Hillf Dantonea5768c2012-01-10 15:08:30 -08001216 page = NULL;
Gerald Schaefercaff3a22008-08-12 15:08:38 -07001217 }
1218
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001219 spin_lock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -07001220 if (page) {
Aneesh Kumar K.V0edaecf2012-07-31 16:42:07 -07001221 INIT_LIST_HEAD(&page->lru);
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001222 r_nid = page_to_nid(page);
Adam Litke7893d1d2007-10-16 01:26:18 -07001223 set_compound_page_dtor(page, free_huge_page);
Aneesh Kumar K.V9dd540e2012-07-31 16:42:15 -07001224 set_hugetlb_cgroup(page, NULL);
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001225 /*
1226 * We incremented the global counters already
1227 */
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001228 h->nr_huge_pages_node[r_nid]++;
1229 h->surplus_huge_pages_node[r_nid]++;
Adam Litke3b116302008-04-28 02:13:06 -07001230 __count_vm_event(HTLB_BUDDY_PGALLOC);
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001231 } else {
Andi Kleena5516432008-07-23 21:27:41 -07001232 h->nr_huge_pages--;
1233 h->surplus_huge_pages--;
Adam Litke3b116302008-04-28 02:13:06 -07001234 __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
Adam Litke7893d1d2007-10-16 01:26:18 -07001235 }
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001236 spin_unlock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -07001237
1238 return page;
1239}
1240
Adam Litkee4e574b2007-10-16 01:26:19 -07001241/*
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001242 * This allocation function is useful in the context where vma is irrelevant.
1243 * E.g. soft-offlining uses this function because it only cares physical
1244 * address of error page.
1245 */
1246struct page *alloc_huge_page_node(struct hstate *h, int nid)
1247{
Joonsoo Kim4ef91842013-09-11 14:21:51 -07001248 struct page *page = NULL;
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001249
1250 spin_lock(&hugetlb_lock);
Joonsoo Kim4ef91842013-09-11 14:21:51 -07001251 if (h->free_huge_pages - h->resv_huge_pages > 0)
1252 page = dequeue_huge_page_node(h, nid);
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001253 spin_unlock(&hugetlb_lock);
1254
Aneesh Kumar K.V94ae8ba2012-07-31 16:42:35 -07001255 if (!page)
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001256 page = alloc_buddy_huge_page(h, nid);
1257
1258 return page;
1259}
1260
1261/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001262 * Increase the hugetlb pool such that it can accommodate a reservation
Adam Litkee4e574b2007-10-16 01:26:19 -07001263 * of size 'delta'.
1264 */
Andi Kleena5516432008-07-23 21:27:41 -07001265static int gather_surplus_pages(struct hstate *h, int delta)
Adam Litkee4e574b2007-10-16 01:26:19 -07001266{
1267 struct list_head surplus_list;
1268 struct page *page, *tmp;
1269 int ret, i;
1270 int needed, allocated;
Hillf Danton28073b02012-03-21 16:34:00 -07001271 bool alloc_ok = true;
Adam Litkee4e574b2007-10-16 01:26:19 -07001272
Andi Kleena5516432008-07-23 21:27:41 -07001273 needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
Adam Litkeac09b3a2008-03-04 14:29:38 -08001274 if (needed <= 0) {
Andi Kleena5516432008-07-23 21:27:41 -07001275 h->resv_huge_pages += delta;
Adam Litkee4e574b2007-10-16 01:26:19 -07001276 return 0;
Adam Litkeac09b3a2008-03-04 14:29:38 -08001277 }
Adam Litkee4e574b2007-10-16 01:26:19 -07001278
1279 allocated = 0;
1280 INIT_LIST_HEAD(&surplus_list);
1281
1282 ret = -ENOMEM;
1283retry:
1284 spin_unlock(&hugetlb_lock);
1285 for (i = 0; i < needed; i++) {
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001286 page = alloc_buddy_huge_page(h, NUMA_NO_NODE);
Hillf Danton28073b02012-03-21 16:34:00 -07001287 if (!page) {
1288 alloc_ok = false;
1289 break;
1290 }
Adam Litkee4e574b2007-10-16 01:26:19 -07001291 list_add(&page->lru, &surplus_list);
1292 }
Hillf Danton28073b02012-03-21 16:34:00 -07001293 allocated += i;
Adam Litkee4e574b2007-10-16 01:26:19 -07001294
1295 /*
1296 * After retaking hugetlb_lock, we need to recalculate 'needed'
1297 * because either resv_huge_pages or free_huge_pages may have changed.
1298 */
1299 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -07001300 needed = (h->resv_huge_pages + delta) -
1301 (h->free_huge_pages + allocated);
Hillf Danton28073b02012-03-21 16:34:00 -07001302 if (needed > 0) {
1303 if (alloc_ok)
1304 goto retry;
1305 /*
1306 * We were not able to allocate enough pages to
1307 * satisfy the entire reservation so we free what
1308 * we've allocated so far.
1309 */
1310 goto free;
1311 }
Adam Litkee4e574b2007-10-16 01:26:19 -07001312 /*
1313 * The surplus_list now contains _at_least_ the number of extra pages
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001314 * needed to accommodate the reservation. Add the appropriate number
Adam Litkee4e574b2007-10-16 01:26:19 -07001315 * of pages to the hugetlb pool and free the extras back to the buddy
Adam Litkeac09b3a2008-03-04 14:29:38 -08001316 * allocator. Commit the entire reservation here to prevent another
1317 * process from stealing the pages as they are added to the pool but
1318 * before they are reserved.
Adam Litkee4e574b2007-10-16 01:26:19 -07001319 */
1320 needed += allocated;
Andi Kleena5516432008-07-23 21:27:41 -07001321 h->resv_huge_pages += delta;
Adam Litkee4e574b2007-10-16 01:26:19 -07001322 ret = 0;
Naoya Horiguchia9869b82010-09-08 10:19:37 +09001323
Adam Litke19fc3f02008-04-28 02:12:20 -07001324 /* Free the needed pages to the hugetlb pool */
Adam Litkee4e574b2007-10-16 01:26:19 -07001325 list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
Adam Litke19fc3f02008-04-28 02:12:20 -07001326 if ((--needed) < 0)
1327 break;
Naoya Horiguchia9869b82010-09-08 10:19:37 +09001328 /*
1329 * This page is now managed by the hugetlb allocator and has
1330 * no users -- drop the buddy allocator's reference.
1331 */
1332 put_page_testzero(page);
Sasha Levin309381fea2014-01-23 15:52:54 -08001333 VM_BUG_ON_PAGE(page_count(page), page);
Andi Kleena5516432008-07-23 21:27:41 -07001334 enqueue_huge_page(h, page);
Adam Litke19fc3f02008-04-28 02:12:20 -07001335 }
Hillf Danton28073b02012-03-21 16:34:00 -07001336free:
Hillf Dantonb0365c82011-12-28 15:57:16 -08001337 spin_unlock(&hugetlb_lock);
Adam Litke19fc3f02008-04-28 02:12:20 -07001338
1339 /* Free unnecessary surplus pages to the buddy allocator */
Joonsoo Kimc0d934b2013-09-11 14:21:02 -07001340 list_for_each_entry_safe(page, tmp, &surplus_list, lru)
1341 put_page(page);
Naoya Horiguchia9869b82010-09-08 10:19:37 +09001342 spin_lock(&hugetlb_lock);
Adam Litkee4e574b2007-10-16 01:26:19 -07001343
1344 return ret;
1345}
1346
1347/*
1348 * When releasing a hugetlb pool reservation, any surplus pages that were
1349 * allocated to satisfy the reservation must be explicitly freed if they were
1350 * never used.
Lee Schermerhorn685f3452009-09-21 17:01:23 -07001351 * Called with hugetlb_lock held.
Adam Litkee4e574b2007-10-16 01:26:19 -07001352 */
Andi Kleena5516432008-07-23 21:27:41 -07001353static void return_unused_surplus_pages(struct hstate *h,
1354 unsigned long unused_resv_pages)
Adam Litkee4e574b2007-10-16 01:26:19 -07001355{
Adam Litkee4e574b2007-10-16 01:26:19 -07001356 unsigned long nr_pages;
1357
Adam Litkeac09b3a2008-03-04 14:29:38 -08001358 /* Uncommit the reservation */
Andi Kleena5516432008-07-23 21:27:41 -07001359 h->resv_huge_pages -= unused_resv_pages;
Adam Litkeac09b3a2008-03-04 14:29:38 -08001360
Andi Kleenaa888a72008-07-23 21:27:47 -07001361 /* Cannot return gigantic pages currently */
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07001362 if (hstate_is_gigantic(h))
Andi Kleenaa888a72008-07-23 21:27:47 -07001363 return;
1364
Andi Kleena5516432008-07-23 21:27:41 -07001365 nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
Adam Litkee4e574b2007-10-16 01:26:19 -07001366
Lee Schermerhorn685f3452009-09-21 17:01:23 -07001367 /*
1368 * We want to release as many surplus pages as possible, spread
Lee Schermerhorn9b5e5d02009-12-14 17:58:32 -08001369 * evenly across all nodes with memory. Iterate across these nodes
1370 * until we can no longer free unreserved surplus pages. This occurs
1371 * when the nodes with surplus pages have no free pages.
1372 * free_pool_huge_page() will balance the the freed pages across the
1373 * on-line nodes with memory and will handle the hstate accounting.
Lee Schermerhorn685f3452009-09-21 17:01:23 -07001374 */
1375 while (nr_pages--) {
Lai Jiangshan8cebfcd2012-12-12 13:51:36 -08001376 if (!free_pool_huge_page(h, &node_states[N_MEMORY], 1))
Lee Schermerhorn685f3452009-09-21 17:01:23 -07001377 break;
Mizuma, Masayoshi7848a4b2014-04-18 15:07:18 -07001378 cond_resched_lock(&hugetlb_lock);
Adam Litkee4e574b2007-10-16 01:26:19 -07001379 }
1380}
1381
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001382/*
1383 * Determine if the huge page at addr within the vma has an associated
1384 * reservation. Where it does not we will need to logically increase
David Gibson90481622012-03-21 16:34:12 -07001385 * reservation and actually increase subpool usage before an allocation
1386 * can occur. Where any new reservation would be required the
1387 * reservation change is prepared, but not committed. Once the page
1388 * has been allocated from the subpool and instantiated the change should
1389 * be committed via vma_commit_reservation. No action is required on
1390 * failure.
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001391 */
Roel Kluine2f17d92009-03-31 15:23:15 -07001392static long vma_needs_reservation(struct hstate *h,
Andi Kleena5516432008-07-23 21:27:41 -07001393 struct vm_area_struct *vma, unsigned long addr)
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001394{
Joonsoo Kim4e35f482014-04-03 14:47:30 -07001395 struct resv_map *resv;
1396 pgoff_t idx;
1397 long chg;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001398
Joonsoo Kim4e35f482014-04-03 14:47:30 -07001399 resv = vma_resv_map(vma);
1400 if (!resv)
Andy Whitcroft84afd992008-07-23 21:27:32 -07001401 return 1;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001402
Joonsoo Kim4e35f482014-04-03 14:47:30 -07001403 idx = vma_hugecache_offset(h, vma, addr);
1404 chg = region_chg(resv, idx, idx + 1);
Andy Whitcroft84afd992008-07-23 21:27:32 -07001405
Joonsoo Kim4e35f482014-04-03 14:47:30 -07001406 if (vma->vm_flags & VM_MAYSHARE)
1407 return chg;
1408 else
1409 return chg < 0 ? chg : 0;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001410}
Andi Kleena5516432008-07-23 21:27:41 -07001411static void vma_commit_reservation(struct hstate *h,
1412 struct vm_area_struct *vma, unsigned long addr)
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001413{
Joonsoo Kim4e35f482014-04-03 14:47:30 -07001414 struct resv_map *resv;
1415 pgoff_t idx;
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001416
Joonsoo Kim4e35f482014-04-03 14:47:30 -07001417 resv = vma_resv_map(vma);
1418 if (!resv)
1419 return;
Joonsoo Kim9119a412014-04-03 14:47:25 -07001420
Joonsoo Kim4e35f482014-04-03 14:47:30 -07001421 idx = vma_hugecache_offset(h, vma, addr);
1422 region_add(resv, idx, idx + 1);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001423}
1424
David Gibson27a85ef2006-03-22 00:08:56 -08001425static struct page *alloc_huge_page(struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07001426 unsigned long addr, int avoid_reserve)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
David Gibson90481622012-03-21 16:34:12 -07001428 struct hugepage_subpool *spool = subpool_vma(vma);
Andi Kleena5516432008-07-23 21:27:41 -07001429 struct hstate *h = hstate_vma(vma);
Adam Litke348ea202007-11-14 16:59:37 -08001430 struct page *page;
Roel Kluine2f17d92009-03-31 15:23:15 -07001431 long chg;
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -07001432 int ret, idx;
1433 struct hugetlb_cgroup *h_cg;
Adam Litke2fc39ce2007-11-14 16:59:39 -08001434
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -07001435 idx = hstate_index(h);
Mel Gormana1e78772008-07-23 21:27:23 -07001436 /*
David Gibson90481622012-03-21 16:34:12 -07001437 * Processes that did not create the mapping will have no
1438 * reserves and will not have accounted against subpool
1439 * limit. Check that the subpool limit can be made before
1440 * satisfying the allocation MAP_NORESERVE mappings may also
1441 * need pages and subpool limit allocated allocated if no reserve
1442 * mapping overlaps.
Mel Gormana1e78772008-07-23 21:27:23 -07001443 */
Andi Kleena5516432008-07-23 21:27:41 -07001444 chg = vma_needs_reservation(h, vma, addr);
Andy Whitcroftc37f9fb2008-07-23 21:27:30 -07001445 if (chg < 0)
Aneesh Kumar K.V76dcee72012-07-31 16:41:57 -07001446 return ERR_PTR(-ENOMEM);
Joonsoo Kim8bb3f122013-09-11 14:21:54 -07001447 if (chg || avoid_reserve)
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07001448 if (hugepage_subpool_get_pages(spool, 1) < 0)
Aneesh Kumar K.V76dcee72012-07-31 16:41:57 -07001449 return ERR_PTR(-ENOSPC);
Mel Gormana1e78772008-07-23 21:27:23 -07001450
Aneesh Kumar K.V6d76dcf2012-07-31 16:42:18 -07001451 ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg);
Jianyu Zhan8f34af62014-06-04 16:10:36 -07001452 if (ret)
1453 goto out_subpool_put;
1454
Mel Gormana1e78772008-07-23 21:27:23 -07001455 spin_lock(&hugetlb_lock);
Joonsoo Kimaf0ed732013-09-11 14:21:18 -07001456 page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve, chg);
Joonsoo Kim81a6fca2013-09-11 14:20:58 -07001457 if (!page) {
Aneesh Kumar K.V94ae8ba2012-07-31 16:42:35 -07001458 spin_unlock(&hugetlb_lock);
Naoya Horiguchibf50bab2010-09-08 10:19:33 +09001459 page = alloc_buddy_huge_page(h, NUMA_NO_NODE);
Jianyu Zhan8f34af62014-06-04 16:10:36 -07001460 if (!page)
1461 goto out_uncharge_cgroup;
1462
Aneesh Kumar K.V79dbb232012-07-31 16:42:32 -07001463 spin_lock(&hugetlb_lock);
1464 list_move(&page->lru, &h->hugepage_activelist);
Joonsoo Kim81a6fca2013-09-11 14:20:58 -07001465 /* Fall through */
Mel Gormana1e78772008-07-23 21:27:23 -07001466 }
Joonsoo Kim81a6fca2013-09-11 14:20:58 -07001467 hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h), h_cg, page);
1468 spin_unlock(&hugetlb_lock);
Mel Gormana1e78772008-07-23 21:27:23 -07001469
David Gibson90481622012-03-21 16:34:12 -07001470 set_page_private(page, (unsigned long)spool);
Mel Gormana1e78772008-07-23 21:27:23 -07001471
Andi Kleena5516432008-07-23 21:27:41 -07001472 vma_commit_reservation(h, vma, addr);
Adam Litke90d8b7e2007-11-14 16:59:42 -08001473 return page;
Jianyu Zhan8f34af62014-06-04 16:10:36 -07001474
1475out_uncharge_cgroup:
1476 hugetlb_cgroup_uncharge_cgroup(idx, pages_per_huge_page(h), h_cg);
1477out_subpool_put:
1478 if (chg || avoid_reserve)
1479 hugepage_subpool_put_pages(spool, 1);
1480 return ERR_PTR(-ENOSPC);
David Gibsonb45b5bd2006-03-22 00:08:55 -08001481}
1482
Naoya Horiguchi74060e42013-09-11 14:22:06 -07001483/*
1484 * alloc_huge_page()'s wrapper which simply returns the page if allocation
1485 * succeeds, otherwise NULL. This function is called from new_vma_page(),
1486 * where no ERR_VALUE is expected to be returned.
1487 */
1488struct page *alloc_huge_page_noerr(struct vm_area_struct *vma,
1489 unsigned long addr, int avoid_reserve)
1490{
1491 struct page *page = alloc_huge_page(vma, addr, avoid_reserve);
1492 if (IS_ERR(page))
1493 page = NULL;
1494 return page;
1495}
1496
Cyrill Gorcunov91f47662009-01-06 14:40:33 -08001497int __weak alloc_bootmem_huge_page(struct hstate *h)
Andi Kleenaa888a72008-07-23 21:27:47 -07001498{
1499 struct huge_bootmem_page *m;
Joonsoo Kimb2261022013-09-11 14:21:00 -07001500 int nr_nodes, node;
Andi Kleenaa888a72008-07-23 21:27:47 -07001501
Joonsoo Kimb2261022013-09-11 14:21:00 -07001502 for_each_node_mask_to_alloc(h, nr_nodes, node, &node_states[N_MEMORY]) {
Andi Kleenaa888a72008-07-23 21:27:47 -07001503 void *addr;
1504
Grygorii Strashko8b89a112014-01-21 15:50:36 -08001505 addr = memblock_virt_alloc_try_nid_nopanic(
1506 huge_page_size(h), huge_page_size(h),
1507 0, BOOTMEM_ALLOC_ACCESSIBLE, node);
Andi Kleenaa888a72008-07-23 21:27:47 -07001508 if (addr) {
1509 /*
1510 * Use the beginning of the huge page to store the
1511 * huge_bootmem_page struct (until gather_bootmem
1512 * puts them into the mem_map).
1513 */
1514 m = addr;
Cyrill Gorcunov91f47662009-01-06 14:40:33 -08001515 goto found;
Andi Kleenaa888a72008-07-23 21:27:47 -07001516 }
Andi Kleenaa888a72008-07-23 21:27:47 -07001517 }
1518 return 0;
1519
1520found:
Luiz Capitulinodf994ea2014-12-12 16:55:21 -08001521 BUG_ON(!IS_ALIGNED(virt_to_phys(m), huge_page_size(h)));
Andi Kleenaa888a72008-07-23 21:27:47 -07001522 /* Put them into a private list first because mem_map is not up yet */
1523 list_add(&m->list, &huge_boot_pages);
1524 m->hstate = h;
1525 return 1;
1526}
1527
David Rientjesf412c972014-04-03 14:47:59 -07001528static void __init prep_compound_huge_page(struct page *page, int order)
Andy Whitcroft18229df2008-11-06 12:53:27 -08001529{
1530 if (unlikely(order > (MAX_ORDER - 1)))
1531 prep_compound_gigantic_page(page, order);
1532 else
1533 prep_compound_page(page, order);
1534}
1535
Andi Kleenaa888a72008-07-23 21:27:47 -07001536/* Put bootmem huge pages into the standard lists after mem_map is up */
1537static void __init gather_bootmem_prealloc(void)
1538{
1539 struct huge_bootmem_page *m;
1540
1541 list_for_each_entry(m, &huge_boot_pages, list) {
Andi Kleenaa888a72008-07-23 21:27:47 -07001542 struct hstate *h = m->hstate;
Becky Bruceee8f2482011-07-25 17:11:50 -07001543 struct page *page;
1544
1545#ifdef CONFIG_HIGHMEM
1546 page = pfn_to_page(m->phys >> PAGE_SHIFT);
Grygorii Strashko8b89a112014-01-21 15:50:36 -08001547 memblock_free_late(__pa(m),
1548 sizeof(struct huge_bootmem_page));
Becky Bruceee8f2482011-07-25 17:11:50 -07001549#else
1550 page = virt_to_page(m);
1551#endif
Andi Kleenaa888a72008-07-23 21:27:47 -07001552 WARN_ON(page_count(page) != 1);
Andy Whitcroft18229df2008-11-06 12:53:27 -08001553 prep_compound_huge_page(page, h->order);
Andrea Arcangelief5a22b2013-10-16 13:46:56 -07001554 WARN_ON(PageReserved(page));
Andi Kleenaa888a72008-07-23 21:27:47 -07001555 prep_new_huge_page(h, page, page_to_nid(page));
Rafael Aquinib0320c72011-06-15 15:08:39 -07001556 /*
1557 * If we had gigantic hugepages allocated at boot time, we need
1558 * to restore the 'stolen' pages to totalram_pages in order to
1559 * fix confusing memory reports from free(1) and another
1560 * side-effects, like CommitLimit going negative.
1561 */
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07001562 if (hstate_is_gigantic(h))
Jiang Liu3dcc0572013-07-03 15:03:21 -07001563 adjust_managed_page_count(page, 1 << h->order);
Andi Kleenaa888a72008-07-23 21:27:47 -07001564 }
1565}
1566
Andi Kleen8faa8b02008-07-23 21:27:48 -07001567static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
1569 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Andi Kleene5ff2152008-07-23 21:27:42 -07001571 for (i = 0; i < h->max_huge_pages; ++i) {
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07001572 if (hstate_is_gigantic(h)) {
Andi Kleenaa888a72008-07-23 21:27:47 -07001573 if (!alloc_bootmem_huge_page(h))
1574 break;
Lee Schermerhorn9b5e5d02009-12-14 17:58:32 -08001575 } else if (!alloc_fresh_huge_page(h,
Lai Jiangshan8cebfcd2012-12-12 13:51:36 -08001576 &node_states[N_MEMORY]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 }
Andi Kleen8faa8b02008-07-23 21:27:48 -07001579 h->max_huge_pages = i;
Andi Kleene5ff2152008-07-23 21:27:42 -07001580}
1581
1582static void __init hugetlb_init_hstates(void)
1583{
1584 struct hstate *h;
1585
1586 for_each_hstate(h) {
Andi Kleen8faa8b02008-07-23 21:27:48 -07001587 /* oversize hugepages were init'ed in early boot */
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07001588 if (!hstate_is_gigantic(h))
Andi Kleen8faa8b02008-07-23 21:27:48 -07001589 hugetlb_hstate_alloc_pages(h);
Andi Kleene5ff2152008-07-23 21:27:42 -07001590 }
1591}
1592
Andi Kleen4abd32d2008-07-23 21:27:49 -07001593static char * __init memfmt(char *buf, unsigned long n)
1594{
1595 if (n >= (1UL << 30))
1596 sprintf(buf, "%lu GB", n >> 30);
1597 else if (n >= (1UL << 20))
1598 sprintf(buf, "%lu MB", n >> 20);
1599 else
1600 sprintf(buf, "%lu KB", n >> 10);
1601 return buf;
1602}
1603
Andi Kleene5ff2152008-07-23 21:27:42 -07001604static void __init report_hugepages(void)
1605{
1606 struct hstate *h;
1607
1608 for_each_hstate(h) {
Andi Kleen4abd32d2008-07-23 21:27:49 -07001609 char buf[32];
Andrew Mortonffb22af2013-02-22 16:32:08 -08001610 pr_info("HugeTLB registered %s page size, pre-allocated %ld pages\n",
Andi Kleen4abd32d2008-07-23 21:27:49 -07001611 memfmt(buf, huge_page_size(h)),
1612 h->free_huge_pages);
Andi Kleene5ff2152008-07-23 21:27:42 -07001613 }
1614}
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616#ifdef CONFIG_HIGHMEM
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08001617static void try_to_free_low(struct hstate *h, unsigned long count,
1618 nodemask_t *nodes_allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
Christoph Lameter4415cc82006-09-25 23:31:55 -07001620 int i;
1621
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07001622 if (hstate_is_gigantic(h))
Andi Kleenaa888a72008-07-23 21:27:47 -07001623 return;
1624
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08001625 for_each_node_mask(i, *nodes_allowed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 struct page *page, *next;
Andi Kleena5516432008-07-23 21:27:41 -07001627 struct list_head *freel = &h->hugepage_freelists[i];
1628 list_for_each_entry_safe(page, next, freel, lru) {
1629 if (count >= h->nr_huge_pages)
Adam Litke6b0c8802007-10-16 01:26:23 -07001630 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (PageHighMem(page))
1632 continue;
1633 list_del(&page->lru);
Andi Kleene5ff2152008-07-23 21:27:42 -07001634 update_and_free_page(h, page);
Andi Kleena5516432008-07-23 21:27:41 -07001635 h->free_huge_pages--;
1636 h->free_huge_pages_node[page_to_nid(page)]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
1638 }
1639}
1640#else
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08001641static inline void try_to_free_low(struct hstate *h, unsigned long count,
1642 nodemask_t *nodes_allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
1644}
1645#endif
1646
Wu Fengguang20a03072009-06-16 15:32:22 -07001647/*
1648 * Increment or decrement surplus_huge_pages. Keep node-specific counters
1649 * balanced by operating on them in a round-robin fashion.
1650 * Returns 1 if an adjustment was made.
1651 */
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08001652static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed,
1653 int delta)
Wu Fengguang20a03072009-06-16 15:32:22 -07001654{
Joonsoo Kimb2261022013-09-11 14:21:00 -07001655 int nr_nodes, node;
Wu Fengguang20a03072009-06-16 15:32:22 -07001656
1657 VM_BUG_ON(delta != -1 && delta != 1);
Wu Fengguang20a03072009-06-16 15:32:22 -07001658
Joonsoo Kimb2261022013-09-11 14:21:00 -07001659 if (delta < 0) {
1660 for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
1661 if (h->surplus_huge_pages_node[node])
1662 goto found;
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001663 }
Joonsoo Kimb2261022013-09-11 14:21:00 -07001664 } else {
1665 for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
1666 if (h->surplus_huge_pages_node[node] <
1667 h->nr_huge_pages_node[node])
1668 goto found;
Lee Schermerhorne8c5c822009-09-21 17:01:22 -07001669 }
Joonsoo Kimb2261022013-09-11 14:21:00 -07001670 }
1671 return 0;
Wu Fengguang20a03072009-06-16 15:32:22 -07001672
Joonsoo Kimb2261022013-09-11 14:21:00 -07001673found:
1674 h->surplus_huge_pages += delta;
1675 h->surplus_huge_pages_node[node] += delta;
1676 return 1;
Wu Fengguang20a03072009-06-16 15:32:22 -07001677}
1678
Andi Kleena5516432008-07-23 21:27:41 -07001679#define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08001680static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count,
1681 nodemask_t *nodes_allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
Adam Litke7893d1d2007-10-16 01:26:18 -07001683 unsigned long min_count, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001685 if (hstate_is_gigantic(h) && !gigantic_page_supported())
Andi Kleenaa888a72008-07-23 21:27:47 -07001686 return h->max_huge_pages;
1687
Adam Litke7893d1d2007-10-16 01:26:18 -07001688 /*
1689 * Increase the pool size
1690 * First take pages out of surplus state. Then make up the
1691 * remaining difference by allocating fresh huge pages.
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001692 *
1693 * We might race with alloc_buddy_huge_page() here and be unable
1694 * to convert a surplus huge page to a normal huge page. That is
1695 * not critical, though, it just means the overall size of the
1696 * pool might be one hugepage larger than it needs to be, but
1697 * within all the constraints specified by the sysctls.
Adam Litke7893d1d2007-10-16 01:26:18 -07001698 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 spin_lock(&hugetlb_lock);
Andi Kleena5516432008-07-23 21:27:41 -07001700 while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08001701 if (!adjust_pool_surplus(h, nodes_allowed, -1))
Adam Litke7893d1d2007-10-16 01:26:18 -07001702 break;
1703 }
1704
Andi Kleena5516432008-07-23 21:27:41 -07001705 while (count > persistent_huge_pages(h)) {
Adam Litke7893d1d2007-10-16 01:26:18 -07001706 /*
1707 * If this allocation races such that we no longer need the
1708 * page, free_huge_page will handle it by freeing the page
1709 * and reducing the surplus.
1710 */
1711 spin_unlock(&hugetlb_lock);
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001712 if (hstate_is_gigantic(h))
1713 ret = alloc_fresh_gigantic_page(h, nodes_allowed);
1714 else
1715 ret = alloc_fresh_huge_page(h, nodes_allowed);
Adam Litke7893d1d2007-10-16 01:26:18 -07001716 spin_lock(&hugetlb_lock);
1717 if (!ret)
1718 goto out;
1719
Mel Gorman536240f22009-12-14 17:59:56 -08001720 /* Bail for signals. Probably ctrl-c from user */
1721 if (signal_pending(current))
1722 goto out;
Adam Litke7893d1d2007-10-16 01:26:18 -07001723 }
Adam Litke7893d1d2007-10-16 01:26:18 -07001724
1725 /*
1726 * Decrease the pool size
1727 * First return free pages to the buddy allocator (being careful
1728 * to keep enough around to satisfy reservations). Then place
1729 * pages into surplus state as needed so the pool will shrink
1730 * to the desired size as pages become free.
Nishanth Aravamudand1c3fb12007-12-17 16:20:12 -08001731 *
1732 * By placing pages into the surplus state independent of the
1733 * overcommit value, we are allowing the surplus pool size to
1734 * exceed overcommit. There are few sane options here. Since
1735 * alloc_buddy_huge_page() is checking the global counter,
1736 * though, we'll note that we're not allowed to exceed surplus
1737 * and won't grow the pool anywhere else. Not until one of the
1738 * sysctls are changed, or the surplus pages go out of use.
Adam Litke7893d1d2007-10-16 01:26:18 -07001739 */
Andi Kleena5516432008-07-23 21:27:41 -07001740 min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
Adam Litke6b0c8802007-10-16 01:26:23 -07001741 min_count = max(count, min_count);
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08001742 try_to_free_low(h, min_count, nodes_allowed);
Andi Kleena5516432008-07-23 21:27:41 -07001743 while (min_count < persistent_huge_pages(h)) {
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08001744 if (!free_pool_huge_page(h, nodes_allowed, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 break;
Mizuma, Masayoshi55f67142014-04-07 15:37:54 -07001746 cond_resched_lock(&hugetlb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 }
Andi Kleena5516432008-07-23 21:27:41 -07001748 while (count < persistent_huge_pages(h)) {
Lee Schermerhorn6ae11b22009-12-14 17:58:16 -08001749 if (!adjust_pool_surplus(h, nodes_allowed, 1))
Adam Litke7893d1d2007-10-16 01:26:18 -07001750 break;
1751 }
1752out:
Andi Kleena5516432008-07-23 21:27:41 -07001753 ret = persistent_huge_pages(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 spin_unlock(&hugetlb_lock);
Adam Litke7893d1d2007-10-16 01:26:18 -07001755 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001758#define HSTATE_ATTR_RO(_name) \
1759 static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
1760
1761#define HSTATE_ATTR(_name) \
1762 static struct kobj_attribute _name##_attr = \
1763 __ATTR(_name, 0644, _name##_show, _name##_store)
1764
1765static struct kobject *hugepages_kobj;
1766static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
1767
Lee Schermerhorn9a305232009-12-14 17:58:25 -08001768static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp);
1769
1770static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001771{
1772 int i;
Lee Schermerhorn9a305232009-12-14 17:58:25 -08001773
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001774 for (i = 0; i < HUGE_MAX_HSTATE; i++)
Lee Schermerhorn9a305232009-12-14 17:58:25 -08001775 if (hstate_kobjs[i] == kobj) {
1776 if (nidp)
1777 *nidp = NUMA_NO_NODE;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001778 return &hstates[i];
Lee Schermerhorn9a305232009-12-14 17:58:25 -08001779 }
1780
1781 return kobj_to_node_hstate(kobj, nidp);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001782}
1783
Lee Schermerhorn06808b02009-12-14 17:58:21 -08001784static ssize_t nr_hugepages_show_common(struct kobject *kobj,
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001785 struct kobj_attribute *attr, char *buf)
1786{
Lee Schermerhorn9a305232009-12-14 17:58:25 -08001787 struct hstate *h;
1788 unsigned long nr_huge_pages;
1789 int nid;
1790
1791 h = kobj_to_hstate(kobj, &nid);
1792 if (nid == NUMA_NO_NODE)
1793 nr_huge_pages = h->nr_huge_pages;
1794 else
1795 nr_huge_pages = h->nr_huge_pages_node[nid];
1796
1797 return sprintf(buf, "%lu\n", nr_huge_pages);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001798}
Eric B Munsonadbe8722011-01-13 15:47:27 -08001799
David Rientjes238d3c12014-08-06 16:06:51 -07001800static ssize_t __nr_hugepages_store_common(bool obey_mempolicy,
1801 struct hstate *h, int nid,
1802 unsigned long count, size_t len)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001803{
1804 int err;
David Rientjesbad44b52009-12-14 17:58:38 -08001805 NODEMASK_ALLOC(nodemask_t, nodes_allowed, GFP_KERNEL | __GFP_NORETRY);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001806
Luiz Capitulino944d9fe2014-06-04 16:07:13 -07001807 if (hstate_is_gigantic(h) && !gigantic_page_supported()) {
Eric B Munsonadbe8722011-01-13 15:47:27 -08001808 err = -EINVAL;
1809 goto out;
1810 }
1811
Lee Schermerhorn9a305232009-12-14 17:58:25 -08001812 if (nid == NUMA_NO_NODE) {
1813 /*
1814 * global hstate attribute
1815 */
1816 if (!(obey_mempolicy &&
1817 init_nodemask_of_mempolicy(nodes_allowed))) {
1818 NODEMASK_FREE(nodes_allowed);
Lai Jiangshan8cebfcd2012-12-12 13:51:36 -08001819 nodes_allowed = &node_states[N_MEMORY];
Lee Schermerhorn9a305232009-12-14 17:58:25 -08001820 }
1821 } else if (nodes_allowed) {
1822 /*
1823 * per node hstate attribute: adjust count to global,
1824 * but restrict alloc/free to the specified node.
1825 */
1826 count += h->nr_huge_pages - h->nr_huge_pages_node[nid];
1827 init_nodemask_of_node(nodes_allowed, nid);
1828 } else
Lai Jiangshan8cebfcd2012-12-12 13:51:36 -08001829 nodes_allowed = &node_states[N_MEMORY];
Lee Schermerhorn9a305232009-12-14 17:58:25 -08001830
Lee Schermerhorn06808b02009-12-14 17:58:21 -08001831 h->max_huge_pages = set_max_huge_pages(h, count, nodes_allowed);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001832
Lai Jiangshan8cebfcd2012-12-12 13:51:36 -08001833 if (nodes_allowed != &node_states[N_MEMORY])
Lee Schermerhorn06808b02009-12-14 17:58:21 -08001834 NODEMASK_FREE(nodes_allowed);
1835
1836 return len;
Eric B Munsonadbe8722011-01-13 15:47:27 -08001837out:
1838 NODEMASK_FREE(nodes_allowed);
1839 return err;
Lee Schermerhorn06808b02009-12-14 17:58:21 -08001840}
1841
David Rientjes238d3c12014-08-06 16:06:51 -07001842static ssize_t nr_hugepages_store_common(bool obey_mempolicy,
1843 struct kobject *kobj, const char *buf,
1844 size_t len)
1845{
1846 struct hstate *h;
1847 unsigned long count;
1848 int nid;
1849 int err;
1850
1851 err = kstrtoul(buf, 10, &count);
1852 if (err)
1853 return err;
1854
1855 h = kobj_to_hstate(kobj, &nid);
1856 return __nr_hugepages_store_common(obey_mempolicy, h, nid, count, len);
1857}
1858
Lee Schermerhorn06808b02009-12-14 17:58:21 -08001859static ssize_t nr_hugepages_show(struct kobject *kobj,
1860 struct kobj_attribute *attr, char *buf)
1861{
1862 return nr_hugepages_show_common(kobj, attr, buf);
1863}
1864
1865static ssize_t nr_hugepages_store(struct kobject *kobj,
1866 struct kobj_attribute *attr, const char *buf, size_t len)
1867{
David Rientjes238d3c12014-08-06 16:06:51 -07001868 return nr_hugepages_store_common(false, kobj, buf, len);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001869}
1870HSTATE_ATTR(nr_hugepages);
1871
Lee Schermerhorn06808b02009-12-14 17:58:21 -08001872#ifdef CONFIG_NUMA
1873
1874/*
1875 * hstate attribute for optionally mempolicy-based constraint on persistent
1876 * huge page alloc/free.
1877 */
1878static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj,
1879 struct kobj_attribute *attr, char *buf)
1880{
1881 return nr_hugepages_show_common(kobj, attr, buf);
1882}
1883
1884static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj,
1885 struct kobj_attribute *attr, const char *buf, size_t len)
1886{
David Rientjes238d3c12014-08-06 16:06:51 -07001887 return nr_hugepages_store_common(true, kobj, buf, len);
Lee Schermerhorn06808b02009-12-14 17:58:21 -08001888}
1889HSTATE_ATTR(nr_hugepages_mempolicy);
1890#endif
1891
1892
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001893static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj,
1894 struct kobj_attribute *attr, char *buf)
1895{
Lee Schermerhorn9a305232009-12-14 17:58:25 -08001896 struct hstate *h = kobj_to_hstate(kobj, NULL);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001897 return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages);
1898}
Eric B Munsonadbe8722011-01-13 15:47:27 -08001899
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001900static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj,
1901 struct kobj_attribute *attr, const char *buf, size_t count)
1902{
1903 int err;
1904 unsigned long input;
Lee Schermerhorn9a305232009-12-14 17:58:25 -08001905 struct hstate *h = kobj_to_hstate(kobj, NULL);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001906
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07001907 if (hstate_is_gigantic(h))
Eric B Munsonadbe8722011-01-13 15:47:27 -08001908 return -EINVAL;
1909
Jingoo Han3dbb95f2013-09-11 14:20:25 -07001910 err = kstrtoul(buf, 10, &input);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001911 if (err)
Eric B Munson73ae31e2011-01-13 15:47:28 -08001912 return err;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001913
1914 spin_lock(&hugetlb_lock);
1915 h->nr_overcommit_huge_pages = input;
1916 spin_unlock(&hugetlb_lock);
1917
1918 return count;
1919}
1920HSTATE_ATTR(nr_overcommit_hugepages);
1921
1922static ssize_t free_hugepages_show(struct kobject *kobj,
1923 struct kobj_attribute *attr, char *buf)
1924{
Lee Schermerhorn9a305232009-12-14 17:58:25 -08001925 struct hstate *h;
1926 unsigned long free_huge_pages;
1927 int nid;
1928
1929 h = kobj_to_hstate(kobj, &nid);
1930 if (nid == NUMA_NO_NODE)
1931 free_huge_pages = h->free_huge_pages;
1932 else
1933 free_huge_pages = h->free_huge_pages_node[nid];
1934
1935 return sprintf(buf, "%lu\n", free_huge_pages);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001936}
1937HSTATE_ATTR_RO(free_hugepages);
1938
1939static ssize_t resv_hugepages_show(struct kobject *kobj,
1940 struct kobj_attribute *attr, char *buf)
1941{
Lee Schermerhorn9a305232009-12-14 17:58:25 -08001942 struct hstate *h = kobj_to_hstate(kobj, NULL);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001943 return sprintf(buf, "%lu\n", h->resv_huge_pages);
1944}
1945HSTATE_ATTR_RO(resv_hugepages);
1946
1947static ssize_t surplus_hugepages_show(struct kobject *kobj,
1948 struct kobj_attribute *attr, char *buf)
1949{
Lee Schermerhorn9a305232009-12-14 17:58:25 -08001950 struct hstate *h;
1951 unsigned long surplus_huge_pages;
1952 int nid;
1953
1954 h = kobj_to_hstate(kobj, &nid);
1955 if (nid == NUMA_NO_NODE)
1956 surplus_huge_pages = h->surplus_huge_pages;
1957 else
1958 surplus_huge_pages = h->surplus_huge_pages_node[nid];
1959
1960 return sprintf(buf, "%lu\n", surplus_huge_pages);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001961}
1962HSTATE_ATTR_RO(surplus_hugepages);
1963
1964static struct attribute *hstate_attrs[] = {
1965 &nr_hugepages_attr.attr,
1966 &nr_overcommit_hugepages_attr.attr,
1967 &free_hugepages_attr.attr,
1968 &resv_hugepages_attr.attr,
1969 &surplus_hugepages_attr.attr,
Lee Schermerhorn06808b02009-12-14 17:58:21 -08001970#ifdef CONFIG_NUMA
1971 &nr_hugepages_mempolicy_attr.attr,
1972#endif
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001973 NULL,
1974};
1975
1976static struct attribute_group hstate_attr_group = {
1977 .attrs = hstate_attrs,
1978};
1979
Jeff Mahoney094e9532010-02-02 13:44:14 -08001980static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent,
1981 struct kobject **hstate_kobjs,
1982 struct attribute_group *hstate_attr_group)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001983{
1984 int retval;
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07001985 int hi = hstate_index(h);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001986
Lee Schermerhorn9a305232009-12-14 17:58:25 -08001987 hstate_kobjs[hi] = kobject_create_and_add(h->name, parent);
1988 if (!hstate_kobjs[hi])
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001989 return -ENOMEM;
1990
Lee Schermerhorn9a305232009-12-14 17:58:25 -08001991 retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001992 if (retval)
Lee Schermerhorn9a305232009-12-14 17:58:25 -08001993 kobject_put(hstate_kobjs[hi]);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07001994
1995 return retval;
1996}
1997
1998static void __init hugetlb_sysfs_init(void)
1999{
2000 struct hstate *h;
2001 int err;
2002
2003 hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj);
2004 if (!hugepages_kobj)
2005 return;
2006
2007 for_each_hstate(h) {
Lee Schermerhorn9a305232009-12-14 17:58:25 -08002008 err = hugetlb_sysfs_add_hstate(h, hugepages_kobj,
2009 hstate_kobjs, &hstate_attr_group);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002010 if (err)
Andrew Mortonffb22af2013-02-22 16:32:08 -08002011 pr_err("Hugetlb: Unable to add hstate %s", h->name);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002012 }
2013}
2014
Lee Schermerhorn9a305232009-12-14 17:58:25 -08002015#ifdef CONFIG_NUMA
2016
2017/*
2018 * node_hstate/s - associate per node hstate attributes, via their kobjects,
Kay Sievers10fbcf42011-12-21 14:48:43 -08002019 * with node devices in node_devices[] using a parallel array. The array
2020 * index of a node device or _hstate == node id.
2021 * This is here to avoid any static dependency of the node device driver, in
Lee Schermerhorn9a305232009-12-14 17:58:25 -08002022 * the base kernel, on the hugetlb module.
2023 */
2024struct node_hstate {
2025 struct kobject *hugepages_kobj;
2026 struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
2027};
2028struct node_hstate node_hstates[MAX_NUMNODES];
2029
2030/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08002031 * A subset of global hstate attributes for node devices
Lee Schermerhorn9a305232009-12-14 17:58:25 -08002032 */
2033static struct attribute *per_node_hstate_attrs[] = {
2034 &nr_hugepages_attr.attr,
2035 &free_hugepages_attr.attr,
2036 &surplus_hugepages_attr.attr,
2037 NULL,
2038};
2039
2040static struct attribute_group per_node_hstate_attr_group = {
2041 .attrs = per_node_hstate_attrs,
2042};
2043
2044/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08002045 * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj.
Lee Schermerhorn9a305232009-12-14 17:58:25 -08002046 * Returns node id via non-NULL nidp.
2047 */
2048static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
2049{
2050 int nid;
2051
2052 for (nid = 0; nid < nr_node_ids; nid++) {
2053 struct node_hstate *nhs = &node_hstates[nid];
2054 int i;
2055 for (i = 0; i < HUGE_MAX_HSTATE; i++)
2056 if (nhs->hstate_kobjs[i] == kobj) {
2057 if (nidp)
2058 *nidp = nid;
2059 return &hstates[i];
2060 }
2061 }
2062
2063 BUG();
2064 return NULL;
2065}
2066
2067/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08002068 * Unregister hstate attributes from a single node device.
Lee Schermerhorn9a305232009-12-14 17:58:25 -08002069 * No-op if no hstate attributes attached.
2070 */
Claudiu Ghioc3cd8b442013-03-04 12:46:15 +02002071static void hugetlb_unregister_node(struct node *node)
Lee Schermerhorn9a305232009-12-14 17:58:25 -08002072{
2073 struct hstate *h;
Kay Sievers10fbcf42011-12-21 14:48:43 -08002074 struct node_hstate *nhs = &node_hstates[node->dev.id];
Lee Schermerhorn9a305232009-12-14 17:58:25 -08002075
2076 if (!nhs->hugepages_kobj)
Lee Schermerhorn9b5e5d02009-12-14 17:58:32 -08002077 return; /* no hstate attributes */
Lee Schermerhorn9a305232009-12-14 17:58:25 -08002078
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07002079 for_each_hstate(h) {
2080 int idx = hstate_index(h);
2081 if (nhs->hstate_kobjs[idx]) {
2082 kobject_put(nhs->hstate_kobjs[idx]);
2083 nhs->hstate_kobjs[idx] = NULL;
Lee Schermerhorn9a305232009-12-14 17:58:25 -08002084 }
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07002085 }
Lee Schermerhorn9a305232009-12-14 17:58:25 -08002086
2087 kobject_put(nhs->hugepages_kobj);
2088 nhs->hugepages_kobj = NULL;
2089}
2090
2091/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08002092 * hugetlb module exit: unregister hstate attributes from node devices
Lee Schermerhorn9a305232009-12-14 17:58:25 -08002093 * that have them.
2094 */
2095static void hugetlb_unregister_all_nodes(void)
2096{
2097 int nid;
2098
2099 /*
Kay Sievers10fbcf42011-12-21 14:48:43 -08002100 * disable node device registrations.
Lee Schermerhorn9a305232009-12-14 17:58:25 -08002101 */
2102 register_hugetlbfs_with_node(NULL, NULL);
2103
2104 /*
2105 * remove hstate attributes from any nodes that have them.
2106 */
2107 for (nid = 0; nid < nr_node_ids; nid++)
Wen Congyang87327942012-12-11 16:00:56 -08002108 hugetlb_unregister_node(node_devices[nid]);
Lee Schermerhorn9a305232009-12-14 17:58:25 -08002109}
2110
2111/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08002112 * Register hstate attributes for a single node device.
Lee Schermerhorn9a305232009-12-14 17:58:25 -08002113 * No-op if attributes already registered.
2114 */
Claudiu Ghioc3cd8b442013-03-04 12:46:15 +02002115static void hugetlb_register_node(struct node *node)
Lee Schermerhorn9a305232009-12-14 17:58:25 -08002116{
2117 struct hstate *h;
Kay Sievers10fbcf42011-12-21 14:48:43 -08002118 struct node_hstate *nhs = &node_hstates[node->dev.id];
Lee Schermerhorn9a305232009-12-14 17:58:25 -08002119 int err;
2120
2121 if (nhs->hugepages_kobj)
2122 return; /* already allocated */
2123
2124 nhs->hugepages_kobj = kobject_create_and_add("hugepages",
Kay Sievers10fbcf42011-12-21 14:48:43 -08002125 &node->dev.kobj);
Lee Schermerhorn9a305232009-12-14 17:58:25 -08002126 if (!nhs->hugepages_kobj)
2127 return;
2128
2129 for_each_hstate(h) {
2130 err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj,
2131 nhs->hstate_kobjs,
2132 &per_node_hstate_attr_group);
2133 if (err) {
Andrew Mortonffb22af2013-02-22 16:32:08 -08002134 pr_err("Hugetlb: Unable to add hstate %s for node %d\n",
2135 h->name, node->dev.id);
Lee Schermerhorn9a305232009-12-14 17:58:25 -08002136 hugetlb_unregister_node(node);
2137 break;
2138 }
2139 }
2140}
2141
2142/*
Lee Schermerhorn9b5e5d02009-12-14 17:58:32 -08002143 * hugetlb init time: register hstate attributes for all registered node
Kay Sievers10fbcf42011-12-21 14:48:43 -08002144 * devices of nodes that have memory. All on-line nodes should have
2145 * registered their associated device by this time.
Lee Schermerhorn9a305232009-12-14 17:58:25 -08002146 */
Luiz Capitulino7d9ca002014-12-12 16:55:24 -08002147static void __init hugetlb_register_all_nodes(void)
Lee Schermerhorn9a305232009-12-14 17:58:25 -08002148{
2149 int nid;
2150
Lai Jiangshan8cebfcd2012-12-12 13:51:36 -08002151 for_each_node_state(nid, N_MEMORY) {
Wen Congyang87327942012-12-11 16:00:56 -08002152 struct node *node = node_devices[nid];
Kay Sievers10fbcf42011-12-21 14:48:43 -08002153 if (node->dev.id == nid)
Lee Schermerhorn9a305232009-12-14 17:58:25 -08002154 hugetlb_register_node(node);
2155 }
2156
2157 /*
Kay Sievers10fbcf42011-12-21 14:48:43 -08002158 * Let the node device driver know we're here so it can
Lee Schermerhorn9a305232009-12-14 17:58:25 -08002159 * [un]register hstate attributes on node hotplug.
2160 */
2161 register_hugetlbfs_with_node(hugetlb_register_node,
2162 hugetlb_unregister_node);
2163}
2164#else /* !CONFIG_NUMA */
2165
2166static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
2167{
2168 BUG();
2169 if (nidp)
2170 *nidp = -1;
2171 return NULL;
2172}
2173
2174static void hugetlb_unregister_all_nodes(void) { }
2175
2176static void hugetlb_register_all_nodes(void) { }
2177
2178#endif
2179
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002180static void __exit hugetlb_exit(void)
2181{
2182 struct hstate *h;
2183
Lee Schermerhorn9a305232009-12-14 17:58:25 -08002184 hugetlb_unregister_all_nodes();
2185
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002186 for_each_hstate(h) {
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07002187 kobject_put(hstate_kobjs[hstate_index(h)]);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002188 }
2189
2190 kobject_put(hugepages_kobj);
Davidlohr Bueso8382d912014-04-03 14:47:31 -07002191 kfree(htlb_fault_mutex_table);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002192}
2193module_exit(hugetlb_exit);
2194
2195static int __init hugetlb_init(void)
2196{
Davidlohr Bueso8382d912014-04-03 14:47:31 -07002197 int i;
2198
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07002199 if (!hugepages_supported())
Benjamin Herrenschmidt0ef89d22008-07-31 00:07:30 -07002200 return 0;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002201
Nick Piggine11bfbf2008-07-23 21:27:52 -07002202 if (!size_to_hstate(default_hstate_size)) {
2203 default_hstate_size = HPAGE_SIZE;
2204 if (!size_to_hstate(default_hstate_size))
2205 hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002206 }
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07002207 default_hstate_idx = hstate_index(size_to_hstate(default_hstate_size));
Nick Piggine11bfbf2008-07-23 21:27:52 -07002208 if (default_hstate_max_huge_pages)
2209 default_hstate.max_huge_pages = default_hstate_max_huge_pages;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002210
2211 hugetlb_init_hstates();
Andi Kleenaa888a72008-07-23 21:27:47 -07002212 gather_bootmem_prealloc();
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002213 report_hugepages();
2214
2215 hugetlb_sysfs_init();
Lee Schermerhorn9a305232009-12-14 17:58:25 -08002216 hugetlb_register_all_nodes();
Jianguo Wu7179e7b2012-12-18 14:23:19 -08002217 hugetlb_cgroup_file_init();
Lee Schermerhorn9a305232009-12-14 17:58:25 -08002218
Davidlohr Bueso8382d912014-04-03 14:47:31 -07002219#ifdef CONFIG_SMP
2220 num_fault_mutexes = roundup_pow_of_two(8 * num_possible_cpus());
2221#else
2222 num_fault_mutexes = 1;
2223#endif
2224 htlb_fault_mutex_table =
2225 kmalloc(sizeof(struct mutex) * num_fault_mutexes, GFP_KERNEL);
2226 BUG_ON(!htlb_fault_mutex_table);
2227
2228 for (i = 0; i < num_fault_mutexes; i++)
2229 mutex_init(&htlb_fault_mutex_table[i]);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002230 return 0;
2231}
2232module_init(hugetlb_init);
2233
2234/* Should be called on processing a hugepagesz=... option */
2235void __init hugetlb_add_hstate(unsigned order)
2236{
2237 struct hstate *h;
Andi Kleen8faa8b02008-07-23 21:27:48 -07002238 unsigned long i;
2239
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002240 if (size_to_hstate(PAGE_SIZE << order)) {
Andrew Mortonffb22af2013-02-22 16:32:08 -08002241 pr_warning("hugepagesz= specified twice, ignoring\n");
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002242 return;
2243 }
Aneesh Kumar K.V47d38342012-07-31 16:41:54 -07002244 BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002245 BUG_ON(order == 0);
Aneesh Kumar K.V47d38342012-07-31 16:41:54 -07002246 h = &hstates[hugetlb_max_hstate++];
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002247 h->order = order;
2248 h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1);
Andi Kleen8faa8b02008-07-23 21:27:48 -07002249 h->nr_huge_pages = 0;
2250 h->free_huge_pages = 0;
2251 for (i = 0; i < MAX_NUMNODES; ++i)
2252 INIT_LIST_HEAD(&h->hugepage_freelists[i]);
Aneesh Kumar K.V0edaecf2012-07-31 16:42:07 -07002253 INIT_LIST_HEAD(&h->hugepage_activelist);
Lai Jiangshan8cebfcd2012-12-12 13:51:36 -08002254 h->next_nid_to_alloc = first_node(node_states[N_MEMORY]);
2255 h->next_nid_to_free = first_node(node_states[N_MEMORY]);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002256 snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB",
2257 huge_page_size(h)/1024);
Andi Kleen8faa8b02008-07-23 21:27:48 -07002258
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002259 parsed_hstate = h;
2260}
2261
Nick Piggine11bfbf2008-07-23 21:27:52 -07002262static int __init hugetlb_nrpages_setup(char *s)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002263{
2264 unsigned long *mhp;
Andi Kleen8faa8b02008-07-23 21:27:48 -07002265 static unsigned long *last_mhp;
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002266
2267 /*
Aneesh Kumar K.V47d38342012-07-31 16:41:54 -07002268 * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter yet,
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002269 * so this hugepages= parameter goes to the "default hstate".
2270 */
Aneesh Kumar K.V47d38342012-07-31 16:41:54 -07002271 if (!hugetlb_max_hstate)
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002272 mhp = &default_hstate_max_huge_pages;
2273 else
2274 mhp = &parsed_hstate->max_huge_pages;
2275
Andi Kleen8faa8b02008-07-23 21:27:48 -07002276 if (mhp == last_mhp) {
Andrew Mortonffb22af2013-02-22 16:32:08 -08002277 pr_warning("hugepages= specified twice without "
2278 "interleaving hugepagesz=, ignoring\n");
Andi Kleen8faa8b02008-07-23 21:27:48 -07002279 return 1;
2280 }
2281
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002282 if (sscanf(s, "%lu", mhp) <= 0)
2283 *mhp = 0;
2284
Andi Kleen8faa8b02008-07-23 21:27:48 -07002285 /*
2286 * Global state is always initialized later in hugetlb_init.
2287 * But we need to allocate >= MAX_ORDER hstates here early to still
2288 * use the bootmem allocator.
2289 */
Aneesh Kumar K.V47d38342012-07-31 16:41:54 -07002290 if (hugetlb_max_hstate && parsed_hstate->order >= MAX_ORDER)
Andi Kleen8faa8b02008-07-23 21:27:48 -07002291 hugetlb_hstate_alloc_pages(parsed_hstate);
2292
2293 last_mhp = mhp;
2294
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002295 return 1;
2296}
Nick Piggine11bfbf2008-07-23 21:27:52 -07002297__setup("hugepages=", hugetlb_nrpages_setup);
2298
2299static int __init hugetlb_default_setup(char *s)
2300{
2301 default_hstate_size = memparse(s, &s);
2302 return 1;
2303}
2304__setup("default_hugepagesz=", hugetlb_default_setup);
Nishanth Aravamudana3437872008-07-23 21:27:44 -07002305
Nishanth Aravamudan8a213462008-07-25 19:44:37 -07002306static unsigned int cpuset_mems_nr(unsigned int *array)
2307{
2308 int node;
2309 unsigned int nr = 0;
2310
2311 for_each_node_mask(node, cpuset_current_mems_allowed)
2312 nr += array[node];
2313
2314 return nr;
2315}
2316
2317#ifdef CONFIG_SYSCTL
Lee Schermerhorn06808b02009-12-14 17:58:21 -08002318static int hugetlb_sysctl_handler_common(bool obey_mempolicy,
2319 struct ctl_table *table, int write,
2320 void __user *buffer, size_t *length, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321{
Andi Kleene5ff2152008-07-23 21:27:42 -07002322 struct hstate *h = &default_hstate;
David Rientjes238d3c12014-08-06 16:06:51 -07002323 unsigned long tmp = h->max_huge_pages;
Michal Hocko08d4a242011-01-13 15:47:26 -08002324 int ret;
Andi Kleene5ff2152008-07-23 21:27:42 -07002325
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07002326 if (!hugepages_supported())
2327 return -ENOTSUPP;
2328
Andi Kleene5ff2152008-07-23 21:27:42 -07002329 table->data = &tmp;
2330 table->maxlen = sizeof(unsigned long);
Michal Hocko08d4a242011-01-13 15:47:26 -08002331 ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
2332 if (ret)
2333 goto out;
Andi Kleene5ff2152008-07-23 21:27:42 -07002334
David Rientjes238d3c12014-08-06 16:06:51 -07002335 if (write)
2336 ret = __nr_hugepages_store_common(obey_mempolicy, h,
2337 NUMA_NO_NODE, tmp, *length);
Michal Hocko08d4a242011-01-13 15:47:26 -08002338out:
2339 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340}
Mel Gorman396faf02007-07-17 04:03:13 -07002341
Lee Schermerhorn06808b02009-12-14 17:58:21 -08002342int hugetlb_sysctl_handler(struct ctl_table *table, int write,
2343 void __user *buffer, size_t *length, loff_t *ppos)
2344{
2345
2346 return hugetlb_sysctl_handler_common(false, table, write,
2347 buffer, length, ppos);
2348}
2349
2350#ifdef CONFIG_NUMA
2351int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write,
2352 void __user *buffer, size_t *length, loff_t *ppos)
2353{
2354 return hugetlb_sysctl_handler_common(true, table, write,
2355 buffer, length, ppos);
2356}
2357#endif /* CONFIG_NUMA */
2358
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08002359int hugetlb_overcommit_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07002360 void __user *buffer,
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08002361 size_t *length, loff_t *ppos)
2362{
Andi Kleena5516432008-07-23 21:27:41 -07002363 struct hstate *h = &default_hstate;
Andi Kleene5ff2152008-07-23 21:27:42 -07002364 unsigned long tmp;
Michal Hocko08d4a242011-01-13 15:47:26 -08002365 int ret;
Andi Kleene5ff2152008-07-23 21:27:42 -07002366
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07002367 if (!hugepages_supported())
2368 return -ENOTSUPP;
2369
Petr Holasekc033a932011-03-22 16:33:05 -07002370 tmp = h->nr_overcommit_huge_pages;
Andi Kleene5ff2152008-07-23 21:27:42 -07002371
Luiz Capitulinobae7f4a2014-06-04 16:07:08 -07002372 if (write && hstate_is_gigantic(h))
Eric B Munsonadbe8722011-01-13 15:47:27 -08002373 return -EINVAL;
2374
Andi Kleene5ff2152008-07-23 21:27:42 -07002375 table->data = &tmp;
2376 table->maxlen = sizeof(unsigned long);
Michal Hocko08d4a242011-01-13 15:47:26 -08002377 ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
2378 if (ret)
2379 goto out;
Andi Kleene5ff2152008-07-23 21:27:42 -07002380
2381 if (write) {
2382 spin_lock(&hugetlb_lock);
2383 h->nr_overcommit_huge_pages = tmp;
2384 spin_unlock(&hugetlb_lock);
2385 }
Michal Hocko08d4a242011-01-13 15:47:26 -08002386out:
2387 return ret;
Nishanth Aravamudana3d0c6a2008-02-08 04:18:18 -08002388}
2389
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390#endif /* CONFIG_SYSCTL */
2391
Alexey Dobriyane1759c22008-10-15 23:50:22 +04002392void hugetlb_report_meminfo(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393{
Andi Kleena5516432008-07-23 21:27:41 -07002394 struct hstate *h = &default_hstate;
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07002395 if (!hugepages_supported())
2396 return;
Alexey Dobriyane1759c22008-10-15 23:50:22 +04002397 seq_printf(m,
Rik van Riel4f98a2f2008-10-18 20:26:32 -07002398 "HugePages_Total: %5lu\n"
2399 "HugePages_Free: %5lu\n"
2400 "HugePages_Rsvd: %5lu\n"
2401 "HugePages_Surp: %5lu\n"
2402 "Hugepagesize: %8lu kB\n",
Andi Kleena5516432008-07-23 21:27:41 -07002403 h->nr_huge_pages,
2404 h->free_huge_pages,
2405 h->resv_huge_pages,
2406 h->surplus_huge_pages,
2407 1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
2409
2410int hugetlb_report_node_meminfo(int nid, char *buf)
2411{
Andi Kleena5516432008-07-23 21:27:41 -07002412 struct hstate *h = &default_hstate;
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07002413 if (!hugepages_supported())
2414 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 return sprintf(buf,
2416 "Node %d HugePages_Total: %5u\n"
Nishanth Aravamudana1de0912008-03-26 14:37:53 -07002417 "Node %d HugePages_Free: %5u\n"
2418 "Node %d HugePages_Surp: %5u\n",
Andi Kleena5516432008-07-23 21:27:41 -07002419 nid, h->nr_huge_pages_node[nid],
2420 nid, h->free_huge_pages_node[nid],
2421 nid, h->surplus_huge_pages_node[nid]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422}
2423
David Rientjes949f7ec2013-04-29 15:07:48 -07002424void hugetlb_show_meminfo(void)
2425{
2426 struct hstate *h;
2427 int nid;
2428
Nishanth Aravamudan457c1b22014-05-06 12:50:00 -07002429 if (!hugepages_supported())
2430 return;
2431
David Rientjes949f7ec2013-04-29 15:07:48 -07002432 for_each_node_state(nid, N_MEMORY)
2433 for_each_hstate(h)
2434 pr_info("Node %d hugepages_total=%u hugepages_free=%u hugepages_surp=%u hugepages_size=%lukB\n",
2435 nid,
2436 h->nr_huge_pages_node[nid],
2437 h->free_huge_pages_node[nid],
2438 h->surplus_huge_pages_node[nid],
2439 1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
2440}
2441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442/* Return the number pages of memory we physically have, in PAGE_SIZE units. */
2443unsigned long hugetlb_total_pages(void)
2444{
Wanpeng Lid0028582013-03-22 15:04:40 -07002445 struct hstate *h;
2446 unsigned long nr_total_pages = 0;
2447
2448 for_each_hstate(h)
2449 nr_total_pages += h->nr_huge_pages * pages_per_huge_page(h);
2450 return nr_total_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
Andi Kleena5516432008-07-23 21:27:41 -07002453static int hugetlb_acct_memory(struct hstate *h, long delta)
Mel Gormanfc1b8a72008-07-23 21:27:22 -07002454{
2455 int ret = -ENOMEM;
2456
2457 spin_lock(&hugetlb_lock);
2458 /*
2459 * When cpuset is configured, it breaks the strict hugetlb page
2460 * reservation as the accounting is done on a global variable. Such
2461 * reservation is completely rubbish in the presence of cpuset because
2462 * the reservation is not checked against page availability for the
2463 * current cpuset. Application can still potentially OOM'ed by kernel
2464 * with lack of free htlb page in cpuset that the task is in.
2465 * Attempt to enforce strict accounting with cpuset is almost
2466 * impossible (or too ugly) because cpuset is too fluid that
2467 * task or memory node can be dynamically moved between cpusets.
2468 *
2469 * The change of semantics for shared hugetlb mapping with cpuset is
2470 * undesirable. However, in order to preserve some of the semantics,
2471 * we fall back to check against current free page availability as
2472 * a best attempt and hopefully to minimize the impact of changing
2473 * semantics that cpuset has.
2474 */
2475 if (delta > 0) {
Andi Kleena5516432008-07-23 21:27:41 -07002476 if (gather_surplus_pages(h, delta) < 0)
Mel Gormanfc1b8a72008-07-23 21:27:22 -07002477 goto out;
2478
Andi Kleena5516432008-07-23 21:27:41 -07002479 if (delta > cpuset_mems_nr(h->free_huge_pages_node)) {
2480 return_unused_surplus_pages(h, delta);
Mel Gormanfc1b8a72008-07-23 21:27:22 -07002481 goto out;
2482 }
2483 }
2484
2485 ret = 0;
2486 if (delta < 0)
Andi Kleena5516432008-07-23 21:27:41 -07002487 return_unused_surplus_pages(h, (unsigned long) -delta);
Mel Gormanfc1b8a72008-07-23 21:27:22 -07002488
2489out:
2490 spin_unlock(&hugetlb_lock);
2491 return ret;
2492}
2493
Andy Whitcroft84afd992008-07-23 21:27:32 -07002494static void hugetlb_vm_op_open(struct vm_area_struct *vma)
2495{
Joonsoo Kimf522c3a2013-09-11 14:21:53 -07002496 struct resv_map *resv = vma_resv_map(vma);
Andy Whitcroft84afd992008-07-23 21:27:32 -07002497
2498 /*
2499 * This new VMA should share its siblings reservation map if present.
2500 * The VMA will only ever have a valid reservation map pointer where
2501 * it is being copied for another still existing VMA. As that VMA
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002502 * has a reference to the reservation map it cannot disappear until
Andy Whitcroft84afd992008-07-23 21:27:32 -07002503 * after this open call completes. It is therefore safe to take a
2504 * new reference here without additional locking.
2505 */
Joonsoo Kim4e35f482014-04-03 14:47:30 -07002506 if (resv && is_vma_resv_set(vma, HPAGE_RESV_OWNER))
Joonsoo Kimf522c3a2013-09-11 14:21:53 -07002507 kref_get(&resv->refs);
Andy Whitcroft84afd992008-07-23 21:27:32 -07002508}
2509
Mel Gormana1e78772008-07-23 21:27:23 -07002510static void hugetlb_vm_op_close(struct vm_area_struct *vma)
2511{
Andi Kleena5516432008-07-23 21:27:41 -07002512 struct hstate *h = hstate_vma(vma);
Joonsoo Kimf522c3a2013-09-11 14:21:53 -07002513 struct resv_map *resv = vma_resv_map(vma);
David Gibson90481622012-03-21 16:34:12 -07002514 struct hugepage_subpool *spool = subpool_vma(vma);
Joonsoo Kim4e35f482014-04-03 14:47:30 -07002515 unsigned long reserve, start, end;
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07002516 long gbl_reserve;
Andy Whitcroft84afd992008-07-23 21:27:32 -07002517
Joonsoo Kim4e35f482014-04-03 14:47:30 -07002518 if (!resv || !is_vma_resv_set(vma, HPAGE_RESV_OWNER))
2519 return;
Andy Whitcroft84afd992008-07-23 21:27:32 -07002520
Joonsoo Kim4e35f482014-04-03 14:47:30 -07002521 start = vma_hugecache_offset(h, vma, vma->vm_start);
2522 end = vma_hugecache_offset(h, vma, vma->vm_end);
Andy Whitcroft84afd992008-07-23 21:27:32 -07002523
Joonsoo Kim4e35f482014-04-03 14:47:30 -07002524 reserve = (end - start) - region_count(resv, start, end);
Andy Whitcroft84afd992008-07-23 21:27:32 -07002525
Joonsoo Kim4e35f482014-04-03 14:47:30 -07002526 kref_put(&resv->refs, resv_map_release);
2527
2528 if (reserve) {
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07002529 /*
2530 * Decrement reserve counts. The global reserve count may be
2531 * adjusted if the subpool has a minimum size.
2532 */
2533 gbl_reserve = hugepage_subpool_put_pages(spool, reserve);
2534 hugetlb_acct_memory(h, -gbl_reserve);
Andy Whitcroft84afd992008-07-23 21:27:32 -07002535 }
Mel Gormana1e78772008-07-23 21:27:23 -07002536}
2537
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538/*
2539 * We cannot handle pagefaults against hugetlb pages at all. They cause
2540 * handle_mm_fault() to try to instantiate regular-sized pages in the
2541 * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
2542 * this far.
2543 */
Nick Piggind0217ac2007-07-19 01:47:03 -07002544static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545{
2546 BUG();
Nick Piggind0217ac2007-07-19 01:47:03 -07002547 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548}
2549
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002550const struct vm_operations_struct hugetlb_vm_ops = {
Nick Piggind0217ac2007-07-19 01:47:03 -07002551 .fault = hugetlb_vm_op_fault,
Andy Whitcroft84afd992008-07-23 21:27:32 -07002552 .open = hugetlb_vm_op_open,
Mel Gormana1e78772008-07-23 21:27:23 -07002553 .close = hugetlb_vm_op_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554};
2555
David Gibson1e8f8892006-01-06 00:10:44 -08002556static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
2557 int writable)
David Gibson63551ae2005-06-21 17:14:44 -07002558{
2559 pte_t entry;
2560
David Gibson1e8f8892006-01-06 00:10:44 -08002561 if (writable) {
Gerald Schaefer106c9922013-04-29 15:07:23 -07002562 entry = huge_pte_mkwrite(huge_pte_mkdirty(mk_huge_pte(page,
2563 vma->vm_page_prot)));
David Gibson63551ae2005-06-21 17:14:44 -07002564 } else {
Gerald Schaefer106c9922013-04-29 15:07:23 -07002565 entry = huge_pte_wrprotect(mk_huge_pte(page,
2566 vma->vm_page_prot));
David Gibson63551ae2005-06-21 17:14:44 -07002567 }
2568 entry = pte_mkyoung(entry);
2569 entry = pte_mkhuge(entry);
Chris Metcalfd9ed9fa2012-04-01 14:01:34 -04002570 entry = arch_make_huge_pte(entry, vma, page, writable);
David Gibson63551ae2005-06-21 17:14:44 -07002571
2572 return entry;
2573}
2574
David Gibson1e8f8892006-01-06 00:10:44 -08002575static void set_huge_ptep_writable(struct vm_area_struct *vma,
2576 unsigned long address, pte_t *ptep)
2577{
2578 pte_t entry;
2579
Gerald Schaefer106c9922013-04-29 15:07:23 -07002580 entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(ptep)));
Chris Forbes32f84522011-07-25 17:12:14 -07002581 if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1))
Russell King4b3073e2009-12-18 16:40:18 +00002582 update_mmu_cache(vma, address, ptep);
David Gibson1e8f8892006-01-06 00:10:44 -08002583}
2584
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07002585static int is_hugetlb_entry_migration(pte_t pte)
2586{
2587 swp_entry_t swp;
2588
2589 if (huge_pte_none(pte) || pte_present(pte))
2590 return 0;
2591 swp = pte_to_swp_entry(pte);
2592 if (non_swap_entry(swp) && is_migration_entry(swp))
2593 return 1;
2594 else
2595 return 0;
2596}
2597
2598static int is_hugetlb_entry_hwpoisoned(pte_t pte)
2599{
2600 swp_entry_t swp;
2601
2602 if (huge_pte_none(pte) || pte_present(pte))
2603 return 0;
2604 swp = pte_to_swp_entry(pte);
2605 if (non_swap_entry(swp) && is_hwpoison_entry(swp))
2606 return 1;
2607 else
2608 return 0;
2609}
David Gibson1e8f8892006-01-06 00:10:44 -08002610
David Gibson63551ae2005-06-21 17:14:44 -07002611int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
2612 struct vm_area_struct *vma)
2613{
2614 pte_t *src_pte, *dst_pte, entry;
2615 struct page *ptepage;
Hugh Dickins1c598272005-10-19 21:23:43 -07002616 unsigned long addr;
David Gibson1e8f8892006-01-06 00:10:44 -08002617 int cow;
Andi Kleena5516432008-07-23 21:27:41 -07002618 struct hstate *h = hstate_vma(vma);
2619 unsigned long sz = huge_page_size(h);
Andreas Sandberge8569dd2014-01-21 15:49:09 -08002620 unsigned long mmun_start; /* For mmu_notifiers */
2621 unsigned long mmun_end; /* For mmu_notifiers */
2622 int ret = 0;
David Gibson1e8f8892006-01-06 00:10:44 -08002623
2624 cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
David Gibson63551ae2005-06-21 17:14:44 -07002625
Andreas Sandberge8569dd2014-01-21 15:49:09 -08002626 mmun_start = vma->vm_start;
2627 mmun_end = vma->vm_end;
2628 if (cow)
2629 mmu_notifier_invalidate_range_start(src, mmun_start, mmun_end);
2630
Andi Kleena5516432008-07-23 21:27:41 -07002631 for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) {
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08002632 spinlock_t *src_ptl, *dst_ptl;
Hugh Dickinsc74df322005-10-29 18:16:23 -07002633 src_pte = huge_pte_offset(src, addr);
2634 if (!src_pte)
2635 continue;
Andi Kleena5516432008-07-23 21:27:41 -07002636 dst_pte = huge_pte_alloc(dst, addr, sz);
Andreas Sandberge8569dd2014-01-21 15:49:09 -08002637 if (!dst_pte) {
2638 ret = -ENOMEM;
2639 break;
2640 }
Larry Woodmanc5c99422008-01-24 05:49:25 -08002641
2642 /* If the pagetables are shared don't copy or take references */
2643 if (dst_pte == src_pte)
2644 continue;
2645
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08002646 dst_ptl = huge_pte_lock(h, dst, dst_pte);
2647 src_ptl = huge_pte_lockptr(h, src, src_pte);
2648 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Naoya Horiguchi4a705fe2014-06-23 13:22:03 -07002649 entry = huge_ptep_get(src_pte);
2650 if (huge_pte_none(entry)) { /* skip none entry */
2651 ;
2652 } else if (unlikely(is_hugetlb_entry_migration(entry) ||
2653 is_hugetlb_entry_hwpoisoned(entry))) {
2654 swp_entry_t swp_entry = pte_to_swp_entry(entry);
2655
2656 if (is_write_migration_entry(swp_entry) && cow) {
2657 /*
2658 * COW mappings require pages in both
2659 * parent and child to be set to read.
2660 */
2661 make_migration_entry_read(&swp_entry);
2662 entry = swp_entry_to_pte(swp_entry);
2663 set_huge_pte_at(src, addr, src_pte, entry);
2664 }
2665 set_huge_pte_at(dst, addr, dst_pte, entry);
2666 } else {
Joerg Roedel34ee6452014-11-13 13:46:09 +11002667 if (cow) {
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07002668 huge_ptep_set_wrprotect(src, addr, src_pte);
Joerg Roedel34ee6452014-11-13 13:46:09 +11002669 mmu_notifier_invalidate_range(src, mmun_start,
2670 mmun_end);
2671 }
Naoya Horiguchi0253d632014-07-23 14:00:19 -07002672 entry = huge_ptep_get(src_pte);
Hugh Dickins1c598272005-10-19 21:23:43 -07002673 ptepage = pte_page(entry);
2674 get_page(ptepage);
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09002675 page_dup_rmap(ptepage);
Hugh Dickins1c598272005-10-19 21:23:43 -07002676 set_huge_pte_at(dst, addr, dst_pte, entry);
2677 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08002678 spin_unlock(src_ptl);
2679 spin_unlock(dst_ptl);
David Gibson63551ae2005-06-21 17:14:44 -07002680 }
David Gibson63551ae2005-06-21 17:14:44 -07002681
Andreas Sandberge8569dd2014-01-21 15:49:09 -08002682 if (cow)
2683 mmu_notifier_invalidate_range_end(src, mmun_start, mmun_end);
2684
2685 return ret;
David Gibson63551ae2005-06-21 17:14:44 -07002686}
2687
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07002688void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
2689 unsigned long start, unsigned long end,
2690 struct page *ref_page)
David Gibson63551ae2005-06-21 17:14:44 -07002691{
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07002692 int force_flush = 0;
David Gibson63551ae2005-06-21 17:14:44 -07002693 struct mm_struct *mm = vma->vm_mm;
2694 unsigned long address;
David Gibsonc7546f82005-08-05 11:59:35 -07002695 pte_t *ptep;
David Gibson63551ae2005-06-21 17:14:44 -07002696 pte_t pte;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08002697 spinlock_t *ptl;
David Gibson63551ae2005-06-21 17:14:44 -07002698 struct page *page;
Andi Kleena5516432008-07-23 21:27:41 -07002699 struct hstate *h = hstate_vma(vma);
2700 unsigned long sz = huge_page_size(h);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002701 const unsigned long mmun_start = start; /* For mmu_notifiers */
2702 const unsigned long mmun_end = end; /* For mmu_notifiers */
Andi Kleena5516432008-07-23 21:27:41 -07002703
David Gibson63551ae2005-06-21 17:14:44 -07002704 WARN_ON(!is_vm_hugetlb_page(vma));
Andi Kleena5516432008-07-23 21:27:41 -07002705 BUG_ON(start & ~huge_page_mask(h));
2706 BUG_ON(end & ~huge_page_mask(h));
David Gibson63551ae2005-06-21 17:14:44 -07002707
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07002708 tlb_start_vma(tlb, vma);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002709 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Hillf Danton569f48b2014-12-10 15:44:41 -08002710 address = start;
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07002711again:
Hillf Danton569f48b2014-12-10 15:44:41 -08002712 for (; address < end; address += sz) {
David Gibsonc7546f82005-08-05 11:59:35 -07002713 ptep = huge_pte_offset(mm, address);
Adam Litke4c887262005-10-29 18:16:46 -07002714 if (!ptep)
David Gibsonc7546f82005-08-05 11:59:35 -07002715 continue;
2716
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08002717 ptl = huge_pte_lock(h, mm, ptep);
Chen, Kenneth W39dde652006-12-06 20:32:03 -08002718 if (huge_pmd_unshare(mm, &address, ptep))
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08002719 goto unlock;
Chen, Kenneth W39dde652006-12-06 20:32:03 -08002720
Hillf Danton66293262012-03-23 15:01:48 -07002721 pte = huge_ptep_get(ptep);
2722 if (huge_pte_none(pte))
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08002723 goto unlock;
Hillf Danton66293262012-03-23 15:01:48 -07002724
2725 /*
Naoya Horiguchi9fbc1f62015-02-11 15:25:32 -08002726 * Migrating hugepage or HWPoisoned hugepage is already
2727 * unmapped and its refcount is dropped, so just clear pte here.
Hillf Danton66293262012-03-23 15:01:48 -07002728 */
Naoya Horiguchi9fbc1f62015-02-11 15:25:32 -08002729 if (unlikely(!pte_present(pte))) {
Gerald Schaefer106c9922013-04-29 15:07:23 -07002730 huge_pte_clear(mm, address, ptep);
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08002731 goto unlock;
Naoya Horiguchi8c4894c2012-12-12 13:52:28 -08002732 }
Hillf Danton66293262012-03-23 15:01:48 -07002733
2734 page = pte_page(pte);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002735 /*
2736 * If a reference page is supplied, it is because a specific
2737 * page is being unmapped, not a range. Ensure the page we
2738 * are about to unmap is the actual page of interest.
2739 */
2740 if (ref_page) {
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002741 if (page != ref_page)
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08002742 goto unlock;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002743
2744 /*
2745 * Mark the VMA as having unmapped its page so that
2746 * future faults in this VMA will fail rather than
2747 * looking like data was lost
2748 */
2749 set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
2750 }
2751
David Gibsonc7546f82005-08-05 11:59:35 -07002752 pte = huge_ptep_get_and_clear(mm, address, ptep);
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07002753 tlb_remove_tlb_entry(tlb, ptep, address);
Gerald Schaefer106c9922013-04-29 15:07:23 -07002754 if (huge_pte_dirty(pte))
Ken Chen6649a382007-02-08 14:20:27 -08002755 set_page_dirty(page);
Hillf Danton9e811302012-03-21 16:34:03 -07002756
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07002757 page_remove_rmap(page);
2758 force_flush = !__tlb_remove_page(tlb, page);
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08002759 if (force_flush) {
Hillf Danton569f48b2014-12-10 15:44:41 -08002760 address += sz;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08002761 spin_unlock(ptl);
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07002762 break;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08002763 }
Hillf Danton9e811302012-03-21 16:34:03 -07002764 /* Bail out after unmapping reference page if supplied */
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08002765 if (ref_page) {
2766 spin_unlock(ptl);
Hillf Danton9e811302012-03-21 16:34:03 -07002767 break;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08002768 }
2769unlock:
2770 spin_unlock(ptl);
David Gibson63551ae2005-06-21 17:14:44 -07002771 }
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07002772 /*
2773 * mmu_gather ran out of room to batch pages, we break out of
2774 * the PTE lock to avoid doing the potential expensive TLB invalidate
2775 * and page-free while holding it.
2776 */
2777 if (force_flush) {
2778 force_flush = 0;
2779 tlb_flush_mmu(tlb);
2780 if (address < end && !ref_page)
2781 goto again;
Chen, Kenneth Wfe1668a2006-10-04 02:15:24 -07002782 }
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002783 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07002784 tlb_end_vma(tlb, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785}
David Gibson63551ae2005-06-21 17:14:44 -07002786
Mel Gormand8333522012-07-31 16:46:20 -07002787void __unmap_hugepage_range_final(struct mmu_gather *tlb,
2788 struct vm_area_struct *vma, unsigned long start,
2789 unsigned long end, struct page *ref_page)
2790{
2791 __unmap_hugepage_range(tlb, vma, start, end, ref_page);
2792
2793 /*
2794 * Clear this flag so that x86's huge_pmd_share page_table_shareable
2795 * test will fail on a vma being torn down, and not grab a page table
2796 * on its way out. We're lucky that the flag has such an appropriate
2797 * name, and can in fact be safely cleared here. We could clear it
2798 * before the __unmap_hugepage_range above, but all that's necessary
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08002799 * is to clear it before releasing the i_mmap_rwsem. This works
Mel Gormand8333522012-07-31 16:46:20 -07002800 * because in the context this is called, the VMA is about to be
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08002801 * destroyed and the i_mmap_rwsem is held.
Mel Gormand8333522012-07-31 16:46:20 -07002802 */
2803 vma->vm_flags &= ~VM_MAYSHARE;
2804}
2805
Chen, Kenneth W502717f2006-10-11 01:20:46 -07002806void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002807 unsigned long end, struct page *ref_page)
Chen, Kenneth W502717f2006-10-11 01:20:46 -07002808{
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07002809 struct mm_struct *mm;
2810 struct mmu_gather tlb;
2811
2812 mm = vma->vm_mm;
2813
Linus Torvalds2b047252013-08-15 11:42:25 -07002814 tlb_gather_mmu(&tlb, mm, start, end);
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07002815 __unmap_hugepage_range(&tlb, vma, start, end, ref_page);
2816 tlb_finish_mmu(&tlb, start, end);
Chen, Kenneth W502717f2006-10-11 01:20:46 -07002817}
2818
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002819/*
2820 * This is called when the original mapper is failing to COW a MAP_PRIVATE
2821 * mappping it owns the reserve page for. The intention is to unmap the page
2822 * from other VMAs and let the children be SIGKILLed if they are faulting the
2823 * same region.
2824 */
Davidlohr Bueso2f4612a2014-08-06 16:06:45 -07002825static void unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
2826 struct page *page, unsigned long address)
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002827{
Adam Litke75266742008-11-12 13:24:56 -08002828 struct hstate *h = hstate_vma(vma);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002829 struct vm_area_struct *iter_vma;
2830 struct address_space *mapping;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002831 pgoff_t pgoff;
2832
2833 /*
2834 * vm_pgoff is in PAGE_SIZE units, hence the different calculation
2835 * from page cache lookup which is in HPAGE_SIZE units.
2836 */
Adam Litke75266742008-11-12 13:24:56 -08002837 address = address & huge_page_mask(h);
Michal Hocko36e4f202012-10-08 16:33:31 -07002838 pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) +
2839 vma->vm_pgoff;
Al Viro496ad9a2013-01-23 17:07:38 -05002840 mapping = file_inode(vma->vm_file)->i_mapping;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002841
Mel Gorman4eb2b1d2009-12-14 17:59:53 -08002842 /*
2843 * Take the mapping lock for the duration of the table walk. As
2844 * this mapping should be shared between all the VMAs,
2845 * __unmap_hugepage_range() is called as the lock is already held
2846 */
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08002847 i_mmap_lock_write(mapping);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -07002848 vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) {
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002849 /* Do not unmap the current VMA */
2850 if (iter_vma == vma)
2851 continue;
2852
2853 /*
2854 * Unmap the page from other VMAs without their own reserves.
2855 * They get marked to be SIGKILLed if they fault in these
2856 * areas. This is because a future no-page fault on this VMA
2857 * could insert a zeroed page instead of the data existing
2858 * from the time of fork. This would look like data corruption
2859 */
2860 if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
Aneesh Kumar K.V24669e52012-07-31 16:42:03 -07002861 unmap_hugepage_range(iter_vma, address,
2862 address + huge_page_size(h), page);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002863 }
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08002864 i_mmap_unlock_write(mapping);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002865}
2866
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09002867/*
2868 * Hugetlb_cow() should be called with page lock of the original hugepage held.
Michal Hockoef009b22012-01-10 15:07:21 -08002869 * Called with hugetlb_instantiation_mutex held and pte_page locked so we
2870 * cannot race with other handlers or page migration.
2871 * Keep the pte_same checks anyway to make transition from the mutex easier.
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09002872 */
David Gibson1e8f8892006-01-06 00:10:44 -08002873static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002874 unsigned long address, pte_t *ptep, pte_t pte,
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08002875 struct page *pagecache_page, spinlock_t *ptl)
David Gibson1e8f8892006-01-06 00:10:44 -08002876{
Andi Kleena5516432008-07-23 21:27:41 -07002877 struct hstate *h = hstate_vma(vma);
David Gibson1e8f8892006-01-06 00:10:44 -08002878 struct page *old_page, *new_page;
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07002879 int ret = 0, outside_reserve = 0;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002880 unsigned long mmun_start; /* For mmu_notifiers */
2881 unsigned long mmun_end; /* For mmu_notifiers */
David Gibson1e8f8892006-01-06 00:10:44 -08002882
2883 old_page = pte_page(pte);
2884
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002885retry_avoidcopy:
David Gibson1e8f8892006-01-06 00:10:44 -08002886 /* If no-one else is actually using this page, avoid the copy
2887 * and just make the page writable */
Joonsoo Kim37a21402013-09-11 14:21:04 -07002888 if (page_mapcount(old_page) == 1 && PageAnon(old_page)) {
2889 page_move_anon_rmap(old_page, vma, address);
David Gibson1e8f8892006-01-06 00:10:44 -08002890 set_huge_ptep_writable(vma, address, ptep);
Nick Piggin83c54072007-07-19 01:47:05 -07002891 return 0;
David Gibson1e8f8892006-01-06 00:10:44 -08002892 }
2893
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002894 /*
2895 * If the process that created a MAP_PRIVATE mapping is about to
2896 * perform a COW due to a shared page count, attempt to satisfy
2897 * the allocation without using the existing reserves. The pagecache
2898 * page is used to determine if the reserve at this address was
2899 * consumed or not. If reserves were used, a partial faulted mapping
2900 * at the time of fork() could consume its reserves on COW instead
2901 * of the full address range.
2902 */
Joonsoo Kim5944d012013-09-11 14:21:55 -07002903 if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002904 old_page != pagecache_page)
2905 outside_reserve = 1;
2906
David Gibson1e8f8892006-01-06 00:10:44 -08002907 page_cache_get(old_page);
Larry Woodmanb76c8cf2009-12-14 17:59:37 -08002908
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07002909 /*
2910 * Drop page table lock as buddy allocator may be called. It will
2911 * be acquired again before returning to the caller, as expected.
2912 */
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08002913 spin_unlock(ptl);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002914 new_page = alloc_huge_page(vma, address, outside_reserve);
David Gibson1e8f8892006-01-06 00:10:44 -08002915
Adam Litke2fc39ce2007-11-14 16:59:39 -08002916 if (IS_ERR(new_page)) {
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002917 /*
2918 * If a process owning a MAP_PRIVATE mapping fails to COW,
2919 * it is due to references held by a child and an insufficient
2920 * huge page pool. To guarantee the original mappers
2921 * reliability, unmap the page from child processes. The child
2922 * may get SIGKILLed if it later faults.
2923 */
2924 if (outside_reserve) {
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07002925 page_cache_release(old_page);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002926 BUG_ON(huge_pte_none(pte));
Davidlohr Bueso2f4612a2014-08-06 16:06:45 -07002927 unmap_ref_private(mm, vma, old_page, address);
2928 BUG_ON(huge_pte_none(pte));
2929 spin_lock(ptl);
2930 ptep = huge_pte_offset(mm, address & huge_page_mask(h));
2931 if (likely(ptep &&
2932 pte_same(huge_ptep_get(ptep), pte)))
2933 goto retry_avoidcopy;
2934 /*
2935 * race occurs while re-acquiring page table
2936 * lock, and our job is done.
2937 */
2938 return 0;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002939 }
2940
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07002941 ret = (PTR_ERR(new_page) == -ENOMEM) ?
2942 VM_FAULT_OOM : VM_FAULT_SIGBUS;
2943 goto out_release_old;
David Gibson1e8f8892006-01-06 00:10:44 -08002944 }
2945
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09002946 /*
2947 * When the original hugepage is shared one, it does not have
2948 * anon_vma prepared.
2949 */
Dean Nelson44e2aa92010-10-26 14:22:08 -07002950 if (unlikely(anon_vma_prepare(vma))) {
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07002951 ret = VM_FAULT_OOM;
2952 goto out_release_all;
Dean Nelson44e2aa92010-10-26 14:22:08 -07002953 }
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09002954
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08002955 copy_user_huge_page(new_page, old_page, address, vma,
2956 pages_per_huge_page(h));
Nick Piggin0ed361d2008-02-04 22:29:34 -08002957 __SetPageUptodate(new_page);
David Gibson1e8f8892006-01-06 00:10:44 -08002958
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002959 mmun_start = address & huge_page_mask(h);
2960 mmun_end = mmun_start + huge_page_size(h);
2961 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07002962
Larry Woodmanb76c8cf2009-12-14 17:59:37 -08002963 /*
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08002964 * Retake the page table lock to check for racing updates
Larry Woodmanb76c8cf2009-12-14 17:59:37 -08002965 * before the page tables are altered
2966 */
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08002967 spin_lock(ptl);
Andi Kleena5516432008-07-23 21:27:41 -07002968 ptep = huge_pte_offset(mm, address & huge_page_mask(h));
Naoya Horiguchia9af0c52014-04-07 15:36:54 -07002969 if (likely(ptep && pte_same(huge_ptep_get(ptep), pte))) {
Joonsoo Kim07443a82013-09-11 14:21:58 -07002970 ClearPagePrivate(new_page);
2971
David Gibson1e8f8892006-01-06 00:10:44 -08002972 /* Break COW */
Gerald Schaefer8fe627e2008-04-28 02:13:28 -07002973 huge_ptep_clear_flush(vma, address, ptep);
Joerg Roedel34ee6452014-11-13 13:46:09 +11002974 mmu_notifier_invalidate_range(mm, mmun_start, mmun_end);
David Gibson1e8f8892006-01-06 00:10:44 -08002975 set_huge_pte_at(mm, address, ptep,
2976 make_huge_pte(vma, new_page, 1));
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09002977 page_remove_rmap(old_page);
Naoya Horiguchicd67f0d2010-09-10 13:23:04 +09002978 hugepage_add_new_anon_rmap(new_page, vma, address);
David Gibson1e8f8892006-01-06 00:10:44 -08002979 /* Make the old page be freed below */
2980 new_page = old_page;
2981 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08002982 spin_unlock(ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002983 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07002984out_release_all:
David Gibson1e8f8892006-01-06 00:10:44 -08002985 page_cache_release(new_page);
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07002986out_release_old:
David Gibson1e8f8892006-01-06 00:10:44 -08002987 page_cache_release(old_page);
Joonsoo Kim83120342013-09-11 14:21:57 -07002988
Davidlohr Buesoad4404a2014-08-06 16:06:47 -07002989 spin_lock(ptl); /* Caller expects lock to be held */
2990 return ret;
David Gibson1e8f8892006-01-06 00:10:44 -08002991}
2992
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002993/* Return the pagecache page at a given address within a VMA */
Andi Kleena5516432008-07-23 21:27:41 -07002994static struct page *hugetlbfs_pagecache_page(struct hstate *h,
2995 struct vm_area_struct *vma, unsigned long address)
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002996{
2997 struct address_space *mapping;
Andy Whitcrofte7c4b0b2008-07-23 21:27:26 -07002998 pgoff_t idx;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07002999
3000 mapping = vma->vm_file->f_mapping;
Andi Kleena5516432008-07-23 21:27:41 -07003001 idx = vma_hugecache_offset(h, vma, address);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003002
3003 return find_lock_page(mapping, idx);
3004}
3005
Hugh Dickins3ae77f42009-09-21 17:03:33 -07003006/*
3007 * Return whether there is a pagecache page to back given address within VMA.
3008 * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page.
3009 */
3010static bool hugetlbfs_pagecache_present(struct hstate *h,
Hugh Dickins2a15efc2009-09-21 17:03:27 -07003011 struct vm_area_struct *vma, unsigned long address)
3012{
3013 struct address_space *mapping;
3014 pgoff_t idx;
3015 struct page *page;
3016
3017 mapping = vma->vm_file->f_mapping;
3018 idx = vma_hugecache_offset(h, vma, address);
3019
3020 page = find_get_page(mapping, idx);
3021 if (page)
3022 put_page(page);
3023 return page != NULL;
3024}
3025
Robert P. J. Daya1ed3dd2007-07-17 04:03:33 -07003026static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
Davidlohr Bueso8382d912014-04-03 14:47:31 -07003027 struct address_space *mapping, pgoff_t idx,
3028 unsigned long address, pte_t *ptep, unsigned int flags)
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01003029{
Andi Kleena5516432008-07-23 21:27:41 -07003030 struct hstate *h = hstate_vma(vma);
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01003031 int ret = VM_FAULT_SIGBUS;
Hillf Danton409eb8c2012-01-20 14:34:13 -08003032 int anon_rmap = 0;
Adam Litke4c887262005-10-29 18:16:46 -07003033 unsigned long size;
Adam Litke4c887262005-10-29 18:16:46 -07003034 struct page *page;
David Gibson1e8f8892006-01-06 00:10:44 -08003035 pte_t new_pte;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003036 spinlock_t *ptl;
Adam Litke4c887262005-10-29 18:16:46 -07003037
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003038 /*
3039 * Currently, we are forced to kill the process in the event the
3040 * original mapper has unmapped pages from the child due to a failed
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003041 * COW. Warn that such a situation has occurred as it may not be obvious
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003042 */
3043 if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
Andrew Mortonffb22af2013-02-22 16:32:08 -08003044 pr_warning("PID %d killed due to inadequate hugepage pool\n",
3045 current->pid);
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003046 return ret;
3047 }
3048
Adam Litke4c887262005-10-29 18:16:46 -07003049 /*
3050 * Use page lock to guard against racing truncation
3051 * before we get page_table_lock.
3052 */
Christoph Lameter6bda6662006-01-06 00:10:49 -08003053retry:
3054 page = find_lock_page(mapping, idx);
3055 if (!page) {
Andi Kleena5516432008-07-23 21:27:41 -07003056 size = i_size_read(mapping->host) >> huge_page_shift(h);
Hugh Dickinsebed4bf2006-10-28 10:38:43 -07003057 if (idx >= size)
3058 goto out;
Mel Gorman04f2cbe2008-07-23 21:27:25 -07003059 page = alloc_huge_page(vma, address, 0);
Adam Litke2fc39ce2007-11-14 16:59:39 -08003060 if (IS_ERR(page)) {
Aneesh Kumar K.V76dcee72012-07-31 16:41:57 -07003061 ret = PTR_ERR(page);
3062 if (ret == -ENOMEM)
3063 ret = VM_FAULT_OOM;
3064 else
3065 ret = VM_FAULT_SIGBUS;
Christoph Lameter6bda6662006-01-06 00:10:49 -08003066 goto out;
3067 }
Andrea Arcangeli47ad8472011-01-13 15:46:47 -08003068 clear_huge_page(page, address, pages_per_huge_page(h));
Nick Piggin0ed361d2008-02-04 22:29:34 -08003069 __SetPageUptodate(page);
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01003070
Mel Gormanf83a2752009-05-28 14:34:40 -07003071 if (vma->vm_flags & VM_MAYSHARE) {
Christoph Lameter6bda6662006-01-06 00:10:49 -08003072 int err;
Ken Chen45c682a2007-11-14 16:59:44 -08003073 struct inode *inode = mapping->host;
Christoph Lameter6bda6662006-01-06 00:10:49 -08003074
3075 err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
3076 if (err) {
3077 put_page(page);
Christoph Lameter6bda6662006-01-06 00:10:49 -08003078 if (err == -EEXIST)
3079 goto retry;
3080 goto out;
3081 }
Joonsoo Kim07443a82013-09-11 14:21:58 -07003082 ClearPagePrivate(page);
Ken Chen45c682a2007-11-14 16:59:44 -08003083
3084 spin_lock(&inode->i_lock);
Andi Kleena5516432008-07-23 21:27:41 -07003085 inode->i_blocks += blocks_per_huge_page(h);
Ken Chen45c682a2007-11-14 16:59:44 -08003086 spin_unlock(&inode->i_lock);
Mel Gorman23be7462010-04-23 13:17:56 -04003087 } else {
Christoph Lameter6bda6662006-01-06 00:10:49 -08003088 lock_page(page);
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09003089 if (unlikely(anon_vma_prepare(vma))) {
3090 ret = VM_FAULT_OOM;
3091 goto backout_unlocked;
3092 }
Hillf Danton409eb8c2012-01-20 14:34:13 -08003093 anon_rmap = 1;
Mel Gorman23be7462010-04-23 13:17:56 -04003094 }
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09003095 } else {
Naoya Horiguchi998b4382010-09-08 10:19:32 +09003096 /*
3097 * If memory error occurs between mmap() and fault, some process
3098 * don't have hwpoisoned swap entry for errored virtual address.
3099 * So we need to block hugepage fault by PG_hwpoison bit check.
3100 */
3101 if (unlikely(PageHWPoison(page))) {
Chris Forbes32f84522011-07-25 17:12:14 -07003102 ret = VM_FAULT_HWPOISON |
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07003103 VM_FAULT_SET_HINDEX(hstate_index(h));
Naoya Horiguchi998b4382010-09-08 10:19:32 +09003104 goto backout_unlocked;
3105 }
Christoph Lameter6bda6662006-01-06 00:10:49 -08003106 }
David Gibson1e8f8892006-01-06 00:10:44 -08003107
Andy Whitcroft57303d82008-08-12 15:08:47 -07003108 /*
3109 * If we are going to COW a private mapping later, we examine the
3110 * pending reservations for this page now. This will ensure that
3111 * any allocations necessary to record that reservation occur outside
3112 * the spinlock.
3113 */
Hugh Dickins788c7df2009-06-23 13:49:05 +01003114 if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED))
Andy Whitcroft2b267362008-08-12 15:08:49 -07003115 if (vma_needs_reservation(h, vma, address) < 0) {
3116 ret = VM_FAULT_OOM;
3117 goto backout_unlocked;
3118 }
Andy Whitcroft57303d82008-08-12 15:08:47 -07003119
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003120 ptl = huge_pte_lockptr(h, mm, ptep);
3121 spin_lock(ptl);
Andi Kleena5516432008-07-23 21:27:41 -07003122 size = i_size_read(mapping->host) >> huge_page_shift(h);
Adam Litke4c887262005-10-29 18:16:46 -07003123 if (idx >= size)
3124 goto backout;
3125
Nick Piggin83c54072007-07-19 01:47:05 -07003126 ret = 0;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07003127 if (!huge_pte_none(huge_ptep_get(ptep)))
Adam Litke4c887262005-10-29 18:16:46 -07003128 goto backout;
3129
Joonsoo Kim07443a82013-09-11 14:21:58 -07003130 if (anon_rmap) {
3131 ClearPagePrivate(page);
Hillf Danton409eb8c2012-01-20 14:34:13 -08003132 hugepage_add_new_anon_rmap(page, vma, address);
Choi Gi-yongac714902014-04-07 15:37:36 -07003133 } else
Hillf Danton409eb8c2012-01-20 14:34:13 -08003134 page_dup_rmap(page);
David Gibson1e8f8892006-01-06 00:10:44 -08003135 new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
3136 && (vma->vm_flags & VM_SHARED)));
3137 set_huge_pte_at(mm, address, ptep, new_pte);
3138
Hugh Dickins788c7df2009-06-23 13:49:05 +01003139 if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
David Gibson1e8f8892006-01-06 00:10:44 -08003140 /* Optimization, do the COW without a second fault */
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003141 ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page, ptl);
David Gibson1e8f8892006-01-06 00:10:44 -08003142 }
3143
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003144 spin_unlock(ptl);
Adam Litke4c887262005-10-29 18:16:46 -07003145 unlock_page(page);
3146out:
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01003147 return ret;
Adam Litke4c887262005-10-29 18:16:46 -07003148
3149backout:
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003150 spin_unlock(ptl);
Andy Whitcroft2b267362008-08-12 15:08:49 -07003151backout_unlocked:
Adam Litke4c887262005-10-29 18:16:46 -07003152 unlock_page(page);
3153 put_page(page);
3154 goto out;
Hugh Dickinsac9b9c62005-10-20 16:24:28 +01003155}
3156
Davidlohr Bueso8382d912014-04-03 14:47:31 -07003157#ifdef CONFIG_SMP
3158static u32 fault_mutex_hash(struct hstate *h, struct mm_struct *mm,
3159 struct vm_area_struct *vma,
3160 struct address_space *mapping,
3161 pgoff_t idx, unsigned long address)
3162{
3163 unsigned long key[2];
3164 u32 hash;
3165
3166 if (vma->vm_flags & VM_SHARED) {
3167 key[0] = (unsigned long) mapping;
3168 key[1] = idx;
3169 } else {
3170 key[0] = (unsigned long) mm;
3171 key[1] = address >> huge_page_shift(h);
3172 }
3173
3174 hash = jhash2((u32 *)&key, sizeof(key)/sizeof(u32), 0);
3175
3176 return hash & (num_fault_mutexes - 1);
3177}
3178#else
3179/*
3180 * For uniprocesor systems we always use a single mutex, so just
3181 * return 0 and avoid the hashing overhead.
3182 */
3183static u32 fault_mutex_hash(struct hstate *h, struct mm_struct *mm,
3184 struct vm_area_struct *vma,
3185 struct address_space *mapping,
3186 pgoff_t idx, unsigned long address)
3187{
3188 return 0;
3189}
3190#endif
3191
Adam Litke86e52162006-01-06 00:10:43 -08003192int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
Hugh Dickins788c7df2009-06-23 13:49:05 +01003193 unsigned long address, unsigned int flags)
Adam Litke86e52162006-01-06 00:10:43 -08003194{
Davidlohr Bueso8382d912014-04-03 14:47:31 -07003195 pte_t *ptep, entry;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003196 spinlock_t *ptl;
David Gibson1e8f8892006-01-06 00:10:44 -08003197 int ret;
Davidlohr Bueso8382d912014-04-03 14:47:31 -07003198 u32 hash;
3199 pgoff_t idx;
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09003200 struct page *page = NULL;
Andy Whitcroft57303d82008-08-12 15:08:47 -07003201 struct page *pagecache_page = NULL;
Andi Kleena5516432008-07-23 21:27:41 -07003202 struct hstate *h = hstate_vma(vma);
Davidlohr Bueso8382d912014-04-03 14:47:31 -07003203 struct address_space *mapping;
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08003204 int need_wait_lock = 0;
Adam Litke86e52162006-01-06 00:10:43 -08003205
KAMEZAWA Hiroyuki1e16a532012-01-10 15:07:22 -08003206 address &= huge_page_mask(h);
3207
Naoya Horiguchifd6a03ed2010-05-28 09:29:21 +09003208 ptep = huge_pte_offset(mm, address);
3209 if (ptep) {
3210 entry = huge_ptep_get(ptep);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09003211 if (unlikely(is_hugetlb_entry_migration(entry))) {
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003212 migration_entry_wait_huge(vma, mm, ptep);
Naoya Horiguchi290408d2010-09-08 10:19:35 +09003213 return 0;
3214 } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry)))
Chris Forbes32f84522011-07-25 17:12:14 -07003215 return VM_FAULT_HWPOISON_LARGE |
Aneesh Kumar K.V972dc4d2012-07-31 16:42:00 -07003216 VM_FAULT_SET_HINDEX(hstate_index(h));
Naoya Horiguchifd6a03ed2010-05-28 09:29:21 +09003217 }
3218
Andi Kleena5516432008-07-23 21:27:41 -07003219 ptep = huge_pte_alloc(mm, address, huge_page_size(h));
Adam Litke86e52162006-01-06 00:10:43 -08003220 if (!ptep)
3221 return VM_FAULT_OOM;
3222
Davidlohr Bueso8382d912014-04-03 14:47:31 -07003223 mapping = vma->vm_file->f_mapping;
3224 idx = vma_hugecache_offset(h, vma, address);
3225
David Gibson3935baa2006-03-22 00:08:53 -08003226 /*
3227 * Serialize hugepage allocation and instantiation, so that we don't
3228 * get spurious allocation failures if two CPUs race to instantiate
3229 * the same page in the page cache.
3230 */
Davidlohr Bueso8382d912014-04-03 14:47:31 -07003231 hash = fault_mutex_hash(h, mm, vma, mapping, idx, address);
3232 mutex_lock(&htlb_fault_mutex_table[hash]);
3233
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07003234 entry = huge_ptep_get(ptep);
3235 if (huge_pte_none(entry)) {
Davidlohr Bueso8382d912014-04-03 14:47:31 -07003236 ret = hugetlb_no_page(mm, vma, mapping, idx, address, ptep, flags);
David Gibsonb4d1d992008-10-15 22:01:11 -07003237 goto out_mutex;
David Gibson3935baa2006-03-22 00:08:53 -08003238 }
Adam Litke86e52162006-01-06 00:10:43 -08003239
Nick Piggin83c54072007-07-19 01:47:05 -07003240 ret = 0;
David Gibson1e8f8892006-01-06 00:10:44 -08003241
Andy Whitcroft57303d82008-08-12 15:08:47 -07003242 /*
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08003243 * entry could be a migration/hwpoison entry at this point, so this
3244 * check prevents the kernel from going below assuming that we have
3245 * a active hugepage in pagecache. This goto expects the 2nd page fault,
3246 * and is_hugetlb_entry_(migration|hwpoisoned) check will properly
3247 * handle it.
3248 */
3249 if (!pte_present(entry))
3250 goto out_mutex;
3251
3252 /*
Andy Whitcroft57303d82008-08-12 15:08:47 -07003253 * If we are going to COW the mapping later, we examine the pending
3254 * reservations for this page now. This will ensure that any
3255 * allocations necessary to record that reservation occur outside the
3256 * spinlock. For private mappings, we also lookup the pagecache
3257 * page now as it is used to determine if a reservation has been
3258 * consumed.
3259 */
Gerald Schaefer106c9922013-04-29 15:07:23 -07003260 if ((flags & FAULT_FLAG_WRITE) && !huge_pte_write(entry)) {
Andy Whitcroft2b267362008-08-12 15:08:49 -07003261 if (vma_needs_reservation(h, vma, address) < 0) {
3262 ret = VM_FAULT_OOM;
David Gibsonb4d1d992008-10-15 22:01:11 -07003263 goto out_mutex;
Andy Whitcroft2b267362008-08-12 15:08:49 -07003264 }
Andy Whitcroft57303d82008-08-12 15:08:47 -07003265
Mel Gormanf83a2752009-05-28 14:34:40 -07003266 if (!(vma->vm_flags & VM_MAYSHARE))
Andy Whitcroft57303d82008-08-12 15:08:47 -07003267 pagecache_page = hugetlbfs_pagecache_page(h,
3268 vma, address);
3269 }
3270
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08003271 ptl = huge_pte_lock(h, mm, ptep);
Naoya Horiguchi0fe6e202010-05-28 09:29:16 +09003272
David Gibson1e8f8892006-01-06 00:10:44 -08003273 /* Check for a racing update before calling hugetlb_cow */
David Gibsonb4d1d992008-10-15 22:01:11 -07003274 if (unlikely(!pte_same(entry, huge_ptep_get(ptep))))
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003275 goto out_ptl;
David Gibsonb4d1d992008-10-15 22:01:11 -07003276
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08003277 /*
3278 * hugetlb_cow() requires page locks of pte_page(entry) and
3279 * pagecache_page, so here we need take the former one
3280 * when page != pagecache_page or !pagecache_page.
3281 */
3282 page = pte_page(entry);
3283 if (page != pagecache_page)
3284 if (!trylock_page(page)) {
3285 need_wait_lock = 1;
3286 goto out_ptl;
3287 }
3288
3289 get_page(page);
David Gibsonb4d1d992008-10-15 22:01:11 -07003290
Hugh Dickins788c7df2009-06-23 13:49:05 +01003291 if (flags & FAULT_FLAG_WRITE) {
Gerald Schaefer106c9922013-04-29 15:07:23 -07003292 if (!huge_pte_write(entry)) {
Andy Whitcroft57303d82008-08-12 15:08:47 -07003293 ret = hugetlb_cow(mm, vma, address, ptep, entry,
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003294 pagecache_page, ptl);
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08003295 goto out_put_page;
David Gibsonb4d1d992008-10-15 22:01:11 -07003296 }
Gerald Schaefer106c9922013-04-29 15:07:23 -07003297 entry = huge_pte_mkdirty(entry);
David Gibsonb4d1d992008-10-15 22:01:11 -07003298 }
3299 entry = pte_mkyoung(entry);
Hugh Dickins788c7df2009-06-23 13:49:05 +01003300 if (huge_ptep_set_access_flags(vma, address, ptep, entry,
3301 flags & FAULT_FLAG_WRITE))
Russell King4b3073e2009-12-18 16:40:18 +00003302 update_mmu_cache(vma, address, ptep);
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08003303out_put_page:
3304 if (page != pagecache_page)
3305 unlock_page(page);
3306 put_page(page);
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003307out_ptl:
3308 spin_unlock(ptl);
Andy Whitcroft57303d82008-08-12 15:08:47 -07003309
3310 if (pagecache_page) {
3311 unlock_page(pagecache_page);
3312 put_page(pagecache_page);
3313 }
David Gibsonb4d1d992008-10-15 22:01:11 -07003314out_mutex:
Davidlohr Bueso8382d912014-04-03 14:47:31 -07003315 mutex_unlock(&htlb_fault_mutex_table[hash]);
Naoya Horiguchi0f792cf2015-02-11 15:25:25 -08003316 /*
3317 * Generally it's safe to hold refcount during waiting page lock. But
3318 * here we just wait to defer the next page fault to avoid busy loop and
3319 * the page is not used after unlocked before returning from the current
3320 * page fault. So we are safe from accessing freed page, even if we wait
3321 * here without taking refcount.
3322 */
3323 if (need_wait_lock)
3324 wait_on_page_locked(page);
David Gibson1e8f8892006-01-06 00:10:44 -08003325 return ret;
Adam Litke86e52162006-01-06 00:10:43 -08003326}
3327
Michel Lespinasse28a35712013-02-22 16:35:55 -08003328long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
3329 struct page **pages, struct vm_area_struct **vmas,
3330 unsigned long *position, unsigned long *nr_pages,
3331 long i, unsigned int flags)
David Gibson63551ae2005-06-21 17:14:44 -07003332{
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08003333 unsigned long pfn_offset;
3334 unsigned long vaddr = *position;
Michel Lespinasse28a35712013-02-22 16:35:55 -08003335 unsigned long remainder = *nr_pages;
Andi Kleena5516432008-07-23 21:27:41 -07003336 struct hstate *h = hstate_vma(vma);
David Gibson63551ae2005-06-21 17:14:44 -07003337
David Gibson63551ae2005-06-21 17:14:44 -07003338 while (vaddr < vma->vm_end && remainder) {
Adam Litke4c887262005-10-29 18:16:46 -07003339 pte_t *pte;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003340 spinlock_t *ptl = NULL;
Hugh Dickins2a15efc2009-09-21 17:03:27 -07003341 int absent;
Adam Litke4c887262005-10-29 18:16:46 -07003342 struct page *page;
3343
3344 /*
David Rientjes02057962015-04-14 15:48:24 -07003345 * If we have a pending SIGKILL, don't keep faulting pages and
3346 * potentially allocating memory.
3347 */
3348 if (unlikely(fatal_signal_pending(current))) {
3349 remainder = 0;
3350 break;
3351 }
3352
3353 /*
Adam Litke4c887262005-10-29 18:16:46 -07003354 * Some archs (sparc64, sh*) have multiple pte_ts to
Hugh Dickins2a15efc2009-09-21 17:03:27 -07003355 * each hugepage. We have to make sure we get the
Adam Litke4c887262005-10-29 18:16:46 -07003356 * first, for the page indexing below to work.
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003357 *
3358 * Note that page table lock is not held when pte is null.
Adam Litke4c887262005-10-29 18:16:46 -07003359 */
Andi Kleena5516432008-07-23 21:27:41 -07003360 pte = huge_pte_offset(mm, vaddr & huge_page_mask(h));
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003361 if (pte)
3362 ptl = huge_pte_lock(h, mm, pte);
Hugh Dickins2a15efc2009-09-21 17:03:27 -07003363 absent = !pte || huge_pte_none(huge_ptep_get(pte));
Adam Litke4c887262005-10-29 18:16:46 -07003364
Hugh Dickins2a15efc2009-09-21 17:03:27 -07003365 /*
3366 * When coredumping, it suits get_dump_page if we just return
Hugh Dickins3ae77f42009-09-21 17:03:33 -07003367 * an error where there's an empty slot with no huge pagecache
3368 * to back it. This way, we avoid allocating a hugepage, and
3369 * the sparse dumpfile avoids allocating disk blocks, but its
3370 * huge holes still show up with zeroes where they need to be.
Hugh Dickins2a15efc2009-09-21 17:03:27 -07003371 */
Hugh Dickins3ae77f42009-09-21 17:03:33 -07003372 if (absent && (flags & FOLL_DUMP) &&
3373 !hugetlbfs_pagecache_present(h, vma, vaddr)) {
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003374 if (pte)
3375 spin_unlock(ptl);
Hugh Dickins2a15efc2009-09-21 17:03:27 -07003376 remainder = 0;
3377 break;
3378 }
3379
Naoya Horiguchi9cc3a5b2013-04-17 15:58:30 -07003380 /*
3381 * We need call hugetlb_fault for both hugepages under migration
3382 * (in which case hugetlb_fault waits for the migration,) and
3383 * hwpoisoned hugepages (in which case we need to prevent the
3384 * caller from accessing to them.) In order to do this, we use
3385 * here is_swap_pte instead of is_hugetlb_entry_migration and
3386 * is_hugetlb_entry_hwpoisoned. This is because it simply covers
3387 * both cases, and because we can't follow correct pages
3388 * directly from any kind of swap entries.
3389 */
3390 if (absent || is_swap_pte(huge_ptep_get(pte)) ||
Gerald Schaefer106c9922013-04-29 15:07:23 -07003391 ((flags & FOLL_WRITE) &&
3392 !huge_pte_write(huge_ptep_get(pte)))) {
Adam Litke4c887262005-10-29 18:16:46 -07003393 int ret;
3394
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003395 if (pte)
3396 spin_unlock(ptl);
Hugh Dickins2a15efc2009-09-21 17:03:27 -07003397 ret = hugetlb_fault(mm, vma, vaddr,
3398 (flags & FOLL_WRITE) ? FAULT_FLAG_WRITE : 0);
Adam Litkea89182c2007-08-22 14:01:51 -07003399 if (!(ret & VM_FAULT_ERROR))
Adam Litke4c887262005-10-29 18:16:46 -07003400 continue;
3401
3402 remainder = 0;
Adam Litke4c887262005-10-29 18:16:46 -07003403 break;
3404 }
David Gibson63551ae2005-06-21 17:14:44 -07003405
Andi Kleena5516432008-07-23 21:27:41 -07003406 pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT;
Gerald Schaefer7f2e9522008-04-28 02:13:29 -07003407 page = pte_page(huge_ptep_get(pte));
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08003408same_page:
Chen, Kenneth Wd6692182006-03-31 02:29:57 -08003409 if (pages) {
Hugh Dickins2a15efc2009-09-21 17:03:27 -07003410 pages[i] = mem_map_offset(page, pfn_offset);
Andrea Arcangelia0368d42014-01-21 15:48:49 -08003411 get_page_foll(pages[i]);
Chen, Kenneth Wd6692182006-03-31 02:29:57 -08003412 }
David Gibson63551ae2005-06-21 17:14:44 -07003413
3414 if (vmas)
3415 vmas[i] = vma;
3416
3417 vaddr += PAGE_SIZE;
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08003418 ++pfn_offset;
David Gibson63551ae2005-06-21 17:14:44 -07003419 --remainder;
3420 ++i;
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08003421 if (vaddr < vma->vm_end && remainder &&
Andi Kleena5516432008-07-23 21:27:41 -07003422 pfn_offset < pages_per_huge_page(h)) {
Chen, Kenneth Wd5d4b0a2006-03-22 00:09:03 -08003423 /*
3424 * We use pfn_offset to avoid touching the pageframes
3425 * of this compound page.
3426 */
3427 goto same_page;
3428 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003429 spin_unlock(ptl);
David Gibson63551ae2005-06-21 17:14:44 -07003430 }
Michel Lespinasse28a35712013-02-22 16:35:55 -08003431 *nr_pages = remainder;
David Gibson63551ae2005-06-21 17:14:44 -07003432 *position = vaddr;
3433
Hugh Dickins2a15efc2009-09-21 17:03:27 -07003434 return i ? i : -EFAULT;
David Gibson63551ae2005-06-21 17:14:44 -07003435}
Zhang, Yanmin8f860592006-03-22 00:08:50 -08003436
Peter Zijlstra7da4d642012-11-19 03:14:23 +01003437unsigned long hugetlb_change_protection(struct vm_area_struct *vma,
Zhang, Yanmin8f860592006-03-22 00:08:50 -08003438 unsigned long address, unsigned long end, pgprot_t newprot)
3439{
3440 struct mm_struct *mm = vma->vm_mm;
3441 unsigned long start = address;
3442 pte_t *ptep;
3443 pte_t pte;
Andi Kleena5516432008-07-23 21:27:41 -07003444 struct hstate *h = hstate_vma(vma);
Peter Zijlstra7da4d642012-11-19 03:14:23 +01003445 unsigned long pages = 0;
Zhang, Yanmin8f860592006-03-22 00:08:50 -08003446
3447 BUG_ON(address >= end);
3448 flush_cache_range(vma, address, end);
3449
Rik van Riela5338092014-04-07 15:36:57 -07003450 mmu_notifier_invalidate_range_start(mm, start, end);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08003451 i_mmap_lock_write(vma->vm_file->f_mapping);
Andi Kleena5516432008-07-23 21:27:41 -07003452 for (; address < end; address += huge_page_size(h)) {
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003453 spinlock_t *ptl;
Zhang, Yanmin8f860592006-03-22 00:08:50 -08003454 ptep = huge_pte_offset(mm, address);
3455 if (!ptep)
3456 continue;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003457 ptl = huge_pte_lock(h, mm, ptep);
Peter Zijlstra7da4d642012-11-19 03:14:23 +01003458 if (huge_pmd_unshare(mm, &address, ptep)) {
3459 pages++;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003460 spin_unlock(ptl);
Chen, Kenneth W39dde652006-12-06 20:32:03 -08003461 continue;
Peter Zijlstra7da4d642012-11-19 03:14:23 +01003462 }
Naoya Horiguchia8bda282015-02-11 15:25:28 -08003463 pte = huge_ptep_get(ptep);
3464 if (unlikely(is_hugetlb_entry_hwpoisoned(pte))) {
3465 spin_unlock(ptl);
3466 continue;
3467 }
3468 if (unlikely(is_hugetlb_entry_migration(pte))) {
3469 swp_entry_t entry = pte_to_swp_entry(pte);
3470
3471 if (is_write_migration_entry(entry)) {
3472 pte_t newpte;
3473
3474 make_migration_entry_read(&entry);
3475 newpte = swp_entry_to_pte(entry);
3476 set_huge_pte_at(mm, address, ptep, newpte);
3477 pages++;
3478 }
3479 spin_unlock(ptl);
3480 continue;
3481 }
3482 if (!huge_pte_none(pte)) {
Zhang, Yanmin8f860592006-03-22 00:08:50 -08003483 pte = huge_ptep_get_and_clear(mm, address, ptep);
Gerald Schaefer106c9922013-04-29 15:07:23 -07003484 pte = pte_mkhuge(huge_pte_modify(pte, newprot));
Tony Lube7517d2013-02-04 14:28:46 -08003485 pte = arch_make_huge_pte(pte, vma, NULL, 0);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08003486 set_huge_pte_at(mm, address, ptep, pte);
Peter Zijlstra7da4d642012-11-19 03:14:23 +01003487 pages++;
Zhang, Yanmin8f860592006-03-22 00:08:50 -08003488 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003489 spin_unlock(ptl);
Zhang, Yanmin8f860592006-03-22 00:08:50 -08003490 }
Mel Gormand8333522012-07-31 16:46:20 -07003491 /*
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08003492 * Must flush TLB before releasing i_mmap_rwsem: x86's huge_pmd_unshare
Mel Gormand8333522012-07-31 16:46:20 -07003493 * may have cleared our pud entry and done put_page on the page table:
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08003494 * once we release i_mmap_rwsem, another task can do the final put_page
Mel Gormand8333522012-07-31 16:46:20 -07003495 * and that page table be reused and filled with junk.
3496 */
Zhang, Yanmin8f860592006-03-22 00:08:50 -08003497 flush_tlb_range(vma, start, end);
Joerg Roedel34ee6452014-11-13 13:46:09 +11003498 mmu_notifier_invalidate_range(mm, start, end);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08003499 i_mmap_unlock_write(vma->vm_file->f_mapping);
Rik van Riela5338092014-04-07 15:36:57 -07003500 mmu_notifier_invalidate_range_end(mm, start, end);
Peter Zijlstra7da4d642012-11-19 03:14:23 +01003501
3502 return pages << h->order;
Zhang, Yanmin8f860592006-03-22 00:08:50 -08003503}
3504
Mel Gormana1e78772008-07-23 21:27:23 -07003505int hugetlb_reserve_pages(struct inode *inode,
3506 long from, long to,
Mel Gorman5a6fe122009-02-10 14:02:27 +00003507 struct vm_area_struct *vma,
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09003508 vm_flags_t vm_flags)
Adam Litkee4e574b2007-10-16 01:26:19 -07003509{
Mel Gorman17c9d122009-02-11 16:34:16 +00003510 long ret, chg;
Andi Kleena5516432008-07-23 21:27:41 -07003511 struct hstate *h = hstate_inode(inode);
David Gibson90481622012-03-21 16:34:12 -07003512 struct hugepage_subpool *spool = subpool_inode(inode);
Joonsoo Kim9119a412014-04-03 14:47:25 -07003513 struct resv_map *resv_map;
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07003514 long gbl_reserve;
Adam Litkee4e574b2007-10-16 01:26:19 -07003515
Mel Gormana1e78772008-07-23 21:27:23 -07003516 /*
Mel Gorman17c9d122009-02-11 16:34:16 +00003517 * Only apply hugepage reservation if asked. At fault time, an
3518 * attempt will be made for VM_NORESERVE to allocate a page
David Gibson90481622012-03-21 16:34:12 -07003519 * without using reserves
Mel Gorman17c9d122009-02-11 16:34:16 +00003520 */
KOSAKI Motohiroca16d142011-05-26 19:16:19 +09003521 if (vm_flags & VM_NORESERVE)
Mel Gorman17c9d122009-02-11 16:34:16 +00003522 return 0;
3523
3524 /*
Mel Gormana1e78772008-07-23 21:27:23 -07003525 * Shared mappings base their reservation on the number of pages that
3526 * are already allocated on behalf of the file. Private mappings need
3527 * to reserve the full area even if read-only as mprotect() may be
3528 * called to make the mapping read-write. Assume !vma is a shm mapping
3529 */
Joonsoo Kim9119a412014-04-03 14:47:25 -07003530 if (!vma || vma->vm_flags & VM_MAYSHARE) {
Joonsoo Kim4e35f482014-04-03 14:47:30 -07003531 resv_map = inode_resv_map(inode);
Joonsoo Kim9119a412014-04-03 14:47:25 -07003532
Joonsoo Kim1406ec92014-04-03 14:47:26 -07003533 chg = region_chg(resv_map, from, to);
Joonsoo Kim9119a412014-04-03 14:47:25 -07003534
3535 } else {
3536 resv_map = resv_map_alloc();
Mel Gorman5a6fe122009-02-10 14:02:27 +00003537 if (!resv_map)
3538 return -ENOMEM;
3539
Mel Gorman17c9d122009-02-11 16:34:16 +00003540 chg = to - from;
3541
Mel Gorman5a6fe122009-02-10 14:02:27 +00003542 set_vma_resv_map(vma, resv_map);
3543 set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
3544 }
3545
Dave Hansenc50ac052012-05-29 15:06:46 -07003546 if (chg < 0) {
3547 ret = chg;
3548 goto out_err;
3549 }
Mel Gorman17c9d122009-02-11 16:34:16 +00003550
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07003551 /*
3552 * There must be enough pages in the subpool for the mapping. If
3553 * the subpool has a minimum size, there may be some global
3554 * reservations already in place (gbl_reserve).
3555 */
3556 gbl_reserve = hugepage_subpool_get_pages(spool, chg);
3557 if (gbl_reserve < 0) {
Dave Hansenc50ac052012-05-29 15:06:46 -07003558 ret = -ENOSPC;
3559 goto out_err;
3560 }
Mel Gorman17c9d122009-02-11 16:34:16 +00003561
3562 /*
3563 * Check enough hugepages are available for the reservation.
David Gibson90481622012-03-21 16:34:12 -07003564 * Hand the pages back to the subpool if there are not
Mel Gorman17c9d122009-02-11 16:34:16 +00003565 */
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07003566 ret = hugetlb_acct_memory(h, gbl_reserve);
Mel Gorman17c9d122009-02-11 16:34:16 +00003567 if (ret < 0) {
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07003568 /* put back original number of pages, chg */
3569 (void)hugepage_subpool_put_pages(spool, chg);
Dave Hansenc50ac052012-05-29 15:06:46 -07003570 goto out_err;
Mel Gorman17c9d122009-02-11 16:34:16 +00003571 }
3572
3573 /*
3574 * Account for the reservations made. Shared mappings record regions
3575 * that have reservations as they are shared by multiple VMAs.
3576 * When the last VMA disappears, the region map says how much
3577 * the reservation was and the page cache tells how much of
3578 * the reservation was consumed. Private mappings are per-VMA and
3579 * only the consumed reservations are tracked. When the VMA
3580 * disappears, the original reservation is the VMA size and the
3581 * consumed reservations are stored in the map. Hence, nothing
3582 * else has to be done for private mappings here
3583 */
Mel Gormanf83a2752009-05-28 14:34:40 -07003584 if (!vma || vma->vm_flags & VM_MAYSHARE)
Joonsoo Kim1406ec92014-04-03 14:47:26 -07003585 region_add(resv_map, from, to);
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07003586 return 0;
Dave Hansenc50ac052012-05-29 15:06:46 -07003587out_err:
Joonsoo Kimf031dd22014-04-03 14:47:28 -07003588 if (vma && is_vma_resv_set(vma, HPAGE_RESV_OWNER))
3589 kref_put(&resv_map->refs, resv_map_release);
Dave Hansenc50ac052012-05-29 15:06:46 -07003590 return ret;
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07003591}
3592
3593void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
3594{
Andi Kleena5516432008-07-23 21:27:41 -07003595 struct hstate *h = hstate_inode(inode);
Joonsoo Kim4e35f482014-04-03 14:47:30 -07003596 struct resv_map *resv_map = inode_resv_map(inode);
Joonsoo Kim9119a412014-04-03 14:47:25 -07003597 long chg = 0;
David Gibson90481622012-03-21 16:34:12 -07003598 struct hugepage_subpool *spool = subpool_inode(inode);
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07003599 long gbl_reserve;
Ken Chen45c682a2007-11-14 16:59:44 -08003600
Joonsoo Kim9119a412014-04-03 14:47:25 -07003601 if (resv_map)
Joonsoo Kim1406ec92014-04-03 14:47:26 -07003602 chg = region_truncate(resv_map, offset);
Ken Chen45c682a2007-11-14 16:59:44 -08003603 spin_lock(&inode->i_lock);
Eric Sandeene4c6f8b2009-07-29 15:02:16 -07003604 inode->i_blocks -= (blocks_per_huge_page(h) * freed);
Ken Chen45c682a2007-11-14 16:59:44 -08003605 spin_unlock(&inode->i_lock);
3606
Mike Kravetz1c5ecae2015-04-15 16:13:39 -07003607 /*
3608 * If the subpool has a minimum size, the number of global
3609 * reservations to be released may be adjusted.
3610 */
3611 gbl_reserve = hugepage_subpool_put_pages(spool, (chg - freed));
3612 hugetlb_acct_memory(h, -gbl_reserve);
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -07003613}
Naoya Horiguchi93f70f92010-05-28 09:29:20 +09003614
Steve Capper3212b532013-04-23 12:35:02 +01003615#ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE
3616static unsigned long page_table_shareable(struct vm_area_struct *svma,
3617 struct vm_area_struct *vma,
3618 unsigned long addr, pgoff_t idx)
3619{
3620 unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) +
3621 svma->vm_start;
3622 unsigned long sbase = saddr & PUD_MASK;
3623 unsigned long s_end = sbase + PUD_SIZE;
3624
3625 /* Allow segments to share if only one is marked locked */
3626 unsigned long vm_flags = vma->vm_flags & ~VM_LOCKED;
3627 unsigned long svm_flags = svma->vm_flags & ~VM_LOCKED;
3628
3629 /*
3630 * match the virtual addresses, permission and the alignment of the
3631 * page table page.
3632 */
3633 if (pmd_index(addr) != pmd_index(saddr) ||
3634 vm_flags != svm_flags ||
3635 sbase < svma->vm_start || svma->vm_end < s_end)
3636 return 0;
3637
3638 return saddr;
3639}
3640
3641static int vma_shareable(struct vm_area_struct *vma, unsigned long addr)
3642{
3643 unsigned long base = addr & PUD_MASK;
3644 unsigned long end = base + PUD_SIZE;
3645
3646 /*
3647 * check on proper vm_flags and page table alignment
3648 */
3649 if (vma->vm_flags & VM_MAYSHARE &&
3650 vma->vm_start <= base && end <= vma->vm_end)
3651 return 1;
3652 return 0;
3653}
3654
3655/*
3656 * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc()
3657 * and returns the corresponding pte. While this is not necessary for the
3658 * !shared pmd case because we can allocate the pmd later as well, it makes the
3659 * code much cleaner. pmd allocation is essential for the shared case because
Davidlohr Buesoc8c06ef2014-12-12 16:54:24 -08003660 * pud has to be populated inside the same i_mmap_rwsem section - otherwise
Steve Capper3212b532013-04-23 12:35:02 +01003661 * racing tasks could either miss the sharing (see huge_pte_offset) or select a
3662 * bad pmd for sharing.
3663 */
3664pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)
3665{
3666 struct vm_area_struct *vma = find_vma(mm, addr);
3667 struct address_space *mapping = vma->vm_file->f_mapping;
3668 pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) +
3669 vma->vm_pgoff;
3670 struct vm_area_struct *svma;
3671 unsigned long saddr;
3672 pte_t *spte = NULL;
3673 pte_t *pte;
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003674 spinlock_t *ptl;
Steve Capper3212b532013-04-23 12:35:02 +01003675
3676 if (!vma_shareable(vma, addr))
3677 return (pte_t *)pmd_alloc(mm, pud, addr);
3678
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08003679 i_mmap_lock_write(mapping);
Steve Capper3212b532013-04-23 12:35:02 +01003680 vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) {
3681 if (svma == vma)
3682 continue;
3683
3684 saddr = page_table_shareable(svma, vma, addr, idx);
3685 if (saddr) {
3686 spte = huge_pte_offset(svma->vm_mm, saddr);
3687 if (spte) {
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08003688 mm_inc_nr_pmds(mm);
Steve Capper3212b532013-04-23 12:35:02 +01003689 get_page(virt_to_page(spte));
3690 break;
3691 }
3692 }
3693 }
3694
3695 if (!spte)
3696 goto out;
3697
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003698 ptl = huge_pte_lockptr(hstate_vma(vma), mm, spte);
3699 spin_lock(ptl);
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08003700 if (pud_none(*pud)) {
Steve Capper3212b532013-04-23 12:35:02 +01003701 pud_populate(mm, pud,
3702 (pmd_t *)((unsigned long)spte & PAGE_MASK));
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08003703 } else {
Steve Capper3212b532013-04-23 12:35:02 +01003704 put_page(virt_to_page(spte));
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08003705 mm_inc_nr_pmds(mm);
3706 }
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003707 spin_unlock(ptl);
Steve Capper3212b532013-04-23 12:35:02 +01003708out:
3709 pte = (pte_t *)pmd_alloc(mm, pud, addr);
Davidlohr Bueso83cde9e2014-12-12 16:54:21 -08003710 i_mmap_unlock_write(mapping);
Steve Capper3212b532013-04-23 12:35:02 +01003711 return pte;
3712}
3713
3714/*
3715 * unmap huge page backed by shared pte.
3716 *
3717 * Hugetlb pte page is ref counted at the time of mapping. If pte is shared
3718 * indicated by page_count > 1, unmap is achieved by clearing pud and
3719 * decrementing the ref count. If count == 1, the pte page is not shared.
3720 *
Kirill A. Shutemovcb900f42013-11-14 14:31:02 -08003721 * called with page table lock held.
Steve Capper3212b532013-04-23 12:35:02 +01003722 *
3723 * returns: 1 successfully unmapped a shared pte page
3724 * 0 the underlying pte page is not shared, or it is the last user
3725 */
3726int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
3727{
3728 pgd_t *pgd = pgd_offset(mm, *addr);
3729 pud_t *pud = pud_offset(pgd, *addr);
3730
3731 BUG_ON(page_count(virt_to_page(ptep)) == 0);
3732 if (page_count(virt_to_page(ptep)) == 1)
3733 return 0;
3734
3735 pud_clear(pud);
3736 put_page(virt_to_page(ptep));
Kirill A. Shutemovdc6c9a32015-02-11 15:26:50 -08003737 mm_dec_nr_pmds(mm);
Steve Capper3212b532013-04-23 12:35:02 +01003738 *addr = ALIGN(*addr, HPAGE_SIZE * PTRS_PER_PTE) - HPAGE_SIZE;
3739 return 1;
3740}
Steve Capper9e5fc742013-04-30 08:02:03 +01003741#define want_pmd_share() (1)
3742#else /* !CONFIG_ARCH_WANT_HUGE_PMD_SHARE */
3743pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)
3744{
3745 return NULL;
3746}
3747#define want_pmd_share() (0)
Steve Capper3212b532013-04-23 12:35:02 +01003748#endif /* CONFIG_ARCH_WANT_HUGE_PMD_SHARE */
3749
Steve Capper9e5fc742013-04-30 08:02:03 +01003750#ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB
3751pte_t *huge_pte_alloc(struct mm_struct *mm,
3752 unsigned long addr, unsigned long sz)
3753{
3754 pgd_t *pgd;
3755 pud_t *pud;
3756 pte_t *pte = NULL;
3757
3758 pgd = pgd_offset(mm, addr);
3759 pud = pud_alloc(mm, pgd, addr);
3760 if (pud) {
3761 if (sz == PUD_SIZE) {
3762 pte = (pte_t *)pud;
3763 } else {
3764 BUG_ON(sz != PMD_SIZE);
3765 if (want_pmd_share() && pud_none(*pud))
3766 pte = huge_pmd_share(mm, addr, pud);
3767 else
3768 pte = (pte_t *)pmd_alloc(mm, pud, addr);
3769 }
3770 }
3771 BUG_ON(pte && !pte_none(*pte) && !pte_huge(*pte));
3772
3773 return pte;
3774}
3775
3776pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
3777{
3778 pgd_t *pgd;
3779 pud_t *pud;
3780 pmd_t *pmd = NULL;
3781
3782 pgd = pgd_offset(mm, addr);
3783 if (pgd_present(*pgd)) {
3784 pud = pud_offset(pgd, addr);
3785 if (pud_present(*pud)) {
3786 if (pud_huge(*pud))
3787 return (pte_t *)pud;
3788 pmd = pmd_offset(pud, addr);
3789 }
3790 }
3791 return (pte_t *) pmd;
3792}
3793
Naoya Horiguchi61f77ed2015-02-11 15:25:15 -08003794#endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
3795
3796/*
3797 * These functions are overwritable if your architecture needs its own
3798 * behavior.
3799 */
3800struct page * __weak
3801follow_huge_addr(struct mm_struct *mm, unsigned long address,
3802 int write)
3803{
3804 return ERR_PTR(-EINVAL);
3805}
3806
3807struct page * __weak
Steve Capper9e5fc742013-04-30 08:02:03 +01003808follow_huge_pmd(struct mm_struct *mm, unsigned long address,
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08003809 pmd_t *pmd, int flags)
Steve Capper9e5fc742013-04-30 08:02:03 +01003810{
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08003811 struct page *page = NULL;
3812 spinlock_t *ptl;
3813retry:
3814 ptl = pmd_lockptr(mm, pmd);
3815 spin_lock(ptl);
3816 /*
3817 * make sure that the address range covered by this pmd is not
3818 * unmapped from other threads.
3819 */
3820 if (!pmd_huge(*pmd))
3821 goto out;
3822 if (pmd_present(*pmd)) {
Gerald Schaefer97534122015-04-14 15:42:30 -07003823 page = pmd_page(*pmd) + ((address & ~PMD_MASK) >> PAGE_SHIFT);
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08003824 if (flags & FOLL_GET)
3825 get_page(page);
3826 } else {
3827 if (is_hugetlb_entry_migration(huge_ptep_get((pte_t *)pmd))) {
3828 spin_unlock(ptl);
3829 __migration_entry_wait(mm, (pte_t *)pmd, ptl);
3830 goto retry;
3831 }
3832 /*
3833 * hwpoisoned entry is treated as no_page_table in
3834 * follow_page_mask().
3835 */
3836 }
3837out:
3838 spin_unlock(ptl);
Steve Capper9e5fc742013-04-30 08:02:03 +01003839 return page;
3840}
3841
Naoya Horiguchi61f77ed2015-02-11 15:25:15 -08003842struct page * __weak
Steve Capper9e5fc742013-04-30 08:02:03 +01003843follow_huge_pud(struct mm_struct *mm, unsigned long address,
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08003844 pud_t *pud, int flags)
Steve Capper9e5fc742013-04-30 08:02:03 +01003845{
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08003846 if (flags & FOLL_GET)
3847 return NULL;
Steve Capper9e5fc742013-04-30 08:02:03 +01003848
Naoya Horiguchie66f17f2015-02-11 15:25:22 -08003849 return pte_page(*(pte_t *)pud) + ((address & ~PUD_MASK) >> PAGE_SHIFT);
Steve Capper9e5fc742013-04-30 08:02:03 +01003850}
3851
Andi Kleend5bd9102010-09-27 09:00:12 +02003852#ifdef CONFIG_MEMORY_FAILURE
3853
Naoya Horiguchi6de2b1a2010-09-08 10:19:36 +09003854/* Should be called in hugetlb_lock */
3855static int is_hugepage_on_freelist(struct page *hpage)
3856{
3857 struct page *page;
3858 struct page *tmp;
3859 struct hstate *h = page_hstate(hpage);
3860 int nid = page_to_nid(hpage);
3861
3862 list_for_each_entry_safe(page, tmp, &h->hugepage_freelists[nid], lru)
3863 if (page == hpage)
3864 return 1;
3865 return 0;
3866}
3867
Naoya Horiguchi93f70f92010-05-28 09:29:20 +09003868/*
3869 * This function is called from memory failure code.
3870 * Assume the caller holds page lock of the head page.
3871 */
Naoya Horiguchi6de2b1a2010-09-08 10:19:36 +09003872int dequeue_hwpoisoned_huge_page(struct page *hpage)
Naoya Horiguchi93f70f92010-05-28 09:29:20 +09003873{
3874 struct hstate *h = page_hstate(hpage);
3875 int nid = page_to_nid(hpage);
Naoya Horiguchi6de2b1a2010-09-08 10:19:36 +09003876 int ret = -EBUSY;
Naoya Horiguchi93f70f92010-05-28 09:29:20 +09003877
3878 spin_lock(&hugetlb_lock);
Naoya Horiguchi6de2b1a2010-09-08 10:19:36 +09003879 if (is_hugepage_on_freelist(hpage)) {
Naoya Horiguchi56f2fb12012-12-12 13:52:33 -08003880 /*
3881 * Hwpoisoned hugepage isn't linked to activelist or freelist,
3882 * but dangling hpage->lru can trigger list-debug warnings
3883 * (this happens when we call unpoison_memory() on it),
3884 * so let it point to itself with list_del_init().
3885 */
3886 list_del_init(&hpage->lru);
Naoya Horiguchi8c6c2ec2010-09-08 10:19:38 +09003887 set_page_refcounted(hpage);
Naoya Horiguchi6de2b1a2010-09-08 10:19:36 +09003888 h->free_huge_pages--;
3889 h->free_huge_pages_node[nid]--;
3890 ret = 0;
3891 }
Naoya Horiguchi93f70f92010-05-28 09:29:20 +09003892 spin_unlock(&hugetlb_lock);
Naoya Horiguchi6de2b1a2010-09-08 10:19:36 +09003893 return ret;
Naoya Horiguchi93f70f92010-05-28 09:29:20 +09003894}
Naoya Horiguchi6de2b1a2010-09-08 10:19:36 +09003895#endif
Naoya Horiguchi31caf662013-09-11 14:21:59 -07003896
3897bool isolate_huge_page(struct page *page, struct list_head *list)
3898{
Sasha Levin309381fea2014-01-23 15:52:54 -08003899 VM_BUG_ON_PAGE(!PageHead(page), page);
Naoya Horiguchi31caf662013-09-11 14:21:59 -07003900 if (!get_page_unless_zero(page))
3901 return false;
3902 spin_lock(&hugetlb_lock);
3903 list_move_tail(&page->lru, list);
3904 spin_unlock(&hugetlb_lock);
3905 return true;
3906}
3907
3908void putback_active_hugepage(struct page *page)
3909{
Sasha Levin309381fea2014-01-23 15:52:54 -08003910 VM_BUG_ON_PAGE(!PageHead(page), page);
Naoya Horiguchi31caf662013-09-11 14:21:59 -07003911 spin_lock(&hugetlb_lock);
3912 list_move_tail(&page->lru, &(page_hstate(page))->hugepage_activelist);
3913 spin_unlock(&hugetlb_lock);
3914 put_page(page);
3915}
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07003916
3917bool is_hugepage_active(struct page *page)
3918{
Sasha Levin309381fea2014-01-23 15:52:54 -08003919 VM_BUG_ON_PAGE(!PageHuge(page), page);
Naoya Horiguchic8721bb2013-09-11 14:22:09 -07003920 /*
3921 * This function can be called for a tail page because the caller,
3922 * scan_movable_pages, scans through a given pfn-range which typically
3923 * covers one memory block. In systems using gigantic hugepage (1GB
3924 * for x86_64,) a hugepage is larger than a memory block, and we don't
3925 * support migrating such large hugepages for now, so return false
3926 * when called for tail pages.
3927 */
3928 if (PageTail(page))
3929 return false;
3930 /*
3931 * Refcount of a hwpoisoned hugepages is 1, but they are not active,
3932 * so we should return false for them.
3933 */
3934 if (unlikely(PageHWPoison(page)))
3935 return false;
3936 return page_count(page) > 0;
3937}