From: Jonathan Cameron <jic23@kernel.org>
To: Matteo Martelli <matteomartelli3@gmail.com>
Cc: Lars-Peter Clausen <lars@metafoo.de>,
Michael Hennerich <Michael.Hennerich@analog.com>,
Alisa-Dariana Roman <alisa.roman@analog.com>,
Christian Eggers <ceggers@arri.de>, Peter Rosin <peda@axentia.se>,
Paul Cercueil <paul@crapouillou.net>,
Sebastian Reichel <sre@kernel.org>,
linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-mips@vger.kernel.org, linux-pm@vger.kernel.org
Subject: Re: [PATCH v4 2/5] iio: consumers: copy/release available info from producer to fix race
Date: Sat, 19 Oct 2024 13:04:14 +0100 [thread overview]
Message-ID: <20241019130414.014a6384@jic23-huawei> (raw)
In-Reply-To: <20241018-iio-read-avail-release-v4-2-53c8ac618585@gmail.com>
On Fri, 18 Oct 2024 12:16:41 +0200
Matteo Martelli <matteomartelli3@gmail.com> wrote:
> Consumers need to call the producer's read_avail_release_resource()
> callback after reading producer's available info. To avoid a race
> condition with the producer unregistration, change inkern
> iio_channel_read_avail() so that it copies the available info from the
> producer and immediately calls its release callback with info_exists
> locked.
>
> Also, modify the users of iio_read_avail_channel_raw() and
> iio_read_avail_channel_attribute() to free the copied available buffers
> after calling these functions.
>
> Signed-off-by: Matteo Martelli <matteomartelli3@gmail.com>
Hi Matteo,
The cleanup.h stuff is new and comes with footguns. As such the
'rules' applied are perhaps stricter than then will be in the long term.
https://lore.kernel.org/all/172294149613.2215.3274492813920223809.tip-bot2@tip-bot2/
is what we have today. Particularly the last few paragraphs on usage.
> @@ -857,7 +879,7 @@ static int iio_channel_read_min(struct iio_channel *chan,
> int *val, int *val2, int *type,
> enum iio_chan_info_enum info)
> {
> - const int *vals;
> + const int *vals __free(kfree) = NULL;
Unlike below this one is 'almost' ok because there isn't much below. However,
still not good because of the risk of future code putting something in between.
At very minimum move it down to just before the place it's allocated.
It's a bit messy but maybe what we need is:
int *iio_read_avail_channel_attribute_retvals(struct iio_channel *chan,
int *type, int *length,
enum iio_chan_info_enum attr)
{
int *vals;
int ret;
ret = iio_read_avail_channel_attribute(chan, &vals, type, len, attr;
if (ret)
return ERR_PTR(ret);
return vals;
}
Then you can do
const int *vals __free(kfree) =
iio_channel_read_avail_retvals(chan, type, &length, info);
if (IS_ERR(vals))
...
and obey the suggested style for cleanup.h usage.
Would need some clear comments on why it exists though!
(+ maybe a better name)
> int length;
> int ret;
>
> diff --git a/drivers/power/supply/ingenic-battery.c b/drivers/power/supply/ingenic-battery.c
> index 0a40f425c27723ccec49985b8b5e14a737b6a7eb..6b7856e69f5fb7b8b73166b9b6825f4af7b19129 100644
> --- a/drivers/power/supply/ingenic-battery.c
> +++ b/drivers/power/supply/ingenic-battery.c
> @@ -6,12 +6,14 @@
> * based on drivers/power/supply/jz4740-battery.c
> */
>
> +#include <linux/cleanup.h>
> #include <linux/iio/consumer.h>
> #include <linux/module.h>
> #include <linux/of.h>
> #include <linux/platform_device.h>
> #include <linux/power_supply.h>
> #include <linux/property.h>
> +#include <linux/slab.h>
>
> struct ingenic_battery {
> struct device *dev;
> @@ -62,7 +64,7 @@ static int ingenic_battery_get_property(struct power_supply *psy,
> */
> static int ingenic_battery_set_scale(struct ingenic_battery *bat)
> {
> - const int *scale_raw;
> + const int *scale_raw __free(kfree) = NULL;
This isn't a good pattern as per the docs I just replied to v3 with.
Whilst the code is functionally correct today, it is fragile for the reasons
in those docs.
> int scale_len, scale_type, best_idx = -1, best_mV, max_raw, i, ret;
> u64 max_mV;
>
next prev parent reply other threads:[~2024-10-19 12:04 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-18 10:16 [PATCH v4 0/5] iio: fix possible race condition during access of available info lists Matteo Martelli
2024-10-18 10:16 ` [PATCH v4 1/5] iio: core: add read_avail_release_resource callback to fix race Matteo Martelli
2024-10-18 10:16 ` [PATCH v4 2/5] iio: consumers: copy/release available info from producer " Matteo Martelli
2024-10-19 12:04 ` Jonathan Cameron [this message]
2024-10-18 10:16 ` [PATCH v4 3/5] iio: pac1921: use read_avail+release APIs instead of custom ext_info Matteo Martelli
2024-10-18 10:16 ` [PATCH v4 4/5] iio: ad7192: copy/release available filter frequencies to fix race Matteo Martelli
2024-10-18 10:16 ` [PATCH v4 5/5] iio: as73211: copy/release available integration times " Matteo Martelli
2024-10-19 6:06 ` Christian Eggers
2024-10-19 12:07 ` [PATCH v4 0/5] iio: fix possible race condition during access of available info lists 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=20241019130414.014a6384@jic23-huawei \
--to=jic23@kernel.org \
--cc=Michael.Hennerich@analog.com \
--cc=alisa.roman@analog.com \
--cc=ceggers@arri.de \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mips@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=matteomartelli3@gmail.com \
--cc=paul@crapouillou.net \
--cc=peda@axentia.se \
--cc=sre@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