From patchwork Mon Feb 6 09:05:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 53092 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2129495wrn; Mon, 6 Feb 2023 01:07:07 -0800 (PST) X-Google-Smtp-Source: AK7set8X8UEFDWnGB4nYZfJdqGvgNzmgcIS0+fjG0Q+r+Le/t3fHqF3N8ccR1hKDEnkm9nq1AiXr X-Received: by 2002:a17:903:41cf:b0:198:d004:bfa0 with SMTP id u15-20020a17090341cf00b00198d004bfa0mr16531198ple.15.1675674427558; Mon, 06 Feb 2023 01:07:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675674427; cv=none; d=google.com; s=arc-20160816; b=aVRSeL256glaVsJ8/eRrIAMjT8CYHOhqy3n7u1271jA6zXO62qr4L7CfLaURBiST5H sHbJfZjkTvfCieVslTrZpvBVCphrjjjjZWLfKuznBEGkX+yL12QaVPns/rnANJyI830z N5nvNxHhMSjjBuE8VJA9wk63Rk3AF+EWf7ZKIdzGj1G66omDeP8e1jMkXTA+jIrOFmzK IP/QCoTBzhNmtVa09m8l6OSqx1zcy4YchxVMpm3BvcZPZ24AKWUivxJZxFpBYuIdlwFL zupvNBMq8dgpur5PbxDe/x75/gJla9moFrsbPKFntQ2aLOy1ZfhT7ToQwLUOoYaWOY81 KnLQ== 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=pBMlJdra9M4jLlprfOlwj4BPd/pGPu8e5KSr8zIBQ08=; b=sGmM93Eo+dgYBbyENdl31huBth/EdrV6AbjLecc2ffShgRsI1v2qKzdpQDyb3KY7y1 rsYUh5aM6tTLQ40cFQ7Nu4OnqwfRhdDqBsLNaKrhcOkQr9Q/irtOK1V87ri+wAV2ACQ1 +ZiAe9Y5zKKoenNkFKhzqC/NvySkoh4DGI9RPtViU7DS5OZ+Y0ALYw5kElg0Tgy0GXq0 HOJNj/0rm08D+tkEJceI/XWb8XcLGGa/hXQEjg0CbxB6naHuH4jgBvl7867dGQVx8kwM 3Nnr5bUwJQjJYkXewK4exiOazS5pbb+YU2/bAX1EHjWfFaQGIiLuat8Yzi7/2/I0qZug tg/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HWicyGK2; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y18-20020a170902e19200b00198dd36c9ccsi2160392pla.47.2023.02.06.01.06.55; Mon, 06 Feb 2023 01:07:07 -0800 (PST) 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=@intel.com header.s=Intel header.b=HWicyGK2; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230048AbjBFJGK (ORCPT + 99 others); Mon, 6 Feb 2023 04:06:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45458 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230046AbjBFJGD (ORCPT ); Mon, 6 Feb 2023 04:06:03 -0500 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93ED71E9DE; Mon, 6 Feb 2023 01:05:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675674352; x=1707210352; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=yKY3wvjzmMI5WNTrF/yXkh7BE3+bK9VyWU1+b1Cz2sM=; b=HWicyGK276hgGJwIS0wYTU17hyKcRWpoxotjaPnAgW6OQ5fDDiILYjdX 1IlMCIlkN9KUXKnlOe4tHbx86SAcwndUQZZW1UKF29HfGgKFxU3ybtF/7 lNCmp6kOI1TeX8M8shWkXQuTQwSgMee+GsrUjWaGY+seYRS8sKPxuIpO+ yzbeA1t5XmAV4aMVt0gVrL73AIzTwEMh4dqwB2rweWHf3V5kcCJ1LdDpC K5xv/+WMo2Ogc0HFcxqw3IkLq4QiZo/xm5Y9iya/H1pJuhDeqXkhWJkbI AyhX7EfNxlrYMZ+d+HT0VgMIM/eCBHbC3RI6S/HC4IIBqGK1ClCjact7I Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="309495835" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="309495835" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Feb 2023 01:05:51 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="911862820" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="911862820" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 06 Feb 2023 01:05:50 -0800 From: Yi Liu To: alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@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, intel-gvt-dev@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org Subject: [PATCH v2 06/14] vfio: Pass struct vfio_device_file * to vfio_device_open/close() Date: Mon, 6 Feb 2023 01:05:24 -0800 Message-Id: <20230206090532.95598-7-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230206090532.95598-1-yi.l.liu@intel.com> References: <20230206090532.95598-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham 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?1757071988795381208?= X-GMAIL-MSGID: =?utf-8?q?1757071988795381208?= This avoids passing too much parameters in multiple functions. Signed-off-by: Yi Liu Reviewed-by: Kevin Tian --- drivers/vfio/group.c | 19 +++++++++++++------ drivers/vfio/vfio.h | 8 ++++---- drivers/vfio/vfio_main.c | 25 +++++++++++++++---------- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/drivers/vfio/group.c b/drivers/vfio/group.c index cc0eded19a9f..2abf55c69281 100644 --- a/drivers/vfio/group.c +++ b/drivers/vfio/group.c @@ -166,8 +166,9 @@ static void vfio_device_group_get_kvm_safe(struct vfio_device *device) spin_unlock(&device->group->kvm_ref_lock); } -static int vfio_device_group_open(struct vfio_device *device) +static int vfio_device_group_open(struct vfio_device_file *df) { + struct vfio_device *device = df->device; int ret; mutex_lock(&device->group->group_lock); @@ -187,7 +188,11 @@ static int vfio_device_group_open(struct vfio_device *device) if (device->open_count == 0) vfio_device_group_get_kvm_safe(device); - ret = vfio_device_open(device, device->group->iommufd); + df->iommufd = device->group->iommufd; + + ret = vfio_device_open(df); + if (ret) + df->iommufd = NULL; if (device->open_count == 0) vfio_device_put_kvm(device); @@ -199,12 +204,14 @@ static int vfio_device_group_open(struct vfio_device *device) return ret; } -void vfio_device_group_close(struct vfio_device *device) +void vfio_device_group_close(struct vfio_device_file *df) { + struct vfio_device *device = df->device; + mutex_lock(&device->group->group_lock); mutex_lock(&device->dev_set->lock); - vfio_device_close(device, device->group->iommufd); + vfio_device_close(df); if (device->open_count == 0) vfio_device_put_kvm(device); @@ -225,7 +232,7 @@ static struct file *vfio_device_open_file(struct vfio_device *device) goto err_out; } - ret = vfio_device_group_open(device); + ret = vfio_device_group_open(df); if (ret) goto err_free; @@ -257,7 +264,7 @@ static struct file *vfio_device_open_file(struct vfio_device *device) return filep; err_close_device: - vfio_device_group_close(device); + vfio_device_group_close(df); err_free: kfree(df); err_out: diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index 00e6ce6ef4c9..d8275881c1f1 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -20,13 +20,13 @@ struct vfio_device_file { struct vfio_device *device; spinlock_t kvm_ref_lock; /* protect kvm field */ struct kvm *kvm; + struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */ }; void vfio_device_put_registration(struct vfio_device *device); bool vfio_device_try_get_registration(struct vfio_device *device); -int vfio_device_open(struct vfio_device *device, struct iommufd_ctx *iommufd); -void vfio_device_close(struct vfio_device *device, - struct iommufd_ctx *iommufd); +int vfio_device_open(struct vfio_device_file *df); +void vfio_device_close(struct vfio_device_file *df); struct vfio_device_file * vfio_allocate_device_file(struct vfio_device *device); @@ -91,7 +91,7 @@ void vfio_device_group_register(struct vfio_device *device); void vfio_device_group_unregister(struct vfio_device *device); int vfio_device_group_use_iommu(struct vfio_device *device); void vfio_device_group_unuse_iommu(struct vfio_device *device); -void vfio_device_group_close(struct vfio_device *device); +void vfio_device_group_close(struct vfio_device_file *df); struct vfio_group *vfio_group_from_file(struct file *file); bool vfio_group_enforced_coherent(struct vfio_group *group); void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm); diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index c529f609fecc..c517252aba19 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -411,9 +411,10 @@ vfio_allocate_device_file(struct vfio_device *device) return df; } -static int vfio_device_first_open(struct vfio_device *device, - struct iommufd_ctx *iommufd) +static int vfio_device_first_open(struct vfio_device_file *df) { + struct vfio_device *device = df->device; + struct iommufd_ctx *iommufd = df->iommufd; int ret; lockdep_assert_held(&device->dev_set->lock); @@ -445,9 +446,11 @@ static int vfio_device_first_open(struct vfio_device *device, return ret; } -static void vfio_device_last_close(struct vfio_device *device, - struct iommufd_ctx *iommufd) +static void vfio_device_last_close(struct vfio_device_file *df) { + struct vfio_device *device = df->device; + struct iommufd_ctx *iommufd = df->iommufd; + lockdep_assert_held(&device->dev_set->lock); if (device->ops->close_device) @@ -459,15 +462,16 @@ static void vfio_device_last_close(struct vfio_device *device, module_put(device->dev->driver->owner); } -int vfio_device_open(struct vfio_device *device, struct iommufd_ctx *iommufd) +int vfio_device_open(struct vfio_device_file *df) { + struct vfio_device *device = df->device; int ret = 0; lockdep_assert_held(&device->dev_set->lock); device->open_count++; if (device->open_count == 1) { - ret = vfio_device_first_open(device, iommufd); + ret = vfio_device_first_open(df); if (ret) device->open_count--; } @@ -475,14 +479,15 @@ int vfio_device_open(struct vfio_device *device, struct iommufd_ctx *iommufd) return ret; } -void vfio_device_close(struct vfio_device *device, - struct iommufd_ctx *iommufd) +void vfio_device_close(struct vfio_device_file *df) { + struct vfio_device *device = df->device; + lockdep_assert_held(&device->dev_set->lock); vfio_assert_device_open(device); if (device->open_count == 1) - vfio_device_last_close(device, iommufd); + vfio_device_last_close(df); device->open_count--; } @@ -527,7 +532,7 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep) struct vfio_device_file *df = filep->private_data; struct vfio_device *device = df->device; - vfio_device_group_close(device); + vfio_device_group_close(df); vfio_device_put_registration(device);