From: Sanjay Chitroda <sanjayembeddedse@gmail.com>
To: jic23@kernel.org, dlechner@baylibre.com, nuno.sa@analog.com,
andy@kernel.org
Cc: kees@kernel.org, linux-iio@vger.kernel.org,
linux-kernel@vger.kernel.org, sanjayembeddedse@gmail.com
Subject: [PATCH v4 4/4] iio: ssp_sensors: reuse preallocated RX buffer for SPI transfers
Date: Thu, 26 Mar 2026 13:48:15 +0530 [thread overview]
Message-ID: <20260326081815.925373-5-sanjayembedded@gmail.com> (raw)
In-Reply-To: <20260326081815.925373-1-sanjayembedded@gmail.com>
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 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 | 11 +++++++++++
drivers/iio/common/ssp_sensors/ssp_spi.c | 17 +++--------------
3 files changed, 19 insertions(+), 14 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 da09c9f3ceb6..35e07132c4a1 100644
--- a/drivers/iio/common/ssp_sensors/ssp_dev.c
+++ b/drivers/iio/common/ssp_sensors/ssp_dev.c
@@ -512,6 +512,17 @@ static int ssp_probe(struct spi_device *spi)
mutex_init(&data->comm_lock);
+ 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");
+ return -ENOMEM;
+ }
+
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 61a4e978d9b2..08cf31fe9dd4 100644
--- a/drivers/iio/common/ssp_sensors/ssp_spi.c
+++ b/drivers/iio/common/ssp_sensors/ssp_spi.c
@@ -369,17 +369,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)
- return -ENOMEM;
+ 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);
@@ -411,22 +407,15 @@ int ssp_irq_msg(struct ssp_data *data)
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
next prev parent reply other threads:[~2026-03-26 8:18 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-26 8:18 [PATCH v4 0/4] iio: ssp_sensors: improve resource cleanup with cleanup.h Sanjay Chitroda
2026-03-26 8:18 ` [PATCH v4 1/4] iio: ssp_sensors: cleanup codestyle warning Sanjay Chitroda
2026-03-26 8:18 ` [PATCH v4 2/4] iio: ssp_sensors: cleanup codestyle check Sanjay Chitroda
2026-03-26 8:18 ` [PATCH v4 3/4] iio: ssp_sensors: ssp_spi: use guard() to release mutexes Sanjay Chitroda
2026-03-26 9:22 ` Andy Shevchenko
2026-03-26 8:18 ` Sanjay Chitroda [this message]
2026-03-26 9:25 ` [PATCH v4 4/4] iio: ssp_sensors: reuse preallocated RX buffer for SPI transfers Andy Shevchenko
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=20260326081815.925373-5-sanjayembedded@gmail.com \
--to=sanjayembeddedse@gmail.com \
--cc=andy@kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=kees@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nuno.sa@analog.com \
/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