Skip to contentSkip to conversation

drm/etnaviv: Add GPU reset counters for robustness

PATCH In review Christian Gmeiner 3 participants 7 messages Jul 10

Subsystems (3)
lore
Share & tools
lore

Christian Gmeiner

author reviewer
#0 lore raw

v3 cover drm/etnaviv: Add GPU reset counters for robustness

The OpenGL robustness extensions (GL_KHR_robustness) and Vulkan (VK_ERROR_DEVICE_LOST) let an application detect a GPU reset and check whether its own context caused it, so it can throw away the broken context and build a new one. etnaviv already resets the GPU after a hang, but userspace has no way to learn about it.

This series adds two counters and a RESET_QUERY ioctl that returns both in one call:

- a global counter that counts every reset of a GPU core.
- a context counter that only counts the resets the calling context was guilty of.

Userspace samples both values and compares them later: if the context counter moved the context was guilty, if only the global counter moved the context was an innocent victim. That is all that is needed to implement glGetGraphicsResetStatus() and Vulkan device loss.

The global counter is kept per GPU core and not per device, so a hang on one pipe does not look like an innocent reset to contexts that only use another pipe.

The first patch is preparation: the counters are updated from the scheduler timeout worker, which can race with the DRM file being closed, so struct etnaviv_file_private becomes reference counted and every submit holds a reference.

Link to the Mesa MR implementing the userspace side:
https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/42826

---
Changes in v3:
- Update the reset counters and read them back under the GPU lock, so
the RESET_QUERY ioctl cannot run between the two updates and report
an innocent reset to the guilty context (Lucas).
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Replace the two GET_PARAM values with a dedicated RESET_QUERY ioctl
that returns both counters in one call. The ioctl has a flags field
that must be zero for now, so the query can later be extended to name
a specific context once a DRM file can hold more than one context
(Lucas).
- Make the reset counters plain u32 instead of atomics (Lucas).
- Use __u32 for the ioctl counter fields to match the internal
counters, following i915 GET_RESET_STATS.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Christian Gmeiner (2):
drm/etnaviv: Reference count struct etnaviv_file_private drm/etnaviv: Add GPU reset counters

 drivers/gpu/drm/etnaviv/etnaviv_drv.c        | 46 ++++++++++++++++++++++++++--
 drivers/gpu/drm/etnaviv/etnaviv_drv.h        | 12 ++++++++
 drivers/gpu/drm/etnaviv/etnaviv_gem_submit.c |  5 ++-
 drivers/gpu/drm/etnaviv/etnaviv_gpu.h        |  2 ++
 drivers/gpu/drm/etnaviv/etnaviv_sched.c      |  5 +++
 include/uapi/drm/etnaviv_drm.h               | 19 +++++++++++-
 6 files changed, 85 insertions(+), 4 deletions(-)

---
base-commit: 8cdeaa50eae8dad34885515f62559ee83e7e8dda
change-id: 20260708-etnaviv-reset-notification-b037153a1aab

Best regards,

Signature · 1 line
Christian Gmeiner <[email protected]>
author reviewer
#1 lore raw

PATCH v3 1/2 drm/etnaviv: Reference count struct etnaviv_file_private

From: Christian Gmeiner <[email protected]>

The next commit updates per-context data from the GPU reset path, which runs in the scheduler timeout worker. This can race with closing the DRM
file: drm_sched_entity_flush() only waits until the entity queue is empty, it does not wait for jobs still running on the hardware. So the context can already be freed while the reset path still needs it.

Reference count the context and let every submit hold a reference, the same way a submit already keeps its mmu context and pid alive. No functional change.

---
 drivers/gpu/drm/etnaviv/etnaviv_drv.c        | 17 ++++++++++++++++-
 drivers/gpu/drm/etnaviv/etnaviv_drv.h        | 11 +++++++++++
 drivers/gpu/drm/etnaviv/etnaviv_gem_submit.c |  5 ++++-
 3 files changed, 31 insertions(+), 2 deletions(-)
drivers/gpu/drm/etnaviv/etnaviv_drv.c +16 -1
index 08aca9035fc1..a27ed014fb4e 100644
@@ -60,6 +60,19 @@ static void load_gpu(struct drm_device *dev)
 	}
 }
 
+static void etnaviv_file_private_release(struct kref *kref)
+{
+	struct etnaviv_file_private *ctx =
+		container_of(kref, struct etnaviv_file_private, refcount);
+
+	kfree(ctx);
+}
+
+void etnaviv_file_private_put(struct etnaviv_file_private *ctx)
+{
+	kref_put(&ctx->refcount, etnaviv_file_private_release);
+}
+
 static int etnaviv_open(struct drm_device *dev, struct drm_file *file)
 {
 	struct etnaviv_drm_private *priv = dev->dev_private;
@@ -70,6 +83,8 @@ static int etnaviv_open(struct drm_device *dev, struct drm_file *file)
 	if (!ctx)
 		return -ENOMEM;
 
+	kref_init(&ctx->refcount);
+
 	ret = xa_alloc_cyclic(&priv->active_contexts, &ctx->id, ctx,
 			      xa_limit_32b, &priv->next_context_id, GFP_KERNEL);
 	if (ret < 0)
@@ -120,7 +135,7 @@ static void etnaviv_postclose(struct drm_device *dev, struct drm_file *file)
 
 	xa_erase(&priv->active_contexts, ctx->id);
 
-	kfree(ctx);
+	etnaviv_file_private_put(ctx);
 }
 
 /*
drivers/gpu/drm/etnaviv/etnaviv_drv.h +11 -0
index 55a9e745604d..cba4323ae589 100644
@@ -7,6 +7,7 @@
 #define __ETNAVIV_DRV_H__
 
 #include <linux/io.h>
+#include <linux/kref.h>
 #include <linux/list.h>
 #include <linux/mm_types.h>
 #include <linux/sizes.h>
@@ -29,6 +30,7 @@ struct etnaviv_iommu_global;
 #define ETNAVIV_SOFTPIN_START_ADDRESS	SZ_4M /* must be >= SUBALLOC_SIZE */
 
 struct etnaviv_file_private {
+	struct kref refcount;
 	int id;
 	struct etnaviv_iommu_context	*mmu;
 	struct drm_sched_entity		sched_entity[ETNA_MAX_PIPES];
@@ -53,6 +55,15 @@ struct etnaviv_drm_private {
 	struct etnaviv_cmdbuf *flop_reset_data_ppu;
 };
 
+void etnaviv_file_private_put(struct etnaviv_file_private *ctx);
+
+static inline struct etnaviv_file_private *
+etnaviv_file_private_get(struct etnaviv_file_private *ctx)
+{
+	kref_get(&ctx->refcount);
+	return ctx;
+}
+
 int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
 		struct drm_file *file);
 
drivers/gpu/drm/etnaviv/etnaviv_gem_submit.c +4 -1
index 1a77a09b3377..98f1f59a8b05 100644
@@ -398,6 +398,9 @@ static void submit_cleanup(struct kref *kref)
 
 	put_pid(submit->pid);
 
+	if (submit->ctx)
+		etnaviv_file_private_put(submit->ctx);
+
 	kfree(submit->pmrs);
 	kfree(submit);
 }
@@ -526,7 +529,7 @@ int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
 	if (ret)
 		goto err_submit_put;
 
-	submit->ctx = file->driver_priv;
+	submit->ctx = etnaviv_file_private_get(file->driver_priv);
 	submit->mmu_context = etnaviv_iommu_context_get(submit->ctx->mmu);
 	submit->exec_state = args->exec_state;
 	submit->flags = args->flags;
Signature · 1 line
2.54.0
#2 lore raw

Re: [PATCH v3 1/2] drm/etnaviv: Reference count struct etnaviv_file_private

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] The return value of drm_sched_entity_init() is ignored in etnaviv_open(), leading to a NULL pointer dereference in etnaviv_postclose() if initialization fails. --

--- Patch [1]: [PATCH v3 1/2] drm/etnaviv: Reference count struct etnaviv_file_private --- commit 15b05c3fcbf409d69374c7e633d70b33641f9909
Author: Christian Gmeiner <[email protected]>

drm/etnaviv: Reference count struct etnaviv_file_private

This commit introduces reference counting for the etnaviv context to prevent a use-after-free when a context is closed while still needed by the GPU reset path.

Quoted 4 lines
drivers/gpu/drm/etnaviv/etnaviv_drv.c +0 -0
index 08aca9035fc1a..a27ed014fb4e7 100644

[ ... ]

drivers/gpu/drm/etnaviv/etnaviv_drv.c static int etnaviv_open(struct drm_device *dev, struct drm_file *file) · line 90

Quoted 9 lines
drivers/gpu/drm/etnaviv/etnaviv_drv.c +2 -0
@@ -70,6 +83,8 @@ static int etnaviv_open(struct drm_device *dev, struct drm_file *file)
 	if (!ctx)
 		return -ENOMEM;
 
+	kref_init(&ctx->refcount);
+
 	ret = xa_alloc_cyclic(&priv->active_contexts, &ctx->id, ctx,
 			      xa_limit_32b, &priv->next_context_id, GFP_KERNEL);
 	if (ret < 0)

[Severity: High]
This is a pre-existing issue, but I noticed the return value of drm_sched_entity_init() is ignored during etnaviv_open().

If drm_sched_entity_init() fails (for example, if memory allocation fails inside drm_sched_entity_stats_new()), it leaves the entity partially initialized with a NULL stats pointer.

When the file descriptor is later closed, etnaviv_postclose() unconditionally calls drm_sched_entity_destroy(). Would this sequence result in a crash?

etnaviv_open()

    drm_sched_entity_init(&ctx->sched_entity[i], ...);
    // Error is ignored

etnaviv_postclose()

    drm_sched_entity_destroy(&ctx->sched_entity[i]);
    // Leads to drm_sched_entity_stats_put(entity->stats)

Does this lead to a NULL pointer dereference when kref_put() operates on the NULL stats pointer?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by on v3 1/2
#3 lore raw

Re: [PATCH v3 1/2] drm/etnaviv: Reference count struct etnaviv_file_private

In reply to Christian Gmeiner

Am Freitag, dem 10.07.2026 um 14:03 +0200 schrieb Christian Gmeiner:

Quoted 8 lines

From: Christian Gmeiner <[email protected]>

The next commit updates per-context data from the GPU reset path, which runs in the scheduler timeout worker. This can race with closing the DRM
file: drm_sched_entity_flush() only waits until the entity queue is empty, it does not wait for jobs still running on the hardware. So the context can already be freed while the reset path still needs it.

Quoted 5 lines

Reference count the context and let every submit hold a reference, the same way a submit already keeps its mmu context and pid alive. No functional change.

Quoted 107 lines
---
 drivers/gpu/drm/etnaviv/etnaviv_drv.c        | 17 ++++++++++++++++-
 drivers/gpu/drm/etnaviv/etnaviv_drv.h        | 11 +++++++++++
 drivers/gpu/drm/etnaviv/etnaviv_gem_submit.c |  5 ++++-
 3 files changed, 31 insertions(+), 2 deletions(-)
drivers/gpu/drm/etnaviv/etnaviv_drv.c +16 -1
index 08aca9035fc1..a27ed014fb4e 100644
@@ -60,6 +60,19 @@ static void load_gpu(struct drm_device *dev)
 	}
 }
 
+static void etnaviv_file_private_release(struct kref *kref)
+{
+	struct etnaviv_file_private *ctx =
+		container_of(kref, struct etnaviv_file_private, refcount);
+
+	kfree(ctx);
+}
+
+void etnaviv_file_private_put(struct etnaviv_file_private *ctx)
+{
+	kref_put(&ctx->refcount, etnaviv_file_private_release);
+}
+
 static int etnaviv_open(struct drm_device *dev, struct drm_file *file)
 {
 	struct etnaviv_drm_private *priv = dev->dev_private;
@@ -70,6 +83,8 @@ static int etnaviv_open(struct drm_device *dev, struct drm_file *file)
 	if (!ctx)
 		return -ENOMEM;
 
+	kref_init(&ctx->refcount);
+
 	ret = xa_alloc_cyclic(&priv->active_contexts, &ctx->id, ctx,
 			      xa_limit_32b, &priv->next_context_id, GFP_KERNEL);
 	if (ret < 0)
@@ -120,7 +135,7 @@ static void etnaviv_postclose(struct drm_device *dev, struct drm_file *file)
 
 	xa_erase(&priv->active_contexts, ctx->id);
 
-	kfree(ctx);
+	etnaviv_file_private_put(ctx);
 }
 
 /*
drivers/gpu/drm/etnaviv/etnaviv_drv.h +11 -0
index 55a9e745604d..cba4323ae589 100644
@@ -7,6 +7,7 @@
 #define __ETNAVIV_DRV_H__
 
 #include <linux/io.h>
+#include <linux/kref.h>
 #include <linux/list.h>
 #include <linux/mm_types.h>
 #include <linux/sizes.h>
@@ -29,6 +30,7 @@ struct etnaviv_iommu_global;
 #define ETNAVIV_SOFTPIN_START_ADDRESS	SZ_4M /* must be >= SUBALLOC_SIZE */
 
 struct etnaviv_file_private {
+	struct kref refcount;
 	int id;
 	struct etnaviv_iommu_context	*mmu;
 	struct drm_sched_entity		sched_entity[ETNA_MAX_PIPES];
@@ -53,6 +55,15 @@ struct etnaviv_drm_private {
 	struct etnaviv_cmdbuf *flop_reset_data_ppu;
 };
 
+void etnaviv_file_private_put(struct etnaviv_file_private *ctx);
+
+static inline struct etnaviv_file_private *
+etnaviv_file_private_get(struct etnaviv_file_private *ctx)
+{
+	kref_get(&ctx->refcount);
+	return ctx;
+}
+
 int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
 		struct drm_file *file);
 
drivers/gpu/drm/etnaviv/etnaviv_gem_submit.c +4 -1
index 1a77a09b3377..98f1f59a8b05 100644
@@ -398,6 +398,9 @@ static void submit_cleanup(struct kref *kref)
 
 	put_pid(submit->pid);
 
+	if (submit->ctx)
+		etnaviv_file_private_put(submit->ctx);
+
 	kfree(submit->pmrs);
 	kfree(submit);
 }
@@ -526,7 +529,7 @@ int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data,
 	if (ret)
 		goto err_submit_put;
 
-	submit->ctx = file->driver_priv;
+	submit->ctx = etnaviv_file_private_get(file->driver_priv);
 	submit->mmu_context = etnaviv_iommu_context_get(submit->ctx->mmu);
 	submit->exec_state = args->exec_state;
 	submit->flags = args->flags;
author reviewer
#4 lore raw

PATCH v3 2/2 drm/etnaviv: Add GPU reset counters

In reply to Christian Gmeiner

From: Christian Gmeiner <[email protected]>

The OpenGL and Vulkan robustness extensions let an application detect a GPU reset and check if its own context caused it, so the application can drop the broken context and build a new one. The kernel knows both facts, but etnaviv has no way to report them to userspace.

Add two counters and a RESET_QUERY ioctl that returns both in one call:
a per-GPU counter that counts every reset of that GPU, and a per-context counter that only counts the resets this context was guilty of. Userspace compares the counters with saved values: if the context counter moved the context was guilty, if only the GPU counter moved the context was an innocent victim.

The GPU counter is per GPU core and not per device, so a hang on one pipe does not look like an innocent reset to contexts that only use another pipe.

The timeout handler updates both counters and the ioctl reads both under the GPU lock. Without the lock the query could run between the two updates, see only the GPU counter move and report an innocent reset to the guilty context.

Bump the driver minor version so userspace can detect the feature.

---
 drivers/gpu/drm/etnaviv/etnaviv_drv.c   | 29 ++++++++++++++++++++++++++++-
 drivers/gpu/drm/etnaviv/etnaviv_drv.h   |  1 +
 drivers/gpu/drm/etnaviv/etnaviv_gpu.h   |  2 ++
 drivers/gpu/drm/etnaviv/etnaviv_sched.c |  5 +++++
 include/uapi/drm/etnaviv_drm.h          | 19 ++++++++++++++++++-
 5 files changed, 54 insertions(+), 2 deletions(-)
drivers/gpu/drm/etnaviv/etnaviv_drv.c +28 -1
index a27ed014fb4e..5c50e5a24408 100644
@@ -296,6 +296,32 @@ static int etnaviv_ioctl_get_param(struct drm_device *dev, void *data,
 	return etnaviv_gpu_get_param(gpu, args->param, &args->value);
 }
 
+static int etnaviv_ioctl_reset_query(struct drm_device *dev, void *data,
+		struct drm_file *file)
+{
+	struct etnaviv_drm_private *priv = dev->dev_private;
+	struct etnaviv_file_private *ctx = file->driver_priv;
+	struct drm_etnaviv_reset_query *args = data;
+	struct etnaviv_gpu *gpu;
+
+	if (args->flags)
+		return -EINVAL;
+
+	if (args->pipe >= ETNA_MAX_PIPES)
+		return -EINVAL;
+
+	gpu = priv->gpu[args->pipe];
+	if (!gpu)
+		return -ENXIO;
+
+	mutex_lock(&gpu->lock);
+	args->global_reset_counter = gpu->reset_counter;
+	args->context_reset_counter = ctx->reset_counter;
+	mutex_unlock(&gpu->lock);
+
+	return 0;
+}
+
 static int etnaviv_ioctl_gem_new(struct drm_device *dev, void *data,
 		struct drm_file *file)
 {
@@ -502,6 +528,7 @@ static const struct drm_ioctl_desc etnaviv_ioctls[] = {
 	ETNA_IOCTL(GEM_WAIT,     gem_wait,     DRM_RENDER_ALLOW),
 	ETNA_IOCTL(PM_QUERY_DOM, pm_query_dom, DRM_RENDER_ALLOW),
 	ETNA_IOCTL(PM_QUERY_SIG, pm_query_sig, DRM_RENDER_ALLOW),
+	ETNA_IOCTL(RESET_QUERY,  reset_query,  DRM_RENDER_ALLOW),
 };
 
 static void etnaviv_show_fdinfo(struct drm_printer *p, struct drm_file *file)
@@ -530,7 +557,7 @@ static const struct drm_driver etnaviv_drm_driver = {
 	.name               = "etnaviv",
 	.desc               = "etnaviv DRM",
 	.major              = 1,
-	.minor              = 4,
+	.minor              = 5,
 };
 
 /*
drivers/gpu/drm/etnaviv/etnaviv_drv.h +1 -0
index cba4323ae589..9c348aa7f8d3 100644
@@ -34,6 +34,7 @@ struct etnaviv_file_private {
 	int id;
 	struct etnaviv_iommu_context	*mmu;
 	struct drm_sched_entity		sched_entity[ETNA_MAX_PIPES];
+	u32 reset_counter;
 };
 
 struct etnaviv_drm_private {
drivers/gpu/drm/etnaviv/etnaviv_gpu.h +2 -0
index 5cb46c84e03a..a97780131426 100644
@@ -148,6 +148,8 @@ struct etnaviv_gpu {
 	u32 hangcheck_primid;
 	u32 hangcheck_fence;
 
+	u32 reset_counter;
+
 	void __iomem *mmio;
 	int irq;
 
drivers/gpu/drm/etnaviv/etnaviv_sched.c +5 -0
index 139e6e38784b..6e9b677122e3 100644
@@ -79,6 +79,11 @@ static enum drm_gpu_sched_stat etnaviv_sched_timedout_job(struct drm_sched_job
 	if(sched_job)
 		drm_sched_increase_karma(sched_job);
 
+	mutex_lock(&gpu->lock);
+	gpu->reset_counter++;
+	submit->ctx->reset_counter++;
+	mutex_unlock(&gpu->lock);
+
 	/* get the GPU back into the init state */
 	etnaviv_core_dump(submit);
 	etnaviv_gpu_recover_hang(submit);
include/uapi/drm/etnaviv_drm.h +18 -1
index af024d90453d..b50a3a0799e6 100644
@@ -265,6 +265,21 @@ struct drm_etnaviv_pm_signal {
 	char  name[64];   /* out, name of domain */
 };
 
+/*
+ * Reset status query:
+ *
+ * Both counters start at zero and only ever increase. Userspace saves
+ * both values and compares them on a later query: if the context
+ * counter moved this context caused a reset, if only the global
+ * counter moved the GPU was reset on behalf of another context.
+ */
+struct drm_etnaviv_reset_query {
+	__u32 pipe;			/* in */
+	__u32 flags;			/* in, must be 0 */
+	__u32 global_reset_counter;	/* out, all resets of this GPU core */
+	__u32 context_reset_counter;	/* out, resets caused by this context */
+};
+
 #define DRM_ETNAVIV_GET_PARAM          0x00
 /* placeholder:
 #define DRM_ETNAVIV_SET_PARAM          0x01
@@ -279,7 +294,8 @@ struct drm_etnaviv_pm_signal {
 #define DRM_ETNAVIV_GEM_WAIT           0x09
 #define DRM_ETNAVIV_PM_QUERY_DOM       0x0a
 #define DRM_ETNAVIV_PM_QUERY_SIG       0x0b
-#define DRM_ETNAVIV_NUM_IOCTLS         0x0c
+#define DRM_ETNAVIV_RESET_QUERY        0x0c
+#define DRM_ETNAVIV_NUM_IOCTLS         0x0d
 
 #define DRM_IOCTL_ETNAVIV_GET_PARAM    DRM_IOWR(DRM_COMMAND_BASE + DRM_ETNAVIV_GET_PARAM, struct drm_etnaviv_param)
 #define DRM_IOCTL_ETNAVIV_GEM_NEW      DRM_IOWR(DRM_COMMAND_BASE + DRM_ETNAVIV_GEM_NEW, struct drm_etnaviv_gem_new)
@@ -292,6 +308,7 @@ struct drm_etnaviv_pm_signal {
 #define DRM_IOCTL_ETNAVIV_GEM_WAIT     DRM_IOW(DRM_COMMAND_BASE + DRM_ETNAVIV_GEM_WAIT, struct drm_etnaviv_gem_wait)
 #define DRM_IOCTL_ETNAVIV_PM_QUERY_DOM DRM_IOWR(DRM_COMMAND_BASE + DRM_ETNAVIV_PM_QUERY_DOM, struct drm_etnaviv_pm_domain)
 #define DRM_IOCTL_ETNAVIV_PM_QUERY_SIG DRM_IOWR(DRM_COMMAND_BASE + DRM_ETNAVIV_PM_QUERY_SIG, struct drm_etnaviv_pm_signal)
+#define DRM_IOCTL_ETNAVIV_RESET_QUERY  DRM_IOWR(DRM_COMMAND_BASE + DRM_ETNAVIV_RESET_QUERY, struct drm_etnaviv_reset_query)
 
 #if defined(__cplusplus)
 }
Signature · 1 line
2.54.0
#5 lore raw

Re: [PATCH v3 2/2] drm/etnaviv: Add GPU reset counters

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Data race on `ctx->reset_counter` when a context submits to multiple GPUs. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Data race on ctx->reset_counter when a context submits to multiple GPUs. --

--- Patch [2]: [PATCH v3 2/2] drm/etnaviv: Add GPU reset counters --- commit abb119dbbf6be05ed3e37091e5708fd834c6fb78
Author: Christian Gmeiner <[email protected]>

drm/etnaviv: Add GPU reset counters

This commit introduces two counters for tracking GPU resets: a per-GPU counter and a per-context counter, queryable via a new RESET_QUERY ioctl. Userspace can use these counters to determine if its context was guilty of a reset or just an innocent victim.

Quoted 4 lines
drivers/gpu/drm/etnaviv/etnaviv_drv.c +0 -0
index a27ed014fb4e7..5c50e5a244082 100644

[ ... ]

Quoted 15 lines
drivers/gpu/drm/etnaviv/etnaviv_drv.c +11 -0
@@ -296,6 +296,32 @@ static int etnaviv_ioctl_get_param(struct drm_device *dev, void *data,
 	return etnaviv_gpu_get_param(gpu, args->param, &args->value);
 }
 
+static int etnaviv_ioctl_reset_query(struct drm_device *dev, void *data,
+		struct drm_file *file)
+{
+	struct etnaviv_drm_private *priv = dev->dev_private;
+	struct etnaviv_file_private *ctx = file->driver_priv;
+	struct drm_etnaviv_reset_query *args = data;
+	struct etnaviv_gpu *gpu;
+
+	if (args->flags)
+		return -EINVAL;
+

drivers/gpu/drm/etnaviv/etnaviv_drv.c static int etnaviv_ioctl_get_param(struct drm_device *dev, void *data, · line 319

Quoted 10 lines
drivers/gpu/drm/etnaviv/etnaviv_drv.c +10 -0
+	if (args->pipe >= ETNA_MAX_PIPES)
+		return -EINVAL;
+
+	gpu = priv->gpu[args->pipe];
+	if (!gpu)
+		return -ENXIO;
+
+	mutex_lock(&gpu->lock);
+	args->global_reset_counter = gpu->reset_counter;
+	args->context_reset_counter = ctx->reset_counter;

[Severity: Medium]
Will this read of ctx->reset_counter race with updates on other GPUs?

Since ctx (struct etnaviv_file_private) can be shared across multiple GPU cores, reading it under a single gpu->lock leaves it exposed to concurrent updates from timeout handlers running on different GPUs.

Quoted 4 lines
drivers/gpu/drm/etnaviv/etnaviv_drv.c +4 -0
+	mutex_unlock(&gpu->lock);
+
+	return 0;
+}

[ ... ]

drivers/gpu/drm/etnaviv/etnaviv_drv.h struct etnaviv_file_private { · line 37

Quoted 9 lines
drivers/gpu/drm/etnaviv/etnaviv_drv.h +1 -0
index cba4323ae589c..9c348aa7f8d35 100644
@@ -34,6 +34,7 @@ struct etnaviv_file_private {
 	int id;
 	struct etnaviv_iommu_context	*mmu;
 	struct drm_sched_entity		sched_entity[ETNA_MAX_PIPES];
+	u32 reset_counter;

[Severity: Medium]
Since this context counter is shared across multiple GPU cores and can be updated concurrently if jobs on different GPUs hang simultaneously, does this need to be an atomic_t?

Using a plain u32 for concurrent accesses can lead to data races and lost updates.

Quoted 1 line
drivers/gpu/drm/etnaviv/etnaviv_drv.h +0 -0
 };

[ ... ]

drivers/gpu/drm/etnaviv/etnaviv_sched.c static enum drm_gpu_sched_stat etnaviv_sched_timedout_job(struct drm_sched_job · line 84

Quoted 11 lines
drivers/gpu/drm/etnaviv/etnaviv_sched.c +3 -0
index 139e6e38784b3..6e9b677122e3d 100644
@@ -79,6 +79,11 @@ static enum drm_gpu_sched_stat etnaviv_sched_timedout_job(struct drm_sched_job
 	if(sched_job)
 		drm_sched_increase_karma(sched_job);
 
+	mutex_lock(&gpu->lock);
+	gpu->reset_counter++;
+	submit->ctx->reset_counter++;

[Severity: Medium]
Could this increment of submit->ctx->reset_counter race with other timeout handlers?

The gpu->lock only protects the specific GPU, but the ctx is shared across all pipes. If two GPUs hang simultaneously, two timeout handlers could run concurrently on different workqueues and race while incrementing this plain integer, causing lost updates.

Quoted 5 lines
drivers/gpu/drm/etnaviv/etnaviv_sched.c +2 -0
+	mutex_unlock(&gpu->lock);
+
 	/* get the GPU back into the init state */
 	etnaviv_core_dump(submit);
 	etnaviv_gpu_recover_hang(submit);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer Reviewed-by on v3 2/2
#6 lore raw

Re: [PATCH v3 2/2] drm/etnaviv: Add GPU reset counters

In reply to Christian Gmeiner

Am Freitag, dem 10.07.2026 um 14:03 +0200 schrieb Christian Gmeiner:

Quoted 18 lines

From: Christian Gmeiner <[email protected]>

The OpenGL and Vulkan robustness extensions let an application detect a GPU reset and check if its own context caused it, so the application can drop the broken context and build a new one. The kernel knows both facts, but etnaviv has no way to report them to userspace.

Add two counters and a RESET_QUERY ioctl that returns both in one call:
a per-GPU counter that counts every reset of that GPU, and a per-context counter that only counts the resets this context was guilty of. Userspace compares the counters with saved values: if the context counter moved the context was guilty, if only the GPU counter moved the context was an innocent victim.

The GPU counter is per GPU core and not per device, so a hang on one pipe does not look like an innocent reset to contexts that only use another pipe.

Quoted 8 lines

The timeout handler updates both counters and the ioctl reads both under the GPU lock. Without the lock the query could run between the two updates, see only the GPU counter move and report an innocent reset to the guilty context.

Bump the driver minor version so userspace can detect the feature.

Quoted 147 lines
---
 drivers/gpu/drm/etnaviv/etnaviv_drv.c   | 29 ++++++++++++++++++++++++++++-
 drivers/gpu/drm/etnaviv/etnaviv_drv.h   |  1 +
 drivers/gpu/drm/etnaviv/etnaviv_gpu.h   |  2 ++
 drivers/gpu/drm/etnaviv/etnaviv_sched.c |  5 +++++
 include/uapi/drm/etnaviv_drm.h          | 19 ++++++++++++++++++-
 5 files changed, 54 insertions(+), 2 deletions(-)
drivers/gpu/drm/etnaviv/etnaviv_drv.c +28 -1
index a27ed014fb4e..5c50e5a24408 100644
@@ -296,6 +296,32 @@ static int etnaviv_ioctl_get_param(struct drm_device *dev, void *data,
 	return etnaviv_gpu_get_param(gpu, args->param, &args->value);
 }
 
+static int etnaviv_ioctl_reset_query(struct drm_device *dev, void *data,
+		struct drm_file *file)
+{
+	struct etnaviv_drm_private *priv = dev->dev_private;
+	struct etnaviv_file_private *ctx = file->driver_priv;
+	struct drm_etnaviv_reset_query *args = data;
+	struct etnaviv_gpu *gpu;
+
+	if (args->flags)
+		return -EINVAL;
+
+	if (args->pipe >= ETNA_MAX_PIPES)
+		return -EINVAL;
+
+	gpu = priv->gpu[args->pipe];
+	if (!gpu)
+		return -ENXIO;
+
+	mutex_lock(&gpu->lock);
+	args->global_reset_counter = gpu->reset_counter;
+	args->context_reset_counter = ctx->reset_counter;
+	mutex_unlock(&gpu->lock);
+
+	return 0;
+}
+
 static int etnaviv_ioctl_gem_new(struct drm_device *dev, void *data,
 		struct drm_file *file)
 {
@@ -502,6 +528,7 @@ static const struct drm_ioctl_desc etnaviv_ioctls[] = {
 	ETNA_IOCTL(GEM_WAIT,     gem_wait,     DRM_RENDER_ALLOW),
 	ETNA_IOCTL(PM_QUERY_DOM, pm_query_dom, DRM_RENDER_ALLOW),
 	ETNA_IOCTL(PM_QUERY_SIG, pm_query_sig, DRM_RENDER_ALLOW),
+	ETNA_IOCTL(RESET_QUERY,  reset_query,  DRM_RENDER_ALLOW),
 };
 
 static void etnaviv_show_fdinfo(struct drm_printer *p, struct drm_file *file)
@@ -530,7 +557,7 @@ static const struct drm_driver etnaviv_drm_driver = {
 	.name               = "etnaviv",
 	.desc               = "etnaviv DRM",
 	.major              = 1,
-	.minor              = 4,
+	.minor              = 5,
 };
 
 /*
drivers/gpu/drm/etnaviv/etnaviv_drv.h +1 -0
index cba4323ae589..9c348aa7f8d3 100644
@@ -34,6 +34,7 @@ struct etnaviv_file_private {
 	int id;
 	struct etnaviv_iommu_context	*mmu;
 	struct drm_sched_entity		sched_entity[ETNA_MAX_PIPES];
+	u32 reset_counter;
 };
 
 struct etnaviv_drm_private {
drivers/gpu/drm/etnaviv/etnaviv_gpu.h +2 -0
index 5cb46c84e03a..a97780131426 100644
@@ -148,6 +148,8 @@ struct etnaviv_gpu {
 	u32 hangcheck_primid;
 	u32 hangcheck_fence;
 
+	u32 reset_counter;
+
 	void __iomem *mmio;
 	int irq;
 
drivers/gpu/drm/etnaviv/etnaviv_sched.c +5 -0
index 139e6e38784b..6e9b677122e3 100644
@@ -79,6 +79,11 @@ static enum drm_gpu_sched_stat etnaviv_sched_timedout_job(struct drm_sched_job
 	if(sched_job)
 		drm_sched_increase_karma(sched_job);
 
+	mutex_lock(&gpu->lock);
+	gpu->reset_counter++;
+	submit->ctx->reset_counter++;
+	mutex_unlock(&gpu->lock);
+
 	/* get the GPU back into the init state */
 	etnaviv_core_dump(submit);
 	etnaviv_gpu_recover_hang(submit);
include/uapi/drm/etnaviv_drm.h +18 -1
index af024d90453d..b50a3a0799e6 100644
@@ -265,6 +265,21 @@ struct drm_etnaviv_pm_signal {
 	char  name[64];   /* out, name of domain */
 };
 
+/*
+ * Reset status query:
+ *
+ * Both counters start at zero and only ever increase. Userspace saves
+ * both values and compares them on a later query: if the context
+ * counter moved this context caused a reset, if only the global
+ * counter moved the GPU was reset on behalf of another context.
+ */
+struct drm_etnaviv_reset_query {
+	__u32 pipe;			/* in */
+	__u32 flags;			/* in, must be 0 */
+	__u32 global_reset_counter;	/* out, all resets of this GPU core */
+	__u32 context_reset_counter;	/* out, resets caused by this context */
+};
+
 #define DRM_ETNAVIV_GET_PARAM          0x00
 /* placeholder:
 #define DRM_ETNAVIV_SET_PARAM          0x01
@@ -279,7 +294,8 @@ struct drm_etnaviv_pm_signal {
 #define DRM_ETNAVIV_GEM_WAIT           0x09
 #define DRM_ETNAVIV_PM_QUERY_DOM       0x0a
 #define DRM_ETNAVIV_PM_QUERY_SIG       0x0b
-#define DRM_ETNAVIV_NUM_IOCTLS         0x0c
+#define DRM_ETNAVIV_RESET_QUERY        0x0c
+#define DRM_ETNAVIV_NUM_IOCTLS         0x0d
 
 #define DRM_IOCTL_ETNAVIV_GET_PARAM    DRM_IOWR(DRM_COMMAND_BASE + DRM_ETNAVIV_GET_PARAM, struct drm_etnaviv_param)
 #define DRM_IOCTL_ETNAVIV_GEM_NEW      DRM_IOWR(DRM_COMMAND_BASE + DRM_ETNAVIV_GEM_NEW, struct drm_etnaviv_gem_new)
@@ -292,6 +308,7 @@ struct drm_etnaviv_pm_signal {
 #define DRM_IOCTL_ETNAVIV_GEM_WAIT     DRM_IOW(DRM_COMMAND_BASE + DRM_ETNAVIV_GEM_WAIT, struct drm_etnaviv_gem_wait)
 #define DRM_IOCTL_ETNAVIV_PM_QUERY_DOM DRM_IOWR(DRM_COMMAND_BASE + DRM_ETNAVIV_PM_QUERY_DOM, struct drm_etnaviv_pm_domain)
 #define DRM_IOCTL_ETNAVIV_PM_QUERY_SIG DRM_IOWR(DRM_COMMAND_BASE + DRM_ETNAVIV_PM_QUERY_SIG, struct drm_etnaviv_pm_signal)
+#define DRM_IOCTL_ETNAVIV_RESET_QUERY  DRM_IOWR(DRM_COMMAND_BASE + DRM_ETNAVIV_RESET_QUERY, struct drm_etnaviv_reset_query)
 
 #if defined(__cplusplus)
 }