Re: [PATCH] drm/mediatek: Add spinlock for setting vblank event in atomic_begin

From: Fei Shao
Date: Mon Sep 18 2023 - 06:49:34 EST


Hi Angelo,

On Wed, Sep 13, 2023 at 4:35 PM AngeloGioacchino Del Regno
<angelogioacchino.delregno@xxxxxxxxxxxxx> wrote:
>
> Il 22/08/23 15:26, Jason-JH.Lin ha scritto:
> > Add spinlock protection to avoid race condition on vblank event
> > between mtk_drm_crtc_atomic_begin() and mtk_drm_finish_page_flip().
> >
>
> Hello Jason,
>
> Can you please provide more information about this race condition?
> (check below)
>
> > Fixes: 119f5173628a ("drm/mediatek: Add DRM Driver for Mediatek SoC MT8173.")
> > Signed-off-by: Jason-JH.Lin <jason-jh.lin@xxxxxxxxxxxx>
> > ---
> > drivers/gpu/drm/mediatek/mtk_drm_crtc.c | 5 +++++
> > 1 file changed, 5 insertions(+)
> >
> > diff --git a/drivers/gpu/drm/mediatek/mtk_drm_crtc.c b/drivers/gpu/drm/mediatek/mtk_drm_crtc.c
> > index d40142842f85..128a672fe3c9 100644
> > --- a/drivers/gpu/drm/mediatek/mtk_drm_crtc.c
> > +++ b/drivers/gpu/drm/mediatek/mtk_drm_crtc.c
> > @@ -746,6 +746,9 @@ static void mtk_drm_crtc_atomic_begin(struct drm_crtc *crtc,
> > crtc);
> > struct mtk_crtc_state *mtk_crtc_state = to_mtk_crtc_state(crtc_state);
> > struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
> > + unsigned long flags;
> > +
> > + spin_lock_irqsave(&crtc->dev->event_lock, flags);
> >
> > if (mtk_crtc->event && mtk_crtc_state->base.event)
> > DRM_ERROR("new event while there is still a pending event\n");
> > @@ -756,6 +759,8 @@ static void mtk_drm_crtc_atomic_begin(struct drm_crtc *crtc,
>
> ...because my suspect is that what creates the race condition in this function is
> the unlocked *assignment* to mtk_crtc->event, not the rest.
>
> If I'm right, you don't need to unconditionally spinlock at the beginning of this
> function hence ever-so-slightly improving performance compared to this version.
>
> Can you please try this one and check if this *also* solves the issue?
>
> if (mtk_crtc_state->base.event) {
> mtk_crtc_state->base.event->pipe = drm_crtc_index(crtc);
> WARN_ON(drm_crtc_vblank_get(crtc) != 0);
>
> spin_lock_irqsave(&crtc->dev->event_lock, flags);
> mtk_crtc->event = mtk_crtc_state->base.event;
> spin_lock_irqrestore(&crtc->dev->event_lock, flags);
>
> mtk_crtc_state->base.event = NULL;
> }
>
> P.S.: I'd try that myself, but I can't seem to reproduce the issue.

I'm still able to reproduce it so I gave it a try, and this approach
also seems to fix the issue. :)
FWIW, the way I reproduce that is to toggle the night light mode on
and off repeatedly through the UI panel while playing YouTube videos
on my device.

Jason, can you post a new version with Angelo's suggestion?

Regards,
Fei

>
> Regards,
> Angelo
>