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