From patchwork Mon Jul 17 18:12:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13316204 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 30ADCC001DF for ; Mon, 17 Jul 2023 18:13:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231432AbjGQSNi (ORCPT ); Mon, 17 Jul 2023 14:13:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231208AbjGQSNd (ORCPT ); Mon, 17 Jul 2023 14:13:33 -0400 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on20603.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e8d::603]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 007CE1725; Mon, 17 Jul 2023 11:13:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LLpfK5T6znm50Yk6cNUSz0rT0NGRHfshg4qUdJVmG3nAG5WYe6cN0lRq/MWkm+d/Arz8G4qB4SnMzAJ2RtCuGY90UMvMf/dxVlN1NfElYn+ZRWHKJ65/YiHeEarN5ca5aVTx7pYF1zPwVV79A6yTAbnKnUbw6DShE8tFgbX4WjtEJEyw6tiLkmjjOsCcBYpbpyMGotoU+Rir4f6fV/v4cqnvR34LFi/bIHvJToPJI9wQCkt2yjZ/ISOUZKXlIgzs5VE1I7hTtdI6E95TbEngnRHYy2itB1c2NUUaL6Won5P12qnJaznESZs6hUlvlGzctyTa3Cp0uut+APQS8okVlQ== 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=TUZiQxZj61NoJJo30CvAa+GoDn8iIdPqmROF3I7g6d4=; b=fae4Oe8LeFjiqAx4XGKj4Smkid1lF9gIhzDYibjqjfRj2RBAQZzp+DxtniF9xcoRmQ7TB+61gU97K+jYAJm8E5x0anFWOCteU8Utmh30aGI8pkLOcJHrPVOfK0WEgHkoEigCDMhJD+W2FNZtCU0dZduXnvaJYwoJ0DA+nuj+s3exe8cw4HTQU9NjrUdSHnVupklGXvrs0eu4I7pU/zKAbXuIm54Ljh474EpPfn9jVL0m6OjrK1Shgoc0umqvbaYjlUQ2jsu9hIJTE0IuRRQ7YULEkimfYwiWQJuUcdCvsC1Sgl6TPW1RYRdkzLizuAar89sHfXBMNec6vf4guwlUgw== 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=TUZiQxZj61NoJJo30CvAa+GoDn8iIdPqmROF3I7g6d4=; b=sB/NlVwc17qZ/AV9+P9dsCrargVuBoNx8pohiVJQHp/57lRDcTegoNHEVPQKjlTIPg5XI/2iCEHpc/0IcAnBRWJTVB3/4XKIhWrJrzDtTMC10N+3ndHNIRkwQ1sDeZ52MDXfOJJ62028obHzNQFATxeIa5Nm0rON6JB/WnH8eRgYoId5Rf7I0JMCqKrQCwIYPUWyB0tCWZJQXHO74XogWWdR4sTSj7TkjaArXZqRZ8KiuUvr56ESzUs9+JFobGYRu4bae/zaYMWpw46BBs9pADf22lPktdFcZko9KDgLJtFajxpC4yxDTFZg+F9r7d2I43L8BEDVAtWKamx8u1969g== 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 SJ2PR12MB9137.namprd12.prod.outlook.com (2603:10b6:a03:562::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6588.31; Mon, 17 Jul 2023 18:12:21 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::5111:16e8:5afe:1da1%6]) with mapi id 15.20.6588.031; Mon, 17 Jul 2023 18:12:21 +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 v8 08/19] iommufd: Move putting a hwpt to a helper function Date: Mon, 17 Jul 2023 15:12:04 -0300 Message-ID: <8-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v8-6659224517ea+532-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: SJ0PR03CA0032.namprd03.prod.outlook.com (2603:10b6:a03:33e::7) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ2PR12MB9137:EE_ X-MS-Office365-Filtering-Correlation-Id: d10d9043-77c7-49a7-11ad-08db86f15b78 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: UP8QJK0DvbfcM8nh0c1yczfklJqwUQx/j8S1QByWsE4xxcl4b8hPm//2Bf620pRdWDxuTVoudUfAaZmSQ9+KMOx0NrU4H7H8b+TP7YMYdZW0cBstz6XqaMbLmFk+9jS7HyL+kYvJ4x7PuBuNXDtQl6YFDOgvwkqBHEZsKlLiVP+bB6xWgK425irklIvGgCLifLoZtIrnKhN4h0vul89qGUA40SCMKBN8zQPl4NcjLxFnrZDsN9RXwKkTPWUHWeL/PAuLGJuTKF9onIz+w73NUmiCMFDPO0uA7DyD8L2ZQ4HnS9IUMXS4QrTFdy6XVi3azg49C86BIelptr/8sO7C5noahUB5fFOxLk4cwwt6fDpP07P65EkYyiLuKXvC3tlqZGYHvYs8KBhS/dFzSLjcS59t81cL3AASYxJdSr8VXis1mKsXwDWX9gCacPisLBopBJ8pB3DB5ypHbPiFGdR6G6wAO8c6a2HiRtTiICt2EEnjqOVuQqYeMN2qPlZBo1/NJF5/HP8VA/nNQWxSEbNoDHIGFl5KIy8RJayEUJ6OvAhgU3FXHBQLjZPwFsom8thvKJ1qW/StDJLVz7DrYLE3vBa+Ld+2D+fatWq/jOAyGuA= 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)(376002)(136003)(39860400002)(396003)(346002)(451199021)(86362001)(2906002)(36756003)(6512007)(186003)(83380400001)(6506007)(26005)(38100700002)(6666004)(6486002)(54906003)(316002)(5660300002)(66476007)(66946007)(66556008)(2616005)(4326008)(478600001)(41300700001)(8936002)(8676002)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: BrhOTk8do0e7Jck8W0uPovWTbFUOMnXBsmYgN8ZK69UIm5PUEg0nKbAraIIYlRoklCYodAiQUE2VHyfQl9AIBLP0JwE0J+EAo6s+YyQNVuxYaz78oBpI7JmWgVZYgNFqcChHkIswaAjoZ1PIuhjgwTPsW7XXm1OHx7gi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vNooSJ1TTen6+BBHVxKZC0yMVdHqs3Mg3pBwsF1WLASk9dLcZZHIkjE453/Y5rXqCXkeDbiD1vSqYVleyj6b1OIGAfYZ64KSCs9gZhtrzcVel1b5FEfd9NVJoQm+0dshi6VdTaY1/yYFP0aj6nBwzOzP+av7UdF7qvZDgBXFfCqyqLKpg9O7Nw7BMi0PDgFc8bxWUC5DT6Z/Q289meJxNNk/RZMTtIWW5ht58+NXEAWpcscK8tPMLxOH4kvZptoqmGA6dje3n1xLi2JaXkS3vEBVqTLEssaEs3P0Nn06ibc8k9PCY/NJ5AGzHRg3OCdNJa0xrP2tRtPHAEx4j3hSlxQmVyrYAM0SaS9f X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d10d9043-77c7-49a7-11ad-08db86f15b78 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 Jul 2023 18:12:18.4221 (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: VSQAZfYDkM3mOUQVEoFvM5Ty66oG54aaFRt+3bcKhosDLyISL/wcqJTd8IP+Ph4c X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9137 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 Tested-by: Nicolin Chen 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 0ca9fdab07dfdc..7db5df11ad9be3 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 9ede238908e7b3..63b2587a37f23d 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;