Skip to contentSkip to conversation

clk: Don't cache errors from clk_ops::get_phase()

PATCH Superseded Stephen Boyd 3 participants 7 messages Oct 1, 2019 – Jan 7, 2020 Common clk

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Stephen Boyd

author maintainer
#0 lore raw

PATCH 1/1 clk: Don't cache errors from clk_ops::get_phase()

We don't check for errors from clk_ops::get_phase() before storing away the result into the clk_core::phase member. This can lead to some fairly confusing debugfs information if these ops do return an error. Let's skip the store when this op fails to fix this. While we're here, move the locking outside of clk_core_get_phase() to simplify callers from the debugfs side.

Cc Douglas Anderson <[email protected]>
Cc Heiko Stuebner <[email protected]>
Cc Jerome Brunet <[email protected]>

---

Resending because I couldn't find this anywhere.

 drivers/clk/clk.c | 44 +++++++++++++++++++++++++++++---------------
 1 file changed, 29 insertions(+), 15 deletions(-)
drivers/clk/clk.c +29 -15
index 1c677d7f7f53..16add5626dfa 100644
@@ -2640,14 +2640,14 @@ EXPORT_SYMBOL_GPL(clk_set_phase);
 
 static int clk_core_get_phase(struct clk_core *core)
 {
-	int ret;
+	int ret = 0;
 
-	clk_prepare_lock();
+	lockdep_assert_held(&prepare_lock);
 	/* Always try to update cached phase if possible */
 	if (core->ops->get_phase)
-		core->phase = core->ops->get_phase(core->hw);
-	ret = core->phase;
-	clk_prepare_unlock();
+		ret = core->ops->get_phase(core->hw);
+	if (ret >= 0)
+		core->phase = ret;
 
 	return ret;
 }
@@ -2661,10 +2661,16 @@ static int clk_core_get_phase(struct clk_core *core)
  */
 int clk_get_phase(struct clk *clk)
 {
+	int ret;
+
 	if (!clk)
 		return 0;
 
-	return clk_core_get_phase(clk->core);
+	clk_prepare_unlock();
+	ret = clk_core_get_phase(clk->core);
+	clk_prepare_unlock();
+
+	return ret;
 }
 EXPORT_SYMBOL_GPL(clk_get_phase);
 
@@ -2878,13 +2884,21 @@ static struct hlist_head *orphan_list[] = {
 static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
 				 int level)
 {
-	seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %5d %6d\n",
+	int phase;
+
+	seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu ",
 		   level * 3 + 1, "",
 		   30 - level * 3, c->name,
 		   c->enable_count, c->prepare_count, c->protect_count,
-		   clk_core_get_rate(c), clk_core_get_accuracy(c),
-		   clk_core_get_phase(c),
-		   clk_core_get_scaled_duty_cycle(c, 100000));
+		   clk_core_get_rate(c), clk_core_get_accuracy(c));
+
+	phase = clk_core_get_phase(c);
+	if (phase >= 0)
+		seq_printf(s, "%5d", phase);
+	else
+		seq_puts(s, "-----");
+
+	seq_printf(s, " %6d\n", clk_core_get_scaled_duty_cycle(c, 100000));
 }
 
 static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
@@ -2921,6 +2935,7 @@ DEFINE_SHOW_ATTRIBUTE(clk_summary);
 
 static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
 {
+	int phase;
 	unsigned long min_rate, max_rate;
 
 	clk_core_get_boundaries(c, &min_rate, &max_rate);
@@ -2934,7 +2949,9 @@ static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
 	seq_printf(s, "\"min_rate\": %lu,", min_rate);
 	seq_printf(s, "\"max_rate\": %lu,", max_rate);
 	seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
-	seq_printf(s, "\"phase\": %d,", clk_core_get_phase(c));
+	phase = clk_core_get_phase(c);
+	if (phase >= 0)
+		seq_printf(s, "\"phase\": %d,", phase);
 	seq_printf(s, "\"duty_cycle\": %u",
 		   clk_core_get_scaled_duty_cycle(c, 100000));
 }
@@ -3349,10 +3366,7 @@ static int __clk_core_init(struct clk_core *core)
 	 * Since a phase is by definition relative to its parent, just
 	 * query the current clock phase, or just assume it's in phase.
 	 */
-	if (core->ops->get_phase)
-		core->phase = core->ops->get_phase(core->hw);
-	else
-		core->phase = 0;
+	clk_core_get_phase(core);
 
 	/*
 	 * Set clk's duty cycle.
Signature · 1 line
Sent by a computer through tubes
#1 lore raw

Hi,

Quoted 8 lines · Stephen Boyd

We don't check for errors from clk_ops::get_phase() before storing away the result into the clk_core::phase member. This can lead to some fairly confusing debugfs information if these ops do return an error. Let's skip the store when this op fails to fix this. While we're here, move the locking outside of clk_core_get_phase() to simplify callers from the debugfs side.

Quoted 7 lines
Cc Douglas Anderson <[email protected]>
Cc Heiko Stuebner <[email protected]>
Cc Jerome Brunet <[email protected]>

---

Resending because I couldn't find this anywhere.

It was at:

https://lore.kernel.org/r/[email protected]

Quoted 7 lines
drivers/clk/clk.c +1 -1
@@ -2640,14 +2640,14 @@ EXPORT_SYMBOL_GPL(clk_set_phase);
 
 static int clk_core_get_phase(struct clk_core *core)
 {
-       int ret;
+       int ret = 0;
 

drivers/clk/clk.c EXPORT_SYMBOL_GPL(clk_set_phase); · line 2650

Quoted 10 lines
drivers/clk/clk.c +4 -4
-       clk_prepare_lock();
+       lockdep_assert_held(&prepare_lock);
        /* Always try to update cached phase if possible */
        if (core->ops->get_phase)
-               core->phase = core->ops->get_phase(core->hw);
-       ret = core->phase;
-       clk_prepare_unlock();
+               ret = core->ops->get_phase(core->hw);
+       if (ret >= 0)
+               core->phase = ret;

It doesn't matter much, but if it were me I'd add this under the "if (core->ops->get_phase)" statement. Then we don't keep doing a memory write of 0 to "core->phase" all the time when "core->ops->get_phase" isn't there. ...plus (to me) it makes more logical sense.

I'd guess you were trying to make sure that core->phase got set to 0 like the old code did in __clk_core_init(). ...but that really shouldn't be needed since the clk_core is initted with kzalloc().

drivers/clk/clk.c static int clk_core_get_phase(struct clk_core *core) · line 2671

Quoted 13 lines
drivers/clk/clk.c +5 -1
@@ -2661,10 +2661,16 @@ static int clk_core_get_phase(struct clk_core *core)
  */
 int clk_get_phase(struct clk *clk)
 {
+       int ret;
+
        if (!clk)
                return 0;
 
-       return clk_core_get_phase(clk->core);
+       clk_prepare_unlock();
+       ret = clk_core_get_phase(clk->core);
+       clk_prepare_unlock();

Probably the first of these two should be clk_prepare_lock() unless you really really wanted the clock to be unlocked.

Quoted 7 lines
drivers/clk/clk.c +2 -1
@@ -2878,13 +2884,21 @@ static struct hlist_head *orphan_list[] = {
 static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
                                 int level)
 {
-       seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %5d %6d\n",
+       int phase;
+

drivers/clk/clk.c static struct hlist_head *orphan_list[] = { · line 2895

Quoted 10 lines
drivers/clk/clk.c +4 -3
+       seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu ",
                   level * 3 + 1, "",
                   30 - level * 3, c->name,
                   c->enable_count, c->prepare_count, c->protect_count,
-                  clk_core_get_rate(c), clk_core_get_accuracy(c),
-                  clk_core_get_phase(c),
-                  clk_core_get_scaled_duty_cycle(c, 100000));
+                  clk_core_get_rate(c), clk_core_get_accuracy(c));
+
+       phase = clk_core_get_phase(c);

Don't you need a clk_prepare_lock() / clk_prepare_unlock() around this now?

drivers/clk/clk.c static int __clk_core_init(struct clk_core *core) · line 3367

Quoted 3 lines
drivers/clk/clk.c +0 -0
@@ -3349,10 +3366,7 @@ static int __clk_core_init(struct clk_core *core)
         * Since a phase is by definition relative to its parent, just
         * query the current clock phase, or just assume it's in phase.

Maybe update the comment to something like "clk_core_get_phase() will cache the phase for us".

Quoted 6 lines
drivers/clk/clk.c +1 -4
         */
-       if (core->ops->get_phase)
-               core->phase = core->ops->get_phase(core->hw);
-       else
-               core->phase = 0;
+       clk_core_get_phase(core);
author maintainer
#2 lore raw

Quoting Doug Anderson (2019-10-01 14:20:50)

Quoted 47 lines

Hi,

Quoted 8 lines · Stephen Boyd

We don't check for errors from clk_ops::get_phase() before storing away the result into the clk_core::phase member. This can lead to some fairly confusing debugfs information if these ops do return an error. Let's skip the store when this op fails to fix this. While we're here, move the locking outside of clk_core_get_phase() to simplify callers from the debugfs side.

Quoted 7 lines
Cc Douglas Anderson <[email protected]>
Cc Heiko Stuebner <[email protected]>
Cc Jerome Brunet <[email protected]>

---

Resending because I couldn't find this anywhere.

It was at:

https://lore.kernel.org/r/[email protected]

Quoted 7 lines
drivers/clk/clk.c +1 -1
@@ -2640,14 +2640,14 @@ EXPORT_SYMBOL_GPL(clk_set_phase);
 
 static int clk_core_get_phase(struct clk_core *core)
 {
-       int ret;
+       int ret = 0;
 

drivers/clk/clk.c EXPORT_SYMBOL_GPL(clk_set_phase); · line 2650

Quoted 10 lines
drivers/clk/clk.c +4 -4
-       clk_prepare_lock();
+       lockdep_assert_held(&prepare_lock);
        /* Always try to update cached phase if possible */
        if (core->ops->get_phase)
-               core->phase = core->ops->get_phase(core->hw);
-       ret = core->phase;
-       clk_prepare_unlock();
+               ret = core->ops->get_phase(core->hw);
+       if (ret >= 0)
+               core->phase = ret;

It doesn't matter much, but if it were me I'd add this under the "if (core->ops->get_phase)" statement. Then we don't keep doing a memory write of 0 to "core->phase" all the time when "core->ops->get_phase" isn't there. ...plus (to me) it makes more logical sense.

Quoted 3 lines

I'd guess you were trying to make sure that core->phase got set to 0 like the old code did in __clk_core_init(). ...but that really shouldn't be needed since the clk_core is initted with kzalloc().

Ok. I bail out early with return 0 now.

Quoted 11 lines
Quoted 11 lines
drivers/clk/clk.c +2 -0
@@ -2661,10 +2661,16 @@ static int clk_core_get_phase(struct clk_core *core)
  */
 int clk_get_phase(struct clk *clk)
 {
+       int ret;
+
        if (!clk)
                return 0;
 
Quoted 7 lines

drivers/clk/clk.c static int clk_core_get_phase(struct clk_core *core) · line 2671

Quoted 4 lines
drivers/clk/clk.c +3 -1
-       return clk_core_get_phase(clk->core);
+       clk_prepare_unlock();
+       ret = clk_core_get_phase(clk->core);
+       clk_prepare_unlock();

Probably the first of these two should be clk_prepare_lock() unless you really really wanted the clock to be unlocked.

Thanks.

Quoted 13 lines
Quoted 13 lines
drivers/clk/clk.c +3 -1
@@ -2878,13 +2884,21 @@ static struct hlist_head *orphan_list[] = {
 static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
                                 int level)
 {
-       seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %5d %6d\n",
+       int phase;
+
+       seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu ",
                   level * 3 + 1, "",
                   30 - level * 3, c->name,
                   c->enable_count, c->prepare_count, c->protect_count,
Quoted 8 lines

drivers/clk/clk.c static struct hlist_head *orphan_list[] = { · line 2895

Quoted 6 lines
drivers/clk/clk.c +3 -3
-                  clk_core_get_rate(c), clk_core_get_accuracy(c),
-                  clk_core_get_phase(c),
-                  clk_core_get_scaled_duty_cycle(c, 100000));
+                  clk_core_get_rate(c), clk_core_get_accuracy(c));
+
+       phase = clk_core_get_phase(c);

Don't you need a clk_prepare_lock() / clk_prepare_unlock() around this now?

Not really, we already hold the lock when this function is called so locking it again is not useful.

Quoted 9 lines

drivers/clk/clk.c static int __clk_core_init(struct clk_core *core) · line 3367

Quoted 5 lines
drivers/clk/clk.c +0 -0
@@ -3349,10 +3366,7 @@ static int __clk_core_init(struct clk_core *core)
         * Since a phase is by definition relative to its parent, just
         * query the current clock phase, or just assume it's in phase.

Maybe update the comment to something like "clk_core_get_phase() will cache the phase for us".

Ok.

#3 lore raw
In reply to Stephen Boyd
Quoted 6 lines · Stephen Boyd

We don't check for errors from clk_ops::get_phase() before storing away the result into the clk_core::phase member. This can lead to some fairly confusing debugfs information if these ops do return an error. Let's skip the store when this op fails to fix this. While we're here, move the locking outside of clk_core_get_phase() to simplify callers from the debugfs side.

Function already under the lock seem to be marked with "_nolock" Maybe one should added for get_phase() ?

Also the debugfs side calls clk_core_get_rate() and clk_core_get_accuracy(). Both are taking the prepare_lock.

So I don't get why clk_get_phase() should do thing differently from the others, and not take the lock ?

Quoted 100 lines
Cc Douglas Anderson <[email protected]>
Cc Heiko Stuebner <[email protected]>
Cc Jerome Brunet <[email protected]>

---

Resending because I couldn't find this anywhere.

 drivers/clk/clk.c | 44 +++++++++++++++++++++++++++++---------------
 1 file changed, 29 insertions(+), 15 deletions(-)
drivers/clk/clk.c +28 -11
index 1c677d7f7f53..16add5626dfa 100644
@@ -2640,14 +2640,14 @@ EXPORT_SYMBOL_GPL(clk_set_phase);
 
 static int clk_core_get_phase(struct clk_core *core)
 {
-	int ret;
+	int ret = 0;
 
-	clk_prepare_lock();
+	lockdep_assert_held(&prepare_lock);
 	/* Always try to update cached phase if possible */
 	if (core->ops->get_phase)
-		core->phase = core->ops->get_phase(core->hw);
-	ret = core->phase;
-	clk_prepare_unlock();
+		ret = core->ops->get_phase(core->hw);
+	if (ret >= 0)
+		core->phase = ret;
 
 	return ret;
 }
@@ -2661,10 +2661,16 @@ static int clk_core_get_phase(struct clk_core *core)
  */
 int clk_get_phase(struct clk *clk)
 {
+	int ret;
+
 	if (!clk)
 		return 0;
 
-	return clk_core_get_phase(clk->core);
+	clk_prepare_unlock();
+	ret = clk_core_get_phase(clk->core);
+	clk_prepare_unlock();
+
+	return ret;
 }
 EXPORT_SYMBOL_GPL(clk_get_phase);
 
@@ -2878,13 +2884,21 @@ static struct hlist_head *orphan_list[] = {
 static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
 				 int level)
 {
-	seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %5d %6d\n",
+	int phase;
+
+	seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu ",
 		   level * 3 + 1, "",
 		   30 - level * 3, c->name,
 		   c->enable_count, c->prepare_count, c->protect_count,
-		   clk_core_get_rate(c), clk_core_get_accuracy(c),
-		   clk_core_get_phase(c),
-		   clk_core_get_scaled_duty_cycle(c, 100000));
+		   clk_core_get_rate(c), clk_core_get_accuracy(c));
+
+	phase = clk_core_get_phase(c);
+	if (phase >= 0)
+		seq_printf(s, "%5d", phase);
+	else
+		seq_puts(s, "-----");
+
+	seq_printf(s, " %6d\n", clk_core_get_scaled_duty_cycle(c, 100000));
 }
 
 static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
@@ -2921,6 +2935,7 @@ DEFINE_SHOW_ATTRIBUTE(clk_summary);
 
 static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
 {
+	int phase;
 	unsigned long min_rate, max_rate;
 
 	clk_core_get_boundaries(c, &min_rate, &max_rate);
@@ -2934,7 +2949,9 @@ static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
 	seq_printf(s, "\"min_rate\": %lu,", min_rate);
 	seq_printf(s, "\"max_rate\": %lu,", max_rate);
 	seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
-	seq_printf(s, "\"phase\": %d,", clk_core_get_phase(c));
+	phase = clk_core_get_phase(c);
+	if (phase >= 0)
+		seq_printf(s, "\"phase\": %d,", phase);
 	seq_printf(s, "\"duty_cycle\": %u",
 		   clk_core_get_scaled_duty_cycle(c, 100000));
 }

drivers/clk/clk.c static int __clk_core_init(struct clk_core *core) · line 3369

Quoted 8 lines
drivers/clk/clk.c +1 -4
@@ -3349,10 +3366,7 @@ static int __clk_core_init(struct clk_core *core)
 	 * Since a phase is by definition relative to its parent, just
 	 * query the current clock phase, or just assume it's in phase.
 	 */
-	if (core->ops->get_phase)
-		core->phase = core->ops->get_phase(core->hw);
-	else
-		core->phase = 0;
+	clk_core_get_phase(core);

Should the error be checked here as well ?

Quoted 3 lines
drivers/clk/clk.c +0 -0
 	/*
 	 * Set clk's duty cycle.
author maintainer
#4 lore raw

(Sorry I'm way behind on emails)

Quoting Jerome Brunet (2019-10-02 01:31:46)

Quoted 10 lines · Stephen Boyd
Quoted 7 lines

We don't check for errors from clk_ops::get_phase() before storing away the result into the clk_core::phase member. This can lead to some fairly confusing debugfs information if these ops do return an error. Let's skip the store when this op fails to fix this. While we're here, move the locking outside of clk_core_get_phase() to simplify callers from the debugfs side.

Quoted 5 lines

Function already under the lock seem to be marked with "_nolock" Maybe one should added for get_phase() ?

Also the debugfs side calls clk_core_get_rate() and clk_core_get_accuracy(). Both are taking the prepare_lock.

Yes both are taking the lock again when we're already holding the lock. It is wasteful. I'll send another patch with the series to make those calls in debugfs use the nolock variants. That will open up the question of how we sometimes recalc rates and other times don't depending on if the nolock or lock variant of the get_rate() API is used.

Quoted 3 lines

So I don't get why clk_get_phase() should do thing differently from the others, and not take the lock ?

Got it.

Quoted 17 lines
Quoted 7 lines
drivers/clk/clk.c +0 -0
index 1c677d7f7f53..16add5626dfa 100644

drivers/clk/clk.c static int __clk_core_init(struct clk_core *core) · line 3369

Quoted 8 lines
drivers/clk/clk.c +1 -4
@@ -3349,10 +3366,7 @@ static int __clk_core_init(struct clk_core *core)
       * Since a phase is by definition relative to its parent, just
       * query the current clock phase, or just assume it's in phase.
       */
-     if (core->ops->get_phase)
-             core->phase = core->ops->get_phase(core->hw);
-     else
-             core->phase = 0;
+     clk_core_get_phase(core);

Should the error be checked here as well ?

What error?

author maintainer
#5 lore raw

Quoting Stephen Boyd (2020-01-04 23:50:49)

Quoted 9 lines

Quoting Jerome Brunet (2019-10-02 01:31:46)

Quoted 4 lines
Quoted 2 lines

+ clk_core_get_phase(core);

Should the error be checked here as well ?

What error?

Ah the error when clk_ops::get_phase() returns an error? I guess we should just silently ignore it to maintain the previous behavior? Or we can bail out of clk registration. Seems low risk to do that in another patch.

#6 lore raw
Quoted 6 lines · Stephen Boyd

Quoting Stephen Boyd (2020-01-04 23:50:49)

Quoted 5 lines

Quoting Jerome Brunet (2019-10-02 01:31:46)

Quoted 3 lines
Quoted 2 lines

+ clk_core_get_phase(core);

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Should the error be checked here as well ?

What error?

Ah the error when clk_ops::get_phase() returns an error? I guess we should just silently ignore it to maintain the previous behavior?

Indeed, that's the previous behavior so we can keep it. I'm just not a fan of silently ignoring errors. These choices tend to come back to haunt us ...

Quoted 3 lines

Or we
can bail out of clk registration. Seems low risk to do that in another patch.

Why not, or at least a warning so we get a hint that something is wrong.