[PATCH v5 5/5] iio: ssp_sensors: reuse preallocated RX buffer for SPI transfers

Sanjay Chitroda
2026-04-06T08:09:14Z

From: Sanjay Chitroda <sanjayembeddedse@gmail.com>

Avoid allocating a temporary DMA buffer in the interrupt context when
handling hub-to-AP and AP-to-hub SPI write messages.

Preallocate RX buffer during probe and reuse it for SPI receive
operations. This removes repeated kzalloc() calls from the IRQ
path, reduces allocation overhead, and avoids potential allocation
failures under memory pressure.

The RX buffer size is tracked and allocated using devm_kzalloc(), ensuring
proper lifetime management tied to the device.

No functional change intended; this is an internal optimization and
robustness improvement.

Signed-off-by: Sanjay Chitroda <sanjayembeddedse@gmail.com>
---
Changes in v5:
- Rebase change on top of latest v5 patch series.
Changes in v4:
- Use preallocated buffer and stash a buffer that gets reused each time instead of a fresh allocation.
- Link to v3: https://lore.kernel.org/all/20260315125509.857195-3-sanjayembedded@gmail.com/
Changes in v3:
- prepare series to have all respective cleanup API support for the ssp_sensors following input from Andy Shevchenko
- Link to v2 https://lore.kernel.org/all/20260311174151.3441429-1-sanjayembedded@gmail.com/
Changes in v2:
- split series to individual patch
- address review comment from Andy Shevchenko
- Link to v1 https://lore.kernel.org/all/20260310200513.2162018-3-sanjayembedded@gmail.com/
---
drivers/iio/common/ssp_sensors/ssp.h | 5 +++++
drivers/iio/common/ssp_sensors/ssp_dev.c | 12 ++++++++++++
drivers/iio/common/ssp_sensors/ssp_spi.c | 19 +++----------------
3 files changed, 20 insertions(+), 16 deletions(-)

diff --git a/drivers/iio/common/ssp_sensors/ssp.h b/drivers/iio/common/ssp_sensors/ssp.h
index f649cdecc277..aa125fd1bed5 100644
--- a/drivers/iio/common/ssp_sensors/ssp.h
+++ b/drivers/iio/common/ssp_sensors/ssp.h
@@ -175,6 +175,8 @@ struct ssp_sensorhub_info {
* @sensor_devs: registered IIO devices table
* @enable_refcount: enable reference count for wdt (watchdog timer)
* @header_buffer: cache aligned buffer for packet header
+ * @rx_buf: buffer to receive SPI data
+ * @rx_buf_size: allocated size of rx_buf
*/
struct ssp_data {
struct spi_device *spi;
@@ -222,6 +224,9 @@ struct ssp_data {
atomic_t enable_refcount;

__le16 header_buffer[SSP_HEADER_BUFFER_SIZE / sizeof(__le16)] __aligned(IIO_DMA_MINALIGN);
+
+ u8 *rx_buf;
+ size_t rx_buf_size;
};

void ssp_clean_pending_list(struct ssp_data *data);
diff --git a/drivers/iio/common/ssp_sensors/ssp_dev.c b/drivers/iio/common/ssp_sensors/ssp_dev.c
index aab28f2a0f75..2a8d6f040ae4 100644
--- a/drivers/iio/common/ssp_sensors/ssp_dev.c
+++ b/drivers/iio/common/ssp_sensors/ssp_dev.c
@@ -516,6 +516,18 @@ static int ssp_probe(struct spi_device *spi)
goto err_setup_spi;
}

+ data->rx_buf_size = SSP_DATA_PACKET_SIZE;
+ data->rx_buf = devm_kzalloc(&spi->dev,
+ data->rx_buf_size,
+ GFP_KERNEL | GFP_DMA);
+
+ if (!data->rx_buf) {
+ dev_err(&spi->dev,
+ "Failed to allocate memory for rx_buf\n");
+ ret = -ENOMEM;
+ goto err_setup_spi;
+ }
+
for (i = 0; i < SSP_SENSOR_MAX; ++i) {
data->delay_buf[i] = SSP_DEFAULT_POLLING_DELAY;
data->batch_latency_buf[i] = 0;
diff --git a/drivers/iio/common/ssp_sensors/ssp_spi.c b/drivers/iio/common/ssp_sensors/ssp_spi.c
index 7c1780e15acf..2f7445e8b4d1 100644
--- a/drivers/iio/common/ssp_sensors/ssp_spi.c
+++ b/drivers/iio/common/ssp_sensors/ssp_spi.c
@@ -383,19 +383,13 @@ int ssp_irq_msg(struct ssp_data *data)
* but the slave should not send such ones - it is to
* check but let's handle this
*/
- buffer = kmalloc(length, GFP_KERNEL | GFP_DMA);
- if (!buffer) {
- ret = -ENOMEM;
- goto _unlock;
- }
+ buffer = data->rx_buf;

/* got dead packet so it is always an error */
ret = spi_read(data->spi, buffer, length);
if (ret >= 0)
ret = -EPROTO;

- kfree(buffer);
-
dev_err(SSP_DEV, "No match error %x\n",
msg_options);

@@ -428,22 +422,15 @@ int ssp_irq_msg(struct ssp_data *data)
mutex_unlock(&data->pending_lock);
break;
case SSP_HUB2AP_WRITE:
- buffer = kzalloc(length, GFP_KERNEL | GFP_DMA);
- if (!buffer)
- return -ENOMEM;
+ buffer = data->rx_buf;

ret = spi_read(data->spi, buffer, length);
if (ret < 0) {
dev_err(SSP_DEV, "spi read fail\n");
- kfree(buffer);
break;
}

- ret = ssp_parse_dataframe(data, buffer, length);
-
- kfree(buffer);
- break;
-
+ return ssp_parse_dataframe(data, buffer, length);
default:
dev_err(SSP_DEV, "unknown msg type\n");
return -EPROTO;
--
2.34.1