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=1796380742; b=HSix7eIVxHGq+gPf2rhodwaZNhoNd03ccveOa1XbJWuEVppqUKb7UWgbm10WTc7pQR25 D+AMbSgLo2L9YZwcmmZJX97t6ftM4mEIssq0sDauroRY9nqb0lgnZ+AsObrSwRnNXuqJu 2p5kV84co16Co6ARHyMXSB3FU7IHwi0HY+sL+nVwq+SKwGSRnoyhEwhHQzejyeb7SlHFw 9bj+/ux9JTYyxQF9OGlBpq5dVz+iIb3wAzP6/jHnUIQ/E35rL7mda1vdFIm1HPJJ4PgTg fgj+4AcpkQryOGh/knzfCkyT/W6FRu14Afdb3m771dxvoVqWRlpu5ENn0pfJOatznzQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796380742; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Type:Content-Transfer-Encoding; bh=QRNzH01Ly+OQYlaboL8KI/BedSSZ2fGcuBQbNG+mqIw=; b=vsPfmE+ZgMcEr/COg4nTbdLRMZMF+kZg2yKSpvQGK2351pjacZC23XUgfWdSJ4Xcfjv3 0fTL+JPoKSFskYtzqSCpU8GdQsz+TANscrkI3mvSVutb1xNC7OvOFSXX2KL1RVSS1BTDf TO6t+bmp3lvrymOgLy5UGizIkXBa5Bh6izggcPFwZcrwmXodVTe3an00nNq5aiyQfTgUb dlwGZWcXlnhyosFDJq0jix/O10X2cOyejN8QJGo77YfpTzBb70ZQrxKAyaenkEVejsHZ6 wHsp9g/IscxB0376h5J3KABPWrtAkRug8C1lhREQ+oK64QXXhcfJwdAcNo+iwqUtwJQ== 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=bnTt+YLC; 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 D03C4606A336; Fri, 4 Dec 2026 10:39: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=bnTt+YLC Received: by smtp.kernel.org (Postfix) with ESMTPSA id 673A21F00936; Fri, 4 Dec 2026 10:39:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796380742; bh=QRNzH01Ly+OQYlaboL8KI/BedSSZ2fGcuBQbNG+mqIw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=bnTt+YLCdIvyDzB8hvMD55YvSOl7nF6s3WuDWYT7onbCILxiwqltWHp/gVgbg8JZZ tBwmkkgMjorbL5yGKgcPDJ5E4hCx5WYIZtbh+Sd04caYhYdw5zNSEbHSjXhpuTOaK/ XxJPaq1LzLTlCN5u37GKE3GMAoukBX9jI1oGGq7Q= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Iago Toral Quiroga , =?UTF-8?q?Ma=C3=ADra=20Canal?= Subject: [PATCH 7.3 406/742] drm/vc4: Complete async page flips from an ordered workqueue Date: Fri, 4 Dec 2026 19:04:30 +0900 Message-ID: <20261204100645.881104756@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore 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 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Maíra Canal commit 12a321af56c436e299414d8c276457dcf387e0cf upstream. Async page flips complete from a dma_fence callback, which runs in the fence signaling context: the VC4 render job IRQ handler on RPi 0-3 since commit e8941ac97f28 ("drm/vc4: Remove BOs seqnos"), and the V3D IRQ handler on RPi 4-5 since commit d19e00ee06a9 ("drm/vc4: crtc: Fix out of order frames during asynchronous page flips"). Completing the flip may sleep: on RPi 0-3, vc4_bo_dec_usecnt() takes bo->madv_lock, and on all platforms, dropping the last framebuffer reference takes fb_lock and can free the BO. When running the kernel with CONFIG_DEBUG_ATOMIC_SLEEP=y on RPi 3, this triggers the following warning: [ 2511.636319] BUG: sleeping function called from invalid context at kernel/locking/mutex.c:623 [ 2511.636667] in_atomic(): 1, irqs_disabled(): 1, non_block: 0, pid: 0, name: swapper/0 [ 2511.636681] preempt_count: 1000002, expected: 0 [ 2511.636689] RCU nest depth: 0, expected: 0 [ 2511.636698] locks held by swapper/0/0: 1, on CPU#0: [ 2511.636711] #0: ffff000037183198 (&fence->inline_lock){-...}-{3:3}, at: dma_fence_signal+0x88/0xec [ 2511.636892] Hardware name: Raspberry Pi 3 Model B Plus Rev 1.3 (DT) [ 2511.636899] Call trace: [ 2511.636991] mutex_lock_nested+0x24/0x30 [ 2511.637002] vc4_bo_dec_usecnt+0x40/0x124 [vc4] [ 2511.637070] vc4_async_page_flip_complete_with_cleanup+0xa0/0xc4 [vc4] [ 2511.637102] dma_fence_signal_timestamp_locked+0x118/0x290 [ 2511.637116] dma_fence_signal+0x9c/0xec [ 2511.637126] vc4_irq_render_job_done+0x24/0x114 [vc4] [ 2511.637158] vc4_irq+0x16c/0x274 [vc4] Make the fence callback only queue a work item on a per-CRTC ordered workqueue and complete the flip from it. Flips with no fence or an already signaled one still complete inline, but only after flushing that workqueue, so they can't complete before earlier flips on the same CRTC whose fence already signaled. Cc: stable@vger.kernel.org Fixes: d19e00ee06a9 ("drm/vc4: crtc: Fix out of order frames during asynchronous page flips") Fixes: e8941ac97f28 ("drm/vc4: Remove BOs seqnos") Reviewed-by: Iago Toral Quiroga Link: https://patch.msgid.link/20261114-vc4-async-flip-fixes-v2-2-d33cbe99ed5d@igalia.com Signed-off-by: Maíra Canal Signed-off-by: Greg Kroah-Hartman --- drivers/gpu/drm/vc4/vc4_crtc.c | 51 +++++++++++++++++++++++++---------------- drivers/gpu/drm/vc4/vc4_drv.h | 6 ++++ 2 files changed, 38 insertions(+), 19 deletions(-) --- a/drivers/gpu/drm/vc4/vc4_crtc.c +++ b/drivers/gpu/drm/vc4/vc4_crtc.c @@ -884,15 +884,18 @@ struct vc4_async_flip_state { struct drm_framebuffer *fb; struct drm_framebuffer *old_fb; struct drm_pending_vblank_event *event; + struct dma_fence *fence; struct dma_fence_cb cb; + struct work_struct work; }; -/* Called when the V3D execution for the BO being flipped to is done, so that - * we can actually update the plane's address to point to it. +/* Executed when the V3D execution for the BO being flipped to is done, so + * that we can actually update the plane's address to point to it. */ -static void -vc4_async_page_flip_complete(struct vc4_async_flip_state *flip_state) +static void vc4_async_page_flip_work(struct work_struct *work) { + struct vc4_async_flip_state *flip_state = + container_of(work, struct vc4_async_flip_state, work); struct drm_crtc *crtc = flip_state->crtc; struct drm_device *dev = crtc->dev; struct vc4_dev *vc4 = to_vc4_dev(dev); @@ -930,17 +933,19 @@ vc4_async_page_flip_complete(struct vc4_ drm_framebuffer_put(flip_state->old_fb); } + dma_fence_put(flip_state->fence); kfree(flip_state); } -static void vc4_async_page_flip_fence_complete(struct dma_fence *fence, - struct dma_fence_cb *cb) +static void vc4_async_page_flip_fence_cb(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_crtc *vc4_crtc = to_vc4_crtc(flip_state->crtc); - vc4_async_page_flip_complete(flip_state); - dma_fence_put(fence); + /* Completing the flip may sleep, so defer it to a worker. */ + queue_work(vc4_crtc->async_flip_wq, &flip_state->work); } static int vc4_async_set_fence_cb(struct drm_device *dev, @@ -948,23 +953,26 @@ static int vc4_async_set_fence_cb(struct { struct drm_framebuffer *fb = flip_state->fb; struct drm_gem_dma_object *dma_bo = drm_fb_dma_get_gem_obj(fb, 0); - struct dma_fence *fence; + struct vc4_crtc *vc4_crtc = to_vc4_crtc(flip_state->crtc); int ret; - ret = dma_resv_get_singleton(dma_bo->base.resv, DMA_RESV_USAGE_READ, &fence); + INIT_WORK(&flip_state->work, vc4_async_page_flip_work); + + ret = dma_resv_get_singleton(dma_bo->base.resv, DMA_RESV_USAGE_READ, + &flip_state->fence); if (ret) return ret; - /* If there's no fence, complete the page flip immediately */ - if (!fence) { - vc4_async_page_flip_fence_complete(fence, &flip_state->cb); + if (flip_state->fence && + !dma_fence_add_callback(flip_state->fence, &flip_state->cb, + vc4_async_page_flip_fence_cb)) return 0; - } - /* If the fence has already been completed, complete the page flip */ - if (dma_fence_add_callback(fence, &flip_state->cb, - vc4_async_page_flip_fence_complete)) - vc4_async_page_flip_fence_complete(fence, &flip_state->cb); + /* No fence or the fence has already signaled. Complete the flip + * immediately, after earlier queued flips. + */ + flush_workqueue(vc4_crtc->async_flip_wq); + vc4_async_page_flip_work(&flip_state->work); return 0; } @@ -990,7 +998,7 @@ vc4_async_page_flip_common(struct drm_cr /* Save the current FB before it's replaced by the new one in * drm_atomic_set_fb_for_plane(). We'll need the old FB in - * vc4_async_page_flip_complete() to decrement the BO usecnt and keep + * vc4_async_page_flip_work() to decrement the BO usecnt and keep * it consistent. * 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() @@ -1397,6 +1405,11 @@ int __vc4_crtc_init(struct drm_device *d drm_crtc_helper_add(crtc, crtc_helper_funcs); + vc4_crtc->async_flip_wq = drmm_alloc_ordered_workqueue(drm, "%s-async-flip", + 0, crtc->name); + if (IS_ERR(vc4_crtc->async_flip_wq)) + return PTR_ERR(vc4_crtc->async_flip_wq); + if (vc4->gen == VC4_GEN_4) { drm_mode_crtc_set_gamma_size(crtc, ARRAY_SIZE(vc4_crtc->lut_r)); drm_crtc_enable_color_mgmt(crtc, 0, false, crtc->gamma_size); --- a/drivers/gpu/drm/vc4/vc4_drv.h +++ b/drivers/gpu/drm/vc4/vc4_drv.h @@ -602,6 +602,12 @@ struct vc4_crtc { * access to that value. */ unsigned int current_hvs_channel; + + /** + * @async_flip_wq: Ordered workqueue completing the CRTC's async + * page flips. + */ + struct workqueue_struct *async_flip_wq; }; #define to_vc4_crtc(_crtc) \