Re: [PATCH v3 33/37] mtd: nand: allocate aligned buffers if NAND_OWN_BUFFERS is unset

From: Boris Brezillon
Date: Sun Apr 09 2017 - 10:17:37 EST


On Thu, 30 Mar 2017 17:15:04 +0900
Masahiro Yamada <yamada.masahiro@xxxxxxxxxxxxx> wrote:

> Some NAND controllers are using DMA engine requiring a specific
> buffer alignment. The core provides no guarantee on the nand_buffers
> pointers, which forces some drivers to allocate their own buffers
> and pass the NAND_OWN_BUFFERS flag.
>
> Rework the nand_buffers allocation logic to allocate each buffer
> independently. This should make most NAND controllers/DMA engine
> happy, and allow us to get rid of these custom buf allocation in
> NAND controller drivers.
>
> Signed-off-by: Masahiro Yamada <yamada.masahiro@xxxxxxxxxxxxx>
> ---
>
> Changes in v3:
> - Reword git-log
>
> Changes in v2:
> - Newly added
>
> drivers/mtd/nand/nand_base.c | 34 +++++++++++++++++++++++++++-------
> 1 file changed, 27 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/mtd/nand/nand_base.c b/drivers/mtd/nand/nand_base.c
> index f828ad7..e9d3195 100644
> --- a/drivers/mtd/nand/nand_base.c
> +++ b/drivers/mtd/nand/nand_base.c
> @@ -4613,13 +4613,25 @@ int nand_scan_tail(struct mtd_info *mtd)
> }
>
> if (!(chip->options & NAND_OWN_BUFFERS)) {
> - nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize
> - + mtd->oobsize * 3, GFP_KERNEL);
> + nbuf = kzalloc(sizeof(*nbuf), GFP_KERNEL);
> if (!nbuf)
> return -ENOMEM;
> - nbuf->ecccalc = (uint8_t *)(nbuf + 1);
> - nbuf->ecccode = nbuf->ecccalc + mtd->oobsize;
> - nbuf->databuf = nbuf->ecccode + mtd->oobsize;
> + nbuf->ecccalc = kmalloc(mtd->oobsize, GFP_KERNEL);
> + if (!nbuf->ecccalc) {
> + ret = -EINVAL;
> + goto err_free;

You have a memory leak here, because chip->buffers = nbuf is only done
after all allocations have succeeded.

> + }
> + nbuf->ecccode = kmalloc(mtd->oobsize, GFP_KERNEL);
> + if (!nbuf->ecccode) {
> + ret = -EINVAL;

ret = -ENOMEM;

I have the following fixup patch, let me know if you're okay with it
and I'll squash it in the original commit.

Thanks,

Boris

--->8---