Article View: linux.kernel.netdev
Article #22298Re: [PATCH bpf-next v2 2/4] bpf: Align lpm_trie map values to 8 bytes
From: "Alexei Starovoitov"
Date: Wed, 25 Nov 2026 04:50
Date: Wed, 25 Nov 2026 04:50
107 lines
15547 bytes
15547 bytes
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
Technical Details
Message-ID:
<DN40RTGK98VJ.2XIDDVK3YL2ZC@gmail.com>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-371029-linux-kernel-netdev=dewy.ruReferences:
<20261124225055.87344-1-tphilipp@gmail.com> <20261124225055.87344-3-tphilipp@gmail.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-371029-linux-kernel-netdev=dewy.ru
From: alexei.starovoitov@gmail.com ("Alexei Starovoitov")
Newsgroups: linux.kernel.netdev
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
Organization: Usenet Network
Message-ID: <DN40RTGK98VJ.2XIDDVK3YL2ZC@gmail.com>
References: <20261124225055.87344-1-tphilipp@gmail.com> <20261124225055.87344-3-tphilipp@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
logging-data="114142"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:yHqH3Eej3F1GbdNQXD4uoi78LeI= sha256:QwYMrHL2cmHT8gKXRLvvZrWqqLaokQJIbVmMA2CRfRY=
sha1:6YhMs4WGYXnsCE6Dfrxn+2PQTkQ= sha256:6ZLjYDO9KeK8+nzBQQQfuo4WfIvsEmH+BWhczO9tL9U=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
by mail.dewy.ru (Postfix) with ESMTP id CBD45125E35; Wed, 25 Nov 2026 07:50:31 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.049
X-Spam-Status: No, score=-3.049 tagged_above=-9999 required=6.31
tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
DKIM_VALID_AU=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.001,
FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249,
MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
header.d=gmail.com
X-Received: from mail.dewy.ru ([127.0.0.1])
by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id k5U42NdWIaAz;
Wed, 25 Nov 2026 07:50:28 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-371029-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN>
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=gmail.com
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
by mail.dewy.ru (Postfix) with ESMTPS id 6D39A125E39; Wed, 25 Nov 2026 07:50:19 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
by sea.lore.kernel.org (Postfix) with ESMTP id D76C734F19; Wed, 25 Nov 2026 04:50:17 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
t=1795582217;
b=gVOEpC3DnTe7HxrJCO50gYjPHML2I48LkDHjG5zam3qEz8nHn83Ahv53uRM6AAEerPyT
ZcaPH+45yJdltYJkXkmBPc8PXWLi1nk1Wf3IOutmi/SfKhX9niysq/7GqtmX63z3fidgb
gdCDeLslpmklGPlqYujMUJ8cfedHUbxqL0n59jlG5Qja9h47EaTGlbnjb+6+e5cGu4Zd/
5kVS8nPW3G15f2zIltkw7ifx/5ZH7PTGF9lPnPSVfgbn9gBCdxATTMfQlYYsxs0gFO2WW
851+wT4zW+SEQ1pyqSPeSAjlt/Ds1oi5rlthvFmthSNplMbGyh5cGlIfAb/I7WqPrsA==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1795582217;
h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:
X-Google-DKIM-Signature:X-Forwarded-Encrypted:X-Gm-Message-State:
X-Gm-Gg:X-Received:Received:Content-Type:Date:Message-Id:From:To:Cc:
Subject:In-Reply-To:References:Content-Transfer-Encoding:
X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
List-Unsubscribe:List-Owner:Precedence:MIME-Version;
bh=WZpqROJT0TFZ0N9AeyXaUSyo/Ga6Gz526tDibJ18xQ0=;
b=oRr5WATDuGwQAuEi+aMiaKkK8svCSVLeGpHi/de0/YeUlQI2DwebtPlV0Qv4qLarP5Dn
q6cbIF95BNAQxzasEaQfvCT0KFT2jYka5L1PENb0elXqZUbVRP8iFGM7ifFgMPNLIv6Ig
xA5BuNSGsF4y+GK9VD07nzz8p14QXcGXs2Xh8+bP8+G4YG6fwSNpZA3hk9krb7Aa7NFhK
C8f20uc/F7ujvBguwxq/dHqagwuZrtfG9aIcE9hbr2u3qsH6WSaj5A7D51Z28pUDfjJMg
IEZDUg6BZj4hMAddiWD00qFbnvxHuM6Fo+vlr5OA7dLraeEo9TAshOS/7um7iIk/SFg==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org;
dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b="LuIyU/xm";
dmarc=pass header.from=gmail.com;
spf=pass smtp.mailfrom=gmail.com;
arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
by smtp.subspace.kernel.org (Postfix) with ESMTP id 258C7606A336;
Wed, 25 Nov 2026 04:50:17 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=LuIyU/xm
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.179
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
t=1795582216;
b=PTrYSA/s5GLX5ZgGEnx75cpwIxvO4uBiGFx7bxHJkZk6K1bzoc7x64cwfU+8CqYMSV1O
NgNNmg1lka4vSmuWohcNYpuUQXINiqHoCGvzYrVbJIVZnCCQIfOEJZwy+adjTdKtLUXGG
++2Abx0eGnsLqTomKKS5Dp7g+a3+dcI1n0BPxBUzhipBPAlUi4O/LJKx8yBtVNMEOHDCl
iD57LjFdL7fhJGn9gSeJYG9wgG22aGZXy+Axf7Eo3/oeVuTWPJ+DKNDo3Csky0VnK6+mg
PusHlm8sh5DYcoZZxH3Gq0pt7ORT2bp0Msw8wMsKUZf5txeydiA6OOYjU4Speul/B9Q==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1795582216;
h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature:
X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received:
Content-Type:Date:Message-Id:From:To:Cc:Subject:In-Reply-To:
References:Content-Transfer-Encoding:MIME-Version;
bh=WZpqROJT0TFZ0N9AeyXaUSyo/Ga6Gz526tDibJ18xQ0=;
b=aJr86yB+c7ieGCaet2KO90+cENClvU3eOgfJHGqJePeSwW0zXn+YpRaRl60R2WciJCuv
JbC/HMpqTXdmTcbGSav5I9/6eC+k3l/XrazZ+z70em+OAFHvGWocHHCHkdSeYKpXrPdiu
V4ySQIff1EsAOfJ85WjIWyspXA2Q+knNdckuUyNk1CKg7nHO7iXqT1wxMtDe2ITbxd667
jo7UgA7vwJHhNe0ucvw5AlLQaGs1VO1pAp45xSKKLS91D5X7rprfHW4NAgosFoxCH79Yp
b1rZ539b45gooLvDFUFnYpk1mNB9K0c3WLHl3/kd/7n1RcswUBJ6UAt1fYY9NGtjPIA==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
dmarc=pass header.from=gmail.com;
spf=pass smtp.mailfrom=gmail.com;
dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b="LuIyU/xm";
arc=none smtp.remote-ip=209.85.214.179
X-Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179])
(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 DDF34606A31E
for <netdev@vger.kernel.org>; Wed, 25 Nov 2026 04:50:15 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DDF34606A31E
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com
X-Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2f0464dd6b6so31855375ad.1
for <netdev@vger.kernel.org>; Tue, 24 Nov 2026 20:50:15 -0800 (PST)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20260707; t=1795582215; x=1796187015; darn=vger.kernel.org;
h=mime-version:content-transfer-encoding:references:in-reply-to
:subject:cc:to:from:message-id:date:content-type:from:to:cc:subject
:date:message-id:reply-to:content-type;
bh=WZpqROJT0TFZ0N9AeyXaUSyo/Ga6Gz526tDibJ18xQ0=;
b=LuIyU/xmh3BDPSXKUR92JkBXXf570oqi3Y8UK36zn1m+izg3teFHRAvTAlsB9d4Yrt
jiyFW/a/MDgD+SiC5LcOCuHZl/d0LZ9ojLoiBiRbLUPiAXPE8V1tn04Z+xZZ9S02OkHv
q74wRiIEDaVGWFMdQDDSj8tnr6h4jMIfQmwG8BpiH8brOWUXWyLafDX1bdQcLxyv3+Xk
2qTfSxK48tIV1vEIW0cJO/CVnuRR6zspU4WW7VZfJtZDjxb1iPSXnQfotUjBi4HNNeuy
HVpkZPW9A3Lq8RWbFGAs1/Whr/lJFzu2iS7gC2zjV4p1SX5A+QZxBfDnIkWGH6l0Zzy+
7YnA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20260707; t=1795582215; x=1796187015;
h=mime-version:content-transfer-encoding:references:in-reply-to
:subject:cc:to:from:message-id:date:content-type:x-gm-gg
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to
:content-type;
bh=WZpqROJT0TFZ0N9AeyXaUSyo/Ga6Gz526tDibJ18xQ0=;
b=00K0y9xanPgoGtF89TzyiVGSE77CdaMfnS6/yrQcPiC96tuFw1somgKL79CDSnbd81
QGGL3rdwU3Xeex1CylmgAYuxkJ0qtDscClLb0ufqfsoAJNcIFQXBAsBHTMuts5Tzulru
B59PXcunOxqaUgMOO2R3Hsz2GRrS6owdfXVUEF8DDxRH+fAUCEMzvT83yKGFp+lWB5Jl
H4/N0pRhYbi3rQeiwgjrrAOazWIyp5SbLLYZP6MOf8X36X6ZAmh49ZVbPOFZDHpzYm9i
f2aWKa8u2fA9UjnZno4mJNFpWYsvo0l+Gm2lVHSbxLKQuDO2rII0raKDITsgaXvxNWXf
QQSQ==
X-Forwarded-Encrypted: i=1; AL+hErG4sXJrdaWQb7SIF8Y8+jGyy1Jg5VyXUgUeBhOwLTJUT6Kq1oEVY7OsJOaorKMWNnn2gFyrEpY=@vger.kernel.org
X-Gm-Message-State: APuU4Z8VVfDB00+81dmS9Ghpqg1+ZRhe81nEwC7pOnkgAmLZHIEO6SkN
z5KxYGzKtbPVjwtdMEWtu2v3ZS4CSUY6f0tlrLx8C4ucUOoTVZDWouot
X-Gm-Gg: AZFdTZf8FA0sePWDT9u0bpUsG1B2krSh6geOaLgIRn0Ierxve5gXc9D7ydRDGM/BA25
1K6+oujBTRZ8Qha/K6dhW6zMmwCFU0Z/RvwckEpoPu8mkYuVdanTWf18g0LPSUro/1ttMX5wOue
3U9EsCps9p9a9CVU1qqaLfhjeaSaEw5b1VXJt18J2EQqny/QKFmiAvsonNFhJ+tiHdMypA7Q/XG
UkWXUCbCiO6b68xZ1C2gBz54DuEI/X/I0VSLAO0xIvxx2DQuqPC7xEcH2Qt3vVBlAowlddgUdzt
LH178+tfVqdFpZluyAWABC7ToPrlB6Y3MA/HwSIbIYEXTWwBdCwWduyvUcSRC6R5k3PFTv++LsU
sKg0CzQ3mZDG7ScD7D0M0l8/0Qm4V0wRfHJu6lsPpmgEwn9ijPBXrsRO6pJ/CdKmlRhOpafOnJk
lC5remXu4hVpybAfG14Zu+U3aceFYjY9B56jEkvhJH+z3Su8T7UOdJbwulydo9gZWPzra6dqvlS
37eOSAkL6r1lhNG44O1dJRYvTH3KONby/BIiGI9LkPqKbSsfF6KW/1Xm9GkOq0GVEqNc9wXzx5X
aZrEvr7kfN9z/WFHdUBfh/x8mQPqqJ+I6dIsHNoIqm5joxKXDA9N6ebCqN//A01+BIOYmN2X6Ao
a/UUz1L477KYCzR4m2yYF7dJAWlNNHyZ09GGlSEa4nTW8GS4JDImLYFw=
X-Received: by 2002:a17:903:b4f:b0:2f0:22fe:65e5 with SMTP id d9443c01a7336-2f03f1d0f8amr148657735ad.60.1795582214938;
Tue, 24 Nov 2026 20:50:14 -0800 (PST)
X-Received: from localhost ([153.61.198.248])
by smtp.gmail.com with ESMTPSA id d9443c01a7336-2f0457fd68dsm70131345ad.39.2026.11.24.20.50.14
(version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);
Tue, 24 Nov 2026 20:50:14 -0800 (PST)
X-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>
X-In-Reply-To: <20261124225055.87344-3-tphilipp@gmail.com>
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list