Skip to contentSkip to conversation

drm/vkms: complete the plane color pipeline

PATCH Superseded Oarora Etimis 3 participants 14 messages Nov 28 – Nov 29

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover drm/vkms: complete the plane color pipeline

The vkms plane pipeline has a fixed matrix, two 1D curves and two 3x4 matrices. Userspace cannot supply its own LUT, do a 3D lookup, or apply a gain to the channels.

Patches 1 to 3 fix three bugs in the pipeline plumbing. Patches 4 to 6 add a 1D LUT, a 3D LUT and a multiplier. Patch 7 adds KUnit coverage for the 3D LUT and the multiplier; the 1D LUT gets its coverage in patch 4.

The color math has KUnit coverage; there is no IGT test for the color pipeline yet, so the uAPI paths are not exercised end-to-end.

The three new colorops are appended after the existing ones; their order is unchanged.

---
Oarora Etimis (7):
drm/vkms: add the colorops of a plane that is being enabled drm/vkms: free the colorop whose initialization failed drm/vkms: use the colorop states of the state being composed drm/vkms: add a programmable 1D LUT colorop
drm/vkms: add a 3D LUT colorop
drm/vkms: add a multiplier colorop
drm/vkms: add KUnit coverage for the 3D LUT and multiplier colorops

 drivers/gpu/drm/vkms/tests/vkms_color_test.c | 194 +++++++++++++++++
 drivers/gpu/drm/vkms/vkms_colorop.c          |  61 +++++-
 drivers/gpu/drm/vkms/vkms_composer.c         | 314 +++++++++++++++++++++++----
 drivers/gpu/drm/vkms/vkms_composer.h         |   5 +
 drivers/gpu/drm/vkms/vkms_crtc.c             |  59 ++++-
 drivers/gpu/drm/vkms/vkms_drv.h              |  61 ++++++
 drivers/gpu/drm/vkms/vkms_plane.c            |   2 +
 7 files changed, 646 insertions(+), 50 deletions(-)

---
base-commit: 5ce32a0c94a827ff44ee60898c28060714dcff75
change-id: 20261113-colorop-693f016a156d

Best regards,

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

PATCH 1/7 drm/vkms: add the colorops of a plane that is being enabled

drm_atomic_add_affected_planes() walks the plane mask of the old CRTC state, so a plane that is being enabled is not in that mask and its colorops are left out of the commit. Its first frame then sees the pipeline as bypassed, and a YUV plane is composed without the YUV-to-RGB conversion.

Add them explicitly in vkms_crtc_atomic_check().

Fixes 589179b18224 ("drm/vkms: Add atomic check and matrix handling for fixed matrix colorop")
---
 drivers/gpu/drm/vkms/vkms_crtc.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
drivers/gpu/drm/vkms/vkms_crtc.c +16 -0
index 019d7bc90cdc..7d18178d5320 100644
@@ -129,6 +129,22 @@ static int vkms_crtc_atomic_check(struct drm_crtc *crtc,
 	if (ret < 0)
 		return ret;
 
+	/*
+	 * drm_atomic_add_affected_planes() only walks the plane mask of the
+	 * old CRTC state. A plane that is being enabled is not in that mask,
+	 * so its colorops would be missing from this commit.
+	 */
+	drm_for_each_plane_mask(plane, crtc->dev, crtc_state->plane_mask) {
+		plane_state = drm_atomic_get_new_plane_state(crtc_state->state, plane);
+
+		if (!plane_state || !plane_state->color_pipeline)
+			continue;
+
+		ret = drm_atomic_add_affected_colorops(crtc_state->state, plane);
+		if (ret < 0)
+			return ret;
+	}
+
 	drm_for_each_plane_mask(plane, crtc->dev, crtc_state->plane_mask) {
 		plane_state = drm_atomic_get_new_plane_state(crtc_state->state, plane);
 		WARN_ON(!plane_state);
Signature · 1 line
2.56.1
#2 lore raw

PATCH 2/7 drm/vkms: free the colorop whose initialization failed

In reply to Oarora Etimis

The error path of vkms_initialize_color_pipeline() walks ops[0..i-1], but ops[i] is already allocated when its drm_plane_colorop_*_init() call fails, so it leaks.

Include it in the walk.

Fixes c1e578bd08da ("drm/vkms: Add enumerated 1D curve colorop")
---
 drivers/gpu/drm/vkms/vkms_colorop.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/vkms/vkms_colorop.c +1 -1
index 9424b1994239..ed557a1c6a05 100644
@@ -126,7 +126,7 @@ static int vkms_initialize_color_pipeline(struct drm_plane *plane, struct drm_pr
 	return 0;
 
 cleanup:
-	for (j = 0; j < i; j++) {
+	for (j = 0; j <= i && j < ARRAY_SIZE(ops); j++) {
 		if (ops[j]) {
 			drm_colorop_cleanup(ops[j]);
 			kfree(ops[j]);
Signature · 1 line
2.56.1
#3 lore raw

Re: [PATCH 2/7] drm/vkms: free the colorop whose initialization failed

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] drm/vkms: NULL dereference in drm_colorop_cleanup() on failed colorop init --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/vkms: NULL dereference in drm_colorop_cleanup() on failed colorop init
--

commit 19fa2bf1e8d2d6f8cce50424490f5da439214e86
Author: Oarora Etimis <[email protected]>

drm/vkms: free the colorop whose initialization failed

This commit extends the error cleanup loop in vkms_initialize_color_pipeline() to include ops[i] so that a colorop whose initialization fails is cleaned up and freed.

drivers/gpu/drm/vkms/vkms_colorop.c static int vkms_initialize_color_pipeline(struct drm_plane *plane, struct drm_pr · line 132

Quoted 13 lines
drivers/gpu/drm/vkms/vkms_colorop.c +1 -1
index 9424b1994239a..ed557a1c6a056 100644
@@ -126,7 +126,7 @@ static int vkms_initialize_color_pipeline(struct drm_plane *plane, struct drm_pr
 	return 0;
 
 cleanup:
-	for (j = 0; j < i; j++) {
+	for (j = 0; j <= i && j < ARRAY_SIZE(ops); j++) {
 		if (ops[j]) {
 			drm_colorop_cleanup(ops[j]);
 			kfree(ops[j]);

[Severity: Medium]
Can calling drm_colorop_cleanup(ops[i]) after a failed drm_plane_colorop_*_init() cause a NULL pointer dereference?

Each drm_plane_colorop_*_init() helper delegates to drm_plane_colorop_init(), which can fail in drm_mode_object_add() before initializing colorop->dev or linking colorop->head:

drivers/gpu/drm/drm_colorop.c:drm_plane_colorop_init() {
	ret = drm_mode_object_add(dev, &colorop->base, DRM_MODE_OBJECT_COLOROP);
	if (ret)
		return ret;

	colorop->base.properties = &colorop->properties;
	colorop->dev = dev;
	...
	list_add_tail(&colorop->head, &config->colorop_list);
	colorop->index = config->num_colorop++;

When drm_mode_object_add() fails, ops[i]->dev and ops[i]->head are still zeroed, so drm_colorop_cleanup(ops[i]) dereferences NULL in list_del(&colorop->head) and config->num_colorop--. Freeing ops[i] with kfree() without calling drm_colorop_cleanup() on the failed entry avoids this.

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

PATCH 3/7 drm/vkms: use the colorop states of the state being composed

In reply to Oarora Etimis

The composer runs asynchronously and reads drm_colorop::state, which a later commit can replace and free. If that commit writes a value the colorop already has, core leaves the plane and its CRTC out, so nothing flushes the composer first and the worker is left holding a pointer to the freed state.

Record the values the composer needs in vkms_plane_state from atomic_flush(), together with a reference of the LUT blob, and read only that copy.

Fixes c1e578bd08da ("drm/vkms: Add enumerated 1D curve colorop")
---
 drivers/gpu/drm/vkms/vkms_colorop.c  |  4 +-
 drivers/gpu/drm/vkms/vkms_composer.c | 92 +++++++++++++++++++-----------------
 drivers/gpu/drm/vkms/vkms_crtc.c     | 43 ++++++++++++++++-
 drivers/gpu/drm/vkms/vkms_drv.h      | 56 ++++++++++++++++++++++
 drivers/gpu/drm/vkms/vkms_plane.c    |  2 +
 5 files changed, 149 insertions(+), 48 deletions(-)
drivers/gpu/drm/vkms/vkms_colorop.c +1 -3
index ed557a1c6a05..01b1ed2a5991 100644
@@ -24,11 +24,9 @@ static const struct drm_colorop_funcs vkms_colorop_funcs = {
 	.destroy = drm_colorop_destroy,
 };
 
-#define MAX_COLOR_PIPELINE_OPS 5
-
 static int vkms_initialize_color_pipeline(struct drm_plane *plane, struct drm_prop_enum_list *list)
 {
-	struct drm_colorop *ops[MAX_COLOR_PIPELINE_OPS];
+	struct drm_colorop *ops[VKMS_MAX_COLOROPS];
 	struct drm_device *dev = plane->dev;
 	int ret;
 	int i = 0, j = 0;
drivers/gpu/drm/vkms/vkms_composer.c +48 -44

113 lines not on this page · raw message

drivers/gpu/drm/vkms/vkms_crtc.c +42 -1

66 lines not on this page · raw message

drivers/gpu/drm/vkms/vkms_drv.h +56 -0

88 lines not on this page · raw message

drivers/gpu/drm/vkms/vkms_plane.c +2 -0
index fd3d34dcf0fe..b05475b0f5d4 100644
@@ -79,6 +79,8 @@ static void vkms_plane_destroy_state(struct drm_plane *plane,
 			drm_framebuffer_put(vkms_state->frame_info.fb);
 	}
 
+	vkms_plane_colorops_release(vkms_state);
+
 	__drm_gem_destroy_shadow_plane_state(&vkms_state->base);
 	kfree(vkms_state);
 }
Signature · 1 line
2.56.1
#5 lore raw

Re: [PATCH 3/7] drm/vkms: use the colorop states of the state being composed

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/vkms: use-after-free in vkms_plane_colorops_update() on nonblocking commit --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/vkms: use-after-free in vkms_plane_colorops_update() on nonblocking commit
--

commit 838e4d7bd5412801fae0f59aa09cd076672b38c7
Author: Oarora Etimis <[email protected]>

drm/vkms: use the colorop states of the state being composed

This commit copies each active plane's colorop state and LUT blob reference into vkms_plane_state during vkms_crtc_atomic_flush() so the asynchronous composer worker does not read drm_colorop::state directly.

Quoted 58 lines
drivers/gpu/drm/vkms/vkms_crtc.c +33 -0
index 7d18178d53205..e7ea487187bce 100644
@@ -5,6 +5,7 @@
 #include <drm/drm_atomic.h>
 #include <drm/drm_atomic_helper.h>
 #include <drm/drm_blend.h>
+#include <drm/drm_colorop.h>
 #include <drm/drm_managed.h>
 #include <drm/drm_print.h>
 #include <drm/drm_probe_helper.h>
@@ -186,11 +187,43 @@ static void vkms_crtc_atomic_begin(struct drm_crtc *crtc,
 	spin_lock_irq(&vkms_output->lock);
 }
 
+static void vkms_plane_colorops_update(struct vkms_plane_state *plane_state,
+				       struct drm_atomic_commit *commit)
+{
+	struct drm_colorop *colorop;
+	size_t slot = 0;
+
+	/*
+	 * The plane state is freshly allocated for this commit, so its
+	 * colorop_states array is zero and holds nothing to release.
+	 */
+	for (colorop = plane_state->base.base.color_pipeline;
+	     colorop && slot < ARRAY_SIZE(plane_state->colorop_states);
+	     colorop = colorop->next) {
+		struct drm_colorop_state *colorop_state;
+		struct vkms_colorop_state *state;
+
+		colorop_state = drm_atomic_get_new_colorop_state(commit, colorop);
+		if (!colorop_state)
+			break;
+
+		state = &plane_state->colorop_states[slot++];
+		state->colorop = colorop;
+		state->bypass = colorop_state->bypass;
+		state->curve_1d_type = colorop_state->curve_1d_type;
+		state->multiplier = colorop_state->multiplier;
+
+		if (colorop_state->data)
+			state->data = drm_property_blob_get(colorop_state->data);
+	}
+}
+
 static void vkms_crtc_atomic_flush(struct drm_crtc *crtc,
 				   struct drm_atomic_commit *state)
 	__releases(&vkms_output->lock)
 {
 	struct vkms_output *vkms_output = drm_crtc_to_vkms_output(crtc);
+	struct vkms_crtc_state *crtc_state = to_vkms_crtc_state(crtc->state);
 
 	if (crtc->state->event) {
 		spin_lock(&crtc->dev->event_lock);
@@ -205,7 +238,15 @@ static void vkms_crtc_atomic_flush(struct drm_crtc *crtc,
 		crtc->state->event = NULL;

drivers/gpu/drm/vkms/vkms_crtc.c static void vkms_crtc_atomic_flush(struct drm_crtc *crtc, · line 247

Quoted 10 lines
drivers/gpu/drm/vkms/vkms_crtc.c +7 -1
 	}
 
-	vkms_output->composer_state = to_vkms_crtc_state(crtc->state);
+	/*
+	 * Capture the color operations of this commit: drm_colorop::state is
+	 * the current value of the colorop, not the one this frame was built
+	 * from, and the composer runs asynchronously, so it must not read it.
+	 */
+	for (size_t i = 0; i < crtc_state->num_active_planes; i++)
+		vkms_plane_colorops_update(crtc_state->active_planes[i], state);

[Severity: High]
Can vkms_plane_colorops_update() read a freed drm_colorop_state and take a reference on an already-released data blob when commit 1 is nonblocking?

CPU0 (commit 1, nonblocking) CPU1 (commit 2, unchanged colorop prop)
drm_mode_atomic_ioctl()
drm_atomic_helper_commit(s1)

    drm_atomic_helper_swap_state(s1)
      colorop->state = cs1
    queue_work(s1->commit_work)
  drm_modeset_drop_locks()
                                  drm_mode_atomic_ioctl()
                                    drm_atomic_set_property()
                                      drm_atomic_get_colorop_state(s2)
                                      /* !replaced: plane not added to s2 */
                                    drm_atomic_helper_commit(s2)
                                      /* no plane/CRTC in s2, no stall */
                                      drm_atomic_helper_swap_state(s2)
                                        s2->...->state_to_destroy = cs1
                                    drm_atomic_commit_put(s2)
                                      drm_colorop_Passed/destroy_state(cs1)
                                        drm_property_blob_put(cs1->data)
                                        kfree(cs1)
commit_work()
  drm_atomic_helper_wait_for_fences()
  vkms_atomic_commit_tail(s1)
    ...
      vkms_crtc_atomic_flush()
        vkms_plane_colorops_update()
          cs1->bypass             <-- use-after-free
          drm_property_blob_get(cs1->data)

For a nonblocking commit, drm_atomic_helper_swap_state() publishes commit 1's new_colorop_state into colorop->state and drops the modeset locks before commit_work runs vkms_atomic_commit_tail(). If commit 1 is still waiting (for example in drm_atomic_helper_wait_for_fences() on an IN_FENCE_FD) when commit 2 writes the same value the colorop property already has, drm_atomic_set_property() adds the colorop to commit 2 without adding its plane or CRTC. Because drm_colorop_state has no drm_crtc_commit dependency tracking, commit 2 swaps and frees commit 1's new_colorop_state immediately, before commit 1 reaches vkms_crtc_atomic_flush().

Capturing colorop_states during atomic_check before drm_atomic_helper_swap_state(), while plane->mutex is still held, would avoid this race.

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

PATCH 4/7 drm/vkms: add a programmable 1D LUT colorop

In reply to Oarora Etimis

The two curve stages apply a curve built into the driver, so userspace can pick one but cannot supply a table.

Add a 1D LUT colorop with 256 entries and linear interpolation, and look the entries up in the composer from the blob it carries. The values are U0.32 and are scaled to the 16-bit range the composer works in.

Add KUnit coverage for the interpolation, including a table that decreases.

The existing colorops and their order are unchanged.

---
 drivers/gpu/drm/vkms/tests/vkms_color_test.c | 89 ++++++++++++++++++++++++++++
 drivers/gpu/drm/vkms/vkms_colorop.c          | 19 ++++++
 drivers/gpu/drm/vkms/vkms_composer.c         | 80 +++++++++++++++++++++++++
 drivers/gpu/drm/vkms/vkms_composer.h         |  2 +
 drivers/gpu/drm/vkms/vkms_drv.h              |  3 +
 5 files changed, 193 insertions(+)
drivers/gpu/drm/vkms/tests/vkms_color_test.c +89 -0
index c3c61037cb71..0d68de6462d9 100644
@@ -120,6 +120,91 @@ static void vkms_color_test_linear(struct kunit *test)
 	}
 }
 
+/*
+ * A two-entry LUT that maps 0 to 0 and 1.0 to 1.0 is the identity, so every
+ * input has to come back unchanged, including the ones between the entries.
+ */
+static const struct drm_color_lut32 test_lut32_identity[] = {
+	{ 0x00000000, 0x00000000, 0x00000000, 0 },
+	{ 0xffffffff, 0xffffffff, 0xffffffff, 0 },
+};
+
+static void vkms_color_test_lut32_identity(struct kunit *test)
+{
+	static const u16 inputs[] = { 0x0000, 0x4000, 0x8000, 0xc000, 0xffff };
+
+	for (size_t i = 0; i < ARRAY_SIZE(inputs); i++)
+		KUNIT_EXPECT_EQ(test, inputs[i],
+				apply_lut32_to_channel_value(test_lut32_identity,
+							     ARRAY_SIZE(test_lut32_identity),
+							     inputs[i], LUT_RED));
+}
+
+/*
+ * Three entries at 0, 0.5 and 1.0. The values tested here fall exactly on
+ * the entries or halfway between two of them, so the expected output is
+ * exact.
+ */
+static const struct drm_color_lut32 test_lut32_three_points[] = {
+	{ 0x00000000, 0x00000000, 0x00000000, 0 },
+	{ 0x80000000, 0x80000000, 0x80000000, 0 },
+	{ 0xffffffff, 0xffffffff, 0xffffffff, 0 },
+};
+
+static void vkms_color_test_lut32_three_points(struct kunit *test)
+{
+	static const struct {
+		u16 input;
+		u16 expected;
+	} cases[] = {
+		{ 0x0000, 0x0000 },
+		{ 0x4000, 0x4000 },
+		{ 0x8000, 0x8000 },
+		{ 0xc000, 0xc000 },
+		{ 0xffff, 0xffff },
+	};
+
+	for (size_t i = 0; i < ARRAY_SIZE(cases); i++)
+		KUNIT_EXPECT_EQ(test, cases[i].expected,
+				apply_lut32_to_channel_value(test_lut32_three_points,
+							     ARRAY_SIZE(test_lut32_three_points),
+							     cases[i].input, LUT_RED));
+}
+
+/* A single-entry LUT returns that entry for every input. */
+static const struct drm_color_lut32 test_lut32_single[] = {
+	{ 0x12345678, 0x12345678, 0x12345678, 0 },
+};
+
+/* A decreasing table must round the same way an increasing one does. */
+static const struct drm_color_lut32 test_lut32_descending[] = {
+	{ 0xffffffff, 0xffffffff, 0xffffffff, 0 },
+	{ 0x00000000, 0x00000000, 0x00000000, 0 },
+};
+
+static void vkms_color_test_lut32_descending(struct kunit *test)
+{
+	KUNIT_EXPECT_EQ(test, 0xffff,
+			apply_lut32_to_channel_value(test_lut32_descending, 2,
+						     0x0000, LUT_RED));
+	KUNIT_EXPECT_EQ(test, 0x7fff,
+			apply_lut32_to_channel_value(test_lut32_descending, 2,
+						     0x8000, LUT_RED));
+	KUNIT_EXPECT_EQ(test, 0x0000,
+			apply_lut32_to_channel_value(test_lut32_descending, 2,
+						     0xffff, LUT_RED));
+}
+
+static void vkms_color_test_lut32_single_entry(struct kunit *test)
+{
+	KUNIT_EXPECT_EQ(test, 0x1234,
+			apply_lut32_to_channel_value(test_lut32_single, 1,
+						     0x0000, LUT_RED));
+	KUNIT_EXPECT_EQ(test, 0x1234,
+			apply_lut32_to_channel_value(test_lut32_single, 1,
+						     0xffff, LUT_RED));
+}
+
 static void vkms_color_srgb_inv_srgb(struct kunit *test)
 {
 	u16 srgb, final;
@@ -385,6 +470,10 @@ static struct kunit_case vkms_color_test_cases[] = {
 	KUNIT_CASE(vkms_color_test_get_uniform_lut_index),
 	KUNIT_CASE(vkms_color_test_lerp),
 	KUNIT_CASE(vkms_color_test_linear),
+	KUNIT_CASE(vkms_color_test_lut32_identity),
+	KUNIT_CASE(vkms_color_test_lut32_three_points),
+	KUNIT_CASE(vkms_color_test_lut32_single_entry),
+	KUNIT_CASE(vkms_color_test_lut32_descending),
 	KUNIT_CASE(vkms_color_srgb_inv_srgb),
 	KUNIT_CASE(vkms_color_ctm_3x4_50_desat),
 	KUNIT_CASE(vkms_color_ctm_3x4_bt709),
drivers/gpu/drm/vkms/vkms_colorop.c +19 -0
index 01b1ed2a5991..584305b0abfd 100644
@@ -119,6 +119,25 @@ static int vkms_initialize_color_pipeline(struct drm_plane *plane, struct drm_pr
 
 	drm_colorop_set_next_property(ops[i - 1], ops[i]);
 
+	i++;
+
+	/* 6th op: 1D LUT */
+	ops[i] = kzalloc_obj(*ops[i]);
+	if (!ops[i]) {
+		drm_err(dev, "KMS: Failed to allocate colorop\n");
+		ret = -ENOMEM;
+		goto cleanup;
+	}
+
+	ret = drm_plane_colorop_curve_1d_lut_init(dev, ops[i], plane, &vkms_colorop_funcs,
+						  VKMS_LUT1D_SIZE,
+						  DRM_COLOROP_LUT1D_INTERPOLATION_LINEAR,
+						  DRM_COLOROP_FLAG_ALLOW_BYPASS);
+	if (ret)
+		goto cleanup;
+
+	drm_colorop_set_next_property(ops[i - 1], ops[i]);
+
 	list->name = kasprintf(GFP_KERNEL, "Color Pipeline %d", ops[0]->base.id);
 
 	return 0;
drivers/gpu/drm/vkms/vkms_composer.c +80 -0

108 lines not on this page · raw message

drivers/gpu/drm/vkms/vkms_composer.h +2 -0
index fa003af12f85..6da85efc5524 100644
@@ -22,6 +22,8 @@ u16 lerp_u16(u16 a, u16 b, s64 t);
 s64 get_uniform_lut_index(const struct vkms_color_lut *lut, u16 channel_value);
 u16 apply_lut_to_channel_value(const struct vkms_color_lut *lut, u16 channel_value,
 			       enum lut_channel channel);
+u16 apply_lut32_to_channel_value(const struct drm_color_lut32 *lut, u32 lut_size,
+				 u16 channel_value, enum lut_channel channel);
 void apply_3x4_matrix(struct pixel_argb_s32 *pixel, const struct drm_color_ctm_3x4 *matrix);
 #endif
 
drivers/gpu/drm/vkms/vkms_drv.h +3 -0
index 1c5205aa6e0c..05fba864cf36 100644
@@ -35,6 +35,9 @@
 
 #define VKMS_GAMMA_LUT_SIZE 256
 
+/* Entries in the 1D LUT colorop of a plane's color pipeline. */
+#define VKMS_LUT1D_SIZE 256
+
 /**
  * struct vkms_frame_info - Structure to store the state of a frame
  *
Signature · 1 line
2.56.1
#7 lore raw

Re: [PATCH 4/7] drm/vkms: add a programmable 1D LUT colorop

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] drm/vkms: raw 64-bit divisions in apply_lut32_to_channel_value() break 32-bit builds - [Medium]…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] drm/vkms: raw 64-bit divisions in apply_lut32_to_channel_value() break 32-bit builds
- [Medium] drm/vkms: NULL dereference on init failure in vkms_initialize_color_pipeline()
--

commit 780023413d21504e42acdc03ff98ae1aafe4ed37
Author: Oarora Etimis <[email protected]>

drm/vkms: add a programmable 1D LUT colorop

This commit adds a 256-entry 1D LUT colorop with linear interpolation to the VKMS plane color pipeline, implements its lookup in the composer, and adds KUnit tests for the interpolation.

Quoted 14 lines
drivers/gpu/drm/vkms/vkms_colorop.c +6 -0
index 01b1ed2a59911..584305b0abfdd 100644
@@ -119,6 +119,25 @@ static int vkms_initialize_color_pipeline(struct drm_plane *plane, struct drm_pr
 
 	drm_colorop_set_next_property(ops[i - 1], ops[i]);
 
+	i++;
+
+	/* 6th op: 1D LUT */
+	ops[i] = kzalloc_obj(*ops[i]);
+	if (!ops[i]) {
+		drm_err(dev, "KMS: Failed to allocate colorop\n");

drivers/gpu/drm/vkms/vkms_colorop.c static int vkms_initialize_color_pipeline(struct drm_plane *plane, struct drm_pr · line 137

Quoted 10 lines
drivers/gpu/drm/vkms/vkms_colorop.c +10 -0
+		ret = -ENOMEM;
+		goto cleanup;
+	}
+
+	ret = drm_plane_colorop_curve_1d_lut_init(dev, ops[i], plane, &vkms_colorop_funcs,
+						  VKMS_LUT1D_SIZE,
+						  DRM_COLOROP_LUT1D_INTERPOLATION_LINEAR,
+						  DRM_COLOROP_FLAG_ALLOW_BYPASS);
+	if (ret)
+		goto cleanup;

[Severity: Medium]
Can this error jump dereference a NULL pointer in drm_colorop_cleanup()?

If drm_plane_colorop_curve_1d_lut_init() -> drm_plane_colorop_init() fails in drm_mode_object_add(), it returns before setting ops[i]->dev or linking ops[i]->head:

drivers/gpu/drm/drm_colorop.c:drm_plane_colorop_init() {
	...
	ret = drm_mode_object_add(dev, &colorop->base, DRM_MODE_OBJECT_COLOROP);
	if (ret)
		return ret;

	colorop->base.properties = &colorop->properties;
	colorop->dev = dev;
	...
	list_add_tail(&colorop->head, &config->colorop_list);
	...
}

The cleanup loop in vkms_initialize_color_pipeline() iterates up to j <= i and calls drm_colorop_cleanup(ops[i]), which runs list_del(&colorop->head) on zeroed list pointers and dereferences colorop->dev.

Quoted 4 lines
drivers/gpu/drm/vkms/vkms_composer.c +0 -0
index 2cd909bc8adcf..e00d1d0b2f2d9 100644

[ ... ]

Quoted 1 line
drivers/gpu/drm/vkms/vkms_composer.c +0 -0
@@ -198,6 +201,61 @@ VISIBLE_IF_KUNIT u16 apply_lut_to_channel_value(const struct vkms_color_lut *lut

[ ... ]

Quoted 19 lines
drivers/gpu/drm/vkms/vkms_composer.c +19 -0
+VISIBLE_IF_KUNIT u16 apply_lut32_to_channel_value(const struct drm_color_lut32 *lut,
+						  u32 lut_size, u16 channel_value,
+						  enum lut_channel channel)
+{
+	u64 position;
+	u32 index, fraction;
+	s64 delta, result;
+	u16 floor_value, ceil_value;
+
+	if (!lut_size)
+		return channel_value;
+
+	if (lut_size == 1)
+		return lut32_channel_value(&lut[0], channel);
+
+	position = (u64)channel_value * (lut_size - 1);
+	index = position / U16_MAX;
+
+	if (index >= lut_size - 1)

drivers/gpu/drm/vkms/vkms_composer.c VISIBLE_IF_KUNIT u16 apply_lut_to_channel_value(const struct vkms_color_lut *lut · line 253

Quoted 10 lines
drivers/gpu/drm/vkms/vkms_composer.c +10 -0
+		return lut32_channel_value(&lut[lut_size - 1], channel);
+
+	fraction = position - (u64)index * U16_MAX;
+
+	floor_value = lut32_channel_value(&lut[index], channel);
+	ceil_value = lut32_channel_value(&lut[index + 1], channel);
+
+	delta = (s64)ceil_value - (s64)floor_value;
+	result = (s64)floor_value +
+		 DIV_ROUND_CLOSEST(delta * fraction, (s64)U16_MAX);

[Severity: High]
Do the 64-bit divisions in apply_lut32_to_channel_value() break 32-bit builds?

position is u64 and delta is s64, so dividing position by U16_MAX with / and dividing delta * fraction by (s64)U16_MAX with DIV_ROUND_CLOSEST() emit __udivdi3 and __divdi3 libcalls on 32-bit architectures, failing at link or modpost time when CONFIG_DRM_VKMS is enabled. Using helpers from linux/math64.h such as div_u64_rem() and DIV_S64_ROUND_CLOSEST() avoids raw 64-bit divisions.

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

PATCH 5/7 drm/vkms: add a 3D LUT colorop

In reply to Oarora Etimis

Add a 3D LUT colorop of 17x17x17 entries, interpolated tetrahedrally. The composer picks the cell each value falls in and weights the four corners of the selected tetrahedron. Red is the fastest moving index, as the uapi documents.

The existing colorops and their order are unchanged.

---
 drivers/gpu/drm/vkms/vkms_colorop.c  |  19 ++++++
 drivers/gpu/drm/vkms/vkms_composer.c | 126 +++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/vkms/vkms_composer.h |   2 +
 drivers/gpu/drm/vkms/vkms_drv.h      |   2 +
 4 files changed, 149 insertions(+)
drivers/gpu/drm/vkms/vkms_colorop.c +19 -0
index 584305b0abfd..cceb9acca0da 100644
@@ -138,6 +138,25 @@ static int vkms_initialize_color_pipeline(struct drm_plane *plane, struct drm_pr
 
 	drm_colorop_set_next_property(ops[i - 1], ops[i]);
 
+	i++;
+
+	/* 7th op: 3D LUT */
+	ops[i] = kzalloc_obj(*ops[i]);
+	if (!ops[i]) {
+		drm_err(dev, "KMS: Failed to allocate colorop\n");
+		ret = -ENOMEM;
+		goto cleanup;
+	}
+
+	ret = drm_plane_colorop_3dlut_init(dev, ops[i], plane, &vkms_colorop_funcs,
+					   VKMS_LUT3D_SIZE,
+					   DRM_COLOROP_LUT3D_INTERPOLATION_TETRAHEDRAL,
+					   DRM_COLOROP_FLAG_ALLOW_BYPASS);
+	if (ret)
+		goto cleanup;
+
+	drm_colorop_set_next_property(ops[i - 1], ops[i]);
+
 	list->name = kasprintf(GFP_KERNEL, "Color Pipeline %d", ops[0]->base.id);
 
 	return 0;
drivers/gpu/drm/vkms/vkms_composer.c +126 -0

144 lines not on this page · raw message

drivers/gpu/drm/vkms/vkms_composer.h +2 -0
index 6da85efc5524..28323df662a4 100644
@@ -24,6 +24,8 @@ u16 apply_lut_to_channel_value(const struct vkms_color_lut *lut, u16 channel_val
 			       enum lut_channel channel);
 u16 apply_lut32_to_channel_value(const struct drm_color_lut32 *lut, u32 lut_size,
 				 u16 channel_value, enum lut_channel channel);
+void apply_3dlut_to_pixel(struct pixel_argb_s32 *pixel,
+			  const struct drm_color_lut32 *lut, u32 size);
 void apply_3x4_matrix(struct pixel_argb_s32 *pixel, const struct drm_color_ctm_3x4 *matrix);
 #endif
 
drivers/gpu/drm/vkms/vkms_drv.h +2 -0
index 05fba864cf36..d975cb684fd9 100644
@@ -38,6 +38,8 @@
 /* Entries in the 1D LUT colorop of a plane's color pipeline. */
 #define VKMS_LUT1D_SIZE 256
 
+/* Entries per dimension in the 3D LUT colorop of a plane's color pipeline. */
+#define VKMS_LUT3D_SIZE 17
 /**
  * struct vkms_frame_info - Structure to store the state of a frame
  *
Signature · 1 line
2.56.1
#9 lore raw

Re: [PATCH 5/7] drm/vkms: add a 3D LUT colorop

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] drm/vkms: raw 64-bit division in apply_3dlut_to_channel_value() breaks 32-bit build --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] drm/vkms: raw 64-bit division in apply_3dlut_to_channel_value() breaks 32-bit build
--

commit 069bec6d031e1f99c4351f4a1c421ad40f2e771c
Author: Oarora Etimis <[email protected]>

drm/vkms: add a 3D LUT colorop

This commit adds a 17x17x17 3D LUT colorop with tetrahedral interpolation to the VKMS plane color pipeline.

Quoted 36 lines
drivers/gpu/drm/vkms/vkms_composer.c +28 -0
index e00d1d0b2f2d9..51ac2ae75cf0c 100644
@@ -256,6 +256,115 @@ VISIBLE_IF_KUNIT u16 apply_lut32_to_channel_value(const struct drm_color_lut32 *
 }
 EXPORT_SYMBOL_IF_KUNIT(apply_lut32_to_channel_value);
 
+/*
+ * Tetrahedral interpolation of one channel in a 3D LUT. The input value of
+ * each axis selects a cell of the LUT; the fractions pick one of the six
+ * tetrahedra the cell is split into, and the four corners of that
+ * tetrahedron are weighted.
+ */
+static u16 apply_3dlut_to_channel_value(const struct drm_color_lut32 *lut, u32 size,
+			       enum lut_channel channel, u16 r, u16 g, u16 b)
+{
+	u32 max_index = size - 1;
+	u32 r_index, g_index, b_index;
+	u32 r_fraction, g_fraction, b_fraction;
+	const struct drm_color_lut32 *v000;
+	u64 position, sum;
+	u16 v000_c, v100_c, v010_c, v001_c;
+	u16 v110_c, v101_c, v011_c, v111_c;
+
+	position = (u64)r * max_index;
+	r_index = position / U16_MAX;
+	r_fraction = position - (u64)r_index * U16_MAX;
+
+	position = (u64)g * max_index;
+	g_index = position / U16_MAX;
+	g_fraction = position - (u64)g_index * U16_MAX;
+
+	position = (u64)b * max_index;
+	b_index = position / U16_MAX;
+	b_fraction = position - (u64)b_index * U16_MAX;

[ ... ]

Quoted 6 lines
drivers/gpu/drm/vkms/vkms_composer.c +6 -0
+VISIBLE_IF_KUNIT void apply_3dlut_to_pixel(struct pixel_argb_s32 *pixel,
+					   const struct drm_color_lut32 *lut,
+					   u32 size)
+{
+	u16 r = clamp_val(pixel->r, 0, 0xffff);
+	u16 g = clamp_val(pixel->g, 0, 0xffff);

drivers/gpu/drm/vkms/vkms_composer.c VISIBLE_IF_KUNIT u16 apply_lut32_to_channel_value(const struct drm_color_lut32 * · line 366

Quoted 10 lines
drivers/gpu/drm/vkms/vkms_composer.c +10 -0
+	u16 b = clamp_val(pixel->b, 0, 0xffff);
+
+	if (size < 2)
+		return;
+
+	pixel->r = apply_3dlut_to_channel_value(lut, size, LUT_RED, r, g, b);
+	pixel->g = apply_3dlut_to_channel_value(lut, size, LUT_GREEN, r, g, b);
+	pixel->b = apply_3dlut_to_channel_value(lut, size, LUT_BLUE, r, g, b);
+}
+EXPORT_SYMBOL_IF_KUNIT(apply_3dlut_to_pixel);

[Severity: High]
Does dividing the u64 variable position with raw / U16_MAX break 32-bit builds with an undefined reference to __udivdi3?

Using div_u64_rem(position, U16_MAX, &r_fraction) avoids the 64-bit division helper, and computing the cell indices and fractions once in apply_3dlut_to_pixel() would also avoid repeating the three divisions for each channel of every pixel.

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

PATCH 6/7 drm/vkms: add a multiplier colorop

In reply to Oarora Etimis

Add a multiplier colorop that scales the channels by an S31.32 gain. The value is sign-magnitude like the 3x4 matrix, so it goes through drm_sm2fixp() first.

---
 drivers/gpu/drm/vkms/vkms_colorop.c  | 17 +++++++++++++++++
 drivers/gpu/drm/vkms/vkms_composer.c | 14 ++++++++++++++
 drivers/gpu/drm/vkms/vkms_composer.h |  1 +
 3 files changed, 32 insertions(+)
drivers/gpu/drm/vkms/vkms_colorop.c +17 -0
index cceb9acca0da..1e1e470be4dd 100644
@@ -157,6 +157,23 @@ static int vkms_initialize_color_pipeline(struct drm_plane *plane, struct drm_pr
 
 	drm_colorop_set_next_property(ops[i - 1], ops[i]);
 
+	i++;
+
+	/* 8th op: multiplier */
+	ops[i] = kzalloc_obj(*ops[i]);
+	if (!ops[i]) {
+		drm_err(dev, "KMS: Failed to allocate colorop\n");
+		ret = -ENOMEM;
+		goto cleanup;
+	}
+
+	ret = drm_plane_colorop_mult_init(dev, ops[i], plane, &vkms_colorop_funcs,
+					  DRM_COLOROP_FLAG_ALLOW_BYPASS);
+	if (ret)
+		goto cleanup;
+
+	drm_colorop_set_next_property(ops[i - 1], ops[i]);
+
 	list->name = kasprintf(GFP_KERNEL, "Color Pipeline %d", ops[0]->base.id);
 
 	return 0;
drivers/gpu/drm/vkms/vkms_composer.c +14 -0
index 51ac2ae75cf0..b94c51264c64 100644
@@ -365,6 +365,17 @@ VISIBLE_IF_KUNIT void apply_3dlut_to_pixel(struct pixel_argb_s32 *pixel,
 }
 EXPORT_SYMBOL_IF_KUNIT(apply_3dlut_to_pixel);
 
+VISIBLE_IF_KUNIT void apply_multiplier_to_pixel(struct pixel_argb_s32 *pixel,
+						u64 multiplier)
+{
+	s64 gain = drm_sm2fixp(multiplier);
+
+	pixel->r = drm_fixp2int_round(drm_fixp_mul(drm_int2fixp(pixel->r), gain));
+	pixel->g = drm_fixp2int_round(drm_fixp_mul(drm_int2fixp(pixel->g), gain));
+	pixel->b = drm_fixp2int_round(drm_fixp_mul(drm_int2fixp(pixel->b), gain));
+}
+EXPORT_SYMBOL_IF_KUNIT(apply_multiplier_to_pixel);
+
 
 static void apply_lut(const struct vkms_crtc_state *crtc_state, struct line_buffer *output_buffer)
 {
@@ -489,6 +500,9 @@ static void apply_colorop(struct pixel_argb_s32 *pixel,
 	case DRM_COLOROP_3D_LUT:
 		apply_3d_lut(pixel, state);
 		break;
+	case DRM_COLOROP_MULTIPLIER:
+		apply_multiplier_to_pixel(pixel, state->multiplier);
+		break;
 	case DRM_COLOROP_FIXED_MATRIX:
 		break;
 	default:
drivers/gpu/drm/vkms/vkms_composer.h +1 -0
index 28323df662a4..5546ee864504 100644
@@ -26,6 +26,7 @@ u16 apply_lut32_to_channel_value(const struct drm_color_lut32 *lut, u32 lut_size
 				 u16 channel_value, enum lut_channel channel);
 void apply_3dlut_to_pixel(struct pixel_argb_s32 *pixel,
 			  const struct drm_color_lut32 *lut, u32 size);
+void apply_multiplier_to_pixel(struct pixel_argb_s32 *pixel, u64 multiplier);
 void apply_3x4_matrix(struct pixel_argb_s32 *pixel, const struct drm_color_ctm_3x4 *matrix);
 #endif
 
Signature · 1 line
2.56.1
#11 lore raw

Re: [PATCH 6/7] drm/vkms: add a multiplier colorop

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] drm/vkms: NULL deref in vkms_initialize_color_pipeline() error cleanup - [Medium] drm/vkms: raw…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] drm/vkms: NULL deref in vkms_initialize_color_pipeline() error cleanup
- [Medium] drm/vkms: raw 64-bit divisions in LUT helpers break 32-bit builds
--

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

drm/vkms: add a multiplier colorop

This commit adds a DRM_COLOROP_MULTIPLIER color operation to the VKMS plane color pipeline and implements per-channel S31.32 sign-magnitude gain scaling in the composer.

Quoted 12 lines
drivers/gpu/drm/vkms/vkms_colorop.c +4 -0
index cceb9acca0da0..1e1e470be4dda 100644
@@ -157,6 +157,23 @@ static int vkms_initialize_color_pipeline(struct drm_plane *plane, struct drm_pr
 
 	drm_colorop_set_next_property(ops[i - 1], ops[i]);
 
+	i++;
+
+	/* 8th op: multiplier */
+	ops[i] = kzalloc_obj(*ops[i]);

drivers/gpu/drm/vkms/vkms_colorop.c static int vkms_initialize_color_pipeline(struct drm_plane *plane, struct drm_pr · line 173

Quoted 10 lines
drivers/gpu/drm/vkms/vkms_colorop.c +10 -0
+	if (!ops[i]) {
+		drm_err(dev, "KMS: Failed to allocate colorop\n");
+		ret = -ENOMEM;
+		goto cleanup;
+	}
+
+	ret = drm_plane_colorop_mult_init(dev, ops[i], plane, &vkms_colorop_funcs,
+					  DRM_COLOROP_FLAG_ALLOW_BYPASS);
+	if (ret)
+		goto cleanup;

[Severity: Medium]
Can jumping to cleanup when drm_plane_colorop_mult_init() fails cause a NULL pointer dereference in drm_colorop_cleanup(ops[i])?

drm_plane_colorop_mult_init() calls drm_plane_colorop_init(), which returns early if drm_mode_object_add() fails, before setting colorop->dev or adding colorop->head to config->colorop_list:

drivers/gpu/drm/drm_colorop.c:drm_plane_colorop_init() {
	...
	ret = drm_mode_object_add(dev, &colorop->base, DRM_MODE_OBJECT_COLOROP);
	if (ret)
		return ret;

	colorop->base.properties = &colorop->properties;
	colorop->dev = dev;
	...
	list_add_tail(&colorop->head, &config->colorop_list);
	...
}

Because the cleanup label loops up to j <= i, it calls drm_colorop_cleanup(ops[i]) on that partially initialized object, which runs list_del(&colorop->head) on an unlinked list head and dereferences the NULL colorop->dev pointer.

drivers/gpu/drm/vkms/vkms_composer.c VISIBLE_IF_KUNIT void apply_3dlut_to_pixel(struct pixel_argb_s32 *pixel, · line 366

Quoted 7 lines
drivers/gpu/drm/vkms/vkms_composer.c +0 -0
index 51ac2ae75cf0c..b94c51264c647 100644
@@ -365,6 +365,17 @@ VISIBLE_IF_KUNIT void apply_3dlut_to_pixel(struct pixel_argb_s32 *pixel,
 }
 EXPORT_SYMBOL_IF_KUNIT(apply_3dlut_to_pixel);

[Severity: Medium]
Do the raw 64-bit divisions in apply_lut32_to_channel_value() and apply_3dlut_to_channel_value() break 32-bit builds with undefined references to __udivdi3 and __divdi3?

Both helpers divide 64-bit values directly by U16_MAX instead of using 64-bit division helpers such as div_u64() or DIV_ROUND_CLOSEST_ULL():

drivers/gpu/drm/vkms/vkms_composer.c:apply_lut32_to_channel_value() {
	...
	position = (u64)channel_value * (lut_size - 1);
	index = position / U16_MAX;
	...
	delta = (s64)ceil_value - (s64)floor_value;
	result = (s64)floor_value +
		 DIV_ROUND_CLOSEST(delta * fraction, (s64)U16_MAX);
	...
}
drivers/gpu/drm/vkms/vkms_composer.c:apply_3dlut_to_channel_value() {
	...
	position = (u64)r * max_index;
	r_index = position / U16_MAX;
	...
	position = (u64)g * max_index;
	g_index = position / U16_MAX;
	...
	position = (u64)b * max_index;
	b_index = position / U16_MAX;
	...
}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#12 lore raw

PATCH 7/7 drm/vkms: add KUnit coverage for the 3D LUT and multiplier colorops

In reply to Oarora Etimis

The 3D LUT test uses a 2x2x2 identity LUT with inputs that reach all six tetrahedra, and checks that values outside [0, 1] saturate. The multiplier test covers gains of 1.0, 0.5, 2.0 and -0.5.

---
 drivers/gpu/drm/vkms/tests/vkms_color_test.c | 105 +++++++++++++++++++++++++++
 1 file changed, 105 insertions(+)
drivers/gpu/drm/vkms/tests/vkms_color_test.c +105 -0
index 0d68de6462d9..15f71a977ed9 100644
@@ -205,6 +205,108 @@ static void vkms_color_test_lut32_single_entry(struct kunit *test)
 						     0xffff, LUT_RED));
 }
 
+/*
+ * A 2x2x2 identity LUT: every corner maps the axis index to the same
+ * value, so any input has to come back unchanged. The entries are ordered
+ * with red as the fastest moving index, as the uapi documents.
+ */
+static const struct drm_color_lut32 test_lut3d_identity[] = {
+	{ 0x00000000, 0x00000000, 0x00000000, 0 },
+	{ 0xffffffff, 0x00000000, 0x00000000, 0 },
+	{ 0x00000000, 0xffffffff, 0x00000000, 0 },
+	{ 0xffffffff, 0xffffffff, 0x00000000, 0 },
+	{ 0x00000000, 0x00000000, 0xffffffff, 0 },
+	{ 0xffffffff, 0x00000000, 0xffffffff, 0 },
+	{ 0x00000000, 0xffffffff, 0xffffffff, 0 },
+	{ 0xffffffff, 0xffffffff, 0xffffffff, 0 },
+};
+
+static void vkms_color_test_lut3d_identity(struct kunit *test)
+{
+	static const struct {
+		u16 r, g, b;
+	} cases[] = {
+		{ 0x0000, 0x0000, 0x0000 },
+		{ 0xffff, 0xffff, 0xffff },
+		{ 0x8000, 0x8000, 0x8000 },
+		{ 0xffff, 0x0000, 0x0000 },
+		{ 0x0000, 0xffff, 0x0000 },
+		{ 0x0000, 0x0000, 0xffff },
+		{ 0x8000, 0x4000, 0x1000 },
+		/* One input per tetrahedron the lists above do not reach. */
+		{ 0x9000, 0x1000, 0x5000 },
+		{ 0x5000, 0x1000, 0x9000 },
+		{ 0x1000, 0x9000, 0x5000 },
+		{ 0x1000, 0x5000, 0x9000 },
+	};
+
+	for (size_t i = 0; i < ARRAY_SIZE(cases); i++) {
+		struct pixel_argb_s32 pixel = {
+			.r = cases[i].r,
+			.g = cases[i].g,
+			.b = cases[i].b,
+			.a = 0xffff,
+		};
+
+		apply_3dlut_to_pixel(&pixel, test_lut3d_identity, 2);
+
+		KUNIT_EXPECT_EQ(test, cases[i].r, pixel.r);
+		KUNIT_EXPECT_EQ(test, cases[i].g, pixel.g);
+		KUNIT_EXPECT_EQ(test, cases[i].b, pixel.b);
+		KUNIT_EXPECT_EQ(test, 0xffff, pixel.a);
+	}
+}
+
+/* A value outside [0, 1] saturates at the LUT input instead of wrapping. */
+static void vkms_color_test_lut3d_clamp(struct kunit *test)
+{
+	struct pixel_argb_s32 pixel = {
+		.r = -1,
+		.g = 0x10000,
+		.b = 0xffff,
+		.a = 0xffff,
+	};
+
+	apply_3dlut_to_pixel(&pixel, test_lut3d_identity, 2);
+
+	KUNIT_EXPECT_EQ(test, 0x0000, pixel.r);
+	KUNIT_EXPECT_EQ(test, 0xffff, pixel.g);
+	KUNIT_EXPECT_EQ(test, 0xffff, pixel.b);
+}
+
+static void vkms_color_test_multiplier(struct kunit *test)
+{
+	struct pixel_argb_s32 pixel;
+
+	/* 1.0 leaves the pixel unchanged. */
+	pixel = (struct pixel_argb_s32){ .r = 0x8000, .g = 0x4000, .b = 0x2000 };
+	apply_multiplier_to_pixel(&pixel, 1ULL << 32);
+	KUNIT_EXPECT_EQ(test, 0x8000, pixel.r);
+	KUNIT_EXPECT_EQ(test, 0x4000, pixel.g);
+	KUNIT_EXPECT_EQ(test, 0x2000, pixel.b);
+
+	/* 0.5 halves every channel. */
+	pixel = (struct pixel_argb_s32){ .r = 0x8000, .g = 0x4000, .b = 0x2000 };
+	apply_multiplier_to_pixel(&pixel, 1ULL << 31);
+	KUNIT_EXPECT_EQ(test, 0x4000, pixel.r);
+	KUNIT_EXPECT_EQ(test, 0x2000, pixel.g);
+	KUNIT_EXPECT_EQ(test, 0x1000, pixel.b);
+
+	/* 2.0 doubles every channel. */
+	pixel = (struct pixel_argb_s32){ .r = 0x4000, .g = 0x2000, .b = 0x1000 };
+	apply_multiplier_to_pixel(&pixel, 1ULL << 33);
+	KUNIT_EXPECT_EQ(test, 0x8000, pixel.r);
+	KUNIT_EXPECT_EQ(test, 0x4000, pixel.g);
+	KUNIT_EXPECT_EQ(test, 0x2000, pixel.b);
+
+	/* A negative gain flips the sign; the composer clamps it at the end. */
+	pixel = (struct pixel_argb_s32){ .r = 0x8000, .g = 0x4000, .b = 0x2000 };
+	apply_multiplier_to_pixel(&pixel, (1ULL << 63) | (1ULL << 31));
+	KUNIT_EXPECT_EQ(test, -0x4000, pixel.r);
+	KUNIT_EXPECT_EQ(test, -0x2000, pixel.g);
+	KUNIT_EXPECT_EQ(test, -0x1000, pixel.b);
+}
+
 static void vkms_color_srgb_inv_srgb(struct kunit *test)
 {
 	u16 srgb, final;
@@ -474,6 +576,9 @@ static struct kunit_case vkms_color_test_cases[] = {
 	KUNIT_CASE(vkms_color_test_lut32_three_points),
 	KUNIT_CASE(vkms_color_test_lut32_single_entry),
 	KUNIT_CASE(vkms_color_test_lut32_descending),
+	KUNIT_CASE(vkms_color_test_lut3d_identity),
+	KUNIT_CASE(vkms_color_test_lut3d_clamp),
+	KUNIT_CASE(vkms_color_test_multiplier),
 	KUNIT_CASE(vkms_color_srgb_inv_srgb),
 	KUNIT_CASE(vkms_color_ctm_3x4_50_desat),
 	KUNIT_CASE(vkms_color_ctm_3x4_bt709),
Signature · 1 line
2.56.1
maintainer
#13 lore raw
In reply to Oarora Etimis

Thanks for the patches!

Quoted 10 lines · Oarora Etimis

The vkms plane pipeline has a fixed matrix, two 1D curves and two 3x4 matrices. Userspace cannot supply its own LUT, do a 3D lookup, or apply a gain to the channels.

Patches 1 to 3 fix three bugs in the pipeline plumbing. Patches 4 to 6 add a 1D LUT, a 3D LUT and a multiplier. Patch 7 adds KUnit coverage for the 3D LUT and the multiplier; the 1D LUT gets its coverage in patch 4.

The color math has KUnit coverage; there is no IGT test for the color pipeline yet, so the uAPI paths are not exercised end-to-end.

I am a bit suprised, Harry worked on some tests: https://lists.freedesktop.org/archives/igt-dev/2024-August/077102.html

I don't have a computer on hand to check if it is merged or not, but you can dig on lore to find relevant tests.

Quoted 29 lines

The three new colorops are appended after the existing ones; their order is unchanged.

---
Oarora Etimis (7):
drm/vkms: add the colorops of a plane that is being enabled drm/vkms: free the colorop whose initialization failed drm/vkms: use the colorop states of the state being composed drm/vkms: add a programmable 1D LUT colorop
drm/vkms: add a 3D LUT colorop
drm/vkms: add a multiplier colorop
drm/vkms: add KUnit coverage for the 3D LUT and multiplier colorops

drivers/gpu/drm/vkms/tests/vkms_color_test.c | 194 +++++++++++++++++
drivers/gpu/drm/vkms/vkms_colorop.c          |  61 +++++-
drivers/gpu/drm/vkms/vkms_composer.c         | 314 +++++++++++++++++++++++----
drivers/gpu/drm/vkms/vkms_composer.h         |   5 +
drivers/gpu/drm/vkms/vkms_crtc.c             |  59 ++++-
drivers/gpu/drm/vkms/vkms_drv.h              |  61 ++++++
drivers/gpu/drm/vkms/vkms_plane.c            |   2 +
7 files changed, 646 insertions(+), 50 deletions(-)

---
base-commit: 5ce32a0c94a827ff44ee60898c28060714dcff75
change-id: 20261113-colorop-693f016a156d

Best regards,

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