Skip to content

Commit 2b2ca35

Browse files
Merge patch series "riscv: Use Kconfig to set unaligned access speed"
Charlie Jenkins <charlie@rivosinc.com> says: 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. * b4-shazam-merge: riscv: Set unaligned access speed at compile time riscv: Decouple emulated unaligned accesses from access speed riscv: Only check online cpus for emulated accesses riscv: lib: Introduce has_fast_unaligned_access() Link: https://lore.kernel.org/r/20240308-disable_misaligned_probe_config-v9-0-a388770ba0ce@rivosinc.com Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
2 parents a13a806 + f413aae commit 2b2ca35

File tree

8 files changed

+374
-293
lines changed

8 files changed

+374
-293
lines changed

arch/riscv/Kconfig

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -704,27 +704,61 @@ config THREAD_SIZE_ORDER
704704
affects irq stack size, which is equal to thread stack size.
705705

706706
config RISCV_MISALIGNED
707-
bool "Support misaligned load/store traps for kernel and userspace"
707+
bool
708708
select SYSCTL_ARCH_UNALIGN_ALLOW
709-
default y
710709
help
711-
Say Y here if you want the kernel to embed support for misaligned
712-
load/store for both kernel and userspace. When disable, misaligned
713-
accesses will generate SIGBUS in userspace and panic in kernel.
710+
Embed support for emulating misaligned loads and stores.
711+
712+
choice
713+
prompt "Unaligned Accesses Support"
714+
default RISCV_PROBE_UNALIGNED_ACCESS
715+
help
716+
This determines the level of support for unaligned accesses. This
717+
information is used by the kernel to perform optimizations. It is also
718+
exposed to user space via the hwprobe syscall. The hardware will be
719+
probed at boot by default.
720+
721+
config RISCV_PROBE_UNALIGNED_ACCESS
722+
bool "Probe for hardware unaligned access support"
723+
select RISCV_MISALIGNED
724+
help
725+
During boot, the kernel will run a series of tests to determine the
726+
speed of unaligned accesses. This probing will dynamically determine
727+
the speed of unaligned accesses on the underlying system. If unaligned
728+
memory accesses trap into the kernel as they are not supported by the
729+
system, the kernel will emulate the unaligned accesses to preserve the
730+
UABI.
731+
732+
config RISCV_EMULATED_UNALIGNED_ACCESS
733+
bool "Emulate unaligned access where system support is missing"
734+
select RISCV_MISALIGNED
735+
help
736+
If unaligned memory accesses trap into the kernel as they are not
737+
supported by the system, the kernel will emulate the unaligned
738+
accesses to preserve the UABI. When the underlying system does support
739+
unaligned accesses, the unaligned accesses are assumed to be slow.
740+
741+
config RISCV_SLOW_UNALIGNED_ACCESS
742+
bool "Assume the system supports slow unaligned memory accesses"
743+
depends on NONPORTABLE
744+
help
745+
Assume that the system supports slow unaligned memory accesses. The
746+
kernel and userspace programs may not be able to run at all on systems
747+
that do not support unaligned memory accesses.
714748

715749
config RISCV_EFFICIENT_UNALIGNED_ACCESS
716-
bool "Assume the CPU supports fast unaligned memory accesses"
750+
bool "Assume the system supports fast unaligned memory accesses"
717751
depends on NONPORTABLE
718752
select DCACHE_WORD_ACCESS if MMU
719753
select HAVE_EFFICIENT_UNALIGNED_ACCESS
720754
help
721-
Say Y here if you want the kernel to assume that the CPU supports
722-
efficient unaligned memory accesses. When enabled, this option
723-
improves the performance of the kernel on such CPUs. However, the
724-
kernel will run much more slowly, or will not be able to run at all,
725-
on CPUs that do not support efficient unaligned memory accesses.
755+
Assume that the system supports fast unaligned memory accesses. When
756+
enabled, this option improves the performance of the kernel on such
757+
systems. However, the kernel and userspace programs will run much more
758+
slowly, or will not be able to run at all, on systems that do not
759+
support efficient unaligned memory accesses.
726760

727-
If unsure what to do here, say N.
761+
endchoice
728762

729763
endmenu # "Platform type"
730764

arch/riscv/include/asm/cpufeature.h

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* SPDX-License-Identifier: GPL-2.0-only */
22
/*
3-
* Copyright 2022-2023 Rivos, Inc
3+
* Copyright 2022-2024 Rivos, Inc
44
*/
55

66
#ifndef _ASM_CPUFEATURE_H
@@ -28,29 +28,38 @@ struct riscv_isainfo {
2828

2929
DECLARE_PER_CPU(struct riscv_cpuinfo, riscv_cpuinfo);
3030

31-
DECLARE_PER_CPU(long, misaligned_access_speed);
32-
3331
/* Per-cpu ISA extensions. */
3432
extern struct riscv_isainfo hart_isa[NR_CPUS];
3533

3634
void riscv_user_isa_enable(void);
3735

38-
#ifdef CONFIG_RISCV_MISALIGNED
39-
bool unaligned_ctl_available(void);
40-
bool check_unaligned_access_emulated(int cpu);
36+
#if defined(CONFIG_RISCV_MISALIGNED)
37+
bool check_unaligned_access_emulated_all_cpus(void);
4138
void unaligned_emulation_finish(void);
39+
bool unaligned_ctl_available(void);
40+
DECLARE_PER_CPU(long, misaligned_access_speed);
4241
#else
4342
static inline bool unaligned_ctl_available(void)
4443
{
4544
return false;
4645
}
46+
#endif
47+
48+
#if defined(CONFIG_RISCV_PROBE_UNALIGNED_ACCESS)
49+
DECLARE_STATIC_KEY_FALSE(fast_unaligned_access_speed_key);
4750

48-
static inline bool check_unaligned_access_emulated(int cpu)
51+
static __always_inline bool has_fast_unaligned_accesses(void)
4952
{
50-
return false;
53+
return static_branch_likely(&fast_unaligned_access_speed_key);
54+
}
55+
#else
56+
static __always_inline bool has_fast_unaligned_accesses(void)
57+
{
58+
if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
59+
return true;
60+
else
61+
return false;
5162
}
52-
53-
static inline void unaligned_emulation_finish(void) {}
5463
#endif
5564

5665
unsigned long riscv_get_elf_hwcap(void);
@@ -135,6 +144,4 @@ static __always_inline bool riscv_cpu_has_extension_unlikely(int cpu, const unsi
135144
return __riscv_isa_extension_available(hart_isa[cpu].isa, ext);
136145
}
137146

138-
DECLARE_STATIC_KEY_FALSE(fast_misaligned_access_speed_key);
139-
140147
#endif

arch/riscv/kernel/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ extra-y += vmlinux.lds
3838
obj-y += head.o
3939
obj-y += soc.o
4040
obj-$(CONFIG_RISCV_ALTERNATIVE) += alternative.o
41-
obj-y += copy-unaligned.o
4241
obj-y += cpu.o
4342
obj-y += cpufeature.o
4443
obj-y += entry.o
@@ -62,6 +61,9 @@ obj-y += tests/
6261
obj-$(CONFIG_MMU) += vdso.o vdso/
6362

6463
obj-$(CONFIG_RISCV_MISALIGNED) += traps_misaligned.o
64+
obj-$(CONFIG_RISCV_MISALIGNED) += unaligned_access_speed.o
65+
obj-$(CONFIG_RISCV_PROBE_UNALIGNED_ACCESS) += copy-unaligned.o
66+
6567
obj-$(CONFIG_FPU) += fpu.o
6668
obj-$(CONFIG_RISCV_ISA_V) += vector.o
6769
obj-$(CONFIG_RISCV_ISA_V) += kernel_mode_vector.o

0 commit comments

Comments
 (0)