From: Anthony Krowiak <akrowiak@linux.ibm.com>
To: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Matthew Rosato <mjrosato@linux.ibm.com>,
David Airlie <airlied@linux.ie>,
Eric Farman <farman@linux.ibm.com>,
dri-devel@lists.freedesktop.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, Yi Liu <yi.l.liu@intel.com>,
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,
Vasily Gorbik <gor@linux.ibm.com>,
Heiko Carstens <hca@linux.ibm.com>,
Eric Auger <eric.auger@redhat.com>,
Harald Freudenberger <freude@linux.ibm.com>,
Rodrigo Vivi <rodrigo.vivi@intel.com>,
intel-gvt-dev@lists.freedesktop.org,
Jason Herne <jjherne@linux.ibm.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: [Intel-gfx] [PATCH 01/15] vfio: Add helpers for unifying vfio_device life cycle
Date: Tue, 30 Aug 2022 15:43:20 -0400 [thread overview]
Message-ID: <857ca47a-e37a-450d-385f-8bdd3fbd2ed9@linux.ibm.com> (raw)
In-Reply-To: <Yw4oUL33TbJK6inc@ziepe.ca>
On 8/30/22 11:10 AM, Jason Gunthorpe wrote:
> On Tue, Aug 30, 2022 at 09:42:42AM -0400, Anthony Krowiak wrote:
>
>>> +/*
>>> + * Alloc and initialize vfio_device so it can be registered to vfio
>>> + * core.
>>> + *
>>> + * Drivers should use the wrapper vfio_alloc_device() for allocation.
>>> + * @size is the size of the structure to be allocated, including any
>>> + * private data used by the driver.
>>
>> It seems the purpose of the wrapper is to ensure that the object being
>> allocated has as its first field a struct vfio_device object and to return
>> its container. Why not just make that a requirement for this function -
>> which I would rename vfio_alloc_device - and document it in the prologue?
>> The caller can then cast the return pointer or use container_of.
> There are three fairly common patterns for this kind of thing
>
> 1) The caller open codes everything:
>
> driver_struct = kzalloc()
> core_init(&driver_struct->core)
>
> 2) Some 'get priv' / 'get data' is used instead of container_of():
>
> core_struct = core_alloc(sizeof(*driver_struct))
> driver_struct = core_get_priv(core_struct)
>
> 3) The allocations and initialization are consolidated in the core,
> but we continue to use container_of()
>
> driver_struct = core_alloc(typeof(*driver_struct))
>
> #1 has a general drawback that people routinely mess up the lifecycle
> model and get really confused about when to do kfree() vs put(),
> creating bugs.
>
> #2 has a general drawback of not using container_of() at all, and being
> a bit confusing in some cases
>
> #3 has the general drawback of being a bit magical, but solves 1 and
> 2's problems.
>
> I would not fix the struct layout without the BUILD_BUG_ON because
> someone will accidently change the order and that becomes a subtle
> runtime error - so at a minimum the wrapper macro has to exist to
> check that.
>
> If you want to allow a dynamic struct layout and avoid the pitfall of
> exposing the user to kalloc/kfree, then you still need the macro, and
> it does some more complicated offset stuff.
>
> Having the wrapper macro be entirely type safe is appealing and
> reduces code in the drivers, IMHO. Tell it what type you are initing
> and get back init'd memory for that type that you always, always free
> with a put operation.
Sounds reasonable, okay I'm buying.
>
> Jason
WARNING: multiple messages have this Message-ID (diff)
From: Anthony Krowiak <akrowiak@linux.ibm.com>
To: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Kevin Tian <kevin.tian@intel.com>,
Zhenyu Wang <zhenyuw@linux.intel.com>,
Zhi Wang <zhi.a.wang@intel.com>,
Jani Nikula <jani.nikula@linux.intel.com>,
Joonas Lahtinen <joonas.lahtinen@linux.intel.com>,
Rodrigo Vivi <rodrigo.vivi@intel.com>,
Tvrtko Ursulin <tvrtko.ursulin@linux.intel.com>,
David Airlie <airlied@linux.ie>, Daniel Vetter <daniel@ffwll.ch>,
Eric Farman <farman@linux.ibm.com>,
Matthew Rosato <mjrosato@linux.ibm.com>,
Halil Pasic <pasic@linux.ibm.com>,
Vineeth Vijayan <vneethv@linux.ibm.com>,
Peter Oberparleiter <oberpar@linux.ibm.com>,
Heiko Carstens <hca@linux.ibm.com>,
Vasily Gorbik <gor@linux.ibm.com>,
Alexander Gordeev <agordeev@linux.ibm.com>,
Christian Borntraeger <borntraeger@linux.ibm.com>,
Sven Schnelle <svens@linux.ibm.com>,
Jason Herne <jjherne@linux.ibm.com>,
Harald Freudenberger <freude@linux.ibm.com>,
Diana Craciun <diana.craciun@oss.nxp.com>,
Alex Williamson <alex.williamson@redhat.com>,
Cornelia Huck <cohuck@redhat.com>,
Longfang Liu <liulongfang@huawei.com>,
Shameer Kolothum <shameerali.kolothum.thodi@huawei.com>,
Yishai Hadas <yishaih@nvidia.com>,
Eric Auger <eric.auger@redhat.com>,
Kirti Wankhede <kwankhede@nvidia.com>,
Leon Romanovsky <leon@kernel.org>,
Abhishek Sahu <abhsahu@nvidia.com>,
intel-gvt-dev@lists.freedesktop.org,
intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org,
linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org,
kvm@vger.kernel.org, Yi Liu <yi.l.liu@intel.com>
Subject: Re: [PATCH 01/15] vfio: Add helpers for unifying vfio_device life cycle
Date: Tue, 30 Aug 2022 15:43:20 -0400 [thread overview]
Message-ID: <857ca47a-e37a-450d-385f-8bdd3fbd2ed9@linux.ibm.com> (raw)
In-Reply-To: <Yw4oUL33TbJK6inc@ziepe.ca>
On 8/30/22 11:10 AM, Jason Gunthorpe wrote:
> On Tue, Aug 30, 2022 at 09:42:42AM -0400, Anthony Krowiak wrote:
>
>>> +/*
>>> + * Alloc and initialize vfio_device so it can be registered to vfio
>>> + * core.
>>> + *
>>> + * Drivers should use the wrapper vfio_alloc_device() for allocation.
>>> + * @size is the size of the structure to be allocated, including any
>>> + * private data used by the driver.
>>
>> It seems the purpose of the wrapper is to ensure that the object being
>> allocated has as its first field a struct vfio_device object and to return
>> its container. Why not just make that a requirement for this function -
>> which I would rename vfio_alloc_device - and document it in the prologue?
>> The caller can then cast the return pointer or use container_of.
> There are three fairly common patterns for this kind of thing
>
> 1) The caller open codes everything:
>
> driver_struct = kzalloc()
> core_init(&driver_struct->core)
>
> 2) Some 'get priv' / 'get data' is used instead of container_of():
>
> core_struct = core_alloc(sizeof(*driver_struct))
> driver_struct = core_get_priv(core_struct)
>
> 3) The allocations and initialization are consolidated in the core,
> but we continue to use container_of()
>
> driver_struct = core_alloc(typeof(*driver_struct))
>
> #1 has a general drawback that people routinely mess up the lifecycle
> model and get really confused about when to do kfree() vs put(),
> creating bugs.
>
> #2 has a general drawback of not using container_of() at all, and being
> a bit confusing in some cases
>
> #3 has the general drawback of being a bit magical, but solves 1 and
> 2's problems.
>
> I would not fix the struct layout without the BUILD_BUG_ON because
> someone will accidently change the order and that becomes a subtle
> runtime error - so at a minimum the wrapper macro has to exist to
> check that.
>
> If you want to allow a dynamic struct layout and avoid the pitfall of
> exposing the user to kalloc/kfree, then you still need the macro, and
> it does some more complicated offset stuff.
>
> Having the wrapper macro be entirely type safe is appealing and
> reduces code in the drivers, IMHO. Tell it what type you are initing
> and get back init'd memory for that type that you always, always free
> with a put operation.
Sounds reasonable, okay I'm buying.
>
> Jason
WARNING: multiple messages have this Message-ID (diff)
From: Anthony Krowiak <akrowiak@linux.ibm.com>
To: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Matthew Rosato <mjrosato@linux.ibm.com>,
David Airlie <airlied@linux.ie>,
Eric Farman <farman@linux.ibm.com>,
dri-devel@lists.freedesktop.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, Yi Liu <yi.l.liu@intel.com>,
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, Zhi Wang <zhi.a.wang@intel.com>,
Kevin Tian <kevin.tian@intel.com>,
Vasily Gorbik <gor@linux.ibm.com>,
Heiko Carstens <hca@linux.ibm.com>,
Eric Auger <eric.auger@redhat.com>,
Alex Williamson <alex.williamson@redhat.com>,
Harald Freudenberger <freude@linux.ibm.com>,
Rodrigo Vivi <rodrigo.vivi@intel.com>,
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>,
Abhishek Sahu <abhsahu@nvidia.com>
Subject: Re: [PATCH 01/15] vfio: Add helpers for unifying vfio_device life cycle
Date: Tue, 30 Aug 2022 15:43:20 -0400 [thread overview]
Message-ID: <857ca47a-e37a-450d-385f-8bdd3fbd2ed9@linux.ibm.com> (raw)
In-Reply-To: <Yw4oUL33TbJK6inc@ziepe.ca>
On 8/30/22 11:10 AM, Jason Gunthorpe wrote:
> On Tue, Aug 30, 2022 at 09:42:42AM -0400, Anthony Krowiak wrote:
>
>>> +/*
>>> + * Alloc and initialize vfio_device so it can be registered to vfio
>>> + * core.
>>> + *
>>> + * Drivers should use the wrapper vfio_alloc_device() for allocation.
>>> + * @size is the size of the structure to be allocated, including any
>>> + * private data used by the driver.
>>
>> It seems the purpose of the wrapper is to ensure that the object being
>> allocated has as its first field a struct vfio_device object and to return
>> its container. Why not just make that a requirement for this function -
>> which I would rename vfio_alloc_device - and document it in the prologue?
>> The caller can then cast the return pointer or use container_of.
> There are three fairly common patterns for this kind of thing
>
> 1) The caller open codes everything:
>
> driver_struct = kzalloc()
> core_init(&driver_struct->core)
>
> 2) Some 'get priv' / 'get data' is used instead of container_of():
>
> core_struct = core_alloc(sizeof(*driver_struct))
> driver_struct = core_get_priv(core_struct)
>
> 3) The allocations and initialization are consolidated in the core,
> but we continue to use container_of()
>
> driver_struct = core_alloc(typeof(*driver_struct))
>
> #1 has a general drawback that people routinely mess up the lifecycle
> model and get really confused about when to do kfree() vs put(),
> creating bugs.
>
> #2 has a general drawback of not using container_of() at all, and being
> a bit confusing in some cases
>
> #3 has the general drawback of being a bit magical, but solves 1 and
> 2's problems.
>
> I would not fix the struct layout without the BUILD_BUG_ON because
> someone will accidently change the order and that becomes a subtle
> runtime error - so at a minimum the wrapper macro has to exist to
> check that.
>
> If you want to allow a dynamic struct layout and avoid the pitfall of
> exposing the user to kalloc/kfree, then you still need the macro, and
> it does some more complicated offset stuff.
>
> Having the wrapper macro be entirely type safe is appealing and
> reduces code in the drivers, IMHO. Tell it what type you are initing
> and get back init'd memory for that type that you always, always free
> with a put operation.
Sounds reasonable, okay I'm buying.
>
> Jason
next prev parent reply other threads:[~2022-09-06 12:34 UTC|newest]
Thread overview: 90+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-27 17:10 [Intel-gfx] [PATCH 00/15] Tidy up vfio_device life cycle Kevin Tian
2022-08-27 17:10 ` Kevin Tian
2022-08-27 10:09 ` [Intel-gfx] ✗ Fi.CI.CHECKPATCH: warning for " Patchwork
2022-08-27 10:29 ` [Intel-gfx] ✓ Fi.CI.BAT: success " Patchwork
2022-08-27 17:10 ` [Intel-gfx] [PATCH 01/15] vfio: Add helpers for unifying " Kevin Tian
2022-08-27 17:10 ` Kevin Tian
2022-08-30 13:42 ` [Intel-gfx] " Anthony Krowiak
2022-08-30 13:42 ` Anthony Krowiak
2022-08-30 15:10 ` Jason Gunthorpe
2022-08-30 15:10 ` Jason Gunthorpe
2022-08-30 19:43 ` Anthony Krowiak [this message]
2022-08-30 19:43 ` Anthony Krowiak
2022-08-30 19:43 ` Anthony Krowiak
2022-08-31 6:03 ` [Intel-gfx] " Tian, Kevin
2022-08-31 6:03 ` Tian, Kevin
2022-08-31 6:03 ` Tian, Kevin
2022-08-27 17:10 ` [Intel-gfx] [PATCH 02/15] vfio/pci: Use the new device life cycle helpers Kevin Tian
2022-08-27 17:10 ` Kevin Tian
2022-08-27 17:10 ` [Intel-gfx] [PATCH 03/15] vfio/mlx5: " Kevin Tian
2022-08-27 17:10 ` Kevin Tian
2022-08-27 17:10 ` [Intel-gfx] [PATCH 04/15] vfio/hisi_acc: " Kevin Tian
2022-08-27 17:10 ` Kevin Tian
2022-08-31 7:36 ` Shameerali Kolothum Thodi
2022-08-27 17:10 ` [Intel-gfx] [PATCH 05/15] vfio/mdpy: " Kevin Tian
2022-08-27 17:10 ` Kevin Tian
2022-08-27 17:10 ` [Intel-gfx] [PATCH 06/15] vfio/mtty: " Kevin Tian
2022-08-27 17:10 ` Kevin Tian
2022-08-27 17:10 ` [Intel-gfx] [PATCH 07/15] vfio/mbochs: " Kevin Tian
2022-08-27 17:10 ` Kevin Tian
2022-08-27 17:10 ` [Intel-gfx] [PATCH 08/15] drm/i915/gvt: " Kevin Tian
2022-08-27 17:10 ` Kevin Tian
2022-08-27 17:10 ` [Intel-gfx] [PATCH 09/15] vfio/ap: " Kevin Tian
2022-08-27 17:10 ` Kevin Tian
2022-08-30 13:43 ` [Intel-gfx] " Anthony Krowiak
2022-08-30 13:43 ` Anthony Krowiak
2022-08-27 17:10 ` [Intel-gfx] [PATCH 10/15] vfio/fsl-mc: " Kevin Tian
2022-08-27 17:10 ` Kevin Tian
2022-08-31 0:22 ` Jason Gunthorpe
2022-08-31 0:22 ` Jason Gunthorpe
2022-08-31 6:14 ` [Intel-gfx] " Tian, Kevin
2022-08-31 6:14 ` Tian, Kevin
2022-08-31 6:14 ` Tian, Kevin
2022-08-27 17:10 ` [Intel-gfx] [PATCH 11/15] vfio/platform: " Kevin Tian
2022-08-27 17:10 ` Kevin Tian
2022-08-27 17:10 ` [Intel-gfx] [PATCH 12/15] vfio/amba: " Kevin Tian
2022-08-27 17:10 ` Kevin Tian
2022-08-27 11:29 ` [Intel-gfx] " kernel test robot
2022-08-27 17:10 ` [Intel-gfx] [PATCH 13/15] vfio/ccw: " Kevin Tian
2022-08-27 17:10 ` Kevin Tian
2022-08-27 10:39 ` [Intel-gfx] " Tian, Kevin
2022-08-27 10:39 ` Tian, Kevin
2022-08-27 17:10 ` [Intel-gfx] [PATCH 14/15] vfio: Rename vfio_device_put() and vfio_device_try_get() Kevin Tian
2022-08-27 17:10 ` Kevin Tian
2022-08-27 17:10 ` [Intel-gfx] [PATCH 15/15] vfio: Add struct device to vfio_device Kevin Tian
2022-08-27 17:10 ` Kevin Tian
2022-08-30 22:18 ` [Intel-gfx] " Alex Williamson
2022-08-30 22:18 ` Alex Williamson
2022-08-30 22:18 ` Alex Williamson
2022-08-30 23:53 ` Jason Gunthorpe
2022-08-30 23:53 ` Jason Gunthorpe
2022-08-31 6:10 ` [Intel-gfx] " Tian, Kevin
2022-08-31 6:10 ` Tian, Kevin
2022-08-31 6:10 ` Tian, Kevin
2022-08-31 17:15 ` [Intel-gfx] " Alex Williamson
2022-08-31 17:15 ` Alex Williamson
2022-08-31 17:15 ` Alex Williamson
2022-09-01 0:46 ` [Intel-gfx] " Tian, Kevin
2022-09-01 0:46 ` Tian, Kevin
2022-09-01 0:46 ` Tian, Kevin
2022-09-01 2:05 ` [Intel-gfx] " Alex Williamson
2022-09-01 2:05 ` Alex Williamson
2022-09-01 2:05 ` Alex Williamson
2022-08-31 0:32 ` Jason Gunthorpe
2022-08-31 0:32 ` Jason Gunthorpe
2022-08-31 6:14 ` [Intel-gfx] " Tian, Kevin
2022-08-31 6:14 ` Tian, Kevin
2022-08-31 6:14 ` Tian, Kevin
2022-08-27 22:41 ` [Intel-gfx] [PATCH 00/15] Tidy up vfio_device life cycle Tian, Kevin
2022-08-27 22:41 ` Tian, Kevin
2022-08-30 18:55 ` [Intel-gfx] ✗ Fi.CI.IGT: failure for " Patchwork
2022-08-31 0:34 ` [Intel-gfx] [PATCH 00/15] " Jason Gunthorpe
2022-08-31 0:34 ` Jason Gunthorpe
2022-08-31 0:34 ` Jason Gunthorpe
2022-08-31 6:14 ` [Intel-gfx] " Tian, Kevin
2022-08-31 6:14 ` Tian, Kevin
2022-08-31 6:14 ` Tian, Kevin
2022-08-31 6:36 ` [Intel-gfx] ✗ Fi.CI.CHECKPATCH: warning for Tidy up vfio_device life cycle (rev2) Patchwork
2022-08-31 6:36 ` [Intel-gfx] ✗ Fi.CI.SPARSE: " Patchwork
2022-08-31 6:48 ` [Intel-gfx] ✓ Fi.CI.BAT: success " Patchwork
2022-08-31 16:46 ` [Intel-gfx] ✓ Fi.CI.IGT: " Patchwork
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=857ca47a-e37a-450d-385f-8bdd3fbd2ed9@linux.ibm.com \
--to=akrowiak@linux.ibm.com \
--cc=abhsahu@nvidia.com \
--cc=agordeev@linux.ibm.com \
--cc=airlied@linux.ie \
--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=intel-gfx@lists.freedesktop.org \
--cc=intel-gvt-dev@lists.freedesktop.org \
--cc=jgg@ziepe.ca \
--cc=jjherne@linux.ibm.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=vneethv@linux.ibm.com \
--cc=yi.l.liu@intel.com \
--cc=yishaih@nvidia.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.