Skip to contentSkip to conversation

ARM: enable IRQ stacks and vmap'ed stacks for UP

RFC PATCH Superseded Ard Biesheuvel 4 participants 11 messages Nov 26, 2021 – Nov 30, 2021

Subsystems (3)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

cover ARM: enable IRQ stacks and vmap'ed stacks for UP

Enable the use of the TLS register to hold the 'current' pointer for all configurations that can support it, including non-SMP ones that target v6k or later CPUs, and multi-platform SMP ones that also support v6 based UP systems.

The remaining configurations are all strictly UP, which means we can switch to a global variable to hold the current pointer. By doing this, we can enable THREAD_INFO_IN_TASK, which moves thread info off the stack, protecting it from overflows. It also permits us to enable IRQ stacks and vmap'ed stacks for UP configurations as well.

Supporting v6 cores without SMP extensions in SMP configurations (e.g., omap2plus_defconfig or imx_v6_v7_defconfig) makes this a bit tricky, and this is a feature we may consider dropping entirely in the future. But for the time being, we can support this mode as well.

The accesses to the global variable holding 'current' are constructed in a way that ensures that no literal pool accesses (and associated D-cache misses) are needed unless the access is from a module and module PLTs are enabled. This means that accessing 'current' is just as costly as before, as it used to require some arithmetic involving the stack pointer and a load from the thread_info::task field.

However, accessing thread_info itself now also involves a load, although it should be noted that all thread_info and current accesses now go via the same variable, which is therefore expected to be hot in the caches at all times.

Cc Russell King <[email protected]>
Cc Nicolas Pitre <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Kees Cook <[email protected]>
Cc Keith Packard <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Tony Lindgren <[email protected]>

Ard Biesheuvel (6):
ARM: entry: preserve thread_info pointer in switch_to
ARM: module: implement support for PC-relative group relocations
ARM: percpu: add SMP_ON_UP support
ARM: smp: defer TPIDRURO update for SMP v6 configurations too
ARM: use TLS register for 'current' on !SMP as well
ARM: implement THREAD_INFO_IN_TASK for uniprocessor systems

 arch/arm/Kconfig                   |  10 +-
 arch/arm/include/asm/assembler.h   | 136 +++++++++++++++-----
 arch/arm/include/asm/current.h     |  56 +++++++-
 arch/arm/include/asm/elf.h         |   3 +
 arch/arm/include/asm/percpu.h      |  29 ++++-
 arch/arm/include/asm/switch_to.h   |   3 +-
 arch/arm/include/asm/thread_info.h |  27 ----
 arch/arm/include/asm/tls.h         |   4 +-
 arch/arm/kernel/asm-offsets.c      |   3 -
 arch/arm/kernel/entry-armv.S       |  26 ++--
 arch/arm/kernel/entry-header.S     |   8 +-
 arch/arm/kernel/head-common.S      |   4 +-
 arch/arm/kernel/module.c           |  63 +++++++++
 arch/arm/kernel/process.c          |   7 +-
 arch/arm/kernel/traps.c            |   4 +
 arch/arm/mm/Kconfig                |   1 +
 16 files changed, 289 insertions(+), 95 deletions(-)
Signature · 7 lines
2.30.2


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#1 lore raw

RFC PATCH 1/6 ARM: entry: preserve thread_info pointer in switch_to

Tweak the UP stack protector handling code so that the thread info pointer is preserved in R7 until set_current is called. This is needed for a subsequent patch that implements THREAD_INFO_IN_TASK and set_current for UP as well.

This also means we will prefer the per-task protector on UP systems that implement the thread ID registers, so tweak the preprocessor conditionals to reflect this.

---
 arch/arm/kernel/entry-armv.S | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
arch/arm/kernel/entry-armv.S +9 -8
index 5fb7465d14d9..1e26d69ebbf1 100644
@@ -814,16 +814,16 @@ ENTRY(__switch_to)
 	ldr	r6, [r2, #TI_CPU_DOMAIN]
 #endif
 	switch_tls r1, r4, r5, r3, r7
-#if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_SMP)
-	ldr	r7, [r2, #TI_TASK]
+#if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_SMP) && \
+    !defined(CONFIG_STACKPROTECTOR_PER_TASK)
+	ldr	r9, [r2, #TI_TASK]
 	ldr	r8, =__stack_chk_guard
 	.if (TSK_STACK_CANARY > IMM12_MASK)
-	add	r7, r7, #TSK_STACK_CANARY & ~IMM12_MASK
+	add	r9, r9, #TSK_STACK_CANARY & ~IMM12_MASK
 	.endif
-	ldr	r7, [r7, #TSK_STACK_CANARY & IMM12_MASK]
-#elif defined(CONFIG_CURRENT_POINTER_IN_TPIDRURO)
-	mov	r7, r2				@ Preserve 'next'
+	ldr	r9, [r9, #TSK_STACK_CANARY & IMM12_MASK]
 #endif
+	mov	r7, r2				@ Preserve 'next'
 #ifdef CONFIG_CPU_USE_DOMAINS
 	mcr	p15, 0, r6, c3, c0, 0		@ Set domain register
 #endif
@@ -832,8 +832,9 @@ ENTRY(__switch_to)
 	ldr	r0, =thread_notify_head
 	mov	r1, #THREAD_NOTIFY_SWITCH
 	bl	atomic_notifier_call_chain
-#if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_SMP)
-	str	r7, [r8]
+#if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_SMP) && \
+    !defined(CONFIG_STACKPROTECTOR_PER_TASK)
+	str	r9, [r8]
 #endif
 	mov	r0, r5
 #if !defined(CONFIG_THUMB2_KERNEL) && !defined(CONFIG_VMAP_STACK)
Signature · 7 lines
2.30.2


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#2 lore raw

RFC PATCH 2/6 ARM: module: implement support for PC-relative group relocations

In reply to Ard Biesheuvel

Add support for the R_ARM_ALU_PC_Gn_NC and R_ARM_LDR_PC_G2 group relocations so we can use them in modules. These will be used to load the current task pointer from a global variable without having to rely on a literal pool entry to carry the address of this variable, which would be slightly less efficient.

---
 arch/arm/include/asm/elf.h |  3 +
 arch/arm/kernel/module.c   | 63 ++++++++++++++++++++
 2 files changed, 66 insertions(+)
arch/arm/include/asm/elf.h +3 -0
index b8102a6ddf16..d68101655b74 100644
@@ -61,6 +61,9 @@ typedef struct user_fp elf_fpregset_t;
 #define R_ARM_MOVT_ABS		44
 #define R_ARM_MOVW_PREL_NC	45
 #define R_ARM_MOVT_PREL		46
+#define R_ARM_ALU_PC_G0_NC	57
+#define R_ARM_ALU_PC_G1_NC	59
+#define R_ARM_LDR_PC_G2		63
 
 #define R_ARM_THM_CALL		10
 #define R_ARM_THM_JUMP24	30
arch/arm/kernel/module.c +63 -0

81 lines not on this page · raw message

Signature · 7 lines
2.30.2


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#3 lore raw

RFC PATCH 3/6 ARM: percpu: add SMP_ON_UP support

In reply to Ard Biesheuvel

Permit the use of the TPIDRPRW system register for carrying the per-CPU offset in generic SMP configurations that also target non-SMP capable ARMv6 cores. This uses the SMP_ON_UP code patching framework to turn all TPIDRPRW accesses into reads/writes of entry #0 in the __per_cpu_offset array.

While at it, switch over some existing direct TPIDRPRW accesses in asm code to invocations of a new helper that is patched in the same way when necessary.

Note that CPU_V6+SMP without SMP_ON_UP results in a kernel that does not boot on v6 CPUs without SMP extensions, so add this dependency to Kconfig as well.

---
 arch/arm/include/asm/assembler.h | 21 ++++++++++++++
 arch/arm/include/asm/percpu.h    | 29 ++++++++++++++++++--
 arch/arm/kernel/entry-armv.S     |  4 +--
 arch/arm/mm/Kconfig              |  1 +
 4 files changed, 50 insertions(+), 5 deletions(-)
arch/arm/include/asm/assembler.h +21 -0
index 1b9d4df331aa..c4c1d5b2edf5 100644
@@ -312,6 +312,27 @@ THUMB(	fpreg	.req	r7	)
 #define ALT_UP_B(label) b label
 #endif
 
+	/*
+	 * this_cpu_offset - load the per-CPU offset of this CPU into
+	 * 		     register 'rd'
+	 */
+	.macro	this_cpu_offset, rd:req
+#ifdef CONFIG_SMP
+ALT_SMP(mrc p15, 0, \rd, c13, c0, 4)
+#ifdef CONFIG_CPU_V6
+ALT_UP_B(.L1_\@)
+.L0_\@:
+	.subsection 1
+.L1_\@: ldr	\rd, =__per_cpu_offset
+	ldr	\rd, [\rd]
+	b	.L0_\@
+	.previous
+#endif
+#else
+	mov	\rd, #0
+#endif
+	.endm
+
 /*
  * Instruction barrier
  */
arch/arm/include/asm/percpu.h +26 -3
index e2fcb3cfd3de..7b984352e402 100644
@@ -5,15 +5,25 @@
 #ifndef _ASM_ARM_PERCPU_H_
 #define _ASM_ARM_PERCPU_H_
 
+#include <linux/threads.h>
+
 register unsigned long current_stack_pointer asm ("sp");
 
 /*
  * Same as asm-generic/percpu.h, except that we store the per cpu offset
  * in the TPIDRPRW. TPIDRPRW only exists on V6K and V7
  */
-#if defined(CONFIG_SMP) && !defined(CONFIG_CPU_V6)
+#ifdef CONFIG_SMP
+extern unsigned long __per_cpu_offset[NR_CPUS];
+extern unsigned int smp_on_up;
+
 static inline void set_my_cpu_offset(unsigned long off)
 {
+	if (IS_ENABLED(CONFIG_CPU_V6) && !smp_on_up) {
+		__per_cpu_offset[0] = off;
+		return;
+	}
+
 	/* Set TPIDRPRW */
 	asm volatile("mcr p15, 0, %0, c13, c0, 4" : : "r" (off) : "memory");
 }
@@ -27,8 +37,21 @@ static inline unsigned long __my_cpu_offset(void)
 	 * We want to allow caching the value, so avoid using volatile and
 	 * instead use a fake stack read to hazard against barrier().
 	 */
-	asm("mrc p15, 0, %0, c13, c0, 4" : "=r" (off)
-		: "Q" (*(const unsigned long *)current_stack_pointer));
+	asm("0:	mrc p15, 0, %0, c13, c0, 4			\n\t"
+#ifdef CONFIG_CPU_V6
+	    "1:							\n\t"
+	    "	.subsection 1					\n\t"
+	    "2: ldr	%0, =__per_cpu_offset			\n\t"
+	    "	ldr	%0, [%0]				\n\t"
+	    "	b	1b					\n\t"
+	    "	.previous					\n\t"
+	    "	.pushsection \".alt.smp.init\", \"a\"		\n\t"
+	    "	.long	0b - .					\n\t"
+	    "	b	. + (2b - 0b)				\n\t"
+	    "	.popsection					\n\t"
+#endif
+	     : "=r" (off)
+	     : "Q" (*(const unsigned long *)current_stack_pointer));
 
 	return off;
 }
arch/arm/kernel/entry-armv.S +2 -2
index 1e26d69ebbf1..09a9fe501094 100644
@@ -41,7 +41,7 @@
 	mov	r0, sp
 #ifdef CONFIG_IRQSTACKS
 	mov_l	r2, irq_stack_ptr	@ Take base address
-	mrc	p15, 0, r3, c13, c0, 4	@ Get CPU offset
+	this_cpu_offset r3
 #ifdef CONFIG_UNWINDER_ARM
 	mov	fpreg, sp		@ Preserve original SP
 #else
@@ -884,7 +884,7 @@ __bad_stack:
 THUMB(	bx	pc		)
 THUMB(	nop			)
 THUMB(	.arm			)
-	mrc	p15, 0, ip, c13, c0, 4		@ Get per-CPU offset
+	this_cpu_offset ip
 
 	.globl	overflow_stack_ptr
 	.reloc	0f, R_ARM_ALU_PC_G0_NC, overflow_stack_ptr
arch/arm/mm/Kconfig +1 -0
index 58afba346729..a91ff22c6c2e 100644
@@ -386,6 +386,7 @@ config CPU_V6
 	select CPU_PABRT_V6
 	select CPU_THUMB_CAPABLE
 	select CPU_TLB_V6 if MMU
+	select SMP_ON_UP if SMP
 
 # ARMv6k
 config CPU_V6K
Signature · 7 lines
2.30.2


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#4 lore raw

RFC PATCH 4/6 ARM: smp: defer TPIDRURO update for SMP v6 configurations too

In reply to Ard Biesheuvel

Defer TPIDURO updates for user space until exit for CPU_V6+SMP configurations as well so that we can decide at runtime whether to use it to carry the current pointer, provided that we are running on a CPU that actually implements this register. This is needed for THREAD_INFO_IN_TASK support for UP systems, which requires that all SMP capable systems use the TPIDRURO based access to 'current' as the only remaining alternative will be a global variable which only work on UP.

---
 arch/arm/include/asm/tls.h     | 4 +++-
 arch/arm/kernel/entry-header.S | 8 +++++++-
 2 files changed, 10 insertions(+), 2 deletions(-)
arch/arm/include/asm/tls.h +3 -1
index c3296499176c..9c0965c14a21 100644
@@ -24,7 +24,9 @@
 	tst	\tmp1, #HWCAP_TLS		@ hardware TLS available?
 	streq	\tp, [\tmp2, #-15]		@ set TLS value at 0xffff0ff0
 	mrcne	p15, 0, \tmp2, c13, c0, 2	@ get the user r/w register
+#ifndef CONFIG_SMP
 	mcrne	p15, 0, \tp, c13, c0, 3		@ yes, set TLS register
+#endif
 	mcrne	p15, 0, \tpuser, c13, c0, 2	@ set user r/w register
 	strne	\tmp2, [\base, #TI_TP_VALUE + 4] @ save it
 	.endm
@@ -43,7 +45,7 @@
 #elif defined(CONFIG_CPU_V6)
 #define tls_emu		0
 #define has_tls_reg		(elf_hwcap & HWCAP_TLS)
-#define defer_tls_reg_update	0
+#define defer_tls_reg_update	IS_ENABLED(CONFIG_SMP)
 #define switch_tls	switch_tls_v6
 #elif defined(CONFIG_CPU_32v6K)
 #define tls_emu		0
arch/arm/kernel/entry-header.S +7 -1
index 81df2a3561ca..aea716c8b97c 100644
@@ -292,12 +292,18 @@
 
 
 	.macro	restore_user_regs, fast = 0, offset = 0
-#if defined(CONFIG_CPU_32v6K) && !defined(CONFIG_CPU_V6)
+#if defined(CONFIG_CPU_32v6K) || \
+    (defined(CONFIG_CPU_V6) && defined(CONFIG_SMP))
 	@ The TLS register update is deferred until return to user space so we
 	@ can use it for other things while running in the kernel
+#ifdef CONFIG_CPU_V6
+ALT_SMP(nop)
+ALT_UP_B(.L0_\@)
+#endif
 	get_thread_info r1
 	ldr	r1, [r1, #TI_TP_VALUE]
 	mcr	p15, 0, r1, c13, c0, 3		@ set TLS register
+.L0_\@:
 #endif
 
 	uaccess_enable r1, isb=0
Signature · 7 lines
2.30.2


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#5 lore raw

RFC PATCH 5/6 ARM: use TLS register for 'current' on !SMP as well

In reply to Ard Biesheuvel

Enable the use of the TLS register to hold the 'current' pointer also on non-SMP configurations that target v6k or later CPUs. This will permit the use of THREAD_INFO_IN_TASK as well as IRQ stacks and vmap'ed stacks for such configurations.

---
 arch/arm/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/Kconfig +1 -1
index e2ab72f2bf4a..61fc5cc03042 100644
@@ -1165,7 +1165,7 @@ config SMP_ON_UP
 
 config CURRENT_POINTER_IN_TPIDRURO
 	def_bool y
-	depends on SMP && CPU_32v6K && !CPU_V6
+	depends on CPU_32v6K && !CPU_V6
 
 config IRQSTACKS
 	def_bool y
Signature · 7 lines
2.30.2


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#6 lore raw

RFC PATCH 6/6 ARM: implement THREAD_INFO_IN_TASK for uniprocessor systems

In reply to Ard Biesheuvel

On UP systems, only a single task can be 'current' at the same time, which means we can use a global variable to track it. This means we can also enable THREAD_INFO_IN_TASK for those systems, as in that case, thread_info is accessed via current rather than the other way around, removing the need to store thread_info at the base of the task stack. This, in turn, permits us to enable IRQ stacks and vmap'ed stacks on UP systems as well.

To partially mitigate the performance overhead of this arrangement, use a ADD/ADD/LDR sequence with the appropriate PC-relative group relocations to load the value of current when needed. This means that accessing current will still only require a single load as before, avoiding the need for a literal to carry the address of the global variable in each function. However, accessing thread_info will now require this load as well.

---
 arch/arm/Kconfig                   |   8 +-
 arch/arm/include/asm/assembler.h   | 115 ++++++++++++++------
 arch/arm/include/asm/current.h     |  56 ++++++++--
 arch/arm/include/asm/switch_to.h   |   3 +-
 arch/arm/include/asm/thread_info.h |  27 -----
 arch/arm/kernel/asm-offsets.c      |   3 -
 arch/arm/kernel/entry-armv.S       |  11 +-
 arch/arm/kernel/head-common.S      |   4 +-
 arch/arm/kernel/process.c          |   7 +-
 arch/arm/kernel/traps.c            |   4 +
 10 files changed, 156 insertions(+), 82 deletions(-)
arch/arm/Kconfig +4 -4
index 61fc5cc03042..3d7476ca4d94 100644
@@ -126,8 +126,8 @@ config ARM
 	select PERF_USE_VMALLOC
 	select RTC_LIB
 	select SYS_SUPPORTS_APM_EMULATION
-	select THREAD_INFO_IN_TASK if CURRENT_POINTER_IN_TPIDRURO
-	select HAVE_ARCH_VMAP_STACK if MMU && THREAD_INFO_IN_TASK && (!LD_IS_LLD || LLD_VERSION >= 140000)
+	select THREAD_INFO_IN_TASK
+	select HAVE_ARCH_VMAP_STACK if MMU && (!LD_IS_LLD || LLD_VERSION >= 140000)
 	select TRACE_IRQFLAGS_SUPPORT if !CPU_V7M
 	# Above selects are sorted alphabetically; please add new ones
 	# according to that.  Thanks.
@@ -1169,7 +1169,7 @@ config CURRENT_POINTER_IN_TPIDRURO
 
 config IRQSTACKS
 	def_bool y
-	depends on GENERIC_IRQ_MULTI_HANDLER && THREAD_INFO_IN_TASK
+	depends on GENERIC_IRQ_MULTI_HANDLER
 	select HAVE_IRQ_EXIT_ON_IRQ_STACK
 	select HAVE_SOFTIRQ_ON_OWN_STACK
 
@@ -1619,7 +1619,7 @@ config CC_HAVE_STACKPROTECTOR_TLS
 
 config STACKPROTECTOR_PER_TASK
 	bool "Use a unique stack canary value for each task"
-	depends on STACKPROTECTOR && THREAD_INFO_IN_TASK && !XIP_DEFLATED_DATA
+	depends on STACKPROTECTOR && CURRENT_POINTER_IN_TPIDRURO && !XIP_DEFLATED_DATA
 	depends on GCC_PLUGINS || CC_HAVE_STACKPROTECTOR_TLS
 	select GCC_PLUGIN_ARM_SSP_PER_TASK if !CC_HAVE_STACKPROTECTOR_TLS
 	default y
arch/arm/include/asm/assembler.h +84 -31

133 lines not on this page · raw message

arch/arm/include/asm/current.h +49 -7

87 lines not on this page · raw message

arch/arm/include/asm/switch_to.h +2 -1
index b55c7b2755e4..a482c99934ff 100644
@@ -40,7 +40,8 @@ static inline void set_ti_cpu(struct task_struct *p)
 do {									\
 	__complete_pending_tlbi();					\
 	set_ti_cpu(next);						\
-	if (IS_ENABLED(CONFIG_CURRENT_POINTER_IN_TPIDRURO))		\
+	if (IS_ENABLED(CONFIG_CURRENT_POINTER_IN_TPIDRURO) ||		\
+	    IS_ENABLED(CONFIG_SMP))					\
 		__this_cpu_write(__entry_task, next);			\
 	last = __switch_to(prev,task_thread_info(prev), task_thread_info(next));	\
 } while (0)
arch/arm/include/asm/thread_info.h +0 -27
index 004b89d86224..aecc403b2880 100644
@@ -62,9 +62,6 @@ struct cpu_context_save {
 struct thread_info {
 	unsigned long		flags;		/* low level flags */
 	int			preempt_count;	/* 0 => preemptable, <0 => bug */
-#ifndef CONFIG_THREAD_INFO_IN_TASK
-	struct task_struct	*task;		/* main task structure */
-#endif
 	__u32			cpu;		/* cpu */
 	__u32			cpu_domain;	/* cpu domain */
 	struct cpu_context_save	cpu_context;	/* cpu context */
@@ -80,39 +77,15 @@ struct thread_info {
 
 #define INIT_THREAD_INFO(tsk)						\
 {									\
-	INIT_THREAD_INFO_TASK(tsk)					\
 	.flags		= 0,						\
 	.preempt_count	= INIT_PREEMPT_COUNT,				\
 }
 
-#ifdef CONFIG_THREAD_INFO_IN_TASK
-#define INIT_THREAD_INFO_TASK(tsk)
-
 static inline struct task_struct *thread_task(struct thread_info* ti)
 {
 	return (struct task_struct *)ti;
 }
 
-#else
-#define INIT_THREAD_INFO_TASK(tsk)	.task = &(tsk),
-
-static inline struct task_struct *thread_task(struct thread_info* ti)
-{
-	return ti->task;
-}
-
-/*
- * how to get the thread information struct from C
- */
-static inline struct thread_info *current_thread_info(void) __attribute_const__;
-
-static inline struct thread_info *current_thread_info(void)
-{
-	return (struct thread_info *)
-		(current_stack_pointer & ~(THREAD_SIZE - 1));
-}
-#endif
-
 #define thread_saved_pc(tsk)	\
 	((unsigned long)(task_thread_info(tsk)->cpu_context.pc))
 #define thread_saved_sp(tsk)	\
arch/arm/kernel/asm-offsets.c +0 -3
index 645845e4982a..2c8d76fd7c66 100644
@@ -43,9 +43,6 @@ int main(void)
   BLANK();
   DEFINE(TI_FLAGS,		offsetof(struct thread_info, flags));
   DEFINE(TI_PREEMPT,		offsetof(struct thread_info, preempt_count));
-#ifndef CONFIG_THREAD_INFO_IN_TASK
-  DEFINE(TI_TASK,		offsetof(struct thread_info, task));
-#endif
   DEFINE(TI_CPU,		offsetof(struct thread_info, cpu));
   DEFINE(TI_CPU_DOMAIN,		offsetof(struct thread_info, cpu_domain));
   DEFINE(TI_CPU_SAVE,		offsetof(struct thread_info, cpu_context));
arch/arm/kernel/entry-armv.S +6 -5
index 09a9fe501094..5f3b882d53b7 100644
@@ -816,12 +816,13 @@ ENTRY(__switch_to)
 	switch_tls r1, r4, r5, r3, r7
 #if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_SMP) && \
     !defined(CONFIG_STACKPROTECTOR_PER_TASK)
-	ldr	r9, [r2, #TI_TASK]
 	ldr	r8, =__stack_chk_guard
 	.if (TSK_STACK_CANARY > IMM12_MASK)
-	add	r9, r9, #TSK_STACK_CANARY & ~IMM12_MASK
-	.endif
+	add	r9, r2, #TSK_STACK_CANARY & ~IMM12_MASK
 	ldr	r9, [r9, #TSK_STACK_CANARY & IMM12_MASK]
+	.else
+	ldr	r9, [r2, #TSK_STACK_CANARY & IMM12_MASK]
+	.endif
 #endif
 	mov	r7, r2				@ Preserve 'next'
 #ifdef CONFIG_CPU_USE_DOMAINS
@@ -838,7 +839,7 @@ ENTRY(__switch_to)
 #endif
 	mov	r0, r5
 #if !defined(CONFIG_THUMB2_KERNEL) && !defined(CONFIG_VMAP_STACK)
-	set_current r7
+	set_current r7, r8
 	ldmia	r4, {r4 - sl, fp, sp, pc}	@ Load all regs saved previously
 #else
 	mov	r1, r7
@@ -860,7 +861,7 @@ ENTRY(__switch_to)
 	@ switches us to another stack, with few other side effects. In order
 	@ to prevent this distinction from causing any inconsistencies, let's
 	@ keep the 'set_current' call as close as we can to the update of SP.
-	set_current r1
+	set_current r1, r2
 	mov	sp, ip
 	ret	lr
 #endif
arch/arm/kernel/head-common.S +1 -3
index da18e0a17dc2..42cae73fcc19 100644
@@ -105,10 +105,8 @@ __mmap_switched:
 	mov	r1, #0
 	bl	__memset			@ clear .bss
 
-#ifdef CONFIG_CURRENT_POINTER_IN_TPIDRURO
 	adr_l	r0, init_task			@ get swapper task_struct
-	set_current r0
-#endif
+	set_current r0, r1
 
 	ldmia	r4, {r0, r1, r2, r3}
 	str	r9, [r0]			@ Save processor ID
arch/arm/kernel/process.c +6 -1
index d47159f3791c..0617af11377f 100644
@@ -36,7 +36,7 @@
 
 #include "signal.h"
 
-#ifdef CONFIG_CURRENT_POINTER_IN_TPIDRURO
+#if defined(CONFIG_CURRENT_POINTER_IN_TPIDRURO) || defined(CONFIG_SMP)
 DEFINE_PER_CPU(struct task_struct *, __entry_task);
 #endif
 
@@ -46,6 +46,11 @@ unsigned long __stack_chk_guard __read_mostly;
 EXPORT_SYMBOL(__stack_chk_guard);
 #endif
 
+#ifndef CONFIG_CURRENT_POINTER_IN_TPIDRURO
+asmlinkage struct task_struct *__current;
+EXPORT_SYMBOL(__current);
+#endif
+
 static const char *processor_modes[] __maybe_unused = {
   "USER_26", "FIQ_26" , "IRQ_26" , "SVC_26" , "UK4_26" , "UK5_26" , "UK6_26" , "UK7_26" ,
   "UK8_26" , "UK9_26" , "UK10_26", "UK11_26", "UK12_26", "UK13_26", "UK14_26", "UK15_26",
arch/arm/kernel/traps.c +4 -0
index b28a705c49cb..3f38357efc46 100644
@@ -865,7 +865,9 @@ early_initcall(allocate_overflow_stacks);
 asmlinkage void handle_bad_stack(struct pt_regs *regs)
 {
 	unsigned long tsk_stk = (unsigned long)current->stack;
+#ifdef CONFIG_IRQSTACKS
 	unsigned long irq_stk = (unsigned long)this_cpu_read(irq_stack_ptr);
+#endif
 	unsigned long ovf_stk = (unsigned long)this_cpu_read(overflow_stack_ptr);
 
 	console_verbose();
@@ -873,8 +875,10 @@ asmlinkage void handle_bad_stack(struct pt_regs *regs)
 
 	pr_emerg("Task stack:     [0x%08lx..0x%08lx]\n",
 		 tsk_stk, tsk_stk + THREAD_SIZE);
+#ifdef CONFIG_IRQSTACKS
 	pr_emerg("IRQ stack:      [0x%08lx..0x%08lx]\n",
 		 irq_stk - THREAD_SIZE, irq_stk);
+#endif
 	pr_emerg("Overflow stack: [0x%08lx..0x%08lx]\n",
 		 ovf_stk - OVERFLOW_STACK_SIZE, ovf_stk);
 
Signature · 7 lines
2.30.2


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#7 lore raw

Re: [RFC PATCH 6/6] ARM: implement THREAD_INFO_IN_TASK for uniprocessor systems

arch/arm/Kconfig config CURRENT_POINTER_IN_TPIDRURO · line 1174

Quoted 8 lines · Ard Biesheuvel
arch/arm/Kconfig +1 -1
@@ -1169,7 +1169,7 @@ config CURRENT_POINTER_IN_TPIDRURO
 
 config IRQSTACKS
        def_bool y
-       depends on GENERIC_IRQ_MULTI_HANDLER && THREAD_INFO_IN_TASK
+       depends on GENERIC_IRQ_MULTI_HANDLER
        select HAVE_IRQ_EXIT_ON_IRQ_STACK
        select HAVE_SOFTIRQ_ON_OWN_STACK

Side note: after this, we might want to investigate finishing off GENERIC_IRQ_MULTI_HANDLER for all architectures. The currently missing platforms are ARM_SINGLE_ARMV7M, ARCH_FOOTBRIDGE, ARCH_IOP32X and ARCH_RPC.

These are a bit tricky (presumably this is why they are not converted yet), but it should be possible to do.

arch/arm/include/asm/current.h line 22

Quoted 7 lines
arch/arm/include/asm/current.h +0 -0
 static inline void set_current(struct task_struct *cur)

{

arch/arm/include/asm/current.h +4 -1
-       if (!IS_ENABLED(CONFIG_CURRENT_POINTER_IN_TPIDRURO))
+       if (!IS_ENABLED(CONFIG_CURRENT_POINTER_IN_TPIDRURO) &&
+           !(IS_ENABLED(CONFIG_SMP) &&
+             IS_ENABLED(CONFIG_SMP_ON_UP) &&
+             smp_on_up)) {

I think you can just use is_smp() here to simplify the condition. You might need to move the definition to a different header if that causes an #include loop.

Quoted 7 lines
arch/arm/include/asm/current.h +3 -0
@@ -39,16 +67,30 @@ static inline struct task_struct *get_current(void)
         * https://github.com/ClangBuiltLinux/linux/issues/1485
         */
        cur = __builtin_thread_pointer();
+#elif defined(CONFIG_CURRENT_POINTER_IN_TPIDRURO) || defined(CONFIG_SMP)
+       asm("0: mrc p15, 0, %0, c13, c0, 3                      \n\t"
+#ifdef CONFIG_CPU_V6

arch/arm/include/asm/current.h static inline struct task_struct *get_current(void) · line 82

Quoted 10 lines
arch/arm/include/asm/current.h +10 -0
+           "1:                                                 \n\t"
+           "   .subsection 1                                   \n\t"
+           "2: " LOAD_CURRENT
+           "   b       1b                                      \n\t"
+           "   .previous                                       \n\t"
+           "   .pushsection \".alt.smp.init\", \"a\"           \n\t"
+           "   .long   0b - .                                  \n\t"
+           "   b       . + (2b - 0b)                           \n\t"
+           "   .popsection                                     \n\t"
+#endif

You mentioned earlier that this gets ugly with SMP_ON_UP on ARMv6, now I see what you meant ;-)

I can see an increasing number of reasons for no longer supporting this option. As we recently discussed on IRC, this would affect omap2plus_defconfig, imx_v6_v7_defconfig and realview_defconfig, which would all have to drop either CPU_V6 or SMP. Since you got it working already, this also seems better left as a cleanup for another time once we can build consensus on it, but my guess is that at this point the benefits of removing it outweigh those of keeping it.

Arnd

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

Acked-by on the v1 series
#9 lore raw
In reply to Ard Biesheuvel
Quoted 10 lines · Ard Biesheuvel

Enable the use of the TLS register to hold the 'current' pointer for all configurations that can support it, including non-SMP ones that target v6k or later CPUs, and multi-platform SMP ones that also support v6 based UP systems.

The remaining configurations are all strictly UP, which means we can switch to a global variable to hold the current pointer. By doing this, we can enable THREAD_INFO_IN_TASK, which moves thread info off the stack, protecting it from overflows. It also permits us to enable IRQ stacks and vmap'ed stacks for UP configurations as well.

I really like what I see here!

I glanced over it but sadly do not have sufficient time to read every detail of it, but I certainly trust to to get things right and iron out any rough corners so FWIW:

on this patch set.

Quoted 4 lines

Supporting v6 cores without SMP extensions in SMP configurations (e.g., omap2plus_defconfig or imx_v6_v7_defconfig) makes this a bit tricky, and this is a feature we may consider dropping entirely in the future. But for the time being, we can support this mode as well.

Hmmm yes these will look odd and I can see it really makes the patch hairy too. I think people might be using them though :/

Yours,
Linus Walleij

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

Acked-by on the v1 series
#10 lore raw
In reply to Ard Biesheuvel
Quoted 23 lines · Ard Biesheuvel

Enable the use of the TLS register to hold the 'current' pointer for all configurations that can support it, including non-SMP ones that target v6k or later CPUs, and multi-platform SMP ones that also support v6 based UP systems.

The remaining configurations are all strictly UP, which means we can switch to a global variable to hold the current pointer. By doing this, we can enable THREAD_INFO_IN_TASK, which moves thread info off the stack, protecting it from overflows. It also permits us to enable IRQ stacks and vmap'ed stacks for UP configurations as well.

Supporting v6 cores without SMP extensions in SMP configurations (e.g., omap2plus_defconfig or imx_v6_v7_defconfig) makes this a bit tricky, and this is a feature we may consider dropping entirely in the future. But for the time being, we can support this mode as well.

The accesses to the global variable holding 'current' are constructed in a way that ensures that no literal pool accesses (and associated D-cache misses) are needed unless the access is from a module and module PLTs are enabled. This means that accessing 'current' is just as costly as before, as it used to require some arithmetic involving the stack pointer and a load from the thread_info::task field.

Quoted 4 lines

However, accessing thread_info itself now also involves a load, although it should be noted that all thread_info and current accesses now go via the same variable, which is therefore expected to be hot in the caches at all times.

Looks nice overall.

Quoted 26 lines
Cc Russell King <[email protected]>
Cc Nicolas Pitre <[email protected]>
Cc Arnd Bergmann <[email protected]>
Cc Kees Cook <[email protected]>
Cc Keith Packard <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Tony Lindgren <[email protected]>

Ard Biesheuvel (6):
ARM: entry: preserve thread_info pointer in switch_to
ARM: module: implement support for PC-relative group relocations
ARM: percpu: add SMP_ON_UP support
ARM: smp: defer TPIDRURO update for SMP v6 configurations too
ARM: use TLS register for 'current' on !SMP as well
ARM: implement THREAD_INFO_IN_TASK for uniprocessor systems

 arch/arm/Kconfig                   |  10 +-
 arch/arm/include/asm/assembler.h   | 136 +++++++++++++++-----
 arch/arm/include/asm/current.h     |  56 +++++++-
 arch/arm/include/asm/elf.h         |   3 +
 arch/arm/include/asm/percpu.h      |  29 ++++-
 arch/arm/include/asm/switch_to.h   |   3 +-
 arch/arm/include/asm/thread_info.h |  27 ----
 arch/arm/include/asm/tls.h         |   4 +-
 arch/arm/kernel/asm-offsets.c      |   3 -
Quoted 13 lines
 arch/arm/kernel/entry-armv.S       |  26 ++--
 arch/arm/kernel/entry-header.S     |   8 +-
 arch/arm/kernel/head-common.S      |   4 +-
 arch/arm/kernel/module.c           |  63 +++++++++
 arch/arm/kernel/process.c          |   7 +-
 arch/arm/kernel/traps.c            |   4 +
 arch/arm/mm/Kconfig                |   1 +
 16 files changed, 289 insertions(+), 95 deletions(-)
Signature · 1 line
2.30.2

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#8 lore raw

Re: [RFC PATCH 6/6] ARM: implement THREAD_INFO_IN_TASK for uniprocessor systems

In reply to Arnd Bergmann
Quoted 27 lines · Arnd Bergmann

arch/arm/Kconfig config CURRENT_POINTER_IN_TPIDRURO · line 1174

Quoted 8 lines · Ard Biesheuvel
arch/arm/Kconfig +1 -1
@@ -1169,7 +1169,7 @@ config CURRENT_POINTER_IN_TPIDRURO
 
 config IRQSTACKS
        def_bool y
-       depends on GENERIC_IRQ_MULTI_HANDLER && THREAD_INFO_IN_TASK
+       depends on GENERIC_IRQ_MULTI_HANDLER
        select HAVE_IRQ_EXIT_ON_IRQ_STACK
        select HAVE_SOFTIRQ_ON_OWN_STACK

Side note: after this, we might want to investigate finishing off GENERIC_IRQ_MULTI_HANDLER for all architectures. The currently missing platforms are ARM_SINGLE_ARMV7M, ARCH_FOOTBRIDGE, ARCH_IOP32X and ARCH_RPC.

These are a bit tricky (presumably this is why they are not converted yet), but it should be possible to do.

arch/arm/include/asm/current.h line 22

Quoted 7 lines
arch/arm/include/asm/current.h +0 -0
 static inline void set_current(struct task_struct *cur)

{

arch/arm/include/asm/current.h +4 -1
-       if (!IS_ENABLED(CONFIG_CURRENT_POINTER_IN_TPIDRURO))
+       if (!IS_ENABLED(CONFIG_CURRENT_POINTER_IN_TPIDRURO) &&
+           !(IS_ENABLED(CONFIG_SMP) &&
+             IS_ENABLED(CONFIG_SMP_ON_UP) &&
+             smp_on_up)) {
Quoted 4 lines

I think you can just use is_smp() here to simplify the condition. You might need to move the definition to a different header if that causes an #include loop.

OK

Quoted 21 lines
Quoted 7 lines
arch/arm/include/asm/current.h +3 -0
@@ -39,16 +67,30 @@ static inline struct task_struct *get_current(void)
         * https://github.com/ClangBuiltLinux/linux/issues/1485
         */
        cur = __builtin_thread_pointer();
+#elif defined(CONFIG_CURRENT_POINTER_IN_TPIDRURO) || defined(CONFIG_SMP)
+       asm("0: mrc p15, 0, %0, c13, c0, 3                      \n\t"
+#ifdef CONFIG_CPU_V6

arch/arm/include/asm/current.h static inline struct task_struct *get_current(void) · line 82

Quoted 10 lines
arch/arm/include/asm/current.h +10 -0
+           "1:                                                 \n\t"
+           "   .subsection 1                                   \n\t"
+           "2: " LOAD_CURRENT
+           "   b       1b                                      \n\t"
+           "   .previous                                       \n\t"
+           "   .pushsection \".alt.smp.init\", \"a\"           \n\t"
+           "   .long   0b - .                                  \n\t"
+           "   b       . + (2b - 0b)                           \n\t"
+           "   .popsection                                     \n\t"
+#endif

You mentioned earlier that this gets ugly with SMP_ON_UP on ARMv6, now I see what you meant ;-)

Quoted 8 lines

I can see an increasing number of reasons for no longer supporting this option. As we recently discussed on IRC, this would affect omap2plus_defconfig, imx_v6_v7_defconfig and realview_defconfig, which would all have to drop either CPU_V6 or SMP. Since you got it working already, this also seems better left as a cleanup for another time once we can build consensus on it, but my guess is that at this point the benefits of removing it outweigh those of keeping it.

Agreed.

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel