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=1796220842; b=gt49ucnKqz6FcX71Q+vfjCHMbJ9TCimNCrcHQgAmJW5dJMDRzc7WX9oMPU6L+3C5Dvc1 wT9lOsgs39znFrfDh2T2N/C5yLJ2ffqsWoyKDn3z4gyUQLZ7sd1UljuwDMRh2xWsVUXf+ LB4u3KgHz7bv/zOyOImy3+l6s57u7VrMw/L1yCcCC2WrJevDi2eOHceYo0aGDpDlPz7Sw ciwn2KLtSQrNMyC22sdqNQx5J7eMRXRJK91EhiY5O1E0t1y3l2xJxQyBf8Lgp7J8koxc2 27T1EGv5ZW/g3TV9eGzUQjTGRPEg5YAXNIroC9s3Tlo+G4ODC4dKUR5zGyOCItMSlIg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796220842; h=Received:DKIM-Signature:Subject:To:Cc:From:Date:Message-ID: MIME-Version:Content-Type:Content-Transfer-Encoding; bh=cpLwlp7/HvgWxap0o1TkvI1vwraLpGoadtrDmbBSGq8=; b=x8ksPhnzy9aP/iEhQX9E/vpLigiH/thdRQwMwSGYWxhTvuYAFcRkRXk8zkc/vHobrBUV Fi/wOBikXDy69auotvGIuAi/UJSKzz72NcUnVFAGYhNVF/Or6MK6WKZx2ine/xx6Cir0J rHI/ZZccJV6R0TflcS8d4MX7rhvP0OWgko9VXvybf4f4BgY2FoQtL1p39IOAfIc8S7XxX fkdpzGf+yIknfWyAsSPT8OozUNGeJJpTUScll3B05UzZ9tdrbJEj1m1L0zH7oxqDWiF+K emdVyygRumWusrJUCRKbhSKuUJKChvBG0ZMJa93V09o7Vb97Hd+STlfR/3VE+O6rOJQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=gFC3IrpK; 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 8AC9E606A322 for ; Wed, 2 Dec 2026 14:14:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=gFC3IrpK Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4A30D1F000C1; Wed, 2 Dec 2026 14:14:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796220841; bh=cpLwlp7/HvgWxap0o1TkvI1vwraLpGoadtrDmbBSGq8=; h=Subject:To:Cc:From:Date; b=gFC3IrpKhcEzdqWeoVNy3NBF90sQQIk3HP65Ek1itglpC7fJwF01mgvPkzqeGvBNI toUF5V6P6p9NnnkT7cNWGWD0jEpNz936NbBzt68IM1vxDnVl8j+96XZ5PdJAsytjYI p9tjBeckvW0KkBbyQa7/pzJiZhOy4C5cHjLM9wwg= Subject: FAILED: patch "[PATCH] drm/vc4: Drop BO usecnt before releasing the old FB" failed to apply to 6.6-stable tree To: mcanal@igalia.com,itoral@igalia.com Cc: From: Date: Wed, 02 Dec 2026 13:44:34 +0100 Message-ID: <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 The patch below does not apply to the 6.6-stable tree. If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to . To reproduce the conflict and resubmit, you may use the following commands: git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.6.y git checkout FETCH_HEAD git cherry-pick -x 43b85636ea0d33152bf16816b7601e785723596d # git commit -s git send-email --to '' --in-reply-to '2026120234-unhappy-corned-ef14@gregkh' --subject-prefix 'PATCH 6.6.y' 'HEAD^..' Possible dependencies: thanks, greg k-h ------------------ original commit in Linus's tree ------------------ >From 43b85636ea0d33152bf16816b7601e785723596d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ADra=20Canal?= Date: Sat, 14 Nov 2026 16:16:50 -0300 Subject: [PATCH] drm/vc4: Drop BO usecnt before releasing the old FB MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 diff --git a/drivers/gpu/drm/vc4/vc4_crtc.c b/drivers/gpu/drm/vc4/vc4_crtc.c index f42cd5353230..c558b527c7e6 100644 --- a/drivers/gpu/drm/vc4/vc4_crtc.c +++ b/drivers/gpu/drm/vc4/vc4_crtc.c @@ -895,6 +895,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); @@ -909,39 +910,27 @@ 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) - drm_framebuffer_put(flip_state->old_fb); - - kfree(flip_state); -} - -static void vc4_async_page_flip_complete_with_cleanup(struct dma_fence *fence, - struct dma_fence_cb *cb) -{ - struct vc4_async_flip_state *flip_state = - container_of(cb, struct vc4_async_flip_state, cb); - 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); + /* + * 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->gen == VC4_GEN_4) { + 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); } - vc4_async_page_flip_complete(flip_state); - dma_fence_put(fence); - - /* - * 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); + kfree(flip_state); } static void vc4_async_page_flip_fence_complete(struct dma_fence *fence, @@ -959,30 +948,23 @@ static int vc4_async_set_fence_cb(struct drm_device *dev, { struct drm_framebuffer *fb = flip_state->fb; struct drm_gem_dma_object *dma_bo = drm_fb_dma_get_gem_obj(fb, 0); - dma_fence_func_t async_page_flip_complete_function; - struct vc4_dev *vc4 = to_vc4_dev(dev); struct dma_fence *fence; int ret; - if (vc4->gen == VC4_GEN_4) - async_page_flip_complete_function = vc4_async_page_flip_complete_with_cleanup; - else - async_page_flip_complete_function = vc4_async_page_flip_fence_complete; - ret = dma_resv_get_singleton(dma_bo->base.resv, DMA_RESV_USAGE_READ, &fence); if (ret) return ret; /* If there's no fence, complete the page flip immediately */ if (!fence) { - async_page_flip_complete_function(fence, &flip_state->cb); + vc4_async_page_flip_fence_complete(fence, &flip_state->cb); return 0; } /* If the fence has already been completed, complete the page flip */ if (dma_fence_add_callback(fence, &flip_state->cb, - async_page_flip_complete_function)) - async_page_flip_complete_function(fence, &flip_state->cb); + vc4_async_page_flip_fence_complete)) + vc4_async_page_flip_fence_complete(fence, &flip_state->cb); return 0; }