linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mmc: sdhci-cadence: use struct_size() helper
@ 2019-08-08 16:53 Gustavo A. R. Silva
  2019-08-12  8:23 ` Adrian Hunter
  2019-08-22 12:13 ` Ulf Hansson
  0 siblings, 2 replies; 3+ messages in thread
From: Gustavo A. R. Silva @ 2019-08-08 16:53 UTC (permalink / raw)
  To: Adrian Hunter, Ulf Hansson; +Cc: linux-mmc, linux-kernel, Gustavo A. R. Silva

One of the more common cases of allocation size calculations is finding
the size of a structure that has a zero-sized array at the end, along
with memory for some number of elements for that array. For example:

struct sdhci_cdns_priv {
	...
        struct sdhci_cdns_phy_param phy_params[0];
};

Make use of the struct_size() helper instead of an open-coded version
in order to avoid any potential type mistakes.

So, replace the following form:

sizeof(*priv) + sizeof(priv->phy_params[0]) * nr_phy_params

with:

struct_size(priv, phy_params, nr_phy_params)

Also, notice that, in this case, variable priv_size is not necessary,
hence it is removed.

This code was detected with the help of Coccinelle.

Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
---
 drivers/mmc/host/sdhci-cadence.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/mmc/host/sdhci-cadence.c b/drivers/mmc/host/sdhci-cadence.c
index 163d1cf4367e..1768a13f89be 100644
--- a/drivers/mmc/host/sdhci-cadence.c
+++ b/drivers/mmc/host/sdhci-cadence.c
@@ -337,7 +337,6 @@ static int sdhci_cdns_probe(struct platform_device *pdev)
 	struct sdhci_pltfm_host *pltfm_host;
 	struct sdhci_cdns_priv *priv;
 	struct clk *clk;
-	size_t priv_size;
 	unsigned int nr_phy_params;
 	int ret;
 	struct device *dev = &pdev->dev;
@@ -351,8 +350,8 @@ static int sdhci_cdns_probe(struct platform_device *pdev)
 		return ret;
 
 	nr_phy_params = sdhci_cdns_phy_param_count(dev->of_node);
-	priv_size = sizeof(*priv) + sizeof(priv->phy_params[0]) * nr_phy_params;
-	host = sdhci_pltfm_init(pdev, &sdhci_cdns_pltfm_data, priv_size);
+	host = sdhci_pltfm_init(pdev, &sdhci_cdns_pltfm_data,
+				struct_size(priv, phy_params, nr_phy_params));
 	if (IS_ERR(host)) {
 		ret = PTR_ERR(host);
 		goto disable_clk;
-- 
2.22.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] mmc: sdhci-cadence: use struct_size() helper
  2019-08-08 16:53 [PATCH] mmc: sdhci-cadence: use struct_size() helper Gustavo A. R. Silva
@ 2019-08-12  8:23 ` Adrian Hunter
  2019-08-22 12:13 ` Ulf Hansson
  1 sibling, 0 replies; 3+ messages in thread
From: Adrian Hunter @ 2019-08-12  8:23 UTC (permalink / raw)
  To: Gustavo A. R. Silva, Ulf Hansson; +Cc: linux-mmc, linux-kernel

On 8/08/19 7:53 PM, Gustavo A. R. Silva wrote:
> One of the more common cases of allocation size calculations is finding
> the size of a structure that has a zero-sized array at the end, along
> with memory for some number of elements for that array. For example:
> 
> struct sdhci_cdns_priv {
> 	...
>         struct sdhci_cdns_phy_param phy_params[0];
> };
> 
> Make use of the struct_size() helper instead of an open-coded version
> in order to avoid any potential type mistakes.
> 
> So, replace the following form:
> 
> sizeof(*priv) + sizeof(priv->phy_params[0]) * nr_phy_params
> 
> with:
> 
> struct_size(priv, phy_params, nr_phy_params)
> 
> Also, notice that, in this case, variable priv_size is not necessary,
> hence it is removed.
> 
> This code was detected with the help of Coccinelle.
> 
> Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>

Acked-by: Adrian Hunter <adrian.hunter@intel.com>

> ---
>  drivers/mmc/host/sdhci-cadence.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/mmc/host/sdhci-cadence.c b/drivers/mmc/host/sdhci-cadence.c
> index 163d1cf4367e..1768a13f89be 100644
> --- a/drivers/mmc/host/sdhci-cadence.c
> +++ b/drivers/mmc/host/sdhci-cadence.c
> @@ -337,7 +337,6 @@ static int sdhci_cdns_probe(struct platform_device *pdev)
>  	struct sdhci_pltfm_host *pltfm_host;
>  	struct sdhci_cdns_priv *priv;
>  	struct clk *clk;
> -	size_t priv_size;
>  	unsigned int nr_phy_params;
>  	int ret;
>  	struct device *dev = &pdev->dev;
> @@ -351,8 +350,8 @@ static int sdhci_cdns_probe(struct platform_device *pdev)
>  		return ret;
>  
>  	nr_phy_params = sdhci_cdns_phy_param_count(dev->of_node);
> -	priv_size = sizeof(*priv) + sizeof(priv->phy_params[0]) * nr_phy_params;
> -	host = sdhci_pltfm_init(pdev, &sdhci_cdns_pltfm_data, priv_size);
> +	host = sdhci_pltfm_init(pdev, &sdhci_cdns_pltfm_data,
> +				struct_size(priv, phy_params, nr_phy_params));
>  	if (IS_ERR(host)) {
>  		ret = PTR_ERR(host);
>  		goto disable_clk;
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] mmc: sdhci-cadence: use struct_size() helper
  2019-08-08 16:53 [PATCH] mmc: sdhci-cadence: use struct_size() helper Gustavo A. R. Silva
  2019-08-12  8:23 ` Adrian Hunter
@ 2019-08-22 12:13 ` Ulf Hansson
  1 sibling, 0 replies; 3+ messages in thread
From: Ulf Hansson @ 2019-08-22 12:13 UTC (permalink / raw)
  To: Gustavo A. R. Silva; +Cc: Adrian Hunter, linux-mmc, Linux Kernel Mailing List

On Thu, 8 Aug 2019 at 18:53, Gustavo A. R. Silva <gustavo@embeddedor.com> wrote:
>
> One of the more common cases of allocation size calculations is finding
> the size of a structure that has a zero-sized array at the end, along
> with memory for some number of elements for that array. For example:
>
> struct sdhci_cdns_priv {
>         ...
>         struct sdhci_cdns_phy_param phy_params[0];
> };
>
> Make use of the struct_size() helper instead of an open-coded version
> in order to avoid any potential type mistakes.
>
> So, replace the following form:
>
> sizeof(*priv) + sizeof(priv->phy_params[0]) * nr_phy_params
>
> with:
>
> struct_size(priv, phy_params, nr_phy_params)
>
> Also, notice that, in this case, variable priv_size is not necessary,
> hence it is removed.
>
> This code was detected with the help of Coccinelle.
>
> Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>

Applied for next, thanks!

Kind regards
Uffe


> ---
>  drivers/mmc/host/sdhci-cadence.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/mmc/host/sdhci-cadence.c b/drivers/mmc/host/sdhci-cadence.c
> index 163d1cf4367e..1768a13f89be 100644
> --- a/drivers/mmc/host/sdhci-cadence.c
> +++ b/drivers/mmc/host/sdhci-cadence.c
> @@ -337,7 +337,6 @@ static int sdhci_cdns_probe(struct platform_device *pdev)
>         struct sdhci_pltfm_host *pltfm_host;
>         struct sdhci_cdns_priv *priv;
>         struct clk *clk;
> -       size_t priv_size;
>         unsigned int nr_phy_params;
>         int ret;
>         struct device *dev = &pdev->dev;
> @@ -351,8 +350,8 @@ static int sdhci_cdns_probe(struct platform_device *pdev)
>                 return ret;
>
>         nr_phy_params = sdhci_cdns_phy_param_count(dev->of_node);
> -       priv_size = sizeof(*priv) + sizeof(priv->phy_params[0]) * nr_phy_params;
> -       host = sdhci_pltfm_init(pdev, &sdhci_cdns_pltfm_data, priv_size);
> +       host = sdhci_pltfm_init(pdev, &sdhci_cdns_pltfm_data,
> +                               struct_size(priv, phy_params, nr_phy_params));
>         if (IS_ERR(host)) {
>                 ret = PTR_ERR(host);
>                 goto disable_clk;
> --
> 2.22.0
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-08-22 12:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-08 16:53 [PATCH] mmc: sdhci-cadence: use struct_size() helper Gustavo A. R. Silva
2019-08-12  8:23 ` Adrian Hunter
2019-08-22 12:13 ` Ulf Hansson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).