Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp5047612ybe; Tue, 17 Sep 2019 01:30:45 -0700 (PDT) X-Google-Smtp-Source: APXvYqxHRjdSX62kTUYzlFUrhUXigOLYP8HL0uH5wp5CMggoiAfpM/f8m9Rz+zLKQahZ2ZARO35O X-Received: by 2002:a17:906:76c2:: with SMTP id q2mr3702521ejn.202.1568709045470; Tue, 17 Sep 2019 01:30:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568709045; cv=none; d=google.com; s=arc-20160816; b=cA02gdZH+mTSREhDGYWml3jkQX9ZXh/ArU2wuiQEjdBW5pu4q9YmZ42DP4K0Y52Znq b4py+6s9TaMMnZ0nIvY3JK3NjAuIJB2m6EGruRd+K++mgRgzeYs7pYpHFx6fSfgeFMEJ yESpxXhVOf1L07H8Yj9KYJNXjyOIfI9YgUI8r4b7VPv8Xxi4EfdzJz+bbs9KuB15KolZ k95C5gB2c/sdgir05dUS1//NyHRa1vazOKE+IoaRLyTfxdvpA8If746qdGzC1p0kjQoG rc1bqZiRU21t0PzMb/XTrLLH0wdClp8rojfbuWE2jm4fa7bh5Oq2gaUxy9NtaDEegTR8 iBSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=bnqOYKyiDGrd0oV5pniqDLM3vUHPvGTMDiClTv3uHdw=; b=VqC5DFNM3WC3OJnhgMci2BJogEF47+cZumC/r2pQjEbmr0yVQFGYoanRV65yQqfopn 7y6W4zsnctMIc7Uq4PE2vM5w5+e3dJxJk1rG8XMABpFVt5gNLTuB2WeLjXKP7gpKrvUs UC52qt4p7CedM2RsQgUs/ov4ab5CIXU5O9ffl6OHAfV3wIjLZfvl4+Dvy5kfVuiMsOhO 06Xf5qxolvq3Dlq3Op4Ii76jy4x981jbGVWICGS55Axsh78DuryaSwHTRMd66AjffG/8 mT89JW/kiovY62QtMQKPMktWPUD1wI4TUpenzIA7KJHwkKSTq/pgpy7q83IYSqKgu791 BNeA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b5si725792ejd.158.2019.09.17.01.30.22; Tue, 17 Sep 2019 01:30:45 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390720AbfIQBFC (ORCPT + 99 others); Mon, 16 Sep 2019 21:05:02 -0400 Received: from mga04.intel.com ([192.55.52.120]:34819 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727996AbfIQBFB (ORCPT ); Mon, 16 Sep 2019 21:05:01 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 16 Sep 2019 18:05:01 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,514,1559545200"; d="scan'208";a="180611948" Received: from dpdk-virtio-tbie-2.sh.intel.com ([10.67.104.71]) by orsmga008.jf.intel.com with ESMTP; 16 Sep 2019 18:04:58 -0700 From: Tiwei Bie To: mst@redhat.com, jasowang@redhat.com, alex.williamson@redhat.com, maxime.coquelin@redhat.com Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, dan.daly@intel.com, cunming.liang@intel.com, zhihong.wang@intel.com, lingshan.zhu@intel.com, tiwei.bie@intel.com Subject: [RFC v4 1/3] vfio: support getting vfio device from device fd Date: Tue, 17 Sep 2019 09:02:02 +0800 Message-Id: <20190917010204.30376-2-tiwei.bie@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190917010204.30376-1-tiwei.bie@intel.com> References: <20190917010204.30376-1-tiwei.bie@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch introduces the support for getting VFIO device from VFIO device fd. With this support, it's possible for vhost to get VFIO device from the group fd and device fd set by the userspace. Signed-off-by: Tiwei Bie --- drivers/vfio/vfio.c | 25 +++++++++++++++++++++++++ include/linux/vfio.h | 4 ++++ 2 files changed, 29 insertions(+) diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c index 388597930b64..697fd079bb3f 100644 --- a/drivers/vfio/vfio.c +++ b/drivers/vfio/vfio.c @@ -890,6 +890,31 @@ static struct vfio_device *vfio_device_get_from_name(struct vfio_group *group, return device; } +struct vfio_device *vfio_device_get_from_fd(struct vfio_group *group, + int device_fd) +{ + struct fd f; + struct vfio_device *it, *device = ERR_PTR(-ENODEV); + + f = fdget(device_fd); + if (!f.file) + return ERR_PTR(-EBADF); + + mutex_lock(&group->device_lock); + list_for_each_entry(it, &group->device_list, group_next) { + if (it == f.file->private_data) { + device = it; + vfio_device_get(device); + break; + } + } + mutex_unlock(&group->device_lock); + + fdput(f); + return device; +} +EXPORT_SYMBOL_GPL(vfio_device_get_from_fd); + /* * Caller must hold a reference to the vfio_device */ diff --git a/include/linux/vfio.h b/include/linux/vfio.h index e42a711a2800..e75b24fd7c5c 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -15,6 +15,8 @@ #include #include +struct vfio_group; + /** * struct vfio_device_ops - VFIO bus driver device callbacks * @@ -50,6 +52,8 @@ extern int vfio_add_group_dev(struct device *dev, extern void *vfio_del_group_dev(struct device *dev); extern struct vfio_device *vfio_device_get_from_dev(struct device *dev); +extern struct vfio_device *vfio_device_get_from_fd(struct vfio_group *group, + int device_fd); extern void vfio_device_put(struct vfio_device *device); extern void *vfio_device_data(struct vfio_device *device); -- 2.17.1