From: Maxime Ripard <mripard@kernel.org>
To: Luca Ceresoli <luca.ceresoli@bootlin.com>
Cc: Andrzej Hajda <andrzej.hajda@intel.com>,
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>,
Simona Vetter <simona@ffwll.ch>,
Dmitry Baryshkov <dmitry.baryshkov@oss.qualcomm.com>,
Anusha Srivatsa <asrivats@redhat.com>,
Thomas Petazzoni <thomas.petazzoni@bootlin.com>,
dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 3/3] drm/bridge: add warning for bridges not using devm_drm_bridge_alloc()
Date: Fri, 20 Jun 2025 13:41:48 +0200 [thread overview]
Message-ID: <20250620-adaptable-loutish-oryx-fee75c@houat> (raw)
In-Reply-To: <20250620-drm-bridge-alloc-getput-drm-bridge-c-v1-3-bad7eba5d117@bootlin.com>
[-- Attachment #1: Type: text/plain, Size: 2965 bytes --]
Hi Luca,
On Fri, Jun 20, 2025 at 11:32:08AM +0200, Luca Ceresoli wrote:
> To the best of my knowledge, all drivers in the mainline kernel adding a
> DRM bridge are now converted to using devm_drm_bridge_alloc() for
> allocation and initialization. Among others this ensures initialization of
> the bridge refcount, allowing dynamic allocation lifetime.
>
> devm_drm_bridge_alloc() is now mandatory for all new bridges. Code using
> the old pattern ([devm_]kzalloc + filling the struct fields +
> drm_bridge_add) is not allowed anymore.
>
> Any drivers that might have been missed during the conversion, patches in
> flight towards mainline and out-of-tre drivers still using the old pattern
> will already be caught by a warning looking like:
>
> ------------[ cut here ]------------
> refcount_t: addition on 0; use-after-free.
> WARNING: CPU: 2 PID: 83 at lib/refcount.c:25 refcount_warn_saturate+0x120/0x148
> [...]
> Call trace:
> refcount_warn_saturate+0x120/0x148 (P)
> drm_bridge_get.part.0+0x70/0x98 [drm]
> drm_bridge_add+0x34/0x108 [drm]
> sn65dsi83_probe+0x200/0x480 [ti_sn65dsi83]
> [...]
>
> This warning comes from the refcount code and happens because
> drm_bridge_add() is increasing the refcount, which is uninitialized and
> thus initially zero.
>
> Having a warning and the corresponding stack trace is surely useful, but
> the warning text does not clarify the root problem nor how to fix it.
>
> Add a DRM_WARN() just before increasing the refcount, so the log will be
> much more readable:
>
> [drm] DRM bridge corrupted or not allocated by devm_drm_bridge_alloc()
> ------------[ cut here ]------------
> refcount_t: addition on 0; use-after-free.
> [...etc...]
>
> A DRM_WARN is used because drm_warn and drm_WARN require a struct
> drm_device pointer which is not yet available when adding a bridge.
>
> Do not print the dev_name() in the warning because struct drm_bridge has no
> pointer to the struct device. The affected driver should be easy to catch
> based on the following stack trace however.
>
> Signed-off-by: Luca Ceresoli <luca.ceresoli@bootlin.com>
>
> ---
>
> This patch was added in v8
> ---
> drivers/gpu/drm/drm_bridge.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/drivers/gpu/drm/drm_bridge.c b/drivers/gpu/drm/drm_bridge.c
> index f001bbe95559aabf0aac9f25f89250ad4e1ad9c8..7d511c28608f1d8ea8fbb81d00efa9e227b02a13 100644
> --- a/drivers/gpu/drm/drm_bridge.c
> +++ b/drivers/gpu/drm/drm_bridge.c
> @@ -295,6 +295,9 @@ EXPORT_SYMBOL(__devm_drm_bridge_alloc);
> */
> void drm_bridge_add(struct drm_bridge *bridge)
> {
> + if (kref_read(&bridge->refcount) == 0)
> + DRM_WARN("DRM bridge corrupted or not allocated by devm_drm_bridge_alloc()\n");
> +
I'm fine with it on principle, but I wonder if using bridge->container
is set wouldn't be a more obvious way to check it?
Maxime
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 273 bytes --]
next prev parent reply other threads:[~2025-06-20 11:41 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-20 9:32 [PATCH 0/3] drm/bridge: get/put the bridge reference in drm_bridge.c, warn on old alloc pattern Luca Ceresoli
2025-06-20 9:32 ` [PATCH 1/3] drm/bridge: get/put the bridge reference in drm_bridge_add/remove() Luca Ceresoli
2025-06-20 9:32 ` [PATCH 2/3] drm/bridge: get/put the bridge reference in drm_bridge_attach/detach() Luca Ceresoli
2025-06-20 9:32 ` [PATCH 3/3] drm/bridge: add warning for bridges not using devm_drm_bridge_alloc() Luca Ceresoli
2025-06-20 11:41 ` Maxime Ripard [this message]
2025-06-20 15:57 ` 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=20250620-adaptable-loutish-oryx-fee75c@houat \
--to=mripard@kernel.org \
--cc=Laurent.pinchart@ideasonboard.com \
--cc=airlied@gmail.com \
--cc=andrzej.hajda@intel.com \
--cc=asrivats@redhat.com \
--cc=dmitry.baryshkov@oss.qualcomm.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=jernej.skrabec@gmail.com \
--cc=jonas@kwiboo.se \
--cc=linux-kernel@vger.kernel.org \
--cc=luca.ceresoli@bootlin.com \
--cc=maarten.lankhorst@linux.intel.com \
--cc=neil.armstrong@linaro.org \
--cc=rfoss@kernel.org \
--cc=simona@ffwll.ch \
--cc=thomas.petazzoni@bootlin.com \
--cc=tzimmermann@suse.de \
/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