public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
From: Matan Cohen <matan@matanco.space>
To: netdev@vger.kernel.org
Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org,
	pabeni@redhat.com, daniel@iogearbox.net,
	linux-kernel@vger.kernel.org, Matan Cohen <matan@matanco.space>
Subject: [PATCH] net: lwt_bpf: replace name-based BPF program comparison with digest
Date: Mon, 27 Apr 2026 22:51:19 +0000	[thread overview]
Message-ID: <20260427225119.13733-1-matan@matanco.space> (raw)

bpf_lwt_prog_cmp() used strcmp() on program names as a workaround
because delete requests rebuild the LWT state, creating a new
bpf_prog instance for the same program.

Name comparison is unreliable: programs with the same name but
different instructions compare equal, while instruction-identical
programs with different names compare unequal.

Replace it with a two-step approach:
- Pointer equality as a cheap fast path.
- SHA256 digest comparison as the fallback. bpf_prog_calc_tag()
  computes the digest (instructions with map fds zeroed) at the
  end of bpf_check(), so it is valid for every successfully
  loaded BPF program.

Signed-off-by: Matan Cohen <matan@matanco.space>
---
 net/core/lwt_bpf.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/net/core/lwt_bpf.c b/net/core/lwt_bpf.c
index f71ef82a5f3d3..6999da98b6b97 100644
--- a/net/core/lwt_bpf.c
+++ b/net/core/lwt_bpf.c
@@ -494,17 +494,20 @@ static int bpf_encap_nlsize(struct lwtunnel_state *lwtstate)
 
 static int bpf_lwt_prog_cmp(struct bpf_lwt_prog *a, struct bpf_lwt_prog *b)
 {
-	/* FIXME:
-	 * The LWT state is currently rebuilt for delete requests which
-	 * results in a new bpf_prog instance. Comparing names for now.
+	/* Delete requests rebuild the LWT state, so pointer equality is
+	 * not sufficient. Compare by digest (SHA256 of instructions with
+	 * map fds zeroed by bpf_prog_calc_tag()) as a reliable fallback.
 	 */
-	if (!a->name && !b->name)
+	if (!a->prog && !b->prog)
 		return 0;
 
-	if (!a->name || !b->name)
+	if (!a->prog || !b->prog)
 		return 1;
 
-	return strcmp(a->name, b->name);
+	if (a->prog == b->prog)
+		return 0;
+
+	return memcmp(a->prog->digest, b->prog->digest, SHA256_DIGEST_SIZE);
 }
 
 static int bpf_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
-- 
2.43.0


                 reply	other threads:[~2026-04-27 22:51 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20260427225119.13733-1-matan@matanco.space \
    --to=matan@matanco.space \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /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