From patchwork Wed Mar 27 18:07:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13607328 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 65B3ECD1288 for ; Wed, 27 Mar 2024 19:18:40 +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=g3SvHxftZ4th35QWUcggtih68fmXLRO+InJg0Fr6cVk=; b=f+PbzGiTwzLWbW BMOvmYBqsVjyR1+oNmEBwA08q3DqeyxhUeynQK4iRHn6Yf280d6yKMydelYGAjaBLqKiT4ngQ8tr6 WaqXpH2XHyOrEWi3jffbkrk6xtUEEKd+cGEXjrm5pEOGmrwHABwZSOQsvgWO/tWN4L1jpSFv8ENjN jOKImXqvbwWiA03+SMVw44o/AiPmO1r/es91tDIhfQEunG4ac/kGcceJH/C423GV0CG7QwwJqkFay 1jSmzNt7G/glEyRcblqXQCjbCyKDPVSgVi6ZIxPb3PPRrLgCqPWFYzU3CxbbTMQuwCOyODjQUn1SQ 50n7remDFadJ0YTvAzbA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rpYn5-0000000AkyF-06j0; Wed, 27 Mar 2024 19:18:27 +0000 Received: from mail-dm6nam10on20611.outbound.protection.outlook.com ([2a01:111:f400:7e88::611] helo=NAM10-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rpXhd-0000000ATdz-1kzJ for linux-arm-kernel@lists.infradead.org; Wed, 27 Mar 2024 18:08:47 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gc5vBK2ZEdNOWa65gUnUI/PzUf5NGwE5N/4dCD8WUhryZ81+fjhCeJCjz9qidNNymBEhq1bqQBBffxIGJJn/BqLRjOnAxp6gWwUEd/TIPqhy5KBJOutWWHrBVlzbuXWgWUR0smXHF6CJzIGC3i2bUZ4NHJPUDCLlDPG7au5dkz3/NFDjUHeJyfBF1/sJ8TaFjYMxfGkpvB2DIk6qtyYj4bEFM5bBYJW3G6YQDthm5qo+Tspnm1q85sQ8pNNIuPE2a6WhaprW1Q8WUCsXcwSUC6BZd4y4O7vJ8KmnHk1moUNj55CPNddf8GTQrc5WXlHOEuHYnYWzB+xfkoUA9g3vtA== 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=YNsVxxAr5oOUMzPY6oB1kMo+IdzMqLmUtep2e7LaAM4=; b=KTxl+AX+tGzuOWCpbEHgxnD64a8eoaQHU5AS2K+53uBZFY9NRoI26FDttRN96L+Nj/OhRz4fPUwo4Vbphn8yVV+SmdAqGuWsdebZWU+D3vA0nxTJ8BYqr51GeugSgiKMeusiURiAEGAah6mbxPfFzd5CEaXLu9nHfK+vqSQSnGSGFM0UBZpP+Uc1FmjQyi9T5fU2IkYtYvquvg+OntZW8pa/WBnsNIMvg5r17dR2KONKHwt7stK2YkMD96knvBNu2rsvqZdcYPNc7VzZqMOMsOzkP+OxhbxrkNnmrxLBPbhBM31Ptd87aExN/pq4TfQC0A6DZfwG9UaoJWeM9MuLXQ== 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=YNsVxxAr5oOUMzPY6oB1kMo+IdzMqLmUtep2e7LaAM4=; b=J7pxMqNCmaXJNYaMZIG8exLchX3VY3+Xfd1C4tKJaEuMdHw0YCSOGU9MPkWT6z4hHrT/gQzFgD9YfqQcu6GqcGy5Zt83i/lbbxSZCvYvq7h92FoXr41ZwG8ULoYnKw2iWCdK2WrtB9PB3+/BmEcULS+2T5PFdNV0ad0ANciw3MQ9Jg3jgna9fErdbbOJjCL5vXBYbvn/S3MknsjvdwzoWV/NSXqIovk96kdDfxegWqFaa5jyEKgQRtOXx+g0T3N5Zh1iG/cESEQ4lmF9aFpWmiUtcapo9hVcLPJDr27SdjB2A6/TdA5m9YgkoUpBCNop2guK91xN3jnNx8XNFYuIbA== 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 IA1PR12MB6044.namprd12.prod.outlook.com (2603:10b6:208:3d4::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7409.33; Wed, 27 Mar 2024 18:08:33 +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:33 +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 01/29] iommu: Validate the PASID in iommu_attach_device_pasid() Date: Wed, 27 Mar 2024 15:07:47 -0300 Message-ID: <1-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: MN2PR20CA0020.namprd20.prod.outlook.com (2603:10b6:208:e8::33) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|IA1PR12MB6044:EE_ X-MS-Office365-Filtering-Correlation-Id: a050baea-c1ec-441c-3f90-08dc4e88e22c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: vfyaMpaGsUzbA788ubvGJcwVQVjW9zAU4idRKpqoiGnpS85nU2pYInuxEuQBrTQk8o8LqsahxIHnVDQY+Q+MirIRbILhGBFJYtcXRNDGajNHH5F+n11vhzp5cCfwGoRpBerhPwjY8oFD+tYaDlo5VblYz+35t3fDwz/ssAF534BM3fkK0Ia5do9dH7ZfvMGCeUiHNv1XlvRzes2utB6/V2Aw8GBPsxXYgO0z9+hW+epOmNNLUMMGOykKKRU9v6JcbDVP6A6no4RHyJpHk7Y+6zGDT0plMA3O7aLJDU+0epasnVVcj+g1/dktMmu0O885nS8sUH5FwEqgfykoiaGXuVuDXXyllroz+xpULAVBEjnr3GhUJpnb5RWWn85vOO2WEkHmtZE3tRwdi4bBBHifzZR2XLrrmLTyBr0cj2MqoPT8iTeLgjiuz8kOvNsIO97Df/P3yDfxHk4tMjqz6ub94pdlmIcvJyWzI0ntTZNlu0R/Z/BWWgzDhuPKQ+C19Ui8oCXv//92hWQJhalRwoqQyN5PJ7emebZ3YtPzhubr9rcZjgszuYGUYwdc5owG1y7F2YAMdQVhY7EHB6srq8d6lk0aRL3l2V8aZ7XXDkWD0PIp2l4nyXA2xpRNjlwBVCyV7vFuTtdgGMJy00I+EYsBlrhDbIP+WjJWrLgzPbTE2RU= 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)(7416005)(1800799015)(376005)(366007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: qDYX4rQFxMoT/quDN7WBKNAL0NZGr6y8E3ygZnP8LZc/q0cGaOYOFMqwZLwDzuR2bxRBpqEexSbTFmuQe3j5H7uGoAxJ5FU9SCefp0D77wb+DBl/p6790aPzsIUnFHMHRxHAu0IIrODNKYk0Kd4tzKVRUCOgU1U8VDrgQ9BTTP11WV2rFPtH+3idp89FMzCMo2Yi0kUoItMXcpQgAf0k+vNC2IuRbyxf2KHWgYjZ35qEm0mvXNfPoIExzNlw2xtfJfNO3grL8+jcGTUiWXGxD2SzkanzUpVbM/mZ+i8YkcJaVsX3PIDqBmKRXhW/od8TJ2DervKhvTsgSjWrVZHze43sfEVB5CU0zkHTEd5tM7Bu+1pwxKnl/FPwu7As0DEZqGFuLM5jVM0bT3a936xI31xTV5z18JIbTFOw+CCNw5EWvXmuJzqY1U5csJNyGfA5xPBbh7OADJ5NhSk2VfUQyKD7vc6vxFxjYXIhnmx/w6TaYNyEyzISMWMpmCbOVQj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ginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: a050baea-c1ec-441c-3f90-08dc4e88e22c 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:19.7766 (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: YWXi3VqY+pLmszmSYMt9jtILmsAP6+n4DBt6ozZVOI5WzaUnW00bpisOgiZeQGWp X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6044 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240327_110845_636187_21DDDC3F X-CRM114-Status: GOOD ( 13.61 ) 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 The SVA code checks that the PASID is valid for the device when assigning the PASID to the MM, but the normal PAGING related path does not check it. Devices that don't support PASID or PASID values too large for the device should not invoke the driver callback. The drivers should rely on the core code for this enforcement. Fixes: 16603704559c ("iommu: Add attach/detach_dev_pasid iommu interfaces") Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommu.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 098869007c69e5..a95a483def2d2a 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -3354,6 +3354,7 @@ int iommu_attach_device_pasid(struct iommu_domain *domain, { /* Caller must be a probed driver on dev */ struct iommu_group *group = dev->iommu_group; + struct group_device *device; void *curr; int ret; @@ -3363,10 +3364,18 @@ int iommu_attach_device_pasid(struct iommu_domain *domain, if (!group) return -ENODEV; - if (!dev_has_iommu(dev) || dev_iommu_ops(dev) != domain->owner) + if (!dev_has_iommu(dev) || dev_iommu_ops(dev) != domain->owner || + pasid == IOMMU_NO_PASID) return -EINVAL; mutex_lock(&group->mutex); + for_each_group_device(group, device) { + if (pasid >= device->dev->iommu->max_pasids) { + ret = -EINVAL; + goto out_unlock; + } + } + curr = xa_cmpxchg(&group->pasid_array, pasid, NULL, domain, GFP_KERNEL); if (curr) { ret = xa_err(curr) ? : -EBUSY;