mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: George Guo <dongtai.guo@linux.dev>
To: yangtiezhu@loongson.cn
Cc: chenhuacai@kernel.org, guodongtai@kylinos.cn,
	loongarch@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v1] LoongArch: Call fixup_exception() for unaligned accesses
Date: Wed,  2 Dec 2026 05:40:56 -0800	[thread overview]
Message-ID: <20261202134056.40836-1-dongtai.guo@linux.dev> (raw)
In-Reply-To: <20261202081108.20060-1-yangtiezhu@loongson.cn>

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;

      parent reply	other threads:[~2026-12-02 13:41 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  8:11 Tiezhu Yang
2026-12-02 13:21 ` George Guo
2026-12-02 13:40 ` George Guo [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=20261202134056.40836-1-dongtai.guo@linux.dev \
    --to=dongtai.guo@linux.dev \
    --cc=chenhuacai@kernel.org \
    --cc=guodongtai@kylinos.cn \
    --cc=linux-kernel@vger.kernel.org \
    --cc=loongarch@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

all inboxes | Powered by JetHome®