From patchwork Wed Jun 19 16:19:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heng Qi X-Patchwork-Id: 13704226 X-Patchwork-Delegate: kuba@kernel.org Received: from out30-131.freemail.mail.aliyun.com (out30-131.freemail.mail.aliyun.com [115.124.30.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AD92B15252B for ; Wed, 19 Jun 2024 16:19:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718813955; cv=none; b=hjDedJD/PTXB//xiQceFuB9kxSvxN1Vu5CzJM37f8Qd1wlxgRf0VwG/UlV+Yg2ooh91fc2XsuN0hG197CLrJ9OpmXfrNInJr6139o6LdJYw32iQbyDhudrH5mh2L5wKOORa9XOwlReTiZQi9uhHmlbna9ZIgXPKsJ/jo7hNHhK4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718813955; c=relaxed/simple; bh=2dVbrgEZt4I1r0w8f74cvB5ZjjW89LI8igQo71XEqz4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bV/UEgHSut/KdzdLXXfGjgYM9MBXBgZ+yCSZiHW+MuSx9gU7bGloHKo7GSDhGnDCAMOZ2zH4TAy+7kKIja4Wlg2F1KjC5tvezTB8ExkWaV8FnftqFd7s5zgHo7NUetIS7L82h/fQYJkBP75UzbZSGcwcVNusow3uZ3khgUXxGdA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=bd5iJlSx; arc=none smtp.client-ip=115.124.30.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="bd5iJlSx" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1718813950; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=BiXA+YPL+qrJL81lBoNvxuO+9hIkSQiVA8RW5iLIO8M=; b=bd5iJlSxeurZUuVlI/NTdIRzJsnJhQLvy0QnXFr1T+4LIRSFZgz3cOrNA0RXZxaHoF/4qSJzX/VRIoFvw4stnkM0A3YNWJUS2FAL+iD9pZTjHxZ5ENHjwAILOQNSQ8ako9K2VWcGXj4aDVWTQAkEsnX+AAsCokP5UcoMeWh7pxs= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R191e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033045075189;MF=hengqi@linux.alibaba.com;NM=1;PH=DS;RN=10;SR=0;TI=SMTPD_---0W8om-3F_1718813949; Received: from localhost(mailfrom:hengqi@linux.alibaba.com fp:SMTPD_---0W8om-3F_1718813949) by smtp.aliyun-inc.com; Thu, 20 Jun 2024 00:19:10 +0800 From: Heng Qi To: netdev@vger.kernel.org, virtualization@lists.linux.dev Cc: Jason Wang , "Michael S. Tsirkin" , Xuan Zhuo , =?utf-8?q?Eugenio_P=C3=A9rez?= , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Subject: [PATCH net-next v4 1/5] virtio_net: passing control_buf explicitly Date: Thu, 20 Jun 2024 00:19:04 +0800 Message-Id: <20240619161908.82348-2-hengqi@linux.alibaba.com> X-Mailer: git-send-email 2.32.0.3.g01195cf9f In-Reply-To: <20240619161908.82348-1-hengqi@linux.alibaba.com> References: <20240619161908.82348-1-hengqi@linux.alibaba.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org In a later patch, the driver may send requests concurrently, in which case each command will have its own control buffer, so we refactor virtnet_send_command_reply() to pass the control buffer explicitly as a parameter. Signed-off-by: Heng Qi --- drivers/net/virtio_net.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c index 61a57d134544..b45f58a902e3 100644 --- a/drivers/net/virtio_net.c +++ b/drivers/net/virtio_net.c @@ -2680,7 +2680,9 @@ static int virtnet_tx_resize(struct virtnet_info *vi, * supported by the hypervisor, as indicated by feature bits, should * never fail unless improperly formatted. */ -static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd, +static bool virtnet_send_command_reply(struct virtnet_info *vi, + u8 class, u8 cmd, + struct control_buf *ctrl, struct scatterlist *out, struct scatterlist *in) { @@ -2693,18 +2695,18 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)); mutex_lock(&vi->cvq_lock); - vi->ctrl->status = ~0; - vi->ctrl->hdr.class = class; - vi->ctrl->hdr.cmd = cmd; + ctrl->status = ~0; + ctrl->hdr.class = class; + ctrl->hdr.cmd = cmd; /* Add header */ - sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr)); + sg_init_one(&hdr, &ctrl->hdr, sizeof(ctrl->hdr)); sgs[out_num++] = &hdr; if (out) sgs[out_num++] = out; /* Add return status. */ - sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status)); + sg_init_one(&stat, &ctrl->status, sizeof(ctrl->status)); sgs[out_num + in_num++] = &stat; if (in) @@ -2732,7 +2734,7 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd } unlock: - ok = vi->ctrl->status == VIRTIO_NET_OK; + ok = ctrl->status == VIRTIO_NET_OK; mutex_unlock(&vi->cvq_lock); return ok; } @@ -2740,7 +2742,7 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, struct scatterlist *out) { - return virtnet_send_command_reply(vi, class, cmd, out, NULL); + return virtnet_send_command_reply(vi, class, cmd, vi->ctrl, out, NULL); } static int virtnet_set_mac_address(struct net_device *dev, void *p) @@ -4020,7 +4022,7 @@ static int __virtnet_get_hw_stats(struct virtnet_info *vi, ok = virtnet_send_command_reply(vi, VIRTIO_NET_CTRL_STATS, VIRTIO_NET_CTRL_STATS_GET, - &sgs_out, &sgs_in); + vi->ctrl, &sgs_out, &sgs_in); if (!ok) return ok; @@ -5880,7 +5882,7 @@ static int virtnet_probe(struct virtio_device *vdev) if (!virtnet_send_command_reply(vi, VIRTIO_NET_CTRL_STATS, VIRTIO_NET_CTRL_STATS_QUERY, - NULL, &sg)) { + vi->ctrl, NULL, &sg)) { pr_debug("virtio_net: fail to get stats capability\n"); rtnl_unlock(); err = -EINVAL; From patchwork Wed Jun 19 16:19:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heng Qi X-Patchwork-Id: 13704227 X-Patchwork-Delegate: kuba@kernel.org Received: from out30-131.freemail.mail.aliyun.com (out30-131.freemail.mail.aliyun.com [115.124.30.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 23CDE15217F for ; Wed, 19 Jun 2024 16:19:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718813956; cv=none; b=XXrHGT2eX3m9/BbDs1b1udXbkRyfoJbY4fejjycrRuhpiPhK9qRCHdp8GqIPcvkQQPP/Y30EjJgy0yjJzol3bTFdMKA95Fg5xCVXlQ0bJHElZOfL4/wwHvH11qMTWjXevji9Nk7c+/Fxp/PDnd7wiT5rg5TFa89EKRoi5zvOe0s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718813956; c=relaxed/simple; bh=1BZJxMvxEUAwC1iRE3nfzUQtnqIt/eiXx6ZTm6rSSRk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WZ5WdmRFvVvSvW93BWM+xbCzKNvC0L/E8MfKVm/HV3C1bxuUqXhILFlsUzJE6EH2/2ASERca7N1qf/Ddaud4QM1ZSwb4SU9yAo7+uumaCwzzZTbJAejvovqSNjs225EGTWwBjYmhZzCuPEK+qvmAGWiS6Y9oVmqFShVa4RWuIMY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=uT69190t; arc=none smtp.client-ip=115.124.30.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="uT69190t" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1718813951; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=d0e/DRwzgHTVqh5xXymRXwDC8hqxwM0bVlAwXi4XQX0=; b=uT69190tkMxqkvLkEXVlMVfJPPvLRZXZXRA/bR3XQMJ6jupfppZcbjQ0b7u/xD9di7ZrWZlLjyDQmGu5RLdJQ6h4I2L5GO/upKM0tnlij2+EgFmRIazW8M4RmbcnUPr6XrdCBnI6YotIaj0EpaC3lqOUGfVuGZASZ9UPDRpDpD4= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R911e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037067112;MF=hengqi@linux.alibaba.com;NM=1;PH=DS;RN=10;SR=0;TI=SMTPD_---0W8oeGZa_1718813950; Received: from localhost(mailfrom:hengqi@linux.alibaba.com fp:SMTPD_---0W8oeGZa_1718813950) by smtp.aliyun-inc.com; Thu, 20 Jun 2024 00:19:11 +0800 From: Heng Qi To: netdev@vger.kernel.org, virtualization@lists.linux.dev Cc: Jason Wang , "Michael S. Tsirkin" , Xuan Zhuo , =?utf-8?q?Eugenio_P=C3=A9rez?= , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Subject: [PATCH net-next v4 2/5] virtio_net: enable irq for the control vq Date: Thu, 20 Jun 2024 00:19:05 +0800 Message-Id: <20240619161908.82348-3-hengqi@linux.alibaba.com> X-Mailer: git-send-email 2.32.0.3.g01195cf9f In-Reply-To: <20240619161908.82348-1-hengqi@linux.alibaba.com> References: <20240619161908.82348-1-hengqi@linux.alibaba.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org If the device does not respond to a request for a long time, then control vq polling elevates CPU utilization, a problem that exacerbates with more command requests. Enabling control vq's irq is advantageous for the guest, and this still doesn't support concurrent requests. Suggested-by: Jason Wang Signed-off-by: Heng Qi --- drivers/net/virtio_net.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c index b45f58a902e3..ed10084997d3 100644 --- a/drivers/net/virtio_net.c +++ b/drivers/net/virtio_net.c @@ -372,6 +372,8 @@ struct virtio_net_ctrl_rss { struct control_buf { struct virtio_net_ctrl_hdr hdr; virtio_net_ctrl_ack status; + /* Wait for the device to complete the cvq request. */ + struct completion completion; }; struct virtnet_info { @@ -664,6 +666,13 @@ static bool virtqueue_napi_complete(struct napi_struct *napi, return false; } +static void virtnet_cvq_done(struct virtqueue *cvq) +{ + struct virtnet_info *vi = cvq->vdev->priv; + + complete(&vi->ctrl->completion); +} + static void skb_xmit_done(struct virtqueue *vq) { struct virtnet_info *vi = vq->vdev->priv; @@ -2724,14 +2733,8 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, if (unlikely(!virtqueue_kick(vi->cvq))) goto unlock; - /* Spin for a response, the kick causes an ioport write, trapping - * into the hypervisor, so the request should be handled immediately. - */ - while (!virtqueue_get_buf(vi->cvq, &tmp) && - !virtqueue_is_broken(vi->cvq)) { - cond_resched(); - cpu_relax(); - } + wait_for_completion(&ctrl->completion); + virtqueue_get_buf(vi->cvq, &tmp); unlock: ok = ctrl->status == VIRTIO_NET_OK; @@ -5312,7 +5315,7 @@ static int virtnet_find_vqs(struct virtnet_info *vi) /* Parameters for control virtqueue, if any */ if (vi->has_cvq) { - callbacks[total_vqs - 1] = NULL; + callbacks[total_vqs - 1] = virtnet_cvq_done; names[total_vqs - 1] = "control"; } @@ -5832,6 +5835,7 @@ static int virtnet_probe(struct virtio_device *vdev) if (vi->has_rss || vi->has_rss_hash_report) virtnet_init_default_rss(vi); + init_completion(&vi->ctrl->completion); enable_rx_mode_work(vi); /* serialize netdev register + virtio_device_ready() with ndo_open() */ From patchwork Wed Jun 19 16:19:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heng Qi X-Patchwork-Id: 13704228 X-Patchwork-Delegate: kuba@kernel.org Received: from out30-100.freemail.mail.aliyun.com (out30-100.freemail.mail.aliyun.com [115.124.30.100]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5CE99152799 for ; Wed, 19 Jun 2024 16:19:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.100 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718813958; cv=none; b=mo6e+KeW5RFsNGWHr3+KAW7BtPx5/pMjC/s5fEOLOj4V4ervV9zcFikNGtBDrTRxpzYE8Pv6nkI+zyze21bb9qEO8+wHINKz8ZLEauaE30uPZrA+UwszYd79wUIKKvBOQvLNlsoFzyX/4IBTJYdSR4Mz86atr2IzuTtgSQ6EHmg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718813958; c=relaxed/simple; bh=3Yh5PsChSezWkJHGbQsZqeGy1wvAeYrKRxbMw+IleIY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=iMrdTIkPSWiTbW33JfG0KjSukF3I4he73AyZL1NRLSMB0clmDmsis8TebLxzLK7Q8er1fIOMd642bw/65aTmn7PyJyZGjW+orx3Els9RQBEQuKK/DlEtHr10j1zfMayiBltY2VHQtVUVr/xYHsTAi4yEASx/uWvz9E3nBMCW72g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=B+ZaMes/; arc=none smtp.client-ip=115.124.30.100 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="B+ZaMes/" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1718813952; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=5/ZfHHCtLhURn17PHo/pz9cGgrwQYJFZxD846L+urmA=; b=B+ZaMes/2SGGkhIEPO0b/MQMZihH6msKHyez2scfZJ0/K3XFACyZCXdQk7GLGTJW/tRRgj7gXO/L2sXIVaiDeifQGuvFMk98r9NW84UnNptiAXJrOosj7l45kY3CRJwo8LGJnGni90z310p6nbsSsTG0WHQbndhBl2u4EWkTCOo= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R251e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033045075189;MF=hengqi@linux.alibaba.com;NM=1;PH=DS;RN=10;SR=0;TI=SMTPD_---0W8olDpW_1718813951; Received: from localhost(mailfrom:hengqi@linux.alibaba.com fp:SMTPD_---0W8olDpW_1718813951) by smtp.aliyun-inc.com; Thu, 20 Jun 2024 00:19:12 +0800 From: Heng Qi To: netdev@vger.kernel.org, virtualization@lists.linux.dev Cc: Jason Wang , "Michael S. Tsirkin" , Xuan Zhuo , =?utf-8?q?Eugenio_P=C3=A9rez?= , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Subject: [PATCH net-next v4 3/5] virtio_net: change the command token to completion Date: Thu, 20 Jun 2024 00:19:06 +0800 Message-Id: <20240619161908.82348-4-hengqi@linux.alibaba.com> X-Mailer: git-send-email 2.32.0.3.g01195cf9f In-Reply-To: <20240619161908.82348-1-hengqi@linux.alibaba.com> References: <20240619161908.82348-1-hengqi@linux.alibaba.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Previously, control vq only allowed a single request to be sent, so using virtnet_info as a global token was fine. To support concurrent requests, the driver needs to use a command-level token to distinguish between requests that have been sent. Signed-off-by: Heng Qi --- drivers/net/virtio_net.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c index ed10084997d3..4256b1eda043 100644 --- a/drivers/net/virtio_net.c +++ b/drivers/net/virtio_net.c @@ -2722,7 +2722,8 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, sgs[out_num + in_num++] = in; BUG_ON(out_num + in_num > ARRAY_SIZE(sgs)); - ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, in_num, vi, GFP_ATOMIC); + ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, in_num, + &ctrl->completion, GFP_ATOMIC); if (ret < 0) { dev_warn(&vi->vdev->dev, "Failed to add sgs for command vq: %d\n.", ret); From patchwork Wed Jun 19 16:19:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heng Qi X-Patchwork-Id: 13704231 X-Patchwork-Delegate: kuba@kernel.org Received: from out30-99.freemail.mail.aliyun.com (out30-99.freemail.mail.aliyun.com [115.124.30.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BBD6515253F for ; Wed, 19 Jun 2024 16:19:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.99 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718813963; cv=none; b=ICiAUq2zrytEVeD/vd8ATgqm9/0DVaGamqJp+AaopTSGjwWgjjPIQkT9xQQFHrodbosZPt5DjUe0wxNNvuDxlo7TPMeUvffmlCcKnaJTm3hceZVqxaTRtwXsH8DlG4wQO6Peb/AeE50k4r2NBRrLJyAmCahGXSdjmt90wfYSV1s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718813963; c=relaxed/simple; bh=H0wPUkKA0zpWUqJrsXS1wYKydUW5738e7/3qTUtPPuY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Bqs4p6kMeH5dJ0yF5rU81o6MCudDn44pE1cw4EDrlhFbdMg77EULcSqfjpHtuUMqg3xL7bSSK9VCjEs1B9op/HtkAjj8hlvEeQ8XhIhXqxdODi7w6dK+We8mb0TtVwkrcC8U0UUxLIq4sxqxvc5qUjFgBEsBMNx3A4AaLs0QuN8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=YXGOofeT; arc=none smtp.client-ip=115.124.30.99 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="YXGOofeT" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1718813954; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=nth99UAEiHeYJcFkVNJBhPlOnKEG+7hPVJACVVD5W88=; b=YXGOofeTnYQb/dBUC8CqE3Piom8paEQC5GhBINoGl9+w2w78pB6fSrPZDPjs3UeGkVY7npEEuqqdTEG+1fTyW1MnoncHJwwXTZsMr/OFpC1xn37/yTFzcjzh9KVMv6uTcPnD8ssedA1saohxqukfwlSfyFqusDXwIql1ztX07uk= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R181e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033068173054;MF=hengqi@linux.alibaba.com;NM=1;PH=DS;RN=10;SR=0;TI=SMTPD_---0W8om-3u_1718813952; Received: from localhost(mailfrom:hengqi@linux.alibaba.com fp:SMTPD_---0W8om-3u_1718813952) by smtp.aliyun-inc.com; Thu, 20 Jun 2024 00:19:13 +0800 From: Heng Qi To: netdev@vger.kernel.org, virtualization@lists.linux.dev Cc: Jason Wang , "Michael S. Tsirkin" , Xuan Zhuo , =?utf-8?q?Eugenio_P=C3=A9rez?= , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Subject: [PATCH net-next v4 4/5] virtio_net: refactor command sending and response handling Date: Thu, 20 Jun 2024 00:19:07 +0800 Message-Id: <20240619161908.82348-5-hengqi@linux.alibaba.com> X-Mailer: git-send-email 2.32.0.3.g01195cf9f In-Reply-To: <20240619161908.82348-1-hengqi@linux.alibaba.com> References: <20240619161908.82348-1-hengqi@linux.alibaba.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Refactor the command handling logic by splitting virtnet_send_command_reply into virtnet_add_command_reply and virtnet_wait_command_response for better clarity and subsequent use. Signed-off-by: Heng Qi --- drivers/net/virtio_net.c | 53 ++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c index 4256b1eda043..807724772bcf 100644 --- a/drivers/net/virtio_net.c +++ b/drivers/net/virtio_net.c @@ -2684,20 +2684,14 @@ static int virtnet_tx_resize(struct virtnet_info *vi, return err; } -/* - * Send command via the control virtqueue and check status. Commands - * supported by the hypervisor, as indicated by feature bits, should - * never fail unless improperly formatted. - */ -static bool virtnet_send_command_reply(struct virtnet_info *vi, - u8 class, u8 cmd, - struct control_buf *ctrl, - struct scatterlist *out, - struct scatterlist *in) +static bool virtnet_add_command_reply(struct virtnet_info *vi, + u8 class, u8 cmd, + struct control_buf *ctrl, + struct scatterlist *out, + struct scatterlist *in) { struct scatterlist *sgs[5], hdr, stat; - u32 out_num = 0, tmp, in_num = 0; - bool ok; + u32 out_num = 0, in_num = 0; int ret; /* Caller should know better */ @@ -2731,18 +2725,47 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, return false; } - if (unlikely(!virtqueue_kick(vi->cvq))) - goto unlock; + if (unlikely(!virtqueue_kick(vi->cvq))) { + mutex_unlock(&vi->cvq_lock); + return false; + } + + return true; +} + +static bool virtnet_wait_command_response(struct virtnet_info *vi, + struct control_buf *ctrl) +{ + unsigned int tmp; + bool ok; wait_for_completion(&ctrl->completion); virtqueue_get_buf(vi->cvq, &tmp); -unlock: ok = ctrl->status == VIRTIO_NET_OK; mutex_unlock(&vi->cvq_lock); return ok; } +/* Send command via the control virtqueue and check status. Commands + * supported by the hypervisor, as indicated by feature bits, should + * never fail unless improperly formatted. + */ +static bool virtnet_send_command_reply(struct virtnet_info *vi, + u8 class, u8 cmd, + struct control_buf *ctrl, + struct scatterlist *out, + struct scatterlist *in) +{ + bool ret; + + ret = virtnet_add_command_reply(vi, class, cmd, ctrl, out, in); + if (!ret) + return ret; + + return virtnet_wait_command_response(vi, ctrl); +} + static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, struct scatterlist *out) { From patchwork Wed Jun 19 16:19:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heng Qi X-Patchwork-Id: 13704229 X-Patchwork-Delegate: kuba@kernel.org Received: from out30-113.freemail.mail.aliyun.com (out30-113.freemail.mail.aliyun.com [115.124.30.113]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 05BB6153517 for ; Wed, 19 Jun 2024 16:19:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.113 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718813959; cv=none; b=rgydw6aqHq18V2Ly2BP5vfK8KtyXlTJKocdpZVI5NDrlVuooBopgA+PBVatLSm/EfJVK2zEVEuHs7ImTkiKn1pcszNQFCmJipiO440V2cuIWJCloKpNcbOdyNQpqSBIGD5IesgKhe/4p97kIn0wwBAgYE0i6iolRSHKE66yepHU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718813959; c=relaxed/simple; bh=mTcvtuNXKLYqNNpz9HH3VkQuCcWXve2g64/UWOHq+aY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=h2bZJS2bS/Bn0hJ5V+lT6P7qaY1XnvZoJS2mB8vQDe637L8EhiqeG/uZC/E/ILSAxgHO/wp6Ng7oVXopJcz6HgddwngamuyX2GP5cBDkorHcY86mQkqJJEDicLaNot0xsXGobxSOBIYHuYtx2EIJD5X/Wr1H1KTYt/mbaBdmJmY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=Ynpz+qbl; arc=none smtp.client-ip=115.124.30.113 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="Ynpz+qbl" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1718813954; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=ITzd3w/WqM0TQWzJVHlTWo29vu31745lICvMHq2EJ+M=; b=Ynpz+qblf2yiryhwF1MBwk+ilTtDw0zn/QMQq/OdB8FYbpMmS67Q6XmKIIaIzSiXS2/h9EcJ8u3f95pC7+NLZRTsWeKy0jm9lno4zO+fI8sqIbWXyUBTTJjWHisUAaJBDzeoTeEU/r/4PMfeQgpLimywUJGs2vANLy/Z+1n9520= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R161e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037067112;MF=hengqi@linux.alibaba.com;NM=1;PH=DS;RN=10;SR=0;TI=SMTPD_---0W8olbkL_1718813953; Received: from localhost(mailfrom:hengqi@linux.alibaba.com fp:SMTPD_---0W8olbkL_1718813953) by smtp.aliyun-inc.com; Thu, 20 Jun 2024 00:19:14 +0800 From: Heng Qi To: netdev@vger.kernel.org, virtualization@lists.linux.dev Cc: Jason Wang , "Michael S. Tsirkin" , Xuan Zhuo , =?utf-8?q?Eugenio_P=C3=A9rez?= , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Subject: [PATCH net-next v4 5/5] virtio_net: improve dim command request efficiency Date: Thu, 20 Jun 2024 00:19:08 +0800 Message-Id: <20240619161908.82348-6-hengqi@linux.alibaba.com> X-Mailer: git-send-email 2.32.0.3.g01195cf9f In-Reply-To: <20240619161908.82348-1-hengqi@linux.alibaba.com> References: <20240619161908.82348-1-hengqi@linux.alibaba.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Currently, control vq handles commands synchronously, leading to increased delays for dim commands during multi-queue VM configuration and directly impacting dim performance. To address this, we are shifting to asynchronous processing of ctrlq's dim commands. Signed-off-by: Heng Qi --- drivers/net/virtio_net.c | 221 +++++++++++++++++++++++++++++++++++---- 1 file changed, 201 insertions(+), 20 deletions(-) diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c index 807724772bcf..bd224e282f9b 100644 --- a/drivers/net/virtio_net.c +++ b/drivers/net/virtio_net.c @@ -376,6 +376,12 @@ struct control_buf { struct completion completion; }; +struct virtnet_coal_node { + struct control_buf ctrl; + struct virtio_net_ctrl_coal_vq coal_vqs; + struct list_head list; +}; + struct virtnet_info { struct virtio_device *vdev; struct virtqueue *cvq; @@ -420,6 +426,12 @@ struct virtnet_info { /* Lock to protect the control VQ */ struct mutex cvq_lock; + /* Work struct for acquisition of cvq processing results. */ + struct work_struct get_cvq; + + /* OK to queue work getting cvq response? */ + bool get_cvq_work_enabled; + /* Host can handle any s/g split between our header and packet data */ bool any_header_sg; @@ -464,6 +476,10 @@ struct virtnet_info { struct virtnet_interrupt_coalesce intr_coal_tx; struct virtnet_interrupt_coalesce intr_coal_rx; + /* Free nodes used for concurrent delivery */ + struct mutex coal_free_lock; + struct list_head coal_free_list; + unsigned long guest_offloads; unsigned long guest_offloads_capable; @@ -666,11 +682,26 @@ static bool virtqueue_napi_complete(struct napi_struct *napi, return false; } +static void enable_get_cvq_work(struct virtnet_info *vi) +{ + rtnl_lock(); + vi->get_cvq_work_enabled = true; + rtnl_unlock(); +} + +static void disable_get_cvq_work(struct virtnet_info *vi) +{ + rtnl_lock(); + vi->get_cvq_work_enabled = false; + rtnl_unlock(); +} + static void virtnet_cvq_done(struct virtqueue *cvq) { struct virtnet_info *vi = cvq->vdev->priv; - complete(&vi->ctrl->completion); + virtqueue_disable_cb(cvq); + schedule_work(&vi->get_cvq); } static void skb_xmit_done(struct virtqueue *vq) @@ -2730,6 +2761,7 @@ static bool virtnet_add_command_reply(struct virtnet_info *vi, return false; } + mutex_unlock(&vi->cvq_lock); return true; } @@ -2740,10 +2772,8 @@ static bool virtnet_wait_command_response(struct virtnet_info *vi, bool ok; wait_for_completion(&ctrl->completion); - virtqueue_get_buf(vi->cvq, &tmp); ok = ctrl->status == VIRTIO_NET_OK; - mutex_unlock(&vi->cvq_lock); return ok; } @@ -2772,6 +2802,89 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, return virtnet_send_command_reply(vi, class, cmd, vi->ctrl, out, NULL); } +static bool virtnet_process_dim_cmd(struct virtnet_info *vi, + struct virtnet_coal_node *node) +{ + u16 qnum = le16_to_cpu(node->coal_vqs.vqn) / 2; + bool ret; + + ret = virtnet_wait_command_response(vi, &node->ctrl); + if (ret) { + mutex_lock(&vi->rq[qnum].dim_lock); + vi->rq[qnum].intr_coal.max_usecs = + le32_to_cpu(node->coal_vqs.coal.max_usecs); + vi->rq[qnum].intr_coal.max_packets = + le32_to_cpu(node->coal_vqs.coal.max_packets); + mutex_unlock(&vi->rq[qnum].dim_lock); + } + + vi->rq[qnum].dim.state = DIM_START_MEASURE; + + mutex_lock(&vi->coal_free_lock); + list_add(&node->list, &vi->coal_free_list); + mutex_unlock(&vi->coal_free_lock); + return ret; +} + +static bool virtnet_add_dim_command(struct virtnet_info *vi, + struct receive_queue *rq, + struct dim_cq_moder update_moder, + struct virtnet_coal_node **avail_coal) +{ + struct virtnet_coal_node *node; + struct scatterlist sg; + bool ret; + + mutex_lock(&vi->coal_free_lock); + if (list_empty(&vi->coal_free_list)) { + mutex_unlock(&vi->coal_free_lock); + return false; + } + + node = list_first_entry(&vi->coal_free_list, + struct virtnet_coal_node, list); + node->coal_vqs.vqn = cpu_to_le16(rxq2vq(rq - vi->rq)); + node->coal_vqs.coal.max_usecs = cpu_to_le32(update_moder.usec); + node->coal_vqs.coal.max_packets = cpu_to_le32(update_moder.pkts); + + sg_init_one(&sg, &node->coal_vqs, sizeof(node->coal_vqs)); + ret = virtnet_add_command_reply(vi, VIRTIO_NET_CTRL_NOTF_COAL, + VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET, + &node->ctrl, &sg, NULL); + if (!ret) { + dev_warn(&vi->dev->dev, + "Failed to change coalescing params.\n"); + mutex_unlock(&vi->coal_free_lock); + return ret; + } + + *avail_coal = node; + list_del(&node->list); + mutex_unlock(&vi->coal_free_lock); + + return true; +} + +static void virtnet_get_cvq_work(struct work_struct *work) +{ + struct virtnet_info *vi = + container_of(work, struct virtnet_info, get_cvq); + unsigned int tmp; + int opaque; + void *res; + +again: + mutex_lock(&vi->cvq_lock); + while ((res = virtqueue_get_buf(vi->cvq, &tmp)) != NULL) + complete((struct completion *)res); + mutex_unlock(&vi->cvq_lock); + + opaque = virtqueue_enable_cb_prepare(vi->cvq); + if (unlikely(virtqueue_poll(vi->cvq, opaque))) { + virtqueue_disable_cb(vi->cvq); + goto again; + } +} static int virtnet_set_mac_address(struct net_device *dev, void *p) { struct virtnet_info *vi = netdev_priv(dev); @@ -4419,35 +4532,54 @@ static int virtnet_send_notf_coal_vq_cmds(struct virtnet_info *vi, return 0; } +static void virtnet_wait_space(struct virtnet_info *vi) +{ + bool no_coal_free = true; + + while (READ_ONCE(vi->cvq->num_free) < 3) + usleep_range(1000, 2000); + + while (no_coal_free) { + mutex_lock(&vi->coal_free_lock); + if (!list_empty(&vi->coal_free_list)) + no_coal_free = false; + mutex_unlock(&vi->coal_free_lock); + if (no_coal_free) + usleep_range(1000, 2000); + } +} + static void virtnet_rx_dim_work(struct work_struct *work) { struct dim *dim = container_of(work, struct dim, work); struct receive_queue *rq = container_of(dim, struct receive_queue, dim); struct virtnet_info *vi = rq->vq->vdev->priv; - struct net_device *dev = vi->dev; + struct virtnet_coal_node *avail_coal; struct dim_cq_moder update_moder; - int qnum, err; - qnum = rq - vi->rq; + update_moder = net_dim_get_rx_moderation(dim->mode, dim->profile_ix); mutex_lock(&rq->dim_lock); - if (!rq->dim_enabled) - goto out; - - update_moder = net_dim_get_rx_moderation(dim->mode, dim->profile_ix); - if (update_moder.usec != rq->intr_coal.max_usecs || - update_moder.pkts != rq->intr_coal.max_packets) { - err = virtnet_send_rx_ctrl_coal_vq_cmd(vi, qnum, - update_moder.usec, - update_moder.pkts); - if (err) - pr_debug("%s: Failed to send dim parameters on rxq%d\n", - dev->name, qnum); + if (!rq->dim_enabled || + (update_moder.usec == rq->intr_coal.max_usecs && + update_moder.pkts == rq->intr_coal.max_packets)) { + rq->dim.state = DIM_START_MEASURE; + mutex_unlock(&rq->dim_lock); + return; } -out: - dim->state = DIM_START_MEASURE; mutex_unlock(&rq->dim_lock); + +again: + virtnet_wait_space(vi); + + if (!virtnet_add_dim_command(vi, rq, update_moder, &avail_coal)) { + if (virtqueue_is_broken(vi->cvq)) + return; + goto again; + } + + virtnet_process_dim_cmd(vi, avail_coal); } static int virtnet_coal_params_supported(struct ethtool_coalesce *ec) @@ -4860,6 +4992,8 @@ static void virtnet_freeze_down(struct virtio_device *vdev) flush_work(&vi->config_work); disable_rx_mode_work(vi); flush_work(&vi->rx_mode_work); + disable_get_cvq_work(vi); + flush_work(&vi->get_cvq); netif_tx_lock_bh(vi->dev); netif_device_detach(vi->dev); @@ -4883,6 +5017,7 @@ static int virtnet_restore_up(struct virtio_device *vdev) enable_delayed_refill(vi); enable_rx_mode_work(vi); + enable_get_cvq_work(vi); if (netif_running(vi->dev)) { err = virtnet_open(vi->dev); @@ -5633,6 +5768,43 @@ static const struct xdp_metadata_ops virtnet_xdp_metadata_ops = { .xmo_rx_hash = virtnet_xdp_rx_hash, }; +static void virtnet_del_coal_free_list(struct virtnet_info *vi) +{ + struct virtnet_coal_node *coal_node, *tmp; + + list_for_each_entry_safe(coal_node, tmp, &vi->coal_free_list, list) { + list_del(&coal_node->list); + kfree(coal_node); + } +} + +static int virtnet_init_coal_list(struct virtnet_info *vi) +{ + struct virtnet_coal_node *coal_node; + int batch_dim_nums; + int i; + + INIT_LIST_HEAD(&vi->coal_free_list); + mutex_init(&vi->coal_free_lock); + + if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) + return 0; + + batch_dim_nums = min((unsigned int)vi->max_queue_pairs, + virtqueue_get_vring_size(vi->cvq) / 3); + for (i = 0; i < batch_dim_nums; i++) { + coal_node = kzalloc(sizeof(*coal_node), GFP_KERNEL); + if (!coal_node) { + virtnet_del_coal_free_list(vi); + return -ENOMEM; + } + init_completion(&coal_node->ctrl.completion); + list_add(&coal_node->list, &vi->coal_free_list); + } + + return 0; +} + static int virtnet_probe(struct virtio_device *vdev) { int i, err = -ENOMEM; @@ -5818,6 +5990,9 @@ static int virtnet_probe(struct virtio_device *vdev) if (err) goto free; + if (virtnet_init_coal_list(vi)) + goto free; + if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) { vi->intr_coal_rx.max_usecs = 0; vi->intr_coal_tx.max_usecs = 0; @@ -5859,7 +6034,9 @@ static int virtnet_probe(struct virtio_device *vdev) if (vi->has_rss || vi->has_rss_hash_report) virtnet_init_default_rss(vi); + INIT_WORK(&vi->get_cvq, virtnet_get_cvq_work); init_completion(&vi->ctrl->completion); + enable_get_cvq_work(vi); enable_rx_mode_work(vi); /* serialize netdev register + virtio_device_ready() with ndo_open() */ @@ -5988,11 +6165,15 @@ static void virtnet_remove(struct virtio_device *vdev) flush_work(&vi->config_work); disable_rx_mode_work(vi); flush_work(&vi->rx_mode_work); + disable_get_cvq_work(vi); + flush_work(&vi->get_cvq); unregister_netdev(vi->dev); net_failover_destroy(vi->failover); + virtnet_del_coal_free_list(vi); + remove_vq_common(vi); free_netdev(vi->dev);