Received: by 2002:a05:7412:da14:b0:e2:908c:2ebd with SMTP id fe20csp1761850rdb; Mon, 9 Oct 2023 01:53:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFH4Q2oT+nW/RIGISUqDz7G5WEpw44G171NGemzFcJjeAsZ+qPlBfJWHBM5gdxKHTsyS1f+ X-Received: by 2002:a05:6871:5cc:b0:1d6:4641:9172 with SMTP id v12-20020a05687105cc00b001d646419172mr15884179oan.48.1696841590793; Mon, 09 Oct 2023 01:53:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696841590; cv=none; d=google.com; s=arc-20160816; b=jc1VhVTS02H+5qALzC76ooFK0uHdtaRXz9uzt/jk7+UzrJt3SPk3nJRoIawxKElyHm 6VkdIX+A+478yC2WvxLVRzW7uFT6D427a4PFVLC/Dk5NkcOC56bHV4D/fa90ocDJMS5b csb6Nr/kOfQcCZoQKyKt1aYuHy3SBgvG9owLcjpCpsc3J4D/Li7GZVLrx/bwjYZXDiLk +b8LPyqYVfYjmSPGw3XdbG4kIjEa2Rm9u5cPqX/JOT2CCFBi+pCsUtJlt2pS+0ov/ljP Qx6X2NSDGO0GPI9amk/3oFjGvsTCeQ/T4ofUD3dSe/yLqyulb5ONd42eJraQyA1KUbcU LtRw== ARC-Message-Signature: i=1; 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=pkkyMlGYde1x3unODyTZ8PmkhvkbExjoFmGt4DXZAIY=; fh=ncJBVmsnOSqrX1O37yfYEzicwaA2e7ARxnsU7aiysyE=; b=VrBNdkhU8umMn/q/L+JhI3vrPgL3UhRuUc8pdIpHGS7kqMat+FfOb0epEVjVtM8UfF Sgpsitg+kL9RpAGqDyQI96xNKvhQQBM6PbhNgX0GXSxfN8EvjFkSdvOUJMe41mcnjwLy IPfXdJW9800vDFkHbCkbs2LXkrtHFiR9sJ5Rz4keOzIhgbSutZ05P8RJkooMxqCy5RSq oyvQVrovlLIpDlakjBc9Updl4WEhW63ATFjwFWEDYRsXEww/vJllGaGK/CvUEms3Cc/P wuM8nuSArdBLiZNrAPpTxIJRZus4XuJsXhnETh2M+U+HU73Dp2aDSeBdFD77rwUaslS4 uRxw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=TwDfRf4Y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id f11-20020a056a0022cb00b00690de92ffe3si7268865pfj.309.2023.10.09.01.53.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 01:53:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=TwDfRf4Y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 06AEC80D2AFE; Mon, 9 Oct 2023 01:52:59 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345654AbjJIIwb (ORCPT + 99 others); Mon, 9 Oct 2023 04:52:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345591AbjJIIvf (ORCPT ); Mon, 9 Oct 2023 04:51:35 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6496AB; Mon, 9 Oct 2023 01:51:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1696841490; x=1728377490; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ZrNdz5dSYF7ymp/w9UQeOTkhWG7Zhjt6N1Zo2oVnAYQ=; b=TwDfRf4YaMU0IFGjwo7TK8AAhvaEtsbMmJwBWNvYXQypgwq8RzWPewIn zEx2B77jlwIKezJzq0c7ThE6DaCNcJWKnAa9tleWb6+DB+43WnoGn16iA 95T7YKQ1f7n+RDOyjeSgR2LBpiSbcCSsOHQ4rqBGVot8qKg0fNJKI+Zsc ZDRkXRprZkI6mW/F9+fNNxom6IvWKZfkVkXiD7DlvzlwE/P31UOvcmJKo SLcIL6OBovvci26TXwAtxod5edHqV5vrYWSRFAKgdF9MgzUwEpIvZwxOT d7j2YpLkSSbuqE1aYSrzKXAxdzJ8lC2+nsRpbVo/aCgIfN0i+avb7oNeq g==; X-IronPort-AV: E=McAfee;i="6600,9927,10857"; a="364390781" X-IronPort-AV: E=Sophos;i="6.03,209,1694761200"; d="scan'208";a="364390781" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Oct 2023 01:51:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10857"; a="818781660" X-IronPort-AV: E=Sophos;i="6.03,209,1694761200"; d="scan'208";a="818781660" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga008.fm.intel.com with ESMTP; 09 Oct 2023 01:51:29 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com, joao.m.martins@oracle.com Subject: [RFC 2/7] iommufd: Introduce iommufd_alloc_device() Date: Mon, 9 Oct 2023 01:51:18 -0700 Message-Id: <20231009085123.463179-3-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231009085123.463179-1-yi.l.liu@intel.com> References: <20231009085123.463179-1-yi.l.liu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=2.7 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Mon, 09 Oct 2023 01:52:59 -0700 (PDT) X-Spam-Level: ** From: Kevin Tian This abstracts the common logic used in the iommufd_device_bind() and the later iommufd_device_bind_pasid() to be helper. Signed-off-by: Kevin Tian Signed-off-by: Yi Liu --- drivers/iommu/iommufd/device.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index ca3919fecc89..9dd76d92b7f6 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -164,6 +164,27 @@ static int iommufd_allow_unsafe_interrupts(struct device *dev) return 0; } +static struct iommufd_device *iommufd_alloc_device(struct iommufd_ctx *ictx, + struct device *dev) +{ + struct iommufd_device *idev; + + idev = iommufd_object_alloc(ictx, idev, IOMMUFD_OBJ_DEVICE); + if (IS_ERR(idev)) + return idev; + idev->ictx = ictx; + if (!iommufd_selftest_is_mock_dev(dev)) + iommufd_ctx_get(ictx); + idev->dev = dev; + idev->enforce_cache_coherency = + device_iommu_capable(dev, IOMMU_CAP_ENFORCE_CACHE_COHERENCY); + xa_init(&idev->pasid_hwpts); + + /* The calling driver is a user until iommufd_device_unbind() */ + refcount_inc(&idev->obj.users); + return idev; +} + /** * iommufd_device_bind - Bind a physical device to an iommu fd * @ictx: iommufd file descriptor @@ -209,24 +230,15 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, if (rc) goto out_group_put; - idev = iommufd_object_alloc(ictx, idev, IOMMUFD_OBJ_DEVICE); + idev = iommufd_alloc_device(ictx, dev); if (IS_ERR(idev)) { rc = PTR_ERR(idev); goto out_release_owner; } - idev->ictx = ictx; - if (!iommufd_selftest_is_mock_dev(dev)) - iommufd_ctx_get(ictx); - idev->dev = dev; - idev->enforce_cache_coherency = - device_iommu_capable(dev, IOMMU_CAP_ENFORCE_CACHE_COHERENCY); - /* The calling driver is a user until iommufd_device_unbind() */ - refcount_inc(&idev->obj.users); + /* igroup refcount moves into iommufd_device */ idev->igroup = igroup; - xa_init(&idev->pasid_hwpts); - /* * If the caller fails after this success it must call * iommufd_unbind_device() which is safe since we hold this refcount. -- 2.34.1