From patchwork Mon Mar 27 18:05:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 75605 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1700000vqo; Mon, 27 Mar 2023 11:12:15 -0700 (PDT) X-Google-Smtp-Source: AKy350YveOf91q5uZZ44cEXlCvVG737MQZaZlBvj39GEGg1+k332kjJ41bK/qjryI213h2pdz1p7 X-Received: by 2002:a17:907:cf92:b0:93f:21fa:eabf with SMTP id ux18-20020a170907cf9200b0093f21faeabfmr9255392ejc.22.1679940734879; Mon, 27 Mar 2023 11:12:14 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1679940734; cv=pass; d=google.com; s=arc-20160816; b=XYnJpZC8Z0c7D9ndpXljxae5F6PVN/kavYqSXF+I+0Zp5hThfGpECcMEmP4lCyro/B 5txP078v15n6ECXQqud/f79G3U8VqM5ApOC3d4WMKQ5CUmyEV2SLfz7fZg96YiSW2L2o bmKTXt7IBqf7U/7C/dzhYYMraVLc7DcUhHT28DHz1Lc830G2aOYzNT7Fx5qKn+4tkGhU d5l94cKVFUOSPlsW8b+0W6fpwsXE5UeEGqBzuHnPO2zGJ1tXxvWfDLrr5FQroZxMzbJV CjZUropH+PVAja/IRb+aoeAk27qii4Ma7pAt265pbiZcmbkpQUD9L1/W7ZcQkbHUHwVm 3W6A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=/NowpQxnwrx6PH8/RJtJ9gNozlYcnvudGcfIdWOhv+w=; b=VnihyhxHShQ0Ep7vP2Q4TeIIgziUX0REPuBQYbiBNDi1K7CyQWSweHzSuLHfZaX36a S86VkqlBfkE9ucd8ngzVmED2p8uCid/kp3837wmsL6WyR9SX6HENzt6W6paUTwJnJ5ab GP0RVTrvhyzbuytVb+FmAlWXYwEzI1EBClyhSqWY+8IL6xlT1by4dyvg0d6mD14qU7Fs Nh5FEM00MO5OoOhh+aK/VpmFwhWfOWkn7N96c770Bf3t2c6EgMK1fUqCrLG99YxEt8Un mG2isqOdle/MIJacPcxSOUx2tsyk3NDzTQFcYZfR8e4cP66vTaKdKIO0RwOlpCeW6bOX ACdg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=Z0FDfmuZ; arc=pass (i=1 spf=pass spfdomain=nvidia.com dmarc=pass fromdomain=nvidia.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=nvidia.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w16-20020a170906d21000b009153f0ec59bsi23982433ejz.800.2023.03.27.11.11.50; Mon, 27 Mar 2023 11:12:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=Z0FDfmuZ; arc=pass (i=1 spf=pass spfdomain=nvidia.com dmarc=pass fromdomain=nvidia.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=nvidia.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232428AbjC0SFt (ORCPT + 99 others); Mon, 27 Mar 2023 14:05:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232554AbjC0SFf (ORCPT ); Mon, 27 Mar 2023 14:05:35 -0400 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2083.outbound.protection.outlook.com [40.107.102.83]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 64B393A99; Mon, 27 Mar 2023 11:05:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dT6+m7kbGuixaJu2WVOjs1FbleSTl8q66jwBeY+MosHxcD5pN+wJsWaBRUV/605VVqAoiY2AWjYcI7r9gCytDdDzZHrGeZghsYx8r3ZYZhF8EbtVBdSm/X+dPAhevcaDAzwB7yffugOh5MR0mVs2TNL+frTiV8YMkSKx1SdI2EvQwQWTxcfJBh02/w3Lo48TPN1Tmv5/BR/N5R40ppgxuC2KZSwcOoyzOJLDfRBnVPjzO1JUvHUSC/G/3sBxcVyi5f3HKR5NJHPjv0TZh/ILlgcsPgqXWyTIcnVa1gcy4AbeMcCG2FE4mX7wX87mjnkAjCQI+FCUeI1lp0o66I5RIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=/NowpQxnwrx6PH8/RJtJ9gNozlYcnvudGcfIdWOhv+w=; b=gYfHItmga+NLSCVBtA3YKPaVl2v5R9XckqKPbyFjqjEqlwipElSeFe2z3rgFNUXcqHXKdKl1xHkPkPsUBv+zBYNcdAr6313XfQJ8WzHkXN69N6/bzbyqs/+/E91EvywytTa79A9HnhPWkGFScCULhKofioqCvj5RRw4wtb/DIXgyN6wv8dTLPhmNU+Rij7a/cvdz/Di0pDKc1o4nKvN8JhKsih33qzAvm/iEwFq0VrXZ8aBD0iT6kBNbkNcu4J296IUt8ns2hovLLpjLu3Hc+jnIKeu9LsQ5bsgJ2Y0+U2qp7n68UTQBQM6S5Uiq8m6kDeaWq7gJieBWAFYeCkAdJg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.232) smtp.rcpttodomain=intel.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none 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=/NowpQxnwrx6PH8/RJtJ9gNozlYcnvudGcfIdWOhv+w=; b=Z0FDfmuZD9+mLUrlmggwvitBAcv5iOxI1Zudqx+RmYGRdzTEB3Rn98GRzpZQcupyohtHsV8eZgORM2JDFPGlV2Hue8DP2sbbsWtE8k7sTOcwNL+St3vlN7X1lsRhdaev/S1d07vnpzUOW63TBZ/tm2r0SklgIFVCHpUoU0QynnaaRVZkOAK6o5DbIMRTsK6zu6+SGkghlrTl+VmnANgsexGM4NOIjLp2ffDQJs+VbAEAq0B6PT9JKmaff4gVnU5wHegUmqusoxKGyf32RvNoGyLpKRgCTh3a8oDxoKzQ6wbMiGkesmNDnI3N6L7DjCun8dkfrdqNVr+3CXu0hCZ1Pw== Received: from DM6PR18CA0029.namprd18.prod.outlook.com (2603:10b6:5:15b::42) by SJ0PR12MB5673.namprd12.prod.outlook.com (2603:10b6:a03:42b::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.38; Mon, 27 Mar 2023 18:05:28 +0000 Received: from DM6NAM11FT052.eop-nam11.prod.protection.outlook.com (2603:10b6:5:15b:cafe::50) by DM6PR18CA0029.outlook.office365.com (2603:10b6:5:15b::42) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.41 via Frontend Transport; Mon, 27 Mar 2023 18:05:28 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.232) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.232 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.232; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.232) by DM6NAM11FT052.mail.protection.outlook.com (10.13.172.111) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.22 via Frontend Transport; Mon, 27 Mar 2023 18:05:27 +0000 Received: from drhqmail202.nvidia.com (10.126.190.181) by mail.nvidia.com (10.127.129.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.5; Mon, 27 Mar 2023 11:05:15 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail202.nvidia.com (10.126.190.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.37; Mon, 27 Mar 2023 11:05:15 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.11) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Mon, 27 Mar 2023 11:05:14 -0700 From: Nicolin Chen To: , , , , , , CC: , , , , , , Subject: [PATCH v6 2/4] iommufd: Add iommufd_access_replace() API Date: Mon, 27 Mar 2023 11:05:03 -0700 Message-ID: <955f7464fafc72984f3ec441671f37948f01d714.1679939952.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: References: MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6NAM11FT052:EE_|SJ0PR12MB5673:EE_ X-MS-Office365-Filtering-Correlation-Id: de52682c-92be-4f14-87dc-08db2eedd8a8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ukVYYLH7OsB24f3MJyVVeuTBxJskD/Dy+Hxcz3JJ8qcwD0Wi5iNpBQ7euwwSV73AssXxGd9vwD0lT9h18eebHI+qqafM3CeawYq5TVJGxaV+Sh41Fqz3t2xK3LVbq81eHseDDMOOXuNbCtv0Mjn/Xf1besdKpMDai75TT1Cyrso2YNjA28Hx50W0flL25v1IfcldTpJ5u/6v5jcDDCTvsxN4lSjhR0rFZUNUpH/Ica1KpbsgsGy/t4PKHIaBeGgbiYNnMXWTKRuHbvpc7Fpu57RUh2lLA/qhivR4MiVrOvWhl9oG9iqKVXd97nbQfgjxm8ZpsnZn2msybzDog0DvHyogsKlN4o/5S61tppDI50x7Dh/60+CsuZ5fdQIjhZsw/h+ldP9DMrduev2h85rpEqYzTQRm+xNn/ZNXe01QEZaERgsju+3P+mGph3un/wb9JmpTmHBLx3g0PviKcflGElXICLQ3uKLe5sb/vZ19K7ZblzN/37y5PYs7r6koO6q+o4B2lPkHTacHSpSehrWKludLfo2tfadCDLQ9F0ivfFC87rRRJivz+cPyB/VuIvGX7HUc2ca/KcoP8MOo+q636bVsXC8FqFifHj4JTdixgHSSRLYPUUZdseY46gWVHSZceXHsXbPakCavOY3biTGpdHHzj+kTqLWQuFqiJQFLaMHXL4hfqJiRG//WzSTqlejcTnPu9KQIP2m7VCoFofMwX5qtwJ9aqsh+pvsY/iqCaJqfrXm08ZZetA1PGV4+tgXzIgKOxtFiJK6okuJdMBuwEL+sOTpQ4vOHTxPXlOoWx4Q= X-Forefront-Antispam-Report: CIP:216.228.118.232;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc7edge1.nvidia.com;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(346002)(376002)(136003)(396003)(451199021)(36840700001)(40470700004)(46966006)(356005)(83380400001)(54906003)(7696005)(34020700004)(186003)(110136005)(478600001)(40460700003)(40480700001)(6666004)(26005)(336012)(70586007)(36860700001)(8676002)(2616005)(70206006)(86362001)(82310400005)(82740400003)(2906002)(316002)(7416002)(41300700001)(5660300002)(36756003)(7636003)(426003)(8936002)(4326008)(47076005);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Mar 2023 18:05:27.8701 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: de52682c-92be-4f14-87dc-08db2eedd8a8 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.118.232];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT052.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB5673 X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761545535863215804?= X-GMAIL-MSGID: =?utf-8?q?1761545535863215804?= Extract all common procedure, between the iommufd_access_attach() API and the new iommufd_access_replace() API, to an iommufd_access_change_pt() helper. Add iommufd_access_replace() function for VFIO emulated pathway to use. Suggested-by: Jason Gunthorpe Signed-off-by: Nicolin Chen Reviewed-by: Kevin Tian --- drivers/iommu/iommufd/device.c | 53 +++++++++++++++++++------ include/linux/iommufd.h | 1 + tools/testing/selftests/iommu/iommfd*.c | 0 3 files changed, 42 insertions(+), 12 deletions(-) create mode 100644 tools/testing/selftests/iommu/iommfd*.c diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 6f7b349ba8f5..b6d61e886fbb 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -752,38 +752,67 @@ void iommufd_access_detach(struct iommufd_access *access) } EXPORT_SYMBOL_NS_GPL(iommufd_access_detach, IOMMUFD); -int iommufd_access_attach(struct iommufd_access *access, u32 ioas_id) +static int iommufd_access_change_pt(struct iommufd_access *access, u32 ioas_id) { struct iommufd_ioas *new_ioas; - int rc = 0; + int rc; - mutex_lock(&access->ioas_lock); - if (access->ioas != NULL && access->ioas->obj.id != ioas_id) { - mutex_unlock(&access->ioas_lock); - return -EINVAL; - } + lockdep_assert_held(&access->ioas_lock); new_ioas = iommufd_get_ioas(access->ictx, ioas_id); - if (IS_ERR(new_ioas)) { - mutex_unlock(&access->ioas_lock); + if (IS_ERR(new_ioas)) return PTR_ERR(new_ioas); - } rc = iopt_add_access(&new_ioas->iopt, access); if (rc) { - mutex_unlock(&access->ioas_lock); iommufd_put_object(&new_ioas->obj); return rc; } iommufd_ref_to_users(&new_ioas->obj); + if (access->ioas) + __iommufd_access_detach(access); access->ioas = new_ioas; access->ioas_unpin = new_ioas; - mutex_unlock(&access->ioas_lock); return 0; } + +int iommufd_access_attach(struct iommufd_access *access, u32 ioas_id) +{ + int rc; + + mutex_lock(&access->ioas_lock); + if (access->ioas != NULL && access->ioas->obj.id != ioas_id) { + mutex_unlock(&access->ioas_lock); + return -EINVAL; + } + + rc = iommufd_access_change_pt(access, ioas_id); + mutex_unlock(&access->ioas_lock); + return rc; +} EXPORT_SYMBOL_NS_GPL(iommufd_access_attach, IOMMUFD); +int iommufd_access_replace(struct iommufd_access *access, u32 ioas_id) +{ + int rc; + + mutex_lock(&access->ioas_lock); + if (!access->ioas) { + mutex_unlock(&access->ioas_lock); + return -ENOENT; + } + if (access->ioas->obj.id == ioas_id) { + mutex_unlock(&access->ioas_lock); + return 0; + } + + rc = iommufd_access_change_pt(access, ioas_id); + mutex_unlock(&access->ioas_lock); + return rc; +} +EXPORT_SYMBOL_NS_GPL(iommufd_access_replace, IOMMUFD); + /** * iommufd_access_notify_unmap - Notify users of an iopt to stop using it * @iopt: iopt to work on diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h index 135dc2a28156..6752c58226d1 100644 --- a/include/linux/iommufd.h +++ b/include/linux/iommufd.h @@ -48,6 +48,7 @@ iommufd_access_create(struct iommufd_ctx *ictx, const struct iommufd_access_ops *ops, void *data, u32 *id); void iommufd_access_destroy(struct iommufd_access *access); int iommufd_access_attach(struct iommufd_access *access, u32 ioas_id); +int iommufd_access_replace(struct iommufd_access *access, u32 ioas_id); void iommufd_access_detach(struct iommufd_access *access); void iommufd_ctx_get(struct iommufd_ctx *ictx); diff --git a/tools/testing/selftests/iommu/iommfd*.c b/tools/testing/selftests/iommu/iommfd*.c new file mode 100644 index 000000000000..e69de29bb2d1