From patchwork Thu Jun 6 06:14:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heng Qi X-Patchwork-Id: 13687845 X-Patchwork-Delegate: kuba@kernel.org Received: from out30-124.freemail.mail.aliyun.com (out30-124.freemail.mail.aliyun.com [115.124.30.124]) (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 04D474DA14 for ; Thu, 6 Jun 2024 06:14:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717654501; cv=none; b=tqthXToYm5BuXiFbI7y306oJUrfLtq0Lvst5OczFSJgXw93QMlBaOlEkC+7VhEDUgJzTCrpj5tTFcqEp7JWLmUVpMSbDaDOK8COTpkfTykfQhqf9EWNUfKkVH+JOhsX/7J0tWYRMRjZUR+apqOvjyBUP2nPpvflAOjwLRdF4bv0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717654501; c=relaxed/simple; bh=fyYpu7MxqD1gUL9xBttutRRy+Nl0wJMZ7nQ9jBzswHE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MJ6Di4fqIk+MA5ithNe6dV2xfUGhQvMT6GcCZP+m6MoKiuzOT+tjI+doLtyTB0+7r1eX90qVAZQMeB2J3qnIV3l4RxP0G9ve7LhchG7CDexZ6OE3C30Onz1nEfLAJWUmJ6yfTk7xciY2ktlxRi5SMcnOSHFKEWp/XtegfEC5H1A= 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=CcSdh+Q1; arc=none smtp.client-ip=115.124.30.124 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="CcSdh+Q1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1717654496; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=REAT+/W1Vu+TlElmxbEOqsqLKmA92uxNeUfymZn1AaQ=; b=CcSdh+Q13YUMOQC7hEfmwsflJvh/D6hj26mtKD6IsRAzSP1iB094BpafEvHImQpdcOFwPsCC4E8HXI1e+poqaXWWvuESCljzudSN47XKr6iW8pDA9xiceBWHQi3SyQI226z+ok5bpHfSdOYZxK+X6NJxs/Nx90E+oGMEilu7/yY= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R151e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037067111;MF=hengqi@linux.alibaba.com;NM=1;PH=DS;RN=10;SR=0;TI=SMTPD_---0W7xKi9d_1717654487; Received: from localhost(mailfrom:hengqi@linux.alibaba.com fp:SMTPD_---0W7xKi9d_1717654487) by smtp.aliyun-inc.com; Thu, 06 Jun 2024 14:14:54 +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?= , Eric Dumazet , "David S. Miller" , Jakub Kicinski , Paolo Abeni Subject: [PATCH net-next v3 1/4] virtio_net: passing control_buf explicitly Date: Thu, 6 Jun 2024 14:14:43 +0800 Message-Id: <20240606061446.127802-2-hengqi@linux.alibaba.com> X-Mailer: git-send-email 2.32.0.3.g01195cf9f In-Reply-To: <20240606061446.127802-1-hengqi@linux.alibaba.com> References: <20240606061446.127802-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 4a802c0ea2cb..0f872936d6ed 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) { @@ -2692,18 +2694,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,13 +2734,13 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd unlock: mutex_unlock(&vi->cvq_lock); - return vi->ctrl->status == VIRTIO_NET_OK; + return ctrl->status == VIRTIO_NET_OK; } 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) @@ -4018,7 +4020,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; @@ -5882,7 +5884,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 Thu Jun 6 06:14:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heng Qi X-Patchwork-Id: 13687847 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 4B54261FD3 for ; Thu, 6 Jun 2024 06:15:09 +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=1717654512; cv=none; b=cI6OCHRYMRUKEFVXHjdHVY8sSbK10uhEkWDJyhhXq7RV/s0s800HlFqx3DOV7wE+dmCqX1YAhtFicyQcZWhYmv+2T4zHA3JWclwXgeA3vyTQvRLED+nb1hsO8Cq+qE3CBOm5Sp7HgoRpad9TZ1NVATmUifpIvfOEEPYAB9XS2Xk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717654512; c=relaxed/simple; bh=VN2iUoBWnyYiXaKESDXLd0fHPmf0gc5XV6WaeeCq+6w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hvH/eRDyz4L+Wqoh2xr92odOGlye4prFTeY09JcFKOziZyxxqb2wAuE9hcjkEnp5JS1jeu7bvz0it/rsNcJWStmTv5m6iCFY/U9sDgv/1rLlthq8Gb7s3JWJcYiAOlVZhShBclJnv+Zvza3V875lGkp4+6wpiLHymgUe16ptrnk= 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=GmUZMl8z; 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="GmUZMl8z" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1717654501; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=3D88LeRE+6voTWokvHbaC35N34h1htu9n5z6n7Tnn/8=; b=GmUZMl8zVBAcpzJF7OT5APyZWqpSRnKI5WmAO4rbJ8s/KcQa8Vs1BALmPqgsfANDkcWpPyyyat90FaFYxKE2jM+TIqBuOoNzy6wiXf7p6a6Ob6c9aiHCMyptQbc5/2CkOCt3hpqDgHJOxKzbGESHeEBScFa4/8Xs8NVzYAC658w= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R101e4;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_---0W7xDgpZ_1717654494; Received: from localhost(mailfrom:hengqi@linux.alibaba.com fp:SMTPD_---0W7xDgpZ_1717654494) by smtp.aliyun-inc.com; Thu, 06 Jun 2024 14:14:58 +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?= , Eric Dumazet , "David S. Miller" , Jakub Kicinski , Paolo Abeni Subject: [PATCH net-next v3 2/4] virtio_net: enable irq for the control vq Date: Thu, 6 Jun 2024 14:14:44 +0800 Message-Id: <20240606061446.127802-3-hengqi@linux.alibaba.com> X-Mailer: git-send-email 2.32.0.3.g01195cf9f In-Reply-To: <20240606061446.127802-1-hengqi@linux.alibaba.com> References: <20240606061446.127802-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 0f872936d6ed..823a9dca51c1 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; @@ -2723,14 +2732,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(&vi->ctrl->completion); + virtqueue_get_buf(vi->cvq, &tmp); unlock: mutex_unlock(&vi->cvq_lock); @@ -5314,7 +5317,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"; } @@ -5834,6 +5837,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 Thu Jun 6 06:14:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heng Qi X-Patchwork-Id: 13687846 X-Patchwork-Delegate: kuba@kernel.org Received: from out30-111.freemail.mail.aliyun.com (out30-111.freemail.mail.aliyun.com [115.124.30.111]) (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 BB2576E5FD for ; Thu, 6 Jun 2024 06:15:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.111 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717654510; cv=none; b=l/P3fZdrNN218H5yV0ictEMz4dktN5GAYhtOXoG/4PGDfFbO5qxtw2BSN/q6BMDMiN7r5Yqzq3D2IPE68lY9/hbru/4LPnwV+oBhckwOf1QVaDdm7S7d3W3X7ouPyxRXTwxdnMo5C409WOFqCZ+hvwd1NwopPi+fPr4ERwIDPuw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717654510; c=relaxed/simple; bh=zXnfciQQNb2Lfc05fsXzI/NnV4/1qVXdVVKeIiTQvI4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=QULROEHYcEoQfxnUakgEiQ+RcWAWSP6V2Rh8Dj+tGlRQiuXxEvKb+MC9UjyUI+zu6aCizrcDtMxNWgXnEZp+UgbHoZEQaZuXSZTh6vqzqIRTFdTx0V8PAkBIQPkO0kDEnuIEzY/K3iZb+DGb6jT4++rFiWqYkTGobyFG6qLhI6k= 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=RPuve9lA; arc=none smtp.client-ip=115.124.30.111 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="RPuve9lA" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1717654506; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=g7p9CKsxWqJ+feqirR1b+m5bMzTWg9wmNNIV2XQLW18=; b=RPuve9lAKzP39i6/B32QLAn3LynbBl3DJdSKOaYG8jEnxuy+Sq/koO5rXVsZ+wmP/ywcwP0rgBz+mzAuSBrYVcDi6r8TQpSaaQPq5omt5ZVoHGrSGTbVwQGLM44C1cDpLmWh31VoRoePfJfO7AjFseaPY1gYF8vpbynQ62HTJtE= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R991e4;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_---0W7xKiCT_1717654499; Received: from localhost(mailfrom:hengqi@linux.alibaba.com fp:SMTPD_---0W7xKiCT_1717654499) by smtp.aliyun-inc.com; Thu, 06 Jun 2024 14:15:04 +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?= , Eric Dumazet , "David S. Miller" , Jakub Kicinski , Paolo Abeni Subject: [PATCH net-next v3 3/4] virtio_net: change the command token to completion Date: Thu, 6 Jun 2024 14:14:45 +0800 Message-Id: <20240606061446.127802-4-hengqi@linux.alibaba.com> X-Mailer: git-send-email 2.32.0.3.g01195cf9f In-Reply-To: <20240606061446.127802-1-hengqi@linux.alibaba.com> References: <20240606061446.127802-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 823a9dca51c1..e59e12bb7601 100644 --- a/drivers/net/virtio_net.c +++ b/drivers/net/virtio_net.c @@ -2721,7 +2721,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 Thu Jun 6 06:14:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heng Qi X-Patchwork-Id: 13687848 X-Patchwork-Delegate: kuba@kernel.org Received: from out30-124.freemail.mail.aliyun.com (out30-124.freemail.mail.aliyun.com [115.124.30.124]) (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 ACCF96EB7D for ; Thu, 6 Jun 2024 06:15:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717654513; cv=none; b=JIh/Y/hZY/X0se/oz3hy4asbzDPzSGuvp6Gm0Qa2pYur1LW3G51ZIH3Xz10n1yIyRlyhcLBJBa3WqDmAjohkG6mLnxlHAbK4fVYYVLpEl/NoKmyDkIlYxQdlKbXKCkfrrtdKxj3+wTutNJqHwvZAuVZidoTF+bZpcrBxmxByaqY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717654513; c=relaxed/simple; bh=PKb0g69v/9FKzMs1KAIgz/iUsqOg7dtznuWf29QBJv0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JoEPNVhzldjAIqx0wQ3Cy3ttwBwNe9zuV5hUJJwNjCrTVTdlbIixoPWGx1S1CMMBrZ8oAHidRXOvqUgkhYcWj8eHZ2cXTwknX+0G81ig3W0+2CbDgdHEkr3RykyL2m6jjjzUzTx8TL++5ig0FNqr5Altl5Oa0u7BSzYOVIDOKZE= 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=psUBIaQg; arc=none smtp.client-ip=115.124.30.124 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="psUBIaQg" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1717654508; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=c1/BCOXkkmZ7AQu9POA+zpI7WHtkP8Ed+hRmQcJ0r4c=; b=psUBIaQgp3T6w3/t9yqSmaXNhHQyCbuRnD0WlFAEnytkOfCKGV9Sb8dIthsZk3zMzFFyeGbtkZlYYMCOR5So0NIniwqW5AgY0mOjkt+y1D9ujaLS6pg8JdT6ab0YOjt23grHePvs8qlx2kdbvIAVO8Lj4iShnbGHp14li8z9z6Y= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R201e4;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_---0W7xB1w5_1717654505; Received: from localhost(mailfrom:hengqi@linux.alibaba.com fp:SMTPD_---0W7xB1w5_1717654505) by smtp.aliyun-inc.com; Thu, 06 Jun 2024 14:15:07 +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?= , Eric Dumazet , "David S. Miller" , Jakub Kicinski , Paolo Abeni Subject: [PATCH net-next v3 4/4] virtio_net: improve dim command request efficiency Date: Thu, 6 Jun 2024 14:14:46 +0800 Message-Id: <20240606061446.127802-5-hengqi@linux.alibaba.com> X-Mailer: git-send-email 2.32.0.3.g01195cf9f In-Reply-To: <20240606061446.127802-1-hengqi@linux.alibaba.com> References: <20240606061446.127802-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 | 233 ++++++++++++++++++++++++++++++++++----- 1 file changed, 208 insertions(+), 25 deletions(-) diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c index e59e12bb7601..0338528993ab 100644 --- a/drivers/net/virtio_net.c +++ b/drivers/net/virtio_net.c @@ -376,6 +376,13 @@ struct control_buf { struct completion completion; }; +struct virtnet_coal_node { + struct control_buf ctrl; + struct virtio_net_ctrl_coal_vq coal_vqs; + bool is_coal_wait; + struct list_head list; +}; + struct virtnet_info { struct virtio_device *vdev; struct virtqueue *cvq; @@ -420,6 +427,9 @@ 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; + /* Host can handle any s/g split between our header and packet data */ bool any_header_sg; @@ -464,6 +474,14 @@ 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; + + /* Filled when there are no free nodes or cvq buffers */ + struct mutex coal_wait_lock; + struct list_head coal_wait_list; + unsigned long guest_offloads; unsigned long guest_offloads_capable; @@ -670,7 +688,7 @@ static void virtnet_cvq_done(struct virtqueue *cvq) { struct virtnet_info *vi = cvq->vdev->priv; - complete(&vi->ctrl->completion); + schedule_work(&vi->get_cvq); } static void skb_xmit_done(struct virtqueue *vq) @@ -2696,7 +2714,7 @@ static bool virtnet_send_command_reply(struct virtnet_info *vi, struct scatterlist *in) { struct scatterlist *sgs[5], hdr, stat; - u32 out_num = 0, tmp, in_num = 0; + u32 out_num = 0, in_num = 0; int ret; /* Caller should know better */ @@ -2730,14 +2748,14 @@ 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; + } + mutex_unlock(&vi->cvq_lock); - wait_for_completion(&vi->ctrl->completion); - virtqueue_get_buf(vi->cvq, &tmp); + wait_for_completion(&ctrl->completion); -unlock: - mutex_unlock(&vi->cvq_lock); return ctrl->status == VIRTIO_NET_OK; } @@ -2747,6 +2765,86 @@ 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 void virtnet_process_dim_cmd(struct virtnet_info *vi, + struct virtnet_coal_node *node) +{ + u16 qnum = le16_to_cpu(node->coal_vqs.vqn) / 2; + + 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); + vi->rq[qnum].dim.state = DIM_START_MEASURE; + mutex_unlock(&vi->rq[qnum].dim_lock); + + if (node->is_coal_wait) { + mutex_lock(&vi->coal_wait_lock); + list_del(&node->list); + mutex_unlock(&vi->coal_wait_lock); + kfree(node); + } else { + mutex_lock(&vi->coal_free_lock); + list_add(&node->list, &vi->coal_free_list); + mutex_unlock(&vi->coal_free_lock); + } +} + +static int virtnet_add_dim_command(struct virtnet_info *vi, + struct virtnet_coal_node *coal_node) +{ + struct scatterlist sg; + int ret; + + sg_init_one(&sg, &coal_node->coal_vqs, sizeof(coal_node->coal_vqs)); + ret = virtnet_send_command_reply(vi, VIRTIO_NET_CTRL_NOTF_COAL, + VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET, + &coal_node->ctrl, &sg, NULL); + if (!ret) { + dev_warn(&vi->dev->dev, + "Failed to change coalescing params.\n"); + return ret; + } + + virtnet_process_dim_cmd(vi, coal_node); + + return 0; +} + +static void virtnet_get_cvq_work(struct work_struct *work) +{ + struct virtnet_info *vi = + container_of(work, struct virtnet_info, get_cvq); + struct virtnet_coal_node *wait_coal; + bool valid = false; + unsigned int tmp; + void *res; + + mutex_lock(&vi->cvq_lock); + while ((res = virtqueue_get_buf(vi->cvq, &tmp)) != NULL) { + complete((struct completion *)res); + valid = true; + } + mutex_unlock(&vi->cvq_lock); + + if (!valid) + return; + + while (true) { + wait_coal = NULL; + mutex_lock(&vi->coal_wait_lock); + if (!list_empty(&vi->coal_wait_list)) + wait_coal = list_first_entry(&vi->coal_wait_list, + struct virtnet_coal_node, + list); + mutex_unlock(&vi->coal_wait_lock); + if (wait_coal) + if (virtnet_add_dim_command(vi, wait_coal)) + break; + else + break; + } +} static int virtnet_set_mac_address(struct net_device *dev, void *p) { struct virtnet_info *vi = netdev_priv(dev); @@ -4398,35 +4496,73 @@ static int virtnet_send_notf_coal_vq_cmds(struct virtnet_info *vi, return 0; } +static void virtnet_put_wait_coal(struct virtnet_info *vi, + struct receive_queue *rq, + struct dim_cq_moder moder) +{ + struct virtnet_coal_node *wait_node; + + wait_node = kzalloc(sizeof(*wait_node), GFP_KERNEL); + if (!wait_node) { + rq->dim.state = DIM_START_MEASURE; + return; + } + + wait_node->is_coal_wait = true; + wait_node->coal_vqs.vqn = cpu_to_le16(rxq2vq(rq - vi->rq)); + wait_node->coal_vqs.coal.max_usecs = cpu_to_le32(moder.usec); + wait_node->coal_vqs.coal.max_packets = cpu_to_le32(moder.pkts); + mutex_lock(&vi->coal_wait_lock); + list_add_tail(&wait_node->list, &vi->coal_wait_list); + mutex_unlock(&vi->coal_wait_lock); +} + 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); - dim->state = DIM_START_MEASURE; + 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: mutex_unlock(&rq->dim_lock); + + mutex_lock(&vi->cvq_lock); + if (vi->cvq->num_free < 3) { + virtnet_put_wait_coal(vi, rq, update_moder); + mutex_unlock(&vi->cvq_lock); + return; + } + mutex_unlock(&vi->cvq_lock); + + mutex_lock(&vi->coal_free_lock); + if (list_empty(&vi->coal_free_list)) { + virtnet_put_wait_coal(vi, rq, update_moder); + mutex_unlock(&vi->coal_free_lock); + return; + } + + avail_coal = list_first_entry(&vi->coal_free_list, + struct virtnet_coal_node, list); + avail_coal->coal_vqs.vqn = cpu_to_le16(rxq2vq(rq - vi->rq)); + avail_coal->coal_vqs.coal.max_usecs = cpu_to_le32(update_moder.usec); + avail_coal->coal_vqs.coal.max_packets = cpu_to_le32(update_moder.pkts); + + list_del(&avail_coal->list); + mutex_unlock(&vi->coal_free_lock); + + virtnet_add_dim_command(vi, avail_coal); } static int virtnet_coal_params_supported(struct ethtool_coalesce *ec) @@ -4839,6 +4975,7 @@ 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); + flush_work(&vi->get_cvq); netif_tx_lock_bh(vi->dev); netif_device_detach(vi->dev); @@ -5612,6 +5749,45 @@ 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); + + INIT_LIST_HEAD(&vi->coal_wait_list); + mutex_init(&vi->coal_wait_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; + } + list_add(&coal_node->list, &vi->coal_free_list); + } + + return 0; +} + static int virtnet_probe(struct virtio_device *vdev) { int i, err = -ENOMEM; @@ -5797,6 +5973,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; @@ -5838,6 +6017,7 @@ 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_rx_mode_work(vi); @@ -5967,11 +6147,14 @@ static void virtnet_remove(struct virtio_device *vdev) flush_work(&vi->config_work); disable_rx_mode_work(vi); flush_work(&vi->rx_mode_work); + 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);