Skip to contentSkip to conversation

riscv: Use Kconfig to set unaligned access speed

PATCH Superseded Charlie Jenkins 5 participants 11 messages Feb 16, 2024 – Feb 28, 2024 RISC-V

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v4 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 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 (2):
riscv: lib: Introduce has_fast_misaligned_access function
riscv: Set unalignment speed at compile time

 arch/riscv/Kconfig                          |  58 +++++-
 arch/riscv/include/asm/cpufeature.h         |  45 ++++-
 arch/riscv/kernel/Makefile                  |   6 +-
 arch/riscv/kernel/cpufeature.c              | 255 --------------------------
 arch/riscv/kernel/misaligned_access_speed.c | 265 ++++++++++++++++++++++++++++
 arch/riscv/kernel/probe_emulated_access.c   |  64 +++++++
 arch/riscv/kernel/sys_hwprobe.c             |  25 +++
 arch/riscv/kernel/traps_misaligned.c        |  54 +-----
 arch/riscv/lib/csum.c                       |   7 +-
 9 files changed, 454 insertions(+), 325 deletions(-)

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

Signature · 1 line
- Charlie
#1 lore raw

PATCH v4 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..47392a83239d 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) == 0))
 		return do_csum_no_alignment(buff, len);
 
 	return do_csum_with_alignment(buff, len);
Signature · 1 line
2.43.0
#2 lore raw

PATCH v4 2/2 riscv: Set unalignment 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         |  30 +++-
 arch/riscv/kernel/Makefile                  |   6 +-
 arch/riscv/kernel/cpufeature.c              | 255 --------------------------
 arch/riscv/kernel/misaligned_access_speed.c | 265 ++++++++++++++++++++++++++++
 arch/riscv/kernel/probe_emulated_access.c   |  64 +++++++
 arch/riscv/kernel/sys_hwprobe.c             |  25 +++
 arch/riscv/kernel/traps_misaligned.c        |  54 +-----
 8 files changed, 442 insertions(+), 315 deletions(-)
arch/riscv/Kconfig +52 -6
index bffbd869a068..3cf700adc43b 100644
@@ -690,25 +690,71 @@ config THREAD_SIZE_ORDER
 config RISCV_MISALIGNED
 	bool "Support misaligned load/store traps for kernel and userspace"
 	select SYSCTL_ARCH_UNALIGN_ALLOW
+	depends on RISCV_PROBE_UNALIGNED_ACCESS || RISCV_EMULATED_UNALIGNED_ACCESS
 	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.
 
+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"
+	help
+	  During boot, the kernel will run a series of tests to determine the
+	  speed of unaligned accesses. This is the only portable option. This
+	  probing will dynamically determine the speed of unaligned accesses on
+	  the boot hardware.
+
+config RISCV_EMULATED_UNALIGNED_ACCESS
+	bool "Assume the CPU expects emulated unaligned memory accesses"
+	depends on NONPORTABLE
+	select RISCV_MISALIGNED
+	help
+	  Assume that the CPU expects emulated unaligned memory accesses.
+	  When enabled, this option notifies the kernel and userspace that
+	  unaligned memory accesses will be emulated by the kernel. To enforce
+	  this expectation, RISCV_MISALIGNED is selected by this option.
+
+config RISCV_SLOW_UNALIGNED_ACCESS
+	bool "Assume the CPU supports slow unaligned memory accesses"
+	depends on NONPORTABLE
+	help
+	  Assume that the CPU supports slow 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 unaligned memory
+	  accesses.
+
 config RISCV_EFFICIENT_UNALIGNED_ACCESS
 	bool "Assume the CPU 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 CPU supports fast 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.
+
+config RISCV_UNSUPPORTED_UNALIGNED_ACCESS
+	bool "Assume the CPU doesn't support unaligned memory accesses"
+	depends on NONPORTABLE
+	help
+	  Assume that the CPU doesn't support unaligned memory accesses. Only
+	  select this option if there is no registered trap handler to emulate
+	  unaligned accesses.
 
-	  If unsure what to do here, say N.
+endchoice
 
 endmenu # "Platform type"
 
arch/riscv/include/asm/cpufeature.h +29 -1
index eb3ac304fc42..928ad3384406 100644
@@ -33,10 +33,26 @@ extern struct riscv_isainfo hart_isa[NR_CPUS];
 
 void riscv_user_isa_enable(void);
 
-#ifdef CONFIG_RISCV_MISALIGNED
+#if defined(CONFIG_RISCV_MISALIGNED)
+#if defined(CONFIG_RISCV_PROBE_UNALIGNED_ACCESS)
 bool unaligned_ctl_available(void);
 bool check_unaligned_access_emulated(int cpu);
 void unaligned_emulation_finish(void);
+#elif defined(CONFIG_RISCV_EMULATED_UNALIGNED_ACCESS)
+static inline bool unaligned_ctl_available(void)
+{
+	return true;
+}
+
+static inline bool check_unaligned_access_emulated(int cpu)
+{
+	return true;
+}
+
+static inline void unaligned_emulation_finish(void) {}
+#else
+#error "CONFIG_RISCV_MISALIGNED is only supported if CONFIG_RISCV_PROBE_UNALIGNED_ACCESS or CONFIG_RISCV_EMULATED_UNALIGNED_ACCESS is selected."
+#endif
 #else
 static inline bool unaligned_ctl_available(void)
 {
@@ -51,6 +67,7 @@ static inline bool check_unaligned_access_emulated(int cpu)
 static inline void unaligned_emulation_finish(void) {}
 #endif
 
+#if defined(CONFIG_RISCV_PROBE_UNALIGNED_ACCESS)
 DECLARE_PER_CPU(long, misaligned_access_speed);
 
 DECLARE_STATIC_KEY_FALSE(fast_misaligned_access_speed_key);
@@ -59,6 +76,17 @@ static __always_inline bool has_fast_misaligned_accesses(void)
 {
 	return static_branch_likely(&fast_misaligned_access_speed_key);
 }
+#elif defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
+static __always_inline bool has_fast_misaligned_accesses(void)
+{
+	return true;
+}
+#else
+static __always_inline bool has_fast_misaligned_accesses(void)
+{
+	return false;
+}
+#endif
 
 unsigned long riscv_get_elf_hwcap(void);
 
arch/riscv/kernel/Makefile +5 -1
index f71910718053..7a1646e01a86 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,11 @@ obj-y	+= tests/
 obj-$(CONFIG_MMU) += vdso.o vdso/
 
 obj-$(CONFIG_RISCV_MISALIGNED)	+= traps_misaligned.o
+ifeq ($(CONFIG_RISCV_PROBE_UNALIGNED_ACCESS), y)
+obj-y	+= copy-unaligned.o
+obj-y	+= misaligned_access_speed.o
+obj-$(CONFIG_RISCV_MISALIGNED)	+= probe_emulated_access.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/probe_emulated_access.c new file +64 -0

64 lines not on this page · raw message

arch/riscv/kernel/sys_hwprobe.c +25 -0
index a7c56b41efd2..33a8abea1a3e 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,29 @@ 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)
+{
+	return RISCV_HWPROBE_MISALIGNED_EMULATED;
+}
+#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;
+}
+#elif defined(CONFIG_RISCV_UNSUPPORTED_UNALIGNED_ACCESS)
+static u64 hwprobe_misaligned(const struct cpumask *cpus)
+{
+	return RISCV_HWPROBE_MISALIGNED_UNSUPPORTED;
+}
+#else
+#error "Invalid unaligned access speed selected. Please check RISCV_PROBE_UNALIGNED_ACCESS_SUPPORT Kconfig."
+#endif
 
 static void hwprobe_one_pair(struct riscv_hwprobe *pair,
 			     const struct cpumask *cpus)
arch/riscv/kernel/traps_misaligned.c +2 -52

70 lines not on this page · raw message

Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH v4 2/2] riscv: Set unalignment speed at compile time

Quoted 25 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         |  30 +++-
 arch/riscv/kernel/Makefile                  |   6 +-
 arch/riscv/kernel/cpufeature.c              | 255 --------------------------
 arch/riscv/kernel/misaligned_access_speed.c | 265 ++++++++++++++++++++++++++++
 arch/riscv/kernel/probe_emulated_access.c   |  64 +++++++
 arch/riscv/kernel/sys_hwprobe.c             |  25 +++
 arch/riscv/kernel/traps_misaligned.c        |  54 +-----
 8 files changed, 442 insertions(+), 315 deletions(-)
arch/riscv/Kconfig +0 -0
index bffbd869a068..3cf700adc43b 100644

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 690

Quoted 1 line
arch/riscv/Kconfig +0 -0
@@ -690,25 +690,71 @@ config THREAD_SIZE_ORDER
 config RISCV_MISALIGNED

Why can we not make up our minds on what to call this? The majority of users are "unaligned" but the file you add and this config option are "misaligned."

Quoted 19 lines
arch/riscv/Kconfig +11 -0
 	bool "Support misaligned load/store traps for kernel and userspace"
 	select SYSCTL_ARCH_UNALIGN_ALLOW
+	depends on RISCV_PROBE_UNALIGNED_ACCESS || RISCV_EMULATED_UNALIGNED_ACCESS
 	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.
 
+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

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 719

Quoted 10 lines
arch/riscv/Kconfig +10 -0
+	bool "Probe for hardware unaligned access support"
+	help
+	  During boot, the kernel will run a series of tests to determine the
+	  speed of unaligned accesses. This is the only portable option. This
+	  probing will dynamically determine the speed of unaligned accesses on
+	  the boot hardware.
+
+config RISCV_EMULATED_UNALIGNED_ACCESS
+	bool "Assume the CPU expects emulated unaligned memory accesses"
+	depends on NONPORTABLE

This is portable too, right?

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 725

Quoted 8 lines
arch/riscv/Kconfig +5 -0
+	select RISCV_MISALIGNED
+	help
+	  Assume that the CPU expects emulated unaligned memory accesses.
+	  When enabled, this option notifies the kernel and userspace that
+	  unaligned memory accesses will be emulated by the kernel.
 

To enforce

arch/riscv/Kconfig +1 -0
+	  this expectation, RISCV_MISALIGNED is selected by this option.

Drop this IMO, let Kconfig handle displaying the dependencies.

arch/riscv/Kconfig config THREAD_SIZE_ORDER

Quoted 8 lines
arch/riscv/Kconfig +8 -0
+
+config RISCV_SLOW_UNALIGNED_ACCESS
+	bool "Assume the CPU supports slow unaligned memory accesses"
+	depends on NONPORTABLE
+	help
+	  Assume that the CPU supports slow unaligned memory accesses. When
+	  enabled, this option improves the performance of the kernel on such
+	  CPUs.

Does it? Are you sure that generating unaligned accesses on systems where they are slow is a performance increase?
That said, I don't really see this option actually doing anything other than setting the value for hwprobe, so I don't actually know what the effect of this option actually is on the kernel's performance.

Generally I would like to suggest a change from "CPU" to "system" here, since the slow cases that exist are mostly because the unaligned access is actually emulated in firmware.

Quoted 12 lines

However, the kernel will run much more slowly, or will not be

arch/riscv/Kconfig +3 -2
+	  able to run at all, on CPUs that do not support unaligned memory
+	  accesses.
+
 config RISCV_EFFICIENT_UNALIGNED_ACCESS
 	bool "Assume the CPU 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

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 749

Quoted 10 lines
arch/riscv/Kconfig +7 -3
-	  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 CPU supports fast 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.
+
+config RISCV_UNSUPPORTED_UNALIGNED_ACCESS

This option needs to be removed. The uabi states that unaligned access is supported in userspace, if the cpu or firmware does not implement unaligned access then the kernel must emulate it.

Quoted 30 lines
arch/riscv/Kconfig +7 -1
+	bool "Assume the CPU doesn't support unaligned memory accesses"
+	depends on NONPORTABLE
+	help
+	  Assume that the CPU doesn't support unaligned memory accesses. Only
+	  select this option if there is no registered trap handler to emulate
+	  unaligned accesses.
 
-	  If unsure what to do here, say N.
+endchoice
 
 endmenu # "Platform type"
arch/riscv/include/asm/cpufeature.h +6 -1
index eb3ac304fc42..928ad3384406 100644
@@ -33,10 +33,26 @@ extern struct riscv_isainfo hart_isa[NR_CPUS];
 
 void riscv_user_isa_enable(void);
 
-#ifdef CONFIG_RISCV_MISALIGNED
+#if defined(CONFIG_RISCV_MISALIGNED)
+#if defined(CONFIG_RISCV_PROBE_UNALIGNED_ACCESS)
 bool unaligned_ctl_available(void);
 bool check_unaligned_access_emulated(int cpu);
 void unaligned_emulation_finish(void);
+#elif defined(CONFIG_RISCV_EMULATED_UNALIGNED_ACCESS)
+static inline bool unaligned_ctl_available(void)
+{
+	return true;

arch/riscv/include/asm/cpufeature.h extern struct riscv_isainfo hart_isa[NR_CPUS]; · line 54

Quoted 10 lines
arch/riscv/include/asm/cpufeature.h +10 -0
+}
+
+static inline bool check_unaligned_access_emulated(int cpu)
+{
+	return true;
+}
+
+static inline void unaligned_emulation_finish(void) {}
+#else
+#error "CONFIG_RISCV_MISALIGNED is only supported if CONFIG_RISCV_PROBE_UNALIGNED_ACCESS or CONFIG_RISCV_EMULATED_UNALIGNED_ACCESS is selected."

Why is this an error? Enforce this in Kconfig (you already have) and drop the clause.

Cheers,
Conor.

#4 lore raw

Re: [PATCH v4 2/2] riscv: Set unalignment speed at compile time

Quoted 21 lines · Conor Dooley
Quoted 20 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         |  30 +++-
 arch/riscv/kernel/Makefile                  |   6 +-
 arch/riscv/kernel/cpufeature.c              | 255 --------------------------
 arch/riscv/kernel/misaligned_access_speed.c | 265 ++++++++++++++++++++++++++++
 arch/riscv/kernel/probe_emulated_access.c   |  64 +++++++
 arch/riscv/kernel/sys_hwprobe.c             |  25 +++
 arch/riscv/kernel/traps_misaligned.c        |  54 +-----
 8 files changed, 442 insertions(+), 315 deletions(-)
Quoted 11 lines

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 690

Quoted 6 lines
arch/riscv/Kconfig +0 -0
index bffbd869a068..3cf700adc43b 100644
@@ -690,25 +690,71 @@ config THREAD_SIZE_ORDER
 config RISCV_MISALIGNED

Why can we not make up our minds on what to call this? The majority of users are "unaligned" but the file you add and this config option are "misaligned."

We have both everywhere, maybe we (I?) should go in and standardize the wording everywhere. I personally prefer "misaligned" which means "incorrectly aligned" over "unaligned" which means "not aligned" because a 7-bit alignment is still "aligned" along a 7-bit boundary, but it is certainly incorrectly aligned.

Quoted 24 lines
Quoted 24 lines
arch/riscv/Kconfig +15 -0
 	bool "Support misaligned load/store traps for kernel and userspace"
 	select SYSCTL_ARCH_UNALIGN_ALLOW
+	depends on RISCV_PROBE_UNALIGNED_ACCESS || RISCV_EMULATED_UNALIGNED_ACCESS
 	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.
 
+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"
+	help
+	  During boot, the kernel will run a series of tests to determine the
+	  speed of unaligned accesses. This is the only portable option. This
Quoted 8 lines

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 719

Quoted 6 lines
arch/riscv/Kconfig +6 -0
+	  probing will dynamically determine the speed of unaligned accesses on
+	  the boot hardware.
+
+config RISCV_EMULATED_UNALIGNED_ACCESS
+	bool "Assume the CPU expects emulated unaligned memory accesses"
+	depends on NONPORTABLE

This is portable too, right?

I guess so? I think I would prefer to have the probing being the only portable option.

Quoted 7 lines
Quoted 6 lines
arch/riscv/Kconfig +5 -0
+	select RISCV_MISALIGNED
+	help
+	  Assume that the CPU expects emulated unaligned memory accesses.
+	  When enabled, this option notifies the kernel and userspace that
+	  unaligned memory accesses will be emulated by the kernel.
Quoted 5 lines

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 725

Quoted 2 lines

To enforce

arch/riscv/Kconfig +1 -0
+	  this expectation, RISCV_MISALIGNED is selected by this option.

Drop this IMO, let Kconfig handle displaying the dependencies.

I was debating if Kconfig handling was enough, so I am glad it is, I will remove this.

Quoted 15 lines

arch/riscv/Kconfig config THREAD_SIZE_ORDER

Quoted 8 lines
arch/riscv/Kconfig +8 -0
+
+config RISCV_SLOW_UNALIGNED_ACCESS
+	bool "Assume the CPU supports slow unaligned memory accesses"
+	depends on NONPORTABLE
+	help
+	  Assume that the CPU supports slow unaligned memory accesses. When
+	  enabled, this option improves the performance of the kernel on such
+	  CPUs.

Does it? Are you sure that generating unaligned accesses on systems where they are slow is a performance increase?
That said, I don't really see this option actually doing anything other than setting the value for hwprobe, so I don't actually know what the effect of this option actually is on the kernel's performance.

Quoted 3 lines

Generally I would like to suggest a change from "CPU" to "system" here, since the slow cases that exist are mostly because the unaligned access is actually emulated in firmware.

It would be ideal if "emulated" was used for any case of emulated accesses (firmware or in the kernel). Doing emulated accesses will be orders of magnitude slower than a processor that "slowly" handles the accesses.

So even if the processor performs a "slow" access, it could still be beneficial for the kernel to do the misaligned access rather than manual do the alignment.

Currently there is no place that takes into account this "slow" option but I wanted to leave it open for future optimizations.

Quoted 19 lines
Quoted 19 lines

However, the kernel will run much more slowly, or will not be

arch/riscv/Kconfig +6 -5
+	  able to run at all, on CPUs that do not support unaligned memory
+	  accesses.
+
 config RISCV_EFFICIENT_UNALIGNED_ACCESS
 	bool "Assume the CPU 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 CPU supports fast 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
Quoted 8 lines

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 749

Quoted 4 lines
arch/riscv/Kconfig +4 -0
+	  able to run at all, on CPUs that do not support efficient unaligned
+	  memory accesses.
+
+config RISCV_UNSUPPORTED_UNALIGNED_ACCESS

This option needs to be removed. The uabi states that unaligned access is supported in userspace, if the cpu or firmware does not implement unaligned access then the kernel must emulate it.

Should it removed from hwprobe as well then?

Quoted 36 lines
Quoted 36 lines
arch/riscv/Kconfig +7 -1
+	bool "Assume the CPU doesn't support unaligned memory accesses"
+	depends on NONPORTABLE
+	help
+	  Assume that the CPU doesn't support unaligned memory accesses. Only
+	  select this option if there is no registered trap handler to emulate
+	  unaligned accesses.
 
-	  If unsure what to do here, say N.
+endchoice
 
 endmenu # "Platform type"
arch/riscv/include/asm/cpufeature.h +11 -1
index eb3ac304fc42..928ad3384406 100644
@@ -33,10 +33,26 @@ extern struct riscv_isainfo hart_isa[NR_CPUS];
 
 void riscv_user_isa_enable(void);
 
-#ifdef CONFIG_RISCV_MISALIGNED
+#if defined(CONFIG_RISCV_MISALIGNED)
+#if defined(CONFIG_RISCV_PROBE_UNALIGNED_ACCESS)
 bool unaligned_ctl_available(void);
 bool check_unaligned_access_emulated(int cpu);
 void unaligned_emulation_finish(void);
+#elif defined(CONFIG_RISCV_EMULATED_UNALIGNED_ACCESS)
+static inline bool unaligned_ctl_available(void)
+{
+	return true;
+}
+
+static inline bool check_unaligned_access_emulated(int cpu)
+{
+	return true;
Quoted 8 lines

arch/riscv/include/asm/cpufeature.h extern struct riscv_isainfo hart_isa[NR_CPUS]; · line 54

Quoted 5 lines
arch/riscv/include/asm/cpufeature.h +5 -0
+}
+
+static inline void unaligned_emulation_finish(void) {}
+#else
+#error "CONFIG_RISCV_MISALIGNED is only supported if CONFIG_RISCV_PROBE_UNALIGNED_ACCESS or CONFIG_RISCV_EMULATED_UNALIGNED_ACCESS is selected."

Why is this an error? Enforce this in Kconfig (you already have) and drop the clause.

Awesome, I wasn't sure if the Kconfig was "enough".

- Charlie

Quoted 3 lines

Cheers,
Conor.

#5 lore raw

Re: [PATCH v4 2/2] riscv: Set unalignment speed at compile time

Quoted 11 lines · Charlie Jenkins
Quoted 7 lines · Conor Dooley

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 719

Quoted 3 lines · Charlie Jenkins
arch/riscv/Kconfig +3 -0
+config RISCV_EMULATED_UNALIGNED_ACCESS
+	bool "Assume the CPU expects emulated unaligned memory accesses"
+	depends on NONPORTABLE

This is portable too, right?

I guess so? I think I would prefer to have the probing being the only portable option.

I dunno, I think there could be value to someone in always emulating this in the kernel and I don't think that should relegate them to the naughty step, given it can work everywhere.

Quoted 18 lines
Quoted 14 lines

arch/riscv/Kconfig config THREAD_SIZE_ORDER

Quoted 7 lines
arch/riscv/Kconfig +7 -0
+config RISCV_SLOW_UNALIGNED_ACCESS
+	bool "Assume the CPU supports slow unaligned memory accesses"
+	depends on NONPORTABLE
+	help
+	  Assume that the CPU supports slow unaligned memory accesses. When
+	  enabled, this option improves the performance of the kernel on such
+	  CPUs.

Does it? Are you sure that generating unaligned accesses on systems where they are slow is a performance increase?
That said, I don't really see this option actually doing anything other than setting the value for hwprobe, so I don't actually know what the effect of this option actually is on the kernel's performance.

Quoted 3 lines

Generally I would like to suggest a change from "CPU" to "system" here, since the slow cases that exist are mostly because the unaligned access is actually emulated in firmware.

Quoted 8 lines

It would be ideal if "emulated" was used for any case of emulated accesses (firmware or in the kernel). Doing emulated accesses will be orders of magnitude slower than a processor that "slowly" handles the accesses.

So even if the processor performs a "slow" access, it could still be beneficial for the kernel to do the misaligned access rather than manual do the alignment.

Right. But, at least from a probing perspective, SLOW is what gets selected when firmware emulates the unaligned access so to userspace seeing slow means that the performance could be horrifically bad:

|     rzfive:
|         cpu0: Ratio of byte access time to unaligned word access is
| 1.05, unaligned accesses are fast
| 
|     icicle:
| 
|         cpu1: Ratio of byte access time to unaligned word access is
| 0.00, unaligned accesses are slow
|         cpu2: Ratio of byte access time to unaligned word access is
| 0.00, unaligned accesses are slow
|         cpu3: Ratio of byte access time to unaligned word access is
| 0.00, unaligned accesses are slow
| 
|         cpu0: Ratio of byte access time to unaligned word access is
| 0.00, unaligned accesses are slow
| 
|     k210:
| 
|         cpu1: Ratio of byte access time to unaligned word access is
| 0.02, unaligned accesses are slow
|         cpu0: Ratio of byte access time to unaligned word access is
| 0.02, unaligned accesses are slow
| 
|     starlight:
| 
|         cpu1: Ratio of byte access time to unaligned word access is
| 0.01, unaligned accesses are slow
|         cpu0: Ratio of byte access time to unaligned word access is
| 0.02, unaligned accesses are slow
| 
|     vexriscv/orangecrab:
| 
|         cpu0: Ratio of byte access time to unaligned word access is
| 0.00, unaligned accesses are slow

https://lore.kernel.org/all/CAMuHMdVtXGjP8VFMiv-7OMFz1XvfU1cz=Fw4jL3fcp4wO1etzQ@mail.gmail.com/

Quoted 2 lines

Currently there is no place that takes into account this "slow" option but I wanted to leave it open for future optimizations.

I don't think you can really do much optimisation if you detect that it is slow, and since this option is analogous to detecting slow I dunno if you can do anything here either? This option really just seems to me to mean "don't do any optimisations for unaligned being fast but also don't emulate it in the kernel".

Quoted 27 lines
Quoted 18 lines
Quoted 18 lines

However, the kernel will run much more slowly, or will not be

arch/riscv/Kconfig +6 -5
+	  able to run at all, on CPUs that do not support unaligned memory
+	  accesses.
+
 config RISCV_EFFICIENT_UNALIGNED_ACCESS
 	bool "Assume the CPU 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 CPU supports fast 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
Quoted 8 lines

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 749

Quoted 4 lines
arch/riscv/Kconfig +4 -0
+	  able to run at all, on CPUs that do not support efficient unaligned
+	  memory accesses.
+
+config RISCV_UNSUPPORTED_UNALIGNED_ACCESS

This option needs to be removed. The uabi states that unaligned access is supported in userspace, if the cpu or firmware does not implement unaligned access then the kernel must emulate it.

Quoted 1 line

Should it removed from hwprobe as well then?

No, I actually suggested that it be documented etc. Originally "UNSUPPORTED" was "UNKNOWN" and nothing more than the default value but I suggested that it be documented since that would allow a system that did not have the same uabi problem to use all the same defines. Technically it is possible for unaligned access to be unsupported, if you have a kernel that does not have the emulator but does have the hwprobe stuff supported. I think there was about a 6 month period where this was the case, so combine that with firmware that does not do the emulation and unaligned accesses are unsupported.

Cheers,
Conor.

#6 lore raw

Re: [PATCH v4 2/2] riscv: Set unalignment speed at compile time

Quoted 10 lines · Conor Dooley
Quoted 9 lines · Charlie Jenkins
Quoted 7 lines · Conor Dooley

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 719

Quoted 3 lines · Charlie Jenkins
arch/riscv/Kconfig +3 -0
+config RISCV_EMULATED_UNALIGNED_ACCESS
+	bool "Assume the CPU expects emulated unaligned memory accesses"
+	depends on NONPORTABLE

This is portable too, right?

Quoted 6 lines
Quoted 2 lines

I guess so? I think I would prefer to have the probing being the only portable option.

I dunno, I think there could be value to someone in always emulating this in the kernel and I don't think that should relegate them to the naughty step, given it can work everywhere.

Alright, I will remove the nonportable.

Quoted 67 lines
Quoted 20 lines
Quoted 16 lines

arch/riscv/Kconfig config THREAD_SIZE_ORDER

Quoted 9 lines
arch/riscv/Kconfig +7 -0
+config RISCV_SLOW_UNALIGNED_ACCESS
+	bool "Assume the CPU supports slow unaligned memory accesses"
+	depends on NONPORTABLE
+	help
+	  Assume that the CPU supports slow unaligned memory accesses. When
+	  enabled, this option improves the performance of the kernel on such
+	  CPUs.

Does it? Are you sure that generating unaligned accesses on systems where they are slow is a performance increase?
That said, I don't really see this option actually doing anything other than setting the value for hwprobe, so I don't actually know what the effect of this option actually is on the kernel's performance.

Quoted 3 lines

Generally I would like to suggest a change from "CPU" to "system" here, since the slow cases that exist are mostly because the unaligned access is actually emulated in firmware.

Quoted 8 lines

It would be ideal if "emulated" was used for any case of emulated accesses (firmware or in the kernel). Doing emulated accesses will be orders of magnitude slower than a processor that "slowly" handles the accesses.

So even if the processor performs a "slow" access, it could still be beneficial for the kernel to do the misaligned access rather than manual do the alignment.

Right. But, at least from a probing perspective, SLOW is what gets selected when firmware emulates the unaligned access so to userspace seeing slow means that the performance could be horrifically bad:

|     rzfive:
|         cpu0: Ratio of byte access time to unaligned word access is
| 1.05, unaligned accesses are fast
| 
|     icicle:
| 
|         cpu1: Ratio of byte access time to unaligned word access is
| 0.00, unaligned accesses are slow
|         cpu2: Ratio of byte access time to unaligned word access is
| 0.00, unaligned accesses are slow
|         cpu3: Ratio of byte access time to unaligned word access is
| 0.00, unaligned accesses are slow
| 
|         cpu0: Ratio of byte access time to unaligned word access is
| 0.00, unaligned accesses are slow
| 
|     k210:
| 
|         cpu1: Ratio of byte access time to unaligned word access is
| 0.02, unaligned accesses are slow
|         cpu0: Ratio of byte access time to unaligned word access is
| 0.02, unaligned accesses are slow
| 
|     starlight:
| 
|         cpu1: Ratio of byte access time to unaligned word access is
| 0.01, unaligned accesses are slow
|         cpu0: Ratio of byte access time to unaligned word access is
| 0.02, unaligned accesses are slow
| 
|     vexriscv/orangecrab:
| 
|         cpu0: Ratio of byte access time to unaligned word access is
| 0.00, unaligned accesses are slow
Quoted 1 line

https://lore.kernel.org/all/CAMuHMdVtXGjP8VFMiv-7OMFz1XvfU1cz=Fw4jL3fcp4wO1etzQ@mail.gmail.com/

If the accesses are horrifically slow then maybe they should be flagged as emulated rather than slow by the probe.

Quoted 9 lines
Quoted 3 lines

Currently there is no place that takes into account this "slow" option but I wanted to leave it open for future optimizations.

I don't think you can really do much optimisation if you detect that it is slow, and since this option is analogous to detecting slow I dunno if you can do anything here either? This option really just seems to me to mean "don't do any optimisations for unaligned being fast but also don't emulate it in the kernel".

I am fine with that being the meaning of this option. However, on a system that has misaligned accesses that are twice as slow as correctly aligned accesses, the misaligned accesses would reasonably be selected as "slow". However, something like the checksum functions would still probably want to do the misaligned accesses because performing the alignment would be even slower. This is all hypothetical and not a "real" use case so maybe I am optimizing where no optimization is needed.

Quoted 30 lines
Quoted 28 lines
Quoted 19 lines
Quoted 19 lines

However, the kernel will run much more slowly, or will not be

arch/riscv/Kconfig +6 -5
+	  able to run at all, on CPUs that do not support unaligned memory
+	  accesses.
+
 config RISCV_EFFICIENT_UNALIGNED_ACCESS
 	bool "Assume the CPU 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 CPU supports fast 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
Quoted 8 lines

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 749

Quoted 4 lines
arch/riscv/Kconfig +4 -0
+	  able to run at all, on CPUs that do not support efficient unaligned
+	  memory accesses.
+
+config RISCV_UNSUPPORTED_UNALIGNED_ACCESS

This option needs to be removed. The uabi states that unaligned access is supported in userspace, if the cpu or firmware does not implement unaligned access then the kernel must emulate it.

Quoted 1 line

Should it removed from hwprobe as well then?

Quoted 9 lines

No, I actually suggested that it be documented etc. Originally "UNSUPPORTED" was "UNKNOWN" and nothing more than the default value but I suggested that it be documented since that would allow a system that did not have the same uabi problem to use all the same defines. Technically it is possible for unaligned access to be unsupported, if you have a kernel that does not have the emulator but does have the hwprobe stuff supported. I think there was about a 6 month period where this was the case, so combine that with firmware that does not do the emulation and unaligned accesses are unsupported.

Sounds great, will remove :)

- Charlie

Quoted 3 lines

Cheers,
Conor.

#7 lore raw

Re: [PATCH v4 2/2] riscv: Set unalignment speed at compile time

Quoted 90 lines · Charlie Jenkins
Quoted 8 lines · Conor Dooley
Quoted 7 lines · Charlie Jenkins
Quoted 6 lines · Conor Dooley
Quoted 3 lines · Charlie Jenkins
arch/riscv/Kconfig +3 -0
+config RISCV_EMULATED_UNALIGNED_ACCESS
+       bool "Assume the CPU expects emulated unaligned memory accesses"
+       depends on NONPORTABLE
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

This is portable too, right?

I guess so? I think I would prefer to have the probing being the only portable option.

I dunno, I think there could be value to someone in always emulating this in the kernel and I don't think that should relegate them to the naughty step, given it can work everywhere.

Alright, I will remove the nonportable.

Quoted 67 lines
Quoted 20 lines
Quoted 16 lines

arch/riscv/Kconfig config THREAD_SIZE_ORDER

Quoted 9 lines
arch/riscv/Kconfig +7 -0
+config RISCV_SLOW_UNALIGNED_ACCESS
+       bool "Assume the CPU supports slow unaligned memory accesses"
+       depends on NONPORTABLE
+       help
+         Assume that the CPU supports slow unaligned memory accesses. When
+         enabled, this option improves the performance of the kernel on such
+         CPUs.

Does it? Are you sure that generating unaligned accesses on systems where they are slow is a performance increase?
That said, I don't really see this option actually doing anything other than setting the value for hwprobe, so I don't actually know what the effect of this option actually is on the kernel's performance.

Quoted 3 lines

Generally I would like to suggest a change from "CPU" to "system" here, since the slow cases that exist are mostly because the unaligned access is actually emulated in firmware.

Quoted 8 lines

It would be ideal if "emulated" was used for any case of emulated accesses (firmware or in the kernel). Doing emulated accesses will be orders of magnitude slower than a processor that "slowly" handles the accesses.

So even if the processor performs a "slow" access, it could still be beneficial for the kernel to do the misaligned access rather than manual do the alignment.

Right. But, at least from a probing perspective, SLOW is what gets selected when firmware emulates the unaligned access so to userspace seeing slow means that the performance could be horrifically bad:

|     rzfive:
|         cpu0: Ratio of byte access time to unaligned word access is
| 1.05, unaligned accesses are fast
|
|     icicle:
|
|         cpu1: Ratio of byte access time to unaligned word access is
| 0.00, unaligned accesses are slow
|         cpu2: Ratio of byte access time to unaligned word access is
| 0.00, unaligned accesses are slow
|         cpu3: Ratio of byte access time to unaligned word access is
| 0.00, unaligned accesses are slow
|
|         cpu0: Ratio of byte access time to unaligned word access is
| 0.00, unaligned accesses are slow
|
|     k210:
|
|         cpu1: Ratio of byte access time to unaligned word access is
| 0.02, unaligned accesses are slow
|         cpu0: Ratio of byte access time to unaligned word access is
| 0.02, unaligned accesses are slow
|
|     starlight:
|
|         cpu1: Ratio of byte access time to unaligned word access is
| 0.01, unaligned accesses are slow
|         cpu0: Ratio of byte access time to unaligned word access is
| 0.02, unaligned accesses are slow
|
|     vexriscv/orangecrab:
|
|         cpu0: Ratio of byte access time to unaligned word access is
| 0.00, unaligned accesses are slow
Quoted 1 line

https://lore.kernel.org/all/CAMuHMdVtXGjP8VFMiv-7OMFz1XvfU1cz=Fw4jL3fcp4wO1etzQ@mail.gmail.com/

Quoted 2 lines

If the accesses are horrifically slow then maybe they should be flagged as emulated rather than slow by the probe.

Yeah, I thought about that too. I didn't feel like I had enough info to come up with the delineating number for "horrifically slow". Plus Clement came in with a series to detect specifically that accesses are emulated (though it will only work on future platforms that can delegate the trap to the kernel).

-Evan

#8 lore raw

Re: [PATCH v4 2/2] riscv: Set unalignment speed at compile time

Quoted 91 lines · Evan Green
Quoted 90 lines · Charlie Jenkins
Quoted 8 lines · Conor Dooley
Quoted 7 lines · Charlie Jenkins
Quoted 6 lines · Conor Dooley
Quoted 3 lines · Charlie Jenkins
arch/riscv/Kconfig +3 -0
+config RISCV_EMULATED_UNALIGNED_ACCESS
+       bool "Assume the CPU expects emulated unaligned memory accesses"
+       depends on NONPORTABLE
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

This is portable too, right?

I guess so? I think I would prefer to have the probing being the only portable option.

I dunno, I think there could be value to someone in always emulating this in the kernel and I don't think that should relegate them to the naughty step, given it can work everywhere.

Alright, I will remove the nonportable.

Quoted 68 lines
Quoted 20 lines
Quoted 16 lines

arch/riscv/Kconfig config THREAD_SIZE_ORDER

Quoted 9 lines
arch/riscv/Kconfig +7 -0
+config RISCV_SLOW_UNALIGNED_ACCESS
+       bool "Assume the CPU supports slow unaligned memory accesses"
+       depends on NONPORTABLE
+       help
+         Assume that the CPU supports slow unaligned memory accesses. When
+         enabled, this option improves the performance of the kernel on such
+         CPUs.

Does it? Are you sure that generating unaligned accesses on systems where they are slow is a performance increase?
That said, I don't really see this option actually doing anything other than setting the value for hwprobe, so I don't actually know what the effect of this option actually is on the kernel's performance.

Quoted 3 lines

Generally I would like to suggest a change from "CPU" to "system" here, since the slow cases that exist are mostly because the unaligned access is actually emulated in firmware.

Quoted 8 lines

It would be ideal if "emulated" was used for any case of emulated accesses (firmware or in the kernel). Doing emulated accesses will be orders of magnitude slower than a processor that "slowly" handles the accesses.

So even if the processor performs a "slow" access, it could still be beneficial for the kernel to do the misaligned access rather than manual do the alignment.

Right. But, at least from a probing perspective, SLOW is what gets selected when firmware emulates the unaligned access so to userspace seeing slow means that the performance could be horrifically bad:

|     rzfive:
|         cpu0: Ratio of byte access time to unaligned word access is
| 1.05, unaligned accesses are fast
|
|     icicle:
|
|         cpu1: Ratio of byte access time to unaligned word access is
| 0.00, unaligned accesses are slow
|         cpu2: Ratio of byte access time to unaligned word access is
| 0.00, unaligned accesses are slow
|         cpu3: Ratio of byte access time to unaligned word access is
| 0.00, unaligned accesses are slow
|
|         cpu0: Ratio of byte access time to unaligned word access is
| 0.00, unaligned accesses are slow
|
|     k210:
|
|         cpu1: Ratio of byte access time to unaligned word access is
| 0.02, unaligned accesses are slow
|         cpu0: Ratio of byte access time to unaligned word access is
| 0.02, unaligned accesses are slow
|
|     starlight:
|
|         cpu1: Ratio of byte access time to unaligned word access is
| 0.01, unaligned accesses are slow
|         cpu0: Ratio of byte access time to unaligned word access is
| 0.02, unaligned accesses are slow
|
|     vexriscv/orangecrab:
|
|         cpu0: Ratio of byte access time to unaligned word access is
| 0.00, unaligned accesses are slow

https://lore.kernel.org/all/CAMuHMdVtXGjP8VFMiv-7OMFz1XvfU1cz=Fw4jL3fcp4wO1etzQ@mail.gmail.com/

Quoted 8 lines
Quoted 2 lines

If the accesses are horrifically slow then maybe they should be flagged as emulated rather than slow by the probe.

Yeah, I thought about that too. I didn't feel like I had enough info to come up with the delineating number for "horrifically slow". Plus Clement came in with a series to detect specifically that accesses are emulated (though it will only work on future platforms that can delegate the trap to the kernel).

Yes, the delegation request mechanism should be part of SBI 3.0. At that point we should be able to detect properly if accesses are emulated or slow (providing the SBI implements the new extension).

Clément

Quoted 2 lines

-Evan

#9 lore raw

Re: [PATCH v4 2/2] riscv: Set unalignment speed at compile time

In reply to Charlie Jenkins
Quoted 152 lines · Charlie Jenkins
Quoted 21 lines · Conor Dooley
Quoted 20 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         |  30 +++-
 arch/riscv/kernel/Makefile                  |   6 +-
 arch/riscv/kernel/cpufeature.c              | 255 --------------------------
 arch/riscv/kernel/misaligned_access_speed.c | 265 ++++++++++++++++++++++++++++
 arch/riscv/kernel/probe_emulated_access.c   |  64 +++++++
 arch/riscv/kernel/sys_hwprobe.c             |  25 +++
 arch/riscv/kernel/traps_misaligned.c        |  54 +-----
 8 files changed, 442 insertions(+), 315 deletions(-)
Quoted 11 lines

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 690

Quoted 6 lines
arch/riscv/Kconfig +0 -0
index bffbd869a068..3cf700adc43b 100644
@@ -690,25 +690,71 @@ config THREAD_SIZE_ORDER
 config RISCV_MISALIGNED

Why can we not make up our minds on what to call this? The majority of users are "unaligned" but the file you add and this config option are "misaligned."

We have both everywhere, maybe we (I?) should go in and standardize the wording everywhere. I personally prefer "misaligned" which means "incorrectly aligned" over "unaligned" which means "not aligned" because a 7-bit alignment is still "aligned" along a 7-bit boundary, but it is certainly incorrectly aligned.

Quoted 24 lines
Quoted 24 lines
arch/riscv/Kconfig +15 -0
    bool "Support misaligned load/store traps for kernel and userspace"
    select SYSCTL_ARCH_UNALIGN_ALLOW
+   depends on RISCV_PROBE_UNALIGNED_ACCESS || RISCV_EMULATED_UNALIGNED_ACCESS
    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.
 
+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"
+   help
+     During boot, the kernel will run a series of tests to determine the
+     speed of unaligned accesses. This is the only portable option. This
Quoted 8 lines

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 719

Quoted 6 lines
arch/riscv/Kconfig +6 -0
+     probing will dynamically determine the speed of unaligned accesses on
+     the boot hardware.
+
+config RISCV_EMULATED_UNALIGNED_ACCESS
+   bool "Assume the CPU expects emulated unaligned memory accesses"
+   depends on NONPORTABLE

This is portable too, right?

I guess so? I think I would prefer to have the probing being the only portable option.

Quoted 7 lines
Quoted 6 lines
arch/riscv/Kconfig +5 -0
+   select RISCV_MISALIGNED
+   help
+     Assume that the CPU expects emulated unaligned memory accesses.
+     When enabled, this option notifies the kernel and userspace that
+     unaligned memory accesses will be emulated by the kernel.
Quoted 5 lines
Quoted 2 lines

To enforce
+ this expectation, RISCV_MISALIGNED is selected by this option.

Drop this IMO, let Kconfig handle displaying the dependencies.

I was debating if Kconfig handling was enough, so I am glad it is, I will remove this.

Quoted 15 lines

arch/riscv/Kconfig config THREAD_SIZE_ORDER

Quoted 8 lines
arch/riscv/Kconfig +8 -0
+
+config RISCV_SLOW_UNALIGNED_ACCESS
+   bool "Assume the CPU supports slow unaligned memory accesses"
+   depends on NONPORTABLE
+   help
+     Assume that the CPU supports slow unaligned memory accesses. When
+     enabled, this option improves the performance of the kernel on such
+     CPUs.

Does it? Are you sure that generating unaligned accesses on systems where they are slow is a performance increase?
That said, I don't really see this option actually doing anything other than setting the value for hwprobe, so I don't actually know what the effect of this option actually is on the kernel's performance.

Quoted 3 lines

Generally I would like to suggest a change from "CPU" to "system" here, since the slow cases that exist are mostly because the unaligned access is actually emulated in firmware.

It would be ideal if "emulated" was used for any case of emulated accesses (firmware or in the kernel). Doing emulated accesses will be orders of magnitude slower than a processor that "slowly" handles the accesses.

So even if the processor performs a "slow" access, it could still be beneficial for the kernel to do the misaligned access rather than manual do the alignment.

Currently there is no place that takes into account this "slow" option but I wanted to leave it open for future optimizations.

Quoted 19 lines
Quoted 19 lines

However, the kernel will run much more slowly, or will not be

arch/riscv/Kconfig +6 -5
+     able to run at all, on CPUs that do not support unaligned memory
+     accesses.
+
 config RISCV_EFFICIENT_UNALIGNED_ACCESS
    bool "Assume the CPU 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 CPU supports fast 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
Quoted 8 lines

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 749

Quoted 4 lines
arch/riscv/Kconfig +4 -0
+     able to run at all, on CPUs that do not support efficient unaligned
+     memory accesses.
+
+config RISCV_UNSUPPORTED_UNALIGNED_ACCESS

This option needs to be removed. The uabi states that unaligned access is supported in userspace, if the cpu or firmware does not implement unaligned access then the kernel must emulate it.

Quoted 1 line

Should it removed from hwprobe as well then?

We had added it as a hwprobe value in this discussion:
https://lore.kernel.org/lkml/Y+1VOXyKDDHEuejJ@spud/

Personally I like it as a possible hwprobe value, even if it is in conflict with the uabi. I can't fully defend it, other than as a very forward looking possibility, and as a nice value for people doing weird things off the beaten path. My preference would be to keep it in hwprobe, but I'm fine with not having a Kconfig for it.

-Evan

#10 lore raw

Re: [PATCH v4 2/2] riscv: Set unalignment speed at compile time

Quoted 158 lines · Evan Green
Quoted 152 lines · Charlie Jenkins
Quoted 21 lines · Conor Dooley
Quoted 20 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         |  30 +++-
 arch/riscv/kernel/Makefile                  |   6 +-
 arch/riscv/kernel/cpufeature.c              | 255 --------------------------
 arch/riscv/kernel/misaligned_access_speed.c | 265 ++++++++++++++++++++++++++++
 arch/riscv/kernel/probe_emulated_access.c   |  64 +++++++
 arch/riscv/kernel/sys_hwprobe.c             |  25 +++
 arch/riscv/kernel/traps_misaligned.c        |  54 +-----
 8 files changed, 442 insertions(+), 315 deletions(-)
Quoted 11 lines

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 690

Quoted 6 lines
arch/riscv/Kconfig +0 -0
index bffbd869a068..3cf700adc43b 100644
@@ -690,25 +690,71 @@ config THREAD_SIZE_ORDER
 config RISCV_MISALIGNED

Why can we not make up our minds on what to call this? The majority of users are "unaligned" but the file you add and this config option are "misaligned."

We have both everywhere, maybe we (I?) should go in and standardize the wording everywhere. I personally prefer "misaligned" which means "incorrectly aligned" over "unaligned" which means "not aligned" because a 7-bit alignment is still "aligned" along a 7-bit boundary, but it is certainly incorrectly aligned.

Quoted 24 lines
Quoted 24 lines
arch/riscv/Kconfig +15 -0
    bool "Support misaligned load/store traps for kernel and userspace"
    select SYSCTL_ARCH_UNALIGN_ALLOW
+   depends on RISCV_PROBE_UNALIGNED_ACCESS || RISCV_EMULATED_UNALIGNED_ACCESS
    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.
 
+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"
+   help
+     During boot, the kernel will run a series of tests to determine the
+     speed of unaligned accesses. This is the only portable option. This
Quoted 8 lines

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 719

Quoted 6 lines
arch/riscv/Kconfig +6 -0
+     probing will dynamically determine the speed of unaligned accesses on
+     the boot hardware.
+
+config RISCV_EMULATED_UNALIGNED_ACCESS
+   bool "Assume the CPU expects emulated unaligned memory accesses"
+   depends on NONPORTABLE

This is portable too, right?

I guess so? I think I would prefer to have the probing being the only portable option.

Quoted 7 lines
Quoted 6 lines
arch/riscv/Kconfig +5 -0
+   select RISCV_MISALIGNED
+   help
+     Assume that the CPU expects emulated unaligned memory accesses.
+     When enabled, this option notifies the kernel and userspace that
+     unaligned memory accesses will be emulated by the kernel.
Quoted 5 lines
Quoted 2 lines

To enforce
+ this expectation, RISCV_MISALIGNED is selected by this option.

Drop this IMO, let Kconfig handle displaying the dependencies.

I was debating if Kconfig handling was enough, so I am glad it is, I will remove this.

Quoted 15 lines

arch/riscv/Kconfig config THREAD_SIZE_ORDER

Quoted 8 lines
arch/riscv/Kconfig +8 -0
+
+config RISCV_SLOW_UNALIGNED_ACCESS
+   bool "Assume the CPU supports slow unaligned memory accesses"
+   depends on NONPORTABLE
+   help
+     Assume that the CPU supports slow unaligned memory accesses. When
+     enabled, this option improves the performance of the kernel on such
+     CPUs.

Does it? Are you sure that generating unaligned accesses on systems where they are slow is a performance increase?
That said, I don't really see this option actually doing anything other than setting the value for hwprobe, so I don't actually know what the effect of this option actually is on the kernel's performance.

Quoted 3 lines

Generally I would like to suggest a change from "CPU" to "system" here, since the slow cases that exist are mostly because the unaligned access is actually emulated in firmware.

It would be ideal if "emulated" was used for any case of emulated accesses (firmware or in the kernel). Doing emulated accesses will be orders of magnitude slower than a processor that "slowly" handles the accesses.

So even if the processor performs a "slow" access, it could still be beneficial for the kernel to do the misaligned access rather than manual do the alignment.

Currently there is no place that takes into account this "slow" option but I wanted to leave it open for future optimizations.

Quoted 19 lines
Quoted 19 lines

However, the kernel will run much more slowly, or will not be

arch/riscv/Kconfig +6 -5
+     able to run at all, on CPUs that do not support unaligned memory
+     accesses.
+
 config RISCV_EFFICIENT_UNALIGNED_ACCESS
    bool "Assume the CPU 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 CPU supports fast 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
Quoted 8 lines

arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 749

Quoted 4 lines
arch/riscv/Kconfig +4 -0
+     able to run at all, on CPUs that do not support efficient unaligned
+     memory accesses.
+
+config RISCV_UNSUPPORTED_UNALIGNED_ACCESS

This option needs to be removed. The uabi states that unaligned access is supported in userspace, if the cpu or firmware does not implement unaligned access then the kernel must emulate it.

Quoted 1 line

Should it removed from hwprobe as well then?

We had added it as a hwprobe value in this discussion:
https://lore.kernel.org/lkml/Y+1VOXyKDDHEuejJ@spud/

Quoted 7 lines

Personally I like it as a possible hwprobe value, even if it is in conflict with the uabi. I can't fully defend it, other than as a very forward looking possibility, and as a nice value for people doing weird things off the beaten path. My preference would be to keep it in hwprobe, but I'm fine with not having a Kconfig for it.

-Evan

Seems reasonable to me, I will remove it from the Kconfig.

- Charlie