From nobody Mon Feb 9 11:51:24 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.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 ARC-Seal: i=1; a=rsa-sha256; t=1562359826; cv=none; d=zoho.com; s=zohoarc; b=VjiVtaUXlWtTwP3/SwD5P2OZ6ZMQWhqd/Mp9ZOTmsWP5b6tC9C6DdmusjnH2YcDkoCmOEl5IF+n6xPnn9wqn4jd8MqtyowojgcrPqqFU62M4T+dNKP1iw2XjHaUzeSvw8trjPxi5JRsNgN+zif/I/65WvdpIxsa/prxnSSQhko4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562359826; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=YbMFVYWCeviKweLwBNufDDcBIGFAoGlnFAeGpWGi3rw=; b=ZdH4ZLc5LldtUu0B/CT6iqV7zEHAymgA9DwQUk8xNdgK+ThjVeyvfgT//lrKhewQVS9K5hEkLC3mOMIUDjJhIZ/GOuLy1bU8T6268zXzF8O38TZ1zAVub5gr3V3KHFME5gDAqq3M7r1pkhcHYmdta9fTLHZdQzjFRHxeiGyPfsI= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.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 header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1562359826369700.6783132900614; Fri, 5 Jul 2019 13:50:26 -0700 (PDT) Received: from localhost ([::1]:56044 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hjV9z-0004BJ-Hy for importer@patchew.org; Fri, 05 Jul 2019 16:50:23 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34184) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hjUk9-0004Le-W3 for qemu-devel@nongnu.org; Fri, 05 Jul 2019 16:23:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hjUk4-0002na-1U for qemu-devel@nongnu.org; Fri, 05 Jul 2019 16:23:39 -0400 Received: from mail-wm1-x334.google.com ([2a00:1450:4864:20::334]:54295) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hjUjz-0002hm-UF for qemu-devel@nongnu.org; Fri, 05 Jul 2019 16:23:33 -0400 Received: by mail-wm1-x334.google.com with SMTP id p74so7245931wme.4 for ; Fri, 05 Jul 2019 13:23:29 -0700 (PDT) Received: from 640k.lan ([93.56.166.5]) by smtp.gmail.com with ESMTPSA id v23sm3035875wmj.32.2019.07.05.13.23.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 05 Jul 2019 13:23:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=YbMFVYWCeviKweLwBNufDDcBIGFAoGlnFAeGpWGi3rw=; b=KEzBGU2Ou3kgfZGkF5Ya6OSGrCnDBMFvgtGMofrjM19PvoAGDM5ohTQSiFJkGVYoYo +mzClx+Z2+zpwSekFPBKvUjDxma8KQNYa0m2wTxPwcr09d0kL8n6+xrOSdwBn1wW1Qf4 tjKd3j7UPady/nKffKQD1CKOoW+iPshGiLv7C7W7OdLfsUIh6rDH5kZTDUh5yumGY+ep o/8Xv6O40dwpmHC8r7lA97uSU1n6zezCBGX441bItQ6hanmGWCBJ+oKCjYOiQ955nStU Lu5lQfiD5TbuMgoHgYbRamozwDskp7LCNyTj3PENMYWlVBkcf5dIN8qYNyvZCkP9FdB+ L/HA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=YbMFVYWCeviKweLwBNufDDcBIGFAoGlnFAeGpWGi3rw=; b=sOZHiUVqvwxZFhJUSyprk5D4BZ3NBMytJ7h1DXMbPXnSjPRTVKS1cR9+viwv+GpTwo mKQZrpqlrzZLar4S4rhEvnJVl5fYpRJBLQ07E8iDhqss1oFTUzsmCQqCTfTl2uhhD+0x U6QVXtYLNOAJR+fJU2VRRwXpZd2V9y8h9AoOpclOrGsQxWHioImCgtAv0j0mCS0ARNnk C80jcVn+gVVBmIzZoHqhtq6/xs1vt41eJzVqV5+aSjlALNY7cp1+QKlpWAY4UxCxPjBl 4USurWG7xm+oWrZSwd6YmZxeXY7jjtD0QQNKo5Yia/jmWh3S0pXWAGrks+lTdB0MHkOh 91ZQ== X-Gm-Message-State: APjAAAUkN+9+sHKbdZfi9AS8OwBDt+naWpcbzJ17Z3wVN0PC37N6DSrI PBel3ZALMVstQkm0TAonLuRq6+fcmMk= X-Google-Smtp-Source: APXvYqymIRyzAL4rxwdCgo+OY1nYDJfeRjvyeW+ERyP9srQfUiOT1oQuR73YkMCqDve8ny1ntixC+Q== X-Received: by 2002:a7b:c776:: with SMTP id x22mr4477030wmk.55.1562358208303; Fri, 05 Jul 2019 13:23:28 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 5 Jul 2019 22:23:15 +0200 Message-Id: <1562358202-28008-6-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1562358202-28008-1-git-send-email-pbonzini@redhat.com> References: <1562358202-28008-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::334 Subject: [Qemu-devel] [PULL 05/12] intel_iommu: Fix unexpected unmaps during global unmap X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Xu Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Peter Xu This is an replacement work of Yan Zhao's patch: https://www.mail-archive.com/qemu-devel@nongnu.org/msg625340.html vtd_address_space_unmap() will do proper page mask alignment to make sure each IOTLB message will have correct masks for notification messages (2^N-1), but sometimes it can be expanded to even supercede the registered range. That could lead to unexpected UNMAP of already mapped regions in some other notifiers. Instead of doing mindless expension of the start address and address mask, we split the range into smaller ones and guarantee that each small range will have correct masks (2^N-1) and at the same time we should also try our best to generate as less IOTLB messages as possible. Reported-by: Yan Zhao Signed-off-by: Peter Xu Reviewed-by: Eric Auger Tested-by: Yan Zhao Message-Id: <20190624091811.30412-3-peterx@redhat.com> Signed-off-by: Paolo Bonzini --- hw/i386/intel_iommu.c | 67 +++++++++++++++++++++++++++++++----------------= ---- 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 719ce19..de86f53 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -3363,11 +3363,28 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s= , PCIBus *bus, int devfn) return vtd_dev_as; } =20 +static uint64_t get_naturally_aligned_size(uint64_t start, + uint64_t size, int gaw) +{ + uint64_t max_mask =3D 1ULL << gaw; + uint64_t alignment =3D start ? start & -start : max_mask; + + alignment =3D MIN(alignment, max_mask); + size =3D MIN(size, max_mask); + + if (alignment <=3D size) { + /* Increase the alignment of start */ + return alignment; + } else { + /* Find the largest page mask from size */ + return 1ULL << (63 - clz64(size)); + } +} + /* Unmap the whole range in the notifier's scope. */ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) { - IOMMUTLBEntry entry; - hwaddr size; + hwaddr size, remain; hwaddr start =3D n->start; hwaddr end =3D n->end; IntelIOMMUState *s =3D as->iommu_state; @@ -3388,39 +3405,37 @@ static void vtd_address_space_unmap(VTDAddressSpace= *as, IOMMUNotifier *n) } =20 assert(start <=3D end); - size =3D end - start; + size =3D remain =3D end - start + 1; =20 - if (ctpop64(size) !=3D 1) { - /* - * This size cannot format a correct mask. Let's enlarge it to - * suite the minimum available mask. - */ - int n =3D 64 - clz64(size); - if (n > s->aw_bits) { - /* should not happen, but in case it happens, limit it */ - n =3D s->aw_bits; - } - size =3D 1ULL << n; + while (remain >=3D VTD_PAGE_SIZE) { + IOMMUTLBEntry entry; + uint64_t mask =3D get_naturally_aligned_size(start, remain, s->aw_= bits); + + assert(mask); + + entry.iova =3D start; + entry.addr_mask =3D mask - 1; + entry.target_as =3D &address_space_memory; + entry.perm =3D IOMMU_NONE; + /* This field is meaningless for unmap */ + entry.translated_addr =3D 0; + + memory_region_notify_one(n, &entry); + + start +=3D mask; + remain -=3D mask; } =20 - entry.target_as =3D &address_space_memory; - /* Adjust iova for the size */ - entry.iova =3D n->start & ~(size - 1); - /* This field is meaningless for unmap */ - entry.translated_addr =3D 0; - entry.perm =3D IOMMU_NONE; - entry.addr_mask =3D size - 1; + assert(!remain); =20 trace_vtd_as_unmap_whole(pci_bus_num(as->bus), VTD_PCI_SLOT(as->devfn), VTD_PCI_FUNC(as->devfn), - entry.iova, size); + n->start, size); =20 - map.iova =3D entry.iova; - map.size =3D entry.addr_mask; + map.iova =3D n->start; + map.size =3D size; iova_tree_remove(as->iova_tree, &map); - - memory_region_notify_one(n, &entry); } =20 static void vtd_address_space_unmap_all(IntelIOMMUState *s) --=20 1.8.3.1