Skip to contentSkip to conversation

drm/vkms: give every composer user its own switch

PATCH Superseded Oarora Etimis 2 participants 4 messages Nov 19

Subsystems (4)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover drm/vkms: give every composer user its own switch

The composer has one switch, shared by the CRC reader and the writeback jobs, so either of them turns it off for the other.

Split it: the switch belongs to whoever took it - each writeback job keeps its own record, and so does the CRC source - and vkms_output keeps only a count of holders. A get() raises the count for the user that asked, a put() lowers it for the user that took one, and the vblank reference is taken by the first holder and dropped by the last.

Patch 2 keeps the lock over the count and the queue, so the state the timer read is still the state it queues.

---
Oarora Etimis (2):
drm/vkms: count the composer users
drm/vkms: queue the composer work under the output lock

 Documentation/gpu/vkms.rst            |  5 -----
 drivers/gpu/drm/vkms/vkms_composer.c  | 39 ++++++++++++++++++++++++++++-------
 drivers/gpu/drm/vkms/vkms_crtc.c      |  5 ++---
 drivers/gpu/drm/vkms/vkms_drv.h       | 31 ++++++++++++++++++++++++----
 drivers/gpu/drm/vkms/vkms_writeback.c |  6 +++---
 5 files changed, 63 insertions(+), 23 deletions(-)

---
base-commit: 97429bac371273886238bfcf63d78e7405cacb46
change-id: 20261111-composer-cc9620515bcd

Best regards,

Signature · 1 line
Oarora Etimis <[email protected]>
#1 lore raw

PATCH v2 1/2 drm/vkms: count the composer users

The composer has one on/off switch, shared by the CRC reader and the writeback jobs, so either of them can turn it off for the other. Replace it with a count of users plus a per-user record of whether that user holds a reference: a get() only raises the count for the user that asked, and a put() can only lower it for the one that took it.

vkms_set_composer() becomes vkms_composer_get() and vkms_composer_put().

Fixes dbd9d80c1b2e ("drm/vkms: Add support for writeback")
---
 Documentation/gpu/vkms.rst            |  5 -----
 drivers/gpu/drm/vkms/vkms_composer.c  | 39 ++++++++++++++++++++++++++++-------
 drivers/gpu/drm/vkms/vkms_crtc.c      |  2 +-
 drivers/gpu/drm/vkms/vkms_drv.h       | 31 ++++++++++++++++++++++++----
 drivers/gpu/drm/vkms/vkms_writeback.c |  6 +++---
 5 files changed, 62 insertions(+), 21 deletions(-)
Documentation/gpu/vkms.rst +0 -5
index 1e79e62a6bc4..fe7dc8def44a 100644
@@ -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.
 
drivers/gpu/drm/vkms/vkms_composer.c +31 -8
index 0fc915a954ba..24b8436eaa74 100644
@@ -711,19 +711,39 @@ 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)
+void vkms_composer_get(struct vkms_output *out, bool *held)
 {
-	bool old_enabled;
+	unsigned int users;
 
-	if (enabled)
-		drm_crtc_vblank_get(&out->crtc);
+	if (*held)
+		return;
+
+	if (drm_crtc_vblank_get(&out->crtc))
+		return;
+
+	spin_lock_irq(&out->lock);
+	*held = true;
+	users = out->composer_users++;
+	spin_unlock_irq(&out->lock);
+
+	/* All users share a single vblank reference. */
+	if (users)
+		drm_crtc_vblank_put(&out->crtc);
+}
+
+void vkms_composer_put(struct vkms_output *out, bool *held)
+{
+	unsigned int users;
+
+	if (!*held)
+		return;
 
 	spin_lock_irq(&out->lock);
-	old_enabled = out->composer_enabled;
-	out->composer_enabled = enabled;
+	*held = false;
+	users = --out->composer_users;
 	spin_unlock_irq(&out->lock);
 
-	if (old_enabled)
+	if (!users)
 		drm_crtc_vblank_put(&out->crtc);
 }
 
@@ -735,7 +755,10 @@ int vkms_set_crc_source(struct drm_crtc *crtc, const char *src_name)
 
 	ret = vkms_crc_parse_source(src_name, &enabled);
 
-	vkms_set_composer(out, enabled);
+	if (enabled)
+		vkms_composer_get(out, &out->composer_crc_hold);
+	else
+		vkms_composer_put(out, &out->composer_crc_hold);
 
 	return ret;
 }
drivers/gpu/drm/vkms/vkms_crtc.c +1 -1
index fe17f49305a9..4e15abfd92a0 100644
@@ -29,7 +29,7 @@ static bool vkms_crtc_handle_vblank_timeout(struct drm_crtc *crtc)
 	state = output->composer_state;
 	spin_unlock(&output->lock);
 
-	if (state && output->composer_enabled) {
+	if (state && READ_ONCE(output->composer_users)) {
 		u64 frame = drm_crtc_accurate_vblank_count(crtc);
 
 		/* update frame_start only if a queued vkms_composer_worker()
drivers/gpu/drm/vkms/vkms_drv.h +27 -4
index dea6cad3f1b9..20c219a15604 100644
@@ -89,6 +89,8 @@ struct vkms_writeback_job {
 	struct iosys_map data[DRM_FORMAT_MAX_PLANES];
 	struct vkms_frame_info wb_frame_info;
 	pixel_write_t pixel_write;
+	/* Whether this job holds a reference to the composer. */
+	bool composer_wb_holds;
 };
 
 /**
@@ -210,8 +212,10 @@ 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: Number of users that currently hold a composer reference; the composer
+ *		    is active only while it is non-zero. Updated under @lock and read with
+ *		    READ_ONCE() from the vblank timeout.
+ * @composer_crc_hold: Protected by @lock, true while the CRC source holds a composer reference
  * @composer_state: Protected by @lock, current state of this VKMS output
  * @composer_lock: Lock used internally to protect @composer_state members
  */
@@ -222,7 +226,8 @@ struct vkms_output {
 	struct workqueue_struct *composer_workq;
 	spinlock_t lock;
 
-	bool composer_enabled;
+	unsigned int composer_users;
+	bool composer_crc_hold;
 	struct vkms_crtc_state *composer_state;
 
 	spinlock_t composer_lock;
@@ -317,7 +322,25 @@ 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);
+
+/**
+ * vkms_composer_get() - Take a composer reference for one user
+ * @out: VKMS output
+ * @held: the user's own record of whether it holds a reference
+ *
+ * The composer stays active while at least one user holds a reference to it.
+ * @held is owned by a single user and lets a user hold at most one reference,
+ * so that it can only ever drop one it has taken.
+ */
+void vkms_composer_get(struct vkms_output *out, bool *held);
+
+/**
+ * vkms_composer_put() - Drop the composer reference recorded in @held
+ * @out: VKMS output
+ * @held: the user's own record of whether it holds a reference
+ */
+void vkms_composer_put(struct vkms_output *out, bool *held);
+
 void vkms_writeback_row(struct vkms_writeback_job *wb, const struct line_buffer *src_buffer, int y);
 
 /* Writeback */
drivers/gpu/drm/vkms/vkms_writeback.c +3 -3
index 858df1f89f66..dda90eaede3c 100644
@@ -118,7 +118,7 @@ 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);
+	vkms_composer_put(vkms_output, &vkmsjob->composer_wb_holds);
 	kfree(vkmsjob);
 }
 
@@ -141,11 +141,11 @@ 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;
 	wb_frame_info = &active_wb->wb_frame_info;
 
+	vkms_composer_get(output, &active_wb->composer_wb_holds);
+
 	spin_lock_irq(&output->composer_lock);
 	crtc_state->active_writeback = active_wb;
 	crtc_state->wb_pending = true;
Signature · 1 line
2.56.1
#2 lore raw

Re: [PATCH v2 1/2] drm/vkms: count the composer users

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] drm/vkms: race on *held in vkms_composer_get() and vkms_composer_put() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/vkms: race on *held in vkms_composer_get() and vkms_composer_put()
--

commit f7732cf6eed7c5182c5ed8c82b94f91369a6ef0b
Author: Oarora Etimis <[email protected]>

drm/vkms: count the composer users

This commit replaces the single composer_enabled boolean with a reference count of active composer users and per-user hold flags so CRC capture and writeback jobs do not disable the composer for each other.

Quoted 40 lines
drivers/gpu/drm/vkms/vkms_composer.c +24 -4
index 0fc915a954bad..24b8436eaa74d 100644
@@ -711,19 +711,39 @@ 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)
+void vkms_composer_get(struct vkms_output *out, bool *held)
 {
-	bool old_enabled;
+	unsigned int users;
 
-	if (enabled)
-		drm_crtc_vblank_get(&out->crtc);
+	if (*held)
+		return;
+
+	if (drm_crtc_vblank_get(&out->crtc))
+		return;
+
+	spin_lock_irq(&out->lock);
+	*held = true;
+	users = out->composer_users++;
+	spin_unlock_irq(&out->lock);
+
+	/* All users share a single vblank reference. */
+	if (users)
+		drm_crtc_vblank_put(&out->crtc);
+}
+
+void vkms_composer_put(struct vkms_output *out, bool *held)
+{
+	unsigned int users;
+
+	if (!*held)
+		return;
 
 	spin_lock_irq(&out->lock);

drivers/gpu/drm/vkms/vkms_composer.c int vkms_verify_crc_source(struct drm_crtc *crtc, const char *src_name, · line 748

Quoted 10 lines
drivers/gpu/drm/vkms/vkms_composer.c +3 -3
-	old_enabled = out->composer_enabled;
-	out->composer_enabled = enabled;
+	*held = false;
+	users = --out->composer_users;
 	spin_unlock_irq(&out->lock);
 
-	if (old_enabled)
+	if (!users)
 		drm_crtc_vblank_put(&out->crtc);
 }

[Severity: Medium]
Can concurrent calls to vkms_composer_get() and vkms_composer_put() race on *held because it is checked outside out->lock and not re-checked after taking the lock?

In drivers/gpu/drm/drm_debugfs_crc.c, crtc_crc_release() clears crc->opened and drops crc->lock before calling crtc->funcs->set_crc_source(crtc, NULL), while crtc_crc_open() sets crc->opened and drops crc->lock before calling crtc->funcs->set_crc_source(crtc, crc->source). Two tasks opening and closing the CRC debugfs file can therefore call vkms_set_crc_source() concurrently with &out->composer_crc_hold:

CPU0 CPU1
crtc_crc_release()
crc->opened = false
vkms_set_crc_source(..., NULL)

    vkms_composer_put()
      if (!*held) [false]
                                      crtc_crc_open()
                                        vkms_set_crc_source(..., "auto")
                                          vkms_composer_get()
                                            if (*held)
                                              return;  <-- skips get
      spin_lock_irq(&out->lock)
      *held = false
      users = --out->composer_users
      spin_unlock_irq(&out->lock)
      drm_crtc_vblank_put(&out->crtc)

This leaves the newly opened CRC reader active with composer_users == 0 and vblanks disabled. Two concurrent puts or gets can likewise both pass the unlocked check and double-decrement or double-increment out->composer_users, underflowing the counter or leaking a composer and vblank reference.

Re-checking *held under out->lock (and dropping the vblank reference in vkms_composer_get() if *held became true in the meantime) would avoid the race.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH v2 2/2 drm/vkms: queue the composer work under the output lock

In reply to Oarora Etimis

vkms_crtc_handle_vblank_timeout() reads output->composer_state under output->lock, then drops the lock and queues a work item that lives inside that state. The commit that retires the state frees it after flush_work(), which does not wait for a work item that has not been queued yet, so the queue can land on freed memory.

Hold the lock across the bookkeeping and the queue, so the state that was read is still the state that gets queued.

Fixes 88ad7f3a5660 ("drm/vkms: Reduce critical section in vblank_simulate")
---
 drivers/gpu/drm/vkms/vkms_crtc.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/gpu/drm/vkms/vkms_crtc.c +2 -3
index 4e15abfd92a0..d2c6ffbad07c 100644
@@ -27,9 +27,7 @@ static bool vkms_crtc_handle_vblank_timeout(struct drm_crtc *crtc)
 		DRM_ERROR("vkms failure on handling vblank");
 
 	state = output->composer_state;
-	spin_unlock(&output->lock);
-
-	if (state && READ_ONCE(output->composer_users)) {
+	if (state && output->composer_users) {
 		u64 frame = drm_crtc_accurate_vblank_count(crtc);
 
 		/* update frame_start only if a queued vkms_composer_worker()
@@ -49,6 +47,7 @@ static bool vkms_crtc_handle_vblank_timeout(struct drm_crtc *crtc)
 		if (!ret)
 			DRM_DEBUG_DRIVER("Composer worker already queued\n");
 	}
+	spin_unlock(&output->lock);
 
 	dma_fence_end_signalling(fence_cookie);
 
Signature · 1 line
2.56.1