From patchwork Mon Feb 26 17:07:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13572537 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 3C726C54E4A for ; Mon, 26 Feb 2024 17:11:47 +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=w7FAtGsrcC9FgOVNs4lyD4v7fk+zFilH02AsrGuK5HY=; b=EbkWCXiUDO3Z7m fCgZtFKVysc9V/xdk3vjkGTMUGKvICQZy1y3ha0EII6aDwS4Jr6WNA5DD1spiORtMu6ubb3dRzVZo 1NwBO/WupG4kMqTtnm9eJ/ZCib0FxD/5mTv7MYiwEGa0afW+QwGOl2abWN/tryZefZfoRm8zKHMVQ 0egj4gUcMDifzt/i2wcBW42fOw1FqDKWR6QBdxxw7XdYn25mwIlNxXMTkdYx6fOhqSShMF+jwZ4GQ +s1xKrIGiWnM8yTcT9Ti6MZQQEiByP5JdH3jbXx4ybvXEN7RniCXqv2MLK+cdbVCwlKFS3qzNeDIu ibjBpGf/e/gwyj69fbtg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1reeVl-00000001rUC-03bv; Mon, 26 Feb 2024 17:11:29 +0000 Received: from mail-dm6nam12on20601.outbound.protection.outlook.com ([2a01:111:f403:2417::601] helo=NAM12-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1reeSI-00000001pjD-1gdS for linux-arm-kernel@lists.infradead.org; Mon, 26 Feb 2024 17:08:01 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HHCL2XFQWeBGq8J/Dq4u/9HE0aSAPUJ+SV7Pyh+n/Ln1R1pRqOl9ocrrdkNuu/GCtDa9IEpv17lysu3/GrnvoIjQFj05NLHCdnrHIcm9LyeL/9GQuL5Rtx8wlKAWzHPsC6vPXqyVJgUSvqdyGCbBrpLgfO9lTCXf36UM8nuioo+FdmIaytX5qya1b/gDFSpn0Rbu+r8xJF3H7smRok/RIdkcCgrA1HAaSlKndK9RmM4zZurx173lm2Kv4Hy+BlrQMDxf/thSHOMtlzB3YgsH/MGY7EOCmzKLIDIR4M1k6Kv988LA/L5DZheJNtVcqsIAyNOljqKuSid6mM2byCuZqA== 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=aGGydjNTWbOHl9vFD60RlvhE91QKMCRM8W/0YXBi9wA=; b=brAbFgajFpxelenDAFGyY8wZ6An0tGr0WfiLpRf3o922xVX0fMYHBo6CIWYbcQLNJdhwgGN6nEmjMEb/RkBqM2xOsU3g35trCoejwM8Xu3XCUNu4Lvo9qaQugzZ6vEhqBxcF/CVsBQpK6KS/hFo059NNmmz1AIdIhtCCPg0Q1GusFOFb3kMPg9n7+ClsG00EwQQjhR+lVON+nqttMeRh7wIbd6MsIp6u14EZhmg63HAsXYXBLv4m0lmS6nwvFrb0NLfGdW46wzxrISbL2jJKVQ6gO+J6XNQ0v5tg3hyRvQ9Z474wwrFtLCk7R6T+hy/GQdwaWEkB2viQBloRbfmPbQ== 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=aGGydjNTWbOHl9vFD60RlvhE91QKMCRM8W/0YXBi9wA=; b=sOzp617E3xAEB2RDxlhs/0WSwHD9u0/l+f9BHvmwojMqD72vOBh+IS5UbrdHIIoo4tVWtcGHnnuffvn9IU5t9TZhwdypfAN8o97/pzNheecd+pOl07bjKnwyX2xqJUGg+5gO4atgn8yKtzJcVNNbL/mcLmccUTmnNdTSIfziQAhQPihYgfa7RNw8l+fJfD6CvHLC5WgR3UyvS4YpeF6GhowLWuck4QpCRPD7Byel20cthRJnXwh9clBuwR7x+j7qGOPVub6S6qEvXgKs/X6kPE/zKpbX74xsU3kfTke2W20rt3vq9vYuA7YHEOV8jylEl+yogomx0NLT/6WMg4vMbA== 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 PH7PR12MB6905.namprd12.prod.outlook.com (2603:10b6:510:1b7::18) 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:35 +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:35 +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 07/16] iommu/arm-smmu-v3: Do not change the STE twice during arm_smmu_attach_dev() Date: Mon, 26 Feb 2024 13:07:18 -0400 Message-ID: <7-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: MN2PR15CA0034.namprd15.prod.outlook.com (2603:10b6:208:1b4::47) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|PH7PR12MB6905:EE_ X-MS-Office365-Filtering-Correlation-Id: 268f0c36-8564-4561-85bc-08dc36ed6ab8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: lTRMldW+qBkq83vhtS4hv3xJfQ9Ui7L1aH1fi1jsJWEBt/jq5SziHIeNjVf7j1LWHiHLiWdwyoahJ14hcfl99hJhloab2v00+4T4788/qOtMrUD8TKPhnksSpJLt8POc1yAVLJcICWCiKSwoaEXMBLSpfuB2nMxAAtdNQ/QgNDB0tWcyNcFNdOBX5zZRtzWnKau0VNCyn8QSqV15GbYFRhY22epe9aBoWIB1c8X0uI/MygL20nSTzH7/GXZjFBiIB15JQ/x5kTSjbhy2qlqQYoDAm4LPb5wZGL+gOqVeNc8YBvbZn1nEaKToTcHOhNkKjWSPmjE2gUp6E2y7OeqKNfiTxpZ08DavU134Du2nI3+svrmlqbJ1VRNMfHWE7EahsZbpq9FCf8BmPAZ83alyInJGg5+G4ZiHOvNFCXCiwNuxKa7dSSK0+2/6rZAlPpNusU9z0DQZUFwAGro0kar5p2iWXj12tyADcqvZ34//8vMm4Hv8QpE3K4ArYcOghHyXpg9nsTm10VbQjCHEzd5d8PTvsoGFrwEBgopaKRPqxJXDHwDpmQkQtmq6uxwpPUqsNl74YQxrKVboU2Wqz8SGQVXoTVkgDI32Baa9eyDWlZQ4If2VfWtRO+KFzhlRZ3I6gipbD+JxKdbflrJODBYLFg== 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: DaMzaiG24BOoIf1aC0l3GNc26GiR4D2+GkTxqVc2Y3gvZoqwx5ds/36zbHO4E0jUB6vrx64ZcD5syxNsFTpWxEJPXD6HtO+xaY0t9zTZgnN9QUZkeFUmXFTLQzNnFNQcRvmVqwEOLwDV4T3uQ/QwQILp7DQfMx0tEGl7aHiVGqDGN90esX8+4DC5zFgPXWYuVNGnYyNWwUs0wIGWZgaVB/PfMGLPqUsxHYdt7/bYyzGnCg7pdDF1s72yXe6o7xPwYZfh/chOp+ASVtq/E5LZjIgBZcf/tInrQMR2fCP9e2fN4tUBmPxJW6kgyrlGJm6oBkEt/K9o2btLc6cB3EUdyDbDc1GHq8dHO9pZC/vwD+hB+i+O6fVYI3Xo8xSZnnc5xbTWciieuRsvEDpI2KRst48habvLALZNZR70e4dpJAUCfqRY2owFJBTu2UuZifn6JDZ/ZcdNEW6feWZK5SwvQSl+YJSq7fxZYhIpTnZYLNppmmuROblO//OI1F3N5EjhSLQa056ciX0bVQIKG4gjPCDkmKV+nUNJ0z9F14bsPb3MFeg2O3vaA/njL5rhMdWZj5Vpls/4D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-85bc-08dc36ed6ab8 X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Feb 2024 17:07:30.6345 (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: u0AZNWOUACyJak5gZlR2YS5KelVLXejNwa1CoAsgj9Y0CI2Qwk5csCLeMGT1IWjx X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB6905 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240226_090758_787432_8AF96EE7 X-CRM114-Status: GOOD ( 15.59 ) 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 This was needed because the STE code required the STE to be in ABORT/BYPASS inorder to program a cdtable or S2 STE. Now that the STE code can automatically handle all transitions we can remove this step from the attach_dev flow. A few small bugs exist because of this: 1) If the core code does BLOCKED -> UNMANAGED with disable_bypass=false then there will be a moment where the STE points at BYPASS. Since this can be done by VFIO/IOMMUFD it is a small security race. 2) If the core code does IDENTITY -> DMA then any IOMMU_RESV_DIRECT regions will temporarily become BLOCKED. We'd like drivers to work in a way that allows IOMMU_RESV_DIRECT to be continuously functional during these transitions. Make arm_smmu_release_device() put the STE back to the correct ABORT/BYPASS setting. Fix a bug where a IOMMU_RESV_DIRECT was ignored on this path. As noted before the reordering of the linked list/STE/CD changes is OK against concurrent arm_smmu_share_asid() because of the arm_smmu_asid_lock. Tested-by: Shameer Kolothum Tested-by: Nicolin Chen Tested-by: Moritz Fischer Reviewed-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 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 6cdf075e9a7ee7..597a8c5f965899 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2509,7 +2509,6 @@ static void arm_smmu_disable_pasid(struct arm_smmu_master *master) static void arm_smmu_detach_dev(struct arm_smmu_master *master) { unsigned long flags; - struct arm_smmu_ste target; struct arm_smmu_domain *smmu_domain = master->domain; if (!smmu_domain) @@ -2523,11 +2522,6 @@ static void arm_smmu_detach_dev(struct arm_smmu_master *master) master->domain = NULL; master->ats_enabled = false; - if (disable_bypass) - arm_smmu_make_abort_ste(&target); - else - arm_smmu_make_bypass_ste(&target); - arm_smmu_install_ste_for_dev(master, &target); /* * Clearing the CD entry isn't strictly required to detach the domain * since the table is uninstalled anyway, but it helps avoid confusion @@ -2875,9 +2869,18 @@ static struct iommu_device *arm_smmu_probe_device(struct device *dev) static void arm_smmu_release_device(struct device *dev) { struct arm_smmu_master *master = dev_iommu_priv_get(dev); + struct arm_smmu_ste target; if (WARN_ON(arm_smmu_master_sva_enabled(master))) iopf_queue_remove_device(master->smmu->evtq.iopf, dev); + + /* Put the STE back to what arm_smmu_init_strtab() sets */ + if (disable_bypass && !dev->iommu->require_direct) + arm_smmu_make_abort_ste(&target); + else + arm_smmu_make_bypass_ste(&target); + arm_smmu_install_ste_for_dev(master, &target); + arm_smmu_detach_dev(master); arm_smmu_disable_pasid(master); arm_smmu_remove_master(master);