Re: [PATCH v2] LoongArch: Call fixup_exception() for unaligned accesses

From: Huacai Chen

Date: Thu Dec 03 2026 - 07:22:27 EST


Applied, thanks.


Huacai

On Thu, Dec 3, 2026 at 9:29 AM Tiezhu Yang <yangtiezhu@xxxxxxxxxxx> wrote:
>
> On LoongArch, atomic memory instructions (AM*) require natural
> alignment and always raise EXCCODE_ALE when given an unaligned
> address, whereas unaligned regular loads and stores are either
> handled in hardware or emulated in software.
>
> Consult fixup_exception() on the failure paths of do_ale() and
> emulate_load_store_insn() so that registered exception handlers
> can catch and recover from unaligned access faults.
>
> Reported-by: Sashiko <sashiko-bot@xxxxxxxxxx>
> Closes: https://lore.kernel.org/bpf/sashiko-outbox-243049@xxxxxxxxxx/
> Signed-off-by: Tiezhu Yang <yangtiezhu@xxxxxxxxxxx>
> Reviewed-by: George Guo <guodongtai@xxxxxxxxxx>
> Tested-by: George Guo <guodongtai@xxxxxxxxxx>
> ---
> v2: Fix the email address in Reported-by tag, also add
> Reviewed-by and Tested-by tags, no code changes.
>
> arch/loongarch/kernel/traps.c | 12 ++++++++----
> arch/loongarch/kernel/unaligned.c | 4 ++++
> 2 files changed, 12 insertions(+), 4 deletions(-)
>
> diff --git a/arch/loongarch/kernel/traps.c b/arch/loongarch/kernel/traps.c
> index 20ce2c8d77a1..906ef7a57343 100644
> --- a/arch/loongarch/kernel/traps.c
> +++ b/arch/loongarch/kernel/traps.c
> @@ -564,8 +564,10 @@ asmlinkage void noinstr do_ale(struct pt_regs *regs)
> irqentry_state_t state = irqentry_enter(regs);
>
> #ifndef CONFIG_ARCH_STRICT_ALIGN
> - die_if_kernel("Kernel ale access", regs);
> - force_sig_fault(SIGBUS, BUS_ADRALN, (void __user *)regs->csr_badvaddr);
> + 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;
> @@ -594,8 +596,10 @@ asmlinkage void noinstr do_ale(struct pt_regs *regs)
> goto out;
>
> sigbus:
> - die_if_kernel("Kernel ale access", regs);
> - force_sig_fault(SIGBUS, BUS_ADRALN, (void __user *)regs->csr_badvaddr);
> + if (!fixup_exception(regs)) {
> + die_if_kernel("Kernel ale access", regs);
> + force_sig_fault(SIGBUS, BUS_ADRALN, (void __user *)regs->csr_badvaddr);
> + }
> out:
> if (!pie)
> local_irq_disable();
> diff --git a/arch/loongarch/kernel/unaligned.c b/arch/loongarch/kernel/unaligned.c
> index cc929c9fe7e9..82f53b199f45 100644
> --- a/arch/loongarch/kernel/unaligned.c
> +++ b/arch/loongarch/kernel/unaligned.c
> @@ -491,6 +491,10 @@ void emulate_load_store_insn(struct pt_regs *regs, void __user *addr, unsigned i
> return;
>
> sigbus:
> + /* Did we have an exception handler installed? */
> + if (fixup_exception(regs))
> + return;
> +
> die_if_kernel("Unhandled kernel unaligned access", regs);
> force_sig(SIGBUS);
>
> --
> 2.42.0
>
>