From patchwork Mon Feb 6 09:05:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 53090 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2129332wrn; Mon, 6 Feb 2023 01:06:38 -0800 (PST) X-Google-Smtp-Source: AK7set9pilscLuZKqxnaGfDWcrhqgGqRbgE536jl8DoOFT3xhQxc8yM1PyK94rc5+Pzc/YVRzYwY X-Received: by 2002:a17:90b:3a8e:b0:219:9da5:40d3 with SMTP id om14-20020a17090b3a8e00b002199da540d3mr20241112pjb.1.1675674397749; Mon, 06 Feb 2023 01:06:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675674397; cv=none; d=google.com; s=arc-20160816; b=KfDlTadxcSovuzDU2s/3QPY3VtpPBsrODTqOkmfL0pbmfkEQOqXdKbEtlPOwK7uRGy lUqGJSkRyPmxHwCBperta+aBFjO9k/VCny2SvKWVy7LNd+aV1E4L9HFc/GNwv2U/2MrM yxSAcKcZB6BqMn+jn5vgFocOsDV/6f+VF+XRlGNqwvM6pvUbnDgLU17TbFBvNk28m3Uq 5Itm9bRJSnNgn7emku5gwXR+6c3btJ/P+vNnwhseialA4ojiF0CqZfa78gIUZ8bR6v7D KXzSgvtT7vB2N32sspKtKWOM8J59IIuLnZDSfBHS/YWRH7ESt8BHLvjzTgmopFM04M8N j8eg== 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=zlqrmYlC8OJg+RQoHZcumIYoQkwpZTq2pJgEk2x0vRo=; b=E1hKYGm9OD0ClM/6Brf+Z+sXrcy8/3f5KL0YbHLBsX3I55Ir1CR0obuVGN24tvD/3w x21jbvpmplSsFzKMvj+efqqQb+XgTbpAkE0Bzj726DCD3z81KnB9wb60bJgnEfh1RfeT BKD33zqlu0wrfoJHNkV0CP+7tFIR5gO038E28Z8JHCTJtF8wTFKSlEQwOVii0GXtaM/B /YXjyrOwLjOugW2f+06IOBcFiDF5D1cryG2k+jgqlw6ejrIsuR4mGbCpmBxiI/CEERLM eXZ/zrSn4RInCCIOPn4RfEoQbKdo7br9OOqDDmDpxjq3A9ehgw/SUrW2pDjU9AS5UvZv HZbQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=XPnMmq56; 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 k3-20020a633d03000000b0046ec058379bsi12008378pga.99.2023.02.06.01.06.24; Mon, 06 Feb 2023 01:06:37 -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=XPnMmq56; 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 S229998AbjBFJFv (ORCPT + 99 others); Mon, 6 Feb 2023 04:05:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44546 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229922AbjBFJFq (ORCPT ); Mon, 6 Feb 2023 04:05:46 -0500 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 015041D90B; Mon, 6 Feb 2023 01:05:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675674344; x=1707210344; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9qq1RhJ1l+oFUNHuWKmnEWcXvnHre5sEKNPwEJFtT9E=; b=XPnMmq56rE5dfUlC3AF1eQNq3kLpnt7y3r7LYQVEDM20sOlcvDCMrTLG LcclRYx257i4EFq1t17kFrwxL+ZG0cu84ikAbC90xmi5T2P7FWqglSHRR 7+NWafnJ0iYLi/ceoC+sG1beoHqdzkVUeGi3UBCNPPm6PtYX3GwSVoSO7 PF7Erl3xvlnMaXc8lx5VUuSl8/JBJ9OFL8K3mP72XbSvkyDB0BQuW9NP0 qjVdqtERFLZm+Y4FnEh581vq/AphiZ/zu4ibd2FWTMAF11bwMDODZ1A/i A3Lrajb+83aplxTZXzuQGf6YrjIdOyEGge2sG/lPwLEOzHyGHDxoTdXjE w==; X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="309495793" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="309495793" 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:43 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="911862782" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="911862782" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 06 Feb 2023 01:05:40 -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 03/14] vfio: Accept vfio device file in the driver facing kAPI Date: Mon, 6 Feb 2023 01:05:21 -0800 Message-Id: <20230206090532.95598-4-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?1757071957399016399?= X-GMAIL-MSGID: =?utf-8?q?1757071957399016399?= This makes the vfio file kAPIs to accepte vfio device files, also a preparation for vfio device cdev support. For the kvm set with vfio device file, kvm pointer is stored in struct vfio_device_file, and use kvm_ref_lock to protect kvm set and kvm pointer usage within VFIO. This kvm pointer will be set to vfio_device after device file is bound to iommufd in the cdev path. Signed-off-by: Yi Liu Reviewed-by: Kevin Tian --- drivers/vfio/vfio.h | 2 ++ drivers/vfio/vfio_main.c | 51 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index 54195b107b45..00e6ce6ef4c9 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -18,6 +18,8 @@ struct vfio_container; struct vfio_device_file { struct vfio_device *device; + spinlock_t kvm_ref_lock; /* protect kvm field */ + struct kvm *kvm; }; void vfio_device_put_registration(struct vfio_device *device); diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 8612ba112e7f..c529f609fecc 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -406,6 +406,7 @@ vfio_allocate_device_file(struct vfio_device *device) return ERR_PTR(-ENOMEM); df->device = device; + spin_lock_init(&df->kvm_ref_lock); return df; } @@ -1167,13 +1168,23 @@ const struct file_operations vfio_device_fops = { .mmap = vfio_device_fops_mmap, }; +static struct vfio_device *vfio_device_from_file(struct file *file) +{ + struct vfio_device_file *df = file->private_data; + + if (file->f_op != &vfio_device_fops) + return NULL; + return df->device; +} + /** * vfio_file_is_valid - True if the file is usable with VFIO APIS * @file: VFIO group file or VFIO device file */ bool vfio_file_is_valid(struct file *file) { - return vfio_group_from_file(file); + return vfio_group_from_file(file) || + vfio_device_from_file(file); } EXPORT_SYMBOL_GPL(vfio_file_is_valid); @@ -1188,15 +1199,36 @@ EXPORT_SYMBOL_GPL(vfio_file_is_valid); */ bool vfio_file_enforced_coherent(struct file *file) { - struct vfio_group *group = vfio_group_from_file(file); + struct vfio_group *group; + struct vfio_device *device; + group = vfio_group_from_file(file); if (group) return vfio_group_enforced_coherent(group); + device = vfio_device_from_file(file); + if (device) + return device_iommu_capable(device->dev, + IOMMU_CAP_ENFORCE_CACHE_COHERENCY); + return true; } EXPORT_SYMBOL_GPL(vfio_file_enforced_coherent); +static void vfio_device_file_set_kvm(struct file *file, struct kvm *kvm) +{ + struct vfio_device_file *df = file->private_data; + + /* + * The kvm is first recorded in the vfio_device_file, and will + * be propagated to vfio_device::kvm when the file is bound to + * iommufd successfully in the vfio device cdev path. + */ + spin_lock(&df->kvm_ref_lock); + df->kvm = kvm; + spin_unlock(&df->kvm_ref_lock); +} + /** * vfio_file_set_kvm - Link a kvm with VFIO drivers * @file: VFIO group file or VFIO device file @@ -1205,10 +1237,14 @@ EXPORT_SYMBOL_GPL(vfio_file_enforced_coherent); */ void vfio_file_set_kvm(struct file *file, struct kvm *kvm) { - struct vfio_group *group = vfio_group_from_file(file); + struct vfio_group *group; + group = vfio_group_from_file(file); if (group) vfio_group_set_kvm(group, kvm); + + if (vfio_device_from_file(file)) + vfio_device_file_set_kvm(file, kvm); } EXPORT_SYMBOL_GPL(vfio_file_set_kvm); @@ -1221,10 +1257,17 @@ EXPORT_SYMBOL_GPL(vfio_file_set_kvm); */ bool vfio_file_has_dev(struct file *file, struct vfio_device *device) { - struct vfio_group *group = vfio_group_from_file(file); + struct vfio_group *group; + struct vfio_device *vdev; + group = vfio_group_from_file(file); if (group) return vfio_group_has_dev(group, device); + + vdev = vfio_device_from_file(file); + if (device) + return vdev == device; + return false; } EXPORT_SYMBOL_GPL(vfio_file_has_dev);