From patchwork Mon Feb 26 17:07:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13572675 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 A690DC5478C for ; Mon, 26 Feb 2024 18:16:39 +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=za62hY2uA33Ed3UmZQ7/AHnLMee+RHxnQknS64qyF6M=; b=B6BmNUjK39O6N7 DhylqVo2sQOdMBhO/r0ugR4BHvZFUYE7FNjH6X7k1dEaj+80jl86qb5IcqUPG37pgKsYgYKTcz+LZ hkQ0TibJtr07o3sb1s/XsVRerK2fIXqZ+4fvYg+AODW6sja8zjwpk1dE4d7pP0y5G+VROFCTBSHPN vqQmK4cKlHXlQJtOSQlvKGne++np/57nLQhAlZv3M3u3AiMP8BnvMi3WmWkoD2/fxNh19kB4c1Tkt 9suBkDhILJrVb3Ksw7bOKj14StWOUex0mdcS4K5D6EM73T8QEc/ii+g87aKtKFIYFvKJV+UQHmHXm FCX5BkWSiQIs9IpkLBvg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1refWg-000000029Cx-1BgB; Mon, 26 Feb 2024 18:16:30 +0000 Received: from mail-bn8nam12on20601.outbound.protection.outlook.com ([2a01:111:f403:2418::601] helo=NAM12-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1reeSC-00000001pm4-2SF4 for linux-arm-kernel@lists.infradead.org; Mon, 26 Feb 2024 17:07:52 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Zso5XF6AfMqpa1uTE2FUfkYRwggAZD9JvdOAhykIKfq5vYGfaIfgtRW5E0y4sA+VRgRA2XPZOLWWcNkQtNDsuAzu7PB5MHwnmTBHZAY8mTtxGyCpkyWuE9iniv9c9FVxbcdpG1CWioGMHX0h0vJ72s661E5aDjFjswA1UArNLYJNtjsndEppX0hg4Y9Rdcp7whTwDCgJXuM6/inCVnu5sygvD1ter+1D9xw+7Dokhks5v02mlu9csa3UyZ0WoHuDm4zFebB4MN7RsHUyTnkij6GPqWMrYpTZQXL5buGYLXEdatQAlB8/wvN8xmGhHSfI2CDWd26NVLJ6hthMQIqJEQ== 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=v4j2mcTm+Aa/oMQiUnxUzj9IKjIrzKi0EFXG/jhnrW4=; b=UVWehrPpMS88ltc5HC+IVv9PhrRL2rN1Aer/fFNUASyPIeHUOuB0BxQMO+3pY8fxhzvEpSZEJZ8TncVeFyNPMuGk7ObzAfhKao3mJKc1ZzxEbIxRqy8EG9PiLTWjZI3QlPdFKBVTOaMFA5tPEYIa+d+tigggyTC82CGsGCD+GJNpnb1uD9QBI/z6Z/wiyZgTZI0RufkcWQOH4ha0eIUrJUs74YAbhvhkARFBtitp5LGKiTUOiyccRP08Nvyl/3RN81DLh7bHCoHJ+MHr36HxIsfD5xPIRbFe8Hqoj1bmkNSA8JVq4Yv1sqYEJXab905/mhEdupVcgPnbm1XIyodlxA== 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=v4j2mcTm+Aa/oMQiUnxUzj9IKjIrzKi0EFXG/jhnrW4=; b=nUxWZX32zyXOlFfFhiThJrO/zsg2B0oGHGL1+MDZth3HEhVt2iQhNh0ddWNhinY4XKJBc1wZS/4MnAxfYm2Di/zQDRkRFQW7ZVax9qR+HecNfdmGlqtubSGqJX7DtUxEefRwPVFNoUOnHuFBwthFkleeAII++POKwQZb0/Mk1Uv+k2kiymYsxyqwjC7UYgFMYnBlHGaqy95V4JE9YNr/g+G0x3XmzvNoj7i/bJEGnN65H/yqldgAuP1ru8hE4dEN0eVkOho2DFpLN9EoN9PtssXw5k+0BUpjE7CkiaAoAMMJTD6QwLjOpL0DClQX3OERzJF6FaFrAu1FRoxB2PaO4g== 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 SN7PR12MB6768.namprd12.prod.outlook.com (2603:10b6:806:268::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7316.33; Mon, 26 Feb 2024 17:07:36 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::2e3e:c7c0:84da:3941]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::2e3e:c7c0:84da:3941%6]) with mapi id 15.20.7316.034; Mon, 26 Feb 2024 17:07:36 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Lu Baolu , Jean-Philippe Brucker , Joerg Roedel , Moritz Fischer , Moritz Fischer , Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Shameerali Kolothum Thodi , Mostafa Saleh , Zhangfei Gao Subject: [PATCH v6 05/16] iommu/arm-smmu-v3: Hold arm_smmu_asid_lock during all of attach_dev Date: Mon, 26 Feb 2024 13:07:16 -0400 Message-ID: <5-v6-96275f25c39d+2d4-smmuv3_newapi_p1_jgg@nvidia.com> In-Reply-To: <0-v6-96275f25c39d+2d4-smmuv3_newapi_p1_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR03CA0013.namprd03.prod.outlook.com (2603:10b6:208:23a::18) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|SN7PR12MB6768:EE_ X-MS-Office365-Filtering-Correlation-Id: 71c39cad-15b9-4c55-b630-08dc36ed6b15 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: FenS+SvTsLBBCCUJKdx3/RILCblm4uYyUytKODQmZ9+7HeUVYM+gptojOigci74NKUwxQEtjKEjGTr9KlLbnEeETz9aR8DBfRPGYiCRoUJpfvG9VWR2dB49AhBk86XRFY9wOAC6HVEhIKBnQKm0E2MiGLwHvfXoRU8ubKWUtGFDPhGoF87UEZuW+DksbnGXFN0fsxiKZU4C/DdA88hX1HT8mcDYb5OC4sPmbxANaaluuRTKKkmMMzuzArHWE2BpBies3fsilDbn0gmuG7Xa5pnQvU3tSeuXSsZD97fApRuMTzDShhZvte6W4wGZlAAnYqXH6tovgpwabO4EMhXA0K1evmNmWcZ8MyaTsYaluaNpXMOxKlVykkcusPDzW3p1Uc36CxEP6kbWk5OwXtYTIvx2M8xrcE7gLnIWBJRUG4VSo2c5Hcg4scNjW9fNOt/8Hz2T8w5RPVa7v0ylUX63ukvxor5r781L5oN/95b8rmak092GZtTKSxJwh+z/VckKkxv2Ld6dk0A0xT8guw6Zb4jYnz80byfuoZMaxGB5YX4Qr+AOWMMKo2MXNtJYRvYQHMKRwmiClOJnKuI7wpcxgwK8Z40umKoWyd6/WFHkMPkmITzM5u8ebA9g+QGrns+ZJHSxPO5uf2iqVHVU/Z/YLJw== 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);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: m34TLF+KlHgLcsZe4DJLXP1F+bjJzvqoiKeFh7xebMLwpT7Lts0odd70ZrtlurHImxNSBOXNdA2WX5CUrNakFBPeWThDqszRnntoZUAdtD8SD65UrOnUHbGNHiLVEooihIkL+GDX6hTULisoek4hpOcdOSVskfFSfh3xtEpnf3M6AJq8omVbFZDCwjOdAPcpQvtiwNqrX04D/rpzTi41vWdGh8OxEkm8Xnypom906fWFtPj04kJOIWwalyx6l9ekSixU+K1iqUSOeaaKhPYRbEdEaMiiyaGqotTx6Fm56R6CG6j6F8QzLfA1d/69LzYTN97Yz88k9CIYYlgO/MEEzkN//yZdZF64FpBxPDvemu9AR+H6ZtLbCJzta2pODSLrVBzqO9EBsuR/LZVkjuivhtrQTf3tND9edb5cBgKjTHa3N5LH77H3dbV4RxPKTMOwNOu15ZtG0tPYrrh7pOOGFSTP5Fv4KUnX0v+jOX9EomeTZ5pEfSAMSfEJnzeA5me1TyH4F/ccYa3k5yEZiXxcEPJRxHRmpIcjEJehw9SFuaeO+Wlkn/HFw6erD6zmRa+SOQT1Ea+N8YxQttyWHGL9d4aZIDNr0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rossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Feb 2024 17:07:31.2846 (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: DpHvcfaUHBx0fMHVLxq7qI6+1pE8XEGrFs45HDeYBB+ESZkL8q81u9aym6qXcUJz X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB6768 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240226_090749_967161_81FB85A2 X-CRM114-Status: GOOD ( 20.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: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The BTM support wants to be able to change the ASID of any smmu_domain. When it goes to do this it holds the arm_smmu_asid_lock and iterates over the target domain's devices list. During attach of a S1 domain we must ensure that the devices list and CD are in sync, otherwise we could miss CD updates or a parallel CD update could push an out of date CD. This is pretty complicated, and almost works today because arm_smmu_detach_dev() removes the master from the linked list before working on the CD entries, preventing parallel update of the CD. However, it does have an issue where the CD can remain programed while the domain appears to be unattached. arm_smmu_share_asid() will then not clear any CD entriess and install its own CD entry with the same ASID concurrently. This creates a small race window where the IOMMU can see two ASIDs pointing to different translations. CPU0 CPU1 arm_smmu_attach_dev() arm_smmu_detach_dev() spin_lock_irqsave(&smmu_domain->devices_lock, flags); list_del(&master->domain_head); spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); arm_smmu_mmu_notifier_get() arm_smmu_alloc_shared_cd() arm_smmu_share_asid(): // Does nothing due to list_del above arm_smmu_update_ctx_desc_devices() arm_smmu_tlb_inv_asid() arm_smmu_write_ctx_desc() ** Now the ASID is in two CDs with different translation arm_smmu_write_ctx_desc(master, IOMMU_NO_PASID, NULL); Solve this by wrapping most of the attach flow in the arm_smmu_asid_lock. This locks more than strictly needed to prepare for the next patch which will reorganize the order of the linked list, STE and CD changes. Move arm_smmu_detach_dev() till after we have initialized the domain so the lock can be held for less time. Reviewed-by: Michael Shavit Reviewed-by: Nicolin Chen Reviewed-by: Mostafa Saleh Tested-by: Shameer Kolothum Tested-by: Nicolin Chen Tested-by: Moritz Fischer Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 22 ++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) 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 b81e621a8e5921..d2fc609fab60ab 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2586,8 +2586,6 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) return -EBUSY; } - arm_smmu_detach_dev(master); - mutex_lock(&smmu_domain->init_mutex); if (!smmu_domain->smmu) { @@ -2602,6 +2600,16 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) if (ret) return ret; + /* + * Prevent arm_smmu_share_asid() from trying to change the ASID + * of either the old or new domain while we are working on it. + * This allows the STE and the smmu_domain->devices list to + * be inconsistent during this routine. + */ + mutex_lock(&arm_smmu_asid_lock); + + arm_smmu_detach_dev(master); + master->domain = smmu_domain; /* @@ -2627,13 +2635,7 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) } } - /* - * Prevent SVA from concurrently modifying the CD or writing to - * the CD entry - */ - mutex_lock(&arm_smmu_asid_lock); ret = arm_smmu_write_ctx_desc(master, IOMMU_NO_PASID, &smmu_domain->cd); - mutex_unlock(&arm_smmu_asid_lock); if (ret) { master->domain = NULL; goto out_list_del; @@ -2643,13 +2645,15 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) arm_smmu_install_ste_for_dev(master); arm_smmu_enable_ats(master); - return 0; + goto out_unlock; out_list_del: spin_lock_irqsave(&smmu_domain->devices_lock, flags); list_del(&master->domain_head); spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); +out_unlock: + mutex_unlock(&arm_smmu_asid_lock); return ret; }