Re: [PATCH v2 1/3] clk: at91: optimize pmc data allocation

From: Stephen Boyd
Date: Fri Mar 20 2020 - 20:16:26 EST


Quote
> Alloc whole data structure in one block. This makes the code shorter,
> more efficient and easier to extend in following patch.
>

Generally looks good to me.

> diff --git a/drivers/clk/at91/pmc.c b/drivers/clk/at91/pmc.c
> index b71515acdec1..fe788512fcc0 100644
> --- a/drivers/clk/at91/pmc.c
> +++ b/drivers/clk/at91/pmc.c
> @@ -76,48 +76,30 @@ struct clk_hw *of_clk_hw_pmc_get(struct of_phandle_args *clkspec, void *data)
> return ERR_PTR(-EINVAL);
> }
>
> -void pmc_data_free(struct pmc_data *pmc_data)
> -{
> - kfree(pmc_data->chws);
> - kfree(pmc_data->shws);
> - kfree(pmc_data->phws);
> - kfree(pmc_data->ghws);
> -}
> -
> struct pmc_data *pmc_data_allocate(unsigned int ncore, unsigned int nsystem,
> unsigned int nperiph, unsigned int ngck)
> {
> - struct pmc_data *pmc_data = kzalloc(sizeof(*pmc_data), GFP_KERNEL);
> + unsigned int num_clks = ncore + nsystem + nperiph + ngck;
> + struct pmc_data *pmc_data;
>
> + pmc_data = kzalloc(sizeof(*pmc_data) + num_clks * sizeof(struct clk_hw *),

I think we have struct_size() for this?

> + GFP_KERNEL);
> if (!pmc_data)
> return NULL;
>
> pmc_data->ncore = ncore;
> - pmc_data->chws = kcalloc(ncore, sizeof(struct clk_hw *), GFP_KERNEL);
> - if (!pmc_data->chws)
> - goto err;
> + pmc_data->chws = pmc_data->hwtable;
>
> pmc_data->nsystem = nsystem;
> - pmc_data->shws = kcalloc(nsystem, sizeof(struct clk_hw *), GFP_KERNEL);
> - if (!pmc_data->shws)
> - goto err;
> + pmc_data->shws = pmc_data->chws + ncore;
>
> pmc_data->nperiph = nperiph;
> - pmc_data->phws = kcalloc(nperiph, sizeof(struct clk_hw *), GFP_KERNEL);
> - if (!pmc_data->phws)
> - goto err;
> + pmc_data->phws = pmc_data->shws + nsystem;
>
> pmc_data->ngck = ngck;
> - pmc_data->ghws = kcalloc(ngck, sizeof(struct clk_hw *), GFP_KERNEL);
> - if (!pmc_data->ghws)
> - goto err;
> + pmc_data->ghws = pmc_data->phws + nperiph;
>
> return pmc_data;
> -
> -err:
> - pmc_data_free(pmc_data);
> -
> - return NULL;
> }
>
> #ifdef CONFIG_PM
> diff --git a/drivers/clk/at91/pmc.h b/drivers/clk/at91/pmc.h
> index 9b8db9cdcda5..49cc3d67b98e 100644
> --- a/drivers/clk/at91/pmc.h
> +++ b/drivers/clk/at91/pmc.h
> @@ -24,6 +24,8 @@ struct pmc_data {
> struct clk_hw **phws;
> unsigned int ngck;
> struct clk_hw **ghws;
> +
> + struct clk_hw *hwtable[0];

I've seen people making this C99 compliant for clang. Please make it

struct clk_hw *hwtable[];

instead.

> };
>