[PATCH 3/4] iio: proximity: aw96103: wait for the configuration load before freeing

From: Arnav Kapoor

Date: Thu Nov 26 2026 - 00:31:21 EST


aw96103_i2c_probe() loads the register configuration with
request_firmware_nowait(), and aw96103_cfg_update() runs later and
writes to the driver data and the chip. Nothing waits for it: if probe
fails after the request (e.g. aw96103_interrupt_init() fails because the
device has no interrupt), or the driver is unbound before the callback
has run, the devm-allocated iio_dev that holds that data is freed first,
and the callback then uses freed memory. The regmap and the vcc supply
it uses are devm-managed as well.

Signal a completion at the end of aw96103_cfg_update() and wait for it
in a devm action added right after the request, so it runs before any
of those resources are released.

Fixes: 07b241262dca ("iio: proximity: aw96103: Add support for aw96103/aw96105 proximity sensor")
Signed-off-by: Arnav Kapoor <kapoorarnav43@xxxxxxxxx>
---
drivers/iio/proximity/aw96103.c | 22 +++++++++++++++++++++-
1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/proximity/aw96103.c b/drivers/iio/proximity/aw96103.c
index 504e92ab9b07..b4aa31a65369 100644
--- a/drivers/iio/proximity/aw96103.c
+++ b/drivers/iio/proximity/aw96103.c
@@ -8,6 +8,7 @@
*/
#include <linux/bits.h>
#include <linux/bitfield.h>
+#include <linux/completion.h>
#include <linux/delay.h>
#include <linux/firmware.h>
#include <linux/i2c.h>
@@ -118,6 +119,8 @@ struct aw96103 {
struct aw_channels_info channels_arr[6];
unsigned int max_channels;
unsigned int chan_en;
+ /* Signalled when the asynchronous configuration load has finished */
+ struct completion cfg_done;
};

static const unsigned int aw96103_reg_default[] = {
@@ -605,7 +608,7 @@ static void aw96103_cfg_update(const struct firmware *fw, void *data)
ret = aw96103_para_loaded(aw96103);
if (ret) {
dev_err(aw96103->dev, "load param error.\n");
- return;
+ goto out;
}
}

@@ -615,6 +618,16 @@ static void aw96103_cfg_update(const struct firmware *fw, void *data)
else
aw96103->channels_arr[i].used = false;
}
+
+out:
+ complete(&aw96103->cfg_done);
+}
+
+static void aw96103_wait_cfg_update(void *data)
+{
+ struct aw96103 *aw96103 = data;
+
+ wait_for_completion(&aw96103->cfg_done);
}

static int aw96103_sw_reset(struct aw96103 *aw96103)
@@ -811,12 +824,19 @@ static int aw96103_i2c_probe(struct i2c_client *i2c)
if (ret)
return ret;

+ init_completion(&aw96103->cfg_done);
ret = request_firmware_nowait(THIS_MODULE, true, "aw96103_0.bin",
aw96103->dev, GFP_KERNEL, aw96103,
aw96103_cfg_update);
if (ret)
return ret;

+ /* The callback uses aw96103, so it must finish before that is freed */
+ ret = devm_add_action_or_reset(aw96103->dev, aw96103_wait_cfg_update,
+ aw96103);
+ if (ret)
+ return ret;
+
ret = aw96103_interrupt_init(indio_dev, i2c);
if (ret)
return ret;
--
2.53.0