From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.164 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796218866; b=Q8sE4hiKHmBbGCzYFCNDha7m18lV5+ADdZdCPneP+z9IRC5X5dJKIsdYcyBdPGtqqIJ3 pgo/AkSQAlprozY5NhLsXPb963n9HF/qCLNqizhUISB8igwlSZV2+RtWVvWbL3ZbO8kDZ dtpHjD45mIBigeYP0NdLTHHSD1BRYPlIJQ+kJY9xDQ++4InKnQISkrZhVIBLc8vvF66qJ 8z3lASocdE3nMqjR7Gle/JIUz9QOF3RkmQ1VYw/QYl8EUkFlj8mpr/KKmfCd21JbDTAJi vhJlDVMihmshboWihnPS9J5KQrG3RTN35CmBSmFR0poGqheClLCtZCXxT4e9iKOmKag== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796218866; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=2/zL14LKObvRQRyTQzLSx9cG81f/4/6k8DRm/9L2VuU=; b=AWJBGhxGLkLV3I6pzo4O76z2bZ7rZfBTgS3Xu72slhVHSa55fFUBLCfsnqB+SWpMIU9Z nj1Cj03uc33sx7u3kPGoOJXCQ9+1U9/k274LXwugC436yowANGtKvqzZCigDVQdvncrwN pqbOFpsDK+BeRIEpRH1Z8zYLXYyJPRn+1VcsldnRRQp/REyd+sFewodiEzxnuUsLTjS2I UddWa/U9Rt8I4Z/1qsk1NEajVnDD6vVQ21gjSyS4vRmCEwVi3gA27t6NpeaR/N4unm27H 1IEygAZ0q+8LF87sIS0IjfFD+bqCF4Y/PNp8h8Hjp5OelhRIG9LCsE9sl+5KpREwlog== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=MWnBNFos; arc=none smtp.remote-ip=91.218.175.164 Received: from mta0.migadu.com (out-164.mta0.migadu.com [91.218.175.164]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CE3AA606A31A for ; Wed, 2 Dec 2026 13:41:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org CE3AA606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=MWnBNFos X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=ZU66e+dADDlMtEv3NQ86RRO0OmpleheOQPbymvmH9+o=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796218863; v=1; x=1796823663; b=MWnBNFosJYVmNMhfASE6Nd7QgZwjhxevT7sP5nGJDkkp87swiG7yZmmM8CZ9zKKID+Nh6fHp lEnpZPc6yHRHBjo9gqFwxkwFt0bJlx0UkWoWvOtSw+NiINuIjFDC+97xj17I6dLwSrHWdlsm/iv 4eMhwch3nC4KKUCMjdnLA/cw= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id c0c785b297339a1f; Wed, 02 Dec 2026 13:41:03 +0000 X-Mizu-Trace-ID: c0c785b297339a1f X-Migadu-Flow: FLOW_OUT From: George Guo 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 Message-ID: <20261202134056.40836-1-dongtai.guo@linux.dev> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261202081108.20060-1-yangtiezhu@loongson.cn> References: <20261202081108.20060-1-yangtiezhu@loongson.cn> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Hi Tiezhu, On 2026/12/2 16:11, Tiezhu Yang wrote: > Reported-by: sashiko-bot This address is not right, the review mail came from sashiko-bot@kernel.org. Please use this in v2: Reported-by: Sashiko 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 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 --- .../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;