[PATCH v1 15/18] vfio/ccw: Manage private with mdev

Eric Farman
2022-06-02T17:20:10Z

Simplify the lifecycle of a vfio-ccw device by moving the alloc/free
of the vfio-ccw private struct to the mdev probe/remove processing,
rather than the subchannel.

Suggested-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Eric Farman <farman@linux.ibm.com>
---
drivers/s390/cio/vfio_ccw_drv.c | 35 ++++++++---------------------
drivers/s390/cio/vfio_ccw_ops.c | 35 +++++++++++++----------------
drivers/s390/cio/vfio_ccw_private.h | 5 +++++
3 files changed, 29 insertions(+), 46 deletions(-)

diff --git a/drivers/s390/cio/vfio_ccw_drv.c b/drivers/s390/cio/vfio_ccw_drv.c
index 6dca35f3ceba..5928460854ec 100644
--- a/drivers/s390/cio/vfio_ccw_drv.c
+++ b/drivers/s390/cio/vfio_ccw_drv.c
@@ -133,7 +133,8 @@ static void vfio_ccw_sch_irq(struct subchannel *sch)
vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_INTERRUPT);
}

-static struct vfio_ccw_private *vfio_ccw_alloc_private(struct subchannel *sch)
+struct vfio_ccw_private *vfio_ccw_alloc_private(struct mdev_device *mdev,
+ struct subchannel *sch)
{
struct vfio_ccw_private *private;

@@ -141,6 +142,8 @@ static struct vfio_ccw_private *vfio_ccw_alloc_private(struct subchannel *sch)
if (!private)
return ERR_PTR(-ENOMEM);

+ vfio_init_group_dev(&private->vdev, &mdev->dev, &vfio_ccw_dev_ops);
+
private->sch = sch;
mutex_init(&private->io_mutex);
private->state = VFIO_CCW_STATE_STANDBY;
@@ -186,11 +189,12 @@ static struct vfio_ccw_private *vfio_ccw_alloc_private(struct subchannel *sch)
kfree(private->cp.guest_cp);
out_free_private:
mutex_destroy(&private->io_mutex);
+ vfio_uninit_group_dev(&private->vdev);
kfree(private);
return ERR_PTR(-ENOMEM);
}

-static void vfio_ccw_free_private(struct vfio_ccw_private *private)
+void vfio_ccw_free_private(struct vfio_ccw_private *private)
{
struct vfio_ccw_crw *crw, *temp;

@@ -205,14 +209,14 @@ static void vfio_ccw_free_private(struct vfio_ccw_private *private)
kmem_cache_free(vfio_ccw_io_region, private->io_region);
kfree(private->cp.guest_cp);
mutex_destroy(&private->io_mutex);
+ vfio_uninit_group_dev(&private->vdev);
kfree(private);
}

static int vfio_ccw_sch_probe(struct subchannel *sch)
{
struct pmcw *pmcw = &sch->schib.pmcw;
- struct vfio_ccw_private *private;
- int ret = -ENOMEM;
+ int ret;

if (pmcw->qf) {
dev_warn(&sch->dev, "vfio: ccw: does not support QDIO: %s\n",
@@ -220,41 +224,20 @@ static int vfio_ccw_sch_probe(struct subchannel *sch)
return -ENODEV;
}

- private = vfio_ccw_alloc_private(sch);
- if (IS_ERR(private))
- return PTR_ERR(private);
-
- dev_set_drvdata(&sch->dev, private);
-
ret = mdev_register_device(&sch->dev, &vfio_ccw_mdev_ops);
if (ret)
- goto out_free;
+ return ret;

VFIO_CCW_MSG_EVENT(4, "bound to subchannel %x.%x.%04x\n",
sch->schid.cssid, sch->schid.ssid,
sch->schid.sch_no);
return 0;
-
-out_free:
- dev_set_drvdata(&sch->dev, NULL);
- vfio_ccw_free_private(private);
- return ret;
}

static void vfio_ccw_sch_remove(struct subchannel *sch)
{
- struct vfio_ccw_private *private = dev_get_drvdata(&sch->dev);
-
- if (!private)
- return;
-
- vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_CLOSE);
mdev_unregister_device(&sch->dev);

- dev_set_drvdata(&sch->dev, NULL);
-
- vfio_ccw_free_private(private);
-
VFIO_CCW_MSG_EVENT(4, "unbound from subchannel %x.%x.%04x\n",
sch->schid.cssid, sch->schid.ssid,
sch->schid.sch_no);
diff --git a/drivers/s390/cio/vfio_ccw_ops.c b/drivers/s390/cio/vfio_ccw_ops.c
index 60a4855e8ecb..1ce7079c9dbb 100644
--- a/drivers/s390/cio/vfio_ccw_ops.c
+++ b/drivers/s390/cio/vfio_ccw_ops.c
@@ -17,8 +17,6 @@

#include "vfio_ccw_private.h"

-static const struct vfio_device_ops vfio_ccw_dev_ops;
-
static int vfio_ccw_mdev_reset(struct vfio_ccw_private *private)
{
/*
@@ -84,29 +82,28 @@ static struct attribute_group *mdev_type_groups[] = {

static int vfio_ccw_mdev_probe(struct mdev_device *mdev)
{
- struct vfio_ccw_private *private = dev_get_drvdata(mdev->dev.parent);
+ struct subchannel *sch = to_subchannel(mdev->dev.parent);
+ struct vfio_ccw_private *private;
int ret;

- if (private->state == VFIO_CCW_STATE_NOT_OPER)
- return -ENODEV;
-
- memset(&private->vdev, 0, sizeof(private->vdev));
- vfio_init_group_dev(&private->vdev, &mdev->dev,
- &vfio_ccw_dev_ops);
+ private = vfio_ccw_alloc_private(mdev, sch);
+ if (IS_ERR(private))
+ return PTR_ERR(private);

VFIO_CCW_MSG_EVENT(2, "sch %x.%x.%04x: create\n",
- private->sch->schid.cssid,
- private->sch->schid.ssid,
- private->sch->schid.sch_no);
+ sch->schid.cssid,
+ sch->schid.ssid,
+ sch->schid.sch_no);

ret = vfio_register_emulated_iommu_dev(&private->vdev);
if (ret)
- goto err_init;
+ goto err_alloc;
dev_set_drvdata(&mdev->dev, private);
+ dev_set_drvdata(&sch->dev, private);
return 0;

-err_init:
- vfio_uninit_group_dev(&private->vdev);
+err_alloc:
+ vfio_ccw_free_private(private);
return ret;
}

@@ -122,12 +119,10 @@ static void vfio_ccw_mdev_remove(struct mdev_device *mdev)
private->sch->schid.ssid,
private->sch->schid.sch_no);

+ dev_set_drvdata(&private->sch->dev, NULL);
dev_set_drvdata(&mdev->dev, NULL);
vfio_unregister_group_dev(&private->vdev);
-
- vfio_ccw_fsm_event(private, VFIO_CCW_EVENT_CLOSE);
-
- vfio_uninit_group_dev(&private->vdev);
+ vfio_ccw_free_private(private);
}

static int vfio_ccw_mdev_open_device(struct vfio_device *vdev)
@@ -603,7 +598,7 @@ static unsigned int vfio_ccw_get_available(struct mdev_type *mtype)
return 1;
}

-static const struct vfio_device_ops vfio_ccw_dev_ops = {
+const struct vfio_device_ops vfio_ccw_dev_ops = {
.open_device = vfio_ccw_mdev_open_device,
.close_device = vfio_ccw_mdev_close_device,
.read = vfio_ccw_mdev_read,
diff --git a/drivers/s390/cio/vfio_ccw_private.h b/drivers/s390/cio/vfio_ccw_private.h
index e568fd6bcf2a..bf11ebd0d32a 100644
--- a/drivers/s390/cio/vfio_ccw_private.h
+++ b/drivers/s390/cio/vfio_ccw_private.h
@@ -117,8 +117,13 @@ struct vfio_ccw_private {

extern int vfio_ccw_sch_quiesce(struct subchannel *sch);

+struct vfio_ccw_private *vfio_ccw_alloc_private(struct mdev_device *mdev,
+ struct subchannel *sch);
+void vfio_ccw_free_private(struct vfio_ccw_private *private);
+
extern struct mdev_driver vfio_ccw_mdev_driver;
extern const struct mdev_parent_ops vfio_ccw_mdev_ops;
+extern const struct vfio_device_ops vfio_ccw_dev_ops;

/*
* States of the device statemachine.
--
2.32.0