public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Sean Anderson <seanga2@gmail.com>
To: Maksim Kiselev <bigunclemax@gmail.com>, u-boot@lists.denx.de
Cc: Lukasz Majewski <lukma@denx.de>
Subject: Re: [PATCH v1] clk: mux: check value returned from clk_mux_val_to_index()
Date: Wed, 1 Nov 2023 14:41:28 -0400	[thread overview]
Message-ID: <d554be7d-c800-78bc-3cf8-51db11e27ed0@gmail.com> (raw)
In-Reply-To: <20230905173307.3404875-1-bigunclemax@gmail.com>

On 9/5/23 13:33, Maksim Kiselev wrote:
> The clk_mux_val_to_index() may return -EINVAL. In this case
> clk_mux_get_parent() will return a wrong parent index and
> this will lead to out of bounds error.
> 
> E.g. when we register mux clk:
> 	ret = clk_register(clk, UBOOT_DM_CLK_CCF_MUX, name,
> 			   parent_names[clk_mux_get_parent(clk)]);
> 
> Let's add a check on the return value of clk_mux_val_to_index() and if
> it's negative, set parent index to 0.
> 
> Signed-off-by: Maksim Kiselev <bigunclemax@gmail.com>
> ---
>   drivers/clk/clk-mux.c | 9 ++++++++-
>   1 file changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/clk/clk-mux.c b/drivers/clk/clk-mux.c
> index 017f25f7a5..00fe916171 100644
> --- a/drivers/clk/clk-mux.c
> +++ b/drivers/clk/clk-mux.c
> @@ -88,6 +88,7 @@ unsigned int clk_mux_index_to_val(u32 *table, unsigned int flags, u8 index)
>   u8 clk_mux_get_parent(struct clk *clk)
>   {
>   	struct clk_mux *mux = to_clk_mux(clk);
> +	int index;
>   	u32 val;
>   
>   #if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF)
> @@ -98,7 +99,13 @@ u8 clk_mux_get_parent(struct clk *clk)
>   	val >>= mux->shift;
>   	val &= mux->mask;
>   
> -	return clk_mux_val_to_index(clk, mux->table, mux->flags, val);
> +	index = clk_mux_val_to_index(clk, mux->table, mux->flags, val);
> +	if (index < 0) {
> +		log_err("Could not fetch index\n");
> +		index = 0;
> +	}
> +
> +	return index;
>   }
>   
>   static int clk_fetch_parent_index(struct clk *clk,

Reviewed-by: Sean Anderson <seanga2@gmail.com>

But can you add something to the commit (or error) message about
how this indicates a bug in the driver (e.g. there aren't enough
parents to match the hardware).

--Sean

      reply	other threads:[~2023-11-01 18:41 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-05 17:33 [PATCH v1] clk: mux: check value returned from clk_mux_val_to_index() Maksim Kiselev
2023-11-01 18:41 ` Sean Anderson [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d554be7d-c800-78bc-3cf8-51db11e27ed0@gmail.com \
    --to=seanga2@gmail.com \
    --cc=bigunclemax@gmail.com \
    --cc=lukma@denx.de \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox