[PATCH v2] iio: proximity: srf08: use stack allocated scan buffer

David Lechner posted 1 patch 2 months, 2 weeks ago
drivers/iio/proximity/srf08.c | 18 +++++++-----------
1 file changed, 7 insertions(+), 11 deletions(-)
[PATCH v2] iio: proximity: srf08: use stack allocated scan buffer
Posted by David Lechner 2 months, 2 weeks ago
Use a stack allocated scan struct in srf08_trigger_handler(). Since the
scan buffer isn't used outside of this function and doesn't need to be
DMA-safe, it doesn't need to be in struct srf08_data. We can also
eliminate an extra local variable for the return value of
srf08_read_ranging() by using scan.chan directly.

Reviewed-by: Andreas Klinger <ak@it-klinger.de>
Signed-off-by: David Lechner <dlechner@baylibre.com>
---
Changes in v2:
- Zero-init the scan struct to avoid leaking uninitialized stack to userspace.
- Link to v1: https://lore.kernel.org/r/20250711-iio-use-more-iio_declare_buffer_with_ts-6-v1-1-25c70b990d6c@baylibre.com
---
 drivers/iio/proximity/srf08.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)

diff --git a/drivers/iio/proximity/srf08.c b/drivers/iio/proximity/srf08.c
index 6e32fdfd161b93a5624f757d5b7de579415b1055..d7e4cc48cfbf700c7828235de99a66324767316a 100644
--- a/drivers/iio/proximity/srf08.c
+++ b/drivers/iio/proximity/srf08.c
@@ -63,12 +63,6 @@ struct srf08_data {
 	int			range_mm;
 	struct mutex		lock;
 
-	/* Ensure timestamp is naturally aligned */
-	struct {
-		s16 chan;
-		aligned_s64 timestamp;
-	} scan;
-
 	/* Sensor-Type */
 	enum srf08_sensor_type	sensor_type;
 
@@ -182,16 +176,18 @@ static irqreturn_t srf08_trigger_handler(int irq, void *p)
 	struct iio_poll_func *pf = p;
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct srf08_data *data = iio_priv(indio_dev);
-	s16 sensor_data;
+	struct {
+		s16 chan;
+		aligned_s64 timestamp;
+	} scan = { };
 
-	sensor_data = srf08_read_ranging(data);
-	if (sensor_data < 0)
+	scan.chan = srf08_read_ranging(data);
+	if (scan.chan < 0)
 		goto err;
 
 	mutex_lock(&data->lock);
 
-	data->scan.chan = sensor_data;
-	iio_push_to_buffers_with_ts(indio_dev, &data->scan, sizeof(data->scan),
+	iio_push_to_buffers_with_ts(indio_dev, &scan, sizeof(scan),
 				    pf->timestamp);
 
 	mutex_unlock(&data->lock);

---
base-commit: cd2731444ee4e35db76f4fb587f12d327eec5446
change-id: 20250711-iio-use-more-iio_declare_buffer_with_ts-6-6ffc8e99552d

Best regards,
-- 
David Lechner <dlechner@baylibre.com>
Re: [PATCH v2] iio: proximity: srf08: use stack allocated scan buffer
Posted by Nuno Sá 2 months, 2 weeks ago
On Mon, Jul 21, 2025 at 05:21:08PM -0500, David Lechner wrote:
> Use a stack allocated scan struct in srf08_trigger_handler(). Since the
> scan buffer isn't used outside of this function and doesn't need to be
> DMA-safe, it doesn't need to be in struct srf08_data. We can also
> eliminate an extra local variable for the return value of
> srf08_read_ranging() by using scan.chan directly.
> 
> Reviewed-by: Andreas Klinger <ak@it-klinger.de>
> Signed-off-by: David Lechner <dlechner@baylibre.com>
> ---
> Changes in v2:
> - Zero-init the scan struct to avoid leaking uninitialized stack to userspace.
> - Link to v1: https://lore.kernel.org/r/20250711-iio-use-more-iio_declare_buffer_with_ts-6-v1-1-25c70b990d6c@baylibre.com
> ---

Reviewed-by: Nuno Sá <nuno.sa@analog.com>

>  drivers/iio/proximity/srf08.c | 18 +++++++-----------
>  1 file changed, 7 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/iio/proximity/srf08.c b/drivers/iio/proximity/srf08.c
> index 6e32fdfd161b93a5624f757d5b7de579415b1055..d7e4cc48cfbf700c7828235de99a66324767316a 100644
> --- a/drivers/iio/proximity/srf08.c
> +++ b/drivers/iio/proximity/srf08.c
> @@ -63,12 +63,6 @@ struct srf08_data {
>  	int			range_mm;
>  	struct mutex		lock;
>  
> -	/* Ensure timestamp is naturally aligned */
> -	struct {
> -		s16 chan;
> -		aligned_s64 timestamp;
> -	} scan;
> -
>  	/* Sensor-Type */
>  	enum srf08_sensor_type	sensor_type;
>  
> @@ -182,16 +176,18 @@ static irqreturn_t srf08_trigger_handler(int irq, void *p)
>  	struct iio_poll_func *pf = p;
>  	struct iio_dev *indio_dev = pf->indio_dev;
>  	struct srf08_data *data = iio_priv(indio_dev);
> -	s16 sensor_data;
> +	struct {
> +		s16 chan;
> +		aligned_s64 timestamp;
> +	} scan = { };
>  
> -	sensor_data = srf08_read_ranging(data);
> -	if (sensor_data < 0)
> +	scan.chan = srf08_read_ranging(data);
> +	if (scan.chan < 0)
>  		goto err;
>  
>  	mutex_lock(&data->lock);
>  
> -	data->scan.chan = sensor_data;
> -	iio_push_to_buffers_with_ts(indio_dev, &data->scan, sizeof(data->scan),
> +	iio_push_to_buffers_with_ts(indio_dev, &scan, sizeof(scan),
>  				    pf->timestamp);
>  
>  	mutex_unlock(&data->lock);
> 
> ---
> base-commit: cd2731444ee4e35db76f4fb587f12d327eec5446
> change-id: 20250711-iio-use-more-iio_declare_buffer_with_ts-6-6ffc8e99552d
> 
> Best regards,
> -- 
> David Lechner <dlechner@baylibre.com>
> 
Re: [PATCH v2] iio: proximity: srf08: use stack allocated scan buffer
Posted by Jonathan Cameron 2 months, 1 week ago
On Tue, 22 Jul 2025 10:10:38 +0100
Nuno Sá <noname.nuno@gmail.com> wrote:

> On Mon, Jul 21, 2025 at 05:21:08PM -0500, David Lechner wrote:
> > Use a stack allocated scan struct in srf08_trigger_handler(). Since the
> > scan buffer isn't used outside of this function and doesn't need to be
> > DMA-safe, it doesn't need to be in struct srf08_data. We can also
> > eliminate an extra local variable for the return value of
> > srf08_read_ranging() by using scan.chan directly.
> > 
> > Reviewed-by: Andreas Klinger <ak@it-klinger.de>
> > Signed-off-by: David Lechner <dlechner@baylibre.com>
> > ---
> > Changes in v2:
> > - Zero-init the scan struct to avoid leaking uninitialized stack to userspace.
> > - Link to v1: https://lore.kernel.org/r/20250711-iio-use-more-iio_declare_buffer_with_ts-6-v1-1-25c70b990d6c@baylibre.com
> > ---  
> 
> Reviewed-by: Nuno Sá <nuno.sa@analog.com>
Applied to the testing branch of iio.git.

Thanks,

Jonathan