Skip to contentSkip to conversation

arm64/arch_timer: include <linux/percpu.h>

PATCH Applied Puranjay Mohan 3 participants 5 messages May 3, 2024 – Jun 12, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 arm64/arch_timer: include <linux/percpu.h>

arch_timer.h includes linux/smp.h since the commit:

6acc71ccac7187fc ("arm64: arch_timer: Allows a CPU-specific erratum to only affect a subset of CPUs")

It was included to use DEFINE_PER_CPU(), etc. But It should have included <linux/percpu.h> rather than <linux/smp.h>. It worked because smp.h includes percpu.h.

The next commit will remove percpu.h from smp.h and it will break this usage.

Explicitly include percpu.h and remove smp.h

---
 arch/arm64/include/asm/arch_timer.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/include/asm/arch_timer.h +1 -1
index 934c658ee947..f5794d50f51d 100644
@@ -15,7 +15,7 @@
 #include <linux/bug.h>
 #include <linux/init.h>
 #include <linux/jump_label.h>
-#include <linux/smp.h>
+#include <linux/percpu.h>
 #include <linux/types.h>
 
 #include <clocksource/arm_arch_timer.h>
Signature · 1 line
2.40.1
#1 lore raw

PATCH v3 2/2 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:

57c82954e77fa12c ("arm64: make cpu number a percpu variable")

Since then, thread_info::cpu was reintroduced, and core code was made to use this in commits:

001430c1910df65a ("arm64: add CPU field to struct thread_info") bcf9033e5449bdca ("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] https://github.com/puranjaymohan/linux/commit/77d3fdd

---
 arch/arm64/include/asm/smp.h | 13 +------------
 arch/arm64/kernel/smp.c      |  9 ++-------
 2 files changed, 3 insertions(+), 19 deletions(-)
arch/arm64/include/asm/smp.h +1 -12
index efb13112b408..2510eec026f7 100644
@@ -25,22 +25,11 @@
 
 #ifndef __ASSEMBLY__
 
-#include <asm/percpu.h>
-
 #include <linux/threads.h>
 #include <linux/cpumask.h>
 #include <linux/thread_info.h>
 
-DECLARE_PER_CPU_READ_MOSTLY(int, cpu_number);
-
-/*
- * We don't use this_cpu_read(cpu_number) as that has implicit writes to
- * preempt_count, and associated (compiler) barriers, that we'd like to avoid
- * the expense of. If we're preemptible, the value can be stale at use anyway.
- * And we can't use this_cpu_ptr() either, as that winds up recursing back
- * here under CONFIG_DEBUG_PREEMPT=y.
- */
-#define raw_smp_processor_id() (*raw_cpu_ptr(&cpu_number))
+#define raw_smp_processor_id() (current_thread_info()->cpu)
 
 /*
  * Logical CPU mapping.
arch/arm64/kernel/smp.c +2 -7
index 4ced34f62dab..98d4e352c3d0 100644
@@ -55,9 +55,6 @@
 
 #include <trace/events/ipi.h>
 
-DEFINE_PER_CPU_READ_MOSTLY(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
@@ -742,8 +739,6 @@ void __init smp_prepare_cpus(unsigned int max_cpus)
 	 */
 	for_each_possible_cpu(cpu) {
 
-		per_cpu(cpu_number, cpu) = cpu;
-
 		if (cpu == smp_processor_id())
 			continue;
 
@@ -1021,12 +1016,12 @@ void __init set_smp_ipi_range(int ipi_base, int n)
 
 		if (ipi_should_be_nmi(i)) {
 			err = request_percpu_nmi(ipi_base + i, ipi_handler,
-						 "IPI", &cpu_number);
+						 "IPI", &irq_stat);
 			WARN(err, "Could not request IPI %d as NMI, err=%d\n",
 			     i, err);
 		} else {
 			err = request_percpu_irq(ipi_base + i, ipi_handler,
-						 "IPI", &cpu_number);
+						 "IPI", &irq_stat);
 			WARN(err, "Could not request IPI %d as IRQ, err=%d\n",
 			     i, err);
 		}
Signature · 1 line
2.40.1
Reviewed-by on v3 2/2
#2 lore raw

Re: [PATCH v3 2/2] arm64: implement raw_smp_processor_id() using thread_info

Quoting Puranjay Mohan (2024-05-03 10:18:47)

Quoted 5 lines

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] https://github.com/puranjaymohan/linux/commit/77d3fdd

Maybe you should have put this under the cut for the patch so its on the mailing list, and lore.

Quoted 4 lines

---

arch/arm64/kernel/smp.c void __init set_smp_ipi_range(int ipi_base, int n) · line 1019

Quoted 10 lines
arch/arm64/kernel/smp.c +1 -1
index 4ced34f62dab..98d4e352c3d0 100644
@@ -1021,12 +1016,12 @@ void __init set_smp_ipi_range(int ipi_base, int n)
 
                if (ipi_should_be_nmi(i)) {
                        err = request_percpu_nmi(ipi_base + i, ipi_handler,
-                                                "IPI", &cpu_number);
+                                                "IPI", &irq_stat);

I know this is mirroring arm, but it would be nice to have a comment saying that we need some percpu variable here even if the handler doesn't use it.

Reviewed-by on v3 1/2
#3 lore raw
In reply to Puranjay Mohan

Quoting Puranjay Mohan (2024-05-03 10:18:46)

Quoted 8 lines

arch_timer.h includes linux/smp.h since the commit:

6acc71ccac7187fc ("arm64: arch_timer: Allows a CPU-specific erratum to only affect a subset of CPUs")

It was included to use DEFINE_PER_CPU(), etc. But It should have included <linux/percpu.h> rather than <linux/smp.h>. It worked because smp.h includes percpu.h.

Quoted 8 lines

The next commit will remove percpu.h from smp.h and it will break this usage.

Explicitly include percpu.h and remove smp.h

---

maintainer Applied
#4 lore raw
In reply to Puranjay Mohan
Quoted 9 lines · Puranjay Mohan

arch_timer.h includes linux/smp.h since the commit:

6acc71ccac7187fc ("arm64: arch_timer: Allows a CPU-specific erratum to only affect a subset of CPUs")

It was included to use DEFINE_PER_CPU(), etc. But It should have included <linux/percpu.h> rather than <linux/smp.h>. It worked because smp.h includes percpu.h.

[...]

Applied to arm64 (for-next/misc), thanks!

[1/2] arm64/arch_timer: include <linux/percpu.h>

      https://git.kernel.org/arm64/c/7647e2b109f4
[2/2] arm64: implement raw_smp_processor_id() using thread_info
      https://git.kernel.org/arm64/c/bf0baa5bbdc9
Signature · 1 line
Catalin