Re: [PATCH v5 5/5] iio: ssp_sensors: reuse preallocated RX buffer for SPI transfers
From: Sanjay Chitroda
Date: Sat Apr 11 2026 - 08:19:52 EST
On 6 April 2026 9:37:49 pm IST, David Lechner <dlechner@xxxxxxxxxxxx> wrote:
>On 4/6/26 3:08 AM, Sanjay Chitroda wrote:
>> From: Sanjay Chitroda <sanjayembeddedse@xxxxxxxxx>
>>
>> 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.
>
>If we are going to claim this is an optimization, we should have
>some measurements to back that up.
>
Hi David,
Thank you for your inputs and feedback.
>>
>> Signed-off-by: Sanjay Chitroda <sanjayembeddedse@xxxxxxxxx>
>> ---
>> 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@xxxxxxxxx/
>> 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@xxxxxxxxx/
>> 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@xxxxxxxxx/
>> ---
>> 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;
>
>No, these can't be after _aligned(IIO_DMA_MINALIGN); without causing problems.
>
>What would work here though is:
>
> u8 rx_buf[SSP_DATA_PACKET_SIZE];
>
Okay, packet size is 960 bytes; to avoid using static memory i have used dynamic memory with devm API.
However following your input I will update change in next series with static allocation.
>> };
>>
>> 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);
>
>Since this is a fixed size, we don't need a separate alloc here. We can
>just embed the array in the data struct.
>
Yes, understood.
>> +
>> + 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;
>
>
>I don't think it is helpful to keep the buffer local variable.
>
I will keep buffer at top of ssp_irq_msg() instead of local to specific switch case.
>>
>> /* 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;
>