Skip to content

Commit

Permalink
vfio/mbochs: Use the new device life cycle helpers
Browse files Browse the repository at this point in the history
and manage avail_mbytes inside @init/@Release.

Signed-off-by: Yi Liu <[email protected]>
Signed-off-by: Kevin Tian <[email protected]>
Reviewed-by: Jason Gunthorpe <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Alex Williamson <[email protected]>
  • Loading branch information
yiliu1765 authored and awilliam committed Sep 21, 2022
1 parent 67c5a18 commit 3d5d18e
Showing 1 changed file with 46 additions and 27 deletions.
73 changes: 46 additions & 27 deletions samples/vfio-mdev/mbochs.c
Original file line number Diff line number Diff line change
Expand Up @@ -505,13 +505,14 @@ static int mbochs_reset(struct mdev_state *mdev_state)
return 0;
}

static int mbochs_probe(struct mdev_device *mdev)
static int mbochs_init_dev(struct vfio_device *vdev)
{
int avail_mbytes = atomic_read(&mbochs_avail_mbytes);
struct mdev_state *mdev_state =
container_of(vdev, struct mdev_state, vdev);
struct mdev_device *mdev = to_mdev_device(vdev->dev);
const struct mbochs_type *type =
&mbochs_types[mdev_get_type_group_id(mdev)];
struct device *dev = mdev_dev(mdev);
struct mdev_state *mdev_state;
int avail_mbytes = atomic_read(&mbochs_avail_mbytes);
int ret = -ENOMEM;

do {
Expand All @@ -520,25 +521,17 @@ static int mbochs_probe(struct mdev_device *mdev)
} while (!atomic_try_cmpxchg(&mbochs_avail_mbytes, &avail_mbytes,
avail_mbytes - type->mbytes));

mdev_state = kzalloc(sizeof(struct mdev_state), GFP_KERNEL);
if (mdev_state == NULL)
goto err_avail;
vfio_init_group_dev(&mdev_state->vdev, &mdev->dev, &mbochs_dev_ops);

mdev_state->vconfig = kzalloc(MBOCHS_CONFIG_SPACE_SIZE, GFP_KERNEL);
if (mdev_state->vconfig == NULL)
goto err_mem;
if (!mdev_state->vconfig)
goto err_avail;

mdev_state->memsize = type->mbytes * 1024 * 1024;
mdev_state->pagecount = mdev_state->memsize >> PAGE_SHIFT;
mdev_state->pages = kcalloc(mdev_state->pagecount,
sizeof(struct page *),
GFP_KERNEL);
if (!mdev_state->pages)
goto err_mem;

dev_info(dev, "%s: %s, %d MB, %ld pages\n", __func__,
type->name, type->mbytes, mdev_state->pagecount);
goto err_vconfig;

mutex_init(&mdev_state->ops_lock);
mdev_state->mdev = mdev;
Expand All @@ -553,31 +546,55 @@ static int mbochs_probe(struct mdev_device *mdev)
mbochs_create_config_space(mdev_state);
mbochs_reset(mdev_state);

dev_info(vdev->dev, "%s: %s, %d MB, %ld pages\n", __func__,
type->name, type->mbytes, mdev_state->pagecount);
return 0;

err_vconfig:
kfree(mdev_state->vconfig);
err_avail:
atomic_add(type->mbytes, &mbochs_avail_mbytes);
return ret;
}

static int mbochs_probe(struct mdev_device *mdev)
{
struct mdev_state *mdev_state;
int ret = -ENOMEM;

mdev_state = vfio_alloc_device(mdev_state, vdev, &mdev->dev,
&mbochs_dev_ops);
if (IS_ERR(mdev_state))
return PTR_ERR(mdev_state);

ret = vfio_register_emulated_iommu_dev(&mdev_state->vdev);
if (ret)
goto err_mem;
goto err_put_vdev;
dev_set_drvdata(&mdev->dev, mdev_state);
return 0;
err_mem:
vfio_uninit_group_dev(&mdev_state->vdev);

err_put_vdev:
vfio_put_device(&mdev_state->vdev);
return ret;
}

static void mbochs_release_dev(struct vfio_device *vdev)
{
struct mdev_state *mdev_state =
container_of(vdev, struct mdev_state, vdev);

atomic_add(mdev_state->type->mbytes, &mbochs_avail_mbytes);
kfree(mdev_state->pages);
kfree(mdev_state->vconfig);
kfree(mdev_state);
err_avail:
atomic_add(type->mbytes, &mbochs_avail_mbytes);
return ret;
vfio_free_device(vdev);
}

static void mbochs_remove(struct mdev_device *mdev)
{
struct mdev_state *mdev_state = dev_get_drvdata(&mdev->dev);

vfio_unregister_group_dev(&mdev_state->vdev);
vfio_uninit_group_dev(&mdev_state->vdev);
atomic_add(mdev_state->type->mbytes, &mbochs_avail_mbytes);
kfree(mdev_state->pages);
kfree(mdev_state->vconfig);
kfree(mdev_state);
vfio_put_device(&mdev_state->vdev);
}

static ssize_t mbochs_read(struct vfio_device *vdev, char __user *buf,
Expand Down Expand Up @@ -1397,6 +1414,8 @@ static struct attribute_group *mdev_type_groups[] = {

static const struct vfio_device_ops mbochs_dev_ops = {
.close_device = mbochs_close_device,
.init = mbochs_init_dev,
.release = mbochs_release_dev,
.read = mbochs_read,
.write = mbochs_write,
.ioctl = mbochs_ioctl,
Expand Down

0 comments on commit 3d5d18e

Please sign in to comment.