From patchwork Wed Aug 7 20:10: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: 13756703 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 531C2C3DA7F for ; Wed, 7 Aug 2024 20:13:31 +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=6aQWktePkOW4o78zFKMWXR88Zqd2sc4/zCRsO6x5eI8=; b=DKNbqld5mN6whCizTyHox6vgBd D5cFHE2ROmP7YMCbLW/x6vSfEQ7jrizpyXlOZC8CrD5R012A6Z+9bek+fN+TINT+jRMTpnXwmzOaZ oZiLyOmipue2Cjw6PcJt+TMIKzrLCo92LIx/NGxHI8D/AJDInlAnCcVY0h0ZVOozYEtZGWUcZa1W6 YzcGi5mY0kZxwTqju0Eltra2ZMvkuT6CkBSDZCEylYJ6Hx7Ag1X0qhj1UEUfyYdx5KyTELnw+WoOP XMMF8w1Unbi6UIz+P6e6tdXDHjMxmh+xJAuhe3vRxE6dnXbIDtiCT2FJvtjkCaIJbJi1/jzLrdaAc +D3CcZqA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbn28-000000069rp-3bh6; Wed, 07 Aug 2024 20:13:20 +0000 Received: from mail-bn8nam12on2061e.outbound.protection.outlook.com ([2a01:111:f403:2418::61e] helo=NAM12-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbn0O-00000006985-1O0H for linux-arm-kernel@lists.infradead.org; Wed, 07 Aug 2024 20:11:33 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=yHL+1+KUKUywUv9wX+qCLr50C7E00AYsDC4Psnh+Y9pXZgg9rdaUMHPoxcbITOzyfCnHViXvTfN7QqBIN729w3HBegrVx2zQYzgAk21gm3c14AYZqkV0APtH0CL0d/WaB72koOIzwoW2aStGw+F4uZ2cpRCyRJ5dkbAoXvYp3c2T2rDcDVL/0d1htSkJMU7LJ4do31QmWE44Odo7m6pFfWJ91CavON4OhezWsFLSWZ4hp418BZKuYI2/qUWFFLW77uFmOWhIsdNbqebrWhJx9lrxRtBHfZXLETkI38tY53thsRn/F6Rd8V3+0aGGsbES67jqLD2vvRpK9vUFUd6viQ== 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=6aQWktePkOW4o78zFKMWXR88Zqd2sc4/zCRsO6x5eI8=; b=dfNu8FekFL1DjiZljfM1nmH4Om8/QQS7zEJsWXhyKN4paxXwKNBrWaE7tc/gEckFG3ZXv7+pQuw/7rgJDeb/ukml+LpnvIQeC6iLLupO99a8kD6sDU8LugM6wUrDJ7NkN5VGoZBb7091PCuq+RxN9OOqLxZfV8iTCNS1shPqS761sMw9M6a59iUTMRvgC3QxEiTvi81vYJleJJLLpldzifikpKsNw687Bb4UU5rFS0kSjfzbJbvOUV/Dfm5mDuu77fDHcS3X8xOjyTQpKTnCI8Z6XLVUQKT7Nz2seySvVoVcMmZejUEkheXkdgVULJG5YPRzRduNILs8ldDNIHANeQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=intel.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=6aQWktePkOW4o78zFKMWXR88Zqd2sc4/zCRsO6x5eI8=; b=Ip4L2Ti2RkU4Ixfgp5AluWsZexyxbEpkpai9Xw/mH6zVSer14bEW3RB9IucBtok5PmJ6qg518eVWU4KbxSvCoeO94Vv3lw/2teA6B3k55BE/pqL6K/Wj0PKnKHD6Bs2A6/FnZATJV5qRkNxXg6vnK6qq9OGaeU8W1P1wHkrK9Ja6VMfHK+1mEuoEgHw1qab/liovq0ruhSycbMW0ibf3aXaYHz4JQmY4UEvNwvgoDCw7ZYY/qv+T0T2AFFIXC7pEsmuCe1dNrtTNBEUaAH9aF7drMMvwMKnDy8Tp8INYDEOs+3xcHaeduJU2SA/Lo59WO7spDlw/MyandGYGMISfXA== Received: from BN8PR15CA0054.namprd15.prod.outlook.com (2603:10b6:408:80::31) by MW4PR12MB7016.namprd12.prod.outlook.com (2603:10b6:303:218::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7828.21; Wed, 7 Aug 2024 20:11:26 +0000 Received: from BN2PEPF000055E0.namprd21.prod.outlook.com (2603:10b6:408:80:cafe::cd) by BN8PR15CA0054.outlook.office365.com (2603:10b6:408:80::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7849.14 via Frontend Transport; Wed, 7 Aug 2024 20:11:26 +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 BN2PEPF000055E0.mail.protection.outlook.com (10.167.245.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.2 via Frontend Transport; Wed, 7 Aug 2024 20:11:26 +0000 Received: from rnnvmail205.nvidia.com (10.129.68.10) 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; Wed, 7 Aug 2024 13:11:09 -0700 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail205.nvidia.com (10.129.68.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Wed, 7 Aug 2024 13:11:09 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.12) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Wed, 7 Aug 2024 13:11:08 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , Subject: [PATCH v1 03/16] iommufd: Allow pt_id to carry viommu_id for IOMMU_HWPT_ALLOC Date: Wed, 7 Aug 2024 13:10:44 -0700 Message-ID: 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: BN2PEPF000055E0:EE_|MW4PR12MB7016:EE_ X-MS-Office365-Filtering-Correlation-Id: 75d80817-c339-457e-9b59-08dcb71d1de4 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|1800799024|7416014|376014|36860700013; X-Microsoft-Antispam-Message-Info: nBlRCTFKKbPctNSvP3WXje2yh/UMMVadIIPYjsFDOPw7sSlOFjfXocvXOUzfQUNKUaCjWNOWnVp+vLMBmFXTd8rxtyC2lSmnxhTI6tVXxsRkjcbXSO7orWKndjjeW6K0+xkyOv9jftwDHua5vq7xfCDdpiAjfZKlbxJOYXQNh7AXRH83b5BwNkb1C5x7+I33hwyY23slhoDZ24YjNYMHWgp0KWDb9XTewg+7YLsB8BYhljrIrk7pRKTULcp3/TdQNZogKv1QncOpBPgDp4QZnl7Zfk3qm2U75bQuONsD/xt371FHjIu7yyuMKaQ/mmH3aBCZYNNyrzFXdChchTWNFAxI+Pk8qzq3ohQ27pYMQnlub4SMOe+Fv7WTSxBgyPcYeiKVfUAUIotxwRRhdsu23fAK2wnHX/dOUCoRLCFR3bJqEr/0o9ZE3bFRCM1MJaFLkLHCd5elsJGbRXFCI7uG3oT/acv2/s++mkmv+1Hp75eTocgOQjTamckXK9Ah4WOfSKsoniqgbJAhYTWcqMd95IuNTP+N3bdPwhicJnhA3wAdWEx/KjWG2C1k9cAcJoikK2ozVl6mkMdF/OE9AwjuZuIUcqGoULStQYW9cwAR0zYwD5TqFWcYEtBSHdRfzkr7FQ2yFMI5sflIjUA9R3yzHl3G06KAFMMQctn94DUwSAXMoZx/ex8C1M+pm57g4OppD+qlo/bO2YcD43z3xlFsBsQHf10SihERA42LMqnP1BxUGmGfLQxNilO5tcZ30UtJe3MHfywH9OJhGxENn2N5NjEW3NctBkjue0qO/vJdUNLOHlPwDrkBz3pXKT+Rxn/09U3+a+6BYqXeo8fAh14xv86H9Qy72+f34pBWRhr3xqEpgICvh1yBjcR2PDTOdTyWdv0QQZ2eNl/7+VWbqIqyXhf2nR6AOyTPG3uVK964Hj67iW89QLKu9RZ+QoBC69aKwDklHDIYQ832xmXqxDblgBA9sUTvIwav7btY6VQ9UXpTaQh/rWH9T9Ume3e/C2jfpreCRqBvD2+5Hxnweh4Q+34Hdohx7w1KHB3n9maajsqeO4EMMTJDtB01f2KbZ9V+9X4205a6BcvrIQoc0ymYh+9UBcF41rm299IbBhtTCwmzY0R6hCCXOBSz8SmJ2TtSujvS38owqQGBUPg2POY0srwsjCTBCh/p5LwRLyivPqswqZSyhIX1VdVb86HgJKHd861YVf1KW4wVNVH53H5TECXmwbvkI16VMq+nCcHNox3Nw6LCNKvj510LLr6AI4C0SX8FLYj9Y6MtP5TdTeYg/1FqSqPMjzB+jPYdw0jO8C58V9wjXhT4tewmQ1TRwX7oqkRhbsagRR+V4QJtSVKuVvLZjqnznCZcfN9MI/nWm6rk/klB5FKOW/fK1nkueLV0Uhih4BZkRl2eHBekjacE+JvSuBT5N4rQNojornAoTpq8rDsxMPxhK5aLb4IpmijO 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)(82310400026)(1800799024)(7416014)(376014)(36860700013);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Aug 2024 20:11:26.1348 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 75d80817-c339-457e-9b59-08dcb71d1de4 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: BN2PEPF000055E0.namprd21.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB7016 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240807_131132_400575_7CE4FB95 X-CRM114-Status: GOOD ( 17.56 ) 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 --- 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 0e3755408911..443575fd3dd4 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 ed6cdc2e0be1..0e384331a9c8 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