mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2026-08-09 06:14:34 +02:00
Like other architectures such as x86, arm64, riscv, powerpc and s390, select THREAD_INFO_IN_TASK for LoongArch to move thread_info off the stack into task_struct. This follows modern kernel standards and also makes the system more secure. With this patch, thread_info is included in task_struct at an offset of 0 instead of being placed at the bottom of the kernel stack. Thus, the $tp register points to both thread_info and task_struct. To support this, introduce a per-CPU variable cpu_tasks to store the pointer to the current task_struct. This decouples the recovery of the $tp register from the stack pointer during exception entry. Then initialize cpu_tasks for the primary and secondary CPUs during arch-specific setup and SMP boot paths. To eliminate the dangerous windows during the early initialization where the cpu_tasks remains uninitialized, set_current() is invoked as early as possible in both setup_arch() and start_secondary(). This ensures the $tp recovery barrier is armed in case any early boot exceptions or kernel panics occur. Modify SAVE_SOME and handle_syscall to restore the $tp register from cpu_tasks, and also use the la_abs absolute addressing for cpu_tasks access in assembly to bypass the relocation limits within exception handling sections. By advancing the preservation of u0 in SAVE_SOME, we reuse the PERCPU_BASE_KS value in u0 for the cpu_tasks calculation, effectively eliminating a duplicate csrrd instruction execution on SMP platforms. Update <asm/switch_to.h> and <kernel/switch.S> to fully support the CONFIG_THREAD_INFO_IN_TASK feature. Remove the obsolete next_ti argument from __switch_to(), which shifts the remaining arguments ahead in the calling convention (sched_ra from a3 to a2, and sched_cfa from a4 to a3). Under the new configuration, __switch_to() now directly derives the thread pointer ($tp) from the next task_struct pointer in a1. To preserve the optimal and clean "move tp, a1" path for 64-bit kernels, the thread pointer ($tp) is assigned directly from a1 in the core path. For 32-bit kernels, where a1 carries a 2000-byte structural pointer bias at entry, an explicit adjustment "PTR_ADDI tp, tp, -TASK_STRUCT_OFFSET" is introduced at the function exit. In the context of __switch_to(), local interrupts are disabled, and the kernel is in a critical switching phase where handling any synchronous exception is practically impossible and prohibited. If any synchronous exception or watchpoint does trigger in this narrow window, it constitutes a fatal double fault and the kernel is expected to die/panic immediately anyway. Therefore, the temporary biased value in $tp is safe and acceptable here. Additionally, evaluate the stack lookup as a single load instruction "LONG_LPTR t0, a1, (TASK_STACK - TASK_STRUCT_OFFSET)", this perfectly satisfies both 32-bit and 64-bit kernels. Using the "next" pointer in a1 as the base register, rather than $tp, effectively unchains the data dependency (RAW hazard) from the preceding move instruction, maximizing the instruction-level parallelism and superscalar execution efficiency while naturally adapting the structural shift. With CONFIG_THREAD_INFO_IN_TASK enabled, the kernel stack life cycle is decoupled from task_struct and can be freed concurrently. Currently, show_stacktrace() reads raw stack data via __get_addr() and subsequently calls show_backtrace() to unwind the frame, without holding any reference to the target task's stack. If show_stacktrace() is called on a concurrently exiting task, it could attempt to read from a freed or reallocated kernel stack. This introduces a severe use-after-free (UAF) read risk or kernel panics. Wrap the entire stack inspection process inside show_stacktrace() with a try_get_task_stack() and put_task_stack() pair. This ensures the task stack remains pinned safely during both the raw stack data dump loop and the subsequent stack unwinding phase. Also, ensure that the task pointer is initialized to "current" early if it is NULL, so that try_get_task_stack() always operates on a valid task reference. Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
143 lines
2.9 KiB
ArmAsm
143 lines
2.9 KiB
ArmAsm
/* SPDX-License-Identifier: GPL-2.0 */
|
|
/*
|
|
* Copyright (C) 2020-2022 Loongson Technology Corporation Limited
|
|
*/
|
|
#include <linux/init.h>
|
|
#include <linux/threads.h>
|
|
|
|
#include <asm/addrspace.h>
|
|
#include <asm/asm.h>
|
|
#include <asm/asmmacro.h>
|
|
#include <asm/bug.h>
|
|
#include <asm/regdef.h>
|
|
#include <asm/loongarch.h>
|
|
#include <asm/stackframe.h>
|
|
|
|
#ifdef CONFIG_EFI_STUB
|
|
|
|
#include "efi-header.S"
|
|
|
|
__HEAD
|
|
|
|
_head:
|
|
.word IMAGE_DOS_SIGNATURE /* "MZ", MS-DOS header */
|
|
.org 0x8
|
|
.dword _kernel_entry /* Kernel entry point (physical address) */
|
|
.dword _kernel_asize /* Kernel image effective size */
|
|
.quad PHYS_LINK_KADDR /* Kernel image load offset from start of RAM */
|
|
.org 0x38 /* 0x20 ~ 0x37 reserved */
|
|
.long LINUX_PE_MAGIC
|
|
.long pe_header - _head /* Offset to the PE header */
|
|
|
|
pe_header:
|
|
__EFI_PE_HEADER
|
|
|
|
SYM_DATA(kernel_asize, .long _kernel_asize);
|
|
SYM_DATA(kernel_fsize, .long _kernel_fsize);
|
|
|
|
#endif
|
|
|
|
__REF
|
|
|
|
.align 12
|
|
|
|
SYM_CODE_START(kernel_entry) # kernel entry point
|
|
UNWIND_HINT_END_OF_STACK
|
|
|
|
SETUP_TWINS
|
|
SETUP_MODES t0
|
|
JUMP_VIRT_ADDR t0, t1
|
|
SETUP_DMWINS t0
|
|
|
|
la.pcrel t0, __bss_start # clear .bss
|
|
LONG_S zero, t0, 0
|
|
la.pcrel t1, __bss_stop - LONGSIZE
|
|
1:
|
|
PTR_ADDI t0, t0, LONGSIZE
|
|
LONG_S zero, t0, 0
|
|
bne t0, t1, 1b
|
|
|
|
la.pcrel t0, fw_arg0
|
|
PTR_S a0, t0, 0 # firmware arguments
|
|
la.pcrel t0, fw_arg1
|
|
PTR_S a1, t0, 0
|
|
la.pcrel t0, fw_arg2
|
|
PTR_S a2, t0, 0
|
|
|
|
#ifdef CONFIG_PAGE_SIZE_4KB
|
|
LONG_LI t0, 0
|
|
LONG_LI t1, CSR_STFILL
|
|
csrxchg t0, t1, LOONGARCH_CSR_IMPCTL1
|
|
#endif
|
|
/* KSave3 used for percpu base, initialized as 0 */
|
|
csrwr zero, PERCPU_BASE_KS
|
|
/* GPR21 used for percpu base (runtime), initialized as 0 */
|
|
move u0, zero
|
|
|
|
la.pcrel tp, init_task
|
|
la.pcrel t0, init_stack
|
|
PTR_LI t1, _THREAD_SIZE
|
|
PTR_ADD t0, t0, t1
|
|
PTR_ADDI sp, t0, -PT_SIZE
|
|
set_saved_sp sp, t0, t1
|
|
|
|
#ifdef CONFIG_RELOCATABLE
|
|
|
|
bl relocate_kernel
|
|
|
|
#ifdef CONFIG_RANDOMIZE_BASE
|
|
/* Repoint the sp into the new kernel */
|
|
LONG_LPTR t0, tp, TASK_STACK
|
|
PTR_LI t1, _THREAD_SIZE
|
|
PTR_ADD t0, t0, t1
|
|
PTR_ADDI sp, t0, -PT_SIZE
|
|
set_saved_sp sp, t0, t1
|
|
|
|
/* Jump to the new kernel: new_pc = current_pc + random_offset */
|
|
pcaddi t0, 0
|
|
PTR_ADD t0, t0, a0
|
|
jirl zero, t0, 0xc
|
|
#endif /* CONFIG_RANDOMIZE_BASE */
|
|
|
|
#endif /* CONFIG_RELOCATABLE */
|
|
|
|
#ifdef CONFIG_KASAN
|
|
bl kasan_early_init
|
|
#endif
|
|
|
|
bl start_kernel
|
|
ASM_BUG()
|
|
|
|
SYM_CODE_END(kernel_entry)
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
/*
|
|
* SMP slave cpus entry point. Board specific code for bootstrap calls this
|
|
* function after setting up the stack and tp registers.
|
|
*/
|
|
SYM_CODE_START(smpboot_entry)
|
|
UNWIND_HINT_END_OF_STACK
|
|
|
|
SETUP_TWINS
|
|
SETUP_MODES t0
|
|
JUMP_VIRT_ADDR t0, t1
|
|
SETUP_DMWINS t0
|
|
|
|
#ifdef CONFIG_PAGE_SIZE_4KB
|
|
LONG_LI t0, 0
|
|
LONG_LI t1, CSR_STFILL
|
|
csrxchg t0, t1, LOONGARCH_CSR_IMPCTL1
|
|
#endif
|
|
la.pcrel t0, cpuboot_data
|
|
ld.d tp, t0, CPU_BOOT_TASK
|
|
ld.d sp, t0, CPU_BOOT_STACK
|
|
ld.d u0, t0, CPU_BOOT_OFFSET
|
|
|
|
bl start_secondary
|
|
ASM_BUG()
|
|
|
|
SYM_CODE_END(smpboot_entry)
|
|
|
|
#endif /* CONFIG_SMP */
|