Skip to contentSkip to conversation

net: Add a warning if NAPI cb missed xdp_do_flush().

PATCH Superseded Sebastian Andrzej Siewior 8 participants 13 messages Sep 29, 2023 – Oct 17, 2023

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: Add a warning if NAPI cb missed xdp_do_flush().

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 creats a warning if one of the functions had a non-empty list. Hide everything behind CONFIG_DEBUG_NET.

---

This is follow-up to

	https://lore.kernel.org/all/[email protected]

It has been compile tested.

 include/linux/bpf.h    | 16 ++++++++++++++++
 include/linux/filter.h |  8 ++++++++
 include/net/xdp_sock.h | 10 ++++++++++
 kernel/bpf/cpumap.c    | 10 ++++++++++
 kernel/bpf/devmap.c    | 10 ++++++++++
 net/core/dev.c         |  2 ++
 net/core/filter.c      | 14 ++++++++++++++
 net/xdp/xsk.c          | 10 ++++++++++
 8 files changed, 80 insertions(+)
include/linux/bpf.h +16 -0
index 30063a760b5af..a4eb8f23d35e6 100644
@@ -2730,6 +2730,22 @@ static inline void bpf_dynptr_set_rdonly(struct bpf_dynptr_kern *ptr)
 }
 #endif /* CONFIG_BPF_SYSCALL */
 
+#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_DEBUG_NET)
+bool __dev_check_flush(void);
+bool __cpu_map_check_flush(void);
+
+#else
+static inline bool __dev_check_flush(void)
+{
+	return false;
+}
+
+static inline bool __cpu_map_check_flush(void)
+{
+	return false;
+}
+#endif
+
 static __always_inline int
 bpf_probe_read_kernel_common(void *dst, u32 size, const void *unsafe_ptr)
 {
include/linux/filter.h +8 -0
index 27406aee2d402..db095d731813e 100644
@@ -1025,6 +1025,14 @@ int xdp_do_redirect_frame(struct net_device *dev,
 			  struct bpf_prog *prog);
 void xdp_do_flush(void);
 
+#ifdef CONFIG_DEBUG_NET
+void xdp_do_check_flushed(struct napi_struct *napi);
+
+#else
+static inline void xdp_do_check_flushed(struct napi_struct *napi) { }
+
+#endif
+
 /* The xdp_do_flush_map() helper has been renamed to drop the _map suffix, as
  * it is no longer only flushing maps. Keep this define for compatibility
  * until all drivers are updated - do not use xdp_do_flush_map() in new code!
include/net/xdp_sock.h +10 -0
index 69b472604b86f..c250b78712771 100644
@@ -109,4 +109,14 @@ 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 */
kernel/bpf/cpumap.c +10 -0
index e42a1bdb7f536..2cded02d83815 100644
@@ -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;
kernel/bpf/devmap.c +10 -0
index 4d42f6ed6c11a..8619ac1b879ed 100644
@@ -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.
net/core/dev.c +2 -0
index 606a366cc2095..9273b12ecf6fa 100644
@@ -6526,6 +6526,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))
net/core/filter.c +14 -0
index a094694899c99..9841d0e32cb94 100644
@@ -4207,6 +4207,20 @@ void xdp_do_flush(void)
 }
 EXPORT_SYMBOL_GPL(xdp_do_flush);
 
+#ifdef CONFIG_DEBUG_NET
+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;
net/xdp/xsk.c +10 -0
index 7482d0aca5046..4eae53478db07 100644
@@ -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);
Signature · 1 line
2.42.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Sebastian Andrzej Siewior

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 creats a warning if one of the functions had a non-empty list. Hide everything behind CONFIG_DEBUG_NET.

Quoted 8 lines

---

This is follow-up to

	https://lore.kernel.org/all/[email protected]

It has been compile tested.

Both with and without CONFIG_DEBUG_NET enabled, I trust? ;)

Anyway, LGTM!

maintainer
#2 lore raw
In reply to Sebastian Andrzej Siewior
Quoted 5 lines · On Fri, 29 Sep 2023 18:58:25 +0200 Sebastian Andrzej Siewior wrote:
include/linux/bpf.h +5 -0
+#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_DEBUG_NET)
+bool __dev_check_flush(void);
+bool __cpu_map_check_flush(void);
+
+#else

include/linux/bpf.h static inline void bpf_dynptr_set_rdonly(struct bpf_dynptr_kern *ptr) · line 2747

Quoted 10 lines
include/linux/bpf.h +10 -0
+static inline bool __dev_check_flush(void)
+{
+	return false;
+}
+
+static inline bool __cpu_map_check_flush(void)
+{
+	return false;
+}
+#endif

I think you're going too hard with the ifdefs.
These functions are only called if DEBUG_NET, add if BPF on the call site and spare us the static inlines for all the __ helpers.

Quoted 8 lines

static __always_inline int
bpf_probe_read_kernel_common(void *dst, u32 size, const void *unsafe_ptr) {

include/linux/filter.h +0 -0
index 27406aee2d402..db095d731813e 100644

include/linux/filter.h int xdp_do_redirect_frame(struct net_device *dev, · line 1034

Quoted 10 lines
include/linux/filter.h +7 -0
@@ -1025,6 +1025,14 @@ int xdp_do_redirect_frame(struct net_device *dev,
 			  struct bpf_prog *prog);
 void xdp_do_flush(void);
 
+#ifdef CONFIG_DEBUG_NET
+void xdp_do_check_flushed(struct napi_struct *napi);
+
+#else
+static inline void xdp_do_check_flushed(struct napi_struct *napi) { }
+
+#endif

Can you move this to net/core/dev.h? Or a new header under net/core if you prefer? This looks internal to the stack.
Also nit: drop the empty lines inside the #ifdef?

#3 lore raw

PATCH v2 1/1 net: Add a warning if NAPI cb missed xdp_do_flush().

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 creats a warning if one of the functions had a non-empty list. Hide everything behind CONFIG_DEBUG_NET.

---
v1…v2:
- Moved xdp_do_check_flushed() to net/core/dev.h.
- Stripped __ from function names.
- Removed empty lines within an ifdef block.
- xdp_do_check_flushed() is now behind CONFIG_DEBUG_NET && CONFIG_BPF_SYSCALL. dev_check_flush and cpu_map_check_flush are now only behind CONFIG_DEBUG_NET. They have no empty inline function for the !CONFIG_DEBUG_NET case since they are only called in CONFIG_DEBUG_NET case.

 include/linux/bpf.h    |  3 +++
 include/net/xdp_sock.h |  9 +++++++++
 kernel/bpf/cpumap.c    | 10 ++++++++++
 kernel/bpf/devmap.c    | 10 ++++++++++
 net/core/dev.c         |  2 ++
 net/core/dev.h         |  6 ++++++
 net/core/filter.c      | 14 ++++++++++++++
 net/xdp/xsk.c          | 10 ++++++++++
 8 files changed, 64 insertions(+)
include/linux/bpf.h +3 -0
index a34ac7f00c86c..584adabd411fc 100644
@@ -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)
 {
include/net/xdp_sock.h +9 -0
index 69b472604b86f..7dd0df2f6f8e6 100644
@@ -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 */
kernel/bpf/cpumap.c +10 -0
index e42a1bdb7f536..8a0bb80fe48a3 100644
@@ -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;
kernel/bpf/devmap.c +10 -0
index 4d42f6ed6c11a..a936c704d4e77 100644
@@ -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.
net/core/dev.c +2 -0
index 606a366cc2095..9273b12ecf6fa 100644
@@ -6526,6 +6526,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))
net/core/dev.h +6 -0
index e075e198092cc..f66125857af77 100644
@@ -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
net/core/filter.c +14 -0
index a094694899c99..5d5921c0ab4a3 100644
@@ -4207,6 +4207,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;
net/xdp/xsk.c +10 -0
index f5e96e0d6e01d..ba070fd37d244 100644
@@ -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);
Signature · 1 line
2.42.0
#4 lore raw
kernel test robot noticed the following build warnings:

Hi Sebastian,

kernel test robot noticed the following build warnings:

[auto build test WARNING on bpf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Sebastian-Andrzej-Siewior/net-Add-a-warning-if-NAPI-cb-missed-xdp_do_flush/20231006-235117
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20231006154933.mQgxQHHt%40linutronix.de
patch subject: [PATCH bpf-next v2] net: Add a warning if NAPI cb missed xdp_do_flush().
config: m68k-allyesconfig (https://download.01.org/0day-ci/archive/20231007/[email protected]/config)
compiler: m68k-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231007/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> net/core/filter.c:4211:6: warning: no previous prototype for 'xdp_do_check_flushed' [-Wmissing-prototypes]

4211 | void xdp_do_check_flushed(struct napi_struct *napi)
| ^~~~~~~~~~~~~~~~~~~~

vim +/xdp_do_check_flushed +4211 net/core/filter.c

4209
4210 #if defined(CONFIG_DEBUG_NET) && defined(CONFIG_BPF_SYSCALL)

> 4211	void xdp_do_check_flushed(struct napi_struct *napi)

4212 {
4213 bool ret;
4214
4215 ret = dev_check_flush();
4216 ret |= cpu_map_check_flush();
4217 ret |= xsk_map_check_flush();
4218
4219 WARN_ONCE(ret, "Missing xdp_do_flush() invocation after NAPI by %ps\n",
4220 napi->poll);
4221 }
4222 #endif
4223

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
maintainer Acked-by on v2 1/1
#5 lore raw
In reply to Sebastian Andrzej Siewior
Quoted 8 lines · On Fri, 6 Oct 2023 17:49:33 +0200 Sebastian Andrzej Siewior wrote:

- Moved xdp_do_check_flushed() to net/core/dev.h.
- Stripped __ from function names.
- Removed empty lines within an ifdef block.
- xdp_do_check_flushed() is now behind CONFIG_DEBUG_NET && CONFIG_BPF_SYSCALL. dev_check_flush and cpu_map_check_flush are now only behind CONFIG_DEBUG_NET. They have no empty inline function for the !CONFIG_DEBUG_NET case since they are only called in CONFIG_DEBUG_NET case.

Other than the minor nit from the build bot - LGTM now thanks!

#6 lore raw

PATCH v3 1/1 net: Add a warning if NAPI cb missed xdp_do_flush().

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 creats a warning if one of the functions had a non-empty list. Hide everything behind CONFIG_DEBUG_NET.

---
v2…v3:
- Collected Reviewed/Acked from the list.
- Added an include dev.h to filter.c, the robot pointed out a missing prototype.

v1…v2:
- Moved xdp_do_check_flushed() to net/core/dev.h.
- Stripped __ from function names.
- Removed empty lines within an ifdef block.
- xdp_do_check_flushed() is now behind CONFIG_DEBUG_NET && CONFIG_BPF_SYSCALL. dev_check_flush and cpu_map_check_flush are now only behind CONFIG_DEBUG_NET. They have no empty inline function for the !CONFIG_DEBUG_NET case since they are only called in CONFIG_DEBUG_NET case.

 include/linux/bpf.h    |  3 +++
 include/net/xdp_sock.h |  9 +++++++++
 kernel/bpf/cpumap.c    | 10 ++++++++++
 kernel/bpf/devmap.c    | 10 ++++++++++
 net/core/dev.c         |  2 ++
 net/core/dev.h         |  6 ++++++
 net/core/filter.c      | 16 ++++++++++++++++
 net/xdp/xsk.c          | 10 ++++++++++
 8 files changed, 66 insertions(+)
include/linux/bpf.h +3 -0
index a34ac7f00c86c..584adabd411fc 100644
@@ -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)
 {
include/net/xdp_sock.h +9 -0
index 69b472604b86f..7dd0df2f6f8e6 100644
@@ -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 */
kernel/bpf/cpumap.c +10 -0
index e42a1bdb7f536..8a0bb80fe48a3 100644
@@ -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;
kernel/bpf/devmap.c +10 -0
index 4d42f6ed6c11a..a936c704d4e77 100644
@@ -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.
net/core/dev.c +2 -0
index 606a366cc2095..9273b12ecf6fa 100644
@@ -6526,6 +6526,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))
net/core/dev.h +6 -0
index e075e198092cc..f66125857af77 100644
@@ -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
net/core/filter.c +16 -0
index a094694899c99..af2d34d5e1815 100644
@@ -82,6 +82,8 @@
 #include <net/mptcp.h>
 #include <net/netfilter/nf_conntrack_bpf.h>
 
+#include "dev.h"
+
 static const struct bpf_func_proto *
 bpf_sk_base_func_proto(enum bpf_func_id func_id);
 
@@ -4207,6 +4209,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;
net/xdp/xsk.c +10 -0
index f5e96e0d6e01d..ba070fd37d244 100644
@@ -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);
Signature · 1 line
2.42.0
#7 lore raw

PATCH 1/1 net: Add a warning if NAPI cb missed xdp_do_flush().

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.

---
v3…v4:
- s/creats/creates as per Simon Horman.

v2…v3:
- Collected Reviewed/Acked from the list.
- Added an include dev.h to filter.c, the robot pointed out a missing prototype.

v1…v2:
- Moved xdp_do_check_flushed() to net/core/dev.h.
- Stripped __ from function names.
- Removed empty lines within an ifdef block.
- xdp_do_check_flushed() is now behind CONFIG_DEBUG_NET && CONFIG_BPF_SYSCALL. dev_check_flush and cpu_map_check_flush are now only behind CONFIG_DEBUG_NET. They have no empty inline function for the !CONFIG_DEBUG_NET case since they are only called in CONFIG_DEBUG_NET case.

 include/linux/bpf.h    |  3 +++
 include/net/xdp_sock.h |  9 +++++++++
 kernel/bpf/cpumap.c    | 10 ++++++++++
 kernel/bpf/devmap.c    | 10 ++++++++++
 net/core/dev.c         |  2 ++
 net/core/dev.h         |  6 ++++++
 net/core/filter.c      | 16 ++++++++++++++++
 net/xdp/xsk.c          | 10 ++++++++++
 8 files changed, 66 insertions(+)
include/linux/bpf.h +3 -0
index a34ac7f00c86c..584adabd411fc 100644
@@ -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)
 {
include/net/xdp_sock.h +9 -0
index 69b472604b86f..7dd0df2f6f8e6 100644
@@ -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 */
kernel/bpf/cpumap.c +10 -0
index e42a1bdb7f536..8a0bb80fe48a3 100644
@@ -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;
kernel/bpf/devmap.c +10 -0
index 4d42f6ed6c11a..a936c704d4e77 100644
@@ -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.
net/core/dev.c +2 -0
index 606a366cc2095..9273b12ecf6fa 100644
@@ -6526,6 +6526,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))
net/core/dev.h +6 -0
index e075e198092cc..f66125857af77 100644
@@ -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
net/core/filter.c +16 -0
index a094694899c99..af2d34d5e1815 100644
@@ -82,6 +82,8 @@
 #include <net/mptcp.h>
 #include <net/netfilter/nf_conntrack_bpf.h>
 
+#include "dev.h"
+
 static const struct bpf_func_proto *
 bpf_sk_base_func_proto(enum bpf_func_id func_id);
 
@@ -4207,6 +4209,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;
net/xdp/xsk.c +10 -0
index f5e96e0d6e01d..ba070fd37d244 100644
@@ -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);
Signature · 1 line
2.42.0
maintainer Acked-by on v1 1/1
#8 lore raw
Quoted 9 lines · Sebastian Andrzej Siewior

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.

Quoted 5 lines

---

LGTM.

maintainer
#9 lore raw

Hi Sebastian,

Quoted 10 lines · John Fastabend
Quoted 9 lines · Sebastian Andrzej Siewior

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.

Quoted 8 lines
Quoted 4 lines

LGTM.

Do you have a chance to send a v5 rebase? It does not apply to bpf-next.

Other than that, the patch lgtm.

Thanks,
Daniel

#10 lore raw

PATCH 1/1 net: Add a warning if NAPI cb missed xdp_do_flush().

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.

---
v4…v5:
- rebase on top of bpf-next
- Collected Reviewed/Acked from the list.

v3…v4:
- s/creats/creates as per Simon Horman.

v2…v3:
- Collected Reviewed/Acked from the list.
- Added an include dev.h to filter.c, the robot pointed out a missing prototype.

v1…v2:
- Moved xdp_do_check_flushed() to net/core/dev.h.
- Stripped __ from function names.
- Removed empty lines within an ifdef block.
- xdp_do_check_flushed() is now behind CONFIG_DEBUG_NET && CONFIG_BPF_SYSCALL. dev_check_flush and cpu_map_check_flush are now only behind CONFIG_DEBUG_NET. They have no empty inline function for the !CONFIG_DEBUG_NET case since they are only called in CONFIG_DEBUG_NET case.

 include/linux/bpf.h    |  3 +++
 include/net/xdp_sock.h |  9 +++++++++
 kernel/bpf/cpumap.c    | 10 ++++++++++
 kernel/bpf/devmap.c    | 10 ++++++++++
 net/core/dev.c         |  2 ++
 net/core/dev.h         |  6 ++++++
 net/core/filter.c      | 16 ++++++++++++++++
 net/xdp/xsk.c          | 10 ++++++++++
 8 files changed, 66 insertions(+)
include/linux/bpf.h +3 -0
index f0891ba24cb1c..92b76360ff58a 100644
@@ -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)
 {
include/net/xdp_sock.h +9 -0
index 69b472604b86f..7dd0df2f6f8e6 100644
@@ -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 */
kernel/bpf/cpumap.c +10 -0
index e42a1bdb7f536..8a0bb80fe48a3 100644
@@ -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;
kernel/bpf/devmap.c +10 -0
index 4d42f6ed6c11a..a936c704d4e77 100644
@@ -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.
net/core/dev.c +2 -0
index 606a366cc2095..9273b12ecf6fa 100644
@@ -6526,6 +6526,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))
net/core/dev.h +6 -0
index e075e198092cc..f66125857af77 100644
@@ -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
net/core/filter.c +16 -0
index cc2e4babc85fb..21d75108c2e94 100644
@@ -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;
net/xdp/xsk.c +10 -0
index 7482d0aca5046..e23689b82914f 100644
@@ -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);
Signature · 1 line
2.42.0
#11 lore raw
This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Quoted 10 lines · On Mon, 16 Oct 2023 14:57:38 +0200 you wrote:

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.

[...]

Here is the summary with links:
- [bpf-next,-v5] net: Add a warning if NAPI cb missed xdp_do_flush().

    https://git.kernel.org/bpf/bpf-next/c/9a675ba55a96

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
reviewer
#12 lore raw
In reply to Sebastian Andrzej Siewior
Quoted 7 lines · Sebastian Andrzej Siewior

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 creats a warning if one of the functions had a non-empty list.

nit: creates

Quoted 4 lines

Hide everything behind CONFIG_DEBUG_NET.

...