From patchwork Fri Jul 26 09:58:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sahil Siddiq X-Patchwork-Id: 13742547 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 754D7C3DA49 for ; Fri, 26 Jul 2024 09:59:37 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sXHir-0005gv-2i; Fri, 26 Jul 2024 05:58:49 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sXHip-0005eI-PC for qemu-devel@nongnu.org; Fri, 26 Jul 2024 05:58:47 -0400 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sXHin-0006N8-Ua for qemu-devel@nongnu.org; Fri, 26 Jul 2024 05:58:47 -0400 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1fc611a0f8cso3985315ad.2 for ; Fri, 26 Jul 2024 02:58:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721987924; x=1722592724; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=sVvw9f50xTkuvlwY1MV4CHABenmr7Ci+oOmj5DJaqa4=; b=QqRBX9gTrGGM4opiVUhCh/q4cw2f4KyhfGTzdNziNkl+/5tLdscrZ76Ho1Xe8EuzVm D5zwe2VuVcwPTEFrtysW/Ic3UaD/CHWh0d5VrPQ9mV/ET4LUUjxz69RpbckXzdFrSNBK HFe5xVvQnISGPz6lzf3RiTyroO/1+6dpaJd2KsmU2V+x7qlHC6bkWUB2CyYndb9vGNRe miZ26P+lR+24hLpox+f/iUDd9uOsxu8rA/bNm6T56awU3/OrHa+l5UATt6qPc5g/nscv i5/pq0Yhv9Ku6BWgWJbJowDKtOwum19p+DrAf9zhENUQns+CG6TstptGH5oWff1PD5oA /n7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721987924; x=1722592724; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sVvw9f50xTkuvlwY1MV4CHABenmr7Ci+oOmj5DJaqa4=; b=VSzgLZEwlk0P6jG6uvWf6VszCpAm+a5sKKB0gKaLoumT1wTGE88Es4uDRAFpFf3nE1 +o21r6CUMlC98XWwkC8UrMAvWbZC1lSgJwvkBJ8WA5RjZERF3IXyLyJrzi36swaICE7P djg+9Qc5GKHHWdlu7UzXWHTUPyjZJgZRdAhMMVss9B814Fv/fTwgjNh1FGOCyYvtLBjw vHMOnU0ZBtNMfgYp0+dTKr3bmv1H4uFKGPRomonZAqXSVXgMhHMCJK8gCK23BCLxq4Fk veV80eNQ+BuNcZ2mU5sYj9am3WMh/KVR5ZnxB9zpsiu4tEecOAyh+8uI76ge2FiaDfW9 08yw== X-Forwarded-Encrypted: i=1; AJvYcCWrl8+TJrqTDtKvTkeg/4uLfZvzbErI91YqRXrQuMGnZ8RUKmyhpTtzrOtXHVDDkO7Oo+9wgpqu1eChi5d7iDf+9dZ0+B8= X-Gm-Message-State: AOJu0YyOw6yWxvTk42AVIFfCv65dCGbbVNaGGE1iDV64EUfCsxYGh8NW m52vXMLQ7sHewv88+psNIJ1Zgtj0ps9UVg6owyg6ui7IoF0ZkCc2Z5FVSw== X-Google-Smtp-Source: AGHT+IH1TkDBtKDFvI5jcyXmLrrOcOzNDBrTCqjT4hikTpMOYVdrRYDnL/f4bshWsFb2R23VDnwiEw== X-Received: by 2002:a17:902:ec91:b0:1fd:8904:ecf6 with SMTP id d9443c01a7336-1fed9259301mr46266625ad.21.1721987924328; Fri, 26 Jul 2024 02:58:44 -0700 (PDT) Received: from valdaarhun.. ([223.233.82.156]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fed7fcd252sm28201655ad.285.2024.07.26.02.58.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 02:58:43 -0700 (PDT) From: Sahil Siddiq X-Google-Original-From: Sahil Siddiq To: eperezma@redhat.com, sgarzare@redhat.com Cc: mst@redhat.com, qemu-devel@nongnu.org, Sahil Siddiq Subject: [RFC v2 1/3] vhost: Introduce packed vq and add buffer elements Date: Fri, 26 Jul 2024 15:28:20 +0530 Message-ID: <20240726095822.104017-2-sahilcdq@proton.me> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240726095822.104017-1-sahilcdq@proton.me> References: <20240726095822.104017-1-sahilcdq@proton.me> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62d; envelope-from=icegambit91@gmail.com; helo=mail-pl1-x62d.google.com X-Spam_score_int: 15 X-Spam_score: 1.5 X-Spam_bar: + X-Spam_report: (1.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_SBL_CSS=3.335, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org This is the first patch in a series to add support for packed virtqueues in vhost_shadow_virtqueue. This patch implements the insertion of available buffers in the descriptor area. It takes into account descriptor chains, but does not consider indirect descriptors. Signed-off-by: Sahil Siddiq --- Changes v1 -> v2: * Split commit from RFC v1 into two commits. * vhost-shadow-virtqueue.c (vhost_svq_add_packed): - Merge with "vhost_svq_vring_write_descs_packed()" - Remove "num == 0" check hw/virtio/vhost-shadow-virtqueue.c | 93 +++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-virtqueue.c index fc5f408f77..c7b7e0c477 100644 --- a/hw/virtio/vhost-shadow-virtqueue.c +++ b/hw/virtio/vhost-shadow-virtqueue.c @@ -217,6 +217,91 @@ static bool vhost_svq_add_split(VhostShadowVirtqueue *svq, return true; } +static bool vhost_svq_add_packed(VhostShadowVirtqueue *svq, + const struct iovec *out_sg, size_t out_num, + const struct iovec *in_sg, size_t in_num, + unsigned *head) +{ + bool ok; + uint16_t head_flags = 0; + g_autofree hwaddr *sgs = g_new(hwaddr, out_num + in_num); + + *head = svq->vring_packed.next_avail_idx; + + /* We need some descriptors here */ + if (unlikely(!out_num && !in_num)) { + qemu_log_mask(LOG_GUEST_ERROR, + "Guest provided element with no descriptors"); + return false; + } + + uint16_t id, curr, i; + unsigned n; + struct vring_packed_desc *descs = svq->vring_packed.vring.desc; + + i = *head; + id = svq->free_head; + curr = id; + + size_t num = out_num + in_num; + + ok = vhost_svq_translate_addr(svq, sgs, out_sg, out_num); + if (unlikely(!ok)) { + return false; + } + + ok = vhost_svq_translate_addr(svq, sgs + out_num, in_sg, in_num); + if (unlikely(!ok)) { + return false; + } + + /* Write descriptors to SVQ packed vring */ + for (n = 0; n < num; n++) { + uint16_t flags = cpu_to_le16(svq->vring_packed.avail_used_flags | + (n < out_num ? 0 : VRING_DESC_F_WRITE) | + (n + 1 == num ? 0 : VRING_DESC_F_NEXT)); + if (i == *head) { + head_flags = flags; + } else { + descs[i].flags = flags; + } + + descs[i].addr = cpu_to_le64(sgs[n]); + descs[i].id = id; + if (n < out_num) { + descs[i].len = cpu_to_le32(out_sg[n].iov_len); + } else { + descs[i].len = cpu_to_le32(in_sg[n - out_num].iov_len); + } + + curr = cpu_to_le16(svq->desc_next[curr]); + + if (++i >= svq->vring_packed.vring.num) { + i = 0; + svq->vring_packed.avail_used_flags ^= + 1 << VRING_PACKED_DESC_F_AVAIL | + 1 << VRING_PACKED_DESC_F_USED; + } + } + + if (i <= *head) { + svq->vring_packed.avail_wrap_counter ^= 1; + } + + svq->vring_packed.next_avail_idx = i; + svq->free_head = curr; + + /* + * A driver MUST NOT make the first descriptor in the list + * available before all subsequent descriptors comprising + * the list are made available. + */ + smp_wmb(); + svq->vring_packed.vring.desc[*head].flags = head_flags; + + return true; +} + static void vhost_svq_kick(VhostShadowVirtqueue *svq) { bool needs_kick; @@ -258,7 +343,13 @@ int vhost_svq_add(VhostShadowVirtqueue *svq, const struct iovec *out_sg, return -ENOSPC; } - ok = vhost_svq_add_split(svq, out_sg, out_num, in_sg, in_num, &qemu_head); + if (virtio_vdev_has_feature(svq->vdev, VIRTIO_F_RING_PACKED)) { + ok = vhost_svq_add_packed(svq, out_sg, out_num, + in_sg, in_num, &qemu_head); + } else { + ok = vhost_svq_add_split(svq, out_sg, out_num, + in_sg, in_num, &qemu_head); + } if (unlikely(!ok)) { return -EINVAL; } From patchwork Fri Jul 26 09:58:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sahil Siddiq X-Patchwork-Id: 13742546 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E66FCC3DA49 for ; Fri, 26 Jul 2024 09:59:19 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sXHiz-0005tu-T1; Fri, 26 Jul 2024 05:58:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sXHiy-0005sw-BM for qemu-devel@nongnu.org; Fri, 26 Jul 2024 05:58:56 -0400 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sXHiw-0006P2-O8 for qemu-devel@nongnu.org; Fri, 26 Jul 2024 05:58:56 -0400 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1fc491f9b55so4623475ad.3 for ; Fri, 26 Jul 2024 02:58:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721987933; x=1722592733; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=BY83cplx/oeP6dLfAfpBBdfnuLxlOhXtCHD6lqzeXbU=; b=fZZNK5nvOLKdYMjTp1nX0Va8m1Cm9eNEK3LXEQmbhqMc/Up3CuVRPwACgO3tbQHsCN 5TzM9wAvMX/sOjLc+udDEKaXYrDNdaq1MlDK636KPrwQhjZXrkmjw0640nklgBocQvay o2MJgWRjObDoo+/R55QaBEqEWUhByu+1SjYAFJoiT0nYLopMkYfFnUG8PSgJrTiJLOV2 27ZKaD1DzGioCtFL3dtgBOili/RTo41owVTgOdETgGS9K6YOC4m+yJ1ueye0Dt1/67W2 CRJnPkr7oN5gADWU5TwAUJW/y/G4VRIgjDJ/V+rPO+8J6BWv71ri7K7I5PK7tk+e5laL lluw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721987933; x=1722592733; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BY83cplx/oeP6dLfAfpBBdfnuLxlOhXtCHD6lqzeXbU=; b=qYCleBtlI6I/f385HQoQAGnYN9MjuzElsI3eZ5Y55ZsyInR+pIIi5voChgu3IrDYbA L//WzoJdqbCb1fCUu3ypSBx8TXz8QOcvbGtLsRCeN8jBz76YUhNAToiiq/myZLSq1qFh P5d7xGVrSHfTLnq2blFa9zt1uhJJEKU+omqNru6pXXrcfBc2MNzU4jb/o27RW8yTe6om pQj3roSBv7wdEAuMsFFiZBgjRB/VOwEXrEqMpvcL7240A8QW5D7ap0gnJ9ZJQFswdJkv n14TPRqhMf4cWq5pldqn9M/EHlCNfQXtwUlxi76pMEK371hi8Cc7xV02MzsqVJYMy0mo OsAA== X-Forwarded-Encrypted: i=1; AJvYcCVhZn55L+7nmJn6XbN3zsp4KkHQzpRXDdJOwik3k4jO2SDkZc8pgR7Jubs7eGscn8qctQpOalpUIXWV3pqtE/XAnhJKbao= X-Gm-Message-State: AOJu0Yy8mxsQHT2GDDChNCBnU/Yx3ZHgu8VnvvYszfoTLieVGDDbtyxh y1lkYPQjwiHijgjah4j+QN/nDlMmdFU9xPbEOuCXTxz1/NNgMYmS X-Google-Smtp-Source: AGHT+IE5nSpI7Zn9iO9PAyLzBnKXZgUqBaUwZsF4WibbpIERNug29gRQTKAxjv+Z4v05R2kedWGRXw== X-Received: by 2002:a17:902:da91:b0:1fb:81ec:26e5 with SMTP id d9443c01a7336-1fed922938fmr44604845ad.28.1721987933171; Fri, 26 Jul 2024 02:58:53 -0700 (PDT) Received: from valdaarhun.. ([223.233.82.156]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fed7fcd252sm28201655ad.285.2024.07.26.02.58.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 02:58:52 -0700 (PDT) From: Sahil Siddiq X-Google-Original-From: Sahil Siddiq To: eperezma@redhat.com, sgarzare@redhat.com Cc: mst@redhat.com, qemu-devel@nongnu.org, Sahil Siddiq Subject: [RFC v2 2/3] vhost: Data structure changes to support packed vqs Date: Fri, 26 Jul 2024 15:28:21 +0530 Message-ID: <20240726095822.104017-3-sahilcdq@proton.me> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240726095822.104017-1-sahilcdq@proton.me> References: <20240726095822.104017-1-sahilcdq@proton.me> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62f; envelope-from=icegambit91@gmail.com; helo=mail-pl1-x62f.google.com X-Spam_score_int: 15 X-Spam_score: 1.5 X-Spam_bar: + X-Spam_report: (1.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_SBL_CSS=3.335, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Introduce "struct vring_packed". Modify VhostShadowVirtqueue so it can support split and packed virtqueue formats. Signed-off-by: Sahil Siddiq --- No changes since v1. hw/virtio/vhost-shadow-virtqueue.h | 66 ++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 22 deletions(-) diff --git a/hw/virtio/vhost-shadow-virtqueue.h b/hw/virtio/vhost-shadow-virtqueue.h index 19c842a15b..ee1a87f523 100644 --- a/hw/virtio/vhost-shadow-virtqueue.h +++ b/hw/virtio/vhost-shadow-virtqueue.h @@ -46,10 +46,53 @@ typedef struct VhostShadowVirtqueueOps { VirtQueueAvailCallback avail_handler; } VhostShadowVirtqueueOps; +struct vring_packed { + /* Actual memory layout for this queue. */ + struct { + unsigned int num; + struct vring_packed_desc *desc; + struct vring_packed_desc_event *driver; + struct vring_packed_desc_event *device; + } vring; + + /* Avail used flags. */ + uint16_t avail_used_flags; + + /* Index of the next avail descriptor. */ + uint16_t next_avail_idx; + + /* Driver ring wrap counter */ + bool avail_wrap_counter; +}; + /* Shadow virtqueue to relay notifications */ typedef struct VhostShadowVirtqueue { + /* Virtio queue shadowing */ + VirtQueue *vq; + + /* Virtio device */ + VirtIODevice *vdev; + + /* SVQ vring descriptors state */ + SVQDescState *desc_state; + + /* + * Backup next field for each descriptor so we can recover securely, not + * needing to trust the device access. + */ + uint16_t *desc_next; + + /* Next free descriptor */ + uint16_t free_head; + + /* Size of SVQ vring free descriptors */ + uint16_t num_free; + /* Shadow vring */ - struct vring vring; + union { + struct vring vring; + struct vring_packed vring_packed; + }; /* Shadow kick notifier, sent to vhost */ EventNotifier hdev_kick; @@ -69,27 +112,12 @@ typedef struct VhostShadowVirtqueue { /* Guest's call notifier, where the SVQ calls guest. */ EventNotifier svq_call; - /* Virtio queue shadowing */ - VirtQueue *vq; - - /* Virtio device */ - VirtIODevice *vdev; - /* IOVA mapping */ VhostIOVATree *iova_tree; - /* SVQ vring descriptors state */ - SVQDescState *desc_state; - /* Next VirtQueue element that guest made available */ VirtQueueElement *next_guest_avail_elem; - /* - * Backup next field for each descriptor so we can recover securely, not - * needing to trust the device access. - */ - uint16_t *desc_next; - /* Caller callbacks */ const VhostShadowVirtqueueOps *ops; @@ -99,17 +127,11 @@ typedef struct VhostShadowVirtqueue { /* Next head to expose to the device */ uint16_t shadow_avail_idx; - /* Next free descriptor */ - uint16_t free_head; - /* Last seen used idx */ uint16_t shadow_used_idx; /* Next head to consume from the device */ uint16_t last_used_idx; - - /* Size of SVQ vring free descriptors */ - uint16_t num_free; } VhostShadowVirtqueue; bool vhost_svq_valid_features(uint64_t features, Error **errp); From patchwork Fri Jul 26 09:58:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sahil Siddiq X-Patchwork-Id: 13742548 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id AF0B7C49EA1 for ; Fri, 26 Jul 2024 09:59:37 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sXHjG-0006Zv-I5; Fri, 26 Jul 2024 05:59:14 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sXHjE-0006VM-BD for qemu-devel@nongnu.org; Fri, 26 Jul 2024 05:59:12 -0400 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sXHjC-0006QB-FW for qemu-devel@nongnu.org; Fri, 26 Jul 2024 05:59:12 -0400 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-1fd640a6454so4895945ad.3 for ; Fri, 26 Jul 2024 02:59:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721987949; x=1722592749; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=CmvhoguNY49Nv0ZaQwQuOKqF+yUD8T+WptGtXxQNvDg=; b=egQMsNMGoyes+ocuDrIzA+kDbptR/Le8BrWsOY2p/kVR96ue2V8iJqG8q02hgxDWf5 uDTu0Sq2/ECXd5v3fXTOCdp/WupU01MZ/P0Z9qPExDt4x08gLvBEzdPuGIuxzKBTRe4x OhQnQnkYBdbuO1eKaeUbROTSJKzvu9w5Y9PKMoLRfyCmh0ztwdoxuDDCjRxAkrSlHO+B 6sIOexwbQDStk+FozcYTAkmiffew1x/yIav/2kR6R9pdRv5B+/THpZiQ3FpA0RRPyx4f nbPquqOmp7TsLOksmnBL4H/ba+mIch4cdEBsqH8hWLggbf8WGcT+bz6Mfyq1ljn685YS dbYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721987949; x=1722592749; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=CmvhoguNY49Nv0ZaQwQuOKqF+yUD8T+WptGtXxQNvDg=; b=mknVMg32ByuIIlLwNoZ7Ks21hdXJNRKfdvRSwkggNFa/FbifjyAdVY8KRJrGi6t4z+ PdIY7/d8pTzPau3AkySIiKldAtjXNQ5CDQcTS4h4dFkNfEvJ0cl+wrNWIgXgk5xn0tVz nVeCDdGa2h/GL9F9fx7cmscyTMKSN/JfK5p/5FgMkN23ik6MBbq7QpMl+yYsG04QrNA8 1uDogCBHPS8fehQQRqqBrJDVH13NOPm1SdBzU9+0R+DqJrIjA8zWz5FWAaRpplDRLv+X dhEQoWmsjbYZr/x9kVPXUbMVuoPb9kCYTjwrUSjJ3xLEBKwYwS28BryG8IMbKRgsIIAz /mFA== X-Forwarded-Encrypted: i=1; AJvYcCXW9E9Uz5fazM9IaWimcfW/Ut40qpGiyXEwVZhhD3/vbC6VPFI7OH0RbTuqrkeVDaYYYIp2zQqTkPaIwem5lSVNaz53O/Y= X-Gm-Message-State: AOJu0Yy5NHLFNh5tUnhqpPeNsykVSToGFjAvmbqTdSyaAF+MvJlavJFP PU8QFyj4stZpq0Z/GBBM0u5C8DTXqrpsitoHYc6OKUbbCc2wIdjw5jXHhA== X-Google-Smtp-Source: AGHT+IF9+qE1+lw0IniWiXck/zsEBoxre6XA92NP8il2cjzAeXrkhzhZrye6DYfvcQoCgoVv4Llldw== X-Received: by 2002:a17:902:e746:b0:1fb:3037:f3a0 with SMTP id d9443c01a7336-1fed38a44ffmr8113205ad.32.1721987948865; Fri, 26 Jul 2024 02:59:08 -0700 (PDT) Received: from valdaarhun.. ([223.233.82.156]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1fed7fcd252sm28201655ad.285.2024.07.26.02.59.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Jul 2024 02:59:08 -0700 (PDT) From: Sahil Siddiq X-Google-Original-From: Sahil Siddiq To: eperezma@redhat.com, sgarzare@redhat.com Cc: mst@redhat.com, qemu-devel@nongnu.org, Sahil Siddiq Subject: [RFC v2 3/3] vhost: Allocate memory for packed vring. Date: Fri, 26 Jul 2024 15:28:22 +0530 Message-ID: <20240726095822.104017-4-sahilcdq@proton.me> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240726095822.104017-1-sahilcdq@proton.me> References: <20240726095822.104017-1-sahilcdq@proton.me> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::629; envelope-from=icegambit91@gmail.com; helo=mail-pl1-x629.google.com X-Spam_score_int: 15 X-Spam_score: 1.5 X-Spam_bar: + X-Spam_report: (1.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_SBL_CSS=3.335, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Allocate memory for the packed vq format and support packed vq in the SVQ "start" operation. Signed-off-by: Sahil Siddiq --- Changes v1 -> v2: * vhost-shadow-virtqueue.h (struct VhostShadowVirtqueue): New member "is_packed" (vhost_svq_get_vring_addr): Renamed function. (vhost_svq_get_vring_addr_packed): New function. (vhost_svq_memory_packed): Likewise. * vhost-shadow-virtqueue.c: (vhost_svq_add): Use "is_packed" to check vq format. (vhost_svq_get_vring_addr): Rename function. (vhost_svq_get_vring_addr_packed): New function but is yet to be implemented. (vhost_svq_memory_packed): New function. (vhost_svq_start): Support packed vq format. * vhost-vdpa.c (vhost_svq_get_vring_addr): Rename function. hw/virtio/vhost-shadow-virtqueue.c | 70 ++++++++++++++++++++++-------- hw/virtio/vhost-shadow-virtqueue.h | 10 ++++- hw/virtio/vhost-vdpa.c | 4 +- 3 files changed, 63 insertions(+), 21 deletions(-) diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-virtqueue.c index c7b7e0c477..045c07304c 100644 --- a/hw/virtio/vhost-shadow-virtqueue.c +++ b/hw/virtio/vhost-shadow-virtqueue.c @@ -343,7 +343,7 @@ int vhost_svq_add(VhostShadowVirtqueue *svq, const struct iovec *out_sg, return -ENOSPC; } - if (virtio_vdev_has_feature(svq->vdev, VIRTIO_F_RING_PACKED)) { + if (svq->is_packed) { ok = vhost_svq_add_packed(svq, out_sg, out_num, in_sg, in_num, &qemu_head); } else { @@ -679,18 +679,29 @@ void vhost_svq_set_svq_call_fd(VhostShadowVirtqueue *svq, int call_fd) } /** - * Get the shadow vq vring address. + * Get the split shadow vq vring address. * @svq: Shadow virtqueue * @addr: Destination to store address */ -void vhost_svq_get_vring_addr(const VhostShadowVirtqueue *svq, - struct vhost_vring_addr *addr) +void vhost_svq_get_vring_addr_split(const VhostShadowVirtqueue *svq, + struct vhost_vring_addr *addr) { addr->desc_user_addr = (uint64_t)(uintptr_t)svq->vring.desc; addr->avail_user_addr = (uint64_t)(uintptr_t)svq->vring.avail; addr->used_user_addr = (uint64_t)(uintptr_t)svq->vring.used; } +/** + * Get the packed shadow vq vring address. + * @svq: Shadow virtqueue + * @addr: Destination to store address + */ +void vhost_svq_get_vring_addr_packed(const VhostShadowVirtqueue *svq, + struct vhost_vring_addr *addr) +{ + /* TODO */ +} + size_t vhost_svq_driver_area_size(const VhostShadowVirtqueue *svq) { size_t desc_size = sizeof(vring_desc_t) * svq->vring.num; @@ -707,6 +718,16 @@ size_t vhost_svq_device_area_size(const VhostShadowVirtqueue *svq) return ROUND_UP(used_size, qemu_real_host_page_size()); } +size_t vhost_svq_memory_packed(const VhostShadowVirtqueue *svq) +{ + size_t desc_size = sizeof(struct vring_packed_desc) * svq->num_free; + size_t driver_event_suppression = sizeof(struct vring_packed_desc_event); + size_t device_event_suppression = sizeof(struct vring_packed_desc_event); + + return ROUND_UP(desc_size + driver_event_suppression + device_event_suppression, + qemu_real_host_page_size()); +} + /** * Set a new file descriptor for the guest to kick the SVQ and notify for avail * @@ -759,19 +780,34 @@ void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev, svq->vq = vq; svq->iova_tree = iova_tree; - svq->vring.num = virtio_queue_get_num(vdev, virtio_get_queue_index(vq)); - svq->num_free = svq->vring.num; - svq->vring.desc = mmap(NULL, vhost_svq_driver_area_size(svq), - PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, - -1, 0); - desc_size = sizeof(vring_desc_t) * svq->vring.num; - svq->vring.avail = (void *)((char *)svq->vring.desc + desc_size); - svq->vring.used = mmap(NULL, vhost_svq_device_area_size(svq), - PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, - -1, 0); - svq->desc_state = g_new0(SVQDescState, svq->vring.num); - svq->desc_next = g_new0(uint16_t, svq->vring.num); - for (unsigned i = 0; i < svq->vring.num - 1; i++) { + if (virtio_vdev_has_feature(svq->vdev, VIRTIO_F_RING_PACKED)) { + svq->is_packed = true; + svq->vring_packed.vring.num = virtio_queue_get_num(vdev, virtio_get_queue_index(vq)); + svq->num_free = svq->vring_packed.vring.num; + svq->vring_packed.vring.desc = mmap(NULL, vhost_svq_memory_packed(svq), + PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, + -1, 0); + desc_size = sizeof(struct vring_packed_desc) * svq->vring.num; + svq->vring_packed.vring.driver = (void *)((char *)svq->vring_packed.vring.desc + desc_size); + svq->vring_packed.vring.device = (void *)((char *)svq->vring_packed.vring.driver + + sizeof(struct vring_packed_desc_event)); + } else { + svq->is_packed = false; + svq->vring.num = virtio_queue_get_num(vdev, virtio_get_queue_index(vq)); + svq->num_free = svq->vring.num; + svq->vring.desc = mmap(NULL, vhost_svq_driver_area_size(svq), + PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, + -1, 0); + desc_size = sizeof(vring_desc_t) * svq->vring.num; + svq->vring.avail = (void *)((char *)svq->vring.desc + desc_size); + svq->vring.used = mmap(NULL, vhost_svq_device_area_size(svq), + PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, + -1, 0); + } + + svq->desc_state = g_new0(SVQDescState, svq->num_free); + svq->desc_next = g_new0(uint16_t, svq->num_free); + for (unsigned i = 0; i < svq->num_free - 1; i++) { svq->desc_next[i] = cpu_to_le16(i + 1); } } diff --git a/hw/virtio/vhost-shadow-virtqueue.h b/hw/virtio/vhost-shadow-virtqueue.h index ee1a87f523..b396daf57d 100644 --- a/hw/virtio/vhost-shadow-virtqueue.h +++ b/hw/virtio/vhost-shadow-virtqueue.h @@ -67,6 +67,9 @@ struct vring_packed { /* Shadow virtqueue to relay notifications */ typedef struct VhostShadowVirtqueue { + /* True if packed virtqueue */ + bool is_packed; + /* Virtio queue shadowing */ VirtQueue *vq; @@ -146,10 +149,13 @@ size_t vhost_svq_poll(VhostShadowVirtqueue *svq, size_t num); void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd); void vhost_svq_set_svq_call_fd(VhostShadowVirtqueue *svq, int call_fd); -void vhost_svq_get_vring_addr(const VhostShadowVirtqueue *svq, - struct vhost_vring_addr *addr); +void vhost_svq_get_vring_addr_split(const VhostShadowVirtqueue *svq, + struct vhost_vring_addr *addr); +void vhost_svq_get_vring_addr_packed(const VhostShadowVirtqueue *svq, + struct vhost_vring_addr *addr); size_t vhost_svq_driver_area_size(const VhostShadowVirtqueue *svq); size_t vhost_svq_device_area_size(const VhostShadowVirtqueue *svq); +size_t vhost_svq_memory_packed(const VhostShadowVirtqueue *svq); void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev, VirtQueue *vq, VhostIOVATree *iova_tree); diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c index 3cdaa12ed5..688de4a662 100644 --- a/hw/virtio/vhost-vdpa.c +++ b/hw/virtio/vhost-vdpa.c @@ -1130,7 +1130,7 @@ static void vhost_vdpa_svq_unmap_rings(struct vhost_dev *dev, struct vhost_vdpa *v = dev->opaque; struct vhost_vring_addr svq_addr; - vhost_svq_get_vring_addr(svq, &svq_addr); + vhost_svq_get_vring_addr_split(svq, &svq_addr); vhost_vdpa_svq_unmap_ring(v, svq_addr.desc_user_addr); @@ -1189,7 +1189,7 @@ static bool vhost_vdpa_svq_map_rings(struct vhost_dev *dev, size_t avail_offset; bool ok; - vhost_svq_get_vring_addr(svq, &svq_addr); + vhost_svq_get_vring_addr_split(svq, &svq_addr); driver_region = (DMAMap) { .translated_addr = svq_addr.desc_user_addr,