Re: [Intel-gfx] [PATCH v2 6/9] vfio/mdev: Add mdev available instance checking to the core

2021-09-24 Thread Eric Farman
On Tue, 2021-09-21 at 10:19 -0300, Jason Gunthorpe wrote:
> On Mon, Sep 20, 2021 at 08:02:29PM +0200, Cornelia Huck wrote:
> > On Thu, Sep 09 2021, Jason Gunthorpe  wrote:
> > 
> > > Many of the mdev drivers use a simple counter for keeping track
> > > of the
> > > available instances. Move this code to the core code and store
> > > the counter
> > > in the mdev_type. Implement it using correct locking, fixing
> > > mdpy.
> > > 
> > > Drivers provide a get_available() callback to set the number of
> > > available
> > > instances for their mtypes which is fixed at registration time.
> > > The core
> > > provides a standard sysfs attribute to return the
> > > available_instances.
> > 
> > So, according to the documentation, available_instances is
> > mandatory. This means that drivers either need to provide
> > get_available
> > or implement their own version of the attribute. I think we want to
> > update vfio-mediated-device.rst as well?
> 
> I added this, and something similar for the device_api patch too,
> thanks
> 
> diff --git a/Documentation/driver-api/vfio-mediated-device.rst
> b/Documentation/driver-api/vfio-mediated-device.rst
> index 9f26079cacae35..0a130d76b33a48 100644
> --- a/Documentation/driver-api/vfio-mediated-device.rst
> +++ b/Documentation/driver-api/vfio-mediated-device.rst
> @@ -106,6 +106,7 @@ structure to represent a mediated device's
> driver::
>int  (*probe)  (struct mdev_device *dev);
>void (*remove) (struct mdev_device *dev);
>struct device_driverdriver;
> +  unsigned int (*get_available)(struct mdev_type *mtype);
>   };
>  
>  A mediated bus driver for mdev should use this structure in the
> function calls
> @@ -230,7 +231,8 @@ Directories and files under the sysfs for Each
> Physical Device
>  * available_instances
>  
>This attribute should show the number of devices of type  
> that can be
> -  created.
> +  created. Drivers can supply a get_availble() function pointer to 

s/availble/available/

> have the core
> +  code create and maintain this sysfs automatically.
>  
>  * [device]
>  



Re: [Intel-gfx] [PATCH v2 6/9] vfio/mdev: Add mdev available instance checking to the core

2021-09-21 Thread Jason Gunthorpe
On Mon, Sep 20, 2021 at 08:02:29PM +0200, Cornelia Huck wrote:
> On Thu, Sep 09 2021, Jason Gunthorpe  wrote:
> 
> > Many of the mdev drivers use a simple counter for keeping track of the
> > available instances. Move this code to the core code and store the counter
> > in the mdev_type. Implement it using correct locking, fixing mdpy.
> >
> > Drivers provide a get_available() callback to set the number of available
> > instances for their mtypes which is fixed at registration time. The core
> > provides a standard sysfs attribute to return the available_instances.
> 
> So, according to the documentation, available_instances is
> mandatory. This means that drivers either need to provide get_available
> or implement their own version of the attribute. I think we want to
> update vfio-mediated-device.rst as well?

I added this, and something similar for the device_api patch too, thanks

diff --git a/Documentation/driver-api/vfio-mediated-device.rst 
b/Documentation/driver-api/vfio-mediated-device.rst
index 9f26079cacae35..0a130d76b33a48 100644
--- a/Documentation/driver-api/vfio-mediated-device.rst
+++ b/Documentation/driver-api/vfio-mediated-device.rst
@@ -106,6 +106,7 @@ structure to represent a mediated device's driver::
 int  (*probe)  (struct mdev_device *dev);
 void (*remove) (struct mdev_device *dev);
 struct device_driverdriver;
+unsigned int (*get_available)(struct mdev_type *mtype);
  };
 
 A mediated bus driver for mdev should use this structure in the function calls
@@ -230,7 +231,8 @@ Directories and files under the sysfs for Each Physical 
Device
 * available_instances
 
   This attribute should show the number of devices of type  that can 
be
-  created.
+  created. Drivers can supply a get_availble() function pointer to have the 
core
+  code create and maintain this sysfs automatically.
 
 * [device]
 


Re: [Intel-gfx] [PATCH v2 6/9] vfio/mdev: Add mdev available instance checking to the core

2021-09-20 Thread Cornelia Huck
On Thu, Sep 09 2021, Jason Gunthorpe  wrote:

> Many of the mdev drivers use a simple counter for keeping track of the
> available instances. Move this code to the core code and store the counter
> in the mdev_type. Implement it using correct locking, fixing mdpy.
>
> Drivers provide a get_available() callback to set the number of available
> instances for their mtypes which is fixed at registration time. The core
> provides a standard sysfs attribute to return the available_instances.

So, according to the documentation, available_instances is
mandatory. This means that drivers either need to provide get_available
or implement their own version of the attribute. I think we want to
update vfio-mediated-device.rst as well?

>
> Signed-off-by: Jason Gunthorpe 
> ---
>  drivers/s390/cio/vfio_ccw_drv.c   |  1 -
>  drivers/s390/cio/vfio_ccw_ops.c   | 26 ++-
>  drivers/s390/cio/vfio_ccw_private.h   |  2 --
>  drivers/s390/crypto/vfio_ap_ops.c | 32 ++-
>  drivers/s390/crypto/vfio_ap_private.h |  2 --
>  drivers/vfio/mdev/mdev_core.c | 11 +++-
>  drivers/vfio/mdev/mdev_private.h  |  2 ++
>  drivers/vfio/mdev/mdev_sysfs.c| 37 +++
>  include/linux/mdev.h  |  2 ++
>  samples/vfio-mdev/mdpy.c  | 22 +---
>  10 files changed, 73 insertions(+), 64 deletions(-)

Otherwise, looks good.



Re: [Intel-gfx] [PATCH v2 6/9] vfio/mdev: Add mdev available instance checking to the core

2021-09-10 Thread Christoph Hellwig
On Thu, Sep 09, 2021 at 04:38:46PM -0300, Jason Gunthorpe wrote:
> Many of the mdev drivers use a simple counter for keeping track of the
> available instances. Move this code to the core code and store the counter
> in the mdev_type. Implement it using correct locking, fixing mdpy.
> 
> Drivers provide a get_available() callback to set the number of available
> instances for their mtypes which is fixed at registration time. The core
> provides a standard sysfs attribute to return the available_instances.

Looks good,

Reviewed-by: Christoph Hellwig 


[Intel-gfx] [PATCH v2 6/9] vfio/mdev: Add mdev available instance checking to the core

2021-09-09 Thread Jason Gunthorpe
Many of the mdev drivers use a simple counter for keeping track of the
available instances. Move this code to the core code and store the counter
in the mdev_type. Implement it using correct locking, fixing mdpy.

Drivers provide a get_available() callback to set the number of available
instances for their mtypes which is fixed at registration time. The core
provides a standard sysfs attribute to return the available_instances.

Signed-off-by: Jason Gunthorpe 
---
 drivers/s390/cio/vfio_ccw_drv.c   |  1 -
 drivers/s390/cio/vfio_ccw_ops.c   | 26 ++-
 drivers/s390/cio/vfio_ccw_private.h   |  2 --
 drivers/s390/crypto/vfio_ap_ops.c | 32 ++-
 drivers/s390/crypto/vfio_ap_private.h |  2 --
 drivers/vfio/mdev/mdev_core.c | 11 +++-
 drivers/vfio/mdev/mdev_private.h  |  2 ++
 drivers/vfio/mdev/mdev_sysfs.c| 37 +++
 include/linux/mdev.h  |  2 ++
 samples/vfio-mdev/mdpy.c  | 22 +---
 10 files changed, 73 insertions(+), 64 deletions(-)

diff --git a/drivers/s390/cio/vfio_ccw_drv.c b/drivers/s390/cio/vfio_ccw_drv.c
index 99f2823361718f..de782e967a5474 100644
--- a/drivers/s390/cio/vfio_ccw_drv.c
+++ b/drivers/s390/cio/vfio_ccw_drv.c
@@ -106,7 +106,6 @@ static struct vfio_ccw_private 
*vfio_ccw_alloc_private(struct subchannel *sch)
INIT_LIST_HEAD(>crw);
INIT_WORK(>io_work, vfio_ccw_sch_io_todo);
INIT_WORK(>crw_work, vfio_ccw_crw_todo);
-   atomic_set(>avail, 1);
 
private->cp.guest_cp = kcalloc(CCWCHAIN_LEN_MAX, sizeof(struct ccw1),
   GFP_KERNEL);
diff --git a/drivers/s390/cio/vfio_ccw_ops.c b/drivers/s390/cio/vfio_ccw_ops.c
index 18a48d1f1e8fff..38ab5c1f25ec09 100644
--- a/drivers/s390/cio/vfio_ccw_ops.c
+++ b/drivers/s390/cio/vfio_ccw_ops.c
@@ -70,20 +70,9 @@ static ssize_t name_show(struct mdev_type *mtype,
 }
 static MDEV_TYPE_ATTR_RO(name);
 
-static ssize_t available_instances_show(struct mdev_type *mtype,
-   struct mdev_type_attribute *attr,
-   char *buf)
-{
-   struct vfio_ccw_private *private =
-   dev_get_drvdata(mtype_get_parent_dev(mtype));
-
-   return sprintf(buf, "%d\n", atomic_read(>avail));
-}
-static MDEV_TYPE_ATTR_RO(available_instances);
 
 static struct attribute *mdev_types_attrs[] = {
_type_attr_name.attr,
-   _type_attr_available_instances.attr,
NULL,
 };
 
@@ -102,9 +91,6 @@ static int vfio_ccw_mdev_probe(struct mdev_device *mdev)
struct vfio_ccw_private *private = dev_get_drvdata(mdev->dev.parent);
int ret;
 
-   if (atomic_dec_if_positive(>avail) < 0)
-   return -EPERM;
-
memset(>vdev, 0, sizeof(private->vdev));
vfio_init_group_dev(>vdev, >dev,
_ccw_dev_ops);
@@ -118,13 +104,12 @@ static int vfio_ccw_mdev_probe(struct mdev_device *mdev)
 
ret = vfio_register_group_dev(>vdev);
if (ret)
-   goto err_atomic;
+   goto err_init;
dev_set_drvdata(>dev, private);
return 0;
 
-err_atomic:
+err_init:
vfio_uninit_group_dev(>vdev);
-   atomic_inc(>avail);
private->mdev = NULL;
return ret;
 }
@@ -141,7 +126,6 @@ static void vfio_ccw_mdev_remove(struct mdev_device *mdev)
vfio_unregister_group_dev(>vdev);
vfio_uninit_group_dev(>vdev);
private->mdev = NULL;
-   atomic_inc(>avail);
 }
 
 static int vfio_ccw_mdev_open_device(struct vfio_device *vdev)
@@ -610,6 +594,11 @@ static void vfio_ccw_mdev_request(struct vfio_device 
*vdev, unsigned int count)
}
 }
 
+static unsigned int vfio_ccw_get_available(struct mdev_type *mtype)
+{
+   return 1;
+}
+
 static const struct vfio_device_ops vfio_ccw_dev_ops = {
.open_device = vfio_ccw_mdev_open_device,
.close_device = vfio_ccw_mdev_close_device,
@@ -627,6 +616,7 @@ struct mdev_driver vfio_ccw_mdev_driver = {
},
.probe = vfio_ccw_mdev_probe,
.remove = vfio_ccw_mdev_remove,
+   .get_available = vfio_ccw_get_available,
 };
 
 static const struct mdev_parent_ops vfio_ccw_mdev_ops = {
diff --git a/drivers/s390/cio/vfio_ccw_private.h 
b/drivers/s390/cio/vfio_ccw_private.h
index 5e98eacdf31074..bbc97eb9d9c6fc 100644
--- a/drivers/s390/cio/vfio_ccw_private.h
+++ b/drivers/s390/cio/vfio_ccw_private.h
@@ -72,7 +72,6 @@ struct vfio_ccw_crw {
  * @sch: pointer to the subchannel
  * @state: internal state of the device
  * @completion: synchronization helper of the I/O completion
- * @avail: available for creating a mediated device
  * @mdev: pointer to the mediated device
  * @nb: notifier for vfio events
  * @io_region: MMIO region to input/output I/O arguments/results
@@ -96,7 +95,6 @@ struct vfio_ccw_private {
struct subchannel   *sch;
int state;
struct completion