From: Jonathan Cameron <jic23@kernel.org>
To: Matti Vaittinen <mazziesaccount@gmail.com>
Cc: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>,
Lars-Peter Clausen <lars@metafoo.de>,
Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>,
linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 2/8] iio: gts: Simplify using __free
Date: Sat, 23 Nov 2024 16:37:13 +0000 [thread overview]
Message-ID: <20241123163713.2ec03a37@jic23-huawei> (raw)
In-Reply-To: <5efc30d832275778d1f48d7e2c75b1ecc63511d5.1732105157.git.mazziesaccount@gmail.com>
On Thu, 21 Nov 2024 10:20:07 +0200
Matti Vaittinen <mazziesaccount@gmail.com> wrote:
> The error path in the gain_to_scaletables() uses goto for unwinding an
> allocation on failure. This can be slightly simplified by using the
> automated free when exiting the scope.
>
> Use __free(kfree) and drop the goto based error handling.
>
> Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com>
>
> ---
>
> Revision history:
> v1 => v2:
> - patch number changed because a change was added to the series.
> - rebased on iio/testing to avoid conflicts with queued fixes
> ---
> drivers/iio/industrialio-gts-helper.c | 19 ++++++++-----------
> 1 file changed, 8 insertions(+), 11 deletions(-)
>
> diff --git a/drivers/iio/industrialio-gts-helper.c b/drivers/iio/industrialio-gts-helper.c
> index 291c0fc332c9..602d3d338e66 100644
> --- a/drivers/iio/industrialio-gts-helper.c
> +++ b/drivers/iio/industrialio-gts-helper.c
> @@ -4,6 +4,7 @@
> * Copyright (c) 2023 Matti Vaittinen <mazziesaccount@gmail.com>
> */
>
> +#include <linux/cleanup.h>
> #include <linux/device.h>
> #include <linux/errno.h>
> #include <linux/export.h>
> @@ -167,8 +168,8 @@ static int iio_gts_gain_cmp(const void *a, const void *b)
>
> static int gain_to_scaletables(struct iio_gts *gts, int **gains, int **scales)
> {
> - int i, j, new_idx, time_idx, ret = 0;
> - int *all_gains;
> + int ret, i, j, new_idx, time_idx;
> + int *all_gains __free(kfree) = NULL;
See the docs in cleanup.h (added recently).
Constructor and destructor should go together. Dan wrote good docs on this
(which are now in cleanup.h) so I'll not go into why!
Upshot is this goes where you do the kcalloc, not up here.
> size_t gain_bytes;
>
> for (i = 0; i < gts->num_itime; i++) {
> @@ -232,10 +233,9 @@ static int gain_to_scaletables(struct iio_gts *gts, int **gains, int **scales)
>
> gts->avail_all_scales_table = kcalloc(new_idx, 2 * sizeof(int),
> GFP_KERNEL);
> - if (!gts->avail_all_scales_table) {
> - ret = -ENOMEM;
> - goto free_out;
> - }
> + if (!gts->avail_all_scales_table)
> + return -ENOMEM;
> +
> gts->num_avail_all_scales = new_idx;
>
> for (i = 0; i < gts->num_avail_all_scales; i++) {
> @@ -246,14 +246,11 @@ static int gain_to_scaletables(struct iio_gts *gts, int **gains, int **scales)
> if (ret) {
> kfree(gts->avail_all_scales_table);
> gts->num_avail_all_scales = 0;
> - goto free_out;
> + return ret;
> }
> }
>
> -free_out:
> - kfree(all_gains);
> -
> - return ret;
> + return 0;
> }
>
> /**
next prev parent reply other threads:[~2024-11-23 16:37 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-21 8:19 [PATCH v2 0/8] Support ROHM KX134ACR-LBZ Matti Vaittinen
2024-11-21 8:19 ` [PATCH v2 1/8] iio: accel: kx022a: Improve reset delay Matti Vaittinen
2024-11-23 16:34 ` Jonathan Cameron
2024-11-21 8:20 ` [PATCH v2 2/8] iio: gts: Simplify using __free Matti Vaittinen
2024-11-23 16:37 ` Jonathan Cameron [this message]
2024-11-25 9:16 ` Matti Vaittinen
2024-11-26 17:52 ` Jonathan Cameron
2024-11-27 14:14 ` Matti Vaittinen
2024-11-21 8:20 ` [PATCH v2 3/8] iio: accel: kx022a: Use cleanup.h helpers Matti Vaittinen
2024-11-23 16:42 ` Jonathan Cameron
2024-11-25 9:34 ` Matti Vaittinen
2024-11-26 17:55 ` Jonathan Cameron
2024-11-27 13:54 ` Matti Vaittinen
2024-11-21 8:20 ` [PATCH v2 4/8] iio: accel: kx022a: Support ICs with different G-ranges Matti Vaittinen
2024-11-21 8:20 ` [PATCH v2 5/8] dt-bindings: ROHM KX134ACR-LBZ Matti Vaittinen
2024-11-21 8:21 ` [PATCH v2 6/8] iio: kx022a: Support " Matti Vaittinen
2024-11-21 8:21 ` [PATCH v2 7/8] dt-bindings: iio: kx022a: Support KX134-1211 Matti Vaittinen
2024-11-21 19:55 ` Conor Dooley
2024-11-21 8:21 ` [PATCH v2 8/8] iio: accel: " Matti Vaittinen
2024-11-23 16:43 ` Jonathan Cameron
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=20241123163713.2ec03a37@jic23-huawei \
--to=jic23@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=krzk+dt@kernel.org \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=matti.vaittinen@fi.rohmeurope.com \
--cc=mazziesaccount@gmail.com \
--cc=robh@kernel.org \
/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;
as well as URLs for NNTP newsgroup(s).