Skip to contentSkip to conversation

iio: proximity: rfd77402: Fix completion race condition in IRQ mode

PATCH Applied Felix Gu 3 participants 4 messages Mar 4 – Mar 14 IIO

lore
Share & tools
lore

Felix Gu

author
#0 lore raw

PATCH 1/1 iio: proximity: rfd77402: Fix completion race condition in IRQ mode

In IRQ mode, the completion was being reinitialized after the measurement command had already been sent to the hardware. This created a race condition where the IRQ handler could call complete() before reinit_completion() was executed. Consequently, wait_for_completion_timeout() would fail to see the signal and wait until it timed out.

Move reinit_completion() to occur before the measurement command is triggered to ensure the synchronization primitive is ready to capture the interrupt.

Fixes dc81be96a73a ("iio: proximity: rfd77402: Add interrupt handling support")
---
 drivers/iio/proximity/rfd77402.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/iio/proximity/rfd77402.c +4 -3
index 6afdbfca3e5a..81b8daf17a54 100644
@@ -173,10 +173,8 @@ static int rfd77402_wait_for_result(struct rfd77402_data *data)
 	struct i2c_client *client = data->client;
 	int val, ret;
 
-	if (data->irq_en) {
-		reinit_completion(&data->completion);
+	if (data->irq_en)
 		return rfd77402_wait_for_irq(data);
-	}
 
 	/*
 	 * As per RFD77402 datasheet section '3.1.1 Single Measure', the
@@ -204,6 +202,9 @@ static int rfd77402_measure(struct rfd77402_data *data)
 	if (ret < 0)
 		return ret;
 
+	if (data->irq_en)
+		reinit_completion(&data->completion);
+
 	ret = i2c_smbus_write_byte_data(client, RFD77402_CMD_R,
 					RFD77402_CMD_SINGLE |
 					RFD77402_CMD_VALID);

---
base-commit: c025f6cf4209e1542ec2afebe49f42bbaf1a5c7b
change-id: 20260304-77402-ec479de1b9f6

Best regards,

Signature · 1 line
Felix Gu <[email protected]>
maintainer
#1 lore raw
Quoted 7 lines · On Wed, 04 Mar 2026 22:14:32 +0800 Felix Gu <[email protected]> wrote:

In IRQ mode, the completion was being reinitialized after the measurement command had already been sent to the hardware. This created a race condition where the IRQ handler could call complete() before reinit_completion() was executed. Consequently, wait_for_completion_timeout() would fail to see the signal and wait until it timed out.

Quoted 6 lines

Move reinit_completion() to occur before the measurement command is triggered to ensure the synchronization primitive is ready to capture the interrupt.

Fixes dc81be96a73a ("iio: proximity: rfd77402: Add interrupt handling support")

Looks good to me. Shrikant, you know this driver better than I do so if you can take a look as well that would be great.

Thanks,

J

Quoted 36 lines
---
 drivers/iio/proximity/rfd77402.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/iio/proximity/rfd77402.c +4 -4
index 6afdbfca3e5a..81b8daf17a54 100644
@@ -173,10 +173,8 @@ static int rfd77402_wait_for_result(struct rfd77402_data *data)
 	struct i2c_client *client = data->client;
 	int val, ret;
 
-	if (data->irq_en) {
-		reinit_completion(&data->completion);
+	if (data->irq_en)
 		return rfd77402_wait_for_irq(data);
-	}
 
 	/*
 	 * As per RFD77402 datasheet section '3.1.1 Single Measure', the
@@ -204,6 +202,9 @@ static int rfd77402_measure(struct rfd77402_data *data)
 	if (ret < 0)
 		return ret;
 
+	if (data->irq_en)
+		reinit_completion(&data->completion);
+
 	ret = i2c_smbus_write_byte_data(client, RFD77402_CMD_R,
 					RFD77402_CMD_SINGLE |
 					RFD77402_CMD_VALID);
 
---

base-commit: c025f6cf4209e1542ec2afebe49f42bbaf1a5c7b
change-id: 20260304-77402-ec479de1b9f6

Best regards,

Reviewed-by on v1 1/1
#2 lore raw
In reply to Felix Gu
Quoted 35 lines · Felix Gu

In IRQ mode, the completion was being reinitialized after the measurement command had already been sent to the hardware. This created a race condition where the IRQ handler could call complete() before reinit_completion() was executed. Consequently, wait_for_completion_timeout() would fail to see the signal and wait until it timed out.

Move reinit_completion() to occur before the measurement command is triggered to ensure the synchronization primitive is ready to capture the interrupt.

Fixes dc81be96a73a ("iio: proximity: rfd77402: Add interrupt handling support")
---
 drivers/iio/proximity/rfd77402.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/iio/proximity/rfd77402.c +1 -3
index 6afdbfca3e5a..81b8daf17a54 100644
@@ -173,10 +173,8 @@ static int rfd77402_wait_for_result(struct rfd77402_data *data)
        struct i2c_client *client = data->client;
        int val, ret;
 
-       if (data->irq_en) {
-               reinit_completion(&data->completion);
+       if (data->irq_en)
                return rfd77402_wait_for_irq(data);
-       }
 
        /*
         * As per RFD77402 datasheet section '3.1.1 Single Measure', the

drivers/iio/proximity/rfd77402.c static int rfd77402_measure(struct rfd77402_data *data) · line 207

Quoted 6 lines
drivers/iio/proximity/rfd77402.c +3 -0
@@ -204,6 +202,9 @@ static int rfd77402_measure(struct rfd77402_data *data)
        if (ret < 0)
                return ret;
 
+       if (data->irq_en)
+               reinit_completion(&data->completion);
+

Looks good to me.

Regards,
Shrikant

maintainer Applied
#3 lore raw
Quoted 39 lines · On Mon, 9 Mar 2026 22:32:58 +0530 Shrikant <[email protected]> wrote:
Quoted 38 lines · Felix Gu

In IRQ mode, the completion was being reinitialized after the measurement command had already been sent to the hardware. This created a race condition where the IRQ handler could call complete() before reinit_completion() was executed. Consequently, wait_for_completion_timeout() would fail to see the signal and wait until it timed out.

Move reinit_completion() to occur before the measurement command is triggered to ensure the synchronization primitive is ready to capture the interrupt.

Fixes dc81be96a73a ("iio: proximity: rfd77402: Add interrupt handling support")
---
 drivers/iio/proximity/rfd77402.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/iio/proximity/rfd77402.c +1 -3
index 6afdbfca3e5a..81b8daf17a54 100644
@@ -173,10 +173,8 @@ static int rfd77402_wait_for_result(struct rfd77402_data *data)
        struct i2c_client *client = data->client;
        int val, ret;
 
-       if (data->irq_en) {
-               reinit_completion(&data->completion);
+       if (data->irq_en)
                return rfd77402_wait_for_irq(data);
-       }
 
        /*
         * As per RFD77402 datasheet section '3.1.1 Single Measure', the
@@ -204,6 +202,9 @@ static int rfd77402_measure(struct rfd77402_data *data)
        if (ret < 0)
                return ret;
 
Quoted 6 lines

drivers/iio/proximity/rfd77402.c static int rfd77402_measure(struct rfd77402_data *data) · line 207

Quoted 3 lines
drivers/iio/proximity/rfd77402.c +3 -0
+       if (data->irq_en)
+               reinit_completion(&data->completion);
+  

Looks good to me.

Applied to the fixes-togreg branch of iio.git. Note I just sent a pull request on that so will probably rebase after that (hopefully) gets merged into char-misc-linus.

Thanks,

Jonathan

Quoted 2 lines

Regards,
Shrikant