From: Maxime Ripard <mripard@kernel.org>
To: Dmitry Baryshkov <dmitry.baryshkov@linaro.org>
Cc: "Luca Ceresoli" <luca.ceresoli@bootlin.com>,
"Simona Vetter" <simona@ffwll.ch>,
"Inki Dae" <inki.dae@samsung.com>,
"Jagan Teki" <jagan@amarulasolutions.com>,
"Marek Szyprowski" <m.szyprowski@samsung.com>,
"Catalin Marinas" <catalin.marinas@arm.com>,
"Will Deacon" <will@kernel.org>,
"Shawn Guo" <shawnguo@kernel.org>,
"Sascha Hauer" <s.hauer@pengutronix.de>,
"Pengutronix Kernel Team" <kernel@pengutronix.de>,
"Fabio Estevam" <festevam@gmail.com>,
"Daniel Thompson" <danielt@kernel.org>,
"Andrzej Hajda" <andrzej.hajda@intel.com>,
"Jonathan Corbet" <corbet@lwn.net>,
"Paul Kocialkowski" <contact@paulk.fr>,
"Neil Armstrong" <neil.armstrong@linaro.org>,
"Robert Foss" <rfoss@kernel.org>,
"Laurent Pinchart" <Laurent.pinchart@ideasonboard.com>,
"Jonas Karlman" <jonas@kwiboo.se>,
"Jernej Skrabec" <jernej.skrabec@gmail.com>,
"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
"Thomas Zimmermann" <tzimmermann@suse.de>,
"David Airlie" <airlied@gmail.com>,
"Hervé Codina" <herve.codina@bootlin.com>,
"Thomas Petazzoni" <thomas.petazzoni@bootlin.com>,
linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org,
linux-doc@vger.kernel.org,
"Paul Kocialkowski" <paul.kocialkowski@bootlin.com>
Subject: Re: [PATCH v5 04/10] drm/bridge: add documentation of refcounted bridges
Date: Mon, 6 Jan 2025 15:49:48 +0100 [thread overview]
Message-ID: <20250106-quick-exuberant-jellyfish-cddde2@houat> (raw)
In-Reply-To: <CAA8EJprhe4+9HwjW-=4K_LUD5pw51ij_dk0SZABbKH+ExnjdzQ@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 4890 bytes --]
On Mon, Jan 06, 2025 at 02:24:00PM +0200, Dmitry Baryshkov wrote:
> On Mon, 6 Jan 2025 at 12:39, Maxime Ripard <mripard@kernel.org> wrote:
> >
> > Hi,
> >
> > Most of these comments affect your earlier patches, but let's work on
> > the API-level view.
> >
> > On Tue, Dec 31, 2024 at 11:39:58AM +0100, Luca Ceresoli wrote:
> > > + * When using refcounted mode, the driver should allocate ``struct
> > > + * my_bridge`` using regular allocation (as opposed to ``devm_`` or
> > > + * ``drmm_`` allocation), call drm_bridge_init() immediately afterwards to
> > > + * transfer lifecycle management to the DRM bridge core, and implement a
> > > + * ``.destroy`` function to deallocate the ``struct my_bridge``, as in this
> > > + * example::
> > > + *
> > > + * static void my_bridge_destroy(struct drm_bridge *bridge)
> > > + * {
> > > + * kfree(container_of(bridge, struct my_bridge, bridge));
> > > + * }
> > > + *
> > > + * static const struct drm_bridge_funcs my_bridge_funcs = {
> > > + * .destroy = my_bridge_destroy,
> > > + * ...
> > > + * };
> > > + *
> > > + * static int my_bridge_probe(...)
> > > + * {
> > > + * struct my_bridge *mybr;
> > > + * int err;
> > > + *
> > > + * mybr = kzalloc(sizeof(*mybr), GFP_KERNEL);
> > > + * if (!mybr)
> > > + * return -ENOMEM;
> > > + *
> > > + * err = drm_bridge_init(dev, &mybr->bridge, &my_bridge_funcs);
> > > + * if (err)
> > > + * return err;
> > > + *
> > > + * ...
> > > + * drm_bridge_add();
> > > + * ...
> > > + * }
> > > + *
> > > + * static void my_bridge_remove()
> > > + * {
> > > + * struct my_bridge *mybr = ...;
> > > + * drm_bridge_remove(&mybr->bridge);
> > > + * // ... NO kfree here!
> > > + * }
> >
> > I'm a bit worried there, since that API is pretty difficult to get
> > right, and we don't have anything to catch bad patterns.
> >
> > Let's take a step back. What we're trying to solve here is:
> >
> > 1) We want to avoid any dangling pointers to a bridge if the bridge
> > device is removed.
> >
> > 2) To do so, we need to switch to reference counted allocations and
> > pointers.
> >
> > 3) Most bridges structures are allocated through devm_kzalloc, and they
> > one that aren't are freed at remove time anyway, so the allocated
> > structure will be gone when the device is removed.
> >
> > 4) To properly track users, each user that will use a drm_bridge needs
> > to take a reference.
>
> 5) Handle the disappearing next_bridge problem: probe() function gets
> a pointer to the next bridge, but then for some reasons (e.g. because
> of the other device being removed or because of some probe deferral)
> the next_bridge driver gets unbdound and the next_bridge becomes
> unusable before a call to drm_bridge_attach().
Oh, right. We need to plumb it in drm_of_find_bridge somehow too.
> > AFAIU, the destroy introduction and the on-purpose omission of kfree in
> > remove is to solve 3.
> >
> > Introducing a function that allocates the drm_bridge container struct
> > (like drmm_encoder_alloc for example), take a reference, register a devm
> > kfree action, and return the pointer to the driver structure would solve
> > that too pretty nicely.
> >
> > So, something like:
> >
> >
> > struct driver_priv {
> > struct drm_bridge bridge;
> >
> > ...
> > }
> >
> > static int driver_probe(...)
> > {
> > struct driver_priv *priv;
> > struct drm_bridge *bridge;
> >
> > ....
> >
> > priv = devm_drm_bridge_alloc(dev, struct driver_priv, bridge);
>
> Ah... And devm-cleanup will just drop a reference to that data,
> freeing it when all refs are cleaned? Nice idea.
Yup.
> > if (IS_ERR(priv))
> > return ERR_PTR(priv);
> > bridge = &priv->bridge;
> >
> > ...
> >
> > drm_bridge_add(bridge);
> > }
> >
> > Would work just as well.
> >
> > I also don't think we need explicit (at least for the common case)
> > drm_bridge_get and drm_bridge_put calls for bridge users.
> > drm_bridge_attach and drm_bridge_detach can get/put the reference
> > directly.
>
> As I wrote previously, I think drm_bridge_attach() might be too late for that.
> It sounds like drm_of_get_panel_or_bridge() and of_drm_find_bridge
> should increment the refcount, possibly adding a devres action to put
> the reference.
We probably need both. drm_bridge_attach adds the bridge pointer to the
encoder bridge_chain list, so if we had something like
bridge = drm_of_find_bridge();
drm_bridge_attach(encoder, bridge);
drm_bridge_put(bridge);
We could have a dangling pointer.
Maxime
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 273 bytes --]
next prev parent reply other threads:[~2025-01-06 14:49 UTC|newest]
Thread overview: 48+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-31 10:39 [PATCH v5 00/10] Add support for hot-pluggable DRM bridges Luca Ceresoli
2024-12-31 10:39 ` [PATCH v5 01/10] drm/bridge: allow bridges to be informed about added and removed bridges Luca Ceresoli
2024-12-31 10:39 ` [PATCH v5 02/10] drm/encoder: add drm_encoder_cleanup_from() Luca Ceresoli
2024-12-31 10:39 ` [PATCH v5 03/10] drm/bridge: add support for refcounted DRM bridges Luca Ceresoli
2024-12-31 11:11 ` Jani Nikula
2025-01-02 12:03 ` Luca Ceresoli
2025-01-03 9:36 ` Jani Nikula
2024-12-31 10:39 ` [PATCH v5 04/10] drm/bridge: add documentation of refcounted bridges Luca Ceresoli
2024-12-31 17:54 ` Randy Dunlap
2025-01-02 12:02 ` Luca Ceresoli
2025-01-06 10:39 ` Maxime Ripard
2025-01-06 12:24 ` Dmitry Baryshkov
2025-01-06 14:49 ` Maxime Ripard [this message]
2025-01-07 10:35 ` Dmitry Baryshkov
2025-01-07 15:12 ` Maxime Ripard
2025-01-08 15:24 ` Luca Ceresoli
2025-01-08 15:24 ` Luca Ceresoli
2025-01-08 16:02 ` Maxime Ripard
2025-01-22 16:12 ` Luca Ceresoli
2025-01-28 14:49 ` Maxime Ripard
2025-01-29 11:51 ` Luca Ceresoli
2025-01-29 12:22 ` Dmitry Baryshkov
2025-01-29 13:11 ` Luca Ceresoli
2024-12-31 10:39 ` [PATCH v5 05/10] drm/tests: bridge: add KUnit tests for DRM bridges (init and destroy) Luca Ceresoli
2024-12-31 10:40 ` [PATCH v5 06/10] drm/bridge: ti-sn65dsi83: use dynamic lifetime management Luca Ceresoli
2024-12-31 10:40 ` [PATCH v5 07/10] drm/bridge: panel: " Luca Ceresoli
2024-12-31 10:40 ` [PATCH v5 08/10] drm/bridge: samsung-dsim: use supporting variable for out_bridge Luca Ceresoli
2024-12-31 14:57 ` Dmitry Baryshkov
2025-01-02 12:01 ` Luca Ceresoli
2025-01-03 6:00 ` Dmitry Baryshkov
2025-01-10 10:58 ` Luca Ceresoli
2025-01-16 10:32 ` Luca Ceresoli
2025-01-16 10:56 ` Dmitry Baryshkov
2025-01-21 11:27 ` Luca Ceresoli
2025-01-21 11:57 ` Dmitry Baryshkov
2025-01-28 15:52 ` Maxime Ripard
2025-01-16 12:26 ` Maxime Ripard
2025-01-21 11:27 ` Luca Ceresoli
2025-01-28 15:09 ` Maxime Ripard
2025-01-29 11:51 ` Luca Ceresoli
2025-02-04 15:44 ` Maxime Ripard
2024-12-31 10:40 ` [PATCH v5 09/10] drm/bridge: samsung-dsim: refcount the out_bridge Luca Ceresoli
2024-12-31 14:58 ` Dmitry Baryshkov
2024-12-31 10:40 ` [PATCH v5 10/10] drm/bridge: hotplug-bridge: add driver to support hot-pluggable DSI bridges Luca Ceresoli
2024-12-31 15:29 ` Dmitry Baryshkov
2025-01-02 12:01 ` Luca Ceresoli
2025-09-09 15:29 ` Luca Ceresoli
2025-09-09 15:39 ` Luca Ceresoli
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=20250106-quick-exuberant-jellyfish-cddde2@houat \
--to=mripard@kernel.org \
--cc=Laurent.pinchart@ideasonboard.com \
--cc=airlied@gmail.com \
--cc=andrzej.hajda@intel.com \
--cc=catalin.marinas@arm.com \
--cc=contact@paulk.fr \
--cc=corbet@lwn.net \
--cc=danielt@kernel.org \
--cc=dmitry.baryshkov@linaro.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=festevam@gmail.com \
--cc=herve.codina@bootlin.com \
--cc=inki.dae@samsung.com \
--cc=jagan@amarulasolutions.com \
--cc=jernej.skrabec@gmail.com \
--cc=jonas@kwiboo.se \
--cc=kernel@pengutronix.de \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luca.ceresoli@bootlin.com \
--cc=m.szyprowski@samsung.com \
--cc=maarten.lankhorst@linux.intel.com \
--cc=neil.armstrong@linaro.org \
--cc=paul.kocialkowski@bootlin.com \
--cc=rfoss@kernel.org \
--cc=s.hauer@pengutronix.de \
--cc=shawnguo@kernel.org \
--cc=simona@ffwll.ch \
--cc=thomas.petazzoni@bootlin.com \
--cc=tzimmermann@suse.de \
--cc=will@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 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.