Skip to contentSkip to conversation

arm64: Add support for handling memory corruption

PATCH Superseded Punit Agrawal 4 participants 9 messages Apr 12, 2017 – May 17, 2017 arm64

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover arm64: Add support for handling memory corruption

Hi,

With support for contiguous hugepages being turned off[0], some of the problems arising from swap entries go away[1]. This simplifies the changes needed to enable memory corruption handling for arm64 (done in this seris).

In this series, we updates huge_pte_offset() to correctly deal with swap entries (Patch 1). This function will need to be updated when contiguous hugepages are re-enabled.

Patch 2 adds support to send SIGBUS to processes that have their memory corrupted. With the prerequisites in place, enable memory corruption handling for arm64 (patch 3).

Thanks,
Punit

[0] https://lkml.org/lkml/2017/4/7/486
[1] https://lkml.org/lkml/2017/4/5/402

Jonathan (Zhixiong) Zhang (2):
arm64: hwpoison: add VM_FAULT_HWPOISON[_LARGE] handling
arm64: kconfig: allow support for memory failure handling

Punit Agrawal (1):
arm64: hugetlb: Fix huge_pte_offset to return poisoned page table
entries

 arch/arm64/Kconfig          |  1 +
 arch/arm64/mm/fault.c       | 22 +++++++++++++++++++---
 arch/arm64/mm/hugetlbpage.c | 20 +++++++++++++++-----
 3 files changed, 35 insertions(+), 8 deletions(-)
Signature · 1 line
2.11.0
#1 lore raw

PATCH 1/3 arm64: hugetlb: Fix huge_pte_offset to return poisoned page table entries

When memory failure is enabled, a poisoned hugepage pte is marked as a swap entry. huge_pte_offset() does not return the poisoned page table entries when it encounters PUD/PMD hugepages.

This behaviour of huge_pte_offset() leads to error such as below when munmap is called on poisoned hugepages.

[  344.165544] mm/pgtable-generic.c:33: bad pmd 000000083af00074.

Fix huge_pte_offset() to return the poisoned pte which is then appropriately handled by the generic layer code.

Cc Catalin Marinas <[email protected]>
Cc Steve Capper <[email protected]>
Cc David Woods <[email protected]>
---
 arch/arm64/mm/hugetlbpage.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
arch/arm64/mm/hugetlbpage.c +15 -5
index 7514a000e361..5f1832165d69 100644
@@ -143,15 +143,24 @@ pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
 	pr_debug("%s: addr:0x%lx pgd:%p\n", __func__, addr, pgd);
 	if (!pgd_present(*pgd))
 		return NULL;
-	pud = pud_offset(pgd, addr);
-	if (!pud_present(*pud))
-		return NULL;
 
-	if (pud_huge(*pud))
+	pud = pud_offset(pgd, addr);
+	/*
+	 * In case of HW Poisoning, a hugepage pud/pmd can contain
+	 * poisoned entries. Poisoned entries are marked as swap
+	 * entries.
+	 *
+	 * For puds/pmds that are not present, check to see if it
+	 * could be a swap entry (!present and !none).
+	 */
+	if ((!pte_present(pud_pte(*pud)) && !pud_none(*pud)) || pud_huge(*pud))
 		return (pte_t *)pud;
+
 	pmd = pmd_offset(pud, addr);
-	if (!pmd_present(*pmd))
+	if (pmd_none(*pmd))
 		return NULL;
+	if (!pmd_present(*pmd) && !pmd_none(*pmd))
+		return (pte_t *)pmd;
 
 	if (pte_cont(pmd_pte(*pmd))) {
 		pmd = pmd_offset(
@@ -160,6 +169,7 @@ pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
 	}
 	if (pmd_huge(*pmd))
 		return (pte_t *)pmd;
+
 	pte = pte_offset_kernel(pmd, addr);
 	if (pte_present(*pte) && pte_cont(*pte)) {
 		pte = pte_offset_kernel(
Signature · 1 line
2.11.0
Tested-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] arm64: hugetlb: Fix huge_pte_offset to return poisoned page table entries

Quoted 9 lines · Punit Agrawal

When memory failure is enabled, a poisoned hugepage pte is marked as a swap entry. huge_pte_offset() does not return the poisoned page table entries when it encounters PUD/PMD hugepages.

This behaviour of huge_pte_offset() leads to error such as below when munmap is called on poisoned hugepages.

[  344.165544] mm/pgtable-generic.c:33: bad pmd 000000083af00074.
Quoted 7 lines

Fix huge_pte_offset() to return the poisoned pte which is then appropriately handled by the generic layer code.

Cc Catalin Marinas <[email protected]>
Cc Steve Capper <[email protected]>
Cc David Woods <[email protected]>

Thanks,
Tyler

Quoted 46 lines
---
  arch/arm64/mm/hugetlbpage.c | 20 +++++++++++++++-----
  1 file changed, 15 insertions(+), 5 deletions(-)
arch/arm64/mm/hugetlbpage.c +15 -5
index 7514a000e361..5f1832165d69 100644
@@ -143,15 +143,24 @@ pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
  	pr_debug("%s: addr:0x%lx pgd:%p\n", __func__, addr, pgd);
  	if (!pgd_present(*pgd))
  		return NULL;
-	pud = pud_offset(pgd, addr);
-	if (!pud_present(*pud))
-		return NULL;
 
-	if (pud_huge(*pud))
+	pud = pud_offset(pgd, addr);
+	/*
+	 * In case of HW Poisoning, a hugepage pud/pmd can contain
+	 * poisoned entries. Poisoned entries are marked as swap
+	 * entries.
+	 *
+	 * For puds/pmds that are not present, check to see if it
+	 * could be a swap entry (!present and !none).
+	 */
+	if ((!pte_present(pud_pte(*pud)) && !pud_none(*pud)) || pud_huge(*pud))
  		return (pte_t *)pud;
+
  	pmd = pmd_offset(pud, addr);
-	if (!pmd_present(*pmd))
+	if (pmd_none(*pmd))
  		return NULL;
+	if (!pmd_present(*pmd) && !pmd_none(*pmd))
+		return (pte_t *)pmd;
 
  	if (pte_cont(pmd_pte(*pmd))) {
  		pmd = pmd_offset(
@@ -160,6 +169,7 @@ pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
  	}
  	if (pmd_huge(*pmd))
  		return (pte_t *)pmd;
+
  	pte = pte_offset_kernel(pmd, addr);
  	if (pte_present(*pte) && pte_cont(*pte)) {
  		pte = pte_offset_kernel(
Signature · 3 lines
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm Technologies, Inc.
Qualcomm Technologies, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project.
maintainer
#3 lore raw

Re: [PATCH 1/3] arm64: hugetlb: Fix huge_pte_offset to return poisoned page table entries

In reply to Punit Agrawal
Quoted 34 lines · Punit Agrawal

When memory failure is enabled, a poisoned hugepage pte is marked as a swap entry. huge_pte_offset() does not return the poisoned page table entries when it encounters PUD/PMD hugepages.

This behaviour of huge_pte_offset() leads to error such as below when munmap is called on poisoned hugepages.

[  344.165544] mm/pgtable-generic.c:33: bad pmd 000000083af00074.

Fix huge_pte_offset() to return the poisoned pte which is then appropriately handled by the generic layer code.

Cc Catalin Marinas <[email protected]>
Cc Steve Capper <[email protected]>
Cc David Woods <[email protected]>
---
 arch/arm64/mm/hugetlbpage.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
arch/arm64/mm/hugetlbpage.c +1 -4
index 7514a000e361..5f1832165d69 100644
@@ -143,15 +143,24 @@ pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
 	pr_debug("%s: addr:0x%lx pgd:%p\n", __func__, addr, pgd);
 	if (!pgd_present(*pgd))
 		return NULL;
-	pud = pud_offset(pgd, addr);
-	if (!pud_present(*pud))
-		return NULL;
 
-	if (pud_huge(*pud))
+	pud = pud_offset(pgd, addr);

arch/arm64/mm/hugetlbpage.c pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr) · line 157

Quoted 10 lines
arch/arm64/mm/hugetlbpage.c +9 -0
+	/*
+	 * In case of HW Poisoning, a hugepage pud/pmd can contain
+	 * poisoned entries. Poisoned entries are marked as swap
+	 * entries.
+	 *
+	 * For puds/pmds that are not present, check to see if it
+	 * could be a swap entry (!present and !none).
+	 */
+	if ((!pte_present(pud_pte(*pud)) && !pud_none(*pud)) || pud_huge(*pud))
 		return (pte_t *)pud;

Since we use puds as huge pages, can we just change pud_present() to match the pmd_present()? I'd like to see similar checks for pud and pmd, it would be easier to follow. Something like (unchecked):

	if (pud_none(*pud))
		return NULL;
	/* swap or huge page */
	if (!pud_present(*pud) || pud_huge(*pud))
		return (pte_t *)pud;
	/* table; check the next level */

arch/arm64/mm/hugetlbpage.c pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr) · line 163

Quoted 7 lines
arch/arm64/mm/hugetlbpage.c +4 -1
+
 	pmd = pmd_offset(pud, addr);
-	if (!pmd_present(*pmd))
+	if (pmd_none(*pmd))
 		return NULL;
+	if (!pmd_present(*pmd) && !pmd_none(*pmd))
+		return (pte_t *)pmd;

At this point, we already know that pmd_none(*pmd) is false, no ned to check it again.

Signature · 1 line
Catalin
#4 lore raw

Re: [PATCH 1/3] arm64: hugetlb: Fix huge_pte_offset to return poisoned page table entries

Quoted 57 lines · Catalin Marinas
Quoted 34 lines · Punit Agrawal

When memory failure is enabled, a poisoned hugepage pte is marked as a swap entry. huge_pte_offset() does not return the poisoned page table entries when it encounters PUD/PMD hugepages.

This behaviour of huge_pte_offset() leads to error such as below when munmap is called on poisoned hugepages.

[  344.165544] mm/pgtable-generic.c:33: bad pmd 000000083af00074.

Fix huge_pte_offset() to return the poisoned pte which is then appropriately handled by the generic layer code.

Cc Catalin Marinas <[email protected]>
Cc Steve Capper <[email protected]>
Cc David Woods <[email protected]>
---
 arch/arm64/mm/hugetlbpage.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
arch/arm64/mm/hugetlbpage.c +1 -4
index 7514a000e361..5f1832165d69 100644
@@ -143,15 +143,24 @@ pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
 	pr_debug("%s: addr:0x%lx pgd:%p\n", __func__, addr, pgd);
 	if (!pgd_present(*pgd))
 		return NULL;
-	pud = pud_offset(pgd, addr);
-	if (!pud_present(*pud))
-		return NULL;
 
-	if (pud_huge(*pud))
+	pud = pud_offset(pgd, addr);

arch/arm64/mm/hugetlbpage.c pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr) · line 157

Quoted 10 lines
arch/arm64/mm/hugetlbpage.c +9 -0
+	/*
+	 * In case of HW Poisoning, a hugepage pud/pmd can contain
+	 * poisoned entries. Poisoned entries are marked as swap
+	 * entries.
+	 *
+	 * For puds/pmds that are not present, check to see if it
+	 * could be a swap entry (!present and !none).
+	 */
+	if ((!pte_present(pud_pte(*pud)) && !pud_none(*pud)) || pud_huge(*pud))
 		return (pte_t *)pud;

Since we use puds as huge pages, can we just change pud_present() to match the pmd_present()? I'd like to see similar checks for pud and pmd, it would be easier to follow. Something like (unchecked):

	if (pud_none(*pud))
		return NULL;
	/* swap or huge page */
	if (!pud_present(*pud) || pud_huge(*pud))
		return (pte_t *)pud;
	/* table; check the next level */
Quoted 10 lines

arch/arm64/mm/hugetlbpage.c pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr) · line 163

Quoted 7 lines
arch/arm64/mm/hugetlbpage.c +4 -1
+
 	pmd = pmd_offset(pud, addr);
-	if (!pmd_present(*pmd))
+	if (pmd_none(*pmd))
 		return NULL;
+	if (!pmd_present(*pmd) && !pmd_none(*pmd))
+		return (pte_t *)pmd;

At this point, we already know that pmd_none(*pmd) is false, no ned to check it again.

Indeed - I was avoiding changing the function to drop contiguous hugepage support which follows this hunk.

I've made changes locally based on your suggestion and will post a revised version after the merge window.

Thanks,
Punit

#5 lore raw

Re: [PATCH 1/3] arm64: hugetlb: Fix huge_pte_offset to return poisoned page table entries

Quoted 5 lines · Punit Agrawal

Indeed - I was avoiding changing the function to drop contiguous hugepage support which follows this hunk.

I've made changes locally based on your suggestion and will post a revised version after the merge window.

Punit, will you be able to post a new version that could be a candidate for 4.13?

Signature · 3 lines
Qualcomm Innovation Center, Inc.
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project.
#6 lore raw

Re: [PATCH 1/3] arm64: hugetlb: Fix huge_pte_offset to return poisoned page table entries

Quoted 9 lines · Timur Tabi
Quoted 5 lines · Punit Agrawal

Indeed - I was avoiding changing the function to drop contiguous hugepage support which follows this hunk.

I've made changes locally based on your suggestion and will post a revised version after the merge window.

Punit, will you be able to post a new version that could be a candidate for 4.13?

Hi Timur,

I've just posted v2 for this series. I was testing the patches on rc1 when I saw your mail come through.

Please shout out if you notice any problems with the new version.

Thanks,
Punit

#7 lore raw

PATCH 2/3 arm64: hwpoison: add VM_FAULT_HWPOISON[_LARGE] handling

In reply to Punit Agrawal

From: "Jonathan (Zhixiong) Zhang" <[email protected]>

Add VM_FAULT_HWPOISON[_LARGE] handling to the arm64 page fault handler. Handling of VM_FAULT_HWPOISON[_LARGE] is very similar to VM_FAULT_OOM, the only difference is that a different si_code (BUS_MCEERR_AR) is passed to user space and si_addr_lsb field is initialized.

(fix new __do_user_fault call-site)

---
 arch/arm64/mm/fault.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
arch/arm64/mm/fault.c +19 -3
index 1b35b8bddbfb..7684219d8cc4 100644
@@ -31,6 +31,7 @@
 #include <linux/highmem.h>
 #include <linux/perf_event.h>
 #include <linux/preempt.h>
+#include <linux/hugetlb.h>
 
 #include <asm/bug.h>
 #include <asm/cpufeature.h>
@@ -207,10 +208,11 @@ static void __do_kernel_fault(struct mm_struct *mm, unsigned long addr,
  */
 static void __do_user_fault(struct task_struct *tsk, unsigned long addr,
 			    unsigned int esr, unsigned int sig, int code,
-			    struct pt_regs *regs)
+			    struct pt_regs *regs, int fault)
 {
 	struct siginfo si;
 	const struct fault_info *inf;
+	unsigned int lsb = 0;
 
 	if (unhandled_signal(tsk, sig) && show_unhandled_signals_ratelimited()) {
 		inf = esr_to_fault_info(esr);
@@ -227,6 +229,17 @@ static void __do_user_fault(struct task_struct *tsk, unsigned long addr,
 	si.si_errno = 0;
 	si.si_code = code;
 	si.si_addr = (void __user *)addr;
+	/*
+	 * Either small page or large page may be poisoned.
+	 * In other words, VM_FAULT_HWPOISON_LARGE and
+	 * VM_FAULT_HWPOISON are mutually exclusive.
+	 */
+	if (fault & VM_FAULT_HWPOISON_LARGE)
+		lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
+	else if (fault & VM_FAULT_HWPOISON)
+		lsb = PAGE_SHIFT;
+	si.si_addr_lsb = lsb;
+
 	force_sig_info(sig, &si, tsk);
 }
 
@@ -242,7 +255,7 @@ static void do_bad_area(unsigned long addr, unsigned int esr, struct pt_regs *re
 	 */
 	if (user_mode(regs)) {
 		inf = esr_to_fault_info(esr);
-		__do_user_fault(tsk, addr, esr, inf->sig, inf->code, regs);
+		__do_user_fault(tsk, addr, esr, inf->sig, inf->code, regs, 0);
 	} else
 		__do_kernel_fault(mm, addr, esr, regs);
 }
@@ -444,6 +457,9 @@ static int __kprobes do_page_fault(unsigned long addr, unsigned int esr,
 		 */
 		sig = SIGBUS;
 		code = BUS_ADRERR;
+	} else if (fault & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE)) {
+		sig = SIGBUS;
+		code = BUS_MCEERR_AR;
 	} else {
 		/*
 		 * Something tried to access memory that isn't in our memory
@@ -454,7 +470,7 @@ static int __kprobes do_page_fault(unsigned long addr, unsigned int esr,
 			SEGV_ACCERR : SEGV_MAPERR;
 	}
 
-	__do_user_fault(tsk, addr, esr, sig, code, regs);
+	__do_user_fault(tsk, addr, esr, sig, code, regs, fault);
 	return 0;
 
 no_context:
Signature · 1 line
2.11.0
#8 lore raw

PATCH 3/3 arm64: kconfig: allow support for memory failure handling

In reply to Punit Agrawal

From: "Jonathan (Zhixiong) Zhang" <[email protected]>

Declare ARCH_SUPPORTS_MEMORY_FAILURE, as arm64 does support memory failure recovery attempt.

(Dropped changes to ACPI APEI Kconfig and updated commit log)

---
 arch/arm64/Kconfig | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/Kconfig +1 -0
index 3741859765cf..993a5fd85452 100644
@@ -19,6 +19,7 @@ config ARM64
 	select ARCH_HAS_STRICT_MODULE_RWX
 	select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
 	select ARCH_USE_CMPXCHG_LOCKREF
+	select ARCH_SUPPORTS_MEMORY_FAILURE
 	select ARCH_SUPPORTS_ATOMIC_RMW
 	select ARCH_SUPPORTS_NUMA_BALANCING
 	select ARCH_WANT_COMPAT_IPC_PARSE_VERSION
Signature · 1 line
2.11.0