From: Mykyta Yatsenko <mykyta.yatsenko5@gmail.com>
To: sashiko-reviews@lists.linux.dev, bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next 4/5] bpf: tcp: Prefetch values of hot sk storage maps before transmit
Date: Fri, 4 Dec 2026 21:35:57 +0000 [thread overview]
Message-ID: <2f700c05-707f-4abc-9fc9-68c44a6ef013@gmail.com> (raw)
In-Reply-To: <sashiko-outbox-250614@kernel.org>
On 12/4/26 8:18 PM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 3 potential
> issues to consider:
> - [Medium] bpf: unconditional per-CPU RMW in
> bpf_sk_storage_prefetch_values()
> - [Medium] bpf: unconditional this_cpu_or() in
> bpf_sk_storage_xmit_mark()
> - [Medium] bpf: RFO prefetchw() in bpf_sk_storage_prefetch_hot() bounces
> shared lines
> --
>
> commit f0130b9d642d8428468cd298e6da4b722527388b
> Author: Mykyta Yatsenko <yatsenko@meta.com>
>
> bpf: tcp: Prefetch values of hot sk storage maps before transmit
>
> This commit records which bpf_local_storage cache[] slots are looked up
> per CPU in bpf_sk_storage_get() and prefetches the elements in those
> slots from __tcp_transmit_skb() before calling ->queue_xmit(), clearing
> the per-CPU mask every 256 transmits.
>
> Link: https://lore.kernel.org/bpf/DN8A36W906WG.1BTUYRC2FIIR@gmail.com/
>
>> diff --git a/include/net/bpf_sk_storage.h b/include/net/bpf_sk_storage.h
>> index c97cbc299bbb2..ec12c3aaa9775 100644
>> --- a/include/net/bpf_sk_storage.h
>> +++ b/include/net/bpf_sk_storage.h
>
> [ ... ]
>
>> @@ -54,6 +66,20 @@ static inline void bpf_sk_storage_prefetch_cache(const struct sock *sk)
>> prefetchw(storage + SMP_CACHE_BYTES);
>> }
>>
>> +/* Prefetch the values of sk in the slots seen in this round of 256 transmits */
>> +static inline void bpf_sk_storage_prefetch_values(const struct sock *sk)
>> +{
>> + u16 seen;
>> +
>> + if (!static_branch_unlikely(&bpf_sk_storage_xmit_key))
>> + return;
>> + if (unlikely(!this_cpu_inc_return(bpf_sk_storage_xmit.round)))
>> + this_cpu_write(bpf_sk_storage_xmit.seen, 0);
>> + seen = this_cpu_read(bpf_sk_storage_xmit.seen);
>> + if (seen && rcu_access_pointer(sk->sk_bpf_storage))
>> + bpf_sk_storage_prefetch_hot(sk, seen);
>> +}
>
> [Severity: Medium]
> Should bpf_sk_storage_prefetch_values() check bpf_sk_storage_xmit.seen
> before incrementing bpf_sk_storage_xmit.round?
>
> Whenever any BPF_MAP_TYPE_SK_STORAGE map exists, __tcp_transmit_skb()
> runs this_cpu_inc_return(bpf_sk_storage_xmit.round) on every TCP segment,
> even on CPUs where seen is zero.
>
> Outside x86, arm64, and s390, this_cpu_add_return_1 falls back to
> raw_local_irq_save()/raw_local_irq_restore() per packet. On arm64 and
> x86-64 it still executes an atomic RMW (ldaddb or xaddb) with a memory
> clobber, dirtying the per-CPU cacheline on every transmit.
>
> Reading seen first and returning when it is zero would skip the RMW when
> no slots are marked, matching bpf_sk_storage_prefetch_cache().
Agree.
>
> [ ... ]
>
>> diff --git a/net/core/bpf_sk_storage.c b/net/core/bpf_sk_storage.c
>> index 875fff681869e..5feea65d25ea3 100644
>> --- a/net/core/bpf_sk_storage.c
>> +++ b/net/core/bpf_sk_storage.c
>> @@ -39,6 +39,43 @@ static void bpf_sk_storage_key_fn(struct work_struct *work)
>
> [ ... ]
>
>> +/* As in a lookup, the RCU read lock keeps the storage and the elements */
>> +void bpf_sk_storage_prefetch_hot(const struct sock *sk, unsigned long hot)
>> +{
>> + struct bpf_local_storage_data *sdata;
>> + struct bpf_local_storage *storage;
>> + int i;
>> +
>> + guard(rcu)();
>> + storage = rcu_dereference(sk->sk_bpf_storage);
>> + if (!storage)
>> + return;
>> + for_each_set_bit(i, &hot, BPF_LOCAL_STORAGE_CACHE_SIZE) {
>> + sdata = rcu_dereference(storage->cache[i]);
>> + if (sdata)
>> + prefetchw(sdata);
>> + }
>> +}
>
> [Severity: Medium]
> Can prefetchw(sdata) here cause cross-CPU cacheline bouncing in
> full-duplex TCP workloads?
>
> On x86-64 (with X86_FEATURE_3DNOWPREFETCH) and arm64 (prfm pstl1keep),
> prefetchw() issues a store-intent read-for-ownership prefetch that
> invalidates shared copies on other CPUs. The same applies to the
> prefetchw() calls on storage->cache[] in bpf_sk_storage_prefetch_cache().
>
> Because bpf_sk_storage_xmit_mark() runs for all bpf_sk_storage_get()
> callers, a map read only on the RX CPU is still pulled into exclusive
> state by the TX CPU on every transmit. On arm64, prefetch() already
> emits prfm pldl1keep and does not need the x86-64 clang workaround.
>
Same as previous patch.
>> +
>> +static void bpf_sk_storage_xmit_mark(struct bpf_map *map)
>> +{
>> + u16 idx = ((struct bpf_local_storage_map *)map)->cache_idx;
>> +
>> + this_cpu_or(bpf_sk_storage_xmit.seen, BIT(idx));
>> +}
>
> [Severity: Medium]
> Can bpf_sk_storage_xmit_mark() skip this_cpu_or() when BIT(idx) is
> already set in bpf_sk_storage_xmit.seen?
>
> Because seen is only cleared every 256 TCP transmits (and never on CPUs
> not transmitting TCP), BIT(idx) is already set on almost every
> bpf_sk_storage_get() and bpf_sk_storage_get_tracing() lookup.
>
> Outside x86, arm64, and s390, this_cpu_or_2 falls back to
> this_cpu_generic_to_op(), adding raw_local_irq_save() and
> raw_local_irq_restore() to every lookup; on arm64 it executes an LSE
> ldseth (or LL/SC loop) instead of a plain load.
>
> Guarding the update with an unlikely check on
> this_cpu_read(bpf_sk_storage_xmit.seen) & BIT(idx) avoids the atomic RMW
> and IRQ disable on the steady-state lookup path.
>
next prev parent reply other threads:[~2026-12-04 21:36 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
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 [this message]
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=2f700c05-707f-4abc-9fc9-68c44a6ef013@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