From nobody Tue Feb 10 03:37:57 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1621443154; cv=none; d=zohomail.com; s=zohoarc; b=m6ukT8ojlGC1sk7Ut4ZLKevr0Kc+TsR3XO7GV7/6qN7BJ1gS5Y+6uu+IaGCDHUJMtfp0fOHE06A6MhDTrbbF3sdutj0R06TlBzeNiGa4KVlO4Xkvr+9GeLGcs9lqSkIbwAlebV17OOPkiTXSUt7KbhxbACPYtXDbPu7mRkR6xOU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621443154; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=r7dtTOJdCmRYBm6EIMcBw+fVvfbwCuQ3tsienHtyc0Y=; b=Sw286+Er+MReFKS43lEK5RPzBwsoMLnoa2thD0tV/pUD3u6qBBRQLaHui2IcesUnfoyKnTCrAUQ27i1hgEYG/308aXvZDIG4mDMiQqUAt09u4qHFC2IbHTvrNg80aDg+pWK/GaUcR63Q2WHP90DqQyLlhxmpnS3s7cYABD+afZY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1621443154767955.0603189279402; Wed, 19 May 2021 09:52:34 -0700 (PDT) Received: from localhost ([::1]:48942 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ljPQv-0004mI-FN for importer@patchew.org; Wed, 19 May 2021 12:52:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35372) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ljP7J-0006qp-50 for qemu-devel@nongnu.org; Wed, 19 May 2021 12:32:17 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:60980) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ljP7H-000672-8U for qemu-devel@nongnu.org; Wed, 19 May 2021 12:32:16 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-34-V6j5Gc9oPmWp9KUswrXCXg-1; Wed, 19 May 2021 12:32:11 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 136ECC73A0; Wed, 19 May 2021 16:32:10 +0000 (UTC) Received: from eperezma.remote.csb (ovpn-113-65.ams2.redhat.com [10.36.113.65]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1EFB55D6AC; Wed, 19 May 2021 16:32:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621441934; 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=r7dtTOJdCmRYBm6EIMcBw+fVvfbwCuQ3tsienHtyc0Y=; b=bv3gNVA5GqJwbonOU3h9BctyrWVr5BZMU6KCDxPcNZqBiDaVLkPBnhJHfmwCWEbr0micFS zW+vnJQefVSB9UwGZwKcardw/oZPZY1qQZnb8mDT+vX2OaJJBsUTWNA/SOOL6CoZbcdvVG FCgOvdFjpQi/PwXVn0B3tIiyrwxyIEs= X-MC-Unique: V6j5Gc9oPmWp9KUswrXCXg-1 From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: qemu-devel@nongnu.org Subject: [RFC v3 24/29] vhost: Add iova_rev_maps_alloc Date: Wed, 19 May 2021 18:28:58 +0200 Message-Id: <20210519162903.1172366-25-eperezma@redhat.com> In-Reply-To: <20210519162903.1172366-1-eperezma@redhat.com> References: <20210519162903.1172366-1-eperezma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eperezma@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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=eperezma@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.39, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Parav Pandit , "Michael S. Tsirkin" , Jason Wang , Juan Quintela , Markus Armbruster , virtualization@lists.linux-foundation.org, Harpreet Singh Anand , Xiao W Wang , Stefan Hajnoczi , Eli Cohen , Michael Lilja , Stefano Garzarella Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) This way the tree can store/map a completely new virtual address space, in the range the device admits. It does not limit the range it will allocate, but the IOVA address of the maps are allocated growing. A range limitation will be need for the cases where start_addr !=3D 0. Tools for remove mappings will be needed also. Signed-off-by: Eugenio P=C3=A9rez --- hw/virtio/vhost-iova-tree.h | 11 +++--- hw/virtio/vhost-iova-tree.c | 72 +++++++++++++++++++++++++++++++------ 2 files changed, 69 insertions(+), 14 deletions(-) diff --git a/hw/virtio/vhost-iova-tree.h b/hw/virtio/vhost-iova-tree.h index 589e86bd91..92e2d992b3 100644 --- a/hw/virtio/vhost-iova-tree.h +++ b/hw/virtio/vhost-iova-tree.h @@ -22,16 +22,17 @@ typedef struct VhostDMAMap { } VhostDMAMap; =20 typedef enum VhostDMAMapNewRC { + VHOST_DMA_MAP_NO_SPACE =3D -3, VHOST_DMA_MAP_OVERLAP =3D -2, VHOST_DMA_MAP_INVALID =3D -1, VHOST_DMA_MAP_OK =3D 0, } VhostDMAMapNewRC; =20 /** - * VhostIOVATree - * - * Store and search IOVA -> Translated mappings and the reverse, from - * translated address to IOVA. + * VhostIOVATree, able to: + * - Translate iova address + * - Reverse translate iova address (from translated to iova) + * - Allocate IOVA regions for translated range (potentially slow operatio= n) * * Note that it cannot remove nodes. */ @@ -54,5 +55,7 @@ const VhostDMAMap *vhost_iova_tree_find_taddr(const Vhost= IOVATree *iova_rm, const VhostDMAMap *map); VhostDMAMapNewRC vhost_iova_tree_insert(VhostIOVATree *iova_rm, VhostDMAMap *map); +VhostDMAMapNewRC vhost_iova_tree_alloc(VhostIOVATree *iova_rm, + VhostDMAMap *map); =20 #endif diff --git a/hw/virtio/vhost-iova-tree.c b/hw/virtio/vhost-iova-tree.c index 2900390a1e..7699d96bbb 100644 --- a/hw/virtio/vhost-iova-tree.c +++ b/hw/virtio/vhost-iova-tree.c @@ -187,8 +187,30 @@ const VhostDMAMap *vhost_iova_tree_find_taddr(const Vh= ostIOVATree *tree, vhost_iova_tree_cmp_iova); } =20 +static bool vhost_iova_tree_find_iova_hole(const GArray *iova_map, + const VhostDMAMap *map, + const VhostDMAMap **prev_elem) +{ + size_t i; + hwaddr iova =3D 0; + + *prev_elem =3D NULL; + for (i =3D 0; i < iova_map->len; i++) { + const VhostDMAMap *next =3D &g_array_index(iova_map, typeof(*next)= , i); + hwaddr hole_end =3D next->iova; + if (map->size < hole_end - iova) { + return true; + } + + iova =3D next->iova + next->size + 1; + *prev_elem =3D next; + } + + return ((hwaddr)-1 - iova) > iova_map->len; +} + /** - * Insert a new map + * Insert a new map - internal * * @tree The iova tree * @map The iova map @@ -197,10 +219,13 @@ const VhostDMAMap *vhost_iova_tree_find_taddr(const V= hostIOVATree *tree, * - VHOST_DMA_MAP_OK if the map fits in the container * - VHOST_DMA_MAP_INVALID if the map does not make sense (like size overf= low) * - VHOST_DMA_MAP_OVERLAP if the tree already contains that map - * Can query the assignated iova in map. + * - VHOST_DMA_MAP_NO_SPACE if iova_rm cannot allocate more space. + * + * It returns assignated iova in map->iova if return value is VHOST_DMA_MA= P_OK. */ -VhostDMAMapNewRC vhost_iova_tree_insert(VhostIOVATree *tree, - VhostDMAMap *map) +static VhostDMAMapNewRC vhost_iova_tree_insert_int(VhostIOVATree *tree, + VhostDMAMap *map, + bool allocate) { const VhostDMAMap *qemu_prev, *iova_prev; int find_prev_rc; @@ -210,12 +235,27 @@ VhostDMAMapNewRC vhost_iova_tree_insert(VhostIOVATree= *tree, return VHOST_DMA_MAP_INVALID; } =20 - /* Check for duplicates, and save position for insertion */ - find_prev_rc =3D vhost_iova_tree_find_prev(tree->iova_taddr_map, - vhost_iova_tree_cmp_iova, map, - &iova_prev); - if (find_prev_rc =3D=3D VHOST_DMA_MAP_OVERLAP) { - return VHOST_DMA_MAP_OVERLAP; + if (allocate) { + /* Search for a hole in iova space big enough */ + bool fit =3D vhost_iova_tree_find_iova_hole(tree->iova_taddr_map, = map, + &iova_prev); + if (!fit) { + return VHOST_DMA_MAP_NO_SPACE; + } + + map->iova =3D iova_prev ? (iova_prev->iova + iova_prev->size) + 1 = : 0; + } else { + if (map->iova + map->size < map->iova) { + return VHOST_DMA_MAP_INVALID; + } + + /* Check for duplicates, and save position for insertion */ + find_prev_rc =3D vhost_iova_tree_find_prev(tree->iova_taddr_map, + vhost_iova_tree_cmp_iova,= map, + &iova_prev); + if (find_prev_rc =3D=3D VHOST_DMA_MAP_OVERLAP) { + return VHOST_DMA_MAP_OVERLAP; + } } =20 find_prev_rc =3D vhost_iova_tree_find_prev(tree->taddr_iova_map, @@ -229,3 +269,15 @@ VhostDMAMapNewRC vhost_iova_tree_insert(VhostIOVATree = *tree, vhost_iova_tree_insert_after(tree->taddr_iova_map, qemu_prev, map); return VHOST_DMA_MAP_OK; } + +VhostDMAMapNewRC vhost_iova_tree_insert(VhostIOVATree *tree, + VhostDMAMap *map) +{ + return vhost_iova_tree_insert_int(tree, map, false); +} + +VhostDMAMapNewRC vhost_iova_tree_alloc(VhostIOVATree *tree, + VhostDMAMap *map) +{ + return vhost_iova_tree_insert_int(tree, map, true); +} --=20 2.27.0