BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Tiezhu Yang" <yangtiezhu@loongson.cn>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH RFC] LoongArch: Enable HAVE_EFFICIENT_UNALIGNED_ACCESS by default
Date: Sat, 05 Dec 2026 10:27:58 +0000	[thread overview]
Message-ID: <sashiko-outbox-251386@kernel.org> (raw)
In-Reply-To: <20261205100121.3605-1-yangtiezhu@loongson.cn>

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

      reply	other threads:[~2026-12-05 10:27 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-251386@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=yangtiezhu@loongson.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox