From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.175 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796419541; b=DabPJVPCkDLTppaz1H+HdAkC0LIU+ZDQ+xtz1Ys2CS1G8MCt0k/RbvAhALI/bwVa2vac AnEk7LS+YiWQmmWPx2sG2Xci0Qn6nZi6rEs6SuIq8VX8z5peCgDdXCbgxJ9kC1AadLfh9 N6TgKzLFH0gVZpOLeSyitYQteZpApmb86sknxN7crD1l1eUmvbTrkSJRprpCpsErRwZhB D/PN3PuD8+nuJFLBXfHpkkyCAmQvNJqHywdCKzcBe/2OWPFqFcx3u+1onNWWtT2+kObvh wrE3p9+1AwHsjXdlPFq7lXqjiIkrosksL0XK/4zTJh7FMe5ncvtI04Z/iExTIAAO6RQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796419541; 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:References: Content-Language:From:In-Reply-To:Content-Type: Content-Transfer-Encoding; bh=Mzas63d1YoaZej9lcKHdef3nMO1FWtCgwBk3KpEmTgU=; b=rf8uHntSExXrZ3VW9Fw7ra1Fjkr9faGCAqIOYJxEACReA3Ju4/p30Cww791XKWHBtPHh O7D517jYFin1p4nB09vgntENNQBXWeCIi0mJtv+A/4owxsW6L7rMZDWXlj1unS8hrW9zV CvVWipEzbiRXhBVbrDLZsoe9hzao0ijXh0khkcUT52gd7mY4C5VXaQNZlLgfzVRl2Ohhl oIgg5vi71V5jiv+4kLpRUug50vNggAu1xD47JSI7tUVl4KE3SNAVDIiPmtxSf9ctV/xJc E8WJ/tZDFlSIdIPU+qXGdV0lilYoOXF/5jODSlNKfW/qYwLUQ+QffjgK8QZjWkkZgQw== 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=db6qzpfW; arc=none smtp.remote-ip=74.125.82.175 Received: from mail-dy1-f175.google.com (mail-dy1-f175.google.com [74.125.82.175]) (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 31CEB606A322 for ; Fri, 4 Dec 2026 21:25:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 31CEB606A322 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=db6qzpfW Received: by mail-dy1-f175.google.com with SMTP id 5a478bee46e88-3282db206d3so666595eec.0 for ; Fri, 04 Dec 2026 13:25:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796419540; x=1797024340; darn=vger.kernel.org; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references:to:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to:content-type; bh=Mzas63d1YoaZej9lcKHdef3nMO1FWtCgwBk3KpEmTgU=; b=db6qzpfWDkTCtWQVht+5ii2eOllXNhtG+4ofgM0+9Qk1XG6ISO6CVZqDuTRwwdIkPU vFsQfAVuW61jq4Vpwp1ySuOCKSEiEKSd7VTwEoN0YWdquEfyoklfYcrE+EhG7VBjxFzz NzmhPVIHl6fKE07/4PleUbAav7S1Gc85bSVBHaZxHLlqJ666jor5OjuaYug0AOkAWqOF wgBUHbv9wGI4IzR9k4/BEVFJfkRNoDnejlokGhRbbUqozw2KDDLv0GdO3QB6I43g4ROe t/sI6T5HeUMvu05vYipg6v97X8L9KD/SOnxbibaDaXzfGL21HFf/fCie0bOrhMmh6mrR 8YbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796419540; x=1797024340; h=content-transfer-encoding:content-type:in-reply-to:from :content-language:references: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=Mzas63d1YoaZej9lcKHdef3nMO1FWtCgwBk3KpEmTgU=; b=ThlIYij3TREDYHNQNp+xFXWGalTLtvjBVBBIo6huSXb5v4iLugYKZHlBN6IAnXiCcv 29NK1OQckYFmaxftOs4s9gltYPMFCyaDK6mXxloekaQuLgnVtTrRiOi+4hxm249B2ozS wt427qofutL9ID5H5szT4aprvEbjBYtxD2b7gxtEU8hlLx/hB8jXTp4JAnAzmy3OEmRy 5z8lUlLufzdc+Hqvng3tRNWndfWP6PQNaNpOiUffaS3avrRbjKu/Yba0V86TPFKdal8s v9Ohz+OBqSAM1vVAePNZZ9JDWSPJURIZ4ttdbLMdPByRFZgkBllkv2CDc5wEm5GPMOXN vaOw== X-Forwarded-Encrypted: i=1; AL+hErFdzxsbbuFp8Hx/zJvIswNGZEmgTS8adz/Xf3dJG500BhqFzeVsjg7vg6fVFStq37NNXVk=@vger.kernel.org X-Gm-Message-State: APuU4Z9fO7fGDrx5L5c4xx1dg3Uxra3isTDj+hAKjtJ3OoDpp4rJZUZa asLRw36KuOJvS1XzP04+cTTa6Ocp1jKTMz8k4Ivk+0q8lnJ+qTudaqL/ X-Gm-Gg: AZFdTZe0cRGEKtjWw3AbIveAIIHkk6jPzylin7tdzjKgjXpYIUnGCWrqoToZ8poNUWz CXZ6vnXFA5thdupTar1ES9ePTZZn06SrvgmZEMu4XiAKjrX8ujm/HfJ4+eV7cYVOSNDrsGpmhLQ M5j8J48ecFFYd4FZQeLrLWAFndVj3811oR3/1ErWYAhi55asQtX36oEHm9p6JSaH6eePpPtZwJd x2YcO80JtH4vkC6kSKa6k+eAl+Zu7MjDDdYvPgDO4pn6KXtalBInsBnovh41GH1sY8R3YCiR0K7 f1hJgekc3o/IDwZTwc/IpNxvpA+pYcTIkqJppvmSvAHGQTZLo8KqdCOrcnGNMZIOHpDZWybEzG7 SdLpitomEhqMn17An2ZVB7uEMwYUbPnA7ctMhGNMsD9K2ew/RWyYbxrBla4yN03TvLmQRjWPjWx +zf+rvPg/g4+le01HGKCYF+GECL2hfxht99O4+hOdAQlmrX+m6vj7kRwAccSTxQNNTdCLN7tHwu NMdrtnf/AZVgkxib9jtM6ksnK9/dRGhI7Rivb1yhaldTlxnPrcftbIAnP6j7nW/0Esi3SkRhyym qJT0zgoDtdAwNcGtZIX1Z8HQzMbHzsLrdWsI26zFDdD53ojDlyYQQ+kEB3flkYJvQA== X-Received: by 2002:a05:7300:7c02:b0:387:4a97:57a7 with SMTP id 5a478bee46e88-3875214224dmr4221909eec.22.1796419540069; Fri, 04 Dec 2026 13:25:40 -0800 (PST) Received: from ?IPV6:2a03:83e0:115c:1:15e7:543c:f32a:fb90? ([2620:10d:c090:500::6:cdc]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-3875218bc9fsm10317976eec.6.2026.12.04.13.25.39 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 04 Dec 2026 13:25:39 -0800 (PST) Message-ID: <9daeb69e-43c5-4fad-8ad3-5eb5a9d81ddd@gmail.com> Date: Fri, 4 Dec 2026 21:25:38 +0000 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 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH bpf-next 3/5] bpf: tcp: Prefetch sk storage cache[] before TCP transmit To: sashiko-reviews@lists.linux.dev, bpf@vger.kernel.org References: <20261204-sk_storage_prefetch-v1-0-ea52fb48b52b@meta.com> <20261204-sk_storage_prefetch-v1-3-ea52fb48b52b@meta.com> Content-Language: en-US From: Mykyta Yatsenko In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit 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 > > 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. >