* [PATCH bpf-next v2 1/4] bpf: Fix alignment of memory allocator objects on 32-bit archs
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 ` Tobias Philipp
2026-11-24 22:50 ` [PATCH bpf-next v2 2/4] bpf: Align lpm_trie map values to 8 bytes Tobias Philipp
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Tobias Philipp @ 2026-11-24 22:50 UTC (permalink / raw)
To: bpf
Cc: Felix Fietkau, Alexei Starovoitov, Daniel Borkmann,
Andrii Nakryiko, Martin KaFai Lau, Eduard Zingerman,
Kumar Kartikeya Dwivedi, Song Liu, Yonghong Song, Jiri Olsa,
John Fastabend, Stanislav Fomichev, Emil Tsalapatis,
Ihor Solodrai, Jesper Dangaard Brouer, David S. Miller,
Jakub Kicinski, Shuah Khan, Thibaut VARÈNE, Stijn Tintel,
Daniel Mack, netdev, linux-kselftest, linux-kernel,
Tobias Philipp
From: Felix Fietkau <nbd@nbd.name>
The BPF memory allocator prefixes each object with a header that holds
struct llist_node while the object sits on a free list, and a pointer
to the owning bpf_mem_cache while it is allocated. The header size was
sizeof(struct llist_node), so on 32-bit architectures the memory handed
out was offset by 4 bytes from the 8-byte aligned kmalloc allocation.
The verifier assumes that allocated objects are 8-byte aligned and
allows BPF_DW atomic instructions on their u64 fields, e.g. on values
of non-preallocated hash maps, objects from bpf_obj_new() and local
storage. On arm32 the JIT does not implement atomics, and the
interpreter executes them via atomic64_*(), which are implemented with
LDREXD/STREXD on ARMv6K and later. Those instructions require 8-byte
aligned addresses; a misaligned address raises an alignment fault and
crashes the kernel.
Pad the header to 8 bytes to preserve the alignment provided by
kmalloc. Store the percpu pointer of percpu objects at the new header
offset instead of assuming the header is pointer-sized, and update the
copy of the header size used by the hash map memory usage accounting.
Fixes: 7c8199e24fa0 ("bpf: Introduce any context BPF specific memory allocator.")
Reported-by: Thibaut VARÈNE <hacks@slashdirt.org>
Reported-by: Stijn Tintel <stijn@linux-ipv6.be>
Signed-off-by: Felix Fietkau <nbd@nbd.name>
Link: https://lore.kernel.org/all/20260717183632.99195-1-nbd@nbd.name/
[tphilipp: rebased onto bpf-next, adjusted context in hashtab.c.
Local storage no longer uses bpf_mem_alloc in bpf-next. On 32-bit
this patch moves an IPv4 lpm_trie value from 0 to 4 mod 8; the next
patch fixes that, so both have to be applied together.]
Signed-off-by: Tobias Philipp <tphilipp@gmail.com>
---
kernel/bpf/hashtab.c | 2 +-
kernel/bpf/memalloc.c | 8 ++++----
2 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c
index a2d0bfd4c..b84911054 100644
--- a/kernel/bpf/hashtab.c
+++ b/kernel/bpf/hashtab.c
@@ -2594,7 +2594,7 @@ static u64 htab_map_mem_usage(const struct bpf_map *map)
else if (!lru)
usage += sizeof(struct htab_elem *) * num_possible_cpus();
} else {
-#define LLIST_NODE_SZ sizeof(struct llist_node)
+#define LLIST_NODE_SZ ALIGN(sizeof(struct llist_node), 8)
num_entries = bpf_map_sum_elem_count(map);
usage += (htab->elem_size + LLIST_NODE_SZ) * num_entries;
diff --git a/kernel/bpf/memalloc.c b/kernel/bpf/memalloc.c
index 15684d0fc..29d62d864 100644
--- a/kernel/bpf/memalloc.c
+++ b/kernel/bpf/memalloc.c
@@ -33,7 +33,7 @@
* Every allocated objected is padded with extra 8 bytes that contains
* struct llist_node.
*/
-#define LLIST_NODE_SZ sizeof(struct llist_node)
+#define LLIST_NODE_SZ ALIGN(sizeof(struct llist_node), 8)
#define BPF_MEM_ALLOC_SIZE_MAX 4096
@@ -148,7 +148,7 @@ static struct llist_node notrace *__llist_del_first(struct llist_head *head)
static void *__alloc(struct bpf_mem_cache *c, int node, gfp_t flags)
{
if (c->percpu_size) {
- void __percpu **obj = kmalloc_node(c->percpu_size, flags, node);
+ void *obj = kmalloc_node(c->percpu_size, flags, node);
void __percpu *pptr = __alloc_percpu_gfp(c->unit_size, 8, flags);
if (!obj || !pptr) {
@@ -156,7 +156,7 @@ static void *__alloc(struct bpf_mem_cache *c, int node, gfp_t flags)
kfree(obj);
return NULL;
}
- obj[1] = pptr;
+ *(void __percpu **)(obj + LLIST_NODE_SZ) = pptr;
return obj;
}
@@ -262,7 +262,7 @@ static void alloc_bulk(struct bpf_mem_cache *c, int cnt, int node, bool atomic)
static void free_one(void *obj, bool percpu)
{
if (percpu)
- free_percpu(((void __percpu **)obj)[1]);
+ free_percpu(*(void __percpu **)(obj + LLIST_NODE_SZ));
kfree(obj);
}
--
2.54.0 (Apple Git-157)
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH bpf-next v2 2/4] bpf: Align lpm_trie map values to 8 bytes
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
2026-11-24 22:50 ` [PATCH bpf-next v2 3/4] bpf: Align devmap " Tobias Philipp
2026-11-24 22:50 ` [PATCH bpf-next v2 4/4] selftests/bpf: Check that map values are 8-byte aligned Tobias Philipp
3 siblings, 0 replies; 5+ messages in thread
From: Tobias Philipp @ 2026-11-24 22:50 UTC (permalink / raw)
To: bpf
Cc: Tobias Philipp, Alexei Starovoitov, Daniel Borkmann,
Andrii Nakryiko, Martin KaFai Lau, Eduard Zingerman,
Kumar Kartikeya Dwivedi, Song Liu, Yonghong Song, Jiri Olsa,
John Fastabend, Stanislav Fomichev, Emil Tsalapatis,
Ihor Solodrai, Jesper Dangaard Brouer, David S. Miller,
Jakub Kicinski, Shuah Khan, Felix Fietkau, Thibaut VARÈNE,
Stijn Tintel, Daniel Mack, netdev, linux-kselftest, linux-kernel
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)
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH bpf-next v2 3/4] bpf: Align devmap values to 8 bytes
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 ` [PATCH bpf-next v2 2/4] bpf: Align lpm_trie map values to 8 bytes Tobias Philipp
@ 2026-11-24 22:50 ` Tobias Philipp
2026-11-24 22:50 ` [PATCH bpf-next v2 4/4] selftests/bpf: Check that map values are 8-byte aligned Tobias Philipp
3 siblings, 0 replies; 5+ messages in thread
From: Tobias Philipp @ 2026-11-24 22:50 UTC (permalink / raw)
To: bpf
Cc: Tobias Philipp, Alexei Starovoitov, Daniel Borkmann,
Andrii Nakryiko, Martin KaFai Lau, Eduard Zingerman,
Kumar Kartikeya Dwivedi, Song Liu, Yonghong Song, Jiri Olsa,
John Fastabend, Stanislav Fomichev, Emil Tsalapatis,
Ihor Solodrai, Jesper Dangaard Brouer, David S. Miller,
Jakub Kicinski, Shuah Khan, Felix Fietkau, Thibaut VARÈNE,
Stijn Tintel, Daniel Mack, netdev, linux-kselftest, linux-kernel
dev_map_lookup_elem() and dev_map_hash_lookup_elem() return &obj->val
as the map value. In struct bpf_dtab_netdev, val follows
"unsigned int idx" and is at offset 52 on 64-bit and 28 on 32-bit
architectures, so it is only 4-byte aligned.
devmap is read-only for BPF programs, but the verifier assumes that map
values are 8-byte aligned and accepts a 64-bit load-acquire at offset 0
of a struct bpf_devmap_val value on 64-bit architectures. The arm64 JIT
emits LDAR for it, which raises an alignment fault on a misaligned
address. FEAT_LSE2 does not change that, because Linux does not set
SCTLR_EL1.nAA. The fault is not fixed up.
Mark val as __aligned(8). On 64-bit it moves to offset 56 and the size
of struct bpf_dtab_netdev stays 64 bytes. On 32-bit the verifier
rejects 64-bit load-acquire, so the misaligned value is harmless there.
Aligning it anyway keeps all map values 8-byte aligned and grows the
struct from 36 to 40 bytes. Entries are allocated with kmalloc, which
provides at least 8-byte alignment.
This patch was written with the help of an AI coding assistant
(Claude Opus 5.5).
Fixes: 880442305a39 ("bpf: Introduce load-acquire and store-release instructions")
Signed-off-by: Tobias Philipp <tphilipp@gmail.com>
---
kernel/bpf/devmap.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/kernel/bpf/devmap.c b/kernel/bpf/devmap.c
index 1b8f0d7f8..0ef24e3f9 100644
--- a/kernel/bpf/devmap.c
+++ b/kernel/bpf/devmap.c
@@ -70,7 +70,10 @@ struct bpf_dtab_netdev {
struct bpf_prog *xdp_prog;
struct rcu_head rcu;
unsigned int idx;
- struct bpf_devmap_val val;
+ /* Returned to BPF programs as the map value, which the verifier
+ * assumes to be 8-byte aligned.
+ */
+ struct bpf_devmap_val val __aligned(8);
};
struct bpf_dtab {
--
2.54.0 (Apple Git-157)
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH bpf-next v2 4/4] selftests/bpf: Check that map values are 8-byte aligned
2026-11-24 22:50 [PATCH bpf-next v2 0/4] bpf: Fix 8-byte alignment of map values Tobias Philipp
` (2 preceding siblings ...)
2026-11-24 22:50 ` [PATCH bpf-next v2 3/4] bpf: Align devmap " Tobias Philipp
@ 2026-11-24 22:50 ` Tobias Philipp
3 siblings, 0 replies; 5+ messages in thread
From: Tobias Philipp @ 2026-11-24 22:50 UTC (permalink / raw)
To: bpf
Cc: Tobias Philipp, Alexei Starovoitov, Daniel Borkmann,
Andrii Nakryiko, Martin KaFai Lau, Eduard Zingerman,
Kumar Kartikeya Dwivedi, Song Liu, Yonghong Song, Jiri Olsa,
John Fastabend, Stanislav Fomichev, Emil Tsalapatis,
Ihor Solodrai, Jesper Dangaard Brouer, David S. Miller,
Jakub Kicinski, Shuah Khan, Felix Fietkau, Thibaut VARÈNE,
Stijn Tintel, Daniel Mack, netdev, linux-kselftest, linux-kernel
The verifier assumes that map values are 8-byte aligned. Add a test
that looks up an IPv4 lpm_trie value, a BPF_F_NO_PREALLOC hash value
and a devmap value from an XDP program, stores the returned addresses
in global variables and checks them in user space.
The program does not execute any instruction that requires alignment,
so on an affected kernel the test fails instead of crashing it.
Without the previous patches the lpm_trie and devmap checks fail on
64-bit architectures, and the hash and devmap checks fail on 32-bit
architectures.
This patch was written with the help of an AI coding assistant
(Claude Opus 5.5).
Signed-off-by: Tobias Philipp <tphilipp@gmail.com>
---
.../bpf/prog_tests/map_value_align.c | 51 +++++++++++++++++++
.../selftests/bpf/progs/map_value_align.c | 48 +++++++++++++++++
2 files changed, 99 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/map_value_align.c
create mode 100644 tools/testing/selftests/bpf/progs/map_value_align.c
diff --git a/tools/testing/selftests/bpf/prog_tests/map_value_align.c b/tools/testing/selftests/bpf/prog_tests/map_value_align.c
new file mode 100644
index 000000000..91898a052
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/map_value_align.c
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include <network_helpers.h>
+#include "map_value_align.skel.h"
+
+#define IFINDEX_LO 1
+
+/* The verifier assumes that map values are 8-byte aligned */
+void test_map_value_align(void)
+{
+ LIBBPF_OPTS(bpf_test_run_opts, topts,
+ .data_in = &pkt_v4,
+ .data_size_in = sizeof(pkt_v4),
+ );
+ struct bpf_devmap_val dval = { .ifindex = IFINDEX_LO };
+ struct { __u32 prefixlen, data; } lkey = { .prefixlen = 32 };
+ struct map_value_align *skel;
+ __u64 val = 0;
+ __u32 key = 0;
+ int err;
+
+ skel = map_value_align__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ return;
+
+ err = bpf_map__update_elem(skel->maps.lpm, &lkey, sizeof(lkey),
+ &val, sizeof(val), BPF_ANY);
+ if (!ASSERT_OK(err, "lpm_update"))
+ goto out;
+ err = bpf_map__update_elem(skel->maps.hash, &key, sizeof(key),
+ &val, sizeof(val), BPF_ANY);
+ if (!ASSERT_OK(err, "hash_update"))
+ goto out;
+ err = bpf_map__update_elem(skel->maps.devmap, &key, sizeof(key),
+ &dval, sizeof(dval), BPF_ANY);
+ if (!ASSERT_OK(err, "devmap_update"))
+ goto out;
+
+ err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.lookup), &topts);
+ if (!ASSERT_OK(err, "test_run"))
+ goto out;
+
+ ASSERT_NEQ(skel->bss->lpm_addr, 0, "lpm_lookup");
+ ASSERT_EQ(skel->bss->lpm_addr & 7, 0, "lpm_value_align");
+ ASSERT_NEQ(skel->bss->hash_addr, 0, "hash_lookup");
+ ASSERT_EQ(skel->bss->hash_addr & 7, 0, "hash_value_align");
+ ASSERT_NEQ(skel->bss->devmap_addr, 0, "devmap_lookup");
+ ASSERT_EQ(skel->bss->devmap_addr & 7, 0, "devmap_value_align");
+out:
+ map_value_align__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/map_value_align.c b/tools/testing/selftests/bpf/progs/map_value_align.c
new file mode 100644
index 000000000..bd0663a13
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/map_value_align.c
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+struct lpm_key {
+ __u32 prefixlen;
+ __u32 data;
+};
+
+struct {
+ __uint(type, BPF_MAP_TYPE_LPM_TRIE);
+ __uint(map_flags, BPF_F_NO_PREALLOC);
+ __uint(max_entries, 1);
+ __type(key, struct lpm_key);
+ __type(value, __u64);
+} lpm SEC(".maps");
+
+struct {
+ __uint(type, BPF_MAP_TYPE_HASH);
+ __uint(map_flags, BPF_F_NO_PREALLOC);
+ __uint(max_entries, 1);
+ __type(key, __u32);
+ __type(value, __u64);
+} hash SEC(".maps");
+
+struct {
+ __uint(type, BPF_MAP_TYPE_DEVMAP);
+ __uint(max_entries, 1);
+ __type(key, __u32);
+ __type(value, struct bpf_devmap_val);
+} devmap SEC(".maps");
+
+/* Addresses of the map values, checked by user space */
+__u64 lpm_addr, hash_addr, devmap_addr;
+
+SEC("xdp")
+int lookup(struct xdp_md *ctx)
+{
+ struct lpm_key lkey = { .prefixlen = 32 };
+ __u32 key = 0;
+
+ lpm_addr = (unsigned long)bpf_map_lookup_elem(&lpm, &lkey);
+ hash_addr = (unsigned long)bpf_map_lookup_elem(&hash, &key);
+ devmap_addr = (unsigned long)bpf_map_lookup_elem(&devmap, &key);
+ return XDP_PASS;
+}
+
+char _license[] SEC("license") = "GPL";
--
2.54.0 (Apple Git-157)
^ permalink raw reply related [flat|nested] 5+ messages in thread