: ARM: Enable THREAD_INFO_IN_TASK
Subsystems (6)
cover : ARM: Enable THREAD_INFO_IN_TASK
Placing thread_info in the kernel stack leaves it vulnerable to stack overflow attacks. This short series addresses that by using the existing THREAD_INFO_IN_TASK infrastructure.
As this is my first patch in this part of the kernel, I'm looking for feedback about the general approach as well as specific comments on places where I've missed something.
I've only run this on armhf running under qemu, so while I've tried to make patches for other code paths, I haven't been able to test those.
(yes, I know checkpatch.pl complains about whitespace in asm-offsets.c, I decided to leave the existing whitespace alone)
PATCH 1/2 ARM: Add per-cpu variable holding cpu number
To help move thread_info into task_struct, stop using the cpu number contained in the thread_info block in C code and use a per-cpu variable instead. This value will be initialized long before the task_struct cpu value for the various idle threads are set, which avoids ordering issues during CPU startup.
--- arch/arm/include/asm/smp.h | 5 ++++- arch/arm/kernel/smp.c | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-)
arch/arm/include/asm/smp.h +4 -1
@@ -15,7 +15,10 @@# error "<asm/smp.h> included in non-SMP build"#endif -#define raw_smp_processor_id () (current_thread_info()->cpu) +#define raw_smp_processor_id () this_cpu_read(cpu_number) +#define __smp_processor_id () __this_cpu_read(cpu_number) + +DECLARE_PER_CPU_READ_MOSTLY(unsigned int, cpu_number);struct seq_file ;
arch/arm/kernel/smp.c +14 -0
@@ -51,6 +51,9 @@#define CREATE_TRACE_POINTS #include <trace/events/ipi.h> +DEFINE_PER_CPU_READ_MOSTLY(unsigned int, cpu_number); +EXPORT_PER_CPU_SYMBOL (cpu_number); +/* * as from 2.5, kernels no longer have an init_tasks structure * so we need some other way of telling a new secondary core
@@ -495,6 +498,7 @@ void __init smp_prepare_boot_cpu(void)void __init smp_prepare_cpus (unsigned int max_cpus) {unsigned int ncores =num_possible_cpus (); +unsigned int cpu;init_cpu_topology ();
@@ -505,6 +509,16 @@ void __init smp_prepare_cpus(unsigned int max_cpus)*/ if (max_cpus > ncores) max_cpus = ncores; + +/* +* Initialize the cpu_number value for each cpu before we +* start it. This ensures that the value is valid during cpu +* initialization, even before the idle task_struct cpu member +* is set +*/ +for_each_possible_cpu (cpu) +per_cpu (cpu_number, cpu) = cpu; +if (ncores >1 && max_cpus) {/* * Initialise the present map, which describes the set of CPUs
Signature · 1 line
2.33.0
PATCH 2/2 ARM: Move thread_info into task_struct
In reply to Keith PackardThis avoids many stack overflow attacks which modified the thread_info structure by moving that into the task_struct as is done is almost all other architectures.
The task_struct is now referenced by a per-cpu variable, 'current_task', allowing the current one on each cpu to be located from both C and assembly.
This also involved removing the 'cpu' member from the thread_info struct and using the one found in the task_struct instead. That is mostly used in asm code, where it might reasonably be replaced with the recent per-cpu 'cpu_number' variable.
--- arch/arm/Kconfig | 1 + arch/arm/include/asm/assembler.h | 63 ++++++++++++++++++++++++------ arch/arm/include/asm/current.h | 36 +++++++++++++++++ arch/arm/include/asm/smp.h | 1 + arch/arm/include/asm/thread_info.h | 14 ------- arch/arm/include/asm/tls.h | 4 +- arch/arm/include/asm/uaccess-asm.h | 6 +-- arch/arm/kernel/asm-offsets.c | 31 ++++++++------- arch/arm/kernel/entry-armv.S | 45 ++++++++++----------- arch/arm/kernel/entry-common.S | 20 +++++----- arch/arm/kernel/entry-v7m.S | 12 +++--- arch/arm/kernel/iwmmxt.S | 14 +++---- arch/arm/kernel/process.c | 1 + arch/arm/kernel/setup.c | 9 ++--- arch/arm/kernel/smp.c | 12 +++++- arch/arm/lib/copy_from_user.S | 4 +- arch/arm/lib/copy_to_user.S | 4 +- arch/arm/mach-ep93xx/crunch-bits.S | 2 +- arch/arm/vfp/entry.S | 4 +- arch/arm/vfp/vfpmodule.c | 29 +++++++------- 20 files changed, 193 insertions(+), 119 deletions(-) create mode 100644 arch/arm/include/asm/current.h
arch/arm/Kconfig +1 -0
@@ -128,6 +128,7 @@ config ARM select RTC_LIB select SET_FS select SYS_SUPPORTS_APM_EMULATION + select THREAD_INFO_IN_TASK # Above selects are sorted alphabetically; please add new ones # according to that. Thanks. help
arch/arm/include/asm/assembler.h +52 -11
84 lines not on this page · raw message
arch/arm/include/asm/current.h new file +36 -0
@@ -0,0 +1,36 @@ +/* +* Copyright © 2021 Keith Packard <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +*/ + +#ifndef _ASM_ARM_CURRENT_H +#define _ASM_ARM_CURRENT_H + +#include <linux/compiler.h> +#include <linux/thread_info.h> +#include <asm/percpu.h> + +#ifndef __ASSEMBLY__ +struct task_struct ; + +DECLARE_PER_CPU(struct task_struct *, current_task); + +static __always_inline struct task_struct *get_current(void) +{ +return raw_cpu_read (current_task); +} + +#define current get_current() + +#endif /* __ASSEMBLY__ */ + +#endif /* _ASM_ARM_CURRENT_H */
arch/arm/include/asm/smp.h +1 -0
@@ -64,6 +64,7 @@ struct secondary_data { };unsigned long swapper_pg_dir;void *stack; +unsigned int cpu; };extern struct secondary_data secondary_data;extern void secondary_startup (void );
arch/arm/include/asm/thread_info.h +0 -14
@@ -55,8 +55,6 @@ struct thread_info {unsigned long flags;/* low level flags */ int preempt_count;/* 0 => preemptable, <0 => bug */ mm_segment_t addr_limit;/* address limit */ -struct task_struct *task;/* main task structure */ -__u32 cpu;/* cpu */ __u32 cpu_domain;/* cpu domain */ #ifdef CONFIG_STACKPROTECTOR_PER_TASK unsigned long stack_canary;
@@ -77,23 +75,11 @@ struct thread_info {#define INIT_THREAD_INFO (tsk) \ { \ - .task = &tsk, \ .flags = 0, \ .preempt_count = INIT_PREEMPT_COUNT, \ .addr_limit = KERNEL_DS, \ } -/* -* 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 )); -} -#define thread_saved_pc (tsk) \ ((unsigned long)(task_thread_info(tsk)->cpu_context.pc))#define thread_saved_sp (tsk) \
arch/arm/include/asm/tls.h +2 -2
@@ -14,7 +14,7 @@ mrc p15, 0, \tmp2, c13, c0, 2 @ get the user r/w register mcr p15, 0, \tp, c13, c0, 3 @ set TLS register mcr p15, 0, \tpuser, c13, c0, 2 @ and the user r/w register - str \tmp2, [\base, #TI_TP_VALUE + 4] @ save it + str \tmp2, [\base, #TSK_TI_TP_VALUE + 4] @ save it .endm .macro switch_tls_v6, base, tp, tpuser, tmp1, tmp2
@@ -26,7 +26,7 @@ mrcne p15, 0, \tmp2, c13, c0, 2 @ get the user r/w register mcrne p15, 0, \tp, c13, c0, 3 @ yes, set TLS register mcrne p15, 0, \tpuser, c13, c0, 2 @ set user r/w register - strne \tmp2, [\base, #TI_TP_VALUE + 4] @ save it + strne \tmp2, [\base, #TSK_TI_TP_VALUE + 4] @ save it .endm .macro switch_tls_software, base, tp, tpuser, tmp1, tmp2
arch/arm/include/asm/uaccess-asm.h +3 -3
@@ -84,9 +84,9 @@* if \disable is set. */ .macro uaccess_entry, tsk, tmp0, tmp1, tmp2, disable - ldr \tmp1, [\tsk, #TI_ADDR_LIMIT] + ldr \tmp1, [\tsk, #TSK_TI_ADDR_LIMIT] ldr \tmp2, =TASK_SIZE - str \tmp2, [\tsk, #TI_ADDR_LIMIT] + str \tmp2, [\tsk, #TSK_TI_ADDR_LIMIT] DACR( mrc p15, 0, \tmp0, c3, c0, 0) DACR( str \tmp0, [sp, #SVC_DACR]) str \tmp1, [sp, #SVC_ADDR_LIMIT]
@@ -108,7 +108,7 @@ .macro uaccess_exit, tsk, tmp0, tmp1 ldr \tmp1, [sp, #SVC_ADDR_LIMIT] DACR( ldr \tmp0, [sp, #SVC_DACR]) - str \tmp1, [\tsk, #TI_ADDR_LIMIT] + str \tmp1, [\tsk, #TSK_TI_ADDR_LIMIT] DACR( mcr p15, 0, \tmp0, c3, c0, 0) .endm
arch/arm/kernel/asm-offsets.c +16 -15
@@ -41,33 +41,34 @@ int main(void)DEFINE (TSK_STACK_CANARY , offsetof(struct task_struct , stack_canary));#endif BLANK (); -DEFINE (TI_FLAGS , offsetof(struct thread_info , flags)); -DEFINE (TI_PREEMPT , offsetof(struct thread_info , preempt_count)); -DEFINE (TI_ADDR_LIMIT , offsetof(struct thread_info , addr_limit)); -DEFINE (TI_TASK , offsetof(struct thread_info , task)); -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)); -DEFINE (TI_USED_CP , offsetof(struct thread_info , used_cp)); -DEFINE (TI_TP_VALUE , offsetof(struct thread_info , tp_value)); -DEFINE (TI_FPSTATE , offsetof(struct thread_info , fpstate)); + DEFINE(TSK_TI_FLAGS, offsetof(struct task_struct, thread_info.flags)); + DEFINE(TSK_TI_PREEMPT, offsetof(struct task_struct, thread_info.preempt_count)); + DEFINE(TSK_TI_ADDR_LIMIT, offsetof(struct task_struct, thread_info.addr_limit)); + DEFINE(TSK_TI_CPU_DOMAIN, offsetof(struct task_struct, thread_info.cpu_domain)); + DEFINE(TSK_TI_CPU_SAVE, offsetof(struct task_struct, thread_info.cpu_context)); + DEFINE(TSK_TI_USED_CP, offsetof(struct task_struct, thread_info.used_cp)); + DEFINE(TSK_TI_TP_VALUE, offsetof(struct task_struct, thread_info.tp_value)); + DEFINE(TSK_TI_FPSTATE, offsetof(struct task_struct, thread_info.fpstate)); +#ifdef CONFIG_SMP +DEFINE (TSK_CPU , offsetof(struct task_struct , cpu)); +#endif #ifdef CONFIG_VFP -DEFINE (TI_VFPSTATE , offsetof(struct thread_info , vfpstate)); + DEFINE(TSK_TI_VFPSTATE, offsetof(struct task_struct, thread_info.vfpstate));#ifdef CONFIG_SMP DEFINE(VFP_CPU, offsetof(union vfp_state, hard.cpu));#endif #endif #ifdef CONFIG_ARM_THUMBEE -DEFINE (TI_THUMBEE_STATE , offsetof(struct thread_info , thumbee_state)); + DEFINE(TSK_TI_THUMBEE_STATE, offsetof(struct task_struct, thread_info.thumbee_state));#endif #ifdef CONFIG_IWMMXT - DEFINE(TI_IWMMXT_STATE, offsetof(struct thread_info, fpstate.iwmmxt)); + DEFINE(TSK_TI_IWMMXT_STATE, offsetof(struct task_struct, thread_info.fpstate.iwmmxt));#endif #ifdef CONFIG_CRUNCH -DEFINE (TI_CRUNCH_STATE , offsetof(struct thread_info , crunchstate)); + DEFINE(TSK_TI_CRUNCH_STATE, offsetof(struct task_struct, thread_info.crunchstate));#endif #ifdef CONFIG_STACKPROTECTOR_PER_TASK -DEFINE (TI_STACK_CANARY , offsetof(struct thread_info , stack_canary)); + DEFINE(TSK_TI_STACK_CANARY, offsetof(struct task_struct, thread_info.stack_canary));#endif DEFINE (THREAD_SZ_ORDER ,THREAD_SIZE_ORDER );BLANK ();
arch/arm/kernel/entry-armv.S +23 -22
141 lines not on this page · raw message
arch/arm/kernel/entry-common.S +10 -10
@@ -49,11 +49,11 @@ __ret_fast_syscall: UNWIND(.fnstart ) UNWIND(.cantunwind ) disable_irq_notrace @ disable interrupts - ldr r2, [tsk, #TI_ADDR_LIMIT] + ldr r2, [tsk, #TSK_TI_ADDR_LIMIT] ldr r1, =TASK_SIZE cmp r2, r1 blne addr_limit_check_failed - ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing + ldr r1, [tsk, #TSK_TI_FLAGS] @ re-check for syscall tracing movs r1, r1, lsl #16 bne fast_work_pending
@@ -87,11 +87,11 @@ __ret_fast_syscall: bl do_rseq_syscall #endif disable_irq_notrace @ disable interrupts - ldr r2, [tsk, #TI_ADDR_LIMIT] + ldr r2, [tsk, #TSK_TI_ADDR_LIMIT] ldr r1, =TASK_SIZE cmp r2, r1 blne addr_limit_check_failed - ldr r1, [tsk, #TI_FLAGS] @ re-check for syscall tracing + ldr r1, [tsk, #TSK_TI_FLAGS] @ re-check for syscall tracing movs r1, r1, lsl #16 beq no_work_pending UNWIND(.fnend )
@@ -129,11 +129,11 @@ ret_slow_syscall: #endif disable_irq_notrace @ disable interrupts ENTRY(ret_to_user_from_irq) - ldr r2, [tsk, #TI_ADDR_LIMIT] + ldr r2, [tsk, #TSK_TI_ADDR_LIMIT] ldr r1, =TASK_SIZE cmp r2, r1 blne addr_limit_check_failed - ldr r1, [tsk, #TI_FLAGS] + ldr r1, [tsk, #TSK_TI_FLAGS] movs r1, r1, lsl #16 bne slow_work_pending no_work_pending:
@@ -156,7 +156,7 @@ ENTRY(ret_from_fork) movne r0, r4 badrne lr, 1f retne r5 -1: get_thread_info tsk +1: get_current tsk, r1 b ret_slow_syscall ENDPROC(ret_from_fork)
@@ -243,7 +243,7 @@ ENTRY(vector_swi) bic scno, scno, #0xff000000 @ mask off SWI op-code eor scno, scno, #__NR_SYSCALL_BASE @ check OS number #endif - get_thread_info tsk + get_current tsk, r10 /* * Reload the registers that may have been corrupted on entry to * the syscall assembly (by tracing or context tracking.)
@@ -251,7 +251,7 @@ ENTRY(vector_swi) TRACE( ldmia sp, {r0 - r3} ) local_restart: - ldr r10, [tsk, #TI_FLAGS] @ check for syscall tracing + ldr r10, [tsk, #TSK_TI_FLAGS] @ check for syscall tracing stmdb sp!, {r4, r5} @ push fifth and sixth args tst r10, #_TIF_SYSCALL_WORK @ are we tracing syscalls?
@@ -278,7 +278,7 @@ local_restart: 9001: sub lr, saved_pc, #4 str lr, [sp, #S_PC] - get_thread_info tsk + get_current tsk, r1 b ret_fast_syscall #endif ENDPROC(vector_swi)
arch/arm/kernel/entry-v7m.S +7 -5
@@ -57,8 +57,8 @@ __irq_entry: tst r0, V7M_SCB_ICSR_RETTOBASE beq 2f - get_thread_info tsk - ldr r2, [tsk, #TI_FLAGS] + get_current tsk, r2 + ldr r2, [tsk, #TSK_TI_FLAGS] movs r2, r2, lsl #16 beq 2f @ no work pending mov r0, #V7M_SCB_ICSR_PENDSVSET
@@ -83,7 +83,7 @@ __pendsv_entry: str r0, [r1, V7M_SCB_ICSR] @ clear PendSV @ execute the pending work, including reschedule - get_thread_info tsk + get_current tsk, why mov why, #0 b ret_to_user_from_irq ENDPROC(__pendsv_entry)
@@ -96,17 +96,19 @@ ENDPROC(__pendsv_entry) ENTRY(__switch_to) .fnstart .cantunwind - add ip, r1, #TI_CPU_SAVE + add ip, r1, #TSK_TI_CPU_SAVE stmia ip!, {r4 - r11} @ Store most regs on stack str sp, [ip], #4 str lr, [ip], #4 mov r5, r0 - add r4, r2, #TI_CPU_SAVE + add r4, r2, #TSK_TI_CPU_SAVE ldr r0, =thread_notify_head mov r1, #THREAD_NOTIFY_SWITCH bl atomic_notifier_call_chain mov ip, r4 mov r0, r5 + ldr r7, [r2, #TSK_TI_TASK] + set_current_task r7, r4, r5 ldmia ip!, {r4 - r11} @ Load all regs saved previously ldr sp, [ip] ldr pc, [ip, #4]!
arch/arm/kernel/iwmmxt.S +7 -7
@@ -84,7 +84,7 @@ ENTRY(iwmmxt_task_enable) PJ4(mcr p15, 0, r2, c1, c0, 2) ldr r3, =concan_owner - add r0, r10, #TI_IWMMXT_STATE @ get task Concan save area + add r0, r10, #TSK_TI_IWMMXT_STATE @ get task Concan save area ldr r2, [sp, #60] @ current task pc value ldr r1, [r3] @ get current Concan owner str r0, [r3] @ this task now owns Concan regs
@@ -96,7 +96,7 @@ ENTRY(iwmmxt_task_enable) bl concan_save #ifdef CONFIG_PREEMPT_COUNT - get_thread_info r10 + get_current r10, r3 #endif 4: dec_preempt_count r10, r3 ret r9 @ normal exit from exception
@@ -199,7 +199,7 @@ ENTRY(iwmmxt_task_disable) msr cpsr_c, r2 ldr r3, =concan_owner - add r2, r0, #TI_IWMMXT_STATE @ get task Concan save area + add r2, r0, #TSK_TI_IWMMXT_STATE @ get task Concan save area ldr r1, [r3] @ get current Concan owner teq r1, #0 @ any current owner? beq 1f @ no: quit
@@ -251,7 +251,7 @@ ENTRY(iwmmxt_task_copy) msr cpsr_c, r2 ldr r3, =concan_owner - add r2, r0, #TI_IWMMXT_STATE @ get task Concan save area + add r2, r0, #TSK_TI_IWMMXT_STATE @ get task Concan save area ldr r3, [r3] @ get current Concan owner teq r2, r3 @ does this task own it... beq 1f
@@ -289,7 +289,7 @@ ENTRY(iwmmxt_task_restore) msr cpsr_c, r2 ldr r3, =concan_owner - add r2, r0, #TI_IWMMXT_STATE @ get task Concan save area + add r2, r0, #TSK_TI_IWMMXT_STATE @ get task Concan save area ldr r3, [r3] @ get current Concan owner bic r2, r2, #0x7 @ 64-bit alignment teq r2, r3 @ does this task own it...
@@ -328,7 +328,7 @@ ENTRY(iwmmxt_task_switch) bne 1f @ yes: block them for next task ldr r2, =concan_owner - add r3, r0, #TI_IWMMXT_STATE @ get next task Concan save area + add r3, r0, #TSK_TI_IWMMXT_STATE @ get next task Concan save area ldr r2, [r2] @ get current Concan owner teq r2, r3 @ next task owns it? retne lr @ no: leave Concan disabled
@@ -355,7 +355,7 @@ ENTRY(iwmmxt_task_release) orr ip, r2, #PSR_I_BIT @ disable interrupts msr cpsr_c, ip ldr r3, =concan_owner - add r0, r0, #TI_IWMMXT_STATE @ get task Concan save area + add r0, r0, #TSK_TI_IWMMXT_STATE @ get task Concan save area ldr r1, [r3] @ get current Concan owner eors r0, r0, r1 @ if equal... streq r0, [r3] @ then clear ownership
arch/arm/kernel/process.c +1 -0
@@ -27,6 +27,7 @@#include <linux/random.h> #include <linux/hw_breakpoint.h> #include <linux/leds.h> +#include <linux/percpu.h> #include <asm/processor.h> #include <asm/thread_notify.h>
arch/arm/kernel/setup.c +3 -6
@@ -532,12 +532,6 @@ void notrace cpu_init(void)BUG (); } -/* -* This only works on resume and secondary cores. For booting on the -* boot cpu, smp_prepare_boot_cpu is called after percpu area setup. -*/ -set_my_cpu_offset (per_cpu_offset (cpu)); -cpu_proc_init ();/*
@@ -733,6 +727,9 @@ static void __init setup_processor(void)elf_hwcap_fixup ();cacheid_init (); + +set_my_cpu_offset (per_cpu_offset (0 )); +cpu_init (); }
arch/arm/kernel/smp.c +10 -2
@@ -51,6 +51,10 @@#define CREATE_TRACE_POINTS #include <trace/events/ipi.h> +DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned = + &init_task; +EXPORT_PER_CPU_SYMBOL (current_task); + DEFINE_PER_CPU_READ_MOSTLY(unsigned int, cpu_number);EXPORT_PER_CPU_SYMBOL (cpu_number);
@@ -156,8 +160,11 @@ int __cpu_up(unsigned int cpu, struct task_struct *idle) secondary_data.pgdir =virt_to_phys (idmap_pgd); secondary_data.swapper_pg_dir =get_arch_pgd (swapper_pg_dir);#endif + secondary_data.cpu = cpu;sync_cache_w (&secondary_data); +per_cpu (current_task, cpu) = idle; +/* * Now bring the CPU into our world. */
@@ -406,7 +413,9 @@ static void smp_store_cpu_info(unsigned int cpuid) asmlinkage void secondary_start_kernel(void) {struct mm_struct *mm = &init_mm; -unsigned int cpu; +unsigned int cpu = secondary_data.cpu; + +set_my_cpu_offset (per_cpu_offset (cpu));secondary_biglittle_init ();
@@ -423,7 +432,6 @@ asmlinkage void secondary_start_kernel(void)* All kernel threads share the same mm context; grab a * reference and switch to it. */ - cpu =smp_processor_id ();mmgrab (mm); current->active_mm = mm;cpumask_set_cpu (cpu,mm_cpumask (mm));
arch/arm/lib/copy_from_user.S +2 -2
@@ -109,8 +109,8 @@ ENTRY(arm_copy_from_user) #ifdef CONFIG_CPU_SPECTRE - get_thread_info r3 - ldr r3, [r3, #TI_ADDR_LIMIT] + get_current r3, ip + ldr r3, [r3, #TSK_TI_ADDR_LIMIT] uaccess_mask_range_ptr r1, r2, r3, ip #endif
arch/arm/lib/copy_to_user.S +2 -2
@@ -109,8 +109,8 @@ ENTRY(__copy_to_user_std) WEAK(arm_copy_to_user) #ifdef CONFIG_CPU_SPECTRE - get_thread_info r3 - ldr r3, [r3, #TI_ADDR_LIMIT] + get_current r3, ip + ldr r3, [r3, #TSK_TI_ADDR_LIMIT] uaccess_mask_range_ptr r0, r2, r3, ip #endif
arch/arm/mach-ep93xx/crunch-bits.S +1 -1
@@ -192,7 +192,7 @@ crunch_load: 1: #ifdef CONFIG_PREEMPT_COUNT - get_thread_info r10 + get_task_info r10, r3 #endif 2: dec_preempt_count r10, r3 ret lr
arch/arm/vfp/entry.S +2 -2
@@ -24,8 +24,8 @@ ENTRY(do_vfp) inc_preempt_count r10, r4 ldr r4, .LCvfp - ldr r11, [r10, #TI_CPU] @ CPU number - add r10, r10, #TI_VFPSTATE @ r10 = workspace + ldr r11, [r10, #TSK_CPU] @ CPU number + add r10, r10, #TSK_TI_VFPSTATE @ r10 = workspace ldr pc, [r4] @ call VFP entry point ENDPROC(do_vfp)
arch/arm/vfp/vfpmodule.c +14 -15
@@ -143,22 +143,22 @@ static void vfp_thread_copy(struct thread_info *thread)* THREAD_NOFTIFY_SWTICH: * - the previously running thread will not be scheduled onto another CPU. * - the next thread to be run (v) will not be running on another CPU. -* - thread->cpu is the local CPU number +* - tsk->cpu is the local CPU number * - not preemptible as we're called in the middle of a thread switch * THREAD_NOTIFY_FLUSH: * - the thread (v) will be running on the local CPU, so -* v === current_thread_info() -* - thread->cpu is the local CPU number at the time it is accessed, +* v === current +* - tsk->cpu is the local CPU number at the time it is accessed, * but may change at any time. * - we could be preempted if tree preempt rcu is enabled, so -* it is unsafe to use thread->cpu. +* it is unsafe to use tsk->cpu. * THREAD_NOTIFY_EXIT * - we could be preempted if tree preempt rcu is enabled, so -* it is unsafe to use thread->cpu. +* it is unsafe to use tsk->cpu. */ static int vfp_notifier (struct notifier_block *self,unsigned long cmd,void *v) { -struct thread_info *thread = v; +struct task_struct *tsk = v;u32 fpexc;#ifdef CONFIG_SMP unsigned int cpu;
@@ -169,7 +169,7 @@ static int vfp_notifier(struct notifier_block *self, unsigned long cmd, void *v) fpexc =fmrx (FPEXC );#ifdef CONFIG_SMP - cpu = thread->cpu; + cpu = tsk->cpu;/* * On SMP, if VFP is enabled, save the old state in
@@ -188,15 +188,15 @@ static int vfp_notifier(struct notifier_block *self, unsigned long cmd, void *v)break ;case THREAD_NOTIFY_FLUSH : -vfp_thread_flush (thread); +vfp_thread_flush (&tsk->thread_info);break ;case THREAD_NOTIFY_EXIT : -vfp_thread_exit (thread); +vfp_thread_exit (&tsk->thread_info);break ;case THREAD_NOTIFY_COPY : -vfp_thread_copy (thread); +vfp_thread_copy (&tsk->thread_info);break ; }
@@ -448,26 +448,25 @@ void __init vfp_disable(void)#ifdef CONFIG_CPU_PM static int vfp_pm_suspend (void ) { -struct thread_info *ti =current_thread_info ();u32 fpexc =fmrx (FPEXC );/* if vfp is on, then save state for resumption */ if (fpexc &FPEXC_EN ) {pr_debug ("%s: saving vfp state\n" , __func__); -vfp_save_state (&ti->vfpstate, fpexc); +vfp_save_state (¤t->thread_info.vfpstate, fpexc);/* disable, just in case */ fmxr (FPEXC ,fmrx (FPEXC ) & ~FPEXC_EN ); - }else if (vfp_current_hw_state[ti->cpu]) { + }else if (vfp_current_hw_state[current->cpu]) {#ifndef CONFIG_SMP fmxr (FPEXC , fpexc |FPEXC_EN ); -vfp_save_state (vfp_current_hw_state[ti->cpu], fpexc); +vfp_save_state (vfp_current_hw_state[current->cpu], fpexc);fmxr (FPEXC , fpexc);#endif }/* clear any information we had about last context state */ - vfp_current_hw_state[ti->cpu] =NULL ; + vfp_current_hw_state[current->cpu] =NULL ;return 0 ; }
Signature · 1 line
2.33.0
Quoted 3 lines · Keith Packard
Placing
thread_infoin the kernel stack leaves it vulnerable to stack overflow attacks. This short series addresses that by using the existingTHREAD_INFO_IN_TASKinfrastructure.
Very cool! Thanks for working on this. If you want, you can refer to the KSPP bug for this too:
https://github.com/KSPP/linux/issues/1
(Anyone want to do MIPS?)
-Kees
Signature · 6 lines
Kees Cook _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Quoted 6 lines · Kees Cook
Quoted 3 lines · Keith Packard
Placing
thread_infoin the kernel stack leaves it vulnerable to stack overflow attacks. This short series addresses that by using the existingTHREAD_INFO_IN_TASKinfrastructure.
Quoted 6 lines
Very cool! Thanks for working on this. If you want, you can refer to the KSPP bug for this too:
https://github.com/KSPP/linux/issues/1(Anyone want to do MIPS?)
I take it this breaks the GCC plugin based per-task stack protector, given that it emits code to mask the stack pointer and apply an offset to the resulting value.
It would be nice if we could replace this with something suitable for THREAD_INFO_IN_TASK, and if it is suitable enough, try and get the GCC/Clang folks to adopt it as well (which was never going to happen for the stack pointer mask/offset approach)
Where can I find these patches? I don't see them on linux-arm-kernel@
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Quoted 14 lines · Ard Biesheuvel
Quoted 6 lines · Kees Cook
Quoted 3 lines · Keith Packard
Placing
thread_infoin the kernel stack leaves it vulnerable to stack overflow attacks. This short series addresses that by using the existingTHREAD_INFO_IN_TASKinfrastructure.Quoted 6 lines
Very cool! Thanks for working on this. If you want, you can refer to the KSPP bug for this too:
https://github.com/KSPP/linux/issues/1(Anyone want to do MIPS?)
Quoted 8 lines
I take it this breaks the GCC plugin based per-task stack protector, given that it emits code to mask the stack pointer and apply an offset to the resulting value.
It would be nice if we could replace this with something suitable for
THREAD_INFO_IN_TASK, and if it is suitable enough, try and get the GCC/Clang folks to adopt it as well (which was never going to happen for the stack pointer mask/offset approach)
I'd love to see the native GCC offset stuff work on arm32, but it's not clear to me how much work that would be. It's implemented for several architectures already. I've tried to capture the matrix here:
https://github.com/KSPP/linux/issues/29
-Kees
Signature · 1 line
Kees Cook
Russell King (Oracle)
maintainerQuoted 8 lines · Kees Cook
Quoted 3 lines · Keith Packard
Placing
thread_infoin the kernel stack leaves it vulnerable to stack overflow attacks. This short series addresses that by using the existingTHREAD_INFO_IN_TASKinfrastructure.Very cool! Thanks for working on this. If you want, you can refer to the KSPP bug for this too:
https://github.com/KSPP/linux/issues/1
Not so fast. It's buggy. I've rejected this "solution" before.
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!
Russell King (Oracle)
maintainerQuoted 8 lines · Keith Packard
Placing
thread_infoin the kernel stack leaves it vulnerable to stack overflow attacks. This short series addresses that by using the existingTHREAD_INFO_IN_TASKinfrastructure.As this is my first patch in this part of the kernel, I'm looking for feedback about the general approach as well as specific comments on places where I've missed something.
Quoted 7 lines
I've only run this on armhf running under qemu, so while I've tried to make patches for other code paths, I haven't been able to test those.
(yes, I know checkpatch.pl complains about whitespace in
asm-offsets.c, I decided to leave the existing whitespace alone)Signed-off-by Keith Packard <[email protected]>
I think you're introducing a circular dependency with this for certain kernel configurations:
E.g. Have you tried running this with CONFIG_CPU_V6 enabled?
+#define raw_smp_processor_id() this_cpu_read(cpu_number) +#define __smp_processor_id() __this_cpu_read(cpu_number) +
+DECLARE_PER_CPU_READ_MOSTLY(unsigned int, cpu_number);
this_cpu_read() is defined as:
#define this_cpu_read(pcp) __pcpu_size_call_return(this_cpu_read_, pcp) (which will call this_cpu_read_4)
#define this_cpu_read_4(pcp) this_cpu_generic_read(pcp)
=> __this_cpu_generic_read_nopreempt()
=> ___ret = READ_ONCE(*raw_cpu_ptr(&(pcp)));
#define raw_cpu_ptr(ptr) \
({ \
__verify_pcpu_ptr(ptr); \
arch_raw_cpu_ptr(ptr); \})
#ifndef arch_raw_cpu_ptr
#define arch_raw_cpu_ptr(ptr) SHIFT_PERCPU_PTR(ptr, __my_cpu_offset) #endif
#ifndef __my_cpu_offset
#define __my_cpu_offset per_cpu_offset(raw_smp_processor_id()) #endif
... which then leads back to a use of raw_smp_processor_id(), thereby creating a circular loop of preprocessor definitions that the compiler can't resolve.
Signature · 2 lines
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/ FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!
Quoted 46 lines · Russell King (Oracle)
Quoted 8 lines · Keith Packard
Placing
thread_infoin the kernel stack leaves it vulnerable to stack overflow attacks. This short series addresses that by using the existingTHREAD_INFO_IN_TASKinfrastructure.As this is my first patch in this part of the kernel, I'm looking for feedback about the general approach as well as specific comments on places where I've missed something.
Quoted 7 lines
I've only run this on armhf running under qemu, so while I've tried to make patches for other code paths, I haven't been able to test those.
(yes, I know checkpatch.pl complains about whitespace in
asm-offsets.c, I decided to leave the existing whitespace alone)Signed-off-by Keith Packard <[email protected]>I think you're introducing a circular dependency with this for certain kernel configurations:
E.g. Have you tried running this with
CONFIG_CPU_V6enabled?(quoted diff) +4
-0+#define raw_smp_processor_id() this_cpu_read(cpu_number) +#define __smp_processor_id() __this_cpu_read(cpu_number) + +DECLARE_PER_CPU_READ_MOSTLY(unsigned int, cpu_number);
this_cpu_read()is defined as:#define
this_cpu_read(pcp)__pcpu_size_call_return(this_cpu_read_, pcp)(which will callthis_cpu_read_4)#define
this_cpu_read_4(pcp)this_cpu_generic_read(pcp)
=>__this_cpu_generic_read_nopreempt()
=>___ret= READ_ONCE(*raw_cpu_ptr(&(pcp)));#define raw_cpu_ptr(ptr) \ ({ \ __verify_pcpu_ptr(ptr); \ arch_raw_cpu_ptr(ptr); \})
#ifndef
arch_raw_cpu_ptr
#definearch_raw_cpu_ptr(ptr)SHIFT_PERCPU_PTR(ptr, __my_cpu_offset)#endif
Quoted 7 lines
#ifndef
__my_cpu_offset
#define__my_cpu_offsetper_cpu_offset(raw_smp_processor_id()) #endif... which then leads back to a use of
raw_smp_processor_id(), thereby creating a circular loop of preprocessor definitions that the compiler can't resolve.
If this isn't easy to fix, perhaps this can be a V7-only feature?
Signature · 1 line
Kees Cook
Quoted 4 lines · Russell King (Oracle)
I think you're introducing a circular dependency with this for certain kernel configurations:
E.g. Have you tried running this with
CONFIG_CPU_V6enabled?
That's very useful feedback -- no, I hadn't ever tried this configuration (presumably the compiler would have complained).
Quoted 1 line
#define
__my_cpu_offsetper_cpu_offset(raw_smp_processor_id())
I've read through the v6 architecture reference manual (again) and cannot find any place to store either a per_cpu_offset or even a small processor id, which I didn't find surprising as I would have expected it to already be used for this purpose.
I'll re-spin the changes making them conditional on !CONFIG_CPU_V6, unless someone has an idea of how to identify the current core in a multi-core ARMv6 system.
Signature · 1 line
-keith
cover ARM: support THREAD_INFO_IN_TASK (v7 only) (v2)
In reply to Keith PackardPlacing thread_info in the kernel stack leaves it vulnerable to stack overflow attacks. This short series addresses that by using the existing THREAD_INFO_IN_TASK infrastructure.
This is the second version of this series, in this version the changes are restricted to v7 hardware which offers a way to identify each cpu in the system without reference to the stack it is using.
The series is broken into three pieces:
1) Change the secondary_start_kernel API to pass the cpu number to this function. This is required for the following patch because the raw_smp_processor_id() macro will use the per_cpu_offset value which needs to have the cpu number to get the right value.
2) Enable THREAD_INFO_IN_TASK by creating a new per-cpu variable, current_task, just like the x86 architecture. The largest changes are in the assembly code where fetching the current_task value requires a temporary register. Fortunately, each location in the code performing this had a reasonably obvious register to use.
3) Optimize access to the cpu number using another new per-cpu variable. This is not functionally necessary, but avoids de-referencing through two pointers at modest memory cost.
PATCH 1/3 ARM: Pass cpu number to secondary_start_kernel
Instead of pulling the CPU value out of the thread_info struct, pass it as an argument. When first initializing secondary processors, this is done by stashing the value in the secondary_data struct. When restarting idle processors, the previous CPU value is passed.
Because the cpu is now known at the top of secondary_start_kernel, the per_cpu_offset value can now be set at this point, instead of in cpu_init where it was also incorrectly setting the per_cpu_offset for the boot processor before that value had been computed.
--- arch/arm/include/asm/smp.h | 3 ++- arch/arm/kernel/head-nommu.S | 1 + arch/arm/kernel/head.S | 1 + arch/arm/kernel/setup.c | 6 ------ arch/arm/kernel/smp.c | 14 +++++++++----- 5 files changed, 13 insertions(+), 12 deletions(-)
arch/arm/include/asm/smp.h +2 -1
@@ -48,7 +48,7 @@ extern void set_smp_ipi_range(int ipi_base, int nr_ipi);* Called from platform specific assembly code, this is the * secondary CPU entry point. */ -asmlinkage void secondary_start_kernel(void); +asmlinkage void secondary_start_kernel(unsigned int cpu);/*
@@ -61,6 +61,7 @@ struct secondary_data { };unsigned long swapper_pg_dir;void *stack; +unsigned int cpu; };extern struct secondary_data secondary_data;extern void secondary_startup (void );
arch/arm/kernel/head-nommu.S +1 -0
@@ -114,6 +114,7 @@ ENTRY(secondary_startup) add r12, r12, r10 ret r12 1: bl __after_proc_init + ldr r0, [r7, #16] @ set up cpu number ldr sp, [r7, #12] @ set up the stack pointer mov fp, #0 b secondary_start_kernel
arch/arm/kernel/head.S +1 -0
@@ -394,6 +394,7 @@ ENDPROC(secondary_startup_arm) ENTRY(__secondary_switched) ldr_l r7, secondary_data + 12 @ get secondary_data.stack + ldr_l r0, secondary_data + 16 @ get secondary_data.cpu mov sp, r7 mov fp, #0 b secondary_start_kernel
arch/arm/kernel/setup.c +0 -6
@@ -532,12 +532,6 @@ void notrace cpu_init(void)BUG (); } -/* -* This only works on resume and secondary cores. For booting on the -* boot cpu, smp_prepare_boot_cpu is called after percpu area setup. -*/ -set_my_cpu_offset (per_cpu_offset (cpu)); -cpu_proc_init ();/*
arch/arm/kernel/smp.c +9 -5
@@ -153,6 +153,7 @@ int __cpu_up(unsigned int cpu, struct task_struct *idle) secondary_data.pgdir =virt_to_phys (idmap_pgd); secondary_data.swapper_pg_dir =get_arch_pgd (swapper_pg_dir);#endif + secondary_data.cpu = cpu;sync_cache_w (&secondary_data);/*
@@ -373,11 +374,14 @@ void arch_cpu_idle_dead(void)* cpu initialisation. There's some initialisation which needs * to be repeated to undo the effects of taking the CPU offline. */ - __asm__("mov sp, %0\n" + __asm__("mov r0, %1\n" +" mov sp, %0\n" " mov fp, #0\n" " b secondary_start_kernel" : - : "r" (task_stack_page(current) + THREAD_SIZE - 8)); + :"r" (task_stack_page (current) +THREAD_SIZE -8 ), +"r" (cpu) + : "r0"); }#endif /* CONFIG_HOTPLUG_CPU */
@@ -400,10 +404,11 @@ static void smp_store_cpu_info(unsigned int cpuid)* This is the secondary CPU boot entry. We're using this CPUs * idle thread stack, but a set of temporary page tables. */ -asmlinkage void secondary_start_kernel(void) +asmlinkage void secondary_start_kernel(unsigned int cpu) {struct mm_struct *mm = &init_mm; -unsigned int cpu; + +set_my_cpu_offset (per_cpu_offset (cpu));secondary_biglittle_init ();
@@ -420,7 +425,6 @@ asmlinkage void secondary_start_kernel(void)* All kernel threads share the same mm context; grab a * reference and switch to it. */ - cpu =smp_processor_id ();mmgrab (mm); current->active_mm = mm;cpumask_set_cpu (cpu,mm_cpumask (mm));
Signature · 1 line
2.33.0
Re: [PATCH 1/3] ARM: Pass cpu number to secondary_start_kernel
Hi Keith,
This looks like a worthwhile cleanup to me regardless of the following patches.
Quoted 6 lines · Keith Packard
Instead of pulling the CPU value out of the
thread_infostruct, pass it as an argument. When first initializing secondary processors, this is done by stashing the value in thesecondary_datastruct. When restarting idle processors, the previous CPU value is passed.
Quoted 6 lines
Because the cpu is now known at the top of
secondary_start_kernel, theper_cpu_offsetvalue can now be set at this point, instead of incpu_initwhere it was also incorrectly setting theper_cpu_offsetfor the boot processor before that value had been computed.Signed-off-by Keith Packard <[email protected]>
Quoted 124 lines
--- arch/arm/include/asm/smp.h | 3 ++- arch/arm/kernel/head-nommu.S | 1 + arch/arm/kernel/head.S | 1 + arch/arm/kernel/setup.c | 6 ------ arch/arm/kernel/smp.c | 14 +++++++++----- 5 files changed, 13 insertions(+), 12 deletions(-)arch/arm/include/asm/smp.h +2
-1@@ -48,7 +48,7 @@ extern void set_smp_ipi_range(int ipi_base, int nr_ipi);* Called from platform specific assembly code, this is the * secondary CPU entry point. */ -asmlinkage void secondary_start_kernel(void); +asmlinkage void secondary_start_kernel(unsigned int cpu);/* @@ -61,6 +61,7 @@ struct secondary_data { };unsigned long swapper_pg_dir;void *stack; +unsigned int cpu; };extern struct secondary_data secondary_data;extern void secondary_startup (void );arch/arm/kernel/head-nommu.S +1
-0@@ -114,6 +114,7 @@ ENTRY(secondary_startup) add r12, r12, r10 ret r12 1: bl __after_proc_init + ldr r0, [r7, #16] @ set up cpu number ldr sp, [r7, #12] @ set up the stack pointer mov fp, #0 b secondary_start_kernelarch/arm/kernel/head.S +1
-0@@ -394,6 +394,7 @@ ENDPROC(secondary_startup_arm) ENTRY(__secondary_switched) ldr_l r7, secondary_data + 12 @ get secondary_data.stack + ldr_l r0, secondary_data + 16 @ get secondary_data.cpu mov sp, r7 mov fp, #0 b secondary_start_kernelarch/arm/kernel/setup.c +0
-6@@ -532,12 +532,6 @@ void notrace cpu_init(void)BUG (); } -/* -* This only works on resume and secondary cores. For booting on the -* boot cpu, smp_prepare_boot_cpu is called after percpu area setup. -*/ -set_my_cpu_offset (per_cpu_offset (cpu)); -cpu_proc_init ();/* arch/arm/kernel/smp.c +9
-6@@ -153,6 +153,7 @@ int __cpu_up(unsigned int cpu, struct task_struct *idle) secondary_data.pgdir =virt_to_phys (idmap_pgd); secondary_data.swapper_pg_dir =get_arch_pgd (swapper_pg_dir);#endif + secondary_data.cpu = cpu;sync_cache_w (&secondary_data);/* @@ -373,11 +374,14 @@ void arch_cpu_idle_dead(void)* cpu initialisation. There's some initialisation which needs * to be repeated to undo the effects of taking the CPU offline. */ - __asm__("mov sp, %0\n" + __asm__("mov r0, %1\n" +" mov sp, %0\n" " mov fp, #0\n" " b secondary_start_kernel" : - : "r" (task_stack_page(current) + THREAD_SIZE - 8)); + :"r" (task_stack_page (current) +THREAD_SIZE -8 ), +"r" (cpu) + : "r0"); }#endif /* CONFIG_HOTPLUG_CPU */ @@ -400,10 +404,11 @@ static void smp_store_cpu_info(unsigned int cpuid)* This is the secondary CPU boot entry. We're using this CPUs * idle thread stack, but a set of temporary page tables. */ -asmlinkage void secondary_start_kernel(void) +asmlinkage void secondary_start_kernel(unsigned int cpu) {struct mm_struct *mm = &init_mm; -unsigned int cpu; + +set_my_cpu_offset (per_cpu_offset (cpu));secondary_biglittle_init ();@@ -420,7 +425,6 @@ asmlinkage void secondary_start_kernel(void)* All kernel threads share the same mm context; grab a * reference and switch to it. */ - cpu =smp_processor_id ();mmgrab (mm); current->active_mm = mm;cpumask_set_cpu (cpu,mm_cpumask (mm)); --2.33.0
PATCH 2/3 ARM: Move thread_info into task_struct (v7 only)
In reply to Keith PackardThis avoids many stack overflow attacks which modified the thread_info structure by moving that into the task_struct as is done is almost all other architectures.
The task_struct is now referenced by a per-cpu variable, 'current_task', allowing the current one on each cpu to be located from both C and assembly.
This also involved removing the 'cpu' member from the thread_info struct and using the one added to the task_struct instead by the THREAD_INFO_IN_TASK code.
This code is currently enabled only for v7 hardware as other ARM architectures do not make the c13 register in the p15 co-processor available for general OS use, leaving identifying the current cpu to the 'cpu' field in the thread_info located off the stack pointer.
--- arch/arm/Kconfig | 1 + arch/arm/Makefile | 8 ++++ arch/arm/include/asm/assembler.h | 66 ++++++++++++++++++++++++++---- arch/arm/include/asm/current.h | 41 +++++++++++++++++++ arch/arm/include/asm/smp.h | 18 ++++++++ arch/arm/include/asm/thread_info.h | 17 ++++++++ arch/arm/kernel/asm-offsets.c | 4 ++ arch/arm/kernel/entry-armv.S | 17 ++++---- arch/arm/kernel/entry-common.S | 6 +-- arch/arm/kernel/entry-v7m.S | 7 +++- arch/arm/kernel/iwmmxt.S | 2 +- arch/arm/kernel/smp.c | 12 ++++++ arch/arm/lib/copy_from_user.S | 2 +- arch/arm/lib/copy_to_user.S | 2 +- arch/arm/mach-ep93xx/crunch-bits.S | 2 +- arch/arm/mm/proc-macros.S | 2 +- arch/arm/vfp/entry.S | 4 ++ arch/arm/vfp/vfpmodule.c | 25 +++++++---- 18 files changed, 204 insertions(+), 32 deletions(-) create mode 100644 arch/arm/include/asm/current.h
arch/arm/Kconfig +1 -0
@@ -128,6 +128,7 @@ config ARM select RTC_LIB select SET_FS select SYS_SUPPORTS_APM_EMULATION + select THREAD_INFO_IN_TASK if CPU_V7 # Above selects are sorted alphabetically; please add new ones # according to that. Thanks. help
arch/arm/Makefile +8 -0
@@ -284,6 +284,14 @@ stack_protector_prepare: prepare0 $(eval GCC_PLUGINS_CFLAGS += $(SSP_PLUGIN_CFLAGS)) endif +ifdef CONFIG_SMP +prepare: task_cpu_prepare + +PHONY += task_cpu_prepare +task_cpu_prepare: prepare0 + $(eval KBUILD_CFLAGS += -D_TSK_CPU=$(shell awk '{if ($$2 == "TSK_CPU") print $$3;}' include/generated/asm-offsets.h)) +endif + all: $(notdir $(KBUILD_IMAGE))
arch/arm/include/asm/assembler.h +59 -7
82 lines not on this page · raw message
arch/arm/include/asm/current.h new file +41 -0
41 lines not on this page · raw message
arch/arm/include/asm/smp.h +18 -0
@@ -15,7 +15,25 @@# error "<asm/smp.h> included in non-SMP build"#endif +#ifdef CONFIG_THREAD_INFO_IN_TASK +/* +* This is particularly ugly: it appears we can't actually get the definition +* of task_struct here, but we need access to the CPU this task is running on. +* Instead of using task_struct we're using TSK_CPU which is extracted from +* asm-offsets.h by kbuild to get the current processor ID. +* +* This also needs to be safeguarded when building asm-offsets.s because at +* that time TSK_CPU is not defined yet. +*/ +#ifndef _TSK_CPU +#define raw_smp_processor_id () (0) +#else +#define raw_smp_processor_id () (*(unsigned int *)((void *)current + _TSK_CPU)) +#endif + +#else #define raw_smp_processor_id () (current_thread_info()->cpu) +#endif struct seq_file ;
arch/arm/include/asm/thread_info.h +17 -0
@@ -55,8 +55,10 @@ struct thread_info {unsigned long flags;/* low level flags */ int preempt_count;/* 0 => preemptable, <0 => bug */ mm_segment_t addr_limit;/* address limit */ +#ifndef CONFIG_THREAD_INFO_IN_TASK struct task_struct *task;/* main task structure */ __u32 cpu;/* cpu */ +#endif __u32 cpu_domain;/* cpu domain */ #ifdef CONFIG_STACKPROTECTOR_PER_TASK unsigned long stack_canary;
@@ -75,6 +77,17 @@ struct thread_info {#endif }; +#ifdef CONFIG_THREAD_INFO_IN_TASK + +#define INIT_THREAD_INFO (tsk) \ +{ \ + .flags = 0, \ + .preempt_count = INIT_PREEMPT_COUNT, \ + .addr_limit = KERNEL_DS, \ +} + +#else +#define INIT_THREAD_INFO (tsk) \ { \ .task = &tsk, \
@@ -83,6 +96,9 @@ struct thread_info { .addr_limit = KERNEL_DS, \ } +#endif + +#ifndef CONFIG_THREAD_INFO_IN_TASK /* * how to get the thread information struct from C */
@@ -93,6 +109,7 @@ 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))
arch/arm/kernel/asm-offsets.c +4 -0
@@ -44,8 +44,12 @@ int main(void)DEFINE (TI_FLAGS , offsetof(struct thread_info , flags));DEFINE (TI_PREEMPT , offsetof(struct thread_info , preempt_count));DEFINE (TI_ADDR_LIMIT , offsetof(struct thread_info , addr_limit)); +#ifdef CONFIG_THREAD_INFO_IN_TASK +DEFINE (TSK_CPU , offsetof(struct task_struct , cpu)); +#else DEFINE (TI_TASK , offsetof(struct thread_info , task));DEFINE (TI_CPU , offsetof(struct thread_info , cpu)); +#endif DEFINE (TI_CPU_DOMAIN , offsetof(struct thread_info , cpu_domain));DEFINE (TI_CPU_SAVE , offsetof(struct thread_info , cpu_context));DEFINE (TI_USED_CP , offsetof(struct thread_info , used_cp));
arch/arm/kernel/entry-armv.S +10 -7
@@ -179,7 +179,7 @@ ENDPROC(__und_invalid) @ stmia r7, {r2 - r6} - get_thread_info tsk + get_current tsk, r0 uaccess_entry tsk, r0, r1, r2, \uaccess .if \trace
@@ -260,7 +260,7 @@ __und_svc: bl __und_fault __und_svc_finish: - get_thread_info tsk + get_current tsk, r5 ldr r5, [sp, #S_PSR] @ Get SVC cpsr svc_exit r5 @ return from exception UNWIND(.fnend )
@@ -428,7 +428,7 @@ __irq_usr: usr_entry kuser_cmpxchg_check irq_handler - get_thread_info tsk + get_current tsk, why mov why, #0 b ret_to_user_from_irq UNWIND(.fnend )
@@ -572,12 +572,12 @@ ENDPROC(__und_usr) @ Fall-through from Thumb-2 __und_usr @ #ifdef CONFIG_NEON - get_thread_info r10 @ get current thread + get_current r10, r6 @ get current thread adr r6, .LCneon_thumb_opcodes b 2f #endif call_fpe: - get_thread_info r10 @ get current thread + get_current r10, r6 @ get current thread #ifdef CONFIG_NEON adr r6, .LCneon_arm_opcodes 2: ldr r5, [r6], #4 @ mask value
@@ -722,7 +722,7 @@ __pabt_usr: ENTRY(ret_from_exception) UNWIND(.fnstart ) UNWIND(.cantunwind ) - get_thread_info tsk + get_current tsk, why mov why, #0 b ret_to_user UNWIND(.fnend )
@@ -735,7 +735,7 @@ __fiq_usr: kuser_cmpxchg_check mov r0, sp @ struct pt_regs *regs bl handle_fiq_as_nmi - get_thread_info tsk + get_current tsk, r0 restore_user_regs fast = 0, offset = 0 UNWIND(.fnend ) ENDPROC(__fiq_usr)
@@ -771,6 +771,9 @@ ENTRY(__switch_to) #endif #ifdef CONFIG_CPU_USE_DOMAINS mcr p15, 0, r6, c3, c0, 0 @ Set domain register +#endif +#ifdef CONFIG_THREAD_INFO_IN_TASK + set_current_task r2, r4, r5 #endif mov r5, r0 add r4, r2, #TI_CPU_SAVE
arch/arm/kernel/entry-common.S +3 -3
@@ -156,7 +156,7 @@ ENTRY(ret_from_fork) movne r0, r4 badrne lr, 1f retne r5 -1: get_thread_info tsk +1: get_current tsk, r1 b ret_slow_syscall ENDPROC(ret_from_fork)
@@ -243,7 +243,7 @@ ENTRY(vector_swi) bic scno, scno, #0xff000000 @ mask off SWI op-code eor scno, scno, #__NR_SYSCALL_BASE @ check OS number #endif - get_thread_info tsk + get_current tsk, r10 /* * Reload the registers that may have been corrupted on entry to * the syscall assembly (by tracing or context tracking.)
@@ -278,7 +278,7 @@ local_restart: 9001: sub lr, saved_pc, #4 str lr, [sp, #S_PC] - get_thread_info tsk + get_current tsk, r1 b ret_fast_syscall #endif ENDPROC(vector_swi)
arch/arm/kernel/entry-v7m.S +5 -2
@@ -57,7 +57,7 @@ __irq_entry: tst r0, V7M_SCB_ICSR_RETTOBASE beq 2f - get_thread_info tsk + get_current tsk, r2 ldr r2, [tsk, #TI_FLAGS] movs r2, r2, lsl #16 beq 2f @ no work pending
@@ -83,7 +83,7 @@ __pendsv_entry: str r0, [r1, V7M_SCB_ICSR] @ clear PendSV @ execute the pending work, including reschedule - get_thread_info tsk + get_current tsk, why mov why, #0 b ret_to_user_from_irq ENDPROC(__pendsv_entry)
@@ -107,6 +107,9 @@ ENTRY(__switch_to) bl atomic_notifier_call_chain mov ip, r4 mov r0, r5 +#ifdef CONFIG_THREAD_INFO_IN_TASK + set_current_task r2, r4, r5 +#endif ldmia ip!, {r4 - r11} @ Load all regs saved previously ldr sp, [ip] ldr pc, [ip, #4]!
arch/arm/kernel/iwmmxt.S +1 -1
@@ -96,7 +96,7 @@ ENTRY(iwmmxt_task_enable) bl concan_save #ifdef CONFIG_PREEMPT_COUNT - get_thread_info r10 + get_current r10, r3 #endif 4: dec_preempt_count r10, r3 ret r9 @ normal exit from exception
arch/arm/kernel/smp.c +12 -0
@@ -51,6 +51,13 @@#define CREATE_TRACE_POINTS #include <trace/events/ipi.h> +#ifdef CONFIG_THREAD_INFO_IN_TASK +DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned = + &init_task; +EXPORT_PER_CPU_SYMBOL (current_task); + +#endif +/* * as from 2.5, kernels no longer have an init_tasks structure * so we need some other way of telling a new secondary core
@@ -156,6 +163,10 @@ int __cpu_up(unsigned int cpu, struct task_struct *idle) secondary_data.cpu = cpu;sync_cache_w (&secondary_data); +#ifdef CONFIG_THREAD_INFO_IN_TASK +per_cpu (current_task, cpu) = idle; +#endif +/* * Now bring the CPU into our world. */
@@ -509,6 +520,7 @@ void __init smp_prepare_cpus(unsigned int max_cpus)*/ if (max_cpus > ncores) max_cpus = ncores; +if (ncores >1 && max_cpus) {/* * Initialise the present map, which describes the set of CPUs
arch/arm/lib/copy_from_user.S +1 -1
@@ -109,7 +109,7 @@ ENTRY(arm_copy_from_user) #ifdef CONFIG_CPU_SPECTRE - get_thread_info r3 + get_current r3, ip ldr r3, [r3, #TI_ADDR_LIMIT] uaccess_mask_range_ptr r1, r2, r3, ip #endif
arch/arm/lib/copy_to_user.S +1 -1
@@ -109,7 +109,7 @@ ENTRY(__copy_to_user_std) WEAK(arm_copy_to_user) #ifdef CONFIG_CPU_SPECTRE - get_thread_info r3 + get_current r3, ip ldr r3, [r3, #TI_ADDR_LIMIT] uaccess_mask_range_ptr r0, r2, r3, ip #endif
arch/arm/mach-ep93xx/crunch-bits.S +1 -1
@@ -192,7 +192,7 @@ crunch_load: 1: #ifdef CONFIG_PREEMPT_COUNT - get_thread_info r10 + get_current r10, r3 #endif 2: dec_preempt_count r10, r3 ret lr
arch/arm/mm/proc-macros.S +1 -1
@@ -30,7 +30,7 @@ * act_mm - get current->active_mm */ .macro act_mm, rd - get_thread_info \rd + get_current \rd, unused @ won't build if THREAD_INFO_IN_TASK ldr \rd, [\rd, #TI_TASK] .if (TSK_ACTIVE_MM > IMM12_MASK) add \rd, \rd, #TSK_ACTIVE_MM & ~IMM12_MASK
arch/arm/vfp/entry.S +4 -0
@@ -24,7 +24,11 @@ ENTRY(do_vfp) inc_preempt_count r10, r4 ldr r4, .LCvfp +#ifdef CONFIG_THREAD_INFO_IN_TASK + ldr r11, [r10, #TSK_CPU] @ CPU number +#else ldr r11, [r10, #TI_CPU] @ CPU number +#endif add r10, r10, #TI_VFPSTATE @ r10 = workspace ldr pc, [r4] @ call VFP entry point ENDPROC(do_vfp)
arch/arm/vfp/vfpmodule.c +17 -8
@@ -143,22 +143,27 @@ static void vfp_thread_copy(struct thread_info *thread)* THREAD_NOFTIFY_SWTICH: * - the previously running thread will not be scheduled onto another CPU. * - the next thread to be run (v) will not be running on another CPU. -* - thread->cpu is the local CPU number +* - tsk->cpu is the local CPU number * - not preemptible as we're called in the middle of a thread switch * THREAD_NOTIFY_FLUSH: * - the thread (v) will be running on the local CPU, so -* v === current_thread_info() -* - thread->cpu is the local CPU number at the time it is accessed, +* v === current +* - tsk->cpu is the local CPU number at the time it is accessed, * but may change at any time. * - we could be preempted if tree preempt rcu is enabled, so -* it is unsafe to use thread->cpu. +* it is unsafe to use tsk->cpu. * THREAD_NOTIFY_EXIT * - we could be preempted if tree preempt rcu is enabled, so -* it is unsafe to use thread->cpu. +* it is unsafe to use tsk->cpu. */ static int vfp_notifier (struct notifier_block *self,unsigned long cmd,void *v) { +#ifdef CONFIG_THREAD_INFO_IN_TASK +struct task_struct *tsk = v; +struct thread_info *thread = &tsk->thread_info; +#else struct thread_info *thread = v; +#endif u32 fpexc;#ifdef CONFIG_SMP unsigned int cpu;
@@ -169,7 +174,11 @@ static int vfp_notifier(struct notifier_block *self, unsigned long cmd, void *v) fpexc =fmrx (FPEXC );#ifdef CONFIG_SMP +#ifdef CONFIG_THREAD_INFO_IN_TASK + cpu = tsk->cpu; +#else cpu = thread->cpu; +#endif /* * On SMP, if VFP is enabled, save the old state in
@@ -458,16 +467,16 @@ static int vfp_pm_suspend(void)/* disable, just in case */ fmxr (FPEXC ,fmrx (FPEXC ) & ~FPEXC_EN ); - }else if (vfp_current_hw_state[ti->cpu]) { + }else if (vfp_current_hw_state[smp_processor_id ()]) {#ifndef CONFIG_SMP fmxr (FPEXC , fpexc |FPEXC_EN ); -vfp_save_state (vfp_current_hw_state[ti->cpu], fpexc); +vfp_save_state (vfp_current_hw_state[smp_processor_id ()], fpexc);fmxr (FPEXC , fpexc);#endif }/* clear any information we had about last context state */ - vfp_current_hw_state[ti->cpu] =NULL ; + vfp_current_hw_state[smp_processor_id ()] =NULL ;return 0 ; }
Signature · 1 line
2.33.0
Re: [PATCH 2/3] ARM: Move thread_info into task_struct (v7 only)
Quoted 9 lines · Keith Packard
This avoids many stack overflow attacks which modified the
thread_infostructure by moving that into thetask_structas is done is almost all other architectures.The
task_structis now referenced by a per-cpu variable, 'current_task', allowing the current one on each cpu to be located from both C and assembly.
Quoted 9 lines
This also involved removing the 'cpu' member from the
thread_infostruct and using the one added to thetask_structinstead by theTHREAD_INFO_IN_TASKcode.This code is currently enabled only for v7 hardware as other ARM architectures do not make the c13 register in the p15 co-processor available for general OS use, leaving identifying the current cpu to the 'cpu' field in the
thread_infolocated off the stack pointer.
c13 is not a register, it is a value in one of the dimensions of the ARM sysreg space, and probably covers other system registers that pre-v7 cores do implement.
Better to use its architectural name (TPIDRPRW) and clarify that pre-v6k/v7 cores simply don't implement it.
Quoted 14 lines
Signed-off-by Keith Packard <[email protected]>--- arch/arm/Kconfig | 1 + arch/arm/Makefile | 8 ++++ arch/arm/include/asm/assembler.h | 66 ++++++++++++++++++++++++++---- arch/arm/include/asm/current.h | 41 +++++++++++++++++++ arch/arm/include/asm/smp.h | 18 ++++++++ arch/arm/include/asm/thread_info.h | 17 ++++++++ arch/arm/kernel/asm-offsets.c | 4 ++ arch/arm/kernel/entry-armv.S | 17 ++++---- arch/arm/kernel/entry-common.S | 6 +-- arch/arm/kernel/entry-v7m.S | 7 +++- arch/arm/kernel/iwmmxt.S | 2 +- arch/arm/kernel/smp.c | 12 ++++++
Quoted 9 lines
arch/arm/lib/copy_from_user.S | 2 +- arch/arm/lib/copy_to_user.S | 2 +- arch/arm/mach-ep93xx/crunch-bits.S | 2 +- arch/arm/mm/proc-macros.S | 2 +- arch/arm/vfp/entry.S | 4 ++ arch/arm/vfp/vfpmodule.c | 25 +++++++---- 18 files changed, 204 insertions(+), 32 deletions(-) create mode 100644 arch/arm/include/asm/current.h
Could we split this up?
arch/arm/Kconfig config ARM · line 131
Quoted 9 lines
arch/arm/Kconfig +1
-0@@ -128,6 +128,7 @@ config ARM select RTC_LIB select SET_FS select SYS_SUPPORTS_APM_EMULATION + select THREAD_INFO_IN_TASK if CPU_V7
CPU_V6K also supports this
Quoted 11 lines
arch/arm/Kconfig +0
-0# Above selects are sorted alphabetically; please add new ones # according to that. Thanks. helparch/arm/Makefile +0
-0@@ -284,6 +284,14 @@ stack_protector_prepare: prepare0 $(eval GCC_PLUGINS_CFLAGS += $(SSP_PLUGIN_CFLAGS)) endif
arch/arm/Makefile stack_protector_prepare: prepare0 · line 296
Quoted 10 lines
arch/arm/Makefile +8
-0+ifdef CONFIG_SMP +prepare: task_cpu_prepare + +PHONY += task_cpu_prepare +task_cpu_prepare: prepare0 + $(eval KBUILD_CFLAGS += -D_TSK_CPU=$(shell awk '{if ($$2 == "TSK_CPU") print $$3;}' include/generated/asm-offsets.h)) +endif + all: $(notdir $(KBUILD_IMAGE))
This is rather horrid, and removed again in the next patch. Can we omit it entirely?
arch/arm/include/asm/assembler.h line 202
Quoted 10 lines
arch/arm/include/asm/assembler.h +1
-0@@ -199,16 +199,68 @@ .endm .endr +#ifdef CONFIG_THREAD_INFO_IN_TASK
No need for this #ifdef - it only guards macros that will have no effect if they are never instantiated (another case below)
Quoted 87 lines
arch/arm/include/asm/assembler.h +58
-7/* -* Get current thread_info. +* Get per-CPU offset +* rd: Destination register */ - .macro get_thread_info, rd - ARM( mov \rd, sp, lsr #THREAD_SIZE_ORDER + PAGE_SHIFT ) - THUMB( mov \rd, sp ) - THUMB( lsr \rd, \rd, #THREAD_SIZE_ORDER + PAGE_SHIFT ) - mov \rd, \rd, lsl #THREAD_SIZE_ORDER + PAGE_SHIFT + .macro this_cpu_offset rd:req + mrc p15, 0, \rd, c13, c0, 4 .endm +/* +* Load a per-cpu variable +* @dst: Destination register to receive value +* @sym: The name of the per-cpu variable +* @tmp: scratch register +*/ + .macro ldr_this_cpu dst : req, sym : req, tmp : req + movw \dst, #:lower16:\sym + movt \dst, #:upper16:\sym + this_cpu_offset \tmp + ldr \dst, [\dst, \tmp] + .endm + +/* + * Store a value in a per-cpu variable + * @src: Source register with value + * @sym: The name of the per-cpu variable + * @t1: scratch register 1 + * @t2: scratch register 2 + */ + .macro str_this_cpu src : req, sym : req, t1 : req, t2 : req + movw \t1, #:lower16:\sym + movt \t1, #:upper16:\sym + this_cpu_offset \t2 + str \src, [\t1, \t2] + .endm +#endif + +/* + * Get current thread_info + * @dst: Destination register to receive current thread info + * @tmp: scratch register + */ + .macro get_current, dst : req, tmp : req +#ifdef CONFIG_THREAD_INFO_IN_TASK + ldr_this_cpu \dst, current_task, \tmp +#else + ARM( mov \dst, sp, lsr #THREAD_SIZE_ORDER + PAGE_SHIFT ) + THUMB( mov \dst, sp ) + THUMB( lsr \dst, \dst, #THREAD_SIZE_ORDER + PAGE_SHIFT ) + mov \dst, \dst, lsl #THREAD_SIZE_ORDER + PAGE_SHIFT +#endif + .endm + +#ifdef CONFIG_THREAD_INFO_IN_TASK +/* + * Set current task info + */ + .macro set_current_task rs : req, t1 : req, t2 : req + str_this_cpu \rs, current_task, \t1, \t2 + .endm +#endif +/* * Increment/decrement the preempt count. */ @@ -226,7 +278,7 @@ .endm .macro dec_preempt_count_ti, ti, tmp - get_thread_info \ti + get_current \ti, \tmp dec_preempt_count \ti, \tmp .endm #elsearch/arm/include/asm/current.h new file +2
-0@@ -0,0 +1,41 @@ +/* +* Copyright © 2021 Keith Packard <[email protected]>
arch/arm/include/asm/current.h line 12
Quoted 10 lines
arch/arm/include/asm/current.h new file +10
-0+* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details.
Please drop the boilerplate and use a SPDX header instead.
Quoted 9 lines
arch/arm/include/asm/current.h new file +9
-0+*/ + +#ifndef _ASM_ARM_CURRENT_H +#define _ASM_ARM_CURRENT_H + +#include <linux/compiler.h> +#include <linux/thread_info.h> +#include <asm/percpu.h> +
arch/arm/include/asm/current.h line 31
Quoted 10 lines
arch/arm/include/asm/current.h new file +10
-0+#ifndef __ASSEMBLY__ + +#ifdef CONFIG_THREAD_INFO_IN_TASK +struct task_struct; + +DECLARE_PER_CPU(struct task_struct *, current_task); + +static __always_inline struct task_struct *get_current(void) +{ + return raw_cpu_read(current_task);
This needs to run with preemption disabled, or we might get migrated to another CPU halfway through, and produce the wrong result (i.e., the current task of the CPU we migrated from). However, it appears that manipulating the preempt count will create a circular dependency here.
Instead of using a per-CPU variable for current, I think it might be better to borrow another system register (TPIDRURO or TPIDRURW) to carry 'current' when THREAD_INFO_IN_TASK is in effect, similar to how arm64 uses SP_EL0 - those registers could be preserved/restored in the entry/exit from/to user space paths rather than in the context switch path, and then be used any way we like while running in the kernel.
Quoted 435 lines
arch/arm/include/asm/current.h new file +10
-0+} + +#define current get_current() +#else +#include <asm-generic/current.h> +#endif + +#endif /* __ASSEMBLY__ */ + +#endif /* _ASM_ARM_CURRENT_H */ arch/arm/include/asm/smp.h +18
-0@@ -15,7 +15,25 @@# error "<asm/smp.h> included in non-SMP build"#endif +#ifdef CONFIG_THREAD_INFO_IN_TASK +/* +* This is particularly ugly: it appears we can't actually get the definition +* of task_struct here, but we need access to the CPU this task is running on. +* Instead of using task_struct we're using TSK_CPU which is extracted from +* asm-offsets.h by kbuild to get the current processor ID. +* +* This also needs to be safeguarded when building asm-offsets.s because at +* that time TSK_CPU is not defined yet. +*/ +#ifndef _TSK_CPU +#define raw_smp_processor_id () (0) +#else +#define raw_smp_processor_id () (*(unsigned int *)((void *)current + _TSK_CPU)) +#endif + +#else #define raw_smp_processor_id () (current_thread_info()->cpu) +#endif struct seq_file ;arch/arm/include/asm/thread_info.h +17
-0@@ -55,8 +55,10 @@ struct thread_info {unsigned long flags;/* low level flags */ int preempt_count;/* 0 => preemptable, <0 => bug */ mm_segment_t addr_limit;/* address limit */ +#ifndef CONFIG_THREAD_INFO_IN_TASK struct task_struct *task;/* main task structure */ __u32 cpu;/* cpu */ +#endif __u32 cpu_domain;/* cpu domain */ #ifdef CONFIG_STACKPROTECTOR_PER_TASK unsigned long stack_canary;@@ -75,6 +77,17 @@ struct thread_info {#endif }; +#ifdef CONFIG_THREAD_INFO_IN_TASK + +#define INIT_THREAD_INFO (tsk) \ +{ \ + .flags = 0, \ + .preempt_count = INIT_PREEMPT_COUNT, \ + .addr_limit = KERNEL_DS, \ +} + +#else +#define INIT_THREAD_INFO (tsk) \ { \ .task = &tsk, \@@ -83,6 +96,9 @@ struct thread_info { .addr_limit = KERNEL_DS, \ } +#endif + +#ifndef CONFIG_THREAD_INFO_IN_TASK /* * how to get the thread information struct from C */ @@ -93,6 +109,7 @@ 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))arch/arm/kernel/asm-offsets.c +4
-0@@ -44,8 +44,12 @@ int main(void)DEFINE (TI_FLAGS , offsetof(struct thread_info , flags));DEFINE (TI_PREEMPT , offsetof(struct thread_info , preempt_count));DEFINE (TI_ADDR_LIMIT , offsetof(struct thread_info , addr_limit)); +#ifdef CONFIG_THREAD_INFO_IN_TASK +DEFINE (TSK_CPU , offsetof(struct task_struct , cpu)); +#else DEFINE (TI_TASK , offsetof(struct thread_info , task));DEFINE (TI_CPU , offsetof(struct thread_info , cpu)); +#endif DEFINE (TI_CPU_DOMAIN , offsetof(struct thread_info , cpu_domain));DEFINE (TI_CPU_SAVE , offsetof(struct thread_info , cpu_context));DEFINE (TI_USED_CP , offsetof(struct thread_info , used_cp));arch/arm/kernel/entry-armv.S +10
-7@@ -179,7 +179,7 @@ ENDPROC(__und_invalid) @ stmia r7, {r2 - r6} - get_thread_info tsk + get_current tsk, r0 uaccess_entry tsk, r0, r1, r2, \uaccess .if \trace@@ -260,7 +260,7 @@ __und_svc: bl __und_fault __und_svc_finish: - get_thread_info tsk + get_current tsk, r5 ldr r5, [sp, #S_PSR] @ Get SVC cpsr svc_exit r5 @ return from exception UNWIND(.fnend )@@ -428,7 +428,7 @@ __irq_usr: usr_entry kuser_cmpxchg_check irq_handler - get_thread_info tsk + get_current tsk, why mov why, #0 b ret_to_user_from_irq UNWIND(.fnend )@@ -572,12 +572,12 @@ ENDPROC(__und_usr) @ Fall-through from Thumb-2 __und_usr @ #ifdef CONFIG_NEON - get_thread_info r10 @ get current thread + get_current r10, r6 @ get current thread adr r6, .LCneon_thumb_opcodes b 2f #endif call_fpe: - get_thread_info r10 @ get current thread + get_current r10, r6 @ get current thread #ifdef CONFIG_NEON adr r6, .LCneon_arm_opcodes 2: ldr r5, [r6], #4 @ mask value@@ -722,7 +722,7 @@ __pabt_usr: ENTRY(ret_from_exception) UNWIND(.fnstart ) UNWIND(.cantunwind ) - get_thread_info tsk + get_current tsk, why mov why, #0 b ret_to_user UNWIND(.fnend )@@ -735,7 +735,7 @@ __fiq_usr: kuser_cmpxchg_check mov r0, sp @ struct pt_regs *regs bl handle_fiq_as_nmi - get_thread_info tsk + get_current tsk, r0 restore_user_regs fast = 0, offset = 0 UNWIND(.fnend ) ENDPROC(__fiq_usr)@@ -771,6 +771,9 @@ ENTRY(__switch_to) #endif #ifdef CONFIG_CPU_USE_DOMAINS mcr p15, 0, r6, c3, c0, 0 @ Set domain register +#endif +#ifdef CONFIG_THREAD_INFO_IN_TASK + set_current_task r2, r4, r5 #endif mov r5, r0 add r4, r2, #TI_CPU_SAVEarch/arm/kernel/entry-common.S +3
-3@@ -156,7 +156,7 @@ ENTRY(ret_from_fork) movne r0, r4 badrne lr, 1f retne r5 -1: get_thread_info tsk +1: get_current tsk, r1 b ret_slow_syscall ENDPROC(ret_from_fork)@@ -243,7 +243,7 @@ ENTRY(vector_swi) bic scno, scno, #0xff000000 @ mask off SWI op-code eor scno, scno, #__NR_SYSCALL_BASE @ check OS number #endif - get_thread_info tsk + get_current tsk, r10 /* * Reload the registers that may have been corrupted on entry to * the syscall assembly (by tracing or context tracking.)@@ -278,7 +278,7 @@ local_restart: 9001: sub lr, saved_pc, #4 str lr, [sp, #S_PC] - get_thread_info tsk + get_current tsk, r1 b ret_fast_syscall #endif ENDPROC(vector_swi)arch/arm/kernel/entry-v7m.S +5
-2@@ -57,7 +57,7 @@ __irq_entry: tst r0, V7M_SCB_ICSR_RETTOBASE beq 2f - get_thread_info tsk + get_current tsk, r2 ldr r2, [tsk, #TI_FLAGS] movs r2, r2, lsl #16 beq 2f @ no work pending@@ -83,7 +83,7 @@ __pendsv_entry: str r0, [r1, V7M_SCB_ICSR] @ clear PendSV @ execute the pending work, including reschedule - get_thread_info tsk + get_current tsk, why mov why, #0 b ret_to_user_from_irq ENDPROC(__pendsv_entry)@@ -107,6 +107,9 @@ ENTRY(__switch_to) bl atomic_notifier_call_chain mov ip, r4 mov r0, r5 +#ifdef CONFIG_THREAD_INFO_IN_TASK + set_current_task r2, r4, r5 +#endif ldmia ip!, {r4 - r11} @ Load all regs saved previously ldr sp, [ip] ldr pc, [ip, #4]!arch/arm/kernel/iwmmxt.S +1
-1@@ -96,7 +96,7 @@ ENTRY(iwmmxt_task_enable) bl concan_save #ifdef CONFIG_PREEMPT_COUNT - get_thread_info r10 + get_current r10, r3 #endif 4: dec_preempt_count r10, r3 ret r9 @ normal exit from exceptionarch/arm/kernel/smp.c +12
-0@@ -51,6 +51,13 @@#define CREATE_TRACE_POINTS #include <trace/events/ipi.h> +#ifdef CONFIG_THREAD_INFO_IN_TASK +DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned = + &init_task; +EXPORT_PER_CPU_SYMBOL (current_task); + +#endif +/* * as from 2.5, kernels no longer have an init_tasks structure * so we need some other way of telling a new secondary core @@ -156,6 +163,10 @@ int __cpu_up(unsigned int cpu, struct task_struct *idle) secondary_data.cpu = cpu;sync_cache_w (&secondary_data); +#ifdef CONFIG_THREAD_INFO_IN_TASK +per_cpu (current_task, cpu) = idle; +#endif +/* * Now bring the CPU into our world. */ @@ -509,6 +520,7 @@ void __init smp_prepare_cpus(unsigned int max_cpus)*/ if (max_cpus > ncores) max_cpus = ncores; +if (ncores >1 && max_cpus) {/* * Initialise the present map, which describes the set of CPUs arch/arm/lib/copy_from_user.S +1
-1@@ -109,7 +109,7 @@ ENTRY(arm_copy_from_user) #ifdef CONFIG_CPU_SPECTRE - get_thread_info r3 + get_current r3, ip ldr r3, [r3, #TI_ADDR_LIMIT] uaccess_mask_range_ptr r1, r2, r3, ip #endifarch/arm/lib/copy_to_user.S +1
-1@@ -109,7 +109,7 @@ ENTRY(__copy_to_user_std) WEAK(arm_copy_to_user) #ifdef CONFIG_CPU_SPECTRE - get_thread_info r3 + get_current r3, ip ldr r3, [r3, #TI_ADDR_LIMIT] uaccess_mask_range_ptr r0, r2, r3, ip #endifarch/arm/mach-ep93xx/crunch-bits.S +1
-1@@ -192,7 +192,7 @@ crunch_load: 1: #ifdef CONFIG_PREEMPT_COUNT - get_thread_info r10 + get_current r10, r3 #endif 2: dec_preempt_count r10, r3 ret lrarch/arm/mm/proc-macros.S +1
-1@@ -30,7 +30,7 @@ * act_mm - get current->active_mm */ .macro act_mm, rd - get_thread_info \rd + get_current \rd, unused @ won't build if THREAD_INFO_IN_TASK ldr \rd, [\rd, #TI_TASK] .if (TSK_ACTIVE_MM > IMM12_MASK) add \rd, \rd, #TSK_ACTIVE_MM & ~IMM12_MASKarch/arm/vfp/entry.S +4
-0@@ -24,7 +24,11 @@ ENTRY(do_vfp) inc_preempt_count r10, r4 ldr r4, .LCvfp +#ifdef CONFIG_THREAD_INFO_IN_TASK + ldr r11, [r10, #TSK_CPU] @ CPU number +#else ldr r11, [r10, #TI_CPU] @ CPU number +#endif add r10, r10, #TI_VFPSTATE @ r10 = workspace ldr pc, [r4] @ call VFP entry point ENDPROC(do_vfp)arch/arm/vfp/vfpmodule.c +17
-9@@ -143,22 +143,27 @@ static void vfp_thread_copy(struct thread_info *thread)* THREAD_NOFTIFY_SWTICH: * - the previously running thread will not be scheduled onto another CPU. * - the next thread to be run (v) will not be running on another CPU. -* - thread->cpu is the local CPU number +* - tsk->cpu is the local CPU number * - not preemptible as we're called in the middle of a thread switch * THREAD_NOTIFY_FLUSH: * - the thread (v) will be running on the local CPU, so -* v === current_thread_info() -* - thread->cpu is the local CPU number at the time it is accessed, +* v === current +* - tsk->cpu is the local CPU number at the time it is accessed, * but may change at any time. * - we could be preempted if tree preempt rcu is enabled, so -* it is unsafe to use thread->cpu. +* it is unsafe to use tsk->cpu. * THREAD_NOTIFY_EXIT * - we could be preempted if tree preempt rcu is enabled, so -* it is unsafe to use thread->cpu. +* it is unsafe to use tsk->cpu. */ static int vfp_notifier (struct notifier_block *self,unsigned long cmd,void *v) { +#ifdef CONFIG_THREAD_INFO_IN_TASK +struct task_struct *tsk = v; +struct thread_info *thread = &tsk->thread_info; +#else struct thread_info *thread = v; +#endif u32 fpexc;#ifdef CONFIG_SMP unsigned int cpu;@@ -169,7 +174,11 @@ static int vfp_notifier(struct notifier_block *self, unsigned long cmd, void *v) fpexc =fmrx (FPEXC );#ifdef CONFIG_SMP +#ifdef CONFIG_THREAD_INFO_IN_TASK + cpu = tsk->cpu; +#else cpu = thread->cpu; +#endif /* * On SMP, if VFP is enabled, save the old state in @@ -458,16 +467,16 @@ static int vfp_pm_suspend(void)/* disable, just in case */ fmxr (FPEXC ,fmrx (FPEXC ) & ~FPEXC_EN ); - }else if (vfp_current_hw_state[ti->cpu]) { + }else if (vfp_current_hw_state[smp_processor_id ()]) {#ifndef CONFIG_SMP fmxr (FPEXC , fpexc |FPEXC_EN ); -vfp_save_state (vfp_current_hw_state[ti->cpu], fpexc); +vfp_save_state (vfp_current_hw_state[smp_processor_id ()], fpexc);fmxr (FPEXC , fpexc);#endif }/* clear any information we had about last context state */ - vfp_current_hw_state[ti->cpu] =NULL ; + vfp_current_hw_state[smp_processor_id ()] =NULL ;return 0 ; } --2.33.0
Re: [PATCH 2/3] ARM: Move thread_info into task_struct (v7 only)
Quoted 6 lines · Ard Biesheuvel
c13 is not a register, it is a value in one of the dimensions of the ARM sysreg space, and probably covers other system registers that pre-v7 cores do implement.
Better to use its architectural name (TPIDRPRW) and clarify that pre-v6k/v7 cores simply don't implement it.
Thanks, I'll reword the commit message
Quoted 1 line
Could we split this up?
I could split up the assembler macro changes which add a temporary register to the calls getting the current thread_info/task_struct value? If that would help get this reviewed, I'd be happy to do that. Otherwise, it's pretty much all-or-nothing as enabling THREAD_INFO_IN_TASK requires a bunch of synchronized changes.
Quoted 4 lines
Quoted 1 line
+#ifdef
CONFIG_THREAD_INFO_IN_TASKNo need for this #ifdef - it only guards macros that will have no effect if they are never instantiated (another case below)
Sure, the resulting code is a bit less noisy, which seems good.
Quoted 11 lines
arch/arm/include/asm/current.h line 31
Quoted 5 lines
arch/arm/include/asm/current.h new file +5
-0+DECLARE_PER_CPU(struct task_struct *, current_task); + +static __always_inline struct task_struct *get_current(void) +{ +return raw_cpu_read (current_task);This needs to run with preemption disabled, or we might get migrated to another CPU halfway through, and produce the wrong result (i.e., the current task of the CPU we migrated from). However, it appears that manipulating the preempt count will create a circular dependency here.
Yeah, I really don't understand the restrictions of this API well. Any code fetching the current task pointer better not be subject to preemption or that value will be stale at some point after it was computed. Do you know if it could ever be run in a context allowing preemption?
Quoted 7 lines
Instead of using a per-CPU variable for current, I think it might be better to borrow another system register (TPIDRURO or TPIDRURW) to carry 'current' when
THREAD_INFO_IN_TASKis in effect, similar to how arm64 usesSP_EL0- those registers could be preserved/restored in the entry/exit from/to user space paths rather than in the context switch path, and then be used any way we like while running in the kernel.
Making sure these values don't leak through to user space somehow? I'm not excited by that prospect at all. But, perhaps someone can help me understand the conditions under which the current task value can be computed where preemption was enabled and have that not be a problem for the enclosing code?
Signature · 1 line
-keith
Re: [PATCH 2/3] ARM: Move thread_info into task_struct (v7 only)
Quoted 12 lines · Keith Packard
Quoted 6 lines · Ard Biesheuvel
c13 is not a register, it is a value in one of the dimensions of the ARM sysreg space, and probably covers other system registers that pre-v7 cores do implement.
Better to use its architectural name (TPIDRPRW) and clarify that pre-v6k/v7 cores simply don't implement it.
Thanks, I'll reword the commit message
Quoted 8 lines
Quoted 1 line
Could we split this up?
I could split up the assembler macro changes which add a temporary register to the calls getting the current thread_info/task_struct value? If that would help get this reviewed, I'd be happy to do that. Otherwise, it's pretty much all-or-nothing as enabling
THREAD_INFO_IN_TASKrequires a bunch of synchronized changes.
Sure, so it is precisely for that reason that it is better to isolate changes that can be isolated.
...
Quoted 12 lines
Quoted 11 lines
arch/arm/include/asm/current.h line 31
Quoted 5 lines
arch/arm/include/asm/current.h new file +5
-0+DECLARE_PER_CPU(struct task_struct *, current_task); + +static __always_inline struct task_struct *get_current(void) +{ +return raw_cpu_read (current_task);This needs to run with preemption disabled, or we might get migrated to another CPU halfway through, and produce the wrong result (i.e., the current task of the CPU we migrated from). However, it appears that manipulating the preempt count will create a circular dependency here.
Quoted 5 lines
Yeah, I really don't understand the restrictions of this API well. Any code fetching the current task pointer better not be subject to preemption or that value will be stale at some point after it was computed. Do you know if it could ever be run in a context allowing preemption?
All the time. 'current' essentially never changes value from the POV of code running in task context, so there is usually no reason to care about preemption/migration when referring to it. Using per-CPU variables is what creates the problem here.
Quoted 10 lines
Quoted 7 lines
Instead of using a per-CPU variable for current, I think it might be better to borrow another system register (TPIDRURO or TPIDRURW) to carry 'current' when
THREAD_INFO_IN_TASKis in effect, similar to how arm64 usesSP_EL0- those registers could be preserved/restored in the entry/exit from/to user space paths rather than in the context switch path, and then be used any way we like while running in the kernel.Making sure these values don't leak through to user space somehow? I'm not excited by that prospect at all.
Moving the code that pokes the right user space value into these registers from the context switch patch to the user space exit path should be rather straight-forward, so I am not too concerned about security issues here (famous last words)
Quoted 5 lines
But, perhaps someone can help me
understand the conditions under which the current task value can be computed where preemption was enabled and have that not be a problem for the enclosing code?
In principle, it should be sufficient to run the per-CPU variable load sequence with preemption disabled. For instance, your asm version
movw \dst, #:lower16:\sym
movt \dst, #:upper16:\sym
this_cpu_offset \tmp
ldr \dst, [\dst, \tmp]must not be preempted and migrated right before the ldr instruction, because that would end up accessing another CPU's value for 'current'.
However, the preempt_count itself is stored in thread_info as well, so I'm not sure there is a way we can safely disable preemption for this sequence to begin with, unless we run the above with interrupts disabled.
Given that we are already relying on the MP extensions for this anyway, I personally think that using another thread ID register to carry 'current' is a reasonable approach as well, since it would also allow us to get per-task stack protector support into the compiler. But I would like to hear from some other folks on cc as well.
Re: [PATCH 2/3] ARM: Move thread_info into task_struct (v7 only)
Quoted 2 lines · Ard Biesheuvel
Sure, so it is precisely for that reason that it is better to isolate changes that can be isolated.
I'll go ahead and split this apart then; that is how I did development, after all.
Quoted 4 lines
All the time. 'current' essentially never changes value from the POV of code running in task context, so there is usually no reason to care about preemption/migration when referring to it. Using per-CPU variables is what creates the problem here.
Thanks for helping me -- I just got the wrong model stuck in my head over the weekend somehow.
If I do have this figured out, we should be able to stick the per_cpu_offset value in thread_info and use TPIDRPRW to hold 'current' as code using per_cpu_offset should already be disabling preemption. That should be an easier change than putting a kernel pointer in a user-visible register.
Quoted 5 lines
Given that we are already relying on the MP extensions for this anyway, I personally think that using another thread ID register to carry 'current' is a reasonable approach as well, since it would also allow us to get per-task stack protector support into the compiler. But I would like to hear from some other folks on cc as well.
That would be awesome; I assume that doesn't require leaving per_cpu_offset in a thread ID register?
In any case, I'll give my plan a try, and then see about trying your plan as well so I can compare the complexity of the two solutions.
Signature · 1 line
-keith
Re: [PATCH 2/3] ARM: Move thread_info into task_struct (v7 only)
Quoted 14 lines · Keith Packard
Quoted 2 lines · Ard Biesheuvel
Sure, so it is precisely for that reason that it is better to isolate changes that can be isolated.
I'll go ahead and split this apart then; that is how I did development, after all.
Quoted 4 lines
All the time. 'current' essentially never changes value from the POV of code running in task context, so there is usually no reason to care about preemption/migration when referring to it. Using per-CPU variables is what creates the problem here.
Quoted 9 lines
Thanks for helping me -- I just got the wrong model stuck in my head over the weekend somehow.
If I do have this figured out, we should be able to stick the
per_cpu_offsetvalue inthread_infoand use TPIDRPRW to hold 'current' as code usingper_cpu_offsetshould already be disabling preemption. That should be an easier change than putting a kernel pointer in a user-visible register.
That is still a bit tricky, given that you now have to swap out the per-CPU offset when you migrate a task, and the generic code is not really set up for that.
Quoted 6 lines
Quoted 5 lines
Given that we are already relying on the MP extensions for this anyway, I personally think that using another thread ID register to carry 'current' is a reasonable approach as well, since it would also allow us to get per-task stack protector support into the compiler. But I would like to hear from some other folks on cc as well.
Quoted 6 lines
That would be awesome; I assume that doesn't require leaving
per_cpu_offsetin a thread ID register?In any case, I'll give my plan a try, and then see about trying your plan as well so I can compare the complexity of the two solutions.
I had a stab at this myself today (long boring day with no Internet connection).
https://android-kvm.googlesource.com/linux/+log/refs/heads/ardb/arm32-ti-in-task
It resembles your code in some places - I suppose we went on the same journey in a sense :-) We'll fix up the credits before this gets resubmitted.
Fixing the per-task stack protector plugin on top of these changes should be trivial but I need a coffee first.
Re: [PATCH 2/3] ARM: Move thread_info into task_struct (v7 only)
Quoted 3 lines · Ard Biesheuvel
That is still a bit tricky, given that you now have to swap out the per-CPU offset when you migrate a task, and the generic code is not really set up for that.
Yeah, I decided to just leverage as much generic code as possible and re-purposed the TPIDRPRW register for 'current', leaving the per_cpu_offset data in memory. That makes the patch series pretty compact, with a bunch of restructuring changes followed by fairly short functional changes.
Quoted 7 lines
I had a stab at this myself today (long boring day with no Internet connection).
https://android-kvm.googlesource.com/linux/+log/refs/heads/ardb/arm32-ti-in-task
It resembles your code in some places - I suppose we went on the same journey in a sense :-) We'll fix up the credits before this gets resubmitted.
This does look great, and preserves the optimization of keeping the per_cpu_offset value in a register.
With the series I posted, getting the per_cpu_offset value requires fetching the cpu value from task_struct and then using that to index the per_cpu_offset arrray. I don't have a good feeling of how important this optimization is? We could stick the per_cpu_offset value into thread_info to avoid one fetch?
Quoted 2 lines
Fixing the per-task stack protector plugin on top of these changes should be trivial but I need a coffee first.
I haven't even looked at that step yet :-)
Signature · 1 line
-keith
Re: [PATCH 2/3] ARM: Move thread_info into task_struct (v7 only)
In reply to Ard BiesheuvelArd Biesheuvel <[email protected]> writes:
(sorry, missed a couple of comments, and also neglected to thank you for your review!)
Quoted 11 lines
arch/arm/Kconfig config ARM · line 131
Quoted 9 lines
arch/arm/Kconfig +1
-0@@ -128,6 +128,7 @@ config ARM select RTC_LIB select SET_FS select SYS_SUPPORTS_APM_EMULATION + select THREAD_INFO_IN_TASK if CPU_V7
CPU_V6Kalso supports this
I've only tested on V7; help getting testing for V6K so we could enable that as well would be greatly appreciated.
Quoted 8 lines
Quoted 8 lines
arch/arm/Makefile +0
-0@@ -284,6 +284,14 @@ stack_protector_prepare: prepare0 $(eval GCC_PLUGINS_CFLAGS += $(SSP_PLUGIN_CFLAGS)) endif
Quoted 13 lines
arch/arm/Makefile stack_protector_prepare: prepare0 · line 296
Quoted 10 lines
arch/arm/Makefile +8
-0+ifdef CONFIG_SMP +prepare: task_cpu_prepare + +PHONY += task_cpu_prepare +task_cpu_prepare: prepare0 + $(eval KBUILD_CFLAGS += -D_TSK_CPU=$(shell awk '{if ($$2 == "TSK_CPU") print $$3;}' include/generated/asm-offsets.h)) +endif + all: $(notdir $(KBUILD_IMAGE))This is rather horrid, and removed again in the next patch. Can we omit it entirely?
Yeah, this did get rather ugly -- I wanted to use the task_struct in the raw_smp_processor_id() macro, but discovered that I couldn't include linux/sched.h in that file.
I found this lovely hack in the powerpc kernel code and just lifted it from there. In terms of code changes, this one is "smaller" than including the new per-cpu variable, "cpu_number", but in terms of ugliness, it's hard to argue which is cleaner.
I was going for fewest logical changes in structure with this patch instead of cleanest result to try and make it slightly easier to review.
Happy to squash these two patches together if you'd prefer; I initially wrote the code with the cpu_number variable but then discovered that I didn't need it if I used the cpu value in the task_struct...
Signature · 1 line
-keith
PATCH 3/3 ARM: Add per-cpu variable cpu_number (v7 only)
In reply to Keith PackardHolds the cpu value for each cpu to make accessing this variable more efficient than fetching the current task struct and pulling the cpu value from there.
This code is only enabled when THREAD_INFO_IN_TASK is selected, which is currently only enabled for v7 hardware.
--- arch/arm/Makefile | 8 -------- arch/arm/include/asm/smp.h | 17 +++-------------- arch/arm/kernel/smp.c | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 22 deletions(-)
arch/arm/Makefile +0 -8
@@ -284,14 +284,6 @@ stack_protector_prepare: prepare0 $(eval GCC_PLUGINS_CFLAGS += $(SSP_PLUGIN_CFLAGS)) endif -ifdef CONFIG_SMP -prepare: task_cpu_prepare - -PHONY += task_cpu_prepare -task_cpu_prepare: prepare0 - $(eval KBUILD_CFLAGS += -D_TSK_CPU=$(shell awk '{if ($$2 == "TSK_CPU") print $$3;}' include/generated/asm-offsets.h)) -endif - all: $(notdir $(KBUILD_IMAGE))
arch/arm/include/asm/smp.h +3 -14
@@ -16,21 +16,10 @@#endif #ifdef CONFIG_THREAD_INFO_IN_TASK -/* -* This is particularly ugly: it appears we can't actually get the definition -* of task_struct here, but we need access to the CPU this task is running on. -* Instead of using task_struct we're using TSK_CPU which is extracted from -* asm-offsets.h by kbuild to get the current processor ID. -* -* This also needs to be safeguarded when building asm-offsets.s because at -* that time TSK_CPU is not defined yet. -*/ -#ifndef _TSK_CPU -#define raw_smp_processor_id () (0) -#else -#define raw_smp_processor_id () (*(unsigned int *)((void *)current + _TSK_CPU)) -#endif +#define raw_smp_processor_id () this_cpu_read(cpu_number) +#define __smp_processor_id () __this_cpu_read(cpu_number) +DECLARE_PER_CPU_READ_MOSTLY(unsigned int, cpu_number);#else #define raw_smp_processor_id () (current_thread_info()->cpu)#endif
arch/arm/kernel/smp.c +16 -0
@@ -56,6 +56,8 @@ DEFINE_PER_CPU(struct task_struct *, current_task) ____cacheline_aligned = &init_task;EXPORT_PER_CPU_SYMBOL (current_task); +DEFINE_PER_CPU_READ_MOSTLY(unsigned int, cpu_number); +EXPORT_PER_CPU_SYMBOL (cpu_number);#endif /*
@@ -510,6 +512,9 @@ void __init smp_prepare_boot_cpu(void)void __init smp_prepare_cpus (unsigned int max_cpus) {unsigned int ncores =num_possible_cpus (); +#ifdef CONFIG_THREAD_INFO_IN_TASK +unsigned int cpu; +#endif init_cpu_topology ();
@@ -521,6 +526,17 @@ void __init smp_prepare_cpus(unsigned int max_cpus)if (max_cpus > ncores) max_cpus = ncores; +#ifdef CONFIG_THREAD_INFO_IN_TASK +/* +* Initialize the cpu_number value for each cpu before we +* start it. This ensures that the value is valid during cpu +* initialization, even before the idle task_struct cpu member +* is set +*/ +for_each_possible_cpu (cpu) +per_cpu (cpu_number, cpu) = cpu; +#endif +if (ncores >1 && max_cpus) {/* * Initialise the present map, which describes the set of CPUs
Signature · 1 line
2.33.0
cover ARM: support THREAD_INFO_IN_TASK (v3)
In reply to Keith PackardPlacing thread_info in the kernel stack leaves it vulnerable to stack overflow attacks. This short series addresses that by using the existing THREAD_INFO_IN_TASK infrastructure.
This is the third version of this series, in this version the changes are restricted to hardware which provides the TPIDRPRW register. This register is repurposed from holding the per_cpu_offset value to holding the 'current' value as that allows fetching this value atomically so that it can be used in a preemptable context.
The series is broken into seven pieces:
1) Change the secondary_start_kernel API to receive the cpu number. This avoids needing to be able to find this value independently in future patches.
2) Change the secondary_start_kernel API to also receive the 'task' value. Passing the value to this function also avoids needing to be able to discover it independently.
3) A cleanup which avoids assuming that THREAD_INFO_IN_TASK is not set.
4) A hack, borrowed from the powerpc arch, which allows locating the 'cpu' field in either thread_info or task_struct, without requiring linux/sched.h to be included in asm/smp.h
5) Disable the optimization storing per_cpu_offset in TPIDRPRW. This leaves the register free to hold 'current' instead.
6) Use TPIDRPRW for 'current'. This is enabled for either CPU_V6K or CPU_V7, but not if CPU_V6 is also enabled.
7) Enable THREAD_INFO_IN_TASK whenever TPIDRPRW is used to hold 'current'.
PATCH 1/7 ARM: Pass cpu number to secondary_start_kernel
Instead of pulling the CPU value out of the thread_info struct, pass it as an argument. When first initializing secondary processors, this is done by stashing the value in the secondary_data struct. When restarting idle processors, the previous CPU value is passed.
Because the cpu is now known at the top of secondary_start_kernel, the per_cpu_offset value can now be set at this point, instead of in cpu_init where it was also incorrectly setting the per_cpu_offset for the boot processor before that value had been computed.
--- arch/arm/include/asm/smp.h | 3 ++- arch/arm/kernel/head-nommu.S | 1 + arch/arm/kernel/head.S | 1 + arch/arm/kernel/setup.c | 6 ------ arch/arm/kernel/smp.c | 14 +++++++++----- 5 files changed, 13 insertions(+), 12 deletions(-)
arch/arm/include/asm/smp.h +2 -1
@@ -48,7 +48,7 @@ extern void set_smp_ipi_range(int ipi_base, int nr_ipi);* Called from platform specific assembly code, this is the * secondary CPU entry point. */ -asmlinkage void secondary_start_kernel(void); +asmlinkage void secondary_start_kernel(unsigned int cpu);/*
@@ -61,6 +61,7 @@ struct secondary_data { };unsigned long swapper_pg_dir;void *stack; +unsigned int cpu; };extern struct secondary_data secondary_data;extern void secondary_startup (void );
arch/arm/kernel/head-nommu.S +1 -0
@@ -114,6 +114,7 @@ ENTRY(secondary_startup) add r12, r12, r10 ret r12 1: bl __after_proc_init + ldr r0, [r7, #16] @ set up cpu number ldr sp, [r7, #12] @ set up the stack pointer mov fp, #0 b secondary_start_kernel
arch/arm/kernel/head.S +1 -0
@@ -394,6 +394,7 @@ ENDPROC(secondary_startup_arm) ENTRY(__secondary_switched) ldr_l r7, secondary_data + 12 @ get secondary_data.stack + ldr_l r0, secondary_data + 16 @ get secondary_data.cpu mov sp, r7 mov fp, #0 b secondary_start_kernel
arch/arm/kernel/setup.c +0 -6
@@ -532,12 +532,6 @@ void notrace cpu_init(void)BUG (); } -/* -* This only works on resume and secondary cores. For booting on the -* boot cpu, smp_prepare_boot_cpu is called after percpu area setup. -*/ -set_my_cpu_offset (per_cpu_offset (cpu)); -cpu_proc_init ();/*
arch/arm/kernel/smp.c +9 -5
@@ -153,6 +153,7 @@ int __cpu_up(unsigned int cpu, struct task_struct *idle) secondary_data.pgdir =virt_to_phys (idmap_pgd); secondary_data.swapper_pg_dir =get_arch_pgd (swapper_pg_dir);#endif + secondary_data.cpu = cpu;sync_cache_w (&secondary_data);/*
@@ -373,11 +374,14 @@ void arch_cpu_idle_dead(void)* cpu initialisation. There's some initialisation which needs * to be repeated to undo the effects of taking the CPU offline. */ - __asm__("mov sp, %0\n" + __asm__("mov r0, %1\n" +" mov sp, %0\n" " mov fp, #0\n" " b secondary_start_kernel" : - : "r" (task_stack_page(current) + THREAD_SIZE - 8)); + :"r" (task_stack_page (current) +THREAD_SIZE -8 ), +"r" (cpu) + : "r0"); }#endif /* CONFIG_HOTPLUG_CPU */
@@ -400,10 +404,11 @@ static void smp_store_cpu_info(unsigned int cpuid)* This is the secondary CPU boot entry. We're using this CPUs * idle thread stack, but a set of temporary page tables. */ -asmlinkage void secondary_start_kernel(void) +asmlinkage void secondary_start_kernel(unsigned int cpu) {struct mm_struct *mm = &init_mm; -unsigned int cpu; + +set_my_cpu_offset (per_cpu_offset (cpu));secondary_biglittle_init ();
@@ -420,7 +425,6 @@ asmlinkage void secondary_start_kernel(void)* All kernel threads share the same mm context; grab a * reference and switch to it. */ - cpu =smp_processor_id ();mmgrab (mm); current->active_mm = mm;cpumask_set_cpu (cpu,mm_cpumask (mm));
Signature · 1 line
2.33.0
PATCH 2/7 ARM: Pass task to secondary_start_kernel
In reply to Keith PackardThis avoids needing to compute the task pointer in this function, allowing it to be used as the source of identification in the future.
--- arch/arm/include/asm/smp.h | 3 ++- arch/arm/kernel/head-nommu.S | 1 + arch/arm/kernel/head.S | 1 + arch/arm/kernel/smp.c | 8 +++++--- 4 files changed, 9 insertions(+), 4 deletions(-)
arch/arm/include/asm/smp.h +2 -1
@@ -48,7 +48,7 @@ extern void set_smp_ipi_range(int ipi_base, int nr_ipi);* Called from platform specific assembly code, this is the * secondary CPU entry point. */ -asmlinkage void secondary_start_kernel(unsigned int cpu); +asmlinkage void secondary_start_kernel(unsigned int cpu, struct task_struct *task);/*
@@ -62,6 +62,7 @@ struct secondary_data {unsigned long swapper_pg_dir;void *stack;unsigned int cpu; +struct task_struct *task; };extern struct secondary_data secondary_data;extern void secondary_startup (void );
arch/arm/kernel/head-nommu.S +1 -0
@@ -115,6 +115,7 @@ ENTRY(secondary_startup) ret r12 1: bl __after_proc_init ldr r0, [r7, #16] @ set up cpu number + ldr r1, [r7, #20] @ set up task pointer ldr sp, [r7, #12] @ set up the stack pointer mov fp, #0 b secondary_start_kernel
arch/arm/kernel/head.S +1 -0
@@ -395,6 +395,7 @@ ENDPROC(secondary_startup_arm) ENTRY(__secondary_switched) ldr_l r7, secondary_data + 12 @ get secondary_data.stack ldr_l r0, secondary_data + 16 @ get secondary_data.cpu + ldr_l r1, secondary_data + 20 @ get secondary_data.task mov sp, r7 mov fp, #0 b secondary_start_kernel
arch/arm/kernel/smp.c +5 -3
@@ -154,6 +154,7 @@ int __cpu_up(unsigned int cpu, struct task_struct *idle) secondary_data.swapper_pg_dir =get_arch_pgd (swapper_pg_dir);#endif secondary_data.cpu = cpu; + secondary_data.task = idle;sync_cache_w (&secondary_data);/*
@@ -375,13 +376,14 @@ void arch_cpu_idle_dead(void)* to be repeated to undo the effects of taking the CPU offline. */ __asm__("mov r0, %1\n" +" mov r1, %2\n" " mov sp, %0\n" " mov fp, #0\n" " b secondary_start_kernel" : :"r" (task_stack_page (current) +THREAD_SIZE -8 ), -"r" (cpu) - : "r0"); +"r" (cpu),"r" (current) + : "r0", "r1"); }#endif /* CONFIG_HOTPLUG_CPU */
@@ -404,7 +406,7 @@ static void smp_store_cpu_info(unsigned int cpuid)* This is the secondary CPU boot entry. We're using this CPUs * idle thread stack, but a set of temporary page tables. */ -asmlinkage void secondary_start_kernel(unsigned int cpu) +asmlinkage void secondary_start_kernel(unsigned int cpu, struct task_struct *task) {struct mm_struct *mm = &init_mm;
Signature · 1 line
2.33.0
PATCH 3/7 ARM: Use smp_processor_id() in vfp_pm_suspend instead of ti->cpu
In reply to Keith PackardThese are equivalent when thread_info contains the CPU value, but when THREAD_INFO_IN_TASK is enabled, cpu moves to task_struct. Using the macro allows either.
--- arch/arm/vfp/vfpmodule.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm/vfp/vfpmodule.c +3 -3
@@ -458,16 +458,16 @@ static int vfp_pm_suspend(void)/* disable, just in case */ fmxr (FPEXC ,fmrx (FPEXC ) & ~FPEXC_EN ); - }else if (vfp_current_hw_state[ti->cpu]) { + }else if (vfp_current_hw_state[smp_processor_id ()]) {#ifndef CONFIG_SMP fmxr (FPEXC , fpexc |FPEXC_EN ); -vfp_save_state (vfp_current_hw_state[ti->cpu], fpexc); +vfp_save_state (vfp_current_hw_state[smp_processor_id ()], fpexc);fmxr (FPEXC , fpexc);#endif }/* clear any information we had about last context state */ - vfp_current_hw_state[ti->cpu] =NULL ; + vfp_current_hw_state[smp_processor_id ()] =NULL ;return 0 ; }
Signature · 1 line
2.33.0
PATCH 4/7 ARM: Use hack from powerpc to get current cpu number
In reply to Keith PackardWhen we enable THREAD_INFO_IN_TASK, the cpu number will disappear from thread_info and reappear in task_struct. As we cannot include linux/sched.h in asm/smp.h, there's no way to use that struct type in the raw_smp_processor_id macro. Instead, a hack from the powerpc code is used. This pulls the TI_CPU offset out of asm-offsets.h and uses that to find the cpu value.
--- arch/arm/Makefile | 8 ++++++++ arch/arm/include/asm/smp.h | 18 +++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-)
arch/arm/Makefile +8 -0
@@ -284,6 +284,14 @@ stack_protector_prepare: prepare0 $(eval GCC_PLUGINS_CFLAGS += $(SSP_PLUGIN_CFLAGS)) endif +ifdef CONFIG_SMP +prepare: task_cpu_prepare + +PHONY += task_cpu_prepare +task_cpu_prepare: prepare0 + $(eval KBUILD_CFLAGS += -D_TI_CPU=$(shell awk '{if ($$2 == "TI_CPU") print $$3;}' include/generated/asm-offsets.h)) +endif + all: $(notdir $(KBUILD_IMAGE))
arch/arm/include/asm/smp.h +17 -1
@@ -15,7 +15,23 @@# error "<asm/smp.h> included in non-SMP build"#endif -#define raw_smp_processor_id () (current_thread_info()->cpu) +/* +* This is particularly ugly: it appears we can't actually get the +* definition of task_struct here, but we need access to the CPU this +* task is running on, which is stored in task_struct when +* THREAD_INFO_IN_TASK is set. Instead of using task_struct we're +* using TI_CPU which is extracted from asm-offsets.h by kbuild to get +* the current processor ID. +* +* This also needs to be safeguarded when building asm-offsets.s +* because at that time TI_CPU is not defined yet. +*/ +#ifndef _TI_CPU +#define raw_smp_processor_id () (0) +#else +#define raw_smp_processor_id () \ + (*(unsigned int *)((void *)current_thread_info() + _TI_CPU)) +#endif struct seq_file ;
Signature · 1 line
2.33.0
Re: [PATCH 4/7] ARM: Use hack from powerpc to get current cpu number
Quoted 52 lines · Keith Packard
When we enable
THREAD_INFO_IN_TASK, the cpu number will disappear fromthread_infoand reappear intask_struct. As we cannot includelinux/sched.hinasm/smp.h, there's no way to use that struct type in theraw_smp_processor_idmacro. Instead, a hack from the powerpc code is used. This pulls theTI_CPUoffset out ofasm-offsets.hand uses that to find the cpu value.Signed-off-by Keith Packard <[email protected]>--- arch/arm/Makefile | 8 ++++++++ arch/arm/include/asm/smp.h | 18 +++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-)arch/arm/Makefile +8
-0@@ -284,6 +284,14 @@ stack_protector_prepare: prepare0 $(eval GCC_PLUGINS_CFLAGS += $(SSP_PLUGIN_CFLAGS)) endif +ifdef CONFIG_SMP +prepare: task_cpu_prepare + +PHONY += task_cpu_prepare +task_cpu_prepare: prepare0 + $(eval KBUILD_CFLAGS += -D_TI_CPU=$(shell awk '{if ($$2 == "TI_CPU") print $$3;}' include/generated/asm-offsets.h)) +endif + all: $(notdir $(KBUILD_IMAGE))arch/arm/include/asm/smp.h +10
-1@@ -15,7 +15,23 @@# error "<asm/smp.h> included in non-SMP build"#endif -#define raw_smp_processor_id () (current_thread_info()->cpu) +/* +* This is particularly ugly: it appears we can't actually get the +* definition of task_struct here, but we need access to the CPU this +* task is running on, which is stored in task_struct when +* THREAD_INFO_IN_TASK is set. Instead of using task_struct we're +* using TI_CPU which is extracted from asm-offsets.h by kbuild to get +* the current processor ID. +* +* This also needs to be safeguarded when building asm-offsets.s +* because at that time TI_CPU is not defined yet.
arch/arm/include/asm/smp.h line 37
Quoted 10 lines
arch/arm/include/asm/smp.h +7
-0+*/ +#ifndef _TI_CPU +#define raw_smp_processor_id () (0) +#else +#define raw_smp_processor_id () \ + (*(unsigned int *)((void *)current_thread_info() + _TI_CPU)) +#endif struct seq_file ;
As I stated before, I would really like to avoid using this hack - I don't think we need to.
PATCH 5/7 ARM: Stop using TPIDRPRW to hold per_cpu_offset
In reply to Keith PackardWe're going to store TPIDRPRW here instead
--- arch/arm/include/asm/percpu.h | 31 ------------------------------- arch/arm/kernel/setup.c | 7 ------- arch/arm/kernel/smp.c | 3 --- 3 files changed, 41 deletions(-)
arch/arm/include/asm/percpu.h +0 -31
@@ -7,37 +7,6 @@ registerunsigned 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 ) -static inline void set_my_cpu_offset (unsigned long off) -{ -/* Set TPIDRPRW */ - asmvolatile ("mcr p15, 0, %0, c13, c0, 4" : :"r" (off) :"memory" ); -} - -static inline unsigned long __my_cpu_offset (void ) -{ -unsigned long off; - -/* -* Read TPIDRPRW. -* 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)); - -return off; -} -#define __my_cpu_offset __my_cpu_offset() -#else -#define set_my_cpu_offset (x) do {} while(0) - -#endif /* CONFIG_SMP */ -#include <asm-generic/percpu.h> #endif /* _ASM_ARM_PERCPU_H_ */
arch/arm/kernel/setup.c +0 -7
@@ -590,13 +590,6 @@ void __init smp_setup_processor_id(void)for (i =1 ; i < nr_cpu_ids; ++i)cpu_logical_map (i) = i == cpu ?0 : i; -/* -* clear __my_cpu_offset on boot CPU to avoid hang caused by -* using percpu variable early, for example, lockdep will -* access percpu variable inside lock_release -*/ -set_my_cpu_offset (0 ); -pr_info ("Booting Linux on physical CPU 0x%x\n" , mpidr); }
arch/arm/kernel/smp.c +0 -3
@@ -410,8 +410,6 @@ asmlinkage void secondary_start_kernel(unsigned int cpu, struct task_struct *tas {struct mm_struct *mm = &init_mm; -set_my_cpu_offset (per_cpu_offset (cpu)); -secondary_biglittle_init ();/*
@@ -495,7 +493,6 @@ void __init smp_cpus_done(unsigned int max_cpus)void __init smp_prepare_boot_cpu (void ) { -set_my_cpu_offset (per_cpu_offset (smp_processor_id ())); }void __init smp_prepare_cpus (unsigned int max_cpus)
Signature · 1 line
2.33.0
Re: [PATCH 5/7] ARM: Stop using TPIDRPRW to hold per_cpu_offset
Quoted 3 lines · Keith Packard
We're going to store TPIDRPRW here instead
?
Quoted 1 line
Signed-off-by Keith Packard <[email protected]>
I'd much prefer to keep using TPIDIRPRW for the per-CPU offsets, and use the user space TLS register for current.
There are several reasons for this:
- arm64 does the same - as someone who still cares about ARM while many have moved on to arm64 or RISC-V, I am still trying to maintain parity between ARM and arm64 where possible.
- efficiency: loading the per-CPU offset using a CPU id stored in memory, which is then used to index the per-CPU offsets array in memory adds two additional loads to every load/store of a per-CPU variable
- 'current' usually does not change value under the code's feet, whereas per-CPU offsets might change at any time. Given the fact that the CPU offset load is visible to the compiler as a memory access, I suppose this should be safe, but I would still prefer per-CPU access to avoid going via current if possible.
Quoted 90 lines
--- arch/arm/include/asm/percpu.h | 31 ------------------------------- arch/arm/kernel/setup.c | 7 ------- arch/arm/kernel/smp.c | 3 --- 3 files changed, 41 deletions(-)arch/arm/include/asm/percpu.h +0
-31@@ -7,37 +7,6 @@ registerunsigned 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 ) -static inline void set_my_cpu_offset (unsigned long off) -{ -/* Set TPIDRPRW */ - asmvolatile ("mcr p15, 0, %0, c13, c0, 4" : :"r" (off) :"memory" ); -} - -static inline unsigned long __my_cpu_offset (void ) -{ -unsigned long off; - -/* -* Read TPIDRPRW. -* 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)); - -return off; -} -#define __my_cpu_offset __my_cpu_offset() -#else -#define set_my_cpu_offset (x) do {} while(0) - -#endif /* CONFIG_SMP */ -#include <asm-generic/percpu.h> #endif /* _ASM_ARM_PERCPU_H_ */ arch/arm/kernel/setup.c +0
-7@@ -590,13 +590,6 @@ void __init smp_setup_processor_id(void)for (i =1 ; i < nr_cpu_ids; ++i)cpu_logical_map (i) = i == cpu ?0 : i; -/* -* clear __my_cpu_offset on boot CPU to avoid hang caused by -* using percpu variable early, for example, lockdep will -* access percpu variable inside lock_release -*/ -set_my_cpu_offset (0 ); -pr_info ("Booting Linux on physical CPU 0x%x\n" , mpidr); }arch/arm/kernel/smp.c +0
-4@@ -410,8 +410,6 @@ asmlinkage void secondary_start_kernel(unsigned int cpu, struct task_struct *tas {struct mm_struct *mm = &init_mm; -set_my_cpu_offset (per_cpu_offset (cpu)); -secondary_biglittle_init ();/* @@ -495,7 +493,6 @@ void __init smp_cpus_done(unsigned int max_cpus)void __init smp_prepare_boot_cpu (void ) { -set_my_cpu_offset (per_cpu_offset (smp_processor_id ())); }void __init smp_prepare_cpus (unsigned int max_cpus) --2.33.0
PATCH 6/7 ARM: Use TPIDRPRW for current
In reply to Keith PackardStore current task pointer in CPU thread ID register TPIDRPRW so that accessing it doesn't depend on being able to locate thread_info off of the kernel stack pointer.
--- arch/arm/Kconfig | 4 +++ arch/arm/include/asm/assembler.h | 8 +++++ arch/arm/include/asm/current.h | 52 ++++++++++++++++++++++++++++++++ arch/arm/kernel/entry-armv.S | 4 +++ arch/arm/kernel/setup.c | 1 + arch/arm/kernel/smp.c | 1 + 6 files changed, 70 insertions(+) create mode 100644 arch/arm/include/asm/current.h
arch/arm/Kconfig +4 -0
@@ -1172,6 +1172,10 @@ config SMP_ON_UP If you don't know what to do here, say Y. +config CURRENT_POINTER_IN_TPIDRPRW + def_bool y + depends on (CPU_V6K || CPU_V7) && !CPU_V6 + config ARM_CPU_TOPOLOGY bool "Support cpu topology definition" depends on SMP && CPU_V7
arch/arm/include/asm/assembler.h +8 -0
@@ -209,6 +209,14 @@ mov \rd, \rd, lsl #THREAD_SIZE_ORDER + PAGE_SHIFT .endm +/* + * Set current task_info + * @src: Source register containing task_struct pointer + */ + .macro set_current src : req + mcr p15, 0, \src, c13, c0, 4 + .endm +/* * Increment/decrement the preempt count. */
arch/arm/include/asm/current.h new file +52 -0
52 lines not on this page · raw message
arch/arm/kernel/entry-armv.S +4 -0
@@ -761,6 +761,10 @@ ENTRY(__switch_to) ldr r6, [r2, #TI_CPU_DOMAIN] #endif switch_tls r1, r4, r5, r3, r7 +#ifdef CONFIG_CURRENT_POINTER_IN_TPIDRPRW + ldr r7, [r2, #TI_TASK] + set_current r7 +#endif #if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_SMP) ldr r7, [r2, #TI_TASK] ldr r8, =__stack_chk_guard
arch/arm/kernel/setup.c +1 -0
@@ -586,6 +586,7 @@ void __init smp_setup_processor_id(void)u32 mpidr =is_smp () ?read_cpuid_mpidr () &MPIDR_HWID_BITMASK :0 ;u32 cpu =MPIDR_AFFINITY_LEVEL (mpidr,0 ); +set_current (&init_task);cpu_logical_map (0 ) = cpu;for (i =1 ; i < nr_cpu_ids; ++i)cpu_logical_map (i) = i == cpu ?0 : i;
arch/arm/kernel/smp.c +1 -0
@@ -410,6 +410,7 @@ asmlinkage void secondary_start_kernel(unsigned int cpu, struct task_struct *tas {struct mm_struct *mm = &init_mm; +set_current (task);secondary_biglittle_init ();/*
Signature · 1 line
2.33.0
Re: [PATCH 6/7] ARM: Use TPIDRPRW for current
Quoted 113 lines · Keith Packard
Store current task pointer in CPU thread ID register TPIDRPRW so that accessing it doesn't depend on being able to locate
thread_infooff of the kernel stack pointer.Signed-off-by Keith Packard <[email protected]>--- arch/arm/Kconfig | 4 +++ arch/arm/include/asm/assembler.h | 8 +++++ arch/arm/include/asm/current.h | 52 ++++++++++++++++++++++++++++++++ arch/arm/kernel/entry-armv.S | 4 +++ arch/arm/kernel/setup.c | 1 + arch/arm/kernel/smp.c | 1 + 6 files changed, 70 insertions(+) create mode 100644 arch/arm/include/asm/current.harch/arm/Kconfig +4
-0@@ -1172,6 +1172,10 @@ config SMP_ON_UP If you don't know what to do here, say Y. +config CURRENT_POINTER_IN_TPIDRPRW + def_bool y + depends on (CPU_V6K || CPU_V7) && !CPU_V6 + config ARM_CPU_TOPOLOGY bool "Support cpu topology definition" depends on SMP && CPU_V7arch/arm/include/asm/assembler.h +8
-0@@ -209,6 +209,14 @@ mov \rd, \rd, lsl #THREAD_SIZE_ORDER + PAGE_SHIFT .endm +/* + * Set current task_info + * @src: Source register containing task_struct pointer + */ + .macro set_current src : req + mcr p15, 0, \src, c13, c0, 4 + .endm +/* * Increment/decrement the preempt count. */ arch/arm/include/asm/current.h new file +52
-0@@ -0,0 +1,52 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright © 2021 Keith Packard <[email protected]> +*/ + +#ifndef _ASM_ARM_CURRENT_H_ +#define _ASM_ARM_CURRENT_H_ + +#ifndef __ASSEMBLY__ + +registerunsigned long current_stack_pointer asm ("sp" ); + +/* +* Same as asm-generic/current.h, except that we store current +* in TPIDRPRW. TPIDRPRW only exists on V6K and V7 +*/ +#ifdef CONFIG_CURRENT_POINTER_IN_TPIDRPRW + +struct task_struct ; + +static inline void set_current (struct task_struct *tsk) +{ +/* Set TPIDRPRW */ + asmvolatile ("mcr p15, 0, %0, c13, c0, 4" : :"r" (tsk) :"memory" ); +} + +static __always_inline struct task_struct *get_current(void) +{ +struct task_struct *tsk; + +/* +* Read TPIDRPRW. +* 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" (tsk) + :"Q" (*(const unsigned long *)current_stack_pointer)); + +return tsk; +} +#define current get_current() +#else + +#define set_current (tsk) do {} while (0) + +#include <asm-generic/current.h> + +#endif /* CONFIG_SMP */ + +#endif /* __ASSEMBLY__ */ + +#endif /* _ASM_ARM_CURRENT_H_ */ arch/arm/kernel/entry-armv.S +0
-0
arch/arm/kernel/entry-armv.S ENTRY(__switch_to) · line 767
Quoted 7 lines
arch/arm/kernel/entry-armv.S +4
-0@@ -761,6 +761,10 @@ ENTRY(__switch_to) ldr r6, [r2, #TI_CPU_DOMAIN] #endif switch_tls r1, r4, r5, r3, r7 +#ifdef CONFIG_CURRENT_POINTER_IN_TPIDRPRW + ldr r7, [r2, #TI_TASK] + set_current r7 +#endif
This is too early: this will cause the thread notification hooks to be called with current pointing to the new task instead of the old one.
Quoted 30 lines
arch/arm/kernel/entry-armv.S +0
-0#if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_SMP) ldr r7, [r2, #TI_TASK] ldr r8, =__stack_chk_guardarch/arm/kernel/setup.c +1
-0@@ -586,6 +586,7 @@ void __init smp_setup_processor_id(void)u32 mpidr =is_smp () ?read_cpuid_mpidr () &MPIDR_HWID_BITMASK :0 ;u32 cpu =MPIDR_AFFINITY_LEVEL (mpidr,0 ); +set_current (&init_task);cpu_logical_map (0 ) = cpu;for (i =1 ; i < nr_cpu_ids; ++i)cpu_logical_map (i) = i == cpu ?0 : i;arch/arm/kernel/smp.c +1
-1@@ -410,6 +410,7 @@ asmlinkage void secondary_start_kernel(unsigned int cpu, struct task_struct *tas {struct mm_struct *mm = &init_mm; +set_current (task);secondary_biglittle_init ();/* -- 2.33.0
PATCH 7/7 ARM: Move thread_info into task_struct (v7 only)
In reply to Keith PackardThis avoids many stack overflow attacks which modified the thread_info structure by moving that into the task_struct as is done is almost all other architectures.
This depends on having 'current' stored in the TPIDRPRW register as that allows us to find thread_info and task_struct once the thread_info cannot be located using the kernel stack pointer.
--- arch/arm/Kconfig | 1 + arch/arm/include/asm/assembler.h | 4 ++++ arch/arm/include/asm/thread_info.h | 12 +++++++++++- arch/arm/kernel/asm-offsets.c | 4 ++++ arch/arm/kernel/entry-armv.S | 4 ++++ arch/arm/vfp/vfpmodule.c | 9 +++++++++ 6 files changed, 33 insertions(+), 1 deletion(-)
arch/arm/Kconfig +1 -0
@@ -128,6 +128,7 @@ config ARM select RTC_LIB select SET_FS select SYS_SUPPORTS_APM_EMULATION + select THREAD_INFO_IN_TASK if CURRENT_POINTER_IN_TPIDRPRW # Above selects are sorted alphabetically; please add new ones # according to that. Thanks. help
arch/arm/include/asm/assembler.h +4 -0
@@ -203,10 +203,14 @@* Get current thread_info. */ .macro get_thread_info, rd +#ifdef CONFIG_THREAD_INFO_IN_TASK + mrc p15, 0, \rd, c13, c0, 4 +#else ARM( mov \rd, sp, lsr #THREAD_SIZE_ORDER + PAGE_SHIFT ) THUMB( mov \rd, sp ) THUMB( lsr \rd, \rd, #THREAD_SIZE_ORDER + PAGE_SHIFT ) mov \rd, \rd, lsl #THREAD_SIZE_ORDER + PAGE_SHIFT +#endif .endm/*
arch/arm/include/asm/thread_info.h +11 -1
@@ -55,8 +55,10 @@ struct thread_info {unsigned long flags;/* low level flags */ int preempt_count;/* 0 => preemptable, <0 => bug */ mm_segment_t addr_limit;/* address limit */ +#ifndef CONFIG_THREAD_INFO_IN_TASK struct task_struct *task;/* main task structure */ __u32 cpu;/* cpu */ +#endif __u32 cpu_domain;/* cpu domain */ #ifdef CONFIG_STACKPROTECTOR_PER_TASK unsigned long stack_canary;
@@ -75,14 +77,21 @@ struct thread_info {#endif }; +#ifdef CONFIG_THREAD_INFO_IN_TASK +#define INIT_THREAD_INFO_TASK (tsk) +#else +#define INIT_THREAD_INFO_TASK (tsk) .task = &tsk, +#endif +#define INIT_THREAD_INFO (tsk) \ { \ - .task = &tsk, \ + INIT_THREAD_INFO_TASK(tsk) \ .flags = 0, \ .preempt_count = INIT_PREEMPT_COUNT, \ .addr_limit = KERNEL_DS, \ } +#ifndef CONFIG_THREAD_INFO_IN_TASK /* * how to get the thread information struct from C */
@@ -93,6 +102,7 @@ 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))
arch/arm/kernel/asm-offsets.c +4 -0
@@ -44,8 +44,12 @@ int main(void)DEFINE (TI_FLAGS , offsetof(struct thread_info , flags));DEFINE (TI_PREEMPT , offsetof(struct thread_info , preempt_count));DEFINE (TI_ADDR_LIMIT , offsetof(struct thread_info , addr_limit)); +#ifdef CONFIG_THREAD_INFO_IN_TASK +DEFINE (TI_CPU , offsetof(struct task_struct , cpu)); +#else DEFINE (TI_TASK , offsetof(struct thread_info , task));DEFINE (TI_CPU , offsetof(struct thread_info , cpu)); +#endif DEFINE (TI_CPU_DOMAIN , offsetof(struct thread_info , cpu_domain));DEFINE (TI_CPU_SAVE , offsetof(struct thread_info , cpu_context));DEFINE (TI_USED_CP , offsetof(struct thread_info , used_cp));
arch/arm/kernel/entry-armv.S +4 -0
@@ -762,9 +762,13 @@ ENTRY(__switch_to) #endif switch_tls r1, r4, r5, r3, r7 #ifdef CONFIG_CURRENT_POINTER_IN_TPIDRPRW +#ifdef CONFIG_THREAD_INFO_IN_TASK + set_current r2 +#else ldr r7, [r2, #TI_TASK] set_current r7 #endif +#endif #if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_SMP) ldr r7, [r2, #TI_TASK] ldr r8, =__stack_chk_guard
arch/arm/vfp/vfpmodule.c +9 -0
@@ -158,7 +158,12 @@ static void vfp_thread_copy(struct thread_info *thread)*/ static int vfp_notifier (struct notifier_block *self,unsigned long cmd,void *v) { +#ifdef CONFIG_THREAD_INFO_IN_TASK +struct task_struct *tsk = v; +struct thread_info *thread = &tsk->thread_info; +#else struct thread_info *thread = v; +#endif u32 fpexc;#ifdef CONFIG_SMP unsigned int cpu;
@@ -169,7 +174,11 @@ static int vfp_notifier(struct notifier_block *self, unsigned long cmd, void *v) fpexc =fmrx (FPEXC );#ifdef CONFIG_SMP +#ifdef CONFIG_THREAD_INFO_IN_TASK + cpu = tsk->cpu; +#else cpu = thread->cpu; +#endif /* * On SMP, if VFP is enabled, save the old state in
Signature · 1 line
2.33.0
Re: [PATCH 0/7] ARM: support THREAD_INFO_IN_TASK (v3)
In reply to Keith PackardQuoted 26 lines · Keith Packard
Placing
thread_infoin the kernel stack leaves it vulnerable to stack overflow attacks. This short series addresses that by using the existingTHREAD_INFO_IN_TASKinfrastructure.This is the third version of this series, in this version the changes are restricted to hardware which provides the TPIDRPRW register. This register is repurposed from holding the
per_cpu_offsetvalue to holding the 'current' value as that allows fetching this value atomically so that it can be used in a preemptable context.The series is broken into seven pieces:
1) Change the
secondary_start_kernelAPI to receive the cpu number. This avoids needing to be able to find this value independently in future patches.2) Change the
secondary_start_kernelAPI to also receive the 'task' value. Passing the value to this function also avoids needing to be able to discover it independently.3) A cleanup which avoids assuming that
THREAD_INFO_IN_TASKis not set.4) A hack, borrowed from the powerpc arch, which allows locating the 'cpu' field in either
thread_infoortask_struct, without requiringlinux/sched.hto be included inasm/smp.h
Quoted 7 lines
5) Disable the optimization storing
per_cpu_offsetin TPIDRPRW. This leaves the register free to hold 'current' instead.6) Use TPIDRPRW for 'current'. This is enabled for either
CPU_V6KorCPU_V7, but not ifCPU_V6is also enabled.7) Enable
THREAD_INFO_IN_TASKwhenever TPIDRPRW is used to hold 'current'.
Hi,
Thanks for your patches. This seems to be a v3 but the patches are not marked with it. Use "-v3" in format-patch to get it right.
The email here also lacks diffstat which is useful, for example to check whether any maintainer's relevant files are touched here. You can get it with "--cover-letter".
In total the command should look like:
git format-patch --cover-letter -v3 -7 HEAD
Of course you can use any other tools to achieve the same result but as of now - result is not the same.
Best regards,
Krzysztof
Re: [PATCH 0/7] ARM: support THREAD_INFO_IN_TASK (v3)
Quoted 45 lines · Krzysztof Kozlowski
Quoted 26 lines · Keith Packard
Placing
thread_infoin the kernel stack leaves it vulnerable to stack overflow attacks. This short series addresses that by using the existingTHREAD_INFO_IN_TASKinfrastructure.This is the third version of this series, in this version the changes are restricted to hardware which provides the TPIDRPRW register. This register is repurposed from holding the
per_cpu_offsetvalue to holding the 'current' value as that allows fetching this value atomically so that it can be used in a preemptable context.The series is broken into seven pieces:
1) Change the
secondary_start_kernelAPI to receive the cpu number. This avoids needing to be able to find this value independently in future patches.2) Change the
secondary_start_kernelAPI to also receive the 'task' value. Passing the value to this function also avoids needing to be able to discover it independently.3) A cleanup which avoids assuming that
THREAD_INFO_IN_TASKis not set.4) A hack, borrowed from the powerpc arch, which allows locating the 'cpu' field in either
thread_infoortask_struct, without requiringlinux/sched.hto be included inasm/smp.hQuoted 7 lines
5) Disable the optimization storing
per_cpu_offsetin TPIDRPRW. This leaves the register free to hold 'current' instead.6) Use TPIDRPRW for 'current'. This is enabled for either
CPU_V6KorCPU_V7, but not ifCPU_V6is also enabled.7) Enable
THREAD_INFO_IN_TASKwhenever TPIDRPRW is used to hold 'current'.Hi,
Thanks for your patches. This seems to be a v3 but the patches are not marked with it. Use "-v3" in format-patch to get it right.
The email here also lacks diffstat which is useful, for example to check whether any maintainer's relevant files are touched here. You can get it with "--cover-letter".
Quoted 6 lines
In total the command should look like:
git format-patch --cover-letter -v3 -7 HEADOf course you can use any other tools to achieve the same result but as of now - result is not the same.
Also, this ended up in my GMail spam folder, likely due to some antispam ID header being set incorrectly?
Re: [PATCH 0/7] ARM: support THREAD_INFO_IN_TASK (v3)
Hi Ard,
Quoted 2 lines · Ard Biesheuvel
Also, this ended up in my GMail spam folder, likely due to some antispam ID header being set incorrectly?
I have a rule to never mark as spam email with "patch" in the subject. So far only one false positive in all these years ;-)
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds