From patchwork Thu Apr 20 18:51:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13219120 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 C24DBC7EE22 for ; Thu, 20 Apr 2023 18:52:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231809AbjDTSw1 (ORCPT ); Thu, 20 Apr 2023 14:52:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56534 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231897AbjDTSvy (ORCPT ); Thu, 20 Apr 2023 14:51:54 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89AB94221; Thu, 20 Apr 2023 11:51:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JSRmmjDvZjN4/WQuFtFBDDSUz7YqPADRW1M1dI/hgwm7pKDkSL0ErPkRNqZLbvUoNRQiDY1DxCcy3nmFG9NC66dl36eKurrxvTxS14/G8ngWmweEayLpeocOwnUZaWqtwewaEuWVFuz2AU2frOxVHs1LrNN7v/EskrP1ombyyJQKsjnQHsyqVyGkiTOAhXgKuihGtNRX5Po4SlVqa7ee/+WeHCMIwoxYklDlUBBBIA4B+GMAJgdKVBXW92yPQlxyy8BDD7fPqzMWnpVCl7MQFti1r0avtpa5b/iAkCo2sHxmXmvomOC6h17rDiHbtwESrvBlLEeFQVbP31apYx5NfQ== 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=jxa71Jdmk1BalfkQ1UgLAXDQ+nsULNuZsStUUfI6AN0=; b=akNENlJ48ZIvg8EXdxcgFhliye6i9Cjxg8CUpK/muXAJ4Fd7Jtov09ZiWjn9PXe/TXyPCoppkshNEOmEcDjQ/p29nnZPYgadZRSI0dA9NsT9xaizZ+lwcbyQlLF+otDkRZE5FPgYrGeGR1zuNMfhz9TFhXk+3W8+xS/u2x1abIbpucmkCEr9MqQD8pSLFy+dPFyBfE5yzIHOJpOSlOLMTYIrKtu/K/TiRBTL5c059rbotBaUtaDDXb2kJ86ugJq3q8xH4rBulFI5t8pQt+q3iqR6Si+XA0ZHiTC8GsuA+Jxs5tMEWvQEZC0yi0Eb+hbQQIcPLKjJ+Tj6VTh9I/GlFA== 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=jxa71Jdmk1BalfkQ1UgLAXDQ+nsULNuZsStUUfI6AN0=; b=guqR3rjZHAuDX7Afxqydklvs7/+mXzMia6i+9/fd1RYt7Luc2A9LtlZ2xTny50V1dMAzPgSdPtifViABpFATb+XukIJ5SG+2vTBFhtX/wfvKXwDZxbyA+qdGzvXrQVGMELUd4ZG1JKusXWjZkM+9/jzBAD9//dunsmL65czbZw2tKkL+pNRtFBln6R3Sg7kMSRGZGbOVvs2yvyh5hQunh30+MTnLmsEb31zBuXEPrOvk3ZqDF2GFai1ClNA9QArNI+YQCu31huNokRxodW+/vNsKPQb8Zb7a1bKEzDz0ZSFqjjt6CxhnWU3KvjXj6VSzD0ivUyfOV2RszzGflxPrdQ== 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 MW3PR12MB4553.namprd12.prod.outlook.com (2603:10b6:303:2c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.22; Thu, 20 Apr 2023 18:51:46 +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.6319.020; Thu, 20 Apr 2023 18:51:46 +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 v6 08/19] iommufd: Move putting a hwpt to a helper function Date: Thu, 20 Apr 2023 15:51:23 -0300 Message-Id: <8-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR14CA0024.namprd14.prod.outlook.com (2603:10b6:208:23e::29) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MW3PR12MB4553:EE_ X-MS-Office365-Filtering-Correlation-Id: 1b44d19a-3ccf-4434-1941-08db41d045a8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: k686SuqL2jAeIjxJBNifWI043FScc1OBgFvqBZKeUmczl+AEOZNJ+tEIo/iJyasGMDG4Lg2oRYbZSUZRhm9M0Ucf882kY99NeF+8tNYcQ7TDpkqYJ5rZYKXy23WVOls4K/6OaYIfvllxgd6HhA8S5+Y4JxYcI3XoW8qolZ8HU4YGg08e4QNEjFgnlaUpxf8CzG4GsRjwQzjCBDYmF1Rd/dclelxri3stJaillBHmF+L+2TJgVZpHxGtSIk9R7izai3f4bojhO2d5Aq5+KpXvZ4gtReN1PT11DHCuZH8DGNLDKqJX48oy27l0aQMl/d+/3dTMCup7ZQcLPUW5Lhw7KICVyDycxdmxa9H54NKsxVLuvgslN62XEowmvyy5Hja6jcUBgWLKVPwOu32Z9t1kkMu9ZAimzQSTzyUU/1JQ2FbyFV6MSZg1HwJS0c4hC3NEcvFHbrmJ+3qYoXFED9h9KHPK6g08SZAYqY/ReUSrYCuFVTIBppOfHkQdn33Bod/B8OaZ1wrcZSKCUwCs5N032cZbfRC+R+D/qqIl2KkNPSVvkvZDbMOZMFd+PHdaGPh3RfGJdHTe+VMeDyd0Ghay439NEepP0rK10J9yDPPXhsE= 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)(366004)(396003)(39860400002)(346002)(376002)(136003)(451199021)(54906003)(4326008)(316002)(66946007)(66556008)(66476007)(478600001)(6666004)(6486002)(5660300002)(8936002)(41300700001)(8676002)(2906002)(86362001)(36756003)(38100700002)(2616005)(6512007)(186003)(6506007)(26005)(83380400001)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: f9/dmvQlJuv0qKh5AlpId23BF1FDGM4aHe2p64m+ggbYcv8rbkHgq+Cd+kpjE4M+ZAo4nePNRaGslqGGlH34yUzx2xXPcVYieP09rPYSmp8sZ73Q8waPTRJ6rPGJVjVn1+npuNoQ1D5cLlXEtmi7mve9kYyf16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ZZwon24DcrFqtqc1FSglvhbg1CxevmlrBZ9xlWEJhWZqj5QwomyQI1qN/qummn2maboZJTeW8UQzVXa3y99I3OXqWxhpi01hOcGuYmSNR5HZsw4KXy/zXKHh6GJPajnckqTng6SvfVA/swzb44aYZagRWMWBkh3LPAoVZI+viuTn5Gasa8JoHfMz6tiTUFcmdDgtjMoPp5habhxpwgbhTjil0+NtQASmSk9Ou+7ID9lH85doha28hFbZaCpu40M/Ex0E2NLolWVYiR+o4PbMwNNMvGHI7DGf4p+c4bNw1Y5f5k//sz/QML7Z+BH1okrGJ6y2QsLX3xilRCYcdADE1BaRu0SMawJKEb9C1LB6kHEJGbWNgWLaZcbiRa X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1b44d19a-3ccf-4434-1941-08db41d045a8 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 18:51:38.2144 (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: N6lDaIWOjuayxLzzB3s0P9bywXki+4CSTJWDxmV6MRGp8IBGo7lt4LhVkrMpSp6J X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4553 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Next patch will need to call this from two places. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 6 +----- drivers/iommu/iommufd/iommufd_private.h | 10 ++++++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 59fca93bbd0c47..ca9776432c56e8 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -483,11 +483,7 @@ void iommufd_device_detach(struct iommufd_device *idev) struct iommufd_hw_pagetable *hwpt; hwpt = iommufd_hw_pagetable_detach(idev); - if (hwpt->auto_domain) - iommufd_object_destroy_user(idev->ictx, &hwpt->obj); - else - refcount_dec(&hwpt->obj.users); - + iommufd_hw_pagetable_put(idev->ictx, hwpt); refcount_dec(&idev->obj.users); } EXPORT_SYMBOL_NS_GPL(iommufd_device_detach, IOMMUFD); diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 7f4936cf537be4..8bf053f4d4a9ce 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -260,6 +260,16 @@ struct iommufd_hw_pagetable * iommufd_hw_pagetable_detach(struct iommufd_device *idev); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); +static inline void iommufd_hw_pagetable_put(struct iommufd_ctx *ictx, + struct iommufd_hw_pagetable *hwpt) +{ + lockdep_assert_not_held(&hwpt->ioas->mutex); + if (hwpt->auto_domain) + iommufd_object_destroy_user(ictx, &hwpt->obj); + else + refcount_dec(&hwpt->obj.users); +} + struct iommufd_group { struct kref ref; struct mutex lock;