Thank you for your contribution! Sashiko AI review found 13 potential
issues to consider:
- [High] drm/msm/dcp: broken HFI pipe detach lifecycle in
  dcp_plane_atomic_update()
- [High] drm/msm/dcp: stale plane visibility in dcp_plane_atomic_check()
- [High] drm/msm/dcp: missing or misordered HFI detach on pipe
  reassignment
- [High] drm/msm/dcp: unlocked plane->state access in
  dcp_plane_pipe_busy()
- [High] drm/msm/dcp: 90/270-degree rotation ignored in scaling and perf
  checks
- [High] drm/msm/dcp: divide-by-zero in dcp_plane_calc_bw() on zero
  vblank
- [High] drm/msm/dcp: missing dcp_plane_state.h include in
  dcp_crtc_aggregate_bw()
- [Medium] drm/msm/dcp: NULL return in dcp_plane_atomic_create_state()
  and missing format copy
- [Medium] drm/msm/dcp: stale bandwidth vote when planes hide in
  dcp_crtc_atomic_begin()
- [Medium] drm/msm/dcp: chroma planes omitted in dcp_plane_calc_perf()
  bpp
- [Medium] drm/msm/dcp: missing minimum source size check for RGB in
  dcp_plane_atomic_check()
- [Medium] drm/msm/dcp: broken IS_ERR() and NULL checks in
  dcp_plane_atomic_check()
- [Medium] drm/msm/dcp: non-alpha formats not forced to opaque blend in
  dcp_plane
--

commit 43ca0df7f906b25d403e77023334a1fac27d3027
Author: Jayant Shekhar <[email protected]>

drm/msm/dcp: Add DCP planes

This commit adds DRM plane support for the DCP display pipeline, including
format and scaling validation, HFI pipe assignment, bandwidth calculation,
and HFI property packet updates.

> diff --git a/drivers/gpu/drm/msm/dcp/dcp_crtc.c 
> b/drivers/gpu/drm/msm/dcp/dcp_crtc.c
> index 512da8c4ddbcb..f58667ca458e9 100644
> --- a/drivers/gpu/drm/msm/dcp/dcp_crtc.c
> +++ b/drivers/gpu/drm/msm/dcp/dcp_crtc.c
> @@ -84,6 +84,36 @@ dcp_crtc_atomic_create_state(struct drm_crtc *crtc)
>       return &cstate->base;
>  }
>  
> +static void dcp_crtc_aggregate_bw(struct drm_crtc_state *crtc_state,
> +                               bool enable)
> +{
> +     struct dcp_crtc_state *cstate = to_dcp_crtc_state(crtc_state);
> +     struct drm_plane *plane;
> +     const struct drm_plane_state *pstate;
> +
> +     cstate->dram_ib  = 0;
> +     cstate->dram_ab  = 0;
> +     cstate->llcc_ib  = 0;
> +     cstate->llcc_ab  = 0;
> +     cstate->core_clk = 0;
> +
> +     if (!enable)
> +             return;
> +
> +     drm_atomic_crtc_state_for_each_plane_state(plane, pstate, crtc_state) {
> +             if (!pstate->visible)
> +                     continue;
> +
> +             const struct dcp_plane_state *dcp_pstate = 
> to_dcp_plane_state(pstate);
> +
> +             cstate->dram_ib  = max(cstate->dram_ib, dcp_pstate->fetch_bw);
> +             cstate->dram_ab += dcp_pstate->fetch_bw;
> +             cstate->llcc_ib  = max(cstate->llcc_ib, dcp_pstate->fetch_bw);
> +             cstate->llcc_ab += dcp_pstate->fetch_bw;
> +             cstate->core_clk = max(cstate->core_clk, dcp_pstate->clk);
> +     }
> +}

[Severity: High]
Does dcp_crtc.c fail to build here without including dcp_plane_state.h?
dcp_crtc_aggregate_bw() calls to_dcp_plane_state() and dereferences struct
dcp_plane_state, which are defined in dcp_plane_state.h, but that header is
only included in the later commit "drm/msm/dcp: Integrate the DSI bridge
with KMS".

[ ... ]

> @@ -538,6 +571,8 @@ static int dcp_crtc_atomic_check(struct drm_crtc *crtc,
>               return -E2BIG;
>       }
>  
> +     dcp_crtc_aggregate_bw(crtc_state, true);
> +
>       return 0;
>  }

[Severity: Medium]
Does disabling or clipping all planes on an active CRTC leave stale
bandwidth and clock votes pinned in firmware?

When no visible planes remain, dcp_crtc_aggregate_bw() zeroes dram_ib,
dram_ab, llcc_ib, llcc_ab, and core_clk, but dcp_crtc_atomic_begin() skips
updating the firmware vote when those fields reach zero:

drivers/gpu/drm/msm/dcp/dcp_crtc.c:dcp_crtc_atomic_begin() {
    ...
        if (cstate->core_clk > 0 || cstate->dram_ib > 0) {
                ret = dcp_hfi_crtc_set_bw(dcp_kms->hfi, session_id,
                                          cstate->dram_ib,  cstate->dram_ab,
                                          cstate->llcc_ib,  cstate->llcc_ab,
                                          cstate->core_clk);
    ...
}

Conversely, when at least one plane is visible, dcp_crtc_atomic_begin()
resends dcp_hfi_crtc_set_bw() on every frame even when the aggregated votes
have not changed.

> diff --git a/drivers/gpu/drm/msm/dcp/dcp_plane.c 
> b/drivers/gpu/drm/msm/dcp/dcp_plane.c
> new file mode 100644
> index 0000000000000..ff6ce28d61c8b
> --- /dev/null
> +++ b/drivers/gpu/drm/msm/dcp/dcp_plane.c

[ ... ]

> +static bool dcp_plane_pipe_busy(struct drm_atomic_commit *state,
> +                             struct drm_plane *self, u32 pipe_id)
> +{
> +     struct drm_plane *plane;
> +     struct drm_plane_state *plane_state;
> +     struct drm_plane *iter_plane;
> +     struct drm_plane_state *new_plane_state;
> +     int i;
> +
> +     drm_for_each_plane(plane, state->dev) {
> +             if (plane == self)
> +                     continue;
> +
> +             plane_state = plane->state;
> +             for_each_new_plane_in_state(state, iter_plane, new_plane_state, 
> i) {
> +                     if (iter_plane == plane) {
> +                             plane_state = new_plane_state;
> +                             break;
> +                     }
> +             }
> +
> +             if (plane_state && plane_state->crtc && plane_state->visible &&
> +                 to_dcp_plane_state(plane_state)->hfi_pipe_id == pipe_id)
> +                     return true;
> +     }
> +
> +     return false;
> +}

[Severity: High]
Can dcp_plane_pipe_busy() trigger a use-after-free on plane->state or assign
the same HFI pipe to two planes during concurrent commits across CRTCs?

An atomic commit on CRTC 0 does not hold plane->mutex for planes bound to
CRTC 1 that are not part of state:

CPU0 (commit on CRTC 0)           CPU1 (commit on CRTC 1)
dcp_plane_atomic_check()
  dcp_plane_assign_pipe()
    dcp_plane_pipe_busy()
      plane_state = plane->state
                                  drm_atomic_helper_swap_state()
                                  ...
                                  dcp_plane_atomic_destroy_state()
                                    kfree(old_pstate)
      plane_state->visible
        <-- use-after-free

Even without a free, nothing serializes pipe allocation across CRTCs, so two
concurrent atomic checks can both see the same pipe_id as free in
dcp_plane_pipe_busy() and assign it to two active planes.

[ ... ]

> +static u64 dcp_plane_calc_bw(u32 bpp,
> +                          const struct drm_display_mode *mode,
> +                          const struct drm_rect *src,
> +                          const struct drm_rect *dst)
> +{
> +     int src_w = drm_rect_width(src) >> 16;
> +     int src_h = drm_rect_height(src) >> 16;
> +     int dst_h = drm_rect_height(dst);
> +     int fps   = drm_mode_vrefresh(mode);
> +     int vbp   = mode->vtotal - mode->vsync_end;
> +     int vpw   = mode->vsync_end - mode->vsync_start;
> +     int vfp   = mode->vsync_start - mode->vdisplay;
> +     u32 hw_latency = 25;
> +     u64 scale_factor = (src_h > dst_h) ? mult_frac(src_h, 1, dst_h) : 1;
> +     u64 plane_bw, prefill_bw;
> +
> +     plane_bw = (u64)src_w * mode->vtotal * fps * bpp * scale_factor;
> +     prefill_bw = plane_bw * hw_latency;
> +
> +     if ((vbp + vpw) > hw_latency)
> +             do_div(prefill_bw, (vbp + vpw));
> +     else if ((vbp + vpw + vfp) < hw_latency)
> +             do_div(prefill_bw, (vbp + vpw + vfp));
> +     else
> +             do_div(prefill_bw, hw_latency);
> +
> +     /* HFI DRAM/LLCC votes are expressed in Kbps. */
> +     return div_u64(max(plane_bw, prefill_bw), 1000);
> +}

[Severity: High]
Can do_div(prefill_bw, (vbp + vpw + vfp)) divide by zero when a mode has
zero vertical blanking?

Both drm_mode_validate_basic() and dcp_crtc_mode_valid() allow a mode where
vdisplay == vsync_start == vsync_end == vtotal, which makes vbp + vpw + vfp
equal to 0 and hits the else-if branch.  Also, computing scale_factor as
mult_frac(src_h, 1, dst_h) before multiplying plane_bw truncates fractional
vertical downscaling to an integer.

[ ... ]

> +void dcp_plane_calc_perf(const struct drm_plane_state *state,
> +                      const struct drm_display_mode *mode,
> +                      struct dcp_plane_perf *perf)
> +{
> +     struct drm_rect src, dst;
> +     u32 bpp;
> +
> +     memset(perf, 0, sizeof(*perf));
> +
> +     if (!state || !state->visible || !state->fb || !mode || !mode->clock ||
> +         !drm_mode_vrefresh(mode))
> +             return;
> +
> +     src = drm_plane_state_src(state);
> +     dst = drm_plane_state_dest(state);
> +     bpp = drm_format_info_bpp(state->fb->format, 0);
> +
> +     perf->fetch_bw = dcp_plane_calc_bw(bpp, mode, &src, &dst);
> +     perf->clk = dcp_plane_calc_clk(mode, &src, &dst);
> +}

[Severity: Medium]
Does calling drm_format_info_bpp(state->fb->format, 0) omit the chroma
planes for multi-planar YUV formats?  For NV12, NV21, P010, YUV420, and
YVU420, plane 0 only accounts for the luma plane, under-calculating
fetch_bw by a third (and drm_format_info_bpp() returns bits per pixel
rather than bytes per pixel).

[ ... ]

> +static int dcp_plane_build_one_hfi_props_packet(struct dcp_plane_state 
> *pstate,
> +                                             const struct drm_plane_state 
> *state,
> +                                             u32 session_id, u32 packet_id,
> +                                             u8 *packet)
> +{
> +     struct drm_framebuffer *fb = state->fb;

[ ... ]

> +     hfi_rotation = dcp_drm_to_hfi_rotation(state->rotation);
> +     hfi_blend_op = dcp_drm_to_hfi_blend_op(state->pixel_blend_mode);
> +
> +     src_x = state->src_x >> 16;
> +     src_y = state->src_y >> 16;
> +     src_w = state->src_w >> 16;
> +     src_h = state->src_h >> 16;
> +     dst_x = state->crtc_x;
> +     dst_y = state->crtc_y;
> +     dst_w = state->crtc_w;
> +     dst_h = state->crtc_h;
> +
> +     hfi_alpha = dcp_hfi_scale_alpha(state, state->alpha);
> +     hfi_bg_alpha = dcp_hfi_scale_alpha(state,
> +                                        
> dcp_compute_bg_alpha(state->pixel_blend_mode,
> +                                                             state->alpha));

[Severity: Medium]
Should planes whose framebuffer format lacks an alpha channel
(!fb->format->has_alpha) fall back to DRM_MODE_BLEND_PIXEL_NONE?  Without
that check, formats such as XRGB8888, RGB565, NV12, and P010 use the default
DRM_MODE_BLEND_PREMULTI mode and program HFI_BLEND_OP_PREMULTIPLIED with
bg_alpha = 0 (when alpha is opaque) instead of HFI_BLEND_OP_OPAQUE.

[ ... ]

> +static int dcp_plane_atomic_check(struct drm_plane *plane,
> +                               struct drm_atomic_commit *state)
> +{

[ ... ]

> +     if (!new_state->crtc)
> +             return 0;
> +
> +     crtc_state = drm_atomic_get_new_crtc_state(state, new_state->crtc);
> +     if (IS_ERR(crtc_state)) {
> +             ret = PTR_ERR(crtc_state);
> +             drm_err(plane->dev, "plane%u: failed to get CRTC state: %d\n",
> +                     plane->base.id, ret);
> +             return ret;
> +     }

[Severity: Medium]
drm_atomic_get_new_crtc_state() returns NULL, not an ERR_PTR, when the CRTC
is not in state, so IS_ERR(crtc_state) will not catch a NULL crtc_state
before drm_atomic_helper_check_plane_state() dereferences it.  Similarly,
in dcp_plane_assign_pipe(), dcp_plane_hfi_pipe_valid() checks
!dcp_kms->caps, but dcp_plane_assign_pipe() then dereferences caps
unconditionally.

> +
> +     if (!new_state->fb)
> +             return 0;

[Severity: High]
Does returning early on !new_state->crtc or !new_state->fb leave
new_state->visible set to true when a visible plane is disabled?

dcp_plane_atomic_duplicate_state() copies visible = true from the old
state, and returning before drm_atomic_helper_check_plane_state() leaves
new_state->visible true.  drm_atomic_plane_disabling() then evaluates to
false in drm_atomic_helper_commit_planes(), so neither
dcp_plane_atomic_disable() nor dcp_plane_atomic_update() runs, leaving the
HFI pipe attached in firmware scanning out old_state->fb after
dcp_plane_cleanup_fb() unpins its IOVA.

In addition, calling dcp_plane_assign_pipe() before
drm_atomic_helper_check_plane_state() causes dcp_plane_pipe_busy() to read
stale visible flags on higher-indexed planes in the same commit that have
not been checked yet, and assigns an HFI pipe even when the current plane
is about to be clipped to invisible.

> +
> +     scaling = new_state->src_w != ((u32)new_state->crtc_w << 16) ||
> +                       new_state->src_h != ((u32)new_state->crtc_h << 16);
> +
> +     pstate->hfi_pipe_id = dcp_plane_assign_pipe(dcp_kms, state, plane,
> +                                                 pstate, 
> new_state->fb->format->is_yuv,
> +                                                 scaling);
> +     if (pstate->hfi_pipe_id == U32_MAX) {
> +             drm_err(plane->dev, "plane%u: no compatible HFI pipe 
> available\n",
> +                     plane->base.id);
> +             return -ENOSPC;
> +     }
> +
> +     if (dcp_plane_pipe_is_vig(dcp_kms->caps, pstate->hfi_pipe_id)) {
> +             min_scale = FRAC_16_16(1, DCP_MAX_UPSCALE_RATIO);
> +             max_scale = DCP_MAX_DOWNSCALE_RATIO << 16;
> +     }
> +
> +     ret = drm_atomic_helper_check_plane_state(new_state, crtc_state,
> +                                               min_scale, max_scale,
> +                                               true, true);

[Severity: High]
Does checking scaling before rotating the source rectangle break 90- and
270-degree rotated planes?

When DRM_MODE_ROTATE_90 or DRM_MODE_ROTATE_270 is set on a non-square
plane, drm_atomic_helper_check_plane_state() swaps the source width and
height via drm_rect_rotate() before checking hscale and vscale.  Because
scaling is computed here from the unrotated src_w and src_h, an unscaled
90/270-degree plane is treated as scaled and forced onto a VIG pipe, while
a scaled 90/270-degree plane with src_w == crtc_w << 16 and
src_h == crtc_h << 16 can be assigned a DMA pipe where
DRM_PLANE_NO_SCALING causes drm_atomic_helper_check_plane_state() to fail
with -ERANGE.  dcp_plane_calc_perf() likewise passes the unrotated source
rectangle to dcp_plane_calc_bw() and dcp_plane_calc_clk().

[ ... ]

> +     if (new_state->fb->format->is_yuv) {
> +             u32 src_x = src.x1 >> 16;
> +             u32 src_y = src.y1 >> 16;
> +             u32 src_w = drm_rect_width(&src) >> 16;
> +             u32 src_h = drm_rect_height(&src) >> 16;
> +
> +             if (src_w < 2 || src_h < 2) {
> +                     drm_err(plane->dev,
> +                             "plane%u: YUV source too small: %ux%u\n",
> +                             plane->base.id, src_w, src_h);
> +                     return -EINVAL;
> +             }

[Severity: Medium]
Should non-YUV planes also reject source dimensions smaller than 1 pixel
((drm_rect_width(&src) >> 16) < 1 || (drm_rect_height(&src) >> 16) < 1)?
On a VIG pipe with min_scale = FRAC_16_16(1, 20), sub-pixel 16.16 source
dimensions pass drm_atomic_helper_check_plane_state() and then truncate to
0 when shifted right by 16 in dcp_plane_calc_bw() and
dcp_plane_build_one_hfi_props_packet().

[ ... ]

> +static void dcp_plane_atomic_update(struct drm_plane *plane,
> +                                 struct drm_atomic_commit *state)
> +{
> +     struct drm_plane_state *new_state =
> +             drm_atomic_get_new_plane_state(state, plane);
> +     struct dcp_plane_state *pstate = to_dcp_plane_state(new_state);
> +     int ret;
> +
> +     drm_dbg_driver(plane->dev,
> +                    "plane%u: atomic_update visible=%d fb=%u pipe_id=%u\n",
> +                    plane->base.id, new_state->visible,
> +                    new_state->fb ? new_state->fb->base.id : 0,
> +                    pstate->hfi_pipe_id);
> +
> +     pstate->prop_pkt_size = 0;
> +
> +     if (!new_state->visible || !new_state->fb)
> +             return;
> +
> +     ret = dcp_plane_submit_update(plane, new_state);
> +     if (ret) {
> +             drm_err(plane->dev,
> +                     "plane%u: HFI update failed: %d\n", plane->base.id, 
> ret);
> +             dcp_plane_set_hfi_error(new_state, ret);
> +             return;
> +     }
> +}

[Severity: High]
When a visible plane is moved off-screen while remaining bound to its CRTC
and framebuffer, does returning early on !new_state->visible leave the HFI
pipe attached in firmware?

In drm_atomic_helper_commit_planes(), drm_atomic_plane_disabling() only
checks old_plane_state->crtc && !new_plane_state->crtc, so a plane that
becomes invisible with new_state->crtc != NULL calls
dcp_plane_atomic_update() rather than dcp_plane_atomic_disable().  Because
dcp_plane_atomic_update() returns without detaching old_state's pipe, the
firmware continues scanning out old_state->fb after dcp_plane_cleanup_fb()
unpins its IOVA; and because dcp_plane_pipe_busy() treats invisible planes'
pipes as free, another plane can claim that pipe while the invisible plane
later detaches it in dcp_plane_atomic_disable().

> +
> +static void dcp_plane_atomic_disable(struct drm_plane *plane,
> +                                  struct drm_atomic_commit *state)
> +{
> +     struct drm_plane_state *old_state =
> +             drm_atomic_get_old_plane_state(state, plane);
> +     struct dcp_plane_state *pstate = to_dcp_plane_state(old_state);
> +     int ret;
> +
> +     drm_dbg_driver(plane->dev,
> +                    "plane%u: atomic_disable pipe_id=%u\n",
> +                    plane->base.id, pstate->hfi_pipe_id);
> +
> +     ret = dcp_plane_submit_disable(plane, old_state);
> +     if (ret) {
> +             drm_err(plane->dev,
> +                     "plane%u: HFI detach failed: %d\n", plane->base.id, 
> ret);
> +             dcp_plane_set_hfi_error(old_state, ret);
> +     }
> +
> +     pstate->prop_pkt_size = 0;
> +     pstate->prop_pkt_id = 0;
> +}

[Severity: High]
What happens to the old or reassigned HFI pipe when dcp_plane_assign_pipe()
changes a plane's hfi_pipe_id or reassigns a pipe between planes in the
same commit?

If an active visible plane switches from a DMA pipe to a VIG pipe (for
example, changing from unscaled RGB to YUV or scaled),
dcp_plane_atomic_update() attaches the new pipe_id but never detaches
old_state's hfi_pipe_id.  Conversely, if a higher-indexed plane is disabled
in the same commit in which a lower-indexed plane claims its pipe_id,
drm_atomic_helper_commit_planes() runs the lower-indexed plane's
dcp_plane_atomic_update() first and the higher-indexed plane's
dcp_plane_atomic_disable() second, detaching the pipe that was just
attached.

> +
> +static struct drm_plane_state *
> +dcp_plane_atomic_duplicate_state(struct drm_plane *plane)
> +{
> +     struct dcp_plane_state *pstate, *old_pstate;
> +
> +     if (WARN_ON(!plane->state))
> +             return NULL;
> +
> +     old_pstate = to_dcp_plane_state(plane->state);
> +     pstate = kzalloc_obj(*pstate, GFP_KERNEL);
> +     if (!pstate)
> +             return NULL;
> +
> +     __drm_atomic_helper_plane_duplicate_state(plane, &pstate->base);
> +     pstate->hfi_pipe_id     = old_pstate->hfi_pipe_id;
> +     pstate->src_img_w       = old_pstate->src_img_w;
> +     pstate->src_img_h       = old_pstate->src_img_h;
> +     pstate->fetch_bw       = old_pstate->fetch_bw;
> +     pstate->clk            = old_pstate->clk;
> +     pstate->needs_dirtyfb  = old_pstate->needs_dirtyfb;
> +     pstate->prop_pkt_size   = 0;
> +     pstate->prop_pkt_id     = old_pstate->prop_pkt_id;
> +
> +     return &pstate->base;
> +}
> +
> +static void dcp_plane_atomic_destroy_state(struct drm_plane *plane,
> +                                        struct drm_plane_state *state)
> +{
> +     __drm_atomic_helper_plane_destroy_state(state);
> +     kfree(to_dcp_plane_state(state));
> +}
> +
> +static struct drm_plane_state *
> +dcp_plane_atomic_create_state(struct drm_plane *plane)
> +{
> +     struct dcp_plane_state *pstate;
> +     u32 hfi_pipe_id = to_dcp_plane(plane)->hfi_pipe_id;
> +
> +     pstate = kzalloc_obj(*pstate, GFP_KERNEL);
> +     if (!pstate)
> +             return NULL;

[Severity: Medium]
Should dcp_plane_atomic_create_state() return ERR_PTR(-ENOMEM) instead of
NULL on allocation failure?  Its caller drm_mode_config_plane_create_state()
checks IS_ERR(plane_state), so a NULL return is treated as success and
leaves plane->state NULL.  Also, dcp_plane_atomic_duplicate_state() above
copies the other dcp_plane_state fields from old_pstate but omits
old_pstate->hfi_format.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=11

Reply via email to