From patchwork Mon May 15 14:00:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13241549 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C1EEFC7EE22 for ; Mon, 15 May 2023 14:00:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239000AbjEOOAr (ORCPT ); Mon, 15 May 2023 10:00:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35430 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238846AbjEOOAq (ORCPT ); Mon, 15 May 2023 10:00:46 -0400 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2063.outbound.protection.outlook.com [40.107.244.63]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C1BFE6E; Mon, 15 May 2023 07:00:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=K+CxnPZ1PKD26mOUhkITuPXgPxHDkStrbyFzphvw3+n76ANwNkYuk/Ze0yCCZyi9IHfMmr6TrfzlMSupxqqwNFqz5Iov0pQuAagNox7ol1ry5pt+Q7D8J9Ro5WZm3LIzHa3EvcAVNW9iLqywRNnJ3hWoohKgaQ1Ry7IkMYHg19/1XgrsxIi+y61hVzZx9EJNpiQzt4TzwIH/ePiBMD4cKCbfnHeUBBw/0uakwxgbJ4txhfd0iBfuujU+5zN0D5Mpn7/piCnlQEd194OBypQiqvP1Yil9CX53RGd3SUMvvwBzZ5l0RhtX0VEsCGF4+Sbwb+jAZ+sq2Sda6XRt06rT1w== 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=O0R9bFyMosiv3kaHwBNp2ckP7QZk5LUDP25hz7C1jAc=; b=BDOXvXenFvyD/egLM6KWVl+WNNYWiy/qenw//lpvP0IGuMd0QjeOhnM5rpqH0mystGrRbgsucRRw3u222QwizKpxq8+sD18H4ZVAZdQmwVOr34Bd2sk7wHZfWsIA9RpdUhC3W4Oa9YZS4CSGSSEfUWRjkVeAueSPxYPUncLtLdQadk0QyAJ2Th44YfQ09PybRoKoxaM3SxqlhTCKcpCqCpcgVo0RQFBS7AYXIybgPKDcLnp+tWZTTmuNk2I1n0Pv6TWJGC+GnxT+TcKdAGISc21BakFUR0JbYGGWNvjMEbPaozvkTqB60JatYp83puDv05OfP+hFSHp4Rv1URfKOew== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; 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=O0R9bFyMosiv3kaHwBNp2ckP7QZk5LUDP25hz7C1jAc=; b=lmPs/q57CWWVfJyiSSGP1DGl6HmxJsHePLT7QBAQ2J0RzVBbcztU+A15SdsCFqG9MiYkEjEFvn3nTHG/n/z4sb8NMwe1KJ5yfBwO3e6xnxtZPLfMLL0WRIdDhH4CMLD7EVZ9yTSrmd6A36l+oXTEzWuILwlwJanryiRxh6gwQwR3+g+tKeJzCIs8A+ftteZF70bbn/OiCzi00/WYPkuTnoyYQP+UmEdT8CQpj0IMUJ6JeDjgAFc1OBKrd/cz7dsGBHv5iZZ2uK6psYQr8p/ferCcbVmlwThU22vqpAc+WLbKbjPTMDDspIc8TwbFBhbkZmf9sqiSyOGEhJR+dp+8kQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by SN7PR12MB7954.namprd12.prod.outlook.com (2603:10b6:806:344::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6387.30; Mon, 15 May 2023 14:00:38 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6387.030; Mon, 15 May 2023 14:00:38 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v7 10/19] iommufd: Allow a hwpt to be aborted after allocation Date: Mon, 15 May 2023 11:00:25 -0300 Message-Id: <10-v7-6c0fd698eda2+5e3-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v7-6c0fd698eda2+5e3-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BLAPR03CA0009.namprd03.prod.outlook.com (2603:10b6:208:32b::14) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SN7PR12MB7954:EE_ X-MS-Office365-Filtering-Correlation-Id: 0eca01ad-aa18-4e64-a44b-08db554cc16e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: m2vzbozBn/lWJlQTmW1Jhwyp43XUUwAqoVplgMf+m6TKkKDdgzwcFeLiSo6zjQJbfx0Mxttru/q7CcwNbnwZK19zKMM5EwFhU4/zs3BYT7nsIC4VKJRSe2v78KaMjxgP6PrIRJCJW0TtD2GQzlOaMZl93viCb/qIoFTRky1b7Jqguhokw3yUpuw0u/vocfyBX+5LNzcuqhZHlrU2gnMhR5YscHr8Nb0YlJX8bElHEo/Rj4HuznTWUqlRqglEcRiYrVeWLRI0HiZQa0bSQKJhse1oiK8n/hfGiiXpAxA4Z19QqZoHQt2qLAZTrYKg2j+YHbMoWhb6rS/hxgq4q4nx0DnW2DsfGOe4CAnjjfoVHmUAceq14It5I8dEQ8NhYrcpmzQKyucsFd/8F+m1EqnZxBh67XWVntP0wfMuu+hy+GFSb4ceAz9hNLtwZ6BW0C9vAg+00wT+DWhHSnaM8x/LufrvL7n24dcG0srdZkAg6umLeiJafVvXqSN1gmJaxFEX9Y66dFsBAGhj8egS3KH97RNVk5O6J+2JQtyXOhTXIUrka3D6mfkgpTNXK3Pn2OES X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(136003)(396003)(39860400002)(366004)(346002)(376002)(451199021)(83380400001)(66476007)(66556008)(66946007)(2616005)(6486002)(26005)(6506007)(6512007)(478600001)(54906003)(6666004)(186003)(5660300002)(86362001)(8676002)(8936002)(36756003)(2906002)(316002)(4326008)(41300700001)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: H7CUYomWcKugNHLkCovnNuPs3/Kl0d0ch5nTtQRjSfhpuzDJ17H2ahQQo8zTGAVRdZcOijOIxBKS7RDieP50XoGj7WG5H/UmMe84+g6edMx/w9E88VYB5skzIHR3d6H/3pRYVgbVQsGl2Ct5BVY5OWj6Ehv5Dex+NZYZ7FboQBYkZDGNRC/g/Ce16l4BkPmYofEGN/pWNQH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4kFhFDhY45MDFomY5gMJGpVzaqx3GPb4PQyItqa2ppAi+Uxe2dViqt1AOGTUCE/ZU/7o18WPLma9rMSKLha7/xb6iuHegKVRym2tqP6/2x7JyZzOwhBiHCGSbfzYAi3D6bTJeNtmCXXod0DmLq6/nvWM56Ta0FtfQQwmDiTg4gKzfz9U7O+wphrm0mWWHHAEi8GLFRFITPMyQlFEGi0hVpuic6ZEKZb8tgiPS6US6nty1rSVh1mVgxdHFzcEiVRycPBPWEuvOaVmSJJ0rdSY7MiAMKFhpAFYBJSG9HEbgRddIqLu/EpeDVAlNhMs43umY X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0eca01ad-aa18-4e64-a44b-08db554cc16e X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 May 2023 14:00:35.5644 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 9/4vmm8XlfJL7aX8n1MiJocM2YkTpE+wO4kHM+Vi/KfV2U9zXnxx4rDNKB4IF8AJ X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7954 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org During creation the hwpt must have the ioas->mutex held until the object is finalized. This means we need to be able to call iommufd_object_abort_and_destroy() while holding the mutex. Since iommufd_hw_pagetable_destroy() also needs the mutex this is problematic. Fix it by creating a special abort op for the object that can assume the caller is holding the lock, as required by the contract. The next patch will add another iommufd_object_abort_and_destroy() for a hwpt. Fixes: e8d57210035b ("iommufd: Add kAPI toward external drivers for physical devices") Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/hw_pagetable.c | 19 +++++++++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 1 + drivers/iommu/iommufd/main.c | 7 ++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index e0699d7f4c64af..2087b51d9807ad 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -25,6 +25,21 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj) refcount_dec(&hwpt->ioas->obj.users); } +void iommufd_hw_pagetable_abort(struct iommufd_object *obj) +{ + struct iommufd_hw_pagetable *hwpt = + container_of(obj, struct iommufd_hw_pagetable, obj); + + /* The ioas->mutex must be held until finalize is called. */ + lockdep_assert_held(&hwpt->ioas->mutex); + + if (!list_empty(&hwpt->hwpt_item)) { + list_del_init(&hwpt->hwpt_item); + iopt_table_remove_domain(&hwpt->ioas->iopt, hwpt->domain); + } + iommufd_hw_pagetable_destroy(obj); +} + int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt) { if (hwpt->enforce_cache_coherency) @@ -49,6 +64,10 @@ int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt) * Allocate a new iommu_domain and return it as a hw_pagetable. The HWPT * will be linked to the given ioas and upon return the underlying iommu_domain * is fully popoulated. + * + * The caller must hold the ioas->mutex until after + * iommufd_object_abort_and_destroy() or iommufd_object_finalize() is called on + * the returned hwpt. */ struct iommufd_hw_pagetable * iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 5ce9eaf5fb88e3..07d60d0640fee3 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -260,6 +260,7 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, struct iommufd_hw_pagetable * iommufd_hw_pagetable_detach(struct iommufd_device *idev); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); +void iommufd_hw_pagetable_abort(struct iommufd_object *obj); static inline void iommufd_hw_pagetable_put(struct iommufd_ctx *ictx, struct iommufd_hw_pagetable *hwpt) diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index e5ed5dfa91a0b5..24f30f384df6f9 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -24,6 +24,7 @@ struct iommufd_object_ops { void (*destroy)(struct iommufd_object *obj); + void (*abort)(struct iommufd_object *obj); }; static const struct iommufd_object_ops iommufd_object_ops[]; static struct miscdevice vfio_misc_dev; @@ -95,7 +96,10 @@ 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) { - iommufd_object_ops[obj->type].destroy(obj); + if (iommufd_object_ops[obj->type].abort) + iommufd_object_ops[obj->type].abort(obj); + else + iommufd_object_ops[obj->type].destroy(obj); iommufd_object_abort(ictx, obj); } @@ -401,6 +405,7 @@ static const struct iommufd_object_ops iommufd_object_ops[] = { }, [IOMMUFD_OBJ_HW_PAGETABLE] = { .destroy = iommufd_hw_pagetable_destroy, + .abort = iommufd_hw_pagetable_abort, }, #ifdef CONFIG_IOMMUFD_TEST [IOMMUFD_OBJ_SELFTEST] = {