kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 00/18] Make vfio_mdev type safe
@ 2021-03-23 17:55 Jason Gunthorpe
  2021-03-23 17:55 ` [PATCH 01/18] vfio/mdev: Fix missing static's on MDEV_TYPE_ATTR's Jason Gunthorpe
                   ` (15 more replies)
  0 siblings, 16 replies; 69+ messages in thread
From: Jason Gunthorpe @ 2021-03-23 17:55 UTC (permalink / raw)
  To: David Airlie, Tony Krowiak, Alex Williamson,
	Christian Borntraeger, Cornelia Huck, Jonathan Corbet,
	Daniel Vetter, dri-devel, Eric Farman, Harald Freudenberger,
	Vasily Gorbik, Heiko Carstens, intel-gfx, intel-gvt-dev,
	Jani Nikula, Joonas Lahtinen, kvm, Kirti Wankhede, linux-doc,
	linux-s390, Peter Oberparleiter, Halil Pasic, Pierre Morel,
	Rodrigo Vivi, Vineeth Vijayan, Zhenyu Wang, Zhi Wang
  Cc: Raj, Ashok, Dong Jia Shi, Neo Jia, Dan Williams,
	Christoph Hellwig, Jike Song, Gerd Hoffmann, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

Prologue
========

This is series #2 in part of a larger work that arose from the minor
remark that the mdev_parent_ops indirection shim is useless and
complicates things.

It follows the "Embed struct vfio_device in all sub-structures" already
sent, and must be applied on top of it.

A preview of the future series's is here:
  https://github.com/jgunthorpe/linux/pull/3/commits

========
This series:

vfio_mdev has a number of different objects: mdev_parent, mdev_type and
mdev_device.

Unfortunately the types of these have been erased in various places
throughout the API, and this makes it very hard to understand this code or
maintain it by the time it reaches all of the drivers.

This series puts in all the types and aligns some of the design with the
driver core standard for a driver core bus driver:

 - Replace 'struct device *' with 'struct mdev_device *
 - Replace 'struct device *' with 'struct mdev_type *' and
   mtype_get_parent_dev()
 - Replace 'struct kobject *' with 'struct mdev_type *'

Now that types are clear it is easy to spot a few places that have
duplicated information.

More significantly we can now understand how to directly fix the
obfuscated 'kobj->name' matching by realizing the the kobj is a mdev_type,
which is linked to the supported_types_list provided by the driver, and
thus the core code can directly return the array indexes all the drivers
actually want.

Jason

Jason Gunthorpe (18):
  vfio/mdev: Fix missing static's on MDEV_TYPE_ATTR's
  vfio/mdev: Add missing typesafety around mdev_device
  vfio/mdev: Simplify driver registration
  vfio/mdev: Use struct mdev_type in struct mdev_device
  vfio/mdev: Do not allow a mdev_type to have a NULL parent pointer
  vfio/mdev: Expose mdev_get/put_parent to mdev_private.h
  vfio/mdev: Add missing reference counting to mdev_type
  vfio/mdev: Reorganize mdev_device_create()
  vfio/mdev: Add missing error handling to dev_set_name()
  vfio/mdev: Remove duplicate storage of parent in mdev_device
  vfio/mdev: Add mdev/mtype_get_type_group_id()
  vfio/mtty: Use mdev_get_type_group_id()
  vfio/mdpy: Use mdev_get_type_group_id()
  vfio/mbochs: Use mdev_get_type_group_id()
  vfio/gvt: Make DRM_I915_GVT depend on VFIO_MDEV
  vfio/gvt: Use mdev_get_type_group_id()
  vfio/mdev: Remove kobj from mdev_parent_ops->create()
  vfio/mdev: Correct the function signatures for the
    mdev_type_attributes

 .../driver-api/vfio-mediated-device.rst       |   9 +-
 drivers/gpu/drm/i915/Kconfig                  |   1 +
 drivers/gpu/drm/i915/gvt/gvt.c                |  41 ++---
 drivers/gpu/drm/i915/gvt/gvt.h                |   4 +-
 drivers/gpu/drm/i915/gvt/kvmgt.c              |   7 +-
 drivers/s390/cio/vfio_ccw_ops.c               |  17 +-
 drivers/s390/crypto/vfio_ap_ops.c             |  14 +-
 drivers/vfio/mdev/mdev_core.c                 | 160 ++++++------------
 drivers/vfio/mdev/mdev_driver.c               |  19 +--
 drivers/vfio/mdev/mdev_private.h              |  40 ++---
 drivers/vfio/mdev/mdev_sysfs.c                |  59 ++++---
 drivers/vfio/mdev/vfio_mdev.c                 |  29 ++--
 drivers/vfio/vfio_iommu_type1.c               |  25 +--
 include/linux/mdev.h                          |  80 ++++++---
 samples/vfio-mdev/mbochs.c                    |  55 +++---
 samples/vfio-mdev/mdpy.c                      |  56 +++---
 samples/vfio-mdev/mtty.c                      |  66 ++------
 17 files changed, 306 insertions(+), 376 deletions(-)

-- 
2.31.0


^ permalink raw reply	[flat|nested] 69+ messages in thread

* [PATCH 01/18] vfio/mdev: Fix missing static's on MDEV_TYPE_ATTR's
  2021-03-23 17:55 [PATCH 00/18] Make vfio_mdev type safe Jason Gunthorpe
@ 2021-03-23 17:55 ` Jason Gunthorpe
  2021-03-23 17:55 ` [PATCH 02/18] vfio/mdev: Add missing typesafety around mdev_device Jason Gunthorpe
                   ` (14 subsequent siblings)
  15 siblings, 0 replies; 69+ messages in thread
From: Jason Gunthorpe @ 2021-03-23 17:55 UTC (permalink / raw)
  To: kvm, Kirti Wankhede
  Cc: Alex Williamson, Raj, Ashok, Dan Williams, Christoph Hellwig,
	Gerd Hoffmann, Leon Romanovsky, Max Gurtovoy, Tarun Gupta

These should always be prefixed with static, otherwise compilation
will fail on non-modular builds with

ld: samples/vfio-mdev/mbochs.o:(.data+0x2e0): multiple definition of `mdev_type_attr_name'; samples/vfio-mdev/mdpy.o:(.data+0x240): first defined here

Fixes: a5e6e6505f38 ("sample: vfio bochs vbe display (host device for bochs-drm)")
Fixes: d61fc96f47fd ("sample: vfio mdev display - host device")
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 samples/vfio-mdev/mbochs.c | 10 +++++-----
 samples/vfio-mdev/mdpy.c   | 10 +++++-----
 2 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/samples/vfio-mdev/mbochs.c b/samples/vfio-mdev/mbochs.c
index e03068917273d4..365afbe2dea558 100644
--- a/samples/vfio-mdev/mbochs.c
+++ b/samples/vfio-mdev/mbochs.c
@@ -1334,7 +1334,7 @@ static const struct attribute_group mdev_dev_group = {
 	.attrs = mdev_dev_attrs,
 };
 
-const struct attribute_group *mdev_dev_groups[] = {
+static const struct attribute_group *mdev_dev_groups[] = {
 	&mdev_dev_group,
 	NULL,
 };
@@ -1344,7 +1344,7 @@ name_show(struct kobject *kobj, struct device *dev, char *buf)
 {
 	return sprintf(buf, "%s\n", kobj->name);
 }
-MDEV_TYPE_ATTR_RO(name);
+static MDEV_TYPE_ATTR_RO(name);
 
 static ssize_t
 description_show(struct kobject *kobj, struct device *dev, char *buf)
@@ -1354,7 +1354,7 @@ description_show(struct kobject *kobj, struct device *dev, char *buf)
 	return sprintf(buf, "virtual display, %d MB video memory\n",
 		       type ? type->mbytes  : 0);
 }
-MDEV_TYPE_ATTR_RO(description);
+static MDEV_TYPE_ATTR_RO(description);
 
 static ssize_t
 available_instances_show(struct kobject *kobj, struct device *dev, char *buf)
@@ -1364,14 +1364,14 @@ available_instances_show(struct kobject *kobj, struct device *dev, char *buf)
 
 	return sprintf(buf, "%d\n", count);
 }
-MDEV_TYPE_ATTR_RO(available_instances);
+static MDEV_TYPE_ATTR_RO(available_instances);
 
 static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
 			       char *buf)
 {
 	return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
 }
-MDEV_TYPE_ATTR_RO(device_api);
+static MDEV_TYPE_ATTR_RO(device_api);
 
 static struct attribute *mdev_types_attrs[] = {
 	&mdev_type_attr_name.attr,
diff --git a/samples/vfio-mdev/mdpy.c b/samples/vfio-mdev/mdpy.c
index 9894693f3be178..d4ec2b52ca49a1 100644
--- a/samples/vfio-mdev/mdpy.c
+++ b/samples/vfio-mdev/mdpy.c
@@ -658,7 +658,7 @@ static const struct attribute_group mdev_dev_group = {
 	.attrs = mdev_dev_attrs,
 };
 
-const struct attribute_group *mdev_dev_groups[] = {
+static const struct attribute_group *mdev_dev_groups[] = {
 	&mdev_dev_group,
 	NULL,
 };
@@ -668,7 +668,7 @@ name_show(struct kobject *kobj, struct device *dev, char *buf)
 {
 	return sprintf(buf, "%s\n", kobj->name);
 }
-MDEV_TYPE_ATTR_RO(name);
+static MDEV_TYPE_ATTR_RO(name);
 
 static ssize_t
 description_show(struct kobject *kobj, struct device *dev, char *buf)
@@ -679,21 +679,21 @@ description_show(struct kobject *kobj, struct device *dev, char *buf)
 		       type ? type->width  : 0,
 		       type ? type->height : 0);
 }
-MDEV_TYPE_ATTR_RO(description);
+static MDEV_TYPE_ATTR_RO(description);
 
 static ssize_t
 available_instances_show(struct kobject *kobj, struct device *dev, char *buf)
 {
 	return sprintf(buf, "%d\n", max_devices - mdpy_count);
 }
-MDEV_TYPE_ATTR_RO(available_instances);
+static MDEV_TYPE_ATTR_RO(available_instances);
 
 static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
 			       char *buf)
 {
 	return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
 }
-MDEV_TYPE_ATTR_RO(device_api);
+static MDEV_TYPE_ATTR_RO(device_api);
 
 static struct attribute *mdev_types_attrs[] = {
 	&mdev_type_attr_name.attr,
-- 
2.31.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 02/18] vfio/mdev: Add missing typesafety around mdev_device
  2021-03-23 17:55 [PATCH 00/18] Make vfio_mdev type safe Jason Gunthorpe
  2021-03-23 17:55 ` [PATCH 01/18] vfio/mdev: Fix missing static's on MDEV_TYPE_ATTR's Jason Gunthorpe
@ 2021-03-23 17:55 ` Jason Gunthorpe
  2021-03-26  2:04   ` Tian, Kevin
  2021-03-30 15:24   ` Cornelia Huck
  2021-03-23 17:55 ` [PATCH 03/18] vfio/mdev: Simplify driver registration Jason Gunthorpe
                   ` (13 subsequent siblings)
  15 siblings, 2 replies; 69+ messages in thread
From: Jason Gunthorpe @ 2021-03-23 17:55 UTC (permalink / raw)
  To: Alex Williamson, Cornelia Huck, Jonathan Corbet, kvm,
	Kirti Wankhede, linux-doc
  Cc: Raj, Ashok, Dan Williams, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

The mdev API should accept and pass a 'struct mdev_device *' in all
places, not pass a 'struct device *' and cast it internally with
to_mdev_device(). Particularly in its struct mdev_driver functions, the
whole point of a bus's struct device_driver wrapper is to provide type
safety compared to the default struct device_driver.

Further, the driver core standard is for bus drivers to expose their
device structure in their public headers that can be used with
container_of() inlines and '&foo->dev' to go between the class levels, and
'&foo->dev' to be used with dev_err/etc driver core helper functions. Move
'struct mdev_device' to mdev.h

Once done this allows moving some one instruction exported functions to
static inlines, which in turns allows removing one of the two grotesque
symbol_get()'s related to mdev in the core code.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 .../driver-api/vfio-mediated-device.rst       |  4 +-
 drivers/vfio/mdev/mdev_core.c                 | 64 ++-----------------
 drivers/vfio/mdev/mdev_driver.c               |  4 +-
 drivers/vfio/mdev/mdev_private.h              | 23 +------
 drivers/vfio/mdev/mdev_sysfs.c                | 26 ++++----
 drivers/vfio/mdev/vfio_mdev.c                 |  7 +-
 drivers/vfio/vfio_iommu_type1.c               | 25 ++------
 include/linux/mdev.h                          | 58 +++++++++++++----
 8 files changed, 83 insertions(+), 128 deletions(-)

diff --git a/Documentation/driver-api/vfio-mediated-device.rst b/Documentation/driver-api/vfio-mediated-device.rst
index 25eb7d5b834ba3..c43c1dc3333373 100644
--- a/Documentation/driver-api/vfio-mediated-device.rst
+++ b/Documentation/driver-api/vfio-mediated-device.rst
@@ -105,8 +105,8 @@ structure to represent a mediated device's driver::
       */
      struct mdev_driver {
 	     const char *name;
-	     int  (*probe)  (struct device *dev);
-	     void (*remove) (struct device *dev);
+	     int  (*probe)  (struct mdev_device *dev);
+	     void (*remove) (struct mdev_device *dev);
 	     struct device_driver    driver;
      };
 
diff --git a/drivers/vfio/mdev/mdev_core.c b/drivers/vfio/mdev/mdev_core.c
index 6de97d25a3f87d..057922a1707e04 100644
--- a/drivers/vfio/mdev/mdev_core.c
+++ b/drivers/vfio/mdev/mdev_core.c
@@ -33,36 +33,6 @@ struct device *mdev_parent_dev(struct mdev_device *mdev)
 }
 EXPORT_SYMBOL(mdev_parent_dev);
 
-void *mdev_get_drvdata(struct mdev_device *mdev)
-{
-	return mdev->driver_data;
-}
-EXPORT_SYMBOL(mdev_get_drvdata);
-
-void mdev_set_drvdata(struct mdev_device *mdev, void *data)
-{
-	mdev->driver_data = data;
-}
-EXPORT_SYMBOL(mdev_set_drvdata);
-
-struct device *mdev_dev(struct mdev_device *mdev)
-{
-	return &mdev->dev;
-}
-EXPORT_SYMBOL(mdev_dev);
-
-struct mdev_device *mdev_from_dev(struct device *dev)
-{
-	return dev_is_mdev(dev) ? to_mdev_device(dev) : NULL;
-}
-EXPORT_SYMBOL(mdev_from_dev);
-
-const guid_t *mdev_uuid(struct mdev_device *mdev)
-{
-	return &mdev->uuid;
-}
-EXPORT_SYMBOL(mdev_uuid);
-
 /* Should be called holding parent_list_lock */
 static struct mdev_parent *__find_parent_device(struct device *dev)
 {
@@ -107,7 +77,7 @@ static void mdev_device_remove_common(struct mdev_device *mdev)
 	int ret;
 
 	type = to_mdev_type(mdev->type_kobj);
-	mdev_remove_sysfs_files(&mdev->dev, type);
+	mdev_remove_sysfs_files(mdev, type);
 	device_del(&mdev->dev);
 	parent = mdev->parent;
 	lockdep_assert_held(&parent->unreg_sem);
@@ -122,12 +92,10 @@ static void mdev_device_remove_common(struct mdev_device *mdev)
 
 static int mdev_device_remove_cb(struct device *dev, void *data)
 {
-	if (dev_is_mdev(dev)) {
-		struct mdev_device *mdev;
+	struct mdev_device *mdev = mdev_from_dev(dev);
 
-		mdev = to_mdev_device(dev);
+	if (mdev)
 		mdev_device_remove_common(mdev);
-	}
 	return 0;
 }
 
@@ -332,7 +300,7 @@ int mdev_device_create(struct kobject *kobj,
 	if (ret)
 		goto add_fail;
 
-	ret = mdev_create_sysfs_files(&mdev->dev, type);
+	ret = mdev_create_sysfs_files(mdev, type);
 	if (ret)
 		goto sysfs_fail;
 
@@ -354,13 +322,11 @@ int mdev_device_create(struct kobject *kobj,
 	return ret;
 }
 
-int mdev_device_remove(struct device *dev)
+int mdev_device_remove(struct mdev_device *mdev)
 {
-	struct mdev_device *mdev, *tmp;
+	struct mdev_device *tmp;
 	struct mdev_parent *parent;
 
-	mdev = to_mdev_device(dev);
-
 	mutex_lock(&mdev_list_lock);
 	list_for_each_entry(tmp, &mdev_list, next) {
 		if (tmp == mdev)
@@ -390,24 +356,6 @@ int mdev_device_remove(struct device *dev)
 	return 0;
 }
 
-int mdev_set_iommu_device(struct device *dev, struct device *iommu_device)
-{
-	struct mdev_device *mdev = to_mdev_device(dev);
-
-	mdev->iommu_device = iommu_device;
-
-	return 0;
-}
-EXPORT_SYMBOL(mdev_set_iommu_device);
-
-struct device *mdev_get_iommu_device(struct device *dev)
-{
-	struct mdev_device *mdev = to_mdev_device(dev);
-
-	return mdev->iommu_device;
-}
-EXPORT_SYMBOL(mdev_get_iommu_device);
-
 static int __init mdev_init(void)
 {
 	return mdev_bus_register();
diff --git a/drivers/vfio/mdev/mdev_driver.c b/drivers/vfio/mdev/mdev_driver.c
index 0d3223aee20b83..44c3ba7e56d923 100644
--- a/drivers/vfio/mdev/mdev_driver.c
+++ b/drivers/vfio/mdev/mdev_driver.c
@@ -48,7 +48,7 @@ static int mdev_probe(struct device *dev)
 		return ret;
 
 	if (drv && drv->probe) {
-		ret = drv->probe(dev);
+		ret = drv->probe(mdev);
 		if (ret)
 			mdev_detach_iommu(mdev);
 	}
@@ -62,7 +62,7 @@ static int mdev_remove(struct device *dev)
 	struct mdev_device *mdev = to_mdev_device(dev);
 
 	if (drv && drv->remove)
-		drv->remove(dev);
+		drv->remove(mdev);
 
 	mdev_detach_iommu(mdev);
 
diff --git a/drivers/vfio/mdev/mdev_private.h b/drivers/vfio/mdev/mdev_private.h
index 74c2e541146999..bb60ec4a8d9d21 100644
--- a/drivers/vfio/mdev/mdev_private.h
+++ b/drivers/vfio/mdev/mdev_private.h
@@ -24,23 +24,6 @@ struct mdev_parent {
 	struct rw_semaphore unreg_sem;
 };
 
-struct mdev_device {
-	struct device dev;
-	struct mdev_parent *parent;
-	guid_t uuid;
-	void *driver_data;
-	struct list_head next;
-	struct kobject *type_kobj;
-	struct device *iommu_device;
-	bool active;
-};
-
-static inline struct mdev_device *to_mdev_device(struct device *dev)
-{
-	return container_of(dev, struct mdev_device, dev);
-}
-#define dev_is_mdev(d)		((d)->bus == &mdev_bus_type)
-
 struct mdev_type {
 	struct kobject kobj;
 	struct kobject *devices_kobj;
@@ -57,11 +40,11 @@ struct mdev_type {
 int  parent_create_sysfs_files(struct mdev_parent *parent);
 void parent_remove_sysfs_files(struct mdev_parent *parent);
 
-int  mdev_create_sysfs_files(struct device *dev, struct mdev_type *type);
-void mdev_remove_sysfs_files(struct device *dev, struct mdev_type *type);
+int  mdev_create_sysfs_files(struct mdev_device *mdev, struct mdev_type *type);
+void mdev_remove_sysfs_files(struct mdev_device *mdev, struct mdev_type *type);
 
 int  mdev_device_create(struct kobject *kobj,
 			struct device *dev, const guid_t *uuid);
-int  mdev_device_remove(struct device *dev);
+int  mdev_device_remove(struct mdev_device *dev);
 
 #endif /* MDEV_PRIVATE_H */
diff --git a/drivers/vfio/mdev/mdev_sysfs.c b/drivers/vfio/mdev/mdev_sysfs.c
index 917fd84c1c6f24..6a5450587b79e9 100644
--- a/drivers/vfio/mdev/mdev_sysfs.c
+++ b/drivers/vfio/mdev/mdev_sysfs.c
@@ -225,6 +225,7 @@ int parent_create_sysfs_files(struct mdev_parent *parent)
 static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
 			    const char *buf, size_t count)
 {
+	struct mdev_device *mdev = to_mdev_device(dev);
 	unsigned long val;
 
 	if (kstrtoul(buf, 0, &val) < 0)
@@ -233,7 +234,7 @@ static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
 	if (val && device_remove_file_self(dev, attr)) {
 		int ret;
 
-		ret = mdev_device_remove(dev);
+		ret = mdev_device_remove(mdev);
 		if (ret)
 			return ret;
 	}
@@ -248,34 +249,37 @@ static const struct attribute *mdev_device_attrs[] = {
 	NULL,
 };
 
-int  mdev_create_sysfs_files(struct device *dev, struct mdev_type *type)
+int mdev_create_sysfs_files(struct mdev_device *mdev, struct mdev_type *type)
 {
+	struct kobject *kobj = &mdev->dev.kobj;
 	int ret;
 
-	ret = sysfs_create_link(type->devices_kobj, &dev->kobj, dev_name(dev));
+	ret = sysfs_create_link(type->devices_kobj, kobj, dev_name(&mdev->dev));
 	if (ret)
 		return ret;
 
-	ret = sysfs_create_link(&dev->kobj, &type->kobj, "mdev_type");
+	ret = sysfs_create_link(kobj, &type->kobj, "mdev_type");
 	if (ret)
 		goto type_link_failed;
 
-	ret = sysfs_create_files(&dev->kobj, mdev_device_attrs);
+	ret = sysfs_create_files(kobj, mdev_device_attrs);
 	if (ret)
 		goto create_files_failed;
 
 	return ret;
 
 create_files_failed:
-	sysfs_remove_link(&dev->kobj, "mdev_type");
+	sysfs_remove_link(kobj, "mdev_type");
 type_link_failed:
-	sysfs_remove_link(type->devices_kobj, dev_name(dev));
+	sysfs_remove_link(type->devices_kobj, dev_name(&mdev->dev));
 	return ret;
 }
 
-void mdev_remove_sysfs_files(struct device *dev, struct mdev_type *type)
+void mdev_remove_sysfs_files(struct mdev_device *mdev, struct mdev_type *type)
 {
-	sysfs_remove_files(&dev->kobj, mdev_device_attrs);
-	sysfs_remove_link(&dev->kobj, "mdev_type");
-	sysfs_remove_link(type->devices_kobj, dev_name(dev));
+	struct kobject *kobj = &mdev->dev.kobj;
+
+	sysfs_remove_files(kobj, mdev_device_attrs);
+	sysfs_remove_link(kobj, "mdev_type");
+	sysfs_remove_link(type->devices_kobj, dev_name(&mdev->dev));
 }
diff --git a/drivers/vfio/mdev/vfio_mdev.c b/drivers/vfio/mdev/vfio_mdev.c
index ae7e322fbe3c26..91b7b8b9eb9cb8 100644
--- a/drivers/vfio/mdev/vfio_mdev.c
+++ b/drivers/vfio/mdev/vfio_mdev.c
@@ -124,9 +124,8 @@ static const struct vfio_device_ops vfio_mdev_dev_ops = {
 	.request	= vfio_mdev_request,
 };
 
-static int vfio_mdev_probe(struct device *dev)
+static int vfio_mdev_probe(struct mdev_device *mdev)
 {
-	struct mdev_device *mdev = to_mdev_device(dev);
 	struct vfio_device *vdev;
 	int ret;
 
@@ -144,9 +143,9 @@ static int vfio_mdev_probe(struct device *dev)
 	return 0;
 }
 
-static void vfio_mdev_remove(struct device *dev)
+static void vfio_mdev_remove(struct mdev_device *mdev)
 {
-	struct vfio_device *vdev = dev_get_drvdata(dev);
+	struct vfio_device *vdev = dev_get_drvdata(&mdev->dev);
 
 	vfio_unregister_group_dev(vdev);
 	kfree(vdev);
diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c
index 4bb162c1d649b3..90b45ff1d87a7b 100644
--- a/drivers/vfio/vfio_iommu_type1.c
+++ b/drivers/vfio/vfio_iommu_type1.c
@@ -1923,28 +1923,13 @@ static bool vfio_iommu_has_sw_msi(struct list_head *group_resv_regions,
 	return ret;
 }
 
-static struct device *vfio_mdev_get_iommu_device(struct device *dev)
-{
-	struct device *(*fn)(struct device *dev);
-	struct device *iommu_device;
-
-	fn = symbol_get(mdev_get_iommu_device);
-	if (fn) {
-		iommu_device = fn(dev);
-		symbol_put(mdev_get_iommu_device);
-
-		return iommu_device;
-	}
-
-	return NULL;
-}
-
 static int vfio_mdev_attach_domain(struct device *dev, void *data)
 {
+	struct mdev_device *mdev = to_mdev_device(dev);
 	struct iommu_domain *domain = data;
 	struct device *iommu_device;
 
-	iommu_device = vfio_mdev_get_iommu_device(dev);
+	iommu_device = mdev_get_iommu_device(mdev);
 	if (iommu_device) {
 		if (iommu_dev_feature_enabled(iommu_device, IOMMU_DEV_FEAT_AUX))
 			return iommu_aux_attach_device(domain, iommu_device);
@@ -1957,10 +1942,11 @@ static int vfio_mdev_attach_domain(struct device *dev, void *data)
 
 static int vfio_mdev_detach_domain(struct device *dev, void *data)
 {
+	struct mdev_device *mdev = to_mdev_device(dev);
 	struct iommu_domain *domain = data;
 	struct device *iommu_device;
 
-	iommu_device = vfio_mdev_get_iommu_device(dev);
+	iommu_device = mdev_get_iommu_device(mdev);
 	if (iommu_device) {
 		if (iommu_dev_feature_enabled(iommu_device, IOMMU_DEV_FEAT_AUX))
 			iommu_aux_detach_device(domain, iommu_device);
@@ -2008,9 +1994,10 @@ static bool vfio_bus_is_mdev(struct bus_type *bus)
 
 static int vfio_mdev_iommu_device(struct device *dev, void *data)
 {
+	struct mdev_device *mdev = to_mdev_device(dev);
 	struct device **old = data, *new;
 
-	new = vfio_mdev_get_iommu_device(dev);
+	new = mdev_get_iommu_device(mdev);
 	if (!new || (*old && *old != new))
 		return -EINVAL;
 
diff --git a/include/linux/mdev.h b/include/linux/mdev.h
index 27eb383cb95de0..52f7ea19dd0f56 100644
--- a/include/linux/mdev.h
+++ b/include/linux/mdev.h
@@ -10,7 +10,21 @@
 #ifndef MDEV_H
 #define MDEV_H
 
-struct mdev_device;
+struct mdev_device {
+	struct device dev;
+	struct mdev_parent *parent;
+	guid_t uuid;
+	void *driver_data;
+	struct list_head next;
+	struct kobject *type_kobj;
+	struct device *iommu_device;
+	bool active;
+};
+
+static inline struct mdev_device *to_mdev_device(struct device *dev)
+{
+	return container_of(dev, struct mdev_device, dev);
+}
 
 /*
  * Called by the parent device driver to set the device which represents
@@ -19,12 +33,17 @@ struct mdev_device;
  *
  * @dev: the mediated device that iommu will isolate.
  * @iommu_device: a pci device which represents the iommu for @dev.
- *
- * Return 0 for success, otherwise negative error value.
  */
-int mdev_set_iommu_device(struct device *dev, struct device *iommu_device);
+static inline void mdev_set_iommu_device(struct mdev_device *mdev,
+					 struct device *iommu_device)
+{
+	mdev->iommu_device = iommu_device;
+}
 
-struct device *mdev_get_iommu_device(struct device *dev);
+static inline struct device *mdev_get_iommu_device(struct mdev_device *mdev)
+{
+	return mdev->iommu_device;
+}
 
 /**
  * struct mdev_parent_ops - Structure to be registered for each parent device to
@@ -126,16 +145,25 @@ struct mdev_type_attribute mdev_type_attr_##_name =		\
  **/
 struct mdev_driver {
 	const char *name;
-	int  (*probe)(struct device *dev);
-	void (*remove)(struct device *dev);
+	int (*probe)(struct mdev_device *dev);
+	void (*remove)(struct mdev_device *dev);
 	struct device_driver driver;
 };
 
 #define to_mdev_driver(drv)	container_of(drv, struct mdev_driver, driver)
 
-void *mdev_get_drvdata(struct mdev_device *mdev);
-void mdev_set_drvdata(struct mdev_device *mdev, void *data);
-const guid_t *mdev_uuid(struct mdev_device *mdev);
+static inline void *mdev_get_drvdata(struct mdev_device *mdev)
+{
+	return mdev->driver_data;
+}
+static inline void mdev_set_drvdata(struct mdev_device *mdev, void *data)
+{
+	mdev->driver_data = data;
+}
+static inline const guid_t *mdev_uuid(struct mdev_device *mdev)
+{
+	return &mdev->uuid;
+}
 
 extern struct bus_type mdev_bus_type;
 
@@ -146,7 +174,13 @@ int mdev_register_driver(struct mdev_driver *drv, struct module *owner);
 void mdev_unregister_driver(struct mdev_driver *drv);
 
 struct device *mdev_parent_dev(struct mdev_device *mdev);
-struct device *mdev_dev(struct mdev_device *mdev);
-struct mdev_device *mdev_from_dev(struct device *dev);
+static inline struct device *mdev_dev(struct mdev_device *mdev)
+{
+	return &mdev->dev;
+}
+static inline struct mdev_device *mdev_from_dev(struct device *dev)
+{
+	return dev->bus == &mdev_bus_type ? to_mdev_device(dev) : NULL;
+}
 
 #endif /* MDEV_H */
-- 
2.31.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 03/18] vfio/mdev: Simplify driver registration
  2021-03-23 17:55 [PATCH 00/18] Make vfio_mdev type safe Jason Gunthorpe
  2021-03-23 17:55 ` [PATCH 01/18] vfio/mdev: Fix missing static's on MDEV_TYPE_ATTR's Jason Gunthorpe
  2021-03-23 17:55 ` [PATCH 02/18] vfio/mdev: Add missing typesafety around mdev_device Jason Gunthorpe
@ 2021-03-23 17:55 ` Jason Gunthorpe
  2021-03-23 19:14   ` Christoph Hellwig
                     ` (2 more replies)
  2021-03-23 17:55 ` [PATCH 04/18] vfio/mdev: Use struct mdev_type in struct mdev_device Jason Gunthorpe
                   ` (12 subsequent siblings)
  15 siblings, 3 replies; 69+ messages in thread
From: Jason Gunthorpe @ 2021-03-23 17:55 UTC (permalink / raw)
  To: Alex Williamson, Cornelia Huck, Jonathan Corbet, kvm,
	Kirti Wankhede, linux-doc
  Cc: Raj, Ashok, Dan Williams, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

This is only done once, we don't need to generate code to initialize a
structure stored in the ELF .data segment. Fill in the three required
.driver members directly instead of copying data into them during
mdev_register_driver().

Further the to_mdev_driver() function doesn't belong in a public header,
just inline it into the two places that need it. Finally, we can now
clearly see that 'drv' derived from dev->driver cannot be NULL, firstly
because the driver core forbids it, and secondly because NULL won't pass
through the container_of(). Remove the dead code.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 Documentation/driver-api/vfio-mediated-device.rst |  5 +----
 drivers/vfio/mdev/mdev_driver.c                   | 15 +++++++--------
 drivers/vfio/mdev/vfio_mdev.c                     |  8 ++++++--
 include/linux/mdev.h                              |  6 +-----
 4 files changed, 15 insertions(+), 19 deletions(-)

diff --git a/Documentation/driver-api/vfio-mediated-device.rst b/Documentation/driver-api/vfio-mediated-device.rst
index c43c1dc3333373..1779b85f014e2f 100644
--- a/Documentation/driver-api/vfio-mediated-device.rst
+++ b/Documentation/driver-api/vfio-mediated-device.rst
@@ -98,13 +98,11 @@ structure to represent a mediated device's driver::
 
      /*
       * struct mdev_driver [2] - Mediated device's driver
-      * @name: driver name
       * @probe: called when new device created
       * @remove: called when device removed
       * @driver: device driver structure
       */
      struct mdev_driver {
-	     const char *name;
 	     int  (*probe)  (struct mdev_device *dev);
 	     void (*remove) (struct mdev_device *dev);
 	     struct device_driver    driver;
@@ -115,8 +113,7 @@ to register and unregister itself with the core driver:
 
 * Register::
 
-    extern int  mdev_register_driver(struct mdev_driver *drv,
-				   struct module *owner);
+    extern int  mdev_register_driver(struct mdev_driver *drv);
 
 * Unregister::
 
diff --git a/drivers/vfio/mdev/mdev_driver.c b/drivers/vfio/mdev/mdev_driver.c
index 44c3ba7e56d923..041699571b7e55 100644
--- a/drivers/vfio/mdev/mdev_driver.c
+++ b/drivers/vfio/mdev/mdev_driver.c
@@ -39,7 +39,8 @@ static void mdev_detach_iommu(struct mdev_device *mdev)
 
 static int mdev_probe(struct device *dev)
 {
-	struct mdev_driver *drv = to_mdev_driver(dev->driver);
+	struct mdev_driver *drv =
+		container_of(dev->driver, struct mdev_driver, driver);
 	struct mdev_device *mdev = to_mdev_device(dev);
 	int ret;
 
@@ -47,7 +48,7 @@ static int mdev_probe(struct device *dev)
 	if (ret)
 		return ret;
 
-	if (drv && drv->probe) {
+	if (drv->probe) {
 		ret = drv->probe(mdev);
 		if (ret)
 			mdev_detach_iommu(mdev);
@@ -58,10 +59,11 @@ static int mdev_probe(struct device *dev)
 
 static int mdev_remove(struct device *dev)
 {
-	struct mdev_driver *drv = to_mdev_driver(dev->driver);
+	struct mdev_driver *drv =
+		container_of(dev->driver, struct mdev_driver, driver);
 	struct mdev_device *mdev = to_mdev_device(dev);
 
-	if (drv && drv->remove)
+	if (drv->remove)
 		drv->remove(mdev);
 
 	mdev_detach_iommu(mdev);
@@ -79,16 +81,13 @@ EXPORT_SYMBOL_GPL(mdev_bus_type);
 /**
  * mdev_register_driver - register a new MDEV driver
  * @drv: the driver to register
- * @owner: module owner of driver to be registered
  *
  * Returns a negative value on error, otherwise 0.
  **/
-int mdev_register_driver(struct mdev_driver *drv, struct module *owner)
+int mdev_register_driver(struct mdev_driver *drv)
 {
 	/* initialize common driver fields */
-	drv->driver.name = drv->name;
 	drv->driver.bus = &mdev_bus_type;
-	drv->driver.owner = owner;
 
 	/* register with core */
 	return driver_register(&drv->driver);
diff --git a/drivers/vfio/mdev/vfio_mdev.c b/drivers/vfio/mdev/vfio_mdev.c
index 91b7b8b9eb9cb8..cc9507ed85a181 100644
--- a/drivers/vfio/mdev/vfio_mdev.c
+++ b/drivers/vfio/mdev/vfio_mdev.c
@@ -152,14 +152,18 @@ static void vfio_mdev_remove(struct mdev_device *mdev)
 }
 
 static struct mdev_driver vfio_mdev_driver = {
-	.name	= "vfio_mdev",
+	.driver = {
+		.name = "vfio_mdev",
+		.owner = THIS_MODULE,
+		.mod_name = KBUILD_MODNAME,
+	},
 	.probe	= vfio_mdev_probe,
 	.remove	= vfio_mdev_remove,
 };
 
 static int __init vfio_mdev_init(void)
 {
-	return mdev_register_driver(&vfio_mdev_driver, THIS_MODULE);
+	return mdev_register_driver(&vfio_mdev_driver);
 }
 
 static void __exit vfio_mdev_exit(void)
diff --git a/include/linux/mdev.h b/include/linux/mdev.h
index 52f7ea19dd0f56..cb771c712da0f4 100644
--- a/include/linux/mdev.h
+++ b/include/linux/mdev.h
@@ -137,21 +137,17 @@ struct mdev_type_attribute mdev_type_attr_##_name =		\
 
 /**
  * struct mdev_driver - Mediated device driver
- * @name: driver name
  * @probe: called when new device created
  * @remove: called when device removed
  * @driver: device driver structure
  *
  **/
 struct mdev_driver {
-	const char *name;
 	int (*probe)(struct mdev_device *dev);
 	void (*remove)(struct mdev_device *dev);
 	struct device_driver driver;
 };
 
-#define to_mdev_driver(drv)	container_of(drv, struct mdev_driver, driver)
-
 static inline void *mdev_get_drvdata(struct mdev_device *mdev)
 {
 	return mdev->driver_data;
@@ -170,7 +166,7 @@ extern struct bus_type mdev_bus_type;
 int mdev_register_device(struct device *dev, const struct mdev_parent_ops *ops);
 void mdev_unregister_device(struct device *dev);
 
-int mdev_register_driver(struct mdev_driver *drv, struct module *owner);
+int mdev_register_driver(struct mdev_driver *drv);
 void mdev_unregister_driver(struct mdev_driver *drv);
 
 struct device *mdev_parent_dev(struct mdev_device *mdev);
-- 
2.31.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 04/18] vfio/mdev: Use struct mdev_type in struct mdev_device
  2021-03-23 17:55 [PATCH 00/18] Make vfio_mdev type safe Jason Gunthorpe
                   ` (2 preceding siblings ...)
  2021-03-23 17:55 ` [PATCH 03/18] vfio/mdev: Simplify driver registration Jason Gunthorpe
@ 2021-03-23 17:55 ` Jason Gunthorpe
  2021-03-23 19:15   ` Christoph Hellwig
                     ` (2 more replies)
  2021-03-23 17:55 ` [PATCH 05/18] vfio/mdev: Do not allow a mdev_type to have a NULL parent pointer Jason Gunthorpe
                   ` (11 subsequent siblings)
  15 siblings, 3 replies; 69+ messages in thread
From: Jason Gunthorpe @ 2021-03-23 17:55 UTC (permalink / raw)
  To: Alex Williamson, Cornelia Huck, kvm, Kirti Wankhede
  Cc: Raj, Ashok, Dan Williams, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

The kobj pointer in mdev_device is actually pointing at a struct
mdev_type. Use the proper type so things are understandable.

There are a number of places that are confused and passing both the mdev
and the mtype as function arguments, fix these to derive the mtype
directly from the mdev to remove the redundancy.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 drivers/vfio/mdev/mdev_core.c    | 16 ++++++----------
 drivers/vfio/mdev/mdev_private.h |  7 +++----
 drivers/vfio/mdev/mdev_sysfs.c   | 15 ++++++++-------
 include/linux/mdev.h             |  4 +++-
 4 files changed, 20 insertions(+), 22 deletions(-)

diff --git a/drivers/vfio/mdev/mdev_core.c b/drivers/vfio/mdev/mdev_core.c
index 057922a1707e04..5ca0efa5266bad 100644
--- a/drivers/vfio/mdev/mdev_core.c
+++ b/drivers/vfio/mdev/mdev_core.c
@@ -73,11 +73,9 @@ static void mdev_put_parent(struct mdev_parent *parent)
 static void mdev_device_remove_common(struct mdev_device *mdev)
 {
 	struct mdev_parent *parent;
-	struct mdev_type *type;
 	int ret;
 
-	type = to_mdev_type(mdev->type_kobj);
-	mdev_remove_sysfs_files(mdev, type);
+	mdev_remove_sysfs_files(mdev);
 	device_del(&mdev->dev);
 	parent = mdev->parent;
 	lockdep_assert_held(&parent->unreg_sem);
@@ -241,13 +239,11 @@ static void mdev_device_release(struct device *dev)
 	mdev_device_free(mdev);
 }
 
-int mdev_device_create(struct kobject *kobj,
-		       struct device *dev, const guid_t *uuid)
+int mdev_device_create(struct mdev_type *type, const guid_t *uuid)
 {
 	int ret;
 	struct mdev_device *mdev, *tmp;
 	struct mdev_parent *parent;
-	struct mdev_type *type = to_mdev_type(kobj);
 
 	parent = mdev_get_parent(type->parent);
 	if (!parent)
@@ -285,14 +281,14 @@ int mdev_device_create(struct kobject *kobj,
 	}
 
 	device_initialize(&mdev->dev);
-	mdev->dev.parent  = dev;
+	mdev->dev.parent = parent->dev;
 	mdev->dev.bus     = &mdev_bus_type;
 	mdev->dev.release = mdev_device_release;
 	dev_set_name(&mdev->dev, "%pUl", uuid);
 	mdev->dev.groups = parent->ops->mdev_attr_groups;
-	mdev->type_kobj = kobj;
+	mdev->type = type;
 
-	ret = parent->ops->create(kobj, mdev);
+	ret = parent->ops->create(&type->kobj, mdev);
 	if (ret)
 		goto ops_create_fail;
 
@@ -300,7 +296,7 @@ int mdev_device_create(struct kobject *kobj,
 	if (ret)
 		goto add_fail;
 
-	ret = mdev_create_sysfs_files(mdev, type);
+	ret = mdev_create_sysfs_files(mdev);
 	if (ret)
 		goto sysfs_fail;
 
diff --git a/drivers/vfio/mdev/mdev_private.h b/drivers/vfio/mdev/mdev_private.h
index bb60ec4a8d9d21..debf27f95b4f10 100644
--- a/drivers/vfio/mdev/mdev_private.h
+++ b/drivers/vfio/mdev/mdev_private.h
@@ -40,11 +40,10 @@ struct mdev_type {
 int  parent_create_sysfs_files(struct mdev_parent *parent);
 void parent_remove_sysfs_files(struct mdev_parent *parent);
 
-int  mdev_create_sysfs_files(struct mdev_device *mdev, struct mdev_type *type);
-void mdev_remove_sysfs_files(struct mdev_device *mdev, struct mdev_type *type);
+int  mdev_create_sysfs_files(struct mdev_device *mdev);
+void mdev_remove_sysfs_files(struct mdev_device *mdev);
 
-int  mdev_device_create(struct kobject *kobj,
-			struct device *dev, const guid_t *uuid);
+int mdev_device_create(struct mdev_type *kobj, const guid_t *uuid);
 int  mdev_device_remove(struct mdev_device *dev);
 
 #endif /* MDEV_PRIVATE_H */
diff --git a/drivers/vfio/mdev/mdev_sysfs.c b/drivers/vfio/mdev/mdev_sysfs.c
index 6a5450587b79e9..321b4d13ead7b8 100644
--- a/drivers/vfio/mdev/mdev_sysfs.c
+++ b/drivers/vfio/mdev/mdev_sysfs.c
@@ -67,7 +67,7 @@ static ssize_t create_store(struct kobject *kobj, struct device *dev,
 	if (ret)
 		return ret;
 
-	ret = mdev_device_create(kobj, dev, &uuid);
+	ret = mdev_device_create(to_mdev_type(kobj), &uuid);
 	if (ret)
 		return ret;
 
@@ -249,16 +249,17 @@ static const struct attribute *mdev_device_attrs[] = {
 	NULL,
 };
 
-int mdev_create_sysfs_files(struct mdev_device *mdev, struct mdev_type *type)
+int mdev_create_sysfs_files(struct mdev_device *mdev)
 {
 	struct kobject *kobj = &mdev->dev.kobj;
 	int ret;
 
-	ret = sysfs_create_link(type->devices_kobj, kobj, dev_name(&mdev->dev));
+	ret = sysfs_create_link(mdev->type->devices_kobj, kobj,
+				dev_name(&mdev->dev));
 	if (ret)
 		return ret;
 
-	ret = sysfs_create_link(kobj, &type->kobj, "mdev_type");
+	ret = sysfs_create_link(kobj, &mdev->type->kobj, "mdev_type");
 	if (ret)
 		goto type_link_failed;
 
@@ -271,15 +272,15 @@ int mdev_create_sysfs_files(struct mdev_device *mdev, struct mdev_type *type)
 create_files_failed:
 	sysfs_remove_link(kobj, "mdev_type");
 type_link_failed:
-	sysfs_remove_link(type->devices_kobj, dev_name(&mdev->dev));
+	sysfs_remove_link(mdev->type->devices_kobj, dev_name(&mdev->dev));
 	return ret;
 }
 
-void mdev_remove_sysfs_files(struct mdev_device *mdev, struct mdev_type *type)
+void mdev_remove_sysfs_files(struct mdev_device *mdev)
 {
 	struct kobject *kobj = &mdev->dev.kobj;
 
 	sysfs_remove_files(kobj, mdev_device_attrs);
 	sysfs_remove_link(kobj, "mdev_type");
-	sysfs_remove_link(type->devices_kobj, dev_name(&mdev->dev));
+	sysfs_remove_link(mdev->type->devices_kobj, dev_name(&mdev->dev));
 }
diff --git a/include/linux/mdev.h b/include/linux/mdev.h
index cb771c712da0f4..349e8ac1fe3382 100644
--- a/include/linux/mdev.h
+++ b/include/linux/mdev.h
@@ -10,13 +10,15 @@
 #ifndef MDEV_H
 #define MDEV_H
 
+struct mdev_type;
+
 struct mdev_device {
 	struct device dev;
 	struct mdev_parent *parent;
 	guid_t uuid;
 	void *driver_data;
 	struct list_head next;
-	struct kobject *type_kobj;
+	struct mdev_type *type;
 	struct device *iommu_device;
 	bool active;
 };
-- 
2.31.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 05/18] vfio/mdev: Do not allow a mdev_type to have a NULL parent pointer
  2021-03-23 17:55 [PATCH 00/18] Make vfio_mdev type safe Jason Gunthorpe
                   ` (3 preceding siblings ...)
  2021-03-23 17:55 ` [PATCH 04/18] vfio/mdev: Use struct mdev_type in struct mdev_device Jason Gunthorpe
@ 2021-03-23 17:55 ` Jason Gunthorpe
  2021-03-23 19:15   ` Christoph Hellwig
                     ` (3 more replies)
  2021-03-23 17:55 ` [PATCH 06/18] vfio/mdev: Expose mdev_get/put_parent to mdev_private.h Jason Gunthorpe
                   ` (10 subsequent siblings)
  15 siblings, 4 replies; 69+ messages in thread
From: Jason Gunthorpe @ 2021-03-23 17:55 UTC (permalink / raw)
  To: Cornelia Huck, kvm
  Cc: Alex Williamson, Raj, Ashok, Dong Jia Shi, Neo Jia, Dan Williams,
	Christoph Hellwig, Jike Song, Kirti Wankhede, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

There is a small race where the parent is NULL even though the kobj has
already been made visible in sysfs.

For instance the attribute_group is made visible in sysfs_create_files()
and the mdev_type_attr_show() does:

    ret = attr->show(kobj, type->parent->dev, buf);

Which will crash on NULL parent. Move the parent setup to before the type
pointer leaves the stack frame.

Fixes: 7b96953bc640 ("vfio: Mediated device Core driver")
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 drivers/vfio/mdev/mdev_sysfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/vfio/mdev/mdev_sysfs.c b/drivers/vfio/mdev/mdev_sysfs.c
index 321b4d13ead7b8..5219cdd6dbbc49 100644
--- a/drivers/vfio/mdev/mdev_sysfs.c
+++ b/drivers/vfio/mdev/mdev_sysfs.c
@@ -105,6 +105,7 @@ static struct mdev_type *add_mdev_supported_type(struct mdev_parent *parent,
 		return ERR_PTR(-ENOMEM);
 
 	type->kobj.kset = parent->mdev_types_kset;
+	type->parent = parent;
 
 	ret = kobject_init_and_add(&type->kobj, &mdev_type_ktype, NULL,
 				   "%s-%s", dev_driver_string(parent->dev),
@@ -132,7 +133,6 @@ static struct mdev_type *add_mdev_supported_type(struct mdev_parent *parent,
 	}
 
 	type->group = group;
-	type->parent = parent;
 	return type;
 
 attrs_failed:
-- 
2.31.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 06/18] vfio/mdev: Expose mdev_get/put_parent to mdev_private.h
  2021-03-23 17:55 [PATCH 00/18] Make vfio_mdev type safe Jason Gunthorpe
                   ` (4 preceding siblings ...)
  2021-03-23 17:55 ` [PATCH 05/18] vfio/mdev: Do not allow a mdev_type to have a NULL parent pointer Jason Gunthorpe
@ 2021-03-23 17:55 ` Jason Gunthorpe
  2021-03-23 19:16   ` Christoph Hellwig
                     ` (2 more replies)
  2021-03-23 17:55 ` [PATCH 07/18] vfio/mdev: Add missing reference counting to mdev_type Jason Gunthorpe
                   ` (9 subsequent siblings)
  15 siblings, 3 replies; 69+ messages in thread
From: Jason Gunthorpe @ 2021-03-23 17:55 UTC (permalink / raw)
  To: Alex Williamson, Cornelia Huck, kvm, Kirti Wankhede
  Cc: Raj, Ashok, Dan Williams, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

The next patch will use these in mdev_sysfs.c

While here remove the now dead code checks for NULL, a mdev_type can never
have a NULL parent.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 drivers/vfio/mdev/mdev_core.c    | 23 +++--------------------
 drivers/vfio/mdev/mdev_private.h | 12 ++++++++++++
 2 files changed, 15 insertions(+), 20 deletions(-)

diff --git a/drivers/vfio/mdev/mdev_core.c b/drivers/vfio/mdev/mdev_core.c
index 5ca0efa5266bad..7ec21c907397a5 100644
--- a/drivers/vfio/mdev/mdev_core.c
+++ b/drivers/vfio/mdev/mdev_core.c
@@ -45,7 +45,7 @@ static struct mdev_parent *__find_parent_device(struct device *dev)
 	return NULL;
 }
 
-static void mdev_release_parent(struct kref *kref)
+void mdev_release_parent(struct kref *kref)
 {
 	struct mdev_parent *parent = container_of(kref, struct mdev_parent,
 						  ref);
@@ -55,20 +55,6 @@ static void mdev_release_parent(struct kref *kref)
 	put_device(dev);
 }
 
-static struct mdev_parent *mdev_get_parent(struct mdev_parent *parent)
-{
-	if (parent)
-		kref_get(&parent->ref);
-
-	return parent;
-}
-
-static void mdev_put_parent(struct mdev_parent *parent)
-{
-	if (parent)
-		kref_put(&parent->ref, mdev_release_parent);
-}
-
 /* Caller must hold parent unreg_sem read or write lock */
 static void mdev_device_remove_common(struct mdev_device *mdev)
 {
@@ -243,12 +229,9 @@ int mdev_device_create(struct mdev_type *type, const guid_t *uuid)
 {
 	int ret;
 	struct mdev_device *mdev, *tmp;
-	struct mdev_parent *parent;
-
-	parent = mdev_get_parent(type->parent);
-	if (!parent)
-		return -EINVAL;
+	struct mdev_parent *parent = type->parent;
 
+	mdev_get_parent(parent);
 	mutex_lock(&mdev_list_lock);
 
 	/* Check for duplicate */
diff --git a/drivers/vfio/mdev/mdev_private.h b/drivers/vfio/mdev/mdev_private.h
index debf27f95b4f10..10eccc35782c4d 100644
--- a/drivers/vfio/mdev/mdev_private.h
+++ b/drivers/vfio/mdev/mdev_private.h
@@ -46,4 +46,16 @@ void mdev_remove_sysfs_files(struct mdev_device *mdev);
 int mdev_device_create(struct mdev_type *kobj, const guid_t *uuid);
 int  mdev_device_remove(struct mdev_device *dev);
 
+void mdev_release_parent(struct kref *kref);
+
+static inline void mdev_get_parent(struct mdev_parent *parent)
+{
+	kref_get(&parent->ref);
+}
+
+static inline void mdev_put_parent(struct mdev_parent *parent)
+{
+	kref_put(&parent->ref, mdev_release_parent);
+}
+
 #endif /* MDEV_PRIVATE_H */
-- 
2.31.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 07/18] vfio/mdev: Add missing reference counting to mdev_type
  2021-03-23 17:55 [PATCH 00/18] Make vfio_mdev type safe Jason Gunthorpe
                   ` (5 preceding siblings ...)
  2021-03-23 17:55 ` [PATCH 06/18] vfio/mdev: Expose mdev_get/put_parent to mdev_private.h Jason Gunthorpe
@ 2021-03-23 17:55 ` Jason Gunthorpe
  2021-03-23 19:17   ` Christoph Hellwig
                     ` (2 more replies)
  2021-03-23 17:55 ` [PATCH 08/18] vfio/mdev: Reorganize mdev_device_create() Jason Gunthorpe
                   ` (8 subsequent siblings)
  15 siblings, 3 replies; 69+ messages in thread
From: Jason Gunthorpe @ 2021-03-23 17:55 UTC (permalink / raw)
  To: Alex Williamson, Cornelia Huck, kvm, Kirti Wankhede
  Cc: Raj, Ashok, Dan Williams, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

struct mdev_type holds a pointer to the kref'd object struct mdev_parent,
but doesn't hold the kref. The lifetime of the parent becomes implicit
because parent_remove_sysfs_files() is supposed to remove all the access
before the parent can be freed, but this is very hard to reason about.

Make it obviously correct by adding the missing get.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 drivers/vfio/mdev/mdev_sysfs.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/vfio/mdev/mdev_sysfs.c b/drivers/vfio/mdev/mdev_sysfs.c
index 5219cdd6dbbc49..d43775bd0ba340 100644
--- a/drivers/vfio/mdev/mdev_sysfs.c
+++ b/drivers/vfio/mdev/mdev_sysfs.c
@@ -81,6 +81,8 @@ static void mdev_type_release(struct kobject *kobj)
 	struct mdev_type *type = to_mdev_type(kobj);
 
 	pr_debug("Releasing group %s\n", kobj->name);
+	/* Pairs with the get in add_mdev_supported_type() */
+	mdev_put_parent(type->parent);
 	kfree(type);
 }
 
@@ -106,6 +108,8 @@ static struct mdev_type *add_mdev_supported_type(struct mdev_parent *parent,
 
 	type->kobj.kset = parent->mdev_types_kset;
 	type->parent = parent;
+	/* Pairs with the put in mdev_type_release() */
+	mdev_get_parent(parent);
 
 	ret = kobject_init_and_add(&type->kobj, &mdev_type_ktype, NULL,
 				   "%s-%s", dev_driver_string(parent->dev),
-- 
2.31.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 08/18] vfio/mdev: Reorganize mdev_device_create()
  2021-03-23 17:55 [PATCH 00/18] Make vfio_mdev type safe Jason Gunthorpe
                   ` (6 preceding siblings ...)
  2021-03-23 17:55 ` [PATCH 07/18] vfio/mdev: Add missing reference counting to mdev_type Jason Gunthorpe
@ 2021-03-23 17:55 ` Jason Gunthorpe
  2021-03-23 19:20   ` Christoph Hellwig
                     ` (2 more replies)
  2021-03-23 17:55 ` [PATCH 09/18] vfio/mdev: Add missing error handling to dev_set_name() Jason Gunthorpe
                   ` (7 subsequent siblings)
  15 siblings, 3 replies; 69+ messages in thread
From: Jason Gunthorpe @ 2021-03-23 17:55 UTC (permalink / raw)
  To: Alex Williamson, Cornelia Huck, kvm, Kirti Wankhede
  Cc: Raj, Ashok, Dan Williams, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

Once the memory for the struct mdev_device is allocated it should
immediately be device_initialize()'d and filled in so that put_device()
can always be used to undo the allocation.

Place the mdev_get/put_parent() so that they are clearly protecting the
mdev->parent pointer. Move the final put to the release function so that
the lifetime rules are trivial to understand.

Remove mdev_device_free() as the release function via device_put() is now
usable in all cases.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 drivers/vfio/mdev/mdev_core.c | 46 +++++++++++++++--------------------
 1 file changed, 20 insertions(+), 26 deletions(-)

diff --git a/drivers/vfio/mdev/mdev_core.c b/drivers/vfio/mdev/mdev_core.c
index 7ec21c907397a5..517b6fd351b63a 100644
--- a/drivers/vfio/mdev/mdev_core.c
+++ b/drivers/vfio/mdev/mdev_core.c
@@ -71,7 +71,6 @@ static void mdev_device_remove_common(struct mdev_device *mdev)
 
 	/* Balances with device_initialize() */
 	put_device(&mdev->dev);
-	mdev_put_parent(parent);
 }
 
 static int mdev_device_remove_cb(struct device *dev, void *data)
@@ -208,8 +207,13 @@ void mdev_unregister_device(struct device *dev)
 }
 EXPORT_SYMBOL(mdev_unregister_device);
 
-static void mdev_device_free(struct mdev_device *mdev)
+static void mdev_device_release(struct device *dev)
 {
+	struct mdev_device *mdev = to_mdev_device(dev);
+
+	/* Pairs with the get in mdev_device_create() */
+	mdev_put_parent(mdev->parent);
+
 	mutex_lock(&mdev_list_lock);
 	list_del(&mdev->next);
 	mutex_unlock(&mdev_list_lock);
@@ -218,59 +222,50 @@ static void mdev_device_free(struct mdev_device *mdev)
 	kfree(mdev);
 }
 
-static void mdev_device_release(struct device *dev)
-{
-	struct mdev_device *mdev = to_mdev_device(dev);
-
-	mdev_device_free(mdev);
-}
-
 int mdev_device_create(struct mdev_type *type, const guid_t *uuid)
 {
 	int ret;
 	struct mdev_device *mdev, *tmp;
 	struct mdev_parent *parent = type->parent;
 
-	mdev_get_parent(parent);
 	mutex_lock(&mdev_list_lock);
 
 	/* Check for duplicate */
 	list_for_each_entry(tmp, &mdev_list, next) {
 		if (guid_equal(&tmp->uuid, uuid)) {
 			mutex_unlock(&mdev_list_lock);
-			ret = -EEXIST;
-			goto mdev_fail;
+			return -EEXIST;
 		}
 	}
 
 	mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
 	if (!mdev) {
 		mutex_unlock(&mdev_list_lock);
-		ret = -ENOMEM;
-		goto mdev_fail;
+		return -ENOMEM;
 	}
 
+	device_initialize(&mdev->dev);
+	mdev->dev.parent  = parent->dev;
+	mdev->dev.bus = &mdev_bus_type;
+	mdev->dev.release = mdev_device_release;
+	mdev->dev.groups = parent->ops->mdev_attr_groups;
+	mdev->type = type;
+	mdev->parent = parent;
+	/* Pairs with the put in mdev_device_release() */
+	mdev_get_parent(parent);
+
 	guid_copy(&mdev->uuid, uuid);
 	list_add(&mdev->next, &mdev_list);
 	mutex_unlock(&mdev_list_lock);
 
-	mdev->parent = parent;
+	dev_set_name(&mdev->dev, "%pUl", uuid);
 
 	/* Check if parent unregistration has started */
 	if (!down_read_trylock(&parent->unreg_sem)) {
-		mdev_device_free(mdev);
 		ret = -ENODEV;
 		goto mdev_fail;
 	}
 
-	device_initialize(&mdev->dev);
-	mdev->dev.parent = parent->dev;
-	mdev->dev.bus     = &mdev_bus_type;
-	mdev->dev.release = mdev_device_release;
-	dev_set_name(&mdev->dev, "%pUl", uuid);
-	mdev->dev.groups = parent->ops->mdev_attr_groups;
-	mdev->type = type;
-
 	ret = parent->ops->create(&type->kobj, mdev);
 	if (ret)
 		goto ops_create_fail;
@@ -295,9 +290,8 @@ int mdev_device_create(struct mdev_type *type, const guid_t *uuid)
 	parent->ops->remove(mdev);
 ops_create_fail:
 	up_read(&parent->unreg_sem);
-	put_device(&mdev->dev);
 mdev_fail:
-	mdev_put_parent(parent);
+	put_device(&mdev->dev);
 	return ret;
 }
 
-- 
2.31.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 09/18] vfio/mdev: Add missing error handling to dev_set_name()
  2021-03-23 17:55 [PATCH 00/18] Make vfio_mdev type safe Jason Gunthorpe
                   ` (7 preceding siblings ...)
  2021-03-23 17:55 ` [PATCH 08/18] vfio/mdev: Reorganize mdev_device_create() Jason Gunthorpe
@ 2021-03-23 17:55 ` Jason Gunthorpe
  2021-03-23 19:21   ` Christoph Hellwig
                     ` (3 more replies)
  2021-03-23 17:55 ` [PATCH 10/18] vfio/mdev: Remove duplicate storage of parent in mdev_device Jason Gunthorpe
                   ` (6 subsequent siblings)
  15 siblings, 4 replies; 69+ messages in thread
From: Jason Gunthorpe @ 2021-03-23 17:55 UTC (permalink / raw)
  To: Cornelia Huck, kvm
  Cc: Alex Williamson, Raj, Ashok, Dong Jia Shi, Neo Jia, Dan Williams,
	Christoph Hellwig, Jike Song, Kirti Wankhede, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

This can fail, and seems to be a popular target for syzkaller error
injection. Check the error return and unwind with put_device().

Fixes: 7b96953bc640 ("vfio: Mediated device Core driver")
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 drivers/vfio/mdev/mdev_core.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/vfio/mdev/mdev_core.c b/drivers/vfio/mdev/mdev_core.c
index 517b6fd351b63a..4b5e372ed58f26 100644
--- a/drivers/vfio/mdev/mdev_core.c
+++ b/drivers/vfio/mdev/mdev_core.c
@@ -258,7 +258,9 @@ int mdev_device_create(struct mdev_type *type, const guid_t *uuid)
 	list_add(&mdev->next, &mdev_list);
 	mutex_unlock(&mdev_list_lock);
 
-	dev_set_name(&mdev->dev, "%pUl", uuid);
+	ret = dev_set_name(&mdev->dev, "%pUl", uuid);
+	if (ret)
+		goto mdev_fail;
 
 	/* Check if parent unregistration has started */
 	if (!down_read_trylock(&parent->unreg_sem)) {
-- 
2.31.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 10/18] vfio/mdev: Remove duplicate storage of parent in mdev_device
  2021-03-23 17:55 [PATCH 00/18] Make vfio_mdev type safe Jason Gunthorpe
                   ` (8 preceding siblings ...)
  2021-03-23 17:55 ` [PATCH 09/18] vfio/mdev: Add missing error handling to dev_set_name() Jason Gunthorpe
@ 2021-03-23 17:55 ` Jason Gunthorpe
  2021-03-23 19:22   ` Christoph Hellwig
                     ` (2 more replies)
  2021-03-23 17:55 ` [PATCH 11/18] vfio/mdev: Add mdev/mtype_get_type_group_id() Jason Gunthorpe
                   ` (5 subsequent siblings)
  15 siblings, 3 replies; 69+ messages in thread
From: Jason Gunthorpe @ 2021-03-23 17:55 UTC (permalink / raw)
  To: Alex Williamson, Cornelia Huck, kvm, Kirti Wankhede
  Cc: Raj, Ashok, Dan Williams, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

mdev_device->type->parent is the same thing.

The struct mdev_device was relying on the kref on the mdev_parent to also
indirectly hold a kref on the mdev_type pointer. Now that the type holds a
kref on the parent we can directly kref the mdev_type and remove this
implicit relationship.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 drivers/vfio/mdev/mdev_core.c | 13 +++++--------
 drivers/vfio/mdev/vfio_mdev.c | 14 +++++++-------
 include/linux/mdev.h          |  1 -
 3 files changed, 12 insertions(+), 16 deletions(-)

diff --git a/drivers/vfio/mdev/mdev_core.c b/drivers/vfio/mdev/mdev_core.c
index 4b5e372ed58f26..493df3da451339 100644
--- a/drivers/vfio/mdev/mdev_core.c
+++ b/drivers/vfio/mdev/mdev_core.c
@@ -29,7 +29,7 @@ static DEFINE_MUTEX(mdev_list_lock);
 
 struct device *mdev_parent_dev(struct mdev_device *mdev)
 {
-	return mdev->parent->dev;
+	return mdev->type->parent->dev;
 }
 EXPORT_SYMBOL(mdev_parent_dev);
 
@@ -58,12 +58,11 @@ void mdev_release_parent(struct kref *kref)
 /* Caller must hold parent unreg_sem read or write lock */
 static void mdev_device_remove_common(struct mdev_device *mdev)
 {
-	struct mdev_parent *parent;
+	struct mdev_parent *parent = mdev->type->parent;
 	int ret;
 
 	mdev_remove_sysfs_files(mdev);
 	device_del(&mdev->dev);
-	parent = mdev->parent;
 	lockdep_assert_held(&parent->unreg_sem);
 	ret = parent->ops->remove(mdev);
 	if (ret)
@@ -212,7 +211,7 @@ static void mdev_device_release(struct device *dev)
 	struct mdev_device *mdev = to_mdev_device(dev);
 
 	/* Pairs with the get in mdev_device_create() */
-	mdev_put_parent(mdev->parent);
+	kobject_put(&mdev->type->kobj);
 
 	mutex_lock(&mdev_list_lock);
 	list_del(&mdev->next);
@@ -250,9 +249,8 @@ int mdev_device_create(struct mdev_type *type, const guid_t *uuid)
 	mdev->dev.release = mdev_device_release;
 	mdev->dev.groups = parent->ops->mdev_attr_groups;
 	mdev->type = type;
-	mdev->parent = parent;
 	/* Pairs with the put in mdev_device_release() */
-	mdev_get_parent(parent);
+	kobject_get(&type->kobj);
 
 	guid_copy(&mdev->uuid, uuid);
 	list_add(&mdev->next, &mdev_list);
@@ -300,7 +298,7 @@ int mdev_device_create(struct mdev_type *type, const guid_t *uuid)
 int mdev_device_remove(struct mdev_device *mdev)
 {
 	struct mdev_device *tmp;
-	struct mdev_parent *parent;
+	struct mdev_parent *parent = mdev->type->parent;
 
 	mutex_lock(&mdev_list_lock);
 	list_for_each_entry(tmp, &mdev_list, next) {
@@ -321,7 +319,6 @@ int mdev_device_remove(struct mdev_device *mdev)
 	mdev->active = false;
 	mutex_unlock(&mdev_list_lock);
 
-	parent = mdev->parent;
 	/* Check if parent unregistration has started */
 	if (!down_read_trylock(&parent->unreg_sem))
 		return -ENODEV;
diff --git a/drivers/vfio/mdev/vfio_mdev.c b/drivers/vfio/mdev/vfio_mdev.c
index cc9507ed85a181..922729071c5a8e 100644
--- a/drivers/vfio/mdev/vfio_mdev.c
+++ b/drivers/vfio/mdev/vfio_mdev.c
@@ -24,7 +24,7 @@
 static int vfio_mdev_open(struct vfio_device *core_vdev)
 {
 	struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
-	struct mdev_parent *parent = mdev->parent;
+	struct mdev_parent *parent = mdev->type->parent;
 
 	int ret;
 
@@ -44,7 +44,7 @@ static int vfio_mdev_open(struct vfio_device *core_vdev)
 static void vfio_mdev_release(struct vfio_device *core_vdev)
 {
 	struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
-	struct mdev_parent *parent = mdev->parent;
+	struct mdev_parent *parent = mdev->type->parent;
 
 	if (likely(parent->ops->release))
 		parent->ops->release(mdev);
@@ -56,7 +56,7 @@ static long vfio_mdev_unlocked_ioctl(struct vfio_device *core_vdev,
 				     unsigned int cmd, unsigned long arg)
 {
 	struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
-	struct mdev_parent *parent = mdev->parent;
+	struct mdev_parent *parent = mdev->type->parent;
 
 	if (unlikely(!parent->ops->ioctl))
 		return -EINVAL;
@@ -68,7 +68,7 @@ static ssize_t vfio_mdev_read(struct vfio_device *core_vdev, char __user *buf,
 			      size_t count, loff_t *ppos)
 {
 	struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
-	struct mdev_parent *parent = mdev->parent;
+	struct mdev_parent *parent = mdev->type->parent;
 
 	if (unlikely(!parent->ops->read))
 		return -EINVAL;
@@ -81,7 +81,7 @@ static ssize_t vfio_mdev_write(struct vfio_device *core_vdev,
 			       loff_t *ppos)
 {
 	struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
-	struct mdev_parent *parent = mdev->parent;
+	struct mdev_parent *parent = mdev->type->parent;
 
 	if (unlikely(!parent->ops->write))
 		return -EINVAL;
@@ -93,7 +93,7 @@ static int vfio_mdev_mmap(struct vfio_device *core_vdev,
 			  struct vm_area_struct *vma)
 {
 	struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
-	struct mdev_parent *parent = mdev->parent;
+	struct mdev_parent *parent = mdev->type->parent;
 
 	if (unlikely(!parent->ops->mmap))
 		return -EINVAL;
@@ -104,7 +104,7 @@ static int vfio_mdev_mmap(struct vfio_device *core_vdev,
 static void vfio_mdev_request(struct vfio_device *core_vdev, unsigned int count)
 {
 	struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
-	struct mdev_parent *parent = mdev->parent;
+	struct mdev_parent *parent = mdev->type->parent;
 
 	if (parent->ops->request)
 		parent->ops->request(mdev, count);
diff --git a/include/linux/mdev.h b/include/linux/mdev.h
index 349e8ac1fe3382..fb582adda28a9b 100644
--- a/include/linux/mdev.h
+++ b/include/linux/mdev.h
@@ -14,7 +14,6 @@ struct mdev_type;
 
 struct mdev_device {
 	struct device dev;
-	struct mdev_parent *parent;
 	guid_t uuid;
 	void *driver_data;
 	struct list_head next;
-- 
2.31.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 11/18] vfio/mdev: Add mdev/mtype_get_type_group_id()
  2021-03-23 17:55 [PATCH 00/18] Make vfio_mdev type safe Jason Gunthorpe
                   ` (9 preceding siblings ...)
  2021-03-23 17:55 ` [PATCH 10/18] vfio/mdev: Remove duplicate storage of parent in mdev_device Jason Gunthorpe
@ 2021-03-23 17:55 ` Jason Gunthorpe
  2021-03-23 19:23   ` Christoph Hellwig
                     ` (2 more replies)
  2021-03-23 17:55 ` [PATCH 12/18] vfio/mtty: Use mdev_get_type_group_id() Jason Gunthorpe
                   ` (4 subsequent siblings)
  15 siblings, 3 replies; 69+ messages in thread
From: Jason Gunthorpe @ 2021-03-23 17:55 UTC (permalink / raw)
  To: Alex Williamson, Cornelia Huck, kvm, Kirti Wankhede
  Cc: Raj, Ashok, Dan Williams, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

This returns the index in the supported_type_groups array that is
associated with the mdev_type attached to the struct mdev_device or its
containing struct kobject.

Each mdev_device can be spawned from exactly one mdev_type, which in turn
originates from exactly one supported_type_group.

Drivers are using weird string calculations to try and get back to this
index, providing a direct access to the index removes a bunch of wonky
driver code.

mdev_type->group can be deleted as the group is obtained using the
type_group_id.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 drivers/vfio/mdev/mdev_core.c    | 20 ++++++++++++++++++++
 drivers/vfio/mdev/mdev_private.h |  2 +-
 drivers/vfio/mdev/mdev_sysfs.c   | 15 +++++++++------
 include/linux/mdev.h             |  3 +++
 4 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/drivers/vfio/mdev/mdev_core.c b/drivers/vfio/mdev/mdev_core.c
index 493df3da451339..3ba5e9464b4d20 100644
--- a/drivers/vfio/mdev/mdev_core.c
+++ b/drivers/vfio/mdev/mdev_core.c
@@ -33,6 +33,26 @@ struct device *mdev_parent_dev(struct mdev_device *mdev)
 }
 EXPORT_SYMBOL(mdev_parent_dev);
 
+/*
+ * Return the index in supported_type_groups that this mdev_device was created
+ * from.
+ */
+unsigned int mdev_get_type_group_id(struct mdev_device *mdev)
+{
+	return mdev->type->type_group_id;
+}
+EXPORT_SYMBOL(mdev_get_type_group_id);
+
+/*
+ * Used in mdev_type_attribute sysfs functions to return the index in the
+ * supported_type_groups that the sysfs is called from.
+ */
+unsigned int mtype_get_type_group_id(struct kobject *mtype_kobj)
+{
+	return container_of(mtype_kobj, struct mdev_type, kobj)->type_group_id;
+}
+EXPORT_SYMBOL(mtype_get_type_group_id);
+
 /* Should be called holding parent_list_lock */
 static struct mdev_parent *__find_parent_device(struct device *dev)
 {
diff --git a/drivers/vfio/mdev/mdev_private.h b/drivers/vfio/mdev/mdev_private.h
index 10eccc35782c4d..a656cfe0346c33 100644
--- a/drivers/vfio/mdev/mdev_private.h
+++ b/drivers/vfio/mdev/mdev_private.h
@@ -29,7 +29,7 @@ struct mdev_type {
 	struct kobject *devices_kobj;
 	struct mdev_parent *parent;
 	struct list_head next;
-	struct attribute_group *group;
+	unsigned int type_group_id;
 };
 
 #define to_mdev_type_attr(_attr)	\
diff --git a/drivers/vfio/mdev/mdev_sysfs.c b/drivers/vfio/mdev/mdev_sysfs.c
index d43775bd0ba340..91ecccdc2f2ec6 100644
--- a/drivers/vfio/mdev/mdev_sysfs.c
+++ b/drivers/vfio/mdev/mdev_sysfs.c
@@ -92,9 +92,11 @@ static struct kobj_type mdev_type_ktype = {
 };
 
 static struct mdev_type *add_mdev_supported_type(struct mdev_parent *parent,
-						 struct attribute_group *group)
+						 unsigned int type_group_id)
 {
 	struct mdev_type *type;
+	struct attribute_group *group =
+		parent->ops->supported_type_groups[type_group_id];
 	int ret;
 
 	if (!group->name) {
@@ -110,6 +112,7 @@ static struct mdev_type *add_mdev_supported_type(struct mdev_parent *parent,
 	type->parent = parent;
 	/* Pairs with the put in mdev_type_release() */
 	mdev_get_parent(parent);
+	type->type_group_id = type_group_id;
 
 	ret = kobject_init_and_add(&type->kobj, &mdev_type_ktype, NULL,
 				   "%s-%s", dev_driver_string(parent->dev),
@@ -135,8 +138,6 @@ static struct mdev_type *add_mdev_supported_type(struct mdev_parent *parent,
 		ret = -ENOMEM;
 		goto attrs_failed;
 	}
-
-	type->group = group;
 	return type;
 
 attrs_failed:
@@ -151,8 +152,11 @@ static struct mdev_type *add_mdev_supported_type(struct mdev_parent *parent,
 
 static void remove_mdev_supported_type(struct mdev_type *type)
 {
+	struct attribute_group *group =
+		type->parent->ops->supported_type_groups[type->type_group_id];
+
 	sysfs_remove_files(&type->kobj,
-			   (const struct attribute **)type->group->attrs);
+			   (const struct attribute **)group->attrs);
 	kobject_put(type->devices_kobj);
 	sysfs_remove_file(&type->kobj, &mdev_type_attr_create.attr);
 	kobject_del(&type->kobj);
@@ -166,8 +170,7 @@ static int add_mdev_supported_type_groups(struct mdev_parent *parent)
 	for (i = 0; parent->ops->supported_type_groups[i]; i++) {
 		struct mdev_type *type;
 
-		type = add_mdev_supported_type(parent,
-					parent->ops->supported_type_groups[i]);
+		type = add_mdev_supported_type(parent, i);
 		if (IS_ERR(type)) {
 			struct mdev_type *ltype, *tmp;
 
diff --git a/include/linux/mdev.h b/include/linux/mdev.h
index fb582adda28a9b..41e91936522394 100644
--- a/include/linux/mdev.h
+++ b/include/linux/mdev.h
@@ -46,6 +46,9 @@ static inline struct device *mdev_get_iommu_device(struct mdev_device *mdev)
 	return mdev->iommu_device;
 }
 
+unsigned int mdev_get_type_group_id(struct mdev_device *mdev);
+unsigned int mtype_get_type_group_id(struct kobject *mtype_kobj);
+
 /**
  * struct mdev_parent_ops - Structure to be registered for each parent device to
  * register the device to mdev module.
-- 
2.31.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 12/18] vfio/mtty: Use mdev_get_type_group_id()
  2021-03-23 17:55 [PATCH 00/18] Make vfio_mdev type safe Jason Gunthorpe
                   ` (10 preceding siblings ...)
  2021-03-23 17:55 ` [PATCH 11/18] vfio/mdev: Add mdev/mtype_get_type_group_id() Jason Gunthorpe
@ 2021-03-23 17:55 ` Jason Gunthorpe
  2021-03-23 19:24   ` Christoph Hellwig
  2021-03-23 17:55 ` [PATCH 13/18] vfio/mdpy: " Jason Gunthorpe
                   ` (3 subsequent siblings)
  15 siblings, 1 reply; 69+ messages in thread
From: Jason Gunthorpe @ 2021-03-23 17:55 UTC (permalink / raw)
  To: kvm, Kirti Wankhede
  Cc: Raj, Ashok, Dan Williams, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

The type_group_id directly gives the single or dual port index, no
need for string searching.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 samples/vfio-mdev/mtty.c | 50 ++++++----------------------------------
 1 file changed, 7 insertions(+), 43 deletions(-)

diff --git a/samples/vfio-mdev/mtty.c b/samples/vfio-mdev/mtty.c
index ce84a300a4dafd..191a587a8d5ab1 100644
--- a/samples/vfio-mdev/mtty.c
+++ b/samples/vfio-mdev/mtty.c
@@ -711,23 +711,7 @@ static ssize_t mdev_access(struct mdev_device *mdev, u8 *buf, size_t count,
 static int mtty_create(struct kobject *kobj, struct mdev_device *mdev)
 {
 	struct mdev_state *mdev_state;
-	char name[MTTY_STRING_LEN];
-	int nr_ports = 0, i;
-
-	if (!mdev)
-		return -EINVAL;
-
-	for (i = 0; i < 2; i++) {
-		snprintf(name, MTTY_STRING_LEN, "%s-%d",
-			dev_driver_string(mdev_parent_dev(mdev)), i + 1);
-		if (!strcmp(kobj->name, name)) {
-			nr_ports = i + 1;
-			break;
-		}
-	}
-
-	if (!nr_ports)
-		return -EINVAL;
+	int nr_ports = mdev_get_type_group_id(mdev) + 1;
 
 	mdev_state = kzalloc(sizeof(struct mdev_state), GFP_KERNEL);
 	if (mdev_state == NULL)
@@ -1311,18 +1295,11 @@ static const struct attribute_group *mdev_dev_groups[] = {
 static ssize_t
 name_show(struct kobject *kobj, struct device *dev, char *buf)
 {
-	char name[MTTY_STRING_LEN];
-	int i;
-	const char *name_str[2] = {"Single port serial", "Dual port serial"};
+	static const char *name_str[2] = { "Single port serial",
+					   "Dual port serial" };
 
-	for (i = 0; i < 2; i++) {
-		snprintf(name, MTTY_STRING_LEN, "%s-%d",
-			 dev_driver_string(dev), i + 1);
-		if (!strcmp(kobj->name, name))
-			return sprintf(buf, "%s\n", name_str[i]);
-	}
-
-	return -EINVAL;
+	return sysfs_emit(buf, "%s\n",
+			  name_str[mtype_get_type_group_id(kobj)]);
 }
 
 static MDEV_TYPE_ATTR_RO(name);
@@ -1330,22 +1307,9 @@ static MDEV_TYPE_ATTR_RO(name);
 static ssize_t
 available_instances_show(struct kobject *kobj, struct device *dev, char *buf)
 {
-	char name[MTTY_STRING_LEN];
-	int i;
 	struct mdev_state *mds;
-	int ports = 0, used = 0;
-
-	for (i = 0; i < 2; i++) {
-		snprintf(name, MTTY_STRING_LEN, "%s-%d",
-			 dev_driver_string(dev), i + 1);
-		if (!strcmp(kobj->name, name)) {
-			ports = i + 1;
-			break;
-		}
-	}
-
-	if (!ports)
-		return -EINVAL;
+	unsigned int ports = mtype_get_type_group_id(kobj) + 1;
+	int used = 0;
 
 	list_for_each_entry(mds, &mdev_devices_list, next)
 		used += mds->nr_ports;
-- 
2.31.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 13/18] vfio/mdpy: Use mdev_get_type_group_id()
  2021-03-23 17:55 [PATCH 00/18] Make vfio_mdev type safe Jason Gunthorpe
                   ` (11 preceding siblings ...)
  2021-03-23 17:55 ` [PATCH 12/18] vfio/mtty: Use mdev_get_type_group_id() Jason Gunthorpe
@ 2021-03-23 17:55 ` Jason Gunthorpe
  2021-03-23 19:25   ` Christoph Hellwig
  2021-03-23 17:55 ` [PATCH 14/18] vfio/mbochs: " Jason Gunthorpe
                   ` (2 subsequent siblings)
  15 siblings, 1 reply; 69+ messages in thread
From: Jason Gunthorpe @ 2021-03-23 17:55 UTC (permalink / raw)
  To: kvm, Kirti Wankhede
  Cc: Raj, Ashok, Dan Williams, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

The mdpy_types array is parallel to the supported_type_groups array, so
the type_group_id indexes both. Instead of doing string searching just
directly index with type_group_id in all places.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 samples/vfio-mdev/mdpy.c | 22 ++++++----------------
 1 file changed, 6 insertions(+), 16 deletions(-)

diff --git a/samples/vfio-mdev/mdpy.c b/samples/vfio-mdev/mdpy.c
index d4ec2b52ca49a1..08c15f9f06a880 100644
--- a/samples/vfio-mdev/mdpy.c
+++ b/samples/vfio-mdev/mdpy.c
@@ -99,16 +99,6 @@ struct mdev_state {
 	void *memblk;
 };
 
-static const struct mdpy_type *mdpy_find_type(struct kobject *kobj)
-{
-	int i;
-
-	for (i = 0; i < ARRAY_SIZE(mdpy_types); i++)
-		if (strcmp(mdpy_types[i].name, kobj->name) == 0)
-			return mdpy_types + i;
-	return NULL;
-}
-
 static void mdpy_create_config_space(struct mdev_state *mdev_state)
 {
 	STORE_LE16((u16 *) &mdev_state->vconfig[PCI_VENDOR_ID],
@@ -228,7 +218,8 @@ static int mdpy_reset(struct mdev_device *mdev)
 
 static int mdpy_create(struct kobject *kobj, struct mdev_device *mdev)
 {
-	const struct mdpy_type *type = mdpy_find_type(kobj);
+	const struct mdpy_type *type =
+		&mdpy_types[mdev_get_type_group_id(mdev)];
 	struct device *dev = mdev_dev(mdev);
 	struct mdev_state *mdev_state;
 	u32 fbsize;
@@ -246,8 +237,6 @@ static int mdpy_create(struct kobject *kobj, struct mdev_device *mdev)
 		return -ENOMEM;
 	}
 
-	if (!type)
-		type = &mdpy_types[0];
 	fbsize = roundup_pow_of_two(type->width * type->height * type->bytepp);
 
 	mdev_state->memblk = vmalloc_user(fbsize);
@@ -256,8 +245,8 @@ static int mdpy_create(struct kobject *kobj, struct mdev_device *mdev)
 		kfree(mdev_state);
 		return -ENOMEM;
 	}
-	dev_info(dev, "%s: %s (%dx%d)\n",
-		 __func__, kobj->name, type->width, type->height);
+	dev_info(dev, "%s: %s (%dx%d)\n", __func__, type->name, type->width,
+		 type->height);
 
 	mutex_init(&mdev_state->ops_lock);
 	mdev_state->mdev = mdev;
@@ -673,7 +662,8 @@ static MDEV_TYPE_ATTR_RO(name);
 static ssize_t
 description_show(struct kobject *kobj, struct device *dev, char *buf)
 {
-	const struct mdpy_type *type = mdpy_find_type(kobj);
+	const struct mdpy_type *type =
+		&mdpy_types[mtype_get_type_group_id(kobj)];
 
 	return sprintf(buf, "virtual display, %dx%d framebuffer\n",
 		       type ? type->width  : 0,
-- 
2.31.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 14/18] vfio/mbochs: Use mdev_get_type_group_id()
  2021-03-23 17:55 [PATCH 00/18] Make vfio_mdev type safe Jason Gunthorpe
                   ` (12 preceding siblings ...)
  2021-03-23 17:55 ` [PATCH 13/18] vfio/mdpy: " Jason Gunthorpe
@ 2021-03-23 17:55 ` Jason Gunthorpe
  2021-03-23 19:25   ` Christoph Hellwig
  2021-03-23 17:55 ` [PATCH 17/18] vfio/mdev: Remove kobj from mdev_parent_ops->create() Jason Gunthorpe
  2021-03-23 17:55 ` [PATCH 18/18] vfio/mdev: Correct the function signatures for the mdev_type_attributes Jason Gunthorpe
  15 siblings, 1 reply; 69+ messages in thread
From: Jason Gunthorpe @ 2021-03-23 17:55 UTC (permalink / raw)
  To: kvm, Kirti Wankhede
  Cc: Raj, Ashok, Dan Williams, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

The mbochs_types array is parallel to the supported_type_groups array, so
the type_group_id indexes both. Instead of doing string searching just
directly index with type_group_id in all places.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 samples/vfio-mdev/mbochs.c | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)

diff --git a/samples/vfio-mdev/mbochs.c b/samples/vfio-mdev/mbochs.c
index 365afbe2dea558..a1af30df10a2ee 100644
--- a/samples/vfio-mdev/mbochs.c
+++ b/samples/vfio-mdev/mbochs.c
@@ -205,16 +205,6 @@ static struct page *__mbochs_get_page(struct mdev_state *mdev_state,
 static struct page *mbochs_get_page(struct mdev_state *mdev_state,
 				    pgoff_t pgoff);
 
-static const struct mbochs_type *mbochs_find_type(struct kobject *kobj)
-{
-	int i;
-
-	for (i = 0; i < ARRAY_SIZE(mbochs_types); i++)
-		if (strcmp(mbochs_types[i].name, kobj->name) == 0)
-			return mbochs_types + i;
-	return NULL;
-}
-
 static void mbochs_create_config_space(struct mdev_state *mdev_state)
 {
 	STORE_LE16((u16 *) &mdev_state->vconfig[PCI_VENDOR_ID],
@@ -518,7 +508,8 @@ static int mbochs_reset(struct mdev_device *mdev)
 
 static int mbochs_create(struct kobject *kobj, struct mdev_device *mdev)
 {
-	const struct mbochs_type *type = mbochs_find_type(kobj);
+	const struct mbochs_type *type =
+		&mbochs_types[mdev_get_type_group_id(mdev)];
 	struct device *dev = mdev_dev(mdev);
 	struct mdev_state *mdev_state;
 
@@ -544,7 +535,7 @@ static int mbochs_create(struct kobject *kobj, struct mdev_device *mdev)
 		goto err_mem;
 
 	dev_info(dev, "%s: %s, %d MB, %ld pages\n", __func__,
-		 kobj->name, type->mbytes, mdev_state->pagecount);
+		 type->name, type->mbytes, mdev_state->pagecount);
 
 	mutex_init(&mdev_state->ops_lock);
 	mdev_state->mdev = mdev;
@@ -1349,7 +1340,8 @@ static MDEV_TYPE_ATTR_RO(name);
 static ssize_t
 description_show(struct kobject *kobj, struct device *dev, char *buf)
 {
-	const struct mbochs_type *type = mbochs_find_type(kobj);
+	const struct mbochs_type *type =
+		&mbochs_types[mtype_get_type_group_id(kobj)];
 
 	return sprintf(buf, "virtual display, %d MB video memory\n",
 		       type ? type->mbytes  : 0);
@@ -1359,7 +1351,8 @@ static MDEV_TYPE_ATTR_RO(description);
 static ssize_t
 available_instances_show(struct kobject *kobj, struct device *dev, char *buf)
 {
-	const struct mbochs_type *type = mbochs_find_type(kobj);
+	const struct mbochs_type *type =
+		&mbochs_types[mtype_get_type_group_id(kobj)];
 	int count = (max_mbytes - mbochs_used_mbytes) / type->mbytes;
 
 	return sprintf(buf, "%d\n", count);
-- 
2.31.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 17/18] vfio/mdev: Remove kobj from mdev_parent_ops->create()
  2021-03-23 17:55 [PATCH 00/18] Make vfio_mdev type safe Jason Gunthorpe
                   ` (13 preceding siblings ...)
  2021-03-23 17:55 ` [PATCH 14/18] vfio/mbochs: " Jason Gunthorpe
@ 2021-03-23 17:55 ` Jason Gunthorpe
  2021-03-23 19:29   ` Christoph Hellwig
                     ` (2 more replies)
  2021-03-23 17:55 ` [PATCH 18/18] vfio/mdev: Correct the function signatures for the mdev_type_attributes Jason Gunthorpe
  15 siblings, 3 replies; 69+ messages in thread
From: Jason Gunthorpe @ 2021-03-23 17:55 UTC (permalink / raw)
  To: David Airlie, Tony Krowiak, Alex Williamson,
	Christian Borntraeger, Cornelia Huck, Daniel Vetter, dri-devel,
	Eric Farman, Harald Freudenberger, Vasily Gorbik, Heiko Carstens,
	intel-gfx, intel-gvt-dev, Jani Nikula, Joonas Lahtinen, kvm,
	Kirti Wankhede, linux-s390, Peter Oberparleiter, Halil Pasic,
	Pierre Morel, Rodrigo Vivi, Vineeth Vijayan, Zhenyu Wang,
	Zhi Wang
  Cc: Raj, Ashok, Dan Williams, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

The kobj here is a type-erased version of mdev_type, which is already
stored in the struct mdev_device being passed in. It was only ever used to
compute the type_group_id, which is now extracted directly from the mdev.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 drivers/gpu/drm/i915/gvt/kvmgt.c  | 2 +-
 drivers/s390/cio/vfio_ccw_ops.c   | 2 +-
 drivers/s390/crypto/vfio_ap_ops.c | 2 +-
 drivers/vfio/mdev/mdev_core.c     | 2 +-
 include/linux/mdev.h              | 3 +--
 samples/vfio-mdev/mbochs.c        | 2 +-
 samples/vfio-mdev/mdpy.c          | 2 +-
 samples/vfio-mdev/mtty.c          | 2 +-
 8 files changed, 8 insertions(+), 9 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
index 16e1e4a38aa1f6..6bf176e8426e63 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -689,7 +689,7 @@ static void kvmgt_put_vfio_device(void *vgpu)
 	vfio_device_put(vdev->vfio_device);
 }
 
-static int intel_vgpu_create(struct kobject *kobj, struct mdev_device *mdev)
+static int intel_vgpu_create(struct mdev_device *mdev)
 {
 	struct intel_vgpu *vgpu = NULL;
 	struct intel_vgpu_type *type;
diff --git a/drivers/s390/cio/vfio_ccw_ops.c b/drivers/s390/cio/vfio_ccw_ops.c
index 68106be4ba7a19..06a82ec136080c 100644
--- a/drivers/s390/cio/vfio_ccw_ops.c
+++ b/drivers/s390/cio/vfio_ccw_ops.c
@@ -110,7 +110,7 @@ static struct attribute_group *mdev_type_groups[] = {
 	NULL,
 };
 
-static int vfio_ccw_mdev_create(struct kobject *kobj, struct mdev_device *mdev)
+static int vfio_ccw_mdev_create(struct mdev_device *mdev)
 {
 	struct vfio_ccw_private *private =
 		dev_get_drvdata(mdev_parent_dev(mdev));
diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c
index 41fc2e4135fe18..6d75ed07bcd49d 100644
--- a/drivers/s390/crypto/vfio_ap_ops.c
+++ b/drivers/s390/crypto/vfio_ap_ops.c
@@ -322,7 +322,7 @@ static void vfio_ap_matrix_init(struct ap_config_info *info,
 	matrix->adm_max = info->apxa ? info->Nd : 15;
 }
 
-static int vfio_ap_mdev_create(struct kobject *kobj, struct mdev_device *mdev)
+static int vfio_ap_mdev_create(struct mdev_device *mdev)
 {
 	struct ap_matrix_mdev *matrix_mdev;
 
diff --git a/drivers/vfio/mdev/mdev_core.c b/drivers/vfio/mdev/mdev_core.c
index 3ba5e9464b4d20..71455812cb84cf 100644
--- a/drivers/vfio/mdev/mdev_core.c
+++ b/drivers/vfio/mdev/mdev_core.c
@@ -286,7 +286,7 @@ int mdev_device_create(struct mdev_type *type, const guid_t *uuid)
 		goto mdev_fail;
 	}
 
-	ret = parent->ops->create(&type->kobj, mdev);
+	ret = parent->ops->create(mdev);
 	if (ret)
 		goto ops_create_fail;
 
diff --git a/include/linux/mdev.h b/include/linux/mdev.h
index 41e91936522394..c3a800051d6146 100644
--- a/include/linux/mdev.h
+++ b/include/linux/mdev.h
@@ -61,7 +61,6 @@ unsigned int mtype_get_type_group_id(struct kobject *mtype_kobj);
  * @create:		Called to allocate basic resources in parent device's
  *			driver for a particular mediated device. It is
  *			mandatory to provide create ops.
- *			@kobj: kobject of type for which 'create' is called.
  *			@mdev: mdev_device structure on of mediated device
  *			      that is being created
  *			Returns integer: success (0) or error (< 0)
@@ -107,7 +106,7 @@ struct mdev_parent_ops {
 	const struct attribute_group **mdev_attr_groups;
 	struct attribute_group **supported_type_groups;
 
-	int     (*create)(struct kobject *kobj, struct mdev_device *mdev);
+	int     (*create)(struct mdev_device *mdev);
 	int     (*remove)(struct mdev_device *mdev);
 	int     (*open)(struct mdev_device *mdev);
 	void    (*release)(struct mdev_device *mdev);
diff --git a/samples/vfio-mdev/mbochs.c b/samples/vfio-mdev/mbochs.c
index a1af30df10a2ee..ac4d0dc2490705 100644
--- a/samples/vfio-mdev/mbochs.c
+++ b/samples/vfio-mdev/mbochs.c
@@ -506,7 +506,7 @@ static int mbochs_reset(struct mdev_device *mdev)
 	return 0;
 }
 
-static int mbochs_create(struct kobject *kobj, struct mdev_device *mdev)
+static int mbochs_create(struct mdev_device *mdev)
 {
 	const struct mbochs_type *type =
 		&mbochs_types[mdev_get_type_group_id(mdev)];
diff --git a/samples/vfio-mdev/mdpy.c b/samples/vfio-mdev/mdpy.c
index 08c15f9f06a880..da88fd7dd42329 100644
--- a/samples/vfio-mdev/mdpy.c
+++ b/samples/vfio-mdev/mdpy.c
@@ -216,7 +216,7 @@ static int mdpy_reset(struct mdev_device *mdev)
 	return 0;
 }
 
-static int mdpy_create(struct kobject *kobj, struct mdev_device *mdev)
+static int mdpy_create(struct mdev_device *mdev)
 {
 	const struct mdpy_type *type =
 		&mdpy_types[mdev_get_type_group_id(mdev)];
diff --git a/samples/vfio-mdev/mtty.c b/samples/vfio-mdev/mtty.c
index 191a587a8d5ab1..f2e36c06ac6aa2 100644
--- a/samples/vfio-mdev/mtty.c
+++ b/samples/vfio-mdev/mtty.c
@@ -708,7 +708,7 @@ static ssize_t mdev_access(struct mdev_device *mdev, u8 *buf, size_t count,
 	return ret;
 }
 
-static int mtty_create(struct kobject *kobj, struct mdev_device *mdev)
+static int mtty_create(struct mdev_device *mdev)
 {
 	struct mdev_state *mdev_state;
 	int nr_ports = mdev_get_type_group_id(mdev) + 1;
-- 
2.31.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* [PATCH 18/18] vfio/mdev: Correct the function signatures for the mdev_type_attributes
  2021-03-23 17:55 [PATCH 00/18] Make vfio_mdev type safe Jason Gunthorpe
                   ` (14 preceding siblings ...)
  2021-03-23 17:55 ` [PATCH 17/18] vfio/mdev: Remove kobj from mdev_parent_ops->create() Jason Gunthorpe
@ 2021-03-23 17:55 ` Jason Gunthorpe
  2021-03-23 19:31   ` Christoph Hellwig
                     ` (2 more replies)
  15 siblings, 3 replies; 69+ messages in thread
From: Jason Gunthorpe @ 2021-03-23 17:55 UTC (permalink / raw)
  To: David Airlie, Tony Krowiak, Alex Williamson,
	Christian Borntraeger, Cornelia Huck, Daniel Vetter, dri-devel,
	Eric Farman, Harald Freudenberger, Vasily Gorbik, Heiko Carstens,
	intel-gfx, intel-gvt-dev, Jani Nikula, Joonas Lahtinen, kvm,
	Kirti Wankhede, linux-s390, Peter Oberparleiter, Halil Pasic,
	Pierre Morel, Rodrigo Vivi, Vineeth Vijayan, Zhenyu Wang,
	Zhi Wang
  Cc: Raj, Ashok, Dan Williams, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

The driver core standard is to pass in the properly typed object, the
properly typed attribute and the buffer data. It stems from the root
kobject method:

  ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,..)

Each subclass of kobject should provide their own function with the same
signature but more specific types, eg struct device uses:

  ssize_t (*show)(struct device *dev, struct device_attribute *attr,..)

In this case the existing signature is:

  ssize_t (*show)(struct kobject *kobj, struct device *dev,..)

Where kobj is a 'struct mdev_type *' and dev is 'mdev_type->parent->dev'.

Change the mdev_type related sysfs attribute functions to:

  ssize_t (*show)(struct mdev_type *mtype, struct mdev_type_attribute *attr,..)

In order to restore type safety and match the driver core standard

There are no current users of 'attr', but if it is ever needed it would be
hard to add in retroactively, so do it now.

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 drivers/gpu/drm/i915/gvt/gvt.c    | 21 +++++++++++----------
 drivers/s390/cio/vfio_ccw_ops.c   | 15 +++++++++------
 drivers/s390/crypto/vfio_ap_ops.c | 12 +++++++-----
 drivers/vfio/mdev/mdev_core.c     | 14 ++++++++++++--
 drivers/vfio/mdev/mdev_sysfs.c    | 11 ++++++-----
 include/linux/mdev.h              | 11 +++++++----
 samples/vfio-mdev/mbochs.c        | 26 +++++++++++++++-----------
 samples/vfio-mdev/mdpy.c          | 24 ++++++++++++++----------
 samples/vfio-mdev/mtty.c          | 18 +++++++++---------
 9 files changed, 90 insertions(+), 62 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/gvt.c b/drivers/gpu/drm/i915/gvt/gvt.c
index 4b47a18e9dfa0f..3703814a669b46 100644
--- a/drivers/gpu/drm/i915/gvt/gvt.c
+++ b/drivers/gpu/drm/i915/gvt/gvt.c
@@ -54,14 +54,15 @@ intel_gvt_find_vgpu_type(struct intel_gvt *gvt, unsigned int type_group_id)
 	return &gvt->types[type_group_id];
 }
 
-static ssize_t available_instances_show(struct kobject *kobj,
-					struct device *dev, char *buf)
+static ssize_t available_instances_show(struct mdev_type *mtype,
+					struct mdev_type_attribute *attr,
+					char *buf)
 {
 	struct intel_vgpu_type *type;
 	unsigned int num = 0;
-	void *gvt = kdev_to_i915(dev)->gvt;
+	void *gvt = kdev_to_i915(mtype_get_parent_dev(mtype))->gvt;
 
-	type = intel_gvt_find_vgpu_type(gvt, mtype_get_type_group_id(kobj));
+	type = intel_gvt_find_vgpu_type(gvt, mtype_get_type_group_id(mtype));
 	if (!type)
 		num = 0;
 	else
@@ -70,19 +71,19 @@ static ssize_t available_instances_show(struct kobject *kobj,
 	return sprintf(buf, "%u\n", num);
 }
 
-static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
-		char *buf)
+static ssize_t device_api_show(struct mdev_type *mtype,
+			       struct mdev_type_attribute *attr, char *buf)
 {
 	return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
 }
 
-static ssize_t description_show(struct kobject *kobj, struct device *dev,
-		char *buf)
+static ssize_t description_show(struct mdev_type *mtype,
+				struct mdev_type_attribute *attr, char *buf)
 {
 	struct intel_vgpu_type *type;
-	void *gvt = kdev_to_i915(dev)->gvt;
+	void *gvt = kdev_to_i915(mtype_get_parent_dev(mtype))->gvt;
 
-	type = intel_gvt_find_vgpu_type(gvt, mtype_get_type_group_id(kobj));
+	type = intel_gvt_find_vgpu_type(gvt, mtype_get_type_group_id(mtype));
 	if (!type)
 		return 0;
 
diff --git a/drivers/s390/cio/vfio_ccw_ops.c b/drivers/s390/cio/vfio_ccw_ops.c
index 06a82ec136080c..74fe21eceb66cc 100644
--- a/drivers/s390/cio/vfio_ccw_ops.c
+++ b/drivers/s390/cio/vfio_ccw_ops.c
@@ -71,23 +71,26 @@ static int vfio_ccw_mdev_notifier(struct notifier_block *nb,
 	return NOTIFY_DONE;
 }
 
-static ssize_t name_show(struct kobject *kobj, struct device *dev, char *buf)
+static ssize_t name_show(struct mdev_type *mtype,
+			 struct mdev_type_attribute *attr, char *buf)
 {
 	return sprintf(buf, "I/O subchannel (Non-QDIO)\n");
 }
 static MDEV_TYPE_ATTR_RO(name);
 
-static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
-			       char *buf)
+static ssize_t device_api_show(struct mdev_type *mtype,
+			       struct mdev_type_attribute *attr, char *buf)
 {
 	return sprintf(buf, "%s\n", VFIO_DEVICE_API_CCW_STRING);
 }
 static MDEV_TYPE_ATTR_RO(device_api);
 
-static ssize_t available_instances_show(struct kobject *kobj,
-					struct device *dev, char *buf)
+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(dev);
+	struct vfio_ccw_private *private =
+		dev_get_drvdata(mtype_get_parent_dev(mtype));
 
 	return sprintf(buf, "%d\n", atomic_read(&private->avail));
 }
diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c
index 6d75ed07bcd49d..cdc5edb0074690 100644
--- a/drivers/s390/crypto/vfio_ap_ops.c
+++ b/drivers/s390/crypto/vfio_ap_ops.c
@@ -366,15 +366,17 @@ static int vfio_ap_mdev_remove(struct mdev_device *mdev)
 	return 0;
 }
 
-static ssize_t name_show(struct kobject *kobj, struct device *dev, char *buf)
+static ssize_t name_show(struct mdev_type *mtype,
+			 struct mdev_type_attribute *attr, char *buf)
 {
 	return sprintf(buf, "%s\n", VFIO_AP_MDEV_NAME_HWVIRT);
 }
 
 static MDEV_TYPE_ATTR_RO(name);
 
-static ssize_t available_instances_show(struct kobject *kobj,
-					struct device *dev, char *buf)
+static ssize_t available_instances_show(struct mdev_type *mtype,
+					struct mdev_type_attribute *attr,
+					char *buf)
 {
 	return sprintf(buf, "%d\n",
 		       atomic_read(&matrix_dev->available_instances));
@@ -382,8 +384,8 @@ static ssize_t available_instances_show(struct kobject *kobj,
 
 static MDEV_TYPE_ATTR_RO(available_instances);
 
-static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
-			       char *buf)
+static ssize_t device_api_show(struct mdev_type *mtype,
+			       struct mdev_type_attribute *attr, char *buf)
 {
 	return sprintf(buf, "%s\n", VFIO_DEVICE_API_AP_STRING);
 }
diff --git a/drivers/vfio/mdev/mdev_core.c b/drivers/vfio/mdev/mdev_core.c
index 71455812cb84cf..9ef1d5bed8069f 100644
--- a/drivers/vfio/mdev/mdev_core.c
+++ b/drivers/vfio/mdev/mdev_core.c
@@ -47,12 +47,22 @@ EXPORT_SYMBOL(mdev_get_type_group_id);
  * Used in mdev_type_attribute sysfs functions to return the index in the
  * supported_type_groups that the sysfs is called from.
  */
-unsigned int mtype_get_type_group_id(struct kobject *mtype_kobj)
+unsigned int mtype_get_type_group_id(struct mdev_type *mtype)
 {
-	return container_of(mtype_kobj, struct mdev_type, kobj)->type_group_id;
+	return mtype->type_group_id;
 }
 EXPORT_SYMBOL(mtype_get_type_group_id);
 
+/*
+ * Used in mdev_type_attribute sysfs functions to return the parent struct
+ * device
+ */
+struct device *mtype_get_parent_dev(struct mdev_type *mtype)
+{
+	return mtype->parent->dev;
+}
+EXPORT_SYMBOL(mtype_get_parent_dev);
+
 /* Should be called holding parent_list_lock */
 static struct mdev_parent *__find_parent_device(struct device *dev)
 {
diff --git a/drivers/vfio/mdev/mdev_sysfs.c b/drivers/vfio/mdev/mdev_sysfs.c
index 91ecccdc2f2ec6..9b0f1a8757a0df 100644
--- a/drivers/vfio/mdev/mdev_sysfs.c
+++ b/drivers/vfio/mdev/mdev_sysfs.c
@@ -26,7 +26,7 @@ static ssize_t mdev_type_attr_show(struct kobject *kobj,
 	ssize_t ret = -EIO;
 
 	if (attr->show)
-		ret = attr->show(kobj, type->parent->dev, buf);
+		ret = attr->show(type, attr, buf);
 	return ret;
 }
 
@@ -39,7 +39,7 @@ static ssize_t mdev_type_attr_store(struct kobject *kobj,
 	ssize_t ret = -EIO;
 
 	if (attr->store)
-		ret = attr->store(&type->kobj, type->parent->dev, buf, count);
+		ret = attr->store(type, attr, buf, count);
 	return ret;
 }
 
@@ -48,8 +48,9 @@ static const struct sysfs_ops mdev_type_sysfs_ops = {
 	.store = mdev_type_attr_store,
 };
 
-static ssize_t create_store(struct kobject *kobj, struct device *dev,
-			    const char *buf, size_t count)
+static ssize_t create_store(struct mdev_type *mtype,
+			    struct mdev_type_attribute *attr, const char *buf,
+			    size_t count)
 {
 	char *str;
 	guid_t uuid;
@@ -67,7 +68,7 @@ static ssize_t create_store(struct kobject *kobj, struct device *dev,
 	if (ret)
 		return ret;
 
-	ret = mdev_device_create(to_mdev_type(kobj), &uuid);
+	ret = mdev_device_create(mtype, &uuid);
 	if (ret)
 		return ret;
 
diff --git a/include/linux/mdev.h b/include/linux/mdev.h
index c3a800051d6146..1fb34ea394ad46 100644
--- a/include/linux/mdev.h
+++ b/include/linux/mdev.h
@@ -47,7 +47,8 @@ static inline struct device *mdev_get_iommu_device(struct mdev_device *mdev)
 }
 
 unsigned int mdev_get_type_group_id(struct mdev_device *mdev);
-unsigned int mtype_get_type_group_id(struct kobject *mtype_kobj);
+unsigned int mtype_get_type_group_id(struct mdev_type *mtype);
+struct device *mtype_get_parent_dev(struct mdev_type *mtype);
 
 /**
  * struct mdev_parent_ops - Structure to be registered for each parent device to
@@ -123,9 +124,11 @@ struct mdev_parent_ops {
 /* interface for exporting mdev supported type attributes */
 struct mdev_type_attribute {
 	struct attribute attr;
-	ssize_t (*show)(struct kobject *kobj, struct device *dev, char *buf);
-	ssize_t (*store)(struct kobject *kobj, struct device *dev,
-			 const char *buf, size_t count);
+	ssize_t (*show)(struct mdev_type *mtype,
+			struct mdev_type_attribute *attr, char *buf);
+	ssize_t (*store)(struct mdev_type *mtype,
+			 struct mdev_type_attribute *attr, const char *buf,
+			 size_t count);
 };
 
 #define MDEV_TYPE_ATTR(_name, _mode, _show, _store)		\
diff --git a/samples/vfio-mdev/mbochs.c b/samples/vfio-mdev/mbochs.c
index ac4d0dc2490705..861c76914e7639 100644
--- a/samples/vfio-mdev/mbochs.c
+++ b/samples/vfio-mdev/mbochs.c
@@ -1330,37 +1330,41 @@ static const struct attribute_group *mdev_dev_groups[] = {
 	NULL,
 };
 
-static ssize_t
-name_show(struct kobject *kobj, struct device *dev, char *buf)
+static ssize_t name_show(struct mdev_type *mtype,
+			 struct mdev_type_attribute *attr, char *buf)
 {
-	return sprintf(buf, "%s\n", kobj->name);
+	const struct mbochs_type *type =
+		&mbochs_types[mtype_get_type_group_id(mtype)];
+
+	return sprintf(buf, "%s\n", type->name);
 }
 static MDEV_TYPE_ATTR_RO(name);
 
-static ssize_t
-description_show(struct kobject *kobj, struct device *dev, char *buf)
+static ssize_t description_show(struct mdev_type *mtype,
+				struct mdev_type_attribute *attr, char *buf)
 {
 	const struct mbochs_type *type =
-		&mbochs_types[mtype_get_type_group_id(kobj)];
+		&mbochs_types[mtype_get_type_group_id(mtype)];
 
 	return sprintf(buf, "virtual display, %d MB video memory\n",
 		       type ? type->mbytes  : 0);
 }
 static MDEV_TYPE_ATTR_RO(description);
 
-static ssize_t
-available_instances_show(struct kobject *kobj, struct device *dev, char *buf)
+static ssize_t available_instances_show(struct mdev_type *mtype,
+					struct mdev_type_attribute *attr,
+					char *buf)
 {
 	const struct mbochs_type *type =
-		&mbochs_types[mtype_get_type_group_id(kobj)];
+		&mbochs_types[mtype_get_type_group_id(mtype)];
 	int count = (max_mbytes - mbochs_used_mbytes) / type->mbytes;
 
 	return sprintf(buf, "%d\n", count);
 }
 static MDEV_TYPE_ATTR_RO(available_instances);
 
-static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
-			       char *buf)
+static ssize_t device_api_show(struct mdev_type *mtype,
+			       struct mdev_type_attribute *attr, char *buf)
 {
 	return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
 }
diff --git a/samples/vfio-mdev/mdpy.c b/samples/vfio-mdev/mdpy.c
index da88fd7dd42329..885b88ea20e234 100644
--- a/samples/vfio-mdev/mdpy.c
+++ b/samples/vfio-mdev/mdpy.c
@@ -652,18 +652,21 @@ static const struct attribute_group *mdev_dev_groups[] = {
 	NULL,
 };
 
-static ssize_t
-name_show(struct kobject *kobj, struct device *dev, char *buf)
+static ssize_t name_show(struct mdev_type *mtype,
+			 struct mdev_type_attribute *attr, char *buf)
 {
-	return sprintf(buf, "%s\n", kobj->name);
+	const struct mdpy_type *type =
+		&mdpy_types[mtype_get_type_group_id(mtype)];
+
+	return sprintf(buf, "%s\n", type->name);
 }
 static MDEV_TYPE_ATTR_RO(name);
 
-static ssize_t
-description_show(struct kobject *kobj, struct device *dev, char *buf)
+static ssize_t description_show(struct mdev_type *mtype,
+				struct mdev_type_attribute *attr, char *buf)
 {
 	const struct mdpy_type *type =
-		&mdpy_types[mtype_get_type_group_id(kobj)];
+		&mdpy_types[mtype_get_type_group_id(mtype)];
 
 	return sprintf(buf, "virtual display, %dx%d framebuffer\n",
 		       type ? type->width  : 0,
@@ -671,15 +674,16 @@ description_show(struct kobject *kobj, struct device *dev, char *buf)
 }
 static MDEV_TYPE_ATTR_RO(description);
 
-static ssize_t
-available_instances_show(struct kobject *kobj, struct device *dev, char *buf)
+static ssize_t available_instances_show(struct mdev_type *mtype,
+					struct mdev_type_attribute *attr,
+					char *buf)
 {
 	return sprintf(buf, "%d\n", max_devices - mdpy_count);
 }
 static MDEV_TYPE_ATTR_RO(available_instances);
 
-static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
-			       char *buf)
+static ssize_t device_api_show(struct mdev_type *mtype,
+			       struct mdev_type_attribute *attr, char *buf)
 {
 	return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
 }
diff --git a/samples/vfio-mdev/mtty.c b/samples/vfio-mdev/mtty.c
index f2e36c06ac6aa2..b9b24be4abdab7 100644
--- a/samples/vfio-mdev/mtty.c
+++ b/samples/vfio-mdev/mtty.c
@@ -1292,23 +1292,24 @@ static const struct attribute_group *mdev_dev_groups[] = {
 	NULL,
 };
 
-static ssize_t
-name_show(struct kobject *kobj, struct device *dev, char *buf)
+static ssize_t name_show(struct mdev_type *mtype,
+			 struct mdev_type_attribute *attr, char *buf)
 {
 	static const char *name_str[2] = { "Single port serial",
 					   "Dual port serial" };
 
 	return sysfs_emit(buf, "%s\n",
-			  name_str[mtype_get_type_group_id(kobj)]);
+			  name_str[mtype_get_type_group_id(mtype)]);
 }
 
 static MDEV_TYPE_ATTR_RO(name);
 
-static ssize_t
-available_instances_show(struct kobject *kobj, struct device *dev, char *buf)
+static ssize_t available_instances_show(struct mdev_type *mtype,
+					struct mdev_type_attribute *attr,
+					char *buf)
 {
 	struct mdev_state *mds;
-	unsigned int ports = mtype_get_type_group_id(kobj) + 1;
+	unsigned int ports = mtype_get_type_group_id(mtype) + 1;
 	int used = 0;
 
 	list_for_each_entry(mds, &mdev_devices_list, next)
@@ -1319,9 +1320,8 @@ available_instances_show(struct kobject *kobj, struct device *dev, char *buf)
 
 static MDEV_TYPE_ATTR_RO(available_instances);
 
-
-static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
-			       char *buf)
+static ssize_t device_api_show(struct mdev_type *mtype,
+			       struct mdev_type_attribute *attr, char *buf)
 {
 	return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
 }
-- 
2.31.0


^ permalink raw reply related	[flat|nested] 69+ messages in thread

* Re: [PATCH 03/18] vfio/mdev: Simplify driver registration
  2021-03-23 17:55 ` [PATCH 03/18] vfio/mdev: Simplify driver registration Jason Gunthorpe
@ 2021-03-23 19:14   ` Christoph Hellwig
  2021-03-26 12:10     ` Jason Gunthorpe
  2021-03-26  2:17   ` Tian, Kevin
  2021-03-30 15:28   ` Cornelia Huck
  2 siblings, 1 reply; 69+ messages in thread
From: Christoph Hellwig @ 2021-03-23 19:14 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Alex Williamson, Cornelia Huck, Jonathan Corbet, kvm,
	Kirti Wankhede, linux-doc, Raj, Ashok, Dan Williams,
	Christoph Hellwig, Leon Romanovsky, Max Gurtovoy, Tarun Gupta

>  static struct mdev_driver vfio_mdev_driver = {
> +	.driver = {
> +		.name = "vfio_mdev",
> +		.owner = THIS_MODULE,
> +		.mod_name = KBUILD_MODNAME,
> +	},

What is the mod_name initialization for?  I've not really seen
that in anywere else, and the only user seems to be
module_add_driver for a rather odd case we shouldn't hit here.

The rest looks good to me:

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 04/18] vfio/mdev: Use struct mdev_type in struct mdev_device
  2021-03-23 17:55 ` [PATCH 04/18] vfio/mdev: Use struct mdev_type in struct mdev_device Jason Gunthorpe
@ 2021-03-23 19:15   ` Christoph Hellwig
  2021-03-26  2:29   ` Tian, Kevin
  2021-03-30 15:31   ` Cornelia Huck
  2 siblings, 0 replies; 69+ messages in thread
From: Christoph Hellwig @ 2021-03-23 19:15 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Alex Williamson, Cornelia Huck, kvm, Kirti Wankhede, Raj, Ashok,
	Dan Williams, Christoph Hellwig, Leon Romanovsky, Max Gurtovoy,
	Tarun Gupta

Looks good,

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 05/18] vfio/mdev: Do not allow a mdev_type to have a NULL parent pointer
  2021-03-23 17:55 ` [PATCH 05/18] vfio/mdev: Do not allow a mdev_type to have a NULL parent pointer Jason Gunthorpe
@ 2021-03-23 19:15   ` Christoph Hellwig
  2021-04-06 16:08     ` Jason Gunthorpe
  2021-03-26  2:38   ` Tian, Kevin
                     ` (2 subsequent siblings)
  3 siblings, 1 reply; 69+ messages in thread
From: Christoph Hellwig @ 2021-03-23 19:15 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Cornelia Huck, kvm, Alex Williamson, Raj, Ashok, Dong Jia Shi,
	Neo Jia, Dan Williams, Christoph Hellwig, Jike Song,
	Kirti Wankhede, Leon Romanovsky, Max Gurtovoy, Tarun Gupta

Looks good,

Reviewed-by: Christoph Hellwig <hch@lst.de>

Shouldn't this be at the beginning of the series, though?

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 06/18] vfio/mdev: Expose mdev_get/put_parent to mdev_private.h
  2021-03-23 17:55 ` [PATCH 06/18] vfio/mdev: Expose mdev_get/put_parent to mdev_private.h Jason Gunthorpe
@ 2021-03-23 19:16   ` Christoph Hellwig
  2021-03-26  2:47   ` Tian, Kevin
  2021-03-30 15:37   ` Cornelia Huck
  2 siblings, 0 replies; 69+ messages in thread
From: Christoph Hellwig @ 2021-03-23 19:16 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Alex Williamson, Cornelia Huck, kvm, Kirti Wankhede, Raj, Ashok,
	Dan Williams, Christoph Hellwig, Leon Romanovsky, Max Gurtovoy,
	Tarun Gupta

On Tue, Mar 23, 2021 at 02:55:23PM -0300, Jason Gunthorpe wrote:
> The next patch will use these in mdev_sysfs.c
> 
> While here remove the now dead code checks for NULL, a mdev_type can never
> have a NULL parent.

I'd normally just move that into the patch that uses it, but the
mechanical change looks fine:

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 07/18] vfio/mdev: Add missing reference counting to mdev_type
  2021-03-23 17:55 ` [PATCH 07/18] vfio/mdev: Add missing reference counting to mdev_type Jason Gunthorpe
@ 2021-03-23 19:17   ` Christoph Hellwig
  2021-03-26  2:52   ` Tian, Kevin
  2021-03-30 15:38   ` Cornelia Huck
  2 siblings, 0 replies; 69+ messages in thread
From: Christoph Hellwig @ 2021-03-23 19:17 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Alex Williamson, Cornelia Huck, kvm, Kirti Wankhede, Raj, Ashok,
	Dan Williams, Christoph Hellwig, Leon Romanovsky, Max Gurtovoy,
	Tarun Gupta

On Tue, Mar 23, 2021 at 02:55:24PM -0300, Jason Gunthorpe wrote:
> struct mdev_type holds a pointer to the kref'd object struct mdev_parent,
> but doesn't hold the kref. The lifetime of the parent becomes implicit
> because parent_remove_sysfs_files() is supposed to remove all the access
> before the parent can be freed, but this is very hard to reason about.
> 
> Make it obviously correct by adding the missing get.

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 08/18] vfio/mdev: Reorganize mdev_device_create()
  2021-03-23 17:55 ` [PATCH 08/18] vfio/mdev: Reorganize mdev_device_create() Jason Gunthorpe
@ 2021-03-23 19:20   ` Christoph Hellwig
  2021-03-26  3:33     ` Tian, Kevin
  2021-04-06 16:40     ` Jason Gunthorpe
  2021-03-26  3:31   ` Tian, Kevin
  2021-03-30 15:50   ` Cornelia Huck
  2 siblings, 2 replies; 69+ messages in thread
From: Christoph Hellwig @ 2021-03-23 19:20 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Alex Williamson, Cornelia Huck, kvm, Kirti Wankhede, Raj, Ashok,
	Dan Williams, Christoph Hellwig, Leon Romanovsky, Max Gurtovoy,
	Tarun Gupta

>  	up_read(&parent->unreg_sem);
> -	put_device(&mdev->dev);
>  mdev_fail:
>
>
>
> -	mdev_put_parent(parent);
> +	put_device(&mdev->dev);

That mdev_fail label is not very descriptive, what about free_device
instead?

Otherwise looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 09/18] vfio/mdev: Add missing error handling to dev_set_name()
  2021-03-23 17:55 ` [PATCH 09/18] vfio/mdev: Add missing error handling to dev_set_name() Jason Gunthorpe
@ 2021-03-23 19:21   ` Christoph Hellwig
  2021-03-26  3:35   ` Tian, Kevin
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 69+ messages in thread
From: Christoph Hellwig @ 2021-03-23 19:21 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Cornelia Huck, kvm, Alex Williamson, Raj, Ashok, Dong Jia Shi,
	Neo Jia, Dan Williams, Christoph Hellwig, Jike Song,
	Kirti Wankhede, Leon Romanovsky, Max Gurtovoy, Tarun Gupta

On Tue, Mar 23, 2021 at 02:55:26PM -0300, Jason Gunthorpe wrote:
> This can fail, and seems to be a popular target for syzkaller error
> injection. Check the error return and unwind with put_device().
> 
> Fixes: 7b96953bc640 ("vfio: Mediated device Core driver")
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>

Looks good,

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 10/18] vfio/mdev: Remove duplicate storage of parent in mdev_device
  2021-03-23 17:55 ` [PATCH 10/18] vfio/mdev: Remove duplicate storage of parent in mdev_device Jason Gunthorpe
@ 2021-03-23 19:22   ` Christoph Hellwig
  2021-03-26  3:53   ` Tian, Kevin
  2021-03-30 16:15   ` Cornelia Huck
  2 siblings, 0 replies; 69+ messages in thread
From: Christoph Hellwig @ 2021-03-23 19:22 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Alex Williamson, Cornelia Huck, kvm, Kirti Wankhede, Raj, Ashok,
	Dan Williams, Christoph Hellwig, Leon Romanovsky, Max Gurtovoy,
	Tarun Gupta

Looks good,

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 11/18] vfio/mdev: Add mdev/mtype_get_type_group_id()
  2021-03-23 17:55 ` [PATCH 11/18] vfio/mdev: Add mdev/mtype_get_type_group_id() Jason Gunthorpe
@ 2021-03-23 19:23   ` Christoph Hellwig
  2021-04-06 18:53     ` Jason Gunthorpe
  2021-03-26  5:52   ` Tian, Kevin
  2021-03-30 16:26   ` Cornelia Huck
  2 siblings, 1 reply; 69+ messages in thread
From: Christoph Hellwig @ 2021-03-23 19:23 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Alex Williamson, Cornelia Huck, kvm, Kirti Wankhede, Raj, Ashok,
	Dan Williams, Christoph Hellwig, Leon Romanovsky, Max Gurtovoy,
	Tarun Gupta

On Tue, Mar 23, 2021 at 02:55:28PM -0300, Jason Gunthorpe wrote:
> +/*
> + * Return the index in supported_type_groups that this mdev_device was created
> + * from.
> + */
> +unsigned int mdev_get_type_group_id(struct mdev_device *mdev)
> +{
> +	return mdev->type->type_group_id;
> +}
> +EXPORT_SYMBOL(mdev_get_type_group_id);
> +
> +/*
> + * Used in mdev_type_attribute sysfs functions to return the index in the
> + * supported_type_groups that the sysfs is called from.
> + */
> +unsigned int mtype_get_type_group_id(struct kobject *mtype_kobj)
> +{
> +	return container_of(mtype_kobj, struct mdev_type, kobj)->type_group_id;
> +}
> +EXPORT_SYMBOL(mtype_get_type_group_id);

The single field accessors are a little silly..

Otherwise this looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 12/18] vfio/mtty: Use mdev_get_type_group_id()
  2021-03-23 17:55 ` [PATCH 12/18] vfio/mtty: Use mdev_get_type_group_id() Jason Gunthorpe
@ 2021-03-23 19:24   ` Christoph Hellwig
  0 siblings, 0 replies; 69+ messages in thread
From: Christoph Hellwig @ 2021-03-23 19:24 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: kvm, Kirti Wankhede, Raj, Ashok, Dan Williams, Christoph Hellwig,
	Leon Romanovsky, Max Gurtovoy, Tarun Gupta

Looks good,

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 13/18] vfio/mdpy: Use mdev_get_type_group_id()
  2021-03-23 17:55 ` [PATCH 13/18] vfio/mdpy: " Jason Gunthorpe
@ 2021-03-23 19:25   ` Christoph Hellwig
  0 siblings, 0 replies; 69+ messages in thread
From: Christoph Hellwig @ 2021-03-23 19:25 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: kvm, Kirti Wankhede, Raj, Ashok, Dan Williams, Christoph Hellwig,
	Leon Romanovsky, Max Gurtovoy, Tarun Gupta

Looks good,

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 14/18] vfio/mbochs: Use mdev_get_type_group_id()
  2021-03-23 17:55 ` [PATCH 14/18] vfio/mbochs: " Jason Gunthorpe
@ 2021-03-23 19:25   ` Christoph Hellwig
  0 siblings, 0 replies; 69+ messages in thread
From: Christoph Hellwig @ 2021-03-23 19:25 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: kvm, Kirti Wankhede, Raj, Ashok, Dan Williams, Christoph Hellwig,
	Leon Romanovsky, Max Gurtovoy, Tarun Gupta

Looks good,

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 17/18] vfio/mdev: Remove kobj from mdev_parent_ops->create()
  2021-03-23 17:55 ` [PATCH 17/18] vfio/mdev: Remove kobj from mdev_parent_ops->create() Jason Gunthorpe
@ 2021-03-23 19:29   ` Christoph Hellwig
  2021-03-26  6:11   ` Tian, Kevin
  2021-03-30 16:29   ` Cornelia Huck
  2 siblings, 0 replies; 69+ messages in thread
From: Christoph Hellwig @ 2021-03-23 19:29 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: David Airlie, Tony Krowiak, Alex Williamson,
	Christian Borntraeger, Cornelia Huck, Daniel Vetter, dri-devel,
	Eric Farman, Harald Freudenberger, Vasily Gorbik, Heiko Carstens,
	intel-gfx, intel-gvt-dev, Jani Nikula, Joonas Lahtinen, kvm,
	Kirti Wankhede, linux-s390, Peter Oberparleiter, Halil Pasic,
	Pierre Morel, Rodrigo Vivi, Vineeth Vijayan, Zhenyu Wang,
	Zhi Wang, Raj, Ashok, Dan Williams, Christoph Hellwig,
	Leon Romanovsky, Max Gurtovoy, Tarun Gupta

Looks good,

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 18/18] vfio/mdev: Correct the function signatures for the mdev_type_attributes
  2021-03-23 17:55 ` [PATCH 18/18] vfio/mdev: Correct the function signatures for the mdev_type_attributes Jason Gunthorpe
@ 2021-03-23 19:31   ` Christoph Hellwig
  2021-04-06 18:34     ` Jason Gunthorpe
  2021-03-26  7:03   ` Tian, Kevin
  2021-03-30 16:35   ` Cornelia Huck
  2 siblings, 1 reply; 69+ messages in thread
From: Christoph Hellwig @ 2021-03-23 19:31 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: David Airlie, Tony Krowiak, Alex Williamson,
	Christian Borntraeger, Cornelia Huck, Daniel Vetter, dri-devel,
	Eric Farman, Harald Freudenberger, Vasily Gorbik, Heiko Carstens,
	intel-gfx, intel-gvt-dev, Jani Nikula, Joonas Lahtinen, kvm,
	Kirti Wankhede, linux-s390, Peter Oberparleiter, Halil Pasic,
	Pierre Morel, Rodrigo Vivi, Vineeth Vijayan, Zhenyu Wang,
	Zhi Wang, Raj, Ashok, Dan Williams, Christoph Hellwig,
	Leon Romanovsky, Max Gurtovoy, Tarun Gupta

On Tue, Mar 23, 2021 at 02:55:35PM -0300, Jason Gunthorpe wrote:
> The driver core standard is to pass in the properly typed object, the
> properly typed attribute and the buffer data. It stems from the root
> kobject method:
> 
>   ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,..)
> 
> Each subclass of kobject should provide their own function with the same
> signature but more specific types, eg struct device uses:
> 
>   ssize_t (*show)(struct device *dev, struct device_attribute *attr,..)
> 
> In this case the existing signature is:
> 
>   ssize_t (*show)(struct kobject *kobj, struct device *dev,..)
> 
> Where kobj is a 'struct mdev_type *' and dev is 'mdev_type->parent->dev'.
> 
> Change the mdev_type related sysfs attribute functions to:
> 
>   ssize_t (*show)(struct mdev_type *mtype, struct mdev_type_attribute *attr,..)
> 
> In order to restore type safety and match the driver core standard
> 
> There are no current users of 'attr', but if it is ever needed it would be
> hard to add in retroactively, so do it now.
> 
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> ---
>  drivers/gpu/drm/i915/gvt/gvt.c    | 21 +++++++++++----------
>  drivers/s390/cio/vfio_ccw_ops.c   | 15 +++++++++------
>  drivers/s390/crypto/vfio_ap_ops.c | 12 +++++++-----
>  drivers/vfio/mdev/mdev_core.c     | 14 ++++++++++++--
>  drivers/vfio/mdev/mdev_sysfs.c    | 11 ++++++-----
>  include/linux/mdev.h              | 11 +++++++----
>  samples/vfio-mdev/mbochs.c        | 26 +++++++++++++++-----------
>  samples/vfio-mdev/mdpy.c          | 24 ++++++++++++++----------
>  samples/vfio-mdev/mtty.c          | 18 +++++++++---------
>  9 files changed, 90 insertions(+), 62 deletions(-)
> 
> diff --git a/drivers/gpu/drm/i915/gvt/gvt.c b/drivers/gpu/drm/i915/gvt/gvt.c
> index 4b47a18e9dfa0f..3703814a669b46 100644
> --- a/drivers/gpu/drm/i915/gvt/gvt.c
> +++ b/drivers/gpu/drm/i915/gvt/gvt.c
> @@ -54,14 +54,15 @@ intel_gvt_find_vgpu_type(struct intel_gvt *gvt, unsigned int type_group_id)
>  	return &gvt->types[type_group_id];
>  }
>  
> -static ssize_t available_instances_show(struct kobject *kobj,
> -					struct device *dev, char *buf)
> +static ssize_t available_instances_show(struct mdev_type *mtype,
> +					struct mdev_type_attribute *attr,
> +					char *buf)
>  {
>  	struct intel_vgpu_type *type;
>  	unsigned int num = 0;
> -	void *gvt = kdev_to_i915(dev)->gvt;
> +	void *gvt = kdev_to_i915(mtype_get_parent_dev(mtype))->gvt;
>  
> -	type = intel_gvt_find_vgpu_type(gvt, mtype_get_type_group_id(kobj));
> +	type = intel_gvt_find_vgpu_type(gvt, mtype_get_type_group_id(mtype));

Somewhere in this series you should probably
switch intel_gvt_find_vgpu_type to only get the mtype, as it can trivially
deduct the gvt from it (which also seems to have lost its type somewhere..)

Otherwise looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 69+ messages in thread

* RE: [PATCH 02/18] vfio/mdev: Add missing typesafety around mdev_device
  2021-03-23 17:55 ` [PATCH 02/18] vfio/mdev: Add missing typesafety around mdev_device Jason Gunthorpe
@ 2021-03-26  2:04   ` Tian, Kevin
  2021-03-30 15:24   ` Cornelia Huck
  1 sibling, 0 replies; 69+ messages in thread
From: Tian, Kevin @ 2021-03-26  2:04 UTC (permalink / raw)
  To: Jason Gunthorpe, Alex Williamson, Cornelia Huck, Jonathan Corbet,
	kvm, Kirti Wankhede, linux-doc
  Cc: Raj, Ashok, Williams, Dan J, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

> From: Jason Gunthorpe <jgg@nvidia.com>
> Sent: Wednesday, March 24, 2021 1:55 AM
> 
> The mdev API should accept and pass a 'struct mdev_device *' in all
> places, not pass a 'struct device *' and cast it internally with
> to_mdev_device(). Particularly in its struct mdev_driver functions, the
> whole point of a bus's struct device_driver wrapper is to provide type
> safety compared to the default struct device_driver.
> 
> Further, the driver core standard is for bus drivers to expose their
> device structure in their public headers that can be used with
> container_of() inlines and '&foo->dev' to go between the class levels, and
> '&foo->dev' to be used with dev_err/etc driver core helper functions. Move
> 'struct mdev_device' to mdev.h
> 
> Once done this allows moving some one instruction exported functions to
> static inlines, which in turns allows removing one of the two grotesque
> symbol_get()'s related to mdev in the core code.
> 
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>

Reviewed-by: Kevin Tian <kevin.tian@intel.com>

> ---
>  .../driver-api/vfio-mediated-device.rst       |  4 +-
>  drivers/vfio/mdev/mdev_core.c                 | 64 ++-----------------
>  drivers/vfio/mdev/mdev_driver.c               |  4 +-
>  drivers/vfio/mdev/mdev_private.h              | 23 +------
>  drivers/vfio/mdev/mdev_sysfs.c                | 26 ++++----
>  drivers/vfio/mdev/vfio_mdev.c                 |  7 +-
>  drivers/vfio/vfio_iommu_type1.c               | 25 ++------
>  include/linux/mdev.h                          | 58 +++++++++++++----
>  8 files changed, 83 insertions(+), 128 deletions(-)
> 
> diff --git a/Documentation/driver-api/vfio-mediated-device.rst
> b/Documentation/driver-api/vfio-mediated-device.rst
> index 25eb7d5b834ba3..c43c1dc3333373 100644
> --- a/Documentation/driver-api/vfio-mediated-device.rst
> +++ b/Documentation/driver-api/vfio-mediated-device.rst
> @@ -105,8 +105,8 @@ structure to represent a mediated device's driver::
>        */
>       struct mdev_driver {
>  	     const char *name;
> -	     int  (*probe)  (struct device *dev);
> -	     void (*remove) (struct device *dev);
> +	     int  (*probe)  (struct mdev_device *dev);
> +	     void (*remove) (struct mdev_device *dev);
>  	     struct device_driver    driver;
>       };
> 
> diff --git a/drivers/vfio/mdev/mdev_core.c
> b/drivers/vfio/mdev/mdev_core.c
> index 6de97d25a3f87d..057922a1707e04 100644
> --- a/drivers/vfio/mdev/mdev_core.c
> +++ b/drivers/vfio/mdev/mdev_core.c
> @@ -33,36 +33,6 @@ struct device *mdev_parent_dev(struct mdev_device
> *mdev)
>  }
>  EXPORT_SYMBOL(mdev_parent_dev);
> 
> -void *mdev_get_drvdata(struct mdev_device *mdev)
> -{
> -	return mdev->driver_data;
> -}
> -EXPORT_SYMBOL(mdev_get_drvdata);
> -
> -void mdev_set_drvdata(struct mdev_device *mdev, void *data)
> -{
> -	mdev->driver_data = data;
> -}
> -EXPORT_SYMBOL(mdev_set_drvdata);
> -
> -struct device *mdev_dev(struct mdev_device *mdev)
> -{
> -	return &mdev->dev;
> -}
> -EXPORT_SYMBOL(mdev_dev);
> -
> -struct mdev_device *mdev_from_dev(struct device *dev)
> -{
> -	return dev_is_mdev(dev) ? to_mdev_device(dev) : NULL;
> -}
> -EXPORT_SYMBOL(mdev_from_dev);
> -
> -const guid_t *mdev_uuid(struct mdev_device *mdev)
> -{
> -	return &mdev->uuid;
> -}
> -EXPORT_SYMBOL(mdev_uuid);
> -
>  /* Should be called holding parent_list_lock */
>  static struct mdev_parent *__find_parent_device(struct device *dev)
>  {
> @@ -107,7 +77,7 @@ static void mdev_device_remove_common(struct
> mdev_device *mdev)
>  	int ret;
> 
>  	type = to_mdev_type(mdev->type_kobj);
> -	mdev_remove_sysfs_files(&mdev->dev, type);
> +	mdev_remove_sysfs_files(mdev, type);
>  	device_del(&mdev->dev);
>  	parent = mdev->parent;
>  	lockdep_assert_held(&parent->unreg_sem);
> @@ -122,12 +92,10 @@ static void mdev_device_remove_common(struct
> mdev_device *mdev)
> 
>  static int mdev_device_remove_cb(struct device *dev, void *data)
>  {
> -	if (dev_is_mdev(dev)) {
> -		struct mdev_device *mdev;
> +	struct mdev_device *mdev = mdev_from_dev(dev);
> 
> -		mdev = to_mdev_device(dev);
> +	if (mdev)
>  		mdev_device_remove_common(mdev);
> -	}
>  	return 0;
>  }
> 
> @@ -332,7 +300,7 @@ int mdev_device_create(struct kobject *kobj,
>  	if (ret)
>  		goto add_fail;
> 
> -	ret = mdev_create_sysfs_files(&mdev->dev, type);
> +	ret = mdev_create_sysfs_files(mdev, type);
>  	if (ret)
>  		goto sysfs_fail;
> 
> @@ -354,13 +322,11 @@ int mdev_device_create(struct kobject *kobj,
>  	return ret;
>  }
> 
> -int mdev_device_remove(struct device *dev)
> +int mdev_device_remove(struct mdev_device *mdev)
>  {
> -	struct mdev_device *mdev, *tmp;
> +	struct mdev_device *tmp;
>  	struct mdev_parent *parent;
> 
> -	mdev = to_mdev_device(dev);
> -
>  	mutex_lock(&mdev_list_lock);
>  	list_for_each_entry(tmp, &mdev_list, next) {
>  		if (tmp == mdev)
> @@ -390,24 +356,6 @@ int mdev_device_remove(struct device *dev)
>  	return 0;
>  }
> 
> -int mdev_set_iommu_device(struct device *dev, struct device
> *iommu_device)
> -{
> -	struct mdev_device *mdev = to_mdev_device(dev);
> -
> -	mdev->iommu_device = iommu_device;
> -
> -	return 0;
> -}
> -EXPORT_SYMBOL(mdev_set_iommu_device);
> -
> -struct device *mdev_get_iommu_device(struct device *dev)
> -{
> -	struct mdev_device *mdev = to_mdev_device(dev);
> -
> -	return mdev->iommu_device;
> -}
> -EXPORT_SYMBOL(mdev_get_iommu_device);
> -
>  static int __init mdev_init(void)
>  {
>  	return mdev_bus_register();
> diff --git a/drivers/vfio/mdev/mdev_driver.c
> b/drivers/vfio/mdev/mdev_driver.c
> index 0d3223aee20b83..44c3ba7e56d923 100644
> --- a/drivers/vfio/mdev/mdev_driver.c
> +++ b/drivers/vfio/mdev/mdev_driver.c
> @@ -48,7 +48,7 @@ static int mdev_probe(struct device *dev)
>  		return ret;
> 
>  	if (drv && drv->probe) {
> -		ret = drv->probe(dev);
> +		ret = drv->probe(mdev);
>  		if (ret)
>  			mdev_detach_iommu(mdev);
>  	}
> @@ -62,7 +62,7 @@ static int mdev_remove(struct device *dev)
>  	struct mdev_device *mdev = to_mdev_device(dev);
> 
>  	if (drv && drv->remove)
> -		drv->remove(dev);
> +		drv->remove(mdev);
> 
>  	mdev_detach_iommu(mdev);
> 
> diff --git a/drivers/vfio/mdev/mdev_private.h
> b/drivers/vfio/mdev/mdev_private.h
> index 74c2e541146999..bb60ec4a8d9d21 100644
> --- a/drivers/vfio/mdev/mdev_private.h
> +++ b/drivers/vfio/mdev/mdev_private.h
> @@ -24,23 +24,6 @@ struct mdev_parent {
>  	struct rw_semaphore unreg_sem;
>  };
> 
> -struct mdev_device {
> -	struct device dev;
> -	struct mdev_parent *parent;
> -	guid_t uuid;
> -	void *driver_data;
> -	struct list_head next;
> -	struct kobject *type_kobj;
> -	struct device *iommu_device;
> -	bool active;
> -};
> -
> -static inline struct mdev_device *to_mdev_device(struct device *dev)
> -{
> -	return container_of(dev, struct mdev_device, dev);
> -}
> -#define dev_is_mdev(d)		((d)->bus == &mdev_bus_type)
> -
>  struct mdev_type {
>  	struct kobject kobj;
>  	struct kobject *devices_kobj;
> @@ -57,11 +40,11 @@ struct mdev_type {
>  int  parent_create_sysfs_files(struct mdev_parent *parent);
>  void parent_remove_sysfs_files(struct mdev_parent *parent);
> 
> -int  mdev_create_sysfs_files(struct device *dev, struct mdev_type *type);
> -void mdev_remove_sysfs_files(struct device *dev, struct mdev_type *type);
> +int  mdev_create_sysfs_files(struct mdev_device *mdev, struct mdev_type
> *type);
> +void mdev_remove_sysfs_files(struct mdev_device *mdev, struct
> mdev_type *type);
> 
>  int  mdev_device_create(struct kobject *kobj,
>  			struct device *dev, const guid_t *uuid);
> -int  mdev_device_remove(struct device *dev);
> +int  mdev_device_remove(struct mdev_device *dev);
> 
>  #endif /* MDEV_PRIVATE_H */
> diff --git a/drivers/vfio/mdev/mdev_sysfs.c
> b/drivers/vfio/mdev/mdev_sysfs.c
> index 917fd84c1c6f24..6a5450587b79e9 100644
> --- a/drivers/vfio/mdev/mdev_sysfs.c
> +++ b/drivers/vfio/mdev/mdev_sysfs.c
> @@ -225,6 +225,7 @@ int parent_create_sysfs_files(struct mdev_parent
> *parent)
>  static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
>  			    const char *buf, size_t count)
>  {
> +	struct mdev_device *mdev = to_mdev_device(dev);
>  	unsigned long val;
> 
>  	if (kstrtoul(buf, 0, &val) < 0)
> @@ -233,7 +234,7 @@ static ssize_t remove_store(struct device *dev, struct
> device_attribute *attr,
>  	if (val && device_remove_file_self(dev, attr)) {
>  		int ret;
> 
> -		ret = mdev_device_remove(dev);
> +		ret = mdev_device_remove(mdev);
>  		if (ret)
>  			return ret;
>  	}
> @@ -248,34 +249,37 @@ static const struct attribute *mdev_device_attrs[] =
> {
>  	NULL,
>  };
> 
> -int  mdev_create_sysfs_files(struct device *dev, struct mdev_type *type)
> +int mdev_create_sysfs_files(struct mdev_device *mdev, struct mdev_type
> *type)
>  {
> +	struct kobject *kobj = &mdev->dev.kobj;
>  	int ret;
> 
> -	ret = sysfs_create_link(type->devices_kobj, &dev->kobj,
> dev_name(dev));
> +	ret = sysfs_create_link(type->devices_kobj, kobj, dev_name(&mdev-
> >dev));
>  	if (ret)
>  		return ret;
> 
> -	ret = sysfs_create_link(&dev->kobj, &type->kobj, "mdev_type");
> +	ret = sysfs_create_link(kobj, &type->kobj, "mdev_type");
>  	if (ret)
>  		goto type_link_failed;
> 
> -	ret = sysfs_create_files(&dev->kobj, mdev_device_attrs);
> +	ret = sysfs_create_files(kobj, mdev_device_attrs);
>  	if (ret)
>  		goto create_files_failed;
> 
>  	return ret;
> 
>  create_files_failed:
> -	sysfs_remove_link(&dev->kobj, "mdev_type");
> +	sysfs_remove_link(kobj, "mdev_type");
>  type_link_failed:
> -	sysfs_remove_link(type->devices_kobj, dev_name(dev));
> +	sysfs_remove_link(type->devices_kobj, dev_name(&mdev->dev));
>  	return ret;
>  }
> 
> -void mdev_remove_sysfs_files(struct device *dev, struct mdev_type *type)
> +void mdev_remove_sysfs_files(struct mdev_device *mdev, struct
> mdev_type *type)
>  {
> -	sysfs_remove_files(&dev->kobj, mdev_device_attrs);
> -	sysfs_remove_link(&dev->kobj, "mdev_type");
> -	sysfs_remove_link(type->devices_kobj, dev_name(dev));
> +	struct kobject *kobj = &mdev->dev.kobj;
> +
> +	sysfs_remove_files(kobj, mdev_device_attrs);
> +	sysfs_remove_link(kobj, "mdev_type");
> +	sysfs_remove_link(type->devices_kobj, dev_name(&mdev->dev));
>  }
> diff --git a/drivers/vfio/mdev/vfio_mdev.c b/drivers/vfio/mdev/vfio_mdev.c
> index ae7e322fbe3c26..91b7b8b9eb9cb8 100644
> --- a/drivers/vfio/mdev/vfio_mdev.c
> +++ b/drivers/vfio/mdev/vfio_mdev.c
> @@ -124,9 +124,8 @@ static const struct vfio_device_ops
> vfio_mdev_dev_ops = {
>  	.request	= vfio_mdev_request,
>  };
> 
> -static int vfio_mdev_probe(struct device *dev)
> +static int vfio_mdev_probe(struct mdev_device *mdev)
>  {
> -	struct mdev_device *mdev = to_mdev_device(dev);
>  	struct vfio_device *vdev;
>  	int ret;
> 
> @@ -144,9 +143,9 @@ static int vfio_mdev_probe(struct device *dev)
>  	return 0;
>  }
> 
> -static void vfio_mdev_remove(struct device *dev)
> +static void vfio_mdev_remove(struct mdev_device *mdev)
>  {
> -	struct vfio_device *vdev = dev_get_drvdata(dev);
> +	struct vfio_device *vdev = dev_get_drvdata(&mdev->dev);
> 
>  	vfio_unregister_group_dev(vdev);
>  	kfree(vdev);
> diff --git a/drivers/vfio/vfio_iommu_type1.c
> b/drivers/vfio/vfio_iommu_type1.c
> index 4bb162c1d649b3..90b45ff1d87a7b 100644
> --- a/drivers/vfio/vfio_iommu_type1.c
> +++ b/drivers/vfio/vfio_iommu_type1.c
> @@ -1923,28 +1923,13 @@ static bool vfio_iommu_has_sw_msi(struct
> list_head *group_resv_regions,
>  	return ret;
>  }
> 
> -static struct device *vfio_mdev_get_iommu_device(struct device *dev)
> -{
> -	struct device *(*fn)(struct device *dev);
> -	struct device *iommu_device;
> -
> -	fn = symbol_get(mdev_get_iommu_device);
> -	if (fn) {
> -		iommu_device = fn(dev);
> -		symbol_put(mdev_get_iommu_device);
> -
> -		return iommu_device;
> -	}
> -
> -	return NULL;
> -}
> -
>  static int vfio_mdev_attach_domain(struct device *dev, void *data)
>  {
> +	struct mdev_device *mdev = to_mdev_device(dev);
>  	struct iommu_domain *domain = data;
>  	struct device *iommu_device;
> 
> -	iommu_device = vfio_mdev_get_iommu_device(dev);
> +	iommu_device = mdev_get_iommu_device(mdev);
>  	if (iommu_device) {
>  		if (iommu_dev_feature_enabled(iommu_device,
> IOMMU_DEV_FEAT_AUX))
>  			return iommu_aux_attach_device(domain,
> iommu_device);
> @@ -1957,10 +1942,11 @@ static int vfio_mdev_attach_domain(struct
> device *dev, void *data)
> 
>  static int vfio_mdev_detach_domain(struct device *dev, void *data)
>  {
> +	struct mdev_device *mdev = to_mdev_device(dev);
>  	struct iommu_domain *domain = data;
>  	struct device *iommu_device;
> 
> -	iommu_device = vfio_mdev_get_iommu_device(dev);
> +	iommu_device = mdev_get_iommu_device(mdev);
>  	if (iommu_device) {
>  		if (iommu_dev_feature_enabled(iommu_device,
> IOMMU_DEV_FEAT_AUX))
>  			iommu_aux_detach_device(domain, iommu_device);
> @@ -2008,9 +1994,10 @@ static bool vfio_bus_is_mdev(struct bus_type
> *bus)
> 
>  static int vfio_mdev_iommu_device(struct device *dev, void *data)
>  {
> +	struct mdev_device *mdev = to_mdev_device(dev);
>  	struct device **old = data, *new;
> 
> -	new = vfio_mdev_get_iommu_device(dev);
> +	new = mdev_get_iommu_device(mdev);
>  	if (!new || (*old && *old != new))
>  		return -EINVAL;
> 
> diff --git a/include/linux/mdev.h b/include/linux/mdev.h
> index 27eb383cb95de0..52f7ea19dd0f56 100644
> --- a/include/linux/mdev.h
> +++ b/include/linux/mdev.h
> @@ -10,7 +10,21 @@
>  #ifndef MDEV_H
>  #define MDEV_H
> 
> -struct mdev_device;
> +struct mdev_device {
> +	struct device dev;
> +	struct mdev_parent *parent;
> +	guid_t uuid;
> +	void *driver_data;
> +	struct list_head next;
> +	struct kobject *type_kobj;
> +	struct device *iommu_device;
> +	bool active;
> +};
> +
> +static inline struct mdev_device *to_mdev_device(struct device *dev)
> +{
> +	return container_of(dev, struct mdev_device, dev);
> +}
> 
>  /*
>   * Called by the parent device driver to set the device which represents
> @@ -19,12 +33,17 @@ struct mdev_device;
>   *
>   * @dev: the mediated device that iommu will isolate.
>   * @iommu_device: a pci device which represents the iommu for @dev.
> - *
> - * Return 0 for success, otherwise negative error value.
>   */
> -int mdev_set_iommu_device(struct device *dev, struct device
> *iommu_device);
> +static inline void mdev_set_iommu_device(struct mdev_device *mdev,
> +					 struct device *iommu_device)
> +{
> +	mdev->iommu_device = iommu_device;
> +}
> 
> -struct device *mdev_get_iommu_device(struct device *dev);
> +static inline struct device *mdev_get_iommu_device(struct mdev_device
> *mdev)
> +{
> +	return mdev->iommu_device;
> +}
> 
>  /**
>   * struct mdev_parent_ops - Structure to be registered for each parent
> device to
> @@ -126,16 +145,25 @@ struct mdev_type_attribute
> mdev_type_attr_##_name =		\
>   **/
>  struct mdev_driver {
>  	const char *name;
> -	int  (*probe)(struct device *dev);
> -	void (*remove)(struct device *dev);
> +	int (*probe)(struct mdev_device *dev);
> +	void (*remove)(struct mdev_device *dev);
>  	struct device_driver driver;
>  };
> 
>  #define to_mdev_driver(drv)	container_of(drv, struct mdev_driver, driver)
> 
> -void *mdev_get_drvdata(struct mdev_device *mdev);
> -void mdev_set_drvdata(struct mdev_device *mdev, void *data);
> -const guid_t *mdev_uuid(struct mdev_device *mdev);
> +static inline void *mdev_get_drvdata(struct mdev_device *mdev)
> +{
> +	return mdev->driver_data;
> +}
> +static inline void mdev_set_drvdata(struct mdev_device *mdev, void *data)
> +{
> +	mdev->driver_data = data;
> +}
> +static inline const guid_t *mdev_uuid(struct mdev_device *mdev)
> +{
> +	return &mdev->uuid;
> +}
> 
>  extern struct bus_type mdev_bus_type;
> 
> @@ -146,7 +174,13 @@ int mdev_register_driver(struct mdev_driver *drv,
> struct module *owner);
>  void mdev_unregister_driver(struct mdev_driver *drv);
> 
>  struct device *mdev_parent_dev(struct mdev_device *mdev);
> -struct device *mdev_dev(struct mdev_device *mdev);
> -struct mdev_device *mdev_from_dev(struct device *dev);
> +static inline struct device *mdev_dev(struct mdev_device *mdev)
> +{
> +	return &mdev->dev;
> +}
> +static inline struct mdev_device *mdev_from_dev(struct device *dev)
> +{
> +	return dev->bus == &mdev_bus_type ? to_mdev_device(dev) : NULL;
> +}
> 
>  #endif /* MDEV_H */
> --
> 2.31.0


^ permalink raw reply	[flat|nested] 69+ messages in thread

* RE: [PATCH 03/18] vfio/mdev: Simplify driver registration
  2021-03-23 17:55 ` [PATCH 03/18] vfio/mdev: Simplify driver registration Jason Gunthorpe
  2021-03-23 19:14   ` Christoph Hellwig
@ 2021-03-26  2:17   ` Tian, Kevin
  2021-03-30 15:28   ` Cornelia Huck
  2 siblings, 0 replies; 69+ messages in thread
From: Tian, Kevin @ 2021-03-26  2:17 UTC (permalink / raw)
  To: Jason Gunthorpe, Alex Williamson, Cornelia Huck, Jonathan Corbet,
	kvm, Kirti Wankhede, linux-doc
  Cc: Raj, Ashok, Williams, Dan J, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

> From: Jason Gunthorpe <jgg@nvidia.com>
> Sent: Wednesday, March 24, 2021 1:55 AM
> 
> This is only done once, we don't need to generate code to initialize a
> structure stored in the ELF .data segment. Fill in the three required
> .driver members directly instead of copying data into them during
> mdev_register_driver().
> 
> Further the to_mdev_driver() function doesn't belong in a public header,
> just inline it into the two places that need it. Finally, we can now
> clearly see that 'drv' derived from dev->driver cannot be NULL, firstly
> because the driver core forbids it, and secondly because NULL won't pass
> through the container_of(). Remove the dead code.
> 
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>

Reviewed-by: Kevin Tian <kevin.tian@intel.com>

> ---
>  Documentation/driver-api/vfio-mediated-device.rst |  5 +----
>  drivers/vfio/mdev/mdev_driver.c                   | 15 +++++++--------
>  drivers/vfio/mdev/vfio_mdev.c                     |  8 ++++++--
>  include/linux/mdev.h                              |  6 +-----
>  4 files changed, 15 insertions(+), 19 deletions(-)
> 
> diff --git a/Documentation/driver-api/vfio-mediated-device.rst
> b/Documentation/driver-api/vfio-mediated-device.rst
> index c43c1dc3333373..1779b85f014e2f 100644
> --- a/Documentation/driver-api/vfio-mediated-device.rst
> +++ b/Documentation/driver-api/vfio-mediated-device.rst
> @@ -98,13 +98,11 @@ structure to represent a mediated device's driver::
> 
>       /*
>        * struct mdev_driver [2] - Mediated device's driver
> -      * @name: driver name
>        * @probe: called when new device created
>        * @remove: called when device removed
>        * @driver: device driver structure
>        */
>       struct mdev_driver {
> -	     const char *name;
>  	     int  (*probe)  (struct mdev_device *dev);
>  	     void (*remove) (struct mdev_device *dev);
>  	     struct device_driver    driver;
> @@ -115,8 +113,7 @@ to register and unregister itself with the core driver:
> 
>  * Register::
> 
> -    extern int  mdev_register_driver(struct mdev_driver *drv,
> -				   struct module *owner);
> +    extern int  mdev_register_driver(struct mdev_driver *drv);
> 
>  * Unregister::
> 
> diff --git a/drivers/vfio/mdev/mdev_driver.c
> b/drivers/vfio/mdev/mdev_driver.c
> index 44c3ba7e56d923..041699571b7e55 100644
> --- a/drivers/vfio/mdev/mdev_driver.c
> +++ b/drivers/vfio/mdev/mdev_driver.c
> @@ -39,7 +39,8 @@ static void mdev_detach_iommu(struct mdev_device
> *mdev)
> 
>  static int mdev_probe(struct device *dev)
>  {
> -	struct mdev_driver *drv = to_mdev_driver(dev->driver);
> +	struct mdev_driver *drv =
> +		container_of(dev->driver, struct mdev_driver, driver);
>  	struct mdev_device *mdev = to_mdev_device(dev);
>  	int ret;
> 
> @@ -47,7 +48,7 @@ static int mdev_probe(struct device *dev)
>  	if (ret)
>  		return ret;
> 
> -	if (drv && drv->probe) {
> +	if (drv->probe) {
>  		ret = drv->probe(mdev);
>  		if (ret)
>  			mdev_detach_iommu(mdev);
> @@ -58,10 +59,11 @@ static int mdev_probe(struct device *dev)
> 
>  static int mdev_remove(struct device *dev)
>  {
> -	struct mdev_driver *drv = to_mdev_driver(dev->driver);
> +	struct mdev_driver *drv =
> +		container_of(dev->driver, struct mdev_driver, driver);
>  	struct mdev_device *mdev = to_mdev_device(dev);
> 
> -	if (drv && drv->remove)
> +	if (drv->remove)
>  		drv->remove(mdev);
> 
>  	mdev_detach_iommu(mdev);
> @@ -79,16 +81,13 @@ EXPORT_SYMBOL_GPL(mdev_bus_type);
>  /**
>   * mdev_register_driver - register a new MDEV driver
>   * @drv: the driver to register
> - * @owner: module owner of driver to be registered
>   *
>   * Returns a negative value on error, otherwise 0.
>   **/
> -int mdev_register_driver(struct mdev_driver *drv, struct module *owner)
> +int mdev_register_driver(struct mdev_driver *drv)
>  {
>  	/* initialize common driver fields */
> -	drv->driver.name = drv->name;
>  	drv->driver.bus = &mdev_bus_type;
> -	drv->driver.owner = owner;
> 
>  	/* register with core */
>  	return driver_register(&drv->driver);
> diff --git a/drivers/vfio/mdev/vfio_mdev.c b/drivers/vfio/mdev/vfio_mdev.c
> index 91b7b8b9eb9cb8..cc9507ed85a181 100644
> --- a/drivers/vfio/mdev/vfio_mdev.c
> +++ b/drivers/vfio/mdev/vfio_mdev.c
> @@ -152,14 +152,18 @@ static void vfio_mdev_remove(struct mdev_device
> *mdev)
>  }
> 
>  static struct mdev_driver vfio_mdev_driver = {
> -	.name	= "vfio_mdev",
> +	.driver = {
> +		.name = "vfio_mdev",
> +		.owner = THIS_MODULE,
> +		.mod_name = KBUILD_MODNAME,
> +	},
>  	.probe	= vfio_mdev_probe,
>  	.remove	= vfio_mdev_remove,
>  };
> 
>  static int __init vfio_mdev_init(void)
>  {
> -	return mdev_register_driver(&vfio_mdev_driver, THIS_MODULE);
> +	return mdev_register_driver(&vfio_mdev_driver);
>  }
> 
>  static void __exit vfio_mdev_exit(void)
> diff --git a/include/linux/mdev.h b/include/linux/mdev.h
> index 52f7ea19dd0f56..cb771c712da0f4 100644
> --- a/include/linux/mdev.h
> +++ b/include/linux/mdev.h
> @@ -137,21 +137,17 @@ struct mdev_type_attribute
> mdev_type_attr_##_name =		\
> 
>  /**
>   * struct mdev_driver - Mediated device driver
> - * @name: driver name
>   * @probe: called when new device created
>   * @remove: called when device removed
>   * @driver: device driver structure
>   *
>   **/
>  struct mdev_driver {
> -	const char *name;
>  	int (*probe)(struct mdev_device *dev);
>  	void (*remove)(struct mdev_device *dev);
>  	struct device_driver driver;
>  };
> 
> -#define to_mdev_driver(drv)	container_of(drv, struct mdev_driver, driver)
> -
>  static inline void *mdev_get_drvdata(struct mdev_device *mdev)
>  {
>  	return mdev->driver_data;
> @@ -170,7 +166,7 @@ extern struct bus_type mdev_bus_type;
>  int mdev_register_device(struct device *dev, const struct mdev_parent_ops
> *ops);
>  void mdev_unregister_device(struct device *dev);
> 
> -int mdev_register_driver(struct mdev_driver *drv, struct module *owner);
> +int mdev_register_driver(struct mdev_driver *drv);
>  void mdev_unregister_driver(struct mdev_driver *drv);
> 
>  struct device *mdev_parent_dev(struct mdev_device *mdev);
> --
> 2.31.0


^ permalink raw reply	[flat|nested] 69+ messages in thread

* RE: [PATCH 04/18] vfio/mdev: Use struct mdev_type in struct mdev_device
  2021-03-23 17:55 ` [PATCH 04/18] vfio/mdev: Use struct mdev_type in struct mdev_device Jason Gunthorpe
  2021-03-23 19:15   ` Christoph Hellwig
@ 2021-03-26  2:29   ` Tian, Kevin
  2021-04-06 18:41     ` Jason Gunthorpe
  2021-03-30 15:31   ` Cornelia Huck
  2 siblings, 1 reply; 69+ messages in thread
From: Tian, Kevin @ 2021-03-26  2:29 UTC (permalink / raw)
  To: Jason Gunthorpe, Alex Williamson, Cornelia Huck, kvm, Kirti Wankhede
  Cc: Raj, Ashok, Williams, Dan J, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

> From: Jason Gunthorpe <jgg@nvidia.com>
> Sent: Wednesday, March 24, 2021 1:55 AM
> 
> The kobj pointer in mdev_device is actually pointing at a struct
> mdev_type. Use the proper type so things are understandable.
> 
> There are a number of places that are confused and passing both the mdev
> and the mtype as function arguments, fix these to derive the mtype
> directly from the mdev to remove the redundancy.
> 
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> ---
>  drivers/vfio/mdev/mdev_core.c    | 16 ++++++----------
>  drivers/vfio/mdev/mdev_private.h |  7 +++----
>  drivers/vfio/mdev/mdev_sysfs.c   | 15 ++++++++-------
>  include/linux/mdev.h             |  4 +++-
>  4 files changed, 20 insertions(+), 22 deletions(-)
> 
> diff --git a/drivers/vfio/mdev/mdev_core.c
> b/drivers/vfio/mdev/mdev_core.c
> index 057922a1707e04..5ca0efa5266bad 100644
> --- a/drivers/vfio/mdev/mdev_core.c
> +++ b/drivers/vfio/mdev/mdev_core.c
> @@ -73,11 +73,9 @@ static void mdev_put_parent(struct mdev_parent
> *parent)
>  static void mdev_device_remove_common(struct mdev_device *mdev)
>  {
>  	struct mdev_parent *parent;
> -	struct mdev_type *type;
>  	int ret;
> 
> -	type = to_mdev_type(mdev->type_kobj);
> -	mdev_remove_sysfs_files(mdev, type);
> +	mdev_remove_sysfs_files(mdev);
>  	device_del(&mdev->dev);
>  	parent = mdev->parent;
>  	lockdep_assert_held(&parent->unreg_sem);
> @@ -241,13 +239,11 @@ static void mdev_device_release(struct device *dev)
>  	mdev_device_free(mdev);
>  }
> 
> -int mdev_device_create(struct kobject *kobj,
> -		       struct device *dev, const guid_t *uuid)
> +int mdev_device_create(struct mdev_type *type, const guid_t *uuid)
>  {
>  	int ret;
>  	struct mdev_device *mdev, *tmp;
>  	struct mdev_parent *parent;
> -	struct mdev_type *type = to_mdev_type(kobj);
> 
>  	parent = mdev_get_parent(type->parent);
>  	if (!parent)
> @@ -285,14 +281,14 @@ int mdev_device_create(struct kobject *kobj,
>  	}
> 
>  	device_initialize(&mdev->dev);
> -	mdev->dev.parent  = dev;
> +	mdev->dev.parent = parent->dev;
>  	mdev->dev.bus     = &mdev_bus_type;
>  	mdev->dev.release = mdev_device_release;
>  	dev_set_name(&mdev->dev, "%pUl", uuid);
>  	mdev->dev.groups = parent->ops->mdev_attr_groups;
> -	mdev->type_kobj = kobj;
> +	mdev->type = type;
> 
> -	ret = parent->ops->create(kobj, mdev);
> +	ret = parent->ops->create(&type->kobj, mdev);
>  	if (ret)
>  		goto ops_create_fail;
> 
> @@ -300,7 +296,7 @@ int mdev_device_create(struct kobject *kobj,
>  	if (ret)
>  		goto add_fail;
> 
> -	ret = mdev_create_sysfs_files(mdev, type);
> +	ret = mdev_create_sysfs_files(mdev);
>  	if (ret)
>  		goto sysfs_fail;
> 
> diff --git a/drivers/vfio/mdev/mdev_private.h
> b/drivers/vfio/mdev/mdev_private.h
> index bb60ec4a8d9d21..debf27f95b4f10 100644
> --- a/drivers/vfio/mdev/mdev_private.h
> +++ b/drivers/vfio/mdev/mdev_private.h
> @@ -40,11 +40,10 @@ struct mdev_type {
>  int  parent_create_sysfs_files(struct mdev_parent *parent);
>  void parent_remove_sysfs_files(struct mdev_parent *parent);
> 
> -int  mdev_create_sysfs_files(struct mdev_device *mdev, struct mdev_type
> *type);
> -void mdev_remove_sysfs_files(struct mdev_device *mdev, struct
> mdev_type *type);
> +int  mdev_create_sysfs_files(struct mdev_device *mdev);
> +void mdev_remove_sysfs_files(struct mdev_device *mdev);
> 
> -int  mdev_device_create(struct kobject *kobj,
> -			struct device *dev, const guid_t *uuid);
> +int mdev_device_create(struct mdev_type *kobj, const guid_t *uuid);
>  int  mdev_device_remove(struct mdev_device *dev);
> 
>  #endif /* MDEV_PRIVATE_H */
> diff --git a/drivers/vfio/mdev/mdev_sysfs.c
> b/drivers/vfio/mdev/mdev_sysfs.c
> index 6a5450587b79e9..321b4d13ead7b8 100644
> --- a/drivers/vfio/mdev/mdev_sysfs.c
> +++ b/drivers/vfio/mdev/mdev_sysfs.c
> @@ -67,7 +67,7 @@ static ssize_t create_store(struct kobject *kobj, struct
> device *dev,
>  	if (ret)
>  		return ret;
> 
> -	ret = mdev_device_create(kobj, dev, &uuid);
> +	ret = mdev_device_create(to_mdev_type(kobj), &uuid);
>  	if (ret)
>  		return ret;
> 
> @@ -249,16 +249,17 @@ static const struct attribute *mdev_device_attrs[] =
> {
>  	NULL,
>  };
> 
> -int mdev_create_sysfs_files(struct mdev_device *mdev, struct mdev_type
> *type)
> +int mdev_create_sysfs_files(struct mdev_device *mdev)
>  {
>  	struct kobject *kobj = &mdev->dev.kobj;

What about adding a local "struct mdev_type *type" here? otherwise,

Reviewed-by: Kevin Tian <kevin.tian@intel.com>

>  	int ret;
> 
> -	ret = sysfs_create_link(type->devices_kobj, kobj, dev_name(&mdev-
> >dev));
> +	ret = sysfs_create_link(mdev->type->devices_kobj, kobj,
> +				dev_name(&mdev->dev));
>  	if (ret)
>  		return ret;
> 
> -	ret = sysfs_create_link(kobj, &type->kobj, "mdev_type");
> +	ret = sysfs_create_link(kobj, &mdev->type->kobj, "mdev_type");
>  	if (ret)
>  		goto type_link_failed;
> 
> @@ -271,15 +272,15 @@ int mdev_create_sysfs_files(struct mdev_device
> *mdev, struct mdev_type *type)
>  create_files_failed:
>  	sysfs_remove_link(kobj, "mdev_type");
>  type_link_failed:
> -	sysfs_remove_link(type->devices_kobj, dev_name(&mdev->dev));
> +	sysfs_remove_link(mdev->type->devices_kobj, dev_name(&mdev-
> >dev));
>  	return ret;
>  }
> 
> -void mdev_remove_sysfs_files(struct mdev_device *mdev, struct
> mdev_type *type)
> +void mdev_remove_sysfs_files(struct mdev_device *mdev)
>  {
>  	struct kobject *kobj = &mdev->dev.kobj;
> 
>  	sysfs_remove_files(kobj, mdev_device_attrs);
>  	sysfs_remove_link(kobj, "mdev_type");
> -	sysfs_remove_link(type->devices_kobj, dev_name(&mdev->dev));
> +	sysfs_remove_link(mdev->type->devices_kobj, dev_name(&mdev-
> >dev));
>  }
> diff --git a/include/linux/mdev.h b/include/linux/mdev.h
> index cb771c712da0f4..349e8ac1fe3382 100644
> --- a/include/linux/mdev.h
> +++ b/include/linux/mdev.h
> @@ -10,13 +10,15 @@
>  #ifndef MDEV_H
>  #define MDEV_H
> 
> +struct mdev_type;
> +
>  struct mdev_device {
>  	struct device dev;
>  	struct mdev_parent *parent;
>  	guid_t uuid;
>  	void *driver_data;
>  	struct list_head next;
> -	struct kobject *type_kobj;
> +	struct mdev_type *type;
>  	struct device *iommu_device;
>  	bool active;
>  };
> --
> 2.31.0


^ permalink raw reply	[flat|nested] 69+ messages in thread

* RE: [PATCH 05/18] vfio/mdev: Do not allow a mdev_type to have a NULL parent pointer
  2021-03-23 17:55 ` [PATCH 05/18] vfio/mdev: Do not allow a mdev_type to have a NULL parent pointer Jason Gunthorpe
  2021-03-23 19:15   ` Christoph Hellwig
@ 2021-03-26  2:38   ` Tian, Kevin
  2021-03-29  8:42   ` Max Gurtovoy
  2021-03-30 15:34   ` Cornelia Huck
  3 siblings, 0 replies; 69+ messages in thread
From: Tian, Kevin @ 2021-03-26  2:38 UTC (permalink / raw)
  To: Jason Gunthorpe, Cornelia Huck, kvm
  Cc: Alex Williamson, Raj, Ashok, Dong Jia Shi, Neo Jia, Williams,
	Dan J, Christoph Hellwig, Jike Song, Kirti Wankhede,
	Leon Romanovsky, Max Gurtovoy, Tarun Gupta

> From: Jason Gunthorpe <jgg@nvidia.com>
> Sent: Wednesday, March 24, 2021 1:55 AM
> 
> There is a small race where the parent is NULL even though the kobj has
> already been made visible in sysfs.
> 
> For instance the attribute_group is made visible in sysfs_create_files()
> and the mdev_type_attr_show() does:
> 
>     ret = attr->show(kobj, type->parent->dev, buf);
> 
> Which will crash on NULL parent. Move the parent setup to before the type
> pointer leaves the stack frame.
> 
> Fixes: 7b96953bc640 ("vfio: Mediated device Core driver")
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>

Reviewed-by: Kevin Tian <kevin.tian@intel.com>

> ---
>  drivers/vfio/mdev/mdev_sysfs.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/vfio/mdev/mdev_sysfs.c
> b/drivers/vfio/mdev/mdev_sysfs.c
> index 321b4d13ead7b8..5219cdd6dbbc49 100644
> --- a/drivers/vfio/mdev/mdev_sysfs.c
> +++ b/drivers/vfio/mdev/mdev_sysfs.c
> @@ -105,6 +105,7 @@ static struct mdev_type
> *add_mdev_supported_type(struct mdev_parent *parent,
>  		return ERR_PTR(-ENOMEM);
> 
>  	type->kobj.kset = parent->mdev_types_kset;
> +	type->parent = parent;
> 
>  	ret = kobject_init_and_add(&type->kobj, &mdev_type_ktype, NULL,
>  				   "%s-%s", dev_driver_string(parent->dev),
> @@ -132,7 +133,6 @@ static struct mdev_type
> *add_mdev_supported_type(struct mdev_parent *parent,
>  	}
> 
>  	type->group = group;
> -	type->parent = parent;
>  	return type;
> 
>  attrs_failed:
> --
> 2.31.0


^ permalink raw reply	[flat|nested] 69+ messages in thread

* RE: [PATCH 06/18] vfio/mdev: Expose mdev_get/put_parent to mdev_private.h
  2021-03-23 17:55 ` [PATCH 06/18] vfio/mdev: Expose mdev_get/put_parent to mdev_private.h Jason Gunthorpe
  2021-03-23 19:16   ` Christoph Hellwig
@ 2021-03-26  2:47   ` Tian, Kevin
  2021-03-30 15:37   ` Cornelia Huck
  2 siblings, 0 replies; 69+ messages in thread
From: Tian, Kevin @ 2021-03-26  2:47 UTC (permalink / raw)
  To: Jason Gunthorpe, Alex Williamson, Cornelia Huck, kvm, Kirti Wankhede
  Cc: Raj, Ashok, Williams, Dan J, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

> From: Jason Gunthorpe <jgg@nvidia.com>
> Sent: Wednesday, March 24, 2021 1:55 AM
> 
> The next patch will use these in mdev_sysfs.c
> 
> While here remove the now dead code checks for NULL, a mdev_type can
> never
> have a NULL parent.
> 
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>

Reviewed-by: Kevin Tian <kevin.tian@intel.com>

> ---
>  drivers/vfio/mdev/mdev_core.c    | 23 +++--------------------
>  drivers/vfio/mdev/mdev_private.h | 12 ++++++++++++
>  2 files changed, 15 insertions(+), 20 deletions(-)
> 
> diff --git a/drivers/vfio/mdev/mdev_core.c
> b/drivers/vfio/mdev/mdev_core.c
> index 5ca0efa5266bad..7ec21c907397a5 100644
> --- a/drivers/vfio/mdev/mdev_core.c
> +++ b/drivers/vfio/mdev/mdev_core.c
> @@ -45,7 +45,7 @@ static struct mdev_parent *__find_parent_device(struct
> device *dev)
>  	return NULL;
>  }
> 
> -static void mdev_release_parent(struct kref *kref)
> +void mdev_release_parent(struct kref *kref)
>  {
>  	struct mdev_parent *parent = container_of(kref, struct mdev_parent,
>  						  ref);
> @@ -55,20 +55,6 @@ static void mdev_release_parent(struct kref *kref)
>  	put_device(dev);
>  }
> 
> -static struct mdev_parent *mdev_get_parent(struct mdev_parent *parent)
> -{
> -	if (parent)
> -		kref_get(&parent->ref);
> -
> -	return parent;
> -}
> -
> -static void mdev_put_parent(struct mdev_parent *parent)
> -{
> -	if (parent)
> -		kref_put(&parent->ref, mdev_release_parent);
> -}
> -
>  /* Caller must hold parent unreg_sem read or write lock */
>  static void mdev_device_remove_common(struct mdev_device *mdev)
>  {
> @@ -243,12 +229,9 @@ int mdev_device_create(struct mdev_type *type,
> const guid_t *uuid)
>  {
>  	int ret;
>  	struct mdev_device *mdev, *tmp;
> -	struct mdev_parent *parent;
> -
> -	parent = mdev_get_parent(type->parent);
> -	if (!parent)
> -		return -EINVAL;
> +	struct mdev_parent *parent = type->parent;
> 
> +	mdev_get_parent(parent);
>  	mutex_lock(&mdev_list_lock);
> 
>  	/* Check for duplicate */
> diff --git a/drivers/vfio/mdev/mdev_private.h
> b/drivers/vfio/mdev/mdev_private.h
> index debf27f95b4f10..10eccc35782c4d 100644
> --- a/drivers/vfio/mdev/mdev_private.h
> +++ b/drivers/vfio/mdev/mdev_private.h
> @@ -46,4 +46,16 @@ void mdev_remove_sysfs_files(struct mdev_device
> *mdev);
>  int mdev_device_create(struct mdev_type *kobj, const guid_t *uuid);
>  int  mdev_device_remove(struct mdev_device *dev);
> 
> +void mdev_release_parent(struct kref *kref);
> +
> +static inline void mdev_get_parent(struct mdev_parent *parent)
> +{
> +	kref_get(&parent->ref);
> +}
> +
> +static inline void mdev_put_parent(struct mdev_parent *parent)
> +{
> +	kref_put(&parent->ref, mdev_release_parent);
> +}
> +
>  #endif /* MDEV_PRIVATE_H */
> --
> 2.31.0


^ permalink raw reply	[flat|nested] 69+ messages in thread

* RE: [PATCH 07/18] vfio/mdev: Add missing reference counting to mdev_type
  2021-03-23 17:55 ` [PATCH 07/18] vfio/mdev: Add missing reference counting to mdev_type Jason Gunthorpe
  2021-03-23 19:17   ` Christoph Hellwig
@ 2021-03-26  2:52   ` Tian, Kevin
  2021-03-30 15:38   ` Cornelia Huck
  2 siblings, 0 replies; 69+ messages in thread
From: Tian, Kevin @ 2021-03-26  2:52 UTC (permalink / raw)
  To: Jason Gunthorpe, Alex Williamson, Cornelia Huck, kvm, Kirti Wankhede
  Cc: Raj, Ashok, Williams, Dan J, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

> From: Jason Gunthorpe <jgg@nvidia.com>
> Sent: Wednesday, March 24, 2021 1:55 AM
> 
> struct mdev_type holds a pointer to the kref'd object struct mdev_parent,
> but doesn't hold the kref. The lifetime of the parent becomes implicit
> because parent_remove_sysfs_files() is supposed to remove all the access
> before the parent can be freed, but this is very hard to reason about.
> 
> Make it obviously correct by adding the missing get.
> 
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>

Reviewed-by: Kevin Tian <kevin.tian@intel.com>

> ---
>  drivers/vfio/mdev/mdev_sysfs.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/drivers/vfio/mdev/mdev_sysfs.c
> b/drivers/vfio/mdev/mdev_sysfs.c
> index 5219cdd6dbbc49..d43775bd0ba340 100644
> --- a/drivers/vfio/mdev/mdev_sysfs.c
> +++ b/drivers/vfio/mdev/mdev_sysfs.c
> @@ -81,6 +81,8 @@ static void mdev_type_release(struct kobject *kobj)
>  	struct mdev_type *type = to_mdev_type(kobj);
> 
>  	pr_debug("Releasing group %s\n", kobj->name);
> +	/* Pairs with the get in add_mdev_supported_type() */
> +	mdev_put_parent(type->parent);
>  	kfree(type);
>  }
> 
> @@ -106,6 +108,8 @@ static struct mdev_type
> *add_mdev_supported_type(struct mdev_parent *parent,
> 
>  	type->kobj.kset = parent->mdev_types_kset;
>  	type->parent = parent;
> +	/* Pairs with the put in mdev_type_release() */
> +	mdev_get_parent(parent);
> 
>  	ret = kobject_init_and_add(&type->kobj, &mdev_type_ktype, NULL,
>  				   "%s-%s", dev_driver_string(parent->dev),
> --
> 2.31.0


^ permalink raw reply	[flat|nested] 69+ messages in thread

* RE: [PATCH 08/18] vfio/mdev: Reorganize mdev_device_create()
  2021-03-23 17:55 ` [PATCH 08/18] vfio/mdev: Reorganize mdev_device_create() Jason Gunthorpe
  2021-03-23 19:20   ` Christoph Hellwig
@ 2021-03-26  3:31   ` Tian, Kevin
  2021-03-30 15:50   ` Cornelia Huck
  2 siblings, 0 replies; 69+ messages in thread
From: Tian, Kevin @ 2021-03-26  3:31 UTC (permalink / raw)
  To: Jason Gunthorpe, Alex Williamson, Cornelia Huck, kvm, Kirti Wankhede
  Cc: Raj, Ashok, Williams, Dan J, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

> From: Jason Gunthorpe <jgg@nvidia.com>
> Sent: Wednesday, March 24, 2021 1:55 AM
> 
> Once the memory for the struct mdev_device is allocated it should
> immediately be device_initialize()'d and filled in so that put_device()
> can always be used to undo the allocation.
> 
> Place the mdev_get/put_parent() so that they are clearly protecting the
> mdev->parent pointer. Move the final put to the release function so that
> the lifetime rules are trivial to understand.
> 
> Remove mdev_device_free() as the release function via device_put() is now
> usable in all cases.
> 
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>

Reviewed-by: Kevin Tian <kevin.tian@intel.com>

> ---
>  drivers/vfio/mdev/mdev_core.c | 46 +++++++++++++++--------------------
>  1 file changed, 20 insertions(+), 26 deletions(-)
> 
> diff --git a/drivers/vfio/mdev/mdev_core.c
> b/drivers/vfio/mdev/mdev_core.c
> index 7ec21c907397a5..517b6fd351b63a 100644
> --- a/drivers/vfio/mdev/mdev_core.c
> +++ b/drivers/vfio/mdev/mdev_core.c
> @@ -71,7 +71,6 @@ static void mdev_device_remove_common(struct
> mdev_device *mdev)
> 
>  	/* Balances with device_initialize() */
>  	put_device(&mdev->dev);
> -	mdev_put_parent(parent);
>  }
> 
>  static int mdev_device_remove_cb(struct device *dev, void *data)
> @@ -208,8 +207,13 @@ void mdev_unregister_device(struct device *dev)
>  }
>  EXPORT_SYMBOL(mdev_unregister_device);
> 
> -static void mdev_device_free(struct mdev_device *mdev)
> +static void mdev_device_release(struct device *dev)
>  {
> +	struct mdev_device *mdev = to_mdev_device(dev);
> +
> +	/* Pairs with the get in mdev_device_create() */
> +	mdev_put_parent(mdev->parent);
> +
>  	mutex_lock(&mdev_list_lock);
>  	list_del(&mdev->next);
>  	mutex_unlock(&mdev_list_lock);
> @@ -218,59 +222,50 @@ static void mdev_device_free(struct mdev_device
> *mdev)
>  	kfree(mdev);
>  }
> 
> -static void mdev_device_release(struct device *dev)
> -{
> -	struct mdev_device *mdev = to_mdev_device(dev);
> -
> -	mdev_device_free(mdev);
> -}
> -
>  int mdev_device_create(struct mdev_type *type, const guid_t *uuid)
>  {
>  	int ret;
>  	struct mdev_device *mdev, *tmp;
>  	struct mdev_parent *parent = type->parent;
> 
> -	mdev_get_parent(parent);
>  	mutex_lock(&mdev_list_lock);
> 
>  	/* Check for duplicate */
>  	list_for_each_entry(tmp, &mdev_list, next) {
>  		if (guid_equal(&tmp->uuid, uuid)) {
>  			mutex_unlock(&mdev_list_lock);
> -			ret = -EEXIST;
> -			goto mdev_fail;
> +			return -EEXIST;
>  		}
>  	}
> 
>  	mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
>  	if (!mdev) {
>  		mutex_unlock(&mdev_list_lock);
> -		ret = -ENOMEM;
> -		goto mdev_fail;
> +		return -ENOMEM;
>  	}
> 
> +	device_initialize(&mdev->dev);
> +	mdev->dev.parent  = parent->dev;
> +	mdev->dev.bus = &mdev_bus_type;
> +	mdev->dev.release = mdev_device_release;
> +	mdev->dev.groups = parent->ops->mdev_attr_groups;
> +	mdev->type = type;
> +	mdev->parent = parent;
> +	/* Pairs with the put in mdev_device_release() */
> +	mdev_get_parent(parent);
> +
>  	guid_copy(&mdev->uuid, uuid);
>  	list_add(&mdev->next, &mdev_list);
>  	mutex_unlock(&mdev_list_lock);
> 
> -	mdev->parent = parent;
> +	dev_set_name(&mdev->dev, "%pUl", uuid);
> 
>  	/* Check if parent unregistration has started */
>  	if (!down_read_trylock(&parent->unreg_sem)) {
> -		mdev_device_free(mdev);
>  		ret = -ENODEV;
>  		goto mdev_fail;
>  	}
> 
> -	device_initialize(&mdev->dev);
> -	mdev->dev.parent = parent->dev;
> -	mdev->dev.bus     = &mdev_bus_type;
> -	mdev->dev.release = mdev_device_release;
> -	dev_set_name(&mdev->dev, "%pUl", uuid);
> -	mdev->dev.groups = parent->ops->mdev_attr_groups;
> -	mdev->type = type;
> -
>  	ret = parent->ops->create(&type->kobj, mdev);
>  	if (ret)
>  		goto ops_create_fail;
> @@ -295,9 +290,8 @@ int mdev_device_create(struct mdev_type *type,
> const guid_t *uuid)
>  	parent->ops->remove(mdev);
>  ops_create_fail:
>  	up_read(&parent->unreg_sem);
> -	put_device(&mdev->dev);
>  mdev_fail:
> -	mdev_put_parent(parent);
> +	put_device(&mdev->dev);
>  	return ret;
>  }
> 
> --
> 2.31.0


^ permalink raw reply	[flat|nested] 69+ messages in thread

* RE: [PATCH 08/18] vfio/mdev: Reorganize mdev_device_create()
  2021-03-23 19:20   ` Christoph Hellwig
@ 2021-03-26  3:33     ` Tian, Kevin
  2021-03-26  3:36       ` Tian, Kevin
  2021-04-06 16:40     ` Jason Gunthorpe
  1 sibling, 1 reply; 69+ messages in thread
From: Tian, Kevin @ 2021-03-26  3:33 UTC (permalink / raw)
  To: Christoph Hellwig, Jason Gunthorpe
  Cc: Alex Williamson, Cornelia Huck, kvm, Kirti Wankhede, Raj, Ashok,
	Williams, Dan J, Leon Romanovsky, Max Gurtovoy, Tarun Gupta

> From: Christoph Hellwig <hch@lst.de>
> Sent: Wednesday, March 24, 2021 3:21 AM
> 
> >  	up_read(&parent->unreg_sem);
> > -	put_device(&mdev->dev);
> >  mdev_fail:
> >
> >
> >
> > -	mdev_put_parent(parent);
> > +	put_device(&mdev->dev);
> 
> That mdev_fail label is not very descriptive, what about free_device
> instead?
> 

There is only one goto to this place after this patch. Possibly just 
call it trylock_fail.


^ permalink raw reply	[flat|nested] 69+ messages in thread

* RE: [PATCH 09/18] vfio/mdev: Add missing error handling to dev_set_name()
  2021-03-23 17:55 ` [PATCH 09/18] vfio/mdev: Add missing error handling to dev_set_name() Jason Gunthorpe
  2021-03-23 19:21   ` Christoph Hellwig
@ 2021-03-26  3:35   ` Tian, Kevin
  2021-03-29  8:50   ` Max Gurtovoy
  2021-03-30 15:53   ` Cornelia Huck
  3 siblings, 0 replies; 69+ messages in thread
From: Tian, Kevin @ 2021-03-26  3:35 UTC (permalink / raw)
  To: Jason Gunthorpe, Cornelia Huck, kvm
  Cc: Alex Williamson, Raj, Ashok, Dong Jia Shi, Neo Jia, Williams,
	Dan J, Christoph Hellwig, Jike Song, Kirti Wankhede,
	Leon Romanovsky, Max Gurtovoy, Tarun Gupta

> From: Jason Gunthorpe <jgg@nvidia.com>
> Sent: Wednesday, March 24, 2021 1:55 AM
> 
> This can fail, and seems to be a popular target for syzkaller error
> injection. Check the error return and unwind with put_device().
> 
> Fixes: 7b96953bc640 ("vfio: Mediated device Core driver")
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>

Reviewed-by: Kevin Tian <kevin.tian@intel.com>

> ---
>  drivers/vfio/mdev/mdev_core.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/vfio/mdev/mdev_core.c
> b/drivers/vfio/mdev/mdev_core.c
> index 517b6fd351b63a..4b5e372ed58f26 100644
> --- a/drivers/vfio/mdev/mdev_core.c
> +++ b/drivers/vfio/mdev/mdev_core.c
> @@ -258,7 +258,9 @@ int mdev_device_create(struct mdev_type *type,
> const guid_t *uuid)
>  	list_add(&mdev->next, &mdev_list);
>  	mutex_unlock(&mdev_list_lock);
> 
> -	dev_set_name(&mdev->dev, "%pUl", uuid);
> +	ret = dev_set_name(&mdev->dev, "%pUl", uuid);
> +	if (ret)
> +		goto mdev_fail;
> 
>  	/* Check if parent unregistration has started */
>  	if (!down_read_trylock(&parent->unreg_sem)) {
> --
> 2.31.0


^ permalink raw reply	[flat|nested] 69+ messages in thread

* RE: [PATCH 08/18] vfio/mdev: Reorganize mdev_device_create()
  2021-03-26  3:33     ` Tian, Kevin
@ 2021-03-26  3:36       ` Tian, Kevin
  0 siblings, 0 replies; 69+ messages in thread
From: Tian, Kevin @ 2021-03-26  3:36 UTC (permalink / raw)
  To: Tian, Kevin, Christoph Hellwig, Jason Gunthorpe
  Cc: Alex Williamson, Cornelia Huck, kvm, Kirti Wankhede, Raj, Ashok,
	Williams, Dan J, Leon Romanovsky, Max Gurtovoy, Tarun Gupta

> From: Tian, Kevin <kevin.tian@intel.com>
> Sent: Friday, March 26, 2021 11:34 AM
> 
> > From: Christoph Hellwig <hch@lst.de>
> > Sent: Wednesday, March 24, 2021 3:21 AM
> >
> > >  	up_read(&parent->unreg_sem);
> > > -	put_device(&mdev->dev);
> > >  mdev_fail:
> > >
> > >
> > >
> > > -	mdev_put_parent(parent);
> > > +	put_device(&mdev->dev);
> >
> > That mdev_fail label is not very descriptive, what about free_device
> > instead?
> >
> 
> There is only one goto to this place after this patch. Possibly just
> call it trylock_fail.

oops, please forget this comment as another goto comes in next patch...

^ permalink raw reply	[flat|nested] 69+ messages in thread

* RE: [PATCH 10/18] vfio/mdev: Remove duplicate storage of parent in mdev_device
  2021-03-23 17:55 ` [PATCH 10/18] vfio/mdev: Remove duplicate storage of parent in mdev_device Jason Gunthorpe
  2021-03-23 19:22   ` Christoph Hellwig
@ 2021-03-26  3:53   ` Tian, Kevin
  2021-03-26 11:53     ` Jason Gunthorpe
  2021-03-30 16:15   ` Cornelia Huck
  2 siblings, 1 reply; 69+ messages in thread
From: Tian, Kevin @ 2021-03-26  3:53 UTC (permalink / raw)
  To: Jason Gunthorpe, Alex Williamson, Cornelia Huck, kvm, Kirti Wankhede
  Cc: Raj, Ashok, Williams, Dan J, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

> From: Jason Gunthorpe <jgg@nvidia.com>
> Sent: Wednesday, March 24, 2021 1:55 AM
> 
> mdev_device->type->parent is the same thing.
> 
> The struct mdev_device was relying on the kref on the mdev_parent to also
> indirectly hold a kref on the mdev_type pointer. Now that the type holds a
> kref on the parent we can directly kref the mdev_type and remove this
> implicit relationship.
> 
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> ---
>  drivers/vfio/mdev/mdev_core.c | 13 +++++--------
>  drivers/vfio/mdev/vfio_mdev.c | 14 +++++++-------
>  include/linux/mdev.h          |  1 -
>  3 files changed, 12 insertions(+), 16 deletions(-)
> 
> diff --git a/drivers/vfio/mdev/mdev_core.c
> b/drivers/vfio/mdev/mdev_core.c
> index 4b5e372ed58f26..493df3da451339 100644
> --- a/drivers/vfio/mdev/mdev_core.c
> +++ b/drivers/vfio/mdev/mdev_core.c
> @@ -29,7 +29,7 @@ static DEFINE_MUTEX(mdev_list_lock);
> 
>  struct device *mdev_parent_dev(struct mdev_device *mdev)
>  {
> -	return mdev->parent->dev;
> +	return mdev->type->parent->dev;
>  }
>  EXPORT_SYMBOL(mdev_parent_dev);
> 
> @@ -58,12 +58,11 @@ void mdev_release_parent(struct kref *kref)
>  /* Caller must hold parent unreg_sem read or write lock */
>  static void mdev_device_remove_common(struct mdev_device *mdev)
>  {
> -	struct mdev_parent *parent;
> +	struct mdev_parent *parent = mdev->type->parent;

What about having a wrapper here, like mdev_parent_dev? For
readability it's not necessary to show that the parent is indirectly
retrieved through mdev_type.

>  	int ret;
> 
>  	mdev_remove_sysfs_files(mdev);
>  	device_del(&mdev->dev);
> -	parent = mdev->parent;
>  	lockdep_assert_held(&parent->unreg_sem);
>  	ret = parent->ops->remove(mdev);
>  	if (ret)
> @@ -212,7 +211,7 @@ static void mdev_device_release(struct device *dev)
>  	struct mdev_device *mdev = to_mdev_device(dev);
> 
>  	/* Pairs with the get in mdev_device_create() */
> -	mdev_put_parent(mdev->parent);
> +	kobject_put(&mdev->type->kobj);

Maybe keep mdev_get/put_parent and change them to accept "struct
mdev_device *" parameter like other places.

> 
>  	mutex_lock(&mdev_list_lock);
>  	list_del(&mdev->next);
> @@ -250,9 +249,8 @@ int mdev_device_create(struct mdev_type *type,
> const guid_t *uuid)
>  	mdev->dev.release = mdev_device_release;
>  	mdev->dev.groups = parent->ops->mdev_attr_groups;
>  	mdev->type = type;
> -	mdev->parent = parent;
>  	/* Pairs with the put in mdev_device_release() */
> -	mdev_get_parent(parent);
> +	kobject_get(&type->kobj);
> 
>  	guid_copy(&mdev->uuid, uuid);
>  	list_add(&mdev->next, &mdev_list);
> @@ -300,7 +298,7 @@ int mdev_device_create(struct mdev_type *type,
> const guid_t *uuid)
>  int mdev_device_remove(struct mdev_device *mdev)
>  {
>  	struct mdev_device *tmp;
> -	struct mdev_parent *parent;
> +	struct mdev_parent *parent = mdev->type->parent;
> 
>  	mutex_lock(&mdev_list_lock);
>  	list_for_each_entry(tmp, &mdev_list, next) {
> @@ -321,7 +319,6 @@ int mdev_device_remove(struct mdev_device *mdev)
>  	mdev->active = false;
>  	mutex_unlock(&mdev_list_lock);
> 
> -	parent = mdev->parent;
>  	/* Check if parent unregistration has started */
>  	if (!down_read_trylock(&parent->unreg_sem))
>  		return -ENODEV;
> diff --git a/drivers/vfio/mdev/vfio_mdev.c b/drivers/vfio/mdev/vfio_mdev.c
> index cc9507ed85a181..922729071c5a8e 100644
> --- a/drivers/vfio/mdev/vfio_mdev.c
> +++ b/drivers/vfio/mdev/vfio_mdev.c
> @@ -24,7 +24,7 @@
>  static int vfio_mdev_open(struct vfio_device *core_vdev)
>  {
>  	struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
> -	struct mdev_parent *parent = mdev->parent;
> +	struct mdev_parent *parent = mdev->type->parent;
> 
>  	int ret;
> 
> @@ -44,7 +44,7 @@ static int vfio_mdev_open(struct vfio_device
> *core_vdev)
>  static void vfio_mdev_release(struct vfio_device *core_vdev)
>  {
>  	struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
> -	struct mdev_parent *parent = mdev->parent;
> +	struct mdev_parent *parent = mdev->type->parent;
> 
>  	if (likely(parent->ops->release))
>  		parent->ops->release(mdev);
> @@ -56,7 +56,7 @@ static long vfio_mdev_unlocked_ioctl(struct vfio_device
> *core_vdev,
>  				     unsigned int cmd, unsigned long arg)
>  {
>  	struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
> -	struct mdev_parent *parent = mdev->parent;
> +	struct mdev_parent *parent = mdev->type->parent;
> 
>  	if (unlikely(!parent->ops->ioctl))
>  		return -EINVAL;
> @@ -68,7 +68,7 @@ static ssize_t vfio_mdev_read(struct vfio_device
> *core_vdev, char __user *buf,
>  			      size_t count, loff_t *ppos)
>  {
>  	struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
> -	struct mdev_parent *parent = mdev->parent;
> +	struct mdev_parent *parent = mdev->type->parent;
> 
>  	if (unlikely(!parent->ops->read))
>  		return -EINVAL;
> @@ -81,7 +81,7 @@ static ssize_t vfio_mdev_write(struct vfio_device
> *core_vdev,
>  			       loff_t *ppos)
>  {
>  	struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
> -	struct mdev_parent *parent = mdev->parent;
> +	struct mdev_parent *parent = mdev->type->parent;
> 
>  	if (unlikely(!parent->ops->write))
>  		return -EINVAL;
> @@ -93,7 +93,7 @@ static int vfio_mdev_mmap(struct vfio_device
> *core_vdev,
>  			  struct vm_area_struct *vma)
>  {
>  	struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
> -	struct mdev_parent *parent = mdev->parent;
> +	struct mdev_parent *parent = mdev->type->parent;
> 
>  	if (unlikely(!parent->ops->mmap))
>  		return -EINVAL;
> @@ -104,7 +104,7 @@ static int vfio_mdev_mmap(struct vfio_device
> *core_vdev,
>  static void vfio_mdev_request(struct vfio_device *core_vdev, unsigned int
> count)
>  {
>  	struct mdev_device *mdev = to_mdev_device(core_vdev->dev);
> -	struct mdev_parent *parent = mdev->parent;
> +	struct mdev_parent *parent = mdev->type->parent;
> 
>  	if (parent->ops->request)
>  		parent->ops->request(mdev, count);
> diff --git a/include/linux/mdev.h b/include/linux/mdev.h
> index 349e8ac1fe3382..fb582adda28a9b 100644
> --- a/include/linux/mdev.h
> +++ b/include/linux/mdev.h
> @@ -14,7 +14,6 @@ struct mdev_type;
> 
>  struct mdev_device {
>  	struct device dev;
> -	struct mdev_parent *parent;
>  	guid_t uuid;
>  	void *driver_data;
>  	struct list_head next;
> --
> 2.31.0


^ permalink raw reply	[flat|nested] 69+ messages in thread

* RE: [PATCH 11/18] vfio/mdev: Add mdev/mtype_get_type_group_id()
  2021-03-23 17:55 ` [PATCH 11/18] vfio/mdev: Add mdev/mtype_get_type_group_id() Jason Gunthorpe
  2021-03-23 19:23   ` Christoph Hellwig
@ 2021-03-26  5:52   ` Tian, Kevin
  2021-03-30 16:26   ` Cornelia Huck
  2 siblings, 0 replies; 69+ messages in thread
From: Tian, Kevin @ 2021-03-26  5:52 UTC (permalink / raw)
  To: Jason Gunthorpe, Alex Williamson, Cornelia Huck, kvm, Kirti Wankhede
  Cc: Raj, Ashok, Williams, Dan J, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

> From: Jason Gunthorpe <jgg@nvidia.com>
> Sent: Wednesday, March 24, 2021 1:55 AM
> 
> This returns the index in the supported_type_groups array that is
> associated with the mdev_type attached to the struct mdev_device or its
> containing struct kobject.
> 
> Each mdev_device can be spawned from exactly one mdev_type, which in
> turn
> originates from exactly one supported_type_group.
> 
> Drivers are using weird string calculations to try and get back to this
> index, providing a direct access to the index removes a bunch of wonky
> driver code.
> 
> mdev_type->group can be deleted as the group is obtained using the
> type_group_id.
> 
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>

Reviewed-by: Kevin Tian <kevin.tian@intel.com>

> ---
>  drivers/vfio/mdev/mdev_core.c    | 20 ++++++++++++++++++++
>  drivers/vfio/mdev/mdev_private.h |  2 +-
>  drivers/vfio/mdev/mdev_sysfs.c   | 15 +++++++++------
>  include/linux/mdev.h             |  3 +++
>  4 files changed, 33 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/vfio/mdev/mdev_core.c
> b/drivers/vfio/mdev/mdev_core.c
> index 493df3da451339..3ba5e9464b4d20 100644
> --- a/drivers/vfio/mdev/mdev_core.c
> +++ b/drivers/vfio/mdev/mdev_core.c
> @@ -33,6 +33,26 @@ struct device *mdev_parent_dev(struct mdev_device
> *mdev)
>  }
>  EXPORT_SYMBOL(mdev_parent_dev);
> 
> +/*
> + * Return the index in supported_type_groups that this mdev_device was
> created
> + * from.
> + */
> +unsigned int mdev_get_type_group_id(struct mdev_device *mdev)
> +{
> +	return mdev->type->type_group_id;
> +}
> +EXPORT_SYMBOL(mdev_get_type_group_id);
> +
> +/*
> + * Used in mdev_type_attribute sysfs functions to return the index in the
> + * supported_type_groups that the sysfs is called from.
> + */
> +unsigned int mtype_get_type_group_id(struct kobject *mtype_kobj)
> +{
> +	return container_of(mtype_kobj, struct mdev_type, kobj)-
> >type_group_id;
> +}
> +EXPORT_SYMBOL(mtype_get_type_group_id);
> +
>  /* Should be called holding parent_list_lock */
>  static struct mdev_parent *__find_parent_device(struct device *dev)
>  {
> diff --git a/drivers/vfio/mdev/mdev_private.h
> b/drivers/vfio/mdev/mdev_private.h
> index 10eccc35782c4d..a656cfe0346c33 100644
> --- a/drivers/vfio/mdev/mdev_private.h
> +++ b/drivers/vfio/mdev/mdev_private.h
> @@ -29,7 +29,7 @@ struct mdev_type {
>  	struct kobject *devices_kobj;
>  	struct mdev_parent *parent;
>  	struct list_head next;
> -	struct attribute_group *group;
> +	unsigned int type_group_id;
>  };
> 
>  #define to_mdev_type_attr(_attr)	\
> diff --git a/drivers/vfio/mdev/mdev_sysfs.c
> b/drivers/vfio/mdev/mdev_sysfs.c
> index d43775bd0ba340..91ecccdc2f2ec6 100644
> --- a/drivers/vfio/mdev/mdev_sysfs.c
> +++ b/drivers/vfio/mdev/mdev_sysfs.c
> @@ -92,9 +92,11 @@ static struct kobj_type mdev_type_ktype = {
>  };
> 
>  static struct mdev_type *add_mdev_supported_type(struct mdev_parent
> *parent,
> -						 struct attribute_group
> *group)
> +						 unsigned int type_group_id)
>  {
>  	struct mdev_type *type;
> +	struct attribute_group *group =
> +		parent->ops->supported_type_groups[type_group_id];
>  	int ret;
> 
>  	if (!group->name) {
> @@ -110,6 +112,7 @@ static struct mdev_type
> *add_mdev_supported_type(struct mdev_parent *parent,
>  	type->parent = parent;
>  	/* Pairs with the put in mdev_type_release() */
>  	mdev_get_parent(parent);
> +	type->type_group_id = type_group_id;
> 
>  	ret = kobject_init_and_add(&type->kobj, &mdev_type_ktype, NULL,
>  				   "%s-%s", dev_driver_string(parent->dev),
> @@ -135,8 +138,6 @@ static struct mdev_type
> *add_mdev_supported_type(struct mdev_parent *parent,
>  		ret = -ENOMEM;
>  		goto attrs_failed;
>  	}
> -
> -	type->group = group;
>  	return type;
> 
>  attrs_failed:
> @@ -151,8 +152,11 @@ static struct mdev_type
> *add_mdev_supported_type(struct mdev_parent *parent,
> 
>  static void remove_mdev_supported_type(struct mdev_type *type)
>  {
> +	struct attribute_group *group =
> +		type->parent->ops->supported_type_groups[type-
> >type_group_id];
> +
>  	sysfs_remove_files(&type->kobj,
> -			   (const struct attribute **)type->group->attrs);
> +			   (const struct attribute **)group->attrs);
>  	kobject_put(type->devices_kobj);
>  	sysfs_remove_file(&type->kobj, &mdev_type_attr_create.attr);
>  	kobject_del(&type->kobj);
> @@ -166,8 +170,7 @@ static int add_mdev_supported_type_groups(struct
> mdev_parent *parent)
>  	for (i = 0; parent->ops->supported_type_groups[i]; i++) {
>  		struct mdev_type *type;
> 
> -		type = add_mdev_supported_type(parent,
> -					parent->ops-
> >supported_type_groups[i]);
> +		type = add_mdev_supported_type(parent, i);
>  		if (IS_ERR(type)) {
>  			struct mdev_type *ltype, *tmp;
> 
> diff --git a/include/linux/mdev.h b/include/linux/mdev.h
> index fb582adda28a9b..41e91936522394 100644
> --- a/include/linux/mdev.h
> +++ b/include/linux/mdev.h
> @@ -46,6 +46,9 @@ static inline struct device
> *mdev_get_iommu_device(struct mdev_device *mdev)
>  	return mdev->iommu_device;
>  }
> 
> +unsigned int mdev_get_type_group_id(struct mdev_device *mdev);
> +unsigned int mtype_get_type_group_id(struct kobject *mtype_kobj);
> +
>  /**
>   * struct mdev_parent_ops - Structure to be registered for each parent
> device to
>   * register the device to mdev module.
> --
> 2.31.0


^ permalink raw reply	[flat|nested] 69+ messages in thread

* RE: [PATCH 17/18] vfio/mdev: Remove kobj from mdev_parent_ops->create()
  2021-03-23 17:55 ` [PATCH 17/18] vfio/mdev: Remove kobj from mdev_parent_ops->create() Jason Gunthorpe
  2021-03-23 19:29   ` Christoph Hellwig
@ 2021-03-26  6:11   ` Tian, Kevin
  2021-03-30 16:29   ` Cornelia Huck
  2 siblings, 0 replies; 69+ messages in thread
From: Tian, Kevin @ 2021-03-26  6:11 UTC (permalink / raw)
  To: Jason Gunthorpe, David Airlie, Tony Krowiak, Alex Williamson,
	Christian Borntraeger, Cornelia Huck, Daniel Vetter, dri-devel,
	Eric Farman, Harald Freudenberger, Vasily Gorbik, Heiko Carstens,
	intel-gfx, intel-gvt-dev, Jani Nikula, Joonas Lahtinen, kvm,
	Kirti Wankhede, linux-s390, Peter Oberparleiter, Halil Pasic,
	Pierre Morel, Vivi, Rodrigo, Vineeth Vijayan, Zhenyu Wang, Wang,
	Zhi A
  Cc: Max Gurtovoy, Raj, Ashok, Tarun Gupta, Williams, Dan J,
	Leon Romanovsky, Christoph Hellwig

> From: Jason Gunthorpe
> Sent: Wednesday, March 24, 2021 1:56 AM
> 
> The kobj here is a type-erased version of mdev_type, which is already
> stored in the struct mdev_device being passed in. It was only ever used to
> compute the type_group_id, which is now extracted directly from the mdev.
> 
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>

Reviewed-by: Kevin Tian <kevin.tian@intel.com>

> ---
>  drivers/gpu/drm/i915/gvt/kvmgt.c  | 2 +-
>  drivers/s390/cio/vfio_ccw_ops.c   | 2 +-
>  drivers/s390/crypto/vfio_ap_ops.c | 2 +-
>  drivers/vfio/mdev/mdev_core.c     | 2 +-
>  include/linux/mdev.h              | 3 +--
>  samples/vfio-mdev/mbochs.c        | 2 +-
>  samples/vfio-mdev/mdpy.c          | 2 +-
>  samples/vfio-mdev/mtty.c          | 2 +-
>  8 files changed, 8 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c
> b/drivers/gpu/drm/i915/gvt/kvmgt.c
> index 16e1e4a38aa1f6..6bf176e8426e63 100644
> --- a/drivers/gpu/drm/i915/gvt/kvmgt.c
> +++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
> @@ -689,7 +689,7 @@ static void kvmgt_put_vfio_device(void *vgpu)
>  	vfio_device_put(vdev->vfio_device);
>  }
> 
> -static int intel_vgpu_create(struct kobject *kobj, struct mdev_device *mdev)
> +static int intel_vgpu_create(struct mdev_device *mdev)
>  {
>  	struct intel_vgpu *vgpu = NULL;
>  	struct intel_vgpu_type *type;
> diff --git a/drivers/s390/cio/vfio_ccw_ops.c
> b/drivers/s390/cio/vfio_ccw_ops.c
> index 68106be4ba7a19..06a82ec136080c 100644
> --- a/drivers/s390/cio/vfio_ccw_ops.c
> +++ b/drivers/s390/cio/vfio_ccw_ops.c
> @@ -110,7 +110,7 @@ static struct attribute_group *mdev_type_groups[] =
> {
>  	NULL,
>  };
> 
> -static int vfio_ccw_mdev_create(struct kobject *kobj, struct mdev_device
> *mdev)
> +static int vfio_ccw_mdev_create(struct mdev_device *mdev)
>  {
>  	struct vfio_ccw_private *private =
>  		dev_get_drvdata(mdev_parent_dev(mdev));
> diff --git a/drivers/s390/crypto/vfio_ap_ops.c
> b/drivers/s390/crypto/vfio_ap_ops.c
> index 41fc2e4135fe18..6d75ed07bcd49d 100644
> --- a/drivers/s390/crypto/vfio_ap_ops.c
> +++ b/drivers/s390/crypto/vfio_ap_ops.c
> @@ -322,7 +322,7 @@ static void vfio_ap_matrix_init(struct ap_config_info
> *info,
>  	matrix->adm_max = info->apxa ? info->Nd : 15;
>  }
> 
> -static int vfio_ap_mdev_create(struct kobject *kobj, struct mdev_device
> *mdev)
> +static int vfio_ap_mdev_create(struct mdev_device *mdev)
>  {
>  	struct ap_matrix_mdev *matrix_mdev;
> 
> diff --git a/drivers/vfio/mdev/mdev_core.c
> b/drivers/vfio/mdev/mdev_core.c
> index 3ba5e9464b4d20..71455812cb84cf 100644
> --- a/drivers/vfio/mdev/mdev_core.c
> +++ b/drivers/vfio/mdev/mdev_core.c
> @@ -286,7 +286,7 @@ int mdev_device_create(struct mdev_type *type,
> const guid_t *uuid)
>  		goto mdev_fail;
>  	}
> 
> -	ret = parent->ops->create(&type->kobj, mdev);
> +	ret = parent->ops->create(mdev);
>  	if (ret)
>  		goto ops_create_fail;
> 
> diff --git a/include/linux/mdev.h b/include/linux/mdev.h
> index 41e91936522394..c3a800051d6146 100644
> --- a/include/linux/mdev.h
> +++ b/include/linux/mdev.h
> @@ -61,7 +61,6 @@ unsigned int mtype_get_type_group_id(struct kobject
> *mtype_kobj);
>   * @create:		Called to allocate basic resources in parent device's
>   *			driver for a particular mediated device. It is
>   *			mandatory to provide create ops.
> - *			@kobj: kobject of type for which 'create' is called.
>   *			@mdev: mdev_device structure on of mediated
> device
>   *			      that is being created
>   *			Returns integer: success (0) or error (< 0)
> @@ -107,7 +106,7 @@ struct mdev_parent_ops {
>  	const struct attribute_group **mdev_attr_groups;
>  	struct attribute_group **supported_type_groups;
> 
> -	int     (*create)(struct kobject *kobj, struct mdev_device *mdev);
> +	int     (*create)(struct mdev_device *mdev);
>  	int     (*remove)(struct mdev_device *mdev);
>  	int     (*open)(struct mdev_device *mdev);
>  	void    (*release)(struct mdev_device *mdev);
> diff --git a/samples/vfio-mdev/mbochs.c b/samples/vfio-mdev/mbochs.c
> index a1af30df10a2ee..ac4d0dc2490705 100644
> --- a/samples/vfio-mdev/mbochs.c
> +++ b/samples/vfio-mdev/mbochs.c
> @@ -506,7 +506,7 @@ static int mbochs_reset(struct mdev_device *mdev)
>  	return 0;
>  }
> 
> -static int mbochs_create(struct kobject *kobj, struct mdev_device *mdev)
> +static int mbochs_create(struct mdev_device *mdev)
>  {
>  	const struct mbochs_type *type =
>  		&mbochs_types[mdev_get_type_group_id(mdev)];
> diff --git a/samples/vfio-mdev/mdpy.c b/samples/vfio-mdev/mdpy.c
> index 08c15f9f06a880..da88fd7dd42329 100644
> --- a/samples/vfio-mdev/mdpy.c
> +++ b/samples/vfio-mdev/mdpy.c
> @@ -216,7 +216,7 @@ static int mdpy_reset(struct mdev_device *mdev)
>  	return 0;
>  }
> 
> -static int mdpy_create(struct kobject *kobj, struct mdev_device *mdev)
> +static int mdpy_create(struct mdev_device *mdev)
>  {
>  	const struct mdpy_type *type =
>  		&mdpy_types[mdev_get_type_group_id(mdev)];
> diff --git a/samples/vfio-mdev/mtty.c b/samples/vfio-mdev/mtty.c
> index 191a587a8d5ab1..f2e36c06ac6aa2 100644
> --- a/samples/vfio-mdev/mtty.c
> +++ b/samples/vfio-mdev/mtty.c
> @@ -708,7 +708,7 @@ static ssize_t mdev_access(struct mdev_device
> *mdev, u8 *buf, size_t count,
>  	return ret;
>  }
> 
> -static int mtty_create(struct kobject *kobj, struct mdev_device *mdev)
> +static int mtty_create(struct mdev_device *mdev)
>  {
>  	struct mdev_state *mdev_state;
>  	int nr_ports = mdev_get_type_group_id(mdev) + 1;
> --
> 2.31.0
> 
> _______________________________________________
> intel-gvt-dev mailing list
> intel-gvt-dev@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/intel-gvt-dev

^ permalink raw reply	[flat|nested] 69+ messages in thread

* RE: [PATCH 18/18] vfio/mdev: Correct the function signatures for the mdev_type_attributes
  2021-03-23 17:55 ` [PATCH 18/18] vfio/mdev: Correct the function signatures for the mdev_type_attributes Jason Gunthorpe
  2021-03-23 19:31   ` Christoph Hellwig
@ 2021-03-26  7:03   ` Tian, Kevin
  2021-03-30 16:35   ` Cornelia Huck
  2 siblings, 0 replies; 69+ messages in thread
From: Tian, Kevin @ 2021-03-26  7:03 UTC (permalink / raw)
  To: Jason Gunthorpe, David Airlie, Tony Krowiak, Alex Williamson,
	Christian Borntraeger, Cornelia Huck, Daniel Vetter, dri-devel,
	Eric Farman, Harald Freudenberger, Vasily Gorbik, Heiko Carstens,
	intel-gfx, intel-gvt-dev, Jani Nikula, Joonas Lahtinen, kvm,
	Kirti Wankhede, linux-s390, Peter Oberparleiter, Halil Pasic,
	Pierre Morel, Vivi, Rodrigo, Vineeth Vijayan, Zhenyu Wang, Wang,
	Zhi A
  Cc: Raj, Ashok, Williams, Dan J, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

> From: Jason Gunthorpe <jgg@nvidia.com>
> Sent: Wednesday, March 24, 2021 1:56 AM
> 
> The driver core standard is to pass in the properly typed object, the
> properly typed attribute and the buffer data. It stems from the root
> kobject method:
> 
>   ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,..)
> 
> Each subclass of kobject should provide their own function with the same
> signature but more specific types, eg struct device uses:
> 
>   ssize_t (*show)(struct device *dev, struct device_attribute *attr,..)
> 
> In this case the existing signature is:
> 
>   ssize_t (*show)(struct kobject *kobj, struct device *dev,..)
> 
> Where kobj is a 'struct mdev_type *' and dev is 'mdev_type->parent->dev'.
> 
> Change the mdev_type related sysfs attribute functions to:
> 
>   ssize_t (*show)(struct mdev_type *mtype, struct mdev_type_attribute
> *attr,..)
> 
> In order to restore type safety and match the driver core standard
> 
> There are no current users of 'attr', but if it is ever needed it would be
> hard to add in retroactively, so do it now.
> 
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>

Reviewed-by: Kevin Tian <kevin.tian@intel.com>

> ---
>  drivers/gpu/drm/i915/gvt/gvt.c    | 21 +++++++++++----------
>  drivers/s390/cio/vfio_ccw_ops.c   | 15 +++++++++------
>  drivers/s390/crypto/vfio_ap_ops.c | 12 +++++++-----
>  drivers/vfio/mdev/mdev_core.c     | 14 ++++++++++++--
>  drivers/vfio/mdev/mdev_sysfs.c    | 11 ++++++-----
>  include/linux/mdev.h              | 11 +++++++----
>  samples/vfio-mdev/mbochs.c        | 26 +++++++++++++++-----------
>  samples/vfio-mdev/mdpy.c          | 24 ++++++++++++++----------
>  samples/vfio-mdev/mtty.c          | 18 +++++++++---------
>  9 files changed, 90 insertions(+), 62 deletions(-)
> 
> diff --git a/drivers/gpu/drm/i915/gvt/gvt.c b/drivers/gpu/drm/i915/gvt/gvt.c
> index 4b47a18e9dfa0f..3703814a669b46 100644
> --- a/drivers/gpu/drm/i915/gvt/gvt.c
> +++ b/drivers/gpu/drm/i915/gvt/gvt.c
> @@ -54,14 +54,15 @@ intel_gvt_find_vgpu_type(struct intel_gvt *gvt,
> unsigned int type_group_id)
>  	return &gvt->types[type_group_id];
>  }
> 
> -static ssize_t available_instances_show(struct kobject *kobj,
> -					struct device *dev, char *buf)
> +static ssize_t available_instances_show(struct mdev_type *mtype,
> +					struct mdev_type_attribute *attr,
> +					char *buf)
>  {
>  	struct intel_vgpu_type *type;
>  	unsigned int num = 0;
> -	void *gvt = kdev_to_i915(dev)->gvt;
> +	void *gvt = kdev_to_i915(mtype_get_parent_dev(mtype))->gvt;
> 
> -	type = intel_gvt_find_vgpu_type(gvt,
> mtype_get_type_group_id(kobj));
> +	type = intel_gvt_find_vgpu_type(gvt,
> mtype_get_type_group_id(mtype));
>  	if (!type)
>  		num = 0;
>  	else
> @@ -70,19 +71,19 @@ static ssize_t available_instances_show(struct kobject
> *kobj,
>  	return sprintf(buf, "%u\n", num);
>  }
> 
> -static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
> -		char *buf)
> +static ssize_t device_api_show(struct mdev_type *mtype,
> +			       struct mdev_type_attribute *attr, char *buf)
>  {
>  	return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
>  }
> 
> -static ssize_t description_show(struct kobject *kobj, struct device *dev,
> -		char *buf)
> +static ssize_t description_show(struct mdev_type *mtype,
> +				struct mdev_type_attribute *attr, char *buf)
>  {
>  	struct intel_vgpu_type *type;
> -	void *gvt = kdev_to_i915(dev)->gvt;
> +	void *gvt = kdev_to_i915(mtype_get_parent_dev(mtype))->gvt;
> 
> -	type = intel_gvt_find_vgpu_type(gvt,
> mtype_get_type_group_id(kobj));
> +	type = intel_gvt_find_vgpu_type(gvt,
> mtype_get_type_group_id(mtype));
>  	if (!type)
>  		return 0;
> 
> diff --git a/drivers/s390/cio/vfio_ccw_ops.c
> b/drivers/s390/cio/vfio_ccw_ops.c
> index 06a82ec136080c..74fe21eceb66cc 100644
> --- a/drivers/s390/cio/vfio_ccw_ops.c
> +++ b/drivers/s390/cio/vfio_ccw_ops.c
> @@ -71,23 +71,26 @@ static int vfio_ccw_mdev_notifier(struct
> notifier_block *nb,
>  	return NOTIFY_DONE;
>  }
> 
> -static ssize_t name_show(struct kobject *kobj, struct device *dev, char *buf)
> +static ssize_t name_show(struct mdev_type *mtype,
> +			 struct mdev_type_attribute *attr, char *buf)
>  {
>  	return sprintf(buf, "I/O subchannel (Non-QDIO)\n");
>  }
>  static MDEV_TYPE_ATTR_RO(name);
> 
> -static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
> -			       char *buf)
> +static ssize_t device_api_show(struct mdev_type *mtype,
> +			       struct mdev_type_attribute *attr, char *buf)
>  {
>  	return sprintf(buf, "%s\n", VFIO_DEVICE_API_CCW_STRING);
>  }
>  static MDEV_TYPE_ATTR_RO(device_api);
> 
> -static ssize_t available_instances_show(struct kobject *kobj,
> -					struct device *dev, char *buf)
> +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(dev);
> +	struct vfio_ccw_private *private =
> +		dev_get_drvdata(mtype_get_parent_dev(mtype));
> 
>  	return sprintf(buf, "%d\n", atomic_read(&private->avail));
>  }
> diff --git a/drivers/s390/crypto/vfio_ap_ops.c
> b/drivers/s390/crypto/vfio_ap_ops.c
> index 6d75ed07bcd49d..cdc5edb0074690 100644
> --- a/drivers/s390/crypto/vfio_ap_ops.c
> +++ b/drivers/s390/crypto/vfio_ap_ops.c
> @@ -366,15 +366,17 @@ static int vfio_ap_mdev_remove(struct
> mdev_device *mdev)
>  	return 0;
>  }
> 
> -static ssize_t name_show(struct kobject *kobj, struct device *dev, char *buf)
> +static ssize_t name_show(struct mdev_type *mtype,
> +			 struct mdev_type_attribute *attr, char *buf)
>  {
>  	return sprintf(buf, "%s\n", VFIO_AP_MDEV_NAME_HWVIRT);
>  }
> 
>  static MDEV_TYPE_ATTR_RO(name);
> 
> -static ssize_t available_instances_show(struct kobject *kobj,
> -					struct device *dev, char *buf)
> +static ssize_t available_instances_show(struct mdev_type *mtype,
> +					struct mdev_type_attribute *attr,
> +					char *buf)
>  {
>  	return sprintf(buf, "%d\n",
>  		       atomic_read(&matrix_dev->available_instances));
> @@ -382,8 +384,8 @@ static ssize_t available_instances_show(struct kobject
> *kobj,
> 
>  static MDEV_TYPE_ATTR_RO(available_instances);
> 
> -static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
> -			       char *buf)
> +static ssize_t device_api_show(struct mdev_type *mtype,
> +			       struct mdev_type_attribute *attr, char *buf)
>  {
>  	return sprintf(buf, "%s\n", VFIO_DEVICE_API_AP_STRING);
>  }
> diff --git a/drivers/vfio/mdev/mdev_core.c
> b/drivers/vfio/mdev/mdev_core.c
> index 71455812cb84cf..9ef1d5bed8069f 100644
> --- a/drivers/vfio/mdev/mdev_core.c
> +++ b/drivers/vfio/mdev/mdev_core.c
> @@ -47,12 +47,22 @@ EXPORT_SYMBOL(mdev_get_type_group_id);
>   * Used in mdev_type_attribute sysfs functions to return the index in the
>   * supported_type_groups that the sysfs is called from.
>   */
> -unsigned int mtype_get_type_group_id(struct kobject *mtype_kobj)
> +unsigned int mtype_get_type_group_id(struct mdev_type *mtype)
>  {
> -	return container_of(mtype_kobj, struct mdev_type, kobj)-
> >type_group_id;
> +	return mtype->type_group_id;
>  }
>  EXPORT_SYMBOL(mtype_get_type_group_id);
> 
> +/*
> + * Used in mdev_type_attribute sysfs functions to return the parent struct
> + * device
> + */
> +struct device *mtype_get_parent_dev(struct mdev_type *mtype)
> +{
> +	return mtype->parent->dev;
> +}
> +EXPORT_SYMBOL(mtype_get_parent_dev);
> +
>  /* Should be called holding parent_list_lock */
>  static struct mdev_parent *__find_parent_device(struct device *dev)
>  {
> diff --git a/drivers/vfio/mdev/mdev_sysfs.c
> b/drivers/vfio/mdev/mdev_sysfs.c
> index 91ecccdc2f2ec6..9b0f1a8757a0df 100644
> --- a/drivers/vfio/mdev/mdev_sysfs.c
> +++ b/drivers/vfio/mdev/mdev_sysfs.c
> @@ -26,7 +26,7 @@ static ssize_t mdev_type_attr_show(struct kobject
> *kobj,
>  	ssize_t ret = -EIO;
> 
>  	if (attr->show)
> -		ret = attr->show(kobj, type->parent->dev, buf);
> +		ret = attr->show(type, attr, buf);
>  	return ret;
>  }
> 
> @@ -39,7 +39,7 @@ static ssize_t mdev_type_attr_store(struct kobject *kobj,
>  	ssize_t ret = -EIO;
> 
>  	if (attr->store)
> -		ret = attr->store(&type->kobj, type->parent->dev, buf, count);
> +		ret = attr->store(type, attr, buf, count);
>  	return ret;
>  }
> 
> @@ -48,8 +48,9 @@ static const struct sysfs_ops mdev_type_sysfs_ops = {
>  	.store = mdev_type_attr_store,
>  };
> 
> -static ssize_t create_store(struct kobject *kobj, struct device *dev,
> -			    const char *buf, size_t count)
> +static ssize_t create_store(struct mdev_type *mtype,
> +			    struct mdev_type_attribute *attr, const char *buf,
> +			    size_t count)
>  {
>  	char *str;
>  	guid_t uuid;
> @@ -67,7 +68,7 @@ static ssize_t create_store(struct kobject *kobj, struct
> device *dev,
>  	if (ret)
>  		return ret;
> 
> -	ret = mdev_device_create(to_mdev_type(kobj), &uuid);
> +	ret = mdev_device_create(mtype, &uuid);
>  	if (ret)
>  		return ret;
> 
> diff --git a/include/linux/mdev.h b/include/linux/mdev.h
> index c3a800051d6146..1fb34ea394ad46 100644
> --- a/include/linux/mdev.h
> +++ b/include/linux/mdev.h
> @@ -47,7 +47,8 @@ static inline struct device
> *mdev_get_iommu_device(struct mdev_device *mdev)
>  }
> 
>  unsigned int mdev_get_type_group_id(struct mdev_device *mdev);
> -unsigned int mtype_get_type_group_id(struct kobject *mtype_kobj);
> +unsigned int mtype_get_type_group_id(struct mdev_type *mtype);
> +struct device *mtype_get_parent_dev(struct mdev_type *mtype);
> 
>  /**
>   * struct mdev_parent_ops - Structure to be registered for each parent
> device to
> @@ -123,9 +124,11 @@ struct mdev_parent_ops {
>  /* interface for exporting mdev supported type attributes */
>  struct mdev_type_attribute {
>  	struct attribute attr;
> -	ssize_t (*show)(struct kobject *kobj, struct device *dev, char *buf);
> -	ssize_t (*store)(struct kobject *kobj, struct device *dev,
> -			 const char *buf, size_t count);
> +	ssize_t (*show)(struct mdev_type *mtype,
> +			struct mdev_type_attribute *attr, char *buf);
> +	ssize_t (*store)(struct mdev_type *mtype,
> +			 struct mdev_type_attribute *attr, const char *buf,
> +			 size_t count);
>  };
> 
>  #define MDEV_TYPE_ATTR(_name, _mode, _show, _store)		\
> diff --git a/samples/vfio-mdev/mbochs.c b/samples/vfio-mdev/mbochs.c
> index ac4d0dc2490705..861c76914e7639 100644
> --- a/samples/vfio-mdev/mbochs.c
> +++ b/samples/vfio-mdev/mbochs.c
> @@ -1330,37 +1330,41 @@ static const struct attribute_group
> *mdev_dev_groups[] = {
>  	NULL,
>  };
> 
> -static ssize_t
> -name_show(struct kobject *kobj, struct device *dev, char *buf)
> +static ssize_t name_show(struct mdev_type *mtype,
> +			 struct mdev_type_attribute *attr, char *buf)
>  {
> -	return sprintf(buf, "%s\n", kobj->name);
> +	const struct mbochs_type *type =
> +		&mbochs_types[mtype_get_type_group_id(mtype)];
> +
> +	return sprintf(buf, "%s\n", type->name);
>  }
>  static MDEV_TYPE_ATTR_RO(name);
> 
> -static ssize_t
> -description_show(struct kobject *kobj, struct device *dev, char *buf)
> +static ssize_t description_show(struct mdev_type *mtype,
> +				struct mdev_type_attribute *attr, char *buf)
>  {
>  	const struct mbochs_type *type =
> -		&mbochs_types[mtype_get_type_group_id(kobj)];
> +		&mbochs_types[mtype_get_type_group_id(mtype)];
> 
>  	return sprintf(buf, "virtual display, %d MB video memory\n",
>  		       type ? type->mbytes  : 0);
>  }
>  static MDEV_TYPE_ATTR_RO(description);
> 
> -static ssize_t
> -available_instances_show(struct kobject *kobj, struct device *dev, char *buf)
> +static ssize_t available_instances_show(struct mdev_type *mtype,
> +					struct mdev_type_attribute *attr,
> +					char *buf)
>  {
>  	const struct mbochs_type *type =
> -		&mbochs_types[mtype_get_type_group_id(kobj)];
> +		&mbochs_types[mtype_get_type_group_id(mtype)];
>  	int count = (max_mbytes - mbochs_used_mbytes) / type->mbytes;
> 
>  	return sprintf(buf, "%d\n", count);
>  }
>  static MDEV_TYPE_ATTR_RO(available_instances);
> 
> -static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
> -			       char *buf)
> +static ssize_t device_api_show(struct mdev_type *mtype,
> +			       struct mdev_type_attribute *attr, char *buf)
>  {
>  	return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
>  }
> diff --git a/samples/vfio-mdev/mdpy.c b/samples/vfio-mdev/mdpy.c
> index da88fd7dd42329..885b88ea20e234 100644
> --- a/samples/vfio-mdev/mdpy.c
> +++ b/samples/vfio-mdev/mdpy.c
> @@ -652,18 +652,21 @@ static const struct attribute_group
> *mdev_dev_groups[] = {
>  	NULL,
>  };
> 
> -static ssize_t
> -name_show(struct kobject *kobj, struct device *dev, char *buf)
> +static ssize_t name_show(struct mdev_type *mtype,
> +			 struct mdev_type_attribute *attr, char *buf)
>  {
> -	return sprintf(buf, "%s\n", kobj->name);
> +	const struct mdpy_type *type =
> +		&mdpy_types[mtype_get_type_group_id(mtype)];
> +
> +	return sprintf(buf, "%s\n", type->name);
>  }
>  static MDEV_TYPE_ATTR_RO(name);
> 
> -static ssize_t
> -description_show(struct kobject *kobj, struct device *dev, char *buf)
> +static ssize_t description_show(struct mdev_type *mtype,
> +				struct mdev_type_attribute *attr, char *buf)
>  {
>  	const struct mdpy_type *type =
> -		&mdpy_types[mtype_get_type_group_id(kobj)];
> +		&mdpy_types[mtype_get_type_group_id(mtype)];
> 
>  	return sprintf(buf, "virtual display, %dx%d framebuffer\n",
>  		       type ? type->width  : 0,
> @@ -671,15 +674,16 @@ description_show(struct kobject *kobj, struct
> device *dev, char *buf)
>  }
>  static MDEV_TYPE_ATTR_RO(description);
> 
> -static ssize_t
> -available_instances_show(struct kobject *kobj, struct device *dev, char *buf)
> +static ssize_t available_instances_show(struct mdev_type *mtype,
> +					struct mdev_type_attribute *attr,
> +					char *buf)
>  {
>  	return sprintf(buf, "%d\n", max_devices - mdpy_count);
>  }
>  static MDEV_TYPE_ATTR_RO(available_instances);
> 
> -static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
> -			       char *buf)
> +static ssize_t device_api_show(struct mdev_type *mtype,
> +			       struct mdev_type_attribute *attr, char *buf)
>  {
>  	return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
>  }
> diff --git a/samples/vfio-mdev/mtty.c b/samples/vfio-mdev/mtty.c
> index f2e36c06ac6aa2..b9b24be4abdab7 100644
> --- a/samples/vfio-mdev/mtty.c
> +++ b/samples/vfio-mdev/mtty.c
> @@ -1292,23 +1292,24 @@ static const struct attribute_group
> *mdev_dev_groups[] = {
>  	NULL,
>  };
> 
> -static ssize_t
> -name_show(struct kobject *kobj, struct device *dev, char *buf)
> +static ssize_t name_show(struct mdev_type *mtype,
> +			 struct mdev_type_attribute *attr, char *buf)
>  {
>  	static const char *name_str[2] = { "Single port serial",
>  					   "Dual port serial" };
> 
>  	return sysfs_emit(buf, "%s\n",
> -			  name_str[mtype_get_type_group_id(kobj)]);
> +			  name_str[mtype_get_type_group_id(mtype)]);
>  }
> 
>  static MDEV_TYPE_ATTR_RO(name);
> 
> -static ssize_t
> -available_instances_show(struct kobject *kobj, struct device *dev, char *buf)
> +static ssize_t available_instances_show(struct mdev_type *mtype,
> +					struct mdev_type_attribute *attr,
> +					char *buf)
>  {
>  	struct mdev_state *mds;
> -	unsigned int ports = mtype_get_type_group_id(kobj) + 1;
> +	unsigned int ports = mtype_get_type_group_id(mtype) + 1;
>  	int used = 0;
> 
>  	list_for_each_entry(mds, &mdev_devices_list, next)
> @@ -1319,9 +1320,8 @@ available_instances_show(struct kobject *kobj,
> struct device *dev, char *buf)
> 
>  static MDEV_TYPE_ATTR_RO(available_instances);
> 
> -
> -static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
> -			       char *buf)
> +static ssize_t device_api_show(struct mdev_type *mtype,
> +			       struct mdev_type_attribute *attr, char *buf)
>  {
>  	return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
>  }
> --
> 2.31.0


^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 10/18] vfio/mdev: Remove duplicate storage of parent in mdev_device
  2021-03-26  3:53   ` Tian, Kevin
@ 2021-03-26 11:53     ` Jason Gunthorpe
  2021-03-30 16:14       ` Cornelia Huck
  0 siblings, 1 reply; 69+ messages in thread
From: Jason Gunthorpe @ 2021-03-26 11:53 UTC (permalink / raw)
  To: Tian, Kevin
  Cc: Alex Williamson, Cornelia Huck, kvm, Kirti Wankhede, Raj, Ashok,
	Williams, Dan J, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

On Fri, Mar 26, 2021 at 03:53:10AM +0000, Tian, Kevin wrote:

> > @@ -58,12 +58,11 @@ void mdev_release_parent(struct kref *kref)
> >  /* Caller must hold parent unreg_sem read or write lock */
> >  static void mdev_device_remove_common(struct mdev_device *mdev)
> >  {
> > -	struct mdev_parent *parent;
> > +	struct mdev_parent *parent = mdev->type->parent;
> 
> What about having a wrapper here, like mdev_parent_dev? For
> readability it's not necessary to show that the parent is indirectly
> retrieved through mdev_type.

I think that is too much wrappering, we only have three usages of the
mdev->type->parent sequence and two are already single line inlines.

> >  	int ret;
> > 
> >  	mdev_remove_sysfs_files(mdev);
> >  	device_del(&mdev->dev);
> > -	parent = mdev->parent;
> >  	lockdep_assert_held(&parent->unreg_sem);
> >  	ret = parent->ops->remove(mdev);
> >  	if (ret)
> > @@ -212,7 +211,7 @@ static void mdev_device_release(struct device *dev)
> >  	struct mdev_device *mdev = to_mdev_device(dev);
> > 
> >  	/* Pairs with the get in mdev_device_create() */
> > -	mdev_put_parent(mdev->parent);
> > +	kobject_put(&mdev->type->kobj);
> 
> Maybe keep mdev_get/put_parent and change them to accept "struct
> mdev_device *" parameter like other places.

We do keep mdev_get/put_parent() they manipulate the refcount inside
the parent and this is only done in side the type logic now.

This is get/put of the refcount on the type, and this is the only
place that does it, so I don't see a reason for more wrappers.

Jason

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 03/18] vfio/mdev: Simplify driver registration
  2021-03-23 19:14   ` Christoph Hellwig
@ 2021-03-26 12:10     ` Jason Gunthorpe
  2021-03-26 12:55       ` Christoph Hellwig
  0 siblings, 1 reply; 69+ messages in thread
From: Jason Gunthorpe @ 2021-03-26 12:10 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Alex Williamson, Cornelia Huck, Jonathan Corbet, kvm,
	Kirti Wankhede, linux-doc, Raj, Ashok, Dan Williams,
	Leon Romanovsky, Max Gurtovoy, Tarun Gupta

On Tue, Mar 23, 2021 at 08:14:15PM +0100, Christoph Hellwig wrote:
> >  static struct mdev_driver vfio_mdev_driver = {
> > +	.driver = {
> > +		.name = "vfio_mdev",
> > +		.owner = THIS_MODULE,
> > +		.mod_name = KBUILD_MODNAME,
> > +	},
> 
> What is the mod_name initialization for?  

It is usually hidden and works like this:

 /* pci_register_driver() must be a macro so KBUILD_MODNAME can be expanded */
 #define pci_register_driver(driver)		\
 	__pci_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
 
 int __pci_register_driver(struct pci_driver *drv, struct module *owner,
 			  const char *mod_name)
 {
	drv->driver.owner = owner;
  	drv->driver.mod_name = mod_name;

> I've not really seen that in anywere else, and the only user seems
> to be module_add_driver for a rather odd case we shouldn't hit here.

vfio_mdev could be compiled built in? 

AFAICT it handles the case where THIS_MODULE==NULL so we still need to
create sysfs links to the built in module.

If it is left NULL then a few sysfs files go missing for the built in
mode but no harm done?

I think it is correct to have it

Thanks,
Jason

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 03/18] vfio/mdev: Simplify driver registration
  2021-03-26 12:10     ` Jason Gunthorpe
@ 2021-03-26 12:55       ` Christoph Hellwig
  0 siblings, 0 replies; 69+ messages in thread
From: Christoph Hellwig @ 2021-03-26 12:55 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Christoph Hellwig, Alex Williamson, Cornelia Huck,
	Jonathan Corbet, kvm, Kirti Wankhede, linux-doc, Raj, Ashok,
	Dan Williams, Leon Romanovsky, Max Gurtovoy, Tarun Gupta

On Fri, Mar 26, 2021 at 09:10:48AM -0300, Jason Gunthorpe wrote:
> It is usually hidden and works like this:
> 
>  /* pci_register_driver() must be a macro so KBUILD_MODNAME can be expanded */
>  #define pci_register_driver(driver)		\
>  	__pci_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
>  
>  int __pci_register_driver(struct pci_driver *drv, struct module *owner,
>  			  const char *mod_name)
>  {
> 	drv->driver.owner = owner;
>   	drv->driver.mod_name = mod_name;

Indeed, there seem to be about two handful of instance of that.

> > I've not really seen that in anywere else, and the only user seems
> > to be module_add_driver for a rather odd case we shouldn't hit here.
> 
> vfio_mdev could be compiled built in? 
> 
> AFAICT it handles the case where THIS_MODULE==NULL so we still need to
> create sysfs links to the built in module.
> 
> If it is left NULL then a few sysfs files go missing for the built in
> mode but no harm done?

Yes, it seems to be needed for a few driver-specific files.  So it looks
ok, even if rather unexpected.

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 05/18] vfio/mdev: Do not allow a mdev_type to have a NULL parent pointer
  2021-03-23 17:55 ` [PATCH 05/18] vfio/mdev: Do not allow a mdev_type to have a NULL parent pointer Jason Gunthorpe
  2021-03-23 19:15   ` Christoph Hellwig
  2021-03-26  2:38   ` Tian, Kevin
@ 2021-03-29  8:42   ` Max Gurtovoy
  2021-03-30 15:34   ` Cornelia Huck
  3 siblings, 0 replies; 69+ messages in thread
From: Max Gurtovoy @ 2021-03-29  8:42 UTC (permalink / raw)
  To: Jason Gunthorpe, Cornelia Huck, kvm
  Cc: Alex Williamson, Raj, Ashok, Dong Jia Shi, Neo Jia, Dan Williams,
	Christoph Hellwig, Jike Song, Kirti Wankhede, Leon Romanovsky,
	Tarun Gupta


On 3/23/2021 7:55 PM, Jason Gunthorpe wrote:
> There is a small race where the parent is NULL even though the kobj has
> already been made visible in sysfs.
>
> For instance the attribute_group is made visible in sysfs_create_files()
> and the mdev_type_attr_show() does:
>
>      ret = attr->show(kobj, type->parent->dev, buf);
>
> Which will crash on NULL parent. Move the parent setup to before the type
> pointer leaves the stack frame.
>
> Fixes: 7b96953bc640 ("vfio: Mediated device Core driver")
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> ---

Looks good,

Reviewed-by: Max Gurtovoy <mgurtovoy@nvidia.com>


^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 09/18] vfio/mdev: Add missing error handling to dev_set_name()
  2021-03-23 17:55 ` [PATCH 09/18] vfio/mdev: Add missing error handling to dev_set_name() Jason Gunthorpe
  2021-03-23 19:21   ` Christoph Hellwig
  2021-03-26  3:35   ` Tian, Kevin
@ 2021-03-29  8:50   ` Max Gurtovoy
  2021-03-30 15:53   ` Cornelia Huck
  3 siblings, 0 replies; 69+ messages in thread
From: Max Gurtovoy @ 2021-03-29  8:50 UTC (permalink / raw)
  To: Jason Gunthorpe, Cornelia Huck, kvm
  Cc: Alex Williamson, Raj, Ashok, Dong Jia Shi, Neo Jia, Dan Williams,
	Christoph Hellwig, Jike Song, Kirti Wankhede, Leon Romanovsky,
	Tarun Gupta


On 3/23/2021 7:55 PM, Jason Gunthorpe wrote:
> This can fail, and seems to be a popular target for syzkaller error
> injection. Check the error return and unwind with put_device().
>
> Fixes: 7b96953bc640 ("vfio: Mediated device Core driver")
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> ---
>   drivers/vfio/mdev/mdev_core.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)

Looks good,

Reviewed-by: Max Gurtovoy <mgurtovoy@nvidia.com>


^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 02/18] vfio/mdev: Add missing typesafety around mdev_device
  2021-03-23 17:55 ` [PATCH 02/18] vfio/mdev: Add missing typesafety around mdev_device Jason Gunthorpe
  2021-03-26  2:04   ` Tian, Kevin
@ 2021-03-30 15:24   ` Cornelia Huck
  1 sibling, 0 replies; 69+ messages in thread
From: Cornelia Huck @ 2021-03-30 15:24 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Alex Williamson, Jonathan Corbet, kvm, Kirti Wankhede, linux-doc,
	Raj, Ashok, Dan Williams, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

On Tue, 23 Mar 2021 14:55:19 -0300
Jason Gunthorpe <jgg@nvidia.com> wrote:

> The mdev API should accept and pass a 'struct mdev_device *' in all
> places, not pass a 'struct device *' and cast it internally with
> to_mdev_device(). Particularly in its struct mdev_driver functions, the
> whole point of a bus's struct device_driver wrapper is to provide type
> safety compared to the default struct device_driver.
> 
> Further, the driver core standard is for bus drivers to expose their
> device structure in their public headers that can be used with
> container_of() inlines and '&foo->dev' to go between the class levels, and
> '&foo->dev' to be used with dev_err/etc driver core helper functions. Move
> 'struct mdev_device' to mdev.h
> 
> Once done this allows moving some one instruction exported functions to
> static inlines, which in turns allows removing one of the two grotesque
> symbol_get()'s related to mdev in the core code.
> 
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> ---
>  .../driver-api/vfio-mediated-device.rst       |  4 +-
>  drivers/vfio/mdev/mdev_core.c                 | 64 ++-----------------
>  drivers/vfio/mdev/mdev_driver.c               |  4 +-
>  drivers/vfio/mdev/mdev_private.h              | 23 +------
>  drivers/vfio/mdev/mdev_sysfs.c                | 26 ++++----
>  drivers/vfio/mdev/vfio_mdev.c                 |  7 +-
>  drivers/vfio/vfio_iommu_type1.c               | 25 ++------
>  include/linux/mdev.h                          | 58 +++++++++++++----
>  8 files changed, 83 insertions(+), 128 deletions(-)

Reviewed-by: Cornelia Huck <cohuck@redhat.com>


^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 03/18] vfio/mdev: Simplify driver registration
  2021-03-23 17:55 ` [PATCH 03/18] vfio/mdev: Simplify driver registration Jason Gunthorpe
  2021-03-23 19:14   ` Christoph Hellwig
  2021-03-26  2:17   ` Tian, Kevin
@ 2021-03-30 15:28   ` Cornelia Huck
  2 siblings, 0 replies; 69+ messages in thread
From: Cornelia Huck @ 2021-03-30 15:28 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Alex Williamson, Jonathan Corbet, kvm, Kirti Wankhede, linux-doc,
	Raj, Ashok, Dan Williams, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

On Tue, 23 Mar 2021 14:55:20 -0300
Jason Gunthorpe <jgg@nvidia.com> wrote:

> This is only done once, we don't need to generate code to initialize a
> structure stored in the ELF .data segment. Fill in the three required
> .driver members directly instead of copying data into them during
> mdev_register_driver().
> 
> Further the to_mdev_driver() function doesn't belong in a public header,
> just inline it into the two places that need it. Finally, we can now
> clearly see that 'drv' derived from dev->driver cannot be NULL, firstly
> because the driver core forbids it, and secondly because NULL won't pass
> through the container_of(). Remove the dead code.
> 
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> ---
>  Documentation/driver-api/vfio-mediated-device.rst |  5 +----
>  drivers/vfio/mdev/mdev_driver.c                   | 15 +++++++--------
>  drivers/vfio/mdev/vfio_mdev.c                     |  8 ++++++--
>  include/linux/mdev.h                              |  6 +-----
>  4 files changed, 15 insertions(+), 19 deletions(-)

Reviewed-by: Cornelia Huck <cohuck@redhat.com>


^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 04/18] vfio/mdev: Use struct mdev_type in struct mdev_device
  2021-03-23 17:55 ` [PATCH 04/18] vfio/mdev: Use struct mdev_type in struct mdev_device Jason Gunthorpe
  2021-03-23 19:15   ` Christoph Hellwig
  2021-03-26  2:29   ` Tian, Kevin
@ 2021-03-30 15:31   ` Cornelia Huck
  2 siblings, 0 replies; 69+ messages in thread
From: Cornelia Huck @ 2021-03-30 15:31 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Alex Williamson, kvm, Kirti Wankhede, Raj, Ashok, Dan Williams,
	Christoph Hellwig, Leon Romanovsky, Max Gurtovoy, Tarun Gupta

On Tue, 23 Mar 2021 14:55:21 -0300
Jason Gunthorpe <jgg@nvidia.com> wrote:

> The kobj pointer in mdev_device is actually pointing at a struct
> mdev_type. Use the proper type so things are understandable.
> 
> There are a number of places that are confused and passing both the mdev
> and the mtype as function arguments, fix these to derive the mtype
> directly from the mdev to remove the redundancy.
> 
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> ---
>  drivers/vfio/mdev/mdev_core.c    | 16 ++++++----------
>  drivers/vfio/mdev/mdev_private.h |  7 +++----
>  drivers/vfio/mdev/mdev_sysfs.c   | 15 ++++++++-------
>  include/linux/mdev.h             |  4 +++-
>  4 files changed, 20 insertions(+), 22 deletions(-)

Reviewed-by: Cornelia Huck <cohuck@redhat.com>


^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 05/18] vfio/mdev: Do not allow a mdev_type to have a NULL parent pointer
  2021-03-23 17:55 ` [PATCH 05/18] vfio/mdev: Do not allow a mdev_type to have a NULL parent pointer Jason Gunthorpe
                     ` (2 preceding siblings ...)
  2021-03-29  8:42   ` Max Gurtovoy
@ 2021-03-30 15:34   ` Cornelia Huck
  3 siblings, 0 replies; 69+ messages in thread
From: Cornelia Huck @ 2021-03-30 15:34 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: kvm, Alex Williamson, Raj, Ashok, Dong Jia Shi, Neo Jia,
	Dan Williams, Christoph Hellwig, Jike Song, Kirti Wankhede,
	Leon Romanovsky, Max Gurtovoy, Tarun Gupta

On Tue, 23 Mar 2021 14:55:22 -0300
Jason Gunthorpe <jgg@nvidia.com> wrote:

> There is a small race where the parent is NULL even though the kobj has
> already been made visible in sysfs.
> 
> For instance the attribute_group is made visible in sysfs_create_files()
> and the mdev_type_attr_show() does:
> 
>     ret = attr->show(kobj, type->parent->dev, buf);
> 
> Which will crash on NULL parent. Move the parent setup to before the type
> pointer leaves the stack frame.
> 
> Fixes: 7b96953bc640 ("vfio: Mediated device Core driver")
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> ---
>  drivers/vfio/mdev/mdev_sysfs.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

Reviewed-by: Cornelia Huck <cohuck@redhat.com>


^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 06/18] vfio/mdev: Expose mdev_get/put_parent to mdev_private.h
  2021-03-23 17:55 ` [PATCH 06/18] vfio/mdev: Expose mdev_get/put_parent to mdev_private.h Jason Gunthorpe
  2021-03-23 19:16   ` Christoph Hellwig
  2021-03-26  2:47   ` Tian, Kevin
@ 2021-03-30 15:37   ` Cornelia Huck
  2 siblings, 0 replies; 69+ messages in thread
From: Cornelia Huck @ 2021-03-30 15:37 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Alex Williamson, kvm, Kirti Wankhede, Raj, Ashok, Dan Williams,
	Christoph Hellwig, Leon Romanovsky, Max Gurtovoy, Tarun Gupta

On Tue, 23 Mar 2021 14:55:23 -0300
Jason Gunthorpe <jgg@nvidia.com> wrote:

> The next patch will use these in mdev_sysfs.c
> 
> While here remove the now dead code checks for NULL, a mdev_type can never
> have a NULL parent.
> 
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> ---
>  drivers/vfio/mdev/mdev_core.c    | 23 +++--------------------
>  drivers/vfio/mdev/mdev_private.h | 12 ++++++++++++
>  2 files changed, 15 insertions(+), 20 deletions(-)

Reviewed-by: Cornelia Huck <cohuck@redhat.com>


^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 07/18] vfio/mdev: Add missing reference counting to mdev_type
  2021-03-23 17:55 ` [PATCH 07/18] vfio/mdev: Add missing reference counting to mdev_type Jason Gunthorpe
  2021-03-23 19:17   ` Christoph Hellwig
  2021-03-26  2:52   ` Tian, Kevin
@ 2021-03-30 15:38   ` Cornelia Huck
  2 siblings, 0 replies; 69+ messages in thread
From: Cornelia Huck @ 2021-03-30 15:38 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Alex Williamson, kvm, Kirti Wankhede, Raj, Ashok, Dan Williams,
	Christoph Hellwig, Leon Romanovsky, Max Gurtovoy, Tarun Gupta

On Tue, 23 Mar 2021 14:55:24 -0300
Jason Gunthorpe <jgg@nvidia.com> wrote:

> struct mdev_type holds a pointer to the kref'd object struct mdev_parent,
> but doesn't hold the kref. The lifetime of the parent becomes implicit
> because parent_remove_sysfs_files() is supposed to remove all the access
> before the parent can be freed, but this is very hard to reason about.
> 
> Make it obviously correct by adding the missing get.
> 
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> ---
>  drivers/vfio/mdev/mdev_sysfs.c | 4 ++++
>  1 file changed, 4 insertions(+)

Reviewed-by: Cornelia Huck <cohuck@redhat.com>


^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 08/18] vfio/mdev: Reorganize mdev_device_create()
  2021-03-23 17:55 ` [PATCH 08/18] vfio/mdev: Reorganize mdev_device_create() Jason Gunthorpe
  2021-03-23 19:20   ` Christoph Hellwig
  2021-03-26  3:31   ` Tian, Kevin
@ 2021-03-30 15:50   ` Cornelia Huck
  2 siblings, 0 replies; 69+ messages in thread
From: Cornelia Huck @ 2021-03-30 15:50 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Alex Williamson, kvm, Kirti Wankhede, Raj, Ashok, Dan Williams,
	Christoph Hellwig, Leon Romanovsky, Max Gurtovoy, Tarun Gupta

On Tue, 23 Mar 2021 14:55:25 -0300
Jason Gunthorpe <jgg@nvidia.com> wrote:

> Once the memory for the struct mdev_device is allocated it should
> immediately be device_initialize()'d and filled in so that put_device()
> can always be used to undo the allocation.
> 
> Place the mdev_get/put_parent() so that they are clearly protecting the
> mdev->parent pointer. Move the final put to the release function so that
> the lifetime rules are trivial to understand.
> 
> Remove mdev_device_free() as the release function via device_put() is now
> usable in all cases.
> 
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> ---
>  drivers/vfio/mdev/mdev_core.c | 46 +++++++++++++++--------------------
>  1 file changed, 20 insertions(+), 26 deletions(-)

Reviewed-by: Cornelia Huck <cohuck@redhat.com>


^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 09/18] vfio/mdev: Add missing error handling to dev_set_name()
  2021-03-23 17:55 ` [PATCH 09/18] vfio/mdev: Add missing error handling to dev_set_name() Jason Gunthorpe
                     ` (2 preceding siblings ...)
  2021-03-29  8:50   ` Max Gurtovoy
@ 2021-03-30 15:53   ` Cornelia Huck
  3 siblings, 0 replies; 69+ messages in thread
From: Cornelia Huck @ 2021-03-30 15:53 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: kvm, Alex Williamson, Raj, Ashok, Dong Jia Shi, Neo Jia,
	Dan Williams, Christoph Hellwig, Jike Song, Kirti Wankhede,
	Leon Romanovsky, Max Gurtovoy, Tarun Gupta

On Tue, 23 Mar 2021 14:55:26 -0300
Jason Gunthorpe <jgg@nvidia.com> wrote:

> This can fail, and seems to be a popular target for syzkaller error
> injection. Check the error return and unwind with put_device().
> 
> Fixes: 7b96953bc640 ("vfio: Mediated device Core driver")
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> ---
>  drivers/vfio/mdev/mdev_core.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)

Reviewed-by: Cornelia Huck <cohuck@redhat.com>


^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 10/18] vfio/mdev: Remove duplicate storage of parent in mdev_device
  2021-03-26 11:53     ` Jason Gunthorpe
@ 2021-03-30 16:14       ` Cornelia Huck
  2021-03-30 16:50         ` Jason Gunthorpe
  0 siblings, 1 reply; 69+ messages in thread
From: Cornelia Huck @ 2021-03-30 16:14 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Tian, Kevin, Alex Williamson, kvm, Kirti Wankhede, Raj, Ashok,
	Williams, Dan J, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

On Fri, 26 Mar 2021 08:53:50 -0300
Jason Gunthorpe <jgg@nvidia.com> wrote:

> On Fri, Mar 26, 2021 at 03:53:10AM +0000, Tian, Kevin wrote:
> 
> > > @@ -58,12 +58,11 @@ void mdev_release_parent(struct kref *kref)
> > >  /* Caller must hold parent unreg_sem read or write lock */
> > >  static void mdev_device_remove_common(struct mdev_device *mdev)
> > >  {
> > > -	struct mdev_parent *parent;
> > > +	struct mdev_parent *parent = mdev->type->parent;  
> > 
> > What about having a wrapper here, like mdev_parent_dev? For
> > readability it's not necessary to show that the parent is indirectly
> > retrieved through mdev_type.  
> 
> I think that is too much wrappering, we only have three usages of the
> mdev->type->parent sequence and two are already single line inlines.

I'm counting more of those in this patch... or do you mean at the end
of the series?


^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 10/18] vfio/mdev: Remove duplicate storage of parent in mdev_device
  2021-03-23 17:55 ` [PATCH 10/18] vfio/mdev: Remove duplicate storage of parent in mdev_device Jason Gunthorpe
  2021-03-23 19:22   ` Christoph Hellwig
  2021-03-26  3:53   ` Tian, Kevin
@ 2021-03-30 16:15   ` Cornelia Huck
  2 siblings, 0 replies; 69+ messages in thread
From: Cornelia Huck @ 2021-03-30 16:15 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Alex Williamson, kvm, Kirti Wankhede, Raj, Ashok, Dan Williams,
	Christoph Hellwig, Leon Romanovsky, Max Gurtovoy, Tarun Gupta

On Tue, 23 Mar 2021 14:55:27 -0300
Jason Gunthorpe <jgg@nvidia.com> wrote:

> mdev_device->type->parent is the same thing.
> 
> The struct mdev_device was relying on the kref on the mdev_parent to also
> indirectly hold a kref on the mdev_type pointer. Now that the type holds a
> kref on the parent we can directly kref the mdev_type and remove this
> implicit relationship.
> 
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> ---
>  drivers/vfio/mdev/mdev_core.c | 13 +++++--------
>  drivers/vfio/mdev/vfio_mdev.c | 14 +++++++-------
>  include/linux/mdev.h          |  1 -
>  3 files changed, 12 insertions(+), 16 deletions(-)

With or without an additional wrapper:

Reviewed-by: Cornelia Huck <cohuck@redhat.com>


^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 11/18] vfio/mdev: Add mdev/mtype_get_type_group_id()
  2021-03-23 17:55 ` [PATCH 11/18] vfio/mdev: Add mdev/mtype_get_type_group_id() Jason Gunthorpe
  2021-03-23 19:23   ` Christoph Hellwig
  2021-03-26  5:52   ` Tian, Kevin
@ 2021-03-30 16:26   ` Cornelia Huck
  2 siblings, 0 replies; 69+ messages in thread
From: Cornelia Huck @ 2021-03-30 16:26 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Alex Williamson, kvm, Kirti Wankhede, Raj, Ashok, Dan Williams,
	Christoph Hellwig, Leon Romanovsky, Max Gurtovoy, Tarun Gupta

On Tue, 23 Mar 2021 14:55:28 -0300
Jason Gunthorpe <jgg@nvidia.com> wrote:

> This returns the index in the supported_type_groups array that is
> associated with the mdev_type attached to the struct mdev_device or its
> containing struct kobject.
> 
> Each mdev_device can be spawned from exactly one mdev_type, which in turn
> originates from exactly one supported_type_group.
> 
> Drivers are using weird string calculations to try and get back to this
> index, providing a direct access to the index removes a bunch of wonky
> driver code.
> 
> mdev_type->group can be deleted as the group is obtained using the
> type_group_id.
> 
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> ---
>  drivers/vfio/mdev/mdev_core.c    | 20 ++++++++++++++++++++
>  drivers/vfio/mdev/mdev_private.h |  2 +-
>  drivers/vfio/mdev/mdev_sysfs.c   | 15 +++++++++------
>  include/linux/mdev.h             |  3 +++
>  4 files changed, 33 insertions(+), 7 deletions(-)

Reviewed-by: Cornelia Huck <cohuck@redhat.com>


^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 17/18] vfio/mdev: Remove kobj from mdev_parent_ops->create()
  2021-03-23 17:55 ` [PATCH 17/18] vfio/mdev: Remove kobj from mdev_parent_ops->create() Jason Gunthorpe
  2021-03-23 19:29   ` Christoph Hellwig
  2021-03-26  6:11   ` Tian, Kevin
@ 2021-03-30 16:29   ` Cornelia Huck
  2 siblings, 0 replies; 69+ messages in thread
From: Cornelia Huck @ 2021-03-30 16:29 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: David Airlie, Tony Krowiak, Alex Williamson,
	Christian Borntraeger, Daniel Vetter, dri-devel, Eric Farman,
	Harald Freudenberger, Vasily Gorbik, Heiko Carstens, intel-gfx,
	intel-gvt-dev, Jani Nikula, Joonas Lahtinen, kvm, Kirti Wankhede,
	linux-s390, Peter Oberparleiter, Halil Pasic, Pierre Morel,
	Rodrigo Vivi, Vineeth Vijayan, Zhenyu Wang, Zhi Wang, Raj, Ashok,
	Dan Williams, Christoph Hellwig, Leon Romanovsky, Max Gurtovoy,
	Tarun Gupta

On Tue, 23 Mar 2021 14:55:34 -0300
Jason Gunthorpe <jgg@nvidia.com> wrote:

> The kobj here is a type-erased version of mdev_type, which is already
> stored in the struct mdev_device being passed in. It was only ever used to
> compute the type_group_id, which is now extracted directly from the mdev.
> 
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> ---
>  drivers/gpu/drm/i915/gvt/kvmgt.c  | 2 +-
>  drivers/s390/cio/vfio_ccw_ops.c   | 2 +-
>  drivers/s390/crypto/vfio_ap_ops.c | 2 +-
>  drivers/vfio/mdev/mdev_core.c     | 2 +-
>  include/linux/mdev.h              | 3 +--
>  samples/vfio-mdev/mbochs.c        | 2 +-
>  samples/vfio-mdev/mdpy.c          | 2 +-
>  samples/vfio-mdev/mtty.c          | 2 +-
>  8 files changed, 8 insertions(+), 9 deletions(-)

Reviewed-by: Cornelia Huck <cohuck@redhat.com>


^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 18/18] vfio/mdev: Correct the function signatures for the mdev_type_attributes
  2021-03-23 17:55 ` [PATCH 18/18] vfio/mdev: Correct the function signatures for the mdev_type_attributes Jason Gunthorpe
  2021-03-23 19:31   ` Christoph Hellwig
  2021-03-26  7:03   ` Tian, Kevin
@ 2021-03-30 16:35   ` Cornelia Huck
  2 siblings, 0 replies; 69+ messages in thread
From: Cornelia Huck @ 2021-03-30 16:35 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: David Airlie, Tony Krowiak, Alex Williamson,
	Christian Borntraeger, Daniel Vetter, dri-devel, Eric Farman,
	Harald Freudenberger, Vasily Gorbik, Heiko Carstens, intel-gfx,
	intel-gvt-dev, Jani Nikula, Joonas Lahtinen, kvm, Kirti Wankhede,
	linux-s390, Peter Oberparleiter, Halil Pasic, Pierre Morel,
	Rodrigo Vivi, Vineeth Vijayan, Zhenyu Wang, Zhi Wang, Raj, Ashok,
	Dan Williams, Christoph Hellwig, Leon Romanovsky, Max Gurtovoy,
	Tarun Gupta

On Tue, 23 Mar 2021 14:55:35 -0300
Jason Gunthorpe <jgg@nvidia.com> wrote:

> The driver core standard is to pass in the properly typed object, the
> properly typed attribute and the buffer data. It stems from the root
> kobject method:
> 
>   ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,..)
> 
> Each subclass of kobject should provide their own function with the same
> signature but more specific types, eg struct device uses:
> 
>   ssize_t (*show)(struct device *dev, struct device_attribute *attr,..)
> 
> In this case the existing signature is:
> 
>   ssize_t (*show)(struct kobject *kobj, struct device *dev,..)
> 
> Where kobj is a 'struct mdev_type *' and dev is 'mdev_type->parent->dev'.
> 
> Change the mdev_type related sysfs attribute functions to:
> 
>   ssize_t (*show)(struct mdev_type *mtype, struct mdev_type_attribute *attr,..)
> 
> In order to restore type safety and match the driver core standard
> 
> There are no current users of 'attr', but if it is ever needed it would be
> hard to add in retroactively, so do it now.
> 
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> ---
>  drivers/gpu/drm/i915/gvt/gvt.c    | 21 +++++++++++----------
>  drivers/s390/cio/vfio_ccw_ops.c   | 15 +++++++++------
>  drivers/s390/crypto/vfio_ap_ops.c | 12 +++++++-----
>  drivers/vfio/mdev/mdev_core.c     | 14 ++++++++++++--
>  drivers/vfio/mdev/mdev_sysfs.c    | 11 ++++++-----
>  include/linux/mdev.h              | 11 +++++++----
>  samples/vfio-mdev/mbochs.c        | 26 +++++++++++++++-----------
>  samples/vfio-mdev/mdpy.c          | 24 ++++++++++++++----------
>  samples/vfio-mdev/mtty.c          | 18 +++++++++---------
>  9 files changed, 90 insertions(+), 62 deletions(-)

Reviewed-by: Cornelia Huck <cohuck@redhat.com>


^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 10/18] vfio/mdev: Remove duplicate storage of parent in mdev_device
  2021-03-30 16:14       ` Cornelia Huck
@ 2021-03-30 16:50         ` Jason Gunthorpe
  0 siblings, 0 replies; 69+ messages in thread
From: Jason Gunthorpe @ 2021-03-30 16:50 UTC (permalink / raw)
  To: Cornelia Huck
  Cc: Tian, Kevin, Alex Williamson, kvm, Kirti Wankhede, Raj, Ashok,
	Williams, Dan J, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

On Tue, Mar 30, 2021 at 06:14:13PM +0200, Cornelia Huck wrote:
> On Fri, 26 Mar 2021 08:53:50 -0300
> Jason Gunthorpe <jgg@nvidia.com> wrote:
> 
> > On Fri, Mar 26, 2021 at 03:53:10AM +0000, Tian, Kevin wrote:
> > 
> > > > @@ -58,12 +58,11 @@ void mdev_release_parent(struct kref *kref)
> > > >  /* Caller must hold parent unreg_sem read or write lock */
> > > >  static void mdev_device_remove_common(struct mdev_device *mdev)
> > > >  {
> > > > -	struct mdev_parent *parent;
> > > > +	struct mdev_parent *parent = mdev->type->parent;  
> > > 
> > > What about having a wrapper here, like mdev_parent_dev? For
> > > readability it's not necessary to show that the parent is indirectly
> > > retrieved through mdev_type.  
> > 
> > I think that is too much wrappering, we only have three usages of the
> > mdev->type->parent sequence and two are already single line inlines.
> 
> I'm counting more of those in this patch... or do you mean at the end
> of the series?

I'm thinking of either moving them to inlines earlier or moving them
all as a final patch. Given how far we've come review wise I'm
inclined to do the latter

Thanks,
Jason 

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 05/18] vfio/mdev: Do not allow a mdev_type to have a NULL parent pointer
  2021-03-23 19:15   ` Christoph Hellwig
@ 2021-04-06 16:08     ` Jason Gunthorpe
  0 siblings, 0 replies; 69+ messages in thread
From: Jason Gunthorpe @ 2021-04-06 16:08 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Cornelia Huck, kvm, Alex Williamson, Raj, Ashok, Dong Jia Shi,
	Neo Jia, Dan Williams, Jike Song, Kirti Wankhede,
	Leon Romanovsky, Max Gurtovoy, Tarun Gupta

On Tue, Mar 23, 2021 at 08:15:58PM +0100, Christoph Hellwig wrote:
> Looks good,
> 
> Reviewed-by: Christoph Hellwig <hch@lst.de>
> 
> Shouldn't this be at the beginning of the series, though?

Yes, done

Jason

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 08/18] vfio/mdev: Reorganize mdev_device_create()
  2021-03-23 19:20   ` Christoph Hellwig
  2021-03-26  3:33     ` Tian, Kevin
@ 2021-04-06 16:40     ` Jason Gunthorpe
  1 sibling, 0 replies; 69+ messages in thread
From: Jason Gunthorpe @ 2021-04-06 16:40 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Alex Williamson, Cornelia Huck, kvm, Kirti Wankhede, Raj, Ashok,
	Dan Williams, Leon Romanovsky, Max Gurtovoy, Tarun Gupta

On Tue, Mar 23, 2021 at 08:20:40PM +0100, Christoph Hellwig wrote:
> >  	up_read(&parent->unreg_sem);
> > -	put_device(&mdev->dev);
> >  mdev_fail:
> >
> >
> >
> > -	mdev_put_parent(parent);
> > +	put_device(&mdev->dev);
> 
> That mdev_fail label is not very descriptive, what about free_device
> instead?

It is all a bit off normal, lets just fix it all in this patch too,
there is alot more changing in this function in later patches that
will all read better:

diff --git a/drivers/vfio/mdev/mdev_core.c b/drivers/vfio/mdev/mdev_core.c
index 517b6fd351b63a..f7559835b0610f 100644
--- a/drivers/vfio/mdev/mdev_core.c
+++ b/drivers/vfio/mdev/mdev_core.c
@@ -263,20 +263,20 @@ int mdev_device_create(struct mdev_type *type, const guid_t *uuid)
 	/* Check if parent unregistration has started */
 	if (!down_read_trylock(&parent->unreg_sem)) {
 		ret = -ENODEV;
-		goto mdev_fail;
+		goto out_put_device;
 	}
 
 	ret = parent->ops->create(&type->kobj, mdev);
 	if (ret)
-		goto ops_create_fail;
+		goto out_unlock;
 
 	ret = device_add(&mdev->dev);
 	if (ret)
-		goto add_fail;
+		goto out_remove;
 
 	ret = mdev_create_sysfs_files(mdev);
 	if (ret)
-		goto sysfs_fail;
+		goto out_del;
 
 	mdev->active = true;
 	dev_dbg(&mdev->dev, "MDEV: created\n");
@@ -284,13 +284,13 @@ int mdev_device_create(struct mdev_type *type, const guid_t *uuid)
 
 	return 0;
 
-sysfs_fail:
+out_del:
 	device_del(&mdev->dev);
-add_fail:
+out_remove:
 	parent->ops->remove(mdev);
-ops_create_fail:
+out_unlock:
 	up_read(&parent->unreg_sem);
-mdev_fail:
+out_put_device:
 	put_device(&mdev->dev);
 	return ret;
 }


Thanks,
Jason

^ permalink raw reply related	[flat|nested] 69+ messages in thread

* Re: [PATCH 18/18] vfio/mdev: Correct the function signatures for the mdev_type_attributes
  2021-03-23 19:31   ` Christoph Hellwig
@ 2021-04-06 18:34     ` Jason Gunthorpe
  0 siblings, 0 replies; 69+ messages in thread
From: Jason Gunthorpe @ 2021-04-06 18:34 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: David Airlie, Tony Krowiak, Alex Williamson,
	Christian Borntraeger, Cornelia Huck, Daniel Vetter, dri-devel,
	Eric Farman, Harald Freudenberger, Vasily Gorbik, Heiko Carstens,
	intel-gfx, intel-gvt-dev, Jani Nikula, Joonas Lahtinen, kvm,
	Kirti Wankhede, linux-s390, Peter Oberparleiter, Halil Pasic,
	Pierre Morel, Rodrigo Vivi, Vineeth Vijayan, Zhenyu Wang,
	Zhi Wang, Raj, Ashok, Dan Williams, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

On Tue, Mar 23, 2021 at 08:31:03PM +0100, Christoph Hellwig wrote:

> > -	type = intel_gvt_find_vgpu_type(gvt, mtype_get_type_group_id(kobj));
> > +	type = intel_gvt_find_vgpu_type(gvt, mtype_get_type_group_id(mtype));
> 
> Somewhere in this series you should probably switch
> intel_gvt_find_vgpu_type to only get the mtype, as it can trivially
> deduct the gvt from it (which also seems to have lost its type
> somewhere..)

I look at just this minor change for a bit and it just is a mess.

This only exists like this because the gvt_type_attrs[] are in the
wrong file and I already tried to fix that and gave up.

Deleting the intel_gvt_ops looks like precondition to do any big
improvement in here :\

Jason

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 04/18] vfio/mdev: Use struct mdev_type in struct mdev_device
  2021-03-26  2:29   ` Tian, Kevin
@ 2021-04-06 18:41     ` Jason Gunthorpe
  0 siblings, 0 replies; 69+ messages in thread
From: Jason Gunthorpe @ 2021-04-06 18:41 UTC (permalink / raw)
  To: Tian, Kevin
  Cc: Alex Williamson, Cornelia Huck, kvm, Kirti Wankhede, Raj, Ashok,
	Williams, Dan J, Christoph Hellwig, Leon Romanovsky,
	Max Gurtovoy, Tarun Gupta

On Fri, Mar 26, 2021 at 02:29:56AM +0000, Tian, Kevin wrote:
> > -int mdev_create_sysfs_files(struct mdev_device *mdev, struct mdev_type
> > *type)
> > +int mdev_create_sysfs_files(struct mdev_device *mdev)
> >  {
> >  	struct kobject *kobj = &mdev->dev.kobj;
> 
> What about adding a local "struct mdev_type *type" here? otherwise,

Okay, saves two links in the diff

Jason

^ permalink raw reply	[flat|nested] 69+ messages in thread

* Re: [PATCH 11/18] vfio/mdev: Add mdev/mtype_get_type_group_id()
  2021-03-23 19:23   ` Christoph Hellwig
@ 2021-04-06 18:53     ` Jason Gunthorpe
  0 siblings, 0 replies; 69+ messages in thread
From: Jason Gunthorpe @ 2021-04-06 18:53 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Alex Williamson, Cornelia Huck, kvm, Kirti Wankhede, Raj, Ashok,
	Dan Williams, Leon Romanovsky, Max Gurtovoy, Tarun Gupta

On Tue, Mar 23, 2021 at 08:23:30PM +0100, Christoph Hellwig wrote:
> On Tue, Mar 23, 2021 at 02:55:28PM -0300, Jason Gunthorpe wrote:
> > +/*
> > + * Return the index in supported_type_groups that this mdev_device was created
> > + * from.
> > + */
> > +unsigned int mdev_get_type_group_id(struct mdev_device *mdev)
> > +{
> > +	return mdev->type->type_group_id;
> > +}
> > +EXPORT_SYMBOL(mdev_get_type_group_id);
> > +
> > +/*
> > + * Used in mdev_type_attribute sysfs functions to return the index in the
> > + * supported_type_groups that the sysfs is called from.
> > + */
> > +unsigned int mtype_get_type_group_id(struct kobject *mtype_kobj)
> > +{
> > +	return container_of(mtype_kobj, struct mdev_type, kobj)->type_group_id;
> > +}
> > +EXPORT_SYMBOL(mtype_get_type_group_id);
> 
> The single field accessors are a little silly..

Looked at this for a while, and to fix it I'd have to pull in the
struct mdev_parent into the public header too (for
mtype_get_parent_dev()) and I think the two silly accessors are the
lessor evil at that point.

Jason

^ permalink raw reply	[flat|nested] 69+ messages in thread

end of thread, other threads:[~2021-04-06 18:53 UTC | newest]

Thread overview: 69+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-23 17:55 [PATCH 00/18] Make vfio_mdev type safe Jason Gunthorpe
2021-03-23 17:55 ` [PATCH 01/18] vfio/mdev: Fix missing static's on MDEV_TYPE_ATTR's Jason Gunthorpe
2021-03-23 17:55 ` [PATCH 02/18] vfio/mdev: Add missing typesafety around mdev_device Jason Gunthorpe
2021-03-26  2:04   ` Tian, Kevin
2021-03-30 15:24   ` Cornelia Huck
2021-03-23 17:55 ` [PATCH 03/18] vfio/mdev: Simplify driver registration Jason Gunthorpe
2021-03-23 19:14   ` Christoph Hellwig
2021-03-26 12:10     ` Jason Gunthorpe
2021-03-26 12:55       ` Christoph Hellwig
2021-03-26  2:17   ` Tian, Kevin
2021-03-30 15:28   ` Cornelia Huck
2021-03-23 17:55 ` [PATCH 04/18] vfio/mdev: Use struct mdev_type in struct mdev_device Jason Gunthorpe
2021-03-23 19:15   ` Christoph Hellwig
2021-03-26  2:29   ` Tian, Kevin
2021-04-06 18:41     ` Jason Gunthorpe
2021-03-30 15:31   ` Cornelia Huck
2021-03-23 17:55 ` [PATCH 05/18] vfio/mdev: Do not allow a mdev_type to have a NULL parent pointer Jason Gunthorpe
2021-03-23 19:15   ` Christoph Hellwig
2021-04-06 16:08     ` Jason Gunthorpe
2021-03-26  2:38   ` Tian, Kevin
2021-03-29  8:42   ` Max Gurtovoy
2021-03-30 15:34   ` Cornelia Huck
2021-03-23 17:55 ` [PATCH 06/18] vfio/mdev: Expose mdev_get/put_parent to mdev_private.h Jason Gunthorpe
2021-03-23 19:16   ` Christoph Hellwig
2021-03-26  2:47   ` Tian, Kevin
2021-03-30 15:37   ` Cornelia Huck
2021-03-23 17:55 ` [PATCH 07/18] vfio/mdev: Add missing reference counting to mdev_type Jason Gunthorpe
2021-03-23 19:17   ` Christoph Hellwig
2021-03-26  2:52   ` Tian, Kevin
2021-03-30 15:38   ` Cornelia Huck
2021-03-23 17:55 ` [PATCH 08/18] vfio/mdev: Reorganize mdev_device_create() Jason Gunthorpe
2021-03-23 19:20   ` Christoph Hellwig
2021-03-26  3:33     ` Tian, Kevin
2021-03-26  3:36       ` Tian, Kevin
2021-04-06 16:40     ` Jason Gunthorpe
2021-03-26  3:31   ` Tian, Kevin
2021-03-30 15:50   ` Cornelia Huck
2021-03-23 17:55 ` [PATCH 09/18] vfio/mdev: Add missing error handling to dev_set_name() Jason Gunthorpe
2021-03-23 19:21   ` Christoph Hellwig
2021-03-26  3:35   ` Tian, Kevin
2021-03-29  8:50   ` Max Gurtovoy
2021-03-30 15:53   ` Cornelia Huck
2021-03-23 17:55 ` [PATCH 10/18] vfio/mdev: Remove duplicate storage of parent in mdev_device Jason Gunthorpe
2021-03-23 19:22   ` Christoph Hellwig
2021-03-26  3:53   ` Tian, Kevin
2021-03-26 11:53     ` Jason Gunthorpe
2021-03-30 16:14       ` Cornelia Huck
2021-03-30 16:50         ` Jason Gunthorpe
2021-03-30 16:15   ` Cornelia Huck
2021-03-23 17:55 ` [PATCH 11/18] vfio/mdev: Add mdev/mtype_get_type_group_id() Jason Gunthorpe
2021-03-23 19:23   ` Christoph Hellwig
2021-04-06 18:53     ` Jason Gunthorpe
2021-03-26  5:52   ` Tian, Kevin
2021-03-30 16:26   ` Cornelia Huck
2021-03-23 17:55 ` [PATCH 12/18] vfio/mtty: Use mdev_get_type_group_id() Jason Gunthorpe
2021-03-23 19:24   ` Christoph Hellwig
2021-03-23 17:55 ` [PATCH 13/18] vfio/mdpy: " Jason Gunthorpe
2021-03-23 19:25   ` Christoph Hellwig
2021-03-23 17:55 ` [PATCH 14/18] vfio/mbochs: " Jason Gunthorpe
2021-03-23 19:25   ` Christoph Hellwig
2021-03-23 17:55 ` [PATCH 17/18] vfio/mdev: Remove kobj from mdev_parent_ops->create() Jason Gunthorpe
2021-03-23 19:29   ` Christoph Hellwig
2021-03-26  6:11   ` Tian, Kevin
2021-03-30 16:29   ` Cornelia Huck
2021-03-23 17:55 ` [PATCH 18/18] vfio/mdev: Correct the function signatures for the mdev_type_attributes Jason Gunthorpe
2021-03-23 19:31   ` Christoph Hellwig
2021-04-06 18:34     ` Jason Gunthorpe
2021-03-26  7:03   ` Tian, Kevin
2021-03-30 16:35   ` Cornelia Huck

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).