From: Sasha Levin <sashal@kernel.org>
To: stable@vger.kernel.org
Cc: "Maíra Canal" <mcanal@igalia.com>,
"Iago Toral Quiroga" <itoral@igalia.com>,
"Sasha Levin" <sashal@kernel.org>
Subject: [PATCH 6.1.y] drm/vc4: Drop BO usecnt before releasing the old FB
Date: Thu, 3 Dec 2026 20:41:00 -0500 [thread overview]
Message-ID: <20261204014024.1660897-1-sashal@kernel.org> (raw)
In-Reply-To: <2026120255-smokiness-navigator-6032@gregkh>
From: Maíra Canal <mcanal@igalia.com>
[ Upstream commit 43b85636ea0d33152bf16816b7601e785723596d ]
The async page flip completion drops the old FB reference before
decrementing its BO usecnt. If that was the last reference, the BO is
already freed or recycled into the BO cache when vc4_bo_dec_usecnt()
runs, leading to a use-after-free or a refcount underflow. For example,
while running glmark2-es2-drm in the Raspberry Pi 3, the following
warning appears:
[ 459.078834] ------------[ cut here ]------------
[ 459.078855] refcount_t: underflow; use-after-free.
[ 459.078952] WARNING: lib/refcount.c:96 at refcount_dec_not_one+0xa0/0xa8, CPU#0: swapper/0/0
<snip>
[ 459.079316] Call trace:
[ 459.079323] refcount_dec_not_one+0xa0/0xa8 (P)
[ 459.079335] vc4_bo_dec_usecnt+0x30/0x118 [vc4]
[ 459.079371] vc4_async_page_flip_complete_with_cleanup+0xa0/0xc4 [vc4]
[ 459.079395] dma_fence_signal_timestamp_locked+0xb0/0x12c
[ 459.079408] dma_fence_signal+0x3c/0x6c
[ 459.079416] vc4_irq_render_job_done+0x24/0xbc [vc4]
[ 459.079440] vc4_irq+0x120/0x138 [vc4]
This could happen when userspace removes the old FB and closes its GEM
handle while the flip is still pending.
Decrement the BO usecnt before releasing the old FB reference. Do this
by unifying the two fence callbacks and only decrementing the BO usecnt
in VC4_GEN_4.
Cc: stable@vger.kernel.org
Fixes: 4d12c36fb73b ("drm/vc4: crtc: Move the BO handling out of common page-flip callback")
Reviewed-by: Iago Toral Quiroga <itoral@igalia.com>
Link: https://patch.msgid.link/20261114-vc4-async-flip-fixes-v2-1-d33cbe99ed5d@igalia.com
Signed-off-by: Maíra Canal <mcanal@igalia.com>
[ sashal: Reworked the cleanup to the older tree's structure: uses !vc4->is_vc5 instead of vc4->gen == VC4_GEN_4, and moves the BO usecnt drop into the existing completion helper rather than the fence callbacks. ]
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
drivers/gpu/drm/vc4/vc4_crtc.c | 39 +++++++++++++++++-----------------
1 file changed, 19 insertions(+), 20 deletions(-)
diff --git a/drivers/gpu/drm/vc4/vc4_crtc.c b/drivers/gpu/drm/vc4/vc4_crtc.c
index 7258975331ca7..736c0697e57c6 100644
--- a/drivers/gpu/drm/vc4/vc4_crtc.c
+++ b/drivers/gpu/drm/vc4/vc4_crtc.c
@@ -835,6 +835,7 @@ vc4_async_page_flip_complete(struct vc4_async_flip_state *flip_state)
{
struct drm_crtc *crtc = flip_state->crtc;
struct drm_device *dev = crtc->dev;
+ struct vc4_dev *vc4 = to_vc4_dev(dev);
struct drm_plane *plane = crtc->primary;
vc4_plane_async_set_fb(plane, flip_state->fb);
@@ -849,8 +850,25 @@ vc4_async_page_flip_complete(struct vc4_async_flip_state *flip_state)
drm_crtc_vblank_put(crtc);
drm_framebuffer_put(flip_state->fb);
- if (flip_state->old_fb)
+ if (flip_state->old_fb) {
+ /*
+ * Decrement the BO usecnt in order to keep the inc/dec
+ * calls balanced when the planes are updated through
+ * the async update path.
+ *
+ * FIXME: we should move to generic async-page-flip when
+ * it's available, so that we can get rid of this
+ * hand-made cleanup_fb() logic.
+ */
+ if (!vc4->is_vc5) {
+ struct drm_gem_dma_object *dma_bo =
+ drm_fb_dma_get_gem_obj(flip_state->old_fb, 0);
+
+ vc4_bo_dec_usecnt(to_vc4_bo(&dma_bo->base));
+ }
+
drm_framebuffer_put(flip_state->old_fb);
+ }
kfree(flip_state);
}
@@ -859,27 +877,8 @@ static void vc4_async_page_flip_seqno_complete(struct vc4_seqno_cb *cb)
{
struct vc4_async_flip_state *flip_state =
container_of(cb, struct vc4_async_flip_state, cb.seqno);
- struct vc4_bo *bo = NULL;
-
- if (flip_state->old_fb) {
- struct drm_gem_dma_object *dma_bo =
- drm_fb_dma_get_gem_obj(flip_state->old_fb, 0);
- bo = to_vc4_bo(&dma_bo->base);
- }
vc4_async_page_flip_complete(flip_state);
-
- /*
- * Decrement the BO usecnt in order to keep the inc/dec
- * calls balanced when the planes are updated through
- * the async update path.
- *
- * FIXME: we should move to generic async-page-flip when
- * it's available, so that we can get rid of this
- * hand-made cleanup_fb() logic.
- */
- if (bo)
- vc4_bo_dec_usecnt(bo);
}
static void vc4_async_page_flip_fence_complete(struct dma_fence *fence,
--
2.53.0
prev parent reply other threads:[~2026-12-04 1:41 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 12:44 FAILED: patch "[PATCH] drm/vc4: Drop BO usecnt before releasing the old FB" failed to apply to 6.1-stable tree gregkh
2026-12-04 1:41 ` Sasha Levin [this message]
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=20261204014024.1660897-1-sashal@kernel.org \
--to=sashal@kernel.org \
--cc=itoral@igalia.com \
--cc=mcanal@igalia.com \
--cc=stable@vger.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