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 at
atomic_check(), before the new colorop states are published, together
with a reference of the LUT blob, and read only that copy.

Fixes: c1e578bd08da ("drm/vkms: Add enumerated 1D curve colorop")

Signed-off-by: Oarora Etimis <[email protected]>
---
 drivers/gpu/drm/vkms/vkms_colorop.c  |  4 +-
 drivers/gpu/drm/vkms/vkms_composer.c | 92 +++++++++++++++++++-----------------
 drivers/gpu/drm/vkms/vkms_crtc.c     | 44 ++++++++++++++++-
 drivers/gpu/drm/vkms/vkms_drv.h      | 56 ++++++++++++++++++++++
 drivers/gpu/drm/vkms/vkms_plane.c    |  2 +
 5 files changed, 150 insertions(+), 48 deletions(-)

diff --git a/drivers/gpu/drm/vkms/vkms_colorop.c 
b/drivers/gpu/drm/vkms/vkms_colorop.c
index 4e6a82193f2e..2c4188f80e9b 100644
--- a/drivers/gpu/drm/vkms/vkms_colorop.c
+++ b/drivers/gpu/drm/vkms/vkms_colorop.c
@@ -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;
diff --git a/drivers/gpu/drm/vkms/vkms_composer.c 
b/drivers/gpu/drm/vkms/vkms_composer.c
index 7a6a7875520c..2cd909bc8adc 100644
--- a/drivers/gpu/drm/vkms/vkms_composer.c
+++ b/drivers/gpu/drm/vkms/vkms_composer.c
@@ -247,39 +247,48 @@ VISIBLE_IF_KUNIT void apply_3x4_matrix(struct 
pixel_argb_s32 *pixel,
 }
 EXPORT_SYMBOL_IF_KUNIT(apply_3x4_matrix);
 
-static void apply_colorop(struct pixel_argb_s32 *pixel, struct drm_colorop 
*colorop)
+static void apply_1d_curve(struct pixel_argb_s32 *pixel,
+                          const struct vkms_colorop_state *state)
 {
-       struct drm_colorop_state *colorop_state = colorop->state;
-       struct drm_device *dev = colorop->dev;
-
-       if (colorop->type == DRM_COLOROP_1D_CURVE) {
-               switch (colorop_state->curve_1d_type) {
-               case DRM_COLOROP_1D_CURVE_SRGB_INV_EOTF:
-                       pixel->r = apply_lut_to_channel_value(&srgb_inv_eotf, 
pixel->r, LUT_RED);
-                       pixel->g = apply_lut_to_channel_value(&srgb_inv_eotf, 
pixel->g, LUT_GREEN);
-                       pixel->b = apply_lut_to_channel_value(&srgb_inv_eotf, 
pixel->b, LUT_BLUE);
-                       break;
-               case DRM_COLOROP_1D_CURVE_SRGB_EOTF:
-                       pixel->r = apply_lut_to_channel_value(&srgb_eotf, 
pixel->r, LUT_RED);
-                       pixel->g = apply_lut_to_channel_value(&srgb_eotf, 
pixel->g, LUT_GREEN);
-                       pixel->b = apply_lut_to_channel_value(&srgb_eotf, 
pixel->b, LUT_BLUE);
-                       break;
-               default:
-                       drm_WARN_ONCE(dev, true,
-                                     "unknown colorop 1D curve type %d\n",
-                                     colorop_state->curve_1d_type);
-                       break;
-               }
-       } else if (colorop->type == DRM_COLOROP_CTM_3X4) {
-               if (colorop_state->data)
-                       apply_3x4_matrix(pixel,
-                                        (struct drm_color_ctm_3x4 
*)colorop_state->data->data);
-       } else if (colorop->type == DRM_COLOROP_FIXED_MATRIX) {
-               /*
-                * Fixed Matrix is a no-op here. YUV-to-RGB conversion
-                * is handled at format read time via the
-                * conversion_matrix set in vkms_plane_atomic_update().
-                */
+       struct drm_device *dev = state->colorop->dev;
+
+       switch (state->curve_1d_type) {
+       case DRM_COLOROP_1D_CURVE_SRGB_INV_EOTF:
+               pixel->r = apply_lut_to_channel_value(&srgb_inv_eotf, pixel->r, 
LUT_RED);
+               pixel->g = apply_lut_to_channel_value(&srgb_inv_eotf, pixel->g, 
LUT_GREEN);
+               pixel->b = apply_lut_to_channel_value(&srgb_inv_eotf, pixel->b, 
LUT_BLUE);
+               break;
+       case DRM_COLOROP_1D_CURVE_SRGB_EOTF:
+               pixel->r = apply_lut_to_channel_value(&srgb_eotf, pixel->r, 
LUT_RED);
+               pixel->g = apply_lut_to_channel_value(&srgb_eotf, pixel->g, 
LUT_GREEN);
+               pixel->b = apply_lut_to_channel_value(&srgb_eotf, pixel->b, 
LUT_BLUE);
+               break;
+       default:
+               drm_WARN_ONCE(dev, true, "unknown colorop 1D curve type %d\n",
+                             state->curve_1d_type);
+               break;
+       }
+}
+
+static void apply_colorop(struct pixel_argb_s32 *pixel,
+                         const struct vkms_colorop_state *state)
+{
+       const struct drm_colorop *colorop = state->colorop;
+
+       switch (colorop->type) {
+       case DRM_COLOROP_1D_CURVE:
+               apply_1d_curve(pixel, state);
+               break;
+       case DRM_COLOROP_CTM_3X4:
+               if (state->data)
+                       apply_3x4_matrix(pixel, (struct drm_color_ctm_3x4 
*)state->data->data);
+               break;
+       case DRM_COLOROP_FIXED_MATRIX:
+               break;
+       default:
+               drm_WARN_ONCE(colorop->dev, true, "unknown colorop type %d\n",
+                             colorop->type);
+               break;
        }
 }
 
@@ -289,8 +298,6 @@ static void pre_blend_color_transform(const struct 
vkms_plane_state *plane_state
        struct pixel_argb_s32 pixel;
 
        for (size_t x = 0; x < output_buffer->n_pixels; x++) {
-               struct drm_colorop *colorop = 
plane_state->base.base.color_pipeline;
-
                /*
                 * Some operations, such as applying a BT709 encoding matrix,
                 * followed by a decoding matrix, require that we preserve
@@ -306,18 +313,15 @@ static void pre_blend_color_transform(const struct 
vkms_plane_state *plane_state
                pixel.g = output_buffer->pixels[x].g;
                pixel.b = output_buffer->pixels[x].b;
 
-               while (colorop) {
-                       struct drm_colorop_state *colorop_state;
-
-                       colorop_state = colorop->state;
-
-                       if (!colorop_state)
-                               return;
+               for (size_t i = 0; i < ARRAY_SIZE(plane_state->colorop_states); 
i++) {
+                       const struct vkms_colorop_state *state;
 
-                       if (!colorop_state->bypass)
-                               apply_colorop(&pixel, colorop);
+                       state = &plane_state->colorop_states[i];
+                       if (!state->colorop)
+                               break;
 
-                       colorop = colorop->next;
+                       if (!state->bypass)
+                               apply_colorop(&pixel, state);
                }
 
                /* clamp values */
diff --git a/drivers/gpu/drm/vkms/vkms_crtc.c b/drivers/gpu/drm/vkms/vkms_crtc.c
index 7d18178d5320..8e26048c4e72 100644
--- a/drivers/gpu/drm/vkms/vkms_crtc.c
+++ b/drivers/gpu/drm/vkms/vkms_crtc.c
@@ -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>
@@ -112,6 +113,37 @@ static const struct drm_crtc_funcs vkms_crtc_funcs = {
        .verify_crc_source      = vkms_verify_crc_source,
 };
 
+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 int vkms_crtc_atomic_check(struct drm_crtc *crtc,
                                  struct drm_atomic_commit *state)
 {
@@ -171,6 +203,15 @@ static int vkms_crtc_atomic_check(struct drm_crtc *crtc,
                        to_vkms_plane_state(plane_state);
        }
 
+       /*
+        * Record the color operations of this commit before swap_state()
+        * publishes the new colorop states: the composer runs asynchronously
+        * and must not read drm_colorop::state, which a later commit can
+        * replace and free.
+        */
+       for (i = 0; i < vkms_state->num_active_planes; i++)
+               vkms_plane_colorops_update(vkms_state->active_planes[i], state);
+
        return 0;
 }
 
@@ -191,6 +232,7 @@ static void vkms_crtc_atomic_flush(struct drm_crtc *crtc,
        __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 +247,7 @@ static void vkms_crtc_atomic_flush(struct drm_crtc *crtc,
                crtc->state->event = NULL;
        }
 
-       vkms_output->composer_state = to_vkms_crtc_state(crtc->state);
+       vkms_output->composer_state = crtc_state;
 
        spin_unlock_irq(&vkms_output->lock);
 }
diff --git a/drivers/gpu/drm/vkms/vkms_drv.h b/drivers/gpu/drm/vkms/vkms_drv.h
index b7eb7033fb4b..1c5205aa6e0c 100644
--- a/drivers/gpu/drm/vkms/vkms_drv.h
+++ b/drivers/gpu/drm/vkms/vkms_drv.h
@@ -6,6 +6,7 @@
 #include <linux/hrtimer.h>
 
 #include <drm/drm.h>
+#include <drm/drm_colorop.h>
 #include <drm/drm_framebuffer.h>
 #include <drm/drm_gem.h>
 #include <drm/drm_gem_atomic_helper.h>
@@ -25,6 +26,13 @@
 
 #define NUM_OVERLAY_PLANES 8
 
+/*
+ * Number of color operations in a plane's color pipeline. Must match the
+ * number of colorops that vkms_initialize_color_pipeline() creates, with
+ * room for the ones that follow.
+ */
+#define VKMS_MAX_COLOROPS 10
+
 #define VKMS_GAMMA_LUT_SIZE 256
 
 /**
@@ -139,6 +147,44 @@ struct conversion_matrix {
        int y_offset;
 };
 
+/**
+ * struct vkms_colorop_state - colorop state recorded at commit time
+ * @colorop: the color operation these states belong to
+ * @data: LUT blob, with a reference held by this state
+ * @multiplier: gain of a DRM_COLOROP_MULTIPLIER
+ * @curve_1d_type: curve of a DRM_COLOROP_1D_CURVE
+ * @bypass: whether the operation is bypassed
+ *
+ * The composer must not read drm_colorop::state: that is the current value
+ * of the colorop, not the one the frame is built from, and the worker runs
+ * asynchronously. Writing the value a colorop property already has does not
+ * count as a change, so core leaves the plane out of that commit and the
+ * replaced state can be freed while a published frame still refers to it.
+ * Copy the states the composer needs instead.
+ */
+struct vkms_colorop_state {
+       const struct drm_colorop *colorop;
+       struct drm_property_blob *data;
+       u64 multiplier;
+       enum drm_colorop_curve_1d_type curve_1d_type;
+       bool bypass;
+};
+
+/**
+ * vkms_colorop_state_release - drop what a state holds
+ * @state: the state to clear
+ *
+ * The blob reference is the only thing a state owns. Clearing a state
+ * without dropping it leaks that reference, so every place that discards
+ * one goes through here.
+ */
+static inline void
+vkms_colorop_state_release(struct vkms_colorop_state *state)
+{
+       drm_property_blob_put(state->data);
+       memset(state, 0, sizeof(*state));
+}
+
 /**
  * struct vkms_plane_state - Driver specific plane state
  * @base: base plane state
@@ -146,14 +192,24 @@ struct conversion_matrix {
  * @pixel_read_line: function to read a pixel line in this plane. The creator 
of a
  *                  struct vkms_plane_state must ensure that this pointer is 
valid
  * @conversion_matrix: matrix used for yuv formats to convert to rgb
+ * @colorop_states: the color operations of this plane's pipeline, with the 
state
+ *           the composer needs, recorded when this state was committed
  */
 struct vkms_plane_state {
        struct drm_shadow_plane_state base;
        struct vkms_frame_info frame_info;
        pixel_read_line_t pixel_read_line;
        struct conversion_matrix conversion_matrix;
+       struct vkms_colorop_state colorop_states[VKMS_MAX_COLOROPS];
 };
 
+static inline void
+vkms_plane_colorops_release(struct vkms_plane_state *plane_state)
+{
+       for (size_t i = 0; i < ARRAY_SIZE(plane_state->colorop_states); i++)
+               vkms_colorop_state_release(&plane_state->colorop_states[i]);
+}
+
 struct vkms_plane {
        struct drm_plane base;
 };
diff --git a/drivers/gpu/drm/vkms/vkms_plane.c 
b/drivers/gpu/drm/vkms/vkms_plane.c
index fd3d34dcf0fe..b05475b0f5d4 100644
--- a/drivers/gpu/drm/vkms/vkms_plane.c
+++ b/drivers/gpu/drm/vkms/vkms_plane.c
@@ -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);
 }

-- 
2.56.1

Reply via email to