Skip to contentSkip to conversation

iio: st_sensors: drop temporary kmalloc buffer and reuse buffer_data

PATCH Superseded Sanjay Chitroda 2 participants 2 messages Mar 12 – Mar 14 IIO

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 iio: st_sensors: drop temporary kmalloc buffer and reuse buffer_data

From: Sanjay Chitroda <[email protected]>

Replace the per-call kmalloc() scratch buffer with the statically allocated 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.

---
Changes in v3:
- fix variable declartion placement to follow kernel coding style
- Link to v2 https://lore.kernel.org/all/[email protected]/
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/[email protected]/

---
 drivers/iio/common/st_sensors/st_sensors_core.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
drivers/iio/common/st_sensors/st_sensors_core.c +3 -8
index dac593be5695..dbc5e16fbde4 100644
@@ -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,
Signature · 1 line
2.34.1
reviewer Reviewed-by on v3 1/1
#1 lore raw
Quoted 3 lines · Sanjay Chitroda

From: Sanjay Chitroda <[email protected]>

Replace the per-call kmalloc() scratch buffer with the statically allocated

"statically allocated" is a bit misleading. It is just an existing buffer in the private driver data (dynamically allocated during probe).

Quoted 9 lines

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.

---