CRC capture and writeback share composer_enabled. Cleaning up a writeback
clears this flag even when CRC capture is still active. The composer then
stops producing CRC samples while the CRC data file remains open. Cleanup
of a prepared but uncommitted writeback can also disable composition for
another user.

Replace the shared boolean with a user count protected by the output lock.
Hold one vblank reference for the combined lifetime of all composer users,
refunding redundant references when concurrent first users race. Track
whether each writeback job acquired a composer reference so its cleanup
only releases a reference it owns. Read the user count under the output
lock in the vblank callback and remove the resolved documentation TODO.

The atomic helpers enable the CRTC before issuing writeback, so warn if
acquiring the writeback reference unexpectedly fails. Opening the CRC
data file can race with a CRTC disable, so return any error from
acquiring its composer reference.

The IGT kms_writeback@writeback-preserves-crc regression times out reading
CRCs on unmodified mainline and passes with this change on ARM64 QEMU
with one and two CPUs. Four existing writeback subtests pass on both
kernels. IGT skips the two XRGB2101010 subtests because the format is
unsupported.

Fixes: dbd9d80c1b2e ("drm/vkms: Add support for writeback")
Signed-off-by: Aditya Tipre <[email protected]>
---
Changes in v2:
- Remove the manual fence-error and job-cleanup branch from the writeback
  commit hook. DRM enables the CRTC and makes vblank available before
  dispatching writeback. Record a successful get for job cleanup and warn
  if this invariant fails. Keep CRC-open error propagation (Oarora Etimis).
- Retest the IGT v4 standalone regression and writeback suite with one and
  two CPUs on ARM64 QEMU/HVF, with lockdep and atomic-sleep checks enabled.
  Baseline and v2 use mainline ca20d5a011d3 and the same configuration.
  The focused VKMS W=1 build is clean.

Original submission:
https://lore.kernel.org/r/[email protected]/
Review:
https://lore.kernel.org/r/[email protected]/
IGT regression:
https://lore.kernel.org/r/[email protected]/

 Documentation/gpu/vkms.rst            |  5 ---
 drivers/gpu/drm/vkms/vkms_composer.c  | 47 ++++++++++++++++++++++-----
 drivers/gpu/drm/vkms/vkms_crtc.c      |  5 +--
 drivers/gpu/drm/vkms/vkms_drv.h       |  9 ++---
 drivers/gpu/drm/vkms/vkms_writeback.c |  9 +++--
 5 files changed, 52 insertions(+), 23 deletions(-)

diff --git a/Documentation/gpu/vkms.rst b/Documentation/gpu/vkms.rst
index 
1e79e62a6bc4c478299e38325688cdacba1850ba..fe7dc8def44a95071c033e66f9790946738c56da
 100644
--- a/Documentation/gpu/vkms.rst
+++ b/Documentation/gpu/vkms.rst
@@ -246,11 +246,6 @@ module through configfs. Use/Test-cases:
 Writeback support
 -----------------
 
-- The writeback and CRC capture operations share the use of composer_enabled
-  boolean to ensure vblanks. Probably, when these operations work together,
-  composer_enabled needs to refcounting the composer state to proper work.
-  [Good to get started]
-
 - Add support for cloned writeback outputs and related test cases using a
   cloned output in the IGT kms_writeback.
 
diff --git a/drivers/gpu/drm/vkms/vkms_composer.c 
b/drivers/gpu/drm/vkms/vkms_composer.c
index 
0fc915a954badb04792b0ce180ec95a5242a71e9..5860e86e66b69dcddc54a870a7f84ead9dfce418
 100644
--- a/drivers/gpu/drm/vkms/vkms_composer.c
+++ b/drivers/gpu/drm/vkms/vkms_composer.c
@@ -711,19 +711,44 @@ int vkms_verify_crc_source(struct drm_crtc *crtc, const 
char *src_name,
        return 0;
 }
 
-void vkms_set_composer(struct vkms_output *out, bool enabled)
+int vkms_composer_get(struct vkms_output *out)
 {
-       bool old_enabled;
+       bool already_enabled;
+       int ret;
 
-       if (enabled)
-               drm_crtc_vblank_get(&out->crtc);
+       spin_lock_irq(&out->lock);
+       if (out->composer_users) {
+               out->composer_users++;
+               spin_unlock_irq(&out->lock);
+               return 0;
+       }
+       spin_unlock_irq(&out->lock);
+
+       ret = drm_crtc_vblank_get(&out->crtc);
+       if (ret)
+               return ret;
 
        spin_lock_irq(&out->lock);
-       old_enabled = out->composer_enabled;
-       out->composer_enabled = enabled;
+       already_enabled = out->composer_users != 0;
+       out->composer_users++;
        spin_unlock_irq(&out->lock);
 
-       if (old_enabled)
+       /* Another user may have enabled composition while we got vblank. */
+       if (already_enabled)
+               drm_crtc_vblank_put(&out->crtc);
+
+       return 0;
+}
+
+void vkms_composer_put(struct vkms_output *out)
+{
+       bool last_user;
+
+       spin_lock_irq(&out->lock);
+       last_user = --out->composer_users == 0;
+       spin_unlock_irq(&out->lock);
+
+       if (last_user)
                drm_crtc_vblank_put(&out->crtc);
 }
 
@@ -734,8 +759,12 @@ int vkms_set_crc_source(struct drm_crtc *crtc, const char 
*src_name)
        int ret = 0;
 
        ret = vkms_crc_parse_source(src_name, &enabled);
+       if (ret)
+               return ret;
 
-       vkms_set_composer(out, enabled);
+       if (enabled)
+               return vkms_composer_get(out);
 
-       return ret;
+       vkms_composer_put(out);
+       return 0;
 }
diff --git a/drivers/gpu/drm/vkms/vkms_crtc.c b/drivers/gpu/drm/vkms/vkms_crtc.c
index 
fe17f49305a9e30ba1229fefb1d3d26ebea95d2d..69ec6b14a864382566a15a47c6b7037b30e21f12
 100644
--- a/drivers/gpu/drm/vkms/vkms_crtc.c
+++ b/drivers/gpu/drm/vkms/vkms_crtc.c
@@ -17,7 +17,7 @@ static bool vkms_crtc_handle_vblank_timeout(struct drm_crtc 
*crtc)
 {
        struct vkms_output *output = drm_crtc_to_vkms_output(crtc);
        struct vkms_crtc_state *state;
-       bool ret, fence_cookie;
+       bool ret, fence_cookie, composer_enabled;
 
        fence_cookie = dma_fence_begin_signalling();
 
@@ -27,9 +27,10 @@ static bool vkms_crtc_handle_vblank_timeout(struct drm_crtc 
*crtc)
                DRM_ERROR("vkms failure on handling vblank");
 
        state = output->composer_state;
+       composer_enabled = output->composer_users != 0;
        spin_unlock(&output->lock);
 
-       if (state && output->composer_enabled) {
+       if (state && composer_enabled) {
                u64 frame = drm_crtc_accurate_vblank_count(crtc);
 
                /* update frame_start only if a queued vkms_composer_worker()
diff --git a/drivers/gpu/drm/vkms/vkms_drv.h b/drivers/gpu/drm/vkms/vkms_drv.h
index 
381483aa4fb05c240b2a2fc5f328241ed23f6648..4919d8036668256e38423a081a651702304904a8
 100644
--- a/drivers/gpu/drm/vkms/vkms_drv.h
+++ b/drivers/gpu/drm/vkms/vkms_drv.h
@@ -89,6 +89,7 @@ struct vkms_writeback_job {
        struct iosys_map data[DRM_FORMAT_MAX_PLANES];
        struct vkms_frame_info wb_frame_info;
        pixel_write_t pixel_write;
+       bool composer_ref;
 };
 
 /**
@@ -210,8 +211,7 @@ struct vkms_crtc_state {
  *            vblank timestamps
  * @composer_workq: Ordered workqueue for @composer_state.composer_work.
  * @lock: Lock used to protect concurrent access to the composer
- * @composer_enabled: Protected by @lock, true when the VKMS composer is 
active (crc needed or
- *                   writeback)
+ * @composer_users: Protected by @lock, number of CRC capture and writeback 
users
  * @composer_state: Protected by @lock, current state of this VKMS output
  * @composer_lock: Lock used internally to protect @composer_state members
  */
@@ -222,7 +222,7 @@ struct vkms_output {
        struct workqueue_struct *composer_workq;
        spinlock_t lock;
 
-       bool composer_enabled;
+       unsigned int composer_users;
        struct vkms_crtc_state *composer_state;
 
        spinlock_t composer_lock;
@@ -317,7 +317,8 @@ int vkms_verify_crc_source(struct drm_crtc *crtc, const 
char *source_name,
 
 /* Composer Support */
 void vkms_composer_worker(struct work_struct *work);
-void vkms_set_composer(struct vkms_output *out, bool enabled);
+int vkms_composer_get(struct vkms_output *out);
+void vkms_composer_put(struct vkms_output *out);
 void vkms_writeback_row(struct vkms_writeback_job *wb, const struct 
line_buffer *src_buffer, int y);
 
 /* Writeback */
diff --git a/drivers/gpu/drm/vkms/vkms_writeback.c 
b/drivers/gpu/drm/vkms/vkms_writeback.c
index 
ecf29a2c0c8e0b3348df43296c1453307f308b11..ea02675be635823dd29a86727097d1b89c1e9d80
 100644
--- a/drivers/gpu/drm/vkms/vkms_writeback.c
+++ b/drivers/gpu/drm/vkms/vkms_writeback.c
@@ -118,7 +118,9 @@ static void vkms_wb_cleanup_job(struct 
drm_writeback_connector *connector,
 
        drm_framebuffer_put(vkmsjob->wb_frame_info.fb);
 
-       vkms_set_composer(vkms_output, false);
+       /* Prepared jobs can be cleaned up without ever being committed. */
+       if (vkmsjob->composer_ref)
+               vkms_composer_put(vkms_output);
        kfree(vkmsjob);
 }
 
@@ -141,9 +143,10 @@ static void vkms_wb_atomic_commit(struct drm_connector 
*conn,
        if (!conn_state)
                return;
 
-       vkms_set_composer(output, true);
-
        active_wb = conn_state->writeback_job->priv;
+       /* The commit helper enables the CRTC before issuing writeback. */
+       active_wb->composer_ref = !vkms_composer_get(output);
+       drm_WARN_ON_ONCE(conn->dev, !active_wb->composer_ref);
        wb_frame_info = &active_wb->wb_frame_info;
 
        spin_lock_irq(&output->composer_lock);

base-commit: ca20d5a011d35fc25e328fe31ac47f7aae557c5a

-- 
2.50.1 (Apple Git-155)

Reply via email to