From patchwork Mon Mar 5 17:01:11 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Sironi, Filippo" X-Patchwork-Id: 10259353 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 0506360365 for ; Mon, 5 Mar 2018 17:01:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EA52828B80 for ; Mon, 5 Mar 2018 17:01:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DEC2B28B8A; Mon, 5 Mar 2018 17:01:55 +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.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9217228B80 for ; Mon, 5 Mar 2018 17:01:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932069AbeCERBn (ORCPT ); Mon, 5 Mar 2018 12:01:43 -0500 Received: from smtp-fw-6002.amazon.com ([52.95.49.90]:23495 "EHLO smtp-fw-6002.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751852AbeCERBl (ORCPT ); Mon, 5 Mar 2018 12:01:41 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1520269301; x=1551805301; h=from:to:cc:subject:date:message-id; bh=cknK28xvM/WrrK+bFy36t18/rfe/h+Rv3jM0HID7Ntg=; b=XZiywviuoRQDo+ZrQsPVCt4+D3W2mQIookCRFOMstoDqbuNbBkYie1QT eCuQqSUiAOi7VxIVT56twCyQNcohlj3mmrrmRCMswBHYBC+bkPWYdrbAl 1S3r12X5thkG50gvdq1pCI3D8x8Dd3YaP0MWKNyuK0TGsVSLtWVl0NRy5 8=; X-IronPort-AV: E=Sophos;i="5.47,427,1515456000"; d="scan'208";a="334437406" Received: from iad6-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-1a-715bee71.us-east-1.amazon.com) ([10.124.125.6]) by smtp-border-fw-out-6002.iad6.amazon.com with ESMTP/TLS/DHE-RSA-AES256-SHA; 05 Mar 2018 17:01:39 +0000 Received: from uf8b156e456a5587c9af4.ant.amazon.com (iad1-ws-svc-lb91-vlan2.amazon.com [10.0.103.146]) by email-inbound-relay-1a-715bee71.us-east-1.amazon.com (8.14.7/8.14.7) with ESMTP id w25H1Wf1024440 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Mon, 5 Mar 2018 17:01:35 GMT Received: from uf8b156e456a5587c9af4.ant.amazon.com (localhost [127.0.0.1]) by uf8b156e456a5587c9af4.ant.amazon.com (8.15.2/8.15.2/Debian-3) with ESMTP id w25H1WW7012722; Mon, 5 Mar 2018 18:01:32 +0100 Received: (from sironi@localhost) by uf8b156e456a5587c9af4.ant.amazon.com (8.15.2/8.15.2/Submit) id w25H1VmE012719; Mon, 5 Mar 2018 18:01:31 +0100 From: Filippo Sironi To: alex.williamson@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Filippo Sironi Subject: [PATCH] vfio/type1: Search for a fitting iommu_domain before attaching the iommu_group Date: Mon, 5 Mar 2018 18:01:11 +0100 Message-Id: <1520269271-12662-1-git-send-email-sironi@amazon.de> X-Mailer: git-send-email 2.7.4 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP ... to avoid an unnecessary attach/detach of the iommu_group to the newly created iommu_domain. This also saves us a context-cache and an IOTLB flush. This is possible because allocating an iommu_domain for the iommu_group we're attaching is enough to understand whether a fitting iommu_domain already exists. Signed-off-by: Filippo Sironi Cc: Alex Williamson Cc: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- drivers/vfio/vfio_iommu_type1.c | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 45657e2b1ff7..88359b4993f3 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -1279,15 +1279,8 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, goto out_domain; } - ret = iommu_attach_group(domain->domain, iommu_group); - if (ret) - goto out_domain; - resv_msi = vfio_iommu_has_sw_msi(iommu_group, &resv_msi_base); - INIT_LIST_HEAD(&domain->group_list); - list_add(&group->next, &domain->group_list); - msi_remap = irq_domain_check_msi_remap() || iommu_capable(bus, IOMMU_CAP_INTR_REMAP); @@ -1295,7 +1288,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, pr_warn("%s: No interrupt remapping support. Use the module param \"allow_unsafe_interrupts\" to enable VFIO IOMMU support on this platform\n", __func__); ret = -EPERM; - goto out_detach; + goto out_domain; } if (iommu_capable(bus, IOMMU_CAP_CACHE_COHERENCY)) @@ -1311,21 +1304,24 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, list_for_each_entry(d, &iommu->domain_list, next) { if (d->domain->ops == domain->domain->ops && d->prot == domain->prot) { - iommu_detach_group(domain->domain, iommu_group); - if (!iommu_attach_group(d->domain, iommu_group)) { - list_add(&group->next, &d->group_list); - iommu_domain_free(domain->domain); - kfree(domain); - mutex_unlock(&iommu->lock); - return 0; - } - - ret = iommu_attach_group(domain->domain, iommu_group); + ret = iommu_attach_group(d->domain, iommu_group); if (ret) goto out_domain; + list_add(&group->next, &d->group_list); + iommu_domain_free(domain->domain); + kfree(domain); + mutex_unlock(&iommu->lock); + return 0; } } + ret = iommu_attach_group(domain->domain, iommu_group); + if (ret) + goto out_domain; + + INIT_LIST_HEAD(&domain->group_list); + list_add(&group->next, &domain->group_list); + vfio_test_domain_fgsp(domain); /* replay mappings on new domains */