From patchwork Wed Dec 6 17:28:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13482146 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 DA068C4167B for ; Wed, 6 Dec 2023 18:31:06 +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=7BpwkH2HReUWpX19lTtSy2eIBJ9f8ocJetkOMaPpcqY=; b=dt9dl9BLGvCAo+ 6sBKd8RpDZYeWaTGROIUABNRV3uOcZGjP1XG1+v1V3W3O8IPDmFgOicNCJntywWO2A0s3eVZSPz4j JsB3NX5ZTHlqYUleebKoq+OyRpyPu6w/hCioTWBOUdws43Mfp7bYsbHGIaSsEmWpczwJptGilDkMS /eYglMf7VUrVa4vFJ9dECCUqOonqXfZKRJm5I7U3SbulSkuj2ChBuy42Ke/o5m7tGAzMT95gQWqe4 +dhBLZQk28cUlq4TVO1xQlmHTgD9qkXDfs/wVgT4rExXf2EXaGhm687wh5l41yy3dUisXoj4xl4ke CV7+TTzqUeNTap11Cxhg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rAwfP-00B2B7-2m; Wed, 06 Dec 2023 18:30:39 +0000 Received: from mail-mw2nam12on20609.outbound.protection.outlook.com ([2a01:111:f400:fe5a::609] helo=NAM12-MW2-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rAvhh-00AtIF-3B for linux-arm-kernel@lists.infradead.org; Wed, 06 Dec 2023 17:29:00 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TcuKrJ+eHCsnatuGwtUBt9ag7Nz9I5A0lUJvWSrzT5oe8cb5VV42nCJYggzD1x0sN7K5tQPd7ncI71OUPrMKVWv8axGV79pGbCJ3APcb03zipd5RVGtzZN55Xs4EhTBK91QrKgcKiekaps95s7F2Deb7u4rt1jW4HX94/RuqqQTe+m0u5uyjtyNOolmR9Y/QSfAT+bwl39yz5PQVyuMt2OX1emb6mcppQCLGPS3kclVMZoodmES7TNTMflL3wWHBLQrCDhETpzNKdFum8bgHaaO8xRyqtv0X4v5r3vIPe4Npx3YvP3S2S6MA4koKS2qs2p2JL8gcm/xeAa3J6jzybg== 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=1OTGz3qIaECpLc7wVaNfV9wng7AosVZuvH9ccjI3ex4=; b=DLitG49i0X+9/3tCiJCnjNIWJ/ASzxjytoPMx3GRu00uykRTohVvnJPxi68QaXrWaaijYSw11vYfAh90nTKz3IHWEFU/4clK7jiQeYQHTVlpuagHpIJmaV08ywiUozsma9kiQavNyfxJ+KlW8f3sphzTT8d1YNwvu8ia93Q9YYGxnN2TDPbj4dMIaJRLal8P8eNcoBqldLy4n5DJ6ZoK3V8BuId3IkPeFNOqDZx08r7kxPXFAAwa4JuRvt5qtovz/vbjPmc768BNbBbsmp0BxQCB1jCAt1Q9B89Sz4H6pYkCk0VAOOD5q7urTKU2A/T8x0oEJi8qBh6TnQj+0SRcVg== 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=1OTGz3qIaECpLc7wVaNfV9wng7AosVZuvH9ccjI3ex4=; b=U4FtBIR0y7nAQmixxNthsTtbsru7L97GUD7Qqy0GAX8vd4QYAI+76HVGy6syOpLcOOJnHEFTAQKtXSmhuRkmm1awHOvZGm5C5TbDOAbVJT838brWSb91YI7BFgRfCzz6C+q9DnEBRn6Q9XggZomfazXz7KCQX/HvlPdbV6BG9MYv+z1ONznHrMX9IuuEOp30QCjKyCR+Tp4gtftIXTPeHWhQsTXYQU3voVhXgdpWliq6EwQ69maq/tdGY0UW2+j2SNlfFMgmD3llunEPULvRaD7bXEV8HImXlB9LmHwBjgVHxz5lAoa1lH4+aXd7cRpj2dmBAb599vXu2jJzgYbcDA== 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 IA1PR12MB8360.namprd12.prod.outlook.com (2603:10b6:208:3d8::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7046.34; Wed, 6 Dec 2023 17:28:43 +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; Wed, 6 Dec 2023 17:28:43 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Eric Auger , Jean-Philippe Brucker , Moritz Fischer , Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Shameerali Kolothum Thodi Subject: [PATCH v3 20/27] iommu: Add ops->domain_alloc_sva() Date: Wed, 6 Dec 2023 13:28:26 -0400 Message-ID: <20-v3-9083a9368a5c+23fb-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v3-9083a9368a5c+23fb-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: BL1P223CA0017.NAMP223.PROD.OUTLOOK.COM (2603:10b6:208:2c4::22) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA1PR12MB8360:EE_ X-MS-Office365-Filtering-Correlation-Id: 7bc88469-475e-4831-66b5-08dbf680c6ed X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: N+K/KGbpmAfU+DZA4YRcMmHkSSrZvtqy/SCM2GnPnv/hzsKkVL0ioqaVCNMP6a7O/Xoo6TckxA0Zz6A5vgJr4laT+ktyp7ZOZj8qvOPBAJ8CSro8eS0WnM5oVEJOJJ14ewTAzimRdoaUg+YNG2kvls2zVc25gUrHHCI9JB10p3zMSJxMYexxZeg9hr93KCAOC06ntVGRu8su7g9HYmgeCsP4XAya/2CuIkNbZAZq/3MR5MIsYkRC1QqXC1RSYDBkEYp72qnp5IXkHDJRfYA4Pos0cXnwAQCoOQTfciKeIEYVLtO42fT+IdB6z2rSkdDhsGY7Tu4xe63VY6iGnkNY+ZeEgag2iUqyS75ZOEAidIOXEOXpf5qCXA9H5Y98bguJRIZeryLxt2l1V/85avFoIc+S/XvfyMqYado3+IEJS8PSw8XA8zbkNfzv0qnNRATKr7nZkSooE/3EbdbShOnAEzdmcy7lYcdbscw2BulgbTQOKQspI5ekmrCxszgxyvmf/bRlLR85ptSkXynZ22SclJnWlR9sI1ZdavE4v+AEODRZQlLqC0stzbCCTILfUrA8 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)(346002)(376002)(136003)(366004)(396003)(230922051799003)(186009)(64100799003)(1800799012)(451199024)(6666004)(6486002)(478600001)(38100700002)(83380400001)(66946007)(6512007)(2616005)(66476007)(6506007)(8676002)(2906002)(4326008)(54906003)(7416002)(5660300002)(86362001)(36756003)(41300700001)(66556008)(8936002)(316002)(110136005)(26005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: dYOSobRA6pt+aSr70d9GYOXQ0RHiBV6vv4xF7uQjdCRENODCtr+HDnLzs0uzPcF1Kko6+TMa8+ibSO2o8f3082CCx2qdpU9xHWnKN2NoDUTIWptmtDh7Gyd4a0bOmDd+Ob7gmZnBitkR+h3RCNgxLdvMFidBzVfDdsJ1ct096uRvn2hnUOTcYU7yRNCmzYneyluAfUe05WqDjko38vx+mVQG2DGifM40bpkCgGPSvG91bXME2L+5RqTRe5AO0sLjhObVNjj3d9fm4OxtMXY8BUB0BWNxYbUVvE8aONd4L4p/rMTZP5aC3VPOTxDlaj3d2lcXVjaD4WcqbqHsOFox0PuIHXfW1wjkAR6aHoK6a5l6qh8o5y5retmkW4puGYxZi022fIPkQi03gDck71VPxI4cQfUhfYA3udHE2QgDxvXv9dlob7CkRshcFLFMBZXzEzuCTeSRYYicv0SgPaPKW+l/zK7LCxvbXHFHXk0tKBlwQB9AlyxnLLuESQQefv8fVJ1BZdmagtOFsRO+9UFg5p0hGBl5mYUoVW1JE8507KRojj8rgx9BHVUhVztWc76rwIQS0tF/kG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475e-4831-66b5-08dbf680c6ed X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Dec 2023 17:28:35.7954 (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: PMMZ4Fvyt2vet3wcb2SuB8ImOt96ZdYAt0uTb7juI6t8gTcZSL8hElHXkNJQytcL X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB8360 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231206_092858_028089_FEA85012 X-CRM114-Status: GOOD ( 20.33 ) 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 Make a new op that receives the device and the mm_struct that the SVA domain should be created for. Unlike domain_alloc_paging() the dev argument is never NULL here. This allows drivers to fully initialize the SVA domain and allocate the mmu_notifier during allocation. It allows the notifier lifetime to follow the lifetime of the iommu_domain. Since we have only one call site, upgrade the new op to return ERR_PTR instead of NULL. Change SMMUv3 to use the new op. Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 12 +++++++++--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 2 +- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 6 +++++- drivers/iommu/iommu-sva.c | 4 ++-- drivers/iommu/iommu.c | 12 +++++++++--- include/linux/iommu.h | 3 +++ 6 files changed, 29 insertions(+), 10 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 5894785aa901e8..991daffbee31aa 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 @@ -638,7 +638,7 @@ static int arm_smmu_sva_set_dev_pasid(struct iommu_domain *domain, static void arm_smmu_sva_domain_free(struct iommu_domain *domain) { - kfree(domain); + kfree(to_smmu_domain(domain)); } static const struct iommu_domain_ops arm_smmu_sva_domain_ops = { @@ -646,14 +646,20 @@ static const struct iommu_domain_ops arm_smmu_sva_domain_ops = { .free = arm_smmu_sva_domain_free }; -struct iommu_domain *arm_smmu_sva_domain_alloc(unsigned type) +struct iommu_domain *arm_smmu_sva_domain_alloc(struct device *dev, + struct mm_struct *mm) { + struct arm_smmu_master *master = dev_iommu_priv_get(dev); + struct arm_smmu_device *smmu = master->smmu; struct arm_smmu_domain *smmu_domain; smmu_domain = arm_smmu_domain_alloc(); if (!smmu_domain) - return NULL; + return ERR_PTR(-ENOMEM); + + smmu_domain->domain.type = IOMMU_DOMAIN_SVA; smmu_domain->domain.ops = &arm_smmu_sva_domain_ops; + smmu_domain->smmu = smmu; return &smmu_domain->domain; } 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 10dc7edd40e1b1..d09505acd4f8ce 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -3229,8 +3229,8 @@ static struct iommu_ops arm_smmu_ops = { .identity_domain = &arm_smmu_identity_domain, .blocked_domain = &arm_smmu_blocked_domain, .capable = arm_smmu_capable, - .domain_alloc = arm_smmu_sva_domain_alloc, .domain_alloc_paging = arm_smmu_domain_alloc_paging, + .domain_alloc_sva = arm_smmu_sva_domain_alloc, .probe_device = arm_smmu_probe_device, .release_device = arm_smmu_release_device, .device_group = arm_smmu_device_group, 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 c1181e456b7d5f..48871c8ee8c88c 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -808,7 +808,8 @@ int arm_smmu_master_enable_sva(struct arm_smmu_master *master); int arm_smmu_master_disable_sva(struct arm_smmu_master *master); bool arm_smmu_master_iopf_supported(struct arm_smmu_master *master); void arm_smmu_sva_notifier_synchronize(void); -struct iommu_domain *arm_smmu_sva_domain_alloc(unsigned int type); +struct iommu_domain *arm_smmu_sva_domain_alloc(struct device *dev, + struct mm_struct *mm); void arm_smmu_sva_remove_dev_pasid(struct iommu_domain *domain, struct device *dev, ioasid_t id); #else /* CONFIG_ARM_SMMU_V3_SVA */ @@ -854,5 +855,8 @@ static inline void arm_smmu_sva_remove_dev_pasid(struct iommu_domain *domain, ioasid_t id) { } + +#define arm_smmu_sva_domain_alloc NULL + #endif /* CONFIG_ARM_SMMU_V3_SVA */ #endif /* _ARM_SMMU_V3_H */ diff --git a/drivers/iommu/iommu-sva.c b/drivers/iommu/iommu-sva.c index b78671a8a9143f..a52b206793b420 100644 --- a/drivers/iommu/iommu-sva.c +++ b/drivers/iommu/iommu-sva.c @@ -87,8 +87,8 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm /* Allocate a new domain and set it on device pasid. */ domain = iommu_sva_domain_alloc(dev, mm); - if (!domain) { - ret = -ENOMEM; + if (IS_ERR(domain)) { + ret = PTR_ERR(domain); goto out_unlock; } diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index f17a1113f3d6a3..899d73062f6e67 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -3543,9 +3543,15 @@ struct iommu_domain *iommu_sva_domain_alloc(struct device *dev, const struct iommu_ops *ops = dev_iommu_ops(dev); struct iommu_domain *domain; - domain = ops->domain_alloc(IOMMU_DOMAIN_SVA); - if (!domain) - return NULL; + if (ops->domain_alloc_sva) { + domain = ops->domain_alloc_sva(dev, mm); + if (IS_ERR(domain)) + return domain; + } else { + domain = ops->domain_alloc(IOMMU_DOMAIN_SVA); + if (!domain) + return ERR_PTR(-ENOMEM); + } domain->type = IOMMU_DOMAIN_SVA; mmgrab(mm); diff --git a/include/linux/iommu.h b/include/linux/iommu.h index ec289c1016f5f2..7bd8c898e67559 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -346,6 +346,7 @@ static inline int __iommu_copy_struct_from_user( * Upon failure, ERR_PTR must be returned. * @domain_alloc_paging: Allocate an iommu_domain that can be used for * UNMANAGED, DMA, and DMA_FQ domain types. + * @domain_alloc_sva: Allocate an iommu_domain for Shared Virtual Addressing. * @probe_device: Add device to iommu driver handling * @release_device: Remove device from iommu driver handling * @probe_finalize: Do final setup work after the device is added to an IOMMU @@ -386,6 +387,8 @@ struct iommu_ops { struct device *dev, u32 flags, struct iommu_domain *parent, const struct iommu_user_data *user_data); struct iommu_domain *(*domain_alloc_paging)(struct device *dev); + struct iommu_domain *(*domain_alloc_sva)(struct device *dev, + struct mm_struct *mm); struct iommu_device *(*probe_device)(struct device *dev); void (*release_device)(struct device *dev);