From: Tobias Philipp <tphilipp@gmail.com>
To: bpf@vger.kernel.org
Cc: "Tobias Philipp" <tphilipp@gmail.com>,
"Alexei Starovoitov" <ast@kernel.org>,
"Daniel Borkmann" <daniel@iogearbox.net>,
"Andrii Nakryiko" <andrii@kernel.org>,
"Martin KaFai Lau" <martin.lau@linux.dev>,
"Eduard Zingerman" <eddyz87@gmail.com>,
"Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
"Song Liu" <song@kernel.org>,
"Yonghong Song" <yonghong.song@linux.dev>,
"Jiri Olsa" <jolsa@kernel.org>,
"John Fastabend" <john.fastabend@gmail.com>,
"Stanislav Fomichev" <sdf@fomichev.me>,
"Emil Tsalapatis" <emil@etsalapatis.com>,
"Ihor Solodrai" <ihor.solodrai@linux.dev>,
"Jesper Dangaard Brouer" <hawk@kernel.org>,
"David S. Miller" <davem@davemloft.net>,
"Jakub Kicinski" <kuba@kernel.org>,
"Shuah Khan" <shuah@kernel.org>, "Felix Fietkau" <nbd@nbd.name>,
"Thibaut VARÈNE" <hacks@slashdirt.org>,
"Stijn Tintel" <stijn@linux-ipv6.be>,
"Daniel Mack" <daniel@zonque.org>,
netdev@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH bpf-next v2 2/4] bpf: Align lpm_trie map values to 8 bytes
Date: Tue, 24 Nov 2026 23:50:53 +0100 [thread overview]
Message-ID: <20261124225055.87344-3-tphilipp@gmail.com> (raw)
In-Reply-To: <20261124225055.87344-1-tphilipp@gmail.com>
The verifier checks the alignment of PTR_TO_MAP_VALUE accesses only
against the offset within the value and assumes that the value itself
starts on an 8-byte boundary. It accepts BPF_DW atomics and 64-bit
load-acquire/store-release at 8-byte aligned offsets of a value.
lpm_trie stores the value directly behind the key bytes, at
node->data + trie->data_size, where data_size is key_size - 4 and can
be anything from 1 to 256. Nodes come from bpf_mem_alloc, which returns
8-byte aligned objects (on 32-bit architectures since the previous
patch), and data[] is at offset 24 (64-bit) or 16 (32-bit) in the node.
So the value is 8-byte aligned only if data_size is a multiple of 8.
For an IPv4 trie (key_size 8, data_size 4) it is 4 mod 8.
On arm64 the JIT emits LSE atomics or LDXR/STXR loops, LDAR and STLR
for these instructions. All of them raise an alignment fault on a
misaligned address. FEAT_LSE2 relaxes that for LSE atomics that stay
within a 16-byte block, and for LDAR/STLR only if SCTLR_EL1.nAA is
set, which Linux does not do. The fault is not fixed up. This oops is
from a 64-bit atomic add at offset 8 of an IPv4 lpm_trie value, which
crosses a 16-byte boundary (Linux 7.3.5 under QEMU, HVF on an
FEAT_LSE2 host):
Mem abort info:
ESR = 0x0000000096000021
FSC = 0x21: alignment fault
Internal error: Oops: 0x96000021 [#1] SMP
pc : bpf_prog_cc9155b98965fc5e+0x64/0x90
Call trace:
bpf_prog_cc9155b98965fc5e+0x64/0x90 (P)
bpf_dispatcher_xdp_func+0x14/0x28
bpf_test_run+0x11c/0x360
bpf_prog_test_run_xdp+0x358/0x548
On x86-64 a locked 8-byte access at 4 mod 8 is a split lock when it
crosses a cache line, which is fatal in kernel mode on CPUs with split
lock detection ("Split lock detected").
Round data_size up to a multiple of 8 when computing the value offset
and the node size, like hashtab does for keys. IPv4 nodes grow by 4
bytes, IPv6 nodes (data_size 16) are unchanged. Add a BUILD_BUG_ON()
for the offset of data[], which the alignment of the value relies on.
This patch and the previous one have to be applied together. Without
the previous patch, nodes are at 4 mod 8 on 32-bit architectures and
this patch misaligns the IPv4 value there, which is aligned today by
accident. Without this patch, the previous one misaligns it.
This was found with AI assistance while trying to verify the
correctness of map value accesses with formal methods. The patch was
written with the help of an AI coding assistant (Claude Opus 5.5).
Fixes: b95a5c4db09b ("bpf: add a longest prefix match trie map implementation")
Signed-off-by: Tobias Philipp <tphilipp@gmail.com>
---
kernel/bpf/lpm_trie.c | 19 +++++++++++++------
1 file changed, 13 insertions(+), 6 deletions(-)
diff --git a/kernel/bpf/lpm_trie.c b/kernel/bpf/lpm_trie.c
index 4d6f25db9..44ad5c163 100644
--- a/kernel/bpf/lpm_trie.c
+++ b/kernel/bpf/lpm_trie.c
@@ -286,7 +286,7 @@ static void *trie_lookup_elem(struct bpf_map *map, void *_key)
if (!found)
return NULL;
- return found->data + trie->data_size;
+ return found->data + round_up(trie->data_size, 8);
}
static struct lpm_trie_node *lpm_trie_node_alloc(struct lpm_trie *trie,
@@ -302,7 +302,7 @@ static struct lpm_trie_node *lpm_trie_node_alloc(struct lpm_trie *trie,
node->flags = 0;
if (value)
- memcpy(node->data + trie->data_size, value,
+ memcpy(node->data + round_up(trie->data_size, 8), value,
trie->map.value_size);
return node;
@@ -597,8 +597,14 @@ static struct bpf_map *trie_alloc(union bpf_attr *attr)
raw_res_spin_lock_init(&trie->lock);
- /* Allocate intermediate and leaf nodes from the same allocator */
- leaf_size = sizeof(struct lpm_trie_node) + trie->data_size +
+ /* Allocate intermediate and leaf nodes from the same allocator.
+ * The key bytes are padded to a multiple of 8 bytes, because the
+ * verifier assumes that map values are 8-byte aligned. Objects from
+ * bpf_mem_alloc are 8-byte aligned, so data[] must be as well.
+ */
+ BUILD_BUG_ON(offsetof(struct lpm_trie_node, data) % 8);
+ leaf_size = sizeof(struct lpm_trie_node) +
+ round_up(trie->data_size, 8) +
trie->map.value_size;
err = bpf_mem_alloc_init(&trie->ma, leaf_size, false);
if (err)
@@ -766,8 +772,9 @@ static u64 trie_mem_usage(const struct bpf_map *map)
struct lpm_trie *trie = container_of(map, struct lpm_trie, map);
u64 elem_size;
- elem_size = sizeof(struct lpm_trie_node) + trie->data_size +
- trie->map.value_size;
+ elem_size = sizeof(struct lpm_trie_node) +
+ round_up(trie->data_size, 8) +
+ trie->map.value_size;
return elem_size * READ_ONCE(trie->n_entries);
}
--
2.54.0 (Apple Git-157)
next prev parent reply other threads:[~2026-11-24 22:51 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 22:50 [PATCH bpf-next v2 0/4] bpf: Fix 8-byte alignment of map values Tobias Philipp
2026-11-24 22:50 ` [PATCH bpf-next v2 1/4] bpf: Fix alignment of memory allocator objects on 32-bit archs Tobias Philipp
2026-11-24 22:50 ` Tobias Philipp [this message]
2026-11-24 22:56 ` [PATCH bpf-next v2 2/4] bpf: Align lpm_trie map values to 8 bytes sashiko-bot
2026-11-24 22:50 ` [PATCH bpf-next v2 3/4] bpf: Align devmap " Tobias Philipp
2026-11-24 22:55 ` sashiko-bot
2026-11-24 22:50 ` [PATCH bpf-next v2 4/4] selftests/bpf: Check that map values are 8-byte aligned Tobias Philipp
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=20261124225055.87344-3-tphilipp@gmail.com \
--to=tphilipp@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=daniel@zonque.org \
--cc=davem@davemloft.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=hacks@slashdirt.org \
--cc=hawk@kernel.org \
--cc=ihor.solodrai@linux.dev \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=nbd@nbd.name \
--cc=netdev@vger.kernel.org \
--cc=sdf@fomichev.me \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=stijn@linux-ipv6.be \
--cc=yonghong.song@linux.dev \
/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