[4/4] clk: Warn if we register a mux without determine_rate

Message ID 20221018-clk-range-checks-fixes-v1-4-f3ef80518140@cerno.tech
State New
Headers
Series clk: Rate range improvements |

Commit Message

Maxime Ripard Oct. 18, 2022, 1:52 p.m. UTC
  The determine_rate hook allows to select the proper parent and its rate
for a given clock configuration. On another hand, set_parent is there to
change the parent of a mux.

Some clocks provide a set_parent hook but don't implement
determine_rate. In such a case, set_parent is pretty much useless since
the clock framework will always assume the current parent is to be used,
and we will thus never change it.

This situation can be solved in two ways:
  - either we don't need to change the parent, and we thus shouldn't
    implement set_parent;
  - or we don't want to change the parent, in this case we should set
    CLK_SET_RATE_NO_REPARENT;
  - or we're missing a determine_rate implementation.

The latter is probably just an oversight from the driver's author, and
we should thus raise their awareness about the fact that the current
state of the driver is confusing.

It's not clear at this point how many drivers are affected though, so
let's make it a warning instead of an error for now.

Signed-off-by: Maxime Ripard <maxime@cerno.tech>
---
 drivers/clk/clk.c | 5 +++++
 1 file changed, 5 insertions(+)
  

Comments

AngeloGioacchino Del Regno Oct. 18, 2022, 2:34 p.m. UTC | #1
Il 18/10/22 15:52, Maxime Ripard ha scritto:
> The determine_rate hook allows to select the proper parent and its rate
> for a given clock configuration. On another hand, set_parent is there to
> change the parent of a mux.
> 
> Some clocks provide a set_parent hook but don't implement
> determine_rate. In such a case, set_parent is pretty much useless since
> the clock framework will always assume the current parent is to be used,
> and we will thus never change it.
> 
> This situation can be solved in two ways:
>    - either we don't need to change the parent, and we thus shouldn't
>      implement set_parent;
>    - or we don't want to change the parent, in this case we should set
>      CLK_SET_RATE_NO_REPARENT;
>    - or we're missing a determine_rate implementation.
> 
> The latter is probably just an oversight from the driver's author, and
> we should thus raise their awareness about the fact that the current
> state of the driver is confusing.
> 
> It's not clear at this point how many drivers are affected though, so
> let's make it a warning instead of an error for now.
> 
> Signed-off-by: Maxime Ripard <maxime@cerno.tech>

That solves my concerns :-) :-)

Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
  
Stephen Boyd Oct. 26, 2022, 2:07 a.m. UTC | #2
Quoting Maxime Ripard (2022-10-18 06:52:59)
> The determine_rate hook allows to select the proper parent and its rate
> for a given clock configuration. On another hand, set_parent is there to
> change the parent of a mux.
> 
> Some clocks provide a set_parent hook but don't implement
> determine_rate. In such a case, set_parent is pretty much useless since
> the clock framework will always assume the current parent is to be used,
> and we will thus never change it.
> 
> This situation can be solved in two ways:
>   - either we don't need to change the parent, and we thus shouldn't
>     implement set_parent;
>   - or we don't want to change the parent, in this case we should set
>     CLK_SET_RATE_NO_REPARENT;
>   - or we're missing a determine_rate implementation.
> 
> The latter is probably just an oversight from the driver's author, and
> we should thus raise their awareness about the fact that the current
> state of the driver is confusing.

There is another case which is a leaf clk that is a mux where you only
expect clk_set_parent() to be used, and not clk_set_rate(). This use
case is odd though, so I'm not sure how much we care.

> 
> It's not clear at this point how many drivers are affected though, so
> let's make it a warning instead of an error for now.
> 
> Signed-off-by: Maxime Ripard <maxime@cerno.tech>
> ---
>  drivers/clk/clk.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
> index 57b83665e5c3..11c41d987ff4 100644
> --- a/drivers/clk/clk.c
> +++ b/drivers/clk/clk.c
> @@ -3700,6 +3700,11 @@ static int __clk_core_init(struct clk_core *core)
>                 goto out;
>         }
>  
> +       /* TODO: Promote to an error */

The documentation should be updated in this patch (see the table of
hardware characteristics in Documentation/driver-api/clk.rst).

> +       if (core->ops->set_parent && !core->ops->determine_rate)
> +               pr_warn("%s: %s must implement .set_parent & .determine_rate\n",

You can grep for it:

 $ git grep -W 'struct clk_ops .*='

but I'm fairly certain a coccinelle script can detect most of these
because clk_ops are usually statically defined (although not always).

Either way I already see that 'owl_comp_div_ops' will trigger this
warning. And 'at91sam9x5_smd_ops' looks even more likely. Given that I'm
not super keen on applying this patch.
  
Maxime Ripard Oct. 26, 2022, 1:52 p.m. UTC | #3
Hi,

On Tue, Oct 25, 2022 at 07:07:58PM -0700, Stephen Boyd wrote:
> Quoting Maxime Ripard (2022-10-18 06:52:59)
> > The determine_rate hook allows to select the proper parent and its rate
> > for a given clock configuration. On another hand, set_parent is there to
> > change the parent of a mux.
> > 
> > Some clocks provide a set_parent hook but don't implement
> > determine_rate. In such a case, set_parent is pretty much useless since
> > the clock framework will always assume the current parent is to be used,
> > and we will thus never change it.
> > 
> > This situation can be solved in two ways:
> >   - either we don't need to change the parent, and we thus shouldn't
> >     implement set_parent;
> >   - or we don't want to change the parent, in this case we should set
> >     CLK_SET_RATE_NO_REPARENT;
> >   - or we're missing a determine_rate implementation.
> > 
> > The latter is probably just an oversight from the driver's author, and
> > we should thus raise their awareness about the fact that the current
> > state of the driver is confusing.
> 
> There is another case which is a leaf clk that is a mux where you only
> expect clk_set_parent() to be used, and not clk_set_rate(). This use
> case is odd though, so I'm not sure how much we care.
> 
> > 
> > It's not clear at this point how many drivers are affected though, so
> > let's make it a warning instead of an error for now.
> > 
> > Signed-off-by: Maxime Ripard <maxime@cerno.tech>
> > ---
> >  drivers/clk/clk.c | 5 +++++
> >  1 file changed, 5 insertions(+)
> > 
> > diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
> > index 57b83665e5c3..11c41d987ff4 100644
> > --- a/drivers/clk/clk.c
> > +++ b/drivers/clk/clk.c
> > @@ -3700,6 +3700,11 @@ static int __clk_core_init(struct clk_core *core)
> >                 goto out;
> >         }
> >  
> > +       /* TODO: Promote to an error */
> 
> The documentation should be updated in this patch (see the table of
> hardware characteristics in Documentation/driver-api/clk.rst).
> 
> > +       if (core->ops->set_parent && !core->ops->determine_rate)
> > +               pr_warn("%s: %s must implement .set_parent & .determine_rate\n",
> 
> You can grep for it:
> 
>  $ git grep -W 'struct clk_ops .*='

TIL about -W. It's awesome, thanks

> but I'm fairly certain a coccinelle script can detect most of these
> because clk_ops are usually statically defined (although not always).
> 
> Either way I already see that 'owl_comp_div_ops' will trigger this
> warning. And 'at91sam9x5_smd_ops' looks even more likely. Given that I'm
> not super keen on applying this patch.

It's the reason why I didn't return an error at first, I wanted to
report that it's invalid and let to drivers the chance to be fixed
still.

Should I take your above comment as you'd rather have the affected
drivers fixed in this patch and we then return an error, or is it that
you don't want that patch at all?

Maxime
  
Stephen Boyd Oct. 27, 2022, 9:45 p.m. UTC | #4
Quoting maxime@cerno.tech (2022-10-26 06:52:15)
> On Tue, Oct 25, 2022 at 07:07:58PM -0700, Stephen Boyd wrote:
> > 
> > You can grep for it:
> > 
> >  $ git grep -W 'struct clk_ops .*='
> 
> TIL about -W. It's awesome, thanks

:)

> 
> > but I'm fairly certain a coccinelle script can detect most of these
> > because clk_ops are usually statically defined (although not always).
> > 
> > Either way I already see that 'owl_comp_div_ops' will trigger this
> > warning. And 'at91sam9x5_smd_ops' looks even more likely. Given that I'm
> > not super keen on applying this patch.
> 
> It's the reason why I didn't return an error at first, I wanted to
> report that it's invalid and let to drivers the chance to be fixed
> still.
> 
> Should I take your above comment as you'd rather have the affected
> drivers fixed in this patch and we then return an error, or is it that
> you don't want that patch at all?

You can try fixing all the drivers that are failing to meet this
requirement (found with grep) and if they are fixed we can start
printing the warning. That seems to be the practical approach to getting
this patch accepted. The TODO irks me to be honest. I'd rather we fix
everything and make it an error and be done with it.
  
Maxime Ripard Nov. 3, 2022, 12:33 p.m. UTC | #5
Going back to this mail.

On Tue, Oct 25, 2022 at 07:07:58PM -0700, Stephen Boyd wrote:
> Quoting Maxime Ripard (2022-10-18 06:52:59)
> > The determine_rate hook allows to select the proper parent and its rate
> > for a given clock configuration. On another hand, set_parent is there to
> > change the parent of a mux.
> > 
> > Some clocks provide a set_parent hook but don't implement
> > determine_rate. In such a case, set_parent is pretty much useless since
> > the clock framework will always assume the current parent is to be used,
> > and we will thus never change it.
> > 
> > This situation can be solved in two ways:
> >   - either we don't need to change the parent, and we thus shouldn't
> >     implement set_parent;
> >   - or we don't want to change the parent, in this case we should set
> >     CLK_SET_RATE_NO_REPARENT;
> >   - or we're missing a determine_rate implementation.
> > 
> > The latter is probably just an oversight from the driver's author, and
> > we should thus raise their awareness about the fact that the current
> > state of the driver is confusing.
> 
> There is another case which is a leaf clk that is a mux where you only
> expect clk_set_parent() to be used, and not clk_set_rate(). This use
> case is odd though, so I'm not sure how much we care.

It looks like there's a good number of clocks that do indeed only
provide get_parent / set_parent. It's hard to tell if it's an oversight
or a choice.

I think we can make that decision explicit by providing a determine_rate
helper that always returns the current parent and its rate. It shouldn't
change anything from a CCF behavior point of view, and it makes it clear
what the behavior is. And if someone wants something else, then they can
change it to whatever they want.

Maxime
  
Maxime Ripard Nov. 3, 2022, 12:39 p.m. UTC | #6
On Thu, Oct 27, 2022 at 02:45:07PM -0700, Stephen Boyd wrote:
> Quoting maxime@cerno.tech (2022-10-26 06:52:15)
> > On Tue, Oct 25, 2022 at 07:07:58PM -0700, Stephen Boyd wrote:
> > > but I'm fairly certain a coccinelle script can detect most of these
> > > because clk_ops are usually statically defined (although not always).
> > > 
> > > Either way I already see that 'owl_comp_div_ops' will trigger this
> > > warning. And 'at91sam9x5_smd_ops' looks even more likely. Given that I'm
> > > not super keen on applying this patch.
> > 
> > It's the reason why I didn't return an error at first, I wanted to
> > report that it's invalid and let to drivers the chance to be fixed
> > still.
> > 
> > Should I take your above comment as you'd rather have the affected
> > drivers fixed in this patch and we then return an error, or is it that
> > you don't want that patch at all?
> 
> You can try fixing all the drivers that are failing to meet this
> requirement (found with grep) and if they are fixed we can start
> printing the warning. That seems to be the practical approach to
> getting this patch accepted. The TODO irks me to be honest. I'd rather
> we fix everything and make it an error and be done with it.

ACK. I had a look this morning and there's indeed a good number of
clocks in that case. I'll work on it.

Maxime
  
Stephen Boyd Nov. 4, 2022, 6:10 p.m. UTC | #7
Quoting Maxime Ripard (2022-11-03 05:33:28)
> On Tue, Oct 25, 2022 at 07:07:58PM -0700, Stephen Boyd wrote:
> > There is another case which is a leaf clk that is a mux where you only
> > expect clk_set_parent() to be used, and not clk_set_rate(). This use
> > case is odd though, so I'm not sure how much we care.
> 
> It looks like there's a good number of clocks that do indeed only
> provide get_parent / set_parent. It's hard to tell if it's an oversight
> or a choice.
> 
> I think we can make that decision explicit by providing a determine_rate
> helper that always returns the current parent and its rate. It shouldn't
> change anything from a CCF behavior point of view, and it makes it clear
> what the behavior is. And if someone wants something else, then they can
> change it to whatever they want.

Ok sounds like a plan.
  

Patch

diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index 57b83665e5c3..11c41d987ff4 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -3700,6 +3700,11 @@  static int __clk_core_init(struct clk_core *core)
 		goto out;
 	}
 
+	/* TODO: Promote to an error */
+	if (core->ops->set_parent && !core->ops->determine_rate)
+		pr_warn("%s: %s must implement .set_parent & .determine_rate\n",
+			__func__, core->name);
+
 	if (core->num_parents > 1 && !core->ops->get_parent) {
 		pr_err("%s: %s must implement .get_parent as it has multi parents\n",
 		       __func__, core->name);