From nobody Sun Feb 23 12:15:32 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1740141019; cv=none; d=zohomail.com; s=zohoarc; b=E2TkAW3NckGKLzB0aZRBsj+/nMwUhMo0nSx4wRXi5sdm69aVdX2ll3JQhWXH5tbHd2CPmsokYU6tRxRdbt0kiutooeetFHjepgprdFgte71lhVreV8szS5ouGA4pDTStKBtOky/9Qgf1HlPNqfg2HkeXakJu0UJicgtMXW6aN+M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1740141019; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=y55Lq3nnb4wo+Mep9PSIMyZizGYpu/mdV63SxawIIlg=; b=G4DLy42G4cWLhiLH84+Ym3eYl3uIFwAAV2DiboEN+h/nAs70uVC6yrQz8u5vyhxHBN55fKgo3aXeVPK1cXEo1LwhZnG/tpMnN5vjF/eYdaqJo5/tDxkxaG3fNzIheI8LFiydzkx1rXKhhyO/e4IHggMjgxGT0a/MJzU4u+mM8TQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 17401410196829.22136488806052; Fri, 21 Feb 2025 04:30:19 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tlS4x-0007tU-19; Fri, 21 Feb 2025 07:24:27 -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 1tlS4n-0007WU-5h for qemu-devel@nongnu.org; Fri, 21 Feb 2025 07:24:17 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tlS4k-0007Ap-72 for qemu-devel@nongnu.org; Fri, 21 Feb 2025 07:24:15 -0500 Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-358-sPgT0ernO1WXshOxFlWDRQ-1; Fri, 21 Feb 2025 07:24:12 -0500 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-43942e82719so13917555e9.2 for ; Fri, 21 Feb 2025 04:24:11 -0800 (PST) Received: from redhat.com ([31.187.78.163]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38f258fc7ecsm22997792f8f.49.2025.02.21.04.24.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Feb 2025 04:24:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740140653; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=y55Lq3nnb4wo+Mep9PSIMyZizGYpu/mdV63SxawIIlg=; b=aabNhQw8IMcm7+llcpSMw0oJIgJtoJLuVURlvy4YEIsWNzwTlWB03VLPptg9g0IbMcjMJn b7WPd9eFW+LGm7qqOh1B7aEQ332xCT0Vl4yTaJLAtt+Qambnti4+/3nnh9ABxqbpbyfYud 1Ep9xcCrQgCaROWP4BeoVwSzbSJA1gg= X-MC-Unique: sPgT0ernO1WXshOxFlWDRQ-1 X-Mimecast-MFC-AGG-ID: sPgT0ernO1WXshOxFlWDRQ_1740140651 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740140650; x=1740745450; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=y55Lq3nnb4wo+Mep9PSIMyZizGYpu/mdV63SxawIIlg=; b=oy60sa0a/0BJ6GIDu8Wbvz/4Q/qwReJBnJVEktaw4ILhjLZ792LCmOVJ3cSAXogZbj P/CcO+cjLKOndcpZw2a/L2x6dS7qXfSRkASlfchJMIqnH7ykW5LDly120hDCF5YlX8NU xd7W4ULy+/yvtOu3l5ggYIs4w+EB4fE3grZIYaHJx8kV/0ZNIp35rHvg0jU4HLNlwsPv ZVZS/nqK9YCcm/2DBFX0rJJWfN9S2my5Ab9zUC/5gHAux5ymcvLSfj4xRiC3AYMilsv1 ptViLcD6SC44UN2LkxtD5o4mLBDlvLm3I3OPBuhIAUXhCb4W8lxK33kLhggcepG1Q7JX A2ZA== X-Gm-Message-State: AOJu0Yy+PMFEJaK4ND7LmQX+4iVoJswogwnMllKdk/R3i16YOk0mria8 L0h3Mng0YRQspCHhJf5fmzf/2OD3uZ91ylr7aTI538UfTUof2QaY70vFTXr3BU3pjP4zrDtQWfe L0Vuuu3jXB0EqJkOqiLwCf5xp+dth7PU4mjBpgK5poNIyepO5QEXA7/Zr3WzTwvri7/ImjCzRui uus/TOXoBojxMYbus1quCaM/gxWDtz4g== X-Gm-Gg: ASbGnctwfSuOfDL0lRrzsBus8gIB26nyorCJiAOtkRVphYFkULsacapJPOHxwTIlZJJ UpZx/vwULeWxEMvHoCex6+9tqMPmoTaBSHR4K4eO26p17X3qJf9nV8yLw8ZjfgX6btgdEqxhOF9 4I32i/B7amWpA98SbGpZMyJsAA4ATWlSQL7pPRCWKNHoE+WinX39EISZkputJh4Ce3kdsSjbcEG zoysBKo8VWWgd6OCn1fIsmv499GTbq9ALM8lTtgOLqJ7XP3FVsZrYXvsLtZ/Yjr/5U6PQGe0QPz QotRUw== X-Received: by 2002:a5d:47a9:0:b0:38f:3888:33bc with SMTP id ffacd0b85a97d-38f6e755eebmr2956940f8f.10.1740140650247; Fri, 21 Feb 2025 04:24:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IH1jDVsRMsGdie1M/p/c7fTH9AvdNu2+7DPqbDnkNGxE7mSHJYD/rRv7K8qAaHKQGbg3i0uWw== X-Received: by 2002:a5d:47a9:0:b0:38f:3888:33bc with SMTP id ffacd0b85a97d-38f6e755eebmr2956895f8f.10.1740140649660; Fri, 21 Feb 2025 04:24:09 -0800 (PST) Date: Fri, 21 Feb 2025 07:24:06 -0500 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org Cc: Peter Maydell , Jonah Palmer , Eugenio =?utf-8?B?UMOpcmV6?= , Lei Yang , Stefano Garzarella , Jason Wang Subject: [PULL 28/41] vhost-iova-tree: Implement an IOVA-only tree Message-ID: <92cf61e70838c20adc82daa3170fdbb9d174b508.1740140520.git.mst@redhat.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Disposition: inline Content-Transfer-Encoding: quoted-printable In-Reply-To: X-Mailer: git-send-email 2.27.0.106.g8ac3dc51b1 X-Mutt-Fcc: =sent Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=mst@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -24 X-Spam_score: -2.5 X-Spam_bar: -- X-Spam_report: (-2.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.424, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1740141021387019100 From: Jonah Palmer Creates and supports an IOVA-only tree to support a SVQ IOVA->HVA and GPA->IOVA tree for host-only and guest-backed memory, respectively, in the next patch. The IOVA allocator still allocates an IOVA range but now adds this range to the IOVA-only tree as well as to the full IOVA->HVA tree. In the next patch, the full IOVA->HVA tree will be split into a partial SVQ IOVA->HVA tree and a GPA->IOVA tree. The motivation behind having an IOVA-only tree was to have a single tree that would keep track of all allocated IOVA ranges between the partial SVQ IOVA->HVA and GPA->IOVA trees. Signed-off-by: Jonah Palmer Acked-by: Eugenio P=C3=A9rez Tested-by: Lei Yang Message-Id: <20250217144936.3589907-2-jonah.palmer@oracle.com> Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/virtio/vhost-iova-tree.h | 3 ++- hw/virtio/vhost-iova-tree.c | 26 ++++++++++++++++++++------ hw/virtio/vhost-vdpa.c | 29 +++++++++++++++++++++-------- net/vhost-vdpa.c | 10 ++++++++-- 4 files changed, 51 insertions(+), 17 deletions(-) diff --git a/hw/virtio/vhost-iova-tree.h b/hw/virtio/vhost-iova-tree.h index 4adfd79ff0..525ce72b1d 100644 --- a/hw/virtio/vhost-iova-tree.h +++ b/hw/virtio/vhost-iova-tree.h @@ -21,7 +21,8 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC(VhostIOVATree, vhost_iova_t= ree_delete); =20 const DMAMap *vhost_iova_tree_find_iova(const VhostIOVATree *iova_tree, const DMAMap *map); -int vhost_iova_tree_map_alloc(VhostIOVATree *iova_tree, DMAMap *map); +int vhost_iova_tree_map_alloc(VhostIOVATree *iova_tree, DMAMap *map, + hwaddr taddr); void vhost_iova_tree_remove(VhostIOVATree *iova_tree, DMAMap map); =20 #endif diff --git a/hw/virtio/vhost-iova-tree.c b/hw/virtio/vhost-iova-tree.c index 3d03395a77..216885aa3c 100644 --- a/hw/virtio/vhost-iova-tree.c +++ b/hw/virtio/vhost-iova-tree.c @@ -28,6 +28,9 @@ struct VhostIOVATree { =20 /* IOVA address to qemu memory maps. */ IOVATree *iova_taddr_map; + + /* Allocated IOVA addresses */ + IOVATree *iova_map; }; =20 /** @@ -44,6 +47,7 @@ VhostIOVATree *vhost_iova_tree_new(hwaddr iova_first, hwa= ddr iova_last) tree->iova_last =3D iova_last; =20 tree->iova_taddr_map =3D iova_tree_new(); + tree->iova_map =3D iova_tree_new(); return tree; } =20 @@ -53,6 +57,7 @@ VhostIOVATree *vhost_iova_tree_new(hwaddr iova_first, hwa= ddr iova_last) void vhost_iova_tree_delete(VhostIOVATree *iova_tree) { iova_tree_destroy(iova_tree->iova_taddr_map); + iova_tree_destroy(iova_tree->iova_map); g_free(iova_tree); } =20 @@ -75,6 +80,7 @@ const DMAMap *vhost_iova_tree_find_iova(const VhostIOVATr= ee *tree, * * @tree: The iova tree * @map: The iova map + * @taddr: The translated address (HVA) * * Returns: * - IOVA_OK if the map fits in the container @@ -83,19 +89,26 @@ const DMAMap *vhost_iova_tree_find_iova(const VhostIOVA= Tree *tree, * * It returns assignated iova in map->iova if return value is VHOST_DMA_MA= P_OK. */ -int vhost_iova_tree_map_alloc(VhostIOVATree *tree, DMAMap *map) +int vhost_iova_tree_map_alloc(VhostIOVATree *tree, DMAMap *map, hwaddr tad= dr) { + int ret; + /* Some vhost devices do not like addr 0. Skip first page */ hwaddr iova_first =3D tree->iova_first ?: qemu_real_host_page_size(); =20 - if (map->translated_addr + map->size < map->translated_addr || - map->perm =3D=3D IOMMU_NONE) { + if (taddr + map->size < taddr || map->perm =3D=3D IOMMU_NONE) { return IOVA_ERR_INVALID; } =20 - /* Allocate a node in IOVA address */ - return iova_tree_alloc_map(tree->iova_taddr_map, map, iova_first, - tree->iova_last); + /* Allocate a node in the IOVA-only tree */ + ret =3D iova_tree_alloc_map(tree->iova_map, map, iova_first, tree->iov= a_last); + if (unlikely(ret !=3D IOVA_OK)) { + return ret; + } + + /* Insert a node in the IOVA->HVA tree */ + map->translated_addr =3D taddr; + return iova_tree_insert(tree->iova_taddr_map, map); } =20 /** @@ -107,4 +120,5 @@ int vhost_iova_tree_map_alloc(VhostIOVATree *tree, DMAM= ap *map) void vhost_iova_tree_remove(VhostIOVATree *iova_tree, DMAMap map) { iova_tree_remove(iova_tree->iova_taddr_map, map); + iova_tree_remove(iova_tree->iova_map, map); } diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c index 3cdaa12ed5..703dcfc929 100644 --- a/hw/virtio/vhost-vdpa.c +++ b/hw/virtio/vhost-vdpa.c @@ -360,14 +360,20 @@ static void vhost_vdpa_listener_region_add(MemoryList= ener *listener, llsize =3D int128_sub(llend, int128_make64(iova)); if (s->shadow_data) { int r; + hwaddr hw_vaddr =3D (hwaddr)(uintptr_t)vaddr; =20 - mem_region.translated_addr =3D (hwaddr)(uintptr_t)vaddr, mem_region.size =3D int128_get64(llsize) - 1, mem_region.perm =3D IOMMU_ACCESS_FLAG(true, section->readonly), =20 - r =3D vhost_iova_tree_map_alloc(s->iova_tree, &mem_region); + r =3D vhost_iova_tree_map_alloc(s->iova_tree, &mem_region, hw_vadd= r); if (unlikely(r !=3D IOVA_OK)) { error_report("Can't allocate a mapping (%d)", r); + + if (mem_region.translated_addr =3D=3D hw_vaddr) { + error_report("Insertion to IOVA->HVA tree failed"); + /* Remove the mapping from the IOVA-only tree */ + goto fail_map; + } goto fail; } =20 @@ -1142,16 +1148,23 @@ static void vhost_vdpa_svq_unmap_rings(struct vhost= _dev *dev, * * @v: Vhost-vdpa device * @needle: The area to search iova + * @taddr: The translated address (HVA) * @errorp: Error pointer */ static bool vhost_vdpa_svq_map_ring(struct vhost_vdpa *v, DMAMap *needle, - Error **errp) + hwaddr taddr, Error **errp) { int r; =20 - r =3D vhost_iova_tree_map_alloc(v->shared->iova_tree, needle); + r =3D vhost_iova_tree_map_alloc(v->shared->iova_tree, needle, taddr); if (unlikely(r !=3D IOVA_OK)) { error_setg(errp, "Cannot allocate iova (%d)", r); + + if (needle->translated_addr =3D=3D taddr) { + error_append_hint(errp, "Insertion to IOVA->HVA tree failed"); + /* Remove the mapping from the IOVA-only tree */ + vhost_iova_tree_remove(v->shared->iova_tree, *needle); + } return false; } =20 @@ -1192,11 +1205,11 @@ static bool vhost_vdpa_svq_map_rings(struct vhost_d= ev *dev, vhost_svq_get_vring_addr(svq, &svq_addr); =20 driver_region =3D (DMAMap) { - .translated_addr =3D svq_addr.desc_user_addr, .size =3D driver_size - 1, .perm =3D IOMMU_RO, }; - ok =3D vhost_vdpa_svq_map_ring(v, &driver_region, errp); + ok =3D vhost_vdpa_svq_map_ring(v, &driver_region, svq_addr.desc_user_a= ddr, + errp); if (unlikely(!ok)) { error_prepend(errp, "Cannot create vq driver region: "); return false; @@ -1206,11 +1219,11 @@ static bool vhost_vdpa_svq_map_rings(struct vhost_d= ev *dev, addr->avail_user_addr =3D driver_region.iova + avail_offset; =20 device_region =3D (DMAMap) { - .translated_addr =3D svq_addr.used_user_addr, .size =3D device_size - 1, .perm =3D IOMMU_RW, }; - ok =3D vhost_vdpa_svq_map_ring(v, &device_region, errp); + ok =3D vhost_vdpa_svq_map_ring(v, &device_region, svq_addr.used_user_a= ddr, + errp); if (unlikely(!ok)) { error_prepend(errp, "Cannot create vq device region: "); vhost_vdpa_svq_unmap_ring(v, driver_region.translated_addr); diff --git a/net/vhost-vdpa.c b/net/vhost-vdpa.c index 231b45246c..5a3a57203d 100644 --- a/net/vhost-vdpa.c +++ b/net/vhost-vdpa.c @@ -510,14 +510,20 @@ static int vhost_vdpa_cvq_map_buf(struct vhost_vdpa *= v, void *buf, size_t size, bool write) { DMAMap map =3D {}; + hwaddr taddr =3D (hwaddr)(uintptr_t)buf; int r; =20 - map.translated_addr =3D (hwaddr)(uintptr_t)buf; map.size =3D size - 1; map.perm =3D write ? IOMMU_RW : IOMMU_RO, - r =3D vhost_iova_tree_map_alloc(v->shared->iova_tree, &map); + r =3D vhost_iova_tree_map_alloc(v->shared->iova_tree, &map, taddr); if (unlikely(r !=3D IOVA_OK)) { error_report("Cannot map injected element"); + + if (map.translated_addr =3D=3D taddr) { + error_report("Insertion to IOVA->HVA tree failed"); + /* Remove the mapping from the IOVA-only tree */ + goto dma_map_err; + } return r; } =20 --=20 MST