From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Tobias Philipp" <tphilipp@gmail.com>, <bpf@vger.kernel.org>
Cc: "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: Re: [PATCH bpf-next v2 2/4] bpf: Align lpm_trie map values to 8 bytes
Date: Wed, 25 Nov 2026 04:50:13 +0000 [thread overview]
Message-ID: <DN40RTGK98VJ.2XIDDVK3YL2ZC@gmail.com> (raw)
In-Reply-To: <20261124225055.87344-3-tphilipp@gmail.com>
On Tue, Nov 24, 2026 at 02:50 PM Tobias Philipp <tphilipp@gmail.com> wrote:
> 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
Not quite. With nAA == 0 LDAR/STLR have the same 16-byte rule.
nAA == 1 removes that rule for them.
> 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.
4 bytes can move all nodes of a map to the next kmalloc bucket.
There is no need to grow. prefixlen is at most 2048 and flags has one bit.
Make both u16. Bytes that bpf_mem_alloc asks from kmalloc on 64-bit:
data_size value_size today this patch u16
--------- ---------- ----- ---------------- ---
4 28 64 68, kmalloc-96 60
4 60 96 100, kmalloc-128 92
16 16 64 64 64
20 12 64 68, kmalloc-96 60
BUILD_BUG_ON() and four copies of round_up() go away.
So does the new comment. It's not in kernel multi-line style.
32-bit still needs patch 1.
This is in place of patch 2.
diff --git a/kernel/bpf/lpm_trie.c b/kernel/bpf/lpm_trie.c
index 4d6f25d..72084ac 100644
--- a/kernel/bpf/lpm_trie.c
+++ b/kernel/bpf/lpm_trie.c
@@ -25,8 +25,8 @@ struct lpm_trie_node;
struct lpm_trie_node {
struct lpm_trie_node __rcu *child[2];
- u32 prefixlen;
- u32 flags;
+ u16 prefixlen;
+ u16 flags;
u8 data[];
};
@@ -40,6 +40,12 @@ struct lpm_trie {
rqspinlock_t lock;
};
+/* The verifier assumes that map values are 8-byte aligned */
+static size_t trie_value_off(const struct lpm_trie *trie)
+{
+ return round_up(offsetof(struct lpm_trie_node, data) + trie->data_size, 8);
+}
+
/* This trie implements a longest prefix match algorithm that can be used to
* match IP addresses to a stored set of ranges.
*
@@ -286,7 +292,7 @@ static void *trie_lookup_elem(struct bpf_map *map, void *_key)
if (!found)
return NULL;
- return found->data + trie->data_size;
+ return (void *)found + trie_value_off(trie);
}
static struct lpm_trie_node *lpm_trie_node_alloc(struct lpm_trie *trie,
@@ -302,7 +308,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((void *)node + trie_value_off(trie), value,
trie->map.value_size);
return node;
@@ -557,6 +563,8 @@ out:
#define LPM_DATA_SIZE_MAX 256
#define LPM_DATA_SIZE_MIN 1
+static_assert(LPM_DATA_SIZE_MAX * 8 <= U16_MAX);
+
#define LPM_VAL_SIZE_MAX (KMALLOC_MAX_SIZE - LPM_DATA_SIZE_MAX - \
sizeof(struct lpm_trie_node))
#define LPM_VAL_SIZE_MIN 1
@@ -598,8 +606,7 @@ 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 +
- trie->map.value_size;
+ leaf_size = trie_value_off(trie) + trie->map.value_size;
err = bpf_mem_alloc_init(&trie->ma, leaf_size, false);
if (err)
goto free_out;
@@ -766,8 +773,7 @@ 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 = trie_value_off(trie) + trie->map.value_size;
return elem_size * READ_ONCE(trie->n_entries);
}
pw-bot: cr
next prev parent reply other threads:[~2026-11-25 4:50 UTC|newest]
Thread overview: 8+ 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 ` [PATCH bpf-next v2 2/4] bpf: Align lpm_trie map values to 8 bytes Tobias Philipp
2026-11-24 22:56 ` sashiko-bot
2026-11-25 4:50 ` Alexei Starovoitov [this message]
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=DN40RTGK98VJ.2XIDDVK3YL2ZC@gmail.com \
--to=alexei.starovoitov@gmail.com \
--cc=andrii@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=tphilipp@gmail.com \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.