2024-03-03 12:14:32

by Andy Shevchenko

[permalink] [raw]
Subject: [PATCH v1 1/1] clk: fractional-divider: Move mask calculations out of lock

There is no need to calculate masks under the lock taken.
Move them out of it.

Signed-off-by: Andy Shevchenko <[email protected]>
---
drivers/clk/clk-fractional-divider.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/clk/clk-fractional-divider.c b/drivers/clk/clk-fractional-divider.c
index a0178182fc72..da057172cc90 100644
--- a/drivers/clk/clk-fractional-divider.c
+++ b/drivers/clk/clk-fractional-divider.c
@@ -195,14 +195,14 @@ static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate,
n--;
}

+ mmask = GENMASK(fd->mwidth - 1, 0) << fd->mshift;
+ nmask = GENMASK(fd->nwidth - 1, 0) << fd->nshift;
+
if (fd->lock)
spin_lock_irqsave(fd->lock, flags);
else
__acquire(fd->lock);

- mmask = GENMASK(fd->mwidth - 1, 0) << fd->mshift;
- nmask = GENMASK(fd->nwidth - 1, 0) << fd->nshift;
-
val = clk_fd_readl(fd);
val &= ~(mmask | nmask);
val |= (m << fd->mshift) | (n << fd->nshift);
--
2.44.0



2024-03-09 01:06:41

by Stephen Boyd

[permalink] [raw]
Subject: Re: [PATCH v1 1/1] clk: fractional-divider: Move mask calculations out of lock

Quoting Andy Shevchenko (2024-03-03 04:14:10)
> There is no need to calculate masks under the lock taken.
> Move them out of it.
>
> Signed-off-by: Andy Shevchenko <[email protected]>
> ---

Applied to clk-next

> diff --git a/drivers/clk/clk-fractional-divider.c b/drivers/clk/clk-fractional-divider.c
> index a0178182fc72..da057172cc90 100644
> --- a/drivers/clk/clk-fractional-divider.c
> +++ b/drivers/clk/clk-fractional-divider.c
> @@ -195,14 +195,14 @@ static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate,
> n--;
> }
>
> + mmask = GENMASK(fd->mwidth - 1, 0) << fd->mshift;
> + nmask = GENMASK(fd->nwidth - 1, 0) << fd->nshift;
> +
> if (fd->lock)
> spin_lock_irqsave(fd->lock, flags);
> else
> __acquire(fd->lock);
>
> - mmask = GENMASK(fd->mwidth - 1, 0) << fd->mshift;
> - nmask = GENMASK(fd->nwidth - 1, 0) << fd->nshift;
> -
> val = clk_fd_readl(fd);
> val &= ~(mmask | nmask);
> val |= (m << fd->mshift) | (n << fd->nshift);

Should we pre-calculate the mask and shift values too!?

2024-03-09 07:21:19

by Christophe JAILLET

[permalink] [raw]
Subject: Re: [PATCH v1 1/1] clk: fractional-divider: Move mask calculations out of lock

Le 03/03/2024 à 13:14, Andy Shevchenko a écrit :
> There is no need to calculate masks under the lock taken.
> Move them out of it.
>
> Signed-off-by: Andy Shevchenko <[email protected]>
> ---
> drivers/clk/clk-fractional-divider.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/clk/clk-fractional-divider.c b/drivers/clk/clk-fractional-divider.c
> index a0178182fc72..da057172cc90 100644
> --- a/drivers/clk/clk-fractional-divider.c
> +++ b/drivers/clk/clk-fractional-divider.c
> @@ -195,14 +195,14 @@ static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate,
> n--;
> }
>
> + mmask = GENMASK(fd->mwidth - 1, 0) << fd->mshift;
> + nmask = GENMASK(fd->nwidth - 1, 0) << fd->nshift;
> +

Hi,

if this is a hot path, you could maybe even compute:
mask = ~(GENMASK(fd->mwidth - 1, 0) << fd->mshift |
GENMASK(fd->nwidth - 1, 0) << fd->nshift)

unless gcc is smart enough to do it by itself.

> if (fd->lock)
> spin_lock_irqsave(fd->lock, flags);
> else
> __acquire(fd->lock);
>
> - mmask = GENMASK(fd->mwidth - 1, 0) << fd->mshift;
> - nmask = GENMASK(fd->nwidth - 1, 0) << fd->nshift;
> -
> val = clk_fd_readl(fd);
> val &= ~(mmask | nmask);

val &= mask;

> val |= (m << fd->mshift) | (n << fd->nshift);

and pre-compute "(m << fd->mshift) | (n << fd->nshift)" outside of the
lock too.

CJ

2024-03-09 16:28:41

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v1 1/1] clk: fractional-divider: Move mask calculations out of lock

On Sat, Mar 9, 2024 at 9:19 AM Christophe JAILLET
<[email protected]> wrote:
> Le 03/03/2024 à 13:14, Andy Shevchenko a écrit :

..

> > @@ -195,14 +195,14 @@ static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate,
> > n--;
> > }
> >
> > + mmask = GENMASK(fd->mwidth - 1, 0) << fd->mshift;
> > + nmask = GENMASK(fd->nwidth - 1, 0) << fd->nshift;

> if this is a hot path, you could maybe even compute:

It's not. set_rate() may be called only on disabled (and unprepared?)
clocks, which makes it already a too slow operation.

> mask = ~(GENMASK(fd->mwidth - 1, 0) << fd->mshift |
> GENMASK(fd->nwidth - 1, 0) << fd->nshift)
>
> unless gcc is smart enough to do it by itself.
>
> > if (fd->lock)
> > spin_lock_irqsave(fd->lock, flags);
> > else
> > __acquire(fd->lock);
> >
> > - mmask = GENMASK(fd->mwidth - 1, 0) << fd->mshift;
> > - nmask = GENMASK(fd->nwidth - 1, 0) << fd->nshift;
> > -
> > val = clk_fd_readl(fd);
> > val &= ~(mmask | nmask);
>
> val &= mask;
>
> > val |= (m << fd->mshift) | (n << fd->nshift);
>
> and pre-compute "(m << fd->mshift) | (n << fd->nshift)" outside of the
> lock too.

All of these sound to me as premature optimisations. I only wanted to
get back to the status quo.

--
With Best Regards,
Andy Shevchenko

2024-03-09 16:30:16

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v1 1/1] clk: fractional-divider: Move mask calculations out of lock

On Sat, Mar 9, 2024 at 3:06 AM Stephen Boyd <[email protected]> wrote:
> Quoting Andy Shevchenko (2024-03-03 04:14:10)

..

> Applied to clk-next

Thank you!

..

> Should we pre-calculate the mask and shift values too!?

Not that it's required, but we _can_ do that, of course.

--
With Best Regards,
Andy Shevchenko