RE: [PATCH] scsi: ufs: core: Ensure clk_gating.lock is used only after initialization

From: Avri Altman
Date: Wed Jan 22 2025 - 06:04:27 EST


> > To fix this issue, we use the existing `is_initialized` flag in the
> > `clk_gating` structure to ensure that the spinlock is only used after
> > it has been properly initialized. We check this flag before using the
> > spinlock in the `ufshcd_setup_clocks` function.
> >
> > It was incorrect in the first place to call `setup_clocks()` before
> > `ufshcd_init_clk_gating()`, and the introduction of the new lock
> > unmasked this bug.
>
> If calling setup_clocks() before ufshcd_init_clk_gating() is incorrect, why are
> you not reordering it?
>
> Checking for 'clk_gating.is_initialized' looks like a hack.
Actually 'clk_gating.is_initialized' seems like the standard way to do this - see e.g. in hold and release.
As for moving setup_clocks() around, I have some concerns about moving it out of ufshcd_hba_init.
Having considered the alternatives, it seems that using 'clk_gating.is_initialized' ,
despite its limitations, is the most practical solution we have.

I am open though for other suggestions.

Thanks,
Avri

>
> - Mani
>
> >
> > Fixes: 209f4e43b806 ("scsi: ufs: core: Introduce a new clock_gating
> > lock")
> > Reported-by: Dmitry Baryshkov <dmitry.baryshkov@xxxxxxxxxx>
> > Signed-off-by: Avri Altman <avri.altman@xxxxxxx>
> > ---
> > drivers/ufs/core/ufshcd.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
> > index f6c38cf10382..a778fc51ca2a 100644
> > --- a/drivers/ufs/core/ufshcd.c
> > +++ b/drivers/ufs/core/ufshcd.c
> > @@ -9142,7 +9142,7 @@ static int ufshcd_setup_clocks(struct ufs_hba
> *hba, bool on)
> > if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
> > clk_disable_unprepare(clki->clk);
> > }
> > - } else if (!ret && on) {
> > + } else if (!ret && on && hba->clk_gating.is_initialized) {
> > scoped_guard(spinlock_irqsave, &hba->clk_gating.lock)
> > hba->clk_gating.state = CLKS_ON;
> > trace_ufshcd_clk_gating(dev_name(hba->dev),
> > --
> > 2.25.1
> >
>
> --
> மணிவண்ணன் சதாசிவம்