Skip to content

Commit b69bcb1

Browse files
dramforeverPaul Walmsley
authored andcommitted
riscv: misaligned: Make enabling delegation depend on NONPORTABLE
The unaligned access emulation code in Linux has various deficiencies. For example, it doesn't emulate vector instructions [1] [2], and doesn't emulate KVM guest accesses. Therefore, requesting misaligned exception delegation with SBI FWFT actually regresses vector instructions' and KVM guests' behavior. Until Linux can handle it properly, guard these sbi_fwft_set() calls behind RISCV_SBI_FWFT_DELEGATE_MISALIGNED, which in turn depends on NONPORTABLE. Those who are sure that this wouldn't be a problem can enable this option, perhaps getting better performance. The rest of the existing code proceeds as before, except as if SBI_FWFT_MISALIGNED_EXC_DELEG is not available, to handle any remaining address misaligned exceptions on a best-effort basis. The KVM SBI FWFT implementation is also not touched, but it is disabled if the firmware emulates unaligned accesses. Cc: stable@vger.kernel.org Fixes: cf5a8ab ("riscv: misaligned: request misaligned exception from SBI") Reported-by: Songsong Zhang <U2FsdGVkX1@gmail.com> # KVM Link: https://lore.kernel.org/linux-riscv/38ce44c1-08cf-4e3f-8ade-20da224f529c@iscas.ac.cn/ [1] Link: https://lore.kernel.org/linux-riscv/b3cfcdac-0337-4db0-a611-258f2868855f@iscas.ac.cn/ [2] Signed-off-by: Vivian Wang <wangruikang@iscas.ac.cn> Acked-by: Conor Dooley <conor.dooley@microchip.com> Link: https://patch.msgid.link/20260401-riscv-misaligned-dont-delegate-v2-1-5014a288c097@iscas.ac.cn Signed-off-by: Paul Walmsley <pjw@kernel.org>
1 parent 50da1c9 commit b69bcb1

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

arch/riscv/Kconfig

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,28 @@ config RISCV_VECTOR_MISALIGNED
937937
help
938938
Enable detecting support for vector misaligned loads and stores.
939939

940+
config RISCV_SBI_FWFT_DELEGATE_MISALIGNED
941+
bool "Request firmware delegation of unaligned access exceptions"
942+
depends on RISCV_SBI
943+
depends on NONPORTABLE
944+
help
945+
Use SBI FWFT to request delegation of load address misaligned and
946+
store address misaligned exceptions, if possible, and prefer Linux
947+
kernel emulation of these accesses to firmware emulation.
948+
949+
Unfortunately, Linux's emulation is still incomplete. Namely, it
950+
currently does not handle vector instructions and KVM guest accesses.
951+
On platforms where these accesses would have been handled by firmware,
952+
enabling this causes unexpected kernel oopses, userspaces crashes and
953+
KVM guest crashes. If you are sure that these are not a problem for
954+
your platform, you can say Y here, which may improve performance.
955+
956+
Saying N here will not worsen emulation support for unaligned accesses
957+
even in the case where the firmware also has incomplete support. It
958+
simply keeps the firmware's emulation enabled.
959+
960+
If you don't know what to do here, say N.
961+
940962
choice
941963
prompt "Unaligned Accesses Support"
942964
default RISCV_PROBE_UNALIGNED_ACCESS

arch/riscv/kernel/traps_misaligned.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ static int cpu_online_check_unaligned_access_emulated(unsigned int cpu)
584584

585585
static bool misaligned_traps_delegated;
586586

587-
#ifdef CONFIG_RISCV_SBI
587+
#if defined(CONFIG_RISCV_SBI_FWFT_DELEGATE_MISALIGNED)
588588

589589
static int cpu_online_sbi_unaligned_setup(unsigned int cpu)
590590
{

0 commit comments

Comments
 (0)