From patchwork Tue Oct 22 00:20:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 13844936 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D19A5D1714F for ; Tue, 22 Oct 2024 00:48:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type: Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date :Subject:CC:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=KdrhTwHRY60JX6v8g95ZafUwTwY/UoASg7RZ4ctBi/Y=; b=G+GRjJ8eypswf9QRsSwtH1kF9U iQhkJAZZh+ZinDMHyftWT6KPoWPjnIlZbj5Q2GS9HDKca35uEeQ8YHcPMxBtCKB6cII1baV6iXwvY ev9/8CIWpCsEAaA2wvQWrVYqGApsqVcjfXtRJs1/B1X9tdipl0rwPfGdXBhSeK/MoBtb9I+QLyEhy BjVD1bKUWo9fSG9LnWECJ7SwIZImGA9dHVSTeEzfKZi3tTPga+JIkFryIb5ix5x5GLPKJuzJsJhli mFCDJv1u6biHfwMY1RaKxGczhawS94t9NHGnE5gmxGVe5VSAK/fVPS/EdKKXCJks+MCZ4n5MWSs6a SpjcFnpw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t3342-0000000978n-41G5; Tue, 22 Oct 2024 00:47:58 +0000 Received: from mail-co1nam11on2061a.outbound.protection.outlook.com ([2a01:111:f403:2416::61a] helo=NAM11-CO1-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t32dt-000000092Sm-0Yei for linux-arm-kernel@lists.infradead.org; Tue, 22 Oct 2024 00:21:00 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=F7ML3mDtshFbRKUt78VEXk5ovJHOz5nHEBySUOWru+fFh6/NM8gYWHTPs66cckfsTqnxLqnrCQOIEoBRr2yBkNTNL9UYokUBxqLr5QC9FKlu6lb09EFCIz+4VOUvRVGJJYxon/kV6Gb/xdY0gAAlP9LmKjiNwVwn3ei2azIQRNrobXgXEgzTz1y+flwcw/VWarbuNMdOefQXBpdTgTfHdc+y5TcOZ4Rb66Hqct8e1CL+vsi0a7OSYZw3SvRsRAzR4Sf2zp7yIYxVTD2nbEIfbbkQdTz3IIs5G7diXJ420KCC3vofsvFFFgp9Iy23Ma0nPeelB8qWmPDeQRDRreRoKw== 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=KdrhTwHRY60JX6v8g95ZafUwTwY/UoASg7RZ4ctBi/Y=; b=jkzmQUaWWUnlrhCzNc+FDhuCueBP20E0XqmreXQISUUDL9WhyZSt9kl6/RZWCCJ4gnU+xL63qrT5oviMiL75oT8rSEukN+AF0vC93otVHawx9C+5aqRjfXtGMk4+HCITizBFiPnu9PxXyMgFZ9Ql6x39zOsPbinTjDkcTE3f6VNJEbco1Sq5wH63l0QzsS63X04/Siz8qsw5xmWbwymOgx1BW68Mdszw7/Y+P3ALkp86bc5lk6nubofqU2Gr4+hTWBYy/eDtzcvJK44AcOnhjj6c1QhxyvBt9eIVJN0LZniLoyTRGM0whgrSew1oMjqJZoMNsoaY/K7FPN+5Ha8GkA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=amd.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=KdrhTwHRY60JX6v8g95ZafUwTwY/UoASg7RZ4ctBi/Y=; b=QvmocKHxMtvZVqovg3Wu+syNpE4Euvt1/yqthJDcpYt+L7Fgm3HNTrqdtKM8+u39sIQ2D/k5fyPd6l1gpjnJYX1aJxHYa8Q1SFWbdjvowzg2USFADXrji9QxugQM9rPv21NqoqNx6L25e11tiUUS/xLtdtv1RevzIwKoxgfpZBNIcpmMfdRUpcb5yZXqEYfslr5VXunQGxOxLrjzefdxd8CxBlGKCzcVNrwpFdA62xt+fTL3WGIR7wGM8Fb/2yC2nyEEybqS+yfatrv+LiNG05n1/cTJVAdQQoYL2yzXiPaDuOOu6pVpn8kIbJFgeEQU+3/sjCmKwCVPsuNNm/c3ZA== Received: from CH2PR18CA0040.namprd18.prod.outlook.com (2603:10b6:610:55::20) by MN2PR12MB4470.namprd12.prod.outlook.com (2603:10b6:208:260::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.29; Tue, 22 Oct 2024 00:20:51 +0000 Received: from CH2PEPF0000014A.namprd02.prod.outlook.com (2603:10b6:610:55:cafe::98) by CH2PR18CA0040.outlook.office365.com (2603:10b6:610:55::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.29 via Frontend Transport; Tue, 22 Oct 2024 00:20:51 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) 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.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by CH2PEPF0000014A.mail.protection.outlook.com (10.167.244.107) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8093.14 via Frontend Transport; Tue, 22 Oct 2024 00:20:51 +0000 Received: from rnnvmail201.nvidia.com (10.129.68.8) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Mon, 21 Oct 2024 17:20:36 -0700 Received: from rnnvmail201.nvidia.com (10.129.68.8) by rnnvmail201.nvidia.com (10.129.68.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Mon, 21 Oct 2024 17:20:36 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.8) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Mon, 21 Oct 2024 17:20:35 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , , , , Subject: [PATCH v4 06/14] iommufd: Allow hwpt_id to carry viommu_id for IOMMU_HWPT_INVALIDATE Date: Mon, 21 Oct 2024 17:20:15 -0700 Message-ID: <8ee26e296f77ee8f1b611ef2b9295713bfe7b542.1729555967.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PEPF0000014A:EE_|MN2PR12MB4470:EE_ X-MS-Office365-Filtering-Correlation-Id: bde0f919-4daf-4569-7523-08dcf22f62b5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|376014|7416014|1800799024|82310400026; X-Microsoft-Antispam-Message-Info: egArw/8eWYXuwuADcCPVEaZZ457s2UIP7Vxn83sYPkybB1I7OhswqKLraHDTPtFvprR5YFrely6wRt0dTa+dlpBB1HD6pjcOf/UOMt9ChF1FNXQX8dn7WT4Jlz1Sv4n9kHZM0uMKviYDLSWq55lUMQllxZhKIUfXGbfFiNNFq77/P8RSxz5SjAhGMj1utIydfTVARka5YAlipBthmXUcXUiKFQPpalc+hgPtId3HgIemclizuNIFT53KamAFHCtU0BqeQL/0Cks7lXruS+eB6w5bfBJsPguV3A79NQQJ7STi3yRUh2NRq/yt3aGipO4jzoZjRTxb4XwOfnR3k8nj7ZcgoBcptVOY5iZ+6m0RcJBc4bYiET/9TxT6ztXylbl3aowgqaEPh8mlFIBJETmxXvNlOfewZ0+igoAq448byCPKzirFpngjjWX1svjBuUFwhJc59qi87BEB3v+m7T21w69GCuqrAwYAhzy2E0w0q4e4gkFvUbrZCuA/y3WNa/JXzkK67yTM24hWGCvL/9FcuDkKOYzUD59X/2gDTmdSZ5dL/yAbfIYq3DC+tFKp2g6ACz0RPH6F65vUZlAyLqrm51qI+f8X4ulS656wGovgfoc7S2ew4bPoZ6Q6UfT3p/CGcYrSX9W+9UChaBT4695TsjixrYbMKASNqRXKRlM4QroaCmj09zBlz8KDtNtWhjutKh4Bse3h0mi4cjkvkfyumMmbEr6bjJsBTjG1xS/zNoZ+xAL5713GvE5+BV6qpNsIHHupRjKVy3VSQ+EjxLXb01cvhWB7VSBpcQNJA14QCh+e1WwxEKdwHTBCg1Q/eqg/7SqpJwlgzBV1rEQyUXzX8soGRYj5NSNz9H9hk3zqaKK6L7Zx1BhXx+9vyyjvpRP8Hf3RAujz/s+Wrwb+UxJkVZMMjN+iHxmqZPlX/HSQCRycTkrnkuUbUWlACiHrPoibCVsf9GtkiDwvODDjcnvC/CA4Uj/o/XOotJJvApnP3LUCdbLPcOhEqTtVBtwK83moEY9e/U7wgIkyuEP4++7CkoYCV6y5bLwG0hUIuRynxyldML0LoGYrXD2p8IY+J/q9kRoTcfjRsDBWEG3f91BD/RFL5gb1XlSNnom5/6KGUCpaEkxBXWD8BOzONoNeD6eZ4PP7NBBBO9kRfklnABQWHLU6XfFhPSg9eGGa9pPCjhs74dVwNN1BAOYEOAUqEKo1qthQvXdbUWnFz4vh1ZDFxr+cQFRQxsNYWfTzk9BLjnrpE8XWtMIjqEohxCu2Sc0/aBG42feAKvOkA5313jf41k6tLoOFAhhskmdY4B0NzZHR3XdaJhoeOCMQ9viUYyGSRrYyn1QP1WinkrrDQtZHmKFYlp7Mybf+tQ2WbmtuiShZ2APHu7Uj/ETMGc47sz5Sso7IwpBIel9KBVN44fzCQQ== X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230040)(36860700013)(376014)(7416014)(1800799024)(82310400026);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2024 00:20:51.1649 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: bde0f919-4daf-4569-7523-08dcf22f62b5 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CH2PEPF0000014A.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB4470 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241021_172057_290502_FDD8D824 X-CRM114-Status: GOOD ( 18.27 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org With a vIOMMU object, use space can flush any IOMMU related cache that can be directed via a vIOMMU object. It is similar to the IOMMU_HWPT_INVALIDATE uAPI, but can cover a wider range than IOTLB, e.g. device/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. Drivers can define different structures for vIOMMU invalidations v.s. HWPT ones. Update the uAPI, kdoc, and selftest case accordingly. Reviewed-by: Jason Gunthorpe Signed-off-by: Nicolin Chen --- include/uapi/linux/iommufd.h | 9 ++++--- drivers/iommu/iommufd/hw_pagetable.c | 32 +++++++++++++++++++------ tools/testing/selftests/iommu/iommufd.c | 4 ++-- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 407747785f6a..3486ae2d62d1 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -730,7 +730,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 a 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 @@ -741,8 +741,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/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 7af6cbe6dbf2..cbc58e98d8b4 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -481,7 +481,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; @@ -495,17 +495,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/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index a8c6bdf64825..e09dba3588ee 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -362,9 +362,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);