mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Boris Brezillon <boris.brezillon@collabora.com>
To: "Adrián Larumbe" <adrian.larumbe@collabora.com>
Cc: Rob Herring <robh@kernel.org>,
	Steven Price <steven.price@arm.com>,
	Maarten Lankhorst <maarten.lankhorst@linux.intel.com>,
	Maxime Ripard <mripard@kernel.org>,
	Thomas Zimmermann <tzimmermann@suse.de>,
	David Airlie <airlied@gmail.com>, Simona Vetter <simona@ffwll.ch>,
	Faith Ekstrand <faith.ekstrand@collabora.com>,
	"Marty E. Plummer" <hanetzer@startmail.com>,
	Tomeu Vizoso <tomeu@tomeuvizoso.net>,
	Eric Anholt <eric@anholt.net>,
	Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>,
	Robin Murphy <robin.murphy@arm.com>,
	Philipp Zabel <p.zabel@pengutronix.de>,
	dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org,
	Collabora Kernel Team <kernel@collabora.com>,
	Neil Armstrong <neil.armstrong@linaro.org>
Subject: Re: [PATCH v9 15/16] drm/panfrost: Fix races between perfcnt and reset sequence
Date: Wed, 23 Sep 2026 10:08:49 +0200	[thread overview]
Message-ID: <20260923100849.573d4d92@fedora-32.home> (raw)
In-Reply-To: <arLg_vX52EGl-AMN@sobremesa>

On Wed, 23 Sep 2026 02:39:10 +0100
Adrián Larumbe <adrian.larumbe@collabora.com> wrote:

> > > -static int panfrost_perfcnt_dump_locked(struct panfrost_device *pfdev)
> > > +static int panfrost_perfcnt_dump_locked(struct panfrost_device *pfdev, u32 *state)
> > >  {
> > > -	u64 gpuva;
> > > +	struct panfrost_perfcnt *perfcnt = pfdev->perfcnt;
> > > +	u64 gpuva = perfcnt->mapping->mmnode.start << PAGE_SHIFT;
> > >  	int ret;
> > >  
> > > -	reinit_completion(&pfdev->perfcnt->dump_comp);
> > > -	gpuva = pfdev->perfcnt->mapping->mmnode.start << PAGE_SHIFT;
> > > -	gpu_write(pfdev, GPU_PERFCNT_BASE_LO, lower_32_bits(gpuva));
> > > -	gpu_write(pfdev, GPU_PERFCNT_BASE_HI, upper_32_bits(gpuva));
> > > -	gpu_write(pfdev, GPU_INT_CLEAR,
> > > -		  GPU_IRQ_CLEAN_CACHES_COMPLETED |
> > > -		  GPU_IRQ_PERFCNT_SAMPLE_COMPLETED);
> > > -	gpu_write(pfdev, GPU_CMD, GPU_CMD_PERFCNT_SAMPLE);
> > > +	scoped_guard(rwsem_read, &pfdev->reset.lock) {
> > > +		perfcnt->dump_finished = false;
> > > +		*state = 0;
> > > +
> > > +		if (!perfcnt->owns_as_ref) {
> > > +			*state = PANFROST_PERFCNT_SESSION_DEAD;
> > > +			return -EIO;
> > > +		}
> > > +
> > > +		if (perfcnt->reset_happened) {
> > > +			*state = PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET;
> > > +			perfcnt->reset_happened = false;
> > > +		}  
> > 
> > 		*state = perfcnt->state;  
> 
> I'm thinking maybe we don't need to set the ioctl output state here. Even if there was
> a reset, if we recovered from it and didn't lose the AS reference, we might be able to
> go forward as usual. In that case, counters would be measured since the latest reset
> rather than the last successful dump, although maybe this is not an issue.

I think userspace needs to know about INTERRUPTED_BY_RESET, at the very
least. But honestly, I'd keep it simple and propagate the state
regardless of the flags userspace might or might not care about.

> > > +	/*
> > > +	 * Here we release the reset semaphore because perfcnt should not get in the way
> > > +	 * of a HW reset. Besides, a legitimate reset might be issued during the wait.
> > > +	 */
> > >  	ret = wait_for_completion_interruptible_timeout(&pfdev->perfcnt->dump_comp,
> > >  							msecs_to_jiffies(1000));
> > > -	if (!ret)
> > > -		ret = -ETIMEDOUT;
> > > -	else if (ret > 0)
> > > -		ret = 0;
> > > +
> > > +	scoped_guard(rwsem_read, &pfdev->reset.lock) {
> > > +		/* Either sample finished or reset happened */
> > > +		if (ret > 0) {
> > > +			ret = perfcnt->dump_finished ? 0 :
> > > +			      perfcnt->owns_as_ref ? -EAGAIN : -EIO;
> > > +
> > > +		} else if (!ret) {
> > > +			ret = -ETIMEDOUT;
> > > +		}
> > > +
> > > +		if (perfcnt->reset_happened)
> > > +			*state |= PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET;
> > > +		if (!perfcnt->owns_as_ref)
> > > +			*state |= PANFROST_PERFCNT_SESSION_DEAD;
> > > +	}  
> > 
> > 	if (!ret)
> > 		return -ETIMEDOUT;  
> 
> I was doing this check inside the guard because, in the time between the completion
> is signalled and the semaphore locked, another reset could come right through.

I wouldn't worry about late signaling to be honest, the timeout is
1sec, which is way more than I would expect a DUMP request to take in
a normal situation. Now, we might want to propagate the state in that
case too, so it might make sense to return ETIMEDOUT inside the
scoped_guard, after the state has been copied.

> And then because I believed counter values were accumulated rather than reset between
> consecutive dumps, it was best to notify the user as soon as possible, even if that
> meant losing one legitimate frame.
> 
> > 	scoped_guard(rwsem_read, &pfdev->reset.lock) {
> > 		u32 new_state = perfcnt->state;
> > 
> > 		*state |= new_state;
> > 		if (new_state & PANFROST_PERFCNT_SESSION_DEAD)
> > 			return -EIO;
> > 
> > 		perfcnt->state = 0;  
> 
> I think this should not be here, because we're already resetting the state before
> running the PERFCNT_SAMPLE GPU command, and also we need to check it right below.

I think we want to reset in both places, especially if we propagate the
state before the DUMP too, otherwise we might leave the
INTERRUPTED_BY_RESET bit behind even though we already informed
userspace about this event in the previous DUMP request. And yes, the
following test should have been on new_state instead of perfcnt->state,
indeed.

> 
> > 		/* If we faced a reset during our SAMPLE, the user needs to try again. */
> > 		if (perfcnt->state & PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET)
> > 			return -EAGAIN;
> > 	}  
> 
> The main reason I added the panfrost_perfcnt::dump_finished flag was that when a reset happens,
> the GPU IRQ handler might've been already running in response to a finished perfcnt sample
> command. However, the reset sequence might come off first and cancel the completion, leaving
> us with a ret > 0 despite sampling having succeeded.
> 
> I'm thinking rather than trying to sort of patch this up precariously as I did in this revision,
> I'll leave it the way you suggested and address the race between the GPU IRQ handler and the
> reset sequence in a future series.

Yes, that's exactly what needs to be done: reset should synchronize
IRQs, mask/suspend them while the reset sequence is happening, and
unmask them when it's done. As for the completion of a DUMP request
interrupted by a RESET, we want a ret > 0 (AKA no-timeout) in that
case, but the perfcnt::state should be updated prior to that, so that
this function knows exactly what to expect from this completion: if
SESSION_INTERRUPTED_BY_RESET is set, it's likely that the DUMP didn't
complete which is why I return EAGAIN in that case, but we can also have
a dedicated flag for DUMP_COMPLETE if you want to be accurate (when
set, DUMP is effective, when not set EAGAIN).

> 
> > 	return 0;  
> 
> We still need to return -ERESTARTSYS when wait_for_completion_interruptible_timeout()
> is interrupted from UM.

Oh, absolutely.

>
> > > +void panfrost_perfcnt_reset(struct panfrost_device *pfdev)
> > > +{
> > > +	struct panfrost_perfcnt *perfcnt = pfdev->perfcnt;
> > > +
> > > +	if (drm_WARN_ON(&pfdev->base, !perfcnt))
> > > +		return;
> > > +
> > > +	lockdep_assert_held(&pfdev->reset.lock);
> > > +
> > > +	if (!perfcnt->user)
> > > +		return;
> > > +
> > > +	perfcnt->owns_as_ref = !panfrost_perfcnt_hw_enable(pfdev);
> > > +	perfcnt->reset_happened = true;
> > > +	complete(&perfcnt->dump_comp);  
> > 
> > 	/* All active AS are released during the MMU post_reset. */
> > 	perfcnt->owns_as_ref = false;  
> 
> I'm guessing this should go away as soon as I move it into panfrost_perfcnt_hw_enable().

If you reset it at the beginning of panfrost_perfcnt_hw_enable(), sure,
but I think it's easier to assume that when
panfrost_perfcnt_hw_enable() is called, the AS is not owned, and reset
it here instead.

> 
> > 	perfcnt->state |= PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET;
> > 	if (panfrost_perfcnt_hw_enable(pfdev))
> > 		perfcnt->state |= PANFROST_PERFCNT_SESSION_DEAD;
> > 
> > 	/* Unblock pending sample requests. */
> > 	complete(&perfcnt->dump_comp);  
> > > +}

  reply	other threads:[~2026-09-23  8:08 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-11 23:28 [PATCH v9 00/16] Collection of fixes for Panfrost: Perfcnt, RPM, refactorings Adrián Larumbe
2026-09-11 23:28 ` [PATCH v9 01/16] drm/panfrost: Move shrinker initialization and unplug one level down Adrián Larumbe
2026-09-14  8:36   ` Boris Brezillon
2026-09-22 19:48     ` Adrián Larumbe
2026-09-23  6:58       ` Boris Brezillon
2026-09-11 23:28 ` [PATCH v9 02/16] drm/panfrost: Move lock and modparam initialisations into their subsystems Adrián Larumbe
2026-09-11 23:28 ` [PATCH v9 03/16] drm/panfrost: Move debugfs initialisation to relevant subsystems Adrián Larumbe
2026-09-11 23:28 ` [PATCH v9 04/16] drm/panfrost: Skip NULL checks for clock enable/disabling Adrián Larumbe
2026-09-11 23:28 ` [PATCH v9 05/16] drm/panfrost: Consolidate device clock management and reset Adrián Larumbe
2026-09-14  8:45   ` Boris Brezillon
2026-09-22 19:50     ` Adrián Larumbe
2026-09-11 23:28 ` [PATCH v9 06/16] drm/panfrost: Fix PM refcnt and autosuspend issues at device probe/remove Adrián Larumbe
2026-09-14  9:22   ` Boris Brezillon
2026-09-22 19:51     ` Adrián Larumbe
2026-09-23  8:00       ` Boris Brezillon
2026-09-11 23:28 ` [PATCH v9 07/16] drm/panfrost: Explicitly enable MMU interrupts at device init Adrián Larumbe
2026-09-11 23:28 ` [PATCH v9 08/16] drm/panfrost: Move all DRM device initialisation into device_init() Adrián Larumbe
2026-09-14  9:31   ` Boris Brezillon
2026-09-11 23:28 ` [PATCH v9 09/16] drm/panfrost: Add warning messages to fatal error conditions Adrián Larumbe
2026-09-11 23:28 ` [PATCH v9 10/16] drm/panfrost: Add debugfs knob for manually triggering a GPU reset Adrián Larumbe
2026-09-14  9:39   ` Boris Brezillon
2026-09-22 19:54     ` Adrián Larumbe
2026-09-23  7:10       ` Boris Brezillon
2026-09-11 23:28 ` [PATCH v9 11/16] drm/panfrost: Move perfcnt GPU disable sequence into a helper Adrián Larumbe
2026-09-11 23:28 ` [PATCH v9 12/16] drm/panfrost: Skip cache flush/invalidate when enabling perfcnt Adrián Larumbe
2026-09-14  9:41   ` Boris Brezillon
2026-09-11 23:28 ` [PATCH v9 13/16] drm/panfrost: Avoid cache flush after perfcnt sample in fully coherent systems Adrián Larumbe
2026-09-11 23:28 ` [PATCH v9 14/16] drm/panfrost: Introduce a reset lock Adrián Larumbe
2026-09-11 23:28 ` [PATCH v9 15/16] drm/panfrost: Fix races between perfcnt and reset sequence Adrián Larumbe
2026-09-14 10:16   ` Boris Brezillon
2026-09-23  1:39     ` Adrián Larumbe
2026-09-23  8:08       ` Boris Brezillon [this message]
2026-09-11 23:28 ` [PATCH v9 16/16] drm/panfrost: Bump driver minor to reflect new DUMP IOCTL req field Adrián Larumbe
2026-09-14 10:19   ` Boris Brezillon
2026-09-22 19:54     ` Adrián Larumbe

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=20260923100849.573d4d92@fedora-32.home \
    --to=boris.brezillon@collabora.com \
    --cc=adrian.larumbe@collabora.com \
    --cc=airlied@gmail.com \
    --cc=alyssa.rosenzweig@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=eric@anholt.net \
    --cc=faith.ekstrand@collabora.com \
    --cc=hanetzer@startmail.com \
    --cc=kernel@collabora.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=mripard@kernel.org \
    --cc=neil.armstrong@linaro.org \
    --cc=p.zabel@pengutronix.de \
    --cc=robh@kernel.org \
    --cc=robin.murphy@arm.com \
    --cc=simona@ffwll.ch \
    --cc=steven.price@arm.com \
    --cc=tomeu@tomeuvizoso.net \
    --cc=tzimmermann@suse.de \
    /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®