riscv: add support for sysctl unaligned_enabled control
authorClément Léger <cleger@rivosinc.com>
Wed, 4 Oct 2023 15:14:02 +0000 (17:14 +0200)
committerPalmer Dabbelt <palmer@rivosinc.com>
Wed, 1 Nov 2023 15:34:56 +0000 (08:34 -0700)
This sysctl tuning option allows the user to disable misaligned access
handling globally on the system. This will also be used by misaligned
detection code to temporarily disable misaligned access handling.

Signed-off-by: Clément Léger <cleger@rivosinc.com>
Reviewed-by: Björn Töpel <bjorn@rivosinc.com>
Link: https://lore.kernel.org/r/20231004151405.521596-6-cleger@rivosinc.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
arch/riscv/Kconfig
arch/riscv/kernel/traps_misaligned.c

index 6e167358a897578f5bcf4bc2aeb4084e4dcab056..1313f83bb0cbac2a78f519331156f13428c530ac 100644 (file)
@@ -638,6 +638,7 @@ config THREAD_SIZE_ORDER
 
 config RISCV_MISALIGNED
        bool "Support misaligned load/store traps for kernel and userspace"
+       select SYSCTL_ARCH_UNALIGN_ALLOW
        default y
        help
          Say Y here if you want the kernel to embed support for misaligned
index 041fd2dbd955a41ebfc4afc2723e47b40ae9b0da..b5fb1ff078e3230f9f7d6d6a84dfbd3b3c4ab1cf 100644 (file)
@@ -396,6 +396,9 @@ union reg_data {
        u64 data_u64;
 };
 
+/* sysctl hooks */
+int unaligned_enabled __read_mostly = 1;       /* Enabled by default */
+
 int handle_misaligned_load(struct pt_regs *regs)
 {
        union reg_data val;
@@ -406,6 +409,9 @@ int handle_misaligned_load(struct pt_regs *regs)
 
        perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, addr);
 
+       if (!unaligned_enabled)
+               return -1;
+
        if (get_insn(regs, epc, &insn))
                return -1;
 
@@ -502,6 +508,9 @@ int handle_misaligned_store(struct pt_regs *regs)
 
        perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, addr);
 
+       if (!unaligned_enabled)
+               return -1;
+
        if (get_insn(regs, epc, &insn))
                return -1;