[PATCH 2/7] iio: buffer: iio_push_to_buffers_with_ts_unaligned() might_sleep()

David Lechner posted 7 patches 2 weeks, 6 days ago
There is a newer version of this series
[PATCH 2/7] iio: buffer: iio_push_to_buffers_with_ts_unaligned() might_sleep()
Posted by David Lechner 2 weeks, 6 days ago
Call might_sleep() in iio_push_to_buffers_with_ts_unaligned() since it
can allocate memory, which may sleep.

Suggested-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Signed-off-by: David Lechner <dlechner@baylibre.com>
---
 drivers/iio/industrialio-buffer.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
index 7da43a1f2f75f32dc93b9a5fe903378a79e82fe3..5599fa37b698dda6ff126496f62939331c12f524 100644
--- a/drivers/iio/industrialio-buffer.c
+++ b/drivers/iio/industrialio-buffer.c
@@ -2412,6 +2412,8 @@ int iio_push_to_buffers_with_ts_unaligned(struct iio_dev *indio_dev,
 {
 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
 
+	might_sleep();
+
 	/*
 	 * Conservative estimate - we can always safely copy the minimum
 	 * of either the data provided or the length of the destination buffer.

-- 
2.43.0
Re: [PATCH 2/7] iio: buffer: iio_push_to_buffers_with_ts_unaligned() might_sleep()
Posted by Andy Shevchenko 2 weeks, 6 days ago
On Fri, Sep 12, 2025 at 11:05:53AM -0500, David Lechner wrote:
> Call might_sleep() in iio_push_to_buffers_with_ts_unaligned() since it
> can allocate memory, which may sleep.

It can or does it always do?
If the first one is correct, better to use might_sleep_if().

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH 2/7] iio: buffer: iio_push_to_buffers_with_ts_unaligned() might_sleep()
Posted by David Lechner 2 weeks, 5 days ago
On 9/12/25 1:10 PM, Andy Shevchenko wrote:
> On Fri, Sep 12, 2025 at 11:05:53AM -0500, David Lechner wrote:
>> Call might_sleep() in iio_push_to_buffers_with_ts_unaligned() since it
>> can allocate memory, which may sleep.
> 
> It can or does it always do?
> If the first one is correct, better to use might_sleep_if().
> 

Just below this in the function is:

	if (iio_dev_opaque->bounce_buffer_size !=  indio_dev->scan_bytes) {
		void *bb;

		bb = devm_krealloc(&indio_dev->dev,
				   iio_dev_opaque->bounce_buffer,
				   indio_dev->scan_bytes, GFP_KERNEL);
		if (!bb)
			return -ENOMEM;
		iio_dev_opaque->bounce_buffer = bb;
		iio_dev_opaque->bounce_buffer_size = indio_dev->scan_bytes;
	}


Would it make sense to move the might_sleep() inside of this
if statement rather than repeat the condition in might_sleep_if()?

devm_krealloc() is the only part of this function that might sleep.
Re: [PATCH 2/7] iio: buffer: iio_push_to_buffers_with_ts_unaligned() might_sleep()
Posted by Jonathan Cameron 2 weeks, 5 days ago
On Fri, 12 Sep 2025 13:40:37 -0500
David Lechner <dlechner@baylibre.com> wrote:

> On 9/12/25 1:10 PM, Andy Shevchenko wrote:
> > On Fri, Sep 12, 2025 at 11:05:53AM -0500, David Lechner wrote:  
> >> Call might_sleep() in iio_push_to_buffers_with_ts_unaligned() since it
> >> can allocate memory, which may sleep.  
> > 
> > It can or does it always do?
> > If the first one is correct, better to use might_sleep_if().
> >   
> 
> Just below this in the function is:
> 
> 	if (iio_dev_opaque->bounce_buffer_size !=  indio_dev->scan_bytes) {
> 		void *bb;
> 
> 		bb = devm_krealloc(&indio_dev->dev,
> 				   iio_dev_opaque->bounce_buffer,
> 				   indio_dev->scan_bytes, GFP_KERNEL);
> 		if (!bb)
> 			return -ENOMEM;
> 		iio_dev_opaque->bounce_buffer = bb;
> 		iio_dev_opaque->bounce_buffer_size = indio_dev->scan_bytes;
> 	}
> 
> 
> Would it make sense to move the might_sleep() inside of this
> if statement rather than repeat the condition in might_sleep_if()?
> 
> devm_krealloc() is the only part of this function that might sleep.
> 
Whilst true that the sleep is only at this point, we always go into
this path the first time (assuming I remember correctly how this works).

So I'd argue a might_sleep() where you have it is appropriate
as we will already have spat out the debug info if we get to the
second case that doesn't sleep.

If this ever matters to a driver, we could add a new function
to allocate the bounce buffer earlier. 

Jonathan