From: "Michael S. Tsirkin" <mst@redhat.com>
To: Bui Quang Minh <minhquangbui99@gmail.com>
Cc: netdev@vger.kernel.org, "Jason Wang" <jasowang@redhat.com>,
"Xuan Zhuo" <xuanzhuo@linux.alibaba.com>,
"Eugenio Pérez" <eperezma@redhat.com>,
"Andrew Lunn" <andrew+netdev@lunn.ch>,
"David S. Miller" <davem@davemloft.net>,
"Eric Dumazet" <edumazet@google.com>,
"Jakub Kicinski" <kuba@kernel.org>,
"Paolo Abeni" <pabeni@redhat.com>,
"Alexei Starovoitov" <ast@kernel.org>,
"Daniel Borkmann" <daniel@iogearbox.net>,
"Jesper Dangaard Brouer" <hawk@kernel.org>,
"John Fastabend" <john.fastabend@gmail.com>,
"Stanislav Fomichev" <sdf@fomichev.me>,
virtualization@lists.linux.dev, linux-kernel@vger.kernel.org,
bpf@vger.kernel.org
Subject: Re: [PATCH net 2/3] virtio-net: ensure rx NAPI is enabled before enabling refill work
Date: Tue, 23 Dec 2025 20:45:29 -0500 [thread overview]
Message-ID: <20251223203908-mutt-send-email-mst@kernel.org> (raw)
In-Reply-To: <20251223152533.24364-3-minhquangbui99@gmail.com>
On Tue, Dec 23, 2025 at 10:25:32PM +0700, Bui Quang Minh wrote:
> Calling napi_disable() on an already disabled napi can cause the
> deadlock. Because the delayed refill work will call napi_disable(), we
> must ensure that refill work is only enabled and scheduled after we have
> enabled the rx queue's NAPI.
>
> Signed-off-by: Bui Quang Minh <minhquangbui99@gmail.com>
> ---
> drivers/net/virtio_net.c | 31 ++++++++++++++++++++++++-------
> 1 file changed, 24 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
> index 63126e490bda..8016d2b378cf 100644
> --- a/drivers/net/virtio_net.c
> +++ b/drivers/net/virtio_net.c
> @@ -3208,16 +3208,31 @@ static int virtnet_open(struct net_device *dev)
> int i, err;
>
> for (i = 0; i < vi->max_queue_pairs; i++) {
> + bool schedule_refill = false;
> +
> + /* - We must call try_fill_recv before enabling napi of the same
> + * receive queue so that it doesn't race with the call in
> + * virtnet_receive.
> + * - We must enable and schedule delayed refill work only when
> + * we have enabled all the receive queue's napi. Otherwise, in
> + * refill_work, we have a deadlock when calling napi_disable on
> + * an already disabled napi.
> + */
I would do:
bool refill = i < vi->curr_queue_pairs;
in fact this is almost the same as resume with
one small difference. pass a flag so we do not duplicate code?
> if (i < vi->curr_queue_pairs) {
> - enable_delayed_refill(&vi->rq[i]);
> /* Make sure we have some buffers: if oom use wq. */
> if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
> - schedule_delayed_work(&vi->rq[i].refill, 0);
> + schedule_refill = true;
> }
>
> err = virtnet_enable_queue_pair(vi, i);
> if (err < 0)
> goto err_enable_qp;
> +
> + if (i < vi->curr_queue_pairs) {
> + enable_delayed_refill(&vi->rq[i]);
> + if (schedule_refill)
> + schedule_delayed_work(&vi->rq[i].refill, 0);
hmm. should not schedule be under the lock?
> + }
> }
>
> if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
> @@ -3456,11 +3471,16 @@ static void __virtnet_rx_resume(struct virtnet_info *vi,
> bool running = netif_running(vi->dev);
> bool schedule_refill = false;
>
> + /* See the comment in virtnet_open for the ordering rule
> + * of try_fill_recv, receive queue napi_enable and delayed
> + * refill enable/schedule.
> + */
so maybe common code?
> if (refill && !try_fill_recv(vi, rq, GFP_KERNEL))
> schedule_refill = true;
> if (running)
> virtnet_napi_enable(rq);
>
> + enable_delayed_refill(rq);
> if (schedule_refill)
> schedule_delayed_work(&rq->refill, 0);
hmm. should not schedule be under the lock?
> }
> @@ -3470,18 +3490,15 @@ static void virtnet_rx_resume_all(struct virtnet_info *vi)
> int i;
>
> for (i = 0; i < vi->max_queue_pairs; i++) {
> - if (i < vi->curr_queue_pairs) {
> - enable_delayed_refill(&vi->rq[i]);
> + if (i < vi->curr_queue_pairs)
> __virtnet_rx_resume(vi, &vi->rq[i], true);
> - } else {
> + else
> __virtnet_rx_resume(vi, &vi->rq[i], false);
> - }
> }
> }
>
> static void virtnet_rx_resume(struct virtnet_info *vi, struct receive_queue *rq)
> {
> - enable_delayed_refill(rq);
> __virtnet_rx_resume(vi, rq, true);
> }
so I would add bool to virtnet_rx_resume and call it everywhere,
removing __virtnet_rx_resume. can be a patch on top.
>
> --
> 2.43.0
next prev parent reply other threads:[~2025-12-24 1:45 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-23 15:25 [PATCH net 0/3] virtio-net: fix the deadlock when disabling rx NAPI Bui Quang Minh
2025-12-23 15:25 ` [PATCH net 1/3] virtio-net: make refill work a per receive queue work Bui Quang Minh
2025-12-24 0:52 ` Jason Wang
2025-12-24 1:37 ` Xuan Zhuo
2025-12-24 1:47 ` Michael S. Tsirkin
2025-12-24 16:49 ` Bui Quang Minh
2025-12-25 15:55 ` Bui Quang Minh
2025-12-25 16:27 ` Michael S. Tsirkin
2025-12-25 7:33 ` Jason Wang
2025-12-25 16:27 ` Michael S. Tsirkin
2025-12-26 1:31 ` Jason Wang
2025-12-26 7:37 ` Michael S. Tsirkin
2025-12-29 2:57 ` Jason Wang
2025-12-30 16:28 ` Bui Quang Minh
2025-12-30 16:44 ` Michael S. Tsirkin
2025-12-24 16:43 ` Bui Quang Minh
2025-12-24 1:34 ` Michael S. Tsirkin
2025-12-24 10:19 ` Michael S. Tsirkin
2025-12-24 17:03 ` Bui Quang Minh
2025-12-23 15:25 ` [PATCH net 2/3] virtio-net: ensure rx NAPI is enabled before enabling refill work Bui Quang Minh
2025-12-24 1:45 ` Michael S. Tsirkin [this message]
2025-12-24 17:49 ` Bui Quang Minh
2025-12-24 10:20 ` Michael S. Tsirkin
2025-12-23 15:25 ` [PATCH net 3/3] virtio-net: schedule the pending refill work after being enabled Bui Quang Minh
2025-12-24 10:17 ` Michael S. Tsirkin
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=20251223203908-mutt-send-email-mst@kernel.org \
--to=mst@redhat.com \
--cc=andrew+netdev@lunn.ch \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=eperezma@redhat.com \
--cc=hawk@kernel.org \
--cc=jasowang@redhat.com \
--cc=john.fastabend@gmail.com \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=minhquangbui99@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=sdf@fomichev.me \
--cc=virtualization@lists.linux.dev \
--cc=xuanzhuo@linux.alibaba.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).