From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.53 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796085186; b=PD9vQR9RwEFx6IKI53G9laq9xQIoUoY0HuLFdlizy4B4OveCWP/Qj80KtSmK9YOhuK0E llIZaYsLLM6fZj+Ty4L1Qs2ZV3dY8iWO+ku8Zo48oRt9vMweGJOrPsBQaOE7nIsssntSg sswIp+kIjdPzZrtH0TzX3clGXvhx1LC7eFj0x9vP5eonoPre0ZeyfGw7ygdKAavooWiki rpGtxVbwPHCSIrC2iFHOuFjoADG61yj31Cr2El7to70ffpi9K/zpMRs0gTG9zKIfWPaG+ 7UNgTWLmSnSHVSp37/qinsxGehSivc2dKEGCVOC/cSjDEfAaKoVGUqimuPabKQNRrbA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796085186; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Message-ID:Date:MIME-Version:User-Agent:Subject:To:Cc:References: Content-Language:From:In-Reply-To:Content-Type: Content-Transfer-Encoding; bh=47tPX4jZXAc0bZEei4sRbSZh57S1FezxylWhmwoBZR8=; b=YjBhct8BJDxXo0ndSWjCUVf6KKNLOvoOcnZF6u/wtP824ZCqvC8XgCR9k9fSU80wV2jJ cEcOWd8cVFsKP9fa87uF9q+7QUcoqfoY0Wxu/0GUghhy+PBTBEFxBhpqRwZi14bJLV1LU LuAGY1RsFcSoMrpfGlGYUvivxqK5dMNgcW/tEQxYjIDYX9NJit5TutDkfThCBH33PAiij Xx0+/3vO6JhHRhOI0W4mXf7gTUoydwOANb8lIRDD0DMF3mrvetOb22YFP0cUNFOOkgQKW jxFNjWNa4fzVTT9c49J1Wv1OwQFaOUZAxQpasDXWrw9oB/VJtkeLVCrnNyZEALM/Muw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=SUqzokW9; arc=none smtp.remote-ip=74.125.82.53 Received: from mail-dl1-f53.google.com (mail-dl1-f53.google.com [74.125.82.53]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9CE96606A31A for ; Tue, 1 Dec 2026 00:33:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9CE96606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=SUqzokW9 Received: by mail-dl1-f53.google.com with SMTP id a92af1059eb24-1b5b93bdc2dso1573183c88.0 for ; Mon, 30 Nov 2026 16:33:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796085185; x=1796689985; darn=vger.kernel.org; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to :content-type; bh=47tPX4jZXAc0bZEei4sRbSZh57S1FezxylWhmwoBZR8=; b=SUqzokW9c7uQQaMim298jiz9LL/iHK4Sl0XKlksJ2C9eg60q6kIFvPTmf799KdDgxJ 8v2Xn5K8SechFlCv37KD/F2QntBJh+ESVp6oRTWgz0Q4Y5D+Qw32vu+wqnZvuVGjZueC 9ti7uo2KYxJBq9Ht1DRwJXmmss7TzNYevw6vkFwanDR3kMA2vZe/OlncTipJKfGysJRt KMU/AWvyL0/0CjGmbVHEWo+cmbJfXpFfwGHGp9fskKgi6Upi78bIXd47xEpTYxbnPAam frQcPPrU7TuVdyW0QqOcEPQELMVZc203zk4YiMZUBzFluoG9McLnoVo2gZltDl3RVkXy d/Qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796085185; x=1796689985; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=47tPX4jZXAc0bZEei4sRbSZh57S1FezxylWhmwoBZR8=; b=YxUQ6n1iLeaILljS93jEMLEaM5TWgRKXJQL0GTesR1ZwNchHg5X8y5jpxp8hn2L14V CWN3+iFOxJHAhtWabWPG5QUkGWjL8mfzqF+JNk1AfUvVNJxhHUlrI4heO1oU2MVUc1Ri k1QcUqjSjn1BaL/V3O7VNoh7pZDSeHCWIq63kU9tDghm4i+f+M0bTfJZ12n6Ts46k/zz XrwgzixmR6ewuYiEJS7uQLtA9MNd4KlirQ49/wQVda1LaJ3TgbPU+FtdmxoTBCG6weyn nIQiTxzh3SGhyAHtwRIayLPlSQ8sMmS3zD0OBvjcxKPZjsSwntYfQNLHJGr5M/gXzqDk z8Fg== X-Forwarded-Encrypted: i=1; AL+hErGFvezJmbNqM3NCNWlqvhltU3lzSWFvg29+5ujFt9WVVQNFNwB4eTDLag+lVvfPh9fuB3GK4FU=@vger.kernel.org X-Gm-Message-State: APuU4Z/vc6qT14Umf4aj8YsQB1/o5ycMxWTTsexIa5QO2SuIIU5panu+ 4JRTKxJO6hVQb/1g5/ZCC1QhY94jfWMzzWQ5vnTgRIcLsXzccKoOuYUw X-Gm-Gg: AZFdTZefyAiG311DYhtlrkWP5YDNQc1va5LCDUDShCLzeMsfjhgkyY+EJi+v1xqYc5s nBfp0eGkfHmMSvwQFciBhrXkr7bopV+OoI5hK54hTFKRrFZ6pQvjVpXTswNYK/RYRjeFfH03g83 C9NXQosVMQz7d/jrLaVSoRRXV3Z+Pf7KLQeFmc+WKAMQDkg5Q3l+GXAvvZerhSD1nVWnhRxXzIw ce3N71j0Us6cwmuy5cSh0jwqgUhomzgK2s3nR8GmFYga9w1kwQWSmb72wBMEBMEEoVqfYS7gj+n aIUuHaAxImhtKh1fdEeFm8rpo47NHRRTzhRj44iGOKNrHOSNLoqJK0ChNjmpO4CAms9aS6/AYJj lMLZKI4w+OQSKbhFibqHCKCMJ6noCjfcdQ2MjkfbY3lOGqag+vYrKmwLReyOKhslnGwWXZoAtKD uyUHqjlVf5nsCdZAnbiE+ghwMR3YatMaHvqYMIwby03KQznaar2AM8mai5F18eo1GAdZdcEvnvC U3GvGhBN+SsOC/x249Em1Ww+ANu/r062ZJTDw8wx4WNZk81F7wx1uYcwZKr9rzEZ8DZTpFmkaQu /JjK9+ptOh9MPTU6PI/xLddLohSg/rNTdtzMuooXNHgzbgSNBvSsGvDZkQDeGx7wmJqAo0rgSJU kwKhRveYeyHml X-Received: by 2002:a05:7022:7f18:b0:1b3:9b4d:85f3 with SMTP id a92af1059eb24-1b5568a28a5mr40073371c88.19.1796085184561; Mon, 30 Nov 2026 16:33:04 -0800 (PST) Received: from ?IPV6:2605:59ca:805a:8f88:9e74:fb48:60ac:3999? ([2605:59ca:805a:8f88:9e74:fb48:60ac:3999]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1b55681301asm81200505c88.8.2026.11.30.16.33.02 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 30 Nov 2026 16:33:04 -0800 (PST) Message-ID: <1f34507a-bca5-4c7a-83ad-19b9dd5b03ce@gmail.com> Date: Tue, 1 Dec 2026 00:33:01 +0000 X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH bpf-next v2 00/13] bpf: Embed local storage values in the storage object To: Alexei Starovoitov , bpf@vger.kernel.org, andrii@kernel.org, daniel@iogearbox.net, kernel-team@meta.com, eddyz87@gmail.com Cc: Mykyta Yatsenko , edumazet@kernel.org, ncardwell@google.com, kuniyu@google.com, martin.lau@linux.dev, netdev@vger.kernel.org References: <20261123-sls_f1-v2-0-76474de9b052@meta.com> Content-Language: en-US From: Mykyta Yatsenko In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 11/30/26 4:58 AM, Alexei Starovoitov wrote: > On Sun, Nov 29, 2026 at 04:57 PM Alexei Starovoitov wrote: >> 1. Move sk_bpf_storage to a tx-hot line. It's a one-line change that may remove the first miss. >> 2. Prefetch early. Under a static key, prefetch sk->sk_bpf_storage in the TCP transmit path, well before tc egress runs. That hides the second miss. >> 3. Prefetch the sibling. Once cache[] is loaded, the other map's pointer is already there, so the first lookup can do prefetch(cache[B]). > > Numbers are back... > 1 gives nothing. > 2 and 3 together are on par with this series. > It's better to do 3 early too. The diff below hides both misses: > > tcp_sendmsg(), __tcp_transmit_skb() prefetch storage->cache[] > right before ->queue_xmit() prefetch cache[i] of hot maps > tc, cgroup, fentry, ... lookup hits > > A map is hot when it was looked up for the socket that has just sent a > packet. It's learned per cpu and ages out after 256 packets, so it > doesn't depend on the prog type or the hook. > No uapi, no new limits, no change to the layout. > > A copy of your workload: 131k loopback connections, 9 maps per socket, > 2 progs, Zen 5 KVM guest. ns per request against bpf-next, +- 60: > > this series selem in the tail diff below > -------------------- ----------- ----------------- ---------- > tcx egress -540 -550 -840 > tcx egress, quickack -570 -380 -860 > cgroup egress -300 - -700 > fentry/ip_output -340 - -530 > cold, no progs +100 - +60 > hot, 8 connections -20 - +15 > > The two lookups in tcx go from 190 and 85 ns to 10 and 1 ns. > Prefetch of all entries of cache[] is simpler, but it's ~200 ns worse > with 9 maps. > > The static key is on while an sk storage map exists. Then the hooks cost > ~10 ns per packet. > Only TCP transmit and sk storage are covered. IPv6 is not measured. > > Could you pls run it on your setup? > On my setup prefetch works faster as well. The only case where my series worked better was when value is absent, prefetch does not optimise that. Either way, I'm happy to move forward with the prefetch approach. Thanks for looking into this! > diff --git a/include/net/bpf_sk_storage.h b/include/net/bpf_sk_storage.h > index 2926f1f00d65..b7e78c4739cc 100644 > --- a/include/net/bpf_sk_storage.h > +++ b/include/net/bpf_sk_storage.h > @@ -13,6 +13,8 @@ > #include > #include > #include > +#include > +#include > > struct sock; > > @@ -29,6 +31,27 @@ struct sk_buff; > struct nlattr; > > #ifdef CONFIG_BPF_SYSCALL > +extern struct static_key_false_deferred bpf_sk_storage_needed; > +void __bpf_sk_storage_prefetch_values(const struct sock *sk); > + > +static inline void bpf_sk_storage_prefetch_cache(const struct sock *sk) > +{ > + if (static_branch_unlikely(&bpf_sk_storage_needed.key)) { > + void *storage = rcu_access_pointer(sk->sk_bpf_storage); > + > + if (storage) { > + prefetch(storage); > + prefetch(storage + SMP_CACHE_BYTES); > + } > + } > +} > + > +static inline void bpf_sk_storage_prefetch_values(const struct sock *sk) > +{ > + if (static_branch_unlikely(&bpf_sk_storage_needed.key)) > + __bpf_sk_storage_prefetch_values(sk); > +} > + > int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk); > struct bpf_sk_storage_diag * > bpf_sk_storage_diag_alloc(const struct nlattr *nla_stgs); > @@ -38,6 +61,8 @@ int bpf_sk_storage_diag_put(struct bpf_sk_storage_diag *diag, > int stg_array_type, > unsigned int *res_diag_size); > #else > +static inline void bpf_sk_storage_prefetch_cache(const struct sock *sk) {} > +static inline void bpf_sk_storage_prefetch_values(const struct sock *sk) {} > static inline int bpf_sk_storage_clone(const struct sock *sk, > struct sock *newsk) > { > diff --git a/net/core/bpf_sk_storage.c b/net/core/bpf_sk_storage.c > index 67288177492a..b1fedc161bde 100644 > --- a/net/core/bpf_sk_storage.c > +++ b/net/core/bpf_sk_storage.c > @@ -17,6 +17,46 @@ > > DEFINE_BPF_STORAGE_CACHE(sk_cache); > > +DEFINE_STATIC_KEY_DEFERRED_FALSE(bpf_sk_storage_needed, HZ); > + > +/* A hint. A race costs a cache miss. */ > +struct sk_storage_xmit { > + const struct sock *sk; /* The last socket that has sent a packet */ > + u16 seen; /* Entries of cache[] looked up for such sockets */ > + u16 mask; /* "seen" of the previous round */ > + u8 round; > +}; > + > +static DEFINE_PER_CPU(struct sk_storage_xmit, sk_storage_xmit); > + > +void __bpf_sk_storage_prefetch_values(const struct sock *sk) > +{ > + struct sk_storage_xmit *xmit = raw_cpu_ptr(&sk_storage_xmit); > + struct bpf_local_storage *storage; > + unsigned long mask; > + void *sdata; > + int i; > + > + xmit->sk = sk; > + if (!++xmit->round) { > + xmit->mask = xmit->seen; > + xmit->seen = 0; > + } > + mask = xmit->mask; > + if (!mask) > + return; > + > + guard(rcu)(); > + storage = rcu_dereference(sk->sk_bpf_storage); > + if (!storage) > + return; > + for_each_set_bit(i, &mask, BPF_LOCAL_STORAGE_CACHE_SIZE) { > + sdata = rcu_access_pointer(storage->cache[i]); > + if (sdata) > + prefetch(sdata); > + } > +} > + > static struct bpf_local_storage_data * > bpf_sk_storage_lookup(struct sock *sk, struct bpf_map *map, bool cacheit_lockit) > { > @@ -29,6 +69,8 @@ bpf_sk_storage_lookup(struct sock *sk, struct bpf_map *map, bool cacheit_lockit) > return NULL; > > smap = (struct bpf_local_storage_map *)map; > + if (raw_cpu_read(sk_storage_xmit.sk) == sk) > + raw_cpu_or(sk_storage_xmit.seen, BIT(smap->cache_idx)); > return bpf_local_storage_lookup(sk_storage, smap, cacheit_lockit); > } > > @@ -64,11 +106,16 @@ void bpf_sk_storage_free(struct sock *sk) > static void bpf_sk_storage_map_free(struct bpf_map *map) > { > bpf_local_storage_map_free(map, &sk_cache); > + static_branch_slow_dec_deferred(&bpf_sk_storage_needed); > } > > static struct bpf_map *bpf_sk_storage_map_alloc(union bpf_attr *attr) > { > - return bpf_local_storage_map_alloc(attr, &sk_cache); > + struct bpf_map *map = bpf_local_storage_map_alloc(attr, &sk_cache); > + > + if (!IS_ERR(map)) > + static_branch_deferred_inc(&bpf_sk_storage_needed); > + return map; > } > > static int notsupp_get_next_key(struct bpf_map *map, void *key, > diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c > index 80de376a9d2e..f8c03b6d4fb0 100644 > --- a/net/ipv4/tcp.c > +++ b/net/ipv4/tcp.c > @@ -273,6 +273,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -1463,6 +1464,7 @@ int tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) > { > int ret; > > + bpf_sk_storage_prefetch_cache(sk); > lock_sock(sk); > ret = tcp_sendmsg_locked(sk, msg, size); > release_sock(sk); > diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c > index cf3ddc055d48..53dcbe563568 100644 > --- a/net/ipv4/tcp_output.c > +++ b/net/ipv4/tcp_output.c > @@ -38,6 +38,7 @@ > #define pr_fmt(fmt) "TCP: " fmt > > #include > +#include > #include > #include > #include > @@ -1605,6 +1606,7 @@ static int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, > int err; > > BUG_ON(!skb || !tcp_skb_pcount(skb)); > + bpf_sk_storage_prefetch_cache(sk); > tp = tcp_sk(sk); > prior_wstamp = tp->tcp_wstamp_ns; > tp->tcp_wstamp_ns = max(tp->tcp_wstamp_ns, tp->tcp_clock_cache); > @@ -1769,6 +1771,7 @@ static int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, > > tcp_add_tx_delay(skb, tp); > > + bpf_sk_storage_prefetch_values(sk); > err = INDIRECT_CALL_INET(icsk->icsk_af_ops->queue_xmit, > inet6_csk_xmit, ip_queue_xmit, > sk, skb, &inet->cork.fl);