[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <YxiGpryRNrxvEoiY@infradead.org>
Date: Wed, 7 Sep 2022 04:55:18 -0700
From: Christoph Hellwig <hch@...radead.org>
To: "Tian, Kevin" <kevin.tian@...el.com>
Cc: Christoph Hellwig <hch@...radead.org>,
Matthew Rosato <mjrosato@...ux.ibm.com>,
David Airlie <airlied@...ux.ie>,
Joonas Lahtinen <joonas.lahtinen@...ux.intel.com>,
"dri-devel@...ts.freedesktop.org" <dri-devel@...ts.freedesktop.org>,
"linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>,
Kirti Wankhede <kwankhede@...dia.com>,
Vineeth Vijayan <vneethv@...ux.ibm.com>,
Diana Craciun <diana.craciun@....nxp.com>,
Alexander Gordeev <agordeev@...ux.ibm.com>,
Longfang Liu <liulongfang@...wei.com>,
"linux-s390@...r.kernel.org" <linux-s390@...r.kernel.org>,
"Liu, Yi L" <yi.l.liu@...el.com>,
"kvm@...r.kernel.org" <kvm@...r.kernel.org>,
Leon Romanovsky <leon@...nel.org>,
Halil Pasic <pasic@...ux.ibm.com>,
Jason Gunthorpe <jgg@...pe.ca>,
Christian Borntraeger <borntraeger@...ux.ibm.com>,
"intel-gfx@...ts.freedesktop.org" <intel-gfx@...ts.freedesktop.org>,
"Wang, Zhi A" <zhi.a.wang@...el.com>,
Tony Krowiak <akrowiak@...ux.ibm.com>,
Eric Farman <farman@...ux.ibm.com>,
Vasily Gorbik <gor@...ux.ibm.com>,
Heiko Carstens <hca@...ux.ibm.com>,
Jani Nikula <jani.nikula@...ux.intel.com>,
Eric Auger <eric.auger@...hat.com>,
Alex Williamson <alex.williamson@...hat.com>,
Harald Freudenberger <freude@...ux.ibm.com>,
Zhenyu Wang <zhenyuw@...ux.intel.com>,
"Vivi, Rodrigo" <rodrigo.vivi@...el.com>,
"intel-gvt-dev@...ts.freedesktop.org"
<intel-gvt-dev@...ts.freedesktop.org>,
Jason Herne <jjherne@...ux.ibm.com>,
Tvrtko Ursulin <tvrtko.ursulin@...ux.intel.com>,
Yishai Hadas <yishaih@...dia.com>,
Cornelia Huck <cohuck@...hat.com>,
Peter Oberparleiter <oberpar@...ux.ibm.com>,
Shameer Kolothum <shameerali.kolothum.thodi@...wei.com>,
Sven Schnelle <svens@...ux.ibm.com>,
Daniel Vetter <daniel@...ll.ch>,
Abhishek Sahu <abhsahu@...dia.com>
Subject: Re: [PATCH v2 01/15] vfio: Add helpers for unifying vfio_device life
cycle
On Wed, Sep 07, 2022 at 12:43:30AM +0000, Tian, Kevin wrote:
> > From: Christoph Hellwig
> > Sent: Tuesday, September 6, 2022 5:42 PM
> >
> > What is the point? This adds indirect calls, and actually creates
> > more boilerplate code in the drivers. i.g. when using this code there
> > is more, and harder to read code.
>
> The point is to align with struct device life cycle when it's introduced
> to vfio_device. The object is released via put_device() then what would
> be the alternative if the driver doesn't provide a @release callback?
>
> and with @release then naturally @init is also expected.
No, with a release no @init is expected. The init method is one
of the major obsfucations here, only topped by the weird
vfio_alloc_device macro. Yes, that saves about 4 lines of code
in every driver, but places a burden on the struct layout and
very much obsfucated things. Without vfio_alloc_device and
the init method I think much of this would make a lot more sense.
See the patch below that goes on top of this series to show how
undoing these two would look on mbochs. It it a slight reduction
lines of code, but more readable and much less churn compared
to the status before this series.
diff --git a/samples/vfio-mdev/mbochs.c b/samples/vfio-mdev/mbochs.c
index df95f25fbc0ede..7f01b335fd4dbd 100644
--- a/samples/vfio-mdev/mbochs.c
+++ b/samples/vfio-mdev/mbochs.c
@@ -505,14 +505,12 @@ static int mbochs_reset(struct mdev_state *mdev_state)
return 0;
}
-static int mbochs_init_dev(struct vfio_device *vdev)
+static int mbochs_probe(struct mdev_device *mdev)
{
- struct mdev_state *mdev_state =
- container_of(vdev, struct mdev_state, vdev);
- struct mdev_device *mdev = to_mdev_device(vdev->dev);
+ int avail_mbytes = atomic_read(&mbochs_avail_mbytes);
const struct mbochs_type *type =
&mbochs_types[mdev_get_type_group_id(mdev)];
- int avail_mbytes = atomic_read(&mbochs_avail_mbytes);
+ struct mdev_state *mdev_state;
int ret = -ENOMEM;
do {
@@ -521,10 +519,14 @@ static int mbochs_init_dev(struct vfio_device *vdev)
} while (!atomic_try_cmpxchg(&mbochs_avail_mbytes, &avail_mbytes,
avail_mbytes - type->mbytes));
- mdev_state->vconfig = kzalloc(MBOCHS_CONFIG_SPACE_SIZE, GFP_KERNEL);
- if (!mdev_state->vconfig)
+ mdev_state = kzalloc(sizeof(struct mdev_state), GFP_KERNEL);
+ if (mdev_state == NULL)
goto err_avail;
+ mdev_state->vconfig = kzalloc(MBOCHS_CONFIG_SPACE_SIZE, GFP_KERNEL);
+ if (mdev_state->vconfig == NULL)
+ goto err_state;
+
mdev_state->memsize = type->mbytes * 1024 * 1024;
mdev_state->pagecount = mdev_state->memsize >> PAGE_SHIFT;
mdev_state->pages = kcalloc(mdev_state->pagecount,
@@ -546,38 +548,33 @@ static int mbochs_init_dev(struct vfio_device *vdev)
mbochs_create_config_space(mdev_state);
mbochs_reset(mdev_state);
+ ret = vfio_init_device(&mdev_state->vdev, &mdev->dev, &mbochs_dev_ops);
+ if (ret)
+ goto err_mem;
+
+ ret = vfio_register_emulated_iommu_dev(&mdev_state->vdev);
+ if (ret) {
+ vfio_put_device(&mdev_state->vdev);
+ return ret;
+ }
+
dev_info(vdev->dev, "%s: %s, %d MB, %ld pages\n", __func__,
type->name, type->mbytes, mdev_state->pagecount);
+
+ dev_set_drvdata(&mdev->dev, mdev_state);
return 0;
+err_mem:
+ kfree(mdev_state->pages);
err_vconfig:
kfree(mdev_state->vconfig);
+err_state:
+ kfree(mdev_state);
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_put_vdev;
- dev_set_drvdata(&mdev->dev, mdev_state);
- return 0;
-
-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 =
@@ -585,7 +582,7 @@ static void mbochs_release_dev(struct vfio_device *vdev)
kfree(mdev_state->pages);
kfree(mdev_state->vconfig);
- vfio_free_device(vdev);
+ kfree(vdev);
atomic_add(mdev_state->type->mbytes, &mbochs_avail_mbytes);
}
@@ -1414,7 +1411,6 @@ 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,
Powered by blists - more mailing lists