[lkml]   [2020]   [Sep]   [30]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
    Patch in this message
    Subject[PATCH][next] mtd: rawnand: Replace one-element array with flexible-array member
    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().


    Build-tested-by: kernel test robot <>
    Signed-off-by: Gustavo A. R. Silva <>
    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 @@

    +/* 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;

     \ /
      Last update: 2020-09-30 23:03    [W:2.264 / U:0.008 seconds]
    ©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site