RE: [PATCH v2 10/13] samples: refine vfio-mdev-pci driver

2019-09-30 Thread Liu, Yi L
Hi Alex,

> From: Alex Williamson [mailto:alex.william...@redhat.com]
> Sent: Thursday, September 26, 2019 10:37 AM
> To: Liu, Yi L 
>
> Subject: Re: [PATCH v2 10/13] samples: refine vfio-mdev-pci driver
> 
> On Thu,  5 Sep 2019 15:59:27 +0800
> Liu Yi L  wrote:
> 
> > From: Alex Williamson 
> >
> > This patch refines the implementation of original vfio-mdev-pci driver.
> >
> > And the vfio-mdev-pci-type_name will be named per the following rule:
> >
> > vmdev->attr.name = kasprintf(GFP_KERNEL,
> >  "%04x:%04x:%04x:%04x:%06x:%02x",
> >  pdev->vendor, pdev->device,
> >  pdev->subsystem_vendor,
> >  pdev->subsystem_device, pdev->class,
> >  pdev->revision);
> >
> > Before usage, check the
> > /sys/bus/pci/devices/$bdf/mdev_supported_types/
> > to ensure the final mdev_supported_types.
> >
> > Cc: Kevin Tian 
> > Cc: Lu Baolu 
> > Signed-off-by: Alex Williamson 
> > Signed-off-by: Liu Yi L 
> > ---
> >  drivers/vfio/pci/vfio_mdev_pci.c | 123
> > +++
> >  1 file changed, 72 insertions(+), 51 deletions(-)
> >
> > diff --git a/drivers/vfio/pci/vfio_mdev_pci.c
> > b/drivers/vfio/pci/vfio_mdev_pci.c
> > index 07c8067..09143d3 100644
> > --- a/drivers/vfio/pci/vfio_mdev_pci.c
> > +++ b/drivers/vfio/pci/vfio_mdev_pci.c
> > @@ -65,18 +65,22 @@ MODULE_PARM_DESC(disable_idle_d3,
> >
> >  static struct pci_driver vfio_mdev_pci_driver;
> >
> > -static ssize_t
> > -name_show(struct kobject *kobj, struct device *dev, char *buf) -{
> > -   return sprintf(buf, "%s-type1\n", dev_name(dev));
> > -}
> > -
> > -MDEV_TYPE_ATTR_RO(name);
> > +struct vfio_mdev_pci_device {
> > +   struct vfio_pci_device vdev;
> > +   struct mdev_parent_ops ops;
> > +   struct attribute_group *groups[2];
> > +   struct attribute_group attr;
> > +   atomic_t avail;
> > +};
> >
> >  static ssize_t
> >  available_instances_show(struct kobject *kobj, struct device *dev,
> > char *buf)  {
> > -   return sprintf(buf, "%d\n", 1);
> > +   struct vfio_mdev_pci_device *vmdev;
> > +
> > +   vmdev = pci_get_drvdata(to_pci_dev(dev));
> > +
> > +   return sprintf(buf, "%d\n", atomic_read(>avail));
> >  }
> >
> >  MDEV_TYPE_ATTR_RO(available_instances);
> > @@ -90,62 +94,57 @@ static ssize_t device_api_show(struct kobject
> > *kobj, struct device *dev,  MDEV_TYPE_ATTR_RO(device_api);
> >
> >  static struct attribute *vfio_mdev_pci_types_attrs[] = {
> > -   _type_attr_name.attr,
> > _type_attr_device_api.attr,
> > _type_attr_available_instances.attr,
> > NULL,
> >  };
> >
> > -static struct attribute_group vfio_mdev_pci_type_group1 = {
> > -   .name  = "type1",
> > -   .attrs = vfio_mdev_pci_types_attrs,
> > -};
> > -
> > -struct attribute_group *vfio_mdev_pci_type_groups[] = {
> > -   _mdev_pci_type_group1,
> > -   NULL,
> > -};
> > -
> >  struct vfio_mdev_pci {
> > struct vfio_pci_device *vdev;
> > struct mdev_device *mdev;
> > -   unsigned long handle;
> >  };
> >
> >  static int vfio_mdev_pci_create(struct kobject *kobj, struct
> > mdev_device *mdev)  {
> > struct device *pdev;
> > -   struct vfio_pci_device *vdev;
> > +   struct vfio_mdev_pci_device *vmdev;
> > struct vfio_mdev_pci *pmdev;
> > int ret;
> >
> > pdev = mdev_parent_dev(mdev);
> > -   vdev = dev_get_drvdata(pdev);
> > +   vmdev = dev_get_drvdata(pdev);
> > +
> > +   if (atomic_dec_if_positive(>avail) < 0)
> > +   return -ENOSPC;
> > +
> > pmdev = kzalloc(sizeof(struct vfio_mdev_pci), GFP_KERNEL);
> > -   if (pmdev == NULL) {
> > -   ret = -EBUSY;
> > -   goto out;
> > -   }
> > +   if (!pmdev)
> > +   return -ENOMEM;
> 
> Needs an atomic_inc(>avail) in this error path.  Thanks,

Oops, yes it is.

> 
> Alex

Thanks,
Yi Liu


Re: [PATCH v2 10/13] samples: refine vfio-mdev-pci driver

2019-09-25 Thread Alex Williamson
On Thu,  5 Sep 2019 15:59:27 +0800
Liu Yi L  wrote:

> From: Alex Williamson 
> 
> This patch refines the implementation of original vfio-mdev-pci driver.
> 
> And the vfio-mdev-pci-type_name will be named per the following rule:
> 
>   vmdev->attr.name = kasprintf(GFP_KERNEL,
>"%04x:%04x:%04x:%04x:%06x:%02x",
>pdev->vendor, pdev->device,
>pdev->subsystem_vendor,
>pdev->subsystem_device, pdev->class,
>pdev->revision);
> 
> Before usage, check the /sys/bus/pci/devices/$bdf/mdev_supported_types/
> to ensure the final mdev_supported_types.
> 
> Cc: Kevin Tian 
> Cc: Lu Baolu 
> Signed-off-by: Alex Williamson 
> Signed-off-by: Liu Yi L 
> ---
>  drivers/vfio/pci/vfio_mdev_pci.c | 123 
> +++
>  1 file changed, 72 insertions(+), 51 deletions(-)
> 
> diff --git a/drivers/vfio/pci/vfio_mdev_pci.c 
> b/drivers/vfio/pci/vfio_mdev_pci.c
> index 07c8067..09143d3 100644
> --- a/drivers/vfio/pci/vfio_mdev_pci.c
> +++ b/drivers/vfio/pci/vfio_mdev_pci.c
> @@ -65,18 +65,22 @@ MODULE_PARM_DESC(disable_idle_d3,
>  
>  static struct pci_driver vfio_mdev_pci_driver;
>  
> -static ssize_t
> -name_show(struct kobject *kobj, struct device *dev, char *buf)
> -{
> - return sprintf(buf, "%s-type1\n", dev_name(dev));
> -}
> -
> -MDEV_TYPE_ATTR_RO(name);
> +struct vfio_mdev_pci_device {
> + struct vfio_pci_device vdev;
> + struct mdev_parent_ops ops;
> + struct attribute_group *groups[2];
> + struct attribute_group attr;
> + atomic_t avail;
> +};
>  
>  static ssize_t
>  available_instances_show(struct kobject *kobj, struct device *dev, char *buf)
>  {
> - return sprintf(buf, "%d\n", 1);
> + struct vfio_mdev_pci_device *vmdev;
> +
> + vmdev = pci_get_drvdata(to_pci_dev(dev));
> +
> + return sprintf(buf, "%d\n", atomic_read(>avail));
>  }
>  
>  MDEV_TYPE_ATTR_RO(available_instances);
> @@ -90,62 +94,57 @@ static ssize_t device_api_show(struct kobject *kobj, 
> struct device *dev,
>  MDEV_TYPE_ATTR_RO(device_api);
>  
>  static struct attribute *vfio_mdev_pci_types_attrs[] = {
> - _type_attr_name.attr,
>   _type_attr_device_api.attr,
>   _type_attr_available_instances.attr,
>   NULL,
>  };
>  
> -static struct attribute_group vfio_mdev_pci_type_group1 = {
> - .name  = "type1",
> - .attrs = vfio_mdev_pci_types_attrs,
> -};
> -
> -struct attribute_group *vfio_mdev_pci_type_groups[] = {
> - _mdev_pci_type_group1,
> - NULL,
> -};
> -
>  struct vfio_mdev_pci {
>   struct vfio_pci_device *vdev;
>   struct mdev_device *mdev;
> - unsigned long handle;
>  };
>  
>  static int vfio_mdev_pci_create(struct kobject *kobj, struct mdev_device 
> *mdev)
>  {
>   struct device *pdev;
> - struct vfio_pci_device *vdev;
> + struct vfio_mdev_pci_device *vmdev;
>   struct vfio_mdev_pci *pmdev;
>   int ret;
>  
>   pdev = mdev_parent_dev(mdev);
> - vdev = dev_get_drvdata(pdev);
> + vmdev = dev_get_drvdata(pdev);
> +
> + if (atomic_dec_if_positive(>avail) < 0)
> + return -ENOSPC;
> +
>   pmdev = kzalloc(sizeof(struct vfio_mdev_pci), GFP_KERNEL);
> - if (pmdev == NULL) {
> - ret = -EBUSY;
> - goto out;
> - }
> + if (!pmdev)
> + return -ENOMEM;

Needs an atomic_inc(>avail) in this error path.  Thanks,

Alex

>  
>   pmdev->mdev = mdev;
> - pmdev->vdev = vdev;
> + pmdev->vdev = >vdev;
>   mdev_set_drvdata(mdev, pmdev);
>   ret = mdev_set_iommu_device(mdev_dev(mdev), pdev);
>   if (ret) {
>   pr_info("%s, failed to config iommu isolation for mdev: %s on 
> pf: %s\n",
>   __func__, dev_name(mdev_dev(mdev)), dev_name(pdev));
> - goto out;
> + kfree(pmdev);
> + atomic_inc(>avail);
> + return ret;
>   }
>  
> -out:
> - return ret;
> + return 0;
>  }
>  
>  static int vfio_mdev_pci_remove(struct mdev_device *mdev)
>  {
>   struct vfio_mdev_pci *pmdev = mdev_get_drvdata(mdev);
> + struct vfio_mdev_pci_device *vmdev;
> +
> + vmdev = container_of(pmdev->vdev, struct vfio_mdev_pci_device, vdev);
>  
>   kfree(pmdev);
> + atomic_inc(>avail);
>   pr_info("%s, succeeded for mdev: %s\n", __func__,
>dev_name(mdev_dev(mdev)));
>  
> @@ -237,24 +236,12 @@ static ssize_t vfio_mdev_pci_write(struct mdev_device 
> *mdev,
>   return vfio_pci_write(pmdev->vdev, (char __user *)buf, count, ppos);
>  }
>  
> -static const struct mdev_parent_ops vfio_mdev_pci_ops = {
> - .supported_type_groups  = vfio_mdev_pci_type_groups,
> - .create = vfio_mdev_pci_create,
> - .remove = vfio_mdev_pci_remove,
> -
> - .open   = vfio_mdev_pci_open,
> - .release= 

[PATCH v2 10/13] samples: refine vfio-mdev-pci driver

2019-09-06 Thread Liu Yi L
From: Alex Williamson 

This patch refines the implementation of original vfio-mdev-pci driver.

And the vfio-mdev-pci-type_name will be named per the following rule:

vmdev->attr.name = kasprintf(GFP_KERNEL,
 "%04x:%04x:%04x:%04x:%06x:%02x",
 pdev->vendor, pdev->device,
 pdev->subsystem_vendor,
 pdev->subsystem_device, pdev->class,
 pdev->revision);

Before usage, check the /sys/bus/pci/devices/$bdf/mdev_supported_types/
to ensure the final mdev_supported_types.

Cc: Kevin Tian 
Cc: Lu Baolu 
Signed-off-by: Alex Williamson 
Signed-off-by: Liu Yi L 
---
 drivers/vfio/pci/vfio_mdev_pci.c | 123 +++
 1 file changed, 72 insertions(+), 51 deletions(-)

diff --git a/drivers/vfio/pci/vfio_mdev_pci.c b/drivers/vfio/pci/vfio_mdev_pci.c
index 07c8067..09143d3 100644
--- a/drivers/vfio/pci/vfio_mdev_pci.c
+++ b/drivers/vfio/pci/vfio_mdev_pci.c
@@ -65,18 +65,22 @@ MODULE_PARM_DESC(disable_idle_d3,
 
 static struct pci_driver vfio_mdev_pci_driver;
 
-static ssize_t
-name_show(struct kobject *kobj, struct device *dev, char *buf)
-{
-   return sprintf(buf, "%s-type1\n", dev_name(dev));
-}
-
-MDEV_TYPE_ATTR_RO(name);
+struct vfio_mdev_pci_device {
+   struct vfio_pci_device vdev;
+   struct mdev_parent_ops ops;
+   struct attribute_group *groups[2];
+   struct attribute_group attr;
+   atomic_t avail;
+};
 
 static ssize_t
 available_instances_show(struct kobject *kobj, struct device *dev, char *buf)
 {
-   return sprintf(buf, "%d\n", 1);
+   struct vfio_mdev_pci_device *vmdev;
+
+   vmdev = pci_get_drvdata(to_pci_dev(dev));
+
+   return sprintf(buf, "%d\n", atomic_read(>avail));
 }
 
 MDEV_TYPE_ATTR_RO(available_instances);
@@ -90,62 +94,57 @@ static ssize_t device_api_show(struct kobject *kobj, struct 
device *dev,
 MDEV_TYPE_ATTR_RO(device_api);
 
 static struct attribute *vfio_mdev_pci_types_attrs[] = {
-   _type_attr_name.attr,
_type_attr_device_api.attr,
_type_attr_available_instances.attr,
NULL,
 };
 
-static struct attribute_group vfio_mdev_pci_type_group1 = {
-   .name  = "type1",
-   .attrs = vfio_mdev_pci_types_attrs,
-};
-
-struct attribute_group *vfio_mdev_pci_type_groups[] = {
-   _mdev_pci_type_group1,
-   NULL,
-};
-
 struct vfio_mdev_pci {
struct vfio_pci_device *vdev;
struct mdev_device *mdev;
-   unsigned long handle;
 };
 
 static int vfio_mdev_pci_create(struct kobject *kobj, struct mdev_device *mdev)
 {
struct device *pdev;
-   struct vfio_pci_device *vdev;
+   struct vfio_mdev_pci_device *vmdev;
struct vfio_mdev_pci *pmdev;
int ret;
 
pdev = mdev_parent_dev(mdev);
-   vdev = dev_get_drvdata(pdev);
+   vmdev = dev_get_drvdata(pdev);
+
+   if (atomic_dec_if_positive(>avail) < 0)
+   return -ENOSPC;
+
pmdev = kzalloc(sizeof(struct vfio_mdev_pci), GFP_KERNEL);
-   if (pmdev == NULL) {
-   ret = -EBUSY;
-   goto out;
-   }
+   if (!pmdev)
+   return -ENOMEM;
 
pmdev->mdev = mdev;
-   pmdev->vdev = vdev;
+   pmdev->vdev = >vdev;
mdev_set_drvdata(mdev, pmdev);
ret = mdev_set_iommu_device(mdev_dev(mdev), pdev);
if (ret) {
pr_info("%s, failed to config iommu isolation for mdev: %s on 
pf: %s\n",
__func__, dev_name(mdev_dev(mdev)), dev_name(pdev));
-   goto out;
+   kfree(pmdev);
+   atomic_inc(>avail);
+   return ret;
}
 
-out:
-   return ret;
+   return 0;
 }
 
 static int vfio_mdev_pci_remove(struct mdev_device *mdev)
 {
struct vfio_mdev_pci *pmdev = mdev_get_drvdata(mdev);
+   struct vfio_mdev_pci_device *vmdev;
+
+   vmdev = container_of(pmdev->vdev, struct vfio_mdev_pci_device, vdev);
 
kfree(pmdev);
+   atomic_inc(>avail);
pr_info("%s, succeeded for mdev: %s\n", __func__,
 dev_name(mdev_dev(mdev)));
 
@@ -237,24 +236,12 @@ static ssize_t vfio_mdev_pci_write(struct mdev_device 
*mdev,
return vfio_pci_write(pmdev->vdev, (char __user *)buf, count, ppos);
 }
 
-static const struct mdev_parent_ops vfio_mdev_pci_ops = {
-   .supported_type_groups  = vfio_mdev_pci_type_groups,
-   .create = vfio_mdev_pci_create,
-   .remove = vfio_mdev_pci_remove,
-
-   .open   = vfio_mdev_pci_open,
-   .release= vfio_mdev_pci_release,
-
-   .read   = vfio_mdev_pci_read,
-   .write  = vfio_mdev_pci_write,
-   .mmap   = vfio_mdev_pci_mmap,
-   .ioctl  = vfio_mdev_pci_ioctl,
-};
-
 static int