From patchwork Wed Mar 27 18:08:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13607245 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 E4BFDC47DD9 for ; Wed, 27 Mar 2024 18:08:57 +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=YsDJl9yvHticO3ryH7+NPY/yxLJHdb9sXmZpCi3Wsrs=; b=2AA9Al/EccUuia bpowHv9xaghqrFU4T4kaKWO5yYpbeby1KZoavasrqA2ZjepWFkpy+Smg463x7ml2qiO+GlYASzKOW NsdoJE9h96WjUVPmGHN7FJUtjuUXdXM+wPmeYa+lxTL1CmmF6/EsKEGsLWV1MlcUo9TomllqkHT9n 1Xi6rq2SS6nB81W6SJRJ/0hKZ9xsYZ7PS9VIbCMXl8Dzqv0BGfOewzJhWB4vb9Z07yj1wM+qJIWso NdEDDal9VPme0aIFUp/QYfc/vcE9xsd2koUr9D8vgq+0HgzCRlSVUcY68jcCajp4BS7AdrRnmnA1G VxYSDN4SDAOddKyxqvCg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rpXhb-0000000ATjS-1HkO; Wed, 27 Mar 2024 18:08:43 +0000 Received: from mail-dm6nam10on20604.outbound.protection.outlook.com ([2a01:111:f400:7e88::604] helo=NAM10-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rpXhS-0000000ATY9-0UAX for linux-arm-kernel@lists.infradead.org; Wed, 27 Mar 2024 18:08:35 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=l4QQZBEUiIUJkHOYZ52XIEm9WkdAqGPwQcNZ2mLgJ/XxyRWiO7kQf/6AnC9HQrrOPnZBynp/uVZu+58y+Tt0n7h8w2YWL5mEN6E5WgpwaBU2c16d9Ph5mhwgjvireoCcLTtF+L/66U0s78JF38y1djwqo6BYxdSorQodIgjWPStRF9HIeuzp/gQTJJZxKAG1MFwS22MtmEnDM0/Hbr5r3NaBsm2CGlIoDPOrP1FYNlZN+gZtneCl96BKEOuMuFnioZYJ8WlL2CJO/9nn6YL5rgBtdYe4F7khuj7RPDaeDvuX4Y+usbjS0J2TK5hPz7bf+SOqjqWima0dP5OL6yLFSw== 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=w7FRZs9o5O65w7A5fHtmKCVArx1qpU0Mag+3cPgbb/k=; b=fkjOrJ6BPU2g+v14gHqlrd6ylqmCS+VV36q80YAb5OtLZNzqBaJFQQ2MqWPDW0MThT4MxCehpHarw/HwzrqWQPwiCiyqpkDRNIa4wDk5TsMGXj16sP2UX97ikyyBgy9YKzgGKq3QN4T5dXoBJQWlQlvP2KiANvV9YAOGXfTINUS1CEQoj56IiXR34jNa90tefMXVnBEobNQ+Q7olmm9PmmpebWObV8bXov6QtX6JIozIR5pFHY4ATJctTi85NggJrvoxJqnm7JO6jD/wWM0fLSWf1DSeuca0co8FTijrs7U+2g8iVFN2uGdm55MuahBr8sVlTLinKkt1crPhlycRxQ== 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=w7FRZs9o5O65w7A5fHtmKCVArx1qpU0Mag+3cPgbb/k=; b=AoRyAWdL0Q6OjX3uUPCehh3EtwfRKTaXDbehCTOTDIDQUR8Q+QFaEx3Xk10iZ8V7jwNx+WuHNERVykLb4aRwsB9khm+fd34lLrcR0kEg1Lz2SPDuq2YyO9fwlVZoXHh3V+/Xa4grXExt41yoYw1DMOnLdv0xWioLscRdZpCng8WUaS27UOs9D1F8kP+t5JPtO75qKs+ks1lQZWzjcaqae7OPHdBDA4E2XnHsGd2oeTnQJ786gLrLhs11kz9UW79jFccqGjec8QyM/xwlD9IkUIxOalfHAY8TPsa4wiOWW8merDbQB1/1GUESLMqyDREUrvzADbOZdoHLI53tAVFXZQ== 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 CH0PR12MB8487.namprd12.prod.outlook.com (2603:10b6:610:18c::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.32; Wed, 27 Mar 2024 18:08:19 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::6aec:dbca:a593:a222]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::6aec:dbca:a593:a222%5]) with mapi id 15.20.7409.031; Wed, 27 Mar 2024 18:08:19 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Lu Baolu , Eric Auger , Jean-Philippe Brucker , Joerg Roedel , Kevin Tian , kernel test robot , Moritz Fischer , Moritz Fischer , Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Shameer Kolothum , Mostafa Saleh , Tony Zhu , Yi Liu , Zhangfei Gao Subject: [PATCH v6 22/29] iommu: Add ops->domain_alloc_sva() Date: Wed, 27 Mar 2024 15:08:08 -0300 Message-ID: <22-v6-228e7adf25eb+4155-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v6-228e7adf25eb+4155-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR20CA0002.namprd20.prod.outlook.com (2603:10b6:208:e8::15) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|CH0PR12MB8487:EE_ X-MS-Office365-Filtering-Correlation-Id: 0d41a608-348b-40aa-e0d3-08dc4e88e0a6 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 2AKnZS+74mTCOI4SpDIpJ7EJj0CejOqX0+nTggYOMzqRni/et+OwGYrFneeGuWCcE+hQ2XRzDJ3xBYeUCArfEke8AXexWKKdswNGW/kw6K6slT70lnXYO5lbwQSz8fWyhWWEnUPMmE7OLc3KZ2duGQkTj/FPP/38j/PCJ4OjYBo1nlRsuo0eBZu8JP29zkVjixbTnWVZjfpsQO4tEdoQ4phd6A5f9HKVRGaZNFocggrrXH1FdFJJgIp4I3hywOMbjJ582yOvotBlaf6F6+fyrkDmT3OcCSsSkKkWXFLSJqIjp+XJDN6HYeHoyPeXnGd+b0oBftoRmwLRVuteWmcPNI3RRyJL0Ze7QDUdHtdsVgmijx1AbDxD24ap4pdHPcgkwZBxXWfT2EBQJJZP+/ddtNAhIXXXDOemmDD+iwikq+BictmDEnEfQ5wXyjzAszvke4NCByzXnLNsP+ZhSALpOwfSOJqZPPBy8hOxCERU9J2GMGrgFpgm4AClGQ+VYiDCHdmVoPapqbw/wp0N0NuJz8YXmFMeOAr8KeYaK/VAxwrisgA2F/CWu6nS0nGjxH3QPIuWO9Zm0kglCKKH3sNqH4cOGsHhQZW283KOMRyxSiY1gZsdtn3HqnghDqjjUOmcGQmwJrFf8dv8BrUc1dvaQffX5KO//wYAsWUDeQpNh3g= 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)(376005)(366007)(1800799015)(7416005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: EBCsPcwmwoAiPuPDF59TBGdUyeLg5cAnEkU1a3hLZXNJIAdOgGFvaT6wfADQFBPW/6CEHwd9X/6BZxWvXLOTZdqVhLB63ZkDC6ZFytiEIlGWZ8gcJshw02jJBbVCIsgheqMTtKDb/8LhYCl/L1TDFkwwbYqRjCMzv8Re+thMkeolbinXS7oVNv8/PKXxx9lls/oryZuz+bbV1Nj1S4RhKjFxtDEfGGb5l4JT/1TZPt6ZlP1RFLlY6A5lFd/lCMkbc21E5L7JEtNXCpiWCxXfJjVZnMmpQz9EMZ8VuyTunqZoX9Zk83oFH0G+ofUYrVlVFmgAjJS/4jq48HUc9Z6DwlxlGGVgeS4XTXvHe6E+boFEXUKA+2nN4MLS7iO9/2cVkov2G2Qyd8B7pmbOtfevqxvyrcqozgPTaCitQgsQAAQIbiSGajHWVz0c8dxWVxPBnnD+HDwXtQX0Wy3jkKDg4VoK1u2umkcyVfcm3t5HbGmGX2qb188HVDNhCP4jWgVdD6MpbDoPNnSHBdH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LLv3JDLRlDL6gwz X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0d41a608-348b-40aa-e0d3-08dc4e88e0a6 X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Mar 2024 18:08:17.3534 (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: cfM/cUhJRjO/RLvZNr+bkEdVGJwVrBYd5npSkvCb8nv1NYhUDcl640BAwjQeoH0I X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB8487 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240327_110834_222415_9C75505A X-CRM114-Status: GOOD ( 20.74 ) 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. Tested-by: Nicolin Chen Tested-by: Shameer Kolothum Reviewed-by: Michael Shavit Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 11 +++++++---- 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 | 16 +++++++++++----- include/linux/iommu.h | 3 +++ 5 files changed, 27 insertions(+), 11 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 3e7aad0960bfd2..e337e40ac5de31 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 @@ -659,17 +659,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; - if (type != IOMMU_DOMAIN_SVA) - return ERR_PTR(-EOPNOTSUPP); - smmu_domain = arm_smmu_domain_alloc(); if (IS_ERR(smmu_domain)) return ERR_CAST(smmu_domain); + + 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 3d9109ad60c19c..7b001afda17aa8 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -3302,8 +3302,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 9db84d5940466a..107a39f1dfe869 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -791,7 +791,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 */ @@ -837,5 +838,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 640acc804e8cdc..18a35e798b729c 100644 --- a/drivers/iommu/iommu-sva.c +++ b/drivers/iommu/iommu-sva.c @@ -108,8 +108,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_free_handle; } @@ -283,9 +283,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 2e925b5eba534c..8aabe83af8f266 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -518,6 +518,7 @@ static inline int __iommu_copy_struct_from_user_array( * 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 @@ -558,6 +559,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);