From nobody Fri May 17 05:00:08 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@intel.com; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1681900955; cv=none; d=zohomail.com; s=zohoarc; b=B1JsElNrurNQp1q9Ajj6nw+rZHfeV70oArFpuQfdR0FbeIYrYO8yn//rwtcgUelKwlD3a6pwM6NEeXoeL1cFMha6cuS8oMVVG56yR59iusCcy7P3kc4sq3CrqYTYoAgMm7eqg71wSsjG7lD7Su3bR5OKKgb9y+qv+Ad3Yaq05wE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1681900955; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=69zB+wqBLcQGijemGEBFd/5IdmAelJdBk3sCg39EOS4=; b=Ol2VXjTw8fHyYaOiUlLGFaD/6pRZci4PhxP/rdMMtlKCCYjR63O1KD0aXoCBAR/wiZ9PSDE4QYfHhZcFxUiUra7vg5/TG1zyxzq5C4FMMHP+49PE2jERtEqtkmnZW5X6TOVz07XvzltSv/4I2k6E7jbZVcsebLH5nDj1hD3Ca3A= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@intel.com; 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 1681900955023611.5047973791434; Wed, 19 Apr 2023 03:42:35 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pp5Fr-0000xy-FB; Wed, 19 Apr 2023 06:41:39 -0400 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 1pp5Fp-0000xa-DX for qemu-devel@nongnu.org; Wed, 19 Apr 2023 06:41:37 -0400 Received: from mga05.intel.com ([192.55.52.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pp5Fm-0005FD-F1 for qemu-devel@nongnu.org; Wed, 19 Apr 2023 06:41:37 -0400 Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Apr 2023 03:41:28 -0700 Received: from duan-server-s2600bt.bj.intel.com ([10.240.192.143]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Apr 2023 03:41:24 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1681900894; x=1713436894; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=dbR9N8KvQguZFppT/f+4kSx1JJuXFg8MeWhH4HZG2Ls=; b=R1SqOAYclavSL6qyqFbpf2KgCcC84je32KmT++Sq1VuAsrGxNlPQRH+x jdCr4dQrPcvbQItHi0qKCMcWb2F6Zy49aKWw4xTiBUT+BosTbEYbe3KA5 11jtdnzwOm05ELO18loN+vkRgxWX0b1qbNHwEKb+5nsGKcqa2TT7dAc0d AA2e0xkGZnVanVi8CivBJvmCuPwNtQeRULq/uVBcd0Hs9atywfIQ6/5UQ 0Ses8BUoauyfqafSHBT1uQPaMacjGaEgUMVtCUgIGqWu4zKApX/u28DIe JvMgx8a6pC9M8r9NjhPG07J9hZDD7eyqRJB2mktYpV0sOqUNPTpbWqZs9 Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10684"; a="431690844" X-IronPort-AV: E=Sophos;i="5.99,208,1677571200"; d="scan'208";a="431690844" X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10684"; a="815591304" X-IronPort-AV: E=Sophos;i="5.99,208,1677571200"; d="scan'208";a="815591304" From: Zhenzhong Duan To: qemu-devel@nongnu.org Cc: mst@redhat.com, peterx@redhat.com, jasowang@redhat.com, marcel.apfelbaum@gmail.com, pbonzini@redhat.com, richard.henderson@linaro.org, eduardo@habkost.net, david@redhat.com, philmd@linaro.org, peter.maydell@linaro.org, chao.p.peng@intel.com Subject: [PATCH v4] memory: Optimize replay of guest mapping Date: Wed, 19 Apr 2023 18:29:40 +0800 Message-Id: <20230419102940.185968-1-zhenzhong.duan@intel.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 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=192.55.52.43; envelope-from=zhenzhong.duan@intel.com; helo=mga05.intel.com X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 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_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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 @intel.com) X-ZM-MESSAGEID: 1681900958088100001 Content-Type: text/plain; charset="utf-8" On x86, there are two notifiers registered due to vtd-ir memory region splitting the entire address space. During replay of the address space for each notifier, the whole address space is scanned which is unnecessary. We only need to scan the space belong to notifier monitored space. While on x86 IOMMU memory region spans over entire address space, but on some other platforms(e.g. arm mps3-an547), IOMMU memory region is only a window in the entire address space. User could register a notifier with arbitrary scope beyond IOMMU memory region. Though in current implementation replay is only triggered by VFIO and dirty page sync with notifiers derived from memory region section, but this isn't guaranteed in the future. So, we replay the intersection part of IOMMU memory region and notifier's scope in memory_region_iommu_replay(). Update doc comment to match this change. Signed-off-by: Zhenzhong Duan Reviewed-by: Peter Xu --- v4: Update doc comment per Perter Maydell v3: Fix assert failure on mps3-an547, fix typos in log per Michael v2: Add an assert per Peter Xu Tested on x86 with a net card passed to guest(kvm/tcg), ping/ssh pass. Also did simple bootup test with mps3-an547 hw/i386/intel_iommu.c | 2 +- include/exec/memory.h | 13 +++++++------ softmmu/memory.c | 5 +++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index a62896759c78..faade7def867 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -3850,7 +3850,7 @@ static void vtd_iommu_replay(IOMMUMemoryRegion *iommu= _mr, IOMMUNotifier *n) .domain_id =3D vtd_get_domain_id(s, &ce, vtd_as->pasid), }; =20 - vtd_page_walk(s, &ce, 0, ~0ULL, &info, vtd_as->pasid); + vtd_page_walk(s, &ce, n->start, n->end, &info, vtd_as->pasid); } } else { trace_vtd_replay_ce_invalid(bus_n, PCI_SLOT(vtd_as->devfn), diff --git a/include/exec/memory.h b/include/exec/memory.h index 15ade918baa4..61da32d8a428 100644 --- a/include/exec/memory.h +++ b/include/exec/memory.h @@ -425,12 +425,13 @@ struct IOMMUMemoryRegionClass { * Called to handle memory_region_iommu_replay(). * * The default implementation of memory_region_iommu_replay() is to - * call the IOMMU translate method for every page in the address space + * call the IOMMU translate method for every page falling in the + * intersection part of IOMMU memory region and notifier's scope * with flag =3D=3D IOMMU_NONE and then call the notifier if translate * returns a valid mapping. If this method is implemented then it * overrides the default behaviour, and must provide the full semantics * of memory_region_iommu_replay(), by calling @notifier for every - * translation present in the IOMMU. + * translation present in the intersection part. * * Optional method -- an IOMMU only needs to provide this method * if the default is inefficient or produces undesirable side effects. @@ -1760,9 +1761,10 @@ int memory_region_register_iommu_notifier(MemoryRegi= on *mr, IOMMUNotifier *n, Error **errp); =20 /** - * memory_region_iommu_replay: replay existing IOMMU translations to + * memory_region_iommu_replay: replay existing IOMMU translations in + * intersection part of IOMMU memory region and notifier's scope to * a notifier with the minimum page granularity returned by - * mr->iommu_ops->get_page_size(). + * memory_region_iommu_get_min_page_size(). * * Note: this is not related to record-and-replay functionality. * @@ -1775,8 +1777,7 @@ void memory_region_iommu_replay(IOMMUMemoryRegion *io= mmu_mr, IOMMUNotifier *n); * memory_region_unregister_iommu_notifier: unregister a notifier for * changes to IOMMU translation entries. * - * @mr: the memory region which was observed and for which notity_stopped() - * needs to be called + * @mr: the memory region which was observed. * @n: the notifier to be removed. */ void memory_region_unregister_iommu_notifier(MemoryRegion *mr, diff --git a/softmmu/memory.c b/softmmu/memory.c index b1a6cae6f583..f7af691991de 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -1925,7 +1925,7 @@ void memory_region_iommu_replay(IOMMUMemoryRegion *io= mmu_mr, IOMMUNotifier *n) { MemoryRegion *mr =3D MEMORY_REGION(iommu_mr); IOMMUMemoryRegionClass *imrc =3D IOMMU_MEMORY_REGION_GET_CLASS(iommu_m= r); - hwaddr addr, granularity; + hwaddr addr, end, granularity; IOMMUTLBEntry iotlb; =20 /* If the IOMMU has its own replay callback, override */ @@ -1935,8 +1935,9 @@ void memory_region_iommu_replay(IOMMUMemoryRegion *io= mmu_mr, IOMMUNotifier *n) } =20 granularity =3D memory_region_iommu_get_min_page_size(iommu_mr); + end =3D MIN(n->end, memory_region_size(mr)); =20 - for (addr =3D 0; addr < memory_region_size(mr); addr +=3D granularity)= { + for (addr =3D n->start; addr < end; addr +=3D granularity) { iotlb =3D imrc->translate(iommu_mr, addr, IOMMU_NONE, n->iommu_idx= ); if (iotlb.perm !=3D IOMMU_NONE) { n->notify(n, &iotlb); --=20 2.25.1