From patchwork Fri Jun 14 13:55:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luigi Leonardi X-Patchwork-Id: 13698729 X-Patchwork-Delegate: kuba@kernel.org Received: from EUR03-AM7-obe.outbound.protection.outlook.com (mail-am7eur03olkn2057.outbound.protection.outlook.com [40.92.59.57]) (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 C4ADD79E1; Fri, 14 Jun 2024 13:55:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.59.57 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718373358; cv=fail; b=F1y9/lVVSwHe89A2Ff0uN2m7uKkDKIP3nwqOu36vK9cJ+NxvVLL/cCnElAub1qIAHUZhKNRCD07JpFOm5pWvyESK2kT41GkUNNsYpfSSw4IOhnxOGKWiptGOatFcB1eRx/uiU0/SeePOG/T0/PFgbms0vCunjei8NKcv9Jp1TPM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718373358; c=relaxed/simple; bh=3Rm3T5SsfVmBTQpujaAghFy9uNS5aqFg+iT0kp0eA3g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=NiSnG1rrcut6ut3XpIKw+GrBj4ZOfXie/Xhu/wmjD5WPKD6n8nB4QC/fUex35K7gtK0EHoYP/OvCU0wgP+5ivnIkVtQD98Xinb3BRW/BLxRDae/Er0x0LPku7P9JDn5xbi2TQr69qSkmI29eU4TnbET6R836HDTaNZQN2QsnwO8= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=QZVf25RY; arc=fail smtp.client-ip=40.92.59.57 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="QZVf25RY" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=luN9FMda+Tf1GEapjHhJX5Db+Z6NqFuQkalIS847Dr3O1kA65Om4Qf9uMnCBodiY0sIzqYsMqF5fvrqneYY6ZcIsni3ncGzzsP1LpIz7+GyFDn3dJN6eIxAyh+PxoYsq8l+rWH+OvLnAtmfCwBD13O9CKD1zqZHbh9EeAl8ZSQ9+Hhc9uJ4T8JzdOJHAqbL7NVVduqQ3FwiTUsjG83StZxle9DHpCzmHecPgZ3vFtnnid47p9g3owWFIZIQvbOSop2B4zM1Rwji8fsNPfmZPJT63okxpZUr9zBMh1X3qOmtFkg3a84nlbgADnsd1NxHVkaKELDh0KVuVUXC9n3fnQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=1EHNKKz1qX7AszV1M/va7aczipvP8ulNoEi2BKcm4xk=; b=dc/GWq0myc5ELXuajWf7X5KeuM+dVjkOAmCbUBatWLqXmTS1pAHpP5m9VnJzj378qtxA6GwAKCfwk7j5FJUEyUP+JxPmGgSvwVxjHztAVcaNK4ghMNkbJZKXWV974zz9IunmyI25vwHolwjvVHf7WAloblpxLnhuil31sm8dzqpdI6KUSwMPIrNJnMrbyYLcfSIlLgMpBFxeP+SUDz4bAEqfgU0d0m+yZ6Vxo1X/ADea4KLSuMIGkikAcs8sXU7dTSuUv1F8iMAbzUw0qgYojr1t27H8x0AMOam7BzPX0nmJPs1O0rTznlDbrVMkBAvUeyieBvT4rCURbslhsKqbww== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=1EHNKKz1qX7AszV1M/va7aczipvP8ulNoEi2BKcm4xk=; b=QZVf25RY9Iu8HHq5Uu2LWFysVKZbIqiTlHfBS8NUE3UalTgJlfgpE+GJuzV8kGkm8arVjRXwUl0f9yhIB8tKREw+/ndfVHr2+fZ+oPqdOjR983ENIB9JHj6kJsQ/AdRnnC3Vs4NM3gqhDxO1Q/DW0gnXe4B5sj4sNEK0eArs9eO6y0vnlI2+sh4HbVUaWOBE3ZpDHUur1hjgFSln/Id4c2aldJISfQUNZtVwCgPqbJbIDnQkrJLrtrdRzIIRkkBcQfYExiBGetro4UV6+hUuBNzVE3vEdj1u8izONhU8P5CeV8hawpDMOvPhhIEujbVypth5lTmXvTAgsPESlitkdg== Received: from AS2P194MB2170.EURP194.PROD.OUTLOOK.COM (2603:10a6:20b:642::8) by PAXP194MB1469.EURP194.PROD.OUTLOOK.COM (2603:10a6:102:1a8::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7677.24; Fri, 14 Jun 2024 13:55:50 +0000 Received: from AS2P194MB2170.EURP194.PROD.OUTLOOK.COM ([fe80::3d63:e123:2c2f:c930]) by AS2P194MB2170.EURP194.PROD.OUTLOOK.COM ([fe80::3d63:e123:2c2f:c930%3]) with mapi id 15.20.7677.024; Fri, 14 Jun 2024 13:55:50 +0000 From: Luigi Leonardi To: sgarzare@redhat.com, edumazet@google.com, virtualization@lists.linux.dev, netdev@vger.kernel.org, kuba@kernel.org, kvm@vger.kernel.org, stefanha@redhat.com, pabeni@redhat.com, davem@davemloft.net Cc: Marco Pinna , Luigi Leonardi Subject: [PATCH net-next 1/2] vsock/virtio: refactor virtio_transport_send_pkt_work Date: Fri, 14 Jun 2024 15:55:42 +0200 Message-ID: X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240614135543.31515-1-luigi.leonardi@outlook.com> References: <20240614135543.31515-1-luigi.leonardi@outlook.com> X-TMN: [cNCghYFMJvywD3gy6YkNRs4FzLxH2tBp] X-ClientProxiedBy: MI0P293CA0015.ITAP293.PROD.OUTLOOK.COM (2603:10a6:290:44::8) To AS2P194MB2170.EURP194.PROD.OUTLOOK.COM (2603:10a6:20b:642::8) X-Microsoft-Original-Message-ID: <20240614135543.31515-2-luigi.leonardi@outlook.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AS2P194MB2170:EE_|PAXP194MB1469:EE_ X-MS-Office365-Filtering-Correlation-Id: 9fa704b6-34e9-4a06-4c8f-08dc8c79b356 X-Microsoft-Antispam: BCL:0;ARA:14566002|461199025|3412199022|440099025|1710799023; X-Microsoft-Antispam-Message-Info: EiLleIyiDG3SbbW9ozssg7t9Dzbe3pryXXHsFP9g4j75kttud71blyxad0/QEZJdveh81rorF52tQ7iPysExscxKW/pWhmzpR6M1taYfAkBBGF3RaWIhtjnKYSFRKdh/+Uxnlebrcx1JJNwEt13Zu5uTArkS5ihaWKK+QE30cx0gxfrWTCZEv3xONh3CkIaUiUQr+bMYpua4jrGG7473s7+tL+oU1u1ku1EDiOuYcaRJpDWkThwgrxK78qwmDThUJKh7I+c66odQI58xZEh+flZGISRzrFfyV9LpcAM128xz8GcUZRb+3row5TJ0FZpHLp4TmOgqNhRkKLsmt+aUVqR1cY2RqqtdywdHlIDfkR8bSMsTrDaDL4io572ZzE58sLT+YUzhVLFqFyA+HklIpc+LvJwZHV9TK8PSoCph2vwkG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inhOntZqFE8OMINxSm7qcahI2DeQ5HxMSjCTzBEu0/alZYTTQe1rPLXPhP/TeugqwGOM3oT0bTu0+j8+Aay5IjLkahBN2I5qLkweU9HG+LuTF77Rpj3IwMn6gruDD96BMPW/OgrZY1YtmaczF9AcsbEv41wi0xvqKwwyeLDiMBJTPTiX2Py+px+U8XpMQF8b6C1Fy2mRE3Rym3SUuaNtkjz2a+lqnObWxuJ7Fbx9X67Y0SJIyOhip2e8EnwNw+ZSQAeeCLm14a8Q0zTCtgB3PcX2rNZIjvEkXzlDX0Ad7z0E5N8JRHdpDI2qgWHXHeMwNDQroaI3wyGTPj/bKg3VGKkYDRhgjrUivuJZt3iVxECx+Q8rLtfq27QAUHWmySLmnsmv2duOSKBSyv1wcMtvGbXwgugnpsE1BCtItdl8pBPyb/t1NctdYUfb5HUKP738hxM/hsu4jaeeTjp+44X X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9fa704b6-34e9-4a06-4c8f-08dc8c79b356 X-MS-Exchange-CrossTenant-AuthSource: AS2P194MB2170.EURP194.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Jun 2024 13:55:50.9054 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAXP194MB1469 X-Patchwork-Delegate: kuba@kernel.org From: Marco Pinna This is a preliminary patch to introduce an optimization to the enqueue system. All the code used to enqueue a packet into the virtqueue is removed from virtio_transport_send_pkt_work() and moved to the new virtio_transport_send_skb() function. Co-developed-by: Luigi Leonardi Signed-off-by: Luigi Leonardi Signed-off-by: Marco Pinna --- net/vmw_vsock/virtio_transport.c | 134 +++++++++++++++++-------------- 1 file changed, 74 insertions(+), 60 deletions(-) diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c index 43d405298857..c930235ecaec 100644 --- a/net/vmw_vsock/virtio_transport.c +++ b/net/vmw_vsock/virtio_transport.c @@ -94,6 +94,78 @@ static u32 virtio_transport_get_local_cid(void) return ret; } +/* Caller need to hold vsock->tx_lock on vq */ +static int virtio_transport_send_skb(struct sk_buff *skb, struct virtqueue *vq, + struct virtio_vsock *vsock, bool *restart_rx) +{ + int ret, in_sg = 0, out_sg = 0; + struct scatterlist **sgs; + bool reply; + + reply = virtio_vsock_skb_reply(skb); + sgs = vsock->out_sgs; + sg_init_one(sgs[out_sg], virtio_vsock_hdr(skb), + sizeof(*virtio_vsock_hdr(skb))); + out_sg++; + + if (!skb_is_nonlinear(skb)) { + if (skb->len > 0) { + sg_init_one(sgs[out_sg], skb->data, skb->len); + out_sg++; + } + } else { + struct skb_shared_info *si; + int i; + + /* If skb is nonlinear, then its buffer must contain + * only header and nothing more. Data is stored in + * the fragged part. + */ + WARN_ON_ONCE(skb_headroom(skb) != sizeof(*virtio_vsock_hdr(skb))); + + si = skb_shinfo(skb); + + for (i = 0; i < si->nr_frags; i++) { + skb_frag_t *skb_frag = &si->frags[i]; + void *va; + + /* We will use 'page_to_virt()' for the userspace page + * here, because virtio or dma-mapping layers will call + * 'virt_to_phys()' later to fill the buffer descriptor. + * We don't touch memory at "virtual" address of this page. + */ + va = page_to_virt(skb_frag_page(skb_frag)); + sg_init_one(sgs[out_sg], + va + skb_frag_off(skb_frag), + skb_frag_size(skb_frag)); + out_sg++; + } + } + + ret = virtqueue_add_sgs(vq, sgs, out_sg, in_sg, skb, GFP_KERNEL); + /* Usually this means that there is no more space available in + * the vq + */ + if (ret < 0) + goto out; + + virtio_transport_deliver_tap_pkt(skb); + + if (reply) { + struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX]; + int val; + + val = atomic_dec_return(&vsock->queued_replies); + + /* Do we now have resources to resume rx processing? */ + if (val + 1 == virtqueue_get_vring_size(rx_vq)) + *restart_rx = true; + } + +out: + return ret; +} + static void virtio_transport_send_pkt_work(struct work_struct *work) { @@ -111,77 +183,19 @@ virtio_transport_send_pkt_work(struct work_struct *work) vq = vsock->vqs[VSOCK_VQ_TX]; for (;;) { - int ret, in_sg = 0, out_sg = 0; - struct scatterlist **sgs; struct sk_buff *skb; - bool reply; + int ret; skb = virtio_vsock_skb_dequeue(&vsock->send_pkt_queue); if (!skb) break; - reply = virtio_vsock_skb_reply(skb); - sgs = vsock->out_sgs; - sg_init_one(sgs[out_sg], virtio_vsock_hdr(skb), - sizeof(*virtio_vsock_hdr(skb))); - out_sg++; - - if (!skb_is_nonlinear(skb)) { - if (skb->len > 0) { - sg_init_one(sgs[out_sg], skb->data, skb->len); - out_sg++; - } - } else { - struct skb_shared_info *si; - int i; + ret = virtio_transport_send_skb(skb, vq, vsock, &restart_rx); - /* If skb is nonlinear, then its buffer must contain - * only header and nothing more. Data is stored in - * the fragged part. - */ - WARN_ON_ONCE(skb_headroom(skb) != sizeof(*virtio_vsock_hdr(skb))); - - si = skb_shinfo(skb); - - for (i = 0; i < si->nr_frags; i++) { - skb_frag_t *skb_frag = &si->frags[i]; - void *va; - - /* We will use 'page_to_virt()' for the userspace page - * here, because virtio or dma-mapping layers will call - * 'virt_to_phys()' later to fill the buffer descriptor. - * We don't touch memory at "virtual" address of this page. - */ - va = page_to_virt(skb_frag_page(skb_frag)); - sg_init_one(sgs[out_sg], - va + skb_frag_off(skb_frag), - skb_frag_size(skb_frag)); - out_sg++; - } - } - - ret = virtqueue_add_sgs(vq, sgs, out_sg, in_sg, skb, GFP_KERNEL); - /* Usually this means that there is no more space available in - * the vq - */ if (ret < 0) { virtio_vsock_skb_queue_head(&vsock->send_pkt_queue, skb); break; } - - virtio_transport_deliver_tap_pkt(skb); - - if (reply) { - struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX]; - int val; - - val = atomic_dec_return(&vsock->queued_replies); - - /* Do we now have resources to resume rx processing? */ - if (val + 1 == virtqueue_get_vring_size(rx_vq)) - restart_rx = true; - } - added = true; } From patchwork Fri Jun 14 13:55:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luigi Leonardi X-Patchwork-Id: 13698730 X-Patchwork-Delegate: kuba@kernel.org Received: from EUR03-AM7-obe.outbound.protection.outlook.com (mail-am7eur03olkn2057.outbound.protection.outlook.com [40.92.59.57]) (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 D15FABE65; Fri, 14 Jun 2024 13:55:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.92.59.57 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718373360; cv=fail; b=gpkcIzoTo3Lrt2GheGa3mb3POlN1Dm5htcgHJRU0xa1IDszohdBg/bhtl7Zqa8cyv69kdoV9uODupUa7toiRuhzr+z7NGT5TRsNUoPE9+7r875pjSZ5dz2UaadCHNIG1cmYktzV6T9ORReCVHeoKid7LNbkYRYRheelZ2QSapjg= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718373360; c=relaxed/simple; bh=PbcR8chyGrvRobCuehcXslLl+Q50d6BejioPRAdAJOM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=hJAIiv35YQLCeSdKUDHSYg7MVXggAS6GfYauyMxF616sgXMRa8V6Av7ZkZMahcJZIHpk5iVjSjCC2pkV2vMVe7g/EKJGAyYXFiqYr5a7HZ+e9BpM2vV+No+yRfcXBZCr5EhHQqhxVwS9ms6A6ZXrZCribfXqPOfMf/8VqE31DGg= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=dLfUj49/; arc=fail smtp.client-ip=40.92.59.57 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="dLfUj49/" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=E2DaS/H4saj1To0E2acGxi9jQxMWbt2h3MTmV8TzgP7IRyVGzFAEKdk7ar4yOec9XFsXvf3C7eAVDBJf+27cDiWizU7MZwIigGldJElLXEOc9hoIBgWrwGEcIuFHSwNyb/SIIOHFjyWytu34Ls7fjyNYsFq/5WbpXKyJDjG841ghaCncL5I8d9ymFL13b8yiVPflN+v8ogoHEjqzXNE18t8HfuLjwFftfDym8f52JQzRn/sSWOAHtoCsSi64ZO8YRzLvFAE4Qb6oEFWRRh5pC4xtRJrkFlbD5kHKSv7BJ4zS7dKSau/SzymB9vVqjJNyIf+aNIEIPTgXuBT60CkgZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Vbq4SBZ24qmRTbz8LqgCwYGwTDk8D5Sa4oWquQYwEhM=; b=SLlDDuglFE/1LZjte4Y48COo1ICmM1G8iWWGiIzpzhI6rC5zqfU4DQJG/NtVpngm5B8Y14ht9HB1TdrgkIcKBVkmFXcKDitK2B4cCanVzcHgv4BL3gxyc7auAhnMGawQPUos7dI7JtRvG7lWKZxHutM0eIowWYkHduUkyEyerW4T4ulbsHM6Fkiq+H6IXasVDXKwBxdBcAMQqc5EmupHT7/K1K6Elxwyzpl7NRWUls11GcEpZHYmr/JYfn39VoxyJ1zk/cz9GbmmRgbCDtwiaDWJtQOgciKaA/5yMrLffx4EN15769WAlr5qJ3qebdGp+K4UZ6TRkqxckSuvmhLzOg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Vbq4SBZ24qmRTbz8LqgCwYGwTDk8D5Sa4oWquQYwEhM=; b=dLfUj49/C5ACfMGcm2Rkvk1bXciMnwKmjzKs+MveUT+BPo1Sat3EHFQbdnNTFYpIYQ/m0MmMwtMBZm8DeAGjVJe2sdi4+oW463rfzL+EJzL/lJ7co5n1QzKn2pwiZw5jJRMyI+Cn7tlmLLnEFKquTEtZ7HU2ZZfBC1wt0BSAKfTncRDRM0SLrGSiWLjFsPdPimJlFaNfY0hGUeW6FY9+bKBjdOW24z6QFuCt1DrFwTCFA1aDa6Pd9osC5Jvomv3uGYLrG/WOhFsuEbEmKkWKLFP3ervGf28H3W8KqTBGv0kgxd4jMGqMQNH62zcP0Apvd/5B1xJWI0K3NPQh7bldyg== Received: from AS2P194MB2170.EURP194.PROD.OUTLOOK.COM (2603:10a6:20b:642::8) by PAXP194MB1469.EURP194.PROD.OUTLOOK.COM (2603:10a6:102:1a8::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7677.24; Fri, 14 Jun 2024 13:55:51 +0000 Received: from AS2P194MB2170.EURP194.PROD.OUTLOOK.COM ([fe80::3d63:e123:2c2f:c930]) by AS2P194MB2170.EURP194.PROD.OUTLOOK.COM ([fe80::3d63:e123:2c2f:c930%3]) with mapi id 15.20.7677.024; Fri, 14 Jun 2024 13:55:51 +0000 From: Luigi Leonardi To: sgarzare@redhat.com, edumazet@google.com, virtualization@lists.linux.dev, netdev@vger.kernel.org, kuba@kernel.org, kvm@vger.kernel.org, stefanha@redhat.com, pabeni@redhat.com, davem@davemloft.net Cc: Marco Pinna , Luigi Leonardi Subject: [PATCH net-next 2/2] vsock/virtio: avoid enqueue packets when work queue is empty Date: Fri, 14 Jun 2024 15:55:43 +0200 Message-ID: X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240614135543.31515-1-luigi.leonardi@outlook.com> References: <20240614135543.31515-1-luigi.leonardi@outlook.com> X-TMN: [zmAx0oZ3aW9aLNZjpKtjFc1D5puwy8oS] X-ClientProxiedBy: MI0P293CA0015.ITAP293.PROD.OUTLOOK.COM (2603:10a6:290:44::8) To AS2P194MB2170.EURP194.PROD.OUTLOOK.COM (2603:10a6:20b:642::8) X-Microsoft-Original-Message-ID: <20240614135543.31515-3-luigi.leonardi@outlook.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AS2P194MB2170:EE_|PAXP194MB1469:EE_ X-MS-Office365-Filtering-Correlation-Id: 76ae548f-9d8b-46db-c92a-08dc8c79b3b9 X-Microsoft-Antispam: BCL:0;ARA:14566002|461199025|3412199022|440099025|1710799023; X-Microsoft-Antispam-Message-Info: WW/B1PvIk5+eqfQhx3R2kSqmMKWbQevi5x8gs1W3/3v/3NrEFRxD2ig37z7hTazVM2HqZizQKLNnLfQS8+ZOhVx/ZODmeNnqpwyIjddVTLqB3uVUR99KMHSHODY4syjMDpIouP5hAM9IGX9Tz50G7YX8+KYWMN9otMLm6N5f9I9yUQ0yfoqgG2qDs+vQ9MT8REdNFQrn8T7QSNLNnTdt1ylMF9zT+bPqNPrrG0tDA+Em0d1d0/iI5Xn1KbRVbzoFO9npNsfeM1a4ge9F0R6I/c7lBsqy4X8GIsLURSxi+sFHn+/vnV0/HaiNZ4ZWY+fotIWHO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vNxdvN7ACxLgOndpO8sZqF+jzH+7jcNYzvI1Xx/I+h0iXQHGJp6zvzOsGSX/3UPXG5M+pgwkOtIw2S9IB6r6vbCv3lIfphdfgsp6LXsleAWqO4JMhlJnIjZp5DX/bPL5DekyGHZBV7zIbOKId4JUe9/CNkIraT+8oDo7Mw/cPKlQVdCJtYtUsD3ZDoLDtpgaSsKx3Tuf5spBQhdTuNDMjMjcNb/fxve8Y+qpl7ojrkwXDppELXkxPvu0kCzj//4KFLCx9gN/OKgrQsCJxgLnEBlqwI42Yb/1LYTk1HPyyXXrRUrE/oQj83EzEsmz/cPLBN9GDKOypfsaBFWZMDWgL9QfF+Xv2mb2DvvxoW8poBwZTakFKmw+lYQBJG8uu4opw6uIdqldUM2Uv9d2iRWWnMSANzZnUGVM6UPRseGCFPo2CqmMAlvd1UI2RIPSIo4UXUXMZB55c5EQ8219GE+CSrMBbOuqj6Lg7Qfa7psfSkH2aFY2Z8gK5xsBTeedydfh0ZAthGOXXae6M46vMVr8PrXI/Cs8ibFQroIO2RmcOzRba2sgKKapYxDj/V/S X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 76ae548f-9d8b-46db-c92a-08dc8c79b3b9 X-MS-Exchange-CrossTenant-AuthSource: AS2P194MB2170.EURP194.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Jun 2024 13:55:51.5906 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAXP194MB1469 X-Patchwork-Delegate: kuba@kernel.org From: Marco Pinna This introduces an optimization in virtio_transport_send_pkt: when the work queue (send_pkt_queue) is empty the packet is put directly in the virtqueue reducing latency. In the following benchmark (pingpong mode) the host sends a payload to the guest and waits for the same payload back. Tool: Fio version 3.37-56 Env: Phys host + L1 Guest Payload: 4k Runtime-per-test: 50s Mode: pingpong (h-g-h) Test runs: 50 Type: SOCK_STREAM Before (Linux 6.8.11) ------ mean(1st percentile): 722.45 ns mean(overall): 1686.23 ns mean(99th percentile): 35379.27 ns After ------ mean(1st percentile): 602.62 ns mean(overall): 1248.83 ns mean(99th percentile): 17557.33 ns Co-developed-by: Luigi Leonardi Signed-off-by: Luigi Leonardi Signed-off-by: Marco Pinna --- net/vmw_vsock/virtio_transport.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c index c930235ecaec..e89bf87282b2 100644 --- a/net/vmw_vsock/virtio_transport.c +++ b/net/vmw_vsock/virtio_transport.c @@ -214,7 +214,9 @@ virtio_transport_send_pkt(struct sk_buff *skb) { struct virtio_vsock_hdr *hdr; struct virtio_vsock *vsock; + bool use_worker = true; int len = skb->len; + int ret = -1; hdr = virtio_vsock_hdr(skb); @@ -235,8 +237,34 @@ virtio_transport_send_pkt(struct sk_buff *skb) if (virtio_vsock_skb_reply(skb)) atomic_inc(&vsock->queued_replies); - virtio_vsock_skb_queue_tail(&vsock->send_pkt_queue, skb); - queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work); + /* If the send_pkt_queue is empty there is no need to enqueue the packet. + * Just put it on the ringbuff using virtio_transport_send_skb. + */ + + if (skb_queue_empty_lockless(&vsock->send_pkt_queue)) { + bool restart_rx = false; + struct virtqueue *vq; + + mutex_lock(&vsock->tx_lock); + + vq = vsock->vqs[VSOCK_VQ_TX]; + + ret = virtio_transport_send_skb(skb, vq, vsock, &restart_rx); + if (ret == 0) { + use_worker = false; + virtqueue_kick(vq); + } + + mutex_unlock(&vsock->tx_lock); + + if (restart_rx) + queue_work(virtio_vsock_workqueue, &vsock->rx_work); + } + + if (use_worker) { + virtio_vsock_skb_queue_tail(&vsock->send_pkt_queue, skb); + queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work); + } out_rcu: rcu_read_unlock();