All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Gustavo A. R. Silva" <gustavoars@kernel.org>
To: Stefan Agner <stefan@agner.ch>, Lucas Stach <dev@lynxeye.de>,
	Miquel Raynal <miquel.raynal@bootlin.com>,
	Richard Weinberger <richard@nod.at>,
	Vignesh Raghavendra <vigneshr@ti.com>,
	Thierry Reding <thierry.reding@gmail.com>,
	Jonathan Hunter <jonathanh@nvidia.com>
Cc: linux-mtd@lists.infradead.org, linux-tegra@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>,
	linux-hardening@vger.kernel.org
Subject: [PATCH][next] mtd: rawnand: Replace one-element array with flexible-array member
Date: Wed, 30 Sep 2020 16:08:24 -0500	[thread overview]
Message-ID: <20200930210824.GA12277@embeddedor> (raw)

There is a regular need in the kernel to provide a way to declare having
a dynamically sized set of trailing elements in a structure. Kernel code
should always use “flexible array members”[1] for these cases. The older
style of one-element or zero-length arrays should no longer be used[2].

Refactor the code according to the use of a flexible-array member
instead of a one-element array. Also, make use of the struct_size()
helper to calculate the size of the allocation for _nand_. In order
to keep the code as maintainable as possible and to keep _cs_ as an
array, add a new macro CS_N to aid in the allocation size calculation,
in case there is a need for more Chip Select IDs in the future. In the
meantime, the macro is set to 1. This also avoids having to use a magic
number '1' as the last argument for struct_size().

[1] https://en.wikipedia.org/wiki/Flexible_array_member
[2] https://www.kernel.org/doc/html/v5.9-rc1/process/deprecated.html#zero-length-and-one-element-arrays

Build-tested-by: kernel test robot <lkp@intel.com>
Link: https://lore.kernel.org/lkml/5f7473c0.Vv4h6yzXSga90P04%25lkp@intel.com/
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 drivers/mtd/nand/raw/tegra_nand.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/drivers/mtd/nand/raw/tegra_nand.c b/drivers/mtd/nand/raw/tegra_nand.c
index fbf67722a049..43b8359dcd85 100644
--- a/drivers/mtd/nand/raw/tegra_nand.c
+++ b/drivers/mtd/nand/raw/tegra_nand.c
@@ -163,6 +163,9 @@
 				HWSTATUS_RBSY_MASK(NAND_STATUS_READY) | \
 				HWSTATUS_RBSY_VALUE(NAND_STATUS_READY))
 
+/* Number of Chip Selects. Currently, only one. */
+#define CS_N			1
+
 struct tegra_nand_controller {
 	struct nand_controller controller;
 	struct device *dev;
@@ -183,7 +186,7 @@ struct tegra_nand_chip {
 	u32 config;
 	u32 config_ecc;
 	u32 bch_config;
-	int cs[1];
+	int cs[];
 };
 
 static inline struct tegra_nand_controller *
@@ -1086,14 +1089,14 @@ static int tegra_nand_chips_init(struct device *dev,
 		return -EINVAL;
 	}
 
-	/* Retrieve CS id, currently only single die NAND supported */
+	/* Retrieve CS id, currently only single-die NAND supported */
 	ret = of_property_read_u32(np_nand, "reg", &cs);
 	if (ret) {
 		dev_err(dev, "could not retrieve reg property: %d\n", ret);
 		return ret;
 	}
 
-	nand = devm_kzalloc(dev, sizeof(*nand), GFP_KERNEL);
+	nand = devm_kzalloc(dev, struct_size(nand, cs, CS_N), GFP_KERNEL);
 	if (!nand)
 		return -ENOMEM;
 
-- 
2.27.0


WARNING: multiple messages have this Message-ID (diff)
From: "Gustavo A. R. Silva" <gustavoars@kernel.org>
To: Stefan Agner <stefan@agner.ch>, Lucas Stach <dev@lynxeye.de>,
	Miquel Raynal <miquel.raynal@bootlin.com>,
	Richard Weinberger <richard@nod.at>,
	Vignesh Raghavendra <vigneshr@ti.com>,
	Thierry Reding <thierry.reding@gmail.com>,
	Jonathan Hunter <jonathanh@nvidia.com>
Cc: linux-tegra@vger.kernel.org, linux-hardening@vger.kernel.org,
	linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>
Subject: [PATCH][next] mtd: rawnand: Replace one-element array with flexible-array member
Date: Wed, 30 Sep 2020 16:08:24 -0500	[thread overview]
Message-ID: <20200930210824.GA12277@embeddedor> (raw)

There is a regular need in the kernel to provide a way to declare having
a dynamically sized set of trailing elements in a structure. Kernel code
should always use “flexible array members”[1] for these cases. The older
style of one-element or zero-length arrays should no longer be used[2].

Refactor the code according to the use of a flexible-array member
instead of a one-element array. Also, make use of the struct_size()
helper to calculate the size of the allocation for _nand_. In order
to keep the code as maintainable as possible and to keep _cs_ as an
array, add a new macro CS_N to aid in the allocation size calculation,
in case there is a need for more Chip Select IDs in the future. In the
meantime, the macro is set to 1. This also avoids having to use a magic
number '1' as the last argument for struct_size().

[1] https://en.wikipedia.org/wiki/Flexible_array_member
[2] https://www.kernel.org/doc/html/v5.9-rc1/process/deprecated.html#zero-length-and-one-element-arrays

Build-tested-by: kernel test robot <lkp@intel.com>
Link: https://lore.kernel.org/lkml/5f7473c0.Vv4h6yzXSga90P04%25lkp@intel.com/
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 drivers/mtd/nand/raw/tegra_nand.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/drivers/mtd/nand/raw/tegra_nand.c b/drivers/mtd/nand/raw/tegra_nand.c
index fbf67722a049..43b8359dcd85 100644
--- a/drivers/mtd/nand/raw/tegra_nand.c
+++ b/drivers/mtd/nand/raw/tegra_nand.c
@@ -163,6 +163,9 @@
 				HWSTATUS_RBSY_MASK(NAND_STATUS_READY) | \
 				HWSTATUS_RBSY_VALUE(NAND_STATUS_READY))
 
+/* Number of Chip Selects. Currently, only one. */
+#define CS_N			1
+
 struct tegra_nand_controller {
 	struct nand_controller controller;
 	struct device *dev;
@@ -183,7 +186,7 @@ struct tegra_nand_chip {
 	u32 config;
 	u32 config_ecc;
 	u32 bch_config;
-	int cs[1];
+	int cs[];
 };
 
 static inline struct tegra_nand_controller *
@@ -1086,14 +1089,14 @@ static int tegra_nand_chips_init(struct device *dev,
 		return -EINVAL;
 	}
 
-	/* Retrieve CS id, currently only single die NAND supported */
+	/* Retrieve CS id, currently only single-die NAND supported */
 	ret = of_property_read_u32(np_nand, "reg", &cs);
 	if (ret) {
 		dev_err(dev, "could not retrieve reg property: %d\n", ret);
 		return ret;
 	}
 
-	nand = devm_kzalloc(dev, sizeof(*nand), GFP_KERNEL);
+	nand = devm_kzalloc(dev, struct_size(nand, cs, CS_N), GFP_KERNEL);
 	if (!nand)
 		return -ENOMEM;
 
-- 
2.27.0


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

             reply	other threads:[~2020-09-30 21:02 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-30 21:08 Gustavo A. R. Silva [this message]
2020-09-30 21:08 ` [PATCH][next] mtd: rawnand: Replace one-element array with flexible-array member Gustavo A. R. Silva
2020-09-30 21:10 ` Jann Horn
2020-09-30 21:10   ` Jann Horn
2020-09-30 21:36   ` Gustavo A. R. Silva
2020-09-30 21:36     ` Gustavo A. R. Silva
2020-09-30 22:32     ` Jann Horn
2020-09-30 22:32       ` Jann Horn
2020-10-01  8:12       ` Miquel Raynal
2020-10-01  8:12         ` Miquel Raynal
2020-10-01  9:21         ` Stefan Agner
2020-10-01  9:21           ` Stefan Agner

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=20200930210824.GA12277@embeddedor \
    --to=gustavoars@kernel.org \
    --cc=dev@lynxeye.de \
    --cc=jonathanh@nvidia.com \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=linux-tegra@vger.kernel.org \
    --cc=miquel.raynal@bootlin.com \
    --cc=richard@nod.at \
    --cc=stefan@agner.ch \
    --cc=thierry.reding@gmail.com \
    --cc=vigneshr@ti.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.