Skip to content

Commit 282f421

Browse files
mjkravetztorvalds
authored andcommitted
hugetlbfs: clean up command line processing
With all hugetlb page processing done in a single file clean up code. - Make code match desired semantics - Update documentation with semantics - Make all warnings and errors messages start with 'HugeTLB:'. - Consistently name command line parsing routines. - Warn if !hugepages_supported() and command line parameters have been specified. - Add comments to code - Describe some of the subtle interactions - Describe semantics of command line arguments This patch also fixes issues with implicitly setting the number of gigantic huge pages to preallocate. Previously on X86 command line, hugepages=2 default_hugepagesz=1G would result in zero 1G pages being preallocated and, # grep HugePages_Total /proc/meminfo HugePages_Total: 0 # sysctl -a | grep nr_hugepages vm.nr_hugepages = 2 vm.nr_hugepages_mempolicy = 2 # cat /proc/sys/vm/nr_hugepages 2 After this patch 2 gigantic pages will be preallocated and all the proc, sysfs, sysctl and meminfo files will accurately reflect this. To address the issue with gigantic pages, a small change in behavior was made to command line processing. Previously the command line, hugepages=128 default_hugepagesz=2M hugepagesz=2M hugepages=256 would result in the allocation of 256 2M huge pages. The value 128 would be ignored without any warning. After this patch, 128 2M pages will be allocated and a warning message will be displayed indicating the value of 256 is ignored. This change in behavior is required because allocation of implicitly specified gigantic pages must be done when the default_hugepagesz= is encountered for gigantic pages. Previously the code waited until later in the boot process (hugetlb_init), to allocate pages of default size. However the bootmem allocator required for gigantic allocations is not available at this time. Signed-off-by: Mike Kravetz <mike.kravetz@oracle.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Tested-by: Sandipan Das <sandipan@linux.ibm.com> Acked-by: Gerald Schaefer <gerald.schaefer@de.ibm.com> [s390] Acked-by: Will Deacon <will@kernel.org> Cc: Albert Ou <aou@eecs.berkeley.edu> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Christian Borntraeger <borntraeger@de.ibm.com> Cc: Christophe Leroy <christophe.leroy@c-s.fr> Cc: Dave Hansen <dave.hansen@linux.intel.com> Cc: David S. Miller <davem@davemloft.net> Cc: Heiko Carstens <heiko.carstens@de.ibm.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: Jonathan Corbet <corbet@lwn.net> Cc: Longpeng <longpeng2@huawei.com> Cc: Mina Almasry <almasrymina@google.com> Cc: Nitesh Narayan Lal <nitesh@redhat.com> Cc: Palmer Dabbelt <palmer@dabbelt.com> Cc: Paul Mackerras <paulus@samba.org> Cc: Paul Walmsley <paul.walmsley@sifive.com> Cc: Peter Xu <peterx@redhat.com> Cc: Randy Dunlap <rdunlap@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Vasily Gorbik <gor@linux.ibm.com> Cc: Anders Roxell <anders.roxell@linaro.org> Cc: "Aneesh Kumar K.V" <aneesh.kumar@linux.ibm.com> Cc: Qian Cai <cai@lca.pw> Cc: Stephen Rothwell <sfr@canb.auug.org.au> Link: http://lkml.kernel.org/r/20200417185049.275845-5-mike.kravetz@oracle.com Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 3823783 commit 282f421

3 files changed

Lines changed: 190 additions & 44 deletions

File tree

‎Documentation/admin-guide/kernel-parameters.txt‎

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -834,12 +834,15 @@
834834
See also Documentation/networking/decnet.txt.
835835

836836
default_hugepagesz=
837-
[same as hugepagesz=] The size of the default
838-
HugeTLB page size. This is the size represented by
839-
the legacy /proc/ hugepages APIs, used for SHM, and
840-
default size when mounting hugetlbfs filesystems.
841-
Defaults to the default architecture's huge page size
842-
if not specified.
837+
[HW] The size of the default HugeTLB page. This is
838+
the size represented by the legacy /proc/ hugepages
839+
APIs. In addition, this is the default hugetlb size
840+
used for shmget(), mmap() and mounting hugetlbfs
841+
filesystems. If not specified, defaults to the
842+
architecture's default huge page size. Huge page
843+
sizes are architecture dependent. See also
844+
Documentation/admin-guide/mm/hugetlbpage.rst.
845+
Format: size[KMG]
843846

844847
deferred_probe_timeout=
845848
[KNL] Debugging option to set a timeout in seconds for
@@ -1479,13 +1482,24 @@
14791482
hugepages using the cma allocator. If enabled, the
14801483
boot-time allocation of gigantic hugepages is skipped.
14811484

1482-
hugepages= [HW,X86-32,IA-64] HugeTLB pages to allocate at boot.
1483-
hugepagesz= [HW,IA-64,PPC,X86-64] The size of the HugeTLB pages.
1484-
On x86-64 and powerpc, this option can be specified
1485-
multiple times interleaved with hugepages= to reserve
1486-
huge pages of different sizes. Valid pages sizes on
1487-
x86-64 are 2M (when the CPU supports "pse") and 1G
1488-
(when the CPU supports the "pdpe1gb" cpuinfo flag).
1485+
hugepages= [HW] Number of HugeTLB pages to allocate at boot.
1486+
If this follows hugepagesz (below), it specifies
1487+
the number of pages of hugepagesz to be allocated.
1488+
If this is the first HugeTLB parameter on the command
1489+
line, it specifies the number of pages to allocate for
1490+
the default huge page size. See also
1491+
Documentation/admin-guide/mm/hugetlbpage.rst.
1492+
Format: <integer>
1493+
1494+
hugepagesz=
1495+
[HW] The size of the HugeTLB pages. This is used in
1496+
conjunction with hugepages (above) to allocate huge
1497+
pages of a specific size at boot. The pair
1498+
hugepagesz=X hugepages=Y can be specified once for
1499+
each supported huge page size. Huge page sizes are
1500+
architecture dependent. See also
1501+
Documentation/admin-guide/mm/hugetlbpage.rst.
1502+
Format: size[KMG]
14891503

14901504
hung_task_panic=
14911505
[KNL] Should the hung task detector generate panics.

‎Documentation/admin-guide/mm/hugetlbpage.rst‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,41 @@ with a huge page size selection parameter "hugepagesz=<size>". <size> must
100100
be specified in bytes with optional scale suffix [kKmMgG]. The default huge
101101
page size may be selected with the "default_hugepagesz=<size>" boot parameter.
102102

103+
Hugetlb boot command line parameter semantics
104+
hugepagesz - Specify a huge page size. Used in conjunction with hugepages
105+
parameter to preallocate a number of huge pages of the specified
106+
size. Hence, hugepagesz and hugepages are typically specified in
107+
pairs such as:
108+
hugepagesz=2M hugepages=512
109+
hugepagesz can only be specified once on the command line for a
110+
specific huge page size. Valid huge page sizes are architecture
111+
dependent.
112+
hugepages - Specify the number of huge pages to preallocate. This typically
113+
follows a valid hugepagesz or default_hugepagesz parameter. However,
114+
if hugepages is the first or only hugetlb command line parameter it
115+
implicitly specifies the number of huge pages of default size to
116+
allocate. If the number of huge pages of default size is implicitly
117+
specified, it can not be overwritten by a hugepagesz,hugepages
118+
parameter pair for the default size.
119+
For example, on an architecture with 2M default huge page size:
120+
hugepages=256 hugepagesz=2M hugepages=512
121+
will result in 256 2M huge pages being allocated and a warning message
122+
indicating that the hugepages=512 parameter is ignored. If a hugepages
123+
parameter is preceded by an invalid hugepagesz parameter, it will
124+
be ignored.
125+
default_hugepagesz - Specify the default huge page size. This parameter can
126+
only be specified once on the command line. default_hugepagesz can
127+
optionally be followed by the hugepages parameter to preallocate a
128+
specific number of huge pages of default size. The number of default
129+
sized huge pages to preallocate can also be implicitly specified as
130+
mentioned in the hugepages section above. Therefore, on an
131+
architecture with 2M default huge page size:
132+
hugepages=256
133+
default_hugepagesz=2M hugepages=256
134+
hugepages=256 default_hugepagesz=2M
135+
will all result in 256 2M huge pages being allocated. Valid default
136+
huge page size is architecture dependent.
137+
103138
When multiple huge page sizes are supported, ``/proc/sys/vm/nr_hugepages``
104139
indicates the current number of pre-allocated huge pages of the default size.
105140
Thus, one can use the following command to dynamically allocate/deallocate

‎mm/hugetlb.c‎

Lines changed: 128 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ __initdata LIST_HEAD(huge_boot_pages);
5959
/* for command line parsing */
6060
static struct hstate * __initdata parsed_hstate;
6161
static unsigned long __initdata default_hstate_max_huge_pages;
62-
static unsigned long __initdata default_hstate_size;
6362
static bool __initdata parsed_valid_hugepagesz = true;
63+
static bool __initdata parsed_default_hugepagesz;
6464

6565
/*
6666
* Protects updates to hugepage_freelists, hugepage_activelist, nr_huge_pages,
@@ -3060,7 +3060,7 @@ static void __init hugetlb_sysfs_init(void)
30603060
err = hugetlb_sysfs_add_hstate(h, hugepages_kobj,
30613061
hstate_kobjs, &hstate_attr_group);
30623062
if (err)
3063-
pr_err("Hugetlb: Unable to add hstate %s", h->name);
3063+
pr_err("HugeTLB: Unable to add hstate %s", h->name);
30643064
}
30653065
}
30663066

@@ -3164,7 +3164,7 @@ static void hugetlb_register_node(struct node *node)
31643164
nhs->hstate_kobjs,
31653165
&per_node_hstate_attr_group);
31663166
if (err) {
3167-
pr_err("Hugetlb: Unable to add hstate %s for node %d\n",
3167+
pr_err("HugeTLB: Unable to add hstate %s for node %d\n",
31683168
h->name, node->dev.id);
31693169
hugetlb_unregister_node(node);
31703170
break;
@@ -3215,19 +3215,35 @@ static int __init hugetlb_init(void)
32153215
if (!hugepages_supported())
32163216
return 0;
32173217

3218-
if (!size_to_hstate(default_hstate_size)) {
3219-
if (default_hstate_size != 0) {
3220-
pr_err("HugeTLB: unsupported default_hugepagesz %lu. Reverting to %lu\n",
3221-
default_hstate_size, HPAGE_SIZE);
3218+
/*
3219+
* Make sure HPAGE_SIZE (HUGETLB_PAGE_ORDER) hstate exists. Some
3220+
* architectures depend on setup being done here.
3221+
*/
3222+
hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
3223+
if (!parsed_default_hugepagesz) {
3224+
/*
3225+
* If we did not parse a default huge page size, set
3226+
* default_hstate_idx to HPAGE_SIZE hstate. And, if the
3227+
* number of huge pages for this default size was implicitly
3228+
* specified, set that here as well.
3229+
* Note that the implicit setting will overwrite an explicit
3230+
* setting. A warning will be printed in this case.
3231+
*/
3232+
default_hstate_idx = hstate_index(size_to_hstate(HPAGE_SIZE));
3233+
if (default_hstate_max_huge_pages) {
3234+
if (default_hstate.max_huge_pages) {
3235+
char buf[32];
3236+
3237+
string_get_size(huge_page_size(&default_hstate),
3238+
1, STRING_UNITS_2, buf, 32);
3239+
pr_warn("HugeTLB: Ignoring hugepages=%lu associated with %s page size\n",
3240+
default_hstate.max_huge_pages, buf);
3241+
pr_warn("HugeTLB: Using hugepages=%lu for number of default huge pages\n",
3242+
default_hstate_max_huge_pages);
3243+
}
3244+
default_hstate.max_huge_pages =
3245+
default_hstate_max_huge_pages;
32223246
}
3223-
3224-
default_hstate_size = HPAGE_SIZE;
3225-
hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
3226-
}
3227-
default_hstate_idx = hstate_index(size_to_hstate(default_hstate_size));
3228-
if (default_hstate_max_huge_pages) {
3229-
if (!default_hstate.max_huge_pages)
3230-
default_hstate.max_huge_pages = default_hstate_max_huge_pages;
32313247
}
32323248

32333249
hugetlb_cma_check();
@@ -3287,29 +3303,43 @@ void __init hugetlb_add_hstate(unsigned int order)
32873303
parsed_hstate = h;
32883304
}
32893305

3290-
static int __init hugetlb_nrpages_setup(char *s)
3306+
/*
3307+
* hugepages command line processing
3308+
* hugepages normally follows a valid hugepagsz or default_hugepagsz
3309+
* specification. If not, ignore the hugepages value. hugepages can also
3310+
* be the first huge page command line option in which case it implicitly
3311+
* specifies the number of huge pages for the default size.
3312+
*/
3313+
static int __init hugepages_setup(char *s)
32913314
{
32923315
unsigned long *mhp;
32933316
static unsigned long *last_mhp;
32943317

3318+
if (!hugepages_supported()) {
3319+
pr_warn("HugeTLB: huge pages not supported, ignoring hugepages = %s\n", s);
3320+
return 0;
3321+
}
3322+
32953323
if (!parsed_valid_hugepagesz) {
3296-
pr_warn("hugepages = %s preceded by "
3297-
"an unsupported hugepagesz, ignoring\n", s);
3324+
pr_warn("HugeTLB: hugepages=%s does not follow a valid hugepagesz, ignoring\n", s);
32983325
parsed_valid_hugepagesz = true;
3299-
return 1;
3326+
return 0;
33003327
}
3328+
33013329
/*
3302-
* !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter yet,
3303-
* so this hugepages= parameter goes to the "default hstate".
3330+
* !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter
3331+
* yet, so this hugepages= parameter goes to the "default hstate".
3332+
* Otherwise, it goes with the previously parsed hugepagesz or
3333+
* default_hugepagesz.
33043334
*/
33053335
else if (!hugetlb_max_hstate)
33063336
mhp = &default_hstate_max_huge_pages;
33073337
else
33083338
mhp = &parsed_hstate->max_huge_pages;
33093339

33103340
if (mhp == last_mhp) {
3311-
pr_warn("hugepages= specified twice without interleaving hugepagesz=, ignoring\n");
3312-
return 1;
3341+
pr_warn("HugeTLB: hugepages= specified twice without interleaving hugepagesz=, ignoring hugepages=%s\n", s);
3342+
return 0;
33133343
}
33143344

33153345
if (sscanf(s, "%lu", mhp) <= 0)
@@ -3327,42 +3357,109 @@ static int __init hugetlb_nrpages_setup(char *s)
33273357

33283358
return 1;
33293359
}
3330-
__setup("hugepages=", hugetlb_nrpages_setup);
3360+
__setup("hugepages=", hugepages_setup);
33313361

3362+
/*
3363+
* hugepagesz command line processing
3364+
* A specific huge page size can only be specified once with hugepagesz.
3365+
* hugepagesz is followed by hugepages on the command line. The global
3366+
* variable 'parsed_valid_hugepagesz' is used to determine if prior
3367+
* hugepagesz argument was valid.
3368+
*/
33323369
static int __init hugepagesz_setup(char *s)
33333370
{
33343371
unsigned long size;
3372+
struct hstate *h;
3373+
3374+
parsed_valid_hugepagesz = false;
3375+
if (!hugepages_supported()) {
3376+
pr_warn("HugeTLB: huge pages not supported, ignoring hugepagesz = %s\n", s);
3377+
return 0;
3378+
}
33353379

33363380
size = (unsigned long)memparse(s, NULL);
33373381

33383382
if (!arch_hugetlb_valid_size(size)) {
3339-
parsed_valid_hugepagesz = false;
3340-
pr_err("HugeTLB: unsupported hugepagesz %s\n", s);
3383+
pr_err("HugeTLB: unsupported hugepagesz=%s\n", s);
33413384
return 0;
33423385
}
33433386

3344-
if (size_to_hstate(size)) {
3345-
pr_warn("HugeTLB: hugepagesz %s specified twice, ignoring\n", s);
3346-
return 0;
3387+
h = size_to_hstate(size);
3388+
if (h) {
3389+
/*
3390+
* hstate for this size already exists. This is normally
3391+
* an error, but is allowed if the existing hstate is the
3392+
* default hstate. More specifically, it is only allowed if
3393+
* the number of huge pages for the default hstate was not
3394+
* previously specified.
3395+
*/
3396+
if (!parsed_default_hugepagesz || h != &default_hstate ||
3397+
default_hstate.max_huge_pages) {
3398+
pr_warn("HugeTLB: hugepagesz=%s specified twice, ignoring\n", s);
3399+
return 0;
3400+
}
3401+
3402+
/*
3403+
* No need to call hugetlb_add_hstate() as hstate already
3404+
* exists. But, do set parsed_hstate so that a following
3405+
* hugepages= parameter will be applied to this hstate.
3406+
*/
3407+
parsed_hstate = h;
3408+
parsed_valid_hugepagesz = true;
3409+
return 1;
33473410
}
33483411

33493412
hugetlb_add_hstate(ilog2(size) - PAGE_SHIFT);
3413+
parsed_valid_hugepagesz = true;
33503414
return 1;
33513415
}
33523416
__setup("hugepagesz=", hugepagesz_setup);
33533417

3418+
/*
3419+
* default_hugepagesz command line input
3420+
* Only one instance of default_hugepagesz allowed on command line.
3421+
*/
33543422
static int __init default_hugepagesz_setup(char *s)
33553423
{
33563424
unsigned long size;
33573425

3426+
parsed_valid_hugepagesz = false;
3427+
if (!hugepages_supported()) {
3428+
pr_warn("HugeTLB: huge pages not supported, ignoring default_hugepagesz = %s\n", s);
3429+
return 0;
3430+
}
3431+
3432+
if (parsed_default_hugepagesz) {
3433+
pr_err("HugeTLB: default_hugepagesz previously specified, ignoring %s\n", s);
3434+
return 0;
3435+
}
3436+
33583437
size = (unsigned long)memparse(s, NULL);
33593438

33603439
if (!arch_hugetlb_valid_size(size)) {
3361-
pr_err("HugeTLB: unsupported default_hugepagesz %s\n", s);
3440+
pr_err("HugeTLB: unsupported default_hugepagesz=%s\n", s);
33623441
return 0;
33633442
}
33643443

3365-
default_hstate_size = size;
3444+
hugetlb_add_hstate(ilog2(size) - PAGE_SHIFT);
3445+
parsed_valid_hugepagesz = true;
3446+
parsed_default_hugepagesz = true;
3447+
default_hstate_idx = hstate_index(size_to_hstate(size));
3448+
3449+
/*
3450+
* The number of default huge pages (for this size) could have been
3451+
* specified as the first hugetlb parameter: hugepages=X. If so,
3452+
* then default_hstate_max_huge_pages is set. If the default huge
3453+
* page size is gigantic (>= MAX_ORDER), then the pages must be
3454+
* allocated here from bootmem allocator.
3455+
*/
3456+
if (default_hstate_max_huge_pages) {
3457+
default_hstate.max_huge_pages = default_hstate_max_huge_pages;
3458+
if (hstate_is_gigantic(&default_hstate))
3459+
hugetlb_hstate_alloc_pages(&default_hstate);
3460+
default_hstate_max_huge_pages = 0;
3461+
}
3462+
33663463
return 1;
33673464
}
33683465
__setup("default_hugepagesz=", default_hugepagesz_setup);

0 commit comments

Comments
 (0)