mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Lechner <dlechner@baylibre.com>
To: Sanjay Chitroda <sanjayembeddedse@gmail.com>,
	jic23@kernel.org, nuno.sa@analog.com, andy@kernel.org
Cc: kees@kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v5 5/5] iio: ssp_sensors: reuse preallocated RX buffer for SPI transfers
Date: Mon, 6 Apr 2026 11:07:49 -0500	[thread overview]
Message-ID: <8c5dfc4c-dff9-46a4-adcc-dbca54f0c125@baylibre.com> (raw)
In-Reply-To: <20260406080852.2727453-6-sanjayembedded@gmail.com>

On 4/6/26 3:08 AM, Sanjay Chitroda wrote:
> 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.

If we are going to claim this is an optimization, we should have
some measurements to back that up.

> 
> 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;

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];

>  };
>  
>  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.

> +
> +	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.

>  
>  			/* 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;


  reply	other threads:[~2026-04-06 16:07 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-06  8:08 [PATCH v5 0/5] iio: ssp_sensors: improve resource cleanup Sanjay Chitroda
2026-04-06  8:08 ` [PATCH v5 1/5] iio: ssp_sensors: cleanup codestyle warning Sanjay Chitroda
2026-04-06 18:33   ` Andy Shevchenko
2026-04-07 17:42     ` Sanjay Chitroda
2026-04-07 19:32       ` Andy Shevchenko
2026-04-06  8:08 ` [PATCH v5 2/5] iio: ssp_sensors: cleanup codestyle check Sanjay Chitroda
2026-04-06  8:08 ` [PATCH v5 3/5] iio: ssp_sensors: factor out pending list add/remove helpers Sanjay Chitroda
2026-04-06 20:04   ` Andy Shevchenko
2026-04-08  8:22     ` Sanjay Chitroda
     [not found]     ` <FA511CDD-1F98-4E66-BCE8-5156DBAF4359@gmail.com>
2026-04-08 11:36       ` Andy Shevchenko
2026-04-06  8:08 ` [PATCH v5 4/5] iio: ssp_sensors: use devm APIs for mutex and IRQ resources Sanjay Chitroda
2026-04-06 16:14   ` David Lechner
2026-04-11 11:35     ` Sanjay Chitroda
2026-04-11 18:31       ` David Lechner
2026-04-12 16:45         ` Sanjay Chitroda
2026-04-07 12:42   ` Andy Shevchenko
2026-04-11 11:41     ` Sanjay Chitroda
2026-04-06  8:08 ` [PATCH v5 5/5] iio: ssp_sensors: reuse preallocated RX buffer for SPI transfers Sanjay Chitroda
2026-04-06 16:07   ` David Lechner [this message]
2026-04-11 11:57     ` Sanjay Chitroda
2026-04-11 12:17     ` Sanjay Chitroda

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=8c5dfc4c-dff9-46a4-adcc-dbca54f0c125@baylibre.com \
    --to=dlechner@baylibre.com \
    --cc=andy@kernel.org \
    --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 \
    --cc=sanjayembeddedse@gmail.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

Powered by JetHome