From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796348249; b=qfLcDQK0Dc1QlOKEAa6KU+vAX8++p1BOmYAovunI7oDq4lJhMTV9eW0i217HPVymH/kw mArlWMq265xcoOy6ynjXsWLpwl/IFdySUlKyYTlDEoXjnFvG/Jx9vBlDIb/KOqejYy+/a rfigqutvAh5/bW1eVYawvYyzIIeDchLjIoDwusflOy/GSqfjxLANqjPkMO3rVkZdqeQfs hCAnoMedT9r8hscruup2H1SvGWnD7/1eauUNLW0d/C77Itr6SqbEvO60/fvp9FK772Bl9 ITzwzVmYeC91VYYec7LhTTXmPgj/Qvyl9YJVQ7lfXLAR1eENd0FUxP3x9iUvGibGdEw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796348249; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=kn9RctcHURNLjKc67nXr87xiRQ5ffgJ59TdQw1cvfMM=; b=RZCKAbnpsMXvR4J3cbo9M1zbS2/6YTlLBO5Hm4Z3X3RnI3O1tgFxlZWBqICoiOFRFVlf ET6prpghPWdJzi0+wjBi9xQwqmvxqaj6f0NBRjxElRIWO/11lTpx/mtlBaGGx59CqM34V BFm8BWbv3JU3qqWSAvXMBee46WsgMgf4hdkZzHPM00P3Z5v2FvXbkfpxxbZpe6RgFsLQW gTUXETUP4htddj0s/XRVP5HX27cvru0l/9KibJMvmHNVXvAt3+qGXNbgiwAuM19w6kcsr RYIjVdC+zyace/US/faZsfDzeG3jwY6uFEXJisE/THSuEgOFvQI0iQRkw2WeC5JPqdQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=jRDVdw+0; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E081E606A322 for ; Fri, 4 Dec 2026 01:37:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=jRDVdw+0 Received: by smtp.kernel.org (Postfix) with ESMTPSA id CE2941F000CE; Fri, 4 Dec 2026 01:37:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796348248; bh=kn9RctcHURNLjKc67nXr87xiRQ5ffgJ59TdQw1cvfMM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=jRDVdw+0B17fSpai2EMOI4OGQVIxfyth5Qo5aMXjFHvCQRNUXhSP1PFT8ZKOSiFHm dZOwXfkQNhMoMc3Yva0JuVUBNZw66E8eFef3jG8smceHB2SxjoESTKWmLvVYv6hR29 QTnXyMcXFxwM7hjyNzf9FdYdJvasKTrR97+twrdvrO1IR5e39RsAie8mBbBBZJG0j8 pkFEinVUod2VFHFW8h7f0LmAw1OlHHLqvCAxnC1nnrCIkuREZrHoVIUB87hAtXSEKc 3EKOUhMmyeV15IwUZ7XY7wCOW90LJB6qw1kw4Qf7BqIMuo8WdhQyv5xD2rVeZMi05V vMSGATxcuW6lQ== From: Sasha Levin To: stable@vger.kernel.org Cc: =?UTF-8?q?Ma=C3=ADra=20Canal?= , Iago Toral Quiroga , Sasha Levin Subject: [PATCH 6.6.y] drm/vc4: Drop BO usecnt before releasing the old FB Date: Thu, 3 Dec 2026 20:37:26 -0500 Message-ID: <20261204013651.1643677-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026120234-unhappy-corned-ef14@gregkh> References: <2026120234-unhappy-corned-ef14@gregkh> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Maíra Canal [ 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 [ 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 Link: https://patch.msgid.link/20261114-vc4-async-flip-fixes-v2-1-d33cbe99ed5d@igalia.com Signed-off-by: Maíra Canal [ sashal: Moved the BO usecnt decrement into the async flip completion before the old FB put, using !vc4->is_vc5 instead of the gen check and skipping the upstream callback unification that depends on missing seqno-removal changes. ] Signed-off-by: Sasha Levin --- 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 8b5a7e5eb1466..f6e0f16c2ab5b 100644 --- a/drivers/gpu/drm/vc4/vc4_crtc.c +++ b/drivers/gpu/drm/vc4/vc4_crtc.c @@ -847,6 +847,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); @@ -861,8 +862,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); } @@ -871,27 +889,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