Skip to content

Commit

Permalink
net, bpf: Add a warning if NAPI cb missed xdp_do_flush().
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Sebastian Andrzej Siewior <[email protected]>
Signed-off-by: Daniel Borkmann <[email protected]>
Reviewed-by: Toke Høiland-Jørgensen <[email protected]>
Acked-by: Jakub Kicinski <[email protected]>
Acked-by: John Fastabend <[email protected]>
Link: https://lore.kernel.org/bpf/[email protected]
  • Loading branch information
Sebastian Andrzej Siewior authored and borkmann committed Oct 17, 2023
1 parent 137df11 commit 9a675ba
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/linux/bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
9 changes: 9 additions & 0 deletions include/net/xdp_sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
10 changes: 10 additions & 0 deletions kernel/bpf/cpumap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions kernel/bpf/devmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions net/core/dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 6 additions & 0 deletions net/core/dev.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions net/core/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions net/xdp/xsk.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9a675ba

Please sign in to comment.