From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtpout-04.galae.net (smtpout-04.galae.net [185.171.202.116]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 34551396B8D for ; Tue, 17 Mar 2026 08:15:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.171.202.116 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773735341; cv=none; b=dcy0wdE8Uk06P+HzrVjOEobalGs9rcs+qC9RpM+Y3kGZ1SJkWyZOm19Gy4NUXvxF7VmC8ZLX+C16rAEWbmfGOwadnwAOEtpL4LgnYgC5DNZK57LcuNWU1G2L/xnIlT7TxV7CQWM/OhyX+Q2OhaYq9Ns4hh2Vhw8cU8qCB0wcn8A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773735341; c=relaxed/simple; bh=kb3jOVZwHuIYh6aH4OhL/LCcdT6G3yQNoI9Qg4j2fBs=; h=Mime-Version:Content-Type:Date:Message-Id:From:Subject:Cc:To: References:In-Reply-To; b=WuQYeSnQkvBaAMY5VdpE57oxyHdENyEkYvahyNilmDQHJFLZkIU11Q55slezjBV+ml6oJuPyzOdtixVD3PUGsV2VZZvYP+ahz56DAtPb9eVV0PzvAMcALI9EIOTVNfLJT9cvkjn3w3S/dh+UUf5tg5k9c3EJlg7swPHghBWCRro= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=Tbd2EQFo; arc=none smtp.client-ip=185.171.202.116 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="Tbd2EQFo" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-04.galae.net (Postfix) with ESMTPS id 12EC0C55055; Tue, 17 Mar 2026 08:15:59 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 205915FC9A; Tue, 17 Mar 2026 08:15:35 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id E791D1045035D; Tue, 17 Mar 2026 09:15:31 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1773735334; h=from:subject:date:message-id:to:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references; bh=/KvbmOvCMwXMd8Y01EHjxoYBy0bzMdwstlWWPIFixJA=; b=Tbd2EQFoFMYhFFfWZ/sYqS0VcPBZKEnCW6wHWcMHju+728aUy2k7q566VMarfvY1DF0eS9 WlrOoxaN5f7ijKlWgZ0a6D0A3S6bnm+bYPphGVPxhmIibjDcZtvK/yw9wwTchgJzbIw5JN zs1FYosCa9sJuO/tjei56x2rM/uww8DWbKJovouATGzXALSDCGzebdx8QVtsZKjd/LidaN /VDYjiOkUECDLCpsaASXSRSQNqR4JjBaF4uXlkTcC1vkz6Ok5KLRUUD9CFoYWb+eLApHmV PY/CArMvOJlxJY1HJeAc9zPgjElr13dd5d7gtZ4+dYcn1T+kpN0EKZrDxgcPsg== Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Tue, 17 Mar 2026 09:15:31 +0100 Message-Id: From: "Luca Ceresoli" Subject: Re: [PATCH] drm/bridge: Fix refcount shown via debugfs for encoder_bridges_show() Cc: "Marco Felsch" , , To: "Liu Ying" , "Andrzej Hajda" , "Neil Armstrong" , "Robert Foss" , "Laurent Pinchart" , "Jonas Karlman" , "Jernej Skrabec" , "Maarten Lankhorst" , "Maxime Ripard" , "Thomas Zimmermann" , "David Airlie" , "Simona Vetter" X-Mailer: aerc 0.20.1 References: <20260312-drm-misc-next-2026-03-05-fix-encoder-bridges-refcount-v1-1-b9ba3d844732@nxp.com> In-Reply-To: X-Last-TLS-Session-Version: TLSv1.3 On Tue Mar 17, 2026 at 3:35 AM CET, Liu Ying wrote: > On Mon, Mar 16, 2026 at 12:15:29PM +0100, Luca Ceresoli wrote: >> Hello Liu, > > Hello Luca, > >> >> On Thu Mar 12, 2026 at 7:05 AM CET, Liu Ying wrote: >>> A typical bridge refcount value is 3 after a bridge chain is formed: >>> - devm_drm_bridge_alloc() initializes the refcount value to be 1. >>> - drm_bridge_add() gets an additional reference hence 2. >>> - drm_bridge_attach() gets the third reference hence 3. >>> >>> This typical refcount value aligns with allbridges_show()'s behaviour. >>> However, since encoder_bridges_show() uses >>> drm_for_each_bridge_in_chain_scoped() to automatically get/put the >>> bridge reference while iterating, a bogus reference is accidentally >>> got when showing the wrong typical refcount value as 4 to users via >>> debugfs. Fix this by caching the refcount value returned from >>> kref_read() while iterating and explicitly decreasing the cached >>> refcount value by 1 before showing it to users. >>> >>> Fixes: bd57048e4576 ("drm/bridge: use drm_for_each_bridge_in_chain_scop= ed()") >>> Signed-off-by: Liu Ying >>> --- >>> drivers/gpu/drm/drm_bridge.c | 32 ++++++++++++++++++++++++++------ >>> 1 file changed, 26 insertions(+), 6 deletions(-) >>> >>> diff --git a/drivers/gpu/drm/drm_bridge.c b/drivers/gpu/drm/drm_bridge.= c >>> index f8b0333a0a3b..84fc3cfd17e0 100644 >>> --- a/drivers/gpu/drm/drm_bridge.c >>> +++ b/drivers/gpu/drm/drm_bridge.c >>> @@ -1567,14 +1567,18 @@ void devm_drm_put_bridge(struct device *dev, st= ruct drm_bridge *bridge) >>> } >>> EXPORT_SYMBOL(devm_drm_put_bridge); >>> >>> -static void drm_bridge_debugfs_show_bridge(struct drm_printer *p, >>> - struct drm_bridge *bridge, >>> - unsigned int idx, >>> - bool lingering) >>> +static void __drm_bridge_debugfs_show_bridge(struct drm_printer *p, >>> + struct drm_bridge *bridge, >>> + unsigned int idx, >>> + bool lingering, >>> + bool scoped) >>> { >>> + unsigned int refcount =3D kref_read(&bridge->refcount); >>> + >>> drm_printf(p, "bridge[%u]: %ps\n", idx, bridge->funcs); >>> >>> - drm_printf(p, "\trefcount: %u%s\n", kref_read(&bridge->refcount), >>> + drm_printf(p, "\trefcount: %u%s\n", >>> + scoped ? --refcount : refcount, >> >> I'd s/--refcount/refcount - 1/ here, no point in modifying the value whi= le >> printing it. > > Well, maybe there is a point if we consider 'scoped =3D=3D true', which m= eans > one reference should be dropped from the refcount. In the future, if the > refcount is used in this function multiple times, then we don't need to > do 'refcount - 1' for each time with '--refcount'. But, for now, since > the refcount is just used for one time in this function, I'm fine with > either '--refcount' or 'refcount - 1', please let me know your preference= . My preference is to not use the '--' operator. I tend to avoid it in function/macros parameters because it can be tricky with macros, and I admit I had to double check to find out drm_printf() is not a macro (but it could become at some point). So my preference is for 'refcount - 1'. Or, if you prefer, decrement just after the assignment: unsigned int refcount =3D kref_read(&bridge->refcount); + refcount =3D scoped ? refcount - 1 : refcount; But anyway this is a minor detail, go for whatever seems best to you. >>> @@ -1599,6 +1603,22 @@ static void drm_bridge_debugfs_show_bridge(struc= t drm_printer *p, >>> drm_puts(p, "\n"); >>> } >>> >>> +static void drm_bridge_debugfs_show_bridge(struct drm_printer *p, >>> + struct drm_bridge *bridge, >>> + unsigned int idx, >>> + bool lingering) >>> +{ >>> + __drm_bridge_debugfs_show_bridge(p, bridge, idx, lingering, false); >>> +} >>> + >>> +static void drm_bridge_debugfs_show_bridge_scoped(struct drm_printer *= p, >>> + struct drm_bridge *bridge, >>> + unsigned int idx, >>> + bool lingering) >>> +{ >>> + __drm_bridge_debugfs_show_bridge(p, bridge, idx, lingering, true); >>> +} >> >> I think this should be much simpler and avoid a lot of the boilerplate >> code: just add a 'bool scoped' argument to drm_bridge_debugfs_show_bridg= e() >> and pass true/false as applicable. > > Hm, I was thinking how to avoid the two bool arguments(lingering and > scoped) for drm_bridge_debugfs_show_bridge(), because they make a functio= n > call look ugly - people have to go back to the function declaration to > check which bool argument is which. So, I came up with the boilerplate > code, at least any function call has just one 'true' or 'false'. I'm ope= n > to any better idea. If you insist on adding a 'bool scoped' argument to > drm_bridge_debugfs_show_bridge() is a good way to go, then I accept that > and would follow - let me know your thoughts. Here I really think adding a dozen lines of boilerplate code for such a simple think is bad for maintainability/readability. The code is simple enough that two bools (or 1 bool + an int offset) will be readable. If/when the needs will become more complex, code can be made more sophisticated accordingly. Luca -- Luca Ceresoli, Bootlin Embedded Linux and Kernel engineering https://bootlin.com