mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mikko Perttunen <mperttunen@nvidia.com>
To: Thierry Reding <thierry.reding@kernel.org>,
	David Airlie <airlied@gmail.com>, Simona Vetter <simona@ffwll.ch>,
	Jonathan Hunter <jonathanh@nvidia.com>,
	stable@vger.kernel.org
Cc: Svyatoslav Ryhel <clamor95@gmail.com>,
	dri-devel@lists.freedesktop.org, linux-tegra@vger.kernel.org,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org,
	Patrick Fischer <admin@tsv-stahl-riesa.de>,
	admin@tsv-stahl-riesa.de
Subject: Re: [PATCH v2] gpu: host1x: Cancel the fence when a syncpoint wait is interrupted
Date: Thu, 26 Nov 2026 15:47:35 +0900	[thread overview]
Message-ID: <bE28ymIUSh2y3qW_UGpq2g@nvidia.com> (raw)
In-Reply-To: <20261122-upstream-host1x-fence-cancel-v2-1-204760056348@tsv-stahl-riesa.de>

On Sunday, November 22, 2026 6:12 PM Patrick Fischer via B4 Relay wrote:
> From: Patrick Fischer <admin@tsv-stahl-riesa.de>
> 
> host1x_syncpt_wait() creates its fence without the internal 30 second
> timeout and cancels it only when dma_fence_wait_timeout() returns 0.
> If the wait is interrupted, dma_fence_wait_timeout() returns
> -ERESTARTSYS and the fence is left on the syncpoint's fence list,
> holding the reference that host1x_syncpt_fence_enable_signaling() took
> for the interrupt path. It is freed only once the syncpoint reaches the
> threshold, which may never happen.
> 
> Callers that retry on -ERESTARTSYS allocate a new fence each time.
> tegra20_channel_capture_frame() retries in a loop without delay, and
> while the syncpoint does not move, for example with a camera that
> delivers no frames, every round leaks one fence until the system runs
> out of memory. Svyatoslav Ryhel reported this as an out-of-memory crash
> in the VI capture thread; the allocations in his report come from
> host1x_fence_create(), called by host1x_syncpt_wait() from
> tegra20_chan_capture_kthread_start(). The DRM syncpoint wait IOCTLs end
> up in host1x_syncpt_wait() as well, so a process that is signalled while
> waiting leaks a fence the same way.
> 
> Cancel the fence whenever the wait did not succeed. host1x_fence_cancel()
> is safe if the interrupt path has signalled the fence in the meantime,
> as do_fence_timeout() then returns early.
> 
> host1x_fence_cancel() can also run before signalling was enabled on the
> fence.

Well, not before this patch is applied, which is good to clarify, but 
not a big deal.

> fence->list is only initialized in host1x_intr_add_fence_locked(),
> so host1x_intr_remove_fence() would then see a zeroed list head, call
> list_del_init() on it and report the fence as removed, after which
> do_fence_timeout() drops an interrupt path reference that was never
> taken. Initialize the list head in host1x_fence_create() instead.
> host1x_syncpt_wait() does not hit this today, as
> dma_fence_wait_timeout() enables signalling before it checks for a
> pending signal, but the cancel path should not depend on that.
> 
> The leak was found with the help of an LLM assistant reading the host1x
> and dma-fence code against the call chain in the report.
> 
> Fixes: d5179020f5ce ("gpu: host1x: External timeout/cancellation for fences")
> Cc: stable@vger.kernel.org
> Reported-by: Svyatoslav Ryhel <clamor95@gmail.com>
> Signed-off-by: Patrick Fischer <admin@tsv-stahl-riesa.de>
> ---
> Svyatoslav reported the out-of-memory crash, and he confirmed off-list
> that v1 stops the leak on his LG P895 (Tegra30). v2 changes the code,
> so that confirmation does not carry over to it.
> 
> I have not run v2 on hardware. The Tegra Note 7 I work on only
> captures with Svyatoslav's out-of-tree VI/CSI patches, and I have not
> tried to trigger an interrupted wait there. Build-tested for arm and
> arm64. The DRM IOCTL case in the changelog comes from reading the code;
> I have not tried it. The same goes for cancelling a fence before
> signalling was enabled, which no caller does today.
> 
> The retry loop in tegra20_channel_capture_frame() still spins without
> delay as long as the wait keeps returning -ERESTARTSYS. With this patch
> that costs CPU time instead of memory; how the VI driver should handle
> an interrupted wait is a separate question.
> 
> Tool use (see Documentation/process/generated-content.rst): an LLM
> assistant (Claude, in a chat) found the leak by reading
> host1x_syncpt_wait() and the fence code against the call chain in the
> OOM report. Claude Code then checked the finding against the code and
> wrote the change, the changelog and this note from my notes and
> instructions. For v2, the chat checked Mikko's point against the code,
> and Claude Code applied the change and extended the changelog. I
> reviewed the change.
> ---
> Changes in v2:
> - Initialize fence->list in host1x_fence_create() instead of
>   host1x_intr_add_fence_locked(), so that cancelling a fence before
>   signalling was enabled is safe (Mikko Perttunen)
> - Link to v1: https://patch.msgid.link/20261113-upstream-host1x-fence-cancel-v1-1-89f453504222@tsv-stahl-riesa.de
> ---
>  drivers/gpu/host1x/fence.c  | 1 +
>  drivers/gpu/host1x/intr.c   | 2 --
>  drivers/gpu/host1x/syncpt.c | 2 +-
>  3 files changed, 2 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/gpu/host1x/fence.c b/drivers/gpu/host1x/fence.c
> index 1acf579c562b..f77a391b9195 100644
> --- a/drivers/gpu/host1x/fence.c
> +++ b/drivers/gpu/host1x/fence.c
> @@ -134,6 +134,7 @@ struct dma_fence *host1x_fence_create(struct host1x_syncpt *sp, u32 threshold,
>  	fence->sp = sp;
>  	fence->threshold = threshold;
>  	fence->timeout = timeout;
> +	INIT_LIST_HEAD(&fence->list);
>  
>  	dma_fence_init(&fence->base, &host1x_syncpt_fence_ops, &sp->fences.lock,
>  		       dma_fence_context_alloc(1), 0);
> diff --git a/drivers/gpu/host1x/intr.c b/drivers/gpu/host1x/intr.c
> index 30f3dff4487b..380f679b4d7f 100644
> --- a/drivers/gpu/host1x/intr.c
> +++ b/drivers/gpu/host1x/intr.c
> @@ -47,8 +47,6 @@ void host1x_intr_add_fence_locked(struct host1x *host, struct host1x_syncpt_fenc
>  {
>  	struct host1x_fence_list *fence_list = &fence->sp->fences;
>  
> -	INIT_LIST_HEAD(&fence->list);
> -
>  	host1x_intr_add_fence_to_list(fence_list, fence);
>  	host1x_intr_update_hw_state(host, fence->sp);
>  }
> diff --git a/drivers/gpu/host1x/syncpt.c b/drivers/gpu/host1x/syncpt.c
> index fe74ff6fc6f2..f9c871f218f3 100644
> --- a/drivers/gpu/host1x/syncpt.c
> +++ b/drivers/gpu/host1x/syncpt.c
> @@ -247,7 +247,7 @@ int host1x_syncpt_wait(struct host1x_syncpt *sp, u32 thresh, long timeout,
>  		return PTR_ERR(fence);
>  
>  	wait_err = dma_fence_wait_timeout(fence, true, timeout);
> -	if (wait_err == 0)
> +	if (wait_err <= 0)
>  		host1x_fence_cancel(fence);
>  	dma_fence_put(fence);
>  
> 
> ---
> base-commit: 9d66f26fefacebfeed15de31c1123451a17ff119
> change-id: 20261113-upstream-host1x-fence-cancel-0b32753150d6
> 
> Best regards,
> --  
> Patrick Fischer <admin@tsv-stahl-riesa.de>
> 
> 
> 

Reviewed-by: Mikko Perttunen <mperttunen@nvidia.com>




      reply	other threads:[~2026-11-26  6:47 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-22  9:12 Patrick Fischer via B4 Relay
2026-11-26  6:47 ` Mikko Perttunen [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=bE28ymIUSh2y3qW_UGpq2g@nvidia.com \
    --to=mperttunen@nvidia.com \
    --cc=admin@tsv-stahl-riesa.de \
    --cc=airlied@gmail.com \
    --cc=clamor95@gmail.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=jonathanh@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tegra@vger.kernel.org \
    --cc=simona@ffwll.ch \
    --cc=stable@vger.kernel.org \
    --cc=thierry.reding@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

all inboxes | Powered by JetHome®