[PATCH] hugetlb: Add nohugepages parameter to prevent hugepages creation

From: Guilherme G. Piccoli
Date: Fri Oct 11 2019 - 18:40:14 EST

Currently there are 2 ways for setting HugeTLB hugepages in kernel; either
users pass parameters on kernel command-line or they can write to sysfs
files (which is effectively the sysctl way).

Kdump kernels won't benefit from hugepages - in fact it's quite opposite,
it may be the case hugepages on kdump kernel can lead to OOM if kernel
gets unable to allocate demanded pages due to the fact the preallocated
hugepages are consuming a lot of memory.

This patch proposes a new kernel parameter to prevent the creation of
HugeTLB hugepages - we currently don't have a way to do that. We can
even have kdump scripts removing the kernel command-line options to
set hugepages, but it's not straightforward to prevent sysctl/sysfs
configuration, given it happens in later boot or anytime when the
system is running.

Signed-off-by: Guilherme G. Piccoli <gpiccoli@xxxxxxxxxxxxx>

About some decisions took in this patch:

* early_param() was used because I couldn't find a way to enforce
parameters' ordering when using __setup(), and we need nohugepages
processed before all other hugepages options.

* The return when sysctl handler is prevented to progress due to
nohugepages is -EINVAL, but could be changed; I've just followed
present code there, but I'm OK changing that if we have suggestions.

Thanks in advance for the review!


Documentation/admin-guide/kernel-parameters.txt | 4 ++++
mm/hugetlb.c | 16 ++++++++++++++++
2 files changed, 20 insertions(+)

diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index c7ac2f3ac99f..eebe0e7b30cf 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -2982,6 +2982,10 @@

nohugeiomap [KNL,x86,PPC] Disable kernel huge I/O mappings.

+ nohugepages [KNL] Disable HugeTLB hugepages completely, preventing
+ its setting either by kernel parameter or sysfs;
+ useful specially in kdump kernel.
nosmt [KNL,S390] Disable symmetric multithreading (SMT).
Equivalent to smt=1.

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index ef37c85423a5..a6c7a68152e5 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -43,6 +43,7 @@
int hugetlb_max_hstate __read_mostly;
unsigned int default_hstate_idx;
struct hstate hstates[HUGE_MAX_HSTATE];
+static int disable_hugepages;
* Minimum page order among possible hugepage sizes, set to a proper value
* at boot time.
@@ -2550,6 +2551,9 @@ static ssize_t __nr_hugepages_store_common(bool obey_mempolicy,
int err;
nodemask_t nodes_allowed, *n_mask;

+ if (disable_hugepages)
+ return -EINVAL;
if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
return -EINVAL;

@@ -2978,6 +2982,9 @@ static int __init hugetlb_nrpages_setup(char *s)
unsigned long *mhp;
static unsigned long *last_mhp;

+ if (disable_hugepages)
+ return 1;
if (!parsed_valid_hugepagesz) {
pr_warn("hugepages = %s preceded by "
"an unsupported hugepagesz, ignoring\n", s);
@@ -3022,6 +3029,15 @@ static int __init hugetlb_default_setup(char *s)
__setup("default_hugepagesz=", hugetlb_default_setup);

+static int __init nohugepages_setup(char *str)
+ disable_hugepages = 1;
+ pr_info("HugeTLB: hugepages disabled by kernel parameter\n");
+ return 0;
+early_param("nohugepages", nohugepages_setup);
static unsigned int cpuset_mems_nr(unsigned int *array)
int node;