Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.169 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796414692; b=2UfqpvgDR19x3ZlU0p1tsQN9XM79LxAAg7Uo2TG4LtLiByV0/D5YwbNLgpK0o7XV82AS 7c4R3xi5lzOXdtLScydWm6zOqPGZU7vdHFiIxIJN7AWeks2LLdN92WNm6JABWyYWg5Rje PevBsL678d9bBpHdeQ3fJAehpa6F1vPz7UC3zFmhVYsKvijezbH5Jxd09H/a/tHzcxBue NCDJ36AR32e/2Pc6u8F9MjtZ7aHcHB2PN/okjn2nJ3vazRYdHkHa92sneU0va6zyiLqyZ EzxR7GcxXH+PSrHYuiqTxZBFHB5C+EB9sdFHSUHXTad6Eh/YOZohNFUF6tZR8Foilcg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796414692; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:Date:Subject: MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id: References:In-Reply-To:To:Cc:X-Mailer:X-Developer-Signature: X-Developer-Key; bh=cB5lMim4PDaTGbYhG6Yt7UbCkJa1D+pz5JjhSwLbVS4=; b=y2XRSGp8WPo6Fkt1W1I0Lv/NwxlAjCndDYibswsEJpBrbRpD61z8FywNTpz7Tq/Kw+XN k2JOdTjECTqLH/07cvKLN2GSLMs7aVImhwsZyQVl4BoEJzBrcjK3oTncXuNFlYMBvwYv4 A6BNb0TqNFNLH3bWtgtFjxNpIW3KsZY3d+c/8co4hc+KpOQFUQGG3H9DBvgXGUmPN8ig/ FrZEYpGsHiq2giZJ1XCWa4/usJnppHCV/i/Lo726oxK5kwjV3Kt24+tlLr3ZDyKMdXJWH aUuJzmzK9VjVHemOOg4wjeJ99HEw4jnvVLaNyqcxO95dvEbXX/TpqE9ZkzaJh6LmLag== 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=XUuupDfr; arc=none smtp.remote-ip=209.85.128.169 Received: from mail-yw1-f169.google.com (mail-yw1-f169.google.com [209.85.128.169]) (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 93789606A322 for ; Fri, 4 Dec 2026 20:04:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 93789606A322 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=XUuupDfr Received: by mail-yw1-f169.google.com with SMTP id 00721157ae682-8b4c685c751so11664077b3.1 for ; Fri, 04 Dec 2026 12:04:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796414691; x=1797019491; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=cB5lMim4PDaTGbYhG6Yt7UbCkJa1D+pz5JjhSwLbVS4=; b=XUuupDfrO+pJP3YsWPIaHp7nfZBDhuYglzPHzC0Z6uLXDbS/WiRydb81r+kCNgXTJB 1A/wejaPzIcGOVeoMIwk4W9BT+JuiK5jQrrmrnFrRxu0yKjBB1chIi0bE1VIlwSDLyo0 RF5Fr3eTGrd/M1DJ3FPtIUsRhW8DvrUYFq8xMCe4rofD80VKI2fLKCVxE9ex2yHIKat0 Nu0odSik1ocfkqTPzIO3HZN/FYRYietimY7dhSA2XdT4TMkid6KzY1HzWkrPcYp2eqHK Ww/eiGeSnLKrIr2JcToclr2aaN8rnto7QfB6z2nrBNOIbKxFPe5O3nYdxymcJggsit3Q B1LA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796414691; x=1797019491; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=cB5lMim4PDaTGbYhG6Yt7UbCkJa1D+pz5JjhSwLbVS4=; b=kjjLCPcMDyTFD3kpESF/7kzmPozK5ehbHaUY0vQBrOAm5f0YGm4G7KFg+hRMxDZcDb 4iScxQKR25LjDEmq/96jvgYGauDB6lfkVtO7fwbUsvhNHSAMMKzzUw7wDip2u+81tM8U HoiaiNXGuOyl5JGtH6q4IkAN8vH4dtMXsGmQxoEOIc1nry0r4pKMcpPRWCwfqh0wvo3L 4ICFhnvVdDLv/UJTu6f8FY6AvonRcPMZw8NxBkxEyhWft21y972lTQjvLdFvYUvUBIcb 1vS02k8V9EOs4Jz36MLWQp6Tlxd19XXi4M83JCawN18pl89mxO+BjE1MPJXqm4PabDIm e6ew== X-Gm-Message-State: AB4q+uHnAvugMf1L7v/48VH30IkEzLgrY9XAoaAD/Xem8/mB3npq0ex1 dM47EkyTuMJt1AlX2c+6XWrQDzDmiQLqs2FtBHyfGV4PFy+tMqtZ1Pg/ X-Gm-Gg: AZFdTZdssv+ncCfuafmuVgp/nzQcsLx7rlwSt5XSvs+mRd6bsASZohLF08o8DWUKHRU e6s8UMOEBxUVI/kWEn7KBoE04Tr9O6/55XmBE3DKzvIgY4bbRur8zzh+tOnILPnBnikrxa9eRq3 PbTZYJkzWTAFS6Q+9UDveI4Hkg5NaQyw4E6cOYMdIvfKwJu+3gIRszNM3QDlosqjaMCpUeADjGp 6yKAGLon2FIhBe/Grf4LvQiFqx/Rs6WdmiR/i0++0In/YFpek6RK0qhge0EiywObl/Vh82Rrl9n sNf8/yRdIuRIHcBpvET/lYilMG/4q41/1HVZZXqUQOhPjvQYOUOkHeUgCOL3aDBiQN3NVoHQV3p xtq785tSmFOzDK+BmwMz/o1ZWRqEqoDCPaBqlauWl84qahqY7e1A/qsBqCqsB1jg68GVOE3PDEV tLXBB0KI2n9LL1upaaq1DFmvtHQ71/VTTWccwS+A5AEqlJui9+NnIvh903LrTCxkKBb2pT0AgDl xh96mRu6Du6GgSrR+VajsjDwX/A6JvWuZFSLc5Qs+M8gt44d66D/EW3BJIDbynsg519EOwB3Zsr 140q4mNkmX0R8LFFeHci X-Received: by 2002:a05:690c:6b83:b0:8b4:b5e4:e76d with SMTP id 00721157ae682-8b660d3e769mr12432137b3.59.1796414690730; Fri, 04 Dec 2026 12:04:50 -0800 (PST) Received: from localhost ([2a03:2880:30ff:73::]) by smtp.gmail.com with ESMTPSA id 00721157ae682-8b66086625dsm14307427b3.14.2026.12.04.12.04.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 12:04:50 -0800 (PST) From: Mykyta Yatsenko Date: Fri, 04 Dec 2026 12:03:55 -0800 Subject: [PATCH bpf-next 4/5] bpf: tcp: Prefetch values of hot sk storage maps before transmit X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261204-sk_storage_prefetch-v1-4-ea52fb48b52b@meta.com> References: <20261204-sk_storage_prefetch-v1-0-ea52fb48b52b@meta.com> In-Reply-To: <20261204-sk_storage_prefetch-v1-0-ea52fb48b52b@meta.com> To: bpf@vger.kernel.org, ast@kernel.org, andrii@kernel.org, daniel@iogearbox.net, kernel-team@meta.com, eddyz87@gmail.com, memxor@gmail.com Cc: Mykyta Yatsenko X-Mailer: b4 0.16-dev X-Developer-Signature: v=1; a=ed25519-sha256; t=1796414679; l=6515; i=yatsenko@meta.com; s=20260324; h=from:subject:message-id; bh=NeEo7J3O3kVJsmUXn1bruE7Ngnoyal+Oxd9Iu43Uvbc=; b=w9DhtZAhZV95frbrTedhb1/2c4l6ck0jVXZ3hFbe3QenZNrA3nimwhdpnUsw1JQqC0FvaVuqn /CTYXIy0XW5C/7eD+3Q84Z218rl4aM3m9FJjy+qxKJh6RXKkYvRO8eQ X-Developer-Key: i=yatsenko@meta.com; a=ed25519; pk=1zCUBXUa66KmzfjNsG8YNlMj2ckPdqBPvFq2ww3/YaA= From: Mykyta Yatsenko After cache[], the lookup still waits for the element. Programs look up the same few maps on most packets, so learn these maps on each CPU and prefetch their elements just before ->queue_xmit(). Each bpf_sk_storage_get() marks the cache[] slot of its map on the CPU, and the hook prefetches the elements of the marked slots. The marks are cleared every 256 transmits of the CPU, so that a slot stops being prefetched when programs stop using it. As in the prototype, a u8 counter that wraps counts the transmits. While no slot is marked on a CPU, the cache[] prefetch is off too. The lookups of all programs mark slots, for all sockets. The hint is only a prefetch: a map that egress programs do not use costs one prefetch on each transmit. Maps that share a cache[] slot share one mark. The hook is inline and the per-CPU state is in the header, so that a transmit with no marked slot makes no call. Suggested-by: Alexei Starovoitov Link: https://lore.kernel.org/bpf/DN8A36W906WG.1BTUYRC2FIIR@gmail.com/ Signed-off-by: Mykyta Yatsenko --- include/net/bpf_sk_storage.h | 29 ++++++++++++++++++++++++++++- net/core/bpf_sk_storage.c | 38 ++++++++++++++++++++++++++++++++++++++ net/ipv4/tcp_output.c | 1 + 3 files changed, 67 insertions(+), 1 deletion(-) 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 @@ -14,6 +14,7 @@ #include #include #include +#include #include struct sock; @@ -33,6 +34,16 @@ struct nlattr; #ifdef CONFIG_BPF_SYSCALL DECLARE_STATIC_KEY_FALSE(bpf_sk_storage_xmit_key); +/* Per CPU hint of the TCP transmit hooks, see net/core/bpf_sk_storage.c */ +struct bpf_sk_storage_xmit { + u16 seen; /* Slots of cache[] looked up in this round */ + u8 round; /* Transmits in this round, wraps at 256 */ +}; + +DECLARE_PER_CPU_ALIGNED(struct bpf_sk_storage_xmit, bpf_sk_storage_xmit); + +void bpf_sk_storage_prefetch_hot(const struct sock *sk, unsigned long hot); + /* * 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 @@ -43,7 +54,8 @@ static inline void bpf_sk_storage_prefetch_cache(const struct sock *sk) { const void *storage; - if (!static_branch_unlikely(&bpf_sk_storage_xmit_key)) + if (!static_branch_unlikely(&bpf_sk_storage_xmit_key) || + !this_cpu_read(bpf_sk_storage_xmit.seen)) return; storage = rcu_access_pointer(sk->sk_bpf_storage); if (!storage) @@ -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); +} + 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); @@ -64,6 +90,7 @@ int bpf_sk_storage_diag_put(struct bpf_sk_storage_diag *diag, 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 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) static DECLARE_DELAYED_WORK(sk_storage_key_work, bpf_sk_storage_key_fn); +/* + * Each bpf_sk_storage_get() marks the slot of its map in cache[] on this CPU, + * and the transmit hooks prefetch the storage and the values of the marked + * slots. The marks are cleared every 256 transmits on this CPU, so that a slot + * stops being prefetched when programs stop using it. + * + * Programs, softirqs and the transmit path on a CPU can race, and a task can + * move to another CPU in a hook. Thus a prefetch can be lost or wasted for one + * round. + */ +DEFINE_PER_CPU_ALIGNED(struct bpf_sk_storage_xmit, bpf_sk_storage_xmit); + +/* 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); + } +} + +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)); +} + static struct bpf_local_storage_data * bpf_sk_storage_lookup(struct sock *sk, struct bpf_map *map, bool cacheit_lockit) { @@ -270,6 +307,7 @@ BPF_CALL_4(bpf_sk_storage_get, struct bpf_map *, map, struct sock *, sk, if (!sk || !sk_fullsock(sk) || flags > BPF_SK_STORAGE_GET_F_CREATE) return (unsigned long)NULL; + bpf_sk_storage_xmit_mark(map); sdata = bpf_sk_storage_lookup(sk, map, true); if (sdata) return (unsigned long)sdata->data; diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c index 2881d924cf376..53dcbe5635684 100644 --- a/net/ipv4/tcp_output.c +++ b/net/ipv4/tcp_output.c @@ -1771,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); -- 2.53.1-Meta