All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jason Gunthorpe <jgg@nvidia.com>
To: Alex Williamson <alex.williamson@redhat.com>,
	Cornelia Huck <cohuck@redhat.com>,
	kvm@vger.kernel.org, Kirti Wankhede <kwankhede@nvidia.com>
Subject: [PATCH 07/10] vfio/mdev: Allow the mdev_parent_ops to specify the device driver to bind
Date: Mon,  7 Jun 2021 21:55:49 -0300	[thread overview]
Message-ID: <7-v1-324b2038f212+1041f1-vfio3a_jgg@nvidia.com> (raw)
In-Reply-To: <0-v1-324b2038f212+1041f1-vfio3a_jgg@nvidia.com>

This allows a mdev driver to opt out of using vfio_mdev.c, instead the
driver will provide a 'struct mdev_driver' and register directly with the
driver core.

Much of mdev_parent_ops becomes unused in this mode:
- create()/remove() are done via the mdev_driver probe()/remove()
- mdev_attr_groups becomes mdev_driver driver.dev_groups
- Wrapper function callbacks are replaced with the same ones from
  struct vfio_device_ops

Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 drivers/vfio/mdev/mdev_core.c   | 30 ++++++++++++++++++++++--------
 drivers/vfio/mdev/mdev_driver.c | 10 ++++++++++
 include/linux/mdev.h            |  2 ++
 3 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/drivers/vfio/mdev/mdev_core.c b/drivers/vfio/mdev/mdev_core.c
index ff8c1a84516698..e4581ec093a6a6 100644
--- a/drivers/vfio/mdev/mdev_core.c
+++ b/drivers/vfio/mdev/mdev_core.c
@@ -94,9 +94,11 @@ static void mdev_device_remove_common(struct mdev_device *mdev)
 	mdev_remove_sysfs_files(mdev);
 	device_del(&mdev->dev);
 	lockdep_assert_held(&parent->unreg_sem);
-	ret = parent->ops->remove(mdev);
-	if (ret)
-		dev_err(&mdev->dev, "Remove failed: err=%d\n", ret);
+	if (parent->ops->remove) {
+		ret = parent->ops->remove(mdev);
+		if (ret)
+			dev_err(&mdev->dev, "Remove failed: err=%d\n", ret);
+	}
 
 	/* Balances with device_initialize() */
 	put_device(&mdev->dev);
@@ -127,7 +129,9 @@ int mdev_register_device(struct device *dev, const struct mdev_parent_ops *ops)
 	char *envp[] = { env_string, NULL };
 
 	/* check for mandatory ops */
-	if (!ops || !ops->create || !ops->remove || !ops->supported_type_groups)
+	if (!ops || !ops->supported_type_groups)
+		return -EINVAL;
+	if (!ops->device_driver && (!ops->create || !ops->remove))
 		return -EINVAL;
 
 	dev = get_device(dev);
@@ -256,6 +260,7 @@ 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;
+	struct mdev_driver *drv = parent->ops->device_driver;
 
 	mutex_lock(&mdev_list_lock);
 
@@ -296,14 +301,22 @@ int mdev_device_create(struct mdev_type *type, const guid_t *uuid)
 		goto out_put_device;
 	}
 
-	ret = parent->ops->create(mdev);
-	if (ret)
-		goto out_unlock;
+	if (parent->ops->create) {
+		ret = parent->ops->create(mdev);
+		if (ret)
+			goto out_unlock;
+	}
 
 	ret = device_add(&mdev->dev);
 	if (ret)
 		goto out_remove;
 
+	if (!drv)
+		drv = &vfio_mdev_driver;
+	ret = device_driver_attach(&drv->driver, &mdev->dev);
+	if (ret)
+		goto out_del;
+
 	ret = mdev_create_sysfs_files(mdev);
 	if (ret)
 		goto out_del;
@@ -317,7 +330,8 @@ int mdev_device_create(struct mdev_type *type, const guid_t *uuid)
 out_del:
 	device_del(&mdev->dev);
 out_remove:
-	parent->ops->remove(mdev);
+	if (parent->ops->remove)
+		parent->ops->remove(mdev);
 out_unlock:
 	up_read(&parent->unreg_sem);
 out_put_device:
diff --git a/drivers/vfio/mdev/mdev_driver.c b/drivers/vfio/mdev/mdev_driver.c
index 041699571b7e55..c368ec824e2b5c 100644
--- a/drivers/vfio/mdev/mdev_driver.c
+++ b/drivers/vfio/mdev/mdev_driver.c
@@ -71,10 +71,20 @@ static int mdev_remove(struct device *dev)
 	return 0;
 }
 
+static int mdev_match(struct device *dev, struct device_driver *drv)
+{
+	/*
+	 * No drivers automatically match. Drivers are only bound by explicit
+	 * device_driver_attach()
+	 */
+	return 0;
+}
+
 struct bus_type mdev_bus_type = {
 	.name		= "mdev",
 	.probe		= mdev_probe,
 	.remove		= mdev_remove,
+	.match		= mdev_match,
 };
 EXPORT_SYMBOL_GPL(mdev_bus_type);
 
diff --git a/include/linux/mdev.h b/include/linux/mdev.h
index 1fb34ea394ad46..3a38598c260559 100644
--- a/include/linux/mdev.h
+++ b/include/linux/mdev.h
@@ -55,6 +55,7 @@ struct device *mtype_get_parent_dev(struct mdev_type *mtype);
  * register the device to mdev module.
  *
  * @owner:		The module owner.
+ * @device_driver:	Which device driver to probe() on newly created devices
  * @dev_attr_groups:	Attributes of the parent device.
  * @mdev_attr_groups:	Attributes of the mediated device.
  * @supported_type_groups: Attributes to define supported types. It is mandatory
@@ -103,6 +104,7 @@ struct device *mtype_get_parent_dev(struct mdev_type *mtype);
  **/
 struct mdev_parent_ops {
 	struct module   *owner;
+	struct mdev_driver *device_driver;
 	const struct attribute_group **dev_attr_groups;
 	const struct attribute_group **mdev_attr_groups;
 	struct attribute_group **supported_type_groups;
-- 
2.31.1


  parent reply	other threads:[~2021-06-08  0:56 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-08  0:55 [PATCH 00/10] Allow mdev drivers to directly create the vfio_device Jason Gunthorpe
2021-06-08  0:55 ` [Intel-gfx] " Jason Gunthorpe
2021-06-08  0:55 ` [PATCH 01/10] driver core: Do not continue searching for drivers if deferred probe is used Jason Gunthorpe
2021-06-08  5:51   ` Christoph Hellwig
2021-06-08  6:44   ` Greg Kroah-Hartman
2021-06-08 12:16     ` Jason Gunthorpe
2021-06-08 13:13       ` Greg Kroah-Hartman
2021-06-08 13:53         ` Jason Gunthorpe
2021-06-08  7:35   ` Greg Kroah-Hartman
2021-06-08 12:17     ` Jason Gunthorpe
2021-06-08  0:55 ` [PATCH 02/10] driver core: Pull required checks into driver_probe_device() Jason Gunthorpe
2021-06-08  5:59   ` Christoph Hellwig
2021-06-08 12:21     ` Jason Gunthorpe
2021-06-08  0:55 ` [PATCH 03/10] driver core: Flow the return code from ->probe() through to sysfs bind Jason Gunthorpe
2021-06-08  6:07   ` Christoph Hellwig
2021-06-08 23:53     ` Jason Gunthorpe
2021-06-08  6:47   ` Greg Kroah-Hartman
2021-06-08 12:30     ` Jason Gunthorpe
2021-06-08 13:16       ` Greg Kroah-Hartman
2021-06-08 14:03         ` Jason Gunthorpe
2021-06-08  0:55 ` [PATCH 04/10] driver core: Don't return EPROBE_DEFER to userspace during " Jason Gunthorpe
2021-06-08  6:14   ` Christoph Hellwig
2021-06-08  7:37   ` Greg Kroah-Hartman
2021-06-08  0:55 ` [PATCH 05/10] driver core: Export device_driver_attach() Jason Gunthorpe
2021-06-08  6:19   ` Christoph Hellwig
2021-06-08 12:33     ` Jason Gunthorpe
2021-06-08  0:55 ` [PATCH 06/10] vfio/mdev: Remove CONFIG_VFIO_MDEV_DEVICE Jason Gunthorpe
2021-06-08  0:55   ` [Intel-gfx] " Jason Gunthorpe
2021-06-08  6:20   ` Christoph Hellwig
2021-06-08  6:20     ` [Intel-gfx] " Christoph Hellwig
2021-06-11 12:40   ` Cornelia Huck
2021-06-11 12:40     ` [Intel-gfx] " Cornelia Huck
2021-06-14 12:35     ` Jason Gunthorpe
2021-06-14 12:35       ` [Intel-gfx] " Jason Gunthorpe
2021-06-14 12:35       ` Jason Gunthorpe
2021-06-08  0:55 ` Jason Gunthorpe [this message]
2021-06-08  6:21   ` [PATCH 07/10] vfio/mdev: Allow the mdev_parent_ops to specify the device driver to bind Christoph Hellwig
2021-06-08  0:55 ` [PATCH 08/10] vfio/mtty: Convert to use vfio_register_group_dev() Jason Gunthorpe
2021-06-08  0:55 ` [PATCH 09/10] vfio/mdpy: " Jason Gunthorpe
2021-06-08  0:55 ` [PATCH 10/10] vfio/mbochs: " Jason Gunthorpe
2021-06-08  6:22 ` [PATCH 00/10] Allow mdev drivers to directly create the vfio_device Christoph Hellwig
2021-06-08  6:22   ` [Intel-gfx] " Christoph Hellwig
2021-06-14 14:34 ` Kirti Wankhede
2021-06-14 14:34   ` [Intel-gfx] " Kirti Wankhede
2021-06-14 14:34   ` Kirti Wankhede
2021-06-14 14:36   ` Jason Gunthorpe
2021-06-14 14:36     ` [Intel-gfx] " Jason Gunthorpe
2021-06-14 14:36     ` Jason Gunthorpe
2021-06-14 15:08 Allow mdev drivers to directly create the vfio_device (v2 / alternative) Christoph Hellwig
2021-06-14 15:08 ` [PATCH 07/10] vfio/mdev: Allow the mdev_parent_ops to specify the device driver to bind Christoph Hellwig
2021-06-15 10:54   ` Cornelia Huck
2021-06-15 10:54     ` Cornelia Huck
2021-06-15 13:35 Allow mdev drivers to directly create the vfio_device (v3) Christoph Hellwig
2021-06-15 13:35 ` [PATCH 07/10] vfio/mdev: Allow the mdev_parent_ops to specify the device driver to bind Christoph Hellwig
2021-06-15 14:06   ` Cornelia Huck
2021-06-15 14:06     ` Cornelia Huck
2021-06-15 14:11   ` Greg Kroah-Hartman
2021-06-15 14:11     ` Greg Kroah-Hartman
2021-06-16  0:00     ` Jason Gunthorpe
2021-06-16  0:00       ` Jason Gunthorpe
2021-06-16  6:39       ` Greg Kroah-Hartman
2021-06-16  6:39         ` Greg Kroah-Hartman
2021-06-16 20:20   ` Kirti Wankhede
2021-06-16 20:20     ` Kirti Wankhede
2021-06-17 14:22 Allow mdev drivers to directly create the vfio_device (v4) Christoph Hellwig
2021-06-17 14:22 ` [PATCH 07/10] vfio/mdev: Allow the mdev_parent_ops to specify the device driver to bind Christoph Hellwig

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=7-v1-324b2038f212+1041f1-vfio3a_jgg@nvidia.com \
    --to=jgg@nvidia.com \
    --cc=alex.williamson@redhat.com \
    --cc=cohuck@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=kwankhede@nvidia.com \
    /path/to/YOUR_REPLY

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

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.