mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/7] iio: buffer: document calling context when pushing to buffers
@ 2025-09-16 21:02 David Lechner
  2025-09-16 21:02 ` [PATCH v2 1/7] iio: buffer: document iio_push_to_buffers_with_ts_unaligned() may sleep David Lechner
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: David Lechner @ 2025-09-16 21:02 UTC (permalink / raw)
  To: Jonathan Cameron, Nuno Sá, Andy Shevchenko
  Cc: linux-iio, linux-kernel, Daniel Lezcano, Andy Shevchenko, David Lechner

It came up in a recent discussion [1] that we need to be careful about
the calling context for various iio_push_to_buffer*() functions. Here is
a series that adds some documentation in a number of places to make this
a bit more visible.

[1]: https://lore.kernel.org/linux-iio/CAHp75Vc8u2N2AHWtnPRmRXWKN3u8Qi=yvx5afbFh4NLNb8-y9A@mail.gmail.com/

---
Changes in v2:
- Picked up a review tag (PATCH 1)
- Clarified comment about scan buffer size (PATCH 3)
- Use DEPRECATED to signal function deprecation (PATCH 4)
- Link to v1: https://lore.kernel.org/r/20250912-iio-doc-push-to-buffers-context-v1-0-c4aad013d96e@baylibre.com

---
David Lechner (7):
      iio: buffer: document iio_push_to_buffers_with_ts_unaligned() may sleep
      iio: buffer: iio_push_to_buffers_with_ts_unaligned() might_sleep()
      iio: buffer: document iio_push_to_buffers_with_ts()
      iio: buffer: deprecated iio_push_to_buffers_with_timestamp()
      iio: buffer: document iio_push_to_buffers() calling context
      iio: buffer: document store_to() callback may be called in any context
      iio: buffer: document that buffer callback must be context safe

 drivers/iio/buffer/industrialio-buffer-cb.c |  1 +
 drivers/iio/industrialio-buffer.c           |  8 ++++++++
 include/linux/iio/buffer.h                  | 22 +++++++++++++++++-----
 include/linux/iio/buffer_impl.h             |  3 ++-
 include/linux/iio/consumer.h                |  3 ++-
 5 files changed, 30 insertions(+), 7 deletions(-)
---
base-commit: 671b9b6d7f4fe17a174c410397e72253877ca64e
change-id: 20250912-iio-doc-push-to-buffers-context-df3ca98fe0df

Best regards,
-- 
David Lechner <dlechner@baylibre.com>


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 1/7] iio: buffer: document iio_push_to_buffers_with_ts_unaligned() may sleep
  2025-09-16 21:02 [PATCH v2 0/7] iio: buffer: document calling context when pushing to buffers David Lechner
@ 2025-09-16 21:02 ` David Lechner
  2025-09-16 21:02 ` [PATCH v2 2/7] iio: buffer: iio_push_to_buffers_with_ts_unaligned() might_sleep() David Lechner
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: David Lechner @ 2025-09-16 21:02 UTC (permalink / raw)
  To: Jonathan Cameron, Nuno Sá, Andy Shevchenko
  Cc: linux-iio, linux-kernel, Daniel Lezcano, Andy Shevchenko, David Lechner

Add Context: documentation comment that
iio_push_to_buffers_with_ts_unaligned() may sleep because it calls
devm_krealloc().

Also document Return: value while here.

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

diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
index a80f7cc25a27109cfe22ce447c4117fc94bc8310..7da43a1f2f75f32dc93b9a5fe903378a79e82fe3 100644
--- a/drivers/iio/industrialio-buffer.c
+++ b/drivers/iio/industrialio-buffer.c
@@ -2401,6 +2401,9 @@ EXPORT_SYMBOL_GPL(iio_push_to_buffers);
  * not require space for the timestamp, or 8 byte alignment of data.
  * It does however require an allocation on first call and additional
  * copies on all calls, so should be avoided if possible.
+ *
+ * Context: May sleep.
+ * Return: 0 on success, negative error code on failure.
  */
 int iio_push_to_buffers_with_ts_unaligned(struct iio_dev *indio_dev,
 					  const void *data,

-- 
2.43.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 2/7] iio: buffer: iio_push_to_buffers_with_ts_unaligned() might_sleep()
  2025-09-16 21:02 [PATCH v2 0/7] iio: buffer: document calling context when pushing to buffers David Lechner
  2025-09-16 21:02 ` [PATCH v2 1/7] iio: buffer: document iio_push_to_buffers_with_ts_unaligned() may sleep David Lechner
@ 2025-09-16 21:02 ` David Lechner
  2025-09-16 21:02 ` [PATCH v2 3/7] iio: buffer: document iio_push_to_buffers_with_ts() David Lechner
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: David Lechner @ 2025-09-16 21:02 UTC (permalink / raw)
  To: Jonathan Cameron, Nuno Sá, Andy Shevchenko
  Cc: linux-iio, linux-kernel, Daniel Lezcano, Andy Shevchenko, David Lechner

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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 3/7] iio: buffer: document iio_push_to_buffers_with_ts()
  2025-09-16 21:02 [PATCH v2 0/7] iio: buffer: document calling context when pushing to buffers David Lechner
  2025-09-16 21:02 ` [PATCH v2 1/7] iio: buffer: document iio_push_to_buffers_with_ts_unaligned() may sleep David Lechner
  2025-09-16 21:02 ` [PATCH v2 2/7] iio: buffer: iio_push_to_buffers_with_ts_unaligned() might_sleep() David Lechner
@ 2025-09-16 21:02 ` David Lechner
  2025-09-16 21:02 ` [PATCH v2 4/7] iio: buffer: deprecated iio_push_to_buffers_with_timestamp() David Lechner
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: David Lechner @ 2025-09-16 21:02 UTC (permalink / raw)
  To: Jonathan Cameron, Nuno Sá, Andy Shevchenko
  Cc: linux-iio, linux-kernel, Daniel Lezcano, David Lechner

Document the iio_push_to_buffers_with_ts() function.

This is copied and slightly cleaned up from
iio_push_to_buffers_with_timestamp().

Signed-off-by: David Lechner <dlechner@baylibre.com>
---
 include/linux/iio/buffer.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/include/linux/iio/buffer.h b/include/linux/iio/buffer.h
index 5c84ec4a981001dfba3b369f811368bab70a3117..e46b818981aaf473539de5adf4aafa7e7d675de6 100644
--- a/include/linux/iio/buffer.h
+++ b/include/linux/iio/buffer.h
@@ -45,6 +45,22 @@ static inline int iio_push_to_buffers_with_timestamp(struct iio_dev *indio_dev,
 	return iio_push_to_buffers(indio_dev, data);
 }
 
+/**
+ * iio_push_to_buffers_with_ts() - push data and timestamp to buffers
+ * @indio_dev:		iio_dev structure for device.
+ * @data:		Pointer to sample data buffer.
+ * @data_total_len:	The size of @data in bytes.
+ * @timestamp:		Timestamp for the sample data.
+ *
+ * Pushes data to the IIO device's buffers. If timestamps are enabled for the
+ * device the function will store the supplied timestamp as the last element in
+ * the sample data buffer before pushing it to the device buffers. The sample
+ * data buffer needs to be large enough to hold the additional timestamp
+ * (usually the buffer should be at least indio->scan_bytes bytes large).
+ *
+ * Context: Any context.
+ * Return: 0 on success, a negative error code otherwise.
+ */
 static inline int iio_push_to_buffers_with_ts(struct iio_dev *indio_dev,
 					      void *data, size_t data_total_len,
 					      s64 timestamp)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 4/7] iio: buffer: deprecated iio_push_to_buffers_with_timestamp()
  2025-09-16 21:02 [PATCH v2 0/7] iio: buffer: document calling context when pushing to buffers David Lechner
                   ` (2 preceding siblings ...)
  2025-09-16 21:02 ` [PATCH v2 3/7] iio: buffer: document iio_push_to_buffers_with_ts() David Lechner
@ 2025-09-16 21:02 ` David Lechner
  2025-09-16 21:02 ` [PATCH v2 5/7] iio: buffer: document iio_push_to_buffers() calling context David Lechner
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: David Lechner @ 2025-09-16 21:02 UTC (permalink / raw)
  To: Jonathan Cameron, Nuno Sá, Andy Shevchenko
  Cc: linux-iio, linux-kernel, Daniel Lezcano, David Lechner

Replace the documentation of iio_push_to_buffers_with_timestamp() with
a deprecation notice pointing to the preferred alternative.

Signed-off-by: David Lechner <dlechner@baylibre.com>
---
 include/linux/iio/buffer.h | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/include/linux/iio/buffer.h b/include/linux/iio/buffer.h
index e46b818981aaf473539de5adf4aafa7e7d675de6..d37f82678f716dabfd4eb3b87cd7bb00f146046d 100644
--- a/include/linux/iio/buffer.h
+++ b/include/linux/iio/buffer.h
@@ -26,11 +26,7 @@ int iio_pop_from_buffer(struct iio_buffer *buffer, void *data);
  * @data:		sample data
  * @timestamp:		timestamp for the sample data
  *
- * Pushes data to the IIO device's buffers. If timestamps are enabled for the
- * device the function will store the supplied timestamp as the last element in
- * the sample data buffer before pushing it to the device buffers. The sample
- * data buffer needs to be large enough to hold the additional timestamp
- * (usually the buffer should be indio->scan_bytes bytes large).
+ * DEPRECATED: Use iio_push_to_buffers_with_ts() instead.
  *
  * Returns 0 on success, a negative error code otherwise.
  */

-- 
2.43.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 5/7] iio: buffer: document iio_push_to_buffers() calling context
  2025-09-16 21:02 [PATCH v2 0/7] iio: buffer: document calling context when pushing to buffers David Lechner
                   ` (3 preceding siblings ...)
  2025-09-16 21:02 ` [PATCH v2 4/7] iio: buffer: deprecated iio_push_to_buffers_with_timestamp() David Lechner
@ 2025-09-16 21:02 ` David Lechner
  2025-09-16 21:02 ` [PATCH v2 6/7] iio: buffer: document store_to() callback may be called in any context David Lechner
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: David Lechner @ 2025-09-16 21:02 UTC (permalink / raw)
  To: Jonathan Cameron, Nuno Sá, Andy Shevchenko
  Cc: linux-iio, linux-kernel, Daniel Lezcano, David Lechner

Document that iio_push_to_buffers() can be called from any context.

Also document the Return: value while here.

Signed-off-by: David Lechner <dlechner@baylibre.com>
---
 drivers/iio/industrialio-buffer.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
index 5599fa37b698dda6ff126496f62939331c12f524..f1448ae1b843fc577599fc1b9cf6d859bba226f1 100644
--- a/drivers/iio/industrialio-buffer.c
+++ b/drivers/iio/industrialio-buffer.c
@@ -2372,6 +2372,9 @@ static int iio_push_to_buffer(struct iio_buffer *buffer, const void *data)
  * iio_push_to_buffers() - push to a registered buffer.
  * @indio_dev:		iio_dev structure for device.
  * @data:		Full scan.
+ *
+ * Context: Any context.
+ * Return: 0 on success, negative error code on failure.
  */
 int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)
 {

-- 
2.43.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 6/7] iio: buffer: document store_to() callback may be called in any context
  2025-09-16 21:02 [PATCH v2 0/7] iio: buffer: document calling context when pushing to buffers David Lechner
                   ` (4 preceding siblings ...)
  2025-09-16 21:02 ` [PATCH v2 5/7] iio: buffer: document iio_push_to_buffers() calling context David Lechner
@ 2025-09-16 21:02 ` David Lechner
  2025-09-16 21:02 ` [PATCH v2 7/7] iio: buffer: document that buffer callback must be context safe David Lechner
  2025-09-20 11:59 ` [PATCH v2 0/7] iio: buffer: document calling context when pushing to buffers Jonathan Cameron
  7 siblings, 0 replies; 9+ messages in thread
From: David Lechner @ 2025-09-16 21:02 UTC (permalink / raw)
  To: Jonathan Cameron, Nuno Sá, Andy Shevchenko
  Cc: linux-iio, linux-kernel, Daniel Lezcano, David Lechner

Document that the struct iio_buffer_access_funcs.store_to() callback
must be safe to call from any context since it is called from
iio_push_to_buffer() which may be called from any context.

Signed-off-by: David Lechner <dlechner@baylibre.com>
---
 include/linux/iio/buffer_impl.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/include/linux/iio/buffer_impl.h b/include/linux/iio/buffer_impl.h
index e72552e026f3a3c15fca04d26aaa91dbd27e17bc..0daff9ff20ce49de67fa0f2ac6191882de2f4a67 100644
--- a/include/linux/iio/buffer_impl.h
+++ b/include/linux/iio/buffer_impl.h
@@ -24,7 +24,8 @@ struct sg_table;
 
 /**
  * struct iio_buffer_access_funcs - access functions for buffers.
- * @store_to:		actually store stuff to the buffer
+ * @store_to:		actually store stuff to the buffer - must be safe to
+ *			call from any context (e.g. must not sleep).
  * @read:		try to get a specified number of bytes (must exist)
  * @data_available:	indicates how much data is available for reading from
  *			the buffer.

-- 
2.43.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 7/7] iio: buffer: document that buffer callback must be context safe
  2025-09-16 21:02 [PATCH v2 0/7] iio: buffer: document calling context when pushing to buffers David Lechner
                   ` (5 preceding siblings ...)
  2025-09-16 21:02 ` [PATCH v2 6/7] iio: buffer: document store_to() callback may be called in any context David Lechner
@ 2025-09-16 21:02 ` David Lechner
  2025-09-20 11:59 ` [PATCH v2 0/7] iio: buffer: document calling context when pushing to buffers Jonathan Cameron
  7 siblings, 0 replies; 9+ messages in thread
From: David Lechner @ 2025-09-16 21:02 UTC (permalink / raw)
  To: Jonathan Cameron, Nuno Sá, Andy Shevchenko
  Cc: linux-iio, linux-kernel, Daniel Lezcano, David Lechner

Document that the callback registered with iio_channel_get_all_cb()
must be safe to call from any context since it is called from by
iio_push_to_buffer() which can be called in any context.

Signed-off-by: David Lechner <dlechner@baylibre.com>
---
 drivers/iio/buffer/industrialio-buffer-cb.c | 1 +
 include/linux/iio/consumer.h                | 3 ++-
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/buffer/industrialio-buffer-cb.c b/drivers/iio/buffer/industrialio-buffer-cb.c
index 3e27385069edb5b717a4c56e72dff2c0a238fa07..f4ebff9684936365ec179489d9041d8bdbe58738 100644
--- a/drivers/iio/buffer/industrialio-buffer-cb.c
+++ b/drivers/iio/buffer/industrialio-buffer-cb.c
@@ -13,6 +13,7 @@
 
 struct iio_cb_buffer {
 	struct iio_buffer buffer;
+	/* Must be safe to call from any context (e.g. must not sleep). */
 	int (*cb)(const void *data, void *private);
 	void *private;
 	struct iio_channel *channels;
diff --git a/include/linux/iio/consumer.h b/include/linux/iio/consumer.h
index a38b277c2c02cb85a766d645a046bdfa534db51a..5039558267e4c17b2e09246036d0df352e242d06 100644
--- a/include/linux/iio/consumer.h
+++ b/include/linux/iio/consumer.h
@@ -131,7 +131,8 @@ struct iio_cb_buffer;
 /**
  * iio_channel_get_all_cb() - register callback for triggered capture
  * @dev:		Pointer to client device.
- * @cb:			Callback function.
+ * @cb:			Callback function. Must be safe to call from any context
+ *			(e.g. must not sleep).
  * @private:		Private data passed to callback.
  *
  * NB right now we have no ability to mux data from multiple devices.

-- 
2.43.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 0/7] iio: buffer: document calling context when pushing to buffers
  2025-09-16 21:02 [PATCH v2 0/7] iio: buffer: document calling context when pushing to buffers David Lechner
                   ` (6 preceding siblings ...)
  2025-09-16 21:02 ` [PATCH v2 7/7] iio: buffer: document that buffer callback must be context safe David Lechner
@ 2025-09-20 11:59 ` Jonathan Cameron
  7 siblings, 0 replies; 9+ messages in thread
From: Jonathan Cameron @ 2025-09-20 11:59 UTC (permalink / raw)
  To: David Lechner
  Cc: Nuno Sá,
	Andy Shevchenko, linux-iio, linux-kernel, Daniel Lezcano,
	Andy Shevchenko

On Tue, 16 Sep 2025 16:02:50 -0500
David Lechner <dlechner@baylibre.com> wrote:

> It came up in a recent discussion [1] that we need to be careful about
> the calling context for various iio_push_to_buffer*() functions. Here is
> a series that adds some documentation in a number of places to make this
> a bit more visible.
> 
> [1]: https://lore.kernel.org/linux-iio/CAHp75Vc8u2N2AHWtnPRmRXWKN3u8Qi=yvx5afbFh4NLNb8-y9A@mail.gmail.com/
> 
Great, thanks for doing this.

Applied to the testing branch of iio.git

Jonathan

> ---
> Changes in v2:
> - Picked up a review tag (PATCH 1)
> - Clarified comment about scan buffer size (PATCH 3)
> - Use DEPRECATED to signal function deprecation (PATCH 4)
> - Link to v1: https://lore.kernel.org/r/20250912-iio-doc-push-to-buffers-context-v1-0-c4aad013d96e@baylibre.com
> 
> ---
> David Lechner (7):
>       iio: buffer: document iio_push_to_buffers_with_ts_unaligned() may sleep
>       iio: buffer: iio_push_to_buffers_with_ts_unaligned() might_sleep()
>       iio: buffer: document iio_push_to_buffers_with_ts()
>       iio: buffer: deprecated iio_push_to_buffers_with_timestamp()
>       iio: buffer: document iio_push_to_buffers() calling context
>       iio: buffer: document store_to() callback may be called in any context
>       iio: buffer: document that buffer callback must be context safe
> 
>  drivers/iio/buffer/industrialio-buffer-cb.c |  1 +
>  drivers/iio/industrialio-buffer.c           |  8 ++++++++
>  include/linux/iio/buffer.h                  | 22 +++++++++++++++++-----
>  include/linux/iio/buffer_impl.h             |  3 ++-
>  include/linux/iio/consumer.h                |  3 ++-
>  5 files changed, 30 insertions(+), 7 deletions(-)
> ---
> base-commit: 671b9b6d7f4fe17a174c410397e72253877ca64e
> change-id: 20250912-iio-doc-push-to-buffers-context-df3ca98fe0df
> 
> Best regards,


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2025-09-20 11:59 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-09-16 21:02 [PATCH v2 0/7] iio: buffer: document calling context when pushing to buffers David Lechner
2025-09-16 21:02 ` [PATCH v2 1/7] iio: buffer: document iio_push_to_buffers_with_ts_unaligned() may sleep David Lechner
2025-09-16 21:02 ` [PATCH v2 2/7] iio: buffer: iio_push_to_buffers_with_ts_unaligned() might_sleep() David Lechner
2025-09-16 21:02 ` [PATCH v2 3/7] iio: buffer: document iio_push_to_buffers_with_ts() David Lechner
2025-09-16 21:02 ` [PATCH v2 4/7] iio: buffer: deprecated iio_push_to_buffers_with_timestamp() David Lechner
2025-09-16 21:02 ` [PATCH v2 5/7] iio: buffer: document iio_push_to_buffers() calling context David Lechner
2025-09-16 21:02 ` [PATCH v2 6/7] iio: buffer: document store_to() callback may be called in any context David Lechner
2025-09-16 21:02 ` [PATCH v2 7/7] iio: buffer: document that buffer callback must be context safe David Lechner
2025-09-20 11:59 ` [PATCH v2 0/7] iio: buffer: document calling context when pushing to buffers Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®