From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from sender4-op-o11.zoho.com (sender4-op-o11.zoho.com [136.143.188.11]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2CB0C383C84 for ; Fri, 28 Aug 2026 20:59:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.11 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787950778; cv=pass; b=PGuMB9W7EaQ38hJKbA4gMHqjpELMP0j1hcOejk+I2u+MEgA/9sD73QKzdhFk8Q3joIZYBo8ubDAJdNjsPYk4pOBRoliM/N4Ox+7rTC+LRLg3d/hSy0WDiWiYVy36tXV4cpY11WLJQgBgpP4QBhNm8QW5FGhD7dOt5kiojrks0GY= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787950778; c=relaxed/simple; bh=RMq/Pg4qpjXkL7BkHA29/oskcfgm3D0lSXfB4M4bDsQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=AQsLMTx5+4H3pSW6cfWaWRsvzB3US4AMQNFNwdqxCB8lsg+ZLT8CBaSgdVOR4uTdhpGUqnMPIrQYzqgWbl/3EFuEMiwhdjj2PcPgE8w9rpNmMNHkP82o5K9DZkWHqNJjSHZUaD40M7QtFQYFmYQ5gZENytKRpbqMMzhzmoPOWQk= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (1024-bit key) header.d=collabora.com header.i=adrian.larumbe@collabora.com header.b=SSdd/lFR; arc=pass smtp.client-ip=136.143.188.11 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=collabora.com header.i=adrian.larumbe@collabora.com header.b="SSdd/lFR" ARC-Seal: i=1; a=rsa-sha256; t=1787950738; cv=none; d=zohomail.com; s=zohoarc; b=khDh1CZMRgYvsjPBghGQ3CM3g4ByHikKRUnT1HUPlHb5U+TcWdAlXH12SfadzjlQdI+LLja+2Z6v0Ym72T4cY1WMS1gKyKT5FCBpCQYuFS/xS66JJrbJn2vZuHKavGcFFC+cf3kOQVvSQrqy3ymJC6TAls9j7Bt5XotFgT1p8AM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1787950738; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=C0089doqZECmAmPICp60ZaprNH+w8kGxAX8UP8ImsrA=; b=YjEleLd1WmqSoPzJ+gPxB0i5EfEWvqLcHFX/t0LSioPIfRnX/3PYz+0bzcNpGxWZpXuZa9VaFNNXct5Nu80jgE34beGHPaWN0mOiTLUl+r/zQgJYPE3L58InTVLPBw0AUhMjAu6g7JBLxZQkgR1qDKR8GUd0iWkI8sMRtVR+hMA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=collabora.com; spf=pass smtp.mailfrom=adrian.larumbe@collabora.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1787950738; s=zohomail; d=collabora.com; i=adrian.larumbe@collabora.com; h=From:From:Date:Date:Subject:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Message-Id:In-Reply-To:To:To:Cc:Cc:Reply-To; bh=C0089doqZECmAmPICp60ZaprNH+w8kGxAX8UP8ImsrA=; b=SSdd/lFR71BsT4nGrPtmZt8KtYUafH48IRGS2YBqKl7zLTGP52yMcy5FwMRz0aab qzXZ1eV4e2eQrvcVwbPIXUAnmZiyf+mdNs1jzUoSDajOy3sYEQoXTV8hIVXTKMOK5mr 5wVcgr10KrXN1X0luUWaP5QPdP2y6MYJUvvbDLpo= Received: by mx.zohomail.com with SMTPS id 1787950736656418.75058282883856; Fri, 28 Aug 2026 13:58:56 -0700 (PDT) From: =?utf-8?q?Adri=C3=A1n_Larumbe?= Date: Fri, 28 Aug 2026 21:56:56 +0100 Subject: [PATCH v7 16/17] drm/panfrost: Fix races between perfcnt and reset sequence Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Message-Id: <20260828-claude-fixes-v7-16-72a13b2c125d@collabora.com> References: <20260828-claude-fixes-v7-0-72a13b2c125d@collabora.com> In-Reply-To: <20260828-claude-fixes-v7-0-72a13b2c125d@collabora.com> To: Boris Brezillon , Rob Herring , Steven Price , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Faith Ekstrand , "Marty E. Plummer" , Tomeu Vizoso , Eric Anholt , Alyssa Rosenzweig , Robin Murphy , Philipp Zabel Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Collabora Kernel Team , =?utf-8?q?Adri=C3=A1n_Larumbe?= , Neil Armstrong X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=11853; i=adrian.larumbe@collabora.com; h=from:subject:message-id; bh=RMq/Pg4qpjXkL7BkHA29/oskcfgm3D0lSXfB4M4bDsQ=; b=owEB7QES/pANAwAKAQ4mfkzuU0M9AcsmYgBqkfYuSvFu/0talBl30O7I2xDumk1i8qS8RxzfJ 01KpI565feJAbMEAAEKAB0WIQQyQDDowAUXXfk3B6QOJn5M7lNDPQUCapH2LgAKCRAOJn5M7lND PZ+RDACQEB2bT1uivFf+pZlps4KZA1lGlLQ0u4bYHtlk4xhmVD1jcD3io3tUr1pQwyDLp3TV+Hz isjS+otg0pNM1QSOk1Nn3S1zYr1C9eVt0je3hF2742s7dwoZl8HVug8SGpKvAgVcNRKQ/AljZw3 kEZfHBIXlTLNR0mJmZgTf3FM53mXS6eD557zMjfcRKdRtuC6oIzgpgQBRuS9KL63aOr2OaoUd4q JfPnClwAGLyIwZ0dewGTa/lKAagFgOjPhwASQiOMb4lJ7MD7e0Ark2t10WYZOhjgflFcl2yjrpU 6eMKDi+txGkePmoqwEKUSLRnjjMFIH2GmjEnnpsZQIq436oJrCf29ZhLLBdKXz+W4Mh+mbi39Ht 2Kh4BCJRN65eU3hiAKF4/tRX8U5HOhhyfgqupUAZ9jcsH29e4yQfozCM6jflIVI/SQe8tUH9vIA YDHgkpRrmheqL4Wpwq1pVfxYD+bC8xzB3xsUyizl1rG5vNo4CKiF4qq3t9R4GneBsIZ0o= X-Developer-Key: i=adrian.larumbe@collabora.com; a=openpgp; fpr=324030E8C005175DF93707A40E267E4CEE53433D 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, because that means counters will go back to 0 and can no longer be accumulated to values previously kept in user space. The new perfcnt-aware reset sequence also takes care to reestablish perfcnt to its original configuration if there was an enabled session. Signed-off-by: Adrián Larumbe --- drivers/gpu/drm/panfrost/panfrost_device.c | 1 + drivers/gpu/drm/panfrost/panfrost_perfcnt.c | 189 ++++++++++++++++++++-------- drivers/gpu/drm/panfrost/panfrost_perfcnt.h | 1 + include/uapi/drm/panfrost_drm.h | 3 +- 4 files changed, 140 insertions(+), 54 deletions(-) diff --git a/drivers/gpu/drm/panfrost/panfrost_device.c b/drivers/gpu/drm/panfrost/panfrost_device.c index c8c5dc26b03d..471bd4b037e6 100644 --- a/drivers/gpu/drm/panfrost/panfrost_device.c +++ b/drivers/gpu/drm/panfrost/panfrost_device.c @@ -498,6 +498,7 @@ void panfrost_device_reset(struct panfrost_device *pfdev) panfrost_gpu_power_on(pfdev); panfrost_mmu_reset(pfdev); panfrost_jm_reset_interrupts(pfdev); + panfrost_perfcnt_reset(pfdev); } static int panfrost_device_runtime_resume(struct device *dev) diff --git a/drivers/gpu/drm/panfrost/panfrost_perfcnt.c b/drivers/gpu/drm/panfrost/panfrost_perfcnt.c index b3f71d7fd82a..0564aa2753f9 100644 --- a/drivers/gpu/drm/panfrost/panfrost_perfcnt.c +++ b/drivers/gpu/drm/panfrost/panfrost_perfcnt.c @@ -11,6 +11,7 @@ #include #include #include +#include #include "panfrost_device.h" #include "panfrost_features.h" @@ -25,14 +26,18 @@ #define BYTES_PER_COUNTER 4 #define BLOCKS_PER_COREGROUP 8 #define V4_SHADERS_PER_COREGROUP 4 +#define PERFCNT_DUMP_MAX_RETRIES 5 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; + bool reset_happened; + bool reset_failed; }; static void panfrost_perfcnt_hw_disable(struct panfrost_device *pfdev) @@ -58,25 +63,107 @@ void panfrost_perfcnt_sample_done(struct panfrost_device *pfdev) complete(&pfdev->perfcnt->dump_comp); } -static int panfrost_perfcnt_dump_locked(struct panfrost_device *pfdev) +static int panfrost_perfcnt_hw_enable(struct panfrost_device *pfdev) { - u64 gpuva; + struct panfrost_perfcnt *perfcnt = pfdev->perfcnt; + u32 cfg, as; 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); + ret = panfrost_mmu_as_get(pfdev, perfcnt->mapping->mmu); + if (ret < 0) + return ret; + + as = ret; + cfg = GPU_PERFCNT_CFG_AS(as) | + GPU_PERFCNT_CFG_MODE(GPU_PERFCNT_CFG_MODE_MANUAL); + + /* + * Bifrost GPUs have 2 set of counters, but we're only interested by + * the first one for now. + */ + if (panfrost_model_is_bifrost(pfdev)) + cfg |= GPU_PERFCNT_CFG_SETSEL(perfcnt->counterset); + + gpu_write(pfdev, GPU_PRFCNT_JM_EN, 0xffffffff); + gpu_write(pfdev, GPU_PRFCNT_SHADER_EN, 0xffffffff); + gpu_write(pfdev, GPU_PRFCNT_MMU_L2_EN, 0xffffffff); + + /* + * Due to PRLAM-8186 we need to disable the Tiler before we enable HW + * counters. + */ + if (panfrost_has_hw_issue(pfdev, HW_ISSUE_8186)) + gpu_write(pfdev, GPU_PRFCNT_TILER_EN, 0); + else + gpu_write(pfdev, GPU_PRFCNT_TILER_EN, 0xffffffff); + + gpu_write(pfdev, GPU_PERFCNT_CFG, cfg); + + if (panfrost_has_hw_issue(pfdev, HW_ISSUE_8186)) + gpu_write(pfdev, GPU_PRFCNT_TILER_EN, 0xffffffff); + + return 0; +} + +static int panfrost_perfcnt_dump_locked(struct panfrost_device *pfdev, + u64 *reset_happened) +{ + struct panfrost_perfcnt *perfcnt = pfdev->perfcnt; + u64 gpuva = perfcnt->mapping->mmnode.start << PAGE_SHIFT; + s64 retries = PERFCNT_DUMP_MAX_RETRIES; + bool retry; + int ret; + +dump_retry: + retry = false; + scoped_guard(rwsem_read, &pfdev->reset.lock) { + *reset_happened = perfcnt->reset_happened; + perfcnt->reset_happened = false; + if (perfcnt->reset_failed) { + ret = panfrost_perfcnt_hw_enable(pfdev); + if (ret) + return ret; + perfcnt->reset_failed = false; + } + + 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. The downside is in the gap between the completion is flagged + * by the GPU IRQ handler and the sempahore taken once again, yet another reset + * could happen, which would lead to a valid sample being discarded. This is + * a solution of compromise between ensuring synchronisation with the reset + * sequence and letting it happen as quickly as possible for jobs to be rerun. + */ + 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) { + if (ret > 0) { + if (perfcnt->reset_happened) { + if (--retries >= 0) + retry = true; + else + ret = -EBUSY; + } else { + ret = 0; + } + } else if (!ret) { + ret = -ETIMEDOUT; + } + } + + if (retry) + goto dump_retry; return ret; } @@ -87,9 +174,8 @@ static int panfrost_perfcnt_enable_locked(struct panfrost_device *pfdev, { struct panfrost_file_priv *user = file_priv->driver_priv; struct panfrost_perfcnt *perfcnt = pfdev->perfcnt; - struct iosys_map map; struct drm_gem_shmem_object *bo; - u32 cfg, as; + struct iosys_map map; int ret; if (user == perfcnt->user) @@ -122,7 +208,9 @@ static int panfrost_perfcnt_enable_locked(struct panfrost_device *pfdev, ret = drm_gem_vmap(&bo->base, &map); if (ret) goto err_put_mapping; + perfcnt->buf = map.vaddr; + perfcnt->counterset = counterset; panfrost_gem_internal_set_label(&bo->base, "Perfcnt sample buffer"); @@ -132,44 +220,19 @@ static int panfrost_perfcnt_enable_locked(struct panfrost_device *pfdev, gpu_write(pfdev, GPU_INT_CLEAR, GPU_IRQ_PERFCNT_SAMPLE_COMPLETED); gpu_write(pfdev, GPU_CMD, GPU_CMD_PERFCNT_CLEAR); - ret = panfrost_mmu_as_get(pfdev, perfcnt->mapping->mmu); - if (ret < 0) - goto err_vunmap; - - as = ret; - cfg = GPU_PERFCNT_CFG_AS(as) | - GPU_PERFCNT_CFG_MODE(GPU_PERFCNT_CFG_MODE_MANUAL); - - /* - * Bifrost GPUs have 2 set of counters, but we're only interested by - * the first one for now. - */ - if (panfrost_model_is_bifrost(pfdev)) - cfg |= GPU_PERFCNT_CFG_SETSEL(counterset); - - gpu_write(pfdev, GPU_PRFCNT_JM_EN, 0xffffffff); - gpu_write(pfdev, GPU_PRFCNT_SHADER_EN, 0xffffffff); - gpu_write(pfdev, GPU_PRFCNT_MMU_L2_EN, 0xffffffff); + scoped_guard(rwsem_read, &pfdev->reset.lock) { + ret = panfrost_perfcnt_hw_enable(pfdev); + if (ret) + goto err_vunmap; - /* - * Due to PRLAM-8186 we need to disable the Tiler before we enable HW - * counters. - */ - if (panfrost_has_hw_issue(pfdev, HW_ISSUE_8186)) - gpu_write(pfdev, GPU_PRFCNT_TILER_EN, 0); - else - gpu_write(pfdev, GPU_PRFCNT_TILER_EN, 0xffffffff); - - gpu_write(pfdev, GPU_PERFCNT_CFG, cfg); - - if (panfrost_has_hw_issue(pfdev, HW_ISSUE_8186)) - gpu_write(pfdev, GPU_PRFCNT_TILER_EN, 0xffffffff); + perfcnt->reset_happened = false; + perfcnt->reset_failed = false; + perfcnt->user = user; + } /* The BO ref is retained by the mapping. */ drm_gem_object_put(&bo->base); - perfcnt->user = user; - return 0; err_vunmap: @@ -195,13 +258,16 @@ static int panfrost_perfcnt_disable_locked(struct panfrost_device *pfdev, if (user != perfcnt->user) return -EINVAL; - panfrost_perfcnt_hw_disable(pfdev); + scoped_guard(rwsem_read, &pfdev->reset.lock) { + panfrost_perfcnt_hw_disable(pfdev); + if (!perfcnt->reset_failed) + panfrost_mmu_as_put(pfdev, perfcnt->mapping->mmu); + perfcnt->user = NULL; + } - perfcnt->user = NULL; drm_gem_vunmap(&perfcnt->mapping->obj->base.base, &map); perfcnt->buf = NULL; panfrost_gem_close(&perfcnt->mapping->obj->base.base, file_priv); - panfrost_mmu_as_put(pfdev, perfcnt->mapping->mmu); panfrost_gem_mapping_put(perfcnt->mapping); perfcnt->mapping = NULL; pm_runtime_put_autosuspend(pfdev->base.dev); @@ -255,7 +321,7 @@ int panfrost_ioctl_perfcnt_dump(struct drm_device *dev, void *data, goto out; } - ret = panfrost_perfcnt_dump_locked(pfdev); + ret = panfrost_perfcnt_dump_locked(pfdev, &req->hw_reset); if (ret) goto out; @@ -338,3 +404,20 @@ void panfrost_perfcnt_fini(struct panfrost_device *pfdev) /* Disable everything before leaving. */ panfrost_perfcnt_hw_disable(pfdev); } + +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->reset_failed = !!panfrost_perfcnt_hw_enable(pfdev); + perfcnt->reset_happened = true; + complete(&perfcnt->dump_comp); +} diff --git a/drivers/gpu/drm/panfrost/panfrost_perfcnt.h b/drivers/gpu/drm/panfrost/panfrost_perfcnt.h index 8bbcf5f5fb33..8b9bc704b634 100644 --- a/drivers/gpu/drm/panfrost/panfrost_perfcnt.h +++ b/drivers/gpu/drm/panfrost/panfrost_perfcnt.h @@ -14,5 +14,6 @@ int panfrost_ioctl_perfcnt_enable(struct drm_device *dev, void *data, struct drm_file *file_priv); int panfrost_ioctl_perfcnt_dump(struct drm_device *dev, void *data, struct drm_file *file_priv); +void panfrost_perfcnt_reset(struct panfrost_device *pfdev); #endif diff --git a/include/uapi/drm/panfrost_drm.h b/include/uapi/drm/panfrost_drm.h index 50d5337f35ef..3bbf9220103d 100644 --- a/include/uapi/drm/panfrost_drm.h +++ b/include/uapi/drm/panfrost_drm.h @@ -47,7 +47,7 @@ extern "C" { * them for anything but debugging purpose. */ #define DRM_IOCTL_PANFROST_PERFCNT_ENABLE DRM_IOW(DRM_COMMAND_BASE + DRM_PANFROST_PERFCNT_ENABLE, struct drm_panfrost_perfcnt_enable) -#define DRM_IOCTL_PANFROST_PERFCNT_DUMP DRM_IOW(DRM_COMMAND_BASE + DRM_PANFROST_PERFCNT_DUMP, struct drm_panfrost_perfcnt_dump) +#define DRM_IOCTL_PANFROST_PERFCNT_DUMP DRM_IOWR(DRM_COMMAND_BASE + DRM_PANFROST_PERFCNT_DUMP, struct drm_panfrost_perfcnt_dump) #define PANFROST_JD_REQ_FS (1 << 0) #define PANFROST_JD_REQ_CYCLE_COUNT (1 << 1) @@ -272,6 +272,7 @@ struct drm_panfrost_perfcnt_enable { struct drm_panfrost_perfcnt_dump { __u64 buf_ptr; + __u64 hw_reset; }; /* madvise provides a way to tell the kernel in case a buffers contents -- 2.55.0