Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] drm/spacemit: incomplete teardown in spacemit_drm_master_unbind() - [High] drm/spacemit: UAF of devm-allocated priv when drm_device outlives remove - [High] drm/spacemit: vblank init ordering in spacemit_drm_master_bind() and race - [High] drm/spacemit: premature event arming in spacemit_crtc_atomic_flush() and ISR --
commit e124be165ca1d42bc693534742294e9b826976ad Author: Cody Kang <[email protected]> drm/spacemit: add Saturn DPU KMS pipeline This commit adds the atomic KMS CRTC and primary plane implementation, component master bind/unbind, and runtime PM callbacks for the SpacemiT Saturn DPU. > diff --git a/drivers/gpu/drm/spacemit/spacemit_crtc.c > b/drivers/gpu/drm/spacemit/spacemit_crtc.c > new file mode 100644 > index 0000000000000..62b33f959f467 > --- /dev/null > +++ b/drivers/gpu/drm/spacemit/spacemit_crtc.c [ ... ] > +static void spacemit_crtc_atomic_flush(struct drm_crtc *crtc, > + struct drm_atomic_commit *state) > +{ > + struct drm_crtc_state *crtc_state = > + drm_atomic_get_new_crtc_state(state, crtc); > + struct spacemit_crtc *a_crtc = to_spacemit_crtc(crtc); > + struct spacemit_drm_private *priv = spacemit_drm_priv(crtc->dev); > + int idx; > + > + /* > + * The event releases the next commit, which rewrites the other > + * command list and points the engine at it. Hold the event until > + * CFG_RDY says the engine has taken this commit's lists; the > + * interrupt then arms it for the following vblank. > + */ > + spin_lock_irq(&crtc->dev->event_lock); > + if (crtc_state->event) { > + if (drm_crtc_vblank_get(crtc) == 0) > + a_crtc->event = crtc_state->event; > + else > + drm_crtc_send_vblank_event(crtc, crtc_state->event); > + crtc_state->event = NULL; > + } > + spin_unlock_irq(&crtc->dev->event_lock); > + > + /* clocks_on is false here only after a failed power-up. */ > + if (priv->clocks_on && drm_dev_enter(crtc->dev, &idx)) { > + spacemit_dpu_run(crtc, state); > + drm_dev_exit(idx); > + } else { > + spacemit_dpu_complete_event(a_crtc); > + } > +} [Severity: High] Can spacemit_dpu_online_isr() arm and complete a_crtc->event before the DPU has taken and scanned out the new command lists? spacemit_crtc_atomic_flush() publishes a_crtc->event and drops event_lock before spacemit_dpu_run() clears stale INT_CFG_RDY status and raises both_cfg_rdy: drivers/gpu/drm/spacemit/dpu/dpu_saturn.c:spacemit_dpu_run() { ... saturn_hee_conf_dpuctrl(crtc, state); /* Ack first: one left by a commit that timed out is not this one's. */ saturn_hee_clr_int_sts(a_crtc, saturn_hee_get_irq_bit(INT_CFG_RDY)); saturn_hee_enable_irq(a_crtc, INT_CFG_RDY, true); ... saturn_hee_cfg_ready(a_crtc, hwdev); } Because spacemit_dpu_online_isr() reads unmasked status from DPU_ONLINE_IRQ_STS and handles INT_CFG_RDY before INT_VSYNC, an interrupt that fires with a leftover INT_CFG_RDY bit set (or at the frame boundary when INT_CFG_RDY and INT_VSYNC are both pending) arms a_crtc->event for vblank->count + 1 and immediately delivers it via drm_crtc_handle_vblank() in the same ISR call: drivers/gpu/drm/spacemit/dpu/dpu_saturn.c:spacemit_dpu_online_isr() { ... irq_bit = saturn_hee_get_irq_bit(INT_CFG_RDY); if (irq_raw & irq_bit) { ... if (a_crtc->event) { drm_crtc_arm_vblank_event(&a_crtc->crtc, a_crtc->event); a_crtc->event = NULL; } spin_unlock(&a_crtc->crtc.dev->event_lock); } irq_bit = saturn_hee_get_irq_bit(INT_VSYNC); if (irq_raw & irq_bit) { saturn_hee_clr_int_sts(a_crtc, irq_bit); drm_crtc_handle_vblank(&a_crtc->crtc); handled |= irq_bit; } ... } Completing the event early signals flip_done before the hardware finishes with the previous buffers, allowing the next commit to overwrite the in-use slot in the two-entry command-list ring and clean up the active framebuffer. [ ... ] > + ret = drm_mode_config_create_initial_state(drm); > + if (ret) { > + dev_err_probe(dev, ret, "failed to create the initial state\n"); > + goto err_unbind_all; > + } > + > + ret = drm_vblank_init(drm, drm->mode_config.num_crtc); > + if (ret) { > + dev_err_probe(dev, ret, "failed to initialize vblank\n"); > + goto err_unbind_all; > + } [Severity: High] Does calling drm_mode_config_create_initial_state() before drm_vblank_init() leave vblank enabled on the initially disabled CRTC and allow spacemit_crtc_enable_vblank() to race with runtime PM? drm_mode_config_create_initial_state() only calls drm_crtc_vblank_reset() when drm_dev_has_vblank(drm) is already true. Because drm_vblank_init() has not run yet, the disabled CRTC is left with vblank->inmodeset == 0 instead of 1, so DRM_IOCTL_WAIT_VBLANK can invoke spacemit_crtc_enable_vblank() before drm_crtc_vblank_on() is called. Furthermore, spacemit_crtc_enable_vblank() only checks READ_ONCE(priv->clocks_on) without synchronization against spacemit_dpu_runtime_suspend() (which only waits on synchronize_irq(priv->irq_online)): CPU0 CPU1 spacemit_crtc_enable_vblank() spacemit_dpu_runtime_suspend() if (!READ_ONCE(priv->clocks_on)) WRITE_ONCE(priv->clocks_on, false); synchronize_irq(priv->irq_online); reset_control_bulk_assert(...); clk_bulk_disable_unprepare(...); saturn_hee_clr_int_sts(...) <-- MMIO write to clock-gated DPU stalls bus During spacemit_crtc_atomic_enable(), spacemit_dpu_runtime_resume() also sets priv->clocks_on = true before spacemit_dpu_init() and drm_crtc_vblank_on(), allowing a concurrent drm_vblank_get() to unmask INT_VSYNC before the timing generator is initialized. [ ... ] > +static void spacemit_drm_master_unbind(struct device *dev) > +{ > + struct spacemit_drm_private *priv = dev_get_drvdata(dev); > + struct drm_device *drm = priv->ddev; > + > + /* > + * A process may keep the card open past this point. Unplug, so that > + * its ioctls fail and the drm_dev_enter() sections keep off the > + * registers. The commits that follow still turn the outputs off: the > + * CRTC drops its runtime PM reference, and the suspend resets the DPU. > + * The interrupt stays alive until then, for a commit that was already > + * waiting on it. > + */ > + drm_dev_unplug(drm); > + /* An open file keeps the drm_device, and its poll worker, alive. */ > + drm_kms_helper_poll_fini(drm); > + drm_atomic_helper_shutdown(drm); > + > + disable_irq(priv->irq_online); > + /* > + * A shutdown commit that failed has not stopped the watchdog of a > + * commit still waiting for CFG_RDY, and the CRTC is freed once unbind > + * returns. > + */ > + timer_shutdown_sync(&priv->a_crtc->cfg_rdy_timer); > + > + component_unbind_all(dev, drm); > + > + /* .shutdown and a later re-bind must not touch the stale instance. */ > + priv->ddev = NULL; > + priv->a_crtc = NULL; > +} [Severity: High] What completes priv->a_crtc->event and stops DPU DMA when spacemit_drm_master_unbind() runs? If a commit is waiting for CFG_RDY when drm_atomic_helper_shutdown() fails, disable_irq() and timer_shutdown_sync() stop both paths that call spacemit_dpu_complete_event(), leaking priv->a_crtc->event and its vblank reference and leaving flip_done unsignaled (along with the CRTC's runtime PM reference). Even when drm_atomic_helper_shutdown() succeeds, calling drm_dev_unplug() first makes drm_dev_enter() in spacemit_crtc_atomic_disable() and spacemit_plane_atomic_disable() return false, skipping spacemit_dpu_uninit() and spacemit_plane_off(). Hardware shutdown then relies entirely on spacemit_dpu_runtime_suspend(), so if runtime suspend does not run (for example when power/control is set to "on" or shutdown failed), the DPU remains clocked and out of reset while component unbind releases the drm_device and frees the command-list rings via dma_free_coherent(). [ ... ] > +static int spacemit_dpu_probe(struct platform_device *pdev) > +{ > + struct device *dev = &pdev->dev; > + const struct spacemit_hw_device *hwdev_template; > + struct spacemit_drm_private *priv; > + int ret; > + > + /* A display MMU entry holds a 26-bit page frame number. */ > + ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(38)); > + if (ret) > + return dev_err_probe(dev, ret, "failed to set DMA mask\n"); > + > + priv = devm_kzalloc_obj(dev, *priv); > + if (!priv) > + return -ENOMEM; > + priv->dev = dev; > + dev_set_drvdata(dev, priv); > + > + /* The match data is shared const; the MMIO mapping is per-instance. */ > + hwdev_template = device_get_match_data(dev); > + if (!hwdev_template) > + return -ENODEV; > + priv->hwdev = devm_kmemdup(dev, hwdev_template, > + sizeof(*hwdev_template), GFP_KERNEL); [Severity: High] Can sdrm->priv be dereferenced after it is freed if an open DRM file outlives spacemit_dpu_remove()? priv and priv->hwdev are allocated with devm on the platform device, so they are freed as soon as spacemit_dpu_remove() finishes, whereas sdrm->base (the drm_device) stays alive until the last open file descriptor is closed. KMS and vblank callbacks on that drm_device still dereference sdrm->priv via spacemit_drm_priv() before or without drm_dev_enter() — for example in spacemit_crtc_atomic_flush() (priv->clocks_on), spacemit_plane_atomic_check() (priv->hwdev), and spacemit_crtc_disable_vblank() from the vblank disable timer — causing a use-after-free. -- Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
