mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v1] LoongArch: Call fixup_exception() for unaligned accesses
@ 2026-12-02  8:11 Tiezhu Yang
  2026-12-02 13:21 ` George Guo
  2026-12-02 13:40 ` George Guo
  0 siblings, 2 replies; 3+ messages in thread
From: Tiezhu Yang @ 2026-12-02  8:11 UTC (permalink / raw)
  To: Huacai Chen; +Cc: George Guo, loongarch, linux-kernel

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-bot <sashiko@sashiko.dev>
Closes: https://lore.kernel.org/bpf/sashiko-outbox-243049@kernel.org/
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 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


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

* Re: [PATCH v1] LoongArch: Call fixup_exception() for unaligned accesses
  2026-12-02  8:11 [PATCH v1] LoongArch: Call fixup_exception() for unaligned accesses Tiezhu Yang
@ 2026-12-02 13:21 ` George Guo
  2026-12-02 13:40 ` George Guo
  1 sibling, 0 replies; 3+ messages in thread
From: George Guo @ 2026-12-02 13:21 UTC (permalink / raw)
  To: yangtiezhu; +Cc: chenhuacai, guodongtai, loongarch, linux-kernel

On 2026/12/2 16:11, Tiezhu Yang 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.

I tested this on Loongson-3C6000, with the LoongArch BPF arena atomics
series applied, using a new arena_atomics/unaligned subtest that does
32-bit fetch add and fetch or, 64-bit xchg and cmpxchg one byte off an
aligned arena address.

Without this patch, the kernel panics at the first atomic:

  Kernel ale access[#1]:
  ...
    ERA: ffff8000020136f4 bpf_prog_53d40cccca5faaf3_unaligned+0x10c/0x284
  ESTAT: 00090000 [ALE] (IS= ECode=9 EsubCode=0)
   BADV: ffff80008d600001
  ...
  Kernel panic - not syncing: Fatal exception

With this patch, the test passed with both CONFIG_ARCH_STRICT_ALIGN=y
and CONFIG_ARCH_STRICT_ALIGN=n:

  $ sudo ./test_progs -a arena_atomics/unaligned
  #3/10    arena_atomics/unaligned:OK
  #3       arena_atomics:OK
  Summary: 1/1 PASSED, 0 SKIPPED, 0/0 FAILED

Reviewed-by: George Guo <guodongtai@kylinos.cn>
Tested-by: George Guo <guodongtai@kylinos.cn>

I will send the test as a follow-up to this patch.

Thanks,
George

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

* Re: [PATCH v1] LoongArch: Call fixup_exception() for unaligned accesses
  2026-12-02  8:11 [PATCH v1] LoongArch: Call fixup_exception() for unaligned accesses Tiezhu Yang
  2026-12-02 13:21 ` George Guo
@ 2026-12-02 13:40 ` George Guo
  1 sibling, 0 replies; 3+ messages in thread
From: George Guo @ 2026-12-02 13:40 UTC (permalink / raw)
  To: yangtiezhu; +Cc: chenhuacai, guodongtai, loongarch, linux-kernel

Hi Tiezhu,

On 2026/12/2 16:11, Tiezhu Yang wrote:
> Reported-by: sashiko-bot <sashiko@sashiko.dev>

This address is not right, the review mail came from
sashiko-bot@kernel.org. Please use this in v2:

  Reported-by: Sashiko <sashiko-bot@kernel.org>

Below is the test I used, for the record. It adds an "unaligned"
subtest to arena_atomics. On LoongArch it needs the BPF arena atomics
series to run, so I will send it formally after the JIT support is
merged.

Thanks,
George

-- >8 --
From: George Guo <guodongtai@kylinos.cn>
Subject: selftests/bpf: Test unaligned arena atomics

The verifier does not check alignment for arena pointers, so a BPF
program can do an atomic operation on an unaligned arena address. Where
the atomic instructions require natural alignment, such as on LoongArch,
this raises an alignment fault instead of a page fault, and it has to be
recovered by the same exception table entry as any other faulting arena
access.

Add an "unaligned" subtest to arena_atomics. It does 32-bit fetch add,
32-bit fetch or, 64-bit xchg and 64-bit cmpxchg one byte past an aligned
address. Each atomic is followed by a decrement of a counter, so the
subtest fails if the fixup does not resume at the next instruction.

The word lives in a page of its own, and every access stays within its
first cache line. Architectures that do unaligned atomics in hardware,
such as x86_64, just perform them, without a split lock.

Before the fix:

  $ sudo ./test_progs -a arena_atomics/unaligned
  Kernel ale access[#1]:
  ...
    ERA: ffff8000020136f4 bpf_prog_53d40cccca5faaf3_unaligned+0x10c/0x284
  ESTAT: 00090000 [ALE] (IS= ECode=9 EsubCode=0)
   BADV: ffff80008d600001
  ...
  Kernel panic - not syncing: Fatal exception

After the fix:

  $ sudo ./test_progs -a arena_atomics/unaligned
  #3/10    arena_atomics/unaligned:OK
  #3       arena_atomics:OK
  Summary: 1/1 PASSED, 0 SKIPPED, 0/0 FAILED

Signed-off-by: George Guo <guodongtai@kylinos.cn>
---
 .../selftests/bpf/prog_tests/arena_atomics.c  | 23 ++++++++++
 .../selftests/bpf/progs/arena_atomics.c       | 45 +++++++++++++++++++
 2 files changed, 68 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/arena_atomics.c b/tools/testing/selftests/bpf/prog_tests/arena_atomics.c
index 222c63b15de0..1879c0bdfdc7 100644
--- a/tools/testing/selftests/bpf/prog_tests/arena_atomics.c
+++ b/tools/testing/selftests/bpf/prog_tests/arena_atomics.c
@@ -213,6 +213,27 @@ static void test_uaf(struct arena_atomics *skel)
 #endif
 }
 
+static void test_unaligned(struct arena_atomics *skel)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, topts);
+	int err, prog_fd;
+
+	/* No need to attach it, just run it directly */
+	prog_fd = bpf_program__fd(skel->progs.unaligned);
+	err = bpf_prog_test_run_opts(prog_fd, &topts);
+	if (!ASSERT_OK(err, "test_run_opts err"))
+		return;
+	if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
+		return;
+
+	/*
+	 * Every atomic has to return, whether the architecture performed it
+	 * unaligned or recovered from the fault it caused.
+	 */
+	ASSERT_EQ(skel->arena->unaligned_recovery_fails, 0,
+		  "unaligned_recovery_fails");
+}
+
 static void test_load_acquire(struct arena_atomics *skel)
 {
 	LIBBPF_OPTS(bpf_test_run_opts, topts);
@@ -311,6 +332,8 @@ void serial_test_arena_atomics(void)
 		test_fetch_r0(skel);
 	if (test__start_subtest("uaf"))
 		test_uaf(skel);
+	if (test__start_subtest("unaligned"))
+		test_unaligned(skel);
 	if (test__start_subtest("load_acquire"))
 		test_load_acquire(skel);
 	if (test__start_subtest("store_release"))
diff --git a/tools/testing/selftests/bpf/progs/arena_atomics.c b/tools/testing/selftests/bpf/progs/arena_atomics.c
index d8dc4ad9d7eb..aeae7514966d 100644
--- a/tools/testing/selftests/bpf/progs/arena_atomics.c
+++ b/tools/testing/selftests/bpf/progs/arena_atomics.c
@@ -423,6 +423,51 @@ int uaf(const void *ctx)
 	return 0;
 }
 
+__u64 __arena_global unaligned_sink = 0;
+volatile __u64 __arena_global unaligned_recovery_fails;
+
+/*
+ * check_ptr_alignment() accepts a PTR_TO_ARENA of any alignment, so an atomic
+ * one byte off a naturally aligned word reaches the JIT. Where the atomic
+ * instructions require natural alignment, such an access takes an alignment
+ * fault rather than a page fault, and it has to be recovered by the same
+ * exception table entry as any other faulting arena access. Each atomic below
+ * is followed by a decrement, so the test fails if any of them does not return.
+ *
+ * The word lives in a page of its own: where unaligned atomics do succeed they
+ * read and write one byte past it.
+ */
+SEC("syscall")
+int unaligned(const void *ctx)
+{
+#if defined(ENABLE_ATOMICS_TESTS) && defined(__BPF_FEATURE_ADDR_SPACE_CAST)
+	void __arena *page;
+	char __arena *p;
+
+	page = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
+	if (!page)
+		return 1;
+
+	*(__u64 __arena *)page = 1;
+	p = (char __arena *)page + 1;
+
+	unaligned_recovery_fails = 4;
+
+	unaligned_sink += __sync_fetch_and_add((__u32 __arena *)p, 1);
+	unaligned_recovery_fails -= 1;
+	unaligned_sink += __sync_fetch_and_or((__u32 __arena *)p, 1);
+	unaligned_recovery_fails -= 1;
+	unaligned_sink += __sync_lock_test_and_set((__u64 __arena *)p, 1);
+	unaligned_recovery_fails -= 1;
+	unaligned_sink += __sync_val_compare_and_swap((__u64 __arena *)p, 0, 1);
+	unaligned_recovery_fails -= 1;
+
+	bpf_arena_free_pages(&arena, page, 1);
+#endif
+
+	return 0;
+}
+
 #if __clang_major__ >= 18
 __u8 __arena_global load_acquire8_value = 0x12;
 __u16 __arena_global load_acquire16_value = 0x1234;

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

end of thread, other threads:[~2026-12-02 13:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-02  8:11 [PATCH v1] LoongArch: Call fixup_exception() for unaligned accesses Tiezhu Yang
2026-12-02 13:21 ` George Guo
2026-12-02 13:40 ` George Guo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®