Skip to contentSkip to conversation

net: don't let netpoll invoke NAPI if in xmit context

PATCH Applied Jakub Kicinski 3 participants 5 messages Mar 31, 2023 – Apr 2, 2023

Subsystems (2)
lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

PATCH 1/1 net: don't let netpoll invoke NAPI if in xmit context

Commit 0db3dc73f7a3 ("[NETPOLL]: tx lock deadlock fix") narrowed down the region under netif_tx_trylock() inside netpoll_send_skb(). (At that point in time netif_tx_trylock() would lock all queues of the device.) Taking the tx lock was problematic because driver's cleanup method may take the same lock. So the change made us hold the xmit lock only around xmit, and expected the driver to take care of locking within ->ndo_poll_controller().

Unfortunately this only works if netpoll isn't itself called with the xmit lock already held. Netpoll code is careful and uses trylock(). The drivers, however, may be using plain lock(). Printing while holding the xmit lock is going to result in rare deadlocks.

Luckily we record the xmit lock owners, so we can scan all the queues, the same way we scan NAPI owners. If any of the xmit locks is held by the local CPU we better not attempt any polling.

It would be nice if we could narrow down the check to only the NAPIs and the queue we're trying to use. I don't see a way to do that now.

Fixes 0db3dc73f7a3 ("[NETPOLL]: tx lock deadlock fix")

---
CC: [email protected]
CC: [email protected]

---
 net/core/netpoll.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
net/core/netpoll.c +18 -1
index a089b704b986..e6a739b1afa9 100644
@@ -137,6 +137,20 @@ static void queue_process(struct work_struct *work)
 	}
 }
 
+static int netif_local_xmit_active(struct net_device *dev)
+{
+	int i;
+
+	for (i = 0; i < dev->num_tx_queues; i++) {
+		struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
+
+		if (READ_ONCE(txq->xmit_lock_owner) == smp_processor_id())
+			return 1;
+	}
+
+	return 0;
+}
+
 static void poll_one_napi(struct napi_struct *napi)
 {
 	int work;
@@ -183,7 +197,10 @@ void netpoll_poll_dev(struct net_device *dev)
 	if (!ni || down_trylock(&ni->dev_lock))
 		return;
 
-	if (!netif_running(dev)) {
+	/* Some drivers will take the same locks in poll and xmit,
+	 * we can't poll if local CPU is already in xmit.
+	 */
+	if (!netif_running(dev) || netif_local_xmit_active(dev)) {
 		up(&ni->dev_lock);
 		return;
 	}
Signature · 1 line
2.39.2
#1 lore raw
Quoted 41 lines · Jakub Kicinski

Commit 0db3dc73f7a3 ("[NETPOLL]: tx lock deadlock fix") narrowed down the region under netif_tx_trylock() inside netpoll_send_skb(). (At that point in time netif_tx_trylock() would lock all queues of the device.) Taking the tx lock was problematic because driver's cleanup method may take the same lock. So the change made us hold the xmit lock only around xmit, and expected the driver to take care of locking within ->ndo_poll_controller().

Unfortunately this only works if netpoll isn't itself called with the xmit lock already held. Netpoll code is careful and uses trylock(). The drivers, however, may be using plain lock(). Printing while holding the xmit lock is going to result in rare deadlocks.

Luckily we record the xmit lock owners, so we can scan all the queues, the same way we scan NAPI owners. If any of the xmit locks is held by the local CPU we better not attempt any polling.

It would be nice if we could narrow down the check to only the NAPIs and the queue we're trying to use. I don't see a way to do that now.

Fixes 0db3dc73f7a3 ("[NETPOLL]: tx lock deadlock fix")

---
CC: [email protected]
CC: [email protected]

---
 net/core/netpoll.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
net/core/netpoll.c +1 -0
index a089b704b986..e6a739b1afa9 100644
@@ -137,6 +137,20 @@ static void queue_process(struct work_struct *work)
        }
 }
 
+static int netif_local_xmit_active(struct net_device *dev)

net/core/netpoll.c static void queue_process(struct work_struct *work) · line 150

Quoted 10 lines
net/core/netpoll.c +10 -0
+{
+       int i;
+
+       for (i = 0; i < dev->num_tx_queues; i++) {
+               struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
+
+               if (READ_ONCE(txq->xmit_lock_owner) == smp_processor_id())
+                       return 1;
+       }
+

Resend in plain text mode for the list, sorry for duplicate

Note that we update WRITE_ONCE(txq->xmit_lock_owner, cpu) _after_ spin_lock(&txq->_xmit_lock);

So there is a tiny window I think, for missing that we got the spinlock, but I do not see how to avoid it without excessive cost.

Quoted 21 lines
net/core/netpoll.c +7 -2
+       return 0;
+}
+
 static void poll_one_napi(struct napi_struct *napi)
 {
        int work;
@@ -183,7 +197,10 @@ void netpoll_poll_dev(struct net_device *dev)
        if (!ni || down_trylock(&ni->dev_lock))
                return;
 
-       if (!netif_running(dev)) {
+       /* Some drivers will take the same locks in poll and xmit,
+        * we can't poll if local CPU is already in xmit.
+        */
+       if (!netif_running(dev) || netif_local_xmit_active(dev)) {
                up(&ni->dev_lock);
                return;
        }
--

2.39.2

author maintainer
#2 lore raw
Quoted 5 lines · On Fri, 31 Mar 2023 04:41:23 +0200 Eric Dumazet wrote:

Note that we update WRITE_ONCE(txq->xmit_lock_owner, cpu) _after_ spin_lock(&txq->_xmit_lock);

So there is a tiny window I think, for missing that we got the spinlock, but I do not see how to avoid it without excessive cost.

Ugh, true. Hopefully the chances of taking an IRQ which tries to print something between those two instructions are fairly low.

I was considering using dev_recursion_level() but AFAICT we don't currently bump it when dequeuing from the qdisc..

Reviewed-by
#3 lore raw
Quoted 5 lines · Jakub Kicinski
Quoted 3 lines · On Fri, 31 Mar 2023 04:41:23 +0200 Eric Dumazet wrote:

Note that we update WRITE_ONCE(txq->xmit_lock_owner, cpu) _after_ spin_lock(&txq->_xmit_lock);

Quoted 8 lines
Quoted 2 lines

So there is a tiny window I think, for missing that we got the spinlock, but I do not see how to avoid it without excessive cost.

Ugh, true. Hopefully the chances of taking an IRQ which tries to print something between those two instructions are fairly low.

I was considering using dev_recursion_level() but AFAICT we don't currently bump it when dequeuing from the qdisc..

SGTM, thanks.

#4 lore raw
In reply to Jakub Kicinski
This patch was applied to netdev/net.git (main) by David S. Miller <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by David S. Miller <[email protected]>:

Quoted 9 lines · On Thu, 30 Mar 2023 19:21:44 -0700 you wrote:

Commit 0db3dc73f7a3 ("[NETPOLL]: tx lock deadlock fix") narrowed down the region under netif_tx_trylock() inside netpoll_send_skb(). (At that point in time netif_tx_trylock() would lock all queues of the device.) Taking the tx lock was problematic because driver's cleanup method may take the same lock. So the change made us hold the xmit lock only around xmit, and expected the driver to take care of locking within ->ndo_poll_controller().

[...]

Here is the summary with links:
- [net] net: don't let netpoll invoke NAPI if in xmit context

    https://git.kernel.org/netdev/net/c/275b471e3d2d

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html