Skip to contentSkip to conversation

riscv: Use Kconfig to set unaligned access speed

PATCH Superseded Charlie Jenkins 3 participants 11 messages Mar 2, 2024 – Mar 6, 2024 RISC-V

v6 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v6 cover riscv: Use Kconfig to set unaligned access speed

If the hardware unaligned access speed is known at compile time, it is possible to avoid running the unaligned access speed probe to speedup boot-time.

---
Changes in v6:
- Consolidate Kconfig into 4 options (probe, emulated, slow,
efficient)
- Change the behavior of "emulated" to allow hwprobe to return "slow" if
unaligned accesses are not emulated by the kernel
- With this consolidation, check_unaligned_access_emulated is able to be
moved back into the original file (traps_misaligned.c)
- Link to v5: https://lore.kernel.org/r/20240227-disable_misaligned_probe_config-v5-0-b6853846e27a@rivosinc.com

Changes in v5:
- Clarify Kconfig options from Conor's feedback
- Use "unaligned" instead of "misaligned" in introduced file/function.
This is a bit hard to standardize because the riscv manual says
"misaligned" but the existing Linux configs say "unaligned".
- Link to v4: https://lore.kernel.org/r/20240216-disable_misaligned_probe_config-v4-0-dc01e581c0ac@rivosinc.com

Changes in v4:
- Add additional Kconfig options for the other unaligned access speeds
- Link to v3: https://lore.kernel.org/r/20240202-disable_misaligned_probe_config-v3-0-c44f91f03bb6@rivosinc.com

Changes in v3:
- Revert change to csum (Eric)
- Change ifndefs for ifdefs (Eric)
- Change config in Makefile (Elliot/Eric)
- Link to v2: https://lore.kernel.org/r/20240201-disable_misaligned_probe_config-v2-0-77c368bed7b2@rivosinc.com

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 (4):
riscv: lib: Introduce has_fast_unaligned_access function
riscv: Only check online cpus for emulated accesses
riscv: Decouple emulated unaligned accesses from access speed
riscv: Set unaligned access speed at compile time

 arch/riscv/Kconfig                         |  58 ++++--
 arch/riscv/include/asm/cpufeature.h        |  31 ++--
 arch/riscv/kernel/Makefile                 |   4 +-
 arch/riscv/kernel/cpufeature.c             | 255 --------------------------
 arch/riscv/kernel/sys_hwprobe.c            |  21 +++
 arch/riscv/kernel/traps_misaligned.c       |  22 +--
 arch/riscv/kernel/unaligned_access_speed.c | 282 +++++++++++++++++++++++++++++
 arch/riscv/lib/csum.c                      |   7 +-
 8 files changed, 383 insertions(+), 297 deletions(-)

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

Signature · 1 line
- Charlie
#1 lore raw

PATCH v6 1/4 riscv: lib: Introduce has_fast_unaligned_access function

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

---
 arch/riscv/include/asm/cpufeature.h | 11 ++++++++---
 arch/riscv/kernel/cpufeature.c      |  6 +++---
 arch/riscv/lib/csum.c               |  7 ++-----
 3 files changed, 13 insertions(+), 11 deletions(-)
arch/riscv/include/asm/cpufeature.h +8 -3
index 5a626ed2c47a..466e1f591919 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
@@ -53,6 +53,13 @@ static inline bool check_unaligned_access_emulated(int cpu)
 static inline void unaligned_emulation_finish(void) {}
 #endif
 
+DECLARE_STATIC_KEY_FALSE(fast_unaligned_access_speed_key);
+
+static __always_inline bool has_fast_unaligned_accesses(void)
+{
+	return static_branch_likely(&fast_unaligned_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/kernel/cpufeature.c +3 -3
index 89920f84d0a3..7878cddccc0d 100644
@@ -810,14 +810,14 @@ static void check_unaligned_access_nonboot_cpu(void *param)
 		check_unaligned_access(pages[cpu]);
 }
 
-DEFINE_STATIC_KEY_FALSE(fast_misaligned_access_speed_key);
+DEFINE_STATIC_KEY_FALSE(fast_unaligned_access_speed_key);
 
 static void modify_unaligned_access_branches(cpumask_t *mask, int weight)
 {
 	if (cpumask_weight(mask) == weight)
-		static_branch_enable_cpuslocked(&fast_misaligned_access_speed_key);
+		static_branch_enable_cpuslocked(&fast_unaligned_access_speed_key);
 	else
-		static_branch_disable_cpuslocked(&fast_misaligned_access_speed_key);
+		static_branch_disable_cpuslocked(&fast_unaligned_access_speed_key);
 }
 
 static void set_unaligned_access_static_branches_except_cpu(int cpu)
arch/riscv/lib/csum.c +2 -5
index af3df5274ccb..7178e0acfa22 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_unaligned_accesses() || (((unsigned long)buff & OFFSET_MASK) == 0))
 		return do_csum_no_alignment(buff, len);
 
 	return do_csum_with_alignment(buff, len);
Signature · 1 line
2.43.2
#2 lore raw

PATCH v6 2/4 riscv: Only check online cpus for emulated accesses

In reply to Charlie Jenkins

The unaligned access checker only sets valid values for online cpus. Check for these values on online cpus rather than on present cpus.

Fixes 71c54b3d169d ("riscv: report misaligned accesses emulation to hwprobe")
---
 arch/riscv/kernel/traps_misaligned.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/traps_misaligned.c +1 -1
index 8ded225e8c5b..c2ed4e689bf9 100644
@@ -632,7 +632,7 @@ void unaligned_emulation_finish(void)
 	 * accesses emulated since tasks requesting such control can run on any
 	 * CPU.
 	 */
-	for_each_present_cpu(cpu) {
+	for_each_online_cpu(cpu) {
 		if (per_cpu(misaligned_access_speed, cpu) !=
 					RISCV_HWPROBE_MISALIGNED_EMULATED) {
 			return;
Signature · 1 line
2.43.2
#4 lore raw

PATCH v6 3/4 riscv: Decouple emulated unaligned accesses from access speed

In reply to Charlie Jenkins

Detecting if a system traps into the kernel on an unaligned access can be performed separately from checking the speed of unaligned accesses. This decoupling will make it possible to selectively enable or disable each of these checks as is done in the following patch.

---
 arch/riscv/include/asm/cpufeature.h  |  2 +-
 arch/riscv/kernel/cpufeature.c       | 25 +++++++++++++++++++++----
 arch/riscv/kernel/traps_misaligned.c | 20 +++++++-------------
 3 files changed, 29 insertions(+), 18 deletions(-)
arch/riscv/include/asm/cpufeature.h +1 -1
index 466e1f591919..6fec91845aa0 100644
@@ -37,7 +37,7 @@ void riscv_user_isa_enable(void);
 
 #ifdef CONFIG_RISCV_MISALIGNED
 bool unaligned_ctl_available(void);
-bool check_unaligned_access_emulated(int cpu);
+bool check_unaligned_access_emulated_all_cpus(void);
 void unaligned_emulation_finish(void);
 #else
 static inline bool unaligned_ctl_available(void)
arch/riscv/kernel/cpufeature.c +21 -4
index 7878cddccc0d..abb3a2f53106 100644
@@ -719,7 +719,8 @@ static int check_unaligned_access(void *param)
 	void *src;
 	long speed = RISCV_HWPROBE_MISALIGNED_SLOW;
 
-	if (check_unaligned_access_emulated(cpu))
+	if (IS_ENABLED(CONFIG_RISCV_MISALIGNED) &&
+	    per_cpu(misaligned_access_speed, cpu) != RISCV_HWPROBE_MISALIGNED_UNKNOWN)
 		return 0;
 
 	/* Make an unaligned destination buffer. */
@@ -896,8 +897,8 @@ static int riscv_offline_cpu(unsigned int cpu)
 	return 0;
 }
 
-/* Measure unaligned access on all CPUs present at boot in parallel. */
-static int check_unaligned_access_all_cpus(void)
+/* Measure unaligned access speed on all CPUs present at boot in parallel. */
+static int check_unaligned_access_speed_all_cpus(void)
 {
 	unsigned int cpu;
 	unsigned int cpu_count = num_possible_cpus();
@@ -935,7 +936,6 @@ static int check_unaligned_access_all_cpus(void)
 				  riscv_online_cpu, riscv_offline_cpu);
 
 out:
-	unaligned_emulation_finish();
 	for_each_cpu(cpu, cpu_online_mask) {
 		if (bufs[cpu])
 			__free_pages(bufs[cpu], MISALIGNED_BUFFER_ORDER);
@@ -945,6 +945,23 @@ static int check_unaligned_access_all_cpus(void)
 	return 0;
 }
 
+#ifdef CONFIG_RISCV_MISALIGNED
+static int check_unaligned_access_all_cpus(void)
+{
+	bool all_cpus_emulated = check_unaligned_access_emulated_all_cpus();
+
+	if (!all_cpus_emulated)
+		return check_unaligned_access_speed_all_cpus();
+
+	return 0;
+}
+#else
+static int check_unaligned_access_all_cpus(void)
+{
+	return check_unaligned_access_speed_all_cpus();
+}
+#endif
+
 arch_initcall(check_unaligned_access_all_cpus);
 
 void riscv_user_isa_enable(void)
arch/riscv/kernel/traps_misaligned.c +7 -13
index c2ed4e689bf9..1e3cec3f5d93 100644
@@ -596,7 +596,7 @@ int handle_misaligned_store(struct pt_regs *regs)
 	return 0;
 }
 
-bool check_unaligned_access_emulated(int cpu)
+static bool check_unaligned_access_emulated(int cpu)
 {
 	long *mas_ptr = per_cpu_ptr(&misaligned_access_speed, cpu);
 	unsigned long tmp_var, tmp_val;
@@ -623,22 +623,16 @@ bool check_unaligned_access_emulated(int cpu)
 	return misaligned_emu_detected;
 }
 
-void unaligned_emulation_finish(void)
+bool check_unaligned_access_emulated_all_cpus(void)
 {
 	int cpu;
 
-	/*
-	 * We can only support PR_UNALIGN controls if all CPUs have misaligned
-	 * accesses emulated since tasks requesting such control can run on any
-	 * CPU.
-	 */
-	for_each_online_cpu(cpu) {
-		if (per_cpu(misaligned_access_speed, cpu) !=
-					RISCV_HWPROBE_MISALIGNED_EMULATED) {
-			return;
-		}
-	}
+	for_each_online_cpu(cpu)
+		if (check_unaligned_access_emulated(cpu))
+			return false;
+
 	unaligned_ctl = true;
+	return true;
 }
 
 bool unaligned_ctl_available(void)
Signature · 1 line
2.43.2
#7 lore raw

PATCH v6 4/4 riscv: Set unaligned access speed at compile time

In reply to Charlie Jenkins

Introduce Kconfig options to set the kernel unaligned access support. These options provide a non-portable alternative to the runtime unaligned access probe.

To support this, the unaligned access probing code is moved into it's own file and gated behind a new RISCV_PROBE_UNALIGNED_ACCESS_SUPPORT option.

---
 arch/riscv/Kconfig                         |  58 ++++--
 arch/riscv/include/asm/cpufeature.h        |  26 +--
 arch/riscv/kernel/Makefile                 |   4 +-
 arch/riscv/kernel/cpufeature.c             | 272 ----------------------------
 arch/riscv/kernel/sys_hwprobe.c            |  21 +++
 arch/riscv/kernel/traps_misaligned.c       |   2 +
 arch/riscv/kernel/unaligned_access_speed.c | 282 +++++++++++++++++++++++++++++
 7 files changed, 369 insertions(+), 296 deletions(-)
arch/riscv/Kconfig +46 -12
index bffbd869a068..60b6de35599d 100644
@@ -688,27 +688,61 @@ config THREAD_SIZE_ORDER
 	  affects irq stack size, which is equal to thread stack size.
 
 config RISCV_MISALIGNED
-	bool "Support misaligned load/store traps for kernel and userspace"
+	bool
 	select SYSCTL_ARCH_UNALIGN_ALLOW
-	default y
 	help
-	  Say Y here if you want the kernel to embed support for misaligned
-	  load/store for both kernel and userspace. When disable, misaligned
-	  accesses will generate SIGBUS in userspace and panic in kernel.
+	  Embed support for misaligned load/store for both kernel and userspace.
+	  When disabled, misaligned accesses will generate SIGBUS in userspace
+	  and panic in kernel.
+
+choice
+	prompt "Unaligned Accesses Support"
+	default RISCV_PROBE_UNALIGNED_ACCESS
+	help
+	  This selects the hardware support for unaligned accesses. This
+	  information is used by the kernel to perform optimizations. It is also
+	  exposed to user space via the hwprobe syscall. The hardware will be
+	  probed at boot by default.
+
+config RISCV_PROBE_UNALIGNED_ACCESS
+	bool "Probe for hardware unaligned access support"
+	select RISCV_MISALIGNED
+	help
+	  During boot, the kernel will run a series of tests to determine the
+	  speed of unaligned accesses. This probing will dynamically determine
+	  the speed of unaligned accesses on the boot hardware. The kernel will
+	  also check if unaligned memory accesses will trap into the kernel and
+	  handle such traps accordingly.
+
+config RISCV_EMULATED_UNALIGNED_ACCESS
+	bool "Assume the system expects emulated unaligned memory accesses"
+	select RISCV_MISALIGNED
+	help
+	  Assume that the system expects unaligned memory accesses to be
+	  emulated. The kernel will check if unaligned memory accesses will
+	  trap into the kernel and handle such traps accordingly.
+
+config RISCV_SLOW_UNALIGNED_ACCESS
+	bool "Assume the system supports slow unaligned memory accesses"
+	depends on NONPORTABLE
+	help
+	  Assume that the system supports slow unaligned memory accesses. The
+	  kernel may not be able to run at all on systems that do not support
+	  unaligned memory accesses.
 
 config RISCV_EFFICIENT_UNALIGNED_ACCESS
-	bool "Assume the CPU supports fast unaligned memory accesses"
+	bool "Assume the system supports fast unaligned memory accesses"
 	depends on NONPORTABLE
 	select DCACHE_WORD_ACCESS if MMU
 	select HAVE_EFFICIENT_UNALIGNED_ACCESS
 	help
-	  Say Y here if you want the kernel to assume that the CPU supports
-	  efficient unaligned memory accesses.  When enabled, this option
-	  improves the performance of the kernel on such CPUs.  However, the
-	  kernel will run much more slowly, or will not be able to run at all,
-	  on CPUs that do not support efficient unaligned memory accesses.
+	  Assume that the system supports fast unaligned memory accesses. When
+	  enabled, this option improves the performance of the kernel on such
+	  systems.  However, the kernel will run much more slowly, or will not
+	  be able to run at all, on systems that do not support efficient
+	  unaligned memory accesses.
 
-	  If unsure what to do here, say N.
+endchoice
 
 endmenu # "Platform type"
 
arch/riscv/include/asm/cpufeature.h +15 -11
index 6fec91845aa0..357972cd8f82 100644
@@ -28,37 +28,41 @@ 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];
 
 void riscv_user_isa_enable(void);
 
-#ifdef CONFIG_RISCV_MISALIGNED
-bool unaligned_ctl_available(void);
+#if defined(CONFIG_RISCV_MISALIGNED)
 bool check_unaligned_access_emulated_all_cpus(void);
 void unaligned_emulation_finish(void);
+bool unaligned_ctl_available(void);
+DECLARE_PER_CPU(long, misaligned_access_speed);
 #else
 static inline bool unaligned_ctl_available(void)
 {
 	return false;
 }
-
-static inline bool check_unaligned_access_emulated(int cpu)
-{
-	return false;
-}
-
-static inline void unaligned_emulation_finish(void) {}
 #endif
 
+#if defined(CONFIG_RISCV_PROBE_UNALIGNED_ACCESS)
 DECLARE_STATIC_KEY_FALSE(fast_unaligned_access_speed_key);
 
 static __always_inline bool has_fast_unaligned_accesses(void)
 {
 	return static_branch_likely(&fast_unaligned_access_speed_key);
 }
+#elif defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
+static __always_inline bool has_fast_unaligned_accesses(void)
+{
+	return true;
+}
+#else
+static __always_inline bool has_fast_unaligned_accesses(void)
+{
+	return false;
+}
+#endif
 
 unsigned long riscv_get_elf_hwcap(void);
 
arch/riscv/kernel/Makefile +3 -1
index f71910718053..c8085126a6f9 100644
@@ -38,7 +38,6 @@ extra-y += vmlinux.lds
 obj-y	+= head.o
 obj-y	+= soc.o
 obj-$(CONFIG_RISCV_ALTERNATIVE) += alternative.o
-obj-y	+= copy-unaligned.o
 obj-y	+= cpu.o
 obj-y	+= cpufeature.o
 obj-y	+= entry.o
@@ -62,6 +61,9 @@ obj-y	+= tests/
 obj-$(CONFIG_MMU) += vdso.o vdso/
 
 obj-$(CONFIG_RISCV_MISALIGNED)	+= traps_misaligned.o
+obj-$(CONFIG_RISCV_MISALIGNED)	+= unaligned_access_speed.o
+obj-$(CONFIG_RISCV_PROBE_UNALIGNED_ACCESS)	+= copy-unaligned.o
+
 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 -272

302 lines not on this page · raw message

arch/riscv/kernel/sys_hwprobe.c +21 -0
index a7c56b41efd2..dad02f5faec3 100644
@@ -147,8 +147,10 @@ static bool hwprobe_ext0_has(const struct cpumask *cpus, unsigned long ext)
 	return (pair.value & ext);
 }
 
+#if defined(CONFIG_RISCV_PROBE_UNALIGNED_ACCESS)
 static u64 hwprobe_misaligned(const struct cpumask *cpus)
 {
+	return RISCV_HWPROBE_MISALIGNED_FAST;
 	int cpu;
 	u64 perf = -1ULL;
 
@@ -169,6 +171,25 @@ static u64 hwprobe_misaligned(const struct cpumask *cpus)
 
 	return perf;
 }
+#elif defined(CONFIG_RISCV_EMULATED_UNALIGNED_ACCESS)
+static u64 hwprobe_misaligned(const struct cpumask *cpus)
+{
+	if (unaligned_ctl_available())
+		return RISCV_HWPROBE_MISALIGNED_EMULATED;
+	else
+		return RISCV_HWPROBE_MISALIGNED_SLOW;
+}
+#elif defined(CONFIG_RISCV_SLOW_UNALIGNED_ACCESS)
+static u64 hwprobe_misaligned(const struct cpumask *cpus)
+{
+	return RISCV_HWPROBE_MISALIGNED_SLOW;
+}
+#elif defined(CONFIG_RISCV_EFFICIENT_UNALIGNED_ACCESS)
+static u64 hwprobe_misaligned(const struct cpumask *cpus)
+{
+	return RISCV_HWPROBE_MISALIGNED_FAST;
+}
+#endif
 
 static void hwprobe_one_pair(struct riscv_hwprobe *pair,
 			     const struct cpumask *cpus)
arch/riscv/kernel/traps_misaligned.c +2 -0
index 1e3cec3f5d93..6153158adcb7 100644
@@ -413,7 +413,9 @@ int handle_misaligned_load(struct pt_regs *regs)
 
 	perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, addr);
 
+#ifdef CONFIG_RISCV_PROBE_UNALIGNED_ACCESS
 	*this_cpu_ptr(&misaligned_access_speed) = RISCV_HWPROBE_MISALIGNED_EMULATED;
+#endif
 
 	if (!unaligned_enabled)
 		return -1;
arch/riscv/kernel/unaligned_access_speed.c new file +282 -0

282 lines not on this page · raw message

Signature · 1 line
2.43.2
Reviewed-by on v6 2/4
#3 lore raw

Re: [PATCH v6 2/4] riscv: Only check online cpus for emulated accesses

In reply to Charlie Jenkins
Quoted 5 lines · Charlie Jenkins

The unaligned access checker only sets valid values for online cpus. Check for these values on online cpus rather than on present cpus.

Fixes 71c54b3d169d ("riscv: report misaligned accesses emulation to hwprobe")

Cheers,
Conor.

Quoted 9 lines
---
 arch/riscv/kernel/traps_misaligned.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/traps_misaligned.c +0 -0
index 8ded225e8c5b..c2ed4e689bf9 100644

arch/riscv/kernel/traps_misaligned.c void unaligned_emulation_finish(void) · line 638

Quoted 8 lines
arch/riscv/kernel/traps_misaligned.c +1 -1
@@ -632,7 +632,7 @@ void unaligned_emulation_finish(void)
 	 * accesses emulated since tasks requesting such control can run on any
 	 * CPU.
 	 */
-	for_each_present_cpu(cpu) {
+	for_each_online_cpu(cpu) {
 		if (per_cpu(misaligned_access_speed, cpu) !=
 					RISCV_HWPROBE_MISALIGNED_EMULATED) {
 			return;

I went looking to see what the practical differences were between "present" and "possible", cos I'd never really seen much code using "present". Turns out present and possible are essentially the same on riscv. TIL.

#5 lore raw

Re: [PATCH v6 3/4] riscv: Decouple emulated unaligned accesses from access speed

In reply to Charlie Jenkins

arch/riscv/kernel/traps_misaligned.c bool check_unaligned_access_emulated(int cpu) · line 634

Quoted 10 lines · Charlie Jenkins
arch/riscv/kernel/traps_misaligned.c +1 -6
-void unaligned_emulation_finish(void)
+bool check_unaligned_access_emulated_all_cpus(void)
 {
 	int cpu;
 
-	/*
-	 * We can only support PR_UNALIGN controls if all CPUs have misaligned
-	 * accesses emulated since tasks requesting such control can run on any
-	 * CPU.
-	 */

Why was this comment removed? This patch doesn't change the situations in which PR_UNALIGN is allowed, right?

Quoted 13 lines
arch/riscv/kernel/traps_misaligned.c +5 -6
-	for_each_online_cpu(cpu) {
-		if (per_cpu(misaligned_access_speed, cpu) !=
-					RISCV_HWPROBE_MISALIGNED_EMULATED) {
-			return;
-		}
-	}
+	for_each_online_cpu(cpu)
+		if (check_unaligned_access_emulated(cpu))
+			return false;
+
 	unaligned_ctl = true;
+	return true;
 }
#8 lore raw

Re: [PATCH v6 4/4] riscv: Set unaligned access speed at compile time

In reply to Charlie Jenkins

Hey,

Quoted 28 lines · Charlie Jenkins

Introduce Kconfig options to set the kernel unaligned access support. These options provide a non-portable alternative to the runtime unaligned access probe.

To support this, the unaligned access probing code is moved into it's own file and gated behind a new RISCV_PROBE_UNALIGNED_ACCESS_SUPPORT option.

---
 arch/riscv/Kconfig                         |  58 ++++--
 arch/riscv/include/asm/cpufeature.h        |  26 +--
 arch/riscv/kernel/Makefile                 |   4 +-
 arch/riscv/kernel/cpufeature.c             | 272 ----------------------------
 arch/riscv/kernel/sys_hwprobe.c            |  21 +++
 arch/riscv/kernel/traps_misaligned.c       |   2 +
 arch/riscv/kernel/unaligned_access_speed.c | 282 +++++++++++++++++++++++++++++
 7 files changed, 369 insertions(+), 296 deletions(-)
arch/riscv/Kconfig +0 -1
index bffbd869a068..60b6de35599d 100644
@@ -688,27 +688,61 @@ config THREAD_SIZE_ORDER
 	  affects irq stack size, which is equal to thread stack size.
 
 config RISCV_MISALIGNED
-	bool "Support misaligned load/store traps for kernel and userspace"

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 696

Quoted 10 lines
arch/riscv/Kconfig +4 -4
+	bool
 	select SYSCTL_ARCH_UNALIGN_ALLOW
-	default y
 	help
-	  Say Y here if you want the kernel to embed support for misaligned
-	  load/store for both kernel and userspace. When disable, misaligned
-	  accesses will generate SIGBUS in userspace and panic in kernel.
+	  Embed support for misaligned load/store for both kernel and userspace.
+	  When disabled, misaligned accesses will generate SIGBUS in userspace
+	  and panic in kernel.

"in the kernel".

arch/riscv/Kconfig config THREAD_SIZE_ORDER

Quoted 7 lines
arch/riscv/Kconfig +6 -0
+
+choice
+	prompt "Unaligned Accesses Support"
+	default RISCV_PROBE_UNALIGNED_ACCESS
+	help
 
+	  This selects the hardware support for unaligned accesses. This

"This determines what level of support for..."

arch/riscv/Kconfig config THREAD_SIZE_ORDER

Quoted 11 lines
arch/riscv/Kconfig +11 -0
+	  information is used by the kernel to perform optimizations. It is also
+	  exposed to user space via the hwprobe syscall. The hardware will be
+	  probed at boot by default.
+
+config RISCV_PROBE_UNALIGNED_ACCESS
+	bool "Probe for hardware unaligned access support"
+	select RISCV_MISALIGNED
+	help
+	  During boot, the kernel will run a series of tests to determine the
+	  speed of unaligned accesses. This probing will dynamically determine
+	  the speed of unaligned accesses on the boot hardware.

"on the underlying system"?

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 715

Quoted 3 lines

The kernel will

arch/riscv/Kconfig +2 -0
+	  also check if unaligned memory accesses will trap into the kernel and
+	  handle such traps accordingly.

I think I would phrase this to be more understandable to users. I think we need to explain why it would trap and what we will do. Maybe something like: "if unaligned memory accesses trap into the kernel as they are not supported by the system, the kernel will emulate the unaligned accesses to preserve the UABI".

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 723

Quoted 7 lines
arch/riscv/Kconfig +7 -0
+config RISCV_EMULATED_UNALIGNED_ACCESS
+	bool "Assume the system expects emulated unaligned memory accesses"
+	select RISCV_MISALIGNED
+	help
+	  Assume that the system expects unaligned memory accesses to be
+	  emulated. The kernel will check if unaligned memory accesses will
+	  trap into the kernel and handle such traps accordingly.

I guess the same suggestion applies here, but I think the description here isn't quite accurate. This option is basically the same as above, but without the speed test, right? It doesn't actually assume emulation is required at all, in fact the assumption we make is that if the hardware supports unaligned access that access is slow.

I think I'd do:
```
boot "Emulate unaligned access where system support is missing" help
If unaligned accesses trap into the kernel as they are not supported by the system, the kernel will emulate the unaligned accesses to preserve the UABI. When the underlying system does support unaligned accesses, probing at boot is not done and unaligned accesses are assumed to be slow.

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 731

Quoted 7 lines
arch/riscv/Kconfig +7 -0
+config RISCV_SLOW_UNALIGNED_ACCESS
+	bool "Assume the system supports slow unaligned memory accesses"
+	depends on NONPORTABLE
+	help
+	  Assume that the system supports slow unaligned memory accesses. The
+	  kernel may not be able to run at all on systems that do not support
+	  unaligned memory accesses.

...and userspace programs cannot use unaligned access either, I think that is worth mentioning.

Quoted 32 lines

config RISCV_EFFICIENT_UNALIGNED_ACCESS

arch/riscv/Kconfig +9 -7
-	bool "Assume the CPU supports fast unaligned memory accesses"
+	bool "Assume the system supports fast unaligned memory accesses"
 	depends on NONPORTABLE
 	select DCACHE_WORD_ACCESS if MMU
 	select HAVE_EFFICIENT_UNALIGNED_ACCESS
 	help
-	  Say Y here if you want the kernel to assume that the CPU supports
-	  efficient unaligned memory accesses.  When enabled, this option
-	  improves the performance of the kernel on such CPUs.  However, the
-	  kernel will run much more slowly, or will not be able to run at all,
-	  on CPUs that do not support efficient unaligned memory accesses.
+	  Assume that the system supports fast unaligned memory accesses. When
+	  enabled, this option improves the performance of the kernel on such
+	  systems.  However, the kernel will run much more slowly, or will not
+	  be able to run at all, on systems that do not support efficient
+	  unaligned memory accesses.
 
-	  If unsure what to do here, say N.
+endchoice
 
 endmenu # "Platform type"
 
+#if defined(CONFIG_RISCV_PROBE_UNALIGNED_ACCESS)
 DECLARE_STATIC_KEY_FALSE(fast_unaligned_access_speed_key);
 
 static __always_inline bool has_fast_unaligned_accesses(void)
 {
 	return static_branch_likely(&fast_unaligned_access_speed_key);
 }
+#elif defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)

arch/riscv/Kconfig config THREAD_SIZE_ORDER

Quoted 10 lines
arch/riscv/Kconfig +10 -0
+static __always_inline bool has_fast_unaligned_accesses(void)
+{
+	return true;
+}
+#else
+static __always_inline bool has_fast_unaligned_accesses(void)
+{
+	return false;
+}
+#endif

These tree could just be one function with if(IS_ENABLED), whatever code gets made dead should be optimised out.

arch/riscv/kernel/sys_hwprobe.c static bool hwprobe_ext0_has(const struct cpumask *cpus, unsigned long ext) · line 153

Quoted 12 lines
arch/riscv/kernel/sys_hwprobe.c +2 -0
index a7c56b41efd2..dad02f5faec3 100644
@@ -147,8 +147,10 @@ static bool hwprobe_ext0_has(const struct cpumask *cpus, unsigned long ext)
 	return (pair.value & ext);
 }
 
+#if defined(CONFIG_RISCV_PROBE_UNALIGNED_ACCESS)
 static u64 hwprobe_misaligned(const struct cpumask *cpus)
 {
+	return RISCV_HWPROBE_MISALIGNED_FAST;

This hack is still here.

Quoted 17 lines
arch/riscv/kernel/sys_hwprobe.c +9 -0
 	int cpu;
 	u64 perf = -1ULL;
 
@@ -169,6 +171,25 @@ static u64 hwprobe_misaligned(const struct cpumask *cpus)
 
 	return perf;
 }
 
+#elif defined(CONFIG_RISCV_EMULATED_UNALIGNED_ACCESS)
+static u64 hwprobe_misaligned(const struct cpumask *cpus)
+{
+	if (unaligned_ctl_available())
+		return RISCV_HWPROBE_MISALIGNED_EMULATED;
+	else
+		return RISCV_HWPROBE_MISALIGNED_SLOW;
+}
+#elif defined(CONFIG_RISCV_SLOW_UNALIGNED_ACCESS)

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

Quoted 10 lines
arch/riscv/kernel/sys_hwprobe.c +10 -0
+static u64 hwprobe_misaligned(const struct cpumask *cpus)
+{
+	return RISCV_HWPROBE_MISALIGNED_SLOW;
+}
+#elif defined(CONFIG_RISCV_EFFICIENT_UNALIGNED_ACCESS)
+static u64 hwprobe_misaligned(const struct cpumask *cpus)
+{
+	return RISCV_HWPROBE_MISALIGNED_FAST;
+}
+#endif

Same applies to these three functions.

Thanks,
Conor.

#6 lore raw

Re: [PATCH v6 3/4] riscv: Decouple emulated unaligned accesses from access speed

In reply to Conor Dooley
Quoted 7 lines · Conor Dooley
Quoted 5 lines · Charlie Jenkins
arch/riscv/kernel/traps_misaligned.c +1 -1
-void unaligned_emulation_finish(void)
+bool check_unaligned_access_emulated_all_cpus(void)
 {
 	int cpu;
Quoted 8 lines

arch/riscv/kernel/traps_misaligned.c bool check_unaligned_access_emulated(int cpu) · line 634

Quoted 5 lines
arch/riscv/kernel/traps_misaligned.c +0 -5
-	/*
-	 * We can only support PR_UNALIGN controls if all CPUs have misaligned
-	 * accesses emulated since tasks requesting such control can run on any
-	 * CPU.
-	 */

Why was this comment removed? This patch doesn't change the situations in which PR_UNALIGN is allowed, right?

I'll add it back, that was unintentional. Thank you.

- Charlie

Quoted 15 lines
Quoted 14 lines
arch/riscv/kernel/traps_misaligned.c +5 -6
-	for_each_online_cpu(cpu) {
-		if (per_cpu(misaligned_access_speed, cpu) !=
-					RISCV_HWPROBE_MISALIGNED_EMULATED) {
-			return;
-		}
-	}
+	for_each_online_cpu(cpu)
+		if (check_unaligned_access_emulated(cpu))
+			return false;
+
 	unaligned_ctl = true;
+	return true;
 }
#9 lore raw

Re: [PATCH v6 4/4] riscv: Set unaligned access speed at compile time

In reply to Conor Dooley
Quoted 92 lines · Conor Dooley

Hey,

Quoted 28 lines · Charlie Jenkins

Introduce Kconfig options to set the kernel unaligned access support. These options provide a non-portable alternative to the runtime unaligned access probe.

To support this, the unaligned access probing code is moved into it's own file and gated behind a new RISCV_PROBE_UNALIGNED_ACCESS_SUPPORT option.

---
 arch/riscv/Kconfig                         |  58 ++++--
 arch/riscv/include/asm/cpufeature.h        |  26 +--
 arch/riscv/kernel/Makefile                 |   4 +-
 arch/riscv/kernel/cpufeature.c             | 272 ----------------------------
 arch/riscv/kernel/sys_hwprobe.c            |  21 +++
 arch/riscv/kernel/traps_misaligned.c       |   2 +
 arch/riscv/kernel/unaligned_access_speed.c | 282 +++++++++++++++++++++++++++++
 7 files changed, 369 insertions(+), 296 deletions(-)
arch/riscv/Kconfig +0 -1
index bffbd869a068..60b6de35599d 100644
@@ -688,27 +688,61 @@ config THREAD_SIZE_ORDER
 	  affects irq stack size, which is equal to thread stack size.
 
 config RISCV_MISALIGNED
-	bool "Support misaligned load/store traps for kernel and userspace"

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 696

Quoted 10 lines
arch/riscv/Kconfig +4 -4
+	bool
 	select SYSCTL_ARCH_UNALIGN_ALLOW
-	default y
 	help
-	  Say Y here if you want the kernel to embed support for misaligned
-	  load/store for both kernel and userspace. When disable, misaligned
-	  accesses will generate SIGBUS in userspace and panic in kernel.
+	  Embed support for misaligned load/store for both kernel and userspace.
+	  When disabled, misaligned accesses will generate SIGBUS in userspace
+	  and panic in kernel.

"in the kernel".

arch/riscv/Kconfig config THREAD_SIZE_ORDER

Quoted 7 lines
arch/riscv/Kconfig +6 -0
+
+choice
+	prompt "Unaligned Accesses Support"
+	default RISCV_PROBE_UNALIGNED_ACCESS
+	help
 
+	  This selects the hardware support for unaligned accesses. This

"This determines what level of support for..."

arch/riscv/Kconfig config THREAD_SIZE_ORDER

Quoted 11 lines
arch/riscv/Kconfig +11 -0
+	  information is used by the kernel to perform optimizations. It is also
+	  exposed to user space via the hwprobe syscall. The hardware will be
+	  probed at boot by default.
+
+config RISCV_PROBE_UNALIGNED_ACCESS
+	bool "Probe for hardware unaligned access support"
+	select RISCV_MISALIGNED
+	help
+	  During boot, the kernel will run a series of tests to determine the
+	  speed of unaligned accesses. This probing will dynamically determine
+	  the speed of unaligned accesses on the boot hardware.

"on the underlying system"?

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 715

Quoted 3 lines

The kernel will

arch/riscv/Kconfig +2 -0
+	  also check if unaligned memory accesses will trap into the kernel and
+	  handle such traps accordingly.

I think I would phrase this to be more understandable to users. I think we need to explain why it would trap and what we will do. Maybe something like: "if unaligned memory accesses trap into the kernel as they are not supported by the system, the kernel will emulate the unaligned accesses to preserve the UABI".

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 723

Quoted 7 lines
arch/riscv/Kconfig +7 -0
+config RISCV_EMULATED_UNALIGNED_ACCESS
+	bool "Assume the system expects emulated unaligned memory accesses"
+	select RISCV_MISALIGNED
+	help
+	  Assume that the system expects unaligned memory accesses to be
+	  emulated. The kernel will check if unaligned memory accesses will
+	  trap into the kernel and handle such traps accordingly.

I guess the same suggestion applies here, but I think the description here isn't quite accurate. This option is basically the same as above, but without the speed test, right? It doesn't actually assume emulation is required at all, in fact the assumption we make is that if the hardware supports unaligned access that access is slow.

Quoted 9 lines

I think I'd do:
```
boot "Emulate unaligned access where system support is missing" help
If unaligned accesses trap into the kernel as they are not supported by the system, the kernel will emulate the unaligned accesses to preserve the UABI. When the underlying system does support unaligned accesses, probing at boot is not done and unaligned accesses are assumed to be slow.

Great suggestions thank you. I think I will change up the second sentence a little bit to be "When the underlying system does support unaligned accesses, the unaligned accesses are assumed to be slow."

Quoted 49 lines

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 731

Quoted 8 lines
arch/riscv/Kconfig +7 -0
+config RISCV_SLOW_UNALIGNED_ACCESS
+	bool "Assume the system supports slow unaligned memory accesses"
+	depends on NONPORTABLE
+	help
+	  Assume that the system supports slow unaligned memory accesses. The
+	  kernel may not be able to run at all on systems that do not support
+	  unaligned memory accesses.

...and userspace programs cannot use unaligned access either, I think that is worth mentioning.

Quoted 37 lines

config RISCV_EFFICIENT_UNALIGNED_ACCESS

arch/riscv/Kconfig +14 -7
-	bool "Assume the CPU supports fast unaligned memory accesses"
+	bool "Assume the system supports fast unaligned memory accesses"
 	depends on NONPORTABLE
 	select DCACHE_WORD_ACCESS if MMU
 	select HAVE_EFFICIENT_UNALIGNED_ACCESS
 	help
-	  Say Y here if you want the kernel to assume that the CPU supports
-	  efficient unaligned memory accesses.  When enabled, this option
-	  improves the performance of the kernel on such CPUs.  However, the
-	  kernel will run much more slowly, or will not be able to run at all,
-	  on CPUs that do not support efficient unaligned memory accesses.
+	  Assume that the system supports fast unaligned memory accesses. When
+	  enabled, this option improves the performance of the kernel on such
+	  systems.  However, the kernel will run much more slowly, or will not
+	  be able to run at all, on systems that do not support efficient
+	  unaligned memory accesses.
 
-	  If unsure what to do here, say N.
+endchoice
 
 endmenu # "Platform type"
 
+#if defined(CONFIG_RISCV_PROBE_UNALIGNED_ACCESS)
 DECLARE_STATIC_KEY_FALSE(fast_unaligned_access_speed_key);
 
 static __always_inline bool has_fast_unaligned_accesses(void)
 {
 	return static_branch_likely(&fast_unaligned_access_speed_key);
 }
+#elif defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
+static __always_inline bool has_fast_unaligned_accesses(void)
+{
+	return true;
+}
+#else
Quoted 9 lines

arch/riscv/Kconfig

Quoted 5 lines
arch/riscv/Kconfig +5 -0
+static __always_inline bool has_fast_unaligned_accesses(void)
+{
+	return false;
+}
+#endif

These tree could just be one function with if(IS_ENABLED), whatever code gets made dead should be optimised out.

Sure, will do.

Quoted 8 lines
Quoted 8 lines
arch/riscv/kernel/sys_hwprobe.c +0 -0
index a7c56b41efd2..dad02f5faec3 100644
@@ -147,8 +147,10 @@ static bool hwprobe_ext0_has(const struct cpumask *cpus, unsigned long ext)
 	return (pair.value & ext);
 }
 
Quoted 6 lines

arch/riscv/kernel/sys_hwprobe.c static bool hwprobe_ext0_has(const struct cpumask *cpus, unsigned long ext) · line 153

Quoted 4 lines
arch/riscv/kernel/sys_hwprobe.c +2 -0
+#if defined(CONFIG_RISCV_PROBE_UNALIGNED_ACCESS)
 static u64 hwprobe_misaligned(const struct cpumask *cpus)
 {
+	return RISCV_HWPROBE_MISALIGNED_FAST;

This hack is still here.

Oh no! I removed it locally but it snuck back in...

Quoted 31 lines
Quoted 18 lines
arch/riscv/kernel/sys_hwprobe.c +9 -0
 	int cpu;
 	u64 perf = -1ULL;
 
@@ -169,6 +171,25 @@ static u64 hwprobe_misaligned(const struct cpumask *cpus)
 
 	return perf;
 }
 
+#elif defined(CONFIG_RISCV_EMULATED_UNALIGNED_ACCESS)
+static u64 hwprobe_misaligned(const struct cpumask *cpus)
+{
+	if (unaligned_ctl_available())
+		return RISCV_HWPROBE_MISALIGNED_EMULATED;
+	else
+		return RISCV_HWPROBE_MISALIGNED_SLOW;
+}
+#elif defined(CONFIG_RISCV_SLOW_UNALIGNED_ACCESS)

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

Quoted 10 lines
arch/riscv/kernel/sys_hwprobe.c +10 -0
+static u64 hwprobe_misaligned(const struct cpumask *cpus)
+{
+	return RISCV_HWPROBE_MISALIGNED_SLOW;
+}
+#elif defined(CONFIG_RISCV_EFFICIENT_UNALIGNED_ACCESS)
+static u64 hwprobe_misaligned(const struct cpumask *cpus)
+{
+	return RISCV_HWPROBE_MISALIGNED_FAST;
+}
+#endif

Same applies to these three functions.

Quoted 2 lines

Thanks,
Conor.

Thank you, I will send out a new version shortly.

- Charlie

#10 lore raw

Re: [PATCH v6 4/4] riscv: Set unaligned access speed at compile time

Quoted 9 lines · Charlie Jenkins
Quoted 8 lines · Conor Dooley
Quoted 5 lines · Charlie Jenkins
arch/riscv/Kconfig +5 -0
+
+choice
+	prompt "Unaligned Accesses Support"
+	default RISCV_PROBE_UNALIGNED_ACCESS
+	help
Quoted 3 lines
Quoted 3 lines

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 702

Quoted 1 line
arch/riscv/Kconfig +1 -0
+	  This selects the hardware support for unaligned accesses. This

"This determines what level of support for..."

Reading this back, s/what/the/

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
arch/riscv/Kconfig +3 -0
+	  information is used by the kernel to perform optimizations. It is also
+	  exposed to user space via the hwprobe syscall. The hardware will be
+	  probed at boot by default.