Currently, CONFIG_ARCH_STRICT_ALIGN is enabled by default in Kconfig to prevent unaligned memory accesses on certain LoongArch processors without hardware support (such as Loongson-2K0300/2K0500/2K1000). However, all Loongson-3 series processors and some newer Loongson-2K processors (Loongson-2K2000/2K3000) natively support unaligned access in hardware. Thus, keeping -mstrict-align enabled by default degrades performance and increases code size on mainstream hardware. Moreover, when ARCH_STRICT_ALIGN is disabled, unaligned.o is not built and software emulation is compiled out. As a result, a single generic kernel image built without ARCH_STRICT_ALIGN cannot run on processors without hardware unaligned access support. Enable HAVE_EFFICIENT_UNALIGNED_ACCESS by default through disabling CONFIG_ARCH_STRICT_ALIGN by default so that modern LoongArch systems can fully benefit from hardware unaligned access, allowing subsystems such as networking, bpf, and cryptography to perform direct word-size unaligned loads/stores instead of falling back to slow byte-by-byte accesses or extra alignment padding. To preserve compatibility so that a single generic kernel image can still run on certain processors without hardware UAL support, keep software unaligned access emulation always available as a fallback (while hot paths like memcpy/memset/copy_user already dynamically switch via ALTERNATIVE based on CPU_FEATURE_UAL). For more info, please refer to: commit 61a6fccc0bd2 ("LoongArch: Add unaligned access support") commit 41596803302d ("LoongArch: Make -mstrict-align configurable") commit f5003098e2f3 ("LoongArch: Update help info of ARCH_STRICT_ALIGN") Signed-off-by: Tiezhu Yang --- Based on the following fix patch: LoongArch: Call fixup_exception() for unaligned accesses https://lore.kernel.org/loongarch/20261203012908.27076-1-yangtiezhu@loongson.cn/ Only tested on Loongson-3A5000/3A6000/3C6000 with hardware UAL support. Not tested on the processors without hardware UAL support, any feedback from testers with such hardware is welcome. With loongson64_defconfig, disabling CONFIG_ARCH_STRICT_ALIGN reduces the text size by 596,688 bytes (~583 KB, -2.35%) and total size by 613,683 bytes (~599 KB, -1.16%): $ size -G vmlinux.before vmlinux.after text data bss total filename 25387840 25547957 1789236 52725033 vmlinux.before 24791152 25540178 1780020 52111350 vmlinux.after arch/loongarch/Kconfig | 14 +++++++++----- arch/loongarch/kernel/Makefile | 4 +--- arch/loongarch/kernel/traps.c | 8 -------- 3 files changed, 10 insertions(+), 16 deletions(-) diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig index b5ed4881d3c0..d2b1ef33ae71 100644 --- a/arch/loongarch/Kconfig +++ b/arch/loongarch/Kconfig @@ -604,7 +604,7 @@ config ARCH_WRITECOMBINE config ARCH_STRICT_ALIGN bool "Enable -mstrict-align to prevent unaligned accesses" if EXPERT - default y + default n help Not all LoongArch cores support h/w unaligned access, we can use -mstrict-align build parameter to prevent unaligned accesses. @@ -619,10 +619,14 @@ config ARCH_STRICT_ALIGN If you want to make sure whether to support unaligned memory access on your hardware, please read the bit 20 (UAL) of CPUCFG1 register. - This option is enabled by default to make the kernel be able to run - on all LoongArch systems. But you can disable it manually if you want - to run kernel only on systems with h/w unaligned access support in - order to optimise for performance. + This option is disabled by default to optimise for performance on + systems with h/w unaligned access support, while software unaligned + access emulation is always built in so the kernel can still run on + systems without h/w unaligned access support. + + You can enable it manually if you want to optimise for performance + on systems without h/w unaligned access support to avoid the extra + overhead of unaligned exception emulation. config CPU_HAS_AMO bool diff --git a/arch/loongarch/kernel/Makefile b/arch/loongarch/kernel/Makefile index 6260c15adaba..fb5a9d023893 100644 --- a/arch/loongarch/kernel/Makefile +++ b/arch/loongarch/kernel/Makefile @@ -10,7 +10,7 @@ always-$(KBUILD_BUILTIN) := vmlinux.lds obj-y += head.o cpu-probe.o cacheinfo.o env.o setup.o entry.o genex.o \ traps.o irq.o idle.o process.o dma.o mem.o reset.o switch.o \ elf.o syscall.o signal.o time.o topology.o inst.o ptrace.o vdso.o \ - alternative.o kdebugfs.o unwind.o + alternative.o kdebugfs.o unaligned.o unwind.o obj-$(CONFIG_ACPI) += acpi.o obj-$(CONFIG_EFI) += efi.o @@ -19,8 +19,6 @@ obj-$(CONFIG_CPU_HAS_FPU) += fpu.o kfpu.o obj-$(CONFIG_CPU_HAS_LBT) += lbt.o -obj-$(CONFIG_ARCH_STRICT_ALIGN) += unaligned.o - CFLAGS_module.o += $(call cc-disable-warning, override-init) CFLAGS_syscall.o += $(call cc-disable-warning, override-init) CFLAGS_traps.o += $(call cc-disable-warning, override-init) diff --git a/arch/loongarch/kernel/traps.c b/arch/loongarch/kernel/traps.c index 906ef7a57343..8c91318272b8 100644 --- a/arch/loongarch/kernel/traps.c +++ b/arch/loongarch/kernel/traps.c @@ -562,13 +562,6 @@ int no_unaligned_warning __read_mostly = 1; /* Only 1 warning by default */ asmlinkage void noinstr do_ale(struct pt_regs *regs) { irqentry_state_t state = irqentry_enter(regs); - -#ifndef CONFIG_ARCH_STRICT_ALIGN - if (!fixup_exception(regs)) { - die_if_kernel("Kernel ale access", regs); - force_sig_fault(SIGBUS, BUS_ADRALN, (void __user *)regs->csr_badvaddr); - } -#else bool pie = regs_irqs_disabled(regs); unsigned int *pc; @@ -603,7 +596,6 @@ asmlinkage void noinstr do_ale(struct pt_regs *regs) out: if (!pie) local_irq_disable(); -#endif irqentry_exit(regs, state); } -- 2.42.0