From nobody Sat May 18 13:16:15 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=1676444054; cv=none; d=zohomail.com; s=zohoarc; b=VNIxz6ZWOcOx1/RC0zadIs6pXF9WGh/6x/zWDbjfNYMQig2AuKEF0ouFgOeF2zh+bo0GFC5pqz6y7uUAdUpNKxSsy0INTTheUzjLQsfPsEHzRi1+VUUOejRBiBFHOfao5XsOK8toMbpu7IrQtWgi+69Lado46RhPMFdGBBMOM3E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1676444054; 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=sA6ibgKVFSIw+JK6PO+N3EJk357wZ2Y7Sda9hpxJIFc=; b=Z8o42nkZLVzUv30Gv57mYVKFUaP7PCOo1Du57erUtIiZ72KuFJCguTT+Li1y2gzal2QgbpLVldJfuqzwYcmqf+mlO4TT+MTIol2vuSZ8wymO1ox1qUITQ70n6MyH7BkmLl65iQyKiDXR/z9hbqeFDKqc0TaHTLAJa9Gh0kcs6fE= 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 1676444054726185.8239369454617; Tue, 14 Feb 2023 22:54:14 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pSBfd-0002hc-2B; Wed, 15 Feb 2023 01:53:37 -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 1pSBfb-0002hT-8G for qemu-devel@nongnu.org; Wed, 15 Feb 2023 01:53:35 -0500 Received: from mga11.intel.com ([192.55.52.93]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pSBfY-0001Zl-TD for qemu-devel@nongnu.org; Wed, 15 Feb 2023 01:53:34 -0500 Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Feb 2023 22:53:22 -0800 Received: from duan-server-s2600bt.bj.intel.com ([10.240.192.143]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Feb 2023 22:53:19 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676444012; x=1707980012; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=843NaJWNaZDkXWQY5W3dnEFjXq9eLgs0LtNR+AkbbuE=; b=PY7eyGVOe7HTXJtetJnkNwA1N5e4FnQ8w7q5ymq35aEOrd/SPgQdnrYx 8WBc2aLc3McUS4YJgu1/0LrYPChZkwpv7/GhmZA+h70wy4kNIAmLiwYTV QtxlmEnjX2UZgRnUM9GIxjxP2T6QKiiZ7KmC5GWqPZ6ZYi9HrBpC1v4pg k8V2OjGT8m2khWwHdUZUVMmiIQb1INk+WH99Qvh1OlsI57LP+ZA2s9qMx YGFjZVihMpHhlR4zANCdmYEJkXsZLH2cR5DX49u3AtcBQ0bZrQo0JvZ0b DWblqTndFLzMaxfFF3nz4JcorPiYrL6Jf70T6FdfaCkZwFn2yirCgqH5k w==; X-IronPort-AV: E=McAfee;i="6500,9779,10621"; a="329077241" X-IronPort-AV: E=Sophos;i="5.97,299,1669104000"; d="scan'208";a="329077241" X-IronPort-AV: E=McAfee;i="6500,9779,10621"; a="758293252" X-IronPort-AV: E=Sophos;i="5.97,299,1669104000"; d="scan'208";a="758293252" From: Zhenzhong Duan To: qemu-devel@nongnu.org Cc: mst@redhat.com, peterx@redhat.com, jasowang@redhat.com, pbonzini@redhat.com, richard.henderson@linaro.org, eduardo@habkost.net, marcel.apfelbaum@gmail.com, david@redhat.com, philmd@linaro.org Subject: [PATCH v2] memory: Optimize replay of guest mapping Date: Wed, 15 Feb 2023 14:52:38 +0800 Message-Id: <20230215065238.713041-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.93; envelope-from=zhenzhong.duan@intel.com; helo=mga11.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: 1676444056795100001 Content-Type: text/plain; charset="utf-8" On x86, there are two notifiers registered due to vtd-ir memory region splitting the whole address space. During replay of the address space for each notifier, the whole address space is scanned which is unnecessory. We only need to scan the space belong to notifier montiored space. Assert when notifier is used to monitor beyond iommu memory region's address space. Signed-off-by: Zhenzhong Duan Acked-by: Jason Wang Acked-by: Peter Xu --- v2: Add an assertion per Peter Xu Tested only on x86 with a net card passed to guest(kvm/tcg), ping/ssh pass. hw/i386/intel_iommu.c | 2 +- softmmu/memory.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 98a5c304a7d7..6b1de80e8573 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -3831,7 +3831,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 9d64efca269b..da7d84661972 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -1900,6 +1900,7 @@ int memory_region_register_iommu_notifier(MemoryRegio= n *mr, iommu_mr =3D IOMMU_MEMORY_REGION(mr); assert(n->notifier_flags !=3D IOMMU_NOTIFIER_NONE); assert(n->start <=3D n->end); + assert(n->end <=3D memory_region_size(mr)); assert(n->iommu_idx >=3D 0 && n->iommu_idx < memory_region_iommu_num_indexes(iommu_mr)); =20 @@ -1923,7 +1924,6 @@ uint64_t memory_region_iommu_get_min_page_size(IOMMUM= emoryRegion *iommu_mr) =20 void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier= *n) { - MemoryRegion *mr =3D MEMORY_REGION(iommu_mr); IOMMUMemoryRegionClass *imrc =3D IOMMU_MEMORY_REGION_GET_CLASS(iommu_m= r); hwaddr addr, granularity; IOMMUTLBEntry iotlb; @@ -1936,7 +1936,7 @@ void memory_region_iommu_replay(IOMMUMemoryRegion *io= mmu_mr, IOMMUNotifier *n) =20 granularity =3D memory_region_iommu_get_min_page_size(iommu_mr); =20 - for (addr =3D 0; addr < memory_region_size(mr); addr +=3D granularity)= { + for (addr =3D n->start; addr < n->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