Skip to contentSkip to conversation

xdp: fix hang while unregistering device bound to xdp socket

PATCH Superseded Ilya Maximets 2 participants 5 messages Jun 26, 2019 – Jun 27, 2019 1 message not in the archive

Subsystems (4)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): CGME20190626181520eucas1p1817d31d958b8755600f0745e92edef5a@eucas1p1.samsung.com

#1 lore raw

v4 cover xdp: fix hang while unregistering device bound to xdp socket

Version 4:

* 'xdp_umem_clear_dev' exposed to be used while unregistering.
* Added XDP socket state to track if resources already unbinded.
* Splitted in two fixes.

Version 3:

* Declaration lines ordered from longest to shortest.
* Checking of event type moved to the top to avoid unnecessary locking.

Version 2:

* Completely re-implemented using netdev event handler.

Ilya Maximets (2):
xdp: hold device for umem regardless of zero-copy mode
xdp: fix hang while unregistering device bound to xdp socket

 include/net/xdp_sock.h |  5 +++
 net/xdp/xdp_umem.c     | 27 +++++++------
 net/xdp/xdp_umem.h     |  1 +
 net/xdp/xsk.c          | 88 ++++++++++++++++++++++++++++++++++++------
 4 files changed, 99 insertions(+), 22 deletions(-)
Signature · 1 line
2.17.1
#2 lore raw

PATCH v4 1/2 xdp: hold device for umem regardless of zero-copy mode

Device pointer stored in umem regardless of zero-copy mode, so we heed to hold the device in all cases.

Fixes c9b47cc1fabc ("xsk: fix bug when trying to use both copy and zero-copy on one queue id")
---
 net/xdp/xdp_umem.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
net/xdp/xdp_umem.c +6 -5
index 9c6de4f114f8..267b82a4cbcf 100644
@@ -105,6 +105,9 @@ int xdp_umem_assign_dev(struct xdp_umem *umem, struct net_device *dev,
 
 	umem->dev = dev;
 	umem->queue_id = queue_id;
+
+	dev_hold(dev);
+
 	if (force_copy)
 		/* For copy-mode, we are done. */
 		goto out_rtnl_unlock;
@@ -124,7 +127,6 @@ int xdp_umem_assign_dev(struct xdp_umem *umem, struct net_device *dev,
 		goto err_unreg_umem;
 	rtnl_unlock();
 
-	dev_hold(dev);
 	umem->zc = true;
 	return 0;
 
@@ -163,10 +165,9 @@ static void xdp_umem_clear_dev(struct xdp_umem *umem)
 	xdp_clear_umem_at_qid(umem->dev, umem->queue_id);
 	rtnl_unlock();
 
-	if (umem->zc) {
-		dev_put(umem->dev);
-		umem->zc = false;
-	}
+	dev_put(umem->dev);
+	umem->dev = NULL;
+	umem->zc = false;
 }
 
 static void xdp_umem_unpin_pages(struct xdp_umem *umem)
Signature · 1 line
2.17.1
#3 lore raw

PATCH v4 2/2 xdp: fix hang while unregistering device bound to xdp socket

In reply to Ilya Maximets

Device that bound to XDP socket will not have zero refcount until the userspace application will not close it. This leads to hang inside 'netdev_wait_allrefs()' if device unregistering requested:

  # ip link del p1
  < hang on recvmsg on netlink socket >
  # ps -x | grep ip
  5126  pts/0    D+   0:00 ip link del p1
  # journalctl -b
  Jun 05 07:19:16 kernel:
  unregister_netdevice: waiting for p1 to become free. Usage count = 1
  Jun 05 07:19:27 kernel:
  unregister_netdevice: waiting for p1 to become free. Usage count = 1
  ...

Fix that by implementing NETDEV_UNREGISTER event notification handler to properly clean up all the resources and unref device.

This should also allow socket killing via ss(8) utility.

Fixes 965a99098443 ("xsk: add support for bind for Rx")
---
 include/net/xdp_sock.h |  5 +++
 net/xdp/xdp_umem.c     | 16 +++++---
 net/xdp/xdp_umem.h     |  1 +
 net/xdp/xsk.c          | 88 ++++++++++++++++++++++++++++++++++++------
 4 files changed, 93 insertions(+), 17 deletions(-)
include/net/xdp_sock.h +5 -0
index d074b6d60f8a..82d153a637c7 100644
@@ -61,6 +61,11 @@ struct xdp_sock {
 	struct xsk_queue *tx ____cacheline_aligned_in_smp;
 	struct list_head list;
 	bool zc;
+	enum {
+		XSK_UNINITIALIZED = 0,
+		XSK_BINDED,
+		XSK_UNBINDED,
+	} state;
 	/* Protects multiple processes in the control path */
 	struct mutex mutex;
 	/* Mutual exclusion of NAPI TX thread and sendmsg error paths
net/xdp/xdp_umem.c +10 -6
index 267b82a4cbcf..56729e74cbea 100644
@@ -140,34 +140,38 @@ int xdp_umem_assign_dev(struct xdp_umem *umem, struct net_device *dev,
 	return err;
 }
 
-static void xdp_umem_clear_dev(struct xdp_umem *umem)
+void xdp_umem_clear_dev(struct xdp_umem *umem)
 {
+	bool lock = rtnl_is_locked();
 	struct netdev_bpf bpf;
 	int err;
 
+	if (!lock)
+		rtnl_lock();
+
 	if (!umem->dev)
-		return;
+		goto out_unlock;
 
 	if (umem->zc) {
 		bpf.command = XDP_SETUP_XSK_UMEM;
 		bpf.xsk.umem = NULL;
 		bpf.xsk.queue_id = umem->queue_id;
 
-		rtnl_lock();
 		err = umem->dev->netdev_ops->ndo_bpf(umem->dev, &bpf);
-		rtnl_unlock();
 
 		if (err)
 			WARN(1, "failed to disable umem!\n");
 	}
 
-	rtnl_lock();
 	xdp_clear_umem_at_qid(umem->dev, umem->queue_id);
-	rtnl_unlock();
 
 	dev_put(umem->dev);
 	umem->dev = NULL;
 	umem->zc = false;
+
+out_unlock:
+	if (!lock)
+		rtnl_unlock();
 }
 
 static void xdp_umem_unpin_pages(struct xdp_umem *umem)
net/xdp/xdp_umem.h +1 -0
index 27603227601b..a63a9fb251f5 100644
@@ -10,6 +10,7 @@
 
 int xdp_umem_assign_dev(struct xdp_umem *umem, struct net_device *dev,
 			u16 queue_id, u16 flags);
+void xdp_umem_clear_dev(struct xdp_umem *umem);
 bool xdp_umem_validate_queues(struct xdp_umem *umem);
 void xdp_get_umem(struct xdp_umem *umem);
 void xdp_put_umem(struct xdp_umem *umem);
net/xdp/xsk.c +76 -11

161 lines not on this page · raw message

Signature · 1 line
2.17.1
#4 lore raw

net/xdp/xdp_umem.c int xdp_umem_assign_dev(struct xdp_umem *umem, struct net_device *dev, · line 145

Quoted 12 lines · Ilya Maximets
net/xdp/xdp_umem.c +2 -1
index 267b82a4cbcf..56729e74cbea 100644
@@ -140,34 +140,38 @@ int xdp_umem_assign_dev(struct xdp_umem *umem, struct net_device *dev,
 	return err;
 }
 
-static void xdp_umem_clear_dev(struct xdp_umem *umem)
+void xdp_umem_clear_dev(struct xdp_umem *umem)
 {
+	bool lock = rtnl_is_locked();

How do you know it's not just locked by someone else? You need to pass the locked state in if this is called from different paths, some of which already hold rtnl.

Preferably factor the code which needs the lock out into a separate function like this:

void __function()

{
	do();
	the();
	things();
	under();
	the();
	lock();
}

void function()

{
	rtnl_lock();
	__function();
	rtnl_unlock();
}
Quoted 5 lines
net/xdp/xdp_umem.c +2 -0
 	struct netdev_bpf bpf;
 	int err;
 
+	if (!lock)
+		rtnl_lock();
#5 lore raw
Quoted 9 lines · Jakub Kicinski
Quoted 8 lines · Ilya Maximets
net/xdp/xdp_umem.c +0 -0
index 267b82a4cbcf..56729e74cbea 100644
@@ -140,34 +140,38 @@ int xdp_umem_assign_dev(struct xdp_umem *umem, struct net_device *dev,
 	return err;
 }
 
Quoted 8 lines

net/xdp/xdp_umem.c int xdp_umem_assign_dev(struct xdp_umem *umem, struct net_device *dev, · line 145

Quoted 4 lines
net/xdp/xdp_umem.c +2 -1
-static void xdp_umem_clear_dev(struct xdp_umem *umem)
+void xdp_umem_clear_dev(struct xdp_umem *umem)
 {
+	bool lock = rtnl_is_locked();

How do you know it's not just locked by someone else? You need to pass the locked state in if this is called from different paths, some of which already hold rtnl.

Oh. That's a shame. I need more sleep.

Thanks for spotting. I'll re-work this part.

Best regards, Ilya Maximets.

Quoted 29 lines

Preferably factor the code which needs the lock out into a separate function like this:

void __function()

{
	do();
	the();
	things();
	under();
	the();
	lock();
}

void function()

{
	rtnl_lock();
	__function();
	rtnl_unlock();
}
Quoted 5 lines
net/xdp/xdp_umem.c +2 -0
 	struct netdev_bpf bpf;
 	int err;
 
+	if (!lock)
+		rtnl_lock();