All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Abhishek Khade" <abhishekkhade69@gmail.com>, <bpf@vger.kernel.org>
Cc: <linux-kernel@vger.kernel.org>, <daniel@iogearbox.net>,
	<andrii@kernel.org>
Subject: Re: [PATCH bpf-next v3 1/2] bpf: Add bpf_memmem and bpf_dynptr_memmem kfuncs
Date: Mon, 23 Nov 2026 00:25:18 +0000	[thread overview]
Message-ID: <DN25VVZI22A9.1KXUP775JTIJB@gmail.com> (raw)
In-Reply-To: <20261122143815.330528-2-abhishekkhade69@gmail.com>

On Sun, Nov 22, 2026 at 06:38 AM Abhishek Khade <abhishekkhade69@gmail.com> wrote:

TLDR: nack

> 1. Writing a loop using bpf_loop() or open-coded iterators: Even with
>    verifier inlining, this executes 1 subprogram call per byte and

No. That's true only for bpf_loop().
bpf_for() has no call per byte since
commit e93347704878 ("bpf: Inline bpf_iter_num_next() kfunc").
Plain C loops load too. See progs/plain_c_*.c in selftests.

>    cannot leverage 64-bit word-at-a-time SIMD scanning, making it
>    significantly slower than kernel-internal scanning.

It can. Leon measured a bpf prog that scans a word at a time.
2048 bytes took 4557 ns in that prog and 7389 ns in bpf_strnstr():
https://lore.kernel.org/bpf/20260728145727.45153-1-leon.hwang@linux.dev/
Andrii's answer there applies here: such code stays in the prog.
This series has no numbers and names no user.

> +			while (n_idx < needle_sz && n[n_idx] == h[h_idx + n_idx])
> +				n_idx++;

This is one byte at a time.
memchr() runs only for a 1 byte needle. On x86-64 it's a byte loop too.

> +	data = __bpf_dynptr_data(ptr, size);
> +	if (!data)
> +		return -EINVAL;

This fails for the use cases from the commit log.
bpf_dynptr_slice() gets a NULL buffer and the full size:

  dynptr          callee                   fails on
  --------------  -----------------------  ----------------------
  non-linear skb  skb_pointer_if_linear()  len > skb_headlen()
  xdp with frags  bpf_xdp_pointer()        len > data_end - data
  file            bpf_file_fetch_bytes()   !buf

A filter that drops on ret >= 0 passes all such packets.
bpf_memmem() doesn't help. The verifier rejects data_end - data
as haystack__sz. See check_mem_size_reg().

No need for new kfuncs. The prog can do it in linear time, with frags.
This one takes needles up to 64 bytes.
dynptr_run() in prog_tests/plain_c.c feeds it an xdp buff with a frag.
clang builds plain_c_dynptr_success.c with this, I didn't load or run it:

diff --git a/tools/testing/selftests/bpf/progs/plain_c_dynptr_success.c b/tools/testing/selftests/bpf/progs/plain_c_dynptr_success.c
index 2c1c4f7..1a47cb5 100644
--- a/tools/testing/selftests/bpf/progs/plain_c_dynptr_success.c
+++ b/tools/testing/selftests/bpf/progs/plain_c_dynptr_success.c
@@ -173,3 +173,46 @@ int test_dynptr_memset_xdp_chunks(struct xdp_md *xdp)
 out:
 	return XDP_DROP;
 }
+
+static const __u8 needle[] = { 0xde, 0x00, 0xad, 0x00, 0xbe, 0x00, 0xef };
+static __u64 mask[256]; /* bit i of mask[c] is set if needle[i] == c */
+
+/* shift-and: bit i of 'state' is set if the last bytes are needle[0..i] */
+static long dynptr_memmem(struct bpf_dynptr *ptr, __u32 needle_sz)
+{
+	__u64 size = bpf_dynptr_size(ptr), off, len, i, state = 0;
+	__u8 buf[256];
+
+	for (off = 0; off < size; off += len) {
+		len = min_t(__u64, sizeof(buf), size - off);
+		if (bpf_dynptr_read(buf, len, ptr, off, 0))
+			return -EFAULT;
+		for (i = 0; i < len; i++) {
+			state = (state << 1 | 1) & mask[buf[i]];
+			if (state >> (needle_sz - 1))
+				return off + i + 1 - needle_sz;
+		}
+	}
+	return -ENOENT;
+}
+
+SEC("?xdp")
+__success
+int test_dynptr_memmem_xdp(struct xdp_md *xdp)
+{
+	void *data_end = (void *)(long)xdp->data_end;
+	void *data = (void *)(long)xdp->data;
+	__u32 off = data_end - data - 3;
+	struct bpf_dynptr ptr;
+	int i;
+
+	for (i = 0; i < sizeof(needle); i++)
+		mask[needle[i]] |= 1ULL << i;
+
+	/* the needle starts in the linear part and ends in the frag */
+	bpf_dynptr_from_xdp(xdp, 0, &ptr);
+	err = bpf_dynptr_write(&ptr, off, (void *)needle, sizeof(needle), 0);
+	if (!err && dynptr_memmem(&ptr, sizeof(needle)) != off)
+		err = 1;
+	return XDP_DROP;
+}

pw-bot: cr

  parent reply	other threads:[~2026-11-23  0:25 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-22 14:38 [PATCH bpf-next v3 0/2] bpf: Add bpf_memmem and bpf_dynptr_memmem kfuncs Abhishek Khade
2026-11-22 14:38 ` [PATCH bpf-next v3 1/2] " Abhishek Khade
2026-11-22 15:17   ` bot+bpf-ci
2026-11-23  0:25   ` Alexei Starovoitov [this message]
2026-11-22 14:38 ` [PATCH bpf-next v3 2/2] selftests/bpf: Add selftests for " Abhishek Khade
2026-11-22 15:17   ` bot+bpf-ci

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=DN25VVZI22A9.1KXUP775JTIJB@gmail.com \
    --to=alexei.starovoitov@gmail.com \
    --cc=abhishekkhade69@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=linux-kernel@vger.kernel.org \
    /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.