netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Lijun Pan <ljp@linux.ibm.com>
To: netdev@vger.kernel.org, kuba@kernel.org, davem@davemloft.net
Cc: tlfalcon@linux.ibm.com, ast@kernel.org, daniel@iogearbox.net,
	andriin@fb.com, edumazet@google.com, weiwan@google.com,
	cong.wang@bytedance.com, ap420073@gmail.com,
	shemminger@linux-foundation.org, Lijun Pan <ljp@linux.ibm.com>
Subject: [PATCH net] net: core: avoid napi_disable to cause deadlock
Date: Thu, 18 Mar 2021 03:04:50 -0500	[thread overview]
Message-ID: <20210318080450.38893-1-ljp@linux.ibm.com> (raw)

There are chances that napi_disable is called twice by NIC driver.
This could generate deadlock. For example,
the first napi_disable will spin until NAPI_STATE_SCHED is cleared
by napi_complete_done, then set it again.
When napi_disable is called the second time, it will loop infinitely
because no dev->poll will be running to clear NAPI_STATE_SCHED.

CPU0				CPU1
 napi_disable
  test_and_set_bit
  (napi_complete_done clears
   NAPI_STATE_SCHED, ret 0,
   and set NAPI_STATE_SCHED)
				napi_disable
				 test_and_set_bit
				 (ret 1 and loop infinitely because
				  no napi instance is scheduled to
				  clear NAPI_STATE_SCHED bit)

Checking the napi state bit to make sure if napi is already disabled,
exit the call early enough to avoid spinning infinitely.

Fixes: bea3348eef27 ("[NET]: Make NAPI polling independent of struct net_device objects.")
Signed-off-by: Lijun Pan <ljp@linux.ibm.com>
---
 net/core/dev.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/net/core/dev.c b/net/core/dev.c
index 6c5967e80132..eb3c0ddd4fd7 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -6809,6 +6809,24 @@ EXPORT_SYMBOL(netif_napi_add);
 void napi_disable(struct napi_struct *n)
 {
 	might_sleep();
+
+	/* make sure napi_disable() runs only once,
+	 * When napi is disabled, the state bits are like:
+	 * NAPI_STATE_SCHED (set by previous napi_disable)
+	 * NAPI_STATE_NPSVC (set by previous napi_disable)
+	 * NAPI_STATE_DISABLE (cleared by previous napi_disable)
+	 * NAPI_STATE_PREFER_BUSY_POLL (cleared by previous napi_complete_done)
+	 * NAPI_STATE_MISSED (cleared by previous napi_complete_done)
+	 */
+
+	if (napi_disable_pending(n))
+		return;
+	if (test_bit(NAPI_STATE_SCHED, &n->state) &&
+	    test_bit(NAPI_STATE_NPSVC, &n->state) &&
+	    !test_bit(NAPI_STATE_MISSED, &n->state) &&
+	    !test_bit(NAPI_STATE_PREFER_BUSY_POLL, &n->state))
+		return;
+
 	set_bit(NAPI_STATE_DISABLE, &n->state);
 
 	while (test_and_set_bit(NAPI_STATE_SCHED, &n->state))
-- 
2.23.0


             reply	other threads:[~2021-03-18  8:05 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-18  8:04 Lijun Pan [this message]
2021-03-18  9:22 ` [PATCH net] net: core: avoid napi_disable to cause deadlock Eric Dumazet
2021-03-18 18:23   ` David Miller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210318080450.38893-1-ljp@linux.ibm.com \
    --to=ljp@linux.ibm.com \
    --cc=andriin@fb.com \
    --cc=ap420073@gmail.com \
    --cc=ast@kernel.org \
    --cc=cong.wang@bytedance.com \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=shemminger@linux-foundation.org \
    --cc=tlfalcon@linux.ibm.com \
    --cc=weiwan@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).