From patchwork Thu Dec 5 20:34:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sahil Siddiq X-Patchwork-Id: 13895997 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 E3884E77173 for ; Thu, 5 Dec 2024 20:36:02 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tJIYu-0005tL-V9; Thu, 05 Dec 2024 15:35:00 -0500 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 1tJIYu-0005t6-2O for qemu-devel@nongnu.org; Thu, 05 Dec 2024 15:35:00 -0500 Received: from mail-pf1-x42a.google.com ([2607:f8b0:4864:20::42a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tJIYs-0004aZ-Ao for qemu-devel@nongnu.org; Thu, 05 Dec 2024 15:34:59 -0500 Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-7258ed68cedso1345175b3a.1 for ; Thu, 05 Dec 2024 12:34:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733430897; x=1734035697; 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=CbuG7Vt0FlvjItwImTZRJUsHMB8HutT99kgERxhxwEk=; b=CrLh7FV5Ww/DEZIdHYbXfxr/Tn1ZMd1vXkvez1olhYfHss3VREjrVAk1ZAOQm2h8ck XASHnlMNFZdPshXDxNAJAia/6HXgr5F2xXAfxFa3uFdwknuVU1rJsfXhbdvVpsAyRKa/ pa1anxDVXDfyILh4y/LffGlZWpZ4WqkYTegFXpnGbs7vpxxv47JEaaCWuLGX8tPvrO5W hGTcQ0xkcH/bbki/vNLItuIRyCNpqHsP8/azzNuVI8wp92subAqZNWo6Bo8rzhuP7mNB pqWN8dbKKNNDXK3+TuWgDRKqhVgOVnKLaQHU/v05TVusCEgc8d6HPJvALSLZuu7PUbMG cQqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733430897; x=1734035697; 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=CbuG7Vt0FlvjItwImTZRJUsHMB8HutT99kgERxhxwEk=; b=QRv6+Fo28Uwk6QNXgYaiXLNXnKk8T/M9ghcA4V/2T291nixLHX9vNG+SsQe9aLpgJy n3PNyvIYKzhkzmXnoDK/UR77VQuKz98sjRcWqRLoaxkVk8D6xF3cYp1Lw1zWX/LNpK0h cqXLcPi/kXi7sOB9htgDxwJnqNs1UrihJz4iLPKvjCHr9PGTV6POaPJ3u9ET6rbTh7qe amwaI0v/g81AMSu9jXZOnLG94QHMLXxsfbwYF7T9vjepzWiML6XkLKaHUxd1c8r6pVU4 zN7pf11rJNnCEiFxSClj6YUDlM2nnXwQPOG+d5m895U02Ijb8u+catirSQf2fAxsX7/a OcCg== X-Forwarded-Encrypted: i=1; AJvYcCX2cQej7HI9HD0Wl9EvU+WRBfC68doJEZcY9wRv4zEaHPlwcZR6+XjnqzRNh6XI+MGC0LNgbFujNar2@nongnu.org X-Gm-Message-State: AOJu0YwE5dXaWOj6l6sH1KTBpENdTiY9WmPQsZFGTQqia36sR3w2GKMn 5Z3ULwf30QXnd5haRCUCvHioplxfLVGGdkIDR2EyCGS+tNgl2EW5 X-Gm-Gg: ASbGncu3Yyhho9aHueYnlS6auIIFuGqou+blHByyutA269zMarx7RS/Z4H7Lj9Vip3j FDI81fB3GjOJSxop2Eh03rgK7NMScbtKVFzP7uPlKzUrcVjFTnMx1ArsbZ9VedUKT+Fgxe8ZShK Fwmci2++lcDJssmsNK9aHvRvup/h4yxgjSwV5XXtWD8n7Wls4OTw0fDYSTJuqgECL2d65a2WFfb 804B3AuiivTyfm6sT17raOrbmoHRSeagtrYQgJEcgk= X-Google-Smtp-Source: AGHT+IEGOPWQMbFQp6mNHx0PzoxZi+YN+kmpcpJEvF4RLEzGniQ8auIsFh1B81AJxfPrgC7dZQc7sw== X-Received: by 2002:a17:90b:3bc3:b0:2ee:9d49:3ae6 with SMTP id 98e67ed59e1d1-2ef69e12624mr900583a91.10.1733430896841; Thu, 05 Dec 2024 12:34:56 -0800 (PST) Received: from valdaarhun.. ([2409:4081:2c01:b2be:f39d:e902:7582:5526]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2ef2701c2f2sm3705905a91.23.2024.12.05.12.34.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Dec 2024 12:34:56 -0800 (PST) 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 v4 1/5] vhost: Refactor vhost_svq_add_split Date: Fri, 6 Dec 2024 02:04:26 +0530 Message-ID: <20241205203430.76251-2-sahilcdq@proton.me> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241205203430.76251-1-sahilcdq@proton.me> References: <20241205203430.76251-1-sahilcdq@proton.me> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::42a; envelope-from=icegambit91@gmail.com; helo=mail-pf1-x42a.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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 commit refactors vhost_svq_add_split and vhost_svq_add to simplify their implementation and prepare for the addition of packed vqs in following commits. Signed-off-by: Sahil Siddiq Acked-by: Eugenio Pérez --- Changes v3 -> v4: - Split commit #1 in v3 into 2 commits. - Changes related to "vhost_svq_add_packed" are now in commit #2. hw/virtio/vhost-shadow-virtqueue.c | 102 ++++++++++++----------------- 1 file changed, 41 insertions(+), 61 deletions(-) diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-virtqueue.c index 37aca8b431..bb7cf6d5db 100644 --- a/hw/virtio/vhost-shadow-virtqueue.c +++ b/hw/virtio/vhost-shadow-virtqueue.c @@ -124,83 +124,48 @@ static bool vhost_svq_translate_addr(const VhostShadowVirtqueue *svq, } /** - * Write descriptors to SVQ vring + * Write descriptors to SVQ split vring * * @svq: The shadow virtqueue - * @sg: Cache for hwaddr - * @iovec: The iovec from the guest - * @num: iovec length - * @more_descs: True if more descriptors come in the chain - * @write: True if they are writeable descriptors - * - * Return true if success, false otherwise and print error. + * @out_sg: The iovec to the guest + * @out_num: Outgoing iovec length + * @in_sg: The iovec from the guest + * @in_num: Incoming iovec length + * @sgs: Cache for hwaddr + * @head: Saves current free_head */ -static bool vhost_svq_vring_write_descs(VhostShadowVirtqueue *svq, hwaddr *sg, - const struct iovec *iovec, size_t num, - bool more_descs, bool write) +static void vhost_svq_add_split(VhostShadowVirtqueue *svq, + const struct iovec *out_sg, size_t out_num, + const struct iovec *in_sg, size_t in_num, + hwaddr *sgs, unsigned *head) { + unsigned avail_idx, n; uint16_t i = svq->free_head, last = svq->free_head; - unsigned n; - uint16_t flags = write ? cpu_to_le16(VRING_DESC_F_WRITE) : 0; + vring_avail_t *avail = svq->vring.avail; vring_desc_t *descs = svq->vring.desc; - bool ok; - - if (num == 0) { - return true; - } + size_t num = in_num + out_num; - ok = vhost_svq_translate_addr(svq, sg, iovec, num); - if (unlikely(!ok)) { - return false; - } + *head = svq->free_head; for (n = 0; n < num; n++) { - if (more_descs || (n + 1 < num)) { - descs[i].flags = flags | cpu_to_le16(VRING_DESC_F_NEXT); + descs[i].flags = cpu_to_le16(n < out_num ? 0 : VRING_DESC_F_WRITE); + if (n + 1 < num) { + descs[i].flags |= cpu_to_le16(VRING_DESC_F_NEXT); descs[i].next = cpu_to_le16(svq->desc_next[i]); + } + + descs[i].addr = cpu_to_le64(sgs[n]); + if (n < out_num) { + descs[i].len = cpu_to_le32(out_sg[n].iov_len); } else { - descs[i].flags = flags; + descs[i].len = cpu_to_le32(in_sg[n - out_num].iov_len); } - descs[i].addr = cpu_to_le64(sg[n]); - descs[i].len = cpu_to_le32(iovec[n].iov_len); last = i; i = cpu_to_le16(svq->desc_next[i]); } svq->free_head = le16_to_cpu(svq->desc_next[last]); - return true; -} - -static bool vhost_svq_add_split(VhostShadowVirtqueue *svq, - const struct iovec *out_sg, size_t out_num, - const struct iovec *in_sg, size_t in_num, - unsigned *head) -{ - unsigned avail_idx; - vring_avail_t *avail = svq->vring.avail; - bool ok; - g_autofree hwaddr *sgs = g_new(hwaddr, MAX(out_num, in_num)); - - *head = svq->free_head; - - /* 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; - } - - ok = vhost_svq_vring_write_descs(svq, sgs, out_sg, out_num, in_num > 0, - false); - if (unlikely(!ok)) { - return false; - } - - ok = vhost_svq_vring_write_descs(svq, sgs, in_sg, in_num, false, true); - if (unlikely(!ok)) { - return false; - } /* * Put the entry in the available array (but don't update avail->idx until @@ -214,7 +179,6 @@ static bool vhost_svq_add_split(VhostShadowVirtqueue *svq, smp_wmb(); avail->idx = cpu_to_le16(svq->shadow_avail_idx); - return true; } static void vhost_svq_kick(VhostShadowVirtqueue *svq) @@ -254,15 +218,31 @@ int vhost_svq_add(VhostShadowVirtqueue *svq, const struct iovec *out_sg, unsigned ndescs = in_num + out_num; bool ok; + /* We need some descriptors here */ + if (unlikely(!ndescs)) { + qemu_log_mask(LOG_GUEST_ERROR, + "Guest provided element with no descriptors"); + return -EINVAL; + } + if (unlikely(ndescs > vhost_svq_available_slots(svq))) { return -ENOSPC; } - ok = vhost_svq_add_split(svq, out_sg, out_num, in_sg, in_num, &qemu_head); + g_autofree hwaddr *sgs = g_new(hwaddr, ndescs); + ok = vhost_svq_translate_addr(svq, sgs, out_sg, out_num); if (unlikely(!ok)) { return -EINVAL; } + ok = vhost_svq_translate_addr(svq, sgs + out_num, in_sg, in_num); + if (unlikely(!ok)) { + return -EINVAL; + } + + vhost_svq_add_split(svq, out_sg, out_num, in_sg, + in_num, sgs, &qemu_head); + svq->num_free -= ndescs; svq->desc_state[qemu_head].elem = elem; svq->desc_state[qemu_head].ndescs = ndescs; From patchwork Thu Dec 5 20:34:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sahil Siddiq X-Patchwork-Id: 13895996 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 0B49CE77170 for ; Thu, 5 Dec 2024 20:35:44 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tJIZ2-0005yV-2C; Thu, 05 Dec 2024 15:35:08 -0500 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 1tJIZ0-0005wI-Fa for qemu-devel@nongnu.org; Thu, 05 Dec 2024 15:35:06 -0500 Received: from mail-pf1-x432.google.com ([2607:f8b0:4864:20::432]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tJIYy-0004lR-Mu for qemu-devel@nongnu.org; Thu, 05 Dec 2024 15:35:06 -0500 Received: by mail-pf1-x432.google.com with SMTP id d2e1a72fcca58-724e14b90cfso1435625b3a.2 for ; Thu, 05 Dec 2024 12:35:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733430903; x=1734035703; 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=Ho7yg6U067P5Xh/RW+CDVPYnTIMYuQPRqRvfGLa5b6g=; b=fS1RHPzJW2Aj30eyjd9HqMIMaAIzRtx85+qPClw27ZS3adaHN8xbW+SDVvTE2cU+9d R3eJwxvjyy8IQ60KqxJaS9aMckzzjT6LN19NZSTZCaBFjT00D2JHw8VuRSkoFmNLaW9x Nh0mpom9J5y6NMnbaqYO0OA72YCJ1Vh0k2oAPkUUImPaFLjyN6jcWPRcTx0PvWmLffr2 wk69iqY3uMi0FxVYmpStiZed0nH1RV2KXIKyiVJF/jcGmpY6u5SIuconpScbn117YTK7 6rUa5QwHLlOVONgRvDTAH6dUb9Xcz5OKqTRPtA8VNM7xk9xYji5ENmwF8b7VYT5ecYB1 D2MA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733430903; x=1734035703; 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=Ho7yg6U067P5Xh/RW+CDVPYnTIMYuQPRqRvfGLa5b6g=; b=MgZel8Xm1E0CRsupaEXtrV8iBjTCPywyUXhCdYTWT+wX6+xCChIt43wV4m/Stjg/cq pTQJDOAIl7vtzTDOhvqbpAshk4SaDquJE6GeWoSwLBi/BRxZkaYhPv2T1Tlyep0guj2n WsJcFqrE9HVNcf6+xVbmQPWYaEk5AthHI9tTkayQU6qI/Wq/JS3Glbe/WDboQ1/Qy/Rt FnfCbmZRLjruQuqXgsV0hwELJJCs8Jbgmn/qoRe5SAoYYr6aEcUzjOPjNDQuUstD+tAI WbvKwDCdeUPVCPVkN/9bCd7KS4l7iOhn44olvxUCHoqQQyH6tQmGzxwWBs97+JB3TP6E wA6Q== X-Forwarded-Encrypted: i=1; AJvYcCXBkyQhFYz9jjcb4erMlNdQH7x0zoAOr7VLEAS5L/NdK76cVcREFCU4sKUj5UdHpUXGFWXRibD68w/2@nongnu.org X-Gm-Message-State: AOJu0YygcAsHt46ADDB6gc8A2I+kFZVDhruDRI6CVrgfIncWEiMgB1T7 Xov5YDqk9kQ6jGsUHBTTlC2rQvkHOKs7QkNuc4purvKgSxJELFKkr+vywQ== X-Gm-Gg: ASbGncug0+ah91KuwDo0hacUtwyFUSv9H3jdvOOZmbFJSWmEIAfNNAZx7MspFp23OF7 g4CcvM2QEu0arwIqX5p0/rTTiQjsQzSBHSENPDAs0CbN/0M7mlV17Kx0DxLuDu8qopUxqEIfAdg X3yJYUUyASTOVLGwTVeZUSKurE3AYTU2eiaHghacTLpoqROr2ICcyrO0s0grqcWAV0qcYdWdM4P +fCU98EVGhLiYJrBT4IRXZiA/rL6/LjSX2n/024s0M= X-Google-Smtp-Source: AGHT+IGaKFkcjrLMfwjlj0++LPVUS7WZYp/D4x5NUBgasLDoRn7VkRiUrUONm/NEj+C0Eu6hcrkL8A== X-Received: by 2002:a17:90b:5384:b0:2ee:dcf6:1c8f with SMTP id 98e67ed59e1d1-2ef69f0b7f8mr812652a91.16.1733430903260; Thu, 05 Dec 2024 12:35:03 -0800 (PST) Received: from valdaarhun.. ([2409:4081:2c01:b2be:f39d:e902:7582:5526]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2ef2701c2f2sm3705905a91.23.2024.12.05.12.35.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Dec 2024 12:35:02 -0800 (PST) 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 v4 2/5] vhost: Write descriptors to packed svq Date: Fri, 6 Dec 2024 02:04:27 +0530 Message-ID: <20241205203430.76251-3-sahilcdq@proton.me> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241205203430.76251-1-sahilcdq@proton.me> References: <20241205203430.76251-1-sahilcdq@proton.me> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::432; envelope-from=icegambit91@gmail.com; helo=mail-pf1-x432.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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 commit is the first 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. Also validate svq-specific features that vdpa supports. Signed-off-by: Sahil Siddiq --- Changes v3 -> v4: - Split commit #1 in v3 into 2 commits. - vhost-shadow-virtqueue.c (vhost_svq_valid_features): Add enums. hw/virtio/vhost-shadow-virtqueue.c | 83 +++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 2 deletions(-) diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-virtqueue.c index bb7cf6d5db..6eee01ab3c 100644 --- a/hw/virtio/vhost-shadow-virtqueue.c +++ b/hw/virtio/vhost-shadow-virtqueue.c @@ -33,6 +33,9 @@ bool vhost_svq_valid_features(uint64_t features, Error **errp) ++b) { switch (b) { case VIRTIO_F_ANY_LAYOUT: + case VIRTIO_F_RING_PACKED: + case VIRTIO_F_RING_RESET: + case VIRTIO_RING_F_INDIRECT_DESC: case VIRTIO_RING_F_EVENT_IDX: continue; @@ -178,7 +181,78 @@ static void vhost_svq_add_split(VhostShadowVirtqueue *svq, /* Update the avail index after write the descriptor */ smp_wmb(); avail->idx = cpu_to_le16(svq->shadow_avail_idx); +} +/** + * Write descriptors to SVQ packed vring + * + * @svq: The shadow virtqueue + * @out_sg: The iovec to the guest + * @out_num: Outgoing iovec length + * @in_sg: The iovec from the guest + * @in_num: Incoming iovec length + * @sgs: Cache for hwaddr + * @head: Saves current free_head + */ +static void vhost_svq_add_packed(VhostShadowVirtqueue *svq, + const struct iovec *out_sg, size_t out_num, + const struct iovec *in_sg, size_t in_num, + hwaddr *sgs, unsigned *head) +{ + uint16_t id, curr, i, head_flags = 0; + size_t num = out_num + in_num; + unsigned n; + + struct vring_packed_desc *descs = svq->vring_packed.vring.desc; + + *head = svq->vring_packed.next_avail_idx; + i = *head; + id = svq->free_head; + curr = id; + + /* 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; } static void vhost_svq_kick(VhostShadowVirtqueue *svq) @@ -240,8 +314,13 @@ int vhost_svq_add(VhostShadowVirtqueue *svq, const struct iovec *out_sg, return -EINVAL; } - vhost_svq_add_split(svq, out_sg, out_num, in_sg, - in_num, sgs, &qemu_head); + if (virtio_vdev_has_feature(svq->vdev, VIRTIO_F_RING_PACKED)) { + vhost_svq_add_packed(svq, out_sg, out_num, in_sg, + in_num, sgs, &qemu_head); + } else { + vhost_svq_add_split(svq, out_sg, out_num, in_sg, + in_num, sgs, &qemu_head); + } svq->num_free -= ndescs; svq->desc_state[qemu_head].elem = elem; From patchwork Thu Dec 5 20:34:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sahil Siddiq X-Patchwork-Id: 13895994 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 6D76CE7716E for ; Thu, 5 Dec 2024 20:35:36 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tJIZ8-00066m-8b; Thu, 05 Dec 2024 15:35:14 -0500 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 1tJIZ7-00066I-08 for qemu-devel@nongnu.org; Thu, 05 Dec 2024 15:35:13 -0500 Received: from mail-pj1-x1036.google.com ([2607:f8b0:4864:20::1036]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tJIZ5-0004n8-Eo for qemu-devel@nongnu.org; Thu, 05 Dec 2024 15:35:12 -0500 Received: by mail-pj1-x1036.google.com with SMTP id 98e67ed59e1d1-2eecc01b5ebso1193328a91.1 for ; Thu, 05 Dec 2024 12:35:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733430910; x=1734035710; 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=VrlYK4yXYr2i1VgD7WUebq8opI+LmqDH8ytWZHxqCgE=; b=NFH1RAGse6UBvhKwSWwaiunGc4H1l+owCPpuJN2BJvf/gEaSRUVh5dC1r0BjDs/SUk 83GXoA6KJCSdUsTHmjeXCXoQ+Gs/QhjO0ce4pdvDMdMuIY+ecI817dsGEpjdeaXAsVEK Sc1wNizhSyZdluas/jETvp15k3eSEmxw/HjVAgp4OI+Ej/Nh1EqKjefwAi22XFSMwM7I tCdVaks0d7oroUTNKxg0vYc4h7+9scPH/HF5iL4bEPkD5E6RhBI5423vgZtbVIIDfa+Q 6i5uQQQxQ733FkZJIy7mYaXKfeRh0llksL9DMOJAFgD7DPoWZ0phHWXUioRNTErWRsv6 48+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733430910; x=1734035710; 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=VrlYK4yXYr2i1VgD7WUebq8opI+LmqDH8ytWZHxqCgE=; b=ImpAnb7C7obtBOxBXBb3R9IILO1uJMY+L/qWh9Y51dl2wGVpDiKsUcxK90J6driLlu xzUWGEA0elze9DRpbGOflPQLn5TK6e2eHL/KldWWJd2wR7OBiin/RkVmBi5n8CgBIEQg 4ZIVSYexqg7agpO9u5sJQXZz2/OUsmGfKDNA0mNtfw05m7QyNhnr/XQMAB2m7Y/q+M7r 0nAavkAxcrHdX8HsXYaZTNc7FhErRMaPpZS1f44qnIJyquSqsYfGK8tVmpOHq5S/Lk6n XP7971ULlFjgfcdSzYxgmiWwCEWgF8rA4wFayW8chgw+dWznBvbMIAdiY8TVnKROhY0R Ia7w== X-Forwarded-Encrypted: i=1; AJvYcCWqC8CElo0Q38HZe3xO6/pxTOCFFCGF+ZRVYWpKUsPSKxktPvD3Vr6ITl66m5ZBuvFqU4T++ioBjzPh@nongnu.org X-Gm-Message-State: AOJu0YxW+8yagTdlHOMqwpydm05I3rKgAVvtZpwmsIPibEQ+FDvLQnCh 71Vb5EIWB0BAH4/1pavOlTNqTrv8mY4tRs/EBfNlBPWdVGP9ZNOc X-Gm-Gg: ASbGnctsaGuI3OtHp+N2nhXXbONfczqwG3mPvGYyawbXwbylbpinZyFBenq4cNpCu2y ZwZd3JppVbRatGVUtHMn4/qdBPDWyO6S/pD0UtSi0t7TwRwQWLcuXhOgD905ye2GgDmQrsHAzIT qIraz0pPes62GXOAaSYHzERcCahJVfcUGv7WbNxCXy+R1rwQPd0fcMllKiS7abbsDKlGK4U3XN/ z0f34luPbG+a0uU749Ydc7sBxdbFUZiHh356KUTb1c= X-Google-Smtp-Source: AGHT+IGMmafjO+Z8rkuT7OJWtzNVIwmPTIjIFSX16Kr73Am/jYb2RDv6S6vzkVafZQgeMfJhXVYZ0Q== X-Received: by 2002:a17:90a:d88e:b0:2ee:aef4:2c5d with SMTP id 98e67ed59e1d1-2ef6ab10353mr726680a91.26.1733430909765; Thu, 05 Dec 2024 12:35:09 -0800 (PST) Received: from valdaarhun.. ([2409:4081:2c01:b2be:f39d:e902:7582:5526]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2ef2701c2f2sm3705905a91.23.2024.12.05.12.35.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Dec 2024 12:35:09 -0800 (PST) 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 v4 3/5] vhost: Data structure changes to support packed vqs Date: Fri, 6 Dec 2024 02:04:28 +0530 Message-ID: <20241205203430.76251-4-sahilcdq@proton.me> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241205203430.76251-1-sahilcdq@proton.me> References: <20241205203430.76251-1-sahilcdq@proton.me> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::1036; envelope-from=icegambit91@gmail.com; helo=mail-pj1-x1036.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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 --- Changes v3 -> v4: - Commit #2 of v3 is now commit #3 in this series. - vhost-shadow-virtqueue.h (struct VhostShadowVirtqueue): Add bool is_packed. This was initially in commit #3 of v3. hw/virtio/vhost-shadow-virtqueue.h | 69 ++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 22 deletions(-) diff --git a/hw/virtio/vhost-shadow-virtqueue.h b/hw/virtio/vhost-shadow-virtqueue.h index 19c842a15b..ce89bafedc 100644 --- a/hw/virtio/vhost-shadow-virtqueue.h +++ b/hw/virtio/vhost-shadow-virtqueue.h @@ -46,10 +46,56 @@ 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 { + /* True if packed virtqueue */ + bool is_packed; + + /* 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 +115,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 +130,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 Thu Dec 5 20:34:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sahil Siddiq X-Patchwork-Id: 13895998 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 9C63AE77170 for ; Thu, 5 Dec 2024 20:36:25 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tJIZE-000681-0Q; Thu, 05 Dec 2024 15:35:20 -0500 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 1tJIZC-00067d-Ml for qemu-devel@nongnu.org; Thu, 05 Dec 2024 15:35:18 -0500 Received: from mail-pj1-x102a.google.com ([2607:f8b0:4864:20::102a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tJIZA-0004oe-MW for qemu-devel@nongnu.org; Thu, 05 Dec 2024 15:35:18 -0500 Received: by mail-pj1-x102a.google.com with SMTP id 98e67ed59e1d1-2ee6abf124aso1160332a91.3 for ; Thu, 05 Dec 2024 12:35:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733430915; x=1734035715; 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=XOxyT26bcT3QQMndkO+7h+Piz5kcLrsCzZSCVUpRwzM=; b=Qpbih514ovEt4CI0288X5eq4GdZJR8ULYahsw+UwbPu4uxmDsxpKt7GD1K/Nnn+vjJ C1LF5fJELT9Wvjxx7K/pw+k0PzS4oKIcmAPpkQUiqye3vgpEz/hRTgYcH4qlWZXp0Xau OPEoAhwozBkcdJzT9sRKiHHytitKhutcgKi4qvJmVBrtc+xyNJqU94FhUxRKIyrgRmOy Am+jPoWUxijW8UbPxY5wIZJdpddya4riwYuCHq7sGu3E6ToP4AMhA0aLn+eGByqCWxEN ljdBoVcIjT02gtJCMRcECKjkls0le/7VGIA1KWF/PvtFrpWMqsb39pOwiHQ1Q5dBw8Qz Qz9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733430915; x=1734035715; 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=XOxyT26bcT3QQMndkO+7h+Piz5kcLrsCzZSCVUpRwzM=; b=XwrNyUT7pqmfl5BuFBL3OMmCsk1SBw33wyVvaAmvnR5aJfU4s6YzFM+EyHD2MOfaJ8 XN6AKDnpzq0o0Mv9LDcdomwzlIVP0p48w4uOc2ewJUf4yiSQbXZdTBjdk6R1gVLIK9Vv QtZMK9a4nUmgqCVKckeAr6tPcatRR1HTDvoKK/eJEZ6d02G3SqtBJ+tXJ9WIJzeyQmvK paCnha4oQ65DiVGb0EvoGm3yNp5+IdAqjWtMwrR3CS//YPUWXQtEcz3UkmPUa/82pHeF QzzxX1RCE5vbVMw85GidP+kIUbHg0PLcSeRDcBSB/W2i/YweF6W9PJULchNkXDgnnlLt gvSA== X-Forwarded-Encrypted: i=1; AJvYcCWDShIvSKvnjlBu+H8bERiSKcZ6wmaSGWAp0EOR6yGCvy65lMNxU6E96A1UhZ8Ono8/ZaMc3ipgqFZk@nongnu.org X-Gm-Message-State: AOJu0YwCKhtIKaOPShonZLkskOF9lWggPsjDqyZhgt0L62PIEL22mHc8 oYbauMY2dWur9r4uco9T5RPHnWDAHhV7TvohZA+MwociUYe690cD17ZkyQ== X-Gm-Gg: ASbGncuqQ59ZZ3upjotiytNAY/Qy6iwqoCC87ahdKjLI8GVmkyyRBB/TW2A4jJJPTz1 hDkpEkCiI4SPt2iucZ/ZPQ9dE4zLxpwskVgWduFxXiWInO8SWtYAoJCIhZ3g4qYLnwg4ubEtvt0 UtU1nx1DZga4CTGgNU4ep9T+MgLOVXy00MXQvJ3gY360sHa7OnD3viTIF8VB9gGWJWGvTMGu+95 LaObaH0YLChRdoton+L1aPfBMX2yQGrwYohuGkftB4= X-Google-Smtp-Source: AGHT+IElkhleK7Y17x5rBUWKryBptOUqd5yjC77Of3O3BhGm0tFCuGgaSjYdyGz/UdDlQLdSb6QSAQ== X-Received: by 2002:a17:90b:37c5:b0:2ee:f687:6adb with SMTP id 98e67ed59e1d1-2ef6956008bmr747090a91.3.1733430915319; Thu, 05 Dec 2024 12:35:15 -0800 (PST) Received: from valdaarhun.. ([2409:4081:2c01:b2be:f39d:e902:7582:5526]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2ef2701c2f2sm3705905a91.23.2024.12.05.12.35.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Dec 2024 12:35:14 -0800 (PST) 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 v4 4/5] vdpa: Allocate memory for svq and map them to vdpa Date: Fri, 6 Dec 2024 02:04:29 +0530 Message-ID: <20241205203430.76251-5-sahilcdq@proton.me> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241205203430.76251-1-sahilcdq@proton.me> References: <20241205203430.76251-1-sahilcdq@proton.me> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::102a; envelope-from=icegambit91@gmail.com; helo=mail-pj1-x102a.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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 map them to the vdpa device. Since "struct vring" and "struct vring_packed's vring" both have the same layout memory, the implementation in svq start and svq stop should not differ based on the vq's format. Signed-off-by: Sahil Siddiq --- Changes v3 -> v4: - Based on commit #3 of v3. - vhost-shadow-virtqueue.c (vhost_svq_memory_packed): Remove function. (vhost_svq_driver_area_size,vhost_svq_descriptor_area_size): Decouple functions. (vhost_svq_device_area_size): Rewrite function. (vhost_svq_start): Simplify implementation. (vhost_svq_stop): Unconditionally munmap(). - vhost-shadow-virtqueue.h: New function declaration. - vhost-vdpa.c (vhost_vdpa_svq_unmap_rings): Call vhost_vdpa_svq_unmap_ring(). (vhost_vdpa_svq_map_rings): New mappings. hw/virtio/vhost-shadow-virtqueue.c | 47 ++++++++++++++++++++---------- hw/virtio/vhost-shadow-virtqueue.h | 1 + hw/virtio/vhost-vdpa.c | 34 +++++++++++++++++---- 3 files changed, 60 insertions(+), 22 deletions(-) diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-virtqueue.c index 6eee01ab3c..be06b12c9a 100644 --- a/hw/virtio/vhost-shadow-virtqueue.c +++ b/hw/virtio/vhost-shadow-virtqueue.c @@ -314,7 +314,7 @@ int vhost_svq_add(VhostShadowVirtqueue *svq, const struct iovec *out_sg, return -EINVAL; } - if (virtio_vdev_has_feature(svq->vdev, VIRTIO_F_RING_PACKED)) { + if (svq->is_packed) { vhost_svq_add_packed(svq, out_sg, out_num, in_sg, in_num, sgs, &qemu_head); } else { @@ -661,19 +661,33 @@ void vhost_svq_get_vring_addr(const VhostShadowVirtqueue *svq, addr->used_user_addr = (uint64_t)(uintptr_t)svq->vring.used; } -size_t vhost_svq_driver_area_size(const VhostShadowVirtqueue *svq) +size_t vhost_svq_descriptor_area_size(const VhostShadowVirtqueue *svq) { size_t desc_size = sizeof(vring_desc_t) * svq->vring.num; - size_t avail_size = offsetof(vring_avail_t, ring[svq->vring.num]) + - sizeof(uint16_t); + return ROUND_UP(desc_size, qemu_real_host_page_size()); +} - return ROUND_UP(desc_size + avail_size, qemu_real_host_page_size()); +size_t vhost_svq_driver_area_size(const VhostShadowVirtqueue *svq) +{ + size_t avail_size; + if (svq->is_packed) { + avail_size = sizeof(uint32_t); + } else { + avail_size = offsetof(vring_avail_t, ring[svq->vring.num]) + + sizeof(uint16_t); + } + return ROUND_UP(avail_size, qemu_real_host_page_size()); } size_t vhost_svq_device_area_size(const VhostShadowVirtqueue *svq) { - size_t used_size = offsetof(vring_used_t, ring[svq->vring.num]) + - sizeof(uint16_t); + size_t used_size; + if (svq->is_packed) { + used_size = sizeof(uint32_t); + } else { + used_size = offsetof(vring_used_t, ring[svq->vring.num]) + + sizeof(uint16_t); + } return ROUND_UP(used_size, qemu_real_host_page_size()); } @@ -718,8 +732,6 @@ void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd) void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev, VirtQueue *vq, VhostIOVATree *iova_tree) { - size_t desc_size; - event_notifier_set_handler(&svq->hdev_call, vhost_svq_handle_call); svq->next_guest_avail_elem = NULL; svq->shadow_avail_idx = 0; @@ -728,20 +740,22 @@ void vhost_svq_start(VhostShadowVirtqueue *svq, VirtIODevice *vdev, svq->vdev = vdev; svq->vq = vq; svq->iova_tree = iova_tree; + svq->is_packed = virtio_vdev_has_feature(svq->vdev, VIRTIO_F_RING_PACKED); 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), + svq->vring.desc = mmap(NULL, vhost_svq_descriptor_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.avail = mmap(NULL, vhost_svq_driver_area_size(svq), + PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, + -1, 0); 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++) { + 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); } } @@ -781,7 +795,8 @@ void vhost_svq_stop(VhostShadowVirtqueue *svq) svq->vq = NULL; g_free(svq->desc_next); g_free(svq->desc_state); - munmap(svq->vring.desc, vhost_svq_driver_area_size(svq)); + munmap(svq->vring.desc, vhost_svq_descriptor_area_size(svq)); + munmap(svq->vring.avail, vhost_svq_driver_area_size(svq)); munmap(svq->vring.used, vhost_svq_device_area_size(svq)); event_notifier_set_handler(&svq->hdev_call, NULL); } diff --git a/hw/virtio/vhost-shadow-virtqueue.h b/hw/virtio/vhost-shadow-virtqueue.h index ce89bafedc..6c0e0c4f67 100644 --- a/hw/virtio/vhost-shadow-virtqueue.h +++ b/hw/virtio/vhost-shadow-virtqueue.h @@ -151,6 +151,7 @@ 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); +size_t vhost_svq_descriptor_area_size(const VhostShadowVirtqueue *svq); size_t vhost_svq_driver_area_size(const VhostShadowVirtqueue *svq); size_t vhost_svq_device_area_size(const VhostShadowVirtqueue *svq); diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c index 3cdaa12ed5..97ed569792 100644 --- a/hw/virtio/vhost-vdpa.c +++ b/hw/virtio/vhost-vdpa.c @@ -1134,6 +1134,8 @@ static void vhost_vdpa_svq_unmap_rings(struct vhost_dev *dev, vhost_vdpa_svq_unmap_ring(v, svq_addr.desc_user_addr); + vhost_vdpa_svq_unmap_ring(v, svq_addr.avail_user_addr); + vhost_vdpa_svq_unmap_ring(v, svq_addr.used_user_addr); } @@ -1181,38 +1183,58 @@ static bool vhost_vdpa_svq_map_rings(struct vhost_dev *dev, Error **errp) { ERRP_GUARD(); - DMAMap device_region, driver_region; + DMAMap descriptor_region, device_region, driver_region; struct vhost_vring_addr svq_addr; struct vhost_vdpa *v = dev->opaque; + size_t descriptor_size = vhost_svq_descriptor_area_size(svq); size_t device_size = vhost_svq_device_area_size(svq); size_t driver_size = vhost_svq_driver_area_size(svq); - size_t avail_offset; bool ok; vhost_svq_get_vring_addr(svq, &svq_addr); - driver_region = (DMAMap) { + descriptor_region = (DMAMap) { .translated_addr = svq_addr.desc_user_addr, + .size = descriptor_size - 1, + .perm = IOMMU_RO, + }; + if (svq->is_packed) { + descriptor_region.perm = IOMMU_RW; + } + + ok = vhost_vdpa_svq_map_ring(v, &descriptor_region, errp); + if (unlikely(!ok)) { + error_prepend(errp, "Cannot create vq descriptor region: "); + return false; + } + addr->desc_user_addr = descriptor_region.iova; + + driver_region = (DMAMap) { + .translated_addr = svq_addr.avail_user_addr, .size = driver_size - 1, .perm = IOMMU_RO, }; ok = vhost_vdpa_svq_map_ring(v, &driver_region, errp); if (unlikely(!ok)) { error_prepend(errp, "Cannot create vq driver region: "); + vhost_vdpa_svq_unmap_ring(v, descriptor_region.translated_addr); return false; } - addr->desc_user_addr = driver_region.iova; - avail_offset = svq_addr.avail_user_addr - svq_addr.desc_user_addr; - addr->avail_user_addr = driver_region.iova + avail_offset; + addr->avail_user_addr = driver_region.iova; device_region = (DMAMap) { .translated_addr = svq_addr.used_user_addr, .size = device_size - 1, .perm = IOMMU_RW, }; + if (svq->is_packed) { + device_region.perm = IOMMU_WO; + } + ok = vhost_vdpa_svq_map_ring(v, &device_region, errp); if (unlikely(!ok)) { error_prepend(errp, "Cannot create vq device region: "); + vhost_vdpa_svq_unmap_ring(v, descriptor_region.translated_addr); vhost_vdpa_svq_unmap_ring(v, driver_region.translated_addr); } addr->used_user_addr = device_region.iova; From patchwork Thu Dec 5 20:34:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sahil Siddiq X-Patchwork-Id: 13895995 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 434D8E7716E for ; Thu, 5 Dec 2024 20:35:41 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tJIZR-0006QZ-SN; Thu, 05 Dec 2024 15:35:33 -0500 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 1tJIZP-0006DM-FP for qemu-devel@nongnu.org; Thu, 05 Dec 2024 15:35:31 -0500 Received: from mail-pj1-x1036.google.com ([2607:f8b0:4864:20::1036]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tJIZM-0004pM-OX for qemu-devel@nongnu.org; Thu, 05 Dec 2024 15:35:31 -0500 Received: by mail-pj1-x1036.google.com with SMTP id 98e67ed59e1d1-2ee709715d9so989888a91.3 for ; Thu, 05 Dec 2024 12:35:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733430921; x=1734035721; 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=vvD8iJMWbVGgLrHgj6Wpg47VvHU8OZqOaHm8l4MsBPw=; b=AK7OBFF0exl668EexGJKspXsq+aGEe5Gwyei7sgSV8qDtF2fAuLm/H9fuqfvGK3Lsy Z8rIT72TxYiCXjNPShrus9IOmCj5THutjxST1aGsIlxA1uvVKpP8SFsRBACo5U28AQZm QDt9hFcrzT8NFurXwfRts7S588eKweUOMkHKQgXbbFSccFHCIJn7pDbpqc64fxnmztan LtHDp+GjaLG28AstCRgxBMy2ihYhTVBZ2jpIR5nfns6mHyXUDu7047sBapoCeh2rtAna 4VlgUdnuDvXhC8wNV4Sp0CKC3/uVfPQ2xl5TXuXJJn6htMDK7WviR6zxREBl3z4x8uFS +0cA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733430921; x=1734035721; 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=vvD8iJMWbVGgLrHgj6Wpg47VvHU8OZqOaHm8l4MsBPw=; b=rE8q0MNSC6BccUCwpate1NsHWaB5uGDg4fMzfddTPfd9fM/1ZXuq56ShRhCTTGGZpI MgQzlK4FctqDGJ+vKnBCwUKM/xTbtGHSbdK/X4V1jUq1ojIwnPzjxEvCPaI1URlsNDZg SxO9LgCXms7d5fYyEROQRoO6zjaIt5oL3FgbvsWnb3YzjH/iekryKzjY4oFhOZwZ+XVA DOFjthOJydC3XOIYDL1CiyMUZWpqEtJACsWG+yegqUBanNhXuIo7EiyE0ahZxWB5XP+d lxvlS3t1FbmX2EqwGpRV1w+WwwmYyh3ilrxnHDkVRp4BoQdSnpIllbDFLMjTaC9QHzhp wLzg== X-Forwarded-Encrypted: i=1; AJvYcCUhSwKEMntz6TaVa7SdN91ryfWqq4kkp8DpPljdsKwj4esXLoNKcy8tCUocVVR38z0mPKGY/sfLYut+@nongnu.org X-Gm-Message-State: AOJu0Yxi1Xj2huMzOZw8JfIEx/DoKg0/sxIE86rUsNGakviDqR6hijWg QybvR130xCoHOWXl1z4//9kodR4FhE2wW9z9DAv8pliwwf4at9Yp X-Gm-Gg: ASbGncuCokGiOhCQx4yQrt/fYE1UdTMSUSICNQggZlxvlDoJQKxiibSgVbk2FcvozNX zvG8wgXp4b8fpkZsao2O72aqYv26EBRKV320aVTOQ3MZeHUdLtfCKWRVOhy32XVQOyzVMdqKpsk jcxoWcC0XULkVhAepzEkXC/wqNWXgYOPt2oz0Nbo0My5RyAJbP6LHAKwK0ESSVq5zxVq6M7XX6W vqHW5HUH/N4K7sX2vCAayXBhGGnTCphdhybHLfso48= X-Google-Smtp-Source: AGHT+IEppdbuIhzZsemGUvQ0MVmTuFmfK56YOZaDymUlTbFQTmQIe4O/O13eTYhx1Dmfh5q3pEh/iQ== X-Received: by 2002:a17:90b:4f46:b0:2ee:eb5b:6e06 with SMTP id 98e67ed59e1d1-2ef6aaf3a46mr772276a91.36.1733430921365; Thu, 05 Dec 2024 12:35:21 -0800 (PST) Received: from valdaarhun.. ([2409:4081:2c01:b2be:f39d:e902:7582:5526]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2ef2701c2f2sm3705905a91.23.2024.12.05.12.35.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Dec 2024 12:35:20 -0800 (PST) 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 v4 5/5] vdpa: Support setting vring_base for packed svq Date: Fri, 6 Dec 2024 02:04:30 +0530 Message-ID: <20241205203430.76251-6-sahilcdq@proton.me> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241205203430.76251-1-sahilcdq@proton.me> References: <20241205203430.76251-1-sahilcdq@proton.me> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::1036; envelope-from=icegambit91@gmail.com; helo=mail-pj1-x1036.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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 Linux commit 1225c216d954 ("vp_vdpa: allow set vq state to initial state after reset") enabled the vp_vdpa driver to set the vq state to the device's initial state. This works differently for split and packed vqs. With shadow virtqueues enabled, vhost-vdpa sets the vring base using the VHOST_SET_VRING_BASE ioctl. The payload (vhost_vring_state) differs for split and packed vqs. The implementation in QEMU currently uses the payload required for split vqs (i.e., the num field of vhost_vring_state is set to 0). The kernel throws EOPNOTSUPP when this payload is used with packed vqs. This patch sets the num field in the payload appropriately so vhost-vdpa (with the vp_vdpa driver) can use packed svqs. Link: https://lists.nongnu.org/archive/html/qemu-devel/2024-10/msg05106.html Link: https://lore.kernel.org/r/20210602021536.39525-4-jasowang@redhat.com Link: 1225c216d954 ("vp_vdpa: allow set vq state to initial state after reset") Signed-off-by: Sahil Siddiq Acked-by: Eugenio Pérez --- Changes v3 -> v4: - New commit. - vhost-vdpa.c (vhost_vdpa_svq_setup): Add comment. hw/virtio/vhost-vdpa.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c index 97ed569792..3e32874ce9 100644 --- a/hw/virtio/vhost-vdpa.c +++ b/hw/virtio/vhost-vdpa.c @@ -1252,6 +1252,19 @@ static bool vhost_vdpa_svq_setup(struct vhost_dev *dev, }; int r; + /* In Linux, the upper 16 bits of s.num is encoded as + the last used idx while the lower 16 bits is encoded + as the last avail idx when using packed vqs. The most + significant bit for each idx represents the counter + and should be set in both cases while the remaining + bits are cleared. */ + if (virtio_vdev_has_feature(dev->vdev, VIRTIO_F_RING_PACKED)) { + uint32_t last_avail_idx = 0 | (1 << VRING_PACKED_EVENT_F_WRAP_CTR); + uint32_t last_used_idx = 0 | (1 << VRING_PACKED_EVENT_F_WRAP_CTR); + + s.num = (last_used_idx << 16) | last_avail_idx; + } + r = vhost_vdpa_set_dev_vring_base(dev, &s); if (unlikely(r)) { error_setg_errno(errp, -r, "Cannot set vring base");