* [PATCH v4] iio: st_sensors: drop temporary kmalloc buffer and reuse buffer_data
@ 2026-03-15 12:16 Sanjay Chitroda
2026-03-21 20:10 ` Jonathan Cameron
0 siblings, 1 reply; 2+ messages in thread
From: Sanjay Chitroda @ 2026-03-15 12:16 UTC (permalink / raw)
To: jic23, dlechner, nuno.sa, andy
Cc: kees, linux-iio, linux-kernel, sanjayembeddedse
From: Sanjay Chitroda <sanjayembeddedse@gmail.com>
Replace the per-call kmalloc() scratch buffer with the existing
buffer_data[] field present in struct st_sensor_data. The existing buffer
is DMA-aligned and sufficiently sized for all channel widths, so using it
avoids unnecessary dynamic memory allocation on each read.
This simplifies the code, removes redundant allocation and cleanup.
No functional change intended.
Signed-off-by: Sanjay Chitroda <sanjayembeddedse@gmail.com>
Reviewed-by: David Lechner <dlechner@baylibre.com>
---
Changes in v4:
- Rectify commit message review by David Lechner
- Link to v3 https://lore.kernel.org/all/20260312063424.3846945-1-sanjayembedded@gmail.com/
Changes in v3:
- fix variable declartion placement to follow kernel coding style
- Link to v2 https://lore.kernel.org/all/20260311182050.3467471-1-sanjayembedded@gmail.com/
Changes in v2:
- split series to individual patch
- address review comment from David Lechner and reuse exising buffer instead of allocation
- Link to v1 https://lore.kernel.org/all/20260310200513.2162018-4-sanjayembedded@gmail.com/
---
drivers/iio/common/st_sensors/st_sensors_core.c | 11 +++--------
1 file changed, 3 insertions(+), 8 deletions(-)
diff --git a/drivers/iio/common/st_sensors/st_sensors_core.c b/drivers/iio/common/st_sensors/st_sensors_core.c
index dac593be5695..dbc5e16fbde4 100644
--- a/drivers/iio/common/st_sensors/st_sensors_core.c
+++ b/drivers/iio/common/st_sensors/st_sensors_core.c
@@ -501,14 +501,12 @@ static int st_sensors_read_axis_data(struct iio_dev *indio_dev,
byte_for_channel = DIV_ROUND_UP(ch->scan_type.realbits +
ch->scan_type.shift, 8);
- outdata = kmalloc(byte_for_channel, GFP_DMA | GFP_KERNEL);
- if (!outdata)
- return -ENOMEM;
+ outdata = sdata->buffer_data;
err = regmap_bulk_read(sdata->regmap, ch->address,
outdata, byte_for_channel);
if (err < 0)
- goto st_sensors_free_memory;
+ return err;
if (byte_for_channel == 1)
*data = (s8)*outdata;
@@ -517,10 +515,7 @@ static int st_sensors_read_axis_data(struct iio_dev *indio_dev,
else if (byte_for_channel == 3)
*data = (s32)sign_extend32(get_unaligned_le24(outdata), 23);
-st_sensors_free_memory:
- kfree(outdata);
-
- return err;
+ return 0;
}
int st_sensors_read_info_raw(struct iio_dev *indio_dev,
--
2.34.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH v4] iio: st_sensors: drop temporary kmalloc buffer and reuse buffer_data
2026-03-15 12:16 [PATCH v4] iio: st_sensors: drop temporary kmalloc buffer and reuse buffer_data Sanjay Chitroda
@ 2026-03-21 20:10 ` Jonathan Cameron
0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Cameron @ 2026-03-21 20:10 UTC (permalink / raw)
To: Sanjay Chitroda
Cc: dlechner, nuno.sa, andy, kees, linux-iio, linux-kernel,
Denis Ciocca
On Sun, 15 Mar 2026 17:46:25 +0530
Sanjay Chitroda <sanjayembeddedse@gmail.com> wrote:
> From: Sanjay Chitroda <sanjayembeddedse@gmail.com>
>
> Replace the per-call kmalloc() scratch buffer with the existing
> buffer_data[] field present in struct st_sensor_data. The existing buffer
> is DMA-aligned and sufficiently sized for all channel widths, so using it
> avoids unnecessary dynamic memory allocation on each read.
>
> This simplifies the code, removes redundant allocation and cleanup.
> No functional change intended.
>
> Signed-off-by: Sanjay Chitroda <sanjayembeddedse@gmail.com>
> Reviewed-by: David Lechner <dlechner@baylibre.com>
I briefly wondered if the locking was strong enough to ensure
this isn't used at the same time as the use of the buffer in
the trigger handler. I think it's fine though due to the
fact the only path to this function has claimed direct mode.
I haven't completely ruled out a race around the disable path for the
buffer but given the worst that can happen is wrong sensor
data I think we are fine even if there is one.
So with that in mind applied to the testing branch of iio.git which
will become togreg and get picked up by linux-next once the bots
have taken a first look at it.
Thanks,
Jonathan
> ---
> Changes in v4:
> - Rectify commit message review by David Lechner
> - Link to v3 https://lore.kernel.org/all/20260312063424.3846945-1-sanjayembedded@gmail.com/
> Changes in v3:
> - fix variable declartion placement to follow kernel coding style
> - Link to v2 https://lore.kernel.org/all/20260311182050.3467471-1-sanjayembedded@gmail.com/
> Changes in v2:
> - split series to individual patch
> - address review comment from David Lechner and reuse exising buffer instead of allocation
> - Link to v1 https://lore.kernel.org/all/20260310200513.2162018-4-sanjayembedded@gmail.com/
> ---
> drivers/iio/common/st_sensors/st_sensors_core.c | 11 +++--------
> 1 file changed, 3 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/iio/common/st_sensors/st_sensors_core.c b/drivers/iio/common/st_sensors/st_sensors_core.c
> index dac593be5695..dbc5e16fbde4 100644
> --- a/drivers/iio/common/st_sensors/st_sensors_core.c
> +++ b/drivers/iio/common/st_sensors/st_sensors_core.c
> @@ -501,14 +501,12 @@ static int st_sensors_read_axis_data(struct iio_dev *indio_dev,
>
> byte_for_channel = DIV_ROUND_UP(ch->scan_type.realbits +
> ch->scan_type.shift, 8);
> - outdata = kmalloc(byte_for_channel, GFP_DMA | GFP_KERNEL);
> - if (!outdata)
> - return -ENOMEM;
> + outdata = sdata->buffer_data;
>
> err = regmap_bulk_read(sdata->regmap, ch->address,
> outdata, byte_for_channel);
> if (err < 0)
> - goto st_sensors_free_memory;
> + return err;
>
> if (byte_for_channel == 1)
> *data = (s8)*outdata;
> @@ -517,10 +515,7 @@ static int st_sensors_read_axis_data(struct iio_dev *indio_dev,
> else if (byte_for_channel == 3)
> *data = (s32)sign_extend32(get_unaligned_le24(outdata), 23);
>
> -st_sensors_free_memory:
> - kfree(outdata);
> -
> - return err;
> + return 0;
> }
>
> int st_sensors_read_info_raw(struct iio_dev *indio_dev,
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-03-21 20:10 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-15 12:16 [PATCH v4] iio: st_sensors: drop temporary kmalloc buffer and reuse buffer_data Sanjay Chitroda
2026-03-21 20:10 ` Jonathan Cameron
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox