From patchwork Tue Dec 5 19:14:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13480668 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 632D5C4167B for ; Tue, 5 Dec 2023 19:16:26 +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=XYoLIv7Eul7tWmXivvLncsLYNaSfroiq1OvV4FR1TLQ=; b=NxHBDRyIgsSknj 7jIYOn6w9S6zOAu1EGZdTCyV847XY2f6f/orfuo2c/fOP6AUkFcmgDsdiCVMzAPCxq7e/IOUeXLff CLy2i68xe05lq+F64soUPYYIRsgXME36bp5do2uqhPH61bMg74jC/zq65Pb5e5IN3r/4PSiMTLk5H dzMJCeYarEcLAzf3G/89SiG2uDBqkRtXraZMt59kbYlBNtsSSNlGzVEmCTDjkSCNwE0Yd6V7iCZ1L hkaFHEC3j5b4xKV6GRNFahUyq6WRhOJ5dhv7nMp1ISHcgJdDOk2J/QUjN7UsMy7sdmGbciNxBhcFZ Lxtz7tKZ4aEBwAXKjoIA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rAatf-008FmS-07; Tue, 05 Dec 2023 19:15:55 +0000 Received: from mail-mw2nam12on2062d.outbound.protection.outlook.com ([2a01:111:f400:fe5a::62d] helo=NAM12-MW2-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rAat3-008F29-0g for linux-arm-kernel@lists.infradead.org; Tue, 05 Dec 2023 19:15:23 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TWYzBeftc4Yfbu4BpA6WkVTxO+rTFEq47joOcc19jc8CuVHgx46CiSePQxJtXj9eMdnQOBw9iqwCR+GS3iq32T/l0zJk1dQOnF2M6cKAj1NTlUJNHNR//lHOeIzDNt7vOYR3AhVvx5UCX6VrRoEvfAltJmIdI7tU0cuUb6iU0w0H/rkJPQi4xnJRJaMHuDJF150pXlTbs7bLfczqWhBzxCfnfaIeTFwlwggjR58+MWFOCUx0FG1voAZ8gRytSAdiw3fDMJTrxW9uNNJONx610UuMw1Mt6/PGGZhWg+uOBHluBOoZS+AGTwEt1Ng12jXf/VaHAGRzLJwA1b9OLSY3tw== 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=1rX071TW7IHzjhqW8BaEjoxygqb/wH4pos15IYzeQ9c=; b=BOl0GUHe3dW3xmD9M7fBxzEaCKsfcfcqU+h8U9ppeqQmJ88197MNW+RoEjUpg1+d92N68hCIyBfNbkqKcQ+FHzIhdD9rna/DM99Ll3m4/pgRsusrsVYyAaEB+BJJEotmBmchCa/rfNk4fG+DQLj8KGQAv3UEINz6DL2h6cQfL6NFbo344eBS/cpZP9eIshJWE0ATIUU82gDGB5Ys+zy/cvtll4JoCMGszEnLtArNwqar4S7eZ4AzvuP8sJOkl7Vt8MhW7aMK1y1w2dvJs7H1lZIo55+BbK1jvoq38UsZhSMThnzBHquDfQUqX5rLHk0UAU6UDtHvNT5KsDC2hoHkwA== 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=1rX071TW7IHzjhqW8BaEjoxygqb/wH4pos15IYzeQ9c=; b=pRjZqSLeMwdw/QNnt2KxWOQZ8RbhgV2AXQsCK+OYu3oBQc7BzJZ4jgzxPSZdVC+qoAhOQqYFFWPEBXknHBvW81zxq5ksS0F33DIb8jSJGoKUnxzz7qeY7Jv/YTb5QJUzWBWC3DdsarRAaYFs7QvrxPz5P3MAzmYhSQy8+V2Da9efRVr+HEjot0awsw7ExZZkIp7qrKbZbdkgYiYueoI3szg+yrECJ5ymT/fNgKPdF3DCCtjJAak6ELSaR38EGg68f05yR3JAEYm+2yDkk6OzfmccsVUDoxMSGKcC5vlDoeZJ/vlUtERbrNOTDOGPhpKgDrJKk/AJKR7v1pTRAQRKVA== 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 PH7PR12MB6588.namprd12.prod.outlook.com (2603:10b6:510:210::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7046.34; Tue, 5 Dec 2023 19:15:01 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::60d4:c1e3:e1aa:8f93]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::60d4:c1e3:e1aa:8f93%4]) with mapi id 15.20.7046.034; Tue, 5 Dec 2023 19:15:01 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Eric Auger , Moritz Fischer , Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Shameer Kolothum Subject: [PATCH v3 11/19] iommu/arm-smmu-v3: Do not change the STE twice during arm_smmu_attach_dev() Date: Tue, 5 Dec 2023 15:14:43 -0400 Message-ID: <11-v3-d794f8d934da+411a-smmuv3_newapi_p1_jgg@nvidia.com> In-Reply-To: <0-v3-d794f8d934da+411a-smmuv3_newapi_p1_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR15CA0042.namprd15.prod.outlook.com (2603:10b6:208:237::11) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|PH7PR12MB6588:EE_ X-MS-Office365-Filtering-Correlation-Id: 21cb0e12-e511-40db-1b46-08dbf5c67650 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: P9EyiQMnu31HikIjjNwrVmfW0ssT633ZXSCo42hQMPITIUeZ2orfQnkm/2bkqcqhSHbVotTGcDQqnwkyWj4BU7x2e/pBg3mgauiPGh5etw5GIdVF78OxzbK0L6c2OyyU06e/UFsruNS5YUTtRoW6rqMaKSI595lPniEhbk/T059L9wjIkPbEp2MOHapkZ97tYG7jmOs89NoId/jJcdAG98+7AbgDnMFzvewieokJSVOM5zhRJRooQPHrMF9RWd0631Rz8toFtz3ynhXFm56CwGxbtjrDjAsPOd6xdYQ4+qtoyPjRNz4YsEqqkycxmINAgriN5s5BaW26FISJ1pJnE/TPxB7JLDEqXUgiWRDFZji1GpfBsU7H7RHYgIbJ38Areemd7IylnBfBQLK6e9MIGLYzFA5s3APTyL5NmVnFmoLr5MSKozGPj3lYhSa6X4ix0WqwPoqd0K3QREMu2u3LW9cpnbMNV78UIqx2nynqr01HDrObvTZqIfRg8Tiw/Z0BqkajuDJrhjirvEgFM8jQZLVh1awQnA8VzIFBWENpV+eguroLnUxeBJAGcLlom6Nf 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)(39860400002)(366004)(136003)(396003)(346002)(376002)(230922051799003)(1800799012)(451199024)(64100799003)(186009)(478600001)(6512007)(6506007)(8936002)(6486002)(2616005)(26005)(6666004)(54906003)(66476007)(110136005)(66556008)(8676002)(66946007)(4326008)(316002)(83380400001)(38100700002)(7416002)(36756003)(5660300002)(2906002)(86362001)(41300700001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: f2PELPqOZbcU9ApwjuXgvrJtkCSVphTzsYv7+PCTiXbmT+2b3GrB/glsuJw97iscD7xBDYs6FwErzCCsaZjHbcg0MSKHcmqOemb3+SSHvnDjd2oN3SFRpqlCNMjVBUNUYAXYXKNnUvJekK8vsFgXc4SP5kHRC2BXJ7a7B1mQgQLJRJi4PtoIaBOD5JxhQo2WF7xnQB7kOlvKkOJtiHnFP/6eholSZdb+sw43jbVA3QojRKN85NZLd1485LAtAPSxHcuIlLikyOP4q3kuOEifsuz5lH1xTzl8WR6Nad1sFTN9XHXru1JmGxijAolmpqjT5Uhbs3HfQ0gnb7AyGI9ZZPBvQXGn5l/5npGcl1xWmdk2/fl8Id/fquAlNTRqhb0GYKWN3ahPvCXHCoHbrWRNCIc60N7piiZkooRhA6HY/0RXSXup6CA4Dybzb7ar8iCa9i2tvosLt7Cjbk4shr0uC1oi7nTdrU2em5f2My5QR7K3YgabUyzvsvs3YQZ7AAyZO26md5yed5KvwfDCFSwHluebJL+mnnxfiWbMe+8xDL23BQxjV7zVI78VXaTTmCE5iR1jQfbBi5c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b0e12-e511-40db-1b46-08dbf5c67650 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Dec 2023 19:14:54.1375 (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: XcbniNq8UpAN7E1UQsxArsD9Y+J62VXdDf57AUPEGXHfjRE71osCeByvQVOP0pBk X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB6588 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231205_111517_272046_B6BF5574 X-CRM114-Status: GOOD ( 15.38 ) 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 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 0c1d70b8f325ed..7b1f7fa27b3df0 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2482,7 +2482,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) @@ -2496,11 +2495,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 @@ -2852,9 +2846,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);