Skip to contentSkip to conversation

riscv: Optimize switch_mm by passing "cpu" to flush_icache_deferred()

PATCH In review Jisheng Zhang 2 participants 2 messages May 11, 2021 – May 26, 2021 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: Optimize switch_mm by passing "cpu" to flush_icache_deferred()

From: Jisheng Zhang <[email protected]>

Directly passing the cpu to flush_icache_deferred() rather than calling smp_processor_id() again.

Here are some performance numbers:

With a run of hackbench 30 times on a single core riscv64 Qemu instance with 1GB memory:

without this patch: mean 36.934
with this patch: mean 36.104 (improved by 2.24%)

---
 arch/riscv/mm/context.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
arch/riscv/mm/context.c +2 -3
index 68aa312fc352..6d445f2888ec 100644
@@ -281,10 +281,9 @@ static inline void set_mm(struct mm_struct *mm, unsigned int cpu)
  * actually performs that local instruction cache flush, which implicitly only
  * refers to the current hart.
  */
-static inline void flush_icache_deferred(struct mm_struct *mm)
+static inline void flush_icache_deferred(struct mm_struct *mm, unsigned int cpu)
 {
 #ifdef CONFIG_SMP
-	unsigned int cpu = smp_processor_id();
 	cpumask_t *mask = &mm->context.icache_stale_mask;
 
 	if (cpumask_test_cpu(cpu, mask)) {
@@ -320,5 +319,5 @@ void switch_mm(struct mm_struct *prev, struct mm_struct *next,
 
 	set_mm(next, cpu);
 
-	flush_icache_deferred(next);
+	flush_icache_deferred(next, cpu);
 }
Signature · 8 lines
2.31.0



_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
maintainer
#1 lore raw
Quoted 5 lines · On Tue, 11 May 2021 10:42:31 PDT (-0700), [email protected] wrote:

From: Jisheng Zhang <[email protected]>

Directly passing the cpu to flush_icache_deferred() rather than calling smp_processor_id() again.

Quoted 7 lines

Here are some performance numbers:

With a run of hackbench 30 times on a single core riscv64 Qemu instance with 1GB memory:

without this patch: mean 36.934
with this patch: mean 36.104 (improved by 2.24%)

I don't really put any stock in QEMU performance numbers for this sort of thing, but for something like this where we're just skipping an expensive call I don't really see a reason to even need performance numbers at all as we can just consider it an obvious optimization.

Quoted 11 lines
---
 arch/riscv/mm/context.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
arch/riscv/mm/context.c +0 -0
index 68aa312fc352..6d445f2888ec 100644
@@ -281,10 +281,9 @@ static inline void set_mm(struct mm_struct *mm, unsigned int cpu)
  * actually performs that local instruction cache flush, which implicitly only

arch/riscv/mm/context.c static inline void set_mm(struct mm_struct *mm, unsigned int cpu) · line 289

Quoted 10 lines
arch/riscv/mm/context.c +1 -2
  * refers to the current hart.
  */
-static inline void flush_icache_deferred(struct mm_struct *mm)
+static inline void flush_icache_deferred(struct mm_struct *mm, unsigned int cpu)
 {
 #ifdef CONFIG_SMP
-	unsigned int cpu = smp_processor_id();
 	cpumask_t *mask = &mm->context.icache_stale_mask;
 
 	if (cpumask_test_cpu(cpu, mask)) {

This proceeds to perform only a local icache flush, which means it will break if any callers use a different CPU number. That large comment at the top alludes to this behavior, but I went ahead and added a line to make that more explicit.

arch/riscv/mm/context.c void switch_mm(struct mm_struct *prev, struct mm_struct *next, · line 323

Quoted 7 lines
arch/riscv/mm/context.c +1 -1
@@ -320,5 +319,5 @@ void switch_mm(struct mm_struct *prev, struct mm_struct *next,
 
 	set_mm(next, cpu);
 
-	flush_icache_deferred(next);
+	flush_icache_deferred(next, cpu);
 }

Thanks, this is on for-next.