[PATCH] vfio-mdev: support mediated device creation in kernel

From: Yonghyun Hwang
Date: Fri Mar 20 2020 - 13:59:22 EST


To enable a mediated device, a device driver registers its device to VFIO
MDev framework. Once the mediated device gets enabled, UUID gets fed onto
the sysfs attribute, "create", to create the mediated device. This
additional step happens after boot-up gets complete. If the driver knows
how many mediated devices need to be created during probing time, the
additional step becomes cumbersome. This commit implements a new function
to allow the driver to create a mediated device in kernel.

Signed-off-by: Yonghyun Hwang <yonghyun@xxxxxxxxxx>
---
drivers/vfio/mdev/mdev_core.c | 45 +++++++++++++++++++++++++++++++++++
include/linux/mdev.h | 3 +++
2 files changed, 48 insertions(+)

diff --git a/drivers/vfio/mdev/mdev_core.c b/drivers/vfio/mdev/mdev_core.c
index b558d4cfd082..a6d32516de42 100644
--- a/drivers/vfio/mdev/mdev_core.c
+++ b/drivers/vfio/mdev/mdev_core.c
@@ -350,6 +350,51 @@ int mdev_device_create(struct kobject *kobj,
return ret;
}

+/*
+ * mdev_create_device : Create a mdev device
+ * @dev: device structure representing parent device.
+ * @uuid: uuid char string for a mdev device.
+ * @group: index to supported type groups for a mdev device.
+ *
+ * Create a mdev device in kernel.
+ * Returns a negative value on error, otherwise 0.
+ */
+int mdev_create_device(struct device *dev,
+ const char *uuid, int group)
+{
+ struct mdev_parent *parent = NULL;
+ struct mdev_type *type = NULL;
+ guid_t guid;
+ int i = 1;
+ int ret;
+
+ ret = guid_parse(uuid, &guid);
+ if (ret) {
+ dev_err(dev, "Failed to parse UUID");
+ return ret;
+ }
+
+ parent = __find_parent_device(dev);
+ if (!parent) {
+ dev_err(dev, "Failed to find parent mdev device");
+ return -ENODEV;
+ }
+
+ list_for_each_entry(type, &parent->type_list, next) {
+ if (i == group)
+ break;
+ i++;
+ }
+
+ if (!type || i != group) {
+ dev_err(dev, "Failed to find mdev device");
+ return -ENODEV;
+ }
+
+ return mdev_device_create(&type->kobj, parent->dev, &guid);
+}
+EXPORT_SYMBOL(mdev_create_device);
+
int mdev_device_remove(struct device *dev)
{
struct mdev_device *mdev, *tmp;
diff --git a/include/linux/mdev.h b/include/linux/mdev.h
index 0ce30ca78db0..b66f67998916 100644
--- a/include/linux/mdev.h
+++ b/include/linux/mdev.h
@@ -145,4 +145,7 @@ 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);

+extern int mdev_create_device(struct device *dev,
+ const char *uuid, int group_idx);
+
#endif /* MDEV_H */
--
2.25.1.696.g5e7596f4ac-goog