From nobody Fri May 17 06:53:59 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=1681384367; cv=none; d=zohomail.com; s=zohoarc; b=VppCtyveK/PMzwQ2W7Hlw+hY5AKdD0zsbyjqBSNFilVWtrLSF3T0wecLQnYNMaXYOnSENfgPdGxCmOw8I3DKEHBJ4VA9UxxEjzO2f9yMCJ+naDSukfCfGmKDhSoC6VoDvvWe7V8UiZcZYGj8fzkzzFfRce+W5lie0gOq49BsySE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1681384367; 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=o40O5L9fpmxOGqNghuqv3lpwsO4jWOX+SGtQvTNlAy4=; b=nzl/dDEW8fzLVAC4ztXsWQ9Sdo6flG4JWOdvZ7YnDHl25yctrPWCJ/fXXH7Cqar9lg/uHNLSoo9GGyVXZc2xjhxzAtXHqRXyLWPi/fAidgQTRy8iKUUUt1Lm38pqLXeA7Q6K/9xBl+kmHmM0Hvgdhn9uf2foblQen9agAx8FZGY= 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 1681384367345642.7738142577895; Thu, 13 Apr 2023 04:12:47 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pmusQ-0001OX-Hp; Thu, 13 Apr 2023 07:12:30 -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 1pmusN-0001O0-VE for qemu-devel@nongnu.org; Thu, 13 Apr 2023 07:12:28 -0400 Received: from mga03.intel.com ([134.134.136.65]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pmusL-0003d2-1k for qemu-devel@nongnu.org; Thu, 13 Apr 2023 07:12:27 -0400 Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Apr 2023 04:12:00 -0700 Received: from duan-server-s2600bt.bj.intel.com ([10.240.192.143]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Apr 2023 04:11:57 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1681384345; x=1712920345; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=uDRw/5tKMmWlFUbbtkcAmcMIP72IJWNu8FG74PCmeL8=; b=ZRUkSz2s2rHIL9Mbsmo9/9bSaQjQdL4SRKhIyzkkNh80NqUed9jcoww1 AHOvoG9fmpp/5On7IUdTz0W5axzYPnpU16TLxEGCsmzmdGYrF+A2LF6ga wnDwvrXnRZoSqGGFpgEi0dXOk447V6anzrooo/SS0/w+eIw35BUcqqOD+ 4kO/b/lW6gjxFTXqcAd5SKH3CeKGbUPKKDhcwFQ+CcAa4CyceraE28YIU KMnpRiBlNoWlWCzdr6Er5aNEYh7Et5tgmmipFSx8Ikoxxl3a2341ZIJbW 0dfXm0RPV/GvOpelrjsRSM9jayZrITcLUTiVThVO1dCZaPFeRQq/kpjny A==; X-IronPort-AV: E=McAfee;i="6600,9927,10678"; a="346839208" X-IronPort-AV: E=Sophos;i="5.99,341,1677571200"; d="scan'208";a="346839208" X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10678"; a="800778098" X-IronPort-AV: E=Sophos;i="5.99,341,1677571200"; d="scan'208";a="800778098" 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 Subject: [PATCH v3] memory: Optimize replay of guest mapping Date: Thu, 13 Apr 2023 19:00:19 +0800 Message-Id: <20230413110019.48922-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=134.134.136.65; envelope-from=zhenzhong.duan@intel.com; helo=mga03.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 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: 1681384368479100003 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 whole 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 IOMMU notifier in memory_region_iommu_replay(). Signed-off-by: Zhenzhong Duan Reviewed-by: Peter Xu --- v3: Fix assert failure on mps3-an547 v2: Add an assert per Peter 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 +- softmmu/memory.c | 5 +++-- 2 files changed, 4 insertions(+), 3 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/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