From: Mykyta Yatsenko <mykyta.yatsenko5@gmail.com>
To: sashiko-reviews@lists.linux.dev, bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next 3/5] bpf: tcp: Prefetch sk storage cache[] before TCP transmit
Date: Fri, 4 Dec 2026 21:25:38 +0000 [thread overview]
Message-ID: <9daeb69e-43c5-4fad-8ad3-5eb5a9d81ddd@gmail.com> (raw)
In-Reply-To: <sashiko-outbox-250619@kernel.org>
On 12/4/26 8:23 PM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 2 potential
> issues to consider:
> - [Medium] bpf: prefetchw() on read-only cache[] in
> bpf_sk_storage_prefetch_cache()
> - [Medium] bpf: tcp: bpf_sk_storage_prefetch_cache() loads cold
> sk_bpf_storage on TX
> --
>
> commit 2c340de4783284547e3b378ba37fabbc3b62bef3
> Author: Mykyta Yatsenko <yatsenko@meta.com>
>
> bpf: tcp: Prefetch sk storage cache[] before TCP transmit
>
> This commit adds a static-key-gated helper to prefetch a socket's BPF
> local storage cache[] array at the start of tcp_sendmsg_locked() and
> __tcp_transmit_skb(), enabled via a delayed work item whenever any
> BPF_MAP_TYPE_SK_STORAGE map exists.
>
> Link: https://lore.kernel.org/bpf/DN8A36W906WG.1BTUYRC2FIIR@gmail.com/
> Link: https://github.com/llvm/llvm-project/pull/248194
>
>> diff --git a/include/net/bpf_sk_storage.h b/include/net/bpf_sk_storage.h
>> index 2926f1f00d656..c97cbc299bbb2 100644
>> --- a/include/net/bpf_sk_storage.h
>> +++ b/include/net/bpf_sk_storage.h
>
> [ ... ]
>
>> @@ -29,6 +31,29 @@ struct sk_buff;
>> struct nlattr;
>>
>> #ifdef CONFIG_BPF_SYSCALL
>> +DECLARE_STATIC_KEY_FALSE(bpf_sk_storage_xmit_key);
>> +
>> +/*
>> + * Prefetch cache[] of the storage of sk before a TCP transmit, so that an
>> + * egress program does not wait for it. There is no RCU read lock: the storage
>> + * can be freed at any time, so only prefetch it and never load from it.
>> + * On x86-64, clang emits no instruction for prefetch(), so use prefetchw().
>> + */
>> +static inline void bpf_sk_storage_prefetch_cache(const struct sock *sk)
>> +{
>> + const void *storage;
>> +
>> + if (!static_branch_unlikely(&bpf_sk_storage_xmit_key))
>> + return;
>> + storage = rcu_access_pointer(sk->sk_bpf_storage);
>> + if (!storage)
>> + return;
>
> [Severity: Medium]
> Does synchronously loading sk->sk_bpf_storage on every TCP transmit add a
> cold cache miss for sockets that do not use egress sk_storage?
In this patch, yes, while any sk storage map exists. Patch 4 adds a
gate: the hooks do the load only when a bpf_sk_storage_get()
has marked a slot.
>
> sk_bpf_storage sits in the cold tail cache line of struct sock alongside
> sk_destruct, sk_reuseport_cb, and sk_rcu, outside sock_read_tx, so TCP TX
> otherwise never touches it.
>
> Because bpf_sk_storage_xmit_key is enabled whenever any sk_storage map
> exists, workloads using sk_storage only outside egress (such as ingress
> filters or tracing) take an extra cache miss in tcp_sendmsg_locked() and
> __tcp_transmit_skb(), even when sk_bpf_storage is NULL.
>
> Moving sk_bpf_storage into sock_read_tx or gating the prefetch on egress
> BPF programs would avoid this TX cache footprint regression.
>
>> + /* cache[] is at the start of the storage: one or two cache lines */
>> + prefetchw(storage);
>> + if (sizeof_field(struct bpf_local_storage, cache) > SMP_CACHE_BYTES)
>> + prefetchw(storage + SMP_CACHE_BYTES);
>
> [Severity: Medium]
> Can prefetchw() on read-only storage->cache[] cause cross-CPU cache-line
> bouncing on full-duplex TCP connections?
Yes, theoretically, when programs on another CPU read the same storage.
The hooks use prefetchw() because, with clang, prefetch() emits no
instruction on x86-64, fixes for both kernel and llvm are submitted.
>
> bpf_local_storage_lookup() only reads local_storage->cache[] on the fast
> path. On x86-64 with X86_FEATURE_3DNOWPREFETCH, prefetchw() issues RFO
> and invalidates shared copies on other CPUs, causing MESI ping-pong when
> TX and RX (tcp_v4_rcv() -> tcp_filter()) run on different CPUs.
>
> Non-x86 architectures such as arm64 already emit inline assembly read
> prefetches for prefetch() (prfm pldl1keep), so using prefetchw() forces
> unnecessary store-intent prefetches (prfm pstl1keep) there as well.
>
> In addition, bpf_local_storage_cache_idx_get() assigns slots starting
> from index 0, so with 8 or fewer sk_storage maps cache[8..15] is never
> used and unconditionally prefetching storage + SMP_CACHE_BYTES doubles
> the unnecessary RFO and L1 traffic.
>
next prev parent reply other threads:[~2026-12-04 21:25 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 20:03 [PATCH bpf-next 0/5] bpf: tcp: Prefetch sk storage before TCP transmit Mykyta Yatsenko
2026-12-04 20:03 ` [PATCH bpf-next 1/5] bpf: Make a NULL cache slot mean that local storage has no value Mykyta Yatsenko
2026-12-04 20:03 ` [PATCH bpf-next 2/5] selftests/bpf: Test sk storage maps that share cache slots Mykyta Yatsenko
2026-12-04 20:03 ` [PATCH bpf-next 3/5] bpf: tcp: Prefetch sk storage cache[] before TCP transmit Mykyta Yatsenko
2026-12-04 20:23 ` sashiko-bot
2026-12-04 21:25 ` Mykyta Yatsenko [this message]
2026-12-04 20:03 ` [PATCH bpf-next 4/5] bpf: tcp: Prefetch values of hot sk storage maps before transmit Mykyta Yatsenko
2026-12-04 20:18 ` sashiko-bot
2026-12-04 21:35 ` Mykyta Yatsenko
2026-12-04 20:03 ` [PATCH bpf-next 5/5] selftests/bpf: Test sk storage lookups during TCP transmit Mykyta Yatsenko
2026-12-04 20:17 ` sashiko-bot
2026-12-04 21:40 ` Mykyta Yatsenko
2026-12-04 21:38 ` [PATCH bpf-next 0/5] bpf: tcp: Prefetch sk storage before " Andrii Nakryiko
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=9daeb69e-43c5-4fad-8ad3-5eb5a9d81ddd@gmail.com \
--to=mykyta.yatsenko5@gmail.com \
--cc=bpf@vger.kernel.org \
--cc=sashiko-reviews@lists.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox