linux-s390.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alex Williamson <alex.williamson@redhat.com>
To: Yi Liu <yi.l.liu@intel.com>
Cc: jgg@nvidia.com, kevin.tian@intel.com, joro@8bytes.org,
	robin.murphy@arm.com, 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.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-s390@vger.kernel.org,
	xudong.hao@intel.com, yan.y.zhao@intel.com,
	terrence.xu@intel.com, yanting.jiang@intel.com
Subject: Re: [PATCH v8 20/24] vfio: Add cdev for vfio_device
Date: Wed, 29 Mar 2023 13:57:19 -0600	[thread overview]
Message-ID: <20230329135719.22ac6c12.alex.williamson@redhat.com> (raw)
In-Reply-To: <20230327094047.47215-21-yi.l.liu@intel.com>

On Mon, 27 Mar 2023 02:40:43 -0700
Yi Liu <yi.l.liu@intel.com> wrote:

> This allows user to directly open a vfio device w/o using the legacy
> container/group interface, as a prerequisite for supporting new iommu
> features like nested translation.
> 
> The device fd opened in this manner doesn't have the capability to access
> the device as the fops open() doesn't open the device until the successful
> BIND_IOMMUFD which be added in next patch.
> 
> With this patch, devices registered to vfio core have both group and device
> interface created.
> 
> - group interface : /dev/vfio/$groupID
> - device interface: /dev/vfio/devices/vfioX - normal device
> 		    /dev/vfio/devices/noiommu-vfioX - noiommu device
> 		    ("X" is the minor number and is unique across devices)
> 
> Given a vfio device the user can identify the matching vfioX by checking
> the sysfs path of the device. Take PCI device (0000:6a:01.0) for example,
> /sys/bus/pci/devices/0000\:6a\:01.0/vfio-dev/vfio0/dev contains the
> major:minor of the matching vfioX.
> 
> Userspace then opens the /dev/vfio/devices/vfioX and checks with fstat
> that the major:minor matches.
> 
> The vfio_device cdev logic in this patch:
> *) __vfio_register_dev() path ends up doing cdev_device_add() for each
>    vfio_device if VFIO_DEVICE_CDEV configured.
> *) vfio_unregister_group_dev() path does cdev_device_del();
> 
> Reviewed-by: Kevin Tian <kevin.tian@intel.com>
> Tested-by: Terrence Xu <terrence.xu@intel.com>
> Tested-by: Nicolin Chen <nicolinc@nvidia.com>
> Tested-by: Matthew Rosato <mjrosato@linux.ibm.com>
> Signed-off-by: Yi Liu <yi.l.liu@intel.com>
> ---
>  drivers/vfio/Kconfig       | 11 +++++++
>  drivers/vfio/Makefile      |  1 +
>  drivers/vfio/device_cdev.c | 62 ++++++++++++++++++++++++++++++++++++++
>  drivers/vfio/vfio.h        | 46 ++++++++++++++++++++++++++++
>  drivers/vfio/vfio_main.c   | 26 +++++++++++-----
>  include/linux/vfio.h       |  4 +++
>  6 files changed, 143 insertions(+), 7 deletions(-)
>  create mode 100644 drivers/vfio/device_cdev.c
> 
> diff --git a/drivers/vfio/Kconfig b/drivers/vfio/Kconfig
> index 89e06c981e43..e2105b4dac2d 100644
> --- a/drivers/vfio/Kconfig
> +++ b/drivers/vfio/Kconfig
> @@ -12,6 +12,17 @@ menuconfig VFIO
>  	  If you don't know what to do here, say N.
>  
>  if VFIO
> +config VFIO_DEVICE_CDEV
> +	bool "Support for the VFIO cdev /dev/vfio/devices/vfioX"
> +	depends on IOMMUFD
> +	help
> +	  The VFIO device cdev is another way for userspace to get device
> +	  access. Userspace gets device fd by opening device cdev under
> +	  /dev/vfio/devices/vfioX, and then bind the device fd with an iommufd
> +	  to set up secure DMA context for device access.
> +
> +	  If you don't know what to do here, say N.
> +
>  config VFIO_CONTAINER
>  	bool "Support for the VFIO container /dev/vfio/vfio"
>  	select VFIO_IOMMU_TYPE1 if MMU && (X86 || S390 || ARM || ARM64)
> diff --git a/drivers/vfio/Makefile b/drivers/vfio/Makefile
> index 70e7dcb302ef..245394aeb94b 100644
> --- a/drivers/vfio/Makefile
> +++ b/drivers/vfio/Makefile
> @@ -4,6 +4,7 @@ obj-$(CONFIG_VFIO) += vfio.o
>  vfio-y += vfio_main.o \
>  	  group.o \
>  	  iova_bitmap.o
> +vfio-$(CONFIG_VFIO_DEVICE_CDEV) += device_cdev.o
>  vfio-$(CONFIG_IOMMUFD) += iommufd.o
>  vfio-$(CONFIG_VFIO_CONTAINER) += container.o
>  vfio-$(CONFIG_VFIO_VIRQFD) += virqfd.o
> diff --git a/drivers/vfio/device_cdev.c b/drivers/vfio/device_cdev.c
> new file mode 100644
> index 000000000000..1c640016a824
> --- /dev/null
> +++ b/drivers/vfio/device_cdev.c
> @@ -0,0 +1,62 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Copyright (c) 2023 Intel Corporation.
> + */
> +#include <linux/vfio.h>
> +
> +#include "vfio.h"
> +
> +static dev_t device_devt;
> +
> +void vfio_init_device_cdev(struct vfio_device *device)
> +{
> +	device->device.devt = MKDEV(MAJOR(device_devt), device->index);
> +	cdev_init(&device->cdev, &vfio_device_fops);
> +	device->cdev.owner = THIS_MODULE;
> +}
> +
> +/*
> + * device access via the fd opened by this function is blocked until
> + * .open_device() is called successfully during BIND_IOMMUFD.
> + */
> +int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep)
> +{
> +	struct vfio_device *device = container_of(inode->i_cdev,
> +						  struct vfio_device, cdev);
> +	struct vfio_device_file *df;
> +	int ret;
> +
> +	if (!vfio_device_try_get_registration(device))
> +		return -ENODEV;
> +
> +	df = vfio_allocate_device_file(device);
> +	if (IS_ERR(df)) {
> +		ret = PTR_ERR(df);
> +		goto err_put_registration;
> +	}
> +
> +	filep->private_data = df;
> +
> +	return 0;
> +
> +err_put_registration:
> +	vfio_device_put_registration(device);
> +	return ret;
> +}
> +
> +static char *vfio_device_devnode(const struct device *dev, umode_t *mode)
> +{
> +	return kasprintf(GFP_KERNEL, "vfio/devices/%s", dev_name(dev));
> +}
> +
> +int vfio_cdev_init(struct class *device_class)
> +{
> +	device_class->devnode = vfio_device_devnode;
> +	return alloc_chrdev_region(&device_devt, 0,
> +				   MINORMASK + 1, "vfio-dev");
> +}
> +
> +void vfio_cdev_cleanup(void)
> +{
> +	unregister_chrdev_region(device_devt, MINORMASK + 1);
> +}
> diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h
> index 41dfc9d5205a..3a8fd0e32f59 100644
> --- a/drivers/vfio/vfio.h
> +++ b/drivers/vfio/vfio.h
> @@ -268,6 +268,52 @@ static inline void vfio_iommufd_unbind(struct vfio_device_file *df)
>  }
>  #endif
>  
> +#if IS_ENABLED(CONFIG_VFIO_DEVICE_CDEV)
> +static inline int vfio_device_add(struct vfio_device *device)
> +{
> +	return cdev_device_add(&device->cdev, &device->device);
> +}
> +
> +static inline void vfio_device_del(struct vfio_device *device)
> +{
> +	cdev_device_del(&device->cdev, &device->device);
> +}
> +
> +void vfio_init_device_cdev(struct vfio_device *device);
> +int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep);
> +int vfio_cdev_init(struct class *device_class);
> +void vfio_cdev_cleanup(void);
> +#else
> +static inline int vfio_device_add(struct vfio_device *device)
> +{
> +	return device_add(&device->device);
> +}
> +
> +static inline void vfio_device_del(struct vfio_device *device)
> +{
> +	device_del(&device->device);
> +}
> +
> +static inline void vfio_init_device_cdev(struct vfio_device *device)
> +{
> +}
> +
> +static inline int vfio_device_fops_cdev_open(struct inode *inode,
> +					     struct file *filep)
> +{
> +	return 0;
> +}
> +
> +static inline int vfio_cdev_init(struct class *device_class)
> +{
> +	return 0;
> +}
> +
> +static inline void vfio_cdev_cleanup(void)
> +{
> +}
> +#endif /* CONFIG_VFIO_DEVICE_CDEV */
> +
>  #if IS_ENABLED(CONFIG_VFIO_VIRQFD)
>  int __init vfio_virqfd_init(void);
>  void vfio_virqfd_exit(void);
> diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c
> index 8e96aab27029..58fc3bb768f2 100644
> --- a/drivers/vfio/vfio_main.c
> +++ b/drivers/vfio/vfio_main.c
> @@ -242,6 +242,7 @@ static int vfio_init_device(struct vfio_device *device, struct device *dev,
>  	device->device.release = vfio_device_release;
>  	device->device.class = vfio.device_class;
>  	device->device.parent = device->dev;
> +	vfio_init_device_cdev(device);
>  	return 0;
>  
>  out_uninit:
> @@ -280,7 +281,7 @@ static int __vfio_register_dev(struct vfio_device *device,
>  	if (ret)
>  		goto err_out;
>  
> -	ret = device_add(&device->device);
> +	ret = vfio_device_add(device);
>  	if (ret)
>  		goto err_out;
>  
> @@ -320,6 +321,12 @@ void vfio_unregister_group_dev(struct vfio_device *device)
>  	bool interrupted = false;
>  	long rc;
>  
> +	/* Prevent new device opened in the group path */
> +	vfio_device_group_unregister(device);
> +
> +	/* Prevent new device opened in the cdev path */
> +	vfio_device_del(device);
> +
>  	vfio_device_put_registration(device);
>  	rc = try_wait_for_completion(&device->comp);
>  	while (rc <= 0) {
> @@ -343,11 +350,6 @@ void vfio_unregister_group_dev(struct vfio_device *device)
>  		}
>  	}
>  
> -	vfio_device_group_unregister(device);
> -
> -	/* Balances device_add in register path */
> -	device_del(&device->device);
> -

Why were these relocated?  And additionally why was the comment
regarding the balance operations dropped?  The move seems unrelated to
the patch here, so if it's actually advisable for some reason, it
should be a separate patch.  Thanks,

Alex

>  	/* Balances vfio_device_set_group in register path */
>  	vfio_device_remove_group(device);
>  }
> @@ -555,7 +557,8 @@ 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(df);
> +	if (df->group)
> +		vfio_device_group_close(df);
>  
>  	vfio_device_put_registration(device);
>  
> @@ -1204,6 +1207,7 @@ static int vfio_device_fops_mmap(struct file *filep, struct vm_area_struct *vma)
>  
>  const struct file_operations vfio_device_fops = {
>  	.owner		= THIS_MODULE,
> +	.open		= vfio_device_fops_cdev_open,
>  	.release	= vfio_device_fops_release,
>  	.read		= vfio_device_fops_read,
>  	.write		= vfio_device_fops_write,
> @@ -1590,9 +1594,16 @@ static int __init vfio_init(void)
>  		goto err_dev_class;
>  	}
>  
> +	ret = vfio_cdev_init(vfio.device_class);
> +	if (ret)
> +		goto err_alloc_dev_chrdev;
> +
>  	pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
>  	return 0;
>  
> +err_alloc_dev_chrdev:
> +	class_destroy(vfio.device_class);
> +	vfio.device_class = NULL;
>  err_dev_class:
>  	vfio_virqfd_exit();
>  err_virqfd:
> @@ -1603,6 +1614,7 @@ static int __init vfio_init(void)
>  static void __exit vfio_cleanup(void)
>  {
>  	ida_destroy(&vfio.device_ida);
> +	vfio_cdev_cleanup();
>  	class_destroy(vfio.device_class);
>  	vfio.device_class = NULL;
>  	vfio_virqfd_exit();
> diff --git a/include/linux/vfio.h b/include/linux/vfio.h
> index 5c06af04ed9e..8719ec2adbbb 100644
> --- a/include/linux/vfio.h
> +++ b/include/linux/vfio.h
> @@ -13,6 +13,7 @@
>  #include <linux/mm.h>
>  #include <linux/workqueue.h>
>  #include <linux/poll.h>
> +#include <linux/cdev.h>
>  #include <uapi/linux/vfio.h>
>  #include <linux/iova_bitmap.h>
>  
> @@ -51,6 +52,9 @@ struct vfio_device {
>  	/* Members below here are private, not for driver use */
>  	unsigned int index;
>  	struct device device;	/* device.kref covers object life circle */
> +#if IS_ENABLED(CONFIG_VFIO_DEVICE_CDEV)
> +	struct cdev cdev;
> +#endif
>  	refcount_t refcount;	/* user count on registered device*/
>  	unsigned int open_count;
>  	struct completion comp;


  reply	other threads:[~2023-03-29 19:58 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-27  9:40 [PATCH v8 00/24] Add vfio_device cdev for iommufd support Yi Liu
2023-03-27  9:40 ` [PATCH v8 01/24] vfio: Allocate per device file structure Yi Liu
2023-03-27  9:40 ` [PATCH v8 02/24] vfio: Refine vfio file kAPIs for KVM Yi Liu
2023-03-27  9:40 ` [PATCH v8 03/24] vfio: Remove vfio_file_is_group() Yi Liu
2023-03-30 23:50   ` Jason Gunthorpe
2023-03-27  9:40 ` [PATCH v8 04/24] vfio: Accept vfio device file in the KVM facing kAPI Yi Liu
2023-03-27  9:40 ` [PATCH v8 05/24] kvm/vfio: Rename kvm_vfio_group to prepare for accepting vfio device fd Yi Liu
2023-03-27  9:40 ` [PATCH v8 06/24] kvm/vfio: Accept vfio device file from userspace Yi Liu
2023-03-27  9:40 ` [PATCH v8 07/24] vfio: Pass struct vfio_device_file * to vfio_device_open/close() Yi Liu
2023-03-27  9:40 ` [PATCH v8 08/24] vfio: Block device access via device fd until device is opened Yi Liu
2023-03-28 21:33   ` Alex Williamson
2023-03-29  2:23     ` Liu, Yi L
2023-03-27  9:40 ` [PATCH v8 09/24] vfio: Add cdev_device_open_cnt to vfio_group Yi Liu
2023-03-27  9:40 ` [PATCH v8 10/24] vfio: Make vfio_device_open() single open for device cdev path Yi Liu
2023-03-30 23:52   ` Jason Gunthorpe
2023-03-27  9:40 ` [PATCH v8 11/24] vfio: Make vfio_device_first_open() to accept NULL iommufd for noiommu Yi Liu
2023-03-30 23:56   ` Jason Gunthorpe
2023-03-27  9:40 ` [PATCH v8 12/24] vfio-iommufd: Move noiommu support out of vfio_iommufd_bind() Yi Liu
2023-03-27  9:40 ` [PATCH v8 13/24] vfio-iommufd: Split bind/attach into two steps Yi Liu
2023-03-27  9:40 ` [PATCH v8 14/24] vfio: Record devid in vfio_device_file Yi Liu
2023-03-27  9:40 ` [PATCH v8 15/24] vfio-iommufd: Add detach_ioas support for physical VFIO devices Yi Liu
2023-03-27  9:40 ` [PATCH v8 16/24] iommufd/device: Add iommufd_access_detach() API Yi Liu
2023-03-28  2:23   ` Jon Pan-Doh
2023-03-28 15:54     ` Nicolin Chen
2023-03-29  2:24       ` Liu, Yi L
2023-03-27  9:40 ` [PATCH v8 17/24] vfio-iommufd: Add detach_ioas support for emulated VFIO devices Yi Liu
2023-03-27  9:40 ` [PATCH v8 18/24] vfio: Determine noiommu in vfio_device registration Yi Liu
2023-03-28  6:36   ` Tian, Kevin
2023-03-27  9:40 ` [PATCH v8 19/24] vfio: Name noiommu vfio_device with "noiommu-" prefix Yi Liu
2023-03-28  6:37   ` Tian, Kevin
2023-03-27  9:40 ` [PATCH v8 20/24] vfio: Add cdev for vfio_device Yi Liu
2023-03-29 19:57   ` Alex Williamson [this message]
2023-03-30  5:35     ` Liu, Yi L
2023-03-27  9:40 ` [PATCH v8 21/24] vfio: Add VFIO_DEVICE_BIND_IOMMUFD Yi Liu
2023-03-29 21:00   ` Alex Williamson
2023-03-29 23:22     ` Jason Gunthorpe
2023-03-30 12:52       ` Liu, Yi L
2023-03-30 12:59         ` Jason Gunthorpe
2023-03-30  7:09     ` Liu, Yi L
2023-03-30 11:52       ` Jason Gunthorpe
2023-03-30 12:53         ` Liu, Yi L
2023-03-27  9:40 ` [PATCH v8 22/24] vfio: Add VFIO_DEVICE_AT[DE]TACH_IOMMUFD_PT Yi Liu
2023-03-29 21:19   ` Alex Williamson
2023-03-30 13:02     ` Liu, Yi L
2023-03-27  9:40 ` [PATCH v8 23/24] vfio: Compile group optionally Yi Liu
2023-03-29 21:51   ` Alex Williamson
2023-03-30 13:06     ` Liu, Yi L
2023-03-27  9:40 ` [PATCH v8 24/24] docs: vfio: Add vfio device cdev description Yi Liu
2023-03-29 22:47   ` Alex Williamson
2023-03-29 22:57     ` Alex Williamson
2023-03-30 13:11     ` Liu, Yi L
2023-03-27 17:48 ` [PATCH v8 00/24] Add vfio_device cdev for iommufd support Nicolin Chen
2023-03-31  3:10 ` Jiang, Yanting
2023-03-31  5:01 ` Jiang, Yanting

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230329135719.22ac6c12.alex.williamson@redhat.com \
    --to=alex.williamson@redhat.com \
    --cc=chao.p.peng@linux.intel.com \
    --cc=cohuck@redhat.com \
    --cc=eric.auger@redhat.com \
    --cc=intel-gfx@lists.freedesktop.org \
    --cc=intel-gvt-dev@lists.freedesktop.org \
    --cc=jasowang@redhat.com \
    --cc=jgg@nvidia.com \
    --cc=joro@8bytes.org \
    --cc=kevin.tian@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=lulu@redhat.com \
    --cc=mjrosato@linux.ibm.com \
    --cc=nicolinc@nvidia.com \
    --cc=peterx@redhat.com \
    --cc=robin.murphy@arm.com \
    --cc=shameerali.kolothum.thodi@huawei.com \
    --cc=suravee.suthikulpanit@amd.com \
    --cc=terrence.xu@intel.com \
    --cc=xudong.hao@intel.com \
    --cc=yan.y.zhao@intel.com \
    --cc=yanting.jiang@intel.com \
    --cc=yi.l.liu@intel.com \
    --cc=yi.y.sun@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).