Skip to content

Commit bf0baa5

Browse files
puranjaymohanctmarinas
authored andcommitted
arm64: implement raw_smp_processor_id() using thread_info
Historically, arm64 implemented raw_smp_processor_id() as a read of current_thread_info()->cpu. This changed when arm64 moved thread_info into task struct, as at the time CONFIG_THREAD_INFO_IN_TASK made core code use thread_struct::cpu for the cpu number, and due to header dependencies prevented using this in raw_smp_processor_id(). As a workaround, we moved to using a percpu variable in commit: 57c8295 ("arm64: make cpu number a percpu variable") Since then, thread_info::cpu was reintroduced, and core code was made to use this in commits: 001430c ("arm64: add CPU field to struct thread_info") bcf9033 ("sched: move CPU field back into thread_info if THREAD_INFO_IN_TASK=y") Consequently it is possible to use current_thread_info()->cpu again. This decreases the number of emitted instructions like in the following example: Dump of assembler code for function bpf_get_smp_processor_id: 0xffff8000802cd608 <+0>: nop 0xffff8000802cd60c <+4>: nop 0xffff8000802cd610 <+8>: adrp x0, 0xffff800082138000 0xffff8000802cd614 <+12>: mrs x1, tpidr_el1 0xffff8000802cd618 <+16>: add x0, x0, #0x8 0xffff8000802cd61c <+20>: ldrsw x0, [x0, x1] 0xffff8000802cd620 <+24>: ret After this patch: Dump of assembler code for function bpf_get_smp_processor_id: 0xffff8000802c9130 <+0>: nop 0xffff8000802c9134 <+4>: nop 0xffff8000802c9138 <+8>: mrs x0, sp_el0 0xffff8000802c913c <+12>: ldr w0, [x0, #24] 0xffff8000802c9140 <+16>: ret A microbenchmark[1] was built to measure the performance improvement provided by this change. It calls the following function given number of times and finds the runtime overhead: static noinline int get_cpu_id(void) { return smp_processor_id(); } Run the benchmark like: modprobe smp_processor_id nr_function_calls=1000000000 +--------------------------+------------------------+ | | Number of Calls | Time taken | +--------+-----------------+------------------------+ | Before | 1000000000 | 1602888401ns | +--------+-----------------+------------------------+ | After | 1000000000 | 1206212658ns | +--------+-----------------+------------------------+ | Difference (decrease) | 396675743ns (24.74%) | +---------------------------------------------------+ Remove the percpu variable cpu_number as it is used only in set_smp_ipi_range() as a dummy variable to be passed to ipi_handler(). Use irq_stat in place of cpu_number here like arm32. [1] puranjaymohan/linux@77d3fdd Signed-off-by: Puranjay Mohan <[email protected]> Acked-by: Mark Rutland <[email protected]> Reviewed-by: Stephen Boyd <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Catalin Marinas <[email protected]>
1 parent 7647e2b commit bf0baa5

File tree

2 files changed

+3
-19
lines changed

2 files changed

+3
-19
lines changed

arch/arm64/include/asm/smp.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,11 @@
2525

2626
#ifndef __ASSEMBLY__
2727

28-
#include <asm/percpu.h>
29-
3028
#include <linux/threads.h>
3129
#include <linux/cpumask.h>
3230
#include <linux/thread_info.h>
3331

34-
DECLARE_PER_CPU_READ_MOSTLY(int, cpu_number);
35-
36-
/*
37-
* We don't use this_cpu_read(cpu_number) as that has implicit writes to
38-
* preempt_count, and associated (compiler) barriers, that we'd like to avoid
39-
* the expense of. If we're preemptible, the value can be stale at use anyway.
40-
* And we can't use this_cpu_ptr() either, as that winds up recursing back
41-
* here under CONFIG_DEBUG_PREEMPT=y.
42-
*/
43-
#define raw_smp_processor_id() (*raw_cpu_ptr(&cpu_number))
32+
#define raw_smp_processor_id() (current_thread_info()->cpu)
4433

4534
/*
4635
* Logical CPU mapping.

arch/arm64/kernel/smp.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@
5555

5656
#include <trace/events/ipi.h>
5757

58-
DEFINE_PER_CPU_READ_MOSTLY(int, cpu_number);
59-
EXPORT_PER_CPU_SYMBOL(cpu_number);
60-
6158
/*
6259
* as from 2.5, kernels no longer have an init_tasks structure
6360
* so we need some other way of telling a new secondary core
@@ -749,8 +746,6 @@ void __init smp_prepare_cpus(unsigned int max_cpus)
749746
*/
750747
for_each_possible_cpu(cpu) {
751748

752-
per_cpu(cpu_number, cpu) = cpu;
753-
754749
if (cpu == smp_processor_id())
755750
continue;
756751

@@ -1028,12 +1023,12 @@ void __init set_smp_ipi_range(int ipi_base, int n)
10281023

10291024
if (ipi_should_be_nmi(i)) {
10301025
err = request_percpu_nmi(ipi_base + i, ipi_handler,
1031-
"IPI", &cpu_number);
1026+
"IPI", &irq_stat);
10321027
WARN(err, "Could not request IPI %d as NMI, err=%d\n",
10331028
i, err);
10341029
} else {
10351030
err = request_percpu_irq(ipi_base + i, ipi_handler,
1036-
"IPI", &cpu_number);
1031+
"IPI", &irq_stat);
10371032
WARN(err, "Could not request IPI %d as IRQ, err=%d\n",
10381033
i, err);
10391034
}

0 commit comments

Comments
 (0)