Received: by 10.223.185.116 with SMTP id b49csp2812095wrg; Mon, 5 Mar 2018 09:03:17 -0800 (PST) X-Google-Smtp-Source: AG47ELtqT8AITY1687rQdf62h+vmf4HSS9w6mbwQDFQw7gqe0Jz4tkEQBbl9PKvY5uzvZyzHr7CE X-Received: by 2002:a17:902:7b90:: with SMTP id w16-v6mr13394633pll.26.1520269397343; Mon, 05 Mar 2018 09:03:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520269397; cv=none; d=google.com; s=arc-20160816; b=O+5UnN3ZbKyWeVQEw4JPIzR+8wSSnq0WgRFfsFHK0h33IVkiqj4GeZaDf9UT/4qvNG ZOoRgarr7a+hsp5ggeBxH/BqXbATO8KPXVmSGlheqfgChkeLOvIAcQ37AzLB6G1ClzdU nA1criUtiBvOy4DawaAg9+SAe/Qo9KyGWluH1nYNZy+RG4kyY2wfeY/g1IRGQ3t5+Cme QbiulB6L5SkFcpjaJdIV8ic+AiEMV9YrDfB1xoyEEJtkIpfErZpewR9YKOZ/rjyTdkO3 9YyxclfPY/n3gthpVqeygW4dBR3HEKy+bYZhg4oDd7176FaLrXn529ofpJ60QTNRyVA3 kdVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=cknK28xvM/WrrK+bFy36t18/rfe/h+Rv3jM0HID7Ntg=; b=cnV0O8k/eWb3Jdsomc7ReRz9Svd7nRZ70AdCEl4t4utBjoWcPy5z+wpojuCGcnNadm NIMv2aK9mbXIFZStuHpxhYOBc83iGa9H8UeDd0mp3sfvnR3/QLlkqX332fA6YsOXa71k OA3e4eOyu0G0lKXqm0+iqDS3i7itpUhsNJIqMYaCSr8BWhPOBNHPpyj6P9VwsndAhRVW PLSRJGKaib/DjFJUsjRKScKVBba9E01yn1JNc86KhmeTpg4D8q3U9MSlIsgRA6lBxwfX hs9T2SsiJEX+HndYT/+Hy0El1bTiuYVgaA4/zeYsKy8jwNrJWIvIL2NpkwEeOLik8Dox KsVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.de header.s=amazon201209 header.b=XZiywviu; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g1-v6si9535744plm.399.2018.03.05.09.02.57; Mon, 05 Mar 2018 09:03:17 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.de header.s=amazon201209 header.b=XZiywviu; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932112AbeCERBo (ORCPT + 99 others); Mon, 5 Mar 2018 12:01:44 -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: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ... 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 */ -- 2.7.4