From patchwork Fri Jul 1 21:44:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 12903798 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B5700C433EF for ; Fri, 1 Jul 2022 21:46:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=AXYZhWVQeDMyPFxuRZppDOe79s3eeS0PGOHdLpHupz8=; b=fH9Rg3Vvbp5bLD B6pv5T2XTZdYzVi/toKu35cE1eua83cN4sq0nuEmIxXlBf5av6oFts0RF1IksHbA2ucsl/rX3QqeY Zj/9PXJfrBIdrlTEtiQTbh+1/9P72X4T3uL+k0SKb1s8fnv8mHWTo25bm2bSZnI6IrNm8IsNzOJye qQxBaKMgdFL5SPG4T/HwuTnYkUUw0687XINtNqpKto7xmgLFJU2CHbBZA2vxzuoHVPqYxhLrrznGE DsSlQSa7FKG8laXB7aQfrGcrGWGPXToZQrua8kEbX27co5Pl9fn4VTfsVN6IhuWo1T5gFbxBviNZd js1FWKHe2JpfLrYd3Mxw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o7OS5-00749e-JA; Fri, 01 Jul 2022 21:45:25 +0000 Received: from mail-bn7nam10on2071.outbound.protection.outlook.com ([40.107.92.71] helo=NAM10-BN7-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1o7ORq-0073xd-Jx for linux-arm-kernel@lists.infradead.org; Fri, 01 Jul 2022 21:45:12 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IChMtKf0R/iOR7pytjbKYjLOU1L1KYEZKnnZKzrBjM6+47ewkuv8QYOFuZOMA3AKjJ8spd0+M5+zvU8TVKQb3VPIZlx5RiES9buJGr9RARe/IbjJdSyUBXbmwk8wCh3P8YasBDNyxPtSumKBPiT2+oNYmcu4lX7xempdfJBFAlsBjvdgYObSBX0qmrl83kYcHKyF5dv3NPj0AVVdN7H6tcwD2/kgPwDCvUJ9+rNvM1GP/uF/Tet5zgqm200cAI0J8wwHB4sySHFZ5MAcKOJDLCeehOSyq8IMCf5EVilggtRYaZw3d613OAb4LKEj7meimIYnnQ/diktg19I4CGu++Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=BAJf9+wzngUYHhzYRGKJeseya+Rr4fgAG4Yv4gHz1Kg=; b=CXSDQg5jZOt1r3ShhMtVY/vpjXlAZNl0pmni3oSiLXLT1ogUSBwvT2nE+zddUqvUp3AwOhHPozuMlua/CokQasdpQv/XrL6pT0J8LNVVfw08o4YUya3DrM5mGd/F/mNIpgntRguRIp9RlWyWeuLn8+HpQAP0HZe5wSkvG98Eg2wkImXIO9jM+U0rD4cQN+wNM0Zxk27L/tEzXWkfAjL++eXchf16SP/N41KJxEy2eBrdpA+h89Sd6GbWtIlokx/QPgWXEbA+fsMGZiOkK0eoF1qzg1OOrmCWxDCVads8mxDE3fj6k+qgxsVor5SbLR+shwt/nENobmCNYAxKd6fMOg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.234) smtp.rcpttodomain=gmail.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=BAJf9+wzngUYHhzYRGKJeseya+Rr4fgAG4Yv4gHz1Kg=; b=krk6YGqIzh3tA392esqpjNXhRwYlsQXhG0PiCOBduoZr6sEHHwhxlplpzIiKuoe4rgFUeTHBfX1SFhuIk98yADwt2pV7KIHCFij/TMHwI7xNxw/Uw+f7kECkwtzdxpryO6ena/1Fk1i1RHmBmX9J8plnvezr3sQNxPrN1SMsr9vvA+YmJt8QFld7Dno6DgfASm81iNbDQkkebYuKRyD7boHx7Vi3peX5PJ/Qx4dm+m5lZ1z0zLFk7smN/wO1vu/a1yR5UBFTZw2oY3LgRN8PNlgMEUAIXi7j+27h4ZUWLQ4o4K1wrUpzgGNgnWYers898R9mfxw7pLK20zzHl771iQ== Received: from BN6PR20CA0059.namprd20.prod.outlook.com (2603:10b6:404:151::21) by CY5PR12MB6622.namprd12.prod.outlook.com (2603:10b6:930:42::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5395.14; Fri, 1 Jul 2022 21:45:05 +0000 Received: from BN8NAM11FT064.eop-nam11.prod.protection.outlook.com (2603:10b6:404:151:cafe::18) by BN6PR20CA0059.outlook.office365.com (2603:10b6:404:151::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5395.14 via Frontend Transport; Fri, 1 Jul 2022 21:45:05 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.234) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.234 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.234; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (12.22.5.234) by BN8NAM11FT064.mail.protection.outlook.com (10.13.176.160) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5395.14 via Frontend Transport; Fri, 1 Jul 2022 21:45:04 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) by DRHQMAIL101.nvidia.com (10.27.9.10) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Fri, 1 Jul 2022 21:45:04 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail202.nvidia.com (10.129.68.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.26; Fri, 1 Jul 2022 14:45:03 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.10) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.986.26 via Frontend Transport; Fri, 1 Jul 2022 14:45:01 -0700 From: Nicolin Chen To: , , , , , , , , , , , , , Subject: [PATCH v5 1/5] iommu: Return -EMEDIUMTYPE for incompatible domain and device/group Date: Fri, 1 Jul 2022 14:44:51 -0700 Message-ID: <20220701214455.14992-2-nicolinc@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220701214455.14992-1-nicolinc@nvidia.com> References: <20220701214455.14992-1-nicolinc@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 4d96553b-de02-4f2c-962a-08da5baaf5b2 X-MS-TrafficTypeDiagnostic: CY5PR12MB6622:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: s4f4/lP0JusuAN091ATJrZ/fq9i0bO35KQWSGK0/o3eB95FF9Sv/Y4rUwXfx7+OYLFtprKMLY93fpSMzaxkEcu5u/brVCuuCQPwLVX6Fb1wzfd0YryXzIIwBujMMWkuwBoCV6RdqhMgwKBN/+k5HvQZedl85Dxe1lNL7daSpnLk9xNEVVPZelMkyiqFh9cOSVQEOoowanXlpzaa8tetCP74lZA5mL3ugrVV8tLHfKi839YQ2UarhsrjqpFcsFYhkrZF3ukXumzpZDI2GZHDa4t2T4lcRBF+ET57zCIw3xLu+w/NKFigRWGRdQU6mXtTuDVCZt4bfabeyzXMbMFpH++PDgmTf9szfwnPt1csxmYwGiXYLMePlB86zTNbPZqc9fghvJsLqHIVoizOzBBEYTJFb+gB92znh1AC7b2IsKDBE3ukMPr47E/DZ0RnOmENUSGRcom2LYW84hwfY2+GnzHwdgoewQdAdHwTYbReUgbZ/fnBoqaVdWInCJwzgwkr2NDMUp8ztZC/vIhjoWGE75y+RT8thDDAWPSLFqX8MWWUCqJ/Sn3V7m2lS5EdWsXflCmTjlyzxzGTA6ebpey1IBJekKr5cdE2O7JoJKnCae8eQyp7IVSUv6Hda58uyzhpuXSw/8eEeJKonOv8MYPjsDUmtOzOl5CUkm9EflCOmIqlt7WfSusBekx9THgoNpO7+Bu6/FQqes1Lqe5x2twk0Cv3n7KkNmru5FKezKChHkgtTvVJ8Fbh3ykfGGVrnd0SN3nrZl34rM3/psxHqvCyUKk27jJZGT0FBNGOGh8zjELpTUKbU2SxQCTArSEsX/T+eEX6k86nP89V/nHhlZaSD5xc4wkD/2LUyWfOTp9di9AY6bx9mdXks1IvIgObZsO35CzCBM2V9X6iq0suzJeEsP8DOCIOVwVSb6U7IQPoPZZdNPIUeYpC7AOAJq4adG3WhEluJT7XfVIfYSwMgziDKzCY6Bout6SLU60c0JcLmkHg= X-Forefront-Antispam-Report: CIP:12.22.5.234;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:InfoNoRecords;CAT:NONE;SFS:(13230016)(4636009)(396003)(39860400002)(376002)(136003)(346002)(36840700001)(40470700004)(46966006)(82310400005)(36756003)(40480700001)(316002)(5660300002)(7406005)(70586007)(110136005)(30864003)(8936002)(8676002)(4326008)(7416002)(2906002)(54906003)(86362001)(478600001)(36860700001)(2616005)(6666004)(70206006)(426003)(1076003)(7696005)(47076005)(336012)(186003)(41300700001)(83380400001)(356005)(82740400003)(81166007)(40460700003)(921005)(26005)(334744004)(36900700001)(83996005)(2101003)(473944003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Jul 2022 21:45:04.8536 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4d96553b-de02-4f2c-962a-08da5baaf5b2 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[12.22.5.234];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT064.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY5PR12MB6622 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220701_144510_767419_41B04FB3 X-CRM114-Status: GOOD ( 23.71 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mjrosato@linux.ibm.com, virtualization@lists.linux-foundation.org, thierry.reding@gmail.com, alyssa@rosenzweig.io, linux-s390@vger.kernel.org, kvm@vger.kernel.org, jonathanh@nvidia.com, iommu@lists.linux.dev, yangyingliang@huawei.com, gerald.schaefer@linux.ibm.com, linux-arm-msm@vger.kernel.org, john.garry@huawei.com, christophe.jaillet@wanadoo.fr, thunder.leizhen@huawei.com, linux-tegra@vger.kernel.org, linux-arm-kernel@lists.infradead.org, chenxiang66@hisilicon.com, cohuck@redhat.com, linux-kernel@vger.kernel.org, iommu@lists.linux-foundation.org, suravee.suthikulpanit@amd.com, dwmw2@infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Cases like VFIO wish to attach a device to an existing domain that was not allocated specifically from the device. This raises a condition where the IOMMU driver can fail the domain attach because the domain and device are incompatible with each other. This is a soft failure that can be resolved by using a different domain. Provide a dedicated errno from the IOMMU driver during attach that the reason attached failed is because of domain incompatability. EMEDIUMTYPE is chosen because it is never used within the iommu subsystem today and evokes a sense that the 'medium' aka the domain is incompatible. VFIO can use this to know attach is a soft failure and it should continue searching. Otherwise the attach will be a hard failure and VFIO will return the code to userspace. Update all drivers to return EMEDIUMTYPE in their failure paths that are related to domain incompatability. Also remove adjacent error prints for these soft failures, to prevent a kernel log spam, since -EMEDIUMTYPE is clear enough to indicate an incompatability error. Add kdocs describing this behavior. Suggested-by: Jason Gunthorpe Reviewed-by: Kevin Tian Reviewed-by: Lu Baolu Signed-off-by: Nicolin Chen Reviewed-by: Jason Gunthorpe --- drivers/iommu/amd/iommu.c | 2 +- drivers/iommu/apple-dart.c | 4 +-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 15 +++-------- drivers/iommu/arm/arm-smmu/arm-smmu.c | 5 +--- drivers/iommu/arm/arm-smmu/qcom_iommu.c | 9 ++----- drivers/iommu/intel/iommu.c | 10 +++----- drivers/iommu/iommu.c | 28 +++++++++++++++++++++ drivers/iommu/ipmmu-vmsa.c | 4 +-- drivers/iommu/omap-iommu.c | 3 +-- drivers/iommu/s390-iommu.c | 2 +- drivers/iommu/sprd-iommu.c | 6 ++--- drivers/iommu/tegra-gart.c | 2 +- drivers/iommu/virtio-iommu.c | 3 +-- 13 files changed, 47 insertions(+), 46 deletions(-) diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index 840831d5d2ad..ad499658a6b6 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -1662,7 +1662,7 @@ static int attach_device(struct device *dev, if (domain->flags & PD_IOMMUV2_MASK) { struct iommu_domain *def_domain = iommu_get_dma_domain(dev); - ret = -EINVAL; + ret = -EMEDIUMTYPE; if (def_domain->type != IOMMU_DOMAIN_IDENTITY) goto out; diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c index 8af0242a90d9..e58dc310afd7 100644 --- a/drivers/iommu/apple-dart.c +++ b/drivers/iommu/apple-dart.c @@ -495,10 +495,10 @@ static int apple_dart_attach_dev(struct iommu_domain *domain, if (cfg->stream_maps[0].dart->force_bypass && domain->type != IOMMU_DOMAIN_IDENTITY) - return -EINVAL; + return -EMEDIUMTYPE; if (!cfg->stream_maps[0].dart->supports_bypass && domain->type == IOMMU_DOMAIN_IDENTITY) - return -EINVAL; + return -EMEDIUMTYPE; ret = apple_dart_finalize_domain(domain, cfg); if (ret) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c index 88817a3376ef..5b64138f549d 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2420,24 +2420,15 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) goto out_unlock; } } else if (smmu_domain->smmu != smmu) { - dev_err(dev, - "cannot attach to SMMU %s (upstream of %s)\n", - dev_name(smmu_domain->smmu->dev), - dev_name(smmu->dev)); - ret = -ENXIO; + ret = -EMEDIUMTYPE; goto out_unlock; } else if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1 && master->ssid_bits != smmu_domain->s1_cfg.s1cdmax) { - dev_err(dev, - "cannot attach to incompatible domain (%u SSID bits != %u)\n", - smmu_domain->s1_cfg.s1cdmax, master->ssid_bits); - ret = -EINVAL; + ret = -EMEDIUMTYPE; goto out_unlock; } else if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1 && smmu_domain->stall_enabled != master->stall_enabled) { - dev_err(dev, "cannot attach to stall-%s domain\n", - smmu_domain->stall_enabled ? "enabled" : "disabled"); - ret = -EINVAL; + ret = -EMEDIUMTYPE; goto out_unlock; } diff --git a/drivers/iommu/arm/arm-smmu/arm-smmu.c b/drivers/iommu/arm/arm-smmu/arm-smmu.c index 2ed3594f384e..4b95673be076 100644 --- a/drivers/iommu/arm/arm-smmu/arm-smmu.c +++ b/drivers/iommu/arm/arm-smmu/arm-smmu.c @@ -1167,10 +1167,7 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) * different SMMUs. */ if (smmu_domain->smmu != smmu) { - dev_err(dev, - "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n", - dev_name(smmu_domain->smmu->dev), dev_name(smmu->dev)); - ret = -EINVAL; + ret = -EMEDIUMTYPE; goto rpm_put; } diff --git a/drivers/iommu/arm/arm-smmu/qcom_iommu.c b/drivers/iommu/arm/arm-smmu/qcom_iommu.c index 4c077c38fbd6..8372f985c14a 100644 --- a/drivers/iommu/arm/arm-smmu/qcom_iommu.c +++ b/drivers/iommu/arm/arm-smmu/qcom_iommu.c @@ -381,13 +381,8 @@ static int qcom_iommu_attach_dev(struct iommu_domain *domain, struct device *dev * Sanity check the domain. We don't support domains across * different IOMMUs. */ - if (qcom_domain->iommu != qcom_iommu) { - dev_err(dev, "cannot attach to IOMMU %s while already " - "attached to domain on IOMMU %s\n", - dev_name(qcom_domain->iommu->dev), - dev_name(qcom_iommu->dev)); - return -EINVAL; - } + if (qcom_domain->iommu != qcom_iommu) + return -EMEDIUMTYPE; return 0; } diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 44016594831d..db5fb799e350 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -4323,19 +4323,15 @@ static int prepare_domain_attach_device(struct iommu_domain *domain, return -ENODEV; if (dmar_domain->force_snooping && !ecap_sc_support(iommu->ecap)) - return -EOPNOTSUPP; + return -EMEDIUMTYPE; /* check if this iommu agaw is sufficient for max mapped address */ addr_width = agaw_to_width(iommu->agaw); if (addr_width > cap_mgaw(iommu->cap)) addr_width = cap_mgaw(iommu->cap); - if (dmar_domain->max_addr > (1LL << addr_width)) { - dev_err(dev, "%s: iommu width (%d) is not " - "sufficient for the mapped address (%llx)\n", - __func__, addr_width, dmar_domain->max_addr); - return -EFAULT; - } + if (dmar_domain->max_addr > (1LL << addr_width)) + return -EMEDIUMTYPE; dmar_domain->gaw = addr_width; /* diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 847ad47a2dfd..5b0afe39275e 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -1972,6 +1972,20 @@ static int __iommu_attach_device(struct iommu_domain *domain, return ret; } +/** + * iommu_attach_device - Attach a device to an IOMMU domain + * @domain: IOMMU domain to attach + * @dev: Device that will be attached + * + * Returns 0 on success and error code on failure + * + * Specifically, -EMEDIUMTYPE is returned as a soft failure if the domain and + * the device are incompatible in some way. This indicates that a caller should + * try another existing IOMMU domain or allocate a new one. And note that it's + * recommended to keep kernel print free when reporting -EMEDIUMTYPE error, as + * this function can be called to test compatibility with domains that will fail + * the test, which will result in a kernel log spam. + */ int iommu_attach_device(struct iommu_domain *domain, struct device *dev) { struct iommu_group *group; @@ -2098,6 +2112,20 @@ static int __iommu_attach_group(struct iommu_domain *domain, return ret; } +/** + * iommu_attach_group - Attach an IOMMU group to an IOMMU domain + * @domain: IOMMU domain to attach + * @group: IOMMU group that will be attached + * + * Returns 0 on success and error code on failure + * + * Specifically, -EMEDIUMTYPE is returned as a soft failure if the domain and + * the device are incompatible in some way. This indicates that a caller should + * try another existing IOMMU domain or allocate a new one. And note that it's + * recommended to keep kernel print free when reporting -EMEDIUMTYPE error, as + * this function can be called to test compatibility with domains that will fail + * the test, which will result in a kernel log spam. + */ int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group) { int ret; diff --git a/drivers/iommu/ipmmu-vmsa.c b/drivers/iommu/ipmmu-vmsa.c index 8fdb84b3642b..82d63394b166 100644 --- a/drivers/iommu/ipmmu-vmsa.c +++ b/drivers/iommu/ipmmu-vmsa.c @@ -628,9 +628,7 @@ static int ipmmu_attach_device(struct iommu_domain *io_domain, * Something is wrong, we can't attach two devices using * different IOMMUs to the same domain. */ - dev_err(dev, "Can't attach IPMMU %s to domain on IPMMU %s\n", - dev_name(mmu->dev), dev_name(domain->mmu->dev)); - ret = -EINVAL; + ret = -EMEDIUMTYPE; } else dev_info(dev, "Reusing IPMMU context %u\n", domain->context_id); diff --git a/drivers/iommu/omap-iommu.c b/drivers/iommu/omap-iommu.c index d9cf2820c02e..6bc8925726bf 100644 --- a/drivers/iommu/omap-iommu.c +++ b/drivers/iommu/omap-iommu.c @@ -1471,8 +1471,7 @@ omap_iommu_attach_dev(struct iommu_domain *domain, struct device *dev) /* only a single client device can be attached to a domain */ if (omap_domain->dev) { - dev_err(dev, "iommu domain is already attached\n"); - ret = -EBUSY; + ret = -EMEDIUMTYPE; goto out; } diff --git a/drivers/iommu/s390-iommu.c b/drivers/iommu/s390-iommu.c index c898bcbbce11..ddcb78b284bb 100644 --- a/drivers/iommu/s390-iommu.c +++ b/drivers/iommu/s390-iommu.c @@ -127,7 +127,7 @@ static int s390_iommu_attach_device(struct iommu_domain *domain, /* Allow only devices with identical DMA range limits */ } else if (domain->geometry.aperture_start != zdev->start_dma || domain->geometry.aperture_end != zdev->end_dma) { - rc = -EINVAL; + rc = -EMEDIUMTYPE; spin_unlock_irqrestore(&s390_domain->list_lock, flags); goto out_restore; } diff --git a/drivers/iommu/sprd-iommu.c b/drivers/iommu/sprd-iommu.c index bd409bab6286..f6ae230ca1cd 100644 --- a/drivers/iommu/sprd-iommu.c +++ b/drivers/iommu/sprd-iommu.c @@ -237,10 +237,8 @@ static int sprd_iommu_attach_device(struct iommu_domain *domain, struct sprd_iommu_domain *dom = to_sprd_domain(domain); size_t pgt_size = sprd_iommu_pgt_size(domain); - if (dom->sdev) { - pr_err("There's already a device attached to this domain.\n"); - return -EINVAL; - } + if (dom->sdev) + return -EMEDIUMTYPE; dom->pgt_va = dma_alloc_coherent(sdev->dev, pgt_size, &dom->pgt_pa, GFP_KERNEL); if (!dom->pgt_va) diff --git a/drivers/iommu/tegra-gart.c b/drivers/iommu/tegra-gart.c index a6700a40a6f8..011c33e6ae31 100644 --- a/drivers/iommu/tegra-gart.c +++ b/drivers/iommu/tegra-gart.c @@ -112,7 +112,7 @@ static int gart_iommu_attach_dev(struct iommu_domain *domain, spin_lock(&gart->dom_lock); if (gart->active_domain && gart->active_domain != domain) { - ret = -EBUSY; + ret = -EMEDIUMTYPE; } else if (dev_iommu_priv_get(dev) != domain) { dev_iommu_priv_set(dev, domain); gart->active_domain = domain; diff --git a/drivers/iommu/virtio-iommu.c b/drivers/iommu/virtio-iommu.c index 25be4b822aa0..a41a62dccb4d 100644 --- a/drivers/iommu/virtio-iommu.c +++ b/drivers/iommu/virtio-iommu.c @@ -733,8 +733,7 @@ static int viommu_attach_dev(struct iommu_domain *domain, struct device *dev) */ ret = viommu_domain_finalise(vdev, domain); } else if (vdomain->viommu != vdev->viommu) { - dev_err(dev, "cannot attach to foreign vIOMMU\n"); - ret = -EXDEV; + ret = -EMEDIUMTYPE; } mutex_unlock(&vdomain->mutex); From patchwork Fri Jul 1 21:44:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 12903799 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 850F1C43334 for ; Fri, 1 Jul 2022 21:46:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=45oHtz9Bn32SsM4B4PMVRkNvtAt2yMTut2MJB0S4v6g=; b=pud7w3s2OfGR0e 11/jLdsDPSNmStY8630teUZ/t0C3EvBUeuifqQKF6zsO2pvI37jJsfx6XRahQhjrC4IGiSskqvBf6 AqBdaWakinxD3M5RZYpOLLjKfsJ5VL8EOn2qeZCm3swdY+4yw72gfhNmjqFG6FatSB3ZF7Cc3ahyE CKTjIdXgZOooOPnQtM9TM1bqDin17kosvVkOsUbWam+0ALBJ+AtdfzHDKYKKksz5TFpi0TuwiSwot 2Oozzz58i0rAGLojDN2ndnF8zV1o4aKnlOT3Zybu3x+TJ5Km7q8MfxeSV3qpNrQX0XGslwjruOu3v MIjZJ69c/qU/0bH045uA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o7OSE-0074Cs-SC; Fri, 01 Jul 2022 21:45:35 +0000 Received: from mail-mw2nam12on2085.outbound.protection.outlook.com ([40.107.244.85] helo=NAM12-MW2-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1o7ORs-0073ya-4A for linux-arm-kernel@lists.infradead.org; Fri, 01 Jul 2022 21:45:13 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mdZPlIPf5uc7hbEKNE1o7UwgP/yWwIBY3YDAXmsUM/QXGU/CanHQyevK4gDrF2XtcacEOdndCU6nCw6+3da9ruxAncfaQrVyb8gQNuIPFJhDc1j5jaRBm4z/2N/ZmAxWbhuB5pKn4rNB0+IFyfkuwq5/GbqreTPs1y3/hyd55WHgY2hxqopkGrJrWzq86XQP9HqMv/aYaKZlNo5UkzZ7oBElnNbVM3z9kG4lD4WEitbgJarjAgDLZmziodXVY73KlttI1m4bNsHGqg5jMrtD5CZo4z74dNhKdatSdTFkUaic+4HkRNFYkl/u5BXeiUk6+UBqSebPkgan5KKAtdvAiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Q8c0HJirraBV0iUyoB0bUoXWVwy2+oMhqJqz0e4/bLU=; b=nUYB8WrBaXZmAKPJBjHYuV9NqeFr9fLJO6W/617deaErfhDcB4foH7CClVVNLgwT2tlcLJtOfq/rRJgVNp16ye9JiphcDg/BEwqOO9s+/9Bjyejarmsm/JJStWJSjSd56Dczb6sQCF/8qJgqJtwtEH3Gp8C76kl8bDuLGWgcTHpl1hoYOA6EOdwIxeEUrRhXGys5WHdELQDrq7wJfKv57Z9C+5Qx6FlC8PJYJEOiFIG45B0v0cbQS3+w7rtrdRSSJloNyY/I1mucUFs/m6RODQhzPThEa7pnNeZc+Dd189dBjlBkkAhFX59dLYwxlOfgjIWjrqLQ817bcBnxtrwpgg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.238) smtp.rcpttodomain=gmail.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Q8c0HJirraBV0iUyoB0bUoXWVwy2+oMhqJqz0e4/bLU=; b=lYeXHVlB9hUo3gS9L+JZ7cukYO8ZQLkY6gnbbpnnoqpkuwgInm0qysDQ9oLYWd9HlXk0zhvvBrgpqeJsKHbuoTPF6xG8/UwsjznPmp2h/MUcXrP5uxk6XfpwKy0p4ucBimIjXmS9NveySpNCeKdXIq/TvlCUQOVMu5eoeTRWLakipJm0Huojot6klB/xUyxgZB/dhDwIbCSt4vttYvsI0Qj6GzM/nvU6iSp+y3wuBVucC1xZq4gqPWfnUgsI53AzxY/Vq/ZvsCwlovzjJOG8kLKnQ6+eKqvgNtkHesoVuffx0jfQigOCw1Q1TbAyw3LPMm1KXmm59heBvYM6CnJ6DQ== Received: from MW4PR03CA0259.namprd03.prod.outlook.com (2603:10b6:303:b4::24) by PH7PR12MB6000.namprd12.prod.outlook.com (2603:10b6:510:1dc::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5395.14; Fri, 1 Jul 2022 21:45:07 +0000 Received: from CO1NAM11FT038.eop-nam11.prod.protection.outlook.com (2603:10b6:303:b4:cafe::5) by MW4PR03CA0259.outlook.office365.com (2603:10b6:303:b4::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5395.15 via Frontend Transport; Fri, 1 Jul 2022 21:45:07 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.238) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.238 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.238; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (12.22.5.238) by CO1NAM11FT038.mail.protection.outlook.com (10.13.174.231) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5395.14 via Frontend Transport; Fri, 1 Jul 2022 21:45:07 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by DRHQMAIL105.nvidia.com (10.27.9.14) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Fri, 1 Jul 2022 21:45:06 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail204.nvidia.com (10.129.68.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.26; Fri, 1 Jul 2022 14:45:05 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.10) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.986.26 via Frontend Transport; Fri, 1 Jul 2022 14:45:03 -0700 From: Nicolin Chen To: , , , , , , , , , , , , , Subject: [PATCH v5 2/5] vfio/iommu_type1: Prefer to reuse domains vs match enforced cache coherency Date: Fri, 1 Jul 2022 14:44:52 -0700 Message-ID: <20220701214455.14992-3-nicolinc@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220701214455.14992-1-nicolinc@nvidia.com> References: <20220701214455.14992-1-nicolinc@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: daa3572e-c837-4fed-2d9e-08da5baaf73d X-MS-TrafficTypeDiagnostic: PH7PR12MB6000:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ZvIsLnjwuKNCAJ95WimQvww3KF/Tvd3cqLfe1JGdIgnsE8npu5nSAW63GRtAxXswz5nUOy9DgSVMwm5mFv04SAUTxaObv+wlsXBLJNno5qvmEnLhd1MNCHWwIzMBlFNuxkWoAXgTZO055Hq6XvXpM48hoi8ikOZnxjcy/qX58KuT/Id45aoDFXNk+rWdIWJh7Uf3rOnCNHqo/TqmvyIgN55Ke1MQWw6bNmiP6/JdpJ3PJClrdfQ5KeQiMVjWcFi6QMZg+ZAlCobV0neF0g61TR+rmlbiTcjcHI/NvUSm3930JToPhzX4tuw5hk3DRxIXdYKCM7Gdpfj29QLkOdJCadD8VCg/J4QrryhCk5T5EVPk94SNcRAMRVmVUzCzbA/sB65tflhOVOHVZA8Tgns3A4duFOREbjEaxf8wpphRpWt2wgJXTbwb3NY62D40+KX6ShjskCDHi+vb7gX+Y4xIz9OHVPONWdLQNl0sCV8bEZXgtRXY31LnwO0eVP7BgYWGmSOWP9CMWXhfcgpXdqptBv5WVh3Mk1fdyyM5Jze0AaDKEMaephNDL6u33Ox0K4a8rpTDVuiPwcwbR33oyXupguhNt0vRx/as+JFaPoPn0hNc2V02QMj/ePmO/v9oBsxvmagEE12SsmFw7gJe+/3V504QYhhqtMA2nw8J8N6dpqCaVOQ4D0SRLo6y6pkXeF1dvin6vU6jFd8ZHgTCnWDJESAl3eNMCQ34cGoQS9LaZBoO5+m/ABMKIWVod9VZU4t3Ko/1cpXzGy3U+2OXK+YPxBjNbXy17WfTS2Nt+SWAl+raObtbdpMYH5n7q4y1jDBFqn33IvEC24PfsG9yGW+rTKogEwNs5YC2kURPHt4o4PsFHjFKlfSvEHSTjH/nOf9PvyMo/flOYHzWdIEs8tS3mIKML2M/PXCJ3sEDj34iiAw= X-Forefront-Antispam-Report: CIP:12.22.5.238;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:InfoNoRecords;CAT:NONE;SFS:(13230016)(4636009)(376002)(136003)(346002)(39860400002)(396003)(40470700004)(36840700001)(46966006)(426003)(1076003)(186003)(336012)(47076005)(36756003)(82310400005)(2616005)(26005)(83380400001)(7696005)(36860700001)(6666004)(40480700001)(40460700003)(4326008)(54906003)(110136005)(7406005)(7416002)(8936002)(70206006)(5660300002)(2906002)(478600001)(82740400003)(81166007)(356005)(70586007)(316002)(8676002)(41300700001)(86362001)(921005)(83996005)(36900700001)(2101003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Jul 2022 21:45:07.5802 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: daa3572e-c837-4fed-2d9e-08da5baaf73d X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[12.22.5.238];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT038.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB6000 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220701_144512_227977_8A223BED X-CRM114-Status: GOOD ( 14.03 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mjrosato@linux.ibm.com, virtualization@lists.linux-foundation.org, thierry.reding@gmail.com, alyssa@rosenzweig.io, linux-s390@vger.kernel.org, kvm@vger.kernel.org, jonathanh@nvidia.com, iommu@lists.linux.dev, yangyingliang@huawei.com, gerald.schaefer@linux.ibm.com, linux-arm-msm@vger.kernel.org, john.garry@huawei.com, christophe.jaillet@wanadoo.fr, thunder.leizhen@huawei.com, linux-tegra@vger.kernel.org, linux-arm-kernel@lists.infradead.org, chenxiang66@hisilicon.com, cohuck@redhat.com, linux-kernel@vger.kernel.org, iommu@lists.linux-foundation.org, suravee.suthikulpanit@amd.com, dwmw2@infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Jason Gunthorpe The KVM mechanism for controlling wbinvd is based on OR of the coherency property of all devices attached to a guest, no matter whether those devices are attached to a single domain or multiple domains. On the other hand, the benefit to using separate domains was that those devices attached to domains supporting enforced cache coherency always mapped with the attributes necessary to provide that feature, therefore if a non-enforced domain was dropped, the associated group removal would re-trigger an evaluation by KVM. In practice however, the only known cases of such mixed domains included an Intel IGD device behind an IOMMU lacking snoop control, where such devices do not support hotplug, therefore this scenario lacks testing and is not considered sufficiently relevant to support. After all, KVM won't take advantage of trying to push a device that could do enforced cache coherency to a dedicated domain vs re-using an existing domain, which is non-coherent. Simplify this code and eliminate the test. This removes the only logic that needed to have a dummy domain attached prior to searching for a matching domain and simplifies the next patches. It's unclear whether we want to further optimize the Intel driver to update the domain coherency after a device is detached from it, at least not before KVM can be verified to handle such dynamics in related emulation paths (wbinvd, vcpu load, write_cr0, ept, etc.). In reality we don't see an usage requiring such optimization as the only device which imposes such non-coherency is Intel GPU which even doesn't support hotplug/hot remove. Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian Reviewed-by: Lu Baolu Signed-off-by: Nicolin Chen --- drivers/vfio/vfio_iommu_type1.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index c496b7d0b96f..7530f0d727e5 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -2288,9 +2288,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, * testing if they're on the same bus_type. */ list_for_each_entry(d, &iommu->domain_list, next) { - if (d->domain->ops == domain->domain->ops && - d->enforce_cache_coherency == - domain->enforce_cache_coherency) { + if (d->domain->ops == domain->domain->ops) { iommu_detach_group(domain->domain, group->iommu_group); if (!iommu_attach_group(d->domain, group->iommu_group)) { From patchwork Fri Jul 1 21:44:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 12903800 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 50477C433EF for ; Fri, 1 Jul 2022 21:46:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=4mXC+gVJ9yZaykEvpbT18s62um7Ol+QXtEU7XFXcr/Q=; b=lVk8XCfMunjbYm CTFpm1TrUrwcj5Xo9SBP4DXengXj0hjCgwwneEHRKcLSaeg9ogGUjoo4Wxf3h6xNa65ZcgwOpO5gx Njw3lp0zsuHFzORME7aAIwZa5CWB0V3lolGyAwu5PDXkoJ4FGuXOTPoO+3GckoiYx5utSVGc7wxSS Vwta5xV+dSIwcTi9SsLdSCvWrvdxYAp3EAdZ5oQm553Gr6DshodFSe9Zqag9i2jmuzjpNmt5TkQZC qrKXN8g7jqtyjD2ak/rNrwNYwwtne8FuzOb5Qg+9CmGm9HOAqg7rygE7jYXP/Sl6HSRo/G/Ihd6HJ VzJ41bZTbmnw+F0pWiDQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o7OSP-0074Fy-5E; Fri, 01 Jul 2022 21:45:45 +0000 Received: from mail-bn8nam11on2041.outbound.protection.outlook.com ([40.107.236.41] helo=NAM11-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1o7ORw-007404-7U for linux-arm-kernel@lists.infradead.org; Fri, 01 Jul 2022 21:45:19 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SBQqz/pSopS1qzI6QG/1iDKJnfcjN8CfvpFdQGmS6DX8SZHLU5YHSYx6UhJgjHjrzqErM8O0wWZnE6TtdJ7f9OzgeTSC1pfD/meZQuW6zbSFLsJccBsiLX4DSXKk24q3yL6PjmroVVhSYkmoW00QPIV0m3QpbVaWVlaxl6kO0oDeWgNxDkzQSTAc2naBi69IWxtLZFaN9CmV4sdZsoGkL+D4ylpq0HtGhL+lSzNZYcB34InfCC1nJg7ffEt2quRosnUurJ1shZdqc9iNam5CxAXruxGS8x7XghSyRAyYkFFNbwC4C23qGuyF/qVOFhLqh2y8eHAq3Umg1a3gmX3g3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=JPSGFk9Xx7MdNiI8/CiOFYiI5dvrJ58k+niwmyR8Gk8=; b=Dveucd0BPOjb5+0sVHIRPtfKyZ6FGA8ZdGDadWrW/nhwnHCNUKrDdT5z+SLNyC0DuDfVAnrGUWS3iMh4q86aPdUl4sEEhbQTmLlMbYqtbkrGLcq0rIpnvIt1p+schJ0s1BT9e6ozikP+wc19rgdA7nPqZ/bdqoG6yVhPREYwUHxUX1pkLG9nAaP3v4F47HHEYRXn1Oq0N43OAR5AhPaeAMQDL1E+oH0etulD3vydHqC6Hfks8j2zyCWQXIF6G2NAG5QVC2Eko2z3Kt3CtnNivfO4KR4VQAE3Gou69n5yny9CLDW1uIcRE4HiMAuMNipmRHbAY6qH5C5gZztRESu6cw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.234) smtp.rcpttodomain=gmail.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=JPSGFk9Xx7MdNiI8/CiOFYiI5dvrJ58k+niwmyR8Gk8=; b=aHphjtJwbBsB9cYBtz3gqHeT9QwJIrAmgke+XF4ih51NS7c55DlCaut1PiLKMtm10+y6w+InER4y2JcUkw3d7UAhA+Bud6tNxQNGO6OLc/YVI6f8Euvx2RTzV1e+D3p5EVX6h4BEUzJCjNp5D6jT5Syk8ZX6wwtP5rCPhnBM0uw03Dgfm77mEDW9iA5pi9KuOg+9+Z5v3J/Vqxvle8nCCGT55VD4DHpq9JHMC72MxIjvDimpyvxz52O45gNq4jDiR1IWBznk2JtSDnvDTaDg4nFN7HTQGa8UTrogd6Zgt9kxudcgkpLOGhoRgKjJ4rV3YQTpJVzLcEvjE5U1tcSORw== Received: from DM6PR18CA0024.namprd18.prod.outlook.com (2603:10b6:5:15b::37) by CH2PR12MB4149.namprd12.prod.outlook.com (2603:10b6:610:7c::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5395.15; Fri, 1 Jul 2022 21:45:10 +0000 Received: from DM6NAM11FT049.eop-nam11.prod.protection.outlook.com (2603:10b6:5:15b:cafe::a) by DM6PR18CA0024.outlook.office365.com (2603:10b6:5:15b::37) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5395.17 via Frontend Transport; Fri, 1 Jul 2022 21:45:10 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.234) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.234 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.234; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (12.22.5.234) by DM6NAM11FT049.mail.protection.outlook.com (10.13.172.188) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5395.14 via Frontend Transport; Fri, 1 Jul 2022 21:45:09 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by DRHQMAIL101.nvidia.com (10.27.9.10) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Fri, 1 Jul 2022 21:45:09 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail204.nvidia.com (10.129.68.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.26; Fri, 1 Jul 2022 14:45:08 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.10) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.986.26 via Frontend Transport; Fri, 1 Jul 2022 14:45:06 -0700 From: Nicolin Chen To: , , , , , , , , , , , , , Subject: [PATCH v5 3/5] vfio/iommu_type1: Remove the domain->ops comparison Date: Fri, 1 Jul 2022 14:44:53 -0700 Message-ID: <20220701214455.14992-4-nicolinc@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220701214455.14992-1-nicolinc@nvidia.com> References: <20220701214455.14992-1-nicolinc@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 93708aec-57cd-4372-09f8-08da5baaf899 X-MS-TrafficTypeDiagnostic: CH2PR12MB4149:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: MbIWIBAl/l18QMJLzy4TV1FhJRm8Y4YIEePb27DcYTO6qDC5Yp3yyUYBZEttLpV8A0quJtgBAO1mLqeXQsuna3vAp7FYQwLGt3TXGzYj5WnHWNiFJFo9z8jLhRdE0DR0z+WIdQCwDiQkdWIgBXhViDSTmMS+nAnpPHytn9bvhxYbhZmQWyyDrwEKaqri1PTLpUQXoOdXeZgrEPX+qAGTA01PNx4bFxawW6aOGUvJwkjLGNak0xopLZSIhP4VusPFn8jTUqh0BOXfj7wEkLqTopK8FWXsyeiLATRwvfN8JUcMnuFIPgkRRWo6EJ/vGkn8BApauuZH9m+52TbqCXm0hcJxBNiMI23ACg+luFA1Z64oVHZDHSgKNSoF8TLPnqsTDyilsooqMCg4HYONF73L9sEu9LnjB1aeE6XU65/eNA7/THsTppWj5A8OMcN1RbaTtJvG8/rddDubY/yoIccvpfR4jrCc7jKmQZ94oHEd3hykaaUg5DqfKIKN0ZQZlH4dOi27PO28xr/eiJqQBIQCBgiF6PDnxGkxTV59ThmFv4XyQvAFSyDV59eUweH1ot8INWn3Khf5AoFf3ipbOJeD+IlCzl0PKGZH3HnwoiI0Qbt+XXI+YVpSfEGFinGOlpQQIyafDx7ADyTSJ0D6Xfq+0Bf+YJpc+J7Z7eH0KOczJVfUluFOPEgcP5bqBchUnWk8iA8PxwKJAn1BokGT3irLapQMsF5UEnK6MBq9qiCZzxorhtntSKNJLTb+T+1DTS6smB0voTwwim+ifwTEalNh1M3lz2gDaC7FQBKLycXqQHcojfEeHRdsv83WFa9ol0SYjnXU4QEaoBgaW9FTHY/h+jIHPvTS/r4PMKbGKQLFG5TnZhrg553ChIFt5Ur65WBlmkHioh6vEZlCbSbsQFOen/ASMCpsaWJE5YxeKP8nJW7kVLWx2zaix4/ZPGG7UfHxGc9zxs7d+WysFRM33tGNqIb1KGBonDABsH1MGs0LuRZakq2vnVfcaWEPyomFURAcc/Y8KBxj0cir24HkdgaSrw== X-Forefront-Antispam-Report: CIP:12.22.5.234;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:InfoNoRecords;CAT:NONE;SFS:(13230016)(4636009)(346002)(396003)(136003)(39860400002)(376002)(46966006)(40470700004)(36840700001)(186003)(2616005)(1076003)(83380400001)(2906002)(81166007)(336012)(426003)(86362001)(82740400003)(356005)(36860700001)(921005)(40460700003)(8936002)(47076005)(8676002)(4326008)(7416002)(966005)(5660300002)(82310400005)(40480700001)(70206006)(70586007)(110136005)(316002)(54906003)(7696005)(7406005)(36756003)(478600001)(26005)(41300700001)(6666004)(36900700001)(83996005)(2101003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Jul 2022 21:45:09.8443 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 93708aec-57cd-4372-09f8-08da5baaf899 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[12.22.5.234];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT049.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4149 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220701_144516_345147_335AFD65 X-CRM114-Status: GOOD ( 13.22 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mjrosato@linux.ibm.com, virtualization@lists.linux-foundation.org, thierry.reding@gmail.com, alyssa@rosenzweig.io, linux-s390@vger.kernel.org, kvm@vger.kernel.org, jonathanh@nvidia.com, iommu@lists.linux.dev, yangyingliang@huawei.com, gerald.schaefer@linux.ibm.com, linux-arm-msm@vger.kernel.org, john.garry@huawei.com, christophe.jaillet@wanadoo.fr, thunder.leizhen@huawei.com, linux-tegra@vger.kernel.org, linux-arm-kernel@lists.infradead.org, chenxiang66@hisilicon.com, cohuck@redhat.com, linux-kernel@vger.kernel.org, iommu@lists.linux-foundation.org, suravee.suthikulpanit@amd.com, dwmw2@infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The domain->ops validation was added, as a precaution, for mixed-driver systems. Per Robin's remarks, * While bus_set_iommu() still exists, the core code prevents multiple drivers from registering, so we can't really run into a situation of having a mixed-driver system: https://lore.kernel.org/kvm/6e1280c5-4b22-ebb3-3912-6c72bc169982@arm.com/ * And there's plenty more significant problems than this to fix; in future when many can be permitted, we will rely on the IOMMU core code to check the domain->ops: https://lore.kernel.org/kvm/6575de6d-94ba-c427-5b1e-967750ddff23@arm.com/ So remove the check in VFIO for simplicity. Reviewed-by: Kevin Tian Signed-off-by: Nicolin Chen Reviewed-by: Jason Gunthorpe --- drivers/vfio/vfio_iommu_type1.c | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 7530f0d727e5..5992ee2345a0 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -2280,29 +2280,19 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, domain->domain->ops->enforce_cache_coherency( domain->domain); - /* - * Try to match an existing compatible domain. We don't want to - * preclude an IOMMU driver supporting multiple bus_types and being - * able to include different bus_types in the same IOMMU domain, so - * we test whether the domains use the same iommu_ops rather than - * testing if they're on the same bus_type. - */ + /* Try to match an existing compatible domain */ list_for_each_entry(d, &iommu->domain_list, next) { - if (d->domain->ops == domain->domain->ops) { - iommu_detach_group(domain->domain, group->iommu_group); - if (!iommu_attach_group(d->domain, - group->iommu_group)) { - list_add(&group->next, &d->group_list); - iommu_domain_free(domain->domain); - kfree(domain); - goto done; - } - - ret = iommu_attach_group(domain->domain, - group->iommu_group); - if (ret) - goto out_domain; + iommu_detach_group(domain->domain, group->iommu_group); + if (!iommu_attach_group(d->domain, group->iommu_group)) { + list_add(&group->next, &d->group_list); + iommu_domain_free(domain->domain); + kfree(domain); + goto done; } + + ret = iommu_attach_group(domain->domain, group->iommu_group); + if (ret) + goto out_domain; } vfio_test_domain_fgsp(domain); From patchwork Fri Jul 1 21:44:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 12903801 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0C169C433EF for ; Fri, 1 Jul 2022 21:47:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=pp9eAgk8K4ryYswJ9AcQpEXlu+qqwNZNPXD9TVMa41M=; b=ARlZTX6hKpCIaD wDO8x4yZdawzivzo28BEwLO68ow7qiWTy9EOCzpBNMUipIT+tLM2FoJa5FM9fpy1+S4dH5Bm8l428 9QI68YxTIRY9VLlumMlSAYIHF9sFTE/0l8dGwgesAX6YBs67cLt5O6Gv1qQ/BX2MMe6UDnv7MXm7j t+wx+FGeMrY8yR7F+Ihg1mbZK+iMCeIcZycMEZBlow2xrnIx2VnbyjVxZotsZtpH5UEtkQGuP2SPO DQMYdW4ykNNiZYiNJphnEV+DTYxLApyLM+6OMsA5VwMNMsMj5fHbQvALiQ1v6e34EpNK82VDC8OK+ oSwbb/1u9gBH5Eh2UlRQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o7OSe-0074Kn-SS; Fri, 01 Jul 2022 21:46:01 +0000 Received: from mail-mw2nam10on2074.outbound.protection.outlook.com ([40.107.94.74] helo=NAM10-MW2-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1o7ORy-00742X-3q for linux-arm-kernel@lists.infradead.org; Fri, 01 Jul 2022 21:45:19 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PyyQHVqeuvwGmTS9N3ut1d64rf5YSi2JnM8gQcRgMaQQP5wDfS27FxciCptX7Ci8neOn5KgL8swiHo/pdNWbhd7c2O9Wjto1ZO2t/goUEzFfmYjyqRslTV7iRSWyijhyfNPOGx6axKfN3zcXFynLS2ub4qAqQkc66JvDdD77U7frIvp8FOd3HorWINIOaK3SPsFRIrELhhUP+wIbCTvuTwVCrUHsRdZCH9IGupm1NnUJe4ZtwWCE5fuNDmNqjGa78l6DmGo5O5ovW8W28B5fU6v8g1bXC3hL3jHLzm1bFwFcm4MiWczxfWxKdZSezNZ7aWjAr7mg12Z0vK5PEJNJ+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=SRNov0IlTB5uPZuj+7TWOzHm5AivjpTU5veR0T+7cO4=; b=ijr6AmhobixWOrfGpGPtRJXP3aTTrvbyLpy2Hd2+aGjo09vt6hi5JLdnabRiVT1tKx2SmKAI9E2TXG6arQl3GC8hv2yvBtzQvSzM/IY6YJaruvWiZTDM6rGGV33hKdXKDlTCB4sZi26zJ/LX1J4odRvnEI1Iykj1hhckhiT+AtdRK7iElpkz0wVhzaKixURVSVjeBiYq13bcm1bCGETPoKit31O8jVL5BKnJOdCbFZYoGHOWipWyx7cvPDs8yShTU5jNDaBAwj/okd3BpznWw3ubPR1j6ztH2RglgmN8RdX2mC2W2fZEv+OM2jGUTqSpaOW4Kgy2g3nu4Uhgk8zKvw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.236) smtp.rcpttodomain=gmail.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=SRNov0IlTB5uPZuj+7TWOzHm5AivjpTU5veR0T+7cO4=; b=eLS6CMt+z7Ay8AQj8YCn/2rAuie9oXtzDR6DjPYjLnX3N1HGP4W/6/SlctGaEFjA21GsYlrklpdePrHSuUXVyROwuI89n7i6iSeFD+AEoEjL+i2bdgEiG4FfBacEYyJ4ad4wi3VqW6L9g4LE6o5Fz3U3A0yAt5dCAJlvHJ8e8vj25KkoU2CEU3Qf7LVqgBB8amUNc5LRtYXdUsJae+OWO5l6JIH1hqZy+XI3E++CMKunfrnC5xPMQUYIuwugNag0QSmDyGACE1Jmz1t78qnCpTVGHvEsnNZfof8tW//O+uTSmkHpXZCKCmRwHgwp9kxhkFOkDotRBz09TL7aZG0xsg== Received: from BN9PR03CA0954.namprd03.prod.outlook.com (2603:10b6:408:108::29) by BYAPR12MB3048.namprd12.prod.outlook.com (2603:10b6:a03:ad::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5395.14; Fri, 1 Jul 2022 21:45:13 +0000 Received: from BN8NAM11FT057.eop-nam11.prod.protection.outlook.com (2603:10b6:408:108:cafe::8f) by BN9PR03CA0954.outlook.office365.com (2603:10b6:408:108::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5395.15 via Frontend Transport; Fri, 1 Jul 2022 21:45:12 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.236) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.236 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.236; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (12.22.5.236) by BN8NAM11FT057.mail.protection.outlook.com (10.13.177.49) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5395.14 via Frontend Transport; Fri, 1 Jul 2022 21:45:12 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by DRHQMAIL109.nvidia.com (10.27.9.19) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Fri, 1 Jul 2022 21:45:11 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail204.nvidia.com (10.129.68.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.26; Fri, 1 Jul 2022 14:45:11 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.10) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.986.26 via Frontend Transport; Fri, 1 Jul 2022 14:45:08 -0700 From: Nicolin Chen To: , , , , , , , , , , , , , Subject: [PATCH v5 4/5] vfio/iommu_type1: Clean up update_dirty_scope in detach_group() Date: Fri, 1 Jul 2022 14:44:54 -0700 Message-ID: <20220701214455.14992-5-nicolinc@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220701214455.14992-1-nicolinc@nvidia.com> References: <20220701214455.14992-1-nicolinc@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: cfb1d1df-fa9a-4f6a-dc9a-08da5baafa39 X-MS-TrafficTypeDiagnostic: BYAPR12MB3048:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: cZk8p+X/kzc9EseWs780VkXxf7c/OkQ8T8gYzb+IbxZqVJdfHDZ5accyM9CYeNmvZ7ksUWyeO3bURx25b7tgZMprJpMGVdmqJ+LgY75TdpItu6UYwpwrDSTcjqSrVAgVTI9ayCgU+G/d11hX9wVh7CUHl3A2zCNp1uEtfYkFoj4vJh/rHtLMnlykQ0DKuKYBqfLx6l3bnOIZSQiP89jhQ36K7kTF33igyWFTVT3VGvE4cnzyVrz2JqELNYJBa6cuFKnQl3Amce5NRzI3MwGorJ+sVaOhnvxao1hlJYJsMjIopY6dcCC5HOHlD9aJjZuX1sGPToMOM9JrkZsRXINSy8Ea+wE9L/TGlaJcSJ2O0+lADTb5BlpJbduc4m0QBtbVgoTh5MZxZ2uR5f4ADU2HUDFyS9zFjfBoG+VdelzmoVVU0eubC59VcrdMP6/ZbGzedKwwxT6WepvUr1j4EvlL+xznPUVY3j6EcEwV5L+TtPy/rfsxPAiSzIP87wt1YZ1tG/q/U/95Y0Wv5WwIMN/fTCUMX+jDJjV9izcMO4YuVqUoYBI6dmsCHJgWMX18820a9owEz767+Nj3ljYUQvfM6vGLunpQ4WyqPk9+gk2vGNnE6Wk8G78TaAGQA2YFYOZqL7prDZKDg4rR8J3RQunJDlMGPS4rJ8MaQoejjznhgoxDJNQk+VhBJO3cGKZDv6H6kOr1DEocRs14w/JPoK+gS7zKNrlinouLv18r+LddAeMy4RaAm+gZM17+Nhtx6Jc6BGbUWatyRCb114iHhI7GWHvu3n4SyF+3rl9P6hzsq3CCRPCXmyuJXbBr4BqtLZQ/B0hgWL8/lcIHgE3nBQXQE+BGZxZI3IHMei/CSZmMEfhOfIc1bODRGLyKHDhPGl3oHSUejGueq6o0/nJa9/s7tea3a/pHNHHYi+i+TRq7C539gs3j9vyuP6+J16g6ee0b X-Forefront-Antispam-Report: CIP:12.22.5.236;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:InfoNoRecords;CAT:NONE;SFS:(13230016)(4636009)(346002)(396003)(136003)(39860400002)(376002)(36840700001)(46966006)(40470700004)(336012)(426003)(47076005)(110136005)(81166007)(316002)(2616005)(41300700001)(82310400005)(1076003)(82740400003)(36860700001)(921005)(54906003)(6666004)(186003)(7696005)(356005)(15650500001)(8676002)(4326008)(478600001)(26005)(2906002)(83380400001)(40480700001)(36756003)(40460700003)(7406005)(70586007)(7416002)(8936002)(70206006)(86362001)(5660300002)(14143004)(83996005)(2101003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Jul 2022 21:45:12.5291 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: cfb1d1df-fa9a-4f6a-dc9a-08da5baafa39 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[12.22.5.236];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT057.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR12MB3048 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220701_144518_245130_A2AE7ACE X-CRM114-Status: GOOD ( 13.44 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mjrosato@linux.ibm.com, virtualization@lists.linux-foundation.org, thierry.reding@gmail.com, alyssa@rosenzweig.io, linux-s390@vger.kernel.org, kvm@vger.kernel.org, jonathanh@nvidia.com, iommu@lists.linux.dev, yangyingliang@huawei.com, gerald.schaefer@linux.ibm.com, linux-arm-msm@vger.kernel.org, john.garry@huawei.com, christophe.jaillet@wanadoo.fr, thunder.leizhen@huawei.com, linux-tegra@vger.kernel.org, linux-arm-kernel@lists.infradead.org, chenxiang66@hisilicon.com, cohuck@redhat.com, linux-kernel@vger.kernel.org, iommu@lists.linux-foundation.org, suravee.suthikulpanit@amd.com, dwmw2@infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org All devices in emulated_iommu_groups have pinned_page_dirty_scope set, so the update_dirty_scope in the first list_for_each_entry is always false. Clean it up, and move the "if update_dirty_scope" part from the detach_group_done routine to the domain_list part. Suggested-by: Jason Gunthorpe Reviewed-by: Kevin Tian Signed-off-by: Nicolin Chen Reviewed-by: Jason Gunthorpe --- drivers/vfio/vfio_iommu_type1.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 5992ee2345a0..5624bbf02ab7 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -2456,14 +2456,12 @@ static void vfio_iommu_type1_detach_group(void *iommu_data, struct vfio_iommu *iommu = iommu_data; struct vfio_domain *domain; struct vfio_iommu_group *group; - bool update_dirty_scope = false; LIST_HEAD(iova_copy); mutex_lock(&iommu->lock); list_for_each_entry(group, &iommu->emulated_iommu_groups, next) { if (group->iommu_group != iommu_group) continue; - update_dirty_scope = !group->pinned_page_dirty_scope; list_del(&group->next); kfree(group); @@ -2472,7 +2470,8 @@ static void vfio_iommu_type1_detach_group(void *iommu_data, WARN_ON(iommu->notifier.head); vfio_iommu_unmap_unpin_all(iommu); } - goto detach_group_done; + mutex_unlock(&iommu->lock); + return; } /* @@ -2488,9 +2487,7 @@ static void vfio_iommu_type1_detach_group(void *iommu_data, continue; iommu_detach_group(domain->domain, group->iommu_group); - update_dirty_scope = !group->pinned_page_dirty_scope; list_del(&group->next); - kfree(group); /* * Group ownership provides privilege, if the group list is * empty, the domain goes away. If it's the last domain with @@ -2513,6 +2510,16 @@ static void vfio_iommu_type1_detach_group(void *iommu_data, vfio_iommu_aper_expand(iommu, &iova_copy); vfio_update_pgsize_bitmap(iommu); } + /* + * Removal of a group without dirty tracking may allow + * the iommu scope to be promoted. + */ + if (!group->pinned_page_dirty_scope) { + iommu->num_non_pinned_groups--; + if (iommu->dirty_page_tracking) + vfio_iommu_populate_bitmap_full(iommu); + } + kfree(group); break; } @@ -2521,16 +2528,6 @@ static void vfio_iommu_type1_detach_group(void *iommu_data, else vfio_iommu_iova_free(&iova_copy); -detach_group_done: - /* - * Removal of a group without dirty tracking may allow the iommu scope - * to be promoted. - */ - if (update_dirty_scope) { - iommu->num_non_pinned_groups--; - if (iommu->dirty_page_tracking) - vfio_iommu_populate_bitmap_full(iommu); - } mutex_unlock(&iommu->lock); } From patchwork Fri Jul 1 21:44:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 12903802 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C66CBC433EF for ; Fri, 1 Jul 2022 21:47:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=6thgFOqHdnzbj3rxCrf4U/yHXDqiSkjToSLluVOoc/o=; b=uSFjyAIZp5aWrP MbPDUb8Y7jxwxBA4Q11Xbu12JpZn26zzpmGc6RQtMTe/U79vzGyQEJ9hXk8vw9GvgwdBXYKC4WNGD xb/F1q07Dex/BaJa1f0skleJzbOpGM2twBzusZwZ+fP35Jhd864VxNntH2tJAvbA7+tFc8n8h0CZT ZMHxEwLJVsGwvLLLJdO9IjDaqwcyS6F09XzdcZt9muVGs8cSI2a0YHW72wvYHqa4vtH53N/UlsSOT t6OHoOa2aVErqngsrxzzIEgceGi978UuE5t9KIxowJA43bo0eYCQvaa5FvMUy5Q80CDbg0dqsqR9K iAlFtOLpVeAkSy/24TJg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1o7OT8-0074aw-SX; Fri, 01 Jul 2022 21:46:31 +0000 Received: from mail-co1nam11on2046.outbound.protection.outlook.com ([40.107.220.46] helo=NAM11-CO1-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1o7OS2-00745U-1e for linux-arm-kernel@lists.infradead.org; Fri, 01 Jul 2022 21:45:25 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gHL4MjaJvC38kBCgetnO9ULWNOlE0yISS3RT/hST3RcuaDU5YPQ+q0hJGWQCd0behlYgfvCq8+hzrN82qz+mD35Tz06HoEkMwH/Mdlw25ZoeOy/cXaaCebAJUmkDUOnjJLdHywgApOl26gbyogpu0kez79sy6dz3ZMwZHXWNDYqHkM+nviDVBbn0hLBHhtvI04xRWDoGP5pF3ozlhOVjieTR8EmH0Y2aSECFSLb11WDd6fW4vUBGCgwitnwi/u144eKP0lZNbo24szbjG9faPn7WY8mvttba3kI5hxnyQrYV2AKk/FGQXfNAorgdnwGEdLlHfT62riR5rpyMS3tDCA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=CwdZHXjoRblbmukEmi+AT7FP9piX2Pi+haS19LpVbXk=; b=SC59yJ3MDxoRJCyCdHT7kgGUtZm1LQNpOyfafK4VBKEUrYZBvuXo901Aa/oGVf6ndwdPyJNbGTXptKK0NQ/vKkWHPIbtyGsX0u7jCZL7j6K4sVa0Fp/5fYD+J8AaiLoqDvq1ZzEBbxutydNlBDg/vqD13j55WFufWFAN1Y3Vkba4ei58oCcpO+VNgyrp6UjOa6/cRIYPM1DGGL9TVrUwzxzZhcE0s6PeS+C3gpjnDG/PtYvyMGAGAHHLYYSlYy8XDgqbvju1Xf1qZ7lFjkKCK7cyxFB0PwUBL+gqf4d36xvHGykaYKcFoJ/Pffn8VEYwSvPN7Y6vEc+cxNnIcOdxMg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.235) smtp.rcpttodomain=gmail.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=CwdZHXjoRblbmukEmi+AT7FP9piX2Pi+haS19LpVbXk=; b=DJ89L6C2sdeU4/hGEF9pN3Y5JJXT1SRVT2GVg/Z6dFGjCmupHZZlbswRmWyCyizDQa0jY8o6FAx1cqIeNobXL71IdPPCb+7EGcgkAelhXwxGfwRw6T6fr06VolfnZXd92b7CWLNWDe7I/G70jcDS0Sk5WI4E0PJf5ZFKkMepQ9grR3nDsdaSd6S2xN5tyH4HgCf08zRLbws6aCsTzOofx9Uv4AUUq3wvBpqkN4Ajy5Q3eVU4ZwrN24jYQYtFyqAv1TuUFtFBdyL/Q4XUpsRwrADATr8xj45l6fX/R5R21V7mqrn8/C/2MyCXYwMj/cKU1VCEfGSjKnLVl2rSyjzopg== Received: from DM6PR03CA0033.namprd03.prod.outlook.com (2603:10b6:5:40::46) by BYAPR12MB3480.namprd12.prod.outlook.com (2603:10b6:a03:aa::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5373.18; Fri, 1 Jul 2022 21:45:15 +0000 Received: from DM6NAM11FT063.eop-nam11.prod.protection.outlook.com (2603:10b6:5:40:cafe::5b) by DM6PR03CA0033.outlook.office365.com (2603:10b6:5:40::46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5395.15 via Frontend Transport; Fri, 1 Jul 2022 21:45:15 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.235) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.235 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.235; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (12.22.5.235) by DM6NAM11FT063.mail.protection.outlook.com (10.13.172.219) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5395.17 via Frontend Transport; Fri, 1 Jul 2022 21:45:15 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by DRHQMAIL107.nvidia.com (10.27.9.16) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Fri, 1 Jul 2022 21:45:14 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail204.nvidia.com (10.129.68.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.26; Fri, 1 Jul 2022 14:45:13 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.10) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.986.26 via Frontend Transport; Fri, 1 Jul 2022 14:45:11 -0700 From: Nicolin Chen To: , , , , , , , , , , , , , Subject: [PATCH v5 5/5] vfio/iommu_type1: Simplify group attachment Date: Fri, 1 Jul 2022 14:44:55 -0700 Message-ID: <20220701214455.14992-6-nicolinc@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220701214455.14992-1-nicolinc@nvidia.com> References: <20220701214455.14992-1-nicolinc@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 7860a5b4-7207-4cf2-c5d2-08da5baafbba X-MS-TrafficTypeDiagnostic: BYAPR12MB3480:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ebABnAaW+oJCCgqUNTIgLdRlqkEdyZMtdqOGBV9z46hvjJhruL0H2U0HKwYgXDoZLO0HB5nDtKENvX8JbcUsDSuW6wE9QeX0thg0qCTL1sxhMt+xoePq8xJhS89E7fQPbifEl7ERindzY4d8uK4sbxhNvMPiSXCqeZasQ7ETn1a5cIQgxt0YMvHX564AyYU+Co8vMMp74ihQRM7Ki3vWGuJF2ZY0/bGmnj0ngHf7xU67o8XMwSIM9mtlqA99xRF3169OJC4gdsjacMPoidj+wxm+7lGqdZuaA5yEeT7/LLjwiyXAvZgNPiHw0LjiVv2dGDmji+VnwmLKRUDqPmSkRfP+N7VU9CgMbfVyHfHqUtdxeqG0BDCNNLDsf+r46HHPd3tGMIz2PD+MH2eyx5mUiyacpcseAMzdgZtIosA5O0vzO+yH7bQ3Gl1IxYfiCwGm9GxUjcb/CNo0KmhQsA+mDteBaXjTqBFIhZgOtm+lH8ix8rkUf7guzv4iKSDuaVsRSROT3gjgkjCD/tEFaC8JT40R5VlDnL6ywymOKW9AyRH4zcb56XDmteJVAgA6MI2FHi9w4NcnfB32LYx3dfOK3VgMdWe+h0BHKgRP2NpAw9vAtxtNpBnrr0MEJ2sMFZX8b6O40wIpNkARf7x5Bkh8A7zYtvP9fQ0Faz5xUArHiAeic+aAaSZeFW4D+OHGQ7r9V1RJAx8myad22kDh6ONF8qlvJyv4o1BNwnNW23EQ72JMDvn6WFOouXCfU6tdISqXJmuCRW7Nc57lznAR7Fkv5mofgUEeaAX3kkfT38GAB+n2YGRzgc7WafmEkKAgyazXNNyQHaUlmEhLk7xGC0uG+Igp+dX9cdzeUuksqICu/wumz1COXEWdhgHTJW8rqlOwZDEFbF3mzcBjptnbQNIb5TwdqZ8pfT3b5UHJnYdXTdo= X-Forefront-Antispam-Report: CIP:12.22.5.235;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:InfoNoRecords;CAT:NONE;SFS:(13230016)(4636009)(346002)(376002)(136003)(39860400002)(396003)(46966006)(36840700001)(40470700004)(7416002)(81166007)(36756003)(6666004)(2906002)(356005)(40480700001)(8676002)(186003)(70586007)(54906003)(7406005)(8936002)(7696005)(70206006)(5660300002)(41300700001)(921005)(86362001)(30864003)(40460700003)(82740400003)(82310400005)(110136005)(426003)(4326008)(2616005)(83380400001)(47076005)(336012)(36860700001)(316002)(1076003)(26005)(478600001)(36900700001)(2101003)(83996005);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Jul 2022 21:45:15.0517 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 7860a5b4-7207-4cf2-c5d2-08da5baafbba X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[12.22.5.235];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT063.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR12MB3480 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220701_144522_147945_904F0317 X-CRM114-Status: GOOD ( 22.55 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mjrosato@linux.ibm.com, virtualization@lists.linux-foundation.org, thierry.reding@gmail.com, alyssa@rosenzweig.io, linux-s390@vger.kernel.org, kvm@vger.kernel.org, jonathanh@nvidia.com, iommu@lists.linux.dev, yangyingliang@huawei.com, gerald.schaefer@linux.ibm.com, linux-arm-msm@vger.kernel.org, john.garry@huawei.com, christophe.jaillet@wanadoo.fr, thunder.leizhen@huawei.com, linux-tegra@vger.kernel.org, linux-arm-kernel@lists.infradead.org, chenxiang66@hisilicon.com, cohuck@redhat.com, linux-kernel@vger.kernel.org, iommu@lists.linux-foundation.org, suravee.suthikulpanit@amd.com, dwmw2@infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Un-inline the domain specific logic from the attach/detach_group ops into two paired functions vfio_iommu_alloc_attach_domain() and vfio_iommu_detach_destroy_domain() that strictly deal with creating and destroying struct vfio_domains. Add the logic to check for EMEDIUMTYPE return code of iommu_attach_group() and avoid the extra domain allocations and attach/detach sequences of the old code. This allows properly detecting an actual attach error, like -ENOMEM, vs treating all attach errors as an incompatible domain. Reviewed-by: Kevin Tian Co-developed-by: Jason Gunthorpe Signed-off-by: Jason Gunthorpe Signed-off-by: Nicolin Chen --- drivers/vfio/vfio_iommu_type1.c | 333 +++++++++++++++++--------------- 1 file changed, 180 insertions(+), 153 deletions(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 5624bbf02ab7..d3a4cedcd082 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -2155,14 +2155,179 @@ static int vfio_iommu_domain_alloc(struct device *dev, void *data) return 1; /* Don't iterate */ } +static struct vfio_domain * +vfio_iommu_alloc_attach_domain(struct vfio_iommu *iommu, + struct vfio_iommu_group *group, + struct list_head *group_resv_regions) +{ + struct iommu_domain *new_domain; + struct vfio_domain *domain; + phys_addr_t resv_msi_base; + int ret = 0; + + /* Try to match an existing compatible domain */ + list_for_each_entry (domain, &iommu->domain_list, next) { + ret = iommu_attach_group(domain->domain, group->iommu_group); + /* -EMEDIUMTYPE means an incompatible domain, so try next one */ + if (ret == -EMEDIUMTYPE) + continue; + if (ret) + return ERR_PTR(ret); + goto done; + } + + /* + * Going via the iommu_group iterator avoids races, and trivially gives + * us a representative device for the IOMMU API call. We don't actually + * want to iterate beyond the first device (if any). + */ + iommu_group_for_each_dev(group->iommu_group, &new_domain, + vfio_iommu_domain_alloc); + if (!new_domain) + return ERR_PTR(-EIO); + + if (iommu->nesting) { + ret = iommu_enable_nesting(new_domain); + if (ret) + goto out_free_iommu_domain; + } + + ret = iommu_attach_group(new_domain, group->iommu_group); + if (ret) + goto out_free_iommu_domain; + + domain = kzalloc(sizeof(*domain), GFP_KERNEL); + if (!domain) { + ret = -ENOMEM; + goto out_detach; + } + + domain->domain = new_domain; + vfio_test_domain_fgsp(domain); + + /* + * If the IOMMU can block non-coherent operations (ie PCIe TLPs with + * no-snoop set) then VFIO always turns this feature on because on Intel + * platforms it optimizes KVM to disable wbinvd emulation. + */ + if (new_domain->ops->enforce_cache_coherency) + domain->enforce_cache_coherency = + new_domain->ops->enforce_cache_coherency(new_domain); + + /* replay mappings on new domains */ + ret = vfio_iommu_replay(iommu, domain); + if (ret) + goto out_free_domain; + + if (vfio_iommu_has_sw_msi(group_resv_regions, &resv_msi_base)) { + ret = iommu_get_msi_cookie(domain->domain, resv_msi_base); + if (ret && ret != -ENODEV) + goto out_free_domain; + } + + INIT_LIST_HEAD(&domain->group_list); + list_add(&domain->next, &iommu->domain_list); + vfio_update_pgsize_bitmap(iommu); + +done: + list_add(&group->next, &domain->group_list); + + /* + * An iommu backed group can dirty memory directly and therefore + * demotes the iommu scope until it declares itself dirty tracking + * capable via the page pinning interface. + */ + iommu->num_non_pinned_groups++; + + return domain; + +out_free_domain: + kfree(domain); +out_detach: + iommu_detach_group(new_domain, group->iommu_group); +out_free_iommu_domain: + iommu_domain_free(new_domain); + return ERR_PTR(ret); +} + +static void vfio_iommu_unmap_unpin_all(struct vfio_iommu *iommu) +{ + struct rb_node *node; + + while ((node = rb_first(&iommu->dma_list))) + vfio_remove_dma(iommu, rb_entry(node, struct vfio_dma, node)); +} + +static void vfio_iommu_unmap_unpin_reaccount(struct vfio_iommu *iommu) +{ + struct rb_node *n, *p; + + n = rb_first(&iommu->dma_list); + for (; n; n = rb_next(n)) { + struct vfio_dma *dma; + long locked = 0, unlocked = 0; + + dma = rb_entry(n, struct vfio_dma, node); + unlocked += vfio_unmap_unpin(iommu, dma, false); + p = rb_first(&dma->pfn_list); + for (; p; p = rb_next(p)) { + struct vfio_pfn *vpfn = rb_entry(p, struct vfio_pfn, + node); + + if (!is_invalid_reserved_pfn(vpfn->pfn)) + locked++; + } + vfio_lock_acct(dma, locked - unlocked, true); + } +} + +static void vfio_iommu_detach_destroy_domain(struct vfio_domain *domain, + struct vfio_iommu *iommu, + struct vfio_iommu_group *group) +{ + iommu_detach_group(domain->domain, group->iommu_group); + list_del(&group->next); + if (!list_empty(&domain->group_list)) + goto out_dirty; + + /* + * Group ownership provides privilege, if the group list is empty, the + * domain goes away. If it's the last domain with iommu and external + * domain doesn't exist, then all the mappings go away too. If it's the + * last domain with iommu and external domain exist, update accounting + */ + if (list_is_singular(&iommu->domain_list)) { + if (list_empty(&iommu->emulated_iommu_groups)) { + WARN_ON(iommu->notifier.head); + vfio_iommu_unmap_unpin_all(iommu); + } else { + vfio_iommu_unmap_unpin_reaccount(iommu); + } + } + iommu_domain_free(domain->domain); + list_del(&domain->next); + kfree(domain); + vfio_update_pgsize_bitmap(iommu); + +out_dirty: + /* + * Removal of a group without dirty tracking may allow the iommu scope + * to be promoted. + */ + if (!group->pinned_page_dirty_scope) { + iommu->num_non_pinned_groups--; + if (iommu->dirty_page_tracking) + vfio_iommu_populate_bitmap_full(iommu); + } +} + static int vfio_iommu_type1_attach_group(void *iommu_data, struct iommu_group *iommu_group, enum vfio_group_type type) { struct vfio_iommu *iommu = iommu_data; struct vfio_iommu_group *group; - struct vfio_domain *domain, *d; - bool resv_msi, msi_remap; - phys_addr_t resv_msi_base = 0; + struct vfio_domain *domain; + bool msi_remap; struct iommu_domain_geometry *geo; LIST_HEAD(iova_copy); LIST_HEAD(group_resv_regions); @@ -2193,32 +2358,17 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, goto out_unlock; } - ret = -ENOMEM; - domain = kzalloc(sizeof(*domain), GFP_KERNEL); - if (!domain) + ret = iommu_get_group_resv_regions(iommu_group, &group_resv_regions); + if (ret) goto out_free_group; - /* - * Going via the iommu_group iterator avoids races, and trivially gives - * us a representative device for the IOMMU API call. We don't actually - * want to iterate beyond the first device (if any). - */ - ret = -EIO; - iommu_group_for_each_dev(iommu_group, &domain->domain, - vfio_iommu_domain_alloc); - if (!domain->domain) - goto out_free_domain; - - if (iommu->nesting) { - ret = iommu_enable_nesting(domain->domain); - if (ret) - goto out_domain; + domain = vfio_iommu_alloc_attach_domain(iommu, group, + &group_resv_regions); + if (IS_ERR(domain)) { + ret = PTR_ERR(domain); + goto out_free_group; } - ret = iommu_attach_group(domain->domain, group->iommu_group); - if (ret) - goto out_domain; - /* Get aperture info */ geo = &domain->domain->geometry; if (vfio_iommu_aper_conflict(iommu, geo->aperture_start, @@ -2227,10 +2377,6 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, goto out_detach; } - ret = iommu_get_group_resv_regions(iommu_group, &group_resv_regions); - if (ret) - goto out_detach; - if (vfio_iommu_resv_conflict(iommu, &group_resv_regions)) { ret = -EINVAL; goto out_detach; @@ -2254,11 +2400,6 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, if (ret) goto out_detach; - resv_msi = vfio_iommu_has_sw_msi(&group_resv_regions, &resv_msi_base); - - INIT_LIST_HEAD(&domain->group_list); - list_add(&group->next, &domain->group_list); - msi_remap = irq_domain_check_msi_remap() || iommu_group_for_each_dev(iommu_group, (void *)IOMMU_CAP_INTR_REMAP, vfio_iommu_device_capable); @@ -2270,107 +2411,25 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, goto out_detach; } - /* - * If the IOMMU can block non-coherent operations (ie PCIe TLPs with - * no-snoop set) then VFIO always turns this feature on because on Intel - * platforms it optimizes KVM to disable wbinvd emulation. - */ - if (domain->domain->ops->enforce_cache_coherency) - domain->enforce_cache_coherency = - domain->domain->ops->enforce_cache_coherency( - domain->domain); - - /* Try to match an existing compatible domain */ - list_for_each_entry(d, &iommu->domain_list, next) { - iommu_detach_group(domain->domain, group->iommu_group); - if (!iommu_attach_group(d->domain, group->iommu_group)) { - list_add(&group->next, &d->group_list); - iommu_domain_free(domain->domain); - kfree(domain); - goto done; - } - - ret = iommu_attach_group(domain->domain, group->iommu_group); - if (ret) - goto out_domain; - } - - vfio_test_domain_fgsp(domain); - - /* replay mappings on new domains */ - ret = vfio_iommu_replay(iommu, domain); - if (ret) - goto out_detach; - - if (resv_msi) { - ret = iommu_get_msi_cookie(domain->domain, resv_msi_base); - if (ret && ret != -ENODEV) - goto out_detach; - } - - list_add(&domain->next, &iommu->domain_list); - vfio_update_pgsize_bitmap(iommu); -done: /* Delete the old one and insert new iova list */ vfio_iommu_iova_insert_copy(iommu, &iova_copy); - /* - * An iommu backed group can dirty memory directly and therefore - * demotes the iommu scope until it declares itself dirty tracking - * capable via the page pinning interface. - */ - iommu->num_non_pinned_groups++; mutex_unlock(&iommu->lock); vfio_iommu_resv_free(&group_resv_regions); return 0; out_detach: - iommu_detach_group(domain->domain, group->iommu_group); -out_domain: - iommu_domain_free(domain->domain); - vfio_iommu_iova_free(&iova_copy); - vfio_iommu_resv_free(&group_resv_regions); -out_free_domain: - kfree(domain); + vfio_iommu_detach_destroy_domain(domain, iommu, group); out_free_group: kfree(group); out_unlock: mutex_unlock(&iommu->lock); + vfio_iommu_iova_free(&iova_copy); + vfio_iommu_resv_free(&group_resv_regions); return ret; } -static void vfio_iommu_unmap_unpin_all(struct vfio_iommu *iommu) -{ - struct rb_node *node; - - while ((node = rb_first(&iommu->dma_list))) - vfio_remove_dma(iommu, rb_entry(node, struct vfio_dma, node)); -} - -static void vfio_iommu_unmap_unpin_reaccount(struct vfio_iommu *iommu) -{ - struct rb_node *n, *p; - - n = rb_first(&iommu->dma_list); - for (; n; n = rb_next(n)) { - struct vfio_dma *dma; - long locked = 0, unlocked = 0; - - dma = rb_entry(n, struct vfio_dma, node); - unlocked += vfio_unmap_unpin(iommu, dma, false); - p = rb_first(&dma->pfn_list); - for (; p; p = rb_next(p)) { - struct vfio_pfn *vpfn = rb_entry(p, struct vfio_pfn, - node); - - if (!is_invalid_reserved_pfn(vpfn->pfn)) - locked++; - } - vfio_lock_acct(dma, locked - unlocked, true); - } -} - /* * Called when a domain is removed in detach. It is possible that * the removed domain decided the iova aperture window. Modify the @@ -2485,44 +2544,12 @@ static void vfio_iommu_type1_detach_group(void *iommu_data, group = find_iommu_group(domain, iommu_group); if (!group) continue; - - iommu_detach_group(domain->domain, group->iommu_group); - list_del(&group->next); - /* - * Group ownership provides privilege, if the group list is - * empty, the domain goes away. If it's the last domain with - * iommu and external domain doesn't exist, then all the - * mappings go away too. If it's the last domain with iommu and - * external domain exist, update accounting - */ - if (list_empty(&domain->group_list)) { - if (list_is_singular(&iommu->domain_list)) { - if (list_empty(&iommu->emulated_iommu_groups)) { - WARN_ON(iommu->notifier.head); - vfio_iommu_unmap_unpin_all(iommu); - } else { - vfio_iommu_unmap_unpin_reaccount(iommu); - } - } - iommu_domain_free(domain->domain); - list_del(&domain->next); - kfree(domain); - vfio_iommu_aper_expand(iommu, &iova_copy); - vfio_update_pgsize_bitmap(iommu); - } - /* - * Removal of a group without dirty tracking may allow - * the iommu scope to be promoted. - */ - if (!group->pinned_page_dirty_scope) { - iommu->num_non_pinned_groups--; - if (iommu->dirty_page_tracking) - vfio_iommu_populate_bitmap_full(iommu); - } + vfio_iommu_detach_destroy_domain(domain, iommu, group); kfree(group); break; } + vfio_iommu_aper_expand(iommu, &iova_copy); if (!vfio_iommu_resv_refresh(iommu, &iova_copy)) vfio_iommu_iova_insert_copy(iommu, &iova_copy); else