]> www.infradead.org Git - users/willy/xarray.git/commitdiff
net, bpf: Add a warning if NAPI cb missed xdp_do_flush().
authorSebastian Andrzej Siewior <bigeasy@linutronix.de>
Mon, 16 Oct 2023 12:57:38 +0000 (14:57 +0200)
committerDaniel Borkmann <daniel@iogearbox.net>
Tue, 17 Oct 2023 13:02:03 +0000 (15:02 +0200)
A few drivers were missing a xdp_do_flush() invocation after
XDP_REDIRECT.

Add three helper functions each for one of the per-CPU lists. Return
true if the per-CPU list is non-empty and flush the list.

Add xdp_do_check_flushed() which invokes each helper functions and
creates a warning if one of the functions had a non-empty list.

Hide everything behind CONFIG_DEBUG_NET.

Suggested-by: Jesper Dangaard Brouer <hawk@kernel.org>
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Reviewed-by: Toke Høiland-Jørgensen <toke@redhat.com>
Acked-by: Jakub Kicinski <kuba@kernel.org>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20231016125738.Yt79p1uF@linutronix.de
include/linux/bpf.h
include/net/xdp_sock.h
kernel/bpf/cpumap.c
kernel/bpf/devmap.c
net/core/dev.c
net/core/dev.h
net/core/filter.c
net/xdp/xsk.c

index d3c51a5075086d42fe4344b7582cf9c7dfa85869..b4b40b45962b211b001206f2cf4add362c6bd13e 100644 (file)
@@ -2478,6 +2478,9 @@ void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
                     enum bpf_dynptr_type type, u32 offset, u32 size);
 void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr);
 void bpf_dynptr_set_rdonly(struct bpf_dynptr_kern *ptr);
+
+bool dev_check_flush(void);
+bool cpu_map_check_flush(void);
 #else /* !CONFIG_BPF_SYSCALL */
 static inline struct bpf_prog *bpf_prog_get(u32 ufd)
 {
index 69b472604b86f5a8dd678a2327449f72e98ece0b..7dd0df2f6f8e6c47c38dba0d45eed355ff550f16 100644 (file)
@@ -109,4 +109,13 @@ static inline void __xsk_map_flush(void)
 
 #endif /* CONFIG_XDP_SOCKETS */
 
+#if defined(CONFIG_XDP_SOCKETS) && defined(CONFIG_DEBUG_NET)
+bool xsk_map_check_flush(void);
+#else
+static inline bool xsk_map_check_flush(void)
+{
+       return false;
+}
+#endif
+
 #endif /* _LINUX_XDP_SOCK_H */
index e42a1bdb7f53650a569efed29b2e63dc8a1b6dbb..8a0bb80fe48a344964e4029fec5e895ee512babf 100644 (file)
@@ -764,6 +764,16 @@ void __cpu_map_flush(void)
        }
 }
 
+#ifdef CONFIG_DEBUG_NET
+bool cpu_map_check_flush(void)
+{
+       if (list_empty(this_cpu_ptr(&cpu_map_flush_list)))
+               return false;
+       __cpu_map_flush();
+       return true;
+}
+#endif
+
 static int __init cpu_map_init(void)
 {
        int cpu;
index 4d42f6ed6c11ae36f1d2b47b3a5d010529f09145..a936c704d4e773c213563e5cdae1091eb3bde879 100644 (file)
@@ -418,6 +418,16 @@ void __dev_flush(void)
        }
 }
 
+#ifdef CONFIG_DEBUG_NET
+bool dev_check_flush(void)
+{
+       if (list_empty(this_cpu_ptr(&dev_flush_list)))
+               return false;
+       __dev_flush();
+       return true;
+}
+#endif
+
 /* Elements are kept alive by RCU; either by rcu_read_lock() (from syscall) or
  * by local_bh_disable() (from XDP calls inside NAPI). The
  * rcu_read_lock_bh_held() below makes lockdep accept both.
index 97e7b9833db9ffb003c142ed2b1c53a47e39f0e1..4420831180c6536c67d0cb5b5e3291bbc4ab3c22 100644 (file)
@@ -6535,6 +6535,8 @@ static int __napi_poll(struct napi_struct *n, bool *repoll)
        if (test_bit(NAPI_STATE_SCHED, &n->state)) {
                work = n->poll(n, weight);
                trace_napi_poll(n, work, weight);
+
+               xdp_do_check_flushed(n);
        }
 
        if (unlikely(work > weight))
index e075e198092cc18eef4d673dcf9950c1f0ad6426..f66125857af773ffe43a1093d31cd6f95ab55ba6 100644 (file)
@@ -136,4 +136,10 @@ static inline void netif_set_gro_ipv4_max_size(struct net_device *dev,
 }
 
 int rps_cpumask_housekeeping(struct cpumask *mask);
+
+#if defined(CONFIG_DEBUG_NET) && defined(CONFIG_BPF_SYSCALL)
+void xdp_do_check_flushed(struct napi_struct *napi);
+#else
+static inline void xdp_do_check_flushed(struct napi_struct *napi) { }
+#endif
 #endif
index cc2e4babc85fbecf9ae3cf670b3d547e2feaa6dc..21d75108c2e94be490af74672a4845c4cb945ce8 100644 (file)
@@ -83,6 +83,8 @@
 #include <net/netfilter/nf_conntrack_bpf.h>
 #include <linux/un.h>
 
+#include "dev.h"
+
 static const struct bpf_func_proto *
 bpf_sk_base_func_proto(enum bpf_func_id func_id);
 
@@ -4208,6 +4210,20 @@ void xdp_do_flush(void)
 }
 EXPORT_SYMBOL_GPL(xdp_do_flush);
 
+#if defined(CONFIG_DEBUG_NET) && defined(CONFIG_BPF_SYSCALL)
+void xdp_do_check_flushed(struct napi_struct *napi)
+{
+       bool ret;
+
+       ret = dev_check_flush();
+       ret |= cpu_map_check_flush();
+       ret |= xsk_map_check_flush();
+
+       WARN_ONCE(ret, "Missing xdp_do_flush() invocation after NAPI by %ps\n",
+                 napi->poll);
+}
+#endif
+
 void bpf_clear_redirect_map(struct bpf_map *map)
 {
        struct bpf_redirect_info *ri;
index f5e96e0d6e01d4c0121201bc74f40e0785762b2c..ba070fd37d244569e9a537bdd473b3c5f1697c7a 100644 (file)
@@ -391,6 +391,16 @@ void __xsk_map_flush(void)
        }
 }
 
+#ifdef CONFIG_DEBUG_NET
+bool xsk_map_check_flush(void)
+{
+       if (list_empty(this_cpu_ptr(&xskmap_flush_list)))
+               return false;
+       __xsk_map_flush();
+       return true;
+}
+#endif
+
 void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries)
 {
        xskq_prod_submit_n(pool->cq, nb_entries);