Skip to content

Commit 6abe93b

Browse files
committed
drm/v3d: Fix race-condition between sysfs/fdinfo and interrupt handler
In V3D, the conclusion of a job is indicated by a IRQ. When a job finishes, then we update the local and the global GPU stats of that queue. But, while the GPU stats are being updated, a user might be reading the stats from sysfs or fdinfo. For example, on `gpu_stats_show()`, we could think about a scenario where `v3d->queue[queue].start_ns != 0`, then an interrupt happens, we update the value of `v3d->queue[queue].start_ns` to 0, we come back to `gpu_stats_show()` to calculate `active_runtime` and now, `active_runtime = timestamp`. In this simple example, the user would see a spike in the queue usage, that didn't match reality. In order to address this issue properly, use a seqcount to protect read and write sections of the code. Fixes: 09a93cc ("drm/v3d: Implement show_fdinfo() callback for GPU usage stats") Reported-by: Tvrtko Ursulin <[email protected]> Signed-off-by: Maíra Canal <[email protected]> Reviewed-by: Tvrtko Ursulin <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/[email protected]
1 parent 12d1624 commit 6abe93b

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

drivers/gpu/drm/v3d/v3d_drv.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ v3d_open(struct drm_device *dev, struct drm_file *file)
121121
1, NULL);
122122

123123
memset(&v3d_priv->stats[i], 0, sizeof(v3d_priv->stats[i]));
124+
seqcount_init(&v3d_priv->stats[i].lock);
124125
}
125126

126127
v3d_perfmon_open_file(v3d_priv);
@@ -145,10 +146,15 @@ v3d_postclose(struct drm_device *dev, struct drm_file *file)
145146
void v3d_get_stats(const struct v3d_stats *stats, u64 timestamp,
146147
u64 *active_runtime, u64 *jobs_completed)
147148
{
148-
*active_runtime = stats->enabled_ns;
149-
if (stats->start_ns)
150-
*active_runtime += timestamp - stats->start_ns;
151-
*jobs_completed = stats->jobs_completed;
149+
unsigned int seq;
150+
151+
do {
152+
seq = read_seqcount_begin(&stats->lock);
153+
*active_runtime = stats->enabled_ns;
154+
if (stats->start_ns)
155+
*active_runtime += timestamp - stats->start_ns;
156+
*jobs_completed = stats->jobs_completed;
157+
} while (read_seqcount_retry(&stats->lock, seq));
152158
}
153159

154160
static void v3d_show_fdinfo(struct drm_printer *p, struct drm_file *file)

drivers/gpu/drm/v3d/v3d_drv.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ struct v3d_stats {
4040
u64 start_ns;
4141
u64 enabled_ns;
4242
u64 jobs_completed;
43+
44+
/*
45+
* This seqcount is used to protect the access to the GPU stats
46+
* variables. It must be used as, while we are reading the stats,
47+
* IRQs can happen and the stats can be updated.
48+
*/
49+
seqcount_t lock;
4350
};
4451

4552
struct v3d_queue_state {

drivers/gpu/drm/v3d/v3d_gem.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ v3d_gem_init(struct drm_device *dev)
251251

252252
queue->fence_context = dma_fence_context_alloc(1);
253253
memset(&queue->stats, 0, sizeof(queue->stats));
254+
seqcount_init(&queue->stats.lock);
254255
}
255256

256257
spin_lock_init(&v3d->mm_lock);

drivers/gpu/drm/v3d/v3d_sched.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,23 @@ v3d_job_start_stats(struct v3d_job *job, enum v3d_queue queue)
114114
struct v3d_stats *local_stats = &file->stats[queue];
115115
u64 now = local_clock();
116116

117+
write_seqcount_begin(&local_stats->lock);
117118
local_stats->start_ns = now;
119+
write_seqcount_end(&local_stats->lock);
120+
121+
write_seqcount_begin(&global_stats->lock);
118122
global_stats->start_ns = now;
123+
write_seqcount_end(&global_stats->lock);
119124
}
120125

121126
static void
122127
v3d_stats_update(struct v3d_stats *stats, u64 now)
123128
{
129+
write_seqcount_begin(&stats->lock);
124130
stats->enabled_ns += now - stats->start_ns;
125131
stats->jobs_completed++;
126132
stats->start_ns = 0;
133+
write_seqcount_end(&stats->lock);
127134
}
128135

129136
void

0 commit comments

Comments
 (0)