Skip to contentSkip to conversation

Two XSKMAP improvements

PATCH Superseded Björn Töpel 3 participants 6 messages May 4, 2019 – May 6, 2019

Subsystems (4)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover Two XSKMAP improvements

This series add two improvements for the XSKMAP, used by AF_XDP sockets.

1. Automatic cleanup when an AF_XDP socket goes out of scope. Instead of manually cleaning out the "released" state socket from the map, this is done automatically.

2. The XSKMAP did not honor the BPF_EXIST/BPF_NOEXIST flag on insert.

Thanks,
Björn

Björn Töpel (2):
xsk: remove AF_XDP socket from map when the socket is released
xsk: honor BPF_EXIST and BPF_NOEXIST flags in XSKMAP

 include/net/xdp_sock.h |   3 +
 kernel/bpf/xskmap.c    | 121 +++++++++++++++++++++++++++++++++++------
 net/xdp/xsk.c          |  25 +++++++++
 3 files changed, 133 insertions(+), 16 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH 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 rings. This results in that all applications need to explicitly clear the map entry from the old "zombie state" socket. This should be done automatically.

After this patch, when a socket is released, it will remove itself from all the XSKMAPs it resides in, allowing the socket application to remove the code that cleans the XSKMAP entry.

This behavior is also closer to that of SOCKMAP, making the two socket maps more consistent.

---
 include/net/xdp_sock.h |   3 ++
 kernel/bpf/xskmap.c    | 106 +++++++++++++++++++++++++++++++++++------
 net/xdp/xsk.c          |  25 ++++++++++
 3 files changed, 120 insertions(+), 14 deletions(-)
include/net/xdp_sock.h +3 -0
index d074b6d60f8a..b5f8f9f826d0 100644
@@ -68,6 +68,8 @@ struct xdp_sock {
 	 */
 	spinlock_t tx_completion_lock;
 	u64 rx_dropped;
+	struct list_head map_list;
+	spinlock_t map_list_lock;
 };
 
 struct xdp_buff;
@@ -87,6 +89,7 @@ struct xdp_umem_fq_reuse *xsk_reuseq_swap(struct xdp_umem *umem,
 					  struct xdp_umem_fq_reuse *newq);
 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_delete_from_node(struct xdp_sock *xs, struct list_head *node);
 
 static inline char *xdp_umem_get_data(struct xdp_umem *umem, u64 addr)
 {
kernel/bpf/xskmap.c +92 -14

164 lines not on this page · raw message

net/xdp/xsk.c +25 -0
index a14e8864e4fa..1931d98a7754 100644
@@ -335,6 +335,27 @@ static int xsk_init_queue(u32 entries, struct xsk_queue **queue,
 	return 0;
 }
 
+static struct list_head *xsk_map_list_pop(struct xdp_sock *xs)
+{
+	struct list_head *node = NULL;
+
+	spin_lock_bh(&xs->map_list_lock);
+	if (!list_empty(&xs->map_list)) {
+		node = xs->map_list.next;
+		list_del(node);
+	}
+	spin_unlock_bh(&xs->map_list_lock);
+	return node;
+}
+
+static void xsk_delete_from_maps(struct xdp_sock *xs)
+{
+	struct list_head *node;
+
+	while ((node = xsk_map_list_pop(xs)))
+		xsk_map_delete_from_node(xs, node);
+}
+
 static int xsk_release(struct socket *sock)
 {
 	struct sock *sk = sock->sk;
@@ -354,6 +375,7 @@ static int xsk_release(struct socket *sock)
 	sock_prot_inuse_add(net, sk->sk_prot, -1);
 	local_bh_enable();
 
+	xsk_delete_from_maps(xs);
 	if (xs->dev) {
 		struct net_device *dev = xs->dev;
 
@@ -767,6 +789,9 @@ static int xsk_create(struct net *net, struct socket *sock, int protocol,
 	mutex_init(&xs->mutex);
 	spin_lock_init(&xs->tx_completion_lock);
 
+	INIT_LIST_HEAD(&xs->map_list);
+	spin_lock_init(&xs->map_list_lock);
+
 	mutex_lock(&net->xdp.lock);
 	sk_add_node_rcu(sk, &net->xdp.list);
 	mutex_unlock(&net->xdp.lock);
Signature · 1 line
2.20.1
maintainer
#2 lore raw

Re: [PATCH bpf-next 1/2] xsk: remove AF_XDP socket from map when the socket is released

Quoted 19 lines · Björn Töpel

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 rings. This results in that all applications need to explicitly clear the map entry from the old "zombie state" socket. This should be done automatically.

After this patch, when a socket is released, it will remove itself from all the XSKMAPs it resides in, allowing the socket application to remove the code that cleans the XSKMAP entry.

This behavior is also closer to that of SOCKMAP, making the two socket maps more consistent.

[...]

Quoted 26 lines
kernel/bpf/xskmap.c +15 -1
+static void __xsk_map_delete_elem(struct xsk_map *map,
+				  struct xdp_sock **map_entry)
+{
+	struct xdp_sock *old_xs;
+
+	spin_lock_bh(&map->lock);
+	old_xs = xchg(map_entry, NULL);
+	if (old_xs)
+		xsk_map_del_node(old_xs, map_entry);
+	spin_unlock_bh(&map->lock);
+
+}
+
 static void xsk_map_free(struct bpf_map *map)
 {
 	struct xsk_map *m = container_of(map, struct xsk_map, map);
@@ -78,15 +142,16 @@ static void xsk_map_free(struct bpf_map *map)
 	bpf_clear_redirect_map(map);
 	synchronize_net();
 
+	spin_lock_bh(&m->lock);
 	for (i = 0; i < map->max_entries; i++) {
+		struct xdp_sock **entry = &m->xsk_map[i];
 		struct xdp_sock *xs;
 
-		xs = m->xsk_map[i];

kernel/bpf/xskmap.c static void xsk_map_free(struct bpf_map *map) · line 155

Quoted 10 lines
kernel/bpf/xskmap.c +4 -4
-		if (!xs)
-			continue;
-
-		sock_put((struct sock *)xs);
+		xs = xchg(entry, NULL);
+		if (xs)
+			__xsk_map_delete_elem(m, entry);
 	}
+	spin_unlock_bh(&m->lock);
 

Was this tested? Doesn't the above straight run into a deadlock?

From xsk_map_free() you iterate over the map with m->lock held. Once you xchg'ed the entry and call into __xsk_map_delete_elem(), you attempt to call map->lock on the same map once again. What am I missing?

Thanks,
Daniel

maintainer
#3 lore raw

Re: [PATCH bpf-next 1/2] xsk: remove AF_XDP socket from map when the socket is released

Quoted 62 lines · Daniel Borkmann
Quoted 19 lines · Björn Töpel

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 rings. This results in that all applications need to explicitly clear the map entry from the old "zombie state" socket. This should be done automatically.

After this patch, when a socket is released, it will remove itself from all the XSKMAPs it resides in, allowing the socket application to remove the code that cleans the XSKMAP entry.

This behavior is also closer to that of SOCKMAP, making the two socket maps more consistent.

[...]

Quoted 26 lines
kernel/bpf/xskmap.c +15 -1
+static void __xsk_map_delete_elem(struct xsk_map *map,
+				  struct xdp_sock **map_entry)
+{
+	struct xdp_sock *old_xs;
+
+	spin_lock_bh(&map->lock);
+	old_xs = xchg(map_entry, NULL);
+	if (old_xs)
+		xsk_map_del_node(old_xs, map_entry);
+	spin_unlock_bh(&map->lock);
+
+}
+
 static void xsk_map_free(struct bpf_map *map)
 {
 	struct xsk_map *m = container_of(map, struct xsk_map, map);
@@ -78,15 +142,16 @@ static void xsk_map_free(struct bpf_map *map)
 	bpf_clear_redirect_map(map);
 	synchronize_net();
 
+	spin_lock_bh(&m->lock);
 	for (i = 0; i < map->max_entries; i++) {
+		struct xdp_sock **entry = &m->xsk_map[i];
 		struct xdp_sock *xs;
 
-		xs = m->xsk_map[i];

kernel/bpf/xskmap.c static void xsk_map_free(struct bpf_map *map) · line 155

Quoted 10 lines
kernel/bpf/xskmap.c +4 -4
-		if (!xs)
-			continue;
-
-		sock_put((struct sock *)xs);
+		xs = xchg(entry, NULL);
+		if (xs)
+			__xsk_map_delete_elem(m, entry);
 	}
+	spin_unlock_bh(&m->lock);
 

Was this tested? Doesn't the above straight run into a deadlock?

Quoted 3 lines

From xsk_map_free() you iterate over the map with m->lock held. Once you xchg'ed the entry and call into __xsk_map_delete_elem(), you attempt to call map->lock on the same map once again. What am I missing?

(It also does the xchg() twice so we'd leak the xs since it's NULL in the
second one.)

Quoted 2 lines

Thanks,
Daniel

#4 lore raw

Re: [PATCH bpf-next 1/2] xsk: remove AF_XDP socket from map when the socket is released

Quoted 63 lines · Daniel Borkmann
Quoted 62 lines · Daniel Borkmann
Quoted 19 lines · Björn Töpel

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 rings. This results in that all applications need to explicitly clear the map entry from the old "zombie state" socket. This should be done automatically.

After this patch, when a socket is released, it will remove itself from all the XSKMAPs it resides in, allowing the socket application to remove the code that cleans the XSKMAP entry.

This behavior is also closer to that of SOCKMAP, making the two socket maps more consistent.

[...]

Quoted 26 lines
kernel/bpf/xskmap.c +15 -1
+static void __xsk_map_delete_elem(struct xsk_map *map,
+				  struct xdp_sock **map_entry)
+{
+	struct xdp_sock *old_xs;
+
+	spin_lock_bh(&map->lock);
+	old_xs = xchg(map_entry, NULL);
+	if (old_xs)
+		xsk_map_del_node(old_xs, map_entry);
+	spin_unlock_bh(&map->lock);
+
+}
+
  static void xsk_map_free(struct bpf_map *map)
  {
  	struct xsk_map *m = container_of(map, struct xsk_map, map);
@@ -78,15 +142,16 @@ static void xsk_map_free(struct bpf_map *map)
  	bpf_clear_redirect_map(map);
  	synchronize_net();
 
+	spin_lock_bh(&m->lock);
  	for (i = 0; i < map->max_entries; i++) {
+		struct xdp_sock **entry = &m->xsk_map[i];
  		struct xdp_sock *xs;
 
-		xs = m->xsk_map[i];

kernel/bpf/xskmap.c static void xsk_map_free(struct bpf_map *map) · line 155

Quoted 10 lines
kernel/bpf/xskmap.c +4 -4
-		if (!xs)
-			continue;
-
-		sock_put((struct sock *)xs);
+		xs = xchg(entry, NULL);
+		if (xs)
+			__xsk_map_delete_elem(m, entry);
  	}
+	spin_unlock_bh(&m->lock);
 

Was this tested? Doesn't the above straight run into a deadlock?

Quoted 7 lines
Quoted 3 lines

From xsk_map_free() you iterate over the map with m->lock held. Once you
xchg'ed the entry and call into __xsk_map_delete_elem(), you attempt to call map->lock on the same map once again. What am I missing?

(It also does the xchg() twice so we'd leak the xs since it's NULL in the
second one.)

No, you're not missing anything. Just plain old sloppiness from my side. Apologies for the wasted time.

Björn

Quoted 2 lines
Quoted 2 lines

Thanks,
Daniel

#5 lore raw

PATCH 2/2 xsk: honor BPF_EXIST and BPF_NOEXIST flags in XSKMAP

In reply to Björn Töpel

From: 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 | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
kernel/bpf/xskmap.c +14 -3
index ad15e8e92a87..4214ea7b8cfc 100644
@@ -236,8 +236,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)
@@ -263,15 +261,28 @@ static int xsk_map_update_elem(struct bpf_map *map, void *key, void *value,
 
 	spin_lock_bh(&m->lock);
 	entry = &m->xsk_map[i];
+	old_xs = *entry;
+	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_node_init(node, m, entry);
 	xsk_map_add_node(xs, node);
-	old_xs = xchg(entry, xs);
 	if (old_xs)
 		xsk_map_del_node(old_xs, entry);
 	spin_unlock_bh(&m->lock);
 
 	sockfd_put(sock);
 	return 0;
+
+out:
+	spin_unlock_bh(&m->lock);
+	sockfd_put(sock);
+	xsk_map_node_free(node);
+	return err;
 }
 
 static int xsk_map_delete_elem(struct bpf_map *map, void *key)
Signature · 1 line
2.20.1