From patchwork Tue Oct 22 00:19:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 837694 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2082.outbound.protection.outlook.com [40.107.236.82]) (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 E69A14A18; Tue, 22 Oct 2024 00:20:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.236.82 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729556422; cv=fail; b=SjAj4b4X45/thMag1EdJswHCQemTJNBm+Wnh6AomkxKWyYed/rGVoVH6u6KfC+spkexd/ZQEcdLQ8slc8L5BlLnofde9xb+I8uEko5X3B+SlI9WdXe/aaVgzZSoJ19URonFRAkEr7lRDXIGgolBTUrXNkHByXgAqNACjNNY13rs= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729556422; c=relaxed/simple; bh=z67RZ/1Qpw88A5yOnrP4tXY2nkV9jbGx5ncLE0sIC2k=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=IDoMcKykz1oLvS7j4TBEMK5RNQC30eV6GLRJiKob7/FvrZ4s7b7lTO1wXDC5wIiX3DAaOEtLkj8h6QgrOSAXzjqVKT+xEzlkZIBc9QiUBMm78h8OhRyCkyV1MZbRyJC/xp4dXb7SdvuIfKLSuexpIZ7BNMDAGBVT9uIMNk5Apxk= 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=oD3jqmF7; arc=fail smtp.client-ip=40.107.236.82 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="oD3jqmF7" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=eqM1T5Q6oNtRMzu67sSxpp/iQrX3RHxaoHjGTtUl3UJRWYUdD9LUkKsfKY5Xs4NfQCMyo14rOQldyrJSOxqYF9rD7aVZi98vwJolSODiVOgEB6uLQOFCnfzekstW/SsOrzIZ84fLyhzwHFUQby0dScuuuqo00H1rr3LpYOzUAzvL2b+QNST9ypfk8Lnbj8l9/cubYtINpG7Xh1momGg9vvGTDEOulRTR3y0cy6BWBhipjoU89SVs7192ZihUxihlA30UnabJFbXnwmHfZY12j/9TKVRyq0p52rNqCq+Km1JawgBj0EjGt3JPUmFH5SkZSgdjYVSEKmC4NwwFejm48A== 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=4+EkijV/Aru8LUJiN5jaZmLE0ssCHKJJ2dO4w4rK4Is=; b=UtHzNosE/Cvv1z9flnkg8LvsR21eXm4g67MxSmNklkoFi5sfnnb8TXU4brzdGHvsAZ9s4Lipi5l2hXcEoyGeANxo77WUbxewbuXyyMrtCzd3w/nqyhkFKuhKWvRHapH05n03E6TNt7wmEsasu5VvvNjCG85Crwwv43OedVLpurUMXjkyFMtLgKxYDDKEG9UH8aJDpi/8lVapxsZ6NXNWIJ7wz7RT/l3bfqk1L21h9s2k1Vil1ekor9yJLbqqmTY6zc48K4nwztT5UYwQfmlCCFQM5oJHVui7wFMoxMXM3PiDXk1/exPALWGAyreoYxHs47rs162LkyYkNrPN3+NO8A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) 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=4+EkijV/Aru8LUJiN5jaZmLE0ssCHKJJ2dO4w4rK4Is=; b=oD3jqmF7RDzoIi1QU4zNrdeiBrFYHxTFrHJHApsCzbmBfwKbMm7iu2HOPpWu0L51/UaMJBJDcGBqxrrmwLaLsc6+iMhZbL1YgPi0Q2lQa+oclB7OeUUAwre9re3gfGMrJPdhO6f3juuKsdPX9hw6rynnY6TE+T4JnIC2gu5zYQI2JzNgYalYyfDwDeO9smGLnsyut9Os7ZdKGLPVx5qk95qnP/aJ8uv/rXbP+ul7H3qefhP1Ta+aj72EK0DdVejE+GcDIiZQA3KAynUhnDdOPlIOUEsWxZgJW9o15j1jbpZhLj0tKmmbLsTEtzAS1gs0aiGmRyP+tN5jltsZiHO+qQ== Received: from BN0PR03CA0001.namprd03.prod.outlook.com (2603:10b6:408:e6::6) by MN2PR12MB4319.namprd12.prod.outlook.com (2603:10b6:208:1dc::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.28; Tue, 22 Oct 2024 00:20:15 +0000 Received: from BL02EPF0001A106.namprd05.prod.outlook.com (2603:10b6:408:e6:cafe::40) by BN0PR03CA0001.outlook.office365.com (2603:10b6:408:e6::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.28 via Frontend Transport; Tue, 22 Oct 2024 00:20:14 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) 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.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by BL02EPF0001A106.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.8093.14 via Frontend Transport; Tue, 22 Oct 2024 00:20:14 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) by mail.nvidia.com (10.129.200.66) 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:01 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail202.nvidia.com (10.129.68.7) 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:01 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Mon, 21 Oct 2024 17:20:00 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , , , , Subject: [PATCH v4 01/11] iommufd: Move struct iommufd_object to public iommufd header Date: Mon, 21 Oct 2024 17:19:23 -0700 Message-ID: <1452c535a2e6a6c61f38fa752132db7e88b55770.1729553811.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: BL02EPF0001A106:EE_|MN2PR12MB4319:EE_ X-MS-Office365-Filtering-Correlation-Id: e59aa6a1-d663-4e67-de90-08dcf22f4d05 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|376014|7416014|82310400026|1800799024|36860700013; X-Microsoft-Antispam-Message-Info: hpIUxeYF5jwicsz0KhY1nomIP3+SlR/jn0VMNDYDOIOmxiGws2GGB8XEd+M+u6v4JMMzfvzCIKlc861mcesuGn52XBE0na9/ONQPG/vXG3XqNfT1VmY50yX2gR8GMUwmMtAPgEmLhl+Gg15t1D8huJLa2DznLobz7KUAvMs431gE9nTqch2abYaYUj+1McR7QROqRxPl5vtWic31yHMRhGSvMAtGf8pVx0gZh/ht5SoZQdgPs8TkUcSK+FNrvwjcUZ1h+D6UvPHFF6AjwNfVmfI3rbfGFRVZZ1EV6xe4hQndJ8P2To5M6CCLXCR6fO0OHbwTXKOrp7o/FbNOfTyQ6FwV+n3WrSPyOx+05nyexB+LD4zv8W9V6xoMYg2iMgCISJMBaFv2LxWLzlrc4J65CDyDJd195P/lLcJkbLD1YLvqwETwQqoIfeXSIsf8CSZxDcO/c5CiFE77FI0iQtgvEP7KLO5uG3XL3Y+XqxpMVEvQETngdA8SSm32AKqMWDXObzykQdoclli/KefHmEXWh4C1T6wlvvj//T8VusJh3rLM5thHDhKzezRBRFp4zv2Fd8ISDO/5J/9BbRL7+HEhlScTpO4XCszrVXZzapM7iuJ6A2tXBnB1Tn1FDOLAIPbsL09eMftHkS6e4GbtRAXdxZ2PcfML6S0IOF7Kh8M7K1Eyg6Pxv2uQty4ruwYlW0mQ+MANa8i0ov3YHTNvo3eyCvBbgBj5xkcqZkvDuuMW7W1a/3MhdcotnJ/J622DEdatHpspcEapyVCKw1XUUU0e/baD21pEoj+9IMZw0L6GvUsBWWMJKZI/nMQzNEZUHOrv3GWGCIiiyOoSsYUp4FQ+BUueTqnipNf96C4R52uAIXd0zUfVu4X2e8oews3lZWvc+aNkv5xr+iRQ1jNmsO/NlsJPQO38mlmlRCG8IEaRHVPg0irKgTSmH9ucjohA78gCiKzJMvI4YrXR2uP4sgXS9Ml5b9tvKI7EWCyoZuELl7I3qyIHa7Z0aVpZSCun3hd0zU88jmXqtRokD+ftirnQ9NRuyv8L8A4cJKmxoRm4W4gdumn/HbuXHVbsDIBreXG77W5tf6PjkCkwazwdzirm1c7BlwHxZWqYs9exnqKvrLXnOVsn3v7rSNmP3qNgEDt6GzZ+7dbmhdGQiKmLGB08KwicElT85niYfxwKXUX0uwIdht1asbKSkbAPHSAXfsaxXP2KngdNwmAKMFUCsPG9msk0IhRjBltS3TidDkLVu4CzlbTB3zra7kMp/r44jXFyJLfq7skAwhBPnQuq9E7MF9NEtmH3pYJmOWORUXe9ppYGC7EE0i55n+YmJzqh+8x6XXrDox9f69uOryU1+0IQn6mseTyk8UyjqosCfkjFkRvOOBekyD0p0zmrTLcFjju3ODE+UtYMYVpze4iKAbBhAw== X-Forefront-Antispam-Report: CIP:216.228.117.160; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge1.nvidia.com; CAT:NONE; SFS:(13230040)(376014)(7416014)(82310400026)(1800799024)(36860700013); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2024 00:20:14.7351 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e59aa6a1-d663-4e67-de90-08dcf22f4d05 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.160]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF0001A106.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB4319 Prepare for an embedded structure design for driver-level iommufd_viommu objects: // include/linux/iommufd.h struct iommufd_viommu { struct iommufd_object obj; .... }; // Some IOMMU driver struct iommu_driver_viommu { struct iommufd_viommu core; .... }; It has to expose struct iommufd_object and enum iommufd_object_type from the core-level private header to the public iommufd header. Reviewed-by: Jason Gunthorpe Signed-off-by: Nicolin Chen Reviewed-by: Kevin Tian --- drivers/iommu/iommufd/iommufd_private.h | 25 +------------------------ include/linux/iommufd.h | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index f1d865e6fab6..1bb8c0aaecd1 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -5,8 +5,8 @@ #define __IOMMUFD_PRIVATE_H #include +#include #include -#include #include #include #include @@ -122,29 +122,6 @@ static inline int iommufd_ucmd_respond(struct iommufd_ucmd *ucmd, return 0; } -enum iommufd_object_type { - IOMMUFD_OBJ_NONE, - IOMMUFD_OBJ_ANY = IOMMUFD_OBJ_NONE, - IOMMUFD_OBJ_DEVICE, - IOMMUFD_OBJ_HWPT_PAGING, - IOMMUFD_OBJ_HWPT_NESTED, - IOMMUFD_OBJ_IOAS, - IOMMUFD_OBJ_ACCESS, - IOMMUFD_OBJ_FAULT, -#ifdef CONFIG_IOMMUFD_TEST - IOMMUFD_OBJ_SELFTEST, -#endif - IOMMUFD_OBJ_MAX, -}; - -/* Base struct for all objects with a userspace ID handle. */ -struct iommufd_object { - refcount_t shortterm_users; - refcount_t users; - enum iommufd_object_type type; - unsigned int id; -}; - static inline bool iommufd_lock_obj(struct iommufd_object *obj) { if (!refcount_inc_not_zero(&obj->users)) diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h index 30f832a60ccb..22948dd03d67 100644 --- a/include/linux/iommufd.h +++ b/include/linux/iommufd.h @@ -8,6 +8,7 @@ #include #include +#include #include struct device; @@ -18,6 +19,29 @@ struct iommufd_ctx; struct iommufd_device; struct page; +enum iommufd_object_type { + IOMMUFD_OBJ_NONE, + IOMMUFD_OBJ_ANY = IOMMUFD_OBJ_NONE, + IOMMUFD_OBJ_DEVICE, + IOMMUFD_OBJ_HWPT_PAGING, + IOMMUFD_OBJ_HWPT_NESTED, + IOMMUFD_OBJ_IOAS, + IOMMUFD_OBJ_ACCESS, + IOMMUFD_OBJ_FAULT, +#ifdef CONFIG_IOMMUFD_TEST + IOMMUFD_OBJ_SELFTEST, +#endif + IOMMUFD_OBJ_MAX, +}; + +/* Base struct for all objects with a userspace ID handle. */ +struct iommufd_object { + refcount_t shortterm_users; + refcount_t users; + enum iommufd_object_type type; + unsigned int id; +}; + struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, struct device *dev, u32 *id); void iommufd_device_unbind(struct iommufd_device *idev); From patchwork Tue Oct 22 00:19:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 837693 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2089.outbound.protection.outlook.com [40.107.92.89]) (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 6FB233FE55; Tue, 22 Oct 2024 00:20:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.92.89 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729556426; cv=fail; b=gE7RCtruZNkEcqgY/d0eV0P+vQ1wiUR/uQXdOnLjhL7NstBTJB6q/9gJI6MkiX6ZmxtNspCHoy+pRcdinYO3ywLYuliCiX5kEtqFXAf8vf5dLbaxh1ySetkAZP5uhczckFp5qWavQHmexYGfKAdFhuY+MYv1luy3X/kK6nGJ/T4= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729556426; c=relaxed/simple; bh=TiLK5K+fdj2/tU/yCDcKo2OE2ok/BlAw9f0TfCGAUm8=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=O/KXcm08Ht+T90Ncgmba7IX+oOln/B2Tj0PW+UTmS1j7Pgdl0YMicNRlxCul8hq6ogulMzNkZtf4VDQ/65ZVxSVtMw8R2tDRWHv6leQFw0+eevo4z3n5iMy8dfDyuWTrP4v9SjE0AiJYcSnULwkKZKPDAdVf1+UYle40C8cfDKk= 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=I8EpLWOn; arc=fail smtp.client-ip=40.107.92.89 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="I8EpLWOn" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=mokEfbEAfqh1zlxCbh0Ln3rOmb6/QhDam6EcY10hxq+VPZcHy/mX4wWDrt6FAlF2yQ5hJN/hh44YvQiE2fgBcS1lkq4Pu5Mu1kKQbd/JL+AC6tNrGJdPpalbJqqQ6K1xBM1tg/P5ew/JKi6ncqY9RJLhMIy/EtRMf7GhCVKJdmTltv57Fgl6W9hI0J1bcJ6XaPtagsw6lDA25S7jl0aaosOhl989SuKfZfxrdkzSF0y89CyBlXyaSc0Tr14RZJuLS3AxmftndWPV+9uewYAwybIoFM5lR15m2l909MvecRXvQE6qMKn8MNPE9BU0RhmluXJTN4yvcH//LDW7BqEa6w== 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=OIR0JAgwvXPvD4kxK81kzaELwIN2DXninNWOz0ykJ7Q=; b=rsmw+YH21Hq4sBBNQoFDAPdIGkkj4iwiJfxET745CtjxbbSaqE9a5QIH7vwjB7Os/XeqUYbXCS7Ecl2QsdBU1hN/iQhbPbSscYNQ+S14lqFsJXAPA96DK0AChc2sK8rZpw2zXW+OhRc4rDZxaXFX97HxjwHST8Zk7l+aovmdiq+AXvHyfxOVzX0kR85voW7QqqH///cwPPuMlk/Wr0HW1Kjir04eBmtoqlFK7oOjZv+MKJx1lV1V3mCwVEj8V1PwQLEw9D7Wqh4I6z7WdyNNeyQUqqp7k03+otnvLYktMuKngRraMGYnb5Gb27/lTnRaD2ldo4nggmfvj6oNW9LysQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) 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=OIR0JAgwvXPvD4kxK81kzaELwIN2DXninNWOz0ykJ7Q=; b=I8EpLWOn+WqgUgxI8dEJUiOyEJPokLYz3e4WwqKfqETAIqPTPoeF7suGm9qSL/1fz4OdmZmLE4QOLNblGpLtQYweuvdyakN1XFeTrv033cwagF4v2iRWzjpxvKDUJlst2vkPBDRQbv4pIHQBS5STfYP6jYQ+IUalJXK8zAfRwv9s39IYM5Ej8K01pNe+9D6Ja2CVUI6wtuL3JrXUzjlxsjXFGu2QPYaugHl2goDD15nZZPm0WoYkpnWFuBYHTj3p7pqdvT6gBF1AqoTpxWLhcig8xNSUbWKPaz8vi5mqndsPvYOd+sflWT0dEp2dBHZUu2SZ4R+DKhqN7bkZj7r6zw== Received: from BL1PR13CA0370.namprd13.prod.outlook.com (2603:10b6:208:2c0::15) by MN2PR12MB4064.namprd12.prod.outlook.com (2603:10b6:208:1d3::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8048.26; Tue, 22 Oct 2024 00:20:17 +0000 Received: from BL02EPF0001A104.namprd05.prod.outlook.com (2603:10b6:208:2c0:cafe::72) by BL1PR13CA0370.outlook.office365.com (2603:10b6:208:2c0::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8093.15 via Frontend Transport; Tue, 22 Oct 2024 00:20:17 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) 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.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by BL02EPF0001A104.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.8093.14 via Frontend Transport; Tue, 22 Oct 2024 00:20:17 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) by mail.nvidia.com (10.129.200.66) 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:03 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail203.nvidia.com (10.129.68.9) 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:02 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Mon, 21 Oct 2024 17:20:01 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , , , , Subject: [PATCH v4 02/11] iommufd: Introduce IOMMUFD_OBJ_VIOMMU and its related struct Date: Mon, 21 Oct 2024 17:19:24 -0700 Message-ID: <74fec8c38a7d568bd88beba9082b4a5a4bc2046f.1729553811.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: BL02EPF0001A104:EE_|MN2PR12MB4064:EE_ X-MS-Office365-Filtering-Correlation-Id: d3648a2d-7efe-4fd4-8643-08dcf22f4e6e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|376014|82310400026|36860700013|7416014|1800799024; X-Microsoft-Antispam-Message-Info: f2NuYkkBsRMrBdG4CiF1+X08YybDhS2H1S8lYJ17LfRAtZ3XN1ahDsRVuCvpJRQbEC0B3v77eRS5pXTqf68BRx9OWPLdDTFMAJVuHQ26L8uL86p9S1tglvPjeROWfsdOBf+08gU5DwCs5YLFIAgQNXnY2wlR8sq+YJMzxs+AjcYDsN63MVXVfMZa7YQ8/Ep3sDwqnKFJwDR46ioQNoGF6feD8wFuSl8fYWDrKvA73JEkETr70um/gHG4t70gmyS4W2jlPEOYPSxJpSsfSLqVjIpv7K0hWQeUi6EbAihQL2kxQ5PPB6jCg5DPYLSaaYDup8p47rtdp+MVAa8E1V8IiGzd4OygtJ2HdPbpTqpzVa4lQfQhaBp0jdVDQJkZRqksI82mKDiGRzM8brpGVMJUxT29+CL2Tid/brsr94QCJmgf5MBhpxZa6DwyNbR03XS5U26TxRgKkteIs72Ydi+oaJmMwzI8VBjMgWOZ0fsSkS487Ivtdik63UOG6c20P2bEYUPssbpncDCreZYHP8QPpXHko7jflUpOv2JxhVU0+CyD6yNsNlSMIMCRgbilRJvdX+sX00m5SiwKAUak3pws+rf1u+N2Fp806VMGHMWe73c9euJY6RKGTdlcExtW76k+XOHIbdR8nP+B5ls27OH83x8fO3unmy4yiPBa4eh0MkPhP1TAOu8zhTXbPyHLHDhCzDGJF/7y90NY+QYK8hkK+vEOAyOO8OYnoocUyYs+GPUWdD6N8fhLxc8sp+pE4U3rFwvZ2GsXuhIb9RurJPuOtSYIjgSCCyfNv0D1QyBb3FoiqJMLWM1LIphYoQUW2+llbh3Q/a14HaFHsvQ3blOtQDkGndJT8d/Sl2FHJK8XlQ5WEPb4sXmRYVTYr7hh28+JAb75ikT204AQuNyMBDm5K/4wBUCTNbigGlu5E4OlkF7Ou+ZHRo+ncMIZ7vDAwPLVHhXnXK1YRLFOA9FD0fsW5flzwtxDRHD6pULoMNPjUp3ihGCvpCPUKXmHW3aCFhGGY3VHoMTE3tJUPHi9YQq+4vMfhdNtNHScGc4mM5egDpi4q65PEVvwUCWzYjpfHTE3UMwjeP+F2YnAHrBDyv3sUZ6GYwB0d5ddrmM/bnob4mqJTi77yFYE4Em45Bzh8m0kwkFeIm9R+vCy5DdygIJzJl0v9/Pht7IRzgntWn/TCD0S5/Drlj2Bc9hcjvVe4YHOIejNlqMx1PPwPNzddtCW050991IyNUrflfIOtBE4oM+c2ZjCffoX/3c/stmYhLLHYXarcZTlE8oi42Dbj9r7Ky8WdJ5W1Us1AI0JC2Eq//6H/dPe5Eyn/vhtHFbsEiu2Mg7sZhB6vtVY3qvM4GdPNs9NpyR8AwxjmgsDgKP3uTmQb1gW7O8isIXHc/3SafdzDA3xUc8xAZ8TnbiIsKCOOQ== X-Forefront-Antispam-Report: CIP:216.228.117.160; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge1.nvidia.com; CAT:NONE; SFS:(13230040)(376014)(82310400026)(36860700013)(7416014)(1800799024); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2024 00:20:17.1612 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d3648a2d-7efe-4fd4-8643-08dcf22f4e6e 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.160]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF0001A104.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB4064 Add a new IOMMUFD_OBJ_VIOMMU with an iommufd_viommu structure to represent a slice of physical IOMMU device passed to or shared with a user space VM. This slice, now a vIOMMU object, is a group of virtualization resources of a physical IOMMU's, such as: - Security namespace for guest owned ID, e.g. guest-controlled cache tags - Access to a sharable nesting parent pagetable across physical IOMMUs - Virtualization of various platforms IDs, e.g. RIDs and others - Delivery of paravirtualized invalidation - Direct assigned invalidation queues - Direct assigned interrupts - Non-affiliated event reporting Add a new viommu_alloc op in iommu_ops, for drivers to allocate their own vIOMMU structures. And this allocation also needs a free(), so add struct iommufd_viommu_ops. To simplify a vIOMMU allocation, provide a iommufd_viommu_alloc() helper. It's suggested that a driver should embed a core-level viommu structure in its driver-level viommu struct and call the iommufd_viommu_alloc() helper, meanwhile the driver can also implement a viommu ops: struct my_driver_viommu { struct iommufd_viommu core; /* driver-owned properties/features */ .... }; static const struct iommufd_viommu_ops my_driver_viommu_ops = { .free = my_driver_viommu_free, /* future ops for virtualization features */ .... }; static struct iommufd_viommu my_driver_viommu_alloc(...) { struct my_driver_viommu *my_viommu = iommufd_viommu_alloc(ictx, my_driver_viommu, core, my_driver_viommu_ops); /* Init my_viommu and related HW feature */ .... return &my_viommu->core; } static struct iommu_domain_ops my_driver_domain_ops = { .... .viommu_alloc = my_driver_viommu_alloc, }; To make the Kernel config work between a driver and the iommufd core, move the _iommufd_object_alloc helper into a new driver.c file that builds with CONFIG_IOMMUFD_DRIVER. Suggested-by: Jason Gunthorpe Signed-off-by: Nicolin Chen --- drivers/iommu/iommufd/Makefile | 2 +- drivers/iommu/iommufd/iommufd_private.h | 4 -- include/linux/iommu.h | 14 +++++++ include/linux/iommufd.h | 56 +++++++++++++++++++++++++ drivers/iommu/iommufd/driver.c | 38 +++++++++++++++++ drivers/iommu/iommufd/main.c | 32 -------------- 6 files changed, 109 insertions(+), 37 deletions(-) create mode 100644 drivers/iommu/iommufd/driver.c diff --git a/drivers/iommu/iommufd/Makefile b/drivers/iommu/iommufd/Makefile index cf4605962bea..435124a8e1f1 100644 --- a/drivers/iommu/iommufd/Makefile +++ b/drivers/iommu/iommufd/Makefile @@ -12,4 +12,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 driver.o diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 1bb8c0aaecd1..5bd41257f2ef 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -202,10 +202,6 @@ iommufd_object_put_and_try_destroy(struct iommufd_ctx *ictx, iommufd_object_remove(ictx, obj, obj->id, 0); } -struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx, - size_t size, - enum iommufd_object_type type); - #define __iommufd_object_alloc(ictx, ptr, type, obj) \ container_of(_iommufd_object_alloc( \ ictx, \ diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 4ad9b9ec6c9b..14f24b5cd16f 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -42,6 +42,8 @@ struct notifier_block; struct iommu_sva; struct iommu_dma_cookie; struct iommu_fault_param; +struct iommufd_ctx; +struct iommufd_viommu; #define IOMMU_FAULT_PERM_READ (1 << 0) /* read */ #define IOMMU_FAULT_PERM_WRITE (1 << 1) /* write */ @@ -542,6 +544,14 @@ static inline int __iommu_copy_struct_from_user_array( * @remove_dev_pasid: Remove any translation configurations of a specific * pasid, so that any DMA transactions with this pasid * will be blocked by the hardware. + * @viommu_alloc: Allocate an iommufd_viommu on a physical IOMMU instance behind + * the @dev, as the set of virtualization resources shared/passed + * to user space IOMMU instance. And associate it with a nesting + * @parent_domain. The @viommu_type must be defined in the header + * include/uapi/linux/iommufd.h + * It is suggested to call iommufd_viommu_alloc() helper for + * a bundled allocation of the core and the driver structures, + * using the given @ictx pointer. * @pgsize_bitmap: bitmap of all possible supported page sizes * @owner: Driver module providing these ops * @identity_domain: An always available, always attachable identity @@ -591,6 +601,10 @@ struct iommu_ops { void (*remove_dev_pasid)(struct device *dev, ioasid_t pasid, struct iommu_domain *domain); + struct iommufd_viommu *(*viommu_alloc)( + struct device *dev, struct iommu_domain *parent_domain, + struct iommufd_ctx *ictx, unsigned int viommu_type); + const struct iommu_domain_ops *default_domain_ops; unsigned long pgsize_bitmap; struct module *owner; diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h index 22948dd03d67..55054fbc793c 100644 --- a/include/linux/iommufd.h +++ b/include/linux/iommufd.h @@ -17,6 +17,7 @@ struct iommu_group; struct iommufd_access; struct iommufd_ctx; struct iommufd_device; +struct iommufd_viommu_ops; struct page; enum iommufd_object_type { @@ -28,6 +29,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 @@ -78,6 +80,26 @@ void iommufd_access_detach(struct iommufd_access *access); void iommufd_ctx_get(struct iommufd_ctx *ictx); +struct iommufd_viommu { + struct iommufd_object obj; + struct iommufd_ctx *ictx; + struct iommu_device *iommu_dev; + struct iommufd_hwpt_paging *hwpt; + + const struct iommufd_viommu_ops *ops; + + unsigned int type; +}; + +/** + * struct iommufd_viommu_ops - vIOMMU specific operations + * @free: Free all driver-specific parts of an iommufd_viommu. The memory of the + * vIOMMU will be free-ed by iommufd core after calling this free op. + */ +struct iommufd_viommu_ops { + void (*free)(struct iommufd_viommu *viommu); +}; + #if IS_ENABLED(CONFIG_IOMMUFD) struct iommufd_ctx *iommufd_ctx_from_file(struct file *file); struct iommufd_ctx *iommufd_ctx_from_fd(int fd); @@ -135,4 +157,38 @@ static inline int iommufd_vfio_compat_set_no_iommu(struct iommufd_ctx *ictx) return -EOPNOTSUPP; } #endif /* CONFIG_IOMMUFD */ + +#if IS_ENABLED(CONFIG_IOMMUFD_DRIVER) +struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx, + size_t size, + enum iommufd_object_type type); +#else /* !CONFIG_IOMMUFD_DRIVER */ +static inline struct iommufd_object * +_iommufd_object_alloc(struct iommufd_ctx *ictx, size_t size, + enum iommufd_object_type type) +{ + return ERR_PTR(-EOPNOTSUPP); +} +#endif /* CONFIG_IOMMUFD_DRIVER */ + +/* + * Helpers for IOMMU driver to allocate driver structures that will be freed by + * the iommufd core. The free op will be called prior to freeing the memory. + */ +#define iommufd_viommu_alloc(ictx, drv_struct, member, viommu_ops) \ + ({ \ + struct drv_struct *ret; \ + \ + static_assert( \ + __same_type(struct iommufd_viommu, \ + ((struct drv_struct *)NULL)->member)); \ + static_assert(offsetof(struct drv_struct, member.obj) == 0); \ + ret = container_of( \ + _iommufd_object_alloc(ictx, sizeof(struct drv_struct), \ + IOMMUFD_OBJ_VIOMMU), \ + struct drv_struct, member.obj); \ + if (!IS_ERR(ret)) \ + ret->member.ops = viommu_ops; \ + ret; \ + }) #endif diff --git a/drivers/iommu/iommufd/driver.c b/drivers/iommu/iommufd/driver.c new file mode 100644 index 000000000000..c0876d3f91c7 --- /dev/null +++ b/drivers/iommu/iommufd/driver.c @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES + */ + +#include "iommufd_private.h" + +struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx, + size_t size, + enum iommufd_object_type type) +{ + struct iommufd_object *obj; + int rc; + + obj = kzalloc(size, GFP_KERNEL_ACCOUNT); + if (!obj) + return ERR_PTR(-ENOMEM); + obj->type = type; + /* Starts out bias'd by 1 until it is removed from the xarray */ + refcount_set(&obj->shortterm_users, 1); + refcount_set(&obj->users, 1); + + /* + * Reserve an ID in the xarray but do not publish the pointer yet since + * the caller hasn't initialized it yet. Once the pointer is published + * in the xarray and visible to other threads we can't reliably destroy + * it anymore, so the caller must complete all errorable operations + * before calling iommufd_object_finalize(). + */ + rc = xa_alloc(&ictx->objects, &obj->id, XA_ZERO_ENTRY, xa_limit_31b, + GFP_KERNEL_ACCOUNT); + if (rc) + goto out_free; + return obj; +out_free: + kfree(obj); + return ERR_PTR(rc); +} +EXPORT_SYMBOL_NS_GPL(_iommufd_object_alloc, IOMMUFD); diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index b5f5d27ee963..92bd075108e5 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -29,38 +29,6 @@ struct iommufd_object_ops { static const struct iommufd_object_ops iommufd_object_ops[]; static struct miscdevice vfio_misc_dev; -struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx, - size_t size, - enum iommufd_object_type type) -{ - struct iommufd_object *obj; - int rc; - - obj = kzalloc(size, GFP_KERNEL_ACCOUNT); - if (!obj) - return ERR_PTR(-ENOMEM); - obj->type = type; - /* Starts out bias'd by 1 until it is removed from the xarray */ - refcount_set(&obj->shortterm_users, 1); - refcount_set(&obj->users, 1); - - /* - * Reserve an ID in the xarray but do not publish the pointer yet since - * the caller hasn't initialized it yet. Once the pointer is published - * in the xarray and visible to other threads we can't reliably destroy - * it anymore, so the caller must complete all errorable operations - * before calling iommufd_object_finalize(). - */ - rc = xa_alloc(&ictx->objects, &obj->id, XA_ZERO_ENTRY, - xa_limit_31b, GFP_KERNEL_ACCOUNT); - if (rc) - goto out_free; - return obj; -out_free: - kfree(obj); - return ERR_PTR(rc); -} - /* * Allow concurrent access to the object. * From patchwork Tue Oct 22 00:19:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 837957 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2046.outbound.protection.outlook.com [40.107.244.46]) (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 0B9C554652; Tue, 22 Oct 2024 00:20:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.244.46 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729556426; cv=fail; b=jNqVnR+HYg7ekVwkdpZcnR+B8ei1f3m51bK5h4QjQAdBrUTODHnL2Mn8ZOr7vd5ofhHdNlFcwNmO/gW1t+K+spjJF+6FF0lPps58iI1Riyduyz5ip2KcqvTz5CTVqgF5ylBGUiWDKJPARReWBlI9sDVyySvk2PnX49dn2CTvP1E= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729556426; c=relaxed/simple; bh=bwehOYCy1nm4Z13PnM1A2jYh1t2+F1T8iVneY2NY1EM=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=LhiH6YDNl0BSTFc77saEyDZi2BBW+wsul0nBWKpq5hQXFtdJkkTxvDt2CtpyiIfokMe/DxJ1Cz2nO3KH9vg7v3Mabfhgni2AEkFFUHTIyZRtas0jU/scmJRBfh/6TmM2odO5TcfgMdDLXNsGncMRS9CaCktcAwFT3N1rSVtpRqU= 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=Uzd/XGtH; arc=fail smtp.client-ip=40.107.244.46 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="Uzd/XGtH" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=q4kKEg5Tun4Xm3BYB+spsAE47xku5UWF2I7pXJe+h48UURVIdlBuiU+kjzKxkMwYiLv+ohgMxCFfDjGz6hA//p0YgdjIy2BBcvRbzMslYYjteb9/zD+09CH7sl644RXEqtVGtx9+gUqt4WUkN3EsK05fAu3R0pHumGi9I9yJQzx6IqDlfqla3Att3Z2jcmTdbIpBDNIKB9YERRzv+KTdnv6SYUbqG/uxj3I2jqu90cBekb5u52qO80l4nCVLdpSuIF012buBmvQ6z1OllBkEaP1orsdDBVLVEWzRCEQESQNOX3QHoPvPYiMzpqHXy6HDlIPf61mOzEtS1VsA/uZiLw== 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=PCiiwbPRF3tMxjrTpTPyUaddxelaA0bQsArIpIV1apE=; b=H7ke4q8GEj6bEW2w2djdXNipXLKXWF31y4aJmaDiE4p5mKJnFe4GtMcYM7kyXwNhZt4pmIpkapyNfi4I30EYJpDd7OU7nPJqmZeVcvRpA4gj7JmjxONnAR1V4+4RzhivT711bn/w+2gHBefcR5LRVoqUr1lC/EBHu2nny6wCgyRZsj+3KmvgPFoCUKRYiEKsMx3SHvDOCfg6hXejKk+u/OD14jcKXHNTnba/gqUbcP9vwLM089r8ly7tK8EgbI94BWDSpV1vWQZUtH6lRutV0brZdHYMm+BRq/WX167ea4a34GAaEggMPGW+WXruDSURtzZhy82bQGC2Ib710fRx4w== 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=PCiiwbPRF3tMxjrTpTPyUaddxelaA0bQsArIpIV1apE=; b=Uzd/XGtH/nQAbenKntjv2ymsZxQCj7sxrjw0Ly4cDmBzXMLZiH6P7vucQC8leT4rbYEJWf3dkm2RniFbrbzQtm8s3nE8GQpgmu45On2DjHTExtsjJr+mPwAyShhVESCzIn8RTYpQhC+zDSgCQRpLS1wRHSh8p++ZiK96XZSRgV5LBaZtpUeyTAFq56NZIhfx1hl0HAfT6GS6N0RpVjzQc4NI8zQY2wTR/DszHqYFWnGvryLKgdR8dVf/ek0k1c6loP63/sVlgzjHHsYOEQuFyBxoJwdFPCxBeL8/0BbfX9ZpEjgttc7f89XYXKuvpKnqdbeXeqIgN64HLudy6VGKHg== Received: from BN0PR08CA0020.namprd08.prod.outlook.com (2603:10b6:408:142::8) by IA1PR12MB8262.namprd12.prod.outlook.com (2603:10b6:208:3f6::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.28; Tue, 22 Oct 2024 00:20:21 +0000 Received: from BN1PEPF00005FFC.namprd05.prod.outlook.com (2603:10b6:408:142:cafe::50) by BN0PR08CA0020.outlook.office365.com (2603:10b6:408:142::8) 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:21 +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 BN1PEPF00005FFC.mail.protection.outlook.com (10.167.243.228) 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:21 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) 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:04 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail203.nvidia.com (10.129.68.9) 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:04 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Mon, 21 Oct 2024 17:20:03 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , , , , Subject: [PATCH v4 03/11] iommufd: Add iommufd_verify_unfinalized_object Date: Mon, 21 Oct 2024 17:19:25 -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: BN1PEPF00005FFC:EE_|IA1PR12MB8262:EE_ X-MS-Office365-Filtering-Correlation-Id: 66f47bef-1b55-49b3-6579-08dcf22f50db X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|82310400026|1800799024|36860700013|7416014|376014; X-Microsoft-Antispam-Message-Info: Law1FOcW+UEbGewWY7M++AYEvwXP4NO19sJoXoSJDM80kygPi8y7k14j3ckkN0las2JcBl/jSQZtn+OhwrnqzRn206PJ0oNAwUv+FxG9rAxraGxZAWyUEYpsB/3cE9G5OCwN+fBUJHLiWsNIx5n3DsxVT+TxEXu0mvQdsxhUdD+wLTahGuZhLtcXUjA5gP8apzr8EbTeVhzz0LUYsWcf1JxRJVZeCIlvEEgcuNk3U8DD/hN2RZEBS3ZxN018vY6HcifKZCsyoFIdMbxhpyR/iCzKHbySy/eZ+r+/WLmKbC0LBjtmsVtaTVr4Ul3WQGrfX0NOP3gd4m0FoOlUbGcCVNKduwFxN9xVOMsrStSp4S1drLjpvkeOBrOlLS67+cFDCx/hQ4obJHdQsDzWGqx9u8CrBDv+Dz4CQGPalcAhDkqAQ+QbELyK8BgFQw//1rfm/Asuk5HArX0GW1qKlGUbMyMG3Abl65082kEgZwhe7/XNqM7QyyDAS2iIO4IpC8Tb2Ginjydn87VO6iwJ5F2tZmSE3m0nYe8A5jmbovE6nkckk9ebXhTtkq/OA+UJsJufXzV/AXb41rUE27YqO2s9qzFybVqkuRz0+fRUdNOF6xflVcLSbzCBDGRh2f69/VeH4mwCei9R2WKrlkT0wv8k/44jHcktP2cyxwXYrMpmF9T0WTHwx7fP0Erx2rKXttEIGGCno/pF1l/MS/ab2egZ/3m1HfgRVFTAs8cnLrQB3mLW535wcJyYl0nlibbcEjs7/trDnZ61hndvZg5FZOLraF0x75s5i4LvbC0HyYyk3JzbBXp1lu02ebxGpf5G+2HPx286aPr6Bs5uSn7d8NT5R6pzQ0TNw9CFeXzIpKdxVHmQq2XbaREsvkUNUrmZ72lFWVF2v7N1VGZZo0bw6lIZ2NVxpNKYCkYYtLbFd0NfIOrvB27/+WGd0olSYWyn3c5SfuUpGGNaHq8J8VFXzp8g5Fj1zq7mmYGcU2um6F+C6H2megjNgsdf6GMjY1TTN9+w+McMSR8nWsIrBZULcqTNJuUDp4WFRk+aNt5afwTl1gEO7rJHo/pYPZQtBFCM7qIXZ7dj72Mh39+a6b+N6fNIdtOiowot4Olo31U/aEXFJoIiV0ps9e4vmp6DskMcCS8/7O04ukHLqpqaikYPGJiRlUzgA23SQCIPLch9S6BAfO04KTRnYfglw2WOXnaomsHafvvVTSmvBi+YRl7ncpIzuHZ2iTMXDo/YgvPJbVJbEsK0v1hNwkdCfq98glxOuIVH/VWetyVQKKVsVwD6aPXQDmVZc5nQK/tGDdTUYgEa0j3tahPXIy95jlUJXx2mDCa8REJT+gxOY+XYSiwjTGPAwcUrPjIY8v9ruFVAn4xdPmbXteojo6A6bB5OGQcRKOG9hiGeOsKC5uSdPhhDcpln4w== 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)(36860700013)(7416014)(376014); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2024 00:20:21.1676 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 66f47bef-1b55-49b3-6579-08dcf22f50db 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: BN1PEPF00005FFC.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB8262 To support driver-allocated vIOMMU objects, it's suggested to call the allocator helper in IOMMU dirvers. However, there is no guarantee that drivers will all use it and allocate objects properly. Add a helper for iommufd core to verify if an unfinalized object is at least reserved in the ictx. Signed-off-by: Nicolin Chen Reviewed-by: Kevin Tian --- drivers/iommu/iommufd/iommufd_private.h | 3 +++ drivers/iommu/iommufd/main.c | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 5bd41257f2ef..d53c1ca75532 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -152,6 +152,9 @@ static inline void iommufd_put_object(struct iommufd_ctx *ictx, wake_up_interruptible_all(&ictx->destroy_wait); } +int iommufd_verify_unfinalized_object(struct iommufd_ctx *ictx, + struct iommufd_object *to_verify); + void iommufd_object_abort(struct iommufd_ctx *ictx, struct iommufd_object *obj); void iommufd_object_abort_and_destroy(struct iommufd_ctx *ictx, struct iommufd_object *obj); diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 92bd075108e5..e244fed1b7ab 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -89,6 +89,26 @@ struct iommufd_object *iommufd_get_object(struct iommufd_ctx *ictx, u32 id, return obj; } +int iommufd_verify_unfinalized_object(struct iommufd_ctx *ictx, + struct iommufd_object *to_verify) +{ + XA_STATE(xas, &ictx->objects, 0); + struct iommufd_object *obj; + int rc = 0; + + if (!to_verify || !to_verify->id) + return -EINVAL; + xas.xa_index = to_verify->id; + + xa_lock(&ictx->objects); + obj = xas_load(&xas); + /* Being an unfinalized object, the loaded obj is a reserved space */ + if (obj != XA_ZERO_ENTRY) + rc = -ENOENT; + xa_unlock(&ictx->objects); + return rc; +} + static int iommufd_object_dec_wait_shortterm(struct iommufd_ctx *ictx, struct iommufd_object *to_destroy) { From patchwork Tue Oct 22 00:19:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 837956 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2076.outbound.protection.outlook.com [40.107.220.76]) (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 D97D084A51; Tue, 22 Oct 2024 00:20:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.220.76 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729556429; cv=fail; b=E9wcQWybAtm4T1+ltbW1xhTksSf9aMGh09EdDnXJbpuwBnPYf1kw7v1cD1FdSLLlScp51iAkhzvZ5Ie87AYt0/kFxyKo3Yy3PPICyV/mE3qRm2bJX8tG+cnoff4WLsVSEYAbaG+DqEMMTYB3T9skI0PJRRgtVbRLkclx7XN0Y+4= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729556429; c=relaxed/simple; bh=qLhswcpjU6K8MhIfaCkUrUbSk2ctdHEo4W9SvV5ZyzA=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=kLE0Zo/kQfMdPboPOeybs/rsNreFwqJxA1Fhb38Z8u6Wm70OU5GLfEcmJs0XRJ5gJe6lHxb6fqss/uCxwqw7BnM648uKBEmmsP5zNspxstXaBcbJ3NPMU3Kmh3wKuBXGnZ0xbT43ASp9CZaPgkfsYpQgK5e+BVv7L88vKCE5AO4= 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=SCGIZLVz; arc=fail smtp.client-ip=40.107.220.76 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="SCGIZLVz" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=TwOENDxZUubk5R4KNvl8mtM0/Dw3Hj1cchhHAaWGtl5FGdmz6FtAt7bDn3XWzNhfV1PfI1h13YJyMgz/yEW2Z8GXBL2o7RfMVo3KIFejuUfnXRl2CZarQijZ7REwJvfSgWZDaXVhSxJlS8MIvEvIWZiixsvd7DBIzzuIp/8ddSJU1Mcg3ttIC6CcwMQyIfAdu3braeTqZfNTBi2h0zzfjcAigpxY7+6z0dA+l64ESy09aZ+7ToKZ5U1oLP5of4P/GrUKZ5KRxZeFYMnuYu7sDe+puO8QU1Cwl9MjGfZZOj1BVNaU9+HVC1ixC6WW3exuV2v7Dmr2C49UnGaZ5EjsNg== 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=bAsGXKoE0NMi9fAvr1waWjg7ITDJ7ByaGMYwt099q/Y=; b=DzsnBqMd+vVPiqhxOkqi/t4oz3vLosu7SzTbY7XZ0dC6VNyjNkIGO6E/Fa8zQycbgRVXfjKxcz8jqaay20ITKigBnqddzQN6r/v3zgZAU33UUOE1SDV09mRJ/Lt0BSvzniqDxLd+WkUrX+4v6PGFU7jdItL/ANbAnYyE02Feh4qTkGlwmqO8eXsNavcNgTCNX+PzbfjAjhStfsKaCoo4DaISiTbBrrCCq4BU24Qk9TuZ8w1v4NayRZnYMz0oT4krSG69DOBzX/a7D/kbUEUvdyz8J7BVhOp3AMS3xUI8rsRHTSxo1yR4nEg2HK5/iljZtNFFfXrA0zyLGcIAlP8GvQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) 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=bAsGXKoE0NMi9fAvr1waWjg7ITDJ7ByaGMYwt099q/Y=; b=SCGIZLVz0tTbMLTd3CbB3iYhUFg2dBzTMy1ZVaAP1vzWqF3fGd8l/vdE8/kXqJo3z9Zo/x12WacCNv/6gMDLHpMLzdlnOl0AzIS5GFL0sCJvi4RvO3jjBBbr3jBxY29wFzc9LqEHG2IPsV+W615JYFGJD9xQctljTXdRSoja3Blbs1nBLH81NiTYZkz4kzTpHkibddascng7QRfyUZnICD99R1d6gPxNI6hglxF3p3fB+5Lp88mWcPQEvIqyez3JmaHtan72oFfoRFPJgi9tWxG7a2dfiqlgJ5muX+aLRhKY63DpMp9NtvJm0TtPFRQaVjZdZayKFTu7/bpcNMkdHQ== Received: from BL1PR13CA0383.namprd13.prod.outlook.com (2603:10b6:208:2c0::28) by DS0PR12MB8503.namprd12.prod.outlook.com (2603:10b6:8:15a::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8048.22; Tue, 22 Oct 2024 00:20:23 +0000 Received: from BL02EPF0001A104.namprd05.prod.outlook.com (2603:10b6:208:2c0:cafe::ad) by BL1PR13CA0383.outlook.office365.com (2603:10b6:208:2c0::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8093.15 via Frontend Transport; Tue, 22 Oct 2024 00:20:23 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) 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.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by BL02EPF0001A104.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.8093.14 via Frontend Transport; Tue, 22 Oct 2024 00:20:23 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) by mail.nvidia.com (10.129.200.66) 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:05 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail203.nvidia.com (10.129.68.9) 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:05 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Mon, 21 Oct 2024 17:20:04 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , , , , Subject: [PATCH v4 04/11] iommufd/viommu: Add IOMMU_VIOMMU_ALLOC ioctl Date: Mon, 21 Oct 2024 17:19:26 -0700 Message-ID: <9da2cf334a182ced4d4ffa578b87889e9c0856f3.1729553811.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: BL02EPF0001A104:EE_|DS0PR12MB8503:EE_ X-MS-Office365-Filtering-Correlation-Id: c7e28250-ebed-4e9d-80b5-08dcf22f5236 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: NZeavq9/hTHZxs5QyBBpkGTQt63WYMpowOyf8TLZy6/4PXdU5/TS8pDylGEfPr0UomHdtY5UJ0iG/jigqN44lCUKV2xrk0+404UaeIdSNZbp9KwJxpXzqqDKcSGSVXSXfvwyd9KmUv55ZeE5aHtY8OSFcos3gRT410j5Mh5HtA0O1MYWDG+Mu1R2ZubvwFNOicLUGQRk+5HlJ5D10JGE+ucyISnGp/T8emR4QoHloZCmCQFFU7j7Lqg4zIMrRowWIHyZF3HuauKWa5W0cmBQvAlyML+5Pn3vLlKA96gXfpdZMrwlzZ3OyuLXIpQL+FkTUN3NVPAA4X1lBk+zYymjcc6Qbu16IyugFeUWr70TQ+ZZEGgucgvNSz+HN8f3tqW8H/vtMfgNJ1C47wa0JZgBbKnrIWYDl5FCi0wh3UYAJpnw0jXcLwzr5PFwdC/zyT7L295D85nunSbKegAx0HBYwdPVBM9NBGZOPijQXJSGm/pcQZcK79U2frgMe3tK8xXQKxb6Z3/s6vVT8i6UlP//NFgCz+MmXUyBAg+7dSc+OFh7ULXZ7v7vW4e89A67mmSSw2zcOj64graINhI96F+v1chN1ElVzTTAvmOTtUQdxi/2RKW5F6dmLJWKij91qMrC3Ow2g2r6KqwPFex6wZxIpqPYbjp7XoCSLDVScjHh63a4XDdmi+VyqaqIb2o9n4OFwA5ai7Sr6famsZAgWSPUTKWXc0kOgrj5049WiZa/7RT0J2bhtHRYfich/0WdeGQ5IWAuZuYQdYLY/fgWwl4mUs3ta0cWTIV7tDKy6NgGbcClHp+QidPxUgErzB7kjAfaB8oRf2isW3Sj/EE2Ct7NUJppE98TILigYEhMPS8M0ZUBPoZX1fpRcg50xOZ/v+ZZphpygTQQr78BR4E5iVKVgYdrr7hESampBNftJZb2Z2PvB2fEEQVY7tvGtVGz7b5R4Ph86Q+s2P22duBEGAjeHcww/Lt1NLiyvHweyzu3TeaREohkaAl0NcKdRwcOiEiIgLbt7dqcp18L8aazdw2H7vhhmb4wEhM7itsPQUv/445Injt9Jy52wKM/baWcFeww4vXekCSrKWnmBLzLnPru6hFoTTdNPQ4s+dftGyg/0XT11cRe3aBUbIM9IhrNffEbQPsw+lmFGMwf+39rpGwcMvTc+fKyk40Nh6g9/vz4Vy7VAM8+Kn0MtSxhWxS+yv1SRXpQsvxBZwnEFZZdKlAOACrYeTDfU/2dcs3tDTtQomPhnIfcrRp9j1HiUeG0lZot/L5Lijulv5hZVN6yDTz70HDrFDURXPfA4f0ZtzLk14te02Odmxiq/KZs5hGBZx6LCmz/YnPXFYZpzgagzwWgY37jTiFZNVGTnc+6Fz4xV7WW1AAN1/dzp/qCu6fp56+Q2W6PJtAf/KKhCwBdkmGHtQ== X-Forefront-Antispam-Report: CIP:216.228.117.160; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge1.nvidia.com; CAT:NONE; SFS:(13230040)(36860700013)(7416014)(376014)(82310400026)(1800799024); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2024 00:20:23.4424 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c7e28250-ebed-4e9d-80b5-08dcf22f5236 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.160]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF0001A104.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8503 Add a new ioctl for user space to do a vIOMMU allocation. It must be based on a nesting parent HWPT, so take its refcount. If an IOMMU driver supports a driver-managed vIOMMU object, it must define its own IOMMU_VIOMMU_TYPE_ in the uAPI header and implement a viommu_alloc op in its iommu_ops. Reviewed-by: Jason Gunthorpe Signed-off-by: Nicolin Chen --- drivers/iommu/iommufd/Makefile | 3 +- drivers/iommu/iommufd/iommufd_private.h | 3 + include/uapi/linux/iommufd.h | 40 ++++++++++++ drivers/iommu/iommufd/main.c | 6 ++ drivers/iommu/iommufd/viommu.c | 85 +++++++++++++++++++++++++ 5 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 drivers/iommu/iommufd/viommu.c diff --git a/drivers/iommu/iommufd/Makefile b/drivers/iommu/iommufd/Makefile index 435124a8e1f1..7c207c5f1eb6 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 d53c1ca75532..9adf8d616796 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -504,6 +504,9 @@ static inline int iommufd_hwpt_replace_device(struct iommufd_device *idev, return iommu_group_replace_domain(idev->igroup->group, hwpt->domain); } +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/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index cd4920886ad0..d1c99285eda0 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,43 @@ 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: Reserved for future use + */ +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's physical IOMMU will be used to back the virtual IOMMU + * @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 represents the underlying physical + * IOMMU's virtualization support. The vIOMMU object is a security-isolated + * slice of the physical IOMMU HW that is unique to a specific VM. Operations + * global to the IOMMU are connected to the vIOMMU, such as: + * - Security namespace for guest owned ID, e.g. guest-controlled cache tags + * - Access to a sharable nesting parent pagetable across physical IOMMUs + * - Virtualization of various platforms IDs, e.g. RIDs and others + * - Delivery of paravirtualized invalidation + * - Direct assigned invalidation queues + * - Direct assigned interrupts + * - Non-affiliated event reporting + */ +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 diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index e244fed1b7ab..ab5ee325d809 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -321,6 +321,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 @@ -372,6 +373,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 @@ -507,6 +510,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..e612f3d539b7 --- /dev/null +++ b/drivers/iommu/iommufd/viommu.c @@ -0,0 +1,85 @@ +// 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); + + if (viommu->ops && viommu->ops->free) + viommu->ops->free(viommu); + 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; + const struct iommu_ops *ops; + int rc; + + if (cmd->flags || cmd->type == IOMMU_VIOMMU_TYPE_DEFAULT) + return -EOPNOTSUPP; + + idev = iommufd_get_device(ucmd, cmd->dev_id); + if (IS_ERR(idev)) + return PTR_ERR(idev); + + ops = dev_iommu_ops(idev->dev); + if (!ops->viommu_alloc) { + rc = -EOPNOTSUPP; + goto out_put_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; + } + + viommu = ops->viommu_alloc(idev->dev, hwpt_paging->common.domain, + ucmd->ictx, cmd->type); + if (IS_ERR(viommu)) { + rc = PTR_ERR(viommu); + goto out_put_hwpt; + } + + rc = iommufd_verify_unfinalized_object(ucmd->ictx, &viommu->obj); + if (rc) { + kfree(viommu); + goto out_put_hwpt; + } + + viommu->type = cmd->type; + viommu->ictx = ucmd->ictx; + viommu->hwpt = hwpt_paging; + /* Assume physical IOMMUs are unpluggable (the most likely case) */ + viommu->iommu_dev = __iommu_get_iommu_dev(idev->dev); + + 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; +} From patchwork Tue Oct 22 00:19:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 837692 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on2087.outbound.protection.outlook.com [40.107.101.87]) (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 80DFD126C14; Tue, 22 Oct 2024 00:20:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.101.87 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729556431; cv=fail; b=ZCbgpjy9u6G/GvRy+fHfjFNbp4WMKqSaNyOtYooC+L9O0Y61ciuY1H9eqOwn9+vdXceCSvOqjVqNw+xtlx4+WT7DJvBQ/eZmT1trrLWJ2Zror+n8i40NxPlo3AK9VuZj7eil7FwAJMJPTtCLSgFlRAeWZtKLgfsD4sWyQeoz+nI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729556431; c=relaxed/simple; bh=zU5dBdo9PnKtkWU30oLHIfo9nGpGuSSi/oenS6hyH2Q=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=CHy7abde/Wc5HqC6iSKxJIar38ge6JkjXdvTM4WPzZhxgtq8sqMZMD91oLR0xazwrAU9oW0yRG1a5K9E/AlynROB5PzxcI864shy9fuh3HevBaJ8DHKAfiojwhM1bMRN6d7468UHL7mr57OAhrXeki47j1Az8glSJkX6JWsVfUY= 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=XAEG8GDQ; arc=fail smtp.client-ip=40.107.101.87 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="XAEG8GDQ" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=AUxPb7KgR0RyLdtjKD4ejGUe1NVcNaS3Z9AKyig9OFUTvWe7m0hm2P0VHJEsSGd0GhmQ7+PrGQEaYnxG5PZtzL0nU6wN5ccMx++qT9nvtheJwLHzfTp/3mbn8enpHInUuRfpBHlbiguMA4PT8wKOKpej+sl0B+rSDWlGmopqt539uE1W9ItIs6rkmRIRe+ySfNPaagp889kDg6S++pYSZJLWsXA5raQrwsZsvkXyUQXWR/ixb9aI7+b5tjoMgOHp7ZLmzC3jtWCt006yXU+7ydZoADBn2fD+dBxYPGrD7EsHdzNaCNYpmdUpssH6Iy/5YbI+lOsPzXNW3Cjmj29AjA== 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=GDOVpruPVHaeXX/yLPhF82POkqZ88LWPXhGzJ+rlYmE=; b=LJuGxXMxWLm3MbVXKzhmAWEGYUUITCo+4gNQtgythCWtT18XISfYIAB88TiHpd4K2MUIDwNXp3GmgRdGU+nXZ0J5BPWNU3clV9vh4WO87UB9icx3uzedY5VpFuEcxfUMYBrSPgkO+fGShNjWIr7hqNkfKGWOQJhmkiEUVv7aS6v579kZ7TKZha9clj8ikNc/sEwnmP+EeCG3Uwdp1vOcOrX9Pb0fKnhLiEoEk2bDCYGJU9HjN1yP/QmYXijbYhECoMaxItSKpOoqsxOI8gJRz1VuSvrnBU/ND1JeKX+ey8rppeNykLpsYeWNVjDqYqrdAxrxp6rebnzaYif8D6+wEg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) 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=GDOVpruPVHaeXX/yLPhF82POkqZ88LWPXhGzJ+rlYmE=; b=XAEG8GDQ9+iwgz5QYBnxgNSUH3oM2sF8DzOQshaET8uir9lmjT68JtpbtZHzTVFUWYwurCAFZnxgN8cXdnkPQQDjv6I+eGXLNJXP70PNEgdDSidaCiYRNGhk5M0RkZfy3AVoPjtjeHcf7ww31EN++Ar7K6oZDEStxeKiXAA5Ej7ZwHl3lq00M8/E4RadX5DvTePFY/+CYPsETi2BtWGKNjVPI6rvnNfFXgNdO7F0rZGizsQTEK7N3t7Llui3UDdqAlpoDGSn9HU4ip6/CXa28hWxt4Tv99zrTBbErDw6wDOF5dXblO0MyV6J4ID/zYwye/XInAmm9vygaE7xIgZ4sg== Received: from MN2PR03CA0015.namprd03.prod.outlook.com (2603:10b6:208:23a::20) by IA1PR12MB6628.namprd12.prod.outlook.com (2603:10b6:208:3a0::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.28; Tue, 22 Oct 2024 00:20:24 +0000 Received: from BL02EPF0001A107.namprd05.prod.outlook.com (2603:10b6:208:23a:cafe::90) by MN2PR03CA0015.outlook.office365.com (2603:10b6:208:23a::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:24 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) 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.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by BL02EPF0001A107.mail.protection.outlook.com (10.167.241.136) 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:24 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) by mail.nvidia.com (10.129.200.66) 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:07 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail202.nvidia.com (10.129.68.7) 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:07 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Mon, 21 Oct 2024 17:20:05 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , , , , Subject: [PATCH v4 05/11] iommufd: Add domain_alloc_nested op to iommufd_viommu_ops Date: Mon, 21 Oct 2024 17:19:27 -0700 Message-ID: <1314e12b76edd84a8c9d0f14a6598538c8eeb50e.1729553811.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: BL02EPF0001A107:EE_|IA1PR12MB6628:EE_ X-MS-Office365-Filtering-Correlation-Id: 8b40a788-fb50-44e1-d181-08dcf22f52f3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|376014|7416014|36860700013|1800799024|82310400026; X-Microsoft-Antispam-Message-Info: JR1dMC1sNuD2TLCQbudjDLiT0eN10ofKJzXldxfMRfN6CdAtACMnK8URBambAgJU/cnGhzN2le1hS4JVzFdmOkn6mNZAH9X58LYh99PyO961PhahkbJTCGWhcxmlj7GtYd2n+X4LoStpU41mn99xccqO+2kte5JklDY1AP25YBsjjgvw8zmjGq/6IqexII23T8Dde+et9wDYivCCl3yiDzlxfogH++rdahAE/wdXpo8omD38KKTfna+ZbVQGisjN7SXk5Bf6RuUN4Gg4+SlFyvmgd/Mm9Q1Uqa/sw3kJ4tJxw1Q2fDTYctym+dCNQQQxxE+12aMgzTWDWOsH2YloGe6hA7T/0wwJJpRcXkISY/bGPDyHFpPKgjtlvAq/TkZETRPNRbg+OiiXaSJwsrkJb8kz2tez1oaVXTGFU/L0O4+y52zSG8VuCqalCOLJwVtux/aeTxfLueXESDZdCibMHJD2aREUW8Lm7Y/QJhEfJQwZD+RazwjQ5G4Dub7KV+nmpN6QG+6MbSEDXfAjEJt9gwa0EO72Mdh0ZOCLESyeg0gSaYnUds17LiVf1JeAaLDSV0KT+eeIgPO256utBsl77kisAkz+6AvA45BAop4iYUM7nq/a4/tCPS8XJIFB5tMoKlbphicaSaJ3oQDaf6ZMwF/7kW6o0z1gLTU2EvywCPQHRQTRYHxV2amn/JTV3itrSUXs5/A4zIsGFtZoNw1nCA5bHqU7ShOtBwygDK3Sx+SbFdH4ylLVTXokitRVEGnL00nF8dCnZVO2pY5tBZhzyJwMilxt4IQ43Z7bYo69eoCHd0u7DaWgIV3nXyPjLO9H2V+2ChhDY4def0FG0vqgH9QkXMrOylzhnN2rl5UGLgu420S2OLqFmNg6/wvsFcLeumnEBT+V7nsjgsCWq7+p3BGad4kFwIvTt85pu2XAwtkqYA1g5JCl09796sPPVRyz2Q36sLFG5bPzM3sBDDkIV79SB7YN6X5x5YdpAqLCASWRDg5x+i6tu321FODHiffZcaEtafKP9CQCFjpJvpwuY1QOM3v9Sk9iYMMVh2kWyWujPLL/7Wd8dDgy8uPDx1ih3tdkwlISGw3/ajvSN4u5PqAxPADHBvSePvcVKup71h1Pkwau2VX407clh5l7LhkoCMbhZWSoibfgPquXjEozJNFTq+Mq8hr4+P9thxAFOdZWqD940hRdoAbk0LiQXZDiERIQhQTAXXARSjiSGioaE9V0u4XLZQG2rsBThyfCNVoSFHhcqvrCpvQNZbX6voLm6JS1kaPLVpZE8NE1Zms6TZ7QA3D72j0VO6yOuj0vmK9wke1AnewyPWYxezHbEGQol7Jji1kLIkijVo00Nrdz33CACDt1ZZ2TeYYCzSW8fswRRA93lMtkp/fD5+9jn+mQvNQFLO+7mRNdyKQf4rBQVg== X-Forefront-Antispam-Report: CIP:216.228.117.160; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge1.nvidia.com; CAT:NONE; SFS:(13230040)(376014)(7416014)(36860700013)(1800799024)(82310400026); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2024 00:20:24.6988 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 8b40a788-fb50-44e1-d181-08dcf22f52f3 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.160]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF0001A107.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6628 Allow IOMMU driver to use a vIOMMU object that holds a nesting parent hwpt/domain to allocate a nested domain. Suggested-by: Jason Gunthorpe Signed-off-by: Nicolin Chen Reviewed-by: Kevin Tian --- include/linux/iommufd.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h index 55054fbc793c..5c13c35952d8 100644 --- a/include/linux/iommufd.h +++ b/include/linux/iommufd.h @@ -14,6 +14,7 @@ struct device; struct file; struct iommu_group; +struct iommu_user_data; struct iommufd_access; struct iommufd_ctx; struct iommufd_device; @@ -95,9 +96,17 @@ struct iommufd_viommu { * struct iommufd_viommu_ops - vIOMMU specific operations * @free: Free all driver-specific parts of an iommufd_viommu. The memory of the * vIOMMU will be free-ed by iommufd core after calling this free op. + * @domain_alloc_nested: Allocate a IOMMU_DOMAIN_NESTED on a vIOMMU that holds a + * nesting parent domain (IOMMU_DOMAIN_PAGING). @user_data + * must be defined in include/uapi/linux/iommufd.h. + * It must fully initialize the new iommu_domain before + * returning. Upon failure, ERR_PTR must be returned. */ struct iommufd_viommu_ops { void (*free)(struct iommufd_viommu *viommu); + struct iommu_domain *(*domain_alloc_nested)( + struct iommufd_viommu *viommu, + const struct iommu_user_data *user_data); }; #if IS_ENABLED(CONFIG_IOMMUFD) From patchwork Tue Oct 22 00:19:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 837955 Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam02on2080.outbound.protection.outlook.com [40.107.212.80]) (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 7D430126C04; Tue, 22 Oct 2024 00:20:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.212.80 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729556431; cv=fail; b=sd5UKIH6lvNWKmQ8iKEua7Wb8S9GPNYSEcjDA3qyd5vLvifS6yznwNYHddMX56VKdUT7OVlB6TNlmomnwClqnKdFfUD7tqnwb+Q8okqgUDk12CfnC5QLUoWxaOTGEzwB/G2uCcsKndRhnVlUie8GUNO+QFZR6qILMug1mFn6JDo= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729556431; c=relaxed/simple; bh=yFHZGiTa3Kr6a9TZ98Gt1kGeEOt25pFOydFmBKLC2s0=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=jqIvpQayQYviv4pg3z92jsI9VbGcK81NwNjfjvAqrq1jGoj9j2YaKSib3c0tylUJ6z2TzNvwszVT9MlYe5IQ6yqiH8azdlJIC1u6rMrqv9qp9ywFOQSnjKc/qUL32RDnZ1lmIKAPV4qYaL6l4/uWCB/+bv45dQ2kFD/Q6W/UCns= 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=I+kmXRpU; arc=fail smtp.client-ip=40.107.212.80 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="I+kmXRpU" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ZctvWnaCZghXZ0hXBEzrMX7rLALiGZLuwjjAQ2FrWrluwDdb5jSk00MudZq/+L0Zar6VA7cTQesK7voQS0Ldod2KGAaAYW/pbjA2mP1Ruu3ZZD1njpCdL5tgpPGU2/x0hL6/w55llEB3AbYyrNZFGvKh+zKrddgnPWmv8tNWUwanZXMw/tlDI9DBM2icolXTmo+VO1nNy/Sn5VXtfK2S6SWgh8E/RU6IlZXSruX1GfSKrwIqyUiV0jGhJhBXVWx7JkPtpzuFfxJP1e75bC2R/93boOczWlWIM6MTBeN+v5ETzlE/FDFZOwLOtGLXMhlW973G7swNBuiD3AZx4ZeJEw== 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=gaZj/0/qzf2gqR5W4kDyC2Pv3YLPq974RgNJQnRyxMM=; b=RoR8qYVnJ9xfEJKpURxmwd3GnBStNu3P4TiXos7V+wJFuCU3rNcBbdl/wAZgXkFnoMZhXp8vDKeTyQolO3edP2wbYAgFqdPiwR9ZWnG/0//47HD2zg0JfIxBU1WI4M1bkcUg7kE8tiYuhPQJR2LfXKXjxmp3Suz6eOf+evAfWE1nouWfJ9/c2y1HcNPGUfwbFFx4WaShlD4k18DZOYeTI6wIAANFykk5k0PvXrHRLdHQntKdLhK/sW+T7t5inzz891UCGHCHfetqdbPtHFd5uQj5vZ+iIJZowgH/ph2vb7qawZHB47KIXUxg1J64l42f5Won0Qowj+w9lM4CHtxf7Q== 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=gaZj/0/qzf2gqR5W4kDyC2Pv3YLPq974RgNJQnRyxMM=; b=I+kmXRpUPT6EUui3Jbfv4ezrwUOsni+M5aOucgDaSgxp8iKICU5FtT2DEbwH1fSCdWHoC7MvMuDFzWDLuU7NAbCsdUO9DDMiJyZ29BtlpQwD1igO/JXlh8zz9bwXKYdwapfJy3MGEFyWPEwJ0FbY0iTjlziHOBI1urTje/bJPgW3MUcT+WaMfnQ1fRnAsTiigubbZQ17btdth9sC4523qy/aDqSBFxI4Nht1rcyoRadAo3KIw9HFzrKP5TCH8QwbtI6VHQsmqmXgT0sH4v9QFF7cDY0BOlHnSacfxbDGGoR1r+w6A5UmJnIGhQ1GK1CB82n4eeZE7x1vLiFaNu7Bpg== Received: from CH0PR03CA0045.namprd03.prod.outlook.com (2603:10b6:610:b3::20) by CH2PR12MB4280.namprd12.prod.outlook.com (2603:10b6:610:ac::11) 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:25 +0000 Received: from CH2PEPF00000148.namprd02.prod.outlook.com (2603:10b6:610:b3:cafe::27) by CH0PR03CA0045.outlook.office365.com (2603:10b6:610:b3::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:25 +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 CH2PEPF00000148.mail.protection.outlook.com (10.167.244.105) 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:25 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) 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:08 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail202.nvidia.com (10.129.68.7) 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:08 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Mon, 21 Oct 2024 17:20:07 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , , , , Subject: [PATCH v4 06/11] iommufd: Allow pt_id to carry viommu_id for IOMMU_HWPT_ALLOC Date: Mon, 21 Oct 2024 17:19:28 -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: CH2PEPF00000148:EE_|CH2PR12MB4280:EE_ X-MS-Office365-Filtering-Correlation-Id: e88d6e03-1194-4834-3bea-08dcf22f535b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|1800799024|82310400026|36860700013|7416014|376014; X-Microsoft-Antispam-Message-Info: ut9DQR0v1Lu5Ju7PvbxnnosglX1o1WObY+ZiekqBBm7mFV1HVZ4Qky0aF17wwIhY9c3jwnLfK6pLtwtwHzkIQFq4PEdPi6p9b6RmoocMShaZ9LuK+zGhgEFOFIlokgf7UclFxLo6uMOVu2O7eKfNrzyaXNUbpdE7dVUKYWCrXV/3bLAMlKdVAACjyl6X0uze0idwfdQ23Je31WZfpZE5+c06uH9QXLGeNtFBQX1HTZHDYlAq6ysU65HOVqzHw4c+fP1ClsRKyV0Gc7vgc2Q4tOIjLnfpN/MzgfGjz7S8c8n7s+RZ8pib/TUaeJuLUKPUPaemDH/rh9EXPRaVYjJfw+c8PSw29ffBqjxr4EhBoCn7QAnX9S9PaWg+R7BNHjPEetiYD8XqdpamGvihZrkNAYJE3M1HXBgdkcrTg3vxmu6BtaN5+lz0ccW9qKE4BhtJVL/xlhFACUph1ZR5WhqeIoYFDbrV4dwify6IuzLZ6jaLPiB2ZY6NEcIWjbj3/XChW6Sl/BD4RaYTVyO+ZkV2juD9MulOy0trQBvZHyKJ18dyHLhXkq1Th292KQH4MYYLRtb+/ZZ0TZEw1OGhnx/jAVNizYKUUXjj6Vx0szH/w4SxL3LATYbmXE6i7Vl9y7wuA4D4PABXxwody3nMZvGzG4wREdCr2B9AsGd8divJ9mo1OlBiK2qRG1B6UxVQ3HIRNWzvW/EOfTBwaGppzUxCHoqo+ITxpyCqf26CG36Er240E7cZloJVUt3gtftN+e8dhn0idcuzY2r4NhRD/4G3/MZWqB/7FpLMP+fe0ceKiuNwVEpfIJpo5ah98OB44jgSqbTOFeHYLSejiPydLsCJdhlWA13m1I/s1UQC1G1oDtCSKYbsBoJ5oOgrZSTGhQEJ6rtmGklIBrenRrIrVt7SdYh/0Hwgwes46+BCElX03xZ+gESUuPON6J/urZ6MaWQLHtp0UWfvBVdn4eXsTN8jrNUUi2d28EVnVhdJ2UmpW8RINGDcA8mZhPh2ylQ4SFuFsX4PSaWTdZEHpIYGrnw0UqRTR1P7fhFOQFssM/T9LxVMMj3v0rpRnnSxSq2Q8YKGDoth7Efktx+veoeSvPiHF/zKNk5NrbPmtdVswc3wvfVq/ZkRcAPvn3zrSx90PEwyJr4ZyZRJAfjcRRxPkPexC7Wj+pgVM7zWIan7yRihAzSMeTU6z4ylHQkxPE3k4ijji1Tr9LMpK+XQjUU/G3upo463g6F+KKvbu48lpOeBKyuQo3UWB+zudyJpa1z9XwOFdCMdFgbpte3+jR46tLtuhjGmF0TwymRvdKn3RZDPQQEgth9+HUUObgTk8k/LKV8zsaK6mIYy0WsQVjg2nstU7AWX4VyWsz/lsPM6IpkPKbJ6MV4QPE5t6pdtV2HcAoLY7J+famVPbWeLF+7wMtN3Hg== 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)(1800799024)(82310400026)(36860700013)(7416014)(376014); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2024 00:20:25.3921 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e88d6e03-1194-4834-3bea-08dcf22f535b 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: CH2PEPF00000148.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4280 Now a vIOMMU holds a shareable nesting parent HWPT. So, it can act like that nesting parent HWPT to allocate a nested HWPT. Support that in the IOMMU_HWPT_ALLOC ioctl handler, and update its kdoc. Also, add an iommufd_hwpt_nested_alloc_for_viommu helper to allocate a nested HWPT for a vIOMMU object. Since a vIOMMU object holds the parent hwpt's refcount already, increase the refcount of the vIOMMU only. Signed-off-by: Nicolin Chen --- drivers/iommu/iommufd/iommufd_private.h | 1 + include/uapi/linux/iommufd.h | 14 ++--- drivers/iommu/iommufd/hw_pagetable.c | 69 ++++++++++++++++++++++++- 3 files changed, 77 insertions(+), 7 deletions(-) diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 9adf8d616796..8c9ab35eaea5 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -288,6 +288,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 d1c99285eda0..f835ccf4a494 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,13 @@ 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. The device + * via @dev_id and the vIOMMU via @pt_id must be associated to the same IOMMU + * instance. * * 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 diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index d06bf6e6c19f..5314cd486ddb 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -57,7 +57,10 @@ void iommufd_hwpt_nested_destroy(struct iommufd_object *obj) container_of(obj, struct iommufd_hwpt_nested, common.obj); __iommufd_hwpt_destroy(&hwpt_nested->common); - refcount_dec(&hwpt_nested->parent->common.obj.users); + if (hwpt_nested->viommu) + refcount_dec(&hwpt_nested->viommu->obj.users); + else + refcount_dec(&hwpt_nested->parent->common.obj.users); } void iommufd_hwpt_nested_abort(struct iommufd_object *obj) @@ -260,6 +263,54 @@ iommufd_hwpt_nested_alloc(struct iommufd_ctx *ictx, return ERR_PTR(rc); } +/** + * iommufd_hwpt_nested_alloc_for_viommu() - Get a hwpt_nested for a vIOMMU + * @viommu: vIOMMU ojbect to associate the hwpt_nested/domain with + * @user_data: user_data pointer. Must be valid + * + * Allocate a new IOMMU_DOMAIN_NESTED for a vIOMMU and return it as a NESTED + * hw_pagetable. + */ +static struct iommufd_hwpt_nested * +iommufd_hwpt_nested_alloc_for_viommu(struct iommufd_viommu *viommu, + const struct iommu_user_data *user_data) +{ + struct iommufd_hwpt_nested *hwpt_nested; + struct iommufd_hw_pagetable *hwpt; + int rc; + + if (!viommu->ops || !viommu->ops->domain_alloc_nested) + return ERR_PTR(-EOPNOTSUPP); + + hwpt_nested = __iommufd_object_alloc( + viommu->ictx, hwpt_nested, IOMMUFD_OBJ_HWPT_NESTED, common.obj); + if (IS_ERR(hwpt_nested)) + return ERR_CAST(hwpt_nested); + hwpt = &hwpt_nested->common; + + hwpt_nested->viommu = viommu; + hwpt_nested->parent = viommu->hwpt; + refcount_inc(&viommu->obj.users); + + hwpt->domain = viommu->ops->domain_alloc_nested(viommu, user_data); + if (IS_ERR(hwpt->domain)) { + rc = PTR_ERR(hwpt->domain); + hwpt->domain = NULL; + goto out_abort; + } + hwpt->domain->owner = viommu->iommu_dev->ops; + + if (WARN_ON_ONCE(hwpt->domain->type != IOMMU_DOMAIN_NESTED)) { + rc = -EINVAL; + goto out_abort; + } + return hwpt_nested; + +out_abort: + iommufd_object_abort_and_destroy(viommu->ictx, &hwpt->obj); + return ERR_PTR(rc); +} + int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) { struct iommu_hwpt_alloc *cmd = ucmd->cmd; @@ -316,6 +367,22 @@ 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); + if (viommu->iommu_dev != __iommu_get_iommu_dev(idev->dev)) { + rc = -EINVAL; + goto out_unlock; + } + hwpt_nested = iommufd_hwpt_nested_alloc_for_viommu(viommu, + &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; From patchwork Tue Oct 22 00:19:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 837954 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2081.outbound.protection.outlook.com [40.107.220.81]) (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 8FB4B17993; Tue, 22 Oct 2024 00:20:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.220.81 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729556436; cv=fail; b=gKVMBdBT2rmgCXlRv9u/p+jR5StCBZjgFP6FxF2AL+i0o4wkefLJvAlMlIllJJj4+MZX2IvzWLGaLDHnKB03XNuEMJ/twbHi7PKkZDTU+dIM011ssq40qxa+K1pJGzN8bClE5Y8BaLuv2M+Ox4Sx03fOmtqZE2xzMZJhTbyI2Jc= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729556436; c=relaxed/simple; bh=6TCHv/bumDCJgyHu0k2K//unc0BlMbXWMuOAWeX4NqU=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=uzqugJeC0Kc7luUYDT/YBJml22Xv4SozK2kugN7llPZxquk4wH+Ir6wLSfAOn1tsnqmvHs2qCs/alG44fOY0J5HIBjDNaGLpNCHUJPAhgEe4HSZ8yRdq25sUmBikjKEcWcl9JNr96C6W6rE+dIrSDOnbHBQeZOE9etd4cYxH3J4= 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=CUc+VHTh; arc=fail smtp.client-ip=40.107.220.81 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="CUc+VHTh" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=WGplvtHd6lFSpoHwyK0CSIgDZLtVKSAkMqmo36OkWZrZURjTB21RBMPCiDLnkGFOCmvQZEa9y2+567DRQzrlsojJcUIkWpM7RR0SfNuM2TOshN29DB1FcafvRYV8TpCU8dSWfY9JLTwe39A1DiiajMr3C73WtawPv4FtXhwInQIP5l2FcF0QFuy1i0pUWpmRmYq7Ha6pF0jvIOA8BjnXrUcnXVCVySKfTyaBOTZgbVCaTux+RHfsE6GFJ3Ke8Xvv4jpVa3YuDNCk6YyZm11+dxATqfTzB9fkF6DwGyKuq1LsSxGbRqZilchoq5CLVP8fO4wKPjQS74FeDuMeKJx/3A== 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=f/hOxjPXfh6nod+qLqLb4dDI2P5tGR5GF7bgkJhIi9U=; b=THDiVOHQv5F6g1UM1XYHu3bABe8qZDMr0D2PDG0YCglX1eSOqvG44xxrjzWBMZPWp/7O3NMslcJGK3LKp3xjEsoActR+3hH5eewe6+djC2ioxZF1UpVocHUiZJ5vRKCZOgWVFOvx3O7fqDnb0NESj+p2eMytYPT6QnGOuQjqSmfgdRSUNln8IohCyMeroFLF2raOfV8XseHBXGOaTGLW4rEFC41Cs9upj7sN1UNpH0d3fb3w/aSp846/8GHmGvcsI/xu1oFupB/UA8QET/LF/fdO2ukMYGERBAKfh5EWfN8bvsMrAWNOtX1PfAgIWREuDZfwcsawxBDp4N+xoJe82g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) 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=f/hOxjPXfh6nod+qLqLb4dDI2P5tGR5GF7bgkJhIi9U=; b=CUc+VHThVT1Rmrb00kyWhfhqmTUxXSJ1206SKFCc/kVJLf6EAxZTpUxBLqke2DZOfZ0p25Cx0WY2OTDFLusoBY9yEpkJvGnDKRnShAjqx+rr5thMAAGcQ+a9OMQSgzZXiTgj0o6AJoS3lHikyeE14yI9AmDIc5/nVCbG7arj8AeulMr4ReUs4/2aEKh/LnjZMYD6lX5pK++rVu0n6Wt9LtLOj/KS7JhnByQkeAxzbI1qHsX/KWMV2FJ3E0SStP4QmOpuZLsblI2KVjgLFdJh3lqQcPExVJWVbbYw0YmL5J2H0DXJy0OXj8tbRNa+vCksjHfcPJ+/6hEebtRFaIdJiQ== Received: from BN0PR08CA0029.namprd08.prod.outlook.com (2603:10b6:408:142::15) by PH7PR12MB8105.namprd12.prod.outlook.com (2603:10b6:510:2b7::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.28; Tue, 22 Oct 2024 00:20:28 +0000 Received: from BL02EPF0001A108.namprd05.prod.outlook.com (2603:10b6:408:142:cafe::20) by BN0PR08CA0029.outlook.office365.com (2603:10b6:408:142::15) 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:28 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) 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.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by BL02EPF0001A108.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.8093.14 via Frontend Transport; Tue, 22 Oct 2024 00:20:28 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) by mail.nvidia.com (10.129.200.66) 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:10 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail202.nvidia.com (10.129.68.7) 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:10 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Mon, 21 Oct 2024 17:20:08 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , , , , Subject: [PATCH v4 07/11] iommufd/selftest: Add refcount to mock_iommu_device Date: Mon, 21 Oct 2024 17:19:29 -0700 Message-ID: <3e03b60ba01ef48d1f10fc41caea0dd9649d19ab.1729553811.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: BL02EPF0001A108:EE_|PH7PR12MB8105:EE_ X-MS-Office365-Filtering-Correlation-Id: 9dba0f30-868a-48b6-186b-08dcf22f54f6 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: i1UNals/xnbOJ5T/TTZRQieOv535bfmYSc0XV/JXh7hGyb0Kyb2a+omSgbkkenj/oaLV24EMsPQ8jyYEZsGBjHx8+cP2jgUmqYruRXZl00bI8oEZJa9VLEPEdgnTcIq0wlhIEPezqn83MqOV0Omy1UDc0LkZN0OM5YfR+8Yelj+qNmOEIh3s87fb6OyjmqfUS/XIW4DXqB4MBpcqzJCbvhQ+EKaSzpwwQ01GV5FvnSSSVFkfra4GhF0D7T+qehUuYE70QSZXpTlazUzGeIBiXraNhwQ4Y0UHL+Lp/rPVerluZYk45KKSAAn8YIeffK0ixLKCtXBlZcatsWhJlQfuPX8cpHZiGZs9r03nWxdhystMzCEsbMvusZKuoF0Nj7xS+izoHxrSrsLNpXp2PKRZiAqNuTOJONVZAPklEwl6atHUvDNYJVfxxd81629mGBOqXg0XkcNpUnxpK5NlGEKlJU5SomFAOmOL83BqZmvyxS6blVVIjOSx07uCA5l30njtv0tzGQdINqoeZHkVUoemH6Bg/NzCRva7uUFK3jsX945kwjmTSRK09+AaqdhlIJtWUardZ174JCqnZo5dBZ3XnfnFQFBwV229xVh5YbthSFB86/g4QEMeQit+MzBKB2+HuEBurGsHamMNX/t0+C+1BGKp0IBe6b8uemck2JVIxdfHNEPnGpedac2HssQZcpGCZCrjNnS33s3iCBs/qiECITk131OVzVJVgV7w1wPKK5xY5cGyMy1iqAaULddS/TQDcz+/Zgmmzl/HzmFkgRI+cNZTrbdP5FbgR+alxuJ8BKHC+nO1k3MdAjDnwdZxhbTRUXMLJzbb0BQu2RU9/r7a2EM8irN87jK9lAGAArtt1bsQynhZLhm7wgmnEEimSECy7ieGCtlUriTpFQIlUO3inDNsKuO+9PITZGEEAvpI5NS5znUO54ZKREebaTjEvafw6WJoQulVsDg/ZfuHAf3g6gwH+07zOQLmj5WisCayGQYOTFBUyVm1nhuvljY9CRLEe2Lle4khEdTYjueJx1CnnB7g1aLT3oC8/0k4oPywxoE3dxv/DqVC5AKB4Dlt8ZntRjNERZgcdpjTu/RA3bwXNzRVoFiP0ZDhIdGk245Q5PCB8Y3zZFmtHBOxWjqIs5GX5iK6TwQ7GMgUTRp+/3pWZnthGCYHISbJ4C4ZjnSoqlBKoXvVUziem3s1l//GCNhTMHMS8UPgD2zsYIdErBHSKkIaMhiB0J51ivpppyCmJuPIEhfErAIcIstaqZkNcMb85zM3Eet8TZx+tsPiR2ojSHUlkmQItM5mkYBY7pvQeKpptEgFi5Sqdr18Lp+buR8MoSE+N3zbISdzj85Qqqi1YRi8p3cJHFixBHYJb9B3IpGdWVfGMhMK5VCCwQhEidm/gk1h2WMS5qAilRnM+TYNIg== X-Forefront-Antispam-Report: CIP:216.228.117.160; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge1.nvidia.com; CAT:NONE; SFS:(13230040)(36860700013)(7416014)(376014)(82310400026)(1800799024); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2024 00:20:28.0535 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9dba0f30-868a-48b6-186b-08dcf22f54f6 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.160]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF0001A108.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB8105 For an iommu_dev that can unplug (so far only this selftest does so), the viommu->iommu_dev pointer has no guarantee of its life cycle after it is copied from the idev->dev->iommu->iommu_dev. Track the user count of the iommu_dev. Postpone the exit routine using a completion, if refcount is unbalanced. The refcount inc/dec will be added in the following patch. Signed-off-by: Nicolin Chen --- drivers/iommu/iommufd/selftest.c | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index 540437be168a..dbd2a78c1074 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -507,14 +507,17 @@ static bool mock_domain_capable(struct device *dev, enum iommu_cap cap) static struct iopf_queue *mock_iommu_iopf_queue; -static struct iommu_device mock_iommu_device = { -}; +static struct mock_iommu_device { + struct iommu_device iommu_dev; + struct completion complete; + refcount_t users; +} mock_iommu; static struct iommu_device *mock_probe_device(struct device *dev) { if (dev->bus != &iommufd_mock_bus_type.bus) return ERR_PTR(-ENODEV); - return &mock_iommu_device; + return &mock_iommu.iommu_dev; } static void mock_domain_page_response(struct device *dev, struct iopf_fault *evt, @@ -1536,24 +1539,27 @@ int __init iommufd_test_init(void) if (rc) goto err_platform; - rc = iommu_device_sysfs_add(&mock_iommu_device, + rc = iommu_device_sysfs_add(&mock_iommu.iommu_dev, &selftest_iommu_dev->dev, NULL, "%s", dev_name(&selftest_iommu_dev->dev)); if (rc) goto err_bus; - rc = iommu_device_register_bus(&mock_iommu_device, &mock_ops, + rc = iommu_device_register_bus(&mock_iommu.iommu_dev, &mock_ops, &iommufd_mock_bus_type.bus, &iommufd_mock_bus_type.nb); if (rc) goto err_sysfs; + refcount_set(&mock_iommu.users, 1); + init_completion(&mock_iommu.complete); + mock_iommu_iopf_queue = iopf_queue_alloc("mock-iopfq"); return 0; err_sysfs: - iommu_device_sysfs_remove(&mock_iommu_device); + iommu_device_sysfs_remove(&mock_iommu.iommu_dev); err_bus: bus_unregister(&iommufd_mock_bus_type.bus); err_platform: @@ -1563,6 +1569,15 @@ int __init iommufd_test_init(void) return rc; } +static void iommufd_test_wait_for_users(void) +{ + if (refcount_dec_and_test(&mock_iommu.users)) + return; + /* Time out waiting for iommu device user count to become 0 */ + WARN_ON(!wait_for_completion_timeout(&mock_iommu.complete, + msecs_to_jiffies(10000))); +} + void iommufd_test_exit(void) { if (mock_iommu_iopf_queue) { @@ -1570,8 +1585,9 @@ void iommufd_test_exit(void) mock_iommu_iopf_queue = NULL; } - iommu_device_sysfs_remove(&mock_iommu_device); - iommu_device_unregister_bus(&mock_iommu_device, + iommufd_test_wait_for_users(); + iommu_device_sysfs_remove(&mock_iommu.iommu_dev); + iommu_device_unregister_bus(&mock_iommu.iommu_dev, &iommufd_mock_bus_type.bus, &iommufd_mock_bus_type.nb); bus_unregister(&iommufd_mock_bus_type.bus); From patchwork Tue Oct 22 00:19:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 837691 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2046.outbound.protection.outlook.com [40.107.94.46]) (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 0005313AD03; Tue, 22 Oct 2024 00:20:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.94.46 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729556435; cv=fail; b=ROasHfHrox/vRpratt2kArnFn40qgMg0SNusgrknZQyEfnxWVroKbnavOt28wec6MT6DLQvPL7oqLO+Tr5s1Qe89zRMLrCFluJULDl0/e54cis34rNMRhz2NLktT0DjW1Tx/n9HRk8sU2Xejmbl9xF90QEdUSmpARRuga/Idx1U= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729556435; c=relaxed/simple; bh=uuGzRXZGIR1348PzemGxHzk/Sf/Zw9Pg41eEpvcVkZs=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=FgINJmHK+uoSwflrtTorK1pThOhVqcy6Ejg2z2vHTqru/hpdgO3XZv1Wy2mmRI0yN059Hip5uF75gE1OGIgTqDAeqkJcoDubzmDiN2gLC0jO1undY6LtbgyITrupuQLxh1RaFNJhI8ONIERcvq3h+9+22N22c9aIeR3Ntyt2j8I= 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=TUQ7ttPM; arc=fail smtp.client-ip=40.107.94.46 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="TUQ7ttPM" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=EvaEpxUGyqivCzfRqHTEj6evwE+u1IY4F+woT0HvwwFYavumRAig6LM4R5RbIMZOuUFcawq36fnSIQcTsMmLqRcAYQbb1bhkPoOkwAJyxFqakqW/6NZuD4SqBFSdaXEibmT4TOfrS1b7mDgQTUQtKx58KckM/+aPXyP9PuV/HJVT6PZdkzAy2SkpMkwVf6rEvGD81alSCj+UmBOyuhMGJnyofpMRIKj+Jzi6WLGKYBjAm3Uze7Y9w6l4GoBRk1veREyP6qfCNBXx0TVRA28HZ1B7f0AMaiTD/AmdSlpPyc7nUY2Zxl3MvsnFLB19C6Gat6SZjC8F+mSBjcJ2BDxSgQ== 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=ppccpeyy1L2Rk3cWFAMsyZahdwqz/tSFTOoVMVOcQpI=; b=ot/5/DIiC5T2gg9w8YYubvwvEcjgDLcCRK7Svo2VwZDNU8ItFanG4PjXsQ2EhcNZ9vpwsb4OXyHwmE+ZMSO9Hes3/MbsEXXJ40QZQpc1nMWq+oDKzwG+/UKxpcomOiMxPV5lbHeYp33F9X+oJEwDnvGvybSJsv8N9VOjJELhWoXn1axuGZWM9Rlk08urVNm2N91qC0Ert4WlZIunbijcLCmqW4DJ03R4tUzLK2Jh/cLDJwlPwEtuBPPjYpvNAlcE4InaLiYKMAg9m7pPKlQi1gkZR/kp65aXGQ4K9cx6N/qpQ6AEwpISTOj4/BpQsd5YVFJqxRXDb6Tm5wd6Uar0hg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) 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=ppccpeyy1L2Rk3cWFAMsyZahdwqz/tSFTOoVMVOcQpI=; b=TUQ7ttPM3JNVX1sHLfBM20kpeENvTsjPpDAD2Uray30YVlZ+/leB1+AgyQHkJ95VBrlePvboOKobnHL7CCtbK210ogCrwat4yekDTT75GLQC93rKlTpMUoOSpK2gvC9GyUSTzSAbiFn0a34TYB7omor/ZM1nzjKqg8+iZ8du4CkVYhNx2E6vBXFY+BCbCsoEts+mgYBPsLj/Cd63J0FgTc9FrWIxfwYlo1zgNgzikG6ukwNLuGZ2xZ0RRpWQtY9HJNpeI0ugFtuKFDj6MLBZMwPQQhCzz573K6aFJeL2G61H7dJNy60SURXErAcHK1KZa999rmO6NUkA6sxPca5+4Q== Received: from BL1P222CA0002.NAMP222.PROD.OUTLOOK.COM (2603:10b6:208:2c7::7) by IA0PR12MB8713.namprd12.prod.outlook.com (2603:10b6:208:48e::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8048.16; Tue, 22 Oct 2024 00:20:30 +0000 Received: from BL02EPF0001A101.namprd05.prod.outlook.com (2603:10b6:208:2c7:cafe::58) by BL1P222CA0002.outlook.office365.com (2603:10b6:208:2c7::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.28 via Frontend Transport; Tue, 22 Oct 2024 00:20:30 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) 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.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by BL02EPF0001A101.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.8093.14 via Frontend Transport; Tue, 22 Oct 2024 00:20:29 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) by mail.nvidia.com (10.129.200.66) 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:11 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail203.nvidia.com (10.129.68.9) 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:11 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Mon, 21 Oct 2024 17:20:10 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , , , , Subject: [PATCH v4 08/11] iommufd/selftest: Add IOMMU_VIOMMU_TYPE_SELFTEST Date: Mon, 21 Oct 2024 17:19:30 -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: BL02EPF0001A101:EE_|IA0PR12MB8713:EE_ X-MS-Office365-Filtering-Correlation-Id: b4b90187-1ce9-4e88-92ea-08dcf22f560c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|1800799024|376014|36860700013|7416014|82310400026; X-Microsoft-Antispam-Message-Info: P/QA11pKMOGDu9e5cOekM7XMbFc2f9tVJSj3MnTDwjJdIeJf6UpIfrJX3Syv1DUS/2gLd2Cox0Vahka4xXEqpu0lI/Laqi6sEC+0S0B/i2pd+bfNVJNfL9JlRbJTHu/NRcNUKTPMBlLAIsZqL3C3XaI2A4Or77XdzzKXjvpMUV+0JHNis+1zhsUgpItS5jIXVeF4pBvlKygCdmYVnYJlgLjuHJAM0IaskH1qOaXtjnKqc7siftKbsU56Xz20s0YPdI/Y81eK3fOzepuBfqxmwkrI1cLQF31qW9spPxQR71mBygl+AXl9USBU5nsUp2ZB7Py3AaH+hr7RMGqHdQz3JrPm5MWxQA03JwrXZmCBDRAoXc+OyZZkYnNCq3kztkA/sOFQEOIObWEkecH7D4laTqa8pGY56YEh9HP9Mi8gosMbX+rlQ5l/mK0ZZFgpwtYGtcGify4XhFpf8dko0pZ0V3Sf1jNGrmNO3IKsyfqW3S2aZPx8dGegPDNPJ5v1qBwab/yvmaQSdefUyokHoLCp4Wd4+fF61+0TZ4258pzFBBsGVfqToApAKJjvC1CHi0dvxqGD+7WU3XRlYcWQQJ/lgSjwmpn5sZw9qSqFB3wsPwpiz40sPZISF5H4Ac9rDgI6bsiM8nIt3tSI1jamvTQ9pcIrWGf4mzRb6wek2Ci/p9LPA/ZWEXmcf4dcpz7LYIjEcnD/GGkKny0NxFmQkRpgrDsTT67MzllsEHoWPtVERm+w/Ifb7rrezJ5vHKTuAHrJcJuW1CMk5Yn7TBdlxeronRuK98IGDMqk1Nza5kE2F7ihfXi8LyEqFb71GNwGPxN6DAZmIBqiVpCY0qQSm8EsoVL7J6XDhEtaqPCKinrSaUSJb22Y+LJQviQVWAvrowze+W5juOcvaEli6+rataEMk9gBcmflSylEZO6uCp/Vn/K1mwI3MpYHavGaf+0Oz1jTIhZohW0pQKxdum/snSTTfnWWRCyqAZzZcIQym4m7zSGtgbIvkvXZdZIWRd0irdKLp2ZmqsdNnwsNrvmzSn+mEru176m2ZjBxe2qGRkY1NuZ6OJ3tUc4BPBznIqifZKedbaNrWEq0TFXnIiJUcBQAlff0mMUwBHTumrl6wknUlQavLe9FSmV0O1XHsH06/MdcftxYyk938O4UGPUefO0lWBNHiZaD/CiUiiQUAg5vspg+UUTLzEJmqQP97nL7lvtKwkkX9oRaZkVhQ8S0wtejfpLIEtkiD15C6eermIVrlFZjwIiBv2W0nm1TzHuV0NNvZPdBSiLNiqBxOFf43LZIPk9Ohs2gwsV39fY2TZPciMG6c4lGi1ETX4GeAfkyx8G/HtOhmLYGFrcqXPLZedFPIGSLVoVt/nMdaL2nxBFA4MgdoTS5U30vimKjlpvQYBYNn9Pfm4OCEI4enEFyOVlAoQ== X-Forefront-Antispam-Report: CIP:216.228.117.160; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge1.nvidia.com; CAT:NONE; SFS:(13230040)(1800799024)(376014)(36860700013)(7416014)(82310400026); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2024 00:20:29.8629 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b4b90187-1ce9-4e88-92ea-08dcf22f560c 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.160]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF0001A101.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB8713 Implement the viommu alloc/free functions to increase/reduce refcount of its dependent mock iommu device. User space can verify this loop via the IOMMU_VIOMMU_TYPE_SELFTEST. Signed-off-by: Nicolin Chen --- drivers/iommu/iommufd/iommufd_test.h | 2 ++ drivers/iommu/iommufd/selftest.c | 41 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index f4bc23a92f9a..edced4ac7cd3 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -180,4 +180,6 @@ struct iommu_hwpt_invalidate_selftest { __u32 iotlb_id; }; +#define IOMMU_VIOMMU_TYPE_SELFTEST 0xdeadbeef + #endif diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index dbd2a78c1074..04dd95fe24ca 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -132,6 +132,10 @@ struct mock_iommu_domain_nested { u32 iotlb[MOCK_NESTED_DOMAIN_IOTLB_NUM]; }; +struct mock_viommu { + struct iommufd_viommu core; +}; + enum selftest_obj_type { TYPE_IDEV, }; @@ -543,6 +547,42 @@ static int mock_dev_disable_feat(struct device *dev, enum iommu_dev_features fea return 0; } +static void mock_viommu_free(struct iommufd_viommu *viommu) +{ + struct mock_iommu_device *mock_iommu = container_of( + viommu->iommu_dev, struct mock_iommu_device, iommu_dev); + + if (refcount_dec_and_test(&mock_iommu->users)) + complete(&mock_iommu->complete); + + /* iommufd core frees mock_viommu and viommu */ +} + +static struct iommufd_viommu_ops mock_viommu_ops = { + .free = mock_viommu_free, +}; + +static struct iommufd_viommu *mock_viommu_alloc(struct device *dev, + struct iommu_domain *domain, + struct iommufd_ctx *ictx, + unsigned int viommu_type) +{ + struct mock_iommu_device *mock_iommu = + iommu_get_iommu_dev(dev, struct mock_iommu_device, iommu_dev); + struct mock_viommu *mock_viommu; + + if (viommu_type != IOMMU_VIOMMU_TYPE_SELFTEST) + return ERR_PTR(-EOPNOTSUPP); + + mock_viommu = + iommufd_viommu_alloc(ictx, mock_viommu, core, &mock_viommu_ops); + if (IS_ERR(mock_viommu)) + return ERR_CAST(mock_viommu); + + refcount_inc(&mock_iommu->users); + return &mock_viommu->core; +} + static const struct iommu_ops mock_ops = { /* * IOMMU_DOMAIN_BLOCKED cannot be returned from def_domain_type() @@ -562,6 +602,7 @@ static const struct iommu_ops mock_ops = { .dev_enable_feat = mock_dev_enable_feat, .dev_disable_feat = mock_dev_disable_feat, .user_pasid_table = true, + .viommu_alloc = mock_viommu_alloc, .default_domain_ops = &(struct iommu_domain_ops){ .free = mock_domain_free, From patchwork Tue Oct 22 00:19:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 837953 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2060.outbound.protection.outlook.com [40.107.95.60]) (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 6FFE713B79F; Tue, 22 Oct 2024 00:20:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.95.60 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729556437; cv=fail; b=AZdASZ9wlWBfTMMhGJzblqgimcV/xaU1BspwRDZ4NL1xmW6YjHBu6nZFhBoePjy8NwzHuowKLHuI55ChkRtOM4G1HySYUuD+XzuKri8pyBaHO8lqLGJzoZi4EJg2k0cCuGncMJtSJ8TJB8BE+8npo51nzpuP1BNz5qG60mng9Ug= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729556437; c=relaxed/simple; bh=z/eaGSovBxvm4gvITyZo4wu91uL8a9SiQWDgai6aGUM=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=NXRTW8Q1zXXITYe0yplxI01ak1zZNj2B3FVHPaNQzLjnJRj5W7C4mrrOpOvaxnoHJTB74iGVT8zq1DJntIviakq8ES+41ewX4ETeC+XprWRMZ950QHX5YLSsDGtqH6xd+n3EEXVjdsIVVeTuG030R2Fwr42g8z0PTQsPyXW0ReE= 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=UiEsvnVU; arc=fail smtp.client-ip=40.107.95.60 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="UiEsvnVU" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=SdrdYZZVFxKuHkRPxRDgQplBsOYg80s0GDrbg0qzASGq+ewHvEHUyT51GitTZkkcqRnyns1e6qdXgfrXra3gNlvVKXdg7N2y8SeaY0I/9p24rDOGwKtk7X0+EQrBwcWlEHQWz/2RnlGrX9xJZ+/UiGNB1T35pYj9YOKVadqGipgej1UOFAg0UyCF/1A825DnbBRTSXVuRcV5k7W4hq6YJn1ylzRXuFiUiCv++u0/XL7lVJHS+Ji1vAhCLuw+l4vwVpJuXcKBSqobrG96FNFRHHt9OJQat8Sx1qpaATFkOTRrAP4xnpywWFDg/t7N0a2xFkW5Xxr4u52Jq+QwUMc6pw== 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=pv4VPS3l4q0xEzuM9E2RNzoeDh1ThJTm3rrynPATA+I=; b=MLO349mJHUnZIoQIoQe6oAm2Kn33JXIKgWZrM7hY7Bon/APG9Y3LkycB/32urMOApZIWYxKjWbDkuC7OXvwa137CxTL+0KAul6xSfuyvT/+3Cz0DVfVoYk4hzVHx2kRXqXKo8xKUve6LAr66v3prxzr3p74nExVEaFslFpm24QOFJCF7PN4jfdAXvTzb0eoZgIkNLuZD8akKb44tTI7p+ehXWVye25tpWIP3xzhKKPUwNrptZZrvJwBvR2656XY2Snrw9o1l7WkU+0bkbQH8/2h5p91KuNO4cDrccyL5XVVLQ91gicUTGFjnCjNrkGlLVbs8GrBEOArFbsn8LsSNbQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) 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=pv4VPS3l4q0xEzuM9E2RNzoeDh1ThJTm3rrynPATA+I=; b=UiEsvnVUHZgqdqaslOMpKyZ4zEEp4o5tmVqG5a35CKcDCODIIF3RxHXC6rdorjO473rEmARNn5dWYQZ/onPe4aboKi/n7SSyYwjewyynvU8x07sRI2S70JASC1SGMemZSgfMYNzqs2YOYhJTOndwLC4NGmNIS/BDPzrrzidXhddfCa47x9yo/aR/xQhYNk4DwWDOUW353J7sqFqHLtaD1/mgGuRKjiabkvg9+E2yHGvwpOMpbT2EYpwE/8Nzk/sDgNdLa1z5x25IAueyLe4IBpU7z6LiOaXPLtaMz/QpsNCn6LnqPNWWRI1aPIphF8Ghx0TNsAL28WIHukea69FW6Q== Received: from BN0PR08CA0001.namprd08.prod.outlook.com (2603:10b6:408:142::6) by BY5PR12MB4324.namprd12.prod.outlook.com (2603:10b6:a03:209::10) 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:30 +0000 Received: from BL02EPF0001A108.namprd05.prod.outlook.com (2603:10b6:408:142:cafe::55) by BN0PR08CA0001.outlook.office365.com (2603:10b6:408:142::6) 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:30 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) 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.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by BL02EPF0001A108.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.8093.14 via Frontend Transport; Tue, 22 Oct 2024 00:20:30 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) by mail.nvidia.com (10.129.200.66) 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:13 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail202.nvidia.com (10.129.68.7) 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:12 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Mon, 21 Oct 2024 17:20:11 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , , , , Subject: [PATCH v4 09/11] iommufd/selftest: Add IOMMU_VIOMMU_ALLOC test coverage Date: Mon, 21 Oct 2024 17:19:31 -0700 Message-ID: <47e6077c79b9bf97476aa91317d35af2e8974240.1729553811.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: BL02EPF0001A108:EE_|BY5PR12MB4324:EE_ X-MS-Office365-Filtering-Correlation-Id: c76aa39f-781d-49b4-6cb6-08dcf22f564d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|376014|36860700013|7416014|1800799024|82310400026; X-Microsoft-Antispam-Message-Info: Tbg6d2CBLXZKUVqwkizu2Nzja9CTQ/1H7GUMS4MFB7g/q/ggyJXQCIo+ZtK0cUPEgjAzVNDslnhJZOcU2UF1Blb745Olxp4+/fpPWUQ5Jc/b7pHkTNJykdYlT+eWkq3Ac4gAZSW9pTvXf9ZQ1E0uzlUJXdt47MADMlpcyNAWKRsFHP3NUESgpCodUFIA1/m979lgxP6j3wad3KfLzgc/Hm8MFE4Uo079Ov/J9Jr2NFOTtZNOz8grvZXCBg7RHUUXdkb7cSqMwiNQ/IAHTIlL8KBlfj2LVU1rVpkgwTlcHkONA0Zj9wG2XsyRCnSQJcw//pFtDVayfacdBofNymG5LGhSXQQAxSb4vSLABbrBfOizDRtwsA+w2ljGIwXQ50U52JhGoZq+2tdJLlBEM9mE+yyMvMkmI0xXNiHA2kFeAjeDjHY8B6s/6wGtPjTGem5gyzl+lXBGjwIqfnnmb4YmTZp9Mn7Di60QXXIjQ0PLJNLVvFZ35PXzS2aJwuTUmM9iHTZdxfo6ABp3qLikQrE3tIHWmX4o9pPSuwetGu/6u5ZEEjQaraz2wkZlfl+HLz5RXuhLeZFylgouMsFfsJXFiiOu2JvsVqVMRNdfj09xS2c0l8EIhi5NXe3rrEuW3Nh5rxK8An8rciaOr9HBEhw61rjS6fL+w9bKJOwASq4D29tWtgye7QUYZKxx6/aHDej8FgQ4nwq9fSx6DVGY9P2V9O1+uhJ1calqf0cYQRjaMS0BC5uQ+ae7E4VVl38+z/F1mf4zakqH6Z3ogb/BX40fMKVKFtXfpr5WBY9TNeu+yz5wzRuOaAoP90pa8PM+MZBh8yvxa+otwnnFwBy+icfj/4hnB8si5Sr2g75I1D9LxECAv/wyM6Cs3kh2vUso58Q8ty8l0QxhLOK0YKZboC8MFWh19O6IUeNB3ptUKp29o2fpsrTklYevti8fQIQZVA4jqyg03gqZHH/eWq/dMxKQ7v9wYOUDylUmiqEU2ScOdAtGxhyR32JoTT5Wk32QW3lDonwSpBb167FiTS+YxuM0DsQNlxGYrV1cZhTaOp3kowPXWkgHvq2tHUEv/W7UWkc6Sgi4ZPujXz8arRZ1y4jjU/x7E11L5nxxhyndhzEvSMjjkzMGjmRvAxo3m1ikUGyeTREjZ9pSPso3eaB1vCBdeOrNsyy9DlcOlYsza75+u/Fk4l2cEuj+M5t69ZtpV3bNVn9LjEBEllLz4mjIt1abc54LN4VNj5YGdmeUVv1yD/moPxBHupvd/ZPOZWb8zB/IYr/Vf+Q5f+XZBRsn9bviEyeZGXfDPp74M9jFr/iNpbhnPArtZio8TO9vz3EPByXhSOIrAOV3KnXnlq7do5oJkmlewJ0aWX9kxZ4x6DJJRQwcTF7yjDnpRxJVqGB90vsD3RdVdu9j8njj8QpwC2LkxQ== X-Forefront-Antispam-Report: CIP:216.228.117.160; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge1.nvidia.com; CAT:NONE; SFS:(13230040)(376014)(36860700013)(7416014)(1800799024)(82310400026); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2024 00:20:30.3035 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c76aa39f-781d-49b4-6cb6-08dcf22f564d 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.160]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL02EPF0001A108.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4324 Add a new iommufd_viommu FIXTURE and setup it up with a vIOMMU object. Any new vIOMMU feature will be added as a TEST_F under that. Signed-off-by: Nicolin Chen --- tools/testing/selftests/iommu/iommufd_utils.h | 28 +++++++ tools/testing/selftests/iommu/iommufd.c | 78 +++++++++++++++++++ .../selftests/iommu/iommufd_fail_nth.c | 11 +++ 3 files changed, 117 insertions(+) diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index 40f6f14ce136..ca09308dad6a 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)) diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index 4927b9add5ad..49ac144cf5a4 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -128,6 +128,7 @@ TEST_F(iommufd, cmd_length) TEST_LENGTH(iommu_ioas_unmap, IOMMU_IOAS_UNMAP, length); TEST_LENGTH(iommu_option, IOMMU_OPTION, val64); TEST_LENGTH(iommu_vfio_ioas, IOMMU_VFIO_IOAS, __reserved); + TEST_LENGTH(iommu_viommu_alloc, IOMMU_VIOMMU_ALLOC, out_viommu_id); #undef TEST_LENGTH } @@ -2386,4 +2387,81 @@ TEST_F(vfio_compat_mock_domain, huge_map) } } +FIXTURE(iommufd_viommu) +{ + int fd; + uint32_t ioas_id; + uint32_t stdev_id; + uint32_t hwpt_id; + uint32_t device_id; + uint32_t viommu_id; +}; + +FIXTURE_VARIANT(iommufd_viommu) +{ + unsigned int viommu; +}; + +FIXTURE_SETUP(iommufd_viommu) +{ + self->fd = open("/dev/iommu", O_RDWR); + ASSERT_NE(-1, self->fd); + test_ioctl_ioas_alloc(&self->ioas_id); + test_ioctl_set_default_memory_limit(); + + if (variant->viommu) { + test_cmd_mock_domain(self->ioas_id, &self->stdev_id, NULL, + &self->device_id); + + /* Negative test -- invalid hwpt */ + test_err_viommu_alloc(ENOENT, self->device_id, self->hwpt_id, + IOMMU_VIOMMU_TYPE_SELFTEST, NULL); + + /* Negative test -- not a nesting parent hwpt */ + test_cmd_hwpt_alloc(self->device_id, self->ioas_id, 0, + &self->hwpt_id); + test_err_viommu_alloc(EINVAL, self->device_id, self->hwpt_id, + IOMMU_VIOMMU_TYPE_SELFTEST, NULL); + test_ioctl_destroy(self->hwpt_id); + + /* Allocate a nesting parent HWP */ + test_cmd_hwpt_alloc(self->device_id, self->ioas_id, + IOMMU_HWPT_ALLOC_NEST_PARENT, + &self->hwpt_id); + /* Negative test -- unsupported viommu type */ + test_err_viommu_alloc(EOPNOTSUPP, self->device_id, + self->hwpt_id, 0xdead, NULL); + + test_cmd_viommu_alloc(self->device_id, self->hwpt_id, + IOMMU_VIOMMU_TYPE_SELFTEST, + &self->viommu_id); + } else { + test_err_viommu_alloc(ENOENT, self->device_id, self->hwpt_id, + IOMMU_VIOMMU_TYPE_SELFTEST, NULL); + } +} + +FIXTURE_TEARDOWN(iommufd_viommu) +{ + if (variant->viommu) { + test_ioctl_destroy(self->viommu_id); + test_ioctl_destroy(self->hwpt_id); + } + teardown_iommufd(self->fd, _metadata); +} + +FIXTURE_VARIANT_ADD(iommufd_viommu, no_viommu) +{ + .viommu = 0, +}; + +FIXTURE_VARIANT_ADD(iommufd_viommu, mock_viommu) +{ + .viommu = 1, +}; + +TEST_F(iommufd_viommu, viommu_auto_destroy) +{ +} + TEST_HARNESS_MAIN diff --git a/tools/testing/selftests/iommu/iommufd_fail_nth.c b/tools/testing/selftests/iommu/iommufd_fail_nth.c index c5d5e69452b0..e9a980b7729b 100644 --- a/tools/testing/selftests/iommu/iommufd_fail_nth.c +++ b/tools/testing/selftests/iommu/iommufd_fail_nth.c @@ -582,6 +582,7 @@ TEST_FAIL_NTH(basic_fail_nth, device) uint32_t stdev_id; uint32_t idev_id; uint32_t hwpt_id; + uint32_t viommu_id; __u64 iova; self->fd = open("/dev/iommu", O_RDWR); @@ -624,6 +625,16 @@ TEST_FAIL_NTH(basic_fail_nth, device) if (_test_cmd_mock_domain_replace(self->fd, stdev_id, hwpt_id, NULL)) return -1; + + if (_test_cmd_hwpt_alloc(self->fd, idev_id, ioas_id, 0, + IOMMU_HWPT_ALLOC_NEST_PARENT, &hwpt_id, + IOMMU_HWPT_DATA_NONE, 0, 0)) + return -1; + + if (_test_cmd_viommu_alloc(self->fd, idev_id, hwpt_id, + IOMMU_VIOMMU_TYPE_SELFTEST, 0, &viommu_id)) + return -1; + return 0; } From patchwork Tue Oct 22 00:19:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 837690 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2057.outbound.protection.outlook.com [40.107.236.57]) (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 2753D1BDE6; Tue, 22 Oct 2024 00:20:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.236.57 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729556437; cv=fail; b=J7WkJe/tUQjjOPeuZUMm3U0AoQsqmaZJ6s+GpO3mcUj5sWcg3Kex8kBRy42CyLBZLDS1u2h+dOsdpDByG3jTNTTvyR8tJbH0s0g7+PmzgEsc7Dh/MHUD9/YyZcIAshJGZccDz2R3yVKYde8/GO8CHui+O6+LQJb6JrNueTq97P8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729556437; c=relaxed/simple; bh=bAHQMRu8mLYF9KBY0J9JT4A9MMilnR3viW+FyxgkGlc=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=LaaoP1vkJ2XJs7mgaSXy/wKXMzgPzJO/baF1UuAJWBVtZESCcb1xuTv8FW9Y5GzBtpciP/90ymP/F0uG5k94tc6N5A9jImIuJJ4XoEY2+XeRjV4IH6fQdRsZycVaXlKGpNTdencN8yWt0ZgC/nGof6eEoFHZmE6EsdPTY+yeMZw= 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=tkkg9mwd; arc=fail smtp.client-ip=40.107.236.57 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="tkkg9mwd" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ZbR8x8RUAxCKXB6yLESvAfldG3pV+O9vq9xklBsJuKA5W8wL15ugMZDNZzoGmGZ4aIdjGoqedgT1v/tpxIvVBvbRi88ARygVDjdmXVe1c0zvF/OG/T+hSMfL62y4S090ASLnvTi8uukL2flRRhlW9nR8xJxKwtyQrAYVpE/fKsNBHLu1274Skzu85JzPZKWPs1ayHH4fv9r8A+9u9OlsFay2p9qXuAh3Y9oQqcc3ljpRdPFPe8ktPGSj30wqwLp8MijdPjAG9hvLEI3wPfemkm6WUK3X5HFNQ62FyCjbV7TJR+0gFeLHBE9hy26JpNv8E8SDITyRHVJ4AXaIltK/dg== 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=6HZwjpIcQWA45svpjL3PR/AIV9ID24JUqVTbg8hBq6A=; b=KL7uu4tyC/OBy8LO4odQnQIQIN+tq1BxZmDdVpYt42llJPWAjASH8/W6O+tgeMuYDnJHE034UZVhmnobNk7kEFAEtqc29YTyK2QiIslmnZvbt9FJGf7X0RDq+Z9HzSLnyeq6xyFLWHAGtSTMftG7B+c8gwPmxMno7EzjF9mNC4UEM7C9pzSUbCVSe4z4KOu6pNIE3XTJVB88Mo5zfCLY8xszUtc61sURakFYZZ7BcZtV0/kbuLAhgxIPKUd2i6yG6yo6SPB4qoVlKWEkh9U8iktHlPlrtN6lJGNyOP6R83rjq7ft+WrGg0j5LQz5jx8ogdPMNabEdBp/X+yYOUKENA== 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=6HZwjpIcQWA45svpjL3PR/AIV9ID24JUqVTbg8hBq6A=; b=tkkg9mwd/1b12qblthkVpwqkrF7NRvDS9mSZlb3R7o21ZCXhBmhGrusJsc1jMmdTfmBWINqJR9n7OBHyOahFAbWfTCBqY1VSxRGOWXP1GggbZRPko3vLJFJ8vFNx6P9zTP8XnFv+Us8gyeR+WbZ1ZPe298KGWaC4QNLNGwIl2vo32/FdCy8MaYR+I9Re99ZevSm8WLUSm/WWLbup5ZHj92FgfgMQ5lbcA3I/OIy9Fbq3qFG8UHeHgNqimXCYHmjigYJyGHI2iW/oLT7pK9cN9VT65uUHQwe/IW0LypSh9t/0EQa202Sx0uHwVlpkpvc0LtkU7hA7EGbI93eJX+N8bw== Received: from CH0PR03CA0015.namprd03.prod.outlook.com (2603:10b6:610:b0::20) by IA0PR12MB8931.namprd12.prod.outlook.com (2603:10b6:208:48a::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8026.20; Tue, 22 Oct 2024 00:20:31 +0000 Received: from CH2PEPF00000149.namprd02.prod.outlook.com (2603:10b6:610:b0:cafe::4f) by CH0PR03CA0015.outlook.office365.com (2603:10b6:610:b0::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:31 +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 CH2PEPF00000149.mail.protection.outlook.com (10.167.244.106) 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:31 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) 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:14 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail203.nvidia.com (10.129.68.9) 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:14 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Mon, 21 Oct 2024 17:20:13 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , , , , Subject: [PATCH v4 10/11] Documentation: userspace-api: iommufd: Update vIOMMU Date: Mon, 21 Oct 2024 17:19:32 -0700 Message-ID: <97fa87a511f0132ee0e233cadf09af075e4404c5.1729553811.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: CH2PEPF00000149:EE_|IA0PR12MB8931:EE_ X-MS-Office365-Filtering-Correlation-Id: 5753aaa6-d689-456a-1dd8-08dcf22f56d9 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|1800799024|82310400026|376014|7416014|36860700013; X-Microsoft-Antispam-Message-Info: zXaZzaMFAshM9D7x+YzPaXSHkWzmgl8lr4CU9YenGgd77eYoZmx8n2YsdBIACtpC39j5U0Yfgaq7pm4db6X85UmxyLfiTkd895cvAs3iJ/AcCLrtMzgeKubVyiQKA5vsjgkNITYexHVbEK/gsz0TfGiJOFkS1japfTjTVe7fW0C2mlG2Nmik8vI4ZOYksSVf0FC3UWJ4ERbSjwpew4xeiXHP908fz8IRq+LI2sbEA4ifLCFt5yvwBiSA05RAjetAsYMJJedxOQc+3+44TxJpio5PIv96rQLMf/TnAcJ7fAI25Dw+rL0isY88uQUSTxLG1NwBqUAlxdwNILjoOlDuOB3GibIPkSiHiGEqV/FJYBPYpOZoJIW8tdfdDhYP6HG5pcLYnh4h2CzCGROJasJAY3dorBX23dCxgquj7ScdgnUflCefwskPBf+RS5+1oYBpsrNtoprlTiKNHLqM4JjVVOqscyW4ZnENYSEjJrUhgko41TLZgmoBHfMAvvqAxMJuNBNjCePEsa/O3pG/V7egL2RxyjMptuzld4Jf+wFFIWhzqOPYCVUqwdRjx6LH0Zs9uPCdl38PS452IwTM8CFkFzljnWQlgY4R2yU6UzuRbh+6Ira/bgQXal6HVhH2IeGKS2ONe6w9xLLKg02wFyu11//YEubh17C6VyVz16YrGlgAycbNtjWIurlxG5Pm9NOvq2kCPSbDTAk3DDnP083A0oc26tjXkAVspWPfBb4Qebu31uS/SDwdV41FtMW+q160YScBxas0XaAQ8AgYHbNeJVvFINGwGYvIPj1La47smcE+DOY2AsIhTIzbO34oiydrPnfT+z5lJeRNQbUKQHgjUrIwO94pmZfMMxs7wIkAGx5rjvaIVQNVF5xcP5G2UI66p7L5M4mn/SRr5vX7CCzLH5KKILkPqb4VzzI7icO6zQ0w5wqK79LOUSc1p8fDD9iCnZTzfzp5n9M4MXjIYVa1A7nmIMKo/m54rD5IAjW33PAsqSO4uIzsL5VmIqPkNXF/s1IhXBo34uBBOe2zkNLn5omFSRhdK8DJvhzzna9wx5lCt3bOLYhoirD3/ZoL9SKQy8GQbCV6f5NrI0319vcpe+wlFkn8XkEfawB+zS0pXV54fyQxxPuoS+bsJbD0YYiGZ0zJWhSJeNr/1WIYw9Q8aL61w0Qaftc9CwPX64EA7E9M3G+oJQMVdYmCtRU8MIiqXdfKCrhqP7K6MyP1RJHpbHqpHNdvdT//TDJsSQnXos/iVGoMPpRG7kbWIQ/6Qo97Y+LQATH6dfNcmRJ7zQgs1tlII13JkHBBy+WOYWYzY/QX+ooh/FUqEefL8ZFuy0UiNOJG+KU4hjPgYWg0D8b4whupwSBfSQNeA2tr1JYoT9iouElWynT+lrsR6/+s8o8wNXTk9hpCpAJyMgmFnozwpQ== 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)(1800799024)(82310400026)(376014)(7416014)(36860700013); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2024 00:20:31.2503 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 5753aaa6-d689-456a-1dd8-08dcf22f56d9 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: CH2PEPF00000149.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB8931 With the introduction of the new object and its infrastructure, update the doc to reflect that and add a new graph. Reviewed-by: Jason Gunthorpe Signed-off-by: Nicolin Chen Reviewed-by: Kevin Tian --- Documentation/userspace-api/iommufd.rst | 69 ++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/Documentation/userspace-api/iommufd.rst b/Documentation/userspace-api/iommufd.rst index 2deba93bf159..92d16efad5b0 100644 --- a/Documentation/userspace-api/iommufd.rst +++ b/Documentation/userspace-api/iommufd.rst @@ -63,6 +63,37 @@ Following IOMMUFD objects are exposed to userspace: space usually has mappings from guest-level I/O virtual addresses to guest- level physical addresses. + - IOMMUFD_OBJ_VIOMMU, representing a slice of the physical IOMMU instance, + passed to or shared with a VM. It may be some HW-accelerated virtualization + features and some SW resources used by the VM. For examples: + * Security namespace for guest owned ID, e.g. guest-controlled cache tags + * Access to a sharable nesting parent pagetable across physical IOMMUs + * Virtualization of various platforms IDs, e.g. RIDs and others + * Delivery of paravirtualized invalidation + * Direct assigned invalidation queues + * Direct assigned interrupts + * Non-affiliated event reporting + Such a vIOMMU object generally has the access to a nesting parent pagetable + to support some HW-accelerated virtualization features. So, a vIOMMU object + must be created given a nesting parent HWPT_PAGING object, and then it would + encapsulate that HWPT_PAGING object. Therefore, a vIOMMU object can be used + to allocate an HWPT_NESTED object in place of the encapsulated HWPT_PAGING. + + .. note:: + + The name "vIOMMU" isn't necessarily identical to a virtualized IOMMU in a + VM. A VM can have one giant virtualized IOMMU running on a machine having + multiple physical IOMMUs, in which case the VMM will dispatch the requests + or configurations from this single virtualized IOMMU instance to multiple + vIOMMU objects created for individual slices of different physical IOMMUs. + In other words, a vIOMMU object is always a representation of one physical + IOMMU, not necessarily of a virtualized IOMMU. For VMMs that want the full + virtualization features from physical IOMMUs, it is suggested to build the + same number of virtualized IOMMUs as the number of physical IOMMUs, so the + passed-through devices would be connected to their own virtualized IOMMUs + backed by corresponding vIOMMU objects, in which case a guest OS would do + the "dispatch" naturally instead of VMM trappings. + All user-visible objects are destroyed via the IOMMU_DESTROY uAPI. The diagrams below show relationships between user-visible objects and kernel @@ -101,6 +132,28 @@ creating the objects and links:: |------------>|iommu_domain|<----|iommu_domain|<----|device| |____________| |____________| |______| + _______________________________________________________________________ + | iommufd (with vIOMMU) | + | | + | [5] | + | _____________ | + | | | | + | |----------------| vIOMMU | | + | | | | | + | | | | | + | | [1] | | [4] [2] | + | | ______ | | _____________ ________ | + | | | | | [3] | | | | | | + | | | IOAS |<---|(HWPT_PAGING)|<---| HWPT_NESTED |<--| DEVICE | | + | | |______| |_____________| |_____________| |________| | + | | | | | | | + |______|________|______________|__________________|_______________|_____| + | | | | | + ______v_____ | ______v_____ ______v_____ ___v__ + | struct | | PFN | (paging) | | (nested) | |struct| + |iommu_device| |------>|iommu_domain|<----|iommu_domain|<----|device| + |____________| storage|____________| |____________| |______| + 1. IOMMUFD_OBJ_IOAS is created via the IOMMU_IOAS_ALLOC uAPI. An iommufd can hold multiple IOAS objects. IOAS is the most generic object and does not expose interfaces that are specific to single IOMMU drivers. All operations @@ -132,7 +185,8 @@ creating the objects and links:: flag is set. 4. IOMMUFD_OBJ_HWPT_NESTED can be only manually created via the IOMMU_HWPT_ALLOC - uAPI, provided an hwpt_id via @pt_id to associate the new HWPT_NESTED object + uAPI, provided an hwpt_id or a viommu_id of a vIOMMU object encapsulating a + nesting parent HWPT_PAGING via @pt_id to associate the new HWPT_NESTED object to the corresponding HWPT_PAGING object. The associating HWPT_PAGING object must be a nesting parent manually allocated via the same uAPI previously with an IOMMU_HWPT_ALLOC_NEST_PARENT flag, otherwise the allocation will fail. The @@ -149,6 +203,18 @@ creating the objects and links:: created via the same IOMMU_HWPT_ALLOC uAPI. The difference is at the type of the object passed in via the @pt_id field of struct iommufd_hwpt_alloc. +5. IOMMUFD_OBJ_VIOMMU can be only manually created via the IOMMU_VIOMMU_ALLOC + uAPI, provided a dev_id (for the device's physical IOMMU to back the vIOMMU) + and an hwpt_id (to associate the vIOMMU to a nesting parent HWPT_PAGING). The + iommufd core will link the vIOMMU object to the struct iommu_device that the + struct device is behind. And an IOMMU driver can implement a viommu_alloc op + to allocate its own vIOMMU data structure embedding the core-level structure + iommufd_viommu and some driver-specific data. If necessary, the driver can + also configure its HW virtualization feature for that vIOMMU (and thus for + the VM). Successful completion of this operation sets up the linkages between + the vIOMMU object and the HWPT_PAGING, then this vIOMMU object can be used + as a nesting parent object to allocate an HWPT_NESTED object described above. + A device can only bind to an iommufd due to DMA ownership claim and attach to at most one IOAS object (no support of PASID yet). @@ -161,6 +227,7 @@ User visible objects are backed by following datastructures: - iommufd_device for IOMMUFD_OBJ_DEVICE. - iommufd_hwpt_paging for IOMMUFD_OBJ_HWPT_PAGING. - iommufd_hwpt_nested for IOMMUFD_OBJ_HWPT_NESTED. +- iommufd_viommu for IOMMUFD_OBJ_VIOMMU. Several terminologies when looking at these datastructures: From patchwork Tue Oct 22 00:19:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 837689 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2058.outbound.protection.outlook.com [40.107.236.58]) (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 57F6813C9A6; Tue, 22 Oct 2024 00:20:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.236.58 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729556440; cv=fail; b=pdClIiCnKPOk8QG5XGlzg9vetYXKMy916YYthJbAzw1c8kgmEbOibcyOF+trqRwWewF2FT6eVM0kY7akz6Zebro8O9BMA/jGvtCRZXdqMSoALhbfaL4dMRHywKQIR4wTXTv3aMpe9n51zT6dnNTq6thhEs3JeE90QIcpsqiSzRA= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729556440; c=relaxed/simple; bh=D1zewypqefrSUjzrw6seFm6Ez+N+fww5CIhLxkto8dU=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=KvPWv4GFoR0EhIDbka8+ln3Q7rNcEpS2SfaFG2c9+nVcPiRL6TsXU3M7Em4TiMWJBuSnkOw2+0i1lh/dM6hiaDdz22yhp2bJi4k2QBsuwm45I+x6Ks1UBZBPnxKSzKIa7x3SH1V9d/WTNIITvmhVxLYwVIE00EIQJDtlvYPze00= 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=U/mr2qhO; arc=fail smtp.client-ip=40.107.236.58 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="U/mr2qhO" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=CYqI4ahD3iCsXpvAZQLzIh1eGw8+pejD6kqv29Ksx6zmUf4pkUjENMBWBWBtrf7Bu2N9NuWfb7yowHLukSiTBdDhP6sUAAXmsjz72jTdk8AHxS/RCqFpfmLD3AtwIyvBr7GhOrnv7R+4VgPKDUxIKTw5CHYVxBAqE2ng3XArd8R9GpkSIuQNA+P51nOjhDji72TP8U253AQZ1TYoO04Mw6SQc8B4tQW/TpMo4T6id3F1OdUZwsgimJeNaz0e2qC7W1BLexruqm/F3w16XqAceSbhzDZu8zimwUgXMRMIUAB45nvBAElkHtjDlNUHQcCBBg73nAkbvjfjhinEHTPfpw== 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=6PYa9vSbGvH7vEQ33iwiogecOJxQ0Z0Hkgn0098ZFTo=; b=AEASiNV4BMCdLlEPblVjueMojAbfNhRMZt6HneROURNrDIPFbclpLvS0B61on52n0r5L05MuIQHqn2gd80W+7gt2PGvFnSBmp7FsTb5ku275Rp/mgR3/ZrozUMahwD9YNXa259mgbnqBoiwen3r5r+ZM6eNW/HmPZs8TnTZbVBhGCURCJzooJqFXEW7mPDuIZIHucqCYd8IDuJQpK9dSrA1+LCFGrLg8VW1psKNH8MXuzcMAmokL0siOUV+aEsHnXQBdCdS0D7PLc76tphMV6XzuqWTu5Rbt3Bu0wIWk8eqjHsdhDRPQ+rGDvotEYcpZuOQzFDQkaEQMS3K5D5HzpQ== 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=6PYa9vSbGvH7vEQ33iwiogecOJxQ0Z0Hkgn0098ZFTo=; b=U/mr2qhOOnA7veskb0ps8oI4ODlpiJAVymtpPCxSa7ZDpLoECRXbenqSONrJ2H6SEnOfzPP56IN/iNE0Z6DQfwukMJoR+gwq6cPcFJQRqoOcIQYMKMnDgh48NDcBzwmINT3q/RXMOBpw+13hLa68giEWXa4dnkeLKwB+O2cP2Rm0hOquMQQQs2MaKCaLy6QwKEg9KIC6QylCX1OxxNfy+PVdZ4DmJ+JUG/yhIEd5lfD3sXDB04kMfeDNW+VakR12EUMZ2ktwjK7q3v2EdkF7SKPZqkJxzlZvGM6sjJ97ZvgEXJ8sYygWN3mpvTzXNHaGynyYIPDOH9BXOQk8E06P4w== Received: from CH0PR03CA0025.namprd03.prod.outlook.com (2603:10b6:610:b0::30) by DM4PR12MB6373.namprd12.prod.outlook.com (2603:10b6:8:a4::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.28; Tue, 22 Oct 2024 00:20:32 +0000 Received: from CH2PEPF00000149.namprd02.prod.outlook.com (2603:10b6:610:b0:cafe::86) by CH0PR03CA0025.outlook.office365.com (2603:10b6:610:b0::30) 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:32 +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 CH2PEPF00000149.mail.protection.outlook.com (10.167.244.106) 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:32 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) 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:16 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail202.nvidia.com (10.129.68.7) 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:15 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Mon, 21 Oct 2024 17:20:14 -0700 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , , , , Subject: [PATCH v4 11/11] iommu/arm-smmu-v3: Add IOMMU_VIOMMU_TYPE_ARM_SMMUV3 support Date: Mon, 21 Oct 2024 17:19:33 -0700 Message-ID: <2180fdf423d0f2fcc5c031687690100b12c2ba51.1729553811.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: CH2PEPF00000149:EE_|DM4PR12MB6373:EE_ X-MS-Office365-Filtering-Correlation-Id: f825288c-75dd-409e-dd07-08dcf22f5759 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|376014|7416014|82310400026|1800799024|36860700013; X-Microsoft-Antispam-Message-Info: +xWBkhnQKCM8cYW8/birDoG0OTXP1N8Kg3NN8kWTm9veASJoBc4T4pJL1QrLGjaTr2AFrLp3PcGf9096jZ6Zc0+gTgt7xJldQQzvWaqYW3EEOZ8tVrHB+ErOKzUPHQEcq0mrQkVJjia5EHPtYOdfSrtnYJWP1Q4uEyQCl31eM81QtRD0L1C+rNb3hZ4foYEs6EjPakMwzg2me3OGBfg3sNsFMaeMMvkuI+lBeXB3Zh1j/eTSSkMcpJoPSaXjqwpp2VbC+7ifXxeat4QcxCqds5m29LA/1VdBirw/r2dfBA0JOeombE20J/dY0owgGLvIojph+ys3GFk6nbzHlLgrCWNvMY+JT1YpL/JKhQ9d+7rJFzXAI4+Px95NSlniz8DA3IvTXEza7xLPcxC4C6wpj+qPW9gjNrs6wQoJBwe4AWSWOf3QOfFWWFlQ3irm8O0rv9JwxLkiHn6Nyt2i7gpb7K1PcBGFTsQ7N5nYxcFSEibEEXq2xMS7buEiH5dkJmULuZKuxkJx+Mf06rJQuRTHG8fFH64A8QBiuWenHjqLKIO1UwLLbaHB7i2/DKj+LlBcfBUYcRsJcxcDZ/qHGTP+Aex+BOB8muLABy+4C4JtDW7v6Uf1/zc+hVRKJpOd2pAWfgSfyrsh4tjewg0dO9zU4J3TYye9kpK2jynJwbf4HH9P2idCrdPnOvUgsV99YQ64o3LQFSTKpYT18i4aM+PJHLiejaaCMmBur7sOiBNhMls/idjRSlZOO8ArL5huf+kMmwHmFmlyd82Oih+CDxp5TpQUgD/ZUHft8VE6smZuFcC7m9rLZg4xtOiBEeLWOyiVCcNPNX7xst1xGmM7wfjC2aI1F3tWEpEL0MdKH1ok13bi8Ov44ELH9H2apD5XONdpYpHdSNK87f7ddp1qoZryvpe9kQVEteeTf741uuxb+t3OvPaPachFwdtyjHmhWz/1D1o84oQlulIwoIzVCsgZlnwtxzuwhv9VIfgws5Z1YFM4C/+mZgXp1WyYMYkMpj4CJ+HMeeXChSS9RjAZ+h/GUv6WFcf5RUuwhjj7sBlXCifIp/AIT4D5v2GUMyCJN4z6KU40ps4K68+n6v4KrafUwdkNNn/eCJnWdR6E+I94faH3z28OlelbhuPW70yA0iFFIqSlKa5JN19NGxnwsH2U33dRzXp69Q6pJVK59297BUUyJXyFpiVZuUmWLUiga7/5Lybr5HqEp6JJ5HqnJo/fklQx2ztrl6AQA/m4/oZyvfEP9i0C40xivGmfgO7IfoOC+R4swhbvkSg3JEPqVUAIgHvOXWAYEfcrAaVBxJBW6jcq0ss4/DPKMMdBpPXpE/y70mGngqAkR7Qe/ewHvju/ypO1RNueAPmJunn6KaXMjWkHD4s+uTEELyol0xleMcKD1qLLxLSX2rIYbTY9qGbTSQ== 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)(376014)(7416014)(82310400026)(1800799024)(36860700013); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2024 00:20:32.1253 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f825288c-75dd-409e-dd07-08dcf22f5759 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: CH2PEPF00000149.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB6373 Add a new driver-type for ARM SMMUv3 to enum iommu_viommu_type. Implement an arm_vsmmu_alloc() with its viommu op arm_vsmmu_domain_alloc_nested(), to replace arm_smmu_domain_alloc_nesting(). As an initial step, copy the VMID from s2_parent. A later cleanup series is required to move the VMID allocation out of the stage-2 domain allocation routine to this. After that, replace nested_domain->s2_parent with nested_domain->vsmmu. Note that the validatting conditions for a nested_domain allocation are moved from arm_vsmmu_domain_alloc_nested to arm_vsmmu_alloc, since there is no point in creating a vIOMMU (vsmmu) from the beginning if it would not support a nested_domain. Signed-off-by: Nicolin Chen --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 26 +++--- include/uapi/linux/iommufd.h | 2 + .../arm/arm-smmu-v3/arm-smmu-v3-iommufd.c | 79 ++++++++++++------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 9 +-- 4 files changed, 70 insertions(+), 46 deletions(-) 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 956c12637866..5a025d310dbe 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 @@ -835,7 +836,7 @@ struct arm_smmu_domain { struct arm_smmu_nested_domain { struct iommu_domain domain; - struct arm_smmu_domain *s2_parent; + struct arm_vsmmu *vsmmu; __le64 ste[2]; }; @@ -1005,21 +1006,22 @@ tegra241_cmdqv_probe(struct arm_smmu_device *smmu) } #endif /* CONFIG_TEGRA241_CMDQV */ +struct arm_vsmmu { + struct iommufd_viommu core; + struct arm_smmu_device *smmu; + struct arm_smmu_domain *s2_parent; + u16 vmid; +}; + #if IS_ENABLED(CONFIG_ARM_SMMU_V3_IOMMUFD) void *arm_smmu_hw_info(struct device *dev, u32 *length, u32 *type); -struct iommu_domain * -arm_smmu_domain_alloc_nesting(struct device *dev, u32 flags, - struct iommu_domain *parent, - const struct iommu_user_data *user_data); +struct iommufd_viommu *arm_vsmmu_alloc(struct device *dev, + struct iommu_domain *parent, + struct iommufd_ctx *ictx, + unsigned int viommu_type); #else #define arm_smmu_hw_info NULL -static inline struct iommu_domain * -arm_smmu_domain_alloc_nesting(struct device *dev, u32 flags, - struct iommu_domain *parent, - const struct iommu_user_data *user_data) -{ - return ERR_PTR(-EOPNOTSUPP); -} +#define arm_vsmmu_alloc NULL #endif /* CONFIG_ARM_SMMU_V3_IOMMUFD */ #endif /* _ARM_SMMU_V3_H */ diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index f835ccf4a494..09c1b4ba46d8 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -859,9 +859,11 @@ struct iommu_fault_alloc { /** * enum iommu_viommu_type - Virtual IOMMU Type * @IOMMU_VIOMMU_TYPE_DEFAULT: Reserved for future use + * @IOMMU_VIOMMU_TYPE_ARM_SMMUV3: ARM SMMUv3 driver specific type */ enum iommu_viommu_type { IOMMU_VIOMMU_TYPE_DEFAULT = 0, + IOMMU_VIOMMU_TYPE_ARM_SMMUV3 = 1, }; /** diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c index 44e1b9bef850..70ad857a57b8 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c @@ -34,7 +34,8 @@ static void arm_smmu_make_nested_cd_table_ste( struct arm_smmu_ste *target, struct arm_smmu_master *master, struct arm_smmu_nested_domain *nested_domain, bool ats_enabled) { - arm_smmu_make_s2_domain_ste(target, master, nested_domain->s2_parent, + arm_smmu_make_s2_domain_ste(target, master, + nested_domain->vsmmu->s2_parent, ats_enabled); target->data[0] = cpu_to_le64(STRTAB_STE_0_V | @@ -75,7 +76,8 @@ static void arm_smmu_make_nested_domain_ste( break; case STRTAB_STE_0_CFG_BYPASS: arm_smmu_make_s2_domain_ste( - target, master, nested_domain->s2_parent, ats_enabled); + target, master, nested_domain->vsmmu->s2_parent, + ats_enabled); break; case STRTAB_STE_0_CFG_ABORT: default: @@ -100,7 +102,7 @@ static int arm_smmu_attach_dev_nested(struct iommu_domain *domain, struct arm_smmu_ste ste; int ret; - if (nested_domain->s2_parent->smmu != master->smmu) + if (nested_domain->vsmmu->smmu != master->smmu) return -EINVAL; if (arm_smmu_ssids_in_use(&master->cd_table)) return -EBUSY; @@ -151,36 +153,15 @@ static int arm_smmu_validate_vste(struct iommu_hwpt_arm_smmuv3 *arg) return 0; } -struct iommu_domain * -arm_smmu_domain_alloc_nesting(struct device *dev, u32 flags, - struct iommu_domain *parent, +static struct iommu_domain * +arm_vsmmu_domain_alloc_nested(struct iommufd_viommu *viommu, const struct iommu_user_data *user_data) { - struct arm_smmu_master *master = dev_iommu_priv_get(dev); + struct arm_vsmmu *vsmmu = container_of(viommu, struct arm_vsmmu, core); struct arm_smmu_nested_domain *nested_domain; - struct arm_smmu_domain *smmu_parent; struct iommu_hwpt_arm_smmuv3 arg; int ret; - if (flags || !(master->smmu->features & ARM_SMMU_FEAT_NESTING)) - return ERR_PTR(-EOPNOTSUPP); - - /* - * Must support some way to prevent the VM from bypassing the cache - * because VFIO currently does not do any cache maintenance. - */ - if (!arm_smmu_master_canwbs(master) && - !(master->smmu->features & ARM_SMMU_FEAT_S2FWB)) - return ERR_PTR(-EOPNOTSUPP); - - /* - * The core code checks that parent was created with - * IOMMU_HWPT_ALLOC_NEST_PARENT - */ - smmu_parent = to_smmu_domain(parent); - if (smmu_parent->smmu != master->smmu) - return ERR_PTR(-EINVAL); - ret = iommu_copy_struct_from_user(&arg, user_data, IOMMU_HWPT_DATA_ARM_SMMUV3, ste); if (ret) @@ -196,9 +177,51 @@ 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->vsmmu = vsmmu; nested_domain->ste[0] = arg.ste[0]; nested_domain->ste[1] = arg.ste[1] & ~cpu_to_le64(STRTAB_STE_1_EATS); return &nested_domain->domain; } + + +static const struct iommufd_viommu_ops arm_vsmmu_ops = { + .domain_alloc_nested = arm_vsmmu_domain_alloc_nested, +}; + +struct iommufd_viommu *arm_vsmmu_alloc(struct device *dev, + struct iommu_domain *parent, + struct iommufd_ctx *ictx, + unsigned int viommu_type) +{ + struct arm_smmu_device *smmu = + iommu_get_iommu_dev(dev, struct arm_smmu_device, iommu); + struct arm_smmu_master *master = dev_iommu_priv_get(dev); + struct arm_smmu_domain *s2_parent = to_smmu_domain(parent); + struct arm_vsmmu *vsmmu; + + if (viommu_type != IOMMU_VIOMMU_TYPE_ARM_SMMUV3) + return ERR_PTR(-EOPNOTSUPP); + + if (!(smmu->features & ARM_SMMU_FEAT_NESTING)) + return ERR_PTR(-EOPNOTSUPP); + + /* + * Must support some way to prevent the VM from bypassing the cache + * because VFIO currently does not do any cache maintenance. + */ + if (!arm_smmu_master_canwbs(master) && + !(smmu->features & ARM_SMMU_FEAT_S2FWB)) + return ERR_PTR(-EOPNOTSUPP); + + vsmmu = iommufd_viommu_alloc(ictx, arm_vsmmu, core, &arm_vsmmu_ops); + if (IS_ERR(vsmmu)) + return ERR_CAST(vsmmu); + + vsmmu->smmu = smmu; + vsmmu->s2_parent = s2_parent; + /* FIXME Move VMID allocation from the S2 domain allocation to here */ + vsmmu->vmid = s2_parent->s2_cfg.vmid; + + return &vsmmu->core; +} 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 c7cc98961019..de598d66b5c2 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2661,7 +2661,7 @@ to_smmu_domain_devices(struct iommu_domain *domain) domain->type == IOMMU_DOMAIN_SVA) return to_smmu_domain(domain); if (domain->type == IOMMU_DOMAIN_NESTED) - return to_smmu_nested_domain(domain)->s2_parent; + return to_smmu_nested_domain(domain)->vsmmu->s2_parent; return NULL; } @@ -3126,13 +3126,9 @@ arm_smmu_domain_alloc_user(struct device *dev, u32 flags, struct arm_smmu_domain *smmu_domain; int ret; - if (parent) - return arm_smmu_domain_alloc_nesting(dev, flags, parent, - user_data); - if (flags & ~PAGING_FLAGS) return ERR_PTR(-EOPNOTSUPP); - if (user_data) + if (parent || user_data) return ERR_PTR(-EOPNOTSUPP); smmu_domain = arm_smmu_domain_alloc(); @@ -3541,6 +3537,7 @@ static struct iommu_ops arm_smmu_ops = { .dev_disable_feat = arm_smmu_dev_disable_feature, .page_response = arm_smmu_page_response, .def_domain_type = arm_smmu_def_domain_type, + .viommu_alloc = arm_vsmmu_alloc, .user_pasid_table = 1, .pgsize_bitmap = -1UL, /* Restricted during device attach */ .owner = THIS_MODULE,