From nobody Sun Feb 8 13:53:26 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1642799354205197.05903071649504; Fri, 21 Jan 2022 13:09:14 -0800 (PST) Received: from localhost ([::1]:35274 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nB19l-0000kC-4y for importer@patchew.org; Fri, 21 Jan 2022 16:09:13 -0500 Received: from eggs.gnu.org ([209.51.188.92]:59414) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nB0cJ-0000Hz-AU for qemu-devel@nongnu.org; Fri, 21 Jan 2022 15:34:39 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:26473) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nB0cG-00055c-HH for qemu-devel@nongnu.org; Fri, 21 Jan 2022 15:34:37 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-572-Swp8W-6ZMpKdbnnsEdbOeg-1; Fri, 21 Jan 2022 15:34:31 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8A2E446860; Fri, 21 Jan 2022 20:34:29 +0000 (UTC) Received: from eperezma.remote.csb (unknown [10.39.193.239]) by smtp.corp.redhat.com (Postfix) with ESMTP id 970B2167A8; Fri, 21 Jan 2022 20:34:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1642797276; 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=UXY13WrHKMeplYTVHN85eP6jeQoSwtEprvNFJ53nnMc=; b=BZDqleeV6MRPObVmikoOeAA+Ux7309nhLJc0pcsDz/N8giSLYIqKrP20h4wrgu3qVb8gkz iHaPNhlu2QVjhXM72RoqB0TCiYfgWLtTiiPWYnVl3fwayucgHNLMZ0JVTD80XYgJe9VAJ/ 5GM+Zd34Dpch/sEC0RcPJRY1l+RmvW0= X-MC-Unique: Swp8W-6ZMpKdbnnsEdbOeg-1 From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= To: qemu-devel@nongnu.org Subject: [PATCH 20/31] util: Store DMA entries in a list Date: Fri, 21 Jan 2022 21:27:22 +0100 Message-Id: <20220121202733.404989-21-eperezma@redhat.com> In-Reply-To: <20220121202733.404989-1-eperezma@redhat.com> References: <20220121202733.404989-1-eperezma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.699, 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_H3=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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Laurent Vivier , Parav Pandit , Cindy Lu , "Michael S. Tsirkin" , Jason Wang , Juan Quintela , Richard Henderson , Markus Armbruster , Gautam Dawar , virtualization@lists.linux-foundation.org, Eduardo Habkost , Harpreet Singh Anand , Xiao W Wang , Peter Xu , Stefan Hajnoczi , Eli Cohen , Paolo Bonzini , Zhu Lingshan , Eric Blake , Stefano Garzarella Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1642799356317100001 SVQ need to allocate iova entries, traversing the list looking for holes. GLib offers methods to both transverse the list and look for entries upper than a key since version 2.68. However qemu may need to compile with earlier versions, so we replicate both methods here. Signed-off-by: Eugenio P=C3=A9rez --- util/iova-tree.c | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/util/iova-tree.c b/util/iova-tree.c index ac089101c4..5063a256dd 100644 --- a/util/iova-tree.c +++ b/util/iova-tree.c @@ -11,15 +11,44 @@ =20 #include "qemu/osdep.h" #include "qemu/iova-tree.h" +#include "qemu/queue.h" =20 typedef struct DMAMapInternal { DMAMap map; + QTAILQ_ENTRY(DMAMapInternal) entry; } DMAMapInternal; =20 struct IOVATree { GTree *tree; + QTAILQ_HEAD(, DMAMapInternal) list; }; =20 +/** + * Search function for the upper bound of a given needle. + * + * The upper bound is the first node that has its key strictly greater tha= n the + * searched key. + * + * TODO: A specialized function is available in GTree since Glib 2.68. Rep= lace + * when Glib minimal version is raised. + */ +static int iova_tree_compare_upper_bound(gconstpointer a, gconstpointer b) +{ + const DMAMapInternal *haystack =3D a, *needle =3D b, *prev; + + if (needle->map.iova >=3D haystack->map.iova) { + return 1; + } + + prev =3D QTAILQ_PREV(haystack, entry); + if (!prev || prev->map.iova < needle->map.iova) { + return 0; + } + + /* More data to the left or end of data */ + return -1; +} + static int iova_tree_compare(gconstpointer a, gconstpointer b, gpointer da= ta) { const DMAMapInternal *i1 =3D a, *i2 =3D b; @@ -43,6 +72,7 @@ IOVATree *iova_tree_new(void) =20 /* We don't have values actually, no need to free */ iova_tree->tree =3D g_tree_new_full(iova_tree_compare, NULL, g_free, N= ULL); + QTAILQ_INIT(&iova_tree->list); =20 return iova_tree; } @@ -77,7 +107,7 @@ static inline void iova_tree_insert_internal(GTree *gtre= e, =20 int iova_tree_insert(IOVATree *tree, const DMAMap *map) { - DMAMapInternal *new; + DMAMapInternal *new, *right; =20 if (map->iova + map->size < map->iova || map->perm =3D=3D IOMMU_NONE) { return IOVA_ERR_INVALID; @@ -92,6 +122,15 @@ int iova_tree_insert(IOVATree *tree, const DMAMap *map) memcpy(&new->map, map, sizeof(new->map)); iova_tree_insert_internal(tree->tree, new); =20 + /* Ordered insertion */ + right =3D g_tree_search(tree->tree, iova_tree_compare_upper_bound, new= ); + if (!right) { + /* Empty or bigger than any other entry */ + QTAILQ_INSERT_TAIL(&tree->list, new, entry); + } else { + QTAILQ_INSERT_BEFORE(right, new, entry); + } + return IOVA_OK; } =20 @@ -116,6 +155,7 @@ int iova_tree_remove(IOVATree *tree, const DMAMap *map) DMAMapInternal *overlap_internal; =20 while ((overlap_internal =3D iova_tree_find_internal(tree, map))) { + QTAILQ_REMOVE(&tree->list, overlap_internal, entry); g_tree_remove(tree->tree, overlap_internal); } =20 --=20 2.27.0