Skip to contentSkip to conversation

powerpc/mm: Handle page table allocation failures

PATCH In review Aneesh Kumar K.V 2 participants 4 messages May 28, 2019 – Jul 8, 2019 PowerPC

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 powerpc/mm: Handle page table allocation failures

This fixes kernel crash that arises due to not handling page table allocation failures while allocating hugetlb page table.

Fixes e2b3d202d1db ("powerpc: Switch 16GB and 16MB explicit hugepages to a different page table format")
---
 arch/powerpc/mm/hugetlbpage.c | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/powerpc/mm/hugetlbpage.c +8 -0
index b5d92dc32844..1de0f43a68e5 100644
@@ -130,6 +130,8 @@ pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr, unsigned long sz
 	} else {
 		pdshift = PUD_SHIFT;
 		pu = pud_alloc(mm, pg, addr);
+		if (!pu)
+			return NULL;
 		if (pshift == PUD_SHIFT)
 			return (pte_t *)pu;
 		else if (pshift > PMD_SHIFT) {
@@ -138,6 +140,8 @@ pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr, unsigned long sz
 		} else {
 			pdshift = PMD_SHIFT;
 			pm = pmd_alloc(mm, pu, addr);
+			if (!pm)
+				return NULL;
 			if (pshift == PMD_SHIFT)
 				/* 16MB hugepage */
 				return (pte_t *)pm;
@@ -154,12 +158,16 @@ pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr, unsigned long sz
 	} else {
 		pdshift = PUD_SHIFT;
 		pu = pud_alloc(mm, pg, addr);
+		if (!pu)
+			return NULL;
 		if (pshift >= PUD_SHIFT) {
 			ptl = pud_lockptr(mm, pu);
 			hpdp = (hugepd_t *)pu;
 		} else {
 			pdshift = PMD_SHIFT;
 			pm = pmd_alloc(mm, pu, addr);
+			if (!pm)
+				return NULL;
 			ptl = pmd_lockptr(mm, pm);
 			hpdp = (hugepd_t *)pm;
 		}
Signature · 1 line
2.21.0
#1 lore raw

PATCH v2 2/3 powerpc/mm/hugetlb: Fix kernel crash if we fail to allocate page table caches

We only check for hugetlb allocations, because with hugetlb we do conditional registration. For PGD/PUD/PMD levels we register them always in pgtable_cache_init.

---
 arch/powerpc/mm/hugetlbpage.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/powerpc/mm/hugetlbpage.c +6 -1
index 1de0f43a68e5..f55dc110f2ad 100644
@@ -61,12 +61,17 @@ static int __hugepte_alloc(struct mm_struct *mm, hugepd_t *hpdp,
 		num_hugepd = 1;
 	}
 
+	if (!cachep) {
+		WARN_ONCE(1, "No page table cache created for hugetlb tables");
+		return -ENOMEM;
+	}
+
 	new = kmem_cache_alloc(cachep, pgtable_gfp_flags(mm, GFP_KERNEL));
 
 	BUG_ON(pshift > HUGEPD_SHIFT_MASK);
 	BUG_ON((unsigned long)new & HUGEPD_SHIFT_MASK);
 
-	if (! new)
+	if (!new)
 		return -ENOMEM;
 
 	/*
Signature · 1 line
2.21.0
#2 lore raw

PATCH v2 3/3 powerpc/mm/hugetlb: Don't enable HugeTLB if we don't have a page table cache

In reply to Aneesh Kumar K.V

This makes sure we don't enable HugeTLB if the cache is not configured. I am still not sure about this. IMHO hugetlb support should be a hardware support derivative and any cache allocation failure should be handled as I did in the earlier patch. But then if we were not able to create hugetlb page table cache, we can as well declare hugetlb support disabled thereby avoiding calling into allocation routines.

---
 arch/powerpc/mm/hugetlbpage.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
arch/powerpc/mm/hugetlbpage.c +8 -2
index f55dc110f2ad..d34540479b1a 100644
@@ -601,6 +601,7 @@ __setup("hugepagesz=", hugepage_setup_sz);
 
 static int __init hugetlbpage_init(void)
 {
+	bool configured = false;
 	int psize;
 
 	if (hugetlb_disabled) {
@@ -651,10 +652,15 @@ static int __init hugetlbpage_init(void)
 			pgtable_cache_add(pdshift - shift);
 		else if (IS_ENABLED(CONFIG_PPC_FSL_BOOK3E) || IS_ENABLED(CONFIG_PPC_8xx))
 			pgtable_cache_add(PTE_T_ORDER);
+
+		configured = true;
 	}
 
-	if (IS_ENABLED(CONFIG_HUGETLB_PAGE_SIZE_VARIABLE))
-		hugetlbpage_init_default();
+	if (configured) {
+		if (IS_ENABLED(CONFIG_HUGETLB_PAGE_SIZE_VARIABLE))
+			hugetlbpage_init_default();
+	} else
+		pr_info("Failed to initialize. Disabling HugeTLB");
 
 	return 0;
 }
Signature · 1 line
2.21.0
#3 lore raw
In reply to Aneesh Kumar K.V
Quoted 5 lines · Aneesh Kumar K.V

This fixes kernel crash that arises due to not handling page table allocation failures while allocating hugetlb page table.

Fixes e2b3d202d1db ("powerpc: Switch 16GB and 16MB explicit hugepages to a different page table format")

Series applied to powerpc next, thanks.

https://git.kernel.org/powerpc/c/2230ebf6e6dd0b7751e2921b40f6cfe34f09bb16

cheers