Skip to contentSkip to conversation

drm/vkms: skip composer work that changes nothing

PATCH New Oarora Etimis 2 participants 4 messages Nov 26

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover drm/vkms: skip composer work that changes nothing

Patch 1 reads a full-screen plane straight into the output line instead of blending it there. Patch 2 writes the frame straight into the writeback buffer instead of going through the 16-bit line buffer.

---
Changes in v2:
- do not queue a CRC for a frame that was copied without computing one
- restrict the direct copy to XRGB8888, the only 4-byte format vkms reads
and writes back
- describe the new parameters in the kernel-doc
- Link to v1: https://patch.msgid.link/[email protected]

---
Oarora Etimis (2):
drm/vkms: skip a blend that cannot change the frame drm/vkms: copy a full-screen frame straight to writeback

 drivers/gpu/drm/vkms/vkms_composer.c | 124 ++++++++++++++++++++++++++++++++---
 drivers/gpu/drm/vkms/vkms_drv.h      |   5 ++
 drivers/gpu/drm/vkms/vkms_formats.c  |  26 ++++++++
 3 files changed, 145 insertions(+), 10 deletions(-)

---
base-commit: 5ce32a0c94a827ff44ee60898c28060714dcff75
change-id: 20261120-composer-fastpath-8e60c7f7e58e

Best regards,

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

PATCH v2 1/2 drm/vkms: skip a blend that cannot change the frame

A single full-screen plane no longer goes through fill_background() and the line blend: read it straight into the output line.

---
 drivers/gpu/drm/vkms/vkms_composer.c | 44 ++++++++++++++++++++++++++++++++----
 1 file changed, 40 insertions(+), 4 deletions(-)
drivers/gpu/drm/vkms/vkms_composer.c +40 -4
index 7a6a7875520c..4e2dd568bc3e 100644
@@ -452,13 +452,15 @@ static void clamp_line_coordinates(enum pixel_read_direction direction,
  * @crtc_x_limit: width of the output buffer
  * @stage_buffer: temporary buffer to convert the pixel line from the source buffer
  * @output_buffer: buffer to blend the read line into.
+ * @blend_direct: false to read the line straight into @output_buffer
  */
 static void blend_line(struct vkms_plane_state *current_plane, int y,
 		       int crtc_x_limit, struct line_buffer *stage_buffer,
-		       struct line_buffer *output_buffer)
+		       struct line_buffer *output_buffer, bool blend_direct)
 {
 	int src_x_start, src_y_start, dst_x_start, pixel_count;
 	struct drm_rect dst_line, tmp_src, src_line;
+	struct line_buffer *read_buffer = blend_direct ? stage_buffer : output_buffer;
 
 	/* Avoid rendering useless lines */
 	if (y < current_plane->frame_info.dst.y1 ||
@@ -542,12 +544,43 @@ static void blend_line(struct vkms_plane_state *current_plane, int y,
 	 * buffer [1].
 	 */
 	current_plane->pixel_read_line(current_plane, src_x_start, src_y_start, direction,
-				       pixel_count, &stage_buffer->pixels[dst_x_start]);
+				       pixel_count, &read_buffer->pixels[dst_x_start]);
+
+	if (!blend_direct)
+		return;
+
 	pre_blend_color_transform(current_plane, stage_buffer);
 	pre_mul_alpha_blend(stage_buffer, output_buffer,
 			    dst_x_start, pixel_count);
 }
 
+static bool compose_direct(const struct vkms_crtc_state *crtc_state)
+{
+	const struct vkms_plane_state *plane;
+	const struct drm_rect *dst;
+
+	if (crtc_state->num_active_planes != 1)
+		return false;
+
+	plane = crtc_state->active_planes[0];
+	if (!plane)
+		return false;
+
+	dst = &plane->frame_info.dst;
+	if (dst->x1 != 0 || dst->y1 != 0 ||
+	    dst->x2 != crtc_state->base.mode.hdisplay ||
+	    dst->y2 != crtc_state->base.mode.vdisplay)
+		return false;
+
+	if (plane->frame_info.fb->format->has_alpha)
+		return false;
+
+	if (plane->base.base.color_pipeline)
+		return false;
+
+	return true;
+}
+
 /**
  * blend - blend the pixels from all planes and compute crc
  * @wb: The writeback frame buffer metadata
@@ -579,6 +612,7 @@ static void blend(struct vkms_writeback_job *wb,
 
 	int crtc_y_limit = crtc_state->base.mode.vdisplay;
 	int crtc_x_limit = crtc_state->base.mode.hdisplay;
+	bool direct = compose_direct(crtc_state);
 
 	/*
 	 * The planes are composed line-by-line to avoid heavy memory usage. It is a necessary
@@ -588,11 +622,13 @@ static void blend(struct vkms_writeback_job *wb,
 	 * algorithm for a specific format, into the staging buffer.
 	 */
 	for (int y = 0; y < crtc_y_limit; y++) {
-		fill_background(&background_color, output_buffer);
+		if (!direct)
+			fill_background(&background_color, output_buffer);
 
 		/* The active planes are composed associatively in z-order. */
 		for (size_t i = 0; i < n_active_planes; i++) {
-			blend_line(plane[i], y, crtc_x_limit, stage_buffer, output_buffer);
+			blend_line(plane[i], y, crtc_x_limit, stage_buffer,
+				   output_buffer, !direct);
 		}
 
 		apply_lut(crtc_state, output_buffer);
Signature · 1 line
2.56.1
#2 lore raw

PATCH v2 2/2 drm/vkms: copy a full-screen frame straight to writeback

In reply to Oarora Etimis

When no CRC is armed and both sides use the same 32-bit format, copy the frame straight into the writeback buffer.

---
 drivers/gpu/drm/vkms/vkms_composer.c | 82 +++++++++++++++++++++++++++++++++---
 drivers/gpu/drm/vkms/vkms_drv.h      |  5 +++
 drivers/gpu/drm/vkms/vkms_formats.c  | 26 ++++++++++++
 3 files changed, 106 insertions(+), 7 deletions(-)
drivers/gpu/drm/vkms/vkms_composer.c +75 -7
index 4e2dd568bc3e..239fdaca2926 100644
@@ -581,6 +581,47 @@ static bool compose_direct(const struct vkms_crtc_state *crtc_state)
 	return true;
 }
 
+/**
+ * writeback_direct() - Can the frame skip the line buffer?
+ * @crtc_state: the state to compose
+ * @wb: the writeback job, or NULL
+ * @plane: the plane the frame comes from
+ * @crc_on: whether this frame has to produce a CRC entry
+ *
+ * True when nothing needs 16-bit pixels: no CRC, no LUT, no colour pipeline.
+ *
+ * Returns: true if the frame can be copied straight into the writeback buffer.
+ */
+static bool writeback_direct(const struct vkms_crtc_state *crtc_state,
+			     const struct vkms_writeback_job *wb,
+			     const struct vkms_plane_state *plane,
+			     bool crc_on)
+{
+	const struct drm_format_info *format;
+
+	/* The CRC is computed over the 16-bit line buffer. */
+	if (!wb || crc_on)
+		return false;
+
+	/* The gamma LUT is applied to the 16-bit line buffer. */
+	if (crtc_state->gamma_lut.y)
+		return false;
+
+	/* The copy walks source rows, so the plane cannot be rotated. */
+	if (plane->frame_info.rotation != DRM_MODE_ROTATE_0)
+		return false;
+
+	if (plane->frame_info.src.x1 || plane->frame_info.src.y1)
+		return false;
+
+	format = plane->frame_info.fb->format;
+	if (format != wb->wb_frame_info.fb->format)
+		return false;
+
+	/* The copy assumes the padding byte, which only XRGB8888 has here. */
+	return format->format == DRM_FORMAT_XRGB8888;
+}
+
 /**
  * blend - blend the pixels from all planes and compute crc
  * @wb: The writeback frame buffer metadata
@@ -589,6 +630,7 @@ static bool compose_direct(const struct vkms_crtc_state *crtc_state)
  * @output_buffer: A buffer of a row that will receive the result of the blend(s)
  * @stage_buffer: The line with the pixels from plane being blend to the output
  * @row_size: The size, in bytes, of a single row
+ * @direct: true when the plane can be read straight into @output_buffer
  *
  * This function blends the pixels (Using the `pre_mul_alpha_blend`)
  * from all planes, calculates the crc32 of the output from the former step,
@@ -597,7 +639,8 @@ static bool compose_direct(const struct vkms_crtc_state *crtc_state)
 static void blend(struct vkms_writeback_job *wb,
 		  struct vkms_crtc_state *crtc_state,
 		  u32 *crc32, struct line_buffer *stage_buffer,
-		  struct line_buffer *output_buffer, size_t row_size)
+		  struct line_buffer *output_buffer, size_t row_size,
+		  bool direct)
 {
 	struct vkms_plane_state **plane = crtc_state->active_planes;
 	u32 n_active_planes = crtc_state->num_active_planes;
@@ -612,7 +655,6 @@ static void blend(struct vkms_writeback_job *wb,
 
 	int crtc_y_limit = crtc_state->base.mode.vdisplay;
 	int crtc_x_limit = crtc_state->base.mode.hdisplay;
-	bool direct = compose_direct(crtc_state);
 
 	/*
 	 * The planes are composed line-by-line to avoid heavy memory usage. It is a necessary
@@ -670,10 +712,12 @@ static int check_iosys_map(struct vkms_crtc_state *crtc_state)
 
 static int compose_active_planes(struct vkms_writeback_job *active_wb,
 				 struct vkms_crtc_state *crtc_state,
-				 u32 *crc32)
+				 u32 *crc32, bool crc_on)
 {
 	size_t line_width, pixel_size = sizeof(struct pixel_argb_u16);
 	struct line_buffer output_buffer, stage_buffer;
+	const struct vkms_plane_state *plane;
+	bool direct;
 	int ret = 0;
 
 	/*
@@ -690,6 +734,17 @@ static int compose_active_planes(struct vkms_writeback_job *active_wb,
 	if (WARN_ON(check_format_funcs(crtc_state, active_wb)))
 		return -EINVAL;
 
+	direct = compose_direct(crtc_state);
+
+	plane = crtc_state->active_planes[0];
+
+	if (direct && writeback_direct(crtc_state, active_wb, plane, crc_on)) {
+		for (int y = 0; y < crtc_state->base.mode.vdisplay; y++)
+			vkms_writeback_row_direct(active_wb, plane, y);
+
+		return 0;
+	}
+
 	line_width = crtc_state->base.mode.hdisplay;
 	stage_buffer.n_pixels = line_width;
 	output_buffer.n_pixels = line_width;
@@ -708,7 +763,7 @@ static int compose_active_planes(struct vkms_writeback_job *active_wb,
 	}
 
 	blend(active_wb, crtc_state, crc32, &stage_buffer,
-	      &output_buffer, line_width * pixel_size);
+	      &output_buffer, line_width * pixel_size, direct);
 
 	kvfree(output_buffer.pixels);
 free_stage_buffer:
@@ -734,7 +789,7 @@ void vkms_composer_worker(struct work_struct *work)
 	struct drm_crtc *crtc = crtc_state->base.crtc;
 	struct vkms_writeback_job *active_wb = crtc_state->active_writeback;
 	struct vkms_output *out = drm_crtc_to_vkms_output(crtc);
-	bool crc_pending, wb_pending;
+	bool crc_pending, wb_pending, crc_on;
 	u64 frame_start, frame_end;
 	u32 crc32 = 0;
 	int ret;
@@ -772,10 +827,16 @@ void vkms_composer_worker(struct work_struct *work)
 	if (!crc_pending)
 		return;
 
+	/*
+	 * One read for both the direct-path decision and the CRC entry: a frame
+	 * must not be queued with a CRC that was never computed.
+	 */
+	crc_on = READ_ONCE(out->crc_enabled);
+
 	if (wb_pending)
-		ret = compose_active_planes(active_wb, crtc_state, &crc32);
+		ret = compose_active_planes(active_wb, crtc_state, &crc32, crc_on);
 	else
-		ret = compose_active_planes(NULL, crtc_state, &crc32);
+		ret = compose_active_planes(NULL, crtc_state, &crc32, crc_on);
 
 	if (ret)
 		return;
@@ -790,6 +851,9 @@ void vkms_composer_worker(struct work_struct *work)
 	/*
 	 * The worker can fall behind the vblank hrtimer, make sure we catch up.
 	 */
+	if (!crc_on)
+		return;
+
 	while (frame_start <= frame_end)
 		drm_crtc_add_crc_entry(crtc, true, frame_start++, &crc32);
 }
@@ -858,6 +922,10 @@ int vkms_set_crc_source(struct drm_crtc *crtc, const char *src_name)
 
 	ret = vkms_crc_parse_source(src_name, &enabled);
 
+	spin_lock_irq(&out->lock);
+	WRITE_ONCE(out->crc_enabled, enabled);
+	spin_unlock_irq(&out->lock);
+
 	vkms_set_composer(out, enabled);
 
 	return ret;
drivers/gpu/drm/vkms/vkms_drv.h +5 -0
index b7eb7033fb4b..69f12cecd2f9 100644
@@ -220,6 +220,8 @@ struct vkms_crtc_state {
  * @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)
+ * @crc_enabled: true while the CRC source is enabled. Written under @lock, read with
+ *		 READ_ONCE() from the composer worker
  * @composer_state: Protected by @lock, current state of this VKMS output
  * @composer_lock: Lock used internally to protect @composer_state members
  */
@@ -231,6 +233,7 @@ struct vkms_output {
 	spinlock_t lock;
 
 	bool composer_enabled;
+	bool crc_enabled;
 	struct vkms_crtc_state *composer_state;
 
 	spinlock_t composer_lock;
@@ -325,6 +328,8 @@ int vkms_verify_crc_source(struct drm_crtc *crtc, const char *source_name,
 void vkms_composer_worker(struct work_struct *work);
 void vkms_set_composer(struct vkms_output *out, bool enabled);
 void vkms_writeback_row(struct vkms_writeback_job *wb, const struct line_buffer *src_buffer, int y);
+void vkms_writeback_row_direct(struct vkms_writeback_job *wb,
+			       const struct vkms_plane_state *plane, int y);
 
 /* Writeback */
 int vkms_enable_writeback_connector(struct vkms_device *vkmsdev, struct vkms_output *vkms_out);
drivers/gpu/drm/vkms/vkms_formats.c +26 -0
index 7f1f29b589e8..8bbd517092a5 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0+
 
 #include <linux/kernel.h>
+#include <linux/unaligned.h>
 #include <linux/minmax.h>
 
 #include <drm/drm_blend.h>
@@ -681,6 +682,31 @@ void vkms_writeback_row(struct vkms_writeback_job *wb,
 		wb->pixel_write(dst_pixels, &in_pixels[x]);
 }
 
+/**
+ * vkms_writeback_row_direct() - Copy a composed line into the writeback buffer
+ * @wb: the writeback job
+ * @plane: the plane the line comes from
+ * @y: line to write
+ *
+ * Only used when writeback_direct() returned true.
+ */
+void vkms_writeback_row_direct(struct vkms_writeback_job *wb,
+			       const struct vkms_plane_state *plane, int y)
+{
+	const struct vkms_frame_info *src_info = &plane->frame_info;
+	struct vkms_frame_info *dst_info = &wb->wb_frame_info;
+	u8 *src_pixels, *dst_pixels;
+	int rem_x, rem_y;
+	int x_limit = drm_rect_width(&dst_info->dst);
+
+	packed_pixels_addr(src_info, src_info->dst.x1, y, 0, &src_pixels, &rem_x, &rem_y);
+	packed_pixels_addr(dst_info, dst_info->dst.x1, y, 0, &dst_pixels, &rem_x, &rem_y);
+
+	/* compose_direct() guarantees no alpha, so byte 3 is padding. */
+	for (int x = 0; x < x_limit; x++, src_pixels += 4, dst_pixels += 4)
+		put_unaligned_le32(get_unaligned_le32(src_pixels) | 0xff000000, dst_pixels);
+}
+
 /**
  * get_pixel_read_line_function() - Retrieve the correct read_line function for a specific
  * format. The returned pointer is NULL for unsupported pixel formats. The caller must ensure that
Signature · 1 line
2.56.1
#3 lore raw

Re: [PATCH v2 2/2] drm/vkms: copy a full-screen frame straight to writeback

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/vkms: out-of-bounds dereference in compose_active_planes() with 0 planes --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/vkms: out-of-bounds dereference in compose_active_planes() with 0 planes
--

commit 78d03e558f502868143b5675a866d31e9e3f5440
Author: Oarora Etimis <[email protected]>

drm/vkms: copy a full-screen frame straight to writeback

This commit copies a single full-screen XRGB8888 plane directly into a matching writeback framebuffer without allocating or blending through the 16-bit line buffer when CRC generation and gamma LUT are disabled.

Quoted 4 lines
drivers/gpu/drm/vkms/vkms_composer.c +0 -0
index 4e2dd568bc3e3..239fdaca2926a 100644

[ ... ]

Quoted 4 lines
drivers/gpu/drm/vkms/vkms_composer.c +0 -0
@@ -690,6 +734,17 @@ static int compose_active_planes(struct vkms_writeback_job *active_wb,
 	if (WARN_ON(check_format_funcs(crtc_state, active_wb)))
 		return -EINVAL;
 

drivers/gpu/drm/vkms/vkms_composer.c static int compose_active_planes(struct vkms_writeback_job *active_wb, · line 746

Quoted 10 lines
drivers/gpu/drm/vkms/vkms_composer.c +10 -0
+	direct = compose_direct(crtc_state);
+
+	plane = crtc_state->active_planes[0];
+
+	if (direct && writeback_direct(crtc_state, active_wb, plane, crc_on)) {
+		for (int y = 0; y < crtc_state->base.mode.vdisplay; y++)
+			vkms_writeback_row_direct(active_wb, plane, y);
+
+		return 0;
+	}

[Severity: High]
Can this dereference crtc_state->active_planes[0] when crtc_state->num_active_planes is 0?

When an active CRTC has no visible planes, vkms_crtc_atomic_check() allocates active_planes with kzalloc_objs(..., 0), which returns ZERO_SIZE_PTR. compose_direct() checks that num_active_planes == 1 and returns false, but reading crtc_state->active_planes[0] before testing direct faults on ZERO_SIZE_PTR.

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