net: xdp: XSKMAP improvements
Subsystems (4)
v5 cover net: xdp: XSKMAP improvements
This series (v5 and counting) add two improvements for the XSKMAP, used by AF_XDP sockets.
1. Automatic cleanup when an AF_XDP socket goes out of scope/is released. Instead of require that the user manually clears the "released" state socket from the map, this is done automatically. Each socket tracks which maps it resides in, and remove itself from those maps at relase. A notable implementation change, is that the sockets references the map, instead of the map referencing the sockets. Which implies that when the XSKMAP is freed, it is by definition cleared of sockets.
2. The XSKMAP did not honor the BPF_EXIST/BPF_NOEXIST flag on insert, which this patch addresses.
Thanks,
Björn
v1->v2: Fixed deadlock and broken cleanup. (Daniel) v2->v3: Rebased onto bpf-nextv3->v4: {READ, WRITE}_ONCE consistency. (Daniel)
Socket release/map update race. (Daniel)v4->v5: Avoid use-after-free on XSKMAP self-assignment [1]. (Daniel)
Removed redundant assignment in xsk_map_update_elem(). Variable name consistency; Use map_entry everywhere.
[1] https://lore.kernel.org/bpf/[email protected]/T/#mc68439e97bc07fa301dad9fc4850ed5aa392f385
Björn Töpel (2):
xsk: remove AF_XDP socket from map when the socket is released
xsk: support BPF_EXIST and BPF_NOEXIST flags in XSKMAP
include/net/xdp_sock.h | 18 ++++++ kernel/bpf/xskmap.c | 133 ++++++++++++++++++++++++++++++++++------- net/xdp/xsk.c | 50 ++++++++++++++++ 3 files changed, 179 insertions(+), 22 deletions(-)
Signature · 1 line
2.20.1
PATCH v5 1/2 xsk: remove AF_XDP socket from map when the socket is released
From: Björn Töpel <[email protected]>
When an AF_XDP socket is released/closed the XSKMAP still holds a reference to the socket in a "released" state. The socket will still use the netdev queue resource, and block newly created sockets from attaching to that queue, but no user application can access the fill/complete/rx/tx queues. This results in that all applications need to explicitly clear the map entry from the old "zombie state" socket. This should be done automatically.
In this patch, the sockets tracks, and have a reference to, which maps it resides in. When the socket is released, it will remove itself from all maps.
--- include/net/xdp_sock.h | 18 ++++++ kernel/bpf/xskmap.c | 125 ++++++++++++++++++++++++++++++++++------- net/xdp/xsk.c | 50 +++++++++++++++++ 3 files changed, 173 insertions(+), 20 deletions(-)
include/net/xdp_sock.h +18 -0
@@ -50,6 +50,16 @@ struct xdp_umem {struct list_head xsk_list; }; +/* Nodes are linked in the struct xdp_sock map_list field, and used to +* track which maps a certain socket reside in. +*/ +struct xsk_map ; +struct xsk_map_node { +struct list_head node; +struct xsk_map *map; +struct xdp_sock **map_entry; +}; +struct xdp_sock {/* struct sock must be the first member of struct xdp_sock */ struct sock sk;
@@ -75,6 +85,9 @@ struct xdp_sock {/* Protects generic receive. */ spinlock_t rx_lock;u64 rx_dropped; +struct list_head map_list; +/* Protects map_list */ +spinlock_t map_list_lock; };struct xdp_buff ;
@@ -96,6 +109,11 @@ struct xdp_umem_fq_reuse *xsk_reuseq_swap(struct xdp_umem *umem,void xsk_reuseq_free (struct xdp_umem_fq_reuse *rq);struct xdp_umem *xdp_get_umem_from_qid (struct net_device *dev,u16 queue_id); +void xsk_map_try_sock_delete (struct xsk_map *map,struct xdp_sock *xs, +struct xdp_sock **map_entry); +int xsk_map_inc (struct xsk_map *map); +void xsk_map_put (struct xsk_map *map); +static inline char *xdp_umem_get_data (struct xdp_umem *umem,u64 addr) {return umem->pages[addr >>PAGE_SHIFT ].addr + (addr & (PAGE_SIZE -1 ));
kernel/bpf/xskmap.c +105 -20
180 lines not on this page · raw message
net/xdp/xsk.c +50 -0
68 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH v5 2/2 xsk: support BPF_EXIST and BPF_NOEXIST flags in XSKMAP
In reply to Björn TöpelFrom: Björn Töpel <[email protected]>
The XSKMAP did not honor the BPF_EXIST/BPF_NOEXIST flags when updating an entry. This patch addresses that.
--- kernel/bpf/xskmap.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/bpf/xskmap.c +6 -2
@@ -226,8 +226,6 @@ static int xsk_map_update_elem(struct bpf_map *map, void *key, void *value,return -EINVAL ;if (unlikely (i >= m->map.max_entries))return -E2BIG ; -if (unlikely (map_flags ==BPF_NOEXIST )) -return -EEXIST ; sock =sockfd_lookup (fd, &err);if (!sock)
@@ -257,6 +255,12 @@ static int xsk_map_update_elem(struct bpf_map *map, void *key, void *value,if (old_xs == xs) { err =0 ; goto out; + }else if (old_xs && map_flags ==BPF_NOEXIST ) { + err = -EEXIST ; + goto out; + }else if (!old_xs && map_flags ==BPF_EXIST ) { + err = -ENOENT ; + goto out; }xsk_map_sock_add (xs, node);WRITE_ONCE (*map_entry, xs);
Signature · 1 line
2.20.1
Daniel Borkmann
maintainerQuoted 29 lines · Björn Töpel
This series (v5 and counting) add two improvements for the XSKMAP, used by
AF_XDPsockets.1. Automatic cleanup when an
AF_XDPsocket goes out of scope/is released. Instead of require that the user manually clears the "released" state socket from the map, this is done automatically. Each socket tracks which maps it resides in, and remove itself from those maps at relase. A notable implementation change, is that the sockets references the map, instead of the map referencing the sockets. Which implies that when the XSKMAP is freed, it is by definition cleared of sockets.2. The XSKMAP did not honor the BPF_EXIST/BPF_NOEXIST flag on insert, which this patch addresses.
Thanks,
Björn
v1->v2: Fixed deadlock and broken cleanup. (Daniel)v2->v3: Rebased onto bpf-nextv3->v4: {READ, WRITE}_ONCE consistency. (Daniel)
Socket release/map update race. (Daniel)v4->v5: Avoid use-after-free on XSKMAP self-assignment [1]. (Daniel)
Removed redundant assignment inxsk_map_update_elem(). Variable name consistency; Usemap_entryeverywhere.[1] https://lore.kernel.org/bpf/[email protected]/T/#mc68439e97bc07fa301dad9fc4850ed5aa392f385
Quoted 9 lines
Björn Töpel (2):
xsk: removeAF_XDPsocket from map when the socket is released
xsk: supportBPF_EXISTandBPF_NOEXISTflags in XSKMAPinclude/net/xdp_sock.h | 18 ++++++ kernel/bpf/xskmap.c | 133 ++++++++++++++++++++++++++++++++++------- net/xdp/xsk.c | 50 ++++++++++++++++ 3 files changed, 179 insertions(+), 22 deletions(-)
Looks better, applied thanks!