From patchwork Wed Nov 1 23:36:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443207 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 45DC4C0018A for ; Thu, 2 Nov 2023 00:45:48 +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=m9gup8jOQV7M2mH9a1UfoD7in6YVkGte4QN6cyhvr2A=; b=MTIHbd912uEH5f 2mpCfu4ppmi2DriJImCCTcMFUXM6IkAsNPjpLjvC+Dhn0u+961Ee/f6DUNwqj8lIzyy/ErWefakIc /qbTGqMhmhH2kZBRgfgRcZ5WkLkT0ibF3HUnR2c995ENyVXm4bHTOHVLIy9NslS5h87VDIuxsMFC2 U3g824FdUBiXerPJf8ocLEfV0uN82Gr8fg6Er7u5OzmpHgf6Zdw/v8rC4KrkzWYRNx23qxP8wF2MD 8YPc1Lo6mr47cz/VzgIV8vb4+FE1exvbV9yrL0780iNOX/1gsvE1ysOUmCp5eVEBKfQmBbKz2Prn3 K4vqxC2/dfwqcmo/W1Pw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyLq6-008Pyr-1t; Thu, 02 Nov 2023 00:45:38 +0000 Received: from mail-bn8nam04on2061d.outbound.protection.outlook.com ([2a01:111:f400:7e8d::61d] helo=NAM04-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKls-008JWb-2R for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:24 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eDyTu0FA/6YWhhgcz2L0PhZdEI6nhtqg9ZSWRmI4TAU6tdwXq46wvlYNJW6PM5xZwcXY0HMBYWCFUdjChCdmg6CawuPGjVORK0oksJwbP8dIp7S8L6hkYDJChcEb+nduUFjv9JXV20cHbOzhS+8jco8tpL1zBnyBhJ1JK/HwDPxBvaX03533YweA/MTNrBuK/+0qhsjKVM7xq+Go7qKMsq/B6rnlBTOvXZMSTMbAcBhD7j54LXMfQz7gNPj7O7nR8v8fITPy9pVm4LAjMafhzNmgaAmL9prP5YsVIvjTg39AAlYVdsM6ogwEt5S7NYRalbqqiMhxgoogZWED/h4SMg== 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=D/nZPUhNVxOcPpYbLqvm+70v5pj1odoJ3S3tIdNDHI4=; b=Vk+cPV++jK47/tSX/tcKmIB6pyUzk+iEcZVv16pLYucm76h2Z/iaqApum216Wf+Blcf07ta7Lw3SigjDLBnUxKsUaUY0Z+vx8d2UtKbL5WY5b8ZbN5o2cLZIgWpnDfyhksJjY4Y/Qt4vnWdzE3ArwjxGEYmzil8o8ljVLMRXxVdieJNkg5T2GWGUvF6dcSNtMZCEOW97CgY7ZneMfuFpC9ZQ91STFg/YpdeLco2M5a6uAz4rRMiJC9eVjyPDd6sf2f/ubp4IRKgfOI+d1NWV//y4iX66nydsC4Ys1vPRsITetBBsVtfDspwdBE7JTFr2UL8VVDatHNTxW2t0zcNhkA== 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=D/nZPUhNVxOcPpYbLqvm+70v5pj1odoJ3S3tIdNDHI4=; b=UxGwds3mHmyF564Oobm6UFX/2vzc7S0jQJHjUkAZDFDP561Oe+ijahpgpfE+QhTq/Z7XuIlfvMGFwmbdYbBYCDfM5mLh9gUUiFQPkyFrV1CDtlYAMnqebm0kUK0fcQ0BFJcpcChGIYeNG7vVy4hxfyrFQexI1RKpT91ss2K4+lZOfKjuQm3UJK1RCLz8Mh58GLqUKnBUHnulT0ihqv1eMsZyC94r4Ys8FSAEljFUVejl/DIagy9jjphyJoMjPM64yGIar7ai6WEcrXP6ed7MVWZ9m4MP/wJmUtRIPaG+x/U5TlGF1OU6dRv6dL9V1L9i0HDWyofBMatzmUnikW4nkg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by BN9PR12MB5338.namprd12.prod.outlook.com (2603:10b6:408:103::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6954.21; Wed, 1 Nov 2023 23:36:50 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:50 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 22/27] iommu/arm-smmu-v3: Consolidate freeing the ASID/VMID Date: Wed, 1 Nov 2023 20:36:40 -0300 Message-ID: <22-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR19CA0052.namprd19.prod.outlook.com (2603:10b6:208:19b::29) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BN9PR12MB5338:EE_ X-MS-Office365-Filtering-Correlation-Id: 2022dc1a-c9fe-4cb1-402c-08dbdb336a0e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ui1N5+7sCkW9kVuNI09AA15N9tL4j2hnDRg+lhnug3EHFf6vyX5TawWcSTMl50vP0UuB4ya5NqAEaaeh/SQrsfP7JLsZgNbOi834uWgmXyxNKXQlwHiMEZkqKEzdOw4H+2B0xBckDBn+jFy2kNq4grJHkZxPv7nLXtLVkgzxaIyx0HNR8fwvfdFXc0CdWeJIM79EVyEayMrU6IYUF814+2iNigjsBB4WCUx+TBQIg32X6uzbNlNHeFzxLKk9Dip3XYgCyhmU0uD7lSRzWf5yh1vQStiWN6c4yD9FN44B3bNq7cq+gTLdH6VSFqfbLrfYpTClYgtf98MilHbe66HT/5Qw3WvTg9z71nYf5+y9XZZtmidBb6dI0AJM39Zjy7nUz5/I+kyMqh/SFUK/VoocqkJ3CWeT8OQWWvSRRzOxQBt/0TgG805VQAQhpTw9lgC1G8ddLAWX3EuZ6/oabB+sHUgQvDqsFDZdu6MUqv/9JlIBDsHwAz7x1+OX5qIIUFQ4pQs2QTn+FOHNUL9JnCOUXBA/98Z6OHyyDMf+L0zfpJOYyiRdYZKxLmHCTLGhaNvz X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(396003)(39860400002)(136003)(346002)(376002)(230922051799003)(451199024)(1800799009)(186009)(64100799003)(6486002)(2906002)(478600001)(36756003)(54906003)(66476007)(86362001)(110136005)(66556008)(66946007)(5660300002)(41300700001)(26005)(107886003)(2616005)(38100700002)(83380400001)(8936002)(8676002)(4326008)(6506007)(316002)(6666004)(6512007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: SKW0e0TXwd65lSadLDnbECK2zc1mKO3gMt46uIoeZPKLk4k6WP6fnm7LbI/Wc9ghJZa/JHg/Oqux9N/OFsfeXtKEMrF7oOad1usazj7d46LX+6X9YohiNCnKZh07dmkLutIFalhAa/mWGy2hoe0oG6I8JWQlfxxWuP50hA8xkmPxRUobH6WM6nHcoc5v3MbWhSN39TGT4xSn7+7Y2mbQCk/ccjOW4sGVIcmCi2Kd5EDeaPBz0mNWNlWj23+mBva5ZDpaSgYBgW5yx8NMjd38BzMEJo7jzlM7FweGMzE6htbVphZSaTeGXs7GO/KHIp3Njur4nRFu1qlp1gCM3Z951aOYqJ/tLC+VN8hnvyWiVEhfA92v4T/huciStJbkeSW8ioBecRq0pTGut2lk0C6WXZtmWGvGNgA4aSU8xelzQl98Yxx1wtnp5guPfswRH7yWG1Ev6k4J29yGIiUFGSzdFu30btouHGmgw2kBwKW5lNCr9bC9AH1SqfkAoKU8+U8WNzCKYI01hCwISzlEst/Pa+fhoYq0NMMRbkWKEE1XAoeXeeT1DdjTWqrP9NOlJq4Dyj9rK0SenguaNI2c7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MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:47.3179 (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: fCMYpz9jOHX2uB5H5SOxxyeyUdoDnFKTIQdf5QrsAhXaSrooQ/Y+ne2m/zsY0vof X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5338 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163712_826447_8EC17EA2 X-CRM114-Status: GOOD ( 20.80 ) 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. Signed-off-by: Jason Gunthorpe --- .../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 a3b85aa5e48ce6..66de6cb62f9387 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 @@ -370,18 +370,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 @@ -426,7 +421,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 c221ab138ebb87..7fd376a4e1752a 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2232,25 +2232,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 a229ad0adf6a49..c7c4f4fda31297 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -789,6 +789,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,