public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Jason Gunthorpe <jgg@ziepe.ca>
To: Christoph Hellwig <hch@infradead.org>
Cc: "Tian, Kevin" <kevin.tian@intel.com>,
	Matthew Rosato <mjrosato@linux.ibm.com>,
	David Airlie <airlied@linux.ie>,
	Joonas Lahtinen <joonas.lahtinen@linux.intel.com>,
	"dri-devel@lists.freedesktop.org"
	<dri-devel@lists.freedesktop.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	Kirti Wankhede <kwankhede@nvidia.com>,
	Vineeth Vijayan <vneethv@linux.ibm.com>,
	Diana Craciun <diana.craciun@oss.nxp.com>,
	Alexander Gordeev <agordeev@linux.ibm.com>,
	Longfang Liu <liulongfang@huawei.com>,
	"linux-s390@vger.kernel.org" <linux-s390@vger.kernel.org>,
	"Liu, Yi L" <yi.l.liu@intel.com>,
	"kvm@vger.kernel.org" <kvm@vger.kernel.org>,
	Leon Romanovsky <leon@kernel.org>,
	Halil Pasic <pasic@linux.ibm.com>,
	Christian Borntraeger <borntraeger@linux.ibm.com>,
	"intel-gfx@lists.freedesktop.org"
	<intel-gfx@lists.freedesktop.org>,
	"Wang, Zhi A" <zhi.a.wang@intel.com>,
	Tony Krowiak <akrowiak@linux.ibm.com>,
	Eric Farman <farman@linux.ibm.com>,
	Vasily Gorbik <gor@linux.ibm.com>,
	Heiko Carstens <hca@linux.ibm.com>,
	Jani Nikula <jani.nikula@linux.intel.com>,
	Eric Auger <eric.auger@redhat.com>,
	Alex Williamson <alex.williamson@redhat.com>,
	Harald Freudenberger <freude@linux.ibm.com>,
	Zhenyu Wang <zhenyuw@linux.intel.com>,
	"Vivi, Rodrigo" <rodrigo.vivi@intel.com>,
	"intel-gvt-dev@lists.freedesktop.org" 
	<intel-gvt-dev@lists.freedesktop.org>,
	Jason Herne <jjherne@linux.ibm.com>,
	Tvrtko Ursulin <tvrtko.ursulin@linux.intel.com>,
	Yishai Hadas <yishaih@nvidia.com>,
	Cornelia Huck <cohuck@redhat.com>,
	Peter Oberparleiter <oberpar@linux.ibm.com>,
	Shameer Kolothum <shameerali.kolothum.thodi@huawei.com>,
	Sven Schnelle <svens@linux.ibm.com>,
	Daniel Vetter <daniel@ffwll.ch>,
	Abhishek Sahu <abhsahu@nvidia.com>
Subject: Re: [PATCH v2 01/15] vfio: Add helpers for unifying vfio_device life cycle
Date: Wed, 7 Sep 2022 09:11:34 -0300	[thread overview]
Message-ID: <YxiKdh6EDBZnTAGH@ziepe.ca> (raw)
In-Reply-To: <YxiGpryRNrxvEoiY@infradead.org>

On Wed, Sep 07, 2022 at 04:55:18AM -0700, Christoph Hellwig wrote:
> 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.

I've seen alot of error handling bugs caused by open-coding patterns
like this. People get confused about what the lifecycle is and botch
the error unwinds, almost 100% of the time :\ They call kfree when
they should call put_device, they call put_device before initing
enough stuff that the release callback doesn't crash, double free
stuff by calling put_device at the wrong point, and so on.

The advantage of init/release is the strict pairing and the core code
helping get the error unwind right, by not calling release until init
succeeds.

The advantage of the vfio_alloc_device() is not saving 4 lines, it is
giving the drivers a simple/sane error handling strategy. Goto unwind
inside init, release undoes everything init does and the probe path
only calls put_device(). It is simple and logical to implement and
hard to make subtle bugs.

Specifically it eliminates the open coded transition of kfree to
put_device that seems so difficult for people to get right.

netdev has done a version of this, so has rdma, and it works well.

Jason

  reply	other threads:[~2022-09-07 12:11 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-01 14:37 [PATCH v2 00/15] Tidy up vfio_device life cycle Kevin Tian
2022-09-01 14:37 ` [PATCH v2 01/15] vfio: Add helpers for unifying " Kevin Tian
2022-09-06  9:41   ` Christoph Hellwig
2022-09-07  0:43     ` Tian, Kevin
2022-09-07 11:55       ` Christoph Hellwig
2022-09-07 12:11         ` Jason Gunthorpe [this message]
2022-09-07 19:28   ` Eric Auger
2022-09-08  6:19     ` Tian, Kevin
2022-09-08  9:08       ` Eric Auger
2022-09-01 14:37 ` [PATCH v2 02/15] vfio/pci: Use the new device life cycle helpers Kevin Tian
2022-09-01 14:37 ` [PATCH v2 03/15] vfio/mlx5: " Kevin Tian
2022-09-01 14:37 ` [PATCH v2 04/15] vfio/hisi_acc: " Kevin Tian
2022-09-01 14:37 ` [PATCH v2 05/15] vfio/mdpy: " Kevin Tian
2022-09-01 14:37 ` [PATCH v2 06/15] vfio/mtty: " Kevin Tian
2022-09-01 14:37 ` [PATCH v2 07/15] vfio/mbochs: " Kevin Tian
2022-09-01 14:37 ` [PATCH v2 08/15] drm/i915/gvt: " Kevin Tian
2022-09-07  3:17   ` Zhenyu Wang
2022-09-01 14:37 ` [PATCH v2 09/15] vfio/ap: " Kevin Tian
2022-09-01 14:37 ` [PATCH v2 10/15] vfio/fsl-mc: " Kevin Tian
2022-09-01 14:37 ` [PATCH v2 11/15] vfio/platform: " Kevin Tian
2022-09-07 19:28   ` Eric Auger
2022-09-01 14:37 ` [PATCH v2 12/15] vfio/amba: " Kevin Tian
2022-09-01  7:37   ` Tian, Kevin
2022-09-07 19:32   ` Eric Auger
2022-09-01 14:37 ` [PATCH v2 13/15] vfio/ccw: " Kevin Tian
2022-09-08  7:19   ` Tian, Kevin
2022-09-08 20:50     ` Eric Farman
2022-09-09  1:52       ` Tian, Kevin
2022-09-01 14:37 ` [PATCH v2 14/15] vfio: Rename vfio_device_put() and vfio_device_try_get() Kevin Tian
2022-09-07 19:35   ` Eric Auger
2022-09-01 14:37 ` [PATCH v2 15/15] vfio: Add struct device to vfio_device Kevin Tian
2022-09-01  7:40   ` Tian, Kevin
2022-09-08  9:06   ` Eric Auger
2022-09-08  9:17     ` Yi Liu
2022-09-08  9:39       ` Eric Auger
2022-09-08 12:37         ` Jason Gunthorpe
2022-09-09  3:09           ` Tian, Kevin

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=YxiKdh6EDBZnTAGH@ziepe.ca \
    --to=jgg@ziepe.ca \
    --cc=abhsahu@nvidia.com \
    --cc=agordeev@linux.ibm.com \
    --cc=airlied@linux.ie \
    --cc=akrowiak@linux.ibm.com \
    --cc=alex.williamson@redhat.com \
    --cc=borntraeger@linux.ibm.com \
    --cc=cohuck@redhat.com \
    --cc=daniel@ffwll.ch \
    --cc=diana.craciun@oss.nxp.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=eric.auger@redhat.com \
    --cc=farman@linux.ibm.com \
    --cc=freude@linux.ibm.com \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=hch@infradead.org \
    --cc=intel-gfx@lists.freedesktop.org \
    --cc=intel-gvt-dev@lists.freedesktop.org \
    --cc=jani.nikula@linux.intel.com \
    --cc=jjherne@linux.ibm.com \
    --cc=joonas.lahtinen@linux.intel.com \
    --cc=kevin.tian@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=kwankhede@nvidia.com \
    --cc=leon@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=liulongfang@huawei.com \
    --cc=mjrosato@linux.ibm.com \
    --cc=oberpar@linux.ibm.com \
    --cc=pasic@linux.ibm.com \
    --cc=rodrigo.vivi@intel.com \
    --cc=shameerali.kolothum.thodi@huawei.com \
    --cc=svens@linux.ibm.com \
    --cc=tvrtko.ursulin@linux.intel.com \
    --cc=vneethv@linux.ibm.com \
    --cc=yi.l.liu@intel.com \
    --cc=yishaih@nvidia.com \
    --cc=zhenyuw@linux.intel.com \
    --cc=zhi.a.wang@intel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox