Skip to contentSkip to conversation

riscv: Use CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS to set misaligned access speed

PATCH Superseded Charlie Jenkins 3 participants 9 messages Feb 1, 2024 – Feb 2, 2024 RISC-V

v2 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v2 cover riscv: Use CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS to set misaligned access speed

If CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS is enabled, no time needs to be spent in the misaligned access speed probe. Disable the probe in this case and set respective uses to "fast" misaligned accesses. On riscv, this config is selected if RISCV_EFFICIENT_UNALIGNED_ACCESS is selected, which is dependent on NONPORTABLE.

---
Changes in v2:
- Move around definitions to reduce ifdefs (Clément)
- Make RISCV_MISALIGNED depend on !HAVE_EFFICIENT_UNALIGNED_ACCESS
(Clément)
- Link to v1: https://lore.kernel.org/r/20240131-disable_misaligned_probe_config-v1-0-98d155e9cda8@rivosinc.com

---
Charlie Jenkins (2):
riscv: lib: Introduce has_fast_misaligned_access function
riscv: Disable misaligned access probe when CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS

 arch/riscv/Kconfig                               |   1 +
 arch/riscv/include/asm/cpufeature.h              |  22 +-
 arch/riscv/include/asm/misaligned_access_speed.h |  29 +++
 arch/riscv/kernel/Makefile                       |   3 +
 arch/riscv/kernel/cpufeature.c                   | 255 ----------------------
 arch/riscv/kernel/misaligned_access_speed.c      | 265 +++++++++++++++++++++++
 arch/riscv/kernel/sys_hwprobe.c                  |   4 +
 arch/riscv/lib/csum.c                            |   7 +-
 8 files changed, 321 insertions(+), 265 deletions(-)

---
base-commit: 6613476e225e090cc9aad49be7fa504e290dd33d
change-id: 20240131-disable_misaligned_probe_config-043aea375f93

Signature · 1 line
- Charlie
#1 lore raw

PATCH v2 1/2 riscv: lib: Introduce has_fast_misaligned_access function

Create has_fast_misaligned_access to avoid needing to explicitly check the fast_misaligned_access_speed_key static key.

---
 arch/riscv/include/asm/cpufeature.h | 15 ++++++++++-----
 arch/riscv/lib/csum.c               |  7 ++-----
 2 files changed, 12 insertions(+), 10 deletions(-)
arch/riscv/include/asm/cpufeature.h +10 -5
index 5a626ed2c47a..eb3ac304fc42 100644
@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 /*
- * Copyright 2022-2023 Rivos, Inc
+ * Copyright 2022-2024 Rivos, Inc
  */
 
 #ifndef _ASM_CPUFEATURE_H
@@ -28,8 +28,6 @@ struct riscv_isainfo {
 
 DECLARE_PER_CPU(struct riscv_cpuinfo, riscv_cpuinfo);
 
-DECLARE_PER_CPU(long, misaligned_access_speed);
-
 /* Per-cpu ISA extensions. */
 extern struct riscv_isainfo hart_isa[NR_CPUS];
 
@@ -53,6 +51,15 @@ static inline bool check_unaligned_access_emulated(int cpu)
 static inline void unaligned_emulation_finish(void) {}
 #endif
 
+DECLARE_PER_CPU(long, misaligned_access_speed);
+
+DECLARE_STATIC_KEY_FALSE(fast_misaligned_access_speed_key);
+
+static __always_inline bool has_fast_misaligned_accesses(void)
+{
+	return static_branch_likely(&fast_misaligned_access_speed_key);
+}
+
 unsigned long riscv_get_elf_hwcap(void);
 
 struct riscv_isa_ext_data {
@@ -135,6 +142,4 @@ static __always_inline bool riscv_cpu_has_extension_unlikely(int cpu, const unsi
 	return __riscv_isa_extension_available(hart_isa[cpu].isa, ext);
 }
 
-DECLARE_STATIC_KEY_FALSE(fast_misaligned_access_speed_key);
-
 #endif
arch/riscv/lib/csum.c +2 -5
index af3df5274ccb..ea2f668fab71 100644
@@ -3,7 +3,7 @@
  * Checksum library
  *
  * Influenced by arch/arm64/lib/csum.c
- * Copyright (C) 2023 Rivos Inc.
+ * Copyright (C) 2023-2024 Rivos Inc.
  */
 #include <linux/bitops.h>
 #include <linux/compiler.h>
@@ -318,10 +318,7 @@ unsigned int do_csum(const unsigned char *buff, int len)
 	 * branches. The largest chunk of overlap was delegated into the
 	 * do_csum_common function.
 	 */
-	if (static_branch_likely(&fast_misaligned_access_speed_key))
-		return do_csum_no_alignment(buff, len);
-
-	if (((unsigned long)buff & OFFSET_MASK) == 0)
+	if (has_fast_misaligned_accesses() || (((unsigned long)buff & OFFSET_MASK) == 0b101))
 		return do_csum_no_alignment(buff, len);
 
 	return do_csum_with_alignment(buff, len);
Signature · 1 line
2.43.0
#4 lore raw

PATCH v2 2/2 riscv: Disable misaligned access probe when CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS

In reply to Charlie Jenkins

When CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS is selected, the cpus can be set to have fast misaligned access without needing to probe.

---
 arch/riscv/Kconfig                               |   1 +
 arch/riscv/include/asm/cpufeature.h              |   7 +
 arch/riscv/include/asm/misaligned_access_speed.h |  29 +++
 arch/riscv/kernel/Makefile                       |   3 +
 arch/riscv/kernel/cpufeature.c                   | 255 ----------------------
 arch/riscv/kernel/misaligned_access_speed.c      | 265 +++++++++++++++++++++++
 arch/riscv/kernel/sys_hwprobe.c                  |   4 +
 7 files changed, 309 insertions(+), 255 deletions(-)
arch/riscv/Kconfig +1 -0
index bffbd869a068..3223d2d08f74 100644
@@ -690,6 +690,7 @@ config THREAD_SIZE_ORDER
 config RISCV_MISALIGNED
 	bool "Support misaligned load/store traps for kernel and userspace"
 	select SYSCTL_ARCH_UNALIGN_ALLOW
+	depends on !HAVE_EFFICIENT_UNALIGNED_ACCESS
 	default y
 	help
 	  Say Y here if you want the kernel to embed support for misaligned
arch/riscv/include/asm/cpufeature.h +7 -0
index eb3ac304fc42..3a29d8e30e05 100644
@@ -51,6 +51,7 @@ static inline bool check_unaligned_access_emulated(int cpu)
 static inline void unaligned_emulation_finish(void) {}
 #endif
 
+#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
 DECLARE_PER_CPU(long, misaligned_access_speed);
 
 DECLARE_STATIC_KEY_FALSE(fast_misaligned_access_speed_key);
@@ -59,6 +60,12 @@ static __always_inline bool has_fast_misaligned_accesses(void)
 {
 	return static_branch_likely(&fast_misaligned_access_speed_key);
 }
+#else
+static __always_inline bool has_fast_misaligned_accesses(void)
+{
+	return true;
+}
+#endif
 
 unsigned long riscv_get_elf_hwcap(void);
 
arch/riscv/include/asm/misaligned_access_speed.h new file +29 -0
new file mode 100644
index 000000000000..81df2aa6fa6b
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright 2024 Rivos, Inc
+ */
+
+#ifndef _ASM_MISALIGNED_ACCESS_SPEED_H
+#define _ASM_MISALIGNED_ACCESS_SPEED_H
+
+#include <linux/compiler_attributes.h>
+#include <linux/jump_label.h>
+#include <linux/percpu.h>
+#include <linux/types.h>
+
+#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
+DECLARE_PER_CPU(long, misaligned_access_speed);
+
+DECLARE_STATIC_KEY_FALSE(fast_misaligned_access_speed_key);
+
+static __always_inline bool has_fast_misaligned_accesses(void)
+{
+	return static_branch_likely(&fast_misaligned_access_speed_key);
+}
+#else
+static __always_inline bool has_fast_misaligned_accesses(void)
+{
+	return static_branch_likely(&fast_misaligned_access_speed_key);
+}
+#endif
+#endif
arch/riscv/kernel/Makefile +3 -0
index f71910718053..8be7f17da9ab 100644
@@ -62,6 +62,9 @@ obj-y	+= tests/
 obj-$(CONFIG_MMU) += vdso.o vdso/
 
 obj-$(CONFIG_RISCV_MISALIGNED)	+= traps_misaligned.o
+ifneq ($(RISCV_EFFICIENT_UNALIGNED_ACCESS), y)
+obj-y	+= misaligned_access_speed.o
+endif
 obj-$(CONFIG_FPU)		+= fpu.o
 obj-$(CONFIG_RISCV_ISA_V)	+= vector.o
 obj-$(CONFIG_RISCV_ISA_V)	+= kernel_mode_vector.o
arch/riscv/kernel/cpufeature.c +0 -255

285 lines not on this page · raw message

arch/riscv/kernel/misaligned_access_speed.c new file +265 -0

265 lines not on this page · raw message

arch/riscv/kernel/sys_hwprobe.c +4 -0
index a7c56b41efd2..3f1a6edfdb08 100644
@@ -149,6 +149,7 @@ static bool hwprobe_ext0_has(const struct cpumask *cpus, unsigned long ext)
 
 static u64 hwprobe_misaligned(const struct cpumask *cpus)
 {
+#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
 	int cpu;
 	u64 perf = -1ULL;
 
@@ -168,6 +169,9 @@ static u64 hwprobe_misaligned(const struct cpumask *cpus)
 		return RISCV_HWPROBE_MISALIGNED_UNKNOWN;
 
 	return perf;
+#else
+	return RISCV_HWPROBE_MISALIGNED_FAST;
+#endif
 }
 
 static void hwprobe_one_pair(struct riscv_hwprobe *pair,
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH v2 2/2] riscv: Disable misaligned access probe when CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS

Hi Charlie,

Quoted 19 lines · Charlie Jenkins

When CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS is selected, the cpus can be set to have fast misaligned access without needing to probe.

---
 arch/riscv/Kconfig                               |   1 +
 arch/riscv/include/asm/cpufeature.h              |   7 +
 arch/riscv/include/asm/misaligned_access_speed.h |  29 +++
 arch/riscv/kernel/Makefile                       |   3 +
 arch/riscv/kernel/cpufeature.c                   | 255 ----------------------
 arch/riscv/kernel/misaligned_access_speed.c      | 265 +++++++++++++++++++++++
 arch/riscv/kernel/sys_hwprobe.c                  |   4 +
 7 files changed, 309 insertions(+), 255 deletions(-)
arch/riscv/kernel/Makefile +0 -0
index f71910718053..8be7f17da9ab 100644

arch/riscv/kernel/Makefile obj-y += tests/ · line 65

Quoted 4 lines
arch/riscv/kernel/Makefile +1 -0
@@ -62,6 +62,9 @@ obj-y	+= tests/
 obj-$(CONFIG_MMU) += vdso.o vdso/
 
 obj-$(CONFIG_RISCV_MISALIGNED)	+= traps_misaligned.o
+ifneq ($(RISCV_EFFICIENT_UNALIGNED_ACCESS), y)

Should this be CONFIG_RISCV_EFFICIENT_UNALIGNED_ACCESS ?

Quoted 5 lines
arch/riscv/kernel/Makefile +2 -0
+obj-y	+= misaligned_access_speed.o
+endif
 obj-$(CONFIG_FPU)		+= fpu.o
 obj-$(CONFIG_RISCV_ISA_V)	+= vector.o
 obj-$(CONFIG_RISCV_ISA_V)	+= kernel_mode_vector.o
#6 lore raw

Re: [PATCH v2 2/2] riscv: Disable misaligned access probe when CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS

Quoted 24 lines · Elliot Berman

Hi Charlie,

Quoted 21 lines · Charlie Jenkins

When CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS is selected, the cpus can be set to have fast misaligned access without needing to probe.

---
 arch/riscv/Kconfig                               |   1 +
 arch/riscv/include/asm/cpufeature.h              |   7 +
 arch/riscv/include/asm/misaligned_access_speed.h |  29 +++
 arch/riscv/kernel/Makefile                       |   3 +
 arch/riscv/kernel/cpufeature.c                   | 255 ----------------------
 arch/riscv/kernel/misaligned_access_speed.c      | 265 +++++++++++++++++++++++
 arch/riscv/kernel/sys_hwprobe.c                  |   4 +
 7 files changed, 309 insertions(+), 255 deletions(-)
arch/riscv/kernel/Makefile +0 -0
index f71910718053..8be7f17da9ab 100644
@@ -62,6 +62,9 @@ obj-y	+= tests/
 obj-$(CONFIG_MMU) += vdso.o vdso/
 
Quoted 4 lines

arch/riscv/kernel/Makefile obj-y += tests/ · line 65

Quoted 2 lines
arch/riscv/kernel/Makefile +1 -0
 obj-$(CONFIG_RISCV_MISALIGNED)	+= traps_misaligned.o
+ifneq ($(RISCV_EFFICIENT_UNALIGNED_ACCESS), y)

Should this be CONFIG_RISCV_EFFICIENT_UNALIGNED_ACCESS ?

Oh thank you Elliot! Yes it is. I was going back and forth on whether I wanted this to be CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS or CONFIG_RISCV_EFFICIENT_UNALIGNED_ACCESS and I must have mistyped when I was changing it. I settled on CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS everywhere else so this should also use that instead. These two configs are effectively the same since the only way to select CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS is by selecting CONFIG_RISCV_EFFICIENT_UNALIGNED_ACCESS.

- Charlie

Quoted 6 lines
Quoted 6 lines
arch/riscv/kernel/Makefile +2 -0
+obj-y	+= misaligned_access_speed.o
+endif
 obj-$(CONFIG_FPU)		+= fpu.o
 obj-$(CONFIG_RISCV_ISA_V)	+= vector.o
 obj-$(CONFIG_RISCV_ISA_V)	+= kernel_mode_vector.o
#2 lore raw

Re: [PATCH v2 1/2] riscv: lib: Introduce has_fast_misaligned_access function

In reply to Charlie Jenkins

arch/riscv/lib/csum.c unsigned int do_csum(const unsigned char *buff, int len) · line 322

Quoted 3 lines · Charlie Jenkins
arch/riscv/lib/csum.c +1 -1
-	if (((unsigned long)buff & OFFSET_MASK) == 0)
+	if (has_fast_misaligned_accesses() || (((unsigned long)buff & OFFSET_MASK) == 0b101))
 		return do_csum_no_alignment(buff, len);

Why did the 0 change into 0b101?

- Eric

#7 lore raw

Re: [PATCH v2 2/2] riscv: Disable misaligned access probe when CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS

In reply to Charlie Jenkins

arch/riscv/include/asm/cpufeature.h static inline bool check_unaligned_access_emulated(int cpu) · line 54

Quoted 9 lines · Charlie Jenkins
arch/riscv/include/asm/cpufeature.h +1 -0
index eb3ac304fc42..3a29d8e30e05 100644
@@ -51,6 +51,7 @@ static inline bool check_unaligned_access_emulated(int cpu)
 static inline void unaligned_emulation_finish(void) {}
 #endif
 
+#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS

#ifdef..#else..#endif is more readable than #ifndef..#else..#endif.

arch/riscv/include/asm/misaligned_access_speed.h

Quoted 5 lines
arch/riscv/include/asm/misaligned_access_speed.h new file +0 -0
new file mode 100644
index 000000000000..81df2aa6fa6b

This new header file isn't included from anywhere.

arch/riscv/kernel/Makefile obj-y += tests/ · line 67

Quoted 11 lines
arch/riscv/kernel/Makefile +3 -0
index f71910718053..8be7f17da9ab 100644
@@ -62,6 +62,9 @@ obj-y	+= tests/
 obj-$(CONFIG_MMU) += vdso.o vdso/
 
 obj-$(CONFIG_RISCV_MISALIGNED)	+= traps_misaligned.o
+ifneq ($(RISCV_EFFICIENT_UNALIGNED_ACCESS), y)
+obj-y	+= misaligned_access_speed.o
+endif

CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS

Quoted 13 lines
arch/riscv/kernel/sys_hwprobe.c +1 -0
index a7c56b41efd2..3f1a6edfdb08 100644
@@ -149,6 +149,7 @@ static bool hwprobe_ext0_has(const struct cpumask *cpus, unsigned long ext)
 
 static u64 hwprobe_misaligned(const struct cpumask *cpus)
 {
+#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
 	int cpu;
 	u64 perf = -1ULL;
 

arch/riscv/kernel/sys_hwprobe.c static u64 hwprobe_misaligned(const struct cpumask *cpus) · line 174

Quoted 6 lines
arch/riscv/kernel/sys_hwprobe.c +3 -0
@@ -168,6 +169,9 @@ static u64 hwprobe_misaligned(const struct cpumask *cpus)
 		return RISCV_HWPROBE_MISALIGNED_UNKNOWN;
 
 	return perf;
+#else
+	return RISCV_HWPROBE_MISALIGNED_FAST;
+#endif

#ifdef..#else..#endif is more readable than #ifndef..#else..#endif.

- Eric

#3 lore raw

Re: [PATCH v2 1/2] riscv: lib: Introduce has_fast_misaligned_access function

In reply to Eric Biggers
Quoted 8 lines · Eric Biggers

arch/riscv/lib/csum.c unsigned int do_csum(const unsigned char *buff, int len) · line 322

Quoted 3 lines · Charlie Jenkins
arch/riscv/lib/csum.c +1 -1
-	if (((unsigned long)buff & OFFSET_MASK) == 0)
+	if (has_fast_misaligned_accesses() || (((unsigned long)buff & OFFSET_MASK) == 0b101))
 		return do_csum_no_alignment(buff, len);

Why did the 0 change into 0b101?

- Eric

Whoops, thank you.

- Charlie

#8 lore raw

Re: [PATCH v2 2/2] riscv: Disable misaligned access probe when CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS

In reply to Eric Biggers
Quoted 13 lines · Eric Biggers

arch/riscv/include/asm/cpufeature.h static inline bool check_unaligned_access_emulated(int cpu) · line 54

Quoted 9 lines · Charlie Jenkins
arch/riscv/include/asm/cpufeature.h +1 -0
index eb3ac304fc42..3a29d8e30e05 100644
@@ -51,6 +51,7 @@ static inline bool check_unaligned_access_emulated(int cpu)
 static inline void unaligned_emulation_finish(void) {}
 #endif
 
+#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS

#ifdef..#else..#endif is more readable than #ifndef..#else..#endif.

Quoted 8 lines

arch/riscv/include/asm/misaligned_access_speed.h

Quoted 5 lines
arch/riscv/include/asm/misaligned_access_speed.h new file +0 -0
new file mode 100644
index 000000000000..81df2aa6fa6b

This new header file isn't included from anywhere.

I thought I deleted that...

Quoted 29 lines

arch/riscv/kernel/Makefile obj-y += tests/ · line 67

Quoted 11 lines
arch/riscv/kernel/Makefile +3 -0
index f71910718053..8be7f17da9ab 100644
@@ -62,6 +62,9 @@ obj-y	+= tests/
 obj-$(CONFIG_MMU) += vdso.o vdso/
 
 obj-$(CONFIG_RISCV_MISALIGNED)	+= traps_misaligned.o
+ifneq ($(RISCV_EFFICIENT_UNALIGNED_ACCESS), y)
+obj-y	+= misaligned_access_speed.o
+endif

CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS

Quoted 15 lines
arch/riscv/kernel/sys_hwprobe.c +1 -0
index a7c56b41efd2..3f1a6edfdb08 100644
@@ -149,6 +149,7 @@ static bool hwprobe_ext0_has(const struct cpumask *cpus, unsigned long ext)
 
 static u64 hwprobe_misaligned(const struct cpumask *cpus)
 {
+#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
 	int cpu;
 	u64 perf = -1ULL;
 
@@ -168,6 +169,9 @@ static u64 hwprobe_misaligned(const struct cpumask *cpus)
 		return RISCV_HWPROBE_MISALIGNED_UNKNOWN;
 
Quoted 8 lines

arch/riscv/kernel/sys_hwprobe.c static u64 hwprobe_misaligned(const struct cpumask *cpus) · line 174

Quoted 4 lines
arch/riscv/kernel/sys_hwprobe.c +3 -0
 	return perf;
+#else
+	return RISCV_HWPROBE_MISALIGNED_FAST;
+#endif

#ifdef..#else..#endif is more readable than #ifndef..#else..#endif.

- Eric

Thanks for pointing these out, I will send a v3.

- Charlie