<div dir="ltr"><div dir="ltr">On Wed, Sep 25, 2019 at 4:52 AM Tian, Kevin <<a href="mailto:kevin.tian@intel.com">kevin.tian@intel.com</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">> From: Alex Williamson<br>
> Sent: Wednesday, September 25, 2019 7:07 AM<br>
> <br>
> On Tue, 24 Sep 2019 21:53:29 +0800<br>
> Jason Wang <<a href="mailto:jasowang@redhat.com" target="_blank">jasowang@redhat.com</a>> wrote:<br>
> <br>
> > Currently, except for the create and remove, the rest of<br>
> > mdev_parent_ops is designed for vfio-mdev driver only and may not help<br>
> > for kernel mdev driver. With the help of class id, this patch<br>
> > introduces device specific callbacks inside mdev_device<br>
> > structure. This allows different set of callback to be used by<br>
> > vfio-mdev and virtio-mdev.<br>
> ><br>
> > Signed-off-by: Jason Wang <<a href="mailto:jasowang@redhat.com" target="_blank">jasowang@redhat.com</a>><br>
> > ---<br>
> > .../driver-api/vfio-mediated-device.rst | 4 +-<br>
> > MAINTAINERS | 1 +<br>
> > drivers/gpu/drm/i915/gvt/kvmgt.c | 17 +++---<br>
> > drivers/s390/cio/vfio_ccw_ops.c | 17 ++++--<br>
> > drivers/s390/crypto/vfio_ap_ops.c | 13 +++--<br>
> > drivers/vfio/mdev/mdev_core.c | 12 +++++<br>
> > drivers/vfio/mdev/mdev_private.h | 1 +<br>
> > drivers/vfio/mdev/vfio_mdev.c | 37 ++++++-------<br>
> > include/linux/mdev.h | 42 ++++-----------<br>
> > include/linux/vfio_mdev.h | 52 +++++++++++++++++++<br>
> > samples/vfio-mdev/mbochs.c | 19 ++++---<br>
> > samples/vfio-mdev/mdpy.c | 19 ++++---<br>
> > samples/vfio-mdev/mtty.c | 17 ++++--<br>
> > 13 files changed, 168 insertions(+), 83 deletions(-)<br>
> > create mode 100644 include/linux/vfio_mdev.h<br>
> ><br>
> > diff --git a/Documentation/driver-api/vfio-mediated-device.rst<br>
> b/Documentation/driver-api/vfio-mediated-device.rst<br>
> > index a5bdc60d62a1..d50425b368bb 100644<br>
> > --- a/Documentation/driver-api/vfio-mediated-device.rst<br>
> > +++ b/Documentation/driver-api/vfio-mediated-device.rst<br>
> > @@ -152,7 +152,9 @@ callbacks per mdev parent device, per mdev type,<br>
> or any other categorization.<br>
> > Vendor drivers are expected to be fully asynchronous in this respect or<br>
> > provide their own internal resource protection.)<br>
> ><br>
> > -The callbacks in the mdev_parent_ops structure are as follows:<br>
> > +The device specific callbacks are referred through device_ops pointer<br>
> > +in mdev_parent_ops. For vfio-mdev device, its callbacks in device_ops<br>
> > +are as follows:<br>
> <br>
> This is not accurate. device_ops is now on the mdev_device and is an<br>
> mdev bus driver specific structure of callbacks that must be registered<br>
> for each mdev device by the parent driver during the create callback.<br>
> There's a one to one mapping of class_id to mdev_device_ops callbacks.<br>
<br>
there is also a mistake in include/Linux/mdev.h, where device_ops is<br>
still part of mdev_parent_ops in the comment line.<br>
<br>
> <br>
> That also suggests to me that we could be more clever in registering<br>
> both of these with mdev-core. Can we embed the class_id in the ops<br>
> structure in a common way so that the core can extract it and the bus<br>
> drivers can access their specific callbacks?<br>
> <br>
> > * open: open callback of mediated device<br>
> > * close: close callback of mediated device<br>
> > diff --git a/MAINTAINERS b/MAINTAINERS<br>
> > index b2326dece28e..89832b316500 100644<br>
> > --- a/MAINTAINERS<br>
> > +++ b/MAINTAINERS<br>
> > @@ -17075,6 +17075,7 @@ S: Maintained<br>
> > F: Documentation/driver-api/vfio-mediated-device.rst<br>
> > F: drivers/vfio/mdev/<br>
> > F: include/linux/mdev.h<br>
> > +F: include/linux/vfio_mdev.h<br>
> > F: samples/vfio-mdev/<br>
> ><br>
> > VFIO PLATFORM DRIVER<br>
> > diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c<br>
> b/drivers/gpu/drm/i915/gvt/kvmgt.c<br>
> > index f793252a3d2a..b274f5ee481f 100644<br>
> > --- a/drivers/gpu/drm/i915/gvt/kvmgt.c<br>
> > +++ b/drivers/gpu/drm/i915/gvt/kvmgt.c<br>
> > @@ -42,6 +42,7 @@<br>
> > #include <linux/kvm_host.h><br>
> > #include <linux/vfio.h><br>
> > #include <linux/mdev.h><br>
> > +#include <linux/vfio_mdev.h><br>
> > #include <linux/debugfs.h><br>
> ><br>
> > #include <linux/nospec.h><br>
> > @@ -643,6 +644,8 @@ static void kvmgt_put_vfio_device(void *vgpu)<br>
> > vfio_device_put(((struct intel_vgpu *)vgpu)->vdev.vfio_device);<br>
> > }<br>
> ><br>
> > +static struct vfio_mdev_device_ops intel_vfio_vgpu_dev_ops;<br>
> > +<br>
> > static int intel_vgpu_create(struct kobject *kobj, struct mdev_device<br>
> *mdev)<br>
> > {<br>
> > struct intel_vgpu *vgpu = NULL;<br>
> > @@ -679,6 +682,7 @@ static int intel_vgpu_create(struct kobject *kobj,<br>
> struct mdev_device *mdev)<br>
> > ret = 0;<br>
> ><br>
> > mdev_set_class_id(mdev, MDEV_ID_VFIO);<br>
> > + mdev_set_dev_ops(mdev, &intel_vfio_vgpu_dev_ops);<br>
> <br>
> This seems rather unrefined. We're registering interdependent data in<br>
> separate calls. All drivers need to make both of these calls. I'm not<br>
> sure if this is a good idea, but what if we had:<br>
> <br>
> static const struct vfio_mdev_device_ops intel_vfio_vgpu_dev_ops = {<br>
> .id = MDEV_ID_VFIO,<br>
> .open = intel_vgpu_open,<br>
> .release = intel_vgpu_release,<br>
> ...<br>
> <br>
> And the set function passed &<a href="http://intel_vfio_vgpu_dev_ops.id" rel="noreferrer" target="_blank">intel_vfio_vgpu_dev_ops.id</a> and the mdev<br>
> bus drivers used container_of to get to their callbacks?<br>
<br>
or just make it explicit? e.g.<br>
<br>
mdev_set_class(mdev, MDEV_ID_VFIO, &intel_vfio_vgpu_dev_ops);<br>
<br>
> <br>
> > out:<br>
> > return ret;<br>
> > }<br>
> > @@ -1601,20 +1605,21 @@ static const struct attribute_group<br>
> *intel_vgpu_groups[] = {<br>
> > NULL,<br>
> > };<br>
> ><br>
> > -static struct mdev_parent_ops intel_vgpu_ops = {<br>
> > - .mdev_attr_groups = intel_vgpu_groups,<br>
> > - .create = intel_vgpu_create,<br>
> > - .remove = intel_vgpu_remove,<br>
> > -<br>
> > +static struct vfio_mdev_device_ops intel_vfio_vgpu_dev_ops = {<br>
> > .open = intel_vgpu_open,<br>
> > .release = intel_vgpu_release,<br>
> > -<br>
> > .read = intel_vgpu_read,<br>
> > .write = intel_vgpu_write,<br>
> > .mmap = intel_vgpu_mmap,<br>
> > .ioctl = intel_vgpu_ioctl,<br>
> > };<br>
> ><br>
> > +static struct mdev_parent_ops intel_vgpu_ops = {<br>
> <br>
> These could maybe be made const at the same time. Thanks,<br>
> <br>
> Alex<br>
> <br>
> > + .mdev_attr_groups = intel_vgpu_groups,<br>
> > + .create = intel_vgpu_create,<br>
> > + .remove = intel_vgpu_remove,<br>
> > +};<br>
> > +<br>
> > static int kvmgt_host_init(struct device *dev, void *gvt, const void *ops)<br>
> > {<br>
> > struct attribute **kvm_type_attrs;<br>
> > diff --git a/drivers/s390/cio/vfio_ccw_ops.c<br>
> b/drivers/s390/cio/vfio_ccw_ops.c<br>
> > index d258ef1fedb9..329d53c1f46b 100644<br>
> > --- a/drivers/s390/cio/vfio_ccw_ops.c<br>
> > +++ b/drivers/s390/cio/vfio_ccw_ops.c<br>
> > @@ -12,6 +12,7 @@<br>
> ><br>
> > #include <linux/vfio.h><br>
> > #include <linux/mdev.h><br>
> > +#include <linux/vfio_mdev.h><br>
> > #include <linux/nospec.h><br>
> > #include <linux/slab.h><br>
> ><br>
> > @@ -110,6 +111,8 @@ static struct attribute_group *mdev_type_groups[]<br>
> = {<br>
> > NULL,<br>
> > };<br>
> ><br>
> > +static const struct vfio_mdev_device_ops vfio_mdev_ops;<br>
> > +<br>
> > static int vfio_ccw_mdev_create(struct kobject *kobj, struct mdev_device<br>
> *mdev)<br>
> > {<br>
> > struct vfio_ccw_private *private =<br>
> > @@ -130,6 +133,7 @@ static int vfio_ccw_mdev_create(struct kobject<br>
> *kobj, struct mdev_device *mdev)<br>
> > private->sch->schid.sch_no);<br>
> ><br>
> > mdev_set_class_id(mdev, MDEV_ID_VFIO);<br>
> > + mdev_set_dev_ops(mdev, &vfio_mdev_ops);<br>
> > return 0;<br>
> > }<br>
> ><br>
> > @@ -575,11 +579,7 @@ static ssize_t vfio_ccw_mdev_ioctl(struct<br>
> mdev_device *mdev,<br>
> > }<br>
> > }<br>
> ><br>
> > -static const struct mdev_parent_ops vfio_ccw_mdev_ops = {<br>
> > - .owner = THIS_MODULE,<br>
> > - .supported_type_groups = mdev_type_groups,<br>
> > - .create = vfio_ccw_mdev_create,<br>
> > - .remove = vfio_ccw_mdev_remove,<br>
> > +static const struct vfio_mdev_device_ops vfio_mdev_ops = {<br>
> > .open = vfio_ccw_mdev_open,<br>
> > .release = vfio_ccw_mdev_release,<br>
> > .read = vfio_ccw_mdev_read,<br>
> > @@ -587,6 +587,13 @@ static const struct mdev_parent_ops<br>
> vfio_ccw_mdev_ops = {<br>
> > .ioctl = vfio_ccw_mdev_ioctl,<br>
> > };<br>
> ><br>
> > +static const struct mdev_parent_ops vfio_ccw_mdev_ops = {<br>
> > + .owner = THIS_MODULE,<br>
> > + .supported_type_groups = mdev_type_groups,<br>
> > + .create = vfio_ccw_mdev_create,<br>
> > + .remove = vfio_ccw_mdev_remove,<br>
> > +};<br>
> > +<br>
> > int vfio_ccw_mdev_reg(struct subchannel *sch)<br>
> > {<br>
> > return mdev_register_device(&sch->dev, &vfio_ccw_mdev_ops);<br>
> > diff --git a/drivers/s390/crypto/vfio_ap_ops.c<br>
> b/drivers/s390/crypto/vfio_ap_ops.c<br>
> > index 2cfd96112aa0..3a89933f0d3e 100644<br>
> > --- a/drivers/s390/crypto/vfio_ap_ops.c<br>
> > +++ b/drivers/s390/crypto/vfio_ap_ops.c<br>
> > @@ -16,6 +16,7 @@<br>
> > #include <linux/bitops.h><br>
> > #include <linux/kvm_host.h><br>
> > #include <linux/module.h><br>
> > +#include <linux/vfio_mdev.h><br>
> > #include <asm/kvm.h><br>
> > #include <asm/zcrypt.h><br>
> ><br>
> > @@ -321,6 +322,8 @@ static void vfio_ap_matrix_init(struct<br>
> ap_config_info *info,<br>
> > matrix->adm_max = info->apxa ? info->Nd : 15;<br>
> > }<br>
> ><br>
> > +static const struct vfio_mdev_device_ops vfio_mdev_ops;<br>
> > +<br>
> > static int vfio_ap_mdev_create(struct kobject *kobj, struct mdev_device<br>
> *mdev)<br>
> > {<br>
> > struct ap_matrix_mdev *matrix_mdev;<br>
> > @@ -344,6 +347,7 @@ static int vfio_ap_mdev_create(struct kobject<br>
> *kobj, struct mdev_device *mdev)<br>
> > mutex_unlock(&matrix_dev->lock);<br>
> ><br>
> > mdev_set_class_id(mdev, MDEV_ID_VFIO);<br>
> > + mdev_set_dev_ops(mdev, &vfio_mdev_ops);<br>
> > return 0;<br>
> > }<br>
> ><br>
> > @@ -1281,15 +1285,18 @@ static ssize_t vfio_ap_mdev_ioctl(struct<br>
> mdev_device *mdev,<br>
> > return ret;<br>
> > }<br>
> ><br>
> > +static const struct vfio_mdev_device_ops vfio_mdev_ops = {<br>
> > + .open = vfio_ap_mdev_open,<br>
> > + .release = vfio_ap_mdev_release,<br>
> > + .ioctl = vfio_ap_mdev_ioctl,<br>
> > +};<br>
> > +<br>
> > static const struct mdev_parent_ops vfio_ap_matrix_ops = {<br>
> > .owner = THIS_MODULE,<br>
> > .supported_type_groups = vfio_ap_mdev_type_groups,<br>
> > .mdev_attr_groups = vfio_ap_mdev_attr_groups,<br>
> > .create = vfio_ap_mdev_create,<br>
> > .remove = vfio_ap_mdev_remove,<br>
> > - .open = vfio_ap_mdev_open,<br>
> > - .release = vfio_ap_mdev_release,<br>
> > - .ioctl = vfio_ap_mdev_ioctl,<br>
> > };<br>
> ><br>
> > int vfio_ap_mdev_register(void)<br>
> > diff --git a/drivers/vfio/mdev/mdev_core.c<br>
> b/drivers/vfio/mdev/mdev_core.c<br>
> > index 8764cf4a276d..6f35f2ced2c9 100644<br>
> > --- a/drivers/vfio/mdev/mdev_core.c<br>
> > +++ b/drivers/vfio/mdev/mdev_core.c<br>
> > @@ -51,6 +51,18 @@ void mdev_set_class_id(struct mdev_device *mdev,<br>
> u16 id)<br>
> > }<br>
> > EXPORT_SYMBOL(mdev_set_class_id);<br>
> ><br>
> > +const void *mdev_get_dev_ops(struct mdev_device *mdev)<br>
> > +{<br>
> > + return mdev->device_ops;<br>
> > +}<br>
> > +EXPORT_SYMBOL(mdev_get_dev_ops);<br>
> > +<br>
> > +void mdev_set_dev_ops(struct mdev_device *mdev, const void *ops)<br>
> > +{<br>
> > + mdev->device_ops = ops;<br>
> > +}<br>
> > +EXPORT_SYMBOL(mdev_set_dev_ops);<br>
> > +<br>
> > struct device *mdev_dev(struct mdev_device *mdev)<br>
> > {<br>
> > return &mdev->dev;<br>
> > diff --git a/drivers/vfio/mdev/mdev_private.h<br>
> b/drivers/vfio/mdev/mdev_private.h<br>
> > index c65f436c1869..b666805f0b1f 100644<br>
> > --- a/drivers/vfio/mdev/mdev_private.h<br>
> > +++ b/drivers/vfio/mdev/mdev_private.h<br>
> > @@ -34,6 +34,7 @@ struct mdev_device {<br>
> > struct device *iommu_device;<br>
> > bool active;<br>
> > u16 class_id;<br>
> > + const void *device_ops;<br>
> > };<br>
> ><br>
> > #define to_mdev_device(dev) container_of(dev, struct<br>
> mdev_device, dev)<br>
> > diff --git a/drivers/vfio/mdev/vfio_mdev.c<br>
> b/drivers/vfio/mdev/vfio_mdev.c<br>
> > index 891cf83a2d9a..95efa054442f 100644<br>
> > --- a/drivers/vfio/mdev/vfio_mdev.c<br>
> > +++ b/drivers/vfio/mdev/vfio_mdev.c<br>
> > @@ -14,6 +14,7 @@<br>
> > #include <linux/slab.h><br>
> > #include <linux/vfio.h><br>
> > #include <linux/mdev.h><br>
> > +#include <linux/vfio_mdev.h><br>
> ><br>
> > #include "mdev_private.h"<br>
> ><br>
> > @@ -24,16 +25,16 @@<br>
> > static int vfio_mdev_open(void *device_data)<br>
> > {<br>
> > struct mdev_device *mdev = device_data;<br>
> > - struct mdev_parent *parent = mdev->parent;<br>
> > + const struct vfio_mdev_device_ops *ops =<br>
> mdev_get_dev_ops(mdev);<br>
> > int ret;<br>
> ><br>
> > - if (unlikely(!parent->ops->open))<br>
> > + if (unlikely(!ops->open))<br>
> > return -EINVAL;<br>
> ><br>
> > if (!try_module_get(THIS_MODULE))<br>
> > return -ENODEV;<br></blockquote><div><br></div><div>RJM>] My understanding lately is that this call to try_module_get(THIS_MODULE) is no longer needed as is considered as a latent bug.</div><div><span style="color:rgb(36,39,41);font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;font-size:15px">Quote from </span><a href="https://stackoverflow.com/questions/1741415/linux-kernel-modules-when-to-use-try-module-get-module-put">https://stackoverflow.com/questions/1741415/linux-kernel-modules-when-to-use-try-module-get-module-put</a><span style="color:rgb(36,39,41);font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;font-size:15px"> :</span></div><div><span style="color:rgb(36,39,41);font-family:Arial,"Helvetica Neue",Helvetica,sans-serif;font-size:15px">There are a number of uses of try_module_get(THIS_MODULE) in the kernel source but most if not all of them are latent bugs that should be cleaned up.</span> </div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> ><br>
> > - ret = parent->ops->open(mdev);<br>
> > + ret = ops->open(mdev);<br>
> > if (ret)<br>
> > module_put(THIS_MODULE);<br>
> ><br>
> > @@ -43,10 +44,10 @@ static int vfio_mdev_open(void *device_data)<br>
> > static void vfio_mdev_release(void *device_data)<br>
> > {<br>
> > struct mdev_device *mdev = device_data;<br>
> > - struct mdev_parent *parent = mdev->parent;<br>
> > + const struct vfio_mdev_device_ops *ops =<br>
> mdev_get_dev_ops(mdev);<br>
> ><br>
> > - if (likely(parent->ops->release))<br>
> > - parent->ops->release(mdev);<br>
> > + if (likely(ops->release))<br>
> > + ops->release(mdev);<br>
> ><br>
> > module_put(THIS_MODULE);<br>
> > }<br>
> > @@ -55,47 +56,47 @@ static long vfio_mdev_unlocked_ioctl(void<br>
> *device_data,<br>
> > unsigned int cmd, unsigned long arg)<br>
> > {<br>
> > struct mdev_device *mdev = device_data;<br>
> > - struct mdev_parent *parent = mdev->parent;<br>
> > + const struct vfio_mdev_device_ops *ops =<br>
> mdev_get_dev_ops(mdev);<br>
> ><br>
> > - if (unlikely(!parent->ops->ioctl))<br>
> > + if (unlikely(!ops->ioctl))<br>
> > return -EINVAL;<br>
> ><br>
> > - return parent->ops->ioctl(mdev, cmd, arg);<br>
> > + return ops->ioctl(mdev, cmd, arg);<br>
> > }<br>
> ><br>
> > static ssize_t vfio_mdev_read(void *device_data, char __user *buf,<br>
> > size_t count, loff_t *ppos)<br>
> > {<br>
> > struct mdev_device *mdev = device_data;<br>
> > - struct mdev_parent *parent = mdev->parent;<br>
> > + const struct vfio_mdev_device_ops *ops =<br>
> mdev_get_dev_ops(mdev);<br>
> ><br>
> > - if (unlikely(!parent->ops->read))<br>
> > + if (unlikely(!ops->read))<br>
> > return -EINVAL;<br>
> ><br>
> > - return parent->ops->read(mdev, buf, count, ppos);<br>
> > + return ops->read(mdev, buf, count, ppos);<br>
> > }<br>
> ><br>
> > static ssize_t vfio_mdev_write(void *device_data, const char __user *buf,<br>
> > size_t count, loff_t *ppos)<br>
> > {<br>
> > struct mdev_device *mdev = device_data;<br>
> > - struct mdev_parent *parent = mdev->parent;<br>
> > + const struct vfio_mdev_device_ops *ops =<br>
> mdev_get_dev_ops(mdev);<br>
> ><br>
> > - if (unlikely(!parent->ops->write))<br>
> > + if (unlikely(!ops->write))<br>
> > return -EINVAL;<br>
> ><br>
> > - return parent->ops->write(mdev, buf, count, ppos);<br>
> > + return ops->write(mdev, buf, count, ppos);<br>
> > }<br>
> ><br>
> > static int vfio_mdev_mmap(void *device_data, struct vm_area_struct<br>
> *vma)<br>
> > {<br>
> > struct mdev_device *mdev = device_data;<br>
> > - struct mdev_parent *parent = mdev->parent;<br>
> > + const struct vfio_mdev_device_ops *ops =<br>
> mdev_get_dev_ops(mdev);<br>
> ><br>
> > - if (unlikely(!parent->ops->mmap))<br>
> > + if (unlikely(!ops->mmap))<br>
> > return -EINVAL;<br>
> ><br>
> > - return parent->ops->mmap(mdev, vma);<br>
> > + return ops->mmap(mdev, vma);<br>
> > }<br>
> ><br>
> > static const struct vfio_device_ops vfio_mdev_dev_ops = {<br>
> > diff --git a/include/linux/mdev.h b/include/linux/mdev.h<br>
> > index 3974650c074f..3414307311f1 100644<br>
> > --- a/include/linux/mdev.h<br>
> > +++ b/include/linux/mdev.h<br>
> > @@ -10,6 +10,11 @@<br>
> > #ifndef MDEV_H<br>
> > #define MDEV_H<br>
> ><br>
> > +#include <linux/types.h><br>
> > +#include <linux/device.h><br>
> > +#include <linux/mdev.h><br>
> > +#include <uapi/linux/uuid.h><br>
> > +<br>
> > struct mdev_device;<br>
> ><br>
> > /*<br>
> > @@ -48,30 +53,8 @@ struct device *mdev_get_iommu_device(struct<br>
> device *dev);<br>
> > * @mdev: mdev_device device structure which is<br>
> being<br>
> > * destroyed<br>
> > * Returns integer: success (0) or error (< 0)<br>
> > - * @open: Open mediated device.<br>
> > - * @mdev: mediated device.<br>
> > - * Returns integer: success (0) or error (< 0)<br>
> > - * @release: release mediated device<br>
> > - * @mdev: mediated device.<br>
> > - * @read: Read emulation callback<br>
> > - * @mdev: mediated device structure<br>
> > - * @buf: read buffer<br>
> > - * @count: number of bytes to read<br>
> > - * @ppos: address.<br>
> > - * Retuns number on bytes read on success or error.<br>
> > - * @write: Write emulation callback<br>
> > - * @mdev: mediated device structure<br>
> > - * @buf: write buffer<br>
> > - * @count: number of bytes to be written<br>
> > - * @ppos: address.<br>
> > - * Retuns number on bytes written on success or error.<br>
> > - * @ioctl: IOCTL callback<br>
> > - * @mdev: mediated device structure<br>
> > - * @cmd: ioctl command<br>
> > - * @arg: arguments to ioctl<br>
> > - * @mmap: mmap callback<br>
> > - * @mdev: mediated device structure<br>
> > - * @vma: vma structure<br>
> > + * @device_ops: Device specific emulation callback.<br>
> > + *<br>
> > * Parent device that support mediated device should be registered with<br>
> mdev<br>
> > * module with mdev_parent_ops structure.<br>
> > **/<br>
> > @@ -83,15 +66,6 @@ struct mdev_parent_ops {<br>
> ><br>
> > int (*create)(struct kobject *kobj, struct mdev_device *mdev);<br>
> > int (*remove)(struct mdev_device *mdev);<br>
> > - int (*open)(struct mdev_device *mdev);<br>
> > - void (*release)(struct mdev_device *mdev);<br>
> > - ssize_t (*read)(struct mdev_device *mdev, char __user *buf,<br>
> > - size_t count, loff_t *ppos);<br>
> > - ssize_t (*write)(struct mdev_device *mdev, const char __user *buf,<br>
> > - size_t count, loff_t *ppos);<br>
> > - long (*ioctl)(struct mdev_device *mdev, unsigned int cmd,<br>
> > - unsigned long arg);<br>
> > - int (*mmap)(struct mdev_device *mdev, struct vm_area_struct<br>
> *vma);<br>
> > };<br>
> ><br>
> > /* interface for exporting mdev supported type attributes */<br>
> > @@ -133,6 +107,8 @@ struct mdev_driver {<br>
> ><br>
> > void *mdev_get_drvdata(struct mdev_device *mdev);<br>
> > void mdev_set_drvdata(struct mdev_device *mdev, void *data);<br>
> > +const void *mdev_get_dev_ops(struct mdev_device *mdev);<br>
> > +void mdev_set_dev_ops(struct mdev_device *mdev, const void *ops);<br>
> > void mdev_set_class_id(struct mdev_device *mdev, u16 id);<br>
> > const guid_t *mdev_uuid(struct mdev_device *mdev);<br>
> ><br>
> > diff --git a/include/linux/vfio_mdev.h b/include/linux/vfio_mdev.h<br>
> > new file mode 100644<br>
> > index 000000000000..3907c5371c2b<br>
> > --- /dev/null<br>
> > +++ b/include/linux/vfio_mdev.h<br>
> > @@ -0,0 +1,52 @@<br>
> > +/* SPDX-License-Identifier: GPL-2.0-only */<br>
> > +/*<br>
> > + * VFIO Mediated device definition<br>
> > + */<br>
> > +<br>
> > +#ifndef VFIO_MDEV_H<br>
> > +#define VFIO_MDEV_H<br>
> > +<br>
> > +#include <linux/mdev.h><br>
> > +<br>
> > +/**<br>
> > + * struct vfio_mdev_device_ops - Structure to be registered for each<br>
> > + * mdev device to register the device to vfio-mdev module.<br>
> > + *<br>
> > + * @open: Open mediated device.<br>
> > + * @mdev: mediated device.<br>
> > + * Returns integer: success (0) or error (< 0)<br>
> > + * @release: release mediated device<br>
> > + * @mdev: mediated device.<br>
> > + * @read: Read emulation callback<br>
> > + * @mdev: mediated device structure<br>
> > + * @buf: read buffer<br>
> > + * @count: number of bytes to read<br>
> > + * @ppos: address.<br>
> > + * Retuns number on bytes read on success or error.<br>
> > + * @write: Write emulation callback<br>
> > + * @mdev: mediated device structure<br>
> > + * @buf: write buffer<br>
> > + * @count: number of bytes to be written<br>
> > + * @ppos: address.<br>
> > + * Retuns number on bytes written on success or error.<br>
> > + * @ioctl: IOCTL callback<br>
> > + * @mdev: mediated device structure<br>
> > + * @cmd: ioctl command<br>
> > + * @arg: arguments to ioctl<br>
> > + * @mmap: mmap callback<br>
> > + * @mdev: mediated device structure<br>
> > + * @vma: vma structure<br>
> > + */<br>
> > +struct vfio_mdev_device_ops {<br>
> > + int (*open)(struct mdev_device *mdev);<br>
> > + void (*release)(struct mdev_device *mdev);<br>
> > + ssize_t (*read)(struct mdev_device *mdev, char __user *buf,<br>
> > + size_t count, loff_t *ppos);<br>
> > + ssize_t (*write)(struct mdev_device *mdev, const char __user *buf,<br>
> > + size_t count, loff_t *ppos);<br>
> > + long (*ioctl)(struct mdev_device *mdev, unsigned int cmd,<br>
> > + unsigned long arg);<br>
> > + int (*mmap)(struct mdev_device *mdev, struct vm_area_struct<br>
> *vma);<br>
> > +};<br>
> > +<br>
> > +#endif<br>
> > diff --git a/samples/vfio-mdev/mbochs.c b/samples/vfio-mdev/mbochs.c<br>
> > index 8a8583c892b2..b4bf29c6136c 100644<br>
> > --- a/samples/vfio-mdev/mbochs.c<br>
> > +++ b/samples/vfio-mdev/mbochs.c<br>
> > @@ -30,6 +30,7 @@<br>
> > #include <linux/iommu.h><br>
> > #include <linux/sysfs.h><br>
> > #include <linux/mdev.h><br>
> > +#include <linux/vfio_mdev.h><br>
> > #include <linux/pci.h><br>
> > #include <linux/dma-buf.h><br>
> > #include <linux/highmem.h><br>
> > @@ -516,6 +517,8 @@ static int mbochs_reset(struct mdev_device<br>
> *mdev)<br>
> > return 0;<br>
> > }<br>
> ><br>
> > +static const struct vfio_mdev_device_ops vfio_mdev_ops;<br>
> > +<br>
> > static int mbochs_create(struct kobject *kobj, struct mdev_device *mdev)<br>
> > {<br>
> > const struct mbochs_type *type = mbochs_find_type(kobj);<br>
> > @@ -562,6 +565,7 @@ static int mbochs_create(struct kobject *kobj,<br>
> struct mdev_device *mdev)<br>
> ><br>
> > mbochs_used_mbytes += type->mbytes;<br>
> > mdev_set_class_id(mdev, MDEV_ID_VFIO);<br>
> > + mdev_set_dev_ops(mdev, &vfio_mdev_ops);<br>
> > return 0;<br>
> ><br>
> > err_mem:<br>
> > @@ -1419,12 +1423,7 @@ static struct attribute_group<br>
> *mdev_type_groups[] = {<br>
> > NULL,<br>
> > };<br>
> ><br>
> > -static const struct mdev_parent_ops mdev_fops = {<br>
> > - .owner = THIS_MODULE,<br>
> > - .mdev_attr_groups = mdev_dev_groups,<br>
> > - .supported_type_groups = mdev_type_groups,<br>
> > - .create = mbochs_create,<br>
> > - .remove = mbochs_remove,<br>
> > +static const struct vfio_mdev_device_ops vfio_mdev_ops = {<br>
> > .open = mbochs_open,<br>
> > .release = mbochs_close,<br>
> > .read = mbochs_read,<br>
> > @@ -1433,6 +1432,14 @@ static const struct mdev_parent_ops<br>
> mdev_fops = {<br>
> > .mmap = mbochs_mmap,<br>
> > };<br>
> ><br>
> > +static const struct mdev_parent_ops mdev_fops = {<br>
> > + .owner = THIS_MODULE,<br>
> > + .mdev_attr_groups = mdev_dev_groups,<br>
> > + .supported_type_groups = mdev_type_groups,<br>
> > + .create = mbochs_create,<br>
> > + .remove = mbochs_remove,<br>
> > +};<br>
> > +<br>
> > static const struct file_operations vd_fops = {<br>
> > .owner = THIS_MODULE,<br>
> > };<br>
> > diff --git a/samples/vfio-mdev/mdpy.c b/samples/vfio-mdev/mdpy.c<br>
> > index 88d7e76f3836..80c2df531326 100644<br>
> > --- a/samples/vfio-mdev/mdpy.c<br>
> > +++ b/samples/vfio-mdev/mdpy.c<br>
> > @@ -26,6 +26,7 @@<br>
> > #include <linux/iommu.h><br>
> > #include <linux/sysfs.h><br>
> > #include <linux/mdev.h><br>
> > +#include <linux/vfio_mdev.h><br>
> > #include <linux/pci.h><br>
> > #include <drm/drm_fourcc.h><br>
> > #include "mdpy-defs.h"<br>
> > @@ -226,6 +227,8 @@ static int mdpy_reset(struct mdev_device *mdev)<br>
> > return 0;<br>
> > }<br>
> ><br>
> > +static const struct vfio_mdev_device_ops vfio_mdev_ops;<br>
> > +<br>
> > static int mdpy_create(struct kobject *kobj, struct mdev_device *mdev)<br>
> > {<br>
> > const struct mdpy_type *type = mdpy_find_type(kobj);<br>
> > @@ -270,6 +273,7 @@ static int mdpy_create(struct kobject *kobj, struct<br>
> mdev_device *mdev)<br>
> ><br>
> > mdpy_count++;<br>
> > mdev_set_class_id(mdev, MDEV_ID_VFIO);<br>
> > + mdev_set_dev_ops(mdev, &vfio_mdev_ops);<br>
> > return 0;<br>
> > }<br>
> ><br>
> > @@ -726,12 +730,7 @@ static struct attribute_group<br>
> *mdev_type_groups[] = {<br>
> > NULL,<br>
> > };<br>
> ><br>
> > -static const struct mdev_parent_ops mdev_fops = {<br>
> > - .owner = THIS_MODULE,<br>
> > - .mdev_attr_groups = mdev_dev_groups,<br>
> > - .supported_type_groups = mdev_type_groups,<br>
> > - .create = mdpy_create,<br>
> > - .remove = mdpy_remove,<br>
> > +static const struct vfio_mdev_device_ops vfio_mdev_ops = {<br>
> > .open = mdpy_open,<br>
> > .release = mdpy_close,<br>
> > .read = mdpy_read,<br>
> > @@ -740,6 +739,14 @@ static const struct mdev_parent_ops mdev_fops<br>
> = {<br>
> > .mmap = mdpy_mmap,<br>
> > };<br>
> ><br>
> > +static const struct mdev_parent_ops mdev_fops = {<br>
> > + .owner = THIS_MODULE,<br>
> > + .mdev_attr_groups = mdev_dev_groups,<br>
> > + .supported_type_groups = mdev_type_groups,<br>
> > + .create = mdpy_create,<br>
> > + .remove = mdpy_remove,<br>
> > +};<br>
> > +<br>
> > static const struct file_operations vd_fops = {<br>
> > .owner = THIS_MODULE,<br>
> > };<br>
> > diff --git a/samples/vfio-mdev/mtty.c b/samples/vfio-mdev/mtty.c<br>
> > index 4e0735143b69..2db860ccb02c 100644<br>
> > --- a/samples/vfio-mdev/mtty.c<br>
> > +++ b/samples/vfio-mdev/mtty.c<br>
> > @@ -27,6 +27,7 @@<br>
> > #include <linux/ctype.h><br>
> > #include <linux/file.h><br>
> > #include <linux/mdev.h><br>
> > +#include <linux/vfio_mdev.h><br>
> > #include <linux/pci.h><br>
> > #include <linux/serial.h><br>
> > #include <uapi/linux/serial_reg.h><br>
> > @@ -723,6 +724,8 @@ static ssize_t mdev_access(struct mdev_device<br>
> *mdev, u8 *buf, size_t count,<br>
> > return ret;<br>
> > }<br>
> ><br>
> > +static const struct vfio_mdev_device_ops vfio_dev_ops;<br>
> > +<br>
> > static int mtty_create(struct kobject *kobj, struct mdev_device *mdev)<br>
> > {<br>
> > struct mdev_state *mdev_state;<br>
> > @@ -771,6 +774,7 @@ static int mtty_create(struct kobject *kobj, struct<br>
> mdev_device *mdev)<br>
> > mutex_unlock(&mdev_list_lock);<br>
> ><br>
> > mdev_set_class_id(mdev, MDEV_ID_VFIO);<br>
> > + mdev_set_dev_ops(mdev, &vfio_dev_ops);<br>
> > return 0;<br>
> > }<br>
> ><br>
> > @@ -1411,6 +1415,14 @@ static struct attribute_group<br>
> *mdev_type_groups[] = {<br>
> > NULL,<br>
> > };<br>
> ><br>
> > +static const struct vfio_mdev_device_ops vfio_dev_ops = {<br>
> > + .open = mtty_open,<br>
> > + .release = mtty_close,<br>
> > + .read = mtty_read,<br>
> > + .write = mtty_write,<br>
> > + .ioctl = mtty_ioctl,<br>
> > +};<br>
> > +<br>
> > static const struct mdev_parent_ops mdev_fops = {<br>
> > .owner = THIS_MODULE,<br>
> > .dev_attr_groups = mtty_dev_groups,<br>
> > @@ -1418,11 +1430,6 @@ static const struct mdev_parent_ops<br>
> mdev_fops = {<br>
> > .supported_type_groups = mdev_type_groups,<br>
> > .create = mtty_create,<br>
> > .remove = mtty_remove,<br>
> > - .open = mtty_open,<br>
> > - .release = mtty_close,<br>
> > - .read = mtty_read,<br>
> > - .write = mtty_write,<br>
> > - .ioctl = mtty_ioctl,<br>
> > };<br>
> ><br>
> > static void mtty_device_release(struct device *dev)<br>
> <br>
> _______________________________________________<br>
> intel-gvt-dev mailing list<br>
> <a href="mailto:intel-gvt-dev@lists.freedesktop.org" target="_blank">intel-gvt-dev@lists.freedesktop.org</a><br>
> <a href="https://lists.freedesktop.org/mailman/listinfo/intel-gvt-dev" rel="noreferrer" target="_blank">https://lists.freedesktop.org/mailman/listinfo/intel-gvt-dev</a><br>
</blockquote></div></div>