Skip to content

Commit f413aae

Browse files
charlie-rivospalmer-dabbelt
authored andcommitted
riscv: Set unaligned access speed at compile time
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. Signed-off-by: Charlie Jenkins <charlie@rivosinc.com> Reviewed-by: Conor Dooley <conor.dooley@microchip.com> Tested-by: Samuel Holland <samuel.holland@sifive.com> Link: https://lore.kernel.org/r/20240308-disable_misaligned_probe_config-v9-4-a388770ba0ce@rivosinc.com Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
1 parent 6e5ce7f commit f413aae

File tree

7 files changed

+359
-296
lines changed

7 files changed

+359
-296
lines changed

arch/riscv/Kconfig

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

690690
config RISCV_MISALIGNED
691-
bool "Support misaligned load/store traps for kernel and userspace"
691+
bool
692692
select SYSCTL_ARCH_UNALIGN_ALLOW
693-
default y
694693
help
695-
Say Y here if you want the kernel to embed support for misaligned
696-
load/store for both kernel and userspace. When disable, misaligned
697-
accesses will generate SIGBUS in userspace and panic in kernel.
694+
Embed support for emulating misaligned loads and stores.
695+
696+
choice
697+
prompt "Unaligned Accesses Support"
698+
default RISCV_PROBE_UNALIGNED_ACCESS
699+
help
700+
This determines the level of support for unaligned accesses. This
701+
information is used by the kernel to perform optimizations. It is also
702+
exposed to user space via the hwprobe syscall. The hardware will be
703+
probed at boot by default.
704+
705+
config RISCV_PROBE_UNALIGNED_ACCESS
706+
bool "Probe for hardware unaligned access support"
707+
select RISCV_MISALIGNED
708+
help
709+
During boot, the kernel will run a series of tests to determine the
710+
speed of unaligned accesses. This probing will dynamically determine
711+
the speed of unaligned accesses on the underlying system. If unaligned
712+
memory accesses trap into the kernel as they are not supported by the
713+
system, the kernel will emulate the unaligned accesses to preserve the
714+
UABI.
715+
716+
config RISCV_EMULATED_UNALIGNED_ACCESS
717+
bool "Emulate unaligned access where system support is missing"
718+
select RISCV_MISALIGNED
719+
help
720+
If unaligned memory accesses trap into the kernel as they are not
721+
supported by the system, the kernel will emulate the unaligned
722+
accesses to preserve the UABI. When the underlying system does support
723+
unaligned accesses, the unaligned accesses are assumed to be slow.
724+
725+
config RISCV_SLOW_UNALIGNED_ACCESS
726+
bool "Assume the system supports slow unaligned memory accesses"
727+
depends on NONPORTABLE
728+
help
729+
Assume that the system supports slow unaligned memory accesses. The
730+
kernel and userspace programs may not be able to run at all on systems
731+
that do not support unaligned memory accesses.
698732

699733
config RISCV_EFFICIENT_UNALIGNED_ACCESS
700-
bool "Assume the CPU supports fast unaligned memory accesses"
734+
bool "Assume the system supports fast unaligned memory accesses"
701735
depends on NONPORTABLE
702736
select DCACHE_WORD_ACCESS if MMU
703737
select HAVE_EFFICIENT_UNALIGNED_ACCESS
704738
help
705-
Say Y here if you want the kernel to assume that the CPU supports
706-
efficient unaligned memory accesses. When enabled, this option
707-
improves the performance of the kernel on such CPUs. However, the
708-
kernel will run much more slowly, or will not be able to run at all,
709-
on CPUs that do not support efficient unaligned memory accesses.
739+
Assume that the system supports fast unaligned memory accesses. When
740+
enabled, this option improves the performance of the kernel on such
741+
systems. However, the kernel and userspace programs will run much more
742+
slowly, or will not be able to run at all, on systems that do not
743+
support efficient unaligned memory accesses.
710744

711-
If unsure what to do here, say N.
745+
endchoice
712746

713747
endmenu # "Platform type"
714748

arch/riscv/include/asm/cpufeature.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,39 @@ 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);
36+
#if defined(CONFIG_RISCV_MISALIGNED)
4037
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
}
47-
48-
static inline bool check_unaligned_access_emulated(int cpu)
49-
{
50-
return false;
51-
}
52-
53-
static inline void unaligned_emulation_finish(void) {}
5446
#endif
5547

48+
#if defined(CONFIG_RISCV_PROBE_UNALIGNED_ACCESS)
5649
DECLARE_STATIC_KEY_FALSE(fast_unaligned_access_speed_key);
5750

5851
static __always_inline bool has_fast_unaligned_accesses(void)
5952
{
6053
return static_branch_likely(&fast_unaligned_access_speed_key);
6154
}
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;
62+
}
63+
#endif
6264

6365
unsigned long riscv_get_elf_hwcap(void);
6466

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)