[PATCH 3/3] ARM: Add per-cpu variable cpu_number (v7 only)

From: Keith Packard
Date: Sat Sep 04 2021 - 02:09:31 EST


Holds 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.

Signed-off-by: Keith Packard <keithpac@xxxxxxxxxx>
---
arch/arm/Makefile | 8 --------
arch/arm/include/asm/smp.h | 17 +++--------------
arch/arm/kernel/smp.c | 16 ++++++++++++++++
3 files changed, 19 insertions(+), 22 deletions(-)

diff --git a/arch/arm/Makefile b/arch/arm/Makefile
index 71a2ba4549d3..415c3514573a 100644
--- a/arch/arm/Makefile
+++ b/arch/arm/Makefile
@@ -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))


diff --git a/arch/arm/include/asm/smp.h b/arch/arm/include/asm/smp.h
index 1c38d1fde641..67d21233bdfe 100644
--- a/arch/arm/include/asm/smp.h
+++ b/arch/arm/include/asm/smp.h
@@ -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
diff --git a/arch/arm/kernel/smp.c b/arch/arm/kernel/smp.c
index be0ede16dbb1..a33397618f1e 100644
--- a/arch/arm/kernel/smp.c
+++ b/arch/arm/kernel/smp.c
@@ -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
--
2.33.0