From patchwork Wed Nov 1 23:36:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443149 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 E7438C4167D for ; Wed, 1 Nov 2023 23:37:55 +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=xDkT09kwy4nv4KF0uLe4lHUIs2w1dW5kuCAFMclxMJs=; b=qkm3HrCuEA0vy9 nypZu9pTetxD/8pk7gBk2sNicKD3nmoxU3WA8bcnb2w6N+hmdxwt1ohzSakAD9iJUykQvCpAvmUkJ kQrnqFxLEGjkSQW10Ej00AoXzYcHxYcejXEn9ckgy2DZNlCJTG0taFpOkjPj9+3hIIM1KhViGft++ kIBGiCRLsCUH/slIeToK43R/4UPIysP1mQV86x6YxqGeA2dViTjeIQyO22P2iLN7BZri+ChmlwTQk 5YgTnhvfCGS6L+3zrlji0FrTP88SoA1tEnUtuza0HE/IcG1OdLGD+ucQnhv6yE7ln3Cm+VT02WQ8Q 7h6HNHFpmVYCpNHST60Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyKlt-008Jlo-2z; Wed, 01 Nov 2023 23:37:13 +0000 Received: from mail-bn8nam04on20610.outbound.protection.outlook.com ([2a01:111:f400:7e8d::610] helo=NAM04-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKlj-008JV7-2C for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:08 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SBXxmUD2h6ptv+qZMCsu4+rbkoLQrdWITWK5J3P8IuCipA1GT+wfkkneQ0TEdhDttWgBO2nOXv9NdtQ3nwDglPUMHTNzOuvkNWLOrNnDuXywjaWW7f8HYFdhFGmXtBiVjmKpT1StMpFCwyOYvX+X4cab3QERkXcuT2nAM6DWCOiDLsufyE3rPjPuGjIULMq4OOfRzcUp2pN4319fkfNsqngrUp2ItoeMrkLGMDkDaHqr1n6F30kzDv9baKpHfm7u5suVHQoQbnvEHaGH0N9C6DhReFMPHIecz3MQI7VMDU5AcDuowVYeo5l2LV9NldM2OcQEOfNnxDFK+XlKojRtXA== 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=TKjlQdEbtFpmWK/cm09Lx7F9KngqmRiMtDiqJUitsfE=; b=OgLqYyk551x706af/MebaBLj66HKpNb8UjrCmU7b4fYPkRiohC1XdwNpcnHH34IxsB88PeLIl8PKlFeQayDKMjCxsg1IBciinrR+e6M7KZIkUQgXKRxIgmazItzWjWyeVvQluMsB2o/mnmquWvXNLBXopmeckno1k83kMP9dHE4UWqEgqBM8WsCsyKyRHipqjesHKf5cqawOcQ58EQcfCguif1I7qfPI8zIOjZHG671T7li0myt/u0iH64sWRxTWYEhFozSpt8EepJPzP+U8QGYianFGLF+ydcZ1OuUga7ra0TDZCeRSvIlIqfAP71sZq63BHVWiwYVQAxH6f8vPOw== 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=TKjlQdEbtFpmWK/cm09Lx7F9KngqmRiMtDiqJUitsfE=; b=UzzWdB4pObyqb1MsZpCHK8cb91gh6NodwRoY3ImzT/rJzAREqkorqi+rrVenVvRvqj0sni36G5kRwGJAroASM2hAVdHVoOX/SQzfQ7aeEhVFUL0u2mfZos3upkDUMJyUJq/PQW8Ldy/WZ/A0Ud6yGgBLOP2CyU8Em67gkl7ghJ1NHGrghc6pcqeqgmFHs1KJhEo8oYrZref0snMb7XdHsRzmwbx7LeMJo843xbu/ZDP9CPnbv+beZIBqMVKscdA8KCqw3Us6N+/7Zh4w/qw1D2zhgIWP6OgGdN6x4Ub5rCu4TTdmjzFVhbYDx+xYbglHraRq029QpyI94KhCn5eyfA== 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:49 +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:49 +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 01/27] iommu/arm-smmu-v3: Check that the RID domain is S1 in SVA Date: Wed, 1 Nov 2023 20:36:19 -0300 Message-ID: <1-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: MN2PR02CA0030.namprd02.prod.outlook.com (2603:10b6:208:fc::43) 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: 6f9ebfce-0b2f-434a-55b5-08dbdb3369e3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: OgicdbPfSETUI0sskXKzc0rb7lerWXswQ4hEHZO0D507GXKofcxZMJYB4wt8Ugp6L+VVpIPVDohrmnWUxflYdGX1B1vGS4fsWOH7XY8QyBvUnWC+vPzbjlo0sToMSRgt4t6xZlL+iBCsjNzBVU+rPgWGkUpfRCHfYrKqF7mkUM65yR4dMdmh9ga7SVXRy1xJhyyX1Xj3JOccxPYD2kDyS910ESex0JKEUWLEgyt9YIwkewl692UfsfdtGqp+efDrM+EUnPF7N3Gb+lZ136w8Tm07wQJpplchl93wXdrox0WveLKtCHFxDZnfcEKiBeT5NQgcGUi+eZrfrQ5MYmxwM/mZLdMpFSBmmxw3xI8zvbz7U3hjfPWjQ2IigSZsK7jAovNqS+23q3QpBI/x6vwX5fD1bImEvFDZDKM76LmicYUkrOn/16Fn3YvKS5XwQEjwDqDMreh9SoW5Ua2sAqoqRhvl3vb5L28I/6M6H+B7c9gRH/8KVW3pcwJNvi+Q8v7QYU23PfTQg3obe+UnIElYsIQfYS6VPCDhfSMLWwCXHHqpiMg5dLKbe93XwbNqSenPuaccmVYJ+xRKpL54h/0Nkxpo22SWD4rVocArwiqe7BU= 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)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: qIzzX0nVQrnuAJzndr1QLwP6MfX0qkFNun+xCsTtUQH2qH4h7XhZSaUCFEcTSo9x+K7vi14wqf0rt6BtjpqDYoxW/b5XDsfswguNgBKSscj/qP93AnW3VJybYx/L3hg+BwyHc+VyyCDLRZTHA1X1swhg4N1lY7X2Fop9AHoPkGMbc/oi3lugmxcEckxy4vOl4fvg5+JKVPX4wVIBHNH6+xPTYf5yvcxQoUoJxbM+zB/e2eE1aE/Qe+h29Q2i7VzGAG00yjtgEBmoTMUxXcTZc8IwBNX8Ydd5csxhc6c3zVWopeqwJKPdKgAXv+JU5opIIy0rWOrXHzGSYg1yHaMfLdY2lPvEpVFTEwQ4QE29Rf7Rg2GDajZqHcggzBL3k3m+/L8qUF9CBY+R1/Qb6UosM4YfXTSRTu6ng0Hzv7yfq1ua/yrAd5rTkRMBf1hPA8yR9OkR/llfzTWaCtMEDeBOk8DuMsEmtSMmvsWesAEX0J132wph3atKELHyLd0/QPkBPzscLtzP5Gq86/ECpCbORpX/8YeDNrRcYl3Hr7ueLWgcNNGOXNVf0dpdrOYNL9igNJ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434a-55b5-08dbdb3369e3 X-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.0402 (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: TbhaoeqwhBM4hJZFwBphq7JtkGaUG5QEkfznV1mFnUgUljpScdSc/Jf2XwPMA68x X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5338 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163703_729054_8EBB032D X-CRM114-Status: GOOD ( 12.75 ) 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 code only works if the RID domain is a S1 domain and has already installed the cdtable. Add a to_smmu_domain_safe() which does a robust conversion from struct iommu_domain to the struct arm_smmu_domain. Signed-off-by: Jason Gunthorpe Reviewed-by: Nicolin Chen --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 7 +++++-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 7 +++---- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 6 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 353248ab18e76d..bd0566381c58a9 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 @@ -379,8 +379,11 @@ static int __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm) int ret; struct arm_smmu_bond *bond; struct arm_smmu_master *master = dev_iommu_priv_get(dev); - struct iommu_domain *domain = iommu_get_domain_for_dev(dev); - struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); + struct arm_smmu_domain *smmu_domain = + to_smmu_domain_safe(iommu_get_domain_for_dev(dev)); + + if (!smmu_domain || smmu_domain->stage != ARM_SMMU_DOMAIN_S1) + return -ENODEV; if (!master || !master->sva_enabled) return -ENODEV; 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 5667521bd18091..43f5531795f0b0 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2486,14 +2486,13 @@ static void arm_smmu_disable_pasid(struct arm_smmu_master *master) static void arm_smmu_detach_dev(struct arm_smmu_master *master) { - struct iommu_domain *domain = iommu_get_domain_for_dev(master->dev); - struct arm_smmu_domain *smmu_domain; + struct arm_smmu_domain *smmu_domain = + to_smmu_domain_safe(iommu_get_domain_for_dev(master->dev)); unsigned long flags; - if (!domain || !(domain->type & __IOMMU_DOMAIN_PAGING)) + if (!smmu_domain) return; - smmu_domain = to_smmu_domain(domain); arm_smmu_disable_ats(master, smmu_domain); spin_lock_irqsave(&smmu_domain->devices_lock, flags); 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 154808f96718df..6f62184eaa2434 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -740,6 +740,20 @@ static inline struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom) return container_of(dom, struct arm_smmu_domain, domain); } +/* + * Check that the domain type has an arm_smmu_domain struct. The global static + * IDENTITY and BLOCKED domains do not. + */ +static inline struct arm_smmu_domain * +to_smmu_domain_safe(struct iommu_domain *domain) +{ + if (!domain) + return NULL; + if (domain->type & __IOMMU_DOMAIN_PAGING) + return to_smmu_domain(domain); + return NULL; +} + extern struct xarray arm_smmu_asid_xa; extern struct mutex arm_smmu_asid_lock; extern struct arm_smmu_ctx_desc quiet_cd;