From patchwork Tue Aug 27 16:59:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 822914 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2086.outbound.protection.outlook.com [40.107.243.86]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4AD591CF298; Tue, 27 Aug 2024 17:01:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.243.86 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778112; cv=fail; b=p2XlbDRzz/+d4Oorh+WxvvpSWo12Ij8neFDe/fjPapX79kp/NOETrkvRTJGxYxTdP+0qBkV36R8LokNSwF7AmHQ6irrFhCV/iHB3feCU1tl+LwK+Vb5/Tz2C1nH0vcsgrNv67PwcY0TqWfySW0kzdwCg26hFeq2T1WUrZjalDoU= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778112; c=relaxed/simple; bh=kRFbTaiChsxVIhRxZ7KGCox8Q6SkULwzCZgRsrFBFSo=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=tdPsHS7JcLuBnGLUAukwI5V5NeTgQC1/47lSlVr5uq8nCwIZoFO4ARBbUuVegZUekFFVEb2dDT30d3HMDjDcEUWMySZH1j8nHkGZSZd4iXNKPNsHgaABEF2OJsjnFZsnBGCJ7L8QxLFc9jRsZNIKLOHvlM/hgeyOq8z8Z1tslz8= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=f8BkJEuR; arc=fail smtp.client-ip=40.107.243.86 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="f8BkJEuR" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=G0T0QLzwQjyxn0zRpPUDeMq6QYouYQKusfo0VgvsoL+R4G05JzuPY3sMVrJCDj6aSUUDkisAlMygpCA0GzeZrWlnNg1D+gLOXnq9eHvzCDslSlLAJd5TDd6OCA6OhrMUTjDvZk3JWGKTWUh5rgfwROssLKkRarL+/uxwdBGIHFcXf5bIFQ3OA0ahL8wrKf5l/wdrZ2auQgsSzveLbj/OHri5qnMD9riMu+Jpw/8JRUxsyfLACUiVpk2KlzYYmbdldjgcHRIyn4yj+dbcmcxqupzwn3fuwWt+uHDJ8dY+5yGlAJHt7osmtNwcSRIsJijN/inwpk9CFlEoVqoFkoxj2A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=GV+bBiFmbtQmzC/TL6Ent3fVUs1+ONbtJSJX9z7T9b0=; b=C56Jj96VnzufXzKiZcMPUDJlp9okgsTyBZeENIuSZ7meSZlxmm3qZiYyYYZRl8JRYKoqAFeYM0KUb+qrfh6S9Q+DfQ7luYeHWXWUFw7qrjYrPTegCoFO16cDwtouqfWrFZ0EhIqCPpn4EF+pCBjX6QZ4GVmXGc6fUvcXIYSKOSrOHcgEA/JSeHNzpkoRtu3u3sYntgUVaOxe/Rot/OWSY8glefYPdP3Qd8q2mruTjU4jMzjUb8UMHX7t1L8DqLObXIwlz7/ri7MgeP5mGFc8izYVLGMj5HP2NBJdq67uhi8Uj7JSO2Luuov1X14X7wRJJQG3Y+UXd8R2up+vmJnHZQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.233) smtp.rcpttodomain=huawei.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=GV+bBiFmbtQmzC/TL6Ent3fVUs1+ONbtJSJX9z7T9b0=; b=f8BkJEuR2rGLEy/qnckredNPKWXMlj6kKVxZr7cmTsE10w/aEBQIcdZUTIRAUi0zvVQJZBN6tNUEnSyw58xw8+YNoisE7BkDDfPsjoGpCn25XmCridzR+/lEiCuVL4JQgZ3gCos6h0w/3a+sO8s+uyfdBG2XN2WrtOA0PJRkzG+IMjH/9vQFHKLILss4Y6nHkzoWcQj6DNJ9eEUn3ddZ/fhHjBIYT7XUrmnH25CAeTPKVgVKyBZDnOVazoiffAQeGN4RGcEclk1UbjU+QSmiw+cTcoStT3HWwVogmuFP/L/HHywzg5+8MgfIQpoFnKh4Q3H7zo1UWFADZDlqFYt04A== Received: from PH8PR21CA0001.namprd21.prod.outlook.com (2603:10b6:510:2ce::25) by DS0PR12MB8765.namprd12.prod.outlook.com (2603:10b6:8:14e::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.25; Tue, 27 Aug 2024 17:01:44 +0000 Received: from CY4PEPF0000E9D0.namprd03.prod.outlook.com (2603:10b6:510:2ce:cafe::91) by PH8PR21CA0001.outlook.office365.com (2603:10b6:510:2ce::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.16 via Frontend Transport; Tue, 27 Aug 2024 17:01:44 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.233) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.233 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.233; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.233) by CY4PEPF0000E9D0.mail.protection.outlook.com (10.167.241.135) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.13 via Frontend Transport; Tue, 27 Aug 2024 17:01:43 +0000 Received: from drhqmail202.nvidia.com (10.126.190.181) by mail.nvidia.com (10.127.129.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:32 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail202.nvidia.com (10.126.190.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:31 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Tue, 27 Aug 2024 10:01:31 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , Subject: [PATCH v2 01/19] iommufd: Reorder struct forward declarations Date: Tue, 27 Aug 2024 09:59:38 -0700 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D0:EE_|DS0PR12MB8765:EE_ X-MS-Office365-Filtering-Correlation-Id: 2755daa3-2d4c-4174-0a13-08dcc6b9ed78 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|7416014|376014|82310400026|36860700013|1800799024; X-Microsoft-Antispam-Message-Info: GXnzoiJOUABPQfrRGAhZW+SfhLuSFNLDfgdb0kfqSLb3pRXqYu40nVhorhwf0ll+7hPBD19EkArk3ayV9st45uH3QZyCJAIgw2lAkvx8zNSGoql6BnIJjb4+R5RfVhK9CRYdXwQgrTGxq26pWYbmgOH3BFAufnffdZVxQ7C7pKaayyHNbsr9VceBaQsXPRcmgnjMwP//WVdWsPcMHWtmf+eqXcamaftRpvrB11lmJvQle3ZbAH0RTKoa8RbVLdulp2yxlJbWVKpkM0XP9bQDmgYtiKAe9/da7FBU1P0derdu3j+/8/aWvaRQAx6vTkCyZjG53DzPIMABoqJCy1sCdmOVcFgj5YTIVuFX8qcYd94mofhImr68NAjDPsu9J/qxExcKK6g/Yte30tqfjVxNC9z9czuEdTLR17SqcmDZX6tJ9tIeg1VWowL8Y7+2+/5+fg5Uq7oogLTKhcODfoSMC0hry4vhaDn2YtF7O022JyGP10t0TGI6JEB5iIwA2a1lksx06j6HAWgPL2wCxoG8ofY/CsMeguL5Bx5HhRJpJFR2vzm20pbcAU1aOUmVU0kTT175LhOsRVuZRsa3lky4gGnGhaDKGq2OhgVCtTYWp6ijX8p+8+Ym5Izhf8HOaF2Re16TjNe6bF2Tzjq2BT4Pf03jnP2QWkiqQmESFHYHMsmQMUUODjw+lN23BjexUTGSOR3oSo4/Cc+vQyQQ5Eqb4a11EDJVyz2Oha4Y/3WCvPp7l1fdBhSw+iWBMrDNF3S3E0GO4c/ejm/iDTCwh8aoOIft+/U79OW/rIZBCRSiJQZyo55wv8zRqijmzZ+gPmDulrmDdoykpb10huciTHuG+d3pNusvvZ8b7rmB0RjOKTBtkShHtpBLXBNGvIXEZPeqTwadjQVqrt1mSw+YSKPtHcMLVoh4e+pR0tEghPCScrAIog9hkreeHK1FPh53/2cAk5PQGCS9BjCD+5bbGQOkrKtaht3X5esP/+QbO+5Zvop+KrrblL5GBtWG681/iyPIqTiTEp3xnO6xa2XfE4XbNdQfyqjerksb0QLd6lGJ4T1kvtzypRaHooADPfk0Noo8v8NkJZxt2mA8Gox1y6aE6SFeGRm2Bq5g/yvgurST9vMB2ggYTTb9ER6X5WPfneTrLAcvWizz6I/7pcBDPDjBp40sIKlhSsrWBur75/afK/R6JbK67lPLoV1K8nKxh5HUynpj/FAn6AriNqBwMW00Tv3L3ZhktwDFvTMi8vzLkpCjVF7eSblsEeZYUPSTPg3qlvkKnxDrBMuah0NwJZpVDB1SjH0WlUukIzLnDa6dsvgD9ONsi3m08exmBlCB2g10OUbbm5+iMJAcw0kCYrGBkFxShyK2oSfX7wsUg2C5Gx609MGwJeRFiVgOmImSB4RBxJMCiFO2xjwSNm7dLk4KAOO1Oo5vb2H7wOhjIRbjUwoMIUoWu5ctmMbKKaDVGBqK X-Forefront-Antispam-Report: CIP:216.228.118.233; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc7edge2.nvidia.com; CAT:NONE; SFS:(13230040)(7416014)(376014)(82310400026)(36860700013)(1800799024); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Aug 2024 17:01:43.4146 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2755daa3-2d4c-4174-0a13-08dcc6b9ed78 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.118.233]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D0.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8765 Reorder struct forward declarations to alphabetic order to simplify maintenance, as upcoming patches will add more to the list. No functional change intended. Signed-off-by: Nicolin Chen --- include/linux/iommufd.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h index c2f2f6b9148e..30f832a60ccb 100644 --- a/include/linux/iommufd.h +++ b/include/linux/iommufd.h @@ -11,12 +11,12 @@ #include struct device; -struct iommufd_device; -struct page; -struct iommufd_ctx; -struct iommufd_access; struct file; struct iommu_group; +struct iommufd_access; +struct iommufd_ctx; +struct iommufd_device; +struct page; struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, struct device *dev, u32 *id); From patchwork Tue Aug 27 16:59:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 823344 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2074.outbound.protection.outlook.com [40.107.94.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9F4A41CF2B3; Tue, 27 Aug 2024 17:01:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.94.74 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778113; cv=fail; b=LF6IsUrCySr++SlRqOyc07cmhFxYLVfcHeXJBluLNlJ89jadPaN3hz0ER9OT0iKgyLfVnWMbpDTPLFquqWNK8/OCzPmSOQhzQms9+z5aB/6qeZ6xg2pw2U/ZNM3BFStLvBZjrSPHvv/AnNJG3nOHwTqXqv9O/J66q0jGMhXqfy8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778113; c=relaxed/simple; bh=SZKgrD+e6LYshGsYVpasROFM9xfBX9K3MRR8qcdloic=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=dGoc4kUfdNR4UZ10aiSGVkHfbOI5p1IpVZRTZos7TaiEmkuswx9H2stDHkCub3vfYfiqnEglW3P0eXISLqxTDYBIFF2SVLwu2loBkg7XgewI59gyI1/1+2aVzjPvzjuAW5EmklQwt52Ri+UmzWrtlaGeeSqK8WpAH0uZ+mqzA4A= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=I9ZZBQ8N; arc=fail smtp.client-ip=40.107.94.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="I9ZZBQ8N" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=IRBgY93mtyBQSyt1ZzmZnsDOXqTyp23o5j/LawXuKzqlfGY91zQJL2tKgG9+kuw9Py+xwKbcgvyQX0WveDhok/Qcv8g1UvbqNA7kBCutCEF4XUNAbseTtIRjapQTniMIF+cYxjsV8qCxwdBX3IOffcBh+/i+FVvRmlSzu3we4ZJeCc2YiFmV2yoP7MKW6I2nOyKukU+vnsn80nUSJXHYfCM9apIbtPKNlQbCq+eK9CNWu3KT1cgtLklIJeypob22pFJkn6RggMBtg664fQLbAR/ZIflU+IziqsSDdyLRPHLrZ3zsj3QahZsWCOH1eSd4mOzqlcw4viiFUb+Qkvqqxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=FoUgUa5I3AASs0dMmQBg6tBhqa5jJGLYAArew/bHCgI=; b=w3FuDNCWnXBIYDDjB3eQ65sSo0NZqJFOUqjmcWo5MZByc1VuOpNdCeyHqJg1Zs9gGNNTcRzbeOu63DoNzcxq6nqE8Q79b0fYpFAMA4CI5EQc+8cNPF0IoNxOGIzH/AP8Muo1C0pceSZWUJQ4ZCilOv3BjMEFtegt6LSQUarCewfwZXWkI4DWCmKj41sY+jARuRA8G3Y85fk2tUBVKzC4z7fQA+XS/B6i8vDFJChpLo76FPm0bmylI4NqDSp6ox1Fd7CB+1n5mVPIFH29V3OO5gH/Zg7XX5CR6+Sh7gFhTWQxf5FhlYUNonCyhH9MNGGEelgyIdFIUmgaAB5XpUZ/sA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.233) smtp.rcpttodomain=huawei.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=FoUgUa5I3AASs0dMmQBg6tBhqa5jJGLYAArew/bHCgI=; b=I9ZZBQ8N2SGVXwtgLckBLHO6yIF+Uwr6hXx+18NNpArEf6kt0Qkj5JjprO5oD4IruqfxU2bhsSv8GglIBShuOTpuCG5+sZ0qkjjmovVQdPB/OZxxfOiLiun5znOtQIvMcE/fboIhspBxd3La83TLVYwBr4pW2f8uILu2zaGwEKchkmKsf+3FhtJBY3Nuraw1uEJEHmWV60KEpLC1HehN5dFZRSw3lFtGrd4Oh+Ezcq9gvTe+DGVEcd3SSmOcbzduHusKz72fjUsY3jqXdFLaI2qQ6ZGj9+3oJLPJaFGrlfQl5+T9LGtdWEJr+BpjSCI4Fd7j7iZsXUvZjgCnE1HPwQ== Received: from DS7P220CA0005.NAMP220.PROD.OUTLOOK.COM (2603:10b6:8:1ca::7) by DS7PR12MB6119.namprd12.prod.outlook.com (2603:10b6:8:99::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7849.23; Tue, 27 Aug 2024 17:01:45 +0000 Received: from CY4PEPF0000E9D3.namprd03.prod.outlook.com (2603:10b6:8:1ca:cafe::bf) by DS7P220CA0005.outlook.office365.com (2603:10b6:8:1ca::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.26 via Frontend Transport; Tue, 27 Aug 2024 17:01:45 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.233) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.233 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.233; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.233) by CY4PEPF0000E9D3.mail.protection.outlook.com (10.167.241.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.11 via Frontend Transport; Tue, 27 Aug 2024 17:01:44 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by mail.nvidia.com (10.127.129.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:33 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail203.nvidia.com (10.126.190.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:33 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Tue, 27 Aug 2024 10:01:32 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , Subject: [PATCH v2 02/19] iommufd/viommu: Add IOMMUFD_OBJ_VIOMMU and IOMMU_VIOMMU_ALLOC ioctl Date: Tue, 27 Aug 2024 09:59:39 -0700 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D3:EE_|DS7PR12MB6119:EE_ X-MS-Office365-Filtering-Correlation-Id: 2793cb74-2c66-4838-fade-08dcc6b9ee4c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|82310400026|376014|36860700013|7416014|1800799024; X-Microsoft-Antispam-Message-Info: MFfTYyFvT9kKybYBToZi0gZAsY24hCVZILJjhwUnHXTbwy5QGrLNrju2qo6ji6TUC4ZqpbajCljXmVYE0s0yBpTrgRZmGpNqNiirhLKGD6vuGl0X1bwxREmNKQoQASd0gL0Y7Szyv2YDu+M21psSDaeTo72vSXY79qxjNr9Er4grU87vYlpJ2n8ke4YIQVfF0rSs34o4c1lmUQ5M86HIZQUzoZwK3mcOuwZYIgErcKlpaEObVEIQ+FC809fsL7belSlOJM8UC5S8q9eMP0TMLwiy9OdlNGU6DivkxXQSnucz4ad5gHO5Qm8HiN3/3KrQPvoe76W8AFhRjhHkKhckBO6BNGhWBVsc9ts4uptGMqSATjTAL3wgDIaqRpqenxmtnwF2CECF37A3ZYRYow1/tCxbQC/od43NZzM75s4sCE81sczF59SYWZBMazNrtdZhMyOzdERRmwbJMkhpkW435JSSbrs0zkyhKa+E0cpZKFv+1BxlQkSHV3iecUzEmfqVR+AXOfbEvHVA2fCX0buYHISDNawYf/jxXRtaa91n/tYBtlctVtf4J6F03pwzxpxI1mJz0AGzCisiqgi1eLZwgOXQAF1SgzU55orLTl84871+4s0deb9z1Kl16/IA9oPgE8JoD+iEc/UMF/ysFXAYRV67gYlgSy2DP+NX06m9YtpvjJ6Tp1J4U0Qt2N/pPcrErWgTqlZsF+a/M5VdZbu3mLMUz6ioayoydSxftqEI2RqRLLawTLIBYzVeby8nikd0VOU27elo6gAPgHUf10KDStUc9M7DhC5jpI6bUdr8P6jqGT8Tfa/ymtytLB+Bsw9mQY4qlT2WZGYlZMun8B+SnmQ8k8seVf0eAXdqbBKPcs/YqtJiZkLTZ3f0gzsGrGxv/2qAlpXOi3W0nkGyTT6YqOCY5lcfzCnpIxodu5WSxoWESJ6pp+IL80MOL9n9u0yhTVOhcoyTPNT2iMnMXhkNxySHVVlYa4mFIqaKHvw0ozsKmx+R9szhngfQkZsTZEKuqnZF8QVElp6AY1lPWZmL/lnrZfrYzMP8NLfjoqlYeFuTYoDMfOUrM1oL9cvOHCDNkusnIC57aWQXqJKb9UrGqv5bcqUhv87h5QyNnsgXNIhZ/S+YuQELSqZKtTX5ojslNkmxnuYrY+pHPgOIckrmW+8oyz6LiC6pWe9U47Cn1ORPze5QlGyuaeJbddk5HWqdtCZa9rp9Xv/aGcY7Ow/p3oCI8zKGjSMzmkxpZ+2Z3czjdXGZDc9wQE18V4cEHueTh4KgrZfIEdBpphm0rx0je0mM5ZFpX5npCFYYTdxX3YuqZRb8ga9RsGXMBukkGiXTflPVJHQu+QWBEMRnIU4apNKmIU8zmTZoWTt4xx4WCnnzrV7WOy9rE35FqyZ1LgcXdvHznfrMyZQ08Kvtf/+eXLF32BsJKOJOx/LvkUZFr7jifjpr13UunYZYjQyBGEZK X-Forefront-Antispam-Report: CIP:216.228.118.233; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc7edge2.nvidia.com; CAT:NONE; SFS:(13230040)(82310400026)(376014)(36860700013)(7416014)(1800799024); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Aug 2024 17:01:44.7945 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2793cb74-2c66-4838-fade-08dcc6b9ee4c X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.118.233]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D3.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB6119 Add a new IOMMUFD_OBJ_VIOMMU with an iommufd_viommu structure to represent a vIOMMU instance in the user space, backed by a physical IOMMU for its HW accelerated virtualization feature, such as nested translation support for a multi-viommu-instance VM, NVIDIA CMDQ-Virtualization extension for ARM SMMUv3, and AMD Hardware Accelerated Virtualized IOMMU (vIOMMU). Also, add a new ioctl for user space to do a viommu allocation. It must be based on a nested parent HWPT, so take its refcount. As an initial version, support a viommu of IOMMU_VIOMMU_TYPE_DEFAULT type. IOMMUFD core can use this viommu to store a virtual device ID lookup table in a following patch. Signed-off-by: Nicolin Chen --- drivers/iommu/iommufd/Makefile | 3 +- drivers/iommu/iommufd/iommufd_private.h | 12 +++++ drivers/iommu/iommufd/main.c | 6 +++ drivers/iommu/iommufd/viommu.c | 72 +++++++++++++++++++++++++ include/uapi/linux/iommufd.h | 30 +++++++++++ 5 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 drivers/iommu/iommufd/viommu.c diff --git a/drivers/iommu/iommufd/Makefile b/drivers/iommu/iommufd/Makefile index cf4605962bea..df490e836b30 100644 --- a/drivers/iommu/iommufd/Makefile +++ b/drivers/iommu/iommufd/Makefile @@ -7,7 +7,8 @@ iommufd-y := \ ioas.o \ main.o \ pages.o \ - vfio_compat.o + vfio_compat.o \ + viommu.o iommufd-$(CONFIG_IOMMUFD_TEST) += selftest.o diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 5d3768d77099..154f7ba5f45c 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -131,6 +131,7 @@ enum iommufd_object_type { IOMMUFD_OBJ_IOAS, IOMMUFD_OBJ_ACCESS, IOMMUFD_OBJ_FAULT, + IOMMUFD_OBJ_VIOMMU, #ifdef CONFIG_IOMMUFD_TEST IOMMUFD_OBJ_SELFTEST, #endif @@ -526,6 +527,17 @@ static inline int iommufd_hwpt_replace_device(struct iommufd_device *idev, return iommu_group_replace_domain(idev->igroup->group, hwpt->domain); } +struct iommufd_viommu { + struct iommufd_object obj; + struct iommufd_ctx *ictx; + struct iommufd_hwpt_paging *hwpt; + + unsigned int type; +}; + +int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd); +void iommufd_viommu_destroy(struct iommufd_object *obj); + #ifdef CONFIG_IOMMUFD_TEST int iommufd_test(struct iommufd_ucmd *ucmd); void iommufd_selftest_destroy(struct iommufd_object *obj); diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index b5f5d27ee963..288ee51b6829 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -333,6 +333,7 @@ union ucmd_buffer { struct iommu_ioas_unmap unmap; struct iommu_option option; struct iommu_vfio_ioas vfio_ioas; + struct iommu_viommu_alloc viommu; #ifdef CONFIG_IOMMUFD_TEST struct iommu_test_cmd test; #endif @@ -384,6 +385,8 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { val64), IOCTL_OP(IOMMU_VFIO_IOAS, iommufd_vfio_ioas, struct iommu_vfio_ioas, __reserved), + IOCTL_OP(IOMMU_VIOMMU_ALLOC, iommufd_viommu_alloc_ioctl, + struct iommu_viommu_alloc, out_viommu_id), #ifdef CONFIG_IOMMUFD_TEST IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last), #endif @@ -519,6 +522,9 @@ static const struct iommufd_object_ops iommufd_object_ops[] = { [IOMMUFD_OBJ_FAULT] = { .destroy = iommufd_fault_destroy, }, + [IOMMUFD_OBJ_VIOMMU] = { + .destroy = iommufd_viommu_destroy, + }, #ifdef CONFIG_IOMMUFD_TEST [IOMMUFD_OBJ_SELFTEST] = { .destroy = iommufd_selftest_destroy, diff --git a/drivers/iommu/iommufd/viommu.c b/drivers/iommu/iommufd/viommu.c new file mode 100644 index 000000000000..200653a4bf57 --- /dev/null +++ b/drivers/iommu/iommufd/viommu.c @@ -0,0 +1,72 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES + */ + +#include "iommufd_private.h" + +void iommufd_viommu_destroy(struct iommufd_object *obj) +{ + struct iommufd_viommu *viommu = + container_of(obj, struct iommufd_viommu, obj); + + refcount_dec(&viommu->hwpt->common.obj.users); +} + +int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) +{ + struct iommu_viommu_alloc *cmd = ucmd->cmd; + struct iommufd_hwpt_paging *hwpt_paging; + struct iommufd_viommu *viommu; + struct iommufd_device *idev; + int rc; + + if (cmd->flags) + return -EOPNOTSUPP; + + idev = iommufd_get_device(ucmd, cmd->dev_id); + if (IS_ERR(idev)) + return PTR_ERR(idev); + + hwpt_paging = iommufd_get_hwpt_paging(ucmd, cmd->hwpt_id); + if (IS_ERR(hwpt_paging)) { + rc = PTR_ERR(hwpt_paging); + goto out_put_idev; + } + + if (!hwpt_paging->nest_parent) { + rc = -EINVAL; + goto out_put_hwpt; + } + + if (cmd->type != IOMMU_VIOMMU_TYPE_DEFAULT) { + rc = -EOPNOTSUPP; + goto out_put_hwpt; + } + + viommu = iommufd_object_alloc(ucmd->ictx, viommu, IOMMUFD_OBJ_VIOMMU); + if (IS_ERR(viommu)) { + rc = PTR_ERR(viommu); + goto out_put_hwpt; + } + + viommu->type = cmd->type; + viommu->ictx = ucmd->ictx; + viommu->hwpt = hwpt_paging; + + refcount_inc(&viommu->hwpt->common.obj.users); + + cmd->out_viommu_id = viommu->obj.id; + rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd)); + if (rc) + goto out_abort; + iommufd_object_finalize(ucmd->ictx, &viommu->obj); + goto out_put_hwpt; + +out_abort: + iommufd_object_abort_and_destroy(ucmd->ictx, &viommu->obj); +out_put_hwpt: + iommufd_put_object(ucmd->ictx, &hwpt_paging->common.obj); +out_put_idev: + iommufd_put_object(ucmd->ictx, &idev->obj); + return rc; +} diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index cd4920886ad0..ac77903b5cc4 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -51,6 +51,7 @@ enum { IOMMUFD_CMD_HWPT_GET_DIRTY_BITMAP = 0x8c, IOMMUFD_CMD_HWPT_INVALIDATE = 0x8d, IOMMUFD_CMD_FAULT_QUEUE_ALLOC = 0x8e, + IOMMUFD_CMD_VIOMMU_ALLOC = 0x8f, }; /** @@ -852,4 +853,33 @@ struct iommu_fault_alloc { __u32 out_fault_fd; }; #define IOMMU_FAULT_QUEUE_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_FAULT_QUEUE_ALLOC) + +/** + * enum iommu_viommu_type - Virtual IOMMU Type + * @IOMMU_VIOMMU_TYPE_DEFAULT: Core-managed VIOMMU type + */ +enum iommu_viommu_type { + IOMMU_VIOMMU_TYPE_DEFAULT = 0, +}; + +/** + * struct iommu_viommu_alloc - ioctl(IOMMU_VIOMMU_ALLOC) + * @size: sizeof(struct iommu_viommu_alloc) + * @flags: Must be 0 + * @type: Type of the virtual IOMMU. Must be defined in enum iommu_viommu_type + * @dev_id: The device to allocate this virtual IOMMU for + * @hwpt_id: ID of a nesting parent HWPT to associate to + * @out_viommu_id: Output virtual IOMMU ID for the allocated object + * + * Allocate a virtual IOMMU object that holds a (shared) nesting parent HWPT + */ +struct iommu_viommu_alloc { + __u32 size; + __u32 flags; + __u32 type; + __u32 dev_id; + __u32 hwpt_id; + __u32 out_viommu_id; +}; +#define IOMMU_VIOMMU_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VIOMMU_ALLOC) #endif From patchwork Tue Aug 27 16:59:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 822913 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2070.outbound.protection.outlook.com [40.107.237.70]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3EAF71D1752; Tue, 27 Aug 2024 17:01:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.70 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778117; cv=fail; b=XhT0gstIIzpXVv7OLLDTu2Q+kn4DhlIEqyTv5lUdy/YkUgV4w0xQ1mOUVfTWj5jEm7mI0biNcDzfXzWZ46fc3MWazmcoW6WD0JnNjkRXfyeiAAytjzeK70WV9GOPl8nXST9GrhM5lZ2LR39Fs4K8C12tZaDo+kQ9bN5+Hkm3t/A= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778117; c=relaxed/simple; bh=AcgTftsgFCWE94a09st6PQ6AzIbrDYAKI6dA7l9ZUnQ=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Uf/uAASKVnwNZMy2QJKVltpSEKKyo4sQG43+LbCEKkPo0ZF+Osbp64neCFADAoKDE9xhfhOVZ5Gm2N3IsDvFE+caxIfiq0hoREIxU67LbPGqBP6NMWn4TCJ28tdmPXlcMN9SYDXOTYhS0ktiHM9PusYzcUsFOui52KD91pCs3d8= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=ee9R9Zh+; arc=fail smtp.client-ip=40.107.237.70 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="ee9R9Zh+" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=fJjw8MyvlUwnIBRZuENkhtrCwr+gqjs2XCDmQ+6TrqIJJoVt3iN4+3atDTqVzSSUf3CU4dKelSdeC+2WqkMGZ2eHZ5ZBAWdrFAG5pIbunPapz3+uhv1yAfko/JQ4N1yIJL0P925qnz/vHfWKfs1ftKyaH+6PrBxn01YfGbg128SKuHZqBVztBHqv8EwBj9dgUN1XkQFiCpyGKU9wFm9jk/oGv/khigHdNJ9C0S4J+Zvb/kiPr+xTnveqOuml0qTD1AsBEbCuQbputaRk2UMgA3cdAKuYI1Q3xNGBpJ4rMzoXOqOwXG66sHJX2I8Fgn09xjg6sJDTckU3UE/5znuPoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=ebiOam/XcwPXKNfo9r5+oOgFBW07nF+uKs/nfcrVn44=; b=lOX2lJM8JDg49zV3c64M3RcUpAv+CTjHP3NUUaQEnERimJhvpw8kdAL3tOYemBFlsIrAKsgjgOeUNeH/gEbV6ngXafTryzBaTYXws8g+i/0cAaWVD/n7WLxlqrw7csNCu7aGxG2cEIz6YmlP2EN/TPRqRO0vA+UU/tM3yERzEYWyxfklFPn8e7gN12auGnC0P/K8ODGOjP6EOQkR9l4muTxdFoxxWFkT0HoaMTmfk1F75DRALPFBeMzZVBPBiOtjrBHBjPfQ1iHIWEHzpAFvPl+uagn6VlrBStBi1JMZkf8BVXp+k2CNvPyMbopuzKIYvq1yKaD4ed0YU0y0d/54bw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.232) smtp.rcpttodomain=huawei.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=ebiOam/XcwPXKNfo9r5+oOgFBW07nF+uKs/nfcrVn44=; b=ee9R9Zh+bm9lgMI5b89Qt1YnimMB6zWo4LzPcGE6P3qTMol776jxjvT9weLZAE58lxFAx4CAEV/poq1+sDmhwdjxeCzsZWn80ll6QARaa4sUCSpoS/02Fs9ruhtdG4BqGWL0axvJ9OtesDYVnCRuU8Uh8Px+1v9XV2ukT6JD0RC/o56NToX5WE7jF6EpbCkBeSOCED6UwxlL6h5k4MHOYOENuLz7AIxSrZaJ/QobUFusyrj8mYmkv75w718p2PtIm4+w0ZC4zWjhNcYWL1SBWc8as1Y5bXh0gS8jCDOjQ6f3bvztigOirH58Hup4YqLBPOXkZEHAdbAjG4s2VlxBVg== Received: from PH7PR17CA0072.namprd17.prod.outlook.com (2603:10b6:510:325::20) by SA3PR12MB8440.namprd12.prod.outlook.com (2603:10b6:806:2f8::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.24; Tue, 27 Aug 2024 17:01:49 +0000 Received: from SN1PEPF000397AE.namprd05.prod.outlook.com (2603:10b6:510:325:cafe::96) by PH7PR17CA0072.outlook.office365.com (2603:10b6:510:325::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.20 via Frontend Transport; Tue, 27 Aug 2024 17:01:49 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.232) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.232 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.232; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.232) by SN1PEPF000397AE.mail.protection.outlook.com (10.167.248.52) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.13 via Frontend Transport; Tue, 27 Aug 2024 17:01:48 +0000 Received: from drhqmail202.nvidia.com (10.126.190.181) by mail.nvidia.com (10.127.129.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:34 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail202.nvidia.com (10.126.190.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:34 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Tue, 27 Aug 2024 10:01:33 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , Subject: [PATCH v2 03/19] iommu: Pass in a viommu pointer to domain_alloc_user op Date: Tue, 27 Aug 2024 09:59:40 -0700 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SN1PEPF000397AE:EE_|SA3PR12MB8440:EE_ X-MS-Office365-Filtering-Correlation-Id: 712faec6-6a0f-4521-de8e-08dcc6b9f05b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|7416014|376014|82310400026|36860700013|1800799024; X-Microsoft-Antispam-Message-Info: liz7OOjYtB8wi8tC9y496dZnfzZF7aTdUzxonWFnj/Nrc5O1WttRqcM3DkxS8WEXksTkkrGeX7U6ZYLzDTzgrm1SRMszFxPi/EDRtvNuffEO60fCEtzpFVoGyUQU4dCg+7330TByY7rFBgPLt0Gp6BY89/nH64cibiQn2ZrOMP5KfSBm+jfkvyS1Z59hUWRCK+8xqpU138i3FWfmpAemQ1XwRLC7/z6hRKwSF3oByRtGOdsNu10uDiskjc4IvZ8DBbLHpZ7CFk0UUSSO/OGJLkiy8BQ4nT0y2a2UeeSiQ/62WoiSoaERpFKyfrgqbA2UzbpDz+57anv3zS47uZHiHWLRBwmL44Pk5LY94vVIZXuwhlInXhlueVVkthSGu7Zj4X0+lOQZ1cid1F9KcxOKsBsxW3WTXl486xziQrnlvRidWOCsoRoFN3bdt0bmZCLv/fcj6Sia+o8tcx34kpb+JmcWWmv9+222uWo8g50DV5t09pYOxC+OLPyGAyWm9YMHdql2AJeTxYGxa+L/MELW9I1zr70OM8J25UZ1AkdrKnzj34QpmSuUX0aAw58t72xsQMeXQ82wtB9HTE81xWOCm94eJa0VOliTSTCmgdGrBnEFQsayJpVvPVKkoBg83E+lYrvOr/omztwLRKS1IPNri1eTjvKcgW5FCwgsWkHB0oKpn6RjKAkj6TYMEj6+3nEEQewdm6vjcgITVo+1X+/JWxM/ngsa8T4ILFsEFZHImGigZxxV8ip6HFTZ8OXs0oaLXXahYqygO/RTEVUqr7fAteyA+fqGaM6J1Sg+s3pT99p/4+2Fcry3mUZDH4dMG2Zc37MmoRadX18f+VLnpwHRZzUx20xEE7RH4Wnc3gNrUwLWVE9Y0qKiI66cN8mbsHciOhO6WlnRr6zkSFdwN8PX1UteTLD/MeLIpUYIPuSJloQGXsG8mwewSVk9P8bsgxNZgr3FMSz5EHvBOWTko8LtIsfXPzriMzodhvUHeZZsvqP/ZkLRjMvhI5cZFCucjocSL0+0zZW/omDVZu019BHnRXAHDPstzJOfCBb5xIN9ZOnuxW+Lb96mb18Xxo7Xy/xBGeuP2z7Uw4wGXVD7atqpOgrbOLsOVDvVNz8Yfp5rGKM9Cv8bjDOKwu6zwW88iFMNoNgLy4OmrIBKXxs2cjhy1uydpO37MSsy+QJiMIo2vQTRDXD8tpsQPLntQ7fApJHpIWDpQPHi0DhyTYDSwNpPbXOFWb3x9juYIkEK+neKvRRhf8dbpgsQ3G8esrEo0pb4hdC+BtGLpVzB2X8psClVXIF9pvdQ1Bg9sM8fEwc3JVgvwxDcKaJ+lk1o8y9B4qj6BT+mhZIpdtVgLdy4D9Mb9AD4fmBicEdqivJ+G1de2YthPz/K/iNQkwNyGU/j1N61rqGGixnM+osllOHKMq7kATBVS2dAONr7sleB23007ar/iSo/ZvwfgtLxC/QrD3De X-Forefront-Antispam-Report: CIP:216.228.118.232; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc7edge1.nvidia.com; CAT:NONE; SFS:(13230040)(7416014)(376014)(82310400026)(36860700013)(1800799024); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Aug 2024 17:01:48.1382 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 712faec6-6a0f-4521-de8e-08dcc6b9f05b X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.118.232]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: SN1PEPF000397AE.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA3PR12MB8440 With a viommu object wrapping a potentially shareable S2 domain, a nested domain should be allocated by associating to a viommu instead. Driver can store this viommu pointer somewhere, so as to later use it calling viommu helpers for virtual device ID lookup and viommu invalidation. For drivers without a viommu support, keep the parent domain input, which should be just viommu->hwpt->common.domain otherwise. Signed-off-by: Nicolin Chen --- drivers/iommu/amd/iommu.c | 1 + drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 1 + drivers/iommu/intel/iommu.c | 1 + drivers/iommu/iommufd/hw_pagetable.c | 5 +++-- drivers/iommu/iommufd/selftest.c | 1 + include/linux/iommu.h | 2 ++ 6 files changed, 9 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c index b19e8c0f48fa..e31f7a5fc650 100644 --- a/drivers/iommu/amd/iommu.c +++ b/drivers/iommu/amd/iommu.c @@ -2432,6 +2432,7 @@ static struct iommu_domain *amd_iommu_domain_alloc(unsigned int type) static struct iommu_domain * amd_iommu_domain_alloc_user(struct device *dev, u32 flags, struct iommu_domain *parent, + struct iommufd_viommu *viommu, const struct iommu_user_data *user_data) { 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 fa75372e1aa9..6d40f1e150cb 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -3347,6 +3347,7 @@ arm_smmu_domain_alloc_nesting(struct device *dev, u32 flags, static struct iommu_domain * arm_smmu_domain_alloc_user(struct device *dev, u32 flags, struct iommu_domain *parent, + struct iommufd_viommu *viommu, const struct iommu_user_data *user_data) { struct arm_smmu_master *master = dev_iommu_priv_get(dev); diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 9ff8b83c19a3..0590528799d8 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -3703,6 +3703,7 @@ static struct iommu_domain *intel_iommu_domain_alloc(unsigned type) static struct iommu_domain * intel_iommu_domain_alloc_user(struct device *dev, u32 flags, struct iommu_domain *parent, + struct iommufd_viommu *viommu, const struct iommu_user_data *user_data) { struct device_domain_info *info = dev_iommu_priv_get(dev); diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index aefde4443671..c21bb59c4022 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -137,7 +137,7 @@ iommufd_hwpt_paging_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, if (ops->domain_alloc_user) { hwpt->domain = ops->domain_alloc_user(idev->dev, flags, NULL, - user_data); + NULL, user_data); if (IS_ERR(hwpt->domain)) { rc = PTR_ERR(hwpt->domain); hwpt->domain = NULL; @@ -239,7 +239,8 @@ iommufd_hwpt_nested_alloc(struct iommufd_ctx *ictx, hwpt->domain = ops->domain_alloc_user(idev->dev, flags & ~IOMMU_HWPT_FAULT_ID_VALID, - parent->common.domain, user_data); + parent->common.domain, + NULL, user_data); if (IS_ERR(hwpt->domain)) { rc = PTR_ERR(hwpt->domain); hwpt->domain = NULL; diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index b60687f57bef..4a23530ea027 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -318,6 +318,7 @@ __mock_domain_alloc_nested(struct mock_iommu_domain *mock_parent, static struct iommu_domain * mock_domain_alloc_user(struct device *dev, u32 flags, struct iommu_domain *parent, + struct iommufd_viommu *viommu, const struct iommu_user_data *user_data) { struct mock_iommu_domain *mock_parent; diff --git a/include/linux/iommu.h b/include/linux/iommu.h index c16ffc31ac70..f62aad8a9e75 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -42,6 +42,7 @@ struct notifier_block; struct iommu_sva; struct iommu_dma_cookie; struct iommu_fault_param; +struct iommufd_viommu; #define IOMMU_FAULT_PERM_READ (1 << 0) /* read */ #define IOMMU_FAULT_PERM_WRITE (1 << 1) /* write */ @@ -564,6 +565,7 @@ struct iommu_ops { struct iommu_domain *(*domain_alloc)(unsigned iommu_domain_type); struct iommu_domain *(*domain_alloc_user)( struct device *dev, u32 flags, struct iommu_domain *parent, + struct iommufd_viommu *viommu, 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, From patchwork Tue Aug 27 16:59:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 823340 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2069.outbound.protection.outlook.com [40.107.237.69]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9D2841D1735; Tue, 27 Aug 2024 17:01:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.69 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778126; cv=fail; b=CFrka9oXvypxrE8nDMgPkiJ5uaXwfcNdl8gaE0y81NbjyrkQ3YF4zf5DO1D1ObQcSk6p9laUB5S/aoi/sVCQeYYUa4jCZQN0o5XcpDuX3vmLUCjUrealzMrg0jfA+zBl217bc+J+5EIUWjRfDlByfKX+b2rcNRm8y3qsvoAJ96o= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778126; c=relaxed/simple; bh=0IylUcmzRjV4meE6CQjfyNhe68McguHncHAb0bsyyGg=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=oeBySN7P8qUm5DMnIM0cCogyllTrnRCBevJB9cniaXF2z7dgiZhK+TA2odR2nmIg8LFq92LDk7w1b4bBQLLllrcDCBW2cdYH0AckrfjASYmbkzworegFoqbj9QzluH5ykUoQrU2Io8zHorAQOjUhqozBzByQG+svWYhEi35IPYM= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=iAw+6XOS; arc=fail smtp.client-ip=40.107.237.69 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="iAw+6XOS" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ZBjbgcTIhjlLeoCAFudLBQxwRyUxmleYfQZO/5c1oheNFwjDpyPZaWarHFV3Jd86gAOS+67u9x/Fde6jmnbMQxs5w1CUNiI/Q1+gnFBlE3WFRwxEvZbgYMYpu/Jjebug+S1nVS+0Wnm/bkGgd/nEB6XNW3/ar1k9va6oDLsmqVWAAT5ydfvy+2foupRIflt9sYFeONUdzINj9z/aE0Q/LPrrgwh851COjs3sAYkBjKkeUWpvZtvXVK8/zLCDdIcngSYvqFFwY0t7fxRYVFwZvIVTTsQPFMi54q7LdMVnB3ocE3haxoGetQ3rVTOsLLC73GeLRAruOf8X7+rm3tT/Pg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=FRo87J3lJqxbRQDxwquN+4RK8PiFhblCS95tLtEOU3M=; b=DJotl8pD2lP2Ih3fqWQr+xaCMQWOMxLa6sNThwAFmOz74qnzSS/G2QjerwLnQmuw41v9knY0QmCo5tYrN5cos9WqjJgwviOlhN+gHRwu9t5llHj0tr486ImNfrTgEKdLlTjpRM4RYXb1YRbPUr9s86FwZsPzLVakG6xYuSxgNQIIez5IMLrIA8hYVPZ+NC6LDocNtlrlTKk5xPpmlrYlTjratre1HXQRa1qDSm6XUwt4WshUywO4iPQai99/wc6rJJaNC6NLLGWzusGcqQJO92bDqkxhX9wCAIgY98Cwaor7JvFttmAuveOmDAF30fyxobDHHBHg68MdFa+VeIx3ZA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.233) smtp.rcpttodomain=huawei.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=FRo87J3lJqxbRQDxwquN+4RK8PiFhblCS95tLtEOU3M=; b=iAw+6XOSVKOA87rc/4mxrvqD5atAdbR5wHGq9JW1uwlLuIUzUIoI6dbpf0l6nbstxIee8vyar5J/lQ9tMRwd39i3qNB0Rp2LEEFGdAX9GCa+lxberm5wQx6c4uhim6ZdS/Tr8MfBbmf7ai0Mi98BrGCXnNj7U9jI1R+O2j2PXVXISnkBRSQrV+UeJdU/2tQpNCBkwoHwwEzD5SGS3X2wBVVjaYDnZFz7HcTvUyGGiOMJ7Izqmz6SxHgOl72cxMXb1xMayGqfaMAkBTyxnNpUSNhE+nOh9dLzoIIIdv4RxFGLOZUTzT7nl9YXkxtjqgQ5D1yr/kgjmBH/kxoUW81PEA== Received: from DS7P220CA0003.NAMP220.PROD.OUTLOOK.COM (2603:10b6:8:1ca::17) by BL3PR12MB6595.namprd12.prod.outlook.com (2603:10b6:208:38e::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.25; Tue, 27 Aug 2024 17:01:48 +0000 Received: from CY4PEPF0000E9D3.namprd03.prod.outlook.com (2603:10b6:8:1ca:cafe::2f) by DS7P220CA0003.outlook.office365.com (2603:10b6:8:1ca::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.26 via Frontend Transport; Tue, 27 Aug 2024 17:01:47 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.233) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.233 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.233; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.233) by CY4PEPF0000E9D3.mail.protection.outlook.com (10.167.241.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.11 via Frontend Transport; Tue, 27 Aug 2024 17:01:47 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by mail.nvidia.com (10.127.129.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:36 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail203.nvidia.com (10.126.190.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:35 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Tue, 27 Aug 2024 10:01:34 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , Subject: [PATCH v2 04/19] iommufd: Allow pt_id to carry viommu_id for IOMMU_HWPT_ALLOC Date: Tue, 27 Aug 2024 09:59:41 -0700 Message-ID: <2d469a5279ef05820d5993df752d32239878338d.1724776335.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D3:EE_|BL3PR12MB6595:EE_ X-MS-Office365-Filtering-Correlation-Id: de376c9a-4059-4779-5d77-08dcc6b9eff5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|1800799024|7416014|376014|36860700013|82310400026; X-Microsoft-Antispam-Message-Info: 2pB2xK6UgGQvNIKuzlF7SYWfK6Nq3DxIIR4fdcMChins7JZQtw0O4x4Ru2tqpl6/0UFiX1E9DAxLtkKyc+mdjWx+pTBlt8zJq6T8YD8McEAkwE9BUUAYNNPv72loC97KlsKcndUVo++iNAItLsVF6LwAnwKxl4AqM6dgGNp6oeDOGcntU1MmGKPUTYo0uULBgdk9UGUmJYCu5vREhkXsqlZKYpnU56eLv59aSebs+c1sPzb8HhiYnWf/Eqs3y5gxCjeaP2lJR6zqr50a9OYECH+CFhwVIkw3gBIYlAfYVZv22XYYOvgexPWp5BkKC2d3wly22sqZlg664eOx1WPPvFXmUNPcFiYPhmafrYTdiBZ9QtRYx8SSIs68DBB0EohoUfOgYNyZyTuJx4n4f77TZKMD+8AnHGtl/kZ3TeIfP7gNXLzRsWyh60G46ZPKcA/Rk4cnEuv1MAKiLgykND8WNyC2LEM7b68ZCG31Eok+3uo92i2TuIiQ1A40WoJiscP1mqvDMPaz1TkPaMCbMmgKOBoDl8oGY+oz/w0czYBT/Xkz3M29nESbQ/ynJk8ApQaxed/4XESy3IfvBvQkesY5Cn11gFMr8MJGu+7y0/gdKuolq5pzEOuYUNXIJrrAMdz6CDAQeaUxzvvu0vjIbYXB5OqTHGDuXCIZvVB+yYGSruDUmMlwmUXZ3wpES6PHT+N6Z+TvtnvgFRr5zd1hzYLzCxMKC2oIHcAkaSQfJw2MpQ/GoaQ11L6VgwcO13pA5iZRFu1RLEkdWR3sa4cGe+CrVTsp+iqq0F7WQtGfPetobc6MTP+8XS9TmGTw5WzSuSgRkeTa8KO7MW6je9r55KVDpm+hyWp9mZ4fmhbeGZMN1gOmx3/eUTblFibt9BBdZVudnpSiT0nw40+MKTOagMOZ9U6qdEBQryxPd3SEbR9aHHUOvm0cGXpwgcDtKsYQ7JgOmOcoMI1Y3tKduUDeKyFAimWZIEmF9GJOJqJXNg16+Wx0J5G2Q+FuyNC9BY8Czy5Y+JsTHAaJWIRJhmzspp6Dw34tfcXd+LXpw6xt12n5WWt4J4LH1pHyLwO3h68D2YWmFr87wgoHqnHtxWXzwgPn58xIcAjGK8XrAZb8RCGIBFeML62UEukFW51iYRnQQQNYG+/U/dhfR9TlD9I79z5e2aSVvFaAkmzlmH0cZdDXmzKVLdxuYz7MTGYWSKzs0r+AAye/5oKt24onrmJrqpxDyc9JFP8yB9zS6zbtsMyvD5Na/d8uRQxSYR0Rfl8lxE/dyKIT4wuTZn3uF/WlxsU4xODpWneThnNWPzRC0r45oPVGkkD76YybfszGSLLbnFc8/IPtH0iDfZdVkVoF6ROO+2H+GFrZUe0B7hYW+33ya9ATQO0SJXmT3B5Bf8Q5D17SDxxII0gdiRMfNVNpY4TycPrgWuyxjmklFxzLPeT8j+KPdjDIWVvCQ5cujZaEG7IB X-Forefront-Antispam-Report: CIP:216.228.118.233; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc7edge2.nvidia.com; CAT:NONE; SFS:(13230040)(1800799024)(7416014)(376014)(36860700013)(82310400026); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Aug 2024 17:01:47.5914 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: de376c9a-4059-4779-5d77-08dcc6b9eff5 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.118.233]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D3.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6595 Now a VIOMMU can wrap a shareable nested parent HWPT. So, it can act like a nested parent HWPT to allocate a nested HWPT. Support that in the IOMMU_HWPT_ALLOC ioctl handler, and update its kdoc. Also, associate a viommu to an allocating nested HWPT. Signed-off-by: Nicolin Chen Reviewed-by: Jason Gunthorpe --- drivers/iommu/iommufd/hw_pagetable.c | 24 ++++++++++++++++++++++-- drivers/iommu/iommufd/iommufd_private.h | 1 + include/uapi/linux/iommufd.h | 12 ++++++------ 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index c21bb59c4022..06adbcc304bc 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -57,6 +57,9 @@ void iommufd_hwpt_nested_destroy(struct iommufd_object *obj) container_of(obj, struct iommufd_hwpt_nested, common.obj); __iommufd_hwpt_destroy(&hwpt_nested->common); + + if (hwpt_nested->viommu) + refcount_dec(&hwpt_nested->viommu->obj.users); refcount_dec(&hwpt_nested->parent->common.obj.users); } @@ -213,6 +216,7 @@ iommufd_hwpt_paging_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, */ static struct iommufd_hwpt_nested * iommufd_hwpt_nested_alloc(struct iommufd_ctx *ictx, + struct iommufd_viommu *viommu, struct iommufd_hwpt_paging *parent, struct iommufd_device *idev, u32 flags, const struct iommu_user_data *user_data) @@ -234,13 +238,16 @@ iommufd_hwpt_nested_alloc(struct iommufd_ctx *ictx, return ERR_CAST(hwpt_nested); hwpt = &hwpt_nested->common; + if (viommu) + refcount_inc(&viommu->obj.users); + hwpt_nested->viommu = viommu; refcount_inc(&parent->common.obj.users); hwpt_nested->parent = parent; hwpt->domain = ops->domain_alloc_user(idev->dev, flags & ~IOMMU_HWPT_FAULT_ID_VALID, parent->common.domain, - NULL, user_data); + viommu, user_data); if (IS_ERR(hwpt->domain)) { rc = PTR_ERR(hwpt->domain); hwpt->domain = NULL; @@ -307,7 +314,7 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) struct iommufd_hwpt_nested *hwpt_nested; hwpt_nested = iommufd_hwpt_nested_alloc( - ucmd->ictx, + ucmd->ictx, NULL, container_of(pt_obj, struct iommufd_hwpt_paging, common.obj), idev, cmd->flags, &user_data); @@ -316,6 +323,19 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) goto out_unlock; } hwpt = &hwpt_nested->common; + } else if (pt_obj->type == IOMMUFD_OBJ_VIOMMU) { + struct iommufd_hwpt_nested *hwpt_nested; + struct iommufd_viommu *viommu; + + viommu = container_of(pt_obj, struct iommufd_viommu, obj); + hwpt_nested = iommufd_hwpt_nested_alloc( + ucmd->ictx, viommu, viommu->hwpt, idev, + cmd->flags, &user_data); + if (IS_ERR(hwpt_nested)) { + rc = PTR_ERR(hwpt_nested); + goto out_unlock; + } + hwpt = &hwpt_nested->common; } else { rc = -EINVAL; goto out_put_pt; diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 154f7ba5f45c..1f2a1c133b9a 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -313,6 +313,7 @@ struct iommufd_hwpt_paging { struct iommufd_hwpt_nested { struct iommufd_hw_pagetable common; struct iommufd_hwpt_paging *parent; + struct iommufd_viommu *viommu; }; static inline bool hwpt_is_paging(struct iommufd_hw_pagetable *hwpt) diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index ac77903b5cc4..51ce6a019c34 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -430,7 +430,7 @@ enum iommu_hwpt_data_type { * @size: sizeof(struct iommu_hwpt_alloc) * @flags: Combination of enum iommufd_hwpt_alloc_flags * @dev_id: The device to allocate this HWPT for - * @pt_id: The IOAS or HWPT to connect this HWPT to + * @pt_id: The IOAS or HWPT or VIOMMU to connect this HWPT to * @out_hwpt_id: The ID of the new HWPT * @__reserved: Must be 0 * @data_type: One of enum iommu_hwpt_data_type @@ -449,11 +449,11 @@ enum iommu_hwpt_data_type { * IOMMU_HWPT_DATA_NONE. The HWPT can be allocated as a parent HWPT for a * nesting configuration by passing IOMMU_HWPT_ALLOC_NEST_PARENT via @flags. * - * A user-managed nested HWPT will be created from a given parent HWPT via - * @pt_id, in which the parent HWPT must be allocated previously via the - * same ioctl from a given IOAS (@pt_id). In this case, the @data_type - * must be set to a pre-defined type corresponding to an I/O page table - * type supported by the underlying IOMMU hardware. + * A user-managed nested HWPT will be created from a given VIOMMU (wrapping a + * parent HWPT) or a parent HWPT via @pt_id, in which the parent HWPT must be + * allocated previously via the same ioctl from a given IOAS (@pt_id). In this + * case, the @data_type must be set to a pre-defined type corresponding to an + * I/O page table type supported by the underlying IOMMU hardware. * * If the @data_type is set to IOMMU_HWPT_DATA_NONE, @data_len and * @data_uptr should be zero. Otherwise, both @data_len and @data_uptr From patchwork Tue Aug 27 16:59:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 823343 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2074.outbound.protection.outlook.com [40.107.223.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A21FB1D1F52; Tue, 27 Aug 2024 17:01:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.223.74 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778118; cv=fail; b=SOblZ7FZRHlEQyg/VmW+WA4TEn/XYKFCrCy2GdrAmkHlHQNClh1mZNmUnwLmknXcXGcdHKzPJ005QT2kvy+NiMnoRSrfVnvc/UQO8Zj93+o6HZxPdcdZVS/0TVVBEcW+DJ3P5pLQIl6DjxA5W6fQv5pqMHx47akJve/B4W++zYI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778118; c=relaxed/simple; bh=Z4jS70XyEoAuTs+0WPyexU4K6XeOI78usmbDp82mmRU=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=a892ziChXqHIAsi7ac2vDcoAqK+N6Dt2FQ3S86gLVVo/NcjKpB5bG2vq2ep1jHf4X9D3RWVi/0Ifx2MXun2ni7GNOn0k1sQdBFiA8Wh3SBxuUvd87DP/iyGtnJl6euX39BTQTd18+wnzJgluCRpg3Tld0hraariqERGyeWBj7Ig= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=Sz29/fmy; arc=fail smtp.client-ip=40.107.223.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="Sz29/fmy" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=okgv18xMTvHnCRW1Y1lF+UnOwP8sGfKVLYcd82AZF2Bo8Lwb+iMxInuxa8OSh4LqvbMTuO5Wl0XHfgeusovC4tSukm12LTscNRAiw1Rsn/XC4jkK/Zhwck3o7oqv2jsHL6gjnT1iPtnChY27YAXWRJXf/gh4+G2MaQAsipgQ2zH9G/Jf6feUYQTFUBB2/Pw0aJKY2Cn3qNPNJdXNADQgwOh58cf/2qQGdAMAW9S3IwtIkxL4l61l7mrS/L2z++/01gD3Bm2ImKek4kay5OyXAoTItfd10MzZjRcYpXQ5lI4W/8n7QE+lrTXQiZjgdR4s5HXpHP+zxK45n63qc1OcBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=keydAWl6Opawj1kud6d+9V9Z+xy/wE0UwmYD8tJmmsc=; b=g1tHeE0uA7TI+h6z1VAQTyD65NsbfmX9XmxT1jP6q3eCsPe5TMAdLawBCSE3KBH9SIo+/hi56V4QgZAOe+m4uVCjoPTcU6R3PdrXvB02mH+Qu2UiInuxBziT4ontz+cpEVcMgWnsfcVSvixd2qzNvmToSQFWmIkPpCu+d4Ocf4v+glMLpjy4pe85G9fzA/ZRZEX8RR9MXk139+EJk1GN0eA/r9aYAzUsV7ztN62y4EKjDorUDT4hf6v2DjpLnD1tq23gtK4UApZgneRWnu5UcLDYGGtZSwhNkCmh/6fkkgFkxWNsosnzS7obYRS1LRymuC87QW12SkNMuRwklkka4w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.233) smtp.rcpttodomain=huawei.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=keydAWl6Opawj1kud6d+9V9Z+xy/wE0UwmYD8tJmmsc=; b=Sz29/fmyJOFb9sYFkW2hyYvZS8aMwmjHhh9n+X7GdlhDNmoD8Q+AbBH3Xnd0nsOSyioQHIcqf/9caJPzrPNV1WaUMDgSO4cr73SPWHibEA9O3kFq6D7iOX/hxvOOIUnp8konJWW5+PgrO09afpRa23IBz6vqtdR2CA0eLz5eQjUSFgKySshU3tghyJaVfJtObGTIP7nBFLcSVScCNFGARQgNzA5weXYvlZyxgXbJFlLgKG960djLJGMH8mXx7GD9ksx2LOPcK69F7EcQZbK6/BaDuAWTNa0V+IRtnPCvzAa43GZy62VSyM1FCnsgty8T8mcs2c8A8INFOuNbQnLoBQ== Received: from PH8PR07CA0015.namprd07.prod.outlook.com (2603:10b6:510:2cd::20) by SJ2PR12MB7896.namprd12.prod.outlook.com (2603:10b6:a03:4c6::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.25; Tue, 27 Aug 2024 17:01:49 +0000 Received: from CY4PEPF0000E9CD.namprd03.prod.outlook.com (2603:10b6:510:2cd:cafe::20) by PH8PR07CA0015.outlook.office365.com (2603:10b6:510:2cd::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7784.29 via Frontend Transport; Tue, 27 Aug 2024 17:00:52 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.233) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.233 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.233; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.233) by CY4PEPF0000E9CD.mail.protection.outlook.com (10.167.241.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.11 via Frontend Transport; Tue, 27 Aug 2024 17:01:48 +0000 Received: from drhqmail202.nvidia.com (10.126.190.181) by mail.nvidia.com (10.127.129.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:37 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail202.nvidia.com (10.126.190.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:36 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Tue, 27 Aug 2024 10:01:36 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , Subject: [PATCH v2 05/19] iommufd/selftest: Add IOMMU_VIOMMU_ALLOC test coverage Date: Tue, 27 Aug 2024 09:59:42 -0700 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9CD:EE_|SJ2PR12MB7896:EE_ X-MS-Office365-Filtering-Correlation-Id: b86d6145-9f75-49c6-e149-08dcc6b9f0b3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|7416014|376014|82310400026|36860700013|1800799024; X-Microsoft-Antispam-Message-Info: FOMEbk3Jpdis8N/t3MUwZoNBo09yw9vkZ9jT04MAkfFnWhutMqQC04QGc8ODb2DnxGG91TM+ZBxZDwLgwDTvu5HEY9lC7fpT16kT9bjyc0cIhVBqqSm2QjOj79aYj13Avp1uStxdd+8oS7oR8mXqMOW0HXQZyWymIdbkd1AcdzYMHMVkUrJg+5oaKIE6FmNWgcu6vdRJ4Ji07R9cMny51X4I3CN4bZEsmNmNWIHMH9NnlFWyj5wirx+FVCSwaiZd7h9UXPTo/RIIqrgV0nfxQK6xgQCp8Ge9UiRsrBoIy+8nXm5iFb7mFHHzF2nn7hyc6dU779pfLOAKfXTb6ffdugQZAwRJfq3/yeWpSqb9Fst0FrEr139J6P/PPQFmS0ywj4SalvU9Pu+N+PTjojgHMKQ+u/JOudsq6trtxdCwy+K4XowqjmwgyuPUVVzE0Mvt0w+E5zLE0EFcOcqjhtYPFImqjx2VIn62MO6R5/hQDSs0PBHDRvhU2TAcaXna11x6Pn/Vc/akOasyYo9GwtC5a9sftgEpt8syJNrkK++0ItX5oyAFvA0zrxAppr4GlJkkpY4WnkpfatBiIVzrS+E81ffZAEwECiP7aXr3msPFMzGFZlg8CPLhPU4+l0d4G3KX8w8a3juSKb6+Rxq8gtDcgRY9rBqqA9Ky/j1zKAx7r1ZSl64o+RJHGMEX51RVe1IOonOD0ZBPyhGtypw9C6XhTxDeTot3s6nMgSqLMVcfMH/7OkAMD/ZU1kB8/ND0tIvOtRCMo1ChCqsyZo7a7eOoz8g7+SC+wuAk6LEGAnMZv77u9vMbVH98qInB+tBLJW879XUvEQ0qnZ7lJ9s502aUXe8PVwNASYAVi9uo1q9TLWeuFghI9zg4eS7auhkDrXfwkB3H+PXKPMqYOJz4xkW4p0M9gCCkdgK78Tq7G/5P+LwA9NeZcVh8JXwaDKQQo8W+Lb/pmcX5n+37AD1Zht92lPh62fEd0t4RV7BcF87HGKwBm0iTK84V1cvwW+3R2vsgKgnfgjQ4io8XisvPhkT9ctmFDh+6UHmM2cponJehEIjPtliat1uyyRf5Rtad4TI7JpRpPD+UTmCHO/iVlPc2xF9BZR0YN2pyhBFfcmm3/DMhwLaGDPBZPQFyWz7Ayz3woEG4HyYIIUc0xFYbu0OzS5iWx7jOCctDfQUsgpsHCEZARj3yKF6LITtpbKNtfMPr7P7Xo1JEOOZdHvWc/LTmSJMOyjQGNJQnI9dJx+Y6tQV23ENahuulE1X+jCTdjijVuFkOlvTVtCtP0YDOJlcv37nHsy9QmVjbtyLqrXZpcWxE6M515OJMVs67fk9jdU3B6twSZO/hYMw+YiEnWOp4eT7dmwhY+BuI4bSMPl4NAepiFSI5/xSTWfhF0MQfyqftztMOulSI6IRy5JhGpqJ1YDWikiLBY4QioLb3K5JvhLV62wPK08TUiI/ioD3QkBR2 X-Forefront-Antispam-Report: CIP:216.228.118.233; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc7edge2.nvidia.com; CAT:NONE; SFS:(13230040)(7416014)(376014)(82310400026)(36860700013)(1800799024); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Aug 2024 17:01:48.8243 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b86d6145-9f75-49c6-e149-08dcc6b9f0b3 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.118.233]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9CD.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB7896 Use IOMMU_VIOMMU_TYPE_DEFAULT to cover the new IOMMU_VIOMMU_ALLOC ioctl. Signed-off-by: Nicolin Chen --- tools/testing/selftests/iommu/iommufd.c | 35 +++++++++++++++++++ tools/testing/selftests/iommu/iommufd_utils.h | 28 +++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index 6343f4053bd4..5c770e94f299 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -554,6 +554,41 @@ TEST_F(iommufd_ioas, alloc_hwpt_nested) } } +TEST_F(iommufd_ioas, viommu_default) +{ + uint32_t dev_id = self->device_id; + uint32_t viommu_id = 0; + uint32_t hwpt_id = 0; + + if (dev_id) { + /* Negative test -- invalid hwpt */ + test_err_viommu_alloc(ENOENT, dev_id, hwpt_id, + IOMMU_VIOMMU_TYPE_DEFAULT, &viommu_id); + + /* Negative test -- not a nested parent hwpt */ + test_cmd_hwpt_alloc(dev_id, self->ioas_id, 0, &hwpt_id); + test_err_viommu_alloc(EINVAL, dev_id, hwpt_id, + IOMMU_VIOMMU_TYPE_DEFAULT, &viommu_id); + test_ioctl_destroy(hwpt_id); + + /* Allocate a nested parent HWP */ + test_cmd_hwpt_alloc(dev_id, self->ioas_id, + IOMMU_HWPT_ALLOC_NEST_PARENT, + &hwpt_id); + /* Negative test -- unsupported viommu type */ + test_err_viommu_alloc(EOPNOTSUPP, dev_id, hwpt_id, + 0xdead, &viommu_id); + /* Allocate a default type of viommu */ + test_cmd_viommu_alloc(dev_id, hwpt_id, + IOMMU_VIOMMU_TYPE_DEFAULT, &viommu_id); + test_ioctl_destroy(viommu_id); + test_ioctl_destroy(hwpt_id); + } else { + test_err_viommu_alloc(ENOENT, dev_id, hwpt_id, + IOMMU_VIOMMU_TYPE_DEFAULT, &viommu_id); + } +} + TEST_F(iommufd_ioas, hwpt_attach) { /* Create a device attached directly to a hwpt */ diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index 40f6f14ce136..307d097db9dd 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -762,3 +762,31 @@ static int _test_cmd_trigger_iopf(int fd, __u32 device_id, __u32 fault_fd) #define test_cmd_trigger_iopf(device_id, fault_fd) \ ASSERT_EQ(0, _test_cmd_trigger_iopf(self->fd, device_id, fault_fd)) + +static int _test_cmd_viommu_alloc(int fd, __u32 device_id, __u32 hwpt_id, + __u32 type, __u32 flags, __u32 *viommu_id) +{ + struct iommu_viommu_alloc cmd = { + .size = sizeof(cmd), + .flags = flags, + .type = type, + .dev_id = device_id, + .hwpt_id = hwpt_id, + }; + int ret; + + ret = ioctl(fd, IOMMU_VIOMMU_ALLOC, &cmd); + if (ret) + return ret; + if (viommu_id) + *viommu_id = cmd.out_viommu_id; + return 0; +} + +#define test_cmd_viommu_alloc(device_id, hwpt_id, type, viommu_id) \ + ASSERT_EQ(0, _test_cmd_viommu_alloc(self->fd, device_id, hwpt_id, \ + type, 0, viommu_id)) +#define test_err_viommu_alloc(_errno, device_id, hwpt_id, type, viommu_id) \ + EXPECT_ERRNO(_errno, _test_cmd_viommu_alloc(self->fd, device_id, \ + hwpt_id, type, 0, \ + viommu_id)) From patchwork Tue Aug 27 16:59:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 822910 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2062.outbound.protection.outlook.com [40.107.237.62]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1B5C21D27AC; Tue, 27 Aug 2024 17:02:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.62 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778123; cv=fail; b=E5g5t8aPJ6teLeBn79IFzq6PMApbsudBwBMY6TnN+JMzaugFdJ0qY+phzL66En1nwZlbItGZHiNv9jN31xa8AgJ7R14gF7uvvMH1UVx9KTknQm1Tgz8qRjK5RDs2B3UeBjgrziDW/i+QA8XP+HjmxhnqiYPAGVumFPnjt6xdMvI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778123; c=relaxed/simple; bh=1dLSKUz9cK0Tk2PP60sO4XwJ8F7R7l5nrqEqvYg4PDI=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=mIL/uDBSDDWGOQ1sbFNf5z1Bok5rk/SPVLkMTTWbRwjZ90NxTWl98jlfcBSJ8fQDrbHlnJXElxC706/YDSM+/E9pED5xIy8/wCoDUs3qCpsaJeNbx3KEnJYr9KRQqNCabtprsGa8dqfcCwa46YYOH6dtFm4rIxrrJAiCslUIwKk= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=Vw2wDmty; arc=fail smtp.client-ip=40.107.237.62 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="Vw2wDmty" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=YivpvPxnpR849GG9eGSLRsKW+t3Hsc1f6Sq4pKkP8vG5/9k64Q17o1ly5jslbbrhY0hFE+lo38T4H9iv79Vk6kxvg6WTL7zTcbs9AQ7xiMqa2TcpjKMYjmE6JzuvgX7hH8Bshcf2UYzK6Hee1NsleOwizVhqblScZJfQHdKMO3R0h+wIVBMRBm8e+Dn6kaL1gpS41OE27oFAiU621lrUtKXfIPK8AxY5Ho2GpeFSdlxfMJVUR08Y1wSwBx7WIYE4dn37BZyJPPZT9p9d20OIw1KhQRL/Sn3+Vns9X9YNQZOaIfy7tGwpJdwpar4XU6S5aivoTRzBdstlQmhu5OYv0Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=LEXfljgMHZ5gX2bgJxGX7K1I4JK7NDnLdML1lryI6L4=; b=JTHPwhk6PtlBtuotyy7kndc3/Gxd2vY4r+Gts+3kow8McxJXahmiyb9Bpj7GfVvvLV6upa7QdYNDZ7qX4AEKvt234Gg0oYh0JmlESD3Y0nq34Pneo+v7wyWehuI/djZbzDKWvgBay8Upst9ljs1EraVE+2DcaiYsI3dybLoCZLF9uKPSxFEvqwJh9Dj0TuE6Eq2PFIBwm8GuWHKdNVEPFSoMwu5scyOrmt9u4gVD51A17dpwXKyx3YkihWYP//6AZimmEuZk+0n15ogHYtz9YV3g+4gB0+GIHGlB/uAiUkh1LfUlZX5kfWFfAGO407nF1RurzTucF4ZMOUVBPXWhkQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.232) smtp.rcpttodomain=huawei.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=LEXfljgMHZ5gX2bgJxGX7K1I4JK7NDnLdML1lryI6L4=; b=Vw2wDmty3E7wRbqO18adkCzP/0KlkYmmW4pO4WKn5ebNmcJuiXQRSgaMu+YX2PocXxViu/xpyjj7lY58/MBSzadosQqYIms1L5d/KDH+OD4cpXJHMDTJPlmIgTExVe0SJ4mMIhNGQu/DbmJnVBmRjsQgmtXinq1Jh3CNHZf82QZGdK/R/J2T04FAwlbYddJYE7jUSWORG6TUDmYN15YroDxRZrdBEdrxOLwxhojeP8L5E9yiubVWGVLnXN2pWuvixa/cgNxQCnrGyjH7p4o2GQZ51IYebJrpSwh35dQmUr8jiFetgbWHdKxiLFMYk0jEM5WB7Tpg5/c5KWbOIAtmDQ== Received: from PH0PR07CA0104.namprd07.prod.outlook.com (2603:10b6:510:4::19) by CH3PR12MB8511.namprd12.prod.outlook.com (2603:10b6:610:15c::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.26; Tue, 27 Aug 2024 17:01:55 +0000 Received: from SN1PEPF000397B4.namprd05.prod.outlook.com (2603:10b6:510:4:cafe::a9) by PH0PR07CA0104.outlook.office365.com (2603:10b6:510:4::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.25 via Frontend Transport; Tue, 27 Aug 2024 17:01:54 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.232) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.232 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.232; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.232) by SN1PEPF000397B4.mail.protection.outlook.com (10.167.248.58) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.13 via Frontend Transport; Tue, 27 Aug 2024 17:01:53 +0000 Received: from drhqmail202.nvidia.com (10.126.190.181) by mail.nvidia.com (10.127.129.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:38 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail202.nvidia.com (10.126.190.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:38 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Tue, 27 Aug 2024 10:01:37 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , Subject: [PATCH v2 06/19] iommufd/viommu: Add IOMMU_VIOMMU_SET/UNSET_VDEV_ID ioctl Date: Tue, 27 Aug 2024 09:59:43 -0700 Message-ID: <6348cc7a72ce9f2ac0e9caf9737e70177a01eb74.1724776335.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SN1PEPF000397B4:EE_|CH3PR12MB8511:EE_ X-MS-Office365-Filtering-Correlation-Id: 9d3ab3d4-8552-4af2-7c6d-08dcc6b9f392 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|376014|7416014|1800799024|36860700013|82310400026; X-Microsoft-Antispam-Message-Info: 7NNo0A4BJrpWX0blTiwkun9NdIW94XBP3kFjugZxhSwXfOwjrhLzJK7vlZP+O9tR+mAsGHt0YYUuDhZl5jod2L1EU6/Kq2FHAQ4Kz+gb+jgvAdHqJgJDnklcokttfkBNgldvGz5jTaVTORMqWyh+Dwaf/V+kP3sAe68GjSfEXtmcecMkIn7+aMiI1Z1W0j42Ys9ndLFw+OKvOrPRZLd/iF+9C2M3yxrfT6Cp5VbzviUy2pT3HRs3iNjy6ZZnW9WxP0rYRgW6Ql1C4NfpnWzJ6J9vNtLjG3YiJbU+G4dJ23Necwlenesw7xXeCUsuJp5TvwMZyjHHBK6YHGnQt3BmQjKQlzfVlxVu1pnK8SsSWUAAWOX0czAAnENAxb7NA+Dy2D281V8hcAwbGwc3aFAJl1PpF4l5Z6gxzHWUypBFB0NYFr4fLjxfL6pYQg+La5OUaR2YnlS/vMV0Rfed1HNU/CZDvg7/SiaVsUv7QPqZpLI7rKjXfUXvqxjuGt3N1+KU+LV4+KclAqIkU38jp2ExDD7v19x/AtzbY5fTc4S8XdHeOd/HkAjHBzYvdORpVnSCg8ca/HauXaQsWtQOq9wQ/JtQnS6D09aSa4088pyBj7XiySl05TgMXYX8Jkz8wxZwUcIDgC01NwjM7TtuGzaggNb3JVU9EEvncyky93wIx9RHAZSRdODArU5Soqf5rWQcwy1L3ymcN4kkQ6gdeaM38BHCpK88K/UATdehj53Rf65x9adHQ26MYPTPn34MnFENGnoVW/7Cji1XqeN0+VROWyB9RpDQLrR94ftSfBwnJ6dE7KSMx7eKWIZBYqay5BsW6cIAEJRpC/f0heOY4E6XXKmDbT3QxbNHQN6ut67w05OTLE9XStfW3E81wdQCNu0R3t/2bBRXVnI4u0M+LKYrBNrgChAOlH01nu0JKuQrxm7ifRkX009jq0QQ1PFnX025P8ziq4f90DuyVLlZMhm4lGOVxEHfaEPTBhBkf65PVbfOqJZGfK8rH9429/cEGXyXdF3rsIYeWnFygmfqjcvfMt0gERtxYDffjFjmItlwzIqtpFovzEIUnhdxSqTbvPZZuCKfV+8JekSuQX/g0Cgf0aG2M23lhwUjnKSgsy5BdWKzCL4p8J3n7JWywDtGvu8scgtfrmSBbzayHm2KTmgA5tLa5HunpyWnI//r0M6l+Rr5yKXWPG1Ig70B/+sg7ImCYXL1Q6Q3qwUJ6ho+S1kyRuXXr2LcqL2KUYhLygFisuWWQPA618BICE5ysX81l3KxJMU/roJ3i4FgtXGCskNRNqc2n9rjxH+EOMLRaDQAbRw/NLahiD2g1WJ4Z6gCKJXuUdbrYejcnwc1pVB1aePuKGr0djGMy6IQlgwXsvfBG4ge31qFdoK2IX59x0VGDVBpjwMRVfF7ze6c0lrDfr90yj1c5pfq68nWj5Q+jNzw/BqAQGKYR5YK0JV818P6dc/3 X-Forefront-Antispam-Report: CIP:216.228.118.232; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc7edge1.nvidia.com; CAT:NONE; SFS:(13230040)(376014)(7416014)(1800799024)(36860700013)(82310400026); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Aug 2024 17:01:53.6230 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9d3ab3d4-8552-4af2-7c6d-08dcc6b9f392 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.118.232]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: SN1PEPF000397B4.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR12MB8511 Introduce a pair of new ioctls to set/unset a per-viommu virtual device id that should be linked to a physical device id via an idev pointer. Continue the support IOMMU_VIOMMU_TYPE_DEFAULT for a core-managed viommu. Provide a lookup function for drivers to load device pointer by a virtual device id. Add a rw_semaphore protection around the vdev_id list. Any future ioctl handlers that potentially access the list must grab the lock too. Signed-off-by: Nicolin Chen --- drivers/iommu/iommufd/device.c | 12 +++ drivers/iommu/iommufd/iommufd_private.h | 21 ++++ drivers/iommu/iommufd/main.c | 6 ++ drivers/iommu/iommufd/viommu.c | 121 ++++++++++++++++++++++++ include/uapi/linux/iommufd.h | 40 ++++++++ 5 files changed, 200 insertions(+) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 5fd3dd420290..3ad759971b32 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -136,6 +136,18 @@ void iommufd_device_destroy(struct iommufd_object *obj) struct iommufd_device *idev = container_of(obj, struct iommufd_device, obj); + /* Unlocked since there should be no race in a destroy() */ + if (idev->vdev_id) { + struct iommufd_vdev_id *vdev_id = idev->vdev_id; + struct iommufd_viommu *viommu = vdev_id->viommu; + struct iommufd_vdev_id *old; + + old = xa_cmpxchg(&viommu->vdev_ids, vdev_id->id, vdev_id, NULL, + GFP_KERNEL); + WARN_ON(old != vdev_id); + kfree(vdev_id); + idev->vdev_id = NULL; + } iommu_device_release_dma_owner(idev->dev); iommufd_put_group(idev->igroup); if (!iommufd_selftest_is_mock_dev(idev->dev)) diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 1f2a1c133b9a..2c6e168c5300 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -416,6 +416,7 @@ struct iommufd_device { struct iommufd_object obj; struct iommufd_ctx *ictx; struct iommufd_group *igroup; + struct iommufd_vdev_id *vdev_id; struct list_head group_item; /* always the physical device */ struct device *dev; @@ -533,11 +534,31 @@ struct iommufd_viommu { struct iommufd_ctx *ictx; struct iommufd_hwpt_paging *hwpt; + /* The locking order is vdev_ids_rwsem -> igroup::lock */ + struct rw_semaphore vdev_ids_rwsem; + struct xarray vdev_ids; + unsigned int type; }; +struct iommufd_vdev_id { + struct iommufd_viommu *viommu; + struct iommufd_device *idev; + u64 id; +}; + +static inline struct iommufd_viommu * +iommufd_get_viommu(struct iommufd_ucmd *ucmd, u32 id) +{ + return container_of(iommufd_get_object(ucmd->ictx, id, + IOMMUFD_OBJ_VIOMMU), + struct iommufd_viommu, obj); +} + int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd); void iommufd_viommu_destroy(struct iommufd_object *obj); +int iommufd_viommu_set_vdev_id(struct iommufd_ucmd *ucmd); +int iommufd_viommu_unset_vdev_id(struct iommufd_ucmd *ucmd); #ifdef CONFIG_IOMMUFD_TEST int iommufd_test(struct iommufd_ucmd *ucmd); diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 288ee51b6829..199ad90fa36b 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -334,6 +334,8 @@ union ucmd_buffer { struct iommu_option option; struct iommu_vfio_ioas vfio_ioas; struct iommu_viommu_alloc viommu; + struct iommu_viommu_set_vdev_id set_vdev_id; + struct iommu_viommu_unset_vdev_id unset_vdev_id; #ifdef CONFIG_IOMMUFD_TEST struct iommu_test_cmd test; #endif @@ -387,6 +389,10 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { __reserved), IOCTL_OP(IOMMU_VIOMMU_ALLOC, iommufd_viommu_alloc_ioctl, struct iommu_viommu_alloc, out_viommu_id), + IOCTL_OP(IOMMU_VIOMMU_SET_VDEV_ID, iommufd_viommu_set_vdev_id, + struct iommu_viommu_set_vdev_id, vdev_id), + IOCTL_OP(IOMMU_VIOMMU_UNSET_VDEV_ID, iommufd_viommu_unset_vdev_id, + struct iommu_viommu_unset_vdev_id, vdev_id), #ifdef CONFIG_IOMMUFD_TEST IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last), #endif diff --git a/drivers/iommu/iommufd/viommu.c b/drivers/iommu/iommufd/viommu.c index 200653a4bf57..8ffcd72b16b8 100644 --- a/drivers/iommu/iommufd/viommu.c +++ b/drivers/iommu/iommufd/viommu.c @@ -8,6 +8,15 @@ void iommufd_viommu_destroy(struct iommufd_object *obj) { struct iommufd_viommu *viommu = container_of(obj, struct iommufd_viommu, obj); + struct iommufd_vdev_id *vdev_id; + unsigned long index; + + xa_for_each(&viommu->vdev_ids, index, vdev_id) { + /* Unlocked since there should be no race in a destroy() */ + vdev_id->idev->vdev_id = NULL; + kfree(vdev_id); + } + xa_destroy(&viommu->vdev_ids); refcount_dec(&viommu->hwpt->common.obj.users); } @@ -53,6 +62,9 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) viommu->ictx = ucmd->ictx; viommu->hwpt = hwpt_paging; + xa_init(&viommu->vdev_ids); + init_rwsem(&viommu->vdev_ids_rwsem); + refcount_inc(&viommu->hwpt->common.obj.users); cmd->out_viommu_id = viommu->obj.id; @@ -70,3 +82,112 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) iommufd_put_object(ucmd->ictx, &idev->obj); return rc; } + +int iommufd_viommu_set_vdev_id(struct iommufd_ucmd *ucmd) +{ + struct iommu_viommu_set_vdev_id *cmd = ucmd->cmd; + struct iommufd_vdev_id *vdev_id, *curr; + struct iommufd_viommu *viommu; + struct iommufd_device *idev; + int rc = 0; + + if (cmd->vdev_id > ULONG_MAX) + return -EINVAL; + + viommu = iommufd_get_viommu(ucmd, cmd->viommu_id); + if (IS_ERR(viommu)) + return PTR_ERR(viommu); + + idev = iommufd_get_device(ucmd, cmd->dev_id); + if (IS_ERR(idev)) { + rc = PTR_ERR(idev); + goto out_put_viommu; + } + + down_write(&viommu->vdev_ids_rwsem); + mutex_lock(&idev->igroup->lock); + if (idev->vdev_id) { + rc = -EEXIST; + goto out_unlock_igroup; + } + + vdev_id = kzalloc(sizeof(*vdev_id), GFP_KERNEL); + if (!vdev_id) { + rc = -ENOMEM; + goto out_unlock_igroup; + } + + vdev_id->idev = idev; + vdev_id->viommu = viommu; + vdev_id->id = cmd->vdev_id; + + curr = xa_cmpxchg(&viommu->vdev_ids, cmd->vdev_id, NULL, vdev_id, + GFP_KERNEL); + if (curr) { + rc = xa_err(curr) ? : -EBUSY; + goto out_free; + } + + idev->vdev_id = vdev_id; + goto out_unlock_igroup; + +out_free: + kfree(vdev_id); +out_unlock_igroup: + mutex_unlock(&idev->igroup->lock); + up_write(&viommu->vdev_ids_rwsem); + iommufd_put_object(ucmd->ictx, &idev->obj); +out_put_viommu: + iommufd_put_object(ucmd->ictx, &viommu->obj); + return rc; +} + +int iommufd_viommu_unset_vdev_id(struct iommufd_ucmd *ucmd) +{ + struct iommu_viommu_unset_vdev_id *cmd = ucmd->cmd; + struct iommufd_viommu *viommu; + struct iommufd_vdev_id *old; + struct iommufd_device *idev; + int rc = 0; + + if (cmd->vdev_id > ULONG_MAX) + return -EINVAL; + + viommu = iommufd_get_viommu(ucmd, cmd->viommu_id); + if (IS_ERR(viommu)) + return PTR_ERR(viommu); + + idev = iommufd_get_device(ucmd, cmd->dev_id); + if (IS_ERR(idev)) { + rc = PTR_ERR(idev); + goto out_put_viommu; + } + + down_write(&viommu->vdev_ids_rwsem); + mutex_lock(&idev->igroup->lock); + if (!idev->vdev_id) { + rc = -ENOENT; + goto out_unlock_igroup; + } + if (idev->vdev_id->id != cmd->vdev_id) { + rc = -EINVAL; + goto out_unlock_igroup; + } + + old = xa_cmpxchg(&viommu->vdev_ids, idev->vdev_id->id, + idev->vdev_id, NULL, GFP_KERNEL); + if (xa_is_err(old)) { + rc = xa_err(old); + goto out_unlock_igroup; + } + kfree(old); + idev->vdev_id = NULL; + +out_unlock_igroup: + mutex_unlock(&idev->igroup->lock); + up_write(&viommu->vdev_ids_rwsem); + iommufd_put_object(ucmd->ictx, &idev->obj); +out_put_viommu: + iommufd_put_object(ucmd->ictx, &viommu->obj); + return rc; +} diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 51ce6a019c34..1816e89c922d 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -52,6 +52,8 @@ enum { IOMMUFD_CMD_HWPT_INVALIDATE = 0x8d, IOMMUFD_CMD_FAULT_QUEUE_ALLOC = 0x8e, IOMMUFD_CMD_VIOMMU_ALLOC = 0x8f, + IOMMUFD_CMD_VIOMMU_SET_VDEV_ID = 0x90, + IOMMUFD_CMD_VIOMMU_UNSET_VDEV_ID = 0x91, }; /** @@ -882,4 +884,42 @@ struct iommu_viommu_alloc { __u32 out_viommu_id; }; #define IOMMU_VIOMMU_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VIOMMU_ALLOC) + +/** + * struct iommu_viommu_set_vdev_id - ioctl(IOMMU_VIOMMU_SET_VDEV_ID) + * @size: sizeof(struct iommu_viommu_set_vdev_id) + * @viommu_id: viommu ID to associate with the device to store its virtual ID + * @dev_id: device ID to set its virtual ID + * @__reserved: Must be 0 + * @vdev_id: Virtual device ID + * + * Set a viommu-specific virtual ID of a device + */ +struct iommu_viommu_set_vdev_id { + __u32 size; + __u32 viommu_id; + __u32 dev_id; + __u32 __reserved; + __aligned_u64 vdev_id; +}; +#define IOMMU_VIOMMU_SET_VDEV_ID _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VIOMMU_SET_VDEV_ID) + +/** + * struct iommu_viommu_unset_vdev_id - ioctl(IOMMU_VIOMMU_UNSET_VDEV_ID) + * @size: sizeof(struct iommu_viommu_unset_vdev_id) + * @viommu_id: viommu ID associated with the device to delete its virtual ID + * @dev_id: device ID to unset its virtual ID + * @__reserved: Must be 0 + * @vdev_id: Virtual device ID (for verification) + * + * Unset a viommu-specific virtual ID of a device + */ +struct iommu_viommu_unset_vdev_id { + __u32 size; + __u32 viommu_id; + __u32 dev_id; + __u32 __reserved; + __aligned_u64 vdev_id; +}; +#define IOMMU_VIOMMU_UNSET_VDEV_ID _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VIOMMU_UNSET_VDEV_ID) #endif From patchwork Tue Aug 27 16:59:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 822912 Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam02on2061.outbound.protection.outlook.com [40.107.212.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 131761D1F55; Tue, 27 Aug 2024 17:01:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.212.61 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778121; cv=fail; b=ZphVWDQAFoUaQQYHDOMYVuPd+d4zLd5exPkdAUoJyJ6Zw9TzAZTnB7k9B7X7egCK5q/AzAvBY+d7JGQdU7GPWInbsB+jQisV1MWqhHp0iv7XYI6ByPrL6NvwEyVQ1hlE5o5kss85PIbyrcs8+NMyi6UpTP/9dGsTWTiTTdtVklQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778121; c=relaxed/simple; bh=hdrvHo+cpmncv0gLtnV1LzMlee7mCa2eLbx+7qHMU7k=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=tTIJNzgKDEc8HENTG7a4BRJXkfkJd/91ZphukMXZ1yquhqHOuWlFkYx31DYho/mM6Umcu76Vov0lOvvnKa2VfmpGuKbsPXgXenirmfh1aVc8JPwqPWG3aonLHqWF936FY0R79ltOrXLX4g6klouq57h5ihcxDch2Zpjcm2YA/+M= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=B2wtJs6w; arc=fail smtp.client-ip=40.107.212.61 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="B2wtJs6w" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=DhcWQhBOR4K17jbmqrLRrXPsZy5JOiqpMoFcP07dCTg7dtDUM6qoT3wz0JRJu+aac+54Oev5IdFJkPHhmqEkcEztInhqCR23br75rfZK39LStJYCjfKaSprF5lal6DcNyxUT5C0e94yS9ASkcMq86h9gDZNbWpSOST9MHF64HKFM0BWlASIF84dIpECzDEYiLJMd8ZwRGydrEgvn16bipgdHsP0dYySsI0Kjm0P0ffMnNJVHKzIg+iQPBrxVA40wLWYwrLldKW3C4GFmqFTI4zyXjEn3w+MyHSbxeNdtp899UqwteRFYd0L6cRoBTrrNp+PDbHBu7WxnEkF/fJCY5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=psrH7MS6rlNiHBO9Z0q+XH9RD0CwC7/PxtOF5kAMoFM=; b=RsUnAYq39t7Ci49B6xtUGZKwoUPbyTkwFThYUDOAOOZJQLI35HBDY6PiJMjGUXQUu7w53g3GT2KE2preNW62Hn2a0RhPea4UFTLxuWbC9mxtSYUAO7mj0IWYgQxMs50ft3U6srG0aWYKrJB30UopJ34a2Go/whTDV9R0zVl0O6NGThLBttPM+U2l3nQgxNRthkHf03d2OYgEKd1V+mWyxsq5Gl1o6B6tRnG/yy0V5zY8CMIjxgzi0NXr9CGv8BNFB//NVVwYveTNTf/HmCLLEU5q6Bi6LOZ9usSMkEbSy/EpccvvHLtvX2yTkWIUj18k/6DM4Gl/0pTnvqAVEOOz8w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.233) smtp.rcpttodomain=huawei.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=psrH7MS6rlNiHBO9Z0q+XH9RD0CwC7/PxtOF5kAMoFM=; b=B2wtJs6wPBFA4ZG0ABhK09Ou3pnqfaKJMoR1pYzPph0XBO9W2teE/Kd1z8W+06DDrniDsMzTH1x4HJxnq8MMzMqPRVlcipmsXcglvCFm+kqLUk6i0MLn144M3JYz0VcaiUda+OpxE2WPZ0e+XevqZ2/lqRab+DUr8O0JShvvHRImvJ+VVDMMBbDPxrWb+LCdTFWKmEnpt/zN+iYtexIuOs73ESOglJbVg+RjMpQtDPDrJJ1A5Q16kduYXN6r2F7qcN3xMf+p63u+XZbG6Y7nmOqXMZld36S5IXul21/HUfcnvTt5wGCV6B593QtDy/yMfvdhDU7bvP9rmEHXl9Olow== Received: from PH8PR07CA0005.namprd07.prod.outlook.com (2603:10b6:510:2cd::29) by CH2PR12MB4231.namprd12.prod.outlook.com (2603:10b6:610:7d::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.26; Tue, 27 Aug 2024 17:01:53 +0000 Received: from CY4PEPF0000E9CD.namprd03.prod.outlook.com (2603:10b6:510:2cd:cafe::ae) by PH8PR07CA0005.outlook.office365.com (2603:10b6:510:2cd::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7784.18 via Frontend Transport; Tue, 27 Aug 2024 17:01:44 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.233) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.233 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.233; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.233) by CY4PEPF0000E9CD.mail.protection.outlook.com (10.167.241.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.11 via Frontend Transport; Tue, 27 Aug 2024 17:01:53 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by mail.nvidia.com (10.127.129.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:39 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail203.nvidia.com (10.126.190.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:39 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Tue, 27 Aug 2024 10:01:38 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , Subject: [PATCH v2 07/19] iommufd/selftest: Add IOMMU_VIOMMU_SET/UNSET_VDEV_ID test coverage Date: Tue, 27 Aug 2024 09:59:44 -0700 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9CD:EE_|CH2PR12MB4231:EE_ X-MS-Office365-Filtering-Correlation-Id: c80cfed1-e71e-48c0-d5a9-08dcc6b9f351 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|82310400026|36860700013|376014|7416014|1800799024; X-Microsoft-Antispam-Message-Info: Z82I4EHS+AMpnzsCJToIVmpTxZaFegM1Y7fV4VZAe7eoVMbT912iAtqvehZS6JPSnhkJddbzEIyrSXtiRX003AvR50gEgfMkEu5/Jgs+SzuflIPYzZVNxDYTciBxK3nPgScCYocc6q5uHNmTGOKyoKbE+oeEjvQMv6Vhnxx9pLnBJB7FJ5oKZ5OUki7jBelFgHXyySvb+H5853NVbRNohH3ey5XkX4x6UqZy5leLWPZFd590EGCC3eL6gz6r7cRxeMemSPRC9n5JnGEmri0C4dH1fzpeciu8/umX3qKhaWDubeChIy5PsQx1d5m/6iibKustGy8N9iK3f9QveCrSDdDldtPbRfkAB/+cLkA4jmFnXDeyNVUDDpI+nTL8Qi2NTTPNORPAqh683Z0nybJADvxABovLsvP6Mp+DhE2SAGoMhVlx3tOq9QvbljuwdzHmJnIXRYLNapZappY1QdotH/RM9QHMlNBsiJRRXH5FzQBfPYC8baM0rkiBDsllFjHvmafELExJ3EmWJGv3i8xM9skfEZDIoTcemDfRgRy0FYpqalrfzvJleiJtKuampURO8qbRx30S+T+cVGmBJ4mui1ZU+E9mgJrT0ntjV/qalABGm898Et4xH+AHEcnMvCUm6zIEqfuVqOleabtuMmFjpUp4ejMSFE6xbDDKjhqMKRiNgWgx3xYPXomJ22+yZNBUYuLljPalG4cQKmo7EK8f/mDk/txj6gqaVR+FAF388B8tpdDWK1deyP2kotiopc+MjP/goKapPnt36y43KhTBd/NImsUubFGrrR435fFcSigdGWiZvdJ6B8Utu8f/rHO3QN4ueQzimmFxFL46Ap9BEMuCr3hpuq4au7VHQsVNt0Fxm6hSh1GJkm6ieTP6G1lGt9bOYz3uqsFOzCUTlvUfMv8PDpyb8q1gCYaHPRS7vNNQzjPCH8pl2cDHOGuu95k3sbYptKENcYesqEicmMqWQKyDIB6ySww5mU9gGYnugKhHh7nHGhNbqlx+1P7rMT3uV9J6Ow0mKw9rG9DDK0ZpAxkptCMzLa2qT+U9UqCVAmBv+tzZ030Ik2W7EUNoCQCu/STkoRMyQzw4i+8iJKm9HYxUwGz57Rka9LVzBXmuIi0CiKim6QWwJuEm+3Iu1/HZvJL5r9T0nxELmH7xW0yWNYA2UOvua3Q9CrPCvsDm/6rV9ChPVjFgNjntOG0IB2B+VOApZmieLkQNV7PjBG+iyiWsDASc7O5aOvejuOscmVGnlzwRLIYyP9UoGaWtEW/rST7bnr9flpOzlWYC1VHuOBRLiPDvuKRLHJcI10jBWOi/pcNVrDaY4qQGTV8X7L3r6YuYQvBIstqxvVnYzWL90tDDV3BHB2c2a4myZVd2MW8V6o+cngGJf/IzoI1VghhgBK0Lqcm+cOwZ3D8E9ooDP8984iPDgauwQcnimD86qpkPCglJLS/Hv21RzGV0C9Gd X-Forefront-Antispam-Report: CIP:216.228.118.233; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc7edge2.nvidia.com; CAT:NONE; SFS:(13230040)(82310400026)(36860700013)(376014)(7416014)(1800799024); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Aug 2024 17:01:53.2150 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c80cfed1-e71e-48c0-d5a9-08dcc6b9f351 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.118.233]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9CD.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4231 A core-managed VIOMMU maintains an xarray to store a list of virtual ids to mock_devs. Add test cases to cover the new IOMMU_VIOMMU_SET/UNSET_VDEV_ID ioctls. Signed-off-by: Nicolin Chen --- tools/testing/selftests/iommu/iommufd.c | 27 +++++++++++- tools/testing/selftests/iommu/iommufd_utils.h | 42 +++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index 5c770e94f299..f383f3bc7c8b 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -556,9 +556,12 @@ TEST_F(iommufd_ioas, alloc_hwpt_nested) TEST_F(iommufd_ioas, viommu_default) { + struct iommu_hwpt_selftest data = { + .iotlb = IOMMU_TEST_IOTLB_DEFAULT, + }; + uint32_t nested_hwpt_id = 0, hwpt_id = 0; uint32_t dev_id = self->device_id; uint32_t viommu_id = 0; - uint32_t hwpt_id = 0; if (dev_id) { /* Negative test -- invalid hwpt */ @@ -575,17 +578,37 @@ TEST_F(iommufd_ioas, viommu_default) test_cmd_hwpt_alloc(dev_id, self->ioas_id, IOMMU_HWPT_ALLOC_NEST_PARENT, &hwpt_id); + test_cmd_mock_domain_replace(self->stdev_id, hwpt_id); + /* Negative test -- unsupported viommu type */ test_err_viommu_alloc(EOPNOTSUPP, dev_id, hwpt_id, 0xdead, &viommu_id); - /* Allocate a default type of viommu */ + + /* Allocate a default type of viommu and a nested hwpt on top */ test_cmd_viommu_alloc(dev_id, hwpt_id, IOMMU_VIOMMU_TYPE_DEFAULT, &viommu_id); + test_cmd_hwpt_alloc_nested(self->device_id, viommu_id, 0, + &nested_hwpt_id, + IOMMU_HWPT_DATA_SELFTEST, &data, + sizeof(data)); + test_cmd_mock_domain_replace(self->stdev_id, nested_hwpt_id); + + /* Set vdev_id to 0x99, unset it, and set to 0x88 */ + test_cmd_viommu_set_vdev_id(viommu_id, dev_id, 0x99); + test_err_viommu_set_vdev_id(EEXIST, viommu_id, dev_id, 0x99); + test_err_viommu_unset_vdev_id(EINVAL, viommu_id, dev_id, 0x88); + test_cmd_viommu_unset_vdev_id(viommu_id, dev_id, 0x99); + test_cmd_viommu_set_vdev_id(viommu_id, dev_id, 0x88); + + test_cmd_mock_domain_replace(self->stdev_id, hwpt_id); + test_ioctl_destroy(nested_hwpt_id); + test_cmd_mock_domain_replace(self->stdev_id, self->ioas_id); test_ioctl_destroy(viommu_id); test_ioctl_destroy(hwpt_id); } else { test_err_viommu_alloc(ENOENT, dev_id, hwpt_id, IOMMU_VIOMMU_TYPE_DEFAULT, &viommu_id); + test_err_viommu_set_vdev_id(ENOENT, viommu_id, dev_id, 0x99); } } diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index 307d097db9dd..be722ea88358 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -790,3 +790,45 @@ static int _test_cmd_viommu_alloc(int fd, __u32 device_id, __u32 hwpt_id, EXPECT_ERRNO(_errno, _test_cmd_viommu_alloc(self->fd, device_id, \ hwpt_id, type, 0, \ viommu_id)) + +static int _test_cmd_viommu_set_vdev_id(int fd, __u32 viommu_id, + __u32 idev_id, __u64 vdev_id) +{ + struct iommu_viommu_set_vdev_id cmd = { + .size = sizeof(cmd), + .dev_id = idev_id, + .viommu_id = viommu_id, + .vdev_id = vdev_id, + }; + + return ioctl(fd, IOMMU_VIOMMU_SET_VDEV_ID, &cmd); +} + +#define test_cmd_viommu_set_vdev_id(viommu_id, idev_id, vdev_id) \ + ASSERT_EQ(0, _test_cmd_viommu_set_vdev_id(self->fd, viommu_id, \ + idev_id, vdev_id)) +#define test_err_viommu_set_vdev_id(_errno, viommu_id, idev_id, vdev_id) \ + EXPECT_ERRNO(_errno, \ + _test_cmd_viommu_set_vdev_id(self->fd, viommu_id, \ + idev_id, vdev_id)) + +static int _test_cmd_viommu_unset_vdev_id(int fd, __u32 viommu_id, + __u32 idev_id, __u64 vdev_id) +{ + struct iommu_viommu_unset_vdev_id cmd = { + .size = sizeof(cmd), + .dev_id = idev_id, + .viommu_id = viommu_id, + .vdev_id = vdev_id, + }; + + return ioctl(fd, IOMMU_VIOMMU_UNSET_VDEV_ID, &cmd); +} + +#define test_cmd_viommu_unset_vdev_id(viommu_id, idev_id, vdev_id) \ + ASSERT_EQ(0, _test_cmd_viommu_unset_vdev_id(self->fd, viommu_id, \ + idev_id, vdev_id)) +#define test_err_viommu_unset_vdev_id(_errno, viommu_id, idev_id, vdev_id) \ + EXPECT_ERRNO(_errno, \ + _test_cmd_viommu_unset_vdev_id(self->fd, viommu_id, \ + idev_id, vdev_id)) From patchwork Tue Aug 27 16:59:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 823342 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2079.outbound.protection.outlook.com [40.107.93.79]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0A3941D279A; Tue, 27 Aug 2024 17:01:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.93.79 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778121; cv=fail; b=VeveZ55S98382+lYQVPe2OYYee9f5SdhvyZlBd8HfJPXJcX7i2Ii8y2lHoh0xNu3x/TLHg/8eUZreDB1yhOBVVRgDSnLsDgusxicFQeRMI5BMfvk58a+iZzM0RdzmUYUfIvnx6pnAbK+BzF2FxsxERk8fNitl9UD9NT0E3CZrNA= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778121; c=relaxed/simple; bh=kobA1afTYmDPHJS9m1k0dinkRlUtTqL8Of0S2cpkGYA=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=kHHnqmDRsAy67g9TOD/TLcvt7JGVt5mTD7Ct0a1jcylVq/LR2EnivPNChujCgHLEQAcyNVmissN2MMCYmq95ee4NW/Oxr3ZCPvzO/D6KDG6jsMmcQ9MGtrlzelrAoxqsaI9gsO6LpLV4A+Fytp3bLp0Tcp/p7n+/CFMmpE+J4Jo= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=LhYZKu/F; arc=fail smtp.client-ip=40.107.93.79 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="LhYZKu/F" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=qcw6aNYIweJAGQwGtMzVeGm1//wMkW148/yQUAoUJGQKJ/FSSAAOiTs364wHyz4nD2K8o0xiS92xc0AXqQ3+KyWQDDNDDUX1Q8eERXlb9G8k8I27t50LforEPuBgJm1PGBujb1+9qlmi1MEU5EpUGxDIt/tz8gZ+rixhoz/pMPr9b4YUD3TZ0ksPlzoTUT+MH0Z1hqvWfGMyByvFM4OaXB8d68Up8kXkBhkDCMgWlqjLq6u+UMAZm7/qf2YZFUBJ4uiX9Bi0KysYzxFPpbZ/4iel9b7Y1horiB9nNwFBr3HcPfjKtiKDutEv9eyRSXdU69QEP3ZMhcLSHKgDRUDYOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=H/DAxjipE88O0r3lbewOow/hSZcf92YrldrW2X4I9q4=; b=iP5de6z7PkR3S03pKCNi5Txvs9Z3Qr7+RDGMb1xJuhgxwBR80r2V/Q5VHfL7yjdfd4U20SW7kHO/k0NLdGitaQNh0XZeGEYsx0tyfoDzVNHLegoqHuYamMJTg/kAM8QVp8fP1PPnSQdnVDbIOfnuSSE8IR/0uKfjplY3YVgdkMtw6BYWljrHma4uxmR06MIBOaxQogoEScpkLKc3J1noJ3SE1Yo8ShT8bQJ63d4xtxxE9mr4awme0+PyZWpgD8s2NfPbfc7kgmDQFTubkxdZReU5SNJGfx1JCQP9sSypVaF8GKhqY5ZKPHzaN0GzzaPyd7kQJSsdT5D+Fbf59jf/NA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.233) smtp.rcpttodomain=huawei.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=H/DAxjipE88O0r3lbewOow/hSZcf92YrldrW2X4I9q4=; b=LhYZKu/Fk1sXEuFeHHbmTbThakx7OH2lCqH5+tRrFY/RhtALtruuKRolhX7z0b2brp3vcciI/VgLNK6OC98tS/X1+q/gIaMwp/0B59xW1yQ/kAmwrujc/jmvYqLGy768jT8HLrhfwVbaw0csUx3SerySHMLYicCKXqlRY2p4nWQBkzGN5h3heszuE1HaFNhPBuYvSrvHZM6gAe2Aj3WUCVg+/8moU7b+w0jFYu8wkPYbJ6Cz30/+mMAqAM9ugy4bJwesDAa5PpR78scE8VQgXYISbkPAMcfmMB6tGPtikfssoPgAzYjxJbIPJPSPdTF2s6CsPGcq2DYlIPZTS9uvvQ== Received: from DS7P220CA0007.NAMP220.PROD.OUTLOOK.COM (2603:10b6:8:1ca::14) by SJ2PR12MB8782.namprd12.prod.outlook.com (2603:10b6:a03:4d0::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.26; Tue, 27 Aug 2024 17:01:54 +0000 Received: from CY4PEPF0000E9D3.namprd03.prod.outlook.com (2603:10b6:8:1ca:cafe::b2) by DS7P220CA0007.outlook.office365.com (2603:10b6:8:1ca::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.26 via Frontend Transport; Tue, 27 Aug 2024 17:01:54 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.233) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.233 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.233; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.233) by CY4PEPF0000E9D3.mail.protection.outlook.com (10.167.241.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.11 via Frontend Transport; Tue, 27 Aug 2024 17:01:54 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by mail.nvidia.com (10.127.129.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:41 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail203.nvidia.com (10.126.190.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:40 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Tue, 27 Aug 2024 10:01:39 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , Subject: [PATCH v2 08/19] iommufd/viommu: Add cache_invalidate for IOMMU_VIOMMU_TYPE_DEFAULT Date: Tue, 27 Aug 2024 09:59:45 -0700 Message-ID: <224732696abf91f220585bb26fa44314d7d2f425.1724776335.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D3:EE_|SJ2PR12MB8782:EE_ X-MS-Office365-Filtering-Correlation-Id: 553db148-648f-481e-df99-08dcc6b9f3e5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|36860700013|1800799024|82310400026|7416014|376014; X-Microsoft-Antispam-Message-Info: 5aRPw60qprdi3u8yowL/w4zz7BkBuAErqxrqKIcF55b6MfSamP4r+4ek/bAHLVbhpvMjfg9n1T3Ud9jF7q0zamtmMz50dQXDiJVaFR2uPorFZke+dIuLfyhNwDC8fxI+utafgf2qOqOYICLI5hcn2c+najR7ecKLD3erchi72MDS8eEdhZDCQMkzL2lI3UE8OsYjmkO+HUM1pJhd08UoetGYS+WhgIpegn7tuePKLruphzDGG//LG+sVXdJOSMxEGSuVTq+vOSDWqYoNg+tYiDseAud8BKf7Fe6VIKghNuUhgztkBr6atqVYCjndAXQV/Nt/H5SAUrcxuaxXmFR5xKuaLQMhRNL2qvZOXTqsHtDo4VM2dWATENpU+6iRQUQ+ytbOm2gcgcsDX1NzPH18pN7tJ2xikihQ5PsM7FZPXJsleLjXY5eqs42QxQ2kjfwa0kyR6lWwdR7NgH2JqZjZP0reawc31y+RrNq09jiIUmyfZLz9LcVrzprFJuqjmOAUHH3wvpX7Q8lp2zhbBsqLPTAhlzeGgGDp9bDMZOGpoF36i3z0B83M73ZvqTEJLvJZKtUSifnZLoOlQp3bjZCaWFdLSL75zfU1b4acXOfScmkMSdc1cZO1+xO0roDSMdFoP5hDos1PcjBJaVMXXiTeBhXLEHfpWUCh7M/wdEleztdnbXp3c0i4n7L+I4UfgOGSj7Z0VY0IM0OvPV45/gs7qHRU7UJnvzMmZmxAZM4uRZM0gHU8JmKWhdCdBEhSiTZ/0wcjkRecefIyvDyrZ/DLhS4gbsVwweSUtnmdB+DHj1fbHGwk1oR1uc180EMFbDzvJGcLMQ/xVw8Nhw/9Bewcd97bnw+3oU94bc3cDaAJghMPPdHXY/1RCk+9pEAoZFb3mhHZgrC87jawRRYwJsAgKYKm99tAmoa50imBo5L16NJhUTKc5zPQogJUTyGMUS3ZpNViYr7T1iBUX0W3Fa3k62hi0ZhfZjAaQ7zr/gPt2FQacC8q+qr0Trv778UO8mlJYbkO43EVS3PZxO4NPiuMN6dP30xKO7U9oqpqr3CyfPCzmtREUj/TW9NRRKgKRPXN3PaRq+Uxskz+MihADPaoTz7jA+qle+XFHM15ka5lyX9zKjKaF4QmhAYjqq0LR/bsanQVHee259ee0qRiOVYBuElbeyvtFK4dAEPnCIPvptDx5pyM0s+pBjfBwavGrKkfiGb1rqr+urQMxJaYZCOYpRuvt8QeEO8L65af2CQf7MpzfHY1Hl9x8l03dAok6jKRS86R3NgfcH4w0mIQs3z/6AFrAHWivQFdy8FMQ8lBb0CUdW3C0x1v/oeDyi8Zag4weQ+WTOZoB67eZxKcM2vDqDesr58oSlRuhTLrYSvkyEPV4EGNVEE/0pqWeqYytOyZV4blWuyHT9skJZb7bCbEAjlBl/wxlwViWC9FQ7TM6sPCwJxAnYRGAf8aWkwd80po X-Forefront-Antispam-Report: CIP:216.228.118.233; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc7edge2.nvidia.com; CAT:NONE; SFS:(13230040)(36860700013)(1800799024)(82310400026)(7416014)(376014); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Aug 2024 17:01:54.2320 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 553db148-648f-481e-df99-08dcc6b9f3e5 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.118.233]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D3.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB8782 Add a default_viommu_ops with a new op for cache invaldiation, similar to the cache_invalidate_user op in structure iommu_domain_ops, but wider. An IOMMU driver that allocated a nested domain with a core-managed viommu is able to use the same viommu pointer for this cache invalidation API. ARM SMMUv3 for example supports IOTLB and ATC device cache invaldiations. The IOTLB invalidation is per-VMID, held currently by a parent S2 domain. The ATC invalidation is per device (Stream ID) that should be tranlsated by a virtual device ID lookup table. Either case fits the viommu context. Signed-off-by: Nicolin Chen --- drivers/iommu/iommufd/iommufd_private.h | 3 +++ drivers/iommu/iommufd/viommu.c | 3 +++ include/linux/iommu.h | 5 +++++ include/linux/iommufd.h | 19 +++++++++++++++++++ 4 files changed, 30 insertions(+) diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 2c6e168c5300..7831b0ca6528 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -5,6 +5,7 @@ #define __IOMMUFD_PRIVATE_H #include +#include #include #include #include @@ -538,6 +539,8 @@ struct iommufd_viommu { struct rw_semaphore vdev_ids_rwsem; struct xarray vdev_ids; + const struct iommufd_viommu_ops *ops; + unsigned int type; }; diff --git a/drivers/iommu/iommufd/viommu.c b/drivers/iommu/iommufd/viommu.c index 8ffcd72b16b8..a4ba8bff4a26 100644 --- a/drivers/iommu/iommufd/viommu.c +++ b/drivers/iommu/iommufd/viommu.c @@ -27,6 +27,7 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) struct iommufd_hwpt_paging *hwpt_paging; struct iommufd_viommu *viommu; struct iommufd_device *idev; + struct iommu_domain *domain; int rc; if (cmd->flags) @@ -46,6 +47,7 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) rc = -EINVAL; goto out_put_hwpt; } + domain = hwpt_paging->common.domain; if (cmd->type != IOMMU_VIOMMU_TYPE_DEFAULT) { rc = -EOPNOTSUPP; @@ -61,6 +63,7 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) viommu->type = cmd->type; viommu->ictx = ucmd->ictx; viommu->hwpt = hwpt_paging; + viommu->ops = domain->ops->default_viommu_ops; xa_init(&viommu->vdev_ids); init_rwsem(&viommu->vdev_ids_rwsem); diff --git a/include/linux/iommu.h b/include/linux/iommu.h index f62aad8a9e75..8c1034cc3f7e 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -43,6 +43,7 @@ struct iommu_sva; struct iommu_dma_cookie; struct iommu_fault_param; struct iommufd_viommu; +struct iommufd_viommu_ops; #define IOMMU_FAULT_PERM_READ (1 << 0) /* read */ #define IOMMU_FAULT_PERM_WRITE (1 << 1) /* write */ @@ -633,6 +634,8 @@ struct iommu_ops { * array->entry_num to report the number of handled * invalidation requests. The driver data structure * must be defined in include/uapi/linux/iommufd.h + * @default_viommu_ops: Driver can choose to use a default core-allocated core- + * managed viommu object by providing a default viommu ops. * @iova_to_phys: translate iova to physical address * @enforce_cache_coherency: Prevent any kind of DMA from bypassing IOMMU_CACHE, * including no-snoop TLPs on PCIe or other platform @@ -665,6 +668,8 @@ struct iommu_domain_ops { phys_addr_t (*iova_to_phys)(struct iommu_domain *domain, dma_addr_t iova); + const struct iommufd_viommu_ops *default_viommu_ops; + bool (*enforce_cache_coherency)(struct iommu_domain *domain); int (*set_pgtable_quirks)(struct iommu_domain *domain, unsigned long quirks); diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h index 30f832a60ccb..85291b346348 100644 --- a/include/linux/iommufd.h +++ b/include/linux/iommufd.h @@ -13,9 +13,11 @@ struct device; struct file; struct iommu_group; +struct iommu_user_data_array; struct iommufd_access; struct iommufd_ctx; struct iommufd_device; +struct iommufd_viommu; struct page; struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, @@ -54,6 +56,23 @@ void iommufd_access_detach(struct iommufd_access *access); void iommufd_ctx_get(struct iommufd_ctx *ictx); +/** + * struct iommufd_viommu_ops - viommu specific operations + * @cache_invalidate: Flush hardware cache used by a viommu. It can be used for + * any IOMMU hardware specific cache as long as a viommu has + * enough information to identify it: for example, a VMID or + * a vdev_id lookup table. + * The @array passes in the cache invalidation requests, in + * form of a driver data structure. A driver must update the + * array->entry_num to report the number of handled requests. + * The data structure of the array entry must be defined in + * include/uapi/linux/iommufd.h + */ +struct iommufd_viommu_ops { + int (*cache_invalidate)(struct iommufd_viommu *viommu, + struct iommu_user_data_array *array); +}; + #if IS_ENABLED(CONFIG_IOMMUFD) struct iommufd_ctx *iommufd_ctx_from_file(struct file *file); struct iommufd_ctx *iommufd_ctx_from_fd(int fd); From patchwork Tue Aug 27 16:59:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 822911 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2052.outbound.protection.outlook.com [40.107.223.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1FDE01D278C; Tue, 27 Aug 2024 17:01:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.223.52 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778122; cv=fail; b=ujO+u9/Gx3OmLZmYQXtAldAJn8CORVnDlhbT+U++AZgc+7Cvp+PXnM/6kUb4MKo+6VHFLQlsyQ8tKBelEIi2Q0CCUQjxMhsZj0Z4bIYae6HBoO9/ptoeE2UhWrdYqXnrRX8Y/KxFeSFg7xOM/9pCBGSGuG5RKS/efAyi5CzIoms= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778122; c=relaxed/simple; bh=cM2Fje7mbQAlmYllEI/wktppd789Zri9q+YN4tnjiZM=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=qpPE88JMrYyH/SstiN8hvSagDCAqMnV8axpu/7eC0+dB2YXXvVgPXDazYKaRu+bZAd9kC87cPcE783thA/qz5n9Vbbg0RjQGX3gxM61DputXWOtRj4ZLKscu/Hl9f3H1ZTQPdTofBmv4EJ9GsW+DYWeDG6/ylohPEpjK+3LzYAg= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=YmgbnHjn; arc=fail smtp.client-ip=40.107.223.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="YmgbnHjn" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=q7BL8IwioVhIQQDYaQmhCbQfowhUTvPIrwJn/hGpJcSCnzJY/dO60jhUaKbiDNVz1nMy/djHEFFmIRumyWuYHxwSg9gzDWVnYOpEMiNfZ1+xA1cu6ffDnr5+jnIq2VUDZzaX9XtOGbzWQQp0BKOYq4gNHmFuL9DnGEYefTh2nlyYRL6ngIYbd+raU7APenWPzQxERx/2GniwaP6ruvR7lOpKDjx7K1US1JgbpABqg5Gdx/ErMMFthByY6a++eX71J2tlzSl7dFI8PrHjwtbmrwZqZVg1xKkr0ApuVeeykT+8j5G+j4Bfmcl+r8szdUjhwcljaGwZy5kOmyDJXSkiHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=3GaZekLxJyKo3YV5xZmPs125vc9+IjLQeHSWASfqbaw=; b=Q/2SBF/NeHlZhkpRhQpQN7EdFcYajTKxVZHEuRGy3/oVYY3Penvd/pFbXpt4fSG/TjrHyZqH20QmTXcEwdcPBQzFq+1mIwfbO3J4967BIp19PBf98O9T7wouH6lKcMp0Js+Kt4sAnccLGPg0n8Jxzi/WtmMv5JUUZTmQUM55nMN5V0CMBfUOrH/4SgnEDuzxmJXXNredwaFVdRkminVIL22pMSWGp3YVo3E23dwB8bLmvkYXJfEQxGcQObvu12KzHANy9NWb385UeTECRDVlUZrnHaW/esp5Rji02X8TftuS1BrAsW8vKH1IB0xcNO3r4U32zDFr3tXXkZGAj9dkHw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.233) smtp.rcpttodomain=huawei.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=3GaZekLxJyKo3YV5xZmPs125vc9+IjLQeHSWASfqbaw=; b=YmgbnHjnxa8nxwgOrq/Ztt7narVjBgUKq6hMMfqLBl1/6s5TaqBtls568xzUSv4rXzwFl98k6rZ1iYROYc9+XKugczQWk0rWta6PpQgTeW17FfjGdvjpCkaetLHtp8Fs6GLloWdCrGTANOJb/riOaWTtasZaqjyievKIhVPydLeVccu0+fRlVSBR74EivfYG3wB7MkfqJ/Kz5HAwi09+iO1JFVJ3RZPesNOvGvL/qnso9ROx1F6XqWMceceort8KKmAYw4OcffqfwdJltLOYLKOw5EorQmTYAQI+r7v7wx8h0y8JEEruzxPDFlRY5zDrgAQtbIKrDgEX20LzQBM1GA== Received: from PH8PR07CA0020.namprd07.prod.outlook.com (2603:10b6:510:2cd::12) by CY5PR12MB6058.namprd12.prod.outlook.com (2603:10b6:930:2d::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.25; Tue, 27 Aug 2024 17:01:55 +0000 Received: from CY4PEPF0000E9CD.namprd03.prod.outlook.com (2603:10b6:510:2cd:cafe::77) by PH8PR07CA0020.outlook.office365.com (2603:10b6:510:2cd::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7784.29 via Frontend Transport; Tue, 27 Aug 2024 17:01:34 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.233) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.233 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.233; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.233) by CY4PEPF0000E9CD.mail.protection.outlook.com (10.167.241.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.11 via Frontend Transport; Tue, 27 Aug 2024 17:01:55 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by mail.nvidia.com (10.127.129.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:42 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail203.nvidia.com (10.126.190.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:41 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Tue, 27 Aug 2024 10:01:40 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , Subject: [PATCH v2 09/19] iommufd: Allow hwpt_id to carry viommu_id for IOMMU_HWPT_INVALIDATE Date: Tue, 27 Aug 2024 09:59:46 -0700 Message-ID: <31c866485c79a0b43dec0d69d1fc6f23fc654e29.1724776335.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9CD:EE_|CY5PR12MB6058:EE_ X-MS-Office365-Filtering-Correlation-Id: 12a78cb5-f90c-4b4d-2ad1-08dcc6b9f46b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|36860700013|1800799024|376014|7416014|82310400026; X-Microsoft-Antispam-Message-Info: vsYIOn6nUASg8j8aIRzvvazEn26flrbZ4HliO/rWurlsf/ZX3rvrHnNoRyPwlzL5ImkeB99/MjkWLEKPhHPRSEwVfjq3udIA3iD/hvnP2NBQqHYeHVEiKEfaKHYqJvsK2AsUqf4maB1uiWZyx5gQGpx3UUsdjYqxlWFhuEQrEi+aZuXoTOxObN48nMA6mvw9D7whajVPGAtCNbv5PKlxGg/Z63380+aWtznYVdcsi1ML2jwWolCsi055XPJDPS9po0FD/s4wJaIk8J0K92mkxkHtfKiZhjRy6nLR9GmtHsolcWOmZeQGAOCJ041JeTjHyEoYUwtwBTiz+RyiSUA8nGZMOsuhx2zzNgQBMTzcoKnfShdO1wBwHtVPsaKajXjskcIbuMb5yQ4J1qkNzw5KyC5+GB2SCCh0jOspOutkQ9nYCIqRkjKGTqL4zXJcQqV9MiFevEDoJVw3Xkxo91WV7lgjL/n4J42t0Flbg7attnQe2+pC8v5OfDTkJazSoweurtX5WQFuBBJ2cK/0UEh/113bwrDznyrdwmOXfcrUENePgCwUp1a+mfiyeZcjRmcbkXDQqZh6RGGx0nyrZWw2pcRjFPMAQVfFBOHvLql6S0KieizF0IHdJc3bXKiDSMTGRVDRb7DBq3NMqdvwVnenW4Mmapx4sFw9+UAksAb70vw3KpBDxh2QhtGHiQF1LJxy8b9RCHob3jiC2JL9iTPTraXEFLMpD+HQnDErwC+KkG7l3jJIcGT3HZzK9J6ZWIcT2Gw8JwAdspkhNQrjsb4Q5qyeJJvYOZLSb9vIEG3dXhzxwfA2OPB4YKO296HAXkzMUvlqOXKhgHRIqGA3hMuh3n6DNOByq0y2OiQsJrkKPIwTTn8QxLVLcQ4EjBHVBU7bBhV0apZJRg9oeP3kj/EjzYFsesXT6Nno+DWqnrcavTy6iByYEdinQSJqiopSJI2s4HN573sOw+2W7ep/CGL5R2hIJgi34AmWQu6PwV6xZ2PY9RNXg1uTmdgkyvGvIJ0r0ZsoRhEBhIdl18PNh/mbIdV/cGYQN4osCq7GPHJZWGWAfcBMNfY7SCST1QJqL300QEX6Fx/S7y0rmMSWPIl302xaGhgsNdvqhjWAowIH200MVrM2d9GD8nG2s3ZDufV15FPsZxdZeKUmHsH1buOZMRDSYuvAARisgiC3AsieFhNYQQ5jLe2VQZgNlt0h5J9wGWHRdVLExwHA2qC1dIzUaVocvyhSp5bQlNa4Egh7oLLg7n8V+gY/enpSzwQ7BfVtGPdthAx27W/F96IT3qD9xITNfDpQY8Dx/BaDk4vdeeZLNhaBEdZFc4B8TgcB+Vbj/UX0YQjqtImMVbUynxSdt9mHOok1gPmL6xPGnzEPrjY85YOIOcQDcxnHszMzzfznaz70Hs1HBk/hJm1zcKJCaUsBZFT/+3RhAjQut5l8YJaHlS7SEgQSfSLLHXK9Pj2i X-Forefront-Antispam-Report: CIP:216.228.118.233; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc7edge2.nvidia.com; CAT:NONE; SFS:(13230040)(36860700013)(1800799024)(376014)(7416014)(82310400026); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Aug 2024 17:01:55.0743 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 12a78cb5-f90c-4b4d-2ad1-08dcc6b9f46b X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.118.233]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9CD.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY5PR12MB6058 With a VIOMMU object, use space can flush any IOMMU related cache that can be directed using the viommu. It is similar to IOMMU_HWPT_INVALIDATE uAPI, but can cover a wider range than IOTLB, such as device cache or desciprtor cache. Allow hwpt_id of the iommu_hwpt_invalidate structure to carry a viommu_id, and reuse the IOMMU_HWPT_INVALIDATE uAPI for VIOMMU invalidations. Driver can define a different structure for VIOMMU invalidations v.s. HWPT ones. Update the uAPI, kdoc, and selftest case accordingly. Signed-off-by: Nicolin Chen Reviewed-by: Jason Gunthorpe --- drivers/iommu/iommufd/hw_pagetable.c | 32 +++++++++++++++++++------ include/uapi/linux/iommufd.h | 9 ++++--- tools/testing/selftests/iommu/iommufd.c | 4 ++-- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 06adbcc304bc..6aaec1b32abc 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -432,7 +432,7 @@ int iommufd_hwpt_invalidate(struct iommufd_ucmd *ucmd) .entry_len = cmd->entry_len, .entry_num = cmd->entry_num, }; - struct iommufd_hw_pagetable *hwpt; + struct iommufd_object *pt_obj; u32 done_num = 0; int rc; @@ -446,17 +446,35 @@ int iommufd_hwpt_invalidate(struct iommufd_ucmd *ucmd) goto out; } - hwpt = iommufd_get_hwpt_nested(ucmd, cmd->hwpt_id); - if (IS_ERR(hwpt)) { - rc = PTR_ERR(hwpt); + pt_obj = iommufd_get_object(ucmd->ictx, cmd->hwpt_id, IOMMUFD_OBJ_ANY); + if (IS_ERR(pt_obj)) { + rc = PTR_ERR(pt_obj); goto out; } + if (pt_obj->type == IOMMUFD_OBJ_HWPT_NESTED) { + struct iommufd_hw_pagetable *hwpt = + container_of(pt_obj, struct iommufd_hw_pagetable, obj); + + rc = hwpt->domain->ops->cache_invalidate_user(hwpt->domain, + &data_array); + } else if (pt_obj->type == IOMMUFD_OBJ_VIOMMU) { + struct iommufd_viommu *viommu = + container_of(pt_obj, struct iommufd_viommu, obj); + + if (!viommu->ops || !viommu->ops->cache_invalidate) { + rc = -EOPNOTSUPP; + goto out_put_pt; + } + rc = viommu->ops->cache_invalidate(viommu, &data_array); + } else { + rc = -EINVAL; + goto out_put_pt; + } - rc = hwpt->domain->ops->cache_invalidate_user(hwpt->domain, - &data_array); done_num = data_array.entry_num; - iommufd_put_object(ucmd->ictx, &hwpt->obj); +out_put_pt: + iommufd_put_object(ucmd->ictx, pt_obj); out: cmd->entry_num = done_num; if (iommufd_ucmd_respond(ucmd, sizeof(*cmd))) diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 1816e89c922d..fd7d16fd441d 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -729,7 +729,7 @@ struct iommu_hwpt_vtd_s1_invalidate { /** * struct iommu_hwpt_invalidate - ioctl(IOMMU_HWPT_INVALIDATE) * @size: sizeof(struct iommu_hwpt_invalidate) - * @hwpt_id: ID of a nested HWPT for cache invalidation + * @hwpt_id: ID of a nested HWPT or VIOMMU, for cache invalidation * @data_uptr: User pointer to an array of driver-specific cache invalidation * data. * @data_type: One of enum iommu_hwpt_invalidate_data_type, defining the data @@ -740,8 +740,11 @@ struct iommu_hwpt_vtd_s1_invalidate { * Output the number of requests successfully handled by kernel. * @__reserved: Must be 0. * - * Invalidate the iommu cache for user-managed page table. Modifications on a - * user-managed page table should be followed by this operation to sync cache. + * Invalidate iommu cache for user-managed page table or vIOMMU. Modifications + * on a user-managed page table should be followed by this operation, if a HWPT + * is passed in via @hwpt_id. Other caches, such as device cache or descriptor + * cache can be flushed if a VIOMMU is passed in via the @hwpt_id field. + * * Each ioctl can support one or more cache invalidation requests in the array * that has a total size of @entry_len * @entry_num. * diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index f383f3bc7c8b..12b5a8f78d4b 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -360,9 +360,9 @@ TEST_F(iommufd_ioas, alloc_hwpt_nested) EXPECT_ERRNO(EBUSY, _test_ioctl_destroy(self->fd, parent_hwpt_id)); - /* hwpt_invalidate only supports a user-managed hwpt (nested) */ + /* hwpt_invalidate does not support a parent hwpt */ num_inv = 1; - test_err_hwpt_invalidate(ENOENT, parent_hwpt_id, inv_reqs, + test_err_hwpt_invalidate(EINVAL, parent_hwpt_id, inv_reqs, IOMMU_HWPT_INVALIDATE_DATA_SELFTEST, sizeof(*inv_reqs), &num_inv); assert(!num_inv); From patchwork Tue Aug 27 16:59:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 823341 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2041.outbound.protection.outlook.com [40.107.223.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C4F621D27A4; Tue, 27 Aug 2024 17:02:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.223.41 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778122; cv=fail; b=Q0t/UYbVTrnPnpPp4NICGDTytcRDt1tgklp/eIJ/24jW+A9ursSib9QIg39GTSho35iWHD3ABZcPUA9iXoZDy5Os44makhoNBg6JL/2z5L/SnY+3wNoOyIH6SEy6xffExZ8S0IWficR3vIHeHKtctvO+zgRy/hxDrUcKGaE0p3E= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778122; c=relaxed/simple; bh=W7dS2XS29qzgHW42M63oQbyuxzBJ+WSNyId2nL5u7Pc=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=XtVK6DNhTyKnBiSVFx7NFQP1TLOMEfZpRbgCNKkiuQSVnvCECmES3QVlH/v3p1LTmxQi0xbjLO2Q908Sc6X3DDZg8hmsi+NPK164q699NnJmcRJCF5CaaHR0cHJ4eHiXYx90ga1f/5/oz8QTytJiefdmOFNa0SjFVqsqYhtFQJw= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=G1IvgYTG; arc=fail smtp.client-ip=40.107.223.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="G1IvgYTG" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=bI8Czw4yYx/shx4cNi48bgg8ozYVsDJ6DS7Qqr6bcM4x6gYzqExQdsl18UQ4GDyBH8a1laztF7f1CMwIsKfWJ/9w1xsvpl2HyvUkkddFQuU/JfwdsvSArhQeVMXcs05K2MnjiuaHWcOltRq7ih32Ndl+PWc0bJjNyUG1Qmo9BEmWuzlzUp+EpMaD9BMXZdU+2prSpR3vgdxOWSL4mo++7ULGnNAtggYk9e2O0o2BW9cd5y8RkuoadTRRuUhpxA/qlqlMHRxHrJaiCdFeaw2caoPiq5ADKcj6tOg9Liz+mT37w2pnjPnAOZ74rPamjO3XMwTs+yXFqQQu2pm7yIWyTA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=MPYOWHi+9nANoYua577/wdgH/ZY9XVaaaAI83InvJGw=; b=ESb0UYI8t2gHWnGva3DAWPUvTktOBfByq8yEyhWOHVYxOuK5yYgYnXl68/VEWVWdoufqJiFiJ8XsjK6Lbp3wZnrv6ctPYDblGUbyzUX9jGxXb4bQMK09QIayTOJdHHX7GwBXmjrUq8QhZL8pGsNOfhZwAj6p143ikMwB1QS032HFNk8CbtdT3K0Zd+aiciW1KLny8xUgPAmjf0yHnmrcfg/M6G0ZqOCsm4rbK/D5SqsQQFj1RZBZY6iUHbpC+7yC9hQAUVM9Pnl+Nrzg6RR6WLHiZFSMAfQSEoi6nEeHLBjCOcLz+4orhX1n6iOSiTxEmi1+arfeeAQmYExaOPDFtA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.233) smtp.rcpttodomain=huawei.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=MPYOWHi+9nANoYua577/wdgH/ZY9XVaaaAI83InvJGw=; b=G1IvgYTG/3eACR9PMyMNF0NO+hiCl/RoDLAw1lEsBnfqR7/JEeLLeUWOntcAmuVUCnMbusWTJqhKGlhXUyOt60EhAM3N14+BwbYKdha5ckp/y4U66PEEYzh34h1b4kBkSxniKbAmNIJ6vGhYK4Vl+tVTRu+c5BF59Tou1RE/mkn5I6cajHDtiabtCxkAoZf7PinWIJxQKStvu0HXBmWxCzZZ/WLYwCwamkmVV6anBg2NjznWwE5OGp+yJPsrbACb/Ui55hjGEJ8VQvmfqW1T1gUPbZXFZKJm9zwAP/Zar6/lx5O5iTYick1A4YBpYAwLywNWsxXccAL66b0tMqx/Cw== Received: from DS7P220CA0010.NAMP220.PROD.OUTLOOK.COM (2603:10b6:8:1ca::8) by SA1PR12MB8118.namprd12.prod.outlook.com (2603:10b6:806:333::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.26; Tue, 27 Aug 2024 17:01:57 +0000 Received: from CY4PEPF0000E9D3.namprd03.prod.outlook.com (2603:10b6:8:1ca:cafe::bc) by DS7P220CA0010.outlook.office365.com (2603:10b6:8:1ca::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.26 via Frontend Transport; Tue, 27 Aug 2024 17:01:56 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.233) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.233 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.233; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.233) by CY4PEPF0000E9D3.mail.protection.outlook.com (10.167.241.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.11 via Frontend Transport; Tue, 27 Aug 2024 17:01:56 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by mail.nvidia.com (10.127.129.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:43 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail203.nvidia.com (10.126.190.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:42 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Tue, 27 Aug 2024 10:01:42 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , Subject: [PATCH v2 10/19] iommufd/viommu: Add vdev_id helpers for IOMMU drivers Date: Tue, 27 Aug 2024 09:59:47 -0700 Message-ID: <6ccdfb27c7aa5a5bb7e153165cf90114cae4687c.1724776335.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D3:EE_|SA1PR12MB8118:EE_ X-MS-Office365-Filtering-Correlation-Id: 7832d643-22d5-4711-ba37-08dcc6b9f565 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|36860700013|1800799024|7416014|376014|82310400026; X-Microsoft-Antispam-Message-Info: dxWj7BjQWkYg5VXNaZO2MzDGuRSMtmPZoBux72VHeUxYB7VWkAqDq0xDgneCuW6EvIp9CvcOZoabEdtghl0AZ77O2BzZsC4Ti+vG1s4ls2F25LO6PktN/iYxN+ZsuiSZKdAn3NKSwtxoESsUtJ065/VwMNxe6PeQxPR2NCXlRMht3qy5geNK02jmvM3jasrTCIg3i6Q0QMn1jFEQOGRXWC4VZK7IP0+kvSyofK/L0v5H27opCEs3tCKyFFn8O4d3jZae+4jk0f4/avJxjeA5uEIQwlzpYWCeXmOFZIYW0WwhOQp1YLACsplMVQ8bth6pg92aXvvt7DiS3n+FJxjy0PVVqzbBr7A+6vvPJ2uJOJhEWss2c1AbhjgoUE9DQI3iTX57+2cuchU+fmWQwLNG+uXffSK4DGzYfAMzOuiUANtvtFmUSTbD3I9DR6gGeEYdR8Uqvz91QLYR80jpn0x1S7hszwglHGsOx0lEg65fYJXqUo0DInX29ETnPUbw6VJjrpLXF+7QH0iXgqSFYlOxIHcOoYAKsEP+iH+kIY+Ths4jw9XoOm4OhycPox91A0WsO97s+hzJjAmd1hjQq5c8iBS2WbC2KUa0e+L21l852QSs7Y3vqVDnJYLNi6DtSd+ZWcIApFQaEzBEdAD4ZC5WHMcuMVrHN+Tq2i5/3rO43aq7oohBwKqWo+gGwXUpsBs9ffUzPFzIKSDqVzRUvrS6F011d0a7iK1NpmPjd9DMTNdpgAQbjAnxuVzicXkprbO2eTZ97SgVhwbOO041r6KVclVMvqUMDXW0VJDQhP4Fr0ugi1SXy3MYdGTn4cKnR0YmzbfFR+OE2awk2FPm+LLHhFk9urDdrLV8oOO+Nqv6JtJk+RcJK/RwG6jVy56DzZjYMWi1F3V2pkdVU5eP0Tih2LSwrrV0ve0yHvlzIbdKYjYIelPd+X2NZ1WufkSe/syD95xd2LG+zGrS+93qanYkVJuBEiyIdoozEldkasLjxnOowDZ24Z3hLaLW4+Bdvz5rpKbUoXKDE9z5t1H4iCGK7M7WlXYuaLaLLeoSVjZns3JPV80FaLTjVe5NZFAJnaCeAzvh9LKmYBfDEp88UvsLV+mq1xMspvQe9UbfFOwdfTYxqZLuWi/kIhhTgfG/ipuBVVeCI16WRg67ipE0YC3pxKPtFOasJl7zAVJzwW8fTm8n5NptILGwU42TsPsuAbbLN8eHZ8uiL4KUo9qeQJ8hOfPdChycuv6rlCwjbQdb4KlF5ipd2TbZJLouzh0MjNVCG6z6qdTrDgh363RJtqsQiw3+0fekayybq/nucCyuN3QPyXpSZILG9dEwYDvNny8hClWms0Y3MmSOE4VY/E/EXSLiANr3Oo9ShcURdpFLBtSCJ+QQGxcAlUyxYPcdufAb+7YI12wDsi0GcUOY4ajEyXgM51BDqz/hJx0JZ93tdc/IUZtF6prHkP8jlxmDNaiR X-Forefront-Antispam-Report: CIP:216.228.118.233; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc7edge2.nvidia.com; CAT:NONE; SFS:(13230040)(36860700013)(1800799024)(7416014)(376014)(82310400026); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Aug 2024 17:01:56.7008 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 7832d643-22d5-4711-ba37-08dcc6b9f565 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.118.233]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D3.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB8118 Driver can call the iommufd_viommu_find_device() to find a device pointer using its per-viommu virtual ID. The returned device must be protected by the pair of iommufd_viommu_lock/unlock_vdev_id() function. Put these three functions into a new viommu_api file, to build it with the IOMMUFD_DRIVER config. Signed-off-by: Nicolin Chen --- drivers/iommu/iommufd/Makefile | 2 +- drivers/iommu/iommufd/viommu_api.c | 39 ++++++++++++++++++++++++++++++ include/linux/iommufd.h | 16 ++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 drivers/iommu/iommufd/viommu_api.c diff --git a/drivers/iommu/iommufd/Makefile b/drivers/iommu/iommufd/Makefile index df490e836b30..288ef3e895e3 100644 --- a/drivers/iommu/iommufd/Makefile +++ b/drivers/iommu/iommufd/Makefile @@ -13,4 +13,4 @@ iommufd-y := \ iommufd-$(CONFIG_IOMMUFD_TEST) += selftest.o obj-$(CONFIG_IOMMUFD) += iommufd.o -obj-$(CONFIG_IOMMUFD_DRIVER) += iova_bitmap.o +obj-$(CONFIG_IOMMUFD_DRIVER) += iova_bitmap.o viommu_api.o diff --git a/drivers/iommu/iommufd/viommu_api.c b/drivers/iommu/iommufd/viommu_api.c new file mode 100644 index 000000000000..e0ee592ce834 --- /dev/null +++ b/drivers/iommu/iommufd/viommu_api.c @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES + */ + +#include "iommufd_private.h" + +void iommufd_viommu_lock_vdev_id(struct iommufd_viommu *viommu) +{ + down_read(&viommu->vdev_ids_rwsem); +} +EXPORT_SYMBOL_NS_GPL(iommufd_viommu_lock_vdev_id, IOMMUFD); + +void iommufd_viommu_unlock_vdev_id(struct iommufd_viommu *viommu) +{ + up_read(&viommu->vdev_ids_rwsem); +} +EXPORT_SYMBOL_NS_GPL(iommufd_viommu_unlock_vdev_id, IOMMUFD); + +/* + * Find a device attached to an VIOMMU object using a virtual device ID that was + * set via an IOMMUFD_CMD_VIOMMU_SET_VDEV_ID. Callers of this function must call + * iommufd_viommu_lock_vdev_id() prior and iommufd_viommu_unlock_vdev_id() after + * + * Return device or NULL. + */ +struct device *iommufd_viommu_find_device(struct iommufd_viommu *viommu, u64 id) +{ + struct iommufd_vdev_id *vdev_id; + + lockdep_assert_held(&viommu->vdev_ids_rwsem); + + xa_lock(&viommu->vdev_ids); + vdev_id = xa_load(&viommu->vdev_ids, (unsigned long)id); + xa_unlock(&viommu->vdev_ids); + if (!vdev_id || vdev_id->id != id) + return NULL; + return vdev_id->idev->dev; +} +EXPORT_SYMBOL_NS_GPL(iommufd_viommu_find_device, IOMMUFD); diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h index 85291b346348..364f151d281d 100644 --- a/include/linux/iommufd.h +++ b/include/linux/iommufd.h @@ -89,6 +89,9 @@ int iommufd_access_rw(struct iommufd_access *access, unsigned long iova, int iommufd_vfio_compat_ioas_get_id(struct iommufd_ctx *ictx, u32 *out_ioas_id); int iommufd_vfio_compat_ioas_create(struct iommufd_ctx *ictx); int iommufd_vfio_compat_set_no_iommu(struct iommufd_ctx *ictx); +void iommufd_viommu_lock_vdev_id(struct iommufd_viommu *viommu); +void iommufd_viommu_unlock_vdev_id(struct iommufd_viommu *viommu); +struct device *iommufd_viommu_find_device(struct iommufd_viommu *viommu, u64 id); #else /* !CONFIG_IOMMUFD */ static inline struct iommufd_ctx *iommufd_ctx_from_file(struct file *file) { @@ -129,5 +132,18 @@ static inline int iommufd_vfio_compat_set_no_iommu(struct iommufd_ctx *ictx) { return -EOPNOTSUPP; } + +void iommufd_viommu_lock_vdev_id(struct iommufd_viommu *viommu) +{ +} + +void iommufd_viommu_unlock_vdev_id(struct iommufd_viommu *viommu) +{ +} + +struct device *iommufd_viommu_find_device(struct iommufd_viommu *viommu, u64 id) +{ + return NULL; +} #endif /* CONFIG_IOMMUFD */ #endif From patchwork Tue Aug 27 16:59:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 822909 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2045.outbound.protection.outlook.com [40.107.244.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E83051D3194; Tue, 27 Aug 2024 17:02:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.244.45 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778126; cv=fail; b=CZ6ZWIPwR4yDHh4z47lOTC95UUYRAMFCrOcXLDEIQ9YHNvJqLfCHXDibItHdsVr+Mk0sWbL0bO8DdxMzwC+rvndsOggLc4r7ci4fWC/bHS/K3357aBOMl277vCMg6FtAd4vi9JiOFEtKjSvlOyoOIde4sp6EVQTbQmyYnzFIZkQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778126; c=relaxed/simple; bh=77Rg+XUUaI4Cyc/+2/ci1D9prOW2G7RCj5IFElQKb9o=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=A3YfvGL9h7Lm66ubnHPzTlhRh+c6ejkD2KPQveQjV2s2vdGS0CF5scXN220BuvEwLtYR1XzIMVsm4xmw54iZMovjErJ8TPLlO1yosFVJGwxKEHMImh8RAGiPO1bu04dkFrrrwLU7ARpddvos7EWV5eRWQly7m2o1MqJ2XF1G5+E= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=LrFihceh; arc=fail smtp.client-ip=40.107.244.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="LrFihceh" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=E3Jexg4pTjX1leCF1a5u8Rwf1S2Az6naUjKERq63PaqAWWn+YjeuWf1uNNnDkJLDHmQM//ydpJj6M/5eXLc9s4b+XcpRm2WC52C1Nb0ZyjZldXg63A3BS9U8hw1kGDWKU1fC9UfS/+PKPsFbOmcLnERK2MB68sKeTtLMFc4p695HDwLy7bjswDlvE73udbwAC+aeYel56XPILWUyO3iE4TX5Q6k1lKbn2He93JoFgWlTMMcG01j4mxLOFacuMs37jtMhhH6JhxkYnhyLKqL/cZUXPPlffQN9M6w8O7+7/hXWDgEG9/Y5b5Cl4aGJ2LSGu65aoEB52bsv3X2xgLCa9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=IV/muEQnoo8rlQRVDk2+Mw6tAvwItl8MKO6PgGjBUTE=; b=IKY4Buzgqe1rdJp0QQKjAAQbBYrD2gxjqgc9tIxpb+/A8VcTKOhP70SpGxyPVFDRO+f1Pscla7SSrlCg6RovYMgRVChEUDbgj8pRQ8+mx0MG9BHZ9PAKdi6dsssAvyspnF4svZiDaz+TFFm0hKE220QQS8OiCM4yN3Iy3nCNNiu4M+kFaYasQ45SwbrVsR5lwiagQWDQoBm9H3cynzVykg2+HQFXPVoIbvY4LrE+3J36kn7NtbV599Baqx+rIZ8gftw8r6y3ajRehKPMxBiOJDcpfFI/zU4AUxqmS35S4fyqZQKsYObsOBlP87CANjxvIsz56b6PhHBL4njZh+EhpA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.233) smtp.rcpttodomain=huawei.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=IV/muEQnoo8rlQRVDk2+Mw6tAvwItl8MKO6PgGjBUTE=; b=LrFihcehP4drJkAXLcUXJr8ZXARPRQLzdMOAUluSkaic2zLZhqMZ1ujwZ5/BNbrajRnwWi6lCvRZHXF/s9TNH42GoyhUDJyBJ0h6S8nmrJaA7agamAu2EqlQ2acirs0+wI0Fhh2Iuq5n7k/QxogMHwtkwpOAbS1N4VZUv8d7fb/H9Gv7qWU3/t65lpWf0muoDMOIHE6UkrontW2DhBaztS2qQqRA6P2Eac5CWcgF+KX+/nbVuyzKcQLoYJLl27N00SnUwD6OZY6fpMZnj4gQyvKoGvftGwPWyybNnHJ6rgC157ONteKGdBJGFFaHbJeq8Ov1vJFnDGOAMLhT8CpGZw== Received: from PH7P220CA0104.NAMP220.PROD.OUTLOOK.COM (2603:10b6:510:32d::26) by CY8PR12MB7514.namprd12.prod.outlook.com (2603:10b6:930:92::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.25; Tue, 27 Aug 2024 17:01:58 +0000 Received: from CY4PEPF0000E9D4.namprd03.prod.outlook.com (2603:10b6:510:32d:cafe::28) by PH7P220CA0104.outlook.office365.com (2603:10b6:510:32d::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.26 via Frontend Transport; Tue, 27 Aug 2024 17:01:57 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.233) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.233 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.233; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.233) by CY4PEPF0000E9D4.mail.protection.outlook.com (10.167.241.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.11 via Frontend Transport; Tue, 27 Aug 2024 17:01:57 +0000 Received: from drhqmail202.nvidia.com (10.126.190.181) by mail.nvidia.com (10.127.129.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:44 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail202.nvidia.com (10.126.190.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:44 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Tue, 27 Aug 2024 10:01:43 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , Subject: [PATCH v2 11/19] iommu: Add iommu_copy_struct_from_full_user_array helper Date: Tue, 27 Aug 2024 09:59:48 -0700 Message-ID: <8cd72612c5c8a3a03df0c7af9749b76bc16bb3d0.1724776335.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D4:EE_|CY8PR12MB7514:EE_ X-MS-Office365-Filtering-Correlation-Id: 2756f5b2-8c92-409c-4ab3-08dcc6b9f5d4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|376014|1800799024|82310400026|7416014|36860700013; X-Microsoft-Antispam-Message-Info: NDUYnIxlrFtykjyU+DWPuC2wBn0qpEUNkGIJXKzpRTgYUxK7XHVWQJZGFgNqsxwTXXd8dTTcUDDS5Drph3w/eoCrzIn1TDakBtGCLfLYGRfQLRueaig1MPKwQqVV8Pw9dEOtoTP340VmoQ0fsDZXX+JlnwRRcxzaxLsb/G6cSDK3qfLkxyhI3+ctcQBb6tDDM7rEBa2wi1cUFBdYTjvylHjEt08WnZr7sKg6SZk7+ccz6bV2tL6JYZTP/eBDKHKWRJwBNrbO8huvoklxotimTpNfjwWC+Lw5qsPq5WF7Ro8o8qo4vECTvFXVTReOE8ZXPqKb5Ic52QvAgHY5GoO4ipNKgilZktaAuJ4N7fy/mKYYdKNiD8rtu9HYlzdtXhFlj5TF4S7Ud5GtURrth6rND78X9IiD9A4knvCkB8yBEuD1mmj5w5Jj8414kTiLHgAsubjZfYXWle4F4dgSe5A8q7PhiAb+vxDU9mOmATH8W8cz67Eav7qTWNM7xVql4FXhgno/cdcg0Oi64UT3zb8TzZIr0knHo9CX9DJFeCo4reRs2ilUNznE9v9MRI+KPg31B5eV5fGQuQ44V5Vet5MV3p7NaBWtQiLD600CiJjGKCInUpwVB+OFXQMQVWiWoVk2NXnQho8bTnJDbyRh3MkpvHcFDjyLD05QN1uk3EggVxl4PbwuqmqlbTM8UoHKvYo1IY2/KyWfl1NBgQH3YRi7nbJTyoCyT0OxnxUiUFjhgztGl98RpymYA9p7skopCFKuTJMyDmBLRepLVqv96jOE91hkrGmIG0EIdVZl2iy9ypWTel7muI6qTAb+MOhE7/CFav1iBh1CSzabo4zXoItMYn4muGnsSt40Pm47BxFTNuCet3WO+XX2pAEeiaJMVUBr96YwC5wuak0aGC9s5JMP5kp5ZBNqLBQmbLRrg2hLYYXARAK1gd6UBYM0SHdPEaxPKk6f3neAxoiOVAzTCAgjHn9/GfSuVasA+PmJarWIlAWsZ5m/C4fXnike5zujeRgOJif0gUyTtPADxFtKD7xCpWcIHcsiefHfqWGoDtmIDKlV3OPia5NxWlrlo4Olse+z071c3EmTTli4/5zgQWVkiL9DPt88BDaGTwD1lSOasUvLHjY9ELSbR3L0NXzGWgrL5Rah8jLofhK9whk8zanE6deUThjEwWBkrWJCwKVcp/VYtcgO919pkjHqhb0va6qvEJDuiGO/Pe2BPgMLfHDuzyOSLPSY9SnVUBRO8KrAJfRNZj0Fdv2gWzl7bgXTf4JNxw4Eofgbkj9oxeb8Y4YXOeuF91w0NtqAIyHQDg8QxYh1EwAKH2s5knvfMH3P855qWwM7zvf1+Dq6RkF6ZXqn7zj9YWH3uCO1MlLmjis9F+t6BmjC0QE+kq6ZgSOSW/k1T9bfjVbPjso2GpOnk32sSCCUbooP268XbX48lOkQuaU+PU3LTn8PS3xcuEoMoEqQ X-Forefront-Antispam-Report: CIP:216.228.118.233; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc7edge2.nvidia.com; CAT:NONE; SFS:(13230040)(376014)(1800799024)(82310400026)(7416014)(36860700013); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Aug 2024 17:01:57.4566 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2756f5b2-8c92-409c-4ab3-08dcc6b9f5d4 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.118.233]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D4.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB7514 From: Jason Gunthorpe The iommu_copy_struct_from_user_array helper can be used to copy a single entry from a user array which might not be efficient if the array is big. Add a new iommu_copy_struct_from_full_user_array to copy the entire user array at once. Update the existing iommu_copy_struct_from_user_array kdoc accordingly. Signed-off-by: Jason Gunthorpe Signed-off-by: Nicolin Chen --- include/linux/iommu.h | 49 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 8c1034cc3f7e..556b6d6cf2a8 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -493,7 +493,9 @@ static inline int __iommu_copy_struct_from_user_array( * @index: Index to the location in the array to copy user data from * @min_last: The last member of the data structure @kdst points in the * initial version. - * Return 0 for success, otherwise -error. + * + * Copy a single entry from a user array. Return 0 for success, otherwise + * -error. */ #define iommu_copy_struct_from_user_array(kdst, user_array, data_type, index, \ min_last) \ @@ -501,6 +503,51 @@ static inline int __iommu_copy_struct_from_user_array( kdst, user_array, data_type, index, sizeof(*(kdst)), \ offsetofend(typeof(*(kdst)), min_last)) + +/** + * iommu_copy_struct_from_full_user_array - Copy iommu driver specific user + * space data from an iommu_user_data_array + * @kdst: Pointer to an iommu driver specific user data that is defined in + * include/uapi/linux/iommufd.h + * @kdst_entry_size: sizeof(*kdst) + * @user_array: Pointer to a struct iommu_user_data_array for a user space + * array + * @data_type: The data type of the @kdst. Must match with @user_array->type + * + * Copy the entire user array. kdst must have room for kdst_entry_size * + * user_array->entry_num bytes. Return 0 for success, otherwise -error. + */ +static inline int +iommu_copy_struct_from_full_user_array(void *kdst, size_t kdst_entry_size, + struct iommu_user_data_array *user_array, + unsigned int data_type) +{ + unsigned int i; + int ret; + + if (user_array->type != data_type) + return -EINVAL; + if (!user_array->entry_num) + return -EINVAL; + if (likely(user_array->entry_len == kdst_entry_size)) { + if (copy_from_user(kdst, user_array->uptr, + user_array->entry_num * + user_array->entry_len)) + return -EFAULT; + } + + /* Copy item by item */ + for (i = 0; i != user_array->entry_num; i++) { + ret = copy_struct_from_user( + kdst + kdst_entry_size * i, kdst_entry_size, + user_array->uptr + user_array->entry_len * i, + user_array->entry_len); + if (ret) + return ret; + } + return 0; +} + /** * struct iommu_ops - iommu ops and capabilities * @capable: check capability From patchwork Tue Aug 27 16:59:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 823339 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on2072.outbound.protection.outlook.com [40.107.101.72]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AFB631D3190; Tue, 27 Aug 2024 17:02:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.101.72 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778127; cv=fail; b=hsIbx3+TYhj5d4QRCe07WO0LAcIjpJATqW1WQSsfP6+k9RBDCucOIa4PgCGr+g5G1+hg8WK36P4VwjBskkmtUjbh9HD5kNEcomm4nIwAIUAckOE6jtxM60e+9zTjUe9ZACwUH22t9xAWTIVPFznlIQrT8PlHhFTnxxpAYECIvSw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778127; c=relaxed/simple; bh=r8svXObukZv1gTcZUHnHzbuWlQeae9Z/BWzEId9gkzY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=byJqShS0nBRHWXbJDARz0AaCg1IMsZIqxV1Bz6wNj/VVJUdc9xHeREX8ajo7C89MV3ZKHH0qzwf1ro36vmcuD5IcZnaTR8zQ+1JvkqST4QJia7u1q/OJeqCQ4m1+XEtzouVnCVU9FP8rNyi1SUnP42L60TYkNyb6nePbEiKu/uA= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=hee69PfV; arc=fail smtp.client-ip=40.107.101.72 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="hee69PfV" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=sUxqvWGKjdJWN8XSZbUYXoWbuXjfEYaNSCysskVRqoWBQnF84JhuhWIVL4eZwMagSb5XVTdwBa/eFQePyQ5oEaphCrNcUVwPJA8SoIKuhx1Tph9s9FBnVnZtlh+u/us7/r4D2UEepzQDBXR62CL3DGmFmlCzJPVsuCv8ByEj6Xx388SP36OJrLSOKLMVS1DSfyNUbhum4meRY6TAvMrP6WN1UBhNIkwGx6zz4Xme2i+V2w1vgcy1e3TwL5XHlzdNvoo9RRDQxxcngnowF5bilxX0vrg5aEvOFWIcGvNZFPMS+i/1h6ExycHF+927kkqfE0x3/+1pV13qAcUIoaaUMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=mqj7E6gIFQVldasjfdEs7cByusGG3RDx1fRjN7lCdVs=; b=QcHLfbaPaahYkuG1u7Bx7/Yog7hw2c1abqLtnurXksxT719EfeCb1hH4h2M72pkhHvf5XssAzZHOoDq310DmL488UaoxB7SS5FUumOF3Tq7+4ZTQRCmtGs+u0e66lzjYxxkhGCzCQQqGx+rnWE17iaC1/CnYOgTzysp1n8z722Xb6Fjkoclcx//T4UMdubzsTGFjbEPPp3d874zONsp0Oz755yH2RPa5CRICCZJD+TbfVgMxC5lYYoeVFns6Q7iVdxU/8zpfFLVmBrPoNJpYj51Y9elnpNtrYn6NtbJbrH8+STp3yOq2iKw88ZRnq2nXB3ZoWu1saDdPSlCOiRbnBw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.233) smtp.rcpttodomain=huawei.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=mqj7E6gIFQVldasjfdEs7cByusGG3RDx1fRjN7lCdVs=; b=hee69PfVYVQk3ps/2UXviJElHjf1A1uFtJPCKA5sIDGWeLcZ/YfER4a/Asvc4Nb9KNSYhi5vJM4/PTlTgHbcgm9NKGq+YkS//vjq73fDRTqz40Tnnywdnpw0qnctxIHZtU6nDmqqj3mH4SB9b+hmU0VwklOAO/Z2aJCuPv78AyMH10Xn7o947XgWqZhoRN0a8T43Hk81vGBqOs8j4BaJ8sdUltPL5xvimVPz8Cl7bgpfhR8xU1gv6XAeXruqGUCFNHra3bd9O1DJDy+5Mu25+/h48Y+lgMYkW3t/lCy8BEYgcRwY57qNtn7Lgk2BJRK1OphRIrVmI+RXlptzx/998g== Received: from PH7P220CA0095.NAMP220.PROD.OUTLOOK.COM (2603:10b6:510:32d::24) by CYYPR12MB8889.namprd12.prod.outlook.com (2603:10b6:930:cb::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.25; Tue, 27 Aug 2024 17:01:59 +0000 Received: from CY4PEPF0000E9D4.namprd03.prod.outlook.com (2603:10b6:510:32d:cafe::f0) by PH7P220CA0095.outlook.office365.com (2603:10b6:510:32d::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.26 via Frontend Transport; Tue, 27 Aug 2024 17:01:58 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.233) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.233 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.233; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.233) by CY4PEPF0000E9D4.mail.protection.outlook.com (10.167.241.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.11 via Frontend Transport; Tue, 27 Aug 2024 17:01:58 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by mail.nvidia.com (10.127.129.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:46 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail203.nvidia.com (10.126.190.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:45 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Tue, 27 Aug 2024 10:01:44 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , Subject: [PATCH v2 12/19] iommufd/selftest: Add mock_viommu_invalidate_user op Date: Tue, 27 Aug 2024 09:59:49 -0700 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D4:EE_|CYYPR12MB8889:EE_ X-MS-Office365-Filtering-Correlation-Id: fa4e1b3a-2e5e-4a29-f45f-08dcc6b9f67f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|82310400026|36860700013|1800799024|376014|7416014; X-Microsoft-Antispam-Message-Info: dH+End6vs4VQNBxTvWqfKUM8yuQBZ9KaczL+8HzemKIJcN68AtplKNu04u8qEyjG0R41jgatmzXgYyl2oZGsbS7u5+HeazzCI/JgfJSjru9MfGI6bhL5NLriX8Nl+iYLLsniYbsUR8G3iHnacrJZBYu/4jMiWH46BO3XyT9KmSeXNmLbDlErK5iTiFOpcahkeRqQrca0SLvamhimcaXrB81cUQLMJ7jggpbvkFJ0Ftxsi+QU4emAk9RGME9+osvj5bMST0htKrWUh4Jv0nE+cbd/MEUoh3MmSnxb3CUFbLqJpbmm5vE0IGiuzNxb0NWNtZi7ksAELWZS0CYgIyZMCutU1/3qQiTosgggHzANetUJxbt7YrBPnOr41ZWgjR40pcdZN6OH8Q48h90cqR+xj557t9QBsSV6FBkN2BYY9n64gKzt0XN6kSBErz0l9x7P4xSNWz8VcC9joAl45oEcetNe4vJl5vprgh5jhZLVtLnlqXvxUCXbQieVb2r3MHOmHYfu8cQ1Cm3Y9y2W2amaBWn78sCowf0hmuyoviMMDgfymypVh5tNYglKNAL4f9ZVwjTSEySg2N+kWldwkpcAn6GkvzTofG0wRMXMM+kLH5q9QKXwHzc0l2ptoDS7TNDenBpiojGqzoNqfrVbGdspwbdRCVS3yjQytdIVAJk9p93wQGaE88z/7HwK9C2rF0vG6Ap9ZBWnDVo2G0ceUD27mXIHTxHQllJH4cPAlURzXAaySe9wsDh+V9VFhNcf6tCuqhiSQw35T62m/lypBhZb4SQ4KV6gVPRGnxfCkWK/TY9EbEq4dJeIc19Ri368XTRrJpxbVDDiuQGvsrEDwIcNfAMb7RhOZOS6HSP2hax4xCRM0suYDb/R8286GD/x87IBIkC0lzvb6hX8CigPtSbVxyYbuOFeF9rXPxg8nxQOCmLUfCMY7q7s6nh3fg1LL5/HrXyK1MjmLU2Yq3F/yXqhKi5y07LyGLvQev4iy21rtqT44vtU3kNE7xWXKvwkgfgJkjwGJEt4KjhvMBGS6ZMQheod7SPJL1zQHsokaRNDcYxCFL2TA5ZqSpNhA9cz29MQIfwUMpGHkznnoCL+gfdqqEJ/M/lNGt2pGnztQanEPmpLQqrc8niBghu4cEhEu31FOqHUivUtFwDc6vYrC2yAq7q/j53q9LP/XfMFhkbf8R2xI7WxOwNmgzUUbubfzcV0H1NsCgVb4RD35G+gCd0T1OI6c5nxVJWnQCZyapFa56MXQGzqxOHPQ3r669KSyaVFCTcmvQtJHIoe9viG/bhFxPwaKpFQFDAW820LrEEERx6t0u9zYOj6m6aO2ph9txdKPMzWHiHLDmR6/IayKtkybyCNnMY0ucq4Qgp6U5sqAqxlFQt13g27FMExBuZjqVQ5AqbqFW8XvaQx9giwu/nTvB3mrEFfEwED270nAuTbly1IhtFFLqVDl1TjfdPO+1wW X-Forefront-Antispam-Report: CIP:216.228.118.233; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc7edge2.nvidia.com; CAT:NONE; SFS:(13230040)(82310400026)(36860700013)(1800799024)(376014)(7416014); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Aug 2024 17:01:58.5660 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: fa4e1b3a-2e5e-4a29-f45f-08dcc6b9f67f X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.118.233]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D4.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CYYPR12MB8889 Similar to the coverage of cache_invalidate_user for iotlb invalidation, add a device cache and an invalidation op to test IOMMU_VIOMMU_INVALIDATE ioctl. Signed-off-by: Nicolin Chen --- drivers/iommu/iommufd/iommufd_test.h | 25 +++++++++ drivers/iommu/iommufd/selftest.c | 76 +++++++++++++++++++++++++++- 2 files changed, 100 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index f4bc23a92f9a..368076da10ca 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -54,6 +54,11 @@ enum { MOCK_NESTED_DOMAIN_IOTLB_NUM = 4, }; +enum { + MOCK_DEV_CACHE_ID_MAX = 3, + MOCK_DEV_CACHE_NUM = 4, +}; + struct iommu_test_cmd { __u32 size; __u32 op; @@ -152,6 +157,7 @@ struct iommu_test_hw_info { /* Should not be equal to any defined value in enum iommu_hwpt_data_type */ #define IOMMU_HWPT_DATA_SELFTEST 0xdead #define IOMMU_TEST_IOTLB_DEFAULT 0xbadbeef +#define IOMMU_TEST_DEV_CACHE_DEFAULT 0xbaddad /** * struct iommu_hwpt_selftest @@ -180,4 +186,23 @@ struct iommu_hwpt_invalidate_selftest { __u32 iotlb_id; }; +/* Should not be equal to any defined value in enum iommu_viommu_invalidate_data_type */ +#define IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST 0xdeadbeef +#define IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST_INVALID 0xdadbeef + +/** + * struct iommu_viommu_invalidate_selftest - Invalidation data for Mock VIOMMU + * (IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST) + * @flags: Invalidate flags + * @cache_id: Invalidate cache entry index + * + * If IOMMU_TEST_INVALIDATE_ALL is set in @flags, @cache_id will be ignored + */ +struct iommu_viommu_invalidate_selftest { +#define IOMMU_TEST_INVALIDATE_FLAG_ALL (1 << 0) + __u32 flags; + __u32 vdev_id; + __u32 cache_id; +}; + #endif diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index 4a23530ea027..8abffc7794c8 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -139,6 +139,7 @@ struct mock_dev { struct device dev; unsigned long flags; int id; + u32 cache[MOCK_DEV_CACHE_NUM]; }; struct selftest_obj { @@ -540,6 +541,74 @@ static int mock_dev_disable_feat(struct device *dev, enum iommu_dev_features fea return 0; } +static int mock_viommu_cache_invalidate(struct iommufd_viommu *viommu, + struct iommu_user_data_array *array) +{ + struct iommu_viommu_invalidate_selftest *cmds; + struct iommu_viommu_invalidate_selftest *cur; + struct iommu_viommu_invalidate_selftest *end; + int rc; + + /* A zero-length array is allowed to validate the array type */ + if (array->entry_num == 0 && + array->type == IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST) { + array->entry_num = 0; + return 0; + } + + cmds = kcalloc(array->entry_num, sizeof(*cmds), GFP_KERNEL); + if (!cmds) + return -ENOMEM; + cur = cmds; + end = cmds + array->entry_num; + + static_assert(sizeof(*cmds) == 3 * sizeof(u32)); + rc = iommu_copy_struct_from_full_user_array( + cmds, sizeof(*cmds), array, + IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST); + if (rc) + goto out; + + iommufd_viommu_lock_vdev_id(viommu); + while (cur != end) { + struct mock_dev *mdev; + struct device *dev; + int i; + + if (cur->flags & ~IOMMU_TEST_INVALIDATE_FLAG_ALL) { + rc = -EOPNOTSUPP; + goto out; + } + + if (cur->cache_id > MOCK_DEV_CACHE_ID_MAX) { + rc = -EINVAL; + goto out; + } + + dev = iommufd_viommu_find_device(viommu, cur->vdev_id); + if (!dev) { + rc = -EINVAL; + goto out; + } + mdev = container_of(dev, struct mock_dev, dev); + + if (cur->flags & IOMMU_TEST_INVALIDATE_FLAG_ALL) { + /* Invalidate all cache entries and ignore cache_id */ + for (i = 0; i < MOCK_DEV_CACHE_NUM; i++) + mdev->cache[i] = 0; + } else { + mdev->cache[cur->cache_id] = 0; + } + + cur++; + } +out: + iommufd_viommu_unlock_vdev_id(viommu); + array->entry_num = cur - cmds; + kfree(cmds); + return rc; +} + static const struct iommu_ops mock_ops = { /* * IOMMU_DOMAIN_BLOCKED cannot be returned from def_domain_type() @@ -566,6 +635,9 @@ static const struct iommu_ops mock_ops = { .map_pages = mock_domain_map_pages, .unmap_pages = mock_domain_unmap_pages, .iova_to_phys = mock_domain_iova_to_phys, + .default_viommu_ops = &(struct iommufd_viommu_ops){ + .cache_invalidate = mock_viommu_cache_invalidate, + }, }, }; @@ -691,7 +763,7 @@ static void mock_dev_release(struct device *dev) static struct mock_dev *mock_dev_create(unsigned long dev_flags) { struct mock_dev *mdev; - int rc; + int rc, i; if (dev_flags & ~(MOCK_FLAGS_DEVICE_NO_DIRTY | MOCK_FLAGS_DEVICE_HUGE_IOVA)) @@ -705,6 +777,8 @@ static struct mock_dev *mock_dev_create(unsigned long dev_flags) mdev->flags = dev_flags; mdev->dev.release = mock_dev_release; mdev->dev.bus = &iommufd_mock_bus_type.bus; + for (i = 0; i < MOCK_DEV_CACHE_NUM; i++) + mdev->cache[i] = IOMMU_TEST_DEV_CACHE_DEFAULT; rc = ida_alloc(&mock_dev_ida, GFP_KERNEL); if (rc < 0) From patchwork Tue Aug 27 16:59:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 822908 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2063.outbound.protection.outlook.com [40.107.92.63]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AAB851D31A7; Tue, 27 Aug 2024 17:02:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.92.63 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778128; cv=fail; b=OwIorZHUboBU5mHFbAg9Y5TcF9bkl7DwPNf6jm6EILjzi6c4wHgakRXlDdomc6zAyUGpZe7xFZF37tBTtFJdHAKuNpQH0JfKRBriCxReIJ5QaIIinypZy5rha4gwGyBrlN1pCichhuDl8wftnsK1uO2ST6WJIlJcsiq+r8jPuLM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778128; c=relaxed/simple; bh=4MPWhsqj/FebZM6x/n/994TUhm9A6//Gy9ZMdKgkZsk=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=NsSicRxXQgdB3BUckgpM3QBLSNk7wo7M51Iy1zL1o/II2E1PBKuph78UdwpO8GKPQ5ZhXemaOtGpoz16YfDrmX6HWjefnWOuvTYt/Ik+/3kfz103+Pf0vpaG6ncvl9bPb0OWOfcG1vBVLLBVPTzSAnSGA2iZsPm4LWQZXUEs/ic= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=Hri/Clc0; arc=fail smtp.client-ip=40.107.92.63 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="Hri/Clc0" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=pfmEiUWdJ8nVB0M/+8L37oyv8OfYaW4TrXMzVRMvMKIyppZ3F+41AsoOK/9hIe7wF0A3Bh6/sB9lOcffsK4uP4c42AcsbESimJUvjPmUQdp4vqpR2zZz0iyorurTDiVkj8JyjVGozNcJP7U8Y6xTzeXUOwmxemcAOtoXhfyxjiE5feGUJkMYj6wVgduqibvmRtJXs92lnAgZXMi+w4uOGqQlPuLlVyiulFiQJuhJ7PIc/cdgLBkmUzKkWvvdd0qtjL7KW7K98wZEZaGJAD+Zxq3+BwkCi6cVlA4QXuI9oLoYn7U74umpqypt9J4bZmkKayImS4sa1r8/18gv/GPQaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=rb50j7u4dY7uovSJkFOcRrYksErIeVocq8zXlcpiRJs=; b=cdAHpEWwzHKFwdyIjMAZCUb8V2g8TqPqPE86rqS7mZcZzmZKblHartGa0sudEV6ScKCqz8UOqid3vqip5EQJoJ0Ppd8CEJ8c12VyCbgU35xGvXIFNoHn+lfJhKFfnzkPrvK/xtlQjZqqrOQs+a+8TN8GRdeeYC3URB6P22hpVokEynVe4QJ7wCMETFivojvjsCyZlD5F+Qe0Xi6g+YTJBUHrbQ8PyOWd98vgf9gdK+4KDMVuD1oRIl0OLX9rfLjzUwruL6J2126HQKfwQc7OsX8gwV0oBe1D3nHX+kGOM/3Wv+PP0EkWWNUxDvp6mj8PvNxY26GLfCCG4KTNnnSkWw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.233) smtp.rcpttodomain=huawei.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=rb50j7u4dY7uovSJkFOcRrYksErIeVocq8zXlcpiRJs=; b=Hri/Clc04HEvD/gYnT8Z0iAd4UqLUAPMLmnzi7/HMEjX9vQGYrlI2qtLbAAQzkh+hIthl4cwh2TWPpImlGZXMzFFIpn00kCBtGX0hzoWaQqypvVwrDIwGCzBA9NFFsgiPj9QJLMtkNype21hStTul9n/iHVqfbWqFi2SPI7o3kaDE5CGpUhUlxY9l3UihVmlEFRVo9wBejVYj2Gl5VEGS0PJKb1Y2ZP6k2FZey8w+22K/pLlc0XTcGwvKwYJQBjsKkHIUMuUOoQ++j0BzIUA3nMtVQFaoF/syJke5y8QQq9+YCinw4AxeQTVHFssqPiqHrl9+iw/p9CbJjKnPZqKoQ== Received: from DS7P220CA0009.NAMP220.PROD.OUTLOOK.COM (2603:10b6:8:1ca::13) by BL1PR12MB5874.namprd12.prod.outlook.com (2603:10b6:208:396::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.24; Tue, 27 Aug 2024 17:02:00 +0000 Received: from CY4PEPF0000E9D3.namprd03.prod.outlook.com (2603:10b6:8:1ca:cafe::6f) by DS7P220CA0009.outlook.office365.com (2603:10b6:8:1ca::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.26 via Frontend Transport; Tue, 27 Aug 2024 17:01:59 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.233) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.233 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.233; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.233) by CY4PEPF0000E9D3.mail.protection.outlook.com (10.167.241.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.11 via Frontend Transport; Tue, 27 Aug 2024 17:01:59 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by mail.nvidia.com (10.127.129.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:47 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail203.nvidia.com (10.126.190.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:47 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Tue, 27 Aug 2024 10:01:46 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , Subject: [PATCH v2 13/19] iommufd/selftest: Add IOMMU_TEST_OP_DEV_CHECK_CACHE test command Date: Tue, 27 Aug 2024 09:59:50 -0700 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D3:EE_|BL1PR12MB5874:EE_ X-MS-Office365-Filtering-Correlation-Id: af78c2e3-1963-4eb5-6931-08dcc6b9f73b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|7416014|376014|82310400026|36860700013|1800799024; X-Microsoft-Antispam-Message-Info: Y6ZAP1MccFnI1sckeADIZaMEonMR07rtUuCcMkWPkJj+wTzNkCQzIvyyA65/XLOmbjXsgBgcabvFTypBf5rXyyTm2t0kRwlq/3dESz3hCgL0OqZu7Rq0ZNpeFRmGbJsxdDaeI23V/9JOALwEvqgCgxnBKXmQ97xfzQdCUB+Y/Vfncr7kO3Ej03k12HPeGoOIFFtqj9hiXC0TNmF2hLYWuksAPgH3RFsDp0SxPhLkSLTFGVH6+JRCl1/jHzsDRWmDr0pAuj7HbZMvebMAt5olUVaGQM/z2nzDtJR2HCn2DphWhopIJjqlIWCbKs/oOIZQbifmWQFlk2unh2E7rh/jSFS7MnQvs3a8OyWG9tLTSdK/HihsMERAlzRv06bnhMdAVIm4zI7z5UuV582Aj+IhPEWEE2b41GwNLQMALuyTcuwj5AMgayEcyXgl0VEmhJkYyK1XCKLET6gtIm/FwsHFhtB6//ngFo6qPKM/B0mdMsioxbfnY3auO/6JBGwM6LLv+oG9r3dT3fffpFXIvQMLmgBA1qy9q5imKl3LDFhXCXQI3Ic3OU1LmEs8+9SOV1Kj26NzbCzb6ipmZe2APY66EOgqFTC/KznWSAvivYPkUqG9nlZczPydDXTcFXPuOUHCyczT9cuVTxrr10PT56rCCbTRmSerSDofCxH0wgoVxjOo66ar9DqvRL50BSIV00RlBVQUfg11bSI8qylJO2oFreHSIoOd2dLvhK7ayLF0GY/isRBKk25gEswqw3bbRji1mP2DC5f1xp/3+pXnoiTm4x3P8BxGuhzBzL0LoSVPJY0cQagnP/1i7KW1RAI0UalqC3lrqkqyFRuauT6JhJt6hmWrkBi1ilTEgG4vvw+8r3eqFmZfXfGts1vabSG3Lc2tgD7FM5m5X8Kl7O1CGbG7EkqGPe1L3Qo5+UDp3lSSghl3BbRqzwd1uKXCMXg5iua8Ut1Omf70Hi3S5xBGSzbIJdtnGlspbSfEx6+Lb5ulUoBaiDGgxyJy4SSCfLe0594W+7iwV/gfiapxDXLPzJaNzP2ygXD4BKMpj2vvRqbtpz5beWb2PMf4nvOD4yLDyBMSoiJiKSr1giQw2yGb17ukWNUCHVW8feZSuYeJ+yPVs5JQIQNQKkh474w2ylFfJkfgWB2ilI2TDu4ezlMHU9yKZ6yrigQ5zoU3KWtSYwpabSzqgCFpIdZpvLQfXHwobAlJ5kQgbXMqhj7lBCeAiNxKBgXQ9b2GmjaA2sHhCSxqpl/CN7XAwW+mGg46Jdrb8Yr5WLSBLtAzftF+ksCfI3pDJmtXGQfkeVDvKs9QR9bpgEyvqeNrOGbwZ7AjEwfrVEMbEssZGJUcdR0nSo2rjaBznGpKfThogtm2IbCPgrKU6yceOo/0HnKyGInAPTtQybj5T8huviDr8khkBvfxtz8DOx80lDscD+QqQUyUG4rDGmHQikiyV4pEjWKCGh2lCyBI X-Forefront-Antispam-Report: CIP:216.228.118.233; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc7edge2.nvidia.com; CAT:NONE; SFS:(13230040)(7416014)(376014)(82310400026)(36860700013)(1800799024); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Aug 2024 17:01:59.8101 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: af78c2e3-1963-4eb5-6931-08dcc6b9f73b X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.118.233]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D3.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5874 Similar to IOMMU_TEST_OP_MD_CHECK_IOTLB verifying a mock_domain's iotlb, IOMMU_TEST_OP_DEV_CHECK_CACHE will be used to verify a mock_dev's cache. Signed-off-by: Nicolin Chen --- drivers/iommu/iommufd/iommufd_test.h | 5 ++++ drivers/iommu/iommufd/selftest.c | 24 +++++++++++++++++++ tools/testing/selftests/iommu/iommufd.c | 7 +++++- tools/testing/selftests/iommu/iommufd_utils.h | 24 +++++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index 368076da10ca..56bade6146ff 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -23,6 +23,7 @@ enum { IOMMU_TEST_OP_DIRTY, IOMMU_TEST_OP_MD_CHECK_IOTLB, IOMMU_TEST_OP_TRIGGER_IOPF, + IOMMU_TEST_OP_DEV_CHECK_CACHE, }; enum { @@ -140,6 +141,10 @@ struct iommu_test_cmd { __u32 perm; __u64 addr; } trigger_iopf; + struct { + __u32 id; + __u32 cache; + } check_dev_cache; }; __u32 last; }; diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index 8abffc7794c8..f512874105ac 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -1035,6 +1035,26 @@ static int iommufd_test_md_check_iotlb(struct iommufd_ucmd *ucmd, return rc; } +static int iommufd_test_dev_check_cache(struct iommufd_ucmd *ucmd, + u32 idev_id, unsigned int cache_id, + u32 cache) +{ + struct iommufd_device *idev; + struct mock_dev *mdev; + int rc = 0; + + idev = iommufd_get_device(ucmd, idev_id); + if (IS_ERR(idev)) + return PTR_ERR(idev); + mdev = container_of(idev->dev, struct mock_dev, dev); + + if (cache_id > MOCK_DEV_CACHE_ID_MAX || + mdev->cache[cache_id] != cache) + rc = -EINVAL; + iommufd_put_object(ucmd->ictx, &idev->obj); + return rc; +} + struct selftest_access { struct iommufd_access *access; struct file *file; @@ -1545,6 +1565,10 @@ int iommufd_test(struct iommufd_ucmd *ucmd) return iommufd_test_md_check_iotlb(ucmd, cmd->id, cmd->check_iotlb.id, cmd->check_iotlb.iotlb); + case IOMMU_TEST_OP_DEV_CHECK_CACHE: + return iommufd_test_dev_check_cache(ucmd, cmd->id, + cmd->check_dev_cache.id, + cmd->check_dev_cache.cache); case IOMMU_TEST_OP_CREATE_ACCESS: return iommufd_test_create_access(ucmd, cmd->id, cmd->create_access.flags); diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index 12b5a8f78d4b..1b45445dbd53 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -220,6 +220,8 @@ FIXTURE_SETUP(iommufd_ioas) for (i = 0; i != variant->mock_domains; i++) { test_cmd_mock_domain(self->ioas_id, &self->stdev_id, &self->hwpt_id, &self->device_id); + test_cmd_dev_check_cache_all(self->device_id, + IOMMU_TEST_DEV_CACHE_DEFAULT); self->base_iova = MOCK_APERTURE_START; } } @@ -1442,9 +1444,12 @@ FIXTURE_SETUP(iommufd_mock_domain) ASSERT_GE(ARRAY_SIZE(self->hwpt_ids), variant->mock_domains); - for (i = 0; i != variant->mock_domains; i++) + for (i = 0; i != variant->mock_domains; i++) { test_cmd_mock_domain(self->ioas_id, &self->stdev_ids[i], &self->hwpt_ids[i], &self->idev_ids[i]); + test_cmd_dev_check_cache_all(self->idev_ids[0], + IOMMU_TEST_DEV_CACHE_DEFAULT); + } self->hwpt_id = self->hwpt_ids[0]; self->mmap_flags = MAP_SHARED | MAP_ANONYMOUS; diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index be722ea88358..d697a7aa55c9 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -234,6 +234,30 @@ static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id, __u32 ft_i test_cmd_hwpt_check_iotlb(hwpt_id, i, expected); \ }) +#define test_cmd_dev_check_cache(device_id, cache_id, expected) \ + ({ \ + struct iommu_test_cmd test_cmd = { \ + .size = sizeof(test_cmd), \ + .op = IOMMU_TEST_OP_DEV_CHECK_CACHE, \ + .id = device_id, \ + .check_dev_cache = { \ + .id = cache_id, \ + .cache = expected, \ + }, \ + }; \ + ASSERT_EQ(0, \ + ioctl(self->fd, \ + _IOMMU_TEST_CMD(IOMMU_TEST_OP_DEV_CHECK_CACHE),\ + &test_cmd)); \ + }) + +#define test_cmd_dev_check_cache_all(device_id, expected) \ + ({ \ + int c; \ + for (c = 0; c < MOCK_DEV_CACHE_NUM; c++) \ + test_cmd_dev_check_cache(device_id, c, expected); \ + }) + static int _test_cmd_hwpt_invalidate(int fd, __u32 hwpt_id, void *reqs, uint32_t data_type, uint32_t lreq, uint32_t *nreqs) From patchwork Tue Aug 27 16:59:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 823338 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2059.outbound.protection.outlook.com [40.107.92.59]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5D5A51D0DC4; Tue, 27 Aug 2024 17:02:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.92.59 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778128; cv=fail; b=QtUZtTznCoO0XP+rhCKY7jZ9CaJX8h0o3LRkeDlRkw3Guf605yg4lqPIWEuWaQTZpZ+DuP2RzHy6Fb0SNGTGipDOEYwg5EBBBHhGX/jJ1KE5RsA89Q9krysGizwrsBLvTuzyeRcsgJcp7vcrP8pbFrWwT44ejkJjBkc4FdkcfJM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778128; c=relaxed/simple; bh=Nzaa0D+x+oEh/dLYkiiVM59gJ9x122X7ZJntP7gSbfY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=e+I4JFM8WOwPLovrHU5HjvmafKgQPlOX2fUw1ONSMRriUi3ad8nBQJjNuFC9ZLeDlY+ouS0sHCKMWiiTlfk13vCUEUAzZBz2USKB0JmI6RyWIKRtrE6436eWnhxJ35PbKSKdP6KHSGYQBXSupEZHENsrd+A6aTM9WD6qBV5PqyY= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=td+QZvF6; arc=fail smtp.client-ip=40.107.92.59 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="td+QZvF6" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=zIh8QKf8eD8oWf63R5gfNbqlHNqwdxlBQLE56nfVZBjpprod3npfs2sGX0AZITiwZOgnvwJUUS3xmXPCsAyGjXdnkcXKb4KW2cTDBteOF2NjUP3IQDEkCMuFC/wjW/u4XmzqA3hxJbK7lEjFqoEXi2V6i3AAkTW0G9nqcDQ486F11KJTuZZQZoqFILAHnGCMPja7EFB2geTpZD8bEeNACiIP3HpPoqSOUTYgNS7bgjo4GBvtyHtksC+/WmC5MwwFqoJTku+k6QEW3neb4FwuzjU/ntA8NdgKi9I10Gxb5AWpoWdRXXfIdgG7SNsS3HyxjGGSY/l8rlg0qKLSifz6xw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=0nk4Q0eDsD7jjc1TKGB5/bX01umEaSqa3c7SrbgA4KE=; b=j/ARjGVWVPSp/rq6FsLtxOvVbc0KJ0d0UWkT3Xg807BTwQAySSGJgP8JAtOvwKnrzjZu/O2mWeOIwMOwmeWUqfPTWOoDhsiYpV8ojJHiFbOMs78kAEUZZV35MSTyivEoiUIuE2Wi8UVlaSHACt6ebfG9jTEJlOoDGT7up9wXAAkre0IiGHpa4IQEOewnTXizgoixWtjtw2v89ZAiEIfSOuDaWbuiJqMDhVOs2Qqt5cugk41itW3doI1mKgFBivkX79d7uBIPRLwamUbONYdQmyAVlti/KsXAwIY4DfERNcbbEZ/RakaGEu33O+l+nnAaj6ualtEp/bykYyPatBSVHg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.232) smtp.rcpttodomain=huawei.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=0nk4Q0eDsD7jjc1TKGB5/bX01umEaSqa3c7SrbgA4KE=; b=td+QZvF6mcF8b5QlgYF6JWHS8ICr0/B8obcoebx9QtBNklo5D0wNu6nl4/E3l4ngn/xgC4gfSbeK0iBXoFdLw7uRoZXaytSjCl6bu0po4H/RdrT2+fhQbZsTsFL7SxR7WxxdqQRrgdJRjPJ/DqBz5ZasQwxco56H0wJVyihx9lctR0o2fSFGG046eurk1UgyPdh+3cLBGycGtShQil/cuWpYZQFOCJ+z8c9FF9Xt4XZ9FbL6J1vmGSAPNkogqNhNHuYbJ48egHJDqPGnW/1em/NG2YYmHaoGTRB4lmegm/muSiB09jT+j6PCJE3Oj/RkI1oglsahXfdY5JoimbEStg== Received: from SN6PR2101CA0015.namprd21.prod.outlook.com (2603:10b6:805:106::25) by DS0PR12MB8563.namprd12.prod.outlook.com (2603:10b6:8:165::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.26; Tue, 27 Aug 2024 17:02:00 +0000 Received: from SN1PEPF000397B4.namprd05.prod.outlook.com (2603:10b6:805:106:cafe::ec) by SN6PR2101CA0015.outlook.office365.com (2603:10b6:805:106::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.16 via Frontend Transport; Tue, 27 Aug 2024 17:02:00 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.232) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.232 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.232; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.232) by SN1PEPF000397B4.mail.protection.outlook.com (10.167.248.58) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.13 via Frontend Transport; Tue, 27 Aug 2024 17:02:00 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by mail.nvidia.com (10.127.129.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:48 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail203.nvidia.com (10.126.190.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:48 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Tue, 27 Aug 2024 10:01:47 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , Subject: [PATCH v2 14/19] iommufd/selftest: Add VIOMMU coverage for IOMMU_HWPT_INVALIDATE ioctl Date: Tue, 27 Aug 2024 09:59:51 -0700 Message-ID: <9c79e722a0bae7d266d0dfebf19f9586f4f725e9.1724776335.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SN1PEPF000397B4:EE_|DS0PR12MB8563:EE_ X-MS-Office365-Filtering-Correlation-Id: 2224753e-51a6-48a1-772d-08dcc6b9f779 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|36860700013|7416014|376014|82310400026|1800799024; X-Microsoft-Antispam-Message-Info: bZ1FUvynkK2cRLOwaCDiFtppi6w6nBpGm1qCfoI/bJ4Xb4uIibX+g0Lml5M0j0NUnhyaN8F3UbiNtrJlZ2utAkChhWnprN64zBI1rGkltclUlzHcSdMk3afg+E5QtsOMsuI6PvyGNAhHa72M3sVl3tT0DfSbxOhvsrnWH3MZBWRuxRKyEagN+OvZ1knMERQ8dXa8IzhRh7Jdla3Z8GZgAGgWx+D2sp5uszSvAZj+IVxtVSC256uDSn+gzZL0AviN+chDlU0F5JP5T5fRjWae8U862/obSWYYvnFZIEHHw3l5IQi6b0dy7YyKhvEljdQ66GS3NIqkVdmCr8xQPMKv0vBI+JIIkTVJ5UC47MoLeevQ1WklvSjGYdyhiimQwPNvbjNUrxfK/WK7SebqpZTLxOyJH6wM7EajYcPVf9yBLtQjoYosHG4NmCYHZO4LL1DBgyWqIrxDRPNXHg1RFQHzEw4NOTjecAAuPsrSisI+9fSgg5cjN4/lSMUIR458E3FcKpIbqMHzopBvceoU9sn5b4RfaUjUQnNesyMNdVrThEp15tCi3DKtF8K6xxQwP8TywxvjUKY1+ZH1Shi6uWq6it+XeVeZFvWowHSx+/JSv98sVEQNu46CFVuBzokqIvfJeQ8slM7ell+BNYDjrUp3m0wikdW07rk9GL4oYMUWr6dNmIhhnA5/rTCtZibm3Ow04YJQUp7YO3VSaZLL+31XeJ0bnZYCHQz+Xv0hj0X6Wqho56/GLwChTXzPOvqU7tDu8gm7nGI9mlCvAKo0b0EJeXy/oQSPd7C6iOIc/7eMIxqaeZnFkyNPq9P7nlVq3N3vSrvx8pcp3l79s3VJ8utA9nY+suwc7apHKS1RYoSWLzyOEWSbrudMRUeKKs1n4DTKuGkjgqP0pMi4xnqiNH9FQmuEO0A5TpUEGMJfTf9Q4VyiYkMAySud5Rvcsby1wj7P6XTCNEVub2pA45t3mJ1+6X8exm5AYyPfX+jUlfD6LvxxGuE7wpDNG/RMQA8HcpvRYePXgRk3pkQHNmJ8nHLUcdUyD/+guJ+LOoM26uhKgovRJNbK9MSfaQMXx7PkxQPl3fwS89lgBhP2jfkPXjFKvHP21YiU8ghwmsOD1GbSgVoq5EyzPwZMjJXzaG2PIZRKByMy+5A6N4S4DlqJqvgn9DpsPcW0tk4p5NirPHUtlpyNeh30tEsoi4a/MFxKSGhvk+JVPsVSLEngAqV57iV9UltVagqZQvPRC3Rnprmf5/APeVLn4xiB4D57jca8ULbreTWT07Xa3Awp3iUzsXLexeEiAXY2wIqfbpmk0lk3Xg+7dtslzAlH75slVd+kVqucT2qwdgdsw7VT1n+eZ20Yx/RG4rvDCpC9gzjJPC85GFPb5pqIzLM8nBvpm5rgxpneoZViyvfQiSWcqWuNgwlOkzEw5tvvxy1o5ViSRPqccTUM48GliqW4KSSsaCRkFan9 X-Forefront-Antispam-Report: CIP:216.228.118.232; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc7edge1.nvidia.com; CAT:NONE; SFS:(13230040)(36860700013)(7416014)(376014)(82310400026)(1800799024); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Aug 2024 17:02:00.1543 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2224753e-51a6-48a1-772d-08dcc6b9f779 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.118.232]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: SN1PEPF000397B4.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8563 Add a viommu_cache test function to cover VIOMMU invalidations using the updated IOMMU_VIOMMU_INVALIDATE ioctl, with similar postive and negative cases to the existing iotlb ones. Signed-off-by: Nicolin Chen --- tools/testing/selftests/iommu/iommufd.c | 190 ++++++++++++++++++ tools/testing/selftests/iommu/iommufd_utils.h | 32 +++ 2 files changed, 222 insertions(+) diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index 1b45445dbd53..6f1014cc208b 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -614,6 +614,196 @@ TEST_F(iommufd_ioas, viommu_default) } } +TEST_F(iommufd_ioas, viommu_dev_cache) +{ + struct iommu_viommu_invalidate_selftest inv_reqs[2] = {}; + struct iommu_hwpt_selftest data = { + .iotlb = IOMMU_TEST_IOTLB_DEFAULT, + }; + uint32_t nested_hwpt_id = 0, hwpt_id = 0; + uint32_t dev_id = self->device_id; + uint32_t viommu_id = 0; + uint32_t num_inv; + + if (dev_id) { + test_cmd_hwpt_alloc(dev_id, self->ioas_id, + IOMMU_HWPT_ALLOC_NEST_PARENT, &hwpt_id); + test_cmd_viommu_alloc(dev_id, hwpt_id, + IOMMU_VIOMMU_TYPE_DEFAULT, &viommu_id); + test_cmd_hwpt_alloc_nested(self->device_id, viommu_id, 0, + &nested_hwpt_id, + IOMMU_HWPT_DATA_SELFTEST, &data, + sizeof(data)); + test_cmd_mock_domain_replace(self->stdev_id, nested_hwpt_id); + test_cmd_viommu_set_vdev_id(viommu_id, dev_id, 0x99); + + test_cmd_dev_check_cache_all(dev_id, + IOMMU_TEST_DEV_CACHE_DEFAULT); + + /* Check data_type by passing zero-length array */ + num_inv = 0; + test_cmd_viommu_invalidate(viommu_id, inv_reqs, + sizeof(*inv_reqs), &num_inv); + assert(!num_inv); + + /* Negative test: Invalid data_type */ + num_inv = 1; + test_err_viommu_invalidate(EINVAL, viommu_id, inv_reqs, + IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST_INVALID, + sizeof(*inv_reqs), &num_inv); + assert(!num_inv); + + /* Negative test: structure size sanity */ + num_inv = 1; + test_err_viommu_invalidate(EINVAL, viommu_id, inv_reqs, + IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST, + sizeof(*inv_reqs) + 1, &num_inv); + assert(!num_inv); + + num_inv = 1; + test_err_viommu_invalidate(EINVAL, viommu_id, inv_reqs, + IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST, + 1, &num_inv); + assert(!num_inv); + + /* Negative test: invalid flag is passed */ + num_inv = 1; + inv_reqs[0].flags = 0xffffffff; + inv_reqs[0].vdev_id = 0x99; + test_err_viommu_invalidate(EOPNOTSUPP, viommu_id, inv_reqs, + IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST, + sizeof(*inv_reqs), &num_inv); + assert(!num_inv); + + /* Negative test: invalid data_uptr when array is not empty */ + num_inv = 1; + inv_reqs[0].flags = 0; + inv_reqs[0].vdev_id = 0x99; + test_err_viommu_invalidate(EINVAL, viommu_id, NULL, + IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST, + sizeof(*inv_reqs), &num_inv); + assert(!num_inv); + + /* Negative test: invalid entry_len when array is not empty */ + num_inv = 1; + inv_reqs[0].flags = 0; + inv_reqs[0].vdev_id = 0x99; + test_err_viommu_invalidate(EINVAL, viommu_id, inv_reqs, + IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST, + 0, &num_inv); + assert(!num_inv); + + /* Negative test: invalid cache_id */ + num_inv = 1; + inv_reqs[0].flags = 0; + inv_reqs[0].vdev_id = 0x99; + inv_reqs[0].cache_id = MOCK_DEV_CACHE_ID_MAX + 1; + test_err_viommu_invalidate(EINVAL, viommu_id, inv_reqs, + IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST, + sizeof(*inv_reqs), &num_inv); + assert(!num_inv); + + /* Negative test: invalid vdev_id */ + num_inv = 1; + inv_reqs[0].flags = 0; + inv_reqs[0].vdev_id = 0x9; + inv_reqs[0].cache_id = 0; + test_err_viommu_invalidate(EINVAL, viommu_id, inv_reqs, + IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST, + sizeof(*inv_reqs), &num_inv); + assert(!num_inv); + + /* + * Invalidate the 1st cache entry but fail the 2nd request + * due to invalid flags configuration in the 2nd request. + */ + num_inv = 2; + inv_reqs[0].flags = 0; + inv_reqs[0].vdev_id = 0x99; + inv_reqs[0].cache_id = 0; + inv_reqs[1].flags = 0xffffffff; + inv_reqs[1].vdev_id = 0x99; + inv_reqs[1].cache_id = 1; + test_err_viommu_invalidate(EOPNOTSUPP, viommu_id, inv_reqs, + IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST, + sizeof(*inv_reqs), &num_inv); + assert(num_inv == 1); + test_cmd_dev_check_cache(dev_id, 0, 0); + test_cmd_dev_check_cache(dev_id, 1, + IOMMU_TEST_DEV_CACHE_DEFAULT); + test_cmd_dev_check_cache(dev_id, 2, + IOMMU_TEST_DEV_CACHE_DEFAULT); + test_cmd_dev_check_cache(dev_id, 3, + IOMMU_TEST_DEV_CACHE_DEFAULT); + + /* + * Invalidate the 1st cache entry but fail the 2nd request + * due to invalid cache_id configuration in the 2nd request. + */ + num_inv = 2; + inv_reqs[0].flags = 0; + inv_reqs[0].vdev_id = 0x99; + inv_reqs[0].cache_id = 0; + inv_reqs[1].flags = 0; + inv_reqs[1].vdev_id = 0x99; + inv_reqs[1].cache_id = MOCK_DEV_CACHE_ID_MAX + 1; + test_err_viommu_invalidate(EINVAL, viommu_id, inv_reqs, + IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST, + sizeof(*inv_reqs), &num_inv); + assert(num_inv == 1); + test_cmd_dev_check_cache(dev_id, 0, 0); + test_cmd_dev_check_cache(dev_id, 1, + IOMMU_TEST_DEV_CACHE_DEFAULT); + test_cmd_dev_check_cache(dev_id, 2, + IOMMU_TEST_DEV_CACHE_DEFAULT); + test_cmd_dev_check_cache(dev_id, 3, + IOMMU_TEST_DEV_CACHE_DEFAULT); + + /* Invalidate the 2nd cache entry and verify */ + num_inv = 1; + inv_reqs[0].flags = 0; + inv_reqs[0].vdev_id = 0x99; + inv_reqs[0].cache_id = 1; + test_cmd_viommu_invalidate(viommu_id, inv_reqs, + sizeof(*inv_reqs), &num_inv); + assert(num_inv == 1); + test_cmd_dev_check_cache(dev_id, 0, 0); + test_cmd_dev_check_cache(dev_id, 1, 0); + test_cmd_dev_check_cache(dev_id, 2, + IOMMU_TEST_DEV_CACHE_DEFAULT); + test_cmd_dev_check_cache(dev_id, 3, + IOMMU_TEST_DEV_CACHE_DEFAULT); + + /* Invalidate the 3rd and 4th cache entries and verify */ + num_inv = 2; + inv_reqs[0].flags = 0; + inv_reqs[0].vdev_id = 0x99; + inv_reqs[0].cache_id = 2; + inv_reqs[1].flags = 0; + inv_reqs[1].vdev_id = 0x99; + inv_reqs[1].cache_id = 3; + test_cmd_viommu_invalidate(viommu_id, inv_reqs, + sizeof(*inv_reqs), &num_inv); + assert(num_inv == 2); + test_cmd_dev_check_cache_all(dev_id, 0); + + /* Invalidate all cache entries for nested_dev_id[1] and verify */ + num_inv = 1; + inv_reqs[0].vdev_id = 0x99; + inv_reqs[0].flags = IOMMU_TEST_INVALIDATE_FLAG_ALL; + test_cmd_viommu_invalidate(viommu_id, inv_reqs, + sizeof(*inv_reqs), &num_inv); + assert(num_inv == 1); + test_cmd_dev_check_cache_all(dev_id, 0); + + test_cmd_mock_domain_replace(self->stdev_id, hwpt_id); + test_ioctl_destroy(nested_hwpt_id); + test_cmd_mock_domain_replace(self->stdev_id, self->ioas_id); + test_ioctl_destroy(viommu_id); + test_ioctl_destroy(hwpt_id); + } +} + TEST_F(iommufd_ioas, hwpt_attach) { /* Create a device attached directly to a hwpt */ diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index d697a7aa55c9..0a81827b903f 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -289,6 +289,38 @@ static int _test_cmd_hwpt_invalidate(int fd, __u32 hwpt_id, void *reqs, data_type, lreq, nreqs)); \ }) +static int _test_cmd_viommu_invalidate(int fd, __u32 viommu_id, void *reqs, + uint32_t data_type, uint32_t lreq, + uint32_t *nreqs) +{ + struct iommu_hwpt_invalidate cmd = { + .size = sizeof(cmd), + .hwpt_id = viommu_id, + .data_type = data_type, + .data_uptr = (uint64_t)reqs, + .entry_len = lreq, + .entry_num = *nreqs, + }; + int rc = ioctl(fd, IOMMU_HWPT_INVALIDATE, &cmd); + *nreqs = cmd.entry_num; + return rc; +} + +#define test_cmd_viommu_invalidate(viommu, reqs, lreq, nreqs) \ + ({ \ + ASSERT_EQ(0, \ + _test_cmd_viommu_invalidate(self->fd, viommu, reqs, \ + IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST, \ + lreq, nreqs)); \ + }) +#define test_err_viommu_invalidate(_errno, viommu_id, reqs, data_type, lreq, \ + nreqs) \ + ({ \ + EXPECT_ERRNO(_errno, _test_cmd_viommu_invalidate( \ + self->fd, viommu_id, reqs, \ + data_type, lreq, nreqs)); \ + }) + static int _test_cmd_access_replace_ioas(int fd, __u32 access_id, unsigned int ioas_id) { From patchwork Tue Aug 27 16:59:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 822907 Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam02on2047.outbound.protection.outlook.com [40.107.212.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C7CE71D0DE4; Tue, 27 Aug 2024 17:02:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.212.47 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778131; cv=fail; b=KYoTIe+z3jOJHyHQTYQKZBPJ5kAJGNbtHrb4Lxq+Rbg6g7ZvUDw/mJyA7EiTiFU4atqSFXgtiPM0YVePcSiy24dpjU2w+LKDrt+0Ja2B/zEVrahYg+zKWq9FNE5frLwepD930TiCKSIgfZGZYFhqVV5nnTnoBeh1E1a+1C4fHqs= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778131; c=relaxed/simple; bh=xnyGNHjGocnlsbj9W6i134TLcyJ+LM+AAJts3mn8n/8=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=QkeY9Am6vhZ8fL2e19Pfj8H1Sh1xQJL4plC5B/QBYBjik+/q8GOiRewQ0pOhZmmRl+Yn+H8QomBIzYQGAPx2hlRulfNxrNo78GPoJRhf7bs3XDXFG8xirDzXA1wkTJqyatzdAuybQj9Ey/DHhY1i8RAFVXBY19cInGYFuq3E5xM= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=lwQMN/yM; arc=fail smtp.client-ip=40.107.212.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="lwQMN/yM" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=oehV5GHTHkgGzoSxlFP3fmIF+R1QaTcDIopDiWwzhAcqmCAvtDq/3ocPJOG5DulGKjT+ZsKGEP61SRAD0tB/0J94NpImrZhZBx8nYFl5V7P7EgoojU3GkHUovudxM/WOZTc8CznAzImv1WRAIX/+B7+01SAce0XgnbFNbRjuOWolFVT9LOXBgMqa0xQMFe4T1yHzlBxjSVHWjHl61XFTXQ88Vc1qrhxlnIZGL2l1RvaurUBHavqFJ90Rd8y8lpcHzEuM4qYTtnbhOE/xrsfs4A+xu2eAxkGUka/HLdSk/KnfHRKR9AQ6dCEmY0hHwOIR+gQ86eL3JeTvY5q5fIZIeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=5Pyph0r5FsokZPup8TAbFXRQjzjHbhzcfuIokg7cvBg=; b=e7palDS9y1dTs/ELBK1gF9zSvHp6lvqYeHpxKT6/GY3v+V9D7TqyZVok6SbTYFGOK3FdkZEXT+a8ckYwBT7ztIi/OZ7DExCMtXCbIdFjvWintBMG6tK6wQC57MP31aDMAqLpBpbEQa2pi/jhDvTwQxdvh0KcnmXZ9hLV/oyK3cCrsehxkftSPksHyHrn9MJ4zx3EFzESzJoODhm2UH9/+p4MqcAcQczmrZ3aM98Rzb9PIUXeOWRcw2Cx/Za2TvA0W405B2ddqrYuq4yonpwMY5GPjhHNk8xaV6xxMdni29S9JqaZuUYvJNCVX+67T/tANlwSAhzIFlnW+Oyf83a7vg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.233) smtp.rcpttodomain=huawei.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=5Pyph0r5FsokZPup8TAbFXRQjzjHbhzcfuIokg7cvBg=; b=lwQMN/yMBl5Jl0n1xTfy6eyoXC8BzM14tX73RNbvUpWfJ17cZLaXvCUCpFcUzBy0uSh8MIJOPn0K/cjQoV/UWqgbRW7jeET7WABaBA2E079W8ThLxUDobzw2ZX8fAZZPiy1sHunRgvrriFN+fNv8vipu2bzcwyVnPw5x3tO1FqS3lElaxpK8Y2A8D87krDKozSRkBOoJGnFgcB7HPxsp/GGdApuPlCdGZYZEzQ/8Cpg24r9Mq6W1eCw7aVqMyOxdnknKEd7GEk506mREhPyQ1NO7h+skKD427dpK6YgDanHihBuqsobl+7SDxhpN2B6vqxaytjoPx44/l1rxTVSzkw== Received: from PH7P220CA0110.NAMP220.PROD.OUTLOOK.COM (2603:10b6:510:32d::21) by SJ2PR12MB8784.namprd12.prod.outlook.com (2603:10b6:a03:4d0::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.25; Tue, 27 Aug 2024 17:02:01 +0000 Received: from CY4PEPF0000E9D4.namprd03.prod.outlook.com (2603:10b6:510:32d:cafe::a1) by PH7P220CA0110.outlook.office365.com (2603:10b6:510:32d::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.25 via Frontend Transport; Tue, 27 Aug 2024 17:02:01 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.233) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.233 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.233; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.233) by CY4PEPF0000E9D4.mail.protection.outlook.com (10.167.241.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.11 via Frontend Transport; Tue, 27 Aug 2024 17:02:01 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by mail.nvidia.com (10.127.129.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:50 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail203.nvidia.com (10.126.190.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:49 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Tue, 27 Aug 2024 10:01:48 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , Subject: [PATCH v2 15/19] iommufd/viommu: Add iommufd_viommu_to_parent_domain helper Date: Tue, 27 Aug 2024 09:59:52 -0700 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D4:EE_|SJ2PR12MB8784:EE_ X-MS-Office365-Filtering-Correlation-Id: 23d6dd6a-279b-4d06-f52e-08dcc6b9f825 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|1800799024|376014|7416014|36860700013|82310400026; X-Microsoft-Antispam-Message-Info: sHy2VatqK8VQMyLWw3pvj4blC2V1ntgNfxvDlz/yiMIo7GVCKf8I3O+H2UNs9xbx5W7IgU9g1DtUN6e6Lrk8LVxtb4gqE96deiWfOGaJOZCeYh8i5XJYUeLXpoG0dyovDMFlOcAIn5OdsoKh1RuL8nCj67gLYWKrfQB6GJSQRBMSHiaz9egw/cgUV9jVcfOsq4TWGFx+pxKICQ67789ymQd2Y2CDwFq6k35UlrYNk7vTm3+a+Le1PRIZDjkPS8A8ihiF3eGEt8fUSCWAHJhC45F/c328pq+ThNRCPXfsbPSxq/syP0FtdwiBuTy2R1JJk0N/SY9aDBRWi4vgcQvEQufb2qWRuOto/pA6Fcd3jdTiOKNsBsiH6MgV3YxEA5Z35mlJ89syR1Ce2HPI/BLK13n92YXKyz6PifBYPScIEDgmpcZ0EPQPecTYp+Em/oVvbGcerHX9HwLVTna5z8JUfU2kIhnqYU0v9WkPRjE76KW2/K1PCL8NJZu2F0QhwV2NOXWLYHoOcUch2fnLgLPnjvQ3wlCFJcIvOTo/55nXEDcHCrxFreOYW1+ZjqxVKVbOwteJF+wMPw3Bri63JIvRSLcEdRI1qrfrvAYhd18GCp1G+aqk1CWmAFAqQbC6l/3WGPE6ciBgO1Jit6ZubyDrlpMzVsokv0AfPd2od8wHyACQnUZwfAQNlBhcAQ4Ql8o5zBH1Fvti9Ug9lOGxIUI3njHgaw4RUPZDfGWTxD8rHC1ZPezwiDHj5xNILXQ5IltgUnsKMfOB2UhLiBju9qn5qz+YZa35GdhC4XDiex77BAXQO794S8eNQ857C17qxtAKTTnbpc0LwNzpKcQI4PCbcRhBQMPN0CYWEo9snCWhSOSjJhNduwLCqSZ2lmoP2TFvIB8ly/sUp/DjKKDtbB9btgRBAz4DqBOR4nsoQuZBOYj7Zt33RTjoO9gF6J2rtTvMjPJN7bsf+YoBuSaVKKvsuVHNCspAvAKUfazYdr3pPZQu+T7yPSFtemJfwxbsB4PCZa1n9FLhEPGKOnFBXRH8x9AlGo0pTc4empMOpqAWAeRSiz/cNRLrN7W70UXExOuGW7XUy0SprfaAcgTEDB411gCFVjjv3ARGyAyYZp+2gMrw1cQs0/crzAognrwsuTnVKjCLsRcrOq0alVRfQDT8q7XdA+cwhryXkulS7Vx66E/6NdfEKe9acWrRTT2w2YRQCMzvY4L/QoGfmYO5ZLAaO1Y2KW8wwIqGmhcqTjbO7pD0KLewP8G8r4D8QqDIP7QnNeEKX4+NkF2L9segCXuA20Vrr+E3bMtRbNFRy53GVKGuOdsO3YLvQpU0PhoGmtp8jkuZNeYRwdxUWW5HLe1j7B6CzxMOTFiFN1ezirXaKtOS3S7nt7aG/C3oeSBoFtJcRuoj/sGttGXZJV8dvHOVRSl8FMlWdexSODbRP88Y6W+F3j2aYyTWXFZYvRYNGNWE X-Forefront-Antispam-Report: CIP:216.228.118.233; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc7edge2.nvidia.com; CAT:NONE; SFS:(13230040)(1800799024)(376014)(7416014)(36860700013)(82310400026); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Aug 2024 17:02:01.2535 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 23d6dd6a-279b-4d06-f52e-08dcc6b9f825 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.118.233]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D4.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB8784 Allow an IOMMU driver to convert a core-managed viommu to a nested parent domain for the info that the domain holds. Signed-off-by: Nicolin Chen --- drivers/iommu/iommufd/viommu_api.c | 14 ++++++++++++++ include/linux/iommufd.h | 8 ++++++++ 2 files changed, 22 insertions(+) diff --git a/drivers/iommu/iommufd/viommu_api.c b/drivers/iommu/iommufd/viommu_api.c index e0ee592ce834..3772a5892a6c 100644 --- a/drivers/iommu/iommufd/viommu_api.c +++ b/drivers/iommu/iommufd/viommu_api.c @@ -37,3 +37,17 @@ struct device *iommufd_viommu_find_device(struct iommufd_viommu *viommu, u64 id) return vdev_id->idev->dev; } EXPORT_SYMBOL_NS_GPL(iommufd_viommu_find_device, IOMMUFD); + +/* + * Convert a viommu to its encapsulated nest parent domain. Caller must be aware + * of the lifecycle of the viommu pointer. Only call this function in a callback + * function where viommu is passed in by the iommu/iommufd core. + */ +struct iommu_domain * +iommufd_viommu_to_parent_domain(struct iommufd_viommu *viommu) +{ + if (!viommu || !viommu->hwpt) + return NULL; + return viommu->hwpt->common.domain; +} +EXPORT_SYMBOL_NS_GPL(iommufd_viommu_to_parent_domain, IOMMUFD); diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h index 364f151d281d..f7c265c6de7c 100644 --- a/include/linux/iommufd.h +++ b/include/linux/iommufd.h @@ -92,6 +92,8 @@ int iommufd_vfio_compat_set_no_iommu(struct iommufd_ctx *ictx); void iommufd_viommu_lock_vdev_id(struct iommufd_viommu *viommu); void iommufd_viommu_unlock_vdev_id(struct iommufd_viommu *viommu); struct device *iommufd_viommu_find_device(struct iommufd_viommu *viommu, u64 id); +struct iommu_domain * +iommufd_viommu_to_parent_domain(struct iommufd_viommu *viommu); #else /* !CONFIG_IOMMUFD */ static inline struct iommufd_ctx *iommufd_ctx_from_file(struct file *file) { @@ -145,5 +147,11 @@ struct device *iommufd_viommu_find_device(struct iommufd_viommu *viommu, u64 id) { return NULL; } + +static inline struct iommu_domain * +iommufd_viommu_to_parent_domain(struct iommufd_viommu *viommu) +{ + return NULL; +} #endif /* CONFIG_IOMMUFD */ #endif From patchwork Tue Aug 27 16:59:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 822906 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2069.outbound.protection.outlook.com [40.107.244.69]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C81DD1D47A8; Tue, 27 Aug 2024 17:02:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.244.69 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778136; cv=fail; b=EZUvNPLGfhe3u6v04LFsf1HaohylnH1oolhue//q4qoGw282cL903oHbx+0/T9e8qMo2/DM+lIzR1+z9ZRvv58G7eUobiGS2YkjkIk8vhWF/6UfW6gkeuMOrbUUbIN73mOTYiyGtGUqda/YMkRAJlFsi/fKrrur43jpATY5h9lY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778136; c=relaxed/simple; bh=0gOPJWglZCrj0XGM0MvHTvCgRioePLfXKFWXa9BpxSc=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=fjhlCioeFIuJUGBOhITBDkgqYZswFW3G1OY7Dq+A1qKD8Lm79A0Mx0idL2/i0l20PgELAJwey0Ax7gwfZ7xaw8PtDg+B0h9E7wSNxlOkA9VvyZxtZojeXhVwmHmSFdyIYY5qX8hajQP0rAf9pXz8I8VHrB3TC7HJZ769nkob9gY= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=C5a9J5J/; arc=fail smtp.client-ip=40.107.244.69 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="C5a9J5J/" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ZEuGyeeN71qV1zN+IDD2T0gOaSZgAZQmeeKyxkbZD47cLyxv6C+1Gvo6DDrNPEryZqIMX3ot4qJ3i1jBgI1qE7LMfLrzAyqIryfzGsah1rJFdzcdcp1dKb5cUfPsVaQLWAel/Wu3aZHdx6Fy8fMRO33k6Zq97nYtvd0XCybN7EvJ5Y70PUrMuDU2oPauZFWGzOW/OCM2/7B7LLiLaHa1RXglOMNWfOrNROO80CYZx2xV1JnIAMS1lnhCmRtxEixUI0J4+Ki3L6HJhyx6BV/JOWuTuh9y6n2CsnU7bfgHwWee0ghzO7bA+hNIspjEo6FS8Rt+Gks5uVKx09WHxlQGMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=fgQHuGwJdlBZ3eGDsGtZNadxJL1VrI90zT+eC/OkuC4=; b=PUIoJpmossnND+5Z+/vzE+ZZbhbTGPgpwO23/YL4HYOthp4N8ZaJXOaE+KcwkkxghwyZ8usx9zmuA/Gs3Z0VpRp0Uoxdm5huq0xrSdDuNON42L0cw/1R1e0i7lXxYeNAAmxeakAJ3c+fMCaskqScR64RtHto/USYnGHoLTtHqF6eK43kqN2VYvy96yuRNKNOspakLiqxqZJaLp8xC/xcP40psZkmAcEg+l0faW2/4t9ji1xlTZDs7K6S+8ZFWG9O+/Q7fHwIc1EKhL5heAFI5seWL3rUJp2gFqbAsepdZQNnQLbNgTOfozcgiduSrzV4mfGU2GH7EDdesMyAJJD+vA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.232) smtp.rcpttodomain=huawei.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=fgQHuGwJdlBZ3eGDsGtZNadxJL1VrI90zT+eC/OkuC4=; b=C5a9J5J/hK9oE74nL5Ps1G0jW4OFS14/Te+sFz2/GFSCCnLPoE0RLulaLnQBi15jM8ZCeNyl4o+nyz7mfBa1E8KDxZTHZAycCBMnoJg/QLrmbBMfTbsxkLAntk32GAQAmD1Ku0RLFEFa33jWCJFWzB8Hgr/Z1SBIKUFv3C+bRMO0idd0fDRIjOAzTlhkYoPurtm+DZDhQKeaxG3p7HCJee88Iy9t6YLc+wT8BJ0f7Mgyq0Y0+ygAAIpeyGOguk7gBdAe5BgNkkx9XUNrI4oXqf25Uf8RxIQ/qKLOtRrJbVgxUftk+7aixHTOfBEKhPjsbmvuZZBZDLnjzFRzutjKSQ== Received: from PH7PR17CA0050.namprd17.prod.outlook.com (2603:10b6:510:325::22) by DS7PR12MB6312.namprd12.prod.outlook.com (2603:10b6:8:93::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.25; Tue, 27 Aug 2024 17:02:05 +0000 Received: from SN1PEPF000397AE.namprd05.prod.outlook.com (2603:10b6:510:325:cafe::b3) by PH7PR17CA0050.outlook.office365.com (2603:10b6:510:325::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.21 via Frontend Transport; Tue, 27 Aug 2024 17:02:05 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.232) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.232 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.232; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.232) by SN1PEPF000397AE.mail.protection.outlook.com (10.167.248.52) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.13 via Frontend Transport; Tue, 27 Aug 2024 17:02:04 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by mail.nvidia.com (10.127.129.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:51 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail203.nvidia.com (10.126.190.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:50 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Tue, 27 Aug 2024 10:01:50 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , Subject: [PATCH v2 16/19] iommu/arm-smmu-v3: Add arm_smmu_cache_invalidate_user Date: Tue, 27 Aug 2024 09:59:53 -0700 Message-ID: <3548bfff43a0e1c072b77fc37a1a8a6c930bcec9.1724776335.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SN1PEPF000397AE:EE_|DS7PR12MB6312:EE_ X-MS-Office365-Filtering-Correlation-Id: e48426dc-876e-43c0-e8ef-08dcc6b9fa3f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|36860700013|1800799024|376014|7416014|82310400026; X-Microsoft-Antispam-Message-Info: HqQUgPGKhDgAA5/dgaTpkdnSmr6EUsI/kEQUsMdvkqgtDYfuvUCwTcy0NVbygrTYVmNpAlwUElxdo2fDIm46rj4Np3tpdILnYY1fCc7vtAdNfX5v5HUdtTyKN7YxQLyV7PfyzopOma+QAgAd+yqIq37GA1R9vfIh/4Ty4u614S2BBXMKRHj4YGSKfzpUjRasKdK9dnnw5Pq3snAQuxvWSNlMcrR2J981OSYxjLoupTFaRwsj2KYSLPtF5p2AQ3aQdqnVNOehX6jhoGBHm+i25f8hxYvLfPZvuOnsHdUlKvZ/dUFaLjIUhfoWaMAVlblnlARp4nsc9FSMDeBJqKyS+jbowB1uo4F1DXjjaRWrcnYeGr1ZXhBiOacLZZR0qHLoYKxVysW0wBaxo2dbl5TTuMc2azFl2VuN5fqdpIyG/+RZj79GoVBWEtQk3GWQfylLGpoagd2qHkSkcHGA0F2cB9McBMKCzrW+CgWZ938cuJzupXDhNajSn7Y7tQxbuq8zJ3YlWPoSGCtOiU/+bkQrx7zJNrIhw2jC+ag+S5oQO8ZUWMAuNcWEgYlbmO0GdDPz5DUiAfMpzb5aFOheXoqXzxqgMCrwnSaHv7DBQgKAlc4PLvFtguCHkNZXwrbYWkUCffvmc/amFuIebFN5NzgwodhTfdsFr/IqEpTUxHJb5fUZImhsYny7C2O6Xjm1i1CiFfSPEMBEODr0shALjF+fZjx2KLTi0p83qyNlkVui9KiKYSMxYRjyIyfOqpb74bxy4UqxsUZNgg1Vt9ebaGHFV+G3aWK0RFu6wovTE1CLnPy9g9y3DJ+K1bVuhFazm1I4HNV/yrmns+Hy+p8wil66UrQTi9lzompEhd+y/PP9GSVi0H+XSXfDrxbiTXL+hRqn+tfUWwxQ3+NDIQ4R0oBmzFSz+vEW3F6LdvmRnpryQMi1gF3wuqFCABUN50ljeLqL451yPJ8Nh54WNOzCr91G7/NNe7BwwIO0N/iRhxut5q1fPvHsk6ZrDgR+VHEsXC/FCWAMh4QAzjZaYGYPkQkZtwHq4nF4o0+C6ZwOD2riZwD6SAjs5q+cHIcM5j2tI4v5ZfUw+QhU3LigTcyijuWdjWcmDR/Md4kkfbZnTHlmy0norKElcxt/UqF16pKpTBKZNy7lO5MjESiPhQ3vFjqPY9Nx7s7ZpDHgUvPQifhxeU+KNt7Dv6uc7LnXj8TU4P5kHiwMltds31NzX4rEuvgKyowYidz+CNZmuAAJWSXQjbCsw3/7bjzXq1JTIbKYR4kaScrUzwAiOUC1+M7ufaRLhORlQn2XMAqf4O+cOHVPStKt+4KVgU7bc+Z+rpseGLgavKuJJ95g3Y9SgUoens+HusEDTYi9K6Pi9lRvFYQHAomCOJA+Qi9VPVVNCWpxFfmIFOh/24MHFepMA3KsvxQhIoGCt0lscYgg2Cup84QitDQApf8evYgtyWY+LvM+AABi X-Forefront-Antispam-Report: CIP:216.228.118.232; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc7edge1.nvidia.com; CAT:NONE; SFS:(13230040)(36860700013)(1800799024)(376014)(7416014)(82310400026); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Aug 2024 17:02:04.8259 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e48426dc-876e-43c0-e8ef-08dcc6b9fa3f X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.118.232]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: SN1PEPF000397AE.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB6312 Add arm_smmu_cache_invalidate_user() function for user space to invalidate IOTLB entries that are still cached by the hardware. Add struct iommu_hwpt_arm_smmuv3_invalidate defining an invalidation entry that is simply the native format of a 128-bit TLBI command. Scan commands against the permitted command list and fix their VMID fields. Co-developed-by: Eric Auger Signed-off-by: Eric Auger Co-developed-by: Jason Gunthorpe Signed-off-by: Jason Gunthorpe Signed-off-by: Nicolin Chen --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 115 ++++++++++++++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 1 + include/uapi/linux/iommufd.h | 21 ++++ 3 files changed, 137 insertions(+) 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 6d40f1e150cb..a2af693bc7b2 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -3267,10 +3267,117 @@ static void arm_smmu_domain_nested_free(struct iommu_domain *domain) kfree(container_of(domain, struct arm_smmu_nested_domain, domain)); } +/* + * Convert, in place, the raw invalidation command into an internal format that + * can be passed to arm_smmu_cmdq_issue_cmdlist(). Internally commands are + * stored in CPU endian. + * + * Enforce the VMID on the command. + */ +static int +arm_smmu_convert_user_cmd(struct arm_smmu_domain *s2_parent, + struct iommu_hwpt_arm_smmuv3_invalidate *cmd) +{ + u16 vmid = s2_parent->s2_cfg.vmid; + + cmd->cmd[0] = le64_to_cpu(cmd->cmd[0]); + cmd->cmd[1] = le64_to_cpu(cmd->cmd[1]); + + switch (cmd->cmd[0] & CMDQ_0_OP) { + case CMDQ_OP_TLBI_NSNH_ALL: + /* Convert to NH_ALL */ + cmd->cmd[0] = CMDQ_OP_TLBI_NH_ALL | + FIELD_PREP(CMDQ_TLBI_0_VMID, vmid); + cmd->cmd[1] = 0; + break; + case CMDQ_OP_TLBI_NH_VA: + case CMDQ_OP_TLBI_NH_VAA: + case CMDQ_OP_TLBI_NH_ALL: + case CMDQ_OP_TLBI_NH_ASID: + cmd->cmd[0] &= ~CMDQ_TLBI_0_VMID; + cmd->cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_VMID, vmid); + break; + default: + return -EIO; + } + return 0; +} + +static int __arm_smmu_cache_invalidate_user(struct arm_smmu_domain *s2_parent, + struct iommu_user_data_array *array) +{ + struct arm_smmu_device *smmu = s2_parent->smmu; + struct iommu_hwpt_arm_smmuv3_invalidate *last_batch; + struct iommu_hwpt_arm_smmuv3_invalidate *cmds; + struct iommu_hwpt_arm_smmuv3_invalidate *cur; + struct iommu_hwpt_arm_smmuv3_invalidate *end; + struct arm_smmu_cmdq_ent ent; + struct arm_smmu_cmdq *cmdq; + int ret; + + /* A zero-length array is allowed to validate the array type */ + if (array->entry_num == 0 && + array->type == IOMMU_HWPT_INVALIDATE_DATA_ARM_SMMUV3) { + array->entry_num = 0; + return 0; + } + + cmds = kcalloc(array->entry_num, sizeof(*cmds), GFP_KERNEL); + if (!cmds) + return -ENOMEM; + cur = cmds; + end = cmds + array->entry_num; + + static_assert(sizeof(*cmds) == 2 * sizeof(u64)); + ret = iommu_copy_struct_from_full_user_array( + cmds, sizeof(*cmds), array, + IOMMU_HWPT_INVALIDATE_DATA_ARM_SMMUV3); + if (ret) + goto out; + + ent.opcode = cmds->cmd[0] & CMDQ_0_OP; + cmdq = arm_smmu_get_cmdq(smmu, &ent); + + last_batch = cmds; + while (cur != end) { + ret = arm_smmu_convert_user_cmd(s2_parent, cur); + if (ret) + goto out; + + /* FIXME work in blocks of CMDQ_BATCH_ENTRIES and copy each block? */ + cur++; + if (cur != end && (cur - last_batch) != CMDQ_BATCH_ENTRIES - 1) + continue; + + ret = arm_smmu_cmdq_issue_cmdlist(smmu, cmdq, last_batch->cmd, + cur - last_batch, true); + if (ret) { + cur--; + goto out; + } + last_batch = cur; + } +out: + array->entry_num = cur - cmds; + kfree(cmds); + return ret; +} + +static int arm_smmu_cache_invalidate_user(struct iommu_domain *domain, + struct iommu_user_data_array *array) +{ + struct arm_smmu_nested_domain *nested_domain = + container_of(domain, struct arm_smmu_nested_domain, domain); + + return __arm_smmu_cache_invalidate_user( + nested_domain->s2_parent, array); +} + static const struct iommu_domain_ops arm_smmu_nested_ops = { .get_msi_mapping_domain = arm_smmu_get_msi_mapping_domain, .attach_dev = arm_smmu_attach_dev_nested, .free = arm_smmu_domain_nested_free, + .cache_invalidate_user = arm_smmu_cache_invalidate_user, }; static struct iommu_domain * @@ -3298,6 +3405,14 @@ arm_smmu_domain_alloc_nesting(struct device *dev, u32 flags, !(master->smmu->features & ARM_SMMU_FEAT_S2FWB)) return ERR_PTR(-EOPNOTSUPP); + /* + * FORCE_SYNC is not set with FEAT_NESTING. Some study of the exact HW + * defect is needed to determine if arm_smmu_cache_invalidate_user() + * needs any change to remove this. + */ + if (WARN_ON(master->smmu->options & ARM_SMMU_OPT_CMDQ_FORCE_SYNC)) + return ERR_PTR(-EOPNOTSUPP); + ret = iommu_copy_struct_from_user(&arg, user_data, IOMMU_HWPT_DATA_ARM_SMMUV3, ste); if (ret) 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 79afaef18906..6c8ae70c90fe 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -523,6 +523,7 @@ struct arm_smmu_cmdq_ent { #define CMDQ_OP_TLBI_NH_ALL 0x10 #define CMDQ_OP_TLBI_NH_ASID 0x11 #define CMDQ_OP_TLBI_NH_VA 0x12 + #define CMDQ_OP_TLBI_NH_VAA 0x13 #define CMDQ_OP_TLBI_EL2_ALL 0x20 #define CMDQ_OP_TLBI_EL2_ASID 0x21 #define CMDQ_OP_TLBI_EL2_VA 0x22 diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index fd7d16fd441d..f3aefb11f681 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -685,9 +685,11 @@ struct iommu_hwpt_get_dirty_bitmap { * enum iommu_hwpt_invalidate_data_type - IOMMU HWPT Cache Invalidation * Data Type * @IOMMU_HWPT_INVALIDATE_DATA_VTD_S1: Invalidation data for VTD_S1 + * @IOMMU_HWPT_INVALIDATE_DATA_ARM_SMMUV3: Invalidation data for ARM SMMUv3 */ enum iommu_hwpt_invalidate_data_type { IOMMU_HWPT_INVALIDATE_DATA_VTD_S1 = 0, + IOMMU_HWPT_INVALIDATE_DATA_ARM_SMMUV3 = 1, }; /** @@ -726,6 +728,25 @@ struct iommu_hwpt_vtd_s1_invalidate { __u32 __reserved; }; +/** + * struct iommu_hwpt_arm_smmuv3_invalidate - ARM SMMUv3 cahce invalidation + * (IOMMU_HWPT_INVALIDATE_DATA_ARM_SMMUV3) + * @cmd: 128-bit cache invalidation command that runs in SMMU CMDQ. + * Must be little-endian. + * + * Supported command list: + * CMDQ_OP_TLBI_NSNH_ALL + * CMDQ_OP_TLBI_NH_VA + * CMDQ_OP_TLBI_NH_VAA + * CMDQ_OP_TLBI_NH_ALL + * CMDQ_OP_TLBI_NH_ASID + * + * -EIO will be returned if the command is not supported. + */ +struct iommu_hwpt_arm_smmuv3_invalidate { + __aligned_u64 cmd[2]; +}; + /** * struct iommu_hwpt_invalidate - ioctl(IOMMU_HWPT_INVALIDATE) * @size: sizeof(struct iommu_hwpt_invalidate) From patchwork Tue Aug 27 16:59:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 823337 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2042.outbound.protection.outlook.com [40.107.243.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 30A051D45F5; Tue, 27 Aug 2024 17:02:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.243.42 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778134; cv=fail; b=WI0NzaajY5IR7sLEQo42c4Wei50vdpOhb8mWUoJzMvZtWvhHI4WL+m4DlPFr5xLif6xBVwnExOSxO0w54CrApmcKry+eGjUEYtVAJGE673EXuXs5Ka3BwoSiTE8Z40ftRrJNePRtTjAPXBLZJS77eaEuFKAgUs6Yk4Yf0qamJsg= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778134; c=relaxed/simple; bh=z6CoklI7fGfDu2fOkVYhUvd0cQDAkAJWwtzQULY6cJY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=vETHEH5idwpS4fyEZA4plcjq2H3mbV0sMDqHQZv+XLBIN0OsG1MMbmnClmWH7/Ct09C5l9IlxsAil1zbevXKq+pqoaj6GR4Np3+vxMWh5Btu0gFzqzaHgnAWxNcpowObwj5fKS/08zO7f/6+EstKMnQaY017mKd7jX6tNRGVu/I= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=Ur+OorAB; arc=fail smtp.client-ip=40.107.243.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="Ur+OorAB" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=TA4QwYUX5tQ7grnyv4HsptQKGtSCSmiNc27DF4TaSUogThGUAQZhyN9DMlwPdIYPCHAQEK105Qv64EmfK1f9LJDsMlTg72Fn9brcz6zqaljHsqar2cQ9gUIzStpZuRFi9tMghR1cpEAjGzrPu3nb6e/yi6oCCAZoN/less8m/I1Kc3HZOo1am0VLxO1py3mshEOwx+j+prQaYDyMPyhey2PvbYdjv9dYgjMWcm63mmjY2cr1A1thtqCwqg977I0TMU7CeCyyfJsRJC6fqwsL7BqxDm86n6v4bNWtwQRt07NGqPV9dwy2D1Sv/pzjyolHzR0A2/rdMGYBC0nLuq15Hg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=M9gwxzjSvuGPTbOjmKyokAYc9NQ9/GvPyzwo1NIRlQA=; b=h9VMi3I04nOFfjw+22wHW1+Xi5nrP1OHrvjh6eXUDC9HXW4evIPeuIGMXijgDquNhWrRgiN9RCa//RAZR9uMEv1ichrvIO3RuaDMmQ1nW3U7LuPLm3psKQeYDM+UVUXsuIeNFwjKXDTHyIcK9sD4BWue3uooU70L3jPp5agWn7HzYkOuE5lvAaG9fw03676ZLtz5ILn0z7oKnVIYR1Y4knwCg1cnLpw355rRcqRYJ7rIZkLwS9DQfgHJsqyE5DLn86bdXsboqDK28/GExmSglcY1U94OVGuSb9cwRLGWoT3YPa/z5HR9jW/d/pdn9pi4bEip2BrUQQI3Oajg9968aw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.232) smtp.rcpttodomain=huawei.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=M9gwxzjSvuGPTbOjmKyokAYc9NQ9/GvPyzwo1NIRlQA=; b=Ur+OorABI7h7xR/vSSBobkCQkHaHvwELtu2xQg8HumIbgnjw1Q+32qlu2iUGkPDMZjXqWEtSKCXXB9EcluUHKSo59rYJBQpdjaPRMMfedSusU8tt+LxXvazWwMPhvMZtNX5Jq24GRqThXgpWf4iZtemLkEuBBgKSgLYbuxbqpAKab+MM1+sBvg3gwDLyGmDN/11pgPi8qHfbY7/q6mhJR2chyudo3Ic6jvr1EaJi40LMpuqN0GXTNFmF9Ydcfp/4VCyhpW7JQlKd55mus0qYrn0z/ej6VkPuLjVS1qxApUCiupJDoxd7oRR797vq3ofMMyPD3hZlZSED//gwkW7TXw== Received: from PH7PR17CA0072.namprd17.prod.outlook.com (2603:10b6:510:325::20) by SA0PR12MB4381.namprd12.prod.outlook.com (2603:10b6:806:70::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.25; Tue, 27 Aug 2024 17:02:06 +0000 Received: from SN1PEPF000397AE.namprd05.prod.outlook.com (2603:10b6:510:325:cafe::7d) by PH7PR17CA0072.outlook.office365.com (2603:10b6:510:325::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.20 via Frontend Transport; Tue, 27 Aug 2024 17:02:06 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.232) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.232 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.232; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.232) by SN1PEPF000397AE.mail.protection.outlook.com (10.167.248.52) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.13 via Frontend Transport; Tue, 27 Aug 2024 17:02:06 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by mail.nvidia.com (10.127.129.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:52 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail203.nvidia.com (10.126.190.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:52 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Tue, 27 Aug 2024 10:01:51 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , Subject: [PATCH v2 17/19] iommu/arm-smmu-v3: Add arm_smmu_viommu_cache_invalidate Date: Tue, 27 Aug 2024 09:59:54 -0700 Message-ID: <4b61aba3bc6c1cce628d9db44d5b18ea567a8be1.1724776335.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SN1PEPF000397AE:EE_|SA0PR12MB4381:EE_ X-MS-Office365-Filtering-Correlation-Id: 03b50a8f-8dda-4362-7eae-08dcc6b9faf7 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|376014|7416014|82310400026|36860700013|1800799024; X-Microsoft-Antispam-Message-Info: 7/5co3YfiFIiHlr7VIKZXaf13Pk20CqvzPtsNpP2c3cPpdfWqRxNbTwowGyyn31cC64zJIcgim73jIn1pUE/znzPGgF+XSt93/aozXaVCUhm4veXRPsYKFiFhHvSNi7MPNq4DaUoTPged6R7NU58kAH8OCClUhcP/nqMLLyLklqZ0XAIHtpA7hHBbSJ2AXjZd4sIXILkk5b/s+W4FkKfQPJ8hAKyAkWrDlhFpiH9NRbG4lgIMRNMUo+Qd7EqjVFFb32qoD1BPWxSosCzLyWI3oHF+aS9RHqIofrXIsocmN9exl/AkorsQBgnb/Jr91Yn9tUWPwUGY1NLUc8OkZgR6fxcUiq2x3/noJDi1TA3dd5wccxm3bpAQbGJwqSG7LEq0DKqSKu9LUWip0t16aGBFj9ZUtxXJR5s1/Y6pswHqkbq8N9rf1x11YvWBiyq47erkMZTCRxjiq3FfDGB2iIBYlT71sck0CAQu2pOd+MOEs0K2CTORk2w786Tcq2pdX4DnKsLVF50o4LBzwsrQkx2Q8WVTnPwCczFlOLwdct1htBCOAg3rnsRZyekGNwjBYkVlv0jLf6VkMCH/7/IjIJixHh3WA9EAxAvy5Afl0t8OBCNCXRwNJc9Qwtu4vBpVvCblwC25qGMaHFC57qyPosdwn1Uzl5LBgfUn6MljNgMO36Xv7Ks6ubAhdKmdou5Q2nY3PL8c3HU/JpKs9A0DFw1jc1+Uz+UiDjpuWrTxzgqElzb67IUPGw2k0z/O/ic+L3YstAyG6e7Hla++nJ9jkezN0E7BsHwJMKMerIUU2Ioe9gmxlrcP7wbuw5CN7fKb62S708QqiB7ZA3ssrl1ZdQ7kvi2EeUudbCj8Xb6TOIQl+FMcIWI0fDnqVyG4E0UAHw4wTc8LdwsWuNsxVB88i1fmiCBn5vcr2s+d3xrliTEG6RSal+zRFGxVayvgLdp9D1ealDJqDqzjju8CnF46LINryPsNlRmY/P2y0GBPsvfpXjuDPzOOjGkLse0GE3aHTeXiWD620kYB+ue+z07DF4blL0+iCcXrmer1gmMcOZl3RCZiebadBE3CVS4v7XuY+iTX3Ig4z3YIEXF39oeNXSdDDEzigtjzHzCUOBYcs8afRtOm3/w6IMQDnEK+rLG/5HlRWsr4z9Ob0ex2MROsBYvX/x6fcopmYQXuk91t7gwxch8aRJIOmoBjaWnlJdNCFhHLMk0qsYCbRkaQ5ImjFFKMSeWfEhp+PgUBLchSOFrRWRpeJubYSUJMZofR0Let/pzuIYWgrVf5PZEJrxQbVWrj7W5b5nsz3v4c7T6yUGpygFcEOIZ9ZrC2zDVx2qyzCIhkoNUdespOLu+DuTGvDIm6fWWPlmVzGQZWwcFdvjDJU6jfDu1KitiO0qWXC2TS6lyNfeE1erM5UZZQOn5RM1oVtkEuMSvU+kDtTpgGnqoENkSAemLlhFuQ0nk89sXrGom X-Forefront-Antispam-Report: CIP:216.228.118.232; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc7edge1.nvidia.com; CAT:NONE; SFS:(13230040)(376014)(7416014)(82310400026)(36860700013)(1800799024); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Aug 2024 17:02:06.0134 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 03b50a8f-8dda-4362-7eae-08dcc6b9faf7 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.118.232]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: SN1PEPF000397AE.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4381 Add an arm_smmu_viommu_cache_invalidate() function for user space to issue cache invalidation commands via viommu. The viommu invalidation takes the same native format of a 128-bit command, as the hwpt invalidation. Thus, reuse the same driver data structure, but make it wider to accept CMDQ_OP_ATC_INV and CMDQ_OP_CFGI_CD{_ALL}. Scan the commands against the supported ist and fix the VMIDs and SIDs. Signed-off-by: Nicolin Chen --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 76 ++++++++++++++++++++- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 1 + include/uapi/linux/iommufd.h | 7 +- 3 files changed, 80 insertions(+), 4 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 a2af693bc7b2..bddbb98da414 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -3267,15 +3267,32 @@ static void arm_smmu_domain_nested_free(struct iommu_domain *domain) kfree(container_of(domain, struct arm_smmu_nested_domain, domain)); } +static int arm_smmu_convert_viommu_vdev_id(struct iommufd_viommu *viommu, + u32 vdev_id, u32 *sid) +{ + struct arm_smmu_master *master; + struct device *dev; + + dev = iommufd_viommu_find_device(viommu, vdev_id); + if (!dev) + return -EIO; + master = dev_iommu_priv_get(dev); + + if (sid) + *sid = master->streams[0].id; + return 0; +} + /* * Convert, in place, the raw invalidation command into an internal format that * can be passed to arm_smmu_cmdq_issue_cmdlist(). Internally commands are * stored in CPU endian. * - * Enforce the VMID on the command. + * Enforce the VMID or the SID on the command. */ static int arm_smmu_convert_user_cmd(struct arm_smmu_domain *s2_parent, + struct iommufd_viommu *viommu, struct iommu_hwpt_arm_smmuv3_invalidate *cmd) { u16 vmid = s2_parent->s2_cfg.vmid; @@ -3297,13 +3314,46 @@ arm_smmu_convert_user_cmd(struct arm_smmu_domain *s2_parent, cmd->cmd[0] &= ~CMDQ_TLBI_0_VMID; cmd->cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_VMID, vmid); break; + case CMDQ_OP_ATC_INV: + case CMDQ_OP_CFGI_CD: + case CMDQ_OP_CFGI_CD_ALL: + if (viommu) { + u32 sid, vsid = FIELD_GET(CMDQ_CFGI_0_SID, cmd->cmd[0]); + + if (arm_smmu_convert_viommu_vdev_id(viommu, vsid, &sid)) + return -EIO; + cmd->cmd[0] &= ~CMDQ_CFGI_0_SID; + cmd->cmd[0] |= FIELD_PREP(CMDQ_CFGI_0_SID, sid); + break; + } + fallthrough; default: return -EIO; } return 0; } +static inline bool +arm_smmu_must_lock_vdev_id(struct iommu_hwpt_arm_smmuv3_invalidate *cmds, + unsigned int num_cmds) +{ + int i; + + for (i = 0; i < num_cmds; i++) { + switch (cmds[i].cmd[0] & CMDQ_0_OP) { + case CMDQ_OP_ATC_INV: + case CMDQ_OP_CFGI_CD: + case CMDQ_OP_CFGI_CD_ALL: + return true; + default: + continue; + } + } + return false; +} + static int __arm_smmu_cache_invalidate_user(struct arm_smmu_domain *s2_parent, + struct iommufd_viommu *viommu, struct iommu_user_data_array *array) { struct arm_smmu_device *smmu = s2_parent->smmu; @@ -3313,6 +3363,7 @@ static int __arm_smmu_cache_invalidate_user(struct arm_smmu_domain *s2_parent, struct iommu_hwpt_arm_smmuv3_invalidate *end; struct arm_smmu_cmdq_ent ent; struct arm_smmu_cmdq *cmdq; + bool must_lock = false; int ret; /* A zero-length array is allowed to validate the array type */ @@ -3335,12 +3386,17 @@ static int __arm_smmu_cache_invalidate_user(struct arm_smmu_domain *s2_parent, if (ret) goto out; + if (viommu) + must_lock = arm_smmu_must_lock_vdev_id(cmds, array->entry_num); + if (must_lock) + iommufd_viommu_lock_vdev_id(viommu); + ent.opcode = cmds->cmd[0] & CMDQ_0_OP; cmdq = arm_smmu_get_cmdq(smmu, &ent); last_batch = cmds; while (cur != end) { - ret = arm_smmu_convert_user_cmd(s2_parent, cur); + ret = arm_smmu_convert_user_cmd(s2_parent, viommu, cur); if (ret) goto out; @@ -3358,6 +3414,8 @@ static int __arm_smmu_cache_invalidate_user(struct arm_smmu_domain *s2_parent, last_batch = cur; } out: + if (must_lock) + iommufd_viommu_unlock_vdev_id(viommu); array->entry_num = cur - cmds; kfree(cmds); return ret; @@ -3370,7 +3428,7 @@ static int arm_smmu_cache_invalidate_user(struct iommu_domain *domain, container_of(domain, struct arm_smmu_nested_domain, domain); return __arm_smmu_cache_invalidate_user( - nested_domain->s2_parent, array); + nested_domain->s2_parent, NULL, array); } static const struct iommu_domain_ops arm_smmu_nested_ops = { @@ -3863,6 +3921,15 @@ static int arm_smmu_def_domain_type(struct device *dev) return 0; } +static int arm_smmu_viommu_cache_invalidate(struct iommufd_viommu *viommu, + struct iommu_user_data_array *array) +{ + struct iommu_domain *domain = iommufd_viommu_to_parent_domain(viommu); + + return __arm_smmu_cache_invalidate_user( + to_smmu_domain(domain), viommu, array); +} + static struct iommu_ops arm_smmu_ops = { .identity_domain = &arm_smmu_identity_domain, .blocked_domain = &arm_smmu_blocked_domain, @@ -3893,6 +3960,9 @@ static struct iommu_ops arm_smmu_ops = { .iotlb_sync = arm_smmu_iotlb_sync, .iova_to_phys = arm_smmu_iova_to_phys, .free = arm_smmu_domain_free_paging, + .default_viommu_ops = &(const struct iommufd_viommu_ops) { + .cache_invalidate = arm_smmu_viommu_cache_invalidate, + } } }; 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 6c8ae70c90fe..e7f6e9194a9e 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -10,6 +10,7 @@ #include #include +#include #include #include #include diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index f3aefb11f681..0d973486b604 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -734,13 +734,18 @@ struct iommu_hwpt_vtd_s1_invalidate { * @cmd: 128-bit cache invalidation command that runs in SMMU CMDQ. * Must be little-endian. * - * Supported command list: + * Supported command list when passing in a HWPT via @hwpt_id: * CMDQ_OP_TLBI_NSNH_ALL * CMDQ_OP_TLBI_NH_VA * CMDQ_OP_TLBI_NH_VAA * CMDQ_OP_TLBI_NH_ALL * CMDQ_OP_TLBI_NH_ASID * + * Additional to the list above, when passing in a VIOMMU via @hwpt_id: + * CMDQ_OP_ATC_INV + * CMDQ_OP_CFGI_CD + * CMDQ_OP_CFGI_CD_ALL + * * -EIO will be returned if the command is not supported. */ struct iommu_hwpt_arm_smmuv3_invalidate { From patchwork Tue Aug 27 16:59:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 822905 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2052.outbound.protection.outlook.com [40.107.95.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1D4451D0DC4; Tue, 27 Aug 2024 17:02:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.95.52 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778137; cv=fail; b=tcLcJbpDiIskHC+0TIS8hUXKG6jF4MBe1nmskr0wXXG60XTkGv3ieog02ZXBrJclgaG1eICO89K0kLo8uvJ1oroBrSVbhMFGxulQiimuR/Sl5YhFF3mCfa085Wt1iU4DK17OL6eycDcWFcQ9NWDsuIzlae06j4sQwBvFLEQ3T30= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778137; c=relaxed/simple; bh=CED3G04sfM5o6y8e9joEGQhf/0Q3YNCZN8Van4ALw14=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=nLhAflEixi+ZBPgSCPrloRzsmUbMlsjnOTCrDnMyl3LoJj5Gnic0V8Qo49KJHp8WFn01jVEcdpmVv1ISNdobiyXc6pCyf7wM7Zhnil4HQrxu8hRlqNVAEuGSphB7we9u7gthF97Q/LOiA2k02dEC8/ZedA+oGgNsOr2bjrhOl8w= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=bQVswOf+; arc=fail smtp.client-ip=40.107.95.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="bQVswOf+" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=itkn3m+gXFid7g0LyLhrkU8rj32LYCfeSi2z13RnjCPUzKzBREWgNYzYQy2MkIzx0n4GIhTRS+Ujv92eWoDc5i97WSE3phNWL5y2oSuSHU66sywRRp2hNuMGT9rXj9H4MQLIDmriHxMoTeDJyfZwMDdMjxNFiFHH7H2mRGMFgrOeiRrMdc5v8pAxuCdanjR0MKSdBpehQ2PjO20OaCSlIQLzGHDyt1woU8vP4Tg+yp7TV7uLDp1G4P6+Uernw1/GhRI7pPJwybn9gLnUbMJDLHMLzV9XUClhu6zi2A9mhFgy0SQJeS2Q2fbolFeKPrIvexQzLybRYCoB/rdGxNSKYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=4LkXVhx7CLy4jkOsRxBVgGp6G0jS0Miqms8A/q0GUFY=; b=wF0Oeck0JLfvvVbNyEhXxxZmahGFWI1FCgUoEAlkgt0eokz7VDSiTsE/m3e3oijZrYZHcGMp5YZ/cUEzLOhZuagU98vI4ob+XfxA4RLCeU2n1SHwrAOvFQTscFPZ5mlvyqPPhIUpP7JU3qFQ2l1nY6vCZn/+8dQ4CYHse4ZTqOeyUuHVdC67MLCjbccG2485ZKW7WUdnhjPtoETD2bdyRSAZWGi29c7s8FdAkEl7BBWi1H3KvWV6PY+S4ToM3MSU4UG2OIGmTHXVOsPzGxLzXHl8+h6PIpHwH9hD5XDVdRL3xH2ub2OWxewyUqy9FsD0ftL6NlIQUN0fH7VEeA/2Sw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.233) smtp.rcpttodomain=huawei.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=4LkXVhx7CLy4jkOsRxBVgGp6G0jS0Miqms8A/q0GUFY=; b=bQVswOf+fWc7Wog0SCeYzSS23tuoL1g3fH0S+zQjm5fitE6klx5ebw1Ds9HLCGf83Nfzt4XZ+3zSlR+hsVKzMRcxjqDviVyXgLHZM6IVfddY2ezE8NBZzxUKTKMZ1FKR0fcXiGhTNpkxNLBCPQoenG2qukUCV8ND02aEYfYWW0GvNIjlRpRXlKP6ec2VCBcIi211GaIA6a3+/SGn51Qt89xm5yEnDsA3Bv2+L0tL1ioFhvpVYL89pAaJ/6fzJ2E0OyPQPFuuoXeM9HT+uT/UAopnUDygyJbhtvZPx/i1jn/3wvZbfU4/NtNBe1ZD8pi7Uc6GeUZLSyMBWelJ5NqoQA== Received: from CYZPR02CA0011.namprd02.prod.outlook.com (2603:10b6:930:a1::9) by PH8PR12MB7157.namprd12.prod.outlook.com (2603:10b6:510:22b::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.26; Tue, 27 Aug 2024 17:02:06 +0000 Received: from CY4PEPF0000E9D2.namprd03.prod.outlook.com (2603:10b6:930:a1:cafe::41) by CYZPR02CA0011.outlook.office365.com (2603:10b6:930:a1::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.26 via Frontend Transport; Tue, 27 Aug 2024 17:02:06 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.233) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.233 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.233; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.233) by CY4PEPF0000E9D2.mail.protection.outlook.com (10.167.241.137) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.11 via Frontend Transport; Tue, 27 Aug 2024 17:02:06 +0000 Received: from drhqmail201.nvidia.com (10.126.190.180) by mail.nvidia.com (10.127.129.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:54 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail201.nvidia.com (10.126.190.180) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:53 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Tue, 27 Aug 2024 10:01:52 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , Subject: [PATCH v2 18/19] iommu/arm-smmu-v3: Allow ATS for IOMMU_DOMAIN_NESTED Date: Tue, 27 Aug 2024 09:59:55 -0700 Message-ID: <3962bef2ca6ab9bd06a52910f114345ecfe48ba6.1724776335.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D2:EE_|PH8PR12MB7157:EE_ X-MS-Office365-Filtering-Correlation-Id: 4568eba1-e99d-4799-17b0-08dcc6b9fb20 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|82310400026|1800799024|36860700013|376014|7416014; X-Microsoft-Antispam-Message-Info: /QVep0xOH2U9v6nvWntIZH/Dt1uDOPFBHeysdcipOyLTrWc1jIIpQJ6WZXi9n+NBE4bbYxMUIiQsZwZjcZvy8t7HQwcjEhoP9gqK2Od2QzkK0RmZ1evu2TpsBqxgw/Z45JHCf3M4OZfEUfGNF81WccNh36tedB6HGs6MXiLlSof+kHLO7HNGLV0Qdlztp85hcafwfXZBNwl7K4EPNCpPZd5vgkLw6uc4r+FfRgjqAurhxFy1dmrpcDLLlL1qHoNVOiJmTHpZT3bfWPOL4JAolUTrScN/ocyqueBDkYuvWnpxCbaP+xDB2klXYby6/1HAlYYbGI9VxwkZAc3JWdyR0IEkoSTBk5Gyay2POwDfTbZaNFxnnqZ/bnkZnsIgzR0fMaIPApaweuN/57IDjU69iMzyQ+NImPOQn0somtCTWGy+bmXSHaGTqISiWywuxAo97cuOwcg01fPIZMhXfq2Kb8tWPaThIqcd3B7B8jEJdMHd0eCyo7oFDY8h5YMkVoYRB2d6sC3wqpJeBlJgF0L9UwRehMqWtUVBcObiDhHaHwZNGS1lyy7rhTRb+G6kpsCutwFDmAYP6ioitZTrZ/xPDQNE8FBVTBgHCh0WRmL/GrN97zky7f6ZOfbGKc5br32JSme37KCkwZ5LXF27/xJC4orHY8f1yVijrz6X9MLc/EuM+OlXTiikOHC5suBieWG3xPorzVTRX6lTj7qxGvlt6WU9E+L982bkS3JxdlrEJ9Ss1aAup0nO4Kmm8D/DGXLxUoWnCQqGiU4wF/2LkCgPU2MmZadbYqwfkw21fNpB6xwQepgZCFJcrqoHopF9oL1eY7z36kklhwtEXrxO0a838DG7kB6m/4MTqw9rYI7G2rD/ZlVPq91eqe+0zvpvJNCmNIGd4EsBNve1x4w+YB32MKt/ZendHdmVosZ31mC+OMjSZRnsdAItl8Y4oemy83HdWIRFV6Yl7eF+FRARLhnelxWNuBGijZ1Kbrw7rptc7if8kWb23FgBRgvMr3jKujlgK2u4oqI1OtLy1TX1H3IJ4Jjg66uMOoIZ0k+2YhFe8xy1V1bU42Mq4Ib+hBRimA0F4+W1grkxc7I2I9prySpbM5FujbtARLZloBRkVWRfQky+6ypbAUIJtP3fJx2HMjstQNEQFJaLDIU0Uxj/68lazJH3QJu9xZykjgbfZcgAua4Kt51WWcLTUqs7xD61yelsUqDrz0LqPNXwiOAv165wByMObQat2Yd8HVmLLzc+97twKCk0JgmWxgDomD60/WIprSxv/eS+2qpMOwpJIEnGtAzkPiIvpPbirzQJaucFohk+XC37spJgmPY+uWQRs/5YK5+4ZFAMDJV5dbNQgiTsAO5LSGbRAFzPf+abK3zoN2RYF9boP/74q3tqWxPZJoRfEpTmSp7zM7ZEpqSohCBFKO7BVyudWcvLiJhy+D4lBHJCODtJMZNEPYZECKjKb1HZ X-Forefront-Antispam-Report: CIP:216.228.118.233; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc7edge2.nvidia.com; CAT:NONE; SFS:(13230040)(82310400026)(1800799024)(36860700013)(376014)(7416014); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Aug 2024 17:02:06.3264 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4568eba1-e99d-4799-17b0-08dcc6b9fb20 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.118.233]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000E9D2.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB7157 From: Jason Gunthorpe Now, ATC invalidation can be done with the VIOMMU invalidation op. A guest owned IOMMU_DOMAIN_NESTED can do an ATS too. Allow it to pass in the EATS field via the vSTE words. Signed-off-by: Jason Gunthorpe Signed-off-by: Nicolin Chen --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 15 ++++++++++++--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 1 + 2 files changed, 13 insertions(+), 3 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 bddbb98da414..6627ab87a697 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -3237,8 +3237,6 @@ static int arm_smmu_attach_dev_nested(struct iommu_domain *domain, .master = master, .old_domain = iommu_get_domain_for_dev(dev), .ssid = IOMMU_NO_PASID, - /* Currently invalidation of ATC is not supported */ - .disable_ats = true, }; struct arm_smmu_ste ste; int ret; @@ -3248,6 +3246,15 @@ static int arm_smmu_attach_dev_nested(struct iommu_domain *domain, return -EINVAL; mutex_lock(&arm_smmu_asid_lock); + /* + * The VM has to control the actual ATS state at the PCI device because + * we forward the invalidations directly from the VM. If the VM doesn't + * think ATS is on it will not generate ATC flushes and the ATC will + * become incoherent. Since we can't access the actual virtual PCI ATS + * config bit here base this off the EATS value in the STE. If the EATS + * is set then the VM must generate ATC flushes. + */ + state.disable_ats = !nested_domain->enable_ats; ret = arm_smmu_attach_prepare(&state, domain); if (ret) { mutex_unlock(&arm_smmu_asid_lock); @@ -3497,8 +3504,9 @@ arm_smmu_domain_alloc_nesting(struct device *dev, u32 flags, cfg != STRTAB_STE_0_CFG_S1_TRANS) return ERR_PTR(-EIO); + /* Only Full ATS or ATS UR is supported */ eats = FIELD_GET(STRTAB_STE_1_EATS, le64_to_cpu(arg.ste[1])); - if (eats != STRTAB_STE_1_EATS_ABT) + if (eats != STRTAB_STE_1_EATS_ABT && eats != STRTAB_STE_1_EATS_TRANS) return ERR_PTR(-EIO); if (cfg != STRTAB_STE_0_CFG_S1_TRANS) @@ -3511,6 +3519,7 @@ arm_smmu_domain_alloc_nesting(struct device *dev, u32 flags, nested_domain->domain.type = IOMMU_DOMAIN_NESTED; nested_domain->domain.ops = &arm_smmu_nested_ops; nested_domain->s2_parent = smmu_parent; + nested_domain->enable_ats = eats == STRTAB_STE_1_EATS_TRANS; nested_domain->ste[0] = arg.ste[0]; nested_domain->ste[1] = arg.ste[1] & ~cpu_to_le64(STRTAB_STE_1_EATS); 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 e7f6e9194a9e..6930810b85cb 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -832,6 +832,7 @@ struct arm_smmu_domain { struct arm_smmu_nested_domain { struct iommu_domain domain; struct arm_smmu_domain *s2_parent; + u8 enable_ats : 1; __le64 ste[2]; }; From patchwork Tue Aug 27 16:59:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 823336 Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam02on2044.outbound.protection.outlook.com [40.107.212.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8D0451D47B1; Tue, 27 Aug 2024 17:02:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.212.44 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778137; cv=fail; b=AtKX4rdceQ2VKuzYZssQdUfRjUF028QkCviA7qP+MyE+u/xHk+iyo7SN6xLObvnN2F2DybFPBmC7cS/Z7eSWAVJ549GbH6hydCzdHia6qgp/zyRX7oZJrcuylq8UiZoT/Vj7IXUEVGt4mJSYSVxY1MTf1qLe2+IczJ0m5ZpiImw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724778137; c=relaxed/simple; bh=mNbpCl2ZWrPM9bpio3SEMXNhv5zl8VwUOFQ3I2YNNXk=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=gNlYo+/DDKEAVU9cTE3uHQACVoiStMihtStp25ZUHxHIoW35uVgHKqt3U5iBPr3xpiZSkLaDHhByRk9aND/qjT2Pf7rq5T6NBr2igWKnbfkKpD2dJ13zBY767Dtm0+ToJNKY1bzLSYOzMtcnc5fFbT75bkd5uK1lUYJtBrlGqY0= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=oN7Z8jwu; arc=fail smtp.client-ip=40.107.212.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="oN7Z8jwu" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ZV4clupA/2eTpYVhdmQB62czdQ1Qpgqvlb+YbdQaGUHOCksIHwC75sEtwMckSqSRrXjehjRXhrvRwTb2fIoX4eIi2RK1x60yLhwb7ozTFAOrrGbBoy4MXBTqBSswFZLN0qtiOXgWS5m9P8IWb7VJf4xYm0lg2GEGjftqk6kBib+Ed57kRMOaHgUjfJoaJ5qcCEpUaV7RQDiD6yMKZKLZiOVYZxXpI6p4rsJHxyAgdg6YS85zqhUUXtddLELwKLUHcCucBOCEonpFDksBcXXlVISWxxR/bH7BedpkRFWRJL0QHddgUUeW+svMynQiFkRhwuZF+dizx8Bi8R8ePwNPtA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=6/VHoxUiLpU/MD5Q98bigKlmbsdt5Sg5CnOVwz95Lgs=; b=B95IIDby5L9rgMffQbVEMmvTONstm8DSOcG7G6i8+R9068z7uAFbcHYDBDfbAQQ92JdJtXD8+6Z7CHoOvNbNZOA8U0nsMgKq6d2Y8Avzem4gWeFt2oY2YySXB25oGKu0PDVXrV4tdNhPQT7dtn/bNDMQRQjK1Ez6DKEgdOH+in6ohS6MczFJzcpDmwZicu1Vpl/vCAQdlL3RBS36Jet2+nqjNHgx8x1Ny4zHOMJpVTxaoV2h2zLEoUXfvbYDMhR7aZ/WEYMsVrsGBwhdxRu7f3TlaK1Bbsna0wHCf3V77CUDiTOscHBQK5FvM1ukk39snC51yGISvMMcHWZ2SxEq9Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.232) smtp.rcpttodomain=huawei.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=6/VHoxUiLpU/MD5Q98bigKlmbsdt5Sg5CnOVwz95Lgs=; b=oN7Z8jwuGr3vUCDXZiq/4S87JAaEbtvgujQBnodtGvOTxhl/BPCqvBMlP/iwGcz5kv3oG3VbDeHAxOiD3Vz6xtbGxszlLIPPOBmCc0M9k1anhnhDsml9CJJ+9zeecXBVlaNjOGYWNt9E3ViUJG5N9aOmnP/ZbmnNGi+eabkBGXgSNEfmG7Qo9GTyoQhdNiqp0HwhTV7MZaZhzYhlAIRONCg3R8ebxSIuN7mCSVa8dpFKCx0bgpofnK8omhDW6fppA4TV8/MKJi+80Ahe9pq+sKvTz0qnegs8teuVFks6B6PqSZJtX7/8inEI7vLAa6/zCaEcTjAMfqK909/jCypYPg== Received: from SN6PR2101CA0001.namprd21.prod.outlook.com (2603:10b6:805:106::11) by IA1PR12MB7592.namprd12.prod.outlook.com (2603:10b6:208:428::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.25; Tue, 27 Aug 2024 17:02:10 +0000 Received: from SN1PEPF000397B4.namprd05.prod.outlook.com (2603:10b6:805:106:cafe::94) by SN6PR2101CA0001.outlook.office365.com (2603:10b6:805:106::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.16 via Frontend Transport; Tue, 27 Aug 2024 17:02:10 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.232) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.232 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.232; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.232) by SN1PEPF000397B4.mail.protection.outlook.com (10.167.248.58) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.13 via Frontend Transport; Tue, 27 Aug 2024 17:02:10 +0000 Received: from drhqmail202.nvidia.com (10.126.190.181) by mail.nvidia.com (10.127.129.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:55 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail202.nvidia.com (10.126.190.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 27 Aug 2024 10:01:54 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Tue, 27 Aug 2024 10:01:54 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , Subject: [PATCH v2 19/19] iommu/arm-smmu-v3: Update comments about ATS and bypass Date: Tue, 27 Aug 2024 09:59:56 -0700 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SN1PEPF000397B4:EE_|IA1PR12MB7592:EE_ X-MS-Office365-Filtering-Correlation-Id: 19ee9509-a54b-435b-46fa-08dcc6b9fd5c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|36860700013|1800799024|7416014|376014|82310400026; X-Microsoft-Antispam-Message-Info: p6ET5sb+Wgx1w+PSqZmC+liRH/fB0vFOSYzfqhfk4Wo4y5XI0wIpsX3+Tfeo/G5hywQxCPwNs7mhKDnz/r1NpFGqABxV0mgZsY/7SVukWeHCZgIzsZbCCZhaS6tl9g2joZaxfhQGZ0LxahZH93PLG44q1x17mmSrCitLjei4Y7/cGP7TF0JHdmpcjH88kE+2F40T4vcUMmuiwZLSdOaJPZdj51dRb71uQd/vd5M8s32r6vXPIV2cEj1bMhpDGFW2e8BMfRrqQn59QVpShkzxsLyWdCjEO36jhcGy05StEWGRGBszE6/Lp8TT14s5A42w/kQIVYjdSViLFK86QV6O1eJeiI9Oj7iI0aI+Xd9c8W9ZMGECigQhnZb2581CpHU6M6Oy2CTFGl4UAPUy5qjWSshrE2wgFVwBoQLQskqcOTNKp/2/d9EBuXDY2i/QXBNde4MU/xxoDNXAPlWvh/Z/TAnfbopaU8cBbOiDIOPdDPL8qggpkzx/WlQh4GamWD2Mq8XEMlJglb4mkBWOWiMihYOGn7x/c5budxZk+6/wlzzDxLaXte7P/YPE4AXVKuZMQyent1DD1aNJlvZ3VWekMa0vcDw8nQKPTnUqtijvA8isTFU6zdd7TIR7Z+Wtmp+PhUC1OlaDQAnTy2vBvXzMgAcgo8O8AhQKXvmhvI9n1+UxpfgXABgTNDk9hMe8sBg1Gs5ilweqN4DHUhCA52OlhhfudrDRAoXVYA0SWrahpzTQ6tVsplKv3C964KZYIGH9CD4VHeUXjltlKlQIGZ9YudV18oavTa7ysP9iDfEltZi5wuwmTJCBERid1Q5M1W+sTs1nMRxzM4a5hFFjNIXUtu1zy0WC1neCY9t606LDcclD1bUlzWW41/WDlAhVDbsCsGuRtAKj2urM9NWD2mxctMmwLWREPBwum04t05JrGxnSIjDEaazO99dOQqaqDwTRd7VbUcz1A5IpT5jlDY1EEDnz/l247XCPVUicoOlQbbkpoTnkATzyMffJJ8w4HIoXo5tk8T29apId6Y9u6tnuIw8lNbdcwgk0Iy/vSCeaRD0Z1KeFe3CnyHh7XaxczvAxw1CSXATl9d0VOY6nLyK9CALhM1Ky+49HiYvz3RMLYtwFOnk2ts6snIJGSEcAMnRsxlho6uRdk8glLyVWVjuMkw8HDItSYCQIDWBh0mtFXRadVYibL9E/XyAZDdTCXxkwFQt2+x6GQKBgZghIIYXeadtRqo8d0QRFB6O6AV8DdqyzYkB0a2It6P19yBqT0Fp2lAUnewDFvDodIHEvO68NYpKviYzhseTv+BHQV15ZxbMwPk9Sqyom249edNgPX6lXSHYW5NBuggREBHuffFGZ+NTj2M+fZwvrSGyJHhrMMO6U/4cp10goo0fbgVibiHY7EumiGjq8Hdczxjpi1uNT5Qg/S79QG8d4kjLq+Mqzemn1lQUcRdCnCLQaeTz8TI7k X-Forefront-Antispam-Report: CIP:216.228.118.232; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc7edge1.nvidia.com; CAT:NONE; SFS:(13230040)(36860700013)(1800799024)(7416014)(376014)(82310400026); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Aug 2024 17:02:10.0294 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 19ee9509-a54b-435b-46fa-08dcc6b9fd5c X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.118.232]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: SN1PEPF000397B4.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB7592 From: Jason Gunthorpe The SMMUv3 spec has a note that BYPASS and ATS don't work together under the STE EATS field definition. However there is another section "13.6.4 Full ATS skipping stage 1" that explains under certain conditions BYPASS and ATS do work together if the STE is using S1DSS to select BYPASS and the CD table has the possibility for a substream. When these comments were written the understanding was that all forms of BYPASS just didn't work and this was to be a future problem to solve. It turns out that ATS and IDENTITY will always work just fine: - If STE.Config = BYPASS then the PCI ATS is disabled - If a PASID domain is attached then S1DSS = BYPASS and ATS will be enabled. This meets the requirements of 13.6.4 to automatically generate 1:1 ATS replies on the RID. Update the comments to reflect this. Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 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 6627ab87a697..ad43351145d0 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2830,9 +2830,14 @@ static int arm_smmu_attach_prepare(struct arm_smmu_attach_state *state, * Translation Requests and Translated transactions are denied * as though ATS is disabled for the stream (STE.EATS == 0b00), * causing F_BAD_ATS_TREQ and F_TRANSL_FORBIDDEN events - * (IHI0070Ea 5.2 Stream Table Entry). Thus ATS can only be - * enabled if we have arm_smmu_domain, those always have page - * tables. + * (IHI0070Ea 5.2 Stream Table Entry). + * + * However, if we have installed a CD table and are using S1DSS + * then ATS will work in S1DSS bypass. See "13.6.4 Full ATS + * skipping stage 1". + * + * Disable ATS if we are going to create a normal 0b100 bypass + * STE. */ state->ats_enabled = !state->disable_ats && arm_smmu_ats_supported(master); @@ -3157,8 +3162,10 @@ static void arm_smmu_attach_dev_ste(struct iommu_domain *domain, if (arm_smmu_ssids_in_use(&master->cd_table)) { /* * If a CD table has to be present then we need to run with ATS - * on even though the RID will fail ATS queries with UR. This is - * because we have no idea what the PASID's need. + * on because we have to assume a PASID is using ATS. For + * IDENTITY this will setup things so that S1DSS=bypass which + * follows the explanation in "13.6.4 Full ATS skipping stage 1" + * and allows for ATS on the RID to work. */ state.cd_needs_ats = true; arm_smmu_attach_prepare(&state, domain);