Skip to content

Commit 457c1b2

Browse files
Nishanth Aravamudantorvalds
authored andcommitted
hugetlb: ensure hugepage access is denied if hugepages are not supported
Currently, I am seeing the following when I `mount -t hugetlbfs /none /dev/hugetlbfs`, and then simply do a `ls /dev/hugetlbfs`. I think it's related to the fact that hugetlbfs is properly not correctly setting itself up in this state?: Unable to handle kernel paging request for data at address 0x00000031 Faulting instruction address: 0xc000000000245710 Oops: Kernel access of bad area, sig: 11 [#1] SMP NR_CPUS=2048 NUMA pSeries .... In KVM guests on Power, in a guest not backed by hugepages, we see the following: AnonHugePages: 0 kB HugePages_Total: 0 HugePages_Free: 0 HugePages_Rsvd: 0 HugePages_Surp: 0 Hugepagesize: 64 kB HPAGE_SHIFT == 0 in this configuration, which indicates that hugepages are not supported at boot-time, but this is only checked in hugetlb_init(). Extract the check to a helper function, and use it in a few relevant places. This does make hugetlbfs not supported (not registered at all) in this environment. I believe this is fine, as there are no valid hugepages and that won't change at runtime. [[email protected]: use pr_info(), per Mel] [[email protected]: fix build when HPAGE_SHIFT is undefined] Signed-off-by: Nishanth Aravamudan <[email protected]> Reviewed-by: Aneesh Kumar K.V <[email protected]> Acked-by: Mel Gorman <[email protected]> Cc: Randy Dunlap <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 93030d8 commit 457c1b2

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

fs/hugetlbfs/inode.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,11 @@ static int __init init_hugetlbfs_fs(void)
10301030
int error;
10311031
int i;
10321032

1033+
if (!hugepages_supported()) {
1034+
pr_info("hugetlbfs: disabling because there are no supported hugepage sizes\n");
1035+
return -ENOTSUPP;
1036+
}
1037+
10331038
error = bdi_init(&hugetlbfs_backing_dev_info);
10341039
if (error)
10351040
return error;

include/linux/hugetlb.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,16 @@ static inline spinlock_t *huge_pte_lockptr(struct hstate *h,
412412
return &mm->page_table_lock;
413413
}
414414

415+
static inline bool hugepages_supported(void)
416+
{
417+
/*
418+
* Some platform decide whether they support huge pages at boot
419+
* time. On these, such as powerpc, HPAGE_SHIFT is set to 0 when
420+
* there is no such support
421+
*/
422+
return HPAGE_SHIFT != 0;
423+
}
424+
415425
#else /* CONFIG_HUGETLB_PAGE */
416426
struct hstate {};
417427
#define alloc_huge_page_node(h, nid) NULL

mm/hugetlb.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,11 +1981,7 @@ static int __init hugetlb_init(void)
19811981
{
19821982
int i;
19831983

1984-
/* Some platform decide whether they support huge pages at boot
1985-
* time. On these, such as powerpc, HPAGE_SHIFT is set to 0 when
1986-
* there is no such support
1987-
*/
1988-
if (HPAGE_SHIFT == 0)
1984+
if (!hugepages_supported())
19891985
return 0;
19901986

19911987
if (!size_to_hstate(default_hstate_size)) {
@@ -2112,6 +2108,9 @@ static int hugetlb_sysctl_handler_common(bool obey_mempolicy,
21122108
unsigned long tmp;
21132109
int ret;
21142110

2111+
if (!hugepages_supported())
2112+
return -ENOTSUPP;
2113+
21152114
tmp = h->max_huge_pages;
21162115

21172116
if (write && h->order >= MAX_ORDER)
@@ -2165,6 +2164,9 @@ int hugetlb_overcommit_handler(struct ctl_table *table, int write,
21652164
unsigned long tmp;
21662165
int ret;
21672166

2167+
if (!hugepages_supported())
2168+
return -ENOTSUPP;
2169+
21682170
tmp = h->nr_overcommit_huge_pages;
21692171

21702172
if (write && h->order >= MAX_ORDER)
@@ -2190,6 +2192,8 @@ int hugetlb_overcommit_handler(struct ctl_table *table, int write,
21902192
void hugetlb_report_meminfo(struct seq_file *m)
21912193
{
21922194
struct hstate *h = &default_hstate;
2195+
if (!hugepages_supported())
2196+
return;
21932197
seq_printf(m,
21942198
"HugePages_Total: %5lu\n"
21952199
"HugePages_Free: %5lu\n"
@@ -2206,6 +2210,8 @@ void hugetlb_report_meminfo(struct seq_file *m)
22062210
int hugetlb_report_node_meminfo(int nid, char *buf)
22072211
{
22082212
struct hstate *h = &default_hstate;
2213+
if (!hugepages_supported())
2214+
return 0;
22092215
return sprintf(buf,
22102216
"Node %d HugePages_Total: %5u\n"
22112217
"Node %d HugePages_Free: %5u\n"
@@ -2220,6 +2226,9 @@ void hugetlb_show_meminfo(void)
22202226
struct hstate *h;
22212227
int nid;
22222228

2229+
if (!hugepages_supported())
2230+
return;
2231+
22232232
for_each_node_state(nid, N_MEMORY)
22242233
for_each_hstate(h)
22252234
pr_info("Node %d hugepages_total=%u hugepages_free=%u hugepages_surp=%u hugepages_size=%lukB\n",

0 commit comments

Comments
 (0)