Skip to contentSkip to conversation

>From 3ebb85898a0542dcccaa3ab6e7b559ed6a4ff50f Mon Sep 17 00:00:00 2001

DISCUSSION Hanjun Guo 2 participants 5 messages Feb 2, 2016

Subsystems (2)
lore
Share & tools
lore

Hanjun Guo

author
#0 lore raw

Hi Greg,

Here are 3 candidate patches for stable linux-4.1.y, patch 2 alredy Cc stable in the change log, but need patch 1 (which also a bugfix) to apply patch 2 cleanly, the last one is worth to take as a bugfix too I think, please consider to merge :)

Thanks
Hanjun

Josh Stone (1):
arm64: fix missing syscall trace exit

Will Deacon (2):
arm64: entry: always restore x0 from the stack on syscall return
arm64: mm: ensure patched kernel text is fetched from PoU

 arch/arm64/kernel/entry.S | 22 +++++++++++-----------
 arch/arm64/kernel/head.S  |  8 ++++++++
 arch/arm64/kernel/sleep.S |  8 ++++++++
 arch/arm64/mm/proc.S      |  1 -
 4 files changed, 27 insertions(+), 12 deletions(-)
Signature · 1 line
1.9.1
author
#1 lore raw

commit 412fcb6cebd758d080cacd5a41a0cbc656ea5fce upstream.

We have a micro-optimisation on the fast syscall return path where we take care to keep x0 live with the return value from the syscall so that we can avoid restoring it from the stack. The benefit of doing this is fairly suspect, since we will be restoring x1 from the stack anyway (which lives adjacent in the pt_regs structure) and the only additional cost is saving x0 back to pt_regs after the syscall handler, which could be seen as a poor man's prefetch.

More importantly, this causes issues with the context tracking code.

The ct_user_enter macro ends up branching into C code, which is free to use x0 as a scratch register and consequently leads to us returning junk back to userspace as the syscall return value. Rather than special case the context-tracking code, this patch removes the questionable optimisation entirely.

---
 arch/arm64/kernel/entry.S | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
arch/arm64/kernel/entry.S +6 -11
index 6657a09..3236b3e 100644
@@ -116,7 +116,7 @@
 	*/
 	.endm
 
-	.macro	kernel_exit, el, ret = 0
+	.macro	kernel_exit, el
 	ldp	x21, x22, [sp, #S_PC]		// load ELR, SPSR
 	.if	\el == 0
 	ct_user_enter
@@ -143,11 +143,7 @@
 	.endif
 	msr	elr_el1, x21			// set up the return data
 	msr	spsr_el1, x22
-	.if	\ret
-	ldr	x1, [sp, #S_X1]			// preserve x0 (syscall return)
-	.else
 	ldp	x0, x1, [sp, #16 * 0]
-	.endif
 	ldp	x2, x3, [sp, #16 * 1]
 	ldp	x4, x5, [sp, #16 * 2]
 	ldp	x6, x7, [sp, #16 * 3]
@@ -609,22 +605,21 @@ ENDPROC(cpu_switch_to)
  */
 ret_fast_syscall:
 	disable_irq				// disable interrupts
+	str	x0, [sp, #S_X0]			// returned x0
 	ldr	x1, [tsk, #TI_FLAGS]		// re-check for syscall tracing
 	and	x2, x1, #_TIF_SYSCALL_WORK
 	cbnz	x2, ret_fast_syscall_trace
 	and	x2, x1, #_TIF_WORK_MASK
-	cbnz	x2, fast_work_pending
+	cbnz	x2, work_pending
 	enable_step_tsk x1, x2
-	kernel_exit 0, ret = 1
+	kernel_exit 0
 ret_fast_syscall_trace:
 	enable_irq				// enable interrupts
-	b	__sys_trace_return
+	b	__sys_trace_return_skipped	// we already saved x0
 
 /*
  * Ok, we need to do extra processing, enter the slow path.
  */
-fast_work_pending:
-	str	x0, [sp, #S_X0]			// returned x0
 work_pending:
 	tbnz	x1, #TIF_NEED_RESCHED, work_resched
 	/* TIF_SIGPENDING, TIF_NOTIFY_RESUME or TIF_FOREIGN_FPSTATE case */
@@ -648,7 +643,7 @@ ret_to_user:
 	cbnz	x2, work_pending
 	enable_step_tsk x1, x2
 no_work_pending:
-	kernel_exit 0, ret = 0
+	kernel_exit 0
 ENDPROC(ret_to_user)
 
 /*
Signature · 1 line
1.9.1
author
#2 lore raw
In reply to Hanjun Guo

The arm64 booting document requires that the bootloader has cleaned the kernel image to the PoC. However, when a CPU re-enters the kernel due to either a CPU hotplug "on" event or resuming from a low-power state (e.g. cpuidle), the kernel text may in-fact be dirty at the PoU due to things like alternative patching or even module loading.

Thanks to I-cache speculation with the MMU off, stale instructions could be fetched prior to enabling the MMU, potentially leading to crashes when executing regions of code that have been modified at runtime.

This patch addresses the issue by ensuring that the local I-cache is invalidated immediately after a CPU has enabled its MMU but before jumping out of the identity mapping. Any stale instructions fetched from the PoC will then be discarded and refetched correctly from the PoU. Patching kernel text executed prior to the MMU being enabled is prohibited, so the early entry code will always be clean.

---
 arch/arm64/kernel/head.S  | 8 ++++++++
 arch/arm64/kernel/sleep.S | 8 ++++++++
 arch/arm64/mm/proc.S      | 1 -
 3 files changed, 16 insertions(+), 1 deletion(-)
arch/arm64/kernel/head.S +8 -0
index 36aa31f..af6e4e8 100644
@@ -682,5 +682,13 @@ __enable_mmu:
 	isb
 	msr	sctlr_el1, x0
 	isb
+	/*
+	 * Invalidate the local I-cache so that any instructions fetched
+	 * speculatively from the PoC are discarded, since they may have
+	 * been dynamically patched at the PoU.
+	 */
+	ic	iallu
+	dsb	nsh
+	isb
 	br	x27
 ENDPROC(__enable_mmu)
arch/arm64/kernel/sleep.S +8 -0
index ede186c..1c6969b 100644
@@ -134,6 +134,14 @@ ENTRY(cpu_resume_mmu)
 	ldr	x3, =cpu_resume_after_mmu
 	msr	sctlr_el1, x0		// restore sctlr_el1
 	isb
+	/*
+	 * Invalidate the local I-cache so that any instructions fetched
+	 * speculatively from the PoC are discarded, since they may have
+	 * been dynamically patched at the PoU.
+	 */
+	ic	iallu
+	dsb	nsh
+	isb
 	br	x3			// global jump to virtual address
 ENDPROC(cpu_resume_mmu)
 cpu_resume_after_mmu:
arch/arm64/mm/proc.S +0 -1
index cdd754e..ee18bbc 100644
@@ -196,7 +196,6 @@ ENDPROC(cpu_do_switch_mm)
  *	value of the SCTLR_EL1 register.
  */
 ENTRY(__cpu_setup)
-	ic	iallu				// I+BTB cache invalidate
 	tlbi	vmalle1is			// invalidate I + D TLBs
 	dsb	ish
 
Signature · 1 line
1.9.1
author
#3 lore raw
In reply to Hanjun Guo

commit 04d7e098f541769721d7511d56aea4b976fd29fd upstream.

If a syscall is entered without TIF_SYSCALL_TRACE set, then it goes on the fast path. It's then possible to have TIF_SYSCALL_TRACE added in the middle of the syscall, but ret_fast_syscall doesn't check this flag again. This causes a ptrace syscall-exit-stop to be missed.

For instance, from a PTRACE_EVENT_FORK reported during do_fork, the tracer might resume with PTRACE_SYSCALL, setting TIF_SYSCALL_TRACE. Now the completion of the fork should have a syscall-exit-stop.

Russell King fixed this on arm by re-checking _TIF_SYSCALL_WORK in the fast exit path. Do the same on arm64.

---
 arch/arm64/kernel/entry.S | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/arm64/kernel/entry.S +6 -1
index bddd04d..6657a09 100644
@@ -609,11 +609,16 @@ ENDPROC(cpu_switch_to)
  */
 ret_fast_syscall:
 	disable_irq				// disable interrupts
-	ldr	x1, [tsk, #TI_FLAGS]
+	ldr	x1, [tsk, #TI_FLAGS]		// re-check for syscall tracing
+	and	x2, x1, #_TIF_SYSCALL_WORK
+	cbnz	x2, ret_fast_syscall_trace
 	and	x2, x1, #_TIF_WORK_MASK
 	cbnz	x2, fast_work_pending
 	enable_step_tsk x1, x2
 	kernel_exit 0, ret = 1
+ret_fast_syscall_trace:
+	enable_irq				// enable interrupts
+	b	__sys_trace_return
 
 /*
  * Ok, we need to do extra processing, enter the slow path.
Signature · 1 line
1.9.1
#4 lore raw
In reply to Hanjun Guo

Hi Greg,

I screw up the tile, please ignore this patch set, I will update it soon.

Thanks
Hanjun

Quoted 23 lines · Hanjun Guo

Hi Greg,

Here are 3 candidate patches for stable linux-4.1.y, patch 2 alredy Cc stable in the change log, but need patch 1 (which also a bugfix) to apply patch 2 cleanly, the last one is worth to take as a bugfix too I think, please consider to merge :)

Thanks
Hanjun

Josh Stone (1):
arm64: fix missing syscall trace exit

Will Deacon (2):
arm64: entry: always restore x0 from the stack on syscall return
arm64: mm: ensure patched kernel text is fetched from PoU

  arch/arm64/kernel/entry.S | 22 +++++++++++-----------
  arch/arm64/kernel/head.S  |  8 ++++++++
  arch/arm64/kernel/sleep.S |  8 ++++++++
  arch/arm64/mm/proc.S      |  1 -
  4 files changed, 27 insertions(+), 12 deletions(-)