Skip to content

Commit 0ca33ad

Browse files
haneensadanvet
authored andcommitted
drm/vkms: Fix race condition around accessing frame number
crtc_state is accessed by both vblank_handle() and the ordered work_struct handle vkms_crc_work_handle() to retrieve and or update the frame number for computed CRC. Since work_struct can fail, add frame_end to account for missing frame numbers. Use (frame_[start/end]) for synchronization between hrtimer callback and ordered work_struct handle. This patch passes the following subtests from igt kms_pipe_crc_basic test: bad-source, read-crc-pipe-A, read-crc-pipe-A-frame-sequence, nonblocking-crc-pipe-A, nonblocking-crc-pipe-A-frame-sequence Signed-off-by: Haneen Mohammed <hamohammed.sa@gmail.com> Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch> Link: https://patchwork.freedesktop.org/patch/msgid/20180903211743.GA2773@haneenDRM
1 parent 428e15c commit 0ca33ad

3 files changed

Lines changed: 54 additions & 6 deletions

File tree

‎drivers/gpu/drm/vkms/vkms_crc.c‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ static uint32_t _vkms_get_crc(struct vkms_crc_data *crc_data)
3434
return crc;
3535
}
3636

37+
/**
38+
* vkms_crc_work_handle - ordered work_struct to compute CRC
39+
*
40+
* @work: work_struct
41+
*
42+
* Work handler for computing CRCs. work_struct scheduled in
43+
* an ordered workqueue that's periodically scheduled to run by
44+
* _vblank_handle() and flushed at vkms_atomic_crtc_destroy_state().
45+
*/
3746
void vkms_crc_work_handle(struct work_struct *work)
3847
{
3948
struct vkms_crtc_state *crtc_state = container_of(work,
@@ -45,8 +54,18 @@ void vkms_crc_work_handle(struct work_struct *work)
4554
output);
4655
struct vkms_crc_data *primary_crc = NULL;
4756
struct drm_plane *plane;
48-
4957
u32 crc32 = 0;
58+
u64 frame_start, frame_end;
59+
unsigned long flags;
60+
61+
spin_lock_irqsave(&out->state_lock, flags);
62+
frame_start = crtc_state->frame_start;
63+
frame_end = crtc_state->frame_end;
64+
spin_unlock_irqrestore(&out->state_lock, flags);
65+
66+
/* _vblank_handle() hasn't updated frame_start yet */
67+
if (!frame_start || frame_start == frame_end)
68+
goto out;
5069

5170
drm_for_each_plane(plane, &vdev->drm) {
5271
struct vkms_plane_state *vplane_state;
@@ -67,7 +86,20 @@ void vkms_crc_work_handle(struct work_struct *work)
6786
if (primary_crc)
6887
crc32 = _vkms_get_crc(primary_crc);
6988

70-
drm_crtc_add_crc_entry(crtc, true, crtc_state->n_frame, &crc32);
89+
frame_end = drm_crtc_accurate_vblank_count(crtc);
90+
91+
/* queue_work can fail to schedule crc_work; add crc for
92+
* missing frames
93+
*/
94+
while (frame_start <= frame_end)
95+
drm_crtc_add_crc_entry(crtc, true, frame_start++, &crc32);
96+
97+
out:
98+
/* to avoid using the same value for frame number again */
99+
spin_lock_irqsave(&out->state_lock, flags);
100+
crtc_state->frame_end = frame_end;
101+
crtc_state->frame_start = 0;
102+
spin_unlock_irqrestore(&out->state_lock, flags);
71103
}
72104

73105
static int vkms_crc_parse_source(const char *src_name, bool *enabled)

‎drivers/gpu/drm/vkms/vkms_crtc.c‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,19 @@ static void _vblank_handle(struct vkms_output *output)
2222
DRM_ERROR("vkms failure on handling vblank");
2323

2424
if (state && output->crc_enabled) {
25-
state->n_frame = drm_crtc_accurate_vblank_count(crtc);
26-
queue_work(output->crc_workq, &state->crc_work);
25+
u64 frame = drm_crtc_accurate_vblank_count(crtc);
26+
27+
/* update frame_start only if a queued vkms_crc_work_handle()
28+
* has read the data
29+
*/
30+
spin_lock(&output->state_lock);
31+
if (!state->frame_start)
32+
state->frame_start = frame;
33+
spin_unlock(&output->state_lock);
34+
35+
ret = queue_work(output->crc_workq, &state->crc_work);
36+
if (!ret)
37+
DRM_WARN("failed to queue vkms_crc_work_handle");
2738
}
2839

2940
spin_unlock(&output->lock);
@@ -211,6 +222,7 @@ int vkms_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
211222
drm_crtc_helper_add(crtc, &vkms_crtc_helper_funcs);
212223

213224
spin_lock_init(&vkms_out->lock);
225+
spin_lock_init(&vkms_out->state_lock);
214226

215227
vkms_out->crc_workq = alloc_ordered_workqueue("vkms_crc_workq", 0);
216228

‎drivers/gpu/drm/vkms/vkms_drv.h‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@ struct vkms_plane_state {
3939
* vkms_crtc_state - Driver specific CRTC state
4040
* @base: base CRTC state
4141
* @crc_work: work struct to compute and add CRC entries
42-
* @n_frame: frame number for computed CRC
42+
* @n_frame_start: start frame number for computed CRC
43+
* @n_frame_end: end frame number for computed CRC
4344
*/
4445
struct vkms_crtc_state {
4546
struct drm_crtc_state base;
4647
struct work_struct crc_work;
47-
unsigned int n_frame;
48+
u64 frame_start;
49+
u64 frame_end;
4850
};
4951

5052
struct vkms_output {
@@ -59,6 +61,8 @@ struct vkms_output {
5961
struct workqueue_struct *crc_workq;
6062
/* protects concurrent access to crc_data */
6163
spinlock_t lock;
64+
/* protects concurrent access to crtc_state */
65+
spinlock_t state_lock;
6266
};
6367

6468
struct vkms_device {

0 commit comments

Comments
 (0)