Re: [PATCH bpf-next v3 1/2] bpf: Add bpf_memmem and bpf_dynptr_memmem kfuncs
From: Alexei Starovoitov
Date: Sun Nov 22 2026 - 19:25:26 EST
On Sun, Nov 22, 2026 at 06:38 AM Abhishek Khade <abhishekkhade69@xxxxxxxxx> 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@xxxxxxxxx/
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