Skip to contentSkip to conversation

clk: fractional-divider: Move mask calculations out of lock

PATCH Applied Andy Shevchenko 5 participants 7 messages Mar 3, 2024 – Mar 9, 2024 Common clk

lore
Share & tools
lore
#0 lore raw

PATCH 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.

---
 drivers/clk/clk-fractional-divider.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/clk/clk-fractional-divider.c +3 -3
index a0178182fc72..da057172cc90 100644
@@ -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);
Signature · 1 line
2.44.0
Reviewed-by on v1 1/1
#2 lore raw
In reply to Andy Shevchenko

Am Sonntag, 3. März 2024, 13:14:10 CET schrieb Andy Shevchenko:

Quoted 4 lines

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

Quoted 27 lines
---
 drivers/clk/clk-fractional-divider.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/clk/clk-fractional-divider.c +3 -3
index a0178182fc72..da057172cc90 100644
@@ -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);
maintainer Applied
#3 lore raw
In reply to Andy Shevchenko

Quoting Andy Shevchenko (2024-03-03 04:14:10)

Quoted 5 lines

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

---

Applied to clk-next

Quoted 12 lines
drivers/clk/clk-fractional-divider.c +3 -0
index a0178182fc72..da057172cc90 100644
@@ -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)

drivers/clk/clk-fractional-divider.c static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate, · line 208

Quoted 10 lines
drivers/clk/clk-fractional-divider.c +0 -3
                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!?

#4 lore raw
Quoted 1 line · Stephen Boyd

Quoting Andy Shevchenko (2024-03-03 04:14:10)

...

Quoted 1 line

Applied to clk-next

Thank you!

...

Quoted 1 line

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

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

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
In reply to Andy Shevchenko

Le 03/03/2024 à 13:14, Andy Shevchenko a écrit :

Quoted 13 lines

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

---
  drivers/clk/clk-fractional-divider.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
drivers/clk/clk-fractional-divider.c +0 -0
index a0178182fc72..da057172cc90 100644

drivers/clk/clk-fractional-divider.c static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate, · line 200

Quoted 6 lines
drivers/clk/clk-fractional-divider.c +3 -0
@@ -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.

drivers/clk/clk-fractional-divider.c static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate, · line 207

Quoted 10 lines
drivers/clk/clk-fractional-divider.c +0 -3
  	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;

drivers/clk/clk-fractional-divider.c static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate, · line 208

Quoted 1 line
drivers/clk/clk-fractional-divider.c +0 -0
  	val |= (m << fd->mshift) | (n << fd->nshift);

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

CJ

#6 lore raw
Quoted 1 line · Christophe JAILLET

Le 03/03/2024 à 13:14, Andy Shevchenko a écrit :

...

Quoted 8 lines

drivers/clk/clk-fractional-divider.c static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate, · line 199

Quoted 6 lines
drivers/clk/clk-fractional-divider.c +2 -0
@@ -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.

Quoted 16 lines
    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.

drivers/clk/clk-fractional-divider.c static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate, · line 207

Quoted 10 lines
drivers/clk/clk-fractional-divider.c +0 -3
      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);
Quoted 6 lines
    val &= mask;

drivers/clk/clk-fractional-divider.c static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate, · line 208

Quoted 1 line
drivers/clk/clk-fractional-divider.c +0 -0
      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.

Signature · 2 lines
With Best Regards,
Andy Shevchenko