riscv: Use Kconfig to set unaligned access speed
v5 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 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 (2):
riscv: lib: Introduce has_fast_unaligned_access function
riscv: Set unaligned access speed at compile time
arch/riscv/Kconfig | 47 +++++- arch/riscv/include/asm/cpufeature.h | 43 ++++- arch/riscv/kernel/Makefile | 6 +- arch/riscv/kernel/cpufeature.c | 255 ------------------------------ arch/riscv/kernel/probe_emulated_access.c | 64 ++++++++ arch/riscv/kernel/sys_hwprobe.c | 23 +++ arch/riscv/kernel/traps_misaligned.c | 54 +------ arch/riscv/lib/csum.c | 7 +- 8 files changed, 173 insertions(+), 326 deletions(-)
---
base-commit: 6613476e225e090cc9aad49be7fa504e290dd33d
change-id: 20240131-disable_misaligned_probe_config-043aea375f93
Signature · 1 line
- Charlie
PATCH v5 1/2 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
@@ -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
@@ -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
@@ -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
PATCH v5 2/2 riscv: Set unaligned access speed at compile time
In reply to Charlie JenkinsIntroduce 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 | 47 +++++- arch/riscv/include/asm/cpufeature.h | 32 +++- arch/riscv/kernel/Makefile | 6 +- arch/riscv/kernel/cpufeature.c | 255 ------------------------------ arch/riscv/kernel/probe_emulated_access.c | 64 ++++++++ arch/riscv/kernel/sys_hwprobe.c | 23 +++ arch/riscv/kernel/traps_misaligned.c | 54 +------ 7 files changed, 163 insertions(+), 318 deletions(-)
arch/riscv/Kconfig +40 -7
@@ -690,25 +690,58 @@ 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 probing will dynamically determine + the speed of unaligned accesses on the boot hardware. + +config RISCV_EMULATED_UNALIGNED_ACCESS + bool "Assume the system expects emulated unaligned memory accesses" + help + Assume that the system expects emulated unaligned memory accesses. + When enabled, this option notifies the kernel and userspace that + unaligned memory accesses will be emulated by either the kernel or + firmware. + +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 +29 -3
@@ -28,17 +28,29 @@ 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 +#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 ) {} +#endif #else static inline bool unaligned_ctl_available (void ) {
@@ -53,12 +65,26 @@ 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_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 +5 -1
@@ -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 += unaligned_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/probe_emulated_access.c new file +64 -0
64 lines not on this page · raw message
arch/riscv/kernel/sys_hwprobe.c +23 -0
@@ -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,27 @@ 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 ; +} +#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.2
Re: [PATCH v5 2/2] riscv: Set unaligned access speed at compile time
arch/riscv/kernel/sys_hwprobe.c static bool hwprobe_ext0_has(const struct cpumask *cpus, unsigned long ext) · line 154
Quoted 13 lines · Charlie Jenkins
arch/riscv/kernel/sys_hwprobe.c +2
-0@@ -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 looks like a left over testing hack.
Quoted 18 lines
arch/riscv/kernel/sys_hwprobe.c +11
-0int cpu;u64 perf =-1ULL ;@@ -169,6 +171,27 @@ 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)
arch/riscv/kernel/sys_hwprobe.c static u64 hwprobe_misaligned(const struct cpumask *cpus) · line 194
Quoted 10 lines
arch/riscv/kernel/sys_hwprobe.c +10
-0+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 ; +} +#endif
I'm curious why we need multiple definitions of this here. My first question is why we cannot have
if (IS_ENABLED(foo))
return bar;
inside the existing function for these cases.
My second question would be why fiddle with this in hwprobe at all? I was specifically wondering why the kernel would not track this information in the per-cpu variable misaligned_access_speed and the syscall code could be left in the dark about how this is implemented.
Quoted 17 lines
arch/riscv/kernel/sys_hwprobe.c +0
-0static void hwprobe_one_pair (struct riscv_hwprobe *pair,const struct cpumask *cpus)arch/riscv/kernel/traps_misaligned.c +0
-2@@ -398,8 +398,6 @@ union reg_data {u64 data_u64; }; -static bool unaligned_ctl__read_mostly ; -/* sysctl hooks */ int unaligned_enabled__read_mostly =1 ;/* Enabled by default */
arch/riscv/kernel/traps_misaligned.c int handle_misaligned_load(struct pt_regs *regs) · line 414
Quoted 4 lines
arch/riscv/kernel/traps_misaligned.c +1
-0@@ -413,7 +411,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
if (IS_ENABLED()), no?
But perhaps more interestingly - why is this being set here at all? My understanding of the emulated stuff was that if the in-kernel emulation was enabled, it would be used unless the CPU itself supported fast accesses. I would expect this to be advertised to userspace once the system has booted, not set the first time that a cpu emulates an access,
I feel like I need to take a look at how this has been implemented, I never really looked too closely at it and how it is enabled does not match my expectations.
Cheers,
Conor.
Quoted 68 lines
arch/riscv/kernel/traps_misaligned.c +1
-50*this_cpu_ptr (&misaligned_access_speed) =RISCV_HWPROBE_MISALIGNED_EMULATED ; +#endif if (!unaligned_enabled)return -1 ;@@ -595,53 +595,3 @@ int handle_misaligned_store(struct pt_regs *regs)return 0 ; } - -bool check_unaligned_access_emulated (int cpu) -{ -long *mas_ptr =per_cpu_ptr (&misaligned_access_speed, cpu); -unsigned long tmp_var, tmp_val; -bool misaligned_emu_detected; - - *mas_ptr =RISCV_HWPROBE_MISALIGNED_UNKNOWN ; - - __asm__ __volatile__ ( -" " REG_L " %[tmp], 1(%[ptr])\n" - : [tmp]"=r" (tmp_val) : [ptr]"r" (&tmp_var) :"memory" ); - - misaligned_emu_detected = (*mas_ptr ==RISCV_HWPROBE_MISALIGNED_EMULATED ); -/* -* If unaligned_ctl is already set, this means that we detected that all -* CPUS uses emulated misaligned access at boot time. If that changed -* when hotplugging the new cpu, this is something we don't handle. -*/ -if (unlikely (unaligned_ctl && !misaligned_emu_detected)) { -pr_crit ("CPU misaligned accesses non homogeneous (expected all emulated)\n" ); -while (true) -cpu_relax (); - } - -return misaligned_emu_detected; -} - -void unaligned_emulation_finish (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_present_cpu (cpu) { -if (per_cpu (misaligned_access_speed, cpu) != -RISCV_HWPROBE_MISALIGNED_EMULATED ) { -return ; - } - } - unaligned_ctl = true; -} - -bool unaligned_ctl_available (void ) -{ -return unaligned_ctl; -}Signature · 8 lines
-- 2.43.2 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Re: [PATCH v5 2/2] riscv: Set unaligned access speed at compile time
Clement,
Quoted 12 lines · Conor Dooley
arch/riscv/kernel/traps_misaligned.c int handle_misaligned_load(struct pt_regs *regs) · line 414
Quoted 5 lines
arch/riscv/kernel/traps_misaligned.c +1
-0@@ -413,7 +411,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 if (
IS_ENABLED()), no?
But perhaps more interestingly - why is this being set here at all? My understanding of the emulated stuff was that if the in-kernel emulation was enabled, it would be used unless the CPU itself supported fast accesses.
Quoted 6 lines
I would expect this to be advertised to userspace once
the system has booted, not set the first time that a cpu emulates an access,
I feel like I need to take a look at how this has been implemented, I never really looked too closely at it and how it is enabled does not match my expectations.
I did look at this more closely and I understand why this is being done. In the vast majority of situations I think it is going to work perfectly fine, since that first access is going to be during the boot time probing.
.
Quoted 5 lines
arch/riscv/kernel/traps_misaligned.c int handle_misaligned_load(struct pt_regs *regs) · line 419
Quoted 5 lines
arch/riscv/kernel/traps_misaligned.c +1
-0*this_cpu_ptr (&misaligned_access_speed) =RISCV_HWPROBE_MISALIGNED_EMULATED ; +#endif if (!unaligned_enabled)return -1 ;
This is in the diff, so I am commenting here. Firstly, why does this return -1 and not a regular negative errno?
Secondly, this looks pretty suspect to me - if the sysctl is used to disable the emulation things like hwprobe will still report that access is emulated if during boot it was enabled.
I think that if this condition is hit, we actually need to change the per-cpu variable to set the alignment to unsupported, given the emulation will (at present & if my understanding is correct) only be enabled when there's not underlying support for unaligned accesses.
Thoughts?
Cheers,
Conor.
Re: [PATCH v5 2/2] riscv: Set unaligned access speed at compile time
In reply to Charlie JenkinsQuoted 57 lines · Charlie Jenkins
arch/riscv/Kconfig +34
-4@@ -690,25 +690,58 @@ 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 probing will dynamically determine + the speed of unaligned accesses on the boot hardware. + +config RISCV_EMULATED_UNALIGNED_ACCESS + bool "Assume the system expects emulated unaligned memory accesses" + help + Assume that the system expects emulated unaligned memory accesses. + When enabled, this option notifies the kernel and userspace that + unaligned memory accesses will be emulated by either the kernel or + firmware. + +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
arch/riscv/Kconfig config THREAD_SIZE_ORDER · line 744
Quoted 10 lines
arch/riscv/Kconfig +6
-3- 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
Thinking about this some more, you've got 6 different options here:
1 probed with no emulation available (choice set to probe + RISCV_MISALIGNED=n) 2 probe with in-kernel emulation available (choice set to probe + RISCV_MISALIGNED=y) 3 in-kernel emulation only (choice set to emulated + RISCV_MISALIGNED=y)
4 no in-kernel emulation but report emulated (choice set to emulated + RISCV_MISALIGNED=n) 5 slow unaligned (choice set to slow)
6 fast unaligned (choice set to fast)
Out of these, only 2 and 3 are portable options, since 1, 4 and 5 will cause uabi issues if the CPUs or firmware does not support unaligned access & 6 will not run in the same circumstances.
My first thought here was about the motivation for the patch and what it has resulted in. Being able to support HAVE_EFFICIENT_ALIGNED_ACCESS is pretty nice, but it then seems like beyond that we are introducing configuration for configurations sake, without looking at what the resultant kernels will be useful for. Having 6 different options for how the kernel can be configured in this way seems excessive and I don't really get why some of them are even useful.
Take for example situation 4. Unless I have misunderstood the Kconfig options above, if you configure a kernel in that way, it will always report as emulated, but there is no emulation provided. This just seems like a option that's only purpose is setting a hwprobe value, which is a dog wagging the tail situation to me.
The other thing is about what options are actually marked as NONPORTABLE, given it is done in the choice option - but whether or not something is actually non-portable actually depends on whether or not the in-kernel emulator exists.
I think what I would do here is simplify this quite a bit, starting by making RISCV_MISALIGNED an internal option that users cannot enable but is selected by the PORTABLE choice options. I would then re-work the choice options a bit. My 4 would be:
1 probe: probe at boot time, falling back to emulated if not performant 2 emulated: always emulate it in the kernel
3 slow: don't probe or emulate in the kernel
4 fast: Your current fast option
1 & 2 select RISCV_UNALIGNED and are portable because they contain the emulated support and thus satisfy the UABI constaints.
3 & 4 are marked NONPORTABLE. I think 3 will run on systems that don't support unaligned accesses but it will have UABI problems. 4 for the reason mentioned in the Kconfig option above.
I think that that gives you 4 meaningful options, what do you think?
Cheers,
Conor.
Re: [PATCH v5 2/2] riscv: Set unaligned access speed at compile time
Quoted 91 lines · Conor Dooley
Quoted 57 lines · Charlie Jenkins
arch/riscv/Kconfig +34
-4@@ -690,25 +690,58 @@ 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 probing will dynamically determine + the speed of unaligned accesses on the boot hardware. + +config RISCV_EMULATED_UNALIGNED_ACCESS + bool "Assume the system expects emulated unaligned memory accesses" + help + Assume that the system expects emulated unaligned memory accesses. + When enabled, this option notifies the kernel and userspace that + unaligned memory accesses will be emulated by either the kernel or + firmware. + +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, thearch/riscv/Kconfig config THREAD_SIZE_ORDER · line 744
Quoted 10 lines
arch/riscv/Kconfig +6
-3- 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. +endchoiceThinking about this some more, you've got 6 different options here:
1 probed with no emulation available (choice set to probe + RISCV_MISALIGNED=n) 2 probe with in-kernel emulation available (choice set to probe + RISCV_MISALIGNED=y) 3 in-kernel emulation only (choice set to emulated + RISCV_MISALIGNED=y)
4 no in-kernel emulation but report emulated (choice set to emulated + RISCV_MISALIGNED=n) 5 slow unaligned (choice set to slow)
6 fast unaligned (choice set to fast)Out of these, only 2 and 3 are portable options, since 1, 4 and 5 will cause uabi issues if the CPUs or firmware does not support unaligned access & 6 will not run in the same circumstances.
My first thought here was about the motivation for the patch and what it has resulted in. Being able to support
HAVE_EFFICIENT_ALIGNED_ACCESSis pretty nice, but it then seems like beyond that we are introducing configuration for configurations sake, without looking at what the resultant kernels will be useful for. Having 6 different options for how the kernel can be configured in this way seems excessive and I don't really get why some of them are even useful.
Quoted 5 lines
Take for example situation 4. Unless I have misunderstood the
Kconfigoptions above, if you configure a kernel in that way, it will always report as emulated, but there is no emulation provided. This just seems like a option that's only purpose is setting a hwprobe value, which is a dog wagging the tail situation to me.
This goes back to my earlier comment that it would make sense for somebody to select "emulated" even if the unaligned address is being emulated by firmware. However, there may be no users for this and if needed we can add that in the future.
Quoted 11 lines
The other thing is about what options are actually marked as NONPORTABLE, given it is done in the choice option - but whether or not something is actually non-portable actually depends on whether or not the in-kernel emulator exists.
I think what I would do here is simplify this quite a bit, starting by making
RISCV_MISALIGNEDan internal option that users cannot enable but is selected by the PORTABLE choice options. I would then re-work the choice options a bit. My 4 would be:
Quoted 4 lines
1 probe: probe at boot time, falling back to emulated if not performant 2 emulated: always emulate it in the kernel
3 slow: don't probe or emulate in the kernel
4 fast: Your current fast option
Emulated doesn't mean that the kernel will always emulate the unaligned accesses. It means that the kernel has the ability to emulate them. It will only emulate them if the CPU traps on unaligned accesses. Kernel code can choose to forcefully align an address it thinks may cause an unaligned access, but that's slightly different from emulated.
Emulated is much slower than "slow", so it seems like it unfairly penalizes "slow" to group the options for firmware emulated with a CPU that actually just does slow unaligned accesses.
- Charlie
Quoted 11 lines
1 & 2 select
RISCV_UNALIGNEDand are portable because they contain the emulated support and thus satisfy the UABI constaints.
3 & 4 are marked NONPORTABLE. I think 3 will run on systems that don't support unaligned accesses but it will have UABI problems. 4 for the reason mentioned in theKconfigoption above.I think that that gives you 4 meaningful options, what do you think?
Cheers,
Conor.
Re: [PATCH v5 2/2] riscv: Set unaligned access speed at compile time
Quoted 98 lines · Charlie Jenkins
Quoted 91 lines · Conor Dooley
Quoted 57 lines · Charlie Jenkins
arch/riscv/Kconfig +34
-4@@ -690,25 +690,58 @@ 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 probing will dynamically determine + the speed of unaligned accesses on the boot hardware. + +config RISCV_EMULATED_UNALIGNED_ACCESS + bool "Assume the system expects emulated unaligned memory accesses" + help + Assume that the system expects emulated unaligned memory accesses. + When enabled, this option notifies the kernel and userspace that + unaligned memory accesses will be emulated by either the kernel or + firmware. + +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, thearch/riscv/Kconfig config THREAD_SIZE_ORDER · line 744
Quoted 10 lines
arch/riscv/Kconfig +6
-3- 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. +endchoiceThinking about this some more, you've got 6 different options here:
1 probed with no emulation available (choice set to probe + RISCV_MISALIGNED=n) 2 probe with in-kernel emulation available (choice set to probe + RISCV_MISALIGNED=y) 3 in-kernel emulation only (choice set to emulated + RISCV_MISALIGNED=y)
4 no in-kernel emulation but report emulated (choice set to emulated + RISCV_MISALIGNED=n) 5 slow unaligned (choice set to slow)
6 fast unaligned (choice set to fast)Out of these, only 2 and 3 are portable options, since 1, 4 and 5 will cause uabi issues if the CPUs or firmware does not support unaligned access & 6 will not run in the same circumstances.
My first thought here was about the motivation for the patch and what it has resulted in. Being able to support
HAVE_EFFICIENT_ALIGNED_ACCESSis pretty nice, but it then seems like beyond that we are introducing configuration for configurations sake, without looking at what the resultant kernels will be useful for. Having 6 different options for how the kernel can be configured in this way seems excessive and I don't really get why some of them are even useful.Quoted 5 lines
Take for example situation 4. Unless I have misunderstood the
Kconfigoptions above, if you configure a kernel in that way, it will always report as emulated, but there is no emulation provided. This just seems like a option that's only purpose is setting a hwprobe value, which is a dog wagging the tail situation to me.
Quoted 4 lines
This goes back to my earlier comment that it would make sense for somebody to select "emulated" even if the unaligned address is being emulated by firmware. However, there may be no users for this and if needed we can add that in the future.
I don't think that is going to provide any useful information, give you will never be able to know if slow means "slow but done by the cpu" or "slow but emulated by the firmware".
Quoted 15 lines
Quoted 10 lines
The other thing is about what options are actually marked as NONPORTABLE, given it is done in the choice option - but whether or not something is actually non-portable actually depends on whether or not the in-kernel emulator exists.
I think what I would do here is simplify this quite a bit, starting by making
RISCV_MISALIGNEDan internal option that users cannot enable but is selected by the PORTABLE choice options. I would then re-work the choice options a bit. My 4 would be:Quoted 4 lines
1 probe: probe at boot time, falling back to emulated if not performant 2 emulated: always emulate it in the kernel
3 slow: don't probe or emulate in the kernel
4 fast: Your current fast option
Quoted 5 lines
Emulated doesn't mean that the kernel will always emulate the unaligned accesses. It means that the kernel has the ability to emulate them. It will only emulate them if the CPU traps on unaligned accesses. Kernel code can choose to forcefully align an address it thinks may cause an unaligned access, but that's slightly different from emulated.
Sure, make option 2 "don't probe at boot time, emulate it in the kernel if we trap". I suppose in this case though, to get a correct output in hwprobe you'd have to still attempt an unaligned access at boot time to see if you trap but it will not perform the speed test?
Quoted 3 lines
Emulated is much slower than "slow", so it seems like it unfairly penalizes "slow" to group the options for firmware emulated with a CPU that actually just does slow unaligned accesses.
As I said in my previous email, "slow" has a huge variance and may not be meaningfully faster. I wonder if it might actually even be slower on some systems that emulate it in firmware?
Quoted 7 lines
Quoted 7 lines
1 & 2 select
RISCV_UNALIGNEDand are portable because they contain the emulated support and thus satisfy the UABI constaints.
3 & 4 are marked NONPORTABLE. I think 3 will run on systems that don't support unaligned accesses but it will have UABI problems. 4 for the reason mentioned in theKconfigoption above.I think that that gives you 4 meaningful options, what do you think?
Re: [PATCH v5 2/2] riscv: Set unaligned access speed at compile time
Quoted 99 lines · Conor Dooley
Quoted 98 lines · Charlie Jenkins
Quoted 96 lines · Conor Dooley
Quoted 57 lines · Charlie Jenkins
arch/riscv/Kconfig +34
-4@@ -690,25 +690,58 @@ 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 probing will dynamically determine + the speed of unaligned accesses on the boot hardware. + +config RISCV_EMULATED_UNALIGNED_ACCESS + bool "Assume the system expects emulated unaligned memory accesses" + help + Assume that the system expects emulated unaligned memory accesses. + When enabled, this option notifies the kernel and userspace that + unaligned memory accesses will be emulated by either the kernel or + firmware. + +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, thearch/riscv/Kconfig config THREAD_SIZE_ORDER · line 744
Quoted 10 lines
arch/riscv/Kconfig +6
-3- 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. +endchoiceThinking about this some more, you've got 6 different options here:
1 probed with no emulation available (choice set to probe + RISCV_MISALIGNED=n) 2 probe with in-kernel emulation available (choice set to probe + RISCV_MISALIGNED=y) 3 in-kernel emulation only (choice set to emulated + RISCV_MISALIGNED=y)
4 no in-kernel emulation but report emulated (choice set to emulated + RISCV_MISALIGNED=n) 5 slow unaligned (choice set to slow)
6 fast unaligned (choice set to fast)Out of these, only 2 and 3 are portable options, since 1, 4 and 5 will cause uabi issues if the CPUs or firmware does not support unaligned access & 6 will not run in the same circumstances.
My first thought here was about the motivation for the patch and what it has resulted in. Being able to support
HAVE_EFFICIENT_ALIGNED_ACCESSis pretty nice, but it then seems like beyond that we are introducing configuration for configurations sake, without looking at what the resultant kernels will be useful for. Having 6 different options for how the kernel can be configured in this way seems excessive and I don't really get why some of them are even useful.Take for example situation 4. Unless I have misunderstood the
Kconfigoptions above, if you configure a kernel in that way, it will always report as emulated, but there is no emulation provided. This just seems like a option that's only purpose is setting a hwprobe value, which is a dog wagging the tail situation to me.
Quoted 8 lines
Quoted 4 lines
This goes back to my earlier comment that it would make sense for somebody to select "emulated" even if the unaligned address is being emulated by firmware. However, there may be no users for this and if needed we can add that in the future.
I don't think that is going to provide any useful information, give you will never be able to know if slow means "slow but done by the cpu" or "slow but emulated by the firmware".
Okay makes sense. If somebody has a usecase in the future it would be easy to change without breaking any UABI.
Quoted 22 lines
Quoted 16 lines
Quoted 11 lines
The other thing is about what options are actually marked as NONPORTABLE, given it is done in the choice option - but whether or not something is actually non-portable actually depends on whether or not the in-kernel emulator exists.
I think what I would do here is simplify this quite a bit, starting by making
RISCV_MISALIGNEDan internal option that users cannot enable but is selected by the PORTABLE choice options. I would then re-work the choice options a bit. My 4 would be:Quoted 4 lines
1 probe: probe at boot time, falling back to emulated if not performant 2 emulated: always emulate it in the kernel
3 slow: don't probe or emulate in the kernel
4 fast: Your current fast optionQuoted 5 lines
Emulated doesn't mean that the kernel will always emulate the unaligned accesses. It means that the kernel has the ability to emulate them. It will only emulate them if the CPU traps on unaligned accesses. Kernel code can choose to forcefully align an address it thinks may cause an unaligned access, but that's slightly different from emulated.
Quoted 4 lines
Sure, make option 2 "don't probe at boot time, emulate it in the kernel if we trap". I suppose in this case though, to get a correct output in hwprobe you'd have to still attempt an unaligned access at boot time to see if you trap but it will not perform the speed test?
Are you trying to cover the case here that the kernel is compiled as "emulate unaligned accesses" but the kernel isn't actually needed to emulate unaligned accesses? Seems like if the kernel is compiled as such it would make sense to report emulated with the assumption that if the kernel isn't emulating it, something else is.
Quoted 8 lines
Quoted 4 lines
Emulated is much slower than "slow", so it seems like it unfairly penalizes "slow" to group the options for firmware emulated with a CPU that actually just does slow unaligned accesses.
As I said in my previous email, "slow" has a huge variance and may not be meaningfully faster. I wonder if it might actually even be slower on some systems that emulate it in firmware?
I have no real data and can only speculate. Your 4 options seem to cover all of the "meaningful" use cases so I can make the change in a new version.
- Charlie
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
1 & 2 select
RISCV_UNALIGNEDand are portable because they contain the emulated support and thus satisfy the UABI constaints.
3 & 4 are marked NONPORTABLE. I think 3 will run on systems that don't support unaligned accesses but it will have UABI problems. 4 for the reason mentioned in theKconfigoption above.I think that that gives you 4 meaningful options, what do you think?
Re: [PATCH v5 2/2] riscv: Set unaligned access speed at compile time
Quoted 11 lines · Charlie Jenkins
Quoted 11 lines
Quoted 10 lines
Quoted 4 lines
1 probe: probe at boot time, falling back to emulated if not performant 2 emulated: always emulate it in the kernel
3 slow: don't probe or emulate in the kernel
4 fast: Your current fast optionEmulated doesn't mean that the kernel will always emulate the unaligned accesses. It means that the kernel has the ability to emulate them. It will only emulate them if the CPU traps on unaligned accesses. Kernel code can choose to forcefully align an address it thinks may cause an unaligned access, but that's slightly different from emulated.
Quoted 8 lines
Quoted 4 lines
Sure, make option 2 "don't probe at boot time, emulate it in the kernel if we trap". I suppose in this case though, to get a correct output in hwprobe you'd have to still attempt an unaligned access at boot time to see if you trap but it will not perform the speed test?
Are you trying to cover the case here that the kernel is compiled as "emulate unaligned accesses" but the kernel isn't actually needed to emulate unaligned accesses?
Nope, the case "don't probe at boot time, emulate it in the kernel if we trap" which is replacing 2 above.
Quoted 3 lines
Seems like if the kernel is compiled as
such it would make sense to report emulated with the assumption that if the kernel isn't emulating it, something else is.
Or maybe nothing is emulating it, we don't know. Feels to me like it should report slow by default, given that's the option you can infer the least information from, and then report emulated on trap.