From patchwork Mon Jul 23 20:16:37 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 10540925 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6505E112B for ; Mon, 23 Jul 2018 20:40:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 50A222849D for ; Mon, 23 Jul 2018 20:40:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 44ECE284A5; Mon, 23 Jul 2018 20:40:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id B6B7A2849D for ; Mon, 23 Jul 2018 20:40:24 +0000 (UTC) Received: from localhost ([::1]:36453 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fhhd1-0003u0-R4 for patchwork-qemu-devel@patchwork.kernel.org; Mon, 23 Jul 2018 16:40:23 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41154) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fhhIl-0001qu-SQ for qemu-devel@nongnu.org; Mon, 23 Jul 2018 16:19:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fhhIk-0003Cd-7M for qemu-devel@nongnu.org; Mon, 23 Jul 2018 16:19:27 -0400 Received: from mail-oi0-x231.google.com ([2607:f8b0:4003:c06::231]:42531) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fhhIk-0003CN-1V; Mon, 23 Jul 2018 16:19:26 -0400 Received: by mail-oi0-x231.google.com with SMTP id n84-v6so3462201oib.9; Mon, 23 Jul 2018 13:19:25 -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=GyQ1vIoBPmPJ8juY4kfmrKlZq3tVvoPLM+z7ItIsYqA=; b=FNnFu+zCcsVQXYF7KuVLwEFBh/p3B9OL4PIv74/fjdnN9lBIRwYY3TjO5iHP8xisK0 81SB+dCxVPszPhDXVGsXAW/tMv31dlHnBU/L+UnKKFd7A80MdD+ydBFnayTSZblEQq8a D+49y86C2BS7FDlBgHViKNxA2h2hQ9JjcCaSL6diTffdX9yzvhBtxvT1ChOxbwn0njFG 1q7htsO6jy5s5cut8lyYOyh49eps5Gg7cMyqyJEwgCsDJTeamnpYSAtRbncHXMmlam7B RE4iVBVQFtxRs7ynUfvWUfxPw882GE8Srbut+uqxkIDyMFqNQIwhKhpQSe/uSGl2UH/K PyMw== 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=GyQ1vIoBPmPJ8juY4kfmrKlZq3tVvoPLM+z7ItIsYqA=; b=rGROOiUYjdly4tBF6jAkJoPj/qStSnW7UPiGaCytjYoZ7cv5QXlVu8u9zuJcT+0Tew AtwTeLvplxkcGPjBT4tpgAp9hXBVdBxaUlKKTdD2xUNnn/hMoN5IHHQO6XFYzB4VxXzA WZDMnsRd1XmZxF0uh1m6CqPLbRd2C0jljKo8POZKw2G0PQc/m8hGaUNAekTgMnQOeydg MF+PAjcfJm9qbj5vO2UzGHY2AfJ4mH+LrF9gR2Yctu0vlwMmttDQ8Iwn8LeF2nmq9fPc 5TslKKgJabcKIdjGnHiZ7fI7TXgbfsUhEDeDZ/cTGT9PQXTiQGkZF1ULUFRu7i7H0TK2 IlUA== X-Gm-Message-State: AOUpUlHNCTTR+uVq0WZh//Zh1h+hvgbrKEkUT0yzQ5CX6qTSAlFeZMgl So/3e8UQVhMIez794hpXQisJEqq0ius= X-Google-Smtp-Source: AAOMgpemIH7vNUBt/qY1/j6/lh4WXoFmGml4m+Zbt6hP/5QTseIFUEt0nASTulFmXIkgC7k2L1uT1A== X-Received: by 2002:aca:cf43:: with SMTP id f64-v6mr307286oig.113.1532377164820; Mon, 23 Jul 2018 13:19:24 -0700 (PDT) Received: from localhost (76-251-165-188.lightspeed.austtx.sbcglobal.net. [76.251.165.188]) by smtp.gmail.com with ESMTPSA id y85-v6sm10623996oie.25.2018.07.23.13.19.23 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 23 Jul 2018 13:19:23 -0700 (PDT) From: Michael Roth To: qemu-devel@nongnu.org Date: Mon, 23 Jul 2018 15:16:37 -0500 Message-Id: <20180723201748.25573-29-mdroth@linux.vnet.ibm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180723201748.25573-1-mdroth@linux.vnet.ibm.com> References: <20180723201748.25573-1-mdroth@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4003:c06::231 Subject: [Qemu-devel] [PATCH 28/99] intel-iommu: only do page walk for MAP notifiers X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-stable@nongnu.org, Peter Xu , "Michael S . Tsirkin" Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Peter Xu For UNMAP-only IOMMU notifiers, we don't need to walk the page tables. Fasten that procedure by skipping the page table walk. That should boost performance for UNMAP-only notifiers like vhost. CC: QEMU Stable Signed-off-by: Peter Xu Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin (cherry picked from commit 4f8a62a933a79094e44bc1b16b63bb23e62d67b4) Signed-off-by: Michael Roth --- hw/i386/intel_iommu.c | 44 +++++++++++++++++++++++++++++++---- include/hw/i386/intel_iommu.h | 2 ++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 8d4069de9f..38ccc741f9 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -138,6 +138,12 @@ static inline void vtd_iommu_unlock(IntelIOMMUState *s) qemu_mutex_unlock(&s->iommu_lock); } +/* Whether the address space needs to notify new mappings */ +static inline gboolean vtd_as_has_map_notifier(VTDAddressSpace *as) +{ + return as->notifier_flags & IOMMU_NOTIFIER_MAP; +} + /* GHashTable functions */ static gboolean vtd_uint64_equal(gconstpointer v1, gconstpointer v2) { @@ -1436,14 +1442,36 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s, VTDAddressSpace *vtd_as; VTDContextEntry ce; int ret; + hwaddr size = (1 << am) * VTD_PAGE_SIZE; QLIST_FOREACH(vtd_as, &(s->vtd_as_with_notifiers), next) { ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus), vtd_as->devfn, &ce); if (!ret && domain_id == VTD_CONTEXT_ENTRY_DID(ce.hi)) { - vtd_page_walk(&ce, addr, addr + (1 << am) * VTD_PAGE_SIZE, - vtd_page_invalidate_notify_hook, - (void *)&vtd_as->iommu, true, s->aw_bits); + if (vtd_as_has_map_notifier(vtd_as)) { + /* + * As long as we have MAP notifications registered in + * any of our IOMMU notifiers, we need to sync the + * shadow page table. + */ + vtd_page_walk(&ce, addr, addr + size, + vtd_page_invalidate_notify_hook, + (void *)&vtd_as->iommu, true, s->aw_bits); + } else { + /* + * For UNMAP-only notifiers, we don't need to walk the + * page tables. We just deliver the PSI down to + * invalidate caches. + */ + IOMMUTLBEntry entry = { + .target_as = &address_space_memory, + .iova = addr, + .translated_addr = 0, + .addr_mask = size - 1, + .perm = IOMMU_NONE, + }; + memory_region_notify_iommu(&vtd_as->iommu, entry); + } } } } @@ -2383,6 +2411,9 @@ static void vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu, exit(1); } + /* Update per-address-space notifier flags */ + vtd_as->notifier_flags = new; + if (old == IOMMU_NOTIFIER_NONE) { QLIST_INSERT_HEAD(&s->vtd_as_with_notifiers, vtd_as, next); } else if (new == IOMMU_NOTIFIER_NONE) { @@ -2891,8 +2922,11 @@ static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n) PCI_FUNC(vtd_as->devfn), VTD_CONTEXT_ENTRY_DID(ce.hi), ce.hi, ce.lo); - vtd_page_walk(&ce, 0, ~0ULL, vtd_replay_hook, (void *)n, false, - s->aw_bits); + if (vtd_as_has_map_notifier(vtd_as)) { + /* This is required only for MAP typed notifiers */ + vtd_page_walk(&ce, 0, ~0ULL, vtd_replay_hook, (void *)n, false, + s->aw_bits); + } } else { trace_vtd_replay_ce_invalid(bus_n, PCI_SLOT(vtd_as->devfn), PCI_FUNC(vtd_as->devfn)); diff --git a/include/hw/i386/intel_iommu.h b/include/hw/i386/intel_iommu.h index 016e74bedb..156f35e919 100644 --- a/include/hw/i386/intel_iommu.h +++ b/include/hw/i386/intel_iommu.h @@ -93,6 +93,8 @@ struct VTDAddressSpace { IntelIOMMUState *iommu_state; VTDContextCacheEntry context_cache_entry; QLIST_ENTRY(VTDAddressSpace) next; + /* Superset of notifier flags that this address space has */ + IOMMUNotifierFlag notifier_flags; }; struct VTDBus {