From patchwork Mon Mar 4 23:44:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13581534 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 5F8C5C54E55 for ; Tue, 5 Mar 2024 02:06:08 +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:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=V4GbEVuUsRNP2+h8d5gvq0n/NzICNuTiVFDJxvnVAhw=; b=VItLwnOYTWEKUJ qfS25oxM0a8K3TMf0o9gN+8fA8/lAE4qEJBqRQKUENDqIpGhNI7N9bcDhTvPZ4wX4/DRVRIA2bKTO kF31bQ7R59VooukY/4+erD9ieg2/6a7acPPPSalmu7gATUOdwGvqR8nBBNYyVEbyG5PXl6o/GBgDU zVZtFLCrbEbMA2AMVcZ8gzFpjjiACFSihKW+17ylFlmWxNS9OgXt1aTXmnlbkHuFb/oEFfQUEqSnG NrRavfitKE2zI3mfvJp7cZ9nwIpHmCEfvdijQ8y0hOWIdi0m8GCSD373JkIGVtgEEQd9rp2boC/ec 4Dl+aOjaVvDDMgKw6O8A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rhKBp-0000000BUD7-0opi; Tue, 05 Mar 2024 02:05:57 +0000 Received: from mail-bn8nam11on20601.outbound.protection.outlook.com ([2a01:111:f403:2414::601] helo=NAM11-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rhHzm-0000000B7Kx-1vBr for linux-arm-kernel@lists.infradead.org; Mon, 04 Mar 2024 23:45:42 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=H2PGcPrm/TsYVqNUvLushVQaMROU3Q2Oh+RYkB6Njqa2uwA7E+DBfBo03cJCVWF9Ld+q1/Cs//4+MqxRouNI5eOqMZQFbUt+xzfUrDkETIdGduLAXavX1HaOVLwJqVsN6eqN6sA7ZFmr704HHiLgCs29/nplYIK/P2gSsvjQKxHMqBusY+IzVUcNc0DMYn+WL76mnipQJ6w+9B2F+VoFN/GbGBqAmvJ/ZSJ/rJJNSkGn9KYhF6+gAJW1fQ07Nl5NG9YS8L4ajncS7DDlZdSKsBy5Z2iRfPNfkib6W3lsKaUFZ6dMEqagYM+omiL1jp3K6JYsgpbASCuz6KeQU/ls/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=GElaMr4LLFHOstJvBBnhlT+J/qd/oGU07MDgIOFHA5o=; b=mmohnQwJf/+wcdlM4CLX+af6Ew16mSFgBtESFuvRJ8SEc5/+S0OtcZt/+YWQ7sRqg6aydqJquCRLHCVHnlI8K0Cl6C28dFTp0J4PPy8blLUh6nkMFWaNDy5lZLzGxM5EgnqLgDVL2JoMXlMNFkYDDib3Hn5IhDkjLsC72Btmiprdosusjqc0bg1l2OI+5JGG4kq7a8A9JEWL52Pt6Tq+1JSwnwlHH2xdqWX4p59n6lEoX8PVjXljSYuLZFJS0Gn+yQDzwqqoDV+kvHyTClUMYP1rhZEHeOB7harm0vN+fUPT9QfKZeGcqlFslkmNYM+OHO4p/3yzeZtyeaiJ9vzoag== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; 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=GElaMr4LLFHOstJvBBnhlT+J/qd/oGU07MDgIOFHA5o=; b=ZTukxv7jrTV9RR3ItveMT+qwC3uaUN4m+peVSlp5GiX0hfKVgfCaIGb0n0mmCa7QKhSI5SY5O/+iIQ1DDwNUVrB7czjWLKMY9+Aleg2BZBb0F1EfKTdmTx/CkWUDG7d+7KS2IeV9xBsiJ8ECORlE+fCBPN+QNJ0GF8i3qWNIHxCO59Pw9EC5suNP7it2FBx29OOo++NOeJje6pjKB6I5r+MFZVJpFaqJCshRVpJ5sRzsPxHGyhHIwZbGzhdeXz1TpcIyw8DPpedZoo/mkov27yRoLJfAnVKBsNn1qIDC1ZvdTkBTEwoBGDXTppETOc5AMzW8Wsr1ViBU9POD57/urw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) by DS0PR12MB6584.namprd12.prod.outlook.com (2603:10b6:8:d0::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7339.39; Mon, 4 Mar 2024 23:44:27 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c33c:18db:c570:33b3]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c33c:18db:c570:33b3%5]) with mapi id 15.20.7339.035; Mon, 4 Mar 2024 23:44:26 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Eric Auger , Jean-Philippe Brucker , Moritz Fischer , Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Shameerali Kolothum Thodi Subject: [PATCH v5 22/27] iommu/arm-smmu-v3: Consolidate freeing the ASID/VMID Date: Mon, 4 Mar 2024 19:44:10 -0400 Message-ID: <22-v5-9a37e0c884ce+31e3-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v5-9a37e0c884ce+31e3-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR20CA0041.namprd20.prod.outlook.com (2603:10b6:208:235::10) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|DS0PR12MB6584:EE_ X-MS-Office365-Filtering-Correlation-Id: 2a3007f3-f315-4bf0-8d17-08dc3ca5025b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: dfO+MV7309QTtdrdUYfbW1kk/EMPp6S8nO9jMkS7n5F9YHZRVTVNv3VNrFVXnm4nYfCUliuCWr/6Q+g7dN8GWIpe6C/be7tAzBncVymEConMy/e8tIBWnGcX1x90FTftsTQxczgII7PmItyo7znE46D6ns4KM4L0wOdwrmzl2GP6iBfCGB1v6lyjn5IkQlTfY7TE9EmS2PJo/njiPw3579n92sGwz4935wCR0IUZWrQ2PCClBfflK4hSk8LoS+W8n26u8gHk1jd5p+Yb2gzEijHQeQL766vLG3biVivaw3Lj10fQpFDb5Y13C7QpDZKHO0R0PsJ6QYUkABcQ7giVlTgKt0dGbKK6aG66P/3mDtQcfEFfs269uGiCMpE8yxMl6T8LOG+nMPoHQ0pRIOt0SZNrA/natWJcSaqAd/Zv0dfnCyweB+e64LpsIpoBNWkh++vt8o7f3n48LY/PZkFuUeQ0o90VkU+sIPZCNoDgHndyH+THKWvxSUqZkDzasNed7JP0tmHTYa0OnZxhqFTUZyDbPZJm9PTC6am/sB35kmJhbvIyPgCYa5sUKWQP3dZBnTUYunytnFDNYDl5of1kv0VWZd+7QDDKFwt8iDYjADp7Bd5rMA9DUiXnMizaWEmwR72vZPlBLPdixm9pe1F8dHsIacP6JwCLM6A76xyoO0A= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR12MB3849.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(376005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: NJ7hR1K8xgURxsRdC/Nt/pW9dU8Fy4gpcITTBws+0rBqFVdbPK5AxviqYKaCd9TSYERsTl9vPnIzA/k8/k0sO5TqfXRpi/WcrU5jRAUDeczmhDjRoPH7oJA42nuRi8SyjT0KcCEN/wX6fkNgyN46TXRGMrE61kce6Uv4xS63Lvtatg8O6sdVBpVDHoPwngwwVCjsVvzSKd0OHFwUMrE4tntE+SU2wne+OV3Dg9lq+hNpVlpt5gVX1J6ri3tZ4sD7MidQcF1bOTdhBiZx2TFDOAgS93r2lpf+3BgG1y/qVf9LdNidJvnkEPem+kGpEym/0uFVtfrSWVrSOD4b5zNnlqQ08MJvp3gfOFznpVwOcBibSE2NIsWP3NjHDDDam/ssMUSscUdJVq0LtlYv8qLIkXCfntAQRc3paV9s5JxsuJYU/oEdc+wBVKntZ4BlvF6knNKsiWKdxNnEjxe197zhoRmkPV/Ka4x1PjRwTZzUrnlqbOm1basrkLjE6g2IVHTkBHPrtjiKF3/k1J29jM+iH5UTr8Np6DehZn9WtPhqoC3pV2+7Nlr6TlFgDSf7K+QNATqJMJD1QEFrgHaD/nb84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ange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Mar 2024 23:44:18.7534 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: GUcgaC2RQoLr1Bm8wq3/PqSG2EzA7sbEF0JTVTGQkFolPo7P9o5H11u5J5jG6v7Z X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB6584 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240304_154522_980243_2DEB8334 X-CRM114-Status: GOOD ( 20.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: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The SMMUv3 IOTLB is tagged with a VMID/ASID cache tag. Any time the underlying translation is changed these need to be invalidated. At boot time the IOTLB starts out empty and all cache tags are available for allocation. When a tag is taken out of the allocator the code assumes the IOTLB doesn't reference it, and immediately programs it into a STE/CD. If the cache is referencing the tag then it will have stale data and IOMMU will become incoherent. Thus, whenever an ASID/VMID is freed back to the allocator we need to know that the IOTLB doesn't have any references to it. The SVA code correctly had an invalidation here, but the paging code does not. Consolidate freeing the VMID/ASID to one place and consistently flush both ID types before returning to their allocators. Tested-by: Nicolin Chen Signed-off-by: Jason Gunthorpe Reviewed-by: Michael Shavit --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 9 ++--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 36 +++++++++++++------ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 1 + 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c index cb0da4e5a5517a..3a9f4ef47c6b6f 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c @@ -381,18 +381,13 @@ static void arm_smmu_sva_domain_free(struct iommu_domain *domain) { struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); - /* - * Ensure the ASID is empty in the iommu cache before allowing reuse. - */ - arm_smmu_tlb_inv_asid(smmu_domain->smmu, smmu_domain->cd.asid); - /* * Notice that the arm_smmu_mm_arch_invalidate_secondary_tlbs op can * still be called/running at this point. We allow the ASID to be * reused, and if there is a race then it just suffers harmless * unnecessary invalidation. */ - xa_erase(&arm_smmu_asid_xa, smmu_domain->cd.asid); + arm_smmu_domain_free_id(smmu_domain); /* * Actual free is defered to the SRCU callback @@ -437,7 +432,7 @@ struct iommu_domain *arm_smmu_sva_domain_alloc(struct device *dev, return &smmu_domain->domain; err_asid: - xa_erase(&arm_smmu_asid_xa, smmu_domain->cd.asid); + arm_smmu_domain_free_id(smmu_domain); err_free: kfree(smmu_domain); return ERR_PTR(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 5642321b2124d9..4f22eb810c8dbd 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2296,25 +2296,41 @@ static struct iommu_domain *arm_smmu_domain_alloc_paging(struct device *dev) return &smmu_domain->domain; } -static void arm_smmu_domain_free(struct iommu_domain *domain) +/* + * Return the domain's ASID or VMID back to the allocator. All IDs in the + * allocator do not have an IOTLB entries referencing them. + */ +void arm_smmu_domain_free_id(struct arm_smmu_domain *smmu_domain) { - struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); struct arm_smmu_device *smmu = smmu_domain->smmu; - free_io_pgtable_ops(smmu_domain->pgtbl_ops); + if ((smmu_domain->stage == ARM_SMMU_DOMAIN_S1 || + smmu_domain->domain.type == IOMMU_DOMAIN_SVA) && + smmu_domain->cd.asid) { + arm_smmu_tlb_inv_asid(smmu, smmu_domain->cd.asid); - /* Free the ASID or VMID */ - if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) { /* Prevent SVA from touching the CD while we're freeing it */ mutex_lock(&arm_smmu_asid_lock); xa_erase(&arm_smmu_asid_xa, smmu_domain->cd.asid); mutex_unlock(&arm_smmu_asid_lock); - } else { - struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg; - if (cfg->vmid) - ida_free(&smmu->vmid_map, cfg->vmid); - } + } else if (smmu_domain->stage == ARM_SMMU_DOMAIN_S2 && + smmu_domain->s2_cfg.vmid) { + struct arm_smmu_cmdq_ent cmd = { + .opcode = CMDQ_OP_TLBI_S12_VMALL, + .tlbi.vmid = smmu_domain->s2_cfg.vmid + }; + arm_smmu_cmdq_issue_cmd_with_sync(smmu, &cmd); + ida_free(&smmu->vmid_map, smmu_domain->s2_cfg.vmid); + } +} + +static void arm_smmu_domain_free(struct iommu_domain *domain) +{ + struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); + + free_io_pgtable_ops(smmu_domain->pgtbl_ops); + arm_smmu_domain_free_id(smmu_domain); kfree(smmu_domain); } diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h index cfae4d69cd810c..4631f0ac396dc3 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -774,6 +774,7 @@ int arm_smmu_set_pasid(struct arm_smmu_master *master, void arm_smmu_remove_pasid(struct arm_smmu_master *master, struct arm_smmu_domain *smmu_domain, ioasid_t pasid); +void arm_smmu_domain_free_id(struct arm_smmu_domain *smmu_domain); void arm_smmu_tlb_inv_asid(struct arm_smmu_device *smmu, u16 asid); void arm_smmu_tlb_inv_range_asid(unsigned long iova, size_t size, int asid, size_t granule, bool leaf,