drm/vkms: flush crc workers earlier in commit flow
authorDaniel Vetter <daniel.vetter@ffwll.ch>
Thu, 6 Jun 2019 22:27:47 +0000 (00:27 +0200)
committerRodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Thu, 27 Jun 2019 02:10:35 +0000 (23:10 -0300)
Currently, we flush pending CRC workers very late in the commit flow,
when we destroy all the old crtc states. Unfortunately, at that point,
the framebuffers are already unpinned (and our vaddr possible gone), so
this isn't good. Also, the plane_states we need might also already be
cleaned up, since cleanup order of state structures isn't well defined.

Fix this by waiting for all CRC workers of the old state to complete
before we start any of the cleanup work. For correct ordering and
avoiding races, we can only flush_work after
drm_atomic_helper_wait_for_vblanks() since we know that all subsequent
queue_work will be for the new state. Only once that's done is
flush_work() useful, before that we might flush the work, and then right
after the hrtimer that simulates vblank queues it again. Every time you
have a flush_work before cleaning up the work structure, the following
sequence must be obeyed, or it can go wrong:

1. Make sure no one else can re-queue the work anymore (in our case
that's done by a combination of first updating output->crc_state and
then waiting for the vblank to pass to make sure the hrtimer has noticed
that change).
2. flush_work()
3. Actually clean up stuff (which isn't done here).

Doing the flush_work before we even completed the output->state update,
much less waited for the vblank to make sure that's happened, missed the
point.

Note that this is not yet race-free because of the hrtimer and crc
worker look at the wrong state pointers, but that will be fixed in
subsequent patches.

Cc: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Cc: Haneen Mohammed <hamohammed.sa@gmail.com>
Cc: Daniel Vetter <daniel@ffwll.ch>
Signed-off-by: Daniel Vetter <daniel.vetter@intel.com>
Reviewed-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Tested-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Signed-off-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20190606222751.32567-7-daniel.vetter@ffwll.ch
drivers/gpu/drm/vkms/vkms_crtc.c
drivers/gpu/drm/vkms/vkms_drv.c

index 81bec95f163e44e23a5b970a54ec6dcbba9b4abe..a03f361e2fe04f191009eca40aa1cba6766c6559 100644 (file)
@@ -125,7 +125,7 @@ static void vkms_atomic_crtc_destroy_state(struct drm_crtc *crtc,
        __drm_atomic_helper_crtc_destroy_state(state);
 
        if (vkms_state) {
-               flush_work(&vkms_state->crc_work);
+               WARN_ON(work_pending(&vkms_state->crc_work));
                kfree(vkms_state);
        }
 }
index f677ab1d0094bcf6d2ce68580c2a78d8ad1f9afb..cc53ef88a331f564389a3cfa1285d2bcd8a4c0f7 100644 (file)
@@ -62,6 +62,9 @@ static void vkms_release(struct drm_device *dev)
 static void vkms_atomic_commit_tail(struct drm_atomic_state *old_state)
 {
        struct drm_device *dev = old_state->dev;
+       struct drm_crtc *crtc;
+       struct drm_crtc_state *old_crtc_state;
+       int i;
 
        drm_atomic_helper_commit_modeset_disables(dev, old_state);
 
@@ -75,6 +78,13 @@ static void vkms_atomic_commit_tail(struct drm_atomic_state *old_state)
 
        drm_atomic_helper_wait_for_vblanks(dev, old_state);
 
+       for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
+               struct vkms_crtc_state *vkms_state =
+                       to_vkms_crtc_state(old_crtc_state);
+
+               flush_work(&vkms_state->crc_work);
+       }
+
        drm_atomic_helper_cleanup_planes(dev, old_state);
 }