Skip to contentSkip to conversation

iio: buffer: serialize buffer teardown with mode claims

PATCH Applied Jinseob Kim 4 participants 4 messages Sep 21 – Sep 25 IIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: buffer: serialize buffer teardown with mode claims

Buffer-mode claims hold mlock to guarantee that the device remains in buffer mode until the claim is released. Normal buffer updates take info_exist_lock followed by mlock in iio_update_buffers().

However, iio_device_unregister() disables and deactivates all buffers without taking mlock. This can invalidate buffer state, including active_scan_mask, while a buffer-mode claim is held.

Take mlock in iio_disable_all_buffers() so that unregister honors the mode-claim lifetime guarantee. The info_exist_lock -> mlock ordering matches iio_update_buffers().

Fixes 0a8565425afd ("iio: core: introduce iio_device_{claim|release}_buffer_mode() APIs")
---
 drivers/iio/industrialio-buffer.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/iio/industrialio-buffer.c +4 -0
index 2c9ec93dff47..d60bad5492e7 100644
@@ -1377,6 +1377,10 @@ EXPORT_SYMBOL_GPL(iio_update_buffers);
 
 void iio_disable_all_buffers(struct iio_dev *indio_dev)
 {
+	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
+
+	guard(mutex)(&iio_dev_opaque->mlock);
+
 	iio_disable_buffers(indio_dev);
 	iio_buffer_deactivate_all(indio_dev);
 }

base-commit: dceba8e581e3e8f475da0efbd7a73f3d9524ddd2

Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · On Mon, 21 Sep 2026 15:24:21 +0900 Jinseob Kim <[email protected]> wrote:

Buffer-mode claims hold mlock to guarantee that the device remains in buffer mode until the claim is released. Normal buffer updates take info_exist_lock followed by mlock in iio_update_buffers().

However, iio_device_unregister() disables and deactivates all buffers without taking mlock. This can invalidate buffer state, including active_scan_mask, while a buffer-mode claim is held.

Quoted 8 lines

Take mlock in iio_disable_all_buffers() so that unregister honors the mode-claim lifetime guarantee. The info_exist_lock -> mlock ordering matches iio_update_buffers().

Fixes 0a8565425afd ("iio: core: introduce iio_device_{claim|release}_buffer_mode() APIs")

---

Signature · 2 lines
Kind regards,
Joshua Crofts
reviewer Reviewed-by on v1 1/1
#2 lore raw
In reply to Jinseob Kim
Quoted 8 lines · Jinseob Kim

Buffer-mode claims hold mlock to guarantee that the device remains in buffer mode until the claim is released. Normal buffer updates take info_exist_lock followed by mlock in iio_update_buffers().

However, iio_device_unregister() disables and deactivates all buffers without taking mlock. This can invalidate buffer state, including active_scan_mask, while a buffer-mode claim is held.

Quoted 8 lines

Take mlock in iio_disable_all_buffers() so that unregister honors the mode-claim lifetime guarantee. The info_exist_lock -> mlock ordering matches iio_update_buffers().

Fixes 0a8565425afd ("iio: core: introduce iio_device_{claim|release}_buffer_mode() APIs")

---

Quoted 23 lines
 drivers/iio/industrialio-buffer.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/iio/industrialio-buffer.c +4 -0
index 2c9ec93dff47..d60bad5492e7 100644
@@ -1377,6 +1377,10 @@ EXPORT_SYMBOL_GPL(iio_update_buffers);
 
 void iio_disable_all_buffers(struct iio_dev *indio_dev)
 {
+	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
+
+	guard(mutex)(&iio_dev_opaque->mlock);
+
 	iio_disable_buffers(indio_dev);
 	iio_buffer_deactivate_all(indio_dev);
 }

base-commit: dceba8e581e3e8f475da0efbd7a73f3d9524ddd2

Signature · 1 line
2.43.0
maintainer Applied
#3 lore raw
Quoted 13 lines · On Thu, 24 Sep 2026 09:24:33 +0100 Nuno Sá <[email protected]> wrote:
Quoted 12 lines · Jinseob Kim

Buffer-mode claims hold mlock to guarantee that the device remains in buffer mode until the claim is released. Normal buffer updates take info_exist_lock followed by mlock in iio_update_buffers().

However, iio_device_unregister() disables and deactivates all buffers without taking mlock. This can invalidate buffer state, including active_scan_mask, while a buffer-mode claim is held.

Take mlock in iio_disable_all_buffers() so that unregister honors the mode-claim lifetime guarantee. The info_exist_lock -> mlock ordering matches iio_update_buffers().

Quoted 6 lines
Quoted 4 lines
Fixes 0a8565425afd ("iio: core: introduce iio_device_{claim|release}_buffer_mode() APIs")

---

Applied to the fixes-togreg branch of iio.git and marked for stable inclusion. It should be safe enough but I do want to get some testing on this. Crossed fingers!

Thanks,

Jonathan

Quoted 24 lines
Quoted 24 lines
 drivers/iio/industrialio-buffer.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/iio/industrialio-buffer.c +4 -0
index 2c9ec93dff47..d60bad5492e7 100644
@@ -1377,6 +1377,10 @@ EXPORT_SYMBOL_GPL(iio_update_buffers);
 
 void iio_disable_all_buffers(struct iio_dev *indio_dev)
 {
+	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
+
+	guard(mutex)(&iio_dev_opaque->mlock);
+
 	iio_disable_buffers(indio_dev);
 	iio_buffer_deactivate_all(indio_dev);
 }

base-commit: dceba8e581e3e8f475da0efbd7a73f3d9524ddd2

Signature · 1 line
2.43.0