From nobody Tue May 13 16:12:00 2025 Delivered-To: importer2@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+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1678215244; cv=none; d=zohomail.com; s=zohoarc; b=UkiyFdJRf4Vf7nbarah2OQ6cOEnu3MWrs0wPtTki4DRk38oVm283j3szndxdAbdMbi256SvZPWkrb19bSvubxBNa8kNXwkDKugFl4gRj9ffcMwD3g+MKpWzsYNG2iCR/fd3QFmRKeZgCFRiA9n6jppiEm4OWtdjPxU0cmgYYB9U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678215244; 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=2TFV2gewErDB/rs/5Hn2jPe4NJtuvDYjYPFLxyPJ/c8=; b=F33QnTmUSa+XiyGqZO/hgF+4hmaJJxIuW97Va2u5K/P58T8us8pi8xw6TwVUgmmp0vV18Pt9gTWLiPhdptLhLEqzMuMFOuHeROHZkEFtwuQiA1AEj1bq6IrCAD+V5C+dqeURrwo3fswOWGuGUucOPYLir1aBHtNQGptT43b3HBE= 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+importer2=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 1678215244949116.8092314492792; Tue, 7 Mar 2023 10:54:04 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pZcQx-0002r4-6N; Tue, 07 Mar 2023 13:53:11 -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 1pZcQv-0002qg-Dm for qemu-devel@nongnu.org; Tue, 07 Mar 2023 13:53:09 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pZcQt-0002V4-SM for qemu-devel@nongnu.org; Tue, 07 Mar 2023 13:53:09 -0500 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-31-JzZIq0-tNt6OfViemb_3SQ-1; Tue, 07 Mar 2023 13:53:05 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1A4708828C1; Tue, 7 Mar 2023 18:53:05 +0000 (UTC) Received: from [172.30.41.16] (unknown [10.22.16.79]) by smtp.corp.redhat.com (Postfix) with ESMTP id A8D042026D4B; Tue, 7 Mar 2023 18:53:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1678215187; 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=2TFV2gewErDB/rs/5Hn2jPe4NJtuvDYjYPFLxyPJ/c8=; b=bjqPbWkLE3aadmV6zmW8TSNbJy6SNTpI4wsH+kUsHLDJbp6YjMJZ2EiUW6S/rqfmOtZoJM ztFfkhQrh9JBJkMW4gaDtlsXwGHLK/FK4JzRMiAePpWClTPVVTGHxF+wRGS/yfFtOi2wH/ ySV+Rbh5UvWmosHyAtQd317a/6//T2E= X-MC-Unique: JzZIq0-tNt6OfViemb_3SQ-1 Subject: [PULL 08/17] vfio/common: Add helper to consolidate iova/end calculation From: Alex Williamson To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, avihaih@nvidia.com, clg@redhat.com, joao.m.martins@oracle.com Date: Tue, 07 Mar 2023 11:53:04 -0700 Message-ID: <167821518431.619792.12932302611023643451.stgit@omen> In-Reply-To: <167821508699.619792.1719671327865445814.stgit@omen> References: <167821508699.619792.1719671327865445814.stgit@omen> User-Agent: StGit/1.5.dev2+g9ce680a52bd9 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 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+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=alex.williamson@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, 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, 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+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1678215245608100005 From: Joao Martins In preparation to be used in device dirty tracking, move the code that calculate a iova/end range from the container/section. This avoids duplication on the common checks across listener callbacks. Signed-off-by: Joao Martins Reviewed-by: C=C3=A9dric Le Goater Link: https://lore.kernel.org/r/20230307125450.62409-9-joao.m.martins@oracl= e.com Signed-off-by: Alex Williamson --- hw/vfio/common.c | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index d38b7c1969b9..63831eab78a1 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -962,6 +962,31 @@ static bool vfio_listener_valid_section(MemoryRegionSe= ction *section, return true; } =20 +static bool vfio_get_section_iova_range(VFIOContainer *container, + MemoryRegionSection *section, + hwaddr *out_iova, hwaddr *out_end, + Int128 *out_llend) +{ + Int128 llend; + hwaddr iova; + + iova =3D REAL_HOST_PAGE_ALIGN(section->offset_within_address_space); + llend =3D int128_make64(section->offset_within_address_space); + llend =3D int128_add(llend, section->size); + llend =3D int128_and(llend, int128_exts64(qemu_real_host_page_mask())); + + if (int128_ge(int128_make64(iova), llend)) { + return false; + } + + *out_iova =3D iova; + *out_end =3D int128_get64(int128_sub(llend, int128_one())); + if (out_llend) { + *out_llend =3D llend; + } + return true; +} + static void vfio_listener_region_add(MemoryListener *listener, MemoryRegionSection *section) { @@ -977,12 +1002,7 @@ static void vfio_listener_region_add(MemoryListener *= listener, return; } =20 - iova =3D REAL_HOST_PAGE_ALIGN(section->offset_within_address_space); - llend =3D int128_make64(section->offset_within_address_space); - llend =3D int128_add(llend, section->size); - llend =3D int128_and(llend, int128_exts64(qemu_real_host_page_mask())); - - if (int128_ge(int128_make64(iova), llend)) { + if (!vfio_get_section_iova_range(container, section, &iova, &end, &lle= nd)) { if (memory_region_is_ram_device(section->mr)) { trace_vfio_listener_region_add_no_dma_map( memory_region_name(section->mr), @@ -992,7 +1012,6 @@ static void vfio_listener_region_add(MemoryListener *l= istener, } return; } - end =3D int128_get64(int128_sub(llend, int128_one())); =20 if (container->iommu_type =3D=3D VFIO_SPAPR_TCE_v2_IOMMU) { hwaddr pgsize =3D 0; @@ -1219,15 +1238,9 @@ static void vfio_listener_region_del(MemoryListener = *listener, */ } =20 - iova =3D REAL_HOST_PAGE_ALIGN(section->offset_within_address_space); - llend =3D int128_make64(section->offset_within_address_space); - llend =3D int128_add(llend, section->size); - llend =3D int128_and(llend, int128_exts64(qemu_real_host_page_mask())); - - if (int128_ge(int128_make64(iova), llend)) { + if (!vfio_get_section_iova_range(container, section, &iova, &end, &lle= nd)) { return; } - end =3D int128_get64(int128_sub(llend, int128_one())); =20 llsize =3D int128_sub(llend, int128_make64(iova)); =20