Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.175 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796414689; b=xcnjfnYwukjRYCnl1wL0Saj+nb6OGIA65jXLNxH+MlosS7A2IP2uL8Ph9OX0zxn6ytPG UgaBIwcmEB033VKKLJK+oPYF5BRkVxyYEqkPs6MhGm4NmuAh6iQ/1Ir02rS5vRiZfMAYB DzaPE0N60sF9ZCfyYCQprecPejKyc3ZzySO5bMZOcGg4IOhWfGsQtPRiDu1oT/qniaaGQ Ry6xcK4hjPJbTkRqgIOy55A2n5E5B/ONLgMtG4NJt9CSrJrXkfMZJbhAzUM8w7/XsVNP6 zcAe3cvIfFTPJapv9YVvrnzQkNjfqwcyFMnBaEwUMCVSGAK7x9HrsohfEmkX8oCzKsA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796414689; 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=NHJ8UFnNe9980pe/DTr+QRm0DiWPb2GcxBixhuHVKzo=; b=i1uqraPTa7lYrMHYPixkVnTPOhg+ziJTGzC52oj2WHSm0QQFAyRNbyv4kUpd+LQJmaSs GgJ2Zi+AX4+qkK0qoApFOLb+Bfr/BynldWgekoj3HBbPWuDa/mNp4PBk/5YVrszSEy38u ULcV1Rq2t3EdjQgTDI77sF1zHDvQBT4cTM2tpnqZZmzL07u1TmjFORKPV4BiK/cqHerjr BahNqnRqXJM4ddRiNF+H/cwwyjdGbW3SaMX+hmB3LoD8d3wZkhOlQPSJmMcq6d/dFnS7O h1uCHjibqk+MIlX6nYhkaeopQGHXKdlsURexzISEMYl69HHwLOgZws13RiFcpUeb4pQ== 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=MnieEDyv; arc=none smtp.remote-ip=209.85.128.175 Received: from mail-yw1-f175.google.com (mail-yw1-f175.google.com [209.85.128.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 2E30B606A322 for ; Fri, 4 Dec 2026 20:04:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2E30B606A322 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=MnieEDyv Received: by mail-yw1-f175.google.com with SMTP id 00721157ae682-8b5981c4834so12779117b3.2 for ; Fri, 04 Dec 2026 12:04:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796414688; x=1797019488; 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=NHJ8UFnNe9980pe/DTr+QRm0DiWPb2GcxBixhuHVKzo=; b=MnieEDyvhxuY/op6SupwPVhHld9dGaMSxqtgwFGQlkk6ZxgJS1dMYkvf784x4pRF3F UbQLRGvk9HMREL2YixhzzjhuhJw3zclOPFbLgUB7u0+WgjtDad1Qb9UAoVw3Y06dIxmW lbYYXoaPHYCv9oKUUrnG0LAueawQhPhCJPyC1NLwNeJF153mbVX3yQu8YQ/4rLzfqkbA QLLF94HowIQco2ar5UTbD0UB3zhJRrMZIE0v5a9b7FgOeQt0aLt2vqSdOb3fWAmOmfS8 OXI29K2Z8TFH0Q50rr1k5RCIQi9G5dNmfmptB3KelX7jsQZwGQrymyhI9ZCblc+FTrqO H2VQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796414688; x=1797019488; 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=NHJ8UFnNe9980pe/DTr+QRm0DiWPb2GcxBixhuHVKzo=; b=Jl3VWQJkJidv6PM83OepJNLXD/ov93KEA9V058VWJ5d+Qg87zwn1rcg44+pPYMKLp4 bfRsiU/6xVzFJeZX7GNrlB8ICtnrslcfEnE2AOpNdRq9dnBUuXnC7Hud6brFqTl+/mIa FwsAL99OuI/m64bYJTzPHrLvYL4i9KEeglRbpYMK94TDJZ3O8OBJFTwfbPSXIV/2Ct4Y 2Xskds3v/vjvGxuWQGdnWSxJ9IACJzst3TbcQHDrKuVkM3U3pA0pACJGj1Iv0JoS3JZH dVna4KZdiGOExiNMohepatsFyGYUh9x057DcwG58tz3678NLDn/potpJ1sisRJQhCW8k IFig== X-Gm-Message-State: AB4q+uFJ/4mxPitvYQjgDgpqgdEg9AYN6+ePUp/6Jmsu9fi3KD8gT6C5 Zkra7qRyi1WcVxz2DRaV6EVLG5K2Bnd+Uupy3IWDhYTQ2OQkO9IAIxYVmouJ0a+fU/Q= X-Gm-Gg: AZFdTZfsT/C6ziE0w/6QZFcscI3aNauuoThbIqQgTRgCyY9Cu8Nges5RGUO5vLarRJu tBn8x5m9bc4hUptzIpsPXpqvtGNVH87C9hPimAwq+n4WXcOG8WmIQrsm/XJJEr0gUlugwzYdi1a 4iopyt1YlK4Sb3HwuHWHOxFBt3jTg3cdbXvhGruHm7i/NUUCHod4iy8iOUNzm/japl4PBrkZCyz 1CgjRy9qMx+Zby8Ws5WDkqhZbR0zcgR+lVl70rQVNUnr4DTIK97JlsDgXJljR7yE5VD9ffpRMpo GJn09sup9eaA5aZjXf8pv6moXdq1+I0GJR/BrIUKre5WH+zDY56dqWkRjNm+6aofmouzDGbYzVd YbkZxQQnPrWkn7GEe2zVVYUa5AJCSNtjhUj2hSoTq21rrjCTpVkjGjBrFcv63IrgMIVcVQxxdm/ 8LgHyHsGJDsP1hGfgPapmvIwc7JCcMtuTIQaoAVYwRa4TuyrCbKt7QXKXtJ5AkTYW2CjLAM8ZDa TuB+AygZIjAkcTh9ftezycM89/Ixn9y+DKLPzbrlkg4ER39av3CGE9Lvw3A/Qa6/Tsu3z8R9R1H 6E9ny66WO8rHuBHV5ErL59RO8i9+mhNcmBC0GO5/ X-Received: by 2002:a05:690c:29b:b0:8b5:61d:f3ff with SMTP id 00721157ae682-8b660d8ba18mr18009617b3.46.1796414688089; Fri, 04 Dec 2026 12:04:48 -0800 (PST) Received: from localhost ([2a03:2880:30ff:4::]) by smtp.gmail.com with ESMTPSA id 00721157ae682-8b660d9a917sm14399597b3.29.2026.12.04.12.04.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 12:04:47 -0800 (PST) From: Mykyta Yatsenko Date: Fri, 04 Dec 2026 12:03:54 -0800 Subject: [PATCH bpf-next 3/5] bpf: tcp: Prefetch sk storage cache[] before TCP 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-3-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=6488; i=yatsenko@meta.com; s=20260324; h=from:subject:message-id; bh=x+iQmGilIBTlX15+YGVrxBPBatTQ4vxbOStyZBu/Bo4=; b=yBLO0Ki76f6h4YinoJRibr5gCCFN2Rcsggtp22BobLa1uSPzJpAappMsbpZA85UaXHc4c87GF OEFhq4vzFjiBSc5ONfCVFAw6LeiNzftB7GcDiKDlanTfNdiqfLJd317 X-Developer-Key: i=yatsenko@meta.com; a=ed25519; pk=1zCUBXUa66KmzfjNsG8YNlMj2ckPdqBPvFq2ww3/YaA= From: Mykyta Yatsenko An egress program that looks up the sk storage of a cold socket waits for two cache misses in a row. The first miss is on cache[] of the storage, and the second miss is on the element. TCP knows the socket early, so prefetch cache[] when sendmsg starts and when a transmit starts. The hooks are on while an sk storage map exists. Until the next patch, they prefetch on each transmit of each socket that has sk storage. Map create can run with migration disabled, for example from a light skeleton loader, and map free always runs with migration disabled. A change of a static key takes cpus_read_lock(), which can then deadlock with a CPU offline. Thus a work item sets the key from a count of the maps. On x86-64, clang emits no instruction for prefetch(), because the kernel builds with -mno-sse. Thus the hooks use prefetchw(), which is inline asm there. Suggested-by: Alexei Starovoitov Link: https://lore.kernel.org/bpf/DN8A36W906WG.1BTUYRC2FIIR@gmail.com/ Link: https://github.com/llvm/llvm-project/pull/248194 Signed-off-by: Mykyta Yatsenko --- include/net/bpf_sk_storage.h | 26 ++++++++++++++++++++++++++ net/core/bpf_sk_storage.c | 31 ++++++++++++++++++++++++++++++- net/ipv4/tcp.c | 2 ++ net/ipv4/tcp_output.c | 2 ++ 4 files changed, 60 insertions(+), 1 deletion(-) 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 @@ -13,6 +13,8 @@ #include #include #include +#include +#include struct sock; @@ -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; + /* 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); +} + 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 +63,7 @@ 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 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 67288177492ac..875fff681869e 100644 --- a/net/core/bpf_sk_storage.c +++ b/net/core/bpf_sk_storage.c @@ -17,6 +17,28 @@ DEFINE_BPF_STORAGE_CACHE(sk_cache); +DEFINE_STATIC_KEY_FALSE(bpf_sk_storage_xmit_key); + +/* + * The TCP transmit hooks are on while an sk storage map exists. Map create can + * run with migration disabled (a light skeleton loader is a BPF syscall + * program), and map free always runs with migration disabled. A change of a + * static key takes cpus_read_lock(), which can then deadlock with a CPU + * offline. So only a work item changes the key. The key gates a prefetch only, + * so it can lag behind the maps. + */ +static atomic_t sk_storage_maps; + +static void bpf_sk_storage_key_fn(struct work_struct *work) +{ + if (atomic_read(&sk_storage_maps)) + static_branch_enable(&bpf_sk_storage_xmit_key); + else + static_branch_disable(&bpf_sk_storage_xmit_key); +} + +static DECLARE_DELAYED_WORK(sk_storage_key_work, bpf_sk_storage_key_fn); + static struct bpf_local_storage_data * bpf_sk_storage_lookup(struct sock *sk, struct bpf_map *map, bool cacheit_lockit) { @@ -64,11 +86,18 @@ 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); + /* Later, so that maps that come and go patch less text */ + if (atomic_dec_and_test(&sk_storage_maps)) + mod_delayed_work(system_dfl_wq, &sk_storage_key_work, HZ); } 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) && atomic_inc_return(&sk_storage_maps) == 1) + mod_delayed_work(system_dfl_wq, &sk_storage_key_work, 0); + 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 80de376a9d2ef..b4b0826496791 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -273,6 +273,7 @@ #include #include #include +#include #include #include #include @@ -1133,6 +1134,7 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size) int zc = 0; long timeo; + bpf_sk_storage_prefetch_cache(sk); flags = msg->msg_flags; sockc = (struct sockcm_cookie){ .tsflags = READ_ONCE(sk->sk_tsflags) }; diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c index cf3ddc055d48d..2881d924cf376 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); -- 2.53.1-Meta