Skip to contentSkip to conversation

iio: trigger: Avoid data race

PATCH In review Gyeyoung Baek 3 participants 5 messages May 28, 2025 – May 29, 2025 IIO

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 iio: trigger: Avoid data race

A data race could occur between atomic_read() and atomic_set(). Use atomic_cmpxchg_relaxed() to group them atomically.

Previously the main logic was executed when use_count was 0. Now it returns early when use_count was not 0.

---
Changelog:

v2:
- Edit commit message.
- Separate variable declaration from logic. (Andy)

---
 drivers/iio/industrialio-trigger.c | 32 +++++++++++++++---------------
 1 file changed, 16 insertions(+), 16 deletions(-)
drivers/iio/industrialio-trigger.c +16 -16
index 54416a384232..fc799910c74d 100644
@@ -196,15 +196,15 @@ void iio_trigger_poll(struct iio_trigger *trig)
 {
 	int i;
 
-	if (!atomic_read(&trig->use_count)) {
-		atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
+	if (atomic_cmpxchg_relaxed(&trig->use_count, 0,
+				   CONFIG_IIO_CONSUMERS_PER_TRIGGER))
+		return;
 
-		for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
-			if (trig->subirqs[i].enabled)
-				generic_handle_irq(trig->subirq_base + i);
-			else
-				iio_trigger_notify_done_atomic(trig);
-		}
+	for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
+		if (trig->subirqs[i].enabled)
+			generic_handle_irq(trig->subirq_base + i);
+		else
+			iio_trigger_notify_done_atomic(trig);
 	}
 }
 EXPORT_SYMBOL(iio_trigger_poll);
@@ -227,15 +227,15 @@ void iio_trigger_poll_nested(struct iio_trigger *trig)
 {
 	int i;
 
-	if (!atomic_read(&trig->use_count)) {
-		atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
+	if (atomic_cmpxchg_relaxed(&trig->use_count, 0,
+				   CONFIG_IIO_CONSUMERS_PER_TRIGGER))
+		return;
 
-		for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
-			if (trig->subirqs[i].enabled)
-				handle_nested_irq(trig->subirq_base + i);
-			else
-				iio_trigger_notify_done(trig);
-		}
+	for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
+		if (trig->subirqs[i].enabled)
+			handle_nested_irq(trig->subirq_base + i);
+		else
+			iio_trigger_notify_done(trig);
 	}
 }
 EXPORT_SYMBOL(iio_trigger_poll_nested);
Signature · 1 line
2.43.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 6 lines · Gyeyoung Baek

A data race could occur between atomic_read() and atomic_set(). Use atomic_cmpxchg_relaxed() to group them atomically.

Previously the main logic was executed when use_count was 0. Now it returns early when use_count was not 0.

This needs Fixes tag, otherwise LGTM,

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 11 lines · Andy Shevchenko
Quoted 6 lines · Gyeyoung Baek

A data race could occur between atomic_read() and atomic_set(). Use atomic_cmpxchg_relaxed() to group them atomically.

Previously the main logic was executed when use_count was 0. Now it returns early when use_count was not 0.

This needs Fixes tag, otherwise LGTM,

Yes, then I'll send v3, thanks!

Quoted 3 lines

--
With Best Regards,
Andy Shevchenko

#3 lore raw
Quoted 8 lines · On Wed, 28 May 2025 22:27:25 +0900 Gyeyoung Baek <[email protected]> wrote:
Quoted 6 lines · Andy Shevchenko
Quoted 4 lines · Gyeyoung Baek

A data race could occur between atomic_read() and atomic_set(). Use atomic_cmpxchg_relaxed() to group them atomically.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Previously the main logic was executed when use_count was 0. Now it returns early when use_count was not 0.

This needs Fixes tag, otherwise LGTM,

Yes, then I'll send v3, thanks!

Slow down :)

Reply to v1. In general don't rush new versions out so quickly.

Quoted 5 lines
Quoted 4 lines

--
With Best Regards,
Andy Shevchenko

#4 lore raw
Quoted 7 lines · Jonathan Cameron
Quoted 3 lines · On Wed, 28 May 2025 22:27:25 +0900 Gyeyoung Baek <[email protected]> wrote:
Quoted 1 line · Andy Shevchenko
Quoted 7 lines
Quoted 4 lines
Quoted 1 line · Gyeyoung Baek

Yes, then I'll send v3, thanks!

Slow down :)

Reply to v1. In general don't rush new versions out so quickly.

Yes, I should have waited for your review... I thought the situation was pretty simple, but there were many details as you pointed out. I'll be more careful going forward, thanks.

--
Best regards,
Gyeyoung