Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mm/hugetlb: allow overcommitting gigantic hugepages

Currently, gigantic hugepages cannot use the overcommit mechanism
(nr_overcommit_hugepages), forcing users to permanently reserve memory via
nr_hugepages even when pages might not be actively used.

The restriction was added in 2011 [1], which was before there was support
for reserving 1G hugepages at runtime. Remove this blanket restriction on
gigantic hugepage overcommit. This will bring the same benefits to
gigantic pages as hugepages:

- Memory is only taken out of regular use when actually needed
- Unused surplus pages can be returned to the system
- Better memory utilization, especially with CMA backing which can
significantly increase the changes of hugepage allocation

Without this patch:
echo 3 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_overcommit_hugepages
bash: echo: write error: Invalid argument

With this patch:
echo 3 > /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_overcommit_hugepages
./mmap_hugetlb_test
Successfully allocated huge pages at address: 0x7f9d40000000

cat mmap_hugetlb_test.c
...
unsigned long ALLOC_SIZE = 3 * (unsigned long) HUGE_PAGE_SIZE;
addr = mmap(NULL,
ALLOC_SIZE, // 3GB
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_HUGE_1GB,
-1,
0);

if (addr == MAP_FAILED) {
fprintf(stderr, "mmap failed: %s\n", strerror(errno));
return 1;
}
printf("Successfully allocated huge pages at address: %p\n", addr);
...

Link: https://lkml.kernel.org/r/20251009172433.4158118-2-usamaarif642@gmail.com
Link: https://git.zx2c4.com/linux-rng/commit/mm/hugetlb.c?id=adbe8726dc2a3805630d517270db17e3af86e526 [1]
Signed-off-by: Usama Arif <usamaarif642@gmail.com>
Reviewed-by: Shakeel Butt <shakeel.butt@linux.dev>
Reviewed-by: Kefeng Wang <wangkefeng.wang@huawei.com>
Acked-by: Oscar Salvador <osalvador@suse.de>
Cc: David Hildenbrand <david@redhat.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: Rik van Riel <riel@surriel.com>
Cc: SeongJae Park <sj@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Usama Arif and committed by
Andrew Morton
eb02f14c a743e0af

+3 -3
+3 -3
mm/hugetlb.c
··· 2223 2223 { 2224 2224 struct folio *folio = NULL; 2225 2225 2226 - if (hstate_is_gigantic(h)) 2226 + if (hstate_is_gigantic_no_runtime(h)) 2227 2227 return NULL; 2228 2228 2229 2229 spin_lock_irq(&hugetlb_lock); ··· 4285 4285 unsigned long input; 4286 4286 struct hstate *h = kobj_to_hstate(kobj, NULL); 4287 4287 4288 - if (hstate_is_gigantic(h)) 4288 + if (hstate_is_gigantic_no_runtime(h)) 4289 4289 return -EINVAL; 4290 4290 4291 4291 err = kstrtoul(buf, 10, &input); ··· 5172 5172 5173 5173 tmp = h->nr_overcommit_huge_pages; 5174 5174 5175 - if (write && hstate_is_gigantic(h)) 5175 + if (write && hstate_is_gigantic_no_runtime(h)) 5176 5176 return -EINVAL; 5177 5177 5178 5178 ret = proc_hugetlb_doulongvec_minmax(table, write, buffer, length, ppos,