From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Daniel Vetter <daniel.vetter@ffwll.ch>,
Intel Graphics Development <intel-gfx@lists.freedesktop.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
DRI Development <dri-devel@lists.freedesktop.org>,
Daniel Vetter <daniel.vetter@intel.com>
Subject: Re: [PATCH 03/52] drm: add managed resources tied to drm_device
Date: Thu, 20 Feb 2020 16:58:28 +0200 [thread overview]
Message-ID: <20200220145828.GI4998@pendragon.ideasonboard.com> (raw)
In-Reply-To: <20200219181932.GA2852663@kroah.com>
Hi Greg,
On Wed, Feb 19, 2020 at 07:19:32PM +0100, Greg Kroah-Hartman wrote:
> On Wed, Feb 19, 2020 at 07:36:52PM +0200, Laurent Pinchart wrote:
> > On Wed, Feb 19, 2020 at 06:00:46PM +0100, Greg Kroah-Hartman wrote:
> >> On Wed, Feb 19, 2020 at 03:22:49PM +0100, Daniel Vetter wrote:
> >>> On Wed, Feb 19, 2020 at 2:33 PM Greg Kroah-Hartman wrote:
> >>>> On Wed, Feb 19, 2020 at 03:28:47PM +0200, Laurent Pinchart wrote:
> >>>>> On Wed, Feb 19, 2020 at 11:20:33AM +0100, Daniel Vetter wrote:
> >>>>>> We have lots of these. And the cleanup code tends to be of dubious
> >>>>>> quality. The biggest wrong pattern is that developers use devm_, which
> >>>>>> ties the release action to the underlying struct device, whereas
> >>>>>> all the userspace visible stuff attached to a drm_device can long
> >>>>>> outlive that one (e.g. after a hotunplug while userspace has open
> >>>>>> files and mmap'ed buffers). Give people what they want, but with more
> >>>>>> correctness.
> >>>>>>
> >>>>>> Mostly copied from devres.c, with types adjusted to fit drm_device and
> >>>>>> a few simplifications - I didn't (yet) copy over everything. Since
> >>>>>> the types don't match code sharing looked like a hopeless endeavour.
> >>>>>>
> >>>>>> For now it's only super simplified, no groups, you can't remove
> >>>>>> actions (but kfree exists, we'll need that soon). Plus all specific to
> >>>>>> drm_device ofc, including the logging. Which I didn't bother to make
> >>>>>> compile-time optional, since none of the other drm logging is compile
> >>>>>> time optional either.
> >>>>>>
> >>>>>> One tricky bit here is the chicken&egg between allocating your
> >>>>>> drm_device structure and initiliazing it with drm_dev_init. For
> >>>>>> perfect onion unwinding we'd need to have the action to kfree the
> >>>>>> allocation registered before drm_dev_init registers any of its own
> >>>>>> release handlers. But drm_dev_init doesn't know where exactly the
> >>>>>> drm_device is emebedded into the overall structure, and by the time it
> >>>>>> returns it'll all be too late. And forcing drivers to be able clean up
> >>>>>> everything except the one kzalloc is silly.
> >>>>>>
> >>>>>> Work around this by having a very special final_kfree pointer. This
> >>>>>> also avoids troubles with the list head possibly disappearing from
> >>>>>> underneath us when we release all resources attached to the
> >>>>>> drm_device.
> >>>>>
> >>>>> This is all a very good idea ! Many subsystems are plagged by drivers
> >>>>> using devm_k*alloc to allocate data accessible by userspace. Since the
> >>>>> introduction of devm_*, we've likely reduced the number of memory leaks,
> >>>>> but I'm pretty sure we've increased the risk of crashes as I've seen
> >>>>> some drivers that used .release() callbacks correctly being naively
> >>>>> converted to incorrect devm_* usage :-(
> >>>>>
> >>>>> This leads me to a question: if other subsystems have the same problem,
> >>>>> could we turn this implementation into something more generic ? It
> >>>>> doesn't have to be done right away and shouldn't block merging this
> >>>>> series, but I think it would be very useful.
> >>>>
> >>>> It shouldn't be that hard to tie this into a drv_m() type of a thing
> >>>> (driver_memory?)
> >>>>
> >>>> And yes, I think it's much better than devm_* for the obvious reasons of
> >>>> this being needed here.
> >>>
> >>> There's two reasons I went with copypasta instead of trying to share code:
> >>> - Type checking, I definitely don't want people to mix up devm_ with
> >>> drmm_. But even if we do a drv_m that subsystems could embed we do
> >>> have quite a few different types of component drivers (and with
> >>> drm_panel and drm_bridge even standardized), and I don't want people
> >>> to be able to pass the wrong kind of struct to e.g. a managed
> >>> drmm_connector_init - it really needs to be the drm_device, not a
> >>> panel or bridge or something else.
> >>
> >> Fair enough, that makes sense.
> >>
> >>> - We could still share the code as a kind of implementation/backend
> >>> library. But it's not much, and with embedding I could use the drm
> >>> device logging stuff which is kinda nice. But if there's more demand
> >>> for this I can definitely see the point in sharing this, as Laurent
> >>> pointed out with the tiny optimization with not allocating a NULL void
> >>> * that I've done (and screwed up) it's not entirely trivial code.
> >>
> >> I think moving over time to having this be a backend library is good.
> >> But no rush/issues here with this going in now, it solves a real need
> >> and we can refactor it later on to try to make it more "bus/class"
> >> generic as needed.
> >
> > >From a type checking point of view, it would then be nice to have a
> > structure that models a device node, other than just struct device that
> > is shared by all types of devices. As someone who was involve in the
> > creation of the device model we have today, and thus know the history,
> > what's your opinion on that ?
>
> My opinion is that 'struct device' was created just for that exact
> thing. If "all you want" is a device node, it is trivial to use:
> device_create();
> or device_create_varargs() or device_create_with_groups()
> and then use device_destroy() when you are done with it.
>
> yes, it can do much more complex things, as needed, but the basics are
> there, so use it in a simple way if you want to, no objection from me.
>
> If there are things that are missing with it, please let me know.
I don't think it's really about anything missing, but about having two
different APIs for driver developers, to associate resources with either
physical devices that can disappear from the system, or with interfaces
exposed to userspace (or other parts of the kernel). The lifetime
constraints are very different, and if both cases are handled with the
devres API and a struct device, it's very easy for driver authors to
pass the wrong struct device to the API, tying the lifetime of a
userspace-facing resource with the physical device (this is what devres
is mostly used for today :-(). Having two different objects would make
it more apparent which API should be used, and would make it easier to
catch incorrect usage during review. I think this is Daniel's main
point, and the reason that prompted him to create a new API instead of
just reusing devres with the struct device that models the userspace
interface.
(On a side node, I wonder if devres shouldn't have been implemented at
the kref level.)
> But creating a new structure/way for this, no, we do not want to go back
> to the 2.4 and older kernel methods where it was all totally disjointed
> and messy.
--
Regards,
Laurent Pinchart
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel
next prev parent reply other threads:[~2020-02-20 14:58 UTC|newest]
Thread overview: 135+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-02-19 10:20 [PATCH 00/52] drm_device managed resources Daniel Vetter
2020-02-19 10:20 ` [PATCH 01/52] mm/sl[uo]b: export __kmalloc_track(_node)_caller Daniel Vetter
2020-02-19 19:42 ` Andrew Morton
2020-02-22 3:42 ` Christopher Lameter
2020-02-19 10:20 ` [PATCH 02/52] drm/i915: Don't clear drvdata in ->release Daniel Vetter
2020-02-19 10:20 ` [PATCH 03/52] drm: add managed resources tied to drm_device Daniel Vetter
2020-02-19 12:31 ` Neil Armstrong
2020-02-19 13:24 ` Daniel Vetter
2020-02-19 13:28 ` Laurent Pinchart
2020-02-19 13:33 ` Greg Kroah-Hartman
2020-02-19 14:22 ` Daniel Vetter
2020-02-19 16:09 ` [Intel-gfx] " Emil Velikov
2020-02-19 16:22 ` Daniel Vetter
2020-02-19 16:41 ` Emil Velikov
2020-02-19 16:46 ` Laurent Pinchart
2020-02-19 16:53 ` Daniel Vetter
2020-02-19 17:02 ` Laurent Pinchart
2020-02-19 17:06 ` Daniel Vetter
2020-02-19 17:00 ` Greg Kroah-Hartman
2020-02-19 17:36 ` Laurent Pinchart
2020-02-19 18:19 ` Greg Kroah-Hartman
2020-02-19 19:57 ` Daniel Vetter
2020-02-20 14:58 ` Laurent Pinchart [this message]
2020-02-19 13:57 ` Daniel Vetter
2020-02-19 10:20 ` [PATCH 04/52] drm: Set final_kfree in drm_dev_alloc Daniel Vetter
2020-02-19 12:03 ` Oleksandr Andrushchenko
2020-02-19 13:39 ` Laurent Pinchart
2020-02-19 14:41 ` Daniel Vetter
2020-02-21 19:07 ` Daniel Vetter
2020-02-19 10:20 ` [PATCH 05/52] drm/mipi_dbi: Use drmm_add_final_kfree in all drivers Daniel Vetter
2020-02-19 11:47 ` Thomas Zimmermann
2020-02-19 12:45 ` Thomas Zimmermann
2020-02-19 13:23 ` Daniel Vetter
2020-02-19 13:29 ` Thomas Zimmermann
2020-02-19 14:32 ` Daniel Vetter
2020-02-20 16:18 ` Noralf Trønnes
2020-02-19 10:20 ` [PATCH 06/52] drm/udl: Use drmm_add_final_kfree Daniel Vetter
2020-02-19 10:20 ` [PATCH 07/52] " Daniel Vetter
2020-02-19 13:42 ` Laurent Pinchart
2020-02-19 14:43 ` Daniel Vetter
2020-02-19 10:20 ` [PATCH 08/52] drm/qxl: " Daniel Vetter
2020-02-19 10:20 ` [PATCH 09/52] drm/i915: " Daniel Vetter
2020-02-19 10:20 ` [PATCH 10/52] drm/cirrus: " Daniel Vetter
2020-02-24 8:13 ` Gerd Hoffmann
2020-02-19 10:20 ` [PATCH 11/52] drm/v3d: " Daniel Vetter
2020-02-19 18:43 ` Eric Anholt
2020-02-19 10:20 ` [PATCH 12/52] drm/tidss: " Daniel Vetter
2020-02-19 10:20 ` [PATCH 13/52] drm/mcde: " Daniel Vetter
2020-02-19 16:12 ` Linus Walleij
2020-02-19 10:20 ` [PATCH 14/52] drm/vgem: " Daniel Vetter
2020-02-19 10:20 ` [PATCH 15/52] drm/vkms: " Daniel Vetter
2020-02-19 10:20 ` [PATCH 16/52] drm/repaper: " Daniel Vetter
2020-02-20 16:18 ` Noralf Trønnes
2020-02-19 10:20 ` [PATCH 17/52] drm/inigenic: " Daniel Vetter
2020-02-19 10:20 ` [PATCH 18/52] drm/gm12u320: " Daniel Vetter
2020-02-19 10:20 ` [PATCH 19/52] drm/<drivers>: " Daniel Vetter
2020-02-19 14:11 ` Laurent Pinchart
2020-02-19 14:30 ` Daniel Vetter
2020-02-19 14:39 ` Laurent Pinchart
2020-02-19 15:29 ` Daniel Vetter
2020-02-19 10:20 ` [PATCH 20/52] drm: Cleanups after drmm_add_final_kfree rollout Daniel Vetter
2020-02-19 10:20 ` [PATCH 21/52] drm: Handle dev->unique with drmm_ Daniel Vetter
2020-02-19 14:28 ` Laurent Pinchart
2020-02-19 15:24 ` Daniel Vetter
2020-02-19 10:20 ` [PATCH 22/52] drm: Use drmm_ for drm_dev_init cleanup Daniel Vetter
2020-02-19 14:35 ` Laurent Pinchart
2020-02-19 15:27 ` Daniel Vetter
2020-02-19 15:37 ` Laurent Pinchart
2020-02-19 15:44 ` Daniel Vetter
2020-02-19 10:20 ` [PATCH 23/52] drm: manage drm_minor cleanup with drmm_ Daniel Vetter
2020-02-19 14:47 ` Laurent Pinchart
2020-02-19 15:34 ` Daniel Vetter
2020-02-19 10:20 ` [PATCH 24/52] drm: Manage drm_gem_init " Daniel Vetter
2020-02-19 14:22 ` Laurent Pinchart
2020-02-19 14:37 ` Daniel Vetter
2020-02-19 14:52 ` Laurent Pinchart
2020-02-19 14:56 ` Daniel Vetter
2020-02-19 10:20 ` [PATCH 25/52] drm: Manage drm_vblank_cleanup " Daniel Vetter
2020-02-19 10:20 ` [PATCH 26/52] drm: Garbage collect drm_dev_fini Daniel Vetter
2020-02-19 10:20 ` [PATCH 27/52] drm: Manage drm_mode_config_init with drmm_ Daniel Vetter
2020-02-19 13:49 ` Laurent Pinchart
2020-02-19 15:47 ` Daniel Vetter
2020-02-19 16:07 ` Laurent Pinchart
2020-02-19 16:23 ` Daniel Vetter
2020-02-19 17:30 ` Noralf Trønnes
2020-02-19 18:12 ` Daniel Vetter
2020-02-19 10:20 ` [PATCH 28/52] drm/bochs: Remove leftover drm_atomic_helper_shutdown Daniel Vetter
2020-02-24 8:14 ` Gerd Hoffmann
2020-02-19 10:20 ` [PATCH 29/52] drm/bochs: Drop explicit drm_mode_config_cleanup Daniel Vetter
2020-02-24 8:15 ` Gerd Hoffmann
2020-02-19 10:21 ` [PATCH 30/52] drm/cirrus: Drop explicit drm_mode_config_cleanup call Daniel Vetter
2020-02-24 8:16 ` Gerd Hoffmann
2020-02-19 10:21 ` [PATCH 31/52] drm/cirrus: Fully embrace devm_ Daniel Vetter
2020-02-24 8:18 ` Gerd Hoffmann
2020-02-19 10:21 ` [PATCH 32/52] drm/ingenic: Drop explicit drm_mode_config_cleanup call Daniel Vetter
2020-02-19 10:21 ` [PATCH 33/52] drm/mcde: " Daniel Vetter
2020-02-19 16:12 ` Linus Walleij
2020-02-19 10:21 ` [PATCH 34/52] drm/mcde: More devm_drm_dev_init Daniel Vetter
2020-02-19 16:13 ` Linus Walleij
2020-02-19 10:21 ` [PATCH 35/52] drm/meson: Drop explicit drm_mode_config_cleanup call Daniel Vetter
2020-02-19 10:39 ` Neil Armstrong
2020-02-19 10:21 ` [PATCH 36/52] drm/pl111: " Daniel Vetter
2020-02-19 10:21 ` [PATCH 37/52] drm/rcar-du: " Daniel Vetter
2020-02-19 10:30 ` Geert Uytterhoeven
2020-02-19 10:56 ` Daniel Vetter
2020-02-19 11:10 ` Geert Uytterhoeven
2020-02-19 12:17 ` Laurent Pinchart
2020-02-19 12:40 ` Daniel Vetter
2020-02-19 13:53 ` Laurent Pinchart
2020-02-19 14:29 ` Daniel Vetter
2020-02-19 10:21 ` [PATCH 38/52] drm/rockchip: " Daniel Vetter
2020-02-19 10:21 ` [PATCH 39/52] drm/stm: " Daniel Vetter
2020-02-20 14:18 ` Philippe CORNU
2020-02-20 16:12 ` Daniel Vetter
2020-02-19 10:21 ` [PATCH 40/52] drm/shmob: " Daniel Vetter
2020-02-19 13:57 ` Laurent Pinchart
2020-02-19 10:21 ` [PATCH 41/52] drm/mtk: " Daniel Vetter
2020-02-19 10:21 ` [PATCH 42/52] drm/tidss: " Daniel Vetter
2020-02-19 10:21 ` [PATCH 43/52] drm/gm12u320: More drmm_ Daniel Vetter
2020-02-19 10:21 ` [PATCH 44/52] drm/gm12u320: Use devm_drm_dev_init Daniel Vetter
2020-02-19 10:21 ` [PATCH 45/52] drm/gm12u320: Use helpers for shutdown/suspend/resume Daniel Vetter
2020-02-19 10:21 ` [PATCH 46/52] drm/gm12u320: Simplify upload work Daniel Vetter
2020-02-19 10:21 ` [PATCH 47/52] drm/repaper: Drop explicit drm_mode_config_cleanup call Daniel Vetter
2020-02-20 16:21 ` Noralf Trønnes
2020-02-19 10:21 ` [PATCH 48/52] drm/mipi-dbi: Move drm_mode_config_init into mipi library Daniel Vetter
2020-02-20 16:22 ` Noralf Trønnes
2020-02-19 10:21 ` [PATCH 49/52] drm/mipi-dbi: Drop explicit drm_mode_config_cleanup call Daniel Vetter
2020-02-20 16:22 ` Noralf Trønnes
2020-02-19 10:21 ` [PATCH 50/52] drm/udl: " Daniel Vetter
2020-02-19 10:21 ` [PATCH 51/52] drm/udl: drop drm_driver.release hook Daniel Vetter
2020-02-19 10:21 ` [PATCH 52/52] drm: Add docs for managed resources Daniel Vetter
2020-02-19 15:08 ` Laurent Pinchart
2020-02-19 15:40 ` Daniel Vetter
2020-02-21 20:23 ` Sam Ravnborg
2020-02-21 21:13 ` Sam Ravnborg
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=20200220145828.GI4998@pendragon.ideasonboard.com \
--to=laurent.pinchart@ideasonboard.com \
--cc=daniel.vetter@ffwll.ch \
--cc=daniel.vetter@intel.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=gregkh@linuxfoundation.org \
--cc=intel-gfx@lists.freedesktop.org \
--cc=rafael@kernel.org \
/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