All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH RFC] LoongArch: Enable HAVE_EFFICIENT_UNALIGNED_ACCESS by default
@ 2026-12-05 10:01 Tiezhu Yang
  2026-12-05 10:27 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Tiezhu Yang @ 2026-12-05 10:01 UTC (permalink / raw)
  To: Huacai Chen; +Cc: loongarch, bpf, linux-kernel

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 <yangtiezhu@loongson.cn>
---
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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH RFC] LoongArch: Enable HAVE_EFFICIENT_UNALIGNED_ACCESS by default
  2026-12-05 10:01 [PATCH RFC] LoongArch: Enable HAVE_EFFICIENT_UNALIGNED_ACCESS by default Tiezhu Yang
@ 2026-12-05 10:27 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-05 10:27 UTC (permalink / raw)
  To: Tiezhu Yang; +Cc: bpf

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] loongarch: disabling ARCH_STRICT_ALIGN breaks early boot on
  non-UAL CPUs
- [High] loongarch: 32-bit ARCH_STRICT_ALIGN=n exposes corrupt
  unaligned_read()
- [Medium] loongarch: unscoped sysctl checks and recursion hazard in
  do_ale()
- [Medium] loongarch: do_ale() drops BUS_ADRALN and si_addr via
  force_sig(SIGBUS)
- [Low] loongarch: ARCH_STRICT_ALIGN gated behind EXPERT despite help
  text advice
--

commit 761f7e330651266d46a18c88be50f6915ae68994
Author: Tiezhu Yang <yangtiezhu@loongson.cn>

LoongArch: Enable HAVE_EFFICIENT_UNALIGNED_ACCESS by default

This commit disables CONFIG_ARCH_STRICT_ALIGN by default to enable
HAVE_EFFICIENT_UNALIGNED_ACCESS on LoongArch, while building unaligned.o
unconditionally so software unaligned access emulation remains available
as a fallback.

> diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig
> index 2331ecab5f401..09907212ba36f 100644
> --- a/arch/loongarch/Kconfig
> +++ b/arch/loongarch/Kconfig
> @@ -595,7 +595,7 @@ config ARCH_IOREMAP
>  
>  config ARCH_STRICT_ALIGN
>  	bool "Enable -mstrict-align to prevent unaligned accesses" if EXPERT
> -	default y
> +	default n

[Severity: High]
Can disabling CONFIG_ARCH_STRICT_ALIGN by default break early boot on
processors without hardware UAL support?

handle_ale is not installed until trap_init() runs late in start_kernel().
Before that, per_cpu_trap_init() wires EXCCODE_ALE to handle_reserved,
which panics via do_reserved() -> die_if_kernel().  With -mno-strict-align
and CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS enabled, early boot code such
as cpu_probe_loongson() (which performs a 64-bit store to cpu_full_name[9])
and sized_strscpy() in init_environ() executes unaligned accesses before
trap_init() and crashes non-UAL systems like Loongson-2K0300/2K0500/2K1000.

The EFI stub and ZBOOT decompressor also inherit -mno-strict-align from
KBUILD_CFLAGS in drivers/firmware/efi/libstub/Makefile and execute before
kernel_entry when no kernel ALE handler exists.

[Severity: High]
Will defaulting CONFIG_ARCH_STRICT_ALIGN to n on CONFIG_32BIT expose
silent data corruption in unaligned_read() on 32-bit non-UAL hardware?

arch/loongarch/lib/unaligned.S:unaligned_read() {
    ...
	LONG_ADDI	t0, a2, -1
	PTR_SLLI	t1, t0, LONGLOG
    ...
	LONG_ADDI	t1, t1, -8
    ...
}

On CONFIG_32BIT, LONGLOG is 2 rather than 3, so t1 starts at (n - 1) * 4
instead of (n - 1) * 8 while the loop still decrements it by 8 each
iteration.  Every emulated unaligned load on 32-bit non-UAL CPUs shifts
bytes to the wrong bit positions and returns corrupted data.  Keeping
ARCH_STRICT_ALIGN enabled by default on CONFIG_32BIT (or fixing
unaligned_read() to shift by 3) would avoid this regression.

[ ... ]

> +	  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.

[Severity: Low]
Since ARCH_STRICT_ALIGN is still gated by "if EXPERT", can non-EXPERT
users follow this advice?  Without CONFIG_EXPERT enabled, the option is
hidden in Kconfig and users targeting non-UAL systems cannot enable it.

[ ... ]

> diff --git a/arch/loongarch/kernel/traps.c b/arch/loongarch/kernel/traps.c
> index 790db6c1926e6..30bef19d28fa7 100644
> --- a/arch/loongarch/kernel/traps.c
> +++ b/arch/loongarch/kernel/traps.c
> @@ -564,13 +564,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;

[Severity: Medium]
Does removing the !CONFIG_ARCH_STRICT_ALIGN branch regress siginfo_t
reporting for unhandled userspace alignment faults?

Previously, CONFIG_ARCH_STRICT_ALIGN=n kernels delivered SIGBUS with
BUS_ADRALN and regs->csr_badvaddr via force_sig_fault().  Now userspace
traps on unemulated instructions (such as unaligned AM*/ll/sc atomics,
which still raise EXCCODE_ALE on UAL-capable CPUs) reach the sigbus label
in emulate_load_store_insn(), which calls force_sig(SIGBUS) and reports
si_code = SI_KERNEL and si_addr = NULL.

emulate_load_store_insn() also ignores the return value of __get_inst(),
so an instruction-fetch fault on an execute-only page likewise falls
through to force_sig(SIGBUS).

[Severity: Medium]
Are the unaligned_enabled and no_unaligned_warning checks in do_ale() safe
for kernel-mode traps now that non-UAL CPUs rely on emulation for normal
kernel code?

Neither check is restricted to user_mode(regs).  Setting
kernel.unaligned-trap to 0 sends the next routine kernel unaligned access
to sigbus and panics in die_if_kernel().  Setting
kernel.ignore-unaligned-usertrap to 0 calls show_registers() without a
recursion guard, which can recurse indefinitely through printk() and
string helpers into do_ale() until the kernel stack overflows.

In addition, arch/loongarch/kernel/Makefile builds unaligned.o with
-mno-strict-align and leaves ftrace and KASAN enabled on it, which can
trigger recursive EXCCODE_ALE faults inside emulate_load_store_insn() on
non-UAL CPUs.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205100121.3605-1-yangtiezhu@loongson.cn?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-05 10:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 10:01 [PATCH RFC] LoongArch: Enable HAVE_EFFICIENT_UNALIGNED_ACCESS by default Tiezhu Yang
2026-12-05 10:27 ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.