From: Andre Przywara <andre.przywara@arm.com>
To: Michael Nazzareno Trimarchi <michael@amarulasolutions.com>,
Richard Genoud <richard.genoud@bootlin.com>
Cc: Dario Binacchi <dario.binacchi@amarulasolutions.com>,
Tom Rini <trini@konsulko.com>,
Andrew Goodbody <andrew.goodbody@linaro.org>,
Miquel Raynal <miquel.raynal@bootlin.com>,
James Hilliard <james.hilliard1@gmail.com>,
Boris Brezillon <bbrezillon@kernel.org>,
Thomas Petazzoni <thomas.petazzoni@bootlin.com>,
u-boot@lists.denx.de
Subject: Re: [PATCH v2 3/5] mtd: rawnand: sunxi: clean sunxi_nand_chip_init()
Date: Fri, 1 May 2026 14:59:03 +0200 [thread overview]
Message-ID: <20260501145903.087f1204@ryzen.lan> (raw)
In-Reply-To: <cfe67c5d-0762-4366-94df-e64728847f0b@arm.com>
On Mon, 27 Apr 2026 14:35:08 +0200
Andre Przywara <andre.przywara@arm.com> wrote:
> Hi,
>
> On 3/27/26 15:42, Michael Nazzareno Trimarchi wrote:
> > Hi Richard
> >
> > On Fri, Mar 27, 2026 at 3:05 PM Richard Genoud
> > <richard.genoud@bootlin.com> wrote:
> >>
> >> In sunxi_nand_chip_init there's quite a lot of kfree/return, it's easy
> >> to forget a kfree(), so use a goto/kfree instead.
> >>
> >> Signed-off-by: Richard Genoud <richard.genoud@bootlin.com>
> >> ---
> >> drivers/mtd/nand/raw/sunxi_nand.c | 41 ++++++++++++++-----------------
> >> 1 file changed, 18 insertions(+), 23 deletions(-)
> >>
> >> diff --git a/drivers/mtd/nand/raw/sunxi_nand.c b/drivers/mtd/nand/raw/sunxi_nand.c
> >> index 9fc9bc5e0198..ecab9ebc9576 100644
> >> --- a/drivers/mtd/nand/raw/sunxi_nand.c
> >> +++ b/drivers/mtd/nand/raw/sunxi_nand.c
> >> @@ -1600,21 +1600,20 @@ static int sunxi_nand_chip_init(struct udevice *dev, struct sunxi_nfc *nfc,
> >> if (ret) {
> >> dev_err(dev, "could not retrieve reg property: %d\n",
> >> ret);
> >> - kfree(chip);
> >> - return ret;
> >> + goto out;
> >> }
> >>
> >> if (tmp > NFC_MAX_CS) {
> >> dev_err(dev,
> >> "invalid reg value: %u (max CS = 7)\n", tmp);
> >> - kfree(chip);
> >> - return -EINVAL;
> >> + ret = -EINVAL;
> >> + goto out;
> >> }
> >>
> >> if (test_and_set_bit(tmp, &nfc->assigned_cs)) {
> >> dev_err(dev, "CS %d already assigned\n", tmp);
> >> - kfree(chip);
> >> - return -EINVAL;
> >> + ret = -EINVAL;
> >> + goto out;
> >> }
> >>
> >> chip->sels[i].cs = tmp;
> >> @@ -1640,15 +1639,13 @@ static int sunxi_nand_chip_init(struct udevice *dev, struct sunxi_nfc *nfc,
> >> dev_err(dev,
> >> "could not retrieve timings for ONFI mode 0: %d\n",
> >> ret);
> >> - kfree(chip);
> >> - return ret;
> >> + goto out;
> >> }
> >>
> >> ret = sunxi_nand_chip_set_timings(nfc, chip, timings);
> >> if (ret) {
> >> dev_err(dev, "could not configure chip timings: %d\n", ret);
> >> - kfree(chip);
> >> - return ret;
> >> + goto out;
> >> }
> >>
> >> nand = &chip->nand;
> >> @@ -1669,10 +1666,8 @@ static int sunxi_nand_chip_init(struct udevice *dev, struct sunxi_nfc *nfc,
> >>
> >> mtd = nand_to_mtd(nand);
> >> ret = nand_scan_ident(mtd, nsels, NULL);
> >> - if (ret) {
> >> - kfree(chip);
> >> - return ret;
> >> - }
> >> + if (ret)
> >> + goto out;
> >>
> >> if (nand->bbt_options & NAND_BBT_USE_FLASH)
> >> nand->bbt_options |= NAND_BBT_NO_OOB;
> >> @@ -1685,34 +1680,34 @@ static int sunxi_nand_chip_init(struct udevice *dev, struct sunxi_nfc *nfc,
> >> ret = sunxi_nand_chip_init_timings(nfc, chip);
> >> if (ret) {
> >> dev_err(dev, "could not configure chip timings: %d\n", ret);
> >> - kfree(chip);
> >> - return ret;
> >> + goto out;
> >> }
> >>
> >> ret = sunxi_nand_ecc_init(mtd, &nand->ecc);
> >> if (ret) {
> >> dev_err(dev, "ECC init failed: %d\n", ret);
> >> - kfree(chip);
> >> - return ret;
> >> + goto out;
> >> }
> >>
> >> ret = nand_scan_tail(mtd);
> >> if (ret) {
> >> dev_err(dev, "nand_scan_tail failed: %d\n", ret);
> >> - kfree(chip);
> >> - return ret;
> >> + goto out;
> >> }
> >>
> >> ret = nand_register(devnum, mtd);
> >> if (ret) {
> >> dev_err(dev, "failed to register mtd device: %d\n", ret);
> >> - kfree(chip);
> >> - return ret;
> >> + goto out;
> >> }
> >>
> >> list_add_tail(&chip->node, &nfc->chips);
> >>
> >> - return 0;
> >> +out:
> >> + if (ret)
> >> + kfree(chip);
> >> +
> >> + return ret;
> >> }
> >>
> >> static int sunxi_nand_chips_init(struct udevice *dev, struct sunxi_nfc *nfc)
> >
> > I need to go a bit in the code but seems that out can happen even if
> > there is no error. It's
> > not so common to have if (ret) if exit condition in case of errot
>
> So I think it's fine in this case, ret is always set before we "goto",
> and in the success case "ret" must be 0 from the last check. But I agree
> that the construct is a bit uncommon and fragile, so what about keeping
> the "return 0;" (to use the common pattern and help readability), and
> rename the label to "out_err" or "out_free", to make it more obvious
> that this the error path only, and we expect ret to be set?
So FYI, in the interest of moving this patch set forward, I took the
freedom of changing this as I suggested above. That's gonna be part of
the PR now.
Cheers,
Andre
>
> Cheers,
> Andre
>
> >
> > Michael
>
next prev parent reply other threads:[~2026-05-01 12:59 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-27 14:05 [PATCH v2 0/5] mtd: rawnand: sunxi: Fix user data length for H6 Richard Genoud
2026-03-27 14:05 ` [PATCH v2 1/5] mtd: rawnand: sunxi: Replace hard coded value by a define Richard Genoud
2026-04-27 11:14 ` Andre Przywara
2026-03-27 14:05 ` [PATCH v2 2/5] mtd: rawnand: sunxi: make the code mode self-explanatory Richard Genoud
2026-03-27 14:40 ` Michael Nazzareno Trimarchi
2026-04-27 12:23 ` Andre Przywara
2026-03-27 14:05 ` [PATCH v2 3/5] mtd: rawnand: sunxi: clean sunxi_nand_chip_init() Richard Genoud
2026-03-27 14:42 ` Michael Nazzareno Trimarchi
2026-04-27 12:35 ` Andre Przywara
2026-05-01 12:59 ` Andre Przywara [this message]
2026-03-27 14:05 ` [PATCH v2 4/5] mtd: rawnand: sunxi: introduce variable user data length Richard Genoud
2026-05-01 12:46 ` Andre Przywara
2026-03-27 14:05 ` [PATCH v2 5/5] mtd: nand: raw: sunxi_spl: remove user data length reset Richard Genoud
2026-05-01 12:47 ` Andre Przywara
2026-04-27 12:28 ` [PATCH v2 0/5] mtd: rawnand: sunxi: Fix user data length for H6 Andre Przywara
2026-04-27 12:31 ` Michael Nazzareno Trimarchi
2026-04-27 13:47 ` Andre Przywara
2026-04-27 14:10 ` Michael Nazzareno Trimarchi
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=20260501145903.087f1204@ryzen.lan \
--to=andre.przywara@arm.com \
--cc=andrew.goodbody@linaro.org \
--cc=bbrezillon@kernel.org \
--cc=dario.binacchi@amarulasolutions.com \
--cc=james.hilliard1@gmail.com \
--cc=michael@amarulasolutions.com \
--cc=miquel.raynal@bootlin.com \
--cc=richard.genoud@bootlin.com \
--cc=thomas.petazzoni@bootlin.com \
--cc=trini@konsulko.com \
--cc=u-boot@lists.denx.de \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox