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 v10 15/15] drm/panfrost: Fix races between perfcnt and reset sequence
Date: Fri, 25 Sep 2026 10:18:30 +0200 [thread overview]
Message-ID: <20260925101830.25d16466@fedora-32.home> (raw)
In-Reply-To: <20260924-claude-fixes-v10-15-755929b3cc19@collabora.com>
On Thu, 24 Sep 2026 19:09:32 +0100
Adrián Larumbe <adrian.larumbe@collabora.com> wrote:
> Formerly, the reset sequence would race with panfrost_mmu_as_put()
> when tearing down a perfcnt session. On top of that, poking GPU
> registers to program a perfcnt session or obtaining a dump might lead to
> undefined behaviour when done at the same time a reset was ongoing.
>
> Use the reset r/w semaphore to govern access to the hardware at reset
> time. On top of that, expand the DRM uAPI for the perfcnt DUMP operation
> so that userspace can be made aware of a reset having happened. UM needs
> to know about this condition because counter data is inaccurate after
> a reset, so the best approach is simply to try again.
>
> The new perfcnt-aware reset sequence also takes care to reestablish
> perfcnt to its original configuration if there was an enabled session,
> or else flags the current session as dead if that failed.
>
> Also bump DRM driver minor to reflect the new DUMP IOCTL req field.
>
> Fixes: 73e467f60acd ("drm/panfrost: Consolidate reset handling")
> Fixes: 7786fd108777 ("drm/panfrost: Expose performance counters through unstable ioctls")
> Signed-off-by: Adrián Larumbe <adrian.larumbe@collabora.com>
> ---
> drivers/gpu/drm/panfrost/panfrost_device.c | 2 +
> drivers/gpu/drm/panfrost/panfrost_drv.c | 3 +-
> drivers/gpu/drm/panfrost/panfrost_perfcnt.c | 200 ++++++++++++++++++++--------
> drivers/gpu/drm/panfrost/panfrost_perfcnt.h | 1 +
> include/uapi/drm/panfrost_drm.h | 8 +-
> 5 files changed, 155 insertions(+), 59 deletions(-)
>
> diff --git a/drivers/gpu/drm/panfrost/panfrost_device.c b/drivers/gpu/drm/panfrost/panfrost_device.c
> index 801ca07ccb28..98de04362517 100644
> --- a/drivers/gpu/drm/panfrost/panfrost_device.c
> +++ b/drivers/gpu/drm/panfrost/panfrost_device.c
> @@ -483,6 +483,8 @@ void panfrost_device_reset(struct panfrost_device *pfdev, bool enable_job_int)
> panfrost_jm_reset_interrupts(pfdev);
> if (enable_job_int)
> panfrost_jm_enable_interrupts(pfdev);
> +
> + panfrost_perfcnt_reset(pfdev);
> }
>
> static int panfrost_device_runtime_resume(struct device *dev)
> diff --git a/drivers/gpu/drm/panfrost/panfrost_drv.c b/drivers/gpu/drm/panfrost/panfrost_drv.c
> index 571a26b84126..de9b1c115181 100644
> --- a/drivers/gpu/drm/panfrost/panfrost_drv.c
> +++ b/drivers/gpu/drm/panfrost/panfrost_drv.c
> @@ -808,6 +808,7 @@ static const struct file_operations panfrost_drm_driver_fops = {
> * - 1.6 - adds PANFROST_BO_MAP_WB, PANFROST_IOCTL_SYNC_BO,
> * PANFROST_IOCTL_QUERY_BO_INFO and
> * DRM_PANFROST_PARAM_SELECTED_COHERENCY
> + * - 1.7 - adds PERFCNT_DUMP req state field
> */
> static const struct drm_driver panfrost_drm_driver = {
> .driver_features = DRIVER_RENDER | DRIVER_GEM | DRIVER_SYNCOBJ,
> @@ -820,7 +821,7 @@ static const struct drm_driver panfrost_drm_driver = {
> .name = "panfrost",
> .desc = "panfrost DRM",
> .major = 1,
> - .minor = 6,
> + .minor = 7,
>
> .gem_create_object = panfrost_gem_create_object,
> .gem_prime_import = panfrost_gem_prime_import,
> diff --git a/drivers/gpu/drm/panfrost/panfrost_perfcnt.c b/drivers/gpu/drm/panfrost/panfrost_perfcnt.c
> index b3f71d7fd82a..91983d450c24 100644
> --- a/drivers/gpu/drm/panfrost/panfrost_perfcnt.c
> +++ b/drivers/gpu/drm/panfrost/panfrost_perfcnt.c
> @@ -11,6 +11,7 @@
> #include <drm/drm_file.h>
> #include <drm/drm_gem_shmem_helper.h>
> #include <drm/panfrost_drm.h>
> +#include <drm/drm_print.h>
>
> #include "panfrost_device.h"
> #include "panfrost_features.h"
> @@ -28,21 +29,31 @@
>
> struct panfrost_perfcnt {
> struct panfrost_gem_mapping *mapping;
> + unsigned int counterset;
> size_t bosize;
> void *buf;
> struct panfrost_file_priv *user;
> struct mutex lock;
> struct completion dump_comp;
> + unsigned int state;
nit:
u32 state;
to be consistent with the uAPI field.
> + bool owns_as_ref;
> };
>
[...]
> +static int panfrost_perfcnt_dump_locked(struct panfrost_device *pfdev, u32 *state)
> +{
> + struct panfrost_perfcnt *perfcnt = pfdev->perfcnt;
> + u64 gpuva = perfcnt->mapping->mmnode.start << PAGE_SHIFT;
> + int ret;
> +
> + scoped_guard(rwsem_read, &pfdev->reset.lock) {
> + *state = perfcnt->state;
> + if (perfcnt->state & PANFROST_PERFCNT_SESSION_DEAD)
> + return -EIO;
> +
> + perfcnt->state = 0;
> +
> + reinit_completion(&pfdev->perfcnt->dump_comp);
> +
> + 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);
> + }
> +
> + /*
> + * 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));
> +
> + /* A reset might come through in the gap between the completion returning and the following
> + * check, but because no sample was produced, we don't care to relay the state back to UM
> + */
> if (!ret)
> - ret = -ETIMEDOUT;
> - else if (ret > 0)
> - ret = 0;
> + return -ETIMEDOUT;
> +
> + 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;
> +
> + /* If we faced a reset during our SAMPLE, the user needs to try again. */
> + if (new_state & PANFROST_PERFCNT_SESSION_INTERRUPTED_BY_RESET)
> + return -EAGAIN;
Okay, so I think we have a weird situation where sometimes the state
should be ignored when an error is returned (we don't
propagate the state on ETIMEDOUT), and sometimes not (if EAGAIN or EIO
is returned, the state has been propagated and cleared). This is problematic
if INTERRUPTED_BY_RESET is set and EAGAIN is returned, because on the next
DUMP this flag will have disappeared, and if userspace didn't store the
information on the EAGAIN, it wouldn't know the counters are disjoint.
We either need to propagate the state all the time, and document the errno
where userspace should not ignore it (EAGAIN, ETIMEDOUT and EIO), or we need
to preserve the INTERRUPTED_BY_RESET bit when EAGAIN is returned (basically
move the perfcnt->state = 0 below the if (INTERRUPTED_BY_RESET) branch, and
restore the state that was cleared in the first scoped_guard()).
Once addressed, this is
Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com>
prev parent reply other threads:[~2026-09-25 8:18 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-24 18:09 [PATCH v10 00/15] Collection of fixes for Panfrost: Perfcnt, RPM, refactorings Adrián Larumbe
2026-09-24 18:09 ` [PATCH v10 01/15] drm/panfrost: Move shrinker initialization and unplug one level down Adrián Larumbe
2026-09-24 18:09 ` [PATCH v10 02/15] drm/panfrost: Move lock and modparam initialisations into their subsystems Adrián Larumbe
2026-09-24 18:09 ` [PATCH v10 03/15] drm/panfrost: Move debugfs initialisation to relevant subsystems Adrián Larumbe
2026-09-24 18:09 ` [PATCH v10 04/15] drm/panfrost: Skip NULL checks for clock enable/disabling Adrián Larumbe
2026-09-24 18:09 ` [PATCH v10 05/15] drm/panfrost: Consolidate device clock management and reset Adrián Larumbe
2026-09-24 18:09 ` [PATCH v10 06/15] drm/panfrost: Fix PM refcnt and autosuspend issues at device probe/remove Adrián Larumbe
2026-09-25 7:46 ` Boris Brezillon
2026-09-24 18:09 ` [PATCH v10 07/15] drm/panfrost: Explicitly enable MMU interrupts at device init Adrián Larumbe
2026-09-24 18:09 ` [PATCH v10 08/15] drm/panfrost: Move all DRM device initialisation into device_init() Adrián Larumbe
2026-09-24 18:09 ` [PATCH v10 09/15] drm/panfrost: Add warning messages to fatal error conditions Adrián Larumbe
2026-09-24 18:09 ` [PATCH v10 10/15] drm/panfrost: Add debugfs knob for manually triggering a GPU reset Adrián Larumbe
2026-09-24 18:09 ` [PATCH v10 11/15] drm/panfrost: Move perfcnt GPU disable sequence into a helper Adrián Larumbe
2026-09-24 18:09 ` [PATCH v10 12/15] drm/panfrost: Skip cache flush/invalidate when enabling perfcnt Adrián Larumbe
2026-09-24 18:09 ` [PATCH v10 13/15] drm/panfrost: Avoid cache flush after perfcnt sample in fully coherent systems Adrián Larumbe
2026-09-24 18:09 ` [PATCH v10 14/15] drm/panfrost: Introduce a reset lock Adrián Larumbe
2026-09-24 18:09 ` [PATCH v10 15/15] drm/panfrost: Fix races between perfcnt and reset sequence Adrián Larumbe
2026-09-25 8:18 ` Boris Brezillon [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=20260925101830.25d16466@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®