mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-08-28 15:47:16 -04: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>
127 lines
3.2 KiB
C
127 lines
3.2 KiB
C
/* SPDX-License-Identifier: GPL-2.0 */
|
|
/*
|
|
* Author: Huacai Chen <chenhuacai@loongson.cn>
|
|
* Copyright (C) 2020-2022 Loongson Technology Corporation Limited
|
|
*/
|
|
#ifndef __ASM_SMP_H
|
|
#define __ASM_SMP_H
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
#include <linux/atomic.h>
|
|
#include <linux/bitops.h>
|
|
#include <linux/linkage.h>
|
|
#include <linux/threads.h>
|
|
#include <linux/cpumask.h>
|
|
|
|
struct smp_ops {
|
|
void (*init_ipi)(void);
|
|
void (*send_ipi_single)(int cpu, unsigned int action);
|
|
void (*send_ipi_mask)(const struct cpumask *mask, unsigned int action);
|
|
};
|
|
extern struct smp_ops mp_ops;
|
|
|
|
extern int smp_num_siblings;
|
|
extern int num_processors;
|
|
extern int disabled_cpus;
|
|
extern cpumask_t cpu_sibling_map[];
|
|
extern cpumask_t cpu_llc_shared_map[];
|
|
extern cpumask_t cpu_core_map[];
|
|
extern cpumask_t cpu_foreign_map[];
|
|
|
|
void loongson_smp_setup(void);
|
|
void loongson_prepare_cpus(unsigned int max_cpus);
|
|
void loongson_boot_secondary(int cpu, struct task_struct *idle);
|
|
void loongson_init_secondary(void);
|
|
void loongson_smp_finish(void);
|
|
#ifdef CONFIG_HOTPLUG_CPU
|
|
int loongson_cpu_disable(void);
|
|
void loongson_cpu_die(unsigned int cpu);
|
|
#endif
|
|
|
|
static inline void __init plat_smp_setup(void)
|
|
{
|
|
loongson_smp_setup();
|
|
}
|
|
|
|
static inline int raw_smp_processor_id(void)
|
|
{
|
|
#if defined(__VDSO__)
|
|
extern int vdso_smp_processor_id(void)
|
|
__compiletime_error("VDSO should not call smp_processor_id()");
|
|
return vdso_smp_processor_id();
|
|
#else
|
|
return current_thread_info()->cpu;
|
|
#endif
|
|
}
|
|
#define raw_smp_processor_id raw_smp_processor_id
|
|
|
|
/* Map from cpu id to sequential logical cpu number. This will only
|
|
* not be idempotent when cpus failed to come on-line. */
|
|
extern int __cpu_number_map[NR_CPUS];
|
|
#define cpu_number_map(cpu) __cpu_number_map[cpu]
|
|
|
|
/* The reverse map from sequential logical cpu number to cpu id. */
|
|
extern int __cpu_logical_map[NR_CPUS];
|
|
#define cpu_logical_map(cpu) __cpu_logical_map[cpu]
|
|
|
|
#define cpu_physical_id(cpu) cpu_logical_map(cpu)
|
|
|
|
#define ACTION_BOOT_CPU 0
|
|
#define ACTION_RESCHEDULE 1
|
|
#define ACTION_CALL_FUNCTION 2
|
|
#define ACTION_IRQ_WORK 3
|
|
#define ACTION_CLEAR_VECTOR 4
|
|
#define SMP_BOOT_CPU BIT(ACTION_BOOT_CPU)
|
|
#define SMP_RESCHEDULE BIT(ACTION_RESCHEDULE)
|
|
#define SMP_CALL_FUNCTION BIT(ACTION_CALL_FUNCTION)
|
|
#define SMP_IRQ_WORK BIT(ACTION_IRQ_WORK)
|
|
#define SMP_CLEAR_VECTOR BIT(ACTION_CLEAR_VECTOR)
|
|
|
|
struct seq_file;
|
|
|
|
struct secondary_data {
|
|
unsigned long task;
|
|
unsigned long stack;
|
|
unsigned long offset;
|
|
};
|
|
extern struct secondary_data cpuboot_data;
|
|
|
|
extern asmlinkage void smpboot_entry(void);
|
|
extern asmlinkage void start_secondary(void);
|
|
|
|
extern void calculate_cpu_foreign_map(void);
|
|
|
|
/*
|
|
* Generate IPI list text
|
|
*/
|
|
extern void show_ipi_list(struct seq_file *p, int prec);
|
|
|
|
static inline void arch_send_call_function_single_ipi(int cpu)
|
|
{
|
|
mp_ops.send_ipi_single(cpu, ACTION_CALL_FUNCTION);
|
|
}
|
|
|
|
static inline void arch_send_call_function_ipi_mask(const struct cpumask *mask)
|
|
{
|
|
mp_ops.send_ipi_mask(mask, ACTION_CALL_FUNCTION);
|
|
}
|
|
|
|
#ifdef CONFIG_HOTPLUG_CPU
|
|
static inline int __cpu_disable(void)
|
|
{
|
|
return loongson_cpu_disable();
|
|
}
|
|
|
|
static inline void __cpu_die(unsigned int cpu)
|
|
{
|
|
loongson_cpu_die(cpu);
|
|
}
|
|
#endif
|
|
|
|
#else /* !CONFIG_SMP */
|
|
#define cpu_logical_map(cpu) 0
|
|
#endif /* CONFIG_SMP */
|
|
|
|
#endif /* __ASM_SMP_H */
|