All of lore.kernel.org
 help / color / mirror / Atom feed
From: Nicholas Piggin <npiggin@gmail.com>
To: linuxppc-dev@lists.ozlabs.org
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>,
	Nicholas Piggin <npiggin@gmail.com>
Subject: [PATCH v7 05/42] powerpc/64s: move the hash fault handling logic to C
Date: Sat, 30 Jan 2021 23:08:15 +1000	[thread overview]
Message-ID: <20210130130852.2952424-6-npiggin@gmail.com> (raw)
In-Reply-To: <20210130130852.2952424-1-npiggin@gmail.com>

The fault handling still has some complex logic particularly around
hash table handling, in asm. Implement most of this in C.

Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
 arch/powerpc/include/asm/book3s/64/mmu-hash.h |   1 +
 arch/powerpc/kernel/exceptions-64s.S          | 127 ++++--------------
 arch/powerpc/mm/book3s64/hash_utils.c         |  77 +++++++----
 3 files changed, 78 insertions(+), 127 deletions(-)

diff --git a/arch/powerpc/include/asm/book3s/64/mmu-hash.h b/arch/powerpc/include/asm/book3s/64/mmu-hash.h
index 066b1d34c7bc..60a669379aa0 100644
--- a/arch/powerpc/include/asm/book3s/64/mmu-hash.h
+++ b/arch/powerpc/include/asm/book3s/64/mmu-hash.h
@@ -454,6 +454,7 @@ static inline unsigned long hpt_hash(unsigned long vpn,
 #define HPTE_NOHPTE_UPDATE	0x2
 #define HPTE_USE_KERNEL_KEY	0x4
 
+int do_hash_fault(struct pt_regs *regs, unsigned long ea, unsigned long dsisr);
 extern int __hash_page_4K(unsigned long ea, unsigned long access,
 			  unsigned long vsid, pte_t *ptep, unsigned long trap,
 			  unsigned long flags, int ssize, int subpage_prot);
diff --git a/arch/powerpc/kernel/exceptions-64s.S b/arch/powerpc/kernel/exceptions-64s.S
index a6333b986a57..07aba8af99d3 100644
--- a/arch/powerpc/kernel/exceptions-64s.S
+++ b/arch/powerpc/kernel/exceptions-64s.S
@@ -1401,14 +1401,15 @@ END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
  *
  * Handling:
  * - Hash MMU
- *   Go to do_hash_page first to see if the HPT can be filled from an entry in
- *   the Linux page table. Hash faults can hit in kernel mode in a fairly
+ *   Go to do_hash_fault, which attempts to fill the HPT from an entry in the
+ *   Linux page table. Hash faults can hit in kernel mode in a fairly
  *   arbitrary state (e.g., interrupts disabled, locks held) when accessing
  *   "non-bolted" regions, e.g., vmalloc space. However these should always be
- *   backed by Linux page tables.
+ *   backed by Linux page table entries.
  *
- *   If none is found, do a Linux page fault. Linux page faults can happen in
- *   kernel mode due to user copy operations of course.
+ *   If no entry is found the Linux page fault handler is invoked (by
+ *   do_hash_fault). Linux page faults can happen in kernel mode due to user
+ *   copy operations of course.
  *
  *   KVM: The KVM HDSI handler may perform a load with MSR[DR]=1 in guest
  *   MMU context, which may cause a DSI in the host, which must go to the
@@ -1439,27 +1440,29 @@ EXC_COMMON_BEGIN(data_access_common)
 	GEN_COMMON data_access
 	ld	r4,_DAR(r1)
 	ld	r5,_DSISR(r1)
+	addi	r3,r1,STACK_FRAME_OVERHEAD
 	andis.	r0,r5,DSISR_DABRMATCH@h
 	bne-	1f
 BEGIN_MMU_FTR_SECTION
-	ld	r6,_MSR(r1)
-	li	r3,0x300
-	b	do_hash_page		/* Try to handle as hpte fault */
+	bl	do_hash_fault
 MMU_FTR_SECTION_ELSE
-	b	handle_page_fault
+	bl	do_page_fault
 ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_TYPE_RADIX)
+	cmpdi	r3,0
+	beq+	interrupt_return
+	mr	r5,r3
+	addi	r3,r1,STACK_FRAME_OVERHEAD
+	ld	r4,_DAR(r1)
+	bl	__bad_page_fault
+	b	interrupt_return
 
-1:	/* We have a data breakpoint exception - handle it */
-	ld      r4,_DAR(r1)
-	ld      r5,_DSISR(r1)
-	addi    r3,r1,STACK_FRAME_OVERHEAD
-	bl      do_break
+1:	bl	do_break
 	/*
 	 * do_break() may have changed the NV GPRS while handling a breakpoint.
 	 * If so, we need to restore them with their updated values.
 	 */
 	REST_NVGPRS(r1)
-	b       interrupt_return
+	b	interrupt_return
 
 	GEN_KVM data_access
 
@@ -1554,13 +1557,19 @@ EXC_COMMON_BEGIN(instruction_access_common)
 	GEN_COMMON instruction_access
 	ld	r4,_DAR(r1)
 	ld	r5,_DSISR(r1)
+	addi	r3,r1,STACK_FRAME_OVERHEAD
 BEGIN_MMU_FTR_SECTION
-	ld      r6,_MSR(r1)
-	li	r3,0x400
-	b	do_hash_page		/* Try to handle as hpte fault */
+	bl	do_hash_fault
 MMU_FTR_SECTION_ELSE
-	b	handle_page_fault
+	bl	do_page_fault
 ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_TYPE_RADIX)
+	cmpdi	r3,0
+	beq+	interrupt_return
+	mr	r5,r3
+	addi	r3,r1,STACK_FRAME_OVERHEAD
+	ld	r4,_DAR(r1)
+	bl	__bad_page_fault
+	b	interrupt_return
 
 	GEN_KVM instruction_access
 
@@ -3235,83 +3244,3 @@ disable_machine_check:
 	RFI_TO_KERNEL
 1:	mtlr	r0
 	blr
-
-/*
- * Hash table stuff
- */
-	.balign	IFETCH_ALIGN_BYTES
-do_hash_page:
-#ifdef CONFIG_PPC_BOOK3S_64
-	lis	r0,(DSISR_BAD_FAULT_64S | DSISR_KEYFAULT)@h
-	ori	r0,r0,DSISR_BAD_FAULT_64S@l
-	and.	r0,r5,r0		/* weird error? */
-	bne-	handle_page_fault	/* if not, try to insert a HPTE */
-
-	/*
-	 * If we are in an "NMI" (e.g., an interrupt when soft-disabled), then
-	 * don't call hash_page, just fail the fault. This is required to
-	 * prevent re-entrancy problems in the hash code, namely perf
-	 * interrupts hitting while something holds H_PAGE_BUSY, and taking a
-	 * hash fault. See the comment in hash_preload().
-	 */
-	ld	r11, PACA_THREAD_INFO(r13)
-	lwz	r0,TI_PREEMPT(r11)
-	andis.	r0,r0,NMI_MASK@h
-	bne	77f
-
-	/*
-	 * r3 contains the trap number
-	 * r4 contains the faulting address
-	 * r5 contains dsisr
-	 * r6 msr
-	 *
-	 * at return r3 = 0 for success, 1 for page fault, negative for error
-	 */
-	bl	__hash_page		/* build HPTE if possible */
-        cmpdi	r3,0			/* see if __hash_page succeeded */
-
-	/* Success */
-	beq	interrupt_return	/* Return from exception on success */
-
-	/* Error */
-	blt-	13f
-
-	/* Reload DAR/DSISR into r4/r5 for handle_page_fault */
-	ld	r4,_DAR(r1)
-	ld      r5,_DSISR(r1)
-#endif /* CONFIG_PPC_BOOK3S_64 */
-
-/* Here we have a page fault that hash_page can't handle. */
-handle_page_fault:
-	addi	r3,r1,STACK_FRAME_OVERHEAD
-	bl	do_page_fault
-	cmpdi	r3,0
-	beq+	interrupt_return
-	mr	r5,r3
-	addi	r3,r1,STACK_FRAME_OVERHEAD
-	ld	r4,_DAR(r1)
-	bl	__bad_page_fault
-	b	interrupt_return
-
-#ifdef CONFIG_PPC_BOOK3S_64
-/* We have a page fault that hash_page could handle but HV refused
- * the PTE insertion
- */
-13:	mr	r5,r3
-	addi	r3,r1,STACK_FRAME_OVERHEAD
-	ld	r4,_DAR(r1)
-	bl	low_hash_fault
-	b	interrupt_return
-#endif
-
-/*
- * We come here as a result of a DSI at a point where we don't want
- * to call hash_page, such as when we are accessing memory (possibly
- * user memory) inside a PMU interrupt that occurred while interrupts
- * were soft-disabled.  We want to invoke the exception handler for
- * the access, or panic if there isn't a handler.
- */
-77:	addi	r3,r1,STACK_FRAME_OVERHEAD
-	li	r5,SIGSEGV
-	bl	bad_page_fault
-	b	interrupt_return
diff --git a/arch/powerpc/mm/book3s64/hash_utils.c b/arch/powerpc/mm/book3s64/hash_utils.c
index 73b06adb6eeb..e866cae57e2f 100644
--- a/arch/powerpc/mm/book3s64/hash_utils.c
+++ b/arch/powerpc/mm/book3s64/hash_utils.c
@@ -1512,16 +1512,40 @@ int hash_page(unsigned long ea, unsigned long access, unsigned long trap,
 }
 EXPORT_SYMBOL_GPL(hash_page);
 
-int __hash_page(unsigned long trap, unsigned long ea, unsigned long dsisr,
-		unsigned long msr)
+int do_hash_fault(struct pt_regs *regs, unsigned long ea, unsigned long dsisr)
 {
 	unsigned long access = _PAGE_PRESENT | _PAGE_READ;
 	unsigned long flags = 0;
-	struct mm_struct *mm = current->mm;
-	unsigned int region_id = get_region_id(ea);
+	struct mm_struct *mm;
+	unsigned int region_id;
+	int err;
+
+	if (unlikely(dsisr & (DSISR_BAD_FAULT_64S | DSISR_KEYFAULT)))
+		goto page_fault;
+
+	/*
+	 * If we are in an "NMI" (e.g., an interrupt when soft-disabled), then
+	 * don't call hash_page, just fail the fault. This is required to
+	 * prevent re-entrancy problems in the hash code, namely perf
+	 * interrupts hitting while something holds H_PAGE_BUSY, and taking a
+	 * hash fault. See the comment in hash_preload().
+	 *
+	 * We come here as a result of a DSI at a point where we don't want
+	 * to call hash_page, such as when we are accessing memory (possibly
+	 * user memory) inside a PMU interrupt that occurred while interrupts
+	 * were soft-disabled.  We want to invoke the exception handler for
+	 * the access, or panic if there isn't a handler.
+	 */
+	if (unlikely(in_nmi())) {
+		bad_page_fault(regs, ea, SIGSEGV);
+		return 0;
+	}
 
+	region_id = get_region_id(ea);
 	if ((region_id == VMALLOC_REGION_ID) || (region_id == IO_REGION_ID))
 		mm = &init_mm;
+	else
+		mm = current->mm;
 
 	if (dsisr & DSISR_NOHPTE)
 		flags |= HPTE_NOHPTE_UPDATE;
@@ -1537,13 +1561,31 @@ int __hash_page(unsigned long trap, unsigned long ea, unsigned long dsisr,
 	 * 2) user space access kernel space.
 	 */
 	access |= _PAGE_PRIVILEGED;
-	if ((msr & MSR_PR) || (region_id == USER_REGION_ID))
+	if (user_mode(regs) || (region_id == USER_REGION_ID))
 		access &= ~_PAGE_PRIVILEGED;
 
-	if (trap == 0x400)
+	if (regs->trap == 0x400)
 		access |= _PAGE_EXEC;
 
-	return hash_page_mm(mm, ea, access, trap, flags);
+	err = hash_page_mm(mm, ea, access, regs->trap, flags);
+	if (unlikely(err < 0)) {
+		// failed to instert a hash PTE due to an hypervisor error
+		if (user_mode(regs)) {
+			if (IS_ENABLED(CONFIG_PPC_SUBPAGE_PROT) && err == -2)
+				_exception(SIGSEGV, regs, SEGV_ACCERR, ea);
+			else
+				_exception(SIGBUS, regs, BUS_ADRERR, ea);
+		} else {
+			bad_page_fault(regs, ea, SIGBUS);
+		}
+		err = 0;
+
+	} else if (err) {
+page_fault:
+		err = do_page_fault(regs, ea, dsisr);
+	}
+
+	return err;
 }
 
 #ifdef CONFIG_PPC_MM_SLICES
@@ -1843,27 +1885,6 @@ void flush_hash_range(unsigned long number, int local)
 	}
 }
 
-/*
- * low_hash_fault is called when we the low level hash code failed
- * to instert a PTE due to an hypervisor error
- */
-void low_hash_fault(struct pt_regs *regs, unsigned long address, int rc)
-{
-	enum ctx_state prev_state = exception_enter();
-
-	if (user_mode(regs)) {
-#ifdef CONFIG_PPC_SUBPAGE_PROT
-		if (rc == -2)
-			_exception(SIGSEGV, regs, SEGV_ACCERR, address);
-		else
-#endif
-			_exception(SIGBUS, regs, BUS_ADRERR, address);
-	} else
-		bad_page_fault(regs, address, SIGBUS);
-
-	exception_exit(prev_state);
-}
-
 long hpte_insert_repeating(unsigned long hash, unsigned long vpn,
 			   unsigned long pa, unsigned long rflags,
 			   unsigned long vflags, int psize, int ssize)
-- 
2.23.0


  parent reply	other threads:[~2021-01-30 13:30 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-30 13:08 [PATCH v7 00/42] powerpc: interrupt wrappers Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 01/42] powerpc/64s: interrupt exit improve bounding of interrupt recursion Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 02/42] KVM: PPC: Book3S HV: Context tracking exit guest context before enabling irqs Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 03/42] powerpc/32s: move DABR match out of handle_page_fault Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 04/42] powerpc/64s: " Nicholas Piggin
2021-01-30 13:08 ` Nicholas Piggin [this message]
2021-01-30 13:08 ` [PATCH v7 06/42] powerpc: remove arguments from fault handler functions Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 07/42] powerpc/fsl_booke/32: CacheLockingException remove args Nicholas Piggin
2021-02-08 10:04   ` Gautham R Shenoy
2021-02-08 11:54     ` Michael Ellerman
2021-01-30 13:08 ` [PATCH v7 08/42] powerpc: do_break get registers from regs Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 09/42] powerpc: DebugException remove args Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 10/42] powerpc/32: transfer can avoid saving r4/r5 over trace call Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 11/42] powerpc: bad_page_fault get registers from regs Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 12/42] powerpc/64s: add do_bad_page_fault_segv handler Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 13/42] powerpc: rearrange do_page_fault error case to be inside exception_enter Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 14/42] powerpc/64s: move bad_page_fault handling to C Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 15/42] powerpc/64s: split do_hash_fault Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 16/42] powerpc/mm: Remove stale do_page_fault comment referring to SLB faults Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 17/42] powerpc/64s: slb comment update Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 18/42] powerpc/traps: add NOKPROBE_SYMBOL for sreset and mce Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 19/42] powerpc/perf: move perf irq/nmi handling details into traps.c Nicholas Piggin
2021-01-31 12:30   ` Athira Rajeev
2021-01-30 13:08 ` [PATCH v7 20/42] powerpc/time: move timer_broadcast_interrupt prototype to asm/time.h Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 21/42] powerpc: add and use unknown_async_exception Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 22/42] powerpc/cell: tidy up pervasive declarations Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 23/42] powerpc: introduce die_mce Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 24/42] powerpc/mce: ensure machine check handler always tests RI Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 25/42] powerpc: improve handling of unrecoverable system reset Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 26/42] powerpc: interrupt handler wrapper functions Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 27/42] powerpc: add interrupt wrapper entry / exit stub functions Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 28/42] powerpc: convert interrupt handlers to use wrappers Nicholas Piggin
2021-02-03  1:54   ` Nicholas Piggin
2021-02-05  8:09   ` Christophe Leroy
2021-02-06  2:43     ` Nicholas Piggin
2021-02-07 12:56     ` Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 29/42] powerpc: add interrupt_cond_local_irq_enable helper Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 30/42] powerpc/64: context tracking remove _TIF_NOHZ Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 31/42] powerpc/64s/hash: improve context tracking of hash faults Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 32/42] powerpc/64: context tracking move to interrupt wrappers Nicholas Piggin
2021-02-09  5:49   ` Christophe Leroy
2021-02-09  7:45     ` Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 33/42] powerpc/64: add context tracking to asynchronous interrupts Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 34/42] powerpc: handle irq_enter/irq_exit in interrupt handler wrappers Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 35/42] powerpc/64s: move context tracking exit to interrupt exit path Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 36/42] powerpc/64s: reconcile interrupts in C Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 37/42] powerpc/64: move account_stolen_time into its own function Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 38/42] powerpc/64: entry cpu time accounting in C Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 39/42] powerpc: move NMI entry/exit code into wrapper Nicholas Piggin
2021-02-04 10:15   ` Michael Ellerman
2021-02-04 11:31     ` Nicholas Piggin
2021-02-05 23:38       ` Michael Ellerman
2021-02-06  2:46         ` Nicholas Piggin
2021-02-07 12:54           ` Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 40/42] powerpc/64s: move NMI soft-mask handling to C Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 41/42] powerpc/64s: runlatch interrupt handling in C Nicholas Piggin
2021-01-30 13:08 ` [PATCH v7 42/42] powerpc/64s: power4 nap fixup " Nicholas Piggin
2021-02-02 10:31   ` Michael Ellerman
2021-02-03  0:35     ` Nicholas Piggin
2021-02-07 12:58     ` Nicholas Piggin
2021-02-02  5:57 ` [PATCH v7 00/42] powerpc: interrupt wrappers Christophe Leroy
2021-02-10 12:57 ` Michael Ellerman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210130130852.2952424-6-npiggin@gmail.com \
    --to=npiggin@gmail.com \
    --cc=atrajeev@linux.vnet.ibm.com \
    --cc=linuxppc-dev@lists.ozlabs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.