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: 13779863 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 CCFD4C54731 for ; Tue, 27 Aug 2024 17:07:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: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=FRo87J3lJqxbRQDxwquN+4RK8PiFhblCS95tLtEOU3M=; b=koSJ2JQyI9e2f/SSnNAMRotKvZ IbTmgBhiZJ+bKlYHSDrrULV6E9EERCOkCA3fSaiToGV9zsDJr7I+FxrBH5ZLwRlcfbANt1NDVZkzo zVZ7KBTsRH2iaU37K/+0yH7Bci4HiI2PXTR5o2xWIFkqcaQI+LDyM1/gAxdbqNRE6Sjd4BPoRYrBr rtQYkK8SCAjD4sJvkHAQmXj70wv/1iZqYEzpOC5f7GBlg+NxnsK9lHdf5m3R/aZGtfgH0illVg8j2 U9gpz9aom1zHVJAAKu6dEXQe/y+TZjxJYIi9FXTDoMydpHlPakRhlejCm6MXaw1kLuXAwGCsaZCOX U19c6nvA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sizfY-0000000CE80-0Tds; Tue, 27 Aug 2024 17:07:48 +0000 Received: from mail-mw2nam12on2061b.outbound.protection.outlook.com ([2a01:111:f403:200a::61b] helo=NAM12-MW2-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sizZt-0000000CBk5-0OEQ for linux-arm-kernel@lists.infradead.org; Tue, 27 Aug 2024 17:01:58 +0000 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: 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240827_100157_163759_5F91EE03 X-CRM114-Status: GOOD ( 17.22 ) 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 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