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